{"nl": {"description": "The main server of Gomble company received a log of one top-secret process, the name of which can't be revealed. The log was written in the following format: «[date:time]: message», where for each «[date:time]» value existed not more than 10 lines. All the files were encoded in a very complicated manner, and only one programmer — Alex — managed to decode them. The code was so complicated that Alex needed four weeks to decode it. Right after the decoding process was finished, all the files were deleted. But after the files deletion, Alex noticed that he saved the recordings in format «[time]: message». So, information about the dates was lost. However, as the lines were added into the log in chronological order, it's not difficult to say if the recordings could appear during one day or not. It is possible also to find the minimum amount of days during which the log was written.So, to make up for his mistake Alex has to find the minimum amount of days covered by the log. Note that Alex doesn't have to find the minimum amount of days between the beginning and the end of the logging, he has to find the minimum amount of dates in which records could be done. (See Sample test 2 for further clarifications).We should remind you that the process made not more than 10 recordings in a minute. Consider that a midnight belongs to coming day.", "input_spec": "The first input line contains number n (1 ≤ n ≤ 100). The following n lines contain recordings in format «[time]: message», where time is given in format «hh:mm x.m.». For hh two-digit numbers from 01 to 12 are used, for mm two-digit numbers from 00 to 59 are used, and x is either character «a» or character «p». A message is a non-empty sequence of Latin letters and/or spaces, it doesn't start or end with a space. The length of each message doesn't exceed 20.", "output_spec": "Output one number — the minimum amount of days covered by the log.", "sample_inputs": ["5\n[05:00 a.m.]: Server is started\n[05:00 a.m.]: Rescan initialized\n[01:13 p.m.]: Request processed\n[01:10 p.m.]: Request processed\n[11:40 p.m.]: Rescan completed", "3\n[09:00 a.m.]: User logged in\n[08:00 a.m.]: User logged in\n[07:00 a.m.]: User logged in"], "sample_outputs": ["2", "3"], "notes": "NoteFormally the 12-hour time format is described at: http://en.wikipedia.org/wiki/12-hour_clock. The problem authors recommend you to look through these descriptions before you start with the problem."}, "positive_code": [{"source_code": "D = 1440\nn = gets.to_i\nn.times{$* << ($1.to_i % 12 * 60 + $2.to_i + ($3 == 'a' ? 0 : 720)) % D if gets =~ /^\\[(\\d+):(\\d+) ([ap])/}\n$*.each_index do |i|\n\t$*[i] += D while i >= 1 && $*[i] < $*[i - 1]\n\t$*[i] += D while i >= 10 && $*[i] == $*[i - 10]\nend\np $*[-1] / D + 1\n"}, {"source_code": "n = gets.to_i\nm = (0...n).map do \n h, m, ap = gets.strip.match(/\\[(\\d+):(\\d+) (.).+\\]/)[1..-1]\n (h.to_i % 12 * 60 + m.to_i + (ap == ?a ? 0 : 720)) % 1440\nend\nans = 1\ni = 0\nuntil i + 1 == n\n if m[i] > m[i + 1]\n ans += 1\n i += 1\n elsif m[i] == m[i + 1]\n j = i + 1\n j += 1 until j == n || j - i == 11 || m[j] != m[i]\n if j - i == 11\n ans += 1\n i = j - 1\n else\n i += 1\n end\n else\n i += 1\n end\nend\np ans\n"}, {"source_code": "require 'time'\nn=gets.to_i\ntab=[]; result=1\nn.times{tab< y\n\t\tresult += 1\n\tend\n\ty\nend\n\nputs result\n\n"}], "negative_code": [{"source_code": "n = gets.to_i\nn.times{$* << ($1.to_i * 60 + $2.to_i + ($3 == 'a' ? 0 : 720)) % 1440 if gets =~ /^\\[(\\d+):(\\d+) ([ap])/}\n$*.each_index do |i|\n\t$*[i] += 1440 while i >= 1 && $*[i] < $*[i - 1]\n\t$*[i] += 1440 while i >= 10 && $*[i] <= $*[i - 10]\nend\np $*[-1] / 1440 + 1\n"}, {"source_code": "n = gets.to_i\nhm = (0...n).map do \n h, m, ap = gets.strip.match(/\\[(\\d+):(\\d+) (.).+\\]/)[1..-1]\n [h.to_i % 12 + (ap == ?a ? 0 : 12), m.to_i]\nend\nans = 1\ni = 0\nuntil i == n\n j = i + 1\n until j == n\n if hm[j - 1][0] < 24 && hm[j][0] == 24 || (hm[j - 1] <=> hm[j]) == 1\n ans += 1\n elsif hm[j - 1] == hm[j]\n k = j + 1\n until k == n || k - i == 11 || hm[k] != hm[i]\n k += 1\n end\n if k - i == 11\n ans += 1\n j = k - 1\n break\n end\n end\n j += 1\n end\n i = j\nend\np ans\n"}, {"source_code": "n = gets.to_i\nhm = (0...n).map do \n h, m, ap = gets.strip.match(/\\[(\\d+):(\\d+) (.).+\\]/)[1..-1]\n [h.to_i + (ap == ?a ? 0 : 12), m.to_i]\nend\nans = 1\ni = 0\nuntil i == n\n j = i + 1\n until j == n\n if hm[j - 1][0] < 24 && hm[j][0] == 24 || (hm[j - 1] <=> hm[j]) == 1\n ans += 1\n elsif hm[j - 1] == hm[j]\n k = j + 1\n until k == n || k - i == 11 || hm[k] != hm[i]\n k += 1\n end\n if k - i == 11\n ans += 1\n j = k\n break\n end\n end\n j += 1\n end\n i = j\nend\np ans\n"}, {"source_code": "n = gets.to_i\nhm = (0...n).map do \n h, m, ap = gets.strip.match(/\\[(\\d+):(\\d+) (.).+\\]/)[1..-1]\n [h.to_i % 12 + (ap == ?a ? 0 : 12), m.to_i]\nend\nans = 1\ni = 0\nuntil i == n\n j = i + 1\n until j == n\n if hm[j - 1][0] < 24 && hm[j][0] == 24 || (hm[j - 1] <=> hm[j]) == 1\n ans += 1\n elsif hm[j - 1] == hm[j]\n k = j + 1\n until k == n || k - i == 11 || hm[k] != hm[i]\n k += 1\n end\n if k - i == 1000\n ans += 1\n j = k\n break\n end\n end\n j += 1\n end\n i = j\nend\np ans\n"}, {"source_code": "n = gets.to_i\nhm = (0...n).map do \n h, m, ap = gets.strip.match(/\\[(\\d+):(\\d+) (.).+\\]/)[1..-1]\n [h.to_i % 12 + (ap == ?a ? 0 : 12), m.to_i]\nend\nans = 1\ni = 0\nuntil i == n\n j = i + 1\n until j == n\n if hm[j - 1][0] < 24 && hm[j][0] == 24 || (hm[j - 1] <=> hm[j]) == 1\n ans += 1\n elsif hm[j - 1] == hm[j]\n k = j + 1\n until k == n || k - i == 11 || hm[k] != hm[i]\n k += 1\n end\n if k - i == 11\n ans += 1\n j = k\n break\n end\n end\n j += 1\n end\n i = j\nend\np ans\n"}, {"source_code": "require 'time'\nn=gets.to_i\ntab=[]; result=1\nn.times{tab< y\n\t\tresult += 1\n\tend\n\ty\nend\n\nputs result\n\n"}], "src_uid": "54f8b8e6711ff5c69e0bc38a6e2f4999"} {"nl": {"description": "Salem gave you $$$n$$$ sticks with integer positive lengths $$$a_1, a_2, \\ldots, a_n$$$.For every stick, you can change its length to any other positive integer length (that is, either shrink or stretch it). The cost of changing the stick's length from $$$a$$$ to $$$b$$$ is $$$|a - b|$$$, where $$$|x|$$$ means the absolute value of $$$x$$$.A stick length $$$a_i$$$ is called almost good for some integer $$$t$$$ if $$$|a_i - t| \\le 1$$$.Salem asks you to change the lengths of some sticks (possibly all or none), such that all sticks' lengths are almost good for some positive integer $$$t$$$ and the total cost of changing is minimum possible. The value of $$$t$$$ is not fixed in advance and you can choose it as any positive integer. As an answer, print the value of $$$t$$$ and the minimum cost. If there are multiple optimal choices for $$$t$$$, print any of them.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$1 \\le n \\le 1000$$$) — the number of sticks. The second line contains $$$n$$$ integers $$$a_i$$$ ($$$1 \\le a_i \\le 100$$$) — the lengths of the sticks.", "output_spec": "Print the value of $$$t$$$ and the minimum possible cost. If there are multiple optimal choices for $$$t$$$, print any of them.", "sample_inputs": ["3\n10 1 4", "5\n1 1 2 2 3"], "sample_outputs": ["3 7", "2 0"], "notes": "NoteIn the first example, we can change $$$1$$$ into $$$2$$$ and $$$10$$$ into $$$4$$$ with cost $$$|1 - 2| + |10 - 4| = 1 + 6 = 7$$$ and the resulting lengths $$$[2, 4, 4]$$$ are almost good for $$$t = 3$$$.In the second example, the sticks lengths are already almost good for $$$t = 2$$$, so we don't have to do anything."}, "positive_code": [{"source_code": "gets;a=gets.split.map &:to_i\nc,t=(1..a.max).map{|t|[a.map{|x|[(t-x).abs-1,0].max}.reduce(:+),t]}.min\nputs [t,c]*' '"}, {"source_code": "n = gets.to_i\narr = gets.split(\" \").map{|i| i.to_i }\nsum, n = 1000000, 0\n(arr.min..arr.max).each do |t|\n s = 0\n arr.each { |i|\n s += i - t - 1 if i > t\n s += t - i - 1 if t > i\n }\n if s < sum\n sum = s\n n = t\n end\nend\nputs \"#{n} #{sum}\""}, {"source_code": "stick_num = gets.to_i\nstick_length = gets.split.map(&:to_i)\ncomp_num = 0\nsum_cost = 0\ntotal_cost = 0\nlength = 0\nindex = 0\n\nfor x in stick_length.min..stick_length.max do\n for i in 0..stick_num - 1 do\n if stick_length[i] < x\n comp_num = x - 1\n elsif stick_length[i] > x\n comp_num = x + 1\n else\n next\n end\n sum_cost += (comp_num - stick_length[i]).abs\n end\n if x == stick_length.min || total_cost > sum_cost\n total_cost = sum_cost\n length = x\n end\n sum_cost = 0\nend\n\nputs \"#{length} #{total_cost}\""}, {"source_code": "n = gets.to_i\narr = gets.split.map(&:to_i)\nt = 1\nres = 0\nans = [1e9, 1e9]\n(1..100).each do |t|\n arr.each do |e|\n if (e > t)\n res += e - t - 1\n else\n if (e < t)\n res += t - e - 1\n end\n end\n end\n if (res < ans[1])\n ans = [t, res]\n end\n res = 0\nend\nputs ans.join(' ')"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nans = 10**6\nm = 1\nfor i in 1..100\n ret = 0\n a.each do |num|\n if num > i\n ret += num-i-1\n elsif num < i\n ret += i-num-1\n end\n end\n if ans > ret\n ans = ret\n m = i\n end\nend\nputs \"#{m} #{ans}\""}, {"source_code": "DBG = true\nn = gets.to_i\na = gets.split.map{|z| z.to_i}\n\ndef foo(j, t)\n if j >= t+1\n return j-t-1\n elsif j <= t-1\n return t-1-j\n else\n return 0\n end\nend\n\nsumbest = 99999999\ntbest = -1\nfor t in 2..99\n sum = 0\n for i in 0...n\n sum += foo(a[i], t)\n end\n if sum < sumbest\n sumbest = sum\n tbest = t\n end\nend\nputs \"#{tbest} #{sumbest}\"\n"}, {"source_code": "def lscan; gets.split.map(&:to_i); end\n\nN = gets.to_i\nA = lscan.sort\n\nbest = [1e18, 0]\n\n(1..1000).each do |i|\n m = i\n c = A.reduce(0){|s,e| s += [(e-m).abs-1, 0].max}\n best = [best, [c, m]].min\nend\n\nputs best.reverse*\" \""}, {"source_code": "if File.exists?(\"input\")\n $stdin = File.open(\"input\")\n $stdout = File.open(\"output\",\"w\")\nend\n\nn = gets.to_i\na = gets.chomp.split(\" \").map(&:to_i)\n\n\nsums = [0]*100\n\n(1..100).each do |x|\n\ta.each do |y|\n\t\tsums[x-1] += [(y-x).abs-1,0].max\n\tend\nend\n\n#p sums\nputs \"#{sums.index(sums.min)+1} #{sums.min}\""}], "negative_code": [{"source_code": "n = gets.to_i\narr = gets.split.map(&:to_i)\nt1 = (arr.inject(0.0) { |sum, x| sum += x } / arr.size).ceil\nt2 = (arr.inject(0.0) { |sum, x| sum += x } / arr.size).truncate\nres1 = 0\nres2 = 0\narr.each do |e|\n if (e > t1)\n res1 += e - t1 - 1\n else\n if (e < t1)\n res1 += t1 - e - 1\n end\n end\n if (e > t2)\n res2 += e - t2 - 1\n else\n if (e < t2)\n res2 += t2 - e - 1\n end\n end\nend\nif (res1 < res2)\n puts [t1, res1].join(' ')\nelse\n puts [t2, res2].join(' ')\nend"}, {"source_code": "def lscan; gets.split.map(&:to_i); end\n\nN = gets.to_i\nA = lscan.sort\n\nbest = [1e18, 0]\n\n(0..1000).each do |i|\n m = i\n c = A.reduce(0){|s,e| s += [(e-m).abs-1, 0].max}\n best = [best, [c, m]].min\nend\n\nputs best.reverse*\" \""}, {"source_code": "def lscan; gets.split.map(&:to_i); end\n\nN = gets.to_i\nA = lscan.sort\n\nmid = (A[N/2] + A[(N-1)/2])/2 - 1\n\nbest = [1e9, mid]\n\n(-1..1).each do |i|\n m = mid + i\n c = A.reduce(0){|s,e| s += [(e-m).abs-1, 0].max}\n best = [best, [c, m]].min\nend\n\nputs best.reverse*\" \""}, {"source_code": "def lscan; gets.split.map(&:to_i); end\n\nN = gets.to_i\nA = lscan.sort\n\nmid = (A[N/2] + A[(N-1)/2])/2 - 1\n\nbest = [1e9, mid]\n\n(0..100).each do |i|\n m = i\n c = A.reduce(0){|s,e| s += [(e-m).abs-1, 0].max}\n best = [best, [c, m]].min\nend\n\nputs best.reverse*\" \""}], "src_uid": "bce9ebad1dc8bd5aae516c4ca9e551c0"} {"nl": {"description": "You are given several queries. In the i-th query you are given a single positive integer ni. You are to represent ni as a sum of maximum possible number of composite summands and print this maximum number, or print -1, if there are no such splittings.An integer greater than 1 is composite, if it is not prime, i.e. if it has positive divisors not equal to 1 and the integer itself.", "input_spec": "The first line contains single integer q (1 ≤ q ≤ 105) — the number of queries. q lines follow. The (i + 1)-th line contains single integer ni (1 ≤ ni ≤ 109) — the i-th query.", "output_spec": "For each query print the maximum possible number of summands in a valid splitting to composite summands, or -1, if there are no such splittings.", "sample_inputs": ["1\n12", "2\n6\n8", "3\n1\n2\n3"], "sample_outputs": ["3", "1\n2", "-1\n-1\n-1"], "notes": "Note12 = 4 + 4 + 4 = 4 + 8 = 6 + 6 = 12, but the first splitting has the maximum possible number of summands.8 = 4 + 4, 6 can't be split into several composite summands.1, 2, 3 are less than any composite number, so they do not have valid splittings."}, "positive_code": [{"source_code": "gets.to_i.times{\nx = gets.to_i\n\tputs ([1,2,3,5,7,11].include?(x) ? -1 : x/4-x%2)\n}"}, {"source_code": "gets.to_i.times do\n n = gets.to_i\n x = 0\n if n & 1 == 1 && n >= 9\n n -= 9\n x += 1\n end\n if n % 4 == 2 && n >= 6\n n -= 6\n x += 1\n end\n if n % 4 == 0\n puts x + n / 4\n else\n puts -1\n end\nend\n"}], "negative_code": [], "src_uid": "0c2550b2df0849a62969edf5b73e0ac5"} {"nl": {"description": "Berland annual chess tournament is coming!Organizers have gathered 2·n chess players who should be divided into two teams with n people each. The first team is sponsored by BerOil and the second team is sponsored by BerMobile. Obviously, organizers should guarantee the win for the team of BerOil.Thus, organizers should divide all 2·n players into two teams with n people each in such a way that the first team always wins.Every chess player has its rating ri. It is known that chess player with the greater rating always wins the player with the lower rating. If their ratings are equal then any of the players can win.After teams assignment there will come a drawing to form n pairs of opponents: in each pair there is a player from the first team and a player from the second team. Every chess player should be in exactly one pair. Every pair plays once. The drawing is totally random.Is it possible to divide all 2·n players into two teams with n people each so that the player from the first team in every pair wins regardless of the results of the drawing?", "input_spec": "The first line contains one integer n (1 ≤ n ≤ 100). The second line contains 2·n integers a1, a2, ... a2n (1 ≤ ai ≤ 1000).", "output_spec": "If it's possible to divide all 2·n players into two teams with n people each so that the player from the first team in every pair wins regardless of the results of the drawing, then print \"YES\". Otherwise print \"NO\".", "sample_inputs": ["2\n1 3 2 4", "1\n3 3"], "sample_outputs": ["YES", "NO"], "notes": null}, "positive_code": [{"source_code": "n,*a=$<.read.split.map &:to_i\na.sort!\nputs a[n-1] a[n - 1]\n puts 'YES'\nelse\n puts 'NO'\nend\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i).to_a.sort\n\nif a[n] > a[n - 1]\n puts :YES\nelse\n puts :NO\nend\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i).to_a.sort!\n\nif a[n] > a[n - 1]\n puts 'YES'\nelse\n puts 'NO'\nend\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i).to_a.sort!\n\nif a[n] > a[n - 1]\n puts :YES\nelse\n puts :NO\nend\n"}, {"source_code": "n, a = gets.to_i, gets.split.map(&:to_i).sort!\nputs a[n - 1] < a[n] ? :YES : :NO\n"}, {"source_code": "n, a = gets.to_i, gets.split.map(&:to_i).sort!\nputs a[n - 1] < a[n] ? :YES : :NO\n\n"}, {"source_code": "n = gets.to_i\narray = gets.split.map(&:to_i)\narray.sort!\nputs array[n] > array[n-1] ? 'YES' : 'NO'\n"}], "negative_code": [{"source_code": "n = gets.strip.to_i\na = gets.strip.split.map(&:to_i).sort\ni = 0\nj = n\nans = \"YES\"\nwhile i < n && j < 2 * n\n if a[i] >= a[j]\n ans = \"NO\"\n break\n end\n i += 1\n j += 1\nend\nputs ans\n"}, {"source_code": "n = gets.strip.to_i\na = gets.strip.split.map(&:to_i).sort\nans = \"YES\"\ncount = Array.new(1000, 0)\n(2 * n).times do |i|\n count[a[i]] += 1\n if count[a[i]] > 1\n ans = \"NO\"\n break\n end\nend\nputs ans\n"}, {"source_code": "n, aa = gets.to_i, gets.split.map(&:to_i).sort!\nputs (0...n).map {|i| aa[i] < aa[i + n]}.all? ? :YES : :NO\n\n"}], "src_uid": "7b806b9a402a83a5b8943e7f3436cc9a"} {"nl": {"description": "Ashish has an array $$$a$$$ of consisting of $$$2n$$$ positive integers. He wants to compress $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$. To do this, he first discards exactly $$$2$$$ (any two) elements from $$$a$$$. He then performs the following operation until there are no elements left in $$$a$$$: Remove any two elements from $$$a$$$ and append their sum to $$$b$$$. The compressed array $$$b$$$ has to have a special property. The greatest common divisor ($$$\\mathrm{gcd}$$$) of all its elements should be greater than $$$1$$$.Recall that the $$$\\mathrm{gcd}$$$ of an array of positive integers is the biggest integer that is a divisor of all integers in the array.It can be proven that it is always possible to compress array $$$a$$$ into an array $$$b$$$ of size $$$n-1$$$ such that $$$gcd(b_1, b_2..., b_{n-1}) > 1$$$. Help Ashish find a way to do so.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 10$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \\leq n \\leq 1000$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, \\ldots, a_{2n}$$$ ($$$1 \\leq a_i \\leq 1000$$$) — the elements of the array $$$a$$$.", "output_spec": "For each test case, output $$$n-1$$$ lines — the operations performed to compress the array $$$a$$$ to the array $$$b$$$. The initial discard of the two elements is not an operation, you don't need to output anything about it. The $$$i$$$-th line should contain two integers, the indices ($$$1$$$ —based) of the two elements from the array $$$a$$$ that are used in the $$$i$$$-th operation. All $$$2n-2$$$ indices should be distinct integers from $$$1$$$ to $$$2n$$$. You don't need to output two initially discarded elements from $$$a$$$. If there are multiple answers, you can find any.", "sample_inputs": ["3\n3\n1 2 3 4 5 6\n2\n5 7 9 10\n5\n1 3 3 4 5 90 100 101 2 3"], "sample_outputs": ["3 6\n4 5\n3 4\n1 9\n2 3\n4 5\n6 10"], "notes": "NoteIn the first test case, $$$b = \\{3+6, 4+5\\} = \\{9, 9\\}$$$ and $$$\\mathrm{gcd}(9, 9) = 9$$$.In the second test case, $$$b = \\{9+10\\} = \\{19\\}$$$ and $$$\\mathrm{gcd}(19) = 19$$$.In the third test case, $$$b = \\{1+2, 3+3, 4+5, 90+3\\} = \\{3, 6, 9, 93\\}$$$ and $$$\\mathrm{gcd}(3, 6, 9, 93) = 3$$$."}, "positive_code": [{"source_code": "def solve\n n = gets.strip.to_i\n a = gets.strip.split(' ').map(&:to_i)\n od = []\n ev = []\n a.each_with_index { |e, i|\n od << i + 1 if e.odd?\n ev << i + 1 if e.even?\n }\n i = 0; c = 0\n while (i + 1 < od.size)\n break if c == n - 1\n puts \"#{od[i]} #{od[i + 1]}\"\n i += 2\n c += 1\n end\n i = 0\n while (i + 1 < ev.size)\n break if c == n - 1\n puts \"#{ev[i]} #{ev[i + 1]}\"\n i += 2\n c += 1\n end\nend\n\ngets.strip.to_i.times { solve }\n"}], "negative_code": [], "src_uid": "96fac9f9377bf03e144067bf93716d3d"} {"nl": {"description": "Little Susie listens to fairy tales before bed every day. Today's fairy tale was about wood cutters and the little girl immediately started imagining the choppers cutting wood. She imagined the situation that is described below.There are n trees located along the road at points with coordinates x1, x2, ..., xn. Each tree has its height hi. Woodcutters can cut down a tree and fell it to the left or to the right. After that it occupies one of the segments [xi - hi, xi] or [xi;xi + hi]. The tree that is not cut down occupies a single point with coordinate xi. Woodcutters can fell a tree if the segment to be occupied by the fallen tree doesn't contain any occupied point. The woodcutters want to process as many trees as possible, so Susie wonders, what is the maximum number of trees to fell. ", "input_spec": "The first line contains integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contain pairs of integers xi, hi (1 ≤ xi, hi ≤ 109) — the coordinate and the height of the і-th tree. The pairs are given in the order of ascending xi. No two trees are located at the point with the same coordinate.", "output_spec": "Print a single number — the maximum number of trees that you can cut down by the given rules.", "sample_inputs": ["5\n1 2\n2 1\n5 10\n10 9\n19 1", "5\n1 2\n2 1\n5 10\n10 9\n20 1"], "sample_outputs": ["3", "4"], "notes": "NoteIn the first sample you can fell the trees like that: fell the 1-st tree to the left — now it occupies segment [ - 1;1] fell the 2-nd tree to the right — now it occupies segment [2;3] leave the 3-rd tree — it occupies point 5 leave the 4-th tree — it occupies point 10 fell the 5-th tree to the right — now it occupies segment [19;20] In the second sample you can also fell 4-th tree to the right, after that it will occupy segment [10;19]."}, "positive_code": [{"source_code": "def solution(arr)\n count = 0\n last = -2<<2048\n for i in 0...arr.size-1 do\n if arr[i][0] - arr[i][1] > last\n count += 1\n last = arr[i][0]\n elsif arr[i][0] + arr[i][1] < arr[i+1][0]\n count += 1\n last = arr[i][0] + arr[i][1]\n else\n last = arr[i][0]\n end\n end\n count += 1\nend\n\nn = gets.to_i\narr = []\nn.times do\n arr << gets.split.map(&:to_i)\nend\nputs solution(arr)"}, {"source_code": "n = gets.to_i\ncnt = 0\nleft = -9999999999\n\ntrees = 1.upto(n).map do |_|\n gets.split(' ').map(&:to_i)\nend\n\ntrees.each_with_index do |tree, idx|\n if left < tree[0] - tree[1]\n cnt += 1\n left = tree[0]\n elsif idx == n-1 || tree[0] + tree[1] < trees[idx+1][0]\n cnt += 1\n left = tree[0] + tree[1]\n else\n left = tree[0]\n end\nend\n\nputs cnt"}, {"source_code": "n = gets.to_i\na = n.times.map{gets.split.map(&:to_i)}\nind = a[0][0]\ncnt = 2\n1.upto(n - 2) do |i|\n if a[i][0] - a[i][1] > ind\n ind = a[i][0]\n cnt += 1\n elsif a[i + 1][0] - a[i][1] > a[i][0]\n ind = a[i][0] + a[i][1]\n cnt += 1\n else\n ind = a[i][0]\n end\nend\np [cnt, n].min\n"}, {"source_code": "n = gets.to_i\na = n.times.map{gets.split.map(&:to_i)}\nind = a[0][0]\nif n == 1\n cnt = 1\nelse\n cnt = 2\nend\n1.upto(n - 2) do |i|\n if a[i][0] - a[i][1] > ind\n ind = a[i][0]\n cnt += 1\n elsif a[i + 1][0] - a[i][1] > a[i][0]\n ind = a[i][0] + a[i][1]\n cnt += 1\n else\n ind = a[i][0]\n end\nend\np cnt\n"}, {"source_code": "#!/usr/bin/env ruby\nxh = gets.to_i.times.map{gets.split.map(&:to_i)}\nl = nil\nr = 0\nxh.size.times.each{ |i|\n if l.nil? or l < xh[i][0] - xh[i][1] then\n l = xh[i][0]\n r += 1\n elsif i == xh.size - 1 or xh[i][0] + xh[i][1] < xh[i+1][0] then\n l = xh[i][0] + xh[i][1]\n r += 1\n else\n l = xh[i][0]\n end\n}\nputs r"}, {"source_code": "n = Integer gets\nx = Array.new n\nh = Array.new n\n(0...n).each do |i|\n\tt = gets.split\n\tx[i] = Integer t[0]\n\th[i] = Integer t[1]\nend\n\npx, a = x[0], 1\n(1...n).each do |i|\n\tif i == n - 1\n\t\ta += 1 \n\telsif x[i] - h[i] > px\n\t\tpx = x[i]\n\t\ta += 1\n\telsif x[i] + h[i] < x[i+1] \n\t\ta += 1\n\t\tpx = x[i] + h[i]\n\telse\n\t\tpx = x[i]\n\tend\nend\nputs a"}, {"source_code": "n = gets.to_i\nx = Array.new(n)\nh = Array.new(n)\nn.times do |i|\n x[i], h[i] = gets.split.map(&:to_i)\nend\nans = 2\n(1..n-2).each do |i|\n if x[i] - h[i] > x[i - 1]\n ans += 1\n elsif x[i] + h[i] < x[i + 1] \n ans += 1\n x[i] += h[i]\n end\nend\nputs [ans, n].min"}, {"source_code": "n=gets.to_i\nx=[]\nn.times{\n\ta,b=gets.split.map{|e| e.to_i}\n\tx<<[a,b]\n}\nans=1\nt1=x[0]\nx.shift\n\n\nwhile !x.empty?\n\tt2=x[0]\n\tl=t2[0]-t2[1]\n\tif t1[0]1 then\n\t\t\tt3=x[1]\n\t\t\tr=t2[0]+t2[1]\n\t\t\tif r h\n l = true\n else\n s = true\n r = true\n end\n else\n # revert last one to stand, must be rightfell (always x_n < x_n+1)\n left -= last_height\n fell -= 1\n if x - left > h\n l = true\n else\n s = true\n r = true\n end\n end\n\n left = x # next left\n\n if l == true\n fell += 1\n last_height = 0 # don't care\n else\n if r == true\n fell += 1\n left += h\n last_height = h\n end\n end\n\n # p \"l #{l} s #{s} r #{r}, left = #{left}, last_height = #{last_height}, fell = #{fell}\"\n}\n\nputs fell\n"}], "negative_code": [{"source_code": "def solution(arr)\n count = 0\n last = -2<<2048\n for i in 0...arr.size-1 do\n if arr[i][0] - arr[i][1] > last\n count += 1\n last = arr[i][0]\n elsif arr[i][0] + arr[i][1] < arr[i+1][0]\n count += 1\n last = arr[i][0] + arr[i][1]\n end\n end\n count += 1\nend\n\nn = gets.to_i\narr = []\nn.times do\n arr << gets.split.map(&:to_i)\nend\nputs solution(arr)\n"}, {"source_code": "n = gets.to_i\na = n.times.map{gets.split.map(&:to_i)}\nind = a[0][0]\ncnt = 2\n1.upto(n - 2) do |i|\n if a[i][0] - ind > a[i][1]\n ind = a[i][0]\n cnt += 1\n elsif a[i + 1][0] - a[i][0] > a[i][1]\n ind = a[i][0] + a[i][1]\n cnt += 1\n end\nend\np cnt\n"}, {"source_code": "n = gets.to_i\na = n.times.map{gets.split.map(&:to_i)}\nind = a[0][0]\ncnt = 2\n# p a\n1.upto(n - 2) do |i|\n if a[i][0] - a[i][1] > ind\n ind = a[i][0]\n cnt += 1\n elsif a[i + 1][0] - a[i][1] > a[i][0]\n ind = a[i][0] + a[i][1]\n cnt += 1\n else\n ind = a[i][0]\n end\n # p \"#{a[i]} #{cnt}\"\nend\np cnt\n"}, {"source_code": "n = gets.to_i\na = n.times.map{gets.split.map(&:to_i)}\nputs 2 + a.each_cons(3).count{|x, y, z| y[0] - x[0] > y[1] || z[0] - y[0] > y[1]}\n"}, {"source_code": "n = Integer gets\nx = Array.new n\nh = Array.new n\n(0...n).each do |i|\n\tt = gets.split\n\tx[i] = Integer t[0]\n\th[i] = Integer t[1]\nend\n\npx = x[0]\na = 1\n(1...n).each do |i|\n\tif x[i] - h[i] > px\n\t\tpx = x[i]\n\t\ta += 1\n\telsif i == n - 1\n\t\ta += 1 \n\telsif x[i] + h[i] < x[i+1] \n\t\ta += 1\n\t\tpx = x[i] + h[i]\n\tend\nend\nputs a"}, {"source_code": "n=gets.to_i\nx=[]\nn.times{\n\ta,b=gets.split.map{|e| e.to_i}\n\tx<<[a,a-b]\n\tx<<[a+b,a]\n}\nx.sort!\nans=1\nt1=x[0]\nx.shift\nwhile !x.empty?\n\tt2=x[0]\n\tif t1[0]= a ? d - a : ((a.even? && d.even?) || (a.odd? && d.odd?)) ? 0 : 1\nend"}, {"source_code": "gets.to_i.times{n,k=gets.split.map(&:to_i);p [k-n,n-k&1].max}\n"}, {"source_code": "require 'set'\n\ndef taskA\n a, b = gets.chomp.split(\" \").map(&:to_i)\n puts b > a ? b - a : (a + b) % 2\nend\n\ndef taskB\n a, b, c = gets.chomp.split(\" \").map(&:to_i)\n z, x, y = gets.chomp.split(\" \").map(&:to_i)\n minCX = [c, x].min\n puts (minCX - [0, y - a - c + minCX].max)*2\nend\n\nt = gets.chomp.to_i\nwhile (t-=1) >= 0\n taskA\nend\n"}, {"source_code": "t = gets.chomp.to_i\n\nwhile (t-=1) >= 0\n a, b = gets.chomp.split(\" \").map(&:to_i)\n puts b > a ? b - a : (a + b) % 2\nend"}, {"source_code": "gets.to_i.times{n,k=gets.split.map{|x|x.to_i};p [k-n,n-k&1].max}"}, {"source_code": "gets.to_i.times{n,k=gets.split.map{|x|x.to_i};puts [k-n,(n-k)%2].max}"}, {"source_code": "gets.to_i.times{n,k=gets.split.map{|x|x.to_i};puts [k-n,n-k&1].max}"}], "negative_code": [], "src_uid": "f98d1d426f68241bad437eb221f617f4"} {"nl": {"description": "Mishka got an integer array $$$a$$$ of length $$$n$$$ as a birthday present (what a surprise!).Mishka doesn't like this present and wants to change it somehow. He has invented an algorithm and called it \"Mishka's Adjacent Replacements Algorithm\". This algorithm can be represented as a sequence of steps: Replace each occurrence of $$$1$$$ in the array $$$a$$$ with $$$2$$$; Replace each occurrence of $$$2$$$ in the array $$$a$$$ with $$$1$$$; Replace each occurrence of $$$3$$$ in the array $$$a$$$ with $$$4$$$; Replace each occurrence of $$$4$$$ in the array $$$a$$$ with $$$3$$$; Replace each occurrence of $$$5$$$ in the array $$$a$$$ with $$$6$$$; Replace each occurrence of $$$6$$$ in the array $$$a$$$ with $$$5$$$; $$$\\dots$$$ Replace each occurrence of $$$10^9 - 1$$$ in the array $$$a$$$ with $$$10^9$$$; Replace each occurrence of $$$10^9$$$ in the array $$$a$$$ with $$$10^9 - 1$$$. Note that the dots in the middle of this algorithm mean that Mishka applies these replacements for each pair of adjacent integers ($$$2i - 1, 2i$$$) for each $$$i \\in\\{1, 2, \\ldots, 5 \\cdot 10^8\\}$$$ as described above.For example, for the array $$$a = [1, 2, 4, 5, 10]$$$, the following sequence of arrays represents the algorithm: $$$[1, 2, 4, 5, 10]$$$ $$$\\rightarrow$$$ (replace all occurrences of $$$1$$$ with $$$2$$$) $$$\\rightarrow$$$ $$$[2, 2, 4, 5, 10]$$$ $$$\\rightarrow$$$ (replace all occurrences of $$$2$$$ with $$$1$$$) $$$\\rightarrow$$$ $$$[1, 1, 4, 5, 10]$$$ $$$\\rightarrow$$$ (replace all occurrences of $$$3$$$ with $$$4$$$) $$$\\rightarrow$$$ $$$[1, 1, 4, 5, 10]$$$ $$$\\rightarrow$$$ (replace all occurrences of $$$4$$$ with $$$3$$$) $$$\\rightarrow$$$ $$$[1, 1, 3, 5, 10]$$$ $$$\\rightarrow$$$ (replace all occurrences of $$$5$$$ with $$$6$$$) $$$\\rightarrow$$$ $$$[1, 1, 3, 6, 10]$$$ $$$\\rightarrow$$$ (replace all occurrences of $$$6$$$ with $$$5$$$) $$$\\rightarrow$$$ $$$[1, 1, 3, 5, 10]$$$ $$$\\rightarrow$$$ $$$\\dots$$$ $$$\\rightarrow$$$ $$$[1, 1, 3, 5, 10]$$$ $$$\\rightarrow$$$ (replace all occurrences of $$$10$$$ with $$$9$$$) $$$\\rightarrow$$$ $$$[1, 1, 3, 5, 9]$$$. The later steps of the algorithm do not change the array.Mishka is very lazy and he doesn't want to apply these changes by himself. But he is very interested in their result. Help him find it.", "input_spec": "The first line of the input contains one integer number $$$n$$$ ($$$1 \\le n \\le 1000$$$) — the number of elements in Mishka's birthday present (surprisingly, an array). The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$) — the elements of the array.", "output_spec": "Print $$$n$$$ integers — $$$b_1, b_2, \\dots, b_n$$$, where $$$b_i$$$ is the final value of the $$$i$$$-th element of the array after applying \"Mishka's Adjacent Replacements Algorithm\" to the array $$$a$$$. Note that you cannot change the order of elements in the array.", "sample_inputs": ["5\n1 2 4 5 10", "10\n10000 10 50605065 1 5 89 5 999999999 60506056 1000000000"], "sample_outputs": ["1 1 3 5 9", "9999 9 50605065 1 5 89 5 999999999 60506055 999999999"], "notes": "NoteThe first example is described in the problem statement."}, "positive_code": [{"source_code": "gets;puts gets.split.map{|_|n=_.to_i;n+n%2-1}*' '"}, {"source_code": "def solution\n gets\n as = read_ints\n\n final = as.map { |x| x.odd? ? x : x - 1 }\n puts final.join(' ')\n\nend\n\ndef read_ints\n gets.split.map(&:to_i)\nend\n\nsolution"}, {"source_code": "gets\nputs gets.split.map{|c| (c.to_i-1)/2*2+1}.join(' ')"}, {"source_code": "ary_size = gets.to_i\n\ngets.split(\" \").each do |number|\n number = number.to_i\n number = number % 2 === 0 ? (number-1) : number\n space = ary_size === 1 ? \"\" : \" \"\n print \"#{number}#{space}\"\n ary_size -= 1\nend\nSTDOUT.flush\n"}, {"source_code": "n = gets.to_i\nas = gets.split.map(&:to_i)\nputs as.map { |a| a.even? ? a - 1 : a }.join(\" \")"}, {"source_code": "x = gets.chomp.to_i\ny = gets.strip.split(' ').map(&:to_i)\n\ny.each do |x|\n if (x%2 == 0)\n print \"#{x-1} \"\n else\n print \"#{x} \"\n end\nend"}, {"source_code": "gets\nputs(gets.split.map{|x| x.to_i}.map{|x| x%2 == 0 ? x - 1 : x}.join(' '))\n"}, {"source_code": "t = gets.to_i\narr = gets.chomp.split(\" \").map(&:to_i)\n\narr.each do |x|\n if x%2==0\n print x-1\n else\n print x\n end\n print ' '\nend"}, {"source_code": "gets;puts gets.split.map{|_|n=_.to_i;n+n%2-1}*' '"}, {"source_code": "n=gets.to_i\na=gets.split\nlen=a.length\ni=0\nwhile i Array.new(n, false),\n 1 => Array.new(n, false)\n}\nqueries.each do |query|\n row = query[0] - 1\n col = query[1] - 1\n was_forbidden = is_forbidden[row][col]\n\n for r in (row - 1..row + 1)\n for c in (col - 1..col + 1)\n next if r == row\n next unless r >= 0 && r <= 1 && c >= 0 && c < n\n\n if is_forbidden[r][c] == true\n if was_forbidden\n bad_nei -= 1\n else\n bad_nei += 1\n end\n end\n\n end\n end\n\n is_forbidden[row][col] = !is_forbidden[row][col]\n\n if bad_nei >= 1\n puts 'NO'\n else\n puts 'YES'\n end\nend\n"}, {"source_code": "def read_array(to)\n gets.strip.split(' ').map(&to)\nend\n\nn, q = read_array(:to_i)\n\ngrid = [\n Array.new(n, 0),\n Array.new(n, 0),\n]\n\ndef flip(n)\n 1 - n\nend\n\nblocked = 0\n\nq.times do\n r, c = read_array(:to_i).map { |x| x - 1 }\n grid[r][c] = flip(grid[r][c])\n\n if grid[r][c] == 1\n [-1, 0, 1].select { |x| (c + x).between?(0, n -1) && grid[flip(r)][c + x] == 1 }.each do |x|\n blocked += 1\n end\n else\n [-1, 0, 1].select { |x| (c + x).between?(0, n -1) && grid[flip(r)][c + x] == 1 }.each do |x|\n blocked -= 1\n end\n end\n\n puts blocked == 0 ? \"Yes\" : \"No\"\nend"}, {"source_code": "def read_array(to)\n gets.strip.split(' ').map(&to)\nend\n\nn, q = read_array(:to_i)\n# top = {}\n# bottom = {}\n\ntrack = [\n {},\n {}\n]\ngrid = [\n Array.new(n, 0),\n Array.new(n, 0),\n]\n\ndef flip(n)\n 1 - n\nend\n\nq.times do\n r, c = read_array(:to_i).map { |x| x - 1 }\n grid[r][c] = flip(grid[r][c])\n\n if grid[r][c] == 1\n [-1, 0, 1].select { |x| (c + x).between?(0, n -1) && grid[flip(r)][c + x] == 1 }.each do |x|\n track[r][c] = (track[r][c] || []) + [c + x]\n track[flip(r)][c + x] = (track[flip(r)][c + x] || []) + [c]\n end\n else\n [-1, 0, 1].select { |x| (c + x).between?(0, n -1) && grid[flip(r)][c + x] == 1 }.each do |x|\n track[r][c] = track[r][c] - [c + x]\n track[flip(r)][c + x] = track[flip(r)][c + x] - [c]\n track[r].delete(c) if track[r][c].empty?\n track[flip(r)].delete(c + x) if track[flip(r)][c + x].empty?\n end\n end\n\n puts track[0].size > 0 ? \"No\" : \"Yes\"\nend"}], "negative_code": [], "src_uid": "af036416721694d1843368988ca78e8e"} {"nl": {"description": "Innokentiy decides to change the password in the social net \"Contact!\", but he is too lazy to invent a new password by himself. That is why he needs your help. Innokentiy decides that new password should satisfy the following conditions: the length of the password must be equal to n, the password should consist only of lowercase Latin letters, the number of distinct symbols in the password must be equal to k, any two consecutive symbols in the password must be distinct. Your task is to help Innokentiy and to invent a new password which will satisfy all given conditions. ", "input_spec": "The first line contains two positive integers n and k (2 ≤ n ≤ 100, 2 ≤ k ≤ min(n, 26)) — the length of the password and the number of distinct symbols in it. Pay attention that a desired new password always exists.", "output_spec": "Print any password which satisfies all conditions given by Innokentiy.", "sample_inputs": ["4 3", "6 6", "5 2"], "sample_outputs": ["java", "python", "phphp"], "notes": "NoteIn the first test there is one of the appropriate new passwords — java, because its length is equal to 4 and 3 distinct lowercase letters a, j and v are used in it.In the second test there is one of the appropriate new passwords — python, because its length is equal to 6 and it consists of 6 distinct lowercase letters.In the third test there is one of the appropriate new passwords — phphp, because its length is equal to 5 and 2 distinct lowercase letters p and h are used in it.Pay attention the condition that no two identical symbols are consecutive is correct for all appropriate passwords in tests. "}, "positive_code": [{"source_code": "#!/usr/bin/ruby\nn,k=gets.split.map &:to_i\nb=[*'a'..'z'][0,k]\na=[]\na+=b while a.size 0\n if n == k\n myAlphabet.each do |letter|\n password = password + letter\n end\n n -= k\n elsif n < k\n myAlphabet = myAlphabet[0,n]\n myAlphabet.each do |letter|\n password = password + letter\n end\n n -= k\n else\n myAlphabet.each do |letter|\n password = password + letter\n end\n n -= k\n end\nend\n\nputs password\n"}], "negative_code": [], "src_uid": "39f5e934bf293053246bd3faa8061c3b"} {"nl": {"description": "HDD hard drives group data by sectors. All files are split to fragments and each of them are written in some sector of hard drive. Note the fragments can be written in sectors in arbitrary order.One of the problems of HDD hard drives is the following: the magnetic head should move from one sector to another to read some file.Find the time need to read file split to n fragments. The i-th sector contains the fi-th fragment of the file (1 ≤ fi ≤ n). Note different sectors contains the different fragments. At the start the magnetic head is in the position that contains the first fragment. The file are reading in the following manner: at first the first fragment is read, then the magnetic head moves to the sector that contains the second fragment, then the second fragment is read and so on until the n-th fragment is read. The fragments are read in the order from the first to the n-th.It takes |a - b| time units to move the magnetic head from the sector a to the sector b. Reading a fragment takes no time.", "input_spec": "The first line contains a positive integer n (1 ≤ n ≤ 2·105) — the number of fragments. The second line contains n different integers fi (1 ≤ fi ≤ n) — the number of the fragment written in the i-th sector.", "output_spec": "Print the only integer — the number of time units needed to read the file.", "sample_inputs": ["3\n3 1 2", "5\n1 3 5 4 2"], "sample_outputs": ["3", "10"], "notes": "NoteIn the second example the head moves in the following way: 1->2 means movement from the sector 1 to the sector 5, i.e. it takes 4 time units 2->3 means movement from the sector 5 to the sector 2, i.e. it takes 3 time units 3->4 means movement from the sector 2 to the sector 4, i.e. it takes 2 time units 4->5 means movement from the sector 4 to the sector 3, i.e. it takes 1 time units So the answer to the second example is 4 + 3 + 2 + 1 = 10."}, "positive_code": [{"source_code": "gets\np gets.split.map(&:to_i).each_with_index.sort_by(&:first).each_cons(2).map{|a,b|(b.last-a.last).abs}.inject(:+)||0\n"}, {"source_code": "gets\np gets.split.map(&:to_i).each_with_index.sort_by(&:first).each_cons(2).map{|a,b|(b.last-a.last).abs}.inject(:+)||0\n"}, {"source_code": "n = gets.strip.to_i\nar = gets.strip.split.map(&:to_i)\n\ntb = Array.new(n)\n\nn.times do |i|\n\ttb[ar[i]-1] = i+1\nend\n\nif n == 1\n\tputs 0\nelse\n\tputs tb.each_cons(2).map{|a,b| (a-b).abs}.reduce(:+)\nend "}, {"source_code": "gets\np gets.split.map(&:to_i).each_with_index.sort_by(&:first).each_cons(2).map{|a,b|(b.last-a.last).abs}.inject(:+)||0\n"}, {"source_code": "gets\np gets.split.map(&:to_i).each_with_index.sort_by(&:first).each_cons(2).map{|a,b|(b.last-a.last).abs}.inject(:+)||0\n"}, {"source_code": "gets\na = gets.split.map(&:to_i)\na = a.each_with_index.to_a.sort_by(&:first).map(&:last)\ndist = 0\n(1...a.size).each do |i|\n dist += (a[i] - a[i - 1]).abs\nend\nputs dist\n"}, {"source_code": "gets\np gets.split.map(&:to_i).each_with_index.sort_by(&:first).each_cons(2).map{|a,b|(b.last-a.last).abs}.inject(:+)||0\n"}, {"source_code": "gets\np gets.split.map(&:to_i).each_with_index.sort_by(&:first).each_cons(2).map{|a,b|(b.last-a.last).abs}.inject(:+)||0\n"}, {"source_code": "gets\np gets.split.map(&:to_i).each_with_index.sort_by(&:first).each_cons(2).map{|a,b|(b.last-a.last).abs}.inject(:+)||0"}, {"source_code": "class Solver\n\tdef main\n\t\tn = gets.to_i\n\t\tf = gets.split.map { |x| x.to_i }\n\t\tpos = Array.new(n + 1, 0)\n\t\tf.each.with_index do |x, i|\n\t\t\tpos[x] = i\n\t\tend\n\t\tans = 0\n\t\t1.upto(n - 1) do |i|\n\t\t\tans += (pos[i] - pos[i + 1]).abs\n\t\tend\n\t\tputs ans\n\tend\nend\nSolver.new.main"}, {"source_code": "gets\np gets.split.map(&:to_i).each_with_index.sort_by(&:first).each_cons(2).map{|a,b|(b.last-a.last).abs}.inject(:+)||0\n"}], "negative_code": [{"source_code": "n = gets.strip.to_i\nar = gets.strip.split.map(&:to_i)\n\ntb = Array.new(n)\n\nn.times do |i|\n\ttb[ar[i]-1] = i+1\nend\n\nputs tb.each_cons(2).map{|a,b| (a-b).abs}.reduce(:+)"}, {"source_code": "n = gets.strip.to_i\nputs n\nar = gets.strip.split.map(&:to_i)\n\ntb = Array.new(ar.size)\n\nar.size.times do |i|\n\ttb[ar[i]-1] = i+1\nend\n\nputs tb.each_cons(2).map{|a,b| a-b}.each.map(&:abs).reduce(:+)"}, {"source_code": "n = gets.strip.to_i\nar = gets.strip.split.map(&:to_i)\n\ntb = Array.new(ar.size)\n\nar.size.times do |i|\n\ttb[ar[i]-1] = i+1\nend\n\nputs tb.each_cons(2).map{|a,b| a-b}.each.map(&:abs).reduce(:+)"}, {"source_code": "n = gets.strip.to_i\nar = gets.strip.split.map(&:to_i)\n\ntb = Array.new(n)\n\nn.times do |i|\n\ttb[ar[i]-1] = i+1\nend\n\na = tb.each_cons(2).map{|a,b| (a-b).abs}.reduce(:+)\na = 1 if a== 0\nputs a"}, {"source_code": "n = gets.strip.to_i\nar = gets.strip.split.map(&:to_i)\n\ntb = Array.new(n)\n\nn.times do |i|\n\ttb[ar[i]-1] = i+1\nend\n\nif n == 1\n\tputs 1\nelse\n\tputs tb.each_cons(2).map{|a,b| (a-b).abs}.reduce(:+)\nend "}, {"source_code": "gets\np gets.split.map(&:to_i).each_with_index.sort_by(&:first).each_cons(2).map{|a,b|(b.last-a.last).abs}.inject(:+)"}], "src_uid": "54e2b6bea0dc6ee68366405945af50c6"} {"nl": {"description": "Valera's finally decided to go on holiday! He packed up and headed for a ski resort.Valera's fancied a ski trip but he soon realized that he could get lost in this new place. Somebody gave him a useful hint: the resort has n objects (we will consider the objects indexed in some way by integers from 1 to n), each object is either a hotel or a mountain.Valera has also found out that the ski resort had multiple ski tracks. Specifically, for each object v, the resort has at most one object u, such that there is a ski track built from object u to object v. We also know that no hotel has got a ski track leading from the hotel to some object.Valera is afraid of getting lost on the resort. So he wants you to come up with a path he would walk along. The path must consist of objects v1, v2, ..., vk (k ≥ 1) and meet the following conditions: Objects with numbers v1, v2, ..., vk - 1 are mountains and the object with number vk is the hotel. For any integer i (1 ≤ i < k), there is exactly one ski track leading from object vi. This track goes to object vi + 1. The path contains as many objects as possible (k is maximal). Help Valera. Find such path that meets all the criteria of our hero!", "input_spec": "The first line contains integer n (1 ≤ n ≤ 105) — the number of objects. The second line contains n space-separated integers type1, type2, ..., typen — the types of the objects. If typei equals zero, then the i-th object is the mountain. If typei equals one, then the i-th object is the hotel. It is guaranteed that at least one object is a hotel. The third line of the input contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ n) — the description of the ski tracks. If number ai equals zero, then there is no such object v, that has a ski track built from v to i. If number ai doesn't equal zero, that means that there is a track built from object ai to object i.", "output_spec": "In the first line print k — the maximum possible path length for Valera. In the second line print k integers v1, v2, ..., vk — the path. If there are multiple solutions, you can print any of them.", "sample_inputs": ["5\n0 0 0 0 1\n0 1 2 3 4", "5\n0 0 1 0 1\n0 1 2 2 4", "4\n1 0 0 0\n2 3 4 2"], "sample_outputs": ["5\n1 2 3 4 5", "2\n4 5", "1\n1"], "notes": null}, "positive_code": [{"source_code": "objn = gets.to_i\nobjtypes = gets.split.map(&:to_i)\nobjtypes.unshift(0)\npaths = gets.split.map(&:to_i)\npaths.unshift(0)\nhotels = []\npathscnt = Array::new(objn+1, 0)\npaths.each_with_index{|p, i| \n\tpathscnt[p] += 1\n\thotels << i if objtypes[i] == 1\n}\nbestpath = []\ncurpath = []\nhotels.each{|h|\n\tcurpath << h\n\tprev = paths[h]\n\twhile prev != 0 && pathscnt[prev] == 1 && objtypes[prev] == 0\n\t\tcurpath << prev\n\t\tprev = paths[prev]\n\tend\n\tbestpath.replace(curpath) if bestpath.size < curpath.size\n\tcurpath.replace([])\n}\n\nputs bestpath.size\nputs bestpath.reverse.join(\" \")"}, {"source_code": "N = gets.chomp.to_i\nT = gets.split.map &:to_i\nA = gets.split.map &:to_i\n\nadj = Array.new(N) { Array.new }\nradj = Array.new(N) { Array.new }\nN.times { |i| next if A[i] == 0; adj[A[i] - 1] << i; radj[i] << A[i] - 1 }\n\nmax_len, best_st = -1, nil\nN.times { |i| next if T[i] == 0\n que = [[1, i]]\n until que.size == 0\n d, u = que[0]\n que.shift\n if max_len < d\n max_len, best_st = d, u\n end\n radj[u].each { |v| next if T[v] == 1 or adj[v].size > 1\n que << [d + 1, v]\n }\n end\n}\n\nans = [best_st].tap { |a| loop { break if T[a[-1]] == 1; a << adj[a[-1]][0] } }\nputs ans.size\nputs ans.map(&:next).join ' '\n"}], "negative_code": [], "src_uid": "a704760d5ccd09d08558ea98d6007835"} {"nl": {"description": "The legendary Farmer John is throwing a huge party, and animals from all over the world are hanging out at his house. His guests are hungry, so he instructs his cow Bessie to bring out the snacks! Moo!There are $$$n$$$ snacks flavors, numbered with integers $$$1, 2, \\ldots, n$$$. Bessie has $$$n$$$ snacks, one snack of each flavor. Every guest has exactly two favorite flavors. The procedure for eating snacks will go as follows: First, Bessie will line up the guests in some way. Then in this order, guests will approach the snacks one by one. Each guest in their turn will eat all remaining snacks of their favorite flavor. In case no favorite flavors are present when a guest goes up, they become very sad. Help Bessie to minimize the number of sad guests by lining the guests in an optimal way.", "input_spec": "The first line contains integers $$$n$$$ and $$$k$$$ ($$$2 \\le n \\le 10^5$$$, $$$1 \\le k \\le 10^5$$$), the number of snacks and the number of guests. The $$$i$$$-th of the following $$$k$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \\le x_i, y_i \\le n$$$, $$$x_i \\ne y_i$$$), favorite snack flavors of the $$$i$$$-th guest.", "output_spec": "Output one integer, the smallest possible number of sad guests.", "sample_inputs": ["5 4\n1 2\n4 3\n1 4\n3 4", "6 5\n2 3\n2 1\n3 4\n6 5\n4 5"], "sample_outputs": ["1", "0"], "notes": "NoteIn the first example, Bessie can order the guests like this: $$$3, 1, 2, 4$$$. Guest $$$3$$$ goes first and eats snacks $$$1$$$ and $$$4$$$. Then the guest $$$1$$$ goes and eats the snack $$$2$$$ only, because the snack $$$1$$$ has already been eaten. Similarly, the guest $$$2$$$ goes up and eats the snack $$$3$$$ only. All the snacks are gone, so the guest $$$4$$$ will be sad. In the second example, one optimal ordering is $$$2, 1, 3, 5, 4$$$. All the guests will be satisfied."}, "positive_code": [{"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\ndef bfs(d)\n st = [d]\n @memo[d] = true\n while st.size > 0\n d = st.pop\n @m[d].each do |a|\n next if(@memo2[a[1]])\n if(@memo[a[0]])\n @ans += 1\n @memo2[a[1]] = true\n next\n end\n @memo[a[0]] = true\n @memo2[a[1]] = true\n st.push(a[0])\n end\n end\nend\n\nn,k = inp\nt = []\n@ans = 0\n@memo = na(n+2,false)\n@memo2 = na(n+2,false)\nk.times{t << inp.sort}\nt.sort!()\n@m = na2(n+2,0)\nd = []\nt.each.with_index do |a,i|\n @m[a[0]].push([a[1],i])\n @m[a[1]].push([a[0],i])\n d.push([a[0],a[1],i])\nend\n(1..n).each do |d|\n next if(@memo[d])\n @memo[d] = true\n bfs(d)\nend\np @ans"}], "negative_code": [{"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\ndef bfs(t)\n return if(@memo2[t[1]])\n @ans+=1 if(@memo[t[0]])\n @memo[t[0]] = true\n @memo2[t[1]] = true\n @m[t[0]].each do |a|\n bfs(a)\n end\nend\nn,k = inp\nt = []\n@ans = 0\n@memo = na(n+1,false)\n@memo2 = na(n+1,false)\nk.times{t << inp}\n@m = na2(n+1,0)\nt.each.with_index do |a,i|\n @m[a[0]].push([a[1],i])\n @m[a[1]].push([a[0],i])\nend\n@m.each do |a|\n next if(a == [])\n a.each do |aa|\n bfs(aa)\n end\nend\np @ans"}, {"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\ndef bfs(d,i,f)\n t = [d,i]\n return if(@memo2[t[1]])\n @ans += 1 if(@memo[t[0]])\n @memo2[t[1]] = true\n @memo[t[0]] = true\n @f = true if(f)\n @m[t[0]].each do |a|\n bfs(a[0],a[1],true)\n end\nend\n\nn,k = inp\nt = []\n@ans = 0\n@memo = na(n+2,false)\n@memo2 = na(n+2,false)\nk.times{t << inp.sort}\nt.sort!()\n@m = na2(n+2,0)\nd = []\nt.each.with_index do |a,i|\n @m[a[0]].push([a[1],i])\n @m[a[1]].push([a[0],i])\n d.push([a[0],a[1],i])\nend\nd.each do |a|\n @f = false\n bfs(a[0],a[2],false)\nend\np @ans"}, {"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\ndef bfs(d,i,f)\n t = [d,i]\n return if(@memo2[t[1]])\n @ans += 1 if(@memo[t[0]])\n @memo2[t[1]] = true\n @memo[t[0]] = true\n @f = true if(f)\n @m[t[0]].each do |a|\n bfs(a[0],a[1],true)\n end\nend\n\nn,k = inp\nt = []\n@ans = 0\n@memo = na(n+2,false)\n@memo2 = na(n+2,false)\nk.times{t << inp.sort}\nt.sort!()\n@m = na2(n+2,0)\nd = []\nt.each.with_index do |a,i|\n @m[a[0]].push([a[1],i])\n @m[a[1]].push([a[0],i])\n d.push([a[0],a[1],i])\nend\nd.each do |a|\n @f = false\n bfs(a[0],a[2],false)\n @memo2[a[2]] = false if(@f== false and @memo[a[1]] = false)\n bfs(a[1],a[2],false)\n a[0] = a[1] = true\nend\np @ans"}, {"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\ndef bfs(d,i)\n t = [d,i]\n return if(i >= 0 and @memo2[t[1]])\n [@ans += 1,t] if(i >= 0 and @memo[t[0]])\n @memo2[t[1]] = true\n @memo[t[0]] = true\n @m[t[0]].each do |a|\n bfs(a[0],a[1])\n end\nend\n\nn,k = inp\nt = []\n@ans = 0\n@memo = na(n+2,false)\n@memo2 = na(n+2,false)\nk.times{t << inp.sort}\nt.sort!()\n@m = na2(n+2,0)\nd = []\nt.each.with_index do |a,i|\n @m[a[0]].push([a[1],i])\n @m[a[1]].push([a[0],i])\n d.push([a[0],a[1],i])\nend\n(1..n).each do |d|\n bfs(d,-1)\nend\np @ans"}, {"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\n\nn,k = inp\nt = []\nans = 0\nk.times{t << inp.sort}\nt.sort!\nm = na(n+1,false)\nt.each do |a|\n ans += 1 if(m[a[0]] and m[a[1]])\n m[a[0]] = m[a[1]] = true\nend\np ans"}, {"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\ndef bfs(d,i)\n t = [d,i]\n return if(@memo2[t[1]])\n @ans += 1 if(@memo[t[0]])\n @memo2[t[1]] = true\n @memo[t[0]] = true\n\n @m[t[0]].each do |a|\n bfs(a[0],a[1])\n end\nend\n\nn,k = inp\nt = []\n@ans = 0\n@memo = na(n+2,false)\n@memo2 = na(n+2,false)\nk.times{t << inp.sort}\nt.sort!()\n@m = na2(n+2,0)\nd = []\nt.each.with_index do |a,i|\n @m[a[0]].push([a[1],i])\n @m[a[1]].push([a[0],i])\n d.push([a[0],a[1],i])\nend\nd.each do |a|\n bfs(a[0],a[2])\n bfs(a[1],a[2])\n a[0] = a[1] = true\nend\np @ans"}, {"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\ndef bfs(d,i)\n t = [d,i]\n return if(@memo2[t[1]])\n @ans += 1 if(@memo[t[0]])\n @memo2[t[1]] = true\n return if(@memo[t[0]])\n @memo[t[0]] = true\n\n @m[t[0]].each do |a|\n bfs(a[0],a[1])\n end\nend\n\nn,k = inp\nt = []\n@ans = 0\n@memo = na(n+2,false)\n@memo2 = na(n+2,false)\nk.times{t << inp.sort}\nt.sort!()\n@m = na2(n+2,0)\nd = []\nt.each.with_index do |a,i|\n @m[a[0]].push([a[1],i])\n @m[a[1]].push([a[0],i])\n d.push([a[0],a[1]])\nend\n@m.each do |a|\n a.each do |aa|\n bfs(aa[0],aa[1])\n end\nend\np @ans"}], "src_uid": "a7d68ecc1a9ee23cf98f51fe6651ae13"} {"nl": {"description": "A positive integer number n is written on a blackboard. It consists of not more than 105 digits. You have to transform it into a beautiful number by erasing some of the digits, and you want to erase as few digits as possible.The number is called beautiful if it consists of at least one digit, doesn't have leading zeroes and is a multiple of 3. For example, 0, 99, 10110 are beautiful numbers, and 00, 03, 122 are not.Write a program which for the given n will find a beautiful number such that n can be transformed into this number by erasing as few digits as possible. You can erase an arbitraty set of digits. For example, they don't have to go one after another in the number n.If it's impossible to obtain a beautiful number, print -1. If there are multiple answers, print any of them.", "input_spec": "The first line of input contains n — a positive integer number without leading zeroes (1 ≤ n < 10100000).", "output_spec": "Print one number — any beautiful number obtained by erasing as few as possible digits. If there is no answer, print  - 1.", "sample_inputs": ["1033", "10", "11"], "sample_outputs": ["33", "0", "-1"], "notes": "NoteIn the first example it is enough to erase only the first digit to obtain a multiple of 3. But if we erase the first digit, then we obtain a number with a leading zero. So the minimum number of digits to be erased is two."}, "positive_code": [{"source_code": "s = gets.strip\nx = s.each_byte.inject(0) {|z, b| z + b} % 3\nif x != 0 then\n r, sep = '', [/[147]/, /[258]/]\n s.reverse!\n (x == 1 ? sep : sep.reverse).zip([2, 3]) do |sep, le|\n l = s.split(sep, le)\n if l.length == le then\n t = l.join.reverse.sub(/^(?:0+)(?=.)/, '')\n r = t if r.length < t.length\n end\n end\n s = r.empty? ? '-1' : r\nend\nputs s"}, {"source_code": "$num = gets.strip\n$rnum = $num.reverse\n\np3c = [0]*3\nfor digit in $num.each_char do\n p3c[digit.to_i % 3] += 1\nend\n\nnump3 = (p3c[1] + 2*p3c[2]) % 3\n$ans = []\n\ndef add_ans(delp1, delp2)\n cans = []\n for digit in $rnum.each_char do\n #p digit\n p3d = digit.to_i % 3\n if (p3d == 1) and (delp1 > 0) \n delp1 -= 1 \n elsif (p3d == 2) and (delp2 > 0) \n delp2 -= 1\n else \n cans << digit \n end\n end\n \n cans.pop while (cans.length > 1) and (cans[-1] == '0')\n #p cans\n $ans << cans if (cans.length > 0) and (delp1 == 0) and (delp2 == 0)\nend\n\n#p nump3\nif nump3 == 0\n puts $num\n exit\nelsif nump3 == 1\n add_ans(1,0)\n add_ans(0,2)\nelse # if nump3 == 2\n add_ans(2,0)\n add_ans(0,1)\nend\n\nif $ans.empty?\n puts -1\n exit\nend\n\nbest = 0\nfor idx in (1...$ans.length)\n best = idx if $ans[idx].length > $ans[best].length\nend\n\nputs $ans[best].reverse.join ''\n"}], "negative_code": [{"source_code": "s = gets.strip\nx = s.each_byte.inject(0) {|z, b| z + b} % 3\nif x != 0 then\n r, sep = '', [/[147]/, /[258]/]\n s.reverse!\n (x == 1 ? sep : sep.reverse).zip([2, 3]) do |sep, le|\n l = s.split(sep, le)\n if l.length == le then\n t = l.join.reverse.sub(/^(?=0+)./, '')\n r = t if r.length < t.length\n end\n end\n s = r.empty? ? '-1' : r\nend\nputs s\n"}, {"source_code": "$num = gets.strip\n$rnum = $num.reverse\n\np3c = [0]*3\nfor digit in $num.each_char do\n p3c[digit.to_i % 3] += 1\nend\n\nnump3 = (p3c[1] + 2*p3c[2]) % 3\n$ans = []\n\ndef add_ans(delp1, delp2)\n cans = []\n for digit in $rnum.each_char do\n #p digit\n p3d = digit.to_i % 3\n if (p3d == 1) and (delp1 > 0) \n delp1 -= 1 \n elsif (p3d == 2) and (delp2 > 0) \n delp1 -= 2 \n else \n cans << digit \n end\n end\n \n cans.pop while (cans.length > 1) and (cans[-1] == '0')\n #p cans\n $ans << cans if (cans.length > 0) and (delp1 == 0) and (delp2 == 0)\nend\n\nif nump3 == 0\n puts $num\n exit\nelsif nump3 == 1\n add_ans(1,0)\n add_ans(0,2)\nelse # if nump3 == 2\n add_ans(2,0)\n add_ans(0,1)\nend\n\nif $ans.empty?\n puts -1\n exit\nend\n\nbest = 0\nfor idx in (1...$ans.length)\n best = idx if $ans[idx].length > $ans[best].length\nend\n\nputs $ans[best].reverse.join ''\n"}], "src_uid": "df9942d1eb66b1f3b5c6b665b446cd3e"} {"nl": {"description": "The city of Fishtopia can be imagined as a grid of $$$4$$$ rows and an odd number of columns. It has two main villages; the first is located at the top-left cell $$$(1,1)$$$, people who stay there love fishing at the Tuna pond at the bottom-right cell $$$(4, n)$$$. The second village is located at $$$(4, 1)$$$ and its people love the Salmon pond at $$$(1, n)$$$.The mayor of Fishtopia wants to place $$$k$$$ hotels in the city, each one occupying one cell. To allow people to enter the city from anywhere, hotels should not be placed on the border cells.A person can move from one cell to another if those cells are not occupied by hotels and share a side.Can you help the mayor place the hotels in a way such that there are equal number of shortest paths from each village to its preferred pond?", "input_spec": "The first line of input contain two integers, $$$n$$$ and $$$k$$$ ($$$3 \\leq n \\leq 99$$$, $$$0 \\leq k \\leq 2\\times(n-2)$$$), $$$n$$$ is odd, the width of the city, and the number of hotels to be placed, respectively.", "output_spec": "Print \"YES\", if it is possible to place all the hotels in a way that satisfies the problem statement, otherwise print \"NO\". If it is possible, print an extra $$$4$$$ lines that describe the city, each line should have $$$n$$$ characters, each of which is \"#\" if that cell has a hotel on it, or \".\" if not.", "sample_inputs": ["7 2", "5 3"], "sample_outputs": ["YES\n.......\n.#.....\n.#.....\n.......", "YES\n.....\n.###.\n.....\n....."], "notes": null}, "positive_code": [{"source_code": "n, k=gets.split.map(&:to_i)\n\nputs 'YES'\ns=Array.new(4){Array.new(n){'.'}}\nif k%2==0\n (k/2).times do |j|\n s[1][j+1]='#'\n s[2][j+1]='#'\n end\nelse\n if k<=n-2\n l, r=1, n-2\n s[1][n/2]='#'\n (k/2).times do \n s[1][l]=s[1][r]='#'\n l+=1\n r-=1\n end\n else\n (n-2).times do |j|\n s[1][j+1]='#'\n end\n k-=(n-2)\n l, r=1, n-2\n (k/2).times do \n s[2][l]=s[2][r]='#'\n l+=1\n r-=1\n end\n end\nend\n\ns.each do |line|\n puts line.join('')\nend"}], "negative_code": [{"source_code": "n, k=gets.split.map(&:to_i)\n\nputs 'YES'\ns=Array.new(4){Array.new(n){'.'}}\nif k%2==0\n (k/2).times do |j|\n s[1][j+1]='#'\n s[2][j+1]='#'\n end\nelse\n if k<=n-2\n k.times do |j|\n s[1][j+1]='#'\n end\n else\n (n-2).times do |j|\n s[1][j+1]='#'\n end\n k-=(n-2)\n l, r=1, n-2\n (k/2).times do \n s[2][l]=s[2][r]='#'\n l+=1\n r-=1\n end\n end\nend\n\ns.each do |line|\n puts line.join('')\nend"}, {"source_code": "n, k=gets.split.map(&:to_i)\n\nif k%2==1 and n-2=n12\n 2.times do |i22|\n f22=i22==1&&m>1&&x>=n22\n 2.times do |i21|\n f21=i21==1&&x>=n21&&(m>1&&n>1||!f12)\n nn=n\n mm=m\n nn-=1 if f12||f21\n if f22\n mm-=2\n elsif f21||f12\n mm-=1\n end\n (nn/2+1).times do |i|\n (mm/2+1).times do |j|\n if i*n1+j*n2+(f12 ? n12:0)+(f22 ? n22:0)+(f21 ? n21:0)==x\n s1='B'*n\n s2='B'*m\n (s1[-1]='A';s2[0]='C') if f12\n (s2[-1]='A';s2[0]='C') if f22\n (s2[-1]='A';s1[0]='C') if f21\n s1.sub!('B'*(i*2),'AC'*i)\n s2.sub!('B'*(j*2),'AC'*j)\n puts s1,s2\n exit\n end\n end\n end\n end\n end\nend\nputs 'Happy new year!'"}, {"source_code": "k,x,n,m=gets.split.map(&:to_i)\n#k,x,n,m=10,143,4,6\nn1= [1,0,1,1,2]\nn2= [0,1,1,2,3]\nn12=[0,0,1,1,2]\nn22=[0,0,0,0,1]\nn21=[0,0,0,1,1]\nk.times do |i|\n n1<=n12)\n 2.times do |i22|\n x-=n22 if (f22=i22==1&&m>1&&x>=n22)\n 2.times do |i21|\n x-=n21 if (f21=i21==1&&x>=n21&&(m>1&&n>1||!f12))\n nn=n\n mm=m\n nn-=1 if f12||f21\n if f22\n mm-=2\n elsif f21||f12\n mm-=1\n end\n\n (nn/2+1).times do |i|\n (mm/2+1).times do |j|\n if i*n1+j*n2==x\n s1='B'*n\n s2='B'*m\n (s1[-1]='A';s2[0]='C') if f12\n (s2[-1]='A';s2[0]='C') if f22\n (s2[-1]='A';s1[0]='C') if f21\n s1.sub!('B'*(i*2),'AC'*i)\n s2.sub!('B'*(j*2),'AC'*j)\n puts s1,s2\n exit\n end\n end\n end\n\n x+=n21 if f21\n end\n x+=n22 if f22\n end\n x+=n12 if f12\nend\nputs 'Happy new year!'"}], "negative_code": [{"source_code": "#k,x,n,m=gets.split.map(&:to_i)\nk,x,n,m=5,4,3,2\nn1= [1,0,1,1,2]\nn2= [0,1,1,2,3]\nn12=[0,0,1,1,2]\nn22=[0,0,0,0,1]\nn21=[0,0,0,1,1]\nk.times do |i|\n n1<=n12)\n 2.times do |i22|\n x-=n22 if (f22=i22==1&&m>1&&x>=n22)\n 2.times do |i21|\n x-=n21 if (f21=i21==1&&x>=n21&&(!f12))\n nn=n\n mm=m\n nn-=1 if f12||f21\n if f22\n mm-=2\n elsif f21||f12\n mm-=1\n end\n\n (nn/2+1).times do |i|\n (mm/2+1).times do |j|\n if i*n1+j*n2==x\n s1='B'*n\n s2='B'*m\n (s1[-1]='A';s2[0]='C') if f12\n (s2[-1]='A';s2[0]='C') if f22\n (s2[-1]='A';s1[0]='C') if f21\n s1.sub!('B'*(i*2),'AC'*i) if i>0\n s2.sub!('B'*(j*2),'AC'*j) if j>0\n puts s1,s2\n exit\n end\n end\n end\n\n x+=n21 if f21\n end\n x+=n22 if f22\n end\n x+=n12 if f12\nend\nputs 'Happy new year!'"}, {"source_code": "k,x,n,m=gets.split.map(&:to_i)\nn1= [1,0,1,1,2]\nn2= [0,1,1,2,3]\nn12=[0,0,1,1,2]\nn22=[0,0,0,0,1]\nn21=[0,0,0,1,1]\nk.times do |i|\n n1<=n12)\n 2.times do |i22|\n x-=n22 if (f22=i22==1&&m>1&&x>=n22)\n 2.times do |i21|\n x-=n21 if (f21=i21==1&&x>=n21&&(!f12))\n mm=m\n mm-=1 if f12||f21\n nn=n\n if f22\n nn-=2\n elsif f21||f12\n nn-=1\n end\n\n (nn/2+1).times do |i|\n (mm/2+1).times do |j|\n if i*n1+j*n2==x\n s1='B'*n\n s2='B'*m\n (s1[-1]='A';s2[0]='C') if f12\n (s2[-1]='A';s2[0]='C') if f22\n (s2[-1]='A';s1[0]='C') if f21\n s1.sub!('B'*(i*2),'AC'*i) if i>0\n s2.sub!('B'*(j*2),'AC'*j) if j>0\n puts s1,s2\n exit\n end\n end\n end\n\n x+=n21 if f21\n end\n x+=n22 if f22\n end\n x+=n12 if f12\nend\nputs 'Happy new year!'"}, {"source_code": "k,x,n,m=gets.split.map(&:to_i)\n#k,x,n,m=5,4,3,2\nn1= [1,0,1,1,2]\nn2= [0,1,1,2,3]\nn12=[0,0,1,1,2]\nn22=[0,0,0,0,1]\nn21=[0,0,0,1,1]\nk.times do |i|\n n1<=n12)\n 2.times do |i22|\n x-=n22 if (f22=i22==1&&m>1&&x>=n22)\n 2.times do |i21|\n x-=n21 if (f21=i21==1&&x>=n21&&(!f12))\n nn=n\n mm=m\n nn-=1 if f12||f21\n if f22\n mm-=2\n elsif f21||f12\n mm-=1\n end\n\n (nn/2+1).times do |i|\n (mm/2+1).times do |j|\n if i*n1+j*n2==x\n s1='B'*n\n s2='B'*m\n (s1[-1]='A';s2[0]='C') if f12\n (s2[-1]='A';s2[0]='C') if f22\n (s2[-1]='A';s1[0]='C') if f21\n s1.sub!('B'*(i*2),'AC'*i) if i>0\n s2.sub!('B'*(j*2),'AC'*j) if j>0\n puts s1,s2\n exit\n end\n end\n end\n\n x+=n21 if f21\n end\n x+=n22 if f22\n end\n x+=n12 if f12\nend\nputs 'Happy new year!'"}], "src_uid": "1d55d31320368ddb1439ee086d40b57c"} {"nl": {"description": "You are given three positive integers $$$n$$$, $$$a$$$ and $$$b$$$. You have to construct a string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters such that each substring of length $$$a$$$ has exactly $$$b$$$ distinct letters. It is guaranteed that the answer exists.You have to answer $$$t$$$ independent test cases.Recall that the substring $$$s[l \\dots r]$$$ is the string $$$s_l, s_{l+1}, \\dots, s_{r}$$$ and its length is $$$r - l + 1$$$. In this problem you are only interested in substrings of length $$$a$$$.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 2000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of a test case contains three space-separated integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$1 \\le a \\le n \\le 2000, 1 \\le b \\le \\min(26, a)$$$), where $$$n$$$ is the length of the required string, $$$a$$$ is the length of a substring and $$$b$$$ is the required number of distinct letters in each substring of length $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2000$$$ ($$$\\sum n \\le 2000$$$).", "output_spec": "For each test case, print the answer — such a string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters that each substring of length $$$a$$$ has exactly $$$b$$$ distinct letters. If there are multiple valid answers, print any of them. It is guaranteed that the answer exists.", "sample_inputs": ["4\n7 5 3\n6 1 1\n6 6 1\n5 2 2"], "sample_outputs": ["tleelte\nqwerty\nvvvvvv\nabcde"], "notes": "NoteIn the first test case of the example, consider all the substrings of length $$$5$$$: \"tleel\": it contains $$$3$$$ distinct (unique) letters, \"leelt\": it contains $$$3$$$ distinct (unique) letters, \"eelte\": it contains $$$3$$$ distinct (unique) letters. "}, "positive_code": [{"source_code": "gets.to_i.times do\n\tn, a, b = gets.split.map(&:to_i)\n\tstr = ('a'...('a'.ord + b).chr).to_a.join + 'a' * (a - b)\n\tputs (str * ((n + a - 1) / a))[0, n]\nend\n"}, {"source_code": "LETTERS = 'abcdefghijklmnopqrstuvwxyz'\n\ngets.to_i.times {\n n, a, b = gets.split.map { |v| v.to_i }\n s = LETTERS[0...b] * (a / b) + LETTERS[0...(a % b)]\n puts s * (n / a) + s[0...(n % a)]\n}\n\n"}, {"source_code": "t = gets.chomp.to_i\n\n(1..t).each{\n n, a, b = gets.chomp.split.map{|x| x.to_i}\n \n (0...n).each{|i|\n print (i % b + 97).chr\n }\n puts\n}"}, {"source_code": "# https://codeforces.com/problemset/problem/1335/B\nC = \"a\".ord\n\ndef solution(n, a, b)\n s = \"\"\n c = C\n i = 0\n j = 0\n n.times do\n s += c.chr\n\n c += 1\n i += 1\n j += 1\n\n if i == b\n c = C\n i = 0\n end\n if j == a\n c = C\n i = 0\n j = 0\n end\n end\n return s\nend\n\nt = gets.chomp.to_i\n\nwhile t > 0 do\n n, a, b = gets.chomp\n .split(\" \")\n .map(&:to_i)\n puts solution(n, a, b)\n t -= 1\nend\n"}, {"source_code": "t = gets.to_i\n\n1.upto(t) do\n n, a, b = gets.split(' ').map(&:to_i)\n\n puts (('a'..'z').to_a[0, b].join * (n.div(b) + 1))[0, n]\nend\n"}, {"source_code": "t = gets.to_i\n(1).upto(t) do\n a=gets.split(' ').map(&:to_i)\n str=[]\n (97).upto(97+a[2]-1) do |i|\n str.push(i.chr)\n end\n while(str.length < a[1]) do\n str.push((97+a[2]-1).chr)\n end\n (0..a[0]-1).each do |i|\n print(str[i.modulo(a[1])])\n end\n puts\nend\n"}], "negative_code": [], "src_uid": "a82f15c1b0ddcacc1de966be20cfc5a2"} {"nl": {"description": "Learn, learn and learn again — Valera has to do this every day. He is studying at mathematical school, where math is the main discipline. The mathematics teacher loves her discipline very much and tries to cultivate this love in children. That's why she always gives her students large and difficult homework. Despite that Valera is one of the best students, he failed to manage with the new homework. That's why he asks for your help. He has the following task. A sequence of n numbers is given. A prefix of a sequence is the part of the sequence (possibly empty), taken from the start of the sequence. A suffix of a sequence is the part of the sequence (possibly empty), taken from the end of the sequence. It is allowed to sequentially make two operations with the sequence. The first operation is to take some prefix of the sequence and multiply all numbers in this prefix by  - 1. The second operation is to take some suffix and multiply all numbers in it by  - 1. The chosen prefix and suffix may intersect. What is the maximum total sum of the sequence that can be obtained by applying the described operations?", "input_spec": "The first line contains integer n (1 ≤ n ≤ 105) — amount of elements in the sequence. The second line contains n integers ai ( - 104 ≤ ai ≤ 104) — the sequence itself.", "output_spec": "The first and the only line of the output should contain the answer to the problem.", "sample_inputs": ["3\n-1 -2 -3", "5\n-4 2 0 5 0", "5\n-1 10 -5 10 -2"], "sample_outputs": ["6", "11", "18"], "notes": null}, "positive_code": [{"source_code": "n = gets.chomp.to_i\ns = gets.chomp\na = s.split(/ /)\nfor i in 0..a.length-1\n\ta[i] = a[i].to_i\nend\n\nsum = 0\nsum1 = 0\nmaxs = 0\nfor i in 0..n-1\n\tsum += a[i]\n\tsum1 += a[i]\n\tsum1 = 0 if (sum1 < 0) \n\tmaxs = [maxs, sum1].max\nend\nputs sprintf(\"%d\\n\", 2*maxs - sum)\n"}], "negative_code": [{"source_code": "n = gets.chomp.to_i\ns = gets.chomp\nna = s.split(/ /)\nfor i in 0..na.length-1\n\tna[i] = na[i].to_i\nend\n\nlastidx = -1\nlastmin = 0\nwk = 0\nfor i in 0..na.length-1\n\twk += na[i]\n\tif wk < 0 && wk < lastmin\n\t\tlastidx = i\n\t\tlastmin = wk\n\tend\nend\n\nif lastidx >= 0\n\tfor i in 0..lastidx\n\t\tna[i] = -na[i] \n\tend\nend\n\nlastidx = -1\nlastmin = 0\nwk = 0\n(na.length-1).downto(0) { |i|\n\twk += na[i]\n\tif wk < 0 && wk < lastmin\n\t\tlastidx = i\n\t\tlastmin = wk\n\tend\n}\n\nif lastidx >= 0\n\t(na.length-1).downto(lastidx) { |i|\n\t\tna[i] = -na[i] \n\t}\nend\n\nret = 0\nna.each {|nn|\n\tret += nn\n}\nputs ret"}, {"source_code": "n = gets.chomp.to_i\ns = gets.chomp\nna = s.split(/ /)\nfor i in 0..na.length-1\n\tna[i] = na[i].to_i\nend\n\nlastidx = -1\nlastmin = 0\nwk = 0\nfor i in 0..na.length-1\n\twk += na[i]\n\tif wk < 0 && wk < lastmin\n\t\tlastidx = i\n\t\tlastmin = wk\n\tend\nend\n\nif lastidx >= 0\n\tfor i in 0..lastidx\n\t\tna[i] = -na[i] \n\tend\nend\n\nlastidx = -1\nlastmin = 0\nwk = 0\n(na.length-1).downto(0) { |i|\n\twk += na[i]\n\tif wk < 0 && wk < lastmin\n\t\tlastidx = i\n\t\tlastmin = wk\n\tend\n}\n\nif lastidx >= 0\n\t(na.length-1).downto(lastidx) { |i|\n\t\tna[i] = -na[i] \n\t}\nend\n\nret = 0\nna.each {|nn|\n\tret += nn\n}\nputs ret"}, {"source_code": "n = gets.chomp.to_i\ns = gets.chomp\nna = s.split(/ /)\nfor i in 0..na.length-1\n\tna[i] = na[i].to_i\nend\n\nlastidx = -1\nwk = 0\nfor i in 0..na.length-1\n\twk += na[i]\n\tlastidx = i if wk < 0\nend\n\nif lastidx >= 0\n\tfor i in 0..lastidx\n\t\tna[i] = -na[i] \n\tend\nend\n\nlastidx = -1\nwk = 0\n(na.length-1).downto(0) { |i|\n\twk += na[i]\n\tlastidx = i if wk < 0\n}\n\nif lastidx >= 0\n\t(na.length-1).downto(lastidx) { |i|\n\t\tna[i] = -na[i] \n\t}\nend\n\nret = 0\nna.each {|nn|\n\tret += nn\n}\nputs ret"}], "src_uid": "89237865c97d64406050fd140d4166f9"} {"nl": {"description": "Colonel has n badges. He wants to give one badge to every of his n soldiers. Each badge has a coolness factor, which shows how much it's owner reached. Coolness factor can be increased by one for the cost of one coin. For every pair of soldiers one of them should get a badge with strictly higher factor than the second one. Exact values of their factors aren't important, they just need to have distinct factors. Colonel knows, which soldier is supposed to get which badge initially, but there is a problem. Some of badges may have the same factor of coolness. Help him and calculate how much money has to be paid for making all badges have different factors of coolness.", "input_spec": "First line of input consists of one integer n (1 ≤ n ≤ 3000). Next line consists of n integers ai (1 ≤ ai ≤ n), which stand for coolness factor of each badge.", "output_spec": "Output single integer — minimum amount of coins the colonel has to pay.", "sample_inputs": ["4\n1 3 1 4", "5\n1 2 3 2 5"], "sample_outputs": ["1", "2"], "notes": "NoteIn first sample test we can increase factor of first badge by 1.In second sample test we can increase factors of the second and the third badge by 1."}, "positive_code": [{"source_code": "n = ARGF.readline.to_i\nline = ARGF.readline.split(\" \")\nnumbers = []\nline.each do |n|\nnumbers.push(n.to_i)\nend\n\nnumbers = numbers.sort\ncounter = 0\n\nfor i in 1..(numbers.length - 1)\n if numbers[i] <= numbers[i - 1]\n \ttemp = (numbers[i] - numbers[i - 1]).abs + 1\n \tnumbers[i] = numbers[i] + temp\n \tcounter = counter + temp\n end\nend\n\nputs counter"}, {"source_code": "total_badges = gets.to_i\nbadges = gets.split(' ')\ncoolness = []\n(total_badges*2+1).times do |x|\n coolness[x] = 0\nend\nbadges.inject(0) do |sum, badge|\n coolness[badge.to_i]+=1\nend\ntotal = 0\ncoolness.each_with_index do |cool, index|\n badge_count_increased = cool-1\n if badge_count_increased > 0\n coolness[index+1]+=badge_count_increased \n total+=badge_count_increased\n end\nend\nputs total"}, {"source_code": "n = gets.to_i\na = gets.split.map &:to_i\na.sort!\nans = 0\nlast = 0\na.each do |x|\n while x <= last\n ans += 1\n x += 1\n end\n last = x\nend\nputs ans"}, {"source_code": "n = gets.chomp.to_i\nfactors = gets.chomp.split.map(&:to_i).sort\nsum = factors.inject(0, :+)\n\n(1...n).each do |idx|\n factors[idx] = factors[idx - 1] + 1 if factors[idx] <= factors[idx - 1]\nend\n\nputs factors.inject(0, :+) - sum\n"}, {"source_code": "badgeNum = gets.chomp.to_i\nbadges = gets.chomp.split(' ').map {|item| item.to_i }\n\nbadges.sort!\n\nunique = badges.select { |e| badges.count(e) == 1 }\nduplicate = badges.select { |e| badges.count(e) > 1}\n\npayNum = 0\nsumNumber = 0\n\n\nfor i in 0...badges.length - 1\n\t#puts \"Debug : #{badges[i]}\"\n\t#puts \"i : #{i}\"\n\tpayNum += (badges[i + 1] - badges[i]).abs + 1 if badges[i] >= badges[i + 1]\n\tbadges[i + 1] += (badges[i + 1] - badges[i]).abs + 1 if badges[i] >= badges[i + 1]\nend\n\n=begin\nuntil duplicate.length == 0\n\n\n\tprint \"Debug : \"\n\tduplicate.each do |item|\n\t\tprint \"#{item} \"\n\tend\n\tputs ''\n\n\tprint \"Debug_2 : \"\n\tunique.each do |item|\n\t\tprint \"#{item} \"\n\tend\n\tputs ''\n\n\n\tif duplicate.count(duplicate[0]) % 2 == 1 && !unique.include?(duplicate[0])\n\t\tunique << duplicate[0]\n\t\tduplicate.delete_at(0)\n\t\tnext\n\tend\n\n\tsumNumber = duplicate\n\n\tloop do\n\t#\tputs \"sumNumber : #{sumNumber}\"\n\t\tbreak if !unique.include?(duplicate[0] + sumNumber)\n\t\tsumNumber += 1\n\tend\n\n\tunique << duplicate[0] + sumNumber\n\tduplicate.delete_at(0)\n\tpayNum += sumNumber\n\tsumNumber = 0\nend\n=end\n\nputs payNum"}, {"source_code": "a=gets.chomp.to_i\nb=gets.chomp.split(\" \").map{|i| i.to_i}\nb.sort!\nr=b[0]\nans=0\n1.upto(a-1) do |i|\nif b[i]<=r+1\nans+=r+1-b[i]\nr+=1\nelse\nr=b[i]\nend\nend\nputs \"#{ans}\""}, {"source_code": "a=gets.chomp.to_i\nb=gets.chomp.split(\" \").map{|i| i.to_i}\nb.sort!\nr=b[0]\nans=0\n1.upto(a-1) do |i|\nif b[i]<=r+1\nans+=r+1-b[i]\nr+=1\nelse\nr=b[i]\nend\nend\nputs \"#{ans}\""}, {"source_code": "n=gets.to_i\na=gets.split.map{|e| e.to_i}.sort\nans=0\n1.upto(n-1){|i|\n\tif a[i-1]>=a[i] then\n\t\tans+=a[i-1]-a[i]+1\n\t\ta[i]=a[i-1]+1\n\tend\n}\nputs ans\n\n"}, {"source_code": "n=gets.to_i\nx=gets.split.map{|e| e.to_i}.sort\n\nans=0\na1=x[0]\nx.shift\nx.each{|e|\n\tif a1>=e then\n\t\tans+=(a1-e+1)\n\t\ta1+=1\n\telse\n\t\ta1=e\n\tend\n}\nputs ans\n\n"}, {"source_code": "# coding: utf-8\n\nn = gets.to_i\na = gets.split.map(&:to_i).sort\ncoins = 0\nfor i in 1...n\n if a[i-1] >= a[i]\n addition = a[i-1] - a[i] + 1\n a[i] += addition\n coins += addition\n end\nend\n\nputs coins"}, {"source_code": "n = gets.split.map(&:to_i)[0]\nc = gets.split.map(&:to_i)\ngao = Hash.new(false)\nans = 0\nfor i in 0..n-1 do\n c[i] += 1 and ans += 1 until !gao[c[i]]\n gao[c[i]] = true\nend\nprint ans"}, {"source_code": "n=gets.to_i\nv=gets.split.map(&:to_i)\nans=0\nv=v.sort\nfor i in 0...v.length do\n for j in 0...v.length do\n if i!=j\n if v[i]==v[j]\n ans+=1\n v[j]+=1\n end\n end\n\n end\nend\nputs ans\n"}, {"source_code": "n=gets.to_i\nv=gets.split.map(&:to_i)\nans=0\nv=v.sort\nfor i in 1...v.length do\n if v[i]<=v[i-1]\n ans+=v[i-1]-v[i]+1\n v[i]=v[i-1]+1\n end\nend\nputs ans\n"}, {"source_code": "n = gets.to_i\na = gets.split.collect{|i| i.to_i}.sort\nans = 0\ncur = a[0]\n(1...n).each do |i|\n if a[i] <= cur\n ans += cur-a[i]+1\n cur += 1\n else\n cur = a[i]\n end\nend\nputs ans\n"}, {"source_code": "n = gets.to_i\ns = gets.split.map(&:to_i).sort\nans = 0\n1.upto(n - 1) do |i|\n if s[i] <= s[i - 1]\n ans += s[i - 1] - s[i] + 1\n s[i] = s[i - 1] + 1\n end\nend\nputs ans\n"}, {"source_code": "#!/usr/bin/env ruby\n\nn = ARGF.gets.to_i # number of badges\ncoolness = ARGF.gets.split(' ').map { |s| s.to_i}\ncoolness.sort!\n\ncoins = 0\n\nfor i in 0...n-1 do\n if coolness[i] >= coolness[i+1]\n cost = 1 + (coolness[i] - coolness[i+1])\n coolness[i+1] += cost \n coins += cost\n end\nend\n\np coins\n\n\n\n"}], "negative_code": [{"source_code": "n = gets.chomp.to_i\nfactors = gets.chomp.split.map(&:to_i).sort\nmin = factors.first\n\nmin_cost = factors.map.with_index do |v, i|\n (v - (min + i)).abs\nend.inject(0, :+)\n\nputs min_cost\n"}, {"source_code": "n = gets.chomp.to_i\nfactors = gets.chomp.split.map(&:to_i).sort\n\nmin_cost = factors.map.with_index { |v, i| (v - (i + 1)).abs }.inject(0, :+)\nputs min_cost\n"}, {"source_code": "badgeNum = gets.chomp.to_i\nbadges = gets.chomp.split(' ').map {|item| item.to_i }\n\nbadges.sort!\n\nunique = badges.select { |e| badges.count(e) == 1 }\nduplicate = badges.select { |e| badges.count(e) > 1}\n\npayNum = 0\nsumNumber = 0\n\n\nfor i in 0...badges.length - 1\n\tputs \"Debug : #{badges[i]}\"\n\tputs \"i : #{i}\"\n\tpayNum += (badges[i + 1] - badges[i]).abs + 1 if badges[i] >= badges[i + 1]\n\tbadges[i + 1] += (badges[i + 1] - badges[i]).abs + 1 if badges[i] >= badges[i + 1]\nend\n\n=begin\nuntil duplicate.length == 0\n\n\n\tprint \"Debug : \"\n\tduplicate.each do |item|\n\t\tprint \"#{item} \"\n\tend\n\tputs ''\n\n\tprint \"Debug_2 : \"\n\tunique.each do |item|\n\t\tprint \"#{item} \"\n\tend\n\tputs ''\n\n\n\tif duplicate.count(duplicate[0]) % 2 == 1 && !unique.include?(duplicate[0])\n\t\tunique << duplicate[0]\n\t\tduplicate.delete_at(0)\n\t\tnext\n\tend\n\n\tsumNumber = duplicate\n\n\tloop do\n\t#\tputs \"sumNumber : #{sumNumber}\"\n\t\tbreak if !unique.include?(duplicate[0] + sumNumber)\n\t\tsumNumber += 1\n\tend\n\n\tunique << duplicate[0] + sumNumber\n\tduplicate.delete_at(0)\n\tpayNum += sumNumber\n\tsumNumber = 0\nend\n=end\n\nputs payNum"}, {"source_code": "badgeNum = gets.chomp.to_i\nbadges = gets.chomp.split(' ').map {|item| item.to_i }\n\nunique = badges.select { |e| badges.count(e) == 1 }\nduplicate = badges.select { |e| badges.count(e) > 1}\ntemp = badges.select { |e| badges.count(e) > 1}\n\nindex = 0\npayNum = 0\n\nuntil duplicate.length == 0\n\n=begin\n\tprint \"Debug : \"\n\tduplicate.each do |item|\n\t\tprint \"#{item} \"\n\tend\n\tputs ''\n\n\tprint \"Debug_2 : \"\n\ttemp.each do |item|\n\t\tprint \"#{item} \"\n\tend\n\tputs ''\n=end\n\n\tsumNumber = 0\n\n\tloop do\n\t#\tputs \"sumNumber : #{sumNumber}\"\n\t\tbreak if !temp.include?(duplicate[0] + sumNumber) && !unique.include?(duplicate[0] + sumNumber)\n\t\tsumNumber += 1\n\tend\n\n\ttemp.delete(duplicate[0])\n\ttemp.delete(duplicate[0])\n\n\ttemp << duplicate[0] + sumNumber\n\ttemp << duplicate[0] + sumNumber\n\n\tduplicate.delete_at(0)\n\tpayNum += sumNumber\n\tsumNumber = 0\n\n\tindex += 1\nend\n\nputs payNum"}, {"source_code": "a=gets.chomp.to_i\nb=gets.chomp.split(\" \").map{|i| i.to_i}\nb.sort!\ntr=0\nans=0\n0.upto(a-1) do |i|\nif b[i]+tr<=i+1\nans+=i+1-b[i]-tr\nelse\nans+=(a-i-1)*(b[i]+tr-i-1)\ntr+=1**(b[i]+tr-i-1)\nend\nend\nputs \"#{ans}\""}, {"source_code": "a=gets.chomp.to_i\nb=gets.chomp.split(\" \").map{|i| i.to_i}\nk=(a*(a+1))/2\nsum=0\nb.each {|i| sum+=i }\nputs \"#{k-sum}\"\n"}, {"source_code": "# coding: utf-8\n\nn = gets.to_i\na = gets.split.map(&:to_i).sort\ncoins = 0\nfor i in 1...n\n if a[i-1] == a[i]\n a[i] += 1\n coins += 1\n end\nend\n\nputs coins"}, {"source_code": "# coding: utf-8\n\nn = gets.to_i\na = gets.split.map(&:to_i).sort\ncoins = 0\nfor i in 1...(n-1)\n if a[i-1] == a[i]\n a[i] += 1\n coins += 1\n end\nend\n\nputs coins"}, {"source_code": "n = gets.split.map(&:to_i)[0]\nc = gets.split.map(&:to_i)\ngao = Hash.new(false)\nans = 0\nfor i in 0..n-1 do\n c[i] += 1 and ans += 1 until !gao[c[i]]\n gao[c[i]] = true\nend"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i).sort\nans = 0\nn.times do |i|\n if a[i] == a[i + 1]\n a[i] += 1\n ans += 1\n a.sort!\n end\nend\nputs ans\n"}, {"source_code": "n = gets.to_i\ns = [nil] + gets.split.map(&:to_i).sort\nans = 0\nn.downto(1) do |i|\n if s[i] != i\n ans += i - s[i]\n s[i] = i\n end\nend\nputs ans\n"}, {"source_code": "n = gets.to_i\ns = [nil] + gets.split.map(&:to_i).sort\nans = 0\nm = [n + s[1..-1].min - 1, s[1..-1].max].max\nmi = m - n + 1\nm.downto(mi) do |i|\n j = i - mi + 1\n if s[j] != i\n ans += i - s[j]\n s[j] = i\n end\nend\nputs ans\n"}, {"source_code": "n = gets.to_i\ns = [nil] + gets.split.map(&:to_i).sort\nans = 0\nmi = s[1..-1].min\nm = n + mi - 1\nm.downto(mi) do |i|\n j = i - mi + 1\n if s[j] != i\n ans += i - s[j]\n s[j] = i\n end\nend\nputs ans\n"}], "src_uid": "53ae714f04fd29721b8bbf77576b7ccf"} {"nl": {"description": "SmallR is an archer. SmallR is taking a match of archer with Zanoes. They try to shoot in the target in turns, and SmallR shoots first. The probability of shooting the target each time is for SmallR while for Zanoes. The one who shoots in the target first should be the winner.Output the probability that SmallR will win the match.", "input_spec": "A single line contains four integers .", "output_spec": "Print a single real number, the probability that SmallR will win the match. The answer will be considered correct if the absolute or relative error doesn't exceed 10 - 6.", "sample_inputs": ["1 2 1 2"], "sample_outputs": ["0.666666666667"], "notes": null}, "positive_code": [{"source_code": "a, b, c, d = gets.split.map(&:to_f)\na /= b\nc /= d\nx = (1 - a) * (1 - c)\ny = (1 - a) * c\np 1 - y / (1 - x)\n"}, {"source_code": "a, b, c, d = gets.chomp.split\na = a.to_f\nb = b.to_f\nc = c.to_f\nd = d.to_f\nprobe1 = 0\nprobe2 = a/b\nn = 2\nwhile (probe2 - probe1 > 0.000000001)\n probe1 = probe2\n probe2 += ((1-a/b)**(n - 1))*((1 - c/d)**(n-1))*(a/b)\n n += 1\nend\nputs probe2"}, {"source_code": "def myfun\nn = gets.chomp\nar = n.split(' ')\na = ar[0].to_i\nb = ar[1].to_i\nc = ar[2].to_i\nd = ar[3].to_i\n\nr = a*d*1.0000000\nr = r/((b*c)+(a*d)-(a*c))\nputs r\nreturn nil\nend\n\nputs myfun\n"}, {"source_code": "#!/usr/bin/ruby1.9\n\na, b, c, d = STDIN.readline.split.collect {|s| s.to_i }\n\nr = 1.0*a/b\n\nk = 1.0*(b-a)*(d-c)/(b*d)\n\nx = r/(1-k)\n\nputs x\n\n"}, {"source_code": "a, b, c, d = gets.split.map(&:to_i)\npa = 1.0 * a / b\npb = 1.0 * c / d\n\nputs(1.0 / (1.0 - (1.0 - pa) * (1.0 - pb)) * pa)\n"}, {"source_code": "a,b,c,d=gets.chomp.split.map(&:to_f)\nputs (a/b)/(1-((1-c/d)*(1-a/b)))"}, {"source_code": "input = STDIN.read\n\ninput =~ /^(\\d+) (\\d+) (\\d+) (\\d+)/\na, b, c, d = $1.to_f, $2.to_f, $3.to_f, $4.to_f\n \np1 = a / b\n\ns = p1 / (1 - (1 - p1) * (1 - (c / d)))\n\nputs s\n\n"}, {"source_code": "a,b,c,d=readline.split(' ').map(&:to_i)\nr=1.0*a*d/(a*d+b*c-a*c)\nputs r\n"}, {"source_code": "str = gets.chomp\na, b, c, d = str.split.map { |i| i.to_f }\nputs (a * d) / (b * c + a * d - a * c)\n"}, {"source_code": "a,b,c,d = gets.split(\" \").map(&:to_i)\np = a/b.to_f\nz = c/d.to_f\nq = (1-p)*(1-z)\n\nputs (p/(1-q))"}, {"source_code": "a,b,c,d = gets.chomp.split(\" \").collect {|x| x.to_i}\n\np1 = a*1.0/b\np2 = c*1.0/d\n\np = (1-p1)*(1-p2)\n\nres = p1 * 1.0/(1-p)\n\nputs res"}, {"source_code": "a, b, c, d = gets.split().map(&:to_i)\ns = a*1.0/b\nz = c*1.0/d\nputs s/(1-(1-s)*(1-z))\n"}, {"source_code": "require 'bigdecimal'\n\na,b,c,d = gets.strip.split(' ').map {|x| x.to_i}\n\np1 = 1 - BigDecimal.new(a)/BigDecimal.new(b)\np2 = 1 - BigDecimal.new(c)/BigDecimal.new(d)\n\nr = (1 - p1)/(1 - p1*p2)\nputs r.to_s(\"F\")"}, {"source_code": "a, b, c, d = gets.split.map(&:to_i)\nputs 1.0 * a * d / (b * d - (b - a) * (d - c))\n"}, {"source_code": "#!/usr/bin/ruby\n\na, b, c, d = gets.chomp.split(\" \").map(&:to_f)\n\np = a / b\nq = c / d\n\nputs p / (1 - (1 - p) * (1 - q))\n"}], "negative_code": [{"source_code": "a, b, c, d = gets.chomp.split\na = a.to_f\nb = b.to_f\nc = c.to_f\nd = d.to_f\nprobe1 = 0\nprobe2 = a/b\nn = 2\nwhile (probe2 - probe1 > 0.0000001)\n probe1 = probe2\n probe2 += ((1-a/b)**(n - 1))*((1 - c/d)**(n-1))*(a/b)\n n += 1\nend\nputs probe2"}, {"source_code": "a, b, c, d = ARGV[0].to_f, ARGV[1].to_f, ARGV[2].to_f, ARGV[3].to_f\nprobe1 = 0\nprobe2 = a/b\nn = 2\nwhile (probe2 - probe1 > 0.0000001)\n probe1 = probe2\n probe2 += ((1-a/b)**(n - 1))*((1 - c/d)**(n-1))*(a/b)\n n += 1\nend\nputs probe2"}, {"source_code": "#!/usr/bin/ruby\n\na, b, c, d = gets.chomp.split(\" \").map(&:to_f)\n#puts a, b, c, d\nputs (2.0 * (a / b)) / (3.0 * (c / d))\n"}], "src_uid": "7b932b2d3ab65a353b18d81cf533a54e"} {"nl": {"description": "This is an interactive problem.We hid from you a permutation $$$p$$$ of length $$$n$$$, consisting of the elements from $$$1$$$ to $$$n$$$. You want to guess it. To do that, you can give us 2 different indices $$$i$$$ and $$$j$$$, and we will reply with $$$p_{i} \\bmod p_{j}$$$ (remainder of division $$$p_{i}$$$ by $$$p_{j}$$$).We have enough patience to answer at most $$$2 \\cdot n$$$ queries, so you should fit in this constraint. Can you do it?As a reminder, a permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).", "input_spec": "The only line of the input contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^4$$$) — length of the permutation.", "output_spec": null, "sample_inputs": ["3\n\n1\n\n2\n\n1\n\n0"], "sample_outputs": ["? 1 2\n\n? 3 2\n\n? 1 3\n\n? 2 1\n\n! 1 3 2"], "notes": null}, "positive_code": [{"source_code": "N = gets.to_i\none_pos = -1\ncur = 1\nans = Array.new(N)\nremain = [*1..N]\nchecked = Hash.new(false)\n\nwhile remain.size > 1\n a, b = remain.pop(2)\n\n STDOUT.puts(\"? #{a} #{b}\")\n STDOUT.flush\n res1 = STDIN.gets.to_i\n\n STDOUT.puts(\"? #{b} #{a}\")\n STDOUT.flush\n res2 = STDIN.gets.to_i\n\n if res1 < res2\n remain << a\n ans[b - 1] = res2\n checked[res2] = true\n else\n remain << b\n ans[a - 1] = res1\n checked[res1] = true\n end\nend\n\n1.upto(N) do |num|\n next if checked[num]\n\n ans[remain.first - 1] = num\n break\nend\n\nputs \"! #{ans.join(' ')}\"\n"}], "negative_code": [], "src_uid": "9bfbd68e61f4d2f3d404fd0413aded35"} {"nl": {"description": "Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again.The device is powered by two wires \"plus\" and \"minus\". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view): Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the \"plus\" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut.To understand the problem better please read the notes to the test samples.", "input_spec": "The single line of the input contains a sequence of characters \"+\" and \"-\" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character \"+\", if on the i-th step from the wall the \"plus\" wire runs above the \"minus\" wire, and the character \"-\" otherwise.", "output_spec": "Print either \"Yes\" (without the quotes) if the wires can be untangled or \"No\" (without the quotes) if the wires cannot be untangled.", "sample_inputs": ["-++-", "+-", "++", "-"], "sample_outputs": ["Yes", "No", "Yes", "No"], "notes": "NoteThe first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the \"plus\" wire lower, thus eliminating the two crosses in the middle, and then draw it under the \"minus\" wire, eliminating also the remaining two crosses.In the second testcase the \"plus\" wire makes one full revolution around the \"minus\" wire. Thus the wires cannot be untangled: In the third testcase the \"plus\" wire simply runs above the \"minus\" wire twice in sequence. The wires can be untangled by lifting \"plus\" and moving it higher: In the fourth testcase the \"minus\" wire runs above the \"plus\" wire once. The wires cannot be untangled without moving the device itself: "}, "positive_code": [{"source_code": "s = gets.chomp.split(//)\nstk = []\nfor i in s\n if stk.last == i\n stk.pop\n else\n stk.push(i)\n end\nend\nputs stk.length > 0 ? :No : :Yes\n"}, {"source_code": "s=gets.chomp.strip.split('').map(&:to_s)\na=Array.new(100005,0)\np=0\nfor i in s\n\tif i!=a[p]\n\t\tp+=1\n\t\ta[p]=i\n\telse\n\t\tp-=1\n\tend\nend\nif p==0\n\tputs \"YES\"\nelse\n\tputs \"NO\"\nend"}, {"source_code": "class D\n def initialize\n sequence = gets.chomp\n\n right = 0\n left = 0\n last = ''\n\n if sequence.size.odd? \n puts \"No\"\n exit\n end\n\n sequence.split('').each_slice(2).map(&:join).each do |elem|\n case elem\n when '++'\n when '--'\n when '+-'\n right += 1\n when '-+'\n left += 1\n end\n end\n\n if right == left\n puts \"Yes\"\n else\n puts \"No\"\n end\n end\nend\n\nd = D.new"}, {"source_code": "class D\n def initialize\n sequence = gets.chomp\n\n right = 0\n left = 0\n last = ''\n\n if sequence.size.odd? \n puts \"No\"\n exit\n end\n\n sequence.split('').each_slice(2).map(&:join).each do |elem|\n case elem\n when '++'\n last = '+'\n when '--'\n last = '-'\n when '+-'\n right += 1\n last = '-'\n when '-+'\n left += 1\n last = '+'\n when '-'\n if last == '+'\n left += 1\n end\n when '+'\n if last == '-'\n right += 1\n end\n end\n end\n\n if right == left\n puts \"Yes\"\n else\n puts \"No\"\n end\n end\nend\n\nd = D.new"}], "negative_code": [{"source_code": "class D\n def initialize\n sequence = gets.chomp\n\n right = 0\n left = 0\n last = ''\n\n if sequence.size == 1\n puts \"No\"\n exit\n end\n\n sequence.split('').each_slice(2).map(&:join).each do |elem|\n case elem\n when '++'\n last = '+'\n when '--'\n last = '-'\n when '+-'\n right += 1\n last = '-'\n when '-+'\n left += 1\n last = '+'\n when '-'\n if last == '+'\n left += 1\n end\n when '+'\n if last == '-'\n right += 1\n end\n end\n end\n\n if right == left\n puts \"Yes\"\n else\n puts \"No\"\n end\n end\nend\n\nd = D.new"}, {"source_code": "class D\n def initialize\n sequence = gets.chomp\n\n right = 0\n left = 0\n last = ''\n\n if sequence.size == 1\n puts \"No\"\n exit\n end\n\n sequence.split('').each_cons(2).map(&:join).each do |elem|\n case elem\n when '++'\n when '--'\n when '+-'\n right += 1\n when '-+'\n left += 1\n end\n end\n\n if right == left\n puts \"Yes\"\n else\n puts \"No\"\n end\n end\nend\n\nd = D.new"}], "src_uid": "89b4a7b4a6160ce784c588409b6ce935"} {"nl": {"description": "You are given an array $$$a$$$ consisting of $$$n$$$ ($$$n \\ge 3$$$) positive integers. It is known that in this array, all the numbers except one are the same (for example, in the array $$$[4, 11, 4, 4]$$$ all numbers except one are equal to $$$4$$$).Print the index of the element that does not equal others. The numbers in the array are numbered from one.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 100$$$). Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$3 \\le n \\le 100$$$) — the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le 100$$$). It is guaranteed that all the numbers except one in the $$$a$$$ array are the same.", "output_spec": "For each test case, output a single integer — the index of the element that is not equal to others.", "sample_inputs": ["4\n4\n11 13 11 11\n5\n1 4 4 4 4\n10\n3 3 3 3 10 3 3 3 3 3\n3\n20 20 10"], "sample_outputs": ["2\n1\n5\n3"], "notes": null}, "positive_code": [{"source_code": "gets;$<.map{a=gets.split;puts a.index(a.min_by{|it|a.count it})+1}"}, {"source_code": "t = gets.to_i\ninputs = []\n\n(0..t-1).each do |i|\n len = gets.to_i\n arr = gets.chomp\n arr = arr.split(\" \").map(&:to_i)\n inputs.push([arr, len])\nend\n\ninputs.each do |input|\n arr = input[0]\n n = input[1]\n map = {}\n index = -1\n (0..n-1).each do |i|\n if map.key?(arr[i])\n map[arr[i]] = [true, i]\n else\n map[arr[i]] = [false, i]\n end\n end\n map.each do |k, v|\n if v[0] == false\n puts v[1]+1\n end\n end\nend\n\n"}, {"source_code": "gets;$<.map{a=gets.split;puts a.index(a.min_by{|it|a.count it})+1}"}, {"source_code": "gets;$<.map{a=gets.split;puts a.index(a.min_by{|it|a.count it})+1}"}, {"source_code": "inputs = STDIN.read.strip.split(\"\\n\")\r\ninputs.shift\r\n\r\ninputs.each_slice(2) do |inp|\r\n inp.shift\r\n nums = inp[0].split(' ').map(&:to_i)\r\n\r\n nums.uniq.each do |n|\r\n next if nums.count{|i| i == n} != 1\r\n\r\n puts nums.index(n) + 1\r\n\r\n break\r\n end\r\nend"}, {"source_code": "for i in 0...gets.chomp.to_i\r\n n = gets.chomp.to_i\r\n arr = gets.chomp.split(\" \").map{|item| item.to_i}\r\n temp = arr.uniq\r\n for i in temp\r\n if arr.count(i) == 1\r\n puts arr.index(i) + 1\r\n end\r\n end\r\nend"}, {"source_code": "t = gets.chomp\r\nfor i in 0...t.to_i\r\n n = gets.chomp\r\n a = gets.chomp.split(\" \")\r\n if a[0] == a[1]\r\n for j in 2...n.to_i\r\n if a[j] != a[0]\r\n puts j + 1\r\n break\r\n end\r\n end\r\n else\r\n if a[0] == a[2] then puts 2\r\n else puts 1\r\n end\r\n end\r\nend"}, {"source_code": "t = gets.to_s.to_i\n\n1.upto(t) do |casenum|\n n = gets.to_s.to_i\n a = gets.to_s.split.map {|x| x.to_i }\n b = a.sort\n v1 = b[0]\n v2 = b[-1]\n if b[1] != v1\n printf(\"%d\\n\", a.index(v1) + 1)\n else\n printf(\"%d\\n\", a.index(v2) + 1)\n end\nend\n\n"}, {"source_code": "gets;$<.map{a=gets.split;puts a.index(a.min_by{|it|a.count it})+1}"}, {"source_code": "gets;$<.map{a=gets.split;puts a.index(a.min_by{|it|a.count it})+1}"}, {"source_code": "t = gets.strip.to_i\r\nt.times do\r\n\tn = gets.strip.to_i\r\n\ta = gets.strip.split.map(&:to_i)\r\n\tcount = {}\r\n\tn.times do |i|\r\n\t\tcurrent = a[i]\r\n\t\tif !count.has_key? current\r\n\t\t\tcount[current] = [1, i]\r\n\t\telse\r\n\t\t\tcount[current][0] += 1\r\n\t\tend\r\n\t\tcount[current][1] = i\r\n\tend\r\n\tans = 0\r\n\tcount.each do |k, v|\r\n\t\tif v[0] == 1\r\n\t\t\tans = v[1] + 1\r\n\t\t\tbreak\r\n\t\tend\r\n\tend\r\n\tputs ans\r\nend"}, {"source_code": "t = gets.to_i\r\nt.times do\r\n\tn = gets.to_i\r\n\ta = gets.split\r\n\r\n\tidx = 0\r\n\r\n\tif a[0] != a[1]\r\n\t\tif a[0] == a[2]\r\n\t\t\tidx = 2\r\n\t\telse\r\n\t\t\tidx = 1\r\n\t\tend\r\n\telse\r\n\t\tfor i in 1...n\r\n\t\t\tif a[i] != a[0]\r\n\t\t\t\tidx = i + 1\r\n\t\t\t\tbreak\r\n\t\t\tend\r\n\t\tend\r\n\tend\r\n\r\n\tputs idx\r\nend\r\n"}], "negative_code": [{"source_code": "gets;$<.map{|s|gets;a=s.split;puts a.index(a.min_by{|it|a.count it})+1}"}, {"source_code": "gets;$<.map{|s|gets;a=s.split;puts a.index(a.min_by {a.count})+1}"}], "src_uid": "224a0b09547ec1441474efbd8e06353b"} {"nl": {"description": "We saw the little game Marmot made for Mole's lunch. Now it's Marmot's dinner time and, as we all know, Marmot eats flowers. At every dinner he eats some red and white flowers. Therefore a dinner can be represented as a sequence of several flowers, some of them white and some of them red.But, for a dinner to be tasty, there is a rule: Marmot wants to eat white flowers only in groups of size k.Now Marmot wonders in how many ways he can eat between a and b flowers. As the number of ways could be very large, print it modulo 1000000007 (109 + 7).", "input_spec": "Input contains several test cases. The first line contains two integers t and k (1 ≤ t, k ≤ 105), where t represents the number of test cases. The next t lines contain two integers ai and bi (1 ≤ ai ≤ bi ≤ 105), describing the i-th test.", "output_spec": "Print t lines to the standard output. The i-th line should contain the number of ways in which Marmot can eat between ai and bi flowers at dinner modulo 1000000007 (109 + 7).", "sample_inputs": ["3 2\n1 3\n2 3\n4 4"], "sample_outputs": ["6\n5\n5"], "notes": "Note For K = 2 and length 1 Marmot can eat (R). For K = 2 and length 2 Marmot can eat (RR) and (WW). For K = 2 and length 3 Marmot can eat (RRR), (RWW) and (WWR). For K = 2 and length 4 Marmot can eat, for example, (WWWW) or (RWWR), but for example he can't eat (WWWR). "}, "positive_code": [{"source_code": "dp=[]\n mod=1000000007\n na=gets.chomp\n \n t=na[0..na.index(\" \")-1].to_i\n i=na.index(\" \")\n k=na[i+1..na.length-1].to_i\n i=1\n dp[0]=1\n while i<100001\n dp[i]=0 \n if(i-k>=0)\n dp[i]=dp[i-k] \n end \n dp[i]+=dp[i-1] \n dp[i]=(dp[i])% mod\n i+=1\n end\n i=1\n while i<100001\n dp[i]+=dp[i-1]\n dp[i]=(dp[i])% mod\n i+=1\n end\n \n while t>0\n na=gets.chomp\n \n a=na[0..na.index(\" \")-1].to_i\n i=na.index(\" \")\n b=na[i+1..na.length-1].to_i\n temp=(dp[b]-dp[a-1])% mod\n puts temp\n \n t-=1\n end"}, {"source_code": "# coding: utf-8\n\n# 총 테스트 케이스 t 에 대해, 꽃을 a개부터 b개 사이로 먹는 경우의 수를 계산한다\n# 단 흰 꽃은 k개 단위로만 먹을 수 있다.\n# 예) k = 2일때, 꽃 4개를 먹을 때는 흰색만 4개를 먹을 수 있지만,\n# k = 3일때는 3개를 먹을 수 없다.\n# 이때 빨간 꽃 R과 흰꽃 W를 섞어서 먹을 수 있는 경우의 수를 modulo 10**9 + 7 로 계산\nMODULO_BASE = 10**9 + 7\nMAX_HEIGHT = 10**5\n# 혹시 k-Tree 문제 생각나십니까? 음 도움이 될지 모르겠는데\n# 각 자리에 올 수 있는건 R 또는 W입니다. 이것은 이진트리입니다.\n# 이진트리의 순회는 DFS와 BFS중 하나로 풀 수 있겠죠.\n# growing을 어떻게 할까요, 되는 노드만 계산합시다. 언제 망한 노드도 계산해요\n# D0: (root) (K = 3) Z = WWW (unbalanced)\n# D1: R1 Z3\n# D2: RR2 RZ4 ZR4 ZZ6\n# D3: RRR3 RRZ5 RZR5 RZZ7 ZRR5 ZRZ7 ZZR7 ZZZ9 \n# D4: RRRR4 RRRZ6 RRZR6 RRZZ8 RZRR6 RZRZ8 RZZR8 RZZZ10 ZRRR6 ZRRZ8 ZRZR8 ZRZZ10 ZZRR8 ZZRZ10 ZZZR10 ZZZZ12 \nt, k = gets.split.map(&:to_i)\n# 아 시발 이런 뜻이라니\n# 각 꽃의 개수를 목적치로 설정합니다.\n# 모든 경우에 대해 R로 채우는 1가지가 있으므로 dp[x] += 1을 적용합니다.\n# dp[x]로부터, k씩 더해 방문 가능한 개수를 1씩 증가시킵니다\n# dp[x+1]로 넘어가서 dp[x+1]에 대해 방문하였으므로 += 1을 수행하고, 여기로부터 방문 가능한 x+k배수를 모두 += 1합니다.\n# 자연스럽게 x' 가 x + k 와 같아지는 순간이 있습니다. 이때도 += 1을 적용합니다.\n# \ndp = Array.new(MAX_HEIGHT + k) { 0 } # 0번 인덱스 안써요\n# 이렇게 초기화해야하는 이유가 있음\n# K = 1일때는 한 자리에 두가지 경우가 바로 발생됨. 그걸 표현하려면 이 방법이 좋다.\ndp[1] += 1\ndp[k] += 1\nacc_dp = Array.new(MAX_HEIGHT + k)\nacc_dp[0] = 0\nacc_dp[1] = dp[1] # 여기도 초기화를 이렇게 해야함, 왜 항상 처음이 1일거라 생각하는거지? K=1이면 이 자리는 2이다.\nacc = dp[1]\nfor i in 2..MAX_HEIGHT\n if i <= k\n dp[i] += 1 # No more cases...\n else\n dp[i] += (dp[i-1]%MODULO_BASE + dp[i-k]%MODULO_BASE) % MODULO_BASE # Came from prev R(i-1), from prev Z(i-k)\n end\n acc += dp[i]\n acc_dp[i] = acc\nend\n\nt.times {\n a, b = gets.split.map(&:to_i)\n # p \"#{acc_dp[b]} #{acc_dp[a-1]}\"\n puts (acc_dp[b] - acc_dp[a-1])%MODULO_BASE\n}"}], "negative_code": [{"source_code": "# coding: utf-8\n\n# 총 테스트 케이스 t 에 대해, 꽃을 a개부터 b개 사이로 먹는 경우의 수를 계산한다\n# 단 흰 꽃은 k개 단위로만 먹을 수 있다.\n# 예) k = 2일때, 꽃 4개를 먹을 때는 흰색만 4개를 먹을 수 있지만,\n# k = 3일때는 3개를 먹을 수 없다.\n# 이때 빨간 꽃 R과 흰꽃 W를 섞어서 먹을 수 있는 경우의 수를 modulo 10**9 + 7 로 계산\nMODULO_BASE = 10**9 + 7\nMAX_HEIGHT = 10**5\n# 혹시 k-Tree 문제 생각나십니까? 음 도움이 될지 모르겠는데\n# 각 자리에 올 수 있는건 R 또는 W입니다. 이것은 이진트리입니다.\n# 이진트리의 순회는 DFS와 BFS중 하나로 풀 수 있겠죠.\n# growing을 어떻게 할까요, 되는 노드만 계산합시다. 언제 망한 노드도 계산해요\n# D0: (root) (K = 3) Z = WWW (unbalanced)\n# D1: R1 Z3\n# D2: RR2 RZ4 ZR4 ZZ6\n# D3: RRR3 RRZ5 RZR5 RZZ7 ZRR5 ZRZ7 ZZR7 ZZZ9 \n# D4: RRRR4 RRRZ6 RRZR6 RRZZ8 RZRR6 RZRZ8 RZZR8 RZZZ10 ZRRR6 ZRRZ8 ZRZR8 ZRZZ10 ZZRR8 ZZRZ10 ZZZR10 ZZZZ12 \nt, k = gets.split.map(&:to_i)\n# 아 시발 이런 뜻이라니\n# 각 꽃의 개수를 목적치로 설정합니다.\n# 모든 경우에 대해 R로 채우는 1가지가 있으므로 dp[x] += 1을 적용합니다.\n# dp[x]로부터, k씩 더해 방문 가능한 개수를 1씩 증가시킵니다\n# dp[x+1]로 넘어가서 dp[x+1]에 대해 방문하였으므로 += 1을 수행하고, 여기로부터 방문 가능한 x+k배수를 모두 += 1합니다.\n# 자연스럽게 x' 가 x + k 와 같아지는 순간이 있습니다. 이때도 += 1을 적용합니다.\n# \ndp = Array.new(MAX_HEIGHT + k) { 0 } # 0번 인덱스 안써요\ndp[1] = 1\ndp[k] = 1\nacc_dp = Array.new(MAX_HEIGHT + k)\nacc_dp[0] = 0\nacc_dp[1] = 1\nacc = 1\nfor i in 2..MAX_HEIGHT\n if i < k\n dp[i] += 1 # No more cases...\n else\n dp[i] += (dp[i-1]%MODULO_BASE + dp[i-k]%MODULO_BASE) % MODULO_BASE # Came from prev R(i-1), from prev Z(i-k)\n end\n acc += dp[i]\n acc_dp[i] = acc\nend\n\nt.times {\n a, b = gets.split.map(&:to_i)\n puts (acc_dp[b] - acc_dp[a-1])%MODULO_BASE\n}"}, {"source_code": "# coding: utf-8\n\n# 총 테스트 케이스 t 에 대해, 꽃을 a개부터 b개 사이로 먹는 경우의 수를 계산한다\n# 단 흰 꽃은 k개 단위로만 먹을 수 있다.\n# 예) k = 2일때, 꽃 4개를 먹을 때는 흰색만 4개를 먹을 수 있지만,\n# k = 3일때는 3개를 먹을 수 없다.\n# 이때 빨간 꽃 R과 흰꽃 W를 섞어서 먹을 수 있는 경우의 수를 modulo 10**9 + 7 로 계산\nMODULO_BASE = 10**9 + 7\nMAX_HEIGHT = 10**5\n# 혹시 k-Tree 문제 생각나십니까? 음 도움이 될지 모르겠는데\n# 각 자리에 올 수 있는건 R 또는 W입니다. 이것은 이진트리입니다.\n# 이진트리의 순회는 DFS와 BFS중 하나로 풀 수 있겠죠.\n# growing을 어떻게 할까요, 되는 노드만 계산합시다. 언제 망한 노드도 계산해요\n# D0: (root) (K = 3) Z = WWW (unbalanced)\n# D1: R1 Z3\n# D2: RR2 RZ4 ZR4 ZZ6\n# D3: RRR3 RRZ5 RZR5 RZZ7 ZRR5 ZRZ7 ZZR7 ZZZ9 \n# D4: RRRR4 RRRZ6 RRZR6 RRZZ8 RZRR6 RZRZ8 RZZR8 RZZZ10 ZRRR6 ZRRZ8 ZRZR8 ZRZZ10 ZZRR8 ZZRZ10 ZZZR10 ZZZZ12 \nt, k = gets.split.map(&:to_i)\n# 아 시발 이런 뜻이라니\n# 각 꽃의 개수를 목적치로 설정합니다.\n# 모든 경우에 대해 R로 채우는 1가지가 있으므로 dp[x] += 1을 적용합니다.\n# dp[x]로부터, k씩 더해 방문 가능한 개수를 1씩 증가시킵니다\n# dp[x+1]로 넘어가서 dp[x+1]에 대해 방문하였으므로 += 1을 수행하고, 여기로부터 방문 가능한 x+k배수를 모두 += 1합니다.\n# 자연스럽게 x' 가 x + k 와 같아지는 순간이 있습니다. 이때도 += 1을 적용합니다.\n# \ndp = Array.new(MAX_HEIGHT + k) { 0 } # 0번 인덱스 안써요\n# 이렇게 초기화해야하는 이유가 있음\n# K = 1일때는 한 자리에 두가지 경우가 바로 발생됨. 그걸 표현하려면 이 방법이 좋다.\ndp[1] += 1\ndp[k] += 1\nacc_dp = Array.new(MAX_HEIGHT + k)\nacc_dp[0] = 0\nacc_dp[1] = dp[1] # 여기도 초기화를 이렇게 해야함, 왜 항상 처음이 1일거라 생각하는거지? K=1이면 이 자리는 2이다.\nacc = 1\nfor i in 2..MAX_HEIGHT\n if i <= k\n dp[i] += 1 # No more cases...\n else\n dp[i] += (dp[i-1]%MODULO_BASE + dp[i-k]%MODULO_BASE) % MODULO_BASE # Came from prev R(i-1), from prev Z(i-k)\n end\n acc += dp[i]\n acc_dp[i] = acc\nend\n\nt.times {\n a, b = gets.split.map(&:to_i)\n # p \"#{acc_dp[b]} #{acc_dp[a-1]}\"\n puts (acc_dp[b] - acc_dp[a-1])%MODULO_BASE\n}"}, {"source_code": "# coding: utf-8\n\n# 총 테스트 케이스 t 에 대해, 꽃을 a개부터 b개 사이로 먹는 경우의 수를 계산한다\n# 단 흰 꽃은 k개 단위로만 먹을 수 있다.\n# 예) k = 2일때, 꽃 4개를 먹을 때는 흰색만 4개를 먹을 수 있지만,\n# k = 3일때는 3개를 먹을 수 없다.\n# 이때 빨간 꽃 R과 흰꽃 W를 섞어서 먹을 수 있는 경우의 수를 modulo 10**9 + 7 로 계산\nMODULO_BASE = 10**9 + 7\nMAX_HEIGHT = 10**5\n# 혹시 k-Tree 문제 생각나십니까? 음 도움이 될지 모르겠는데\n# 각 자리에 올 수 있는건 R 또는 W입니다. 이것은 이진트리입니다.\n# 이진트리의 순회는 DFS와 BFS중 하나로 풀 수 있겠죠.\n# growing을 어떻게 할까요, 되는 노드만 계산합시다. 언제 망한 노드도 계산해요\n# D0: (root) (K = 3) Z = WWW (unbalanced)\n# D1: R1 Z3\n# D2: RR2 RZ4 ZR4 ZZ6\n# D3: RRR3 RRZ5 RZR5 RZZ7 ZRR5 ZRZ7 ZZR7 ZZZ9 \n# D4: RRRR4 RRRZ6 RRZR6 RRZZ8 RZRR6 RZRZ8 RZZR8 RZZZ10 ZRRR6 ZRRZ8 ZRZR8 ZRZZ10 ZZRR8 ZZRZ10 ZZZR10 ZZZZ12 \nt, k = gets.split.map(&:to_i)\n# 아 시발 이런 뜻이라니\n# 각 꽃의 개수를 목적치로 설정합니다.\n# 모든 경우에 대해 R로 채우는 1가지가 있으므로 dp[x] += 1을 적용합니다.\n# dp[x]로부터, k씩 더해 방문 가능한 개수를 1씩 증가시킵니다\n# dp[x+1]로 넘어가서 dp[x+1]에 대해 방문하였으므로 += 1을 수행하고, 여기로부터 방문 가능한 x+k배수를 모두 += 1합니다.\n# 자연스럽게 x' 가 x + k 와 같아지는 순간이 있습니다. 이때도 += 1을 적용합니다.\n# \ndp = Array.new(MAX_HEIGHT + k) { 0 } # 0번 인덱스 안써요\ndp[1] = 1\ndp[k] = 1\nacc_dp = Array.new(MAX_HEIGHT + k)\nacc_dp[0] = 0\nacc_dp[1] = 1\nacc = 1\nfor i in 2..MAX_HEIGHT\n if i < k\n dp[i] += 1 # No more cases...\n else\n dp[i] += (dp[i-1]%MODULO_BASE + dp[i-k]%MODULO_BASE) % MODULO_BASE # Came from prev R(i-1), from prev Z(i-k)\n end\n acc = (acc%MODULO_BASE + dp[i]%MODULO_BASE)%MODULO_BASE\n acc_dp[i] = acc\nend\n\nt.times {\n a, b = gets.split.map(&:to_i)\n puts (acc_dp[b] - acc_dp[a-1]).abs\n}"}, {"source_code": "# coding: utf-8\n\n# 총 테스트 케이스 t 에 대해, 꽃을 a개부터 b개 사이로 먹는 경우의 수를 계산한다\n# 단 흰 꽃은 k개 단위로만 먹을 수 있다.\n# 예) k = 2일때, 꽃 4개를 먹을 때는 흰색만 4개를 먹을 수 있지만,\n# k = 3일때는 3개를 먹을 수 없다.\n# 이때 빨간 꽃 R과 흰꽃 W를 섞어서 먹을 수 있는 경우의 수를 modulo 10**9 + 7 로 계산\nMODULO_BASE = 10**9 + 7\nMAX_HEIGHT = 10**5\n# 혹시 k-Tree 문제 생각나십니까? 음 도움이 될지 모르겠는데\n# 각 자리에 올 수 있는건 R 또는 W입니다. 이것은 이진트리입니다.\n# 이진트리의 순회는 DFS와 BFS중 하나로 풀 수 있겠죠.\n# growing을 어떻게 할까요, 되는 노드만 계산합시다. 언제 망한 노드도 계산해요\n# D0: (root) (K = 3) Z = WWW (unbalanced)\n# D1: R1 Z3\n# D2: RR2 RZ4 ZR4 ZZ6\n# D3: RRR3 RRZ5 RZR5 RZZ7 ZRR5 ZRZ7 ZZR7 ZZZ9 \n# D4: RRRR4 RRRZ6 RRZR6 RRZZ8 RZRR6 RZRZ8 RZZR8 RZZZ10 ZRRR6 ZRRZ8 ZRZR8 ZRZZ10 ZZRR8 ZZRZ10 ZZZR10 ZZZZ12 \nt, k = gets.split.map(&:to_i)\n# 아 시발 이런 뜻이라니\n# 각 꽃의 개수를 목적치로 설정합니다.\n# 모든 경우에 대해 R로 채우는 1가지가 있으므로 dp[x] += 1을 적용합니다.\n# dp[x]로부터, k씩 더해 방문 가능한 개수를 1씩 증가시킵니다\n# dp[x+1]로 넘어가서 dp[x+1]에 대해 방문하였으므로 += 1을 수행하고, 여기로부터 방문 가능한 x+k배수를 모두 += 1합니다.\n# 자연스럽게 x' 가 x + k 와 같아지는 순간이 있습니다. 이때도 += 1을 적용합니다.\n# \ndp = Array.new(MAX_HEIGHT + k) { 0 } # 0번 인덱스 안써요\n# 이렇게 초기화해야하는 이유가 있음\n# K = 1일때는 한 자리에 두가지 경우가 바로 발생됨. 그걸 표현하려면 이 방법이 좋다.\ndp[1] += 1\ndp[k] += 1\nacc_dp = Array.new(MAX_HEIGHT + k)\nacc_dp[0] = 0\nacc_dp[1] = dp[1] # 여기도 초기화를 이렇게 해야함, 왜 항상 처음이 1일거라 생각하는거지? K=1이면 이 자리는 2이다.\nacc = 1\nfor i in 2..MAX_HEIGHT\n if i < k\n dp[i] += 1 # No more cases...\n else\n dp[i] += (dp[i-1]%MODULO_BASE + dp[i-k]%MODULO_BASE) % MODULO_BASE # Came from prev R(i-1), from prev Z(i-k)\n end\n acc += dp[i]\n acc_dp[i] = acc\nend\n\nt.times {\n a, b = gets.split.map(&:to_i)\n # p \"#{acc_dp[b]} #{acc_dp[a-1]}\"\n puts (acc_dp[b]%MODULO_BASE - acc_dp[a-1]%MODULO_BASE)%MODULO_BASE\n}"}, {"source_code": "# coding: utf-8\n\n# 총 테스트 케이스 t 에 대해, 꽃을 a개부터 b개 사이로 먹는 경우의 수를 계산한다\n# 단 흰 꽃은 k개 단위로만 먹을 수 있다.\n# 예) k = 2일때, 꽃 4개를 먹을 때는 흰색만 4개를 먹을 수 있지만,\n# k = 3일때는 3개를 먹을 수 없다.\n# 이때 빨간 꽃 R과 흰꽃 W를 섞어서 먹을 수 있는 경우의 수를 modulo 10**9 + 7 로 계산\nMODULO_BASE = 10**9 + 7\nMAX_HEIGHT = 10**5\n# 혹시 k-Tree 문제 생각나십니까? 음 도움이 될지 모르겠는데\n# 각 자리에 올 수 있는건 R 또는 W입니다. 이것은 이진트리입니다.\n# 이진트리의 순회는 DFS와 BFS중 하나로 풀 수 있겠죠.\n# growing을 어떻게 할까요, 되는 노드만 계산합시다. 언제 망한 노드도 계산해요\n# D0: (root) (K = 3) Z = WWW (unbalanced)\n# D1: R1 Z3\n# D2: RR2 RZ4 ZR4 ZZ6\n# D3: RRR3 RRZ5 RZR5 RZZ7 ZRR5 ZRZ7 ZZR7 ZZZ9 \n# D4: RRRR4 RRRZ6 RRZR6 RRZZ8 RZRR6 RZRZ8 RZZR8 RZZZ10 ZRRR6 ZRRZ8 ZRZR8 ZRZZ10 ZZRR8 ZZRZ10 ZZZR10 ZZZZ12 \nt, k = gets.split.map(&:to_i)\n# 아 시발 이런 뜻이라니\n# 각 꽃의 개수를 목적치로 설정합니다.\n# 모든 경우에 대해 R로 채우는 1가지가 있으므로 dp[x] += 1을 적용합니다.\n# dp[x]로부터, k씩 더해 방문 가능한 개수를 1씩 증가시킵니다\n# dp[x+1]로 넘어가서 dp[x+1]에 대해 방문하였으므로 += 1을 수행하고, 여기로부터 방문 가능한 x+k배수를 모두 += 1합니다.\n# 자연스럽게 x' 가 x + k 와 같아지는 순간이 있습니다. 이때도 += 1을 적용합니다.\n# \ndp = Array.new(MAX_HEIGHT + k) { 0 } # 0번 인덱스 안써요\ndp[1] = 1\ndp[k] = 1\nacc_dp = Array.new(MAX_HEIGHT + k)\nacc_dp[0] = 0\nacc_dp[1] = 1\nacc = 1\nfor i in 2..MAX_HEIGHT\n if i < k\n dp[i] += 1 # No more cases...\n else\n dp[i] += (dp[i-1]%MODULO_BASE + dp[i-k]%MODULO_BASE) % MODULO_BASE # Came from prev R(i-1), from prev Z(i-k)\n end\n acc += dp[i]\n acc_dp[i] = acc\nend\n\nt.times {\n a, b = gets.split.map(&:to_i)\n puts (acc_dp[b] - acc_dp[a-1])\n}"}, {"source_code": "# coding: utf-8\n\n# 총 테스트 케이스 t 에 대해, 꽃을 a개부터 b개 사이로 먹는 경우의 수를 계산한다\n# 단 흰 꽃은 k개 단위로만 먹을 수 있다.\n# 예) k = 2일때, 꽃 4개를 먹을 때는 흰색만 4개를 먹을 수 있지만,\n# k = 3일때는 3개를 먹을 수 없다.\n# 이때 빨간 꽃 R과 흰꽃 W를 섞어서 먹을 수 있는 경우의 수를 modulo 10**9 + 7 로 계산\nMODULO_BASE = 10**9 + 7\nMAX_HEIGHT = 10**5\n# 혹시 k-Tree 문제 생각나십니까? 음 도움이 될지 모르겠는데\n# 각 자리에 올 수 있는건 R 또는 W입니다. 이것은 이진트리입니다.\n# 이진트리의 순회는 DFS와 BFS중 하나로 풀 수 있겠죠.\n# growing을 어떻게 할까요, 되는 노드만 계산합시다. 언제 망한 노드도 계산해요\n# D0: (root) (K = 3) Z = WWW (unbalanced)\n# D1: R1 Z3\n# D2: RR2 RZ4 ZR4 ZZ6\n# D3: RRR3 RRZ5 RZR5 RZZ7 ZRR5 ZRZ7 ZZR7 ZZZ9 \n# D4: RRRR4 RRRZ6 RRZR6 RRZZ8 RZRR6 RZRZ8 RZZR8 RZZZ10 ZRRR6 ZRRZ8 ZRZR8 ZRZZ10 ZZRR8 ZZRZ10 ZZZR10 ZZZZ12 \nt, k = gets.split.map(&:to_i)\n# 아 시발 이런 뜻이라니\n# 각 꽃의 개수를 목적치로 설정합니다.\n# 모든 경우에 대해 R로 채우는 1가지가 있으므로 dp[x] += 1을 적용합니다.\n# dp[x]로부터, k씩 더해 방문 가능한 개수를 1씩 증가시킵니다\n# dp[x+1]로 넘어가서 dp[x+1]에 대해 방문하였으므로 += 1을 수행하고, 여기로부터 방문 가능한 x+k배수를 모두 += 1합니다.\n# 자연스럽게 x' 가 x + k 와 같아지는 순간이 있습니다. 이때도 += 1을 적용합니다.\n# \ndp = Array.new(MAX_HEIGHT + k) { 0 } # 0번 인덱스 안써요\n# 이렇게 초기화해야하는 이유가 있음\n# K = 1일때는 한 자리에 두가지 경우가 바로 발생됨. 그걸 표현하려면 이 방법이 좋다.\ndp[1] += 1\ndp[k] += 1\nacc_dp = Array.new(MAX_HEIGHT + k)\nacc_dp[0] = 0\nacc_dp[1] = dp[1] # 여기도 초기화를 이렇게 해야함, 왜 항상 처음이 1일거라 생각하는거지? K=1이면 이 자리는 2이다.\nacc = 1\nfor i in 2..MAX_HEIGHT\n if i < k\n dp[i] += 1 # No more cases...\n else\n dp[i] += (dp[i-1]%MODULO_BASE + dp[i-k]%MODULO_BASE) % MODULO_BASE # Came from prev R(i-1), from prev Z(i-k)\n end\n acc += dp[i]\n acc_dp[i] = acc\nend\n\nt.times {\n a, b = gets.split.map(&:to_i)\n # p \"#{acc_dp[b]} #{acc_dp[a-1]}\"\n puts (acc_dp[b] - acc_dp[a-1])%MODULO_BASE\n}"}], "src_uid": "16c016c0735be1815c7b94c5c50516f1"} {"nl": {"description": "You talked to Polycarp and asked him a question. You know that when he wants to answer \"yes\", he repeats Yes many times in a row.Because of the noise, you only heard part of the answer — some substring of it. That is, if he answered YesYes, then you could hear esY, YesYes, sYes, e, but you couldn't Yess, YES or se.Determine if it is true that the given string $$$s$$$ is a substring of YesYesYes... (Yes repeated many times in a row).", "input_spec": "The first line of input data contains the singular $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases in the test. Each test case is described by a single string of Latin letters $$$s$$$ ($$$1 \\le |s| \\le 50$$$) — the part of Polycarp's answer that you heard, where $$$|s|$$$ — is the length of the string $$$s$$$.", "output_spec": "Output $$$t$$$ lines, each of which is the answer to the corresponding test case. As an answer, output \"YES\" if the specified string $$$s$$$ is a substring of the string YesYesYes...Yes (the number of words Yes is arbitrary), and \"NO\" otherwise. You can output the answer in any case (for example, the strings \"yEs\", \"yes\", \"Yes\" and \"YES\" will be recognized as a positive answer).", "sample_inputs": ["12\n\nYES\n\nesYes\n\ncodeforces\n\nes\n\nse\n\nYesY\n\nesYesYesYesYesYesYe\n\nseY\n\nYess\n\nsY\n\no\n\nYes"], "sample_outputs": ["NO\nYES\nNO\nYES\nNO\nYES\nYES\nNO\nNO\nYES\nNO\nYES"], "notes": null}, "positive_code": [{"source_code": "main = -> {\r\n s = gets.chomp\r\n yes = \"Yes\" * 50\r\n yes.include?(s) ? \"YES\" : \"NO\"\r\n}\r\n\r\nt = gets.to_i\r\nputs t.times.map{ main.call }.join(\"\\n\")"}, {"source_code": "ans = []\r\ngets.to_i.times do\r\n s = gets.chomp\r\n a = 'Yes' * 50\r\n ans << if a.include? s\r\n 'YES'\r\n else\r\n 'NO'\r\n end\r\nend\r\nputs ans\r\n"}, {"source_code": "gets.to_i.times do\n s=gets.chomp\n cur=nil\n s.each_char do|c|\n if cur==nil\n if c!='Y' and c!='e' and c!='s'\n cur=nil\n break\n else \n cur=c\n end\n elsif (cur=='Y' and c=='e') or (cur=='e' and c=='s') or (cur=='s' and c=='Y')\n cur=c\n else\n cur=nil \n break\n end\n end \n if cur==nil\n puts 'NO'\n else\n puts 'YES'\n end\nend"}], "negative_code": [], "src_uid": "3cd56870a96baf8860e9b7e89008d895"} {"nl": {"description": "You are asked to watch your nephew who likes to play with toy blocks in a strange way.He has $$$n$$$ boxes and the $$$i$$$-th box has $$$a_i$$$ blocks. His game consists of two steps: he chooses an arbitrary box $$$i$$$; he tries to move all blocks from the $$$i$$$-th box to other boxes. If he can make the same number of blocks in each of $$$n - 1$$$ other boxes then he will be happy, otherwise, will be sad. Note that your nephew can only move the blocks from the chosen box to the other boxes; he cannot move blocks from the other boxes.You don't want to make your nephew sad, so you decided to put several extra blocks into some boxes in such a way that no matter which box $$$i$$$ he chooses he won't be sad. What is the minimum number of extra blocks you need to put?", "input_spec": "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 integer $$$n$$$ ($$$2 \\le n \\le 10^5$$$) — the number of boxes. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$0 \\le a_i \\le 10^9$$$) — the number of blocks in each box. It's guaranteed that the sum of $$$n$$$ over test cases doesn't exceed $$$10^5$$$.", "output_spec": "For each test case, print a single integer — the minimum number of blocks you need to put. It can be proved that the answer always exists, i. e. the number of blocks is finite.", "sample_inputs": ["3\n3\n3 2 2\n4\n2 2 3 2\n3\n0 3 0"], "sample_outputs": ["1\n0\n3"], "notes": "NoteIn the first test case, you can, for example, put one extra block into the first box and make $$$a = [4, 2, 2]$$$. If your nephew chooses the box with $$$4$$$ blocks, then we will move two blocks to the second box and two blocks to the third box. If he chooses the box with $$$2$$$ blocks then he will move these two blocks to the other box with $$$2$$$ blocks.In the second test case, you don't need to put any extra blocks, since no matter which box your nephew chooses, he can always make other boxes equal.In the third test case, you should put $$$3$$$ extra blocks. For example, you can put $$$2$$$ blocks in the first box and $$$1$$$ block in the third box. You'll get array $$$a = [2, 3, 1]$$$."}, "positive_code": [{"source_code": "DEBUG = false\nMOD = 10**9+7\nYESNO = %w(Yes No)\n\ndef main\n t = int\n t.times do\n n = int\n a = ints\n\n seg = Segtree.new(a, 0) { |x, y| max(x, y) }\n sum = a.sum\n\n ans_max = 0\n n.times do |i|\n othermax = max(seg.prod(0, i), seg.prod(i+1, n))\n diff = othermax * (n-1) - sum\n if diff < 0\n diff %= n - 1\n end\n ans_max = max(ans_max, diff)\n end\n\n puts ans_max\n end\nend\n\ndef int; gets.to_i; end\ndef ints; gets.split.map &:to_i; end\ndef float; gets.to_f; end\ndef floats; gets.split.map &:to_f; end\ndef array(&convert); gets.split.map(&convert); end\ndef string; gets.chomp; end\ndef rep(n, &b); Array.new(n, &b); end\ndef yes; puts YESNO[0]; end\ndef no; puts YESNO[1]; end\ndef yesno t; t ? yes : no; end\ndef zip(xs, *yss); Enumerator.new { |y| xs.zip(*yss) { |a| y.yield(*a) } }; end\ndef sorted_uniq(xs); x = nil; xs.filter! { |y| c = x === y; x = y; !c }; end\ndef max(*xs); xs.max; end\ndef min(*xs); xs.min; end\ndef minmax(*xs); xs.minmax; end\ndef matrix(h, w, fill=nil, &block); return Array.new(h) { Array.new(w, &block) } if block_given?; Array.new(h) { [fill] * w }; end\ndef debug(x); if DEBUG; STDERR.puts (block_given? ? yield(x) : x).inspect; end; x; end\ndef debug_grid(grid, width = 1); grid.each { |row| STDERR.puts row.map { |x| x.inspect.ljust(width) }.join(\"\") } if DEBUG; grid; end\ndef if_debug; yield if DEBUG; end\n\ndef div_ceil(x, y); (x + y - 1) / y; end\ndef gcd(*xs); xs.inject(0) { |y, x| y.gcd(x) }; end\ndef factorial(n, mod); (2..n).inject(1) { |f, x| f * x % mod }; end\ndef cumsum(xs); ys = [0]; xs.each { |x| ys << x + ys[-1] }; ys; end\ndef cumdiff(ys); xs = []; xs.inject { |x, y| xs << (d = y - x); d }; end\ndef cumfold(ys, range); r = range.end; r -= 1 if range.exclusive_end?; ys[r] - ys[range.begin-1]; end\ndef mod_inv(x, mod); x.pow(mod-2, mod); end\ndef mod_div(x, y, mod); x * mod_inv(y, mod) % mod; end\ndef bitbrute(size, &block); (1<> i) }\n end\n\n def get(pos)\n @d[@leaf_size + pos]\n end\n\n def prod(l, r)\n return @e if l == r\n\n sml = @e\n smr = @e\n l += @leaf_size\n r += @leaf_size\n\n while l < r\n if l[0] == 1\n sml = @op.call(sml, @d[l])\n l += 1\n end\n if r[0] == 1\n r -= 1\n smr = @op.call(@d[r], smr)\n end\n l /= 2\n r /= 2\n end\n\n @op.call(sml, smr)\n end\n\n def all_prod\n @d[1]\n end\n\n def max_right(l, &block)\n return @n if l == @n\n\n f = proc(&block)\n\n l += @leaf_size\n sm = @e\n loop do\n l /= 2 while l.even?\n unless f.call(@op.call(sm, @d[l]))\n while l < @leaf_size\n l *= 2\n if f.call(@op.call(sm, @d[l]))\n sm = @op.call(sm, @d[l])\n l += 1\n end\n end\n\n return l - @leaf_size\n end\n\n sm = @op.call(sm, @d[l])\n l += 1\n break if (l & -l) == l\n end\n\n @n\n end\n\n def min_left(r, &block)\n return 0 if r == 0\n\n f = proc(&block)\n\n r += @leaf_size\n sm = @e\n loop do\n r -= 1\n r /= 2 while r > 1 && r.odd?\n unless f.call(@op.call(@d[r], sm))\n while r < @leaf_size\n r = r * 2 + 1\n if f.call(@op.call(@d[r], sm))\n sm = @op.call(@d[r], sm)\n r -= 1\n end\n end\n\n return r + 1 - @leaf_size\n end\n\n sm = @op.call(@d[r], sm)\n break if (r & -r) == r\n end\n\n 0\n end\n\n def update(k)\n @d[k] = @op.call(@d[2 * k], @d[2 * k + 1])\n end\n\n def inspect\n t = 0\n res = \"SegmentTree @e = #{@e}, @n = #{@n}, @leaf_size = #{@leaf_size} @op = #{@op}\\n \"\n a = @d[1, @d.size - 1]\n a.each_with_index do |e, i|\n res << e.to_s << ' '\n if t == i && i < @leaf_size\n res << \"\\n \"\n t = t * 2 + 2\n end\n end\n res\n end\nend\n\nmain"}, {"source_code": "testcases = gets.to_i\ntestcases.times do\n n = gets.to_i\n an = gets.chomp!.split.map(&:to_i)\n if an.size <= 2\n p 0\n else\n res = an.max * (n - 1) - an.sum\n p res >=0 ? res : res % (n-1)\n end\nend"}, {"source_code": "testcases = gets.to_i\ntestcases.times do\n n = gets.to_i\n an = gets.chomp!.split.map(&:to_i)\n an.sort!\n an.reverse!\n if an.size <= 2\n p 0\n else\n res = an[0] * (n - 1) - an.sum\n p res >=0 ? res : res % (n-1)\n end\nend\n"}], "negative_code": [{"source_code": "DEBUG = false\nMOD = 10**9+7\nYESNO = %w(Yes No)\n\ndef main\n t = int\n t.times do\n n = int\n a = ints\n\n seg = Segtree.new(a, 0) { |x, y| max(x, y) }\n sum = a.sum\n\n ans_min = 2e9\n n.times do |i|\n othermax = max(seg.prod(0, i), seg.prod(i+1, n))\n diff = othermax * (n-1) - sum\n diff %= n - 1 if diff < 0\n ans_min = min(ans_min, diff)\n end\n\n puts ans_min\n end\nend\n\ndef int; gets.to_i; end\ndef ints; gets.split.map &:to_i; end\ndef float; gets.to_f; end\ndef floats; gets.split.map &:to_f; end\ndef array(&convert); gets.split.map(&convert); end\ndef string; gets.chomp; end\ndef rep(n, &b); Array.new(n, &b); end\ndef yes; puts YESNO[0]; end\ndef no; puts YESNO[1]; end\ndef yesno t; t ? yes : no; end\ndef zip(xs, *yss); Enumerator.new { |y| xs.zip(*yss) { |a| y.yield(*a) } }; end\ndef sorted_uniq(xs); x = nil; xs.filter! { |y| c = x === y; x = y; !c }; end\ndef max(*xs); xs.max; end\ndef min(*xs); xs.min; end\ndef minmax(*xs); xs.minmax; end\ndef matrix(h, w, fill=nil, &block); return Array.new(h) { Array.new(w, &block) } if block_given?; Array.new(h) { [fill] * w }; end\ndef debug(x); if DEBUG; STDERR.puts (block_given? ? yield(x) : x).inspect; end; x; end\ndef debug_grid(grid, width = 1); grid.each { |row| STDERR.puts row.map { |x| x.inspect.ljust(width) }.join(\"\") } if DEBUG; grid; end\ndef if_debug; yield if DEBUG; end\n\ndef div_ceil(x, y); (x + y - 1) / y; end\ndef gcd(*xs); xs.inject(0) { |y, x| y.gcd(x) }; end\ndef factorial(n, mod); (2..n).inject(1) { |f, x| f * x % mod }; end\ndef cumsum(xs); ys = [0]; xs.each { |x| ys << x + ys[-1] }; ys; end\ndef cumdiff(ys); xs = []; xs.inject { |x, y| xs << (d = y - x); d }; end\ndef cumfold(ys, range); r = range.end; r -= 1 if range.exclusive_end?; ys[r] - ys[range.begin-1]; end\ndef mod_inv(x, mod); x.pow(mod-2, mod); end\ndef mod_div(x, y, mod); x * mod_inv(y, mod) % mod; end\ndef bitbrute(size, &block); (1<> i) }\n end\n\n def get(pos)\n @d[@leaf_size + pos]\n end\n\n def prod(l, r)\n return @e if l == r\n\n sml = @e\n smr = @e\n l += @leaf_size\n r += @leaf_size\n\n while l < r\n if l[0] == 1\n sml = @op.call(sml, @d[l])\n l += 1\n end\n if r[0] == 1\n r -= 1\n smr = @op.call(@d[r], smr)\n end\n l /= 2\n r /= 2\n end\n\n @op.call(sml, smr)\n end\n\n def all_prod\n @d[1]\n end\n\n def max_right(l, &block)\n return @n if l == @n\n\n f = proc(&block)\n\n l += @leaf_size\n sm = @e\n loop do\n l /= 2 while l.even?\n unless f.call(@op.call(sm, @d[l]))\n while l < @leaf_size\n l *= 2\n if f.call(@op.call(sm, @d[l]))\n sm = @op.call(sm, @d[l])\n l += 1\n end\n end\n\n return l - @leaf_size\n end\n\n sm = @op.call(sm, @d[l])\n l += 1\n break if (l & -l) == l\n end\n\n @n\n end\n\n def min_left(r, &block)\n return 0 if r == 0\n\n f = proc(&block)\n\n r += @leaf_size\n sm = @e\n loop do\n r -= 1\n r /= 2 while r > 1 && r.odd?\n unless f.call(@op.call(@d[r], sm))\n while r < @leaf_size\n r = r * 2 + 1\n if f.call(@op.call(@d[r], sm))\n sm = @op.call(@d[r], sm)\n r -= 1\n end\n end\n\n return r + 1 - @leaf_size\n end\n\n sm = @op.call(@d[r], sm)\n break if (r & -r) == r\n end\n\n 0\n end\n\n def update(k)\n @d[k] = @op.call(@d[2 * k], @d[2 * k + 1])\n end\n\n def inspect\n t = 0\n res = \"SegmentTree @e = #{@e}, @n = #{@n}, @leaf_size = #{@leaf_size} @op = #{@op}\\n \"\n a = @d[1, @d.size - 1]\n a.each_with_index do |e, i|\n res << e.to_s << ' '\n if t == i && i < @leaf_size\n res << \"\\n \"\n t = t * 2 + 2\n end\n end\n res\n end\nend\n\nmain"}, {"source_code": "DEBUG = false\nMOD = 10**9+7\nYESNO = %w(Yes No)\n\ndef main\n t = int\n t.times do\n n = int\n a = ints\n\n cumax1 = [0] + a\n 1.upto(n) { |i| cumax1[i] = cumax1[i-1] if cumax1[i] < cumax1[i-1] }\n cumax2 = a + [0]\n (n-1).downto(0) { |i| cumax1[i] = cumax1[i+1] if cumax1[i] < cumax1[i+1] }\n sum = a.sum\n\n ans_min = 2e9\n n.times do |i|\n othermax = max(cumax1[i], cumax2[i+1])\n diff = othermax * (n-1) - sum\n diff %= n - 1 if diff < 0\n ans_min = min(ans_min, diff)\n end\n\n puts ans_min\n end\nend\n\ndef int; gets.to_i; end\ndef ints; gets.split.map &:to_i; end\ndef float; gets.to_f; end\ndef floats; gets.split.map &:to_f; end\ndef array(&convert); gets.split.map(&convert); end\ndef string; gets.chomp; end\ndef rep(n, &b); Array.new(n, &b); end\ndef yes; puts YESNO[0]; end\ndef no; puts YESNO[1]; end\ndef yesno t; t ? yes : no; end\ndef zip(xs, *yss); Enumerator.new { |y| xs.zip(*yss) { |a| y.yield(*a) } }; end\ndef sorted_uniq(xs); x = nil; xs.filter! { |y| c = x === y; x = y; !c }; end\ndef max(*xs); xs.max; end\ndef min(*xs); xs.min; end\ndef minmax(*xs); xs.minmax; end\ndef matrix(h, w, fill=nil, &block); return Array.new(h) { Array.new(w, &block) } if block_given?; Array.new(h) { [fill] * w }; end\ndef debug(x); if DEBUG; STDERR.puts (block_given? ? yield(x) : x).inspect; end; x; end\ndef debug_grid(grid, width = 1); grid.each { |row| STDERR.puts row.map { |x| x.inspect.ljust(width) }.join(\"\") } if DEBUG; grid; end\ndef if_debug; yield if DEBUG; end\n\ndef div_ceil(x, y); (x + y - 1) / y; end\ndef gcd(*xs); xs.inject(0) { |y, x| y.gcd(x) }; end\ndef factorial(n, mod); (2..n).inject(1) { |f, x| f * x % mod }; end\ndef cumsum(xs); ys = [0]; xs.each { |x| ys << x + ys[-1] }; ys; end\ndef cumdiff(ys); xs = []; xs.inject { |x, y| xs << (d = y - x); d }; end\ndef cumfold(ys, range); r = range.end; r -= 1 if range.exclusive_end?; ys[r] - ys[range.begin-1]; end\ndef mod_inv(x, mod); x.pow(mod-2, mod); end\ndef mod_div(x, y, mod); x * mod_inv(y, mod) % mod; end\ndef bitbrute(size, &block); (1<> i) }\n end\n\n def get(pos)\n @d[@leaf_size + pos]\n end\n\n def prod(l, r)\n return @e if l == r\n\n sml = @e\n smr = @e\n l += @leaf_size\n r += @leaf_size\n\n while l < r\n if l[0] == 1\n sml = @op.call(sml, @d[l])\n l += 1\n end\n if r[0] == 1\n r -= 1\n smr = @op.call(@d[r], smr)\n end\n l /= 2\n r /= 2\n end\n\n @op.call(sml, smr)\n end\n\n def all_prod\n @d[1]\n end\n\n def max_right(l, &block)\n return @n if l == @n\n\n f = proc(&block)\n\n l += @leaf_size\n sm = @e\n loop do\n l /= 2 while l.even?\n unless f.call(@op.call(sm, @d[l]))\n while l < @leaf_size\n l *= 2\n if f.call(@op.call(sm, @d[l]))\n sm = @op.call(sm, @d[l])\n l += 1\n end\n end\n\n return l - @leaf_size\n end\n\n sm = @op.call(sm, @d[l])\n l += 1\n break if (l & -l) == l\n end\n\n @n\n end\n\n def min_left(r, &block)\n return 0 if r == 0\n\n f = proc(&block)\n\n r += @leaf_size\n sm = @e\n loop do\n r -= 1\n r /= 2 while r > 1 && r.odd?\n unless f.call(@op.call(@d[r], sm))\n while r < @leaf_size\n r = r * 2 + 1\n if f.call(@op.call(@d[r], sm))\n sm = @op.call(@d[r], sm)\n r -= 1\n end\n end\n\n return r + 1 - @leaf_size\n end\n\n sm = @op.call(@d[r], sm)\n break if (r & -r) == r\n end\n\n 0\n end\n\n def update(k)\n @d[k] = @op.call(@d[2 * k], @d[2 * k + 1])\n end\n\n def inspect\n t = 0\n res = \"SegmentTree @e = #{@e}, @n = #{@n}, @leaf_size = #{@leaf_size} @op = #{@op}\\n \"\n a = @d[1, @d.size - 1]\n a.each_with_index do |e, i|\n res << e.to_s << ' '\n if t == i && i < @leaf_size\n res << \"\\n \"\n t = t * 2 + 2\n end\n end\n res\n end\nend\n\nmain"}], "src_uid": "e75b88ce4341062c20b6014da1152d29"} {"nl": {"description": "Overall there are m actors in Berland. Each actor has a personal identifier — an integer from 1 to m (distinct actors have distinct identifiers). Vasya likes to watch Berland movies with Berland actors, and he has k favorite actors. He watched the movie trailers for the next month and wrote the following information for every movie: the movie title, the number of actors who starred in it, and the identifiers of these actors. Besides, he managed to copy the movie titles and how many actors starred there, but he didn't manage to write down the identifiers of some actors. Vasya looks at his records and wonders which movies may be his favourite, and which ones may not be. Once Vasya learns the exact cast of all movies, his favorite movies will be determined as follows: a movie becomes favorite movie, if no other movie from Vasya's list has more favorite actors.Help the boy to determine the following for each movie: whether it surely will be his favourite movie; whether it surely won't be his favourite movie; can either be favourite or not.", "input_spec": "The first line of the input contains two integers m and k (1 ≤ m ≤ 100, 1 ≤ k ≤ m) — the number of actors in Berland and the number of Vasya's favourite actors. The second line contains k distinct integers ai (1 ≤ ai ≤ m) — the identifiers of Vasya's favourite actors. The third line contains a single integer n (1 ≤ n ≤ 100) — the number of movies in Vasya's list. Then follow n blocks of lines, each block contains a movie's description. The i-th movie's description contains three lines: the first line contains string si (si consists of lowercase English letters and can have the length of from 1 to 10 characters, inclusive) — the movie's title, the second line contains a non-negative integer di (1 ≤ di ≤ m) — the number of actors who starred in this movie, the third line has di integers bi, j (0 ≤ bi, j ≤ m) — the identifiers of the actors who star in this movie. If bi, j = 0, than Vasya doesn't remember the identifier of the j-th actor. It is guaranteed that the list of actors for a movie doesn't contain the same actors. All movies have distinct names. The numbers on the lines are separated by single spaces.", "output_spec": "Print n lines in the output. In the i-th line print: 0, if the i-th movie will surely be the favourite; 1, if the i-th movie won't surely be the favourite; 2, if the i-th movie can either be favourite, or not favourite. ", "sample_inputs": ["5 3\n1 2 3\n6\nfirstfilm\n3\n0 0 0\nsecondfilm\n4\n0 0 4 5\nthirdfilm\n1\n2\nfourthfilm\n1\n5\nfifthfilm\n1\n4\nsixthfilm\n2\n1 0", "5 3\n1 3 5\n4\njumanji\n3\n0 0 0\ntheeagle\n5\n1 2 3 4 0\nmatrix\n3\n2 4 0\nsourcecode\n2\n2 4"], "sample_outputs": ["2\n2\n1\n1\n1\n2", "2\n0\n1\n1"], "notes": "NoteNote to the second sample: Movie jumanji can theoretically have from 1 to 3 Vasya's favourite actors. Movie theeagle has all three favourite actors, as the actor Vasya failed to remember, can only have identifier 5. Movie matrix can have exactly one favourite actor. Movie sourcecode doesn't have any favourite actors. Thus, movie theeagle will surely be favourite, movies matrix and sourcecode won't surely be favourite, and movie jumanji can be either favourite (if it has all three favourite actors), or not favourite."}, "positive_code": [{"source_code": "if ENV['USER'] != 'watashi'\n $stdin = open('input.txt')\n $> = open('output.txt', 'w')\nend\n\nI = ->{gets.split.map(&:to_i)}\nm, k = I[]\nf = I[]\nn, _ = I[]\na = Array.new(n){\n I[]\n I[]\n b = I[]\n x = b.count{|_| f.index(_)}\n y = b.count(0)\n [[x + y, k].min, [x, b.size - (m - k)].max]\n}\nputs a.map{|i|\n a.all?{|j| i === j || i[1] >= j[0]} ?\n 0 :\n a.all?{|j| i[0] >= j[1]} ? 2 : 1\n} * \"\\n\"\n"}, {"source_code": "if ENV['USER'] != 'watashi'\n $stdin = open('input.txt')\n $> = open('output.txt', 'w')\nend\n\nI = ->{gets.split.map(&:to_i)}\nm, k = I[]\nf = I[]\nn, _ = I[]\na = Array.new(n){\n I[]\n I[]\n b = I[]\n x = b.count{|_| f.index(_)}\n y = b.count(0)\n [[x + y, k].min, [x, b.size - (m - k)].max]\n}\nputs a.map{|i|\n a.all?{|j| i === j || i[1] >= j[0]} ?\n 0 :\n a.all?{|j| i[0] >= j[1]} ? 2 : 1\n} * \"\\n\"\n\n"}, {"source_code": "=begin\n , \\ / ,\n / \\ )\\__/( / \\\n / \\ (_\\ /_) / \\\n ____/_____\\__\\@ @/___/_____\\____\n | |\\../| |\n | \\VV/ |\n | ------hoi------- |\n |_________________________________|\n | /\\ / \\\\ \\ /\\ |\n | / V )) V \\ |\n |/ ` // ' \\|\n ` V '\n=end\n\n$stdin = open('input.txt')\n$> = open('output.txt', 'w')\n\n\nI = ->{gets.split.map(&:to_i)}\nm, k = I[]\nf = I[]\nn, _ = I[]\na = Array.new(n){\n I[]\n I[]\n b = I[]\n x = b.count{|_| f.index(_)}\n y = b.count(0)\n [[x + y, k].min, [x, b.size - (m - k)].max]\n}\nputs a.map{|i|\n a.all?{|j| i === j || i[1] >= j[0]} ?\n 0 :\n a.all?{|j| i[0] >= j[1]} ? 2 : 1\n} * \"\\n\" "}, {"source_code": "if ENV['USER'] != 'watashi'\n $stdin = open('input.txt')\n $> = open('output.txt', 'w')\nend\n\nI = ->{gets.split.map(&:to_i)}\nm, k = I[]\nf = I[]\nn, _ = I[]\na = Array.new(n){\n I[]\n I[]\n b = I[]\n x = b.count{|_| f.index(_)}\n y = b.count(0)\n [[x + y, k].min, [x, b.size - (m - k)].max]\n}\nputs a.map{|i|\n a.all?{|j| i === j || i[1] >= j[0]} ?\n 0 :\n a.all?{|j| i[0] >= j[1]} ? 2 : 1\n} * \"\\n\"\n\n"}, {"source_code": "if ENV['USER'] != 'watashi'\n $stdin = open('input.txt')\n $> = open('output.txt', 'w')\nend\n\nI = ->{gets.split.map(&:to_i)}\nm, k = I[]\nf = I[]\nn, _ = I[]\na = Array.new(n){\n I[]\n I[]\n b = I[]\n x = b.count{|_| f.index(_)}\n y = b.count(0)\n [[x + y, k].min, [x, b.size - (m - k)].max]\n}\nputs a.map{|i|\n a.all?{|j| i === j || i[1] >= j[0]} ?\n 0 :\n a.all?{|j| i[0] >= j[1]} ? 2 : 1\n} * \"\\n\"\n\n"}, {"source_code": "if ENV['USER'] != 'watashi'\n $stdin = open('input.txt')\n $> = open('output.txt', 'w')\nend\n\nI = ->{gets.split.map(&:to_i)}\nm, k = I[]\nf = I[]\nn, _ = I[]\na = Array.new(n){\n I[]\n I[]\n b = I[]\n x = b.count{|_| f.index(_)}\n y = b.count(0)\n [[x + y, k].min, [x, b.size - (m - k)].max]\n}\nputs a.map{|i|\n a.all?{|j| i === j || i[1] >= j[0]} ?\n 0 :\n a.all?{|j| i[0] >= j[1]} ? 2 : 1\n} * \"\\n\"\n\n"}, {"source_code": "if ENV['USER'] != 'watashi'\n $stdin = open('input.txt')\n $> = open('output.txt', 'w')\nend\n I = ->{gets.split.map(&:to_i)}\nm, k = I[]\nf = I[]\nn, _ = I[]\na = Array.new(n){\n I[]\n I[]\n b = I[]\n x = b.count{|_| f.index(_)}\n y = b.count(0)\n [[x + y, k].min, [x, b.size - (m - k)].max]\n}\nputs a.map{|i|\n a.all?{|j| i === j || i[1] >= j[0]} ?\n 0 :\n a.all?{|j| i[0] >= j[1]} ? 2 : 1\n} * \"\\n\""}, {"source_code": "if ENV['USER'] != 'watashi'\n $stdin = open('input.txt')\n $> = open('output.txt', 'w')\nend\n\nI = ->{gets.split.map(&:to_i)}\nm, k = I[]\nf = I[]\nn, _ = I[]\na = Array.new(n){\n I[]\n I[]\n b = I[]\n x = b.count{|_| f.index(_)}\n y = b.count(0)\n [[x + y, k].min, [x, b.size - (m - k)].max]\n}\nputs a.map{|i|\n a.all?{|j| i === j || i[1] >= j[0]} ?\n 0 :\n a.all?{|j| i[0] >= j[1]} ? 2 : 1\n} * \"\\n\"\n\n"}, {"source_code": "if ENV['USER'] != 'watashi'\n $stdin = open('input.txt')\n $> = open('output.txt', 'w')\nend\n\nI = ->{gets.split.map(&:to_i)}\nm, k = I[]\nf = I[]\nn, _ = I[]\na = Array.new(n){\n I[]\n I[]\n b = I[]\n x = b.count{|_| f.index(_)}\n y = b.count(0)\n [[x + y, k].min, [x, b.size - (m - k)].max]\n}\nputs a.map{|i|\n a.all?{|j| i === j || i[1] >= j[0]} ?\n 0 :\n a.all?{|j| i[0] >= j[1]} ? 2 : 1\n} * \"\\n\"\n\n"}, {"source_code": "if ENV['USER'] != 'watashi'\n $stdin = open('input.txt')\n $> = open('output.txt', 'w')\nend\n\nI = ->{gets.split.map(&:to_i)}\nm, k = I[]\nf = I[]\nn, _ = I[]\na = Array.new(n){\n I[]\n I[]\n b = I[]\n x = b.count{|_| f.index(_)}\n y = b.count(0)\n [[x + y, k].min, [x, b.size - (m - k)].max]\n}\nputs a.map{|i|\n a.all?{|j| i === j || i[1] >= j[0]} ?\n 0 :\n a.all?{|j| i[0] >= j[1]} ? 2 : 1\n} * \"\\n\"\n\n"}, {"source_code": "if ENV['USER'] != 'watashi'\n $stdin = open('input.txt')\n $> = open('output.txt', 'w')\nend\n\nI = ->{gets.split.map(&:to_i)}\nm, k = I[]\nf = I[]\nn, _ = I[]\na = Array.new(n){\n I[]\n I[]\n b = I[]\n x = b.count{|_| f.index(_)}\n y = b.count(0)\n [[x + y, k].min, [x, b.size - (m - k)].max]\n}\nputs a.map{|i|\n a.all?{|j| i === j || i[1] >= j[0]} ?\n 0 :\n a.all?{|j| i[0] >= j[1]} ? 2 : 1\n} * \"\\n\"\n\n"}], "negative_code": [], "src_uid": "93437df101a317c2306b37bf020a5012"} {"nl": {"description": "This morning Tolik has understood that while he was sleeping he had invented an incredible problem which will be a perfect fit for Codeforces! But, as a \"Discuss tasks\" project hasn't been born yet (in English, well), he decides to test a problem and asks his uncle.After a long time thinking, Tolik's uncle hasn't any ideas on how to solve it. But, he doesn't want to tell Tolik about his inability to solve it, so he hasn't found anything better than asking you how to solve this task.In this task you are given a cell field $$$n \\cdot m$$$, consisting of $$$n$$$ rows and $$$m$$$ columns, where point's coordinates $$$(x, y)$$$ mean it is situated in the $$$x$$$-th row and $$$y$$$-th column, considering numeration from one ($$$1 \\leq x \\leq n, 1 \\leq y \\leq m$$$). Initially, you stand in the cell $$$(1, 1)$$$. Every move you can jump from cell $$$(x, y)$$$, which you stand in, by any non-zero vector $$$(dx, dy)$$$, thus you will stand in the $$$(x+dx, y+dy)$$$ cell. Obviously, you can't leave the field, but also there is one more important condition — you're not allowed to use one vector twice. Your task is to visit each cell of the field exactly once (the initial cell is considered as already visited).Tolik's uncle is a very respectful person. Help him to solve this task!", "input_spec": "The first and only line contains two positive integers $$$n, m$$$ ($$$1 \\leq n \\cdot m \\leq 10^{6}$$$) — the number of rows and columns of the field respectively.", "output_spec": "Print \"-1\" (without quotes) if it is impossible to visit every cell exactly once. Else print $$$n \\cdot m$$$ pairs of integers, $$$i$$$-th from them should contain two integers $$$x_i, y_i$$$ ($$$1 \\leq x_i \\leq n, 1 \\leq y_i \\leq m$$$) — cells of the field in order of visiting, so that all of them are distinct and vectors of jumps between them are distinct too. Notice that the first cell should have $$$(1, 1)$$$ coordinates, according to the statement.", "sample_inputs": ["2 3", "1 1"], "sample_outputs": ["1 1\n1 3\n1 2\n2 2\n2 3\n2 1", "1 1"], "notes": "NoteThe vectors from the first example in the order of making jumps are $$$(0, 2), (0, -1), (1, 0), (0, 1), (0, -2)$$$."}, "positive_code": [{"source_code": "n,m = gets.split.map(&:to_i)\nx,y = 1,1\nt = n+1\nwhile x <= n/2\n puts \"#{x} #{y}\"\n (2*m-1).times do |i|\n x = n+1-x\n y = m+1-y\n y+=1 if i%2==1\n puts \"#{x} #{y}\"\n end\n x = n+1-x+1\n y = 1\nend\nif n%2 == 1\n x,y = (n+1)/2,1\n puts \"#{x} #{y}\"\n (m-1).times do |i|\n y = m+1-y\n y+=1 if i%2==1\n puts \"#{x} #{y}\"\n end\nend\n"}], "negative_code": [], "src_uid": "cc67015b9615f150aa06f7b8ed7e3152"} {"nl": {"description": "Vasya's house is situated in a forest, and there is a mushroom glade near it. The glade consists of two rows, each of which can be divided into n consecutive cells. For each cell Vasya knows how fast the mushrooms grow in this cell (more formally, how many grams of mushrooms grow in this cell each minute). Vasya spends exactly one minute to move to some adjacent cell. Vasya cannot leave the glade. Two cells are considered adjacent if they share a common side. When Vasya enters some cell, he instantly collects all the mushrooms growing there.Vasya begins his journey in the left upper cell. Every minute Vasya must move to some adjacent cell, he cannot wait for the mushrooms to grow. He wants to visit all the cells exactly once and maximize the total weight of the collected mushrooms. Initially, all mushrooms have a weight of 0. Note that Vasya doesn't need to return to the starting cell.Help Vasya! Calculate the maximum total weight of mushrooms he can collect.", "input_spec": "The first line contains the number n (1 ≤ n ≤ 3·105) — the length of the glade. The second line contains n numbers a1, a2, ..., an (1 ≤ ai ≤ 106) — the growth rate of mushrooms in the first row of the glade. The third line contains n numbers b1, b2, ..., bn (1 ≤ bi ≤ 106) is the growth rate of mushrooms in the second row of the glade.", "output_spec": "Output one number — the maximum total weight of mushrooms that Vasya can collect by choosing the optimal route. Pay attention that Vasya must visit every cell of the glade exactly once.", "sample_inputs": ["3\n1 2 3\n6 5 4", "3\n1 1000 10000\n10 100 100000"], "sample_outputs": ["70", "543210"], "notes": "NoteIn the first test case, the optimal route is as follows: Thus, the collected weight of mushrooms will be 0·1 + 1·2 + 2·3 + 3·4 + 4·5 + 5·6 = 70.In the second test case, the optimal route is as follows: Thus, the collected weight of mushrooms will be 0·1 + 1·10 + 2·100 + 3·1000 + 4·10000 + 5·100000 = 543210."}, "positive_code": [{"source_code": "N = gets.to_i\nas = []\nasum = 0\n2.times do |i|\n as[i] = gets.split.map(&:to_i)\n asum += as[i].inject(:+)\nend\n\nm1 = Array.new(2){Array.new(N)}\nm2 = Array.new(2){Array.new(N)}\n\nsum1, sum2 = 0, 0\nN.times do |i|\n m1[0][i] = i\n m1[1][i] = 2*N-1-i\n m2[1][i] = i\n m2[0][i] = 2*N-1-i\n sum1 += m1[0][i] * as[0][i] + m1[1][i] * as[1][i]\n sum2 += m2[0][i] * as[0][i] + m2[1][i] * as[1][i]\nend\n\nans = sum1\nsum = 0\nn1, n2 = 0, 0\nN.times do |i|\n if i % 2 == 0\n sum += (n1+n2)*as[0][i]\n asum -= as[0][i]\n sum1 -= (m1[0][i] + n1) * as[0][i]\n sum2 -= (m2[0][i] + n2) * as[0][i]\n sum2 += asum\n ans = sum+sum2 if ans < sum+sum2\n n2 += 1\n\n sum += (n1+n2)*as[1][i]\n asum -= as[1][i]\n sum1 -= (m1[1][i] + n1) * as[1][i]\n sum2 -= (m2[1][i] + n2) * as[1][i]\n sum1 += asum\n n1 += 1\n else\n sum += (n1+n2)*as[1][i]\n asum -= as[1][i]\n sum1 -= (m1[1][i] + n1) * as[1][i]\n sum2 -= (m2[1][i] + n2) * as[1][i]\n sum1 += asum\n ans = sum+sum1 if ans < sum+sum1\n n1 += 1\n\n sum += (n1+n2)*as[0][i]\n asum -= as[0][i]\n sum1 -= (m1[0][i] + n1) * as[0][i]\n sum2 -= (m2[0][i] + n2) * as[0][i]\n sum2 += asum\n n2 += 1\n end\nend\nputs ans"}], "negative_code": [{"source_code": "N = gets.to_i\nas = []\nasum = 0\n2.times do |i|\n as[i] = gets.split.map(&:to_i)\n asum += as[i].inject(:+)\nend\n\nm1 = Array.new(2){Array.new(N)}\nm2 = Array.new(2){Array.new(N)}\n\nsum1, sum2 = 0, 0\nN.times do |i|\n m1[0][i] = i\n m1[1][i] = 2*N-1-i\n m2[1][i] = i\n m2[0][i] = 2*N-1-i\n sum1 += m1[0][i] * as[0][i] + m1[1][i] * as[1][i]\n sum2 += m2[0][i] * as[0][i] + m2[1][i] * as[1][i]\nend\n\nans = sum1\nsum = 0\nn1, n2 = 0, 0\nN.times do |i|\n if i % 2 == 0\n sum += (n1+n2)*as[0][i]\n asum -= as[0][i]\n sum1 -= (m1[0][i] + n1) * as[0][i]\n sum2 -= (m2[0][i] + n2) * as[0][i]\n sum2 += asum\n ans = sum+sum2 if ans < sum+sum2\n n2 += 1\n\n sum += (n1+n2)*as[1][i]\n asum -= as[1][i]\n sum1 -= (m1[1][i] + n1) * as[1][i]\n sum2 -= (m2[1][i] + n2) * as[1][i]\n sum1 += asum\n n1 += 1\n else\n sum += (n1+n2)*as[1][i]\n asum -= as[1][i]\n sum1 -= (m1[1][i] + n1) * as[1][i]\n sum2 -= (m2[1][i] + n2) * as[1][i]\n sum2 += asum\n ans = sum+sum1 if ans < sum+sum1\n n2 += 1\n\n sum += (n1+n2)*as[0][i]\n asum -= as[0][i]\n sum1 -= (m1[0][i] + n1) * as[0][i]\n sum2 -= (m2[0][i] + n2) * as[0][i]\n sum1 += asum\n n1 += 1\n end\nend\nputs ans"}], "src_uid": "948429d3788b212e7763774f8cab097b"} {"nl": {"description": "A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).For a positive integer $$$n$$$, we call a permutation $$$p$$$ of length $$$n$$$ good if the following condition holds for every pair $$$i$$$ and $$$j$$$ ($$$1 \\le i \\le j \\le n$$$) — $$$(p_i \\text{ OR } p_{i+1} \\text{ OR } \\ldots \\text{ OR } p_{j-1} \\text{ OR } p_{j}) \\ge j-i+1$$$, where $$$\\text{OR}$$$ denotes the bitwise OR operation. In other words, a permutation $$$p$$$ is good if for every subarray of $$$p$$$, the $$$\\text{OR}$$$ of all elements in it is not less than the number of elements in that subarray. Given a positive integer $$$n$$$, output any good permutation of length $$$n$$$. We can show that for the given constraints such a permutation always exists.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 100$$$). Description of the test cases follows. The first and only line of every test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 100$$$).", "output_spec": "For every test, output any good permutation of length $$$n$$$ on a separate line. ", "sample_inputs": ["3\n1\n3\n7"], "sample_outputs": ["1\n3 1 2\n4 3 5 2 7 1 6"], "notes": "NoteFor $$$n = 3$$$, $$$[3,1,2]$$$ is a good permutation. Some of the subarrays are listed below. $$$3\\text{ OR }1 = 3 \\geq 2$$$ $$$(i = 1,j = 2)$$$ $$$3\\text{ OR }1\\text{ OR }2 = 3 \\geq 3$$$ $$$(i = 1,j = 3)$$$ $$$1\\text{ OR }2 = 3 \\geq 2$$$ $$$(i = 2,j = 3)$$$ $$$1 \\geq 1$$$ $$$(i = 2,j = 2)$$$ Similarly, you can verify that $$$[4,3,5,2,7,1,6]$$$ is also good."}, "positive_code": [{"source_code": "gets.to_i.times{|s|puts (1..gets.to_i).to_a.join(\" \")}"}, {"source_code": "t = gets.chomp.to_i\nt.times do \n n = gets.chomp.to_i\n n.downto (1) do |i|\n print i\n if i != 1\n print \" \"\n elsif i == 1\n print \"\\n\"\n end\n end\nend"}, {"source_code": "t = gets.to_i\nans = []\nt.times do\n n = gets.to_i\n arr = []\n n.times do |i|\n arr << i+1\n end\n ans << arr\nend\nans.each do |ar|\n ar.each do |x|\n print \"#{x} \"\n end\n puts \"\"\nend"}], "negative_code": [], "src_uid": "1b3ac752bc9c0b5e20a76f028d4b3c15"} {"nl": {"description": "Palo Alto is an unusual city because it is an endless coordinate line. It is also known for the office of Lyft Level 5.Lyft has become so popular so that it is now used by all $$$m$$$ taxi drivers in the city, who every day transport the rest of the city residents — $$$n$$$ riders.Each resident (including taxi drivers) of Palo-Alto lives in its unique location (there is no such pair of residents that their coordinates are the same).The Lyft system is very clever: when a rider calls a taxi, his call does not go to all taxi drivers, but only to the one that is the closest to that person. If there are multiple ones with the same distance, then to taxi driver with a smaller coordinate is selected.But one morning the taxi drivers wondered: how many riders are there that would call the given taxi driver if they were the first to order a taxi on that day? In other words, you need to find for each taxi driver $$$i$$$ the number $$$a_{i}$$$ — the number of riders that would call the $$$i$$$-th taxi driver when all drivers and riders are at their home?The taxi driver can neither transport himself nor other taxi drivers.", "input_spec": "The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n,m \\le 10^5$$$) — number of riders and taxi drivers. The second line contains $$$n + m$$$ integers $$$x_1, x_2, \\ldots, x_{n+m}$$$ ($$$1 \\le x_1 < x_2 < \\ldots < x_{n+m} \\le 10^9$$$), where $$$x_i$$$ is the coordinate where the $$$i$$$-th resident lives. The third line contains $$$n + m$$$ integers $$$t_1, t_2, \\ldots, t_{n+m}$$$ ($$$0 \\le t_i \\le 1$$$). If $$$t_i = 1$$$, then the $$$i$$$-th resident is a taxi driver, otherwise $$$t_i = 0$$$. It is guaranteed that the number of $$$i$$$ such that $$$t_i = 1$$$ is equal to $$$m$$$.", "output_spec": "Print $$$m$$$ integers $$$a_1, a_2, \\ldots, a_{m}$$$, where $$$a_i$$$ is the answer for the $$$i$$$-th taxi driver. The taxi driver has the number $$$i$$$ if among all the taxi drivers he lives in the $$$i$$$-th smallest coordinate (see examples for better understanding).", "sample_inputs": ["3 1\n1 2 3 10\n0 0 1 0", "3 2\n2 3 4 5 6\n1 0 0 0 1", "1 4\n2 4 6 10 15\n1 1 1 1 0"], "sample_outputs": ["3", "2 1", "0 0 0 1"], "notes": "NoteIn the first example, we have only one taxi driver, which means an order from any of $$$n$$$ riders will go to him.In the second example, the first taxi driver lives at the point with the coordinate $$$2$$$, and the second one lives at the point with the coordinate $$$6$$$. Obviously, the nearest taxi driver to the rider who lives on the $$$3$$$ coordinate is the first one, and to the rider who lives on the coordinate $$$5$$$ is the second one. The rider who lives on the $$$4$$$ coordinate has the same distance to the first and the second taxi drivers, but since the first taxi driver has a smaller coordinate, the call from this rider will go to the first taxi driver.In the third example, we have one rider and the taxi driver nearest to him is the fourth one."}, "positive_code": [{"source_code": "n, m = gets.split.map(&:to_i)\nX = gets.split.map(&:to_i)\nT = gets.split.map(&:to_i)\n\nTM = X.select.with_index do |val, i|\n T[i] != 0\nend\n\nXR = X.select.with_index do |val, i|\n T[i] == 0\nend\n\nR = Array.new(m, 0)\n\ni = 0\nXR.each do |x|\n i += 1 while i < (m - 1) && (TM[i + 1] - x).abs < (TM[i] - x).abs\n R[i] += 1\nend\n\nR.each { |x| print(x, \" \") }\n"}], "negative_code": [], "src_uid": "56ea328f84b2930656ff5eb9b8fda8e0"} {"nl": {"description": "Phoenix loves beautiful arrays. An array is beautiful if all its subarrays of length $$$k$$$ have the same sum. A subarray of an array is any sequence of consecutive elements.Phoenix currently has an array $$$a$$$ of length $$$n$$$. He wants to insert some number of integers, possibly zero, into his array such that it becomes beautiful. The inserted integers must be between $$$1$$$ and $$$n$$$ inclusive. Integers may be inserted anywhere (even before the first or after the last element), and he is not trying to minimize the number of inserted integers.", "input_spec": "The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 50$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le k \\le n \\le 100$$$). The second line of each test case contains $$$n$$$ space-separated integers ($$$1 \\le a_i \\le n$$$) — the array that Phoenix currently has. This array may or may not be already beautiful.", "output_spec": "For each test case, if it is impossible to create a beautiful array, print -1. Otherwise, print two lines. The first line should contain the length of the beautiful array $$$m$$$ ($$$n \\le m \\le 10^4$$$). You don't need to minimize $$$m$$$. The second line should contain $$$m$$$ space-separated integers ($$$1 \\le b_i \\le n$$$) — a beautiful array that Phoenix can obtain after inserting some, possibly zero, integers into his array $$$a$$$. You may print integers that weren't originally in array $$$a$$$. If there are multiple solutions, print any. It's guaranteed that if we can make array $$$a$$$ beautiful, we can always make it with resulting length no more than $$$10^4$$$.", "sample_inputs": ["4\n4 2\n1 2 2 1\n4 3\n1 2 2 1\n3 2\n1 2 3\n4 4\n4 3 4 2"], "sample_outputs": ["5\n1 2 1 2 1\n4\n1 2 2 1\n-1\n7\n4 3 2 1 4 3 2"], "notes": "NoteIn the first test case, we can make array $$$a$$$ beautiful by inserting the integer $$$1$$$ at index $$$3$$$ (in between the two existing $$$2$$$s). Now, all subarrays of length $$$k=2$$$ have the same sum $$$3$$$. There exists many other possible solutions, for example: $$$2, 1, 2, 1, 2, 1$$$ $$$1, 2, 1, 2, 1, 2$$$ In the second test case, the array is already beautiful: all subarrays of length $$$k=3$$$ have the same sum $$$5$$$.In the third test case, it can be shown that we cannot insert numbers to make array $$$a$$$ beautiful.In the fourth test case, the array $$$b$$$ shown is beautiful and all subarrays of length $$$k=4$$$ have the same sum $$$10$$$. There exist other solutions also."}, "positive_code": [{"source_code": "require 'set'\n\n$cpp = []\ndef solve arr, k\n return [arr.length, arr] if arr.length == k\n uniq = arr.to_set\n return [-1, nil] if uniq.length > k\n expect = uniq.to_a\n expect << 1 while expect.length < k\n idx = 0\n iter = 0\n arr.each do |i|\n until i == expect[idx % k]\n $cpp << expect[idx % k]\n idx += 1\n end\n $cpp << i\n idx += 1\n end\n [$cpp.length, $cpp]\nend\n\nt = STDIN.gets.chomp.to_i\n\n(1..t).each do |i|\n n, k = STDIN.gets.chomp.split.map {|i| i.to_i}\n arr = STDIN.gets.chomp.split.map {|i| i.to_i}\n res = solve arr, k\n puts res[0]\n puts res[1].join(\" \") unless res[0] == -1\n $cpp = []\nend\n"}, {"source_code": "require 'set'\n\ndef solve arr, k\n return [arr.length, arr] if arr.length == k\n uniq = arr.to_set\n return [-1, nil] if uniq.length > k\n res = []\n expect = uniq.to_a\n expect << 1 while expect.length < k\n idx = 0\n iter = 0\n arr.each do |i|\n until i == expect[idx % k]\n res << expect[idx % k]\n idx += 1\n end\n res << i\n idx += 1\n end\n [res.length, res]\nend\n\nt = STDIN.gets.chomp.to_i\n\n(1..t).each do |i|\n n, k = STDIN.gets.chomp.split.map {|i| i.to_i}\n arr = STDIN.gets.chomp.split.map {|i| i.to_i}\n res = solve arr, k\n puts res[0]\n puts res[1].join(\" \") unless res[0] == -1\nend\n"}], "negative_code": [], "src_uid": "80d4b2d01215b12ebd89b8ee2d1ac6ed"} {"nl": {"description": "A widely known among some people Belarusian sport programmer Lesha decided to make some money to buy a one square meter larger flat. To do this, he wants to make and carry out a Super Rated Match (SRM) on the site Torcoder.com. But there's a problem — a severe torcoder coordinator Ivan does not accept any Lesha's problem, calling each of them an offensive word \"duped\" (that is, duplicated). And one day they nearely quarrelled over yet another problem Ivan wouldn't accept.You are invited to act as a fair judge and determine whether the problem is indeed brand new, or Ivan is right and the problem bears some resemblance to those used in the previous SRMs.You are given the descriptions of Lesha's problem and each of Torcoder.com archive problems. The description of each problem is a sequence of words. Besides, it is guaranteed that Lesha's problem has no repeated words, while the description of an archive problem may contain any number of repeated words.The \"similarity\" between Lesha's problem and some archive problem can be found as follows. Among all permutations of words in Lesha's problem we choose the one that occurs in the archive problem as a subsequence. If there are multiple such permutations, we choose the one with the smallest number of inversions. Then the \"similarity\" of a problem can be written as , where n is the number of words in Lesha's problem and x is the number of inversions in the chosen permutation. Note that the \"similarity\" p is always a positive integer.The problem is called brand new if there is not a single problem in Ivan's archive which contains a permutation of words from Lesha's problem as a subsequence.Help the boys and determine whether the proposed problem is new, or specify the problem from the archive which resembles Lesha's problem the most, otherwise.", "input_spec": "The first line contains a single integer n (1 ≤ n ≤ 15) — the number of words in Lesha's problem. The second line contains n space-separated words — the short description of the problem. The third line contains a single integer m (1 ≤ m ≤ 10) — the number of problems in the Torcoder.com archive. Next m lines contain the descriptions of the problems as \"k s1 s2 ... sk\", where k (1 ≤ k ≤ 500000) is the number of words in the problem and si is a word of the problem description. All words from all problem descriptions contain no more than 10 lowercase English letters. It is guaranteed that the total length of words in all problem descriptions does not exceed 500015.", "output_spec": "If Lesha's problem is brand new, print string \"Brand new problem!\" (without quotes). Otherwise, on the first line print the index of the archive problem which resembles Lesha's problem most. If there are multiple such problems, print the one with the smallest index. On the second line print a string consisting of characters [:, character | repeated p times, and characters :], where p is the \"similarity\" between this problem and Lesha's one. The archive problems are numbered starting from one in the order in which they are given in the input.", "sample_inputs": ["4\nfind the next palindrome\n1\n10 find the previous palindrome or print better luck next time", "3\nadd two numbers\n3\n1 add\n2 two two\n3 numbers numbers numbers", "4\nthese papers are formulas\n3\n6 what are these formulas and papers\n5 papers are driving me crazy\n4 crazy into the night", "3\nadd two decimals\n5\n4 please two decimals add\n5 decimals want to be added\n4 two add decimals add\n4 add one two three\n7 one plus two plus three equals six"], "sample_outputs": ["1\n[:||||||:]", "Brand new problem!", "1\n[:||||:]", "3\n[:|||:]"], "notes": "NoteLet us remind you that the number of inversions is the number of pairs of words that follow in the permutation not in their original order. Thus, for example, if the original problem is \"add two numbers\", then permutation \"numbers add two\" contains two inversions — pairs of words \"numbers\" and \"add\", \"numbers\" and \"two\". Sequence b1,  b2,  ...,  bk is a subsequence of sequence a1, a2,  ...,  an if there exists such a set of indices 1 ≤ i1 <  i2 < ...   < ik ≤ n that aij  =  bj (in other words, if sequence b can be obtained from a by deleting some of its elements).In the first test case the first problem contains the \"find the palindrome next\" permutation as a subsequence, in which the number of inversions equals 1 (words \"palindrome\" and \"next\").In the second test case there is no problem that contains a permutation of words from Lesha's problem as a subsequence."}, "positive_code": [{"source_code": "#!/usr/bin/ruby1.9\n\nn = STDIN.readline.to_i\nproblem = STDIN.readline.split\nm = STDIN.readline.to_i\n\nbest_candidate_index = -1\nmax_similarity = 0\n\ndef find_similarity(problem, pi, candidate, ci, permutation)\n\treturn 0 if ci == candidate.length\n\ta = find_similarity(problem, pi, candidate, ci+1, permutation)\n\tpermutation[pi] = candidate[ci]\n\tif pi+1 < problem.length\n\t\tb = find_similarity(problem, pi+1, candidate, ci+1, permutation)\n\telsif permutation.sort != problem.sort\n\t\tb = 0\n\telse\n\t\tinvert = []\n\t\tpermutation.each {|word| invert.push(problem.index(word)) }\n\t\tx = 0\n\t\tn = invert.length\n\t\tn.times do |j|\n\t\t\tj.times do |i|\n\t\t\t\tx += 1 if invert[i] > invert[j]\n\t\t\tend\n\t\tend\n\t\tb = n*(n-1)/2 - x + 1\n\tend\n\treturn (a > b ? a : b)\nend\n\nm.times do |candidate_index|\n\tcandidate = STDIN.readline.split[1..-1]\n\tpermutation = Array.new(n)\n\tsimilarity = find_similarity(problem, 0, candidate, 0, permutation)\n\tif similarity > max_similarity\n\t\tmax_similarity = similarity\n\t\tbest_candidate_index = candidate_index\n\tend\nend\n\nif max_similarity == 0\n\tputs 'Brand new problem!'\nelse\n\tputs best_candidate_index+1\n\tputs '[:%s:]' % ('|'*max_similarity)\nend\n"}], "negative_code": [], "src_uid": "18f44ab990ef841c947e2da831321845"} {"nl": {"description": "Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off.The building consists of n floors with stairs at the left and the right sides. Each floor has m rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2 columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms.Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights.Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.", "input_spec": "The first line contains two integers n and m (1 ≤ n ≤ 15 and 1 ≤ m ≤ 100) — the number of floors and the number of rooms in each floor, respectively. The next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor. The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.", "output_spec": "Print a single integer — the minimum total time needed to turn off all the lights.", "sample_inputs": ["2 2\n0010\n0100", "3 4\n001000\n000010\n000010", "4 3\n01110\n01110\n01110\n01110"], "sample_outputs": ["5", "12", "18"], "notes": "NoteIn the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs.In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor.In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor."}, "positive_code": [{"source_code": "def normalize(a)\n a.length.times do |i|\n l = a[i].index('1')\n r = a[i].rindex('1')\n l = l + 1 if l != nil\n r = r + 1 if r != nil\n a[i] = {:l => l, :r => r}\n end\nend\n\n\nn, m = gets.chomp.split.map(&:to_i)\nm += 2\na = []\ncur_pos = 1\nans = n * m\n\nvars = [:l, :r].repeated_permutation(n).to_a\n\nn.times do |x|\n a.push(gets.chomp)\nend\n\na.reverse!\nnormalize(a)\n\nwhile n > 0 and a[-1][:l] == nil\n n -= 1\n a.pop\nend\n\nif n == 0\n puts 0\n exit\nend\n\nvars.length.times do |i|\n if vars[i][0] != :r\n lans = 0\n (1..n-1).each do |j|\n if vars[i][j-1] != vars[i][j]\n lans += (m - 1)\n elsif a[j-1][:l] == nil\n lans += 0\n elsif vars[i][j] == :r\n lans += (m - a[j-1][:l]) * 2\n elsif vars[i][j] == :l\n lans += (a[j-1][:r] - 1) * 2\n end\n end\n if a[n-1][:l] == nil\n lans += 0\n elsif vars[i][n-1] == :r\n lans += (m - a[n-1][:l])\n else\n lans += (a[n-1][:r] - 1)\n end\n if lans < ans\n ans = lans\n end\n end\nend\n\nans += n - 1\nputs ans\n"}, {"source_code": "N, M = gets.chomp.split(\" \").map(&:to_i)\n$bldg = Array.new()\n\nnflr = 0\nN.times do\n flr = gets.chomp.split(\"\").map(&:to_i)\n $bldg << flr\n nflr += 1 if flr.include?(1)\nend\n$bldg.reverse!\n$ret = []\n\ndef hausmeister(i, s, t, last)\n curr = 0\n flr = $bldg[i]\n if flr.include?(1) then\n if s == 0 then\n pos = flr.rindex(1)\n curr += pos\n else\n pos = flr.index(1)\n curr += (M + 2 - pos - 1)\n end\n left = pos + 1\n right = M + 2 - pos\n if i == N - 1 then\n $ret << t + curr\n return t + curr\n end\n hausmeister(i+1, 0, t + curr + left, left)\n hausmeister(i+1, -1, t + curr + right, right)\n else\n if i == N - 1 then\n $ret << t - last\n return t - last\n end\n if s == 0 then\n pos = 0\n else\n pos = M + 1\n end\n left = pos + 1\n right = M + 2 - pos\n hausmeister(i+1, 0, t + left, last + left)\n hausmeister(i+1, -1, t + right, last + right)\n end\nend\nif nflr > 0 then\n hausmeister(0, 0, 0, 0)\n puts $ret.min\nelse\n puts 0\nend\n"}], "negative_code": [{"source_code": "n, M = gets.chomp.split(\" \").map(&:to_i)\n$bldg = Array.new()\nn.times do\n flr = gets.chomp.split(\"\").map(&:to_i)\n $bldg << flr if flr.include?(1)\nend\n$bldg.reverse!\n$ret = []\nN = $bldg.length\ndef hausmeister(i, s, t)\n curr = 0\n flr = $bldg[i]\n\n if flr.include?(1) then\n if s == 0 then\n pos = flr.rindex(1)\n curr += pos\n else\n pos = flr.index(1)\n curr += (M + 2 - pos - 1)\n end\n left = pos + 1\n right = M + 2 - pos\n if i == N - 1 then\n $ret << t + curr\n return t + curr\n end\n hausmeister(i+1, 0, t + curr + left)\n hausmeister(i+1, -1, t + curr + right)\n else\n if i == N - 1 then\n $ret << t\n return t + curr\n end\n if s == 0 then\n pos = 0\n else\n pos = M + 1\n end\n left = pos + 1\n right = M + 2 - pos\n hausmeister(i+1, 0, t + left)\n hausmeister(i+1, -1, t + right)\n end\nend\nif N > 0 then\n hausmeister(0, 0, 0)\n puts $ret.min\nelse\n puts 0\nend"}, {"source_code": "N, M = gets.chomp.split(\" \").map(&:to_i)\n$bldg = Array.new()\nN.times do\n flr = gets.chomp.split(\"\").map(&:to_i)\n $bldg << flr\nend\n$bldg.reverse!\n$ret = []\ndef hausmeister(i, s, t)\n curr = 0\n flr = $bldg[i]\n\n nlight = flr.inject(:+)\n if nlight > 0 then\n if s == 0 then\n pos = flr.rindex(1)\n curr += pos\n else\n pos = flr.index(1)\n curr += (M + 2 - pos - 1)\n end\n left = pos + 1\n right = M + 2 - pos\n if i == N - 1 then\n $ret << t + curr\n return t + curr\n end\n hausmeister(i+1, 0, t + curr + left)\n hausmeister(i+1, -1, t + curr + right)\n else\n if i == N - 1 then\n $ret << t + curr\n return t + curr\n end\n if s == 0 then\n pos = 0\n else\n pos = M + 1\n end\n left = pos + 1\n right = M + 2 - pos\n hausmeister(i+1, 0, t + left)\n hausmeister(i+1, -1, t + right)\n end\nend\n\nhausmeister(0, 0, 0)\nputs $ret.min"}, {"source_code": "N, M = gets.chomp.split(\" \").map(&:to_i)\n$bldg = Array.new()\n\nnflr = 0\nN.times do\n flr = gets.chomp.split(\"\").map(&:to_i)\n $bldg << flr\n nflr += 1 if flr.include?(1)\nend\n$bldg.reverse!\n$ret = []\n\ndef hausmeister(i, s, t, last)\n curr = 0\n flr = $bldg[i]\n if flr.include?(1) then\n if s == 0 then\n pos = flr.rindex(1)\n curr += pos\n else\n pos = flr.index(1)\n curr += (M + 2 - pos - 1)\n end\n left = pos + 1\n right = M + 2 - pos\n if i == N - 1 then\n $ret << t + curr\n return t + curr\n end\n hausmeister(i+1, 0, t + curr + left, left)\n hausmeister(i+1, -1, t + curr + right, right)\n else\n if i == N - 1 then\n $ret << t - last\n return t - last\n end\n if s == 0 then\n pos = 0\n else\n pos = M + 1\n end\n left = pos + 1\n right = M + 2 - pos\n hausmeister(i+1, 0, t + left, left)\n hausmeister(i+1, -1, t + right, right)\n end\nend\nif nflr > 0 then\n hausmeister(0, 0, 0, 0)\n puts $ret.min\nelse\n puts 0\nend\n"}], "src_uid": "55070f8e5dba8a6ec669a53a169e557d"} {"nl": {"description": "Vasya became interested in bioinformatics. He's going to write an article about similar cyclic DNA sequences, so he invented a new method for determining the similarity of cyclic sequences.Let's assume that strings s and t have the same length n, then the function h(s, t) is defined as the number of positions in which the respective symbols of s and t are the same. Function h(s, t) can be used to define the function of Vasya distance ρ(s, t): where is obtained from string s, by applying left circular shift i times. For example, ρ(\"AGC\", \"CGT\") =  h(\"AGC\", \"CGT\") + h(\"AGC\", \"GTC\") + h(\"AGC\", \"TCG\") +  h(\"GCA\", \"CGT\") + h(\"GCA\", \"GTC\") + h(\"GCA\", \"TCG\") +  h(\"CAG\", \"CGT\") + h(\"CAG\", \"GTC\") + h(\"CAG\", \"TCG\") =  1 + 1 + 0 + 0 + 1 + 1 + 1 + 0 + 1 = 6Vasya found a string s of length n on the Internet. Now he wants to count how many strings t there are such that the Vasya distance from the string s attains maximum possible value. Formally speaking, t must satisfy the equation: .Vasya could not try all possible strings to find an answer, so he needs your help. As the answer may be very large, count the number of such strings modulo 109 + 7.", "input_spec": "The first line of the input contains a single integer n (1 ≤ n ≤ 105). The second line of the input contains a single string of length n, consisting of characters \"ACGT\".", "output_spec": "Print a single number — the answer modulo 109 + 7.", "sample_inputs": ["1\nC", "2\nAG", "3\nTTT"], "sample_outputs": ["1", "4", "1"], "notes": "NotePlease note that if for two distinct strings t1 and t2 values ρ(s, t1) и ρ(s, t2) are maximum among all possible t, then both strings must be taken into account in the answer even if one of them can be obtained by a circular shift of another one.In the first sample, there is ρ(\"C\", \"C\") = 1, for the remaining strings t of length 1 the value of ρ(s, t) is 0.In the second sample, ρ(\"AG\", \"AG\") = ρ(\"AG\", \"GA\") = ρ(\"AG\", \"AA\") = ρ(\"AG\", \"GG\") = 4.In the third sample, ρ(\"TTT\", \"TTT\") = 27"}, "positive_code": [{"source_code": "n=gets.to_i\ns=gets\na=\"ACGT\".chars.map{|c|s.count(c)}\np (a.count(a.max)**n)%1000000007\n"}, {"source_code": "n=gets.to_i\ns=gets\na=\"ACGT\".chars.map{|c|s.count(c)}\np (a.count(a.max)**n)%1000000007\n"}, {"source_code": "n=gets.to_i\ns=gets\na=\"ACGT\".chars.map{|c|s.count(c)}\np (a.count(a.max)**n)%1000000007\n"}, {"source_code": "n=gets.to_i\ns=gets\na=\"ACGT\".chars.map{|c|s.count(c)}\np (a.count(a.max)**n)%1000000007\n"}, {"source_code": "n=gets.to_i\ns=gets\na=\"ACGT\".chars.map{|c|s.count(c)}\np (a.count(a.max)**n)%1000000007\n"}, {"source_code": "n = gets.to_i\ns = gets.chomp\nm = [s.count(\"A\"), s.count(\"C\"), s.count(\"T\"), s.count(\"G\")]\ncntmax = m.max\ncnt = 0\nm.each{|mm| cnt += 1 if mm == cntmax}\nputs (cnt**n)%(10**9+7)\n"}, {"source_code": "n=gets.to_i\ns=gets\na=\"ACGT\".chars.map{|c|s.count(c)}\np (a.count(a.max)**n)%1000000007\n"}, {"source_code": "n=gets.to_i\ns=gets\na=\"ACGT\".chars.map{|c|s.count(c)}\np (a.count(a.max)**n)%1000000007\n"}, {"source_code": "n=gets.to_i\ns=gets\na=\"ACGT\".chars.map{|c|s.count(c)}\np (a.count(a.max)**n)%1000000007\n"}, {"source_code": "n=gets.to_i\ns=gets\na=\"ACGT\".chars.map{|c|s.count(c)}\np (a.count(a.max)**n)%1000000007"}, {"source_code": "n=gets.to_i\ns=gets\na=\"ACGT\".chars.map{|c|s.count(c)}\np (a.count(a.max)**n)%1000000007"}, {"source_code": "n = gets.to_i\ns = gets.chomp\nm = [s.count(\"A\"), s.count(\"C\"), s.count(\"T\"), s.count(\"G\")]\ncntmax = m.max\ncnt = 0\nm.each{|mm| cnt += 1 if mm == cntmax}\nputs (cnt**n)%(10**9+7)\n"}], "negative_code": [{"source_code": "n=gets.to_i\ns=gets\na=\"ACGT\".chars.map{|c|s.count(c)}.sort-[0]\nb=a.chunk{|i|i}\nc=b.map{|k,v|[k,k*v.size]}\nd=b.map{|k,v|v.size**(k*v.size)}.inject(:*)\np (1..n).inject(:*)/c.map{|_,i|(1..i).inject(:*)}.inject(:*)*d\n"}, {"source_code": "n=gets.to_i\ns=gets\na=\"ACGT\".chars.map{|c|s.count(c)}.sort-[0]\nb=a.chunk{|i|i}\nc=b.map{|k,v|[k,k*v.size]}\nd=b.map{|k,v|v.size**(k*v.size)}.inject(:*)\np (1..n).inject(:*)/c.map{|_,i|(1..i).inject(:*)}.inject(:*)*d%1000000007\n"}], "src_uid": "f35c042f23747988f65c5b5e8d5ddacd"} {"nl": {"description": "You are given two integers $$$A$$$ and $$$B$$$, calculate the number of pairs $$$(a, b)$$$ such that $$$1 \\le a \\le A$$$, $$$1 \\le b \\le B$$$, and the equation $$$a \\cdot b + a + b = conc(a, b)$$$ is true; $$$conc(a, b)$$$ is the concatenation of $$$a$$$ and $$$b$$$ (for example, $$$conc(12, 23) = 1223$$$, $$$conc(100, 11) = 10011$$$). $$$a$$$ and $$$b$$$ should not contain leading zeroes.", "input_spec": "The first line contains $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. Each test case contains two integers $$$A$$$ and $$$B$$$ $$$(1 \\le A, B \\le 10^9)$$$.", "output_spec": "Print one integer — the number of pairs $$$(a, b)$$$ such that $$$1 \\le a \\le A$$$, $$$1 \\le b \\le B$$$, and the equation $$$a \\cdot b + a + b = conc(a, b)$$$ is true.", "sample_inputs": ["3\n\n1 11\n\n4 2\n\n191 31415926"], "sample_outputs": ["1\n0\n1337"], "notes": "NoteThere is only one suitable pair in the first test case: $$$a = 1$$$, $$$b = 9$$$ ($$$1 + 9 + 1 \\cdot 9 = 19$$$)."}, "positive_code": [{"source_code": "T = gets.to_i\n\nT.times do\n a, b = gets.split.map(&:to_i)\n\n cnt = 0\n\n 1.upto(b.to_s.size) do |n|\n s = ('9' * n).to_i\n\n cnt += 1 if s <= b\n end\n\n puts a * cnt\nend\n"}], "negative_code": [], "src_uid": "dc67dd2102c70ea476df642b863ae8d3"} {"nl": {"description": "Asya loves animals very much. Recently, she purchased $$$n$$$ kittens, enumerated them from $$$1$$$ and $$$n$$$ and then put them into the cage. The cage consists of one row of $$$n$$$ cells, enumerated with integers from $$$1$$$ to $$$n$$$ from left to right. Adjacent cells had a partially transparent partition wall between them, hence there were $$$n - 1$$$ partitions originally. Initially, each cell contained exactly one kitten with some number.Observing the kittens, Asya noticed, that they are very friendly and often a pair of kittens in neighboring cells wants to play together. So Asya started to remove partitions between neighboring cells. In particular, on the day $$$i$$$, Asya: Noticed, that the kittens $$$x_i$$$ and $$$y_i$$$, located in neighboring cells want to play together. Removed the partition between these two cells, efficiently creating a single cell, having all kittens from two original cells. Since Asya has never putted partitions back, after $$$n - 1$$$ days the cage contained a single cell, having all kittens.For every day, Asya remembers numbers of kittens $$$x_i$$$ and $$$y_i$$$, who wanted to play together, however she doesn't remember how she placed kittens in the cage in the beginning. Please help her and find any possible initial arrangement of the kittens into $$$n$$$ cells.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$2 \\le n \\le 150\\,000$$$) — the number of kittens. Each of the following $$$n - 1$$$ lines contains integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \\le x_i, y_i \\le n$$$, $$$x_i \\ne y_i$$$) — indices of kittens, which got together due to the border removal on the corresponding day. It's guaranteed, that the kittens $$$x_i$$$ and $$$y_i$$$ were in the different cells before this day.", "output_spec": "For every cell from $$$1$$$ to $$$n$$$ print a single integer — the index of the kitten from $$$1$$$ to $$$n$$$, who was originally in it. All printed integers must be distinct. It's guaranteed, that there is at least one answer possible. In case there are multiple possible answers, print any of them.", "sample_inputs": ["5\n1 4\n2 5\n3 1\n4 5"], "sample_outputs": ["3 1 4 2 5"], "notes": "NoteThe answer for the example contains one of several possible initial arrangements of the kittens.The picture below shows how the cells were united for this initial arrangement. Note, that the kittens who wanted to play together on each day were indeed in adjacent cells. "}, "positive_code": [{"source_code": "\ndef init(n)\n n.times do |i|\n Parent[i] = i\n Next[i] = nil\n Last[i] = i\n Rank[i] = 0\n end\nend\n\ndef find(x)\n if Parent[x] == x\n return x\n else\n return Parent[x] = find(Parent[x])\n end\nend\n\ndef unite(x,y)\n x,y = find(x),find(y)\n return if x==y\n\n x,y = y,x if Rank[x] < Rank[y]\n Rank[x] += 1 if Rank[x] == Rank[y]\n\n Parent[y] = x;\n Next[Last[x]] = y\n Last[x] = Last[y]\nend\n\nn = gets.to_i\nParent,Rank,Next,Last = [],[],[],[]\ninit(n+1)\n\n(n-1).times do\n a,b = gets.split.map(&:to_i)\n unite(a,b)\nend\nt = find(1)\nn.times do |i|\n print \"#{t}\"\n print \" \" if i < n-1\n t = Next[t]\nend\n\n"}, {"source_code": "class Base\n attr_accessor :parent\n def root\n parent ? parent.root : self\n end\nend\n\nclass Integer\n def size\n 1\n end\n\n def parent=(a)\n end\nend\n\nclass Node < Base\n attr_reader :size, :items\n def initialize(items)\n @size = items.map(&:size).reduce(&:+)\n @items = items\n @items.each do |item|\n item.parent = item\n end\n end\n\n def add(item)\n @items << item\n @size += item.size\n item.parent = self\n self\n end\n\n def merge(b)\n if size >= b.size\n add(b)\n else\n b.add(self)\n end\n end\nend\n\nN = STDIN.gets.to_i\nmap = Array.new(N) { nil }\n\n(N-1).times do |i|\n a,b = STDIN.gets.strip.split.map(&:to_i)\n a -= 1\n b -= 1\n map[a] ||= Node.new([a])\n map[b] ||= Node.new([b])\n map[a].root.merge(map[b].root)\nend\n\ndef print_node(node)\n case node\n when Integer\n print(\"#{node+1} \")\n else\n node.items.each do |item|\n print_node(item)\n end\n end\nend\n\nprint_node(map[0].root)\nputs"}, {"source_code": "DBG = !true\n\ndef edge(x)\n return x if $lbl[x] == -1\n y = edge($lbl[x])\n $lbl[x] = y if y != x\n return y\nend\n\ndef setedge(x,lb)\n if $lbl[x] != -1 && $lbl[x] != lb\n setedge($lbl[x], lb)\n end\n $lbl[x] = lb if lb != x\nend\n\ndef con(x,y)\n p = edge(x)\n q = edge(y)\n setedge(x,$top)\n setedge(y,$top)\n $lbl[p] = $top\n $lbl[q] = $top\n $rev1[$top] = p\n $rev2[$top] = q\n $top += 1\n if DBG\n print $a\n puts \" - a\"\n print $rev1\n puts \" - rev1\"\n print $rev2\n puts \" - rev2\"\n print $lbl\n puts \" - lbl\"\n end\nend\n\ndef pr(y)\n stk = [y]\n while !stk.empty?\n x = stk.pop\n if x >= $n\n stk << ($rev2[x])\n stk << ($rev1[x])\n else\n print \"#{x+1} \"\n end\n end\n puts \"\"\nend\n\n#### main\n\nn = gets.to_i\n$n = n\n$top = n\n$a = Array.new(2*n+1, -1)\n$rev1 = Array.new(2*n+1, -1)\n$rev2 = Array.new(2*n+1, -1)\n$lbl = Array.new(2*n+1, -1)\n\nfor i in 1...n\n x,y = gets.split.map{|z| z.to_i}\n puts \"do #{x-1} #{y-1}\" if DBG\n con(x-1, y-1)\nend\npr($top-1)\n"}], "negative_code": [{"source_code": "\ndef init(n)\n n.times do |i|\n Parent[i] = i\n Member[i] =[i]\n Rank[i] = 0\n end\nend\n\ndef find(x)\n if Parent[x] == x\n return x\n else\n return Parent[x] = find(Parent[x])\n end\nend\n\ndef unite(x,y)\n x,y = find(x),find(y)\n return if x==y\n\n if Rank[x] < Rank[y]\n Parent[x] = y\n Member[y] += Member[x]\n else\n Parent[y] = x;\n Member[x] += Member[y]\n Rank[x] += 1 if Rank[x] == Rank[y]\n end\nend\n\nn = gets.to_i\nParent = []*(n+1)\nRank = []*(n+1)\nMember = []*(n+1)\ninit(n+1)\n\n(n-1).times do\n a,b = gets.split.map(&:to_i)\n unite(a,b)\nend\nn.times do |i|\n print \"#{Member[1][i]}\"\n print \" \" if i < n-1\nend\n\n"}], "src_uid": "2c665f95370058bc7fdec405db7d155d"} {"nl": {"description": "The R2 company has n employees working for it. The work involves constant exchange of ideas, sharing the stories of success and upcoming challenging. For that, R2 uses a famous instant messaging program Spyke.R2 has m Spyke chats just to discuss all sorts of issues. In each chat, some group of employees exchanges messages daily. An employee can simultaneously talk in multiple chats. If some employee is in the k-th chat, he can write messages to this chat and receive notifications about messages from this chat. If an employee writes a message in the chat, all other participants of the chat receive a message notification.The R2 company is conducting an audit. Now the specialists study effective communication between the employees. For this purpose, they have a chat log and the description of chat structure. You, as one of audit specialists, are commissioned to write a program that will use this data to determine the total number of message notifications received by each employee.", "input_spec": "The first line contains three space-separated integers n, m and k (2 ≤ n ≤ 2·104; 1 ≤ m ≤ 10; 1 ≤ k ≤ 2·105) — the number of the employees, the number of chats and the number of events in the log, correspondingly. Next n lines contain matrix a of size n × m, consisting of numbers zero and one. The element of this matrix, recorded in the j-th column of the i-th line, (let's denote it as aij) equals 1, if the i-th employee is the participant of the j-th chat, otherwise the element equals 0. Assume that the employees are numbered from 1 to n and the chats are numbered from 1 to m. Next k lines contain the description of the log events. The i-th line contains two space-separated integers xi and yi (1 ≤ xi ≤ n; 1 ≤ yi ≤ m) which mean that the employee number xi sent one message to chat number yi. It is guaranteed that employee number xi is a participant of chat yi. It is guaranteed that each chat contains at least two employees.", "output_spec": "Print in the single line n space-separated integers, where the i-th integer shows the number of message notifications the i-th employee receives.", "sample_inputs": ["3 4 5\n1 1 1 1\n1 0 1 1\n1 1 0 0\n1 1\n3 1\n1 3\n2 4\n3 2", "4 3 4\n0 1 1\n1 0 1\n1 1 1\n0 0 0\n1 2\n2 1\n3 1\n1 3"], "sample_outputs": ["3 3 1", "0 2 3 0"], "notes": null}, "positive_code": [{"source_code": "r=lambda{gets.split.map{|i| i.to_i}}\nn,m,k=r.call\na=[]\ns=[0]*m\nb=[0]*n\nn.times{a<= 2 and $array[-1] == $array[-2]\n $array.pop\n $array.pop\n collapse\n end\nend\ns = gets.chomp\n$array = []\ns.split('').each do |c|\n $array.push c\n collapse\nend\nputs $array*''"}, {"source_code": "s=%w()\ngets.scan(/./).each{|i| s[-1] == i ? s.pop : s< 0\n\t\t\trollcount += 1\n\t\tend\n\t\twk = rollcount * ma[j][2]\n\t\tif sumwk < 0 || sumwk > wk\n\t\t\tsumwk = wk\n\t\tend\n\tend\n\tsum += sumwk\nend\nputs sum\n"}, {"source_code": "n = gets.to_i\na = Array.new(n) do\n l, w, h = gets.split.map(&:to_i)\n [h, 2 * (l + w)]\nend\nm = gets.to_i\nb = Array.new(m) do\n gets.split.map(&:to_i)\nend\n\ns = 0\na.each do |x, y|\n t = 9e9\n b.each do |u, v, w|\n u /= x\n next if u == 0\n v *= u\n t = [t, (y + v - 1) / v * w].min\n end\n s += t\nend\np s\n"}, {"source_code": "n=gets.split[0].to_i\nroom=Array.new()\nn.times{ room.push(gets.split.map(&:to_i)) }\nm=gets.split[0].to_i\npaper=Array.new()\nm.times{ paper.push(gets.split.map(&:to_i)) }\n\nans=0\nroom.each{|a|\n room_ans=[]\n paper.each{|p|\n t=2*(a[0]+a[1])\n next if p[0]= a[2] }\n\tr += s\n}\nputs r\n"}, {"source_code": "a = gets.to_i.times.map{gets.split.map{|i| i.to_i}}\nb = gets.to_i.times.map{gets.split.map{|i| i.to_i}}\nr = 0\na.each{ |a|\n\ts = 1 << 20\n\tp = (a[0] + a[1]) * 2\n\tb.each{ |b|\n\t\tif b[0] >= a[2]\n\t\t\th = b[0] / a[2] * b[1]\n\t\t\tw = (p - 1) / h + 1\n\t\t\ts = [s, w * b[2]].min\n\t\tend\n\t}\n\tr += s\n}\nputs r\n"}, {"source_code": "#f = File.open(\"b.in\", \"r\")\nn = gets.to_i\na = Array.new(n) {Array.new(3)}\nfor i in 0...n\n\ta[i] = gets.split.map{|i| i.to_i}\nend\nm = gets.to_i\nb = Array.new(m) {Array.new(3)}\nr = 0\nfor i in 0...m\n\tb[i] = gets.split.map{|i| i.to_i}\nend\nfor i in 0...n\n\ts = 1 << 20\n\tp = a[i][0] * 2 + a[i][1] * 2\n\tfor j in 0...m\n\t\tif b[j][0] >= a[i][2]\n\t\t\th = b[j][0] / a[i][2] * b[j][1]\n\t\t\tw = (p - 1) / h + 1\n\t\t\ts = [s, w * b[j][2]].min\n\t\tend\n\tend\n\tr += s\nend\nputs r\n"}, {"source_code": "n = gets.to_i\na = n.times.map{gets.split.map{|i| i.to_i}}\nm = gets.to_i\nb = m.times.map{gets.split.map{|i| i.to_i}}\nr = 0\nfor i in 0...n\n\ts = 1 << 20\n\tp = (a[i][0] + a[i][1]) * 2\n\tfor j in 0...m\n\t\tif b[j][0] >= a[i][2]\n\t\t\th = b[j][0] / a[i][2] * b[j][1]\n\t\t\tw = (p - 1) / h + 1\n\t\t\ts = [s, w * b[j][2]].min\n\t\tend\n\tend\n\tr += s\nend\nputs r\n"}, {"source_code": "n = STDIN.readline.to_i\nrooms = n.times.map { STDIN.readline.split.map {|s| s.to_i } }\nm = STDIN.readline.to_i\nwallpapers = m.times.map { STDIN.readline.split.map {|s| s.to_i } }\ntotal = 0\n\nrooms.each do |length, width, height|\n circumference = 2*(length + width)\n best = -1\n wallpapers.each do |length, width, price|\n strips = (circumference + width - 1) / width\n strips_per_roll = length / height\n next if strips_per_roll == 0\n rolls = (strips + strips_per_roll - 1) / strips_per_roll\n cost = rolls * price\n #puts '%d * %d = %d' % [rolls, price, cost]\n if best == -1 or cost < best\n best = cost\n end\n end\n total += best\nend\n\nputs total\n"}, {"source_code": "#!/usr/bin/env ruby\n\n\ndef read_ints\n return gets.split.map(&:to_i)\nend\n\nn = gets.to_i\nrooms = []\nn.times do\n rooms << read_ints\nend\n\nm = gets.to_i\npapers = []\nm.times do\n papers << read_ints\nend\n\nresult = 0\nrooms.each do |rl, rw, rh|\n perimeter = 2 * (rl + rw)\n best = 10**10\n papers.each do |pl, pw, price|\n if pl >= rh\n cost = (perimeter.to_f / ((pl / rh) * pw)).ceil * price\n best = [best, cost].min\n end\n end\n result += best\nend\nputs result\n"}, {"source_code": "#!/usr/bin/ruby\nn = gets.chomp.to_i\nmvys = Array.new(n)\nmsir = Array.new(n)\nmhl = Array.new(n)\ncenacelkem = 0\nfor i in 0..n-1\n\tk = gets.chomp.split(' ')\n\tmsir[i] = k[0].to_i\n\tmhl[i] = k[1].to_i\n\tmvys[i] = k[2].to_i\n\t#puts(k)\nend\nm = gets.chomp.to_i\ntapvys = Array.new(m)\ntapsir = Array.new(m)\ntapcena = Array.new(m)\nfor i in 0..m-1\n\tk = gets.chomp.split(' ')\n\ttapvys[i] = k[0].to_i\n\t#puts tapvys[i]\n\ttapsir[i] = k[1].to_i\n\ttapcena[i] = k[2].to_i\nend\nfor i in 0..n-1\n\tobvod = msir[i]+msir[i]+mhl[i]+mhl[i]\n\tvys = mvys[i]\n\t#puts obvod\n\tcenamin = 2147486646\n\tfor j in 0..m-1\n\t\tvtap = tapvys[j]\n\t\tstap = tapsir[j]\n\t\tctap = tapcena[j]\n\t\t#puts vtap\n\t\tif vtap >= vys\n\t\t\t#puts((vtap/vys).to_s)\n\t\t\tprouzku = vtap/vys\n\t\t\trolem = prouzku*stap\n\t\t\t#puts rolem\n\t\t\troli = (obvod/rolem.to_f).ceil\n\t\t\tcena = roli*ctap;\n\t\t\tif cenamin > cena\n\t\t\t\tcenamin = cena\n\t\t\tend\n\t\tend\n\tend\n\tcenacelkem = cenacelkem + cenamin\nend\nputs cenacelkem\n"}, {"source_code": "n = gets.chomp.to_i\nrooms = Array.new(n) { gets.split.collect{|x| x.to_i} }\n\nm = gets.chomp.to_i\ntypes = Array.new(m) { gets.split.collect{|x| x.to_i} }\n \nsum_cost = 0\nrooms.each{|room|\n l,w,h = room\n min_cost = nil\n \n types.each{|type|\n lt,wt,c = type\n cols = lt / h\n next if cols == 0\n \n cols_req = ((2 * l + 2 * w) / wt.to_f).ceil\n cost = c * (cols_req / cols.to_f).ceil\n \n min_cost = cost if min_cost.nil? or cost < min_cost\n }\n sum_cost += min_cost\n}\n\nputs sum_cost"}, {"source_code": "n=gets.to_i\nrooms=n.times.map{gets.split.map(&:to_i)}\nm=gets.to_i\npapers=m.times.map{gets.split.map(&:to_i)}\n\ns=0\nrooms.each{|l,w,h|\n\tperimeter=(l+w)*2\n\ts+=papers.map{|p_l,p_w,p_p|\n\t\tif p_l a[i][2]\n\t\t\th = b[j][0] / a[i][2] * b[j][1]\n\t\t\tw = (p - 1) / h + 1\n\t\t\ts = [s, w * b[j][2]].min\n\t\telse\n\t\t\th = (a[i][2] - 1) / b[j][0] + 1\n\t\t\tw = (p - 1) / b[j][1] + 1\n\t\t\ts = [s, w * b[j][2] * h].min\n\t\tend\n\tend\n\tr += s\nend\nputs r\n"}, {"source_code": "#!/usr/bin/env ruby\n\n\ndef read_ints\n return gets.split.map(&:to_i)\nend\n\nn = gets.to_i\nrooms = []\nn.times do\n rooms << read_ints\nend\n\nm = gets.to_i\npapers = []\nm.times do\n papers << read_ints\nend\n\nrooms.each do |rl, rw, rh|\n perimeter = 2 * (rl + rw)\n best = 10**10\n papers.each do |pl, pw, price|\n if pl >= rh\n cost = (perimeter.to_f / ((pl / rh) * pw)).ceil * price\n best = [best, cost].min\n end\n end\n puts best\nend\n\n"}, {"source_code": "#!/usr/bin/env ruby\n\ndef read_ints\n return gets.split.map(&:to_i)\nend\n\nn = gets.to_i\nrooms = []\nn.times do\n rooms << read_ints\nend\n\nm = gets.to_i\npapers = []\nm.times do\n papers << read_ints\nend\n\nrooms.each do |rl, rw, rh|\n perimeter = 2 * (rl + rw)\n best = 10**10\n papers.each do |pl, pw, price|\n div = pl < rh ? 1 : pl / rh\n mul = pl < rh ? rh / pl : 1\n cost = (perimeter.to_f / (div * pw)).ceil * mul * price\n best = [best, cost].min\n end\n puts best\nend\n\n"}], "src_uid": "29639971c98dd98f0292d994d4433e3a"} {"nl": {"description": "Professor GukiZ likes programming contests. He especially likes to rate his students on the contests he prepares. Now, he has decided to prepare a new contest. In total, n students will attend, and before the start, every one of them has some positive integer rating. Students are indexed from 1 to n. Let's denote the rating of i-th student as ai. After the contest ends, every student will end up with some positive integer position. GukiZ expects that his students will take places according to their ratings. He thinks that each student will take place equal to . In particular, if student A has rating strictly lower then student B, A will get the strictly better position than B, and if two students have equal ratings, they will share the same position. GukiZ would like you to reconstruct the results by following his expectations. Help him and determine the position after the end of the contest for each of his students if everything goes as expected.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 2000), number of GukiZ's students. The second line contains n numbers a1, a2, ... an (1 ≤ ai ≤ 2000) where ai is the rating of i-th student (1 ≤ i ≤ n).", "output_spec": "In a single line, print the position after the end of the contest for each of n students in the same order as they appear in the input.", "sample_inputs": ["3\n1 3 3", "1\n1", "5\n3 5 3 4 5"], "sample_outputs": ["3 1 1", "1", "4 1 4 3 1"], "notes": "NoteIn the first sample, students 2 and 3 are positioned first (there is no other student with higher rating), and student 1 is positioned third since there are two students with higher rating.In the second sample, first student is the only one on the contest.In the third sample, students 2 and 5 share the first position with highest rating, student 4 is next with third position, and students 1 and 3 are the last sharing fourth position."}, "positive_code": [{"source_code": "gets\nx=gets.split.map{|e| e.to_i}\nc=(0..2000).map{0}\nx.each{|e|\n\tc[e]+=1\n}\nans=1\n2000.downto(1){|i|\n\tif c[i]>0 then\n\t\tt=c[i]\n\t\tc[i]=ans\n\t\tans+=t\n\tend\n}\n\nputs x.map{|e|\n\tc[e]\n}*\" \"\n\n"}, {"source_code": "n = gets.to_i\na = gets.chomp.split(\"\\s\").map.with_index { |e, i| [i, e.to_i] }.sort { |a, b| b[1] <=> a[1] }\n\nbefore = -Float::INFINITY\nposition = 0\nres = []\n\nn.times do |i|\n position = i + 1 unless before == a[i][1]\n res << [a[i][0], position]\n before = a[i][1]\nend\n\nputs res.sort { |a, b| a[0] <=> b[0] }.map { |e| e[1] }.join(\"\\s\")\n"}, {"source_code": "a=Hash[(1..gets.to_i).zip(gets.split.map{|t|t.to_i})]\nputs a.map{|i,ai|1+a.select{|j,aj|(j!=i)&&(aj>ai)}.size}.join(' ')\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\ns = a.each_with_index.to_a.sort { |x,y| y <=> x }\nanswer = [0] * n\nt = nil\nlast = nil\ns.each_with_index do |tuple, index|\n if last != tuple[0]\n last = tuple[0]\n t = index + 1\n end\n answer[tuple[1]] = t\n\nend\nputs answer.join ' '"}, {"source_code": "# coding: utf-8\n\nn = gets.to_i\nar = gets.split.map(&:to_i)\nac = Array.new(2000 + 1) { 0 }\nfor i in ar\n ac[i] += 1\nend\narc = [0]\nacc = 0\nfor i in 1..2000\n acc += ac[i]\n arc << acc\nend\nresults = []\nfor i in ar\n results << 1 + (arc[2000] - arc[i])\nend\nputs results.join(' ')"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nb = a.map{|x| a.count{|y| y > x} + 1}\nputs b.join(' ')\n"}, {"source_code": "def solution(n, arr)\n arr2 = arr.sort\n h = {}\n (0...n).each do |i|\n j = i + 1\n while(arr2[j] == arr2[i]) do\n j += 1\n end\n h[arr2[i]] = n - j + 1\n end\n\n puts arr.map{|a| a = h[a]}.join(' ')\nend\n\nsolution(gets.chomp.to_i, gets.split.map(&:to_i))"}, {"source_code": "# http://codeforces.com/problemset/problem/551/A\n\nn = gets.chomp.to_i\na = gets.chomp.split.map(&:to_i)\n\nsorted_a = a.sort\nresult = {}\n\nfor i in 0..(n - 1)\n if sorted_a[i + 1] != sorted_a[i]\n result[sorted_a[i]] = n - i\n end\nend\n\nfor i in 0..(n - 1)\n print result[a[i]], \" \"\nend\n"}, {"source_code": "n = gets().chomp().to_i\nstudent = []\nstudent = gets().split(' ').map {|p| p.to_i}\n\ndef find_max(student)\n n= student.size()\nmaximum =0\nindex =0\nfor i in (0..n-1)\n if student[i]>maximum\n maximum = student[i]\n index = i\n end\nend\n\nreturn maximum,index\nend\n\n\nposition = Array.new(n,0)\n\n\nfound =false\nrating = 1\n\nlast,index = find_max(student)\n\nfor j in (1..n)\n maximum,index = find_max(student)\n\n student[index]=0\n if last > maximum\n position[index]=j\n rating = j\n else\n position[index]=rating\n end\n last = maximum\n=begin\n for i in (0..n-1)\n if student[i]==maximum\n student[i]=0\n position[i]=rating\n found =true\n break\n end\n end\n\n\n if found\n found = false\n else\n puts \"hello\"\n rating = j+1\n end\n=end\nend\n\nposition.each { |p| print \"#{p} \"}\nputs\n"}, {"source_code": "#!/usr/bin/ruby\n#coding:utf-8\n#derived from 3年C組の成績表\ngets\nm=gets.split.map(&:to_i).each_with_index.to_a\n1.times{|i| # 各列を順位に変換\n\tm.sort_by!{|e|-e[i]}\n\ta=[] # 順位\n\trank=1\n\tadd=0\n\ta<m[j][i] # 点数が異なっていたら、同じだった人数を順位に加える\n\t\t\trank+=add\n\t\t\tadd=0\n\t\tend\n\t\ta< a}\n\nnewScore.each_with_index do |value, index|\n\tif !hash.has_key?(value)\n\t\thash[value] = index + 1\n\tend\nend\n\nscore.each do |value|\n\tprint \"#{hash[value]} \"\nend\n\nputs ''"}, {"source_code": "n = gets.to_i\na = gets.split.map{|r| r.to_i}\nf = Array.new(n)\n\nfor i in 0..n-1\n f[i] = 1\n max = a[i]\n a.each do |x|\n if x>max\n f[i]+=1\n end\n end\nend\nputs f"}, {"source_code": "gets\narr = gets.split.map { |x| x.to_i }\nhashed = Hash.new(0)\narr.uniq.each { |x| hashed[x] = arr.sort.count { |y| y > x } + 1 }\narr.each do |x|\n print hashed[x], \" \" \nend\n"}, {"source_code": "n = gets.chomp.to_i\narr = gets.split.map(&:to_i)\narr2 = arr.sort\nnumber = Hash.new\nn.times do |i|\n number[arr2[i]] = i\nend\nn.times do |i|\n print n-number[arr[i]] ; print \" \"\nend"}, {"source_code": "n = gets.split(' ').map(&:to_i)\na = gets.split(' ').map(&:to_i)\nb = a.clone()\nb = b.sort!.reverse\nfor x in a\n b.length.times{ |i|\n if (b[i]==x)\n print i+1\n print ' '\n break\n end\n }\nend"}, {"source_code": "gets\na=1\nputs gets.split.map(&:to_i).each_with_index.sort_by{|r|-r[0]}.chunk{|r|r[0]}.reduce([]){|z,r|z+=r[1].map{|s|[s[1],a]};a+=r[1].size;z}.sort.map{|r|r[1]}*?\\s\n"}], "negative_code": [{"source_code": "def solution(n, arr)\n arr.sort!\n (0...n).each do |index|\n arr[index] = arr[n-1] - arr[index] + 1\n end\n print arr\nend\n\nsolution(gets.chomp.to_i, gets.split.map(&:to_i))"}, {"source_code": "def solution(n, arr)\n arr.sort!\n (0...n).each do |i|\n j = i + 1\n while(arr[j] == arr[i]) do\n j += 1\n end\n arr[i] = n - j + 1\n end\n puts arr.join(' ')\nend\n\nsolution(gets.chomp.to_i, gets.split.map(&:to_i))"}, {"source_code": "gets\narr = gets.chomp.split.join(\"\")\nmax = 0\narr.scan(/0+/) { |x| max = x.length if x.length > max }\nputs arr.split(\"\").count { |x| x == \"1\" } + max\n"}, {"source_code": "n = gets.chomp.to_i\narr = gets.split.map(&:to_i)\nnumber = Hash.new\nn.times do |i|\n number[arr[i]] = i\nend\nn.times do |i|\n print n-number[arr[i]] ; print \" \"\nend"}], "src_uid": "a5edbf422616cdac35e95a4f07efc7fd"} {"nl": {"description": "Petya loves computer games. Finally a game that he's been waiting for so long came out!The main character of this game has n different skills, each of which is characterized by an integer ai from 0 to 100. The higher the number ai is, the higher is the i-th skill of the character. The total rating of the character is calculated as the sum of the values ​​of for all i from 1 to n. The expression ⌊ x⌋ denotes the result of rounding the number x down to the nearest integer.At the beginning of the game Petya got k improvement units as a bonus that he can use to increase the skills of his character and his total rating. One improvement unit can increase any skill of Petya's character by exactly one. For example, if a4 = 46, after using one imporvement unit to this skill, it becomes equal to 47. A hero's skill cannot rise higher more than 100. Thus, it is permissible that some of the units will remain unused.Your task is to determine the optimal way of using the improvement units so as to maximize the overall rating of the character. It is not necessary to use all the improvement units.", "input_spec": "The first line of the input contains two positive integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 107) — the number of skills of the character and the number of units of improvements at Petya's disposal. The second line of the input contains a sequence of n integers ai (0 ≤ ai ≤ 100), where ai characterizes the level of the i-th skill of the character.", "output_spec": "The first line of the output should contain a single non-negative integer — the maximum total rating of the character that Petya can get using k or less improvement units.", "sample_inputs": ["2 4\n7 9", "3 8\n17 15 19", "2 2\n99 100"], "sample_outputs": ["2", "5", "20"], "notes": "NoteIn the first test case the optimal strategy is as follows. Petya has to improve the first skill to 10 by spending 3 improvement units, and the second skill to 10, by spending one improvement unit. Thus, Petya spends all his improvement units and the total rating of the character becomes equal to lfloor frac{100}{10} rfloor +  lfloor frac{100}{10} rfloor = 10 + 10 =  20.In the second test the optimal strategy for Petya is to improve the first skill to 20 (by spending 3 improvement units) and to improve the third skill to 20 (in this case by spending 1 improvement units). Thus, Petya is left with 4 improvement units and he will be able to increase the second skill to 19 (which does not change the overall rating, so Petya does not necessarily have to do it). Therefore, the highest possible total rating in this example is .In the third test case the optimal strategy for Petya is to increase the first skill to 100 by spending 1 improvement unit. Thereafter, both skills of the character will be equal to 100, so Petya will not be able to spend the remaining improvement unit. So the answer is equal to . "}, "positive_code": [{"source_code": "#!/usr/bin/ruby\nn,k=gets.split.map(&:to_i)\na=gets.split.map(&:to_i).sort_by{|e|0-e%10}\na.size.times{|i|\n\ty=10-a[i]%10\n\tnext if y==10\n\tx=[y,k].min\n\ta[i]+=x\n\tk-=x\n}\na.size.times{|i|\n\ty=100-a[i]\n\tnext if y==0\n\tx=[y,k].min\n\ta[i]+=x\n\tk-=x\n}\np a.map{|e|e/10}.reduce(:+)"}, {"source_code": "# Codeforces Round\n\n=begin\n# A\na, b = gets.split.map(&:to_i)\nc, d = [a, b].min, [a, b].max\nputs \"#{[a, b].min} #{(a - b).abs / 2}\"\n\n# B\nn = gets.to_i\nh = gets.split.map(&:to_i)\nm, a = [0] * (n - 1) + [h[n - 1]], [0] * n\n(n - 2).downto(0) do |i|\n if h[i] <= m[i + 1]\n m[i] = m[i + 1]\n a[i] = m[i] - h[i] + 1\n else\n m[i] = h[i]\n end\nend\nputs a.join(' ')\n=end\n\n# C\nn, k = gets.split.map(&:to_i)\ncmp = ->(x, y) { (y % 10) <=> (x % 10) }\na = gets.split.map(&:to_i).sort(&cmp).map do |x|\n if x % 10 == 0 || k == 0\n x\n else\n d = [10 - x % 10, k].min\n k -= d\n x += d\n end\nend\nans = 0\na.each do |y|\n d = [100 - y, k].min\n k -= d\n ans += (y + d) / 10\nend\nputs ans\n"}, {"source_code": "input = STDIN.gets\narr = input.split.map(&:to_i)\n# всего навыков\nn = arr[0]\n# очки улучшений\nk = arr[1]\n# очков на одну способность, если поровну\n# constant = (k / n).floor\n# получаем все очки\ninput = STDIN.gets\nabilities = input.split.map(&:to_i)\n# создаем словарь\nindexes = Hash.new\n\ni = 0\n# n шагов\nabilities.each do |item|\n # заполняем словарь значениями\n indexes[i] = 10 - item % 10\n i += 1\nend\n# создаем ещё один словарь из первого\n# отсортированный по значениям\n# ? шагов - не знаю как сортировка устроена\nsort = Hash[indexes.sort_by{|k, v| v}]\n\n# добавляем очки там, где их нужно меньше всего,\n# чтобы \"добить\" до величины, кратной 10-ти\n# ещё n шагов\nsort.each do |key, value|\n if k - value >= 0\n abilities[key] += value\n k = k - value\n else\n break\n end\nend\n\n# если ещё остались очки - разбрасываем их по всем, добивая до 100 сколько\n# сможем\nif k > 0\n abilities.map! { |element|\n if k >= 100 - element\n k = k - (100 - element)\n 100\n else\n prev_k = k\n k = 0\n element + prev_k\n end\n }\nend\n\nrating = 0\n# финальный проход\n# n шагов\nabilities.each do |item|\n if item > 100\n item = 100\n end\n rating += (item * 0.1).floor\nend\n\n# Итого примерно\n# 3n + ? шагов\n# ? - сортировка\nputs \"#{rating}\"\n"}, {"source_code": "input = gets.chomp.split\nn = Integer(input[0])\nk = Integer(input[1])\ninput = gets.chomp.split\nskills = []\nfor i in 0...n\n skills[i] = Integer(input[i])\nend\n\nsum = 0\nsequence = []\nskill = []\nfor i in 0...n\n if skills[i] == 100\n sum += 10\n else\n skill.push(skills[i])\n end\nend\nfor i in 0...skill.length\n sequence[i] = []\n sequence[i][0] = skill[i] / 10\n sequence[i][1] = skill[i] % 10\nend\nsequence = sequence.sort{|x, y| y[1] <=> x[1]}\nfor i in 0...sequence.length\n if 10 - sequence[i][1] < k\n k -= 10 - sequence[i][1]\n sequence[i][1] = 10\n else\n sequence[i][1] += k\n k = 0\n break\n end\nend\nnumbers = []\nfor i in 0...sequence.length\n numbers[i] = 10 * sequence[i][0] + sequence[i][1]\nend\nif k >= 10\n for i in 0...numbers.length\n if k >= 100 - numbers[i]\n k -= 100 - numbers[i]\n numbers[i] = 100\n else\n numbers[i] += k\n k = 0\n break\n end\n end\nend\n\nfor i in 0...numbers.length\n sum += (numbers[i].to_f / 10).floor\nend\nputs sum\n"}, {"source_code": "firstInput = gets.chomp.split(' ').map {|item| item.to_i}\nskillNum, itemNum = firstInput[0], firstInput[1]\nans = 0\nskillLevel = gets.chomp.split(' ').map {|item| item.to_i}\nskillLevel.sort! {|a, b| b % 10 <=> a % 10}\n\n10.times do |a|\nindex = 0\nuntil itemNum == 0 || index == skillNum\n\tif skillLevel[index] == 100\n\t\tindex += 1\n\t\tnext\n\tend\n\n\trest = 10 - (skillLevel[index] % 10)\n\titemNum -= rest\n\tbreak if itemNum < 0\n\tskillLevel[index] += rest\n\tindex += 1\nend\nend\n\nskillLevel.each do |level|\n\tans += (level / 10)\nend\n\nputs ans"}], "negative_code": [{"source_code": "input = STDIN.gets\narr = input.split.map(&:to_i)\n# всего навыков\nn = arr[0]\n# очки улучшений\nk = arr[1]\n# очков на одну способность, если поровну\n# constant = (k / n).floor\n# получаем все очки\ninput = STDIN.gets\nabilities = input.split.map(&:to_i)\n# создаем словарь\nindexes = Hash.new\n\ni = 0\n# n шагов\nabilities.each do |item|\n # заполняем словарь значениями\n indexes[i] = 10 - item % 10\n i += 1\nend\n# создаем ещё один словарь из первого\n# отсортированный по значениям\n# ? шагов - не знаю как сортировка устроена\nsort = Hash[indexes.sort_by{|k, v| v}]\n\n# добавляем очки там, где их нужно меньше всего,\n# чтобы \"добить\" до величины, кратной 10-ти\n# ещё n шагов\nsort.each do |key, value|\n if k - value >= 0\n abilities[key] += value\n k = k - value\n else\n break\n end\nend\n\n# если ещё остались очки - разбрасываем их по всем, добивая до 100 сколько\n# сможем\nif k > 0\n abilities.map! { |element|\n if k >= 100 - element\n k = k - (100 - element)\n element = 100\n else\n element += k\n k = 0\n end\n }\nend\n\nrating = 0\n# финальный проход\n# n шагов\nabilities.each do |item|\n if item > 100\n item = 100\n end\n rating += (item * 0.1).floor\nend\n\n# Итого примерно\n# 3n + ? шагов\n# ? - сортировка\nputs \"#{rating}\"\n"}, {"source_code": "input = STDIN.gets\narr = input.split.map(&:to_i)\n# всего навыков\nn = arr[0]\n# очки улучшений\nk = arr[1]\n# очков на одну способность, если поровну\n# constant = (k / n).floor\n# получаем все очки\ninput = STDIN.gets\nabilities = input.split.map(&:to_i)\n# создаем словарь\nindexes = Hash.new\n\ni = 0\n# n шагов\nabilities.each do |item|\n # заполняем словарь значениями\n indexes[i] = 10 - item % 10\n i += 1\nend\n# создаем ещё один словарь из первого\n# отсортированный по значениям\n# ? шагов - не знаю как сортировка устроена\nsort = Hash[indexes.sort_by{|k, v| v}]\n\n# добавляем очки там, где их нужно меньше всего,\n# чтобы \"добить\" до величины, кратной 10-ти\n# ещё n шагов\nsort.each do |key, value|\n if k - value >= 0\n abilities[key] += value\n k = k - value\n else\n break\n end\nend\n\n# если ещё остались очки - разбрасываем их по всем, добивая до 100 сколько\n# сможем\nif k > 0\n abilities.map! { |element|\n if k >= 100 - element\n k = k - (100 - element)\n element = 100\n else\n element += k\n k = 0\n end\n }\nend\n\nputs \"#{abilities}\"\nrating = 0\n# финальный проход\n# n шагов\nabilities.each do |item|\n if item > 100\n item = 100\n end\n rating += (item * 0.1).floor\nend\n\n# Итого примерно\n# 3n + ? шагов\n# ? - сортировка\nputs \"#{rating}\"\n"}, {"source_code": "input = STDIN.gets\narr = input.split.map(&:to_i)\n# всего навыков\nn = arr[0]\n# очки улучшений\nk = arr[1]\n# получаем все очки\ninput = STDIN.gets\nabilities = input.split.map(&:to_i)\n# создаем словарь\nindexes = Hash.new\n\ni = 0\n# n шагов\nabilities.each do |item|\n # заполняем словарь значениями\n indexes[i] = 10 - item % 10\n i += 1\nend\n# создаем ещё один словарь из первого\n# отсортированный по значениям\n# ? шагов - не знаю как сортировка устроена\nsort = Hash[indexes.sort_by{|k, v| v}]\n\n# добавляем очки там, где их нужно меньше всего,\n# чтобы \"добить\" до величины, кратной 10-ти\n# ещё n шагов в пределе, хотя может и раньше закончиться\nsort.each do |key, value|\n if k - value >= 0\n abilities[key] += value\n k = k - value\n else\n break\n end\nend\n\nrating = 0\n# финальный проход\n# n шагов\nabilities.each do |item|\n rating += (item * 0.1).floor\nend\n\n# Итого примерно\n# 3n + ? шагов\n# ? - сортировка\nputs \"#{rating}\"\n"}, {"source_code": "input = STDIN.gets\narr = input.split.map(&:to_i)\n# всего навыков\nn = arr[0]\n# очки улучшений\nk = arr[1]\n# получаем все очки\ninput = STDIN.gets\nabilities = input.split.map(&:to_i)\n# создаем словарь\nindexes = Hash.new\n\nrating = 0\ni = 0\n# n шагов\nabilities.each do |item|\n # заполняем словарь значениями\n indexes[i] = 10 - item % 10\n i += 1\nend\n# создаем ещё один словарь из первого\n# отсортированный по значениям\n# ? шагов - не знаю как сортировка устроена\nsort = Hash[indexes.sort_by{|k, v| v}]\n\n# добавляем очки там, где их нужно меньше всего,\n# чтобы \"добить\" до величины, кратной 10-ти\n# ещё n шагов в пределе, хотя может и раньше закончиться\nsort.each do |key, value|\n if k - value > 0\n abilities[key] += value\n k = k - value\n else\n break\n end\nend\n\n# финальный проход\n# n шагов\nabilities.each do |item|\n rating += (item * 0.1).floor\nend\n\n# Итого примерно\n# 3n + ? шагов\n# ? - сортировка\nputs \"#{rating}\"\n"}, {"source_code": "input = gets.chomp.split\nn = Integer(input[0])\nk = Integer(input[1])\ninput = gets.chomp.split\nskills = []\nfor i in 0...n\n skills[i] = Integer(input[i])\nend\n\nskills.sort!{|x, y| y <=> x}\nfor i in 0...n\n if skills[i] % 10 != 0\n unit_needed = 10 - skills[i] % 10\n else\n unit_needed = 0\n end\n if unit_needed > k\n break\n else\n skills[i] += unit_needed\n k -= unit_needed\n end\nend\nif k >= 10 \n for i in 0...n\n x = k / 10\n if skills[i] < 100\n y = (100 - skills[i]) / 10\n if x <= y\n skills[i] += 10 * x\n break\n else\n skills[i] = 100\n k -= 10 * y\n end\n end\n end\nend\nsum = 0\nif skills[0] == skills[n-1] && skills[0] == 0\n sum = (k.to_f / 10).floor\nelse\n for i in 0...n\n sum += (skills[i].to_f / 10).floor\n end\nend\nputs sum"}, {"source_code": "input = gets.chomp.split\nn = Integer(input[0])\nk = Integer(input[1])\ninput = gets.chomp.split\nskills = []\nfor i in 0...n\n skills[i] = Integer(input[i])\nend\n\nsum = 0\nfor i in 0...skills.length\n if skills[i] == 100\n sum += 10\n skills.delete_at(i)\n end\nend\nsequence = []\nfor i in 0...skills.length\n sequence[i] = []\n sequence[i][0] = skills[i] / 10\n sequence[i][1] = skills[i] % 10\nend\nsequence = sequence.sort{|x, y| y[1] <=> x[1]}\nfor i in 0...sequence.length\n if 10 - sequence[i][1] < k\n k -= 10 - sequence[i][1]\n sequence[i][1] = 10\n else\n sequence[i][1] += k\n k = 0\n break\n end\nend\nnumbers = []\nfor i in 0...sequence.length\n numbers[i] = 10 * sequence[i][0] + sequence[i][1]\nend\nif k >= 10\n for i in 0...numbers.length\n if k >= 100 - numbers[i]\n numbers[i] = 100\n k -= 100 - numbers[i]\n else\n numbers[i] += k\n k = 0\n break\n end\n end\nend\nfor i in 0...numbers.length\n sum += (numbers[i].to_f / 10).floor\nend\nputs sum\n"}, {"source_code": "input = gets.chomp.split\nn = Integer(input[0])\nk = Integer(input[1])\ninput = gets.chomp.split\nskills = []\nfor i in 0...n\n skills[i] = Integer(input[i])\nend\n\nskills.sort!{|x, y| y <=> x}\nfor i in 0...n\n if skills[i] % 10 != 0\n unit_needed = 10 - skills[i] % 10\n else\n unit_needed = 0\n end\n if unit_needed > k\n break\n else\n skills[i] += unit_needed\n k -= unit_needed\n end\nend\nsum = 0\nif skills[0] == skills[n-1] && skills[0] == 0\n sum = (k.to_f / 10).floor\nelse\n for i in 0...n\n sum += (skills[i].to_f / 10).floor\n end\nend\nputs sum"}, {"source_code": "input = gets.chomp.split\nn = Integer(input[0])\nk = Integer(input[1])\ninput = gets.chomp.split\nskills = []\nfor i in 0...n\n skills[i] = Integer(input[i])\nend\n\nskills.sort!{|x, y| y <=> x}\nfor i in 0...n\n if skills[i] % 10 != 0\n unit_needed = 10 - skills[i] % 10\n else\n unit_needed = 0\n end\n if unit_needed > k\n break\n else\n skills[i] += unit_needed\n k -= unit_needed\n end\nend\nif k >= 10\n skills[0] += 10 * (k / 10)\nend\nsum = 0\nif skills[0] == skills[n-1] && skills[0] == 0\n sum = (k.to_f / 10).floor\nelse\n for i in 0...n\n sum += (skills[i].to_f / 10).floor\n end\nend\nputs sum"}, {"source_code": "input = gets.chomp.split\nn = Integer(input[0])\nk = Integer(input[1])\ninput = gets.chomp.split\nskills = []\nfor i in 0...n\n skills[i] = Integer(input[i])\nend\n\nskills.sort!{|x, y| y <=> x}\nfor i in 0...n\n if skills[i] % 10 != 0\n unit_needed = 10 - skills[i] % 10\n else\n unit_needed = 0\n end\n if unit_needed > k\n break\n else\n skills[i] += unit_needed\n k -= unit_needed\n end\nend\nsum = 0\nfor i in 0...n\n sum += (skills[i].to_f / 10).floor\nend\nputs sum"}, {"source_code": "firstInput = gets.chomp.split(' ').map {|item| item.to_i}\nskillNum, itemNum = firstInput[0], firstInput[1]\nskillHash = {}\nans = 0\nskillLevel = gets.chomp.split(' ').map {|item| item.to_i}\nskillLevel.each_with_index do |value, index|\n\tskillHash[index] = value % 10\nend\n\nskillLevel.sort! {|a, b| b % 10 <=> a % 10}\n\nindex = 0\nuntil itemNum == 0 || index == skillNum\n\trest = 10 - (skillLevel[index] % 10)\n\titemNum -= rest\n\tbreak if itemNum < 0\n\tskillLevel[index] += rest\n\tindex += 1\nend\n\nskillLevel.each do |level|\n\tans += (level / 10)\nend\n\nputs ans\n\n"}, {"source_code": "firstInput = gets.chomp.split(' ').map {|item| item.to_i}\nskillNum, itemNum = firstInput[0], firstInput[1]\nans = 0\nskillLevel = gets.chomp.split(' ').map {|item| item.to_i}\nskillLevel.sort! {|a, b| b % 10 <=> a % 10}\n\nindex = 0\nuntil itemNum == 0 || index == skillNum\n\tif skillLevel[index] == 100\n\t\tindex += 1\n\t\tnext\n\tend\n\n\trest = 10 - (skillLevel[index] % 10)\n\titemNum -= rest\n\tbreak if itemNum < 0\n\tskillLevel[index] += rest\n\tindex += 1\nend\n\nskillLevel.sort! {|a, b| b % 100 <=> a % 100}\n\nindex = 0\nuntil itemNum == 0 || index == skillNum\n\tif skillLevel[index] == 100\n\t\tindex += 1\n\t\tnext\n\tend\n\n\trest = 100 - (skillLevel[index] % 100)\n\titemNum -= rest\n\tbreak if itemNum < 0\n\tskillLevel[index] += rest\n\tindex += 1\nend\n\nskillLevel.each do |level|\n\tans += (level / 10)\nend\n\nputs ans"}], "src_uid": "b4341e1b0ec0b7341fdbe6edfe81a0d4"} {"nl": {"description": "There are n cities in the country where the Old Peykan lives. These cities are located on a straight line, we'll denote them from left to right as c1, c2, ..., cn. The Old Peykan wants to travel from city c1 to cn using roads. There are (n - 1) one way roads, the i-th road goes from city ci to city ci + 1 and is di kilometers long.The Old Peykan travels 1 kilometer in 1 hour and consumes 1 liter of fuel during this time.Each city ci (except for the last city cn) has a supply of si liters of fuel which immediately transfers to the Old Peykan if it passes the city or stays in it. This supply refreshes instantly k hours after it transfers. The Old Peykan can stay in a city for a while and fill its fuel tank many times. Initially (at time zero) the Old Peykan is at city c1 and s1 liters of fuel is transferred to it's empty tank from c1's supply. The Old Peykan's fuel tank capacity is unlimited. Old Peykan can not continue its travel if its tank is emptied strictly between two cities.Find the minimum time the Old Peykan needs to reach city cn.", "input_spec": "The first line of the input contains two space-separated integers m and k (1 ≤ m, k ≤ 1000). The value m specifies the number of roads between cities which is equal to n - 1. The next line contains m space-separated integers d1, d2, ..., dm (1 ≤ di ≤ 1000) and the following line contains m space-separated integers s1, s2, ..., sm (1 ≤ si ≤ 1000).", "output_spec": "In the only line of the output print a single integer — the minimum time required for The Old Peykan to reach city cn from city c1.", "sample_inputs": ["4 6\n1 2 5 2\n2 3 3 4", "2 3\n5 6\n5 5"], "sample_outputs": ["10", "14"], "notes": "NoteIn the second sample above, the Old Peykan stays in c1 for 3 hours."}, "positive_code": [{"source_code": "m,k=gets.split.map(&:to_i)\nd=gets.split.map(&:to_i)\ns=gets.split.map(&:to_i)\nf=0\nmaxi=0\nres=0\nfor i in 0..m-1 do\n maxi=i if s[maxi]=d[i]\n\t\t\tpet-=d[i]\n\t\t\tmint+=d[i]\n\t\t\tif i < m-1\n\t\t\t\tpet+=s[i+1]\n\t\t\tend\n\t\telse\n\t\t\tmaxp=s[0..i].max\n\t\t\twhile(pet=d[i]\n pet-=d[i]\n tiws+=d[i]\n if i < m-1\n pet+=s[i+1]\n end\n else\n stop=true;\n break;\n end\n end\n if stop\n pet=s[0]\n for i in 0...m\n if pet>=d[i]\n pet-=d[i]\n mint+=d[i]\n if i < m-1\n pet+=s[i+1]\n end\n else\n maxp=s[0..i].max\n while(pet=d[i]\n\t\t\tpet-=d[i]\n\t\t\ttiws+=d[i]\n\t\t\tif i < m-1\n\t\t\t\tpet+=s[i+1]\n\t\t\tend\n\t\telse\n\t\t\tstop=true;\n\t\t\tbreak;\n\t\tend\n\tend\n\tif stop\n\t\tpet=s[0]\n\t\tfor i in 0...m\n\t\t\tif pet>=d[i]\n\t\t\t\tpet-=d[i]\n\t\t\t\tmint+=d[i]\n\t\t\t\tif i < m\n\t\t\t\t\tpet+=s[i+1]\n\t\t\t\tend\n\t\t\telse\n\t\t\t\tmaxp=s[0..i].max\n\t\t\t\twhile(pet2\n end\n if h.size==1\n if h[0]>0\n puts 1,a[0]\n else\n d=a[1]-a[0]\n puts 2,[a[0]-d,a[-1]+d].sort*' '\n end\n elsif h.size==2\n k=h.keys\n d=e=nil\n if k[0]*2==k[1]\n d=k[0]\n e=k[1]\n elsif k[1]*2==k[0]\n d=k[1]\n e=k[0]\n end\n if d&&e&&h[e]==1\n (1...n).each do |i|\n if a[i]-a[i-1]==e\n puts 1,a[i-1]+d\n exit\n end\n end\n else\n p 0\n end\n else\n p 0\n end\nend"}, {"source_code": "inp = ARGF.read.split\nn = inp.shift.to_i\na = inp.map(&:to_i).sort!\n\nsolve = lambda {\n if n == 0 then 0\n elsif n == 1 then -1\n else\n mp = {}\n (1...n).each { |i|\n x = a[i] - a[i - 1]\n mp[x] = 0 if not mp.key? x\n mp[x] += 1\n }\n x, y = mp.keys.min, mp.keys.max\n if mp.size == 1\n d = a[1] - a[0]\n il = [a[0] - d, a[n - 1] + d]\n il << a[0] + d / 2 if a.size == 2 and d % 2 == 0\n il.sort!.uniq!\n il.size.to_s + \"\\n\" + il.map(&:to_s).join(' ')\n elsif mp.size == 2 and mp[y] == 1 and x * 2 == y\n (1...n).step(1) { |i|\n return \"1\\n#{a[i - 1] + x}\" if a[i] - a[i - 1] == y\n }\n else 0 end\n end\n}\n\nputs solve.call\n"}], "negative_code": [{"source_code": "# Sort the elements\n# find diff array\n# check if d is the same for all elements\n# if so insert at first and last\n# if d is diff for just one element insert at that position\n# if d is diff for more than one element no ap is possible\n# special case when 2 elements - can be inserted in between by changing d as d/ 2\n\nn = gets.to_i\narr = gets.split(\" \").map(&:to_i)\nans = nil\nif arr.length == 1\n\tputs -1\nelse\n\tarr.sort!\n\tif arr.length == 2\n\t\td = arr[1] - arr[0]\n\t\tans = [arr[0] - d,arr[1]+d]\n\t\tmid = (arr[0] + arr[1]) / 2\n\t\tans << mid if (mid - arr[0] == arr[1] - mid) \n\telse\n\t\tdiff = []\n\t\tarr.each_cons(2) do |a,b|\n\t\t\tdiff << b - a\n\t\tend\n\t\td = diff.min\n\t\tcount = diff.count{|x| x!=d}\n\t\tif count == 0\n\t\t\tans = [arr[0] - d,arr[-1]+d]\n\t\telsif count == 1\n\t\t\ti = diff.index{|x| x!=d}\n\t\t\tans = [arr[i]+d] if arr[i]+d == arr[i+1]-d\n\t\tend\n\n\tend\n\n\tif ans\n\t\tputs ans.length\n\t\tputs ans.sort.join(\" \")\n\telse\n\t\tputs 0\n\tend\nend"}, {"source_code": "inp = ARGF.read.split\nn = inp.shift.to_i\na = inp.map(&:to_i).sort!\n\nsolve = lambda {\n if n == 0 then 0\n elsif n == 1 then -1\n else\n mp = {}\n (1...n).each { |i|\n x = a[n - i] - a[n - i - 1]\n mp[x] = 0 if not mp.key? x\n mp[x] += 1\n }\n if mp.size == 1\n d = a[1] - a[0]\n il = [a[0] - d, a[n - 1] + d]\n il << a[0] + d / 2 if a.size == 2 and d % 2 == 0\n il.sort!.uniq!\n il.size.to_s + \"\\n\" + il.map(&:to_s).join(' ')\n elsif mp.size == 2\n x, y = mp.keys.min, mp.keys.max\n if x * 2 == y\n (1...n).step(1) { |i|\n return \"1\\n#{a[i - 1] + x}\" if a[i] - a[i - 1] == y\n }\n else 0 end\n end\n end\n}\n\nputs solve.call\n"}], "src_uid": "e3ca8338beb8852c201be72650e9aabd"} {"nl": {"description": "Dr. Moriarty is about to send a message to Sherlock Holmes. He has a string s. String p is called a substring of string s if you can read it starting from some position in the string s. For example, string \"aba\" has six substrings: \"a\", \"b\", \"a\", \"ab\", \"ba\", \"aba\".Dr. Moriarty plans to take string s and cut out some substring from it, let's call it t. Then he needs to change the substring t zero or more times. As a result, he should obtain a fixed string u (which is the string that should be sent to Sherlock Holmes). One change is defined as making one of the following actions: Insert one letter to any end of the string. Delete one letter from any end of the string. Change one letter into any other one. Moriarty is very smart and after he chooses some substring t, he always makes the minimal number of changes to obtain u. Help Moriarty choose the best substring t from all substrings of the string s. The substring t should minimize the number of changes Moriarty should make to obtain the string u from it.", "input_spec": "The first line contains a non-empty string s, consisting of lowercase Latin letters. The second line contains a non-empty string u, consisting of lowercase Latin letters. The lengths of both strings are in the range from 1 to 2000, inclusive.", "output_spec": "Print the only integer — the minimum number of changes that Dr. Moriarty has to make with the string that you choose.", "sample_inputs": ["aaaaa\naaa", "abcabc\nbcd", "abcdef\nklmnopq"], "sample_outputs": ["0", "1", "7"], "notes": "NoteIn the first sample Moriarty can take any substring of length 3, and it will be equal to the required message u, so Moriarty won't have to make any changes.In the second sample you should take a substring consisting of characters from second to fourth (\"bca\") or from fifth to sixth (\"bc\"). Then you will only have to make one change: to change or to add the last character.In the third sample the initial string s doesn't contain any character that the message should contain, so, whatever string you choose, you will have to make at least 7 changes to obtain the required message."}, "positive_code": [{"source_code": "def find_max a\n\tmax = 0\n\tfor i in 1...a.length\n\t\tmax = i if a[i] > a[max]\n\tend\n\treturn a[max]\nend\n\ns = gets.strip.split(\"\")\nu = gets.strip.split(\"\")\n\na = Array.new(s.length)\nb = Array.new(u.length)\n\ns.each_index do |i|\n\tsum = 0\n\tfor j in 0...u.length\n\t\tsum += 1 if s[i+j] == u[j]\n\tend\n\ta[i] = sum\nend\n\nu.each_index do |i|\n\tsum = 0\n\tfor j in 0...s.length\n\t\tsum += 1 if u[i+j] == s[j]\n\tend\n\tb[i] = sum\nend\n\na_max = find_max(a)\nb_max = find_max(b)\n\n#puts a.to_s\n#puts b.to_s\n#puts a_max\n#puts b_max\n#puts u.length\n#puts s.length\n\n\nif a_max > b_max\n\tc = a_max\nelse\n\tc = b_max\nend\n\nputs u.length - c\n"}], "negative_code": [{"source_code": "def find_max_index a\n\tmax = 0\n\tfor i in 1...a.length\n\t\tmax = i if a[i] > a[max]\n\tend\n\treturn max\nend\n\ns = gets.strip.split(\"\")\nu = gets.strip.split(\"\")\n\nb = Array.new(s.length)\n\ns.each_index do |i|\n\t\tsum = 0\n\t\tfor j in 0...u.length\n\t\t\tsum += 1 if s[i+j] == u[j]\n\t\tend\n\t\tb[i] = sum\n\tend\n\nputs u.length - b[find_max_index(b)]\n"}, {"source_code": "def find_max_index a\n\tmax = 0\n\tfor i in 1...a.length\n\t\tmax = i if a[i] > a[max]\n\tend\n\treturn max\nend\n\ns = gets.strip.split(\"\")\nu = gets.strip.split(\"\")\n\nif(s.length >= u.length)\n\tsmaller = u\n\tbigger = s\nelse\n\tsmaller = s\n\tbigger = u\nend\n\nb = Array.new(s.length)\n\nbigger.each_index do |i|\n\tsum = 0\n\tfor j in 0...smaller.length\n\t\tsum += 1 if bigger[i+j] == smaller[j]\n\tend\n\tb[i] = sum\nend\n\nputs u.length - b[find_max_index(b)]\n"}, {"source_code": "def find_max_index a\n\tmax = 0\n\tfor i in 1...a.length\n\t\tmax = i if a[i] > a[max]\n\tend\n\treturn max\nend\n\ns = gets.strip.split(\"\")\nu = gets.strip.split(\"\")\n\nif(s.length > u.length)\n\tsmaller = u\n\tbigger = s\nelse\n\tsmaller = s\n\tbigger = u\nend\n\nb = Array.new(s.length)\n\nbigger.each_index do |i|\n\tsum = 0\n\tfor j in 0...smaller.length\n\t\tsum += 1 if bigger[i+j] == smaller[j]\n\tend\n\tb[i] = sum\nend\n\nputs b.to_s\nputs find_max_index(b)\nputs u.length\nputs s.length\n\nputs bigger.length - b[find_max_index(b)]\n"}], "src_uid": "430486b13b2f3be12cf47fac105056ae"} {"nl": {"description": "Polycarp has created his own training plan to prepare for the programming contests. He will train for $$$n$$$ days, all days are numbered from $$$1$$$ to $$$n$$$, beginning from the first.On the $$$i$$$-th day Polycarp will necessarily solve $$$a_i$$$ problems. One evening Polycarp plans to celebrate the equator. He will celebrate it on the first evening of such a day that from the beginning of the training and to this day inclusive he will solve half or more of all the problems.Determine the index of day when Polycarp will celebrate the equator.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$1 \\le n \\le 200\\,000$$$) — the number of days to prepare for the programming contests. The second line contains a sequence $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10\\,000$$$), where $$$a_i$$$ equals to the number of problems, which Polycarp will solve on the $$$i$$$-th day.", "output_spec": "Print the index of the day when Polycarp will celebrate the equator.", "sample_inputs": ["4\n1 3 2 1", "6\n2 2 2 2 2 2"], "sample_outputs": ["2", "3"], "notes": "NoteIn the first example Polycarp will celebrate the equator on the evening of the second day, because up to this day (inclusive) he will solve $$$4$$$ out of $$$7$$$ scheduled problems on four days of the training.In the second example Polycarp will celebrate the equator on the evening of the third day, because up to this day (inclusive) he will solve $$$6$$$ out of $$$12$$$ scheduled problems on six days of the training."}, "positive_code": [{"source_code": "n,*a=$<.read.split.map &:to_i\ns=a.reduce :+\nr=0\np (1..n).find{|i|(r+=a[i-1])*2>=s}"}, {"source_code": "n,*a=$<.read.split.map &:to_i\ns=a.reduce :+\nr=0\np (1..n).find{|i|(r+=a[i-1])*2>=s}"}], "negative_code": [], "src_uid": "241157c465fe5dd96acd514010904321"} {"nl": {"description": "You are given two arrays $$$a$$$ and $$$b$$$, both consisting of $$$n$$$ integers.In one move, you can choose two indices $$$i$$$ and $$$j$$$ ($$$1 \\le i, j \\le n$$$; $$$i \\neq j$$$) and swap $$$a_i$$$ with $$$a_j$$$ and $$$b_i$$$ with $$$b_j$$$. You have to perform the swap in both arrays.You are allowed to perform at most $$$10^4$$$ moves (possibly, zero). Can you make both arrays sorted in a non-decreasing order at the end? If you can, print any sequence of moves that makes both arrays sorted.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of testcases. The first line of each testcase contains a single integer $$$n$$$ ($$$2 \\le n \\le 100$$$) — the number of elements in both arrays. The second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le n$$$) — the first array. The third line contains $$$n$$$ integers $$$b_1, b_2, \\dots, b_n$$$ ($$$1 \\le b_i \\le n$$$) — the second array.", "output_spec": "For each testcase, print the answer. If it's impossible to make both arrays sorted in a non-decreasing order in at most $$$10^4$$$ moves, print -1. Otherwise, first, print the number of moves $$$k$$$ $$$(0 \\le k \\le 10^4)$$$. Then print $$$i$$$ and $$$j$$$ for each move $$$(1 \\le i, j \\le n$$$; $$$i \\neq j)$$$. If there are multiple answers, then print any of them. You don't have to minimize the number of moves.", "sample_inputs": ["3\n\n2\n\n1 2\n\n1 2\n\n2\n\n2 1\n\n1 2\n\n4\n\n2 3 1 2\n\n2 3 2 3"], "sample_outputs": ["0\n-1\n3\n3 1\n3 2\n4 3"], "notes": null}, "positive_code": [{"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n as = gets.split.map(&:to_i)\r\n bs = gets.split.map(&:to_i)\r\n swaps = []\r\n 0.upto(n - 1).each do |i|\r\n (i + 1).upto(n - 1).each do |j|\r\n if as[i] > as[j] || bs[i] > bs[j]\r\n swaps << [i, j]\r\n as[i], as[j] = as[j], as[i]\r\n bs[i], bs[j] = bs[j], bs[i]\r\n end\r\n end\r\n end\r\n sorted = as.each_cons(2).all? { |x, y| x <= y } && bs.each_cons(2).all? { |x, y| x <= y }\r\n if sorted\r\n p swaps.size\r\n swaps.each do |i, j|\r\n puts \"#{i + 1} #{j + 1}\"\r\n end\r\n else\r\n p -1\r\n end\r\nend\r\n"}], "negative_code": [], "src_uid": "c1f13141a70c7b9228015c0382c7ca71"} {"nl": {"description": "Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.Petya and his friend Vasya play an interesting game. Petya randomly chooses an integer p from the interval [pl, pr] and Vasya chooses an integer v from the interval [vl, vr] (also randomly). Both players choose their integers equiprobably. Find the probability that the interval [min(v, p), max(v, p)] contains exactly k lucky numbers.", "input_spec": "The single line contains five integers pl, pr, vl, vr and k (1 ≤ pl ≤ pr ≤ 109, 1 ≤ vl ≤ vr ≤ 109, 1 ≤ k ≤ 1000).", "output_spec": "On the single line print the result with an absolute error of no more than 10 - 9.", "sample_inputs": ["1 10 1 10 2", "5 6 8 10 1"], "sample_outputs": ["0.320000000000", "1.000000000000"], "notes": "NoteConsider that [a, b] denotes an interval of integers; this interval includes the boundaries. That is, In first case there are 32 suitable pairs: (1, 7), (1, 8), (1, 9), (1, 10), (2, 7), (2, 8), (2, 9), (2, 10), (3, 7), (3, 8), (3, 9), (3, 10), (4, 7), (4, 8), (4, 9), (4, 10), (7, 1), (7, 2), (7, 3), (7, 4), (8, 1), (8, 2), (8, 3), (8, 4), (9, 1), (9, 2), (9, 3), (9, 4), (10, 1), (10, 2), (10, 3), (10, 4). Total number of possible pairs is 10·10 = 100, so answer is 32 / 100.In second case Petya always get number less than Vasya and the only lucky 7 is between this numbers, so there will be always 1 lucky number."}, "positive_code": [{"source_code": "p1,p2,v1,v2,k = gets.split(' ').map(&:to_i)\n\nlucky = [4, 7]\n\ni = 0\n\nwhile true do\n if lucky[i] == 444444444 then break end\n lucky << lucky[i] * 10 + 4\n lucky << lucky[i] * 10 + 7\n i += 1\nend\n\ndef acc(lucky, i, k, p1, p2, v1, v2)\n if i > 0 then p1 = [p1, lucky[i - 1] + 1].max end\n if i + k < lucky.length then\n v2 = [v2, lucky[i + k] - 1].min\n end\n a = [lucky[i], p2].min - p1 + 1\n b = v2 - [v1, lucky[i + k - 1]].max + 1\n if (a > 0 and b > 0) then return a * b end\n return 0\nend\n\nans = 0\n(lucky.length - k + 1).times do |i|\n ans += acc(lucky, i, k, p1, p2, v1, v2)\n ans += acc(lucky, i, k, v1, v2, p1, p2)\nend\n\nlucky.each do |a|\n if k == 1 and (v1..v2).include?(a) and (p1..p2).include?(a) then ans -= 1 end\nend\n\nputs ans.to_f / ((p2 - p1 + 1) * (v2 - v1 + 1))\n\n"}], "negative_code": [{"source_code": "p1,p2,v1,v2,k = gets.split(' ').map(&:to_i)\n\nlucky = [4, 7]\n\ni = 0\n\nwhile true do\n if lucky[i] == 444444444 then break end\n lucky << lucky[i] * 10 + 4\n lucky << lucky[i] * 10 + 7\n i += 1\nend\n\ndef acc(lucky, i, k, p1, p2, v1, v2)\n if i > 0 then p1 = [p1, lucky[i - 1] + 1].max end\n if i + k < lucky.length then\n v2 = [v2, lucky[i + k] - 1].min\n end\n a = [lucky[i], p2].min - p1 + 1\n b = v2 - [v1, lucky[i + k - 1]].max + 1\n if (a > 0 and b > 0) then return a * b end\n return 0\nend\n\nans = 0\n(lucky.length - k + 1).times do |i|\n ans += acc(lucky, i, k, p1, p2, v1, v2)\n ans += acc(lucky, i, k, v1, v2, p1, p2)\nend\n\nlucky.each do |a|\n if (v1..v2).include?(a) and (p1..p2).include?(a) then ans -= 1 end\nend\n\nputs ans.to_f / ((p2 - p1 + 1) * (v2 - v1 + 1))\n\n"}, {"source_code": "p1,p2,v1,v2,k = gets.split(' ').map(&:to_i)\n\nlucky = [4, 7]\n\ni = 0\n\nwhile true do\n if lucky[i] == 444444444 then break end\n lucky << lucky[i] * 10 + 4\n lucky << lucky[i] * 10 + 7\n i += 1\nend\n\ndef acc(lucky, i, k, p1, p2, v1, v2, add)\n if i > 0 then p1 = [p1, lucky[i - 1] + 1].max end\n if i + k < lucky.length then\n v2 = [v2, lucky[i + k] - 1].min\n end\n a = [lucky[i], p2].min - p1 + 1\n b = v2 - [v1, lucky[i + k - 1] + add].max + 1\n if (a > 0 and b > 0) then return a * b end\n return 0\nend\n\nans = 0\n(lucky.length - k + 1).times do |i|\n ans += acc(lucky, i, k, p1, p2, v1, v2, 0)\n ans += acc(lucky, i, k, v1, v2, p1, p2, k == 1 ? 1 : 0)\nend\n\nputs ans.to_f / ((p2 - p1 + 1) * (v2 - v1 + 1))\n\n"}], "src_uid": "5d76ec741a9d873ce9d7c3ef55eb984c"} {"nl": {"description": "Given three distinct integers $$$a$$$, $$$b$$$, and $$$c$$$, find the medium number between all of them.The medium number is the number that is neither the minimum nor the maximum of the given three numbers. For example, the median of $$$5,2,6$$$ is $$$5$$$, since the minimum is $$$2$$$ and the maximum is $$$6$$$.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 6840$$$) — the number of test cases. The description of each test case consists of three distinct integers $$$a$$$, $$$b$$$, $$$c$$$ ($$$1 \\leq a, b, c \\leq 20$$$).", "output_spec": "For each test case, output a single integer — the medium number of the three numbers.", "sample_inputs": ["9\n\n5 2 6\n\n14 3 4\n\n20 2 1\n\n1 2 3\n\n11 19 12\n\n10 8 20\n\n6 20 3\n\n4 1 3\n\n19 8 4"], "sample_outputs": ["5\n4\n2\n2\n12\n10\n6\n3\n8"], "notes": null}, "positive_code": [{"source_code": "main = -> {\n abc = gets.split.map(&:to_i)\n abc.sum - abc.min - abc.max\n}\n\n\nt = gets.to_i\nputs t.times.map{ main.call }.join(\"\\n\")\n"}, {"source_code": "gets.to_i.times{\r\n n=gets.split.map &:to_i\r\n n=n.sort\r\n puts n[1]\r\n}"}, {"source_code": "=begin\n\nWelcome to GDB Online.\nGDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,\nC#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.\nCode, Compile, Run and Debug online from anywhere in world.\n\n=end\n\nt=gets.to_i\nfor i in 1..t\n a,b,c =gets.split.map &:to_i\n\n if a>b\n if (b>c)\n p b\n elsif a>c\n p c\n else\n p a\n end \n else\n if (a>c)\n p a\n elsif b>c\n p c\n else\n p b\n end \n end\n\nend\n"}, {"source_code": "gets\r\nx=$<.map{|x|x.split.map(&:to_i).sort[1]}\r\nputs x"}], "negative_code": [], "src_uid": "63c2142461c93ae4c962eac1ecb5b192"} {"nl": {"description": "Jack decides to invite Emma out for a dinner. Jack is a modest student, he doesn't want to go to an expensive restaurant. Emma is a girl with high taste, she prefers elite places.Munhattan consists of n streets and m avenues. There is exactly one restaurant on the intersection of each street and avenue. The streets are numbered with integers from 1 to n and the avenues are numbered with integers from 1 to m. The cost of dinner in the restaurant at the intersection of the i-th street and the j-th avenue is cij.Jack and Emma decide to choose the restaurant in the following way. Firstly Emma chooses the street to dinner and then Jack chooses the avenue. Emma and Jack makes their choice optimally: Emma wants to maximize the cost of the dinner, Jack wants to minimize it. Emma takes into account that Jack wants to minimize the cost of the dinner. Find the cost of the dinner for the couple in love.", "input_spec": "The first line contains two integers n, m (1 ≤ n, m ≤ 100) — the number of streets and avenues in Munhattan. Each of the next n lines contains m integers cij (1 ≤ cij ≤ 109) — the cost of the dinner in the restaurant on the intersection of the i-th street and the j-th avenue.", "output_spec": "Print the only integer a — the cost of the dinner for Jack and Emma.", "sample_inputs": ["3 4\n4 1 3 5\n2 2 2 2\n5 4 5 1", "3 3\n1 2 3\n2 3 1\n3 1 2"], "sample_outputs": ["2", "1"], "notes": "NoteIn the first example if Emma chooses the first or the third streets Jack can choose an avenue with the cost of the dinner 1. So she chooses the second street and Jack chooses any avenue. The cost of the dinner is 2.In the second example regardless of Emma's choice Jack can choose a restaurant with the cost of the dinner 1."}, "positive_code": [{"source_code": "gets;p$<.map{|e|e.split.map(&:to_i).min}.max"}, {"source_code": "max_min = 0\nn, m = gets.split.map(&:to_i)\nfor i in 1..n do\n row = gets.split.map(&:to_i)\n min = 1000000001\n row.each do |num|\n min = num if num < min \n end\n max_min = min if min > max_min\nend\nputs max_min\n"}, {"source_code": "n, _ = gets.split.map &:to_i\nprices = Array.new n\nn.times do |i|\n prices[i] = gets.split.map(&:to_i).min\nend\nputs prices.max\n"}, {"source_code": "ans = 0\nrows, cols = gets.split.map(&:to_i)\nrows.times do\n\tans = [ans, gets.split.map(&:to_i).min].max\nend\n\nputs ans\n"}, {"source_code": "gets;p$<.map{|e|e.split.map(&:to_i).min}.max\n"}, {"source_code": "n_e_m = gets.split(\" \")\nn = n_e_m[0].to_i\nm = n_e_m[1].to_i\nrestaurantes = []\nrestaurante_escolhido = 0\nn.times do \n\truas_numero = []\n\truas = gets.split(\" \")\n\tfor i in 0..ruas.size - 1 do\n\t\truas_numero << ruas[i].to_i\n\tend\n\trestaurantes << ruas_numero.sort\nend\nfor avenida in 0..restaurantes.size-1 do\n\tif restaurantes[avenida][0] > restaurante_escolhido\n\t\trestaurante_escolhido = restaurantes[avenida][0]\n\tend\nend\nputs restaurante_escolhido\n"}, {"source_code": "n, m = gets.chomp.split(\" \").map(&:to_i)\nrestraunts = []\nstreet = gets.chomp.split(\" \").map(&:to_i)\nstreet_min = street.min\nrow = 0\nrestraunts << street\n(n-1).times do |i|\n street = gets.chomp.split(\" \").map(&:to_i)\n if street_min < street.min then\n street_min = street.min\n row = i + 1\n end\n restraunts << street\nend\nputs restraunts[row].min"}, {"source_code": "n, m = gets.split(' ').map(&:to_i);\na = [];\nn.times {\n a += [(gets.split(' ').map(&:to_i).min)];\n}\nputs a.max;\n"}, {"source_code": "n =gets.split\nans=0\nn[0].to_i.times do\n now=2000000000\n a=gets.split\n for x in a do\n now=x.to_i if x.to_ians\nend\np ans"}, {"source_code": "gets;p$<.map{|e|e.split.map(&:to_i).min}.max\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\nmax = 0\nn.times do\n a = gets.split.map(&:to_i).min\n max = a if a > max\nend\nputs max"}, {"source_code": "gets;p$<.map{|e|e.split.map(&:to_i).min}.max\n"}, {"source_code": "gets;p$<.map{|e|e.split.map(&:to_i).min}.max\n"}], "negative_code": [{"source_code": "n_e_m = gets.split(\" \")\nn = n_e_m[0].to_i\nm = n_e_m[1].to_i\nrestaurantes = []\nruas_numero = []\nrestaurante_escolhido = 0\nn.times do \n\truas = gets.split(\" \")\n\truas.each do |rua|\n\t\truas_numero << rua.to_i\n\tend\n\trestaurantes << ruas_numero.sort\nend\nfor avenida in 0..restaurantes.size-1 do\n\tif restaurantes[avenida][0] > restaurante_escolhido\n\t\trestaurante_escolhido = restaurantes[avenida][0]\n\tend\nend\nputs restaurante_escolhido"}, {"source_code": "n_e_m = gets.split(\" \")\nn = n_e_m[0].to_i\nrestaurantes = []\nrestaurante_escolhido = 0\nn.times do \n\trestaurantes << gets.split(\" \").sort\nend\nfor avenida in 0..restaurantes.size-1 do\n\tif restaurantes[avenida][0].to_i > restaurante_escolhido\n\t\tavenida_escolhida = restaurantes[avenida]\n\t\trestaurante_escolhido = restaurantes[avenida][0].to_i\n\tend\nend\nputs restaurante_escolhido"}, {"source_code": "n_e_m = gets.split(\" \")\nn = n_e_m[0].to_i\nm = n_e_m[1].to_i\nrestaurantes = []\navenida_letras = []\nrestaurante_da_emma = 0\nemma = 0\njack = 100000000\nn.times do \n\trestaurantes << gets.split(\" \")\nend\nrestaurantes.each_with_index do |avenida, index|\n\tfor rua in 0..avenida.size - 1 do\n\t\trestaurante_atual = restaurantes[index][rua].to_i\n\t\tif restaurante_atual > restaurante_da_emma\n\t\t\temma = index\n\t\t\trestaurante_da_emma = restaurante_atual\n\t\tend\n\tend\nend\nescolha_da_emma = restaurantes[emma]\nfor escolha in 0..escolha_da_emma.size-1 do\n\tif escolha_da_emma[escolha].to_i < jack\n\t\tjack = escolha_da_emma[escolha].to_i\n\tend\nend\nputs jack\n\n"}, {"source_code": "n_e_m = gets.split(\" \")\nn = n_e_m[0].to_i\nm = n_e_m[1].to_i\nrestaurantes = []\navenida_letras = []\nemma = 0\nnova_emma = 1000000000\njack = 100000000\nn.times do \n\trestaurantes << gets.split(\" \")\nend\nrestaurantes.each_with_index do |avenida, index|\n\tfor rua in 0..avenida.size - 1 do\n\t\trestaurante_atual = restaurantes[index][rua].to_i\n\t\tif restaurante_atual < nova_emma\n\t\t\tnova_emma = restaurante_atual\n\t\tend\n\tend\n\tif nova_emma > emma\n\t\temma = index\n\tend\nend\nescolha_da_emma = restaurantes[emma]\nfor escolha in 0..escolha_da_emma.size-1 do\n\tif escolha_da_emma[escolha].to_i < jack\n\t\tjack = escolha_da_emma[escolha].to_i\n\tend\nend\nputs jack\n\n"}, {"source_code": "n_e_m = gets.split(\" \")\nn = n_e_m[0].to_i\nm = n_e_m[1].to_i\nrestaurantes = []\navenida_letras = []\nemma = 0\nnova_emma = 1000000000\njack = 100000000\nn.times do \n\trestaurantes << gets.split(\" \")\nend\nrestaurantes.each_with_index do |avenida, index|\n\tfor rua in 0..avenida.size - 1 do\n\t\trestaurante_atual = restaurantes[index][rua].to_i\n\t\tif restaurante_atual > nova_emma\n\t\t\tnova_emma = restaurante_atual\n\t\tend\n\tend\n\tif nova_emma > emma\n\t\temma = index\n\tend\nend\nescolha_da_emma = restaurantes[emma]\nfor escolha in 0..escolha_da_emma.size-1 do\n\tif escolha_da_emma[escolha].to_i < jack\n\t\tjack = escolha_da_emma[escolha].to_i\n\tend\nend\nputs jack"}], "src_uid": "f2142bc2f44e5d8b77f8561c29038a73"} {"nl": {"description": "Not so long ago, Vlad had a birthday, for which he was presented with a package of candies. There were $$$n$$$ types of candies, there are $$$a_i$$$ candies of the type $$$i$$$ ($$$1 \\le i \\le n$$$).Vlad decided to eat exactly one candy every time, choosing any of the candies of a type that is currently the most frequent (if there are several such types, he can choose any of them). To get the maximum pleasure from eating, Vlad does not want to eat two candies of the same type in a row.Help him figure out if he can eat all the candies without eating two identical candies in a row.", "input_spec": "The first line of input data contains an integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of input test cases. The following is a description of $$$t$$$ test cases of input, two lines for each. The first line of the case contains the single number $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) — the number of types of candies in the package. The second line of the case contains $$$n$$$ integers $$$a_i$$$ ($$$1 \\le a_i \\le 10^9$$$) — the number of candies of the type $$$i$$$. It is guaranteed that the sum of $$$n$$$ for all cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "Output $$$t$$$ lines, each of which contains the answer to the corresponding test case of input. As an answer, output \"YES\" if Vlad can eat candy as planned, and \"NO\" otherwise. You can output the answer in any case (for example, the strings \"yEs\", \"yes\", \"Yes\" and \"YES\" will be recognized as a positive answer).", "sample_inputs": ["6\n2\n2 3\n1\n2\n5\n1 6 2 4 3\n4\n2 2 2 1\n3\n1 1000000000 999999999\n1\n1"], "sample_outputs": ["YES\nNO\nNO\nYES\nYES\nYES"], "notes": "NoteIn the first example, it is necessary to eat sweets in this order: a candy of the type $$$2$$$, it is the most frequent, now $$$a = [2, 2]$$$; a candy of the type $$$1$$$, there are the same number of candies of the type $$$2$$$, but we just ate one, now $$$a = [1, 2]$$$; a candy of the type $$$2$$$, it is the most frequent, now $$$a = [1, 1]$$$; a candy of the type $$$1$$$, now $$$a = [0, 1]$$$; a candy of the type $$$2$$$, now $$$a = [0, 0]$$$ and the candy has run out.In the second example, all the candies are of the same type and it is impossible to eat them without eating two identical ones in a row.In the third example, first of all, a candy of the type $$$2$$$ will be eaten, after which this kind will remain the only kind that is the most frequent, and you will have to eat a candy of the type $$$2$$$ again."}, "positive_code": [{"source_code": "def solve(a)\r\n if a.length == 1 && a[0] == 1\r\n return true\r\n end\r\n\r\n if a.length < 2\r\n return false\r\n end\r\n\r\n a = a.sort\r\n\r\n if a[-1] - a[-2] > 1\r\n return false\r\n end\r\n\r\n return true\r\nend\r\n \r\nfor _ in 1..(gets.to_i)\r\n gets\r\n a = gets.split.map(&:to_i)\r\n puts solve(a) ? 'YES' : 'NO'\r\nend"}], "negative_code": [], "src_uid": "8b926a19f380a56018308668c17c6928"} {"nl": {"description": "Phoenix has decided to become a scientist! He is currently investigating the growth of bacteria.Initially, on day $$$1$$$, there is one bacterium with mass $$$1$$$.Every day, some number of bacteria will split (possibly zero or all). When a bacterium of mass $$$m$$$ splits, it becomes two bacteria of mass $$$\\frac{m}{2}$$$ each. For example, a bacterium of mass $$$3$$$ can split into two bacteria of mass $$$1.5$$$.Also, every night, the mass of every bacteria will increase by one.Phoenix is wondering if it is possible for the total mass of all the bacteria to be exactly $$$n$$$. If it is possible, he is interested in the way to obtain that mass using the minimum possible number of nights. Help him become the best scientist!", "input_spec": "The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$2 \\le n \\le 10^9$$$) — the sum of bacteria masses that Phoenix is interested in. ", "output_spec": "For each test case, if there is no way for the bacteria to exactly achieve total mass $$$n$$$, print -1. Otherwise, print two lines. The first line should contain an integer $$$d$$$  — the minimum number of nights needed. The next line should contain $$$d$$$ integers, with the $$$i$$$-th integer representing the number of bacteria that should split on the $$$i$$$-th day. If there are multiple solutions, print any.", "sample_inputs": ["3\n9\n11\n2"], "sample_outputs": ["3\n1 0 2 \n3\n1 1 2\n1\n0"], "notes": "NoteIn the first test case, the following process results in bacteria with total mass $$$9$$$: Day $$$1$$$: The bacterium with mass $$$1$$$ splits. There are now two bacteria with mass $$$0.5$$$ each. Night $$$1$$$: All bacteria's mass increases by one. There are now two bacteria with mass $$$1.5$$$. Day $$$2$$$: None split. Night $$$2$$$: There are now two bacteria with mass $$$2.5$$$. Day $$$3$$$: Both bacteria split. There are now four bacteria with mass $$$1.25$$$. Night $$$3$$$: There are now four bacteria with mass $$$2.25$$$. The total mass is $$$2.25+2.25+2.25+2.25=9$$$. It can be proved that $$$3$$$ is the minimum number of nights needed. There are also other ways to obtain total mass 9 in 3 nights.$$$ $$$In the second test case, the following process results in bacteria with total mass $$$11$$$: Day $$$1$$$: The bacterium with mass $$$1$$$ splits. There are now two bacteria with mass $$$0.5$$$. Night $$$1$$$: There are now two bacteria with mass $$$1.5$$$. Day $$$2$$$: One bacterium splits. There are now three bacteria with masses $$$0.75$$$, $$$0.75$$$, and $$$1.5$$$. Night $$$2$$$: There are now three bacteria with masses $$$1.75$$$, $$$1.75$$$, and $$$2.5$$$. Day $$$3$$$: The bacteria with mass $$$1.75$$$ and the bacteria with mass $$$2.5$$$ split. There are now five bacteria with masses $$$0.875$$$, $$$0.875$$$, $$$1.25$$$, $$$1.25$$$, and $$$1.75$$$. Night $$$3$$$: There are now five bacteria with masses $$$1.875$$$, $$$1.875$$$, $$$2.25$$$, $$$2.25$$$, and $$$2.75$$$. The total mass is $$$1.875+1.875+2.25+2.25+2.75=11$$$. It can be proved that $$$3$$$ is the minimum number of nights needed. There are also other ways to obtain total mass 11 in 3 nights.$$$ $$$In the third test case, the bacterium does not split on day $$$1$$$, and then grows to mass $$$2$$$ during night $$$1$$$."}, "positive_code": [{"source_code": "\ndef solve n\n arr = []\n (0..50).each do |i|\n arr << (2 ** i)\n break if arr.reduce(:+) >= n\n end\n rem = arr.reduce(:+) - n\n idx = arr.length - 1\n idx.downto(1).each do |i|\n break if rem == 0\n if arr[i] - rem < arr[i - 1]\n rem -= (arr[i] - arr[i - 1])\n arr[i] = arr[i - 1]\n else\n arr[i] -= rem\n rem = 0\n end\n end\n res = []\n (1...arr.length).each do |i|\n res << 0 if arr[i] == arr[i - 1]\n if arr[i] > arr[i - 1]\n if arr[i] == arr[i - 1] * 2 \n res << arr[i - 1]\n else\n res << arr[i] % arr[i - 1]\n end\n end\n end\n res\nend\n\nt = STDIN.gets.chomp.to_i\n\n(1..t).each do |i|\n n = STDIN.gets.chomp.to_i\n res = solve n\n puts res.length\n puts res.join(\" \")\nend\n"}], "negative_code": [], "src_uid": "e21f235ffe7f26d9a7af12a7f3f9a2fd"} {"nl": {"description": "A frog is currently at the point $$$0$$$ on a coordinate axis $$$Ox$$$. It jumps by the following algorithm: the first jump is $$$a$$$ units to the right, the second jump is $$$b$$$ units to the left, the third jump is $$$a$$$ units to the right, the fourth jump is $$$b$$$ units to the left, and so on.Formally: if the frog has jumped an even number of times (before the current jump), it jumps from its current position $$$x$$$ to position $$$x+a$$$; otherwise it jumps from its current position $$$x$$$ to position $$$x-b$$$. Your task is to calculate the position of the frog after $$$k$$$ jumps.But... One more thing. You are watching $$$t$$$ different frogs so you have to answer $$$t$$$ independent queries.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of queries. Each of the next $$$t$$$ lines contain queries (one query per line). The query is described as three space-separated integers $$$a, b, k$$$ ($$$1 \\le a, b, k \\le 10^9$$$) — the lengths of two types of jumps and the number of jumps, respectively.", "output_spec": "Print $$$t$$$ integers. The $$$i$$$-th integer should be the answer for the $$$i$$$-th query.", "sample_inputs": ["6\n5 2 3\n100 1 4\n1 10 5\n1000000000 1 6\n1 1 1000000000\n1 1 999999999"], "sample_outputs": ["8\n198\n-17\n2999999997\n0\n1"], "notes": "NoteIn the first query frog jumps $$$5$$$ to the right, $$$2$$$ to the left and $$$5$$$ to the right so the answer is $$$5 - 2 + 5 = 8$$$.In the second query frog jumps $$$100$$$ to the right, $$$1$$$ to the left, $$$100$$$ to the right and $$$1$$$ to the left so the answer is $$$100 - 1 + 100 - 1 = 198$$$.In the third query the answer is $$$1 - 10 + 1 - 10 + 1 = -17$$$.In the fourth query the answer is $$$10^9 - 1 + 10^9 - 1 + 10^9 - 1 = 2999999997$$$.In the fifth query all frog's jumps are neutralized by each other so the answer is $$$0$$$.The sixth query is the same as the fifth but without the last jump so the answer is $$$1$$$."}, "positive_code": [{"source_code": "gets;p *$<.map{|e|a,b,k=e.split.map &:to_i;k/2*(a-b)+k%2*a}"}, {"source_code": "#(5-2) * (3/2 - 3%2 /2) + 5 * (3%2)\nn = gets.to_i\n\nfor i in 0...n\n pos = 0\n a, b, t = gets.split.map(&:to_i)\n pos += (a - b) * (t / 2) + a * (t % 2)\n puts pos\nend"}, {"source_code": "n = gets.strip.to_i\nresults = []\nfor i in 0..n-1\n a, b, k = gets.strip.split.map(&:to_i)\n\n result = 0\n\n if a == b\n if k % 2 == 0\n result = 0\n else\n result = a\n end\n else\n if k % 2 == 0\n result = a*k/2-b*k/2\n else\n result = a*((k/2).floor+1)-b*(k/2).floor\n end\n end\n\n results << result\nend\nresults.each do |result|\n p result\nend\n"}, {"source_code": "class Sol\n def initialize(a, b, k)\n @k = k\n @b = b\n @a = a\n end\n\n def res\n puts (@k + 1) / 2 * @a - (@k / 2) * @b\n end\nend\n\nt = gets.to_i\nt.times do\n a, b, k = gets.split.map &:to_i\n s = Sol.new(a, b, k)\n s.res\nend\n\n"}, {"source_code": "t = gets.strip.to_i\nt.times do\n a, b, k = gets.strip.split.map(&:to_i)\n puts (a - b) * (k / 2) + ((k % 2).zero? ? 0 : a)\nend"}, {"source_code": "gets.to_i.times do\n a, b, k = gets.split.map(&:to_i)\n puts (k/2) * (a-b) + (k%2)*a\nend\n"}, {"source_code": "gets.to_i.times do\n a,b,k = gets.split.map(&:to_i)\n puts a*((k+1)/2)-b*(k/2)\nend"}, {"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\n\nt = gets.to_i\nt.times {\n a, b, k = gets.split.map(&:to_i)\n even_jumps, odd_jumps = (k/2.0).ceil, k/2\n puts even_jumps * a - odd_jumps * b\n}\n"}, {"source_code": "t = gets.to_i\n\nt.times do\n s = gets.split.map &:to_i\n q = s[2] / 2 + s[2] % 2\n puts q * s[0] - (s[2] - q) * s[1]\nend"}, {"source_code": "t = gets.to_i\nt.times do\n a, b, k = gets.split.map(&:to_i)\n p(a*((k + 1)/2) - b*(k/2))\nend\n"}, {"source_code": "def solve()\n line = gets\n\n a = line.split(' ')[0].to_i\n b = line.split(' ')[1].to_i\n k = line.split(' ')[2].to_i\n\n if ( k%2 == 0 )\n return ( a * (k/2) - b * (k/2)) \n end\n\n return ( a * (k/2) - b * (k/2)) + a\nend\n\nntest = gets\n\nntest.to_i.times { |nn|\n\n puts solve()\n\n}\n"}, {"source_code": "1.upto(gets.to_i) do\n a, b, k = gets.split(' ').map(&:to_i)\n\n puts (a-b)*k.div(2) + a*k.modulo(2)\nend\n"}, {"source_code": "t=gets.to_i\n(1).upto(t) do\n a=gets.split(' ').map(&:to_i)\n x = a[1]*(a[2]>>1)\n y = a[0]*(1+a[2]>>1)\n puts y-x\nend\n"}], "negative_code": [], "src_uid": "1f435ba837f59b007167419896c836ae"} {"nl": {"description": "A string is binary, if it consists only of characters \"0\" and \"1\".String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string \"010\" has six substrings: \"0\", \"1\", \"0\", \"01\", \"10\", \"010\". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs.You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters \"1\".", "input_spec": "The first line contains the single integer k (0 ≤ k ≤ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters.", "output_spec": "Print the single number — the number of substrings of the given string, containing exactly k characters \"1\". Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.", "sample_inputs": ["1\n1010", "2\n01010", "100\n01010"], "sample_outputs": ["6", "4", "0"], "notes": "NoteIn the first sample the sought substrings are: \"1\", \"1\", \"10\", \"01\", \"10\", \"010\".In the second sample the sought substrings are: \"101\", \"0101\", \"1010\", \"01010\"."}, "positive_code": [{"source_code": "k = gets.to_i\ns = gets.chop\na = [0]\nb = []\ns.size.times{|i|\n if s[i] == '1'\n a << a[-1] + 1\n b << i\n else\n a << a[-1]\n end\n}\nb += [s.size] * (k + 1)\nc = 0\na.each_with_index{|i, j|\n c += b[i + k] - (k == 0 ? j : b[i + k - 1])\n}\np c\n"}], "negative_code": [{"source_code": "k = gets.to_i\ns = gets.chop\na = [0]\nb = []\ns.size.times{|i|\n if s[i] == '1'\n a << a[-1] + 1\n b << i\n else\n a << a[-1]\n end\n}\nb += [s.size] * (k + 1)\np a.map{|i| b[i + k] - b[i + k - 1]}.inject(&:+)\n"}], "src_uid": "adc43f273dd9b3f1c58b052a34732a50"} {"nl": {"description": "The GCD table G of size n × n for an array of positive integers a of length n is defined by formula Let us remind you that the greatest common divisor (GCD) of two positive integers x and y is the greatest integer that is divisor of both x and y, it is denoted as . For example, for array a = {4, 3, 6, 2} of length 4 the GCD table will look as follows: Given all the numbers of the GCD table G, restore array a.", "input_spec": "The first line contains number n (1 ≤ n ≤ 500) — the length of array a. The second line contains n2 space-separated numbers — the elements of the GCD table of G for array a. All the numbers in the table are positive integers, not exceeding 109. Note that the elements are given in an arbitrary order. It is guaranteed that the set of the input data corresponds to some array a.", "output_spec": "In the single line print n positive integers — the elements of array a. If there are multiple possible solutions, you are allowed to print any of them.", "sample_inputs": ["4\n2 1 2 3 4 3 2 6 1 1 2 2 1 2 3 2", "1\n42", "2\n1 1 1 1"], "sample_outputs": ["4 3 6 2", "42", "1 1"], "notes": null}, "positive_code": [{"source_code": "n = gets.chomp.to_i\n\nh = Hash.new(0)\ngets.chomp.split(' ').map(&:to_i).each { |item| h[item] += 1 }\n\narr = h.keys.sort.reverse\n\nans = Array.new(n, nil)\n\ndef gcd(a, b)\n return a if b == 0\n gcd(b, a % b)\nend\n\nindex = 0\narr.each do |item|\n until h[ item ] == 0\n ans[ index ] = item\n h[ item ] -= 1\n index.times do |i|\n h[ gcd( item, ans[i] ) ] -= 2\n end\n index += 1\n end\nend\n\nputs ans.join(' ')\n"}], "negative_code": [], "src_uid": "71dc07f0ea8962f23457af1d6509aeee"} {"nl": {"description": "You are given an array $$$a$$$ of $$$n$$$ positive integers. Determine if, by rearranging the elements, you can make the array strictly increasing. In other words, determine if it is possible to rearrange the elements such that $$$a_1 < a_2 < \\dots < a_n$$$ holds.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 100$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 100$$$) — the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_i$$$ ($$$1 \\leq a_i \\leq 10^9$$$) — the elements of the array.", "output_spec": "For each test case, output \"YES\" (without quotes) if the array satisfies the condition, and \"NO\" (without quotes) otherwise. You can output the answer in any case (for example, the strings \"yEs\", \"yes\", \"Yes\" and \"YES\" will be recognized as a positive answer).", "sample_inputs": ["3\n\n4\n\n1 1 1 1\n\n5\n\n8 7 1 3 4\n\n1\n\n5"], "sample_outputs": ["NO\nYES\nYES"], "notes": "NoteIn the first test case any rearrangement will keep the array $$$[1,1,1,1]$$$, which is not strictly increasing.In the second test case, you can make the array $$$[1,3,4,7,8]$$$."}, "positive_code": [{"source_code": "n = gets.to_i\r\nn.times do\r\n _ = gets.to_i\r\n puts gets.split(' ').uniq! ? 'NO' : 'YES'\r\nend\r\n"}, {"source_code": "gets.to_i.times do\r\n n=gets.to_i\r\n a=gets.split.map(&:to_i).uniq\r\n puts a.size==n ? 'YES' : 'NO'\r\nend"}, {"source_code": "t=gets.to_i\nt.times do\n _=gets\n a=gets.split.map(&:to_i)\n l=a.length\n nl=a.uniq.length\n if l==nl\n puts \"YES\"\n else\n puts \"NO\"\n end\nend"}, {"source_code": "gets.to_i.times do\n n=gets.to_i\n a=gets.split.map(&:to_i).uniq\n puts a.size==n ? 'YES' : 'NO'\nend\n"}], "negative_code": [], "src_uid": "288f147bb8a3c30b0bb712a01c65109b"} {"nl": {"description": "BerOilGasDiamondBank has branches in n cities, at that n is an even number. The bank management wants to publish a calendar with the names of all those cities written in two columns: the calendar should consist of exactly n / 2 lines of strictly equal length, each of which contains exactly two names and exactly one separator character between them. The name of every city should be used in the calendar exactly once. For historical reasons the symbol d is used as the separator of words in the calendar. The BerOilGasDiamondBank management wants to show that all its branches are equally important to it, that's why the order of their appearance in the calendar should be following: if we \"glue\"(concatinate) all the n / 2 calendar lines (from top to bottom) to make a single line, then the lexicographically minimal line is obtained. No separator character will be used to separate calendar lines. For example, if the lines are \"bertown!berville\", \"newberville!bera\", then the resulting line is \"bertown!bervillenewberville!bera\". In some sense one has to find the lexicographically minimal calendar, where the comparison of calendars happens line by line.Help BerOilGasDiamondBank and construct the required calendar.", "input_spec": "The first line contains an integer n (1 ≤ n ≤ 104, n is even) which is the number of branches. Then follow n lines which are the names of the cities. All the names consist of lowercase Latin letters; their lengths are no less than 1 and no more than 10 symbols. The next line contains a single symbol d (d has an ASCII-code from 33 to 126 inclusively, excluding lowercase Latin letters) which is the separator between words in the calendar lines. It is guaranteed that the calendar is possible to be constructed and all the names are different.", "output_spec": "Print n / 2 lines of similar length which are the required calendar. Every line should contain exactly two words and exactly one separator between them. If there are several solutions, print the lexicographically minimal one. The lexicographical comparison of lines is realized by the \"<\" operator in the modern programming languages.", "sample_inputs": ["4\nb\naa\nhg\nc\n.", "2\naa\na\n!", "2\naa\na\n|"], "sample_outputs": ["aa.b\nc.hg", "a!aa", "aa|a"], "notes": null}, "positive_code": [{"source_code": "class Solution\n def initialize\n @n = gets.to_i\n @cities = []\n @n.times {@cities << gets.chomp}\n @d = gets.chomp\n end\n\n def solve\n length = (@cities.inject(0){|x, s| x + s.length})/(@n/2)\n\n left = []\n right = []\n str_by_len = []\n for c in @cities do\n str_by_len[c.length] ||= []\n str_by_len[c.length] << c\n end\n (1..10).each do |i|\n unless str_by_len[i].nil?\n str_by_len[i].sort!\n end\n end\n\n (@n/2).times do\n min_s = '~' * 21\n min_i = -1\n min_j = -1\n (1..10).each do |i|\n next if str_by_len[i].nil? or str_by_len[i].empty?\n s1 = str_by_len[i].shift\n j = length - i\n next if str_by_len[j].nil? or str_by_len[j].empty?\n s2 = str_by_len[j].shift\n if s1 + @d + s2 < min_s\n min_s = s1 + @d + s2\n min_i = i\n min_j = j\n end\n str_by_len[j].unshift(s2)\n str_by_len[i].unshift(s1)\n end\n left << str_by_len[min_i].shift\n right << str_by_len[min_j].shift\n end\n \n # find the last right column item\n (1..10).each do |i|\n if (not str_by_len[i].nil?) and (not str_by_len[i].empty?)\n right << str_by_len[i].shift\n break\n end\n end\n\n (@n/2).times do\n puts left.shift + @d + right.shift\n end\n end\nend\n\nsolution = Solution.new\nsolution.solve\n"}, {"source_code": "class Solution\n def initialize\n @n = gets.to_i\n @cities = []\n @n.times {@cities << gets.chomp}\n @d = gets.chomp\n end\n\n def solve\n length = (@cities.inject(0){|x, s| x + s.length})/(@n/2)\n\n left = []\n right = []\n str_by_len = []\n for c in @cities do\n str_by_len[c.length] ||= []\n str_by_len[c.length] << c\n end\n (1..10).each do |i|\n unless str_by_len[i].nil?\n str_by_len[i].sort!\n end\n end\n\n (@n/2).times do\n min_s = '~' * 21\n min_i = -1\n min_j = -1\n (1..10).each do |i|\n next if str_by_len[i].nil? or str_by_len[i].empty?\n s1 = str_by_len[i].shift\n j = length - i\n next if str_by_len[j].nil? or str_by_len[j].empty?\n s2 = str_by_len[j].shift\n if s1 + @d + s2 < min_s\n min_s = s1 + @d + s2\n min_i = i\n min_j = j\n end\n str_by_len[j].unshift(s2)\n str_by_len[i].unshift(s1)\n end\n left << str_by_len[min_i].shift\n right << str_by_len[min_j].shift\n end\n \n (@n/2).times do\n puts left.shift + @d + right.shift\n end\n end\nend\n\nsolution = Solution.new\nsolution.solve\n"}], "negative_code": [], "src_uid": "0f04f757dc734208d803ee0f6be5d1f0"} {"nl": {"description": "One day Vasya went out for a walk in the yard but there weren't any of his friends outside and he had no one to play touch and run. But the boy didn't lose the high spirits and decided to play touch and run with himself. You may ask: \"How did he do that?\" The answer is simple.Vasya noticed that the yard is a rectangular n × m field. The squares have coordinates (x, y) (1 ≤ x ≤ n, 1 ≤ y ≤ m), where x is the index of the row and y is the index of the column.Initially Vasya stands in the square with coordinates (xc, yc). To play, he has got a list of k vectors (dxi, dyi) of non-zero length. The game goes like this. The boy considers all vectors in the order from 1 to k, and consecutively chooses each vector as the current one. After the boy has chosen a current vector, he makes the maximally possible number of valid steps in the vector's direction (it is possible that he makes zero steps).A step is defined as one movement from the square where the boy is standing now, in the direction of the current vector. That is, if Vasya is positioned in square (x, y), and the current vector is (dx, dy), one step moves Vasya to square (x + dx, y + dy). A step is considered valid, if the boy does not go out of the yard if he performs the step.Vasya stepped on and on, on and on until he ran out of vectors in his list. Ha had been stepping for so long that he completely forgot how many steps he had made. Help the boy and count how many steps he had made.", "input_spec": "The first input line contains two integers n and m (1 ≤ n, m ≤ 109) — the yard's sizes. The second line contains integers xc and yc — the initial square's coordinates (1 ≤ xc ≤ n, 1 ≤ yc ≤ m). The third line contains an integer k (1 ≤ k ≤ 104) — the number of vectors. Then follow k lines, each of them contains two integers dxi and dyi (|dxi|, |dyi| ≤ 109, |dx| + |dy| ≥ 1).", "output_spec": "Print the single number — the number of steps Vasya had made. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.", "sample_inputs": ["4 5\n1 1\n3\n1 1\n1 1\n0 -2", "10 10\n1 2\n1\n-1 0"], "sample_outputs": ["4", "0"], "notes": "NoteIn the first sample Vasya is initially positioned at square (1, 1) and makes 3 steps by the first vector (1, 1). So, he consecutively visits the squares (2, 2), (3, 3), (4, 4). Then he makes 0 steps by the second vector (1, 1). He makes 1 more step by the third vector (0,  - 2) and he ends up in square (4, 2). Overall, Vasya makes 4 steps.In the second sample Vasya is initially positioned in square (1, 2) and makes 0 steps by vector ( - 1, 0), as the square with coordinates (0, 2) is located outside the yard."}, "positive_code": [{"source_code": "n,m = gets.chomp.split(/ /).map!{|x| x.to_i}\np0 = gets.chomp.split(/ /).map!{|x| x.to_i}\nk = gets.chomp.to_i\nstep = 0\nfor i in 0..k-1\n\tp = gets.chomp.split(/ /).map!{|x| x.to_i}\n\tsx = 0\n\tsy = 0\n\tif p[0] != 0\n\t\tif p[0] < 0\n\t\t\tsx = (p0[0]-1) / -p[0]\n\t\telse\n\t\t\tsx = (n - p0[0]) / p[0]\n\t\tend\n\tend\n\tif p[1] != 0\n\t\tif p[1] < 0\n\t\t\tsy = (p0[1]-1) / -p[1]\n\t\telse\n\t\t\tsy = (m - p0[1]) / p[1]\n\t\tend\n\tend\n\tws = [sx, sy].min\n\tws = sy if p[0] == 0 && p[1] != 0\n\tws = sx if p[0] != 0 && p[1] == 0\n\tp0 = [p0[0]+p[0]*ws, p0[1]+p[1]*ws]\n\tstep += ws\nend\nputs step\n\n"}, {"source_code": "I = ->{gets.split.map(&:to_i)}\nF = ->(s, t, d){d == 0 ? 10 ** 10 : (d > 0 ? t - s : 1 - s) / d}\nr, c = I[]\nx, y = I[]\nn = 0\nI[][0].times do\n u, v = I[]\n m = [F[x, r, u], F[y, c, v]].min\n n += m\n x += m * u\n y += m * v\nend\np n\n"}, {"source_code": "w,h=gets.split.map(&:to_i)\nx,y=gets.split.map(&:to_i)\nx,y=x-1,y-1\nk=gets.to_i\nans=0\nk.times{|i|\n\tdx,dy=gets.split.map(&:to_i)\n\tminimum=[]\n\tif dx<0\n\t\tminimum+=[x/-dx]\n\telsif dx>0\n\t\tminimum+=[(w-x-1)/dx]\n\tend\n\n\tif dy<0\n\t\tminimum+=[y/-dy]\n\telsif dy>0\n\t\tminimum+=[(h-y-1)/dy]\n\tend\n\tans+=minimum.min\n\tx+=dx*minimum.min\n\ty+=dy*minimum.min\n}\n\np ans\n"}, {"source_code": "#!/usr/bin/env ruby\n\ndef getmax(cur, lim, d)\n if d > 0\n (lim-cur) / d\n elsif d < 0\n (cur-1) / (-d)\n else\n 1000000000\n end\nend\n\nn, m = gets.split.map(&:to_i)\nx, y = gets.split.map(&:to_i)\nk = gets.to_i\n\nans = 0\nk.times do\n dx, dy = gets.split.map(&:to_i)\n max_x = getmax(x, n, dx)\n max_y = getmax(y, m, dy)\n maxstep = [max_x, max_y].min\n x += dx*maxstep\n y += dy*maxstep\n ans += maxstep\nend\n\np ans\n"}, {"source_code": "I = ->{gets.split.map(&:to_i)}\nF = ->(s, t, d){d == 0 ? 10 ** 10 : (d > 0 ? t - s : 1 - s) / d}\nr, c = I[]\nx, y = I[]\nn = 0\nI[][0].times do\n u, v = I[]\n m = [F[x, r, u], F[y, c, v]].min\n n += m\n x += m * u\n y += m * v\nend\np n\n\n"}, {"source_code": "I = ->{gets.split.map(&:to_i)}\nF = ->(s, t, d){d == 0 ? 10 ** 10 : (d > 0 ? t - s : 1 - s) / d}\nr, c = I[]\nx, y = I[]\nn = 0\nI[][0].times do\n u, v = I[]\n m = [F[x, r, u], F[y, c, v]].min\n n += m\n x += m * u\n y += m * v\nend\np n\n\n"}, {"source_code": "n, m = STDIN.readline.split.map {|s| s.to_i }\nx, y = STDIN.readline.split.map {|s| s.to_i }\n\ndef max_steps(x, d, n)\n return 10**9 if d == 0\n span = (d > 0 ? n-x : x-1)\n d = d.abs\n return span/d\nend\n\ncount = 0\nk = STDIN.readline.to_i\nk.times do\n dx, dy = STDIN.readline.split.map {|s| s.to_i }\n a, b = max_steps(x, dx, n), max_steps(y, dy, m)\n c = [a, b].min\n x += c*dx\n y += c*dy\n count += c\nend\n\nputs count\n\n"}, {"source_code": "I = ->{gets.split.map(&:to_i)}\nF = ->(s, t, d){d == 0 ? 10 ** 10 : (d > 0 ? t - s : 1 - s) / d}\nr, c = I[]\nx, y = I[]\nn = 0\nI[][0].times do\n u, v = I[]\n m = [F[x, r, u], F[y, c, v]].min\n n += m\n x += m * u\n y += m * v\nend\np n\n"}, {"source_code": "I = ->{gets.split.map(&:to_i)}\nF = ->(s, t, d){d == 0 ? 10 ** 10 : (d > 0 ? t - s : 1 - s) / d}\nr, c = I[]\nx, y = I[]\nn = 0\nI[][0].times do\n u, v = I[]\n m = [F[x, r, u], F[y, c, v]].min\n n += m\n x += m * u\n y += m * v\nend\np n\n\n"}, {"source_code": "I = ->{gets.split.map(&:to_i)}\nF = ->(s, t, d){d == 0 ? 10 ** 10 : (d > 0 ? t - s : 1 - s) / d}\nr, c = I[]\nx, y = I[]\nn = 0\nI[][0].times do\n u, v = I[]\n m = [F[x, r, u], F[y, c, v]].min\n n += m\n x += m * u\n y += m * v\nend\np n\n\n"}, {"source_code": "sizes=gets.chomp\ninit=gets.chomp\nv=[]\nn=gets.chomp.to_i\nn.times do\n v<-1 do\n delta=(max-min)/2\n i=min+delta\n if !(c[0]+a[j][0]*i<1||c[0]+a[j][0]*i>s[0]||c[1]+a[j][1]*i<1||c[1]+a[j][1]*i>s[1])\n min=i\n if delta==0\n c[0]+=a[j][0]*i;c[1]+=a[j][1]*i\n steps+=i\n break\n end\n else\n max=i\n end\nend\nend\nputs steps"}, {"source_code": "n,m = gets.split.map(&:to_i)\nx,y = gets.split.map(&:to_i)\nvs = gets.to_i\nv = []\nvs.times{ v << gets.split.map(&:to_i) }\nst = 0\n\nv.each do |dxy|\n dx,dy = dxy[0],dxy[1]\n cx = dx > 0 ? n : 1\n cy = dy > 0 ? m : 1\n xx = dx != 0 ? ((cx-x)/dx).abs : 1000000000000\n yy = dy != 0 ? ((cy-y)/dy).abs : 1000000000000\n s = [yy,xx].min\n y = y+s*dy\n x = x+s*dx\n st += s\nend\np st\n"}, {"source_code": "I = ->{gets.split.map(&:to_i)}\nF = ->(s, t, d){d == 0 ? 10 ** 10 : (d > 0 ? t - s : 1 - s) / d}\nr, c = I[]\nx, y = I[]\nn = 0\nI[][0].times do\n u, v = I[]\n m = [F[x, r, u], F[y, c, v]].min\n n += m\n x += m * u\n y += m * v\nend\np n"}, {"source_code": "I = ->{gets.split.map(&:to_i)}\nF = ->(s, t, d){d == 0 ? 10 ** 10 : (d > 0 ? t - s : 1 - s) / d}\nr, c = I[]\nx, y = I[]\nn = 0\nI[][0].times do\n u, v = I[]\n m = [F[x, r, u], F[y, c, v]].min\n n += m\n x += m * u\n y += m * v\nend\np n\n\n"}, {"source_code": "inp = gets.split(\" \")\nn = inp[0].to_i\nm = inp[1].to_i\ninp = gets.split(\" \")\nxc = inp[0].to_i\nyc = inp[1].to_i\nk = gets.to_i\nsteps = 0\nfor i in 0..(k-1)\n\n inp = gets.split(\" \")\n dxi = inp[0].to_i\n dyi = inp[1].to_i\n \n xsteps = 1000000000\n ysteps = 1000000000\n if(dxi > 0)\n xsteps = (n - xc) / dxi\n elsif (dxi < 0)\n xsteps = (xc - 1) / (-dxi)\n end\n if(dyi > 0)\n ysteps = (m - yc) / dyi\n elsif (dyi < 0)\n ysteps = (yc - 1) / (-dyi)\n end\n #puts ysteps\n s = [xsteps, ysteps].min\n xc += s * dxi\n yc += s * dyi\n #puts s\n #puts xc.to_s + \",\" + yc.to_s\n steps += s\nend\n\nputs steps"}, {"source_code": "I = ->{gets.split.map(&:to_i)}\nF = ->(s, t, d){d == 0 ? 10 ** 10 : (d > 0 ? t - s : 1 - s) / d}\nr, c = I[]\nx, y = I[]\nn = 0\nI[][0].times do\n u, v = I[]\n m = [F[x, r, u], F[y, c, v]].min\n n += m\n x += m * u\n y += m * v\nend\np n\n\n"}, {"source_code": "INF = 1000000001\nnm = gets.split(\" \")\nn = nm[0].to_i\nm = nm[1].to_i\n\nxy = gets.split(\" \")\nx = xy[0].to_i\ny = xy[1].to_i\n\nk = gets.to_i\nans = 0\nwhile k > 0\n dxy = gets.split(\" \")\n dx = dxy[0].to_i\n w = INF\n if dx > 0 then\n w = (n - x) / dx\n elsif dx < 0 then\n w = (x-1) / (-dx)\n end\n dy = dxy[1].to_i\n h = INF\n if dy > 0 then\n h = (m - y) / dy\n elsif dy < 0 then\n h = (y - 1) / (-dy)\n end\n mn = w < h ? w : h\n ans += mn\n x += dx * mn\n y += dy * mn\n k = k-1\nend\n\nputs ans\n"}, {"source_code": "I = ->{gets.split.map(&:to_i)}\nF = ->(s, t, d){d == 0 ? 10 ** 10 : (d > 0 ? t - s : 1 - s) / d}\nr, c = I[]\nx, y = I[]\nn = 0\nI[][0].times do\n u, v = I[]\n m = [F[x, r, u], F[y, c, v]].min\n n += m\n x += m * u\n y += m * v\nend\np n\n\n"}, {"source_code": "I = ->{gets.split.map(&:to_i)}\nF = ->(s, t, d){d == 0 ? 10 ** 10 : (d > 0 ? t - s : 1 - s) / d}\nr, c = I[]\nx, y = I[]\nn = 0\nI[][0].times do\n u, v = I[]\n m = [F[x, r, u], F[y, c, v]].min\n n += m\n x += m * u\n y += m * v\nend\np n\n\n"}], "negative_code": [{"source_code": "I = ->{gets.split.map(&:to_i)}\nF = ->(s, t, d){d == 0 ? t : (d > 0 ? t - s : 1 - s) / d}\nr, c = I[]\nx, y = I[]\nn = 0\nI[][0].times do\n u, v = I[]\n m = [F[x, r, u], F[y, c, v]].min\n n += m\n x += m * u\n y += m * v\nend\np n\n"}, {"source_code": "sizes=gets.chomp\ninit=gets.chomp\nv=[]\nn=gets.chomp.to_i\nn.times do\n v<0 do\n if !(c[0]+a[j][0]*i<1||c[0]+a[j][0]*i>s[0]||c[1]+a[j][1]*i<1||c[1]+a[j][1]*i>s[1])\n c[0]+=a[j][0]*i;c[1]+=a[j][1]*i\n steps+=i\n break\n end\n i/=2\nend\nend\nputs steps"}, {"source_code": "sizes=gets.chomp\ninit=gets.chomp\nv=[]\n(n_v=gets.chomp.to_i).times do\n v<s[0]||c[1]<1||c[1]>s[1] do\n c[0]+=a[i][0];c[1]+=a[i][1]\n if c[0]<1||c[0]>s[0]||c[1]<1||c[1]>s[1]\n c[0]-=a[i][0];c[1]-=a[i][1]\n break\n end\n steps+=1\n end\n i+=1\nend\nputs steps\n"}, {"source_code": "inp = gets.split(\" \")\nn = inp[0].to_i\nm = inp[1].to_i\ninp = gets.split(\" \")\nxc = inp[0].to_i\nyc = inp[1].to_i\nk = gets.to_i\nsteps = 0\nfor i in 0..(k-1)\n\n inp = gets.split(\" \")\n dxi = inp[0].to_i\n dyi = inp[1].to_i\n \n xsteps = 1000000000\n ysteps = 1000000000\n if(dxi > 0)\n xsteps = (n - xc) / dxi\n elsif (dxi < 0)\n xsteps = (xc - 1) / (-dxi)\n end\n if(dyi > 0)\n ysteps = (m - yc) / dyi\n elsif (dyi < 0)\n ysteps = (yc - 1) / (-dyi)\n end\n #puts ysteps\n s = [xsteps, ysteps].min\n xc += s * dxi\n yc += s * dyi\n puts s\n puts xc.to_s + \",\" + yc.to_s\n steps += s\nend\n\nputs steps"}, {"source_code": "inp = gets.split(\" \")\nn = inp[0].to_i\nm = inp[1].to_i\ninp = gets.split(\" \")\nxc = inp[0].to_i\nyc = inp[1].to_i\nk = gets.to_i\nsteps = 0\nfor i in 0..(k-1)\n\n inp = gets.split(\" \")\n dxi = inp[0].to_i\n dyi = inp[1].to_i\n \n xsteps = 1000000000\n ysteps = 1000000000\n if(dxi > 0)\n xsteps = (n - xc) / dxi\n elsif (dxi < 0)\n xsteps = (xc - 1) / (-dxi)\n end\n if(dyi > 0)\n ysteps = (n - yc) / dyi\n elsif (dyi < 0)\n ysteps = (yc - 1) / (-dyi)\n end\n #puts ysteps\n s = [xsteps, ysteps].min\n xc += s * dxi\n yc += s * dyi\n #puts s\n #puts xc.to_s + \",\" + yc.to_s\n steps += s\nend\n\nputs steps"}], "src_uid": "2737d7aa245627b1f7992b1148ed49ce"} {"nl": {"description": "A class of students wrote a multiple-choice test.There are $$$n$$$ students in the class. The test had $$$m$$$ questions, each of them had $$$5$$$ possible answers (A, B, C, D or E). There is exactly one correct answer for each question. The correct answer for question $$$i$$$ worth $$$a_i$$$ points. Incorrect answers are graded with zero points.The students remember what answers they gave on the exam, but they don't know what are the correct answers. They are very optimistic, so they want to know what is the maximum possible total score of all students in the class. ", "input_spec": "The first line contains integers $$$n$$$ and $$$m$$$ ($$$1 \\le n, m \\le 1000$$$) — the number of students in the class and the number of questions in the test. Each of the next $$$n$$$ lines contains string $$$s_i$$$ ($$$|s_i| = m$$$), describing an answer of the $$$i$$$-th student. The $$$j$$$-th character represents the student answer (A, B, C, D or E) on the $$$j$$$-th question. The last line contains $$$m$$$ integers $$$a_1, a_2, \\ldots, a_m$$$ ($$$1 \\le a_i \\le 1000$$$) — the number of points for the correct answer for every question.", "output_spec": "Print a single integer — the maximum possible total score of the class.", "sample_inputs": ["2 4\nABCD\nABCE\n1 2 3 4", "3 3\nABC\nBCD\nCDE\n5 4 12"], "sample_outputs": ["16", "21"], "notes": "NoteIn the first example, one of the most optimal test answers is \"ABCD\", this way the total number of points will be $$$16$$$.In the second example, one of the most optimal test answers is \"CCC\", this way each question will be answered by exactly one student and the total number of points is $$$5 + 4 + 12 = 21$$$."}, "positive_code": [{"source_code": "a, b = gets.split.map(&:to_i)\nc = Array.new(a).map do\n gets.chomp.bytes\nend\nf = gets.split.map(&:to_i)\nputs(\n c.transpose.zip(f).map do |d, i|\n i * d.uniq.map{ |e| d.count e }.max\n end.inject(:+)\n)\n"}, {"source_code": "n, m = STDIN.gets.chomp.split(\" \").map(&:to_i)\n\nfreq = Array.new(m){ {'A' => 0, 'B' => 0, 'C' => 0, 'D' => 0, 'E' => 0} }\n\nn.times do |i|\n line = STDIN.gets.chomp.each_char.with_index do |c, j|\n freq[j][c] += 1\n end\nend\n\nscores = STDIN.gets.chomp.split(\" \").map(&:to_i)\nputs freq.map.with_index{|f, i| f.values.max*scores[i]}.reduce(&:+)\n"}, {"source_code": "n, m = gets.split.map &:to_i\ns = []\nn.times { s << gets.chomp }\nb = m.times.map do |j|\n h = {}\n (0...n).each do |i|\n h[s[i][j]] ||= 0\n h[s[i][j]] += 1\n end\n h.values.max\nend\nputs gets.split.each_with_index.inject(0) { |s, x| s + x[0].to_i*b[x[1]] }\n"}], "negative_code": [], "src_uid": "2022f53e5a88d5833e133dc3608a122c"} {"nl": {"description": "At the beginning of the new semester there is new schedule in the Berland State University. According to this schedule, n groups have lessons at the room 31. For each group the starting time of the lesson and the finishing time of the lesson are known. It has turned out that it is impossible to hold all lessons, because for some groups periods of their lessons intersect. If at some moment of time one groups finishes it's lesson, and the other group starts the lesson, their lessons don't intersect.The dean wants to cancel the lesson in one group so that no two time periods of lessons of the remaining groups intersect. You are to find all ways to do that.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 5000) — amount of groups, which have lessons in the room 31. Then n lines follow, each of them contains two integers li ri (1 ≤ li < ri ≤ 106) — starting and finishing times of lesson of the i-th group. It is possible that initially no two lessons intersect (see sample 1).", "output_spec": "Output integer k — amount of ways to cancel the lesson in exactly one group so that no two time periods of lessons of the remaining groups intersect. In the second line output k numbers — indexes of groups, where it is possible to cancel the lesson. Groups are numbered starting from 1 in the order that they were given in the input. Output the numbers in increasing order.", "sample_inputs": ["3\n3 10\n20 30\n1 3", "4\n3 10\n20 30\n1 3\n1 39", "3\n1 5\n2 6\n3 7"], "sample_outputs": ["3\n1 2 3", "1\n4", "0"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\na = [[-1e9, -1e9]] + 1.upto(n).map{|i| gets.split.map(&:to_i) << i}.sort + [[1e9, 1e9]]\nb = a.map{1}\n1.upto(n + 1){|i| b[i] = b[i - 1] && a[i - 1][1] <= a[i][0]}\nc = a.map{1}\n(n).downto(0){|i| c[i] = c[i + 1] && a[i][1] <= a[i + 1][0]}\nd = (1 .. n).select{|i| b[i - 1] && c[i + 1] && a[i - 1][1] <= a[i + 1][0]}.map{|i| a[i][2]}\np d.size\nputs d.sort * ' '\n"}, {"source_code": "n = gets.to_i\na = [[-1e9, -1e9]] + 1.upto(n).map{|i| gets.split.map(&:to_i) << i}.sort + [[1e9, 1e9]]\nb = a.map{1}\n1.upto(n + 1){|i| b[i] = b[i - 1] && a[i - 1][1] <= a[i][0]}\nc = a.map{1}\n(n).downto(0){|i| c[i] = c[i + 1] && a[i][1] <= a[i + 1][0]}\nd = (1 .. n).select{|i| b[i - 1] && c[i + 1] && a[i - 1][1] <= a[i + 1][0]}.map{|i| a[i][2]}\np d.size\nputs d.sort * ' '\n\n"}, {"source_code": "=begin\n , \\ / ,\n / \\ )\\__/( / \\\n / \\ (_\\ /_) / \\\n ____/_____\\__\\@ @/___/_____\\____\n | |\\../| |\n | \\VV/ |\n | ------hoi------- |\n |_________________________________|\n | /\\ / \\\\ \\ /\\ |\n | / V )) V \\ |\n |/ ` // ' \\|\n ` V '\n=end\n\nn = gets.to_i\na = [[-1e9, -1e9]] + 1.upto(n).map{|i| gets.split.map(&:to_i) << i}.sort + [[1e9, 1e9]]\nb = a.map{1}\n1.upto(n + 1){|i| b[i] = b[i - 1] && a[i - 1][1] <= a[i][0]}\nc = a.map{1}\n(n).downto(0){|i| c[i] = c[i + 1] && a[i][1] <= a[i + 1][0]}\nd = (1 .. n).select{|i| b[i - 1] && c[i + 1] && a[i - 1][1] <= a[i + 1][0]}.map{|i| a[i][2]}\np d.size\nputs d.sort * ' '"}, {"source_code": "n = gets.to_i\na = [[-1e9, -1e9]] + 1.upto(n).map{|i| gets.split.map(&:to_i) << i}.sort + [[1e9, 1e9]]\nb = a.map{1}\n1.upto(n + 1){|i| b[i] = b[i - 1] && a[i - 1][1] <= a[i][0]}\nc = a.map{1}\n(n).downto(0){|i| c[i] = c[i + 1] && a[i][1] <= a[i + 1][0]}\nd = (1 .. n).select{|i| b[i - 1] && c[i + 1] && a[i - 1][1] <= a[i + 1][0]}.map{|i| a[i][2]}\np d.size\nputs d.sort * ' '\n\n"}, {"source_code": "n = gets.to_i\na = [[-1e9, -1e9]] + 1.upto(n).map{|i| gets.split.map(&:to_i) << i}.sort + [[1e9, 1e9]]\nb = a.map{1}\n1.upto(n + 1){|i| b[i] = b[i - 1] && a[i - 1][1] <= a[i][0]}\nc = a.map{1}\n(n).downto(0){|i| c[i] = c[i + 1] && a[i][1] <= a[i + 1][0]}\nd = (1 .. n).select{|i| b[i - 1] && c[i + 1] && a[i - 1][1] <= a[i + 1][0]}.map{|i| a[i][2]}\np d.size\nputs d.sort * ' '\n\n"}, {"source_code": "n = gets.to_i\na = [[-1e9, -1e9]] + 1.upto(n).map{|i| gets.split.map(&:to_i) << i}.sort + [[1e9, 1e9]]\nb = a.map{1}\n1.upto(n + 1){|i| b[i] = b[i - 1] && a[i - 1][1] <= a[i][0]}\nc = a.map{1}\n(n).downto(0){|i| c[i] = c[i + 1] && a[i][1] <= a[i + 1][0]}\nd = (1 .. n).select{|i| b[i - 1] && c[i + 1] && a[i - 1][1] <= a[i + 1][0]}.map{|i| a[i][2]}\np d.size\nputs d.sort * ' '\n\n"}, {"source_code": "n = gets.to_i\na = [[-1e9, -1e9]] + 1.upto(n).map{|i| gets.split.map(&:to_i) << i}.sort + [[1e9, 1e9]]\nb = a.map{1}\n1.upto(n + 1){|i| b[i] = b[i - 1] && a[i - 1][1] <= a[i][0]}\nc = a.map{1}\n(n).downto(0){|i| c[i] = c[i + 1] && a[i][1] <= a[i + 1][0]}\nd = (1 .. n).select{|i| b[i - 1] && c[i + 1] && a[i - 1][1] <= a[i + 1][0]}.map{|i| a[i][2]}\np d.size\nputs d.sort * ' '"}, {"source_code": "n = gets.to_i\na = [[-1e9, -1e9]] + 1.upto(n).map{|i| gets.split.map(&:to_i) << i}.sort + [[1e9, 1e9]]\nb = a.map{1}\n1.upto(n + 1){|i| b[i] = b[i - 1] && a[i - 1][1] <= a[i][0]}\nc = a.map{1}\n(n).downto(0){|i| c[i] = c[i + 1] && a[i][1] <= a[i + 1][0]}\nd = (1 .. n).select{|i| b[i - 1] && c[i + 1] && a[i - 1][1] <= a[i + 1][0]}.map{|i| a[i][2]}\np d.size\nputs d.sort * ' '\n\n"}, {"source_code": "n = gets.to_i\na = [[-1e9, -1e9]] + 1.upto(n).map{|i| gets.split.map(&:to_i) << i}.sort + [[1e9, 1e9]]\nb = a.map{1}\n1.upto(n + 1){|i| b[i] = b[i - 1] && a[i - 1][1] <= a[i][0]}\nc = a.map{1}\n(n).downto(0){|i| c[i] = c[i + 1] && a[i][1] <= a[i + 1][0]}\nd = (1 .. n).select{|i| b[i - 1] && c[i + 1] && a[i - 1][1] <= a[i + 1][0]}.map{|i| a[i][2]}\np d.size\nputs d.sort * ' '\n\n"}, {"source_code": "n = gets.to_i\na = [[-1e9, -1e9]] + 1.upto(n).map{|i| gets.split.map(&:to_i) << i}.sort + [[1e9, 1e9]]\nb = a.map{1}\n1.upto(n + 1){|i| b[i] = b[i - 1] && a[i - 1][1] <= a[i][0]}\nc = a.map{1}\n(n).downto(0){|i| c[i] = c[i + 1] && a[i][1] <= a[i + 1][0]}\nd = (1 .. n).select{|i| b[i - 1] && c[i + 1] && a[i - 1][1] <= a[i + 1][0]}.map{|i| a[i][2]}\np d.size\nputs d.sort * ' '\n\n"}, {"source_code": "n = gets.to_i\na = [[-1e9, -1e9]] + 1.upto(n).map{|i| gets.split.map(&:to_i) << i}.sort + [[1e9, 1e9]]\nb = a.map{1}\n1.upto(n + 1){|i| b[i] = b[i - 1] && a[i - 1][1] <= a[i][0]}\nc = a.map{1}\n(n).downto(0){|i| c[i] = c[i + 1] && a[i][1] <= a[i + 1][0]}\nd = (1 .. n).select{|i| b[i - 1] && c[i + 1] && a[i - 1][1] <= a[i + 1][0]}.map{|i| a[i][2]}\np d.size\nputs d.sort * ' '\n\n"}], "negative_code": [], "src_uid": "df28bb63a627a41d67f02cbd927d5e9a"} {"nl": {"description": "In Berland, there is the national holiday coming — the Flag Day. In the honor of this event the president of the country decided to make a big dance party and asked your agency to organize it. He has several conditions: overall, there must be m dances; exactly three people must take part in each dance; each dance must have one dancer in white clothes, one dancer in red clothes and one dancer in blue clothes (these are the colors of the national flag of Berland). The agency has n dancers, and their number can be less than 3m. That is, some dancers will probably have to dance in more than one dance. All of your dancers must dance on the party. However, if some dance has two or more dancers from a previous dance, then the current dance stops being spectacular. Your agency cannot allow that to happen, so each dance has at most one dancer who has danced in some previous dance. You considered all the criteria and made the plan for the m dances: each dance had three dancers participating in it. Your task is to determine the clothes color for each of the n dancers so that the President's third condition fulfilled: each dance must have a dancer in white, a dancer in red and a dancer in blue. The dancers cannot change clothes between the dances.", "input_spec": "The first line contains two space-separated integers n (3 ≤ n ≤ 105) and m (1 ≤ m ≤ 105) — the number of dancers and the number of dances, correspondingly. Then m lines follow, describing the dances in the order of dancing them. The i-th line contains three distinct integers — the numbers of the dancers that take part in the i-th dance. The dancers are numbered from 1 to n. Each dancer takes part in at least one dance.", "output_spec": "Print n space-separated integers: the i-th number must represent the color of the i-th dancer's clothes (1 for white, 2 for red, 3 for blue). If there are multiple valid solutions, print any of them. It is guaranteed that at least one solution exists.", "sample_inputs": ["7 3\n1 2 3\n1 4 5\n4 6 7", "9 3\n3 6 9\n2 5 8\n1 4 7", "5 2\n4 1 5\n3 1 2"], "sample_outputs": ["1 2 3 3 2 2 1", "1 1 1 2 2 2 3 3 3", "2 3 1 1 3"], "notes": null}, "positive_code": [{"source_code": "n, m = gets.split.map(&:to_i)\ncolor = Array.new(n+1, 0)\nfor i in 0...m\n a = gets.split.map(&:to_i)\n mark = Array.new(4, false)\n mark[color[a[0]]] = true\n mark[color[a[1]]] = true\n mark[color[a[2]]] = true\n if !mark[1] then\n for j in 0..2\n if color[a[j]] == 0 then\n\tcolor[a[j]] = 1\n\tbreak\n end\n end\n end\n if !mark[2] then\n for j in 0..2\n if color[a[j]] == 0 then\n\tcolor[a[j]] = 2\n\tbreak\n end\n end\n end\n if !mark[3] then\n for j in 0..2\n if color[a[j]] == 0 then\n\tcolor[a[j]] = 3\n\tbreak\n end\n end\n end\nend\n\nfor i in 1..n\n print color[i], \" \"\nend\n\n"}, {"source_code": "m,d = gets.split.map{|s| s.to_i}\nmen = Array::new(m+1,0)\nd.times {\n\tcd = gets.split.map{|s| s.to_i}\n\tfixed = 1\n\tcd.each {|i| \n\t\tfixed = men[i] if men[i] > 0 # only one men participating in the dance may have color associated previously\n\t}\n\tidx = fixed\n\tcd.each {|i|\n\t\tif men[i] != fixed\n\t\t\tmen[i] = idx%3 + 1\n\t\t\tidx += 1\n\t\tend\t\n\t}\n}\nprint men[1..-1].join(\" \")"}, {"source_code": "ar=Array.new(100100,0)\nn,m=gets.chomp.split.map(&:to_i)\nm.times do\n\ta=gets.chomp.split.map(&:to_i)\n\tb=[0,0,0,0]\n\tb[ar[a[0]]],b[ar[a[1]]],b[ar[a[2]]]=1,1,1\n\t3.times do |i|\n\t\tfor j in 1...4\n\t\t\tif b[j]==0 && ar[a[i]]==0\n\t\t\t\tar[a[i]]=j\n\t\t\t\tb[j]=1\n\t\t\tend\n\t\tend\n\tend\nend\nfor i in 1..n\n\tprint \"#{ar[i]} \"\nend\nputs"}, {"source_code": "n,m = gets.split(\" \").map(&:to_i)\n\n\nassignment = {}\n\nm.times do\n\tarr = gets.split(\" \").map(&:to_i)\n\tcolors = [3,2,1]\n\tintersec = arr.detect{|a| assignment[a]}\n\tif intersec\n\t\tarr = arr - [intersec]\n\t\tcolors -= [assignment[intersec]]\n\t\t# puts \"colors #{intersec}\"\n\tend\n\tarr.each do |a|\t\t\n\t\tassignment[a] = colors.pop\t\t\n\tend\nend\nans = []\nassignment.keys.sort.each do |k|\n\tans << assignment[k]\t\nend\nputs ans.join(\" \")"}, {"source_code": "inp = gets.split.map(&:to_i)\nd = inp[1].times.map{gets.split.map(&:to_i)}\nans = Array.new(inp[0])\nd.each do |x|\n\tif ans[x[0]]==1 && ans[x[1]]==2\n\t\tans[x[2]] = 3\n\telsif ans[x[0]]==1 && ans[x[1]]==3\n\t\tans[x[2]] = 2\n\telsif ans[x[0]]==2 && ans[x[1]]==3\n\t\tans[x[2]] = 1\n\telsif ans[x[0]]==1 && ans[x[2]]==2\n\t\tans[x[1]] = 3\n\telsif ans[x[0]]==1 && ans[x[2]]==3\n\t\tans[x[1]] = 2\n\telsif ans[x[0]]==2 && ans[x[2]]==3\n\t\tans[x[1]] = 1\n\telsif ans[x[1]]==1 && ans[x[2]]==2\n\t\tans[x[0]] = 3\n\telsif ans[x[1]]==1 && ans[x[2]]==3\n\t\tans[x[0]] = 2\n\telsif ans[x[1]]==2 && ans[x[2]]==3\n\t\tans[x[0]] = 1\n\telsif ans[x[0]] == 1\n\t\tans[x[1]] = 2\n\t\tans[x[2]] = 3\n\telsif ans[x[0]] == 2\n\t\tans[x[1]] = 1\n\t\tans[x[2]] = 3\n\telsif ans[x[0]] == 3\n\t\tans[x[1]] = 1\n\t\tans[x[2]] = 2\n\telsif ans[x[1]] == 1\n\t\tans[x[0]] = 2\n\t\tans[x[2]] = 3\n\telsif ans[x[1]] == 2\n\t\tans[x[0]] = 1\n\t\tans[x[2]] = 3\n\telsif ans[x[1]] == 3\n\t\tans[x[0]] = 1\n\t\tans[x[2]] = 2\n\telsif ans[x[2]] == 1\n\t\tans[x[0]] = 2\n\t\tans[x[1]] = 3\n\telsif ans[x[2]] == 2\n\t\tans[x[0]] = 1\n\t\tans[x[1]] = 3\n\telsif ans[x[1]] == 3\n\t\tans[x[0]] = 1\n\t\tans[x[1]] = 2\n\telse\n\t\tans[x[0]] = 1\n\t\tans[x[1]] = 2\n\t\tans[x[2]] = 3\n\tend\nend\n\n(inp[0]+1).times do |i|\n\tif i>0\n\t\tprint ans[i]\n\t\tprint \" \"\n\tend\nend"}, {"source_code": "def solution(n, dancers)\n input = {}\n value = 0\n\n dancers.each do |i|\n color = [1, 2, 3]\n value = 0\n\n i.each do |j|\n if input.has_key?(j)\n color.delete(input[j])\n end\n end\n\n i.each_with_index do |j, k|\n value += 1 if input.has_key?(j)\n input[j] = color[k-value] unless input.has_key?(j)\n end\n end\n\n output = []\n n.times { |i| output << input[i+1] }\n\n output.join(\" \") \nend\n\n\nn, m = STDIN.readline.split(\" \").map(&:to_i)\ndancers = []\nm.times do \n dancers << STDIN.readline.split(\" \").map(&:to_i)\nend\n\nvalues = solution(n,dancers)\nputs values\n\n\n"}, {"source_code": "#! /usr/bin/env ruby\n\nWHITE = 4\nRED = 2\nBLUE = 1\n\ndef convert(calc_num)\n case calc_num\n when WHITE\n 1\n when RED\n 2\n when BLUE\n 3\n end\nend\n\nn, m = STDIN.gets.split(' ').map(&:to_i)\ndances = []\nm.times {\n dances << STDIN.gets.split(' ').map(&:to_i)\n}\n\n\ncolors = Array.new(n){0}\n\ndef fill(colors, dancers, color)\n dancers.each do |dancer|\n if colors[dancer.to_i - 1].zero?\n colors[dancer.to_i - 1] = color\n break\n end\n end\n colors\nend\n\ndances.each do |dancers|\n result = dancers.inject(0) {|s, i|\n s += colors[i - 1]\n }\n next if result == 7\n\n if result < WHITE\n colors = fill(colors, dancers, WHITE)\n else\n result -= WHITE\n end\n\n if result < RED\n colors = fill(colors, dancers, RED)\n else\n result -= RED\n end\n\n if result < BLUE\n colors = fill(colors, dancers, BLUE)\n end\nend\n\nanswer = colors.map do |color|\n convert(color)\nend.join(' ')\nputs answer\n\n"}], "negative_code": [{"source_code": "n,m = gets.split(\" \").map(&:to_i)\n\n\nassignment = {}\n\nm.times do\n\tarr = gets.split(\" \").map(&:to_i)\n\tcolors = [3,2,1]\n\tintersec = arr.detect{|a| assignment[a]}\n\tif !intersec\n\t\tarr = arr - [intersec]\n\t\tcolors -= [assignment[intersec]]\n\t\t# puts \"colors #{intersec}\"\n\tend\n\tarr.each do |a|\t\t\n\t\tassignment[a] = colors.pop\t\t\n\tend\nend\nans = []\nassignment.keys.sort.each do |k|\n\tans << assignment[k]\t\nend\nputs ans.join(\" \")"}, {"source_code": "n,m = gets.split(\" \").map(&:to_i)\n\n\nassignment = {}\n\nm.times do\n\tarr = gets.split(\" \").map(&:to_i)\n\tcolors = [3,2,1]\n\tintersec = arr & assignment.keys\n\tif !intersec.empty?\n\t\tarr = arr - assignment.keys \n\t\tcolors -= [assignment[intersec[0]]]\n\t\t# puts \"colors #{intersec}\"\n\tend\n\tarr.each do |a|\t\t\n\t\tassignment[a] = colors.pop\t\t\n\tend\nend\nputs assignment.values.join(\" \")"}, {"source_code": "def solution(n, dancers)\n input = {}\n value = 0\n\n dancers.each do |i|\n color = [1, 2, 3]\n value = 0\n i.each_with_index do |j, k|\n if input.has_key?(j)\n color.delete(input[j])\n value += 1\n else\n input[j] = color[k-value]\n end\n end\n end\n\n input.values.join(\" \") \nend\n\n\nn, m = STDIN.readline.split(\" \").map(&:to_i)\ndancers = []\nm.times do \n dancers << STDIN.readline.split(\" \").map(&:to_i)\nend\n\nvalues = solution(n,dancers)\nputs values\n"}, {"source_code": "def solution(n, dancers)\n input = {}\n value = 0\n\n dancers.each do |i|\n color = [1, 2, 3]\n value = 0\n i. each do |j| \n if input.has_key?(j)\n color.delete(input[j])\n value += 1\n end\n end\n\n i.each_with_index do |j, k|\n input[j] = color[k-value]\n end\n end\n\n output = []\n n.times { |i| output << input[i+1] }\n\n output.join(\" \") \nend\n\n\nn, m = STDIN.readline.split(\" \").map(&:to_i)\ndancers = []\nm.times do \n dancers << STDIN.readline.split(\" \").map(&:to_i)\nend\n\nvalues = solution(n,dancers)\nputs values\n\n\n"}, {"source_code": "def solution(n, dancers)\n input = {}\n value = 0\n\n dancers.each do |i|\n color = [1, 2, 3]\n value = 0\n i. each do |j| \n if input.has_key?(j)\n color.delete(input[j])\n value += 1\n end\n end\n\n i.each_with_index do |j, k|\n input[j] = color[k]\n end\n end\n\n output = []\n n.times { |i| output << input[i+1] }\n\n output.join(\" \") \nend\n\n\nn, m = STDIN.readline.split(\" \").map(&:to_i)\ndancers = []\nm.times do \n dancers << STDIN.readline.split(\" \").map(&:to_i)\nend\n\nvalues = solution(n,dancers)\nputs values\n\n\n"}, {"source_code": "def solution(n, dancers)\n input = {}\n value = 0\n\n dancers.each do |i|\n color = [1, 2, 3]\n value = 0\n i.each_with_index do |j, k|\n if input.has_key?(j)\n color.delete(input[j])\n value += 1\n else\n input[j] = color[k-value]\n end\n end\n end\n\n output = []\n n.times { |i| output << input[i+1] }\n\n output.join(\" \") \nend\n\n\nn, m = STDIN.readline.split(\" \").map(&:to_i)\ndancers = []\nm.times do \n dancers << STDIN.readline.split(\" \").map(&:to_i)\nend\n\nvalues = solution(n,dancers)\nputs values\n"}], "src_uid": "ee523bb4da5cb794e05fb62b7da8bb89"} {"nl": {"description": "Let's denote correct match equation (we will denote it as CME) an equation $$$a + b = c$$$ there all integers $$$a$$$, $$$b$$$ and $$$c$$$ are greater than zero.For example, equations $$$2 + 2 = 4$$$ (||+||=||||) and $$$1 + 2 = 3$$$ (|+||=|||) are CME but equations $$$1 + 2 = 4$$$ (|+||=||||), $$$2 + 2 = 3$$$ (||+||=|||), and $$$0 + 1 = 1$$$ (+|=|) are not.Now, you have $$$n$$$ matches. You want to assemble a CME using all your matches. Unfortunately, it is possible that you can't assemble the CME using all matches. But you can buy some extra matches and then assemble CME!For example, if $$$n = 2$$$, you can buy two matches and assemble |+|=||, and if $$$n = 5$$$ you can buy one match and assemble ||+|=|||. Calculate the minimum number of matches which you have to buy for assembling CME.Note, that you have to answer $$$q$$$ independent queries.", "input_spec": "The first line contains one integer $$$q$$$ ($$$1 \\le q \\le 100$$$) — the number of queries. The only line of each query contains one integer $$$n$$$ ($$$2 \\le n \\le 10^9$$$) — the number of matches.", "output_spec": "For each test case print one integer in single line — the minimum number of matches which you have to buy for assembling CME. ", "sample_inputs": ["4\n2\n5\n8\n11"], "sample_outputs": ["2\n1\n0\n1"], "notes": "NoteThe first and second queries are explained in the statement.In the third query, you can assemble $$$1 + 3 = 4$$$ (|+|||=||||) without buying matches.In the fourth query, buy one match and assemble $$$2 + 4 = 6$$$ (||+||||=||||||)."}, "positive_code": [{"source_code": "gets.to_i.times do\n n = gets.to_i\n puts n == 2 ? 2 : n % 2\nend"}, {"source_code": "#!usr/bin/env ruby\n\n# https://codeforces.com/problemset/problem/1223/A\n\ncount = gets.to_i\nanswers = []\n\ncount.times do\n match_count = gets.to_i\n answers << if match_count == 2\n 2\n else\n match_count % 2\n end\nend\n\nanswers.each { |answer| puts answer }\n"}, {"source_code": "q = gets.to_i\nq.times do\n n = gets.to_i\n if n == 1\n puts 3\n elsif n == 2\n puts 2\n else\n a = (n.to_f/2).ceil\n b = n - a\n puts (a-b).abs\n end\nend\n"}, {"source_code": "n = gets.chomp.to_i\nn.times do \n\tngo = gets.chomp.to_i\n\tif ngo <= 3\n\t\tputs 4 - ngo\n\telsif ngo.even?\n\t\tputs 0\n\telse\n\t puts 1\n\tend\nend\n\n\n\n"}, {"source_code": "q = gets.to_i\nq.times do\n n = gets.to_i\n puts n < 4 ? 4 - n : n%2\nend\n"}, {"source_code": "1.upto(gets.to_i) do\n n = gets.to_i\n\n case n\n when 2..4\n puts 4 - n\n when proc(&:odd?)\n puts 1\n else\n puts 0\n end\nend\n"}], "negative_code": [], "src_uid": "178876bfe161ba9ccfd80c9310f75cbc"} {"nl": {"description": "You are a rebel leader and you are planning to start a revolution in your country. But the evil Government found out about your plans and set your punishment in the form of correctional labor.You must paint a fence which consists of $$$10^{100}$$$ planks in two colors in the following way (suppose planks are numbered from left to right from $$$0$$$): if the index of the plank is divisible by $$$r$$$ (such planks have indices $$$0$$$, $$$r$$$, $$$2r$$$ and so on) then you must paint it red; if the index of the plank is divisible by $$$b$$$ (such planks have indices $$$0$$$, $$$b$$$, $$$2b$$$ and so on) then you must paint it blue; if the index is divisible both by $$$r$$$ and $$$b$$$ you can choose the color to paint the plank; otherwise, you don't need to paint the plank at all (and it is forbidden to spent paint on it). Furthermore, the Government added one additional restriction to make your punishment worse. Let's list all painted planks of the fence in ascending order: if there are $$$k$$$ consecutive planks with the same color in this list, then the Government will state that you failed the labor and execute you immediately. If you don't paint the fence according to the four aforementioned conditions, you will also be executed.The question is: will you be able to accomplish the labor (the time is not important) or the execution is unavoidable and you need to escape at all costs.", "input_spec": "The first line contains single integer $$$T$$$ ($$$1 \\le T \\le 1000$$$) — the number of test cases. The next $$$T$$$ lines contain descriptions of test cases — one per line. Each test case contains three integers $$$r$$$, $$$b$$$, $$$k$$$ ($$$1 \\le r, b \\le 10^9$$$, $$$2 \\le k \\le 10^9$$$) — the corresponding coefficients.", "output_spec": "Print $$$T$$$ words — one per line. For each test case print REBEL (case insensitive) if the execution is unavoidable or OBEY (case insensitive) otherwise.", "sample_inputs": ["4\n1 1 2\n2 10 4\n5 2 3\n3 2 2"], "sample_outputs": ["OBEY\nREBEL\nOBEY\nOBEY"], "notes": null}, "positive_code": [{"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\nrequire 'prime'\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\ndef big(a,b) return (a>b)? a:b end\n\ndef small(a,b) return (a=k)\n puts \"REBEL\"\n next\n else\n puts \"OBEY\"\n end\nend"}], "negative_code": [{"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\nrequire 'prime'\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\ndef big(a,b) return (a>b)? a:b end\n\ndef small(a,b) return (ak or r_up(b,a)>k)\n puts \"REBEL\"\n else\n puts \"OBEY\"\n end\nend"}, {"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\nrequire 'prime'\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\ndef big(a,b) return (a>b)? a:b end\n\ndef small(a,b) return (ak or r_up(b,a)>k)\n puts \"REBEL\"\n next\n else\n puts \"OBEY\"\n end\nend"}, {"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\nrequire 'prime'\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\ndef big(a,b) return (a>b)? a:b end\n\ndef small(a,b) return (a=k)\n puts \"REBEL\"\n next\n else\n puts \"OBEY\"\n end\nend"}, {"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\nrequire 'prime'\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\ndef big(a,b) return (a>b)? a:b end\n\ndef small(a,b) return (a=(k) or k == 1)\n puts \"REBEL\"\n next\n else\n puts \"OBEY\"\n end\nend"}], "src_uid": "be141f316d6e5d9d8f09192913f4be47"} {"nl": {"description": "There are $$$n$$$ boxes with different quantities of candies in each of them. The $$$i$$$-th box has $$$a_i$$$ candies inside.You also have $$$n$$$ friends that you want to give the candies to, so you decided to give each friend a box of candies. But, you don't want any friends to get upset so you decided to eat some (possibly none) candies from each box so that all boxes have the same quantity of candies in them. Note that you may eat a different number of candies from different boxes and you cannot add candies to any of the boxes.What's the minimum total number of candies you have to eat to satisfy the requirements?", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\leq t \\leq 1000$$$) — the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$1 \\leq n \\leq 50$$$) — the number of boxes you have. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\leq a_i \\leq 10^7$$$) — the quantity of candies in each box.", "output_spec": "For each test case, print a single integer denoting the minimum number of candies you have to eat to satisfy the requirements.", "sample_inputs": ["5\n\n5\n\n1 2 3 4 5\n\n6\n\n1000 1000 5 1000 1000 1000\n\n10\n\n1 2 3 5 1 2 7 9 13 5\n\n3\n\n8 8 8\n\n1\n\n10000000"], "sample_outputs": ["10\n4975\n38\n0\n0"], "notes": "NoteFor the first test case, you can eat $$$1$$$ candy from the second box, $$$2$$$ candies from the third box, $$$3$$$ candies from the fourth box and $$$4$$$ candies from the fifth box. Now the boxes have $$$[1, 1, 1, 1, 1]$$$ candies in them and you ate $$$0 + 1 + 2 + 3 + 4 = 10$$$ candies in total so the answer is $$$10$$$.For the second test case, the best answer is obtained by making all boxes contain $$$5$$$ candies in them, thus eating $$$995 + 995 + 0 + 995 + 995 + 995 = 4975$$$ candies in total."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n gets\r\n xs = gets.split.map(&:to_i)\r\n min = xs.min\r\n p xs.map { |x| x - min }.sum\r\nend\r\n"}, {"source_code": "t = Integer(gets)\r\n\r\nfor i in 0..t-1\r\n n = Integer(gets)\r\n arr = Array.new\r\n a = gets\r\n arr = a.split(\" \")\r\n \r\n for j in 0..n-1\r\n arr[j] = Integer(arr[j]);\r\n end\r\n \r\n mn = arr.min\r\n ans = 0\r\n for j in 0..n-1\r\n ans += (arr[j] - mn)\r\n end\r\n puts ans\r\nend"}, {"source_code": "gets.to_i.times do\n gets\n as = gets.split.map(&:to_i)\n m = as.min\n puts as.map { _1 - m }.sum\nend\n"}, {"source_code": "def main\n gets.to_i.times do\n n = gi\n a = gli.sort\n puts a.sum - a[0] * n\n end\nend\n\nMOD = 10**9 + 7\ndef gs = gets.chomp\ndef gi = gets.to_i\ndef gli = gets.split.map(&:to_i)\ndef cyesno(x) = x ? :Yes : :No\ndef uyesno(x) = x ? :YES : :NO\n\nmain\n"}, {"source_code": "main = -> {\r\n T = int\r\n T.times do\r\n n = int\r\n a = ints\r\n m = a.min\r\n puts a.sum { |x| x - m }\r\n end\r\n}\r\n\r\nDEBUG = true\r\nMOD = 10**9+7\r\n$yesno = %w(No Yes)\r\nYesNo = %w(No Yes)\r\nYESNO = %w(NO YES)\r\nINF = 10**18\r\n\r\nrequire \"set\"\r\nrequire \"prime\"\r\n\r\ndef int; gets.to_s.to_i end\r\ndef ints; gets.to_s.split.map { |s| s.to_i } end\r\ndef int1s; gets.to_s.split.map { |s| s.to_i - 1 } end\r\ndef float; gets.to_s.to_f end\r\ndef floats; gets.to_s.split.map { |s| s.to_f } end\r\ndef array_of(&convert); gets.to_s.split.map(&convert) end\r\ndef string; gets.to_s.chomp end\r\ndef strings; gets.to_s.split end\r\ndef rep(n, &b); Array.new(n, &b) end\r\ndef yes; puts $yesno[1] end\r\ndef no; puts $yesno[0] end\r\ndef yesno t; puts $yesno[t] end\r\ndef YesNo t; puts YesNo[t] end\r\ndef YESNO t; puts YESNO[t] end\r\ndef zip(xs, *yss); Enumerator.new { |y| xs.zip(*yss) { |a| y.yield(*a) } } end\r\ndef max(*xs, &block); block_given? ? xs.max_by(&block) : xs.max end\r\ndef min(*xs, &block); block_given? ? xs.min_by(&block) : xs.min end\r\ndef minmax(*xs, &block); block_given? ? xs.minmax_by(&block) : xs.minmax end\r\ndef gcd(*xs); xs.inject(0, :gcd) end\r\ndef matrix(h, w, fill=nil, &block); return Array.new(h) { Array.new(w, &block) } if block_given?; Array.new(h) { [fill] * w } end\r\ndef debug(x = nil); STDERR.puts (block_given? ? yield(x) : x).inspect if DEBUG; x end\r\ndef debug_grid(grid, width = 1); grid.each { |row| STDERR.puts row.map { |x| x.inspect.ljust(width) }.join(\"\") } if DEBUG; grid end\r\ndef if_debug; yield if DEBUG end\r\n\r\nmodule Boolean\r\n def coerce(other); [other, to_i] end\r\n def +@; to_i end\r\n def to_int; to_i end\r\n def *(other); to_i * other end\r\nend\r\n\r\nclass TrueClass\r\n include Boolean\r\n def to_i; 1 end\r\nend\r\n\r\nclass FalseClass\r\n include Boolean\r\n def to_i; 0 end\r\nend\r\n\r\nclass Integer\r\n def div_ceil(y); (self + y - 1) / y end\r\n def mod_inv(mod = MOD); pow(mod-2, mod) end\r\n def mod_div(y, mod = MOD); self * y.mod_inv(mod) % mod end\r\n def mod_nCr(r, mod = MOD); x = y = 1; (1..r).each { |i| x = x * (self + 1 - i) % mod; y = y * i % mod }; x.mod_div(y, mod); end\r\n def factorial(mod = MOD); (2..self).inject(1) { |f, x| f * x % mod } end\r\n def popcount; x = self; c = 0; while x > 0; c += 1 if x & 1 == 1; x >>= 1 end; c end #TODO: faster\r\n def bitbrute(&block); (1< 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\r\n def lower_bound; ac, wa = self.end_open, self.begin; while ac - wa > 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\r\n def shakutori(r2, &pred); Enumerator.new { |y| j, r = r2.begin, r2.end_open; each { |i| j += 1 while j + 1 < r and pred[i, j+1]; y.yield(i, j) } }; end\r\n def widest(&block); Enumerator.new { |y| j, n = self.begin, self.end_open; each { |i| j += 1 while j < n and block[i, j]; y.yield(i, j) if block[i, j] } } end\r\nend\r\n\r\nclass Array\r\n def power(&block); (0 ... 1 << size).each(&block) end\r\n def sorted_merge(other); a = []; i = j = 0; n, m = size, other.size; if j < m and other[j] < self[i]; a << other[j]; j += 1 else; a << self[i]; i += 1 end while i < n; a.push(*other[j..-1]) if j < m; a end\r\n def upper_bound; ac, wa = 0, size; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\r\n def lower_bound; ac, wa = size, 0; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\r\n def cum(*xs, &op); a = []; a << xs[0] if xs.size > 0; a << x = self[0]; (1...size).each { |i| a << x = op[x, self[i]] }; a end\r\n def cumdiff(range); self[range.end_open] - self[range.begin]; end\r\n def compress(kinds = uniq.sort!); map { |x| kinds.bsearch_index { _1 >= x } }; end\r\nend\r\n\r\nmodule Enumerable\r\n def sorted_uniq; x = nil; filter { |y| c = x === y; x = y; !c } end\r\n def cumsum; ys = [0]; each { |x| ys << x + ys[-1] }; ys end\r\nend\r\n\r\nmain[]"}], "negative_code": [], "src_uid": "20dd260775ea71b1fb5b42bcac90a6f2"} {"nl": {"description": "Tyndex is again well ahead of the rivals! The reaction to the release of Zoozle Chrome browser was the release of a new browser Tyndex.Brome!The popularity of the new browser is growing daily. And the secret is not even the Tyndex.Bar installed (the Tyndex.Bar automatically fills the glass with the finest 1664 cognac after you buy Tyndex.Bottles and insert in into a USB port). It is highly popular due to the well-thought interaction with the user.Let us take, for example, the system of automatic address correction. Have you entered codehorses instead of codeforces? The gloomy Zoozle Chrome will sadly say that the address does not exist. Tyndex.Brome at the same time will automatically find the closest address and sent you there. That's brilliant!How does this splendid function work? That's simple! For each potential address a function of the F error is calculated by the following rules: for every letter ci from the potential address c the closest position j of the letter ci in the address (s) entered by the user is found. The absolute difference |i - j| of these positions is added to F. So for every i (1 ≤ i ≤ |c|) the position j is chosen such, that ci = sj, and |i - j| is minimal possible. if no such letter ci exists in the address entered by the user, then the length of the potential address |c| is added to F. After the values of the error function have been calculated for all the potential addresses the most suitable one is found. To understand the special features of the above described method better, it is recommended to realize the algorithm of calculating the F function for an address given by the user and some set of potential addresses. Good luck!", "input_spec": "The first line contains two integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ 105). They are the number of potential addresses and the length of the address entered by the user. The next line contains k lowercase Latin letters. They are the address entered by the user (s). Each next i-th (1 ≤ i ≤ n) line contains a non-empty sequence of lowercase Latin letters. They are the potential address. It is guaranteed that the total length of all the lines does not exceed 2·105.", "output_spec": "On each n line of the output file print a single number: the value of the error function when the current potential address is chosen. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).", "sample_inputs": ["2 10\ncodeforces\ncodeforces\ncodehorses", "9 9\nvkontakte\nvcontacte\nvkontrakte\nvkollapse\nvkrokodile\nvtopke\nvkapuste\nvpechke\nvk\nvcodeforcese"], "sample_outputs": ["0\n12", "18\n14\n36\n47\n14\n29\n30\n0\n84"], "notes": null}, "positive_code": [{"source_code": "def search_min(a, i)\n x0 = 0\n x1 = a.size - 1\n\n return 0 if a[x0] == i || a[x1] == i\n \n while x1-x0 > 1\n x2 = (x0+x1)/2\n if a[x2] == i\n return 0 \n elsif a[x2] > i\n x1 = x2\n else\n x0 = x2\n end\n end\n\n [(a[x0]-i).abs, (a[x1]-i).abs].min\nend\n\nn, k = gets.split.map(&:to_i)\nbase = gets.chomp\ntbl = {}\nbase.each_char.with_index{|c, i| (tbl[c]||=[]) << i}\n\nwhile l = gets\n l.chomp!\n s = 0\n l.each_char.with_index do |c, i|\n if !tbl[c]\n s += l.size\n else\n s += search_min(tbl[c], i)\n end\n end\n puts s\nend\n"}], "negative_code": [], "src_uid": "a587188db6f0c17927923a158fdca1be"} {"nl": {"description": "The only difference between the easy and the hard versions is the maximum value of $$$k$$$.You are given an infinite sequence of form \"112123123412345$$$\\dots$$$\" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $$$1$$$ to $$$1$$$, the second one — from $$$1$$$ to $$$2$$$, the third one — from $$$1$$$ to $$$3$$$, $$$\\dots$$$, the $$$i$$$-th block consists of all numbers from $$$1$$$ to $$$i$$$. So the first $$$56$$$ elements of the sequence are \"11212312341234512345612345671234567812345678912345678910\". Elements of the sequence are numbered from one. For example, the $$$1$$$-st element of the sequence is $$$1$$$, the $$$3$$$-rd element of the sequence is $$$2$$$, the $$$20$$$-th element of the sequence is $$$5$$$, the $$$38$$$-th element is $$$2$$$, the $$$56$$$-th element of the sequence is $$$0$$$.Your task is to answer $$$q$$$ independent queries. In the $$$i$$$-th query you are given one integer $$$k_i$$$. Calculate the digit at the position $$$k_i$$$ of the sequence.", "input_spec": "The first line of the input contains one integer $$$q$$$ ($$$1 \\le q \\le 500$$$) — the number of queries. The $$$i$$$-th of the following $$$q$$$ lines contains one integer $$$k_i$$$ $$$(1 \\le k_i \\le 10^9)$$$ — the description of the corresponding query.", "output_spec": "Print $$$q$$$ lines. In the $$$i$$$-th line print one digit $$$x_i$$$ $$$(0 \\le x_i \\le 9)$$$ — the answer to the query $$$i$$$, i.e. $$$x_i$$$ should be equal to the element at the position $$$k_i$$$ of the sequence.", "sample_inputs": ["5\n1\n3\n20\n38\n56", "4\n2132\n506\n999999999\n1000000000"], "sample_outputs": ["1\n2\n5\n2\n0", "8\n2\n9\n8"], "notes": "NoteAnswers on queries from the first example are described in the problem statement."}, "positive_code": [{"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n \ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n \ndef inps() a=gets.chomp.split(\" \")end\n \ndef copy(a) Marshal.load(Marshal.dump(a)) end\n \ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n \ndef na(n,d=0) Array.new(n,d)end\n \ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n \ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n \ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n \ndef r_up(a, b) (a+b-1)/b end\n \ndef sum(a) a.inject(:+) end\n \nn = inp[0]\nn.times do\n d = inp[0]\n keta = 0\n ld = d\n li = 0\n 100000.times do |i|\n ld = d\n keta += (i+1).to_s.size\n d -= keta\n li = i\n break if(d <= 0)\n end\n pta = keta\n keta = 0\n d = ld\n while 0 < d\n ld = d\n keta += 1\n d -= (9*(10**(keta-1)))*keta\n end\n ld -= 1\n no = ld % keta\n ka = ld/keta\n puts (ka+10**(keta-1)).to_s[no]\nend"}, {"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\nn = inp[0]\nn.times do\n d = inp[0]\n keta = 0\n ld = d\n li = 0\n 100000.times do |i|\n ld = d\n keta += (i+1).to_s.size\n d -= keta\n li = i\n break if(d <= 0)\n end\n pta = keta\n keta = 0\n d = ld\n while 0 < d\n ld = d\n keta += 1\n d -= (9*(10**(keta-1)))*keta\n end\n ld -= 1\n no = ld % keta\n ka = ld/keta\n puts (ka+10**(keta-1)).to_s[no]\nend"}], "negative_code": [{"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\nn = inp[0]\nn.times do\n d = inp[0]\n keta = 0\n ld = d\n li = 0\n 100000.times do |i|\n ld = d\n keta += (i+1).to_s.size\n d -= keta\n li = i\n break if(d <= 0)\n end\n pta = keta\n keta = 0\n d = ld\n while 0 <= d\n ld = d\n keta += 1\n d -= (9*(10**(keta-1)))*keta\n end\n ld -= 1\n no = ld % keta\n ka = ld/keta\n puts (ka+10**(keta-1)).to_s[no]\nend"}], "src_uid": "7e03c9e316f36c1d9487286237e24c6f"} {"nl": {"description": "Phone number in Berland is a sequence of n digits. Often, to make it easier to memorize the number, it is divided into groups of two or three digits. For example, the phone number 1198733 is easier to remember as 11-987-33. Your task is to find for a given phone number any of its divisions into groups of two or three digits.", "input_spec": "The first line contains integer n (2 ≤ n ≤ 100) — amount of digits in the phone number. The second line contains n digits — the phone number to divide into groups.", "output_spec": "Output any of divisions of the given phone number into groups of two or three digits. Separate groups by single character -. If the answer is not unique, output any.", "sample_inputs": ["6\n549871", "7\n1198733"], "sample_outputs": ["54-98-71", "11-987-33"], "notes": null}, "positive_code": [{"source_code": "d=gets.to_i\nw=gets\nif d%2==0\n\tputs w.scan(/../)*'-'\nelse\n\tputs [w[0,3],*w[3..-1].scan(/../)]*'-'\nend"}, {"source_code": "n = gets.chomp.to_i\ns = gets.chomp\nfirst = true\nif n % 2 == 0\n\twhile s.length > 0\n\t\tprint \"-\" if not first\n\t\tprint s.slice!(0,2)\n\t\tfirst = false\n\tend\nelse\n\tlen = 3\n\twhile s.length > 0\n\t\tprint \"-\" if not first\n\t\tprint s.slice!(0,len)\n\t\tfirst = false\n\t\tlen = 2\n\tend\nend\nputs\n"}, {"source_code": "#!/usr/bin/ruby\ngets;s=gets.chomp\nif s.size%3==1\n\tt=s[-4..-1]\n\tx=s[0..-5].chars.each_slice(3).map{|e|e*''}*'-'\n\tputs (x.size>0 ? x+'-' : '')+t[0,2]+'-'+t[2,2]\nelse\n\tputs s.chars.each_slice(3).map{|e|e*''}*'-'\nend"}, {"source_code": "gets\ns = gets.chop\nprint s.slice!(0..1)+'-' if s.size % 3 == 1\ns.gsub!(/.../){$&+'-'}\ns.chop! if s =~ /-$/\nputs s\n"}, {"source_code": "\nn=gets.to_i\nm=gets.split[0]\ni=0\ns=0\nif n%3==1 then\n print m[0..1]\n print \"-\"\n print m[2..3]\n s=1\n i=4\nelsif n%3==2 then\n print m[0..1]\n s=1\n i=2\nend\nwhile i0 then\n print \"-\"\n end\n s=1\n print m[i..(i+2)]\n i+=3\nend\nputs\n\n"}, {"source_code": "n=gets.chomp.to_i\nphone=gets.chomp\ns=\"\"\nif n%2==0\n n.times do |i|\n s+=phone[i,1]\n if i%2 == 1\n s+=\"-\"\n end\n end\n puts s[0,s.length-1]\nelse\n (n-2).times do |i|\n s+=phone[i,1]\n if i%2 == 1\n s+=\"-\"\n end\n end\n s+=phone[-2,3]\n puts s\nend\n"}, {"source_code": "\nn = gets.to_i\ns = gets.chomp\n\nk = n % 3\nr = ''\nif k != 0\n r = s[0, 2] + '-'\n s = s[2 ... s.length]\nend\n\nfor i in 0...n/3\n r = r + s[3 * i, 3] + '-'\nend\nputs r[0, r.length - 1]\n"}, {"source_code": "digits = gets.to_i\nnumber = gets.chomp\nif digits % 2 == 0\n telephone = number.scan(/.{2}/)\n (0...telephone.size).each do |i|\n if i == telephone.size - 1\n puts telephone[i]\n else\n print \"#{telephone[i]}-\"\n end\n end\nelse\n if digits % 3 == 0\n telephone = number.scan(/.{3}/)\n (0...telephone.size).each do |i|\n if i == telephone.size - 1\n puts telephone[i]\n else\n print \"#{telephone[i]}-\"\n end\n end\n else\n if (digits-3) % 2 == 0\n telephone = number[0...digits-3].scan(/.{2}/)\n (0...telephone.size).each do |i|\n print \"#{telephone[i]}-\"\n end\n puts number[digits-3...digits]\n end\n end\nend"}, {"source_code": "n, phone = gets, gets.chomp\nleft = phone.length\ni=0\ns=\"\"\nwhile left>0\n s+=\"-\" if s!=\"\"\n x = left==3?2:1;\n s << phone[i..i+x]\n i = i+x+1\n left = left-x-1\nend\nputs s"}, {"source_code": "n = gets.to_i\ngets.strip.each_char.with_index do |y,i|\n if i > 0 && (i%2 == 0 && i < n-1 || i == n-3 && n%2 != 0)\n print '-';\n end\n print y\nend\n"}, {"source_code": "n = gets.to_i\ngets.strip.each_char.with_index do |y,i|\n if i > 0 && (i%2 == 0 && i < n-1 || i == n-3 && n%2 != 0)\n print '-';\n end;\n print y;\nend;\n"}, {"source_code": "n = gets.to_i\ngets.strip.each_char.with_index do |y,i|\n if i > 0 && (i%2 == 0 && i < n-1 || i == n-3 && n%2 != 0)\n print '-';\n end;\n print y\nend\n"}, {"source_code": "n = gets.to_i\ngets.strip.each_char.with_index do |y,i|\n if i > 0 && (i%2 == 0 && i < n-1 || i == n-3 && n%2 != 0)\n print '-'\n end\n print y\nend\n"}, {"source_code": "n = gets.to_i\ngets.strip.each_char.with_index do |y,i|\n if i > 0 && (i%2 == 0 && i < n-1 || i == n-3 && n%2 != 0)\n print '-';\n end;\n print y;\nend\n"}, {"source_code": "n = gets.to_i\ngets.strip.each_char.with_index do |y,i|\n if i > 0 && (i%2 == 0 && i < n-1 || i == n-3 && n%2 != 0);\n print '-';\n end;\n print y;\nend;\n"}, {"source_code": "numOfDigits = gets.chomp.to_i\nphoneNumber = gets.chomp\n\ndiv5 = numOfDigits / 5\nmod5 = numOfDigits % 5\n\nnumOf2 = div5\nnumOf3 = div5\n\nif mod5 > 0\n if mod5 % 2 == 0\n #2 or 4\n numOf2 = numOf2 + (mod5 / 2)\n elsif mod5 % 3 == 0\n #3\n numOf3 = numOf3 + 1\n else\n #1\n numOf2 = numOf2 - 1\n numOf3 = numOf3 + 1\n end \nend\n\nresult = \"\"\nindex = 0\n\n1.upto(numOf2) do |i|\n result = result + \"-\" + phoneNumber[index, 2]\n index = index + 2 \nend\n\n1.upto(numOf3) do |i|\n result = result + \"-\" + phoneNumber[index, 3]\n index = index + 3 \nend\n\nif result[0, 1] == \"-\"\n result = result[1, result.length - 1]\nend\n\nputs result"}, {"source_code": "gets\nphone = gets.chomp.scan(/./)\n\nres = \"\"\nwhile phone.size >= 2\n res << phone.shift(2).join << '-'\nend\nres = res[0..-2]\nres << phone.join\n\nputs res"}, {"source_code": "puts gets.to_i%2<1 ? gets.scan(/../).to_a*\"-\" : ([gets[0..2]]+$_[3..-1].scan(/../).to_a)*\"-\""}], "negative_code": [{"source_code": "gets\nw=gets\nif w.size%2==0\n\tputs w.scan(/../)*'-'\nelse\n\tputs [*w[0..-3].scan(/../),w[-3..-1]]*'-'\nend"}, {"source_code": "gets\nw=gets\nif w.size%2==0\n\tputs w.scan(/../)*'-'\nelse\n\tputs [w[0,3],*w[3..-1].scan(/../)]*'-'\nend"}, {"source_code": "#!/usr/bin/ruby\ngets;s=gets.chomp\nif s.size%3==1\n\tt=s[-4..-1]\n\tputs s[0..-5].chars.each_slice(3).map{|e|e*''}*'-'+'-'+t[0,2]+'-'+t[2,2]\nelse\n\tputs s.chars.each_slice(3).map{|e|e*''}*'-'\nend"}], "src_uid": "6f6859aabc1c9cbb9ee0d910064d87c2"} {"nl": {"description": "Bob has a permutation of integers from 1 to n. Denote this permutation as p. The i-th element of p will be denoted as pi. For all pairs of distinct integers i, j between 1 and n, he wrote the number ai, j = min(pi, pj). He writes ai, i = 0 for all integer i from 1 to n.Bob gave you all the values of ai, j that he wrote down. Your job is to reconstruct any permutation that could have generated these values. The input will be formed so that it is guaranteed that there is at least one solution that is consistent with the information given.", "input_spec": "The first line of the input will contain a single integer n (2 ≤ n ≤ 50). The next n lines will contain the values of ai, j. The j-th number on the i-th line will represent ai, j. The i-th number on the i-th line will be 0. It's guaranteed that ai, j = aj, i and there is at least one solution consistent with the information given.", "output_spec": "Print n space separated integers, which represents a permutation that could have generated these values. If there are multiple possible solutions, print any of them.", "sample_inputs": ["2\n0 1\n1 0", "5\n0 2 2 1 2\n2 0 4 1 3\n2 4 0 1 3\n1 1 1 0 1\n2 3 3 1 0"], "sample_outputs": ["2 1", "2 5 4 1 3"], "notes": "NoteIn the first case, the answer can be {1, 2} or {2, 1}.In the second case, another possible answer is {2, 4, 5, 1, 3}."}, "positive_code": [{"source_code": "n = gets.to_i\nm = (1..n).map{gets.strip.split(\" \").map(&:to_i)}\n\na = [nil] * n\n\ni = 1\nwhile i <= n\n m.each_with_index do |s, k|\n if s.all? {|x| x <= i} && s.any? {|x| x == i-1}\n a[k] = i\n i += 1\n end\n end\nend\n\nputs a.join(\" \")\n"}, {"source_code": "n=gets.to_i\na=n.times.map{gets.split.map(&:to_i).max}\na[a.index(n-1)]=n\nputs a*' '"}, {"source_code": "# http://codeforces.com/problemset/problem/618/B\n\nn = gets.to_i\nans = []\nn.times do |i|\n arr = gets.split.map(&:to_i)\n ans << arr.max\nend\nans[ans.index(n - 1)] = n\nputs ans * \" \""}, {"source_code": "n=gets.to_i\na=n.times.map{gets.split.map(&:to_i).max}\na[a.index(n-1)]=n\nputs a*' '\n"}, {"source_code": "n=gets.to_i\na=n.times.map{gets.split.map(&:to_i).max}\na[a.index(n-1)]=n\nputs a*' '\n"}, {"source_code": "n = gets.chomp.to_i\nps = []\nf = false\nn.times do\n as = gets.chomp.split.map(&:to_i)\n p = as.sort!.uniq.size - 1\n if p == n - 1\n ps << (f ? p : n)\n f = true\n else\n ps << p\n end\nend\n\nputs ps.*' '\n"}, {"source_code": "n = gets.to_i\ng = []\nfor _ in 0...n\n a = gets.split(' ').map(&:to_i)\n g << a\nend\n\nr = []\nfor j in 0...n\n for k in 0..j\n if g[j][k] == n-1\n r[j] = n\n r[k] = n-1\n end\n end\nend\n\nfor i in 2...n\n x = n-i\n for j in 0...n\n for k in 0..j\n if g[j][k] == x && (!r[j] || !r[k])\n if r[k]\n r[j] = x\n else\n r[k] = x\n end\n end\n end\n end\nend\n\nputs r.join(' ')"}, {"source_code": "n = gets.chomp.to_i\nn.times do |i|\n r = gets.chomp.split(\" \")\n if r.uniq.length == n then\n r[i] = n\n puts r.join(\" \")\n exit\n end\nend\n"}, {"source_code": "n=gets.to_i\na=n.times.map{gets.split.map(&:to_i).max}\na[a.index(n-1)]=n\nputs a*' '\n"}, {"source_code": "n=gets.to_i\na=n.times.map{gets.split.map(&:to_i).max}\na[a.index(n-1)]=n\nputs a*' '\n"}, {"source_code": "n=gets.to_i\na=n.times.map{gets.split.map(&:to_i).max}\na[a.index(n-1)]=n\nputs a*' '\n"}], "negative_code": [{"source_code": "# http://codeforces.com/problemset/problem/618/B\n\nn = gets.to_i\nans = []\nn.times do |i|\n arr = gets.split.map(&:to_i)\n ans << arr.max\nend\nans[ans.index(n - 1)] = n\np ans * \" \""}], "src_uid": "1524c658129aaa4175208b278fdc467c"} {"nl": {"description": "There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.", "input_spec": "The first line contains an integer n (1 ≤ n ≤ 106). The second line contains a sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 3).", "output_spec": "Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.", "sample_inputs": ["9\n1 3 2 2 2 1 1 2 3"], "sample_outputs": ["5"], "notes": "NoteIn the example all the numbers equal to 1 and 3 should be replaced by 2."}, "positive_code": [{"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nputs n - (1..3).map{|x| a.count(x)}.max\n"}, {"source_code": "def function\na=gets.chomp\nb=gets.chomp\nb=b.split.map(&:to_i)\nputs (b.length-b.count(b.uniq.max_by{ |i| b.count( i ) }))\nend\nfunction\n"}, {"source_code": "a=gets.to_i;gets;p a-(?1..?3).map{|i|$_.count i}.max\n"}, {"source_code": "a=gets.to_i;gets;p a-(?1..?3).map{|i|$_.count i}.max\n"}, {"source_code": "n = gets.chomp.to_i\nints = gets.chomp.split\nh = Hash.new(0)\nints.each { |i| h[i] += 1 }\nputs n - h.values.max"}, {"source_code": "a=gets.to_i;gets;p a-(?1..?3).map{|i|$_.count i}.max"}, {"source_code": "n=gets.to_i\narr=gets.split(' ').map(&:to_i)\na = [0,0,0,0]\narr.each do |x|\na[x] += 1\nend\nb = [a[1]+a[2],a[1]+a[3],a[2]+a[3]]\nputs b.min"}, {"source_code": "a=gets.to_i;gets;p a-(?1..?3).map{|i|$_.count i}.max\n"}, {"source_code": "a=gets.to_i;gets;p a-(?1..?3).map{|i|$_.count i}.max\n"}, {"source_code": "a=gets.to_i;gets;p a-(?1..?3).map{|i|$_.count i}.max"}, {"source_code": "n = gets\narr = gets\n\nnr1 = 0\nnr2 = 0\nnr3 = 0\n\narr.split(\" \").map(&:to_i).each do |x|\n if x == 1\n nr1 += 1\n elsif x == 2\n nr2 += 1\n else\n nr3 += 1\n end\nend\n\nputs n.to_i - [nr1,nr2, nr3].max"}, {"source_code": "a=gets.to_i;gets;p a-(?1..?3).map{|i|$_.count i}.max"}, {"source_code": "a=gets.to_i;gets;p a-(?1..?3).map{|i|$_.count i}.max\n"}, {"source_code": "gets;n=gets;k,l,m=n.count(\"1\"),n.count(\"2\"),n.count(\"3\");puts (k>l ?(k>m ?(m+l):(k+l)):(l>m ?(k+m):(k+l)))"}, {"source_code": "n = gets.chomp.to_i\nints = gets.chomp.split\nh = Hash.new(0)\nints.each { |i| h[i] += 1 }\nputs n - h.values.max"}, {"source_code": "n = gets.chomp.to_i\nints = gets.chomp.split\nh = Hash.new(0)\nints.each { |i| h[i] += 1 }\nputs n - h.values.max"}, {"source_code": "a=gets.to_i;gets;p a-(?1..?3).map{|i|$_.count i}.max\n"}, {"source_code": "a=gets.to_i;gets;p a-(?1..?3).map{|i|$_.count i}.max\n"}, {"source_code": "gets\ngets\np [*?1..?3].map{|_|$_.count _}.combination(2).map{|a,b|a+b}.min"}, {"source_code": "gets; n = gets.strip\nprint [n.count(\"1\"), n.count(\"2\"), n.count(\"3\")].sort[0,2].inject(0) { |sum, e| sum += e }\n"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split.map(&:to_i)\nputs n - [a.select {|x| x == 3 }.length, a.select{|x| x==2}.length, a.select{|x| x==1}.length].max"}], "negative_code": [{"source_code": "def function\na=gets.chomp\nb=gets.chomp\nb=b.split.map(&:to_i)\nprint (b.length-b.count(b.uniq.max_by{ |i| b.count( i ) }))\nend\n"}, {"source_code": "def function\na=gets.chomp\nb=gets.chomp\nb=b.split.map(&:to_i)\nputs b.length-b.count(b.uniq.max_by{ |i| b.count( i ) })\nend\n"}, {"source_code": "n = gets\narr = gets\n\nnr1 = 0\nnr2 = 0\nnr3 = 0\n\narr.split(\" \").map(&:to_i).each do |x|\n if x == 1\n nr1 += 1\n elsif x == 2\n nr2 += 1\n else\n nr3 += 1\n end\nend\n\nn.to_i - [nr1,nr2, nr3].max"}, {"source_code": "n = gets\narr = gets\n\nnr1 = 0\nnr2 = 0\nnr3 = 0\n\nif n.to_i == 0\n puts 0\nelsif n.to_i == 1\n puts 0\nend\n\narr.split(\" \").map(&:to_i).each do |x|\n if x == 1\n nr1 += 1\n elsif x == 2\n nr2 += 1\n else\n nr3 += 1\n end\nend\n\nputs n.to_i - [nr1,nr2, nr3].max"}, {"source_code": "n = gets\narr = gets\n\nnr1 = 0\nnr2 = 0\nnr3 = 0\n\nif n.to_i == 0\n puts 0\nelsif n.to_i == 1\n puts 1\nend\n\narr.split(\" \").map(&:to_i).each do |x|\n if x == 1\n nr1 += 1\n elsif x == 2\n nr2 += 1\n else\n nr3 += 1\n end\nend\n\nputs n.to_i - [nr1,nr2, nr3].max"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split.map(&:to_i)\nputs n - [a.select {|x| x == 1 }.length, a.select{|x| x==2}.length, a.select{|x| x==1}.length].max"}], "src_uid": "fcb6a715dfe302d7ae5a6695ca8976aa"} {"nl": {"description": "Marina loves Sasha. But she keeps wondering whether Sasha loves her. Of course, the best way to know it is fortune telling. There are many ways of telling fortune, but Marina has picked the easiest one. She takes in her hand one or several camomiles and tears off the petals one by one. After each petal she pronounces alternatively \"Loves\" and \"Doesn't love\", at that Marina always starts with \"Loves\". There are n camomiles growing in the field, possessing the numbers of petals equal to a1, a2, ... an. Marina wants to pick a bouquet with the maximal possible total number of petals so that the result would still be \"Loves\". Help her do that; find the maximal number of petals possible in the bouquet.", "input_spec": "The first line contains an integer n (1 ≤ n ≤ 100), which is the number of flowers growing in the field. The second line contains n integers ai (1 ≤ ai ≤ 100) which represent the number of petals on a given i-th camomile.", "output_spec": "Print a single number which is the maximal number of petals in the bouquet, the fortune telling on which would result in \"Loves\". If there are no such bouquet, print 0 instead. The bouquet may consist of a single flower.", "sample_inputs": ["1\n1", "1\n2", "3\n5 6 7"], "sample_outputs": ["1", "0", "13"], "notes": null}, "positive_code": [{"source_code": "n=gets.to_i\na=gets.split.map(&:to_i).sort\nb,c=[],[]\na.each{|k|\n if k%2==0 then\n b.push(k)\n else\n c.push(k)\n end\n}\ninit=1\nif c.length==0 then\n puts 0\n exit\nend\nans=0\nif c.length%2==0 then\n for i in 1..(c.length-1)\n ans+=c[i]\n end\nelse\n for i in 0..(c.length-1)\n ans+=c[i]\n end\nend\nb.each{|k| ans+=k}\np ans\n"}, {"source_code": "s = gets\nodds = []\nevens = []\n\ngets.split(\" \").each {|e|\n t = e.to_i\n if (t % 2 == 0)\n evens.push t\n else\n odds.push t\n end\n}\n\nodds.sort!\nevens.sort!\n\nif (odds.size == 0)\n puts 0\nelse\n t = odds.inject(0) {|r, i| r + i} + evens.inject(0) {|r, i| r + i}\n if (odds.size % 2 == 0) \n t -= odds[0]\n end\n puts t\nend\n"}, {"source_code": "N = gets.to_i\na = gets.split(' ').map {|i| i.to_i}\nsum = a.inject(0) {|s, i| s + i}\ncntNe = a.inject(0) {|s, i|\n r = s\n r += 1 if i % 2 != 0\n r\n}\n\nif cntNe == 0\n puts 0\n exit\nend\n\nif cntNe % 2 != 0\n puts sum\n exit\nend\n\nputs sum - a.select{|i| i % 2 != 0}.min"}, {"source_code": "n = gets.chomp\ninput = gets.chomp.split(' ').collect { |i| i.to_i }\nsum = 0\n\nsmallestOdd = input.collect do |i| \n\t\t\t\t\tif i.odd? then i else 101 end\n\t\t\t end.min\nsmallestOdd = -1 if smallestOdd == 101\ninput.each { |i| sum = sum + i }\n\nif sum.odd? \n puts sum\nelsif smallestOdd == -1\nputs 0\nelse \nputs sum - smallestOdd\nend\n\n\n\n\n\n\n"}, {"source_code": "gets\nflowers = gets.chomp.split.map(&:to_i).sort\nsum = flowers.inject(0) { |s, v| s + v }\n\nif sum.odd?\n puts sum\n exit\nelse\n first_odd = flowers.detect{ |f| f.odd? }\n if first_odd\n puts sum - first_odd\n exit\n end\nend\nputs 0"}], "negative_code": [{"source_code": "\nn=gets.to_i\na=gets.split.map(&:to_i)\nb,c=[],[]\na.each{|k|\n if k%2==0 then\n b.push(k)\n else\n c.push(k)\n end\n}\ninit=1\nif c.length==0 then\n puts 0\n exit\nend\nans=0\nif c.length%2==0 then\n for i in 1..(c.length-1)\n ans+=c[i]\n end\nelse\n for i in 0..(c.length-1)\n ans+=c[i]\n end\nend\nb.each{|k| ans+=k}\np ans\n"}, {"source_code": "n = gets.chomp\ninput = gets.chomp.split(' ').collect { |i| i.to_i }\nsum = 0\n\nsmallestOdd = input.collect do |i| \n\t\t\t\t\tif i.odd? then i else 101 end\n\t\t\t end.min\nsmallestOdd = 0 if smallestOdd == 101\ninput.each { |i| sum = sum + i }\n\nif sum.odd? then puts sum\nelse puts sum - smallestOdd\nend\n\n\n\n\n\n\n"}, {"source_code": "\n\nn = gets.chomp\ninput = gets.chomp.split(' ').collect { |i| i.to_i }\nsum = 0\n\nsmallestOdd = input.collect do |i| \n\t\t\t\t\tif i.odd? then i else 101 end\n\t\t\t end.min\ninput.each { |i| sum = sum + i }\n\nputs sum - smallestOdd if sum.even?\nputs sum if sum.odd?\n\n\n\n"}, {"source_code": "gets\nflowers = gets.chomp.split.map(&:to_i).sort\nsum = flowers.inject(0) { |s, v| s + v }\n\nif sum.odd?\n puts sum\n exit\nelse\n while flowers.size > 0\n sum = sum - flowers.shift\n if sum.odd?\n puts sum\n exit\n end\n end\nend\nputs 0"}], "src_uid": "a5d56056fd66713128616bc7c2de8b22"} {"nl": {"description": "Riley is a very bad boy, but at the same time, he is a yo-yo master. So, he decided to use his yo-yo skills to annoy his friend Anton.Anton's room can be represented as a grid with $$$n$$$ rows and $$$m$$$ columns. Let $$$(i, j)$$$ denote the cell in row $$$i$$$ and column $$$j$$$. Anton is currently standing at position $$$(i, j)$$$ in his room. To annoy Anton, Riley decided to throw exactly two yo-yos in cells of the room (they can be in the same cell).Because Anton doesn't like yo-yos thrown on the floor, he has to pick up both of them and return back to the initial position. The distance travelled by Anton is the shortest path that goes through the positions of both yo-yos and returns back to $$$(i, j)$$$ by travelling only to adjacent by side cells. That is, if he is in cell $$$(x, y)$$$ then he can travel to the cells $$$(x + 1, y)$$$, $$$(x - 1, y)$$$, $$$(x, y + 1)$$$ and $$$(x, y - 1)$$$ in one step (if a cell with those coordinates exists).Riley is wondering where he should throw these two yo-yos so that the distance travelled by Anton is maximized. But because he is very busy, he asked you to tell him.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of each test case contains four integers $$$n$$$, $$$m$$$, $$$i$$$, $$$j$$$ ($$$1 \\leq n, m \\leq 10^9$$$, $$$1\\le i\\le n$$$, $$$1\\le j\\le m$$$) — the dimensions of the room, and the cell at which Anton is currently standing.", "output_spec": "For each test case, print four integers $$$x_1$$$, $$$y_1$$$, $$$x_2$$$, $$$y_2$$$ ($$$1 \\leq x_1, x_2 \\leq n$$$, $$$1\\le y_1, y_2\\le m$$$) — the coordinates of where the two yo-yos should be thrown. They will be thrown at coordinates $$$(x_1,y_1)$$$ and $$$(x_2,y_2)$$$. If there are multiple answers, you may print any.", "sample_inputs": ["7\n2 3 1 1\n4 4 1 2\n3 5 2 2\n5 1 2 1\n3 1 3 1\n1 1 1 1\n1000000000 1000000000 1000000000 50"], "sample_outputs": ["1 2 2 3\n4 1 4 4\n3 1 1 5\n5 1 1 1\n1 1 2 1\n1 1 1 1\n50 1 1 1000000000"], "notes": "NoteHere is a visualization of the first test case. "}, "positive_code": [{"source_code": "t = Integer(gets.chomp)\r\nt.times{\r\n x, y, i, j = gets.chomp.split().map!{|c| Integer(c)}\r\n \r\n puts \"1 1 #{x} #{y}\"\r\n}"}], "negative_code": [], "src_uid": "5aae6b27f35852512a250751ef957ab9"} {"nl": {"description": "You are given the array $$$a$$$ consisting of $$$n$$$ positive (greater than zero) integers.In one move, you can choose two indices $$$i$$$ and $$$j$$$ ($$$i \\ne j$$$) such that the absolute difference between $$$a_i$$$ and $$$a_j$$$ is no more than one ($$$|a_i - a_j| \\le 1$$$) and remove the smallest of these two elements. If two elements are equal, you can remove any of them (but exactly one).Your task is to find if it is possible to obtain the array consisting of only one element using several (possibly, zero) such moves or not.You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 \\le n \\le 50$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 100$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$.", "output_spec": "For each test case, print the answer: \"YES\" if it is possible to obtain the array consisting of only one element using several (possibly, zero) moves described in the problem statement, or \"NO\" otherwise.", "sample_inputs": ["5\n3\n1 2 2\n4\n5 5 5 5\n3\n1 2 4\n4\n1 3 4 4\n1\n100"], "sample_outputs": ["YES\nYES\nNO\nNO\nYES"], "notes": "NoteIn the first test case of the example, we can perform the following sequence of moves: choose $$$i=1$$$ and $$$j=3$$$ and remove $$$a_i$$$ (so $$$a$$$ becomes $$$[2; 2]$$$); choose $$$i=1$$$ and $$$j=2$$$ and remove $$$a_j$$$ (so $$$a$$$ becomes $$$[2]$$$). In the second test case of the example, we can choose any possible $$$i$$$ and $$$j$$$ any move and it doesn't matter which element we remove.In the third test case of the example, there is no way to get rid of $$$2$$$ and $$$4$$$."}, "positive_code": [{"source_code": "gets.to_i.times do\n answer = true\n\n gets\n gets.split(' ').map(&:to_i).sort.each_cons(2) { |a, b| answer &&= b - a <= 1 }\n\n puts answer ? 'YES' : 'NO'\nend\n"}, {"source_code": "t=gets.chomp.to_i\n#p \"t= #{t}\"\nwhile t!=0\n\nlen=gets.chomp.to_i\n#p \"length= #{len}\"\nk=0\n#while k<=len-1\nar = gets.chomp.split(' ').map!{ |o| o.to_i }\n#k+=1\n#end\nar.sort!\n #p ar\ndef func(ar)\n \nl=ar.size\nif l==1\n return \"YES\"\nend\n i=0\nwhile i < l-1\n \nif ar[i+1]-ar[i]>1\n return \"NO\"\nend\ni+=1\nend\nreturn \"YES\"\nend\n \n \nputs func(ar)\n \nt-=1\nend"}, {"source_code": "times = gets.to_i\n\ndef resolve(array)\n array.sort.each_cons(2) {|e,n| return \"NO\" if n-e >1}\n \"YES\"\nend\n\n(1..times).each do |i|\n array_size = gets.to_i\n\n a = Array.new(array_size)\n a = gets.split(/\\s+/).map(&:to_i)\n puts resolve(a)\nend"}, {"source_code": "t = gets.chomp.to_i\nt.times do\n n = gets.chomp.to_i\n a = gets.chomp.split(\" \").map(&:to_i)\n aa = a.sort\n i = 0\n while true\n if n.to_s == \"1\"\n puts \"YES\"\n break\n end\n sa = (aa[i+1] - aa[i]).abs\n if sa <= 1\n aa.shift\n i = 0\n else\n puts \"NO\"\n break\n end\n if aa.size == 1\n puts \"YES\"\n break\n end\n end\nend\n"}, {"source_code": "gets.to_i.times do\n n = gets.to_i\n a = gets.split.map(&:to_i).sort!\n ok = true\n (1...n).each do |i|\n ok &= a[i] - a[i - 1] <= 1\n end\n puts ok ? \"YES\" : \"NO\"\nend "}, {"source_code": "t = gets.to_i\nans = []\nt.times do\n n = gets.to_i\n a = gets.chomp.split(\" \").map(&:to_i).sort\n judge = true\n (n-1).times do |i|\n if a[i+1] - a[i] >= 2\n judge = false\n end\n end\n if judge\n ans << \"YES\"\n else\n ans << \"NO\"\n end\nend\nans.each do |str|\n puts str\nend\n"}, {"source_code": "gets.to_i.times do\n answer = true\n \n gets\n gets.split(' ').map(&:to_i).sort.each_cons(2) { |a, b| answer &&= b - a <= 1 }\n \n puts answer ? 'YES' : 'NO'\nend"}, {"source_code": "gets.to_i.times do\n gets\n a = gets.split(' ').map(&:to_i).sort\n\n p = a.shift\n x = 0\n\n while a.size > 0\n x += 1 if (a.first - p).abs > 1\n p = a.shift\n end\n\n puts x.zero? ? \"YES\" : \"NO\"\nend\n"}, {"source_code": "test_case = gets.to_i\ni = 0\nwhile i < test_case\n result = true\n i += 1\n len_ = gets.to_i\n Acase = gets.chomp.split(' ').map(&:to_i)\n Acase.sort!\n #puts Acase.inspect()\n j = 0\n while j < len_-1\n \n if Acase[j+1].to_i - Acase[j].to_i >= 2\n result = false\n j += 1\n break\n end\n j += 1\n end\n puts (result) ? \"Yes\" : \"No\" \n \nend"}, {"source_code": "tc = gets().to_i\ntc.times do\n n = gets().to_i\n arr = gets().split(' ').map(&:to_i).sort\n k = true\n for i in (1..n-1) do\n if arr[i]-arr[i-1] > 1\n puts \"NO\";\n k = false\n break;\n end\n end\n if (k)\n puts \"YES\"\n end\nend"}], "negative_code": [{"source_code": "t=gets.chomp.to_i\n#p \"t= #{t}\"\nwhile t!=0\n\nlen=gets.chomp.to_i\n#p \"length= #{len}\"\nk=0\n#while k<=len-1\nar = gets.chomp.split(' ').map!{ |o| o.to_i }\n#k+=1\n#end\nar.sort!\n p ar\ndef func(ar)\n \nl=ar.size\nif l==1\n return \"YES\"\nend\n i=0\nwhile i < l-1\n \nif ar[i+1]-ar[i]>1\n return \"NO\"\nend\ni+=1\nend\nreturn \"YES\"\nend\n \n \np func(ar)\n \nt-=1\nend"}, {"source_code": "t=gets.chomp.to_i\n#p \"t= #{t}\"\nwhile t!=0\n\nlen=gets.chomp.to_i\n#p \"length= #{len}\"\nk=0\n#while k<=len-1\nar = gets.chomp.split(' ').map!{ |o| o.to_i }\n#k+=1\n#end\nar.sort!\n #p ar\ndef func(ar)\n \nl=ar.size\nif l==1\n return \"YES\"\nend\n i=0\nwhile i < l-1\n \nif ar[i+1]-ar[i]>1\n return \"NO\"\nend\ni+=1\nend\nreturn \"YES\"\nend\n \n \np func(ar)\n \nt-=1\nend"}, {"source_code": "t = gets.chomp.to_i\nt.times do\n n = gets.chomp.to_i\n a = gets.chomp.split(\" \").map(&:to_i)\n aa = a.sort\n i = 0\n while true\n if n == 1\n puts \"YES\"\n break\n end\n sa = (a[i+1] - a[i]).abs\n if sa <= 1\n a.shift\n i = 0\n else\n puts \"NO\"\n break\n end\n if a.size == 1\n puts \"YES\"\n break\n end\n end\nend\n"}, {"source_code": "def array(arr)\n arr.sort!.each_cons(2) do |a, b|\n b - a > 1 ? 'NO' : 'YES'\n end\nend"}, {"source_code": "test_case = gets.to_i\ni = 0\nwhile i < test_case\n result = true\n i += 1\n len_ = gets.to_i\n Acase = gets.chomp.split(' ').map(&:to_i)\n Acase.sort!\n #puts Acase.inspect()\n j = 0\n while j < len_-1\n j += 1\n if Acase[j+1].to_i - Acase[j].to_i >= 2\n result = false\n break\n end\n end\n puts (result) ? \"Yes\" : \"No\" \n \nend"}, {"source_code": "tc = gets().to_i\ntc.times do\n n = gets().to_i\n arr = gets().split(' ').map(&:to_i).sort\n if arr[n-1]-arr[0] > 1\n puts(\"NO\")\n else\n puts(\"YES\")\n end\nend"}, {"source_code": "tc = gets().to_i\ntc.times do\n n = gets().to_i\n arr = gets().split(' ').map(&:to_i)\n k = true\n for i in (1..n-1) do\n if arr[i]-arr[i-1] > 1\n puts \"NO\";\n k = false\n break;\n end\n end\n if (k)\n puts \"YES\"\n end\nend"}], "src_uid": "ed449ba7c453a43e2ac5904dc0174530"} {"nl": {"description": "In the Main Berland Bank n people stand in a queue at the cashier, everyone knows his/her height hi, and the heights of the other people in the queue. Each of them keeps in mind number ai — how many people who are taller than him/her and stand in queue in front of him.After a while the cashier has a lunch break and the people in the queue seat on the chairs in the waiting room in a random order.When the lunch break was over, it turned out that nobody can remember the exact order of the people in the queue, but everyone remembers his number ai.Your task is to restore the order in which the people stood in the queue if it is possible. There may be several acceptable orders, but you need to find any of them. Also, you need to print a possible set of numbers hi — the heights of people in the queue, so that the numbers ai are correct.", "input_spec": "The first input line contains integer n — the number of people in the queue (1 ≤ n ≤ 3000). Then n lines contain descriptions of the people as \"namei ai\" (one description on one line), where namei is a non-empty string consisting of lowercase Latin letters whose length does not exceed 10 characters (the i-th person's name), ai is an integer (0 ≤ ai ≤ n - 1), that represents the number of people who are higher and stand in the queue in front of person i. It is guaranteed that all names are different.", "output_spec": "If there's no acceptable order of the people in the queue, print the single line containing \"-1\" without the quotes. Otherwise, print in n lines the people as \"namei hi\", where hi is the integer from 1 to 109 (inclusive), the possible height of a man whose name is namei. Print the people in the order in which they stand in the queue, starting from the head of the queue and moving to its tail. Numbers hi are not necessarily unique.", "sample_inputs": ["4\na 0\nb 2\nc 0\nd 0", "4\nvasya 0\npetya 1\nmanya 3\ndunay 3"], "sample_outputs": ["a 150\nc 170\nd 180\nb 160", "-1"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\na = n.times.map{\n x, y = gets.split\n [y.to_i, x]\n}.sort\nb = []\na.each do |x, y|\n break if x > b.size\n b.each{|_| _[1] += 1 if _[1] >= x}\n b << [y, x]\nend\nputs b.size < n ? -1 : b.map{|i, j| \"#{i} #{n - j}\"} * \"\\n\"\n"}, {"source_code": "n=gets.to_i\nans=[]\nn.times.map{name,a=gets.split;[name,a.to_i]}.sort_by{|name,a|a}.each{|name,a|\n\tif ans.size b.size\n\t\t\t b.each{|_| _[1] += 1 if _[1] >= x}\n\t\t\t\t\t b << [y, x]\nend\nputs b.size < n ? -1 : b.map{|i, j| \"#{i} #{n - j}\"} * \"\\n\"\n"}, {"source_code": "n=gets.to_i\nans=[]\nn.times.map{name,a=gets.split;[name,a.to_i]}.sort_by{|name,a|a}.each{|name,a|\n\tif ans.size i\n ho[a, 0] = n\n end\n\n data.each do |n, a|\n ret << [n, data.size-ho.index(n)]\n end\n ret\nend\n\ndef check(sol)\n hs = []\n sol.map{|n,h| v = [n, hs.count{|x| x > h}]; hs << h; v}\nend\n\ndef test\n srand(122039)\n 100.times do |i|\n p i\n d0 = 30.times.map{|x|[\"a#{x}\", rand(10**9)]}\n input = check(d0)\n out = solve(input)\n input2 = check(out)\n if input != input2\n p d0, input, input2 \n raise\n end\n end\n exit\nend\n\nn = gets.to_i\ndata = n.times.map{ a, b = gets.split; [a, b.to_i]}\nret = solve(data)\nif ret\n ret.each{|a| puts a*\" \"}\nelse\n puts -1\nend\n"}, {"source_code": "n=gets.to_i\nans=[]\nn.times.map{name,a=gets.split;[name,a.to_i]}.sort_by{|name,a|a}.each{|name,a|\n\tif ans.size 0\n\t\tpos += 1\n\telse\n\t\tneg += 1\n\tend\nend\n\nif pos == arr.size || neg == arr.size\n\tprint \"Yes\"\nelse\n\tif pos == 1 || neg == 1\n\t\tprint \"Yes\"\n\telse\n\t\tprint \"No\"\n\tend\nend"}], "negative_code": [{"source_code": "puts gets.to_i.times.count{gets.split.map(&:to_i).reduce(:+)<1}<1 ? :YES : :NO"}, {"source_code": "puts gets.to_i.times.count{gets.split.map(&:to_i).reduce(:+)<1}<2 ? :YES : :NO"}, {"source_code": "gets;puts$<.group_by{|e|e.to_i<0}.any?{|k,v|v.size<2} ? :YES : :NO"}], "src_uid": "cf7bf89a6038586b69d3b8021cee0b27"} {"nl": {"description": "You are given two positive integers $$$n$$$ ($$$1 \\le n \\le 10^9$$$) and $$$k$$$ ($$$1 \\le k \\le 100$$$). Represent the number $$$n$$$ as the sum of $$$k$$$ positive integers of the same parity (have the same remainder when divided by $$$2$$$).In other words, find $$$a_1, a_2, \\ldots, a_k$$$ such that all $$$a_i>0$$$, $$$n = a_1 + a_2 + \\ldots + a_k$$$ and either all $$$a_i$$$ are even or all $$$a_i$$$ are odd at the same time.If such a representation does not exist, then report it.", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases in the input. Next, $$$t$$$ test cases are given, one per line. Each test case is two positive integers $$$n$$$ ($$$1 \\le n \\le 10^9$$$) and $$$k$$$ ($$$1 \\le k \\le 100$$$).", "output_spec": "For each test case print: YES and the required values $$$a_i$$$, if the answer exists (if there are several answers, print any of them); NO if the answer does not exist. The letters in the words YES and NO can be printed in any case.", "sample_inputs": ["8\n10 3\n100 4\n8 7\n97 2\n8 8\n3 10\n5 3\n1000000000 9"], "sample_outputs": ["YES\n4 2 4\nYES\n55 5 5 35\nNO\nNO\nYES\n1 1 1 1 1 1 1 1\nNO\nYES\n3 1 1\nYES\n111111110 111111110 111111110 111111110 111111110 111111110 111111110 111111110 111111120"], "notes": null}, "positive_code": [{"source_code": " t = gets.to_i\n t.times do\n n, k = gets.split(' ').map(&:to_i)\n if k == 1\n puts 'YES'\n puts n\n next\n end\n res = []\n (k-1).times do |i|\n res << 1\n end\n if((_res = n - res.reduce(&:+)) % 2 != 0 && _res > 0)\n res << _res\n puts 'YES'\n puts res.join(' ')\n else\n res = []\n (k-1).times do |i|\n res << 2\n end\n if((_res = n - res.reduce(&:+)) % 2 == 0 && _res > 0)\n res << _res\n puts 'YES'\n puts res.join(' ')\n else\n puts 'NO'\n end\n end\n end"}, {"source_code": "t = gets.to_i\n\ndef all(a,b)\n puts \"YES\"\n (1..a).each do\n print \"#{b} \"\n end\n puts\nend\n\ndef printArr(arr)\n puts \"YES\"\n arr.each do |x|\n print \"#{x} \"\n end\n puts\nend\n\n(1).upto(t) do\n x = gets.split(' ')\n n = x[0].to_i\n k = x[1].to_i\n val1 = n/k\n val2 = n.modulo(k)\n if val1 ==0\n puts \"NO\"\n next\n end\n if(val2 == 0)\n all(k,val1)\n next\n end\n arr = []\n (1..k).each do\n arr.push(val1)\n end\n if val2.modulo(2)==0\n arr[0]+=val2\n printArr(arr)\n next\n end\n if k.modulo(2)==0\n puts \"NO\"\n next\n end\n if arr[0]!=1\n (0.. arr.length-1).each do |i|\n arr[i]-=1\n val2+=1\n end\n arr[0]+=val2\n printArr(arr)\n next\n end\n puts \"NO\"\nend\n"}, {"source_code": "# Created by IntelliJ IDEA.\n# User: gautam.roy\n# Date: 5/9/20\n# Time: 9:37 PM\n# To change this template use File | Settings | File Templates.\n\nt = gets.to_i\nfor i in 1..t do\n n, k = gets.split(' ').map { |s| s.to_i }\n ret = []\n if n < k\n puts \"NO\"\n next\n end\n if k == 1\n puts \"Yes\"\n puts n\n next\n end\n if k.even? && !n.even?\n puts \"NO\"\n next\n end\n if (k.even? && n.even?) || (!k.even? && !n.even?)\n puts \"Yes\"\n for j in 1..(k - 1)\n ret << 1\n end\n ret << n - (k - 1)\n puts ret.join(\" \")\n next\n end\n # k odd and n even\n if (n - 2 * (k - 1)) > 0\n puts \"YES\"\n for j in 1..(k - 1)\n ret << 2\n end\n ret << n - 2 * (k - 1)\n puts ret.join(\" \")\n else\n puts \"NO\"\n end\nend"}, {"source_code": "gets.to_i.times{\n n,k=gets.split.map &:to_i\n res=[1]*(k-1)\n res< n\n puts \"NO\"\n else\n puts \"YES\"\n puts \"1 \"*(k - 1) + (n - k + 1).to_s\n end\n elsif n.even? and k.odd?\n if k*2 > n\n puts \"NO\"\n else\n puts \"YES\"\n puts \"2 \"*(k - 1) + (n - (k - 1)*2).to_s\n end\n else\n if k.even? or k > n\n puts \"NO\"\n else\n puts \"YES\"\n puts \"1 \"*(k - 1) + (n - k + 1).to_s\n end\n end\nend\n"}, {"source_code": "gets.to_i.times do\n n, k = gets.split(' ').map(&:to_i)\n\n f = [1, 2].find do |i|\n delta = n - (k-1)*i\n delta > 0 && (delta.even? == i.even?)\n end\n\n if f\n puts \"YES\"\n puts \"#{f} \" * (k-1) + (n - (k-1)*f).to_s\n else\n puts \"NO\"\n end \nend\n"}, {"source_code": "n = gets.chomp.to_i\n\ntests = []\n\nn.times do\n tests.push(gets.split(' ').map {|e| e.to_i})\nend\n\ntests.each do |test|\n n, k = test\n \n if n < k then\n puts 'NO'\n next\n end\n \n if n.odd? && k.even? then\n puts 'NO'\n next\n end\n\n results = []\n if n.even? && k.odd? then\n if 2 * k > n then\n puts 'NO'\n next\n end\n results.fill(2, 0..k - 2)\n results_sum = results.reduce(0) {|sum, value| sum + value }\n results.push(n - results_sum)\n puts 'YES'\n puts results.join(' ')\n else\n results.fill(1, 0..k - 2)\n results_sum = results.reduce(0) {|sum, value| sum + value }\n results.push(n - results_sum)\n puts 'YES'\n puts results.join(' ')\n end\n\nend"}], "negative_code": [{"source_code": "t = gets.to_i\nt.times do\n n, k = gets.split(' ').map(&:to_i)\n _res = n / k\n res = []\n if n % k == 0\n k.times { res << _res }\n else\n t_res = _res + 1\n n_res = n - (t_res * (k-1))\n if n_res < 1\n elsif((n_res.odd? && t_res.odd?) || (n_res.even? && t_res.even?))\n (k - 1).times { res << t_res }\n res << n_res\n end\n (2..(k-1)).to_a.each do |i|\n t_res += 1\n n_res = (n - (t_res * (k-i))) / i\n if n_res < 1\n elsif((n_res.odd? && t_res.odd?) || (n_res.even? && t_res.even?))\n (k - i).times { res << t_res }\n i.times { res << n_res }\n end\n if res.length != 0 && res.reduce(&:+) == n\n break\n else\n res = []\n end\n end if res.length == 0\n end\n puts(res.length == 0 ? 'NO' : 'YES')\n next if res.length == 0\n puts(res.join(' '))\nend\n"}, {"source_code": "t = gets.to_i\n\ndef all(a,b)\n puts \"YES\"\n (1..a).each do\n print \"#{b} \"\n end\n puts\nend\n\ndef printArr(arr)\n puts \"YES\"\n arr.each do |x|\n print \"#{x} \"\n end\n puts\nend\n\n(1).upto(t) do\n x = gets.split(' ')\n n = x[0].to_i\n k = x[1].to_i\n val1 = n/k\n val2 = n.modulo(k)\n if(val2 == 0)\n all(k,val1)\n next\n end\n arr = []\n (1..k).each do\n arr.push(val1)\n end\n if val2.modulo(2)==0\n arr[0]+=val2\n printArr(arr)\n next\n end\n if k.modulo(2)==0\n puts \"NO\"\n next\n end\n if arr[0]!=1\n (0.. arr.length-1).each do |i|\n arr[i]-=1\n val2+=1\n end\n arr[0]+=val2\n printArr(arr)\n next\n end\n puts \"NO\"\nend\n"}, {"source_code": "def func(n, k)\n res=2**40-1\n a=2**39\n while a>0\n t=res^a\n cnt=t-t/n\n res=t if cnt>=k\n a>>=1\n end\n res\nend\n\ngets.to_i.times{\n n, k=gets.split.map &:to_i\n puts func(n, k)\n}\n"}, {"source_code": "gets.to_i.times do\n n, k = gets.split.map &:to_i\n if n.even? and k.even?\n if k > n\n puts \"NO\"\n else\n puts \"YES\"\n puts \"1 \"*(k - 1) + (n - k + 1).to_s\n end\n elsif n.even? and k.odd?\n if k*2 > n\n puts \"NO\"\n else\n puts \"YES\"\n puts \"2 \"*(k - 1) + (n - (k - 1)*2).to_s\n end\n else\n if k.even?\n puts \"NO\"\n else\n puts \"YES\"\n puts \"1 \"*(k - 1) + (n - k + 1).to_s\n end\n end\nend\n"}, {"source_code": "n = gets.chomp.to_i\n\ntests = []\n\nn.times do\n tests.push(gets.split(' ').map {|e| e.to_i})\nend\n\ntests.each do |test|\n if test[0] % 2 == 1 && test[1] % 2 == 0 then\n puts 'NO'\n next\n end\n \n if test[0] < test[1] then\n puts 'NO'\n next\n end\n \n results = []\n \n if test[0] == test[1] then\n puts 'YES'\n results.fill(1, 0..test[1])\n puts results.join(' ')\n next\n end\n \n if test[0] % test[1] == 0 then\n puts 'YES'\n results.fill(test[0] / test[1], 0..test[1]-1)\n puts results.join(' ')\n elsif test[0] % 2 == 0 && test[1] % 2 == 1 then\n division = test[0] / test[1]\n division -= 1\n if division > 0 then\n results.fill(division, 0..test[1]-2)\n sum = results.reduce(0){|sum, value| sum + value}\n results.push(test[0] - sum)\n puts 'YES'\n puts results.join(' ')\n else\n puts 'NO' \n end\n elsif test[0] % 2 == 1 && test[1] % 2 == 1 then\n division = test[0] / test[1]\n results.fill(division, 0..test[1]-2)\n sum = results.reduce(0){|sum, value| sum + value}\n results.push(test[0] - sum)\n puts 'YES'\n puts results.join(' ')\n end\nend"}, {"source_code": "n = gets.chomp.to_i\n\ntests = []\n\nn.times do\n tests.push(gets.split(' ').map {|e| e.to_i})\nend\n\ntests.each do |test|\n if test[0] % 2 == 1 && test[1] % 2 == 0 then\n puts 'NO'\n next\n end\n \n if test[0] < test[1] then\n puts 'NO'\n next\n end\n \n results = []\n \n if test[0] == test[1] then\n puts 'YES'\n results.fill(1, 0..test[1]-1)\n puts results.join(' ')\n next\n end\n \n if test[0] % test[1] == 0 then\n puts 'YES'\n results.fill(test[0] / test[1], 0..test[1]-1)\n puts results.join(' ')\n elsif test[0] % 2 == 0 && test[1] % 2 == 1 then\n division = test[0] / test[1]\n division -= 1\n if division > 0 then\n results.fill(division, 0..test[1]-2)\n sum = results.reduce(0){|sum, value| sum + value}\n results.push(test[0] - sum)\n puts 'YES'\n puts results.join(' ')\n else\n puts 'NO' \n end\n elsif test[0] % 2 == 1 && test[1] % 2 == 1 then\n division = test[0] / test[1]\n results.fill(division, 0..test[1]-2)\n sum = results.reduce(0){|sum, value| sum + value}\n results.push(test[0] - sum)\n puts 'YES'\n puts results.join(' ')\n end\nend"}, {"source_code": "n = gets.chomp.to_i\n\ntests = []\n\nn.times do\n tests.push(gets.split(' ').map {|e| e.to_i})\nend\n\ntests.each do |test|\n n, k = test\n \n if n < k then\n puts 'NO'\n next\n end\n \n if n.odd? && k.even? then\n puts 'NO'\n next\n end\n \n if n - k == 1 && n.even? then\n puts 'NO'\n next\n end\n \n results = []\n if n.even? && k.odd? then\n results.fill(2, 0..k - 2)\n results_sum = results.reduce(0) {|sum, value| sum + value }\n results.push(n - results_sum)\n puts 'YES'\n puts results.join(' ')\n else\n results.fill(1, 0..k - 2)\n results_sum = results.reduce(0) {|sum, value| sum + value }\n results.push(n - results_sum)\n puts 'YES'\n puts results.join(' ')\n end\n\nend"}, {"source_code": "n = gets.chomp.to_i\n\ntests = []\n\nn.times do\n tests.push(gets.split(' ').map {|e| e.to_i})\nend\n\ntests.each do |test|\n n, k = test\n \n if n < k then\n puts 'NO'\n next\n end\n \n if n.odd? && k.even? then\n puts 'NO'\n next\n end\n \n if n - k == 1 && n.even? then\n puts 'NO'\n next\n end\n \n results = []\n \n results.fill(1, 0..k - 2)\n results_sum = results.reduce(0) {|sum, value| sum + value }\n results.push(n - results_sum)\n puts 'YES'\n puts results.join(' ')\nend"}], "src_uid": "6b94dcd088b0328966b54acefb5c6d22"} {"nl": {"description": "Polycarp doesn't like integers that are divisible by $$$3$$$ or end with the digit $$$3$$$ in their decimal representation. Integers that meet both conditions are disliked by Polycarp, too.Polycarp starts to write out the positive (greater than $$$0$$$) integers which he likes: $$$1, 2, 4, 5, 7, 8, 10, 11, 14, 16, \\dots$$$. Output the $$$k$$$-th element of this sequence (the elements are numbered from $$$1$$$).", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of one line containing one integer $$$k$$$ ($$$1 \\le k \\le 1000$$$).", "output_spec": "For each test case, output in a separate line one integer $$$x$$$ — the $$$k$$$-th element of the sequence that was written out by Polycarp.", "sample_inputs": ["10\n1\n2\n3\n4\n5\n6\n7\n8\n9\n1000"], "sample_outputs": ["1\n2\n4\n5\n7\n8\n10\n11\n14\n1666"], "notes": null}, "positive_code": [{"source_code": "MOD = 10 ** 9 + 7\r\ndef gs; gets.chomp; end\r\ndef gi; gets.to_i; end\r\ndef gli; gets.split.map(&:to_i); end\r\ndef cyesno(x); puts x ? :Yes : :No; end\r\ndef uyesno(x); puts x ? :YES : :NO; end\r\ndef array_to_hash_by_count(c); Hash.new(0).tap { |x| c.each { |z| x[z] += 1 } }; end\r\n\r\na = []\r\nc = 0\r\ni = 1;\r\nwhile c < 1010\r\n unless i% 3 == 0 || i % 10 == 3\r\n a << i\r\n c += 1\r\n end\r\n i += 1\r\nend\r\n\r\ngets.to_i.times do \r\n g = gets.to_i\r\n puts a[g-1]\r\nend\r\n"}, {"source_code": "# 1 2 3 4 5 6 7 8 9 1000\n# 1 2 4 5 7 8 10 11 14 1666\n\nt = gets.to_i\nk = []\n(0..t-1).each do\n x = gets.to_i\n k.push(x)\nend\n\nk.each do |i|\n j = 1\n c = 0\n while true\n if j % 3 != 0 && j % 10 != 3\n c += 1\n end\n if c == i\n puts j\n break\n end\n j += 1\n end\n # p arr\n # puts arr.last\nend"}, {"source_code": "t = gets.to_i\na = []\ni = 1\nwhile a.length() < 1001\n if i % 3 != 0 && i.to_s[-1] != \"3\"\n a << i\n end\n i += 1\nend\nwhile t > 0\n t -= 1\n n = gets.to_i\n puts a[n - 1]\nend\n"}, {"source_code": "\ndef solve(k)\n cnt = 0\n ptr = 0\n while cnt < k do\n ptr += 1\n next if ptr % 3 == 0 || ptr%10 == 3\n cnt += 1\n end\n ptr\nend\n\nt = gets.chomp.to_i\n\nt.times do\n k = gets.chomp.to_i\n puts \"#{solve(k)}\\n\"\nend\n"}], "negative_code": [{"source_code": "MOD = 10 ** 9 + 7\r\ndef gs; gets.chomp; end\r\ndef gi; gets.to_i; end\r\ndef gli; gets.split.map(&:to_i); end\r\ndef cyesno(x); puts x ? :Yes : :No; end\r\ndef uyesno(x); puts x ? :YES : :NO; end\r\ndef array_to_hash_by_count(c); Hash.new(0).tap { |x| c.each { |z| x[z] += 1 } }; end\r\n\r\na = []\r\nc = 0\r\ni = 1;\r\nwhile c < 1010\r\n unless i% 3 == 0 || i % 10 == 0\r\n a << i\r\n c += 1\r\n end\r\n i += 1\r\nend\r\n\r\ngets.to_i.times do \r\n g = gets.to_i\r\n puts a[g-1]\r\nend\r\n"}], "src_uid": "c37604d5d833a567ff284d7ce5eda059"} {"nl": {"description": "Coming up with a new problem isn't as easy as many people think. Sometimes it is hard enough to name it. We'll consider a title original if it doesn't occur as a substring in any titles of recent Codeforces problems. You've got the titles of n last problems — the strings, consisting of lowercase English letters. Your task is to find the shortest original title for the new problem. If there are multiple such titles, choose the lexicographically minimum one. Note, that title of the problem can't be an empty string.A substring s[l... r] (1 ≤ l ≤ r ≤ |s|) of string s = s1s2... s|s| (where |s| is the length of string s) is string slsl + 1... sr.String x = x1x2... xp is lexicographically smaller than string y = y1y2... yq, if either p < q and x1 = y1, x2 = y2, ... , xp = yp, or there exists such number r (r < p, r < q), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1. The string characters are compared by their ASCII codes.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 30) — the number of titles you've got to consider. Then follow n problem titles, one per line. Each title only consists of lowercase English letters (specifically, it doesn't contain any spaces) and has the length from 1 to 20, inclusive.", "output_spec": "Print a string, consisting of lowercase English letters — the lexicographically minimum shortest original title.", "sample_inputs": ["5\nthreehorses\ngoodsubstrings\nsecret\nprimematrix\nbeautifulyear", "4\naa\nbdefghijklmn\nopqrstuvwxyz\nc"], "sample_outputs": ["j", "ab"], "notes": "NoteIn the first sample the first 9 letters of the English alphabet (a, b, c, d, e, f, g, h, i) occur in the problem titles, so the answer is letter j.In the second sample the titles contain 26 English letters, so the shortest original title cannot have length 1. Title aa occurs as a substring in the first title."}, "positive_code": [{"source_code": " def nl(x)\n case x[-1]\n when \"a\"\n return (x[0...-1] + \"b\")\n when \"b\"\n return (x[0...-1] + \"c\")\n when \"c\"\n return (x[0...-1] + \"d\")\n when \"d\"\n return (x[0...-1] + \"e\")\n when \"e\"\n return (x[0...-1] + \"f\")\n when \"f\"\n return (x[0...-1] + \"g\")\n when \"g\"\n return (x[0...-1] + \"h\")\n when \"h\"\n return (x[0...-1] + \"i\")\n when \"i\"\n return (x[0...-1] + \"j\")\n when \"j\"\n return (x[0...-1] + \"k\")\n when \"k\"\n return (x[0...-1] + \"l\")\n when \"l\"\n return (x[0...-1] + \"m\")\n when \"m\"\n return (x[0...-1] + \"n\")\n when \"n\"\n return (x[0...-1] + \"o\")\n when \"o\"\n return (x[0...-1] + \"p\")\n when \"p\"\n return (x[0...-1] + \"q\")\n when \"q\"\n return (x[0...-1] + \"r\")\n when \"r\"\n return (x[0...-1] + \"s\")\n when \"s\"\n return (x[0...-1] + \"t\")\n when \"t\"\n return (x[0...-1] + \"u\")\n when \"u\"\n return (x[0...-1] + \"v\")\n when \"v\"\n return (x[0...-1] + \"w\")\n when \"w\"\n return (x[0...-1] + \"x\")\n when \"x\"\n return (x[0...-1] + \"y\")\n when \"y\"\n return (x[0...-1] + \"z\")\n when \"z\"\n return nil\n end\n end\n\n def func(mas, st,n)\n ans = []\n for s in \"a\"..\"z\" do\n t = true\n i = 0\n while t && i < n do\n if mas[i].include? (st + s) then\n t = false\n end\n i += 1\n end\n if t then \n ans << (st + s)\n end\n end\n return ans\n end\n\n n = gets.to_i\n\n names = []\n n.times do \n names << gets.chomp\n end\n\n ans = func(names,\"\",n)\n st = \"a\"\n k = 1\n while ans.empty? do\n if !st then\n k += 1\n st = k*\"a\"\n end\n ans = func(names,st,n)\n st = nl(st)\n end\n\n puts ans.min"}, {"source_code": "a = ''\nn = gets.to_i.times{ a += (gets.to_s.chomp + ' ')}\ns = 'a'\nwhile a.index(s) != nil do\n\ts.next!\nend\nputs s\n\n"}, {"source_code": "a = ''\nn = gets.to_i.times{ a += (gets.to_s.chomp + ' ')}\ns = 'a'\nwhile a.index(s) != nil do\n\ts.next!\nend\nputs s\n"}, {"source_code": "def get_str(x)\n abc = \" abcdefghijklmnopqrstuvwxyz\".split(\"\")\n sn = x.to_s 27\n r = \"\"\n sn.split(\"\").each do |l|\n r += abc[l.to_i 27]\n end\n r\nend\n\ntn = gets.chomp.to_i\ntitles = []\ntn.times do\n titles.push(gets.chomp)\nend\n\nn = 0\nwhile true do\n n+=1\n done = true\n if n%27 != 0 then\n str = get_str n\n titles.each do |ttl|\n if ttl.include? str then\n done = false\n break\n end\n end\n puts str if done\n break if done\n end\nend\n\n"}, {"source_code": "a = ''\nn = gets.to_i.times{ a += (gets.to_s.chomp + ' ')}\ns = 'a'\nwhile a.index(s) != nil do\n\ts.next!\nend\nputs s\n\n"}, {"source_code": "a = ''\nn = gets.to_i.times{ a += (gets.to_s.chomp + ' ')}\ns = 'a'\nwhile a.index(s) != nil do\n s.next!\nend\nputs s"}, {"source_code": "a = ''\nn = gets.to_i.times{ a += (gets.to_s.chomp + ' ')}\ns = 'a'\nwhile a.index(s) != nil do\n s.next!\nend\nputs s"}, {"source_code": "#!/usr/bin/ruby1.9\n\nn = STDIN.readline.to_i\nwords = []\nn.times { words.push(STDIN.readline.strip) }\n\ntitle = ''\n\nwhile true\n\tpos = title.length-1\n\twhile true\n\t\tif pos == -1\n\t\t\ttitle = (title.length+1).times.collect { 'a' }.join\n\t\t\tbreak\n\t\tend\n\t\tif title[pos] < 'z'\n\t\t\tsuffix = (title.length-1-pos).times.collect { 'a' }.join\n\t\t\ttitle = title.slice(0, pos) + title[pos].next + suffix\n\t\t\tbreak\n\t\tend\n\t\tpos -= 1\n\tend\n\n\tokay = true\n\tn.times do |i|\n\t\tif words[i].index(title) != nil\n\t\t\tokay = false\n\t\t\tbreak\n\t\tend\n\tend\n\tif okay\n\t\tputs title\n\t\texit\n\tend\nend\n\n"}, {"source_code": "#!/usr/bin/ruby1.9\n\nn = STDIN.readline.to_i\nhash = {}\nn.times do\n\tword = STDIN.readline.strip\n\t(0..word.length-1).each do |start|\n\t\t(1..word.length-start).each do |length|\n\t\t\thash[word.slice(start, length)] = true\n\t\tend\n\tend\nend\n\ntitle = 'a'\ntitle = title.next while hash[title] != nil\nputs title\n\n"}, {"source_code": "#!/usr/bin/ruby1.9\n\nn = STDIN.readline.to_i\nwords = []\nn.times { words.push(STDIN.readline.strip) }\n\ntitle = 'a'\n\nwhile true\n\tokay = true\n\tn.times do |i|\n\t\tif words[i].index(title) != nil\n\t\t\tokay = false\n\t\t\tbreak\n\t\tend\n\tend\n\tif okay\n\t\tputs title\n\t\texit\n\tend\n\n\ttitle = title.next\nend\n\n"}, {"source_code": "a = ''\nn = gets.to_i.times{ a += (gets.to_s.chomp + ' ')}\ns = 'a'\nwhile a.index(s) != nil do\n\ts.next!\nend\nputs s\n"}, {"source_code": "a = ''\nn = gets.to_i.times{ a += (gets.to_s.chomp + ' ')}\ns = 'a'\nwhile a.index(s) != nil do\n\ts.next!\nend\nputs s\n\n"}, {"source_code": "a = ''\nn = gets.to_i.times{ a += (gets.to_s.chomp + ' ')}\ns = 'a'\nwhile a.index(s) != nil do\n\ts.next!\nend\nputs s\n\n"}, {"source_code": "n=gets.to_i\ns=[]\nn.times do\n s< y.size\n 1\n else\n if x <= y then -1 else 1 end\n end\n}.uniq\nans=\"a\"\na.each{|x| \n break if x != ans\n ans.succ!\n}\nputs ans\n\n"}, {"source_code": "def titles(prefix = '', len)\n len -= 1\n block = (len == 0) ? lambda { |z| prefix + z } : lambda { |z| titles(prefix + z, len) }\n ('a'..'z').to_a.map &block\nend\n\ndef solve(used_titles)\n (1..2).each { |i|\n titles(i).flatten.each { |title|\n return title if used_titles.all? { |used_title| !used_title.include? title }\n }\n }\nend\n\nif __FILE__ == $0\n puts solve((1..gets.to_i).map { gets.chomp })\nend"}, {"source_code": "def titles(prefix = '', len)\n len -= 1\n block = (len == 0) ? lambda { |z| prefix + z } : lambda { |z| titles(prefix + z, len) }\n ('a'..'z').to_a.map &block\nend\n\ndef solve(used_titles)\n (1..3).each { |i|\n titles(i).flatten.each { |title|\n return title if used_titles.all? { |used_title| !used_title.include? title }\n }\n }\nend\n\nif __FILE__ == $0\n puts solve((1..gets.to_i).map { gets.chomp })\nend"}, {"source_code": "ns = gets.to_i.times.map { gets.chomp }\nstart = \"a\"\nwhile ns.any? { |n| n.index(start) }\n start.succ!\nend\nputs start"}, {"source_code": "a = ''\nn = gets.to_i.times{ a += (gets.to_s.chomp + ' ')}\ns = 'a'\nwhile a.index(s) != nil do\n\ts.next!\nend\nputs s\n\n"}, {"source_code": "ns = gets.to_i.times.map { gets.chomp }\nstart = \"a\"\nwhile ns.any? { |n| n.index(start) }\n start.succ!\nend\nputs start"}, {"source_code": "a = ''\nn = gets.to_i.times{ a += (gets.to_s.chomp + ' ')}\ns = 'a'\nwhile a.index(s) != nil do\n\ts.next!\nend\nputs s\n\n"}], "negative_code": [{"source_code": "def get_str(x)\n abc = \" abcdefghijklmnopqrstuvwxyz\".split(\"\")\n sn = x.to_s 27\n r = \"\"\n sn.split(\"\").each do |l|\n r += abc[l.to_i 27]\n end\n r\nend\n\nn = gets.chomp.to_i\ntitles = []\nn.times do\n titles.push(gets.chomp)\nend\n\nn = 0\nwhile true do\n n+=1\n done = true\n if n%27 != 0 then\n str = get_str n\n titles.each do |ttl|\n puts ttl\n if ttl.include? str then\n done = false\n break\n end\n end\n puts str if done\n break if done\n end\nend\n\n"}, {"source_code": "#!/usr/bin/ruby\n\nn = STDIN.readline.to_i\n\ntitles = []\nn.times { titles.push(STDIN.readline.strip) }\n\nanswer = ''\n\nwhile true\n\t('a'..'z').each do |ch|\n\t\tcandidate = answer+ch\n\t\tokay = true\n\t\ttitles.each do |title|\n\t\t\tif title.index(candidate) != nil\n\t\t\t\tokay = false\n\t\t\t\tbreak\n\t\t\tend\n\t\tend\n\t\tif okay\n\t\t\tputs candidate\n\t\t\texit\n\t\tend\n\tend\n\tanswer += 'a'\nend\n\n"}, {"source_code": "n = gets.to_i\n\ns = []\nn.times do |i|\n\ts[i] = gets\nend\n\n\nalphabet = \"a b c d e f g h i j k l m n o p q r s t u v w x y z\".split(' ')\n\nmyname = \"idk\"\nfound = false\n\nallstring = s.join('')\n\nalphabet.each do |let|\n\tif !allstring.include?(let)\n\t\tmyname = let\n\t\tfound = true\n\t\tbreak\n\tend\nend\n\n\nmyname = \"a\" if !found\nwhile !found do\n\talphabet.each do |let|\n\t\t# p myname + let\n\t\tif !allstring.include?(myname + let)\n\t\t\tmyname = myname + let\n\t\t\tfound = true\n\t\t\tbreak\n\t\tend\n\n\t\t# myname += \"a\" if let = \"z\" && !found\n\tend\n\tmyname += \"a\" if !found\nend\n\n\np myname"}, {"source_code": "ns = gets.to_i.times.map { gets.chomp }\nstart = \"a\"\nwhile ns.any? { |n| n.index(start) }\n start.succ!\nend\np start"}], "src_uid": "58fa5c2f270e2c34e8f9671d5ffdb9c8"} {"nl": {"description": "You are given a keyboard that consists of $$$26$$$ keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.You have to type the word $$$s$$$ on this keyboard. It also consists only of lowercase Latin letters.To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions $$$8$$$, $$$5$$$, $$$12$$$ and $$$15$$$, respectively. Therefore, it will take $$$|5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13$$$ units of time to type the word \"hello\". Determine how long it will take to print the word $$$s$$$.", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\leq t \\leq 1000$$$) — the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. The first line of a description contains a keyboard — a string of length $$$26$$$, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard. The second line of the description contains the word $$$s$$$. The word has a length from $$$1$$$ to $$$50$$$ letters inclusive and consists of lowercase Latin letters.", "output_spec": "Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word $$$s$$$ on the given keyboard.", "sample_inputs": ["5\nabcdefghijklmnopqrstuvwxyz\nhello\nabcdefghijklmnopqrstuvwxyz\ni\nabcdefghijklmnopqrstuvwxyz\ncodeforces\nqwertyuiopasdfghjklzxcvbnm\nqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\nqwertyuiopasdfghjklzxcvbnm\nabacaba"], "sample_outputs": ["13\n0\n68\n0\n74"], "notes": null}, "positive_code": [{"source_code": "STDIN.read\r\n .split(/\\n/)\r\n .drop(1)\r\n .map!(&:strip)\r\n .each_slice(2) do |alphabet, word|\r\n puts word\r\n .split(\"\")\r\n .map{ |c| alphabet.index(c) }\r\n .each_cons(2)\r\n .map{|a, b| (a - b).abs}\r\n .sum\r\n end\r\n"}, {"source_code": "t = gets.to_i\r\nt.times do\r\n dist = {}\r\n keys = gets.chomp\r\n s = gets.chomp\r\n keys.each_char.with_index do |c, i|\r\n dist[c] = i\r\n end\r\n ans = 0\r\n s.chars.each_cons(2) do |c1, c2|\r\n ans += (dist[c1] - dist[c2]).abs\r\n end\r\n puts ans\r\nend"}, {"source_code": "t = gets.to_i\nt.times do\n dist = {}\n keys = gets.chomp\n s = gets.chomp\n keys.each_char.with_index do |c, i|\n dist[c] = i\n end\n ans = 0\n s.chars.each_cons(2) do |c1, c2|\n ans += (dist[c1] - dist[c2]).abs\n end\n puts ans\nend"}, {"source_code": "n = gets.to_i\na = []\n\ndef find_ans(s,w)\n i = 0\n time = 0\n while (i < w.length - 1)\n if (w[i] == w[i+1])\n time += 0\n else\n time += (s.index(w[i]) - s.index(w[i+1])).abs\n end\n i += 1\n end\n time\nend\n \n(0..n-1).each do |x|\n\ts = gets.chomp\n w = gets.chomp\n\ta.append(find_ans(s,w))\nend\n \n \na.each do |x|\n p x\nend"}, {"source_code": "gets.to_i.times do\r\n xs = gets.chomp.chars\r\n puts gets.chomp.chars.each_cons(2).map { |a, b| (xs.index(a) - xs.index(b)).abs }.sum\r\nend"}, {"source_code": "for _ in 0...gets.chomp.to_i\r\n keyboard = gets.chomp\r\n word = gets.chomp\r\n time = 0\r\n if word.length() <= 1\r\n puts time\r\n else\r\n for i in 0...(word.length() - 1)\r\n time += (keyboard.index(word[i]) - keyboard.index(word[i + 1])).abs\r\n end\r\n puts time\r\n end\r\nend"}, {"source_code": "arr = []\r\nt = gets.chomp.to_i\r\nindex = 0\r\nwhile t>index\r\n s1 = gets.chomp.chars\r\n s2 = gets.chomp.chars\r\n kol_s2 = s2.length.to_i-1\r\n summa =0;\r\n i=1;\r\n while kol_s2>=1\r\n if s2.length>1\r\n summa = summa + ((s1.index(s2[i]))-(s1.index(s2[i-1]))).abs\r\n kol_s2-=1\r\n i+=1\r\n else\r\n summa = 0\r\n end\r\n end\r\n arr[index] = summa\r\n index+=1\r\nend\r\narr.each{|a| puts a}"}, {"source_code": "gets.to_i.times do\r\n a=gets.chomp.chars\r\n h={}\r\n 26.times do |i|\r\n h[a[i]]=i\r\n end\r\n c=0\r\n s=gets.chomp.chars\r\n (s.size-1).times do |i|\r\n c+=(h[s[i+1]]-h[s[i]]).abs\r\n end\r\n puts c\r\nend\r\n"}], "negative_code": [], "src_uid": "7f9853be7ac857bb3c4eb17e554ad3f1"} {"nl": {"description": "Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on n·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: 2, 7, 4. 1, 7, 3; 5, 4, 2; 1, 3, 5; 3, 1, 2, 4; 5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?", "input_spec": "The first input file line contains a number n (2 ≤ n ≤ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on n·(n - 1) / 2 lines. Each set starts with the number ki (2 ≤ ki ≤ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≤ aij ≤ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.", "output_spec": "Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.", "sample_inputs": ["4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "3\n2 1 2\n2 1 3\n2 2 3"], "sample_outputs": ["1 7 \n2 2 4 \n2 1 3 \n1 5", "3 7 8 9 \n2 6 100 \n1 1 \n1 2", "1 1 \n1 2 \n1 3"], "notes": null}, "positive_code": [{"source_code": "require 'set'\nn = gets.to_i\nm = n * (n - 1) / 2\nb = m.times.map{gets.split.map(&:to_i)[1..-1].to_set}\nif n == 2\n\ta = [b[0].to_a[0..0], b[0].to_a[1..-1]]\nelse\n\ta = []\n\t1.upto(b.size - 1) do |i|\n\t\tunless (b[0] & b[i]).empty?\n\t\t\ta << (b[0] & b[i])\n\t\t\tbreak\n\t\tend\n\tend\n\tb.each do |c|\n\t\tunless (c & a[0]).empty?\n\t\t\ta << (c - a[0])\n\t\tend\n\tend\nend\na.each do |c|\n\tputs \"#{c.size} #{c.to_a * ' '}\"\nend\n\n"}, {"source_code": "\ndef and_(x,y_)\n b=0\n res=[]\n y=y_+[9999999999]\n x.length.times{|a|\n while x[a]>y[b]\n b+=1\n end\n if x[a]==y[b] then\n res.push(x[a])\n end\n }\n return res\nend\n\nsets=[]\nn=gets.to_i\n(n*(n-1)/2).times{\n k=gets.split.map(&:to_i)\n k.shift\n k.sort!\n sets.push(k)\n\n}\nif n==2 then\n printf \"1 %d\\n\",sets[0][0]\n printf \"%d \",sets[0].length-1\n 1.upto(sets[0].length-1){|i|\n printf \"%d \",sets[0][i]\n }\n puts\n exit\nend\nans={}\nloop do\n i=rand(sets.length)\n j=rand(sets.length)\n if i==j then\n next\n end\n t=and_(sets[i],sets[j])\n\n if t.length==0 then\n next\n end\n ans[t]=1\n\n if ans.length==n then\n break\n end\nend\nans=ans.keys\nans.each{|a|\n printf \"%d \",a.length\n a.each{|x|\n printf \"%d \",x\n }\n puts\n}\n"}, {"source_code": "#!/usr/bin/env ruby\n# -*- coding: utf-8 -*-\n\nN = gets.to_i\nh = Hash.new\n\n(N * (N-1) / 2).times do |i|\n k, *as = gets.chomp.split.map{|e| e.to_i }\n\n as.each {|a|\n h[a] ||= []\n h[a].push i\n }\nend\n\nwarn h\n\nnh = Hash.new\nh.each do |k, v|\n nh[v] ||= []\n nh[v].push k\nend\n\nif N != 2\n nh.each do |k,v|\n puts \"#{v.size} #{v.join(\" \")}\"\n end\nelse\n tmp = nh[[0]]\n puts \"1 #{tmp.pop}\"\n puts \"#{tmp.size} #{tmp.join(\" \")}\"\nend\n\n"}, {"source_code": "N = gets.to_i\n\nh = Hash.new\n(N * (N - 1) / 2).times do |i|\n k, *as = gets.chomp.split.map{|e| e.to_i}\n as.each {|a|\n h[a] ||= []\n h[a].push i\n }\nend\n\nnew_hash = Hash.new\nh.each {|k, v|\n new_hash[v] ||= []\n new_hash[v].push k\n}\n\nif N != 2\n new_hash.each {|k, v|\n puts \"#{v.size} #{v.join(\" \")}\"\n }\nelse\n tmp = new_hash[[0]]\n puts \"1 #{tmp.pop}\"\n puts \"#{tmp.size} #{tmp.join(\" \")}\"\nend\n\n\n"}, {"source_code": "#=================================\nrequire 'prime'\ninclude Math\n\nif ARGV.include?(\"ONLINE_JUDGE\")\n\t$stdin = File.open(\"./input.txt\", \"r\")\n\tARGV.delete(\"ONLINE_JUDGE\")\nend\n#=================================\n\nn=gets.to_i\n\nsets=[]\nall_sets=n*(n-1)/2\n\nall_sets.times{\n\tset=gets.split.map(&:to_i)[1..-1]\n\tsets << set.sort\n}\n\nsets.sort!\n\nif all_sets==1\n\tputs \"1 #{sets[0][0]}\"\n\tputs \"#{sets[0][1..-1].size} #{sets[0][1..-1].join(\" \")}\"\n\texit\nend\n\nresulting_sets=Hash.new(nil)\nfor i in 0...sets.size-1\n\ta, b=sets[i], sets[i+1]\n\tunion=a&b\n\t\n\tif !union.empty?\n\t\ttmp_sets=[a-union, b-union, union]\n\t\t\n\t\ttmp_sets.each{|s| resulting_sets[s.sort]=1 if !resulting_sets.has_key?(s.sort)}\n\tend\nend\n\nresulting_sets.keys.each{|v|\n\tputs \"#{v.size} #{v.join(\" \")}\"\n}\n"}, {"source_code": "N = gets.to_i\n \nh = Hash.new\n(N * (N - 1) / 2).times do |i|\n k, *as = gets.chomp.split.map{|e| e.to_i}\n as.each {|a|\n h[a] ||= []\n h[a].push i\n }\nend\n \nnew_hash = Hash.new\nh.each {|k, v|\n new_hash[v] ||= []\n new_hash[v].push k\n}\n \nif N != 2\n new_hash.each {|k, v|\n puts \"#{v.size} #{v.join(\" \")}\"\n }\nelse\n tmp = new_hash[[0]]\n puts \"1 #{tmp.pop}\"\n puts \"#{tmp.size} #{tmp.join(\" \")}\"\nend"}, {"source_code": "n = gets.to_i\nh = Hash.new { |hash, key| hash[key] = [] }\n(n * (n - 1) / 2).times do |i|\n t, *v = gets.split.map(&:to_i)\n v.each do |x|\n h[x].push << i\n end\nend\n\nhash2 = Hash.new { |hash, key| hash[key] = [] }\nh.each do |k, v|\n hash2[v] << k\nend\n\nif n != 2\n hash2.each do |k, v|\n puts \"#{v.size} #{v.join(\" \")}\"\n end\nelse\n tmp = hash2[[0]]\n puts \"1 #{tmp.pop}\"\n puts \"#{tmp.size} #{tmp.join(\" \")}\"\nend"}], "negative_code": [{"source_code": "\ndef and_(x,y_)\n b=0\n res=[]\n y=y_+[9999999999]\n x.length.times{|a|\n while x[a]>y[b]\n b+=1\n end\n if x[a]==y[b] then\n res.push(x[a])\n end\n }\n return res\nend\n\nsets=[]\nn=gets.to_i\n(n*(n-1)/2).times{\n k=gets.split.map(&:to_i)\n k.shift\n k.sort!\n sets.push(k)\n\n}\nif n==2 then\n printf \"1 %d\\n\",sets[0][1]\n printf \"%d \",sets[0].length-1\n 1.upto(sets[0].length-1){|i|\n printf \"%d \",sets[0][i]\n }\n puts\n exit\nend\nans={}\nloop do\n i=rand(sets.length)\n j=rand(sets.length)\n if i==j then\n next\n end\n t=and_(sets[i],sets[j])\n\n if t.length==0 then\n next\n end\n ans[t]=1\n\n if ans.length==n then\n break\n end\nend\nans=ans.keys\nans.each{|a|\n printf \"%d \",a.length\n a.each{|x|\n printf \"%d \",x\n }\n puts\n}\n"}, {"source_code": "def and(x,y_)\n b=0\n res=[]\n y=y_+[9999999999]\n x.length.times{|a|\n while x[a]y[b]\n b+=1\n end\n if x[a]==y[b] then\n res.push(x[a])\n end\n }\n return res\nend\n\nsets=[]\nn=gets.to_i\n(n*(n-1)/2).times{\n k=gets.split.map(&:to_i)\n k.shift\n k.sort!\n sets.push(k)\n\n}\nif n==2 then\n printf \"1 %d\\n\",sets[0][1]\n printf \"1 %d\\n\",sets[0][0]\n exit\nend\nans={}\nloop do\n i=rand(sets.length)\n j=rand(sets.length)\n if i==j then\n next\n end\n t=and_(sets[i],sets[j])\n\n if t.length==0 then\n next\n end\n ans[t]=1\n\n if ans.length==n then\n break\n end\nend\nans=ans.keys\nans.each{|a|\n printf \"%d \",a.length\n a.each{|x|\n printf \"%d \",x\n }\n puts\n}\n"}, {"source_code": "\ndef and_(x,y_)\n b=0\n res=[]\n y=y_+[9999999999]\n x.length.times{|a|\n while x[a]>y[b]\n b+=1\n end\n if x[a]==y[b] then\n res.push(x[a])\n end\n }\n return res\nend\n\nsets=[]\nn=gets.to_i\n(n*(n-1)/2).times{\n k=gets.split.map(&:to_i)\n k.shift\n k.sort!\n sets.push(k)\n\n}\nif n==2 then\n printf \"1 %d\\n\",sets[0][1]\n printf \"2 %d\\n\",sets[0][0]\n exit\nend\nans={}\nloop do\n i=rand(sets.length)\n j=rand(sets.length)\n if i==j then\n next\n end\n t=and_(sets[i],sets[j])\n\n if t.length==0 then\n next\n end\n ans[t]=1\n\n if ans.length==n then\n break\n end\nend\nans=ans.keys\nans.each{|a|\n printf \"%d \",a.length\n a.each{|x|\n printf \"%d \",x\n }\n puts\n}\n"}, {"source_code": "#=================================\nrequire 'prime'\ninclude Math\n\nif ARGV.include?(\"ONLINE_JUDGE\")\n\t$stdin = File.open(\"./input.txt\", \"r\")\n\tARGV.delete(\"ONLINE_JUDGE\")\nend\n#=================================\n\nn=gets.to_i\n\nsets=[]\nall_sets=n*(n-1)/2\nall_sets.times{\n\tset=gets.split.map(&:to_i)[1..-1]\n\tsets << set.sort\n}\n\nsets.sort!\n\nresulting_sets=Hash.new(nil)\nfor i in 0...sets.size-1\n\ta, b=sets[i], sets[i+1]\n\tunion=a&b\n\t\n\tif !union.empty?\n\t\ttmp_sets=[a-union, b-union, union]\n\t\t\n\t\ttmp_sets.each{|s| resulting_sets[s]=1 if !resulting_sets.has_key?(s)}\n\tend\nend\n\nresulting_sets.keys.each{|v|\n\tputs \"#{v.size} #{v.join(\" \")}\"\n}\n"}, {"source_code": "#=================================\nrequire 'prime'\ninclude Math\n\nif ARGV.include?(\"ONLINE_JUDGE\")\n\t$stdin = File.open(\"./input.txt\", \"r\")\n\tARGV.delete(\"ONLINE_JUDGE\")\nend\n#=================================\n\nn=gets.to_i\n\nsets=[]\nall_sets=n*(n-1)/2\nall_sets.times{\n\tset=gets.split.map(&:to_i)[1..-1]\n\tsets << set.sort\n}\n\nsets.sort!\n\nresulting_sets=Hash.new(nil)\nfor i in 0...sets.size-1\n\ta, b=sets[i], sets[i+1]\n\tunion=a&b\n\t\n\tif !union.empty?\n\t\ttmp_sets=[a-union, b-union, union]\n\t\t\n\t\ttmp_sets.each{|s| resulting_sets[s.sort]=1 if !resulting_sets.has_key?(s.sort)}\n\tend\nend\n\nresulting_sets.keys.each{|v|\n\tputs \"#{v.size} #{v.join(\" \")}\"\n}\n"}, {"source_code": "#=================================\nrequire 'prime'\ninclude Math\n\nif ARGV.include?(\"ONLINE_JUDGE\")\n\t$stdin = File.open(\"./input.txt\", \"r\")\n\tARGV.delete(\"ONLINE_JUDGE\")\nend\n#=================================\n\nn=gets.to_i\n\nsets=[]\nall_sets=n*(n-1)/2\n\nall_sets.times{\n\tset=gets.split.map(&:to_i)[1..-1]\n\tsets << set.sort\n}\n\nsets.sort!\n\nif all_sets==1\n\tputs \"1 #{sets[0][0]}\"\n\tputs \"1 #{sets[0][1]}\"\n\texit\nend\n\nresulting_sets=Hash.new(nil)\nfor i in 0...sets.size-1\n\ta, b=sets[i], sets[i+1]\n\tunion=a&b\n\t\n\tif !union.empty?\n\t\ttmp_sets=[a-union, b-union, union]\n\t\t\n\t\ttmp_sets.each{|s| resulting_sets[s.sort]=1 if !resulting_sets.has_key?(s.sort)}\n\tend\nend\n\nresulting_sets.keys.each{|v|\n\tputs \"#{v.size} #{v.join(\" \")}\"\n}\n"}, {"source_code": "#=================================\nrequire 'prime'\ninclude Math\n\nif ARGV.include?(\"ONLINE_JUDGE\")\n\t$stdin = File.open(\"./input.txt\", \"r\")\n\tARGV.delete(\"ONLINE_JUDGE\")\nend\n#=================================\n\nn=gets.to_i\n\nsets=[]\nall_sets=n*(n-1)/2\n\nall_sets.times{\n\tset=gets.split.map(&:to_i)[1..-1]\n\tsets << set.sort\n}\n\nsets.sort!\n\nif all_sets==1\n\tputs \"1 #{sets[0][0]}\"\n\tputs \"1 #{sets[0][1..-1]}\"\n\texit\nend\n\nresulting_sets=Hash.new(nil)\nfor i in 0...sets.size-1\n\ta, b=sets[i], sets[i+1]\n\tunion=a&b\n\t\n\tif !union.empty?\n\t\ttmp_sets=[a-union, b-union, union]\n\t\t\n\t\ttmp_sets.each{|s| resulting_sets[s.sort]=1 if !resulting_sets.has_key?(s.sort)}\n\tend\nend\n\nresulting_sets.keys.each{|v|\n\tputs \"#{v.size} #{v.join(\" \")}\"\n}\n"}, {"source_code": "#=================================\nrequire 'prime'\ninclude Math\n\nif ARGV.include?(\"ONLINE_JUDGE\")\n\t$stdin = File.open(\"./input.txt\", \"r\")\n\tARGV.delete(\"ONLINE_JUDGE\")\nend\n#=================================\n\nn=gets.to_i\n\nsets=[]\nall_sets=n*(n-1)/2\n\nall_sets.times{\n\tset=gets.split.map(&:to_i)[1..-1]\n\tsets << set.sort\n}\n\nsets.sort!\n\nif all_sets==1\n\tputs \"1 #{sets[0][0]}\"\n\tputs \"1 #{sets[0][1..-1].join(\" \")}\"\n\texit\nend\n\nresulting_sets=Hash.new(nil)\nfor i in 0...sets.size-1\n\ta, b=sets[i], sets[i+1]\n\tunion=a&b\n\t\n\tif !union.empty?\n\t\ttmp_sets=[a-union, b-union, union]\n\t\t\n\t\ttmp_sets.each{|s| resulting_sets[s.sort]=1 if !resulting_sets.has_key?(s.sort)}\n\tend\nend\n\nresulting_sets.keys.each{|v|\n\tputs \"#{v.size} #{v.join(\" \")}\"\n}\n"}, {"source_code": "require 'set'\n\nn = gets.to_i\nsets = []\n(n * (n - 1) / 2).times do\n t, *x = gets.split.map(&:to_i) \n sets << Set.new(x)\nend\n\n$EMPTY = Set.new\n\nsol = Array.new\nfor i in 0...sets.size\n next if sets[i].empty?\n \n for j in (i + 1)...sets.size\n next if sets[j].empty?\n \n t = sets[i] & sets[j]\n unless t.empty?\n sol << t\n sets[i] -= t\n sets[j] -= t\n sol << sets[i] unless sets[i].empty?\n sol << sets[j] unless sets[j].empty?\n \n for k in (j + 1)...sets.size\n next if sets[k].empty?\n sets[k] -= sets[i]\n sets[k] -= sets[j]\n tt = sets[k] & t\n unless tt.empty?\n uu = sets[k] - t\n sol << sets[k] unless sets[k].empty?\n sets[k] = $EMPTY\n end\n end\n sets[i] = $EMPTY\n sets[j] = $EMPTY\n end\n end\nend\n\nsol.each do |x|\n print \"#{x.size}\"\n x.each do |t|\n print \" #{t}\"\n end\n puts \"\"\nend\n"}], "src_uid": "bde894dc01c65da67d32c716981926f6"} {"nl": {"description": "The problem describes the properties of a command line. The description somehow resembles the one you usually see in real operating systems. However, there are differences in the behavior. Please make sure you've read the statement attentively and use it as a formal document.In the Pindows operating system a strings are the lexemes of the command line — the first of them is understood as the name of the program to run and the following lexemes are its arguments. For example, as we execute the command \" run.exe one, two . \", we give four lexemes to the Pindows command line: \"run.exe\", \"one,\", \"two\", \".\". More formally, if we run a command that can be represented as string s (that has no quotes), then the command line lexemes are maximal by inclusion substrings of string s that contain no spaces.To send a string with spaces or an empty string as a command line lexeme, we can use double quotes. The block of characters that should be considered as one lexeme goes inside the quotes. Embedded quotes are prohibited — that is, for each occurrence of character \"\"\" we should be able to say clearly that the quotes are opening or closing. For example, as we run the command \"\"run.exe o\" \"\" \" ne, \" two . \" \" \", we give six lexemes to the Pindows command line: \"run.exe o\", \"\" (an empty string), \" ne, \", \"two\", \".\", \" \" (a single space).It is guaranteed that each lexeme of the command line is either surrounded by spaces on both sides or touches the corresponding command border. One of its consequences is: the opening brackets are either the first character of the string or there is a space to the left of them.You have a string that consists of uppercase and lowercase English letters, digits, characters \".,?!\"\" and spaces. It is guaranteed that this string is a correct OS Pindows command line string. Print all lexemes of this command line string. Consider the character \"\"\" to be used only in order to denote a single block of characters into one command line lexeme. In particular, the consequence is that the given string has got an even number of such characters.", "input_spec": "The single line contains a non-empty string s. String s consists of at most 105 characters. Each character is either an uppercase or a lowercase English letter, or a digit, or one of the \".,?!\"\" signs, or a space. It is guaranteed that the given string is some correct command line string of the OS Pindows. It is guaranteed that the given command line string contains at least one lexeme.", "output_spec": "In the first line print the first lexeme, in the second line print the second one and so on. To make the output clearer, print the \"<\" (less) character to the left of your lexemes and the \">\" (more) character to the right. Print the lexemes in the order in which they occur in the command. Please, follow the given output format strictly. For more clarifications on the output format see the test samples.", "sample_inputs": ["\"RUn.exe O\" \"\" \" 2ne, \" two! . \" \"", "firstarg second \"\""], "sample_outputs": ["<RUn.exe O>\n<>\n< 2ne, >\n<two!>\n<.>\n< >", "<firstarg>\n<second>\n<>"], "notes": null}, "positive_code": [{"source_code": "str = gets\nregsplit = str.scan(/(\\\"[^\\\"\"]*\\\"|[^\\\"\\s]+)/).flatten\nlex = []\nlex = regsplit.collect do |e|\n if e[0] == '\"' then\n e = \"<#{e[1,e.length-2]}>\"\n else\n e = \"<#{e}>\"\n end\nend\nputs lex"}, {"source_code": "s = gets.chomp\nquote = false\nt = \"\"\ns.each_char do |c|\n if c == '\"'\n quote = !quote\n if quote == false\n puts \"<#{t}>\"\n t=''\n end\n else\n if quote == false\n if c == ' '\n puts \"<#{t}>\" if t!=''\n t=\"\"\n else\n t<\" if t.size > 0"}, {"source_code": "class Command\n attr_accessor :commands\n\n def initialize\n @commands = []\n end\n\n def read_commands command_string\n this_lexume = ''\n in_lexume = false\n block_mode = false\n command_string.each_char do |c|\n\n # Handle opening block\n if not in_lexume\n # New lexume if the char is not a space\n if c != ' '\n in_lexume = true\n block_mode = (c == '\"')\n this_lexume = ''\n this_lexume << c if not block_mode\n end\n next\n end\n\n # Handle closing block\n if in_lexume\n if block_mode and c == '\"'\n in_lexume = false\n end\n\n if not block_mode and c == ' '\n in_lexume = false\n end\n \n if not in_lexume\n @commands.push(this_lexume)\n next\n end\n\n end\n\n # Add char to this lexume\n this_lexume << c if in_lexume\n end\n\n # Handle the last lexume\n @commands.push(this_lexume) if in_lexume\n end\n\n def print_commands\n @commands.each do |command|\n puts \"<#{command}>\"\n end\n end\nend\n\ndef main\n input = gets.chomp\n c = Command.new\n c.read_commands(input)\n c.print_commands\nend\n\nmain"}, {"source_code": "class Command\n attr_accessor :commands\n\n def initialize\n @commands = []\n end\n\n def read_commands command_string\n this_lexume = ''\n in_lexume = false\n block_mode = false\n command_string.each_char do |c|\n\n # Handle opening block\n if not in_lexume\n # New lexume if the char is not a space\n if c != ' '\n in_lexume = true\n block_mode = (c == '\"')\n this_lexume = ''\n this_lexume << c if not block_mode\n end\n next\n end\n\n # Handle closing block\n if in_lexume\n in_lexume = false if block_mode and c == '\"'\n in_lexume = false if not block_mode and c == ' '\n \n if in_lexume # If still in lexume, append the char\n this_lexume << c\n else # Else, save this lexume\n @commands.push(this_lexume)\n next\n end\n\n end\n end\n\n # Handle the last lexume\n @commands.push(this_lexume) if in_lexume\n end\n\n def print_commands\n @commands.each do |command|\n puts \"<#{command}>\"\n end\n end\nend\n\ndef main\n input = gets.chomp\n c = Command.new\n c.read_commands(input)\n c.print_commands\nend\n\nmain"}, {"source_code": "class Command\n attr_accessor :commands\n\n def initialize\n @commands = []\n end\n\n def read_commands command_string\n this_lexume = ''\n in_lexume = false\n quote_mode = false\n command_string.each_char do |c|\n\n # Handle opening block\n if not in_lexume\n # New lexume if the char is not a space\n if c != ' '\n in_lexume = true\n quote_mode = (c == '\"')\n this_lexume = ''\n this_lexume << c if not quote_mode\n end\n next\n end\n\n # Handle closing block\n if in_lexume\n in_lexume = false if quote_mode and c == '\"'\n in_lexume = false if not quote_mode and c == ' '\n \n if in_lexume # If still in lexume, append the char\n this_lexume << c\n else # Else, save this lexume\n @commands.push(this_lexume)\n next\n end\n\n end\n end\n\n # Handle the last lexume\n @commands.push(this_lexume) if in_lexume\n end\n\n def print_commands\n @commands.each do |command|\n puts \"<#{command}>\"\n end\n end\nend\n\ndef main\n input = gets.chomp\n c = Command.new\n c.read_commands(input)\n c.print_commands\nend\n\nmain"}, {"source_code": "class B\n def initialize\n str = $stdin.gets.chomp\n list = parse(str)\n\n list.each do |elem|\n puts \"<#{elem}>\"\n end\n end\n\n def parse(str)\n list = []\n elem = \"\"\n elem2 = \"\"\n flag = false\n str.each_char do |ch|\n if ch == '\"'\n flag ^= true\n unless flag\n list << elem\n elem = \"\"\n end\n next\n end\n\n if ch != ' ' && !flag\n elem2 += ch\n elsif !flag\n list << elem2 if elem2.size > 0\n elem2 = \"\"\n end\n\n if flag && ch != '\"'\n elem += ch\n next\n end\n end\n\n list << elem2 if elem2.size > 0\n list\n end\nend\n\nb = B.new"}, {"source_code": "def parse_command(command)\n regex = /\"(.*?)\"|\\s*([a-zA-Z0-9.,?!]+)\\s*/\n command.scan(regex).map { |quoted, spaced| quoted || spaced }\nend\n\ncommand = STDIN.read\nlexemes = parse_command(command)\nlexemes.each { |lexeme| puts \"<#{lexeme}>\" }\n"}, {"source_code": "s = gets.chomp\n\nstart = s.length\nfor i in 0...s.length\n if s[i] != \" \"\n start = i\n break\n end\nend\n\ni = start\nif s.length - start > 1\n while i < s.length \n if s[i] != \" \"\n if s[i] == \"\\\"\"\n #for j in i+1...s.length\n j = i + 1\n while j < s.length\n if s[j] == \"\\\"\"\n stop = j \n if start + 1 == stop\n print \"<>\\n\"\n else\n print \"<\", s[start+1..stop-1], \">\\n\"\n end\n start = j + 1\n break\n elsif j == s.length - 1\n print \"<\", s[start], \">\\n\"\n start += 1\n break\n end \n j += 1 \n end\n else\n #for j in i+1...s.length\n if i == s.length - 1\n print \"<\", s[i], \">\\n\"\n start = s.length\n else\n j = i + 1\n while j < s.length\n if s[j] == \" \"\n stop = j\n print \"<\", s[start..stop-1], \">\\n\"\n start = j + 1\n break\n elsif j == s.length - 1 && s[j] != \" \"\n print \"<\", s[start..j], \">\\n\"\n start = j + 1\n break\n end\n j += 1 \n end\n end\n end\n else \n start += 1 \n end\n i = start\n end\nelse\n print \"<\", s[start], \">\\n\"\nend\n\n"}, {"source_code": "s = gets.chomp\n\ni = 0\nstate = 0\nwhile i < s.length\n if state == 2\n if s[i].ord == 34\n print \">\\n\"\n state = 0\n else\n print s[i]\n end\n elsif state == 0\n if s[i].ord == 34\n state = 2\n print \"<\"\n elsif s[i].ord == 32\n else\n state = 1\n print \"<\"\n print s[i]\n end\n else\n if s[i].ord == 34\n state = 2\n print \">\\n\"\n elsif s[i].ord == 32\n print \">\\n\"\n state = 0\n else\n print s[i]\n end\n end\n i += 1\nend\nif state == 1\n print \">\\n\"\n state = 0\nend"}, {"source_code": "def output(str)\n puts \"<#{str}>\"\nend\n\n(\" \" << gets).split('\"').each_with_index do |str, i|\n if i.even?\n str.split.each(&method(:output))\n else\n output(str)\n end\nend\n"}, {"source_code": "str=gets.chomp\nq_flag=false\nbuf=\"\"\nstr.each_char do |c| \n if c=='\"' then\n if q_flag then\n print '<'\n print buf\n puts '>'\n buf=\"\"\n end \n q_flag=!q_flag\n next\n end\n if q_flag \n buf=buf+c\n else\n if c==' ' && !buf.empty? then\n print '<'\n print buf\n puts '>'\n buf=\"\"\n end\n if c!=' ' \n buf=buf+c\n end\n end \nend\nif !buf.empty? then\n print '<'\n print buf\n puts '>'\nend"}, {"source_code": "str = gets.chomp\n\n\nstr.scan(/(?:\"(?:\\\\.|[^\"])*\"|[^\" ])+/).each do |arg|\n puts \"<#{arg.gsub /\"/, ''}>\"\nend\n"}, {"source_code": "result = []\ni = 0\nqmode = false\n(ARGF.readlines.last || '').strip.each_char do |c|\n\tresult[i] = result[i] || ''\n\tcase c\n\twhen ' ' then\n\tif qmode \n\t\t\tresult[i] << c \n\t\telse\n\t\t\ti+=1 unless result[i].empty?\n\t\tend\t\n\twhen '\"' then\n\t\tqmode = !qmode\n\t\ti+=1 unless (qmode && result[i].empty?)\n\telse\n\t\tresult[i] << c\n\tend\nend\n\n\nputs result.map{|x| '<'+x+'>'}\n"}, {"source_code": "s = gets.chomp\ns += ' '\ns.split(/\"/).each_with_index do |l, i|\n if i%2 == 1\n puts \"<#{l}>\"\n else\n l.split(/ /).each do |w|\n puts \"<#{w}>\" unless w.size == 0\n end\n end\nend"}, {"source_code": "str = gets.chomp\nts = []\ns = 0\np = ' '\nword = false\nfor i in 0...str.size\n if word\n if str[i] == p\n ts << str[s...i]\n word = false\n end\n elsif str[i] != ' '\n if str[i] == '\"'\n s = i + 1\n p = '\"'\n else\n s = i\n p = ' '\n end\n word = true\n end\nend\nts << str[s...str.size] if word\nputs(ts.map { |t| \"<#{t}>\" })"}, {"source_code": "str = gets.strip\nlexems = []\nstate = :init\nlexem = ''\nstr.chars.each do |char|\n case char\n when ' '\n if state == :quote_lexem\n lexem += char\n else\n state = :end_lexem\n lexems << lexem unless lexem.empty?\n lexem = ''\n end\n when '\"'\n if state == :quote_lexem\n state = :end_lexem\n lexems << lexem\n lexem = ''\n else\n state = :quote_lexem\n end\n else\n lexem += char\n end\nend\nlexems << lexem unless lexem.empty?\n\nlexems.each do |lexem|\n puts '<'+lexem+'>'\nend\n"}], "negative_code": [{"source_code": "s = gets.chomp\nquote = false\nt = \"\"\ns.each_char do |c|\n if c == '\"'\n quote = !quote\n if quote == false\n puts \"<#{t}>\"\n t=''\n end\n else\n if quote == false\n if c == ' '\n puts \"<#{t}>\" if t!=''\n t=\"\"\n else\n t<\"\n end\n end\n\n def parse(str)\n list = []\n elem = \"\"\n elem2 = \"\"\n flag = false\n str.each_char do |ch|\n if ch == '\"'\n flag ^= true\n unless flag\n list << elem\n elem = \"\"\n end\n end\n\n if ch != ' ' && ch != '\"' && !flag\n elem2 += ch\n else\n list << elem2 if elem2.size > 0\n elem2 = \"\"\n end\n\n if flag && ch != '\"'\n elem += ch\n end\n end\n\n list\n end\nend\n\nb = B.new"}, {"source_code": "class B\n def initialize\n str = $stdin.gets.chomp\n list = parse(str)\n\n list.each do |elem|\n puts \"<#{elem}>\"\n end\n end\n\n def parse(str)\n list = []\n elem = \"\"\n elem2 = \"\"\n flag = false\n str.each_char do |ch|\n if ch == '\"'\n flag ^= true\n unless flag\n list << elem\n elem = \"\"\n end\n next\n end\n\n if ch != ' ' && !flag\n elem2 += ch\n next\n elsif !flag\n list << elem2 if elem2.size > 0\n elem2 = \"\"\n end\n\n if flag && ch != '\"'\n elem += ch\n next\n end\n end\n\n list\n end\nend\n\nb = B.new"}, {"source_code": "s = gets.chomp\n\nstart = 0\nfor i in 0...s.length\n if s[i] != \" \"\n start = i\n break\n end\nend\n\ni = start\nwhile i < s.length\n if s.length - start > 1\n if s[i] != \" \"\n if s[i] == \"\\\"\"\n for j in i+1...s.length\n if s[j] == \"\\\"\"\n stop = j \n if start + 1 == stop\n print \"<>\"\n puts\n else\n print \"<\", s[start+1..stop-1], \">\"\n puts\n end\n start = j + 1\n break\n end \n end\n else\n for j in i+1...s.length\n if s[j] == \" \"\n stop = j\n print \"<\", s[start..stop-1], \">\"\n puts\n start = j + 1\n break\n end \n end\n end\n else \n start += 1 \n end\n i = start\n else\n print \"<\", s[start], \">\"\n break\n end\nend\n"}, {"source_code": "s = gets.chomp\n\ni = 0\nstate = 0\nwhile i < s.length\n if state == 2\n if s[i].ord == 34\n print \">\\n\"\n state = 0\n else\n print s[i]\n end\n elsif state == 0\n if s[i].ord == 34\n state = 2\n print \"<\"\n elsif s[i].ord == 32\n else\n state = 1\n print \"<\"\n print s[i]\n end\n else\n if s[i].ord == 34\n state = 2\n print \">\\n\"\n elsif s[i].ord == 32\n print \">\\n\"\n state = 0\n else\n print s[i]\n end\n end\n i += 1\nend"}, {"source_code": "str=gets.chomp\nq_flag=false\nbuf=\"\"\nstr.each_char do |c| \n if c=='\"' then\n if q_flag then\n print '<'\n print buf\n puts '>'\n buf=\"\"\n end \n q_flag=!q_flag\n next\n end\n if q_flag \n buf=buf+c\n else\n if c==' ' && !buf.empty? then\n print '<'\n print buf\n puts '>'\n buf=\"\"\n end\n if c!=' ' \n buf=buf+c\n end\n end \nend"}, {"source_code": "str = gets.chomp\n\nstr.scan(/(?:\\.|\"[^\"]*\")+/).each do |arg|\n puts \"<#{arg.gsub /\"/, ''}>\"\nend\n"}, {"source_code": "str = gets.chomp\n\nstr.scan(/(?:\\w|\"[^\"]*\")+/).each do |arg|\n puts \"<#{arg.gsub /\"/, ''}>\"\nend\n"}, {"source_code": "s = gets.chomp\nif s=='\"\"'\n puts \"<>\"\n exit\nend\ns.split(/\"/).each_with_index do |l, i|\n if i%2 == 1\n puts \"<#{l}>\"\n else\n l.split(/ /).each do |w|\n puts \"<#{w}>\" unless w.size == 0\n end\n end\nend"}, {"source_code": "s = gets.chomp\ns += ' '\nputs s.split(/\"/).inspect\ns.split(/\"/).each_with_index do |l, i|\n if i%2 == 1\n puts \"<#{l}>\"\n else\n l.split(/ /).each do |w|\n puts \"<#{w}>\" unless w.size == 0\n end\n end\nend"}, {"source_code": "str = gets.chomp\nts = []\ns = 0\np = ' '\nword = false\nfor i in 0...str.size\n if word\n if str[i] == p\n ts << str[s...i]\n word = false\n end\n elsif str[i] != ' '\n if str[i] == '\"'\n s = i + 1\n p = '\"'\n else\n s = i\n p = ' '\n end\n word = true\n end\nend\nputs(ts.map { |t| \"<#{t}>\" })"}, {"source_code": "str = gets.strip\nlexems = []\nstate = :init\nlexem = ''\nstr.chars.each do |char|\n case char\n when ' '\n if state == :quote_lexem\n lexem += char\n else\n state = :end_lexem\n lexems << lexem unless lexem.empty?\n lexem = ''\n end\n when '\"'\n if state == :quote_lexem\n state = :end_lexem\n lexems << lexem\n lexem = ''\n else\n state = :quote_lexem\n end\n else\n lexem += char\n end\nend\n\nlexems.each do |lexem|\n puts '<'+lexem+'>'\nend\n"}], "src_uid": "6c7858731c57e1b24c7a299a8eeab373"} {"nl": {"description": "Indian summer is such a beautiful time of the year! A girl named Alyona is walking in the forest and picking a bouquet from fallen leaves. Alyona is very choosy — she doesn't take a leaf if it matches the color and the species of the tree of one of the leaves she already has. Find out how many leaves Alyona has picked.", "input_spec": "The first line contains an integer n (1 ≤ n ≤ 100) — the number of leaves Alyona has found. The next n lines contain the leaves' descriptions. Each leaf is characterized by the species of the tree it has fallen from and by the color. The species of the trees and colors are given in names, consisting of no more than 10 lowercase Latin letters. A name can not be an empty string. The species of a tree and the color are given in each line separated by a space.", "output_spec": "Output the single number — the number of Alyona's leaves.", "sample_inputs": ["5\nbirch yellow\nmaple red\nbirch yellow\nmaple yellow\nmaple green", "3\noak yellow\noak yellow\noak yellow"], "sample_outputs": ["4", "1"], "notes": null}, "positive_code": [{"source_code": "Leaf = Struct.new(\"Leaf\", :tree, :color)\n\ncollection = {}\n\ngets.to_i.times do\n collection[Leaf.new(*gets.split)] = true\nend\n\n\nputs collection.size\n\n"}, {"source_code": "n = gets.to_i\n\nt = {}\nl = 0\n\nn.times do |i|\n tree,color = gets.split\n if t[tree] then\n if !(t[tree].include? color) then\n t[tree] << color \n l += 1\n end\n else\n t[tree] = [color]\n l += 1\n end\nend\n\nputs l"}, {"source_code": "n = gets.chomp.to_i\nh = Hash.new\nfor i in 0..n-1\n\th[gets.chomp] = true\nend\nputs h.length\n"}, {"source_code": "p gets(p).split($/).uniq.size-1\n"}, {"source_code": "#!/usr/bin/ruby\nh={}\ngets.to_i.times{h[gets]=1}\np h.size"}, {"source_code": "p gets(p).split($/).uniq.size-1\n"}, {"source_code": "p gets(p).split($/).uniq.size-1\n"}, {"source_code": "p gets(p).split($/).uniq.size-1"}, {"source_code": "p gets(p).split($/).uniq.size-1\n"}, {"source_code": "p gets(p).split($/).uniq.size-1\n"}, {"source_code": "p gets(p).split($/).uniq.size-1"}, {"source_code": "p gets(p).split($/).uniq.size-1\n"}, {"source_code": "n = gets.to_i\n\nmas = Array.new()\nfor i in 0..n-1\n mas[i] = gets\nend\n\nputs mas.uniq.size"}, {"source_code": "p gets(p).split($/).uniq.size-1\n"}, {"source_code": "p gets(p).split($/).uniq.size-1\n"}, {"source_code": "p gets(p).split($/).uniq.size-1"}, {"source_code": "ls = Hash.new { |a, b| a[b] = [] }\nn, count = gets.to_i, 0\n\nn.times do\n\tl, c = gets.strip.split(\" \").map &:to_sym\n\tunless ls[l].include? c\n\t\tls[l].push c \n\t\tcount += 1\n\tend\nend\n\nprint count"}, {"source_code": "p gets(p).split($/).uniq.size-1\n"}], "negative_code": [], "src_uid": "07c370b99fe85984f5e20826a3bf5eb9"} {"nl": {"description": "Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos.In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word \"helllo\" contains a typo). Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words \"helloo\" and \"wwaatt\" contain typos).Write a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word.", "input_spec": "The single line of the input contains word s, its length is from 1 to 200000 characters. The given word s consists of lowercase English letters.", "output_spec": "Print such word t that it doesn't contain any typos described in the problem statement and is obtained from s by deleting the least number of letters. If there are multiple solutions, print any of them.", "sample_inputs": ["helloo", "woooooow"], "sample_outputs": ["hello", "woow"], "notes": "NoteThe second valid answer to the test from the statement is \"heloo\"."}, "positive_code": [{"source_code": "s = gets.chomp.to_s\ns = s.gsub(/(.)\\1\\1+/m,'\\1\\1')\ns = s.gsub(/(.)\\1(.)\\2/m,'\\1\\1\\2')\nputs s\n"}, {"source_code": "s = gets.chomp.to_s\ns = s.gsub(/(.)\\1\\1+/m,'\\1\\1')\ns = s.gsub(/(.)\\1(.)\\2/m,'\\1\\1\\2')\nputs s\n"}, {"source_code": "s = gets.chomp.to_s\ns = s.gsub(/(.)\\1\\1+/m,'\\1\\1')\ns = s.gsub(/(.)\\1(.)\\2/m,'\\1\\1\\2')\nputs s\n"}, {"source_code": "s = gets.chomp.to_s\ns = s.gsub(/(.)\\1\\1+/m,'\\1\\1')\ns = s.gsub(/(.)\\1(.)\\2/m,'\\1\\1\\2')\nputs s\n"}, {"source_code": "string = gets.chomp\nlength = string.length\n\ncharacter_old = nil\ncharacter_current = nil\ncount_old = 0\ncount_current = 0\ni = 0\nstr = \"\"\nwhile i < length do\n if string[i] != character_current\n print character_old * count_old if character_old\n character_old = character_current\n character_current = string[i]\n count_old = count_current\n count_current = 1\n else\n count_current += 1\n if count_current == 3\n count_current -= 1\n elsif count_current == 2\n if count_old == 2\n count_current -=1\n end\n else\n end\n end\n i += 1\nend\n\nprint character_old * count_old if character_old\nprint character_current * count_current\n"}, {"source_code": "s = gets.chomp.to_s\ns = s.gsub(/(.)\\1\\1+/m,'\\1\\1')\ns = s.gsub(/(.)\\1(.)\\2/m,'\\1\\1\\2')\nputs s"}, {"source_code": "s = gets.chomp.to_s\ns = s.gsub(/(.)\\1\\1+/m,'\\1\\1')\ns = s.gsub(/(.)\\1(.)\\2/m,'\\1\\1\\2')\nputs s\n"}, {"source_code": "s = gets.chomp\nans = \"\"\ns.each_char do |a|\n\tif ((a == ans[-1]) && ans[-2] && (a == ans[-2])) || ((a == ans[-1]) && (ans[-2] && ans[-3] && (ans[-2] == ans[-3])))\n\telse\n\t\tans << a\n\tend\nend\nputs ans"}, {"source_code": "s = gets.strip\n\na = Array.new(s.size + 5)\npos = 0\n\nx = '*'\ny = '!'\nz = '~'\n\ns.each_char do |c|\n next if y == z and z == c\n next if x == y and z == c\n\n pos += 1\n a[pos] = c\n\n x = y\n y = z\n z = c\nend\n\n1.upto(pos) do |i|\n print a[i]\nend\nputs \"\"\n"}, {"source_code": "s = gets.chomp.to_s\ns = s.gsub(/(.)\\1\\1+/m,'\\1\\1')\ns = s.gsub(/(.)\\1(.)\\2/m,'\\1\\1\\2')\nputs s\n"}, {"source_code": "s = gets.chomp.to_s\ns = s.gsub(/(.)\\1\\1+/m,'\\1\\1')\ns = s.gsub(/(.)\\1(.)\\2/m,'\\1\\1\\2')\nputs s\n"}, {"source_code": "s = gets.chomp.to_s\ns = s.gsub(/(.)\\1\\1+/m,'\\1\\1')\ns = s.gsub(/(.)\\1(.)\\2/m,'\\1\\1\\2')\nputs s\n"}], "negative_code": [{"source_code": "string = gets.chomp\nlength = string.length\n\ncharacter_old = nil\ncharacter_current = nil\ncount_old = 0\ncount_current = 0\ni = 0\nremove_indexes = []\nwhile i < length do\n if string[i] != character_current\n character_old = character_current\n character_current = string[i]\n\n count_old = count_current\n count_current = 1\n else\n count_current += 1\n if count_current == 3\n remove_indexes << i - 1\n count_current -= 1\n elsif count_current == 2\n if count_old == 2\n remove_indexes << i - 2\n count_old -=1\n end\n else\n end\n end\n i += 1\nend\n\nstr = \"\"\nlength.times do |i|\n str += string[i] unless remove_indexes.include?(i)\nend\n\nputs str\n"}, {"source_code": "s = gets.chomp.to_s\ns = s.gsub(/(.)\\1\\1/m,'\\1\\1')\ns = s.gsub(/(.)\\1(.)\\2/m,'\\1\\1\\2')\nputs s"}, {"source_code": "s = gets.chomp\nans = \"\"\ns.each_char do |a|\n\tif (a == ans[-1] && a == ans[-2]) || ((a == ans[-1]) && (ans[-2] == ans[-3]))\n\telse\n\t\tans << a\n\tend\nend\nputs ans"}, {"source_code": "s = gets.strip\n\na = Array.new(s.size + 5)\npos = 0\n\nx = '*'\ny = '*'\nz = '*'\n\ns.each_char do |c|\n next if y == z and z == c\n next if x == y and z == c\n\n pos += 1\n a[pos] = c\n\n x = y\n y = z\n z = c\nend\n\n1.upto(pos) do |i|\n print a[i]\nend\nputs \"\"\n"}], "src_uid": "31d803d886c47fe681bdcfbe6c74f090"} {"nl": {"description": "You have $$$r$$$ red and $$$b$$$ blue beans. You'd like to distribute them among several (maybe, one) packets in such a way that each packet: has at least one red bean (or the number of red beans $$$r_i \\ge 1$$$); has at least one blue bean (or the number of blue beans $$$b_i \\ge 1$$$); the number of red and blue beans should differ in no more than $$$d$$$ (or $$$|r_i - b_i| \\le d$$$) Can you distribute all beans?", "input_spec": "The first line contains the single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. The first and only line of each test case contains three integers $$$r$$$, $$$b$$$, and $$$d$$$ ($$$1 \\le r, b \\le 10^9$$$; $$$0 \\le d \\le 10^9$$$) — the number of red and blue beans and the maximum absolute difference in each packet.", "output_spec": "For each test case, if you can distribute all beans, print YES. Otherwise, print NO. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).", "sample_inputs": ["4\n1 1 0\n2 7 3\n6 1 4\n5 4 0"], "sample_outputs": ["YES\nYES\nNO\nNO"], "notes": "NoteIn the first test case, you can form one packet with $$$1$$$ red and $$$1$$$ blue bean. The absolute difference $$$|1 - 1| = 0 \\le d$$$.In the second test case, you can form two packets: $$$1$$$ red and $$$4$$$ blue beans in the first packet and $$$1$$$ red and $$$3$$$ blue beans in the second one.In the third test case, since $$$b = 1$$$, you can form only one packet with $$$6$$$ red and $$$1$$$ blue beans. The absolute difference $$$|6 - 1| = 5 > d$$$.In the fourth test case, since $$$d = 0$$$ so each packet should contain the same number of red and blue beans, but $$$r \\neq b$$$."}, "positive_code": [{"source_code": "gets.to_i.times{\r\n r,b,d = gets.split.map(&:to_i)\r\n r,b = [r,b].sort\r\n\tb = (b.to_f/r.to_f).ceil\r\n if b-1>d\r\n puts \"NO\"\r\n else \r\n puts \"YES\"\r\n end\r\n}\r\n"}, {"source_code": "test_cases = gets.chomp.to_i\r\nfor i in 1..test_cases do\r\n line = gets.chomp.split(\" \")\r\n r = line[0].to_i\r\n b = line[1].to_i\r\n d = line[2].to_i\r\n puts [r, b].min * (d + 1) >= [r, b].max ? \"Yes\" : \"No\"\r\nend"}, {"source_code": "t = gets.to_s.to_i\n\ndef solve(r, b, d)\n if d == 0\n return r == b\n end\n\n maxb = b + b * d\n return maxb >= r\nend\n\n1.upto(t) do |casenum|\n r, b, d = gets.to_s.split.map {|x| x.to_i }\n printf(\"%s\\n\", solve([r, b].max, [r, b].min, d) ? \"YES\" : \"NO\")\nend\n"}], "negative_code": [{"source_code": "gets.to_i.times{\r\n r,b,d = gets.split.map(&:to_i)\r\n r,b = [r,b].sort\r\n\tb = (b/r)+b%r\r\n if b-1>d\r\n puts \"NO\"\r\n else \r\n puts \"YES\"\r\n end\r\n}\r\n"}, {"source_code": "gets.to_i.times{\r\n r,b,d = gets.split.map(&:to_i)\r\n r,b = [r,b].sort\r\n\tb = (b/r)+b%r\r\n\tp b\r\n if b-1>d\r\n puts \"NO\"\r\n else \r\n puts \"YES\"\r\n end\r\n}\r\n"}], "src_uid": "c0ad2a6d14b0c9e09af2221d88a34d52"} {"nl": {"description": "There are two types of burgers in your restaurant — hamburgers and chicken burgers! To assemble a hamburger you need two buns and a beef patty. To assemble a chicken burger you need two buns and a chicken cutlet. You have $$$b$$$ buns, $$$p$$$ beef patties and $$$f$$$ chicken cutlets in your restaurant. You can sell one hamburger for $$$h$$$ dollars and one chicken burger for $$$c$$$ dollars. Calculate the maximum profit you can achieve.You have to answer $$$t$$$ independent queries.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) – the number of queries. The first line of each query contains three integers $$$b$$$, $$$p$$$ and $$$f$$$ ($$$1 \\le b, ~p, ~f \\le 100$$$) — the number of buns, beef patties and chicken cutlets in your restaurant. The second line of each query contains two integers $$$h$$$ and $$$c$$$ ($$$1 \\le h, ~c \\le 100$$$) — the hamburger and chicken burger prices in your restaurant.", "output_spec": "For each query print one integer — the maximum profit you can achieve.", "sample_inputs": ["3\n15 2 3\n5 10\n7 5 2\n10 12\n1 100 100\n100 100"], "sample_outputs": ["40\n34\n0"], "notes": "NoteIn first query you have to sell two hamburgers and three chicken burgers. Your income is $$$2 \\cdot 5 + 3 \\cdot 10 = 40$$$.In second query you have to ell one hamburgers and two chicken burgers. Your income is $$$1 \\cdot 10 + 2 \\cdot 12 = 34$$$.In third query you can not create any type of burgers because because you have only one bun. So your income is zero."}, "positive_code": [{"source_code": "t = readline.to_i\n\nt.times do\n b,p,f = readline.split.map(&:to_i)\n h,c = readline.split.map(&:to_i)\n if h >= c\n p1 = [b/2,p].min\n p2 = [(b - 2*p1)/2,f].min\n else\n p2 = [b/2,f].min\n p1 = [(b - 2*p2)/2,p].min\n end\n puts h*p1 + c*p2\nend"}, {"source_code": "q = gets.to_i\nq.times do\n n,a,b = gets.split.map(&:to_i)\n h,c = gets.split.map(&:to_i)\n ans = 0\n if h > c\n t = [n/2,a].min\n ans += t*h\n n -= t*2\n ans += c*[n/2,b].min\n else\n t = [n/2,b].min\n ans += t*c\n n -= t*2\n ans += h*[n/2,a].min\n end\n p ans\nend"}, {"source_code": "t=gets.to_i\nwhile t > 0\n total = 0\n b, p, f = gets.split.map(&:to_i)\n h, c= gets.split.map(&:to_i)\n t-=1\n if h > c\n total += ([(b/2).to_i, p].min) * h\n b -= ([(b/2).to_i, p].min)*2\n total += ([(b/2).to_i, f].min) * c\n else\n total += ([(b/2).to_i, f].min) * c\n b -= ([(b/2).to_i, f].min)*2\n total += ([(b/2).to_i, p].min) * h\n end\n puts(total) \nend\n"}, {"source_code": "gets.to_i.times do\n b, p, f = gets.split.map(&:to_i)\n h, c = gets.split.map(&:to_i)\n ans = 0\n if h >= c\n p_num = [b/2, p].min\n b -= p_num * 2\n ans += p_num * h\n ans += [b/2, f].min * c\n else\n f_num = [b/2, f].min\n b -= f_num * 2\n ans += f_num * c\n ans += [b/2, p].min * h\n end\n puts ans\nend"}, {"source_code": "def maximum_profit\n q = gets.chomp.to_i\n results = Array.new\n until q.zero? do\n ingredients = gets.chomp\n ingredients = get_each(ingredients)\n prices = gets.chomp\n prices = get_each(prices)\n results << get_maximum(ingredients, prices)\n q -= 1\n end\n results\nend\n$burger_num = 0\n\ndef get_maximum(ingredients, prices)\n $burger_num = ingredients[0]/2\n ham_num = ingredients[1]\n chicken_num = ingredients[2]\n pay = 0\n if prices[0] >= prices[1]\n pay += payment(prices[0], ham_num)\n pay += payment(prices[1], chicken_num)\n else\n pay += payment(prices[1], chicken_num)\n pay += payment(prices[0], ham_num)\n end\n pay\nend\n\ndef payment(price, ingredient)\n pay = 0\n if $burger_num >= ingredient\n pay = price * ingredient\n $burger_num -= ingredient\n else\n pay = price * $burger_num\n $burger_num = 0\n end\n pay\nend\n\ndef get_each(str)\n arr = Array.new\n num = 0\n str.each_char do |e|\n if e == ' '\n arr << num\n num = 0\n else\n num = num * 10 + e.to_i\n end\n end\n arr << num unless num.zero?\n arr\nend\n\nputs maximum_profit\n"}, {"source_code": "# http://codeforces.com/contest/1207/problem/A\n\ndef maximum_profit\n q = gets.chomp.to_i\n results = Array.new\n until q.zero? do\n ingredients = gets.chomp\n ingredients = get_each(ingredients)\n prices = gets.chomp\n prices = get_each(prices)\n results << get_maximum(ingredients, prices)\n q -= 1\n end\n results\nend\n$burger_num = 0\n\ndef get_maximum(ingredients, prices)\n $burger_num = ingredients[0]/2\n ham_num = ingredients[1]\n chicken_num = ingredients[2]\n pay = 0\n if prices[0] >= prices[1]\n pay += payment(prices[0], ham_num)\n pay += payment(prices[1], chicken_num)\n else\n pay += payment(prices[1], chicken_num)\n pay += payment(prices[0], ham_num)\n end\n pay\nend\n\ndef payment(price, ingredient)\n pay = 0\n if $burger_num >= ingredient\n pay = price * ingredient\n $burger_num -= ingredient\n else\n pay = price * $burger_num\n $burger_num = 0\n end\n pay\nend\n\ndef get_each(str)\n arr = Array.new\n num = 0\n str.each_char do |e|\n if e == ' '\n arr << num\n num = 0\n else\n num = num * 10 + e.to_i\n end\n end\n arr << num unless num.zero?\n arr\nend\n\nputs maximum_profit\n"}, {"source_code": "t = gets.to_i\nbpf = []\nhc = []\nt.times do|_x|\n bpf << gets.split.map(&:to_i)\n hc << gets.split.map(&:to_i)\nend\n\nmax = []\n0.upto(t-1) do |i|\n price = 0\n buns = bpf[i][0]\n beef = bpf[i][1]\n chi = bpf[i][2]\n price1 = hc[i][0]\n price2 = hc[i][1]\n\n if price1 >= price2\n # ハンバーガーを優先して作る\n if beef*2 > buns\n # beefが多いならbunsが足りるまで使う\n price = buns / 2 * price1\n else\n buns -= beef * 2\n price = beef*price1 + (chi*2>buns ? buns/2 : chi)*price2\n end\n else\n # チキンバーガーを優先して作る\n if chi*2 > buns\n # chiが多いならbunsが足りるまで使う\n price = buns / 2 * price2\n else\n buns -= chi * 2\n price = chi*price2 + (beef*2>buns ? buns/2 : beef)*price1\n end\n end\n max << price\nend\nputs max.join(\"\\n\")"}, {"source_code": "t = gets.to_i\nt.times do\n b, p, f = gets.split.map &:to_i\n h, c = gets.split.map &:to_i\n if h > c\n hs = [b/2, p].min\n cs = [(b - hs*2)/2, f].min\n else\n cs = [b/2, f].min\n hs = [(b - cs*2)/2, p].min\n end\n puts hs*h + cs*c\nend\n"}, {"source_code": "1.upto(gets.to_i) do\n b, p, f = gets.split(' ').map(&:to_i)\n h, c = gets.split(' ').map(&:to_i)\n\n puts (0..b/2).map { |x| [x, p].min*h + [(b/2 - x), f].min*c }.max\nend\n"}], "negative_code": [], "src_uid": "92bf30e66f4d5ddebb697d2fa4fa0689"} {"nl": {"description": "\"The Chamber of Secrets has been opened again\" — this news has spread all around Hogwarts and some of the students have been petrified due to seeing the basilisk. Dumbledore got fired and now Harry is trying to enter the Chamber of Secrets. These aren't good news for Lord Voldemort. The problem is, he doesn't want anybody to be able to enter the chamber. The Dark Lord is going to be busy sucking life out of Ginny.The Chamber of Secrets is an n × m rectangular grid in which some of the cells are columns. A light ray (and a basilisk's gaze) passes through the columns without changing its direction. But with some spell we can make a column magic to reflect the light ray (or the gaze) in all four directions when it receives the ray. This is shown in the figure below. The left light ray passes through a regular column, and the right ray — through the magic column. The basilisk is located at the right side of the lower right cell of the grid and is looking to the left (in the direction of the lower left cell). According to the legend, anyone who meets a basilisk's gaze directly dies immediately. But if someone meets a basilisk's gaze through a column, this person will get petrified. We know that the door to the Chamber is located on the left side of the upper left corner of the grid and anyone who wants to enter will look in the direction of its movement (in the direction of the upper right cell) from that position. This figure illustrates the first sample test. Given the dimensions of the chamber and the location of regular columns, Lord Voldemort has asked you to find the minimum number of columns that we need to make magic so that anyone who wants to enter the chamber would be petrified or just declare that it's impossible to secure the chamber.", "input_spec": "The first line of the input contains two integer numbers n and m (2 ≤ n, m ≤ 1000). Each of the next n lines contains m characters. Each character is either \".\" or \"#\" and represents one cell of the Chamber grid. It's \".\" if the corresponding cell is empty and \"#\" if it's a regular column.", "output_spec": "Print the minimum number of columns to make magic or -1 if it's impossible to do.", "sample_inputs": ["3 3\n.#.\n...\n.#.", "4 3\n##.\n...\n.#.\n.#."], "sample_outputs": ["2", "2"], "notes": "NoteThe figure above shows the first sample test. In the first sample we should make both columns magic. The dragon figure represents the basilisk and the binoculars represent the person who will enter the Chamber of secrets. The black star shows the place where the person will be petrified. Yellow lines represent basilisk gaze moving through columns."}, "positive_code": [{"source_code": "n,m = gets.split.map &:to_i\na = [nil]*(n+m)\n0.upto(n+m) {|i| a[i] = []}\n\n0.upto(n-1) do |i|\n s = gets.chomp\n len = s.size\n 0.upto(len) do |j|\n if s[j] == '#'\n a[i] << n+j\n a[n+j] << i\n end\n end\nend\n\nd = [100500] * 2100\nq = []\nd[0] = 0\nq << 0\nwhile !q.empty? do\n x = q.shift\n a[x].each do |y|\n if d[y] == 100500\n d[y] = d[x] + 1\n q << y\n end\n end\nend\n\nif d[n-1] == 100500\n puts -1\n exit\nend\nputs d[n-1]\n\n"}, {"source_code": "n,m = gets.split.map &:to_i\na = [nil]*(n+m)\n0.upto(n+m) {|i| a[i] = []}\n\n0.upto(n-1) do |i|\n s = gets.chomp\n len = s.size\n 0.upto(len) do |j|\n if s[j] == '#'\n a[i] << n+j\n a[n+j] << i\n end\n end\nend\n\nd = [100500] * 2100\nq = []\nd[0] = 0\nq << 0\nwhile !q.empty? do\n x = q.shift\n a[x].each do |y|\n if d[y] == 100500\n d[y] = d[x] + 1\n q << y\n end\n end\nend\n\nif d[n-1] == 100500\n puts -1\n exit\nend\nputs d[n-1]\n\n"}, {"source_code": "n,m = gets.split.map &:to_i\na = [nil]*(n+m)\n0.upto(n+m) {|i| a[i] = []}\n\n0.upto(n-1) do |i|\n s = gets.chomp\n len = s.size\n 0.upto(len) do |j|\n if s[j] == '#'\n a[i] << n+j\n a[n+j] << i\n end\n end\nend\n\nd = [100500] * 2100\nq = []\nd[0] = 0\nq << 0\nwhile !q.empty? do\n x = q.shift\n a[x].each do |y|\n if d[y] == 100500\n d[y] = d[x] + 1\n q << y\n end\n end\nend\n\nif d[n-1] == 100500\n puts -1\n exit\nend\nputs d[n-1]\n"}, {"source_code": "n,m = gets.split.map &:to_i\na = [nil]*(n+m)\n0.upto(n+m) {|i| a[i] = []}\n\n0.upto(n-1) do |i|\n s = gets.chomp\n len = s.size\n 0.upto(len) do |j|\n if s[j] == '#'\n a[i] << n+j\n a[n+j] << i\n end\n end\nend\n\nd = [100500] * 2100\nq = []\nd[0] = 0\nq << 0\nwhile !q.empty? do\n x = q.shift\n a[x].each do |y|\n if d[y] == 100500\n d[y] = d[x] + 1\n q << y\n end\n end\nend\n\nif d[n-1] == 100500\n puts -1\n exit\nend\nputs d[n-1]\n\n"}, {"source_code": "n,m = gets.split.map &:to_i\na = [nil]*(n+m)\n0.upto(n+m) {|i| a[i] = []}\n\n0.upto(n-1) do |i|\n s = gets.chomp\n len = s.size\n 0.upto(len) do |j|\n if s[j] == '#'\n a[i] << n+j\n a[n+j] << i\n end\n end\nend\n\nd = [100500] * 2100\nq = []\nd[0] = 0\nq << 0\nwhile !q.empty? do\n x = q.shift\n a[x].each do |y|\n if d[y] == 100500\n d[y] = d[x] + 1\n q << y\n end\n end\nend\n\nif d[n-1] == 100500\n puts -1\n exit\nend\nputs d[n-1]\n\n"}, {"source_code": "n,m = gets.split.map &:to_i\na = [nil]*(n+m)\n0.upto(n+m) {|i| a[i] = []}\n 0.upto(n-1) do |i|\n s = gets.chomp\n len = s.size\n 0.upto(len) do |j|\n if s[j] == '#'\n a[i] << n+j\n a[n+j] << i\n end\n end\nend\n d = [100500] * 2100\nq = []\nd[0] = 0\nq << 0\nwhile !q.empty? do\n x = q.shift\n a[x].each do |y|\n if d[y] == 100500\n d[y] = d[x] + 1\n q << y\n end\n end\nend\n if d[n-1] == 100500\n puts -1\n exit\nend\nputs d[n-1]"}, {"source_code": "n,m = gets.split.map &:to_i\na = [nil]*(n+m)\n0.upto(n+m) {|i| a[i] = []}\n\n0.upto(n-1) do |i|\n s = gets.chomp\n len = s.size\n 0.upto(len) do |j|\n if s[j] == '#'\n a[i] << n+j\n a[n+j] << i\n end\n end\nend\n\nd = [100500] * 2100\nq = []\nd[0] = 0\nq << 0\nwhile !q.empty? do\n x = q.shift\n a[x].each do |y|\n if d[y] == 100500\n d[y] = d[x] + 1\n q << y\n end\n end\nend\n\nif d[n-1] == 100500\n puts -1\n exit\nend\nputs d[n-1]\n"}, {"source_code": "n,m = gets.split.map &:to_i\na = [nil]*(n+m)\n0.upto(n+m) {|i| a[i] = []}\n\n0.upto(n-1) do |i|\n s = gets.chomp\n len = s.size\n 0.upto(len) do |j|\n if s[j] == '#'\n a[i] << n+j\n a[n+j] << i\n end\n end\nend\n\nd = [100500] * 2100\nq = []\nd[0] = 0\nq << 0\nwhile !q.empty? do\n x = q.shift\n a[x].each do |y|\n if d[y] == 100500\n d[y] = d[x] + 1\n q << y\n end\n end\nend\n\nif d[n-1] == 100500\n puts -1\n exit\nend\nputs d[n-1]\n\n"}, {"source_code": "n,m = gets.split.map &:to_i\na = [nil]*(n+m)\n0.upto(n+m) {|i| a[i] = []}\n\n0.upto(n-1) do |i|\n s = gets.chomp\n len = s.size\n 0.upto(len) do |j|\n if s[j] == '#'\n a[i] << n+j\n a[n+j] << i\n end\n end\nend\n\nd = [100500] * 2100\nq = []\nd[0] = 0\nq << 0\nwhile !q.empty? do\n x = q.shift\n a[x].each do |y|\n if d[y] == 100500\n d[y] = d[x] + 1\n q << y\n end\n end\nend\n\nif d[n-1] == 100500\n puts -1\n exit\nend\nputs d[n-1]\n\n"}, {"source_code": "n,m = gets.split.map &:to_i\na = [nil]*(n+m)\n0.upto(n+m) {|i| a[i] = []}\n\n0.upto(n-1) do |i|\n s = gets.chomp\n len = s.size\n 0.upto(len) do |j|\n if s[j] == '#'\n a[i] << n+j\n a[n+j] << i\n end\n end\nend\n\nd = [100500] * 2100\nq = []\nd[0] = 0\nq << 0\nwhile !q.empty? do\n x = q.shift\n a[x].each do |y|\n if d[y] == 100500\n d[y] = d[x] + 1\n q << y\n end\n end\nend\n\nif d[n-1] == 100500\n puts -1\n exit\nend\nputs d[n-1]\n\n"}, {"source_code": "n,m = gets.split.map &:to_i\na = [nil]*(n+m)\n0.upto(n+m) {|i| a[i] = []}\n\n0.upto(n-1) do |i|\n s = gets.chomp\n len = s.size\n 0.upto(len) do |j|\n if s[j] == '#'\n a[i] << n+j\n a[n+j] << i\n end\n end\nend\n\nd = [100500] * 2100\nq = []\nd[0] = 0\nq << 0\nwhile !q.empty? do\n x = q.shift\n a[x].each do |y|\n if d[y] == 100500\n d[y] = d[x] + 1\n q << y\n end\n end\nend\n\nif d[n-1] == 100500\n puts -1\n exit\nend\nputs d[n-1]\n\n"}], "negative_code": [], "src_uid": "8b6f93cf2a41445649e0cbfc471541b6"} {"nl": {"description": "The only difference between the easy and hard versions is that the given string $$$s$$$ in the easy version is initially a palindrome, this condition is not always true for the hard version.A palindrome is a string that reads the same left to right and right to left. For example, \"101101\" is a palindrome, while \"0101\" is not.Alice and Bob are playing a game on a string $$$s$$$ (which is initially a palindrome in this version) of length $$$n$$$ consisting of the characters '0' and '1'. Both players take alternate turns with Alice going first.In each turn, the player can perform one of the following operations: Choose any $$$i$$$ ($$$1 \\le i \\le n$$$), where $$$s[i] =$$$ '0' and change $$$s[i]$$$ to '1'. Pay 1 dollar. Reverse the whole string, pay 0 dollars. This operation is only allowed if the string is currently not a palindrome, and the last operation was not reverse. That is, if Alice reverses the string, then Bob can't reverse in the next move, and vice versa. Reversing a string means reordering its letters from the last to the first. For example, \"01001\" becomes \"10010\" after reversing.The game ends when every character of string becomes '1'. The player who spends minimum dollars till this point wins the game and it is a draw if both spend equal dollars. If both players play optimally, output whether Alice wins, Bob wins, or if it is a draw.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^3$$$). Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^3$$$). The second line of each test case contains the string $$$s$$$ of length $$$n$$$, consisting of the characters '0' and '1'. It is guaranteed that the string $$$s$$$ is a palindrome and contains at least one '0'. Note that there is no limit on the sum of $$$n$$$ over test cases.", "output_spec": "For each test case print a single word in a new line: \"ALICE\", if Alice will win the game, \"BOB\", if Bob will win the game, \"DRAW\", if the game ends in a draw. ", "sample_inputs": ["2\n4\n1001\n1\n0"], "sample_outputs": ["BOB\nBOB"], "notes": "NoteIn the first test case of the example, in the $$$1$$$-st move Alice has to perform the $$$1$$$-st operation, since the string is currently a palindrome. in the $$$2$$$-nd move Bob reverses the string. in the $$$3$$$-rd move Alice again has to perform the $$$1$$$-st operation. All characters of the string are '1', game over. Alice spends $$$2$$$ dollars while Bob spends $$$0$$$ dollars. Hence, Bob always wins."}, "positive_code": [{"source_code": "gets.to_i.times {\n gets\n s = gets.strip\n n = s.count '0'\n if s.size.odd? && s[s.size / 2] == '0'\n puts(n > 1 ? 'ALICE' : 'BOB')\n else\n #puts(n % 4 == 0 ? 'DRAW' : 'BOB')\n puts 'BOB'\n end\n}\n"}, {"source_code": "gets.to_i.times do\r\n gets\r\n s = gets.chomp\r\n cnt = s.count '0'\r\n if cnt == 1\r\n puts \"BOB\"\r\n elsif cnt % 2 == 1\r\n puts \"ALICE\"\r\n else\r\n puts \"BOB\"\r\n end\r\nend"}, {"source_code": "gets.to_i.times{|c|\r\n\tn = gets.to_i\r\n\ts = gets.chomp\r\n\tif s.count(?0)==1 then\r\n\t\tputs 'BOB'\r\n\t\tnext\r\n\telsif s.count(?0)==0 then\r\n\t\tputs 'DRAW'\r\n\t\tnext\r\n\tend\r\n\tputs (n%2==1&&s[n/2]=='0') ? 'ALICE' : 'BOB'\r\n}"}], "negative_code": [{"source_code": "gets.to_i.times {\n gets\n n = gets.strip.count '0'\n puts(n % 4 == 0 ? 'DRAW' : 'BOB')\n}\n"}], "src_uid": "42b425305ccc28b0d081b4c417fe77a1"} {"nl": {"description": "Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with $$$n$$$ elements. The $$$i$$$-th element is $$$a_i$$$ ($$$i$$$ = $$$1, 2, \\ldots, n$$$). He gradually takes the first two leftmost elements from the deque (let's call them $$$A$$$ and $$$B$$$, respectively), and then does the following: if $$$A > B$$$, he writes $$$A$$$ to the beginning and writes $$$B$$$ to the end of the deque, otherwise, he writes to the beginning $$$B$$$, and $$$A$$$ writes to the end of the deque. We call this sequence of actions an operation.For example, if deque was $$$[2, 3, 4, 5, 1]$$$, on the operation he will write $$$B=3$$$ to the beginning and $$$A=2$$$ to the end, so he will get $$$[3, 4, 5, 1, 2]$$$.The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him $$$q$$$ queries. Each query consists of the singular number $$$m_j$$$ $$$(j = 1, 2, \\ldots, q)$$$. It is required for each query to answer which two elements he will pull out on the $$$m_j$$$-th operation.Note that the queries are independent and for each query the numbers $$$A$$$ and $$$B$$$ should be printed in the order in which they will be pulled out of the deque.Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides.", "input_spec": "The first line contains two integers $$$n$$$ and $$$q$$$ ($$$2 \\leq n \\leq 10^5$$$, $$$0 \\leq q \\leq 3 \\cdot 10^5$$$) — the number of elements in the deque and the number of queries. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$, where $$$a_i$$$ $$$(0 \\leq a_i \\leq 10^9)$$$ — the deque element in $$$i$$$-th position. The next $$$q$$$ lines contain one number each, meaning $$$m_j$$$ ($$$1 \\leq m_j \\leq 10^{18}$$$).", "output_spec": "For each teacher's query, output two numbers $$$A$$$ and $$$B$$$ — the numbers that Valeriy pulls out of the deque for the $$$m_j$$$-th operation.", "sample_inputs": ["5 3\n1 2 3 4 5\n1\n2\n10", "2 0\n0 0"], "sample_outputs": ["1 2\n2 3\n5 2", ""], "notes": "Note Consider all 10 steps for the first test in detail: $$$[1, 2, 3, 4, 5]$$$ — on the first operation, $$$A$$$ and $$$B$$$ are $$$1$$$ and $$$2$$$, respectively.So, $$$2$$$ we write to the beginning of the deque, and $$$1$$$ — to the end.We get the following status of the deque: $$$[2, 3, 4, 5, 1]$$$. $$$[2, 3, 4, 5, 1] \\Rightarrow A = 2, B = 3$$$. $$$[3, 4, 5, 1, 2]$$$ $$$[4, 5, 1, 2, 3]$$$ $$$[5, 1, 2, 3, 4]$$$ $$$[5, 2, 3, 4, 1]$$$ $$$[5, 3, 4, 1, 2]$$$ $$$[5, 4, 1, 2, 3]$$$ $$$[5, 1, 2, 3, 4]$$$ $$$[5, 2, 3, 4, 1] \\Rightarrow A = 5, B = 2$$$. "}, "positive_code": [{"source_code": "inp = gets\nn, q = inp.split(' ').map(&:to_i)\n\ninp = gets\na = inp.split(' ').map(&:to_i)\n\ni_max = 0\ndeque = []\nn_deque = 0\n\nfor i in 0..n-1\n if a[i] > a[i_max]\n i_max = i\n end\nend\n\nfor i in i_max+1..n-1\n deque[n_deque] = a[i]\n n_deque += 1\nend\n\nst = []\ned = []\nn_query = 0\ni1 = 0\ni2 = 1\n\nwhile i1 != i_max \n st[n_query] = a[i1]\n ed[n_query] = a[i2]\n if a[i1] > a[i2]\n deque[n_deque] = a[i2]\n i2 += 1\n else\n deque[n_deque] = a[i1]\n i1 = i2\n i2 += 1 \n end\n n_deque += 1\n n_query += 1\nend\n\nfor i in 0..q-1\n inp = gets\n q_i = inp.to_i\n if q_i <= n_query\n puts \"#{st[q_i-1]} #{ed[q_i-1]}\"\n else\n puts \"#{a[i_max]} #{deque[(q_i - i_max + n_deque) % n_deque - 1]}\"\n end\nend"}, {"source_code": "n, m = gets.split(' ').map(&:to_i)\nl = gets.split(' ').map(&:to_i)\na, r = l[0], []\n(1...n).each do |i|\n b = l[i]\n l[i] = \"#{a} #{b}\"\n a, b = b, a if a < b\n r.push b\nend\nn -= 1\nm.times do\n q = gets.to_i\n puts n < q ? \"#{a} #{r[(q - 1) % n]}\" : l[q]\nend"}, {"source_code": "n, m = gets.split(' ').map(&:to_i)\nl = gets.split(' ')\nsa, r = l[0], []\na = sa.to_i\n(1...n).each do |i|\n sb = l[i]\n b = sb.to_i\n l[i] = \"#{a} #{b}\"\n a, sa, sb = b, sb, sa, a if a < b\n r.push sb\nend\nn -= 1\nm.times do\n q = gets.to_i\n puts n < q ? \"#{sa} #{r[(q - 1) % n]}\" : l[q]\nend"}, {"source_code": "n, m = gets.split(' ').map(&:to_i)\nl = gets.split(' ')\nsa, r = l[0], []\na = sa.to_i\n(1...n).each do |i|\n sb = l[i]\n b = sb.to_i\n l[i] = \"#{a} #{b}\"\n a, sa, sb = b, sb, sa, a if a < b\n r.push sb\nend\nn -= 1\nm.times do\n q = gets.to_i\n puts n < q ? \"#{a} #{r[(q - 1) % n]}\" : l[q]\nend"}], "negative_code": [{"source_code": "inp = gets\nn, q = inp.split(' ').map(&:to_i)\n\ninp = gets\na = inp.split(' ').map(&:to_i)\n\ni_max = 0\ndeque = []\nn_deque = 0\n\nfor i in 0..n-1\n if a[i] > a[i_max]\n i_max = i\n end\nend\n\nfor i in i_max+1..n-1\n deque[n_deque] = a[i]\n n_deque += 1\nend\n\nst = []\ned = []\nn_query = 0\ni1 = 0\ni2 = 1\n\nwhile i1 != i_max \n st[n_query] = a[i1]\n ed[n_query] = a[i2]\n if a[i1] > a[i2]\n deque[n_deque] = a[i2]\n i2 += 1\n else\n deque[n_deque] = a[i1]\n i1 = i2\n i2 += 1 \n end\n n_deque += 1\n n_query += 1\nend\n\nfor i in 0..q-1\n inp = gets\n q_i = inp.to_i\n if q_i <= n_query\n puts \"#{st[q_i-1]} #{ed[q_i-1]}\"\n else\n puts i_max\n puts q_i\n puts n_deque\n puts \"#{a[i_max]} #{deque[(q_i - i_max + n_deque) % n_deque - 1]}\"\n end\nend"}, {"source_code": "n, m = gets.split(' ').map(&:to_i)\nl = gets.split(' ').map(&:to_i)\na, buf, res = l.shift, [], []\nl.each do |b|\n buf.push \"#{a} #{b}\"\n a, b = b, a if a < b\n res.push b\nend\nn -= 1\nm.times do\n q = gets.to_i\n puts q < n ? buf[q - 1] : \"#{a} #{res[(q - 1) % n]}\"\nend"}], "src_uid": "3cb19ef77e9cb919a2f0a1d687b2845d"} {"nl": {"description": "You are given array $$$a_1, a_2, \\dots, a_n$$$. Find the subsegment $$$a_l, a_{l+1}, \\dots, a_r$$$ ($$$1 \\le l \\le r \\le n$$$) with maximum arithmetic mean $$$\\frac{1}{r - l + 1}\\sum\\limits_{i=l}^{r}{a_i}$$$ (in floating-point numbers, i.e. without any rounding).If there are many such subsegments find the longest one.", "input_spec": "The first line contains single integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$) — length of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$0 \\le a_i \\le 10^9$$$) — the array $$$a$$$.", "output_spec": "Print the single integer — the length of the longest subsegment with maximum possible arithmetic mean.", "sample_inputs": ["5\n6 1 6 6 0"], "sample_outputs": ["2"], "notes": "NoteThe subsegment $$$[3, 4]$$$ is the longest among all subsegments with maximum arithmetic mean."}, "positive_code": [{"source_code": "n = gets\na = gets.to_s.chomp.split(\" \").map{|x| x.to_i}\nmax = a.max\ni = 0\nres = 0\na.each do |x|\n if(x == max)\n i += 1\n res = [i, res].max\n else\n i = 0\n #print \"#{x} #{max} #{i}\"\n #puts\n end\nend\nputs res"}], "negative_code": [{"source_code": "gets\na = gets.to_s.chomp.split(\" \").map{|x| x.to_i}\nmax = 0\ni = 0\nres = 0\na.each do |x|\n if(max <= x)\n i += 1\n max = x\n if(res < i)\n res = i\n end\n else\n i = 0\n end\n #print \"#{x} #{max} #{i}\"\n #puts\nend\nputs res"}, {"source_code": "gets\na = gets.to_s.chomp.split(\" \").map{|x| x.to_i}\nmax = 0\ni = 1\nres = 0\na.each do |x|\n if(max <= x)\n i += 1\n max = x\n if(res < i)\n res = i\n end\n else\n i = 0\n end\n #print \"#{x} #{max} #{i}\"\n #puts\nend\nputs res"}], "src_uid": "5db2ae5b2c91b29e4fe4a45ab864e3f1"} {"nl": {"description": "Tokitsukaze has a sequence $$$a$$$ of length $$$n$$$. For each operation, she selects two numbers $$$a_i$$$ and $$$a_j$$$ ($$$i \\ne j$$$; $$$1 \\leq i,j \\leq n$$$). If $$$a_i = a_j$$$, change one of them to $$$0$$$. Otherwise change both of them to $$$\\min(a_i, a_j)$$$. Tokitsukaze wants to know the minimum number of operations to change all numbers in the sequence to $$$0$$$. It can be proved that the answer always exists.", "input_spec": "The first line contains a single positive integer $$$t$$$ ($$$1 \\leq t \\leq 1000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \\leq n \\leq 100$$$) — the length of the sequence $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$0 \\leq a_i \\leq 100$$$) — the sequence $$$a$$$.", "output_spec": "For each test case, print a single integer — the minimum number of operations to change all numbers in the sequence to $$$0$$$.", "sample_inputs": ["3\n3\n1 2 3\n3\n1 2 2\n3\n1 2 0"], "sample_outputs": ["4\n3\n2"], "notes": "NoteIn the first test case, one of the possible ways to change all numbers in the sequence to $$$0$$$:In the $$$1$$$-st operation, $$$a_1 < a_2$$$, after the operation, $$$a_2 = a_1 = 1$$$. Now the sequence $$$a$$$ is $$$[1,1,3]$$$.In the $$$2$$$-nd operation, $$$a_1 = a_2 = 1$$$, after the operation, $$$a_1 = 0$$$. Now the sequence $$$a$$$ is $$$[0,1,3]$$$.In the $$$3$$$-rd operation, $$$a_1 < a_2$$$, after the operation, $$$a_2 = 0$$$. Now the sequence $$$a$$$ is $$$[0,0,3]$$$.In the $$$4$$$-th operation, $$$a_2 < a_3$$$, after the operation, $$$a_3 = 0$$$. Now the sequence $$$a$$$ is $$$[0,0,0]$$$.So the minimum number of operations is $$$4$$$."}, "positive_code": [{"source_code": "gets.to_i.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n zc = 0\n ac = {}\n flag = false\n 0.upto(a.size-1) do |i|\n flag = true if ac[a[i]]\n zc += 1 if a[i] == 0\n ac[a[i]] = true\n end\n if zc > 0 then\n puts n - zc\n else\n puts flag ? (n) : (n+1)\n end\nend\n"}], "negative_code": [], "src_uid": "ad242f98f1c8eb8d30789ec672fc95a0"} {"nl": {"description": "Simon has a rectangular table consisting of n rows and m columns. Simon numbered the rows of the table from top to bottom starting from one and the columns — from left to right starting from one. We'll represent the cell on the x-th row and the y-th column as a pair of numbers (x, y). The table corners are cells: (1, 1), (n, 1), (1, m), (n, m).Simon thinks that some cells in this table are good. Besides, it's known that no good cell is the corner of the table. Initially, all cells of the table are colorless. Simon wants to color all cells of his table. In one move, he can choose any good cell of table (x1, y1), an arbitrary corner of the table (x2, y2) and color all cells of the table (p, q), which meet both inequations: min(x1, x2) ≤ p ≤ max(x1, x2), min(y1, y2) ≤ q ≤ max(y1, y2).Help Simon! Find the minimum number of operations needed to color all cells of the table. Note that you can color one cell multiple times.", "input_spec": "The first line contains exactly two integers n, m (3 ≤ n, m ≤ 50). Next n lines contain the description of the table cells. Specifically, the i-th line contains m space-separated integers ai1, ai2, ..., aim. If aij equals zero, then cell (i, j) isn't good. Otherwise aij equals one. It is guaranteed that at least one cell is good. It is guaranteed that no good cell is a corner.", "output_spec": "Print a single number — the minimum number of operations Simon needs to carry out his idea.", "sample_inputs": ["3 3\n0 0 0\n0 1 0\n0 0 0", "4 3\n0 0 0\n0 0 1\n1 0 0\n0 0 0"], "sample_outputs": ["4", "2"], "notes": "NoteIn the first sample, the sequence of operations can be like this: For the first time you need to choose cell (2, 2) and corner (1, 1). For the second time you need to choose cell (2, 2) and corner (3, 3). For the third time you need to choose cell (2, 2) and corner (3, 1). For the fourth time you need to choose cell (2, 2) and corner (1, 3). In the second sample the sequence of operations can be like this: For the first time you need to choose cell (3, 1) and corner (4, 3). For the second time you need to choose cell (2, 3) and corner (1, 1). "}, "positive_code": [{"source_code": "def main\n n, m = STDIN.readline.chomp.split.map{|x| x.to_i}\n lis = []\n n.times do |i|\n lis << STDIN.readline.chomp.split.map{|x|x.to_i}\n end\n \n if (not lis.first.select{|x| x == 1}.empty?) or (not lis.last.select{|x| x == 1}.empty?) then\n puts 2\n return\n else\n lis.each do |x|\n if x.first == 1 or x.last == 1 then\n puts 2\n return\n end\n end\n end\n puts 4\nend\n\nif __FILE__ == $0\n main\nend\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\nn.times do |i|\n a = gets.split(' ')\n if i == 0 || i == n-1\n if a.any? { |e| e == '1' }\n puts 2\n exit\n end\n elsif a.first == '1' || a.last == '1'\n puts 2\n exit\n end\nend\nputs 4\n"}, {"source_code": "n,m=gets.chomp.split.map(&:to_i)\na=gets.chomp.split.include? '1'\nfor i in 0..n-3 do\n\tx=gets.chomp.split\n\tif x[-1]=='1' || x[0]=='1'\n\t\tb=true\n\tend\nend\nc=gets.chomp.split.include? '1'\nputs (a||c||b)? \"2\" : \"4\""}, {"source_code": "def run\n m, n = $stdin.gets.split.map(&:to_i)\n a = $stdin.readlines.map {|line| line.split.map(&:to_i) }\n min = 4\n a.each_with_index do |x, i|\n x.each_with_index do |y, j|\n if y == 1\n cnt = 0\n cnt += 1 if i == 0 or i == m - 1\n cnt += 1 if j == 0 or j == n - 1\n\n if cnt == 1\n min = [2, min].min\n elsif cnt == 2\n min = [1, min].min\n end\n end\n end\n end\n\n puts min\nend\n\nrun if $0 == __FILE__\n"}, {"source_code": "n, m = gets.chomp.split(/ /).map(&:to_i)\nans = 4\nn.times do |i|\n line = gets.chomp.split(/ /).map(&:to_i)\n m.times do |j|\n if line[j] == 1\n ans = 2 if i == 0 || i == n - 1\n ans = 2 if j == 0 || j == m - 1\n end\n end\nend\nputs ans\n"}, {"source_code": "m,n = gets.split(\" \").map(&:to_i)\narr = []\nm.times do\n\tarr << gets.split(\" \").map(&:to_i)\nend\nans = nil\nans = 2 if arr[0].include?(1) || arr[m-1].include?(1)\nunless ans\n\tj = 0\n\t(0..m-1).each do |i|\n\t\tif arr[i][j] == 1\n\t\t\tans = 2\n\t\t\tbreak\n\t\tend\n\tend\nend\n\nunless ans\n\tj = n - 1\n\t(0..m-1).each do |i|\n\t\tif arr[i][j] == 1\n\t\t\tans = 2\n\t\t\tbreak\n\t\tend\n\tend\nend\n\nans = 4 unless ans\nputs ans"}, {"source_code": "n, m = gets.split.collect{|i| i.to_i}\na = Array.new(n)\n(0...n).each do |i|\n a[i] = gets.split\nend\nif a[0].count(\"1\") > 0 or a[n-1].count(\"1\") > 0 or\\\n a.transpose[0].count(\"1\") > 0 or a.transpose[m-1].count(\"1\") > 0\n puts 2\nelse\n puts 4\nend\n"}, {"source_code": "def parse(s)\n s = s.split\n for i in 0...s.length do s[i] = s[i].to_i end\n s\nend\n \nn,m = parse gets\na = []\n(0...n).each { |i|\n a << []\n (parse gets).each { |val|\n a[-1] << val\n } \n}\n\n(a[0]+a[-1]).each do |i| if i == 1 then puts 2; exit 0 end end\n(0...n).each do |i| if a[i][-1] == 1 or a[i][0] == 1 then puts 2; exit 0 end end\nputs 4\n"}, {"source_code": "n,m=gets.split.map(&:to_i)\nd=false\nn.times do |i|\n r=gets.split.map(&:to_i)\n if (i==0||i==n-1)&&r.include?(1)||r[0]==1|| r[m-1]==1\n d=true\n break\n end\nend\np d ? 2:4\n\n"}, {"source_code": "line = gets.chomp.split(\" \").map {|e| e.to_i}\n\nside = false\n\ntable = []\nline[0].times do\n row = gets.chomp.split(\" \").map {|e| e.to_i}\n if ((table.length == 0 || table.length == (line[0] - 1)) && row.include?(1))\n side = true\n end\n\n if row[0] == 1 || row[line[1] - 1] == 1\n side = true\n end\n\n table.push(row)\nend\n\nif side\n puts 2\nelse\n puts 4\nend\n\n\n"}], "negative_code": [{"source_code": "n, m = gets.split.map(&:to_i)\nn.times do\n a = gets.split(' ')\n if a.first == '1' || a.last == '1'\n puts 2\n exit\n end\nend\nputs 4\n"}, {"source_code": "n,m=gets.chomp.split.map(&:to_i)\na=gets.chomp.split.include? '1'\nfor i in 0..n-3 do\n\tx=gets.chomp.split\n\tif x[-1]=='1'\n\t\tb=true\n\tend\nend\nc=gets.chomp.split.include? '1'\nputs (a||c||b)? \"2\" : \"4\""}, {"source_code": "n, m = gets.chomp.split(/ /).map(&:to_i)\ngood_cells = []\nn.times do |i|\n line = gets.chomp.split(/ /).map(&:to_i)\n line.each do |j|\n good_cells << [i, j] if line[j] == 1\n end\nend\nans = 4\ngood_cells.each do |cell|\n x = cell[0]\n y = cell[1]\n if x == 0 || y == 0 || x == n - 1 || y == m - 1\n ans = 2\n break\n end\nend\nputs ans\n"}, {"source_code": "n, m = gets.chomp.split(/ /).map(&:to_i)\ntable = []\ngood_cells = []\nn.times do |i|\n line = gets.chomp.split(/ /).map(&:to_i)\n line.each do |j|\n good_cells << [i, j] if line[j] == 1\n end\n table << line\nend\nans = 4\ngood_cells.each do |cell|\n x = cell[0]\n y = cell[1]\n ans = 2 if x == 0 || y == 0 || x == n - 1 || y == m - 1\nend\nputs ans\n"}, {"source_code": "n, m = gets.chomp.split(/ /).map(&:to_i)\ntable = []\ngood_cells = []\nn.times do |i|\n line = gets.chomp.split(/ /).map(&:to_i)\n line.each do |j|\n good_cells << [i, j] if line[j] == 1\n end\n table << line\nend\nans = 4\ngood_cells.each do |cell|\n x = cell[0]\n y = cell[1]\n ans = 2 if x == 0 || x == n - 1\n ans = 2 if y == 0 || y == m - 1\nend\nputs ans\n"}], "src_uid": "0d2fd9b58142c4f5282507c916690244"} {"nl": {"description": "Integer factorisation is hard. The RSA Factoring Challenge offered $$$$100\\,000$$$ for factoring RSA-$$$1024$$$, a $$$1024$$$-bit long product of two prime numbers. To this date, nobody was able to claim the prize. We want you to factorise a $$$1024$$$-bit number.Since your programming language of choice might not offer facilities for handling large integers, we will provide you with a very simple calculator. To use this calculator, you can print queries on the standard output and retrieve the results from the standard input. The operations are as follows: + x y where $$$x$$$ and $$$y$$$ are integers between $$$0$$$ and $$$n-1$$$. Returns $$$(x+y) \\bmod n$$$. - x y where $$$x$$$ and $$$y$$$ are integers between $$$0$$$ and $$$n-1$$$. Returns $$$(x-y) \\bmod n$$$. * x y where $$$x$$$ and $$$y$$$ are integers between $$$0$$$ and $$$n-1$$$. Returns $$$(x \\cdot y) \\bmod n$$$. / x y where $$$x$$$ and $$$y$$$ are integers between $$$0$$$ and $$$n-1$$$ and $$$y$$$ is coprime with $$$n$$$. Returns $$$(x \\cdot y^{-1}) \\bmod n$$$ where $$$y^{-1}$$$ is multiplicative inverse of $$$y$$$ modulo $$$n$$$. If $$$y$$$ is not coprime with $$$n$$$, then $$$-1$$$ is returned instead. sqrt x where $$$x$$$ is integer between $$$0$$$ and $$$n-1$$$ coprime with $$$n$$$. Returns $$$y$$$ such that $$$y^2 \\bmod n = x$$$. If there are multiple such integers, only one of them is returned. If there are none, $$$-1$$$ is returned instead. ^ x y where $$$x$$$ and $$$y$$$ are integers between $$$0$$$ and $$$n-1$$$. Returns $$${x^y \\bmod n}$$$. Find the factorisation of $$$n$$$ that is a product of between $$$2$$$ and $$$10$$$ distinct prime numbers, all of form $$$4x + 3$$$ for some integer $$$x$$$.Because of technical issues, we restrict number of requests to $$$100$$$.", "input_spec": "The only line contains a single integer $$$n$$$ ($$$21 \\leq n \\leq 2^{1024}$$$). It is guaranteed that $$$n$$$ is a product of between $$$2$$$ and $$$10$$$ distinct prime numbers, all of form $$$4x + 3$$$ for some integer $$$x$$$.", "output_spec": "You can print as many queries as you wish, adhering to the time limit (see the Interaction section for more details). When you think you know the answer, output a single line of form ! k p_1 p_2 ... p_k, where $$$k$$$ is the number of prime factors of $$$n$$$, and $$$p_i$$$ are the distinct prime factors. You may print the factors in any order. Hacks input For hacks, use the following format:. The first should contain $$$k$$$ ($$$2 \\leq k \\leq 10$$$) — the number of prime factors of $$$n$$$. The second should contain $$$k$$$ space separated integers $$$p_1, p_2, \\dots, p_k$$$ ($$$21 \\leq n \\leq 2^{1024}$$$) — the prime factors of $$$n$$$. All prime factors have to be of form $$$4x + 3$$$ for some integer $$$x$$$. They all have to be distinct. ", "sample_inputs": ["21\n\n7\n\n17\n\n15\n\n17\n\n11\n\n-1\n\n15"], "sample_outputs": ["+ 12 16\n\n- 6 10\n\n* 8 15\n\n/ 5 4\n\nsqrt 16\n\nsqrt 5\n\n^ 6 12\n\n! 2 3 7"], "notes": "NoteWe start by reading the first line containing the integer $$$n = 21$$$. Then, we ask for: $$$(12 + 16) \\bmod 21 = 28 \\bmod 21 = 7$$$. $$$(6 - 10) \\bmod 21 = -4 \\bmod 21 = 17$$$. $$$(8 \\cdot 15) \\bmod 21 = 120 \\bmod 21 = 15$$$. $$$(5 \\cdot 4^{-1}) \\bmod 21 = (5 \\cdot 16) \\bmod 21 = 80 \\bmod 21 = 17$$$. Square root of $$$16$$$. The answer is $$$11$$$, as $$$(11 \\cdot 11) \\bmod 21 = 121 \\bmod 21 = 16$$$. Note that the answer may as well be $$$10$$$. Square root of $$$5$$$. There is no $$$x$$$ such that $$$x^2 \\bmod 21 = 5$$$, so the output is $$$-1$$$. $$$(6^{12}) \\bmod 21 = 2176782336 \\bmod 21 = 15$$$. We conclude that our calculator is working, stop fooling around and realise that $$$21 = 3 \\cdot 7$$$."}, "positive_code": [{"source_code": "def modpow(a,b,m)\n r = 1\n while b > 0\n r = r * a % m if b % 2 == 1\n a = a * a % m\n b >>= 1\n end\n return r\nend\n\ndef isprime(n)\n return true if n == 2\n return false if n == 1 || n & 1 == 0\n d = n-1\n d >>= 1 while d & 1 == 0\n 20.times do\n a = rand(n-2) + 1\n t = d\n y = modpow(a,t,n)\n while t != n-1 && y != 1 && y != n-1\n y = (y * y) % n\n t <<= 1\n end\n return false if y != n-1 && t & 1 == 0\n end\n return true\nend\n\ndef solve(n,gn)\n # puts \"solve #{n}\"\n while true\n x = rand(1...n)\n x2 = x * x % gn\n puts \"sqrt #{x2}\"\n STDOUT.flush\n y = gets.chomp.to_i % n\n next if x == y\n gg = x - y\n gg = -gg if gg < 0\n q = n.gcd(gg)\n next if q == 1 or q == n\n # ok\n ret = []\n if isprime(q)\n ret.push(q)\n else\n ret = solve(q,gn)\n end\n q = n/q\n if isprime(q)\n ret.push(q)\n else\n ret.concat(solve(q,gn))\n end\n return ret\n end\nend\n\nn = gets.chomp.to_i\nans = solve(n,n)\nansstr = \"! #{ans.length} #{ans.join(' ')}\"\nputs ansstr\nSTDOUT.flush\n"}, {"source_code": "require 'openssl'\nrequire 'prime'\n\n$stdout.sync = true\n\ndef ask(x)\n puts \"sqrt #{x}\"\n gets.to_i\nend\n\ndef run(n)\n loop do\n a = rand(2..n - 1)\n g = n.gcd(a)\n return g if g != 1\n a2 = a * a % n\n next if a2.gcd(n) != 1\n r = ask(a2) % n\n next if r == -1 || r == a || r == n - a\n g = n.gcd(n + r - a)\n next if g == 1\n return g\n end\nend\n\nn = gets.to_i\nfac = [n]\n\nuntil fac.all?{|f| f.to_bn.prime?}\n x = run(n)\n nfac = []\n fac.each do |y|\n g = x.gcd(y)\n if g == 1 || g == y\n nfac << y\n else\n nfac << g << y / g\n x /= g\n end\n end\n fac << x if x > 1\n fac = nfac\nend\n\nputs \"! #{fac.size} #{fac * ' '}\"\n"}], "negative_code": [{"source_code": "def modpow(a,b,m)\n r = 1\n while b > 0\n r = r * a % m if b % 2 == 1\n a = a * a % m\n b >>= 1\n end\n return r\nend\n\ndef isprime(n)\n return true if n == 2\n return false if n == 1 || n & 1 == 0\n d = n-1\n d >>= 1 while d & 1 == 0\n 20.times do\n a = rand(n-2) + 1\n t = d\n y = modpow(a,t,n)\n while t != n-1 && y != 1 && y != n-1\n y = (y * y) % n\n t <<= 1\n end\n return false if y != n-1 && t & 1 == 0\n end\n return true\nend\n\ndef solve(n,gn)\n # puts \"solve #{n}\"\n while true\n x = rand(1...n)\n x2 = x * x % n\n puts \"sqrt #{x2}\"\n STDOUT.flush\n y = gets.chomp.to_i % n\n next if x == y\n gg = x - y\n gg = -gg if gg < 0\n q = n.gcd(gg)\n next if q == 1 or q == n\n # ok\n ret = []\n if isprime(q)\n ret.push(q)\n else\n ret = solve(q,gn)\n end\n q = n/q\n if isprime(q)\n ret.push(q)\n else\n ret.concat(solve(q,gn))\n end\n return ret\n end\nend\n\nn = gets.chomp.to_i\nans = solve(n,n)\nansstr = \"! #{ans.length} #{ans.join(' ')}\"\nputs ansstr\nSTDOUT.flush\n"}, {"source_code": "def modpow(a,b,m)\n r = 1\n while b > 0\n r = r * a % m if b % 2 == 1\n a = a * a % m\n b >>= 1\n end\n return r\nend\n\ndef isprime(n)\n return true if n == 2\n return false if n == 1 || n & 1 == 0\n d = n-1\n d >>= 1 while d & 1 == 0\n 20.times do\n a = rand(n-2) + 1\n t = d\n y = modpow(a,t,n)\n while t != n-1 && y != 1 && y != n-1\n y = (y * y) % n\n t <<= 1\n end\n return false if y != n-1 && t & 1 == 0\n end\n return true\nend\n\ndef solve(n)\n while true\n x = rand(1...n)\n x2 = x * x % n\n puts \"sqrt #{x2}\"\n STDOUT.flush\n y = gets.chomp.to_i\n next if x == y\n gg = x - y\n gg = -gg if gg < 0\n q = n.gcd(gg)\n next if q == 1 or q == n\n # ok\n ret = []\n if isprime(q)\n ret.push(q)\n else\n ret = solve(q)\n end\n q = n/q\n if isprime(q)\n ret.push(q)\n else\n ret.concat(solve(q))\n end\n return ret\n end\nend\n\nn = gets.chomp.to_i\nans = solve(n)\nm = 1\nfor i in 0...ans.length\n m = m * ans[i]\nend\nif m != n\n exit(1)\nend\nansstr = \"! #{ans.length} #{ans.join(' ')}\"\nputs ansstr\nSTDOUT.flush\n"}, {"source_code": "def modpow(a,b,m)\n r = 1\n while b > 0\n r = r * a % m if b % 2 == 1\n a = a * a % m\n b >>= 1\n end\n return r\nend\n\ndef isprime(n)\n return true if n == 2\n return false if n == 1 || n & 1 == 0\n d = n-1\n d >>= 1 while d & 1 == 0\n 20.times do\n a = rand(n-2) + 1\n t = d\n y = modpow(a,t,n)\n while t != n-1 && y != 1 && y != n-1\n y = (y * y) % n\n t <<= 1\n end\n return false if y != n-1 && t & 1 == 0\n end\n return true\nend\n\ndef solve(n)\n while true\n x = rand(1...n)\n x2 = x * x % n\n puts \"sqrt #{x2}\"\n STDOUT.flush\n y = gets.chomp.to_i\n next if x == y\n gg = x - y\n gg = -gg if gg < 0\n q = n.gcd(gg)\n next if q == 1 or q == n\n # ok\n ret = []\n if isprime(q)\n ret.push(q)\n else\n ret = solve(q)\n end\n q = n/q\n if isprime(q)\n ret.push(q)\n else\n ret.concat(solve(q))\n end\n return ret\n end\nend\n\nn = gets.chomp.to_i\nans = solve(n)\nansstr = \"! #{ans.length} #{ans.join(' ')}\"\nputs ansstr"}, {"source_code": "def modpow(a,b,m)\n r = 1\n while b > 0\n r = r * a % m if b % 2 == 1\n a = a * a % m\n b >>= 1\n end\n return r\nend\n\ndef fa()\n n = 30\n while true\n n = n * n\n n = n % 252521\n end\n return n\nend\ncnt = 0\n\ndef isprime(n)\n return true if n == 2\n return false if n == 1 || n & 1 == 0\n d = n-1\n d >>= 1 while d & 1 == 0\n 20.times do\n a = rand(n-2) + 1\n t = d\n y = modpow(a,t,n)\n while t != n-1 && y != 1 && y != n-1\n y = (y * y) % n\n t <<= 1\n end\n return false if y != n-1 && t & 1 == 0\n end\n return true\nend\n\ndef solve(n)\n while true\n x = rand(1...n)\n x2 = x * x % n\n puts \"sqrt #{x2}\"\n STDOUT.flush\n y = gets.chomp.to_i % n\n next if x == y\n gg = x - y\n gg = -gg if gg < 0\n q = n.gcd(gg)\n next if q == 1 or q == n\n # ok\n ret = []\n if isprime(q)\n ret.push(q)\n else\n ret = solve(q)\n end\n q = n/q\n if isprime(q)\n ret.push(q)\n else\n ret.concat(solve(q))\n end\n return ret\n end\nend\n\nn = gets.chomp.to_i\nans = solve(n)\nansstr = \"! #{ans.length} #{ans.join(' ')}\"\nputs ansstr\nSTDOUT.flush\n"}, {"source_code": "require 'openssl'\nrequire 'prime'\n\n$stdout.sync = true\n$sol = []\n$memo = {}\n\ndef ask(x)\n return -1 if x.gcd($n) != 1\n return -1 if x < 0 || x >= $n\n return $memo[x] if $memo.include?(x)\n puts \"sqrt #{x}\"\n ans = gets.to_i\n $memo[x] = ans\n ans\nend\n\ndef go(n)\n if n.to_bn.prime?\n $sol << n\n return\n end\n\n if n < 100000\n $sol += n.prime_division.map(&:first)\n return\n end\n\n loop do\n a = rand(2..n-1)\n g = n.gcd(a)\n if g != 1 && g != n\n go(g)\n go(n / g)\n break\n end\n a2 = a * a % n\n r = ask(a2)\n next if r == -1\n r %= n\n next unless r * r % n == a2\n next if r == a || r == n - a\n g = n.gcd(n + r - a)\n next if g == 1 || g == n\n go(g)\n go(n / g)\n break\n end\nend\n\nn = gets.to_i\n$n = n\ngo(n)\nputs \"! #{$sol.size} #{$sol.sort.join(' ')}\"\n"}, {"source_code": "require 'openssl'\nrequire 'prime'\n\n$stdout.sync = true\n\ndef ask(x)\n puts \"sqrt #{x}\"\n gets.to_i\nend\n\ndef run(n)\n loop do\n a = rand(2..n - 1)\n g = n.gcd(a)\n return g if g != 1\n a2 = a * a % n\n next if a2.gcd(n) != 1\n r = ask(a2) % n\n next if r == -1 || r == a || r == n - a\n g = n.gcd(n + r - a)\n next if g == 1\n return g\n end\nend\n\nn = gets.to_i\nfac = [n]\n\nuntil fac.all?{|f| f.to_bn.prime?}\n x = run(n)\n nfac = []\n fac.each do |y|\n g = x.gcd(y)\n if g == 1\n nfac << y\n else\n nfac << g << y / g\n x /= g\n end\n end\n fac = nfac\nend\n\nputs \"! #{fac.size} #{fac * ' '}\"\n"}, {"source_code": "require 'openssl'\nrequire 'prime'\n\n$stdout.sync = true\n$sol = []\n\ndef go(n)\n if n.to_bn.prime?\n $sol << n\n return\n end\n\n if n < 100000\n $sol += n.prime_division.map(&:first)\n return\n end\n\n loop do\n a = rand(2..n-1)\n g = n.gcd(a)\n if g != 1 && g != n\n go(g)\n go(n / g)\n break\n end\n a2 = a * a % n\n puts \"sqrt #{a2}\"\n r = gets.to_i\n next if r == -1\n r %= n\n next unless r * r % n == a2\n next if r == a || r == n - a\n g = n.gcd(n + r - a)\n next if g == 1 || g == n\n go(g)\n go(n / g)\n break\n end\nend\n\nn = gets.to_i\ngo(n)\nputs \"! #{$sol.size} #{$sol.sort.join(' ')}\"\n"}, {"source_code": "require 'openssl'\nrequire 'prime'\n\n$stdout.sync = true\n$sol = []\n\ndef go(n)\n if n.to_bn.prime?\n $sol << n\n return\n end\n\n # if n < 100000\n # $sol += n.prime_division.map(&:first)\n # return\n # end\n\n loop do\n a = rand(2..n-1)\n a2 = a * a % n\n if a2 == 0\n g = n.gcd(a)\n next if g == 1 || g == n\n go(g)\n go(n / g)\n break\n end\n puts \"sqrt #{a2}\"\n r = gets.to_i\n next if r == -1\n r %= n\n next unless r * r % n == a2\n next if r == a || r == n - a\n g = n.gcd(n + r - a)\n next if g == 1 || g == n\n go(g)\n go(n / g)\n break\n end\nend\n\nn = gets.to_i\ngo(n)\nputs \"! #{$sol.size} #{$sol.sort.join(' ')}\"\n"}, {"source_code": "require 'openssl'\nrequire 'prime'\n\n$stdout.sync = true\n$sol = []\n$memo = {}\n\ndef ask(x)\n return -1 if x.gcd($n) != 1\n return $memo[x] if $memo.include?(x)\n puts \"sqrt #{x}\"\n ans = gets.to_i\n $memo[x] = ans\n ans\nend\n\ndef go(n)\n if n.to_bn.prime?\n $sol << n\n return\n end\n\n if n < 100000\n $sol += n.prime_division.map(&:first)\n return\n end\n\n loop do\n a = rand(2..n-1)\n g = n.gcd(a)\n if g != 1 && g != n\n go(g)\n go(n / g)\n break\n end\n a2 = a * a % n\n r = ask(a2)\n next if r == -1\n r %= n\n next unless r * r % n == a2\n next if r == a || r == n - a\n g = n.gcd(n + r - a)\n next if g == 1 || g == n\n go(g)\n go(n / g)\n break\n end\nend\n\nn = gets.to_i\n$n = n\ngo(n)\nputs \"! #{$sol.size} #{$sol.sort.join(' ')}\"\n"}, {"source_code": "require 'openssl'\nrequire 'prime'\n\n$stdout.sync = true\n$sol = []\n\ndef ask(x)\n return -1 if x.gcd($n) != 1\n puts \"sqrt #{x}\"\n gets.to_i\nend\n\ndef go(n)\n if n.to_bn.prime?\n $sol << n\n return\n end\n\n if n < 100000\n $sol += n.prime_division.map(&:first)\n return\n end\n\n loop do\n a = rand(2..n-1)\n g = n.gcd(a)\n if g != 1 && g != n\n go(g)\n go(n / g)\n break\n end\n a2 = a * a % n\n r = ask(a2)\n next if r == -1\n r %= n\n next unless r * r % n == a2\n next if r == a || r == n - a\n g = n.gcd(n + r - a)\n next if g == 1 || g == n\n go(g)\n go(n / g)\n break\n end\nend\n\nn = gets.to_i\n$n = n\ngo(n)\nputs \"! #{$sol.size} #{$sol.sort.join(' ')}\"\n"}, {"source_code": "require 'openssl'\nrequire 'prime'\n\n$stdout.sync = true\n$sol = []\n$memo = {}\n\ndef ask(x)\n return -1 if x.gcd($n) != 1\n return -1 if x < 0 || x >= $n\n return $memo[x] if $memo.include?(x)\n puts \"sqrt #{x}\"\n ans = gets.to_i\n $memo[x] = ans\n ans\nend\n\ndef go(n)\n if n.to_bn.prime?\n $sol << n\n return\n end\n\n if n < 1000000\n $sol += n.prime_division.map(&:first)\n return\n end\n\n loop do\n a = rand(2..n-1)\n g = n.gcd(a)\n if g != 1 && g != n\n go(g)\n go(n / g)\n break\n end\n a2 = a * a % n\n r = ask(a2)\n next if r == -1\n r %= n\n next unless r * r % n == a2\n next if r == a || r == n - a\n g = n.gcd(n + r - a)\n next if g == 1 || g == n\n go(g)\n go(n / g)\n break\n end\nend\n\nn = gets.to_i\n$n = n\ngo(n)\nputs \"! #{$sol.size} #{$sol.sort.join(' ')}\"\n"}, {"source_code": "require 'openssl'\nrequire 'prime'\n\n$stdout.sync = true\n$sol = []\n$memo = {}\n\ndef ask(x)\n return -1 if x.gcd($n) != 1\n return -1 if x < 0 || x >= $n\n return $memo[x] if $memo.include?(x)\n puts \"sqrt #{x}\"\n ans = gets.to_i\n $memo[x] = ans\n ans\nend\n\nn = gets.to_i\n$n = n\n\ndef chk(x)\n if x.to_bn.prime?\n $sol << x\n $sol.uniq!\n m = 1\n $sol.each{|t| m *= t}\n if m == $n\n puts \"! #{$sol.size} #{$sol.sort.join(' ')}\"\n exit\n end\n end\nend\n\nloop do\n a = rand(2..n-1)\n g = n.gcd(a)\n if g != 1 && g != n\n chk(g)\n chk(n / g)\n next\n end\n a2 = a * a % n\n r = ask(a2)\n next if r == -1\n r %= n\n next unless r * r % n == a2\n next if r == a || r == n - a\n g = n.gcd(n + r - a)\n next if g == 1 || g == n\n chk(g)\n chk(n / g)\nend\nputs \"! #{$sol.size} #{$sol.sort.join(' ')}\"\n"}, {"source_code": "require 'openssl'\nrequire 'prime'\n\n$stdout.sync = true\n$sol = []\n\ndef go(n)\n if n.to_bn.prime?\n $sol << n\n return\n end\n\n if n < 10000\n $sol += n.prime_division.map(&:first)\n return\n end\n\n loop do\n a = rand(2..n-1)\n a2 = a * a % n\n puts \"sqrt #{a2}\"\n r = gets.to_i\n next if r == -1 || r == a || r == n - a\n g = n.gcd(n + r - a)\n next if g == 1 || g == n\n go(g)\n go(n / g)\n break\n end\nend\n\nn = gets.to_i\ngo(n)\nputs \"! #{$sol.size} #{$sol.sort.join(' ')}\"\n"}, {"source_code": "require 'openssl'\nrequire 'prime'\n\n$stdout.sync = true\n$sol = []\n\ndef go(n)\n if n.to_bn.prime?\n $sol << n\n return\n end\n\n if n < 10000\n $sol += n.prime_division.map(&:first)\n return\n end\n\n loop do\n a = rand(2..n-1)\n a2 = a * a % n\n puts \"sqrt #{a2}\"\n r = gets.to_i\n next if r == -1 || r == a || r == n - a\n g = n.gcd(n + r - a)\n next if g == 1 || g == n\n go(g)\n go(n / g)\n break\n end\nend\n\nn = gets.to_i\ngo(n)\nputs \"! #{$sol.sort.join(' ')}\"\n"}], "src_uid": "166fbe42dd60317e85e699d9bed76957"} {"nl": {"description": "Greg has an array a = a1, a2, ..., an and m operations. Each operation looks as: li, ri, di, (1 ≤ li ≤ ri ≤ n). To apply operation i to the array means to increase all array elements with numbers li, li + 1, ..., ri by value di.Greg wrote down k queries on a piece of paper. Each query has the following form: xi, yi, (1 ≤ xi ≤ yi ≤ m). That means that one should apply operations with numbers xi, xi + 1, ..., yi to the array.Now Greg is wondering, what the array a will be after all the queries are executed. Help Greg.", "input_spec": "The first line contains integers n, m, k (1 ≤ n, m, k ≤ 105). The second line contains n integers: a1, a2, ..., an (0 ≤ ai ≤ 105) — the initial array. Next m lines contain operations, the operation number i is written as three integers: li, ri, di, (1 ≤ li ≤ ri ≤ n), (0 ≤ di ≤ 105). Next k lines contain the queries, the query number i is written as two integers: xi, yi, (1 ≤ xi ≤ yi ≤ m). The numbers in the lines are separated by single spaces.", "output_spec": "On a single line print n integers a1, a2, ..., an — the array after executing all the queries. Separate the printed numbers by spaces. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams of the %I64d specifier.", "sample_inputs": ["3 3 3\n1 2 3\n1 2 1\n1 3 2\n2 3 4\n1 2\n1 3\n2 3", "1 1 1\n1\n1 1 1\n1 1", "4 3 6\n1 2 3 4\n1 2 1\n2 3 2\n3 4 4\n1 2\n1 3\n2 3\n1 2\n1 3\n2 3"], "sample_outputs": ["9 18 17", "2", "5 18 31 20"], "notes": null}, "positive_code": [{"source_code": "n, m, k = gets.split.collect{|i| i.to_i}\na = gets.split.collect{|i| i.to_i}\nop = Array.new(m)\n(0...m).each{|i| op[i] = gets.split.collect{|j| j.to_i}}\n\nq = Array.new(m,0)\nk.times do\n tmp = gets.split.collect{|i| i.to_i-1}\n q[tmp[0]] += 1\n q[tmp[1]+1] -= 1 if tmp[1]+1 < m\nend\n(1...m).each{|i| q[i] += q[i-1]}\n\ndp = Array.new(n,0)\n(0...m).each do |i|\n dp[op[i][0]-1] += op[i][2]*q[i]\n dp[op[i][1]] -= op[i][2]*q[i] if op[i][1] < n\nend\n(1...n).each{|i| dp[i] += dp[i-1]}\nputs (0...n).collect{|i| a[i]+dp[i]}.join(\" \")\n"}], "negative_code": [], "src_uid": "c3120f96894c17957bd8acb968bf37cd"} {"nl": {"description": "A permutation is a sequence of $$$n$$$ integers from $$$1$$$ to $$$n$$$, in which all the numbers occur exactly once. For example, $$$[1]$$$, $$$[3, 5, 2, 1, 4]$$$, $$$[1, 3, 2]$$$ are permutations, and $$$[2, 3, 2]$$$, $$$[4, 3, 1]$$$, $$$[0]$$$ are not.Polycarp was given four integers $$$n$$$, $$$l$$$, $$$r$$$ ($$$1 \\le l \\le r \\le n)$$$ and $$$s$$$ ($$$1 \\le s \\le \\frac{n (n+1)}{2}$$$) and asked to find a permutation $$$p$$$ of numbers from $$$1$$$ to $$$n$$$ that satisfies the following condition: $$$s = p_l + p_{l+1} + \\ldots + p_r$$$. For example, for $$$n=5$$$, $$$l=3$$$, $$$r=5$$$, and $$$s=8$$$, the following permutations are suitable (not all options are listed): $$$p = [3, 4, 5, 2, 1]$$$; $$$p = [5, 2, 4, 3, 1]$$$; $$$p = [5, 2, 1, 3, 4]$$$. But, for example, there is no permutation suitable for the condition above for $$$n=4$$$, $$$l=1$$$, $$$r=1$$$, and $$$s=5$$$.Help Polycarp, for the given $$$n$$$, $$$l$$$, $$$r$$$, and $$$s$$$, find a permutation of numbers from $$$1$$$ to $$$n$$$ that fits the condition above. If there are several suitable permutations, print any of them.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 500$$$). Then $$$t$$$ test cases follow. Each test case consist of one line with four integers $$$n$$$ ($$$1 \\le n \\le 500$$$), $$$l$$$ ($$$1 \\le l \\le n$$$), $$$r$$$ ($$$l \\le r \\le n$$$), $$$s$$$ ($$$1 \\le s \\le \\frac{n (n+1)}{2}$$$). It is guaranteed that the sum of $$$n$$$ for all input data sets does not exceed $$$500$$$.", "output_spec": "For each test case, output on a separate line: $$$n$$$ integers — a permutation of length $$$n$$$ that fits the condition above if such a permutation exists; -1, otherwise. If there are several suitable permutations, print any of them.", "sample_inputs": ["5\n5 2 3 5\n5 3 4 1\n3 1 2 4\n2 2 2 2\n2 1 1 3"], "sample_outputs": ["1 2 3 4 5 \n-1\n1 3 2 \n1 2 \n-1"], "notes": null}, "positive_code": [{"source_code": "require 'set'\r\n\r\ndef tri(k)\r\n\tk*(k + 1)/2\r\nend\r\n\r\ndef get(n, k, swaps)\r\n\ta = (1..k).to_a\r\n\tfor i in 0...k\r\n\t\tj = k - i - 1\r\n\t\tmax = n - i - a[j]\r\n\t\ta[j] += [max, swaps].min\r\n\t\tswaps -= [max, swaps].min\r\n\tend\r\n\ta\r\nend\r\n\r\nt = gets.to_i\r\nt.times do\r\n\tn,l,r,s = gets.split.map &:to_i\r\n\tk = r - l + 1\r\n\tmi = tri(k)\r\n\tma = tri(n) - tri(n - k)\r\n\r\n\tif s < mi || s > ma\r\n\t\tputs -1\r\n\t\tnext\r\n\tend\r\n\r\n\ta = get(n, k, s - tri(k))\r\n\tbad = a.to_set\r\n\t\r\n\tget_next = ->(x){\r\n\t\twhile bad.include?(x)\r\n\t\t\tx += 1\r\n\t\tend\r\n\t\tx\r\n\t}\r\n\t\r\n\tnxt = 0\r\n\t\r\n\tfor i in 1...l do\r\n\t\tnxt = get_next.call(nxt + 1)\r\n\t\tprint nxt, \" \"\r\n\tend\r\n\r\n\tprint a.join(\" \")\r\n\r\n\tfor i in r...n do\r\n\t\tnxt = get_next.call(nxt + 1)\r\n\t\tprint \" \", nxt\r\n\tend\r\n\r\n\tputs \"\"\r\nend\r\n"}], "negative_code": [], "src_uid": "88c8376ad65c5c932c15dc09d6c4d75f"} {"nl": {"description": "Polycarp came up with a new programming language. There are only two types of statements in it: \"x := s\": assign the variable named x the value s (where s is a string). For example, the statement var := hello assigns the variable named var the value hello. Note that s is the value of a string, not the name of a variable. Between the variable name, the := operator and the string contains exactly one space each. \"x = a + b\": assign the variable named x the concatenation of values of two variables a and b. For example, if the program consists of three statements a := hello, b := world, c = a + b, then the variable c will contain the string helloworld. It is guaranteed that the program is correct and the variables a and b were previously defined. There is exactly one space between the variable names and the = and + operators. All variable names and strings only consist of lowercase letters of the English alphabet and do not exceed $$$5$$$ characters.The result of the program is the number of occurrences of string haha in the string that was written to the variable in the last statement.Polycarp was very tired while inventing that language. He asks you to implement it. Your task is — for given program statements calculate the number of occurrences of string haha in the last assigned variable.", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 10^3$$$). Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 50$$$) — the number of statements in the program. All variable names and strings are guaranteed to consist only of lowercase letters of the English alphabet and do not exceed $$$5$$$ characters. This is followed by $$$n$$$ lines describing the statements in the format described above. It is guaranteed that the program is correct.", "output_spec": "For each set of input data, output the number of occurrences of the haha substring in the string that was written to the variable in the last statement.", "sample_inputs": ["4\n6\na := h\nb := aha\nc = a + b\nc = c + c\ne = c + c\nd = a + c\n15\nx := haha\nx = x + x\nx = x + x\nx = x + x\nx = x + x\nx = x + x\nx = x + x\nx = x + x\nx = x + x\nx = x + x\nx = x + x\nx = x + x\nx = x + x\nx = x + x\nx = x + x\n1\nhaha := hah\n5\nhaahh := aaaha\nahhhh = haahh + haahh\nhaahh = haahh + haahh\nahhhh = ahhhh + haahh\nahhaa = haahh + ahhhh"], "sample_outputs": ["3\n32767\n0\n0"], "notes": "NoteIn the first test case the resulting value of d is hhahahaha."}, "positive_code": [{"source_code": "def o (s)\n (0..(s.size - 1)).map {|i| s[i..i+3]}.select {|a| a == \"haha\"}.size\nend\n\ndef add (s)\n [s, o(s)]\nend\n\ndef m (s, t)\n x = s + t\n x = s[0..2] + 'x' + t.reverse[0..2].reverse if x.size > 6\n x\nend\n\ndef cs (s, t)\n s.reverse[0..2].reverse + t[0..2]\nend\n\ndef c (s, t)\n [m(s[0], t[0]), s[1] + t[1] + o(cs(s[0], t[0]))]\nend\n\ndef solve (n, a)\n h = Hash.new\n last = []\n a.each {|s| last = h[s[0]] = (s[1] == ':=' ? add(s[2]) : c(h[s[2]], h[s[4]]))}\n last[1]\nend\n\n(1..(gets.to_i)).each { puts solve(n = gets.to_i, (1..n).map {|i| gets.split(' ')})}\n"}, {"source_code": "def get_occ (s)\r\n (0..(s.size - 1)).map {|i| s[i..i+3]}.select {|a| a == \"haha\"}.size\r\nend\r\n\r\ndef add (s)\r\n [s, get_occ(s)]\r\nend\r\n\r\ndef merge_chars (s, t)\r\n x = s + t\r\n x = s[0..2] + 'x' + t[(t.size - [t.size, 3].min)..(t.size - 1)] if x.size >= 7\r\n x\r\nend\r\n\r\ndef concat_sub_strings (s, t)\r\n s[(s.size - [s.size, 3].min)..(s.size - 1)] + t[0..2]\r\nend\r\n\r\ndef concat (s, t)\r\n [merge_chars(s[0], t[0]), s[1] + t[1] + get_occ(concat_sub_strings(s[0], t[0]))]\r\nend\r\n\r\ndef solve (n, a)\r\n mp = Hash.new\r\n last = []\r\n a.each {|s| last = mp[s[0]] = (s[1] == ':=' ? add(s[2]) : concat(mp[s[2]], mp[s[4]]))}\r\n last[1]\r\nend\r\n\r\nt = gets.to_i\r\n\r\n(1..t).each do\r\n n = gets.to_i\r\n a = (1..n).map {|i| gets.split(' ')}\r\n puts solve(n, a)\r\nend\r\n"}, {"source_code": "t = gets.chomp.to_i\r\n\r\ndef count_em(string, substring)\r\n string.scan(/(?=#{substring})/).count\r\nend\r\n\r\nvars = Hash.new\r\n\r\n(1..t).each do\r\n n = gets.chomp.to_i\r\n (1..n).each do |j|\r\n inp = gets.chomp.split(\" \")\r\n if inp[1] == \":=\"\r\n l = inp[2].length\r\n vars[inp[0]] = {cnt: count_em(inp[2], \"haha\"),\r\n s: inp[2][0..2], end: inp[2][[l - 3, 0].max..l - 1]}\r\n else\r\n tmp = (vars[inp[2]][:end] + vars[inp[4]][:s])\r\n sum = vars[inp[2]][:cnt] + vars[inp[4]][:cnt]\r\n cont = count_em(tmp, \"haha\")\r\n l = tmp.length\r\n s = vars[inp[2]][:s]\r\n if s.length < 3 then s = (s + vars[inp[4]][:s])[0..2] end\r\n endd = vars[inp[4]][:end]\r\n if endd.length < 3\r\n endd = vars[inp[2]][:end] + endd\r\n l = endd.length\r\n endd = endd[[l - 3, 0].max..l - 1]\r\n end\r\n vars[inp[0]] = {cnt: sum + cont,\r\n s: s,\r\n end: endd}\r\n end\r\n puts vars[inp[0]][:cnt] if j == n\r\n end\r\nend"}], "negative_code": [{"source_code": "def get_occ (s)\n (0..(s.size - 1)).map {|i| s[i..i+3]}.select {|a| a ==\"haha\"}.size\nend\n\ndef add (s)\n [s, get_occ(s)]\nend\n\ndef merge_chars (s, t)\n x = s + t\n x = s[0..2] + 'x' + t[(t.size - [t.size, 3].min)..(t.size - 1)] if x.size > 6\n x\nend\n\ndef concat (s, t)\n [merge_chars(s[0], t[0]), s[1] + t[1] + [1, get_occ(s[0] + t[0])].min]\nend\n\ndef solve (n, a)\n mp = Hash.new\n last = []\n a.each {|s| last = mp[s[0]] = (s[1] == ':=' ? add(s[2]):concat(mp[s[2]], mp[s[4]]))}\n last[1]\nend\n\nt = gets.to_i\n\n(1..t).each do\n n = gets.to_i\n a = (1..n).map {|i| gets.split(' ')}\n puts solve(n, a)\nend\n\n"}, {"source_code": "def get_occ (s)\n result = 0\n (0..(s.size - 1)).each do |i|\n a = s[i..i+3]\n result += 1 if a ==\"haha\"\n end\n result\nend\n\n\ndef add (s)\n [s, get_occ(s), s]\nend\n\ndef merge_chars (s, t)\n x = s + t\n x = s[0..3] + 'x' + t[(t.size - [t.size, 3].min)..(t.size - 1)] if x.size > 6\n [x, x]\nend\n\ndef concat (s, t)\n pref, suff = merge_chars(s[0], t[-1])\n [pref, s[1] + t[1] + get_occ(s[-1] + t[0]), suff]\nend\n\ndef solve (n, a)\n mp = Hash.new\n last = []\n a.each do |s|\n if s[1] == ':='\n mp[s[0]] = add(s[2])\n else\n mp[s[0]] = concat(mp[s[2]], mp[s[4]])\n end\n last = mp[s[0]]\n end\n last[1] + 1\nend\n\nt = gets.to_i\n\n(1..t).each do\n n = gets.to_i\n a = (1..n).map{|i| gets.split(' ')}\n puts solve(n, a) >> 1\n\nend\n"}], "src_uid": "356bd72aa9143681f1efd695969a8c8e"} {"nl": {"description": "Alice got an array of length $$$n$$$ as a birthday present once again! This is the third year in a row! And what is more disappointing, it is overwhelmengly boring, filled entirely with zeros. Bob decided to apply some changes to the array to cheer up Alice.Bob has chosen $$$m$$$ changes of the following form. For some integer numbers $$$x$$$ and $$$d$$$, he chooses an arbitrary position $$$i$$$ ($$$1 \\le i \\le n$$$) and for every $$$j \\in [1, n]$$$ adds $$$x + d \\cdot dist(i, j)$$$ to the value of the $$$j$$$-th cell. $$$dist(i, j)$$$ is the distance between positions $$$i$$$ and $$$j$$$ (i.e. $$$dist(i, j) = |i - j|$$$, where $$$|x|$$$ is an absolute value of $$$x$$$).For example, if Alice currently has an array $$$[2, 1, 2, 2]$$$ and Bob chooses position $$$3$$$ for $$$x = -1$$$ and $$$d = 2$$$ then the array will become $$$[2 - 1 + 2 \\cdot 2,~1 - 1 + 2 \\cdot 1,~2 - 1 + 2 \\cdot 0,~2 - 1 + 2 \\cdot 1]$$$ = $$$[5, 2, 1, 3]$$$. Note that Bob can't choose position $$$i$$$ outside of the array (that is, smaller than $$$1$$$ or greater than $$$n$$$).Alice will be the happiest when the elements of the array are as big as possible. Bob claimed that the arithmetic mean value of the elements will work fine as a metric.What is the maximum arithmetic mean value Bob can achieve?", "input_spec": "The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n, m \\le 10^5$$$) — the number of elements of the array and the number of changes. Each of the next $$$m$$$ lines contains two integers $$$x_i$$$ and $$$d_i$$$ ($$$-10^3 \\le x_i, d_i \\le 10^3$$$) — the parameters for the $$$i$$$-th change.", "output_spec": "Print the maximal average arithmetic mean of the elements Bob can achieve. Your answer is considered correct if its absolute or relative error doesn't exceed $$$10^{-6}$$$.", "sample_inputs": ["2 3\n-1 3\n0 0\n-1 -4", "3 2\n0 2\n5 0"], "sample_outputs": ["-2.500000000000000", "7.000000000000000"], "notes": null}, "positive_code": [{"source_code": "N, M = gets.split.map(&:to_i)\nmin, max = 0, 0\nN.times do |i|\n min += (i-N/2).abs\n max += i\nend\n\nans = 0\nM.times do\n x, d = gets.split.map(&:to_i)\n ans += N * x + d * (d>0 ? max : min)\nend\nputs ans.to_f/N"}, {"source_code": "n, m = gets.strip.split.map(&:to_i)\naps = []\nm.times do\n aps << gets.strip.split.map(&:to_i)\nend\n\nps=0\npo=0\naps.each do |ap|\n if ap[1]>=0\n po += ap[0]\n ps += ap[1]\n end\nend\n\nsum = 0\nsum += (po * n)\nsum += (ps * ((n*(n-1))/2))\n\nns=0\nno=0\naps.each do |ap|\n if ap[1]<0\n no += ap[0]\n ns += ap[1]\n end\nend\n\nsum += (no * n)\nif n%2==0\n an = n/2\n sum += (ns * ((an*(an+1))/2))\n sum += (ns * ((an*(an-1))/2))\nelse\n an = n/2\n sum += (ns * ((an*(an+1))/2))\n sum += (ns * ((an*(an+1))/2))\nend\n\nputs (1.0*sum)/(1.0*n)"}], "negative_code": [], "src_uid": "1c8423407ea7a0b2647e41392670d6b7"} {"nl": {"description": "Omkar is building a waterslide in his water park, and he needs your help to ensure that he does it as efficiently as possible.Omkar currently has $$$n$$$ supports arranged in a line, the $$$i$$$-th of which has height $$$a_i$$$. Omkar wants to build his waterslide from the right to the left, so his supports must be nondecreasing in height in order to support the waterslide. In $$$1$$$ operation, Omkar can do the following: take any contiguous subsegment of supports which is nondecreasing by heights and add $$$1$$$ to each of their heights. Help Omkar find the minimum number of operations he needs to perform to make his supports able to support his waterslide!An array $$$b$$$ is a subsegment of an array $$$c$$$ if $$$b$$$ can be obtained from $$$c$$$ by deletion of several (possibly zero or all) elements from the beginning and several (possibly zero or all) elements from the end.An array $$$b_1, b_2, \\dots, b_n$$$ is called nondecreasing if $$$b_i\\le b_{i+1}$$$ for every $$$i$$$ from $$$1$$$ to $$$n-1$$$.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\leq t \\leq 100$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \\leq n \\leq 2 \\cdot 10^5$$$) — the number of supports Omkar has. The second line of each test case contains $$$n$$$ integers $$$a_{1},a_{2},...,a_{n}$$$ $$$(0 \\leq a_{i} \\leq 10^9)$$$ — the heights of the supports. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, output a single integer — the minimum number of operations Omkar needs to perform to make his supports able to support his waterslide.", "sample_inputs": ["3\n4\n5 3 2 5\n5\n1 2 3 5 3\n3\n1 1 1"], "sample_outputs": ["3\n2\n0"], "notes": "NoteThe subarray with which Omkar performs the operation is bolded.In the first test case:First operation:$$$[5, 3, \\textbf{2}, 5] \\to [5, 3, \\textbf{3}, 5]$$$Second operation:$$$[5, \\textbf{3}, \\textbf{3}, 5] \\to [5, \\textbf{4}, \\textbf{4}, 5]$$$Third operation:$$$[5, \\textbf{4}, \\textbf{4}, 5] \\to [5, \\textbf{5}, \\textbf{5}, 5]$$$In the third test case, the array is already nondecreasing, so Omkar does $$$0$$$ operations."}, "positive_code": [{"source_code": "# your code goes here\n\nt = gets.chomp.to_i\nfor tc in 1..t\n\tn = gets.chomp.to_i;\n\ta = gets.split\n\tfor i in 0..(n-1)\n\t\ta[i] = a[i].to_i\n\tend\n\tb = Array.new(n, 0)\n\tmax = a[0]\n\tfor i in 1..(n-1)\n\t\tif a[i] > max\n\t\t\tmax = a[i]\n\t\tend\n\t\tb[i] = max - a[i]\n\tend\n\tans = 0\n\tfor i in 1..(n-1)\n\t\tif b[i] > b[i-1]\n\t\t\tans += b[i] - b[i-1]\n\t\tend\n\tend\n\tputs ans\nend"}], "negative_code": [{"source_code": "t = gets.chomp.to_i\nfor tc in 1..t\n\tn = gets.chomp.to_i;\n\ta = gets.split\n\tfor i in 0..(n-1)\n\t\ta[i] = a[i].to_i\n\tend\n\tans = 0\n\tfor i in 1..(n-1)\n\t\tif a[i] > a[i-1]\n\t\t\tans += a[i] - a[i-1]\n\t\tend\n\tend\n\tputs ans\nend"}], "src_uid": "0bd06900e42db9f83cdd43c24da6686e"} {"nl": {"description": "You have an axis-aligned rectangle room with width $$$W$$$ and height $$$H$$$, so the lower left corner is in point $$$(0, 0)$$$ and the upper right corner is in $$$(W, H)$$$.There is a rectangular table standing in this room. The sides of the table are parallel to the walls, the lower left corner is in $$$(x_1, y_1)$$$, and the upper right corner in $$$(x_2, y_2)$$$.You want to place another rectangular table in this room with width $$$w$$$ and height $$$h$$$ with the width of the table parallel to the width of the room.The problem is that sometimes there is not enough space to place the second table without intersecting with the first one (there are no problems with tables touching, though).You can't rotate any of the tables, but you can move the first table inside the room. Example of how you may move the first table. What is the minimum distance you should move the first table to free enough space for the second one?", "input_spec": "The first line contains the single integer $$$t$$$ ($$$1 \\le t \\le 5000$$$) — the number of the test cases. The first line of each test case contains two integers $$$W$$$ and $$$H$$$ ($$$1 \\le W, H \\le 10^8$$$) — the width and the height of the room. The second line contains four integers $$$x_1$$$, $$$y_1$$$, $$$x_2$$$ and $$$y_2$$$ ($$$0 \\le x_1 < x_2 \\le W$$$; $$$0 \\le y_1 < y_2 \\le H$$$) — the coordinates of the corners of the first table. The third line contains two integers $$$w$$$ and $$$h$$$ ($$$1 \\le w \\le W$$$; $$$1 \\le h \\le H$$$) — the width and the height of the second table.", "output_spec": "For each test case, print the minimum distance you should move the first table, or $$$-1$$$ if there is no way to free enough space for the second table. Your answer will be considered correct if its absolute or relative error doesn't exceed $$$10^{-6}$$$.", "sample_inputs": ["5\n8 5\n2 1 7 4\n4 2\n5 4\n2 2 5 4\n3 3\n1 8\n0 3 1 6\n1 5\n8 1\n3 0 6 1\n5 1\n8 10\n4 5 7 8\n8 5"], "sample_outputs": ["1.000000000\n-1\n2.000000000\n2.000000000\n0.000000000"], "notes": "NoteThe configuration of the first test case is shown in the picture. But the movement of the first table is not optimal. One of the optimal movement, for example, is to move the table by $$$(0, -1)$$$, so the lower left corner will move from $$$(2, 1)$$$ to $$$(2, 0)$$$. Then you can place the second table at $$$(0, 3)-(4, 5)$$$.In the second test case, there is no way to fit both tables in the room without intersecting.In the third test case, you can move the first table by $$$(0, 2)$$$, so the lower left corner will move from $$$(0, 3)$$$ to $$$(0, 5)$$$."}, "positive_code": [{"source_code": "gets.to_i.times.each do\r\n w0, h0 = gets.split.map(&:to_i)\r\n x1, y1, x2, y2 = gets.split.map(&:to_i)\r\n w, h = gets.split.map(&:to_i)\r\n dw = [w - [x1, w0 - x2].max, 0].max\r\n dh = [h - [y1, h0 - y2].max, 0].max\r\n nw = w0 < x2 - x1 + w\r\n nh = h0 < y2 - y1 + h\r\n puts case\r\n when nw && nh then -1\r\n when nw then dh\r\n when nh then dw\r\n else [dw, dh].min\r\n end\r\nend"}, {"source_code": "MOD = 10 ** 9 + 7\r\ndef gs; gets.chomp; end\r\ndef gi; gets.to_i; end\r\ndef gli; gets.split.map(&:to_i); end\r\ndef cyesno(x); puts x ? :Yes : :No; end\r\ndef uyesno(x); puts x ? :YES : :NO; end\r\ndef array_to_hash_by_count(c); Hash.new(0).tap { |x| c.each { |z| x[z] += 1 } }; end\r\n\r\ndef solve\r\n w, h = gli\r\n x1, y1, x2, y2 = gli\r\n wq, hq = gli\r\n w1 = x2 - x1\r\n h1 = y2 - y1\r\n if w1 + wq > w && h1 + hq > h\r\n puts (-1) ** 1\r\n else\r\n d = []\r\n d << [wq - x1, 0].max if w1 + wq <= w\r\n d << [hq - y1, 0].max if h1 + hq <= h\r\n d << [y2 - (h - hq), 0].max if h1 + hq <= h\r\n d << [x2 - (w - wq), 0].max if w1 + wq <= w\r\n puts d.min\r\n end\r\nend\r\n\r\ndef main\r\n gi.times { solve }\r\nend\r\n\r\nmain\r\n"}], "negative_code": [{"source_code": "gets.to_i.times.each do\r\n w0, h0 = gets.split.map(&:to_i)\r\n x1, y1, x2, y2 = gets.split.map(&:to_i)\r\n w, h = gets.split.map(&:to_i)\r\n dw = w - [x1, w0 - x2].max\r\n dh = h - [y1, h0 - y2].max\r\n nw = w0 < x2 - x1 + w\r\n nh = h0 < y2 - y1 + h\r\n puts case\r\n when nw && nh then -1\r\n when nw then dh\r\n when nh then dw\r\n else [dh, dw].min\r\n end\r\nend"}], "src_uid": "29fd4c77a2f28478ebce98dfc6496aac"} {"nl": {"description": "We have a secret array. You don't know this array and you have to restore it. However, you know some facts about this array: The array consists of $$$n$$$ distinct positive (greater than $$$0$$$) integers. The array contains two elements $$$x$$$ and $$$y$$$ (these elements are known for you) such that $$$x < y$$$. If you sort the array in increasing order (such that $$$a_1 < a_2 < \\ldots < a_n$$$), differences between all adjacent (consecutive) elements are equal (i.e. $$$a_2 - a_1 = a_3 - a_2 = \\ldots = a_n - a_{n-1})$$$. It can be proven that such an array always exists under the constraints given below.Among all possible arrays that satisfy the given conditions, we ask you to restore one which has the minimum possible maximum element. In other words, you have to minimize $$$\\max(a_1, a_2, \\dots, a_n)$$$.You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains three integers $$$n$$$, $$$x$$$ and $$$y$$$ ($$$2 \\le n \\le 50$$$; $$$1 \\le x < y \\le 50$$$) — the length of the array and two elements that are present in the array, respectively.", "output_spec": "For each test case, print the answer: $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of the required array. If there are several answers, you can print any (it also means that the order of elements doesn't matter). It can be proven that such an array always exists under the given constraints.", "sample_inputs": ["5\n2 1 49\n5 20 50\n6 20 50\n5 3 8\n9 13 22"], "sample_outputs": ["1 49 \n20 40 30 50 10\n26 32 20 38 44 50 \n8 23 18 13 3 \n1 10 13 4 19 22 25 16 7"], "notes": null}, "positive_code": [{"source_code": "def solve\n n, x, y = gets.strip.split(' ').map(&:to_i)\n my_stack = []\n\n 51.times { |i|\n next if i.zero?\n el = x\n count = n - 1\n flg = false\n while el < y\n if count.zero? || el + i > y\n flg = true\n break\n end\n el += i\n count -= 1\n end\n next if flg == true\n my_stack << i\n }\n\n res = []\n mn = 1e9 + 1\n my_stack.each do |e|\n list = [x]\n count = n - 1\n el = x\n while el + e <= y && !count.zero?\n el += e\n list.append(el)\n count -= 1\n end \n el = x\n while el - e > 0 && !count.zero?\n el -= e\n list.prepend(el)\n count -= 1\n end\n el = y\n while !count.zero?\n el += e\n list.append(el)\n count -= 1\n end\n if list.max < mn\n mn = list.max\n res = list\n end\n end\n puts res.join(' ')\nend\n\ngets.strip.to_i.times { solve }"}], "negative_code": [{"source_code": "def solve\n n, x, y = gets.strip.split(' ').map(&:to_i)\n my_stack = []\n\n 51.times { |i|\n next if i.zero?\n el = x\n count = n - 1\n flg = false\n while el < y\n if count.zero? || el + i > y\n flg = true\n break\n end\n el += i\n count -= 1\n end\n next if flg == true\n my_stack << i\n }\n\n res = []\n mn = 1e9 + 1\n my_stack.each do |e|\n list = [x]\n count = n - 1\n el = x\n while el - e > 0 && !count.zero?\n el -= e\n list.prepend(el)\n count -= 1\n end \n el = x\n while !count.zero?\n el += e\n list.append(el)\n count -= 1\n end\n if list.max < mn\n mn = list.max\n res = list\n end\n end\n puts res.join(' ')\nend\n\ngets.strip.to_i.times { solve }"}], "src_uid": "ca9d97e731e86cf8223520f39ef5d945"} {"nl": {"description": "Young wilderness explorers set off to their first expedition led by senior explorer Russell. Explorers went into a forest, set up a camp and decided to split into groups to explore as much interesting locations as possible. Russell was trying to form groups, but ran into some difficulties...Most of the young explorers are inexperienced, and sending them alone would be a mistake. Even Russell himself became senior explorer not long ago. Each of young explorers has a positive integer parameter $$$e_i$$$ — his inexperience. Russell decided that an explorer with inexperience $$$e$$$ can only join the group of $$$e$$$ or more people.Now Russell needs to figure out how many groups he can organize. It's not necessary to include every explorer in one of the groups: some can stay in the camp. Russell is worried about this expedition, so he asked you to help him.", "input_spec": "The first line contains the number of independent test cases $$$T$$$($$$1 \\leq T \\leq 2 \\cdot 10^5$$$). Next $$$2T$$$ lines contain description of test cases. The first line of description of each test case contains the number of young explorers $$$N$$$ ($$$1 \\leq N \\leq 2 \\cdot 10^5$$$). The second line contains $$$N$$$ integers $$$e_1, e_2, \\ldots, e_N$$$ ($$$1 \\leq e_i \\leq N$$$), where $$$e_i$$$ is the inexperience of the $$$i$$$-th explorer. It's guaranteed that sum of all $$$N$$$ doesn't exceed $$$3 \\cdot 10^5$$$.", "output_spec": "Print $$$T$$$ numbers, each number on a separate line. In $$$i$$$-th line print the maximum number of groups Russell can form in $$$i$$$-th test case.", "sample_inputs": ["2\n3\n1 1 1\n5\n2 3 1 2 2"], "sample_outputs": ["3\n2"], "notes": "NoteIn the first example we can organize three groups. There will be only one explorer in each group. It's correct because inexperience of each explorer equals to $$$1$$$, so it's not less than the size of his group.In the second example we can organize two groups. Explorers with inexperience $$$1$$$, $$$2$$$ and $$$3$$$ will form the first group, and the other two explorers with inexperience equal to $$$2$$$ will form the second group.This solution is not unique. For example, we can form the first group using the three explorers with inexperience equal to $$$2$$$, and the second group using only one explorer with inexperience equal to $$$1$$$. In this case the young explorer with inexperience equal to $$$3$$$ will not be included in any group."}, "positive_code": [{"source_code": "def f(x)\n return -100_000 if x < 0\n return $f[x]\nend\n\n1.upto(gets.to_i) do\n n = gets.to_i\n a = gets.split(' ').map(&:to_i).sort.unshift(0)\n $f = Array.new(n, 0)\n\n a.each_with_index do |e, i|\n next if i.zero?\n\n $f[i] = [f(i-1), f(i-e)+1].max\n end\n\n puts $f.last\nend\n"}], "negative_code": [{"source_code": "1.upto(gets.to_i) do\n gets\n a = gets.split(' ').map(&:to_i).sort.reverse\n\n result = 0\n while !a.empty?\n break if a.first > a.size\n\n a.shift(a.first)\n result += 1\n end\n\n puts result\nend\n"}], "src_uid": "8e766dea94dc2033ba2d5759d7e5cd80"} {"nl": {"description": "In this problem, a $$$n \\times m$$$ rectangular matrix $$$a$$$ is called increasing if, for each row of $$$i$$$, when go from left to right, the values strictly increase (that is, $$$a_{i,1}<a_{i,2}<\\dots<a_{i,m}$$$) and for each column $$$j$$$, when go from top to bottom, the values strictly increase (that is, $$$a_{1,j}<a_{2,j}<\\dots<a_{n,j}$$$).In a given matrix of non-negative integers, it is necessary to replace each value of $$$0$$$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of $$$0$$$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).", "input_spec": "The first line contains integers $$$n$$$ and $$$m$$$ ($$$3 \\le n, m \\le 500$$$) — the number of rows and columns in the given matrix $$$a$$$. The following lines contain $$$m$$$ each of non-negative integers — the values in the corresponding row of the given matrix: $$$a_{i,1}, a_{i,2}, \\dots, a_{i,m}$$$ ($$$0 \\le a_{i,j} \\le 8000$$$). It is guaranteed that for all $$$a_{i,j}=0$$$, $$$1 < i < n$$$ and $$$1 < j < m$$$ are true.", "output_spec": "If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.", "sample_inputs": ["4 5\n1 3 5 6 7\n3 0 7 0 9\n5 0 0 0 10\n8 9 10 11 12", "3 3\n1 2 3\n2 0 4\n4 5 6", "3 3\n1 2 3\n3 0 4\n4 5 6", "3 3\n1 2 3\n2 3 4\n3 4 2"], "sample_outputs": ["144", "30", "-1", "-1"], "notes": "NoteIn the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value $$$3$$$ must be put in the middle cell.In the third example, the desired resultant matrix does not exist."}, "positive_code": [{"source_code": "# https://codeforces.com/problemset/problem/1231/C\n\ninput = gets.split(' ').map(&:to_i)\nn = input[0]\nm = input[1]\n\nanswer = 0\nmatrix = []\n\nn.times do \n matrix << gets.split(' ').map(&:to_i)\nend\n\ninvalid = false\n\nmatrix.to_enum.with_index.reverse_each do |row, row_index|\n break if invalid\n row.to_enum.with_index.reverse_each do |number, col_index|\n break if invalid\n\n if number == 0 \n number = matrix[row_index][col_index] =\n [matrix[row_index][col_index+1], matrix[row_index+1][col_index]].min - 1\n elsif (row_index != n-1 &&\n matrix[row_index][col_index] >= matrix[row_index+1][col_index]) ||\n (col_index != m-1 &&\n matrix[row_index][col_index] >= matrix[row_index][col_index+1])\n invalid = true\n answer = -1\n break\n end\n\n answer += number\n end\nend\n\nputs answer\n"}], "negative_code": [], "src_uid": "ebc5c1fe4b4a253647b82a608e8a084f"} {"nl": {"description": "Vasya claims that he had a paper square. He cut it into two rectangular parts using one vertical or horizontal cut. Then Vasya informed you the dimensions of these two rectangular parts. You need to check whether Vasya originally had a square. In other words, check if it is possible to make a square using two given rectangles.", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case is given in two lines. The first line contains two integers $$$a_1$$$ and $$$b_1$$$ ($$$1 \\le a_1, b_1 \\le 100$$$) — the dimensions of the first one obtained after cutting rectangle. The sizes are given in random order (that is, it is not known which of the numbers is the width, and which of the numbers is the length). The second line contains two integers $$$a_2$$$ and $$$b_2$$$ ($$$1 \\le a_2, b_2 \\le 100$$$) — the dimensions of the second obtained after cutting rectangle. The sizes are given in random order (that is, it is not known which of the numbers is the width, and which of the numbers is the length).", "output_spec": "Print $$$t$$$ answers, each of which is a string \"YES\" (in the case of a positive answer) or \"NO\" (in the case of a negative answer). The letters in words can be printed in any case (upper or lower).", "sample_inputs": ["3\n2 3\n3 1\n3 2\n1 3\n3 3\n1 3"], "sample_outputs": ["Yes\nYes\nNo"], "notes": null}, "positive_code": [{"source_code": "gets.to_i.times do\n a = gets.split.map(&:to_i).sort\n b = gets.split.map(&:to_i).sort\n puts a[0] + b[0] == a[1] && a[1] == b[1] ? :Yes : :No\nend"}, {"source_code": "n = gets\nn = n.to_i\n\nwhile n > 0 do\n a = gets\n a = a.split\n a[0] = a[0].to_f\n a[1] = a[1].to_f\n\n area1 = a[0] * a[1]\n #puts area1\n\n b = gets\n b = b.split\n\n b[0] = b[0].to_f\n b[1] = b[1].to_f\n\n area2 = b[0] * b[1]\n \n area_total = area1 + area2\n\n\n if a[0] + b[0] == a[1] and a[0] + b[0] == b[1]\n puts \"Yes\"\n elsif a[0] + b[1] == a[1] and a[0] + b[1] == b[0]\n puts \"Yes\"\n elsif a[1] + b[0] == a[0] and a[1] + b[0] == b[1]\n puts \"Yes\"\n elsif a[1] + b[1] == a[0] and a[1] + b[1] == b[0]\n puts \"Yes\"\n else\n puts \"No\"\n end\n n = n - 1\nend"}, {"source_code": "t = gets.to_i\nt.times do\n\ta, b = gets.split.map(&:to_i)\n\tc, d = gets.split.map(&:to_i)\n\tif (a == d && a == b + c) || (b == c && b == a + d) || (a == c && a == b + d) || (b == d && b == a + c)\n\t\tputs \"Yes\"\n\telse\n\t\tputs \"No\"\n\tend\nend"}, {"source_code": "i = gets.chomp.to_i\n\ndef isCube(b, c, d, e)\n if b + d == c && b + d == e\n return 1\n end\n\n if b + e == c && b + e == d\n return 1\n end\n\n if c + d == b && c + d == e\n return 1\n end\n\n if c + e == b && c + e == d\n return 1\n end\n\n return 0\nend\n\nwhile (i > 0)\n a1, a2 = gets.split.map(&:to_i)\n b1, b2 = gets.split.map(&:to_i)\n\n if isCube(a1, a2, b1, b2) == 1\n puts 'Yes'\n else\n puts 'No'\n end\n\n i -= 1\nend"}, {"source_code": "1.upto(gets.chomp.to_i) do\n\n a,b = gets.chomp.split.map(&:to_i)\n c,d = gets.chomp.split.map(&:to_i)\n\n if ([a,b].max == [c,d].max) && ([a,b].min + [c,d].min == [a,b].max)\n puts 'Yes'\n else\n puts 'No'\n end\n\nend\n"}, {"source_code": "n = gets.to_i\nfor i in 1..n\n a1, b1 = gets.chomp.split(' ').map(&:to_i)\n a2, b2 = gets.chomp.split(' ').map(&:to_i)\n f0 = ((a1 + a2) == b1) && (a1 + a2) == b2\n f1 = ((a1 + b2) == b1) && (a1 + b2) == a2\n f2 = ((b1 + a2) == a1) && (b1 + a2) == b2\n f3 = ((b1 + b2) == a1) && (b1 + b2) == a2\n f = f0 || f1 || f2 || f3\n if f\n puts \"Yes\"\n else\n puts \"No\"\n end\nend"}, {"source_code": "t = gets.chomp.to_i\nwhile t > 0\n r1 = gets.chomp.split(' ').map { |x| x.to_i }.sort\n r2 = gets.chomp.split(' ').map { |x| x.to_i }.sort\n\n if r1.last != r2.last\n puts 'No'\n elsif r1.last == r1.first + r2.first\n puts 'Yes'\n else\n puts 'No'\n end\n\n t -= 1\nend\n"}], "negative_code": [{"source_code": "n = gets\nn = n.to_i\n\nwhile n > 0 do\n a = gets\n a = a.split\n a[0] = a[0].to_f\n a[1] = a[1].to_f\n\n area1 = a[0] * a[1]\n #puts area1\n\n b = gets\n b = b.split\n\n b[0] = b[0].to_f\n b[1] = b[1].to_f\n\n if a[0] + b[0] == a[1]\n puts \"yes\"\n elsif a[0] + b[1] == a[1]\n puts \"yes\"\n elsif a[1] + b[0] == a[0]\n puts \"yes\"\n elsif a[1] + b[1] == a[0]\n puts \"yes\"\n else\n puts \"no\"\n end\n n = n - 1\nend"}, {"source_code": "n = gets\nn = n.to_i\n\nwhile n > 0 do\n a = gets\n a = a.split\n a[0] = a[0].to_f\n a[1] = a[1].to_f\n\n area1 = a[0] * a[1]\n #puts area1\n\n b = gets\n b = b.split\n\n b[0] = b[0].to_f\n b[1] = b[1].to_f\n\n area2 = b[0] * b[1]\n \n area_total = area1 + area2\n\n\n if a[0] + b[0] == a[1] and a[0] + b[0] == b[1]\n puts \"Yes\"\n elsif a[0] + b[1] == a[1] and a[0] + b[1] == b[1]\n puts \"Yes\"\n elsif a[1] + b[0] == a[0] and a[1] + b[0] == b[0]\n puts \"Yes\"\n elsif a[1] + b[1] == a[0] and a[1] + b[1] == b[0]\n puts \"Yes\"\n else\n puts \"No\"\n end\n n = n - 1\nend"}, {"source_code": "n = gets\nn = n.to_i\n\nwhile n > 0 do\n a = gets\n a = a.split\n a[0] = a[0].to_f\n a[1] = a[1].to_f\n\n area1 = a[0] * a[1]\n #puts area1\n\n b = gets\n b = b.split\n\n b[0] = b[0].to_f\n b[1] = b[1].to_f\n\n if a[0] + b[0] == a[1]\n puts \"Yes\"\n elsif a[0] + b[1] == a[1]\n puts \"Yes\"\n elsif a[1] + b[0] == a[0]\n puts \"Yes\"\n elsif a[1] + b[1] == a[0]\n puts \"Yes\"\n else\n puts \"No\"\n end\n n = n - 1\nend"}, {"source_code": "n = gets\nn = n.to_i\n\nwhile n > 0 do\n a = gets\n a = a.split\n a[0] = a[0].to_f\n a[1] = a[1].to_f\n\n area1 = a[0] * a[1]\n\n b = gets\n b = b.split\n\n b[0] = b[0].to_f\n b[1] = b[1].to_f\n\n area2 = b[0] * b[1]\n\n area_total = area1 + area2\n\n if Math.sqrt(area_total) % 1 == 0\n if ( a[0] + b[0] != a[1] + b[1] ) or ( a[0] + b[1] != a[1] + b[0] )\n puts \"no\"\n break\n end\n puts \"yes\"\n else\n puts \"no\"\n end\n n = n - 1\nend"}, {"source_code": "n = gets\nn = n.to_i\n\nwhile n > 0 do\n a = gets\n a = a.split\n a[0] = a[0].to_f\n a[1] = a[1].to_f\n\n area1 = a[0] * a[1]\n #puts area1\n\n b = gets\n b = b.split\n\n b[0] = b[0].to_f\n b[1] = b[1].to_f\n\n area2 = b[0] * b[1]\n\n area_total = area1 + area2\n\n if Math.sqrt(area_total) % 1 == 0\n if (( a[0] + b[0] != a[1] + b[1] ) and ( a[0] + b[1] != a[1] + b[0] )) and Math.sqrt(area_total) % 2 == 0\n puts \"no\"\n else\n puts \"yes\"\n end\n\n else\n puts \"no\"\n end\n n = n - 1\nend"}, {"source_code": "n = gets\nn = n.to_i\n\nwhile n > 0 do\n a = gets\n a = a.split\n a[0] = a[0].to_f\n a[1] = a[1].to_f\n\n area1 = a[0] * a[1]\n #puts area1\n\n b = gets\n b = b.split\n\n b[0] = b[0].to_f\n b[1] = b[1].to_f\n\n area2 = b[0] * b[1]\n\n area_total = area1 + area2\n\n if Math.sqrt(area_total) % 1 == 0\n if ( a[0] + b[0] != a[1] + b[1] ) or ( a[0] + b[1] != a[1] + b[0] )\n puts \"no\"\n break\n end\n puts \"yes\"\n else\n puts \"no\"\n end\n n = n - 1\nend"}, {"source_code": "n = gets\nn = n.to_i\n\nwhile n > 0 do\n a = gets\n a = a.split\n a[0] = a[0].to_f\n a[1] = a[1].to_f\n\n area1 = a[0] * a[1]\n #puts area1\n\n b = gets\n b = b.split\n\n b[0] = b[0].to_f\n b[1] = b[1].to_f\n\n area2 = b[0] * b[1]\n\n area_total = area1 + area2\n\n if Math.sqrt(area_total) % 1 == 0\n if ( a[0] + b[0] != a[1] + b[1] ) or ( a[0] + b[1] != a[1] + b[0] )\n puts \"no\"\n else\n puts \"yes\"\n end\n\n else\n puts \"no\"\n end\n n = n - 1\nend"}, {"source_code": "n = gets\nn = n.to_i\n\nwhile n > 0 do\n a = gets\n a = a.split\n a[0] = a[0].to_f\n a[1] = a[1].to_f\n\n area1 = a[0] * a[1]\n #puts area1\n\n b = gets\n b = b.split\n\n b[0] = b[0].to_f\n b[1] = b[1].to_f\n\n area2 = b[0] * b[1]\n\n area_total = area1 + area2\n\n if Math.sqrt(area_total) % 1 == 0\n puts \"yes\"\n else\n puts \"no\"\n end\n n = n - 1\nend"}, {"source_code": "n = gets\nn = n.to_i\n\nwhile n > 0 do\n a = gets\n a = a.split\n a[0] = a[0].to_f\n a[1] = a[1].to_f\n\n area1 = a[0] * a[1]\n #puts area1\n\n b = gets\n b = b.split\n\n b[0] = b[0].to_f\n b[1] = b[1].to_f\n\n area2 = b[0] * b[1]\n\n area_total = area1 + area2\n\n if Math.sqrt(area_total) % 1 == 0\n if (( a[0] + b[0] != a[1] + b[1] ) or ( a[0] + b[1] != a[1] + b[0] )) and Math.sqrt(area_total) % 2 == 0\n puts \"no\"\n else\n puts \"yes\"\n end\n\n else\n puts \"no\"\n end\n n = n - 1\nend"}, {"source_code": "t = gets.to_i\nt.times do\n\ta, b = gets.split.map(&:to_i)\n\tc, d = gets.split.map(&:to_i)\n\tif (a == d && a == b + c) || (b == c && b == a + d)\n\t\tputs \"Yes\"\n\telse\n\t\tputs \"No\"\n\tend\nend"}, {"source_code": "t = gets.to_i\nt.times do\n\ta, b = gets.split.map(&:to_i)\n\tc, d = gets.split.map(&:to_i)\n\tif (a == d && a == b + c) || (b == c && b == a + d) || (a == c && a == b + d)\n\t\tputs \"Yes\"\n\telse\n\t\tputs \"No\"\n\tend\nend"}, {"source_code": "n = gets.to_i\nfor i in 1..n\n a1, b1 = gets.chomp.split(' ').map(&:to_i)\n a2, b2 = gets.chomp.split(' ').map(&:to_i)\n if a1 == a2\n if b1 + b2 == a1\n puts \"YES\"\n else\n puts \"NO\"\n end\n elsif a1 == b2\n if b1 + a2 == a1\n puts \"YES\"\n else\n puts \"NO\"\n end\n elsif a2 == b1\n if a1 + b2 == a2\n puts \"YES\"\n else\n puts \"NO\"\n end\n elsif b1 == b2\n if a1 + a2 == b2\n puts \"YES\"\n else\n puts \"NO\"\n end\n else\n puts \"NO\"\n end\nend"}], "src_uid": "a375dd323b7adbfa9f1cad9aa48f7247"} {"nl": {"description": "You may have already known that a standard ICPC team consists of exactly three members. The perfect team however has more restrictions. A student can have some specialization: coder or mathematician. She/he can have no specialization, but can't have both at the same time.So the team is considered perfect if it includes at least one coder, at least one mathematician and it consists of exactly three members.You are a coach at a very large university and you know that $$$c$$$ of your students are coders, $$$m$$$ are mathematicians and $$$x$$$ have no specialization.What is the maximum number of full perfect teams you can distribute them into? Note that some students can be left without a team and each student can be a part of no more than one team.You are also asked to answer $$$q$$$ independent queries.", "input_spec": "The first line contains a single integer $$$q$$$ ($$$1 \\le q \\le 10^4$$$) — the number of queries. Each of the next $$$q$$$ lines contains three integers $$$c$$$, $$$m$$$ and $$$x$$$ ($$$0 \\le c, m, x \\le 10^8$$$) — the number of coders, mathematicians and students without any specialization in the university, respectively. Note that the no student is both coder and mathematician at the same time. ", "output_spec": "Print $$$q$$$ integers — the $$$i$$$-th of them should be the answer to the $$$i$$$ query in the order they are given in the input. The answer is the maximum number of full perfect teams you can distribute your students into. ", "sample_inputs": ["6\n1 1 1\n3 6 0\n0 0 0\n0 1 1\n10 1 10\n4 4 1"], "sample_outputs": ["1\n3\n0\n0\n1\n3"], "notes": "NoteIn the first example here are how teams are formed: the only team of 1 coder, 1 mathematician and 1 without specialization; all three teams consist of 1 coder and 2 mathematicians; no teams can be formed; no teams can be formed; one team consists of 1 coder, 1 mathematician and 1 without specialization, the rest aren't able to form any team; one team consists of 1 coder, 1 mathematician and 1 without specialization, one consists of 2 coders and 1 mathematician and one consists of 1 coder and 2 mathematicians. "}, "positive_code": [{"source_code": "n = gets.to_i\nn.times do\n\ta, b, c = gets.split.map(&:to_i)\n\tif a < b\n\t\tx = a\n\telse\n\t\tx = b end\n\tm = a + b + c - 3 * x\n\tif m >= 0\n\t\tprint \"#{x}\\n\"\n\telse\n\t\tprint \"#{(3 * x + m) / 3}\\n\" end\nend"}], "negative_code": [], "src_uid": "b18dac401b655c06bee331e71eb3e4de"} {"nl": {"description": "Harry Potter has a difficult homework. Given a rectangular table, consisting of n × m cells. Each cell of the table contains the integer. Harry knows how to use two spells: the first spell change the sign of the integers in the selected row, the second — in the selected column. Harry's task is to make non-negative the sum of the numbers in each row and each column using these spells.Alone, the boy can not cope. Help the young magician!", "input_spec": "The first line contains two integers n and m (1 ≤ n,  m ≤ 100) — the number of rows and the number of columns. Next n lines follow, each contains m integers: j-th integer in the i-th line is ai, j (|ai, j| ≤ 100), the number in the i-th row and j-th column of the table. The rows of the table numbered from 1 to n. The columns of the table numbered from 1 to m.", "output_spec": "In the first line print the number a — the number of required applications of the first spell. Next print a space-separated integers — the row numbers, you want to apply a spell. These row numbers must be distinct! In the second line print the number b — the number of required applications of the second spell. Next print b space-separated integers — the column numbers, you want to apply a spell. These column numbers must be distinct! If there are several solutions are allowed to print any of them.", "sample_inputs": ["4 1\n-1\n-1\n-1\n-1", "2 4\n-1 -1 -1 2\n1 1 1 1"], "sample_outputs": ["4 1 2 3 4 \n0", "1 1 \n1 4"], "notes": null}, "positive_code": [{"source_code": "n,m = gets.split.map(&:to_i)\n\nr_ans = []\nc_ans = []\n\na = []\n\n0.upto(n - 1) do\n a.push(gets.split.map(&:to_i))\nend\n\ndef row_sum(a, i)\n a[i].inject do |x,y|\n x + y\n end\nend\n\ndef flip_row(a, i)\n a[i].map! {|x| -x }\nend\n\ndef flip_col(a, j)\n for x in a\n x[j] = -x[j]\n end\nend\n\ndef col_sum(a, j)\n ans = 0\n for x in a\n ans += x[j]\n end\n\n ans\nend\n\nloop do\n min_sum = 0\n r_dex = -1\n c_dex = -1\n\n 0.upto(n - 1) do |i|\n r = row_sum(a, i)\n if r < min_sum\n min_sum = r\n r_dex = i\n end\n end\n\n 0.upto(m - 1) do |j|\n r = col_sum(a,j)\n if r < min_sum\n min_sum = r\n r_dex = -1\n c_dex = j\n end\n end\n\n break if min_sum == 0\n\n# print \"debug: \", r_dex, \" \", c_dex,\" \", min_sum, \"\\n\"\n\n if r_dex != -1\n flip_row(a, r_dex)\n r_ans.push(r_dex)\n else\n flip_col(a, c_dex)\n c_ans.push(c_dex)\n end\n\nend\n\ndef print_it(ans)\n ht = Array.new(110,0)\n for x in ans\n ht[x] += 1\n end\n ans.clear\n ht.each_index do |i|\n ans.push(i) if ht[i] % 2 == 1\n end\nend\n\nprint_it(r_ans)\nprint_it(c_ans)\n\n\np r_ans.size\nfor x in r_ans\n p x + 1\nend\n\np c_ans.size\nfor x in c_ans\n p x + 1\nend\n\n"}], "negative_code": [{"source_code": "n,m = gets.split.map(&:to_i)\n\nr_ans = []\nc_ans = []\n\na = []\n\n0.upto(n - 1) do\n a.push(gets.split.map(&:to_i))\nend\n\ndef row_sum(a, i)\n a[i].inject do |x,y|\n x + y\n end\nend\n\ndef flip_row(a, i)\n a[i].map! {|x| -x }\nend\n\ndef flip_col(a, j)\n for x in a\n x[j] = -x[j]\n end\nend\n\ndef col_sum(a, j)\n ans = 0\n for x in a\n ans += x[j]\n end\n\n ans\nend\n\nloop do\n min_sum = 0\n r_dex = -1\n c_dex = -1\n\n 0.upto(n - 1) do |i|\n r = row_sum(a, i)\n if r < min_sum\n min_sum = r\n r_dex = i\n end\n end\n\n 0.upto(m - 1) do |j|\n r = col_sum(a,j)\n if r < min_sum\n min_sum = r\n r_dex = -1\n c_dex = j\n end\n end\n\n break if min_sum == 0\n\n# print \"debug: \", r_dex, \" \", c_dex,\" \", min_sum, \"\\n\"\n\n if r_dex != -1\n flip_row(a, r_dex)\n r_ans.push(r_dex)\n else\n flip_col(a, c_dex)\n c_ans.push(c_dex)\n end\n\nend\n\nr_ans.uniq!\nc_ans.uniq!\n\n\ndef print_it(ans)\n ht = Array.new(110,0)\n for x in ans\n ht[x] += 1\n end\n ans.clear\n ht.each_index do |i|\n ans.push(i) if ht[i] % 2 == 1\n end\nend\n\nprint_it(r_ans)\nprint_it(c_ans)\n\n\np r_ans.size\nfor x in r_ans\n p x + 1\nend\n\np c_ans.size\nfor x in c_ans\n p x + 1\nend\n\n"}, {"source_code": "n,m = gets.split.map(&:to_i)\n\nr_ans = []\nc_ans = []\n\na = []\n\n0.upto(n - 1) do\n a.push(gets.split.map(&:to_i))\nend\n\ndef row_sum(a, i)\n a[i].inject do |x,y|\n x + y\n end\nend\n\ndef flip_row(a, i)\n a[i].map! {|x| -x }\nend\n\ndef flip_col(a, j)\n for x in a\n x[j] = -x[j]\n end\nend\n\ndef col_sum(a, j)\n ans = 0\n for x in a\n ans += x[j]\n end\n\n ans\nend\n\nloop do\n min_sum = 0\n r_dex = -1\n c_dex = -1\n\n 0.upto(n - 1) do |i|\n r = row_sum(a, i)\n if r < min_sum\n min_sum = r\n r_dex = i\n end\n end\n\n 0.upto(m - 1) do |j|\n r = col_sum(a,j)\n if r < min_sum\n min_sum = r\n r_dex = -1\n c_dex = j\n end\n end\n\n break if min_sum == 0\n\n# print \"debug: \", r_dex, \" \", c_dex,\" \", min_sum, \"\\n\"\n\n if r_dex != -1\n flip_row(a, r_dex)\n r_ans.push(r_dex)\n else\n flip_col(a, c_dex)\n c_ans.push(c_dex)\n end\n\nend\n\nr_ans.uniq!\nc_ans.uniq!\n\n\ndef print_it(ans)\n ht = Array.new(110,0)\n for x in ans\n ht[x] += 1\n end\n ans = []\n ht.each_index do |i|\n ans.push(i) if ht[i] % 2 == 1\n end\nend\n\nprint_it(r_ans)\nprint_it(c_ans)\n\n\np r_ans.size\nfor x in r_ans\n p x + 1\nend\n\np c_ans.size\nfor x in c_ans\n p x + 1\nend\n\n"}, {"source_code": "n,m = gets.split.map(&:to_i)\n\nr_ans = []\nc_ans = []\n\na = []\n\n0.upto(n - 1) do\n a.push(gets.split.map(&:to_i))\nend\n\ndef row_sum(a, i)\n a[i].inject do |x,y|\n x + y\n end\nend\n\ndef flip_row(a, i)\n a[i].map! {|x| -x }\nend\n\ndef flip_col(a, j)\n for x in a\n x[j] = -x[j]\n end\nend\n\ndef col_sum(a, j)\n ans = 0\n for x in a\n ans += x[j]\n end\n\n ans\nend\n\nloop do\n min_sum = 0\n r_dex = -1\n c_dex = -1\n\n 0.upto(n - 1) do |i|\n r = row_sum(a, i)\n if r < min_sum\n min_sum = r\n r_dex = i\n end\n end\n\n 0.upto(m - 1) do |j|\n r = col_sum(a,j)\n if r < min_sum\n min_sum = r\n r_dex = -1\n c_dex = j\n end\n end\n\n break if min_sum == 0\n\n# p r_dex, c_dex, min_sum\n\n if r_dex != -1\n flip_row(a, r_dex)\n r_ans.push(r_dex)\n else\n flip_col(a, c_dex)\n c_ans.push(c_dex)\n end\n\nend\n\nr_ans.uniq!\nc_ans.uniq!\n\np r_ans.size\n\nfor x in r_ans\n p x + 1\nend\n\np c_ans.size\nfor x in c_ans\n p x + 1\nend\n\n"}], "src_uid": "0756d67a04b30e3924b0c8554ad872ad"} {"nl": {"description": "Let's call left cyclic shift of some string $$$t_1 t_2 t_3 \\dots t_{n - 1} t_n$$$ as string $$$t_2 t_3 \\dots t_{n - 1} t_n t_1$$$.Analogically, let's call right cyclic shift of string $$$t$$$ as string $$$t_n t_1 t_2 t_3 \\dots t_{n - 1}$$$.Let's say string $$$t$$$ is good if its left cyclic shift is equal to its right cyclic shift.You are given string $$$s$$$ which consists of digits 0–9.What is the minimum number of characters you need to erase from $$$s$$$ to make it good?", "input_spec": "The first line contains single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test cases — one per line. The first and only line of each test case contains string $$$s$$$ ($$$2 \\le |s| \\le 2 \\cdot 10^5$$$). Each character $$$s_i$$$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, print the minimum number of characters you need to erase from $$$s$$$ to make it good.", "sample_inputs": ["3\n95831\n100120013\n252525252525"], "sample_outputs": ["3\n5\n0"], "notes": "NoteIn the first test case, you can erase any $$$3$$$ characters, for example, the $$$1$$$-st, the $$$3$$$-rd, and the $$$4$$$-th. You'll get string 51 and it is good.In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good.In the third test case, the given string $$$s$$$ is already good."}, "positive_code": [{"source_code": "t = gets.to_i\nans = []\nt.times do\n s = gets.chomp\n len = s.length\n arr = Array.new(10){Array.new(10,0)}\n prev = Array.new(10){Array.new(10,false)}\n num = Array.new(10,0)\n len.times do |i|\n x = s[i].to_i\n num[x] += 1\n 10.times do |j|\n if prev[j][x]\n prev[j][x] = false\n arr[j][x] += 1\n end\n if !prev[x][j]\n prev[x][j] = true\n end\n end\n end\n 10.times do |i|\n arr[i][i] = 0\n end\n max = 0\n 10.times do |i|\n tmp = arr[i].max\n max = tmp if max < tmp\n end\n max *= 2\n ans << [len-max,len-num.max].min\nend\nt.times do |i|\n puts ans[i]\nend"}], "negative_code": [{"source_code": "t = gets.to_i\nans = []\nt.times do\n s = gets.chomp\n len = s.length\n arr = Array.new(10,0)\n len.times do |i|\n arr[s[i].to_i] += 1\n end\n max = arr.max\n m1 = arr.index(max)\n arr[m1] = 0\n max = arr.max\n m2 = arr.index(max)\n arr[m2] = 0\n m1m1 = 0\n m1m2 = 0\n m1m2_prev = -1\n m2m1 = 0\n m2m1_prev = -1\n max = arr.max\n m3 = arr.index(max)\n m2m3 = 0\n m2m3_prev = -1\n m3m2 = 0\n m3m2_prev = -1\n m1m3 = 0\n m1m3_prev = -1\n m3m1 = 0\n m3m1_prev = -1\n len.times do |i|\n if s[i].to_i == m1\n if m1m2_prev != m1\n m1m2_prev = m1\n else\n m1m2 += 1\n end\n if m2m1_prev == m2\n m2m1_prev = -1\n else\n m2m1 += 1\n end\n\n if m1m3_prev != m1\n m1m3_prev = m1\n else\n m1m3 += 1\n end\n if m3m1_prev == m3\n m3m1_prev = -1\n else\n m3m1 += 1\n end\n\n m2m3 += 1\n m3m2 += 1\n elsif s[i].to_i == m2\n if m1m2_prev == m1\n m1m2_prev = -1\n else\n m1m2 += 1\n end\n if m2m1_prev != m2\n m2m1_prev = m2\n else\n m2m1 += 1\n end\n m1m1 += 1\n m1m3 += 1\n m3m1 += 1\n\n if m3m2_prev == m3\n m3m2_prev = -1\n else\n m3m2 += 1\n end\n if m2m3_prev != m2\n m2m3_prev = m2\n else\n m2m3 += 1\n end\n elsif s[i].to_i == m3\n if m2m3_prev == m2\n m2m3_prev = -1\n else\n m2m3 += 1\n end\n if m3m2_prev != m3\n m3m2_prev = m3\n else\n m3m2 += 1\n end\n if m1m3_prev == m1\n m1m3_prev = -1\n else\n m1m3 += 1\n end\n if m3m1_prev != m3\n m3m1_prev = m3\n else\n m3m1 += 1\n end\n\n m1m1 += 1\n m1m2 += 1\n m2m1 += 1\n else\n m1m1 += 1\n m1m2 += 1\n m2m1 += 1\n m2m3 += 1\n m3m2 += 1\n m1m3 += 1\n m3m1 += 1\n end\n end\n m1m2 += 1 if m1m2_prev != -1\n m2m1 += 1 if m2m1_prev != -1\n m2m3 += 1 if m2m3_prev != -1\n m3m2 += 1 if m3m2_prev != -1\n m1m3 += 1 if m1m3_prev != -1\n m3m1 += 1 if m3m1_prev != -1\n ans << [m1m1,m1m2,m2m1,m2m3,m3m2,m1m3,m3m1].min\nend\nt.times do |i|\n puts ans[i]\nend"}, {"source_code": "t = gets.to_i\nans = []\nt.times do\n s = gets.chomp\n len = s.length\n arr = Array.new(10,0)\n len.times do |i|\n arr[s[i].to_i] += 1\n end\n max = arr.max\n m1 = arr.index(max)\n arr[m1] = 0\n max = arr.max\n m2 = arr.index(max)\n arr[m2] = 0\n m1m1 = 0\n m1m2 = 0\n m1m2_prev = -1\n m2m1 = 0\n m2m1_prev = -1\n m3 = arr.index(max)\n m2m3 = 0\n m2m3_prev = -1\n m3m2 = 0\n m3m2_prev = -1\n len.times do |i|\n if s[i].to_i == m1\n if m1m2_prev != m1\n m1m2_prev = m1\n else\n m1m2 += 1\n end\n if m2m1_prev == m2\n m2m1_prev = -1\n else\n m2m1 += 1\n end\n m2m3 += 1\n m3m2 += 1\n elsif s[i].to_i == m2\n if m1m2_prev == m1\n m1m2_prev = -1\n else\n m1m2 += 1\n end\n if m2m1_prev != m2\n m2m1_prev = m2\n else\n m2m1 += 1\n end\n m1m1 += 1\n\n if m3m2_prev == m3\n m3m2_prev = -1\n else\n m3m2 += 1\n end\n if m2m3_prev != m2\n m2m3_prev = m2\n else\n m2m3 += 1\n end\n elsif s[i].to_i == m3\n if m2m3_prev == m2\n m2m3_prev = -1\n else\n m2m3 += 1\n end\n if m3m2_prev != m3\n m3m2_prev = m3\n else\n m3m2 += 1\n end\n m1m1 += 1\n m1m2 += 1\n m2m1 += 1\n else\n m1m1 += 1\n m1m2 += 1\n m2m1 += 1\n m2m3 += 1\n m3m2 += 1\n end\n end\n m1m2 += 1 if m1m2_prev != -1\n m2m1 += 1 if m2m1_prev != -1\n m2m3 += 1 if m2m3_prev != -1\n m3m2 += 1 if m3m2_prev != -1\n ans << [m1m1,m1m2,m2m1,m2m3,m3m2].min\nend\nt.times do |i|\n puts ans[i]\nend"}, {"source_code": "t = gets.to_i\nans = []\nt.times do\n s = gets.chomp\n len = s.length\n arr = Array.new(10,0)\n len.times do |i|\n arr[s[i].to_i] += 1\n end\n max = arr.max\n m1 = arr.index(max)\n arr[m1] = 0\n max = arr.max\n m2 = arr.index(max)\n arr[m2] = 0\n m1m1 = 0\n m1m2 = 0\n m1m2_prev = -1\n m2m1 = 0\n m2m1_prev = -1\n m3 = arr.index(max)\n m2m3 = 0\n m2m3_prev = -1\n m3m2 = 0\n m3m2_prev = -1\n len.times do |i|\n if s[i].to_i == m1\n if m1m2_prev != m1\n m1m2_prev = m1\n else\n m1m2 += 1\n end\n if m2m1_prev == m2\n m2m1_prev = -1\n else\n m2m1 += 1\n end\n m2m3 += 1\n m3m2 += 1\n elsif s[i].to_i == m2\n if m1m2_prev == m1\n m1m2_prev = -1\n else\n m1m2 += 1\n end\n if m2m1_prev != m2\n m2m1_prev = m2\n else\n m2m1 += 1\n end\n m1m1 += 1\n\n if m3m2_prev == m3\n m3m2_prev = -1\n else\n m3m2 += 1\n end\n if m2m3_prev != m2\n m2m3_prev = m2\n else\n m2m3 += 1\n end\n elsif s[i].to_i == m3\n if m2m3_prev == m2\n m2m3_prev = -1\n else\n m2m3 += 1\n end\n if m3m2_prev != m3\n m3m2_prev = m3\n else\n m3m2 += 1\n end\n m1m1 += 1\n m1m2 += 1\n m2m1 += 1\n else\n m1m1 += 1\n m1m2 += 1\n m2m1 += 1\n m2m3 += 1\n m3m2 += 1\n end\n end\n m1m2 += 1 if m1m2_prev != -1\n m2m1 += 1 if m2m1_prev != -1\n ans << [m1m1,m1m2,m2m1,m2m3,m3m2].min\nend\nt.times do |i|\n puts ans[i]\nend"}, {"source_code": "t = gets.to_i\nans = []\nt.times do\n s = gets.chomp\n len = s.length\n arr = Array.new(10,0)\n len.times do |i|\n arr[s[i].to_i] += 1\n end\n max = arr.max\n m1 = arr.index(max)\n arr[m1] = 0\n max = arr.max\n m2 = arr.index(max)\n m1m1 = 0\n m1m2 = 0\n m1m2_prev = -1\n m2m1 = 0\n m2m1_prev = -1\n len.times do |i|\n if s[i].to_i == m1\n if m1m2_prev != m1\n m1m2_prev = m1\n else\n m1m2 += 1\n end\n if m2m1_prev == m2\n m2m1_prev = -1\n else\n m2m1 += 1\n end\n elsif s[i].to_i == m2\n if m1m2_prev == m1\n m1m2_prev = -1\n else\n m1m2 += 1\n end\n if m2m1_prev != m2\n m2m1_prev = m2\n else\n m2m1 += 1\n end\n m1m1 += 1\n else\n m1m1 += 1\n m1m2 += 1\n m2m1 += 1\n end\n end\n m1m2 += 1 if m1m2_prev != -1\n m2m1 += 1 if m2m1_prev != -1\n ans << [m1m1,m1m2,m2m1].min\nend\nt.times do |i|\n puts ans[i]\nend"}, {"source_code": "t = gets.to_i\nans = []\nt.times do\n s = gets.chomp\n len = s.length\n arr = Array.new(10,0)\n len.times do |i|\n arr[s[i].to_i] += 1\n end\n max = arr.max\n m1 = arr.index(max)\n arr[m1] = 0\n max = arr.max\n m2 = arr.index(max)\n arr[m2] = 0\n m1m1 = 0\n m1m2 = 0\n m1m2_prev = -1\n m2m1 = 0\n m2m1_prev = -1\n m3 = arr.index(max)\n m2m3 = 0\n m2m3_prev = -1\n m3m2 = 0\n m3m2_prev = -1\n m1m3 = 0\n m1m3_prev = -1\n m3m1 = 0\n m3m1_prev = -1\n len.times do |i|\n if s[i].to_i == m1\n if m1m2_prev != m1\n m1m2_prev = m1\n else\n m1m2 += 1\n end\n if m2m1_prev == m2\n m2m1_prev = -1\n else\n m2m1 += 1\n end\n\n if m1m3_prev != m1\n m1m3_prev = m1\n else\n m1m3 += 1\n end\n if m3m1_prev == m3\n m3m1_prev = -1\n else\n m3m1 += 1\n end\n\n m2m3 += 1\n m3m2 += 1\n elsif s[i].to_i == m2\n if m1m2_prev == m1\n m1m2_prev = -1\n else\n m1m2 += 1\n end\n if m2m1_prev != m2\n m2m1_prev = m2\n else\n m2m1 += 1\n end\n m1m1 += 1\n m1m3 += 1\n m3m1 += 1\n\n if m3m2_prev == m3\n m3m2_prev = -1\n else\n m3m2 += 1\n end\n if m2m3_prev != m2\n m2m3_prev = m2\n else\n m2m3 += 1\n end\n elsif s[i].to_i == m3\n if m2m3_prev == m2\n m2m3_prev = -1\n else\n m2m3 += 1\n end\n if m3m2_prev != m3\n m3m2_prev = m3\n else\n m3m2 += 1\n end\n if m1m3_prev == m1\n m1m3_prev = -1\n else\n m1m3 += 1\n end\n if m3m1_prev != m3\n m3m1_prev = m3\n else\n m3m1 += 1\n end\n\n m1m1 += 1\n m1m2 += 1\n m2m1 += 1\n else\n m1m1 += 1\n m1m2 += 1\n m2m1 += 1\n m2m3 += 1\n m3m2 += 1\n m1m3 += 1\n m3m1 += 1\n end\n end\n m1m2 += 1 if m1m2_prev != -1\n m2m1 += 1 if m2m1_prev != -1\n m2m3 += 1 if m2m3_prev != -1\n m3m2 += 1 if m3m2_prev != -1\n m1m3 += 1 if m1m3_prev != -1\n m3m1 += 1 if m3m1_prev != -1\n ans << [m1m1,m1m2,m2m1,m2m3,m3m2,m1m3,m3m1].min\nend\nt.times do |i|\n puts ans[i]\nend"}], "src_uid": "977db81b5c1d3725e384e8f093655172"} {"nl": {"description": "Recently, Masha was presented with a chessboard with a height of $$$n$$$ and a width of $$$m$$$.The rows on the chessboard are numbered from $$$1$$$ to $$$n$$$ from bottom to top. The columns are numbered from $$$1$$$ to $$$m$$$ from left to right. Therefore, each cell can be specified with the coordinates $$$(x,y)$$$, where $$$x$$$ is the column number, and $$$y$$$ is the row number (do not mix up).Let us call a rectangle with coordinates $$$(a,b,c,d)$$$ a rectangle lower left point of which has coordinates $$$(a,b)$$$, and the upper right one — $$$(c,d)$$$.The chessboard is painted black and white as follows: An example of a chessboard. Masha was very happy with the gift and, therefore, invited her friends Maxim and Denis to show off. The guys decided to make her a treat — they bought her a can of white and a can of black paint, so that if the old board deteriorates, it can be repainted. When they came to Masha, something unpleasant happened: first, Maxim went over the threshold and spilled white paint on the rectangle $$$(x_1,y_1,x_2,y_2)$$$. Then after him Denis spilled black paint on the rectangle $$$(x_3,y_3,x_4,y_4)$$$.To spill paint of color $$$color$$$ onto a certain rectangle means that all the cells that belong to the given rectangle become $$$color$$$. The cell dyeing is superimposed on each other (if at first some cell is spilled with white paint and then with black one, then its color will be black).Masha was shocked! She drove away from the guests and decided to find out how spoiled the gift was. For this, she needs to know the number of cells of white and black color. Help her find these numbers!", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^3$$$) — the number of test cases. Each of them is described in the following format: The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n,m \\le 10^9$$$) — the size of the board. The second line contains four integers $$$x_1$$$, $$$y_1$$$, $$$x_2$$$, $$$y_2$$$ ($$$1 \\le x_1 \\le x_2 \\le m, 1 \\le y_1 \\le y_2 \\le n$$$) — the coordinates of the rectangle, the white paint was spilled on. The third line contains four integers $$$x_3$$$, $$$y_3$$$, $$$x_4$$$, $$$y_4$$$ ($$$1 \\le x_3 \\le x_4 \\le m, 1 \\le y_3 \\le y_4 \\le n$$$) — the coordinates of the rectangle, the black paint was spilled on.", "output_spec": "Output $$$t$$$ lines, each of which contains two numbers — the number of white and black cells after spilling paint, respectively.", "sample_inputs": ["5\n2 2\n1 1 2 2\n1 1 2 2\n3 4\n2 2 3 2\n3 1 4 3\n1 5\n1 1 5 1\n3 1 5 1\n4 4\n1 1 4 2\n1 3 4 4\n3 4\n1 2 4 2\n2 1 3 3"], "sample_outputs": ["0 4\n3 9\n2 3\n8 8\n4 8"], "notes": "NoteExplanation for examples:The first picture of each illustration shows how the field looked before the dyes were spilled. The second picture of each illustration shows how the field looked after Maxim spoiled white dye (the rectangle on which the dye was spilled is highlighted with red). The third picture in each illustration shows how the field looked after Denis spoiled black dye (the rectangle on which the dye was spilled is highlighted with red).In the first test, the paint on the field changed as follows: In the second test, the paint on the field changed as follows: In the third test, the paint on the field changed as follows: In the fourth test, the paint on the field changed as follows: In the fifth test, the paint on the field changed as follows: "}, "positive_code": [{"source_code": "def is_white?(x, y)\n (x + y)%2 == 0\nend\n\ndef is_black?(x, y)\n (x + y)%2 != 0\nend\n\ndef white(x1, y1, x2, y2)\n return 0 if x2 < x1 || y2 < y1\n o = 1 + (x2 - x1)/2\n e = (x2 - x1 + 1)/2\n no = 1 + (y2 - y1)/2\n ne = (y2 - y1 + 1)/2\n\n if is_white?(x1, y1)\n no*o + ne*e\n else\n ne*o + no*e\n end\nend\n\ndef black(x1, y1, x2, y2)\n return 0 if x2 < x1 || y2 < y1\n o = 1 + (x2 - x1)/2\n e = (x2 - x1 + 1)/2\n no = 1 + (y2 - y1)/2\n ne = (y2 - y1 + 1)/2\n\n if is_black?(x1, y1)\n no*o + ne*e\n else\n ne*o + no*e\n end \nend\n\ndef intersect(x1, y1, x2, y2, x3, y3, x4, y4)\n f, s = [x1, y1, x2, y2], [x3, y3, x4, y4]\n f, s = s, f if x3 < x1\n return nil if s[1] > f[3] || s[0] > f[2] || s[3] < f[1]\n xi1, yi1 = [f[2], s[0]].min, [f[1], s[1]].max\n xi2, yi2 = [s[2], f[2]].min, [s[3], f[3]].min\n [xi1, yi1, xi2, yi2]\nend\n\nt = gets.to_i\nt.times do\n n, m = gets.split.map{|x| x.to_i}\n x1, y1, x2, y2 = gets.split.map{|x| x.to_i}\n x3, y3, x4, y4 = gets.split.map{|x| x.to_i}\n nwhite = white(1, 1, m, n)\n nblack = black(1, 1, m, n)\n #p [nwhite, nblack]\n nwhite += black(x1, y1, x2, y2)\n nblack -= black(x1, y1, x2, y2)\n #p [nwhite, nblack]\n inter = intersect(x1, y1, x2, y2, x3, y3, x4, y4)\n #p inter\n if inter\n ab = black(inter[0], inter[1], inter[2], inter[3])\n else\n ab = 0\n end\n nwhite -= white(x3, y3, x4, y4) + ab\n nblack += white(x3, y3, x4, y4) + ab\n puts nwhite.to_s + ' ' + nblack.to_s\nend\n"}, {"source_code": "t = gets.to_i\n\n\ndef colorsin(a,b,n,m)\n\n squares = (n-a+1)*(m-b+1)\n if (a+b)%2 == 0 #bottom left square is white (more whites)\n black = squares/2\n white = squares-black\n else\n white = squares/2\n black = squares-white\n end\n\n return [white,black]\nend\n\n\nt.times do\n nm = gets.chomp.split(\" \").map(&:to_i)\n rec1 = gets.chomp.split(\" \").map(&:to_i)\n rec2 = gets.chomp.split(\" \").map(&:to_i)\n\n n = nm[0]\n m = nm[1]\n\n x1 = rec1[0]\n y1 = rec1[1]\n x2 = rec1[2]\n y2 = rec1[3]\n\n x3 = rec2[0]\n y3 = rec2[1]\n x4 = rec2[2]\n y4 = rec2[3]\n\n squares = n*m\n blacks = (squares)/2\n whites = squares-blacks\n\n\n overlap = true\n if (x3>x2 || x4y2 || y4= 0 && a[i - 1] < m\n ans = i + 1\n end\n\n if i + 1 < n && a[i + 1] < m\n ans = i + 1\n end\n end\n\n puts ans\nend\n"}, {"source_code": "t = gets.chomp.to_i\nt.times do\n n = gets.chomp.to_i\n a = gets.chomp.split(\" \").map(&:to_i)\n max = 0\n arr = []\n count = 0\n a.each_with_index do |i,j|\n max = [max,i].max\n if n >= 3\n if i >= max && j >= 1 && j <= n - 2\n if i != a[j-1] || i != a[j+1]\n arr << j\n end\n elsif i >= max && j == 0\n # p i\n # puts \"I\"\n if i != a[j+1]\n # p i\n # p j\n # puts \"Iiiii\"\n arr << j\n end\n elsif i >= max && j == n - 1\n if i != a[j-1]\n arr << j\n end\n #max = [max,i].max\n #arr << j\n #count += 1\n end\n end\n # puts \"OOOOO\"\n # p max\n # puts \"IIIIIIIII\"\n end\n if n == 2\n big = a.max\n a.each_with_index do |i,j|\n if i == big\n arr << j\n end\n end\n end\n a = a.map(&:to_s)\n a = a.uniq\n if a.size == 1\n puts \"-1\"\n else\n ans = arr.pop + 1\n puts ans\n end\nend\n"}, {"source_code": "gets.to_i.times do\n n = gets.to_i\n v = gets.split.map(&:to_i)\n max = v.max\n r = (0...n).find do |i|\n v[i] == max && ((i > 0 && v[i] > v[i-1]) || (i+1 < n && v[i] > v[i+1]))\n end\n puts r.nil? ? -1 : r+1\nend\n"}, {"source_code": "gets.to_i.times do\n n = gets.to_i\n v = gets.split.map(&:to_i)\n max = v.max\n r = (0...n).find { |i| v[i] == max && ((i > 0 && v[i] > v[i - 1]) || (i + 1 < n && v[i] > v[i + 1])) }\n puts r.nil? ? -1 : r + 1\nend\n"}, {"source_code": "t = gets.to_i\nans = []\nt.times do\n n = gets.to_i\n a = gets.chomp.split(\" \").map(&:to_i)\n max = a.max\n index = -1\n n.times do |i|\n if a[i] == max\n if (i-1 >= 0 && a[i-1] != max) || (i+1 < n && a[i+1] != max)\n index = i+1\n end\n end\n end\n ans << index\nend\nputs ans"}, {"source_code": "gets.to_i.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n ma = a.max\n next puts(-1) if a.count(ma) == a.length\n puts (n.times do |i|\n break i + 1 if i > 0 and a[i] == ma and a[i] > a[i - 1]\n break i + 1 if a[i] == ma and a[i] > a[i + 1]\n end\n )\nend\n"}, {"source_code": "gets.to_i.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n ma = a.max\n if a.count(ma) == a.length\n puts(-1)\n else \n puts (n.times do |i|\n break i + 1 if i > 0 and a[i] == ma and a[i] > a[i - 1]\n break i + 1 if a[i] == ma and a[i] > a[i + 1]\n end\n )\n end\nend\n"}, {"source_code": "t=gets.to_i\nt.times do \n n=gets.to_i\n if n==1\n p 1\n next\n end\n an=gets.chomp.split(' ').map(&:to_i)\n maxi = an.max\n flag = false\n catch(:break_loop) do\n n.times do |i|\n if an[i]==maxi && (i!=0 && an[i-1] 1\n q << i\n d[i] = 1\n end\n q.each { |v| a[v].delete(v) }\n while (v = q.shift)\n dv = d[v]\n a[v].each do |u|\n a[u].delete(v)\n next if a[u].size > 1\n next if d[u]\n d[u] = dv + 1\n q << u\n end\n end\n d[0] = 0\n d\nend\n \ngets.to_i.times do\n gets\n n, k = gets.split.map!(&:to_i)\n a = Array.new(n + 1) { Set.new }\n n.pred.times do\n u, v = gets.split.map!(&:to_i)\n a[u] << v\n a[v] << u\n end \n d = f(a)\n puts d.count { |di| di > k }\nend"}, {"source_code": "require 'set'\r\n\r\ndef f(nbrs)\r\n d = Array.new(nbrs.size)\r\n\r\n q = []\r\n nbrs.each_with_index do |nbr, i|\r\n next if nbr.size > 1\r\n q << i\r\n d[i] = 1\r\n end\r\n q.each { |v| nbrs[v].delete(v) }\r\n\r\n while (v = q.shift)\r\n dv = d[v]\r\n nbrs[v].each do |u|\r\n nbrs[u].delete(v)\r\n next if nbrs[u].size > 1\r\n next if d[u]\r\n d[u] = dv + 1\r\n q << u\r\n end\r\n end\r\n\r\n d[0] = 0\r\n d\r\nend\r\n\r\ngets.to_i.times do\r\n\r\ngets # empty line\r\nn, k = gets.split.map!(&:to_i)\r\nnbrs = Array.new(n + 1) { Set.new }\r\nn.pred.times do\r\n u, v = gets.split.map!(&:to_i)\r\n nbrs[u] << v\r\n nbrs[v] << u\r\nend\r\n\r\nd = f(nbrs)\r\nputs d.count { |di| di > k }\r\n\r\nend\r\n"}], "negative_code": [{"source_code": "require 'set'\r\n\r\ndef f(nbrs)\r\n d = Array.new(nbrs.size)\r\n\r\n q = []\r\n nbrs.each_with_index do |nbr, i|\r\n next if nbr.size > 1\r\n q << i\r\n d[i] = 1\r\n end\r\n q.each { |v| nbrs[v].delete(v) }\r\n\r\n while (v = q.shift)\r\n dv = d[v]\r\n nbrs[v].each do |u|\r\n nbrs[u].delete(v)\r\n next if nbrs[u].size > 1\r\n d[u] = dv + 1\r\n q << u\r\n end\r\n end\r\n\r\n d[0] = 0\r\n d\r\nend\r\n\r\ngets.to_i.times do\r\n\r\ngets # empty line\r\nn, k = gets.split.map!(&:to_i)\r\nnbrs = Array.new(n + 1) { Set.new }\r\nn.pred.times do\r\n u, v = gets.split.map!(&:to_i)\r\n nbrs[u] << v\r\n nbrs[v] << u\r\nend\r\n\r\nd = f(nbrs)\r\nputs d.count { |di| di > k }\r\n\r\nend\r\n"}], "src_uid": "07ac198c1086323517540ecd0669eb4c"} {"nl": {"description": "The History of Magic is perhaps the most boring subject in the Hogwarts school of Witchcraft and Wizardry. Harry Potter is usually asleep during history lessons, and his magical quill writes the lectures for him. Professor Binns, the history of magic teacher, lectures in such a boring and monotonous voice, that he has a soporific effect even on the quill. That's why the quill often makes mistakes, especially in dates.So, at the end of the semester Professor Binns decided to collect the students' parchments with notes and check them. Ron Weasley is in a panic: Harry's notes may contain errors, but at least he has some notes, whereas Ron does not have any. Ronald also has been sleeping during the lectures and his quill had been eaten by his rat Scabbers. Hermione Granger refused to give Ron her notes, because, in her opinion, everyone should learn on their own. Therefore, Ron has no choice but to copy Harry's notes.Due to the quill's errors Harry's dates are absolutely confused: the years of goblin rebellions and other important events for the wizarding world do not follow in order, and sometimes even dates from the future occur. Now Ron wants to change some of the digits while he copies the notes so that the dates were in the chronological (i.e. non-decreasing) order and so that the notes did not have any dates strictly later than 2011, or strictly before than 1000. To make the resulting sequence as close as possible to the one dictated by Professor Binns, Ron will change no more than one digit in each date into other digit. Help him do it.", "input_spec": "The first input line contains an integer n (1 ≤ n ≤ 1000). It represents the number of dates in Harry's notes. Next n lines contain the actual dates y1, y2, ..., yn, each line contains a date. Each date is a four-digit integer (1000 ≤ yi ≤ 9999).", "output_spec": "Print n numbers z1, z2, ..., zn (1000 ≤ zi ≤ 2011). They are Ron's resulting dates. Print each number on a single line. Numbers zi must form the non-decreasing sequence. Each number zi should differ from the corresponding date yi in no more than one digit. It is not allowed to change the first digit of a number into 0. If there are several possible solutions, print any of them. If there's no solution, print \"No solution\" (without the quotes).", "sample_inputs": ["3\n1875\n1936\n1721", "4\n9999\n2000\n3000\n3011", "3\n1999\n5055\n2000"], "sample_outputs": ["1835\n1836\n1921", "1999\n2000\n2000\n2011", "No solution"], "notes": null}, "positive_code": [{"source_code": "def min(x, lim)\n (lim..\"2011\").each do |s|\n d = 0\n for i in 0..3\n d += (s[i] == x[i]) ? 0 : 1\n end\n next if d > 1\n\n return s\n end\n\n return false\nend\n\nlim = \"1000\"\no = []\ngets.to_i.times do \n s = gets.chomp\n r = min(s, lim)\n unless r\n puts \"No solution\"\n exit\n end\n o << r\n lim = r\nend\nputs o\n"}, {"source_code": "def min(x, lim)\n (lim..\"2011\").each do |s|\n d = 0\n for i in 0..3\n d += (s[i] == x[i]) ? 0 : 1\n end\n next if d > 1\n\n return s\n end\n\n return false\nend\n\nlim = \"1000\"\no = []\ngets.to_i.times do \n s = gets.chomp\n r = min(s, lim)\n unless r\n puts \"No solution\"\n exit\n end\n o << r\n lim = r\nend\nputs o\n"}, {"source_code": "def min(x, lim)\n (lim..\"2011\").each do |s|\n d = 0\n for i in 0..3\n d += (s[i] == x[i]) ? 0 : 1\n end\n next if d > 1\n\n return s\n end\n\n return false\nend\n\nlim = \"1000\"\no = []\ngets.to_i.times do \n s = gets.chomp\n r = min(s, lim)\n unless r\n puts \"No solution\"\n exit\n end\n o << r\n lim = r\nend\nputs o"}, {"source_code": "def min(x, lim)\n (lim..\"2011\").each do |s|\n d = 0\n for i in 0..3\n d += (s[i] == x[i]) ? 0 : 1\n end\n next if d > 1\n\n return s\n end\n\n return false\nend\n\nlim = \"1000\"\no = []\ngets.to_i.times do \n s = gets.chomp\n r = min(s, lim)\n unless r\n puts \"No solution\"\n exit\n end\n o << r\n lim = r\nend\nputs o\n"}, {"source_code": "def min(x, lim)\n (lim..\"2011\").each do |s|\n d = 0\n for i in 0..3\n d += (s[i] == x[i]) ? 0 : 1\n end\n next if d > 1\n\n return s\n end\n\n return false\nend\n\nlim = \"1000\"\no = []\ngets.to_i.times do \n s = gets.chomp\n r = min(s, lim)\n unless r\n puts \"No solution\"\n exit\n end\n o << r\n lim = r\nend\nputs o\n"}, {"source_code": "def min(x, lim)\n (lim..\"2011\").each do |s|\n d = 0\n for i in 0..3\n d += (s[i] == x[i]) ? 0 : 1\n end\n next if d > 1\n\n return s\n end\n\n return false\nend\n\nlim = \"1000\"\no = []\ngets.to_i.times do \n s = gets.chomp\n r = min(s, lim)\n unless r\n puts \"No solution\"\n exit\n end\n o << r\n lim = r\nend\nputs o"}, {"source_code": "N = gets.to_i\nyears = []\nans = []\n\nN.times {\n tmp = []\n year = gets.chomp\n 0.upto(3) { |i|\n 0.upto(9) {|j|\n changed = year.dup\n changed[i, 1] = j.to_s\n changed = changed.to_i\n if (changed >= 1000 && changed <= 2011)\n tmp.push changed\n end\n }\n }\n tmp.sort!\n years.push tmp\n}\n\nfailed = false\nans.push years[0][0]\n\n1.upto(N - 1) {|i|\n ans[i] = -1\n years[i].each {|e|\n if (e >= ans[i - 1]) \n ans[i] = e\n break\n end\n }\n if (ans[i] == -1)\n puts \"No solution\"\n exit\n end\n}\n\nans.each {|e| puts e}\n"}, {"source_code": "def min(x, lim)\n (lim..\"2011\").each do |s|\n d = 0\n for i in 0..3\n d += (s[i] == x[i]) ? 0 : 1\n end\n next if d > 1\n\n return s\n end\n\n return false\nend\n\nlim = \"1000\"\no = []\ngets.to_i.times do \n s = gets.chomp\n r = min(s, lim)\n unless r\n puts \"No solution\"\n exit\n end\n o << r\n lim = r\nend\nputs o"}, {"source_code": "def min(x, lim)\n (lim..\"2011\").each do |s|\n d = 0\n for i in 0..3\n d += (s[i] == x[i]) ? 0 : 1\n end\n next if d > 1\n\n return s\n end\n\n return false\nend\n\nlim = \"1000\"\no = []\ngets.to_i.times do \n s = gets.chomp\n r = min(s, lim)\n unless r\n puts \"No solution\"\n exit\n end\n o << r\n lim = r\nend\nputs o\n"}, {"source_code": "def find_min(x, lim)\n return false if lim > \"2011\"\n\n (lim..\"2011\").each do |s|\n sum = (s[0] == x[0]) ? 0 : 1\n sum += (s[1] == x[1]) ? 0 : 1\n next if sum > 1\n sum += (s[2] == x[2]) ? 0 : 1\n next if sum > 1\n sum += (s[3] == x[3]) ? 0 : 1\n next if sum > 1\n\n return s\n end\n\n return false\nend\n\nn = gets.to_i\nlim = \"1000\"\no = []\nn.times do \n s = gets.chomp\n r = find_min(s, lim)\n unless r\n puts \"No solution\"\n exit\n end\n o << r\n lim = r\nend\n\nputs o\n"}, {"source_code": "def min(x, lim)\n (lim..\"2011\").each do |s|\n d = 0\n for i in 0..3\n d += (s[i] == x[i]) ? 0 : 1\n end\n next if d > 1\n\n return s\n end\n\n return false\nend\n\nlim = \"1000\"\no = []\ngets.to_i.times do \n s = gets.chomp\n r = min(s, lim)\n unless r\n puts \"No solution\"\n exit\n end\n o << r\n lim = r\nend\nputs o\n"}, {"source_code": "def min(x, lim)\n (lim..\"2011\").each do |s|\n d = 0\n for i in 0..3\n d += (s[i] == x[i]) ? 0 : 1\n end\n next if d > 1\n\n return s\n end\n\n return false\nend\n\nlim = \"1000\"\no = []\ngets.to_i.times do \n s = gets.chomp\n r = min(s, lim)\n unless r\n puts \"No solution\"\n exit\n end\n o << r\n lim = r\nend\nputs o\n"}, {"source_code": "def min(x, lim)\n (lim..\"2011\").each do |s|\n d = 0\n for i in 0..3\n d += (s[i] == x[i]) ? 0 : 1\n end\n next if d > 1\n\n return s\n end\n\n return false\nend\n\nlim = \"1000\"\no = []\ngets.to_i.times do \n s = gets.chomp\n r = min(s, lim)\n unless r\n puts \"No solution\"\n exit\n end\n o << r\n lim = r\nend\nputs o\n"}], "negative_code": [], "src_uid": "c175d010d75c391d0b25391fecff007c"} {"nl": {"description": "Sengoku still remembers the mysterious \"colourful meteoroids\" she discovered with Lala-chan when they were little. In particular, one of the nights impressed her deeply, giving her the illusion that all her fancies would be realized.On that night, Sengoku constructed a permutation p1, p2, ..., pn of integers from 1 to n inclusive, with each integer representing a colour, wishing for the colours to see in the coming meteor outburst. Two incredible outbursts then arrived, each with n meteorids, colours of which being integer sequences a1, a2, ..., an and b1, b2, ..., bn respectively. Meteoroids' colours were also between 1 and n inclusive, and the two sequences were not identical, that is, at least one i (1 ≤ i ≤ n) exists, such that ai ≠ bi holds.Well, she almost had it all — each of the sequences a and b matched exactly n - 1 elements in Sengoku's permutation. In other words, there is exactly one i (1 ≤ i ≤ n) such that ai ≠ pi, and exactly one j (1 ≤ j ≤ n) such that bj ≠ pj.For now, Sengoku is able to recover the actual colour sequences a and b through astronomical records, but her wishes have been long forgotten. You are to reconstruct any possible permutation Sengoku could have had on that night.", "input_spec": "The first line of input contains a positive integer n (2 ≤ n ≤ 1 000) — the length of Sengoku's permutation, being the length of both meteor outbursts at the same time. The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ n) — the sequence of colours in the first meteor outburst. The third line contains n space-separated integers b1, b2, ..., bn (1 ≤ bi ≤ n) — the sequence of colours in the second meteor outburst. At least one i (1 ≤ i ≤ n) exists, such that ai ≠ bi holds.", "output_spec": "Output n space-separated integers p1, p2, ..., pn, denoting a possible permutation Sengoku could have had. If there are more than one possible answer, output any one of them. Input guarantees that such permutation exists.", "sample_inputs": ["5\n1 2 3 4 3\n1 2 5 4 5", "5\n4 4 2 3 1\n5 4 5 3 1", "4\n1 1 3 4\n1 4 3 4"], "sample_outputs": ["1 2 5 4 3", "5 4 2 3 1", "1 2 3 4"], "notes": "NoteIn the first sample, both 1, 2, 5, 4, 3 and 1, 2, 3, 4, 5 are acceptable outputs.In the second sample, 5, 4, 2, 3, 1 is the only permutation to satisfy the constraints."}, "positive_code": [{"source_code": "n = gets.chomp.to_i\n\nax = ay = missing = -1\na = gets.chomp.split.map &:to_i\nfor i in 0...n\n for j in 0...i\n if a[i] == a[j]\n ax = i\n ay = j\n end\n end\nend\nfor i in 1..n\n has = false\n for j in 0...n\n if a[j] == i\n has = true\n end\n end\n if !has\n missing = i\n end\nend\n\nb = gets.chomp.split.map &:to_i\n\nif a[ax] == b[ax] || b[ay] == missing\n a[ay] = missing\nelse \n a[ax] = missing\nend\nputs a.join \" \""}, {"source_code": "n = Integer(gets.chomp)\na = []\na = gets.chomp.split.map &:to_i\nb = []\nb = gets.chomp.split.map &:to_i\n\nx = Array.new(n) { |i| i + 1}\nposition = 0\nposition_other = 0\nfor i in 0...n\n if x.include?(a[i])\n x.delete(a[i])\n else\n position = i\n end\nend\nleft = x[0]\n\n\nfor i in 0...n\n if a[i] == a[position]\n position_other = i\n break\n end\nend\noutput1 = []\noutput1.replace(a)\noutput1[position_other] = left \noutput2 = []\noutput2.replace(a)\noutput2[position] = left\ncount = 0\nfor i in 0...n\n if b[i] != output1[i]\n count += 1\n end\nend\nif count == 1\n print output1.join(\" \")\nelse\n print output2.join(\" \")\nend"}, {"source_code": "def ok(a, b, p)\n n = p.length\n x = 0\n y = 0\n for i in 0...n\n x += 1 if a[i] == p[i]\n y += 1 if b[i] == p[i]\n end\n return x == n - 1 && y == n - 1\nend\n\ndef solve(a, b)\n x = -1\n y = -1\n n = a.length\n\n mrk = Hash.new(0)\n\n p = []\n for i in 0...n\n if a[i] != b[i]\n if x == -1\n x = i\n p.push(-1)\n else\n y = i\n p.push(-1)\n end\n else\n mrk[a[i]] = 1\n p.push(a[i])\n end\n end\n\n if y == -1\n for i in 1..n\n if mrk[i] == 0\n p[x] = i\n end\n end\n\n return p\n end\n\n lst = []\n for i in 1..n\n if mrk[i] == 0\n lst.push(i)\n end\n end\n\n p[x] = lst[0]\n p[y] = lst[1]\n\n return p if ok(a, b, p)\n\n p[x] = lst[1]\n p[y] = lst[0]\n \n return p\nend\n\nn = gets.split.map(&:to_i)\n\nline = gets.chomp\na = line.split.map(&:to_i)\n\nline = gets.chomp\nb = line.split.map(&:to_i)\n\np = solve(a, b)\np.each { |x| print x.to_s + \" \" }\nputs\n"}], "negative_code": [{"source_code": "n = gets.chomp.to_i\n\nax = ay = missing = -1\na = gets.chomp.split.map &:to_i\nfor i in 0...n\n for j in 0...i\n if a[i] == a[j]\n ax = i\n ay = j\n end\n end\nend\nfor i in 1..n\n has = false\n for j in 0...n\n if a[j] == i\n has = true\n end\n end\n if !has\n missing = i\n end\nend\n\nb = gets.chomp.split.map &:to_i\n\nif a[ax] == b[ax]\n a[ay] = missing\nelse \n a[ax] = missing\nend\nputs a.join \" \""}, {"source_code": "n = gets.chomp.to_i\n\nax = ay = missing = -1\na = gets.chomp.split.map &:to_i\nfor i in 0...n\n for j in 0...i\n if a[i] == a[j]\n ax = i\n ay = j\n end\n end\nend\nfor i in 1..n\n has = false\n for j in 0...n\n if a[j] == i\n has = true\n end\n end\n if !has\n missing = i\n end\nend\n\nbx = by = -1\nb = gets.chomp.split.map &:to_i\nfor i in 0...n\n for j in 0...i\n if b[i] == b[j]\n bx = i\n by = j\n end\n end\nend\n\nif ax == bx || ax == by\n a[ax] = missing\nelsif\n a[ay] = missing\nend\n\nputs a.join \" \""}, {"source_code": "n = Integer(gets.chomp)\na = []\na = gets.chomp.split.map &:to_i\nb = []\nb = gets.chomp.split.map &:to_i\n\nx = Array.new(n) { |i| i + 1}\nposition = 0\nposition_other = 0\nfor i in 0...n\n if x.include?(a[i])\n x.delete(a[i])\n else\n position = i\n end\nend\nleft = x[0]\n\n\nfor i in 0...n\n if a[i] == a[position]\n position_other = i\n break\n end\nend\noutput1 = []\noutput1.replace(a)\noutput1[position_other] = left \noutput2 = []\noutput2.replace(a)\noutput2[position] = left\ncount = 0\nfor i in 0...n\n if b[i] != output1[i]\n count += 1\n end\nend\nif count == 1\n print output1\nelse\n print output2\nend"}, {"source_code": "def solve(a, b)\n x = -1\n y = -1\n n = a.length\n\n mrk = Hash.new(0)\n\n p = []\n i = 0\n \n while i < n\n if a[i] != b[i]\n if x == -1\n x = i\n p.push(-1)\n else\n y = i\n p.push(-1)\n end\n else\n mrk[a[i]] = 1\n p.push(a[i])\n end\n i += 1\n end\n\n if y == -1\n for i in 1..n\n if mrk[i] == 0\n p[x] = i\n end\n end\n\n return p\n end\n\n for i in 1..n\n if mrk[i] == 0\n if (a[x] == i || b[x] == i) && x != -1\n p[x] = i\n x = -1\n else\n p[y] = i\n end\n end\n end\n \n return p\nend\n\nn = gets.split.map(&:to_i)\n\nline = gets.chomp\na = line.split.map(&:to_i)\n\nline = gets.chomp\nb = line.split.map(&:to_i)\n\np = solve(a, b)\np.each { |x| print x.to_s + \" \" }\nputs\n"}], "src_uid": "6fc3da19da8d9ab024cdd5acfc4f4164"} {"nl": {"description": "Circular land is an $$$2n \\times 2n$$$ grid. Rows of this grid are numbered by integers from $$$1$$$ to $$$2n$$$ from top to bottom and columns of this grid are numbered by integers from $$$1$$$ to $$$2n$$$ from left to right. The cell $$$(x, y)$$$ is the cell on the intersection of row $$$x$$$ and column $$$y$$$ for $$$1 \\leq x \\leq 2n$$$ and $$$1 \\leq y \\leq 2n$$$.There are $$$n^2$$$ of your friends in the top left corner of the grid. That is, in each cell $$$(x, y)$$$ with $$$1 \\leq x, y \\leq n$$$ there is exactly one friend. Some of the other cells are covered with snow.Your friends want to get to the bottom right corner of the grid. For this in each cell $$$(x, y)$$$ with $$$n+1 \\leq x, y \\leq 2n$$$ there should be exactly one friend. It doesn't matter in what cell each of friends will be.You have decided to help your friends to get to the bottom right corner of the grid.For this, you can give instructions of the following types: You select a row $$$x$$$. All friends in this row should move to the next cell in this row. That is, friend from the cell $$$(x, y)$$$ with $$$1 \\leq y < 2n$$$ will move to the cell $$$(x, y + 1)$$$ and friend from the cell $$$(x, 2n)$$$ will move to the cell $$$(x, 1)$$$. You select a row $$$x$$$. All friends in this row should move to the previous cell in this row. That is, friend from the cell $$$(x, y)$$$ with $$$1 < y \\leq 2n$$$ will move to the cell $$$(x, y - 1)$$$ and friend from the cell $$$(x, 1)$$$ will move to the cell $$$(x, 2n)$$$. You select a column $$$y$$$. All friends in this column should move to the next cell in this column. That is, friend from the cell $$$(x, y)$$$ with $$$1 \\leq x < 2n$$$ will move to the cell $$$(x + 1, y)$$$ and friend from the cell $$$(2n, y)$$$ will move to the cell $$$(1, y)$$$. You select a column $$$y$$$. All friends in this column should move to the previous cell in this column. That is, friend from the cell $$$(x, y)$$$ with $$$1 < x \\leq 2n$$$ will move to the cell $$$(x - 1, y)$$$ and friend from the cell $$$(1, y)$$$ will move to the cell $$$(2n, y)$$$. Note how friends on the grid border behave in these instructions. Example of applying the third operation to the second column. Here, colorful circles denote your friends and blue cells are covered with snow. You can give such instructions any number of times. You can give instructions of different types. If after any instruction one of your friends is in the cell covered with snow he becomes ill.In order to save your friends you can remove snow from some cells before giving the first instruction: You can select the cell $$$(x, y)$$$ that is covered with snow now and remove snow from this cell for $$$c_{x, y}$$$ coins. You can do this operation any number of times.You want to spend the minimal number of coins and give some instructions to your friends. After this, all your friends should be in the bottom right corner of the grid and none of them should be ill.Please, find how many coins you will spend.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 100$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \\leq n \\leq 250$$$). Each of the next $$$2n$$$ lines contains $$$2n$$$ integers $$$c_{i, 1}, c_{i, 2}, \\ldots, c_{i, 2n}$$$ ($$$0 \\leq c_{i, j} \\leq 10^9$$$) — costs of removing snow from cells. If $$$c_{i, j} = 0$$$ for some $$$i, j$$$ than there is no snow in cell $$$(i, j)$$$. Otherwise, cell $$$(i, j)$$$ is covered with snow. It is guaranteed that $$$c_{i, j} = 0$$$ for $$$1 \\leq i, j \\leq n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$250$$$.", "output_spec": "For each test case output one integer — the minimal number of coins you should spend.", "sample_inputs": ["4\n\n1\n\n0 8\n\n1 99\n\n2\n\n0 0 0 0\n\n0 0 0 0\n\n9 9 2 2\n\n9 9 9 9\n\n2\n\n0 0 4 2\n\n0 0 2 4\n\n4 2 4 2\n\n2 4 2 4\n\n4\n\n0 0 0 0 0 0 0 2\n\n0 0 0 0 0 0 2 0\n\n0 0 0 0 0 2 0 0\n\n0 0 0 0 2 0 0 0\n\n0 0 0 2 2 0 2 2\n\n0 0 2 0 1 6 2 1\n\n0 2 0 0 2 4 7 4\n\n2 0 0 0 2 0 1 6"], "sample_outputs": ["100\n22\n14\n42"], "notes": "NoteIn the first test case you can remove snow from the cells $$$(2, 1)$$$ and $$$(2, 2)$$$ for $$$100$$$ coins. Then you can give instructions All friends in the first collum should move to the previous cell. After this, your friend will be in the cell $$$(2, 1)$$$. All friends in the second row should move to the next cell. After this, your friend will be in the cell $$$(2, 2)$$$. In the second test case you can remove all snow from the columns $$$3$$$ and $$$4$$$ for $$$22$$$ coins. Then you can give instructions All friends in the first row should move to the next cell. All friends in the first row should move to the next cell. All friends in the second row should move to the next cell. All friends in the second row should move to the next cell. All friends in the third column should move to the next cell. All friends in the third column should move to the next cell. All friends in the fourth column should move to the next cell. All friends in the fourth column should move to the next cell. It can be shown that none of the friends will become ill and that it is impossible to spend less coins."}, "positive_code": [{"source_code": "gets.to_i.times do\n n = gets.to_i\n nn = 2 * n\n c = Array.new(nn) { gets.split.map(&:to_i) }\n\n ans = 0\n (n...nn).each { |i|\n (n...nn).each { |j|\n ans += c[i][j]\n c[i][j] = 0\n }\n }\n\n var = [\n [ \"Left-up\", c[0][-1] ],\n [ \"Left-down\", c[n - 1][-1] ],\n [ \"Right-up\", c[0][n] ],\n [ \"Right-down\", c[n - 1][n] ],\n [ \"Up-left\", c[-1][0] ],\n [ \"Up-right\", c[-1][n - 1] ],\n [ \"Down-left\", c[n][0] ],\n [ \"Down-right\", c[n][n - 1] ],\n ].min_by { _1[1] }\n ans += var[1]\n\n p ans\nend\n"}], "negative_code": [{"source_code": "gets.to_i.times do\n n = gets.to_i\n nn = 2 * n\n c = Array.new(nn) { gets.split.map(&:to_i) }\n\n ans = 0\n (n...nn).each { |i|\n (n...nn).each { |j|\n ans += c[i][j]\n c[i][j] = 0\n }\n }\n\n ans += [\n c[0][-1],\n c[0][ n],\n c[-1][0],\n c[-1][n - 1]\n ].min\n\n p ans\nend\n"}, {"source_code": "gets.to_i.times do\n n = gets.to_i\n nn = 2 * n\n c = Array.new(nn) { gets.split.map(&:to_i) }\n\n ans = 0\n (n...nn).each { |i|\n (n...nn).each { |j|\n ans += c[i][j]\n }\n }\n\n ans += [\n c[ 0][-1],\n c[ 0][ n],\n c[-1][ 0],\n c[-1][ n]\n ].min\n\n p ans\nend\n"}], "src_uid": "acec4108e865c3f894de14248156abfd"} {"nl": {"description": "You are given a string $$$s$$$, consisting of $$$n$$$ lowercase Latin letters.A substring of string $$$s$$$ is a continuous segment of letters from $$$s$$$. For example, \"defor\" is a substring of \"codeforces\" and \"fors\" is not. The length of the substring is the number of letters in it.Let's call some string of length $$$n$$$ diverse if and only if there is no letter to appear strictly more than $$$\\frac n 2$$$ times. For example, strings \"abc\" and \"iltlml\" are diverse and strings \"aab\" and \"zz\" are not.Your task is to find any diverse substring of string $$$s$$$ or report that there is none. Note that it is not required to maximize or minimize the length of the resulting substring.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$1 \\le n \\le 1000$$$) — the length of string $$$s$$$. The second line is the string $$$s$$$, consisting of exactly $$$n$$$ lowercase Latin letters.", "output_spec": "Print \"NO\" if there is no diverse substring in the string $$$s$$$. Otherwise the first line should contain \"YES\". The second line should contain any diverse substring of string $$$s$$$.", "sample_inputs": ["10\ncodeforces", "5\naaaaa"], "sample_outputs": ["YES\ncode", "NO"], "notes": "NoteThe first example has lots of correct answers. Please, restrain yourself from asking if some specific answer is correct for some specific test or not, these questions always lead to \"No comments\" answer."}, "positive_code": [{"source_code": "gets;s=gets.chomp.chars.uniq\nputs s.size<2 ? :NO : \"YES\\n%s%s\"%s[0,2]"}, {"source_code": "# http://codeforces.com/contest/1073/problem/A\n\nn = gets.chomp.to_i\ns = gets.chomp\n\nA_ORD = 'a'.ord\n\ndef solve(n, s)\n results = []\n\n s.each_char.each_with_index do |char, i|\n new_result = Array.new(26, 0)\n new_ord = char.ord - A_ORD\n new_result[new_ord] = 1\n\n results.each_with_index do |(result, total, max), j|\n result[new_ord] += 1\n if result[new_ord] > max\n max = result[new_ord]\n end\n\n return [i, j] if max <= (total + 1) / 2\n\n results[j] = [result, total + 1, max]\n end\n\n results << [new_result, 1, 1]\n end\n\n nil\nend\n\nresult = solve(n, s)\n\nif result.nil?\n puts 'NO'\nelse\n puts 'YES'\n i, j = result\n puts s[j..i]\nend\n"}, {"source_code": "len = gets.strip.to_i\nstr = gets.strip\n\ndiff = false\n\nlast_char = str[0]\nskip = true\nout = nil\nstr.each_char do |c|\n if skip\n skip = false\n next\n end\n if c != last_char\n out = last_char + c\n break\n end\nend\n\nif out.nil?\n puts \"NO\"\nelse\n puts \"YES\"\n puts out\nend\n"}], "negative_code": [{"source_code": "s=gets.chomp.chars.uniq\nputs s.size<2 ? :NO : \"YES\\n%s %s\"%[s,2]"}, {"source_code": "gets;s=gets.chomp.chars.uniq\nputs s.size<2 ? :NO : \"YES\\n%s %s\"%s[0,2]"}, {"source_code": "s=gets.chomp.chars.uniq\nputs s.size<2 ? :NO : \"YES\\n%s %s\"%s[0,2]"}, {"source_code": "# http://codeforces.com/contest/1073/problem/A\n\nn = gets.chomp.to_i\ns = gets.chomp\n\ndef solve(n, s)\n last_result = {}\n results = [last_result]\n\n s.each_char do |char|\n new_result = last_result.dup\n new_result[char] ||= 0\n new_result[char] += 1\n last_result = new_result\n results << new_result\n end\n\n length = results.length\n\n length.times do |i|\n (i - 1).times do |j|\n differences = results[i].inject({}) do |result, (char, count)|\n j_count = results[j][char] || 0\n result.merge(char => count - j_count)\n end\n\n total_count = differences.inject(0) { |sum, (_, count)| sum + count }\n\n differences.each do |char, count|\n if count <= total_count / 2\n return [i-1, j]\n end\n end\n end\n end\n\n nil\nend\n\nresult = solve(n, s)\n\nif result.nil?\n puts 'NO'\nelse\n puts 'YES'\n i, j = result\n puts s[j..i]\nend\n"}], "src_uid": "ce4443581d4ee12db6607695cd567070"} {"nl": {"description": "ZS the Coder is playing a game. There is a number displayed on the screen and there are two buttons, ' + ' (plus) and '' (square root). Initially, the number 2 is displayed on the screen. There are n + 1 levels in the game and ZS the Coder start at the level 1.When ZS the Coder is at level k, he can : Press the ' + ' button. This increases the number on the screen by exactly k. So, if the number on the screen was x, it becomes x + k. Press the '' button. Let the number on the screen be x. After pressing this button, the number becomes . After that, ZS the Coder levels up, so his current level becomes k + 1. This button can only be pressed when x is a perfect square, i.e. x = m2 for some positive integer m. Additionally, after each move, if ZS the Coder is at level k, and the number on the screen is m, then m must be a multiple of k. Note that this condition is only checked after performing the press. For example, if ZS the Coder is at level 4 and current number is 100, he presses the '' button and the number turns into 10. Note that at this moment, 10 is not divisible by 4, but this press is still valid, because after it, ZS the Coder is at level 5, and 10 is divisible by 5.ZS the Coder needs your help in beating the game — he wants to reach level n + 1. In other words, he needs to press the '' button n times. Help him determine the number of times he should press the ' + ' button before pressing the '' button at each level. Please note that ZS the Coder wants to find just any sequence of presses allowing him to reach level n + 1, but not necessarily a sequence minimizing the number of presses.", "input_spec": "The first and only line of the input contains a single integer n (1 ≤ n ≤ 100 000), denoting that ZS the Coder wants to reach level n + 1.", "output_spec": "Print n non-negative integers, one per line. i-th of them should be equal to the number of times that ZS the Coder needs to press the ' + ' button before pressing the '' button at level i. Each number in the output should not exceed 1018. However, the number on the screen can be greater than 1018. It is guaranteed that at least one solution exists. If there are multiple solutions, print any of them.", "sample_inputs": ["3", "2", "4"], "sample_outputs": ["14\n16\n46", "999999999999999998\n44500000000", "2\n17\n46\n97"], "notes": "NoteIn the first sample case:On the first level, ZS the Coder pressed the ' + ' button 14 times (and the number on screen is initially 2), so the number became 2 + 14·1 = 16. Then, ZS the Coder pressed the '' button, and the number became . After that, on the second level, ZS pressed the ' + ' button 16 times, so the number becomes 4 + 16·2 = 36. Then, ZS pressed the '' button, levelling up and changing the number into .After that, on the third level, ZS pressed the ' + ' button 46 times, so the number becomes 6 + 46·3 = 144. Then, ZS pressed the '' button, levelling up and changing the number into . Note that 12 is indeed divisible by 4, so ZS the Coder can reach level 4.Also, note that pressing the ' + ' button 10 times on the third level before levelling up does not work, because the number becomes 6 + 10·3 = 36, and when the '' button is pressed, the number becomes and ZS the Coder is at Level 4. However, 6 is not divisible by 4 now, so this is not a valid solution.In the second sample case:On the first level, ZS the Coder pressed the ' + ' button 999999999999999998 times (and the number on screen is initially 2), so the number became 2 + 999999999999999998·1 = 1018. Then, ZS the Coder pressed the '' button, and the number became . After that, on the second level, ZS pressed the ' + ' button 44500000000 times, so the number becomes 109 + 44500000000·2 = 9·1010. Then, ZS pressed the '' button, levelling up and changing the number into . Note that 300000 is a multiple of 3, so ZS the Coder can reach level 3."}, "positive_code": [{"source_code": "n = gets.to_i\n\nk=2\nfor i in 2..n+1\n puts (i**2 * (i-1)**2 - k) / (i-1)\n k=i*(i-1)\nend"}, {"source_code": "$vis = Array.new(100005,0)\ndef cal\n\tpre = 2\n sim = 2\n for i in 1...100002\n pre /= i\n n = i\n while true\n cur = i * n * n + 2 * n * n + (n * n / i)\n if cur >= pre\n $vis[i] = cur - pre\n pre = (i+1) * n\n break\n end\n end\n sim = (sim + ($vis[i] * i))\n temp = Math.sqrt(sim).to_i\n if (temp * temp != sim || temp % (i+1) != 0)\n puts temp\n end\n sim = temp\n\tend\nend\n\ncal()\n\nN = gets.to_i\nfor i in 1..N\n\tputs $vis[i]\nend\n\n"}, {"source_code": "n = gets.to_i\n\nputs 2\n\n(2..n).each do |k|\n puts k*(k+1)*(k+1) - (k-1)\nend\n"}, {"source_code": "n=gets.to_i\nk=2\nfor i in 2..n+1\n puts (i*i*(i-1)*(i-1)-k)/(i-1)\n k=i*(i-1)\nend\n"}, {"source_code": "n = gets.chomp.to_i\n\nans = [2]\nfor k in 2..n\n x = (k - 1) * k\n y = k * (k + 1)\n ans << (y**2 - x) / k\nend\nputs ans.*\"\\n\"\n\n"}, {"source_code": "n = gets.to_i\nsr = 2\nx = 2\nans = [2]\n2.upto(n) do |m|\n sr = sr + 2 * m\n ans << (sr * sr - x) / m\n x = sr\nend\nans.each{|x| puts x}\n"}, {"source_code": "level = gets.chomp.to_i\n\n(1..level).each do |l|\n puts 2 if l == 1\n puts l * (l + 1) * (l + 1) - (l - 1) if l != 1\nend"}, {"source_code": "def gcd(a, b)\n return a if b == 0\n return gcd(b, a % b)\nend\n\ndef lcm(a, b)\n return a * b / gcd(a, b)\nend\n\nn = gets.to_i\n\ncur = 2\n1.upto(n) do |ni|\n nxt = lcm(ni, ni + 1)\n k = (nxt * nxt - cur) / ni\n puts k\n cur = nxt\nend\n"}, {"source_code": "n=gets.to_i\nk=2\nfor i in 2..n+1\n puts (i*i*(i-1)*(i-1)-k)/(i-1)\n k=i*(i-1)\nend"}, {"source_code": "n=gets.to_i()\nm=2\n(1..n).each{|k|\n\tb=k*k+k\n\tputs (b*b-m)/k\n\tm=b\n}"}], "negative_code": [{"source_code": "n = gets.chomp.to_i\n\nx = 2\nfor k in 1..n\n x = k * (k + 1)\n y = k * (k + 1) * (k + 2)\n puts (y**2 - x) / k\nend\n\n"}, {"source_code": "n = gets.to_i\nsr = 2\nx = 2\nans = []\n2.upto(n) do |m|\n sr = sr + 2 * m\n ans << (sr * sr - x) / m\n x = sr\nend\nans.each{|x| puts x}\n"}, {"source_code": "level = gets.chomp.to_i\nlevel += 1\n\ntarget = level ** 2\ncurrent = 1\nnumber = 2\n\nuntil current == level\nhit = target - number * current\n\n\nnumber = target ** 0.5\n\n\ncurrent += 1\nuntil number % current == 0\n\tcurrent += 1\nend\n\nputs hit\n#puts \"Number = #{number}\"\n#puts \"Hit = #{hit}\"\n#puts \"Curr = #{current}\"\nend"}, {"source_code": "level = gets.chomp.to_i\nlevel += 1\n\ntarget = level ** 2\ncurrent = 1\nnumber = 2\n\nuntil current == level\nhit = target - number * current\n\n\nnumber = target ** 0.5\n\n\ncurrent += 1\nuntil number % current == 0\n\tcurrent += 1\nend\n\nputs hit.to_i\n#puts \"Number = #{number}\"\n#puts \"Hit = #{hit}\"\n#puts \"Curr = #{current}\"\nend"}, {"source_code": "level = gets.chomp.to_i\nlevel += 1\n\ntarget = (level ** 2) * 4\ncurrent = 1\nnumber = 2\n\nuntil current == level\nhit = (target - number) / current\n\n\nnumber = target ** 0.5\n\n\ncurrent += 1\nuntil number % current == 0\n\tcurrent += 1\nend\n\nputs hit.to_i\n#puts \"Number = #{number}\"\n#puts \"Hit = #{hit}\"\n#puts \"Curr = #{current}\"\nend"}, {"source_code": "level = gets.chomp.to_i\nlevel += 1\n\ntarget = level ** 2\ncurrent = 1\nnumber = 2\n\nuntil current == level\nhit = (target - number) / current\n\n\nnumber = target ** 0.5\n\n\ncurrent += 1\nuntil number % current == 0\n\tcurrent += 1\nend\n\nputs hit.to_i\n#puts \"Number = #{number}\"\n#puts \"Hit = #{hit}\"\n#puts \"Curr = #{current}\"\nend"}], "src_uid": "6264405c66b2690ada9f8cc6cff55f0b"} {"nl": {"description": "There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is hi meters, distinct planks can have distinct heights. Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1] Polycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible.Write the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic).", "input_spec": "The first line of the input contains integers n and k (1 ≤ n ≤ 1.5·105, 1 ≤ k ≤ n) — the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers h1, h2, ..., hn (1 ≤ hi ≤ 100), where hi is the height of the i-th plank of the fence.", "output_spec": "Print such integer j that the sum of the heights of planks j, j + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them.", "sample_inputs": ["7 3\n1 2 6 1 1 7 1"], "sample_outputs": ["3"], "notes": "NoteIn the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8."}, "positive_code": [{"source_code": "INF = 1.5 * 10 * 10 * 10 * 10 * 10 * 1000\n\ntmp = gets.chomp.split(\" \")\nn, k = tmp[0].to_i, tmp[1].to_i\nfence = gets.chomp.split(\" \").map!{|v| v.to_i}\nfor i in 1...fence.size do\n fence[i] += fence[i - 1]\nend\nfence = [0] + fence\nans, max = 0, INF\nfor i in 0...(fence.size - k) do\n if (max > fence[i + k] - fence[i])\n ans = i + 1\n max = fence[i + k] - fence[i]\n end\nend\nputs ans\n"}, {"source_code": "n,w=gets.split.map{|e| e.to_i}\na=gets.split.map{|e| e.to_i}\n\ns=0\nans=1\nw.times{|i|\n\ts+=a[i]\n}\nt=s\n1.upto(n-w){|i|\n\ts=s+a[i+w-1]-a[i-1]\n\tif t>s then\n\t\tt=s\n\t\tans=i+1\n\tend\n}\nputs ans\n\n"}, {"source_code": "n,k = gets.chomp.split(\" \").map{|i| i.to_i}\nh = gets.chomp.split(\" \").map{|i| i.to_i}\n\nmin = h[0...k].inject(0) {|sum, i| sum += i}\nmin_index = 0\nlast_sum = min\n(k...n).each do |i|\n last_sum = last_sum + h[i] - h[i-k]\n if min > last_sum\n min_index = i - k + 1\n min = last_sum\n end\nend\n\nputs min_index + 1\n"}, {"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\n\nn, k = gets.split.map(&:to_i)\nhs = gets.split.map(&:to_i)\nhsum = hs[0...k].reduce(:+)\nminimum_hsum = hsum\nminimum_idx = 1 #zero-based\n(0...(n-k)).each { |idx|\n hsum -= hs[idx]\n hsum += hs[idx + k]\n\n if hsum < minimum_hsum\n minimum_hsum = hsum\n minimum_idx = idx + 1 + 1 # one-based, exact index\n end\n}\n\nputs minimum_idx\n"}, {"source_code": "def solution(fence, take)\n cost = Array.new(fence.size, Float::INFINITY)\n cost[0] = fence[0...take].reduce(:+)\n \n for i in 1...fence.size-take+1 do\n cost[i] = cost[i-1] - fence[i-1] + fence[i + take - 1]\n end\n\n remove = 0\n min_cost = Float::INFINITY\n cost.each_with_index do |c, index|\n if min_cost > c\n min_cost = c\n remove = index \n end\n end\n remove + 1\nend\n\n\ntables, take = gets.split.map(&:to_i)\nfence = gets.split.map(&:to_i)\n\nputs solution(fence, take)\n"}, {"source_code": "def solution(fence, take)\n prefix_sum = fence.inject([0]) { |v, i| v << v.last + i }\n\n min_cost = Float::INFINITY\n for i in 0...prefix_sum.size-take do\n if min_cost > prefix_sum[i+take] - prefix_sum[i]\n min_cost = prefix_sum[i+take] - prefix_sum[i]\n index = i + 1\n end\n end\n index\nend\n\ntables, take = gets.split.map(&:to_i)\nfence = gets.split.map(&:to_i)\n\nputs solution(fence, take)\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\nb = []\nc = 0\nn.times do |i|\n c += a[i]\n if i >= k - 1\n b << c \n c -= a[i - k + 1]\n end\nend\nputs b.index(b.min) + 1\n"}, {"source_code": "s = gets.split.map(&:to_i)\nn = s[0]\nk = s[1]\na = gets.split.map(&:to_i)\n\nsum = Array.new(n + 1, 0)\nminimum = 10000000000\nbest = 0\n\n1.upto(n) do |i|\n sum[i] = sum[i - 1] + a[i - 1]\n if i >= k\n sumk = sum[i] - sum[i - k]\n if sumk < minimum\n minimum = sumk\n best = i - k + 1\n end\n end\nend\n\nputs best\n"}, {"source_code": "n, k = gets.split.map{|x| x.to_i}\nh = gets.split.map{|x| x.to_i}\na = [h[0..k-1].inject(0){|s, x| s + x}]\n(k..n-1).each{|i| a << a.last + h[i] - h[i - k]}\nputs(a.index(a.min) + 1)\n"}, {"source_code": "n, k = gets.split(' ').map(&:to_i)\na = gets.split(' ').map(&:to_i)\na.unshift(0)\n\nsum = 0\nans = -1\nmin = 1_000_000_000\na.each_with_index do |e, i|\n sum += e\n sum -= a[i-k] if i>k\n\n if i >= k && min > sum\n min = sum\n ans = i - k + 1\n end\nend\n\nputs ans\n"}, {"source_code": "line = gets.chomp.split(\" \").map {|e| e.to_i}\nn = line[0]\nk = line[1]\n\nhs = gets.chomp.split(\" \").map {|e| e.to_i}\n\nif k == 1\n min = -1\n minInd = -1\n for i in 0...(hs.length)\n if min == -1 || hs[i] < min\n min = hs[i]\n minInd = i + 1\n end\n end\n puts minInd\nelse\n ps = [0]\n\n acc = 0\n for i in 0...(hs.length)\n acc += hs[i]\n ps.push acc\n end\n\n min = -1\n minInd = -1\n for i in k..(hs.length)\n t = ps[i] - ps[i - k]\n if min == -1 || t < min\n min = t\n minInd = i - k\n end\n end\n\n puts (minInd + 1)\nend\n"}], "negative_code": [{"source_code": "n,k = gets.chomp.split(\" \").map{|i| i.to_i}\nh = gets.chomp.split(\" \").map{|i| i.to_i}\n\nmin = h[0...k].inject(0) {|sum, i| sum += i}\nlast_sum = min\n(k...n).each do |i|\n last_sum = last_sum + h[i] - h[i-k]\n min = last_sum if min > last_sum\nend\n\nputs min\n"}, {"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\n\nn, k = gets.split.map(&:to_i)\nhs = gets.split.map(&:to_i)\nhsum = hs[0...k].reduce(:+)\nminimum_hsum = hsum\nminimum_idx = 0 #zero-based\n(0...(n-k)).each { |idx|\n hsum -= hs[idx]\n hsum += hs[idx + k]\n\n if hsum < minimum_hsum\n minimum_hsum = hsum\n minimum_idx = idx\n end\n}\n\n# p [minimum_idx, minimum_hsum]\nputs minimum_idx+1+1 #1-based index, another one for obtaining exact index\n"}, {"source_code": "def solution(fence, take)\n prefix_sum = fence.inject([0]) { |v, i| v << v.last + i }\n\n min_cost = Float::INFINITY\n index = 1\n \n for i in 1...fence.size-take do\n if min_cost > prefix_sum[i+take-1] - prefix_sum[i-1]\n min_cost = prefix_sum[i+take-1] - prefix_sum[i-1]\n index = i\n end\n end\n index\nend\n\ntables, take = gets.split.map(&:to_i)\nfence = gets.split.map(&:to_i)\n\nputs solution(fence, take)"}, {"source_code": "def solution(fence, take)\n\n prefix_sum = fence.inject([0]) { |v, i| v << v.last + i }\n\n min_cost = Float::INFINITY\n index = -1\n \n for i in 1...fence.size-take do\n if min_cost > prefix_sum[i+take-1] - prefix_sum[i-1]\n min_cost = prefix_sum[i+take-1] - prefix_sum[i-1]\n index = i\n end\n end\n index\nend\n\ntables, take = gets.split.map(&:to_i)\nfence = gets.split.map(&:to_i)\n\nputs solution(fence, take)\n"}, {"source_code": "n, k = gets.split(' ').map(&:to_i)\na = gets.split(' ').map(&:to_i)\na.unshift(0)\n\nsum = 0\nans = -1\nmin = 1_000_000\na.each_with_index do |e, i|\n sum += e\n sum -= a[i-k] if i>k\n\n if i >= k && min > sum\n min = sum\n ans = i - k + 1\n end\nend\n\nputs ans\n"}, {"source_code": "line = gets.chomp.split(\" \").map {|e| e.to_i}\nn = line[0]\nk = line[1]\n\nhs = gets.chomp.split(\" \").map {|e| e.to_i}\n\nif k == 1\n min = -1\n minInd = -1\n for i in 0...(hs.length)\n if min == -1 || hs[i] < min\n min = hs[i]\n minInd = i + 1\n end\n end\n puts minInd\nelse\n ps = []\n\n acc = 0\n for i in 0...(hs.length)\n acc += hs[i]\n ps.push acc\n end\n\n min = -1\n minInd = -1\n for i in (k - 1)...(hs.length)\n t = ps[i] - ps[i - k + 1]\n if min == -1 || t < min\n min = t\n minInd = i - k + 1\n end\n end\n\n puts (minInd + 1)\n\nend\n"}, {"source_code": "line = gets.chomp.split(\" \").map {|e| e.to_i}\nn = line[0]\nk = line[1]\n\nhs = gets.chomp.split(\" \").map {|e| e.to_i}\n\nif k == 1\n min = -1\n minInd = -1\n for i in 0...(hs.length)\n if min == -1 || hs[i] < min\n min = hs[i]\n minInd = i + 1\n end\n end\n puts minInd\nelse\n ps = [0]\n\n acc = 0\n for i in 0...(hs.length)\n acc += hs[i]\n ps.push acc\n end\n\n min = -1\n minInd = -1\n for i in k...(hs.length)\n t = ps[i] - ps[i - k]\n if min == -1 || t < min\n min = t\n minInd = i - k\n end\n end\n\n puts (minInd + 1)\nend\n"}, {"source_code": "line = gets.chomp.split(\" \").map {|e| e.to_i}\nn = line[0]\nk = line[1]\n\nhs = gets.chomp.split(\" \").map {|e| e.to_i}\nps = []\n\nacc = 0\nfor i in 0...(hs.length)\n acc += hs[i]\n ps.push acc\nend\n\nmin = -1\nminInd = -1\nfor i in (k - 1)...(hs.length)\n t = ps[i] - ps[i - k + 1]\n if min == -1 || t < min\n min = t\n minInd = i - k + 1\n end\nend\n\nputs (minInd + 1)\n\n"}], "src_uid": "69f4e340b3f6e1d807e0545ebea1fe2f"} {"nl": {"description": "Johnny has recently found an ancient, broken computer. The machine has only one register, which allows one to put in there one variable. Then in one operation, you can shift its bits left or right by at most three positions. The right shift is forbidden if it cuts off some ones. So, in fact, in one operation, you can multiply or divide your number by $$$2$$$, $$$4$$$ or $$$8$$$, and division is only allowed if the number is divisible by the chosen divisor. Formally, if the register contains a positive integer $$$x$$$, in one operation it can be replaced by one of the following: $$$x \\cdot 2$$$ $$$x \\cdot 4$$$ $$$x \\cdot 8$$$ $$$x / 2$$$, if $$$x$$$ is divisible by $$$2$$$ $$$x / 4$$$, if $$$x$$$ is divisible by $$$4$$$ $$$x / 8$$$, if $$$x$$$ is divisible by $$$8$$$ For example, if $$$x = 6$$$, in one operation it can be replaced by $$$12$$$, $$$24$$$, $$$48$$$ or $$$3$$$. Value $$$6$$$ isn't divisible by $$$4$$$ or $$$8$$$, so there're only four variants of replacement.Now Johnny wonders how many operations he needs to perform if he puts $$$a$$$ in the register and wants to get $$$b$$$ at the end.", "input_spec": "The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \\leq t \\leq 1000$$$) — the number of test cases. The following $$$t$$$ lines contain a description of test cases. The first and only line in each test case contains integers $$$a$$$ and $$$b$$$ ($$$1 \\leq a, b \\leq 10^{18}$$$) — the initial and target value of the variable, respectively.", "output_spec": "Output $$$t$$$ lines, each line should contain one integer denoting the minimum number of operations Johnny needs to perform. If Johnny cannot get $$$b$$$ at the end, then write $$$-1$$$.", "sample_inputs": ["10\n10 5\n11 44\n17 21\n1 1\n96 3\n2 128\n1001 1100611139403776\n1000000000000000000 1000000000000000000\n7 1\n10 8"], "sample_outputs": ["1\n1\n-1\n0\n2\n2\n14\n0\n-1\n-1"], "notes": "NoteIn the first test case, Johnny can reach $$$5$$$ from $$$10$$$ by using the shift to the right by one (i.e. divide by $$$2$$$).In the second test case, Johnny can reach $$$44$$$ from $$$11$$$ by using the shift to the left by two (i.e. multiply by $$$4$$$).In the third test case, it is impossible for Johnny to reach $$$21$$$ from $$$17$$$.In the fourth test case, initial and target values are equal, so Johnny has to do $$$0$$$ operations.In the fifth test case, Johnny can reach $$$3$$$ from $$$96$$$ by using two shifts to the right: one by $$$2$$$, and another by $$$3$$$ (i.e. divide by $$$4$$$ and by $$$8$$$)."}, "positive_code": [{"source_code": "def solve(num)\n start = num[0].to_i.to_s(2)\n target = num[1].to_i.to_s(2)\n start_index = start.index(\"1\")\n target_index = target.index(\"1\")\n start = start[start_index..-1]\n target = target[target_index..-1]\n start_rindex = start.rindex(\"1\")\n target_rindex = target.rindex(\"1\")\n return -1 if start_rindex != target_rindex\n i = 0\n while i <= start_rindex\n return -1 if start[i] != target[i]\n i += 1\n end\n longer = [start.length, target.length].max\n shorter = [start.length, target.length].min\n diff = longer - shorter \n (diff / 3.0).ceil\nend\n\namount = gets.chomp.to_i\namount.times do\n input = gets.chomp.split(\" \")\n puts solve(input)\nend\n"}, {"source_code": "def solve(num)\n start = num[0].to_i.to_s(2)\n target = num[1].to_i.to_s(2)\n i = 0\n while start[i] == 0\n start.shift\n end\n i = 0\n while target[i] == 0\n target.shift\n end\n start_index = start.rindex(\"1\")\n target_index = target.rindex(\"1\")\n return -1 if start_index != target_index\n i = 0\n while i <= start_index\n foo = start[i]\n bar = target[i]\n return -1 if foo != bar \n i += 1\n end\n \n foo = [start.length, target.length].max\n baz = [start.length, target.length].min\n t = foo - baz\n (t / 3.0).ceil\n\nend\n\namount = gets.chomp.to_i\namount.times do\n input = gets.chomp.split(\" \")\n puts solve(input)\nend\n"}, {"source_code": "t = 0\nt = gets.chomp.to_i\n\nwhile t>0\n a,b = gets.chomp.split(' ').map{|a| a.to_i}\n #puts \"Value of a is\" ,a\n #puts \"Value of b is \",b\n i = 0\n min = 0\n value = 0\n if a < b\n while(a to_value\n\t\tif from_value % to_value > 0\n\t\t\tputs -1\n\t\t\tnext\n\t\tend\n\t\tif (from_value / to_value) % 2 > 0\n\t\t\tputs -1\n\t\t\tnext\n\t\tend\n\tend\n\tif to_value > from_value\n\t\tif to_value % from_value > 0\n\t\t\tputs -1\n\t\t\tnext\n\t\tend\n\t\tif (to_value / from_value) % 2 > 0\n\t\t\tputs -1\n\t\t\tnext\n\t\tend\n\tend\n\tresult = nil\n\titer = 0\n\tif from_value > to_value\n\t\ttmp = to_value\n\t\tto_value = from_value\n\t\tfrom_value = tmp\n\tend\n\t# p 'from_value = ' + from_value.to_s\n\t# p 'to_value = ' + to_value.to_s\n\n\twhile from_value != to_value\n\t\t# p 'from_value = ' + from_value.to_s\n\t\titer += 1\n\t\t\n\t\tif to_value >= from_value * 8\n\t\t\t# p 'yes'\n\t\t\tfrom_value = from_value * 8\n\t\telsif to_value >= from_value * 4\n\t\t\t# p 'yes2'\n\t\t\tfrom_value = from_value * 4\n\t\telsif to_value >= from_value * 2\n\t\t\t# p 'yes3'\n\t\t\tfrom_value = from_value * 2\n\t\telse\n\t\t\tfrom_value = to_value\n\t\t\titer = -1\n\t\tend\n\tend\n\tputs iter\nend\n\n"}], "negative_code": [], "src_uid": "541039ef3c9b8251b758608811533e06"} {"nl": {"description": "Autocomplete is a program function that enables inputting the text (in editors, command line shells, browsers etc.) completing the text by its inputted part. Vasya is busy working on a new browser called 'BERowser'. He happens to be working on the autocomplete function in the address line at this very moment. A list consisting of n last visited by the user pages and the inputted part s are known. Your task is to complete s to make it an address of one of the pages from the list. You have to find the lexicographically smallest address having a prefix s.", "input_spec": "The first line contains the s line which is the inputted part. The second line contains an integer n (1 ≤ n ≤ 100) which is the number of visited pages. Then follow n lines which are the visited pages, one on each line. All the lines have lengths of from 1 to 100 symbols inclusively and consist of lowercase Latin letters only.", "output_spec": "If s is not the beginning of any of n addresses of the visited pages, print s. Otherwise, print the lexicographically minimal address of one of the visited pages starting from s. The lexicographical order is the order of words in a dictionary. The lexicographical comparison of lines is realized by the '<' operator in the modern programming languages.", "sample_inputs": ["next\n2\nnextpermutation\nnextelement", "find\n4\nfind\nfindfirstof\nfindit\nfand", "find\n4\nfondfind\nfondfirstof\nfondit\nfand"], "sample_outputs": ["nextelement", "find", "find"], "notes": null}, "positive_code": [{"source_code": "s = gets.chomp!\nn = gets.to_i\nvisited = $stdin.readlines.map(&:chomp!).sort!\nt = visited.bsearch{|x| x >= s}\nif t.nil? or (/#{s}/ =~ t) != 0\n puts s\nelse\n puts t\nend\n"}, {"source_code": "a=gets.chop;puts$<.select{|s|s=~/^#{a}/}.min||a\n"}, {"source_code": "word = gets.strip\nn = gets.to_i\n\na = []\no = []\n\nfor i in 0..(n-1)\n a << gets.strip \nend\n\na.each {|x| o << x if x.start_with?(word) }\n\no.length == 0 ? (puts word) : (puts o.min)"}, {"source_code": "a=gets.chop;puts$<.select{|s|s=~/^#{a}/}.min||a\n"}, {"source_code": "str = STDIN.gets.chomp\nn = STDIN.gets.to_i\nlines = []\nn.times { lines << STDIN.gets.chomp }\n\nlines.sort.each do |line|\n if line.rindex(str) == 0\n puts line\n exit\n end\nend\nputs str"}, {"source_code": "a=gets.chop;puts$<.select{|s|s=~/^#{a}/}.min||a"}, {"source_code": "a=gets.chop;puts$<.select{|s|s=~/^#{a}/}.min||a"}, {"source_code": "s, n = gets.chomp, gets.to_i\nlines = []\nn.times { lines << gets.chomp }\n\nlines.sort.each do |line|\n if line.start_with?(s)\n puts line; exit\n end\nend\nputs s"}, {"source_code": "a=gets.chop;puts$<.select{|s|s=~/^#{a}/}.min||a\n"}, {"source_code": "a=gets.chop;puts$<.select{|s|s=~/^#{a}/}.min||a\n"}, {"source_code": "=begin 59A - Word\nstr = gets.chomp\nlower = 0\nupper = 0\nstr.each_char { |w| /[[:lower:]]/.match(w) ? lower+= 1 : upper+= 1 }\nputs upper > lower ? str.upcase : str.downcase\n=end\nstr = gets.chomp\nn = gets.to_i\nout = nil\nwhile n > 0\n\ttmp = gets.chomp\n if /\\A#{str}[[:lower:]]*/.match(tmp)\n if out.nil? || tmp < out\n out = tmp\n end\n end\n n-=1\nend\nputs out.nil? ? str : out"}, {"source_code": "a=gets.chop;puts$<.select{|s|s=~/^#{a}/}.min||a"}, {"source_code": "a=gets.chop;puts$<.select{|s|s=~/^#{a}/}.min||a\n"}, {"source_code": "s=gets.strip\nn=gets.to_i\nm=\"\"\nn.times{|i| w=gets.strip; m=(m lower ? str.upcase : str.downcase\n=end\nstr = gets.chomp\nn = gets.to_i\nmax = 101\nout = str\nwhile n > 0\n\ttmp = gets.chomp\n if /\\A#{str}[[:lower:]]*/.match(tmp)\n if tmp.size < max\n max = tmp.size\n out = tmp\n end\n end\n n-=1\nend\nputs out"}, {"source_code": "=begin 59A - Word\nstr = gets.chomp\nlower = 0\nupper = 0\nstr.each_char { |w| /[[:lower:]]/.match(w) ? lower+= 1 : upper+= 1 }\nputs upper > lower ? str.upcase : str.downcase\n=end\nstr = gets.chomp\nn = gets.to_i\nmax = 101\nout = str\nwhile n > 0\n\ttmp = gets.chomp\n if /\\A#{str}[[:lower:]]*/.match(tmp)\n if tmp.size < max\n max = tmp.size\n out = tmp\n elsif tmp.size == max\n out = tmp < out ? tmp : out\n end\n end\n n-=1\nend\nputs out"}, {"source_code": "s=gets.strip\nn=gets.to_i # n can bo omitted in calculations\nok=false\nm=\"\"\nn.times{|i| w=gets.strip; m,ok=((m= 0 && (k ** 0.5).to_i ** 2 == k do\n k = a.pop\nend\nputs k\n"}, {"source_code": "#!usr/local/bin/ruby -w\n\ndef isPerfectSqr?(a)\n\tk = Math.sqrt(a).ceil\n\tif(k*k == a) then return true\n\telse\n\t\treturn false \n\tend\nend\n\n\nn = gets.chomp.to_i\n\narr = gets.split.map{|i| i.to_i}\n\nans = -1e7;\n\narr.each do |a|\n\tif(a<0 || !isPerfectSqr?(a) ) then ans = [ans,a].max end\nend\n\nputs ans"}, {"source_code": "gets\np gets.split.map{|e|e[0]!=?- && (e.to_i**0.5).to_i**2==e.to_i ? -10**9 : e.to_i}.max\n"}, {"source_code": "n = gets.to_i\nnum = gets.chomp.split(' ').map{|n| n.to_i }.sort\naddress = n -1\nwhile (address > 0) do\n if num[address] == -1\n break\n end\n if ((Math.sqrt(num[address].abs) - Math.sqrt(num[address].abs).to_i) != 0)\n break\n end\n address = address - 1\nend\n\nputs num[address]\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i).sort.reverse\n\na.each{|t|\n y = Math.sqrt(t.abs).to_i\n if y*y!=t\n puts t\n exit\n end\n}\n"}, {"source_code": "gets\np gets.split.map(&:to_i).select {|x| x < 0 || Math.sqrt(x).modulo(1) != 0}.max\n"}, {"source_code": "gets;p gets.split.map(&:to_i).select{|x|x<0||Math.sqrt(x).floor**2!=x}.sort[-1]"}, {"source_code": "n=gets.to_i;p gets.split.map(&:to_i).select{|x|x<0||Math.sqrt(x).floor**2!=x}.sort[-1]"}, {"source_code": "a = gets.to_i\narray = gets.chomp.split(\" \")\narray = array.map { |n| n.to_i }\nx = array.select { |n| n>=0 and Math.sqrt(n)%1!=0 }.max\ny = array.select { |n| n<0 }.max\nans = []\nif x !=nil\n ans << x\nend\n\nif y !=nil\n ans << y\nend\n\nif ans.length !=0\n puts ans.max\nend"}], "negative_code": [{"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\na.sort!\nk = a.pop\nwhile k > 0 && (k ** 0.5).to_i ** 2 == k do\n k = a.pop\nend\nputs k\n"}, {"source_code": "n = gets.to_i\nnum = gets.chomp.split(' ').map{|n| n.to_i }.sort\naddress = n -1\nwhile (address > 0) do\n if num[address].abs == 1\n break\n end\n if ((Math.sqrt(num[address].abs) - Math.sqrt(num[address].abs).to_i) != 0)\n break\n end\n address = address - 1\nend\n\nputs num[address]"}, {"source_code": "n = gets.to_i\nnum = gets.chomp.split(' ').map{|n| n.to_i }.sort\naddress = n -1\nwhile (address > 0) do\n if ((Math.sqrt(num[address].abs) - Math.sqrt(num[address].abs).to_i) != 0)\n break\n end\n address = address - 1\nend\nputs num[address]\n"}, {"source_code": "n = gets.to_i\nnum = gets.chomp.split(' ').map{|n| n.to_i }\naddress = n -1\nwhile (address > 0) do\n if ((Math.sqrt(num[address].abs) - Math.sqrt(num[address].abs).to_i) != 0)\n break\n end\n address = address - 1\nend\nputs num[address]\n"}, {"source_code": "gets\np gets.split.map(&:to_i).select {|x| x >= 0 && Math.sqrt(x).modulo(1) != 0}.max\n"}, {"source_code": "n=gets.to_i;p gets.split.map(&:to_i).reject{|x|x<0 or Math.sqrt(x).floor**2==x}.sort[-1]\n\n"}, {"source_code": "a = gets.to_i\narray = gets.chomp.split(\" \")\narray = array.map { |n| n.to_i }\nx = array.select { |n| n>=0 and (Math.sqrt(n)%1!=0 or n==1) }.max\ny = array.select { |n| n<0 }.max\nans = []\nif x !=nil\n ans << x\nend\n\nif y !=nil\n ans << y\nend\n\nif ans.length !=0\n puts ans.max\nend"}, {"source_code": "a = gets.to_i\narray = gets.chomp.split(\" \")\narray = array.map { |n| n.to_i }\nx = array.select { |n| n>=0 and (Math.sqrt(n)%1!=0 or n==1) }.max\ny = array.select { |n| n<0 and (Math.sqrt(n*-1)%1!=0 or n==-1) }.max\nans = []\nif x !=nil\n ans << x\nend\n\nif y !=nil\n ans << y\nend\n\nif ans.length !=0\n puts ans.max\nend"}, {"source_code": "a = gets.to_i\narray = gets.chomp.split(\" \")\narray = array.map { |n| n.to_i }\nx = array.select { |n| n>=0 and Math.sqrt(n)%1!=0 }.max\ny = array.select { |n| n<0 and Math.sqrt(n*-1)%1!=0 }.max\nans = []\nif x !=nil\n ans << x\nend\n\nif y !=nil\n ans << y\nend\n\nif ans.length !=0\n puts ans.max\nend"}], "src_uid": "d46d5f130d8c443f28b52096c384fef3"} {"nl": {"description": "In this problem we consider a special type of an auction, which is called the second-price auction. As in regular auction n bidders place a bid which is price a bidder ready to pay. The auction is closed, that is, each bidder secretly informs the organizer of the auction price he is willing to pay. After that, the auction winner is the participant who offered the highest price. However, he pay not the price he offers, but the highest price among the offers of other participants (hence the name: the second-price auction).Write a program that reads prices offered by bidders and finds the winner and the price he will pay. Consider that all of the offered prices are different.", "input_spec": "The first line of the input contains n (2 ≤ n ≤ 1000) — number of bidders. The second line contains n distinct integer numbers p1, p2, ... pn, separated by single spaces (1 ≤ pi ≤ 10000), where pi stands for the price offered by the i-th bidder.", "output_spec": "The single output line should contain two integers: index of the winner and the price he will pay. Indices are 1-based.", "sample_inputs": ["2\n5 7", "3\n10 2 8", "6\n3 8 2 9 4 14"], "sample_outputs": ["2 5", "1 8", "6 9"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/ruby\ngets\na=gets.split.map(&:to_i).each_with_index.to_a.sort.reverse\nputs \"#{a[0].last+1} #{a[1].first}\""}, {"source_code": "n = gets.to_i\ndx = gets.split().map{|i| i.to_i }\ni = dx.index(dx.max) + 1\ndx.sort!.reverse!\nputs i.to_s + \" \" + dx[1].to_s\n"}, {"source_code": "# https://codeforces.com/problemset/problem/386/A\n\ngets\nbids = gets.split(' ').map(&:to_i)\n\nhighest_bid = 0\nhighest_bid_index = 0\nsecond_highest_bid = 0\n\nbids.each_with_index do |bid, index|\n if bid > highest_bid\n second_highest_bid = highest_bid\n highest_bid = bid\n highest_bid_index = index\n elsif bid > second_highest_bid\n second_highest_bid = bid\n end\nend\n\nputs \"#{highest_bid_index+1} #{second_highest_bid}\"\n"}, {"source_code": "n=gets.to_i\nmas=gets.split.map &:to_i\ni=mas.index(mas.max)+1\nmas[mas.index(mas.max)]=-1\nputs \"#{i} #{mas.max}\""}, {"source_code": "n = gets.to_i\nv = gets.split.map(&:to_i)\nans = v.map.with_index.to_a.sort{ |a, b| b[0] <=> a[0] }\nputs \"#{ans[0][1] + 1} #{ans[1][0]}\"\n"}], "negative_code": [], "src_uid": "1d4aaf15e5c6fcde50515880aae74720"} {"nl": {"description": "Igor the analyst has adopted n little bunnies. As we all know, bunnies love carrots. Thus, Igor has bought a carrot to be shared between his bunnies. Igor wants to treat all the bunnies equally, and thus he wants to cut the carrot into n pieces of equal area. Formally, the carrot can be viewed as an isosceles triangle with base length equal to 1 and height equal to h. Igor wants to make n - 1 cuts parallel to the base to cut the carrot into n pieces. He wants to make sure that all n pieces have the same area. Can you help Igor determine where to cut the carrot so that each piece have equal area? Illustration to the first example. ", "input_spec": "The first and only line of input contains two space-separated integers, n and h (2 ≤ n ≤ 1000, 1 ≤ h ≤ 105).", "output_spec": "The output should contain n - 1 real numbers x1, x2, ..., xn - 1. The number xi denotes that the i-th cut must be made xi units away from the apex of the carrot. In addition, 0 < x1 < x2 < ... < xn - 1 < h must hold. Your output will be considered correct if absolute or relative error of every number in your output doesn't exceed 10 - 6. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if .", "sample_inputs": ["3 2", "2 100000"], "sample_outputs": ["1.154700538379 1.632993161855", "70710.678118654752"], "notes": "NoteDefinition of isosceles triangle: https://en.wikipedia.org/wiki/Isosceles_triangle."}, "positive_code": [{"source_code": "n,h = gets.split.map(&:to_f)\nfor i in 1..n-1\np h*((i/n)**0.5)\nend\n "}, {"source_code": "n, h = gets.chomp.split(\" \").map(&:to_f)\nb = 1\n\nhh = h / Math.sqrt(n)\n\n(n.to_i - 1).times do |i| \n print(hh * Math.sqrt(i+1), \" \")\nend\nputs "}, {"source_code": "n, h = gets.chomp.split.map(&:to_i)\na = []\n(1...n).each { |i|\n a << Math.sqrt(i.to_f / n) * h\n}\nputs a.map(&:to_s).join(' ')\n"}], "negative_code": [], "src_uid": "6f8a1a138ea2620f2013f426e29e4d98"} {"nl": {"description": "Erelong Leha was bored by calculating of the greatest common divisor of two factorials. Therefore he decided to solve some crosswords. It's well known that it is a very interesting occupation though it can be very difficult from time to time. In the course of solving one of the crosswords, Leha had to solve a simple task. You are able to do it too, aren't you?Leha has two strings s and t. The hacker wants to change the string s at such way, that it can be found in t as a substring. All the changes should be the following: Leha chooses one position in the string s and replaces the symbol in this position with the question mark \"?\". The hacker is sure that the question mark in comparison can play the role of an arbitrary symbol. For example, if he gets string s=\"ab?b\" as a result, it will appear in t=\"aabrbb\" as a substring.Guaranteed that the length of the string s doesn't exceed the length of the string t. Help the hacker to replace in s as few symbols as possible so that the result of the replacements can be found in t as a substring. The symbol \"?\" should be considered equal to any other symbol.", "input_spec": "The first line contains two integers n and m (1 ≤ n ≤ m ≤ 1000) — the length of the string s and the length of the string t correspondingly. The second line contains n lowercase English letters — string s. The third line contains m lowercase English letters — string t.", "output_spec": "In the first line print single integer k — the minimal number of symbols that need to be replaced. In the second line print k distinct integers denoting the positions of symbols in the string s which need to be replaced. Print the positions in any order. If there are several solutions print any of them. The numbering of the positions begins from one.", "sample_inputs": ["3 5\nabc\nxaybz", "4 10\nabcd\nebceabazcd"], "sample_outputs": ["2\n2 3", "1\n2"], "notes": null}, "positive_code": [{"source_code": "def go a, b\n res = []\n for i in 0..(a.length)\n if a[i] != b[i]\n res.push i\n end\n end\n\n return res\nend\n\nn, m = gets.strip.split.map &:to_i\na = gets.strip\nb = gets.strip\n\nres = *(1..n)\nfor i in 0..(m-n)\n curr = go(a, b[i..i+n-1]).map{ |x| x + 1 }\n if curr.length < res.length\n res = curr\n end\nend\n\nprint res.length.to_s + \"\\n\"\nres.each{ |x| print x.to_s + \" \" }\nprint \"\\n\"\n"}, {"source_code": "sub_str_length, str_length = gets.split.map(&:to_i)\n\nsub_str = gets.chomp\nstr = gets.chomp \nmin_replacements = 1000;\nmin_replace = *(1..sub_str_length)\n\ndef string_diff(str1, str2)\n return [] if (str1 == str2)\n (0..str1.length).map { |i| i + 1 if str1[i] != str2[i] }.compact\nend\n\nif str == sub_str\n puts 0\nelsif sub_str == ''\n puts str.length\nelse\n (0..(str_length - sub_str_length)).each do |i|\n replacements = string_diff(str[i, sub_str_length], sub_str)\n min_replace == replacements if i == 0\n if replacements.empty?\n min_replacements = 0\n min_replace = []\n break\n elsif replacements.length < min_replacements\n min_replacements = replacements.length\n min_replace = replacements\n end\n end\n puts min_replacements\n puts min_replace.join(' ')\nend\n\n\n"}, {"source_code": "inp = gets.split(' ')\n\na = gets.chomp\nb = gets.chomp\n\ndef match(a,b)\n\tmin_errors_size = 10**9\n\tmin_errors_array = Array.new\n\tfor i in (0..b.size-a.size)\n\t\terrors = []\n\t\tfor j in (0..a.size-1)\n\t\t\tif a[j]!=b[i+j]\n\t\t\t\t\terrors << j+1\n\t\t\tend\n\t\tend\n\t\tif errors.size < min_errors_size\n\t\t\tmin_errors_array = errors\n\t\t\tmin_errors_size = errors.size\n\t\tend\n\tend\n\treturn min_errors_array\nend\n\nans = match(a,b)\n\nputs ans.size\n\nputs ans.join(' ')"}, {"source_code": "str = gets.split(\" \")\nn = str[0].to_i; m = str[1].to_i\ns = gets.chomp\nt = gets.chomp\n\nres = []\nans = 1e5\n\nfor i in (0...(m-n+1))\n cnt = 0\n temp = []\n k = i\n for j in (0...n)\n if t[k] != s[j]\n cnt+=1\n temp.push(j+1)\n end\n k+=1\n end\n if cnt < ans\n ans = cnt\n res = temp\n end\nend\n\nprint ans\nprint \"\\n\"\n\nres.each do |val|\n print \"#{val} \"\nend"}], "negative_code": [{"source_code": "sub_str_length, str_length = gets.split.map(&:to_i)\n\nsub_str = gets.chomp\nstr = gets.chomp\nmin_replacements = 1000;\nmin_replace = []\n\ndef string_diff(str1, str2)\n return [] if(str1 == str2)\n (0..str1.length).map { |i| i + 1 if str1[i] != str2[i] }.compact\nend\n\nif str == sub_str\n puts 0\nelse\n (0..(str_length - sub_str_length)).each do |i|\n replacements = string_diff(str[i, sub_str_length + i - 1], sub_str)\n if replacements.empty?\n min_replacements = 0\n min_replace = []\n break\n elsif replacements.length < min_replacements\n min_replacements = replacements.length\n min_replace = replacements\n end\n end\n puts min_replacements\n puts min_replace.join(' ')\nend"}, {"source_code": "sub_str_length, str_length = gets.split.map(&:to_i)\n\nsub_str = gets.chomp\nstr = gets.chomp\nmin_replacements = 1000;\nmin_replace = []\n\ndef string_diff(str1, str2)\n return [] if(str1 == str2)\n (0..str1.length).map { |i| i + 1 if str1[i] != str2[i] }.compact\nend\n\nif str == sub_str\n puts 0\nelse\n (0..(str_length - sub_str_length)).each do |i|\n replacements = string_diff(str[i, sub_str_length], sub_str)\n if replacements.empty?\n min_replacements = 0\n min_replace = []\n break\n elsif replacements.length < min_replacements\n min_replacements = replacements.length\n min_replace = replacements\n end\n end\n puts min_replacements\n puts min_replace.join(' ')\nend"}, {"source_code": "sub_str_length, str_length = gets.split.map(&:to_i)\n\nsub_str = gets.chomp\nstr = gets.chomp\nmin_replacements = 1000;\nmin_replace = []\n\ndef string_diff(str1, str2)\n return [] if(str1 == str2)\n (0..str1.length).map { |i| i + 1 if str1[i] != str2[i] }.compact\nend\n\nif str == sub_str\n puts 0\nelse\n (0..(str_length - sub_str_length)).each do |i|\n replacements = string_diff(str[i, sub_str_length + i], sub_str)\n if replacements.empty?\n min_replacements = 0\n min_replace = []\n break\n elsif replacements.length < min_replacements\n min_replacements = replacements.length\n min_replace = replacements\n end\n end\n puts min_replacements\n puts min_replace.join(' ')\nend"}, {"source_code": "sub_str_length, str_length = gets.split.map(&:to_i)\n\nsub_str = gets.chomp\nstr = gets.chomp\nmin_replacements = 1000;\nmin_replace = []\n\ndef string_diff(str1, str2)\n return [] if(str1 == str2)\n puts \"=====#{str1}========#{str2}====\"\n (0..str1.length).map { |i| i + 1 if str1[i] != str2[i] }.compact\nend\n\nif str == sub_str\n puts 0\nelse\n (0..(str_length - sub_str_length)).each do |i|\n replacements = string_diff(str[i, sub_str_length], sub_str)\n if replacements.empty?\n min_replacements = 0\n min_replace = []\n break\n elsif replacements.length < min_replacements\n min_replacements = replacements.length\n min_replace = replacements\n end\n end\n puts min_replacements\n puts min_replace.join(' ')\nend"}, {"source_code": "sub_str_length, str_length = gets.split.map(&:to_i)\n\nsub_str = gets.chomp\nstr = gets.chomp\nmin_replacements = 1000;\nmin_replace = []\n\ndef string_diff(str1, str2)\n (0..str1.length).map { |i| i + 1 if str1[i] != str2[i] }.compact\nend\n\n(0..(str_length - sub_str_length)).each do |i|\n replacements = string_diff(str[i, sub_str_length + i - 1], sub_str)\n if replacements.length < min_replacements\n min_replacements = replacements.length\n min_replace = replacements\n end\nend\n\nputs min_replacements\nputs min_replace.join(' ')"}, {"source_code": "sub_str_length, str_length = gets.split.map(&:to_i)\n\nsub_str = gets.chomp\nstr = gets.chomp\nmin_replacements = 1000;\nmin_replace = []\n\ndef string_diff(str1, str2)\n (0..str1.length).map { |i| i + 1 if str1[i] != str2[i] }.compact\nend\n\n(0..(str_length - sub_str_length)).each do |i|\n replacements = string_diff(str[i, sub_str_length + i - 1], sub_str)\n if replacements.length < min_replacements\n min_replacements = replacements.length\n min_replace = replacements\n end\nend\n\nputs min_replacements\np min_replace.join(' ')"}, {"source_code": "inp = gets.split(' ')\n\nn = inp.first.to_i\nm = inp.last.to_i\n\na = gets.chomp\nb = gets.chomp\n\ndef match(a,b)\n\tmin_errors_size = 10**9\n\tmin_errors_array = Array.new\n\tfor i in (0..b.size-1)\n\t\terrors = []\n\t\tfor j in (0..a.size-1)\n\t\t\tif a[j]!=b[i+j]\n\t\t\t\t\terrors << j+1\n\t\t\tend\n\t\tend\n\t\tif errors.size < min_errors_size\n\t\t\tmin_errors_array = errors\n\t\t\tmin_errors_size = errors.size\n\t\tend\n\tend\n\treturn min_errors_array\nend\n\nans = match(a,b)\n\nputs ans.size\n\nputs ans.join(' ')"}], "src_uid": "dd26f45869b73137e5e5cc6820cdc2e4"} {"nl": {"description": "Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order.According to the schedule, a student can take the exam for the i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day bi (bi < ai). Thus, Valera can take an exam for the i-th subject either on day ai, or on day bi. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number ai.Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date.", "input_spec": "The first line contains a single positive integer n (1 ≤ n ≤ 5000) — the number of exams Valera will take. Each of the next n lines contains two positive space-separated integers ai and bi (1 ≤ bi < ai ≤ 109) — the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly.", "output_spec": "Print a single integer — the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date.", "sample_inputs": ["3\n5 2\n3 1\n4 2", "3\n6 1\n5 2\n4 3"], "sample_outputs": ["2", "6"], "notes": "NoteIn the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5.In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject."}, "positive_code": [{"source_code": "n, k = STDIN.readline.split.map {|s| s.to_i }\npairs = []\nn.times do\n\ta, b = STDIN.readline.split.map {|s| s.to_i }\n\tpairs.push([a, b])\nend\npairs.sort!\npos = 0\npairs.each do |late, early|\n\tif early >= pos\n\t\tpos = early\n\telse\n\t\tpos = late\n\tend\nend\nputs pos\n"}, {"source_code": "\nn = gets.chomp.to_i;\narr = Array.new(n);\n\nn.times { |x|\n arr[x] = gets.chomp.split(' ').map(&:to_i);\n}\narr.sort!;\npre = -1;\n\narr.each { |x, y|\n if (pre <= y)\n pre = y;\n else\n pre = x;\n end \n}\nputs pre;\n"}, {"source_code": "n=gets.to_i\narr=[]\nn.times{|e|\n\tx=gets.split.map{|e| e.to_i}\n\tarr<= x\n\t\tx = arr[i].min\n\telse\n\t\tx = arr[i].max\n\tend\nend\n\nprint x"}, {"source_code": "n = gets.to_i\narr = []\nb_arr = []\nn.times do\n\tarr << gets.split(\" \").map(&:to_i)\n\tb_arr << arr[-1][1]\nend\narr.sort!\n# max_h = {}\n# x = arr.chunk{|x| x[0]}.each do |a|\n# \tmax_h[a[0]] = a[1].collect{|e| e[1]}.max\n# end\nmax = 0\narr.each do |a|\n\tif max <= a[1] \n\t\tmax = a[1]\n\telse\n\t\tmax = a[0]\n\tend\nend\nputs \"#{max}\""}, {"source_code": "n = gets.to_i\na = []\nn.times do\n\ta << gets.split.map(&:to_i)\nend\na.sort!\n\nprev = 0\nn.times do |i|\n\tprev = a[i][1] >= prev ? a[i][1] : a[i][0]\nend\nputs prev"}, {"source_code": "n = gets.to_i\na = []\nn.times do\n a << gets.chomp.split.map(&:to_i)\nend\na.sort!\nd = 0\nfor i in 0...n\n a[i].sort!\n if a[i][0] >= d\n s = a[i][0]\n elsif a[i][1] >= d\n s = a[i][1]\n end\n d = s\nend\nputs d\n"}, {"source_code": "n = gets.to_i\nz = []\nn.times {\n z << gets.chomp.split.map(&:to_i)\n}\n\nz.sort! { |x, y| p = x[0] <=> y[0]; p != 0 ? p : x[1] <=> y[1] }\nx = z[0][1]\n(1...n).each do |i|\n x = z[i][1] >= x ? z[i][1] : z[i][0]\nend\nputs x"}], "negative_code": [{"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\nDay = Struct.new(:a, :b)\nn = gets.to_i\ndays = []\nn.times {\n a, b = gets.split.map(&:to_i)\n days << Day.new(a, b)\n}\ndays.sort! { |d1, d2|\n if d1.a != d2.a\n d1.a - d2.a\n else\n d1.b - d2.b\n end\n}\nbday_broken = false\nbprev = 0\ndays.each { |day|\n if bprev <= day.b\n bprev = day.b\n else\n bday_broken = true\n end\n}\n\nif bday_broken\n puts days.last.a\nelse\n puts days.last.b\nend\n"}, {"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\nDay = Struct.new(:a, :b)\nn = gets.to_i\ndays = []\nn.times {\n a, b = gets.split.map(&:to_i)\n days << Day.new(a, b)\n}\ndays.sort! { |d1, d2|\n if d1.a != d2.a\n d1.a - d2.a\n else\n d1.b - d1.b\n end\n}\nbday_broken = false\nbprev = 0\ndays.each { |day|\n if bprev <= day.b\n bprev = day.b\n else\n bday_broken = true\n end\n}\n\nif bday_broken\n puts days.last.a\nelse\n puts days.last.b\nend\n"}, {"source_code": "n = Integer(gets)\na = []\nnums = []\nfor i in 0...n\n a[i] = []\n nums[i] = []\n nums[i] = gets.chomp.split\n a[i][0] = Integer(nums[i][0])\n a[i][1] = Integer(nums[i][1])\nend\n\na = a.sort_by{|x,y|x}\nbest = -1\nfor i in 0...n\n if best <= a[i][1]\n best = a[i][1]\n else\n best = a[i][0]\n end\nend\nprint best"}, {"source_code": "n = Integer(gets)\na = []\nnums = []\nfor i in 0...n\n a[i] = []\n nums[i] = []\n nums[i] = gets.chomp.split\n a[i][0] = Integer(nums[i][0])\n a[i][1] = Integer(nums[i][1])\nend\npointer = 1\nfor i in 0...n-1\n if a[i][0] == a[i + 1][0]\n pointer = 1\n else\n pointer = 0\n break\n end\nend\nbest = -1\nif pointer == 0\n a = a.sort_by{|x,y|x}\nelse\n a = a.sort_by{|x,y|y}\nend\nfor i in 0...n\n if best <= a[i][1]\n best = a[i][1]\n else\n best = a[i][0]\n end\nend\nprint best"}, {"source_code": "n = gets.chomp.to_i\narr = []\nfor i in (0..n-1)\n\tarr << gets.chomp.split.map(&:to_i)\nend\n\narr = arr.sort_by{|x,y| x&&y}\n\nx = arr[0].min\nfor i in (1..n-1)\n\tif arr[i].min >= x\n\t\tx = arr[i].min\n\telse\n\t\tx = arr[i].max\n\tend\nend\n\nprint x"}, {"source_code": "n = gets.chomp.to_i\narr = []\nfor i in (0..n-1)\n\tarr << gets.chomp.split.map(&:to_i)\nend\n\narr = arr.sort_by{|x,y| y&&x}\n\nx = arr[0].min\nfor i in (1..n-1)\n\tif arr[i].min >= x\n\t\tx = arr[i].min\n\telse\n\t\tx = arr[i].max\n\tend\nend\n\nprint x"}, {"source_code": "n = gets.chomp.to_i\narr = []\nfor i in (0..n-1)\n\tarr << gets.chomp.split.map(&:to_i)\nend\n\narr = arr.sort_by{|x,y| y&&x}\nif arr[0].min <= arr[n-1].min\n\tprint arr[n-1].min\nelse\n\tprint arr[n-1].max\nend"}, {"source_code": "n = gets.to_i\narr = []\nb_arr = []\nn.times do\n\tarr << gets.split(\" \").map(&:to_i)\n\tb_arr << arr[-1][1]\nend\narr.sort!\nmax_h = {}\nx = arr.chunk{|x| x[0]}.each do |a|\n\tmax_h[a[0]] = a[1].collect{|e| e[1]}.max\nend\nmax = 0\nmax_h.each do |k,v|\n\tif max <= v \n\t\tmax = v\n\telse\n\t\tmax = k\n\tend\nend\nputs \"#{max}\""}, {"source_code": "n = gets.to_i\narr = []\nb_arr = []\nn.times do\n\tarr << gets.split(\" \").map(&:to_i)\n\tb_arr << arr[-1][1]\nend\narr.sort!\nmax_h = {}\nx = arr.chunk{|x| x[0]}.each do |a|\n\tmax_h[a[0]] = a[1].collect{|e| e[1]}.max\nend\nmax = 0\nmax_h.each do |k,v|\n\tif v >= max\n\t\tmax = v\n\telse\n\t\tmax = k\n\tend\nend\nputs \"#{max}\""}, {"source_code": "n = gets.to_i\narr = []\nb_arr = []\nn.times do\n\tarr << gets.split(\" \").map(&:to_i)\n\tb_arr << arr[-1][1]\nend\narr.sort!\nx = arr.chunk{|x| x[0]}.to_a.last[1].collect{|e| e[1]}\nz = x.max\nif b_arr.all?{|e| z >= e}\n\tans = z\nelse\n\tans = arr[-1][0]\nend\nputs ans"}, {"source_code": "n = gets.to_i\nz = []\nn.times {\n z << gets.chomp.split.map(&:to_i)\n}\n\nz.sort! { |x, y| x[0] <=> y[0] }\nx = z[0][1]\n(1...n).each do |i|\n x = z[i][1] >= x ? z[i][1] : z[i][0]\nend\nputs x"}], "src_uid": "71bc7c4eb577441f2563e40d95306752"} {"nl": {"description": "You have $$$n$$$ chains, the $$$i$$$-th chain consists of $$$c_i$$$ vertices. Vertices in each chain are numbered independently from $$$1$$$ to $$$c_i$$$ along the chain. In other words, the $$$i$$$-th chain is the undirected graph with $$$c_i$$$ vertices and $$$(c_i - 1)$$$ edges connecting the $$$j$$$-th and the $$$(j + 1)$$$-th vertices for each $$$1 \\le j < c_i$$$.Now you decided to unite chains in one graph in the following way: the first chain is skipped; the $$$1$$$-st vertex of the $$$i$$$-th chain is connected by an edge with the $$$a_i$$$-th vertex of the $$$(i - 1)$$$-th chain; the last ($$$c_i$$$-th) vertex of the $$$i$$$-th chain is connected by an edge with the $$$b_i$$$-th vertex of the $$$(i - 1)$$$-th chain. Picture of the first test case. Dotted lines are the edges added during uniting process Calculate the length of the longest simple cycle in the resulting graph.A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.", "input_spec": "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$$$ ($$$2 \\le n \\le 10^5$$$) — the number of chains you have. The second line of each test case contains $$$n$$$ integers $$$c_1, c_2, \\dots, c_n$$$ ($$$2 \\le c_i \\le 10^9$$$) — the number of vertices in the corresponding chains. The third line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$a_1 = -1$$$; $$$1 \\le a_i \\le c_{i - 1}$$$). The fourth line of each test case contains $$$n$$$ integers $$$b_1, b_2, \\dots, b_n$$$ ($$$b_1 = -1$$$; $$$1 \\le b_i \\le c_{i - 1}$$$). Both $$$a_1$$$ and $$$b_1$$$ are equal to $$$-1$$$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.", "output_spec": "For each test case, print the length of the longest simple cycle.", "sample_inputs": ["3\n4\n3 4 3 3\n-1 1 2 2\n-1 2 2 3\n2\n5 6\n-1 5\n-1 1\n3\n3 5 2\n-1 1 1\n-1 3 5"], "sample_outputs": ["7\n11\n8"], "notes": "NoteIn the first test case, the longest simple cycle is shown below: We can't increase it with the first chain, since in such case it won't be simple — the vertex $$$2$$$ on the second chain will break simplicity."}, "positive_code": [{"source_code": "def read\r\n gets.split.map &:to_i\r\nend\r\ngets.to_i.times do\r\n n = gets.to_i\r\n c, a, b = read, read, read\r\n dp = [0] * (n + 1)\r\n (1...n).each do |i|\r\n d = (a[i] - b[i]).abs\r\n if d == 0\r\n dp[i + 1] = c[i] + 1\r\n else\r\n dp[i + 1] = c[i] + 1 + [d, dp[i] - d].max\r\n end\r\n end\r\n puts dp.max\r\nend"}], "negative_code": [{"source_code": "def read\r\n gets.split.map &:to_i\r\nend\r\ngets.to_i.times do\r\n n = gets.to_i\r\n c, a, b = read, read, read\r\n dp = [c[0]] + [0] * (n - 1)\r\n (1...n).each do |i|\r\n d = (a[i] - b[i]).abs\r\n if d == 0\r\n dp[i] = c[i] + 1\r\n else\r\n dp[i] = c[i] + 1 + [d, dp[i - 1] - d].max\r\n end\r\n end\r\n puts dp.max\r\nend"}], "src_uid": "3d898a45ab89b93e006270a77db49017"} {"nl": {"description": "Once little Vasya read an article in a magazine on how to make beautiful handmade garland from colored paper. Vasya immediately went to the store and bought n colored sheets of paper, the area of each sheet is 1 square meter.The garland must consist of exactly m pieces of colored paper of arbitrary area, each piece should be of a certain color. To make the garland, Vasya can arbitrarily cut his existing colored sheets into pieces. Vasya is not obliged to use all the sheets to make the garland.Vasya wants the garland to be as attractive as possible, so he wants to maximize the total area of ​​m pieces of paper in the garland. Calculate what the maximum total area of ​​the pieces of paper in the garland Vasya can get.", "input_spec": "The first line contains a non-empty sequence of n (1 ≤ n ≤ 1000) small English letters (\"a\"...\"z\"). Each letter means that Vasya has a sheet of paper of the corresponding color. The second line contains a non-empty sequence of m (1 ≤ m ≤ 1000) small English letters that correspond to the colors of the pieces of paper in the garland that Vasya wants to make.", "output_spec": "Print an integer that is the maximum possible total area of the pieces of paper in the garland Vasya wants to get or -1, if it is impossible to make the garland from the sheets he's got. It is guaranteed that the answer is always an integer.", "sample_inputs": ["aaabbac\naabbccac", "a\nz"], "sample_outputs": ["6", "-1"], "notes": "NoteIn the first test sample Vasya can make an garland of area 6: he can use both sheets of color b, three (but not four) sheets of color a and cut a single sheet of color c in three, for example, equal pieces. Vasya can use the resulting pieces to make a garland of area 6.In the second test sample Vasya cannot make a garland at all — he doesn't have a sheet of color z."}, "positive_code": [{"source_code": "#!/usr/bin/ruby\nh=Hash.new(0)\ngets.chomp.each_char{|c|h[c]+=1}\ng=Hash.new(0)\ngets.chomp.each_char{|c|g[c]+=1}\nr=0\ng.each{|k,v|\n\tif h[k]==0\n\t\tp -1\n\t\texit\n\tend\n\tr+=[h[k],v].min\n}\np r"}, {"source_code": "a,b,ans=Array.new(26,0),Array.new(26,0),0\ngets.chomp.split('').each{|i| a[i.ord-97]+=1}\ngets.chomp.split('').each{|i| b[i.ord-97]+=1}\n26.times do |i|\n\tif a[i]==0 and a[i]!=b[i]\n\t\tputs \"-1\"\n\t\texit\n\tend\nend\n26.times{|i| ans+=[a[i],b[i]].min}\nputs ans"}, {"source_code": "h = {}\n# ?a.upto(?z) { |c| h[c] = 0 }\ns = STDIN.readline.chop\ns.each_char do |c|\n\tif h[c] == nil ;\n\t\th[c] = 0\n\tend\n\th[c] += 1\nend\n\nans = 0\nflag = true\ns = STDIN.readline.chop\ns.each_char do |c|\n\tif h[c] == nil ;\n\t\tflag = false\n\telsif h[c] > 0 ;\n\t\th[c] -= 1\n\t\tans += 1\n\tend\nend\n\nprint flag ? ans : -1\n"}, {"source_code": "s1 = gets.chomp.split(\"\")\ns2 = gets.chomp.split(\"\")\ncount1 = Hash.new(0)\ns1.each do |a|\n\tcount1[a] += 1\nend\n\ncount2 = Hash.new(0)\ns2.each do |a|\n\tcount2[a] += 1\nend\n\nans = true\ns = 0\ncount2.each do |k,v|\t\n\tif count1[k] != 0\n\t\ts += [count1[k],v].min\n\telse\n\t\tans = false\n\t\tbreak\n\tend\t\nend\nputs ans ? s : \"-1\""}, {"source_code": "c1, c2 = {}, {}\ngets.chomp.each_byte{|c| c1[c]=c1[c].nil? ? 1 : c1[c]+1}\ngets.chomp.each_byte{|c| c2[c]=c2[c].nil? ? 1 : c2[c]+1}\nsum = 0\nc2.each do |k,v|\n\tif c1[k].nil?\n\t\tputs -1\n\t\texit 0\n\tend\n\tsum += [v, c1[k]].min\nend\nputs sum\n"}, {"source_code": "a = gets.chomp\nb = gets.chomp\ncnt1 = Hash.new\ncnt2 = Hash.new\nfor x in a.scan /./\n if cnt1[x] == nil\n cnt1[x] = 0\n end\n cnt1[x] += 1\nend\nfor x in b.scan /./\n if cnt2[x] == nil\n cnt2[x] = 0\n end\n cnt2[x] += 1\nend\nans = 0\ncnt2.each_pair do |k, v|\n if cnt1[k] == nil\n puts -1\n exit\n end\n ans += [v, cnt1[k]].min\nend\nputs ans\n"}], "negative_code": [{"source_code": "h = {}\n?a.upto(?z) { |c| h[c] = 0 }\ns = STDIN.readline.chop\ns.each_char { |c| h[c] += 1 }\n\nans = 0\ns = STDIN.readline.chop\ns.each_char do |c|\n\tif h[c] > 0\n\t\th[c] -= 1\n\t\tans += 1\n\tend\nend\nans = -1 if ans == 0\nprint ans\n"}, {"source_code": "t = gets.to_i\nt.times do \n\tn = gets.to_i\n\ts = 0\n\ti = 1\n\tans = -1\n\t(1..n).each do |i|\n\t\ts += i\n\t\tif s >= n\n\t\t\tans = i\n\t\t\ts -= i\n\t\t\tbreak\n\t\tend\n\tend\n\trow,col = ans,n - s\n\n\n\tsum = row + 1\n\tnumerator = sum - col\n\tdenominator = col\n\n\tnumerator,denominator = denominator,numerator if row.even?\n\n\tputs \"TERM #{n} IS #{numerator}/#{denominator}\"\nend"}, {"source_code": "c1, c2 = {}, {}\ngets.chomp.each_byte{|c| c1[c]=c1[c].nil? ? 1 : c1[c]+1}\ngets.chomp.each_byte{|c| c2[c]=c2[c].nil? ? 1 : c2[c]+1}\nsum = 0\nc1.each{|k,v| sum+=[v,c2[k].nil? ? 0 : c2[k]].min}\nsum = -1 if sum == 0\nputs sum\n"}], "src_uid": "b1e09df7c47dbd04992e64826337c28a"} {"nl": {"description": "Karen has just arrived at school, and she has a math test today! The test is about basic addition and subtraction. Unfortunately, the teachers were too busy writing tasks for Codeforces rounds, and had no time to make an actual test. So, they just put one question in the test that is worth all the points.There are n integers written on a row. Karen must alternately add and subtract each pair of adjacent integers, and write down the sums or differences on the next row. She must repeat this process on the values on the next row, and so on, until only one integer remains. The first operation should be addition.Note that, if she ended the previous row by adding the integers, she should start the next row by subtracting, and vice versa.The teachers will simply look at the last integer, and then if it is correct, Karen gets a perfect score, otherwise, she gets a zero for the test.Karen has studied well for this test, but she is scared that she might make a mistake somewhere and it will cause her final answer to be wrong. If the process is followed, what number can she expect to be written on the last row?Since this number can be quite large, output only the non-negative remainder after dividing it by 109 + 7.", "input_spec": "The first line of input contains a single integer n (1 ≤ n ≤ 200000), the number of numbers written on the first row. The next line contains n integers. Specifically, the i-th one among these is ai (1 ≤ ai ≤ 109), the i-th number on the first row.", "output_spec": "Output a single integer on a line by itself, the number on the final row after performing the process above. Since this number can be quite large, print only the non-negative remainder after dividing it by 109 + 7.", "sample_inputs": ["5\n3 6 9 12 15", "4\n3 7 5 2"], "sample_outputs": ["36", "1000000006"], "notes": "NoteIn the first test case, the numbers written on the first row are 3, 6, 9, 12 and 15.Karen performs the operations as follows: The non-negative remainder after dividing the final number by 109 + 7 is still 36, so this is the correct output.In the second test case, the numbers written on the first row are 3, 7, 5 and 2.Karen performs the operations as follows: The non-negative remainder after dividing the final number by 109 + 7 is 109 + 6, so this is the correct output."}, "positive_code": [{"source_code": "require 'openssl'\nN = gets.chomp.to_i\nA = gets.split.map(&:to_i)\nM = 1000000007\nsign = 1\nuntil (N - 1) % 4 == 0\n na = Array.new(N - 1, 0)\n (N - 1).times { |i| na[i] = A[i] + sign * A[i + 1]; sign *= -1 }\n N, A, na = N - 1, na, A\nend\nf = (1..N / 2 + 1).each_with_object([1]) { |i, o| o << o[-1] * i % M }\nvf = (-(N / 2)..0).each_with_object([f[N / 2 + 1].to_bn.mod_exp(M - 2, M)]) { |i, o| o << o[-1] * (-i + 1) % M }.reverse\np (0...N).step(2).reduce(0) { |o, i| o = (o + A[i] * f[N / 2] * vf[i / 2] * vf[N / 2 - i / 2]) % M }\n"}, {"source_code": "=begin\narr = [1]\nsign = -1\n25.times { |i|\n print \"%d: \" % i\n p arr\n n_arr = Array.new(arr.length + 1, 0)\n for i in 0..arr.length\n n_arr[i] += arr[i - 1] if i - 1 >= 0\n (n_arr[i] += sign * arr[i]; sign *= -1) if i < arr.length\n end\n arr, n_arr = n_arr, arr\n}\n0: [1]\n1: [-1, 1]\n2: [-1, -2, 1]\n3: [-1, 1, -1, 1]\n4: [1, 0, 2, 0, 1]\n5: [-1, 1, -2, 2, -1, 1]\n6: [-1, -2, -1, -4, 1, -2, 1]\n7: [-1, 1, -3, 3, -3, 3, -1, 1]\n8: [1, 0, 4, 0, 6, 0, 4, 0, 1]\n9: [-1, 1, -4, 4, -6, 6, -4, 4, -1, 1]\n10: [-1, -2, -3, -8, -2, -12, 2, -8, 3, -2, 1]\n11: [-1, 1, -5, 5, -10, 10, -10, 10, -5, 5, -1, 1]\n12: [1, 0, 6, 0, 15, 0, 20, 0, 15, 0, 6, 0, 1]\n13: [-1, 1, -6, 6, -15, 15, -20, 20, -15, 15, -6, 6, -1, 1]\n14: [-1, -2, -5, -12, -9, -30, -5, -40, 5, -30, 9, -12, 5, -2, 1]\n15: [-1, 1, -7, 7, -21, 21, -35, 35, -35, 35, -21, 21, -7, 7, -1, 1]\n16: [1, 0, 8, 0, 28, 0, 56, 0, 70, 0, 56, 0, 28, 0, 8, 0, 1]\n17: [-1, 1, -8, 8, -28, 28, -56, 56, -70, 70, -56, 56, -28, 28, -8, 8, -1, 1]\n18: [-1, -2, -7, -16, -20, -56, -28, -112, -14, -140, 14, -112, 28, -56, 20, -16, 7, -2, 1]\n19: [-1, 1, -9, 9, -36, 36, -84, 84, -126, 126, -126, 126, -84, 84, -36, 36, -9, 9, -1, 1]\n20: [1, 0, 10, 0, 45, 0, 120, 0, 210, 0, 252, 0, 210, 0, 120, 0, 45, 0, 10, 0, 1]\n21: [-1, 1, -10, 10, -45, 45, -120, 120, -210, 210, -252, 252, -210, 210, -120, 120, -45, 45, -10, 10, -1, 1]\n22: [-1, -2, -9, -20, -35, -90, -75, -240, -90, -420, -42, -504, 42, -420, 90, -240, 75, -90, 35, -20, 9, -2, 1]\n23: [-1, 1, -11, 11, -55, 55, -165, 165, -330, 330, -462, 462, -462, 462, -330, 330, -165, 165, -55, 55, -11, 11, -1, 1]\n24: [1, 0, 12, 0, 66, 0, 220, 0, 495, 0, 792, 0, 924, 0, 792, 0, 495, 0, 220, 0, 66, 0, 12, 0, 1]\n=end \nrequire 'openssl'\nN = $stdin.gets.chomp.to_i\nA = $stdin.gets.split.map(&:to_i)\nM = (1e9 + 7).to_i\nsign = 1\nuntil (N - 1) % 4 == 0\n na = Array.new(N - 1, 0)\n (N - 1).times { |i| na[i] = A[i] + sign * A[i + 1]; sign *= -1 }\n N, A, na = N - 1, na, A\nend\nf = (1..N / 2 + 1).each_with_object([1]) { |i, o| o << o[-1] * i % M }\nvf = (-(N / 2)..0).each_with_object([f[N / 2 + 1].to_bn.mod_exp(M - 2, M)]) { |i, o| o << o[-1] * (-i + 1) % M }.reverse\nans = 0\n(0...N).step(2).each { |i| ans = (ans + A[i] * f[N / 2] * vf[i / 2] * vf[N / 2 - i / 2]) % M }\nans += M if ans < 0\nputs ans\n"}, {"source_code": "require 'openssl'\nN = gets.chomp.to_i\nA = gets.split.map(&:to_i)\nM = 1000000007\nsign = 1\nuntil (N - 1) % 4 == 0\n na = Array.new(N - 1, 0)\n (N - 1).times { |i| na[i] = A[i] + sign * A[i + 1]; sign *= -1 }\n N, A, na = N - 1, na, A\nend\nf = (1..N / 2 + 1).each_with_object([1]) { |i, o| o << o[-1] * i % M }\nvf = (-(N / 2)..0).each_with_object([f[N / 2 + 1].to_bn.mod_exp(M - 2, M)]) { |i, o| o << o[-1] * (-i + 1) % M }.reverse\nans = 0\n(0...N).step(2).each { |i| ans = (ans + A[i] * f[N / 2] * vf[i / 2] * vf[N / 2 - i / 2]) % M }\nans += M if ans < 0\np ans\n"}], "negative_code": [{"source_code": "=begin\narr = [1]\nsign = -1\n25.times { |i|\n print \"%d: \" % i\n p arr\n n_arr = Array.new(arr.length + 1, 0)\n for i in 0..arr.length\n n_arr[i] += arr[i - 1] if i - 1 >= 0\n (n_arr[i] += sign * arr[i]; sign *= -1) if i < arr.length\n end\n arr, n_arr = n_arr, arr\n}\n0: [1]\n1: [-1, 1]\n2: [-1, -2, 1]\n3: [-1, 1, -1, 1]\n4: [1, 0, 2, 0, 1]\n5: [-1, 1, -2, 2, -1, 1]\n6: [-1, -2, -1, -4, 1, -2, 1]\n7: [-1, 1, -3, 3, -3, 3, -1, 1]\n8: [1, 0, 4, 0, 6, 0, 4, 0, 1]\n9: [-1, 1, -4, 4, -6, 6, -4, 4, -1, 1]\n10: [-1, -2, -3, -8, -2, -12, 2, -8, 3, -2, 1]\n11: [-1, 1, -5, 5, -10, 10, -10, 10, -5, 5, -1, 1]\n12: [1, 0, 6, 0, 15, 0, 20, 0, 15, 0, 6, 0, 1]\n13: [-1, 1, -6, 6, -15, 15, -20, 20, -15, 15, -6, 6, -1, 1]\n14: [-1, -2, -5, -12, -9, -30, -5, -40, 5, -30, 9, -12, 5, -2, 1]\n15: [-1, 1, -7, 7, -21, 21, -35, 35, -35, 35, -21, 21, -7, 7, -1, 1]\n16: [1, 0, 8, 0, 28, 0, 56, 0, 70, 0, 56, 0, 28, 0, 8, 0, 1]\n17: [-1, 1, -8, 8, -28, 28, -56, 56, -70, 70, -56, 56, -28, 28, -8, 8, -1, 1]\n18: [-1, -2, -7, -16, -20, -56, -28, -112, -14, -140, 14, -112, 28, -56, 20, -16, 7, -2, 1]\n19: [-1, 1, -9, 9, -36, 36, -84, 84, -126, 126, -126, 126, -84, 84, -36, 36, -9, 9, -1, 1]\n20: [1, 0, 10, 0, 45, 0, 120, 0, 210, 0, 252, 0, 210, 0, 120, 0, 45, 0, 10, 0, 1]\n21: [-1, 1, -10, 10, -45, 45, -120, 120, -210, 210, -252, 252, -210, 210, -120, 120, -45, 45, -10, 10, -1, 1]\n22: [-1, -2, -9, -20, -35, -90, -75, -240, -90, -420, -42, -504, 42, -420, 90, -240, 75, -90, 35, -20, 9, -2, 1]\n23: [-1, 1, -11, 11, -55, 55, -165, 165, -330, 330, -462, 462, -462, 462, -330, 330, -165, 165, -55, 55, -11, 11, -1, 1]\n24: [1, 0, 12, 0, 66, 0, 220, 0, 495, 0, 792, 0, 924, 0, 792, 0, 495, 0, 220, 0, 66, 0, 12, 0, 1]\n=end \nrequire 'openssl'\nN = $stdin.gets.chomp.to_i\nA = $stdin.gets.split.map(&:to_i)\nM = (1e9 + 7).to_i\nsign = 1\nuntil (N - 1) % 4 == 0\n na = Array.new(N - 1, 0)\n (N - 1).times { |i| na[i] = A[i] + sign * A[i + 1]; sign *= -1 }\n N, A, na = N - 1, na, A\nend\nf = (1..N / 2 + 1).each_with_object([1]) { |i, o| o << o[-1] * i % M }\nvf = (-(N / 2)..0).each_with_object([f[N / 2 + 1].to_bn.mod_exp(M - 2, M)]) { |i, o| o << o[-1] * (-i + 1) % M }.reverse\nans = 0\n(0...N).step(2).each { |i| ans = (ans + A[i] * sign * f[N / 2] * vf[i / 2] * vf[N / 2 - i / 2]) % M }\nans += M if ans < 0\nputs ans\n"}], "src_uid": "cf210ef68d0525dcb1574f450773da39"} {"nl": {"description": "The territory of Berland is represented by a rectangular field n × m in size. The king of Berland lives in the capital, located on the upper left square (1, 1). The lower right square has coordinates (n, m). One day the king decided to travel through the whole country and return back to the capital, having visited every square (except the capital) exactly one time. The king must visit the capital exactly two times, at the very beginning and at the very end of his journey. The king can only move to the side-neighboring squares. However, the royal advise said that the King possibly will not be able to do it. But there is a way out — one can build the system of one way teleporters between some squares so that the king could fulfill his plan. No more than one teleporter can be installed on one square, every teleporter can be used any number of times, however every time it is used, it transports to the same given for any single teleporter square. When the king reaches a square with an installed teleporter he chooses himself whether he is or is not going to use the teleport. What minimum number of teleporters should be installed for the king to complete the journey? You should also compose the journey path route for the king.", "input_spec": "The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 100, 2 ≤  n · m) — the field size. The upper left square has coordinates (1, 1), and the lower right square has coordinates of (n, m).", "output_spec": "On the first line output integer k — the minimum number of teleporters. Then output k lines each containing 4 integers x1 y1 x2 y2 (1 ≤ x1, x2 ≤ n, 1 ≤ y1, y2 ≤ m) — the coordinates of the square where the teleporter is installed (x1, y1), and the coordinates of the square where the teleporter leads (x2, y2). Then print nm + 1 lines containing 2 numbers each — the coordinates of the squares in the order in which they are visited by the king. The travel path must start and end at (1, 1). The king can move to side-neighboring squares and to the squares where a teleporter leads. Besides, he also should visit the capital exactly two times and he should visit other squares exactly one time.", "sample_inputs": ["2 2", "3 3"], "sample_outputs": ["0\n1 1\n1 2\n2 2\n2 1\n1 1", "1\n3 3 1 1\n1 1\n1 2\n1 3\n2 3\n2 2\n2 1\n3 1\n3 2\n3 3\n1 1"], "notes": null}, "positive_code": [{"source_code": "n,m=gets.scan(/\\d+/).map(&:to_i)\nf=1\nif ( n==1 || m==1 ) && (n*m!=2)\n\tputs 1\n\tputs n.to_s+\" \"+m.to_s+\" 1 1\"\n\t(n*m).times{|x| \n\t\tputs n>1? (x+1).to_s+\" 1\":\"1 \"+(x+1).to_s\n\t}\n\tputs \"1 1\"\nelsif n%2==0\n\tputs 0\n\tputs \"1 1\"\n\tn.times{|x|\n\t\t(m-1).times{|y|\n\t\t\tputs (x+1).to_s+\" \"+(f==1?y+2:m-y).to_s\n\t\t}\n\t\tf=-1*f\n\t}\n\tn.times{|x| puts (n-x).to_s+\" 1\"}\nelsif m%2==0\n\tputs 0\n\tputs \"1 1\"\n\tm.times{|x|\n\t\t(n-1).times{|y|\n\t\t\tputs (f==1?y+2:n-y).to_s+\" \"+(x+1).to_s\n\t\t}\n\t\tf=-1*f\n\t}\n\tm.times{|x| puts \"1 \"+(m-x).to_s}\nelse\n\tputs 1\n\tputs n.to_s+\" \"+m.to_s+\" 1 1\"\n\tn.times{|x|\n\t\tm.times{|y|\n\t\t\tputs (x+1).to_s+\" \"+(f==1?y+1:m-y).to_s\n\t\t}\n\t\tf=-1*f\n\t}\n\tputs \"1 1\"\nend"}, {"source_code": "m,n=gets.split.map(&:to_i)\nf,n,m=0,m,n if m%2>z=0\ns=[[i=1,j=1]]\nv=\"s.unshift [n,m,z=1,n<2?1:j]if j%2>0||n<2&&m>2;n<2&&j=1\"\nloop{s<<[i+=1,j]while i2\nbreak eval v if m==j\ns<<[i,j+=1]}\ns<<[1,j+1]until 0>j-=1\nf||s.map!{|i|i.each_slice(2).to_a.flat_map &:reverse}\nputs z,s.map{|i|i*\" \"}"}, {"source_code": "n,m=gets.split.map(&:to_i)\nf,n,m=0,m,n if n%2==0\ns=[[i=1,j=1]]\nz = 0\nloop do\n while i < n\n s << [i+=1,j]\n end\n if m == j\n if n == 1\n s.unshift [1,m,1,1]\n s << [1,1]\n else\n s.unshift [n,m,1,j=m]\n if i > 1\n s << [1,j]\n while j > 1\n s << [1,j-=1]\n end\n end\n end\n z = 1\n break\n end\n s << [i,j+=1]\n while i > 2\n s << [i-=1,j]\n end\n if m == j\n if n == 1\n if m > 2\n z = 1\n s.unshift [1,m,1,1]\n end\n s << [1,1]\n else\n #s.unshift [n,m,1,j=m]\n if i > 1\n s << [1,j]\n while j > 1\n s << [1,j-=1]\n end\n end\n end\n break\n end\n s << [i,j+=1]\nend\np z\nif f\n s.map!{|i|i.each_slice(2).to_a.flat_map &:reverse}\nend\ns.each{|i|puts i*\" \"}\n"}, {"source_code": "m,n=gets.split.map(&:to_i)\nf,n,m=0,m,n if m%2>z=0\ns=[[i=1,j=1]]\nv=\"s.unshift [n,m,z=1,n<2?1:j]if j%2>0||n<2&&m>2;n<2&&j=1\"\nloop{s<<[i+=1,j]while i2\nbreak eval v if m==j\ns<<[i,j+=1]}\ns<<[1,j+1]until 0>j-=1\nf||s.map!{|i|i.each_slice(2).to_a.flat_map &:reverse}\nputs z,s.map{|i|i*\" \"}\n"}, {"source_code": "m,n=gets.split.map(&:to_i)\nf,n,m=0,m,n if m%2>z=0\ns=[[i=1,j=1]]\nv=\"s.unshift [n,m,z=1,n<2?1:j]if j%2>0||n<2&&m>2;n<2&&j=1\"\nloop{s<<[i+=1,j]while i2\nbreak eval v if m==j\ns<<[i,j+=1]}\ns<<[1,j+1]until 0>j-=1\nf||s.map!{|i|i.each_slice(2).to_a.flat_map &:reverse}\nputs z,s.map{|i|i*\" \"}\n"}, {"source_code": "m,n=gets.split.map(&:to_i)\nf,n,m=0,m,n if m%2>z=0\ns=[[i=1,j=1]]\nv=\"s.unshift [n,m,z=1,n<2?1:j]if j%2>0||n<2&&m>2;n<2&&j=1\"\nloop{s<<[i+=1,j]while i2\nbreak eval v if m==j\ns<<[i,j+=1]}\ns<<[1,j+1]until 0>j-=1\nf||s.map!{|i|i.each_slice(2).to_a.flat_map &:reverse}\nputs z,s.map{|i|i*\" \"}\n"}, {"source_code": "m,n=gets.split.map(&:to_i)\nf,n,m=0,m,n if m%2>z=0\ns=[[i=1,j=1]]\nv=\"s.unshift [n,m,z=1,n<2?1:j]if j%2>0||n<2&&m>2;n<2&&j=1\"\nloop{s<<[i+=1,j]while i2\nbreak eval v if m==j\ns<<[i,j+=1]}\ns<<[1,j+1]until 0>j-=1\nf||s.map!{|i|i.each_slice(2).to_a.flat_map &:reverse}\nputs z,s.map{|i|i*\" \"}\n"}, {"source_code": "m,n=gets.split.map(&:to_i)\nf,n,m=0,m,n if m%2>z=0\ns=[[i=1,j=1]]\nv=\"s.unshift [n,m,z=1,n<2?1:j]if j%2>0||n<2&&m>2;n<2&&j=1\"\nloop{s<<[i+=1,j]while i2\nbreak eval v if m==j\ns<<[i,j+=1]}\ns<<[1,j+1]until 0>j-=1\nf||s.map!{|i|i.each_slice(2).to_a.flat_map &:reverse}\nputs z,s.map{|i|i*\" \"}"}, {"source_code": "m,n=gets.split.map(&:to_i)\nf,n,m=0,m,n if m%2>z=0\ns=[[i=1,j=1]]\nv=\"s.unshift [n,m,z=1,n<2?1:j]if j%2>0||n<2&&m>2;n<2&&j=1\"\nloop{s<<[i+=1,j]while i2\nbreak eval v if m==j\ns<<[i,j+=1]}\ns<<[1,j+1]until 0>j-=1\nf||s.map!{|i|i.each_slice(2).to_a.flat_map &:reverse}\nputs z,s.map{|i|i*\" \"}\n"}, {"source_code": "m,n=gets.split.map(&:to_i)\nf,n,m=0,m,n if m%2>z=0\ns=[[i=1,j=1]]\nv=\"s.unshift [n,m,z=1,n<2?1:j]if j%2>0||n<2&&m>2;n<2&&j=1\"\nloop{s<<[i+=1,j]while i2\nbreak eval v if m==j\ns<<[i,j+=1]}\ns<<[1,j+1]until 0>j-=1\nf||s.map!{|i|i.each_slice(2).to_a.flat_map &:reverse}\nputs z,s.map{|i|i*\" \"}\n"}, {"source_code": "m,n=gets.split.map(&:to_i)\nf,n,m=0,m,n if m%2>z=0\ns=[[i=1,j=1]]\nv=\"s.unshift [n,m,z=1,n<2?1:j]if j%2>0||n<2&&m>2;n<2&&j=1\"\nloop{s<<[i+=1,j]while i2\nbreak eval v if m==j\ns<<[i,j+=1]}\ns<<[1,j+1]until 0>j-=1\nf||s.map!{|i|i.each_slice(2).to_a.flat_map &:reverse}\nputs z,s.map{|i|i*\" \"}\n"}, {"source_code": "def generate_teleport_path(n, m)\n path = []\n columns = [ (1 .. m).to_a.reverse, (1 .. m).to_a ]\n (1 .. n).each do |row|\n index = row % 2\n columns[index].each { |col| path.push([row, col]) }\n end\n path.push([1, 1])\n return path\nend\n\ndef generate_path(n, m)\n if n % 2 == 1\n result = generate_path(m, n)\n return result.map { |v| v.reverse }\n end\n path = []\n (1 .. m).each { |col| path.push([1, col]) }\n columns = [ (2 .. m).to_a.reverse, (2 .. m).to_a ]\n (2 .. n).each do |row|\n index = row % 2\n columns[index].each { |col| path.push([row, col]) }\n end\n n.downto(1) { |row| path.push([row, 1]) }\n return path\nend\n\n\nn, m = gets.split.map { |v| v.to_i }\n\nteleports = []\n\nif n == 1 and m == 1\n path = [[1, 1], [1, 1]]\nelsif (n == 1 and m == 2) or (n == 2 and m == 1)\n path = [[1, 1], [n, m], [1, 1]]\nelsif n % 2 == 1 and m % 2 == 1 or n == 1 or m == 1\n teleports = [[n, m, 1, 1]]\n path = generate_teleport_path(n, m)\nelse\n path = generate_path(n, m)\nend\n\nputs teleports.size\nteleports.each { |port| puts port.join(' ') }\npath.each { |pair| puts pair.join(' ') }\n"}, {"source_code": "m,n=gets.split.map(&:to_i)\nf,n,m=0,m,n if m%2>z=0\ns=[[i=1,j=1]]\nv=\"s.unshift [n,m,z=1,n<2?1:j]if j%2>0||n<2&&m>2;n<2&&j=1\"\nloop{s<<[i+=1,j]while i2\nbreak eval v if m==j\ns<<[i,j+=1]}\ns<<[1,j+1]until 0>j-=1\nf||s.map!{|i|i.each_slice(2).to_a.flat_map &:reverse}\nputs z,s.map{|i|i*\" \"}\n"}], "negative_code": [{"source_code": "n,m=gets.scan(/\\d+/).map(&:to_i)\nf=1\nif n==1 || m==1\n\tputs 1\n\tputs n.to_s+\" \"+m.to_s+\" 1 1\"\n\t(n*m).times{|x| \n\t\tputs n>1? (x+1).to_s+\" 1\":\"1 \"+(x+1).to_s\n\t}\n\tputs \"1 1\"\nelsif n%2==0\n\tputs 0\n\tputs \"1 1\"\n\tn.times{|x|\n\t\t(m-1).times{|y|\n\t\t\tputs (x+1).to_s+\" \"+(f==1?y+2:m-y).to_s\n\t\t}\n\t\tf=-1*f\n\t}\n\tn.times{|x| puts (n-x).to_s+\" 1\"}\nelsif m%2==0\n\tputs 0\n\tputs \"1 1\"\n\tm.times{|x|\n\t\t(n-1).times{|y|\n\t\t\tputs (f==1?y+2:n-y).to_s+\" \"+(x+1).to_s\n\t\t}\n\t\tf=-1*f\n\t}\n\tm.times{|x| puts \"1 \"+(m-x).to_s}\nelse\n\tputs 1\n\tputs n.to_s+\" \"+m.to_s+\" 1 1\"\n\tn.times{|x|\n\t\tm.times{|y|\n\t\t\tputs (x+1).to_s+\" \"+(f==1?y+1:m-y).to_s\n\t\t}\n\t\tf=-1*f\n\t}\n\tputs \"1 1\"\nend"}, {"source_code": "n,m=gets.split.map(&:to_i)\nf,n,m=0,m,n if n%2==0\ns=[[i=1,j=1]]\nz = 0\nloop do\n while i < n\n s << [i+=1,j]\n end\n if m == j\n z = 1\n s.unshift [n,m,1,j=m]\n break\n end\n s << [i,j+=1]\n while i > 2\n s << [i-=1,j]\n end\n if m == j\n break\n end\n s << [i,j+=1]\nend\ns << [1,j] if i>1\nwhile j > 1\n s << [1,j-=1]\nend\np z\nif f\n s.map!{|i|i.each_slice(2).to_a.flat_map &:reverse}\nend\ns.each{|i|puts i*\" \"}\n"}, {"source_code": "n,m=gets.split.map(&:to_i)\nf,n,m=0,m,n if n%2==0\ns=[[i=1,j=1]]\nz = 0\nloop do\n while i < n\n s << [i+=1,j]\n end\n if m == j\n if n == 1\n s.unshift [1,m,1,1]\n s << [1,1]\n else\n s.unshift [n,m,1,j=m]\n if i > 1\n s << [1,j]\n while j > 1\n s << [1,j-=1]\n end\n end\n end\n z = 1\n break\n end\n s << [i,j+=1]\n while i > 2\n s << [i-=1,j]\n end\n if m == j\n if n == 1\n s.unshift [1,m,1,1]\n s << [1,1]\n else\n #s.unshift [n,m,1,j=m]\n if i > 1\n s << [1,j]\n while j > 1\n s << [1,j-=1]\n end\n end\n end\n z = 1\n break\n end\n s << [i,j+=1]\nend\np z\nif f\n s.map!{|i|i.each_slice(2).to_a.flat_map &:reverse}\nend\ns.each{|i|puts i*\" \"}\n"}, {"source_code": "n,m=gets.split.map(&:to_i)\nf,n,m=0,m,n if n%2==0\ns=[[i=1,j=1]]\nz = 0\nloop do\n while i < n\n s << [i+=1,j]\n end\n if m == j\n z = 1\n s.unshift [n,m,1,j=m]\n break\n end\n s << [i,j+=1]\n while i > 2\n s << [i-=1,j]\n end\n if m == j\n break\n end\n s << [i,j+=1]\nend\ns << [1,j]\nwhile j > 1\n s << [1,j-=1]\nend\np z\nif f\n s.map!{|i|i.each_slice(2).to_a.flat_map &:reverse}\nend\ns.each{|i|puts i*\" \"}\n"}, {"source_code": "n,m=gets.split.map(&:to_i)\nf,n,m=0,m,n if n%2==0\ns=[[i=1,j=1]]\nz = 0\nloop do\n while i < n\n s << [i+=1,j]\n end\n if m == j\n if n == 1\n z = 1\n s.unshift [1,m,1,1]\n s << [1,1]\n else\n z = 1\n s.unshift [n,m,1,j=m]\n if i > 1\n s << [1,j]\n while j > 1\n s << [1,j-=1]\n end\n end\n end\n z = 1\n break\n end\n s << [i,j+=1]\n while i > 2\n s << [i-=1,j]\n end\n if m == j\n if n == 1\n z = 1\n s.unshift [1,m,1,1]\n s << [1,1]\n else\n #s.unshift [n,m,1,j=m]\n if i > 1\n s << [1,j]\n while j > 1\n s << [1,j-=1]\n end\n end\n end\n z = 1\n break\n end\n s << [i,j+=1]\nend\np z\nif f\n s.map!{|i|i.each_slice(2).to_a.flat_map &:reverse}\nend\ns.each{|i|puts i*\" \"}\n"}, {"source_code": "n,m=gets.split.map(&:to_i)\nf,n,m=0,m,n if n%2==0\ns=[[i=1,j=1]]\nz = 0\nloop do\n while i < n\n s << [i+=1,j]\n end\n if m == j\n if n == 1\n s.unshift [1,m,1,1]\n s << [1,1]\n else\n s.unshift [n,m,1,j=m]\n if i > 1\n s << [1,j]\n while j > 1\n s << [1,j-=1]\n end\n end\n end\n z = 1\n break\n end\n s << [i,j+=1]\n while i > 2\n s << [i-=1,j]\n end\n if m == j\n if n == 1\n s.unshift [1,m,1,1]\n s << [1,1]\n else\n s.unshift [n,m,1,j=m]\n if i > 1\n s << [1,j]\n while j > 1\n s << [1,j-=1]\n end\n end\n end\n z = 1\n break\n end\n s << [i,j+=1]\nend\np z\nif f\n s.map!{|i|i.each_slice(2).to_a.flat_map &:reverse}\nend\ns.each{|i|puts i*\" \"}\n"}, {"source_code": "n,m=gets.split.map(&:to_i)\nf,n,m=0,m,n if n%2==0\ns=[[i=1,j=1]]\nz = 0\nloop do\n while i < n\n s << [i+=1,j]\n end\n if m == j\n if n == 1\n s.unshift [1,m,1,1]\n s << [1,1]\n else\n s.unshift [n,m,1,j=m]\n if i > 1\n s << [1,j]\n while j > 1\n s << [1,j-=1]\n end\n end\n end\n z = 1\n break\n end\n s << [i,j+=1]\n while i > 2\n s << [i-=1,j]\n end\n if m == j\n if n == 1\n z = 1\n s.unshift [1,m,1,1]\n s << [1,1]\n else\n #s.unshift [n,m,1,j=m]\n if i > 1\n s << [1,j]\n while j > 1\n s << [1,j-=1]\n end\n end\n end\n break\n end\n s << [i,j+=1]\nend\np z\nif f\n s.map!{|i|i.each_slice(2).to_a.flat_map &:reverse}\nend\ns.each{|i|puts i*\" \"}\n"}, {"source_code": "n,m=gets.split.map(&:to_i)\nf,n,m=0,m,n if n%2==0\ns=[[i=1,j=1]]\nz = 0\nloop do\n while i < n\n s << [i+=1,j]\n end\n if m == j\n z = 1\n s.unshift [m,n,j=m,1]\n break\n end\n s << [i,j+=1]\n while i > 2\n s << [i-=1,j]\n end\n if m == j\n break\n end\n s << [i,j+=1]\nend\ns << [1,j]\nwhile j > 1\n s << [1,j-=1]\nend\np z\nif f\n s.map!{|i|i.each_slice(2).to_a.flat_map &:reverse}\nend\ns.each{|i|puts i*\" \"}\n"}, {"source_code": "def generate_teleport_path(n, m)\n path = []\n columns = [ (1 .. m).to_a.reverse, (1 .. m).to_a ]\n (1 .. n).each do |row|\n index = row % 2\n columns[index].each { |col| path.push([row, col]) }\n end\n path.push([1, 1])\n return path\nend\n\ndef generate_path(n, m)\n if n % 2 == 1\n result = generate_path(m, n)\n return result.map { |v| v.reverse }\n end\n path = []\n (1 .. m).each { |col| path.push([1, col]) }\n columns = [ (2 .. m).to_a.reverse, (2 .. m).to_a ]\n (2 .. n).each do |row|\n index = row % 2\n columns[index].each { |col| path.push([row, col]) }\n end\n n.downto(1) { |row| path.push([row, 1]) }\n return path\nend\n\n\nn, m = gets.split.map { |v| v.to_i }\n\nteleports = []\n\nif n == 1 and m == 1\n path = [[1, 1], [1, 1]]\nelsif n % 2 == 1 and m % 2 == 1 or n == 1 or m == 1\n teleports = [[n, m, 1, 1]]\n path = generate_teleport_path(n, m)\nelse\n path = generate_path(n, m)\nend\n\nputs teleports.size\nteleports.each { |port| puts port.join(' ') }\npath.each { |pair| puts pair.join(' ') }\n"}], "src_uid": "a98622d5b6d6d139df90b6fee3baa544"} {"nl": {"description": "One day Polycarp published a funny picture in a social network making a poll about the color of his handle. Many of his friends started reposting Polycarp's joke to their news feed. Some of them reposted the reposts and so on.These events are given as a sequence of strings \"name1 reposted name2\", where name1 is the name of the person who reposted the joke, and name2 is the name of the person from whose news feed the joke was reposted. It is guaranteed that for each string \"name1 reposted name2\" user \"name1\" didn't have the joke in his feed yet, and \"name2\" already had it in his feed by the moment of repost. Polycarp was registered as \"Polycarp\" and initially the joke was only in his feed.Polycarp measures the popularity of the joke as the length of the largest repost chain. Print the popularity of Polycarp's joke.", "input_spec": "The first line of the input contains integer n (1 ≤ n ≤ 200) — the number of reposts. Next follow the reposts in the order they were made. Each of them is written on a single line and looks as \"name1 reposted name2\". All the names in the input consist of lowercase or uppercase English letters and/or digits and have lengths from 2 to 24 characters, inclusive. We know that the user names are case-insensitive, that is, two names that only differ in the letter case correspond to the same social network user.", "output_spec": "Print a single integer — the maximum length of a repost chain.", "sample_inputs": ["5\ntourist reposted Polycarp\nPetr reposted Tourist\nWJMZBMR reposted Petr\nsdya reposted wjmzbmr\nvepifanov reposted sdya", "6\nMike reposted Polycarp\nMax reposted Polycarp\nEveryOne reposted Polycarp\n111 reposted Polycarp\nVkCup reposted Polycarp\nCodeforces reposted Polycarp", "1\nSoMeStRaNgEgUe reposted PoLyCaRp"], "sample_outputs": ["6", "2", "2"], "notes": null}, "positive_code": [{"source_code": "@data = $stdin.read.split\nn = @data.shift.to_i\nresult = 0\nh = {}\n(@data.size / 3).times do\n\ta, rep, b = @data.shift, @data.shift, @data.shift\n\traise 'wrong data' unless rep == 'reposted'\n\th[b.downcase] ||= 1\n\th[a.downcase] = h[b.downcase] + 1\n\tresult = [result, h[a.downcase]].max\nend\nputs result"}, {"source_code": "class List\n attr_accessor :element, :next_elem\n\n def initialize(element, next_elem)\n @element = element\n @next_elem = next_elem\n end\nend\n\nclass Graph\n attr_accessor :vertices\n\n def initialize(vertices)\n @vertices = vertices\n end\n\n def add_edge(name1, name2)\n if vertices[name1].nil? then\n vertices[name1] = List.new(name2, nil)\n else\n vertices[name1] = List.new(name2, vertices[name1])\n end\n end\n\n def print_edges \n vertices.each do |name, vertex| \n # next if vertex == \"\"\n while !vertex.nil? do \n puts \"edge #{name} - #{vertex.element}\"\n vertex = vertex.next_elem \n end \n end \n end \n\n # def print_verteces\n # puts vertices.keys.join(\" \")\n # end\nend \n\ndef dfs(graph, name)\n current_vertex = graph.vertices[name]\n if current_vertex.nil?\n return 1\n else\n max = 1\n while !current_vertex.nil?\n max = [max, dfs(graph, current_vertex.element)].max\n current_vertex = current_vertex.next_elem\n end\n end\n return max + 1\nend\n\nreposts = Graph.new({})\nn = gets.strip.to_i\nn.times do |i|\n str = gets.strip.split(\" \")\n reposts.add_edge(str.last.downcase, str.first.downcase)\nend\n# reposts.print_edges\nputs dfs(reposts, \"polycarp\")\n\n"}, {"source_code": "rlevels = Hash.new\n\nrlevels[\"polycarp\"] = 1\n\nrepcount = gets.strip.to_i\n\nrepcount.times do |i|\n string = gets.strip.downcase.split(\" \")\n name1 = string[0]\n name2 = string[2]\n rl = rlevels[name2]\n rlevels[name1] = rl+1\nend\n\np rlevels.values.max"}, {"source_code": "h = Hash.new\n\nn = gets.to_i\n\nh['polycarp'] = 1\nans = 1\n\nn.times do |i|\n a, b, c = gets.split.map {|s| s.downcase }\n h[a] = h[c] + 1\n\n ans = [ans, h[a]].max\nend\n\nputs ans\n"}, {"source_code": "amount = gets.chomp.to_i\n$names = []\n\namount.times {\n\trepost = gets.chomp.split(' ')\n\t$names.push([repost[0].downcase, repost[2].downcase])\n}\n\n$max = 0\n\ndef dfs(name, length)\n\t$names.each { |x|\n\t\tif x[1] == name\n\t\t\tdfs(x[0], length+1)\n\t\tend\n\t}\n\n\tif length > $max\n\t\t$max = length\n\tend\nend\n\ndfs(\"polycarp\", 0)\n\nputs $max + 1"}, {"source_code": "gets\nw = {}\nwhile(ln = gets)\n dst, _, src = ln.downcase.split(' ')\n w[dst] = (w[src] || 1) + 1\nend\nputs w.values.max\n"}, {"source_code": "gets\nw = {}\nwhile(ln = gets)\n dst, src = ln.downcase.split(' ') - ['reposted']\n w[dst] = (w[src] || 1) + 1\nend\nputs w.values.max\n"}, {"source_code": "h=Hash.new(0)\n\ngets.to_i.times do |x|\n a=gets.chomp.split\n n1=a[0].downcase\n n2=a[2].downcase\n h[n2]=1 if x==0\n h[n1]=h[n2]+1\nend\n\np h.values.max\n"}, {"source_code": "n = gets.sub(\"\\n\",\"\").to_i\nh = {\"polycarp\"=>1}\nn.downto(1) do\n\ta,b = gets.sub(\"\\n\",\"\").split(\" reposted \").map{|i| i.downcase}\n\th[a] = h[b] + 1\nend\nmax = 0\nh.values.each{|i| max = i if(i>max)}\nputs max"}, {"source_code": "\n@inf = 1000000000\ne = gets.strip.to_i\n\ndepth = {}\ndepth['polycarp'] = 1\n\ne.times do |i|\n x, _, y = gets.strip.split.map(&:to_s).map(&:downcase)\n depth[x] = (depth[y] || 0) + 1\nend\n\nputs depth.values.max\n"}, {"source_code": "i = gets.to_i\nseq = 0\nreposts = Array.new()\nowners = Array.new().push(\"polycarp\")\nreposters = Array.new()\n1.upto(i) { reposts.push(gets.split(' ').map { |e| e.downcase } ) }\nwhile owners.count > 0\n\towners.each do |owner|\n\t\treposts.each do |repost|\n\t\t\treposters<< repost.first if repost.last == owner\n\t\tend\n\tend\n\towners = reposters.clone()\n\treposters.clear()\n\tseq+=1\nend\nputs seq"}], "negative_code": [], "src_uid": "16d4035b138137bbad247ccd5e560051"} {"nl": {"description": "A permutation is a sequence of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. Let's denote the i-th element of permutation p as pi. We'll call number n the size of permutation p1, p2, ..., pn.Nickolas adores permutations. He likes some permutations more than the others. He calls such permutations perfect. A perfect permutation is such permutation p that for any i (1 ≤ i ≤ n) (n is the permutation size) the following equations hold ppi = i and pi ≠ i. Nickolas asks you to print any perfect permutation of size n for the given n.", "input_spec": "A single line contains a single integer n (1 ≤ n ≤ 100) — the permutation size.", "output_spec": "If a perfect permutation of size n doesn't exist, print a single integer -1. Otherwise print n distinct integers from 1 to n, p1, p2, ..., pn — permutation p, that is perfect. Separate printed numbers by whitespaces.", "sample_inputs": ["1", "2", "4"], "sample_outputs": ["-1", "2 1", "2 1 4 3"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\nif n % 2 == 1\n puts -1\nelse\n puts (1..n).each_slice(2).map{|x,y| [y,x]}.inject(:+).map(&:to_s).join(' ')\nend"}, {"source_code": "sin = STDIN\nsout = STDOUT\n# sin = File.open(\"test.in\", \"r\")\n# sout = File.open(\"test.out\", \"w\")\n\nn = sin.readline.to_i\nsout.puts(n.even? ? (1..n).to_a.reverse.to_s.gsub(\",\", \"\")[1...-1] : -1)"}, {"source_code": "jom = gets.to_i \n\nif jom%2 == 1 \n print \"-1\"\nelse \n print \"2 1 \" \n jom.downto(3) { |n| print n.to_s + \" \" } \nend"}, {"source_code": "#!/usr/bin/ruby\nn=gets.to_i\nputs n%2==1 ? -1 : [*1..n].each_slice(2).map{|a,b|[b,a]}*' '"}, {"source_code": "n = gets.to_i\nputs n.odd? ? -1 : [*1..n].each_slice(2).map(&:reverse)\n"}, {"source_code": "n=gets.to_i\nputs n&1>0?-1:[*n.downto(1)]*\" \"\n"}, {"source_code": "n=gets.to_i\nif n%2!=0 \nputs -1\nelse\n\ta=(1..n).to_a\n\ti=0\n\twhile i0?-1:[*n.downto(1)]*\" \"\n"}, {"source_code": "def permute(n)\n return -1 if n % 2 == 1\n a = (1..n).to_a\n\n i = 0\n p = []\n while i < n-1\n p[i] = a[i+1]\n p[i+1] = a[i]\n i +=2\n end\n p.join(' ')\nend\n\nn = gets.to_i\nputs permute(n)"}, {"source_code": "n=gets.to_i\nputs n&1>0?-1:[*n.downto(1)]*\" \""}, {"source_code": "n=gets.to_i\na=[0]\nif n&1==1\n\tputs \"-1\"\n\texit\nend\nfor i in 1..n\n\ta.push(i)\nend\nfor k in 1..(n/2)+2\n\tif 2*(k)<=n\n\t\ta[2*k],a[(2*k)-1]=a[(2*k)-1],a[2*k]\n\tend\nend\nfor i in 1..n\n\tprint a[i].to_s+' '\nend\nputs"}, {"source_code": "n = gets.to_i\nif n.odd?\n puts (-1)\n exit\nelse\n a = (1..n).to_a\n (0...n).select { |x| x.even? }.map { |x| a[x], a[x+1] = a[x+1], a[x]}\n puts a*' '\nend"}, {"source_code": "n = gets.to_i\nputs n & 1 > 0 ? -1 : [*n.downto(1)] * ' '"}, {"source_code": "n=gets.to_i\nputs n&1>0?-1:[*n.downto(1)]*\" \"\n"}, {"source_code": "a=gets.chomp.to_i\nif a%2==1\nputs \"-1\"\nelse\na.downto(1) do |i|\nprintf\"#{i} \"\nend\nend\n "}, {"source_code": "n = gets.to_i\nif n % 2 == 1 then\n puts -1\nelse\n (n / 2).times do |i|\n print (2 * (i + 1)).to_s + \" \" + (2 * (i + 1) - 1).to_s \n if i == n - 1 then print \"\\n\" else print \" \" end\n end\nend"}, {"source_code": "n = gets.to_i\n\nif n.odd?\n puts -1\nelse\n ans = []\n (1..n).each do |i|\n if i.odd?\n ans << i + 1\n else\n ans << i - 1\n end\n end\n puts ans.join(\" \")\nend"}, {"source_code": "n=gets.to_i\nputs n&1>0?-1:[*n.downto(1)]*\" \"\n"}, {"source_code": "n = gets.chomp.to_i\nif n.even?\n array = (1..n).to_a\n index = 0\n while index < n\n array[index], array[index + 1] = array[index + 1], array[index]\n index += 2\n end\n puts array.join(\" \")\nelse\n puts \"-1\"\nend\n"}, {"source_code": "a=gets.chomp.to_i\nif a%2==1\nputs \"-1\"\nelse\na.downto(1) do |i|\nprintf\"#{i} \"\nend\nend"}, {"source_code": "n=gets.to_i\nif n%2==1 then\n\tputs -1\nelse\n\tarr=[]\n\t2.step(n,2){|i|\n\t\tarr<<(i)\n\t\tarr<<(i-1)\n\t}\n\tputs arr*\" \"\nend\n\n"}, {"source_code": "n = gets.to_i\nif n % 2 == 1\n puts \"-1\"\n exit\nend\na = -1\nn.times do |i|\n if i % 2 == 0\n a += 3\n else\n a -= 1\n end\n print a\n if n != i-1\n print \" \"\n else\n print \"\\n\"\n end\nend"}, {"source_code": "n=gets.to_i\nputs n&1>0?-1:[*n.downto(1)]*\" \""}, {"source_code": "require 'set'\nrequire 'rational'\nrequire 'mathn'\ninclude Math\n$stdin = File.open(\"./input.txt\", \"r\") if ENV[\"USER\"] == \"new\"\n# ====================================\n\nn = gets.to_i\n\nif n%2!=0 then\n print -1\n exit \nend\n\nn.downto(1) do |index|\n print index,\" \"\nend"}, {"source_code": "a=1\nn=gets.to_i\nhalf=n/2\n\nif n==2\n\tputs '2 1'\nelsif n%2==0\n\thalf.times{|i|\n\tprint a+1,' ',a\n\tif i!=(half-1) then\n\t\tprint ' '\n\telse\n\t\tprint \"\\n\"\n\tend\n\ta+=2}\nelse\n\tputs '-1'\nend\n"}, {"source_code": "n=gets.to_i;puts [[*1..n].reverse*' ',-1][n%2]"}, {"source_code": "n = gets.to_i\nif n%2==1\n\tp -1\n\texit\nend\n(n/2).times do |x|\n\tprint \"#{2*x+2} #{2*x+1} \"\nend\n"}, {"source_code": "n=gets.to_i;puts [[*1..n].reverse*' ',-1][n%2]\n"}, {"source_code": "n=gets.to_i\nputs n&1>0?-1:[*n.downto(1)]*\" \"\n"}, {"source_code": "n = gets.to_i\nif n.odd?\n\tputs -1\nelse\n\tp = (1..n).each_slice(2).map{|a| [a[1],a[0]]}.flatten\n\tputs p.join(\" \")\nend"}, {"source_code": "n=gets.to_i\nputs n%2==1 ?\"-1\":[*n.downto(1)]*\" \""}, {"source_code": "l = (1..gets.to_i).to_a\ni = 1\nwhile i < l.size\n\tl[i - 1], l[i] = l[i], l[i - 1]\n\ti += 2\nend\n\nif l.each_with_index.all? { |x, i| l[x - 1] == i + 1 and x != i + 1 }\n\tputs l.join(\" \")\nelse\n\tputs -1\nend"}, {"source_code": "n = gets.to_i\nif n%2==0\n puts (1..n).to_a.select{|x| x%2!=0}.map{|x| [x+1,x]}.flatten.join(' ')\nelse\n p -1\nend"}, {"source_code": "n=gets.to_i;puts [[*1..n].reverse*' ',-1][n%2]"}, {"source_code": "a=gets.chomp.to_i\nif a%2==1\nputs \"-1\"\nelse\na.downto(1) do |i|\nprintf\"#{i} \"\nend\nend"}, {"source_code": "n=gets.to_i\nputs n&1>0?-1:[*n.downto(1)]*\" \""}, {"source_code": "n = gets.to_i\nif n%2 == 0\n (n/2).times {|x| print x*2 + 2, ' ', x*2 + 1, ' '}\nelse\n puts -1\nend\n"}, {"source_code": "#!/usr/bin/env ruby\n$stdin.readlines.each do |line|\n n = line.strip.to_i\n if n % 2 == 0\n nums = (1..n).to_a\n i = 0\n while i < n-1\n t = nums[i]\n nums[i] = nums[i+1]\n nums[i+1] = t\n i += 2\n end\n puts nums.join(' ')\n else\n puts '-1'\n end\nend\n"}, {"source_code": "n = gets.to_i\nif n % 2 == 0\n (1..n/2).each { |i|\n print 2*i, ' ', 2*i-1, ' '\n }\nelse\n puts -1\nend\n"}, {"source_code": "n = gets.to_i\nif (n%2==1)\n puts -1\n exit\nend\n(n/2).times do |i|\n print \"#{2*(i+1)} #{2*(i+1)-1} \"\nend"}, {"source_code": "n=gets.to_i\nputs n&1>0?-1:[*n.downto(1)]*\" \"\n"}, {"source_code": "n = Integer(gets.chomp)\nif n.even?\n a = []\n for i in (1..n).step(2)\n a << i + 1 << i\n end\n puts(a * ' ')\nelse\n puts -1\nend"}, {"source_code": "n=gets.to_i\nputs n&1>0?-1:[*n.downto(1)]*\" \"\n"}, {"source_code": "n=gets.to_i\nputs n&1>0?-1:[*n.downto(1)]*\" \""}, {"source_code": "n = gets.to_i\n\nif n.odd?\n\tputs -1\nelse\n\tp = Array.new(n) { |i, v| v = i + 1 }\n\t(n / 2).times do |i|\n\t\tp[i * 2], p[i * 2 + 1] = p[i * 2 + 1], p[i * 2]\n\tend\n\tputs p.join ' '\nend"}, {"source_code": "n=gets.to_i\nputs n&1>0?-1:[*n.downto(1)]*\" \"\n"}], "negative_code": [{"source_code": "jom = gets.to_i \n\nif jom == 1 or jom == 3 \n puts \"-1\"\nelse \n print \"2 1 \" \n jom.downto(3) { |n| print n.to_s + \" \" } \nend "}, {"source_code": "def main(n)\n return [-1] if n == 1\n \n ans = []\n if n % 2 == 0 \n 1.step(n, 2) do |i|\n ans << i + 1\n ans << i\n end \n else\n 1.step(n, 3) do |i|\n ans << i + 1\n ans << i + 2\n ans << i\n end\n end\n \n return ans\nend \n \nn = gets.chomp.to_i\nans = main(n)\nputs ans.join(\" \")\n\n"}, {"source_code": "def permute(n)\n return -1 if n == 1\n a = (1..n).to_a\n # Swapping elements with their consecutives\n (1..n-1).each {|i| t = a[i-1]; a[i-1] = a[i]; a[i] = t}\n a.join(' ')\nend\n\nn = gets.to_i\nputs permute(n)"}, {"source_code": "def permute(n)\n return -1 if n == 1\n a = (1..n).to_a\n\n i = 0\n p = []\n while i < n-1\n p[i] = a[i+1]\n p[i+1] = a[i]\n i +=2\n end\n p.join(' ')\nend\n\nn = gets.to_i\nputs permute(n)"}, {"source_code": "n = gets.chomp.to_i\nif n > 1\n array = (1..n).to_a\n index = 0\n while index < n\n array[index], array[index + 1] = array[index + 1], array[index]\n index += 2\n end\n puts array.join(\" \")\nelse\n puts \"-1\"\nend\n"}, {"source_code": "n=gets.to_i\nif n==1 then\n\tputs -1\nelse\n\tputs (1..(n-1)).to_a.unshift(n)*\" \"\nend\n\n"}, {"source_code": "n=gets.to_i\nhalf=n/2\n\nif n==1 then\n\tputs '-1'\nelsif n==2\n\tputs '2 1'\nelse\n\thalf.downto(1){|i|\n\tprint i,' '\n\t}\n\t\n\t(n).downto(half+1){|i|\n\tprint i\n\tprint (if i!=(half+1) then\n\t\t' ' \n\telse\n\t\t\"\\n\"\n\tend)\n\t}\nend\n"}, {"source_code": "n=gets.to_i\nhalf=n/2\n\nif n==2\n\tputs '2 1'\nelsif n%2==0\n\thalf.downto(1){|i|\n\tprint i,' '\n\t}\n\t\n\t(n).downto(half+1){|i|\n\tprint i\n\tprint (if i!=(half+1) then\n\t\t' ' \n\telse\n\t\t\"\\n\"\n\tend)\n\t}\nelse\n\tputs '-1'\nend\n"}], "src_uid": "204ba74195a384c59fb1357bdd71e16c"} {"nl": {"description": "Every Codeforces user has rating, described with one integer, possibly negative or zero. Users are divided into two divisions. The first division is for users with rating 1900 or higher. Those with rating 1899 or lower belong to the second division. In every contest, according to one's performance, his or her rating changes by some value, possibly negative or zero.Limak competed in n contests in the year 2016. He remembers that in the i-th contest he competed in the division di (i.e. he belonged to this division just before the start of this contest) and his rating changed by ci just after the contest. Note that negative ci denotes the loss of rating.What is the maximum possible rating Limak can have right now, after all n contests? If his rating may be arbitrarily big, print \"Infinity\". If there is no scenario matching the given information, print \"Impossible\".", "input_spec": "The first line of the input contains a single integer n (1 ≤ n ≤ 200 000). The i-th of next n lines contains two integers ci and di ( - 100 ≤ ci ≤ 100, 1 ≤ di ≤ 2), describing Limak's rating change after the i-th contest and his division during the i-th contest contest.", "output_spec": "If Limak's current rating can be arbitrarily big, print \"Infinity\" (without quotes). If the situation is impossible, print \"Impossible\" (without quotes). Otherwise print one integer, denoting the maximum possible value of Limak's current rating, i.e. rating after the n contests.", "sample_inputs": ["3\n-7 1\n5 2\n8 2", "2\n57 1\n22 2", "1\n-5 1", "4\n27 2\n13 1\n-50 1\n8 2"], "sample_outputs": ["1907", "Impossible", "Infinity", "1897"], "notes": "NoteIn the first sample, the following scenario matches all information Limak remembers and has maximum possible final rating: Limak has rating 1901 and belongs to the division 1 in the first contest. His rating decreases by 7. With rating 1894 Limak is in the division 2. His rating increases by 5. Limak has rating 1899 and is still in the division 2. In the last contest of the year he gets  + 8 and ends the year with rating 1907. In the second sample, it's impossible that Limak is in the division 1, his rating increases by 57 and after that Limak is in the division 2 in the second contest."}, "positive_code": [{"source_code": "#!/usr/bin/ruby\nma=Float::INFINITY\nmi=-ma\ns=0\ngets.to_i.times{\n\tc,d=gets.split.map &:to_i\n\tif d==1\n\t\tmi=[mi,1900-s].max\n\telse\n\t\tma=[ma,1899-s].min\n\tend\n\ts+=c\n}\nputs ma==Float::INFINITY ? :Infinity : ma>=mi ? ma+s : :Impossible"}, {"source_code": "n = gets.to_i\na = n.times.map{gets.split.map(&:to_i)}\n\nd1acc = 0\ncurrent = nil\nans = nil\na.each_with_index do |x, i|\n change, div = x\n if div == 1\n d1acc += change\n elsif div == 2\n ans = 'Impossible' if i > 0 && a[i - 1][1] == 1 && d1acc >= 0\n d1acc = 0\n current = 1899 unless current\n end\n if current\n current += change\n if current < 1900 && i < n - 1 && a[i + 1][1] == 1\n current = 1900\n elsif current >= 1900 && i < n - 1 && a[i + 1][1] == 2\n current = 1899\n end\n end\nend\nif current\n try = current\n a.reverse.each do |change, div|\n try -= change\n ans = 'Impossible' if try < 1900 && div != 2 || try >= 1900 && div != 1\n end\nelse\n ans = 'Infinity'\nend\nputs ans || current\n"}, {"source_code": "def solve(a)\n current = nil\n a.each do |change, div|\n current ||= 1899 if div == 2\n if current\n current = 1900 if div == 1 && current < 1900\n current = 1899 if div == 2 && current >= 1900\n current += change\n end\n end\n\n if current\n a.reverse_each.inject(current) do |try, (change, div)|\n try -= change\n return 'Impossible' if div == 1 && try < 1900 || div == 2 && try >= 1900\n try\n end\n else\n return 'Infinity'\n end\n\n return current\nend\n\nn = gets.to_i\na = n.times.map{gets.split.map(&:to_i)}\n\nputs solve(a)\n"}], "negative_code": [], "src_uid": "2a4c24341231cabad6021697f15d953a"} {"nl": {"description": "A competitive eater, Alice is scheduling some practices for an eating contest on a magical calendar. The calendar is unusual because a week contains not necessarily $$$7$$$ days!In detail, she can choose any integer $$$k$$$ which satisfies $$$1 \\leq k \\leq r$$$, and set $$$k$$$ days as the number of days in a week.Alice is going to paint some $$$n$$$ consecutive days on this calendar. On this calendar, dates are written from the left cell to the right cell in a week. If a date reaches the last day of a week, the next day's cell is the leftmost cell in the next (under) row.She wants to make all of the painted cells to be connected by side. It means, that for any two painted cells there should exist at least one sequence of painted cells, started in one of these cells, and ended in another, such that any two consecutive cells in this sequence are connected by side.Alice is considering the shape of the painted cells. Two shapes are the same if there exists a way to make them exactly overlapped using only parallel moves, parallel to the calendar's sides.For example, in the picture, a week has $$$4$$$ days and Alice paints $$$5$$$ consecutive days. [1] and [2] are different shapes, but [1] and [3] are equal shapes. Alice wants to know how many possible shapes exists if she set how many days a week has and choose consecutive $$$n$$$ days and paints them in calendar started in one of the days of the week. As was said before, she considers only shapes, there all cells are connected by side.", "input_spec": "The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. For each test case, the only line contains two integers $$$n$$$, $$$r$$$ ($$$1 \\le n \\le 10^9, 1 \\le r \\le 10^9$$$).", "output_spec": "For each test case, print a single integer  — the answer to the problem. Please note, that the answer for some test cases won't fit into $$$32$$$-bit integer type, so you should use at least $$$64$$$-bit integer type in your programming language.", "sample_inputs": ["5\n3 4\n3 2\n3 1\n13 7\n1010000 9999999"], "sample_outputs": ["4\n3\n1\n28\n510049495001"], "notes": "NoteIn the first test case, Alice can set $$$1,2,3$$$ or $$$4$$$ days as the number of days in a week.There are $$$6$$$ possible paintings shown in the picture, but there are only $$$4$$$ different shapes. So, the answer is $$$4$$$. Notice that the last example in the picture is an invalid painting because all cells are not connected by sides. In the last test case, be careful with the overflow issue, described in the output format."}, "positive_code": [{"source_code": "t = gets.to_i\nt.times do\n\tn, r = gets.split.map &:to_i\n\tx = 0\n\tk = [r, n-1].min\n\tx += [0, (k+1)*k/2].max\n\tx += 1 if r >= n\n\tp x\nend\n"}, {"source_code": "require 'set'; require 'prime'; require 'pp'\nINF=Float::INFINITY; MOD=10**9+7\nt = gets.chomp.to_i\nt.times do\n n,r = gets.chomp.split.map(&:to_i)\n if n > r\n num = r\n puts num * (num+1) / 2\n else\n num = n - 1\n puts num * (num+1) / 2 + 1\n end\nend\n#####\n"}, {"source_code": "require 'set'; require 'prime'; require 'pp'\nINF=Float::INFINITY; MOD=10**9+7\nt = gets.chomp.to_i\nt.times do\n n,r = gets.chomp.split.map(&:to_i)\n if n > r\n num = r\n puts num * (num+1) / 2\n else\n num = n - 1\n puts num * (num+1) / 2 + 1\n end\nend\n####\n"}, {"source_code": "require 'set'; require 'prime'; require 'pp'\nINF=Float::INFINITY; MOD=10**9+7\nt = gets.chomp.to_i\nt.times do\n n,r = gets.chomp.split.map(&:to_i)\n if n > r\n num = r\n puts num * (num+1) / 2\n else\n num = n - 1\n puts num * (num+1) / 2 + 1\n end\nend\n#######\n"}, {"source_code": "require 'set'; require 'prime'; require 'pp'\nINF=Float::INFINITY; MOD=10**9+7\nt = gets.chomp.to_i\nt.times do\n n,r = gets.chomp.split.map(&:to_i)\n if n > r\n num = r\n puts num * (num+1) / 2\n else\n num = n - 1\n puts num * (num+1) / 2 + 1\n end\nend\n"}, {"source_code": "require 'set'; require 'prime'; require 'pp'\nINF=Float::INFINITY; MOD=10**9+7\nt = gets.chomp.to_i\nt.times do\n n,r = gets.chomp.split.map(&:to_i)\n if n > r\n num = r\n puts num * (num+1) / 2\n else\n num = n - 1\n puts num * (num+1) / 2 + 1\n end\nend\n##\n"}, {"source_code": "require 'set'; require 'prime'; require 'pp'\nINF=Float::INFINITY; MOD=10**9+7\nt = gets.chomp.to_i\nt.times do\n n,r = gets.chomp.split.map(&:to_i)\n if n > r\n num = r\n puts num * (num+1) / 2\n else\n num = n - 1\n puts num * (num+1) / 2 + 1\n end\nend\n#\n"}, {"source_code": "n = gets.chomp.to_i\nn.times do \n n, r = gets.chomp.split(\" \").map(&:to_i)\n if n > r\n ans = r * (1 + r) \n puts ans / 2\n elsif n <= r\n ans = (n-1) * n\n puts ans / 2 + 1\n end\nend"}, {"source_code": "# this code is just to observe speed\nt = gets.chomp.to_i\na = Array.new(t)\nfor i in 0..t-1 do\n n,r = gets.chomp.split.map(&:to_i)\n if n > r\n num = r\n a[i] = num * (num+1) / 2\n else\n num = n - 1\n a[i] = num * (num+1) / 2 + 1\n end\nend\n\nputs a.join(\"\\n\")"}], "negative_code": [], "src_uid": "eb3d8259ca598c3c455ddfdbe433cb78"} {"nl": {"description": "Malek has recently found a treasure map. While he was looking for a treasure he found a locked door. There was a string s written on the door consisting of characters '(', ')' and '#'. Below there was a manual on how to open the door. After spending a long time Malek managed to decode the manual and found out that the goal is to replace each '#' with one or more ')' characters so that the final string becomes beautiful. Below there was also written that a string is called beautiful if for each i (1 ≤ i ≤ |s|) there are no more ')' characters than '(' characters among the first i characters of s and also the total number of '(' characters is equal to the total number of ')' characters. Help Malek open the door by telling him for each '#' character how many ')' characters he must replace it with.", "input_spec": "The first line of the input contains a string s (1 ≤ |s| ≤ 105). Each character of this string is one of the characters '(', ')' or '#'. It is guaranteed that s contains at least one '#' character.", "output_spec": "If there is no way of replacing '#' characters which leads to a beautiful string print  - 1. Otherwise for each character '#' print a separate line containing a positive integer, the number of ')' characters this character must be replaced with. If there are several possible answers, you may output any of them.", "sample_inputs": ["(((#)((#)", "()((#((#(#()", "#", "(#)"], "sample_outputs": ["1\n2", "2\n2\n1", "-1", "-1"], "notes": "Note|s| denotes the length of the string s."}, "positive_code": [{"source_code": "s = STDIN.readline.strip\n\ndef fail\n puts -1\n exit\nend\n\nresult = []\nsum = 0\ns.each_char do |ch|\n if ch == '('\n sum += 1\n elsif ch == ')'\n sum -= 1\n fail if sum < 0\n else\n result.push(1)\n sum -= 1\n fail if sum < 0\n end\nend\nresult[-1] += sum\n\npos = s.index('(', s.rindex('#'))\nif pos != nil\n tail_sum = 0\n s[pos..-1].each_char do |ch|\n if ch == '('\n tail_sum += 1\n else\n tail_sum -= 1\n end\n end\n fail if tail_sum > 0\nend\n\nputs result.join(\"\\n\")\n"}, {"source_code": "def solve(s)\n to = s.count {|c| c == '('}\n tc = s.count {|c| c == ')'}\n tp = s.count {|c| c == '#'}\n co = 0\n cc = 0\n cp = 0\n res = []\n i = 0\n r = tc\n s.each do |c|\n if c == '#'\n cp += 1\n if cp < tp\n res << 1\n cc += 1\n else\n if cc >= to\n return nil\n end\n diff = to - cc - r\n return nil if diff == 0\n res << diff\n cc += diff\n end\n elsif c == '('\n co += 1\n elsif c == ')'\n cc += 1\n r -= 1\n end\n if cc > co\n #puts \"#{i}: #{co} #{cc}\"\n return nil\n end\n i += 1\n end\n return res\nend\ns = STDIN.gets.chomp.split(//)\nres = solve(s)\nif not res\n puts \"-1\"\nelse\n res.each do |r|\n puts r\n end\nend\n"}], "negative_code": [], "src_uid": "0a30830361b26838b192d7de1efcdd2f"} {"nl": {"description": "This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.The main element of this machine are $$$n$$$ rods arranged along one straight line and numbered from $$$1$$$ to $$$n$$$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $$$1$$$ or $$$-1$$$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.More formally, the rods can be represented as an array of $$$n$$$ numbers characterizing the charge: either $$$1$$$ or $$$-1$$$. Then the condition must hold: $$$a_1 - a_2 + a_3 - a_4 + \\ldots = 0$$$, or $$$\\sum\\limits_{i=1}^n (-1)^{i-1} \\cdot a_i = 0$$$.Sparky charged all $$$n$$$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $$$q$$$ questions. In the $$$i$$$th question Sparky asks: if the machine consisted only of rods with numbers $$$l_i$$$ to $$$r_i$$$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.Help your friends and answer all of Sparky's questions!", "input_spec": "Each test contains multiple test cases. The first line contains one positive integer $$$t$$$ ($$$1 \\le t \\le 10^3$$$), denoting the number of test cases. Description of the test cases follows. The first line of each test case contains two positive integers $$$n$$$ and $$$q$$$ ($$$1 \\le n, q \\le 3 \\cdot 10^5$$$) — the number of rods and the number of questions. The second line of each test case contains a non-empty string $$$s$$$ of length $$$n$$$, where the charge of the $$$i$$$-th rod is $$$1$$$ if $$$s_i$$$ is the \"+\" symbol, or $$$-1$$$ if $$$s_i$$$ is the \"-\" symbol. Each next line from the next $$$q$$$ lines contains two positive integers $$$l_i$$$ ans $$$r_i$$$ ($$$1 \\le l_i \\le r_i \\le n$$$) — numbers, describing Sparky's questions. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 \\cdot 10^5$$$, and the sum of $$$q$$$ over all test cases does not exceed $$$3 \\cdot 10^5$$$.", "output_spec": "For each test case, print a single integer — the minimal number of rods that can be removed.", "sample_inputs": ["3\n14 1\n+--++---++-++-\n1 14\n14 3\n+--++---+++---\n1 14\n6 12\n3 10\n4 10\n+-+-\n1 1\n1 2\n1 3\n1 4\n2 2\n2 3\n2 4\n3 3\n3 4\n4 4"], "sample_outputs": ["2\n2\n1\n0\n1\n2\n1\n2\n1\n2\n1\n1\n2\n1"], "notes": "NoteIn the first test case for the first query you can remove the rods numbered $$$5$$$ and $$$8$$$, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.In the second test case: For the first query, we can remove the rods numbered $$$1$$$ and $$$11$$$, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero. For the second query we can remove the rod numbered $$$9$$$, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero. For the third query we can not remove the rods at all. "}, "positive_code": [{"source_code": "t = gets.to_i\nt.times do\n\nn, q = gets.split.map!(&:to_i)\ns = gets.chomp.chars.map! { |c| c == \"+\" ? 1 : -1 }\n1.step(n-1, 2) { |i| s[i] = -s[i] }\n\nx = 0\ncs = s.map { |si| x += si }.unshift(0)\n\nq.times do\n l, r = gets.split.map!(&:to_i)\n l -= 1\n diff = cs[r] - cs[l]\n\n if diff == 0\n puts 0\n next\n end\n\n if (r - l).even?\n puts 2\n else\n puts 1\n end\nend\n\nend\n"}], "negative_code": [], "src_uid": "3f7f29e57cc03be8ff1251ab42738739"} {"nl": {"description": "Not so long ago, Vlad came up with an interesting function: $$$f_a(x)=\\left\\lfloor\\frac{x}{a}\\right\\rfloor + x \\bmod a$$$, where $$$\\left\\lfloor\\frac{x}{a}\\right\\rfloor$$$ is $$$\\frac{x}{a}$$$, rounded down, $$$x \\bmod a$$$ — the remainder of the integer division of $$$x$$$ by $$$a$$$.For example, with $$$a=3$$$ and $$$x=11$$$, the value $$$f_3(11) = \\left\\lfloor\\frac{11}{3}\\right\\rfloor + 11 \\bmod 3 = 3 + 2 = 5$$$.The number $$$a$$$ is fixed and known to Vlad. Help Vlad find the maximum value of $$$f_a(x)$$$ if $$$x$$$ can take any integer value from $$$l$$$ to $$$r$$$ inclusive ($$$l \\le x \\le r$$$).", "input_spec": "The first line of input data contains an integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of input test cases. This is followed by $$$t$$$ lines, each of which contains three integers $$$l_i$$$, $$$r_i$$$ and $$$a_i$$$ ($$$1 \\le l_i \\le r_i \\le 10^9, 1 \\le a_i \\le 10^9$$$) — the left and right boundaries of the segment and the fixed value of $$$a$$$.", "output_spec": "For each test case, output one number on a separate line — the maximum value of the function on a given segment for a given $$$a$$$.", "sample_inputs": ["5\n\n1 4 3\n\n5 8 4\n\n6 10 6\n\n1 1000000000 1000000000\n\n10 12 8"], "sample_outputs": ["2\n4\n5\n999999999\n5"], "notes": "NoteIn the first sample: $$$f_3(1) = \\left\\lfloor\\frac{1}{3}\\right\\rfloor + 1 \\bmod 3 = 0 + 1 = 1$$$, $$$f_3(2) = \\left\\lfloor\\frac{2}{3}\\right\\rfloor + 2 \\bmod 3 = 0 + 2 = 2$$$, $$$f_3(3) = \\left\\lfloor\\frac{3}{3}\\right\\rfloor + 3 \\bmod 3 = 1 + 0 = 1$$$, $$$f_3(4) = \\left\\lfloor\\frac{4}{3}\\right\\rfloor + 4 \\bmod 3 = 1 + 1 = 2$$$ As an answer, obviously, $$$f_3(2)$$$ and $$$f_3(4)$$$ are suitable."}, "positive_code": [{"source_code": "n = STDIN.gets.chomp.to_i\r\nn.times.each do\r\n l,r,a = STDIN.gets.chomp.split(\" \").map(&:to_i)\r\n puts [r/a+r%a,(r-r%a-1)/a+(r-r%a-1)%a].max if r-r%a-1>=l\r\n puts r/a+r%a if r-r%a-1 f(y,a)}.drop(1).sort!\r\n #puts(\" xxx #{func} xxx\")\r\n fl=f(l,a)\r\n fm=f(m,a)\r\n fr=f(r,a)\r\n\r\n if fm>fl\r\n return process(m,r,a)\r\n elsif fm>fr\r\n return process(l,m,a)\r\n elsif fl>fr\r\n return process(l,m,a)\r\n else\r\n return process(m,r,a)\r\n\r\n end\r\n\r\n\r\n #if f(m,a)>=f(l,a)\r\n#\r\n # return process(m,r,a)\r\n #elsif f(m,a)>=f(r,a)\r\n#\r\n # return process(l,m,a)\r\n #else\r\n # return process(m,r,a)\r\n #end\r\nend\r\n\r\nn = STDIN.gets.chomp.to_i\r\nn.times.each do\r\n l,r,a = STDIN.gets.chomp.split(\" \").map(&:to_i)\r\n #puts \"#### #{l} #{r} #{a} ####\"\r\n puts(\"#{process(l,r,a)}\")\r\nend\r\n\r\n\r\n\r\n#n = STDIN.gets.chomp.to_i\r\n#n.times.each do\r\n# l,r,a = STDIN.gets.chomp.split(\" \").map(&:to_i)\r\n#\r\n# puts ([r-(r%a)-1,l].max ... r+1).to_a.map { |value| (value/a) + value%a }.max\r\n#end\r\n\r\n#352038877 421606206 705339697\r\n#\r\n#l=352038877\r\n#r=421606206\r\n#a=705339697\r\n#\r\n#puts([r-a-1,l].max ... r+1)\r\n#puts(\"count : #{([r-a-1,l].max ... r+1).count}\\n\")\r\n#puts( \"r%a : #{r%a}\")\r\n#\r\n#([r-(r%a)-1,l].max ... r+1).to_a.each { |x|\r\n# puts \"#{x}/#{a} + #{x}%#{a} #{x/a} #{x%a} = #{x/a+x%a}\"\r\n#}\r\n#\r\n\r\n\r\n#r-(r/a)-1\r\n#12-(1)-1\r\n#a=8\r\n#l=0\r\n#(1...12).to_a.each do |i|\r\n# x=l+i\r\n#\r\n# puts \"#{x}/#{a} + #{x}%#{a} #{x/a} #{x%a} = #{x/a+x%a}\"\r\n#end\r\n#\r\n#a=10\r\n#l=90\r\n#r=110\r\n#x=r\r\n#while x % a != a-1 and x >= l\r\n# x-=1\r\n#end\r\n#puts x\r\n\r\n\r\n\r\n\r\n#l=5\r\n#r=8\r\n#a=4\r\n##x=r\r\n#puts ([r-(r/a),l].max ... r+1).to_a.map { |value| (value/a) + value%a }.max\r\n\r\n# l 5\r\n# r 8\r\n# a 4\r\n# x 7\r\n#\r\n# 4 = 7/4 + 7%4\r\n#\r\n# 10/2 + 10%2 5 0 = 0\r\n# 11/2 + 11%2 5 1 = 1\r\n#\r\n#\r\n#\r\n#\r\n# x/a + x%a = Y"}, {"source_code": "def f(x,a)\r\n return(x/a + x % a)\r\nend\r\n\r\ndef process(l, r, a)\r\n\r\n return [f(l, a),f(r,a)].max if r-l<=1\r\n m = (l + r ) / 2\r\n\r\n if f(m,a)>=f(l,a)\r\n\r\n return process(m,r,a)\r\n elsif f(m,a)>=f(r,a)\r\n\r\n return process(l,m,a)\r\n else\r\n return process(m,r,a)\r\n end\r\nend\r\n\r\nn = STDIN.gets.chomp.to_i\r\nn.times.each do\r\n l,r,a = STDIN.gets.chomp.split(\" \").map(&:to_i)\r\n\r\n puts(process(l,r,a))\r\nend"}, {"source_code": "n = STDIN.gets.chomp.to_i\r\nn.times.each do\r\n l,r,a = STDIN.gets.chomp.split(\" \").map(&:to_i)\r\n puts ([a-l-1,l].max ... r+1).map { |value| (value/a) + value%a }.max\r\nend\r\n"}, {"source_code": "n = STDIN.gets.chomp.to_i\r\nn.times.each do\r\n l,r,a = STDIN.gets.chomp.split(\" \").map(&:to_i)\r\n puts (a-l ... r+1).map { |value| (value/a) + value%a }.max\r\nend\r\n\r\n"}], "src_uid": "681ee82880ddd0de907aac2ccad8fc04"} {"nl": {"description": "Polycarp had an array $$$a$$$ of $$$3$$$ positive integers. He wrote out the sums of all non-empty subsequences of this array, sorted them in non-decreasing order, and got an array $$$b$$$ of $$$7$$$ integers.For example, if $$$a = \\{1, 4, 3\\}$$$, then Polycarp wrote out $$$1$$$, $$$4$$$, $$$3$$$, $$$1 + 4 = 5$$$, $$$1 + 3 = 4$$$, $$$4 + 3 = 7$$$, $$$1 + 4 + 3 = 8$$$. After sorting, he got an array $$$b = \\{1, 3, 4, 4, 5, 7, 8\\}.$$$Unfortunately, Polycarp lost the array $$$a$$$. He only has the array $$$b$$$ left. Help him to restore the array $$$a$$$.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 5000$$$) — the number of test cases. Each test case consists of one line which contains $$$7$$$ integers $$$b_1, b_2, \\dots, b_7$$$ ($$$1 \\le b_i \\le 10^9$$$; $$$b_i \\le b_{i+1}$$$). Additional constraint on the input: there exists at least one array $$$a$$$ which yields this array $$$b$$$ as described in the statement.", "output_spec": "For each test case, print $$$3$$$ integers — $$$a_1$$$, $$$a_2$$$ and $$$a_3$$$. If there can be several answers, print any of them.", "sample_inputs": ["5\n1 3 4 4 5 7 8\n1 2 3 4 5 6 7\n300000000 300000000 300000000 600000000 600000000 600000000 900000000\n1 1 2 999999998 999999999 999999999 1000000000\n1 2 2 3 3 4 5"], "sample_outputs": ["1 4 3\n4 1 2\n300000000 300000000 300000000\n999999998 1 1\n1 2 2"], "notes": "NoteThe subsequence of the array $$$a$$$ is a sequence that can be obtained from $$$a$$$ by removing zero or more of its elements.Two subsequences are considered different if index sets of elements included in them are different. That is, the values of the elements don't matter in the comparison of subsequences. In particular, any array of length $$$3$$$ has exactly $$$7$$$ different non-empty subsequences."}, "positive_code": [{"source_code": "n = gets.to_i\na = []\n\ndef find_ans(arr)\n i = 1\n j = i + 1\n while arr[i] + arr[j] != arr.last - arr[0]\n if j == i + 1\n j += 1\n else\n i += 1\n end\n end\n [arr[0], arr[i], arr[j]]\nend\n \n(0..n-1).each do |x|\n\tq = gets.chomp.split().map { |e| e.to_i }\n p\n\ta.append(find_ans(q))\nend\n \n \na.each do |x|\n puts x.join(' ')\nend"}, {"source_code": "t = gets.to_i\r\nans = []\r\nt.times do\r\n b = gets.split.map(&:to_i)\r\n a = [b[0],b[1]]\r\n if b[0]+b[1] == b[2]\r\n a << b[3]\r\n else\r\n a << b[2]\r\n end\r\n ans << a\r\nend\r\nans.each do |arr|\r\n puts arr.join(\" \")\r\nend"}, {"source_code": "testcases = Integer(gets.chomp)\r\nresultados = []\r\n\r\nfor i in 0..testcases-1\r\n arreglo = gets.chomp\r\n b = arreglo.split(\" \")\r\n numeroAConstruir = b[6].to_i\r\n validar = true\r\n finalPrincipal = false\r\n aumentoPrincipal = 0\r\n finalSecundario = false\r\n aumentoSecundario = 1\r\n finalTerceario = false\r\n aumentoTercerario = 2\r\n while validar\r\n if finalTerceario\r\n finalTerceario = false\r\n aumentoSecundario+=1\r\n if aumentoSecundario == 5\r\n finalSecundario = true\r\n end\r\n aumentoTercerario = aumentoSecundario + 1\r\n end\r\n if finalSecundario\r\n finalSecundario = false\r\n aumentoPrincipal += 1\r\n aumentoSecundario = aumentoPrincipal + 1\r\n aumentoTercerario = aumentoSecundario + 1\r\n end\r\n b1 = b[aumentoPrincipal].to_i\r\n b2 = b[aumentoSecundario].to_i\r\n b3 = b[aumentoTercerario].to_i\r\n if b1+b2+b3 == numeroAConstruir\r\n resultados.push(b1.to_s+\" \"+b2.to_s+\" \"+b3.to_s)\r\n break\r\n end \r\n aumentoTercerario +=1\r\n if aumentoTercerario == 6\r\n finalTerceario = true\r\n end\r\n end\r\nend\r\nputs resultados"}], "negative_code": [{"source_code": "n = gets.to_i\na = []\n\ndef find_ans(arr)\n i = 1\n j = i + 1\n while arr[i] + arr[j] != arr.last - arr[0]\n if j == i + 1\n j += 1\n else\n i += 1\n end\n end\n [arr[0], arr[i], arr[j]]\nend\n \n(0..n-1).each do |x|\n\tq = gets.chomp.split().map { |e| e.to_i }\n p\n\ta.append(find_ans(q))\nend\n \n \na.each do |x|\n p x.map(&:inspect).join(' ')\nend"}, {"source_code": "n = gets.to_i\na = []\n\ndef find_ans(arr)\n i = 1\n j = i + 1\n while arr[i] + arr[j] != arr.last - arr[0]\n if j == i + 1\n j += 1\n else\n i += 1\n end\n end\n [arr[0], arr[i], arr[j]]\nend\n \n(0..n-1).each do |x|\n\tq = gets.chomp.split().map { |e| e.to_i }\n p\n\ta.append(find_ans(q))\nend\n \n \na.each do |x|\n p x\nend"}], "src_uid": "e0ec0cd81d2ec632ef89d207d80fa8a3"} {"nl": {"description": "This is the easy version of the problem. The difference between the versions is that in the easy version all prices $$$a_i$$$ are different. You can make hacks if and only if you solved both versions of the problem.Today is Sage's birthday, and she will go shopping to buy ice spheres. All $$$n$$$ ice spheres are placed in a row and they are numbered from $$$1$$$ to $$$n$$$ from left to right. Each ice sphere has a positive integer price. In this version all prices are different.An ice sphere is cheap if it costs strictly less than two neighboring ice spheres: the nearest to the left and the nearest to the right. The leftmost and the rightmost ice spheres are not cheap. Sage will choose all cheap ice spheres and then buy only them.You can visit the shop before Sage and reorder the ice spheres as you wish. Find out the maximum number of ice spheres that Sage can buy, and show how the ice spheres should be reordered.", "input_spec": "The first line contains a single integer $$$n$$$ $$$(1 \\le n \\le 10^5)$$$ — the number of ice spheres in the shop. The second line contains $$$n$$$ different integers $$$a_1, a_2, \\dots, a_n$$$ $$$(1 \\le a_i \\le 10^9)$$$ — the prices of ice spheres.", "output_spec": "In the first line print the maximum number of ice spheres that Sage can buy. In the second line print the prices of ice spheres in the optimal order. If there are several correct answers, you can print any of them.", "sample_inputs": ["5\n1 2 3 4 5"], "sample_outputs": ["2\n3 1 4 2 5"], "notes": "NoteIn the example it's not possible to place ice spheres in any order so that Sage would buy $$$3$$$ of them. If the ice spheres are placed like this $$$(3, 1, 4, 2, 5)$$$, then Sage will buy two spheres: one for $$$1$$$ and one for $$$2$$$, because they are cheap."}, "positive_code": [{"source_code": "N = gets.to_i\nA = gets.split.map(&:to_i).sort\nres = []\n\nN.times do |i|\n if i.even?\n res << A.pop\n else\n res << A.shift\n end\nend\n\nif N.even?\n puts N / 2 - 1\n puts res.join(' ')\nelse\n puts N / 2\n puts res.join(' ')\nend\n"}], "negative_code": [], "src_uid": "bcd9439fbf6aedf6882612d5f7d6220f"} {"nl": {"description": "CQXYM is counting permutations length of $$$2n$$$.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).A permutation $$$p$$$(length of $$$2n$$$) will be counted only if the number of $$$i$$$ satisfying $$$p_i<p_{i+1}$$$ is no less than $$$n$$$. For example: Permutation $$$[1, 2, 3, 4]$$$ will count, because the number of such $$$i$$$ that $$$p_i<p_{i+1}$$$ equals $$$3$$$ ($$$i = 1$$$, $$$i = 2$$$, $$$i = 3$$$). Permutation $$$[3, 2, 1, 4]$$$ won't count, because the number of such $$$i$$$ that $$$p_i<p_{i+1}$$$ equals $$$1$$$ ($$$i = 3$$$). CQXYM wants you to help him to count the number of such permutations modulo $$$1000000007$$$ ($$$10^9+7$$$).In addition, modulo operation is to get the remainder. For example: $$$7 \\mod 3=1$$$, because $$$7 = 3 \\cdot 2 + 1$$$, $$$15 \\mod 4=3$$$, because $$$15 = 4 \\cdot 3 + 3$$$. ", "input_spec": "The input consists of multiple test cases. The first line contains an integer $$$t (t \\geq 1)$$$ — the number of test cases. The description of the test cases follows. Only one line of each test case contains an integer $$$n(1 \\leq n \\leq 10^5)$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$", "output_spec": "For each test case, print the answer in a single line.", "sample_inputs": ["4\n1\n2\n9\n91234"], "sample_outputs": ["1\n12\n830455698\n890287984"], "notes": "Note$$$n=1$$$, there is only one permutation that satisfies the condition: $$$[1,2].$$$In permutation $$$[1,2]$$$, $$$p_1<p_2$$$, and there is one $$$i=1$$$ satisfy the condition. Since $$$1 \\geq n$$$, this permutation should be counted. In permutation $$$[2,1]$$$, $$$p_1>p_2$$$. Because $$$0<n$$$, this permutation should not be counted.$$$n=2$$$, there are $$$12$$$ permutations: $$$[1,2,3,4],[1,2,4,3],[1,3,2,4],[1,3,4,2],[1,4,2,3],[2,1,3,4],[2,3,1,4],[2,3,4,1],[2,4,1,3],[3,1,2,4],[3,4,1,2],[4,1,2,3].$$$"}, "positive_code": [{"source_code": "t = gets.to_i\r\nMOD = 1000000007\r\nt.times do\r\n n = gets.to_i\r\n a = 1\r\n (1..n*2).each {|i| a = (a * i) % MOD}\r\n puts (a * 500000004) % MOD\r\nend\r\n"}], "negative_code": [], "src_uid": "19a2550af6a46308fd92c7a352f12a5f"} {"nl": {"description": "Valera has array a, consisting of n integers a0, a1, ..., an - 1, and function f(x), taking an integer from 0 to 2n - 1 as its single argument. Value f(x) is calculated by formula , where value bit(i) equals one if the binary representation of number x contains a 1 on the i-th position, and zero otherwise.For example, if n = 4 and x = 11 (11 = 20 + 21 + 23), then f(x) = a0 + a1 + a3.Help Valera find the maximum of function f(x) among all x, for which an inequality holds: 0 ≤ x ≤ m.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 105) — the number of array elements. The next line contains n space-separated integers a0, a1, ..., an - 1 (0 ≤ ai ≤ 104) — elements of array a. The third line contains a sequence of digits zero and one without spaces s0s1... sn - 1 — the binary representation of number m. Number m equals .", "output_spec": "Print a single integer — the maximum value of function f(x) for all .", "sample_inputs": ["2\n3 8\n10", "5\n17 0 10 2 1\n11010"], "sample_outputs": ["3", "27"], "notes": "NoteIn the first test case m = 20 = 1, f(0) = 0, f(1) = a0 = 3.In the second sample m = 20 + 21 + 23 = 11, the maximum value of function equals f(5) = a0 + a2 = 17 + 10 = 27."}, "positive_code": [{"source_code": "\nn = gets.chomp.to_i\na = gets.chomp.split(\" \").map{|i| i.to_i}\nmbits = gets.chomp\n\nlength = mbits.length\nmax = 0\nlength.times.each do |i|\n max += a[i] if mbits[i] == '1'\nend\n\ndefault = max\nsub_sum = 0\ni = 0\nwhile i < n do\n if mbits[i] == '1'\n last_sum = default + sub_sum - a[i]\n max = last_sum if last_sum > max\n else\n sub_sum += a[i]\n end\n i += 1\n \nend\n\nputs max\n"}, {"source_code": "n = gets.to_i\na = gets.chomp.split(\" \").map{|i| i.to_i}\nb = gets.chomp.split(\"\").map{|i| i.to_i}\n\nS = []\nT = []\ns = 0\nS[0] = a[0]\n(1..n-1).each do |i|\n S[i] = S[i-1] + a[i]\nend\n\nmax = 0\nj = n-1\nT[n] = 0\nwhile j>= 0 do\n if b[j] == 0\n T[j] = T[j+1]\n else\n T[j] = T[j+1] + a[j]\n end\n j -= 1\nend\n\nmax = T[0]\n\n(1..n-1).each do |i|\n if b[i] == 1\n s = S[i-1] + T[i+1] \n end\n if s > max \n max = s\n end\nend\n\nputs max"}], "negative_code": [], "src_uid": "9366e1626b33b4f4e49cf35200c0448f"} {"nl": {"description": "Summer holidays! Someone is going on trips, someone is visiting grandparents, but someone is trying to get a part-time job. This summer Noora decided that she wants to earn some money, and took a job in a shop as an assistant.Shop, where Noora is working, has a plan on the following n days. For each day sales manager knows exactly, that in i-th day ki products will be put up for sale and exactly li clients will come to the shop that day. Also, the manager is sure, that everyone, who comes to the shop, buys exactly one product or, if there aren't any left, leaves the shop without buying anything. Moreover, due to the short shelf-life of the products, manager established the following rule: if some part of the products left on the shelves at the end of the day, that products aren't kept on the next day and are sent to the dump.For advertising purposes manager offered to start a sell-out in the shop. He asked Noora to choose any f days from n next for sell-outs. On each of f chosen days the number of products were put up for sale would be doubled. Thus, if on i-th day shop planned to put up for sale ki products and Noora has chosen this day for sell-out, shelves of the shop would keep 2·ki products. Consequently, there is an opportunity to sell two times more products on days of sell-out.Noora's task is to choose f days to maximize total number of sold products. She asks you to help her with such a difficult problem.", "input_spec": "The first line contains two integers n and f (1 ≤ n ≤ 105, 0 ≤ f ≤ n) denoting the number of days in shop's plan and the number of days that Noora has to choose for sell-out. Each line of the following n subsequent lines contains two integers ki, li (0 ≤ ki, li ≤ 109) denoting the number of products on the shelves of the shop on the i-th day and the number of clients that will come to the shop on i-th day.", "output_spec": "Print a single integer denoting the maximal number of products that shop can sell.", "sample_inputs": ["4 2\n2 1\n3 5\n2 3\n1 5", "4 1\n0 2\n0 3\n3 5\n0 6"], "sample_outputs": ["10", "5"], "notes": "NoteIn the first example we can choose days with numbers 2 and 4 for sell-out. In this case new numbers of products for sale would be equal to [2, 6, 2, 2] respectively. So on the first day shop will sell 1 product, on the second — 5, on the third — 2, on the fourth — 2. In total 1 + 5 + 2 + 2 = 10 product units.In the second example it is possible to sell 5 products, if you choose third day for sell-out."}, "positive_code": [{"source_code": "# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n############################################################\ndef gs() gets.strip end\ndef gi() gets.to_i end\ndef gss() gs.split end\ndef gis() gss.map(&:to_i) end\ndef nmapf(n,f) n.times.map{ __send__ f } end\ndef ngs(n) nmapf n,:gs end\ndef ngi(n) nmapf n,:gi end\ndef ngss(n) nmapf n,:gss end\ndef ngis(n) nmapf n,:gis end\ndef arr2d(h,w,v=0) h.times.map{[v] * w} end\ndef for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end\ndef nsum(n) n * (n + 1) / 2 end\ndef vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end\n############################################################\n\n\nn, f = gis\n\nans = 0\nkls = []\nn.times do \n kl = gis\n k, l = kl\n mn = kl.min\n kls << [k * 2, l].min - mn\n ans += mn\nend\n\nxs = kls.sort.reverse\nf.times do |i|\n ans += xs[i]\nend\n\nputs ans"}], "negative_code": [], "src_uid": "c9b322a9138410a82e541179272eb6bf"} {"nl": {"description": "Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made. Value of x is calculated as maximum of p·ai + q·aj + r·ak for given p, q, r and array a1, a2, ... an such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative.", "input_spec": "First line of input contains 4 integers n, p, q, r ( - 109 ≤ p, q, r ≤ 109, 1 ≤ n ≤ 105). Next line of input contains n space separated integers a1, a2, ... an ( - 109 ≤ ai ≤ 109).", "output_spec": "Output a single integer the maximum value of p·ai + q·aj + r·ak that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n.", "sample_inputs": ["5 1 2 3\n1 2 3 4 5", "5 1 2 -3\n-1 -2 -3 -4 -5"], "sample_outputs": ["30", "12"], "notes": "NoteIn the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30.In second sample case, selecting i = j = 1 and k = 5 gives the answer 12."}, "positive_code": [{"source_code": "n, p, q, r = gets.split.map{|x| x.to_i}\narr = gets.split.map{|x| x.to_i}\n\nINF = 9 * 10**18\n\ndp = [[-INF, -INF, -INF]]\n\nn.times do |i|\n\tdp << [0, 0, 0]\n\tdp[i + 1][0] = [dp[i][0], p * arr[i]].max\n\tdp[i + 1][1] = [dp[i][1], dp[i + 1][0] + q * arr[i]].max\n\tdp[i + 1][2] = [dp[i][2], dp[i + 1][1] + r * arr[i]].max\nend\n\nans = -INF\nn.times{|i| ans = [ans, dp[i + 1][2]].max}\n\np ans"}, {"source_code": "array = gets.split(\" \")\nn = array.shift\nbig_max = -999999999999999999999\nshuzu = gets.split(\" \")\nsum=[]\nsum[0] = -9999999999999999999999\nfor i in 0..n.to_i-1 do\n sum[i] = sum[i-1] if i>0\n temp = array[0].to_i*shuzu[i].to_i\n if temp > sum[i]\n sum[i] = temp;\n end\nend\n\nsum2 = []\nsum2[0] = -9999999999999999999999\nfor i in 0..n.to_i-1 do\n sum2[i] = sum2[i-1] if i>0\n temp = sum[i]+array[1].to_i*shuzu[i].to_i\n if temp > sum2[i]\n sum2[i] = temp;\n end\nend\n\n#p sum\n#p sum2\nfor i in 0..n.to_i-1\n max = sum2[i]+array[2].to_i*shuzu[i].to_i\n big_max=max if max>big_max\nend\nprint big_max\n\n"}], "negative_code": [{"source_code": "n, p, q, r = gets.split.map{|x| x.to_i}\narr = gets.split.map{|x| x.to_i}\n\nminus = arr.min\nplus = arr.max\n\np p * (p < 0 ? minus : plus) + q * (q < 0 ? minus : plus) + r * (r < 0 ? minus : plus)"}, {"source_code": "n, p, q, r = gets.split.map{|x| x.to_i}\narr = gets.split.map{|x| x.to_i}\n\nINF = 10**18\n\ndp = [[-INF, -INF, -INF]]\n\nn.times do |i|\n\tdp << [0, 0, 0]\n\tdp[i + 1][0] = [dp[i][0], p * arr[i]].max\n\tdp[i + 1][1] = [dp[i][1], dp[i + 1][0] + q * arr[i]].max\n\tdp[i + 1][2] = [dp[i][2], dp[i + 1][1] + r * arr[i]].max\nend\n\nans = -INF\nn.times{|i| ans = [ans, dp[i + 1][2]].max}\n\np ans"}, {"source_code": "array = gets.split(\" \")\nn = array.shift\nbig_max = -9999999999999999999\nshuzu = gets.split(\" \")\nsum=[]\nsum[0] = -9999999999999999999\nfor i in 0..n.to_i-1 do\n sum[i] = sum[i-1] if i>0\n temp = array[0].to_i*shuzu[i].to_i\n if temp > sum[i]\n sum[i] = temp;\n end\nend\n\nsum2 = []\nsum2[0] = -9999999999999999999\nfor i in 0..n.to_i-1 do\n sum2[i] = sum2[i-1] if i>0\n temp = sum[i]+array[1].to_i*shuzu[i].to_i\n if temp > sum2[i]\n sum2[i] = temp;\n end\nend\n\np sum\np sum2\nfor i in 0..n.to_i-1\n max = sum2[i]+array[2].to_i*shuzu[i].to_i\n big_max=max if max>big_max\nend\nprint big_max\n\n"}], "src_uid": "a8e56ad4de6f0eecbe5521226c0335ab"} {"nl": {"description": "In an ICPC contest, balloons are distributed as follows: Whenever a team solves a problem, that team gets a balloon. The first team to solve a problem gets an additional balloon. A contest has 26 problems, labelled $$$\\textsf{A}$$$, $$$\\textsf{B}$$$, $$$\\textsf{C}$$$, ..., $$$\\textsf{Z}$$$. You are given the order of solved problems in the contest, denoted as a string $$$s$$$, where the $$$i$$$-th character indicates that the problem $$$s_i$$$ has been solved by some team. No team will solve the same problem twice.Determine the total number of balloons that the teams received. Note that some problems may be solved by none of the teams.", "input_spec": "The first line of the input contains an integer $$$t$$$ ($$$1 \\leq t \\leq 100$$$) — the number of testcases. The first line of each test case contains an integer $$$n$$$ ($$$1 \\leq n \\leq 50$$$) — the length of the string. The second line of each test case contains a string $$$s$$$ of length $$$n$$$ consisting of uppercase English letters, denoting the order of solved problems.", "output_spec": "For each test case, output a single integer — the total number of balloons that the teams received.", "sample_inputs": ["6\n\n3\n\nABA\n\n1\n\nA\n\n3\n\nORZ\n\n5\n\nBAAAA\n\n4\n\nBKPT\n\n10\n\nCODEFORCES"], "sample_outputs": ["5\n2\n6\n7\n8\n17"], "notes": "NoteIn the first test case, $$$5$$$ balloons are given out: Problem $$$\\textsf{A}$$$ is solved. That team receives $$$2$$$ balloons: one because they solved the problem, an an additional one because they are the first team to solve problem $$$\\textsf{A}$$$. Problem $$$\\textsf{B}$$$ is solved. That team receives $$$2$$$ balloons: one because they solved the problem, an an additional one because they are the first team to solve problem $$$\\textsf{B}$$$. Problem $$$\\textsf{A}$$$ is solved. That team receives only $$$1$$$ balloon, because they solved the problem. Note that they don't get an additional balloon because they are not the first team to solve problem $$$\\textsf{A}$$$. The total number of balloons given out is $$$2+2+1=5$$$.In the second test case, there is only one problem solved. The team who solved it receives $$$2$$$ balloons: one because they solved the problem, an an additional one because they are the first team to solve problem $$$\\textsf{A}$$$."}, "positive_code": [{"source_code": "#!/usr/bin/env ruby\n\nnumtests = gets.chomp.to_i\n\n(1..numtests).each do |numline|\n ballons = 0\n solved = []\n n = gets.chomp.to_i\n l = gets.chomp\n (0..n-1).each do |i|\n c = l[i]\n if solved.include?(c)\n ballons += 1\n else\n ballons += 2\n solved << c\n end\n end\n puts ballons\nend\n\n\n"}, {"source_code": "require 'set'\n\nt = gets.to_i\n\nt.times do\n gets\n ln = gets.chomp\n s = Set.new\n b = 0\n ln.each_char do |c|\n b += 1 if not s.include? c\n s << c\n b += 1\n end\n puts b\nend\n"}, {"source_code": "def icpc_baloons(str, n)\n map = {}\n str.chars.each do |s|\n if map.key?(s)\n map[s] += 1\n else\n map[s] = 2\n end\n end\n ans = 0\n\n map.each do |k, v|\n ans += v\n end\n ans\nend\nt = gets.chomp.to_i\ninputs = []\n(0..t-1).each do\n n = gets.chomp.to_i\n str = gets.chomp\n inputs.push([str, n])\nend\n\ninputs.each do |input|\n puts icpc_baloons(input[0], input[1])\nend\n"}], "negative_code": [], "src_uid": "66777b8719b1756bf4b6bf93feb2e439"} {"nl": {"description": "A big football championship will occur soon! $$$n$$$ teams will compete in it, and each pair of teams will play exactly one game against each other.There are two possible outcomes of a game: the game may result in a tie, then both teams get $$$1$$$ point; one team might win in a game, then the winning team gets $$$3$$$ points and the losing team gets $$$0$$$ points. The score of a team is the number of points it gained during all games that it played.You are interested in a hypothetical situation when all teams get the same score at the end of the championship. A simple example of that situation is when all games result in ties, but you want to minimize the number of ties as well.Your task is to describe a situation (choose the result of each game) so that all teams get the same score, and the number of ties is the minimum possible.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. Then the test cases follow. Each test case is described by one line containing one integer $$$n$$$ ($$$2 \\le n \\le 100$$$) — the number of teams.", "output_spec": "For each test case, print $$$\\frac{n(n - 1)}{2}$$$ integers describing the results of the games in the following order: the first integer should correspond to the match between team $$$1$$$ and team $$$2$$$, the second — between team $$$1$$$ and team $$$3$$$, then $$$1$$$ and $$$4$$$, ..., $$$1$$$ and $$$n$$$, $$$2$$$ and $$$3$$$, $$$2$$$ and $$$4$$$, ..., $$$2$$$ and $$$n$$$, and so on, until the game between the team $$$n - 1$$$ and the team $$$n$$$. The integer corresponding to the game between the team $$$x$$$ and the team $$$y$$$ should be $$$1$$$ if $$$x$$$ wins, $$$-1$$$ if $$$y$$$ wins, or $$$0$$$ if the game results in a tie. All teams should get the same score, and the number of ties should be the minimum possible. If there are multiple optimal answers, print any of them. It can be shown that there always exists a way to make all teams have the same score.", "sample_inputs": ["2\n2\n3"], "sample_outputs": ["0 \n1 -1 1"], "notes": "NoteIn the first test case of the example, both teams get $$$1$$$ point since the game between them is a tie.In the second test case of the example, team $$$1$$$ defeats team $$$2$$$ (team $$$1$$$ gets $$$3$$$ points), team $$$1$$$ loses to team $$$3$$$ (team $$$3$$$ gets $$$3$$$ points), and team $$$2$$$ wins against team $$$3$$$ (team $$$2$$$ gets $$$3$$$ points)."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n n=gets.to_i\r\n l=[]\r\n if n.odd?\r\n n-=1\r\n while n>0\r\n n.times do |i|\r\n if i.even?\r\n l << 1\r\n else\r\n l << -1\r\n end\r\n end\r\n n-=1\r\n end\r\n puts l.join(\" \")\r\n else\r\n n-=1\r\n nn=n-1\r\n while n>0\r\n n.times do |i|\r\n if i 0\r\n s += @data[r]\r\n r ^= r & -r\r\n end\r\n s\r\n end\r\n end\r\nend\r\n\r\nt = gets.to_i\r\nt.times do\r\n\r\nn, m = gets.split.map!(&:to_i)\r\na = gets.split.map!(&:to_i)\r\n\r\ncnt = 0\r\nh = a.tally\r\nh1 = {}\r\nh2 = {}\r\nh.keys.sort.each { |k| h1[k] = cnt; h2[k] = (cnt += h[k]) }\r\n\r\nscore = 0\r\nft = Array.new(n) { AtCoder::FenwickTree.new(m) }\r\na.each do |ai|\r\n i1, j1 = h1[ai].divmod(m)\r\n i2, j2 = h2[ai].divmod(m)\r\n\r\n if i1 < i2\r\n score += ft[i1].sum(0, m)\r\n h1[ai] += 1\r\n next\r\n end\r\n\r\n i = i1\r\n j = j2 - 1\r\n x = ft[i]\r\n score += x.sum(0, j)\r\n x.add(j, 1)\r\n\r\n h1[ai] += 1\r\n h2[ai] -= 1\r\nend\r\n\r\nputs score\r\n\r\nend\r\n"}], "negative_code": [], "src_uid": "99c5e62d8e51e61cfd0c2531a231e7a8"} {"nl": {"description": "Little walrus Fangy loves math very much. That's why when he is bored he plays with a number performing some operations.Fangy takes some positive integer x and wants to get a number one from it. While x is not equal to 1, Fangy repeats the following action: if x is odd, then he adds 1 to it, otherwise he divides x by 2. Fangy knows that for any positive integer number the process ends in finite time.How many actions should Fangy perform to get a number one from number x?", "input_spec": "The first line contains a positive integer x in a binary system. It is guaranteed that the first digit of x is different from a zero and the number of its digits does not exceed 106.", "output_spec": "Print the required number of actions.", "sample_inputs": ["1", "1001001", "101110"], "sample_outputs": ["0", "12", "8"], "notes": "NoteLet's consider the third sample. Number 101110 is even, which means that we should divide it by 2. After the dividing Fangy gets an odd number 10111 and adds one to it. Number 11000 can be divided by 2 three times in a row and get number 11. All that's left is to increase the number by one (we get 100), and then divide it by 2 two times in a row. As a result, we get 1."}, "positive_code": [{"source_code": "n=gets.split[0]\nn=n.bytes.to_a\nk=0\nl=n.length\nans=0\n(l-1).downto(0){|i|\n if i==0 && k==0 then\n ans+=1\n else\n if n[i]+k==49 then\n ans+=1\n k=1\n elsif n[i]+k==50 then\n k=1\n else\n k=0\n end\n ans+=1\n end \n}\nans+=k\nputs ans-1\n\n"}, {"source_code": "a=c=0\ngets.chomp.reverse.bytes.to_a[0..-2].map{|n|a+=(n+c==48?1:n+c==49?(c=1;2):1)}\np a+c\n"}, {"source_code": "a=c=0\ngets.chomp.reverse.bytes.to_a[0..-2].map{|n|a+=(n+c==48?1:n+c==49?(c=1;2):1)}\np a+c"}, {"source_code": "a=c=0\ngets.chomp.reverse.bytes.to_a[0..-2].map{|n|a+=(n+c==48?1:n+c==49?(c=1;2):1)}\np a+c\n"}, {"source_code": "a=c=0\ngets.chomp.reverse.bytes.to_a[0..-2].map{|n|a+=(n+c==48?1:n+c==49?(c=1;2):1)}\np a+c\n"}, {"source_code": "a=c=0\ngets.chomp.reverse.bytes.to_a[0..-2].map{|n|a+=(n+c==48?1:n+c==49?(c=1;2):1)}\np a+c"}, {"source_code": "#=================================\nrequire 'prime'\nrequire 'set'\nrequire 'matrix'\nrequire 'tsort'\ninclude Math\n\ndef local?;\tENV[\"LOGNAME\"]==\"skydos\"; end\n\nif local?\n\t$stdin = File.open(\"./input.txt\", \"r\")\nend\n#=================================\n\n#=================================\nif local?\n\tstart_time = Time.now\nend\n#=================================\n\nb=gets.chomp.split(//).map(&:to_i)\n\ni=b.size-1\ncount=0\n\nwhile i>=1\n\tcount+=1\n\t\n\tif b[i]==0\n\t\ti-=1\n\telse\n\t\tcount+=1\n\t\ti-=1\n\t\twhile i>=0 && b[i]==1\n\t\t\tcount+=1\n\t\t\ti-=1\n\t\tend\n\t\t\n\t\tb[i]=1 if i>=0\n\tend\nend\n\nputs count\n\n#=================================\nif local?\n\tend_time = Time.now\n\tputs \"\\n\\n\"\n\tputs \"===\"*10\n\tprintf \"Time: %.4f sec\\n\", (end_time-start_time)/1000.0\n\tputs \"===\"*10\nend\n#=================================\n"}, {"source_code": "a=c=0\ngets.chomp.reverse.bytes.to_a[0..-2].map{|n|a+=(n+c==48?1:n+c==49?(c=1;2):1)}\np a+c\n"}, {"source_code": "a=c=0\ngets.chomp.reverse.bytes.to_a[0..-2].map{|n|a+=(n+c==48?1:n+c==49?(c=1;2):1)}\np a+c"}, {"source_code": "a=c=0\ngets.chomp.reverse.bytes.to_a[0..-2].map{|n|a+=(n+c==48?1:n+c==49?(c=1;2):1)}\np a+c\n"}], "negative_code": [{"source_code": "n=gets.split[0]\nn=n.bytes.to_a\nk=0\nl=n.length\nans=0\n(l-1).downto(0){|i|\n if i==0 && k==0 then\n ans+=1;k=1\n else\n if n[i]+k==49 then\n ans+=1\n k=1\n elsif n[i]+k==50 then\n k=1\n else\n k=0\n end\n ans+=1\n end \n}\nans+=k\nputs ans-1\n\n"}, {"source_code": "#=================================\nrequire 'prime'\nrequire 'set'\nrequire 'matrix'\nrequire 'tsort'\ninclude Math\n\nif ARGV.include?(\"ONLINE_JUDGE\")\n\t$stdin = File.open(\"./input.txt\", \"r\")\n\tARGV.delete(\"ONLINE_JUDGE\")\nend\n#=================================\n\nb=gets.strip\n\nif b.length==1 && b==\"1\"\n\tputs 0\n\texit\nend\n\nans=count=0\n\nloop do\t\n\tif b[-1]==\"1\"\n\t\tcount+=1\n\t\ti=b.length-1\n\t\t\n\t\twhile b[i]==\"1\"\n\t\t\tb[i]=\"0\"\n\t\t\ti-=1\n\t\t\tcount+=1\n\t\tend\n\t\t\n\t\tb[i]=\"1\" if i>=0\n\t\tb=b[0..i]\n\telse\n\t\ti=b.length-1\n\t\t\n\t\twhile b[i]==\"0\"\n\t\t\tcount+=1\n\t\t\ti-=1\n\t\tend\n\t\t\n\t\tb=b[0..i]\n\tend\n\t\n\tans=count if b.length!=0\n\tbreak if b.length==0\nend\n\nputs ans\n"}, {"source_code": "#=================================\nrequire 'prime'\nrequire 'set'\nrequire 'matrix'\nrequire 'tsort'\ninclude Math\n\ndef local?;\tENV[\"LOGNAME\"]==\"skydos\"; end\n\nif local?\n\t$stdin = File.open(\"./input.txt\", \"r\")\nend\n#=================================\n\n#=================================\nif local?\n\tstart_time = Time.now\nend\n#=================================\n\nb=gets.strip\n\nif b==\"1\"\n\tputs 0\n\texit\nend\n\ni=b.length-1\ncount=0\n\nwhile i>-1\t\n\tif b[i]==\"0\"\n\t\tcount+=1\n\t\ti-=1\n\telse\n\t\tcount+=1\n\t\twhile i>-1 && b[i]==\"1\"\n\t\t\tcount+=1\n\t\t\ti-=1\n\t\tend\n\t\t\n\t\tb[i]=\"1\" if i>-1\n\tend\nend\n\nputs count\n\n#=================================\nif local?\n\tend_time = Time.now\n\tputs \"\\n\\n\"\n\tputs \"===\"*10\n\tprintf \"Time: %.4f sec\\n\", (end_time-start_time)/100.0\n\tputs \"===\"*10\nend\n#=================================\n"}, {"source_code": "#=================================\nrequire 'prime'\nrequire 'set'\nrequire 'matrix'\nrequire 'tsort'\ninclude Math\n\ndef local?;\tENV[\"LOGNAME\"]==\"skydos\"; end\n\nif local?\n\t$stdin = File.open(\"./input.txt\", \"r\")\nend\n#=================================\n\n#=================================\nif local?\n\tstart_time = Time.now\nend\n#=================================\n\nb=gets.strip\n\ni=b.length-1\ncount=0\n\nwhile i>-1\t\n\tif b[i]==\"0\"\n\t\tcount+=1\n\t\ti-=1\n\telse\n\t\tcount+=1\n\t\twhile i>-1 && b[i]==\"1\"\n\t\t\tcount+=1\n\t\t\ti-=1\n\t\tend\n\t\t\n\t\tb[i]=\"1\" if i>-1\n\tend\nend\n\nputs count\n\n#=================================\nif local?\n\tend_time = Time.now\n\tputs \"\\n\\n\"\n\tputs \"===\"*10\n\tprintf \"Time: %.4f sec\\n\", (end_time-start_time)/100.0\n\tputs \"===\"*10\nend\n#=================================\n"}], "src_uid": "e46c6406d19e8679fd282d72882ae78d"} {"nl": {"description": "And again a misfortune fell on Poor Student. He is being late for an exam.Having rushed to a bus stop that is in point (0, 0), he got on a minibus and they drove along a straight line, parallel to axis OX, in the direction of increasing x.Poor Student knows the following: during one run the minibus makes n stops, the i-th stop is in point (xi, 0) coordinates of all the stops are different the minibus drives at a constant speed, equal to vb it can be assumed the passengers get on and off the minibus at a bus stop momentarily Student can get off the minibus only at a bus stop Student will have to get off the minibus at a terminal stop, if he does not get off earlier the University, where the exam will be held, is in point (xu, yu) Student can run from a bus stop to the University at a constant speed vs as long as needed a distance between two points can be calculated according to the following formula: Student is already on the minibus, so, he cannot get off at the first bus stop Poor Student wants to get to the University as soon as possible. Help him to choose the bus stop, where he should get off. If such bus stops are multiple, choose the bus stop closest to the University.", "input_spec": "The first line contains three integer numbers: 2 ≤ n ≤ 100, 1 ≤ vb, vs ≤ 1000. The second line contains n non-negative integers in ascending order: coordinates xi of the bus stop with index i. It is guaranteed that x1 equals to zero, and xn ≤ 105. The third line contains the coordinates of the University, integers xu and yu, not exceeding 105 in absolute value. ", "output_spec": "In the only line output the answer to the problem — index of the optimum bus stop.", "sample_inputs": ["4 5 2\n0 2 4 6\n4 1", "2 1 1\n0 100000\n100000 100000"], "sample_outputs": ["3", "2"], "notes": "NoteAs you know, students are a special sort of people, and minibuses usually do not hurry. That's why you should not be surprised, if Student's speed is higher than the speed of the minibus."}, "positive_code": [{"source_code": "n,vb,vs = gets.chomp.split(/ /).map!{|x| x.to_i}\nar = gets.chomp.split(/ /).map!{|x| x.to_i}\nxu,yu = gets.chomp.split(/ /).map!{|x| x.to_i}\n\nmint = 0\nmind = 0\nidx = 0\nfor i in 1..n-1\n\twk = (ar[i]-xu).abs\n\twkt = ar[i].to_f/vb+Math.sqrt(wk*wk + yu*yu)/vs\n\tif idx == 0 || mint > wkt\n\t\tmint = wkt\n\t\tmind = wk\n\t\tidx = i+1\n\telsif mint == wkt\n\t\tif mind > wk\n\t\t\tmint = wkt\n\t\t\tmind = wk\n\t\t\tidx = i+1\n\t\tend\n\tend\nend\nputs idx\n"}, {"source_code": "n, vb, vs = gets.split.map{|i| i.to_f}\nx = gets.split.map{|i| i.to_i}\nxu, yu = gets.split.map{|i| i.to_i}\nans = [1e100, -1, -1]\n1.upto(x.length - 1) do |i|\n\td = (xu - x[i]) ** 2 + yu ** 2\n\tt = x[i] / vb + Math.sqrt(d) / vs\n\tans = [t, d, i] if t < ans[0] - 1e-6 || (t < ans[0] + 1e-6 && d < ans[1])\nend\np ans[2] + 1\n\n#\n"}, {"source_code": "n,vb,vs=STDIN.gets.chomp.split\nxn=STDIN.gets.chomp.split\nxu,yu=STDIN.gets.chomp.split\nn=n.to_i\nvb=vb.to_i\nvs=vs.to_i\nxu=xu.to_i\nyu=yu.to_i\nfor i in 0...xn.length do xn[i]=xn[i].to_i end\nidx=0\nmin_t=-1\nmin_u=-1\nfor i in 1...xn.length\n d=Math::sqrt((xu-xn[i])*(xu-xn[i])+yu*yu)\n t=1.0*(xn[i]-xn[1])/vb\n t+=1.0*d/vs\n if min_t==-1\n min_t=t\n min_u=d\n idx=i\n elsif ttemp_t\n t=temp_t\n bu = l\n res = i+2\n elsif t==temp_t\n if bu>l\n t=temp_t\n bu = l\n res = i+2\n end\n end\nend\nputs res\n"}, {"source_code": "n,vb,vs=gets.split.map &:to_i;z=[1e9,1e9,0];\na=gets.split.map &:to_f;x,y=gets.split.map &:to_i\n1.upto(n-1){|i|d=((x-a[i])**2+y**2)**0.5;z=[[d/vs+a[i]/vb,d,i],z].min}\np z[2]+1"}, {"source_code": "n,vb,vs=gets.split.map &:to_i;z=[1e9,1e9,0];\na=gets.split.map &:to_f;x,y=gets.split.map &:to_i\n1.upto(n-1){|i|d=((x-a[i])**2+y**2)**0.5;z=[[d/vs+a[i]/vb,d,i],z].min}\np z[2]+1\n"}, {"source_code": "def dist(x1,y1,x2,y2)\n\tMath.sqrt(((x2-x1)**2) + ((y2-y1)**2))\nend\n\nn,v_b,v_s = gets.split(\" \").map(&:to_i)\narr = gets.split(\" \").map(&:to_i)\nuniv_x,univ_y = gets.split(\" \").map(&:to_i)\n\nans = {}\n\narr[1..-1].each_with_index do |a,i|\n\tbus_time = a/v_b.to_f\n\t# puts \"h33 #{bus_time}\"\n\twalk_time = (dist(univ_x,univ_y,a,0))/v_s\n\t# puts \"#{[bus_time + walk_time,walk_time]}\"\n\tans[[bus_time + walk_time,walk_time]] = i\n\nend\n# puts \"#{ans}\"\n\nkey = ans.keys.min\n\nputs \"#{ans[key]+2}\""}, {"source_code": "#$stdin = File.open('in.txt', 'r')\n@n,@vb,@vs = gets.split(/\\s+/).map {|x| x.to_i}\n@stopes = gets.split(/\\s+/).map {|x| x.to_i}\n@xu,@yu = gets.split(/\\s+/).map {|x| x.to_i}\n\n@stopes.unshift(0)\n# a = 2\n# b = @n\n# \ndef compute(index)\n @stopes[index]/@vb.to_f + Math.sqrt(@yu*@yu+(@stopes[index]-@xu)*(@stopes[index]-@xu))/@vs\nend\n\nif @vs > @vb\n puts 2\nelse\n kms = @stopes[2].abs\n x = @stopes[2]\n answer = 2\n min = kms/@vb.to_f + Math.sqrt(@yu*@yu+(x-@xu)*(x-@xu))/@vs\n \n i = 3\n# puts \"#{answer} #{min} #{compute(2)}\"\n\n while i <= @n\n kms += (@stopes[i-1].abs-@stopes[i].abs).abs\n x = @stopes[i]\n val = kms/@vb.to_f + Math.sqrt(@yu*@yu+(x-@xu)*(x-@xu))/@vs\n\n if val < min\n answer = i\n min = val\n elsif (val == min) && ((@stopes[answer]-@xu).abs > (@stopes[i]-@xu).abs)\n answer = i\n end\n i += 1\n end\n puts answer\nend\n# av = compute(a)\n# bv = compute(b)\n# \n# while b > a\n# # puts \"a #{a} #{av}\"\n# # puts \"b #{b} #{bv}\"\n# if bv > av\n# b = ((b+a) / 2)\n# bv = compute(b)\n# else\n# a = ((b+a) / 2) + 1\n# av = compute(a)\n# end\n# end\n# #puts \"#{a} #{b}\"\n# # if (b-a) == 1\n# # mv = compute(a+1)\n# # if mv < av\n# # a += 1\n# # av = mv\n# # end\n# # end\n# #puts \"#{av} #{bv}\"\n# if av > bv\n# puts b\n# else #if bv > av\n# puts a \n# # else\n# # av = (@stopes[a]-@xu).abs\n# # bv = (@stopes[b]-@xu).abs\n# # puts bv > av ? a : b\n# end\n# \n"}, {"source_code": "n, vb, vs = gets.split(/\\s+/).map{|x| x.to_i}\nx = gets.split(/\\s+/).map{|x| x.to_i}\nxu, yu = gets.split(/\\s+/).map{|x| x.to_i}\n\nres = 0\nrest = 0.0\nresd = 0.0\n(1...n).each {|i|\n dx = (x[i] - xu).to_f\n dy = (0 - yu).to_f\n d = Math.sqrt(dx * dx + dy * dy)\n t = x[i].to_f / vb + d / vs\n if res == 0 || t < rest || (t == rest && d < resd) then\n res = i\n rest = t\n resd = d\n end \n}\np res + 1\n"}, {"source_code": "#� ������ ������ ���� ��� ����� �����: 2???n? ??100, 1???vb,?vs? ??1000. �� ������ ������ ���� n ��������������� ����� ����� � ������� �����������\n#: ���������� xi ��������� ����� i. �������������, ��� x1 ����� ����, � xn? ??105. \n#� ������� ������ ���� ���������� ������������, ����� ����� xu � yu, �� ������ �� ����������� 105.\n\n\nn, vb, vs = gets.split(' ').map{|z|z.to_i}\ncrd = gets.split(' ').map{|z|z.to_i}\nxu,yu = gets.split(' ').map{|z|z.to_i}\n\nmin = 105000000\nru = 105000000\nres=0\ncrd[1..-1].each_with_index{|xi,i| \n mru = (xu-xi)*(xu-xi)+yu*yu\n d = (Math.sqrt(xi*xi)*vs+Math.sqrt(mru)*vb)\n if (d < min)||((d==min)&&(mru < ru)) then\n min = d\n res = i+1\n ru = mru\n end\n}\n\nputs res+1"}, {"source_code": "n,vb,vs=gets.split.map &:to_i;z=[1e9,1e9,0];\na=gets.split.map &:to_f;x,y=gets.split.map &:to_i\n1.upto(n-1){|i|d=((x-a[i])**2+y**2)**0.5;z=[[d/vs+a[i]/vb,d,i],z].min}\np z[2]+1"}, {"source_code": "n,vb,vs=gets.split.map &:to_i;z=[1e9,1e9,0];\na=gets.split.map &:to_f;x,y=gets.split.map &:to_i\n1.upto(n-1){|i|d=((x-a[i])**2+y**2)**0.5;z=[[d/vs+a[i]/vb,d,i],z].min}\np z[2]+1\n"}, {"source_code": "n,vb,vs=gets.split.map &:to_i;z=[1e9,1e9,0];\na=gets.split.map &:to_f;x,y=gets.split.map &:to_i\n1.upto(n-1){|i|d=((x-a[i])**2+y**2)**0.5;z=[[d/vs+a[i]/vb,d,i],z].min}\np z[2]+1\n"}], "negative_code": [{"source_code": "n,vb,vs = gets.chomp.split(/ /).map!{|x| x.to_i}\nar = gets.chomp.split(/ /).map!{|x| x.to_i}\nxu,yu = gets.chomp.split(/ /).map!{|x| x.to_i}\n\nmind = 0\nidx = 0\nfor i in 0..n-1\n\twk = (ar[i]-xu).abs\n\tif idx == 0 || mind > wk\n\t\tmind = wk\n\t\tidx = i+1\n\tend\nend\nputs idx\n"}, {"source_code": "n,vb,vs = gets.chomp.split(/ /).map!{|x| x.to_i}\nar = gets.chomp.split(/ /).map!{|x| x.to_i}\nxu,yu = gets.chomp.split(/ /).map!{|x| x.to_i}\n\nmint = 0\nidx = 0\nfor i in 1..n-1\n\twk = (ar[i]-xu).abs\n\twkt = ar[i]/vb+Math.sqrt(wk*wk + yu*yu)/vs\n\tif idx == 0 || mint >= wkt\n\t\tmint = wkt\n\t\tmind = wk\n\t\tidx = i+1\n\tend\nend\nputs idx\n"}, {"source_code": "n,vb,vs = gets.chomp.split(/ /).map!{|x| x.to_i}\nar = gets.chomp.split(/ /).map!{|x| x.to_i}\nxu,yu = gets.chomp.split(/ /).map!{|x| x.to_i}\n\nmind = 0\nidx = 0\nfor i in 0..n-1\n\twk = (ar[i]-xu).abs\n\tif idx == 0 || mind > wk\n\t\tmind = wk\n\t\tidx = i+1\n\tend\nend\nputs idx\n"}, {"source_code": "n,vb,vs = gets.chomp.split(/ /).map!{|x| x.to_i}\nar = gets.chomp.split(/ /).map!{|x| x.to_i}\nxu,yu = gets.chomp.split(/ /).map!{|x| x.to_i}\n\nmind = 0\nidx = 0\nfor i in 0..n-1\n\twk = (ar[i]-xu).abs\n\tif idx == 0 || mind > wk\n\t\tmind = wk\n\t\tidx = i+1\n\tend\nend\nputs idx\n"}, {"source_code": "n,vb,vs = gets.chomp.split(/ /).map!{|x| x.to_i}\nar = gets.chomp.split(/ /).map!{|x| x.to_i}\nxu,yu = gets.chomp.split(/ /).map!{|x| x.to_i}\n\nmind = 0\nidx = 0\nfor i in 0..n-1\n\twk = (ar[i]-xu).abs\n\tif idx == 0 || mind > wk\n\t\tmind = wk\n\t\tidx = i+1\n\tend\nend\nputs idx\n"}, {"source_code": "n,vb,vs = gets.chomp.split(/ /).map!{|x| x.to_i}\nar = gets.chomp.split(/ /).map!{|x| x.to_i}\nxu,yu = gets.chomp.split(/ /).map!{|x| x.to_i}\n\nmint = 0\nidx = 0\nfor i in 1..n-1\n\twk = (ar[i]-xu).abs\n\twkt = ar[i]/vb+Math.sqrt(wk*wk + yu*yu)/vs\n\tif idx == 0 || mint + 1e-6 > wkt\n\t\tmint = wkt\n\t\tmind = wk\n\t\tidx = i+1\n\telsif (mint - wkt).abs < 1e-6\n\t\tif mind > wk\n\t\t\tmint = wkt\n\t\t\tmind = wk\n\t\t\tidx = i+1\n\t\tend\n\tend\nend\nputs idx\n"}, {"source_code": "n,vb,vs = gets.chomp.split(/ /).map!{|x| x.to_i}\nar = gets.chomp.split(/ /).map!{|x| x.to_i}\nxu,yu = gets.chomp.split(/ /).map!{|x| x.to_i}\n\nmint = 0\nmind = 0\nidx = 0\nfor i in 1..n-1\n\twk = (ar[i]-xu).abs\n\twkt = ar[i]/vb+Math.sqrt(wk*wk + yu*yu)/vs\n\tif idx == 0 || mint > wkt\n\t\tmint = wkt\n\t\tmind = wk\n\t\tidx = i+1\n\telsif mint == wkt\n\t\tif mind > wk\n\t\t\tmint = wkt\n\t\t\tmind = wk\n\t\t\tidx = i+1\n\t\tend\n\tend\nend\nputs idx\n"}, {"source_code": "n,vb,vs = gets.chomp.split(/ /).map!{|x| x.to_i}\nar = gets.chomp.split(/ /).map!{|x| x.to_i}\nxu,yu = gets.chomp.split(/ /).map!{|x| x.to_i}\n\nmint = 0\nidx = 0\nfor i in 1..n-1\n\twk = (ar[i]-xu).abs\n\twkt = ar[i]/vb+Math.sqrt(wk*wk + yu*yu)/vs\n\tif idx == 0 || mint > wkt\n\t\tmint = wkt\n\t\tmind = wk\n\t\tidx = i+1\n\telsif mint == wkt\n\t\tif mind > wk\n\t\t\tmint = wkt\n\t\t\tmind = wk\n\t\t\tidx = i+1\n\t\tend\n\tend\nend\nputs idx\n"}, {"source_code": "n,vb,vs = gets.chomp.split(/ /).map!{|x| x.to_i}\nar = gets.chomp.split(/ /).map!{|x| x.to_i}\nxu,yu = gets.chomp.split(/ /).map!{|x| x.to_i}\n\nmint = 0\nmind = 0\nidx = 0\nfor i in 1..n-1\n\twk = (ar[i]-xu).abs\n\twkt = ar[i]/vb+Math.sqrt(wk*wk + yu*yu)/vs\n\tif idx == 0 || mint > wkt\n\t\tmint = wkt\n\t\tmind = wk\n\t\tidx = i+1\n\tend\nend\nputs idx\n"}, {"source_code": "n,vb,vs = gets.chomp.split(/ /).map!{|x| x.to_i}\nar = gets.chomp.split(/ /).map!{|x| x.to_i}\nxu,yu = gets.chomp.split(/ /).map!{|x| x.to_i}\n\nmint = 0\nidx = 0\nfor i in 1..n-1\n\twk = (ar[i]-xu).abs\n\twkt = ar[i]/vb+Math.sqrt(wk*wk + yu*yu)/vs\n\tif idx == 0 || mint > wkt\n\t\tmint = wkt\n\t\tmind = wk\n\t\tidx = i+1\n\telsif (mint - wkt).abs < 1e-6\n\t\tif mind > wk\n\t\t\tmint = wkt\n\t\t\tmind = wk\n\t\t\tidx = i+1\n\t\tend\n\tend\nend\nputs idx\n"}, {"source_code": "n, vb, vs = gets.split.map{|i| i.to_f}\nx = gets.split.map{|i| i.to_i}\nxu, yu = gets.split.map{|i| i.to_i}\nans = [1e100, -1, -1]\n1.upto(x.length - 1) do |i|\n\td = (xu - x[i]) ** 2 + yu ** 2\n\tt = x[i] / vb + Math.sqrt(d) / vs\n\tans = [d, t, i] if t < ans[0] - 1e-8 || (t < ans[0] + 1e-8 && d < ans[1])\nend\np ans[2] + 1\n"}, {"source_code": "n, vb, vs = gets.split.map{|i| i.to_i}\nx = gets.split.map{|i| i.to_i}\nxu, yu = gets.split.map{|i| i.to_i}\nbest = 1e100\nans = -1\nx.each_index do |i|\n\ttemp = x[i] / vb + Math.sqrt(yu * yu + (xu - x[i]) * (xu - x[i])) / vs\n\tans, best = i, temp if temp < best\nend\np ans\n"}, {"source_code": "n, vb, vs = gets.split.map{|i| i.to_f}\nx = gets.split.map{|i| i.to_i}\nxu, yu = gets.split.map{|i| i.to_i}\nx = 1.upto(x.length - 1).map do |i|\n\td = (xu - x[i]) ** 2 + yu ** 2\n\tt = x[i] / vb + Math.sqrt(d) / vs\n\t[t, -d, i + 1]\nend\np x.min[2]\n"}, {"source_code": "n, vb, vs = gets.split.map{|i| i.to_i}\nx = gets.split.map{|i| i.to_i}\nxu, yu = gets.split.map{|i| i.to_i}\nbest = 1e100\nans = -1\n1.upto(x.length - 1) do |i|\n\ttemp = x[i] / vb + Math.sqrt((xu - x[i]) * (xu - x[i]) + yu * yu) / vs\n\tans, best = i, temp if temp <= best + 1e-8\nend\nprint ans + 1, \"\\n\"\n"}, {"source_code": "n, vb, vs = gets.split.map{|i| i.to_i}\nx = gets.split.map{|i| i.to_i}\nxu, yu = gets.split.map{|i| i.to_i}\nbest = 1e100\nans = -1\nx.each_index do |i|\n\ttemp = x[i] / vb + Math.sqrt(yu * yu + (xu - x[i]) * (xu - x[i])) / vs\n\tans, best = i, temp if temp < best\nend\nprint ans + 1, \"\\n\"\n"}, {"source_code": "n,vb,vs=STDIN.gets.chomp.split\nxn=STDIN.gets.chomp.split\nxu,yu=STDIN.gets.chomp.split\nn=n.to_i\nvb=vb.to_i\nvs=vs.to_i\nxu=xu.to_i\nyu=yu.to_i\nfor i in 0...xn.length do xn[i]=xn[i].to_i end\nxn<temp_t\n t=temp_t\n res = i+2\n end\nend\nputs res\n"}, {"source_code": "n,vb,vs=gets.split.map &:to_i\nz=[1e9,1e9,0];\na=gets.split.map &:to_i\nx,y=gets.split.map &:to_i\nn.times{|i|d=((x-a[i])**2+y**2)**0.5;z=[[d/vs+x[i]/vb,d,i],z].min}\nprint z[2]+1"}, {"source_code": "def dist(x1,y1,x2,y2)\n\tMath.sqrt(((x2-x1)**2) + ((y2-y1)**2))\nend\n\nn,v_b,v_s = gets.split(\" \").map(&:to_i)\narr = gets.split(\" \").map(&:to_i)\nuniv_x,univ_y = gets.split(\" \").map(&:to_i)\n\nans = {}\n\narr[1..-1].each_with_index do |a,i|\n\tbus_time = a/v_b\n\twalk_time = (dist(univ_x,univ_y,a,0))/v_s\n\tans[[bus_time + walk_time,walk_time]] = i\nend\n# puts \"#{ans}\"\n\nkey = ans.keys.min\n\nputs \"#{ans[key]+2}\""}, {"source_code": "#$stdin = File.open('in.txt', 'r')\n@n,@vb,@vs = gets.split(/\\s+/).map {|x| x.to_i}\n@stopes = gets.split(/\\s+/).map {|x| x.to_i}\n@xu,@yu = gets.split(/\\s+/).map {|x| x.to_i}\n\n@stopes.unshift(0)\na = 2\nb = @n\n\n\ndef compute(index)\n @stopes[index]/@vb.to_f + Math.sqrt(@yu*@yu+(@stopes[index]-@xu)*(@stopes[index]-@xu))/@vs\nend\n\nav = compute(a)\nbv = compute(b)\n\nwhile b > a\n if av > bv\n a = ((b+a) / 2) + 1\n av = compute(a)\n else\n b = ((b+a) / 2)\n bv = compute(b)\n end\nend\n# if (b-a) == 1\n# mv = compute(a+1)\n# if mv < av\n# a += 1\n# av = mv\n# end\n# end\n#puts \"#{av} #{bv}\"\nif av > bv\n puts b\nelse bv > av\n puts a \n# else\n# puts a\n # av = (@stopes[a]-@xu).abs\n # bv = (@stopes[b]-@xu).abs\n # puts av > bv ? b : a\nend\n"}, {"source_code": "@n,@vb,@vs = gets.split(/\\s+/).map {|x| x.to_i}\n@stopes = gets.split(/\\s+/).map {|x| x.to_i}\n@xu,@yu = gets.split(/\\s+/).map {|x| x.to_i}\n\n@stopes.unshift(0)\na = 1\nb = @n\n\n\ndef compute(index)\n @stopes[index]/@vb.to_f + Math.sqrt(@yu*@yu+(@stopes[index]-@xu)*(@stopes[index]-@xu))/@vs\nend\n\nav = compute(a)\nbv = compute(b)\n\nwhile (b-a) > 1 \n if av > bv\n a = ((b+a) / 2)\n else\n b = ((b+a) / 2)+1\n end\n\n av = compute(a)\n bv = compute(b)\nend\n#puts \"#{av} #{bv}\"\nif av > bv\n puts b\nelsif bv > av\n puts a\nelse\n av = (@stopes[a]-@xu).abs\n bv = (@stopes[b]-@xu).abs\n puts av > bv ? b : a\nend\n"}, {"source_code": "#$stdin = File.open('in.txt', 'r')\n@n,@vb,@vs = gets.split(/\\s+/).map {|x| x.to_i}\n@stopes = gets.split(/\\s+/).map {|x| x.to_i}\n@xu,@yu = gets.split(/\\s+/).map {|x| x.to_i}\n\n@stopes.unshift(0)\na = 2\nb = @n\n\n\ndef compute(index)\n @stopes[index]/@vb.to_f + Math.sqrt(@yu*@yu+(@stopes[index]-@xu)*(@stopes[index]-@xu))/@vs\nend\n\nav = compute(a)\nbv = compute(b)\n\nwhile (b-a) > 1 \n if av > bv\n a = ((b+a) / 2)\n else\n b = ((b+a) / 2)\n end\n\n av = compute(a)\n bv = compute(b)\nend\nif (b-a) == 1\n mv = compute(a+1)\n if mv < av\n a += 1\n av = mv\n end\nend\n#puts \"#{av} #{bv}\"\nif av > bv\n puts b\nelse bv > av\n puts a \n# else\n# puts a\n # av = (@stopes[a]-@xu).abs\n # bv = (@stopes[b]-@xu).abs\n # puts av > bv ? b : a\nend\n"}, {"source_code": "@n,@vb,@vs = gets.split(/\\s+/).map {|x| x.to_i}\n@stopes = gets.split(/\\s+/).map {|x| x.to_i}\n@xu,@yu = gets.split(/\\s+/).map {|x| x.to_i}\n\n@stopes.unshift(0)\na = 2\nb = @n\n\n\ndef compute(index)\n @stopes[index]/@vb.to_f + Math.sqrt(@yu*@yu+(@stopes[index]-@xu)*(@stopes[index]-@xu))/@vs\nend\n\nav = compute(a)\nbv = compute(b)\n\nwhile (b-a) > 1 \n if av > bv\n a = ((b+a) / 2)\n else\n b = ((b+a) / 2)\n end\n\n av = compute(a)\n bv = compute(b)\nend\n#puts \"#{av} #{bv}\"\nif av > bv\n puts b\nelsif bv > av\n puts a\nelse\n av = (@stopes[a]-@xu).abs\n bv = (@stopes[b]-@xu).abs\n puts av > bv ? b : a\nend\n"}, {"source_code": "#$stdin = File.open('in.txt', 'r')\n@n,@vb,@vs = gets.split(/\\s+/).map {|x| x.to_i}\n@stopes = gets.split(/\\s+/).map {|x| x.to_i}\n@xu,@yu = gets.split(/\\s+/).map {|x| x.to_i}\n\n@stopes.unshift(0)\na = 2\nb = @n\n\n\ndef compute(index)\n @stopes[index]/@vb.to_f + Math.sqrt(@yu*@yu+(@stopes[index]-@xu)*(@stopes[index]-@xu))/@vs\nend\n\nav = compute(a)\nbv = compute(b)\n\nwhile (b-a) > 1 \n if av > bv\n a = ((b+a) / 2)\n else\n b = ((b+a) / 2)\n end\n\n av = compute(a)\n bv = compute(b)\nend\nif (b-a) == 1\n mv = compute(a+1)\n if mv < av\n a += 1\n av = mv\n end\nend\n#puts \"#{av} #{bv}\"\nif av > bv\n puts b\nelsif bv > av\n puts a \nelse\n av = (@stopes[a]-@xu).abs\n bv = (@stopes[b]-@xu).abs\n puts av > bv ? b : a\nend\n"}, {"source_code": "#$stdin = File.open('in.txt', 'r')\n@n,@vb,@vs = gets.split(/\\s+/).map {|x| x.to_i}\n@stopes = gets.split(/\\s+/).map {|x| x.to_i}\n@xu,@yu = gets.split(/\\s+/).map {|x| x.to_i}\n\n@stopes.unshift(0)\na = 2\nb = @n\n\n\ndef compute(index)\n @stopes[index]/@vb.to_f + Math.sqrt(@yu*@yu+(@stopes[index]-@xu)*(@stopes[index]-@xu))/@vs\nend\n\nav = compute(a)\nbv = compute(b)\n\nwhile b > a\n # puts \"a #{a} #{av}\"\n # puts \"b #{b} #{bv}\"\n if bv > av\n b = ((b+a) / 2)\n bv = compute(b)\n else\n a = ((b+a) / 2) + 1\n av = compute(a)\n end\nend\n#puts \"#{a} #{b}\"\n# if (b-a) == 1\n# mv = compute(a+1)\n# if mv < av\n# a += 1\n# av = mv\n# end\n# end\n#puts \"#{av} #{bv}\"\nif av > bv\n puts b\nelse #if bv > av\n puts a \n# else\n# av = (@stopes[a]-@xu).abs\n# bv = (@stopes[b]-@xu).abs\n# puts bv > av ? a : b\nend\n"}, {"source_code": "#� ������ ������ ���� ��� ����� �����: 2???n? ??100, 1???vb,?vs? ??1000. �� ������ ������ ���� n ��������������� ����� ����� � ������� �����������\n#: ���������� xi ��������� ����� i. �������������, ��� x1 ����� ����, � xn? ??105. \n#� ������� ������ ���� ���������� ������������, ����� ����� xu � yu, �� ������ �� ����������� 105.\n\n\nn, vb, vs = gets.split(' ').map{|z|z.to_i}\ncrd = gets.split(' ').map{|z|z.to_i}\nxu,yu = gets.split(' ').map{|z|z.to_i}\n\nmin = 105000000\nres= 0\ncrd[1..-1].each_with_index{|xi,i| \n d = Math.sqrt(xi*xi)*vs+Math.sqrt((xu-xi)*(xu-xi)+yu*yu)*vb\n if d < min then\n min = d\n res = i+1\n end\n}\n\nputs res+1"}], "src_uid": "15fa49860e978d3b3fb7a20bf9f8aa86"} {"nl": {"description": "Petya once wrote a sad love song and shared it to Vasya. The song is a string consisting of lowercase English letters. Vasya made up $$$q$$$ questions about this song. Each question is about a subsegment of the song starting from the $$$l$$$-th letter to the $$$r$$$-th letter. Vasya considers a substring made up from characters on this segment and repeats each letter in the subsegment $$$k$$$ times, where $$$k$$$ is the index of the corresponding letter in the alphabet. For example, if the question is about the substring \"abbcb\", then Vasya repeats letter 'a' once, each of the letters 'b' twice, letter 'c\" three times, so that the resulting string is \"abbbbcccbb\", its length is $$$10$$$. Vasya is interested about the length of the resulting string.Help Petya find the length of each string obtained by Vasya.", "input_spec": "The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1\\leq n\\leq 100\\,000$$$, $$$1\\leq q \\leq 100\\,000$$$) — the length of the song and the number of questions. The second line contains one string $$$s$$$ — the song, consisting of $$$n$$$ lowercase letters of English letters. Vasya's questions are contained in the next $$$q$$$ lines. Each line contains two integers $$$l$$$ and $$$r$$$ ($$$1 \\leq l \\leq r \\leq n$$$) — the bounds of the question.", "output_spec": "Print $$$q$$$ lines: for each question print the length of the string obtained by Vasya.", "sample_inputs": ["7 3\nabacaba\n1 3\n2 5\n1 7", "7 4\nabbabaa\n1 3\n5 7\n6 6\n2 4", "13 7\nsonoshikumiwo\n1 5\n2 10\n7 7\n1 13\n4 8\n2 5\n3 9"], "sample_outputs": ["4\n7\n11", "5\n4\n1\n5", "82\n125\n9\n191\n62\n63\n97"], "notes": "NoteIn the first example Vasya is interested in three questions. In the first question Vasya considers the substring \"aba\", that transforms to \"abba\", so the answer is equal to $$$4$$$. In the second question Vasya considers \"baca\", that transforms to \"bbaccca\", so the answer is $$$7$$$. In the third question Vasya considers the string \"abacaba\",that transforms to \"abbacccabba\" of length $$$11$$$."}, "positive_code": [{"source_code": "inputs = STDIN.read.strip.split(\"\\n\")\r\ninputs.shift\r\n\r\nclass NumArray\r\n def initialize(nums)\r\n @x = nums.clone\r\n @x.unshift(0)\r\n (1...@x.size).each do |i|\r\n @x[i] += @x[i-1]\r\n end\r\n end\r\n\r\n def sum_range(left, right)\r\n @x[right+1] - @x[left]\r\n end\r\nend\r\n\r\nnums = []\r\ninputs.shift.bytes.each do |b|\r\n nums << b - 96\r\nend\r\n\r\nnums.unshift(0)\r\nnums = NumArray.new(nums)\r\n\r\ninputs.each do |inp|\r\n from, to = inp.split(' ').map(&:to_i)\r\n\r\n puts nums.sum_range(from, to)\r\nend"}, {"source_code": "n,q=gets.split.map(&:to_i)\r\ns=gets.chomp.chars.map{|x|x.ord-96}\r\nt=[0]\r\ns.each do |ss|\r\n t << t[-1]+ss\r\nend\r\nq.times do\r\n l,r=gets.split.map(&:to_i)\r\n puts t[r]-t[l-1]\r\nend\r\n"}], "negative_code": [], "src_uid": "461378e9179c9de454674ea9dc49c56c"} {"nl": {"description": "You have k pieces of laundry, each of which you want to wash, dry and fold. You are at a laundromat that has n1 washing machines, n2 drying machines and n3 folding machines. Each machine can process only one piece of laundry at a time. You can't dry a piece of laundry before it is washed, and you can't fold it before it is dried. Moreover, after a piece of laundry is washed, it needs to be immediately moved into a drying machine, and after it is dried, it needs to be immediately moved into a folding machine.It takes t1 minutes to wash one piece of laundry in a washing machine, t2 minutes to dry it in a drying machine, and t3 minutes to fold it in a folding machine. Find the smallest number of minutes that is enough to wash, dry and fold all the laundry you have.", "input_spec": "The only line of the input contains seven integers: k, n1, n2, n3, t1, t2, t3 (1 ≤ k ≤ 104; 1 ≤ n1, n2, n3, t1, t2, t3 ≤ 1000).", "output_spec": "Print one integer — smallest number of minutes to do all your laundry.", "sample_inputs": ["1 1 1 1 5 5 5", "8 4 3 2 10 5 2"], "sample_outputs": ["15", "32"], "notes": "NoteIn the first example there's one instance of each machine, each taking 5 minutes to complete. You have only one piece of laundry, so it takes 15 minutes to process it.In the second example you start washing first two pieces at moment 0. If you start the third piece of laundry immediately, then by the time it is dried, there will be no folding machine available, so you have to wait, and start washing third piece at moment 2. Similarly, you can't start washing next piece until moment 5, since otherwise there will be no dryer available, when it is washed. Start time for each of the eight pieces of laundry is 0, 0, 2, 5, 10, 10, 12 and 15 minutes respectively. The last piece of laundry will be ready after 15 + 10 + 5 + 2 = 32 minutes."}, "positive_code": [{"source_code": "$n = Array.new(3)\n$t = Array.new(3)\nk, $n[0], $n[1], $n[2], $t[0], $t[1], $t[2] = gets.split.map &:to_i\n\n$now = Array.new(k) { 0 }\n\n1.upto(2) do |i|\n $t[i] += $t[i - 1]\nend\n\ndef make (q, p)\n x = q + 1 - $n[p]\n time = 0\n if x >= 0\n time = $now[x]\n time += $t[p]\n elsif p > 0\n time += $t[p - 1]\n end\n time\nend\n\n$now[0] = 0\n\n0.upto(k - 2) do |i|\n ans = []\n ans << make(i, 2) - $t[1] << make(i, 1) - $t[0] << make(i, 0)\n st = ans.max\n $now[i + 1] = st\nend\nputs ($now[k - 1] + $t[2])"}, {"source_code": "n=Array.new(3)\nt=Array.new(3)\nk,n[0],n[1],n[2],t[0],t[1],t[2]=gets.split.map(&:to_i)\n\nstarts=Array.new(k){0}\n\ndef when_free (k, n, t, starts, nnum)\n\tnum=n[nnum]\n\tx=k+1-num\n\t\n\ttime=0\n\tif(x>=0)\n\t\ttime=starts[x]\n\t\n\t\tif (nnum==2)\n\t\t\ttime+=(t[0]+t[1]+t[2])\n\t\tend\n\n\t\tif (nnum==1)\n\t\t\ttime+=(t[0]+t[1])\n\t\tend\n\n\t\tif(nnum==0)\n\t\t\ttime+=t[0]\n\t\tend\n\telse\n\t\tif (nnum==2)\n\t\t\ttime+=(t[0]+t[1])\n\t\tend\n\n\t\tif (nnum==1)\n\t\t\ttime+=(t[0])\n\t\tend\n\tend\n\ttime\nend\n\nstarts[0]=0\nfor i in 1...k do \n\tt2free=when_free(i-1,n,t,starts,2)\n\tt1free=when_free(i-1,n,t,starts,1)\n\tt0free=when_free(i-1,n,t,starts,0)\n\n\tstart=t2free-t[1]-t[0]\n\tif (t1free>start+t[0])\n\t\tstart=t1free-t[0]\n\tend\n\n\tif (t0free>start)\n\t\tstart=t0free\n\tend\n\tstarts[i]=start\nend\n\nputs (starts[k-1]+t[0]+t[1]+t[2])\n\n"}], "negative_code": [], "src_uid": "dd1d166772ee06b383d4ceb94b530fd1"} {"nl": {"description": "A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, sequence [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] are not.A fixed point of a function is a point that is mapped to itself by the function. A permutation can be regarded as a bijective function. We'll get a definition of a fixed point in a permutation. An integer i is a fixed point of permutation a0, a1, ..., an - 1 if and only if ai = i. For example, permutation [0, 2, 1] has 1 fixed point and permutation [0, 1, 2] has 3 fixed points.You are given permutation a. You are allowed to swap two elements of the permutation at most once. Your task is to maximize the number of fixed points in the resulting permutation. Note that you are allowed to make at most one swap operation.", "input_spec": "The first line contains a single integer n (1 ≤ n ≤ 105). The second line contains n integers a0, a1, ..., an - 1 — the given permutation.", "output_spec": "Print a single integer — the maximum possible number of fixed points in the permutation after at most one swap operation.", "sample_inputs": ["5\n0 1 3 4 2"], "sample_outputs": ["3"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\narray = gets.chop!.split(' ').map(&:to_i)\nfixed_points, remain_points = [], []\narray.each_with_index do |e, i|\n fixed_points.push e if e == i\n remain_points.push [e, i] if e != i\nend\nresult = fixed_points.size\nremain_points.each do |point|\n if array[point[0]] == point[1]\n result += 2\n break\n end\nend\nresult += 1 if result == fixed_points.size && remain_points.size != 0\nputs result"}, {"source_code": "n = gets.to_i\narray = gets.chop!.split(' ').map(&:to_i)\nfixed_points, remain_points, plus = [], [], 0\narray.each_with_index do |e, i|\n if e == i\n fixed_points.push e\n else\n plus = 2 if array[e] == i\n plus = 1 if plus == 0\n end\nend\nputs fixed_points.size + plus"}, {"source_code": "n = gets.to_i\narray = gets.chop!.split(' ').map(&:to_i)\nfixed_points = array.select.with_index { |e, i| e == i}\nplus = array.select.with_index { |e, i| e != i && array[e] == i }.empty? ? 1 : 2\nplus -= 1 if fixed_points.size == array.size\nputs fixed_points.size + plus"}, {"source_code": "n = Integer(gets)\ninput = gets.chomp\nnums = input.split\na = Array.new\nfor i in 0...n\n\ta[i] = Integer(nums[i])\nend\nc = 0\nfor i in 0...n\n\tif a[i] == i\n\t\tc += 1\n\tend\nend\ndef match(a,i,j)\n\tif a[i] == j && a[j] == i\n\t\treturn true\n\telse\n\t\treturn false\n\tend\nend\nd = 0\nfor i in 0...n\n\tj = a[i]\n\t\tif a[i] == j && a[j] == i && i != j\n\t\t\td = 1\n\t\t\tbreak\n\t\tend\n\t\nend\nif c == n\n\tputs n\nelse\n\tif d == 1\n\t\tputs c + 2\n\telse\n\t\tputs c + 1\n\tend\nend\n"}, {"source_code": "def main()\n\tn=gets.chomp.to_i\n\tar=gets.chomp.split(\" \").map{|i| i.to_i}\n\tans=0\n\tkey=[]\n\tfor i in 0...n\n\t\tif i==ar[i]\n\t\t\tans+=1\n\t\telse\n\t\t\tkey< i && a[v] == i\n double_swap = true\n end\nend\ncount += double_swap ? 2 : 1 if count < n\nputs count"}, {"source_code": "n = gets.to_i\np = gets.split.map { |x| x.to_i }\n\ncnt = 0\nflag = false\nn.times do |i|\n\tif p[i] == i\n\t\tcnt += 1\n\telsif p[p[i]] == i\n\t\tflag = true\n\tend\nend\n\nif cnt == n\n\tputs cnt\nelsif flag\n\tputs cnt + 2\nelse\n\tputs cnt + 1\nend"}], "negative_code": [{"source_code": "n = gets.to_i\np = gets.split.map { |x| x.to_i }\n\ncnt = 0\nflag = false\nn.times do |i|\n\tif p[i] == i\n\t\tcnt += 1\n\telsif p[p[i]] == i\n\t\tputs i, p[i], p[p[i]]\n\t\tflag = true\n\tend\nend\n\nif cnt == n\n\tputs cnt\nelsif flag\n\tputs cnt + 2\nelse\n\tputs cnt + 1\nend"}], "src_uid": "e63de0fffd00b2da103545a7f1e405be"} {"nl": {"description": "This problem is same as the next one, but has smaller constraints.It was a Sunday morning when the three friends Selena, Shiro and Katie decided to have a trip to the nearby power station (do not try this at home). After arriving at the power station, the cats got impressed with a large power transmission system consisting of many chimneys, electric poles, and wires. Since they are cats, they found those things gigantic.At the entrance of the station, there is a map describing the complicated wiring system. Selena is the best at math among three friends. He decided to draw the map on the Cartesian plane. Each pole is now a point at some coordinates $$$(x_i, y_i)$$$. Since every pole is different, all of the points representing these poles are distinct. Also, every two poles are connected with each other by wires. A wire is a straight line on the plane infinite in both directions. If there are more than two poles lying on the same line, they are connected by a single common wire.Selena thinks, that whenever two different electric wires intersect, they may interfere with each other and cause damage. So he wonders, how many pairs are intersecting? Could you help him with this problem?", "input_spec": "The first line contains a single integer $$$n$$$ ($$$2 \\le n \\le 50$$$) — the number of electric poles. Each of the following $$$n$$$ lines contains two integers $$$x_i$$$, $$$y_i$$$ ($$$-10^4 \\le x_i, y_i \\le 10^4$$$) — the coordinates of the poles. It is guaranteed that all of these $$$n$$$ points are distinct.", "output_spec": "Print a single integer — the number of pairs of wires that are intersecting.", "sample_inputs": ["4\n0 0\n1 1\n0 3\n1 2", "4\n0 0\n0 2\n0 4\n2 0", "3\n-1 -1\n1 0\n3 1"], "sample_outputs": ["14", "6", "0"], "notes": "NoteIn the first example: In the second example: Note that the three poles $$$(0, 0)$$$, $$$(0, 2)$$$ and $$$(0, 4)$$$ are connected by a single wire.In the third example: "}, "positive_code": [{"source_code": "# coding: utf-8\nn = readline.to_i\npoles = (0...n).map { |_| readline.split.map(&:to_i) }\n\ndef c2(n)\n n * (n-1) / 2\nend\n\nlines = {}\nlines.default = 0\nparallels = {}\nparallels.default = 0\nfor i in 0...n do\n x1, y1 = poles[i]\n for j in i+1...n do\n x2, y2 = poles[j]\n a = y1 - y2\n b = x2 - x1\n if a < 0 or (a == 0 and b < 0)\n a = -a\n b = -b\n end\n d = a.gcd b\n a /= d\n b /= d\n c = a*x1 + b*y1\n line = [a, b, c]\n STDERR.print \"(#{x1},#{y1})—(#{x2},#{y2}): #{a}x+#{b}y+#{c}=0\\n\"\n lines[line] += 1\n parallels[[a, b]] += 1 if lines[line] == 1\n end\nend\n\nr = c2(lines.length)\nparallels.values.each { |x| r -= c2(x) }\np r\n"}, {"source_code": "n = readline.to_i\npoles = (0...n).map { |_| readline.split.map(&:to_i) }\n\ndef line_x(x1, y1, x2, y2)\n if y1 == y2\n [nil, nil]\n else\n slope = Rational(x1-x2, y1-y2)\n [slope, x1 - slope * y1]\n end\nend\n\ndef c2(n)\n n * (n-1) / 2\nend\n\nlines = {}\nlines.default = 0\nparallels = {}\nparallels.default = 0\nfor i in 0...n do\n x1, y1 = poles[i]\n for j in i+1...n do\n x2, y2 = poles[j]\n sx, lx = line_x(x1, y1, x2, y2)\n sy, ly = line_x(y1, x1, y2, x2)\n line = [sx, lx, ly]\n lines[line] += 1\n parallels[[sx,sy]] += 1 if lines[line] == 1\n end\nend\n\nr = c2(lines.length)\nparallels.values.each { |x| r -= c2(x) }\np r\n"}], "negative_code": [{"source_code": "n = readline.to_i\npoles = (0...n).map { |_| readline.split.map(&:to_i) }\n\ndef c2(n)\n n * (n-1) / 2\nend\n\nlines = {}\nlines.default = 0\nparallels = {}\nparallels.default = 0\nfor i in 0...n do\n x1, y1 = poles[i]\n for j in i+1...n do\n x2, y2 = poles[j]\n a = y1 - y2\n b = x2 - x1\n d = a.gcd b\n a /= d\n b /= d\n c = a*x1 + b*y1\n line = [a, b, c]\n lines[line] += 1\n parallels[[a, b]] += 1 if lines[line] == 1\n end\nend\n\nr = c2(lines.length)\nparallels.values.each { |x| r -= c2(x) }\np r\n"}, {"source_code": "n = readline.to_i\npoles = (0...n).map { |_| readline.split.map(&:to_i) }\n\ndef line_x(x1, y1, x2, y2)\n if y1 == y2\n [nil, nil]\n else\n slope = Rational((x1-x2)/(y1-y2))\n [slope, x1 - slope * y1]\n end\nend\n\ndef c2(n)\n n * (n-1) / 2\nend\n\nlines = {}\nlines.default = 0\nparallels = {}\nparallels.default = 0\nfor i in 0...n do\n x1, y1 = poles[i]\n for j in i+1...n do\n x2, y2 = poles[j]\n sx, lx = line_x(x1, y1, x2, y2)\n sy, ly = line_x(y1, x1, y2, x2)\n line = [sx, sy, lx, ly]\n lines[line] += 1\n parallels[[sx, sy]] += 1 if lines[line] == 1\n end\nend\n\nr = c2(lines.length)\nparallels.values.each { |x| r -= c2(x) }\np r\n"}], "src_uid": "8c2e0cd780cf9390e933e28e57643cba"} {"nl": {"description": "Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees.There are $$$n$$$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $$$a_1, a_2, \\dots, a_n$$$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $$$i$$$ and decrease the level of moisture of the trees $$$1, 2, \\dots, i$$$ by $$$1$$$. Choose a position $$$i$$$ and decrease the level of moisture of the trees $$$i, i + 1, \\dots, n$$$ by $$$1$$$. Increase the level of moisture of all trees by $$$1$$$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $$$0$$$.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 2 \\cdot 10^4$$$)  — the number of test cases. The description of $$$t$$$ test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 200\\,000$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$-10^9 \\leq a_i \\leq 10^9$$$) — the initial levels of trees moisture. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$200\\,000$$$.", "output_spec": "For each test case output a single integer — the minimum number of actions. It can be shown that the answer exists.", "sample_inputs": ["4\n3\n-2 -2 -2\n3\n10 4 7\n4\n4 -4 4 -4\n5\n1 -2 3 -4 5"], "sample_outputs": ["2\n13\n36\n33"], "notes": "NoteIn the first test case it's enough to apply the operation of adding $$$1$$$ to the whole array $$$2$$$ times. In the second test case you can apply the operation of decreasing $$$4$$$ times on the prefix of length $$$3$$$ and get an array $$$6, 0, 3$$$. After that apply the operation of decreasing $$$6$$$ times on the prefix of length $$$1$$$ and $$$3$$$ times on the suffix of length $$$1$$$. In total, the number of actions will be $$$4 + 6 + 3 = 13$$$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $$$13$$$. "}, "positive_code": [{"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n a = gets.chomp.split.map(&:to_i)\r\n ans = 0\r\n cur = a[0]\r\n down = 0\r\n (1..n-1).each do |i|\r\n ans += [cur - (a[i] - down), (a[i] - down) - cur].max\r\n down += [(a[i] - down) - cur, 0].max\r\n cur = a[i] - down\r\n end\r\n puts ans + (cur > 0 ? cur : -cur)\r\n \r\nend"}, {"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n a = gets.chomp.split.map(&:to_i)\r\n ans = 0\r\n cur = a[0]\r\n down = 0\r\n for i in 1..n-1\r\n ans += [cur - (a[i] - down), (a[i] - down) - cur].max\r\n down += [(a[i] - down) - cur, 0].max\r\n cur = a[i] - down\r\n end\r\n puts ans + (cur > 0 ? cur : -cur)\r\n \r\nend"}, {"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n a = gets.chomp.split.map(&:to_i)\r\n ans = 0\r\n cur = a[0]\r\n down = 0\r\n for i in 1..n-1\r\n if(cur >= a[i] - down)\r\n ans += cur - (a[i] - down)\r\n cur = a[i] - down\r\n else\r\n ans += (a[i] - down) - cur\r\n down += (a[i] - down) - cur\r\n end\r\n end\r\n ans += cur > 0 ? cur : -cur\r\n \r\n puts ans\r\nend"}], "negative_code": [], "src_uid": "f54c1448a279e59f96847a158f993101"} {"nl": {"description": "Let's call a number a binary decimal if it's a positive integer and all digits in its decimal notation are either $$$0$$$ or $$$1$$$. For example, $$$1\\,010\\,111$$$ is a binary decimal, while $$$10\\,201$$$ and $$$787\\,788$$$ are not.Given a number $$$n$$$, you are asked to represent $$$n$$$ as a sum of some (not necessarily distinct) binary decimals. Compute the smallest number of binary decimals required for that.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$), denoting the number of test cases. The only line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^9$$$), denoting the number to be represented.", "output_spec": "For each test case, output the smallest number of binary decimals required to represent $$$n$$$ as a sum.", "sample_inputs": ["3\n121\n5\n1000000000"], "sample_outputs": ["2\n5\n1"], "notes": "NoteIn the first test case, $$$121$$$ can be represented as $$$121 = 110 + 11$$$ or $$$121 = 111 + 10$$$.In the second test case, $$$5$$$ can be represented as $$$5 = 1 + 1 + 1 + 1 + 1$$$.In the third test case, $$$1\\,000\\,000\\,000$$$ is a binary decimal itself, thus the answer is $$$1$$$."}, "positive_code": [{"source_code": "$n = gets.chomp.to_i\n$n.times do\n s = gets.chomp.to_i\n ans = 0\n while s > 0\n ans = [ans, s % 10].max\n s = s / 10\n end\n puts ans\nend\n \t\t \t\t \t\t \t\t \t\t\t\t \t"}, {"source_code": "gets.to_i.times do |i|\r\n puts gets.chomp.chars.max\r\nend"}], "negative_code": [], "src_uid": "1a6881aeb197b8ed429f46850eb27b9c"} {"nl": {"description": "A rectangle with its opposite corners in $$$(0, 0)$$$ and $$$(w, h)$$$ and sides parallel to the axes is drawn on a plane.You are given a list of lattice points such that each point lies on a side of a rectangle but not in its corner. Also, there are at least two points on every side of a rectangle.Your task is to choose three points in such a way that: exactly two of them belong to the same side of a rectangle; the area of a triangle formed by them is maximum possible. Print the doubled area of this triangle. It can be shown that the doubled area of any triangle formed by lattice points is always an integer.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of testcases. The first line of each testcase contains two integers $$$w$$$ and $$$h$$$ ($$$3 \\le w, h \\le 10^6$$$) — the coordinates of the corner of a rectangle. The next two lines contain the description of the points on two horizontal sides. First, an integer $$$k$$$ ($$$2 \\le k \\le 2 \\cdot 10^5$$$) — the number of points. Then, $$$k$$$ integers $$$x_1 < x_2 < \\dots < x_k$$$ ($$$0 < x_i < w$$$) — the $$$x$$$ coordinates of the points in the ascending order. The $$$y$$$ coordinate for the first line is $$$0$$$ and for the second line is $$$h$$$. The next two lines contain the description of the points on two vertical sides. First, an integer $$$k$$$ ($$$2 \\le k \\le 2 \\cdot 10^5$$$) — the number of points. Then, $$$k$$$ integers $$$y_1 < y_2 < \\dots < y_k$$$ ($$$0 < y_i < h$$$) — the $$$y$$$ coordinates of the points in the ascending order. The $$$x$$$ coordinate for the first line is $$$0$$$ and for the second line is $$$w$$$. The total number of points on all sides in all testcases doesn't exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each testcase print a single integer — the doubled maximum area of a triangle formed by such three points that exactly two of them belong to the same side.", "sample_inputs": ["3\n5 8\n2 1 2\n3 2 3 4\n3 1 4 6\n2 4 5\n10 7\n2 3 9\n2 1 7\n3 1 3 4\n3 4 5 6\n11 5\n3 1 6 8\n3 3 6 8\n3 1 3 4\n2 2 4"], "sample_outputs": ["25\n42\n35"], "notes": "NoteThe points in the first testcase of the example: $$$(1, 0)$$$, $$$(2, 0)$$$; $$$(2, 8)$$$, $$$(3, 8)$$$, $$$(4, 8)$$$; $$$(0, 1)$$$, $$$(0, 4)$$$, $$$(0, 6)$$$; $$$(5, 4)$$$, $$$(5, 5)$$$. The largest triangle is formed by points $$$(0, 1)$$$, $$$(0, 6)$$$ and $$$(5, 4)$$$ — its area is $$$\\frac{25}{2}$$$. Thus, the doubled area is $$$25$$$. Two points that are on the same side are: $$$(0, 1)$$$ and $$$(0, 6)$$$."}, "positive_code": [{"source_code": "area = ->(x1, y1, x2, y2, x3, y3) {\r\n (x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)).abs\r\n}\r\n\r\ngets.to_i.times do\r\n w, h = gets.split.map(&:to_i)\r\n as = gets.split.drop(1).map { |x| [x.to_i, 0] }\r\n bs = gets.split.drop(1).map { |x| [x.to_i, h] }\r\n cs = gets.split.drop(1).map { |x| [0, x.to_i] }\r\n ds = gets.split.drop(1).map { |x| [w, x.to_i] }\r\n all = as + bs + cs + ds\r\n p([as, bs, cs, ds].map do |s|\r\n x1, y1 = s[0]\r\n x2, y2 = s[-1]\r\n all.map { |x3, y3| area[x1, y1, x2, y2, x3, y3] }.max\r\n end.max)\r\nend"}, {"source_code": "input = STDIN.read.split(\"\\n\")\r\n\r\n(0..input.shift.to_i - 1).to_a.reverse.each do |i|\r\n wh = input.shift.split(' ').map(&:to_i)\r\n h1a = input.shift.split(' ').map(&:to_i)\r\n h2a = input.shift.split(' ').map(&:to_i)\r\n w1a = input.shift.split(' ').map(&:to_i)\r\n w2a = input.shift.split(' ').map(&:to_i)\r\n\r\n h1 = h1a[h1a[0]] - h1a[1]\r\n h2 = h2a[h2a[0]] - h2a[1]\r\n maxh = [h1, h2].max\r\n\r\n w1 = w1a[w1a[0]] - w1a[1]\r\n w2 = w2a[w2a[0]] - w2a[1]\r\n maxw = [w1, w2].max\r\n\r\n puts [maxw * wh[0], maxh * wh[1]].max\r\nend"}], "negative_code": [], "src_uid": "2c67ee95eba7ffbbed99cb488abb5f3d"} {"nl": {"description": "Xenia lives in a city that has n houses built along the main ringroad. The ringroad houses are numbered 1 through n in the clockwise order. The ringroad traffic is one way and also is clockwise.Xenia has recently moved into the ringroad house number 1. As a result, she's got m things to do. In order to complete the i-th task, she needs to be in the house number ai and complete all tasks with numbers less than i. Initially, Xenia is in the house number 1, find the minimum time she needs to complete all her tasks if moving from a house to a neighboring one along the ringroad takes one unit of time.", "input_spec": "The first line contains two integers n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ 105). The second line contains m integers a1, a2, ..., am (1 ≤ ai ≤ n). Note that Xenia can have multiple consecutive tasks in one house.", "output_spec": "Print a single integer — the time Xenia needs to complete all tasks. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.", "sample_inputs": ["4 3\n3 2 3", "4 3\n2 3 3"], "sample_outputs": ["6", "2"], "notes": "NoteIn the first test example the sequence of Xenia's moves along the ringroad looks as follows: 1 → 2 → 3 → 4 → 1 → 2 → 3. This is optimal sequence. So, she needs 6 time units."}, "positive_code": [{"source_code": "n, m = gets.split(' ').map(&:to_i)\nar = gets.split(' ').map(&:to_i)\nputs ar.each_cons(2).select{|x,y| x>y}.count * n + ar[-1] - 1"}, {"source_code": "#!/usr/bin/ruby\nn=gets.to_i\np ([0]+gets.split.map{|e|e.to_i-1}).each_cons(2).map{|a,b|b-a<0 ? b+n-a : b-a}.reduce(:+)"}, {"source_code": "line1 = gets.chomp.split(\" \")\nn = line1[0].to_i\nm = line1[1].to_i\ntask = gets.chomp.split(\" \").map {|x| x.to_i}\npos = 1\nans = 0\nfor t in task\n if t > pos\n ans += t-pos\n elsif t < pos\n ans += t+n-pos\n end\n pos = t\nend\nputs ans\n"}, {"source_code": "def get_i\n return gets.split(' ').map(&:to_i)\nend\nn,m=get_i\na=get_i.unshift(1)\nans=0\nfor i in 1..m do\n ans+=(a[i]-a[i-1]+n)%n\nend\nputs ans"}, {"source_code": "n, m = gets.split.map { |i| i.to_i }\na = gets.split.map { |i| i.to_i }\n\ns = a[0] - 1\nfor i in 1..(a.count - 1) do\n y = a[i] - a[i - 1]\n if y < 0 then y = n + y end\n s += y\nend\n\nputs s\n"}, {"source_code": "#!/usr/bin/ruby\nn=gets.to_i\np ([0]+gets.split.map{|e|e.to_i-1}).each_cons(2).map{|a,b|b-a<0 ? b+n-a : b-a}.reduce(:+)\n"}, {"source_code": "#!/usr/bin/ruby1.9\n\nn, m = STDIN.readline.split.collect {|s| s.to_i }\ntasks = STDIN.readline.split.collect {|s| s.to_i }\n\ntotal = 0\npos = 1\ntasks.each do |next_pos|\n\tif next_pos >= pos\n\t\ttotal += next_pos - pos\n\telse\n\t\ttotal += (n - pos) + 1 + (next_pos - 1)\n\tend\n\tpos = next_pos\nend\nputs total\n\n"}, {"source_code": "hn, dn = gets.split.map(&:to_i)\ndeals = gets.split.map(&:to_i)\ntime = 0\ncurh = 1\ndeals.each {|d|\n\ttime += (d-curh)%hn\n\tcurh = d\n}\nputs time"}, {"source_code": "def string_to_int(string, delim = \" \")\n result = []\n string = string.split(delim)\n for i in 0...string.length\n result[i] = string[i].to_i\n end\n return result\nend\n\ninput = string_to_int(gets.chomp)\ndest = string_to_int(gets.chomp)\n\nwalk = dest[0] - 1\n\nfor i in 0...dest.length - 1\n\tif dest[i] <= dest[i+1]\n\t\twalk += dest[i+1] - dest[i]\n\telse\n\t\twalk += input[0] + dest[i+1] - dest[i]\n\tend\nend\n\nputs walk\n"}, {"source_code": "r = -> { gets.split.map(&:to_i )}\nn,m = r[]\nv = r[]\nv.unshift(1)\ns = 0\n(1..m).each do |x|\n if v[x - 1] < v[x]\n s += v[x] - v[x - 1]\n elsif v[x - 1] > v[x] \n s += n - v[x - 1] + v[x]\n end\nend\np s"}, {"source_code": "n,m=gets.chomp.split.map(&:to_i)\na=gets.chomp.split.map(&:to_i)\ny,ans,x=1,0,0\na.each do |i|\n\tans+=1 if i= prev\n moves += item - prev\n prev = item \n else\n moves += n - prev + item\n prev = item\n end\nend\nputs moves\n"}, {"source_code": "n,m = gets.split.map(&:to_i)\narr = gets.split.map(&:to_i)\nres = arr[0]-1\nfor i in 1...m\n if arr[i] < arr[i-1]\n res += n - (arr[i] - arr[i-1]).abs\n else\n res += arr[i] - arr[i-1];\n end\nend\n\nputs res\n\n"}, {"source_code": "n, m = gets.chomp.split(/ /).map(&:to_i)\ntasks = gets.chomp.split(/ /).map(&:to_i)\nans = 0\nprev_task = 0\n(0...m).each do |task|\n current_task = tasks[task]\n if prev_task == 0\n ans += current_task\n else\n if current_task != prev_task\n if current_task < prev_task\n ans += (n - prev_task) + current_task\n else\n ans += current_task - prev_task\n end\n end\n end\n prev_task = current_task\nend\nputs ans - 1\n"}, {"source_code": "n,m=gets.split.map{|e| e.to_i}\nx=gets.split.map{|e| e.to_i-1}\n\nans=x[0]\n(m-1).times{|i|\n\tif x[i+1]>=x[i] then\n\t\tans=ans+x[i+1]-x[i]\n\telse\n\t\tans=ans+x[i+1]+n-x[i]\n\tend\n}\nputs ans"}, {"source_code": "s = gets.split.map { |e| e.to_i }\ns2 = gets.split.map { |e| e.to_i }\nn = s[0]\nm = s[1]\nx = m[0]\nc = 0\nfor i in s2\n\tif i < x\n\t\tc +=1\n\tend\t\n\tx = i\nend\nputs (c * n) + s2[m-1] - 1\n\n"}, {"source_code": "n,m = gets.split(\" \").map { |x| x.to_i }\na = gets.split(\" \").map { |x| x.to_i }\nres = 0\nxprev = 1\na.each do |x|\n if xprev > x then\n res += n\n end\n res += x - xprev\n xprev = x\nend\nputs res"}, {"source_code": "input = gets.chomp\nnum = input.split\nn = Integer(num[0])\nm = Integer(num[1])\ninput2 = gets.chomp\nnums = input2.split\na = Array.new\nfor i in 0...m\n a[i] = Integer(nums[i])\nend\n\nb = Array.new\nb[0] = a[0]\nfor i in 0...m-1\n if a[i+1] != a[i]\n b.push(a[i+1])\n end\nend\ntime = 0\nl = b.length\nfor i in 0...l-1\n if b[i+1] < b[i]\n time += 1\n end\nend\n\nputs time * n - 1 + b[l-1]"}, {"source_code": "nm = gets.chomp\nnm = nm.split\nhouses = nm[0].to_i\ntasks = nm[1].to_i\neach_task = gets.chomp\neach_task = each_task.split\n\nhours = 0\nposition = 1\nfor i in (0..each_task.size-1)\n\tif position < each_task[i].to_i\n\t\thours += (each_task[i].to_i - position)\n\t\tposition = each_task[i].to_i\n\telsif position > each_task[i].to_i\n\t\thours += (houses - position) + each_task[i].to_i\n\t\tposition = each_task[i].to_i\n\tend\nend\n\nprint hours"}, {"source_code": "n,m = gets.split(\" \").map(&:to_i)\ntasks = gets.split(\" \").map(&:to_i)\ncost = 0\ncurrent = 1\ntasks.each do |a|\n\tif a > current\n\t\tcost += a - current\n\t\t\n\t\tcurrent = a\n\telsif a < current\n\t\tcost += (n - current) + a\n\t\tcurrent = a\n\tend\nend\nputs cost"}, {"source_code": "n, m = gets.split.map &:to_i\na = gets.split.map &:to_i\ns = 0\np = 1\n\nfor i in (0..m-1)\n a[i] < p ? s += n - p + a[i] : s += a[i] - p\n p = a[i]\nend\nputs s\n"}, {"source_code": "#!/usr/bin/env ruby\n\nn,m = gets.split.map(&:to_i)\nx = gets.split.map(&:to_i)\nnow = 1\nans = 0\nx.each do |p|\n if now <= p\n ans += p - now\n else\n ans += p + n - now\n end\n now = p\nend\nputs ans\n"}, {"source_code": "n, m = gets.strip.split.map(&:to_i)\na = gets.strip.split.map(&:to_i)\nnow = 1\ndis = 0\na.each do |x|\n if x < now\n dis += (n - (now - x))\n else\n dis += x - now\n end\n now = x\nend\nputs dis\n"}, {"source_code": "n, m = gets.split(' ').map(&:to_i)\n\ncurr = 1\nputs(\n gets.split(' ').map(&:to_i).inject(0) do |result, x|\n c = curr\n curr = x\n result + (x >= c ? x - c : n - c + x)\n end\n)\n"}, {"source_code": "n, m = gets.chomp.split.map { |x| x.to_i }\nh = [1].concat(gets.chomp.split.map { |x| x.to_i })\ncount = 0\nfor i in 1..m\n if h[i] > h[i - 1]\n count += h[i] - h[i - 1]\n elsif h[i] < h[i - 1]\n count += n - h[i - 1] + h[i]\n end\nend\nputs count"}, {"source_code": "r = gets.split(\" \")[0].to_i\ns = gets.split(\" \").map { |i| i.to_i }\n\nb = 1\nres = 0\ns.each do |i|\n\tif b < i\n\t\tres += (i-b)\n\telsif b > i\n\t\tres += (r-b+i)\n\tend\n\tb = i\nend\n\nputs res\n"}, {"source_code": "def read_line\n\tline = gets.chomp.split(' ')\n\tline.map { |elem| elem.to_i }\nend\n\ndef distance(a, b, n)\n\treturn b - a if a <= b\n\tn - a + 1 + b - 1\nend\n\nn, m = read_line\na = read_line\ncurrent = 1\nres = 0\na.each do |after|\n\tres += distance(current, after, n)\n\tcurrent = after\nend\n\nputs res\n\n"}, {"source_code": "# f = File.new('test1.txt', 'r')\nf = STDIN\nwhile s = f.gets\n n, m = s.split.map(&:to_i)\n arr = f.gets.split.map(&:to_i)\n ans, start = 0, 1\n arr.each do |e|\n if e >= start\n ans += e - start\n else\n ans += n - start + e\n end\n start = e\n end\n puts ans\nend\nf.close"}], "negative_code": [{"source_code": "def string_to_int(string, delim = \" \")\n result = []\n string = string.split(delim)\n for i in 0...string.length\n result[i] = string[i].to_i\n end\n return result\nend\n\ninput = string_to_int(gets.chomp)\ndest = string_to_int(gets.chomp)\n\nwalk = dest[0] - 1\n\nfor i in 0...dest.length - 1\n\tif dest[i] < dest[i+1]\n\t\twalk += dest[i+1] - dest[i]\n\telse\n\t\twalk += input[0] + dest[i+1] - dest[i]\n\tend\nend\n\nputs walk\n"}, {"source_code": "def string_to_int(string, delim = \" \")\n result = []\n string = string.split(delim)\n for i in 0...string.length\n result[i] = string[i].to_i\n end\n return result\nend\n\ninput = string_to_int(gets.chomp)\ndest = string_to_int(gets.chomp)\n\nhouse_no = 0\nhouses = Array.new(input[0]){|house_no| house_no+1}\nwalk = 0\n\nj = 0\nfor i in 0...dest.length\n\twhile houses[j] != dest[i]\n\t\tif j > dest.length - 1\n\t\t\tj = 0\n\t\t\twalk += 1\n\t\telse\n\t\t\tj += 1\n\t\t\twalk += 1\n\t\tend\n\tend\nend\n\nputs walk\n"}, {"source_code": "def read_line\n\tline = gets.chomp.split(' ')\n\tline.map { |elem| elem.to_i }\nend\n\ndef distance(a, b, n)\n\treturn b - a if a <= b\n\tn - a + 1 + b - 1\nend\n\nn, m = read_line\na = read_line.uniq\ncurrent = 1\nres = 0\na.each do |after|\n\tres += distance(current, after, n)\n\tcurrent = after\nend\n\nputs res\n\n"}], "src_uid": "2c9c96dc5b6f8d1f0ddeea8e07640d3e"} {"nl": {"description": "A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once.Let the fixedness of a permutation $$$p$$$ be the number of fixed points in it — the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$.You are asked to build a sequence of permutations $$$a_1, a_2, \\dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, \\dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$.For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$.Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$ — that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$ — that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$ — that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$ — that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 99$$$) — the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 \\le n \\le 100$$$) — the required length of permutations in the chain.", "output_spec": "For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, \\dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, \\dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.", "sample_inputs": ["2\n\n2\n\n3"], "sample_outputs": ["2\n1 2\n2 1\n3\n1 2 3\n3 2 1\n3 1 2"], "notes": null}, "positive_code": [{"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n array = [*1..n]\r\n p n\r\n n.times do |i|\r\n puts array * \" \"\r\n if i + 1 < n\r\n array[i], array[i + 1] = array[i + 1], array[i]\r\n end\r\n end\r\nend\r\n"}], "negative_code": [], "src_uid": "02bdd12987af6e666d4283f075f73725"} {"nl": {"description": "In this problem is used an extremely simplified version of HTML table markup. Please use the statement as a formal document and read it carefully.A string is a bHTML table, if it satisfies the grammar: TABLE ::= <table>ROWS</table>ROWS ::= ROW | ROW ROWSROW ::= <tr>CELLS</tr>CELLS ::= CELL | CELL CELLSCELL ::= <td></td> | <td>TABLE</td>Blanks in the grammar are only for purposes of illustration, in the given data there will be no spaces. The bHTML table is very similar to a simple regular HTML table in which meet only the following tags : \"table\", \"tr\", \"td\", all the tags are paired and the table contains at least one row and at least one cell in each row. Have a look at the sample tests as examples of tables.As can be seen, the tables may be nested. You are given a table (which may contain other(s)). You need to write a program that analyzes all the tables and finds the number of cells in each of them. The tables are not required to be rectangular.", "input_spec": "For convenience, input data can be separated into non-empty lines in an arbitrary manner. The input data consist of no more than 10 lines. Combine (concatenate) all the input lines into one, to get a text representation s of the specified table. String s corresponds to the given grammar (the root element of grammar is TABLE), its length does not exceed 5000. Only lower case letters are used to write tags. There are no spaces in the given string s.", "output_spec": "Print the sizes of all the tables in the non-decreasing order.", "sample_inputs": ["<table><tr><td></td></tr></table>", "<table>\n<tr>\n<td>\n<table><tr><td></td></tr><tr><td></\ntd\n></tr><tr\n><td></td></tr><tr><td></td></tr></table>\n</td>\n</tr>\n</table>", "<table><tr><td>\n<table><tr><td>\n<table><tr><td>\n<table><tr><td></td><td></td>\n</tr><tr><td></td></tr></table>\n</td></tr></table>\n</td></tr></table>\n</td></tr></table>"], "sample_outputs": ["1", "1 4", "1 1 1 3"], "notes": null}, "positive_code": [{"source_code": "TABLE_PAT = ''\nROW_PAT = ''\nCELL_PAT = '\" then stack[-1] += 1\n end\nend\n\nputs ans.sort.join(' ')\n"}, {"source_code": "a=b=[];([*$<]*'').tr(?\\n,\"\").scan(/<\\/?ta|\\d+)/=~l\n unless (row and col)\n /(?[A-Z]+)(?\\d+)/=~l\n col_ = 0\n i=0\n col.each_byte do |a|\n pos = col.length - i - 1\n col_+=('Z'.bytes.to_a.first-'A'.bytes.to_a.first+1)**pos*(a-'A'.bytes.to_a.first+1)\n i+=1\n end\n row = row.to_i\n puts \"R\"+row.to_s+\"C\"+col_.to_s\n else\n col = col.to_i\n col_ = ''\n while col!=0\n temp = col % ('Z'.bytes.to_a.first-'A'.bytes.to_a.first+1)\n if temp ==0\n temp = ('Z'.bytes.to_a.first-'A'.bytes.to_a.first)\n col-=1\n else\n temp-=1\n end\n col_+=['A'.bytes.to_a.first+temp].pack(\"U\")\n col /= ('Z'.bytes.to_a.first-'A'.bytes.to_a.first+1)\n end\n col_.reverse!\n puts col_+row\n end\n n-=1\nend\n\n"}, {"source_code": "def n2s(n)\n s = \"\"\n while n > 0\n s = (\"A\".ord + (n - 1) % 26).chr + s\n n = (n - (n - 1) % 26 - 1) / 26\n end\n return s\nend\n\ndef s2n(s)\n n = 0\n s.size.times do |i|\n n *= 26\n n += s[i].ord - \"A\".ord + 1\n end\n return n\nend\n\nn = gets.to_i\nn.times do\n str = gets\n if str =~ /^([A-Z]+)(\\d+)$/\n c_s = $1\n r = $2.to_i\n c = s2n(c_s)\n puts \"R#{r}C#{c}\"\n elsif str =~ /^R(\\d+)C(\\d+)$/\n r = $1.to_i\n c = $2.to_i\n c_s = n2s(c)\n puts \"#{c_s}#{r}\"\n end\nend\n"}, {"source_code": "def num_to_ch(num)\n str = \"\"\n until num<1\n num -= 1\n str << (65+num%26).chr\n num /= 26\n end\n str.reverse\nend\ndef ch_to_num(ch)\n num = 0\n ch.each_codepoint{|x| num = 26 * num + x - 64}\n num\nend\nn = gets.to_i\nn.times do\n input = gets.chomp\n if input =~ /R\\d+C\\d+/\n row, col = input.scan(/R(\\d+)C(\\d+)/).first.map(&:to_i)\n puts \"#{num_to_ch(col)}#{row}\"\n else\n col, row = input.scan(/([[:upper:]]+)(\\d+)/).first\n puts \"R#{row}C#{ch_to_num(col)}\"\n end\nend"}, {"source_code": "num = gets.chomp.to_i\nnum.times do\n old = gets.chomp\n results = old.match('R(\\d+)C(\\d+)')\n if results != nil\n col_num = results[2].to_i\n base = 1;\n col_str = \"\"\n while col_num > 26\n this_char = col_num % 26\n col_num /= 26\n if this_char == 0\n this_char = 26\n col_num -= 1\n end\n col_str = \"#{(this_char+64).chr}#{col_str}\"\n end\n col_str = \"#{(col_num+64).chr}#{col_str}\"\n puts \"#{col_str}#{results[1]}\"\n else\n results = old.match('([A-Z]+)(\\d+)')\n col_str = results[1]\n base = 26**(col_str.length-1)\n col_num = 0\n col_str.each_char do |c| \n col_num += (c.ord-64)*base\n base /= 26\n end\n puts \"R#{results[2]}C#{col_num}\"\n end\nend"}, {"source_code": "$table = [\"A\", \"B\", \"C\", \"D\", \"E\", \"F\", \"G\", \"H\", \"I\", \"J\", \"K\", \"L\", \"M\", \"N\", \"O\", \"P\", \"Q\", \"R\", \"S\", \"T\", \"U\", \"V\", \"W\", \"X\", \"Y\", \"Z\"]\n\ndef to_E(x)\n b = \"\"\n t = true\n while x > 26 do\n if x % 26 == 0 then\n str = to_E(x-1)\n str[-1] = \"Z\"\n b = str + b\n x = 0\n t = false\n else\n b = $table[(x % 26) - 1] + b\n x /= 26\n end\n end\n if t then\n b = $table[x-1] + b\n end\n return b\nend\n\ndef to_T(x)\n str = x.gsub(/[0123456789]/, \"*\")\n k = str.index(\"*\")\n str1 = str[0...k]\n r = 0\n q = 26\n n = str1.size\n n.times do |i|\n r += ($table.index(str1[i]) + 1)*q**(n-i-1)\n# p (n-i-1)\n# p $table.index(str1[i]) + 1\n# p r\n end\n result = \"R\" + x[k..-1] + \"C\" + r.to_s\n return result \nend\n\nn = gets.to_i\nind = []\noutd = []\nn.times do |i|\n ind[i] = gets.chomp\n r = ind[i].index(\"R\")\n c = ind[i].index(\"C\")\n str = ind[i].gsub(/[ABDEFGHIJKLMNOPQSTUVWXYZ]/, \"*\")\n k = str.index(\"*\")\n if r && c && !k && c-r>1 then\n ind[i] = to_E(ind[i][c+1..-1].to_i) + ind[i][r+1...c]\n else\n ind[i] = to_T(ind[i])\n end\nend\nn.times do |i|\n puts \"#{ind[i]}\"\nend"}, {"source_code": "def f(s)\n\tr=0\n\ts.each_byte{|c| r=r*26+c-64}\n\tr\nend\ndef g(n)\n\tr=\"\"\n\twhile n>0 do\n\t\tx=n%26\n\t\tif x==0 then\n\t\t\tx=26\n\t\t\tn-=x\n\t\tend\n\t\tr=(x+64).chr+r\n\t\tn/=26\n\tend\n\tr\nend\nn=gets.to_i\nn.times{\n\ts=gets\n\tif s=~/^([A-Z]+)(\\d+)$/ then\n\t\tputs \"R\"+$2.to_s+\"C\"+f($1).to_s\n\tend\n\tif s=~/R(\\d+)C(\\d+)/ then\n\t\tputs g($2.to_i)+$1\n\tend\n}\n"}, {"source_code": "def f(x)\nreturn x<26?((x+65).chr):(f(x/26-1)+(x%26+65).chr)\nend\na=*$<.drop(1).map(&:chomp).each{|x|puts /R(\\d+)C(\\d+)/.match(x)?(f($2.to_i-1)+$1):(y=0;v=1;/(\\D+)(\\d+)/.match(x);$1.reverse.each_char{|c|y+=(c.ord-64)*v;v*=26};\"R\"+$2+\"C\"+y.to_s)}"}, {"source_code": "def f x;x<26?((x+65).chr):f(x/26-1)+(x%26+65).chr;end;gets.to_i.times{x=gets;puts /R(\\d+)C(\\d+)/.match(x)?f($2.to_i-1)+$1:(y=0;v=1;/(\\D+)(\\d+)/.match x;$1.reverse.each_char{|c|y+=(c.ord-64)*v;v*=26};\"R#{$2}C#{y}\")}"}, {"source_code": "def f(x)\nreturn x<26?((x+65).chr):(f(x/26-1)+(x%26+65).chr)\nend\ngets.to_i.times{x=gets;puts /R(\\d+)C(\\d+)/.match(x)?(f($2.to_i-1)+$1):(y=0;v=1;/(\\D+)(\\d+)/.match(x);$1.reverse.each_char{|c|y+=(c.ord-64)*v;v*=26};\"R\"+$2+\"C\"+y.to_s)}"}, {"source_code": "n=gets.chomp.to_i\ndata=[]\n\nalpha=['A', 'B', 'C', 'D', 'E',\n\t\t'F', 'G', 'H', 'I', 'J',\n\t\t'K', 'L', 'M', 'N', 'O',\n\t\t'P', 'Q', 'R', 'S', 'T',\n\t\t'U', 'V', 'W', 'X', 'Y', 'Z']\na=0\n\nwhile a0\n\t\t\t\tb=cells%osn\n\t\t\t\tcs.push(alpha[b-1])\n\t\t\t\tif b==0\n\t\t\t\t\tcells-=(b+26)\n\t\t\t\telse\n\t\t\t\t\tcells-=b\n\t\t\t\tend\n\t\t\t\tcells/=osn\n\t\t\tend\n\t\t\t\n\t\t\tcs.reverse!\n\t\t\tcs.push(rows)\n\n\t\t\tputs cs.join\n\t\telse\n\t\t\tsn=[]\n\t\t\ta=0\n\t\t\twhile word[a].to_i==0\n\t\t\t\tsn.push(word[a])\n\t\t\t\ta+=1\n\t\t\tend\n\n\t\t\tnumber=[]\n\t\t\tfor i in(a..word.length-1)\n\t\t\t\tnumber.push(word[i])\n\t\t\tend\n\t\t\tnumb=number.join.to_i\n\n\t\t\tsn.reverse!\n\t\t\trw=0\n\t\t\tfor i in(0..sn.length-1)\n\t\t\t\trw+=(alpha.index(sn[i])+1)*osn**i\n\t\t\tend\n\n\t\t\tanswer=[]\n\t\t\tanswer.push('R')\n\t\t\tanswer.push(numb)\n\t\t\tanswer.push('C')\n\t\t\tanswer.push(rw)\n\t\t\tputs answer.join\n\t\tend\n\telse\n\t\tsn=[]\n\t\ta=0\n\t\twhile word[a].to_i==0\n\t\t\tsn.push(word[a])\n\t\t\ta+=1\n\t\tend\n\n\t\tnumber=[]\n\t\tfor i in(a..word.length-1)\n\t\t\tnumber.push(word[i])\n\t\tend\n\t\tnumb=number.join.to_i\n\n\t\tsn.reverse!\n\t\trw=0\n\t\tfor i in(0..sn.length-1)\n\t\t\trw+=(alpha.index(sn[i])+1)*osn**i\n\t\tend\n\n\t\tanswer=[]\n\t\tanswer.push('R')\n\t\tanswer.push(numb)\n\t\tanswer.push('C')\n\t\tanswer.push(rw)\n\t\tputs answer.join\n\tend\nend"}, {"source_code": "arr=(\"A\"..\"Z\").to_a\ngets.to_i.times{\n\ts=gets.chomp\n\tif /R\\d+C\\d+/.match(s) then\n\t\ts.slice!(0)\n\t\tr,c=s.split(\"C\").map{|e| e.to_i}\n\t\tstr=\"\"\n\t\tc=c-1\n\t\twhile c>25\n\t\t\tstr< 2 and s.include?('C')\n a(s)\n else\n b(s)\n end\nend\n"}, {"source_code": "d=[*?A..'BEAAA']\ndef t s\np=a=0\ns.each_char{|c|a+=(c.ord-?A.ord+1)*26**p;p+=1}\na\nend\ngets.to_i.times{l=gets;puts (if /R(\\d+)C(\\d+)/.match l;\n\"#{d[$2.to_i-1]}#{$1}\"\nelse\na,b=l.scan(/([A-Z]+)(\\d+)/)[0];\"R#{b}C#{t a.reverse}\"\nend)}\n"}, {"source_code": "d=['', *?A..'BEAAA']\n\ndef t s\n p=a=0\n s.reverse!\n \n s.each_char{|c|\n a+= (c.ord - 'A'.ord + 1) * 26**p\n p+=1\n }\n \n a\nend\n\nn=gets.to_i\n\nn.times{\n l=gets.strip\n \n if /R(\\d+)C(\\d+)/.match l\n puts \"#{d[$2.to_i]}#{$1}\"\n else\n a, b = l.scan(/([A-Z]+)([0-9]+)/)[0]\n puts \"R#{b}C#{t a}\"\n end\n}"}, {"source_code": "#=================================\n# Better be sure than sorry. \n#=================================\n$stdin = File.open(\"./input.txt\", \"r\") if ENV[\"LOGNAME\"]==\"skydos\"\n#=================================\ndigs=['', *?A..'BEAAA']\n\ndef to_numbers str\n\tans=0\n\tstr.reverse!\n\t\n\tpow=0\n\tstr.each_char{|c|\n\t\tans+= (c.ord - 'A'.ord + 1) * 26**pow\n\t\tpow+=1\n\t}\n\t\n\tans\nend\n\nn=gets.to_i\n\nn.times{\n\tline=gets.strip\n\t\n\tif /R(\\d+)C(\\d+)/.match line\n\t\tputs \"#{digs[$2.to_i]}#{$1}\"\n\telse\n\t\tp1, p2 = line.scan(/([A-Z]+)([0-9]+)/)[0]\n\t\tputs \"R#{p2}C#{to_numbers p1}\"\n\tend\n}\n"}, {"source_code": "d=[*?A..'BEAAA'];def t s;p=a=0;s.each_char{|c|a+=(c.ord-?A.ord+1)*26**p;p+=1};a;end;gets.to_i.times{l=gets;puts (if /R(\\d+)C(\\d+)/.match l;\"#{d[$2.to_i-1]}#{$1}\";else;a,b=l.scan(/([A-Z]+)(\\d+)/)[0];\"R#{b}C#{t a.reverse}\";end)}\n"}, {"source_code": "arr = STDIN.read.split(\"\\n\")\nn = arr[0].to_i\n\n1.upto(n).each do |i|\n str = arr[i]\n match = str.match(/R([0-9]+)C([0-9]+)/)\n\n if match\n row = match[2].to_i\n col = match[1]\n\n r = row\n str = \"\"\n while(r > 0) do\n val = (r-1) % 26\n str += (val + 65).chr\n r = (r-1)/26\n end\n str = str.reverse\n str += col\n puts str\n else\n match = str.match(/([A-Z]+)([0-9]+)/)\n col = match[1]\n row = match[2]\n\n s = 0\n rev = col.reverse.split(\"\")\n rev.each_index do |i|\n chr = (rev[i][0].ord - 65 + 1) * ( 26 ** i )\n s += chr\n end\n\n puts \"R#{row}C#{s}\"\n end\nend"}, {"source_code": "# # # # # # # # # # # # # # # # # # # # # # # # # # # #\n\ndef numToAZ(col)\n if col > 0\n numToAZ((col-1)/26) + ('A'.ord + (col-1)%26).chr\n else\n \"\"\n end\nend\n\ndef to_CX(row, column)\n return numToAZ(column.to_i) + row\nend\n\n# # # # # # # # # # # # # # # # # # # # # # # # # # # #\ndef to_RXCY(row, column)\n\n azLen = ('A'..'Z').to_a.length\n\n columnInt = 0\n column.reverse.each_char.each_with_index { |chr, idx| \n columnInt += (chr.ord - 'A'.ord + 1) * azLen**idx\n }\n\n return \"R#{row}C#{columnInt}\"\n\nend\n\n# # # # # # # # # # # # # # # # # # # # # # # # # # # #\ndef translate(coord)\n if coord =~ /^R(\\d+)C(\\d+)$/\n to_CX($1, $2)\n elsif coord =~ /^([A-Z]+)(\\d+)/\n to_RXCY($2, $1)\n end\nend\n\n# # # # # # # # # # # # # # # # # # # # # # # # # # # #\n# Main\n# # # # # # # # # # # # # # # # # # # # # # # # # # # #\n\ngets.to_i.times {\n puts translate(gets.strip)\n}\n"}, {"source_code": "# # # # # # # # # # # # # # # # # # # # # # # # # # # #\ndef to_1var(row, column)\n\n az = ('A'..'Z').to_a\n azLen = az.length()\n\n columnAZ = \"\"\n columnInt = column.to_i\n\n while columnInt > 0\n idx = columnInt % azLen\n columnAZ.insert(0, az[idx-1])\n columnInt = (columnInt / azLen) - ((idx == 0)? 1 : 0)\n end\n\n return columnAZ + row\n\nend\n\n# # # # # # # # # # # # # # # # # # # # # # # # # # # #\ndef to_2var(row, column)\n\n azLen = ('A'..'Z').to_a.length\n\n columnInt = 0\n column.reverse.each_char.each_with_index { |chr, idx| \n columnInt += (chr.ord - 'A'.ord + 1) * azLen**idx\n }\n\n return \"R#{row}C#{columnInt}\"\n\nend\n\n# # # # # # # # # # # # # # # # # # # # # # # # # # # #\ndef translate(coord)\n if coord =~ /^R(\\d+)C(\\d+)$/\n to_1var($1, $2)\n elsif coord =~ /^([A-Z]+)(\\d+)/\n to_2var($2, $1)\n end\nend\n\n# # # # # # # # # # # # # # # # # # # # # # # # # # # #\n# Main\n# # # # # # # # # # # # # # # # # # # # # # # # # # # #\n\ngets.to_i.times {\n puts translate(gets.strip)\n}\n"}, {"source_code": "n=gets.chomp.to_i\nn.times {\n s=gets.chomp.to_s\n s.scan(/^R([0-9]+)C([0-9]+)$/)\n unless !$~\n r=$~[1]\n c=$~[2].to_i\n remainder=-1\n g=\"\"\n while c>0\n remainder=c%26\n #print (\"#{remainder} \")\n if(remainder==0)\n g+=\"Z\"\n c-=26\n else\n g+=(remainder+64).chr\n c-=remainder\n end\n #puts (g)\n c/=26\n end\n print(g.reverse)\n puts (r)\n next\n end\n s.scan(/^([A-Z]+)([0-9]+)$/)\n unless !$~\n c=$~[1]\n r=$~[2]\n k=1\n sum=0\n i=1\n len=c.length\n while i<=len\n #puts (i)\n hold=(c.getbyte(-1*i)-64)*k\n #puts (\"hold = #{hold}\")\n sum+=hold\n k*=26\n i+=1\n end\n puts (\"R#{r}C#{sum}\")\n end\n\n}"}, {"source_code": "$itoc =['Z'] + ('A'..'Z').to_a\n$ctoi = Hash.new\n26.times{|i| $ctoi[$itoc[i+1]] = i+1}\n\ndef change str\n if str =~ /R(\\d+)C(\\d+)/\n return toAlph($2.to_i)+$1\n else\n str =~ /([A-Z]+)(\\d+)/\n return 'R'+$2+'C'+toNum($1)\n end\nend\ndef toAlph x\n ret = ''\n while x>0 do\n ret = $itoc[x%26] + ret\n x -= 26 if x%26 == 0\n x /= 26\n end\n return ret\nend\ndef toNum x\n ret = 0\n x.reverse!\n x.size.times do |i|\n ret += ($ctoi[x[i]] * (26**i))\n end\n return ret.to_s\nend\n\nn = gets.to_i\nn.times do\n puts (change gets)\nend"}, {"source_code": "class SpreadSheets\n def main\n numberOfLines = gets.to_i\n coordinates = Array.new(numberOfLines)\n\n i = 0\n while i < numberOfLines\n coordinates[i] = gets\n i += 1\n end\n\n puts convert(coordinates)\n end\n\n def convert(coordinates)\n result = Array::new\n numericDef = /R(\\d+)C(\\d+)/\n alphanumericDef = /([a-zA-Z]+)(\\d+)/\n\n coordinates.each do |coordinate|\n if (numericDef.match(coordinate))\n matches = numericDef.match(coordinate)\n row = matches[1]\n column = matches[2]\n convertedColumn = convertNumToAlpha(column.to_i)\n result.push(\"#{convertedColumn}#{row}\")\n elsif (alphanumericDef.match(coordinate))\n matches = alphanumericDef.match(coordinate)\n row = matches[2]\n column = matches[1]\n convertedColumn = convertAlphaToNum(column)\n result.push(\"R#{row}C#{convertedColumn}\")\n end\n end\n\n return result\n end\n\n def convertNumToAlpha(num)\n result = String::new\n\n zeroLetter = 'A';\n base26 = 26;\n loop do\n num -= 1;\n remainder = num % base26;\n result = \"#{(zeroLetter.ord + remainder).chr}#{result}\"\n num /= base26;\n break if (num == 0)\n end\n\n return result\n end\n\n def convertAlphaToNum(alpha)\n result = 0\n\n zeroLetter = 'A'\n currentOrderOfMagnitude = 1\n base26 = 26\n alpha.split(\"\").reverse_each do |letter|\n convertedDigit = letter.ord - zeroLetter.ord + 1\n result += currentOrderOfMagnitude * convertedDigit\n currentOrderOfMagnitude *= base26\n end\n\n return result.to_s\n end\nend\n\nSpreadSheets::new.main\n"}, {"source_code": "def to_num s\n num = 0\n s.size.times do |i|\n num *= 26\n num += s[i].ord - 'A'.ord + 1\n end\n return num\nend\n\ndef to_letters num\n s = \"\"\n while num > 0\n mod = num % 26\n if mod == 0\n mod = 26\n num -= 1\n end\n s += ('A'.ord + mod - 1).chr\n num /= 26\n end\n return s.reverse\nend\n\ngets.to_i.times do\n s = gets\n if s =~ /R\\d+C\\d+/\n r, c = s.tr(\"RC\", \" \").split.map &:to_i\n \n puts \"#{to_letters c}#{r}\"\n \n elsif s =~ /[A-Z]+\\d+/\n r = s[/\\d+/].to_i\n c = to_num(s[/[A-Z]+/])\n \n puts \"R#{r}C#{c}\"\n end\nend"}, {"source_code": "n = Integer(gets)\ncoo = Array.new\nfor i in 0...n\n coo[i] = Array.new\n input = gets.chomp\n coo[i] = input\nend\n\nclass Cell\n attr_accessor :row, :column\n\n\n def initialize(row,column,value)\n @row = row\n @column = column\n end\nend\n\n def rc_name_to_cell(rc_name)\n rc_name = rc_name[1,rc_name.length - 1]\n nums = rc_name.split(\"C\")\n c_row = Integer(nums[0])\n c_column = Integer(nums[1])\n return c_row, c_column\n end\n\n def cell_to_excel_name(c_row,c_column)\n name = \"\"\n while (c_column > 0) do \n c_column -= 1\n name += (c_column % 26 + 'A'.ord).chr\n c_column = c_column / 26\n end\n return name.reverse + c_row.to_s\n end\n\n def excel_name_to_cell(excel_name)\n c_row = Array.new\n c_column = Array.new\n for i in 0...excel_name.length\n if excel_name[i] >= \"0\" && excel_name[i] <= \"9\"\n c_row.push(excel_name[i])\n else\n c_column.push(excel_name[i])\n end\n end\n sov = 0\n for i in 0 ...c_column.length\n sov += (c_column[i].ord - \"A\".ord + 1) * 26 ** (c_column.length - i - 1)\n end\n return c_row.join.to_i, sov \n end\n\n def cell_to_rc_name(c_row,c_column)\n print \"R\" + c_row.to_s + \"C\" + c_column.to_s\n end\n\ndef sov(coo)\n c = 0\n a = -1\n b = -1\n for j in 0...coo.length\n if coo[j] == \"R\"\n a = j\n elsif coo[j] == \"C\"\n b = j\n end\n end\n if a != -1 && b != -1\n for m in a + 1...b\n if coo[m] > \"0\" && coo[m] <= \"9\"\n c = 1\n end\n end\n end\n u = 0\n v = 0\n if c == 1\n u, v = rc_name_to_cell(coo)\n return cell_to_excel_name(u, v)\n else\n u, v = excel_name_to_cell(coo)\n return cell_to_rc_name(u, v) \n end\nend\nfor i in 0...n\n puts sov(coo[i])\nend"}, {"source_code": "n = Integer(gets)\ncoo = Array.new\nfor i in 0...n\n coo[i] = Array.new\n input = gets.chomp\n coo[i] = input\nend\n\nclass Cell\n attr_accessor :row, :column\n\n\n def initialize(row,column,value)\n @row = row\n @column = column\n end\nend\n\n def rc_name_to_cell(rc_name)\n rc_name = rc_name[1,rc_name.length - 1]\n nums = rc_name.split(\"C\")\n c_row = Integer(nums[0])\n c_column = Integer(nums[1])\n return c_row, c_column\n end\n\n def cell_to_excel_name(c_row,c_column)\n name = \"\"\n while (c_column > 0) do \n c_column -= 1\n name += (c_column % 26 + 'A'.ord).chr\n c_column = c_column / 26\n end\n return name.reverse + c_row.to_s\n end\n\n def excel_name_to_cell(excel_name)\n c_row = Array.new\n c_column = Array.new\n for i in 0...excel_name.length\n if excel_name[i] >= \"0\" && excel_name[i] <= \"9\"\n c_row.push(excel_name[i])\n else\n c_column.push(excel_name[i])\n end\n end\n sov = 0\n for i in 0 ...c_column.length\n sov += (c_column[i].ord - \"A\".ord + 1) * 26 ** (c_column.length - i - 1)\n end\n return c_row.join.to_i, sov \n end\n\n def cell_to_rc_name(c_row,c_column)\n print \"R\" + c_row.to_s + \"C\" + c_column.to_s\n end\n\n\nfor i in 0...n\n c = 0\n a = -1\n b = -1\n for j in 0...coo[i].length\n if coo[i][j] == \"R\"\n a = j\n elsif coo[i][j] == \"C\"\n b = j\n end\n end\n if a != -1 && b != -1\n for m in a + 1...b\n if coo[i][m] > \"0\" && coo[i][m] <= \"9\"\n c = 1\n end\n end\n end\n if c == 1\n u, v = rc_name_to_cell(coo[i])\n print cell_to_excel_name(u, v)\n puts\n else\n u, v = excel_name_to_cell(coo[i])\n print cell_to_rc_name(u, v)\n puts\n end\n\nend\n"}, {"source_code": "class Integer\n private\n LETTERS = [\"Z\"].concat((\"A\"..\"Y\").to_a)\n \n public\n def to_ab\n number = self\n ab = \"\"\n while(number != 0) do\n letter_index = number % 26\n ab << LETTERS[letter_index]\n number /= 26\n if(letter_index == 0) then number -= 1 end\n end\n return ab.reverse\n end\nend\n\nclass String\n private\n LETTERS = (\"A\"..\"Z\").to_a\n \n public\n def to_rc\n rstring = self.clone.reverse!\n rc = 0\n rstring.length.times {\n |i|\n rc += ((LETTERS.index(rstring[i,1]) + 1) * (26 ** i))\n }\n return rc.to_s\n end\nend\n\ndef rc_ab(rc)\n row, column = rc.scan(/\\d+/)\n column = column.to_i.to_ab\n ab = column << row\n return ab\nend\n\ndef ab_rc(ab)\n column, row = ab.scan(/([A-Z]+)(\\d+)/)[0]\n column = column.to_rc\n rc = \"R\" << row << \"C\" << column\n return rc\nend\n\nn = gets().chomp.to_i\na = []\nn.times {\n|i|\n a[i] = gets().chomp()\n}\na.each{\n|i|\n if (/^R\\d+C\\d+$/ =~ i) then\n #RXCY \n puts(rc_ab(i))\n else\n #AB23\n puts(ab_rc(i))\n end\n}"}, {"source_code": "def solve\n ans = \"\"\n input = gets\n #R23C55\n if (/^R\\d+C\\d+/ =~ input)\n r,c = input.scan(/\\d+/).map(&:to_i)\n while c > 0\n c -= 1\n ans += ('A'.ord + c%26).chr\n c /= 26\n end\n ans.reverse!\n ans += r.to_s\n elsif /.\\d+/ =~ input\n #BC23\n c = input.scan(/[A-Z]+/) #BC\n r = input.scan(/\\d+/)#23\n ans += \"R\" + r.inject(\"\"){|i,j| i + j} + \"C\" + ((0..c[0].size-1).inject(0){|i,j| i+26**j} + c[0].tr(\"A-Z\",\"0-9A-Z\").to_i(26)).to_s \n end\n puts ans\nend\n\nn = gets.to_i\nn.times{ solve}\n\n"}, {"source_code": "def solve\n ans = \"\"\n input = gets\n if (/^R\\d+C\\d+/ =~ input)\n r,c = input.scan(/\\d+/).map(&:to_i)\n while c > 0\n c -= 1\n ans += ('A'.ord + c%26).chr\n c /= 26\n end\n ans.reverse!\n ans += r.to_s\n else\n c = input.scan(/[A-Z]+/).reverse! #BC\n r = input.scan(/\\d+/) #23\n ans += \"R\" \n r.each {|tmp| ans += tmp.to_s}\n ans += \"C\"\n base = 1\n col = 0\n c[0].reverse!\n for i in 0...c[0].size do \n col += (c[0][i].ord-'A'.ord+1)*base\n base *= 26\n end\n ans += col.to_s\n end\n puts ans\nend\n\nn = gets.to_i\nn.times{ solve}\n\n"}, {"source_code": "n = gets.to_i\n\ndef gao(val)\n ret = \"\"\n while val > 0\n if val % 26 == 0\n ret.concat('Z')\n val -= 1\n else\n ret.concat((val % 26 + 65 - 1).chr)\n end\n val /= 26\n end\n ret.reverse!\nend\n\nn.times do\n s = gets\n m = /R(\\d+)C(\\d+)/.match(s)\n if m != nil\n val = m[2].to_i\n puts \"#{gao(val)}#{m[1]}\"\n else\n m = /([A-Z]+)(\\d+)/.match(s)\n col = m[1]\n col.reverse!\n cnt, base = 0, 1\n col.length.times do |i|\n cnt += (col[i].ord - 'A'.ord + 1) * base\n base *= 26\n end\n puts \"R#{m[2]}C#{cnt}\"\n end\nend\n\n"}, {"source_code": "n = gets.to_i\n\ndef gao(val)\n ret = \"\"\n while val > 0\n if val % 26 == 0\n ret.concat('Z')\n val -= 26\n else\n ret.concat((val % 26 + 65 - 1).chr)\n end\n val /= 26\n end\n ret.reverse!\nend\n\nn.times do\n s = gets\n m = /R(\\d+)C(\\d+)/.match(s)\n if m != nil\n val = m[2].to_i\n puts \"#{gao(val)}#{m[1]}\"\n else\n m = /([A-Z]+)(\\d+)/.match(s)\n col = m[1]\n col.reverse!\n cnt, base = 0, 1\n col.length.times do |i|\n cnt += (col[i].ord - 'A'.ord + 1) * base\n base *= 26\n end\n puts \"R#{m[2]}C#{cnt}\"\n end\nend\n\n"}, {"source_code": "#!/usr/bin/ruby\ndef fromcell(s)\n r=0\n s.each_byte{|i|\n r=r*26+i-64\n }\n return r\nend\n\ndef tocell(n)\n r=\"\"\n while n>0 do\n x=n%26\n if x==0 then x=26; n-=26 end\n r=(x+64).chr+r\n n/=26\n end\n return r\nend\n\nn=gets.chomp.to_i\nn.times{|z|\n s=gets.chomp\n if s=~/^([A-Z]+)(\\d+)$/ then puts \"R\"+$2.to_s+\"C\"+fromcell($1).to_s end\n if s=~/R(\\d+)C(\\d+)/ then puts tocell($2.to_i)+$1.to_s end\n}\n"}, {"source_code": "gets.to_i.times do\n\tgets\n\tif /^(\\D+)(\\d+)$/ then\n\t\tputs \"R#{$2}C#{(0..$1.size-1).map{|i|26**i}.inject{|i,j|i+j}+$1.tr('A-Z','0-9A-Z').to_i(26)}\"\n\telsif /R(.*)C/ then\n\t\tl,n=-1,$'.to_i\n\t\tn-=26**(l+=1) while n>=0\n\t\tn+=26**l\n\t\tputs n.to_s(26).tr('0-9a-z','A-Z').rjust(l,'A')+$1\n\tend\nend\n"}, {"source_code": "n = gets.to_i\nrow, col, string = nil\n$alf = ('A'..'Z').to_a\n\ndef int_to_symbolyc(int)\n result = ''\n mult = 1\n while int > 0\n to_sub = int % 26\n index = (to_sub - 1 % 26)\n to_sub = 26 if to_sub==0\n result = $alf[index] + result\n int -= to_sub\n int = int / 26\n end\n result\nend\n\ndef sym_to_int(string)\n result = 0\n a_code = 'A'.bytes.first\n mult = 1\n string.reverse.bytes.each_with_index do |char, i|\n to_add = char - a_code + 1\n to_add *= mult\n result += to_add\n mult *= 26\n end\n result\nend\n\nn.times do\n string = gets\n if matches = string.match(/R(\\d+)C(\\d+)/)\n row, col = matches[1], matches[2]\n puts \"#{int_to_symbolyc(col.to_i)}#{row}\"\n else\n matches = string.match(/([A-Z]+)(\\d+)/)\n col, row = matches[1], matches[2]\n puts \"R#{row}C#{sym_to_int(col)}\"\n end\nend"}, {"source_code": "n = gets.to_i\nn.times do\n\tstr = gets.chomp\n\t\n\tif re = str.match(/^R(\\d+)C(\\d+)$/)\n\t\tc = re[2].to_i\n\t\ts = \"\"\n\t\twhile c != 0\n\t\t\tmod = c % 26\n\t\t\tif mod == 0\n\t\t\t\tmod = 26\n\t\t\t\tc -= mod\n\t\t\tend\n\t\t\ts << (mod + 64).chr\n\t\t\tc /= 26\n\t\tend\n\t\tprint s.reverse, re[1], \"\\n\"\n\telsif re = str.match(/^([A-Z]+)(\\d+)$/)\n\t\tc = 0\n\t\tre[1].each_byte do |char|\n\t\t\tc = char - 64 + c * 26\n\t\tend\n\t\tprint \"R\", re[2], \"C\", c, \"\\n\"\n\tend\nend"}, {"source_code": "n = gets.to_i\nfor x in gets(\"\").split.take(n)\n if x[/R(\\d+)C(\\d+)/]\n a = $2.to_i - 1\n t = (\"A\"..\"Z\").to_a.join[a%26]\n a = a.div 26\n until a == 0\n b,c = (a-1).divmod(26)\n t = (\"A\"..\"Z\").to_a.join[c] + t\n a = b\n end\n puts t + $1\n else\n puts \"R#{x[/\\d+/]}C#{x[/\\D+/].chars.inject(0){|i,j|i*26+j.ord-\"A\".ord+1}}\"\n end\nend"}, {"source_code": "# Codeforces, Problem 1B\n# http://codeforces.com/contest/1/problem/B\n# Nate Collings\n\ndef first_int(cell)\n cell.split('').each_with_index do |char, i|\n begin\n Integer(char)\n return i\n rescue ArgumentError\n end\n end\nend\n\ndef abc_to_int(str)\n num = 0\n power = 1\n str.reverse.split('').each do |char|\n num += (char.ord - 'A'.ord + 1) * power\n power *= 26\n end\n num\nend\n\ndef convert_row_col_to_standard(r, c)\n dividend = c\n column = ''\n while dividend > 0\n remainder = (dividend - 1) % 26\n column = (65 + remainder).chr + column\n dividend = ((dividend - remainder) / 26).to_i\n end\n \"#{column}#{r}\"\nend\n\ndef to_standard(cell)\n c_index = cell.index('C')\n row = cell[1...c_index].to_i\n col = cell[c_index+1..-1].to_i\n convert_row_col_to_standard(row, col)\nend\n\ndef to_rxcy(cell)\n r_index = first_int(cell)\n col = abc_to_int(cell[0...r_index])\n row = cell[r_index..-1]\n \"R#{row}C#{col}\"\nend\n\ndef is_rxcy?(cell)\n /R[0-9]+C[0-9]+/.match(cell)\nend\n\ngets.chomp.to_i.times do\n cell = gets.chomp\n if is_rxcy?(cell)\n puts to_standard(cell)\n else\n puts to_rxcy(cell)\n end\nend\n"}, {"source_code": "n = gets.to_i\n1.upto(n) do\n str = gets\n state = 0\n 0.upto(str.size-1) do |i|\n if str[i].ord>=48 and str[i].ord<=57\n state+=1 if state%2==0\n else\n state+=1 if state%2==1\n end\n end\n if state==2\n col = nil\n row = nil\n 0.upto(str.size-1) do |i|\n if str[i].ord>=48 and str[i].ord<=57\n col = str[0..i-1]\n row = str[i..-1].strip\n break\n end\n end\n coln = 0\n 0.upto(col.size-1) do |i|\n coln *= 26\n coln += col[i].ord-64\n end\n puts \"R#{row}C#{coln}\"\n else\n row = nil\n col = nil\n ci = nil\n 0.upto(str.size-1) do |i|\n if str[i].ord == 67\n ci = i\n end\n end\n row = str[1..ci-1]\n col = str[ci+1..-1].to_i\n cols = \"\"\n while col>0\n if col%26==0\n cols = \"Z\" + cols\n col = (col-1)/26\n else\n cols = (col%26+64).chr + cols\n col /= 26\n end\n end\n puts cols+row\n end\nend\n"}, {"source_code": "\t#recibiendo el numero de iteraciones\n\tinput=gets.chomp\n\tn=input.to_i\n\t#metodo para saber si la cadena es excel o random\n\tdef validar_excel(a)\n\t\tif (a.include?(\"R\") && a.include?(\"C\")) #como el metodo random tiene R&C \n\t\t\tir=a.index(\"R\") \n\t\t\tic=a.index(\"C\")\n\t\t\t\n\t\t\tif a.index(\"R\")==0\n\t\t\t\tif a.index(\"C\")==1\n\t\t\t\t\treturn true\n\t\t\t\telse\n\t\t\t\t\tnum_char=0\n\t\t\t\t\t0.upto(a.size-1) do |j|\n\t\t\t\t\t\tif (a[j].ord>=48 && a[j].ord<=57) #ASCII para diferenciar num & str\n\t\t\t\t\t\t\tnum_char+=0\n\t\t\t\t\t\telse\n\t\t\t\t\t\t\tnum_char+=1\n\t\t\t\t\t\tend\n\t\t\t\t\tend\n\t\t\t\t\t\n\t\t\t\t\treturn num_char==2? false : true\n\t\t\t\tend\t\t\t\t\n\t\t\telse\n\t\t\t\treturn true\n\t\t\tend\n\t\telse\n\t\t\treturn true\n\t\tend\n\tend\n\t#revisar\n\tdef convertir(ar)\n\t\tn=ar.size-1\t\n\t\ti=0\n\t\tnum=0\n\t\t\n\t\tloop do\n\t\t\tif n<0\n\t\t\t\tbreak\n\t\t\tend\n\t\t\t\n\t\t\tnum=num+(ar[i].ord-64)*(26**n)\n\t\t\tn-=1\n\t\t\ti+=1\n\t\tend\n\t\t\n\t\treturn num\n\tend\n\t\n\t#primer metodo de BC23 a R23C55\n\tdef excel_to_random(arr)\n\t\tparte_num=\"\"\n\t\tparte_str=\"\"\n\t\t\n\t\t0.upto(arr.size-1) do |k|\n\t\t\tif (arr[k].ord>=48 && arr[k].ord<=57) #ASCII para diferenciar num & str\n\t\t\t\tparte_num=parte_num+arr[k]\n\t\t\telse\n\t\t\t\tparte_str=parte_str+arr[k]\n\t\t\tend\n\t\tend\n\t\n\t\tparte_c=convertir(parte_str)\n\t\trandom=\"R\"+parte_num+\"C\"+\"#{parte_c}\" #creando el random\n\t\t\n\t\treturn random\n\tend\n\t\n\t#metodo divisiones sucesivas?\n\tdef divsuc(num)\n\t\tnum_car=26\n\t\tpivot=num\n\t\tndig=0\n\t\tarreglo_num=[]\n\t\tpotencia=1\n\t\t\n\t\tloop do\n\t\t\tpivot=pivot-26**potencia\n\t\t\tndig+=1\n\t\t\tpotencia+=1\n\t\t\tif pivot<=0\n\t\t\t\tbreak \n\t\t\tend\n\t\tend\n\t\t\n\t\t#real deal\n\t\tloop do\n\t\t\tif num==0\n\t\t\t\tbreak\n\t\t\tend\n\t\t\tif num%num_car !=0\n\t\t\t\tarreglo_num[ndig-1]=((num%num_car)+64).chr()\n\t\t\t\tnum=num/num_car\n\t\t\telse #COMPORTAMIENTO DIFERENTE CUANDO EL num ES MULTIPLE DE 26 !! (2 days!)\n\t\t\t\tq=num/num_car\n\t\t\t\tnum=q-1\n\t\t\t\tarreglo_num[ndig-1]=(num_car+64).chr()\n\t\t\tend\n\t\t\tndig-=1\n\t\tend\n\t\treturn arreglo_num\n\tend\n\t\n\t#segundo metodo de R23C55 a BC23\n\tdef random_to_excel(arr)\n\t\trow=\"\"\n\t\tcol=\"\"\n\t\ta=0\n\t\t\n\t\t1.upto(arr.size-1) do |k|\n\t\t\t(arr[k]==\"C\")? a+=1:a+=0\n\n\t\t\tif a==0\n\t\t\t\trow=row+arr[k]\n\t\t\telse\n\t\t\t\tif (arr[k]!=\"C\") \n\t\t\t\t\tcol=col+arr[k]\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\t\tcol=col.to_i\n\t\tcole=divsuc(col)#convertir la columna a excel\n\t\texcel=cole.join+row\n\t\t\n\t\treturn excel\n\tend\n\t\n\t$res=[] #definición de la variable GLOBAL, para poder imprimirlo después\n\t\n\t0.upto(n-1) do |j|\n\t\tcadena=gets.chomp.upcase\n\t\tarray=cadena.split(\"\")\n\t\ti=validar_excel(array)\n\t\t\n\t\tif i\n\t\t\tresultado=excel_to_random(array) #TRUE ES excel\n\t\telse\n\t\t\tresultado=random_to_excel(array)\n\t\tend\n\t\t\t\n\t\t$res[j]=resultado \t#recordar que el puts imprime con /n y el print no lo hace\n\tend\n\t#IMPRIMIR RESULTADO\n\tputs $res"}, {"source_code": "d=[*?A..'BEAAA'];def t s;p=a=0;s.each_char{|c|a+=(c.ord-?A.ord+1)*26**p;p+=1};a;end;gets.to_i.times{l=gets;puts (if /R(\\d+)C(\\d+)/.match l;\"#{d[$2.to_i-1]}#{$1}\";else;a,b=l.scan(/([A-Z]+)(\\d+)/)[0];\"R#{b}C#{t a.reverse}\";end)}"}, {"source_code": "def excel_to_index(number)\n row = number.gsub(/[a-zA-Z]/, '').chomp\n column = number.gsub(/\\d/, '').chomp\n\n length = column.length\n column_index = 0\n column.each_char do |c|\n column_index += (c.ord - 'A'.ord + 1) * (26 ** (length - 1))\n length -= 1\n end\n\n 'R' + row + 'C' + column_index.to_s\nend\n\ndef index_to_excel(number)\n row, column = number.sub(/R/, '').split(/C/).map(&:to_i)\n\n result = []\n begin\n x = column % 26\n x = 26 if 0 == x\n column = (column - x) / 26\n\n result.push x\n end while column > 0\n\n column_letters = ''\n result.reverse_each { |e| \n column_letters += (e.ord + 'A'.ord - 1).chr\n }\n\n column_letters + row.to_s\nend \n\ncount = gets.to_i\n\ncount.times do\n num = gets.upcase\n if num[/R\\d+C\\d+/]\n puts index_to_excel num\n else\n puts excel_to_index num\n end \nend"}, {"source_code": "d=[*?A..'BEAAA'];def t s;p=a=0;s.each_char{|c|a+=(c.ord-?A.ord+1)*26**p;p+=1};a;end;gets.to_i.times{l=gets;puts (if /R(\\d+)C(\\d+)/.match l;\"#{d[$2.to_i-1]}#{$1}\";else;a,b=l.scan(/([A-Z]+)(\\d+)/)[0];\"R#{b}C#{t a.reverse}\";end)}"}, {"source_code": "d=[*?A..'BEAAA'];def t s;p=a=0;s.each_char{|c|a+=(c.ord-?A.ord+1)*26**p;p+=1};a;end;gets.to_i.times{l=gets;puts (if /R(\\d+)C(\\d+)/.match l;\"#{d[$2.to_i-1]}#{$1}\";else;a,b=l.scan(/([A-Z]+)(\\d+)/)[0];\"R#{b}C#{t a.reverse}\";end)}"}, {"source_code": "# Codeforces 1B\n# TAG: Math\n\ndef rc2xls(r, c)\n a = []\n while c > 0 do a << ((c-1) % 26 + 1); c = (c-a.last)/26 end\n \"#{a.reverse.reduce(\"\") {|s, i| s += (i+64).chr }}#{r}\"\nend\n\ndef xls2rc(s)\n /(?[A-Z]+)(?\\d+)/ =~ s\n c = c.split(//).map(&:ord).reduce(0) {|s, i| s = s*26 + i - 64 }\n \"R#{r}C#{c}\"\nend\n\ngets.to_i.times do\n puts /R(?\\d+)C(?\\d+)/ =~ (s = gets) ? rc2xls(r.to_i, c.to_i) : xls2rc(s)\nend\n"}, {"source_code": "def f(r,c)a=[]\nwhile c>0;t=((c-1)%26+1);a=[t]+a;c=(c-t)/26 end\n\"#{a.reduce(\"\"){|s,i|s+=(i+64).chr}}#{r}\"\nend\ndef g(s)/(?\\D+)(?\\d+)/=~s\n\"R#{r}C#{c.split(//).map(&:ord).reduce(0){|s,i|s=s*26+i-64}}\"\nend\ngets.to_i.times {puts /R(?\\d+)C(?\\d+)/=~(s=gets)?f(r,c.to_i):g(s)}"}, {"source_code": "def f(r,c)\na=[]\nwhile c>0 do t=((c-1)%26+1);a<\\D+)(?\\d+)/=~s\n\"R#{r}C#{c.split(//).map(&:ord).reduce(0){|s,i|s=s*26+i-64}}\"\nend\ngets.to_i.times do\nputs /R(?\\d+)C(?\\d+)/=~(s=gets)?f(r.to_i,c.to_i):g(s)\nend\n"}, {"source_code": "def rc2xls(r, c)\n a = []\n while c>0 do a << ((c-1) % 26 + 1); c = (c-a.last)/26 end\n \"#{a.reverse.reduce(\"\") {|s,i| s += (i+64).chr }}#{r}\"\nend\n\ndef xls2rc(s)\n /(?[A-Z]+)(?\\d+)/ =~ s\n \"R#{r}C#{c.split(//).map(&:ord).reduce(0) {|s,i| s = s*26+i-64 }}\"\nend\n\ngets.to_i.times do\n puts /R(?\\d+)C(?\\d+)/ =~ (s = gets) ? rc2xls(r.to_i, c.to_i) : xls2rc(s)\nend"}, {"source_code": "# Codeforces 1B\n# TAG: Math\n\ndef rc2xls(r, c)\n a = []\n while c>0\n t = ((c-1)%26+1)\n a << t\n c = (c-t)/26\n end\n \"#{a.reverse.reduce(\"\") {|s,i| s += (i+64).chr }}#{r}\"\nend\n\ndef xls2rc(s)\n /(?\\D+)(?\\d+)/ =~ s\n \"R#{r}C#{c.split(//).map(&:ord).reduce(0) {|s, i| s = s*26+i-64 }}\"\nend\n\ngets.to_i.times do\n puts /R(?\\d+)C(?\\d+)/ =~ (s = gets) ? rc2xls(r, c.to_i) : xls2rc(s)\nend\n"}, {"source_code": "def f(s)\n r=0\n s.each_byte{|c| r=r*26+c-64}\n r\nend\ndef g(n)\n r=\"\"\n while n>0 do\n x=n%26\n if x==0 then\n x=26\n n-=x\n end\n r=(x+64).chr+r\n n/=26\n end\n r\nend\nn=gets.to_i\nn.times{\n s=gets\n if s=~/^([A-Z]+)(\\d+)$/ then\n puts \"R\"+$2.to_s+\"C\"+f($1).to_s\n end\n if s=~/R(\\d+)C(\\d+)/ then\n puts g($2.to_i)+$1\n end\n}"}, {"source_code": "\ndef a_to_n(line)\n length = line.length\n i = 0\n sum = 0\n while i < length\n sum = sum * 26 + (line[i].ord - 'A'.ord + 1) \n i = i + 1\n end\n return sum\nend\n\ndef n_to_a(line)\n\n row_str = \"\"\n row_num = line.to_i\n while row_num > 0\n if(row_num % 26 != 0)\n row_str.prepend( (64 + row_num % 26).chr) \n row_num = row_num/26\n else\n row_str.prepend('Z') \n row_num = row_num/26 - 1\n end\n \n end\n return row_str\n\nend\n\ndef xy_to_rc(line)\n col_size = line =~ /[0-9]/\n\n col_str = line[0...col_size]\n row_str = line[col_size...-1]\n\n col = a_to_n(col_str)\n row = row_str.to_i\n\n print(\"R\")\n print(row)\n print(\"C\")\n print(col)\n\nend\n\ndef rc_to_xy(line)\n col_size = line =~ /[C]/\n\n row_str = line[1...col_size]\n col_str = line[col_size+1...-1]\n\n col = n_to_a(col_str)\n row = row_str.to_i\n\n print(col)\n print(row)\nend\n\ndef isrc(line)\n if line[0] == 'R' && line[1] =~ /[0-9]/\n if line[2...-1].include? \"C\"\n return true\n end\n end\n return false\nend\n\n\nline_count = gets.to_i\n\nwhile line_count > 0 \n line_string = gets\n if isrc(line_string)\n rc_to_xy(line_string)\n else\n xy_to_rc(line_string)\n end\n print(\"\\n\")\n line_count = line_count-1\nend\n"}, {"source_code": "def to_ex d\n str = \"\"\n while d > 0\n d, a = (d - 1).divmod(26)\n str = (a + 65).chr + str\n end\n str\nend\n\ndef ex_to str\n str.chars.each.with_index.inject(0){ |z, (v, i)| z = z * 26 + (v.ord - 64) }\nend\n\ndef solution str\n a, b = str.scan(/\\d+/).map(&:to_i)\n if b\n \"#{to_ex(b)}#{a}\"\n else\n a, b = str.scan(/([A-Z]+)(\\d+)/).first\n \"R#{b}C#{ex_to(a)}\"\n end \nend\n\nn = STDIN.gets.to_i\nArray.new(n){ solution(STDIN.gets.chomp) }.each{ |v| print v, \"\\n\" }"}, {"source_code": "def convert(s)\n if (s =~ /R([0-9]+)C([0-9]+)/)\n c, r = $1, $2.to_i\n \n x = 1\n t = 26\n while r > t do\n r -= t\n x += 1\n t *= 26\n end\n\n d = []\n r -= 1\n for i in 0..x - 1\n d.push(r % 26) \n r /= 26\n end\n\n r = \"\"\n for i in (d.size - 1).step(0, -1)\n r += (d[i] + 'A'.ord).chr\n end\n return r + c\n elsif (s =~ /([A-Z]+)([0-9]+)/ )\n c, r = $1, $2\n\n\n ans, w = 0, 26\n for i in 1..c.size - 1\n ans += w\n w *= 26\n end\n\n cw = 0\n for i in 0..c.size - 1\n cw = c[i].ord - 'A'.ord + cw * 26\n end\n return 'R' + r + 'C' + (cw + 1 + ans).to_s\n end\nend\n\nn = gets.to_i\n\nfor cas in 0..n - 1\n s = gets\n ans = convert(s)\n puts(ans)\nend\n"}, {"source_code": "pattern_r = /R[0-9]+C[0-9]+/\npattern_a = /[A-Z]+[0-9]+/\nn = gets.to_i\nfor i in 0...n\n s = gets\n if pattern_r =~ s\n x = s[/\\d+/]\n y = s[/(C)(\\d+)/, 2]\n y = y.to_i\n ans1 = \"\"\n while y != 0\n if y % 26 == 0\n ans1 = 'Z' << ans1\n y = y - 26\n else\n ans1 = ('A'.ord + y % 26 - 1).chr << ans1\n end\n y = y / 26\n end\n puts ans1 << x\n else\n x = s[/[A-Z]+/]\n y = s[/\\d+/]\n ans1 = 0\n x.each_byte do |c|\n ans1 = ans1 * 26 + (c - \"A\".bytes.to_a[0]) + 1\n end\n puts \"R\" << y << \"C\" << ans1.to_s\n end\nend"}, {"source_code": "LettersCount = 'Z'.ord - 'A'.ord + 1\n\ndef intToLetters(i)\n result = ''\n while i > 0\n i -= 1\n letter = ('A'.ord + i % LettersCount).chr\n result << letter\n i /= LettersCount\n end\n result.reverse!\nend\n\ndef lettersToInt(l)\n result = 0\n l.each_char {\n |char|\n result *= LettersCount \n result += char.ord - 'A'.ord + 1\n }\n result\nend\n\ndef format2to1(r, c)\n intToLetters(c) + r.to_s\nend\n\ndef format1to2(c, r)\n 'R' + r + 'C' + lettersToInt(c).to_s\nend\n\nresult = []\n\nline = $stdin.gets\nn = line.to_i\n(1..n).each do\n line = $stdin.gets\n match = /R(\\d+)C(\\d+)/.match(line)\n if match\n captures = match.captures.map {|x| x.to_i} \n result << format2to1(captures[0], captures[1])\n elsif\n captures = /([^\\d]+)(\\d+)/.match(line).captures\n result << format1to2(captures[0], captures[1])\n end\nend\n\nresult.each do |x|\n puts x\nend\n"}, {"source_code": "#!/bin/ruby\n\n$ASCII_CODE_A = \"A\".bytes.to_a[0]\n\ndef to_alpha(n)\n ary = Array.new\n while n > 0\n c = ($ASCII_CODE_A + (n - 1) % 26).chr\n ary << c\n n = (n - 1) / 26\n end\n return ary.reverse.join\nend\n\ndef to_number(as)\n n = 0\n as.each_byte do |c|\n n *= 26\n n += c - $ASCII_CODE_A + 1\n end\n return n\nend\n\nn = gets.to_i\n(1..n).step do |i|\n input = gets\n if input =~ /^R(\\d+)C(\\d+)$/ then\n row = $1\n col = to_alpha($2.to_i)\n puts col + row\n elsif input =~ /^([A-Z]+)(\\d+)$/ then\n row = $2\n col = to_number($1).to_s\n puts \"R\" + row + \"C\" + col\n end\nend\n"}, {"source_code": "#!/usr/bin/env ruby\n\ndef get_series_string(n)\n return \"\" if n == 0\n n = n - 1\n q = n/26\n r = n%26\n return \"#{get_series_string(q)}#{(r+65).chr}\"\nend\n\ndef get_number_for_series(str)\n n = 0\n str.chars.each do | c |\n n = n * 26 + c.ord - 65 + 1\n end\n return n\nend\n\ns = gets.to_i\na = []\nwhile s > 0 do\n a << gets.chomp!\n s -= 1\nend\n\na.each do |e|\n parsed = e.scan(/([A-Z]+)(\\d+)/)\n if parsed.length == 2\n puts \"#{get_series_string(parsed[1][1].to_i)}#{parsed[0][1]}\"\n elsif parsed.length == 1\n puts \"R#{parsed[0][1]}C#{get_number_for_series(parsed[0][0])}\"\n end\nend\n"}, {"source_code": "#!/usr/bin/env ruby\n\ndef get_series_string(n)\n str = \"\"\n while n > 0 do\n q = (n - 1) / 26\n r = (n - 1) % 26\n str = \"#{(r + 65).chr}#{str}\"\n n = q\n end\n return str\nend\n\ndef get_number_for_series(str)\n n = 0\n str.chars.each do | c |\n n = n * 26 + c.ord - 65 + 1\n end\n return n\nend\n\ns = gets.to_i\ns.times do\n e = gets.chomp!\n parsed = e.scan(/([A-Z]+)(\\d+)/)\n if parsed.length == 2\n puts \"#{get_series_string(parsed[1][1].to_i)}#{parsed[0][1]}\"\n elsif parsed.length == 1\n puts \"R#{parsed[0][1]}C#{get_number_for_series(parsed[0][0])}\"\n end\n s -= 1\nend\n"}, {"source_code": "gets.to_i.times do\n\tgets\n\tif /^(\\D+)(\\d+)$/ then\n\t\tputs \"R#{$2}C#{(0..$1.size-1).map{|i|26**i}.inject{|i,j|i+j}+$1.tr('A-Z','0-9A-Z').to_i(26)}\"\n\telsif /R(.*)C/ then\n\t\tl,n=-1,$'.to_i\n\t\tn-=26**(l+=1) while n>=0\n\t\tn+=26**l\n\t\tputs n.to_s(26).tr('0-9a-z','A-Z').rjust(l,'A')+$1\n\tend\nend"}, {"source_code": "r = /R\\d+C\\d+/\nrow_regex = /\\d+/\n\nn = gets.chomp.to_i\n\nn.times do\n line = gets.chomp\n if line.match(r)\n row = line[1.. (line =~ /C/)].to_i\n c = line[(line =~ /C/)+1..-1].to_i\n a = 0; b = 0\n a = c\n res = \"\"\n while (a+26) > 26\n a = c / 26\n \t\t\tb = c % 26 == 0 ? 26 : c % 26\n \t\t\ta = c % 26 == 0 ? a - 1 : a\n \t\t\tc = a\n res.prepend(((b-1) + 65).chr)\n end\n puts \"#{res}#{row}\"\n else\n num_index = line =~ row_regex\n row = line[num_index..line.length()]\n col = 0\n col_str = line[0..num_index-1]\n col_str.split('').map.with_index do |chr, i|\n col += (chr.ord - 'A'.ord + 1) * (26 ** (num_index-1-i))\n end\n puts \"R#{row}C#{col}\"\n end\nend\n"}, {"source_code": "number_of_input=gets.to_i\ninput_arry=[]\ni_a=0\n\n\nloop do\n\tbreak if i_a==number_of_input\n\tinput_arry[i_a]=gets.chomp\n\ti_a +=1\nend\n\nvalue=(1..26)\nalph=('A'..'Z')\nvalue=[*value]\nalph=[*alph]\n@hash={}\ni_b=0\n\nloop do\n\tbreak if i_b==26\n\t@hash[alph[i_b]]=value[i_b]\n\ti_b+=1\nend\t\n\n\n\ndef S1_to_S2 (a1)\n\tr=a1[0]\n\ti=1\n\t\n\tloop do\n\t\tbreak if a1[i].to_i !=0\n\t\tr =r + a1[i]\n\t\ti+=1\n\tend\n\n\tr1=r.length\n\tr2=a1.length\n\tc1= a1[0 .. (r1-1)].reverse\n\trow= a1[ r1 .. r2].to_i\n\tc=0\n\ti=0\n\t\n\tloop do\n\t\tbreak if i==c1.length\n\t\tc= c + (@hash[c1[i]]*(26**i))\n\t\ti+=1\n\tend\n\t\n\tputs \"R#{row}C#{c}\"\nend\n\ndef S2_to_S1 (a1)\n\ts2=a1.split(\"\").map{|s| s}\n\tr3=3\n\ts2.find {|i| r3=s2.index(i)+1 if i=='C'}\n\ti=s2[r3 .. (s2.length - 1)].join.to_i\n\trow=s2[1 .. (r3-2)].join.to_i\n\tstr_c=\"\"\n\t\n\tloop do\n\t\ti=i-26 if i>25 && i%26==0\n\t\tif i%26==0\n\t\tstr_c = str_c + \"Z\"\n\t\telse\n\t\tstr_c = str_c + @hash.key(i%26)\n\t\tend\n\t\ti=i/26\n\t\tbreak if i==0\n\tend\n\t\n\tstr_c=str_c.reverse\n\tputs \"#{str_c}#{row}\"\nend\n\t\ni_c =0\n\nloop do\n break if i_c==input_arry.length\n\tinput1=input_arry[i_c]\n\t\n\tif \tinput1[0]!='R' || (input1[0]=='R' && input1[1].to_i==0) \n\t\tS1_to_S2 (input1)\n\telsif input1[0]=='R' && input1[1].to_i!=0\n\t\tk2=input1.split(\"\")\n\t\tk=0\n\t\tk2.find{|s| k=1 if s=='C'}\n\t\tif k==1\n\t\t\tS2_to_S1(input1)\n\t\telse\n\t\t\tS1_to_S2 (input1)\n\t\tend\t\n\tend\n\t\n\ti_c +=1\n\t\nend"}, {"source_code": "number_of_input=gets.to_i\ninput_arry=[]\ni=0\n\n\nloop do\n\tbreak if i==number_of_input\n\tinput_arry[i]=gets.chomp\n\ti +=1\nend\n\nvalue=(1..26)\nalph=('A'..'Z')\nvalue=[*value]\nalph=[*alph]\n@hash={}\ni=0\n\nloop do\n\tbreak if i==26\n\t@hash[alph[i]]=value[i]\n\ti+=1\nend\t\n\n\n\ndef S1_to_S2 (a1)\n\tr=a1[0]\n\ti=1\n\t\n\tloop do\n\t\tbreak if a1[i].to_i !=0\n\t\tr =r + a1[i]\n\t\ti+=1\n\tend\n\n\tr1=r.length\n\tr2=a1.length\n\tc1= a1[0 .. (r1-1)].reverse\n\trow= a1[ r1 .. r2].to_i\n\tc=0\n\ti=0\n\t\n\tloop do\n\t\tbreak if i==c1.length\n\t\tc= c + (@hash[c1[i]]*(26**i))\n\t\ti+=1\n\tend\n\t\n\tputs \"R#{row}C#{c}\"\nend\n\ndef S2_to_S1 (a1)\n\ts2=a1.split(\"\").map{|s| s}\n\tr3=3\n\ts2.find {|i| r3=s2.index(i)+1 if i=='C'}\n\ti=s2[r3 .. (s2.length - 1)].join.to_i\n\trow=s2[1 .. (r3-2)].join.to_i\n\tstr_c=\"\"\n\t\n\tloop do\n\t\ti=i-26 if i>25 && i%26==0\n\t\tif i%26==0\n\t\tstr_c = str_c + \"Z\"\n\t\telse\n\t\tstr_c = str_c + @hash.key(i%26)\n\t\tend\n\t\ti=i/26\n\t\tbreak if i==0\n\tend\n\t\n\tstr_c=str_c.reverse\n\tputs \"#{str_c}#{row}\"\nend\n\t\ni=0\n\nloop do\n break if i==input_arry.length\n\tinput1=input_arry[i]\n\t\n\tif \tinput1[0]!='R' || (input1[0]=='R' && input1[1].to_i==0) \n\t\tS1_to_S2 (input1)\n\telsif input1[0]=='R' && input1[1].to_i!=0\n\t\tk2=input1.split(\"\")\n\t\tk=0\n\t\tk2.find{|s| k=1 if s=='C'}\n\t\tif k==1\n\t\t\tS2_to_S1(input1)\n\t\telse\n\t\t\tS1_to_S2 (input1)\n\t\tend\t\n\tend\n\t\n\ti +=1\n\t\nend"}, {"source_code": "\n\nnumber_of_input=gets.to_i\ninput_arry=[]\ni_a=0\n\n\nloop do\n\tbreak if i_a==number_of_input\n\tinput_arry[i_a]=gets.chomp\n\ti_a +=1\nend\nvalue=(1..26)\nalph=('A'..'Z')\nvalue=[*value]\nalph=[*alph]\n@hash={}\ni_b=0\n\nloop do\n\tbreak if i_b==26\n\t@hash[alph[i_b]]=value[i_b]\n\ti_b+=1\nend\t\n\nvalue=(1..26)\nalph=('A'..'Z')\nvalue=[*value]\nalph=[*alph]\n@hash2={}\ni_b=0\n\nloop do\n\tbreak if i_b==26\n\t@hash2[alph[i_b]]=value[i_b]\n\ti_b+=1\nend\t\n@hash2['Z']=0\n\n\ndef S1_to_S2 (a1)\n\tr=a1[0]\n\ti=1\n\t\n\tloop do\n\t\tbreak if a1[i].to_i !=0\n\t\tr =r + a1[i]\n\t\ti+=1\n\tend\n\n\tr1=r.length\n\tr2=a1.length\n\tc1= a1[0 .. (r1-1)].reverse\n\trow= a1[ r1 .. r2].to_i\n\tc=0\n\ti=0\n\t\n\tloop do\n\t\tbreak if i==c1.length\n\t\tc= c + (@hash[c1[i]]*(26**i))\n\t\ti+=1\n\tend\n\t\n\tputs \"R#{row}C#{c}\"\nend\n\ndef S2_to_S1 (a1)\n\ts2=a1.split(\"\").map{|s| s}\n\tr3=3\n\ts2.find {|i| r3=s2.index(i)+1 if i=='C'}\n\ti=s2[r3 .. (s2.length - 1)].join.to_i\n\trow=s2[1 .. (r3-2)].join.to_i\n\tstr_c=\"\"\n\t\n\tloop do\n\t\ti=i-26 if i>25 && i%26==0\n\t\tstr_c = str_c + @hash2.key(i%26)\n\t\ti=i/26\n\t\tbreak if i==0\n\tend\n\t\n\tstr_c=str_c.reverse\n\tputs \"#{str_c}#{row}\"\nend\n\t\ni_c =0\n\nloop do\n break if i_c==input_arry.length\n\tinput1=input_arry[i_c]\n\t\n\tif \tinput1[0]!='R' || (input1[0]=='R' && input1[1].to_i==0) \n\t\tS1_to_S2 (input1)\n\telsif input1[0]=='R' && input1[1].to_i!=0\n\t\tk2=input1.split(\"\")\n\t\tk=0\n\t\tk2.find{|s| k=1 if s=='C'}\n\t\tif k==1\n\t\t\tS2_to_S1(input1)\n\t\telse\n\t\t\tS1_to_S2 (input1)\n\t\tend\t\n\tend\n\t\n\ti_c +=1\n\t\nend\n\t"}, {"source_code": "n = gets().to_i\nfor i in 1..n\n\tline = gets().split(/(\\d+)/)\n\tif line.length >= 4\n\t\tcol = line[3].to_i\n\t\tfor j in 1..100\n\t\t\tbreak if 26**j >= col\n\t\t\tcol -= 26**j\n\t\tend\n\t\ts = (col-1).to_s(26).rjust(j,'0').tr('0123456789abcdefghijklmnop', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')\n\t\tputs s << line[1]\n\telse\n\t\trow = line[1]\n\t\tans = 1 + line[0].tr('ABCDEFGHIJKLMNOPQRSTUVWXYZ', '0123456789abcdefghijklmnop').to_i(26) \n\t\tfor j in 1..line[0].length-1\n\t\t\tans += 26**j\n\t\tend\n\t\tputs 'R' << row << 'C' << ans.to_s\n\tend\nend\n"}, {"source_code": "#!/usr/bin/ruby\n\n$ord_A = \"A\".ord\n\ndef name2addr(name)\n\taddr = 0\n\tname.each_char do |c|\n\t\taddr = addr*26 + c.ord - $ord_A + 1\n\tend\n\treturn addr\nend\n\ndef addr2name(addr)\n\tname = \"\"\n\twhile addr != 0\n\t\taddr -= 1\n\t\tname = (addr%26 + $ord_A).chr + name\n\t\taddr /= 26\n\tend\n\treturn name\nend\n\ngets\t# skip first line\nwhile gets\n\tline = $_.chomp\n\tif m = /R(\\d+)C(\\d+)/.match(line)\n\t\tputs addr2name(m[2].to_i) + m[1]\n\telsif m = /([A-Z]+)(\\d+)/.match(line)\n\t\tputs \"R\" + m[2] + \"C\" + name2addr(m[1]).to_s\n\telse\n\t\tabort \"bad_format\"\n\tend\nend\n"}, {"source_code": "class SpreadsheetCoordinateConverter\n EXCEL_SYSTEM_BASE = 26\n\n class << self\n def convert(lines)\n lines.map do |line|\n if line =~ /R(\\d+)C(\\d+)/\n rc_to_excel($1.to_i, $2.to_i)\n elsif line =~ /([A-Z]+)(\\d+)/\n excel_to_rc($1.to_s, $2.to_i)\n end\n end\n end\n\n private\n\n # R23C55 -> BC23\n def rc_to_excel(row, col)\n excel_col = ''\n while col > 0\n col -= 1\n excel_col << num_to_chr(col % EXCEL_SYSTEM_BASE)\n col /= EXCEL_SYSTEM_BASE\n end\n excel_col.reverse! + row.to_s\n end\n\n # BC23 -> R23C55\n def excel_to_rc(excel_col, row)\n col = 0\n power_multiplier = 1\n excel_col.reverse.each_char do |chr|\n col += (chr_to_num(chr) + 1) * power_multiplier\n power_multiplier *= EXCEL_SYSTEM_BASE\n end\n \"R#{row}C#{col}\"\n end\n\n def num_to_chr(num)\n (num + 'A'.ord).chr\n end\n\n def chr_to_num(chr)\n chr.ord - 'A'.ord\n end\n end\nend\n\nif __FILE__ == $0\n lineCount = gets.to_i\n lines = []\n lineCount.times { |line| lines << gets }\n SpreadsheetCoordinateConverter.convert(lines).each { |line| puts line }\nend\n"}, {"source_code": "\n\nnumber_of_input=gets.to_i\ninput_arry=[]\ni_a=0\n\n\nloop do\n\tbreak if i_a==number_of_input\n\tinput_arry[i_a]=gets.chomp\n\ti_a +=1\nend\n\nvalue=(1..26)\nalph=('A'..'Z')\nvalue=[*value]\nalph=[*alph]\n@hash={}\ni_b=0\n\nloop do\n\tbreak if i_b==26\n\t@hash[alph[i_b]]=value[i_b]\n\ti_b+=1\nend\t\n\nvalue=(1..26)\nalph=('A'..'Z')\nvalue=[*value]\nalph=[*alph]\n@hash2={}\ni_b=0\n\nloop do\n\tbreak if i_b==26\n\t@hash2[alph[i_b]]=value[i_b]\n\ti_b+=1\nend\t\n@hash2['Z']=0\n\n\ndef S1_to_S2 (a1)\n\tr=a1[0]\n\ti=1\n\t\n\tloop do\n\t\tbreak if a1[i].to_i !=0\n\t\tr =r + a1[i]\n\t\ti+=1\n\tend\n\n\tr1=r.length\n\tr2=a1.length\n\tc1= a1[0 .. (r1-1)].reverse\n\trow= a1[ r1 .. r2].to_i\n\tc=0\n\ti=0\n\t\n\tloop do\n\t\tbreak if i==c1.length\n\t\tc= c + (@hash[c1[i]]*(26**i))\n\t\ti+=1\n\tend\n\t\n\tputs \"R#{row}C#{c}\"\nend\n\ndef S2_to_S1 (a1)\n\ts2=a1.split(\"\").map{|s| s}\n\tr3=3\n\ts2.find {|i| r3=s2.index(i)+1 if i=='C'}\n\ti=s2[r3 .. (s2.length - 1)].join.to_i\n\trow=s2[1 .. (r3-2)].join.to_i\n\tstr_c=\"\"\n\t\n\tloop do\n\t\ti=i-26 if i>25 && i%26==0\n\t\tstr_c = str_c + @hash2.key(i%26)\n\t\ti=i/26\n\t\tbreak if i==0\n\tend\n\t\n\tstr_c=str_c.reverse\n\tputs \"#{str_c}#{row}\"\nend\n\t\ni_c =0\n\nloop do\n break if i_c==input_arry.length\n\tinput1=input_arry[i_c]\n\t\n\tif \tinput1[0]!='R' || (input1[0]=='R' && input1[1].to_i==0) \n\t\tS1_to_S2 (input1)\n\telsif input1[0]=='R' && input1[1].to_i!=0\n\t\tk2=input1.split(\"\")\n\t\tk=0\n\t\tk2.find{|s| k=1 if s=='C'}\n\t\tif k==1\n\t\t\tS2_to_S1(input1)\n\t\telse\n\t\t\tS1_to_S2 (input1)\n\t\tend\t\n\tend\n\t\n\ti_c +=1\n\t\nend\n\t\n\t\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n"}], "negative_code": [{"source_code": "arr = STDIN.read.split(\"\\n\")\nn = arr[0].to_i\n\n1.upto(n).each do |i|\n str = arr[i]\n match = str.match(/R([0-9]+)C([0-9]+)/)\n\n if match\n row = match[2].to_i\n col = match[1]\n\n r = row\n str = \"\"\n while(r > 0) do\n val = (r-1) % 26\n str += (val + 65).chr\n r /= 26\n end\n str = str.reverse\n str += col\n puts str\n else\n match = str.match(/([A-Z]+)([0-9]+)/)\n col = match[1]\n row = match[2]\n\n puts \"A\"\n s = 0\n rev = col.reverse.split(\"\")\n puts \"B\"\n rev.each_index do |i|\n puts \"rev[i]\" + rev[i]\n puts rev[i].ord - 65 + 1\n chr = (rev[i].ord - 65 + 1) * ( 26 ** i )\n puts \"chr:#{chr}\"\n s += chr\n end\n\n puts \"R#{row}C#{s}\"\n end\nend"}, {"source_code": "#!/usr/bin/ruby\ndef fromcell(s)\n r=0\n s.length.times{|i|\n #r=r*26+s[i]-64\n }\n return r\nend\n\ndef tocell(n)\n r=\"\"\n while n>0 do\n r=(n%26+64).chr+r\n n/=26\n end\n return r\nend\n\nn=gets.chomp.to_i\nn.times{|z|\n s=gets.chomp\n if s=~/^([A-Z]+)(\\d+)$/ then puts \"R\"+$2.to_s+\"C\"+fromcell($1).to_s end\n if s=~/R(\\d+)C(\\d+)/ then puts tocell($2.to_i)+$1.to_s end\n}\nexit(0)\n"}, {"source_code": "n = gets().to_i\nfor i in 1..n\n\tline = gets().split(/(\\d+)/)\n\tif line[0] == 'R'\n\t\tcol = line[3].to_i\n\t\tfor j in 1..100\n\t\t\tbreak if 26**j >= col\n\t\t\tcol -= 26**j\n\t\tend\n\t\ts = (col-1).to_s(26).rjust(j,'0').tr('0123456789abcdefghijklmnop', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')\n\t\tputs s << line[1]\n\telse\n\t\trow = line[1]\n\t\tans = 1 + line[0].tr('ABCDEFGHIJKLMNOPQRSTUVWXYZ', '0123456789abcdefghijklmnop').to_i(26) \n\t\tfor j in 1..line[0].length-1\n\t\t\tans += 26**j\n\t\tend\n\t\tputs 'R' << row << 'C' << ans.to_s\n\tend\nend\n"}, {"source_code": "def n2s(n)\n s = \"\"\n while n > 0\n s = (\"A\".ord + n % 26 - 1).chr + s\n n /= 26\n end\n return s\nend\n\ndef s2n(s)\n n = 0\n s.size.times do |i|\n n *= 26\n n += s[i].ord - \"A\".ord + 1\n end\n return n\nend\n\nn = gets.to_i\nn.times do\n str = gets\n if str =~ /^([A-Z]+)(\\d+)$/\n c_s = $1\n r = $2.to_i\n c = s2n(c_s)\n puts \"R#{r}C#{c}\"\n elsif str =~ /^R(\\d+)C(\\d+)$/\n r = $1.to_i\n c = $2.to_i\n c_s = n2s(c)\n puts \"#{c_s}#{r}\"\n end\nend\n"}, {"source_code": "alph = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']\n\nn = gets.chomp.to_i\n\nlines = []\nn.times {lines << gets.chomp}\nlines.each do |l|\n if (l =~ /R\\d+C\\d+/).nil?\n col = 0\n colstr = l.scan(/[A-Z]+/).first.split(\"\")\n colstr.each_with_index do |ch, index|\n col += (alph.index(ch) + 1) * (26 ** (colstr.length - index - 1))\n end\n row = l.scan(/[0-9]+/).first\n puts \"R#{row}C#{col}\"\n else\n row = l.scan(/R([0-9]+)/).flatten.first\n col = l.scan(/C([0-9]+)/).flatten.first.to_i\n cols = \"\"\n remainder = 1\n loop do\n minus = remainder == 0 ? 2 : 1\n remainder = (col % 26)\n col = col / 26\n unless col == 0 and minus == 2 and remainder <= 1\n cols = alph[remainder - minus] + cols\n end\n break if col == 0\n end\n puts \"#{cols}#{row}\"\n end\nend\n"}, {"source_code": "#=================================\n# Better be sure than sorry. \n#=================================\n$stdin = File.open(\"./input.txt\", \"r\") if ENV[\"LOGNAME\"]==\"skydos\"\n#=================================\n\ndef to_letters num\n\tans=\"\"\n\tdigs=[*?A..?Z]\n\t\n\tnum+=num/27\n\t\n\twhile num!=0\t\n\t\tans+=digs[num%27-1]\n\t\tnum/=27\n\tend\n\t\n\tans.reverse\nend\n\ndef to_numbers str\n\tans=0\n\tstr.reverse!\n\t\n\tpow=0\n\tstr.each_char{|c|\n\t\tans+= (c.ord - 'A'.ord + 1) * 26**pow\n\t\tpow+=1\n\t}\n\t\n\tans\nend\n\nn=gets.to_i\n\nn.times{\n\tline=gets.strip\n\t\n\tif /R(\\d+)C(\\d+)/.match line\n\t\tputs \"#{to_letters $2.to_i}#{$1}\"\n\telse\n\t\tp1, p2 = line.scan(/([A-Z]+)([0-9]+)/)[0]\n\t\tputs \"R#{p2}C#{to_numbers p1}\"\n\tend\n}\n"}, {"source_code": "def to_ex d\n str = \"\"\n while d > 1\n d, a = d.divmod(26)\n str = (a + 64).chr + str\n end\n str\nend\n\ndef ex_to str\n str.chars.each.with_index.inject(0){ |z, (v, i)| z = z * 26 + (v.ord - 64) }\nend\n\ndef solution str\n a, b = str.scan(/\\d+/).map(&:to_i)\n if b\n \"#{to_ex(b)}#{a}\"\n else\n a, b = str.scan(/([A-Z]+)(\\d+)/).first\n \"R#{b}C#{ex_to(a)}\"\n end \nend\n\nn = STDIN.gets.to_i\nArray.new(n){ solution(STDIN.gets.chomp) }.each{ |v| print v, \"\\n\" }"}, {"source_code": "alph = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']\n\nn = gets.chomp.to_i\n\nlines = []\nn.times {lines << gets.chomp}\nlines.each do |l|\n if (l =~ /R\\d+C\\d+/).nil?\n col = 0\n colstr = l.scan(/[A-Z]+/).first.split(\"\")\n colstr.each_with_index do |ch, index|\n col += (alph.index(ch) + 1) * (26 ** (colstr.length - index - 1))\n end\n row = l.scan(/[0-9]+/).first\n puts \"R#{row}C#{col}\"\n else\n row = l.scan(/R([0-9]+)/).flatten.first\n col = l.scan(/C([0-9]+)/).flatten.first.to_i\n cols = \"\"\n loop do\n remainder = (col % 26)\n cols = alph[remainder - 1] + cols\n col = col / 26\n break if col == 0\n end\n puts \"#{cols}#{row}\"\n end\nend\n"}, {"source_code": "\n\nnumber_of_input=gets.to_i\ninput_arry=[]\ni_a=0\n\n\nloop do\n\tbreak if i_a==number_of_input\n\tinput_arry[i_a]=gets.chomp\n\ti_a +=1\nend\nputs \"______________________\"\nvalue=(1..26)\nalph=('A'..'Z')\nvalue=[*value]\nalph=[*alph]\n@hash={}\ni_b=0\n\nloop do\n\tbreak if i_b==26\n\t@hash[alph[i_b]]=value[i_b]\n\ti_b+=1\nend\t\n\nvalue=(1..26)\nalph=('A'..'Z')\nvalue=[*value]\nalph=[*alph]\n@hash2={}\ni_b=0\n\nloop do\n\tbreak if i_b==26\n\t@hash2[alph[i_b]]=value[i_b]\n\ti_b+=1\nend\t\n@hash2['Z']=0\n\n\ndef S1_to_S2 (a1)\n\tr=a1[0]\n\ti=1\n\t\n\tloop do\n\t\tbreak if a1[i].to_i !=0\n\t\tr =r + a1[i]\n\t\ti+=1\n\tend\n\n\tr1=r.length\n\tr2=a1.length\n\tc1= a1[0 .. (r1-1)].reverse\n\trow= a1[ r1 .. r2].to_i\n\tc=0\n\ti=0\n\t\n\tloop do\n\t\tbreak if i==c1.length\n\t\tc= c + (@hash[c1[i]]*(26**i))\n\t\ti+=1\n\tend\n\t\n\tputs \"R#{row}C#{c}\"\nend\n\ndef S2_to_S1 (a1)\n\ts2=a1.split(\"\").map{|s| s}\n\tr3=3\n\ts2.find {|i| r3=s2.index(i)+1 if i=='C'}\n\ti=s2[r3 .. (s2.length - 1)].join.to_i\n\trow=s2[1 .. (r3-2)].join.to_i\n\tstr_c=\"\"\n\t\n\tloop do\n\t\ti=i-26 if i>25 && i%26==0\n\t\tstr_c = str_c + @hash2.key(i%26)\n\t\ti=i/26\n\t\tbreak if i==0\n\tend\n\t\n\tstr_c=str_c.reverse\n\tputs \"#{str_c}#{row}\"\nend\n\t\ni_c =0\n\nloop do\n break if i_c==input_arry.length\n\tinput1=input_arry[i_c]\n\t\n\tif \tinput1[0]!='R' || (input1[0]=='R' && input1[1].to_i==0) \n\t\tS1_to_S2 (input1)\n\telsif input1[0]=='R' && input1[1].to_i!=0\n\t\tk2=input1.split(\"\")\n\t\tk=0\n\t\tk2.find{|s| k=1 if s=='C'}\n\t\tif k==1\n\t\t\tS2_to_S1(input1)\n\t\telse\n\t\t\tS1_to_S2 (input1)\n\t\tend\t\n\tend\n\t\n\ti_c +=1\n\t\nend\n\t"}, {"source_code": "\t#recibiendo el numero de iteraciones\n\tinput=gets.chomp\n\tn=input.to_i\n\t#metodo para saber si la cadena es excel o random\n\tdef validar_excel(a)\n\t\tif a.include?(\"R\") && a.include?(\"C\") #como el metodo random tiene R&C \n\t\t\tir=a.index(\"R\") #queremos el index de cada letra\n\t\t\tic=a.index(\"C\") #porque para q sea random, la diferencia debe ser 1\n\t\t\treturn (ic-ir)==1? true : false \n\t\telse\n\t\t\treturn true\n\t\tend\n\tend\n\t#revisar\n\tdef convertir(ar)\n\t\tn=ar.size-1\t\n\t\ti=0\n\t\tnum=0\n\t\t\n\t\tloop do\n\t\t\tif n<0\n\t\t\t\tbreak\n\t\t\tend\n\t\t\t\n\t\t\tnum=num+(ar[i].ord-64)*(26**n)\n\t\t\tn-=1\n\t\t\ti+=1\n\t\tend\n\t\t\n\t\treturn num\n\tend\n\t\n\t#primer metodo de BC23 a R23C55\n\tdef excel_to_random(arr)\n\t\tparte_num=\"\"\n\t\tparte_str=\"\"\n\t\t\n\t\t0.upto(arr.size-1) do |k|\n\t\t\tif (arr[k].ord<=57 && arr[k].ord>=48) #ASCII para diferenciar num & str\n\t\t\t\tparte_num=parte_num+arr[k]\n\t\t\telse\n\t\t\t\tparte_str=parte_str+arr[k]\n\t\t\tend\n\t\tend\n\t\t\n\t\tparte_c=convertir(parte_str)\n\t\trandom=\"R\"+\"#{parte_num}\"+\"C\"+\"#{parte_c}\" #creando el random\n\t\t\n\t\treturn random\n\tend\n\t\n\t#metodo divisiones sucesivas?\n\tdef divsuc(num)\n\t\tnum_car=26\n\t\tpivot=num\n\t\tndig=0\n\t\tarreglo_num=[]\n\t\tpotencia=1\n\t\t\n\t\tloop do\n\t\t\tpivot=pivot-26**potencia\n\t\t\tndig+=1\n\t\t\tpotencia+=1\n\t\t\tif pivot<=0\n\t\t\t\tbreak \n\t\t\tend\n\t\tend\n\t\t\n\t\t#real deal\n\t\tloop do\n\t\t\tif num==0\n\t\t\t\tbreak\n\t\t\tend\n\t\t\tif num%num_car !=0\n\t\t\t\tarreglo_num[ndig-1]=((num%num_car)+64).chr()\n\t\t\t\tnum=num/num_car\n\t\t\telse #COMPORTAMIENTO DIFERENTE CUANDO EL num ES MULTIPLE DE 26 !! (2 days!)\n\t\t\t\tq=num/num_car\n\t\t\t\tnum=q-1\n\t\t\t\tarreglo_num[ndig-1]=(num_car+64).chr()\n\t\t\tend\n\t\t\tndig-=1\n\t\tend\n\t\treturn arreglo_num\n\tend\n\t\n\t#segundo metodo de R23C55 a BC23\n\tdef random_to_excel(arr)\n\t\trow=\"\"\n\t\tcol=\"\"\n\t\ta=0\n\t\t\n\t\t1.upto(arr.size-1) do |k|\n\t\t\t(arr[k]==\"C\")? a+=1:a+=0\n\n\t\t\tif a==0\n\t\t\t\trow=row+arr[k]\n\t\t\telse\n\t\t\t\tif (arr[k]!=\"C\") \n\t\t\t\t\tcol=col+arr[k]\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\t\tcol=col.to_i\n\t\tcole=divsuc(col)#convertir la columna a excel\n\t\texcel=cole.join+row\n\t\t\n\t\treturn excel\n\tend\n\t\n\t$res=[] #definición de la variable GLOBAL, para poder imprimirlo después\n\t\n\t0.upto(n-1) do |j|\n\t\tcadena=gets.chomp.upcase\n\t\tarray=cadena.split(\"\")\n\t\ti=validar_excel(array)\n\t\t\n\t\tif i\n\t\t\tresultado=excel_to_random(array)\n\t\telse\n\t\t\tresultado=random_to_excel(array)\n\t\tend\n\t\t\t\n\t\t$res[j]=resultado \t#recordar que el puts imprime con /n y el print no lo hace\n\tend\n\t\n\t#IMPRIMIR RESULTADO\n\tputs $res"}, {"source_code": "num = gets.chomp.to_i\nnum.times do\n old = gets.chomp\n results = old.match('R(\\d+)C(\\d+)')\n if results != nil\n col_num = results[2].to_i\n base = 1;\n col_str = \"\"\n while col_num > 26\n this_char = col_num % 26\n col_num /= 26\n if this_char == 0\n this_char = 26\n end\n col_num -= 1\n col_str = \"#{(this_char+64).chr}#{col_str}\"\n end\n col_str = \"#{(col_num+64).chr}#{col_str}\"\n puts \"#{col_str}#{results[1]}\"\n else\n results = old.match('([A-Z]+)(\\d+)')\n col_str = results[1]\n base = 26**(col_str.length-1)\n col_num = 0\n col_str.each_char do |c| \n col_num += (c.ord-64)*base\n base /= 26\n end\n puts \"R#{results[2]}C#{col_num}\"\n end\nend"}, {"source_code": "def excel_to_index(number)\n row = number.gsub(/[a-zA-Z]/, '').chomp\n column = number.gsub(/\\d/, '').chomp\n\n length = column.length\n column_index = 0\n column.each_char do |c|\n column_index += (c.ord - 'A'.ord + 1) * (26 ** (length - 1))\n length -= 1\n end\n\n 'R' + row + 'C' + column_index.to_s\nend\n\ndef index_to_excel(number)\n row, column = number.sub(/R/, '').split(/C/).map(&:to_i)\n\n column_index = column.to_s(26).upcase\n column_letters = ''\n column_index.each_char do |c|\n if c.ord > '9'.ord\n column_letters += (c.ord + 9).chr\n else\n column_letters += (c.ord + 'A'.ord - '1'.ord).chr\n end \n end\n\n column_letters + row.to_s\nend \n\ncount = gets.to_i\n\ncount.times do\n num = gets.upcase\n if num[/R\\d+C\\d+/]\n puts index_to_excel num\n else\n puts excel_to_index num\n end \nend"}, {"source_code": "#=================================\n# Better be sure than sorry. \n#=================================\n$stdin = File.open(\"./input.txt\", \"r\") if ENV[\"LOGNAME\"]==\"skydos\"\n#=================================\n\ndef to_number str\n\tnum = 0\n\tdigits = [*?A..?Z]\n\t\n\tpow=1\n\tstr.reverse.each_char{|c|\t\t\n\t\td = digits.index(c) + 1\n\t\t\n\t\tnum+=d*pow\n\t\tpow*=26\n\t}\n\t\n\tnum\nend\n\ndef to_first line\n\tletters, number = line.scan(/([A-Z]+)([0-9]+)/)[0]\n\t\"R#{number}C#{to_number letters}\"\nend\n\ndef to_27 num\n\tans = \"\"\n\tdigits = [*?A..?Z]\n\t\n\tnum = num.to_i\n\twhile num!=0\n\t\tans+=digits[num%27]\n\t\tnum/=27\n\tend\n\t\n\tans\nend\n\ndef to_second line\n\ta, b = line.scan(/R([0-9]+)C([0-9]+)/)[0]\t\n\t\"#{to_27 b}#{a}\"\nend\n\nn=gets.to_i\n\nn.times{\n\tline=gets.strip\n\t\n\tif /R([0-9]+)C([0-9]+)/.match line\n\t\tputs to_second line\n\telse\n\t\tputs to_first line\n\tend\n}\n"}, {"source_code": "#!/usr/bin/ruby\ndef fromcell(s)\n r=0\n s.length.times{|i|\n r=r*26+s[i]-64\n }\n return r\nend\n\ndef tocell(n)\n r=\"\"\n while n>0 do\n r=(n%26+64).chr+r\n n/=26\n end\n return r\nend\n\nn=gets.chomp.to_i\nn.times{|z|\n s=gets.chomp\n if s=~/^([A-Z]+)(\\d+)$/ then \nputs \"R\"\n#+$2.to_s+\"C\"+fromcell($1).to_s\nend\n if s=~/R(\\d+)C(\\d+)/ then puts tocell($2.to_i)+$1.to_s end\n}\nexit(0)\n"}, {"source_code": "n = STDIN.gets\nn.to_i.times do |i|\n a = STDIN.gets\n if a =~ /\\d+[A-Z]+/\n r = a.split(/[A-Z]/)[1]\n c = a.split(/[A-Z]/)[2].to_i\n completeAddress = Array.new\n while (c != 0)\n completeAddress.push((c%26 + 64).chr)\n c /= 26\n end\n completeAddress = completeAddress.reverse.join(\"\")\n completeAddress += r + \"\\n\"\n print completeAddress\n else\n c = a[0,a=~/\\d/]\n r = a.gsub(/[A-Z]+/,\"\")\n realrow = 0\n c.bytes.each do |i|\n realrow *= 26\n realrow += (i - 64)\n end\n print \"R\" + r.strip + \"C\" + realrow.to_s + \"\\n\"\n end\nend "}, {"source_code": "def f(x)\nreturn x<27?((x+64).chr):(f(x/26)+(x%26+65).chr)\nend\na=*$<.drop(1).map(&:chomp).each{|x|puts /R(\\d+)C(\\d+)/.match(x)?(f($2.to_i-1)+$1):(y=0;v=1;/(\\D+)(\\d+)/.match(x);$1.reverse.each_char{|c|y+=(c.ord-64)*v;v*=26};\"R\"+$2+\"C\"+y.to_s)}"}, {"source_code": "class SpreadSheets\n def main\n numberOfLines = gets.to_i\n coordinates = Array.new(numberOfLines)\n\n i = 0\n while i < numberOfLines\n coordinates[i] = gets\n i += 1\n end\n\n puts convert(coordinates)\n end\n\n def convert(coordinates)\n result = Array::new\n numericDef = /R(\\d+)C(\\d+)/\n alphanumericDef = /([a-zA-Z]+)(\\d+)/\n\n coordinates.each do |coordinate|\n if (numericDef.match(coordinate))\n matches = numericDef.match(coordinate)\n row = matches[1]\n column = matches[2]\n convertedColumn = convertNumToAlpha(column.to_i)\n result.push(\"#{convertedColumn}#{row}\")\n elsif (alphanumericDef.match(coordinate))\n matches = alphanumericDef.match(coordinate)\n row = matches[2]\n column = matches[1]\n convertedColumn = convertAlphaToNum(column)\n result.push(\"R#{row}C#{convertedColumn}\")\n end\n end\n\n return result\n end\n\n def convertNumToAlpha(num)\n result = String::new\n\n zeroLetter = 'A';\n base26 = 26;\n loop do\n num -= 1;\n remainder = num % base26;\n result = \"#{(zeroLetter.ord + remainder).chr}#{result}\"\n num /= base26;\n break if (num == 0)\n end\n\n return result\n end\n\n def convertAlphaToNum(alpha)\n result = 0\n\n zeroLetter = 'A'\n currentExp = 1\n base26 = 26\n alpha.split(\"\").reverse_each do |letter|\n convertedDigit = letter.ord - zeroLetter.ord\n result = result * currentExp + (convertedDigit + 1)\n currentExp *= base26\n end\n\n return result.to_s\n end\nend\n\nSpreadSheets::new.main\n"}, {"source_code": "\t#recibiendo el numero de iteraciones\n\tinput=gets.chomp\n\tn=input.to_i\n\t#metodo para saber si la cadena es excel o random\n\tdef validar_excel(a)\n\t\tif a.include?(\"R\") && a.include?(\"C\") #como el metodo random tiene R&C \n\t\t\tir=a.index(\"R\") #queremos el index de cada letra\n\t\t\tic=a.index(\"C\") #porque para q sea random, la diferencia debe ser 1\n\t\t\treturn (ic-ir)==1? true : false \n\t\telse\n\t\t\treturn true\n\t\tend\n\tend\n\t#revisar\n\tdef convertir(ar)\n\t\tn=ar.size-1\t\n\t\ti=0\n\t\tnum=0\n\t\t\n\t\tloop do\n\t\t\tif n<0\n\t\t\t\tbreak\n\t\t\tend\n\t\t\t\n\t\t\tnum=num+(ar[i].ord-64)*(26**n)\n\t\t\tn-=1\n\t\t\ti+=1\n\t\tend\n\t\t\n\t\treturn num\n\tend\n\t\n\t#primer metodo de BC23 a R23C55\n\tdef excel_to_random(arr)\n\t\tparte_num=\"\"\n\t\tparte_str=\"\"\n\t\t\n\t\t0.upto(arr.size-1) do |k|\n\t\t\tif (arr[k].ord<=57 && arr[k].ord>=48) #ASCII para diferenciar num & str\n\t\t\t\tparte_num=parte_num+arr[k]\n\t\t\telse\n\t\t\t\tparte_str=parte_str+arr[k]\n\t\t\tend\n\t\tend\n\t\t\n\t\tparte_c=convertir(parte_str)\n\t\trandom=\"R\"+parte_num+\"C\"+\"#{parte_c}\" #creando el random\n\t\t\n\t\treturn random\n\tend\n\t\n\t#metodo divisiones sucesivas?\n\tdef divsuc(num)\n\t\tnum_car=26\n\t\tpivot=num\n\t\tndig=0\n\t\tarreglo_num=[]\n\t\tpotencia=1\n\t\t\n\t\tloop do\n\t\t\tpivot=pivot-26**potencia\n\t\t\tndig+=1\n\t\t\tpotencia+=1\n\t\t\tif pivot<=0\n\t\t\t\tbreak \n\t\t\tend\n\t\tend\n\t\t\n\t\t#real deal\n\t\tloop do\n\t\t\tif num==0\n\t\t\t\tbreak\n\t\t\tend\n\t\t\tif num%num_car !=0\n\t\t\t\tarreglo_num[ndig-1]=((num%num_car)+64).chr()\n\t\t\t\tnum=num/num_car\n\t\t\telse #COMPORTAMIENTO DIFERENTE CUANDO EL num ES MULTIPLE DE 26 !! (2 days!)\n\t\t\t\tq=num/num_car\n\t\t\t\tnum=q-1\n\t\t\t\tarreglo_num[ndig-1]=(num_car+64).chr()\n\t\t\tend\n\t\t\tndig-=1\n\t\tend\n\t\treturn arreglo_num\n\tend\n\t\n\t#segundo metodo de R23C55 a BC23\n\tdef random_to_excel(arr)\n\t\trow=\"\"\n\t\tcol=\"\"\n\t\ta=0\n\t\t\n\t\t1.upto(arr.size-1) do |k|\n\t\t\t(arr[k]==\"C\")? a+=1:a+=0\n\n\t\t\tif a==0\n\t\t\t\trow=row+arr[k]\n\t\t\telse\n\t\t\t\tif (arr[k]!=\"C\") \n\t\t\t\t\tcol=col+arr[k]\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\t\tcol=col.to_i\n\t\tcole=divsuc(col)#convertir la columna a excel\n\t\texcel=cole.join+row\n\t\t\n\t\treturn excel\n\tend\n\t\n\t$res=[] #definición de la variable GLOBAL, para poder imprimirlo después\n\t\n\t0.upto(n-1) do |j|\n\t\tcadena=gets.chomp.upcase\n\t\tarray=cadena.split(\"\")\n\t\ti=validar_excel(array)\n\t\t\n\t\tif i\n\t\t\tresultado=excel_to_random(array)\n\t\telse\n\t\t\tresultado=random_to_excel(array)\n\t\tend\n\t\t\t\n\t\t$res[j]=resultado \t#recordar que el puts imprime con /n y el print no lo hace\n\tend\n\t\n\t#IMPRIMIR RESULTADO\n\tputs $res"}, {"source_code": "def excel_to_index(number)\n row = number.gsub(/[a-zA-Z]/, '').chomp\n column = number.gsub(/\\d/, '').chomp\n\n length = column.length\n column_index = 0\n column.each_char do |c|\n column_index += (c.ord - 'A'.ord + 1) * (26 ** (length - 1))\n length -= 1\n end\n\n 'R' + row + 'C' + column_index.to_s\nend\n\ndef index_to_excel(number)\n row, column = number.sub(/R/, '').split(/C/).map(&:to_i)\n\n column_index = column.to_s(26)\n column_letters = ''\n column_index.each_char do |c|\n if c.ord > '9'.ord\n column_letters += (c.ord + 9).chr\n else\n column_letters += (c.ord + 'A'.ord - '1'.ord).chr\n end \n end\n\n column_letters + row.to_s\nend \n\ncount = gets.to_i\n\ncount.times do\n num = gets.upcase\n if num[/R\\d+C\\d+/]\n puts index_to_excel num\n else\n puts excel_to_index num\n end \nend"}, {"source_code": "num = gets.chomp.to_i\nnum.times do\n old = gets.chomp\n results = old.match('R(\\d+)C(\\d+)')\n if results != nil\n col_num = results[2].to_i\n base = 1;\n col_str = \"\"\n while col_num > 26\n this_char = col_num % 26\n if this_char == 0\n this_char = 26\n end\n col_num /= 26\n col_str = \"#{(this_char+64).chr}#{col_str}\"\n end\n col_str = \"#{(col_num+64).chr}#{col_str}\"\n puts \"#{col_str}#{results[1]}\"\n else\n results = old.match('([A-Z]+)(\\d+)')\n col_str = results[1]\n base = 26**(col_str.length-1)\n col_num = 0\n col_str.each_char do |c| \n col_num += (c.ord-64)*base\n base /= 26\n end\n puts \"R#{results[2]}C#{col_num}\"\n end\nend"}, {"source_code": "$table = [\"A\", \"B\", \"C\", \"D\", \"E\", \"F\", \"G\", \"H\", \"I\", \"J\", \"K\", \"L\", \"M\", \"N\", \"O\", \"P\", \"Q\", \"R\", \"S\", \"T\", \"U\", \"V\", \"W\", \"X\", \"Y\", \"Z\"]\n\ndef to_E(x, y)\n b = \"\"\n if x % 26 == 0 then\n b = to_E(x-1, y)\n b[-y.size-1] = \"Z\"\n else\n while x > 26 do\n b = $table[(x % 26) - 1] + b\n x /= 26\n end\n b = $table[x-1] + b\n b += y \n end\n return b\nend\n\ndef to_T(x)\n str = x.gsub(/[0123456789]/, \"*\")\n k = str.index(\"*\")\n str1 = str[0...k]\n r = 0\n q = 26\n n = str1.size\n n.times do |i|\n r += ($table.index(str1[i])+1)*q**(n-i-1)\n end\n result = \"R\" + x[k..-1] + \"C\" + r.to_s\n return result \nend\n\nn = gets.to_i\nind = []\noutd = []\nn.times do |i|\n ind[i] = gets.chomp\n r = ind[i].index(\"R\")\n c = ind[i].index(\"C\")\n if r && c && c-r > 1 then\n ind[i] = to_E(ind[i][c+1..-1].to_i, ind[i][r+1...c])\n else\n ind[i] = to_T(ind[i])\n end\nend\nn.times do |i|\n puts \"#{ind[i]}\"\nend"}, {"source_code": "def f(x)\nreturn x<27?((x+64).chr):(f(x/26)+(x%26+65).chr)\nend\na=*$<.drop(1).map(&:chomp).each{|x|puts /R(\\d+)C(\\d+)/.match(x)?(f($2.to_i-1)+$1):(y=0;v=1;/(\\D+)(\\d+)/.match(x);$1.reverse.each_char{|c|y+=(c.ord-64)*v;v*=26};\"R\"+$2+\"C\"+y.to_s)}"}, {"source_code": "$letters = [\"\"].concat((\"A\"..\"Z\").to_a)\n\ndef rc_ab(rc)\n rc_a = rc.scan(/\\d+/)\n rc_a[1] = rc_a[1].to_i\n s = \"\"\n \n while(rc_a[1] != 0) do\n s << $letters[rc_a[1] % 26]\n rc_a[1] /= 26\n end\n \n s = s.reverse << rc_a[0]\n return s\nend \n\ndef ab_rc(ab)\n ab_c = /[A-Z]+/.match(ab)\n \n ab_s = ab_c.to_s.reverse\n ab_n = 0\n \n ab_s.length.times {\n |i|\n ab_n += $letters.index(ab_s[i,1]) * 26 ** i\n }\n \n s = \"R\" << ab_n.to_s << \"C\" << /\\d+/.match(ab).to_s\n return s\nend\n\nn = gets().chomp.to_i\na = []\nn.times {\n|i|\n a[i] = gets().chomp()\n}\na.each{\n|i|\n if (/^R\\d+C\\d+$/ =~ i) then\n #RXCY \n puts(rc_ab(i))\n else\n #AB23\n puts(ab_rc(i))\n end\n}\n"}, {"source_code": "letters = %w(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z)\n\ngets.to_i.times do\n input = gets.strip\n if input =~ /R(\\d)+C/\n codes = input.gsub(\"R\",\"\").split(\"C\").map(&:to_i)\n ans = \"\"\n codes[1]\n puts codes[1].inspect\n while codes[1] > 0 do\n ans << letters[(codes[1]%26-1)]\n puts codes[1].inspect\n codes[1] /= 26\n codes[1] -= 1\n end\n\n\n puts \"#{ans.reverse}#{codes[0]}\"\n else\n code = input.match(/([A-Z])+/).to_s\n sum = 0\n code.chars.each_with_index do |el,i|\n sum += (1+letters.index(el)) * (26**(code.size-i-1))\n end\n puts \"R#{input.match(/([0-9])+/)}C#{sum}\"\n end\nend"}, {"source_code": "num = gets.chomp.to_i\nputs num\nnum.times do\n old = gets.chomp\n results = old.match('R(\\d+)C(\\d+)')\n if results != nil\n col_num = results[2].to_i\n base = 1;\n col_str = \"\"\n while col_num > 26\n this_char = col_num % 26\n col_num /= 26\n col_str = \"#{(this_char+64).chr}#{col_str}\"\n end\n col_str = \"#{(col_num+64).chr}#{col_str}\"\n puts \"#{col_str}#{results[1]}\"\n else\n results = old.match('([A-Z]+)(\\d+)')\n col_str = results[1]\n base = 26**(col_str.length-1)\n col_num = 0\n col_str.each_char do |c| \n col_num += (c.ord-64)*base\n base /= 26\n end\n puts \"R#{results[2]}C#{col_num}\"\n end\nend"}, {"source_code": "def f(x)\nreturn x<27?((x+65).chr):(f(x/26-1)+(x%26+65).chr)\nend\na=*$<.drop(1).map(&:chomp).each{|x|puts /R(\\d+)C(\\d+)/.match(x)?(f($2.to_i-1)+$1):(y=0;v=1;/(\\D+)(\\d+)/.match(x);$1.reverse.each_char{|c|y+=(c.ord-64)*v;v*=26};\"R\"+$2+\"C\"+y.to_s)}"}, {"source_code": "#=================================\n# Better be sure than sorry. \n#=================================\n$stdin = File.open(\"./input.txt\", \"r\") if ENV[\"LOGNAME\"]==\"skydos\"\n#=================================\n\ndef to_number str\n\tnum = 0\n\tdigits = [*?A..?Z]\n\t\n\tpow=1\n\tstr.reverse.each_char{|c|\t\t\n\t\td = digits.index(c) + 1\n\t\t\n\t\tnum+=d*pow\n\t\tpow*=26\n\t}\n\t\n\tnum\nend\n\ndef to_first line\n\tletters, number = line.scan(/([A-Z]+)([0-9]+)/)[0]\n\t\"R#{number}C#{to_number letters}\"\nend\n\ndef to_27 num\n\tans = \"\"\n\tdigits = [*?A..?Z]\n\t\n\tnum = num.to_i\n\twhile num!=0\n\t\tans = digits[num%26-1] + ans\n\t\tnum/=26\n\tend\n\t\n\tans\nend\n\ndef to_second line\n\ta, b = line.scan(/R([0-9]+)C([0-9]+)/)[0]\t\n\t\"#{to_27 b}#{a}\"\nend\n\nn=gets.to_i\n\nn.times{\n\tline=gets.strip\n\t\n\tif /R([0-9]+)C([0-9]+)/.match line\n\t\tputs to_second line\n\telse\n\t\tputs to_first line\n\tend\n}\n"}, {"source_code": "def solve(input)\n if (/R\\d+C\\d+/ =~ input) == nil\n fromBase26(input)\n else\n toBase26(input)\n end\nend\n\ndef fromBase26(input)\n charByte = \"A\".bytes[0]\n numByte = \"1\".bytes[0]\n letters = Array.new\n numbers = Array.new\n\n input.chomp.each_byte do |byte|\n if byte < charByte\n numbers.unshift(byte-(numByte-1))\n else\n letters.unshift(byte-(charByte-1))\n end\n end\n\n rows = arrayToInt(numbers, 10)\n cols = arrayToInt(letters, 26)\n \"R#{rows}C#{cols}\"\nend\n\ndef toBase26(input)\n input[0] = \"\"\n rows, cols = input.split(\"C\")\n val = cols.to_i\n digits = []\n while val != 0\n digits.unshift(val%26)\n val /= 26\n end\n cols = \"\"\n for digit in digits\n val = digit + (\"A\".bytes[0]-1)\n cols += val.chr\n end\n cols + \"#{rows}\"\nend\n\ndef arrayToInt(arr, base)\n curr = 1\n int = 0\n for val in arr\n int += val*curr\n curr *= base\n end\n int\nend\n\nn = gets.to_i\nans = Array.new(n, nil)\nfor i in (0...n)\n ans[i] = solve(gets)\nend\n\nfor answer in ans\n puts answer\nend\n"}, {"source_code": "#!/usr/bin/env ruby\n\ndef convert(str)\n if str =~ /^R(\\d+)C(\\d+)$/\n rows, cols = $1.to_i, $2.to_i\n cols -= 1\n a = []\n loop do\n a.unshift cols % 26\n cols /= 26\n break if cols == 0\n end\n \"#{a.map{|i| (i + 65).chr}.join}#{rows}\"\n elsif str =~ /^([A-Z]+)(\\d+)$/\n cols = 0\n $1.size.times do |i|\n cols += ($1.unpack('C*')[-i - 1] - 64) * (26 ** i)\n end\n \"R#{$2}C#{cols}\"\n else\n \"invalid arg\"\n end\nend\n\nn = gets.to_i\nlines = []\nn.times do\n lines << convert(gets.chomp)\nend\n\nlines.each{|s| puts s}\n"}, {"source_code": "def excel_to_index(number)\n row = number.gsub(/[a-zA-Z]/, '').chomp\n column = number.gsub(/\\d/, '').chomp\n\n length = column.length\n column_index = 0\n column.each_char do |c|\n column_index += (c.ord - 'A'.ord + 1) * (26 ** (length - 1))\n length -= 1\n end\n\n 'R' + row + 'C' + column_index.to_s\nend\n\ndef index_to_excel(number)\n row, column = number.sub(/R/, '').split(/C/).map(&:to_i)\n\n column_index = column.to_s(26).upcase\n column_letters = ''\n column_index.each_char do |c|\n if c.ord > '9'.ord\n column_letters += (c.ord + 9).chr\n else\n column_letters += (c.ord + 'A'.ord - '1'.ord).chr\n end \n end\n\n column_letters + row.to_s\nend \n\ncount = gets.to_i\n\ncount.times do\n num = gets.upcase\n if num[/R\\d+C\\d+/]\n puts index_to_excel num\n else\n puts excel_to_index num\n end \nend"}, {"source_code": "class Integer\n private\n LETTERS = [\"Z\"].concat((\"A\"..\"Y\").to_a)\n \n public\n def to_ab\n number = self\n ab = \"\"\n while(number != 0) do\n ab << LETTERS[number % 26]\n number /= 26\n end\n return ab.reverse\n end\nend\n\nclass String\n private\n LETTERS = (\"A\"..\"Z\").to_a\n \n public\n def to_rc\n rstring = self.clone.reverse!\n rc = 0\n rstring.length.times {\n |i|\n rc += ((LETTERS.index(rstring[i,1]) + 1) * (26 ** i))\n }\n return rc.to_s\n end\nend\n\ndef rc_ab(rc)\n row, column = rc.scan(/\\d+/)\n column = column.to_i.to_ab\n ab = column << row\n return ab\nend\n\ndef ab_rc(ab)\n column, row = ab.scan(/([A-Z]+)(\\d+)/)[0]\n column = column.to_rc\n rc = \"R\" << row << \"C\" << column\n return rc\nend\n\nn = gets().chomp.to_i\na = []\nn.times {\n|i|\n a[i] = gets().chomp()\n}\na.each{\n|i|\n if (/^R\\d+C\\d+$/ =~ i) then\n #RXCY \n puts(rc_ab(i))\n else\n #AB23\n puts(ab_rc(i))\n end\n}"}, {"source_code": "\t#recibiendo el numero de iteraciones\n\tinput=gets.chomp\n\tn=input.to_i\n\t#metodo para saber si la cadena es excel o random\n\tdef validar_excel(a)\n\t\tif a.include?(\"R\") && a.include?(\"C\") #como el metodo random tiene R&C \n\t\t\tif a.index(\"R\")==0\n\t\t\t\tir=a.index(\"R\") #queremos el index de cada letra\n\t\t\t\tic=a.index(\"C\") #porque para q sea random, la diferencia debe ser 1\n\t\t\t\treturn (ic-ir)==1? true : false \n\t\t\telse\n\t\t\t\treturn true\n\t\t\tend\n\t\telse\n\t\t\treturn true\n\t\tend\n\tend\n\t#revisar\n\tdef convertir(ar)\n\t\tn=ar.size-1\t\n\t\ti=0\n\t\tnum=0\n\t\t\n\t\tloop do\n\t\t\tif n<0\n\t\t\t\tbreak\n\t\t\tend\n\t\t\t\n\t\t\tnum=num+(ar[i].ord-64)*(26**n)\n\t\t\tn-=1\n\t\t\ti+=1\n\t\tend\n\t\t\n\t\treturn num\n\tend\n\t\n\t#primer metodo de BC23 a R23C55\n\tdef excel_to_random(arr)\n\t\tparte_num=\"\"\n\t\tparte_str=\"\"\n\t\t\n\t\t0.upto(arr.size-1) do |k|\n\t\t\tif (arr[k].ord>=48 && arr[k].ord<=57) #ASCII para diferenciar num & str\n\t\t\t\tparte_num=parte_num+arr[k]\n\t\t\telse\n\t\t\t\tparte_str=parte_str+arr[k]\n\t\t\tend\n\t\tend\n\t\n\t\tparte_c=convertir(parte_str)\n\t\trandom=\"R\"+parte_num+\"C\"+\"#{parte_c}\" #creando el random\n\t\t\n\t\treturn random\n\tend\n\t\n\t#metodo divisiones sucesivas?\n\tdef divsuc(num)\n\t\tnum_car=26\n\t\tpivot=num\n\t\tndig=0\n\t\tarreglo_num=[]\n\t\tpotencia=1\n\t\t\n\t\tloop do\n\t\t\tpivot=pivot-26**potencia\n\t\t\tndig+=1\n\t\t\tpotencia+=1\n\t\t\tif pivot<=0\n\t\t\t\tbreak \n\t\t\tend\n\t\tend\n\t\t\n\t\t#real deal\n\t\tloop do\n\t\t\tif num==0\n\t\t\t\tbreak\n\t\t\tend\n\t\t\tif num%num_car !=0\n\t\t\t\tarreglo_num[ndig-1]=((num%num_car)+64).chr()\n\t\t\t\tnum=num/num_car\n\t\t\telse #COMPORTAMIENTO DIFERENTE CUANDO EL num ES MULTIPLE DE 26 !! (2 days!)\n\t\t\t\tq=num/num_car\n\t\t\t\tnum=q-1\n\t\t\t\tarreglo_num[ndig-1]=(num_car+64).chr()\n\t\t\tend\n\t\t\tndig-=1\n\t\tend\n\t\treturn arreglo_num\n\tend\n\t\n\t#segundo metodo de R23C55 a BC23\n\tdef random_to_excel(arr)\n\t\trow=\"\"\n\t\tcol=\"\"\n\t\ta=0\n\t\t\n\t\t1.upto(arr.size-1) do |k|\n\t\t\t(arr[k]==\"C\")? a+=1:a+=0\n\n\t\t\tif a==0\n\t\t\t\trow=row+arr[k]\n\t\t\telse\n\t\t\t\tif (arr[k]!=\"C\") \n\t\t\t\t\tcol=col+arr[k]\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\t\tcol=col.to_i\n\t\tcole=divsuc(col)#convertir la columna a excel\n\t\texcel=cole.join+row\n\t\t\n\t\treturn excel\n\tend\n\t\n\t$res=[] #definición de la variable GLOBAL, para poder imprimirlo después\n\t\n\t0.upto(n-1) do |j|\n\t\tcadena=gets.chomp.upcase\n\t\tarray=cadena.split(\"\")\n\t\ti=validar_excel(array)\n\t\t\n\t\tif i\n\t\t\tresultado=excel_to_random(array)\n\t\telse\n\t\t\tresultado=random_to_excel(array)\n\t\tend\n\t\t\t\n\t\t$res[j]=resultado \t#recordar que el puts imprime con /n y el print no lo hace\n\tend\n\t#IMPRIMIR RESULTADO\n\tputs $res"}, {"source_code": "$letters = [\"\"].concat((\"A\"..\"Z\").to_a)\n\ndef rc_ab(rc)\n rc_a = rc.scan(/\\d+/)\n rc_a[1] = rc_a[1].to_i\n s = \"\"\n \n while(rc_a[1] != 0) do\n s << $letters[rc_a[1] % 26]\n rc_a[1] /= 26\n end\n \n s = s.reverse << rc_a[0]\n return s\nend \n\ndef ab_rc(ab)\n ab_c = /[A-Z]+/.match(ab)\n \n ab_s = ab_c.to_s.reverse\n ab_n = 0\n \n ab_s.length.times {\n |i|\n ab_n += $letters.index(ab_s[i,1]) * 26 ** i\n }\n \n s = \"R\" << ab_n.to_s << \"C\" << /\\d+/.match(ab).to_s\n return s\nend\n\nn = gets().chomp.to_i\na = []\nn.times {\n|i|\n a[i] = gets().chomp()\n}\na.each{\n|i|\n if (/^R\\d+C\\d+$/ =~ i) then\n #RXCY\n puts(\"rc_ab\")\n puts(rc_ab(i))\n else\n #AB23\n puts(ab_rc(i))\n end\n}\n"}, {"source_code": "def excel_to_index(number)\n row = number.gsub(/[a-zA-Z]/, '').chomp\n column = number.gsub(/\\d/, '').chomp\n\n length = column.length\n column_index = 0\n column.each_char do |c|\n column_index += (c.ord - 'A'.ord + 1) * (26 ** (length - 1))\n length -= 1\n end\n\n 'R' + row + 'C' + column_index.to_s\nend\n\ndef index_to_excel(number)\n row, column = number.sub(/R/, '').split(/C/).map(&:to_i)\n\n column_index = column.to_s(26).upcase\n column_letters = ''\n column_index.each_char do |c|\n if c.ord > '9'.ord\n if '0' == c\n column_letters += 'Z'\n else\n column_letters += (c.ord + 9).chr\n end\n else\n column_letters += (c.ord + 'A'.ord - '1'.ord).chr\n end \n end\n\n column_letters + row.to_s\nend \n\ncount = gets.to_i\n\ncount.times do\n num = gets.upcase\n if num[/R\\d+C\\d+/]\n puts index_to_excel num\n else\n puts excel_to_index num\n end \nend"}, {"source_code": "def excel_to_index(number)\n row = number.gsub(/[a-zA-Z]/, '').chomp\n column = number.gsub(/\\d/, '').chomp\n\n length = column.length\n column_index = 0\n column.each_char do |c|\n column_index += (c.ord - 'A'.ord + 1) * (26 ** (length - 1))\n length -= 1\n end\n\n 'R' + row + 'C' + column_index.to_s\nend\n\ndef index_to_excel(number)\n row, column = number.sub(/R/, '').split(/C/).map(&:to_i)\n\n column_index = column.to_s(26)\n column_letters = ''\n column_index.each_char do |c|\n if c.ord > '9'.ord\n column_letters += (c.ord + 9).chr\n else\n column_letters += (c.ord + 'A'.ord - '1'.ord).chr\n end \n end\n\n column_letters + row.to_s\nend \n\ncount = gets.to_i\n\ncount.times do\n num = gets.upcase\n if num[/R\\d+C\\d+/]\n puts index_to_excel num\n else\n puts excel_to_index num\n end \nend"}, {"source_code": "puts $<.read.split.drop(1).map { |x|\n if x[/(.)(\\d+)(.)(\\d+)/]\n (\"A\"..\"Z\").to_a.join[$4.to_i.div 26] + (\"A\"..\"Z\").to_a.join[$4.to_i % 26] + $2\n else\n t = x[/\\D+/][-2]\n \"R#{x[/\\d+/]}C#{(t ? t.ord-\"A\".ord+1 : 0)*26+x[/\\D+/][-1].ord-\"A\".ord+1}\"\n end\n}"}, {"source_code": "#=================================\n# Better be sure than sorry. \n#=================================\n$stdin = File.open(\"./input.txt\", \"r\") if ENV[\"LOGNAME\"]==\"skydos\"\n#=================================\n\ndef to_letters num\n\tans=\"\"\n\tdigs=[*?A..?Z]\n\t\n\twhile num!=0\n\t\tans+=digs[num%26 - 1]\n\t\tnum/=26\n\tend\n\t\n\tans.reverse\nend\n\ndef to_numbers str\n\tans=0\n\tstr.reverse!\n\t\n\tpow=0\n\tstr.each_char{|c|\n\t\tans+= (c.ord - 'A'.ord + 1) * 26**pow\n\t\tpow+=1\n\t}\n\t\n\tans\nend\n\nn=gets.to_i\n\nn.times{\n\tline=gets.strip\n\t\n\tif /R(\\d+)C(\\d+)/.match line\n\t\tputs \"#{to_letters $2.to_i}#{$1}\"\n\telse\n\t\tp1, p2 = line.scan(/([A-Z]+)([0-9]+)/)[0]\n\t\tputs \"R#{p2}C#{to_numbers p1}\"\n\tend\n}\n"}, {"source_code": "def to_ex d\n str = \"\"\n while d > 1\n d, a = d.divmod(26)\n str = (a + 64).chr + str\n end\n str\nend\n\ndef ex_to str\n str.chars.each.with_index.inject(0){ |z, (v, i)| z = z * 26 + (v.ord - 64) }\nend\n\ndef solution str\n a, b = str.scan(/\\d+/).map(&:to_i)\n if b\n \"#{to_ex(b)}#{a}\"\n else\n a, b = str.scan(/([A-Z]+)(\\d+)/).first\n \"R#{b}C#{ex_to(a)}\"\n end \nend\n\nn = STDIN.gets.to_i\nArray.new(n){ solution(STDIN.gets.chomp) }.each{ |v| p v }"}, {"source_code": "num = gets.chomp.to_i\nnum.times do\n old = gets.chomp\n results = old.match('R(\\d+)C(\\d+)')\n if results != nil\n col_num = results[2].to_i\n base = 1;\n col_str = \"\"\n while col_num > 26\n this_char = col_num % 26\n col_num /= 26\n if this_char == 0\n this_char = 26\n end\n col_num -= 1\n col_str = \"#{(this_char+64).chr}#{col_str}\"\n end\n col_str = \"#{(col_num+64).chr}#{col_str}\"\n puts \"#{col_str}#{results[1]}\"\n else\n results = old.match('([A-Z]+)(\\d+)')\n col_str = results[1]\n base = 26**(col_str.length-1)\n col_num = 0\n col_str.each_char do |c| \n col_num += (c.ord-64)*base\n base /= 26\n end\n puts \"R#{results[2]}C#{col_num}\"\n end\nend"}, {"source_code": "def f(s)\n\tr=0\n\ts.each_byte{|c| r=r*10+c-64}\n\tr\nend\ndef g(n)\n\tr=\"\"\n\twhile n>0 do\n\t\tx=n%26\n\t\tif x==0 then\n\t\t\tx=26\n\t\t\tn-=x\n\t\tend\n\t\tr=(x+64).chr+r\n\t\tn/=26\n\tend\n\tr\nend\nn=gets.to_i\nn.times{\n\ts=gets\n\tif s=~/^([A-Z]+)(\\d+)$/ then\n\t\tputs \"R\"+$2.to_s+\"C\"+f($1).to_s\n\tend\n\tif s=~/R(\\d+)C(\\d+)/ then\n\t\tputs g($2.to_i)+$1\n\tend\n}\n"}, {"source_code": "#=================================\n# Better be sure than sorry. \n#=================================\n$stdin = File.open(\"./input.txt\", \"r\") if ENV[\"LOGNAME\"]==\"skydos\"\n#=================================\n\ndef to_number str\n\tnum = 0\n\tdigits = [*?A..?Z]\n\t\n\tpow = 0\n\tstr.reverse.each_char{|c|\t\t\n\t\td = digits.index(c)+1\n\t\t\n\t\tnum+=d * 26**pow\n\t\tpow+=1\n\t}\n\t\n\tnum\nend\n\ndef to_first line\n\tletters, number = line.scan(/([A-Z]+)([0-9]+)/)[0]\n\t\"R#{number}C#{to_number letters}\"\nend\n\ndef to_27 num\n\tans = \"\"\n\tdigits = [*?A..?Z]\n\t\n\tnum = num.to_i\n\twhile num!=0\n\t\tans = digits[num%27-1] + ans\n\t\tnum/=27\n\tend\n\t\n\tans.reverse\nend\n\ndef to_second line\n\ta, b = line.scan(/R([0-9]+)C([0-9]+)/)[0]\t\n\t\"#{to_27 b}#{a}\"\nend\n\nn=gets.to_i\n\nn.times{\n\tline=gets.strip\n\t\n\tif /R([0-9]+)C([0-9]+)/.match line\n\t\tputs to_second line\n\telse\n\t\tputs to_first line\n\tend\n}\n"}, {"source_code": "arr = STDIN.read.split(\"\\n\")\nn = arr[0].to_i\n\n1.upto(n).each do |i|\n str = arr[i]\n match = str.match(/R([0-9]+)C([0-9]+)/)\n\n if match\n row = match[2].to_i\n col = match[1]\n\n r = row\n str = \"\"\n while(r > 0) do\n val = (r-1) % 26\n str += (val + 65).chr\n r /= 26\n end\n str = str.reverse\n str += col\n puts str\n else\n match = str.match(/([A-Z]+)([0-9]+)/)\n col = match[1]\n row = match[2]\n\n s = 0\n rev = col.reverse.split(\"\")\n rev.each_index do |i|\n chr = (rev[i].ord - 65 + 1) * ( 26 ** i )\n puts \"chr:#{chr}\"\n s += chr\n end\n\n puts \"R#{row}C#{s}\"\n end\nend"}, {"source_code": "def n2s(n)\n s = \"\"\n while n > 0\n s = (\"A\".ord + n % 26 - 1).chr + s\n n /= 26\n end\n return s\nend\n\ndef s2n(s)\n n = 0\n s.size.times do |i|\n n *= 26\n n += s[i].ord - \"A\".ord + 1\n end\n return n\nend\n\nn = gets.to_i\nn.times do\n str = gets\n if str =~ /^([A-Z]+)(\\d+)$/\n c_s = $1\n r = $2.to_i\n c = s2n(c_s)\n puts \"R#{r}C#{c}\"\n elsif str =~ /^R(\\d+)C(\\d+)$/\n r = $1.to_i\n c = $2.to_i\n c_s = n2s(c)\n puts \"#{c_s}#{r}\"\n end\nend\n"}, {"source_code": "def to_ex d\n str = \"\"\n while d > 1\n d, a = d.divmod(26)\n str = (a + 64).chr + str\n end\n str\nend\n\ndef ex_to str\n str.chars.each.with_index.inject(0){ |z, (v, i)| z = z * 26 + (v.ord - 64) }\nend\n\ndef solution str\n a, b = str.scan(/\\d+/).map(&:to_i)\n if b\n p \"#{to_ex(b)}#{a}\"\n else\n a, b = str.scan(/([A-Z]+)(\\d+)/).first\n p \"R#{b}C#{ex_to(a)}\"\n end \nend\n\nn = STDIN.gets.to_i\np Array.new(n){ solution(STDIN.gets.chomp) }"}, {"source_code": "alph = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']\n\nn = gets.chomp.to_i\n\nlines = []\nn.times {lines << gets.chomp}\nlines.each do |l|\n if (l =~ /R\\d+C\\d+/).nil?\n col = 0\n colstr = l.scan(/[A-Z]+/).first.split(\"\")\n colstr.each_with_index do |ch, index|\n col += (alph.index(ch) + 1) * (26 ** (colstr.length - index - 1))\n end\n row = l.scan(/[0-9]+/).first\n puts \"R#{row}C#{col}\"\n else\n row = l.scan(/R([0-9]+)/).flatten.first\n col = l.scan(/C([0-9]+)/).flatten.first.to_i\n cols = \"\"\n loop do\n remainder = (col % 26)\n cols = alph[remainder - 1] + cols\n col = col / 26\n break if col == 0\n end\n puts \"#{cols}#{row}\"\n end\nend\n"}, {"source_code": "alph = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']\n\nn = gets.chomp.to_i\n\nlines = []\nn.times {lines << gets.chomp}\nlines.each do |l|\n if (l =~ /R\\d+C\\d+/).nil?\n col = 0\n colstr = l.scan(/[A-Z]+/).first.split(\"\")\n colstr.each_with_index do |ch, index|\n col += (alph.index(ch) + 1) * (26 ** (colstr.length - index - 1))\n end\n row = l.scan(/[0-9]+/).first\n puts \"R#{row}C#{col}\"\n else\n row = l.scan(/R([0-9]+)/).flatten.first\n col = l.scan(/C([0-9]+)/).flatten.first.to_i\n cols = \"\"\n remainder = 1\n loop do\n minus = remainder == 0 ? 2 : 1\n remainder = (col % 26)\n cols = alph[remainder - minus] + cols\n col = col / 26\n break if col == 0\n end\n puts \"#{cols}#{row}\"\n end\nend\n"}, {"source_code": "\t#recibiendo el numero de iteraciones\n\tinput=gets.chomp\n\tn=input.to_i\n\t#metodo para saber si la cadena es excel o random\n\tdef validar_excel(a)\n\t\tif a.include?(\"R\") && a.include?(\"C\") #como el metodo random tiene R&C \n\t\t\tir=a.index(\"R\") #queremos el index de cada letra\n\t\t\tic=a.index(\"C\") #porque para q sea random, la diferencia debe ser 1\n\t\t\treturn (ic-ir)==1? true : false \n\t\telse\n\t\t\treturn true\n\t\tend\n\tend\n\t#revisar\n\tdef convertir(ar)\n\t\tn=ar.size-1\t\n\t\ti=0\n\t\tnum=0\n\t\t\n\t\tloop do\n\t\t\tif n<0\n\t\t\t\tbreak\n\t\t\tend\n\t\t\t\n\t\t\tnum=num+(ar[i].ord-64)*(26**n)\n\t\t\tn-=1\n\t\t\ti+=1\n\t\tend\n\t\t\n\t\treturn num\n\tend\n\t\n\t#primer metodo de BC23 a R23C55\n\tdef excel_to_random(arr)\n\t\tparte_num=\"\"\n\t\tparte_str=\"\"\n\t\t\n\t\t0.upto(arr.size-1) do |k|\n\t\t\tif (arr[k].ord<=57 && arr[k].ord>=48) #ASCII para diferenciar num & str\n\t\t\t\tparte_num=parte_num+arr[k]\n\t\t\telse\n\t\t\t\tparte_str=parte_str+arr[k]\n\t\t\tend\n\t\tend\n\t\t\n\t\tparte_c=convertir(parte_str)\n\t\trandom=\"R\"+\"#{parte_num}\"+\"C\"+\"#{parte_c}\" #creando el random\n\t\t\n\t\treturn random\n\tend\n\t\n\t#metodo divisiones sucesivas?\n\tdef divsuc(num)\n\t\tnum_car=26\n\t\tpivot=num\n\t\tndig=0\n\t\tarreglo_num=[]\n\t\tpotencia=1\n\t\t\n\t\tloop do\n\t\t\tpivot=pivot-26**potencia\n\t\t\tndig+=1\n\t\t\tpotencia+=1\n\t\t\tif pivot<=0\n\t\t\t\tbreak \n\t\t\tend\n\t\tend\n\t\t\n\t\t#real deal\n\t\tloop do\n\t\t\tif num==0\n\t\t\t\tbreak\n\t\t\tend\n\t\t\tif num%num_car !=0\n\t\t\t\tarreglo_num[ndig-1]=((num%num_car)+64).chr()\n\t\t\t\tnum=num/num_car\n\t\t\telse #COMPORTAMIENTO DIFERENTE CUANDO EL num ES MULTIPLE DE 26 !! (2 days!)\n\t\t\t\tq=num/num_car\n\t\t\t\tnum=q-1\n\t\t\t\tarreglo_num[ndig-1]=(num_car+64).chr()\n\t\t\tend\n\t\t\tndig-=1\n\t\tend\n\t\treturn arreglo_num\n\tend\n\t\n\t#segundo metodo de R23C55 a BC23\n\tdef random_to_excel(arr)\n\t\trow=\"\"\n\t\tcol=\"\"\n\t\ta=0\n\t\t\n\t\t1.upto(arr.size-1) do |k|\n\t\t\t(arr[k]==\"C\")? a+=1:a+=0\n\n\t\t\tif a==0\n\t\t\t\trow=row+arr[k]\n\t\t\telse\n\t\t\t\tif (arr[k]!=\"C\") \n\t\t\t\t\tcol=col+arr[k]\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\t\tcol=col.to_i\n\t\tcole=divsuc(col)#convertir la columna a excel\n\t\texcel=cole.join+row\n\t\t\n\t\treturn excel\n\tend\n\t\n\t$res=[] #definición de la variable GLOBAL, para poder imprimirlo después\n\t\n\t0.upto(n-1) do |j|\n\t\tcadena=gets.chomp.upcase\n\t\tarray=cadena.split(\"\")\n\t\ti=validar_excel(array)\n\t\t\n\t\tif i\n\t\t\tresultado=excel_to_random(array)\n\t\telse\n\t\t\tresultado=random_to_excel(array)\n\t\tend\n\t\t\t\n\t\t$res[j]=resultado \t#recordar que el puts imprime con /n y el print no lo hace\n\tend\n\t\n\t#IMPRIMIR RESULTADO\n\tputs $res"}, {"source_code": "puts $<.read.split.drop(1).map { |x|\n if x[/(.)(\\d+)(.)(\\d+)/]\n (\"A\"..\"Z\").to_a.join[$4.to_i.div 26] + (\"A\"..\"Z\").to_a.join[$4.to_i % 26] + $2\n else\n t = x[/\\D+/][-2]\n \"R#{x[/\\d+/]}C#{(t ? t.ord-\"A\".ord+1 : 0)*26+x[/\\D+/][-1].ord-\"A\".ord+1}\"\n end\n}"}, {"source_code": "n = gets.to_i\n1.upto(n) do\n str = gets\n state = 0\n 0.upto(str.size-1) do |i|\n if str[i].ord>=48 and str[i].ord<=57\n state+=1 if state%2==0\n else\n state+=1 if state%2==1\n end\n end\n if state==2\n col = nil\n row = nil\n 0.upto(str.size-1) do |i|\n if str[i].ord>=48 and str[i].ord<=57\n col = str[0..i-1]\n row = str[i..-1].strip\n break\n end\n end\n coln = 0\n 0.upto(col.size-1) do |i|\n coln *= 26\n coln += col[i].ord-64\n end\n puts \"R#{row}C#{coln}\"\n else\n row = nil\n col = nil\n ci = nil\n 0.upto(str.size-1) do |i|\n if str[i].ord == 67\n ci = i\n end\n end\n row = str[1..ci-1]\n col = str[ci+1..-1].to_i\n cols = \"\"\n while col>0\n cols = (col%26+64).chr + cols\n col /= 26\n end\n puts cols+row\n end\nend\n"}, {"source_code": "n = STDIN.gets\nn.to_i.times do |i|\n a = STDIN.gets\n if a =~ /\\d+[A-Z]+/\n p \"Big format\"\n r = a.split(/[A-Z]/)[1]\n c = a.split(/[A-Z]/)[2].to_i\n completeAddress = \"\"\n while (c > 26)\n completeAddress += ((c/26.0).ceil + 63).chr\n if ((c / 26) < 26)\n c %= 26\n else\n c /= 26\n end\n \n end\n completeAddress += (c + 64).chr + r\n p completeAddress\n else\n p \"Short format\"\n c = a[0,a=~/\\d/]\n r = a.gsub(/[A-Z]+/,\"\")\n realrow = 0\n c.bytes.each do |i|\n realrow *= 26\n realrow += (i - 64)\n end\n p \"R\" + r + \"C\" + realrow.to_s\n end\nend "}, {"source_code": "class SpreadSheets\n def main\n numberOfLines = gets.to_i\n coordinates = Array.new(numberOfLines)\n\n i = 0\n while i < numberOfLines\n coordinates[i] = gets\n i += 1\n end\n\n puts convert(coordinates)\n end\n\n def convert(coordinates)\n result = Array::new\n numericDef = /R(\\d+)C(\\d+)/\n alphanumericDef = /(\\w+)(\\d+)/\n\n coordinates.each do |coordinate|\n if (numericDef.match(coordinate))\n matches = numericDef.match(coordinate)\n row = matches[1]\n column = matches[2]\n convertedColumn = convertNumToAlpha(column.to_i)\n result.push(\"#{convertedColumn}#{row}\")\n elsif (alphanumericDef.match(coordinate))\n matches = alphanumericDef.match(coordinate)\n row = matches[2]\n column = matches[1]\n convertedColumn = convertAlphaToNum(column)\n result.push(\"R#{row}C#{convertedColumn}\")\n end\n end\n\n return result\n end\n\n def convertNumToAlpha(num)\n result = String::new\n\n num -= 1;\n zeroLetter = 'A';\n base26 = 26;\n loop do\n remainder = num % base26;\n result = \"#{(zeroLetter.ord + remainder).chr}#{result}\"\n num /= base26;\n break if (num == 0)\n end\n\n return result\n end\n\n def convertAlphaToNum(alpha)\n result = 1\n\n zeroLetter = 'A'\n currentExp = 1\n base26 = 26\n alpha.split(\"\").reverse_each do |letter|\n convertedDigit = letter.ord - zeroLetter.ord\n result += convertedDigit * currentExp\n currentExp *= base26\n end\n\n return result.to_s\n end\nend\n\nSpreadSheets::new.main\n"}, {"source_code": "#=================================\n# Better be sure than sorry. \n#=================================\n$stdin = File.open(\"./input.txt\", \"r\") if ENV[\"LOGNAME\"]==\"skydos\"\n#=================================\n\ndef to_number str\n\tnum = 0\n\tdigits = [*?A..?Z]\n\t\n\tpow=1\n\tstr.reverse.each_char{|c|\t\t\n\t\td = digits.index(c) + 1\n\t\t\n\t\tnum+=d*pow\n\t\tpow*=26\n\t}\n\t\n\tnum\nend\n\ndef to_first line\n\tletters, number = line.scan(/([A-Z]+)([0-9]+)/)[0]\n\t\"R#{number}C#{to_number letters}\"\nend\n\ndef to_36 num\n\tans = \"\"\n\tdigits = [*?A..?Z]\n\t\n\tnum=num.to_i\n\twhile num!=0\n\t\tans+=digits[num%26-1]\n\t\tnum/=26\n\tend\n\t\n\tans.reverse\nend\n\ndef to_second line\n\ta, b = line.scan(/R([0-9]+)C([0-9]+)/)[0]\t\n\t\"#{to_36 b}#{a}\"\nend\n\nn=gets.to_i\n\nn.times{\n\tline=gets.strip\n\t\n\tif /R([0-9]+)C([0-9]+)/.match line\n\t\tputs to_second line\n\telse\n\t\tputs to_first line\n\tend\n}\n"}, {"source_code": "r = /R\\d+C\\d+/\nrow_regex = /\\d+/\n\nn = gets.chomp.to_i\n\nn.times do\n line = gets.chomp\n puts line.match(r)\n if line.match(r)\n row = line[1.. (line =~ /C/)].to_i\n c = line[(line =~ /C/)+1..-1].to_i\n a = 0; b = 0\n a = c\n res = \"\"\n while (a+26) > 26\n a = c / 26\n \t\t\tb = c % 26 == 0 ? 26 : c % 26\n \t\t\ta = c % 26 == 0 ? a - 1 : a\n \t\t\tc = a\n res.prepend(((b-1) + 65).chr)\n end\n puts \"#{res}#{row}\"\n else\n num_index = line =~ row_regex\n row = line[num_index..line.length()]\n col = 0\n col_str = line[0..num_index-1]\n col_str.split('').map.with_index do |chr, i|\n col += (chr.ord - 'A'.ord + 1) * (26 ** (num_index-1-i))\n end\n puts \"R#{row}C#{col}\"\n end\nend\n"}, {"source_code": "\t#recibiendo el numero de iteraciones\n\tinput=gets.chomp\n\tn=input.to_i\n\t#metodo para saber si la cadena es excel o random\n\tdef validar_excel(a)\n\t\tif a.include?(\"R\") && a.include?(\"C\") #como el metodo random tiene R&C \n\t\t\tir=a.index(\"R\") #queremos el index de cada letra\n\t\t\tic=a.index(\"C\") #porque para q sea random, la diferencia debe ser 1\n\t\t\treturn (ic-ir)==1? true : false \n\t\telse\n\t\t\treturn true\n\t\tend\n\tend\n\t#revisar\n\tdef convertir(ar)\n\t\tn=ar.size-1\t\n\t\ti=0\n\t\tnum=0\n\t\t\n\t\tloop do\n\t\t\tif n<0\n\t\t\t\tbreak\n\t\t\tend\n\t\t\t\n\t\t\tnum=num+(ar[i].ord-64)*(26**n)\n\t\t\tn-=1\n\t\t\ti+=1\n\t\tend\n\t\t\n\t\treturn num\n\tend\n\t\n\t#primer metodo de BC23 a R23C55\n\tdef excel_to_random(arr)\n\t\tparte_num=\"\"\n\t\tparte_str=\"\"\n\t\t\n\t\t0.upto(arr.size-1) do |k|\n\t\t\tif (arr[k].ord<=57 && arr[k].ord>=48) #ASCII para diferenciar num & str\n\t\t\t\tparte_num=parte_num+arr[k]\n\t\t\telse\n\t\t\t\tparte_str=parte_str+arr[k]\n\t\t\tend\n\t\tend\n\t\t\n\t\tparte_c=convertir(parte_str)\n\t\trandom=\"R\"+parte_num+\"C\"+\"#{parte_c}\" #creando el random\n\t\t\n\t\treturn random\n\tend\n\t\n\t#metodo divisiones sucesivas?\n\tdef divsuc(num)\n\t\tnum_car=26\n\t\tpivot=num\n\t\tndig=0\n\t\tarreglo_num=[]\n\t\tpotencia=1\n\t\t\n\t\tloop do\n\t\t\tpivot=pivot-26**potencia\n\t\t\tndig+=1\n\t\t\tpotencia+=1\n\t\t\tif pivot<=0\n\t\t\t\tbreak \n\t\t\tend\n\t\tend\n\t\t\n\t\t#real deal\n\t\tloop do\n\t\t\tif num==0\n\t\t\t\tbreak\n\t\t\tend\n\t\t\tif num%num_car !=0\n\t\t\t\tarreglo_num[ndig-1]=((num%num_car)+64).chr()\n\t\t\t\tnum=num/num_car\n\t\t\telse #COMPORTAMIENTO DIFERENTE CUANDO EL num ES MULTIPLE DE 26 !! (2 days!)\n\t\t\t\tq=num/num_car\n\t\t\t\tnum=q-1\n\t\t\t\tarreglo_num[ndig-1]=(num_car+64).chr()\n\t\t\tend\n\t\t\tndig-=1\n\t\tend\n\t\treturn arreglo_num\n\tend\n\t\n\t#segundo metodo de R23C55 a BC23\n\tdef random_to_excel(arr)\n\t\trow=\"\"\n\t\tcol=\"\"\n\t\ta=0\n\t\t\n\t\t1.upto(arr.size-1) do |k|\n\t\t\t(arr[k]==\"C\")? a+=1:a+=0\n\n\t\t\tif a==0\n\t\t\t\trow=row+arr[k]\n\t\t\telse\n\t\t\t\tif (arr[k]!=\"C\") \n\t\t\t\t\tcol=col+arr[k]\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\t\tcol=col.to_i\n\t\tcole=divsuc(col)#convertir la columna a excel\n\t\texcel=cole.join+row\n\t\t\n\t\treturn excel\n\tend\n\t\n\t$res=[] #definición de la variable GLOBAL, para poder imprimirlo después\n\t\n\t0.upto(n-1) do |j|\n\t\tcadena=gets.chomp.upcase\n\t\tarray=cadena.split(\"\")\n\t\ti=validar_excel(array)\n\t\t\n\t\tif i\n\t\t\tresultado=excel_to_random(array)\n\t\telse\n\t\t\tresultado=random_to_excel(array)\n\t\tend\n\t\t\t\n\t\t$res[j]=resultado \t#recordar que el puts imprime con /n y el print no lo hace\n\tend\n\t\n\t#IMPRIMIR RESULTADO\n\tputs $res"}, {"source_code": "\t#recibiendo el numero de iteraciones\n\tinput=gets.chomp\n\tn=input.to_i\n\t#metodo para saber si la cadena es excel o random\n\tdef validar_excel(a)\n\t\tif a.include?(\"R\") && a.include?(\"C\") #como el metodo random tiene R&C \n\t\t\tir=a.index(\"R\") #queremos el index de cada letra\n\t\t\tic=a.index(\"C\") #porque para q sea random, la diferencia debe ser 1\n\t\t\treturn (ic-ir)==1? true : false \n\t\telse\n\t\t\treturn true\n\t\tend\n\tend\n\t#revisar\n\tdef convertir(ar)\n\t\t\n\t\tcase ar.size\n\t\twhen 3\n\t\t\tnum=(ar[0].ord-64)*26*26 +(ar[1].ord-64)*26+(ar[2].ord-64)\n\t\twhen 2\n\t\t\tnum=(ar[0].ord-64)*26 +(ar[1].ord-64)\n\t\twhen 1\n\t\t\tnum=ar[0].ord-64\n\t\telse\n\t\t\tnum=0\n\t\tend\n\t\t\n\t\treturn num\n\tend\n\t\n\t#primer metodo de BC23 a R23C55\n\tdef excel_to_random(arr)\n\t\tparte_num=\"\"\n\t\tparte_str=\"\"\n\t\t\n\t\t0.upto(arr.size-1) do |k|\n\t\t\tif (arr[k].ord<=57 && arr[k].ord>=48) #ASCII para diferenciar num & str\n\t\t\t\tparte_num=parte_num+arr[k]\n\t\t\telse\n\t\t\t\tparte_str=parte_str+arr[k]\n\t\t\tend\n\t\tend\n\t\t\n\t\tparte_c=convertir(parte_str)\n\t\trandom=\"R\"+parte_num+\"C\"+\"#{parte_c}\" #creando el random\n\t\t\n\t\treturn random\n\tend\n\t\n\t#metodo divisiones sucesivas?\n\tdef divsuc(num)\n\t\tnum_car=26\n\t\tpivot=num\n\t\tndig=0\n\t\tarreglo_num=[]\n\t\tpotencia=1\n\t\t\n\t\tloop do\n\t\t\tpivot=pivot-26**potencia\n\t\t\tndig+=1\n\t\t\tpotencia+=1\n\t\t\tif pivot<=0\n\t\t\t\tbreak \n\t\t\tend\n\t\tend\n\t\t\n\t\t#real deal\n\t\tloop do\n\t\t\tif num==0\n\t\t\t\tbreak\n\t\t\tend\n\t\t\tif num%num_car !=0\n\t\t\t\tarreglo_num[ndig-1]=((num%num_car)+64).chr()\n\t\t\t\tnum=num/num_car\n\t\t\telse #COMPORTAMIENTO DIFERENTE CUANDO EL num ES MULTIPLE DE 26 !! (2 days!)\n\t\t\t\tq=num/num_car\n\t\t\t\tnum=q-1\n\t\t\t\tarreglo_num[ndig-1]=(num_car+64).chr()\n\t\t\tend\n\t\t\tndig-=1\n\t\tend\n\t\treturn arreglo_num\n\tend\n\t\n\t#segundo metodo de R23C55 a BC23\n\tdef random_to_excel(arr)\n\t\trow=\"\"\n\t\tcol=\"\"\n\t\ta=0\n\t\t\n\t\t1.upto(arr.size-1) do |k|\n\t\t\t(arr[k]==\"C\")? a+=1:a+=0\n\n\t\t\tif a==0\n\t\t\t\trow=row+arr[k]\n\t\t\telse\n\t\t\t\tif (arr[k]!=\"C\") \n\t\t\t\t\tcol=col+arr[k]\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\t\tcol=col.to_i\n\t\tcole=divsuc(col)#convertir la columna a excel\n\t\texcel=cole.join+row\n\t\t\n\t\treturn excel\n\tend\n\t\n\t$res=[] #definición de la variable GLOBAL, para poder imprimirlo después\n\t\n\t0.upto(n-1) do |j|\n\t\tcadena=gets.chomp.upcase\n\t\tarray=cadena.split(\"\")\n\t\ti=validar_excel(array)\n\t\t\n\t\tif i\n\t\t\tresultado=excel_to_random(array)\n\t\telse\n\t\t\tresultado=random_to_excel(array)\n\t\tend\n\t\t\t\n\t\t$res[j]=resultado \t#recordar que el puts imprime con /n y el print no lo hace\n\tend\n\t\n\t#IMPRIMIR RESULTADO\n\tputs $res"}, {"source_code": "n = gets.to_i\n1.upto(n) do\n str = gets\n state = 0\n 0.upto(str.size-1) do |i|\n if str[i].ord>=48 and str[i].ord<=57\n state+=1 if state%2==0\n else\n state+=1 if state%2==1\n end\n end\n if state==2\n col = nil\n row = nil\n 0.upto(str.size-1) do |i|\n if str[i].ord>=48 and str[i].ord<=57\n col = str[0..i-1]\n row = str[i..-1].strip\n break\n end\n end\n coln = 0\n 0.upto(col.size-1) do |i|\n coln *= 26\n coln += col[i].ord-64\n end\n puts \"R#{row}C#{coln}\"\n else\n row = nil\n col = nil\n ci = nil\n 0.upto(str.size-1) do |i|\n if str[i].ord == 67\n ci = i\n end\n end\n row = str[1..ci-1]\n col = str[ci+1..-1].to_i\n cols = \"\"\n while col>0\n cols = (col%26+64).chr + cols\n col /= 26\n end\n puts cols+row\n end\nend\n"}, {"source_code": "number_of_input=gets.to_i\ninput_arry=[]\ni_a=0\n\n\nloop do\n break if i_a==number_of_input\n input_arry[i_a]=gets.chomp\n i_a +=1\nend\n\nvalue=(1..26)\nalph=('A'..'Z')\nvalue=[*value]\nalph=[*alph]\n@hash={}\ni_b=0\n\nloop do\n break if i_b==26\n @hash[alph[i_b]]=value[i_b]\n i_b+=1\nend \n\nvalue=(1..26)\nalph=('A'..'Z')\nvalue=[*value]\nalph=[*alph]\n@hash2={}\ni_b=0\n\nloop do\n break if i_b==26\n @hash2[alph[i_b]]=value[i_b]\n i_b+=1\nend \n@hash2['Z']=0\n\n\n\n\ni_c =0\n\nloop do\n break if i_c==input_arry.length\n input1=input_arry[i_c]\n \n if input1[0]!='R' || (input1[0]=='R' && input1[1].to_i==0) \n a1=input1\n r=a1[0]\n i=1\n \n loop do\n break if a1[i].to_i !=0\n r =r + a1[i]\n i+=1\n end\n\n r1=r.length\n r2=a1.length\n c1= a1[0 .. (r1-1)].reverse\n row= a1[ r1 .. r2].to_i\n c=0\n i=0\n \n loop do\n break if i==c1.length\n c= c + (@hash[c1[i]]*(26**i))\n i+=1\n end\n \n puts \"R#{row}C#{c}\"\n \n elsif input1[0]=='R' && input1[1].to_i!=0\n k2=input1.split(\"\")\n \n if k2.index(\"C\")>1\n a1=input1\n r=a1[0]\n i=1\n \n loop do\n break if a1[i].to_i !=0\n r =r + a1[i]\n i+=1\n end\n\n r1=r.length\n r2=a1.length\n c1= a1[0 .. (r1-1)].reverse\n row= a1[ r1 .. r2].to_i\n c=0\n i=0\n \n loop do\n break if i==c1.length\n c= c + (@hash[c1[i]]*(26**i))\n i+=1\n end\n \n puts \"R#{row}C#{c}\"\n else\n a1=input1\n s2=a1.split(\"\").map{|s| s}\n r3=3\n s2.find {|i| r3=s2.index(i)+1 if i=='C'}\n i=s2[r3 .. (s2.length - 1)].join.to_i\n row=s2[1 .. (r3-2)].join.to_i\n str_c=\"\"\n loop do\n str_c = str_c + @hash2.key(i%26)\n i=i/26\n break if i==0\n end\n \n str_c=str_c.reverse\n puts \"#{str_c}#{row}\"\n end \n \n end \ni_c +=1\nend\n "}, {"source_code": "a=[*?A..'BEAAA'];gets.to_i.times{x=gets;puts /R(\\d+)C(\\d+)/.match(x)?(a[$2.to_i-1]+$1):(y=v=0;/(\\D+)(\\d+)/.match x;$1.reverse.each_char{|c|y+=(c.ord-64)*v**26;v+=1};\"R#{$2}C#{y}\")}"}, {"source_code": "arr = STDIN.read.split(\"\\n\")\nn = arr[0].to_i\n\n1.upto(n).each do |i|\n str = arr[i]\n match = str.match(/R([0-9]+)C([0-9]+)/)\n\n if match\n row = match[2].to_i\n col = match[1]\n\n r = row\n str = \"\"\n while(r > 0) do\n val = (r-1) % 26\n str += (val + 65).chr\n r /= 26\n end\n str = str.reverse\n str += col\n puts str\n else\n match = str.match(/([A-Z]+)([0-9]+)/)\n col = match[1]\n row = match[2]\n\n puts \"A\"\n s = 0\n rev = col.reverse.split(\"\")\n puts \"B\"\n rev.each_index do |i|\n puts \"rev[i]\" + rev[i]\n puts rev[i].ord - 65 + 1\n chr = (rev[i].ord - 65 + 1) * ( 26 ** i )\n puts \"chr:#{chr}\"\n s += chr\n end\n\n puts \"R#{row}C#{s}\"\n end\nend"}, {"source_code": "\ndef a_to_n(line)\n length = line.length\n i = 0\n sum = 0\n while i < length\n sum = sum * 26 + (line[i].ord - 'A'.ord + 1) \n i = i + 1\n end\n return sum\nend\n\ndef n_to_a(line)\n\n row_str = \"\"\n row_num = line.to_i\n while row_num > 0\n row_str.prepend( (64 + row_num % 26).chr) \n row_num = row_num/26\n end\n return row_str\n\nend\n\ndef xy_to_rc(line)\n col_size = line =~ /[0-9]/\n\n col_str = line[0...col_size]\n row_str = line[col_size...-1]\n\n col = a_to_n(col_str)\n row = row_str.to_i\n\n print(\"R\")\n print(row)\n print(\"C\")\n print(col)\n\nend\n\ndef rc_to_xy(line)\n col_size = line =~ /[C]/\n\n row_str = line[1...col_size]\n col_str = line[col_size+1...-1]\n\n col = n_to_a(col_str)\n row = row_str.to_i\n\n print(col)\n print(row)\nend\n\ndef isrc(line)\n if line[0] == 'R' && line[1] =~ /[0-9]/\n if line[2...-1].include? \"C\"\n return true\n end\n end\n return false\nend\n\n\nline_count = gets.to_i\n\nwhile line_count > 0 \n line_string = gets\n if isrc(line_string)\n rc_to_xy(line_string)\n else\n xy_to_rc(line_string)\n end\n print(\"\\n\")\n line_count = line_count-1\nend\n"}, {"source_code": "#=================================\n# Better be sure than sorry. \n#=================================\n$stdin = File.open(\"./input.txt\", \"r\") if ENV[\"LOGNAME\"]==\"skydos\"\n#=================================\n\ndef to_number str\n\tnum = 0\n\tdigits = [*?A..?Z]\n\t\n\tpow=1\n\tstr.reverse.each_char{|c|\t\t\n\t\td = digits.index(c) + 1\n\t\t\n\t\tnum+=d*pow\n\t\tpow*=26\n\t}\n\t\n\tnum\nend\n\ndef to_first line\n\tletters, number = line.scan(/([A-Z]+)([0-9]+)/)[0]\n\t\"R#{number}C#{to_number letters}\"\nend\n\ndef to_36 num\n\tans = \"\"\n\tdigits = [*?A..?Z]\n\t\n\tnum=num.to_i\n\twhile num!=0\n\t\tans+=digits[num%26-1]\n\t\tnum/=26\n\tend\n\t\n\tans.reverse\nend\n\ndef to_second line\n\ta, b = line.scan(/R([0-9]+)C([0-9]+)/)[0]\t\n\t\"#{to_36 b}#{a}\"\nend\n\nn=gets.to_i\n\nn.times{\n\tline=gets.strip\n\t\n\tif /R([0-9]+)C([0-9]+)/.match line\n\t\tputs to_second line\n\telse\n\t\tputs to_first line\n\tend\n}\n"}, {"source_code": "#=================================\n# Better be sure than sorry. \n#=================================\n$stdin = File.open(\"./input.txt\", \"r\") if ENV[\"LOGNAME\"]==\"skydos\"\n#=================================\n\ndef to_letters num\n\tans=\"\"\n\tdigs=[*?A..?Z]\n\t\n\twhile num!=0\n\t\tans+=digs[num%26 - 1]\n\t\tnum/=26\n\tend\n\t\n\tans.reverse\nend\n\ndef to_numbers str\n\tans=0\n\tstr.reverse!\n\t\n\tpow=0\n\tstr.each_char{|c|\n\t\tans+= (c.ord - 'A'.ord + 1) * 26**pow\n\t\tpow+=1\n\t}\n\t\n\tans\nend\n\nn=gets.to_i\n\nn.times{\n\tline=gets.strip\n\t\n\tif /R(\\d+)C(\\d+)/.match line\n\t\tputs \"#{to_letters $2.to_i}#{$1}\"\n\telse\n\t\tp1, p2 = line.scan(/([A-Z]+)([0-9]+)/)[0]\n\t\tputs \"R#{p2}C#{to_numbers p1}\"\n\tend\n}\n"}, {"source_code": "num = gets.chomp.to_i\nnum.times do\n old = gets.chomp\n results = old.match('R(\\d+)C(\\d+)')\n if results != nil\n col_num = results[2].to_i\n base = 1;\n col_str = \"\"\n while col_num > 26\n this_char = col_num % 26\n col_num /= 26\n col_str = \"#{(this_char+64).chr}#{col_str}\"\n end\n col_str = \"#{(col_num+64).chr}#{col_str}\"\n puts \"#{col_str}#{results[1]}\"\n else\n results = old.match('([A-Z]+)(\\d+)')\n col_str = results[1]\n base = 26**(col_str.length-1)\n col_num = 0\n col_str.each_char do |c| \n col_num += (c.ord-64)*base\n base /= 26\n end\n puts \"R#{results[2]}C#{col_num}\"\n end\nend"}, {"source_code": "$letters = [\"\"].concat((\"A\"..\"Z\").to_a)\n\ndef rc_ab(rc)\n rc_a = rc.scan(/\\d+/)\n rc_a[1] = rc_a[1].to_i\n s = \"\"\n \n while(rc_a[1] != 0) do\n s << $letters[rc_a[1] % 26]\n rc_a[1] /= 26\n end\n \n s = s.reverse << rc_a[0]\n return s\nend \n\ndef ab_rc(ab)\n ab_c = /[A-Z]+/.match(ab)\n \n ab_s = ab_c.to_s.reverse\n ab_n = 0\n \n ab_s.length.times {\n |i|\n ab_n += $letters.index(ab_s[i,1]) * 26 ** i\n }\n \n s = \"R\" << ab_n.to_s << \"C\" << /\\d+/.match(ab).to_s\n return s\nend\n\nn = gets().chomp.to_i\na = []\nn.times {\n|i|\n a[i] = gets().chomp()\n}\na.each{\n|i|\n if (/^R\\d+C\\d+$/ =~ i) then\n #RXCY \n puts(rc_ab(i))\n else\n #AB23\n puts(ab_rc(i))\n end\n}\n"}, {"source_code": "n = gets.to_i\n1.upto(n) do\n str = gets\n state = 0\n 0.upto(str.size-1) do |i|\n if str[i].ord>=48 and str[i].ord<=57\n state+=1 if state%2==0\n else\n state+=1 if state%2==1\n end\n end\n if state==2\n col = nil\n row = nil\n 0.upto(str.size-1) do |i|\n if str[i].ord>=48 and str[i].ord<=57\n col = str[0..i-1]\n row = str[i..-1].strip\n break\n end\n end\n coln = 0\n 0.upto(col.size-1) do |i|\n coln *= 26\n coln += col[i].ord-64\n end\n puts \"R#{row}C#{coln}\"\n else\n row = nil\n col = nil\n ci = nil\n 0.upto(str.size-1) do |i|\n if str[i].ord == 67\n ci = i\n end\n end\n row = str[1..ci-1]\n col = str[ci+1..-1].to_i\n cols = \"\"\n while col>0\n cols = (col%26+64).chr + cols\n col /= 26\n end\n puts cols+row\n end\nend\n"}, {"source_code": "def excel_to_index(number)\n row = number.gsub(/[a-zA-Z]/, '').chomp\n column = number.gsub(/\\d/, '').chomp\n\n length = column.length\n column_index = 0\n column.each_char do |c|\n column_index += (c.ord - 'A'.ord + 1) * (26 ** (length - 1))\n length -= 1\n end\n\n 'R' + row + 'C' + column_index.to_s\nend\n\ndef index_to_excel(number)\n row, column = number.sub(/R/, '').split(/C/).map(&:to_i)\n\n column_index = column.to_s(26).upcase\n column_letters = ''\n column_index.each_char do |c|\n if c.ord > '9'.ord\n column_letters += (c.ord + 9).chr\n else\n if '0' == c\n let = column_letters[column_letters.length - 1]\n column_letters[column_letters.length - 1] = (let.ord - 1).chr\n column_letters += 'Z'\n else\n column_letters += (c.ord + 'A'.ord - '1'.ord).chr\n end\n end \n end\n\n column_letters + row.to_s\nend \n\ncount = gets.to_i\n\ncount.times do\n num = gets.upcase\n if num[/R\\d+C\\d+/]\n puts index_to_excel num\n else\n puts excel_to_index num\n end \nend"}, {"source_code": "n = gets\nn = Integer(n)\nwhile n>0\n string = gets\n m = string.size()\n index = 0\n while ('A'..'Z').include? string[index]\n index += 1\n end\n while !(('A'..'Z').include? string[index])\n index += 1\n if index >= m\n break;\n end\n end\n if index == m\n index = 0\n while ('A'..'Z').include? string[index]\n index+=1\n end\n index -= 1\n c = 0\n count = 1\n til = index\n until index < 0\n c += (string[index].unpack(\"c\")[0] - 'A'.unpack(\"c\")[0] + 1) * count\n index -= 1\n count *= 26\n end\n puts \"R#{string[til+1..m].chomp}C#{c}\"\n else\n index = string.index(\"C\")\n count = Integer(string[index+1..m-2].chomp)\n k = \"\"\n while count > 0\n k = (count % 26 + 64).chr + k\n count /= 26\n end\n puts \"#{k}#{string[1..index-1]}\"\n end\n n-=1\nend"}, {"source_code": "def excel_to_index(number)\n row = number.gsub(/[a-zA-Z]/, '').chomp\n column = number.gsub(/\\d/, '').chomp\n\n length = column.length\n column_index = 0\n column.each_char do |c|\n column_index += (c.ord - 'A'.ord + 1) * (26 ** (length - 1))\n length -= 1\n end\n\n 'R' + row + 'C' + column_index.to_s\nend\n\ndef index_to_excel(number)\n row, column = number.sub(/R/, '').split(/C/).map(&:to_i)\n\n column_letters = ''\n while (column / 26) > 0\n column_letters += (\"A\".ord - 1 + column / 26).chr\n column -= 26 * (column / 26)\n end\n column_letters += (\"A\".ord - 1 + column).chr\n\n column_letters + row.to_s\nend \n\ncount = gets.to_i\n\ncount.times do\n num = gets.upcase\n if num[/R\\d+C\\d+/]\n puts index_to_excel num\n else\n puts excel_to_index num\n end \nend"}, {"source_code": "#=================================\n# Better be sure than sorry. \n#=================================\n$stdin = File.open(\"./input.txt\", \"r\") if ENV[\"LOGNAME\"]==\"skydos\"\n#=================================\n\ndef to_number str\n\tnum = 0\n\tdigits = [*?A..?Z]\n\t\n\tpow = 0\n\tstr.reverse.each_char{|c|\t\t\n\t\td = digits.index(c)+1\n\t\t\n\t\tnum+=d * 26**pow\n\t\tpow+=1\n\t}\n\t\n\tnum\nend\n\ndef to_first line\n\tletters, number = line.scan(/([A-Z]+)([0-9]+)/)[0]\n\t\"R#{number}C#{to_number letters}\"\nend\n\ndef to_27 num\n\tans = \"\"\n\tdigits = [*?A..?Z]\n\t\n\tnum = num.to_i\n\twhile num!=0\n\t\tans = digits[num%27] + ans\n\t\tnum/=27\n\tend\n\t\n\tans.reverse\nend\n\ndef to_second line\n\ta, b = line.scan(/R([0-9]+)C([0-9]+)/)[0]\t\n\t\"#{to_27 b}#{a}\"\nend\n\nn=gets.to_i\n\nn.times{\n\tline=gets.strip\n\t\n\tif /R([0-9]+)C([0-9]+)/.match line\n\t\tputs to_second line\n\telse\n\t\tputs to_first line\n\tend\n}\n"}, {"source_code": "# # # # # # # # # # # # # # # # # # # # # # # # # # # #\ndef to_1var(row, column)\n\n az = ('A'..'Z').to_a\n azLen = az.length()\n\n columnAZ = \"\"\n columnInt = column.to_i\n\n while columnInt > 0\n columnAZ.insert(0, az[(columnInt % azLen)-1])\n columnInt /= azLen;\n end\n\n return columnAZ + row\n\nend\n\n# # # # # # # # # # # # # # # # # # # # # # # # # # # #\ndef to_2var(row, column)\n\n azLen = ('A'..'Z').to_a.length\n\n columnInt = 0\n column.reverse.each_char.each_with_index { |chr, idx| \n columnInt += (chr.ord - 'A'.ord + 1) * azLen**idx\n }\n\n return \"R#{row}C#{columnInt}\"\n\nend\n\n# # # # # # # # # # # # # # # # # # # # # # # # # # # #\n# Main\n# # # # # # # # # # # # # # # # # # # # # # # # # # # #\n\ngets.to_i.times {\n\n coord = gets.strip\n\n if coord =~ /^R(\\d+)C(\\d+)$/\n puts to_1var($1, $2)\n elsif coord =~ /^([A-Z]+)(\\d+)/\n puts to_2var($2, $1)\n end\n\n}\n\n\n"}, {"source_code": "n=gets.chomp.to_i\ndata=[]\n\nalpha=['A', 'B', 'C', 'D', 'E',\n\t\t'F', 'G', 'H', 'I', 'J',\n\t\t'K', 'L', 'M', 'N', 'O',\n\t\t'P', 'Q', 'R', 'S', 'T',\n\t\t'U', 'V', 'W', 'X', 'Y', 'Z']\na=0\n\nwhile a0\n\t\t\t\tb=cells%osn\n\t\t\t\tcs.push(alpha[b-1])\n\t\t\t\tcells-=b\n\t\t\t\tcells/=osn\n\t\t\tend\n\t\t\t\n\t\t\tcs.reverse!\n\t\t\tcs.push(rows)\n\n\t\t\tputs cs.join\n\t\telse\n\t\t\tsn=[]\n\t\t\ta=0\n\t\t\twhile word[a].to_i==0\n\t\t\t\tsn.push(word[a])\n\t\t\t\ta+=1\n\t\t\tend\n\n\t\t\tnumber=[]\n\t\t\tfor i in(a..word.length-1)\n\t\t\t\tnumber.push(word[i])\n\t\t\tend\n\t\t\tnumb=number.join.to_i\n\n\t\t\tsn.reverse!\n\t\t\trw=0\n\t\t\tfor i in(0..sn.length-1)\n\t\t\t\trw+=(alpha.index(sn[i])+1)*osn**i\n\t\t\tend\n\n\t\t\tanswer=[]\n\t\t\tanswer.push('R')\n\t\t\tanswer.push(numb)\n\t\t\tanswer.push('C')\n\t\t\tanswer.push(rw)\n\t\t\tputs answer.join\n\t\tend\n\telse\n\t\tsn=[]\n\t\ta=0\n\t\twhile word[a].to_i==0\n\t\t\tsn.push(word[a])\n\t\t\ta+=1\n\t\tend\n\n\t\tnumber=[]\n\t\tfor i in(a..word.length-1)\n\t\t\tnumber.push(word[i])\n\t\tend\n\t\tnumb=number.join.to_i\n\n\t\tsn.reverse!\n\t\trw=0\n\t\tfor i in(0..sn.length-1)\n\t\t\trw+=(alpha.index(sn[i])+1)*osn**i\n\t\tend\n\n\t\tanswer=[]\n\t\tanswer.push('R')\n\t\tanswer.push(numb)\n\t\tanswer.push('C')\n\t\tanswer.push(rw)\n\t\tputs answer.join\n\tend\nend"}, {"source_code": "n = gets\nn = Integer(n)\nwhile n>0\n string = gets\n m = string.size()\n index = 0\n while ('A'..'Z').include? string[index]\n index += 1\n end\n while !(('A'..'Z').include? string[index])\n index += 1\n if index >= m\n break;\n end\n end\n if index == m\n index = 0\n while ('A'..'Z').include? string[index]\n index+=1\n end\n index -= 1\n c = 0\n count = 1\n til = index\n until index < 0\n c += (string[index].unpack(\"c\")[0] - 'A'.unpack(\"c\")[0] + 1) * count\n index -= 1\n count *= 26\n end\n puts \"R#{string[til+1..m].chomp}C#{c}\"\n else\n index = string.index(\"C\")\n count = Integer(string[index+1..m-2].chomp)\n k = \"\"\n while count > 0\n if count % 26 == 0\n k = \"Z\" + k\n count /= 26\n count -= 1\n end\n if count>0\n k = (count % 26 + 64).chr + k\n count /= 26 \n end\n end\n puts \"#{k}#{string[1..index-1]}\"\n end\n n-=1\nend"}, {"source_code": "puts $<.read.split.drop(1).map { |x|\n if x[/(.)(\\d+)(.)(\\d+)/]\n (\"A\"..\"Z\").to_a.join[$4.to_i.div 26] + (\"A\"..\"Z\").to_a.join[$4.to_i % 26] + $2\n else\n t = x[/\\D+/][-2]\n \"R#{x[/\\d+/]}C#{(t ? t.ord-\"A\".ord+1 : 0)*26+x[/\\D+/][-1].ord-\"A\".ord+1}\"\n end\n}"}, {"source_code": "num = gets.chomp.to_i\nputs num\nnum.times do\n old = gets.chomp\n results = old.match('R(\\d+)C(\\d+)')\n if results != nil\n col_num = results[2].to_i\n base = 1;\n col_str = \"\"\n while col_num > 26\n this_char = col_num / 26\n col_num %= 26\n col_str += (this_char+64).chr\n end\n col_str += (col_num+65).chr\n puts \"#{col_str}#{results[1]}\"\n else\n results = old.match('([A-Z]+)(\\d+)')\n col_str = results[1]\n base = 26**(col_str.length-1)\n col_num = 0\n col_str.each_char do |c| \n col_num += (c.ord-64)*base\n base /= 26\n end\n puts \"R#{results[2]}C#{col_num}\"\n end\nend"}, {"source_code": "puts $<.read.split.drop(1).map { |x|\n if x[/R(\\d+)C(\\d+)/]\n \"#{(\"A\"..\"Z\").to_a.join[$2.to_i.div(26)-27]}#{(\"A\"..\"Z\").to_a.join[$2.to_i%26 - 1]}\" + $1\n else\n t = x[/\\D+/][-2]\n \"R#{x[/\\d+/]}C#{(t ? t.ord-\"A\".ord+1 : 0)*26+x[/\\D+/][-1].ord-\"A\".ord+1}\"\n end\n}"}, {"source_code": "n = gets.to_i\nfor x in gets(\"\").split.take(n)\n if x[/R(\\d+)C(\\d+)/]\n a = $2.to_i - 1\n t = (\"A\"..\"Z\").to_a.join[a%26]\n a = a.div 26\n until a == 0\n b,c = (a-1).divmod(26)\n t = (\"A\"..\"Z\").to_a.join[c] + t\n a = b\n end\n puts t + $1\n else\n t = x[/\\D+/][-2]\n puts \"R#{x[/\\d+/]}C#{(t ? t.ord-\"A\".ord+1 : 0)*26+x[/\\D+/][-1].ord-\"A\".ord+1}\"\n end\nend\n"}, {"source_code": "arr = STDIN.read.split(\"\\n\")\nn = arr[0].to_i\n\n1.upto(n).each do |i|\n str = arr[i]\n match = str.match(/R([0-9]+)C([0-9]+)/)\n\n if match\n row = match[2].to_i\n col = match[1]\n\n r = row\n str = \"\"\n while(r > 0) do\n val = (r-1) % 26\n str += (val + 65).chr\n r /= 26\n end\n str = str.reverse\n str += col\n puts str\n else\n match = str.match(/([A-Z]+)([0-9]+)/)\n col = match[1]\n row = match[2]\n\n s = 0\n col.reverse.split(\"\").each_index do |i|\n chr = (col[i].ord - 65) * (26**i) + 1\n s += chr\n end\n puts \"R#{row}C#{s}\"\n end\nend\n"}, {"source_code": "$letters = [\"\"].concat((\"A\"..\"Z\").to_a)\n\ndef rc_ab(rc)\n rc_a = rc.scan(/\\d+/)\n rc_a[1] = rc_a[1].to_i\n s = \"\"\n \n while(rc_a[1] != 0) do\n s << $letters[rc_a[1] % 26]\n rc_a[1] /= 26\n end\n \n s = s.reverse << rc_a[0]\n return s\nend \n\ndef ab_rc(ab)\n ab_c = /[A-Z]+/.match(ab)\n \n ab_s = ab_c.to_s.reverse\n ab_n = 0\n \n ab_s.length.times {\n |i|\n ab_n += $letters.index(ab_s[i,1]) * 26 ** i\n }\n \n s = \"R\" << /\\d+/.match(ab).to_s << \"C\" << ab_n.to_s \n return s\nend\n\nn = gets().chomp.to_i\na = []\nn.times {\n|i|\n a[i] = gets().chomp()\n}\na.each{\n|i|\n if (/^R\\d+C\\d+$/ =~ i) then\n #RXCY \n puts(rc_ab(i))\n else\n #AB23\n puts(ab_rc(i))\n end\n}\n"}, {"source_code": "n=gets.chomp.to_i\nn.times {\n\ts=gets.chomp.to_s\n\ts.scan(/^R([0-9]+)C([0-9]+)$/)\n\tunless !$~\n\t\tr=$~[1].to_i\n\t\tc=$~[2].to_i\n\t\tremainder=c%26\n\t\tg=(remainder+64).chr\n\t\twhile remainder !=c\n\t\t\tc-=remainder\n\t\t\tc/=26\n\t\t\tremainder=c%26\n\t\t\tg+=(remainder+64).chr\n\t\tend\n\t\tprint(g.reverse)\n\t\tputs (r)\n\t\tnext\n\tend\n\ts.scan(/^([A-Z]+)([0-9]+)$/)\n\tunless !$~\n\t\tc=$~[1]\n\t\tr=$~[2].to_i\n\t\tk=1\n\t\tsum=0\n\t\tc=c.reverse\n\t\tc=c.split(\"\")\n\t\tfor i in c\n\t\t\t#puts (i)\n\t\t\thold=(i.getbyte(0)-64)*k\n\t\t\t#puts (\"hold = #{hold}\")\n\t\t\tsum+=hold\n\t\t\tk*=26\n\t\tend\n\t\tputs (\"R#{r}C#{sum}\")\n\tend\n\n}"}, {"source_code": "class Integer\n private\n LETTERS = [\"Z\"].concat((\"A\"..\"Y\").to_a)\n \n public\n def to_ab\n number = self\n ab = \"\"\n while(number != 0) do\n ab << LETTERS[number % 26]\n number /= 26\n end\n return ab.reverse\n end\nend\n\nclass String\n private\n LETTERS = (\"A\"..\"Z\").to_a\n \n public\n def to_rc\n rstring = self.clone.reverse!\n rc = 0\n rstring.length.times {\n |i|\n rc += ((LETTERS.index(rstring[i,1]) + 1) * (26 ** i))\n }\n return rc.to_s\n end\nend\n\ndef rc_ab(rc)\n row, column = rc.scan(/\\d+/)\n column = column.to_i.to_ab\n ab = column << row\n return ab\nend\n\ndef ab_rc(ab)\n column, row = ab.scan(/([A-Z]+)(\\d+)/)[0]\n column = column.to_rc\n rc = \"R\" << row << \"C\" << column\n return rc\nend\n\nn = gets().chomp.to_i\na = []\nn.times {\n|i|\n a[i] = gets().chomp()\n}\na.each{\n|i|\n if (/^R\\d+C\\d+$/ =~ i) then\n #RXCY \n puts(rc_ab(i))\n else\n #AB23\n puts(ab_rc(i))\n end\n}"}, {"source_code": "#=================================\n# Better be sure than sorry. \n#=================================\n$stdin = File.open(\"./input.txt\", \"r\") if ENV[\"LOGNAME\"]==\"skydos\"\n#=================================\n\ndef to_number str\n\tnum = 0\n\tdigits = [*?A..?Z]\n\t\n\tpow=1\n\tstr.reverse.each_char{|c|\t\t\n\t\td = digits.index(c) + 1\n\t\t\n\t\tnum+=d*pow\n\t\tpow*=26\n\t}\n\t\n\tnum\nend\n\ndef to_first line\n\tletters, number = line.scan(/([A-Z]+)([0-9]+)/)[0]\n\t\"R#{number}C#{to_number letters}\"\nend\n\ndef to_27 num\n\tans = \"\"\n\tdigits = [*?A..?Z]\n\t\n\tnum = num.to_i\n\twhile num!=0\n\t\tans+=digits[num%27]\n\t\tnum/=27\n\tend\n\t\n\tans\nend\n\ndef to_second line\n\ta, b = line.scan(/R([0-9]+)C([0-9]+)/)[0]\t\n\t\"#{to_27 b}#{a}\"\nend\n\nn=gets.to_i\n\nn.times{\n\tline=gets.strip\n\t\n\tif /R([0-9]+)C([0-9]+)/.match line\n\t\tputs to_second line\n\telse\n\t\tputs to_first line\n\tend\n}\n"}, {"source_code": "#!/usr/bin/env ruby\n\ndef i2c\nend\n\ndef c2i\nend\n\ndef convert(str)\n if str =~ /^R(\\d+)C(\\d+)$/\n rows, cols = $1.to_i, $2.to_i\n a = []\n loop do\n a.unshift cols % 26\n cols /= 26\n break if cols == 0\n end\n \"#{a.map{|i| (i + 64).chr}.join}#{rows}\"\n elsif str =~ /^([A-Z]+)(\\d+)$/\n cols = 0\n $1.size.times do |i|\n cols += ($1.unpack('C*')[-i - 1] - 64) * (26 ** i)\n end\n \"R#{$2}C#{cols}\"\n else\n \"invalid arg\"\n end\nend\n\nn = gets.to_i\nlines = []\nn.times do\n lines << convert(gets.chomp)\nend\n\nlines.each{|s| puts s}\n"}, {"source_code": "#!/usr/bin/env ruby\n\ndef i2c\nend\n\ndef c2i\nend\n\ndef convert(str)\n if str =~ /^R(\\d+)C(\\d+)$/\n rows, cols = $1.to_i, $2.to_i\n a = []\n loop do\n a.unshift cols % 26\n cols /= 26\n break if cols == 0\n end\n \"#{a.map{|i| (i + 64).chr}.join}#{rows}\"\n elsif str =~ /^([A-Z]+)(\\d+)$/\n cols = 0\n $1.size.times do |i|\n cols += ($1.unpack('C*')[-i - 1] - 64) * (26 ** i)\n end\n \"R#{$2}C#{cols}\"\n else\n \"invalid arg\"\n end\nend\n\nn = gets.to_i\nlines = []\nn.times do\n lines << convert(gets.chomp)\nend\n\nlines.each{|s| puts s}\n"}, {"source_code": "r = /R\\d+C\\d+/\nrow_regex = /\\d+/\n\nn = gets.chomp.to_i\n\nn.times do\n line = gets.chomp\n puts line.match(r)\n if line.match(r)\n row = line[1.. (line =~ /C/)].to_i\n c = line[(line =~ /C/)+1..-1].to_i\n a = 0; b = 0\n a = c\n res = \"\"\n while (a+26) > 26\n a = c / 26\n \t\t\tb = c % 26 == 0 ? 26 : c % 26\n \t\t\ta = c % 26 == 0 ? a - 1 : a\n \t\t\tc = a\n res.prepend(((b-1) + 65).chr)\n end\n puts \"#{res}#{row}\"\n else\n num_index = line =~ row_regex\n row = line[num_index..line.length()]\n col = 0\n col_str = line[0..num_index-1]\n col_str.split('').map.with_index do |chr, i|\n col += (chr.ord - 'A'.ord + 1) * (26 ** (num_index-1-i))\n end\n puts \"R#{row}C#{col}\"\n end\nend\n"}, {"source_code": "$table = [\"A\", \"B\", \"C\", \"D\", \"E\", \"F\", \"G\", \"H\", \"I\", \"J\", \"K\", \"L\", \"M\", \"N\", \"O\", \"P\", \"Q\", \"R\", \"S\", \"T\", \"U\", \"V\", \"W\", \"X\", \"Y\", \"Z\"]\n\ndef to_E(x)\n b = \"\"\n t = true\n while x > 26 do\n if x % 26 == 0 then\n str = to_E(x-1)\n str[-1] = \"Z\"\n b = str + b\n x = 0\n t = false\n else\n b = $table[(x % 26) - 1] + b\n x /= 26\n end\n end\n if t then\n b = $table[x-1] + b\n end\n return b\nend\n\ndef to_T(x)\n str = x.gsub(/[0123456789]/, \"*\")\n k = str.index(\"*\")\n str1 = str[0...k]\n r = 0\n q = 26\n n = str1.size\n n.times do |i|\n r += ($table.index(str1[i]) + 1)*q**(n-i-1)\n# p (n-i-1)\n# p $table.index(str1[i]) + 1\n# p r\n end\n result = \"R\" + x[k..-1] + \"C\" + r.to_s\n return result \nend\n\nn = gets.to_i\nind = []\noutd = []\nn.times do |i|\n ind[i] = gets.chomp\n r = ind[i].index(\"R\")\n c = ind[i].index(\"C\")\n str = ind[i].gsub(/[ABDEFGHIJKLMNOPQSTUVWXYZ]/, \"*\")\n k = str.index(\"*\")\n if r && c && !k then\n ind[i] = to_E(ind[i][c+1..-1].to_i) + ind[i][r+1...c]\n else\n ind[i] = to_T(ind[i])\n end\nend\nn.times do |i|\n puts \"#{ind[i]}\"\nend"}, {"source_code": "def excel_to_index(number)\n row = number.gsub(/[a-zA-Z]/, '').chomp\n column = number.gsub(/\\d/, '').chomp\n\n length = column.length\n column_index = 0\n column.each_char do |c|\n column_index += (c.ord - 'A'.ord + 1) * (26 ** (length - 1))\n length -= 1\n end\n\n 'R' + row + 'C' + column_index.to_s\nend\n\ndef index_to_excel(number)\n row, column = number.sub(/R/, '').split(/C/).map(&:to_i)\n\n result = []\n begin\n x = column % 26\n x = 26 if 0 == x\n column = (column - x) / 26\n\n result.push x\n puts column\n end while column > 0\n\n column_letters = ''\n result.reverse_each { |e| \n column_letters += (e.ord + 'A'.ord - 1).chr\n }\n\n column_letters + row.to_s\nend \n\ncount = gets.to_i\n\ncount.times do\n num = gets.upcase\n if num[/R\\d+C\\d+/]\n puts index_to_excel num\n else\n puts excel_to_index num\n end \nend"}, {"source_code": "#!/usr/bin/ruby\ndef fromcell(s)\n r=0\n s.length.times{|i|\n #r=r*26+s[i]-64\n }\n return r\nend\n\ndef tocell(n)\n r=\"\"\n while n>0 do\n r=(n%26+64).chr+r\n n/=26\n end\n return r\nend\n\nn=gets.chomp.to_i\nn.times{|z|\n s=gets.chomp\n if s=~/^([A-Z]+)(\\d+)$/ then \nputs \"R\"\n#+$2.to_s+\"C\"+fromcell($1).to_s\nend\n if s=~/R(\\d+)C(\\d+)/ then puts tocell($2.to_i)+$1.to_s end\n}\nexit(0)\n"}, {"source_code": "\ndef a_to_n(line)\n length = line.length\n i = 0\n sum = 0\n while i < length\n sum = sum * 26 + (line[i].ord - 'A'.ord + 1) \n i = i + 1\n end\n return sum\nend\n\ndef n_to_a(line)\n\n row_str = \"\"\n row_num = line.to_i\n while row_num > 0\n row_str.prepend( (64 + row_num % 26).chr) \n row_num = row_num/26\n end\n return row_str\n\nend\n\ndef xy_to_rc(line)\n col_size = line =~ /[0-9]/\n\n col_str = line[0...col_size]\n row_str = line[col_size...-1]\n\n col = a_to_n(col_str)\n row = row_str.to_i\n\n print(\"R\")\n print(row)\n print(\"C\")\n print(col)\n\nend\n\ndef rc_to_xy(line)\n col_size = line =~ /[C]/\n\n row_str = line[1...col_size]\n col_str = line[col_size+1...-1]\n\n col = n_to_a(col_str)\n row = row_str.to_i\n\n print(col)\n print(row)\nend\n\ndef isrc(line)\n if line[0] == 'R' && line[1] =~ /[0-9]/\n if line[2...-1].include? \"C\"\n return true\n end\n end\n return false\nend\n\n\nline_count = gets.to_i\n\nwhile line_count > 0 \n line_string = gets\n if isrc(line_string)\n rc_to_xy(line_string)\n else\n xy_to_rc(line_string)\n end\n print(\"\\n\")\n line_count = line_count-1\nend\n"}, {"source_code": "\ndef a_to_n(line)\n length = line.length\n i = 0\n sum = 0\n while i < length\n sum = sum * 26 + (line[i].ord - 'A'.ord + 1) \n i = i + 1\n end\n return sum\nend\n\ndef n_to_a(line)\n\n row_str = \"\"\n row_num = line.to_i\n while row_num > 0\n row_str.prepend( (64 + row_num % 26).chr) \n row_num = row_num/26\n end\n return row_str\n\nend\n\ndef xy_to_rc(line)\n col_size = line =~ /[0-9]/\n\n col_str = line[0...col_size]\n row_str = line[col_size...-1]\n\n col = a_to_n(col_str)\n row = row_str.to_i\n\n print(\"R\")\n print(row)\n print(\"C\")\n print(col)\n\nend\n\ndef rc_to_xy(line)\n col_size = line =~ /[C]/\n\n row_str = line[1...col_size]\n col_str = line[col_size+1...-1]\n\n col = n_to_a(col_str)\n row = row_str.to_i\n\n print(col)\n print(row)\nend\n\ndef isrc(line)\n if line[0] == 'R' && line[1] =~ /[0-9]/\n if line[2...-1].include? \"C\"\n return true\n end\n end\n return false\nend\n\n\nline_count = gets.to_i\n\nwhile line_count > 0 \n line_string = gets\n if isrc(line_string)\n rc_to_xy(line_string)\n else\n xy_to_rc(line_string)\n end\n print(\"\\n\")\n line_count = line_count-1\nend\n"}, {"source_code": "\t#recibiendo el numero de iteraciones\n\tinput=gets.chomp\n\tn=input.to_i\n\t#metodo para saber si la cadena es excel o random\n\tdef validar_excel(a)\n\t\tif (a.include?(\"R\") && a.include?(\"C\")) #como el metodo random tiene R&C \n\t\t\tir=a.index(\"R\") \n\t\t\tic=a.index(\"C\")\n\t\t\t\n\t\t\tif a.index(\"R\")==0\n\t\t\t\tif a.index(\"C\")==1\n\t\t\t\t\tif (a[2].ord>=48 && a[2].ord<=57)\n\t\t\t\t\t\treturn true\n\t\t\t\t\telse\n\t\t\t\t\t\treturn false\n\t\t\t\t\tend\n\t\t\t\telse\n\t\t\t\t\tnum_char=0\n\t\t\t\t\t0.upto(a.size-1) do |j|\n\t\t\t\t\t\tif (a[j].ord>=48 && a[j].ord<=57) #ASCII para diferenciar num & str\n\t\t\t\t\t\t\tnum_char+=0\n\t\t\t\t\t\telse\n\t\t\t\t\t\t\tnum_char+=1\n\t\t\t\t\t\tend\n\t\t\t\t\tend\n\t\t\t\tend\n\t\t\t\t\n\t\t\t\treturn num_char==2? false : true\n\t\t\telse\n\t\t\t\treturn true\n\t\t\tend\n\t\telse\n\t\t\treturn true\n\t\tend\n\tend\n\t#revisar\n\tdef convertir(ar)\n\t\tn=ar.size-1\t\n\t\ti=0\n\t\tnum=0\n\t\t\n\t\tloop do\n\t\t\tif n<0\n\t\t\t\tbreak\n\t\t\tend\n\t\t\t\n\t\t\tnum=num+(ar[i].ord-64)*(26**n)\n\t\t\tn-=1\n\t\t\ti+=1\n\t\tend\n\t\t\n\t\treturn num\n\tend\n\t\n\t#primer metodo de BC23 a R23C55\n\tdef excel_to_random(arr)\n\t\tparte_num=\"\"\n\t\tparte_str=\"\"\n\t\t\n\t\t0.upto(arr.size-1) do |k|\n\t\t\tif (arr[k].ord>=48 && arr[k].ord<=57) #ASCII para diferenciar num & str\n\t\t\t\tparte_num=parte_num+arr[k]\n\t\t\telse\n\t\t\t\tparte_str=parte_str+arr[k]\n\t\t\tend\n\t\tend\n\t\n\t\tparte_c=convertir(parte_str)\n\t\trandom=\"R\"+parte_num+\"C\"+\"#{parte_c}\" #creando el random\n\t\t\n\t\treturn random\n\tend\n\t\n\t#metodo divisiones sucesivas?\n\tdef divsuc(num)\n\t\tnum_car=26\n\t\tpivot=num\n\t\tndig=0\n\t\tarreglo_num=[]\n\t\tpotencia=1\n\t\t\n\t\tloop do\n\t\t\tpivot=pivot-26**potencia\n\t\t\tndig+=1\n\t\t\tpotencia+=1\n\t\t\tif pivot<=0\n\t\t\t\tbreak \n\t\t\tend\n\t\tend\n\t\t\n\t\t#real deal\n\t\tloop do\n\t\t\tif num==0\n\t\t\t\tbreak\n\t\t\tend\n\t\t\tif num%num_car !=0\n\t\t\t\tarreglo_num[ndig-1]=((num%num_car)+64).chr()\n\t\t\t\tnum=num/num_car\n\t\t\telse #COMPORTAMIENTO DIFERENTE CUANDO EL num ES MULTIPLE DE 26 !! (2 days!)\n\t\t\t\tq=num/num_car\n\t\t\t\tnum=q-1\n\t\t\t\tarreglo_num[ndig-1]=(num_car+64).chr()\n\t\t\tend\n\t\t\tndig-=1\n\t\tend\n\t\treturn arreglo_num\n\tend\n\t\n\t#segundo metodo de R23C55 a BC23\n\tdef random_to_excel(arr)\n\t\trow=\"\"\n\t\tcol=\"\"\n\t\ta=0\n\t\t\n\t\t1.upto(arr.size-1) do |k|\n\t\t\t(arr[k]==\"C\")? a+=1:a+=0\n\n\t\t\tif a==0\n\t\t\t\trow=row+arr[k]\n\t\t\telse\n\t\t\t\tif (arr[k]!=\"C\") \n\t\t\t\t\tcol=col+arr[k]\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\t\tcol=col.to_i\n\t\tcole=divsuc(col)#convertir la columna a excel\n\t\texcel=cole.join+row\n\t\t\n\t\treturn excel\n\tend\n\t\n\t$res=[] #definición de la variable GLOBAL, para poder imprimirlo después\n\t\n\t0.upto(n-1) do |j|\n\t\tcadena=gets.chomp.upcase\n\t\tarray=cadena.split(\"\")\n\t\ti=validar_excel(array)\n\t\t\n\t\tif i\n\t\t\tresultado=excel_to_random(array) #TRUE ES excel\n\t\telse\n\t\t\tresultado=random_to_excel(array)\n\t\tend\n\t\t\t\n\t\t$res[j]=resultado \t#recordar que el puts imprime con /n y el print no lo hace\n\tend\n\t#IMPRIMIR RESULTADO\n\tputs $res"}, {"source_code": "n = gets.to_i\nn.times do\n s = gets\n m = /R(\\d+)C(\\d+)/.match(s)\n if m != nil\n val = m[2].to_i\n col = \"\"\n while val > 0\n col.concat((val % 26 + 65 - 1).chr)\n val /= 26\n end\n col.reverse!\n puts \"#{col}#{m[1]}\"\n else\n m = /([A-Z]+)(\\d+)/.match(s)\n col = m[1]\n col.reverse!\n cnt, base = 0, 1\n col.length.times do |i|\n cnt += (col[i].ord - 'A'.ord + 1) * base\n base *= 26\n end\n puts \"R#{m[2]}C#{cnt}\"\n end\nend\n\n"}, {"source_code": "n = gets\nn = Integer(n)\nwhile n>0\n string = gets\n m = string.size()\n index = 0\n while ('A'..'Z').include? string[index]\n index += 1\n end\n while !(('A'..'Z').include? string[index])\n index += 1\n if index >= m\n break;\n end\n end\n if index == m\n index = 0\n while ('A'..'Z').include? string[index]\n index+=1\n end\n index -= 1\n c = 0\n count = 1\n til = index\n until index < 0\n c += (string[index].unpack(\"c\")[0] - 'A'.unpack(\"c\")[0] + 1) * count\n index -= 1\n count *= 26\n end\n puts \"R#{string[til+1..m].chomp}C#{c}\"\n else\n index = string.index(\"C\")\n count = Integer(string[index+1..m-2].chomp)\n k = \"\"\n while count > 0\n if count % 26 == 0\n k = \"Z\" + k\n count /= 26\n count -= 1\n end\n if count>0\n k = (count % 26 + 64).chr + k\n count /= 26 \n end\n end\n puts \"#{k}#{string[1..index-1]}\"\n end\n n-=1\nend"}, {"source_code": "#=================================\n# Better be sure than sorry. \n#=================================\n$stdin = File.open(\"./input.txt\", \"r\") if ENV[\"LOGNAME\"]==\"skydos\"\n#=================================\n\ndef to_number str\n num = 0\n digits = [*?A..?Z]\n \n pow=1\n str.reverse.each_char{|c| \n d = digits.index(c) + 1\n \n num+=d*pow\n pow*=26\n }\n \n num\nend\n\ndef to_first line\n letters, number = line.scan(/([A-Z]+)([0-9]+)/)[0]\n \"R#{number}C#{to_number letters}\"\nend\n\ndef to_27 num\n ans = \"\"\n digits = [*?A..?Z]\n \n num = num.to_i\n while num!=0\n ans+=digits[num%27 - 1]\n num/=27\n end\n \n ans.reverse\nend\n\ndef to_second line\n a, b = line.scan(/R([0-9]+)C([0-9]+)/)[0] \n \"#{to_27 b}#{a}\"\nend\n\nn=gets.to_i\n\nn.times{\n line=gets.strip\n \n if /R([0-9]+)C([0-9]+)/.match line\n puts to_second line\n else\n puts to_first line\n end\n}"}, {"source_code": "num = gets.chomp.to_i\nnum.times do\n old = gets.chomp\n results = old.match('R(\\d+)C(\\d+)')\n if results != nil\n col_num = results[2].to_i\n base = 1;\n col_str = \"\"\n while col_num > 26\n this_char = col_num % 26\n col_num /= 26\n if this_char == 0\n this_char = 26\n end\n col_num -= 1\n col_str = \"#{(this_char+64).chr}#{col_str}\"\n end\n col_str = \"#{(col_num+64).chr}#{col_str}\"\n puts \"#{col_str}#{results[1]}\"\n else\n results = old.match('([A-Z]+)(\\d+)')\n col_str = results[1]\n base = 26**(col_str.length-1)\n col_num = 0\n col_str.each_char do |c| \n col_num += (c.ord-64)*base\n base /= 26\n end\n puts \"R#{results[2]}C#{col_num}\"\n end\nend"}, {"source_code": "def main\n\ttc=gets.chomp.to_i;\n\talp=[*'A'..'Z'];\n\ths={}\n\ti=1;\n\talp.each do |z|\n\t\ths[z]=i;\n\t\ti+=1;\n\tend\n\tfor i in 1..tc\n\t\ts=gets.chomp.to_s;\n\t\tif s.count(\"R\")==1 and s.count(\"C\")==1\n\t\t\tindc=s.index(\"C\");\n\t\t\tcol=s[indc+1...(s.length)].to_i;\n\t\t\tj=1;\n\t\t\twhile(26*j+10\n l = STDIN.gets.chop\n row = nil\n col = nil\n /R(?\\d+)C(?\\d+)/=~l\n unless (row and col)\n /(?[A-Z]+)(?\\d+)/=~l\n col_ = 0\n i=0\n col.each_byte do |a|\n pos = col.length - i - 1\n col_+=('Z'.bytes.to_a.first-'A'.bytes.to_a.first+1)**pos*(a-'A'.bytes.to_a.first+1)\n i+=1\n end\n row = row.to_i\n puts \"R\"+row.to_s+\"C\"+col_.to_s\n else\n col = col.to_i\n col_ = ''\n while col!=0\n temp = col % ('Z'.bytes.to_a.first-'A'.bytes.to_a.first+1)\n col_+=['A'.bytes.to_a.first+temp-1].pack(\"U\")\n col /= ('Z'.bytes.to_a.first-'A'.bytes.to_a.first+1)\n end\n col_.reverse!\n puts col_+row\n end\n n-=1\nend\n\n"}, {"source_code": "\t#recibiendo el numero de iteraciones\n\tinput=gets.chomp\n\tn=input.to_i\n\t#metodo para saber si la cadena es excel o random\n\tdef validar_excel(a)\n\t\tif (a.include?(\"R\") && a.include?(\"C\")) #como el metodo random tiene R&C \n\t\t\tif a.index(\"R\")==0\n\t\t\t\tnum_char=0\n\t\t\t\t0.upto(a.size-1) do |j|\n\t\t\t\t\tif (a[j].ord>=48 && a[j].ord<=57) #ASCII para diferenciar num & str\n\t\t\t\t\t\tnum_char+=0\n\t\t\t\t\telse\n\t\t\t\t\t\tnum_char+=1\n\t\t\t\t\tend\n\t\t\t\tend\n\t\t\t\t\n\t\t\t\treturn num_char==2? false : true\n\t\t\telse\n\t\t\t\treturn true\n\t\t\tend\n\t\telse\n\t\t\treturn true\n\t\tend\n\tend\n\t#revisar\n\tdef convertir(ar)\n\t\tn=ar.size-1\t\n\t\ti=0\n\t\tnum=0\n\t\t\n\t\tloop do\n\t\t\tif n<0\n\t\t\t\tbreak\n\t\t\tend\n\t\t\t\n\t\t\tnum=num+(ar[i].ord-64)*(26**n)\n\t\t\tn-=1\n\t\t\ti+=1\n\t\tend\n\t\t\n\t\treturn num\n\tend\n\t\n\t#primer metodo de BC23 a R23C55\n\tdef excel_to_random(arr)\n\t\tparte_num=\"\"\n\t\tparte_str=\"\"\n\t\t\n\t\t0.upto(arr.size-1) do |k|\n\t\t\tif (arr[k].ord>=48 && arr[k].ord<=57) #ASCII para diferenciar num & str\n\t\t\t\tparte_num=parte_num+arr[k]\n\t\t\telse\n\t\t\t\tparte_str=parte_str+arr[k]\n\t\t\tend\n\t\tend\n\t\n\t\tparte_c=convertir(parte_str)\n\t\trandom=\"R\"+parte_num+\"C\"+\"#{parte_c}\" #creando el random\n\t\t\n\t\treturn random\n\tend\n\t\n\t#metodo divisiones sucesivas?\n\tdef divsuc(num)\n\t\tnum_car=26\n\t\tpivot=num\n\t\tndig=0\n\t\tarreglo_num=[]\n\t\tpotencia=1\n\t\t\n\t\tloop do\n\t\t\tpivot=pivot-26**potencia\n\t\t\tndig+=1\n\t\t\tpotencia+=1\n\t\t\tif pivot<=0\n\t\t\t\tbreak \n\t\t\tend\n\t\tend\n\t\t\n\t\t#real deal\n\t\tloop do\n\t\t\tif num==0\n\t\t\t\tbreak\n\t\t\tend\n\t\t\tif num%num_car !=0\n\t\t\t\tarreglo_num[ndig-1]=((num%num_car)+64).chr()\n\t\t\t\tnum=num/num_car\n\t\t\telse #COMPORTAMIENTO DIFERENTE CUANDO EL num ES MULTIPLE DE 26 !! (2 days!)\n\t\t\t\tq=num/num_car\n\t\t\t\tnum=q-1\n\t\t\t\tarreglo_num[ndig-1]=(num_car+64).chr()\n\t\t\tend\n\t\t\tndig-=1\n\t\tend\n\t\treturn arreglo_num\n\tend\n\t\n\t#segundo metodo de R23C55 a BC23\n\tdef random_to_excel(arr)\n\t\trow=\"\"\n\t\tcol=\"\"\n\t\ta=0\n\t\t\n\t\t1.upto(arr.size-1) do |k|\n\t\t\t(arr[k]==\"C\")? a+=1:a+=0\n\n\t\t\tif a==0\n\t\t\t\trow=row+arr[k]\n\t\t\telse\n\t\t\t\tif (arr[k]!=\"C\") \n\t\t\t\t\tcol=col+arr[k]\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\t\tcol=col.to_i\n\t\tcole=divsuc(col)#convertir la columna a excel\n\t\texcel=cole.join+row\n\t\t\n\t\treturn excel\n\tend\n\t\n\t$res=[] #definición de la variable GLOBAL, para poder imprimirlo después\n\t\n\t0.upto(n-1) do |j|\n\t\tcadena=gets.chomp.upcase\n\t\tarray=cadena.split(\"\")\n\t\ti=validar_excel(array)\n\t\t\n\t\tif i\n\t\t\tresultado=excel_to_random(array) #TRUE ES excel\n\t\telse\n\t\t\tresultado=random_to_excel(array)\n\t\tend\n\t\t\t\n\t\t$res[j]=resultado \t#recordar que el puts imprime con /n y el print no lo hace\n\tend\n\t#IMPRIMIR RESULTADO\n\tputs $res"}, {"source_code": "r = /R\\d+C\\d+/\nrow_regex = /\\d+/\n\nn = gets.chomp.to_i\n\nn.times do\n line = gets.chomp\n puts line.match(r)\n if line.match(r)\n row = line[1.. (line =~ /C/)].to_i\n c = line[(line =~ /C/)+1..-1].to_i\n a = 0; b = 0\n a = c\n res = \"\"\n while (a+26) > 26\n a = c / 26\n \t\t\tb = c % 26 == 0 ? 26 : c % 26\n \t\t\ta = c % 26 == 0 ? a - 1 : a\n \t\t\tc = a\n res.prepend(((b-1) + 65).chr)\n end\n puts \"#{res}#{row}\"\n else\n num_index = line =~ row_regex\n row = line[num_index..line.length()]\n col = 0\n col_str = line[0..num_index-1]\n col_str.split('').map.with_index do |chr, i|\n col += (chr.ord - 'A'.ord + 1) * (26 ** (num_index-1-i))\n end\n puts \"R#{row}C#{col}\"\n end\nend\n"}, {"source_code": "n = gets().to_i\nfor i in 1..n\n\tline = gets().split(/(\\d+)/)\n\tif line[0] == 'R'\n\t\tcol = line[3].to_i\n\t\tfor j in 1..100\n\t\t\tbreak if 26**j >= col\n\t\t\tcol -= 26**j\n\t\tend\n\t\ts = (col-1).to_s(26).rjust(j,'0').tr('0123456789abcdefghijklmnop', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')\n\t\tputs s << line[1]\n\telse\n\t\trow = line[1]\n\t\tans = 1 + line[0].tr('ABCDEFGHIJKLMNOPQRSTUVWXYZ', '0123456789abcdefghijklmnop').to_i(26) \n\t\tfor j in 1..line[0].length-1\n\t\t\tans += 26**j\n\t\tend\n\t\tputs 'R' << row << 'C' << ans.to_s\n\tend\nend\n"}], "src_uid": "910c0e650d48af22fa51ab05e8123709"} {"nl": {"description": "Many computer strategy games require building cities, recruiting army, conquering tribes, collecting resources. Sometimes it leads to interesting problems. Let's suppose that your task is to build a square city. The world map uses the Cartesian coordinates. The sides of the city should be parallel to coordinate axes. The map contains mines with valuable resources, located at some points with integer coordinates. The sizes of mines are relatively small, i.e. they can be treated as points. The city should be built in such a way that all the mines are inside or on the border of the city square. Building a city takes large amount of money depending on the size of the city, so you have to build the city with the minimum area. Given the positions of the mines find the minimum possible area of the city.", "input_spec": "The first line of the input contains number n — the number of mines on the map (2 ≤ n ≤ 1000). Each of the next n lines contains a pair of integers xi and yi — the coordinates of the corresponding mine ( - 109 ≤ xi, yi ≤ 109). All points are pairwise distinct.", "output_spec": "Print the minimum area of the city that can cover all the mines with valuable resources.", "sample_inputs": ["2\n0 0\n2 2", "2\n0 0\n0 3"], "sample_outputs": ["4", "9"], "notes": null}, "positive_code": [{"source_code": "n = STDIN.readline.to_i\nx_min, y_min = 10**10, 10**10\nx_max, y_max = -x_min, -y_min\nn.times do\n\tx, y = STDIN.readline.split.map {|s| s.to_i }\n\tx_min = x if x < x_min\n\ty_min = y if y < y_min\n\tx_max = x if x > x_max\n\ty_max = y if y > y_max\nend\na, b = x_max-x_min, y_max-y_min\nputs [a, b].max**2\n"}, {"source_code": "n= gets.to_i\nmx, my, lx, ly = -1.0/0.0, -1.0/0.0, 1.0/0.0, 1.0/0.0\nn.times do\n x,y = gets.split.map(&:to_i)\n mx = [mx,x].max\n my = [my,y].max\n lx = [lx,x].min\n ly = [ly,y].min\nend\nputs [mx-lx,my-ly].max**2\n"}, {"source_code": "data = gets.to_i.times.map{gets.split.map{|x|x.to_i}}\nmin_x = data.map{|x|x[0]}.min\nmax_x = data.map{|x|x[0]}.max\nmin_y = data.map{|x|x[1]}.min\nmax_y = data.map{|x|x[1]}.max\nresult = [(max_x - min_x), (max_y - min_y)].max**2\nputs result"}, {"source_code": "# 485B\n\nmin_x = min_y = 10 ** 9 + 1\nmax_x = max_y = -min_x\ngets.to_i.times do\n x, y = gets.chomp.split.map &:to_i\n if x < min_x\n min_x = x\n end\n if x > max_x\n max_x = x\n end\n if y < min_y\n min_y = y\n end\n if y > max_y\n max_y = y\n end\nend\nputs [max_x - min_x, max_y - min_y].max ** 2"}, {"source_code": "n = gets.to_i\nmaxX = -10 ** 9\nmaxY = -10 ** 9\nminX = 10 ** 9\nminY = 10 ** 9\nn.times do\n\tinput = gets\n\tx_coor = input.split[0].to_i\n\ty_coor = input.split[1].to_i\n\tif x_coor > maxX\n\t\tmaxX = x_coor\n\tend\n\tif y_coor > maxY\n\t\tmaxY = y_coor\n\tend\n\tif x_coor < minX\n\t\tminX = x_coor\n\tend\n\tif y_coor < minY\n\t\tminY = y_coor\n\tend\t\nend\nx = maxX - minX\ny = maxY - minY\nputs x > y ? x ** 2 : y ** 2"}, {"source_code": "INF = 2e10;\n\ndef max(a, b)\n a > b ? a : b\nend\n\ndef min(a, b)\n a < b ? a : b\nend\n\nn = gets.chomp.to_i\ntop = -INF\nbottom = INF\nright = -INF\nleft = INF\n\nn.times do\n p = gets.chomp.split(\" \").map{|str|str.to_i}\n \n top = max(top, p[1])\n bottom = min(bottom, p[1])\n right = max(right, p[0])\n left = min(left, p[0])\nend\n\nwidth = right - left\nheight = top - bottom\n\nputs max(width, height) ** 2\n"}], "negative_code": [{"source_code": "n = STDIN.readline.to_i\nx_min, y_min = 10**10, 10**10\nx_max, y_max = -x_min, -y_min\nn.times do\n\tx, y = STDIN.readline.split.map {|s| s.to_i }\n\tx_min = x if x < x_min\n\ty_min = y if y < y_min\n\tx_max = x if x > x_min\n\ty_max = y if y > y_min\nend\na, b = x_max-x_min, y_max-y_min\nputs [a, b].max**2\n"}, {"source_code": "n= gets.to_i\nmx,my,lx,ly=0,0,0,0\nn.times do\n x,y = gets.split.map(&:to_i)\n mx = [mx,x].max\n my = [my,y].max\n lx = [lx,x].min\n ly = [ly,y].min\nend\nputs [mx-lx,my-ly].max**2"}, {"source_code": "data = gets.to_i.times.map{gets.split.map{|x|x.to_i}}\nmin_x = data.min{|row| row[0]}[0]\nmax_x = data.max{|row| row[0]}[0]\nmin_y = data.min{|row| row[1]}[1]\nmax_y = data.max{|row| row[1]}[1]\nresult = [(max_x - min_x), (max_y - min_y)].max**2\nputs result"}, {"source_code": "def get_task_data(filename)\n raw_data = open(filename){|f|f.readlines}\n count = raw_data[0].to_i\n return false if count.zero?\n data = raw_data[1,raw_data.size].map{|str|str.split.map{|digit| digit.to_i}}\n return false if data.select{|row| row.size!=2}.any?\n return data.size==count ? data : false\nend\n\ndef solve(filename)\n task_data = get_task_data filename\n return 0 unless task_data\n min_x = task_data.min{|row| row[0]}[0]\n max_x = task_data.max{|row| row[0]}[0]\n min_y = task_data.min{|row| row[1]}[1]\n max_y = task_data.max{|row| row[1]}[1]\n [(max_x - min_x), (max_y - min_y)].max**2\nend"}, {"source_code": "data = gets.to_i.times.map{gets.split.map{|x|x.to_i}}\nputs data\nmin_x = data.map{|x|x[0]}.min\nmax_x = data.map{|x|x[0]}.max\nmin_y = data.map{|x|x[1]}.min\nmax_y = data.map{|x|x[1]}.max\nputs \"min_x:#{min_x} max_x:#{max_x} min_y:#{min_y} max_y:#{max_y}\"\nresult = [(max_x - min_x), (max_y - min_y)].max**2\nputs result"}, {"source_code": "# 485B\n\nmin_x = min_y = 10 ** 9 + 1\nmax_x = max_y = -min_x\ngets.to_i.times do\n x, y = gets.chomp.split.map &:to_i\n if x < min_x\n min_x = x\n elsif x > max_x\n max_x = x\n end\n if y < min_y\n min_y = y\n elsif y > max_y\n max_y = y\n end\nend\nputs [max_x - min_x, max_y - min_y].max ** 2"}, {"source_code": "# 485B\n\nmin_x = min_y = 10 ** 9 + 1\nmax_x = max_y = -min_x\ngets.to_i.times do\n x, y = gets.chomp.split.map &:to_i\n if x < min_x\n min_x = x\n end\n if x > max_x\n max_x = x\n end\n if y < min_y\n min_y = y\n end\n if y > max_y\n max_y = y\n end\nend\np [min_x, max_x, min_y, max_y]\nputs [max_x - min_x, max_y - min_y].max ** 2"}, {"source_code": "n = gets.to_i\nmaxX = -10 ** 9\nmaxY = -10 ** 9\nminX = 10 ** 9\nminY = 10 ** 9\nn.times do\n\tinput = gets\n\tx_coor = input.split[0].to_i\n\ty_coor = input.split[1].to_i\n\tif x_coor > maxX\n\t\tmax = x_coor\n\tend\n\tif y_coor > maxY\n\t\tmaxY = y_coor\n\tend\n\tif x_coor < minX\n\t\tminX = x_coor\n\tend\n\tif y_coor < minY\n\t\tminY = y_coor\n\tend\t\nend\nx = maxX - minX\ny = maxY - minY\nputs x > y ? x ** 2 : y ** 2"}, {"source_code": "n = gets.to_i\nmax = -10 ** 9\nmin = 10 ** 9\nn.times do\n\tinput = gets\n\tx_coor = input.split[0].to_i\n\ty_coor = input.split[1].to_i\n\tif x_coor > max\n\t\tmax = x_coor\n\telsif y_coor > max\n\t\tmax = y_coor\n\tend\n\tif x_coor < min\n\t\tmin = x_coor\n\telsif y_coor < min\n\t\tmin = y_coor\n\tend\t\nend\n\nputs (max - min) ** 2"}, {"source_code": "n = gets.to_i\nmax = 0\nn.times do\n\tinput = gets\n\tx_coor = input.split[0].to_i\n\ty_coor = input.split[1].to_i\n\tif x_coor > max\n\t\tmax = x_coor\n\telsif y_coor > max\n\t\tmax = y_coor\n\tend\t\nend\n\nputs max ** 2"}, {"source_code": "n = gets.to_i\nmaxX = -10 ** 9\nmaxY = -10 ** 9\nminX = 10 ** 9\nminY = -10 ** 9\nn.times do\n\tinput = gets\n\tx_coor = input.split[0].to_i\n\ty_coor = input.split[1].to_i\n\tif x_coor > maxX\n\t\tmax = x_coor\n\tend\n\tif y_coor > maxY\n\t\tmaxY = y_coor\n\tend\n\tif x_coor < minX\n\t\tminX = x_coor\n\tend\n\tif y_coor < minY\n\t\tminY = y_coor\n\tend\t\nend\nx = maxX - minX\ny = maxY - minY\nputs x > y ? x ** 2 : y ** 2"}], "src_uid": "016e00b2b960be792d4ec7fcfb7976e2"} {"nl": {"description": "Heidi has finally found the mythical Tree of Life – a legendary combinatorial structure which is said to contain a prophecy crucially needed to defeat the undead armies.On the surface, the Tree of Life is just a regular undirected tree well-known from computer science. This means that it is a collection of n points (called vertices), some of which are connected using n - 1 line segments (edges) so that each pair of vertices is connected by a path (a sequence of one or more edges).To decipher the prophecy, Heidi needs to perform a number of steps. The first is counting the number of lifelines in the tree – these are paths of length 2, i.e., consisting of two edges. Help her!", "input_spec": "The first line of the input contains a single integer n – the number of vertices in the tree (1 ≤ n ≤ 10000). The vertices are labeled with the numbers from 1 to n. Then n - 1 lines follow, each describing one edge using two space-separated numbers a b – the labels of the vertices connected by the edge (1 ≤ a < b ≤ n). It is guaranteed that the input represents a tree.", "output_spec": "Print one integer – the number of lifelines in the tree.", "sample_inputs": ["4\n1 2\n1 3\n1 4", "5\n1 2\n2 3\n3 4\n3 5"], "sample_outputs": ["3", "4"], "notes": "NoteIn the second sample, there are four lifelines: paths between vertices 1 and 3, 2 and 4, 2 and 5, and 4 and 5."}, "positive_code": [{"source_code": "nodes = gets.to_i\nedges = nodes-1\ng = {}\nedges.times do\n from, to = gets.split(' ').map(&:to_i)\n g[from] ||= []\n g[from].push(to)\n g[to] ||= []\n g[to].push(from)\nend\n\nputs g.reduce(0) { |res, (n, nxts)|\n res + nxts.size * (nxts.size - 1) / 2\n}\n"}], "negative_code": [], "src_uid": "fdd50853348b6f297a62a3b729d8d4a5"} {"nl": {"description": "Consider the set of all nonnegative integers: $$${0, 1, 2, \\dots}$$$. Given two integers $$$a$$$ and $$$b$$$ ($$$1 \\le a, b \\le 10^4$$$). We paint all the numbers in increasing number first we paint $$$0$$$, then we paint $$$1$$$, then $$$2$$$ and so on.Each number is painted white or black. We paint a number $$$i$$$ according to the following rules: if $$$i = 0$$$, it is colored white; if $$$i \\ge a$$$ and $$$i - a$$$ is colored white, $$$i$$$ is also colored white; if $$$i \\ge b$$$ and $$$i - b$$$ is colored white, $$$i$$$ is also colored white; if $$$i$$$ is still not colored white, it is colored black. In this way, each nonnegative integer gets one of two colors.For example, if $$$a=3$$$, $$$b=5$$$, then the colors of the numbers (in the order from $$$0$$$) are: white ($$$0$$$), black ($$$1$$$), black ($$$2$$$), white ($$$3$$$), black ($$$4$$$), white ($$$5$$$), white ($$$6$$$), black ($$$7$$$), white ($$$8$$$), white ($$$9$$$), ...Note that: It is possible that there are infinitely many nonnegative integers colored black. For example, if $$$a = 10$$$ and $$$b = 10$$$, then only $$$0, 10, 20, 30$$$ and any other nonnegative integers that end in $$$0$$$ when written in base 10 are white. The other integers are colored black. It is also possible that there are only finitely many nonnegative integers colored black. For example, when $$$a = 1$$$ and $$$b = 10$$$, then there is no nonnegative integer colored black at all. Your task is to determine whether or not the number of nonnegative integers colored black is infinite.If there are infinitely many nonnegative integers colored black, simply print a line containing \"Infinite\" (without the quotes). Otherwise, print \"Finite\" (without the quotes).", "input_spec": "The first line of input contains a single integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases in the input. Then $$$t$$$ lines follow, each line contains two space-separated integers $$$a$$$ and $$$b$$$ ($$$1 \\le a, b \\le 10^4$$$).", "output_spec": "For each test case, print one line containing either \"Infinite\" or \"Finite\" (without the quotes). Output is case-insensitive (i.e. \"infinite\", \"inFiNite\" or \"finiTE\" are all valid answers).", "sample_inputs": ["4\n10 10\n1 10\n6 9\n7 3"], "sample_outputs": ["Infinite\nFinite\nInfinite\nFinite"], "notes": null}, "positive_code": [{"source_code": "def solve(a, b)\n a.gcd(b) == 1 ? \"Finite\" : \"Infinite\"\nend\n\n\ngets.strip.to_i.times do\n puts solve(*gets.strip.split(' ').map(&:to_i))\nend"}, {"source_code": "number_of_tests = gets.chomp.to_i\n\nnumber_of_tests.times do\n a, b = gets.chomp.split.map(&:to_i)\n puts a.gcd(b) == 1 ? 'Finite' : 'Infinite'\nend\n"}], "negative_code": [], "src_uid": "388450021f2f33177d905879485bb531"} {"nl": {"description": "PizzaForces is Petya's favorite pizzeria. PizzaForces makes and sells pizzas of three sizes: small pizzas consist of $$$6$$$ slices, medium ones consist of $$$8$$$ slices, and large pizzas consist of $$$10$$$ slices each. Baking them takes $$$15$$$, $$$20$$$ and $$$25$$$ minutes, respectively.Petya's birthday is today, and $$$n$$$ of his friends will come, so he decided to make an order from his favorite pizzeria. Petya wants to order so much pizza that each of his friends gets at least one slice of pizza. The cooking time of the order is the total baking time of all the pizzas in the order.Your task is to determine the minimum number of minutes that is needed to make pizzas containing at least $$$n$$$ slices in total. For example: if $$$12$$$ friends come to Petya's birthday, he has to order pizzas containing at least $$$12$$$ slices in total. He can order two small pizzas, containing exactly $$$12$$$ slices, and the time to bake them is $$$30$$$ minutes; if $$$15$$$ friends come to Petya's birthday, he has to order pizzas containing at least $$$15$$$ slices in total. He can order a small pizza and a large pizza, containing $$$16$$$ slices, and the time to bake them is $$$40$$$ minutes; if $$$300$$$ friends come to Petya's birthday, he has to order pizzas containing at least $$$300$$$ slices in total. He can order $$$15$$$ small pizzas, $$$10$$$ medium pizzas and $$$13$$$ large pizzas, in total they contain $$$15 \\cdot 6 + 10 \\cdot 8 + 13 \\cdot 10 = 300$$$ slices, and the total time to bake them is $$$15 \\cdot 15 + 10 \\cdot 20 + 13 \\cdot 25 = 750$$$ minutes; if only one friend comes to Petya's birthday, he can order a small pizza, and the time to bake it is $$$15$$$ minutes. ", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of testcases. Each testcase consists of a single line that contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^{16}$$$) — the number of Petya's friends.", "output_spec": "For each testcase, print one integer — the minimum number of minutes that is needed to bake pizzas containing at least $$$n$$$ slices in total.", "sample_inputs": ["6\n12\n15\n300\n1\n9999999999999999\n3"], "sample_outputs": ["30\n40\n750\n15\n25000000000000000\n15"], "notes": null}, "positive_code": [{"source_code": "\r\n\r\n/Variables:/\r\nfriends = 0\r\ntotal_time = []\r\ntestcases = 0\r\n\r\n/Prerequisites: Enter de numer of testcases/\r\ntestcases = Integer(gets.chomp)\r\n\r\n/Firts: Request the number of friends to come/\r\nfor i in (0..(testcases-1))\r\n friends = Integer(gets.chomp)\r\n\r\n if friends <= 6\r\n friends = 6\r\n end\r\n if (friends%2)!=0\r\n friends = friends + 1\r\n end\r\n total_time[i] = (5*friends)/2\r\nend\r\ntotal_time.each do |elements|\r\n puts (elements)\r\nend\r\n"}, {"source_code": "MOD = 10 ** 9 + 7\r\ndef gs; gets.chomp; end\r\ndef gi; gets.to_i; end\r\ndef gli; gets.split.map(&:to_i); end\r\ndef cyesno(x); puts x ? :Yes : :No; end\r\ndef uyesno(x); puts x ? :YES : :NO; end\r\ndef array_to_hash_by_count(c); Hash.new(0).tap { |x| c.each { |z| x[z] += 1 } }; end\r\n\r\ndef solve\r\n puts [((gi + 1) / 2) * 5 , 15].max\r\nend\r\n\r\ndef main\r\n gi.times { solve }\r\nend\r\n\r\nmain\r\n"}, {"source_code": "gets\r\n$stdin.map(&:to_i).each do |n|\r\n puts [(n + 1) / 2 * 5, 15].max\r\nend"}], "negative_code": [{"source_code": "/\r\nProblem: Petya wants to order pizza for her birthday. \r\nShe does not know how much of her friends will come and \r\nshe wants to order so much pizza that each of his friends gets at least one slice of pizza. \r\nThe cooking time of the order is the total baking time of all the pizzas in the order.\r\nInput\r\nThe first line contains a single integer t (1≤t≤104) — the number of testcases.\r\nEach testcase consists of a single line that contains a single integer n (1≤n≤1016) — the number of Petya's friends.\r\nOutput\r\nFor each testcase, print one integer — the minimum number of minutes that is needed to bake pizzas containing at least n slices in total.\r\n/\r\n/Solution:/ \r\n/Declare the variables necessaries./\r\n/Constants:/\r\nSMALL_PIZZA = 6\r\nMEDIUM_PIZZA = 8\r\nLONG_PIZZA = 10\r\nTIME_SMALL_PIZZA = 15\r\nTIME_MEDIUM_PIZZA = 20\r\nTIME_LONG_PIZZA = 25\r\n\r\n/Variables:/\r\nfriends = 0\r\nsmall_pizza_order = 1\r\nmedium_pizza_order = 0\r\nlong_pizza_order = 0\r\ntotal_time = 0\r\n\r\ncondition = true\r\nlimit_small_pizza = false\r\nlimit_medium_pizza = false\r\nlimit_long_pizza = false\r\n\r\n/Firts: Request the number of friends to come/\r\nfriends = Integer(gets.chomp)\r\n\r\n/Second: Determinate the number of pizzas to order/\r\n/In order to translate de minimun slides at one for friends, when it is one more slide than the number of friends, /\r\n/it does not matter, because satisfate the condicion of minimun slides for person./\r\n/So, for friends equals or less than 6, the case just be order a small pizza/\r\n\r\nif friends <= 6\r\n /Case for small pizza:/\r\n small_pizza_order = 1\r\nelse\r\n /Other case/\r\n /We must to do a loop until aprox our value to the number of friends/\r\n while condition\r\n /For the max number of small pizza/\r\n if !limit_small_pizza\r\n /Sum one more pizza/\r\n small_pizza_order = small_pizza_order + 1\r\n /Quest if the number of slides is equals than the number of friends/\r\n if SMALL_PIZZA*small_pizza_order == friends || (SMALL_PIZZA*small_pizza_order) == (friends +1)\r\n limit_small_pizza = true\r\n limit_medium_pizza = true\r\n limit_long_pizza = true\r\n condition = false\r\n /Quest if is more than the number of friends/\r\n elsif SMALL_PIZZA*small_pizza_order > friends\r\n small_pizza_order = small_pizza_order - 2\r\n limit_small_pizza = true\r\n /Other case slides is small than friends and continue/\r\n end\r\n \r\n /For the max number of medium pizza/\r\n elsif !limit_medium_pizza\r\n /Sum one more pizza/\r\n medium_pizza_order = medium_pizza_order + 1\r\n /Quest if the number of slides is equals than the number of friends/\r\n if (SMALL_PIZZA*small_pizza_order + MEDIUM_PIZZA*medium_pizza_order) == friends || (SMALL_PIZZA*small_pizza_order + MEDIUM_PIZZA*medium_pizza_order) == (friends +1) || ((SMALL_PIZZA*small_pizza_order + MEDIUM_PIZZA*medium_pizza_order)-1) == friends\r\n limit_small_pizza = true\r\n limit_medium_pizza = true\r\n limit_long_pizza = true\r\n condition = false\r\n /Quest if is more than the number of friends/\r\n elsif (SMALL_PIZZA*small_pizza_order + MEDIUM_PIZZA*medium_pizza_order) > friends\r\n medium_pizza_order = medium_pizza_order - 2\r\n limit_medium_pizza = true\r\n /Other case slides is medium than friends and continue/\r\n end\r\n \r\n /For the max number of large pizza/\r\n elsif !limit_long_pizza\r\n /Sum one more pizza/\r\n long_pizza_order = long_pizza_order + 1\r\n /Quest if the number of slides is equals than the number of friends/\r\n if (SMALL_PIZZA*small_pizza_order + MEDIUM_PIZZA*medium_pizza_order + LONG_PIZZA*long_pizza_order) == friends || (SMALL_PIZZA*small_pizza_order + MEDIUM_PIZZA*medium_pizza_order + LONG_PIZZA*long_pizza_order) == (friends +1) || ((SMALL_PIZZA*small_pizza_order + MEDIUM_PIZZA*medium_pizza_order + LONG_PIZZA*long_pizza_order)-1) == friends\r\n limit_small_pizza = true\r\n limit_medium_pizza = true\r\n limit_long_pizza = true\r\n condition = false\r\n /Quest if is more than the number of friends/\r\n elsif (SMALL_PIZZA*small_pizza_order + MEDIUM_PIZZA*medium_pizza_order + LONG_PIZZA*long_pizza_order) > friends\r\n long_pizza_order = long_pizza_order - 1\r\n limit_long_pizza = true\r\n condition = false\r\n /Other case slides is large than friends and continue/\r\n end\r\n end\r\n end\r\nend\r\ntotal_time = TIME_SMALL_PIZZA*small_pizza_order + TIME_MEDIUM_PIZZA*medium_pizza_order + TIME_LONG_PIZZA*long_pizza_order\r\nputs(total_time)\r\n"}, {"source_code": "/\r\nProblem: Petya wants to order pizza for her birthday. \r\nShe does not know how much of her friends will come and \r\nshe wants to order so much pizza that each of his friends gets at least one slice of pizza. \r\nThe cooking time of the order is the total baking time of all the pizzas in the order.\r\nInput\r\nThe first line contains a single integer t (1≤t≤104) — the number of testcases.\r\nEach testcase consists of a single line that contains a single integer n (1≤n≤1016) — the number of Petya's friends.\r\nOutput\r\nFor each testcase, print one integer — the minimum number of minutes that is needed to bake pizzas containing at least n slices in total.\r\n/\r\n/Solution:/ \r\n/Declare the variables necessaries./\r\n/Constants:/\r\nSMALL_PIZZA = 6\r\nMEDIUM_PIZZA = 8\r\nLONG_PIZZA = 10\r\nTIME_SMALL_PIZZA = 15\r\nTIME_MEDIUM_PIZZA = 20\r\nTIME_LONG_PIZZA = 25\r\n\r\n/Variables:/\r\nfriends = 0\r\nsmall_pizza_order = 1\r\nmedium_pizza_order = 0\r\nlong_pizza_order = 0\r\ntotal_time = 0\r\n\r\ncondition = true\r\nlimit_small_pizza = false\r\nlimit_medium_pizza = false\r\nlimit_long_pizza = false\r\n\r\n/Firts: Request the number of friends to come/\r\nputs(\"Enter the number of friends that will come to Petya party\")\r\nfriends = Integer(gets.chomp)\r\n\r\n/Second: Determinate the number of pizzas to order/\r\n/In order to translate de minimun slides at one for friends, when it is one more slide than the number of friends, /\r\n/it does not matter, because satisfate the condicion of minimun slides for person./\r\n/So, for friends equals or less than 6, the case just be order a small pizza/\r\n\r\nif friends <= 6\r\n /Case for small pizza:/\r\n small_pizza_order = 1\r\nelse\r\n /Other case/\r\n /We must to do a loop until aprox our value to the number of friends/\r\n while condition\r\n /For the max number of small pizza/\r\n if !limit_small_pizza\r\n /Sum one more pizza/\r\n small_pizza_order = small_pizza_order + 1\r\n /Quest if the number of slides is equals than the number of friends/\r\n if SMALL_PIZZA*small_pizza_order == friends || (SMALL_PIZZA*small_pizza_order) == (friends +1)\r\n limit_small_pizza = true\r\n limit_medium_pizza = true\r\n limit_long_pizza = true\r\n condition = false\r\n /Quest if is more than the number of friends/\r\n elsif SMALL_PIZZA*small_pizza_order > friends\r\n small_pizza_order = small_pizza_order - 2\r\n limit_small_pizza = true\r\n /Other case slides is small than friends and continue/\r\n end\r\n \r\n /For the max number of medium pizza/\r\n elsif !limit_medium_pizza\r\n /Sum one more pizza/\r\n medium_pizza_order = medium_pizza_order + 1\r\n /Quest if the number of slides is equals than the number of friends/\r\n if (SMALL_PIZZA*small_pizza_order + MEDIUM_PIZZA*medium_pizza_order) == friends || (SMALL_PIZZA*small_pizza_order + MEDIUM_PIZZA*medium_pizza_order) == (friends +1) || ((SMALL_PIZZA*small_pizza_order + MEDIUM_PIZZA*medium_pizza_order)-1) == friends\r\n limit_small_pizza = true\r\n limit_medium_pizza = true\r\n limit_long_pizza = true\r\n condition = false\r\n /Quest if is more than the number of friends/\r\n elsif (SMALL_PIZZA*small_pizza_order + MEDIUM_PIZZA*medium_pizza_order) > friends\r\n medium_pizza_order = medium_pizza_order - 2\r\n limit_medium_pizza = true\r\n /Other case slides is medium than friends and continue/\r\n end\r\n \r\n /For the max number of large pizza/\r\n elsif !limit_long_pizza\r\n /Sum one more pizza/\r\n long_pizza_order = long_pizza_order + 1\r\n puts(long_pizza_order)\r\n /Quest if the number of slides is equals than the number of friends/\r\n if (SMALL_PIZZA*small_pizza_order + MEDIUM_PIZZA*medium_pizza_order + LONG_PIZZA*long_pizza_order) == friends || (SMALL_PIZZA*small_pizza_order + MEDIUM_PIZZA*medium_pizza_order + LONG_PIZZA*long_pizza_order) == (friends +1) || ((SMALL_PIZZA*small_pizza_order + MEDIUM_PIZZA*medium_pizza_order + LONG_PIZZA*long_pizza_order)-1) == friends\r\n limit_small_pizza = true\r\n limit_medium_pizza = true\r\n limit_long_pizza = true\r\n condition = false\r\n /Quest if is more than the number of friends/\r\n elsif (SMALL_PIZZA*small_pizza_order + MEDIUM_PIZZA*medium_pizza_order + LONG_PIZZA*long_pizza_order) > friends\r\n long_pizza_order = long_pizza_order - 1\r\n limit_long_pizza = true\r\n condition = false\r\n /Other case slides is large than friends and continue/\r\n end\r\n end\r\n end\r\nend\r\ntotal_time = TIME_SMALL_PIZZA*small_pizza_order + TIME_MEDIUM_PIZZA*medium_pizza_order + TIME_LONG_PIZZA*long_pizza_order\r\nputs(total_time)\r\n"}], "src_uid": "3e27f1c06a263760f5b53c3afe4bf7ee"} {"nl": {"description": "Gildong owns a bulgogi restaurant. The restaurant has a lot of customers, so many of them like to make a reservation before visiting it.Gildong tries so hard to satisfy the customers that he even memorized all customers' preferred temperature ranges! Looking through the reservation list, he wants to satisfy all customers by controlling the temperature of the restaurant.The restaurant has an air conditioner that has 3 states: off, heating, and cooling. When it's off, the restaurant's temperature remains the same. When it's heating, the temperature increases by 1 in one minute. Lastly, when it's cooling, the temperature decreases by 1 in one minute. Gildong can change the state as many times as he wants, at any integer minutes. The air conditioner is off initially.Each customer is characterized by three values: $$$t_i$$$ — the time (in minutes) when the $$$i$$$-th customer visits the restaurant, $$$l_i$$$ — the lower bound of their preferred temperature range, and $$$h_i$$$ — the upper bound of their preferred temperature range.A customer is satisfied if the temperature is within the preferred range at the instant they visit the restaurant. Formally, the $$$i$$$-th customer is satisfied if and only if the temperature is between $$$l_i$$$ and $$$h_i$$$ (inclusive) in the $$$t_i$$$-th minute.Given the initial temperature, the list of reserved customers' visit times and their preferred temperature ranges, you're going to help him find if it's possible to satisfy all customers.", "input_spec": "Each test contains one or more test cases. The first line contains the number of test cases $$$q$$$ ($$$1 \\le q \\le 500$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n \\le 100$$$, $$$-10^9 \\le m \\le 10^9$$$), where $$$n$$$ is the number of reserved customers and $$$m$$$ is the initial temperature of the restaurant. Next, $$$n$$$ lines follow. The $$$i$$$-th line of them contains three integers $$$t_i$$$, $$$l_i$$$, and $$$h_i$$$ ($$$1 \\le t_i \\le 10^9$$$, $$$-10^9 \\le l_i \\le h_i \\le 10^9$$$), where $$$t_i$$$ is the time when the $$$i$$$-th customer visits, $$$l_i$$$ is the lower bound of their preferred temperature range, and $$$h_i$$$ is the upper bound of their preferred temperature range. The preferred temperature ranges are inclusive. The customers are given in non-decreasing order of their visit time, and the current time is $$$0$$$.", "output_spec": "For each test case, print \"YES\" if it is possible to satisfy all customers. Otherwise, print \"NO\". You can print each letter in any case (upper or lower).", "sample_inputs": ["4\n3 0\n5 1 2\n7 3 5\n10 -1 0\n2 12\n5 7 10\n10 16 20\n3 -100\n100 0 0\n100 -50 50\n200 100 100\n1 100\n99 -100 0"], "sample_outputs": ["YES\nNO\nYES\nNO"], "notes": "NoteIn the first case, Gildong can control the air conditioner to satisfy all customers in the following way: At $$$0$$$-th minute, change the state to heating (the temperature is 0). At $$$2$$$-nd minute, change the state to off (the temperature is 2). At $$$5$$$-th minute, change the state to heating (the temperature is 2, the $$$1$$$-st customer is satisfied). At $$$6$$$-th minute, change the state to off (the temperature is 3). At $$$7$$$-th minute, change the state to cooling (the temperature is 3, the $$$2$$$-nd customer is satisfied). At $$$10$$$-th minute, the temperature will be 0, which satisfies the last customer. In the third case, Gildong can change the state to heating at $$$0$$$-th minute and leave it be. Then all customers will be satisfied. Note that the $$$1$$$-st customer's visit time equals the $$$2$$$-nd customer's visit time.In the second and the fourth case, Gildong has to make at least one customer unsatisfied."}, "positive_code": [{"source_code": "# frozen_string_literal: true\n\n@data = $stdin.read.split\n\ndef next_token\n @data.shift\nend\n\ndef next_int\n next_token.to_i\nend\n\ndef solve(n, m, t, l, r)\n low = m\n high = m\n last_t = 0\n n.times do |i|\n d = (t[i] - last_t)\n low -= d\n high += d\n low = l[i] if low < l[i]\n high = r[i] if high > r[i]\n return false if low > high\n\n last_t = t[i]\n end\n true\nend\n\nT = next_int\nt = Array.new(100)\nl = Array.new(100)\nr = Array.new(100)\n\nT.times do\n n = next_int\n m = next_int\n n.times do |i|\n t[i] = next_int\n l[i] = next_int\n r[i] = next_int\n end\n puts solve(n, m, t, l, r) ? 'YES' : 'NO'\nend\n"}], "negative_code": [], "src_uid": "a75b8b9b99b800762645ef7c3bc29905"} {"nl": {"description": "Polycarp is a great fan of television.He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment li and ends at moment ri.Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.Polycarp wants to check out all n shows. Are two TVs enough to do so?", "input_spec": "The first line contains one integer n (1 ≤ n ≤ 2·105) — the number of shows. Each of the next n lines contains two integers li and ri (0 ≤ li < ri ≤ 109) — starting and ending time of i-th show.", "output_spec": "If Polycarp is able to check out all the shows using only two TVs then print \"YES\" (without quotes). Otherwise, print \"NO\" (without quotes).", "sample_inputs": ["3\n1 2\n2 3\n4 5", "4\n1 2\n2 3\n2 3\n1 2"], "sample_outputs": ["YES", "NO"], "notes": null}, "positive_code": [{"source_code": "l, c = [], 0\ngets.to_i.times do\n a, b = gets.split.map(&:to_f)\n l.push(a)\n l.push(b + 0.5)\nend\nl.sort!.each do |a|\n if a % 1 == 0\n c += 1\n break if c > 2\n else\n c -= 1\n end\nend\nputs c > 2 ? :NO : :YES"}, {"source_code": "n = gets.to_i\nshows = []\nn.times do\n shows.push(gets.split.map(&:to_i))\nend\nshows.sort_by! { |start, _| start }\n\nbuffer = []\nshows.each do |start, ending|\n buffer = buffer.select { |show| show[1] >= start } + [[start, ending]]\n break if buffer.count > 2\nend\nputs buffer.count > 2 ? \"NO\" : \"YES\"\n"}], "negative_code": [], "src_uid": "9fa772b53e655a55f2ec502bc96d0e28"} {"nl": {"description": "This problem is given in two versions that differ only by constraints. If you can solve this problem in large constraints, then you can just write a single solution to the both versions. If you find the problem too difficult in large constraints, you can write solution to the simplified version only.Waking up in the morning, Apollinaria decided to bake cookies. To bake one cookie, she needs n ingredients, and for each ingredient she knows the value ai — how many grams of this ingredient one needs to bake a cookie. To prepare one cookie Apollinaria needs to use all n ingredients.Apollinaria has bi gram of the i-th ingredient. Also she has k grams of a magic powder. Each gram of magic powder can be turned to exactly 1 gram of any of the n ingredients and can be used for baking cookies.Your task is to determine the maximum number of cookies, which Apollinaria is able to bake using the ingredients that she has and the magic powder.", "input_spec": "The first line of the input contains two positive integers n and k (1 ≤ n, k ≤ 1000) — the number of ingredients and the number of grams of the magic powder. The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000), where the i-th number is equal to the number of grams of the i-th ingredient, needed to bake one cookie. The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ 1000), where the i-th number is equal to the number of grams of the i-th ingredient, which Apollinaria has.", "output_spec": "Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.", "sample_inputs": ["3 1\n2 1 4\n11 3 16", "4 3\n4 3 5 6\n11 12 14 20"], "sample_outputs": ["4", "3"], "notes": "NoteIn the first sample it is profitably for Apollinaria to make the existing 1 gram of her magic powder to ingredient with the index 2, then Apollinaria will be able to bake 4 cookies.In the second sample Apollinaria should turn 1 gram of magic powder to ingredient with the index 1 and 1 gram of magic powder to ingredient with the index 3. Then Apollinaria will be able to bake 3 cookies. The remaining 1 gram of the magic powder can be left, because it can't be used to increase the answer."}, "positive_code": [{"source_code": "n, k = readline.split(' ').collect(&:to_i)\na = readline.split(' ').collect(&:to_i)\nb = readline.split(' ').collect(&:to_i)\ndown = 0\nup = (b[0] + k) / a[0] + 1\nwhile down + 1 < up\n mid = (down + up) / 2\n need = 0\n a.zip(b).each do |aa, bb|\n if aa * mid > bb\n need += aa * mid - bb\n end\n end\n if need <= k\n down = mid\n else\n up = mid\n end\nend\nputs down\n"}, {"source_code": "input = gets.chomp.split(' ').map { |i| i.to_i }\ningredientNum, magicPowderNum = input[0], input[1]\n\ninput = gets.chomp.split(' ').map { |i| i.to_i }\nneeded = []\n\ninput.each do |item|\n\tneeded << item\nend\n\ninput = gets.chomp.split(' ').map { |i| i.to_i }\nhave = []\n\ninput.each do |item|\n\thave << item\nend\n\nnowMade = []\n\nfor i in (0...ingredientNum)\n\tnowMade << ((have[i] / needed[i]).floor)\nend\n\n=begin\nnowMade.each do |i|\n\tputs \"nowMade : #{i}\"\nend\n=end\n\nmin = 0\n\nuntil magicPowderNum <= 0\n\tmin = nowMade.sort[0]\n#\tputs \"min : #{min}\"\n\n\tidx = nowMade.find_index { |i| i == min }\n\n#\tputs \"idx : #{idx}\"\n\tsubVal = (needed[idx] - (have[idx] % needed[idx]))\n\tmagicPowderNum -= subVal\n\n\tbreak if magicPowderNum < 0\n\thave[idx] += subVal\n\tnowMade[idx] += 1\n\n#\tputs \"Remained : #{magicPowderNum}\"\nend\n\nmin = nowMade.sort[0]\nputs min"}, {"source_code": "parse_int = lambda { gets.split.map{|x| x.to_i} }\n\nn, magic = parse_int.call\nfor_one = parse_int.call\nhavin = parse_int.call\n\ncookies = 0\n\nwhile true\n\n #puts havin.to_s\n #puts magic\n #puts\n\n n.times{ |_|\n havin[_] -= for_one[_]\n if havin[_] < 0\n magic += havin[_]\n havin[_] = 0\n end\n }\n break if magic < 0\n cookies += 1\nend\n\nprint cookies"}], "negative_code": [], "src_uid": "02bb7502135afa0f3bb26527427ba9e3"} {"nl": {"description": "You are given integer $$$n$$$. You have to arrange numbers from $$$1$$$ to $$$2n$$$, using each of them exactly once, on the circle, so that the following condition would be satisfied:For every $$$n$$$ consecutive numbers on the circle write their sum on the blackboard. Then any two of written on the blackboard $$$2n$$$ numbers differ not more than by $$$1$$$.For example, choose $$$n = 3$$$. On the left you can see an example of a valid arrangement: $$$1 + 4 + 5 = 10$$$, $$$4 + 5 + 2 = 11$$$, $$$5 + 2 + 3 = 10$$$, $$$2 + 3 + 6 = 11$$$, $$$3 + 6 + 1 = 10$$$, $$$6 + 1 + 4 = 11$$$, any two numbers differ by at most $$$1$$$. On the right you can see an invalid arrangement: for example, $$$5 + 1 + 6 = 12$$$, and $$$3 + 2 + 4 = 9$$$, $$$9$$$ and $$$12$$$ differ more than by $$$1$$$. ", "input_spec": "The first and the only line contain one integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$).", "output_spec": "If there is no solution, output \"NO\" in the first line. If there is a solution, output \"YES\" in the first line. In the second line output $$$2n$$$ numbers — numbers from $$$1$$$ to $$$2n$$$ in the order they will stay in the circle. Each number should appear only once. If there are several solutions, you can output any of them.", "sample_inputs": ["3", "4"], "sample_outputs": ["YES\n1 4 5 2 3 6", "NO"], "notes": "NoteExample from the statement is shown for the first example. It can be proved that there is no solution in the second example."}, "positive_code": [{"source_code": "n = gets.to_i\nif n%2 == 0\n puts 'NO'\nelse\n puts 'YES'\n a = []\n (1..n).each do |x|\n if x%2 != 0\n a[x - 1] = 2*x - 1\n a[x - 1 + n] = 2*x\n else\n a[x - 1] = 2*x\n a[x - 1 + n] = 2*x - 1\n end\n end\n a.each {|x| print x, ' '}\nend\n"}, {"source_code": "n = gets.to_i\nif n%2==0\n puts \"NO\"\n exit\nend\nans = []\n\n# 以下で1から順番に入れる。\n#1から反対側に飛ぶ。{右隣に1つ移動。反対側に飛ぶ}を繰り返す\nans[0] = 1\nside_a = 0\nside_b = n\n2.step(n*2,2).with_index do |num, i|\n if i%2==0\n ans[side_b + i] = num\n ans[side_b+1 + i] = num+1\n else\n ans[side_a + i]= num\n ans[side_a+1 + i]= num+1\n end\nend\nputs \"YES\\n#{ans[0..(2*n-1)].join(' ')}\""}], "negative_code": [], "src_uid": "5e23f02afc7446ecf8688ad2a8fa284d"} {"nl": {"description": "You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \\dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \\ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \\le B$$$ for all $$$i$$$ and $$$\\sum\\limits_{i=0}^{n}{a_i}$$$ is maximum possible.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$; $$$1 \\le B, x, y \\le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, print one integer — the maximum possible $$$\\sum\\limits_{i=0}^{n}{a_i}$$$.", "sample_inputs": ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"], "sample_outputs": ["15\n4000000000\n-10"], "notes": "NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n n, b, x, y = gets.split.map(&:to_i)\r\n a, sum = 0, 0\r\n n.times do\r\n if a + x <= b\r\n a += x\r\n else\r\n a -= y\r\n end\r\n sum += a\r\n end\r\n p sum\r\nend\r\n"}, {"source_code": "gets.to_i.times do\r\n n, b, x, y = gets.split.map(&:to_i)\r\n a, sum = 0, 0\r\n n.times do\r\n if a + x <= b && a + x - y <= b\r\n a += x\r\n else\r\n a -= y\r\n end\r\n sum += a\r\n end\r\n p sum\r\nend\r\n"}, {"source_code": "main = -> {\n t = int\n t.times do\n n, b, x, y = ints\n sum = 0\n cur = 0\n n.times do\n if cur + x <= b\n cur += x\n else\n cur -= y\n end\n sum += cur\n end\n puts sum\n end\n}\n\nDEBUG = true\nMOD = 10**9+7\n$yesno = %w(No Yes)\nYesNo = %w(No Yes)\nYESNO = %w(NO YES)\nINF = 10**18\n\nrequire \"set\"\nrequire \"prime\"\n\ndef int; gets.to_s.to_i end\ndef ints; gets.to_s.split.map { |s| s.to_i } end\ndef int1s; gets.to_s.split.map { |s| s.to_i - 1 } end\ndef float; gets.to_s.to_f end\ndef floats; gets.to_s.split.map { |s| s.to_f } end\ndef array_of(&convert); gets.to_s.split.map(&convert) end\ndef string; gets.to_s.chomp end\ndef strings; gets.to_s.split end\ndef rep(n, &b); Array.new(n, &b) end\ndef yes; puts $yesno[1] end\ndef no; puts $yesno[0] end\ndef yesno t; puts $yesno[t] end\ndef YesNo t; puts YesNo[t] end\ndef YESNO t; puts YESNO[t] end\ndef zip(xs, *yss); Enumerator.new { |y| xs.zip(*yss) { |a| y.yield(*a) } } end\ndef max(*xs, &block); block_given? ? xs.max_by(&block) : xs.max end\ndef min(*xs, &block); block_given? ? xs.min_by(&block) : xs.min end\ndef minmax(*xs, &block); block_given? ? xs.minmax_by(&block) : xs.minmax end\ndef gcd(*xs); xs.inject(0, :gcd) end\ndef matrix(h, w, fill=nil, &block); return Array.new(h) { Array.new(w, &block) } if block_given?; Array.new(h) { [fill] * w } end\ndef debug(x = nil); STDERR.puts (block_given? ? yield(x) : x).inspect if DEBUG; x end\ndef debug_grid(grid, width = 1); grid.each { |row| STDERR.puts row.map { |x| x.inspect.ljust(width) }.join(\"\") } if DEBUG; grid end\ndef if_debug; yield if DEBUG end\n\nmodule Boolean\n def coerce(other); [other, to_i] end\n def +@; to_i end\n def to_int; to_i end\n def *(other); to_i * other end\nend\n\nclass TrueClass\n include Boolean\n def to_i; 1 end\nend\n\nclass FalseClass\n include Boolean\n def to_i; 0 end\nend\n\nclass Integer\n def div_ceil(y); (self + y - 1) / y end\n def mod_inv(mod = MOD); pow(mod-2, mod) end\n def mod_div(y, mod = MOD); self * y.mod_inv(mod) % mod end\n def mod_nCr(r, mod = MOD); x = y = 1; (1..r).each { |i| x = x * (self + 1 - i) % mod; y = y * i % mod }; x.mod_div(y, mod); end\n def factorial(mod = MOD); (2..self).inject(1) { |f, x| f * x % mod } end\n def popcount; x = self; c = 0; while x > 0; c += 1 if x & 1 == 1; x >>= 1 end; c end #TODO: faster\n def bitbrute(&block); (1< 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\n def lower_bound; ac, wa = self.end_open, self.begin; while ac - wa > 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\n def shakutori(r2, &pred); Enumerator.new { |y| j, r = r2.begin, r2.end_open; each { |i| j += 1 while j + 1 < r and pred[i, j+1]; y.yield(i, j) } }; end\n def widest(&block); Enumerator.new { |y| j, n = self.begin, self.end_open; each { |i| j += 1 while j < n and block[i, j]; y.yield(i, j) if block[i, j] } } end\nend\n\nclass Array\n def power(&block); (0 ... 1 << size).each(&block) end\n def sorted_merge(other); a = []; i = j = 0; n, m = size, other.size; if j < m and other[j] < self[i]; a << other[j]; j += 1 else; a << self[i]; i += 1 end while i < n; a.push(*other[j..-1]) if j < m; a end\n def upper_bound; ac, wa = 0, size; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\n def lower_bound; ac, wa = size, 0; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\n def cum(*xs, &op); a = []; a << xs[0] if xs.size > 0; a << x = self[0]; (1...size).each { |i| a << x = op[x, self[i]] }; a end\n def cumdiff(range); self[range.end_open] - self[range.begin]; end\n def compress(kinds = uniq.sort!); map { |x| kinds.bsearch_index { _1 >= x } }; end\nend\n\nmodule Enumerable\n def sorted_uniq; x = nil; filter { |y| c = x === y; x = y; !c } end\n def cumsum; ys = [0]; each { |x| ys << x + ys[-1] }; ys end\nend\n\nmain[]"}], "negative_code": [], "src_uid": "2c921093abf2c5963f5f0e96cd430456"} {"nl": {"description": "Pasha and Akim were making a forest map — the lawns were the graph's vertexes and the roads joining the lawns were its edges. They decided to encode the number of laughy mushrooms on every lawn in the following way: on every edge between two lawns they wrote two numbers, the greatest common divisor (GCD) and the least common multiple (LCM) of the number of mushrooms on these lawns. But one day Pasha and Akim had an argument about the laughy mushrooms and tore the map. Pasha was left with just some part of it, containing only m roads. Your task is to help Pasha — use the map he has to restore the number of mushrooms on every lawn. As the result is not necessarily unique, help Pasha to restore any one or report that such arrangement of mushrooms does not exist. It is guaranteed that the numbers on the roads on the initial map were no less that 1 and did not exceed 106.", "input_spec": "The first line contains two numbers n and m () which are the numbers of lawns and roads we know about. Each of the following m lines contains four numbers which are the numbers of lawns the road connects, the GCD and the LCM of the numbers of mushrooms on these lawns (1 ≤ GCD, LCM ≤ 106). It is guaranteed, that no road connects lawn to itself, and no two lawns are connected by more than one road.", "output_spec": "The answer should contain \"YES\" or \"NO\" on the first line, saying whether it is possible or not to perform the arrangement. If the answer is \"YES\", print on the following line n numbers which are the numbers of mushrooms on the corresponding lawns.", "sample_inputs": ["1 0", "2 1\n1 2 1 3", "3 2\n3 2 1 2\n3 1 1 10", "2 1\n1 2 3 7"], "sample_outputs": ["YES\n1", "YES\n1 3", "YES\n5 1 2", "NO"], "notes": null}, "positive_code": [{"source_code": "def candidates(a, b)\n return nil if b%a != 0\n ret = []\n i = a\n while true\n if b%i == 0 \n ret << i\n end\n i += a\n break if i > b\n end\n\n ret.sort.uniq\nend\n\ndef do_assign(rules, idx, v, assign)\n assign[idx] = v\n rules.each do |i,j,a,b|\n if i == idx\n return nil if a*b%v != 0\n o = a*b/v\n return nil if v.gcd(o) != a\n assign[j] ||= o\n return nil if assign[j] != o\n end\n\n if j == idx\n return nil if a*b%v != 0\n o = a*b/v\n return nil if v.gcd(o) != a\n assign[i] ||= o\n return nil if assign[i] != o\n end\n end\n\n return true\nend\n\ndef do_it(rules, cd, i, assign={})\n return assign if i > cd.keys.max \n\n i += 1 until cd[i]\n\n cd[i].each do |a|\n assign2 = assign.dup\n ret = do_assign(rules, i, a, assign2)\n if ret\n ret2 = do_it(rules, cd, i+1, assign2)\n return ret2 if ret2\n end\n end\n\n return nil\nend\n\ndef hoge(data)\n return {} if data.empty?\n\n gg = {}\n ll = {}\n\n data.each do |i,j,a,b|\n ll[i] ||= b\n ll[j] ||= b\n\n ll[i] = ll[i].gcd(b)\n ll[j] = ll[j].gcd(b)\n\n gg[i] ||= a\n gg[j] ||= a\n\n gg[i] = gg[i]*a/gg[i].gcd(a)\n gg[j] = gg[j]*a/gg[j].gcd(a)\n end\n\n cd = {}\n gg.keys.each do |i|\n cd[i] = candidates(gg[i], ll[i])\n return nil unless cd[i]\n end\n\n ret = do_it(data, cd, 0, {})\nend\n\nn, m = gets.split.map(&:to_i)\ndata = []\nm.times do\n data << gets.split.map(&:to_i)\nend\n\nret = hoge(data)\nif ret\n puts \"YES\"\n o = []\n 1.upto(n){|i| o << (ret[i] || 1)}\n\n puts o*\" \"\nelse\n puts \"NO\"\nend\n"}], "negative_code": [{"source_code": "def candidates(a, b)\n return nil if b%a != 0\n ret = []\n i = a\n while true\n if b%i == 0 \n ret << i\n end\n i += a\n break if i > b\n end\n\n ret.sort.uniq\nend\n\ndef do_assign(rules, idx, v, assign)\n assign[idx] = v\n rules.each do |i,j,a,b|\n if i == idx\n return nil if a*b%v != 0\n o = a*b/v\n assign[j] ||= o\n return nil if assign[j] != o\n end\n\n if j == idx\n return nil if a*b%v != 0\n o = a*b/v\n assign[i] ||= o\n return nil if assign[i] != o\n end\n end\n\n return true\nend\n\ndef do_it(rules, cd, i, assign={})\n return assign if i > cd.keys.max \n\n i += 1 until cd[i]\n\n cd[i].each do |a|\n assign2 = assign.dup\n ret = do_assign(rules, i, a, assign2)\n if ret\n ret2 = do_it(rules, cd, i+1, assign2)\n return ret2 if ret2\n end\n end\n\n return nil\nend\n\ndef hoge(data)\n return {} if data.empty?\n\n gg = {}\n ll = {}\n\n data.each do |i,j,a,b|\n ll[i] ||= b\n ll[j] ||= b\n\n ll[i] = ll[i].gcd(b)\n ll[j] = ll[j].gcd(b)\n\n gg[i] ||= a\n gg[j] ||= a\n\n gg[i] = gg[i]*a/gg[i].gcd(a)\n gg[j] = gg[j]*a/gg[j].gcd(a)\n end\n\n cd = {}\n gg.keys.each do |i|\n cd[i] = candidates(gg[i], ll[i])\n return nil unless cd[i]\n end\n\n ret = do_it(data, cd, 0, {})\nend\n\nn, m = gets.split.map(&:to_i)\ndata = []\nm.times do\n data << gets.split.map(&:to_i)\nend\n\nret = hoge(data)\nif ret\n puts \"YES\"\n o = []\n 1.upto(n){|i| o << (ret[i] || 1)}\n\n puts o*\" \"\nelse\n puts \"NO\"\nend\n"}, {"source_code": "$cache = {}\ndef pairs(a, b)\n d = b/a\n unless $cache[d]\n r = []\n 1.upto(d/2) do |k|\n if d%k == 0\n r << [k, d/k]\n end\n end\n\n $cache[d] = r\n end\n\n $cache[d].map{|x,y| [x*a, y*a]}\nend\n\ndef hoge(data,o=[])\n return o if data.empty?\n\n i, j, a, b = data[0]\n\n return nil if b%a != 0\n\n pairs(a, b).each do |x, y|\n o2 = o.dup\n o2[i] ||= x\n o2[j] ||= y\n\n data.each do |vv|\n if vv[0] == i\n return nil if vv[3]%x != 0\n o2[vv[1]] ||= vv[3]/x\n return nil if o2[vv[1]] != vv[3]/x\n end\n if vv[1] == i\n return nil if vv[3]%x != 0\n o2[vv[0]] ||= vv[3]/x\n return nil if o2[vv[0]] != vv[3]/x\n end\n\n if vv[0] == j\n return nil if vv[3]%y != 0\n o2[vv[1]] ||= vv[3]/y\n return nil if o2[vv[1]] != vv[3]/y\n end\n if vv[1] == j\n return nil if vv[3]%y != 0\n o2[vv[0]] ||= vv[3]/y\n return nil if o2[vv[0]] != vv[3]/y\n end\n end\n\n if o2[i] == x && o2[j] == y\n ret = hoge(data[1..-1], o2)\n return ret if ret\n end\n end\n\n return nil\nend\n\nn, m = gets.split.map(&:to_i)\ndata = []\nm.times do\n data << gets.split.map(&:to_i)\nend\n\n\nret = hoge(data)\nif ret\n puts \"YES\"\n 1.upto(n){|i|ret[i] ||= 1}\n\n puts ret[1..-1]*\" \"\nelse\n puts \"NO\"\nend\n"}, {"source_code": "$cache = {}\ndef pairs(a, b)\n d = b/a\n unless $cache[d]\n r = []\n 1.upto(d) do |k|\n if d%k == 0\n r << [k, d/k]\n end\n end\n\n $cache[d] = r\n end\n\n $cache[d].map{|x,y| [x*a, y*a]}\nend\n\ndef hoge(data,o=[])\n return o if data.empty?\n\n i, j, a, b = data[0]\n\n return nil if b%a != 0\n\n pairs(a, b).each do |x, y|\n o2 = o.dup\n o2[i] ||= x\n o2[j] ||= y\n\n data.each do |vv|\n vi, vj, va, vb = vv\n\n if vi == i\n return nil if vb%x != 0\n o2[vj] ||= vb*va/x\n return nil if o2[vj] != vb*va/x\n end\n\n if vj == i\n return nil if vb%x != 0\n o2[vi] ||= vb*va/x\n return nil if o2[vi] != vb*va/x\n end\n\n if vi == j\n return nil if vb%y != 0\n o2[vj] ||= vb*va/y\n return nil if o2[vj] != vb*va/y\n end\n\n if vj == j\n return nil if vb%y != 0\n o2[vi] ||= vb*va/y\n return nil if o2[vi] != vb*va/y\n end\n end\n\n if o2[i] == x && o2[j] == y\n ret = hoge(data[1..-1], o2)\n return ret if ret\n end\n end\n\n return nil\nend\n\nn, m = gets.split.map(&:to_i)\ndata = []\nm.times do\n data << gets.split.map(&:to_i)\nend\n\nret = hoge(data)\nif ret\n puts \"YES\"\n 1.upto(n){|i|ret[i] ||= 1}\n\n puts ret[1..-1]*\" \"\nelse\n puts \"NO\"\nend\n"}], "src_uid": "05f33454f70551bce8ffa765d4a8f8e1"} {"nl": {"description": "You have a string $$$s$$$ consisting of $$$n$$$ characters. Each character is either 0 or 1.You can perform operations on the string. Each operation consists of two steps: select an integer $$$i$$$ from $$$1$$$ to the length of the string $$$s$$$, then delete the character $$$s_i$$$ (the string length gets reduced by $$$1$$$, the indices of characters to the right of the deleted one also get reduced by $$$1$$$); if the string $$$s$$$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed.For example, if you have a string $$$s =$$$ 111010, the first operation can be one of the following: select $$$i = 1$$$: we'll get 111010 $$$\\rightarrow$$$ 11010 $$$\\rightarrow$$$ 010; select $$$i = 2$$$: we'll get 111010 $$$\\rightarrow$$$ 11010 $$$\\rightarrow$$$ 010; select $$$i = 3$$$: we'll get 111010 $$$\\rightarrow$$$ 11010 $$$\\rightarrow$$$ 010; select $$$i = 4$$$: we'll get 111010 $$$\\rightarrow$$$ 11110 $$$\\rightarrow$$$ 0; select $$$i = 5$$$: we'll get 111010 $$$\\rightarrow$$$ 11100 $$$\\rightarrow$$$ 00; select $$$i = 6$$$: we'll get 111010 $$$\\rightarrow$$$ 11101 $$$\\rightarrow$$$ 01. You finish performing operations when the string $$$s$$$ becomes empty. What is the maximum number of operations you can perform?", "input_spec": "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 a single integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) — the length of the string $$$s$$$. The second line contains string $$$s$$$ of $$$n$$$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $$$n$$$ over test cases doesn't exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, print a single integer — the maximum number of operations you can perform.", "sample_inputs": ["5\n6\n111010\n1\n0\n1\n1\n2\n11\n6\n101010"], "sample_outputs": ["3\n1\n1\n1\n3"], "notes": "NoteIn the first test case, you can, for example, select $$$i = 2$$$ and get string 010 after the first operation. After that, you can select $$$i = 3$$$ and get string 1. Finally, you can only select $$$i = 1$$$ and get empty string."}, "positive_code": [{"source_code": "T = gets.to_i\n\nT.times do\n n = gets.to_i\n s = gets.chomp\n stack = []\n cur = ''\n len = 0\n\n s.chars.reverse_each do |ch|\n if cur == ''\n cur = ch\n len = 1\n else\n if cur == ch\n len += 1\n else\n stack << len\n\n cur = ch\n len = 1\n end\n end\n end\n\n stack << len if len > 0\n ans = 0\n idx = -1\n\n until stack.empty?\n while stack[idx] && stack[idx] == 1\n idx -= 1\n end\n\n if stack[idx].nil?\n ans += (stack.size + 1) / 2\n break\n else\n stack[idx] -= 1\n end\n\n ans += 1\n stack.pop\n idx += 1 if idx < -1\n end\n\n puts ans\nend\n"}], "negative_code": [], "src_uid": "d0030996e6b29c8580463fae43bb04d4"} {"nl": {"description": "The BFS algorithm is defined as follows. Consider an undirected graph with vertices numbered from $$$1$$$ to $$$n$$$. Initialize $$$q$$$ as a new queue containing only vertex $$$1$$$, mark the vertex $$$1$$$ as used. Extract a vertex $$$v$$$ from the head of the queue $$$q$$$. Print the index of vertex $$$v$$$. Iterate in arbitrary order through all such vertices $$$u$$$ that $$$u$$$ is a neighbor of $$$v$$$ and is not marked yet as used. Mark the vertex $$$u$$$ as used and insert it into the tail of the queue $$$q$$$. If the queue is not empty, continue from step 2. Otherwise finish. Since the order of choosing neighbors of each vertex can vary, it turns out that there may be multiple sequences which BFS can print.In this problem you need to check whether a given sequence corresponds to some valid BFS traversal of the given tree starting from vertex $$$1$$$. The tree is an undirected graph, such that there is exactly one simple path between any two vertices.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) which denotes the number of nodes in the tree. The following $$$n - 1$$$ lines describe the edges of the tree. Each of them contains two integers $$$x$$$ and $$$y$$$ ($$$1 \\le x, y \\le n$$$) — the endpoints of the corresponding edge of the tree. It is guaranteed that the given graph is a tree. The last line contains $$$n$$$ distinct integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le n$$$) — the sequence to check.", "output_spec": "Print \"Yes\" (quotes for clarity) if the sequence corresponds to some valid BFS traversal of the given tree and \"No\" (quotes for clarity) otherwise. You can print each letter in any case (upper or lower).", "sample_inputs": ["4\n1 2\n1 3\n2 4\n1 2 3 4", "4\n1 2\n1 3\n2 4\n1 2 4 3"], "sample_outputs": ["Yes", "No"], "notes": "NoteBoth sample tests have the same tree in them.In this tree, there are two valid BFS orderings: $$$1, 2, 3, 4$$$, $$$1, 3, 2, 4$$$. The ordering $$$1, 2, 4, 3$$$ doesn't correspond to any valid BFS order."}, "positive_code": [{"source_code": "def f \n n = gets.to_i\n\n ri = Hash.new {|k,v| k[v]=[]}\n\n (1...n).each do \n a = gets.split.map(&:to_i)\n ri[a[0]] << a[1]\n ri[a[1]] << a[0]\n end\n\n cmp = gets.split.map(&:to_i)\n\n a = Array.new(10,0)\n id = Array.new(10,0)\n\n (1..n).each do |i|\n a[i] = cmp[i-1]\n id[cmp[i-1]] = i\n end\n # ri = ri.sort.to_h\n # p ri\n # p a\n # p id\n q = [1]\n l = 0\n # p a\n while q.length > 0\n # p q\n x = q[0]\n q.shift\n # p q\n \n l += 1\n # print \" al x #{a[l]} #{x}\\n\"\n if (x != a[l])\n return \"No\" \n end \n\n # pp ri\n ri[x].sort! { |a,b| id[a] <=> id[b] }\n # pp ri\n\n ri[x].each do |y|\n # print \"#{id[y]} #{id[x]}\\n\"\n q << y if id[y] > id[x]\n end \n end\n return \"Yes\"\nend \n\nprint f"}], "negative_code": [{"source_code": "def f \n n = gets.to_i\n\n ri = Hash.new {|k,v| k[v]=[]}\n\n (1...n).each do \n a = gets.split.map(&:to_i)\n ri[a[0]] << a[1]\n ri[a[1]] << a[0]\n end\n\n cmp = gets.split.map(&:to_i)\n\n a = Array.new(10,0)\n id = Array.new(10,0)\n\n (1..n).each do |i|\n a[i] = i\n id[cmp[i-1]] = i\n end\n\n # p id\n # p a\n q = [1]\n l = 0\n # p a\n while q.length > 0\n # p q\n x = q[0]\n q.shift\n # p q\n \n l += 1\n # print \" al x #{a[l]} #{x}\\n\"\n if (x != a[l])\n return \"No\" \n end \n\n # ri[x].sort! { |a,b| id[a] <=> id[b] }\n # p ri[x]\n\n ri[x].each do |y|\n # print \"#{id[y]} #{id[x]}\\n\"\n if id[y] > id[x]\n q.push(id[y])\n end \n end \n end\n return \"Yes\"\nend \n\nprint f"}, {"source_code": "def f \n n = gets.to_i\n\n h = Hash.new {|k,v| k[v]=[]}\n\n (n-1).times do \n a = gets.split.map(&:to_i)\n h[a[0]] << a[1]\n h[a[1]] << a[0]\n end\n\n cmp = gets.split.map(&:to_i)\n\n id = Array.new(10,0)\n\n (1..n).each do |i|\n id[cmp[i-1]] = i\n end\n\n q = []\n q << 1\n l = 0\n \n while q.length > 0\n x = q[0]\n q.pop\n \n return \"No\" if (x != cmp[l])\n l += 1\n \n h[x].sort! { |a,b| id[a] <=> id[b] }\n\n p h[x]\n\n h[x].each do |y|\n q << y if id[y] > id[x]\n end \n end\n return \"Yes\"\nend \n\nprint f"}, {"source_code": "N = gets.to_i\n\nh = Hash.new {|k,v| k[v]=Array.new}\n\n(N-1).times do \n a = gets.split\n h[a[0]].push(a[1])\n h[a[1]].push(a[0])\nend\n\ncmp = gets.split\n\nindex = 1\n\ncmp.each do |i|\n while h[i].include? cmp[index]\n index += 1 \n end\n break if !h[i].include? cmp[index]\nend\n\nprint index == cmp.length ? \"Yes\" : \"No\""}, {"source_code": "def f \n n = gets.to_i\n\n ri = Hash.new {|k,v| k[v]=[]}\n\n (1...n).each do \n a = gets.split.map(&:to_i)\n ri[a[0]] << a[1]\n ri[a[1]] << a[0]\n end\n\n cmp = gets.split.map(&:to_i)\n\n a = Array.new(10,0)\n id = Array.new(10,0)\n\n (1..n).each do |i|\n a[i] = i\n id[cmp[i-1]] = i\n end\n\n\n q = [1]\n l = 0\n\n while q.length > 0\n x = q[0]\n q.shift\n \n l += 1\n\n if (x != a[l])\n return \"No\" \n end \n\n ri[x].sort! { |a,b| id[a] <=> id[b] }\n\n ri[x].each do |y|\n if id[y] > id[x]\n q << y\n end \n end \n end\n return \"Yes\"\nend \n\nprint f"}, {"source_code": "class Graph \n attr_accessor :graph\n\n def initialize(size)\n @graph = Hash.new { |h, k| h[k] = Array.new }\n end \n\n def <<(x)\n @graph[x[0]] << x[1]\n end\n\n def bfs(v)\n chain = []\n visited = Array.new(@graph.length, false)\n visited[v] = true\n\n queue = Queue.new\n queue.enq(v)\n \n while queue.length > 0\n v = queue.deq\n chain << v\n\n @graph[v].each do |i|\n if !visited[i] \n queue.enq(i) \n visited[i] = true\n end \n end \n end\n chain.join\n end \nend \n\nn = gets.to_i\n\ng = Graph.new(n)\n\n(n-1).times do \n v, w = gets.split.map(&:to_i)\n g << [v,w]\nend \n\ncmp = gets.split.join\n\nfound = 0\n\nn.times do |i|\n if cmp == g.bfs(i)\n found = 1\n break\n end\nend \n\nprint found == 1 ? \"Yes\" : \"No\"\n"}, {"source_code": "N = gets.to_i\n\nh = Hash.new {|k,v| k[v]=Array.new}\n\n(N-1).times do \n a = gets.split\n h[a[0]] << a[1]\nend\n\ncmp = gets.split\n\nqueue = h[cmp[0]]\n\nindex = 1\n\ncmp.each do |i|\n if queue.include? cmp[index]\n queue.delete(cmp[index])\n queue += h[cmp[index]]\n index += 1 \n end\nend\n\nprint index == cmp.length ? \"Yes\" : \"No\""}, {"source_code": "N = gets.to_i\n\nh = Hash.new {|k,v| k[v]=Array.new}\n\n(N-1).times do \n a = gets.split\n h[a[0]] << a[1]\nend\n\ncmp = gets.split\n\nindex = 1\n\ncmp.each do |i|\n while h[i].include? cmp[index]\n index += 1 \n end\nend\n\nprint index == cmp.length ? \"Yes\" : \"No\""}], "src_uid": "82ff8ae62e39b8e64f9a273b736bf59e"} {"nl": {"description": "You are given a positive integer $$$x$$$. Find any such $$$2$$$ positive integers $$$a$$$ and $$$b$$$ such that $$$GCD(a,b)+LCM(a,b)=x$$$.As a reminder, $$$GCD(a,b)$$$ is the greatest integer that divides both $$$a$$$ and $$$b$$$. Similarly, $$$LCM(a,b)$$$ is the smallest integer such that both $$$a$$$ and $$$b$$$ divide it.It's guaranteed that the solution always exists. If there are several such pairs $$$(a, b)$$$, you can output any of them.", "input_spec": "The first line contains a single integer $$$t$$$ $$$(1 \\le t \\le 100)$$$  — the number of testcases. Each testcase consists of one line containing a single integer, $$$x$$$ $$$(2 \\le x \\le 10^9)$$$.", "output_spec": "For each testcase, output a pair of positive integers $$$a$$$ and $$$b$$$ ($$$1 \\le a, b \\le 10^9)$$$ such that $$$GCD(a,b)+LCM(a,b)=x$$$. It's guaranteed that the solution always exists. If there are several such pairs $$$(a, b)$$$, you can output any of them.", "sample_inputs": ["2\n2\n14"], "sample_outputs": ["1 1\n6 4"], "notes": "NoteIn the first testcase of the sample, $$$GCD(1,1)+LCM(1,1)=1+1=2$$$.In the second testcase of the sample, $$$GCD(6,4)+LCM(6,4)=2+12=14$$$."}, "positive_code": [{"source_code": "gets.to_i.times do\n\tx = gets.to_i\n\tputs [1, x-1].join(' ')\nend"}, {"source_code": "t = gets.to_i\nt.times do\n x = gets.to_i\n puts [1, x-1].join(\" \")\nend\n"}, {"source_code": "#! /usr/bin/env ruby\n# encoding: UTF-8\n\n=begin\na = p q\nb = q r\n\ngcd = q\nlcm = pqr\nq(1+pr)\n\n若偶数,直接输出 a=b=x/2\n若奇数 > 1,必然有 1+pr > 1\n若奇数 = 1,无解(题目保证>=2)\n\n简化:令q === 1,则1+pr总是>1\n=end\n\ndef solve\n\tn = gets.to_i\n\t#q = 1\n\t#p = 1\n\t#r = n-1 # ((n/q)-1 ) / p\n\tputs \"#{1} #{n-1}\"\nend\n\ngets.to_i.times{ solve }\n\n\n"}, {"source_code": "require 'set'; require 'prime'\nINF=Float::INFINITY; MOD=10**9+7\nt = gets.chomp.to_i\n\nt.times do\n x = gets.chomp.to_i\n puts \"#{x-1} #{1}\"\nend\n"}, {"source_code": "T = gets.to_i\n\nT.times do\n x = gets.to_i\n\n if x > 2 && x % 2 == 0\n puts \"2 #{x - 2}\"\n else\n puts \"1 #{x - 1}\"\n end\nend\n"}, {"source_code": "# cook your code here\n\nn = gets.to_i;\n\n(0..n-1).each do |i|\n x = gets.to_i;\n p 1, x-1;\nend\n \n \n"}, {"source_code": "1.upto(gets.to_i) do\n puts \"1 #{gets.to_i - 1}\"\nend\n"}, {"source_code": "t = gets.chomp.to_i\n(0...t).each do |_|\n x = gets.chomp.to_i\n print 1, ' ', x-1\n puts\nend\n\n"}], "negative_code": [{"source_code": "# cook your code here\n\nn = gets.to_i;\n\n(0..n).each do |i|\n x = gets.to_i;\n p 1, x-1;\nend\n \n \n"}, {"source_code": "# cook your code here\nx = gets.to_i;\np 1, x-1;"}], "src_uid": "2fa3e88688b92c27ad26a23884e26009"} {"nl": {"description": "Thumbelina has had an accident. She has found herself on a little island in the middle of a swamp and wants to get to the shore very much.One can get to the shore only by hills that are situated along a straight line that connects the little island with the shore. Let us assume that the hills are numbered from 1 to n and the number of a hill is equal to the distance in meters between it and the island. The distance between the n-th hill and the shore is also 1 meter.Thumbelina is too small to make such jumps. Fortunately, a family of frogs living in the swamp suggests to help her. Each frog agrees to give Thumbelina a ride but Thumbelina should choose only one frog. Each frog has a certain jump length. If Thumbelina agrees to accept help from a frog whose jump length is d, the frog will jump from the island on the hill d, then — on the hill 2d, then 3d and so on until they get to the shore (i.e. find itself beyond the hill n).However, there is one more problem: mosquitoes also live in the swamp. At the moment they have a siesta, and they are having a nap on some hills. If the frog jumps on a hill with a mosquito the frog will smash it. The frogs Thumbelina has met are pacifists, so they will find the death of each mosquito very much sad. Help Thumbelina choose a frog that will bring her to the shore and smash as small number of mosquitoes as possible.", "input_spec": "The first line contains three integers n, m and k (1 ≤ n ≤ 109, 1 ≤ m, k ≤ 100) — the number of hills, frogs and mosquitoes respectively. The second line contains m integers di (1 ≤ di ≤ 109) — the lengths of the frogs’ jumps. The third line contains k integers — the numbers of the hills on which each mosquito is sleeping. No more than one mosquito can sleep on each hill. The numbers in the lines are separated by single spaces.", "output_spec": "In the first line output the number of frogs that smash the minimal number of mosquitoes, in the second line — their numbers in increasing order separated by spaces. The frogs are numbered from 1 to m in the order of the jump length given in the input data.", "sample_inputs": ["5 3 5\n2 3 4\n1 2 3 4 5", "1000000000 2 3\n2 5\n999999995 999999998 999999996"], "sample_outputs": ["2\n2 3", "1\n2"], "notes": null}, "positive_code": [{"source_code": "n,m,k = gets.chomp.split(/ /).map!{|x|x.to_i}\nma = gets.chomp.split(/ /).map!{|x|x.to_i}\nka = gets.chomp.split(/ /).map!{|x|x.to_i}\nsummin = k+1\nmas = Array.new\nfor i in 0..m-1\n\tsum = 0\n\tfor j in 0..k-1\n\t\tsum += 1 if ka[j] % ma[i] == 0\n\tend\n\tif summin > sum\n\t\tmas.clear\n\t\tmas.push(i+1)\n\t\tsummin = sum\n\telsif summin == sum\n\t\tmas.push(i+1)\n\tend\nend\nputs mas.length\nputs mas.join(' ')"}, {"source_code": "gets\na, b = 2.times.map{gets.split.map(&:to_i)}\nc = a.each_with_index.map{|j, i| [b.count{|k| k % j == 0}, i + 1]}\nc.reject!{|j, i| j != c.min[0]}\np c.size\nputs c.map(&:last) * ' '\n"}, {"source_code": "gets\na, b = 2.times.map{gets.split.map(&:to_i)}\nc = a.each_with_index.map{|j, i| [b.count{|k| k % j == 0}, i + 1]}\nc.reject!{|j, i| j != c.min[0]}\np c.size\nputs c.map(&:last) * ' '\n\n"}, {"source_code": "gets\na, b = 2.times.map{gets.split.map(&:to_i)}\nc = a.each_with_index.map{|j, i| [b.count{|k| k % j == 0}, i + 1]}\nc.reject!{|j, i| j != c.min[0]}\np c.size\nputs c.map(&:last) * ' '\n\n"}, {"source_code": "gets\na, b = 2.times.map{gets.split.map(&:to_i)}\nc = a.each_with_index.map{|j, i| [b.count{|k| k % j == 0}, i + 1]}\nc.reject!{|j, i| j != c.min[0]}\np c.size\nputs c.map(&:last) * ' '\n\n"}, {"source_code": "gets\na, b = 2.times.map{gets.split.map(&:to_i)}\nc = a.each_with_index.map{|j, i| [b.count{|k| k % j == 0}, i + 1]}\nc.reject!{|j, i| j != c.min[0]}\np c.size\nputs c.map(&:last) * ' '\n\n"}, {"source_code": "gets\na, b = 2.times.map{gets.split.map(&:to_i)}\nc = a.each_with_index.map{|j, i| [b.count{|k| k % j == 0}, i + 1]}\nc.reject!{|j, i| j != c.min[0]}\np c.size\nputs c.map(&:last) * ' '"}, {"source_code": "gets\na, b = 2.times.map{gets.split.map(&:to_i)}\nc = a.each_with_index.map{|j, i| [b.count{|k| k % j == 0}, i + 1]}\nc.reject!{|j, i| j != c.min[0]}\np c.size\nputs c.map(&:last) * ' '\n\n"}, {"source_code": "gets\na, b = 2.times.map{gets.split.map(&:to_i)}\nc = a.each_with_index.map{|j, i| [b.count{|k| k % j == 0}, i + 1]}\nc.reject!{|j, i| j != c.min[0]}\np c.size\nputs c.map(&:last) * ' '\n\n"}, {"source_code": "gets\na, b = 2.times.map{gets.split.map(&:to_i)}\nc = a.each_with_index.map{|j, i| [b.count{|k| k % j == 0}, i + 1]}\nc.reject!{|j, i| j != c.min[0]}\np c.size\nputs c.map(&:last) * ' '\n\n"}, {"source_code": "gets\na, b = 2.times.map{gets.split.map(&:to_i)}\nc = a.each_with_index.map{|j, i| [b.count{|k| k % j == 0}, i + 1]}\nc.reject!{|j, i| j != c.min[0]}\np c.size\nputs c.map(&:last) * ' '\n\n"}], "negative_code": [{"source_code": "gets\na, b = 2.times.map{gets.split.map(&:to_i).sort}\nc = a.each_with_index.map{|j, i| [b.count{|k| k % j == 0}, i + 1]}\nc.reject!{|j, i| j != c.min[0]}\np c.size\nputs c.map(&:last) * ' '\n"}], "src_uid": "0701a595ee3a2b9edee53444b9dd1d51"} {"nl": {"description": "There are two popular keyboard layouts in Berland, they differ only in letters positions. All the other keys are the same. In Berland they use alphabet with 26 letters which coincides with English alphabet.You are given two strings consisting of 26 distinct letters each: all keys of the first and the second layouts in the same order. You are also given some text consisting of small and capital English letters and digits. It is known that it was typed in the first layout, but the writer intended to type it in the second layout. Print the text if the same keys were pressed in the second layout.Since all keys but letters are the same in both layouts, the capitalization of the letters should remain the same, as well as all other characters.", "input_spec": "The first line contains a string of length 26 consisting of distinct lowercase English letters. This is the first layout. The second line contains a string of length 26 consisting of distinct lowercase English letters. This is the second layout. The third line contains a non-empty string s consisting of lowercase and uppercase English letters and digits. This is the text typed in the first layout. The length of s does not exceed 1000.", "output_spec": "Print the text if the same keys were pressed in the second layout.", "sample_inputs": ["qwertyuiopasdfghjklzxcvbnm\nveamhjsgqocnrbfxdtwkylupzi\nTwccpQZAvb2017", "mnbvcxzlkjhgfdsapoiuytrewq\nasdfghjklqwertyuiopzxcvbnm\n7abaCABAABAcaba7"], "sample_outputs": ["HelloVKCup2017", "7uduGUDUUDUgudu7"], "notes": null}, "positive_code": [{"source_code": "layout1 = gets.chomp\nlayout2 = gets.chomp\ns = gets.chomp\n\nres = ''\ns.split(\"\").each do |c|\n i = layout1.index(c.downcase)\n unless i\n res += c\n else\n if c == c.upcase\n res += layout2[i].upcase\n else\n res += layout2[i]\n end\n end\nend\nputs res"}, {"source_code": "a,b,c=$<.map &:chomp\nputs c.tr(a+a.upcase,b+b.upcase)"}, {"source_code": "a,b,c=$<.map &:chomp\nputs c.tr(a+a.upcase,b+b.upcase)"}, {"source_code": "a,b,c=$<.map &:chomp\nputs c.tr(a+a.upcase,b+b.upcase)"}, {"source_code": "#!/usr/bin/ruby -w\n\nclass String\n\n def is_upper?\n not self.match /[[:lower:]]/\n end\n\n def is_lower?\n not self.match /[[:upper:]]/\n end\n\nend\n\nstr1 = gets\nstr2 = gets\nstrr = gets\n\nstr_siz = strr.size\nstr_siz.times do |i|\n idx = str1.index(strr[i].downcase)\n if idx != nil then\n if strr[i].is_upper? then\n strr[i] = str2[idx].capitalize\n else\n strr[i] = str2[idx]\n end\n end\nend\nputs strr\n"}, {"source_code": "fs = gets.chomp\nss = gets.chomp\nfsl = fs.upcase\nssl = ss.upcase\n\nans = ''\ngets.chomp.chars.each do |s|\n if id = fs.index(s)\n ans += ss[id]\n elsif id = fsl.index(s)\n ans += ssl[id]\n else\n ans += s\n end\nend\n\nputs ans"}, {"source_code": "f = gets.strip.split(\"\")\ns = gets.strip.split(\"\")\nt = gets.strip\nans = \"\"\nt.each_char do |c|\n id = f.index c.downcase\n if /[[:upper:]]/.match c\n ans += s[id].upcase\n elsif /[[:digit:]]/.match c\n ans += c\n else\n ans += s[id]\n end\nend\nputs ans\n"}, {"source_code": "str1=gets.chomp\nstr2=gets.chomp\n\nhs={}\nstr1.size.times{|i|\n\te1=str1[i]\n\te2=str2[i]\n\ths[e1]=e2\n\ths[e1.upcase]=e2.upcase\n}\nstr3=gets.chomp\nstr3.chars.each{|e|\n\tif hs.member?(e)==false\n\t\tprint e\n\telse\n\t\tprint hs[e]\n\tend\n}\nprint \"\\n\""}, {"source_code": "a = gets\nb = gets\nh = {}\n(0...26).each { |i| h[a[i]] = b[i] }\nputs gets.chomp('').split('').map { |x|\n c = h[x.downcase] || x\n x =~ /[A-Z]/ ? c.upcase : c\n}.join\n"}, {"source_code": "a = gets.chomp!\nb = gets.chomp!\nt = gets.chomp!\n\nh = Hash.new\n26.times do |i|\n\th[a[i]] = b[i]\nend\n\nf = \"\"\nt.chars do |c|\n\tif c == c.downcase\n\t\tf.insert(-1, h[c] || c)\n\telse\n\t\tf.insert(-1, h[c.downcase].upcase)\n\tend\nend\n\nputs f\n"}, {"source_code": "\nfirst=gets.chomp\nsecond=gets.chomp\nthird=gets.chomp\n \nthird.each_char do |x|\nif x=~/[[:digit:]]/\n print x\n next\nend\nindex = first.index(x.downcase)\n\nif x == x.upcase\n print second[index].upcase\nelse\n print second[index]\n end\nend"}, {"source_code": "a = gets\nb = gets\nh = {}\n(0...26).each { |i| h[a[i]] = b[i] }\nputs gets.chomp('').split('').map { |x|\n c = h[x.downcase] || x\n x =~ /[A-Z]/ ? c.upcase : c\n}.join\n"}, {"source_code": "k1 = gets.chomp\nk2 = gets.chomp\n\nmap = {}\nfor i in 0 ... 26\n map[k1[i]] = k2[i]\nend\n\ns = gets.chomp\nt = s.split('').map{|c|\n if c != c.downcase\n map[c.downcase].upcase\n else\n map[c] ? map[c] : c\n end\n}.join()\nputs t\n"}], "negative_code": [{"source_code": "a,b,c=$<.map &:chomp\nputs c.tr(b+b.upcase,a+a.upcase)"}, {"source_code": "#!/usr/bin/ruby -w\n\nclass String\n\n def is_upper?\n not self.match /[[:lower:]]/\n end\n\n def is_lower?\n not self.match /[[:upper:]]/\n end\n\nend\n\nputs 'Hello World'\n\nstr1 = gets\nstr2 = gets\nstrr = gets\n\nstr_siz = strr.size\nstr_siz.times do |i|\n idx = str1.index(strr[i].downcase)\n if idx != nil then\n if strr[i].is_upper? then\n strr[i] = str2[idx].capitalize\n else\n strr[i] = str2[idx]\n end\n end\nend\nputs strr\n"}], "src_uid": "d6f01ece3f251b7aac74bf3fd8231a80"} {"nl": {"description": "IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting.Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. \"It would look much better when I'll swap some of them!\" — thought the girl — \"but how to do it?\". After a while, she got an idea. IA will look at all prefixes with lengths from $$$1$$$ to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing?A string $$$a$$$ is lexicographically smaller than a string $$$b$$$ if and only if one of the following holds: $$$a$$$ is a prefix of $$$b$$$, but $$$a \\ne b$$$; in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$b$$$.", "input_spec": "The first and the only line contains a string $$$s$$$ ($$$1 \\le |s| \\le 1000$$$), describing the initial string formed by magnets. The string $$$s$$$ consists only of characters 'a' and 'b'.", "output_spec": "Output exactly $$$|s|$$$ integers. If IA should reverse the $$$i$$$-th prefix (that is, the substring from $$$1$$$ to $$$i$$$), the $$$i$$$-th integer should be equal to $$$1$$$, and it should be equal to $$$0$$$ otherwise. If there are multiple possible sequences leading to the optimal answer, print any of them.", "sample_inputs": ["bbab", "aaaaa"], "sample_outputs": ["0 1 1 0", "1 0 0 0 1"], "notes": "NoteIn the first example, IA can reverse the second and the third prefix and get a string \"abbb\". She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string.In the second example, she can reverse any subset of prefixes — all letters are 'a'."}, "positive_code": [{"source_code": "str = gets.strip\nn = str.size\nseq = Array.new(n,0)\n\n1.upto(n-1).each do |p|\n if str[p]=='a'\n if seq[p-1]==0\n seq[p-1]=1\n else\n seq[p-1]=0\n end\n seq[p]=1\n end\nend\n\nputs seq.join(\" \")"}], "negative_code": [], "src_uid": "fc0d3b122d800dcbcb99795581229d42"} {"nl": {"description": "Paw the Spider is making a web. Web-making is a real art, Paw has been learning to do it his whole life. Let's consider the structure of the web. There are n main threads going from the center of the web. All main threads are located in one plane and divide it into n equal infinite sectors. The sectors are indexed from 1 to n in the clockwise direction. Sectors i and i + 1 are adjacent for every i, 1 ≤ i < n. In addition, sectors 1 and n are also adjacent.Some sectors have bridge threads. Each bridge connects the two main threads that make up this sector. The points at which the bridge is attached to the main threads will be called attachment points. Both attachment points of a bridge are at the same distance from the center of the web. At each attachment point exactly one bridge is attached. The bridges are adjacent if they are in the same sector, and there are no other bridges between them.A cell of the web is a trapezoid, which is located in one of the sectors and is bounded by two main threads and two adjacent bridges. You can see that the sides of the cell may have the attachment points of bridges from adjacent sectors. If the number of attachment points on one side of the cell is not equal to the number of attachment points on the other side, it creates an imbalance of pulling forces on this cell and this may eventually destroy the entire web. We'll call such a cell unstable. The perfect web does not contain unstable cells.Unstable cells are marked red in the figure. Stable cells are marked green.Paw the Spider isn't a skillful webmaker yet, he is only learning to make perfect webs. Help Paw to determine the number of unstable cells in the web he has just spun.", "input_spec": "The first line contains integer n (3 ≤ n ≤ 1000) — the number of main threads. The i-th of following n lines describe the bridges located in the i-th sector: first it contains integer ki (1 ≤ ki ≤ 105) equal to the number of bridges in the given sector. Then follow ki different integers pij (1 ≤ pij ≤ 105; 1 ≤ j ≤ ki). Number pij equals the distance from the attachment points of the j-th bridge of the i-th sector to the center of the web. It is guaranteed that any two bridges between adjacent sectors are attached at a different distance from the center of the web. It is guaranteed that the total number of the bridges doesn't exceed 105.", "output_spec": "Print a single integer — the number of unstable cells in Paw the Spider's web.", "sample_inputs": ["7\n3 1 6 7\n4 3 5 2 9\n2 8 1\n4 3 7 6 4\n3 2 5 9\n3 6 3 8\n3 4 2 9"], "sample_outputs": ["6"], "notes": null}, "positive_code": [{"source_code": "s,m,n=0,1e6,Array.new(n=gets.to_i){gets.split.map(&:to_i)[1..-1].sort}\n(n+n[0,2]).each_cons(3){|a,b,c|r,b,*e=b.dup\nb&&(a,*w=a.drop_while{|i|ia||b>c ? (a>b ? (h+=1;c=r.shift||m):(g+=1;a=w.shift||m)):(g!=h&&s+=1\nb>m||(g=h;break if !b=e.shift))})}\np s"}, {"source_code": "#require \"profile\"\n\ntime = Time.now\n\nSTDOUT.sync = true\n\nnet = Array.new(n = gets.to_i) do\n gets \" \"\n gets.split.map(&:to_i).sort\nend\n\nnet += net.take(2)\n\ns = 0\n\nnet.each_cons(3) do |l1t,l2t,l3t| #puts [l2t].inspect\n l2 = l2t.dup; start = l2.shift; #puts start\n l1 = l1t.dup; l1 = l1.drop_while{ |i| i < start }\n l3 = l3t.dup; l3 = l3.drop_while{ |i| i < start }\n a = l1.shift || 1000000\n next unless b = l2.shift\n c = l3.shift || 1000000\n g1 = g2 = 0\n loop do\n #puts [a,b,c,g1,g2,s].inspect\n if b <= a && b <= c\n if b == 1000000\n s += 1 if g1 != g2\n break\n end\n #puts \"b\"\n s += 1 if g1 != g2\n g1 = g2 = 0\n break unless b = l2.shift\n elsif a <= b && a <= c\n #puts \"a\"\n g1 += 1\n a = l1.shift || 1000000\n else\n #puts \"c\"\n g2 += 1\n c = l3.shift || 1000000\n end\n end\n #puts s\nend\n\nputs s\n\n#puts Time.now - time\n"}, {"source_code": "n = (1..(n=gets.to_i)).map do\n gets.split.map(&:to_i).drop(1).sort\nend\n\ns = 0\nm = 1e6\n\n(n + n[0,2]).each_cons(3) do |a,b,c|\n r,b,*e = b.dup\n next if !b\n a,*w = a.dup.drop_while{ |i| i < r }; a ||= m\n c,*r = c.dup.drop_while{ |i| i < r }; c ||= m\n g = h = 0\n loop do\n b <= a && b <= c ? (\n s += 1 if g != h\n (b <= m) && ( g = h; break if !b = e.shift )\n ) : a <= b && a <= c ?\n ( g += 1; a = w.shift || m ) :\n ( h += 1; c = r.shift || m )\n end\nend\n\nputs s\n"}, {"source_code": "s,m,n=0,1e6,Array.new(n=gets.to_i){gets.split.map(&:to_i)[1..-1].sort}\n(n+n[0,2]).each_cons(3){|a,b,c|r,b,*e=b.dup\nb&&(a,*w=a.drop_while{|i|ia||b>c ? (a>b ? (h+=1;c=r.shift||m):(g+=1;a=w.shift||m)):(g!=h&&s+=1\nb>m||(g=h;break if !b=e.shift))})}\np s\n"}, {"source_code": "s,m,n=0,1e6,Array.new(n=gets.to_i){gets.split.map(&:to_i)[1..-1].sort}\n(n+n[0,2]).each_cons(3){|a,b,c|r,b,*e=b.dup\nb&&(a,*w=a.drop_while{|i|ia||b>c ? (a>b ? (h+=1;c=r.shift||m):(g+=1;a=w.shift||m)):(g!=h&&s+=1\nb>m||(g=h;break if !b=e.shift))})}\np s\n"}, {"source_code": "s,m,n=0,1e6,Array.new(n=gets.to_i){gets.split.map(&:to_i)[1..-1].sort}\n(n+n[0,2]).each_cons(3){|a,b,c|r,b,*e=b.dup\nb&&(a,*w=a.drop_while{|i|ia||b>c ? (a>b ? (h+=1;c=r.shift||m):(g+=1;a=w.shift||m)):(g!=h&&s+=1\nb>m||(g=h;break if !b=e.shift))})}\np s\n"}, {"source_code": "s,m,n=0,1e6,Array.new(n=gets.to_i){gets.split.map(&:to_i)[1..-1].sort}\n(n+n[0,2]).each_cons(3){|a,b,c|r,b,*e=b.dup\nb&&(a,*w=a.drop_while{|i|ia||b>c ? (a>b ? (h+=1;c=r.shift||m):(g+=1;a=w.shift||m)):(g!=h&&s+=1\nb>m||(g=h;break if !b=e.shift))})}\np s\n"}, {"source_code": "s,m,n=0,1e6,Array.new(n=gets.to_i){gets.split.map(&:to_i)[1..-1].sort}\n(n+n[0,2]).each_cons(3){|a,b,c|r,b,*e=b.dup\nb&&(a,*w=a.drop_while{|i|ia||b>c ? (a>b ? (h+=1;c=r.shift||m):(g+=1;a=w.shift||m)):(g!=h&&s+=1\nb>m||(g=h;break if !b=e.shift))})}\np s\n"}, {"source_code": "s,m,n=0,1e6,Array.new(n=gets.to_i){gets.split.map(&:to_i)[1..-1].sort}\n(n+n[0,2]).each_cons(3){|a,b,c|r,b,*e=b.dup\nb&&(a,*w=a.drop_while{|i|ia||b>c ? (a>b ? (h+=1;c=r.shift||m):(g+=1;a=w.shift||m)):(g!=h&&s+=1\nb>m||(g=h;break if !b=e.shift))})}\np s\n"}, {"source_code": "s,m,n=0,1e6,Array.new(n=gets.to_i){gets.split.map(&:to_i)[1..-1].sort}\n(n+n[0,2]).each_cons(3){|a,b,c|r,b,*e=b.dup\nb&&(a,*w=a.drop_while{|i|ia||b>c ? (a>b ? (h+=1;c=r.shift||m):(g+=1;a=w.shift||m)):(g!=h&&s+=1\nb>m||(g=h;break if !b=e.shift))})}\np s\n"}, {"source_code": "s,m,n=0,1e6,Array.new(n=gets.to_i){gets.split.map(&:to_i)[1..-1].sort}\n(n+n[0,2]).each_cons(3){|a,b,c|r,b,*e=b.dup\nb&&(a,*w=a.drop_while{|i|ia||b>c ? (a>b ? (h+=1;c=r.shift||m):(g+=1;a=w.shift||m)):(g!=h&&s+=1\nb>m||(g=h;break if !b=e.shift))})}\np s\n"}], "negative_code": [{"source_code": "#require \"profile\"\n\ntime = Time.now\n\nSTDOUT.sync = true\n\nnet = Array.new(n = gets.to_i) do\n gets \" \"\n gets.split.map(&:to_i).sort\nend\n\nnet += net.take(2)\n\ns = 0\n\nnet.each_cons(3) do |l1t,l2t,l3t| #puts [l2t].inspect\n l2 = l2t.dup; start = l2.shift; #puts start\n l1 = l1t.dup; l1 = l1.drop_while{ |i| i < start }\n l3 = l3t.dup; l3 = l3.drop_while{ |i| i < start }\n a = l1.shift || 1000000\n next unless b = l2.shift\n c = l3.shift || 1000000\n g1 = g2 = 0\n loop do\n #puts [a,b,c,g1,g2,s].inspect\n if b <= a && b <= c\n if b == 1000000\n s += 1 if g1 != g2\n break\n end\n #puts \"b\"\n s += 1 if g1 != g2\n g1 = g2 = 0\n break unless b = l2.shift\n elsif a <= b && a <= c\n #puts \"a\"\n g1 += 1\n a = l1.shift || 1000000\n else\n #puts \"c\"\n g2 += 1\n c = l3.shift || 1000000\n end\n end\n #puts s\nend\n\nputs s\n\nputs Time.now - time\n"}], "src_uid": "c8951278f649e78e5fae5c2b2b844315"} {"nl": {"description": "Arseniy is already grown-up and independent. His mother decided to leave him alone for m days and left on a vacation. She have prepared a lot of food, left some money and washed all Arseniy's clothes. Ten minutes before her leave she realized that it would be also useful to prepare instruction of which particular clothes to wear on each of the days she will be absent. Arseniy's family is a bit weird so all the clothes is enumerated. For example, each of Arseniy's n socks is assigned a unique integer from 1 to n. Thus, the only thing his mother had to do was to write down two integers li and ri for each of the days — the indices of socks to wear on the day i (obviously, li stands for the left foot and ri for the right). Each sock is painted in one of k colors.When mother already left Arseniy noticed that according to instruction he would wear the socks of different colors on some days. Of course, that is a terrible mistake cause by a rush. Arseniy is a smart boy, and, by some magical coincidence, he posses k jars with the paint — one for each of k colors.Arseniy wants to repaint some of the socks in such a way, that for each of m days he can follow the mother's instructions and wear the socks of the same color. As he is going to be very busy these days he will have no time to change the colors of any socks so he has to finalize the colors now.The new computer game Bota-3 was just realised and Arseniy can't wait to play it. What is the minimum number of socks that need their color to be changed in order to make it possible to follow mother's instructions and wear the socks of the same color during each of m days.", "input_spec": "The first line of input contains three integers n, m and k (2 ≤ n ≤ 200 000, 0 ≤ m ≤ 200 000, 1 ≤ k ≤ 200 000) — the number of socks, the number of days and the number of available colors respectively. The second line contain n integers c1, c2, ..., cn (1 ≤ ci ≤ k) — current colors of Arseniy's socks. Each of the following m lines contains two integers li and ri (1 ≤ li, ri ≤ n, li ≠ ri) — indices of socks which Arseniy should wear during the i-th day.", "output_spec": "Print one integer — the minimum number of socks that should have their colors changed in order to be able to obey the instructions and not make people laugh from watching the socks of different colors.", "sample_inputs": ["3 2 3\n1 2 3\n1 2\n2 3", "3 2 2\n1 1 2\n1 2\n2 1"], "sample_outputs": ["2", "0"], "notes": "NoteIn the first sample, Arseniy can repaint the first and the third socks to the second color.In the second sample, there is no need to change any colors."}, "positive_code": [{"source_code": "def solve\n n, m, k = gets.split.map(&:to_i)\n cs = gets.split.map(&:to_i)\n uf = UnionFind.new(n)\n m.times do\n l, r = gets.split.map(&:to_i)\n uf.unit(l - 1, r - 1)\n end\n g2c = {}\n cs.each_with_index do |e, i|\n x = uf.find(i)\n colors = (g2c[x] ||= {})\n colors[e] ||= 0\n colors[e] += 1\n end\n ans = 0\n g2c.each_value do |colors|\n vs = colors.values\n sum = vs.inject(0, &:+)\n max = vs.max\n ans += sum - max\n end\n puts ans\nend\n\nclass UnionFind\n def initialize(size)\n @parent = (0...size).to_a\n @rank = Array.new(size, 0)\n end\n\n def find(x)\n return x if @parent[x] == x\n return (@parent[x] = find(@parent[x]))\n end\n\n def unit(x, y)\n x, y = find(x), find(y)\n return if x == y\n if @rank[x] < @rank[y]\n @parent[x] = y\n else\n @parent[y] = x\n @rank[x] += 1 if @rank[x] == @rank[y]\n end\n end\n\n def same(x, y)\n find(x) == find(y)\n end\nend\n\nsolve\n"}, {"source_code": "# TEMPLATE BEGIN\ndef read_vector\n gets.chomp.split(/\\s+/)\nend\ndef read_vector_int\n read_vector.map(&:to_i)\nend\n# TEMPLATE END\n\nclass SNM\n def initialize(n)\n @a = (0..n).to_a\n end\n\n def get(v)\n while @a[v] != v\n @a[v] = @a[@a[v]]\n v = @a[v]\n end\n return v\n end\n\n def join(u, v)\n pu = get(u)\n pv = get(v)\n return if pu == pv\n if rand < 0.5\n @a[pu] = @a[pv]\n else\n @a[pv] = @a[pu]\n end\n end\nend\n\nn, m, k = read_vector_int\ncolors = [nil] + read_vector_int\n\nsnm = SNM.new(n)\nm.times.each do\n v, u = read_vector_int\n snm.join(v, u)\nend\n\ncolors_in_comp = Array.new(n + 1) { [] }\n(1..n).each do |id|\n comp_id = snm.get(id)\n colors_in_comp[comp_id].push(colors[id])\nend\n\nans = 0\ncolors_in_comp.each do |c|\n next if c.empty?\n\n cnt = Hash.new(0)\n c.sort!\n c.each{ |col| cnt[col] += 1 }\n max_cnt = cnt.values.max\n ans += c.size - max_cnt\nend\n\nputs ans\n"}, {"source_code": "n, m, k = gets.split.map(&:to_i)\n$socks = [nil] + gets.split.map(&:to_i)\ndays = m.times.map{gets.split.map(&:to_i)}\n\n$visited = Array.new(n + 1)\n$e = Array.new(n + 1){Array.new}\n\ndays.each do |l, r|\n $e[l] << r\n $e[r] << l\nend\n\ndef bfs(u)\n todo = []\n push = lambda do |v|\n $visited[v] = true\n todo << v\n end\n push.(u)\n while u = todo.shift\n $rainbow[$socks[u]] += 1\n $e[u].each do |v|\n next if $visited[v]\n push.(v)\n end\n end\nend\n\nans = 0\n1.upto(n) do |i|\n next if $visited[i]\n $rainbow = Hash.new(0)\n bfs(i)\n v = $rainbow.values\n ans += v.inject(:+) - v.max\nend\n\nputs ans\n"}], "negative_code": [], "src_uid": "39232c03c033da238c5d1e20e9595d6d"} {"nl": {"description": "Consider 2n rows of the seats in a bus. n rows of the seats on the left and n rows of the seats on the right. Each row can be filled by two people. So the total capacity of the bus is 4n.Consider that m (m ≤ 4n) people occupy the seats in the bus. The passengers entering the bus are numbered from 1 to m (in the order of their entering the bus). The pattern of the seat occupation is as below:1-st row left window seat, 1-st row right window seat, 2-nd row left window seat, 2-nd row right window seat, ... , n-th row left window seat, n-th row right window seat.After occupying all the window seats (for m > 2n) the non-window seats are occupied:1-st row left non-window seat, 1-st row right non-window seat, ... , n-th row left non-window seat, n-th row right non-window seat.All the passengers go to a single final destination. In the final destination, the passengers get off in the given order.1-st row left non-window seat, 1-st row left window seat, 1-st row right non-window seat, 1-st row right window seat, ... , n-th row left non-window seat, n-th row left window seat, n-th row right non-window seat, n-th row right window seat. The seating for n = 9 and m = 36. You are given the values n and m. Output m numbers from 1 to m, the order in which the passengers will get off the bus.", "input_spec": "The only line contains two integers, n and m (1 ≤ n ≤ 100, 1 ≤ m ≤ 4n) — the number of pairs of rows and the number of passengers.", "output_spec": "Print m distinct integers from 1 to m — the order in which the passengers will get off the bus.", "sample_inputs": ["2 7", "9 36"], "sample_outputs": ["5 1 6 2 7 3 4", "19 1 20 2 21 3 22 4 23 5 24 6 25 7 26 8 27 9 28 10 29 11 30 12 31 13 32 14 33 15 34 16 35 17 36 18"], "notes": null}, "positive_code": [{"source_code": "n,m=gets.split.map(&:to_i)\na=[*1..(m+3)/4*4].each_slice(2).each_slice(n).to_a\nputs a.shift.zip(*a).flat_map{|(a,b),(c,d)|[c,a,d,b]}.select{|e|e&&e<=m}*' '"}, {"source_code": "n, m = readline.split(' ').collect(&:to_i)\narr = []\n1.upto(n) do |i|\n arr += [i * 2 - 1 + n * 2, i * 2 - 1, i * 2 + n * 2, i * 2]\nend\narr.keep_if {|num| num <= m}\narr.each_with_index do |num, ind|\n if ind != 0\n print \" \"\n end\n print num\nend\nputs \"\"\n"}, {"source_code": "a=gets.chomp.split(\" \")\nn=a[0].to_i\nm=a[1].to_i\ni=1\np=2\nn.times do \n if (i+2*n)<=m\n print \"#{i+2*n} \"\n end\n if i<=m\n print \"#{i} \"\n end\n \n i+=2\n if (p+2*n)<=m\n print \"#{p+2*n} \"\n end\n if p<=m\n print \"#{p} \"\n end\n \n p+=2\nend\n"}, {"source_code": "a=gets.chomp.split(\" \")\nn=a[0].to_i\nm=a[1].to_i\ni=1\np=2\nn.times do \n if (i+2*n)<=m\n print \"#{i+2*n} \"\n end\n if i<=m\n print \"#{i} \"\n end\n \n i+=2\n if (p+2*n)<=m\n print \"#{p+2*n} \"\n end\n if p<=m\n print \"#{p} \"\n end\n \n p+=2\nend"}, {"source_code": "buf = gets.split(' ')\nn,m=buf[0].to_i,buf[1].to_i\nr=f=0\na=Array.new(n)\nfor i in 0..n-1\n\ta[i] = Array.new(4)\nend\nfor i in 1..m\n\ta[r][0] = i if f==0 && i%2==1\n\ta[r][3] = i if f==0 && i%2==0\n\ta[r][1] = i if f==1 && i%2==1\n\ta[r][2] = i if f==1 && i%2==0\n\tr+=1 if i%2==0\n\tif r==n\n\t\tr=0\n\t\tf=1\n\tend\nend\nans=\"\"\nfor i in 0..n-1\n\tans+= ' ' + a[i][1].to_s if a[i][1]\n\tans+= ' ' + a[i][0].to_s if a[i][0]\n\tans+= ' ' + a[i][2].to_s if a[i][2]\n\tans+= ' ' + a[i][3].to_s if a[i][3]\nend\nputs ans.strip"}], "negative_code": [{"source_code": "n,m=gets.split.map(&:to_i)\nm0=(m+2*n-1)/(2*n)\nputs [*1..m0*2*n].each_slice(n).each_slice(m0).to_a.transpose.flat_map{|a|b=a.reverse;b.shift.zip(*b).flatten}.select{|e|e<=m}*' '"}, {"source_code": "#!/usr/bin/ruby\nn,m=gets.split.map(&:to_i)\nm0=(m+3)/4\nputs [*1..m0*4].each_slice(2).each_slice(m0).to_a.transpose.flat_map{|(a,b),(c,d)|[c,a,d,b]}.select{|e|e<=m}*' '"}], "src_uid": "0a701242ca81029a1791df74dc8ca59b"} {"nl": {"description": "Polycarp wants to assemble his own keyboard. Layouts with multiple rows are too complicated for him — his keyboard will consist of only one row, where all $$$26$$$ lowercase Latin letters will be arranged in some order.Polycarp uses the same password $$$s$$$ on all websites where he is registered (it is bad, but he doesn't care). He wants to assemble a keyboard that will allow to type this password very easily. He doesn't like to move his fingers while typing the password, so, for each pair of adjacent characters in $$$s$$$, they should be adjacent on the keyboard. For example, if the password is abacaba, then the layout cabdefghi... is perfect, since characters a and c are adjacent on the keyboard, and a and b are adjacent on the keyboard. It is guaranteed that there are no two adjacent equal characters in $$$s$$$, so, for example, the password cannot be password (two characters s are adjacent).Can you help Polycarp with choosing the perfect layout of the keyboard, if it is possible?", "input_spec": "The first line contains one integer $$$T$$$ ($$$1 \\le T \\le 1000$$$) — the number of test cases. Then $$$T$$$ lines follow, each containing one string $$$s$$$ ($$$1 \\le |s| \\le 200$$$) representing the test case. $$$s$$$ consists of lowercase Latin letters only. There are no two adjacent equal characters in $$$s$$$.", "output_spec": "For each test case, do the following: if it is impossible to assemble a perfect keyboard, print NO (in upper case, it matters in this problem); otherwise, print YES (in upper case), and then a string consisting of $$$26$$$ lowercase Latin letters — the perfect layout. Each Latin letter should appear in this string exactly once. If there are multiple answers, print any of them. ", "sample_inputs": ["5\nababa\ncodedoca\nabcda\nzxzytyz\nabcdefghijklmnopqrstuvwxyza"], "sample_outputs": ["YES\nbacdefghijklmnopqrstuvwxyz\nYES\nedocabfghijklmnpqrstuvwxyz\nNO\nYES\nxzytabcdefghijklmnopqrsuvw\nNO"], "notes": null}, "positive_code": [{"source_code": "T = gets.to_i\n\nT.times do\n s = gets.chomp\n\n if s.size == 1\n puts 'YES'\n puts [*'a'..'z'].join\n next\n end\n\n edge = Hash.new { |h, k| h[k] = [] }\n\n s.chars.each_cons(2) do |v, u|\n edge[v] |= [u]\n edge[u] |= [v]\n end\n\n use_key = edge.keys\n vc = use_key.size\n remain = [*'a'..'z'] - use_key\n min_d = Float::INFINITY\n max_d = 0\n one_cnt = 0\n\n edge.each do |k, list|\n len = list.size\n\n one_cnt += 1 if len == 1\n\n min_d = len if min_d > len\n max_d = len if max_d < len\n end\n\n if max_d >= 3\n puts 'NO'\n next\n end\n\n if one_cnt != 2\n puts 'NO'\n next\n end\n\n root = edge.find { |_, list| list.size == 1 }\n queue = [root[0]]\n board = []\n visited = Hash.new(false)\n\n until queue.empty?\n key = queue.shift\n\n next if visited[key]\n visited[key] = true\n\n board << key\n\n edge[key].each do |u|\n queue << u\n end\n end\n\n res = board + remain\n\n if res.size < 26\n puts 'NO'\n else\n puts 'YES'\n puts res.join\n end\nend\n"}], "negative_code": [], "src_uid": "8fb62b497b6fb2a5fb4f2669aeb51b73"} {"nl": {"description": "Nowadays the one-way traffic is introduced all over the world in order to improve driving safety and reduce traffic jams. The government of Berland decided to keep up with new trends. Formerly all n cities of Berland were connected by n two-way roads in the ring, i. e. each city was connected directly to exactly two other cities, and from each city it was possible to get to any other city. Government of Berland introduced one-way traffic on all n roads, but it soon became clear that it's impossible to get from some of the cities to some others. Now for each road is known in which direction the traffic is directed at it, and the cost of redirecting the traffic. What is the smallest amount of money the government should spend on the redirecting of roads so that from every city you can get to any other?", "input_spec": "The first line contains integer n (3 ≤ n ≤ 100) — amount of cities (and roads) in Berland. Next n lines contain description of roads. Each road is described by three integers ai, bi, ci (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ ci ≤ 100) — road is directed from city ai to city bi, redirecting the traffic costs ci.", "output_spec": "Output single integer — the smallest amount of money the government should spend on the redirecting of roads so that from every city you can get to any other.", "sample_inputs": ["3\n1 3 1\n1 2 1\n3 2 1", "3\n1 3 1\n1 2 5\n3 2 1", "6\n1 5 4\n5 3 8\n2 4 15\n1 6 16\n2 3 23\n4 6 42", "4\n1 2 9\n2 3 8\n3 4 7\n4 1 5"], "sample_outputs": ["1", "2", "39", "0"], "notes": null}, "positive_code": [{"source_code": "def dfs(u, g, used)\n used[u], result = true, 0\n g[u].each do |v, cost|\n result += cost + dfs(v, g, used) unless used[v]\n end\n result\nend\n\ndef solve(n, g)\n left, right = g[0]\n\n g[0] = [left]\n alpha = dfs(0, g, Array.new(n, false)) + g[right.first].assoc(0)[-1]\n\n g[0] = [right]\n beta = dfs(0, g, Array.new(n, false)) + g[left.first].assoc(0)[-1]\n\n [alpha, beta].min\nend\n\n\nn = gets.to_i\ng = Array.new(n) { [] }\n\nn.times do\n a, b, c = gets.split.map { |v| v.to_i }\n a -= 1\n b -= 1\n g[a] << [b, 0]\n g[b] << [a, c]\nend\n\nputs solve(n, g)\n"}, {"source_code": "def dfs(u, g, used)\n used[u] = true\n result = 0\n g[u].each do |v, cost|\n result += cost + dfs(v, g, used) unless used[v]\n end\n result\nend\n\ndef solve(n, g)\n left, right = g[0]\n\n g[0] = [left]\n alpha = dfs(0, g, Array.new(n, false)) + g[right.first].assoc(0)[-1]\n\n g[0] = [right]\n beta = dfs(0, g, Array.new(n, false)) + g[left.first].assoc(0)[-1]\n\n [alpha, beta].min\nend\n\n\nn = gets.to_i\ng = Array.new(n) { [] }\n\nn.times do\n a, b, c = gets.split.map { |v| v.to_i }\n a -= 1\n b -= 1\n g[a] << [b, 0]\n g[b] << [a, c]\nend\n\nputs solve(n, g)\n"}, {"source_code": "def dfs(u, g, used)\n used[u] = true\n result = 0\n g[u].each do |pair|\n v, cost = pair\n result += cost + dfs(v, g, used) if not used[v]\n end\n result\nend\n\ndef solve(n, g)\n first, last = g[0]\n\n g[0] = [first]\n alpha = dfs(0, g, Array.new(n, false)) + g[last.first].assoc(0)[-1]\n\n g[0] = [last]\n beta = dfs(0, g, Array.new(n, false)) + g[first.first].assoc(0)[-1]\n\n [alpha, beta].min\nend\n\n\nn = gets.to_i\ng = Array.new(n) { [] }\n\nn.times do\n a, b, c = gets.split.map { |v| v.to_i }\n a -= 1\n b -= 1\n g[a] << [b, 0]\n g[b] << [a, c]\nend\n\nputs solve(n, g)\n"}, {"source_code": "def gao(e, v, p)\n\te[v].map{|w, c| w == p ? 0 : c + gao(e, w, v)}.inject(&:+)\nend\n\nn = gets.to_i\ne = Array.new(n + 1){[]}\ny = 0\n(n - 1).times do\n\ta, b, c = gets.split.map(&:to_i)\n\te[a] << [b, 0]\n\te[b] << [a, c]\n\ty += c\nend\na, b, c = gets.split.map(&:to_i)\nx = gao(e, a, -1)\np [x + c, y - x].min\n"}, {"source_code": "n=gets.to_i\ng=Array.new(n){[]}\nn.times{\n input=gets.split.map(&:to_i)\n a=input[0]-1\n b=input[1]-1\n c=input[2]\n g[a].push([a,b,c])\n g[b].push([a,b,c])\n}\nres=[]\nfor q in 0..1\n cost=0\n v=g[0][q][1]\n if v==0 then\n cost+=g[0][q][2]\n v=g[0][q][0]\n end\n back=g[0][q]\n loop do\n if v==0 then\n break\n end\n for i in 0..1\n if g[v][i]!=back then\n back=g[v][i]\n if g[v][i][1]==v then\n cost+=g[v][i][2]\n v=g[v][i][0]\n else\n v=g[v][i][1]\n end\n break\n end\n end\n end\n res.push(cost)\nend\nputs res.min"}, {"source_code": "h=Array.new(gets.to_i){[]}\nwhile gets\na,b,c=$_.split.map &:to_i\nh[a-1]<<[b-1,0]\nh[b-1]<<[a-1,c]\nend\np h[0].map{|a,s|x=0\nwhile a>0\nh[a].map{|b,c|if b!=x\na,x=b,a\ns+=c\nbreak\nend}\nend\ns}.min\n"}, {"source_code": "h=Array.new(gets.to_i){[]}\nwhile gets\na,b,c=$_.split.map &:to_i\nh[a-1]<<[b-1,0]\nh[b-1]<<[a-1,c]\nend\np h[0].map{|a,s|x=0\nwhile a>0\nh[a].map{|b,c|if b!=x\na,x=b,a\ns+=c\nbreak\nend}\nend\ns}.min\n"}, {"source_code": "h=Array.new(gets.to_i){[]}\nwhile gets\na,b,c=$_.split.map &:to_i\nh[a-1]<<[b-1,0]\nh[b-1]<<[a-1,c]\nend\np h[0].map{|a,s|x=0\nwhile a>0\nh[a].map{|b,c|if b!=x\na,x=b,a\ns+=c\nbreak\nend}\nend\ns}.min"}, {"source_code": "h=Array.new(gets.to_i){[]}\nwhile gets\na,b,c=$_.split.map &:to_i\nh[a-1]<<[b-1,0]\nh[b-1]<<[a-1,c]\nend\np h[0].map{|a,s|x=0\nwhile a>0\nh[a].map{|b,c|if b!=x\na,x=b,a\ns+=c\nbreak\nend}\nend\ns}.min"}, {"source_code": "h=Array.new(gets.to_i){[]}\nwhile gets\na,b,c=$_.split.map &:to_i\nh[a-1]<<[b-1,0]\nh[b-1]<<[a-1,c]\nend\np h[0].map{|a,s|x=0\nwhile a>0\nh[a].map{|b,c|if b!=x\na,x=b,a\ns+=c\nbreak\nend}\nend\ns}.min\n"}, {"source_code": "h=Array.new(gets.to_i){[]}\nwhile gets\na,b,c=$_.split.map &:to_i\nh[a-1]<<[b-1,0]\nh[b-1]<<[a-1,c]\nend\np h[0].map{|a,s|x=0\nwhile a>0\nh[a].map{|b,c|if b!=x\na,x=b,a\ns+=c\nbreak\nend}\nend\ns}.min\n"}, {"source_code": "#!/usr/bin/ruby\n\nN = gets.to_i\n\ncities = Array.new(N + 1) {Array.new}\n\nN.times do\n (a, b, c) = gets.split.map{|e| e.to_i}\n\n cities[a].push [b, 0]\n cities[b].push [a, c]\nend\n\nprev = 1\nnow = cities[1][0][0]\ncost0 = cities[1][0][1]\n\n(N - 1).times do # not N - 2\n id = (cities[now][0][0] == prev) ? 1 : 0\n prev = now\n cost0 += cities[now][id][1]\n now = cities[now][id][0]\nend\n\nprev = 1\nnow = cities[1][1][0]\ncost1 = cities[1][1][1]\n\n(N - 1).times do # not N - 2\n id = (cities[now][0][0] == prev) ? 1 : 0\n prev = now\n cost1 += cities[now][id][1]\n now = cities[now][id][0]\nend\n\nputs [cost0, cost1].min\n"}, {"source_code": "h=Array.new(gets.to_i){[]}\nwhile gets\na,b,c=$_.split.map &:to_i\nh[a-1]<<[b-1,0]\nh[b-1]<<[a-1,c]\nend\np h[0].map{|a,s|x=0\nwhile a>0\nh[a].map{|b,c|if b!=x\na,x=b,a\ns+=c\nbreak\nend}\nend\ns}.min"}, {"source_code": "h=Array.new(gets.to_i){[]}\nwhile gets\na,b,c=$_.split.map &:to_i\nh[a-1]<<[b-1,0]\nh[b-1]<<[a-1,c]\nend\np h[0].map{|a,s|x=0\nwhile a>0\nh[a].map{|b,c|if b!=x\na,x=b,a\ns+=c\nbreak\nend}\nend\ns}.min\n"}, {"source_code": "p = Array.new(101)\n101.times{|i|\n p[i]=Array.new(101,0)\n}\n\ndef find_cost(prev, now, n, p)\n res, d = 0, p[prev][now]\n #puts \"find_cost: \"+prev.to_s+\" \"+now.to_s\n res = res - d if (d<0)\n return res if (now==1)\n (1..n).each{|i|\n res += find_cost(now, i, n, p) if (p[now][i].to_i!=0)&&(i!=prev)\n }\n res\nend\n\nn = gets.chomp.to_i\n\nn.times { |i|\n a,b,c = gets.chomp.split(' ').map{|it| it.to_i}\n p[a][b], p[b][a] = c, -c\n}\n\nans = -1\n\n(2..n).each{|i|\n if p[1][i]!=0\n candidate = find_cost(1, i, n, p)\n ans = candidate if (ans==-1 || candidate < ans)\n end\n}\n\nputs ans"}, {"source_code": "h=Array.new(gets.to_i){[]}\nwhile gets\n a,b,c=$_.split.map &:to_i\n h[a-1]<<[b-1,0]\n h[b-1]<<[a-1,c]\nend\np h[0].map{|a,s|x=0\n while a>0\n h[a].map{|b,c|\n if b!=x\n a,x=b,a\n s+=c\n break\n end\n }\n end\n s\n}.min\n"}, {"source_code": "require \"pp\"\n\nclass Edge\n def initialize (src,dst,cost)\n @src = src\n @dst = dst\n @cost = cost\n end\n \n attr_accessor :src, :dst, :cost\nend\n\n\nedges = Array.new\nnum = gets.to_i\ncost = 0\n\nfor i in 0..num-1\n\tary = gets.split.map{|elem| elem.to_i}\n\tedges << Edge.new(ary[0],ary[1],ary[2])\n\tcost += ary[2]\nend\n\nfirst = edges.shift\narc = Edge.new(first.src,first.dst,0)\n\nwhile edges.length > 0\n\tedges.each_with_index do |edge, i|\n\t\tif edge.src == arc.dst\n\t\t\tarc.dst = edge.dst\n\t\t\tedges.delete_at(i)\n\t\telsif edge.dst == arc.dst\n\t\t\tarc.dst = edge.src\n\t\t\tarc.cost += edge.cost\n\t\t\tedges.delete_at(i)\n\t\tend\n\tend\nend\n\np [arc.cost,cost - arc.cost].min\n\n"}, {"source_code": "# start 2010/7/28 17:11\n# implementation finish at 18:57\n\n#ring_road\n\n\nclass Edge\n def initialize(u,v,inv_cost)\n @src = u\n @dst = v\n @inv_cost = inv_cost\n end\n\n attr_accessor :src, :dst, :inv_cost\nend\n\n\n\n#f = open(\"hoge.txt\")\n\nnum = gets.to_i\n#num = f.gets.to_i\n#p num\n\nedges = Array.new\nedge_subsets = Array.new\n\ntotal_cost = 0\n\nfor i in 0..num-1\n\t#ary = f.gets.split.map {|elem| elem.to_i}\n\tary = gets.split.map {|elem| elem.to_i}\n\t# p ary\n\t\n\t# e(u,v) , inv_cost_of_e �Ƃ���format.\n\tedges << Edge.new(ary[0],ary[1],ary[2])\n\t\n\ttotal_cost += ary[2]\nend\n\n\n#circle�����\n\t#edge��subset������čs���Amerge���Ă���\n\nedges.each {|edge|\n\tif edge_subsets.length == 0\n\t\tsubset = Array.new\n\t\tsubset << edge.src << edge.dst\n\t\tedge_subsets << subset\n\telse\n\t\tmerge_flg = false\n\n\t\tedge_subsets.each{|subset|\n\t\t\tmerge_flg = true\n\t\t\t\n\t\t\tif edge.src == subset.first\n\t\t\t\tsubset.unshift edge.dst\n\t\t\telsif edge.src == subset.last\n\t\t\t\tsubset << edge.dst\n\t\t\telsif edge.dst == subset.first\n\t\t\t\tsubset.unshift edge.src\n\t\t\telsif edge.dst == subset.last\n\t\t\t\tsubset << edge.src\n\t\t\telse\n\t\t\t\tmerge_flg = false\n\t\t\tend\n\t\t\t\n\t\t\tif merge_flg\n\t\t\t\tbreak\n\t\t\tend\n\t\t}\n\t\t\n\t\tunless merge_flg\n\t\t\tsubset = Array.new\n\t\t\tsubset << edge.src << edge.dst\n\t\t\tedge_subsets << subset\n\t\tend\n\tend\n}\n\n#p edge_subsets\n\n#subset���m��merge\n\n#puts \"merge subsets\"\n\nwhile edge_subsets.length > 1\n\ttemp_ss = edge_subsets.pop\n\n\tedge_subsets.each{|subset|\n\t\tif temp_ss.first == subset.first\n\t\t\ttemp_ss.reverse!\n\t\t\ttemp_ss.pop\n\t\t\tsubset = temp_ss + subset\n\t\telsif temp_ss.first == subset.last\n\t\t\ttemp_ss.shift\n\t\t\tsubset.concat temp_ss\n\t\telsif temp_ss.last == subset.first\n\t\t\ttemp_ss.pop\n\t\t\tsubset = temp_ss + subset\n\t\telsif temp_ss.last == subset.last\n\t\t\ttemp_ss.reverse!\n\t\t\ttemp_ss.shift\n\t\t\tsubset.concat temp_ss\n\t\tend\n\t}\nend\n\n#p edge_subsets\n\ncircle = edge_subsets.pop\n#p circle\n\nrev_cost = 0\n\nedges.each {|edge|\n\tidx = circle.index(edge.src)\n\tif circle[idx+1] != edge.dst\n\t\t#p edge\n\t\trev_cost += edge.inv_cost\n\tend\n}\n\nalt = total_cost - rev_cost\nif rev_cost < alt\n\tp rev_cost\nelse\n\tp alt\nend\n\n=begin\n\n[4, 6, 1, 5, 3, 2]\n\n---\nn=6\n\n1 5\n5 3\n\n2 4\n\n1 6\n\n2 3\n4 6\n\n=end\n"}, {"source_code": "h=Array.new(gets.to_i){[]}\nwhile gets\na,b,c=$_.split.map &:to_i\nh[a-1]<<[b-1,0]\nh[b-1]<<[a-1,c]\nend\np h[0].map{|a,s|x=0\nwhile a>0\nh[a].map{|b,c|if b!=x\na,x=b,a\ns+=c\nbreak\nend}\nend\ns}.min\n"}, {"source_code": "h=Array.new(gets.to_i){[]}\nwhile gets\na,b,c=$_.split.map &:to_i\nh[a-1]<<[b-1,0]\nh[b-1]<<[a-1,c]\nend\np h[0].map{|a,s|x=0\nwhile a>0\nh[a].map{|b,c|if b!=x\na,x=b,a\ns+=c\nbreak\nend}\nend\ns}.min\n"}, {"source_code": "h=Array.new(gets.to_i){[]}\nwhile gets\na,b,c=$_.split.map &:to_i\nh[a-1]<<[b-1,0]\nh[b-1]<<[a-1,c]\nend\np h[0].map{|a,s|x=0\nwhile a>0\nh[a].map{|b,c|if b!=x\na,x=b,a\ns+=c\nbreak\nend}\nend\ns}.min"}, {"source_code": "h=Array.new(gets.to_i){[]}\nwhile gets\na,b,c=$_.split.map &:to_i\nh[a-1]<<[b-1,0]\nh[b-1]<<[a-1,c]\nend\np h[0].map{|a,s|x=0\nwhile a>0\nh[a].map{|b,c|if b!=x\na,x=b,a\ns+=c\nbreak\nend}\nend\ns}.min\n"}, {"source_code": "num_of_edge = gets\nedge_list = []\nwhile line = gets\n\tobj = {}\n\tobj[:start], obj[:dest], obj[:cost] = line.chomp.split.map{|s| s.to_i}\n\tedge_list << obj\nend\ncost_pair = [0,0]\nring_obj = edge_list.pop\ncost_pair[0] += ring_obj[:cost]\n\nwhile edge_list.length > 0\n\tdel_obj = edge_list.find{|obj|\n\t\tif obj[:start] == ring_obj[:dest]\n\t\t\tring_obj[:dest] = obj[:dest]\n\t\t\tcost_pair[0] += obj[:cost]\n\t\telsif obj[:dest] == ring_obj[:dest]\n\t\t\tcost_pair[1] += obj[:cost]\n\t\t\tring_obj[:dest] = obj[:start]\n\t\tend\n\t}\n\tedge_list.delete(del_obj)\nend\nputs cost_pair.min\n\n__END__\n6\n1 5 4\n5 3 8\n2 4 15\n1 6 16\n2 3 23\n4 6 42\n"}], "negative_code": [{"source_code": "num_of_edge = gets.to_i\nedge_list = []\nring_hash = Hash.new\nwhile line = gets\n\tobj = {}\n\tobj[:start], obj[:dest], obj[:cost] = line.chomp.split.map{|s| s.to_i}\n\tedge_list << obj\nend\n\nedge_list.each{|obj|\n\tif !ring_hash.include?(obj[:start])\n\t\tring_hash[ obj[:start] ] = obj[:dest]\n\t\tobj[:clock_wise] = true\n\telse\n\t\tring_hash[ obj[:dest] ] = obj[:start]\n\tend\n}\nputs [ edge_list.select{|obj| obj[:clock_wise] }.inject(0){|sum, obj| sum += obj[:cost] } , edge_list.select{|obj| !obj[:clock_wise] }.inject(0){|sum, obj| sum += obj[:cost] } ].min\n"}, {"source_code": "num_of_edge = gets.to_i\nedge_list = []\nclock_wise_edge_list = []\nnon_clock_wise_edge_list = []\nring_hash = Hash.new\nwhile line = gets\n\tobj = {}\n\tobj[:start], obj[:dest], obj[:cost] = line.chomp.split.map{|s| s.to_i}\n\tedge_list << obj\nend\nedge_list.each{|obj|\n\tif !ring_hash.include?(obj[:start])\n\t\tring_hash[ obj[:start] ] = obj[:dest]\n\t\tclock_wise_edge_list << obj\n\telse\n\t\tring_hash[ obj[:dest] ] = obj[:start]\n\t\tnon_clock_wise_edge_list << obj\n\tend\n}\nputs [ clock_wise_edge_list.inject(0){|sum, obj| sum += obj[:cost] } , non_clock_wise_edge_list.select{|obj| !obj[:clock_wise?] }.inject(0){|sum, obj| sum += obj[:cost] } ].min\n"}, {"source_code": "num_of_edge = gets.to_i\nedge_list = []\nring_hash = Hash.new\nwhile line = gets\n\tobj = {}\n\tobj[:start], obj[:dest], obj[:cost] = line.chomp.split.map{|s| s.to_i}\n\tedge_list << obj\nend\nedge_list.each{|obj|\n\tif ring_hash.value?(obj[:start])\n\t\tring_hash[ obj[:start] ] = obj[:dest]\n\telsif ring_hash.key?(obj[:start])\n\t\tring_hash[ obj[:dest] ] = obj[:start]\n\t\tobj[:reverse] = true\n\telse\n\t\tring_hash[ obj[:start] ] = obj[:dest]\n\tend\n}\nputs [ edge_list.select{|obj| obj[:reverse] }.inject(0){|sum, obj| sum += obj[:cost] } , edge_list.select{|obj| !obj[:reverse] }.inject(0){|sum, obj| sum += obj[:cost] } ].min\n"}, {"source_code": "gets\nedge_list = []\nring_hash = Hash.new\nwhile line = gets\n\tobj = {}\n\tobj[:start], obj[:dest], obj[:cost] = line.chomp.split.map{|s| s.to_i}\n\tedge_list << obj\nend\nedge_list.each{|obj|\n\tif !ring_hash.include?(obj[:start])\n\t\tring_hash[ obj[:start] ] = obj[:dest]\n\t\tobj[:clock_wise?] = true\n\telse\n\t\tring_hash[ obj[:dest] ] = obj[:start]\n\tend\n}\nputs [ edge_list.select{|obj| obj[:clock_wise?] }.inject(0){|sum, obj| sum += obj[:cost] } , edge_list.select{|obj| !obj[:clock_wise?] }.inject(0){|sum, obj| sum += obj[:cost] } ].min\n"}], "src_uid": "d85c7a8f7e6f5fc6dffed554bffef3ec"} {"nl": {"description": "You are given string s. Let's call word any largest sequence of consecutive symbols without symbols ',' (comma) and ';' (semicolon). For example, there are four words in string \"aba,123;1a;0\": \"aba\", \"123\", \"1a\", \"0\". A word can be empty: for example, the string s=\";;\" contains three empty words separated by ';'.You should find all words in the given string that are nonnegative INTEGER numbers without leading zeroes and build by them new string a. String a should contain all words that are numbers separating them by ',' (the order of numbers should remain the same as in the string s). By all other words you should build string b in the same way (the order of numbers should remain the same as in the string s).Here strings \"101\", \"0\" are INTEGER numbers, but \"01\" and \"1.0\" are not.For example, for the string aba,123;1a;0 the string a would be equal to \"123,0\" and string b would be equal to \"aba,1a\".", "input_spec": "The only line of input contains the string s (1 ≤ |s| ≤ 105). The string contains only symbols '.' (ASCII 46), ',' (ASCII 44), ';' (ASCII 59), digits, lowercase and uppercase latin letters.", "output_spec": "Print the string a to the first line and string b to the second line. Each string should be surrounded by quotes (ASCII 34). If there are no words that are numbers print dash (ASCII 45) on the first line. If all words are numbers print dash on the second line.", "sample_inputs": ["aba,123;1a;0", "1;;01,a0,", "1", "a"], "sample_outputs": ["\"123,0\"\n\"aba,1a\"", "\"1\"\n\",01,a0,\"", "\"1\"\n-", "-\n\"a\""], "notes": "NoteIn the second example the string s contains five words: \"1\", \"\", \"01\", \"a0\", \"\"."}, "positive_code": [{"source_code": "puts gets.chomp.tr(';',',').split(',',-1).partition{|e|e=~/^-?([1-9][0-9]*|0)$/}.map{|e|e.empty? ? '-' : '\"'+e*','+'\"'}"}, {"source_code": "#!/usr/bin/env ruby\n\na = gets.strip.split /[,;]/, -1\nb, c = a.partition { |x| x[/^(0|[1-9]\\d*)$/] }\nputs b.empty? ? '-' : ('\"' + b.join(',') + '\"')\nputs c.empty? ? '-' : ('\"' + c.join(',') + '\"')"}, {"source_code": "puts gets.chomp.tr(';',',').split(',',-1).partition{|e|e=~/^-?([1-9][0-9]*|0)$/}.map{|e|e.empty? ? '-' : '\"'+e*','+'\"'}"}, {"source_code": "#!/usr/bin/env ruby\n\na = gets.strip.split /[,;]/, -1\nb, c = a.partition { |x| x[/^(0|[1-9]\\d*)$/] }\nputs b.empty? ? '-' : ('\"' + b.join(',') + '\"')\nputs c.empty? ? '-' : ('\"' + c.join(',') + '\"')\n"}, {"source_code": "puts gets.chomp.tr(';',',').split(',',-1).partition{|e|e=~/^-?([1-9][0-9]*|0)$/}.map{|e|e.empty? ? '-' : '\"'+e*','+'\"'}"}, {"source_code": "s = gets.chomp.split(/[,;]/, -1)\nnumber = []\nnonnumber = []\ns.each do |w|\n if w.match(/^[0-9]+$/) then\n if w.length >= 2 and w[0] == \"0\" then\n nonnumber << w\n else\n number << w\n end\n else\n nonnumber << w\n end\nend\nif number.length == 0 then\n puts \"-\"\nelse\n p number.join(\",\")\nend\nif nonnumber.length == 0 then\n puts \"-\"\nelse\n p nonnumber.join(\",\")\nend\n"}, {"source_code": "str = gets\nwords = str.split(/,|;/)\nwords[words.length-1] = words[words.length-1][0...words[words.length-1].length-1]\nnums = []\nothers = []\nwords.each do |word|\n\tif word.to_i.to_s == word\n\t\tnums.push(word)\n\telse\n\t\tothers.push(word)\n\tend\t\t\nend\nif nums.empty?\n\tputs \"-\"\nelse \n\tputs \"\\\"#{nums.join(',')}\\\"\"\nend\nif others.empty?\n\tputs \"-\"\nelse\n\tputs \"\\\"#{others.join(',')}\\\"\"\nend"}], "negative_code": [{"source_code": "puts gets.chomp.tr(';',',').split(',').partition{|e|e=~/^-?([1-9][0-9]*|0)$/}.map{|e|e.empty? ? '-' : e*','}"}, {"source_code": "puts gets.chomp.tr(';',',').split(',').partition{|e|e=~/^-?([1-9][0-9]*|0)$/}.map{|e|e.empty? ? '-' : '\"'+e*','+'\"'}"}, {"source_code": "str = gets\nstr = str[0...str.length-1]\nwords = str.split(/,|;/)\nnums = []\nothers = []\nwords.each do |word|\n\tif word.to_i.to_s == word\n\t\tnums.push(word)\n\telse\n\t\tothers.push(word)\n\tend\t\t\nend\nif nums.empty?\n\tputs \"-\"\nelse \n\tputs \"\\\"#{nums.join(',')}\\\"\"\nend\nif others.empty?\n\tputs \"-\"\nelse\n\tputs \"\\\"#{others.join(',')}\\\"\"\nend"}], "src_uid": "ad02cead427d0765eb642203d13d3b99"} {"nl": {"description": "You are given a integer $$$n$$$ ($$$n > 0$$$). Find any integer $$$s$$$ which satisfies these conditions, or report that there are no such numbers:In the decimal representation of $$$s$$$: $$$s > 0$$$, $$$s$$$ consists of $$$n$$$ digits, no digit in $$$s$$$ equals $$$0$$$, $$$s$$$ is not divisible by any of it's digits. ", "input_spec": "The input consists of multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 400$$$), the number of test cases. The next $$$t$$$ lines each describe a test case. Each test case contains one positive integer $$$n$$$ ($$$1 \\leq n \\leq 10^5$$$). It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.", "output_spec": "For each test case, print an integer $$$s$$$ which satisfies the conditions described above, or \"-1\" (without quotes), if no such number exists. If there are multiple possible solutions for $$$s$$$, print any solution.", "sample_inputs": ["4\n1\n2\n3\n4"], "sample_outputs": ["-1\n57\n239\n6789"], "notes": "NoteIn the first test case, there are no possible solutions for $$$s$$$ consisting of one digit, because any such solution is divisible by itself.For the second test case, the possible solutions are: $$$23$$$, $$$27$$$, $$$29$$$, $$$34$$$, $$$37$$$, $$$38$$$, $$$43$$$, $$$46$$$, $$$47$$$, $$$49$$$, $$$53$$$, $$$54$$$, $$$56$$$, $$$57$$$, $$$58$$$, $$$59$$$, $$$67$$$, $$$68$$$, $$$69$$$, $$$73$$$, $$$74$$$, $$$76$$$, $$$78$$$, $$$79$$$, $$$83$$$, $$$86$$$, $$$87$$$, $$$89$$$, $$$94$$$, $$$97$$$, and $$$98$$$.For the third test case, one possible solution is $$$239$$$ because $$$239$$$ is not divisible by $$$2$$$, $$$3$$$ or $$$9$$$ and has three digits (none of which equals zero)."}, "positive_code": [{"source_code": "#!usr/bin/env ruby\n\n# https://codeforces.com/problemset/problem/1326/A\n\nn = gets.to_i\n\nn.times do\n a = gets.to_i\n\n if a == 1\n puts '-1'\n else\n puts ('9' * (a - 1) + '8')\n end\nend\n"}, {"source_code": "T = gets.to_i\n\nT.times do\n n = gets.to_i\n\n if n == 1\n puts -1\n else\n puts '2' + '3' * (n - 1)\n end\nend\n"}, {"source_code": "def main\n t = gets.chomp.to_i\n t.times { solve }\nend\n\ndef solve\n n = gets.chomp.to_i\n # a, b, c = gets.chomp.split.map(&:to_i)\n # S = gets.chomp.chars\n # p n\n # p [a, b, c]\n if n == 1\n p(-1)\n else\n ans = \"5\" * (n-1)\n ans += \"4\"\n print (ans + \"\\n\")\n end\nend\n\nmain\n"}, {"source_code": "t = gets.to_i\nt.times do\n n = gets.to_i\n if n == 1\n puts -1\n else\n case n % 3\n when 0\n puts \"2\" * (n - 1) + \"3\"\n when 1\n puts \"2\" * (n - 2) + \"33\"\n when 2\n puts \"2\" * (n - 1) + \"3\"\n end\n end\nend\n"}, {"source_code": "1.upto(gets.to_i) do\n n = gets.to_i\n\n if n == 1\n puts -1\n next\n end\n\n if (2*(n-1) + 3).modulo(3).zero?\n puts '2' * (n-2) + '33'\n else\n puts '2' * (n-1) + '3'\n end\nend\n"}], "negative_code": [{"source_code": "def main\n t = gets.chomp.to_i\n t.times { solve }\nend\n\ndef solve\n n = gets.chomp.to_i\n # a, b, c = gets.chomp.split.map(&:to_i)\n # S = gets.chomp.chars\n # p n\n # p [a, b, c]\n if n == 1\n p(-1)\n else\n ans = \"2\" * (n-1)\n ans += \"9\"\n print (ans + \"\\n\")\n end\nend\n\nmain\n"}], "src_uid": "43996d7e052aa628a46d03086f9c5436"} {"nl": {"description": "There is a field divided into $$$n$$$ rows and $$$m$$$ columns. Some cells are empty (denoted as E), other cells contain robots (denoted as R).You can send a command to all robots at the same time. The command can be of one of the four types: move up; move right; move down; move left. When you send a command, all robots at the same time attempt to take one step in the direction you picked. If a robot tries to move outside the field, it explodes; otherwise, every robot moves to an adjacent cell in the chosen direction.You can send as many commands as you want (possibly, zero), in any order. Your goal is to make at least one robot reach the upper left corner of the field. Can you do this without forcing any of the robots to explode?", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 5000$$$) — the number of test cases. Each test case starts with a line containing two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n, m \\le 5$$$) — the number of rows and the number of columns, respectively. Then $$$n$$$ lines follow; each of them contains a string of $$$m$$$ characters. Each character is either E (empty cell} or R (robot). Additional constraint on the input: in each test case, there is at least one robot on the field.", "output_spec": "If it is possible to make at least one robot reach the upper left corner of the field so that no robot explodes, print YES. Otherwise, print NO.", "sample_inputs": ["6\n\n1 3\n\nERR\n\n2 2\n\nER\n\nRE\n\n2 2\n\nER\n\nER\n\n1 1\n\nR\n\n4 3\n\nEEE\n\nEEE\n\nERR\n\nEER\n\n3 3\n\nEEE\n\nEER\n\nREE"], "sample_outputs": ["YES\nNO\nYES\nYES\nYES\nNO"], "notes": "NoteExplanations for test cases of the example: in the first test case, it is enough to send a command to move left. in the second test case, if you try to send any command, at least one robot explodes. in the third test case, it is enough to send a command to move left. in the fourth test case, there is already a robot in the upper left corner. in the fifth test case, the sequence \"move up, move left, move up\" leads one robot to the upper left corner; in the sixth test case, if you try to move any robot to the upper left corner, at least one other robot explodes. "}, "positive_code": [{"source_code": "def get_top_left_robot(a)\n a.each_with_index do |sub, r|\n sub.each_char.with_index do |v, c|\n if v == 'R'\n return r, c\n end\n end\n end\nend\n\ndef move(a, n, m)\n row, col = get_top_left_robot(a)\n\n (row+1..n-1).each do |r|\n (0..col-1).each do |c|\n return \"NO\" if a[r][c] == 'R'\n end\n end\n\n \"YES\"\nend\n\nt = gets.chomp.to_i\nans = []\n\nt.times do\n n, m = gets.chomp.split.map(&:to_i)\n a = []\n n.times do\n a << gets.chomp\n end\n\n ans << move(a, n, m)\nend\n\nputs ans"}, {"source_code": "gets.to_i.times do\r\n n, m = gets.split.map(&:to_i)\r\n xxs = n.times.map { gets.chomp }\r\n i, j = xxs.filter_map.with_index do |xs, i|\r\n if j = xs.index(\"R\")\r\n [i, j]\r\n end\r\n end.first\r\n if xxs[...i].join.include?(\"R\") || xxs.map { |xs| xs[...j] }.join.include?(\"R\")\r\n puts \"NO\"\r\n else\r\n puts \"YES\"\r\n end\r\nend\r\n"}, {"source_code": "def solve(n,m,a)\n 0.upto(n-1) do |i|\n 0.upto(m-1) do |j|\n if a[i][j] == 'R' then\n c = 0\n 0.upto(n-1) do |k|\n 0.upto(m-1) do |l|\n if a[k][l] == 'R' and ((k 0 && even > 0 ? \"YES\" : \"NO\"\n\tend\nend"}, {"source_code": "gets.to_i.times {\n gets\n counts = Array.new(2, 0)\n gets.split.map { |v|\n v = v.to_i\n counts[v % 2] += 1\n }\n if counts[1] == 0 or counts[0] == 0 and counts[1] % 2 == 0\n puts \"NO\"\n else\n puts \"YES\"\n end\n}"}, {"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\n\nt = gets.to_i\nt.times {\n n = gets.to_i\n a = gets.split.map(&:to_i)\n evens, odds = a.count(&:even?), a.count(&:odd?)\n if odds.odd?\n puts \"YES\"\n else\n if evens > 0 && odds > 0\n puts \"YES\"\n else\n puts \"NO\"\n end\n end\n}"}, {"source_code": "1.upto(gets.to_i) do\n gets\n a = gets.split(' ').map(&:to_i)\n\n result = a.inject(&:+).odd? || (a.any?(&:odd?) && a.any?(&:even?))\n\n puts result ? 'YES' : 'NO'\nend\n"}, {"source_code": "t = gets.to_i\n(1).upto(t) do\n n = gets.to_i\n a = gets.split(' ').map(&:to_i)\n odd=false\n even = false\n a.each do |i|\n odd = true if(i&1)==1\n even = true if(i&1)==0\n end\n if odd and even\n puts \"YES\"\n elsif odd and (n&1)==1\n puts\"YES\"\n else\n puts \"NO\"\n end\nend\n"}], "negative_code": [{"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\n\nt = gets.to_i\nt.times {\n n = gets.to_i\n a = gets.split.map(&:to_i)\n evens, odds = a.count(&:even?), a.count(&:odd?)\n if odds.odd?\n puts \"YES\"\n else\n puts \"NO\"\n end\n}"}], "src_uid": "2e8f7f611ba8d417fb7d12fda22c908b"} {"nl": {"description": "Ringo found a string $$$s$$$ of length $$$n$$$ in his yellow submarine. The string contains only lowercase letters from the English alphabet. As Ringo and his friends love palindromes, he would like to turn the string $$$s$$$ into a palindrome by applying two types of operations to the string. The first operation allows him to choose $$$i$$$ ($$$2 \\le i \\le n-1$$$) and to append the substring $$$s_2s_3 \\ldots s_i$$$ ($$$i - 1$$$ characters) reversed to the front of $$$s$$$.The second operation allows him to choose $$$i$$$ ($$$2 \\le i \\le n-1$$$) and to append the substring $$$s_i s_{i + 1}\\ldots s_{n - 1}$$$ ($$$n - i$$$ characters) reversed to the end of $$$s$$$.Note that characters in the string in this problem are indexed from $$$1$$$.For example suppose $$$s=$$$abcdef. If he performs the first operation with $$$i=3$$$ then he appends cb to the front of $$$s$$$ and the result will be cbabcdef. Performing the second operation on the resulted string with $$$i=5$$$ will yield cbabcdefedc.Your task is to help Ringo make the entire string a palindrome by applying any of the two operations (in total) at most $$$30$$$ times. The length of the resulting palindrome must not exceed $$$10^6$$$It is guaranteed that under these constraints there always is a solution. Also note you do not have to minimize neither the number of operations applied, nor the length of the resulting string, but they have to fit into the constraints.", "input_spec": "The only line contains the string $$$S$$$ ($$$3 \\le |s| \\le 10^5$$$) of lowercase letters from the English alphabet.", "output_spec": "The first line should contain $$$k$$$ ($$$0\\le k \\le 30$$$)  — the number of operations performed. Each of the following $$$k$$$ lines should describe an operation in form L i or R i. $$$L$$$ represents the first operation, $$$R$$$ represents the second operation, $$$i$$$ represents the index chosen. The length of the resulting palindrome must not exceed $$$10^6$$$.", "sample_inputs": ["abac", "acccc", "hannah"], "sample_outputs": ["2\nR 2\nR 5", "2\nL 4\nL 2", "0"], "notes": "NoteFor the first example the following operations are performed:abac $$$\\to$$$ abacab $$$\\to$$$ abacabaThe second sample performs the following operations: acccc $$$\\to$$$ cccacccc $$$\\to$$$ ccccaccccThe third example is already a palindrome so no operations are required."}, "positive_code": [{"source_code": "S = gets.chomp\nn = S.size\nans = []\n\nif S[0] != S[2]\n ans << \"L 2\"\n n += 1\nend\n\nans << \"R 2\"\nn += n - 2\nans << \"R #{n - 1}\"\n\nputs ans.size\nputs ans\n"}], "negative_code": [{"source_code": "S = gets.chomp\nn = S.size\n\nif S[0] != S[2]\n puts \"L 2\"\n n += 1\nend\n\nputs \"R 2\"\nn += n - 2\nputs \"R #{n - 1}\"\n"}], "src_uid": "6ca35987757bf64860eb08f98a9e6d90"} {"nl": {"description": "You are given two integers $$$l$$$ and $$$r$$$, $$$l\\le r$$$. Find the largest possible value of $$$a \\bmod b$$$ over all pairs $$$(a, b)$$$ of integers for which $$$r\\ge a \\ge b \\ge l$$$.As a reminder, $$$a \\bmod b$$$ is a remainder we get when dividing $$$a$$$ by $$$b$$$. For example, $$$26 \\bmod 8 = 2$$$.", "input_spec": "Each test contains multiple test cases. The first line contains one positive integer $$$t$$$ $$$(1\\le t\\le 10^4)$$$, denoting the number of test cases. Description of the test cases follows. The only line of each test case contains two integers $$$l$$$, $$$r$$$ ($$$1\\le l \\le r \\le 10^9$$$).", "output_spec": "For every test case, output the largest possible value of $$$a \\bmod b$$$ over all pairs $$$(a, b)$$$ of integers for which $$$r\\ge a \\ge b \\ge l$$$.", "sample_inputs": ["4\n1 1\n999999999 1000000000\n8 26\n1 999999999"], "sample_outputs": ["0\n1\n12\n499999999"], "notes": "NoteIn the first test case, the only allowed pair is $$$(a, b) = (1, 1)$$$, for which $$$a \\bmod b = 1 \\bmod 1 = 0$$$.In the second test case, the optimal choice is pair $$$(a, b) = (1000000000, 999999999)$$$, for which $$$a \\bmod b = 1$$$."}, "positive_code": [{"source_code": "t = gets.to_i\nt.times do\n\nl, r = gets.split.map!(&:to_i)\n\nif l * 2 <= r\n puts r.pred / 2\nelse\n puts r - l\nend\n\nend\n"}], "negative_code": [], "src_uid": "c34db7897f051b0de2f49f2696c6ee2f"} {"nl": {"description": "You are given N points on a plane. Write a program which will find the sum of squares of distances between all pairs of points.", "input_spec": "The first line of input contains one integer number N (1 ≤ N ≤ 100 000) — the number of points. Each of the following N lines contain two integer numbers X and Y ( - 10 000 ≤ X, Y ≤ 10 000) — the coordinates of points. Two or more points may coincide.", "output_spec": "The only line of output should contain the required sum of squares of distances between all pairs of points.", "sample_inputs": ["4\n1 1\n-1 -1\n1 -1\n-1 1"], "sample_outputs": ["32"], "notes": null}, "positive_code": [{"source_code": "def solve(x,n)\n sum=0\n ans=0\n x.each{|i| sum+=i}\n x.each{|i|\n ans+=(n-1)*i*i-(sum-i)*i\n }\n ans\nend\n\nn=gets.to_i\nx=Array.new(n,0)\ny=Array.new(n,0)\nn.times{|i|\n input=gets.split.map(&:to_i)\n x[i]=input[0]\n y[i]=input[1]\n}\n\np solve(x,n)+solve(y,n)\n"}, {"source_code": "#Infinity\n#geti,getia\n#Object # dcopy\n#\n#String # to_a\n# \"abc\".to_a #=> [\"a\",\"b\",\"c\"]\n#String # to_ia\n# \"1 2 3\".to_ia #=> [1,2,3]\n#\n#Array # rsort,rsort!\n# [1,3,2,4].rsort #=> [4,3,2,1]\n#\n#Integer # divisor\n# 24.divisor #=> [1,2,3,4,6,8,12,24]\n#\ninclude Math\nrequire \"prime\"\nrequire \"bigdecimal\"\nrequire \"bigdecimal/math\"\nrequire \"matrix\"\nrequire \"pp\"\nrequire \"benchmark\"\n\nInfinity = 1/0.0\nclass Matrix;def inspect;empty? ? \"Matrix.empty(#{row_size}, #{column_size})\":\"Matrix[\\n#{@rows.map(&:inspect).join(\"\\n\")}\\n]\";end;end\ndef pbin(obj);case obj;when Integer;puts obj.to_s(2);end;end\nclass String;def to_a;split(\"\");end;end\nclass String;def to_ia;split.map(&:to_i);end;end\ndef getia;gets.to_ia;end\ndef geti;gets.to_i;end\nclass Object;def dcopy;Marshal.load(Marshal.dump(self));end;end\nclass Array;def rsort;sort.reverse;end;def rsort!;sort!.reverse!;end;end\n\nn = geti\npx = []\npy = []\nres = 0\nn.times{\n p = getia\n res += (p[0]**2 + p[1]**2) * (n - 1)\n px.push p[0]\n py.push p[1]\n}\nxs = px.inject(&:+)\nxx = 0\npx.each{|x| xx += x * (xs - x) }\nys = py.inject(&:+)\nyy = 0\npy.each{|y| yy += y * (ys - y) }\n\np res - xx - yy\n"}, {"source_code": "n=gets.to_i\nsigx1,sigx2,sigy1,sigy2=[0,0,0,0]\nn.times do\n a,b=gets.split.map(&:to_i)\n sigx1+=a\n sigx2+=a*a\n sigy1+=b\n sigy2+=b*b\nend\np n*(sigx2+sigy2)-sigx1**2-sigy1**2"}], "negative_code": [], "src_uid": "9bd6fea892857d7c94ebce4d4cd46d30"} {"nl": {"description": "Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder.", "input_spec": "The first line contains an integer n (1 ≤ n ≤ 1000).", "output_spec": "On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any.", "sample_inputs": ["2"], "sample_outputs": ["2\nC.\n.C"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\ndx = Array.new(n).map{|v| v = Array.new(n, 0)}\nfor i in 0...n do\n for j in 0...n do\n if i % 2 == 0\n dx[i][j] = (j % 2 == 0 ? \"C\" : \".\")\n else\n dx[i][j] = (j % 2 == 0 ? \".\" : \"C\")\n end\n end\nend\ncnt = 0\nfor i in 0...n do\n for j in 0...n do\n cnt += 1 if dx[i][j] == 'C'\n end\nend\nputs cnt\nfor i in 0...n do\n puts dx[i].join()\nend\n"}, {"source_code": "n = STDIN.gets.to_i\nv1 = (n.to_f / 2).floor\nv2 = (n.to_f / 2).ceil\nputs v1*v1 + v2*v2\nlineCoder = true\n(0...n).each do\n colCoder = lineCoder\n (0...n).each do\n print(colCoder ? 'C' : '.')\n colCoder = !colCoder\n end\n puts\n lineCoder = !lineCoder\nend\n"}, {"source_code": "n = gets.chomp().to_i\nif (n % 2 == 0)\n\tputs(n*n/2)\nelse\n\tputs((n-1)*n/2 + (n+1)/2)\nend\nfor i in 1..n\n\tfor j in 1..n\n\t\tif i%2 == j%2\n\t\t\tprint 'C' \n\t\telse\n\t\t\tprint '.'\n\t\tend\n\tend\n\tputs\nend"}, {"source_code": "n = gets.to_i\nres = n ** 2 / 4 + (n + 1) / 2 * (n / 2)\nputs n.even? ? res : res + 1\nn.times do |i|\n if i.even?\n puts (\"C.\" * ((n + 1) / 2)).slice(0..n - 1)\n else\n puts (\".C\" * ((n + 1) / 2)).slice(0..n - 1)\n end\nend\n"}, {"source_code": "n = gets.to_i\n\nans = []\nn.times do |i|\n if i.even?\n ans << \"C.\" * (n/2) + \"C\" * (n%2)\n else\n ans << \".C\" * (n/2) + \".\" * (n%2)\n end\nend\nputs n ** 2 / 2 + n % 2\nputs ans"}, {"source_code": "n=gets.to_i\nif n%2==0 then\n\tputs n/2*n\nelse\n\tputs n*(n/2)+(n/2+1)\nend\n\nn.times{|i|\n\tn.times{|j|\n\t\tif (i+j)%2==0 then\n\t\t\tprint \"C\"\n\t\telse\n\t\t\tprint \".\"\n\t\tend\n\t}\n\tputs \"\"\n}\n\n"}, {"source_code": "n = STDIN.gets.to_i\nv1 = (n.to_f / 2).floor\nv2 = (n.to_f / 2).ceil\nputs v1*v1 + v2*v2\nlineCoder = true\n(0...n).each do\n colCoder = lineCoder\n (0...n).each do\n print(colCoder ? 'C' : '.')\n colCoder = !colCoder\n end\n puts\n lineCoder = !lineCoder\nend"}, {"source_code": "n = gets.to_i\nans = Array.new(n) {Array.new(n) {'.'}}\ncount = 0\nn.times do |i|\n n.times do |j|\n if (i+j)%2==0\n ans[i][j] = 'C'\n count+=1\n end\n end\nend\np count\nans.each{|x| puts x.join('')}"}, {"source_code": "n = gets.to_i\na = ['C', '.']\ns = []\n(n / 2 + 1).times do\n s << a\nend\ns = s.join(\"\").chars\nputs (n * n + 1) / 2\nn.times do |i|\n puts s[(i % 2)...(i % 2 + n)].join(\"\")\nend\n"}, {"source_code": "n = gets.to_i\nputs ((n + 1)/2)**2 + (n/2)**2\nn.times do |i|\n n.times do |j|\n print i%2 == j%2 ? 'C' : '.'\n end\n puts\nend\n"}, {"source_code": "n=gets.to_i\ns=[]\ns[0]='C.'*(n/2)+(n%2==1?'C':'')\ns[1]=('.'+s[0])[0..-2]\na=s.map{|x|x.count('C')}\nc=0\nn.times do |i|\n c+=a[i%2]\nend\nputs c\nn.times do |i|\n puts s[i%2]\nend\n\n"}, {"source_code": "n = gets.chomp.to_i\n\nnumber = 0\t\t\n\n(0...n).each do |row|\n\t(0...n).each do |col|\n\t\tif row % 2 == 0\n\t\t\tnumber += 1 if col % 2 == 0\n\t\telse\n\t\t\tnumber += 1 if col % 2 == 1\n\t\tend\n\tend\nend\n\nputs number\n\n(0...n).each do |row|\n\t(0...n).each do |col|\n\t\tif row % 2 == 0\n\t\t\tprint 'C' if col % 2 == 0\n\t\t\tprint '.' if col % 2 == 1\n\t\telse\n\t\t\tprint 'C' if col % 2 == 1\n\t\t\tprint '.' if col % 2 == 0\n\t\tend\n\tend\n\n\tputs ''\nend"}, {"source_code": "n = gets.to_i\nputs (n*n+1)/2\n(1..n).each_with_index do |x,id|\n if x % 2 == 1\n puts \"C.\" * (n/2) + \"C\" * (n%2)\n else\n puts \".C\" * (n/2) + \".\" * (n%2)\n end\nend"}], "negative_code": [{"source_code": "n = gets.to_i\ndx = Array.new(n).map{|v| v = Array.new(n, 0)}\nfor i in 0...n do\n for j in 0...n do\n if i % 2 == 0\n dx[i][j] = (j % 2 == 0 ? \"C\" : \".\")\n else\n dx[i][j] = (j % 2 == 0 ? \".\" : \"C\")\n end\n end\nend\ncnt = 0\nfor i in 0...n do\n for i in 0...n do\n cnt += 1 if dx[i][j] == 'C'\n end\nend\nputs cnt\nfor i in 0...n do\n puts dx[i].join()\nend\n"}, {"source_code": "n = STDIN.gets.to_i\nv1 = (n / 2).floor\nv2 = (n / 2).ceil\nputs v1*v1 + v2*v2\nline_coder = true\n(0...n).each do\n col_coder = line_coder\n (0...n).each do\n print(col_coder ? 'C' : '.')\n col_coder = !col_coder\n end\n puts\n line_coder = !line_coder\nend\n"}, {"source_code": "n = gets.to_i\nn.times do |i|\n if i.even?\n puts (\"C.\" * ((n + 1) / 2)).slice(0..n - 1)\n else\n puts (\".C\" * ((n + 1) / 2)).slice(0..n - 1)\n end\nend\n"}, {"source_code": "n=gets.to_i\nputs n\nn.times{|i|\n\tn.times{|j|\n\t\tif i==j then\n\t\t\tprint \"C\"\n\t\telse\n\t\t\tprint \".\"\n\t\tend\n\t}\n\tputs \"\"\n}"}, {"source_code": "n = gets.to_i\nans = Array.new(n) {Array.new(n) {'.'}}\nn.times do |i|\n n.times do |j|\n if (i+j)%2==0\n ans[i][j] = 'C'\n end\n end\nend\nans.each{|x| puts x.join('')}\n"}, {"source_code": "n = gets.chomp.to_i\n\nif n == 1\n\tputs \"1\"\nend\n\n\nnumber = 0\t\t\n\n(0...n).each do |row|\n\t(0...n).each do |col|\n\t\tif row % 2 == 0\n\t\t\tnumber += 1 if col % 2 == 0\n\t\telse\n\t\t\tnumber += 1 if col % 2 == 1\n\t\tend\n\tend\nend\n\nputs number\n\n(0...n).each do |row|\n\t(0...n).each do |col|\n\t\tif row % 2 == 0\n\t\t\tprint 'C' if col % 2 == 0\n\t\t\tprint '.' if col % 2 == 1\n\t\telse\n\t\t\tprint 'C' if col % 2 == 1\n\t\t\tprint '.' if col % 2 == 0\n\t\tend\n\tend\n\n\tputs ''\nend"}, {"source_code": "n = gets.to_i\n(1..n).each_with_index do |x,id|\n if x % 2 == 1\n puts \"C.\" * (n/2) + \"C\" * (n%2)\n else\n puts \".C\" * (n/2) + \".\" * (n%2)\n end\nend"}], "src_uid": "1aede54b41d6fad3e74f24a6592198eb"} {"nl": {"description": "This is an interactive problem!Ehab plays a game with Laggy. Ehab has 2 hidden integers $$$(a,b)$$$. Laggy can ask a pair of integers $$$(c,d)$$$ and Ehab will reply with: 1 if $$$a \\oplus c>b \\oplus d$$$. 0 if $$$a \\oplus c=b \\oplus d$$$. -1 if $$$a \\oplus c<b \\oplus d$$$. Operation $$$a \\oplus b$$$ is the bitwise-xor operation of two numbers $$$a$$$ and $$$b$$$.Laggy should guess $$$(a,b)$$$ with at most 62 questions. You'll play this game. You're Laggy and the interactor is Ehab.It's guaranteed that $$$0 \\le a,b<2^{30}$$$.", "input_spec": "See the interaction section.", "output_spec": "To print the answer, print \"! a b\" (without quotes). Don't forget to flush the output after printing the answer.", "sample_inputs": ["1\n-1\n0"], "sample_outputs": ["? 2 1\n? 1 2\n? 2 0\n! 3 1"], "notes": "NoteIn the sample:The hidden numbers are $$$a=3$$$ and $$$b=1$$$.In the first query: $$$3 \\oplus 2 = 1$$$ and $$$1 \\oplus 1 = 0$$$, so the answer is 1.In the second query: $$$3 \\oplus 1 = 2$$$ and $$$1 \\oplus 2 = 3$$$, so the answer is -1.In the third query: $$$3 \\oplus 2 = 1$$$ and $$$1 \\oplus 0 = 1$$$, so the answer is 0.Then, we printed the answer."}, "positive_code": [{"source_code": "a = b = 0\nputs \"? 0 0\"\nSTDOUT.flush\ns = gets.to_i\nl = s > 0 ? true : false\nf = false\nf = true if s == 0\n29.downto(0) do |i|\n break if f\n puts \"? #{a + (1 << i)} #{b + (1 << i)}\"\n STDOUT.flush\n s = gets.to_i\n if s == 0\n f = true\n elsif ((s == -1) && l) || ((s == 1) && !l)\n if s == -1\n a += 1 << i\n else\n b += 1 << i\n end\n puts \"? #{a} #{b}\"\n STDOUT.flush\n s = gets.to_i\n l = s > 0 ? true : false\n f = true if s == 0\n end\nend\n29.downto(0) do |i|\n next if a[i] == 1 || b[i] == 1\n puts \"? #{a} #{b+(1 << i)}\"\n STDOUT.flush\n s = gets.to_i\n if (s == 1)\n a += 1 << i\n b += 1 << i\n end\nend\nputs \"! #{a} #{b}\""}, {"source_code": "ahyp = \"X\"*30\nbhyp = \"X\"*30\ncheckedi = 0\n\nputs \"? \" + \"0\" + \" \" + \"0\"\nSTDOUT.flush\nans = gets.chomp.to_i\n\nif ans==1\n aisgreater = true\n theyareequal = false\nelsif ans==-1\n aisgreater = false\n theyareequal = false\nelse\n aisgreater = false\n theyareequal = true\nend \n\npreva = \"\"\nprevb = \"\"\n\n61.times do\n\n# p ahyp\n# p bhyp\n\n if aisgreater and not theyareequal\n puts \"? \" + (preva+\"1\"+\"0\"*(29-checkedi)).to_i(2).to_s + \" \" + (prevb+\"0\"+\"0\"*(29-checkedi)).to_i(2).to_s\n STDOUT.flush\n res = gets.to_i\n\n if res==1\n puts \"? \" + (preva+\"0\"+\"0\"*(29-checkedi)).to_i(2).to_s + \" \" + (prevb+\"1\"+\"0\"*(29-checkedi)).to_i(2).to_s\n STDOUT.flush\n newres = gets.to_i\n\n if newres ==1\n ahyp[checkedi] = \"1\"\n bhyp[checkedi] = \"0\"\n aisgreater = true\n #ALAT > BLAT\n elsif newres==-1\n ahyp[checkedi] = \"0\"\n bhyp[checkedi] = \"0\"\n aisgreater = true\n #ALAT > BLAT\n end\n elsif res==-1\n puts \"? \" + (preva+\"0\"+\"0\"*(29-checkedi)).to_i(2).to_s + \" \" + (prevb+\"1\"+\"0\"*(29-checkedi)).to_i(2).to_s\n STDOUT.flush\n newres = gets.to_i\n\n if newres ==1\n ahyp[checkedi] = \"1\"\n bhyp[checkedi] = \"1\"\n aisgreater = true\n #ALAT>BLAT\n elsif newres==-1\n ahyp[checkedi] = \"1\"\n bhyp[checkedi] = \"0\"\n aisgreater = false\n #ALATBLAT\n elsif newres==-1\n ahyp[checkedi] = \"1\"\n bhyp[checkedi] = \"1\"\n aisgreater = false\n #ALAT 0 ? true : false\nf = false\nf = true if s == 0\n30.downto(0) do |i|\n break if f\n puts \"? #{a + (1 << i)} #{b + (1 << i)}\"\n STDOUT.flush\n s = gets.to_i\n if s == 0\n f = true\n elsif ((s == -1) && l) || ((s == 1) && !l)\n if s == -1\n a += 1 << i\n else\n b += 1 << i\n end\n puts \"? #{a} #{b}\"\n STDOUT.flush\n s = gets.to_i\n l = s > 0 ? true : false\n f = true if s == 0\n end\nend\n30.downto(0) do |i|\n next if a[i] == 1 || b[i] == 1\n puts \"? #{a} #{b+(1 << i)}\"\n STDOUT.flush\n s = gets.to_i\n if (s == 1)\n a += 1 << i\n b += 1 << i\n end\nend\nputs \"! #{a} #{b}\""}, {"source_code": "a = b = 0\nputs \"? 0 0\"\nSTDOUT.flush\ns = gets.to_i\nl = s > 0 ? true : false\nf = false\nf = true if s == 0\n30.downto(0) do |i|\n break if f\n puts \"? #{a + (1 << i)} #{b + (1 << i)}\"\n STDOUT.flush\n s = gets.to_i\n if s == 0\n f = true\n elsif (s == -1) && l\n a += 1 << i\n puts \"? #{a} #{b}\"\n STDOUT.flush\n s = gets.to_i\n l = s > 0 ? true : false\n f = true if s == 0\n elsif (s == 1) && !l\n b += 1 << i\n end\nend\n30.downto(0) do |i|\n next if a[i] == 1 || b[i] == 1\n puts \"? #{a} #{b+(1 << i)}\"\n STDOUT.flush\n s = gets.to_i\n if (s == 1)\n a += 1 << i\n b += 1 << i\n end\nend\nputs \"! #{a} #{b}\""}, {"source_code": "ahyp = \"X\"*30\nbhyp = \"X\"*30\ncheckedi = 0\n\nputs \"? \" + \"0\" + \" \" + \"0\"\nSTDOUT.flush\nans = gets.chomp.to_i\n\nif ans==1\n aisgreater = true\n theyareequal = true\nelsif ans==-1\n aisgreater = false\n theyareequal = true\nelse\n aisgreater = false\n theyareequal = true\nend \n\npreva = \"\"\nprevb = \"\"\n\n61.times do\n\n if aisgreater and not theyareequal\n puts \"? \" + (preva+\"1\"+\"0\"*(29-checkedi)).to_i(2).to_s + \" \" + (prevb+\"0\"+\"0\"*(29-checkedi)).to_i(2).to_s\n STDOUT.flush\n res = gets.to_i\n\n if res==1\n puts \"? \" + (preva+\"0\"+\"0\"*(29-checkedi)).to_i(2).to_s + \" \" + (prevb+\"1\"+\"0\"*(29-checkedi)).to_i(2).to_s\n STDOUT.flush\n newres = gets.to_i\n\n if newres ==1\n ahyp[checkedi] = \"1\"\n bhyp[checkedi] = \"0\"\n aisgreater = true\n #ALAT > BLAT\n elsif newres==-1\n ahyp[checkedi] = \"0\"\n bhyp[checkedi] = \"0\"\n aisgreater = true\n #ALAT > BLAT\n end\n elsif res==-1\n if newres ==1\n ahyp[checkedi] = \"1\"\n bhyp[checkedi] = \"1\"\n aisgreater = true\n #ALAT>BLAT\n elsif newres==-1\n ahyp[checkedi] = \"1\"\n bhyp[checkedi] = \"0\"\n aisgreater = false\n #ALATBLAT\n elsif newres==-1\n ahyp[checkedi] = \"1\"\n bhyp[checkedi] = \"1\"\n aisgreater = false\n #ALAT BLAT\n elsif newres==-1\n ahyp[checkedi] = \"0\"\n bhyp[checkedi] = \"0\"\n aisgreater = true\n #ALAT > BLAT\n end\n elsif res==-1\n puts \"? \" + (preva+\"0\"+\"0\"*(29-checkedi)).to_i(2).to_s + \" \" + (prevb+\"1\"+\"0\"*(29-checkedi)).to_i(2).to_s\n STDOUT.flush\n newres = gets.to_i\n\n if newres ==1\n ahyp[checkedi] = \"1\"\n bhyp[checkedi] = \"1\"\n aisgreater = true\n #ALAT>BLAT\n elsif newres==-1\n ahyp[checkedi] = \"1\"\n bhyp[checkedi] = \"0\"\n aisgreater = false\n #ALATBLAT\n elsif newres==-1\n ahyp[checkedi] = \"1\"\n bhyp[checkedi] = \"1\"\n aisgreater = false\n #ALAT BLAT\n elsif newres==-1\n ahyp[checkedi] = \"0\"\n bhyp[checkedi] = \"0\"\n aisgreater = true\n #ALAT > BLAT\n end\n elsif res==-1\n puts \"? \" + (preva+\"0\"+\"0\"*(29-checkedi)).to_i(2).to_s + \" \" + (prevb+\"1\"+\"0\"*(29-checkedi)).to_i(2).to_s\n STDOUT.flush\n newres = gets.to_i\n\n if newres ==1\n ahyp[checkedi] = \"1\"\n bhyp[checkedi] = \"1\"\n aisgreater = true\n #ALAT>BLAT\n elsif newres==-1\n ahyp[checkedi] = \"1\"\n bhyp[checkedi] = \"0\"\n aisgreater = false\n #ALATBLAT\n elsif newres==-1\n ahyp[checkedi] = \"1\"\n bhyp[checkedi] = \"1\"\n aisgreater = false\n #ALAT BLAT\n elsif newres==-1\n ahyp[checkedi] = \"0\"\n bhyp[checkedi] = \"0\"\n aisgreater = true\n #ALAT > BLAT\n end\n elsif res==-1\n puts \"? \" + (preva+\"0\"+\"0\"*(29-checkedi)).to_i(2).to_s + \" \" + (prevb+\"1\"+\"0\"*(29-checkedi)).to_i(2).to_s\n STDOUT.flush\n newres = gets.to_i\n\n if newres ==1\n ahyp[checkedi] = \"1\"\n bhyp[checkedi] = \"1\"\n aisgreater = true\n #ALAT>BLAT\n elsif newres==-1\n ahyp[checkedi] = \"1\"\n bhyp[checkedi] = \"0\"\n aisgreater = false\n #ALATBLAT\n elsif newres==-1\n ahyp[checkedi] = \"1\"\n bhyp[checkedi] = \"1\"\n aisgreater = false\n #ALAT BLAT\n elsif newres==-1\n ahyp[checkedi] = \"0\"\n bhyp[checkedi] = \"0\"\n aisgreater = true\n #ALAT > BLAT\n end\n elsif res==-1\n puts \"? \" + (preva+\"0\"+\"0\"*(29-checkedi)).to_i(2).to_s + \" \" + (prevb+\"1\"+\"0\"*(29-checkedi)).to_i(2).to_s\n STDOUT.flush\n newres = gets.to_i\n\n if newres ==1\n ahyp[checkedi] = \"1\"\n bhyp[checkedi] = \"1\"\n aisgreater = true\n #ALAT>BLAT\n elsif newres==-1\n ahyp[checkedi] = \"1\"\n bhyp[checkedi] = \"0\"\n aisgreater = false\n #ALATBLAT\n elsif newres==-1\n ahyp[checkedi] = \"1\"\n bhyp[checkedi] = \"1\"\n aisgreater = false\n #ALAT (x, y) { (y % 10) <=> (x % 10) }\na = gets.split.map(&:to_i).sort(&cmp).map do |x|\n if x % 10 == 0 || k == 0\n x\n else\n d = [10 - x % 10, k].min\n k -= d\n x += d\n end\nend\nans = 0\na.each do |y|\n d = [100 - y, k].min\n k -= d\n ans += (y + d) / 10\nend\nputs ans\n=end\n\n# D\nx, y, c = [0] * 3, [0] * 3, ['A', 'B', 'C']\nx[0], y[0], x[1], y[1], x[2], y[2] = gets.split.map(&:to_i)\nx_y_c, s = [], 0\n3.times do |i|\n s += x[i] * y[i]\n x_y_c << ([x[i], y[i]].sort.reverse + [c[i]])\nend\nx_y_c.sort!.reverse!\na = b = x_y_c[0][0]\nif a ** 2 != s\n puts -1\nelse\n puts a\n # a == x_y_c[0][0]\n x_y_c[0][1].times { puts x_y_c[0][2] * a }\n b -= x_y_c[0][1]\n if x_y_c[1][0] == b # b == x_y_c[2][0] && a == x_y_c[1][1] + x_y_c[2][1]\n b.times { puts x_y_c[1][2] * x_y_c[1][1] + x_y_c[2][2] * x_y_c[2][1] }\n else\n if x_y_c[1][0] == a # a == x_y_c[2][0] && b == x_y_c[1][1] + x_y_c[2][1]\n x_y_c[1][1].times { puts x_y_c[1][2] * a }\n x_y_c[2][1].times { puts x_y_c[2][2] * a }\n else # b == x_y_c[1][1]\n b.times { puts x_y_c[1][2] * x_y_c[1][0] + x_y_c[2][2] * (a - x_y_c[1][0]) }\n end\n end\nend\n"}, {"source_code": "# Codeforces Round\n\n=begin\n# A\na, b = gets.split.map(&:to_i)\nc, d = [a, b].min, [a, b].max\nputs \"#{[a, b].min} #{(a - b).abs / 2}\"\n\n# B\nn = gets.to_i\nh = gets.split.map(&:to_i)\nm, a = [0] * (n - 1) + [h[n - 1]], [0] * n\n(n - 2).downto(0) do |i|\n if h[i] <= m[i + 1]\n m[i] = m[i + 1]\n a[i] = m[i] - h[i] + 1\n else\n m[i] = h[i]\n end\nend\nputs a.join(' ')\n\n# C\nn, k = gets.split.map(&:to_i)\ncmp = -> (x, y) { (y % 10) <=> (x % 10) }\na = gets.split.map(&:to_i).sort(&cmp).map do |x|\n if x % 10 == 0 || k == 0\n x\n else\n d = [10 - x % 10, k].min\n k -= d\n x += d\n end\nend\nans = 0\na.each do |y|\n d = [100 - y, k].min\n k -= d\n ans += (y + d) / 10\nend\nputs ans\n=end\n\n# D\nx, y, c = [0] * 3, [0] * 3, ['A', 'B', 'C']\nx[0], y[0], x[1], y[1], x[2], y[2] = gets.split.map(&:to_i)\nx_y_c, s = [], 0\n3.times do |i|\n s += x[i] * y[i]\n x_y_c << ([x[i], y[i]].sort.reverse + [c[i]])\nend\nx_y_c.sort!.reverse!\n#p x_y_c\na = b = x_y_c[0][0]\nif a ** 2 != s\n puts -1\nelse\n puts a\n x_y_c[0][1].times { puts x_y_c[0][2] * a }\n b -= x_y_c[0][1]\n if x_y_c[1][0] == b # b == x_y_c[2][0] && a == x_y_c[1][1] + x_y_c[2][1]\n b.times { puts x_y_c[1][2] * x_y_c[1][1] + x_y_c[2][2] * x_y_c[2][1] }\n else # b == x_y_c[1][1] + x_y_c[2][1] && a == x_y_c[1][0] && a == x_y_c[2][0]\n if x_y_c[1][0] == a # a == x_y_c[2][0] && b == x_y_c[1][1] + x_y_c[2][1]\n x_y_c[1][1].times { puts x_y_c[1][2] * x_y_c[1][0] }\n x_y_c[2][1].times { puts x_y_c[2][2] * x_y_c[2][0] }\n else # b == x_y_c[1][1]\n x_y_c[1][1].times { puts x_y_c[1][2] * x_y_c[1][0] + x_y_c[2][2] * (a - x_y_c[1][0]) }\n end\n end\n \n #b.times do\n # [1, 2].each do |j|\n # #z = x_y_c[j][0] == b ? x_y_c[j][1] : x_y_c[j][]\n # print (x_y_c[j][0] == b ? x_y_c[j][2] * x_y_c[j][1] : x_y_c[j][2] * x_y_c[j][0])\n # end\n # puts\n #end\nend\n"}], "negative_code": [{"source_code": "# Codeforces Round\n\n=begin\n# A\na, b = gets.split.map(&:to_i)\nc, d = [a, b].min, [a, b].max\nputs \"#{[a, b].min} #{(a - b).abs / 2}\"\n\n# B\nn = gets.to_i\nh = gets.split.map(&:to_i)\nm, a = [0] * (n - 1) + [h[n - 1]], [0] * n\n(n - 2).downto(0) do |i|\n if h[i] <= m[i + 1]\n m[i] = m[i + 1]\n a[i] = m[i] - h[i] + 1\n else\n m[i] = h[i]\n end\nend\nputs a.join(' ')\n\n# C\nn, k = gets.split.map(&:to_i)\ncmp = -> (x, y) { (y % 10) <=> (x % 10) }\na = gets.split.map(&:to_i).sort(&cmp).map do |x|\n if x % 10 == 0 || k == 0\n x\n else\n d = [10 - x % 10, k].min\n k -= d\n x += d\n end\nend\nans = 0\na.each do |y|\n d = [100 - y, k].min\n k -= d\n ans += (y + d) / 10\nend\nputs ans\n=end\n\n# D\nx, y, c = [0] * 3, [0] * 3, ['A', 'B', 'C']\nx[0], y[0], x[1], y[1], x[2], y[2] = gets.split.map(&:to_i)\nx_y_c, s = [], 0\n3.times do |i|\n s += x[i] * y[i]\n x_y_c << ([x[i], y[i]].sort.reverse + [c[i]])\nend\nx_y_c.sort!.reverse!\n#p x_y_c\na = b = x_y_c[0][0]\nif a ** 2 != s\n puts -1\nelse\n puts a\n x_y_c[0][1].times { puts x_y_c[0][2] * a }\n b -= x_y_c[0][1]\n if x_y_c[1][0] == b # b == x_y_c[2][0]\n b.times { puts x_y_c[1][2] * x_y_c[1][1] + x_y_c[2][2] * x_y_c[2][1] }\n else # b == x_y_c[1][1] + x_y_c[2][1]\n x_y_c[1][1].times { puts x_y_c[1][2] * x_y_c[1][0] }\n x_y_c[2][1].times { puts x_y_c[2][2] * x_y_c[2][0] }\n end\n \n #b.times do\n # [1, 2].each do |j|\n # #z = x_y_c[j][0] == b ? x_y_c[j][1] : x_y_c[j][]\n # print (x_y_c[j][0] == b ? x_y_c[j][2] * x_y_c[j][1] : x_y_c[j][2] * x_y_c[j][0])\n # end\n # puts\n #end\nend\n"}, {"source_code": "# Codeforces Round\n\n=begin\n# A\na, b = gets.split.map(&:to_i)\nc, d = [a, b].min, [a, b].max\nputs \"#{[a, b].min} #{(a - b).abs / 2}\"\n\n# B\nn = gets.to_i\nh = gets.split.map(&:to_i)\nm, a = [0] * (n - 1) + [h[n - 1]], [0] * n\n(n - 2).downto(0) do |i|\n if h[i] <= m[i + 1]\n m[i] = m[i + 1]\n a[i] = m[i] - h[i] + 1\n else\n m[i] = h[i]\n end\nend\nputs a.join(' ')\n\n# C\nn, k = gets.split.map(&:to_i)\ncmp = -> (x, y) { (y % 10) <=> (x % 10) }\na = gets.split.map(&:to_i).sort(&cmp).map do |x|\n if x % 10 == 0 || k == 0\n x\n else\n d = [10 - x % 10, k].min\n k -= d\n x += d\n end\nend\nans = 0\na.each do |y|\n d = [100 - y, k].min\n k -= d\n ans += (y + d) / 10\nend\nputs ans\n=end\n\n# D\nx, y, c = [0] * 3, [0] * 3, ['A', 'B', 'C']\nx[0], y[0], x[1], y[1], x[2], y[2] = gets.split.map(&:to_i)\nx_y_c, s = [], 0\n3.times do |i|\n s += x[i] * y[i]\n x_y_c << ([x[i], y[i]].sort.reverse + [c[i]])\nend\nx_y_c.sort!.reverse!\n#p x_y_c\na = b = x_y_c[0][0]\nif a ** 2 != s\n puts -1\nelse\n puts a\n x_y_c[0][1].times { puts x_y_c[0][2] * a }\n b -= x_y_c[0][1]\n if x_y_c[1][0] == b # b == x_y_c[2][0]\n b.times { puts x_y_c[1][2] * x_y_c[1][1] + x_y_c[2][2] * x_y_c[2][1] }\n else # b == x_y_c[1][1] + x_y_c[2][1]\n x_y_c[1][1].times { print x_y_c[1][2] * x_y_c[1][0] }\n x_y_c[2][1].times { print x_y_c[2][2] * x_y_c[2][0] }\n puts\n end\n \n #b.times do\n # [1, 2].each do |j|\n # #z = x_y_c[j][0] == b ? x_y_c[j][1] : x_y_c[j][]\n # print (x_y_c[j][0] == b ? x_y_c[j][2] * x_y_c[j][1] : x_y_c[j][2] * x_y_c[j][0])\n # end\n # puts\n #end\nend\n"}, {"source_code": "# Codeforces Round\n\n=begin\n# A\na, b = gets.split.map(&:to_i)\nc, d = [a, b].min, [a, b].max\nputs \"#{[a, b].min} #{(a - b).abs / 2}\"\n\n# B\nn = gets.to_i\nh = gets.split.map(&:to_i)\nm, a = [0] * (n - 1) + [h[n - 1]], [0] * n\n(n - 2).downto(0) do |i|\n if h[i] <= m[i + 1]\n m[i] = m[i + 1]\n a[i] = m[i] - h[i] + 1\n else\n m[i] = h[i]\n end\nend\nputs a.join(' ')\n\n# C\nn, k = gets.split.map(&:to_i)\ncmp = -> (x, y) { (y % 10) <=> (x % 10) }\na = gets.split.map(&:to_i).sort(&cmp).map do |x|\n if x % 10 == 0 || k == 0\n x\n else\n d = [10 - x % 10, k].min\n k -= d\n x += d\n end\nend\nans = 0\na.each do |y|\n d = [100 - y, k].min\n k -= d\n ans += (y + d) / 10\nend\nputs ans\n=end\n\n# D\nx, y, c = [0] * 3, [0] * 3, ['A', 'B', 'C']\nx[0], y[0], x[1], y[1], x[2], y[2] = gets.split.map(&:to_i)\nx_y_c, s = [], 0\n3.times do |i|\n s += x[i] * y[i]\n x_y_c << ([x[i], y[i]].sort.reverse + [c[i]])\nend\nx_y_c.sort!.reverse!\n#p x_y_c\na = b = x_y_c[0][0]\nif a ** 2 != s\n puts -1\nelse\n puts a\n x_y_c[0][1].times { puts x_y_c[0][2] * a }\n b -= x_y_c[0][1]\n if x_y_c[1][0] == b # b == x_y_c[2][0] && a == x_y_c[1][1] + x_y_c[2][1]\n b.times { puts x_y_c[1][2] * x_y_c[1][1] + x_y_c[2][2] * x_y_c[2][1] }\n else # b == x_y_c[1][1] + x_y_c[2][1] && a == x_y_c[1][0] && a == x_y_c[2][0]\n x_y_c[1][1].times { puts x_y_c[1][2] * x_y_c[1][0] }\n x_y_c[2][1].times { puts x_y_c[2][2] * x_y_c[2][0] }\n end\n \n #b.times do\n # [1, 2].each do |j|\n # #z = x_y_c[j][0] == b ? x_y_c[j][1] : x_y_c[j][]\n # print (x_y_c[j][0] == b ? x_y_c[j][2] * x_y_c[j][1] : x_y_c[j][2] * x_y_c[j][0])\n # end\n # puts\n #end\nend\n"}], "src_uid": "2befe5da2df57d23934601cbe4d4f151"} {"nl": {"description": "You are given three integers $$$x, y$$$ and $$$n$$$. Your task is to find the maximum integer $$$k$$$ such that $$$0 \\le k \\le n$$$ that $$$k \\bmod x = y$$$, where $$$\\bmod$$$ is modulo operation. Many programming languages use percent operator % to implement it.In other words, with given $$$x, y$$$ and $$$n$$$ you need to find the maximum possible integer from $$$0$$$ to $$$n$$$ that has the remainder $$$y$$$ modulo $$$x$$$.You have to answer $$$t$$$ independent test cases. It is guaranteed that such $$$k$$$ exists for each test case.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 5 \\cdot 10^4$$$) — the number of test cases. The next $$$t$$$ lines contain test cases. The only line of the test case contains three integers $$$x, y$$$ and $$$n$$$ ($$$2 \\le x \\le 10^9;~ 0 \\le y < x;~ y \\le n \\le 10^9$$$). It can be shown that such $$$k$$$ always exists under the given constraints.", "output_spec": "For each test case, print the answer — maximum non-negative integer $$$k$$$ such that $$$0 \\le k \\le n$$$ and $$$k \\bmod x = y$$$. It is guaranteed that the answer always exists.", "sample_inputs": ["7\n7 5 12345\n5 0 4\n10 5 15\n17 8 54321\n499999993 9 1000000000\n10 5 187\n2 0 999999999"], "sample_outputs": ["12339\n0\n15\n54306\n999999995\n185\n999999998"], "notes": "NoteIn the first test case of the example, the answer is $$$12339 = 7 \\cdot 1762 + 5$$$ (thus, $$$12339 \\bmod 7 = 5$$$). It is obvious that there is no greater integer not exceeding $$$12345$$$ which has the remainder $$$5$$$ modulo $$$7$$$."}, "positive_code": [{"source_code": "gets.to_i.times do\n\tx, y, n = gets.split.map(&:to_i)\n\tif n % x >= y\n\t\tputs n / x * x + y\n\telse\n\t\tputs (n / x - 1) * x + y\n\tend\nend\n"}, {"source_code": "t = gets.to_i\nt.times do\n\tx,y,n = gets.split.map(&:to_i)\n\tans = x * (n/x) + y\n\tif ans > n\n\t\tans -= x\n\tend\n\tputs ans\nend"}, {"source_code": "def run\n x, y, n = gets.chomp.split(\" \").map(&:to_i)\n\n l = n % x\n\n # puts [x, y, n, l].inspect\n\n if l == y\n puts n\n elsif l > y\n puts n - (l - y)\n else\n puts n - (x - (y - l))\n end\nend\n\n\ndef main\n t = gets.chomp.to_i\n\n t.times do\n run\n end\nend\n\nmain\n"}, {"source_code": "n = gets.to_i\nn.times do \n x ,y, n = gets.chomp.split(\" \").map(&:to_i)\n first = n - y \n kari = first / x\n ans = kari * x + y\n puts ans\nend"}, {"source_code": "gets.to_i.times do\n x, y, n = gets.split.map(&:to_i)\n puts (n - y) / x * x + y\nend\n\n"}, {"source_code": "def answer(n, x, y)\n return answer(n - n.modulo(x) - 1, x, y) if n.modulo(x) < y\n\n return n - (n.modulo(x) - y)\nend\n\ngets.to_i.times do\n x, y, n = gets.split(' ').map(&:to_i)\n\n puts answer(n, x, y)\nend\n"}], "negative_code": [], "src_uid": "2589e832f22089fac9ccd3456c0abcec"} {"nl": {"description": "This is an interactive problem.I want to play a game with you...We hid from you a cyclic graph of $$$n$$$ vertices ($$$3 \\le n \\le 10^{18}$$$). A cyclic graph is an undirected graph of $$$n$$$ vertices that form one cycle. Each vertex belongs to the cycle, i.e. the length of the cycle (the number of edges in it) is exactly $$$n$$$. The order of the vertices in the cycle is arbitrary.You can make queries in the following way: \"? a b\" where $$$1 \\le a, b \\le 10^{18}$$$ and $$$a \\neq b$$$. In response to the query, the interactor outputs on a separate line the length of random of two paths from vertex $$$a$$$ to vertex $$$b$$$, or -1 if $$$\\max(a, b) > n$$$. The interactor chooses one of the two paths with equal probability. The length of the path —is the number of edges in it.You win if you guess the number of vertices in the hidden graph (number $$$n$$$) by making no more than $$$50$$$ queries.Note that the interactor is implemented in such a way that for any ordered pair $$$(a, b)$$$, it always returns the same value for query \"? a b\", no matter how many such queries. Note that the \"? b a\" query may be answered differently by the interactor.The vertices in the graph are randomly placed, and their positions are fixed in advance.Hacks are forbidden in this problem. The number of tests the jury has is $$$50$$$.", "input_spec": null, "output_spec": null, "sample_inputs": ["1\n\n2\n\n-1"], "sample_outputs": ["? 1 2\n\n? 1 3\n\n? 1 4\n\n! 3"], "notes": "NoteIn the first example, the graph could look like this The lengths of the simple paths between all pairs of vertices in this case are $$$1$$$ or $$$2$$$. The first query finds out that one of the simple paths from vertex $$$1$$$ to vertex $$$2$$$ has a length of $$$1$$$. With the second query, we find out that one of the simple paths from vertex $$$1$$$ to vertex $$$3$$$ has length $$$2$$$. In the third query, we find out that vertex $$$4$$$ is not in the graph. Consequently, the size of the graph is $$$3$$$. "}, "positive_code": [{"source_code": "(2..26).each do |i|\n puts(\"? 1 #{i}\")\n $stdout.flush\n a = gets.to_i\n puts(\"? #{i} 1\")\n $stdout.flush\n b = gets.to_i\n if a != b\n puts(\"! #{a + b}\")\n break\n elsif a == -1\n puts(\"! #{i - 1}\")\n break\n end\nend\n"}], "negative_code": [{"source_code": "(2..26).each do |i|\n puts(\"? 1 #{i}\")\n $stdout.flush\n a = gets.to_i\n puts(\"? #{i} 1\")\n $stdout.flush\n b = gets.to_i\n if a != b\n puts(\"! #{a + b}\")\n break\n elsif a == -1\n puts(\"! #{a - 1}\")\n break\n end\nend\n"}, {"source_code": "puts(\"? 1 2\")\n$stdout.flush\na = gets.to_i\nb = 0\n49.times do\n puts(\"? 2 1\")\n $stdout.flush\n b = gets.to_i\n break if a != b\nend\nputs(\"! #{a + b}\")\n"}, {"source_code": "puts(\"? 1 2\")\n$stdout.flush\na = gets.to_i\nb = 0\n49.times do\n puts(\"? 1 2\")\n $stdout.flush\n b = gets.to_i\n break if a != b\nend\nputs(\"! #{a + b}\")\n"}, {"source_code": "puts(\"? 1 2\")\n$stdout.flush\na = gets.to_i\nb = 0\n50.times do\n puts(\"? 1 2\")\n $stdout.flush\n b = gets.to_i\n break if a != b\nend\nputs(\"! #{a + b}\")\n"}, {"source_code": "puts(\"? 1 2\")\n$stdout.flush\na = gets.to_i\nloop do\n puts(\"? 1 2\")\n $stdout.flush\n b = gets.to_i\n if a != b\n puts(\"! #{a + b}\")\n break\n end\nend\n"}, {"source_code": "l = 3\nr = 10**18 + 1\nc = 50\nwhile l < r && c > 0\n m = (l + r) / 2\n puts(\"? 1 #{m}\")\n $stdout.flush\n s = gets.to_i\n if s == -1\n r = m\n elsif l < s\n l = s + 1\n end\n c -= 1\nend\nputs(\"! #{l}\")\n"}, {"source_code": "l = 1\nr = 10**18 + 1\nc = 50\nwhile l < r && c > 0\n m = (l + r) / 2\n puts(\"? 1 #{m}\")\n $stdout.flush\n s = gets.to_i\n if s == -1\n r = m\n elsif l < s\n l = s + 1\n end\n c -= 1\nend\nputs(\"! #{l}\")\n"}], "src_uid": "8590f40e7509614694486165ee824587"} {"nl": {"description": "3R2 - Standby for ActionOur dear Cafe's owner, JOE Miller, will soon take part in a new game TV-show \"1 vs. $$$n$$$\"!The game goes in rounds, where in each round the host asks JOE and his opponents a common question. All participants failing to answer are eliminated. The show ends when only JOE remains (we assume that JOE never answers a question wrong!).For each question JOE answers, if there are $$$s$$$ ($$$s > 0$$$) opponents remaining and $$$t$$$ ($$$0 \\le t \\le s$$$) of them make a mistake on it, JOE receives $$$\\displaystyle\\frac{t}{s}$$$ dollars, and consequently there will be $$$s - t$$$ opponents left for the next question.JOE wonders what is the maximum possible reward he can receive in the best possible scenario. Yet he has little time before show starts, so can you help him answering it instead?", "input_spec": "The first and single line contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$), denoting the number of JOE's opponents in the show.", "output_spec": "Print a number denoting the maximum prize (in dollars) JOE could have. Your answer will be considered correct if it's absolute or relative error won't exceed $$$10^{-4}$$$. In other words, if your answer is $$$a$$$ and the jury answer is $$$b$$$, then it must hold that $$$\\frac{|a - b|}{max(1, b)} \\le 10^{-4}$$$.", "sample_inputs": ["1", "2"], "sample_outputs": ["1.000000000000", "1.500000000000"], "notes": "NoteIn the second example, the best scenario would be: one contestant fails at the first question, the other fails at the next one. The total reward will be $$$\\displaystyle \\frac{1}{2} + \\frac{1}{1} = 1.5$$$ dollars."}, "positive_code": [{"source_code": "s,k=0.0,1.0\na=gets.to_i\nwhile k<=a\n\ts+=1/k\n\tk+=1\nend\t\t\nputs (s)\t\n"}, {"source_code": "\nn = gets.to_i\nans = 0\n(1..n).each do |x|\n\tans += 1 / x.to_f\nend\nputs ans.to_f\n"}], "negative_code": [], "src_uid": "260666df22ee510fcce3ebdfbb8b71a2"} {"nl": {"description": "You are given three strings $$$a$$$, $$$b$$$ and $$$c$$$ of the same length $$$n$$$. The strings consist of lowercase English letters only. The $$$i$$$-th letter of $$$a$$$ is $$$a_i$$$, the $$$i$$$-th letter of $$$b$$$ is $$$b_i$$$, the $$$i$$$-th letter of $$$c$$$ is $$$c_i$$$.For every $$$i$$$ ($$$1 \\leq i \\leq n$$$) you must swap (i.e. exchange) $$$c_i$$$ with either $$$a_i$$$ or $$$b_i$$$. So in total you'll perform exactly $$$n$$$ swap operations, each of them either $$$c_i \\leftrightarrow a_i$$$ or $$$c_i \\leftrightarrow b_i$$$ ($$$i$$$ iterates over all integers between $$$1$$$ and $$$n$$$, inclusive).For example, if $$$a$$$ is \"code\", $$$b$$$ is \"true\", and $$$c$$$ is \"help\", you can make $$$c$$$ equal to \"crue\" taking the $$$1$$$-st and the $$$4$$$-th letters from $$$a$$$ and the others from $$$b$$$. In this way $$$a$$$ becomes \"hodp\" and $$$b$$$ becomes \"tele\".Is it possible that after these swaps the string $$$a$$$ becomes exactly the same as the string $$$b$$$?", "input_spec": "The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 100$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains a string of lowercase English letters $$$a$$$. The second line of each test case contains a string of lowercase English letters $$$b$$$. The third line of each test case contains a string of lowercase English letters $$$c$$$. It is guaranteed that in each test case these three strings are non-empty and have the same length, which is not exceeding $$$100$$$.", "output_spec": "Print $$$t$$$ lines with answers for all test cases. For each test case: If it is possible to make string $$$a$$$ equal to string $$$b$$$ print \"YES\" (without quotes), otherwise print \"NO\" (without quotes). You can print either lowercase or uppercase letters in the answers.", "sample_inputs": ["4\naaa\nbbb\nccc\nabc\nbca\nbca\naabb\nbbaa\nbaba\nimi\nmii\niim"], "sample_outputs": ["NO\nYES\nYES\nNO"], "notes": "NoteIn the first test case, it is impossible to do the swaps so that string $$$a$$$ becomes exactly the same as string $$$b$$$.In the second test case, you should swap $$$c_i$$$ with $$$a_i$$$ for all possible $$$i$$$. After the swaps $$$a$$$ becomes \"bca\", $$$b$$$ becomes \"bca\" and $$$c$$$ becomes \"abc\". Here the strings $$$a$$$ and $$$b$$$ are equal.In the third test case, you should swap $$$c_1$$$ with $$$a_1$$$, $$$c_2$$$ with $$$b_2$$$, $$$c_3$$$ with $$$b_3$$$ and $$$c_4$$$ with $$$a_4$$$. Then string $$$a$$$ becomes \"baba\", string $$$b$$$ becomes \"baba\" and string $$$c$$$ becomes \"abab\". Here the strings $$$a$$$ and $$$b$$$ are equal.In the fourth test case, it is impossible to do the swaps so that string $$$a$$$ becomes exactly the same as string $$$b$$$."}, "positive_code": [{"source_code": "# frozen_string_literal: true\n\n@data = $stdin.read.split\n\ndef next_token\n @data.shift\nend\n\ndef next_int\n next_token.to_i\nend\n\nnext_int.times do |_i|\n a = next_token\n b = next_token\n c = next_token\n ok = true\n a.size.times do |i|\n next if a[i] == c[i] || b[i] == c[i]\n\n ok = false\n end\n if ok\n puts 'YES'\n else\n puts 'NO'\n end\nend\n"}, {"source_code": "T = gets.to_i\n\nT.times do\n a = gets.chomp\n b = gets.chomp\n c = gets.chomp\n n = a.size\n\n success = true\n\n n.times do |i|\n if a[i] != c[i] && b[i] != c[i]\n success = false\n end\n end\n\n if success\n puts 'YES'\n else\n puts 'NO'\n end\nend\n"}, {"source_code": "n = gets.to_i\nn.times {\n a = gets.chomp\n b = gets.chomp\n c = gets.chomp\n flag = true\n for i in (0..a.length - 1)\n if a[i] != c[i] and b[i] != c[i]\n flag = false\n end\n end\n puts flag ? \"YES\" : \"NO\"\n}\n"}, {"source_code": "n = Integer gets\nfor jj in 0...n\ns1, s2, s3 = gets.chop!, gets.chop!, gets.chop!\nflg = \"YES\"\nfor i in 0...s3.size\n if s3[i] != s1[i] && s3[i] != s2[i] then flg = \"NO\" end\nend\nputs flg\nend"}, {"source_code": "gets.to_i.times{\n a = gets.chomp\n b = gets.chomp\n c = gets.chomp\n x = true\n (0...a.length).each do |i|\n if (a[i] == b[i] && a[i] != c[i] || a[i] != b[i] && b[i] != c[i] && a[i] != c[i])\n x = false\n break\n end\n end\n puts x ? \"YES\" : \"NO\"\n}"}], "negative_code": [{"source_code": "gets.to_i.times{\n a = gets.chomp\n b = gets.chomp\n c = gets.chomp\n x = true\n (0...a.length).each do |i|\n if !((a[i] == c[i]) ^ (b[i] == c[i]) ^ (a[i] == b[i]))\n x = false\n break\n end\n end\n puts x ? \"YES\" : \"NO\"\n}"}, {"source_code": "gets.to_i.times{\n a = gets.chomp\n b = gets.chomp\n c = gets.chomp\n x = true\n (0...a.length).each do |i|\n if !((a[i] == c[i]) ^ (b[i] == c[i]))\n x = false\n break\n end\n end\n puts x ? \"YES\" : \"NO\"\n}"}], "src_uid": "08679e44ee5d3c3287230befddf7eced"} {"nl": {"description": "Nastya just made a huge mistake and dropped a whole package of rice on the floor. Mom will come soon. If she sees this, then Nastya will be punished.In total, Nastya dropped $$$n$$$ grains. Nastya read that each grain weighs some integer number of grams from $$$a - b$$$ to $$$a + b$$$, inclusive (numbers $$$a$$$ and $$$b$$$ are known), and the whole package of $$$n$$$ grains weighs from $$$c - d$$$ to $$$c + d$$$ grams, inclusive (numbers $$$c$$$ and $$$d$$$ are known). The weight of the package is the sum of the weights of all $$$n$$$ grains in it.Help Nastya understand if this information can be correct. In other words, check whether each grain can have such a mass that the $$$i$$$-th grain weighs some integer number $$$x_i$$$ $$$(a - b \\leq x_i \\leq a + b)$$$, and in total they weigh from $$$c - d$$$ to $$$c + d$$$, inclusive ($$$c - d \\leq \\sum\\limits_{i=1}^{n}{x_i} \\leq c + d$$$).", "input_spec": "The input consists of multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 \\leq t \\leq 1000)$$$  — the number of test cases. The next $$$t$$$ lines contain descriptions of the test cases, each line contains $$$5$$$ integers: $$$n$$$ $$$(1 \\leq n \\leq 1000)$$$  — the number of grains that Nastya counted and $$$a, b, c, d$$$ $$$(0 \\leq b < a \\leq 1000, 0 \\leq d < c \\leq 1000)$$$  — numbers that determine the possible weight of one grain of rice (from $$$a - b$$$ to $$$a + b$$$) and the possible total weight of the package (from $$$c - d$$$ to $$$c + d$$$).", "output_spec": "For each test case given in the input print \"Yes\", if the information about the weights is not inconsistent, and print \"No\" if $$$n$$$ grains with masses from $$$a - b$$$ to $$$a + b$$$ cannot make a package with a total mass from $$$c - d$$$ to $$$c + d$$$.", "sample_inputs": ["5\n7 20 3 101 18\n11 11 10 234 2\n8 9 7 250 122\n19 41 21 321 10\n3 10 8 6 1"], "sample_outputs": ["Yes\nNo\nYes\nNo\nYes"], "notes": "NoteIn the first test case of the example, we can assume that each grain weighs $$$17$$$ grams, and a pack $$$119$$$ grams, then really Nastya could collect the whole pack.In the third test case of the example, we can assume that each grain weighs $$$16$$$ grams, and a pack $$$128$$$ grams, then really Nastya could collect the whole pack.In the fifth test case of the example, we can be assumed that $$$3$$$ grains of rice weigh $$$2$$$, $$$2$$$, and $$$3$$$ grams, and a pack is $$$7$$$ grams, then really Nastya could collect the whole pack.In the second and fourth test cases of the example, we can prove that it is impossible to determine the correct weight of all grains of rice and the weight of the pack so that the weight of the pack is equal to the total weight of all collected grains."}, "positive_code": [{"source_code": "t = gets.to_i\nt.times do\n n, a, b, c, d = gets.split.map(&:to_i)\n ok = true\n if (a - b) * n > (c + d) || (a + b) * n < (c - d)\n ok = false\n end\n puts ok ? \"Yes\" : \"No\"\nend\n"}, {"source_code": "t = gets.chomp.to_i\nt.times do \n\tn, a, b, c, d = gets.chomp.split(\" \").map(&:to_i)\n\tflag = false\n\t# (n*(a-b)..n*(a+b)).each do |ngo|\n\t# \t#print ngo.to_s + \" \"\n\t# \tif c - d <= ngo && ngo <= c + d then\n\t# \t\tflag = true\n\t# \t\tbreak\n\t# \tend\n\t# end\n\tnab1 = n*(a-b)\n\tnab2 = n*(a+b)\n\tputs (nab1 <= c-d && c + d <= nab2) || (c-d<=nab1 && nab1<=c+d) || (c-d<=nab2&&nab2<=c+d) ? \"Yes\" : \"No\"\nend\n"}, {"source_code": "# Task 1341A\n# require 'byebug'\n\nt = gets.chomp.to_i\nanswers = []\nt.times do\n # Reading input\n str = gets.chomp\n n, a, b, c, d = str.split(\" \").map{ |n| n.to_i }\n\n min_weight = a - b\n max_weight = a + b\n min_total = c - d\n max_total = c + d\n\n if (max_weight * n) < min_total || max_total < (min_weight * n)\n answers << \"No\"\n else\n answers << \"Yes\"\n end\nend\nputs answers"}, {"source_code": "class Range\n def crosses?(x)\n include?(x.first) || include?(x.last) || x.include?(first) || x.include?(last)\n end\nend\n\n1.upto(gets.to_i) do\n n, a, b, c, d = gets.split(' ').map(&:to_i)\n\n puts ((c-d)..(c+d)).crosses?((n*(a-b))..(n*(a+b))) ? 'Yes' : 'No'\nend\n"}, {"source_code": "\n\ndef solve n, a, b, c, d\n return \"Yes\" if n * (a-b) <= c+d && n * (a+b) >= c-d\n \"No\"\nend\n\nt = STDIN.gets.chomp.to_i\n\n(1..t).each do |i|\n n, a, b, c, d = STDIN.gets.chomp.split.map {|i| i.to_i}\n res = solve n, a, b, c, d\n puts res\nend\n"}, {"source_code": "t = gets.to_i\nt.times do\n\tn, a, b, c, d = gets.strip.split.map(&:to_i)\n\tmin_grains = (a - b) * n\n\tmax_grains = (a + b) * n\n\tmin_pack = c - d\n\tmax_pack = c + d\n\tif max_grains < min_pack || max_pack < min_grains\n\t\tputs \"No\"\n\telse\n\t\tputs \"Yes\"\n\tend\nend"}, {"source_code": "gets.to_i.times {\n n, a, b, c, d = gets.split.map { |v| v.to_i }\n min_sum = n * (a - b)\n max_sum = n * (a + b)\n if min_sum > c + d or max_sum < c - d\n puts \"No\"\n else\n puts \"Yes\"\n end\n}\n"}, {"source_code": "1.upto(gets.chomp.to_i) do\n n,a,b,c,d = gets.chomp.split.map(&:to_i)\n if (n * (a-b) <= (c+d) && n*(a+b) >= (c-d))\n puts 'Yes'\n else\n puts 'No'\n end\nend\n"}], "negative_code": [{"source_code": "t = gets.chomp.to_i\nt.times do \n\tn, a, b, c, d = gets.chomp.split(\" \").map(&:to_i)\n\tflag = false\n\t(a-b..a+b).each do |ngo|\n\t\tif c - d <= n * ngo && n * ngo <= c + d then\n\t\t\tputs \"Yes\"\n\t\t\tflag = true\n\t\t\tbreak\n\t\tend\n\tend\n\tputs \"No\" if flag == false\nend\n"}, {"source_code": "# Task 1341A\n# require 'byebug'\n\nt = gets.chomp.to_i\nanswers = []\nt.times do\n # Reading input\n str = gets.chomp\n n, a, b, c, d = str.split(\" \").map{ |n| n.to_i }\n\n min_weight = a - b\n max_weight = a + b\n min_total = c - d\n max_total = c + d\n\n if (min_weight * n) >= min_total || ((max_weight * n) <= max_total && (max_weight * n) >= min_total)\n answers << \"Yes\"\n else\n answers << \"No\"\n end\nend\nputs answers"}, {"source_code": "\n\ndef solve n, a, b, c, d\n tot = [c-d, c+d]\n ((a-b)..(a+b)).each do |i|\n return \"Yes\" if n * i >= tot[0] && n * i <= tot[1]\n return \"Yes\" if n * i >= tot[0] && n * i <= tot[1]\n end\n \"No\"\nend\n\nt = STDIN.gets.chomp.to_i\n\n(1..t).each do |i|\n n, a, b, c, d = STDIN.gets.chomp.split.map {|i| i.to_i}\n res = solve n, a, b, c, d\n puts res\nend\n"}, {"source_code": "\n\ndef solve n, a, b, c, d\n tot = [c-d, c+d]\n return \"Yes\" if n * (a-b) >= tot[0] && n * (a-b) <= tot[1]\n return \"Yes\" if n * (a+b) >= tot[0] && n * (a+b) <= tot[1]\n \"No\"\nend\n\nt = STDIN.gets.chomp.to_i\n\n(1..t).each do |i|\n n, a, b, c, d = STDIN.gets.chomp.split.map {|i| i.to_i}\n res = solve n, a, b, c, d\n puts res\nend\n"}, {"source_code": "t = gets.to_i\nt.times do\n\tn, a, b, c, d = gets.strip.split.map(&:to_i)\n\tmin_grains = (a - b) * n\n\tmax_grains = (a + b) * n\n\tmin_pack = c - d\n\tmax_pack = c + d\n\tif (min_pack <= min_grains && max_pack >= min_grains) || (min_pack <= max_grains && max_pack >= max_grains)\n\t\tputs \"Yes\"\n\telse\n\t\tputs \"No\"\n\tend\nend"}, {"source_code": "t = gets.to_i\nt.times do\n\tn, a, b, c, d = gets.strip.split.map(&:to_i)\n\tmin_grains = (a - b) * n\n\tmax_grains = (a + b) * n\n\tmin_pack = c - d\n\tmax_pack = c + d\n\tif (min_pack <= min_grains && min_grains <= max_pack) || (min_pack <= max_grains && max_grains <= max_pack)\n\t\tputs \"Yes\"\n\telse\n\t\tputs \"No\"\n\tend\nend"}], "src_uid": "30cfce44a7a0922929fbe54446986748"} {"nl": {"description": "An IPv6-address is a 128-bit number. For convenience, this number is recorded in blocks of 16 bits in hexadecimal record, the blocks are separated by colons — 8 blocks in total, each block has four hexadecimal digits. Here is an example of the correct record of a IPv6 address: \"0124:5678:90ab:cdef:0124:5678:90ab:cdef\". We'll call such format of recording an IPv6-address full.Besides the full record of an IPv6 address there is a short record format. The record of an IPv6 address can be shortened by removing one or more leading zeroes at the beginning of each block. However, each block should contain at least one digit in the short format. For example, the leading zeroes can be removed like that: \"a56f:00d3:0000:0124:0001:f19a:1000:0000\"  →  \"a56f:d3:0:0124:01:f19a:1000:00\". There are more ways to shorten zeroes in this IPv6 address.Some IPv6 addresses contain long sequences of zeroes. Continuous sequences of 16-bit zero blocks can be shortened to \"::\". A sequence can consist of one or several consecutive blocks, with all 16 bits equal to 0. You can see examples of zero block shortenings below: \"a56f:00d3:0000:0124:0001:0000:0000:0000\"  →  \"a56f:00d3:0000:0124:0001::\"; \"a56f:0000:0000:0124:0001:0000:1234:0ff0\"  →  \"a56f::0124:0001:0000:1234:0ff0\"; \"a56f:0000:0000:0000:0001:0000:1234:0ff0\"  →  \"a56f:0000::0000:0001:0000:1234:0ff0\"; \"a56f:00d3:0000:0124:0001:0000:0000:0000\"  →  \"a56f:00d3:0000:0124:0001::0000\"; \"0000:0000:0000:0000:0000:0000:0000:0000\"  →  \"::\". It is not allowed to shorten zero blocks in the address more than once. This means that the short record can't contain the sequence of characters \"::\" more than once. Otherwise, it will sometimes be impossible to determine the number of zero blocks, each represented by a double colon.The format of the record of the IPv6 address after removing the leading zeroes and shortening the zero blocks is called short.You've got several short records of IPv6 addresses. Restore their full record.", "input_spec": "The first line contains a single integer n — the number of records to restore (1 ≤ n ≤ 100). Each of the following n lines contains a string — the short IPv6 addresses. Each string only consists of string characters \"0123456789abcdef:\". It is guaranteed that each short address is obtained by the way that is described in the statement from some full IPv6 address.", "output_spec": "For each short IPv6 address from the input print its full record on a separate line. Print the full records for the short IPv6 addresses in the order, in which the short records follow in the input.", "sample_inputs": ["6\na56f:d3:0:0124:01:f19a:1000:00\na56f:00d3:0000:0124:0001::\na56f::0124:0001:0000:1234:0ff0\na56f:0000::0000:0001:0000:1234:0ff0\n::\n0ea::4d:f4:6:0"], "sample_outputs": ["a56f:00d3:0000:0124:0001:f19a:1000:0000\na56f:00d3:0000:0124:0001:0000:0000:0000\na56f:0000:0000:0124:0001:0000:1234:0ff0\na56f:0000:0000:0000:0001:0000:1234:0ff0\n0000:0000:0000:0000:0000:0000:0000:0000\n00ea:0000:0000:0000:004d:00f4:0006:0000"], "notes": null}, "positive_code": [{"source_code": "gets.to_i.times do\n bb = gets.strip.gsub('::', ':zero:').split(':')\n seq_len = 8 - bb.size + 1\n blocks = []\n bb.each do |b|\n if b == 'zero'\n seq_len.times{ blocks << '0000' }\n else\n blocks << '0' * (4 - b.size) + b\n end\n end\n\n puts blocks.join(':')\nend\n"}, {"source_code": "n = gets.to_i\nn.times do\n c = false\n ip = gets.strip.split(\":\")\n if ip[0] == \"\" then ip = ip.drop(1) end\n l = ip.count\n for i in 0...l\n if ip[i] == \"\"\n if i > 0 then print \":\" end\n print \"0000\"\n (8 - l).times do\n print \":0000\"\n end\n c = true\n else\n if i > 0 then print \":\" end\n (4 - ip[i].length).times do print \"0\" end\n print ip[i]\n end\n end\n if l != 8 && !c\n if l > 0 then print \":\" end\n print \"0000\"\n (7 - l).times do\n print \":0000\"\n end\n end\n puts \"\"\nend"}, {"source_code": "gets.to_i.times {\n short = gets.strip.gsub('::', ':zero:').split(':')\n seq_len = 8 - short.size + 1\n blocks = []\n short.each { |b|\n if b == 'zero'\n seq_len.times{ blocks << '0000' }\n else\n blocks << '0' * (4 - b.size) + b\n end\n }\n\n puts blocks.join(':')\n}\n"}, {"source_code": "gets;puts $<.map{|x|x.sub('::',?:*(9-x.count(?:))).gsub(/\\w*\\W/){|y|y.rjust 5,?0}}"}, {"source_code": "puts(readlines[1..-1].map{|x|x.sub('::',?:*(9-x.count(?:))).gsub(/\\w*\\W/){|y|\"0000#{y}\"[-5,5]}})\n"}, {"source_code": "gets;puts $<.lines.map{|x|x.sub('::',?:*(9-x.count(?:))).gsub(/\\w*\\W/){|y|y.rjust 5,?0}}"}, {"source_code": "gets\nputs$<.map{|x|x.sub('::',?:*(9-x.count(?:))).gsub(/\\w*\\W/){|y|y.rjust 5,?0}}"}, {"source_code": "#require 'colorize'\n\ndef recoveryPart(part)\n zones = part.split(':')\n fullPart = String.new\n\n zones.each { |zone|\n fullPart += ('0' * (4 - zone.length)) + zone + ':'\n\n }\n\n fullPart[0..-2]\nend\n\n\ndef recoveryIpv6(shortIpv6)\n\n part = shortIpv6.split('::')\n\n case part.length\n when 0\n result = ('0000:'*8)[0..-2]\n when 1\n if (shortIpv6[shortIpv6.length-2..shortIpv6.length] == '::')\n result = recoveryPart(part[0]) + ':' + ('0000:' * (8 - part[0].split(':').length))\n else\n result = recoveryPart(shortIpv6)\n end\n when 2\n result = recoveryPart(part[0]) + ':' + ('0000:' * (8 - (part[0].split(':').length + part[1].split(':').length))) + recoveryPart(part[1])\n end\n\n if (result[0..0] == ':')\n result = result[1..result.length]\n end\n\n if (result[result.length-1..result.length] == ':')\n result = result[0..result.length-2]\n end\n result\n\nend\n\ncount = gets.chomp.to_i;\n\ncount.times do\n puts recoveryIpv6(gets.chomp)\nend\n\n\n#\n# TEST\n#\n\n#if recoveryIpv6('a56f:d3:0:0124:01:f19a:1000:00') == 'a56f:00d3:0000:0124:0001:f19a:1000:0000'\n# puts 'OK'.green\n#else\n# puts 'FAIL'.red\n#end\n#\n#if recoveryIpv6('a56f:00d3:0000:0124:0001::') == 'a56f:00d3:0000:0124:0001:0000:0000:0000'\n# puts 'OK'.green\n#else\n# puts 'FAIL'.red\n#end\n#\n#if recoveryIpv6('::a56f:00d3:0000:0124:0001') == '0000:0000:0000:a56f:00d3:0000:0124:0001'\n# puts 'OK'.green\n#else\n# puts 'FAIL'.red\n#end\n#\n#if recoveryIpv6('a56f::0124:0001:0000:1234:0ff0') == 'a56f:0000:0000:0124:0001:0000:1234:0ff0'\n# puts 'OK'.green\n#else\n# puts 'FAIL'.red\n#end\n#\n#if recoveryIpv6('a56f:0000::0000:0001:0000:1234:0ff0') == 'a56f:0000:0000:0000:0001:0000:1234:0ff0'\n# puts 'OK'.green\n#else\n# puts 'FAIL'.red\n#end\n#\n#if recoveryIpv6('::') == '0000:0000:0000:0000:0000:0000:0000:0000'\n# puts 'OK'.green\n#else\n# puts 'FAIL'.red\n#end\n#\n#if recoveryIpv6('0ea::4d:f4:6:0') == '00ea:0000:0000:0000:004d:00f4:0006:0000'\n# puts 'OK'.green\n#else\n# puts 'FAIL'.red\n#end\n"}, {"source_code": "gets\nputs $<.map { |s|\n s.strip!\n x = s.split(?:).reject(&:empty?)\n s = s.gsub(/::/, \":#{'0:' * (8 - x.size)}\").gsub(/^:|:$/, '')\n s.split(?:).map { |t| t.rjust(4, ?0) } .join(?:)\n}\n"}, {"source_code": "n=Integer(gets.chomp)\n\nn.times do\ns=gets.chomp\na=s.split '::'\nif a[0]!=nil\nb=a[0].split ':'\nelse\nb=[]\nend\nif a[1]!=nil\nc=a[1].split ':'\nelse\nc=[]\nend\n\na=b+Array.new(8-(b.size+c.size),\"0000\")+c\na.map!{|i| \"0\"*(4-i.size)+i}\n\n(a.size-1).times do |i|\nprint a[i]+':'\nend\nprint a[-1]+\"\\n\"\n\nend\n"}], "negative_code": [{"source_code": "n = gets.to_i\nn.times do\n c = false\n ip = gets.strip.split(\":\")\n l = ip.count\n for i in 0...l\n if ip[i] == \"\"\n if i > 0 then print \":\" end\n print \"0000\"\n (8 - l).times do\n print \":0000\"\n end\n c = true\n else\n if i > 0 then print \":\" end\n (4 - ip[i].length).times do print \"0\" end\n print ip[i]\n end\n end\n if l != 8 && !c\n if l > 0 then print \":\" end\n print \"0000\"\n (7 - l).times do\n print \":0000\"\n end\n end\n puts \"\"\nend"}, {"source_code": "n = gets.to_i\nn.times do\n c = false\n ip = gets.strip.split(\":\")\n l = ip.count\n for i in 0...l\n if ip[i] == \"\"\n if i > 0 then print \":\" end\n print \"0000\"\n (8 - l).times do\n print \":0000\"\n end\n c = true\n else\n if i > 0 then print \":\" end\n (4 - ip[i].length).times do print \"0\" end\n print ip[i]\n end\n end\n if !c\n if l > 0 then print \":\" end\n print \"0000\"\n (7 - l).times do\n print \":0000\"\n end\n end\n puts \"\"\nend"}, {"source_code": "#!ruby -plr ipaddr\n#$_=IPAddr.new($_).to_string\n$_=2\n"}], "src_uid": "20f69ee5f04c8cbb769be3ab646031ab"} {"nl": {"description": "Some dwarves that are finishing the StUDY (State University for Dwarven Youngsters) Bachelor courses, have been told \"no genome, no degree\". That means that all dwarves should write a thesis on genome. Dwarven genome is far from simple. It is represented by a string that consists of lowercase Latin letters.Dwarf Misha has already chosen the subject for his thesis: determining by two dwarven genomes, whether they belong to the same race. Two dwarves belong to the same race if we can swap two characters in the first dwarf's genome and get the second dwarf's genome as a result. Help Dwarf Misha and find out whether two gnomes belong to the same race or not.", "input_spec": "The first line contains the first dwarf's genome: a non-empty string, consisting of lowercase Latin letters. The second line contains the second dwarf's genome: a non-empty string, consisting of lowercase Latin letters. The number of letters in each genome doesn't exceed 105. It is guaranteed that the strings that correspond to the genomes are different. The given genomes may have different length.", "output_spec": "Print \"YES\", if the dwarves belong to the same race. Otherwise, print \"NO\".", "sample_inputs": ["ab\nba", "aa\nab"], "sample_outputs": ["YES", "NO"], "notes": "Note First example: you can simply swap two letters in string \"ab\". So we get \"ba\". Second example: we can't change string \"aa\" into string \"ab\", because \"aa\" does not contain letter \"b\". "}, "positive_code": [{"source_code": "s1 = gets.chomp.split(\"\")\ns2 = gets.chomp.split(\"\")\n\nif s1.sort == s2.sort then\n i = 0\n d = 0\n while i < s1.size do \n if s1[i] != s2[i] then\n d += 1\n end\n i += 1\n end\n if d <= 2 then\n puts \"YES\"\n else\n puts \"NO\"\n end\nelse\n puts \"NO\"\nend"}, {"source_code": "s1 = gets.chomp\ns2 = gets.chomp\nret = \"NO\"\nr1 = nil\nr2 = nil\nif s1.length == s2.length\n for i in 0..s1.length-1\n next if s1[i] == s2[i]\n if r1 == nil\n r1 = s1[i]\n r2 = s2[i]\n else\n if ret == \"NO\"\n if r1 == s2[i] && r2 == s1[i]\n ret = \"YES\"\n else\n break\n end\n else\n ret = \"NO\"\n break\n end\n end\n end\nend\nputs ret\n"}, {"source_code": "require 'set'\n\ndef compare_str(a, b)\n return \"NO\" if a.length != b.length\n a_set = []\n b_set = []\n (0..a.length).each do |i|\n if a[i] != b[i]\n a_set << a[i]\n b_set << b[i]\n end\n end\n return \"NO\" if a_set.length > 2\n return \"YES\" if a_set.sort == b_set.sort\n return \"NO\"\nend\n\n\na = gets.chomp\nb = gets.chomp\n\nputs compare_str(a, b)\n\n"}, {"source_code": "def diff(a, b)\n c = 0\n for i in 0..(a.length - 1)\n if a[i] != b[i]\n c += 1\n end\n end\n c\nend\n\na = gets.chomp.split \"\"\nb = gets.chomp.split \"\"\n\nputs ((a.sort == b.sort) and diff(a, b) == 2) ? \"YES\" : \"NO\"\n"}, {"source_code": "s = STDIN.readline.strip\nt = STDIN.readline.strip\nif s.length != t.length\n\tputs 'NO'\n\texit\nend\ndifferences = []\ns.length.times do |i|\n\tdifferences.push(i) if s[i] != t[i]\nend\nif differences.length != 2\n\tputs 'NO'\n\texit\nend\na, b = differences\ns[a], s[b] = s[b], s[a]\nif s == t\n\tputs 'YES'\nelse\n\tputs 'NO'\nend\n"}, {"source_code": "a=gets.chomp.strip\nb=gets.chomp.strip\nx=[]\nif a.length!=b.length\n\tputs \"NO\"\n\texit\nend\nfor i in 0...a.length\n\tif a[i]!=b[i]\n\t\tx.push(i)\n\tend\nend\nif x.length!=2\n\tputs \"NO\"\n\texit\nend\nputs (a[x[0]]==b[x[1]] and b[x[0]]==a[x[1]])? \"YES\" : \"NO\"\n"}, {"source_code": "a=gets.chomp.split(\"\")\nb=gets.chomp.split(\"\")\n\nif a.length !=b.length\n\tputs \"NO\"\nelsif a.sort != b.sort\n\tputs \"NO\"\nelsif a==b\n\tputs \"YES\"\nelsif a.reverse==b && a.length==2\n\tputs \"YES\"\nelse\n\nk=[]\n\t\n\t0.upto(a.length-1) do|i|\n\t\tk[i]=[a[i],b[i]]\n\tend\n\nk.each{|i| i.uniq!}\nk.sort! {|x,y| y.length <=> x.length}\nd=0\n\n\t0.upto(2) do |i|\n\t\tif k[i].length==2\n\t\t\td+=1\n\t\tend\n\tend\n\tif d<=1\n\t\tputs \"YES\"\n\telsif d>2\n\t\tputs \"NO\"\n\telse\n\t\tif k[0].reverse==k[1]\n\t\t\tputs \"YES\"\n\t\telse\n\t\t\tputs \"NO\"\n\t\tend\n\tend\n\nend\t"}, {"source_code": "a = gets.to_s\nb = gets.to_s\n\nif a.size != b.size then\n\tputs \"NO\"\n\texit\nend\n\ncount = 0\nx = 0\ny = 0\n\n0.upto (a.size-1) do |i|\n\tif a[i] != b[i] then\n\t\tcount += 1\n\t\tif count == 1 then\n\t\t\tx = a[i]\n\t\t\ty = b[i]\n\t\telsif count == 2 then\n\t\t\tif (x != b[i]) or (y != a[i]) then\n\t\t\t\tputs \"NO\"\n\t\t\t\texit\n\t\t\tend\n\t\telse\n\t\t\tputs \"NO\"\n\t\t\texit\n\t\tend\n\tend\nend\n\nif count == 0 then\n\td = {}\n\t0.upto (a.size -1) do |i| \n\t\tif d[i] == 1 then \n\t\t\tputs \"YES\"\n\t\t\texit\n\t\telse\n\t\t\td[i] = 1\n\t\tend\n\tend\n\tputs \"NO\"\t\n\texit\nelsif count == 1 then\n\tputs \"NO\"\n\texit\nelse\n\tputs \"YES\"\n\texit\nend\n"}, {"source_code": "first = gets.chomp\nsecond = gets.chomp\nans = \"NO\"\nif first.length == second.length\n diff = []\n act = []\n (0...first.length).each do |i|\n a = first[i]\n b = second[i]\n if a != b\n diff << a\n act << b\n end\n end\n if diff.length == 2\n ans = \"YES\" if act[0] == diff[1] && act[1] == diff[0]\n end\nend\nputs ans\n"}, {"source_code": "a=gets.chomp.split(\"\")\nb=gets.chomp.split(\"\")\n\nif a.length !=b.length\n\tputs \"NO\"\nelsif a.sort != b.sort\n\tputs \"NO\"\nelsif a==b\n\tputs \"YES\"\nelsif a.reverse==b && a.length==2\n\tputs \"YES\"\nelse\n\nk=[]\n\t\n\t0.upto(a.length-1) do|i|\n\t\tk[i]=[a[i],b[i]]\n\tend\n\nk.each{|i| i.uniq!}\nk.sort! {|x,y| y.length <=> x.length}\nd=0\n\n\t0.upto(2) do |i|\n\t\tif k[i].length==2\n\t\t\td+=1\n\t\tend\n\tend\n\tif d<=1\n\t\tputs \"YES\"\n\telsif d>2\n\t\tputs \"NO\"\n\telse\n\t\tif k[0].reverse==k[1]\n\t\t\tputs \"YES\"\n\t\telse\n\t\t\tputs \"NO\"\n\t\tend\n\tend\n\nend"}, {"source_code": "a = gets.chomp\nb = gets.chomp\n\ndef work(a, b)\n return false if a.size != b.size\n d = []\n (0...a.size).each {|x| d << x if a[x] != b[x]}\n # p d.size\n return false if d.size != 0 and d.size != 2\n\n if d.size == 2\n return true if a[d[0]] == b[d[1]] and a[d[1]] == b[d[0]]\n return false\n end\n \n hs = Hash.new(0)\n a.each_byte{|x| hs[x] = hs[x] + 1}\n return true if hs.values.any?{|x| x > 1}\n \n return false\nend\n\nif work(a,b)\n puts 'YES'\nelse\n puts 'NO'\nend"}, {"source_code": "a=gets\nb=gets\nif a.size != b.size\n puts \"NO\"\nelse\n s0=[]\n s1=[]\n (a.size-1).times do |i|\n if a[i] != b[i]\n s0 << a[i]\n s1 << b[i]\n end\n end\n if s0.size!=2 || s0[1] != s1[0] || s0[0] != s1[1]\n puts \"NO\"\n else\n puts \"YES\"\n end\nend \n "}, {"source_code": "first_count = Hash.new(0)\nsecond_count = Hash.new(0)\n\na = gets.chomp\nb = gets.chomp\nans = false\nmismatch_ind = []\nif a.length == b.length\n\t(0..a.length - 1).each do |i|\n\t\t\n\t\ta_char = a[i]\n\t\tb_char = b[i]\n\t\t\n\t\tfirst_count[a_char] += 1\n\t\tsecond_count[b_char] += 1\n\t\tmismatch_ind << i if a_char != b_char\t\t\t\t\t\t\t\t\n\tend\n\n\tans = true if first_count == second_count && ( mismatch_ind.length == 0 || (mismatch_ind.length == 2 && a[mismatch_ind[0]] == b[mismatch_ind[1]] && a[mismatch_ind[1]] == b[mismatch_ind[0]]))\n\nend\nputs ans ? \"YES\" : \"NO\""}, {"source_code": "l = []\na = gets\nb = gets\nif a.size == b.size\n\ta.split(//).each_with_index do |x, i|\n\t\tl << a[i].ord - b[i].ord if a[i].ord - b[i].ord != 0\n\t\tif l.size > 2\n\t\t\tbreak\n\t\tend\n\tend\nelse\n\tl << 0\nend\n\nif l.size == 0 or (l.size == 2 and l[0] == -l[1])\n\tputs \"YES\"\nelse\n\tputs \"NO\"\nend"}, {"source_code": "as, bs = gets.chomp.chars.to_a, gets.chomp.chars.to_a\nfirst = true\nfirst_a, first_b = nil, nil\noutput = false\nanswer = \"NO\"\nif as.length == bs.length\n as.each_with_index do |a, i|\n b = bs[i]\n if a != b\n if output\n answer = \"NO\"\n break\n end\n if first\n first_a = a\n first_b = b\n first = false\n else\n answer = \"YES\" if a == first_b && b == first_a\n output = true\n end\n end\n end\n\n puts answer\nelse\n puts \"NO\"\nend\n"}, {"source_code": "as, bs = gets.chomp.chars.to_a, gets.chomp.chars.to_a\n\nif as.length != bs.length\n puts \"NO\"\nelse\n diff = []\n\n as.zip(bs).each_with_index do |(a, b), i|\n diff << i if a != b\n end\n\n puts diff.length == 2 && as[diff[0]] == bs[diff[1]] && as[diff[1]] == bs[diff[0]] ? \"YES\" : \"NO\"\nend\n"}, {"source_code": "def main\n a = STDIN.gets\n b = STDIN.gets\n\n return 'NO' if a.length != b.length\n\n diffs = []\n (0 ... a.length).each do |i|\n diffs.push(i) if a[i] != b[i]\n end\n\n return 'NO' if diffs.length != 2\n\n return a[diffs[0]] == b[diffs[1]] && b[diffs[0]] == a[diffs[1]] ? 'YES' : 'NO'\nend\n\nputs main\n"}, {"source_code": "a=gets.chomp.split(\"\")\nb=gets.chomp.split(\"\")\n\nif a.length !=b.length\n\tputs \"NO\"\nelsif a.sort != b.sort\n\tputs \"NO\"\nelsif a==b\n\tputs \"YES\"\nelsif a.reverse==b && a.length==2\n\tputs \"YES\"\nelse\n\nk=[]\n\t\n\t0.upto(a.length-1) do|i|\n\t\tk[i]=[a[i],b[i]]\n\tend\n\nk.each{|i| i.uniq!}\nk.sort! {|x,y| y.length <=> x.length}\nd=0\n\n\t0.upto(2) do |i|\n\t\tif k[i].length==2\n\t\t\td+=1\n\t\tend\n\tend\n\tif d<=1\n\t\tputs \"YES\"\n\telsif d>2\n\t\tputs \"NO\"\n\telse\n\t\tif k[0].reverse==k[1]\n\t\t\tputs \"YES\"\n\t\telse\n\t\t\tputs \"NO\"\n\t\tend\n\tend\n\nend\t"}, {"source_code": "def say_no\n puts 'NO'\n exit\nend\n\ng1, g2 = [gets.chomp, gets.chomp]\nn = g1.size\nsay_no if n != g2.size\nd = []\nfor i in 0...n\n say_no if g1[i] != g2[i] && ((t = (d << i).size) > 2 || (t == 2 && (g1[d[0]] != g2[d[1]] || g1[d[1]] != g2[d[0]])))\nend\nputs [0, 2].any? { |p| p == d.size } ? 'YES' : 'NO'"}, {"source_code": "a = gets\nb = gets\nans = false\nif a.length == b.length\n\ts = []\n\ta.length.times do |i|\n\t\tif a[i] != b[i]\n\t\t\ts << i\n\t\tend\n\tend\n\tans = s.empty? || ((s.length == 2 and a[s[0]] == b[s[1]] and a[s[1]] == b[s[0]]))\nend\nputs ans ? \"YES\" : \"NO\""}, {"source_code": "a, b, c, diff = gets.strip, gets.strip, 0, []\n\nif a.length == b.length\n\ta.length.times do |i|\n\t\tunless a[i] == b[i]\n\t\t\tc += 1\n\t\t\tc < 3 ? diff.push([a[i], b[i]]) : break\n\t\tend\n\tend\n\tprint c == 2 && diff[0] == diff[1].reverse ? :YES : :NO\nelse\n\tprint :NO\n\texit\nend"}, {"source_code": "a = gets.chomp\nb = gets.chomp\n\ndef work(a, b)\n #puts a.size,b.size\n return false if a.size != b.size\n d = []\n (0...a.size).each {|x| d << x if a[x] != b[x]}\n # p d.size\n return false if d.size != 0 and d.size != 2\n\n if d.size == 2\n return true if a[d[0]] == b[d[1]] and a[d[1]] == b[d[0]]\n return false\n end\n \n hs = Hash.new(0)\n a.each_byte{|x| hs[x] = hs[x] + 1}\n return true if hs.values.any?{|x| x > 1}\n \n return false\nend\n\nif work(a,b)\n puts 'YES'\nelse\n puts 'NO'\nend\n\n "}], "negative_code": [{"source_code": "s1 = gets.chomp\ns2 = gets.chomp\n\ni = 0\nwhile i < s2.size do\n if s1[i] == s2[i] then\n s1 = s1[0...i] + s1[i+1..-1]\n s2 = s2[0...i] + s2[i+1..-1]\n end\n i += 1\nend\n\ns1 = s1.delete(s2)\n\nif s1 == \"\" then\n puts \"YES\"\nelse\n puts \"NO\"\nend\n"}, {"source_code": "s1 = gets.chomp.split(\"\")\ns2 = gets.chomp.split(\"\")\n\ns1 = s1.sort\ns2 = s2.sort\n\nif s1 == s2 then\n i = 0\n d = 0\n while i < s1.size do \n if s1[i] != s2[i] then\n d += 1\n end\n i += 1\n end\n if d <= 2 then\n puts \"YES\"\n else\n puts \"NO\"\n end\nelse\n puts \"NO\"\nend"}, {"source_code": "s1 = gets.chomp\ns2 = gets.chomp\n\nif s1.size == s2.size then\n i = 0\n while i < s2.size do\n if s1[i] == s2[i] then\n s1 = s1.sub(s1[i], \"\")\n s2 = s2.sub(s2[i], \"\")\n end\n i += 1\n end\n if s1.size == 2 then\n s1 = s1.delete(s2)\n if s1 == \"\" then\n puts \"YES\"\n else\n puts \"NO\"\n end\n else\n puts \"NO\"\n end\nelse\n puts \"NO\"\nend\n"}, {"source_code": "s1 = gets.chomp\ns2 = gets.chomp\n\nif s1.size == s2.size then\n i = 0\n str = \"\"\n while i < s2.size do\n if s1[i] == s2[i] then\n str += s1[i]\n end\n i += 1\n end\n s1 = s1.sub(str, \"\")\n s2 = s2.sub(str, \"\")\n if s1.size == 2 then\n s1 = s1.delete(s2)\n if s1 == \"\" then\n puts \"YES\"\n else\n puts \"NO\"\n end\n else\n puts \"NO\"\n end\nelse\n puts \"NO\"\nend\n"}, {"source_code": "def diff(a, b)\n c = 0\n for i in 0..(a.length - 1)\n if a[i] == b[i]\n c += 1\n end\n end\n c\nend\n\na = gets.chomp.split \"\"\nb = gets.chomp.split \"\"\n\nputs ((a.sort == b.sort) and diff(a, b) == 2) ? \"YES\" : \"NO\"\n"}, {"source_code": "def diff(a, b)\n c = 0\n for i in 0..(a.length - 1)\n if a[i] == b[i]\n c += 1\n end\n end\n c\nend\n\na = gets.chomp.split \"\"\nb = gets.chomp.split \"\"\n\na.sort!\nb.sort!\n\nputs ((a == b) and diff(a, b) == 2) ? \"YES\" : \"NO\"\n"}, {"source_code": "a = gets.chomp.split \"\"\nb = gets.chomp.split \"\"\n\na.sort!\nb.sort!\n\nputs (a == b) ? \"YES\" : \"NO\"\n"}, {"source_code": "a=gets.chomp.split(\"\")\nb=gets.chomp.split(\"\")\n\nif a.length !=b.length\n\tputs \"NO\"\nelsif a.sort != b.sort\n\tputs \"NO\"\nelsif a==b\n\tputs \"YES\"\nelsif a.reverse==b\n\tputs \"YES\"\nelse\n\nk=[]\n\t\n\t0.upto(a.length-1) do|i|\n\t\tk[i]=[a[i],b[i]]\n\tend\n\nk.each{|i| i.uniq!}\nk.sort! {|x,y| y.length <=> x.length}\nd=0\n\t\n\t0.upto(2) do |i|\n\t\tif k[i].length==2\n\t\t\td+=1\n\t\tend\n\tend\n\t\n\tif d<=1\n\t\tputs \"YES\"\n\telsif d>2\n\t\tputs \"NO\"\n\telse\n\t\tif k[0].reverse==k[1]\n\t\t\tputs \"YES\"\n\t\telse\n\t\t\tputs \"NO\"\n\t\tend\n\tend\n\nend\t"}, {"source_code": "a=gets\nb=gets\nif a.size != b.size\n puts \"NO\"\nelse\n s0=[]\n s1=[]\n (a.size-1).times do |i|\n puts i\n if a[i] != b[i]\n s0 << a[i]\n s1 << b[i]\n end\n end\n if s0.size!=2 || s0[1] != s1[0] || s0[0] != s1[1]\n puts \"NO\"\n else\n puts \"YES\"\n end\nend \n "}, {"source_code": "first_count = Hash.new(0)\nsecond_count = Hash.new(0)\n\na = gets.chomp\nb = gets.chomp\n\na.each_char do |i|\n\tfirst_count[i] += 1\nend\n\nb.each_char do |i|\n\tsecond_count[i] += 1\nend\n\nputs first_count == second_count ? \"YES\" : \"NO\""}, {"source_code": "as, bs = gets.chomp.chars.to_a, gets.chomp.chars.to_a\nfirst = true\nfirst_a, first_b = nil, nil\noutput = false\nas.each_with_index do |a, i|\n b = bs[i]\n if a != b\n if first\n first_a = a\n first_b = b\n first = false\n else\n puts a == first_b && b == first_a ? \"YES\" : \"NO\"\n output = true\n break\n end\n end\nend\n\nputs \"NO\" unless output\n"}, {"source_code": "as, bs = gets.chomp.chars.to_a, gets.chomp.chars.to_a\nfirst = true\nfirst_a, first_b = nil, nil\noutput = false\nanswer = \"NO\"\nas.each_with_index do |a, i|\n b = bs[i]\n if a != b\n if output\n answer = \"NO\"\n break\n end\n if first\n first_a = a\n first_b = b\n first = false\n else\n answer = \"YES\" if a == first_b && b == first_a\n output = true\n end\n end\nend\n\nputs answer\n"}, {"source_code": "def main\n a = STDIN.gets\n b = STDIN.gets\n\n return 'NO' if a.length != b.length\n\n i = 0\n i += 1 while a[i] == b[i]\n\n return 'YES' if i == a.length\n\n j = a.length - 1\n j -= 1 while a[j] == b[j]\n\n return 'YES' if j == i + 1 && a[i] == b[j] && a[j] == b[i]\n \n return 'NO'\nend\n\nprint main\n"}, {"source_code": "def say_no\n puts 'NO'\n exit\nend\n\ng1, g2 = [gets.chomp, gets.chomp]\nn = g1.size\nsay_no if n != g2.size\nd = []\nfor i in 0...n\n say_no if g1[i] != g2[i] && ((t = (d << i).size) > 2 || (t == 2 && (g1[d[0]] != g2[d[1]] || g1[d[1]] != g2[d[0]])))\nend\nputs 'YES'"}, {"source_code": "a , b = gets.strip, gets.strip\nac, bc = Hash.new(0), Hash.new(0)\n\na.each_char { |c| ac[c.to_sym] += 1 }\nb.each_char { |c| bc[c.to_sym] += 1 }\n\nprint ac == bc ? :YES : :NO\n"}, {"source_code": "a, b, c = gets.strip, gets.strip, 0\n(print :NO; exit) unless a.length == b.length\na.length.times { |i| c += 1 unless a[i] == b[i] }\nprint c == 2 ? :YES : :NO"}, {"source_code": "a, b = STDIN.read.split(\"\\n\")\na1, a2 = a.split('').uniq \nb1, b2 = b.split('').uniq\n\nac1 = a.count(a1)\nbc1 = b.count(b1)\n\nac2 = a2 ? a.count(a2) : 0\nbc2 = b2 ? b.count(b2) : 0\n\nprint ac1 == bc1 && ac2 == bc2 ? :YES : :NO"}], "src_uid": "c659bdeda1c1da08cfc7f71367222332"} {"nl": {"description": "Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string \"abab\" has two divisors — \"ab\" and \"abab\".Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.", "input_spec": "The first input line contains a non-empty string s1. The second input line contains a non-empty string s2. Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.", "output_spec": "Print the number of common divisors of strings s1 and s2. ", "sample_inputs": ["abcdabcd\nabcdabcdabcdabcd", "aaa\naa"], "sample_outputs": ["2", "1"], "notes": "NoteIn first sample the common divisors are strings \"abcd\" and \"abcdabcd\".In the second sample the common divisor is a single string \"a\". String \"aa\" isn't included in the answer as it isn't a divisor of string \"aaa\"."}, "positive_code": [{"source_code": "def make_fail(str)\n kmp = Array.new(str.length)\n i, j = [1, 0]\n kmp[0] = 0\n while (i < str.length) do\n kmp[i] = j\n while (j > 0 and str[i] != str[j]) do\n j = kmp[j]\n end\n i, j = i + 1, j + 1\n end\n return kmp\nend\n\ndef get_cycle(str, kmp)\n last = kmp.length - 1\n if kmp[last] == 0 or str[last] != str[kmp[last]]\n return str.length - 1\n else\n if last % (last - kmp[last]) == 0\n return last - kmp[last]\n end\n return last\n end\nend\n\ndef gcd(a, b)\n if b == 0\n return a\n end\n return gcd(b, a % b)\nend\n\ndef num_of_divisors(num)\n if num < 2\n return 1\n end\n i, cnt = 1, 0\n while i <= num do\n cnt = cnt + 1 if num % i == 0\n i = i + 1\n end\n return cnt\nend\n\ns1 = \" \" + gets.split.first\ns2 = \" \" + gets.split.first\n\nc1 = get_cycle(s1, make_fail(s1))\nc2 = get_cycle(s2, make_fail(s2))\nn1 = (s1.length - 1) / c1\nn2 = (s2.length - 1) / c2\n\n# puts n1.to_s + \" \" + n2.to_s\n\nif c1 != c2 or s1[1, c1] != s2[1, c2]\n # puts c1.to_s + \" \" + c2.to_s\n puts 0\nelse\n g = gcd(n1, n2)\n puts num_of_divisors(g)\nend\n"}, {"source_code": "# ProblemD\n\ndef divisors(n)\n ret = []\n\n i = 1\n while i * i <= n\n if n % i == 0\n ret << i\n if (ret.index(n / i) == nil)\n ret << (n / i)\n end\n end\n i += 1\n end\n\n return ret\nend\n\ns1 = gets.chomp\ns2 = gets.chomp\n\nbase = \"\"\ntarget = \"\"\nif s1.size <= s2.size\n base = s1\n target = s2\nelse\n base = s2\n target = s1\nend\n\nbase_size = base.size\ntarget_size = target.size\n\nres = 0\n\nbase_divisors = divisors(base_size)\nbase_divisors.each do |base_divisor|\n tmp_str = base[0..base_divisor-1]\n if (tmp_str * (base_size / base_divisor)) == base\n if target_size % base_divisor == 0\n if (tmp_str * (target_size / base_divisor)) == target\n res += 1\n end\n end\n end\nend\n\nputs res\n"}], "negative_code": [{"source_code": "def make_fail(str)\n kmp = Array.new(str.length)\n i, j = [1, 0]\n kmp[0] = 0\n while (i < str.length) do\n kmp[i] = j\n while (j > 0 and str[i] != str[j]) do\n j = kmp[j]\n end\n i, j = i + 1, j + 1\n end\n return kmp\nend\n\ndef get_cycle(str, kmp)\n last = kmp.length - 1\n if kmp[last] == 0 or str[last] != str[kmp[last]]\n return str.length - 1\n else\n return last - kmp[last]\n end\nend\n\ndef gcd(a, b)\n if b == 0\n return a\n end\n return gcd(b, a % b)\nend\n\ndef num_of_divisors(num)\n if num < 2\n return 1\n end\n i, cnt = 1, 0\n while i <= num do\n cnt = cnt + 1 if num % i == 0\n i = i + 1\n end\n return cnt\nend\n\ns1 = \" \" + gets.split.first\ns2 = \" \" + gets.split.first\n\nc1 = get_cycle(s1, make_fail(s1))\nc2 = get_cycle(s2, make_fail(s2))\nn1 = (s1.length - 1) / c1\nn2 = (s2.length - 1) / c2\n\n# puts n1.to_s + \" \" + n2.to_s\n\nif c1 != c2 or s1[1, c1] != s2[1, c2]\n puts 0\nelse\n g = gcd(n1, n2)\n puts num_of_divisors(g) # if s1.length < 100\n # puts s1.length.to_s + \" \" + s2.length.to_s if s1.length > 100\nend\n"}, {"source_code": "def make_fail(str)\n kmp = Array.new(str.length)\n i, j = [1, 0]\n kmp[0] = 0\n while (i < str.length) do\n kmp[i] = j\n while (j > 0 and str[i] != str[j]) do\n j = kmp[j]\n end\n i, j = i + 1, j + 1\n end\n return kmp\nend\n\ndef get_cycle(str, kmp)\n last = kmp.length - 1\n if kmp[last] == 0 or str[last] != str[kmp[last]]\n return str.length - 1\n else\n return last - kmp[last]\n end\nend\n\ndef gcd(a, b)\n if b == 0\n return a\n end\n return gcd(b, a % b)\nend\n\ns1 = \" \" + gets.split.first\ns2 = \" \" + gets.split.first\n\nn1 = (s1.length - 1) / get_cycle(s1, make_fail(s1))\nn2 = (s2.length - 1) / get_cycle(s2, make_fail(s2))\n\n# puts n1.to_s + \" \" + n2.to_s\n\nif !(s1.include? s2) and !(s2.include? s2)\n puts 0\nelse\n g = gcd(n1, n2)\n i = 1\n cnt = 0\n while i * i <= g do\n if g % i == 0\n cnt = cnt + 1\n end\n i = i + 1\n end\n\n cnt = cnt + 1 if g != 1\n\n puts cnt\nend\n"}, {"source_code": "def make_fail(str)\n kmp = Array.new(str.length)\n i, j = [1, 0]\n kmp[0] = 0\n while (i < str.length) do\n kmp[i] = j\n while (j > 0 and str[i] != str[j]) do\n j = kmp[j]\n end\n i, j = i + 1, j + 1\n end\n return kmp\nend\n\ndef get_cycle(str, kmp)\n last = kmp.length - 1\n if kmp[last] == 0 or str[last] != str[kmp[last]]\n return str.length - 1\n else\n return last - kmp[last]\n end\nend\n\ndef gcd(a, b)\n if b == 0\n return a\n end\n return gcd(b, a % b)\nend\n\ndef num_of_divisors(num)\n if num < 2\n return 1\n end\n i, cnt = 2, 2\n while i * i <= num do\n cnt = cnt + 1 if num % i == 0\n i = i + 1\n end\n return cnt\nend\n\ns1 = \" \" + gets.split.first\ns2 = \" \" + gets.split.first\n\nn1 = (s1.length - 1) / get_cycle(s1, make_fail(s1))\nn2 = (s2.length - 1) / get_cycle(s2, make_fail(s2))\n\n# puts n1.to_s + \" \" + n2.to_s\n\nif !(s1.include? s2) and !(s2.include? s2)\n puts 0\nelse\n g = gcd(n1, n2)\n puts num_of_divisors(g) if s1.length < 100\n puts s1.length.to_s + \" \" + s2.length.to_s if s1.length > 100\nend\n"}, {"source_code": "def make_fail(str)\n kmp = Array.new(str.length)\n i, j = [1, 0]\n kmp[0] = 0\n while (i < str.length) do\n kmp[i] = j\n while (j > 0 and str[i] != str[j]) do\n j = kmp[j]\n end\n i, j = i + 1, j + 1\n end\n return kmp\nend\n\ndef get_cycle(str, kmp)\n last = kmp.length - 1\n if kmp[last] == 0 or str[last] != str[kmp[last]]\n return str.length - 1\n else\n return last - kmp[last]\n end\nend\n\ndef gcd(a, b)\n if b == 0\n return a\n end\n return gcd(b, a % b)\nend\n\ndef num_of_divisors(num)\n if num < 2\n return 1\n end\n i, cnt = 1, 0\n while i <= num do\n cnt = cnt + 1 if num % i == 0\n i = i + 1\n end\n return cnt\nend\n\ns1 = \" \" + gets.split.first\ns2 = \" \" + gets.split.first\n\nn1 = (s1.length - 1) / get_cycle(s1, make_fail(s1))\nn2 = (s2.length - 1) / get_cycle(s2, make_fail(s2))\n\n# puts n1.to_s + \" \" + n2.to_s\n\nif !(s1.include? s2) and !(s2.include? s1)\n puts 0\nelse\n g = gcd(n1, n2)\n puts num_of_divisors(g) # if s1.length < 100\n # puts s1.length.to_s + \" \" + s2.length.to_s if s1.length > 100\nend\n"}, {"source_code": "def make_fail(str)\n kmp = Array.new(str.length)\n i, j = [1, 0]\n kmp[0] = 0\n while (i < str.length) do\n kmp[i] = j\n while (j > 0 and str[i] != str[j]) do\n j = kmp[j]\n end\n i, j = i + 1, j + 1\n end\n return kmp\nend\n\ndef get_cycle(str, kmp)\n last = kmp.length - 1\n if kmp[last] == 0 or str[last] != str[kmp[last]]\n return str.length - 1\n else\n return last - kmp[last]\n end\nend\n\ndef gcd(a, b)\n if b == 0\n return a\n end\n return gcd(b, a % b)\nend\n\ndef num_of_divisors(num)\n if num < 2\n return 1\n end\n i, cnt = 1, 0\n while i <= num do\n cnt = cnt + 1 if num % i == 0\n i = i + 1\n end\n return cnt\nend\n\ns1 = \" \" + gets.split.first\ns2 = \" \" + gets.split.first\n\nn1 = (s1.length - 1) / get_cycle(s1, make_fail(s1))\nn2 = (s2.length - 1) / get_cycle(s2, make_fail(s2))\n\n# puts n1.to_s + \" \" + n2.to_s\n\nif !(s1.include? s2) and !(s2.include? s2)\n puts 0\nelse\n g = gcd(n1, n2)\n puts num_of_divisors(g) # if s1.length < 100\n # puts s1.length.to_s + \" \" + s2.length.to_s if s1.length > 100\nend\n"}, {"source_code": "# ProblemD\n\ns1 = gets.chomp\ns2 = gets.chomp\n\nbase = \"\"\ntarget = \"\"\nif s1.size <= s2.size\n base = s1\n target = s2\nelse\n base = s2\n target = s1\nend\n\nbase_size = base.size\ntarget_size = target.size\n\nres = 0\nbase.size.times do |i|\n tmp_str = base[0..i]\n if target.start_with?(tmp_str)\n n = i + 1\n if (base_size % n == 0) && (target_size % n == 0)\n if tmp_str * (base_size / n) == base && tmp_str * (target_size / n) == target\n puts tmp_str\n res += 1\n end\n end\n end\nend\n\nputs res\n"}], "src_uid": "d28614f0365ea53530e35c6bd1e6f1dd"} {"nl": {"description": "This is yet another problem dealing with regular bracket sequences.We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.", "input_spec": "The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.", "output_spec": "Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing \"0 1\".", "sample_inputs": [")((())))(()())", "))("], "sample_outputs": ["6 2", "0 1"], "notes": null}, "positive_code": [{"source_code": "line = gets\nline.strip!\nlongest = [] # )对应的最长的sequence长度\nopn = []\ni = 0\nline.each_char do |ch|\n\tif ch == '('\n\t\topn << i\n\telse\n\t\tif opn.empty?\n\t\t\tlongest[i] = -1\n\t\telse\n\t\t\trelative = opn.pop\n\t\t\tlongest[i] = i - relative + 1 # 不是最优,需要向前搜索\n\t\t\tif relative > 0 && line[relative - 1] == \")\" && longest[relative - 1] != -1\n\t\t\t\tlongest[i] += longest[relative - 1]\n\t\t\tend\n\t\tend\n\tend\n\ti += 1\nend\nmax = 0\ncnt = 1\n# 不能使用.max方法,因为数组中有nil,不能将nil与整数相比较\nlongest.each do |item|\n\tif item && (item > max)\n\t\tmax = item\n\t\tcnt = 1\n\telsif item == max\n\t\tcnt += 1\n\tend\nend\nputs \"#{max} #{cnt}\""}, {"source_code": "str = gets\nc = Array.new\nstack = Array.new\ni = 0\nstr.strip!\nstr.each_char do |s|\n if s==\"(\"\n stack << i\n else\n if stack.empty?\n c[i]=-1\n else\n corr=stack.pop\n c[i]=i-corr+1\n if corr>0 and c[corr-1] and c[corr-1]!=-1\n c[i]+=c[corr-1]\n end\n end\n end\n i+=1\nend\nmax=0\ncnt=1\nc.each do |cc|\n if cc and cc>max\n max=cc\n cnt=1\n elsif cc==max\n cnt+=1\n end\nend\nputs \"#{max} #{cnt}\"\n"}, {"source_code": "s = gets.chomp\nh = Hash.new(0)\ncount = 0\nstk = [-1]\n(0...s.length).each { |i|\n\ta = s[i]\n\tif a == \"(\"\n\t\tstk.push(i)\n\telse\n\t\tstk.pop\n\t\tif stk.empty?\n\t\t\tstk.push(i)\n\t\telse\n\t\t\th[i - stk.last] += 1\n\t\tend\n\tend\n}\nv, c = h.sort_by { |k, v| k }.last\nc = 1 if !v || v == 0\nputs \"#{v.to_i} #{c.to_i}\"\n"}, {"source_code": "s = gets.chomp\n\ndiff = 0\nmatch = []\nstack = []\n\n(0...(s.size)).each do |i|\n\tif s[i] == '('\n\t\tdiff += 1\n\t\tstack << i\n\telsif s[i] == ')' and diff > 0\n\t\tdiff -= 1\n\t\tindex = stack.pop\n\t\tmatch[index] = i\n\tend\nend\n\ntime = 0\nmax = 0\nrecord = Array.new(s.size)\n(0...s.size).each do |i|\n\ttotal = 0\n\tif (not match[i].nil?) and record[i].nil?\n\t\trecord[i] = true\n\t\ttotal += (match[i] - i + 1)\n\t\tj = match[i] + 1\n\t\twhile match[j] != nil\n\t\t\ttotal += (match[j] - j + 1)\n\t\t\trecord[j] = true\n\t\t\tj = match[j] + 1\n\t\tend\n\t\t\n\t\tif max < total\n\t\t\tmax = total\n\t\t\ttime = 1\n\t\telsif max == total\n\t\t\ttime += 1\n\t\tend\n\tend\nend\n\ntime = 1 if max == 0\n\nputs \"#{max} #{time}\"\n"}], "negative_code": [{"source_code": "s = gets.chomp\nh = Hash.new(0)\ncount = 0\nstk = []\n(0...s.length).each { |i|\n\ta = s[i]\n\tif a == \"(\"\n\t\tstk.push(a)\n\telse\n\t\tif stk.last == \"(\"\n\t\t\tstk.pop\n\t\t\tcount += 2\n\t\telse\n\t\t\tcount = 0\n\t\tend\n\t\th[count] += 1\n\tend\n}\nv, c = h.sort_by { |k, v| k }.last\nc = 1 if !v\nputs \"#{v.to_i} #{c.to_i}\"\n"}, {"source_code": "s = gets.chomp\nh = Hash.new(0)\ncount = 0\nstk = []\n(0...s.length).each { |i|\n\ta = s[i]\n\tif a == \"(\"\n\t\tstk.push(a)\n\telse\n\t\tif stk.last == \"(\"\n\t\t\tstk.pop\n\t\t\tcount += 2\n\t\telse\n\t\t\tcount = 0\n\t\tend\n\t\th[count] += 1\n\tend\n}\nv, c = h.sort_by { |k, v| k }.last\nc = 1 if !v || v == 0\nputs \"#{v.to_i} #{c.to_i}\"\n"}, {"source_code": "s = gets.chomp\nh = Hash.new(0)\ncount = 0\nstk = []\n(0...s.length).each { |i|\n\ta = s[i]\n\tif a == \"(\"\n\t\tstk.push(a)\n\telse\n\t\tif stk.last == \"(\"\n\t\t\tstk.pop\n\t\t\tcount += 2\n\t\t\th[count] += 1\n\t\telse\n\t\t\tcount = 0\n\t\tend\n\tend\n}\nv, c = h.sort_by { |k, v| v }.last\nc = 1 if !v\nputs \"#{v.to_i} #{c.to_i}\"\n"}, {"source_code": "s = gets.chomp\n\ndiff = 0\nmatch = []\nstack = []\n\n(0...(s.size)).each do |i|\n\tif s[i] == '('\n\t\tdiff += 1\n\t\tstack << i\n\telsif s[i] == ')' and diff > 0\n\t\tdiff -= 1\n\t\tindex = stack.pop\n\t\tmatch[index] = i\n\t\tmatch[i] = index\n\tend\nend\n\nmax = -1\ntime = 0\n(0...s.size).each do |i|\n\tnext if match[i].nil?\n\n\tif max < (i - match[i]).abs\n\t\tmax = (i - match[i]).abs\n\t\ttime += 1\n\telsif max == (i - match[i]).abs\n\t\ttime += 1\n\tend\n\nend\n\ntime = 2 if max == -1\n\nputs \"#{max + 1} #{time / 2}\"\n"}], "src_uid": "91c3af92731cd7d406674598c0dcbbbc"} {"nl": {"description": "Emuskald is an avid horticulturist and owns the world's longest greenhouse — it is effectively infinite in length.Over the years Emuskald has cultivated n plants in his greenhouse, of m different plant species numbered from 1 to m. His greenhouse is very narrow and can be viewed as an infinite line, with each plant occupying a single point on that line.Emuskald has discovered that each species thrives at a different temperature, so he wants to arrange m - 1 borders that would divide the greenhouse into m sections numbered from 1 to m from left to right with each section housing a single species. He is free to place the borders, but in the end all of the i-th species plants must reside in i-th section from the left.Of course, it is not always possible to place the borders in such way, so Emuskald needs to replant some of his plants. He can remove each plant from its position and place it anywhere in the greenhouse (at any real coordinate) with no plant already in it. Since replanting is a lot of stress for the plants, help Emuskald find the minimum number of plants he has to replant to be able to place the borders.", "input_spec": "The first line of input contains two space-separated integers n and m (1 ≤ n, m ≤ 5000, n ≥ m), the number of plants and the number of different species. Each of the following n lines contain two space-separated numbers: one integer number si (1 ≤ si ≤ m), and one real number xi (0 ≤ xi ≤ 109), the species and position of the i-th plant. Each xi will contain no more than 6 digits after the decimal point. It is guaranteed that all xi are different; there is at least one plant of each species; the plants are given in order \"from left to the right\", that is in the ascending order of their xi coordinates (xi < xi + 1, 1 ≤ i < n).", "output_spec": "Output a single integer — the minimum number of plants to be replanted.", "sample_inputs": ["3 2\n2 1\n1 2.0\n1 3.100", "3 3\n1 5.0\n2 5.5\n3 6.0", "6 3\n1 14.284235\n2 17.921382\n1 20.328172\n3 20.842331\n1 25.790145\n1 27.204125"], "sample_outputs": ["1", "0", "2"], "notes": "NoteIn the first test case, Emuskald can replant the first plant to the right of the last plant, so the answer is 1.In the second test case, the species are already in the correct order, so no replanting is needed."}, "positive_code": [{"source_code": "n=gets.split[0].to_i;a=(R=0...n).map{gets.split[0].to_i};b=[0]+[M=8192]*M;q=0;R.each{|i|c=0;s=M;c+=s if b[c+(s>>=1)]<=a[i] while s>0;b[c+1]=a[i];q=c if q>=1)]<=a[i] while s>0;b[c+1]=a[i];q=c if q>=1)]<=a[i] while s>0;b[c+1]=a[i];q=c if q>=1)]<=a[i] while s>0;b[c+1]=a[i];q=c if q>=1)]<=a[i] while s>0;b[c+1]=a[i];q=c if q>=1)]<=a[i] while s>0;b[c+1]=a[i];q=c if q>=1)]<=a[i] while s>0;b[c+1]=a[i];q=c if q0) do \n\t\tc+=s if b[c+s]<=a[i]\n\t\ts>>=1\n\tend\n\tb[c+1]=a[i]\nend\nc=M-1;\nwhile (b[c]>=M) do \n c-=1\nend\np a.size-c\n"}, {"source_code": "n=(a=STDIN.read.split(\"\\n\")[1..-1].map{|x|x.split[0].to_i}).size\nb=[0]+[M=9999]*M\nfor i in 0...n \n\tc=0;s=8192;\n\tc+=s if b[c+(s>>=1)]<=a[i] while s>0 \n\tb[c+1]=a[i]\nend\nc=M-1;\nwhile (b[c]>=M) do \n c-=1\nend\np n-c\n"}, {"source_code": "n=gets.split[0].to_i;a=(0...n).map{gets.split[0].to_i};b=[0]+[M=9999]*M;q=0;for i in 0...n\nc=0;s=8192;c+=s if b[c+(s>>=1)]<=a[i] while s>0;b[c+1]=a[i];q=c if q>=1)]<=a[i] while s>0;b[c+1]=a[i]\nend\nc=M;c-=1 while b[c]>=M;p n-c"}, {"source_code": "n=gets.split[0].to_i;a=(R=0...n).map{gets.split[0].to_i};b=[0]+[M=8192]*M;q=0;R.each{|i|c=0;s=M;c+=s if b[c+(s>>=1)]<=a[i] while s>0;b[c+1]=a[i];q=c if q>=1)]<=a[i] while s>0;b[c+1]=a[i];q=c if q>=1)]<=a[i] while s>0;b[c+1]=a[i];q=c if q>=1)]<=a[i] while s>0;b[c+1]=a[i];q=c if q= 3\n puts 'Yes'\n elsif even_number == 2\n puts 'No'\n elsif arr.take(3).include?(0)\n puts 'No'\n else\n puts 'Yes'\n end \nend\n"}, {"source_code": "t = gets.to_i\nt.times do\n\tr, g, b, w = gets.split.map &:to_i\n\tis_ok = [r, g, b, w].count(&:odd?) < 2\n\tis_ok ||= [r, g, b].min > 0 && [r-1, g-1, b-1, w+3].count(&:odd?) < 2\n\tputs (is_ok ? \"Yes\" : \"No\")\nend"}, {"source_code": "t = gets.to_i\nans = []\nt.times do\n r,g,b,w = gets.chomp.split(\" \").map(&:to_i)\n sum = r+g+b+w\n if sum % 2 == 0\n if r%2==0 && g%2==0 && b%2==0 && w%2==0\n ans << \"YES\"\n next\n end\n else\n judge = 0\n judge += 1 if r%2==1\n judge += 1 if g%2==1\n judge += 1 if b%2==1\n judge += 1 if w%2==1\n if judge == 1\n ans << \"YES\"\n next\n end\n end\n if r > 0 && g > 0 && b > 0\n r -= 1\n g -= 1\n b -= 1\n w += 3\n if sum % 2 == 0\n if r%2==0 && g%2==0 && b%2==0 && w%2==0\n ans << \"YES\"\n next\n end\n else\n judge = 0\n judge += 1 if r%2==1\n judge += 1 if g%2==1\n judge += 1 if b%2==1\n judge += 1 if w%2==1\n if judge == 1\n ans << \"YES\"\n next\n end\n end\n end\n ans << \"NO\"\nend\nans.each do |s|\n puts s\nend"}], "negative_code": [{"source_code": "gets.to_i.times do\n arr = gets.split(' ').map(&:to_i)\n even_number = arr.count { |num| num.even? }\n \n if even_number >= 3\n puts 'Yes'\n elsif even_number == 2\n puts 'No'\n elsif arr.include?(0)\n puts 'No'\n else\n puts 'Yes'\n end \nend\n"}, {"source_code": "t = gets.to_i\nt.times do\n\tr, g, b, w = gets.split.map &:to_i\n\tis_ok = [r, g, b, w].count(&:odd?) < 2\n\tis_ok ||= [r, g, b].min > 0 && [r-1, g-1, b-1, w].count(&:odd?) < 2\n\tputs (is_ok ? \"Yes\" : \"No\")\nend"}, {"source_code": "t = gets.to_i\nt.times do\n\tr, g, b, w = gets.split.map &:to_i\n\tis_ok = [r, g, b, w].count(&:odd?) < 2\n\tis_ok ||= [r, g, b].min > 0 && [r-1, g-1, b-1, w].count(&:odd?)\n\tputs (is_ok ? \"Yes\" : \"No\")\nend"}], "src_uid": "749a106d462555543c91753f00a5a479"} {"nl": {"description": "A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 ≤ pi ≤ n).A lucky permutation is such permutation p, that any integer i (1 ≤ i ≤ n) meets this condition ppi = n - i + 1.You have integer n. Find some lucky permutation p of size n.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 105) — the required permutation size.", "output_spec": "Print \"-1\" (without the quotes) if the lucky permutation p of size n doesn't exist. Otherwise, print n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) after a space — the required permutation. If there are multiple answers, you can print any of them.", "sample_inputs": ["1", "2", "4", "5"], "sample_outputs": ["1", "-1", "2 4 1 3", "2 5 3 1 4"], "notes": null}, "positive_code": [{"source_code": "N = gets.chomp.to_i\na = Array.new(N+1)\nif N.modulo(4) <= 1\n (1..N/2).step(2) do |i|\n a[i], a[i+1], a[N-i+1], a[N-i] = i+1, N-i+1, N-i, i\n end\n a[N / 2 + 1] = N / 2 + 1 if N % 2 == 1\n puts a[1..N].join(' ') \nelsif\n puts \"-1\"\nend"}, {"source_code": "n = gets.to_i\n\np = [0]*n\n\n#puts \"#{n} --- #{p}\"\n\nif n%4!=0 and n%4!=1\n\tputs -1\nelse\n\tk = n/4\n\t(1..k).each { |t|\n#\t puts \"#{v} #{k}\"\n v = 2*t-1\n\t\tp[v-1]=v+1\n\t\tp[v] = n-v+1\n\t\tp[n-v] = n-v\n\t\tp[n-v-1] = v\n\t}\n\tif n%2==1\n\t\tp[2*k]=2*k+1\n\tend\n\tputs p.join(\" \")\nend\n\n"}, {"source_code": "n,a=gets.to_i,[]\nexit !puts('-1')if n%4>1\n1.step(n/2,2){|i|a[i]=n-i;a[i+1]=i;a[n-i]=n-i+1;a[n-i+1]=i+1}\na[n/2+1]=n/2+1 if n.odd?\nputs a.join' '\n"}, {"source_code": "n,a=gets.to_i,[]\nexit !puts('-1')if n%4>1\n1.step(n/2,2){|i|a[i]=n-i;a[i+1]=i;a[n-i]=n-i+1;a[n-i+1]=i+1}\na[n/2+1]=n/2+1 if n.odd?\nputs a.join' '\n"}, {"source_code": "n,a=gets.to_i,[]\nexit !puts('-1')if n%4>1\n1.step(n/2,2){|i|a[i]=n-i;a[i+1]=i;a[n-i]=n-i+1;a[n-i+1]=i+1}\na[n/2+1]=n/2+1 if n.odd?\nputs a.join' '"}, {"source_code": "n,a=gets.to_i,[]\nexit !puts('-1')if n%4>1\n1.step(n/2,2){|i|a[i]=n-i;a[i+1]=i;a[n-i]=n-i+1;a[n-i+1]=i+1}\na[n/2+1]=n/2+1 if n.odd?\nputs a.join' '\n"}, {"source_code": "n,a=gets.to_i,[]\nexit !puts('-1')if n%4>1\n1.step(n/2,2){|i|a[i]=n-i;a[i+1]=i;a[n-i]=n-i+1;a[n-i+1]=i+1}\na[n/2+1]=n/2+1 if n.odd?\nputs a.join' '\n"}, {"source_code": "N = gets.chomp.to_i\na = Array.new(N+1)\nif N.modulo(4) <= 1\n (1..N/2).step(2) do |i|\n a[i], a[i+1], a[N-i+1], a[N-i] = i+1, N-i+1, N-i, i\n end\n a[N / 2 + 1] = N / 2 + 1 if N % 2 == 1\n puts a[1..N].join(' ') \nelsif\n puts \"-1\"\nend"}, {"source_code": "n,a=gets.to_i,[]\nexit !puts('-1')if n%4>1\n1.step(n/2,2){|i|a[i]=n-i;a[i+1]=i;a[n-i]=n-i+1;a[n-i+1]=i+1}\na[n/2+1]=n/2+1 if n.odd?\nputs a.join' '"}, {"source_code": "n,a=gets.to_i,[]\nexit !puts('-1')if n%4>1\n1.step(n/2,2){|i|a[i]=n-i;a[i+1]=i;a[n-i]=n-i+1;a[n-i+1]=i+1}\na[n/2+1]=n/2+1 if n.odd?\nputs a.join' '\n"}, {"source_code": "n = gets.to_i\n\nif n % 4 == 2 || n % 4 == 3 then\n puts \"-1\"\n exit\nend \n\nret = Array.new(n, 0)\nret[n / 2] = n / 2 if n % 2\ni = 0\nwhile i < n / 2 do\n ret[i], ret[i + 1], ret[n - 2 - i], ret[n - 1 - i] = i + 1, n - 1 - i, i, n - 2 - i\n i += 2\nend\n\nret.map!{ |i| i + 1 }\nputs ret.join(' ')"}, {"source_code": "n,a=gets.to_i,[]\nexit !puts('-1')if n%4>1\n1.step(n/2,2){|i|a[i]=n-i;a[i+1]=i;a[n-i]=n-i+1;a[n-i+1]=i+1}\na[n/2+1]=n/2+1 if n.odd?\nputs a.join' '\n"}, {"source_code": "n = gets.to_i;\n\nif n % 4 > 1 \n puts \"-1\"\nelse\n a = Array.new(n);\n (1..(n/4)).each{|i|\n j = (i-1)*2 + 1;\n a[j-1] = j+1;\n a[j] = n - j + 1;\n a[n-j] = n - j;\n a[n-j-1] = j;\n }\n a[n/2] = n/2+1 if n % 4 ==1\n puts a.join(' ');\nend\n"}, {"source_code": "n,a=gets.to_i,[]\nexit !puts('-1')if n%4>1\n1.step(n/2,2){|i|a[i]=n-i;a[i+1]=i;a[n-i]=n-i+1;a[n-i+1]=i+1}\na[n/2+1]=n/2+1 if n.odd?\nputs a.join' '\n"}, {"source_code": "n,a=gets.to_i,[]\nexit !puts('-1')if n%4>1\n1.step(n/2,2){|i|a[i]=n-i;a[i+1]=i;a[n-i]=n-i+1;a[n-i+1]=i+1}\na[n/2+1]=n/2+1 if n.odd?\nputs a.join' '\n"}], "negative_code": [{"source_code": "n = gets.to_i\n\np = [0]*n\n\n#puts \"#{n} --- #{p}\"\n\nif n%4!=0 and n%4!=1\n\tputs -1\nelse\n\tk = n/4\n\t(1..k).each { |v|\n#\t puts \"#{v} #{k}\"\n\t\tp[v-1]=v+1\n\t\tp[v] = n-v+1\n\t\tp[n-v] = n-v\n\t\tp[n-v-1] = v\n\t}\n\tif n%2==1\n\t\tp[2*k]=2*k+1\n\tend\n\tputs p.join(\" \")\nend\n\n"}], "src_uid": "dfca19b36c1d682ee83224a317e495e9"} {"nl": {"description": "Andrew often reads articles in his favorite magazine 2Char. The main feature of these articles is that each of them uses at most two distinct letters. Andrew decided to send an article to the magazine, but as he hasn't written any article, he just decided to take a random one from magazine 26Char. However, before sending it to the magazine 2Char, he needs to adapt the text to the format of the journal. To do so, he removes some words from the chosen article, in such a way that the remaining text can be written using no more than two distinct letters.Since the payment depends from the number of non-space characters in the article, Andrew wants to keep the words with the maximum total length.", "input_spec": "The first line of the input contains number n (1 ≤ n ≤ 100) — the number of words in the article chosen by Andrew. Following are n lines, each of them contains one word. All the words consist only of small English letters and their total length doesn't exceed 1000. The words are not guaranteed to be distinct, in this case you are allowed to use a word in the article as many times as it appears in the input.", "output_spec": "Print a single integer — the maximum possible total length of words in Andrew's article.", "sample_inputs": ["4\nabb\ncacc\naaa\nbbb", "5\na\na\nbcbcb\ncdecdecdecdecdecde\naaaa"], "sample_outputs": ["9", "6"], "notes": "NoteIn the first sample the optimal way to choose words is {'abb', 'aaa', 'bbb'}.In the second sample the word 'cdecdecdecdecdecde' consists of three distinct letters, and thus cannot be used in the article. The optimal answer is {'a', 'a', 'aaaa'}."}, "positive_code": [{"source_code": "ws = []\ngets.to_i.times do\n ws << gets.strip\nend\n\nm = 0\n('a'..'z').each do |c1|\n ('a'..'z').each do |c2|\n s = ws.select {|w| w.chars.all?{|c| c == c1 || c == c2 } }.join.size\n m = [m, s].max\n end\nend\np m\n"}, {"source_code": "# Problem: http://codeforces.com/contest/593/problem/A\na = []\nrs = [0]\ngets.to_i.times {a << gets.chomp }\nfor i in ('a'..'z')\n for j in (i..'z')\n sum = 0\n a.each do |s|\n continue = true\n s.split(\"\").each do |c|\n if c != i and c != j\n continue = false\n end\n end\n if continue == true\n sum += s.size\n end\n end\n if sum > 0\n rs << sum\n end\n end\nend\nputs rs.max\n"}, {"source_code": "# Problem: http://codeforces.com/contest/593/problem/A\na = []\nrs = [0]\ngets.to_i.times {a << gets.chomp }\nfor i in ('a'..'z')\n for j in (i..'z')\n sum = 0\n a.each do |s|\n continue = true\n s.size.times do |k|\n if s[k] != i and s[k] != j\n continue = false\n end\n end\n if continue == true\n sum += s.size\n end\n end\n if sum > 0\n rs << sum\n end\n end\nend\nputs rs.max\n"}, {"source_code": "# Problem: http://codeforces.com/contest/593/problem/A\na = []\nd = Hash.new 0\ngets.to_i.times {a << gets.chomp }\na.each do |s|\n s_asize = s.size\n s_uniq = s.split(\"\").uniq\n s_size = s_uniq.size\n if s_size > 2\n next\n end\n if s_size == 1\n c = s_uniq.pop\n d[c] += s_asize\n d.keys.each do |k|\n if k.size == 2 and k.include? c\n d[k] += s_asize\n elsif k.size == 1 and k != c\n new_k = [k, c].sort.join\n if d[new_k] == 0\n d[new_k] = d[k] + d[c]\n end\n end\n end\n end\n if s_size == 2\n k = s_uniq.sort.join\n if d[k] == 0\n s_uniq.each { |t| d[k] += d[t] }\n end\n d[k] += s_asize\n end\nend\nif d.empty?\n puts 0\nelse\n puts d.values.max\nend\n"}, {"source_code": "a=(\"a\" .. \"z\" ).to_a\nhash={}\n0.upto(25) {|i| hash[a[i]]=i }\ngrid = Array.new(26) { Array.new(26,0) }\na=gets.chomp.to_i\na.times do\ntemp=gets.chomp.split(\"\")\nt1=temp.length\ntemp.uniq!\nt2=temp.length\nif t2<3\nif t2==1\nr=hash[temp[0]]\nc=hash[temp[0]]\ngrid[r][c]+=t1\nelse\nr=hash[temp[0]]\nc=hash[temp[1]]\ngrid[r][c]+=t1\ngrid[c][r]+=t1\nend\nend\nend\nans=0\n\n0.upto(25) do |i|\n0.upto(25) do |j|\nif i!=j\ntemp=grid[i][i]+grid[i][j]+grid[j][j]\nif temp>ans\nans=temp\nend\nend\nend\nend\nputs \"#{ans}\""}, {"source_code": "require 'set'\n\nfile = File.exist?('in.in') ? File.new('in.in', 'r') : STDIN\n\ndef init_both(char1,char2)\n total = 0\n words = []\n [char1,char2].each do |char|\n if @global_count[char]\n total += @global_count[char].total\n words << @global_count[char].words\n end\n end\n Pair.new(total, words.flatten)\nend\n\nn = file.gets.to_i\nPair = Struct.new(:total, :words)\n@global_count = {}\nglobal_words = {}\n\nwhile n > 0 do\n n -= 1\n word = file.gets.chomp\n\n valid_word = true\n char1 = word.chars.first\n char2 = nil\n current_count = {}\n word.chars.each do |char|\n char2 ||= char if char != char1\n if char != char1 && char != char2\n valid_word = false\n break\n end\n current_count[char] ||= 0\n current_count[char] += 1\n end\n if valid_word\n if char2\n both_key = \"#{char1}#{char2}\".chars.sort.join\n @global_count[both_key] ||= init_both(char1,char2)\n @global_count[both_key].total += word.length\n @global_count[both_key].words << word\n global_words[char1] ||= Set.new [char1]\n global_words[char1].add both_key\n global_words[char2] ||= Set.new [char2]\n global_words[char2].add both_key\n else\n @global_count[char1] ||= Pair.new(0, [])\n global_words[char1] ||= Set.new [char1]\n global_words[char1].add char1\n\n global_words[char1].each do |char|\n @global_count[char].total += word.length\n @global_count[char].words << word\n end\n end\n end\nend\nglobal_words.each do |k,v|\n\tif k.length == 1\n\t\tglobal_words.each do |q,b|\n\t\t\tboth_key = \"#{q}#{k}\".chars.sort.join\n\t\t\tif q != k && q.length == 1\n\t\t\t\tglobal_words[k].add both_key\n\t\t\t\t@global_count[both_key] ||= init_both(k,q)\n\t\t\tend\n\t\tend\n\tend\nend\n@global_count = @global_count.sort {|a1,a2| a2[1].total <=> a1[1].total }\n@global_count = Hash[*@global_count.flatten]\n# puts @global_count\n# puts global_words\nif @global_count.values.first\n answer = @global_count.values.first.total\n puts answer || 0\nelse\n puts 0\nend"}, {"source_code": "require 'set'\n\nfile = File.exist?('in.in') ? File.new('in.in', 'r') : STDIN\n\ndef init_both(char1,char2)\n total = 0\n [char1,char2].each do |char|\n if @global_count[char]\n total += @global_count[char]\n end\n end\n total\nend\n\nn = file.gets.to_i\n@global_count = {}\nglobal_words = {}\n\nwhile n > 0 do\n n -= 1\n word = file.gets.chomp\n\n valid_word = true\n char1 = word.chars.first\n char2 = nil\n current_count = {}\n word.chars.each do |char|\n char2 ||= char if char != char1\n if char != char1 && char != char2\n valid_word = false\n break\n end\n current_count[char] ||= 0\n current_count[char] += 1\n end\n if valid_word\n if char2\n both_key = \"#{char1}#{char2}\".chars.sort.join\n @global_count[both_key] ||= init_both(char1,char2)\n @global_count[both_key] += word.length\n global_words[char1] ||= Set.new [char1]\n global_words[char1].add both_key\n global_words[char2] ||= Set.new [char2]\n global_words[char2].add both_key\n else\n @global_count[char1] ||= 0\n global_words[char1] ||= Set.new [char1]\n global_words[char1].add char1\n\n global_words[char1].each do |char|\n @global_count[char] += word.length\n end\n end\n end\nend\n# Generate all possible combinations for every single word with word.length == 1\nglobal_words.each do |k,v|\n\tif k.length == 1\n\t\tglobal_words.each do |q,b|\n\t\t\tboth_key = \"#{q}#{k}\".chars.sort.join\n\t\t\tif q != k && q.length == 1\n\t\t\t\tglobal_words[k].add both_key\n\t\t\t\t@global_count[both_key] ||= init_both(k,q)\n\t\t\tend\n\t\tend\n\tend\nend\n\n@global_count = @global_count.sort {|a1,a2| a2[1] <=> a1[1] }\n@global_count = Hash[*@global_count.flatten]\nputs @global_count.values.first || 0"}, {"source_code": "n = gets.chomp.to_i\nhs = Hash.new(0)\nfor c0 in 'a'..'y'\n for c1 in 'a'..'z'\n hs[c0 + c1] = 0 if c0 < c1\n end\nend\n\nn.times do\n line = gets.chomp\n cs = line.split('').sort.uniq\n if cs.size == 1\n hs[cs[0]] += line.size\n elsif cs.size == 2\n hs[cs[0] + cs[1]] += line.size\n end\nend\n\nrtn = 0\n\nhs.each do |k, v|\n if k.size == 2\n hs[k] += hs[k[0]] + hs[k[1]]\n end\nend\n\nhs.each do |k, v|\n rtn = [rtn, v].max\nend\n\nputs rtn\n"}, {"source_code": "def max(a, b)\n return a > b ? a : b\nend\nn = gets.chomp.to_i\nv = []\nfor i in(1..n)\n v[i-1] = gets.chomp\nend\nc = 0\nfor i in('a'..'z')\n for j in(i..'z')\n c1 = 0\n for x in(1..n)\n sw = 0\n for y in(0..(v[x-1].size-1))\n if v[x-1][y] == i || v[x-1][y] == j\n else\n sw = 1\n end\n end\n if sw==0\n c1+=v[x-1].size\n end\n end\n c = max(c,c1)\n end\nend\nputs c\n"}, {"source_code": "n = gets.chomp.to_i\nd = Hash.new(0)\nn.times do\n word = gets.chomp\n k = word.split(\"\").uniq.sort\n v = word.length\n d[k] += v if k.length <= 2\nend\nres = 0\n(?a..?z).to_a.combination(2) do |kk|\n res = [res, d[[kk[0]]] + d[[kk[1]]] + d[kk]].max\nend\nputs res"}, {"source_code": "def get_array(arr)\n a = arr[0]\n b = arr.delete(a)[0]\n return [a,b]\nend\nn = gets.chomp.to_i\nx, arr, len, arr1, l1, sum = [], [], [0], [], [], 0 #len初始化为0,防止所有输入都不符合,返回0\n(1..n).each do |i|\n a = gets.chomp\n t = String.new(a)\n num = 0\n a.each_char do |x|\n if a.delete(x) != a\n a.delete!(x)\n num += 1\n end\n break if num == 3\n end\n arr << t if num < 3\nend\narr.each_with_index do |arr1, index1|\n len[index1] = arr1.length\n arr.each_with_index do |arr2, index2|\n next if index1 == index2\n len[index1] += arr2.length if get_array(arr1) == get_array(arr2) || get_array(arr1).reverse == get_array(arr2) || arr1.squeeze.include?(arr2.squeeze)\n end\nend\nl = len.sort.last\narr.each do |a|\n arr1 << a.squeeze if a.squeeze.length == 1\nend\nif arr1.length > 0 #排除超过一种字符的行,这里操作每行都是一种字符的情况\n arr1.uniq.each do |a1|\n sum = 0\n arr.each do |a|\n sum += a.length if a1 == a.squeeze\n end\n l1 << sum\n end\n l1.sort!\n p l1.last + l1[l1.length-2] > l && l1.length > 1 ? l1.last + l1[l1.length-2] : l\nelse\n p l\nend"}, {"source_code": "def get_arr(arr)\n a = arr[0]\n b = arr.delete(a)[0]\n return [a,b]\nend\n\ndef exist?(arr, arr1)\n arr.each do |x|\n if x == arr1\n return true\n break\n end\n end\n false\nend\nn = gets.chomp.to_i\nx, arr, array, len, arr1, l1, sum = [], [], [], [0], [], [], 0\n(1..n).each do |i|\n a = gets.chomp\n t = String.new(a)\n num = 0\n a.each_char do |x|\n if a.delete(x) != a\n a.delete!(x)\n num += 1\n end\n if num == 3\n break\n end\n end\n if num < 3\n arr << t\n end\nend\narr.each_with_index do |arr1, index1|\n len[index1] = arr1.length\n if !exist?(array, arr1)\n arr.each_with_index do |arr2, index2|\n if index1 == index2\n next\n else\n if get_arr(arr1) == get_arr(arr2) || get_arr(arr1).reverse == get_arr(arr2) || arr1.squeeze.include?(arr2.squeeze)\n len[index1] += arr2.length\n end\n end\n end\n array << get_arr(arr1)\n end\nend\nl = len.sort.last\narr.each do |a|\n if a.squeeze.length == 1\n arr1 << a.squeeze\n end\nend\nif arr1.length > 0\n arr1.uniq.each do |a1|\n sum = 0\n arr.each do |a|\n if a1 == a.squeeze\n sum += a.length\n end\n end\n l1 << sum\n end\n l1.sort!\n p l1.last + l1[l1.length-2] > l && l1.length > 1 ? l1.last + l1[l1.length-2] : l\nelse\n p l\nend\n"}, {"source_code": "def get_arr(arr)\n a = arr[0]\n b = arr.delete(a)[0]\n return [a,b]\nend\n# def exist?(arr, arr1)\n# arr.each do |x|\n# if x == arr1\n# true\n# break\n# end\n# end\n# false\n# end\nn = gets.chomp.to_i\nx, arr, array, len, arr1, l1, sum = [], [], [], [0], [], [], 0\n(1..n).each do |i|\n a = gets.chomp\n t = String.new(a)\n num = 0\n a.each_char do |x|\n if a.delete(x) != a\n a.delete!(x)\n num += 1\n end\n if num == 3\n break\n end\n end\n if num < 3\n arr << t\n end\nend\narr.each_with_index do |arr1, index1|\n len[index1] = arr1.length\n # if !exist?(array, arr1)\n arr.each_with_index do |arr2, index2|\n if index1 == index2\n next\n else\n if get_arr(arr1) == get_arr(arr2) || get_arr(arr1).reverse == get_arr(arr2) || arr1.squeeze.include?(arr2.squeeze)\n len[index1] += arr2.length\n end\n end\n end\n array << get_arr(arr1)\n # end\nend\nl = len.sort.last\narr.each do |a|\n if a.squeeze.length == 1\n arr1 << a.squeeze\n end\nend\nif arr1.length > 0\n arr1.uniq.each do |a1|\n sum = 0\n arr.each do |a|\n if a1 == a.squeeze\n sum += a.length\n end\n end\n l1 << sum\n end\n l1.sort!\n p l1.last + l1[l1.length-2] > l && l1.length > 1 ? l1.last + l1[l1.length-2] : l\nelse\n p l\nend\n"}], "negative_code": [{"source_code": "# Problem: http://codeforces.com/contest/593/problem/A\na = []\nd = Hash.new 0\ngets.to_i.times {a << gets.chomp }\na.each do |s|\n s_asize = s.size\n s_uniq = s.split(\"\").uniq\n s_size = s_uniq.size\n if s_size > 2\n next\n end\n if s_size == 1\n c = s_uniq.pop\n d[c] += s_asize\n d.keys.each do |k|\n if k.size == 2 and k.include? c\n d[k] += s_asize\n end\n end\n end\n if s_size == 2\n k = s_uniq.sort.join\n d[k] += s_asize\n end\nend\nputs d.values.max\n"}, {"source_code": "# Problem: http://codeforces.com/contest/593/problem/A\na = []\nd = Hash.new 0\ngets.to_i.times {a << gets.chomp }\na.each do |s|\n s_asize = s.size\n s_uniq = s.split(\"\").uniq\n s_size = s_uniq.size\n if s_size > 2\n next\n end\n if s_size == 1\n c = s_uniq.pop\n d[c] += s_asize\n d.keys.each do |k|\n if k.size == 2 and k.include? c\n d[k] += s_asize\n elsif k.size == 1 and k != c\n new_k = [k, c].sort.join\n if d[new_k] == 0\n d[new_k] = d[k] + d[c]\n end\n end\n end\n end\n if s_size == 2\n k = s_uniq.sort.join\n d[k] += s_asize\n end\nend\nif d.empty?\n puts 0\nelse\n puts d.values.max\nend\n"}, {"source_code": "# Problem: http://codeforces.com/contest/593/problem/A\na = []\nd = Hash.new 0\ngets.to_i.times {a << gets.chomp }\na.each do |s|\n s_asize = s.size\n s_uniq = s.split(\"\").uniq\n s_size = s_uniq.size\n if s_size > 2\n next\n end\n if s_size == 1\n c = s_uniq.pop\n d[c] += s_asize\n d.keys.each do |k|\n if k.size == 2 and k.include? c\n d[k] += s_asize\n elsif k.size == 1 and k != c\n new_k = [k, c].sort.join\n if d[new_k] == 0\n d[new_k] = d[k] + d[c]\n end\n end\n end\n end\n if s_size == 2\n k = s_uniq.sort.join\n d[k] += s_asize\n d.keys.each do |t|\n if t.size == 1 and k.include? t\n d[k] += d[t]\n end\n end\n end\nend\nif d.empty?\n puts 0\nelse\n puts d.values.max\nend\n"}, {"source_code": "# Problem: http://codeforces.com/contest/593/problem/A\na = []\nd = Hash.new 0\ngets.to_i.times {a << gets.chomp }\na.each do |s|\n s_asize = s.size\n s_uniq = s.split(\"\").uniq\n s_size = s_uniq.size\n if s_size > 2\n next\n end\n if s_size == 1\n c = s_uniq.pop\n d[c] += s_asize\n d.keys.each do |k|\n if k.size == 2 and k.include? c\n d[k] += s_asize\n elsif k.size == 1 and k != c\n new_k = [k, c].sort.join\n d[new_k] = d[k] + d[c]\n end\n end\n end\n if s_size == 2\n k = s_uniq.sort.join\n d[k] += s_asize\n end\nend\nif d.empty?\n puts 0\nelse\n puts d.values.max\nend\n"}, {"source_code": "# Problem: http://codeforces.com/contest/593/problem/A\na = []\nd = Hash.new 0\ngets.to_i.times {a << gets.chomp }\na.each do |s|\n s_asize = s.size\n s_uniq = s.split(\"\").uniq\n s_size = s_uniq.size\n if s_size > 2\n next\n end\n if s_size == 1\n c = s_uniq.pop\n d[c] += s_asize\n d.keys.each do |k|\n if k.size == 2 and k.include? c\n d[k] += s_asize\n end\n end\n end\n if s_size == 2\n k = s_uniq.sort.join\n d[k] += s_asize\n end\nend\nif d.empty?\n puts 0\nelse\n puts d.values.max\nend\n"}, {"source_code": "require 'set'\nn = gets.to_i\nglobal_count = {}\nglobal_words = {}\n\nwhile n > 0 do\n\tn -= 1\n\tword = gets.chomp\n\n\tvalid_word = true\n\tchar1 = word.chars.first\n\tchar2 = nil\n\tcurrent_count = {}\n\tword.chars.each do |char|\n\t\tchar2 ||= char if char != char1\n\t\tif char != char1 && char != char2\n\t\t\tvalid_word = false\n\t\t\tbreak\n\t\tend\n\t\tcurrent_count[char] ||= 0\n\t\tcurrent_count[char] += 1\n\tend\n\n\tif valid_word\n\t\tboth_key = \"#{char1}#{char2}\".chars.sort.join\n\n\t\tglobal_count[char1] ||= 0\n\t\tglobal_words[char1] ||= Set.new [char1]\n\t\tglobal_count[both_key] ||= 0\n\t\tglobal_words[char1].add both_key\n\n\t\tif char2\n\n\t\t\tglobal_count[char2] ||= 0\n\t\t\tglobal_words[char2] ||= Set.new [char2]\n\t\t\tglobal_words[char2].add both_key\n\n\t\t\tglobal_count[both_key] += current_count[char1]\n\t\t\tglobal_count[char1] += current_count[char1]\n\t\t\tglobal_count[both_key] += current_count[char2]\n\t\t\tglobal_count[char2] += current_count[char2]\n\t\telse\n\t\t\tglobal_words[char1].each do |word|\n\t\t\t\tglobal_count[word] += current_count[char1]\n\t\t\tend\n\t\tend\n\tend\nend\n\nputs global_count.values.max\n"}, {"source_code": "require 'set'\n\nclass Pair\n attr_accessor :total, :words\n\n def initialize(total, words)\n @total = total\n @words = words\n end\nend\n\nn = gets.to_i\n# Pair = Struct.new(:total, :words)\n@global_count = {}\nglobal_words = {}\n\n# def init_both(char1,char2)\n# \ttotal = 0\n# \twords = []\n# \t[char1,char2].each do |char|\n# \t\tif @global_count[char]\n# \t\t\ttotal += @global_count[char].total\n# \t\t\twords = @global_count[char].words\n# \t\tend\n# \tend\n# \tPair.new(total, words)\n# end\n\n# while n > 0 do\n# \tn -= 1\n# \tword = gets.chomp\n\n# \tvalid_word = true\n# \tchar1 = word.chars.first\n# \tchar2 = nil\n# \tcurrent_count = {}\n# \tword.chars.each do |char|\n# \t\tchar2 ||= char if char != char1\n# \t\tif char != char1 && char != char2\n# \t\t\tvalid_word = false\n# \t\t\tbreak\n# \t\tend\n# \t\tcurrent_count[char] ||= 0\n# \t\tcurrent_count[char] += 1\n# \tend\n\n# \tif valid_word\n# \t\tif char2\n# \t\t\tboth_key = \"#{char1}#{char2}\".chars.sort.join\n# \t\t\t@global_count[both_key] ||= init_both(char1,char2)\n# \t\t\t@global_count[both_key].total += word.length\n# \t\t\t@global_count[both_key].words << word\n# \t\t\tglobal_words[char1] ||= Set.new [char1]\n# \t\t\tglobal_words[char1].add both_key\n# \t\t\tglobal_words[char2] ||= Set.new [char2]\n# \t\t\tglobal_words[char2].add both_key\n# \t\telse\n# \t\t\t@global_count[char1] ||= Pair.new(0, [])\n# \t\t\tglobal_words[char1] ||= Set.new [char1]\n# \t\t\tglobal_words[char1].add char1\n\n# \t\t\tglobal_words[char1].each do |char|\n# \t\t\t\t@global_count[char].total += word.length\n# \t\t\t\t@global_count[char].words << word\n# \t\t\tend\n# \t\tend\n# \tend\n# end\n\n# @global_count = @global_count.sort {|a1,a2| a2[1].total <=> a1[1].total }.to_h\n# if @global_count.values.first\n# \tanswer = @global_count.values.first.total\n# \tif @global_count.keys.first.length == 1\n# \t\tsize = 0\n# \t\tanswer = 0\n# \t\t@global_count.keys.each do |key|\n# \t\t\tif key.length == 1\n# \t\t\t\tsize +=1\n# \t\t\t\tanswer += @global_count[key].total\n# \t\t\tend\n# \t\t\tbreak if size == 2\n# \t\tend\n# \tend\n# \tputs answer || 0\n\n# else\n# \tputs 0\n# end\n"}, {"source_code": "require 'set'\nn = gets.to_i\nglobal_count = {}\nglobal_words = {}\n\nwhile n > 0 do\n\tn -= 1\n\tword = gets.chomp\n\n\tvalid_word = true\n\tchar1 = word.chars.first\n\tchar2 = nil\n\tcurrent_count = {}\n\tword.chars.each do |char|\n\t\tchar2 ||= char if char != char1\n\t\tif char != char1 && char != char2\n\t\t\tvalid_word = false\n\t\t\tbreak\n\t\tend\n\t\tcurrent_count[char] ||= 0\n\t\tcurrent_count[char] += 1\n\tend\n\n\tif valid_word\n\t\tboth_key = \"#{char1}#{char2}\".chars.sort.join\n\n\t\tglobal_count[char1] ||= 0\n\t\tglobal_words[char1] ||= Set.new [char1]\n\t\tglobal_count[both_key] ||= 0\n\t\tglobal_words[char1].add both_key\n\n\t\tif char2\n\n\t\t\tglobal_count[char2] ||= 0\n\t\t\tglobal_words[char2] ||= Set.new [char2]\n\t\t\tglobal_words[char2].add both_key\n\n\t\t\tglobal_count[both_key] += current_count[char1]\n\t\t\tglobal_count[char1] += current_count[char1]\n\t\t\tglobal_count[both_key] += current_count[char2]\n\t\t\tglobal_count[char2] += current_count[char2]\n\t\telse\n\t\t\tglobal_words[char1].each do |word|\n\t\t\t\tglobal_count[word] += current_count[char1]\n\t\t\tend\n\t\tend\n\tend\nend\n\nputs global_count.values.max || 0\n"}, {"source_code": "require 'set'\n\nfile = File.exist?('in.in') ? File.new('in.in', 'r') : STDIN\n\ndef init_both(char1,char2)\n total = 0\n words = []\n [char1,char2].each do |char|\n if @global_count[char]\n total += @global_count[char].total\n words = @global_count[char].words.dup\n end\n end\n Pair.new(total, words)\nend\n\nn = file.gets.to_i\nPair = Struct.new(:total, :words)\n@global_count = {}\nglobal_words = {}\n\nwhile n > 0 do\n n -= 1\n word = file.gets.chomp\n\n valid_word = true\n char1 = word.chars.first\n char2 = nil\n current_count = {}\n word.chars.each do |char|\n char2 ||= char if char != char1\n if char != char1 && char != char2\n valid_word = false\n break\n end\n current_count[char] ||= 0\n current_count[char] += 1\n end\n if valid_word\n if char2\n both_key = \"#{char1}#{char2}\".chars.sort.join\n @global_count[both_key] ||= init_both(char1,char2)\n @global_count[both_key].total += word.length\n @global_count[both_key].words << word\n global_words[char1] ||= Set.new [char1]\n global_words[char1].add both_key\n global_words[char2] ||= Set.new [char2]\n global_words[char2].add both_key\n else\n @global_count[char1] ||= Pair.new(0, [])\n global_words[char1] ||= Set.new [char1]\n global_words[char1].add char1\n\n global_words[char1].each do |char|\n @global_count[char].total += word.length\n @global_count[char].words << word\n end\n\n\t\t\tglobal_words.each do |k,v|\n\t\t\t\tboth_key = \"#{char1}#{k}\".chars.sort.join\n\t\t\t\tif char1 != k && k.length == 1\n\t\t\t\t\tglobal_words[char1].add both_key\n\t\t\t\t\t@global_count[both_key] ||= init_both(char1,k)\n\t\t\t\tend\n\t\t\tend\n\n end\n end\nend\n@global_count = @global_count.sort {|a1,a2| a2[1].total <=> a1[1].total }\n@global_count = Hash[*@global_count.flatten]\n# puts @global_count\n# puts global_words\nif @global_count.values.first\n answer = @global_count.values.first.total\n if @global_count.keys.first.length == 1\n size = 0\n answer = 0\n @global_count.keys.each do |key|\n if key.length == 1\n size +=1\n answer += @global_count[key].total\n end\n break if size == 2\n end\n end\n puts answer || 0\n\nelse\n puts 0\nend"}, {"source_code": "require 'set'\n\ndef init_both(char1,char2)\n total = 0\n words = []\n [char1,char2].each do |char|\n if @global_count[char]\n total += @global_count[char].total\n words = @global_count[char].words\n end\n end\n Pair.new(total, words)\nend\n\nn = gets.to_i\nPair = Struct.new(:total, :words)\n@global_count = {}\nglobal_words = {}\n\nwhile n > 0 do\n n -= 1\n word = gets.chomp\n\n valid_word = true\n char1 = word.chars.first\n char2 = nil\n current_count = {}\n word.chars.each do |char|\n char2 ||= char if char != char1\n if char != char1 && char != char2\n valid_word = false\n break\n end\n current_count[char] ||= 0\n current_count[char] += 1\n end\n if valid_word\n if char2\n both_key = \"#{char1}#{char2}\".chars.sort.join\n @global_count[both_key] ||= init_both(char1,char2)\n @global_count[both_key].total += word.length\n @global_count[both_key].words << word\n global_words[char1] ||= Set.new [char1]\n global_words[char1].add both_key\n global_words[char2] ||= Set.new [char2]\n global_words[char2].add both_key\n else\n @global_count[char1] ||= Pair.new(0, [])\n global_words[char1] ||= Set.new [char1]\n global_words[char1].add char1\n\n global_words.each do |k,v|\n if k != char1 && k.length == 1\n both_key = \"#{char1}#{k}\".chars.sort.join\n unless @global_count[both_key]\n @global_count[both_key] ||= init_both(char1,k)\n global_words[char1].add both_key\n end\n end\n end\n\n global_words[char1].each do |char|\n @global_count[char].total += word.length\n @global_count[char].words << word\n end\n end\n end\nend\n@global_count = @global_count.sort {|a1,a2| a2[1].total <=> a1[1].total }\n@global_count = Hash[*@global_count.flatten]\n\n# puts @global_count\n# puts global_words\n\nif @global_count.values.first\n answer = @global_count.values.first.total\n if @global_count.keys.first.length == 1\n size = 0\n answer = 0\n @global_count.keys.each do |key|\n if key.length == 1\n size +=1\n answer += @global_count[key].total\n end\n break if size == 2\n end\n end\n puts answer || 0\n\nelse\n puts 0\nend"}, {"source_code": "require 'set'\n\ndef init_both(char1,char2)\n total = 0\n words = []\n [char1,char2].each do |char|\n if @global_count[char]\n total += @global_count[char].total\n words = @global_count[char].words\n end\n end\n Pair.new(total, words)\nend\n\nn = gets.to_i\nPair = Struct.new(:total, :words)\n@global_count = {}\nglobal_words = {}\n\nwhile n > 0 do\n n -= 1\n word = gets.chomp\n\n valid_word = true\n char1 = word.chars.first\n char2 = nil\n current_count = {}\n word.chars.each do |char|\n char2 ||= char if char != char1\n if char != char1 && char != char2\n valid_word = false\n break\n end\n current_count[char] ||= 0\n current_count[char] += 1\n end\n if valid_word\n if char2\n both_key = \"#{char1}#{char2}\".chars.sort.join\n @global_count[both_key] ||= init_both(char1,char2)\n @global_count[both_key].total += word.length\n @global_count[both_key].words << word\n global_words[char1] ||= Set.new [char1]\n global_words[char1].add both_key\n global_words[char2] ||= Set.new [char2]\n global_words[char2].add both_key\n else\n @global_count[char1] ||= Pair.new(0, [])\n global_words[char1] ||= Set.new [char1]\n global_words[char1].add char1\n\n global_words.each do |k,v|\n if k != char1 && k.length == 1\n both_key = \"#{char1}#{k}\".chars.sort.join\n @global_count[both_key] ||= init_both(char1,char2)\n global_words[char1].add both_key\n end\n end\n\n global_words[char1].each do |char|\n @global_count[char].total += word.length\n @global_count[char].words << word\n end\n end\n end\nend\n@global_count = @global_count.sort {|a1,a2| a2[1].total <=> a1[1].total }\n@global_count = Hash[*@global_count.flatten]\n\n# puts @global_count\n# puts global_words\n\nif @global_count.values.first\n answer = @global_count.values.first.total\n if @global_count.keys.first.length == 1\n size = 0\n answer = 0\n @global_count.keys.each do |key|\n if key.length == 1\n size +=1\n answer += @global_count[key].total\n end\n break if size == 2\n end\n end\n puts answer || 0\n\nelse\n puts 0\nend"}, {"source_code": "require 'set'\nn = gets.to_i\nglobal_count = {}\nglobal_words = {}\n\nwhile n > 0 do\n\tn -= 1\n\tword = gets.chomp\n\n\tvalid_word = true\n\tchar1 = word.chars.first\n\tchar2 = nil\n\tcurrent_count = {}\n\tword.chars.each do |char|\n\t\tchar2 ||= char if char != char1\n\t\tif char != char1 && char != char2\n\t\t\tvalid_word = false\n\t\t\tbreak\n\t\tend\n\t\tcurrent_count[char] ||= 0\n\t\tcurrent_count[char] += 1\n\tend\n\n\tif valid_word\n\n\t\tboth_key = \"#{char1}#{char2}\".chars.sort.join\n\n\t\tglobal_count[char1] ||= 0\n\t\tglobal_words[char1] ||= Set.new [char1]\n\t\tglobal_words[char1].add both_key\n\n\t\tif char2\n\t\t\tglobal_count[char2] ||= 0\n\t\t\tglobal_words[char2] ||= Set.new [char2]\n\t\t\tglobal_words[char2].add both_key\n\t\t\tglobal_count[char2] += current_count[char2]\n\t\t\tglobal_count[char1] += current_count[char1]\n\n\t\t\tglobal_count[both_key] ||= 0\n\t\t\tglobal_count[both_key] = global_count[char1] + global_count[char2]\n\n\t\telse\n\t\t\tglobal_words[char1].each do |word|\n\t\t\t\tglobal_count[word] += current_count[char1]\n\t\t\tend\n\t\tend\n\tend\nend\nputs global_count.values.max || 0\n"}, {"source_code": "require 'set'\n\ndef init_both(char1,char2)\n total = 0\n words = []\n [char1,char2].each do |char|\n if @global_count[char]\n total += @global_count[char].total\n words = @global_count[char].words\n end\n end\n Pair.new(total, words)\nend\n\nn = gets.to_i\nPair = Struct.new(:total, :words)\n@global_count = {}\nglobal_words = {}\n\nwhile n > 0 do\n n -= 1\n word = gets.chomp\n\n valid_word = true\n char1 = word.chars.first\n char2 = nil\n current_count = {}\n word.chars.each do |char|\n char2 ||= char if char != char1\n if char != char1 && char != char2\n valid_word = false\n break\n end\n current_count[char] ||= 0\n current_count[char] += 1\n end\n if valid_word\n if char2\n both_key = \"#{char1}#{char2}\".chars.sort.join\n @global_count[both_key] ||= init_both(char1,char2)\n @global_count[both_key].total += word.length\n @global_count[both_key].words << word\n global_words[char1] ||= Set.new [char1]\n global_words[char1].add both_key\n global_words[char2] ||= Set.new [char2]\n global_words[char2].add both_key\n else\n @global_count[char1] ||= Pair.new(0, [])\n global_words[char1] ||= Set.new [char1]\n global_words[char1].add char1\n\n global_words[char1].each do |char|\n @global_count[char].total += word.length\n @global_count[char].words << word\n end\n end\n end\nend\n@global_count = @global_count.sort {|a1,a2| a2[1].total <=> a1[1].total }\n@global_count = Hash[*@global_count.flatten]\nif @global_count.values.first\n answer = @global_count.values.first.total\n if @global_count.keys.first.length == 1\n size = 0\n answer = 0\n @global_count.keys.each do |key|\n if key.length == 1\n size +=1\n answer += @global_count[key].total\n end\n break if size == 2\n end\n end\n puts answer || 0\n\nelse\n puts 0\nend"}, {"source_code": "require 'set'\n\ndef init_both(char1,char2)\n total = 0\n words = []\n [char1,char2].each do |char|\n if @global_count[char]\n total += @global_count[char].total\n words = @global_count[char].words\n end\n end\n Pair.new(total, words)\nend\n\nn = gets.to_i\nPair = Struct.new(:total, :words)\n@global_count = {}\nglobal_words = {}\n\nwhile n > 0 do\n n -= 1\n word = gets.chomp\n\n valid_word = true\n char1 = word.chars.first\n char2 = nil\n current_count = {}\n word.chars.each do |char|\n char2 ||= char if char != char1\n if char != char1 && char != char2\n valid_word = false\n break\n end\n current_count[char] ||= 0\n current_count[char] += 1\n end\n if valid_word\n if char2\n both_key = \"#{char1}#{char2}\".chars.sort.join\n @global_count[both_key] ||= init_both(char1,char2)\n @global_count[both_key].total += word.length\n @global_count[both_key].words << word\n global_words[char1] ||= Set.new [char1]\n global_words[char1].add both_key\n global_words[char2] ||= Set.new [char2]\n global_words[char2].add both_key\n else\n @global_count[char1] ||= Pair.new(0, [])\n global_words[char1] ||= Set.new [char1]\n global_words[char1].add char1\n\n global_words[char1].each do |char|\n @global_count[char].total += word.length\n @global_count[char].words << word\n end\n end\n end\nend\nputs 0\nputs @global_count\n@global_count = @global_count.sort {|a1,a2| puts 'ww'; puts a2[1].total; a2[1].total <=> a1[1].total }\nputs 0.3\n@global_count = Hash[*@global_count.flatten]\nputs @global_count\nif @global_count.values.first\n puts 3\n answer = @global_count.values.first.total\n if @global_count.keys.first.length == 1\n puts 4\n size = 0\n answer = 0\n @global_count.keys.each do |key|\n if key.length == 1\n size +=1\n answer += @global_count[key].total\n end\n break if size == 2\n end\n end\n puts answer || 0\n\nelse\n puts 0\nend"}, {"source_code": "n = gets.chomp.to_i\nhs = Hash.new(0)\nn.times do\n line = gets.chomp\n cs = line.split('').sort.uniq\n if cs.size == 1\n hs[cs[0]] += line.size\n elsif cs.size == 2\n hs[cs[0] + cs[1]] += line.size\n end\nend\n\nrtn = 0\n\nhs.each do |k, v|\n if k.size == 2\n hs[k] += hs[k[0]] + hs[k[1]]\n end\nend\n\nhs.each do |k, v|\n rtn = [rtn, v].max\nend\n\nputs rtn\n"}, {"source_code": "n = gets.chomp.to_i\nhs = Hash.new(0)\nn.times do\n cs = gets.chomp.split('').sort\n if cs.uniq.size == 1\n hs[cs[0]] += cs.size\n elsif cs.uniq.size == 2\n hs[cs[0] + cs[1]] += cs.size\n end\nend\n\nrtn = 0\n\nhs.each do |k, v|\n if k.size == 2\n hs[k] += hs[k[0]] + hs[k[1]]\n end\nend\n\nhs.each do |k, v|\n rtn = [rtn, v].max\nend\n\nputs rtn\n"}, {"source_code": "n = gets.chomp.to_i\nd = Hash.new(0)\nn.times do\n word = gets.chomp\n k = word.split(\"\").uniq\n v = word.length\n d[k] += v if k.length <= 2\nend\n\nif d.empty? then\n res = 0\nelse\n res = d.values.max\n d.keys.combination(2) do |kk|\n if kk.flatten.uniq.length > 2 then\n next\n else\n res = [res, d[kk[0]] + d[kk[1]]].max\n end\n end\n\n d.keys.combination(3) do |kk|\n if kk.flatten.uniq.length > 2 then\n next\n else\n res = [res, d[kk[0]] + d[kk[1]]+d[kk[2]]].max\n end\n end\nend\n\nputs res"}, {"source_code": "n = gets.chomp.to_i\nd = Hash.new(0)\nn.times do\n word = gets.chomp\n k = word.split(\"\").uniq\n v = word.length\n d[k] += v if k.length <= 2\nend\nres = 0\n(?a..?z).to_a.combination(2) do |kk|\n res = [res, d[[kk[0]]] + d[[kk[1]]] + d[kk]].max\nend\n\nputs res"}, {"source_code": "n = gets.chomp.to_i\nd = Hash.new(0)\nn.times do\n word = gets.chomp\n k = word.split(\"\").uniq\n v = word.length\n d[k] += v if k.length <= 2\nend\nres = d.values.max\nd.keys.combination(2) do |kk|\n if kk.flatten.uniq.length > 2 then\n next\n else\n res = [res, d[kk[0]] + d[kk[1]]].max\n end\nend\nd.keys.combination(3) do |kk|\n if kk.flatten.uniq.length > 2 then\n next\n else\n res = [res, d[kk[0]] + d[kk[1]]+d[kk[2]]].max\n end\nend\nputs res"}, {"source_code": "def get_arr(arr)\n a = arr[0]\n b = arr.split(a)[0]\n return [a,b]\nend\n\ndef exist?(arr, arr1)\n arr.each do |x|\n if x == arr1\n return true\n break\n end\n end\n false\nend\nn = gets.chomp.to_i\narr, array, len = [], [], []\n(1..n).each do |i|\n a = gets.chomp\n t = String.new(a)\n num = 0\n a.each_char do |x|\n if a.delete(x) != a\n a.delete!(x)\n num += 1\n end\n if num == 3\n break\n end\n end\n if num < 3\n arr << t\n end\nend\narr.each_with_index do |arr1, index1|\n len[index1] = arr1.length\n if !exist?(array, arr1)\n arr.each_with_index do |arr2, index2|\n if index1 == index2\n next\n else\n if get_arr(arr1) == get_arr(arr2) || arr1.squeeze.include?(arr2.squeeze)\n len[index1] += arr2.length\n end\n end\n end\n array << get_arr(arr1)\n end\nend\np len.sort.last\n"}, {"source_code": "def get_arr(arr)\n a = arr[0]\n b = arr.delete(a)[0]\n return [a,b]\nend\n\ndef exist?(arr, arr1)\n arr.each do |x|\n if x == arr1\n return true\n break\n end\n end\n false\nend\nn = gets.chomp.to_i\nx, arr, array, len = [], [], [], [0]\n(1..n).each do |i|\n a = gets.chomp\n t = String.new(a)\n num = 0\n a.each_char do |x|\n if a.delete(x) != a\n a.delete!(x)\n num += 1\n end\n if num == 3\n break\n end\n end\n if num < 3\n arr << t\n end\nend\narr.each_with_index do |arr1, index1|\n len[index1] = arr1.length\n if !exist?(array, arr1)\n arr.each_with_index do |arr2, index2|\n if index1 == index2\n next\n else\n if get_arr(arr1) == get_arr(arr2) || get_arr(arr1).reverse == get_arr(arr2) || arr1.squeeze.include?(arr2.squeeze)\n len[index1] += arr2.length\n end\n end\n end\n array << get_arr(arr1)\n end\nend\nl = len.sort.last\narr1 = \"\"\nl1 = 0\narr.each do |a|\n if a.length == l\n arr1 = a\n break\n end\nend\nif arr1.squeeze.length == 1\n arr.each do |a|\n if a.squeeze.length == 1 && a != arr1 && a.length > l1\n l1 = a.length\n end\n end\n p l+l1\nelse\n p l\nend\n"}, {"source_code": "def get_arr(arr)\n a = arr[0]\n b = arr.delete(a)[0]\n return [a,b]\nend\n\ndef exist?(arr, arr1)\n arr.each do |x|\n if x == arr1\n return true\n break\n end\n end\n false\nend\nn = gets.chomp.to_i\nx, arr, array, len = [], [], [], [0]\n(1..n).each do |i|\n a = gets.chomp\n t = String.new(a)\n num = 0\n a.each_char do |x|\n if a.delete(x) != a\n a.delete!(x)\n num += 1\n end\n if num == 3\n break\n end\n end\n if num < 3\n arr << t\n end\nend\narr.each_with_index do |arr1, index1|\n len[index1] = arr1.length\n if !exist?(array, arr1)\n arr.each_with_index do |arr2, index2|\n if index1 == index2\n next\n else\n if get_arr(arr1) == get_arr(arr2) || arr1.squeeze.include?(arr2.squeeze)\n len[index1] += arr2.length\n end\n end\n end\n array << get_arr(arr1)\n end\nend\nl = len.sort.last\nif arr.length > 1\n arr.each do |k|\n if k.squeeze.length == 1\n x << k.length\n end\n end\n x.sort!\n l1 = x.last + x[x.length-2]\nend\np l1 && l1 > l ? l1 : l\n"}, {"source_code": "def get_arr(arr)\n a = arr[0]\n b = arr.delete(a)[0]\n return [a,b]\nend\n\ndef exist?(arr, arr1)\n arr.each do |x|\n if x == arr1\n return true\n break\n end\n end\n false\nend\nn = gets.chomp.to_i\nx, arr, array, len = [], [], [], [0]\n(1..n).each do |i|\n a = gets.chomp\n t = String.new(a)\n num = 0\n a.each_char do |x|\n if a.delete(x) != a\n a.delete!(x)\n num += 1\n end\n if num == 3\n break\n end\n end\n if num < 3\n arr << t\n end\nend\narr.each_with_index do |arr1, index1|\n len[index1] = arr1.length\n if !exist?(array, arr1)\n arr.each_with_index do |arr2, index2|\n if index1 == index2\n next\n else\n if get_arr(arr1) == get_arr(arr2) || arr1.squeeze.include?(arr2.squeeze)\n len[index1] += arr2.length\n end\n end\n end\n array << get_arr(arr1)\n end\nend\nl = len.sort.last\narr.each do |k|\n if k.squeeze.length == 1\n x << k.length\n end\nend\nx.sort!\nl1 = x.last + x[x.length-2]\np l1 > l ? l1 : l\n"}, {"source_code": "def get_arr(arr)\n a = arr[0]\n b = arr.delete(a)[0]\n return [a,b]\nend\n\ndef exist?(arr, arr1)\n arr.each do |x|\n if x == arr1\n return true\n break\n end\n end\n false\nend\nn = gets.chomp.to_i\nx, arr, array, len = [], [], [], [0]\n(1..n).each do |i|\n a = gets.chomp\n t = String.new(a)\n num = 0\n a.each_char do |x|\n if a.delete(x) != a\n a.delete!(x)\n num += 1\n end\n if num == 3\n break\n end\n end\n if num < 3\n arr << t\n end\nend\narr.each_with_index do |arr1, index1|\n len[index1] = arr1.length\n if !exist?(array, arr1)\n arr.each_with_index do |arr2, index2|\n if index1 == index2\n next\n else\n if get_arr(arr1) == get_arr(arr2) || get_arr(arr1).reverse == get_arr(arr2) || arr1.squeeze.include?(arr2.squeeze)\n len[index1] += arr2.length\n end\n end\n end\n array << get_arr(arr1)\n end\nend\nl = len.sort.last\nindex = len.index(l)\nif arr[index].squeeze.length == 1\n l1 = 0\n arr.each do |arr1|\n if arr1.length > l1 && arr1 != arr[index]\n l1 = arr1.length\n end\n end\n p l+l1\nelse\n p l\nend\n"}], "src_uid": "d8a93129cb5e7f05a5d6bbeedbd9ef1a"} {"nl": {"description": "Little Vasya went to the supermarket to get some groceries. He walked about the supermarket for a long time and got a basket full of products. Now he needs to choose the cashier to pay for the products.There are n cashiers at the exit from the supermarket. At the moment the queue for the i-th cashier already has ki people. The j-th person standing in the queue to the i-th cashier has mi, j items in the basket. Vasya knows that: the cashier needs 5 seconds to scan one item; after the cashier scans each item of some customer, he needs 15 seconds to take the customer's money and give him the change. Of course, Vasya wants to select a queue so that he can leave the supermarket as soon as possible. Help him write a program that displays the minimum number of seconds after which Vasya can get to one of the cashiers.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 100) — the number of cashes in the shop. The second line contains n space-separated integers: k1, k2, ..., kn (1 ≤ ki ≤ 100), where ki is the number of people in the queue to the i-th cashier. The i-th of the next n lines contains ki space-separated integers: mi, 1, mi, 2, ..., mi, ki (1 ≤ mi, j ≤ 100) — the number of products the j-th person in the queue for the i-th cash has.", "output_spec": "Print a single integer — the minimum number of seconds Vasya needs to get to the cashier.", "sample_inputs": ["1\n1\n1", "4\n1 4 3 2\n100\n1 2 2 3\n1 9 1\n7 8"], "sample_outputs": ["20", "100"], "notes": "NoteIn the second test sample, if Vasya goes to the first queue, he gets to the cashier in 100·5 + 15 = 515 seconds. But if he chooses the second queue, he will need 1·5 + 2·5 + 2·5 + 3·5 + 4·15 = 100 seconds. He will need 1·5 + 9·5 + 1·5 + 3·15 = 100 seconds for the third one and 7·5 + 8·5 + 2·15 = 105 seconds for the fourth one. Thus, Vasya gets to the cashier quicker if he chooses the second or the third queue."}, "positive_code": [{"source_code": "#!/usr/bin/ruby\np $<.drop(2).map{|e|e.split.map{|f|f.to_i*5+15}.reduce(:+)}.min\n"}, {"source_code": "#!/usr/bin/ruby\np $<.drop(2).map{|e|e.split.map{|f|f.to_i*5+15}.reduce(:+)}.min\n"}, {"source_code": "n = gets.chomp.to_i\ncashiers = gets.chomp.split(/ /).map(&:to_i)\nitems = []\nn.times do\n items << gets.chomp.split(/ /).map(&:to_i)\nend\nans = 100000\ncashiers.each_index do |cashier|\n people = cashiers[cashier]\n time = 0\n people.times do |p|\n item = items[cashier][p]\n time += item * 5\n end\n time += people * 15\n ans = [time, ans].min\nend\nputs ans\n"}, {"source_code": "a=gets.chomp.to_i\nb=gets.chomp.split(\" \")\n0.upto(a-1) {|i| b[i]=b[i].to_i}\nar=[]\n0.upto(a-1) do |i|\ntemp=gets.chomp.split(\" \")\ntemp2=0\ntemp.each {|j| temp2+=j.to_i }\ntemp2=temp2*5+ b[i]*15\nar << temp2\nend\nans=ar.min\nputs ans.to_s"}, {"source_code": "#!/usr/bin/ruby\np $<.drop(2).map{|e|e.split.map{|f|f.to_i*5+15}.reduce(:+)}.min\n"}, {"source_code": "n = STDIN.readline.to_i\nf = STDIN.readline.split.map { |x| x.to_i }\nans = Array.new(n,0)\nn.times do |i|\n\tt = STDIN.readline.split.map { |x| x.to_i }\n\tt.each { |x| ans[i] += 5*x+15 }\nend\nprint ans.min\n"}, {"source_code": "N=gets().to_i\ndef Solve(line)\n res=0\n for i in line\n res+=(i*5)\n end\n res+=15*line.size\n res\nend\npeople_num_arr=gets().split(\" \").map{|i| i.to_i}\nlis=[]\nfor i in 0...N\n line=gets().split(\" \").map{|i| i.to_i}\n lis<(){gets.split.map(&:to_i)}\nn,k,mn=gets.to_i,readin[],0x3f3f3f3f\nn.times{|i|mn=[mn,readin[].inject(:+)*5+k[i]*15].min}\nputs mn\n"}, {"source_code": "n = Integer gets\nk = gets.split.map {|x| Integer x}\nans = Array.new(n, 0)\nfor i in 0..n - 1\n sum = 0\n arr = gets.split.map {|x|\n sum += Integer x\n Integer x\n }\n ans[i] += sum * 5 + 15 * arr.length\nend\nputs ans.min"}, {"source_code": "#!/usr/bin/ruby\np $<.drop(2).map{|e|e.split.map{|f|f.to_i*5+15}.reduce(:+)}.min\n"}, {"source_code": "#!/usr/bin/ruby\np $<.drop(2).map{|e|e.split.map{|f|f.to_i*5+15}.reduce(:+)}.min\n"}, {"source_code": "n = gets.to_i\nk = gets.split(' ').map{|i| i.to_i}\nbest = 1023456789 \nfor i in 0..n-1\n\tbest = [best, gets.split(' ').map{|i| i.to_i}.reduce{|a, b| a+b} * 5 + k[i] * 15].min\nend\nputs best\n"}, {"source_code": "#!/usr/bin/ruby\np $<.drop(2).map{|e|e.split.map{|f|f.to_i*5+15}.reduce(:+)}.min"}, {"source_code": "#!/usr/bin/ruby\np $<.drop(2).map{|e|e.split.map{|f|f.to_i*5+15}.reduce(:+)}.min\n"}, {"source_code": "#!/usr/bin/ruby\np $<.drop(2).map{|e|e.split.map{|f|f.to_i*5+15}.reduce(:+)}.min\n"}, {"source_code": "=begin\n$stdin = File.open(\"./in.txt\", \"r\")\n$stdout = File.open(\"./out.txt\", \"w\")\n=end\n\nn = gets.to_i\nk = gets.split.map { |number| number.to_i }\nm = []\nn.times do |i|\n m << gets.split.map { |number| number.to_i }\nend\n\nanswer = m.map { |queue|\n time = queue.size * 15\n queue.each { |items| time += items * 5 }\n time\n}.min\n\nprint answer"}, {"source_code": "#!/usr/bin/ruby\np $<.drop(2).map{|e|e.split.map{|f|f.to_i*5+15}.reduce(:+)}.min"}, {"source_code": "n=gets.to_i;a=Array.new(n,0)\nk=gets.chomp.split.map(&:to_i)\na=[]\nn.times do\n\tx=gets.chomp.split.map(&:to_i)\n\tsum=0\n\tx.each do |i|\n\t\tsum+=(5*i)+15\n\tend\n\ta< 1 && miss[j] < x)\n\t\tcnt[x] -= 1\n\t\tj += 1\n\t\tmiss[j - 1]\n\telse\n\t\texist[x] = 1\n\t\tx\n\tend\n}.join(\" \")"}, {"source_code": "n, l = gets.to_i + 1, gets.split.map(&:to_i)\ncnt, b, c = Array.new(n, 0), 1, 0\nl.each {|a| cnt[a] += 1}\nl.each_with_index do |a, i|\n if cnt[a] > 1\n b += 1 while cnt[b] > 0\n if a < b && cnt[a] < n\n cnt[a] = 999999\n else\n cnt[a] -= 1\n l[i] = b\n b += 1\n c += 1\n end\n end\nend\np c\nputs l.map(&:to_s).join(' ')"}, {"source_code": "n, l = gets.to_i + 1, gets.split.map(&:to_i)\nc, b = Array.new(n, 0), 1\nl.each {|a| c[a] += 1}\nl.each_with_index do |a, i|\n if c[a] > 1\n b += 1 while c[b] > 0\n if a < b && c[a] < n\n c[a] = 999999\n else\n c[a] -= 1\n l[i] = b\n b += 1\n end\n end\nend\np c.count(0) - 1\nputs l.map(&:to_s).join(' ')"}], "negative_code": [{"source_code": "n, l = gets.to_i + 1, gets.split.map(&:to_i)\ncnt, b = Array.new(n, 0), 1\nl.each {|a| cnt[a] += 1}\nl.each_with_index do |a, i|\n if cnt[a] > 1\n b += 1 while cnt[b] > 0\n if a < b && cnt[a] < n\n cnt[a] = 999999\n else\n cnt[a] -= 1\n l[i] = b\n b += 1\n end\n end\nend\nputs l.map(&:to_s).join(\" \")"}, {"source_code": "n, aa = gets.to_i+1, gets.split.map(&:to_i)\ncnt, b = Array.new(n, 0), 1\naa.each {|a| cnt[a] += 1}\naa.each_with_index do |a, i|\n if cnt[a] > 1\n b += 1 while cnt[b] > 0\n if a < b && cnt[a] < n\n cnt[a] = 999999\n else\n cnt[a] -= 1\n aa[i] = b\n b += 1\n end\n end\nend\np aa"}], "src_uid": "0560658d84cbf91c0d86eea4be92a4d9"} {"nl": {"description": "For some reason in many American cartoons anvils fall from time to time onto heroes' heads. Of course, safes, wardrobes, cruisers, planes fall sometimes too... But anvils do so most of all.Anvils come in different sizes and shapes. Quite often they get the hero stuck deep in the ground. But have you ever thought who throws anvils from the sky? From what height? We are sure that such questions have never troubled you!It turns out that throwing an anvil properly is not an easy task at all. Let's describe one of the most popular anvil throwing models.Let the height p of the potential victim vary in the range [0;a] and the direction of the wind q vary in the range [ - b;b]. p and q could be any real (floating) numbers. Then we can assume that the anvil will fit the toon's head perfectly only if the following equation has at least one real root: Determine the probability with which an aim can be successfully hit by an anvil.You can assume that the p and q coefficients are chosen equiprobably and independently in their ranges.", "input_spec": "The first line contains integer t (1 ≤ t ≤ 10000) — amount of testcases. Each of the following t lines contain two space-separated integers a and b (0 ≤ a, b ≤ 106). Pretests contain all the tests with 0 < a < 10, 0 ≤ b < 10.", "output_spec": "Print t lines — the probability of a successful anvil hit for each testcase. The absolute or relative error of the answer should not exceed 10 - 6.", "sample_inputs": ["2\n4 2\n1 2"], "sample_outputs": ["0.6250000000\n0.5312500000"], "notes": null}, "positive_code": [{"source_code": "#Infinity\n#geti,getia\n#Object # dcopy\n#\n#String # to_a\n# \"abc\".to_a #=> [\"a\",\"b\",\"c\"]\n#String # to_ia\n# \"1 2 3\".to_ia #=> [1,2,3]\n#\n#Array # rsort,rsort!\n# [1,3,2,4].rsort #=> [4,3,2,1]\n#\n#Integer # divisor\n# 24.divisor #=> [1,2,3,4,6,8,12,24]\n#\ninclude Math\nrequire \"prime\"\nrequire \"bigdecimal\"\nrequire \"bigdecimal/math\"\nrequire \"matrix\"\nrequire \"pp\"\nrequire \"benchmark\"\n\nInfinity = 1/0.0\nclass Matrix;def inspect;empty? ? \"Matrix.empty(#{row_size}, #{column_size})\":\"Matrix[\\n#{@rows.map(&:inspect).join(\"\\n\")}\\n]\";end;end\ndef pbin(obj);case obj;when Integer;puts obj.to_s(2);end;end\nclass String;def to_a;split(\"\");end;end\nclass String;def to_ia;split.map(&:to_i);end;end\ndef getia;gets.to_ia;end\ndef geti;gets.to_i;end\nclass Object;def dcopy;Marshal.load(Marshal.dump(self));end;end\nclass Array;def rsort;sort.reverse;end;def rsort!;sort!.reverse!;end;end\n\n\nt = geti\nt.times do\n a,b = gets.split.map(&:to_f)\n y = a * b\n if 4 * b > a\n y += a * a / 8.0\n else\n y += y - b * b * 2.0\n end\n if a == 0 && b == 0\n puts 1\n elsif a == 0\n puts 0.5\n elsif b == 0\n puts 1\n else\n puts y / (a * b * 2.0)\n end\nend\n\n\n\n"}, {"source_code": "n = gets.to_i\nn.times do\n a, b = gets.split.map(&:to_i)\n if b == 0\n o = 1.0\n elsif a == 0\n o = 0.5\n elsif a < 4*b\n o = (a*b + a*a*Rational(1,8))/(2*a*b)\n else\n o = (2*a*b - b*b*Rational(2,1))/(2*a*b)\n end\n p o.to_f\nend\n"}, {"source_code": "#!/usr/bin/ruby\n\n$stdin = File.open('test_69_4') if $DEBUG\n\nt = gets.to_i\nt.times do\n a, b = gets.split.map(&:to_i)\n total = b*2*a\n square = a.to_f/4 >= b ? b*b*2 : a*(b - a.to_f/8)\n puts b == 0 ? 1.0 : a == 0 ? 0.5 : (total - square).to_f / total\nend\n\n"}, {"source_code": "T = gets.to_i\n\nT.times do\n a, b = gets.split.map{|e| e.to_i}\n\n if (a == 0 && b == 0)\n puts 1.0\n elsif (a == 0)\n puts 0.5\n elsif (b == 0)\n puts 1.0\n else\n if (b > a / 4.0)\n puts 0.5 + (0.5 * a * a / 4.0) / (2 * a * b)\n else\n puts 1.0 - (0.5 * b * b * 4) / (2 * a * b)\n end\n end\n\nend\n"}], "negative_code": [{"source_code": "#Infinity\n#geti,getia\n#Object # dcopy\n#\n#String # to_a\n# \"abc\".to_a #=> [\"a\",\"b\",\"c\"]\n#String # to_ia\n# \"1 2 3\".to_ia #=> [1,2,3]\n#\n#Array # rsort,rsort!\n# [1,3,2,4].rsort #=> [4,3,2,1]\n#\n#Integer # divisor\n# 24.divisor #=> [1,2,3,4,6,8,12,24]\n#\ninclude Math\nrequire \"prime\"\nrequire \"bigdecimal\"\nrequire \"bigdecimal/math\"\nrequire \"matrix\"\nrequire \"pp\"\nrequire \"benchmark\"\n\nInfinity = 1/0.0\nclass Matrix;def inspect;empty? ? \"Matrix.empty(#{row_size}, #{column_size})\":\"Matrix[\\n#{@rows.map(&:inspect).join(\"\\n\")}\\n]\";end;end\ndef pbin(obj);case obj;when Integer;puts obj.to_s(2);end;end\nclass String;def to_a;split(\"\");end;end\nclass String;def to_ia;split.map(&:to_i);end;end\ndef getia;gets.to_ia;end\ndef geti;gets.to_i;end\nclass Object;def dcopy;Marshal.load(Marshal.dump(self));end;end\nclass Array;def rsort;sort.reverse;end;def rsort!;sort!.reverse!;end;end\n\n\nt = geti\nt.times do\n a,b = getia\n y = a * b\n if 4 * b > a\n y += a * a / 8.0\n else\n y += y - b * b * 2.0\n end\n puts y / (a * b * 2.0)\nend\n\n\n"}, {"source_code": "n = gets.to_i\nn.times do\n a, b = gets.split.map(&:to_i)\n if a == 0 || b == 0\n o = 1.0\n elsif a < 4*b\n o = (a*b + a*a*Rational(1,8))/(2*a*b)\n else\n o = (2*a*b - b*b*Rational(1,8))/(2*a*b)\n end\n p o.to_f\nend\n"}, {"source_code": "n = gets.to_i\nn.times do\n a, b = gets.split.map(&:to_i)\n if b == 0 \n o = 1.0\n elsif a == 0\n o = 0.5\n elsif a < 4*b\n o = (a*b + a*a*Rational(1,8))/(2*a*b)\n else\n o = (2*a*b - b*b*Rational(1,8))/(2*a*b)\n end\n p o.to_f\nend\n"}, {"source_code": "#!/usr/bin/ruby\n\n$stdin = File.open('test_69_4') if $DEBUG\n\nt = gets.to_i\nt.times do\n a, b = gets.split.map(&:to_i)\n total = b*2*a\n square = a.to_f/4 >= b ? b*b*2 : a*(b - a.to_f/8)\n puts b == 0 ? 1 : a == 0 ? 0.5 : (total - square) / total\nend\n\n"}, {"source_code": "T = gets.to_i\n\nT.times do\n a, b = gets.split.map{|e| e.to_i}\n\n if (b > a / 4.0)\n puts 0.5 + (0.5 * a * a / 4.0) / (2 * a * b)\n else\n puts 1.0 - (0.5 * b * b * 4) / (2 * a * b)\n end\n\nend\n"}], "src_uid": "92feda270834af751ca37bd80083aafa"} {"nl": {"description": "There are n Imperial stormtroopers on the field. The battle field is a plane with Cartesian coordinate system. Each stormtrooper is associated with his coordinates (x, y) on this plane. Han Solo has the newest duplex lazer gun to fight these stormtroopers. It is situated at the point (x0, y0). In one shot it can can destroy all the stormtroopers, situated on some line that crosses point (x0, y0).Your task is to determine what minimum number of shots Han Solo needs to defeat all the stormtroopers.The gun is the newest invention, it shoots very quickly and even after a very large number of shots the stormtroopers don't have enough time to realize what's happening and change their location. ", "input_spec": "The first line contains three integers n, x0 и y0 (1 ≤ n ≤ 1000,  - 104 ≤ x0, y0 ≤ 104) — the number of stormtroopers on the battle field and the coordinates of your gun. Next n lines contain two integers each xi, yi ( - 104 ≤ xi, yi ≤ 104) — the coordinates of the stormtroopers on the battlefield. It is guaranteed that no stormtrooper stands at the same point with the gun. Multiple stormtroopers can stand at the same point.", "output_spec": "Print a single integer — the minimum number of shots Han Solo needs to destroy all the stormtroopers. ", "sample_inputs": ["4 0 0\n1 1\n2 2\n2 0\n-1 -1", "2 1 2\n1 1\n1 0"], "sample_outputs": ["2", "1"], "notes": "NoteExplanation to the first and second samples from the statement, respectively: "}, "positive_code": [{"source_code": "n,x,y=gets.split.map(&:to_i)\na=[]\nn.times do\n x1,y1 =gets.split.map(&:to_f)\n a << (x1==x ? 'w':(y1-y)/(x1-x))\nend\np a.uniq.count\n\n"}, {"source_code": "require 'set'\n\nn, x0, y0 = gets.split.map(&:to_f)\n\nset = Set.new\nn.to_i.times do\n x, y = gets.split.map(&:to_f)\n\n if x == x0\n set << true\n else\n set << (y - y0) / (x - x0)\n end\nend\n\nputs set.length\n"}, {"source_code": "n,x,y=gets.split.map(&:to_i)\na=[]\nn.times do\n x1,y1 =gets.split.map(&:to_f)\n a << (x1==x ? 'w':(y1-y)/(x1-x))\nend\np a.uniq.count\n\n"}, {"source_code": "n,x,y=gets.split.map(&:to_i)\ns={}\nn.times do\n x1,y1=gets.split.map(&:to_f)\n s[(y1-y)/(x1-x)]=1\nend\np s[1.0/0]&&s[-1.0/0]?(s.size-1):s.size"}, {"source_code": "n,x0,y0=gets.chomp.split.map(&:to_i)\nvx,vy,ans,i,j=[],[],n,0,0\nn.times do |i|\n\tx,y=gets.chomp.split.map(&:to_i)\n\tvx<<(x-x0);vy<<(y-y0)\n\tfor j in 0...i\n\t\tif vx[i] * vy[j] == vx[j] * vy[i]\n\t\t\tans-=1\n\t\t\tbreak\n\t\tend\n\tend\nend\nputs ans"}, {"source_code": "#!/usr/bin/ruby\n# coding: utf-8\n\nn, x0, y0 = gets.split.map &:to_i\ndata = Array.new(n) { Array.new(2, 0) }\nl = []\nflag = 0\nm = 0\nn.times{|_|\n data[_] = gets.split.map &:to_i\n if x0 == data[_][0]\n flag = 1\n else\n l << (1.0 * y0 - data[_][1]) / (1.0 * x0 - data[_][0])\n end\n}\nputs (l.uniq.size + flag)\n\n"}, {"source_code": "n,x,y=gets.split.map(&:to_i)\na=[]\nn.times do\n x1,y1 =gets.split.map(&:to_f)\n a << (x1==x ? 'w':(y1-y)/(x1-x))\nend\np a.uniq.count\n\n"}, {"source_code": "n,x,y=gets.split.map(&:to_i)\na=[]\nn.times do\n x1,y1 =gets.split.map(&:to_f)\n a << (x1==x ? 'w':(y1-y)/(x1-x))\nend\np a.uniq.count\n\n"}, {"source_code": "require 'set'\ndef read_ints; STDIN.gets.split.map(&:to_i); end\n\nn, x0, y0 = read_ints\n\ninp = n.times.map do\n p = read_ints\n p[0] -= x0\n p[1] -= y0\n p\nend\n\ndot_set = inp.to_set\n\nans = 0\nloop do\n break if dot_set.empty?\n ans += 1\n p = dot_set.first\n dot_set.delete_if do |dot|\n dot[0] * p[1] == dot[1] * p[0]\n end\nend\n\nputs ans\n"}, {"source_code": "n,x,y=gets.split.map{|e| e.to_i}\nps=Hash.new(0)\nadd=0\nn.times{\n\txi,yi=gets.split.map{|e| e.to_i}\n\txi=(xi-x)\n\tyi=(yi-y)\n\tif xi==0 then\n\t\tadd=1\n\telse\n\t\tm1=1\n\t\tm1=-1 if xi<0\n\t\td1=yi.abs.gcd(xi.abs)\n\t\tps[[yi/d1*m1,xi/d1*m1]]+=1\n\t\n\tend\n}\nputs ps.size+add"}, {"source_code": "#!/usr/bin/ruby\n\nSMALL_NUMBER = 0.000000000001\ninput_line = gets\npar = input_line.split(' ').map(&:to_f)\nn = par[0]\nx0 = par[1]\ny0 = par[2]\ni = 0;\ncoords = []\nwhile(i < n) do\n input_line = gets\n par = input_line.split(' ').map(&:to_f)\n coords << [par[0],par[1]]\n i = i+1\nend\ncounter = 0\nwhile (coords.size() > 0) do\n counter = counter + 1\n x1 = coords[0][0]\n y1 = coords[0][1]\n\n if ((x0-x1).abs > SMALL_NUMBER && (y0-y1).abs > SMALL_NUMBER) \n coords.delete_if do |coord|\n true if ( ( (coord[0]-x0)/(x1-x0) - (coord[1]-y0)/(y1-y0) ).abs < SMALL_NUMBER)\n end\n elsif ((x0-x1).abs < SMALL_NUMBER)\n coords.delete_if do |coord|\n true if ( (coord[0]-x0).abs < SMALL_NUMBER)\n end\n elsif ((y0-y1).abs < SMALL_NUMBER)\n coords.delete_if do |coord|\n true if ( (coord[1]-y0).abs < SMALL_NUMBER)\n end\n end\nend\n\nputs counter\n\n"}, {"source_code": "n,x,y=gets.split.map(&:to_i)\ns={}\n(1..n).each do |i|\n x1,y1=gets.split.map(&:to_f)\n s[(y1-y)/(x1-x)]=1\nend\np s[1.0/0]&&s[-1.0/0]?(s.size-1):s.size"}, {"source_code": "n,x,y=gets.split.map(&:to_i)\ns={}\nn.times do\n x1,y1=gets.split.map(&:to_f)\n s[(y1-y)/(x1-x)]=1\nend\np s[1.0/0]&&s[-1.0/0]?(s.size-1):s.size"}, {"source_code": "# coding: utf-8\nrequire \"set\"\n\nn, x0, y0 = gets.split.map(&:to_i)\nincline_hash = Set.new\nn.times {\n x, y = gets.split.map(&:to_i)\n dx, dy = x0 - x, y0 - y\n if dx == 0\n incline_hash.add -1\n else\n incline_hash.add dy*1.0/dx\n end\n}\n\nputs incline_hash.length"}, {"source_code": "n,x,y=gets.split.map(&:to_i)\na=[]\nn.times do\n x1,y1 =gets.split.map(&:to_f)\n a << (x1==x ? 'w':(y1-y)/(x1-x))\nend\np a.uniq.count\n\n"}, {"source_code": "n,x1,y1 = gets.split(\" \").map(&:to_i)\narr = []\nn.times do\n\tarr << gets.split(\" \").map(&:to_i)\nend\neq = arr.map do |a|\n\tx2,y2 = a\n\t(x2-x1) == 0 ? 10**9 : (y2-y1)/(x2-x1).to_f\nend\nputs eq.uniq.count"}, {"source_code": "require 'set'\n\ndef gcd(a, b)\n return a if b == 0\n gcd(b, a%b)\nend\n\na = Set.new\nn,x,y = gets.split.map(&:to_i)\n1.upto(n).map do |_|\n ex,ey = gets.split.map(&:to_i)\n dx = ex - x\n dy = ey - y\n if dx < 0\n dx *= -1\n dy *= -1\n end\n if dx == 0\n a << [0,1]\n elsif dy == 0\n a << [1,0]\n else\n g = gcd(dx,dy).abs\n a << [dx/g, dy/g]\n end\nend\n\nputs a.size\n"}, {"source_code": "n, x0, y0 = gets.split.map(&:to_i)\na = n.times.map do \n x1, y1 = gets.split.map(&:to_f)\n x1 == x0 ? 'w' : (y1 - y0) / (x1 - x0)\nend\nputs a.uniq.size\n"}, {"source_code": "n,x,y=gets.split.map(&:to_i)\na=[]\nn.times do\n x1,y1 =gets.split.map(&:to_f)\n a << (x1==x ? 'w':(y1-y)/(x1-x))\nend\np a.uniq.count\n"}, {"source_code": "n, x, y = gets.split.map(&:to_i)\na = []\nn.times do\n x1, y1 = gets.split.map(&:to_f)\n a << (x1 == x ? 'w' : (y1 - y) / (x1 - x))\nend\nputs a.uniq.count\n"}, {"source_code": "n, x, y = gets.split.map(&:to_i)\na = []\nn.times do\n x1, y1 = gets.split.map(&:to_f)\n c = (y1 - y) / (x1 - x)\n a << (c.infinite? ? c.abs : c.to_r)\nend\nputs a.uniq.count\n"}, {"source_code": "n,x,y=gets.split.map(&:to_i)\na=[]\nn.times do\n x1,y1 =gets.split.map(&:to_f)\n a << (x1==x ? 'w':(y1-y)/(x1-x))\nend\np a.uniq.count\n\n"}, {"source_code": "Point = Struct.new(:x, :y)\n\ndef points_on_line(arr, zero, p)\n left = p.x - zero.x\n right = p.y - zero.y\n\n arr = arr.map { |elem|\n if (left * (elem.y - zero.y)) == (right * (elem.x - zero.x))\n nil\n else\n elem\n end\n }\n\n arr.compact\nend\n\nn, x0, y0 = gets.split(' ').map(&:to_i)\n\nzero = Point.new(x0, y0)\n\narr = []\nn.times do |_|\n x, y = gets.split(' ').map(&:to_i)\n arr << Point.new(x, y)\nend\n\ncounter = 0\nuntil arr.empty?\n p = arr.pop\n arr = points_on_line(arr, zero, p)\n\n counter += 1\nend\n\np counter\n"}, {"source_code": "n,x,y=gets.split.map(&:to_i)\na=[]\nn.times do\n x1,y1 =gets.split.map(&:to_f)\n a << (x1==x ? 'w':(y1-y)/(x1-x))\nend\np a.uniq.count\n\n"}, {"source_code": "n,x,y=gets.split.map(&:to_i)\na=[]\nn.times do\n x1,y1 =gets.split.map(&:to_f)\n a << (x1==x ? 'w':(y1-y)/(x1-x))\nend\np a.uniq.count\n\n"}], "negative_code": [{"source_code": "require 'set'\n\nn, x0, y0 = gets.split.map(&:to_f)\n\nset = Set.new\nn.to_i.times do\n x, y = gets.split.map(&:to_f)\n set << (y - y0) / (x - x0)\nend\n\nputs set.length\n"}, {"source_code": "# http://codeforces.com/contest/514/problem/A\n\ndigits = gets.chomp.chars.map(&:to_i)\ninverted = digits.map.with_index do |n, idx|\n case idx\n when 0\n (9 - n) < n && n != 9 ? 9 - n : n\n else\n (9 - n) < n ? 9 - n : n\n end\nend\n\nputs inverted.join.to_i\n"}, {"source_code": "#!/usr/bin/ruby\n# coding: utf-8\n\nn, x0, y0 = gets.split.map &:to_i\ndata = Array.new(n) { Array.new(2, 0) }\nl = []\nflag = 0\nm = 0\nn.times{|_|\n data[_] = gets.split.map &:to_i\n if x0 == data[0]\n flag = 1\n else\n l << (1.0 * y0 - data[_][1]) / (1.0 * x0 - data[_][0])\n end\n}\nputs (l.uniq.size + flag)\n\n"}, {"source_code": "#!/usr/bin/ruby\n# coding: utf-8\n\nn, x0, y0 = gets.split.map &:to_i\ndata = Array.new(n) { Array.new(2, 0) }\nl = []\nflag = 0\nm = 0\nn.times{|_|\n data[_] = gets.split.map &:to_i\n if x0 = data[0]\n flag = 1\n else\n l << (1.0 * y0 - data[_][1]) / (1.0 * x0 - data[_][0]) \n end\n}\nputs (l.uniq.size + flag)\n\n"}, {"source_code": "n,x,y=gets.split.map{|e| e.to_i}\nps=Hash.new(0)\nadd=0\nn.times{\n\txi,yi=gets.split.map{|e| e.to_i}\n\txi=(xi-x)\n\tyi=(yi-y)\n\tif xi==0 then\n\t\tadd=1\n\telse\n\t\tm=1\n\t\tm=-1 if xi<0\n\t\tps[yi/(yi.abs.gcd(xi.abs))*m]+=1\n\t\n\tend\n}\nputs ps.size+add"}, {"source_code": "#!/usr/bin/ruby\n\nSMALL_NUMBER = 0.0000001\ninput_line = gets\npar = input_line.split(' ').map(&:to_i)\nn = par[0]\nx0 = par[1]\ny0 = par[2]\ni = 0;\ncoords = []\nwhile(i < n) do\n input_line = gets\n par = input_line.split(' ').map(&:to_i)\n coords << [par[0],par[1]]\n i = i+1\nend\ncounter = 0\nwhile (coords.size() > 0) do\n ## puts coords.to_s\n ## gets\n counter = counter + 1\n x1 = coords[0][0]\n y1 = coords[0][1]\n\n if ((x0-x1).abs > SMALL_NUMBER && (y0-y1).abs > SMALL_NUMBER) \n coords.delete_if do |coord|\n true if ( ( (coord[0]-x0)/(x1-x0) - (coord[1]-y0)/(y1-y0) ).abs < SMALL_NUMBER)\n end\n elsif ((x0-x1).abs < SMALL_NUMBER)\n coords.delete_if do |coord|\n true if ( (coord[0]-x0).abs < SMALL_NUMBER)\n end\n elsif ((y0-y1).abs < SMALL_NUMBER)\n coords.delete_if do |coord|\n true if ( (coord[1]-y0).abs < SMALL_NUMBER)\n end\n end\nend\n\nputs counter\n"}, {"source_code": "n,x,y=gets.split.map(&:to_i)\ns={}\n(1..n).each do |i|\n x1,y1=gets.split.map(&:to_i)\n s[1.0*(x1-x)/(y1-y)]=1\nend\np s[1.0/0]&&s[-1.0/0]?(s.size):s.size-1"}, {"source_code": "n,x,y=gets.split.map(&:to_i)\ns={}\n(1..n).each do |i|\n\tx1,y1=gets.split.map(&:to_i)\n\ts[1.0*(y1-y)/(x1-x)]=1\nend\np s.size"}, {"source_code": "n,x1,y1 = gets.split(\" \").map(&:to_i)\narr = []\nn.times do\n\tarr << gets.split(\" \").map(&:to_i)\nend\neq = arr.map do |a|\n\tx2,y2 = a\n\t(x2-x1) == 0 ? 10**9 : (y2-y1)/(x2-x1)\nend\nputs eq.uniq.count"}, {"source_code": "n, x, y = gets.split.map(&:to_i)\na = []\nn.times do\n x1, y1 = gets.split.map(&:to_i)\n dx = x1 - x\n dy = y1 - y\n gcd = dx.gcd(dy)\n a << \"#{(dy/gcd).abs}/#{(dx/gcd).abs}\"\nend\nputs a.uniq.count\n"}, {"source_code": "n, x, y = gets.split.map(&:to_i)\na = []\nn.times do\n x1, y1 = gets.split.map(&:to_f)\n a << (x1-x) / (y1-y)\nend\nputs a.uniq.count\n"}, {"source_code": "n, x, y = gets.split.map(&:to_i)\na = []\nn.times do\n x1, y1 = gets.split.map(&:to_f)\n c = (y1 - y) / (x1 - x)\n a << (c.infinite? ? c : c.to_r)\nend\nputs a.uniq.count\n"}], "src_uid": "8d2845c33645ac45d4d37f9493b0c380"} {"nl": {"description": "Ahmed and Mostafa used to compete together in many programming contests for several years. Their coach Fegla asked them to solve one challenging problem, of course Ahmed was able to solve it but Mostafa couldn't.This problem is similar to a standard problem but it has a different format and constraints.In the standard problem you are given an array of integers, and you have to find one or more consecutive elements in this array where their sum is the maximum possible sum.But in this problem you are given n small arrays, and you will create one big array from the concatenation of one or more instances of the small arrays (each small array could occur more than once). The big array will be given as an array of indexes (1-based) of the small arrays, and the concatenation should be done in the same order as in this array. Then you should apply the standard problem mentioned above on the resulting big array.For example let's suppose that the small arrays are {1, 6, -2}, {3, 3} and {-5, 1}. And the indexes in the big array are {2, 3, 1, 3}. So the actual values in the big array after formatting it as concatenation of the small arrays will be {3, 3, -5, 1, 1, 6, -2, -5, 1}. In this example the maximum sum is 9.Can you help Mostafa solve this problem?", "input_spec": "The first line contains two integers n and m, n is the number of the small arrays (1 ≤ n ≤ 50), and m is the number of indexes in the big array (1 ≤ m ≤ 250000). Then follow n lines, the i-th line starts with one integer l which is the size of the i-th array (1 ≤ l ≤ 5000), followed by l integers each one will be greater than or equal -1000 and less than or equal 1000. The last line contains m integers which are the indexes in the big array, and you should concatenate the small arrays in the same order, and each index will be greater than or equal to 1 and less than or equal to n. The small arrays are numbered from 1 to n in the same order as given in the input. Some of the given small arrays may not be used in big array. Note, that the array is very big. So if you try to build it straightforwardly, you will probably get time or/and memory limit exceeded.", "output_spec": "Print one line containing the maximum sum in the big array after formatting it as described above. You must choose at least one element for the sum, i. e. it cannot be empty. Please, do not use %lld specificator to write 64-bit integers in C++. It is preferred to use cout (also you may use %I64d).", "sample_inputs": ["3 4\n3 1 6 -2\n2 3 3\n2 -5 1\n2 3 1 3", "6 1\n4 0 8 -3 -10\n8 3 -2 -5 10 8 -9 -5 -4\n1 0\n1 -3\n3 -8 5 6\n2 9 6\n1"], "sample_outputs": ["9", "8"], "notes": null}, "positive_code": [{"source_code": "\nsmall_size, big_indexes = gets.chomp.split.map{|s| s.to_i}\nsmalls = []\nsmall_size.times do\n array = gets.chomp.split.map{|s| s.to_i}\n array.shift\n sum = r_sum = 0\n left = right = max = -1001\n tmp = 0\n array.size.times do |i|\n sum += array[i]\n r_sum += array[-i - 1]\n left = sum if sum > left\n right = r_sum if r_sum > right\n max = array[i] if array[i] > max\n tmp += array[i]\n if tmp < 0\n tmp = 0\n else\n max = tmp if tmp > max\n end\n end\n small = {\n :sum => sum,\n :left => left,\n :right => right,\n :max => max,\n }\n smalls << small\nend\nmax = -1001\ntmp = 0\ngets.chomp.split.map{|s| s.to_i}.each do |i|\n i -= 1\n max = smalls[i][:max] if smalls[i][:max] >= max\n max = tmp + smalls[i][:left] if tmp + smalls[i][:left] > max\n tmp += smalls[i][:sum]\n tmp = smalls[i][:right] if smalls[i][:right] > tmp\n tmp = 0 if tmp < 0\nend\nputs max\n"}], "negative_code": [{"source_code": "\nsmall_size, big_indexes = gets.chomp.split.map{|s| s.to_i}\nsmalls = []\nsmall_size.times do\n array = gets.chomp.split.map{|s| s.to_i}\n array.shift\n sum = r_sum = 0\n left = right = max = -1001\n tmp = 0\n array.size.times do |i|\n sum += array[i]\n r_sum += array[-i]\n left = [sum, left].max\n right = [r_sum, right].max\n max = [max, array[i]].max\n tmp += array[i]\n if tmp < 0\n tmp = 0\n else\n max = [max, tmp].max\n end\n end\n small = {\n :sum => sum,\n :left => left,\n :right => right,\n :max => max,\n }\n smalls << small\nend\nmax = -1001\ntmp = 0\ngets.chomp.split.map{|s| s.to_i}.each do |i|\n i -= 1\n max = [max, smalls[i][:max]].max\n max = [max, tmp + smalls[i][:left]].max if tmp > 0\n tmp = [0, [smalls[i][:right], tmp + smalls[i][:sum]].max].max\nend\nputs max\n"}, {"source_code": "small_size, big_indexes = gets.chomp.split.map{|s| s.to_i}\nsmalls = []\nsmall_size.times do\n array = gets.chomp.split.map{|s| s.to_i}\n array.shift\n sum = left = max = 0\n array.each do |i|\n sum += i\n max = [max, i].max\n left = [sum, left].max\n end\n r_sum = right = 0\n array.reverse.each do |i|\n r_sum += i\n right = [sum, right].max\n end\n small = {\n :sum => sum,\n :left => left,\n :right => right,\n :max => max,\n }\n smalls << small\nend\nmax = 0\ntmp = nil\ngets.chomp.split.map{|s| s.to_i}.each do |i|\n i -= 1\n unless tmp\n tmp = [smalls[i][:sum], smalls[i][:right], 0].max\n max = [max, smalls[i][:max]].max\n next\n end\n if smalls[i][:left] > 0 && tmp + smalls[i][:left] > max\n max = tmp + smalls[i][:left]\n end\n if smalls[i][:sum] >= 0\n tmp += smalls[i][:sum]\n next\n end\n if tmp + smalls[i][:sum] < 0\n max = [tmp, max].max\n tmp = nil\n else\n tmp += smalls[i][:sum]\n end\nend\nputs [max, tmp].max\n"}, {"source_code": "small_size, big_indexes = gets.chomp.split.map{|s| s.to_i}\nsmalls = []\nsmall_size.times do\n array = gets.chomp.split.map{|s| s.to_i}\n array.shift\n sum = r_sum = 0\n left = right = max = -1001\n array.each do |i|\n sum += i\n max = [max, i].max\n left = [sum, left].max\n end\n array.reverse.each do |i|\n r_sum += i\n right = [r_sum, right].max\n end\n small = {\n :sum => sum,\n :left => left,\n :right => right,\n :max => max,\n }\n smalls << small\nend\nmax = -1001\ntmp = nil\ngets.chomp.split.map{|s| s.to_i}.each do |i|\n i -= 1\n unless tmp\n tmp = [smalls[i][:sum], smalls[i][:right]].max\n max = [max, smalls[i][:max]].max\n tmp = nil if tmp < 0\n next\n end\n if smalls[i][:left] > 0 && tmp + smalls[i][:left] > max\n max = tmp + smalls[i][:left]\n end\n if smalls[i][:sum] >= 0\n tmp += smalls[i][:sum]\n next\n end\n if tmp + smalls[i][:sum] < 0\n max = [tmp, max].max\n tmp = nil\n else\n tmp += smalls[i][:sum]\n end\nend\nputs [max, tmp || -1001].max\n"}, {"source_code": "\nsmall_size, big_indexes = gets.chomp.split.map{|s| s.to_i}\nsmalls = []\nsmall_size.times do\n array = gets.chomp.split.map{|s| s.to_i}\n array.shift\n sum = r_sum = 0\n left = right = max = -1001\n tmp = nil\n array.each do |i|\n sum += i\n left = [sum, left].max\n if tmp.nil?\n tmp = i if i > 0\n max = [max, i].max\n else\n max = [max, tmp].max\n if i >= 0 || tmp + i > 0\n tmp += i\n else\n tmp = nil\n end\n end\n end\n array.reverse.each do |i|\n r_sum += i\n right = [r_sum, right].max\n end\n small = {\n :sum => sum,\n :left => left,\n :right => right,\n :max => max,\n }\n smalls << small\nend\nmax = -1001\ntmp = nil\ngets.chomp.split.map{|s| s.to_i}.each do |i|\n i -= 1\n unless tmp\n tmp = smalls[i][:right] >= 0 ? smalls[i][:right] : nil\n max = [max, smalls[i][:max]].max\n else\n if smalls[i][:left] > 0 && tmp + smalls[i][:left] > max\n max = tmp + smalls[i][:left]\n end\n if smalls[i][:sum] >= 0 || tmp + smalls[i][:sum] >= 0\n tmp += smalls[i][:sum]\n else\n max = [tmp, max].max\n tmp = nil\n end\n end\nend\nputs [max, tmp || -1001].max\n"}, {"source_code": "small_size, big_indexes = gets.chomp.split.map{|s| s.to_i}\nsmalls = []\nsmall_size.times do\n array = gets.chomp.split.map{|s| s.to_i}\n array.shift\n sum = left = right = 0\n array.each{|i| sum += i}\n array.each do |i|\n if left + i > left\n left += i\n else\n break\n end\n end\n array.reverse.each do |i|\n if right + i > right\n right += i\n else\n break\n end\n end\n small = {\n :sum => sum,\n :left => left,\n :right => right\n }\n smalls << small\nend\nmax = 0\ntmp = nil\ngets.chomp.split.map{|s| s.to_i}.each do |i|\n i -= 1\n unless tmp\n tmp = [smalls[i][:sum], smalls[i][:right], 0].max\n next\n end\n if smalls[i][:left] > 0 && tmp + smalls[i][:left] > max\n max = tmp + smalls[i][:left]\n end\n if smalls[i][:sum] >= 0\n tmp += smalls[i][:sum]\n next\n end\n if tmp + smalls[i][:sum] < 0\n max = [tmp, max].max\n tmp = nil\n else\n tmp += smalls[i][:sum]\n end\nend\nputs [max, tmp].max\n"}, {"source_code": "small_size, big_indexes = gets.chomp.split.map{|s| s.to_i}\nsmalls = []\nsmall_size.times do\n array = gets.chomp.split.map{|s| s.to_i}\n array.shift\n sum = left = max = 0\n array.each do |i|\n sum += i\n max = [max, i].max\n left = [sum, left].max\n end\n r_sum = right = 0\n array.reverse.each do |i|\n r_sum += i\n right = [r_sum, right].max\n end\n small = {\n :sum => sum,\n :left => left,\n :right => right,\n :max => max,\n }\n smalls << small\nend\nmax = 0\ntmp = nil\ngets.chomp.split.map{|s| s.to_i}.each do |i|\n i -= 1\n unless tmp\n tmp = [smalls[i][:sum], smalls[i][:right]].max\n max = [max, smalls[i][:max]].max\n tmp = nil if tmp <= 0\n next\n end\n if smalls[i][:left] > 0 && tmp + smalls[i][:left] > max\n max = tmp + smalls[i][:left]\n end\n if smalls[i][:sum] >= 0\n tmp += smalls[i][:sum]\n next\n end\n if tmp + smalls[i][:sum] < 0\n max = [tmp, max].max\n tmp = nil\n else\n tmp += smalls[i][:sum]\n end\nend\nputs [max, tmp || 0].max\n"}, {"source_code": "\nsmall_size, big_indexes = gets.chomp.split.map{|s| s.to_i}\nsmalls = []\nsmall_size.times do\n array = gets.chomp.split.map{|s| s.to_i}\n array.shift\n sum = r_sum = 0\n left = right = max = -1001\n tmp = 0\n array.size.times do |i|\n i = array[i]\n sum += i\n r_sum += i\n left = [sum, left].max\n right = [sum, right].max\n max = [max, i].max\n tmp += i\n if tmp < 0\n tmp = 0\n else\n max = [max, tmp].max\n end\n end\n array.reverse.each do |i|\n r_sum += i\n right = [r_sum, right].max\n end\n small = {\n :sum => sum,\n :left => left,\n :right => right,\n :max => max,\n }\n smalls << small\nend\nmax = -1001\ntmp = 0\ngets.chomp.split.map{|s| s.to_i}.each do |i|\n i -= 1\n max = [max, smalls[i][:max]].max\n max = [max, tmp + smalls[i][:left]].max if tmp > 0\n tmp = [0, [smalls[i][:right], tmp + smalls[i][:sum]].max].max\nend\nputs max\n"}, {"source_code": "small_size, big_indexes = gets.chomp.split.map{|s| s.to_i}\nsmalls = []\nsmall_size.times do\n array = gets.chomp.split.map{|s| s.to_i}\n array.shift\n sum = left = right = 0\n array.each do |i|\n sum += i\n left += i if left + i > left\n end\n array.reverse.each do |i|\n right += i if right + i > right\n end\n small = {\n :sum => sum,\n :left => left,\n :right => right\n }\n smalls << small\nend\nmax = 0\ntmp = nil\ngets.chomp.split.map{|s| s.to_i}.each do |i|\n i -= 1\n unless tmp\n tmp = [smalls[i][:sum], smalls[i][:right], 0].max\n next\n end\n if smalls[i][:sum] >= 0\n tmp += smalls[i][:sum]\n next\n end\n if smalls[i][:left] > 0 && tmp + smalls[i][:left] > max\n max = tmp + smalls[i][:left]\n end\n if tmp + smalls[i][:sum] < 0\n tmp = nil\n else\n tmp += smalls[i][:sum]\n end\nend\nputs max\n"}, {"source_code": "\nsmall_size, big_indexes = gets.chomp.split.map{|s| s.to_i}\nsmalls = []\nsmall_size.times do\n array = gets.chomp.split.map{|s| s.to_i}\n array.shift\n sum = r_sum = 0\n left = right = max = -1001\n tmp = 0\n array.size.times do |i|\n sum += array[i]\n r_sum += array[-i - 1]\n left = [sum, left].max\n right = [r_sum, right].max\n max = [max, array[i]].max\n tmp += array[i]\n if tmp < 0\n tmp = 0\n else\n max = [max, tmp].max\n end\n end\n small = {\n :sum => sum,\n :left => left,\n :right => right,\n :max => max,\n }\n smalls << small\nend\nmax = -1001\ntmp = 0\ngets.chomp.split.map{|s| s.to_i}.each do |i|\n i -= 1\n p smalls[i]\n p tmp\n max = [max, smalls[i][:max]].max\n max = [max, tmp + smalls[i][:left]].max if tmp > 0\n tmp = [0, [smalls[i][:right], tmp + smalls[i][:sum]].max].max\nend\nputs max\n"}], "src_uid": "13fa378c913bb7a15612327099b59f83"} {"nl": {"description": "Valentin participates in a show called \"Shockers\". The rules are quite easy: jury selects one letter which Valentin doesn't know. He should make a small speech, but every time he pronounces a word that contains the selected letter, he receives an electric shock. He can make guesses which letter is selected, but for each incorrect guess he receives an electric shock too. The show ends when Valentin guesses the selected letter correctly.Valentin can't keep in mind everything, so he could guess the selected letter much later than it can be uniquely determined and get excessive electric shocks. Excessive electric shocks are those which Valentin got after the moment the selected letter can be uniquely determined. You should find out the number of excessive electric shocks.", "input_spec": "The first line contains a single integer n (1 ≤ n ≤ 105) — the number of actions Valentin did. The next n lines contain descriptions of his actions, each line contains description of one action. Each action can be of one of three types: Valentin pronounced some word and didn't get an electric shock. This action is described by the string \". w\" (without quotes), in which \".\" is a dot (ASCII-code 46), and w is the word that Valentin said. Valentin pronounced some word and got an electric shock. This action is described by the string \"! w\" (without quotes), in which \"!\" is an exclamation mark (ASCII-code 33), and w is the word that Valentin said. Valentin made a guess about the selected letter. This action is described by the string \"? s\" (without quotes), in which \"?\" is a question mark (ASCII-code 63), and s is the guess — a lowercase English letter. All words consist only of lowercase English letters. The total length of all words does not exceed 105. It is guaranteed that last action is a guess about the selected letter. Also, it is guaranteed that Valentin didn't make correct guesses about the selected letter before the last action. Moreover, it's guaranteed that if Valentin got an electric shock after pronouncing some word, then it contains the selected letter; and also if Valentin didn't get an electric shock after pronouncing some word, then it does not contain the selected letter.", "output_spec": "Output a single integer — the number of electric shocks that Valentin could have avoided if he had told the selected letter just after it became uniquely determined.", "sample_inputs": ["5\n! abc\n. ad\n. b\n! cd\n? c", "8\n! hello\n! codeforces\n? c\n. o\n? d\n? h\n. l\n? e", "7\n! ababahalamaha\n? a\n? b\n? a\n? b\n? a\n? h"], "sample_outputs": ["1", "2", "0"], "notes": "NoteIn the first test case after the first action it becomes clear that the selected letter is one of the following: a, b, c. After the second action we can note that the selected letter is not a. Valentin tells word \"b\" and doesn't get a shock. After that it is clear that the selected letter is c, but Valentin pronounces the word cd and gets an excessive electric shock. In the second test case after the first two electric shocks we understand that the selected letter is e or o. Valentin tries some words consisting of these letters and after the second word it's clear that the selected letter is e, but Valentin makes 3 more actions before he makes a correct hypothesis.In the third example the selected letter can be uniquely determined only when Valentin guesses it, so he didn't get excessive electric shocks."}, "positive_code": [{"source_code": "n = gets.to_i\n\ns = ('a'..'z').to_a\ncnt = 0\nn.times do |k|\n t, w = gets.split\n if t == '!'\n cnt += 1 if s.size == 1\n s = s & w.split('')\n elsif t == '.'\n s = s - w.split('')\n elsif t == '?'\n break if k == n - 1\n cnt += 1 if s.size == 1\n s = s - w.split('')\n end\nend\n\nputs cnt"}], "negative_code": [{"source_code": "n = gets.to_i\n\ns = ('a'..'z').to_a\ncnt = 0\nn.times do\n t, w = gets.split\n if t == '!'\n s = s & w.split('')\n cnt += 1 if s.size == 1\n elsif t == '.'\n s = s - w.split('')\n elsif t == '?'\n cnt += 1 if s.size == 1 && !w.split('').include?(s[0])\n end\nend\n\nputs cnt\n"}, {"source_code": "n = gets.to_i\n\ns = ('a'..'z').to_a\ncnt = 0\nn.times do\n t, w = gets.split\n if t == '!'\n cnt += 1 if s.size == 1\n s = s & w.split('')\n elsif t == '.'\n s = s - w.split('')\n elsif t == '?'\n cnt += 1 if s.size == 1 && !w.split('').include?(s[0])\n end\nend\n\nputs cnt\n"}, {"source_code": "n = gets.to_i\n\ns = ('a'..'z').to_a\ncnt = 0\nn.times do |k|\n t, w = gets.split\n if t == '!'\n s = s & w.split('')\n cnt += 1 if s.size == 1\n elsif t == '.'\n s = s - w.split('')\n elsif t == '?'\n if k == n - 1\n cnt += 1 if s.size != 1\n else\n cnt += 1 if s.size != 1 || !w.split('').include?(s[0])\n end\n end\nend\n\nputs cnt\n"}, {"source_code": "n = gets.to_i\n\ns = ('a'..'z').to_a\ncnt = 0\nn.times do |k|\n t, w = gets.split\n if t == '!'\n s = s & w.split('')\n cnt += 1 if s.size == 1\n elsif t == '.'\n s = s - w.split('')\n elsif t == '?'\n if k == n - 1\n cnt += 1 if s.size != 1\n else\n cnt += 1 if s.size == 1 && !w.split('').include?(s[0])\n end\n end\nend\n\nputs cnt\n"}], "src_uid": "3583a9762191ee8f8c3c2a287cb1ec1d"} {"nl": {"description": "В самолёте есть n рядов мест. Если смотреть на ряды сверху, то в каждом ряду есть 3 места слева, затем проход между рядами, затем 4 центральных места, затем ещё один проход между рядами, а затем ещё 3 места справа.Известно, что некоторые места уже заняты пассажирами. Всего есть два вида пассажиров — статусные (те, которые часто летают) и обычные. Перед вами стоит задача рассадить ещё k обычных пассажиров так, чтобы суммарное число соседей у статусных пассажиров было минимально возможным. Два пассажира считаются соседями, если они сидят в одном ряду и между ними нет других мест и прохода между рядами. Если пассажир является соседним пассажиром для двух статусных пассажиров, то его следует учитывать в сумме соседей дважды.", "input_spec": "В первой строке следуют два целых числа n и k (1 ≤ n ≤ 100, 1 ≤ k ≤ 10·n) — количество рядов мест в самолёте и количество пассажиров, которых нужно рассадить. Далее следует описание рядов мест самолёта по одному ряду в строке. Если очередной символ равен '-', то это проход между рядами. Если очередной символ равен '.', то это свободное место. Если очередной символ равен 'S', то на текущем месте будет сидеть статусный пассажир. Если очередной символ равен 'P', то на текущем месте будет сидеть обычный пассажир. Гарантируется, что количество свободных мест не меньше k. Гарантируется, что все ряды удовлетворяют описанному в условии формату.", "output_spec": "В первую строку выведите минимальное суммарное число соседей у статусных пассажиров. Далее выведите план рассадки пассажиров, который минимизирует суммарное количество соседей у статусных пассажиров, в том же формате, что и во входных данных. Если в свободное место нужно посадить одного из k пассажиров, выведите строчную букву 'x' вместо символа '.'. ", "sample_inputs": ["1 2\nSP.-SS.S-S.S", "4 9\nPP.-PPPS-S.S\nPSP-PPSP-.S.\n.S.-S..P-SS.\nP.S-P.PP-PSP"], "sample_outputs": ["5\nSPx-SSxS-S.S", "15\nPPx-PPPS-S.S\nPSP-PPSP-xSx\nxSx-SxxP-SSx\nP.S-PxPP-PSP"], "notes": "ПримечаниеВ первом примере нужно посадить ещё двух обычных пассажиров. Для минимизации соседей у статусных пассажиров, нужно посадить первого из них на третье слева место, а второго на любое из оставшихся двух мест, так как независимо от выбора места он станет соседом двух статусных пассажиров. Изначально, у статусного пассажира, который сидит на самом левом месте уже есть сосед. Также на четвёртом и пятом местах слева сидят статусные пассажиры, являющиеся соседями друг для друга (что добавляет к сумме 2).Таким образом, после посадки ещё двух обычных пассажиров, итоговое суммарное количество соседей у статусных пассажиров станет равно пяти."}, "positive_code": [{"source_code": "n,k=gets.split.map(&:to_i)\nrows=[]\nn.times do\n rows<[b[1],b[0][0]]\nend[0..k-1]\nsum = initial_sum + selected.inject(0){|memo, k| memo+k[1]}\nselected.map do |p|\n rows[p[0][0]][p[0][1]] = \"x\"\nend\n\nputs sum\nputs rows.map(&:join)"}, {"source_code": "def near_count(row, pos, variants)\n [-1, 1].inject(0) do |res, delta|\n target = pos+delta\n res + (target >= 0 && target < row.length && variants.include?(row[target]) ? 1 : 0)\n end\nend\n\nn, k = gets.split(\" \").map{|e| e.to_i }\n\nqueue = [[], [], []]\n\nplane = (0...n).map do |i|\n row = gets.chomp\n (0...row.length).each do |j|\n next unless row[j] == '.'\n elite_near = near_count(row, j, ['S'])\n queue[elite_near] << [i, j]\n end\n row\nend\n\nrq = queue.inject([]){|r, e| r + e}\n\nrq[0...k].each do |plan|\n plane[plan[0]][plan[1]] = 'x'\nend\n\nans = plane.inject(0) do |r, row|\n r + (0...row.length).inject(0) do |r2, i|\n r2 + (row[i] != 'S' ? 0 : near_count(row, i, ['P', 'x', 'S']))\n end\nend\n\nputs ans\nputs plane\n"}], "negative_code": [], "src_uid": "4f45c5fd31e500c1128cf6cb5e41e3d6"} {"nl": {"description": "A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb into the largest car and he likes it. Also, mother bear can climb into the middle car and she likes it. Moreover, son bear can climb into the smallest car and he likes it. It's known that the largest car is strictly larger than the middle car, and the middle car is strictly larger than the smallest car. Masha came to test these cars. She could climb into all cars, but she liked only the smallest car. It's known that a character with size a can climb into some car with size b if and only if a ≤ b, he or she likes it if and only if he can climb into this car and 2a ≥ b.You are given sizes of bears and Masha. Find out some possible integer non-negative sizes of cars.", "input_spec": "You are given four integers V1, V2, V3, Vm(1 ≤ Vi ≤ 100) — sizes of father bear, mother bear, son bear and Masha, respectively. It's guaranteed that V1 > V2 > V3.", "output_spec": "Output three integers — sizes of father bear's car, mother bear's car and son bear's car, respectively. If there are multiple possible solutions, print any. If there is no solution, print \"-1\" (without quotes).", "sample_inputs": ["50 30 10 10", "100 50 10 21"], "sample_outputs": ["50\n30\n10", "-1"], "notes": "NoteIn first test case all conditions for cars' sizes are satisfied.In second test case there is no answer, because Masha should be able to climb into smallest car (so size of smallest car in not less than 21), but son bear should like it, so maximum possible size of it is 20."}, "positive_code": [{"source_code": "q,m,s,h=gets.split.map &:to_i\nputs s>2*h||h>2*s||h>=m ? -1 : [2*q,2*m,[s,h].max]*' '"}], "negative_code": [], "src_uid": "56535017d012fdfcc13695dfd5b33084"} {"nl": {"description": "You are given a sequence of integers of length $$$n$$$ and integer number $$$k$$$. You should print any integer number $$$x$$$ in the range of $$$[1; 10^9]$$$ (i.e. $$$1 \\le x \\le 10^9$$$) such that exactly $$$k$$$ elements of given sequence are less than or equal to $$$x$$$.Note that the sequence can contain equal elements.If there is no such $$$x$$$, print \"-1\" (without quotes).", "input_spec": "The first line of the input contains integer numbers $$$n$$$ and $$$k$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$, $$$0 \\le k \\le n$$$). The second line of the input contains $$$n$$$ integer numbers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$) — the sequence itself.", "output_spec": "Print any integer number $$$x$$$ from range $$$[1; 10^9]$$$ such that exactly $$$k$$$ elements of given sequence is less or equal to $$$x$$$. If there is no such $$$x$$$, print \"-1\" (without quotes).", "sample_inputs": ["7 4\n3 7 5 1 10 3 20", "7 2\n3 7 5 1 10 3 20"], "sample_outputs": ["6", "-1"], "notes": "NoteIn the first example $$$5$$$ is also a valid answer because the elements with indices $$$[1, 3, 4, 6]$$$ is less than or equal to $$$5$$$ and obviously less than or equal to $$$6$$$.In the second example you cannot choose any number that only $$$2$$$ elements of the given sequence will be less than or equal to this number because $$$3$$$ elements of the given sequence will be also less than or equal to this number."}, "positive_code": [{"source_code": "\ndef solution\n _, k = read_ints\n\n as = read_ints.sort\n\n if k == 0\n if as[0] == 1\n puts -1\n else\n puts 1\n end\n return\n end\n\n max_incl = as[k-1]\n min_excl = as[k]\n\n if max_incl == min_excl\n puts(-1)\n else\n puts max_incl\n end\n\n\n\nend\n\ndef read_int\n gets.to_i\nend\n\ndef read_ints\n gets.split.map(&:to_i)\nend\n\ndef read_string\n gets.chomp\nend\n\nsolution unless ENV['TEST__MODE']\n\n"}, {"source_code": "n, k=gets.split.map &:to_i\na=gets.split.map(&:to_i).sort\nif k==0\n puts (a[0]==1 ? -1 : 1)\nelse\n puts (k 0\n acc = 0\n ans = -1\n h.keys.sort.each { |i|\n acc += h[i]\n if acc < k\n next\n elsif acc == k\n ans = i\n break\n else\n ans = -1\n break\n end\n }\n \n puts ans\nelse\n minimum = h.keys.min\n if minimum > 1\n puts minimum - 1\n else\n puts \"-1\"\n end\nend"}], "negative_code": [{"source_code": "n, k=gets.split.map &:to_i\na=gets.split.map(&:to_i).sort\nputs (k 0\n ans = ar[k-1]\n if h[ans] > 1\n puts \"-1\"\n else\n puts ans\n end\nelse\n if ar.first > 1\n puts ar.first - 1\n else\n puts \"-1\"\n end\nend"}, {"source_code": "# coding: utf-8\n\nn, k = gets.split.map(&:to_i)\nar = [-1] + gets.split.map(&:to_i)\nar.sort!\n#p ar\nif (k < n && ar[k] == ar[k+1]) || (k > 0 && ar[k-1] == ar[k])\n puts ar[0]\nelse\n if k == 0\n if ar[1] > 1\n puts ar[1] - 1\n else\n puts ar[0]\n end\n else\n puts ar[k]\n end\nend"}, {"source_code": "# coding: utf-8\n\nn, k = gets.split.map(&:to_i)\nar = [-1] + gets.split.map(&:to_i)\nar.sort!\n#p ar\nif k == 0 || n == 0 || (k < n && ar[k] == ar[k+1]) || (k > 0 && ar[k-1] == ar[k])\n puts ar[0]\nelse\n puts ar[k]\nend"}], "src_uid": "55297e2a65144323af4d6abd6a6ef050"} {"nl": {"description": "Anton's favourite geometric figures are regular polyhedrons. Note that there are five kinds of regular polyhedrons: Tetrahedron. Tetrahedron has 4 triangular faces. Cube. Cube has 6 square faces. Octahedron. Octahedron has 8 triangular faces. Dodecahedron. Dodecahedron has 12 pentagonal faces. Icosahedron. Icosahedron has 20 triangular faces. All five kinds of polyhedrons are shown on the picture below: Anton has a collection of n polyhedrons. One day he decided to know, how many faces his polyhedrons have in total. Help Anton and find this number!", "input_spec": "The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of polyhedrons in Anton's collection. Each of the following n lines of the input contains a string si — the name of the i-th polyhedron in Anton's collection. The string can look like this: \"Tetrahedron\" (without quotes), if the i-th polyhedron in Anton's collection is a tetrahedron. \"Cube\" (without quotes), if the i-th polyhedron in Anton's collection is a cube. \"Octahedron\" (without quotes), if the i-th polyhedron in Anton's collection is an octahedron. \"Dodecahedron\" (without quotes), if the i-th polyhedron in Anton's collection is a dodecahedron. \"Icosahedron\" (without quotes), if the i-th polyhedron in Anton's collection is an icosahedron. ", "output_spec": "Output one number — the total number of faces in all the polyhedrons in Anton's collection.", "sample_inputs": ["4\nIcosahedron\nCube\nTetrahedron\nDodecahedron", "3\nDodecahedron\nOctahedron\nOctahedron"], "sample_outputs": ["42", "28"], "notes": "NoteIn the first sample Anton has one icosahedron, one cube, one tetrahedron and one dodecahedron. Icosahedron has 20 faces, cube has 6 faces, tetrahedron has 4 faces and dodecahedron has 12 faces. In total, they have 20 + 6 + 4 + 12 = 42 faces."}, "positive_code": [{"source_code": "phs = {\"Tetrahedron\" => 4,\"Cube\" => 6,\"Octahedron\" => 8,\"Dodecahedron\" => 12,\"Icosahedron\" => 20}\nn = gets.to_i\nans = 0\n\nn.times do\n s = gets.chomp\n phs.each do |key,value|\n ans += value if key == s\n end\nend\n\nputs ans\n"}, {"source_code": "phs = {'Tetrahedron' => 4,'Cube' => 6,'Octahedron' => 8,'Dodecahedron' => 12,'Icosahedron' => 20}\nn = gets.to_i\nans = 0\n\nn.times do\n s = gets.chomp\n ans += phs[s]\nend\n\nputs ans\n"}, {"source_code": "#!/usr/bin/ruby\nT={'Tetrahedron'=>4,'Cube'=>6,'Octahedron'=>8,'Dodecahedron'=>12,'Icosahedron'=>20}\ns=0;gets.to_i.times{s+=T[gets.chomp]};p s"}, {"source_code": "n = gets.to_i\ndict = {\n\t\"Icosahedron\" => 20,\n\t\"Cube\" => 6,\n\t\"Tetrahedron\" => 4,\n\t\"Dodecahedron\" => 12,\n\t\"Octahedron\" => 8\n}\n\ncount = 0\nwhile n != 0\n\tfigure = gets.chomp.to_s\n\tcount += dict[figure]\n\tn -= 1\nend\nputs count\n\n"}, {"source_code": "# https://codeforces.com/problemset/problem/785/A\n\ncount = gets.to_i\n\nfaces_count = 0\ncount.times do\n name = gets.chomp\n\n faces_count += \n if name == \"Tetrahedron\"\n 4\n elsif name == \"Cube\"\n 6\n elsif name == \"Octahedron\"\n 8\n elsif name == \"Dodecahedron\"\n 12\n elsif name == \"Icosahedron\"\n 20\n end\nend\n\nputs faces_count\n"}, {"source_code": "n = gets.chomp.to_i\nans = 0\nn.times do\n s = gets.chomp\n case s\n when \"Tetrahedron\"\n ans += 4\n when \"Cube\"\n ans += 6\n when \"Octahedron\"\n ans += 8\n when \"Dodecahedron\"\n ans += 12\n when \"Icosahedron\"\n ans += 20\n end\nend\nputs ans"}, {"source_code": "#!/usr/bin/env ruby -w\n\npolyhedrons = {\"Tetrahedron\" => 4, \"Cube\" => 6, \"Octahedron\" => 8,\n \"Dodecahedron\" => 12, \"Icosahedron\" => 20}\n\nn = gets.strip.to_i\ns = 0\n\nn.times do\n s += polyhedrons[gets.strip.chomp]\nend\n\nputs s\n"}, {"source_code": "ans = 0\ngets.to_i.times do\n s = gets.chomp\n if s == 'Icosahedron'\n ans += 20\n elsif s == 'Cube'\n ans += 6\n elsif s == 'Tetrahedron'\n ans += 4\n elsif s == 'Dodecahedron'\n ans += 12\n elsif s == 'Octahedron'\n ans += 8\n end\nend\nputs ans\n"}, {"source_code": "poly = { \"Tetrahedron\" => 4, \"Cube\" => 6, \"Octahedron\" => 8, \"Dodecahedron\" => 12, \"Icosahedron\" => 20 }\nn = gets.strip.to_i\nans = 0\nn.times do\n s = gets.strip\n ans += poly[s]\nend\nputs ans\n"}, {"source_code": "h={}\nh[\"Tetrahedron\"]=4\nh[\"Cube\"]=6\nh[\"Octahedron\"]=8\nh[\"Dodecahedron\"]=12\nh[\"Icosahedron\"]=20\n\nans=0\ngets.to_i.times{\n\tans+=h[gets.chomp]\n}\n\nputs ans\n\n\n"}, {"source_code": "n = gets.chomp.to_i\no = {'Tetrahedron' => 0,'Cube'=>0,'Octahedron'=>0,'Dodecahedron'=>0,'Icosahedron'=>0}\nn.times.each do \ns = gets.chomp\n o[s] += 1\nend\n\nputs o['Tetrahedron'] * 4 + o['Cube'] * 6 + o['Octahedron'] * 8 + o['Dodecahedron'] * 12 + o['Icosahedron'] * 20\n"}, {"source_code": "def total(poly)\n\treturn 4 if poly == \"Tetrahedron\"\n\treturn 6 if poly == \"Cube\"\n\treturn 8 if poly == \"Octahedron\"\n\treturn 12 if poly == \"Dodecahedron\"\n\treturn 20 if poly == \"Icosahedron\"\nend\n\nfaces = 0\nn = gets.chomp.to_i\n\nn.times do\n\tpoly = gets.chomp\n\tfaces = faces + total(poly)\nend\n\nputs(faces)"}, {"source_code": "f = {\n Tetrahedron: 4,\n Cube: 6,\n Octahedron: 8,\n Dodecahedron: 12,\n Icosahedron: 20\n}\n\nn = gets.to_i\ns = 0\n\n(1..n).each { s += f[gets.chomp.to_sym] }\n\nputs s"}, {"source_code": "n=gets.to_i\np (1..n).map{\n s=gets[0]\n s==?T ? 4 : s==?C ? 6 : s==?O ? 8 : s==?D ? 12 : 20\n}.inject(:+)\n\n"}, {"source_code": "n=gets.chomp\n#puts n\ntotal_faces=0\nfaces = {\"Tetrahedron\" => 4, \"Cube\" => 6, \"Octahedron\" => 8, \"Dodecahedron\" => 12, \"Icosahedron\" => 20}\n#puts faces[\"Tetrahedron\"]\n#puts faces[\"Cube\"]\nfor i in 1..n.to_i\n #polyhedron=gets\n polyhedron=gets.chomp\n #print i\n #puts polyhedron\n #puts faces[\"Cube\"]\n total_faces=total_faces + faces[polyhedron].to_i\nend\nputs total_faces\n"}, {"source_code": "# coding: utf-8\n\n# 4\n# Icosahedron\n# Cube\n# Tetrahedron\n# Dodecahedron\n\nMAPPED = {\n \"Tetrahedron\" => 4,\n \"Cube\" => 6,\n \"Octahedron\" => 8,\n \"Dodecahedron\" => 12,\n \"Icosahedron\" => 20\n}\n\nn = gets.to_i\nc = 0\nn.times {\n input = gets\n input.strip!\n c += MAPPED[input]\n}\n\nputs c"}, {"source_code": "n = gets.chomp.to_i\npolyhedrons = { \"Tetrahedron\" => 4, \"Cube\" => 6, \"Octahedron\" => 8, \"Dodecahedron\" => 12, \"Icosahedron\" => 20}\nsum = 0\nfor i in (0..n-1)\n\tx = gets.chomp\n\tif x == \"Tetrahedron\"\n\t\tsum += polyhedrons[\"Tetrahedron\"]\n\telsif x == \"Cube\"\n\t\tsum += polyhedrons[\"Cube\"]\n\telsif x == \"Octahedron\"\n\t\tsum += polyhedrons[\"Octahedron\"]\n\telsif x == \"Dodecahedron\"\n\t\tsum += polyhedrons[\"Dodecahedron\"]\n\telsif x == \"Icosahedron\"\n\t\tsum += polyhedrons[\"Icosahedron\"]\n\tend\nend\nprint sum"}, {"source_code": "c = {'T'=>4,'C'=>6,'O'=>8,'D'=>12,'I'=>20}\np gets.to_i.times.map {c[gets[0]]}.inject(:+)"}, {"source_code": "n = gets.to_i\ncount = {}\ncount['Tetrahedron']||=0\ncount['Cube']||=0\ncount['Octahedron']||=0\ncount['Dodecahedron']||=0\ncount['Icosahedron']||=0\nfor i in (0...n)\n cur = gets.chomp\n count[cur]+=1\nend\nputs (count['Tetrahedron']*4 + count['Cube']*6 + count['Octahedron']*8 + count['Dodecahedron']*12 + count['Icosahedron']*20)"}, {"source_code": "n = gets.to_i\ncount = {'Tetrahedron'=>4,'Cube'=>6,'Octahedron'=>8,'Dodecahedron'=>12,'Icosahedron'=>20}\nans = 0\nfor i in (0...n) do ans+=count[gets.chomp] end\nputs ans"}, {"source_code": "n = gets.to_i\ncount = {}\ncount['Tetrahedron']||=4\ncount['Cube']||=6\ncount['Octahedron']||=8\ncount['Dodecahedron']||=12\ncount['Icosahedron']||=20\nans = 0\nfor i in (0...n)\n cur = gets.chomp\n ans+=count[cur]\nend\nputs ans"}, {"source_code": "n = gets.to_i\ncount = {'Tetrahedron'=>4,'Cube'=>6,'Octahedron'=>8,'Dodecahedron'=>12,'Icosahedron'=>20}\nans = 0\nfor i in (0...n)\n cur = gets.chomp\n ans+=count[cur]\nend\nputs ans"}, {"source_code": "$n = gets\n$n = $n.to_i\narr = []\n(0...$n).each do |i|\n\tarr << gets.chomp\nend\n\npolygon = {\"Tetrahedron\" => 4, \"Icosahedron\" => 20, \"Cube\" => 6, \"Dodecahedron\" => 12, \"Octahedron\" => 8}\n\n$k = 0\narr.each do |i|\n\t$k += polygon[i]\nend\nprint $k\n"}, {"source_code": "\nn = gets.to_i\ni = 0,sum =0\nn.times do\n str = gets.strip\n if str == \"Tetrahedron\"\n sum = sum +4\n elsif str == \"Cube\"\n sum = sum + 6\n elsif str == \"Octahedron\"\n sum= sum+ 8\n elsif str == \"Dodecahedron\"\n sum = sum + 12\n else\n sum = sum + 20\n end\n\nend\nprint(sum,\"\\n\")"}, {"source_code": "n = gets.chomp.to_i\nsum = 0\nhash = {}\nhash[\"Tetrahedron\"] = 4\nhash[\"Cube\"] = 6\nhash[\"Octahedron\"] = 8\nhash[\"Dodecahedron\"] = 12\nhash[\"Icosahedron\"] = 20\n\nn.times do \n\tsum += hash[gets.chomp]\nend\nputs sum\n"}, {"source_code": "h = {\n 'Tetrahedron' => 4,\n 'Cube' => 6,\n 'Octahedron' => 8,\n 'Dodecahedron' => 12,\n 'Icosahedron' => 20\n}\nn = gets.to_i\np n.times.map{gets.chomp}.inject(0){|s, x| s + h[x]}\n\n\n"}, {"source_code": "sides = {\"Tetrahedron\"=>4, \"Cube\"=>6, \"Octahedron\"=>8,\n\t\"Dodecahedron\"=>12, \"Icosahedron\"=>20}\nn=gets.chomp.to_i\nans=0\nfor i in 0...n\n\tans+=sides[gets.chomp]\nend\nputs ans"}, {"source_code": "h = {\n 'Tetrahedron' => 4,\n 'Cube' => 6,\n 'Octahedron' => 8,\n 'Dodecahedron' => 12,\n 'Icosahedron' => 20\n}\nn = gets.to_i\nnh = 0\nn.times do\n s = gets.strip\n nh += h[s]\nend\nputs nh\n\n"}, {"source_code": "n = gets.to_i\n\nsizes = {\n 'Tetrahedron' => 4,\n 'Cube' => 6,\n 'Octahedron' => 8,\n 'Dodecahedron' => 12,\n 'Icosahedron' => 20\n}\n\nputs 1.upto(n).inject(0) { |result, i| result + sizes[gets.chomp] }\n"}, {"source_code": "poly = {\n \"Tetrahedron\" => 4,\n \"Cube\" => 6,\n \"Octahedron\" => 8,\n \"Dodecahedron\" => 12,\n \"Icosahedron\" => 20\n}\nn = gets.strip.to_i\nans = 0;\nfor i in 1..n\n s = gets.strip\n ans += poly[s]\nend\nputs ans"}, {"source_code": "n = gets.to_i\n\na = []\nn.times do\n a << gets.strip\nend\n\nsum = 0\na.each do |x|\n if x == \"Tetrahedron\"\n sum += 4\n elsif x == \"Cube\"\n sum += 6\n elsif x == \"Octahedron\"\n sum += 8\n elsif x == \"Icosahedron\"\n sum += 20\n else\n sum += 12\n end\nend\n\nputs sum\n"}, {"source_code": "D = {'T' => 4, 'C' => 6, 'O' => 8, 'D' => 12, 'I' => 20}\np gets.to_i.times.map { D[gets[0]] }.inject(:+)"}, {"source_code": "L = [6, 12, 0, 0, 0, 0, 20, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 4].freeze\np gets.to_i.times.map { L[gets[0].ord - 67] }.inject(:+)"}, {"source_code": "D = {'T' => 4, 'C' => 6, 'O' => 8, 'D' => 12, 'I' => 20}.freeze\np (1..gets.to_i).map { D[gets[0]] }.inject(:+)\n"}, {"source_code": "D, x = {'Tetrahedron' => 4, 'Cube' => 6, 'Octahedron' => 8, 'Dodecahedron' => 12, 'Icosahedron' => 20}.freeze, 0\nfor i in 1..gets.to_i\n x += D[gets.strip]\nend\np x"}], "negative_code": [{"source_code": "n = gets.chomp.to_i\nans = 0\nn.times do\n s = gets.chomp\n case s\n when \"Icosahedron\"\n ans += 4\n when \"Cube\"\n ans += 6\n when \"Octahedron\"\n ans += 8\n when \"Dodecahedron\"\n ans += 12\n else \n ans += 20\n end\nend\nputs ans"}], "src_uid": "e6689123fefea251555e0e096f58f6d1"} {"nl": {"description": "There are $$$n$$$ people in a horizontal line, each looking either to the left or the right. Each person counts the number of people in the direction they are looking. The value of the line is the sum of each person's count.For example, in the arrangement LRRLL, where L stands for a person looking left and R stands for a person looking right, the counts for each person are $$$[0, 3, 2, 3, 4]$$$, and the value is $$$0+3+2+3+4=12$$$.You are given the initial arrangement of people in the line. For each $$$k$$$ from $$$1$$$ to $$$n$$$, determine the maximum value of the line if you can change the direction of at most $$$k$$$ people.", "input_spec": "The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \\leq t \\leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \\leq n \\leq 2\\cdot10^5$$$) — the length of the line. The following line contains a string consisting of $$$n$$$ characters, each of which is either L or R, representing a person facing left or right, respectively — the description of the line. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\\cdot10^5$$$. Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).", "output_spec": "For each test case, output $$$n$$$ space-separated non-negative integers — the maximum value of the line if you can change the direction of at most $$$k$$$ people for each $$$k$$$ from $$$1$$$ to $$$n$$$, inclusive.", "sample_inputs": ["6\n\n3\n\nLLR\n\n5\n\nLRRLL\n\n1\n\nL\n\n12\n\nLRRRLLLRLLRL\n\n10\n\nLLLLLRRRRR\n\n9\n\nLRLRLRLRL"], "sample_outputs": ["3 5 5 \n16 16 16 16 16 \n0 \n86 95 98 101 102 102 102 102 102 102 102 102 \n29 38 45 52 57 62 65 68 69 70 \n44 50 54 56 56 56 56 56 56"], "notes": "NoteIn the first test case: $$$k=1$$$: change the direction of $$$1$$$ person to make the line RLR. The total value is $$$2+1+0=3$$$. $$$k=2$$$: change the direction of $$$2$$$ people to make the line RLL. The total value is $$$2+1+2=5$$$. $$$k=3$$$: change the direction of $$$2$$$ people to make the line RLL. The total value is $$$2+1+2=5$$$. Note that you have to change the direction of at most $$$k$$$ people. In the second test case, it is optimal to only change the direction of the first person for all $$$k$$$ from $$$1$$$ to $$$5$$$ (that is, make the line RRRLL)."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n s = gets.chomp\r\n dxs = []\r\n sum = s.chars.zip(0..).map do |c, i|\r\n x = c == \"L\" ? i : n - i - 1\r\n dxs << [i, n - i - 1].max - x\r\n x\r\n end.sum\r\n answer = []\r\n dxs.sort.reverse.each do |dx|\r\n sum += dx\r\n answer << sum\r\n end\r\n puts answer.join(\" \")\r\nend\r\n"}], "negative_code": [], "src_uid": "f0402399cbaf8997993ac2ee59a60696"} {"nl": {"description": "Mahmoud wants to send a message to his friend Ehab. Their language consists of n words numbered from 1 to n. Some words have the same meaning so there are k groups of words such that all the words in some group have the same meaning.Mahmoud knows that the i-th word can be sent with cost ai. For each word in his message, Mahmoud can either replace it with another word of the same meaning or leave it as it is. Can you help Mahmoud determine the minimum cost of sending the message?The cost of sending the message is the sum of the costs of sending every word in it.", "input_spec": "The first line of input contains integers n, k and m (1 ≤ k ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of words in their language, the number of groups of words, and the number of words in Mahmoud's message respectively. The second line contains n strings consisting of lowercase English letters of length not exceeding 20 which represent the words. It's guaranteed that the words are distinct. The third line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) where ai is the cost of sending the i-th word. The next k lines describe the groups of words of same meaning. The next k lines each start with an integer x (1 ≤ x ≤ n) which means that there are x words in this group, followed by x integers which represent the indices of words in this group. It's guaranteed that each word appears in exactly one group. The next line contains m space-separated words which represent Mahmoud's message. Each of these words appears in the list of language's words.", "output_spec": "The only line should contain the minimum cost to send the message after replacing some words (maybe none) with some words of the same meaning.", "sample_inputs": ["5 4 4\ni loser am the second\n100 1 1 5 10\n1 1\n1 3\n2 2 5\n1 4\ni am the second", "5 4 4\ni loser am the second\n100 20 1 5 10\n1 1\n1 3\n2 2 5\n1 4\ni am the second"], "sample_outputs": ["107", "116"], "notes": "NoteIn the first sample, Mahmoud should replace the word \"second\" with the word \"loser\" because it has less cost so the cost will be 100+1+5+1=107.In the second sample, Mahmoud shouldn't do any replacement so the cost will be 100+1+5+10=116."}, "positive_code": [{"source_code": "def min(*values)\n values.min\nend\ninput_str = gets.chomp\ninput_array = input_str.split(\" \")\nn = Integer(input_array[0])\nk = Integer(input_array[1])\nm = Integer(input_array[2])\ninput_str = gets.chomp\nwords = input_str.split(\" \")\ninput_str = gets.chomp\nscore = input_str.split(\" \")\nfor i in 0...n do\n\tscore[i] = Integer(score[i])\nend\nmin_score = Array.new(k)\ngroup = Hash.new\nfor i in 0...k do\n\tinput_str = gets.chomp\n\tinput_array = input_str.split(\" \")\n\tinput_array.shift\n\tinput_array.each{ |input|\n\t\ttemp = Integer(input)-1\n\t\tif min_score[i] == nil\n\t\t\tmin_score[i] = score[temp]\n\t\telse\n\t\t\tmin_score[i] = min(min_score[i],score[temp])\n\t\tend\n\t\tgroup[words[temp]] = i\n\t}\nend\ninput_str = gets.chomp\ninput_array = input_str.split(\" \")\nresult = 0\ninput_array.each{ |input|\n\tresult+=min_score[group[input]]\n}\nputs result\n\t"}, {"source_code": "def min(*values)\n values.min\nend\ninput_str = gets.chomp\ninput_array = input_str.split(\" \")\nn = Integer(input_array[0])\nk = Integer(input_array[1])\nm = Integer(input_array[2])\ninput_str = gets.chomp\nwords = input_str.split(\" \")\ninput_str = gets.chomp\nscore = input_str.split(\" \").map(&:to_i)\nmin_score = Array.new(k)\ngroup = Hash.new\nfor i in 0...k do\n\tinput_str = gets.chomp\n\tinput_array = input_str.split(\" \")\n\tinput_array.shift\n\tinput_array.each{ |input|\n\t\ttemp = Integer(input)-1\n\t\tif min_score[i] == nil\n\t\t\tmin_score[i] = score[temp]\n\t\telse\n\t\t\tmin_score[i] = min(min_score[i],score[temp])\n\t\tend\n\t\tgroup[words[temp]] = i\n\t}\nend\ninput_str = gets.chomp\ninput_array = input_str.split(\" \")\nresult = 0\ninput_array.each{ |input|\n\tresult+=min_score[group[input]]\n}\nputs result\n\t"}], "negative_code": [], "src_uid": "296552dc2df23b3920baef7d47d0a591"} {"nl": {"description": "You are playing a computer game, where you lead a party of $$$m$$$ soldiers. Each soldier is characterised by his agility $$$a_i$$$.The level you are trying to get through can be represented as a straight line segment from point $$$0$$$ (where you and your squad is initially located) to point $$$n + 1$$$ (where the boss is located).The level is filled with $$$k$$$ traps. Each trap is represented by three numbers $$$l_i$$$, $$$r_i$$$ and $$$d_i$$$. $$$l_i$$$ is the location of the trap, and $$$d_i$$$ is the danger level of the trap: whenever a soldier with agility lower than $$$d_i$$$ steps on a trap (that is, moves to the point $$$l_i$$$), he gets instantly killed. Fortunately, you can disarm traps: if you move to the point $$$r_i$$$, you disarm this trap, and it no longer poses any danger to your soldiers. Traps don't affect you, only your soldiers.You have $$$t$$$ seconds to complete the level — that is, to bring some soldiers from your squad to the boss. Before the level starts, you choose which soldiers will be coming with you, and which soldiers won't be. After that, you have to bring all of the chosen soldiers to the boss. To do so, you may perform the following actions: if your location is $$$x$$$, you may move to $$$x + 1$$$ or $$$x - 1$$$. This action consumes one second; if your location is $$$x$$$ and the location of your squad is $$$x$$$, you may move to $$$x + 1$$$ or to $$$x - 1$$$ with your squad in one second. You may not perform this action if it puts some soldier in danger (i. e. the point your squad is moving into contains a non-disarmed trap with $$$d_i$$$ greater than agility of some soldier from the squad). This action consumes one second; if your location is $$$x$$$ and there is a trap $$$i$$$ with $$$r_i = x$$$, you may disarm this trap. This action is done instantly (it consumes no time). Note that after each action both your coordinate and the coordinate of your squad should be integers.You have to choose the maximum number of soldiers such that they all can be brought from the point $$$0$$$ to the point $$$n + 1$$$ (where the boss waits) in no more than $$$t$$$ seconds.", "input_spec": "The first line contains four integers $$$m$$$, $$$n$$$, $$$k$$$ and $$$t$$$ ($$$1 \\le m, n, k, t \\le 2 \\cdot 10^5$$$, $$$n < t$$$) — the number of soldiers, the number of integer points between the squad and the boss, the number of traps and the maximum number of seconds you may spend to bring the squad to the boss, respectively. The second line contains $$$m$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_m$$$ ($$$1 \\le a_i \\le 2 \\cdot 10^5$$$), where $$$a_i$$$ is the agility of the $$$i$$$-th soldier. Then $$$k$$$ lines follow, containing the descriptions of traps. Each line contains three numbers $$$l_i$$$, $$$r_i$$$ and $$$d_i$$$ ($$$1 \\le l_i \\le r_i \\le n$$$, $$$1 \\le d_i \\le 2 \\cdot 10^5$$$) — the location of the trap, the location where the trap can be disarmed, and its danger level, respectively.", "output_spec": "Print one integer — the maximum number of soldiers you may choose so that you may bring them all to the boss in no more than $$$t$$$ seconds.", "sample_inputs": ["5 6 4 14\n1 2 3 4 5\n1 5 2\n1 2 5\n2 3 5\n3 5 3"], "sample_outputs": ["3"], "notes": "NoteIn the first example you may take soldiers with agility $$$3$$$, $$$4$$$ and $$$5$$$ with you. The course of action is as follows: go to $$$2$$$ without your squad; disarm the trap $$$2$$$; go to $$$3$$$ without your squad; disartm the trap $$$3$$$; go to $$$0$$$ without your squad; go to $$$7$$$ with your squad. The whole plan can be executed in $$$13$$$ seconds."}, "positive_code": [{"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\nrequire 'prime'\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\ndef big(a,b) return (a>b)? a:b end\n\ndef small(a,b) return (a mob[mob_i]\n t[trap_i]\n paint_i = t[trap_i][0]\n while paint_i <= t[trap_i][1]\n if pt[paint_i]\n paint_i = pt[paint_i]\n else\n pt[paint_i] = t[trap_i][1]\n pl[mob_i] += 1\n end\n paint_i += 1\n end\n trap_i += 1\n end\nend\ntime -= (n+1)\npl.each.with_index do |d,i|\n if(d+d > time)\n p i\n exit\n end\nend\np m\n"}], "negative_code": [{"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\nrequire 'prime'\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\ndef big(a,b) return (a>b)? a:b end\n\ndef small(a,b) return (a mob[mob_i]\n t[trap_i]\n (t[trap_i][0]..t[trap_i][1]).each do |paint_i|\n if pt[paint_i]\n break\n else\n pt[paint_i] = true\n pl[mob_i] += 1\n end\n end\n (t[trap_i][0]..t[trap_i][1]).reverse_each do |paint_i|\n if pt[paint_i]\n break\n else\n pt[paint_i] = true\n pl[mob_i] += 1\n end\n end\n trap_i += 1\n end\nend\ntime -= (n+1)\npl.each.with_index do |d,i|\n if(d+d > time)\n p i\n exit\n end\nend\n"}, {"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\nrequire 'prime'\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\ndef big(a,b) return (a>b)? a:b end\n\ndef small(a,b) return (a mob[mob_i]\n t[trap_i]\n (t[trap_i][0]..t[trap_i][1]).each do |paint_i|\n if pt[paint_i]\n break\n else\n pt[paint_i] = true\n pl[mob_i] += 1\n end\n end\n (t[trap_i][0]..t[trap_i][1]).reverse_each do |paint_i|\n if pt[paint_i]\n break\n else\n pt[paint_i] = true\n pl[mob_i] += 1\n end\n end\n trap_i += 1\n end\nend\ntime -= (n+1)\npl.each.with_index do |d,i|\n if(d+d > time)\n p i\n exit\n end\nend\np m\n"}, {"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\nrequire 'prime'\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\ndef big(a,b) return (a>b)? a:b end\n\ndef small(a,b) return (a mob[mob_i]\n t[trap_i]\n (t[trap_i][0]..t[trap_i][1]).each do |paint_i|\n if pt[paint_i]\n break\n else\n pt[paint_i] = true\n pl[mob_i] += 1\n end\n end\n (t[trap_i][0]..t[trap_i][1]).reverse_each do |paint_i|\n if pt[paint_i]\n break\n else\n pt[paint_i] = true\n pl[mob_i] += 1\n end\n end\n trap_i += 1\n end\nend\ntime -= (n+1)\npl.each.with_index do |d,i|\n if(d+d > time)\n p i\n exit\n end\nend\np m\n"}, {"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\nrequire 'prime'\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\ndef big(a,b) return (a>b)? a:b end\n\ndef small(a,b) return (a mob[mob_i]\n t[trap_i]\n (t[trap_i][0]..t[trap_i][1]).each do |paint_i|\n if pt[paint_i]\n break\n else\n pt[paint_i] = true\n pl[mob_i] += 1\n end\n end\n (t[trap_i][0]..t[trap_i][1]).reverse_each do |paint_i|\n if pt[paint_i]\n break\n else\n pt[paint_i] = true\n pl[mob_i] += 1\n end\n end\n trap_i += 1\n end\nend\np time -= (n+1)\npl.each.with_index do |d,i|\n if(d+d > time)\n p i\n exit\n end\nend\np n\n"}], "src_uid": "14d158dd02d65096946445e14a5f210d"} {"nl": {"description": "Let's call some square matrix with integer values in its cells palindromic if it doesn't change after the order of rows is reversed and it doesn't change after the order of columns is reversed.For example, the following matrices are palindromic: The following matrices are not palindromic because they change after the order of rows is reversed: The following matrices are not palindromic because they change after the order of columns is reversed: You are given $$$n^2$$$ integers. Put them into a matrix of $$$n$$$ rows and $$$n$$$ columns so that each number is used exactly once, each cell contains exactly one number and the resulting matrix is palindromic. If there are multiple answers, print any. If there is no solution, print \"NO\".", "input_spec": "The first line contains one integer $$$n$$$ ($$$1 \\le n \\le 20$$$). The second line contains $$$n^2$$$ integers $$$a_1, a_2, \\dots, a_{n^2}$$$ ($$$1 \\le a_i \\le 1000$$$) — the numbers to put into a matrix of $$$n$$$ rows and $$$n$$$ columns.", "output_spec": "If it is possible to put all of the $$$n^2$$$ numbers into a matrix of $$$n$$$ rows and $$$n$$$ columns so that each number is used exactly once, each cell contains exactly one number and the resulting matrix is palindromic, then print \"YES\". Then print $$$n$$$ lines with $$$n$$$ space-separated numbers — the resulting matrix. If it's impossible to construct any matrix, then print \"NO\". You can print each letter in any case (upper or lower). For example, \"YeS\", \"no\" and \"yES\" are all acceptable.", "sample_inputs": ["4\n1 8 8 1 2 2 2 2 2 2 2 2 1 8 8 1", "3\n1 1 1 1 1 3 3 3 3", "4\n1 2 1 9 8 4 3 8 8 3 4 8 9 2 1 1", "1\n10"], "sample_outputs": ["YES\n1 2 2 1\n8 2 2 8\n8 2 2 8\n1 2 2 1", "YES\n1 3 1\n3 1 3\n1 3 1", "NO", "YES\n10"], "notes": "NoteNote that there exist multiple answers for the first two examples."}, "positive_code": [{"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\ncnt = a.tally.map { |k, v| [v, k] }\n\nans = Array.new(n) { [-1] * n }\n\nset = lambda do |i, j|\n\tcount =\n\t\tif n.even?\n\t\t\t4\n\t\telse\n\t\t\tf1, f2 = i == n / 2, j == n / 2\n\t\t\tif f1 && f2\n\t\t\t\t1\n\t\t\telsif !f1 && !f2\n\t\t\t\t4\n\t\t\telse\n\t\t\t\t2\n\t\t\tend\n\t\tend\n\tcnt = cnt.sort.reverse\n\tcnt[0][0] -= count\n\tputs('NO') + exit if cnt[0][0] < 0\n\tval = cnt[0][1]\n\tans[i][j] = val\n\tans[i][~j] = val\n\tans[~i][j] = val\n\tans[~i][~j] = val\nend\n\nif n.even?\n\t(0...n / 2).each { |i| (0...n / 2).each { |j| set[i, j] } }\nelse\n\t(0...n / 2).each { |i| (0...n / 2).each { |j| set[i, j] } }\n\t(0...n / 2).each do |i|\n\t\tset[n / 2, i]\n\t\tset[i, n / 2]\n\tend\n\tset[n / 2, n / 2]\nend\nputs 'YES'\nputs ans.map { _1.join(' ') }.join(\"\\n\")\n"}, {"source_code": "n = gets.to_i\ncnt = gets.split.map(&:to_i).tally.map { [_2, _1] }\nans = Array.new(n) { [-1] * n }\n\nset = lambda do |i, j|\n\tf1, f2 = n.odd? && i == n / 2, n.odd? && j == n / 2\n\tcnt.sort!.reverse!\n\tcnt[0][0] -=\n\t\tif f1 && f2\n\t\t\t1\n\t\telsif !f1 && !f2\n\t\t\t4\n\t\telse\n\t\t\t2\n\t\tend\n\tputs('NO') + exit if cnt[0][0] < 0\n\tans[i][j] = ans[i][~j] = ans[~i][j] = ans[~i][~j] = cnt[0][1]\nend\n\n(0...n / 2).each { |i| (0...n / 2).each { |j| set[i, j] } }\nif n.odd?\n\t(0...n / 2).each do |i|\n\t\tset[n / 2, i]\n\t\tset[i, n / 2]\n\tend\n\tset[n / 2, n / 2]\nend\nputs 'YES', ans.map { _1.join(' ') }.join(\"\\n\")\n"}, {"source_code": "def print_even(n, r4)\n puts \"YES\"\n h = n/2\n for i in 0..n-1\n for j in 0..n-1\n if i=n/2 && j=n/2\n x = i\n y = j%h\n y = h-y-1\n end\n\n if i>=n/2 && j>=n/2\n x = i%h\n x = h-x-1\n y = j%h\n y = h-y-1\n end\n\n print(r4[x*h+y].to_s)\n print(\" \") unless j == n-1\n end\n puts\n end\nend\n\ndef print_odd(n, r4, r2, r1)\n puts \"YES\"\n c = n/2\n h = n/2\n\n r2_1 = r2[0..n/2-1]\n r2_2 = r2[n/2..-1]\n for i in 0..n-1\n for j in 0..n-1\n if i == c || j == c\n if i == c && j == c\n print r1\n elsif i == c\n if j < c\n print r2_1[j]\n else\n y = (j-1)%h\n y = h-y-1\n print r2_1[y]\n end\n\n elsif j == c\n if i < c\n print r2_2[i]\n else\n y = (i-1)%h\n y = h-y-1\n print r2_2[y]\n end\n end\n else\n\n if ic && jc\n x = i\n y = (j-1)%h\n y = h-y-1\n end\n\n if i>c && j>c\n x = (i-1)%h\n x = h-x-1\n y = (j-1)%h\n y = h-y-1\n end\n\n print(r4[x*h+y].to_s)\n end\n print(\" \") unless j == n-1\n end\n puts\n end\nend\n\n\ndef main()\n n = STDIN.gets.to_i\n aa = STDIN.gets.strip.split.map(&:to_i)\n counts = {}\n aa.each do |a|\n counts[a] ||= 0;\n counts[a] += 1;\n end\n\n center = nil\n if n%2 == 0\n unless counts.all? { |k, v| v%4==0 }\n return puts \"NO\"\n end\n\n r4 = []\n counts.each do |k, v|\n (v/4).times do\n r4.push(k)\n end\n end\n print_even(n, r4)\n else\n # (4*n-1) + 2*(n-1) + 1\n r1 = counts.keys.select { |k| counts[k] % 2 == 1 }\n unless r1.size == 1\n puts \"NO\"\n return\n end\n r1 = r1[0]\n counts[r1] -= 1\n\n # すべて偶数\n unless counts.all? { |k, v| v%2==0 }\n return puts \"NO\"\n end\n\n r4_num = ((n-1)/2) ** 2\n r4 = []\n r2 = []\n\n keys = counts.keys.sort_by { |k| counts[k] }.reverse\n idx = 0\n while idx <= keys.length-1 do\n k = keys[idx]\n if counts[k] == 0\n idx+=1\n next\n end\n\n if r4.length < r4_num\n if counts[k]%4==0\n counts[k] -= 4\n r4.push(k)\n next\n end\n end\n\n counts[k] -= 2\n r2.push(k)\n end\n\n if r4.length < r4_num\n puts \"NO\"\n return\n end\n print_odd(n, r4, r2, r1)\n end\nend\n\nmain"}, {"source_code": "$n = Integer(gets)\n$ans = []\ndef setans(key, val)\n $ans[key[0]][key[1]] = val\n $ans[$n-key[0]-1][key[1]] = val\n $ans[key[0]][$n-key[1]-1] = val\n $ans[$n-key[0]-1][$n-key[1]-1] = val\nend\nfor i in 1..$n \n $ans.push(Array.new($n))\nend\nhsh = Hash.new()\ngets.split().each do |y| \n x = Integer(y)\n hsh[x] = 0 unless hsh[x]\n hsh[x] += 1\nend\nfours = []\nfor i in 0...$n/2\n for j in 0...$n/2\n fours.push([i, j])\n end\nend\ntwos = []\nones = []\nif $n % 2 == 1\n for i in 0...$n/2 \n twos.push([i, $n/2])\n twos.push([$n/2, i])\n end\n ones.push([$n/2, $n/2])\nend\nable = true\nbegin\n hsh.each do |num, cnt|\n if cnt%2 == 1\n setans(ones.pop(), num)\n cnt-=1\n end\n if cnt%4 == 2\n setans(twos.pop(), num)\n cnt-=2\n end\n until cnt == 0 or fours.empty?\n setans(fours.pop(), num)\n cnt-=4\n end\n until cnt == 0 or twos.empty?\n setans(twos.pop(), num)\n cnt-=2\n end\n until cnt == 0 or ones.empty?\n setans(ones.pop(), num)\n cnt-=1\n end\n raise \"jizz\" if cnt != 0\n end\n puts \"YES\"\n $ans.each do |x|\n puts x.join(\" \")\n end\nrescue\n puts \"NO\"\nend"}, {"source_code": "DBG = !true\nn = gets.to_i\na = gets.split.map{|z| z.to_i}\nh = {}\nfor x in a\n if h.has_key?(x)\n h[x] += 1\n else\n h[x] = 1\n end\nend\ns4 = {}\ns2 = {}\ns1 = {}\nh.each{ |k,v|\n if v%4 == 0\n s4[k] = v\n elsif v%2 == 0\n if n%2 == 0\n puts \"NO\"\n exit 0\n end\n s4[k] = v-2 if v>=6\n s2[k] = 2\n else\n if n%2 == 0 || s1.size > 0\n puts \"NO\"\n exit 0\n end\n if v>=4\n s4[k] = v-v%4\n v = v%4\n end\n if v>=2 # must be 3\n s2[k] = v-1\n end\n s1[k] = 1\n end\n}\nif n%2 == 1\n m = n/2\n if DBG\n print s4\n puts \"- s4\"\n end\n sum4 = s4.values.inject(:+)\n sum4 = 0 if sum4.nil?\n if sum4 < 4*m*m\n puts \"NO\"\n exit 0\n end\nend\n\nif DBG\n puts \"n #{n}\"\n print s4\n puts \"\"\n print s2\n puts \"\"\n print s1\n puts \"\"\nend\n\nt = Array.new(n)\nt.map!{Array.new(n)}\ni = 0\nj = 0\n\nm = n/2\nif n%2 == 0\n s4.each { |k,v|\n while v>0\n t[i][j] = k\n v -= 4\n s4[k] -= 4\n if j==m-1\n i += 1\n j = 0\n else\n j += 1\n end\n end\n }\n for i in 0...m\n for j in 0...m\n t[2*m-1-i][j] = t[i][j]\n t[i][2*m-1-j] = t[i][j]\n t[2*m-1-i][2*m-1-j] = t[i][j]\n end\n end\n\nelse # n%2 == 1\n s4.each { |k,v|\n while v>0 && i0\n t[i][j] = k\n v -= 2\n if j==m-1\n i = 0\n j = m\n vert = false\n elsif vert\n j += 1\n else\n i += 1\n end\n end\n }\n\n s1.each { |k,v|\n t[m][m] = k\n }\n\n if DBG\n puts \"after mm\"\n for i in 0..m\n for j in 0..m\n print \"#{t[i][j] }\"\n end\n puts \"\"\n end\n puts \"\"\n end\n\n for i in 0...m\n for j in 0...m\n t[2*m-i][2*m-j] = t[i][j]\n t[i][2*m-j] = t[i][j]\n t[2*m-i][j] = t[i][j]\n end\n t[2*m-i][m] = t[i][m]\n t[m][2*m-i] = t[m][i]\n end\nend\n\nputs \"YES\"\nfor i in 0...n\n for j in 0...n\n print \"#{t[i][j]} \"\n end\n puts \"\"\nend\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nb = Hash.new(0)\na.each do |i|\n b[i] += 1\nend\nc = d = 0\nb.values.each do |i|\n c += 1 if i.odd?\n d += 1 if i%4 > 1\nend\nif (n.odd? && (c != 1 || d > n-1)) || (n.even? && (c != 0 || d != 0))\n puts \"NO\"\n exit\nend\nans = Array.new(n).map{Array.new(n,0)}\nif n.odd?\n b.keys.each do |k|\n if b[k].odd?\n ans[n/2][n/2] = k\n b[k] -= 1\n break\n end\n end\n for i in 0..n/2-1\n b.keys.each do |k|\n if b[k]%4 == 2\n ans[i][n/2] = ans[n-i-1][n/2] = k\n b[k] -= 2\n break\n end\n end\n end\n for i in 0..n/2-1\n b.keys.each do |k|\n if b[k]%4 == 2\n ans[n/2][i] = ans[n/2][n-i-1] = k\n b[k] -= 2\n break\n end\n end\n end\n for i in 0..n/2-1\n b.keys.each do |k|\n if ans[i][n/2] == 0 && b[k] != 0\n ans[i][n/2] = ans[n-i-1][n/2] = k\n b[k] -= 2\n break\n end\n end\n end\n for i in 0..n/2-1\n b.keys.each do |k|\n if ans[n/2][i] == 0 && b[k] != 0\n ans[n/2][i] = ans[n/2][n-i-1] = k\n b[k] -= 2\n break\n end\n end\n end\nend\nfor i in 0..n/2-1\n for j in 0..n/2-1\n b.keys.each do |k|\n if b[k] != 0\n ans[i][j] = ans[i][n-1-j] = ans[n-1-i][j] = ans[n-1-i][n-1-j] = k\n b[k] -= 4\n break\n end\n end\n end\nend\nputs \"YES\"\nans.each do |arr|\n puts arr.join(\" \")\nend"}], "negative_code": [{"source_code": "def print_even(n, r4)\n puts \"YES\"\n h = n/2\n for i in 0..n-1\n for j in 0..n-1\n if i=n/2 && j=n/2\n x = i\n y = j%h\n y = h-y-1\n end\n\n if i>=n/2 && j>=n/2\n x = i%h\n x = h-x-1\n y = j%h\n y = h-y-1\n end\n\n print(r4[x*h+y].to_s)\n print(\" \") unless j == n-1\n end\n puts\n end\nend\n\ndef print_odd(n, r4, r2, r1)\n puts \"YES\"\n c = n/2\n h = n/2\n\n r2_1 = r2[0..n/2-1]\n r2_2 = r2[n/2..-1]\n for i in 0..n-1\n for j in 0..n-1\n if i == c || j == c\n if i == c && j == c\n print r1\n elsif i == c\n if j < c\n print r2_1[j]\n else\n y = j%h\n print r2_1[y]\n end\n\n elsif j == c\n if i < c\n print r2_2[i]\n else\n x = i%h\n print r2_2[x]\n end\n end\n else\n\n if ic && jc\n x = i\n y = (j-1)%h\n y = h-y-1\n end\n\n if i>c && j>c\n x = (i-1)%h\n x = h-x-1\n y = (j-1)%h\n y = h-y-1\n end\n\n print(r4[x*h+y].to_s)\n end\n print(\" \") unless j == n-1\n end\n puts\n end\nend\n\n\ndef main()\n n = STDIN.gets.to_i\n aa = STDIN.gets.strip.split.map(&:to_i)\n counts = {}\n aa.each do |a|\n counts[a] ||= 0;\n counts[a] += 1;\n end\n\n center = nil\n if n%2 == 0\n unless counts.all? { |k, v| v%4==0 }\n return puts \"NO\"\n end\n\n r4 = []\n counts.each do |k, v|\n (v/4).times do\n r4.push(k)\n end\n end\n print_even(n, r4)\n else\n # (4*n-1) + 2*(n-1) + 1\n r1 = counts.keys.select { |k| counts[k] % 2 == 1 }\n unless r1.size == 1\n puts \"NO\"\n return\n end\n r1 = r1[0]\n counts[r1] -= 1\n\n # すべて偶数\n unless counts.all? { |k, v| v%2==0 }\n return puts \"NO\"\n end\n\n r4_num = ((n-1)/2) ** 2\n r4 = []\n r2 = []\n\n keys = counts.keys.sort_by { |k| counts[k] }.reverse\n idx = 0\n while idx <= keys.length-1 do\n k = keys[idx]\n if counts[k] == 0\n idx+=1\n next\n end\n\n if r4.length < r4_num\n if counts[k]%4==0\n counts[k] -= 4\n r4.push(k)\n next\n end\n end\n\n counts[k] -= 2\n r2.push(k)\n end\n\n if r4.length < r4_num\n puts \"NO\"\n return\n end\n print_odd(n, r4, r2, r1)\n end\nend\n\nmain"}, {"source_code": "def print_even(n, r4)\n puts \"YES\"\n h = n/2\n for i in 0..n-1\n for j in 0..n-1\n if i=n/2 && j=n/2\n x = i\n y = j%h\n y = h-y-1\n end\n\n if i>=n/2 && j>=n/2\n x = i%h\n x = h-x-1\n y = j%h\n y = h-y-1\n end\n\n print(r4[x*h+y].to_s)\n print(\" \") unless j == n-1\n end\n puts\n end\nend\n\ndef print_odd(n, r4, r2, r1)\n puts \"YES\"\n c = n/2\n h = n/2\n for i in 0..n-1\n for j in 0..n-1\n if i == c || j == c\n if i == c && j == c\n print r1\n elsif i == c\n if j < c\n print r2[j]\n else\n print r2[n-j-1]\n end\n\n elsif j == c\n if i < c\n print r2[i]\n else\n print r2[n-i-1]\n end\n end\n else\n\n if ic && jc\n x = i\n y = (j-1)%h\n y = h-y-1\n end\n\n if i>c && j>c\n x = (i-1)%h\n x = h-x-1\n y = (j-1)%h\n y = h-y-1\n end\n\n print(r4[x*h+y].to_s)\n end\n print(\" \") unless j == n-1\n end\n puts\n end\nend\n\n\ndef main()\n n = STDIN.gets.to_i\n aa = STDIN.gets.strip.split.map(&:to_i)\n counts = {}\n aa.each do |a|\n counts[a] ||= 0;\n counts[a] += 1;\n end\n\n center = nil\n if n%2 == 0\n unless counts.all? { |k, v| v%4==0 }\n return puts \"NO\"\n end\n\n r4 = []\n counts.each do |k, v|\n (v/4).times do\n r4.push(k)\n end\n end\n print_even(n, r4)\n else\n # (4*n-1) + 2*(n-1) + 1\n r1 = counts.keys.select { |k| counts[k] % 4 == 1 }\n unless r1.size == 1\n puts \"NO\"\n return\n end\n r1 = r1[0]\n counts[r1] -= 1\n\n # すべて偶数\n unless counts.all? { |k, v| v%2==0 }\n return puts \"NO\"\n end\n\n r4_num = ((n-1)/2) ** 2\n r4 = []\n r2 = []\n\n keys = counts.keys.sort_by { |k| counts[k] }.reverse\n idx = 0\n while idx <= keys.length-1 do\n k = keys[idx]\n if counts[k] == 0\n idx+=1\n next\n end\n\n if r4.length < r4_num\n if counts[k]%4==0\n counts[k] -= 4\n r4.push(k)\n next\n end\n end\n\n counts[k] -= 2\n r2.push(k)\n end\n\n if r4.length < r4_num\n puts \"NO\"\n return\n end\n print_odd(n, r4, r2, r1)\n end\nend\n\nmain"}, {"source_code": "def print_even(n, r4)\n puts \"YES\"\n h = n/2\n for i in 0..n-1\n for j in 0..n-1\n if i=n/2 && j=n/2\n x = i\n y = j%h\n y = h-y-1\n end\n\n if i>=n/2 && j>=n/2\n x = i%h\n x = h-x-1\n y = j%h\n y = h-y-1\n end\n\n print(r4[x*h+y].to_s)\n print(\" \") unless j == n-1\n end\n puts\n end\nend\n\ndef print_odd(n, r4, r2, r1)\n puts \"YES\"\n c = n/2\n h = n/2\n for i in 0..n-1\n for j in 0..n-1\n if i == c || j == c\n if i == c && j == c\n print r1\n elsif i == c\n if j < c\n print r2[j]\n else\n print r2[n-j-1]\n end\n\n elsif j == c\n if i < c\n print r2[i]\n else\n print r2[n-i-1]\n end\n end\n else\n\n if ic && jc\n x = i\n y = (j-1)%h\n y = h-y-1\n end\n\n if i>c && j>c\n x = (i-1)%h\n x = h-x-1\n y = (j-1)%h\n y = h-y-1\n end\n\n print(r4[x*h+y].to_s)\n end\n print(\" \") unless j == n-1\n end\n puts\n end\nend\n\n\ndef main()\n n = STDIN.gets.to_i\n aa = STDIN.gets.strip.split.map(&:to_i)\n counts = {}\n aa.each do |a|\n counts[a] ||= 0;\n counts[a] += 1;\n end\n\n center = nil\n if n%2 == 0\n unless counts.all? { |k, v| v%4==0 }\n return puts \"NO\"\n end\n\n r4 = []\n counts.each do |k, v|\n (v/4).times do\n r4.push(k)\n end\n end\n print_even(n, r4)\n else\n # (4*n-1) + 2*(n-1) + 1\n r1 = counts.keys.select { |k| counts[k] % 2 == 1 }\n unless r1.size == 1\n puts \"NO\"\n return\n end\n r1 = r1[0]\n counts[r1] -= 1\n\n # すべて偶数\n unless counts.all? { |k, v| v%2==0 }\n return puts \"NO\"\n end\n\n r4_num = ((n-1)/2) ** 2\n r4 = []\n r2 = []\n\n keys = counts.keys.sort_by { |k| counts[k] }.reverse\n idx = 0\n while idx <= keys.length-1 do\n k = keys[idx]\n if counts[k] == 0\n idx+=1\n next\n end\n\n if r4.length < r4_num\n if counts[k]%4==0\n counts[k] -= 4\n r4.push(k)\n next\n end\n end\n\n counts[k] -= 2\n r2.push(k)\n end\n\n if r4.length < r4_num\n puts \"NO\"\n return\n end\n print_odd(n, r4, r2, r1)\n end\nend\n\nmain"}, {"source_code": "DBG = !true\nn = gets.to_i\na = gets.split.map{|z| z.to_i}\nh = {}\nfor x in a\n if h.has_key?(x)\n h[x] += 1\n else\n h[x] = 1\n end\nend\ns4 = {}\ns2 = {}\ns1 = {}\nh.each{ |k,v|\n if v%4 == 0\n s4[k] = v\n elsif v%2 == 0\n if n%2 == 0\n puts \"NO\"\n exit 0\n end\n s4[k] = v-2 if v>=6\n s2[k] = 2\n else\n if n%2 == 0 || s1.size > 0\n puts \"NO\"\n exit 0\n end\n if v>=4\n s4[k] = v-v%4\n v = v%4\n end\n if v>=2 # must be 3\n s2[k] = v-1\n end\n s1[k] = 1\n end\n}\nif n%2 == 1\n m = n/2\n print s4 if DBG\n sum4 = s4.values.inject(:+)\n sum4 = 0 if sum4.nil?\n if sum4 < m*m\n puts \"NO\"\n exit 0\n end\nend\n\nif DBG\n puts \"n #{n}\"\n print s4\n puts \"\"\n print s2\n puts \"\"\n print s1\n puts \"\"\nend\n\nt = Array.new(n)\nt.map!{Array.new(n)}\ni = 0\nj = 0\n\nm = n/2\nif n%2 == 0\n s4.each { |k,v|\n while v>0\n t[i][j] = k\n v -= 4\n s4[k] -= 4\n if j==m-1\n i += 1\n j = 0\n else\n j += 1\n end\n end\n }\n for i in 0...m\n for j in 0...m\n t[2*m-1-i][j] = t[i][j]\n t[i][2*m-1-j] = t[i][j]\n t[2*m-1-i][2*m-1-j] = t[i][j]\n end\n end\n\nelse # n%2 == 1\n s4.each { |k,v|\n while v>0 && i0\n t[i][j] = k\n v -= 2\n if j==m-1\n i = 0\n j = m\n vert = false\n elsif vert\n j += 1\n else\n i += 1\n end\n end\n }\n\n s1.each { |k,v|\n t[m][m] = k\n }\n\n if DBG\n puts \"after mm\"\n for i in 0..m\n for j in 0..m\n print \"#{t[i][j] }\"\n end\n puts \"\"\n end\n puts \"\"\n end\n\n for i in 0...m\n for j in 0...m\n t[2*m-i][2*m-j] = t[i][j]\n t[i][2*m-j] = t[i][j]\n t[2*m-i][j] = t[i][j]\n end\n t[2*m-i][m] = t[i][m]\n t[m][2*m-i] = t[m][i]\n end\nend\n\nputs \"YES\"\nfor i in 0...n\n for j in 0...n\n print \"#{t[i][j]} \"\n end\n puts \"\"\nend\n"}, {"source_code": "DBG = !true\nn = gets.to_i\na = gets.split.map{|z| z.to_i}\nh = {}\nfor x in a\n if h.has_key?(x)\n h[x] += 1\n else\n h[x] = 1\n end\nend\ns4 = {}\ns2 = {}\ns1 = {}\nh.each{ |k,v|\n if v%4 == 0\n s4[k] = v\n elsif v%2 == 0\n if n%2 == 0\n puts \"NO\"\n exit 0\n end\n s4[k] = v-2 if v>=6\n s2[k] = 2\n else\n if n%2 == 0 || s1.size > 0\n puts \"NO\"\n exit 0\n end\n if v>=4\n s4[k] = v-v%4\n v = v%4\n end\n if v>=2 # must be 3\n s2[k] = v-1\n end\n s1[k] = 1\n end\n}\n\nif DBG\n puts \"n #{n}\"\n print s4\n puts \"\"\n print s2\n puts \"\"\n print s1\n puts \"\"\nend\n\nt = Array.new(n)\nt.map!{Array.new(n)}\ni = 0\nj = 0\n\nm = n/2\nif n%2 == 0\n s4.each { |k,v|\n while v>0\n t[i][j] = k\n v -= 4\n s4[k] -= 4\n if j==m-1\n i += 1\n j = 0\n else\n j += 1\n end\n end\n }\n for i in 0...m\n for j in 0...m\n t[2*m-1-i][j] = t[i][j]\n t[i][2*m-1-j] = t[i][j]\n t[2*m-1-i][2*m-1-j] = t[i][j]\n end\n end\n\nelse # n%2 == 1\n s4.each { |k,v|\n while v>0 && i0\n t[i][j] = k\n v -= 2\n if j==m-1\n i = 0\n j = m\n vert = false\n elsif vert\n j += 1\n else\n i += 1\n end\n end\n }\n\n s1.each { |k,v|\n t[m][m] = k\n }\n\n if DBG\n puts \"after mm\"\n for i in 0..m\n for j in 0..m\n print \"#{t[i][j] }\"\n end\n puts \"\"\n end\n puts \"\"\n end\n\n for i in 0...m\n for j in 0...m\n t[2*m-i][2*m-j] = t[i][j]\n t[i][2*m-j] = t[i][j]\n t[2*m-i][j] = t[i][j]\n end\n t[2*m-i][m] = t[i][m]\n t[m][2*m-i] = t[m][i]\n end\nend\n\nputs \"YES\"\nfor i in 0...n\n for j in 0...n\n print \"#{t[i][j]} \"\n end\n puts \"\"\nend\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nb = Hash.new(0)\na.each do |i|\n b[i] += 1\nend\nc = 0\nb.values.each do |i|\n c += 1 if i%4 != 0\nend\nif (n.odd? && c != 1) || (n.even? && c != 0)\n puts \"NO\"\n exit\nend\nans = Array.new(n).map{Array.new(n,0)}\nif n.odd?\n b.keys.each do |k|\n if b[k].odd?\n ans[n/2][n/2] = k\n b[k] -= 1\n break\n end\n end\n for i in 0..n/2-1\n b.keys.each do |k|\n if b[k] != 0\n ans[i][n/2] = ans[n-i-1][n/2] = ans[n/2][i] = ans[n/2][n-i-1] = k\n b[k] -= 4\n break\n end\n end\n end\nend\nfor i in 0..n/2-1\n for j in 0..n/2-1\n b.keys.each do |k|\n if b[k] != 0\n ans[i][j] = ans[i][n-1-j] = ans[n-1-i][j] = ans[n-1-i][n-1-j] = k\n b[k] -= 4\n break\n end\n end\n end\nend\nputs \"YES\"\nans.each do |arr|\n puts arr.join(\" \")\nend"}], "src_uid": "20928dd8e512bee2d86c6611c5e76390"} {"nl": {"description": "Polycarp is a frequent user of the very popular messenger. He's chatting with his friends all the time. He has $$$n$$$ friends, numbered from $$$1$$$ to $$$n$$$.Recall that a permutation of size $$$n$$$ is an array of size $$$n$$$ such that each integer from $$$1$$$ to $$$n$$$ occurs exactly once in this array.So his recent chat list can be represented with a permutation $$$p$$$ of size $$$n$$$. $$$p_1$$$ is the most recent friend Polycarp talked to, $$$p_2$$$ is the second most recent and so on.Initially, Polycarp's recent chat list $$$p$$$ looks like $$$1, 2, \\dots, n$$$ (in other words, it is an identity permutation).After that he receives $$$m$$$ messages, the $$$j$$$-th message comes from the friend $$$a_j$$$. And that causes friend $$$a_j$$$ to move to the first position in a permutation, shifting everyone between the first position and the current position of $$$a_j$$$ by $$$1$$$. Note that if the friend $$$a_j$$$ is in the first position already then nothing happens.For example, let the recent chat list be $$$p = [4, 1, 5, 3, 2]$$$: if he gets messaged by friend $$$3$$$, then $$$p$$$ becomes $$$[3, 4, 1, 5, 2]$$$; if he gets messaged by friend $$$4$$$, then $$$p$$$ doesn't change $$$[4, 1, 5, 3, 2]$$$; if he gets messaged by friend $$$2$$$, then $$$p$$$ becomes $$$[2, 4, 1, 5, 3]$$$. For each friend consider all position he has been at in the beginning and after receiving each message. Polycarp wants to know what were the minimum and the maximum positions.", "input_spec": "The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n, m \\le 3 \\cdot 10^5$$$) — the number of Polycarp's friends and the number of received messages, respectively. The second line contains $$$m$$$ integers $$$a_1, a_2, \\dots, a_m$$$ ($$$1 \\le a_i \\le n$$$) — the descriptions of the received messages.", "output_spec": "Print $$$n$$$ pairs of integers. For each friend output the minimum and the maximum positions he has been in the beginning and after receiving each message.", "sample_inputs": ["5 4\n3 5 1 4", "4 3\n1 2 4"], "sample_outputs": ["1 3\n2 5\n1 4\n1 5\n1 5", "1 3\n1 2\n3 4\n1 4"], "notes": "NoteIn the first example, Polycarp's recent chat list looks like this: $$$[1, 2, 3, 4, 5]$$$ $$$[3, 1, 2, 4, 5]$$$ $$$[5, 3, 1, 2, 4]$$$ $$$[1, 5, 3, 2, 4]$$$ $$$[4, 1, 5, 3, 2]$$$ So, for example, the positions of the friend $$$2$$$ are $$$2, 3, 4, 4, 5$$$, respectively. Out of these $$$2$$$ is the minimum one and $$$5$$$ is the maximum one. Thus, the answer for the friend $$$2$$$ is a pair $$$(2, 5)$$$.In the second example, Polycarp's recent chat list looks like this: $$$[1, 2, 3, 4]$$$ $$$[1, 2, 3, 4]$$$ $$$[2, 1, 3, 4]$$$ $$$[4, 2, 1, 3]$$$ "}, "positive_code": [{"source_code": "class BIT\n def initialize(size)\n @arr = Array.new(size+1, 0)\n end\n\n def update(p, v)\n while p < @arr.length\n @arr[p] += v\n p += (p&-p)\n end\n end\n\n def pquery(p)\n ans = 0\n while p > 0\n ans += @arr[p]\n p -= (p&-p)\n end\n ans\n end\n\n def query(l, r)\n l <= r ? pquery(r) - pquery(l-1) : 0\n end\nend\n\nn, m = gets.chomp.split(' ').map(&:to_i)\narr = gets.chomp.split(' ').map(&:to_i)\nlastpos = Array.new(n+1, -1)\nmaxpos = (0..n).to_a\nbit_m = BIT.new(m)\nbit_n = BIT.new(n)\n\n(1..m).each do |i|\n x = arr[i-1]\n if lastpos[x] == -1\n curr = x + bit_n.query(x+1, n)\n else\n curr = 1 + bit_m.query(lastpos[x]+1, i)\n bit_m.update(lastpos[x], -1)\n end\n maxpos[x] = curr if curr > maxpos[x]\n\n lastpos[x] = i\n bit_m.update(i, 1)\n bit_n.update(x, 1) if bit_n.query(x, x) == 0\nend\n\n(1..n).each do |x|\n if lastpos[x] == -1\n curr = x + bit_n.query(x+1, n)\n else\n curr = 1 + bit_m.query(lastpos[x]+1, m)\n end\n maxpos[x] = curr if curr > maxpos[x]\nend\n\nminpos = (0..n).map {|x| lastpos[x] == -1 ? x : 1}\nanswer = minpos.zip(maxpos).map {|a, b| \"#{a} #{b}\"}\nputs answer[1..n]"}], "negative_code": [{"source_code": "class BIT\n def initialize(size)\n @arr = Array.new(size+1, 0)\n end\n\n def update(p, v)\n while p < @arr.length\n @arr[p] += v\n p += (p&-p)\n end\n end\n\n def pquery(p)\n ans = 0\n while p > 0\n ans += @arr[p]\n p -= (p&-p)\n end\n ans\n end\n\n def query(l, r)\n l <= r ? pquery(r) - pquery(l-1) : 0\n end\nend\n\nn, m = gets.chomp.split(' ').map(&:to_i)\narr = gets.chomp.split(' ').map(&:to_i)\nlastpos = Array.new(n+1, -1)\nmaxpos = (0..n).to_a\nbit_m = BIT.new(m)\nbit_n = BIT.new(n)\n\n(1..m).each do |i|\n x = arr[i-1]\n if lastpos[x] == -1\n curr = x + bit_n.query(x+1, n)\n else\n curr = 1 + bit_m.query(lastpos[x]+1, i)\n bit_m.update(lastpos[x], -1)\n end\n maxpos[x] = curr if curr > maxpos[x]\n\n lastpos[x] = i\n bit_m.update(i, 1)\n bit_n.update(x, 1) if bit_n.query(x, x) == 0\nend\n\nfinal_bit_m = (1..m).map {|x| bit_m.query(x, x) }\nputs final_bit_m.join(' ')\n\n(1..n).each do |x|\n if lastpos[x] == -1\n curr = x + bit_n.query(x+1, n)\n else\n curr = 1 + bit_m.query(lastpos[x]+1, m)\n end\n maxpos[x] = curr if curr > maxpos[x]\nend\n\nminpos = (0..n).map {|x| lastpos[x] == -1 ? x : 1}\nanswer = minpos.zip(maxpos).map {|a, b| \"#{a} #{b}\"}\nputs answer[1..n]"}, {"source_code": "class BIT\n def initialize(size)\n @arr = Array.new(size+1, 0)\n end\n\n def update(p, v)\n while p < @arr.length\n @arr[p] += v\n p += (p&-p)\n end\n end\n\n def pquery(p)\n ans = 0\n while p > 0\n ans += @arr[p]\n p -= (p&-p)\n end\n ans\n end\n\n def query(l, r)\n l <= r ? pquery(r) - pquery(l-1) : 0\n end\nend\n\nn, m = gets.chomp.split(' ').map(&:to_i)\narr = gets.chomp.split(' ').map(&:to_i)\nlastpos = Array.new(n+1, -1)\nmaxpos = (0..n).to_a\nbit_m = BIT.new(m)\nbit_n = BIT.new(n)\n\n(1..m).each do |i|\n x = arr[i-1]\n if lastpos[x] == -1\n curr = x + bit_n.query(x+1, n)\n else\n curr = 1 + bit_m.query(lastpos[x]+1, i-1)\n bit_m.update(i, -1)\n end\n maxpos[x] = curr if curr > maxpos[x] \n lastpos[x] = i\n bit_m.update(i, 1)\n bit_n.update(x, 1) if bit_n.query(x, x) == 0\nend\n\n(1..n).each do |x|\n if lastpos[x] != -1\n curr = 1 + bit_m.query(lastpos[x]+1, m)\n else\n curr = x + bit_n.query(x+1, n)\n end\n maxpos[x] = curr if curr > maxpos[x]\nend\n\nminpos = (0..n).map {|x| lastpos[x] == -1 ? x : 1}\nanswer = minpos.zip(maxpos).map {|a, b| \"#{a} #{b}\"}\nputs answer[1..n]"}], "src_uid": "f57ed2d5009724b006202349675e2f2c"} {"nl": {"description": "Recently, your friend discovered one special operation on an integer array $$$a$$$: Choose two indices $$$i$$$ and $$$j$$$ ($$$i \\neq j$$$); Set $$$a_i = a_j = |a_i - a_j|$$$. After playing with this operation for a while, he came to the next conclusion: For every array $$$a$$$ of $$$n$$$ integers, where $$$1 \\le a_i \\le 10^9$$$, you can find a pair of indices $$$(i, j)$$$ such that the total sum of $$$a$$$ will decrease after performing the operation. This statement sounds fishy to you, so you want to find a counterexample for a given integer $$$n$$$. Can you find such counterexample and prove him wrong?In other words, find an array $$$a$$$ consisting of $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$) such that for all pairs of indices $$$(i, j)$$$ performing the operation won't decrease the total sum (it will increase or not change the sum).", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains a single integer $$$n$$$ ($$$2 \\le n \\le 1000$$$) — the length of array $$$a$$$.", "output_spec": "For each test case, if there is no counterexample array $$$a$$$ of size $$$n$$$, print NO. Otherwise, print YES followed by the array $$$a$$$ itself ($$$1 \\le a_i \\le 10^9$$$). If there are multiple counterexamples, print any.", "sample_inputs": ["3\n\n2\n\n512\n\n3"], "sample_outputs": ["YES\n1 337\nNO\nYES\n31 4 159"], "notes": "NoteIn the first test case, the only possible pairs of indices are $$$(1, 2)$$$ and $$$(2, 1)$$$.If you perform the operation on indices $$$(1, 2)$$$ (or $$$(2, 1)$$$), you'll get $$$a_1 = a_2 = |1 - 337| = 336$$$, or array $$$[336, 336]$$$. In both cases, the total sum increases, so this array $$$a$$$ is a counterexample."}, "positive_code": [{"source_code": "main = -> {\r\n t = int\r\n pow3 = [1, 3, 9, 27, 81, 243, 729, 2187, 6561, 19683, 59049, 177147, 531441, 1594323, 4782969, 14348907, 43046721, 129140163, 387420489]\r\n len = pow3.size\r\n t.times do\r\n n = int\r\n if n <= len\r\n puts \"YES\"\r\n puts pow3[0, n].join(\" \")\r\n else\r\n puts \"NO\"\r\n end\r\n end\r\n}\r\n\r\nDEBUG = true\r\nMOD = 10**9+7\r\n$yesno = %w(No Yes)\r\nYesNo = %w(No Yes)\r\nYESNO = %w(NO YES)\r\nINF = 10**9\r\n\r\nrequire \"set\"\r\nrequire \"prime\"\r\n\r\ndef int; gets.to_s.to_i end\r\ndef ints; gets.to_s.split.map { |s| s.to_i } end\r\ndef int1s; gets.to_s.split.map { |s| s.to_i - 1 } end\r\ndef float; gets.to_s.to_f end\r\ndef floats; gets.to_s.split.map { |s| s.to_f } end\r\ndef array_of(&convert); gets.to_s.split.map(&convert) end\r\ndef string; gets.to_s.chomp end\r\ndef strings; gets.to_s.split end\r\ndef rep(n, &b); Array.new(n, &b) end\r\ndef yes; puts $yesno[1] end\r\ndef no; puts $yesno[0] end\r\ndef yesno t; puts $yesno[t] end\r\ndef YesNo t; puts YesNo[t] end\r\ndef YESNO t; puts YESNO[t] end\r\ndef zip(xs, *yss); Enumerator.new { |y| xs.zip(*yss) { |a| y.yield(*a) } } end\r\ndef max(*xs, &block); block_given? ? xs.max_by(&block) : xs.max end\r\ndef min(*xs, &block); block_given? ? xs.min_by(&block) : xs.min end\r\ndef minmax(*xs, &block); block_given? ? xs.minmax_by(&block) : xs.minmax end\r\ndef gcd(*xs); xs.inject(0, :gcd) end\r\ndef matrix(h, w, fill=nil, &block); return Array.new(h) { Array.new(w, &block) } if block_given?; Array.new(h) { [fill] * w } end\r\ndef debug(x = nil); STDERR.puts (block_given? ? yield(x) : x).inspect if DEBUG; x end\r\ndef debug_grid(grid, width = 1); grid.each { |row| STDERR.puts row.map { |x| x.inspect.ljust(width) }.join(\"\") } if DEBUG; grid end\r\ndef if_debug; yield if DEBUG end\r\n\r\nmodule Boolean\r\n def coerce(other); [other, to_i] end\r\n def +@; to_i end\r\n def to_int; to_i end\r\n def *(other); to_i * other end\r\nend\r\n\r\nclass TrueClass\r\n include Boolean\r\n def to_i; 1 end\r\nend\r\n\r\nclass FalseClass\r\n include Boolean\r\n def to_i; 0 end\r\nend\r\n\r\nclass Integer\r\n def div_ceil(y); (self + y - 1) / y end\r\n def mod_inv(mod = MOD); pow(mod-2, mod) end\r\n def mod_div(y, mod = MOD); self * y.mod_inv(mod) % mod end\r\n def mod_nCr(r, mod = MOD); x = y = 1; (1..r).each { |i| x = x * (self + 1 - i) % mod; y = y * i % mod }; x.mod_div(y, mod); end\r\n def factorial(mod = MOD); (2..self).inject(1) { |f, x| f * x % mod } end\r\n def popcount; x = self; c = 0; while x > 0; c += 1 if x & 1 == 1; x >>= 1 end; c end #TODO: faster\r\n def bitbrute(&block); (1< 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\r\n def lower_bound; ac, wa = self.end_open, self.begin; while ac - wa > 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\r\n def shakutori(r2, &pred); Enumerator.new { |y| j, r = r2.begin, r2.end_open; each { |i| j += 1 while j + 1 < r and pred[i, j+1]; y.yield(i, j) } }; end\r\n def widest(&block); Enumerator.new { |y| j, n = self.begin, self.end_open; each { |i| j += 1 while j < n and block[i, j]; y.yield(i, j) if block[i, j] } } end\r\nend\r\n\r\nclass Array\r\n def power(&block); (0 ... 1 << size).each(&block) end\r\n def sorted_merge(other); a = []; i = j = 0; n, m = size, other.size; if j < m and other[j] < self[i]; a << other[j]; j += 1 else; a << self[i]; i += 1 end while i < n; a.push(*other[j..-1]) if j < m; a end\r\n def upper_bound; ac, wa = 0, size; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\r\n def lower_bound; ac, wa = size, 0; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\r\n def cum(*xs, &op); a = []; a << xs[0] if xs.size > 0; a << x = self[0]; (1...size).each { |i| a << x = op[x, self[i]] }; a end\r\n def cumdiff(range); self[range.end_open] - self[range.begin]; end\r\n def compress(kinds = uniq.sort!); map { |x| kinds.bsearch_index { _1 >= x } }; end\r\nend\r\n\r\nmodule Enumerable\r\n def sorted_uniq; x = nil; filter { |y| c = x === y; x = y; !c } end\r\n def cumsum; ys = [0]; each { |x| ys << x + ys[-1] }; ys end\r\nend\r\n\r\nmain[]"}], "negative_code": [], "src_uid": "c8fddee2f1c7d325437a7d0b82758b03"} {"nl": {"description": "Codeforces user' handle color depends on his rating — it is red if his rating is greater or equal to 2400; it is orange if his rating is less than 2400 but greater or equal to 2200, etc. Each time participant takes part in a rated contest, his rating is changed depending on his performance.Anton wants the color of his handle to become red. He considers his performance in the rated contest to be good if he outscored some participant, whose handle was colored red before the contest and his rating has increased after it.Anton has written a program that analyses contest results and determines whether he performed good or not. Are you able to do the same?", "input_spec": "The first line of the input contains a single integer n (1 ≤ n ≤ 100) — the number of participants Anton has outscored in this contest . The next n lines describe participants results: the i-th of them consists of a participant handle namei and two integers beforei and afteri ( - 4000 ≤ beforei, afteri ≤ 4000) — participant's rating before and after the contest, respectively. Each handle is a non-empty string, consisting of no more than 10 characters, which might be lowercase and uppercase English letters, digits, characters «_» and «-» characters. It is guaranteed that all handles are distinct.", "output_spec": "Print «YES» (quotes for clarity), if Anton has performed good in the contest and «NO» (quotes for clarity) otherwise.", "sample_inputs": ["3\nBurunduk1 2526 2537\nBudAlNik 2084 2214\nsubscriber 2833 2749", "3\nApplejack 2400 2400\nFluttershy 2390 2431\nPinkie_Pie -2500 -2450"], "sample_outputs": ["YES", "NO"], "notes": "NoteIn the first sample, Anton has outscored user with handle Burunduk1, whose handle was colored red before the contest and his rating has increased after the contest.In the second sample, Applejack's rating has not increased after the contest, while both Fluttershy's and Pinkie_Pie's handles were not colored red before the contest."}, "positive_code": [{"source_code": "n = gets.to_i\n\nn.times do\n\ts, b, a = gets.split\n\tbi = b.to_i\n\tif 2400 <= bi && bi < a.to_i then\n\t\tputs \"YES\"\n\t\texit 0\n\tend\nend\n\nputs \"NO\"\n"}, {"source_code": "#!/usr/bin/ruby\nputs gets.to_i.times.any?{\n\te=gets.split.map(&:to_i)\n\t2400<=e[1] && e[1]= 2400) ? \"red\" : \"other\"\nend\n\nn = gets.chomp.to_i\narr = [].tap do |out|\n n.times do\n out << gets.chomp.split\n end\nend\n\nputs arr.any? { |n, a, b| color(a.to_i) == \"red\" && b.to_i > a.to_i } ? \"YES\" : \"NO\"\n"}, {"source_code": "n = gets.to_i\nans = 'NO'\nn.times do\n name, b, a = gets.split\n if b.to_i >= 2400 && b.to_i < a.to_i\n ans = 'YES'\n end\nend\nputs ans"}, {"source_code": "n = gets.strip.to_i\nans = \"NO\"\nn.times do\n line = gets.strip.split(/ /)\n before = line[1].to_i\n after = line[2].to_i\n ans = \"YES\" if before >= 2400 && after > before\nend\nputs ans\n"}, {"source_code": "n = gets.to_i\nq = []\n\n(1..n).each do |i|\n a, b, c = gets.split\n b, c = b.to_i, c.to_i\n q << [a,b,c]\nend\n\nq.delete_if { |e| e[1] < 2400 || e[1] >= e[2] }\n\nputs q.empty? ? 'NO' : 'YES'"}, {"source_code": "n = gets.to_i\nn.times do |i|\n a, b, c = gets.split\n if b.to_i >= 2400 && c.to_i - b.to_i > 0\n puts \"YES\"\n exit\n end\nend\nputs \"NO\""}, {"source_code": "n = gets.chomp.to_i\nf = false\nn.times do\n name, before, after = gets.chomp.split\n if 2400 <= before.to_i and before.to_i < after.to_i\n f = true\n end\nend\n\nputs f ? \"YES\" : \"NO\"\n"}, {"source_code": "n = gets.chomp.to_i\na = []\ncount = 0\nfor i in (0..n-1)\n\ta << gets.chomp.split\n\tif a[i][1].to_i >= 2400 && a[i][2].to_i > a[i][1].to_i\n\t\tcount += 1\n\tend\nend\nif count > 0\n\tprint \"YES\"\nelse\n\tprint \"NO\"\nend"}, {"source_code": "puts gets.to_i.times.map{gets.split.map(&:to_i)}.any?{|*,a,b| a>=2400 && b>a} ? \"YES\" : \"NO\""}, {"source_code": "n = gets.to_i\nans = false\nn.times do\n _, s1, s2 = gets.split.map(&:to_i)\n ans = true if s1 >= 2400 && s2 > s1\nend \nputs ans ? 'YES' : 'NO'\n"}, {"source_code": "n = gets.to_i\n\nout = 'NO'\nfor i in 1..n\n user = gets.split\n pre = user[1].to_i\n if pre >= 2400\n if user[2].to_i > pre\n out = 'YES'\n end\n end\nend\n\nputs out\n"}, {"source_code": "n=gets.to_i\nans=false;\nn.times{\n\tbuf=gets.split(' ');\n\tname,before,after = buf[0], buf[1].to_i, buf[2].to_i\n\tans=true if before>=2400&&after>before\n}\nputs ans==true ? \"YES\" : \"NO\""}, {"source_code": "puts gets.to_i.times.map{gets.split.map(&:to_i)}.any?{|*,a,b| a>=2400 && b>a} ? \"YES\" : \"NO\""}, {"source_code": "n = gets.to_i\nn.times do\n _, before, after = gets.split.map &:to_i\n if 2400 <= before and before < after\n puts 'YES'\n exit\n end\nend\nputs 'NO'\n"}, {"source_code": "POINT_VALUE = 2400\n\ntime = gets.chomp.to_i\n\nresult = false\ntime.times do |n| \n\tline = gets.chomp\n\t_, before, after = line.split(' ').map &:to_i\n\n\tresult = true if before >= POINT_VALUE and after > before\nend\n\nputs result ? 'YES' : 'NO'"}, {"source_code": "good = false\n\nn = gets.chomp.to_i\nn.times do\n name, prev_score, after_score = gets.chomp.split\n if(prev_score.to_i >= 2400 && after_score.to_i > prev_score.to_i)\n good = true\n break\n end\nend\n\nif good\n puts \"YES\"\nelse\n puts \"NO\"\nend"}, {"source_code": "peopleNum = gets.chomp.to_i\npeoples = []\n\npeopleNum.times do\n people = gets.chomp.split(' ')\n people[1] = people[1].to_i\n people[2] = people[2].to_i\n \n peoples << people\nend\n\nred = []\n\npeoples.each do |people|\n red << people if people[1] >= 2400 && people[2] > people[1]\nend\n\nans = if red.length > 0\n \"YES\"\nelse\n \"NO\"\nend\n\nputs ans"}], "negative_code": [{"source_code": "n = gets.chomp.to_i\na = []\nfor i in (0..n-1)\n\ta << gets.chomp.split\n\tcount = 0\n\tif a[i][1].to_i >= 2400 && a[i][2].to_i > a[i][1].to_i\n\t\tcount += 1\n\tend\nend\nif count > 0\n\tprint \"YES\"\nelse\n\tprint \"NO\"\nend"}, {"source_code": "n = gets.chomp.to_i\na = []\nfor i in (0..n-1)\n\ta << gets.chomp.split\n\tcount = 0\n\tif a[i][1].to_i >= 2400 && a[i][2].to_i != a[i][1].to_i && a[i][2].to_i >= 2400\n\t\tcount += 1\n\tend\nend\nif count > 0\n\tprint \"YES\"\nelse\n\tprint \"NO\"\nend"}, {"source_code": "p gets.to_i.times.map{gets.split.map(&:to_i)}.any?{|*,a,b| a>=2400 && b>a} ? \"YES\" : \"NO\""}, {"source_code": "POINT_VALUE = 2400\n\ntime = gets.chomp.to_i\n\nresult = false\ntime.times do |n| \n\tline = gets.chomp\n\t_, before, after = line.split(' ').map &:to_i\n\n\tresult = true if before > POINT_VALUE and after > before\nend\n\nputs result ? 'YES' : 'NO'"}], "src_uid": "3bff9b69423cf6c8425e347a4beef96b"} {"nl": {"description": "Emuskald is a well-known illusionist. One of his trademark tricks involves a set of magical boxes. The essence of the trick is in packing the boxes inside other boxes.From the top view each magical box looks like a square with side length equal to 2k (k is an integer, k ≥ 0) units. A magical box v can be put inside a magical box u, if side length of v is strictly less than the side length of u. In particular, Emuskald can put 4 boxes of side length 2k - 1 into one box of side length 2k, or as in the following figure: Emuskald is about to go on tour performing around the world, and needs to pack his magical boxes for the trip. He has decided that the best way to pack them would be inside another magical box, but magical boxes are quite expensive to make. Help him find the smallest magical box that can fit all his boxes.", "input_spec": "The first line of input contains an integer n (1 ≤ n ≤ 105), the number of different sizes of boxes Emuskald has. Each of following n lines contains two integers ki and ai (0 ≤ ki ≤ 109, 1 ≤ ai ≤ 109), which means that Emuskald has ai boxes with side length 2ki. It is guaranteed that all of ki are distinct.", "output_spec": "Output a single integer p, such that the smallest magical box that can contain all of Emuskald’s boxes has side length 2p.", "sample_inputs": ["2\n0 3\n1 5", "1\n0 4", "2\n1 10\n2 2"], "sample_outputs": ["3", "1", "3"], "notes": "NotePicture explanation. If we have 3 boxes with side length 2 and 5 boxes with side length 1, then we can put all these boxes inside a box with side length 4, for example, as shown in the picture.In the second test case, we can put all four small boxes into a box with side length 2."}, "positive_code": [{"source_code": "#!/usr/bin/ruby1.9\n\nn = STDIN.readline.to_i\nsizes = []\nn.times do\n\tk, a = STDIN.readline.split.collect {|s| s.to_i }\n\tp = (Math.log2(a)/2).ceil\n\tp = 1 if p == 0\n\tsizes.push(k+p)\nend\n\nputs sizes.sort[n-1]\n\n"}, {"source_code": "#!/usr/bin/ruby1.9\n\nn = STDIN.readline.to_i\nka = []\nn.times do\n\tk, a = STDIN.readline.split.collect {|s| s.to_i }\n\tka.push([k, a])\nend\n#ka.sort!\n\nsizes = []\nka.each do |k, a|\n\tp = (Math.log2(a)/2).ceil\n\tp = 1 if p == 0\n\tsizes.push(k+p)\nend\n\nputs sizes.sort[n-1]\n\n"}, {"source_code": "n = gets.to_i\nmax = 0;\nn.times{ \n k,x = gets.split(\" \").map(&:to_i)\n d = (Math.log2(Math.sqrt(x)) + k.to_f).ceil\n if ( d == k ) then \n d = d+1\n end\n if( d > max) then \n max = d\n end\n}\nif max>0 then\nputs max\nelse\nputs \"0\"\nend"}, {"source_code": "def run\n n = ARGF.readline.to_i\n\n ret = 0\n ARGF.readlines.each do |line|\n k, a = line.split.map(&:to_i)\n\n cur = k + [(Math.log(a, 4)).ceil, 1].max\n ret = [cur, ret].max\n end\n\n puts ret\nend\n\nrun if $0 == __FILE__\n"}, {"source_code": "#!/usr/bin/env ruby\n\nn = gets.to_i\np = 0\nn.times do\n\tk, a = gets.split.map(&:to_i)\n\tif a == 1\n\t\tp = [p, k + 1].max\n\telse\n\t\tp = [p, k + Math.log(a, 4).ceil].max\n\tend\nend\nputs p\n"}], "negative_code": [{"source_code": "#!/usr/bin/ruby1.9\n\nn = STDIN.readline.to_i\nka = []\nn.times do\n\tk, a = STDIN.readline.split.collect {|s| s.to_i }\n\tka.push([k, a])\nend\nka.sort!\n\nsizes = []\nka.each do |k, a|\n\tp = (Math.log2(a)/2).ceil\n\tsizes.push(k+p)\nend\n\nputs sizes.sort[n-1]\n\n"}, {"source_code": "n = gets.to_i\nmax = 0;\nn.times{ \n k,x = gets.split(\" \").map(&:to_i)\n d = (Math.log2(x) + k.to_f).ceil\n if ( d == k ) then \n d = d+1\n end\n if( d > max) then \n max = d\n end\n}\nif max>0 then\nputs max\nelse\nputs \"0\"\nend"}, {"source_code": "n = gets.to_i\nmax = 0;\nn.times{ \n\tk,x = gets.split(\" \").map(&:to_i)\n\tt = ((2**(k))**2) * x\n\tif( t > max) then \n\t\tmax = t \n\tend\n}\nif max>0 then\nputs Math.log2(Math.sqrt(max)).ceil\nelse\nputs \"0\"\nend\n"}, {"source_code": "n = gets.to_i\nmax = 0;\nn.times{ \n\tk,x = gets.split(\" \").map(&:to_i)\n\tt = ((2**(k))**2) * x\n\tif( t > max) then \n\t\tmax = t \n\tend\n}\nputs Math.log2(Math.sqrt(max)).ceil\n\n"}, {"source_code": "n = gets.to_i\nmax = 0;\nn.times{ \n k,x = gets.split(\" \").map(&:to_i)\n d = k + Math.log2(x).ceil\n if ( d == k ) then \n d = d+1\n end\n if( d > max) then \n max = d\n end\n}\nif max>0 then\nputs max\nelse\nputs \"0\"\nend"}, {"source_code": "def run\n n = ARGF.readline.to_i\n\n ret = 0\n ARGF.readlines.each do |line|\n k, a = line.split.map(&:to_i)\n\n cur = k + [(Math.log(a, 4)).ceil, 1].min\n ret = [cur, ret].max\n end\n\n puts ret\nend\n\nrun if $0 == __FILE__\n"}], "src_uid": "15aac7420160f156d5b73059af1fae3b"} {"nl": {"description": "According to the regulations of Berland's army, a reconnaissance unit should consist of exactly two soldiers. Since these two soldiers shouldn't differ much, their heights can differ by at most d centimeters. Captain Bob has n soldiers in his detachment. Their heights are a1, a2, ..., an centimeters. Some soldiers are of the same height. Bob wants to know, how many ways exist to form a reconnaissance unit of two soldiers from his detachment.Ways (1, 2) and (2, 1) should be regarded as different.", "input_spec": "The first line contains two integers n and d (1 ≤ n ≤ 1000, 1 ≤ d ≤ 109) — amount of soldiers in Bob's detachment and the maximum allowed height difference respectively. The second line contains n space-separated integers — heights of all the soldiers in Bob's detachment. These numbers don't exceed 109.", "output_spec": "Output one number — amount of ways to form a reconnaissance unit of two soldiers, whose height difference doesn't exceed d.", "sample_inputs": ["5 10\n10 20 50 60 65", "5 1\n55 30 29 31 55"], "sample_outputs": ["6", "6"], "notes": null}, "positive_code": [{"source_code": "n, d = gets.split.map{|i| i.to_i}\n\nsolds = gets.split.map{|i| i.to_i}\n\nres = 0\nn.times do |i|\n n.times do |j|\n if (solds[i] - solds[j]).abs <= d then\n res += 1\n end\n end\nend\n\nputs res-n"}, {"source_code": "s = gets.chomp\nns,ds = s.split(/ /)\nn = ns.to_i\nd = ds.to_i\ns = gets.chomp\nss = s.split(/ /)\n\na = Array.new\nfor i in 0..n-1\n\ta[i] = ss[i].to_i\nend\n\nsum = 0\nfor i in 0..n-2\n\tfor j in i+1..n-1\n\t\tif (a[i]-a[j]).abs <= d\n\t\t\tsum += 2 \n\t\tend\n\tend\t\nend\nputs sum"}, {"source_code": "n,d=gets.split.map &:to_i\na=gets.split.map &:to_i\nans=0\nn.times do |i|\n n.times do |j|\n if i!=j && (a[i]-a[j]).abs<=d\n ans+=1\n end\n end\nend\np ans"}, {"source_code": "#!/usr/bin/ruby\nd,a=$<.map{|l|l.split.map(&:to_i)}\nr=0\na.size.times{|i|(i+1).step(a.size-1){|j|r+=2 if (a[i]-a[j]).abs<=d[1]}}\np r"}, {"source_code": "d=gets.split[1].to_i\nans=0\ni=gets.split.map(&:to_i)\ni.each{|a|\n i.each{|b|\n ans=ans+(((a-b).abs<=d)?1:0)\n }\n}\nputs ans-i.length"}, {"source_code": "n,d=gets.split.map(&:to_i)\np gets.split.map(&:to_i).combination(2).count{|a,b|(a-b).abs<=d}*2\n"}, {"source_code": "n,d=gets.split.map(&:to_i)\np gets.split.map(&:to_i).combination(2).count{|a,b|(a-b).abs<=d}*2\n"}, {"source_code": "n,d=gets.split.map(&:to_i)\np gets.split.map(&:to_i).combination(2).count{|a,b|(a-b).abs<=d}*2"}, {"source_code": "n,d=gets.split.map(&:to_i)\np gets.split.map(&:to_i).combination(2).count{|a,b|(a-b).abs<=d}*2\n"}, {"source_code": "n, d = gets.split.map(&:to_i)\nas = gets.split.map(&:to_i).sort\n\nans = 0\n(0..n-2).each do |i|\n ((i+1)..n-1).each do |j|\n if (as[i] - as[j]).abs <= d\n ans += 1\n end\n end\nend\n\nputs ans * 2\n"}, {"source_code": "n,d=gets.split.map(&:to_i)\np gets.split.map(&:to_i).combination(2).count{|a,b|(a-b).abs<=d}*2\n"}, {"source_code": "n,d=gets.split.map(&:to_i)\np gets.split.map(&:to_i).combination(2).count{|a,b|(a-b).abs<=d}*2"}, {"source_code": "ma = gets.split(\" \").map{|i| i.to_i}\nmas = gets.split(\" \").map{|i| i.to_i}\nmas = mas.sort\nans = 0\nmas.each{|a|\n mas.each{|b|\n \tif (a - b).abs <= ma[1]\n \t\tans = ans + 1\n \tend\n }\n}\nputs ans - ma[0]"}, {"source_code": "def main()\n\tn,d=gets.chomp.split(\" \").map{|i| i.to_i}\n\thar=gets.chomp.split(\" \").map{|i| i.to_i}\n\tans=0\n\tfor i in 0...n\n\t\tfor j in 0...n\n\t\t\tif i==j\n\t\t\t\tnext;\n\t\t\tend\n\t\t\tif (har[i]-har[j]).abs<=d\n\t\t\t\tans+=1\n\t\t\tend\n\t\tend\n\tend\n\tputs ans\nend\nmain()"}, {"source_code": "n,d=gets.split.map(&:to_i)\np gets.split.map(&:to_i).combination(2).count{|a,b|(a-b).abs<=d}*2\n"}, {"source_code": "s = gets.chomp!.split(\" \")\nn = s[0].to_i\nd = s[1].to_i\n\ns = gets.chomp!.split(\" \")\nmas = Array.new\nfor i in 1..n\n mas[i] = s[i-1].to_i\nend\n\ncount = 0\nfor i in 1..n\n for j in 1..n\n if (i != j && (mas[i] - mas[j]).abs <= d)\n count += 1\n end\n end\nend\n\nputs(count)"}, {"source_code": "n=gets(\" \").chomp.to_i\nd=gets.chomp.to_i\ndata=gets.chomp.split(/ /)\nsol=[]\nfor i in 0..(n-1)\n\tsol.push(data[i].to_i)\nend\n#puts sol\nsol=sol.sort{|a,b|\n\ta<=>b\n}\n#puts sol\n\nans=0\nfor i in 0..(n-1)\n\tj=1\n\twhile (i+j) 1\n mid = (ok+ng)/2\n if arr[mid] >= t\n ok = mid\n else\n ng = mid\n end\n end\n return ok\nend\n\nn,m,ta,tb,k = gets.split.map(&:to_i)\n\na = gets.split.map(&:to_i)\nb = gets.split.map(&:to_i)\n\nif n <= k || m <= k\n p -1\n exit\nend\nans = 0\n0.upto(k) do |i|\n ib = bsearch(b,a[i]+ta)\n if ib+k-i > m-1 || a[i]+ta > b[ib]\n p -1\n exit\n end\n#p [i,ib]\n ans = b[ib+k-i] if ans < b[ib+k-i]\nend\np ans+tb\n"}], "negative_code": [{"source_code": "def bsearch(arr,t)\n ok,ng = arr.length-1,-1\n while (ng - ok).abs > 1\n mid = (ok+ng)/2\n if arr[mid] >= t\n ok = mid\n else\n ng = mid\n end\n end\n return ok\nend\n\nn,m,ta,tb,k = gets.split.map(&:to_i)\n\na = gets.split.map(&:to_i)\nb = gets.split.map(&:to_i)\n\nia = 0\n\nif n <= k\n p -1\n exit\nend\nans = 0\n1.upto(k) do |i|\n ib = bsearch(b,a[i]+ta)\n if ib+k-i > m-1 || a[i]+ta > b[ib]\n p -1\n exit\n end\n#p [i,ib]\n ans = b[ib+k-i] if ans < b[ib+k-i]\nend\np ans+tb\n"}, {"source_code": "def bsearch(arr,t)\n ok,ng = arr.length-1,-1\n while (ng - ok).abs > 1\n mid = (ok+ng)/2\n if arr[mid] >= t\n ok = mid\n else\n ng = mid\n end\n end\n return ok\nend\n\nn,m,ta,tb,k = gets.split.map(&:to_i)\n\na = gets.split.map(&:to_i)\nb = gets.split.map(&:to_i)\n\nia = 0\nib = bsearch(b,a[0]+ta)\n\nt = b[ib] + tb\nans = []\n\nif n <= k\n p -1\n exit\nend\nans = 0\n1.upto(k) do |i|\n ib = bsearch(b,a[i]+ta)\n if ib+k-i > m-1\n p -1\n exit\n end\n#p [i,ib]\n ans = b[ib+k-i] if ans < b[ib+k-i]\nend\np ans+tb\n"}], "src_uid": "bf60899aa2bd7350c805437d0fee1583"} {"nl": {"description": "You have a description of a lever as string s. We'll represent the string length as record |s|, then the lever looks as a horizontal bar with weights of length |s| - 1 with exactly one pivot. We will assume that the bar is a segment on the Ox axis between points 0 and |s| - 1.The decoding of the lever description is given below. If the i-th character of the string equals \"^\", that means that at coordinate i there is the pivot under the bar. If the i-th character of the string equals \"=\", that means that at coordinate i there is nothing lying on the bar. If the i-th character of the string equals digit c (1-9), that means that at coordinate i there is a weight of mass c on the bar. Your task is, given the lever description, print if it will be in balance or not. Assume that the bar doesn't weight anything. Assume that the bar initially is in balance then all weights are simultaneously put on it. After that the bar either tilts to the left, or tilts to the right, or is in balance.", "input_spec": "The first line contains the lever description as a non-empty string s (3 ≤ |s| ≤ 106), consisting of digits (1-9) and characters \"^\" and \"=\". It is guaranteed that the line contains exactly one character \"^\". It is guaranteed that the pivot of the lever isn't located in any end of the lever bar. To solve the problem you may need 64-bit integer numbers. Please, do not forget to use them in your programs.", "output_spec": "Print \"left\" if the given lever tilts to the left, \"right\" if it tilts to the right and \"balance\", if it is in balance.", "sample_inputs": ["=^==", "9===^==1", "2==^7==", "41^52=="], "sample_outputs": ["balance", "left", "right", "balance"], "notes": "NoteAs you solve the problem, you may find the following link useful to better understand how a lever functions: http://en.wikipedia.org/wiki/Lever.The pictures to the examples: "}, "positive_code": [{"source_code": "a=gets.chomp.split('')\ns=0\nlen=a.length\nfor i in 0..len-1\n\tif a[i]=='^'\n\t\tbreak\n\tend\nend\nfor j in 0..len-1\n\tif a[j]!='='\n\t\ts+=a[j].to_i*(i-j)\n\tend\nend\nputs s>0? \"left\" : s==0? \"balance\" : \"right\"\n"}, {"source_code": "s = gets.chomp\nmid = s.index \"^\"\nleft = 0\nright = 0\n(0...mid).each do |i|\n left += (mid - i) * s[i].to_i if s[i] != '='\nend\n((mid + 1)...s.length).each do |i|\n right += (i - mid) * s[i].to_i if s[i] != '='\nend\nans = right <=> left\ncase ans\nwhen 0\n puts \"balance\"\nwhen 1\n puts \"right\"\nelse\n puts \"left\"\nend\n"}, {"source_code": "class Weights\n attr_accessor :string, :balance\n\n def initialize(value)\n raise \"Node value shouldn't be nil or empty\" if value.nil?\n @string = value\n\n end\n\n def balance\n left, right = @string.split(\"^\")\n left = left.reverse\n\n left_weight = weigh(left)\n right_weight = weigh(right)\n\n\n if left_weight > right_weight\n return \"left\"\n elsif left_weight < right_weight\n return \"right\"\n else\n return \"balance\"\n end\n\n end\n\n private\n\n def weigh(input)\n w = 0\n input.split(\"\").each_with_index do |element, index|\n w += element.to_i * (index + 1) unless element == \"=\"\n end\n w\n end\n\nend\n\n\nstr = gets\nputs Weights.new(str).balance"}, {"source_code": "left,right = gets.split(\"^\")\nleft.reverse!\nsum_arr = []\ni = 1\nsum = 0\nleft.each_char do |l|\n\tsum += l.to_i * i if l != '='\n\ti += 1\nend\n\nsum_arr << sum\n\ni = 1\nsum = 0\nright.each_char do |r|\n\tsum += r.to_i * i if r != '='\n\ti += 1\nend\nsum_arr << sum\n# puts \"#{sum_arr}\"\n\nif sum_arr[0] == sum_arr[1]\n\tans = \"balance\"\nelsif sum_arr[0] > sum_arr[1]\n\tans = \"left\"\nelse\n\tans = \"right\"\nend\nputs ans\n"}, {"source_code": "s = gets.chomp\nfulcrum = s.index('^')\nleft = 0\nright = 0\ni = 1\nwhile fulcrum-i >= 0\n if s[fulcrum-i].to_i.to_s == s[fulcrum-i]\n left += i*s[fulcrum-i].to_i\n end\n i += 1\nend\ni = 1\nwhile fulcrum+i < s.size\n if s[fulcrum+i].to_i.to_s == s[fulcrum+i]\n right += i*s[fulcrum+i].to_i\n end\n i += 1\nend\ncase left <=> right\nwhen 1\n puts 'left'\nwhen -1\n puts 'right'\nwhen 0\n puts 'balance'\nend\n"}, {"source_code": "s = gets.chomp\nfulcrum = s.index('^')\nleft = 0\nright = 0\ni = 1\nwhile fulcrum-i >= 0\n left += i*s[fulcrum-i].to_i if s[fulcrum-i].to_i.to_s == s[fulcrum-i]\n i += 1\nend\ni = 1\nwhile fulcrum+i < s.size\n right += i*s[fulcrum+i].to_i if s[fulcrum+i].to_i.to_s == s[fulcrum+i]\n i += 1\nend\ncase left <=> right\nwhen 1\n puts 'left'\nwhen -1\n puts 'right'\nwhen 0\n puts 'balance'\nend\n"}, {"source_code": "bar = gets\npivot = bar.index(\"^\")\n\nleft_weight = 0\nright_weight = 0\n\n(0...bar.length).each do |index|\n piece = bar[index].chr\n if piece =~ /[[:digit:]]/\n if index < pivot\n left_weight += (pivot - index) * piece.to_i\n else\n right_weight += (index - pivot) * piece.to_i\n end\n end\nend\n\nif left_weight > right_weight\n puts \"left\"\nelsif right_weight > left_weight\n puts \"right\"\nelse\n puts \"balance\"\nend\n\n\n\n"}, {"source_code": "s=gets.chomp\nx=s.index '^'\ni=1\nz=0\nwhile x-i>=0\n c=s[x-i].to_i\n z+=c*i if c>0\n i+=1\nend\ni=1\nwhile x+i0\n i+=1\nend\nif z>0\n puts 'left'\nelsif z==0\n puts 'balance'\nelse\n puts 'right'\nend\n"}, {"source_code": "puts (->(x){ x.first > x.last ? 'left' : (x.first < x.last ? 'right' : 'balance')}.call(->(x) {[x.first.reverse, x.last] }.call((STDIN.readline.chomp).split('^').map{|p| p.scan(/./)}).map{|x| x.each_with_index.inject(0){|s,p| s = s + p.first.to_i * (p.last + 1) }}))"}], "negative_code": [{"source_code": "s = gets.chomp\nmid = s.index \"^\"\nleft = []\nright = []\n(0...mid).each do |i|\n if s[i] != '='\n left << s[i].to_i\n else\n left << 0\n end\nend\n((mid + 1)...s.length).each do |i|\n if s[i] != '='\n right << s[i].to_i\n else\n right << 0\n end\nend\nleft.delete 0\nright.delete 0\nans = 0\nif left.length == right.length\n left.length.times do |i|\n l = left[left.length - 1 - i]\n r = right[i]\n ans += r <=> l\n end\nelse\n ans = left.inject(:+) <=> right.inject(:+)\nend\ncase ans\nwhen 0\n puts \"balance\"\nwhen 1\n puts \"right\"\nelse\n puts \"left\"\nend\n"}, {"source_code": "s = gets.chomp\nmid = s.index \"^\"\nleft = 0\nright = 0\n(0...mid).each do |i|\n left += (mid - 1) * s[i].to_i if s[i] != '='\nend\n((mid + 1)...s.length).each do |i|\n right += (i - mid) * s[i].to_i if s[i] != '='\nend\nans = right <=> left\ncase ans\nwhen 0\n puts \"balance\"\nwhen 1\n puts \"right\"\nelse\n puts \"left\"\nend\n"}, {"source_code": "->(x){ x.first > x.last ? 'left' : (x.first < x.last ? 'right' : 'balance')}.call(->(x) {[x.first.reverse, x.last] }.call((STDIN.readline.chomp).split('^').map{|p| p.scan(/./)}).map{|x| x.each_with_index.map{|p,i| [p, i+1]}}.map{|x| x.map{|t|[t.first.to_i, t.last]}}.map{|x| x.inject(0){|s, t| s = s + t.first * t.last }})"}, {"source_code": "puts (->(x){ x.first > x.last ? 'left' : (x.first < x.last ? 'right' : 'balance')}.call(->(x) {[x.first.reverse, x.last] }.call((STDIN.readline.chomp).split('^').map{|p| p.scan(/./)}).map{|x| x.each_with_index.inject(0){|s,p| s = s + p.first.to_i * p.last}}))"}, {"source_code": "->(x){ x.first > x.last ? 'left' : (x.first < x.last ? 'right' : 'balance')}.call(->(x) {[x.first.reverse, x.last] }.call((STDIN.readline.chomp).split('^').map{|p| p.scan(/./)}).map{|x| x.each_with_index.map{|p,i| [p, i+1]}}.map{|x| x.map{|t|[t.first.to_i, t.last]}}.map{|x| x.inject(0){|s, t| s = s + t.first * t.last }})\n"}], "src_uid": "19f2c21b18e84f50e251b1dfd557d32f"} {"nl": {"description": "A positive integer $$$x$$$ is called a power of two if it can be represented as $$$x = 2^y$$$, where $$$y$$$ is a non-negative integer. So, the powers of two are $$$1, 2, 4, 8, 16, \\dots$$$.You are given two positive integers $$$n$$$ and $$$k$$$. Your task is to represent $$$n$$$ as the sum of exactly $$$k$$$ powers of two.", "input_spec": "The only line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le n \\le 10^9$$$, $$$1 \\le k \\le 2 \\cdot 10^5$$$).", "output_spec": "If it is impossible to represent $$$n$$$ as the sum of $$$k$$$ powers of two, print NO. Otherwise, print YES, and then print $$$k$$$ positive integers $$$b_1, b_2, \\dots, b_k$$$ such that each of $$$b_i$$$ is a power of two, and $$$\\sum \\limits_{i = 1}^{k} b_i = n$$$. If there are multiple answers, you may print any of them.", "sample_inputs": ["9 4", "8 1", "5 1", "3 7"], "sample_outputs": ["YES\n1 2 2 4", "YES\n8", "NO", "NO"], "notes": null}, "positive_code": [{"source_code": "# Utility\ndef digits(num, base)\n num.to_s(base).split(\"\").reverse!.map { |c| c.to_i }\nend\n\n# Input\nn, k = *(gets.split.map { |s| s.to_i })\n\n# Processing\nb = digits(n, 2)\n#p b\ndecomposition = b.map.with_index { |e, i| e * 2**i }\ndecomposition = decomposition.reject { |e| e == 0 }\ndecomposition.reverse!\n# decomposition is sorted so largest element is first\n#p decomposition\n\nif decomposition.length > k || k > n\n puts \"NO\"\nelse\n # Will be splitting numbers until we get enough\n while decomposition.length != k\n num = decomposition.shift\n if num == 1\n decomposition.push(num)\n else\n decomposition.push(num / 2, num / 2)\n end\n end\n\n puts \"YES\"\n puts decomposition.join(\" \")\nend\n"}, {"source_code": "N,K = gets.split.map(&:to_i)\nbs = []\nn = N\nwhile n > 0\n bs << (n&1)\n n >>= 1\nend\n\nt = bs.count(1)\nif K < t || N < K\n puts \"NO\"; exit\nend\n\n(1...bs.size).reverse_each{ |b|\n if K - t <= bs[b]\n bs[b] -= K-t\n bs[b-1] += 2*(K-t)\n break\n end\n bs[b-1] += 2*bs[b]\n t += bs[b]\n bs[b] = 0\n}\n\nputs \"YES\"\nb = 1\nputs bs.map.with_index{ |b,i| [1 << i] * b }.flatten.join(\" \")"}, {"source_code": "nk = gets.chomp.split(\" \").map(&:to_i)\nn = nk[0]\nk = nk[1]\n\nn = n.to_s(2)\n\nmink = n.count(\"1\")\nmaxk = 0\nlen = n.length\n\nn.each_char.with_index do |ch,i|\n pos = len-i-1\n maxk += 2**pos if ch == \"1\"\nend\n\nif k < mink or k > maxk\n puts \"NO\"\n exit\nend\n\ndef myprint(narr,k)\n sum = narr.inject(:+)\n while sum < k\n narr[0] -= 1\n narr[1] += 2\n sum += 1\n if narr[0] == 0\n narr.shift\n end\n end\n\n len = narr.length\n narr.each_with_index do |x,i|\n x.times do\n print 2**(len-i-1)\n print \" \"\n end\n end\nend\n\nputs \"YES\"\nmyprint(n.each_char.to_a.map(&:to_i),k)\nputs\n"}], "negative_code": [{"source_code": "# Utility\ndef digits(num, base)\n num.to_s(base).split(\"\").map { |c| c.to_i }\nend\n\n# Input\nn, k = *(gets.split.map { |s| s.to_i })\n\n# Processing\nb = digits(n, 2)\ndecomposition = b.map.with_index { |e, i| e * 2**i }\ndecomposition = decomposition.reject { |e| e == 0 }\ndecomposition.reverse!\n# decomposition is sorted so largest element is first\n\nif decomposition.length > k || k > n\n puts \"NO\"\nelse\n # Will be splitting numbers until we get enough\n while decomposition.length != k\n num = decomposition.shift\n if num == 1\n decomposition.push(num)\n else\n decomposition.push(num / 2, num / 2)\n end\n end\n\n puts \"YES\"\n puts decomposition.join(\" \")\nend\n"}], "src_uid": "10d6179b479e1238a51154a9a6fc13cb"} {"nl": {"description": "You have $$$n$$$ rectangular wooden blocks, which are numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th block is $$$1$$$ unit high and $$$\\lceil \\frac{i}{2} \\rceil$$$ units long.Here, $$$\\lceil \\frac{x}{2} \\rceil$$$ denotes the result of division of $$$x$$$ by $$$2$$$, rounded up. For example, $$$\\lceil \\frac{4}{2} \\rceil = 2$$$ and $$$\\lceil \\frac{5}{2} \\rceil = \\lceil 2.5 \\rceil = 3$$$.For example, if $$$n=5$$$, then the blocks have the following sizes: $$$1 \\times 1$$$, $$$1 \\times 1$$$, $$$1 \\times 2$$$, $$$1 \\times 2$$$, $$$1 \\times 3$$$. The available blocks for $$$n=5$$$ Find the maximum possible side length of a square you can create using these blocks, without rotating any of them. Note that you don't have to use all of the blocks. One of the ways to create $$$3 \\times 3$$$ square using blocks $$$1$$$ through $$$5$$$ ", "input_spec": "Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^9$$$) — the number of blocks.", "output_spec": "For each test case, print one integer — the maximum possible side length of a square you can create.", "sample_inputs": ["3\n\n2\n\n5\n\n197654321"], "sample_outputs": ["1\n3\n98827161"], "notes": "NoteIn the first test case, you can create a $$$1 \\times 1$$$ square using only one of the blocks.In the second test case, one of the possible ways to create a $$$3 \\times 3$$$ square is shown in the statement. It is impossible to create a $$$4 \\times 4$$$ or larger square, so the answer is $$$3$$$."}, "positive_code": [{"source_code": "gets.to_i.times{p (gets.to_i+1)/2}"}, {"source_code": "gets.to_i.times{p (gets.to_i+1)/2}"}, {"source_code": "gets.to_i.times{p (gets.to_i+1)/2}"}, {"source_code": "gets.to_i.times{p (gets.to_i+1)/2}"}, {"source_code": "gets.to_i.times{p (gets.to_i+1)/2}"}], "negative_code": [], "src_uid": "0c5cf0af057b0c838f13b491b923447a"} {"nl": {"description": "You are given a special jigsaw puzzle consisting of $$$n\\cdot m$$$ identical pieces. Every piece has three tabs and one blank, as pictured below. The jigsaw puzzle is considered solved if the following conditions hold: The pieces are arranged into a grid with $$$n$$$ rows and $$$m$$$ columns. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece. Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle.", "input_spec": "The test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1\\le t\\le 1000$$$) — the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. Each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n,m \\le 10^5$$$).", "output_spec": "For each test case output a single line containing \"YES\" if it is possible to solve the jigsaw puzzle, or \"NO\" otherwise. You can print each letter in any case (upper or lower).", "sample_inputs": ["3\n1 3\n100000 100000\n2 2"], "sample_outputs": ["YES\nNO\nYES"], "notes": "NoteFor the first test case, this is an example solution: For the second test case, we can show that no solution exists.For the third test case, this is an example solution: "}, "positive_code": [{"source_code": "file = File.exist?('in.in') ? File.new('in.in','r') : STDIN\nt = file.gets.chomp.to_i\nt.times do\n n, m = file.gets.chomp.split.map(&:to_i)\n if (n == 1 || m == 1) || (n==2 && m == 2)\n puts \"YES\"\n else\n puts \"NO\"\n end\nend"}, {"source_code": "t = gets.to_i\n0.upto t-1 do\n t1, t2 = gets.split.map(&:to_i)\n if t1 == 1 or t2 == 1\n puts \"YES\"\n elsif t1 == 2 and t2 == 2\n puts \"YES\"\n else\n puts \"NO\"\n end\nend"}, {"source_code": "t = gets.to_i\nt.times do \n a ,b =gets.split.map(&:to_i)\n if a==1 or b==1 or a+b==4\n puts \"YES\"\n else \n puts\"NO\"\n end\nend"}, {"source_code": "1.upto(gets.chomp.to_i) do\n a, b = gets.chomp.split.map(&:to_i)\n if (a == 1 || b == 1 || (a <= 2 && b <= 2))\n puts 'YES'\n else\n puts 'NO'\n end\nend\n"}, {"source_code": "1.upto(gets.to_i) do\n n, m = gets.split(' ').map(&:to_i)\n\n puts (n == 1 || m == 1 || (n == 2 && m == 2)) ? 'YES' : 'NO'\nend\n"}], "negative_code": [], "src_uid": "55595ff38a08b80bc86cf1ebae6f55af"} {"nl": {"description": "Once Bob needed to find the second order statistics of a sequence of integer numbers. Lets choose each number from the sequence exactly once and sort them. The value on the second position is the second order statistics of the given sequence. In other words it is the smallest element strictly greater than the minimum. Help Bob solve this problem.", "input_spec": "The first input line contains integer n (1 ≤ n ≤ 100) — amount of numbers in the sequence. The second line contains n space-separated integer numbers — elements of the sequence. These numbers don't exceed 100 in absolute value.", "output_spec": "If the given sequence has the second order statistics, output this order statistics, otherwise output NO.", "sample_inputs": ["4\n1 2 2 -4", "5\n1 2 3 1 1"], "sample_outputs": ["1", "2"], "notes": null}, "positive_code": [{"source_code": "gets;puts gets.split.map(&:to_i).uniq.sort[1]||\"NO\"\n"}, {"source_code": "gets;puts gets.split.map(&:to_i).uniq.sort[1]||\"NO\"\n"}, {"source_code": "gets;puts gets.split.map(&:to_i).uniq.sort[1]||\"NO\""}, {"source_code": "gets;puts gets.split.map(&:to_i).uniq.sort[1]||\"NO\""}, {"source_code": "n=gets.chomp.to_i\nmemo=gets.chomp.split.map{|i| i.to_i}.sort!\nmin=memo[0]\nn.times do |i|\n if min!=memo[i]\n puts(memo[i])\n exit\n end\nend\nputs(\"NO\")\n"}, {"source_code": "gets;puts gets.split.map(&:to_i).uniq.sort[1]||\"NO\"\n"}, {"source_code": "gets\nputs gets.split.map(&:to_i).sort.uniq[1]||\"NO\"\n"}, {"source_code": "gets;puts gets.split.map(&:to_i).uniq.sort[1]||\"NO\"\n"}, {"source_code": "gets;puts gets.split.map(&:to_i).uniq.sort[1]||\"NO\""}, {"source_code": "gets\nputs (s=gets.split.map(&:to_i).uniq.sort[1])?s:\"NO\"\n"}, {"source_code": "n = gets.to_i\na = gets.split(' ')[0...n].map { |s| s.to_i }\nputs a.sort.uniq[1] || 'NO'"}, {"source_code": "gets;puts gets.split.map(&:to_i).uniq.sort[1]||\"NO\"\n"}, {"source_code": "num = gets.chomp\ndata = gets.chomp\n\narray = data.split(/\\s* \\s*/).uniq.sort{|a, b| a.to_i <=> b.to_i}\n\nif array.length > 1\n puts array[1]\nelse\n puts \"NO\"\nend\n"}, {"source_code": "amount = gets.to_i\nelements = gets.split(' ').map { |e| e.to_i }\nelements = elements.to_a.sort\nanswer = min = elements[0]\n(0...amount).each do |i|\n if elements[i] > min\n answer = elements[i]\n break\n end\nend\nif answer == min\n print \"NO\"\nelse\n print answer\nend"}, {"source_code": "gets\nk=gets.split.map(&:to_i).sort.uniq \nputs k.size<=1 ?\"NO\":k[1]"}, {"source_code": "n = gets.to_i\na = gets.split.collect{|i| i.to_i}\nans = nil\namin = a.min\na.each{|aa| ans = aa if aa > amin and (ans == nil or aa < ans)}\nif ans == nil\n puts \"NO\"\nelse\n puts ans\nend\n"}, {"source_code": "gets\nnums = gets.chomp.split.map(&:to_i)\n\nmin = nums.min\nres = nums.sort.detect{ |x| x > min }\nputs res.nil? ? \"NO\" : res"}, {"source_code": "gets;puts gets.split.map(&:to_i).uniq.sort[1]||\"NO\"\n"}, {"source_code": "gets.chomp # Skip the first line\nputs((seq = gets.chomp.split.map { |x| x.to_i }.uniq).size > 1 ? seq.sort[1] : 'NO')"}, {"source_code": "gets;puts gets.split.map(&:to_i).uniq.sort[1]||\"NO\"\n"}, {"source_code": "n = gets.to_i\n\na = gets.split.collect{|x| x.to_i}\n\nmin = a.min\na.delete(min)\n\nanswer = a.min\n\nif answer == nil\n\tputs 'NO'\nelse\n\tputs answer\nend"}, {"source_code": "gets;puts gets.split.map(&:to_i).uniq.sort[1]||\"NO\"\n"}, {"source_code": "STDIN.readlines.drop(1).join.split.map(&:to_i).uniq.sort.tap { |x| puts(x.length < 2 ? \"NO\" : x[1])}"}, {"source_code": "gets\nvalues = gets.split.map { |v| v.to_i }.sort.uniq\nresult = values.size >= 2 ? values[1] : \"NO\"\nputs result\n"}, {"source_code": "n = gets.to_i\n\ns = gets.split.map{|i| i.to_i}.sort\n\nif s[0] == s[-1] then\n result = \"NO\"\nelse\n i = 1\n while s[i] == s[0] && i < n do \n i += 1\n end\n result = s[i]\nend\n\nputs result"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split(/ /)\nan = Hash.new(n)\na.each{|aa| an[aa.to_i] = 0 }\nann = an.keys.sort\nif ann.count <= 1\n\tputs \"NO\"\nelse\n\tputs ann[1]\nend\n"}, {"source_code": "gets;puts gets.split.map(&:to_i).uniq.sort[1]||\"NO\""}, {"source_code": "gets;puts gets.split.map(&:to_i).uniq.sort[1]||\"NO\""}, {"source_code": "gets\na=gets.split.map(&:to_i).uniq.sort[1]\nputs a ?a:\"NO\""}, {"source_code": "gets\nputs gets.split.map(&:to_i).sort.uniq[1] || 'NO'\n"}, {"source_code": "gets\nx = gets.split.map(&:to_i).uniq.sort\nputs (x[1] || 'NO')"}, {"source_code": "gets\narr = gets.split.map(&:to_i).sort\nfor i in arr\n if not i == arr[0]\n puts i\n exit 0\n end\nend\nputs 'NO'\n\n"}, {"source_code": "n=gets.to_i\ni=gets.split.map(&:to_i)\ni.sort!\nm=i[0]\nans=\"NO\"\ni.each{|j|\n if j>m then\n ans=j.to_s\n break\n end\n}\n\nputs ans"}], "negative_code": [{"source_code": "n=gets.chomp.to_i\nmemo=gets.chomp.split.map{|i| i.to_i}.sort!\nmin=memo[0]\nn.times do |i|\n if min!=memo[i]\n puts(memo[i])\n exit\n end\nend\nputs(-1)\n"}, {"source_code": "amount = gets.to_i\nelements = gets.split(' ').sort.map { |e| e.to_i }\nmin = elements[0]\n(0...amount).each do |i|\n if elements[i] > min\n min = elements[i]\n break\n end\nend\nprint min"}, {"source_code": "amount = gets.to_i\nelements = gets.split(' ').sort.map { |e| e.to_i }\nanswer = min = elements[0]\n(0...amount).each do |i|\n if elements[i] > min\n answer = elements[i]\n break\n end\nend\nif answer == min\n print \"NO\"\nelse\n print answer\nend"}, {"source_code": "gets\nk=gets.split.map(&:to_i).sort.uniq \nputs k[1]"}, {"source_code": "gets\nnums = gets.chomp.split.map(&:to_i)\n\nmin = nums.min\nres = nums.detect{ |x| x > min }\nputs res.nil? ? \"NO\" : res"}, {"source_code": "puts gets.split.map(&:to_i).uniq.sort[1]||\"NO\""}, {"source_code": "gets\narr = gets.split.map(&:to_i).sort\nfor i in arr\n if not i == arr[0]\n puts i\n break\n end\nend\n\n"}, {"source_code": "gets\narr = gets.split.map(&:to_i).sort\nfor i in arr\n if not i == arr[0]\n puts i\n exit 0\n end\nend\nputs arr[0]\n\n"}, {"source_code": "gets\narr = gets.split.map(&:to_i).sort\nfor i in arr\n if not i == arr[0]\n puts i\n break\n end\nend\nputs arr[0]\n\n"}], "src_uid": "930be5ec102fbe062062aa23eac75187"} {"nl": {"description": "Little penguin Polo has an n × m matrix, consisting of integers. Let's index the matrix rows from 1 to n from top to bottom and let's index the columns from 1 to m from left to right. Let's represent the matrix element on the intersection of row i and column j as aij.In one move the penguin can add or subtract number d from some matrix element. Find the minimum number of moves needed to make all matrix elements equal. If the described plan is impossible to carry out, say so.", "input_spec": "The first line contains three integers n, m and d (1 ≤ n, m ≤ 100, 1 ≤ d ≤ 104) — the matrix sizes and the d parameter. Next n lines contain the matrix: the j-th integer in the i-th row is the matrix element aij (1 ≤ aij ≤ 104).", "output_spec": "In a single line print a single integer — the minimum number of moves the penguin needs to make all matrix elements equal. If that is impossible, print \"-1\" (without the quotes).", "sample_inputs": ["2 2 2\n2 4\n6 8", "1 2 7\n6 7"], "sample_outputs": ["4", "-1"], "notes": null}, "positive_code": [{"source_code": "n,m,d=gets.chomp.split.map(&:to_i)\na,flag=[],1\nn.times { |j| gets.chomp.split.map(&:to_i).each {|i| a< 1\n\tputs \"-1\"\nelse\n\tputs sum / d\nend\n\n\n# (0..(n-1)).each do |i|\n# \t(0..(m-1)).each do |j|\n# \t\tval = gcd(val,arr[i][j])\n# \tend\n# end\n"}, {"source_code": "n, m, d = gets.split.map(&:to_i)\nxs = n.times.map { gets.split.map(&:to_i) }.flatten.sort\nmod = xs[0] % d\nif xs.all? { |x| x % d == mod }\n median = xs[xs.size / 2]\n if median % d != mod\n median += median % d + mod\n end\n p xs.map { |x| (x - median).abs / d }.reduce(:+)\nelse\n p -1\nend"}, {"source_code": "n, m, d = gets.split(\" \").collect{|x| x.to_i}\n\narr = []\nn.times do\n gets.split(\" \").collect{|x| arr << x.to_i}\nend\n\nrem = arr[0] % d\ninv = 0\narr.each{|num|\n if num % d != rem\n inv = 1\n break\n end\n}\n\nif inv == 1\n puts -1\nelse\n arr.sort!\n target = arr[n * m / 2]\n\n mods = 0\n arr.each{|num|\n mods += (num - target).abs / d\n }\n\n puts mods\nend\n"}, {"source_code": "n,m,d = gets.strip.split.map(&:to_i)\n\nmatrix = []\nn.times do\n matrix += gets.strip.split.map(&:to_i)[0...m]\nend\n\ndef possible?(matrix, d)\n mod_d = matrix[0] % d\n matrix.each do |x|\n return false unless (x % d) == mod_d\n end\n true\nend\n\ndef trace(vars={})\n vars.each do |x,y|\n puts \"#{x} => #{y}\"\n end\nend\n\ndef get_count(matrix, c, d)\n matrix.map{|x| (c-x).abs/d}.inject :+\nend\n\nif possible?(matrix, d)\n matrix.sort!\n c = matrix[matrix.length/2]\n puts get_count(matrix, c, d)\nelse\n puts -1\nend\n\n\n"}], "negative_code": [{"source_code": "require \"Set\"\n\nn,m,d = gets.split(\" \").map(&:to_i)\narr = []\n\nn.times do \n\tarr << gets.split(\" \").map(&:to_i)\nend\narr.flatten!\narr.sort!\nmid = arr[(arr.length-1)/2 + 1]\n# puts \"#{mid}\"\nrem = Set.new\nsum = 0\narr.each do |a|\n\trem.add(a%d)\n\tsum += (a - mid).abs\nend\nif rem.length > 1\n\tputs \"-1\"\nelse\n\tputs sum / d\nend\n\n\n# (0..(n-1)).each do |i|\n# \t(0..(m-1)).each do |j|\n# \t\tval = gcd(val,arr[i][j])\n# \tend\n# end\n"}, {"source_code": "n, m, d = gets.split.map(&:to_i)\nmatrix = n.times.map { gets.split.map(&:to_i) }\nmod = matrix[0][0] % d\nif matrix.all? { |row| row.all? { |el| el % d == mod } }\n sum = matrix.map { |row| row.reduce(:+) }.reduce(:+)\n target = sum / (n * m)\n target += target % d + mod if target % d != mod\n p matrix.map { |row| row.map { |el| (el - target).abs / d }.reduce(:+) }.reduce(:+)\nelse\n p -1\nend"}, {"source_code": "n, m, d = gets.split.map(&:to_i)\nmatrix = n.times.map { gets.split.map(&:to_i) }\nif matrix.all? { |row| row.all? { |el| el % d == 0 } }\n sum = matrix.map { |row| row.reduce(:+) }.reduce(:+)\n target = sum / (n * m)\n target += target % d if target % d != 0\n p matrix.map { |row| row.map { |el| (el - target).abs / d }.reduce(:+) }.reduce(:+)\nelse\n p -1\nend"}, {"source_code": "n, m, d = gets.split.map(&:to_i)\nmatrix = n.times.map { gets.split.map(&:to_i) }\nif matrix.all? { |row| row.all? { |el| el % d == 0 } }\n sum = matrix.map { |row| row.reduce(:+) }.reduce(:+)\n avg = sum / (n * m)\n p matrix.map { |row| row.map { |el| (el - avg).abs / d }.reduce(:+) }.reduce(:+)\nelse\n p -1\nend"}, {"source_code": "n, m, d = gets.split(\" \").collect{|x| x.to_i}\n\narr = []\nn.times do\n gets.split(\" \").collect{|x| arr << x.to_i}\nend\n\nrem = arr[0] % d\nsum = 0\ninv = 0\narr.each{|num|\n if num % d != rem\n inv = 1\n break\n end\n \n sum += num\n}\n\nif inv == 1\n puts -1\nelse\n avg = sum.to_f / (n * m)\n ce = arr[0]\n diff = (arr[0] - avg).abs\n\n arr.each{|num|\n if (num - avg).abs < diff\n ce = num\n diff = (num - avg).abs\n end\n }\n\n mods = 0\n arr.each{|num|\n mods += (num - ce).abs / d\n }\n\n puts mods\nend\n"}, {"source_code": "n,m,d = gets.strip.split.map(&:to_i)\n\nmatrix = []\nn.times do\n matrix += gets.strip.split.map(&:to_i)[0...m]\nend\n\ndef possible?(matrix, d)\n mod_d = matrix[0] % d\n matrix.each do |x|\n return false unless (x % d) == mod_d\n end\n true\nend\n\ndef trace(vars={})\n vars.each do |x,y|\n puts \"#{x} => #{y}\"\n end\nend\n\nif possible?(matrix, d)\n mod_d = matrix[0] % d\n sum = matrix.inject(:+)\n avg = (( (sum.to_f / (n*m)).round - mod_d ) / d).round.to_i * d + mod_d\n\n #avg1 = ((avg - mod_d) / d).floor.to_i * d + mod_d\n #avg2 = ((avg - mod_d) / d).ceil.to_i * d + mod_d\n #trace avg1: avg1, avg2: avg2\n #\n #n1 = matrix.map{|x| (avg1 - x).abs / d}.inject(:+)\n #n2 = matrix.map{|x| (avg2 - x).abs / d}.inject(:+)\n #puts [n1,n2].min\n\n puts matrix.map{|x| (avg - x).abs / d}.inject(:+)\n\n\nelse\n puts -1\nend\n\n\n"}, {"source_code": "n,m,d = gets.strip.split.map(&:to_i)\n\nmatrix = []\nn.times do\n matrix += gets.strip.split.map(&:to_i)[0...m]\nend\n\ndef possible?(matrix, d)\n mod_d = matrix[0] % d\n matrix.each do |x|\n return false unless x % d == mod_d\n end\n true\nend\n\nif possible?(matrix, d)\n mod_d = matrix[0] % d\n sum = matrix.inject(:+)\n avg = sum / (n*m)\n avg1 = ((avg - mod_d) / d) * d + mod_d\n avg2 = ((avg - mod_d) / d + 1) * d + mod_d\n n1 = matrix.map{|x| (x - avg1).abs / d}.inject(:+)\n n2 = matrix.map{|x| (x - avg2).abs / d}.inject(:+)\n puts [n1,n2].min\nelse\n puts -1\nend\n\n\n"}, {"source_code": "n,m,d = gets.strip.split.map(&:to_i)\n\nmatrix = []\nn.times do\n matrix += gets.strip.split.map(&:to_i)[0...m]\nend\n\ndef possible?(matrix, d)\n mod_d = matrix[0] % d\n matrix.each do |x|\n return false unless (x % d) == mod_d\n end\n true\nend\n\ndef trace(vars={})\n vars.each do |x,y|\n puts \"#{x} => #{y}\"\n end\nend\n\ndef get_count(matrix, c)\n matrix.map{|x| (c-x).abs}.inject :+\nend\n\nif possible?(matrix, d)\n matrix.sort!\n min = matrix.first\n max = matrix.last\n c = min\n n = nil\n while c <= max\n count = get_count(matrix, c)\n n = count if n.nil? || count < n\n c += d\n end\n puts n\nelse\n puts -1\nend\n\n\n"}, {"source_code": "n,m,d = gets.strip.split.map(&:to_i)\n\nmatrix = []\nn.times do\n matrix += gets.strip.split.map(&:to_i)[0...m]\nend\n\ndef possible?(matrix, d)\n mod_d = matrix[0] % d\n matrix.each do |x|\n return false unless x % d == mod_d\n end\n true\nend\n\nif possible?(matrix, d)\n sum = matrix.inject(:+)\n avg = sum.to_f / (n*m)\n avg1 = (avg / d).to_i * d\n avg2 = ((avg / d).to_i + 1) * d\n n1 = matrix.map{|x| (x - avg1).abs / d}.inject(:+)\n n2 = matrix.map{|x| (x - avg2).abs / d}.inject(:+)\n puts [n1,n2].min\nelse\n puts -1\nend\n\n\n"}, {"source_code": "n,m,d = gets.strip.split.map(&:to_i)\n\nmatrix = []\nn.times do\n matrix += gets.strip.split.map(&:to_i)[0...m]\nend\n\n$stderr.puts n,m,d, matrix.inspect\n\ndef possible?(matrix, d)\n mod_d = matrix[0] % d\n matrix.each do |x|\n return false unless (x % d) == mod_d\n end\n true\nend\n\nif possible?(matrix, d)\n mod_d = matrix[0] % d\n sum = matrix.inject(:+)\n avg = sum.to_f / (n*m)\n\n avg1 = ((avg - mod_d) / d).to_i * d + mod_d\n avg2 = (((avg - mod_d) / d).to_i + 1) * d + mod_d\n n1 = matrix.map{|x| (x - avg1).abs / d}.inject(:+)\n n2 = matrix.map{|x| (x - avg2).abs / d}.inject(:+)\n puts [n1,n2].min\nelse\n puts -1\nend\n\n\n"}], "src_uid": "3488bb49de69c0c17ea0439e71b1e6ae"} {"nl": {"description": "Bizon the Champion isn't just attentive, he also is very hardworking.Bizon the Champion decided to paint his old fence his favorite color, orange. The fence is represented as n vertical planks, put in a row. Adjacent planks have no gap between them. The planks are numbered from the left to the right starting from one, the i-th plank has the width of 1 meter and the height of ai meters.Bizon the Champion bought a brush in the shop, the brush's width is 1 meter. He can make vertical and horizontal strokes with the brush. During a stroke the brush's full surface must touch the fence at all the time (see the samples for the better understanding). What minimum number of strokes should Bizon the Champion do to fully paint the fence? Note that you are allowed to paint the same area of the fence multiple times.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 5000) — the number of fence planks. The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109).", "output_spec": "Print a single integer — the minimum number of strokes needed to paint the whole fence.", "sample_inputs": ["5\n2 2 1 2 1", "2\n2 2", "1\n5"], "sample_outputs": ["3", "2", "1"], "notes": "NoteIn the first sample you need to paint the fence in three strokes with the brush: the first stroke goes on height 1 horizontally along all the planks. The second stroke goes on height 2 horizontally and paints the first and second planks and the third stroke (it can be horizontal and vertical) finishes painting the fourth plank.In the second sample you can paint the fence with two strokes, either two horizontal or two vertical strokes.In the third sample there is only one plank that can be painted using a single vertical stroke."}, "positive_code": [{"source_code": "\ndef b(l,r,v=1)\n\tif l==r \n\t\t$t[v]=l \n\t\treturn 0\n\tend\n\treturn 0 if l>r\n\tm=(l+r)/2\n\tb(l,m,v*2)\n\tb(m+1,r,v*2+1)\n\tif $a[$t[v*2]]<$a[$t[v*2+1]]\n\t\t$t[v]=$t[v*2]\n\telse\n\t\t$t[v]=$t[v*2+1]\n\tend\nend\ndef g(l,r,tl=0,tr=$n-1,v=1)\n\treturn $n if tl>r || tr=l && tr<=r\n\tm=(tl+tr)/2\n\ta=g(l,r,tl,m,v*2)\n\tb=g(l,r,m+1,tr,v*2+1)\n\treturn ($a[a]<$a[b]) ? a : b \nend\ndef z(l,r,h)\n\treturn 0 if l>r\n\tm=g(l,r)\n\treturn [r-l+1,z(l,m-1,$a[m])+z(m+1,r,$a[m])+$a[m]-h].min\nend\nn=gets.to_i\n$n=2\nwhile $n y.min}\n\t\t\t@seg_tree[n] = Info.new(left, right, min.min, min.index)\n\t\tend\n\n\t\t@seg_tree[n]\n\tend\n\n\tdef seg_min(l, r, n = 1)\n\t\tmid = (@seg_tree[n].left + @seg_tree[n].right) / 2\n\n\t\tif l <= @seg_tree[n].left and @seg_tree[n].right <= r\n\t\t\t@seg_tree[n]\n\t\telsif mid >= r\n\t\t\tseg_min(l, r, n*2)\n\t\telsif mid < l\n\t\t\tseg_min(l, r, n*2 + 1)\n\t\telse\n\t\t\t[seg_min(l, r, n*2), seg_min(l, r, n*2 + 1)].min{|x, y| x.min <=> y.min}\n\t\tend\n\tend\nend\n\nn = gets.chomp.to_i\na = gets.chomp.split.map{|i| i.to_i}\na.create_seg\n# puts a.seg_tree\n# puts a.seg_min(1, 2)\n\ndef sol(arr, l, r, base)\n\t# print arr\n\treturn 0 if r == l && base == arr[r]\n\treturn r - l + 1 if r - l + 1 <= 1\n\tm = arr.seg_min(l, r).min\n\tmid = arr.seg_min(l, r).index\n\tans = [r - l + 1, sol(arr, l, mid - 1, m) + sol(arr, mid + 1, r, m) + (m - base)].min\n\t# puts \"r - l + 1 = #{r - l + 1}, sol(arr, #{l}, #{mid - 1}, #{m}) = #{sol(arr, l, mid - 1, m)}, sol(arr, #{mid + 1}, #{r}, #{m}) = #{sol(arr, mid + 1, r, m)}\"\n\t# puts \"in (#{l}, #{r}), min = #{m}, base = #{base}, ans = #{ans}\"\n\tans\nend\n\nputs sol(a, 0, a.size - 1, 0)\n"}], "negative_code": [{"source_code": "n = gets.chomp.to_i\na = gets.chomp.split.map{|i| i.to_i}\n\ndef split0(arr)\n\tslice = []\n\tans = []\n\tarr.each do |i|\n\t\tif i == 0 && (not slice.empty?)\n\t\t\tans << slice\n\t\t\tslice = []\n\t\telsif i != 0\n\t\t\tslice << i\n\t\tend\n\tend\n\tif (not slice.empty?)\n\t\tans << slice\n\tend\n\tans\nend\n\n\ndef sol(arr)\n\treturn arr.size if arr.size <= 1 \n\tnarr = arr.map{|i| i - arr.min}\n\t\n\treturn [arr.size, split0(narr).map{|i| sol i}.inject(0, :+) + 1].min\nend\n\nputs sol(a)\n"}, {"source_code": "class Array\n\tattr_accessor :seg_tree\n\tInfo = Struct.new(:left, :right, :min, :index)\n\n\tdef create_seg(left = 0, right = size - 1, n = 1)\n\t\t@seg_tree = [] if @seg_tree == nil\n\n\t\tif right == left\n\t\t\t@seg_tree[n] = Info.new(left, left, slice(left), left)\n\t\telse\n\t\t\tmid = (left + right) / 2\n\t\t\tmin = [create_seg(left, mid, 2*n), create_seg(mid + 1, right, 2*n+1)].min{|x, y| x.min <=> y.min}\n\t\t\t@seg_tree[n] = Info.new(left, right, min.min, min.index)\n\t\tend\n\n\t\t@seg_tree[n]\n\tend\n\n\tdef seg_min(l, r, n = 1)\n\t\tmid = (@seg_tree[n].left + @seg_tree[n].right) / 2\n\n\t\tif l <= @seg_tree[n].left && r >= @seg_tree[n].right\n\t\t\t@seg_tree[n]\n\t\telsif mid >= r\n\t\t\tseg_min(l, r, n*2)\n\t\telsif mid <= l\n\t\t\tseg_min(l, r, n*2 + 1)\n\t\telse\n\t\t\t[seg_min(l, r, n*2), seg_min(l, r, n*2 + 1)].min{|x, y| x.min <=> y.min}\n\t\tend\n\tend\nend\n\nn = gets.chomp.to_i\na = gets.chomp.split.map{|i| i.to_i}\na.create_seg\n\ndef sol(arr, l, r, base)\n\t# print arr\n\treturn 0 if r == l && base == arr[r]\n\treturn r - l + 1 if r - l + 1 <= 1\n\tm = arr.seg_min(l, r).min\n\tmid = arr.seg_min(l, r).index\n\treturn [r - l + 1, sol(arr, l, mid - 1, m) + sol(arr, mid + 1, r, m) + (m - base)].min\nend\n\nputs sol(a, 0, a.size - 1, 0)\n"}], "src_uid": "ddab0e510f9aceb2fbf75e26d27df166"} {"nl": {"description": "There are n cows playing poker at a table. For the current betting phase, each player's status is either \"ALLIN\", \"IN\", or \"FOLDED\", and does not change throughout the phase. To increase the suspense, a player whose current status is not \"FOLDED\" may show his/her hand to the table. However, so as not to affect any betting decisions, he/she may only do so if all other players have a status of either \"ALLIN\" or \"FOLDED\". The player's own status may be either \"ALLIN\" or \"IN\".Find the number of cows that can currently show their hands without affecting any betting decisions.", "input_spec": "The first line contains a single integer, n (2 ≤ n ≤ 2·105). The second line contains n characters, each either \"A\", \"I\", or \"F\". The i-th character is \"A\" if the i-th player's status is \"ALLIN\", \"I\" if the i-th player's status is \"IN\", or \"F\" if the i-th player's status is \"FOLDED\".", "output_spec": "The first line should contain a single integer denoting the number of players that can currently show their hands.", "sample_inputs": ["6\nAFFAAA", "3\nAFI"], "sample_outputs": ["4", "1"], "notes": "NoteIn the first sample, cows 1, 4, 5, and 6 can show their hands. In the second sample, only cow 3 can show her hand."}, "positive_code": [{"source_code": "n=gets\ntext= gets\nI=text.scan('I').length\nF=text.scan('F').length\nA=text.scan('A').length\n\nif I>1\n puts \"0\"\nelsif I==1\n puts \"1\"\nelse\n puts A.to_s\nend"}, {"source_code": "n = gets.to_i\ns = gets.strip\n\nac = 0\nic = 0\n\nfor i in 0...n\n if s[i] == 'A' then ac += 1 end\n if s[i] == 'I' then ic += 1 end\nend\n\nif ic > 1 then puts 0\nelsif ic == 1 then puts 1\nelse puts ac end"}, {"source_code": "#!/usr/bin/ruby1.9\n\nn = STDIN.readline.to_i\ns = STDIN.readline.strip.downcase\n\ncounts = {'a' => 0, 'i' => 0, 'f' => 0}\n\nn.times do |i|\n\tcounts[s[i]] += 1\nend\n\nif counts['i'] > 1\n\tanswer = 0\nelsif counts['i'] == 1\n\tanswer = 1\nelse\n\tanswer = counts['a']\nend\n\nputs answer\n"}, {"source_code": "x = gets.to_i\nps = gets\nans = 0\nss={'A'=>0,'I'=>0,'F'=>0}\nx.to_i.times{|i|\n ss[ps[i]]+=1\n}\nx.to_i.times{|i|\n ss[ps[i]]-=1\n ans += 1 if ps[i] != 'F' && ss['I'] == 0\n ss[ps[i]]+=1\n}\nputs ans\n"}, {"source_code": "n=gets.to_i\ns=gets.chomp\ni=s.count(?I)\np i>1 ? 0 : i>0 ? 1 : n-s.count(?F)"}, {"source_code": "n = gets.to_i\ns = gets.chomp\nplayers = s.count('I')\n\nif players == 1\n puts \"1\"\nelse\n if players == 0\n puts s.count('A')\n else\n puts \"0\"\n end\nend\n"}, {"source_code": "n = gets.to_i\ns = gets.chomp\nplayers = s.count('I')\n\nif players == 1\n puts \"1\"\nelse\n if players == 0\n puts s.count('A')\n else\n puts \"0\"\n end\nend"}, {"source_code": "gets;a=gets;a.count('I')==1 ?(puts 1):(a.count('I')==0 ?(puts a.count('A')):(puts 0))"}, {"source_code": "n = gets.to_i\ns = gets.chomp\n\nl = s.size\na = 0\ni = 0\nf = 0\n(0...l).each { |p|\n if s[p].downcase == \"a\" \n a+=1\n end\n if s[p].downcase == \"f\"\n f+=1\n end\n if s[p].downcase == \"i\" \n i+=1\n end\n \n}\n\nif i == 0\n puts a\nend\n\nif i == 1\n puts 1\nend\n\nif i > 1\n puts 0\nend"}, {"source_code": "n = gets.to_i\ns = gets\n\na = i = 0\nn.times do |idx|\n if s[idx] == \"A\"\n a += 1\n elsif s[idx] == \"I\"\n i += 1\n end\nend\n\nif i > 1\n puts 0\nelsif i == 0\n puts a\nelse\n puts 1\nend"}, {"source_code": "gets # Skip the first line\ns = gets.chomp\nplayers_in = s.count('I')\nputs(players_in == 1 ? 1 : players_in == 0 ? s.count('A') : 0)"}, {"source_code": "n = gets; a = gets\na.count('I')==1? (puts 1) : (a.count('I')==0? (puts a.count('A') ):(puts 0))"}], "negative_code": [{"source_code": "#!/usr/bin/ruby1.9\n\nn = STDIN.readline.to_i\ns = STDIN.readline.strip.downcase\n\ncounts = {'a' => 0, 'i' => 0, 'f' => 0}\n\nn.times do |i|\n\tcounts[s[i]] += 1\nend\n\nif counts['i'] == 1\n\tanswer = 1\nelse\n\tanswer = counts['a']\nend\n\nputs answer\n"}, {"source_code": "n=gets.to_i\ns=gets.chomp\np s.count(?I)>1 ? 0 : n-s.count(?F)"}], "src_uid": "5e4defe52832cc0d36e7ea5d9f86f895"} {"nl": {"description": "Petya loves football very much, especially when his parents aren't home. Each morning he comes to the yard, gathers his friends and they play all day. From time to time they have a break to have some food or do some chores (for example, water the flowers).The key in football is to divide into teams fairly before the game begins. There are n boys playing football in the yard (including Petya), each boy's football playing skill is expressed with a non-negative characteristic ai (the larger it is, the better the boy plays). Let's denote the number of players in the first team as x, the number of players in the second team as y, the individual numbers of boys who play for the first team as pi and the individual numbers of boys who play for the second team as qi. Division n boys into two teams is considered fair if three conditions are fulfilled: Each boy plays for exactly one team (x + y = n). The sizes of teams differ in no more than one (|x - y| ≤ 1). The total football playing skills for two teams differ in no more than by the value of skill the best player in the yard has. More formally: Your task is to help guys divide into two teams fairly. It is guaranteed that a fair division into two teams always exists.", "input_spec": "The first line contains the only integer n (2 ≤ n ≤ 105) which represents the number of guys in the yard. The next line contains n positive space-separated integers, ai (1 ≤ ai ≤ 104), the i-th number represents the i-th boy's playing skills. ", "output_spec": "On the first line print an integer x — the number of boys playing for the first team. On the second line print x integers — the individual numbers of boys playing for the first team. On the third line print an integer y — the number of boys playing for the second team, on the fourth line print y integers — the individual numbers of boys playing for the second team. Don't forget that you should fulfil all three conditions: x + y = n, |x - y| ≤ 1, and the condition that limits the total skills. If there are multiple ways to solve the problem, print any of them. The boys are numbered starting from one in the order in which their skills are given in the input data. You are allowed to print individual numbers of boys who belong to the same team in any order.", "sample_inputs": ["3\n1 2 1", "5\n2 3 3 1 1"], "sample_outputs": ["2\n1 2 \n1\n3", "3\n4 1 3 \n2\n5 2"], "notes": "NoteLet's consider the first sample test. There we send the first and the second boy to the first team and the third boy to the second team. Let's check all three conditions of a fair division. The first limitation is fulfilled (all boys play), the second limitation on the sizes of groups (|2 - 1| = 1 ≤ 1) is fulfilled, the third limitation on the difference in skills ((2 + 1) - (1) = 2 ≤ 2) is fulfilled."}, "positive_code": [{"source_code": "n = gets.chomp.to_i\na = gets.chomp.split(/ /).map!{|x| x.to_i}\nb = []\nidx = 1\na.each{|aa|\n b << [aa, idx]\n idx += 1\n}\nb = b.sort{|b2,b1|\n b1[0] <=> b2[0]\n}\nc1 = []\nc2 = []\nc1s = 0\nc2s = 0\nb.each{|bb|\n if c1.length > c2.length\n c2 << bb[1]\n c2s += bb[0]\n elsif c2.length > c1.length\n c1 << bb[1]\n c1s += bb[0]\n else\n if c1s > c2s\n c2 << bb[1]\n c2s += bb[0]\n else\n c1 << bb[1]\n c1s += bb[0]\n end\n end\n}\nputs c1.length\nputs c1.join(\" \")\nputs c2.length\nputs c2.join(\" \")"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i).zip(1.upto(n)).sort.reverse\nl, r, x, y = 0, 0, [], []\n\nif n.odd?\n b = a.pop\n l += b[0]\n x << b[1]\nend\n\n(n / 2).times do\n b = a.pop\n c = a.pop\n b, c = c, b if l <= r\n l += b[0]\n x << b[1]\n r += c[0]\n y << c[1]\nend\n\np x.size\nputs x * ' '\np y.size\nputs y * ' '\n"}, {"source_code": "n=gets.chomp.to_i\nplayers=[]\narr=gets.chomp.split\narr.each_index{|i| players.push [i+1,arr[i].to_i]}\nplayers.sort_by!{|a| a[1]}\ndif=0\ni=2\ns_i=0\nl_i=players.size-1\na=[]\nb=[]\nwhile i <= n\n if dif >0\n a.push players[s_i][0]\n dif =dif+players[s_i][1]\n s_i=s_i+1\n else\n a.push players[l_i][0]\n dif =dif+players[l_i][1]\n l_i=l_i-1\n end\n if dif <0\n b.push players[s_i][0]\n dif =dif-players[s_i][1]\n s_i=s_i+1\n else\n dif =dif-players[l_i][1]\n b.push players[l_i][0]\n l_i=l_i-1\n end\ni=i+2\nend\nif n%2!=0\n if dif >0\n b.push players[l_i][0]\n else\n a.push players[l_i][0]\n end\nend\n#p dif\nputs a.size,a.to_s.delete('[').delete(']').gsub(\",\",\"\"),b.size,b.to_s.delete('[').delete(']').gsub(\",\",\"\")\n\n"}], "negative_code": [{"source_code": "n=gets.chomp.to_i\nplayers=[]\narr=gets.chomp.split\narr.each_index{|i| players.push [i+1,arr[i].to_i]}\nplayers.sort_by!{|a| a[1]}\ndif=0\ni=2\ns_i=0\nl_i=players.size-1\na=[]\nb=[]\nwhile i <= n\n if dif >0\n a.push players[s_i][0]\n dif =dif+players[s_i][1]\n s_i=s_i+1\n else\n a.push players[l_i][0]\n dif =dif+players[l_i][1]\n l_i=l_i-1\n end\n if dif <0\n b.push players[s_i][0]\n dif =dif+players[s_i][1]\n s_i=s_i+1\n else\n dif =dif+players[l_i][1]\n b.push players[l_i][0]\n l_i=l_i-1\n end\ni=i+2\nend\nif n%2!=0\n if dif >0\n b.push players[l_i][0]\n else\n a.push players[l_i][0]\n end\nend\nputs a.size,a.to_s.delete('[').delete(']').gsub(\",\",\"\"),b.size,b.to_s.delete('[').delete(']').gsub(\",\",\"\")\n\n"}], "src_uid": "0937a7e2f912fc094cc4275fd47cd457"} {"nl": {"description": "Vasya's bicycle chain drive consists of two parts: n stars are attached to the pedal axle, m stars are attached to the rear wheel axle. The chain helps to rotate the rear wheel by transmitting the pedal rotation.We know that the i-th star on the pedal axle has ai (0 < a1 < a2 < ... < an) teeth, and the j-th star on the rear wheel axle has bj (0 < b1 < b2 < ... < bm) teeth. Any pair (i, j) (1 ≤ i ≤ n; 1 ≤ j ≤ m) is called a gear and sets the indexes of stars to which the chain is currently attached. Gear (i, j) has a gear ratio, equal to the value .Since Vasya likes integers, he wants to find such gears (i, j), that their ratios are integers. On the other hand, Vasya likes fast driving, so among all \"integer\" gears (i, j) he wants to choose a gear with the maximum ratio. Help him to find the number of such gears.In the problem, fraction denotes division in real numbers, that is, no rounding is performed.", "input_spec": "The first input line contains integer n (1 ≤ n ≤ 50) — the number of stars on the bicycle's pedal axle. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 104) in the order of strict increasing. The third input line contains integer m (1 ≤ m ≤ 50) — the number of stars on the rear wheel axle. The fourth line contains m integers b1, b2, ..., bm (1 ≤ bi ≤ 104) in the order of strict increasing. It is guaranteed that there exists at least one gear (i, j), that its gear ratio is an integer. The numbers on the lines are separated by spaces.", "output_spec": "Print the number of \"integer\" gears with the maximum ratio among all \"integer\" gears.", "sample_inputs": ["2\n4 5\n3\n12 13 15", "4\n1 2 3 4\n5\n10 11 12 13 14"], "sample_outputs": ["2", "1"], "notes": "NoteIn the first sample the maximum \"integer\" gear ratio equals 3. There are two gears that have such gear ratio. For one of them a1 = 4, b1 = 12, and for the other a2 = 5, b3 = 15."}, "positive_code": [{"source_code": "gets\na = gets.split.map &:to_i\ngets\nb = gets.split.map &:to_i\n\nc = b.product(a).map{ |bi,aj| bi*1.0/aj }.select{ |i| i%1 == 0 }\nputs c.count c.max\n"}, {"source_code": "gets;a=gets.split.map &:to_i;gets;b=gets.split.map &:to_i;c=a.product(b).map{|x,y|y%x==0?(y/x):0};p c.count c.max\n"}, {"source_code": "gets;a=gets.split.map &:to_i;gets;b=gets.split.map &:to_i;c=a.product(b).map{|x,y|y%x==0?(y/x):0};p c.count c.max\n"}, {"source_code": "n = gets.to_i\ngears1 = gets.split(' ').map{|x| x.to_i}\nm = gets.to_i\ngears2 = gets.split(' ').map{|x| x.to_i}\n\nbest = -1\ncount = 0\n\nfor i in gears1\n for j in gears2\n if j % i == 0 && j / i > best\n best = j / i\n count = 1\n elsif j % i == 0 && j / i == best\n count += 1\n end\n end\nend\n\nprint \"#{count}\\n\""}, {"source_code": "gets;a=gets.split.map &:to_i;gets;b=gets.split.map &:to_i;c=a.product(b).map{|x,y|y%x==0?(y/x):0};p c.count c.max"}, {"source_code": "n, a, m, b = readlines.map(&:chomp).map(&:split).map { |x| x.map!(&:to_i) }\ns = a.product(b).map { |x| x[1]%x[0] == 0 ? x[1]/x[0] : nil }.compact\nputs s.count { |x| x == s.max }"}, {"source_code": "gets;a=gets.split.map &:to_i;gets;b=gets.split.map &:to_i;c=a.product(b).map{|x,y|y%x==0?(y/x):0};p c.count c.max\n"}, {"source_code": "n = gets.to_i\na = gets.split.map! {|x| x.to_i }\nm = gets.to_i\nb = gets.split.map! {|x| x.to_i }\nans = []\nfor x in a do\n for y in b do\n ans << y / x if y % x == 0\n end\nend\nmx = -1\nans.each do |x|\n mx = x if x > mx\nend\ncnt = 0\nans.each do |x|\n cnt += 1 if x == mx\nend\nputs cnt"}, {"source_code": "gets;a=gets.split.map &:to_i;gets;b=gets.split.map &:to_i;c=a.product(b).map{|x,y|y%x==0?(y/x):0};p c.count c.max\n"}, {"source_code": "a=gets.chomp.to_i\nb=gets.chomp.split(\" \")\n0.upto(a-1) {|i| b[i]=b[i].to_i}\nc=gets.chomp.to_i\nd=gets.chomp.split(\" \")\n0.upto(c-1) {|i| d[i]=d[i].to_i}\n\nnum=[]\nd.each do |i|\nb.each do |j|\nif i%j==0\nnum << i/j\nend\nend\nend\nnum.sort!\nmax=num[num.length-1]\nans=num.count(max)\nputs \"#{ans}\""}, {"source_code": "gets\na=gets.split.map(&:to_i)\ngets\nb=gets.split.map(&:to_i)\nc = []\na.each do |aa|\n b.each do |bb|\n c << bb / aa if bb % aa == 0\n end\nend\np c.count c.max\n\n"}, {"source_code": "require 'set'\nrequire 'rational'\nrequire 'mathn'\ninclude Math\n$stdin = File.open(\"./input.txt\", \"r\") if ENV[\"USER\"] == \"new\"\n# ====================================\n\nn = gets.to_i\nfront = gets.split.map &:to_i\nm = gets.to_i\nback = gets.split.map &:to_i\n\nmax_value = 0\nmax_count = 0\n\nfor front_x in front do\n for back_x in back do\n next if back_x % front_x != 0\n next if back_x / front_x < max_value\n \n if back_x / front_x == max_value then\n max_count+=1\n else\n max_value = back_x / front_x\n max_count = 1\n end\n end\nend\n\nprint max_count"}, {"source_code": "require 'set'\nrequire 'rational'\nrequire 'mathn'\ninclude Math\n$stdin = File.open(\"./input.txt\", \"r\") if ENV[\"USER\"] == \"new\"\n# ====================================\n\nn = gets.to_i\nfront = gets.split.map &:to_i\nm = gets.to_i\nback = gets.split.map &:to_i\n\nhash = Hash.new( 0 )\n\nfor front_x in front do\n for back_x in back do\n next if back_x % front_x != 0\n \n hash[back_x / front_x]+=1\n end\nend\n\nprint hash.to_a.max[1]"}, {"source_code": "require 'set'\nrequire 'rational'\nrequire 'mathn'\ninclude Math\n$stdin = File.open(\"./input.txt\", \"r\") if ENV[\"USER\"] == \"new\"\n# ====================================\n\ngets\nfront = gets.split.map &:to_i\ngets\nback = gets.split.map &:to_i\n\nhash = Hash.new( 0 )\n\nfront.each{|v1|\n back.each{|v2|\n next if v2 % v1 != 0\n \n hash[v2 / v1]+=1\n }\n}\n\nprint hash.to_a.max[1]"}, {"source_code": "#!/usr/bin/ruby\nn = gets.chomp.to_i\na = gets.chomp.split(' ').map{|x| x.to_i}\nm = gets.chomp.to_i\nb = gets.chomp.split(' ').map{|x| x.to_i}\nmaxrat = 0\nints = 0\nfor i in 0..n-1\n\tfor j in 0..m-1\n\t\tif b[j]%a[i] == 0\n\t\t\tif b[j]/a[i] > maxrat\n\t\t\t\tints = 1\n\t\t\t\tmaxrat = b[j]/a[i]\n\t\t\telsif b[j]/a[i] == maxrat\n\t\t\t\tints +=1\n\t\t\tend\n\t\tend\n\tend\nend\nputs ints\n"}, {"source_code": "gets;a=gets.split.map &:to_i;gets;b=gets.split.map &:to_i;c=a.product(b).map{|x,y|y%x==0?(y/x):0};p c.count c.max"}, {"source_code": "gets;a=gets.split.map &:to_i;gets;b=gets.split.map &:to_i;c=a.product(b).map{|x,y|y%x==0?(y/x):0};p c.count c.max\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nm = gets.to_i\nb = gets.split.map(&:to_i)\n\nvalues = []\nb.each do |i|\n a.each do |j|\n values << i/j if i%j == 0\n end\nend\n\nvalues = values.sort_by{|el| -el}\nputs values.count(values[0])\n"}, {"source_code": "gets;a=gets.split.map &:to_i;gets;b=gets.split.map &:to_i;c=a.product(b).map{|x,y|y%x==0?(y/x):0};p c.count c.max\n"}, {"source_code": "n = Integer(gets.chomp)\na = gets.chomp.split.map { |x| x.to_i }\nm = Integer(gets.chomp)\nb = gets.chomp.split.map { |x| x.to_i }\n\nmax = 0\nfor i in (m - 1).downto(0)\n for j in 0...n\n if b[i] % a[j] == 0\n r = b[i] / a[j]\n max = r if r > max\n end\n end\nend\n\nc = 0\nfor i in 0...m\n for j in 0...n\n c += 1 if b[i] % a[j] == 0 && b[i] / a[j] == max\n end\nend\nputs c"}, {"source_code": "gets\na = gets.split.map!(&:to_i)\ngets\nb = gets.split.map!(&:to_i)\n\nmax = 0\ncount = 0\n\na.each do |ai|\n b.each do |bj|\n if bj % ai == 0\n r = bj / ai\n if r == max\n count += 1\n elsif r > max\n max = r\n count = 1\n end\n end\n end\nend\n\np count\n"}, {"source_code": "gets\na = gets.split.map!(&:to_i)\ngets\nb = gets.split.map!(&:to_i)\n\nc = a.product(b).map!{|x,y| y%x == 0 ? y/x : 0}\n\np c.count(c.max)\n"}, {"source_code": "gets;a=gets.split.map &:to_i;gets;b=gets.split.map &:to_i;c=a.product(b).map{|x,y|y%x==0?(y/x):0};p c.count c.max\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nm = gets.to_i\nb = gets.split.map(&:to_i)\n\nvalues = []\nb.each do |i|\n a.each do |j|\n values << i/j if i%j == 0\n end\nend\n\nvalues = values.sort_by{|el| -el}\nputs values.count(values[0])\n"}, {"source_code": "gets;a=gets.split.map &:to_i;gets;b=gets.split.map &:to_i;c=a.product(b).map{|x,y|y%x==0?(y/x):0};p c.count c.max\n"}, {"source_code": "n = gets.to_i\nnn = []\nnmas = gets.split\nm = gets.to_i\nmm = []\nmmas = gets.split\ni = 0\nwhile i max then \n result = 1\n max = k\n end\n end\n j += 1\n end\n i += 1\nend\nprint \"#{result}\\n\"\n"}], "negative_code": [{"source_code": "n = Integer(gets.chomp)\na = gets.chomp.split.map { |x| x.to_i }\nm = Integer(gets.chomp)\nb = gets.chomp.split.map { |x| x.to_i }\n\nmax = 0\ncatch (:done) do\n for i in (m - 1).downto(0)\n for j in 0...n\n if b[i] % a[j] == 0\n max = b[i] / a[j]\n throw :done\n end\n end\n end\nend\n\nc = 0\nfor i in 0...m\n for j in 0...n\n c += 1 if b[i] % a[j] == 0 && b[i] / a[j] == max\n end\nend\nputs c"}], "src_uid": "102667eaa3aee012fef70f4192464674"} {"nl": {"description": "Polycarp has to solve exactly $$$n$$$ problems to improve his programming skill before an important programming competition. But this competition will be held very soon, most precisely, it will start in $$$k$$$ days. It means that Polycarp has exactly $$$k$$$ days for training!Polycarp doesn't want to procrastinate, so he wants to solve at least one problem during each of $$$k$$$ days. He also doesn't want to overwork, so if he solves $$$x$$$ problems during some day, he should solve no more than $$$2x$$$ problems during the next day. And, at last, he wants to improve his skill, so if he solves $$$x$$$ problems during some day, he should solve at least $$$x+1$$$ problem during the next day.More formally: let $$$[a_1, a_2, \\dots, a_k]$$$ be the array of numbers of problems solved by Polycarp. The $$$i$$$-th element of this array is the number of problems Polycarp solves during the $$$i$$$-th day of his training. Then the following conditions must be satisfied: sum of all $$$a_i$$$ for $$$i$$$ from $$$1$$$ to $$$k$$$ should be $$$n$$$; $$$a_i$$$ should be greater than zero for each $$$i$$$ from $$$1$$$ to $$$k$$$; the condition $$$a_i < a_{i + 1} \\le 2 a_i$$$ should be satisfied for each $$$i$$$ from $$$1$$$ to $$$k-1$$$. Your problem is to find any array $$$a$$$ of length $$$k$$$ satisfying the conditions above or say that it is impossible to do it.", "input_spec": "The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le n \\le 10^9, 1 \\le k \\le 10^5$$$) — the number of problems Polycarp wants to solve and the number of days Polycarp wants to train.", "output_spec": "If it is impossible to find any array $$$a$$$ of length $$$k$$$ satisfying Polycarp's rules of training, print \"NO\" in the first line. Otherwise print \"YES\" in the first line, then print $$$k$$$ integers $$$a_1, a_2, \\dots, a_k$$$ in the second line, where $$$a_i$$$ should be the number of problems Polycarp should solve during the $$$i$$$-th day. If there are multiple answers, you can print any.", "sample_inputs": ["26 6", "8 3", "1 1", "9 4"], "sample_outputs": ["YES\n1 2 4 5 6 8", "NO", "YES\n1", "NO"], "notes": null}, "positive_code": [{"source_code": "n,k = gets.split.map(&:to_i)\nans = Array.new(k){|i|i+1}\nn -= k*(k+1)/2\nif n < 0\n puts \"NO\"\n exit\nend\nk.times do |i|\n ans[i] += n/k\nend\nn %= k\n(k-1).downto(1) do |i|\n m = [ans[i-1]*2-ans[i],n].min\n m = [ans[i+1]-1,m].min if i+1 < k\n ans[i] += m\n n -= m\nend\n(k-1).times do |i|\n if ans[i]*2 < ans[i+1]\n puts \"NO\"\n exit\n end\nend\nif n > 0\n puts \"NO\"\n exit\nend\nputs \"YES\",ans.join(\" \")"}, {"source_code": "n,k = gets.split.map(&:to_i)\nans = Array.new(k){|i|i+1}\nn -= k*(k+1)/2\nif n < 0\n puts \"NO\"\n exit\nend\nk.times do |i|\n ans[i] += n/k\nend\nn %= k\n(k-1).downto(1) do |i|\n m = [ans[i-1]*2-ans[i],n].min\n ans[i] += m\n n -= m\nend\n(k-1).times do |i|\n if ans[i]*2 < ans[i+1]\n puts \"NO\"\n exit\n end\nend\nif n > 0\n puts \"NO\"\n exit\nend\nputs \"YES\",ans.join(\" \")"}], "negative_code": [], "src_uid": "df801ebbe05c17bb1d157108d523d1f8"} {"nl": {"description": "A binary string is a string that consists of characters $$$0$$$ and $$$1$$$. A bi-table is a table that has exactly two rows of equal length, each being a binary string.Let $$$\\operatorname{MEX}$$$ of a bi-table be the smallest digit among $$$0$$$, $$$1$$$, or $$$2$$$ that does not occur in the bi-table. For example, $$$\\operatorname{MEX}$$$ for $$$\\begin{bmatrix} 0011\\\\ 1010 \\end{bmatrix}$$$ is $$$2$$$, because $$$0$$$ and $$$1$$$ occur in the bi-table at least once. $$$\\operatorname{MEX}$$$ for $$$\\begin{bmatrix} 111\\\\ 111 \\end{bmatrix}$$$ is $$$0$$$, because $$$0$$$ and $$$2$$$ do not occur in the bi-table, and $$$0 < 2$$$.You are given a bi-table with $$$n$$$ columns. You should cut it into any number of bi-tables (each consisting of consecutive columns) so that each column is in exactly one bi-table. It is possible to cut the bi-table into a single bi-table — the whole bi-table.What is the maximal sum of $$$\\operatorname{MEX}$$$ of all resulting bi-tables can be?", "input_spec": "The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. Description of the test cases follows. The first line of the description of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$) — the number of columns in the bi-table. Each of the next two lines contains a binary string of length $$$n$$$ — the rows of the bi-table. It's guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.", "output_spec": "For each test case print a single integer — the maximal sum of $$$\\operatorname{MEX}$$$ of all bi-tables that it is possible to get by cutting the given bi-table optimally.", "sample_inputs": ["4\n7\n0101000\n1101100\n5\n01100\n10101\n2\n01\n01\n6\n000000\n111111"], "sample_outputs": ["8\n8\n2\n12"], "notes": "NoteIn the first test case you can cut the bi-table as follows: $$$\\begin{bmatrix} 0\\\\ 1 \\end{bmatrix}$$$, its $$$\\operatorname{MEX}$$$ is $$$2$$$. $$$\\begin{bmatrix} 10\\\\ 10 \\end{bmatrix}$$$, its $$$\\operatorname{MEX}$$$ is $$$2$$$. $$$\\begin{bmatrix} 1\\\\ 1 \\end{bmatrix}$$$, its $$$\\operatorname{MEX}$$$ is $$$0$$$. $$$\\begin{bmatrix} 0\\\\ 1 \\end{bmatrix}$$$, its $$$\\operatorname{MEX}$$$ is $$$2$$$. $$$\\begin{bmatrix} 0\\\\ 0 \\end{bmatrix}$$$, its $$$\\operatorname{MEX}$$$ is $$$1$$$. $$$\\begin{bmatrix} 0\\\\ 0 \\end{bmatrix}$$$, its $$$\\operatorname{MEX}$$$ is $$$1$$$.The sum of $$$\\operatorname{MEX}$$$ is $$$8$$$."}, "positive_code": [{"source_code": "t = gets.to_i\r\nt.times do\r\n\r\nn = gets.to_i\r\ns1 = gets.chomp.chars.map!(&:to_i)\r\ns2 = gets.chomp.chars.map!(&:to_i)\r\n\r\ns = s1.zip(s2).map { |a, b| (a + b + 1) % 3 }\r\nc = s.join.gsub(/01|10/, \"2\").chars.map!(&:to_i).sum\r\n\r\nputs c\r\n\r\nend\r\n"}], "negative_code": [], "src_uid": "6c4445ee23fedcb913ed3de1beacc099"} {"nl": {"description": "DZY loves collecting special strings which only contain lowercase letters. For each lowercase letter c DZY knows its value wc. For each special string s = s1s2... s|s| (|s| is the length of the string) he represents its value with a function f(s), where Now DZY has a string s. He wants to insert k lowercase letters into this string in order to get the largest possible value of the resulting string. Can you help him calculate the largest possible value he could get? ", "input_spec": "The first line contains a single string s (1 ≤ |s| ≤ 103). The second line contains a single integer k (0 ≤ k ≤ 103). The third line contains twenty-six integers from wa to wz. Each such number is non-negative and doesn't exceed 1000.", "output_spec": "Print a single integer — the largest possible value of the resulting string DZY could get.", "sample_inputs": ["abc\n3\n1 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1"], "sample_outputs": ["41"], "notes": "NoteIn the test sample DZY can obtain \"abcbbc\", value = 1·1 + 2·2 + 3·2 + 4·2 + 5·2 + 6·2 = 41."}, "positive_code": [{"source_code": "s = gets.chomp\nk = gets.to_i\narr = gets.split(\" \").map(&:to_i)\n\nans = 0 \ns.each_char.with_index do |c,i|\n\tweight = arr[c.ord - 97]\n\tans += weight * (i +1)\nend\nsum = ((s.length+1)..(s.length+k)).inject(0,&:+)\nans += sum * arr.max\nputs ans"}, {"source_code": "s = gets.chomp\nk = gets.to_i\narr = gets.split(\" \").map(&:to_i)\n\nans = 0 \ns.each_char.with_index do |c,i|\n\tweight = arr[c.ord - 97]\n\tans += weight * (i +1)\nend\nsum = ((s.length+1)..(s.length+k)).inject(0,&:+)\nans += sum * arr.max\nputs ans\n# "}, {"source_code": "str = gets.chomp\nk = gets.to_i\nazarr = gets.split(\" \").collect { |e| e.to_i}\n\naz = []\nstr.each_char { |e|\n az << azarr[e.ord - 97]\n}\n\nmax = [az.max, azarr.max].max\n\ni = 1\nlen = str.length\n#p len\nsum = max*(k*(2*len+1+k))/2\n#p sum\naz.each { |e|\n sum += i*e\n i += 1\n}\np sum"}, {"source_code": "a = gets.chomp.split('').map { |c| c.ord - 97 }\nk = gets.to_i\nw = gets.chomp.split.map(&:to_i)\nn = a.size + 1\nputs a.each_with_index.map { |x, i| w[x] * (i + 1) }.inject(:+) + ((2 * n + k - 1) * k / 2) * w.max"}, {"source_code": "a = gets.chomp.split('').map { |c| c.ord - 97 }\nk = gets.to_i\nw = gets.chomp.split.map(&:to_i)\nputs a.each_with_index.map { |x, i| w[x] * (i + 1) }.inject(:+) + ((2 * a.size + k + 1) * k / 2) * w.max"}, {"source_code": "s=gets.chomp\nk=gets.to_i\nw=gets.split.map &:to_i\nl,t=s.size,0\nl.times{|i| t+=(i+1)*w[s[i].ord-'a'.ord]}\nt+=(k*l+k*(k+1)/2)*w.max\np t"}, {"source_code": "s = gets.chomp\nk = gets.to_i\nw = gets.split.map {|i| i.to_i}\nm = w.max\nsum = 0\ns.scan(/./).each_with_index {|i, x| sum += w[i.ord - 'a'.ord] * (x + 1)}\nfor i in 1..k\n sum += m * (i + s.length)\nend\nputs sum\n"}, {"source_code": "#!/usr/bin/ruby\ns=gets.chomp.bytes.map{|e|e-97}\nn=gets.to_i\na=gets.split.map(&:to_i)\np (s.map{|e|a[e]}+[a.max]*n).each.with_index(1).reduce(0){|s,(e,i)|s+e*i}"}, {"source_code": "s = gets\nk = gets\nvals = gets\n\nk = k.to_i\nvals = vals.split(\" \")\nvals = vals.map(&:to_i)\n\ns = s.split(\" \")\nv1 = 0\ni = 1\n\ns[0].each_char do |c|\n v1+=(i*vals[c.ord-97])\n i+=1\nend\n\nvals = vals.sort.reverse\n\nfor j in 1..k\n v1 += (vals[0]*i)\n i+=1\nend\ni = 1\n\nputs v1\n\n"}, {"source_code": "s=gets.chomp.split('')\nk=gets.to_i\na=gets.chomp.split.map(&:to_i)\nm,ans,i=a.max,0,0\nwhile i i[1]\nend\n\nk.times do |i|\n total += (i + 1 + s.length) * values[0][1]\nend\n\nputs total"}, {"source_code": "s = gets.strip\nk = gets.to_i\nweights = gets.split(\" \").map(&:to_i)\n\nalph = [\"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\", \"h\", \"i\", \"j\", \"k\", \"l\", \"m\", \"n\", \"o\", \"p\", \"q\", \"r\", \"s\", \"t\", \"u\", \"v\", \"w\", \"x\", \"y\", \"z\"]\nalph_weights = {}\nalph.each_with_index { |letter, index| alph_weights[letter] = weights[index] }\n\nmax, max_index = weights.each_with_index.max\n\ns << alph[max_index] * k\n\nstr_weight = 0\ns.chars.each_with_index { |char, index|\n str_weight += alph_weights[char]*(index+1)\n}\n\n#puts s\nputs str_weight\n"}, {"source_code": "s = gets.chomp\nk = gets.to_i\nw = gets.split.map { |x| x.to_i }\n\nans = 0\ns.each_char.with_index do |c, i|\n\tans += w[c.ord - 97] * (i + 1)\nend\nans += k * (s.length + 1 + s.length + k) / 2 * w.max\n\nputs ans\n"}], "negative_code": [{"source_code": "#!/usr/bin/ruby\ns=gets.chomp.bytes.map{|e|e-97}\nn=gets.to_i\na=gets.split.map(&:to_i)\np (s.map{|e|a[e]}+[a.max]*3).sort.each.with_index(1).reduce(0){|s,(e,i)|s+e*i}"}, {"source_code": "#!/usr/bin/ruby\ns=gets.chomp.bytes.map{|e|e-97}\nn=gets.to_i\na=gets.split.map(&:to_i)\np (s.map{|e|a[e]}+[a.max]*3).each.with_index(1).reduce(0){|s,(e,i)|s+e*i}"}, {"source_code": "s = gets\nk = gets\nvals = gets\n\nk = k.to_i\nvals = vals.split(\" \")\ns = s.split(\" \")\nv1 = 0\ni = 1\n\ns[0].each_char do |c|\n v1+=(i*vals[c.ord-97].to_i)\n i+=1\nend\n\nvals = vals.sort.reverse\n\nfor j in 1..k\n v1 += (vals[0].to_i * i )\n i+=1\nend\n\nputs v1"}], "src_uid": "85f90533a9840e944deef9f3b4229cb8"} {"nl": {"description": "You are given an array $$$a$$$ of $$$n$$$ integers, and another integer $$$k$$$ such that $$$2k \\le n$$$.You have to perform exactly $$$k$$$ operations with this array. In one operation, you have to choose two elements of the array (let them be $$$a_i$$$ and $$$a_j$$$; they can be equal or different, but their positions in the array must not be the same), remove them from the array, and add $$$\\lfloor \\frac{a_i}{a_j} \\rfloor$$$ to your score, where $$$\\lfloor \\frac{x}{y} \\rfloor$$$ is the maximum integer not exceeding $$$\\frac{x}{y}$$$.Initially, your score is $$$0$$$. After you perform exactly $$$k$$$ operations, you add all the remaining elements of the array to the score.Calculate the minimum possible score you can get.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 500$$$) — the number of test cases. Each test case consists of two lines. The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le n \\le 100$$$; $$$0 \\le k \\le \\lfloor \\frac{n}{2} \\rfloor$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 2 \\cdot 10^5$$$).", "output_spec": "Print one integer — the minimum possible score you can get.", "sample_inputs": ["5\n7 3\n1 1 1 2 1 3 1\n5 1\n5 5 5 5 5\n4 2\n1 3 3 7\n2 0\n4 2\n9 2\n1 10 10 1 10 2 7 10 3"], "sample_outputs": ["2\n16\n0\n6\n16"], "notes": "NoteLet's consider the example test.In the first test case, one way to obtain a score of $$$2$$$ is the following one: choose $$$a_7 = 1$$$ and $$$a_4 = 2$$$ for the operation; the score becomes $$$0 + \\lfloor \\frac{1}{2} \\rfloor = 0$$$, the array becomes $$$[1, 1, 1, 1, 3]$$$; choose $$$a_1 = 1$$$ and $$$a_5 = 3$$$ for the operation; the score becomes $$$0 + \\lfloor \\frac{1}{3} \\rfloor = 0$$$, the array becomes $$$[1, 1, 1]$$$; choose $$$a_1 = 1$$$ and $$$a_2 = 1$$$ for the operation; the score becomes $$$0 + \\lfloor \\frac{1}{1} \\rfloor = 1$$$, the array becomes $$$[1]$$$; add the remaining element $$$1$$$ to the score, so the resulting score is $$$2$$$. In the second test case, no matter which operations you choose, the resulting score is $$$16$$$.In the third test case, one way to obtain a score of $$$0$$$ is the following one: choose $$$a_1 = 1$$$ and $$$a_2 = 3$$$ for the operation; the score becomes $$$0 + \\lfloor \\frac{1}{3} \\rfloor = 0$$$, the array becomes $$$[3, 7]$$$; choose $$$a_1 = 3$$$ and $$$a_2 = 7$$$ for the operation; the score becomes $$$0 + \\lfloor \\frac{3}{7} \\rfloor = 0$$$, the array becomes empty; the array is empty, so the score doesn't change anymore. In the fourth test case, no operations can be performed, so the score is the sum of the elements of the array: $$$4 + 2 = 6$$$."}, "positive_code": [{"source_code": "t = gets.to_i\r\nans = []\r\nt.times do |i|\r\n n,k = gets.split.map(&:to_i)\r\n a = gets.split.map(&:to_i).sort.reverse\r\n arr = []\r\n k.times do |i|\r\n arr << a[i]\r\n end\r\n k.times do |i|\r\n arr[i] = a[k+i] / arr[i]\r\n end\r\n (n-2*k).times do |i|\r\n arr << a[2*k+i]\r\n end\r\n ans << arr.sum\r\nend\r\nputs ans"}], "negative_code": [], "src_uid": "f48d55c60c12136979fe6db1e15c6053"} {"nl": {"description": "You are given an array $$$a$$$ consisting of $$$n$$$ integers.Let's call a pair of indices $$$i$$$, $$$j$$$ good if $$$1 \\le i < j \\le n$$$ and $$$\\gcd(a_i, 2a_j) > 1$$$ (where $$$\\gcd(x, y)$$$ is the greatest common divisor of $$$x$$$ and $$$y$$$).Find the maximum number of good index pairs if you can reorder the array $$$a$$$ in an arbitrary way.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$2 \\le n \\le 2000$$$) — the number of elements in the array. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^5$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2000$$$.", "output_spec": "For each test case, output a single integer — the maximum number of good index pairs if you can reorder the array $$$a$$$ in an arbitrary way.", "sample_inputs": ["3\n4\n3 6 5 3\n2\n1 7\n5\n1 4 2 4 1"], "sample_outputs": ["4\n0\n9"], "notes": "NoteIn the first example, the array elements can be rearranged as follows: $$$[6, 3, 5, 3]$$$.In the third example, the array elements can be rearranged as follows: $$$[4, 4, 2, 1, 1]$$$."}, "positive_code": [{"source_code": "gets.to_i.times do\n n = gets.to_i\n a = gets.split.map &:to_i\n a.sort! { |x, y|\n if x.even? && y.odd?\n -1\n elsif x.odd? && y.even?\n 1\n else\n 0\n end\n }\n ans = 0\n i = 0\n while i < n\n j = i + 1\n while j < n\n ans += 1 if a[i].gcd(2*a[j]) > 1\n j += 1\n end\n i += 1\n end\n puts ans\nend\n"}], "negative_code": [], "src_uid": "d638f524fe07cb8e822b5c6ec3fe8216"} {"nl": {"description": "Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.Petya wonders eagerly what minimum lucky number has the sum of digits equal to n. Help him cope with the task.", "input_spec": "The single line contains an integer n (1 ≤ n ≤ 106) — the sum of digits of the required lucky number.", "output_spec": "Print on the single line the result — the minimum lucky number, whose sum of digits equals n. If such number does not exist, print -1.", "sample_inputs": ["11", "10"], "sample_outputs": ["47", "-1"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/ruby\n\nn = gets.to_i\nseven = (n/7)\nwhile true\n\tif (n-seven*7) % 4 == 0\n\t\tputs '4'*((n-seven*7)/4) << '7'*seven\n\t\texit\n\tend\n\tseven -= 1\n\tif seven == -1\n\t\tputs -1\n\t\texit\n\tend\nend\n"}, {"source_code": "n = gets.chop.to_i\na = 0\nr = 0\nvyslo = 0\n(n-1).downto(0){ |b|\n\t#puts b\n\ta = (n-7*b)/4\n\tif 4*a + 7*b == n && a >= 0\n\t\tr = b\n\t\tvyslo = 1\n\t\tbreak\n\tend\n}\nif vyslo == 1\n\tfor i in 0..a-1\n\t\tprint \"4\"\n\tend\n\tfor i in 0..r-1\n\t\tprint \"7\"\n\tend\nelse\n\tprint \"-1\"\nend\nprint \"\\n\"\n"}, {"source_code": "def check\n count = 0\n val.each_char { |s|\n if (s == '4' or s == '7')\n count += 1\n end\n }\n\n if (count == 4 or count == 7)\n return true\n else\n return false\n end\nend\n\ndef succ(val, index)\n if index == -1\n return '4' + val\n end\n\n if (val[index] == '7')\n val[index] = '4'\n val = succ(val, index - 1)\n else\n val[index] = '7'\n end\n return val\nend\n\ndef isucc(i)\n s = i.to_s\n return succ(s, s.length-1).to_i\nend\n\ndef sum(i)\n h = Hash.new(0)\n h['4'] = 0\n h['7'] = 0\n i.each_char { |c|\n h[c] += 1\n }\n return h['4']*4 + h['7']*7\nend\n\ni = gets.chop.to_i\n\nmax4 = i / 4\nmax7 = i / 7\nmax4 += 1\nmax7 += 1\n\narr = Array.new(0)\nres = \"-1\"\nfound = false\nmax7.downto(0) { |i7|\n #puts i7\n max4 = i - i7*7 + 7\n max4.downto(0) { |i4|\n if i == i7*7 + i4*4\n res = '4'*i4 + '7'*i7\n found = true\n break\n end\n }\n if found == true\n break\n end\n}\n\nputs res\n\n#start = '4'\n#100.times {\n# start = succ(start, start.length-1)\n# puts \"#{start} sum = #{sum(start)}\"\n#}"}, {"source_code": "inp = gets.to_i\n\na = 0\nx = inp/7\nfound=false\nres1=''\nres2=''\nwhile x >= 0 do\n a = inp - 7*x\n if a%4 == 0\n found = true\n break\n end \n x = x-1 \nend\n\nres1 = res1.rjust(x,'7')\nres2 = res2.rjust(a/4,'4')\n\n\nif found\n puts res2+res1\nelse\n puts -1\nend \n"}, {"source_code": "i = STDIN.gets.to_i\n if (i % 7) == 0\n r = '7'*(i/7)\n else\n if i < 4 \n r = '-1'\n else\n seven = i/7\n while seven > -1 && (i-seven*7) % 4 != 0\n seven -= 1\n end\n \n if seven == -1\n r = '-1'\n else\n r = \"#{'4' * ((i - seven*7) / 4)}#{'7' * seven}\\n\"\n end\n end\n end\nputs r"}, {"source_code": "n = gets.to_i\ns = n / 7\nr = n - s * 7\nwhile s > 0 and r % 4 != 0 do s -= 1; r = n - s * 7 end\nif r % 4 != 0 or n < 4\n puts -1\nelse\n f = r / 4\n puts \"4\" * f + \"7\" * s\nend"}, {"source_code": "#!/usr/bin/env ruby\n\n\nn = STDIN.gets.to_i\n\ncount = n/7\nwhile count > -1\n if (n-7*count) % 4 == 0\n puts [[4]*((n-7*count)/4),[7]*count].flatten.join\n exit\n end\n count -= 1\nend\nputs -1\n"}, {"source_code": "def func(n)\n if (n == 0) then \n s = \"-1\"\n elsif (n % 7 == 0) then\n s = \"7\" * (n/7)\n elsif (n % 4 == 0) then\n s = \"4\"\n if n != 4 then\n n -= 4\n s += func(n)\n end\n else \n s = \"-1\"\n end\n return s\nend\n\nn = gets.to_i\nif (s = func(n)) != \"-1\" then \n print \"#{s}\\n\"\nelse\n r = 4\n s = \"4\"\n while r < n do\n if func(n-r) != \"-1\" then \n s += func(n-r)\n r = n\n elsif (r + 7) == n then \n r += 7\n s += \"7\"\n else\n r += 4\n s += \"4\"\n end\n end\n if r > n then s = \"-1\"\n end\n print \"#{s}\\n\"\nend\n"}, {"source_code": "n = gets.to_i\n# n = 7 * n7 + 4 * n4\n\nn7 = n / 7\nwhile n7 >= 0 do\n if (n - n7 * 7) % 4 == 0\n n4 = (n - n7 * 7) / 4\n break\n end\n n7 -= 1\nend\n\nif n7 >= 0\n puts (Array.new(n4) {4}).join + (Array.new(n7) {7}).join\nelse\n puts -1\nend"}, {"source_code": "sum = gets.chomp.to_i\ndiv = 1\ncount = 0\nwhile sum>=0\n if(sum%7==0 or sum == 0)\n puts (\"4\"*count + \"7\"*(sum/7)).split(\"\").sort.join\n exit\n end\n sum-=4\n count+=1\nend\nputs -1"}, {"source_code": "n = gets.to_i\n\ns = n / 7\nf = 0\nwhile s * 7 + f * 4 != n and s >= 0 do\n f += 1\n if s * 7 + f * 4 > n then s -= 1 end\nend\n\nif s >= 0 then\n st = ''\n puts '4' * f + '7' * s \nelse\n puts -1\nend\n\n"}, {"source_code": "# def lucky(four,seven,n)\n# \tif n == 0\n# \t\t# puts \"here\"\n# \t\t@fo = four\n# \t\t@sev = seven\n# \t\treturn true\n# \tend\n# \treturn false if n < 0\n# \tif lucky(four,seven+1,n-7) || lucky(four+1,seven,n-4)\n# \t\treturn true\n# \telse\n# \t\treturn false\n# \tend\t\n# end\n\n# @fo = 0\n# @sev = 0\n\n# n = gets.to_i\n\n# # puts lucky(0,0,n)\n# if lucky(0,0,n)\n# \tputs \"4\" * @fo + \"7\" * @sev\n# else\n# \tputs \"-1\"\n# end\n\nn = gets.to_i\n\nb = 0\nmin_a = 10 ** 12\nmin_b = 10 ** 12\nmin_ab = 10 ** 12\n\na = 0\nans = false\n# a * 4 + b * 7 = n\nwhile a * 4 <= n \n\tb = (n - (a * 4))/7\n\tif (a * 4 + b * 7 == n) && ((a + b < min_ab) || (a + b == min_ab && b < min_b))\n\t\tmin_a = a\n\t\tmin_b = b\n\t\tmin_ab = a + b\n\t\tans = true\n\tend\n\ta += 1\nend\n\nif ans\n\tputs \"4\" * min_a + \"7\" * min_b\nelse\n\tputs \"-1\"\nend\n"}, {"source_code": "n = gets.to_i\nm = -1\nfor i in 0..n/7\n\tm = i if ( n - 7 * i ) % 4 == 0\nend\nif m == -1\n\tputs -1\nelse\n\tn = ( n - 7 * m ) / 4\n\tn.times { print 4 }\n\tm.times { print 7 }\n\tputs\nend"}, {"source_code": "n = gets.to_i\ncnt_7 = n/7\ncnt_4 = (n-cnt_7*7)/4\nwhile cnt_7 >= 0 and cnt_7*7+cnt_4*4 != n\n cnt_7 -= 1\n cnt_4 = (n-cnt_7*7)/4\nend\nif cnt_7 >= 0 and cnt_7*7+cnt_4*4 == n\n puts '4'*cnt_4+'7'*cnt_7\nelse\n puts -1\nend\n"}], "negative_code": [{"source_code": "#!/usr/bin/ruby\n\nn = gets.to_i\ndp = Array.new(n+1, nil)\ndp[0] = ''\n1.upto(n) do |i|\n if i-4 >= 0 && dp[i-4]\n str = dp[i-4] + '4'\n if dp[i].nil?\n dp[i] = str\n else\n dp[i] = str if dp[i] > str\n end\n end\n\n if i-7 >= 0 && dp[i-7]\n str = dp[i-7] + '7'\n if dp[i].nil?\n dp[i] = str\n else\n dp[i] = str if dp[i] > str\n end\n end\nend\n\nputs dp[n] || -1\n"}, {"source_code": "n = gets.chop.to_i\na = 0\nb = 0\nvyslo = 0\nfor b in 0..n-1\n\ta = (n-7*b)/4\n\tif 4*a + 7*b == n && a >= 0\n\t\tvyslo = 1\n\t\tbreak\n\tend\nend\nif vyslo == 1\n\tfor i in 0..a-1\n\t\tprint \"4\"\n\tend\n\tfor i in 0..b-1\n\t\tprint \"7\"\n\tend\nelse\n\tprint \"-1\"\nend\nprint \"\\n\"\n"}, {"source_code": "n = gets.chop.to_i\na = 0\nb = 0\nfor b in 0..n-1\n\ta = (n-7*b)/4\n\tif 4*a + 7*b == n\n\t\tbreak\n\n\tend\nend\nputs \"A\" + a.to_s\nputs \"B\" + b.to_s\n"}, {"source_code": "n = gets.chop.to_i\na = 0\nb = 0\nfor b in 0..n-1\n\ta = (n-7*b)/4\n\tif 4*a + 7*b == n\n\t\tbreak\n\n\tend\nend\nfor i in 0..a-1\n\tprint \"4\"\nend\nfor i in 0..b-1\n\tprint \"7\"\nend\nprint \"\\n\"\n"}, {"source_code": "n = gets.chop.to_i\na = 0\nb = 0\nvyslo = 0;\nfor b in 0..n-1\n\ta = (n-7*b)/4\n\tif 4*a + 7*b == n\n\t\tvyslo = 1\n\t\tbreak\n\tend\nend\nif vyslo == 1\n\tfor i in 0..a-1\n\t\tprint \"4\"\n\tend\n\tfor i in 0..b-1\n\t\tprint \"7\"\n\tend\nelse\n\tprint \"-1\"\nend\nprint \"\\n\"\n"}, {"source_code": "def check\n count = 0\n val.each_char { |s|\n if (s == '4' or s == '7')\n count += 1\n end\n }\n\n if (count == 4 or count == 7)\n return true\n else\n return false\n end\nend\n\ndef succ(val, index)\n if index == -1\n return '4' + val\n end\n\n if (val[index] == '7')\n val[index] = '4'\n val = succ(val, index - 1)\n else\n val[index] = '7'\n end\n return val\nend\n\ndef isucc(i)\n s = i.to_s\n return succ(s, s.length-1).to_i\nend\n\ndef sum(i)\n h = Hash.new(0)\n h['4'] = 0\n h['7'] = 0\n i.each_char { |c|\n h[c] += 1\n }\n return h['4']*4 + h['7']*7\nend\n\ni = gets.chop.to_i\nii = i + 4 + 7 + 4\nx = 4\nfail = false\n\nfour_count = i / 4\nstart = '4' * four_count\n#x = start.to_i\n\ns = sum(start)\nwhile s != i\n if (s > ii)\n fail = true\n break\n end\n\n start = succ(start, start.length-1)\n s = sum(start)\nend\n\nif (fail == false)\n puts start\nelse\n puts -1\nend"}, {"source_code": "i = STDIN.gets.to_i\n if (i % 4) == 0\n r = '4'*(i/4)\n else\n if i < 7 \n r = '-1'\n else\n four = i/4\n while four > -1 && (i-four*4) % 7 != 0\n four -= 1\n end\n \n if four == -1\n r = '-1'\n else\n r = \"#{'4' * four}#{'7' * ((i - four*4) / 7)}\\n\"\n end\n end\n end\nputs r"}, {"source_code": "#!/usr/bin/env ruby\n\n\nn = STDIN.gets.to_i\n\ncount = 0\nwhile (7*count) < n\n if (n-7*count) % 4 == 0\n puts [[4]*((n-7*count)/4),[7]*count].flatten.join\n exit\n end\n count += 1\nend\nputs -1\n"}, {"source_code": "def func(n)\n if (n == 0) then \n s = \"-1\"\n elsif (n % 7 == 0) then\n s = \"7\" * (n/7)\n elsif (n % 4 == 0) then\n s = \"4\" * (n/4) \n else \n s = \"-1\"\n end\n return s\nend\n\nn = gets.to_i\nif (s = func(n)) != \"-1\" then \n print \"#{s}\\n\"\nelse\n r = 4\n s = \"4\"\n while r < n do\n if func(n-r) != \"-1\" then \n s += func(n-r)\n r = n\n elsif (r + 7) == n then \n r += 7\n s += \"7\"\n else\n r += 4\n s += \"4\"\n end\n end\n if r > n then s = \"-1\"\n end\n print \"#{s}\\n\"\nend\n"}, {"source_code": "def func(n)\n if (n == 0) then \n s = \"-1\"\n elsif (n % 7 == 0) then\n s = \"7\" * (n/7)\n elsif (n % 4 == 0) then\n s = \"4\"\n n -= 4\n s += func(n)\n else \n s = \"-1\"\n end\n return s\nend\n\nn = gets.to_i\nif (s = func(n)) != \"-1\" then \n print \"#{s}\\n\"\nelse\n r = 4\n s = \"4\"\n while r < n do\n if func(n-r) != \"-1\" then \n s += func(n-r)\n r = n\n elsif (r + 7) == n then \n r += 7\n s += \"7\"\n else\n r += 4\n s += \"4\"\n end\n end\n if r > n then s = \"-1\"\n end\n print \"#{s}\\n\"\nend\n"}, {"source_code": "sum = gets.chomp.to_i\nq4 = sum/4\ndiv4 = sum%4\nq7 = sum - q4*4\ndiv7 = (sum - q4*4)%7\n\nif(div7==0)\n num = \"4\"*q4 + \"7\"*q7\n num = num.split(\"\").sort.join\n puts num\nelse\n puts -1\nend"}, {"source_code": "sum = gets.chomp.to_i\ndiv = 1\ncount = 0\nwhile sum>0\n sum-=4\n count+=1\n if(sum%7==0)\n puts (\"4\"*count + \"7\"*(sum/7)).split(\"\").sort.join\n exit\n end\nend\nputs -1"}, {"source_code": "sum = gets.chomp.to_i\nq7 = sum/7\ndiv7 = sum%7\nq4 = (sum - q7*7)/4\ndiv4 = (sum-q7*7)%4\n\nif(div4==0)\n num = \"4\"*q4 + \"7\"*q7\n num = num.split(\"\").sort.join\n puts num\nelse\n puts -1\nend\n\n"}, {"source_code": "sum = gets.chomp.to_i\nq7 = sum/7\ndiv7 = sum%7\nq4 = (sum - q7*7)/4\ndiv4 = (sum-q7*7)%4\n\nif(div4==0)\n num = \"4\"*q4 + \"7\"*q7\n num = num.split(\"\").sort.join\n puts num\nelse\n puts -1\nend"}, {"source_code": "require 'mathn'\nsum = gets.chomp.to_i\nq7 = (sum/7).floor\ndiv7 = sum%7\nq4 = ((sum - q7*7)/4).floor\ndiv4 = (sum-q7*7)%4\n\nif(div4==0)\n num = \"4\"*q4 + \"7\"*q7\n num = num.split(\"\").sort.join\n puts num\nelse\n puts -1\nend"}, {"source_code": "# def lucky(four,seven,n)\n# \tif n == 0\n# \t\t# puts \"here\"\n# \t\t@fo = four\n# \t\t@sev = seven\n# \t\treturn true\n# \tend\n# \treturn false if n < 0\n# \tif lucky(four,seven+1,n-7) || lucky(four+1,seven,n-4)\n# \t\treturn true\n# \telse\n# \t\treturn false\n# \tend\t\n# end\n\n# @fo = 0\n# @sev = 0\n\n# n = gets.to_i\n\n# # puts lucky(0,0,n)\n# if lucky(0,0,n)\n# \tputs \"4\" * @fo + \"7\" * @sev\n# else\n# \tputs \"-1\"\n# end\n\nn = gets.to_i\n\nb = 0\nmin_a = 10 ** 5\nmin_b = 10 ** 5\nmin_ab = 10 ** 5\n\na = 0\nans = false\n# a * 4 + b * 7 = n\nwhile a * 4 <= n \n\tb = (n - (a * 4))/7\n\tif (a * 4 + b * 7 == n) && ((a + b < min_ab) || (a + b == min_ab && b < min_b))\n\t\tmin_a = a\n\t\tmin_b = b\n\t\tmin_ab = a + b\n\t\tans = true\n\tend\n\ta += 1\nend\n\nif ans\n\tputs \"4\" * min_a + \"7\" * min_b\nelse\n\tputs \"-1\"\nend\n"}], "src_uid": "2584fa8c1adb1aa8cd5c28a8610ff72d"} {"nl": {"description": "$$$n$$$ students are taking an exam. The highest possible score at this exam is $$$m$$$. Let $$$a_{i}$$$ be the score of the $$$i$$$-th student. You have access to the school database which stores the results of all students.You can change each student's score as long as the following conditions are satisfied: All scores are integers $$$0 \\leq a_{i} \\leq m$$$ The average score of the class doesn't change. You are student $$$1$$$ and you would like to maximize your own score.Find the highest possible score you can assign to yourself such that all conditions are satisfied.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 200$$$). The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\leq n \\leq 10^{3}$$$, $$$1 \\leq m \\leq 10^{5}$$$)  — the number of students and the highest possible score respectively. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$ 0 \\leq a_{i} \\leq m$$$)  — scores of the students.", "output_spec": "For each testcase, output one integer  — the highest possible score you can assign to yourself such that both conditions are satisfied._", "sample_inputs": ["2\n4 10\n1 2 3 4\n4 5\n1 2 3 4"], "sample_outputs": ["10\n5"], "notes": "NoteIn the first case, $$$a = [1,2,3,4] $$$, with average of $$$2.5$$$. You can change array $$$a$$$ to $$$[10,0,0,0]$$$. Average remains $$$2.5$$$, and all conditions are satisfied.In the second case, $$$0 \\leq a_{i} \\leq 5$$$. You can change $$$a$$$ to $$$[5,1,1,3]$$$. You cannot increase $$$a_{1}$$$ further as it will violate condition $$$0\\le a_i\\le m$$$."}, "positive_code": [{"source_code": "t = gets.to_i\nt.times do\n n,m = gets.split.map(&:to_i)\n a = gets.split.map(&:to_i)\n p [a.inject(:+), m].min\nend\n"}, {"source_code": "def read_input()\n \n rules = STDIN.gets()\n students = STDIN.gets()\n \n rules = rules.split(' ').map(&:to_i)\n students = students.split(' ').map(&:to_i)\n \n raise \"Invalid rules input\" if rules.size != 2\n raise \"Invalid input\" if students.size != rules[0]\n \n return rules, students\n \nend\n\ndef do_some_magic(cnt)\n cnt.times {\n \n rules, students = read_input()\n \n oth_cnt = students.size - 1\n \n # we need this num of points\n x = rules[1] - students[0]\n \n # available this num of points\n y = 0\n 1.upto(oth_cnt) { |s|\n y += students[s]\n }\n \n # and this num of points we can add to first student\n z = y < x ? y : x\n \n puts students[0] + z\n \n }\nend\n\ncnt = Integer(STDIN.gets())\n\ndo_some_magic(cnt)\n"}, {"source_code": "#! /usr/bin/env ruby\n# encoding: UTF-8\n\ndef solve\n\tn,m = gets.split.map{|x| x.to_i }\n\txs = gets.split.map {|x| x.to_i }\n\tsum = xs.reduce(:+)\n\tputs([m, sum].min)\nend\n\nt = gets.to_i\nt.times {solve}\n\n\n"}, {"source_code": "T = gets.to_i\n\nT.times do\n n, m = gets.split.map(&:to_i)\n a = gets.split.map(&:to_i)\n\n sum = a.inject(:+)\n\n puts [m, sum].min\nend\n"}, {"source_code": "def main\n t = gets.chomp.to_i\n t.times { solve }\nend\n\ndef solve\n n, m = gets.chomp.split.map(&:to_i)\n a = gets.chomp.split.map(&:to_i)\n # S = gets.chomp.chars\n \n sum = 0\n 0.upto(n-1).each do |i|\n sum += a[i]\n end\n a0 = [sum, m].min # min(sum, m) is a0\n p a0\nend\n\nmain\n"}], "negative_code": [], "src_uid": "7c2a61de728e6767b25e58c74497bbae"} {"nl": {"description": "You received a notebook which is called Death Note. This notebook has infinite number of pages. A rule is written on the last page (huh) of this notebook. It says: \"You have to write names in this notebook during $$$n$$$ consecutive days. During the $$$i$$$-th day you have to write exactly $$$a_i$$$ names.\". You got scared (of course you got scared, who wouldn't get scared if he just receive a notebook which is named Death Note with a some strange rule written in it?).Of course, you decided to follow this rule. When you calmed down, you came up with a strategy how you will write names in the notebook. You have calculated that each page of the notebook can contain exactly $$$m$$$ names. You will start writing names from the first page. You will write names on the current page as long as the limit on the number of names on this page is not exceeded. When the current page is over, you turn the page. Note that you always turn the page when it ends, it doesn't matter if it is the last day or not. If after some day the current page still can hold at least one name, during the next day you will continue writing the names from the current page.Now you are interested in the following question: how many times will you turn the page during each day? You are interested in the number of pages you will turn each day from $$$1$$$ to $$$n$$$.", "input_spec": "The first line of the input contains two integers $$$n$$$, $$$m$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$, $$$1 \\le m \\le 10^9$$$) — the number of days you will write names in the notebook and the number of names which can be written on each page of the notebook. The second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$), where $$$a_i$$$ means the number of names you will write in the notebook during the $$$i$$$-th day.", "output_spec": "Print exactly $$$n$$$ integers $$$t_1, t_2, \\dots, t_n$$$, where $$$t_i$$$ is the number of times you will turn the page during the $$$i$$$-th day.", "sample_inputs": ["3 5\n3 7 9", "4 20\n10 9 19 2", "1 100\n99"], "sample_outputs": ["0 2 1", "0 0 1 1", "0"], "notes": "NoteIn the first example pages of the Death Note will look like this $$$[1, 1, 1, 2, 2], [2, 2, 2, 2, 2], [3, 3, 3, 3, 3], [3, 3, 3, 3]$$$. Each number of the array describes during which day name on the corresponding position will be written. It is easy to see that you should turn the first and the second page during the second day and the third page during the third day."}, "positive_code": [{"source_code": "def single_input\n\tgets.chomp.to_i\nend\n\ndef multiple_input\n\tgets.chomp.split(' ').map(&:to_i)\nend\n\nn, m = multiple_input\nnames = multiple_input\n\nrem = m\nac = []\nn.times do |i|\n\tif names[i] < rem\n\t\trem -= names[i]\n\t\tac << 0\n\t\tnext\n\tend\n\n\tif rem == names[i]\n\t\trem = m\n\t\tac << 1\n\t\tnext\n\tend\n\n\n\tx = (names[i]-rem) / m\n\trem = (names[i]-rem) % m\n\trem = m - rem\n\tac << (x + 1)\nend\n\nprint ac.map(&:to_s).join(\" \")"}, {"source_code": "n,m,*a=$<.read.split.map &:to_i\ns=q=0;r=[]\na.each{|e|s+=e;r<= m\n end\n puts pages.join(' ')\nend\n\nsolve\n"}, {"source_code": "N, M = gets.split.map(&:to_i)\nas = gets.split.map(&:to_i)\nsum = 0\nans = []\nN.times do |i|\n ans[i] = (sum+as[i])/M - sum/M\n sum += as[i]\nend\nputs ans.join(' ')"}, {"source_code": "n, m = gets.strip.split.map(&:to_i)\nais = gets.strip.split.map(&:to_i)\n\nans = Array.new(n, 0)\ncu=m\n0.upto(n-1).each do |i|\n ai = ais[i]\n if aicu\n rai = ai-cu\n ans[i]+=1\n ans[i]+= (rai/m)\n cu = m - (rai%m)\n cu = m if cu==0\n end\nend\n\nputs ans.join(' ')"}], "negative_code": [{"source_code": "def single_input\n\tgets.chomp.to_i\nend\n\ndef multiple_input\n\tgets.chomp.split(' ').map(&:to_i)\nend\n\nn, m = multiple_input\nnames = multiple_input\n\nrem = m\nac = []\nn.times do |i|\n\tif names[i] < rem\n\t\trem -= names[i]\n\t\tac << 0\n\t\tnext\n\tend\n\n\tif rem == names[i]\n\t\trem = 0\n\t\tac << 1\n\t\tnext\n\tend\n\n\n\tx = (names[i]-rem) / m\n\trem = (names[i]-rem) % m\n\trem = m - rem\n\tac << (x + 1)\nend\n\nprint ac.map(&:to_s).join(\" \")"}, {"source_code": "def single_input\n\tgets.chomp.to_i\nend\n\ndef multiple_input\n\tgets.chomp.split(' ').map(&:to_i)\nend\n\nn, m = multiple_input\nnames = multiple_input\n\nrem = m\nac = []\nn.times do |i|\n\tif names[i] < rem\n\t\trem -= names[i]\n\t\tac << 0\n\t\tnext\n\tend\n\n\tif rem == names[i]\n\t\trem = 0\n\t\tac << 1\n\t\tnext\n\tend\n\n\n\tx = (names[i]-rem) / m\n\trem = (names[i]-rem) % m\n\trem = m - rem\n\tac << (x + 1)\nend\n\np ac.map(&:to_s).join(\" \")"}], "src_uid": "a2085c2d21b2dbe4cc27a15fa4a1ec4f"} {"nl": {"description": "Cowboy Vlad has a birthday today! There are $$$n$$$ children who came to the celebration. In order to greet Vlad, the children decided to form a circle around him. Among the children who came, there are both tall and low, so if they stand in a circle arbitrarily, it may turn out, that there is a tall and low child standing next to each other, and it will be difficult for them to hold hands. Therefore, children want to stand in a circle so that the maximum difference between the growth of two neighboring children would be minimal possible.Formally, let's number children from $$$1$$$ to $$$n$$$ in a circle order, that is, for every $$$i$$$ child with number $$$i$$$ will stand next to the child with number $$$i+1$$$, also the child with number $$$1$$$ stands next to the child with number $$$n$$$. Then we will call the discomfort of the circle the maximum absolute difference of heights of the children, who stand next to each other.Please help children to find out how they should reorder themselves, so that the resulting discomfort is smallest possible.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$2 \\leq n \\leq 100$$$) — the number of the children who came to the cowboy Vlad's birthday. The second line contains integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\leq a_i \\leq 10^9$$$) denoting heights of every child.", "output_spec": "Print exactly $$$n$$$ integers — heights of the children in the order in which they should stand in a circle. You can start printing a circle with any child. If there are multiple possible answers, print any of them.", "sample_inputs": ["5\n2 1 1 3 2", "3\n30 10 20"], "sample_outputs": ["1 2 3 2 1", "10 20 30"], "notes": "NoteIn the first example, the discomfort of the circle is equal to $$$1$$$, since the corresponding absolute differences are $$$1$$$, $$$1$$$, $$$1$$$ and $$$0$$$. Note, that sequences $$$[2, 3, 2, 1, 1]$$$ and $$$[3, 2, 1, 1, 2]$$$ form the same circles and differ only by the selection of the starting point.In the second example, the discomfort of the circle is equal to $$$20$$$, since the absolute difference of $$$10$$$ and $$$30$$$ is equal to $$$20$$$."}, "positive_code": [{"source_code": "n = gets.to_i\na = gets.split.map(&:to_i).sort\nans = []\nc = 0\na.each do |v|\n if c % 2 == 0\n ans.push(v)\n else\n ans.unshift(v)\n end\n c += 1\nend\nputs ans.join(\" \")\n"}, {"source_code": "DBG = true\nn = gets.to_i\na = gets.split.map{|z| z.to_i}\na.sort!\nm = n/2\nb = Array.new(n)\nfor i in 0...m\n b[i] = a[2*i]\n b[n-1-i] = a[2*i+1]\nend\nif n%2 == 1\n b[m] = a[n-1]\nend\nfor i in 0...n\n print \"#{b[i]} \"\nend\nputs \"\"\n"}, {"source_code": "N = gets.to_i\nX = gets.split.map(&:to_i).sort\n\no = X.each_with_index.select{|e,i| i.odd?}.map(&:shift)\no += X.each_with_index.select{|e,i| i.even?}.map(&:shift).reverse\n\nputs o*\" \"\n\n# #pragma GCC optimize (\"O3\")\n# #include \"bits/stdc++.h\"\n# \n# using namespace std;\n# using ll = long long int;\n# \n# #define debugos cout\n# #define debug(v) {printf(\"L%d %s > \",__LINE__,#v);debugos<<(v)< \",__LINE__,#v);for(auto e:(v)){debugos< \",__LINE__,#m);for(int x=0;x<(w);x++){debugos<<(m)[x]<<\" \";}debugos<\\n\",__LINE__,#m);for(int y=0;y<(h);y++){for(int x=0;x<(w);x++){debugos<<(m)[y][x]<<\" \";}debugos<::type>::type cnt=0;(cnt)<(l);++(cnt))\n# #define rrepeat(cnt,l) for(auto cnt=(l)-1;0<=(cnt);--(cnt))\n# #define iterate(cnt,b,e) for(auto cnt=(b);(cnt)!=(e);++(cnt))\n# #define diterate(cnt,b,e) for(auto cnt=(b);(cnt)!=(e);--(cnt))\n# const ll MD = 1000000007ll; const long double PI = 3.1415926535897932384626433832795L;\n# inline void assert_call(bool assertion, function f) { if (!assertion) { cerr << \"assertion fault:\" << endl; f(); abort(); } }\n# template inline ostream& operator <<(ostream &o, const pair p) { o << '(' << p.first << ':' << p.second << ')'; return o; }\n# template inline ostream& _ostream_vecprint(ostream& os, const Vec& a) {\n# os << '['; for (const auto& e : a) os << ' ' << e << ' '; os << ']'; return os;\n# }\n# template inline ostream& operator<<(ostream& o, const vector& v) { return _ostream_vecprint(o, v); }\n# template inline ostream& operator<<(ostream& o, const array& v) { return _ostream_vecprint(o, v); }\n# template inline T& chmax(T& to, const T& val) { return to = max(to, val); }\n# template inline T& chmin(T& to, const T& val) { return to = min(to, val); }\n# void bye(string s, int code = 0) { cout << s << endl; exit(code); }\n# mt19937_64 randdev(8901016);\n# template inline T rand(T l, T h) { return uniform_int_distribution(l, h)(randdev); }\n# template<> inline double rand(double l, double h) { return uniform_real_distribution(l, h)(randdev); }\n# template<> inline float rand(float l, float h) { return uniform_real_distribution(l, h)(randdev); }\n# \n# #if defined(_WIN32) || defined(_WIN64)\n# #define getchar_unlocked _getchar_nolock\n# #define putchar_unlocked _putchar_nolock\n# #elif defined(__GNUC__)\n# #else\n# #define getchar_unlocked getchar\n# #define putchar_unlocked putchar\n# #endif\n# namespace {\n# #define isvisiblechar(c) (0x21<=(c)&&(c)<=0x7E)\n# class MaiScanner {\n# public:\n# template void input_integer(T& var) {\n# var = 0; T sign = 1;\n# int cc = getchar_unlocked();\n# for (; cc < '0' || '9' < cc; cc = getchar_unlocked())\n# if (cc == '-') sign = -1;\n# for (; '0' <= cc && cc <= '9'; cc = getchar_unlocked())\n# var = (var << 3) + (var << 1) + cc - '0';\n# var = var * sign;\n# }\n# inline int c() { return getchar_unlocked(); }\n# inline MaiScanner& operator>>(int& var) { input_integer(var); return *this; }\n# inline MaiScanner& operator>>(long long& var) { input_integer(var); return *this; }\n# inline MaiScanner& operator>>(string& var) {\n# int cc = getchar_unlocked();\n# for (; !isvisiblechar(cc); cc = getchar_unlocked());\n# for (; isvisiblechar(cc); cc = getchar_unlocked())\n# var.push_back(cc);\n# return *this;\n# }\n# template void in(IT begin, IT end) { for (auto it = begin; it != end; ++it) *this >> *it; }\n# };\n# class MaiPrinter {\n# public:\n# template\n# void output_integer(T var) {\n# if (var == 0) { putchar_unlocked('0'); return; }\n# if (var < 0)\n# putchar_unlocked('-'),\n# var = -var;\n# char stack[32]; int stack_p = 0;\n# while (var)\n# stack[stack_p++] = '0' + (var % 10),\n# var /= 10;\n# while (stack_p)\n# putchar_unlocked(stack[--stack_p]);\n# }\n# inline MaiPrinter& operator<<(char c) { putchar_unlocked(c); return *this; }\n# inline MaiPrinter& operator<<(int var) { output_integer(var); return *this; }\n# inline MaiPrinter& operator<<(long long var) { output_integer(var); return *this; }\n# inline MaiPrinter& operator<<(char* str_p) { while (*str_p) putchar_unlocked(*(str_p++)); return *this; }\n# inline MaiPrinter& operator<<(const string& str) {\n# const char* p = str.c_str();\n# const char* l = p + str.size();\n# while (p < l) putchar_unlocked(*p++);\n# return *this;\n# }\n# template void join(IT begin, IT end, char sep = ' ') { for (bool b = 0; begin != end; ++begin, b = 1) b ? *this << sep << *begin : *this << *begin; }\n# };\n# }\n# MaiScanner scanner;\n# MaiPrinter printer;\n# \n# \n# //\n# \n# /*\n# あらかじめソート\n# \n# 低い人->高い人 を選択\n# 高い人->低い人 を未選択する\n# \n# これを動的計画法する\n# */\n# \n# int N;\n# ll A[111];\n# \n# ll dp[111][111];\n# \n# int main() {\n# scanner >> N;\n# scanner.in(A, A + N);\n# sort(A, A + N);\n# \n# fill(dp[0], dp[110], ll(1e18));\n# dp[0][0] = 0;\n# \n# int M = N - 1;\n# iterate(i, 0, N) {\n# iterate(j, 0, i+1) {\n# ll d = 0;\n# int jp = j;\n# iterate(k, i+1, N) {\n# chmin(dp[k][j], max(dp[i][j], max(d, A[k] - A[i])));\n# \n# chmax(d, A[k] - A[jp]);\n# jp = k;\n# }\n# }\n# }\n# ll ans = 1e18;\n# repeat(i, N) {\n# chmin(ans, max(dp[N-1][i], A[N - 1] - A[i]));\n# }\n# \n# debugaa(dp, N+1, N+1);\n# \n# cout << ans << endl;\n# \n# return 0;\n# }"}, {"source_code": "if File.exists?(\"input\")\n $stdin = File.open(\"input\")\n $stdout = File.open(\"output\",\"w\")\nend\n\nn = gets.to_i\narr = []\narr= gets.chomp.split(\" \").map(&:to_i)\n\ndef getdisc(arr)\n\tmax = 0\n\tarr[1..-1].each_with_index do |x,i|\n\t\tasd = (x-arr[i-1]).abs\n\t\tmax = asd if asd>max\n\tend\n\tasd = (arr[-1]-arr[0]).abs\n\tmax=asd if asd>max\n\treturn max\nend\n\ndef newgetdisc(arr)\n\tmax = 0\n\tarr[1..-1].each_with_index do |x,i|\n\t\tasd = (x-arr[i-1]).abs\n\t\tmax+= asd\n\tend\n\tasd = (arr[-1]-arr[0]).abs\n\tmax+=asd# if asd>max\n\treturn max\nend\n\ndef swap(arr,a,b)\n\ttemp = arr[a]\n\tarr[a]=arr[b]\n\tarr[b]=temp\n\treturn arr\nend\n\n#newarr = []\n#disc = \n\n#p arr\n=begin\narr.each do |x|\n\tif newarr.length==0\n\t\tnewarr.push x\n\telse\n\t\tscorearr = []\n\t\t(0..newarr.length).each do |insloc|\n\t\t\tnewnewarr = newarr.dup\n\t\t\tnewnewarr.insert(insloc,x)\n\t\t\tscorearr.push newgetdisc(newnewarr)\n\t\tend\n\t\ttheloc = scorearr.index(scorearr.min)\n\t\tnewarr.insert(theloc,x)\n\tend\n\tp newarr\nend\n=end\n=begin\nnewarr = arr.dup\n\nwhile true\n\tcurrscore = getdisc(newarr)\n\ttobreak = false\n\tnewarr.each_with_index do |x,i|\n\t\tnewarr[i+1..-1].each_with_index do |y,j|\n\t\t\tnewnewarr = newarr.dup\n\t\t\tnewnewarr = swap(newnewarr,i,j)\n\t\t\tif(getdisc(newnewarr) dic[i].size \n res +=dic[i]\n else \n res +=i\n end \n res +=' '\nend \nputs res\n"}, {"source_code": "n,m=gets.split.map{|e| e.to_i}\nh={}\nm.times{\n\ta,b=gets.chomp.split\n\tif ((a.size)<=>(b.size))<1 then\n\t\th[a]=a\n\t\th[b]=a\n\telse\n\t\th[a]=b\n\t\th[b]=b\n\tend\n}\ns=gets.chomp.split\ni=0\ns.each{|e|\n\tprint \" \" if i>0\n\tprint h[e]\n\ti+=1\n}\nprint \"\\n\"\n\n"}, {"source_code": "#$stdin = File.open \"input.txt\"\n\nn, m = gets.split.map &:to_i\n\nreplace = {}\nm.times do\n\ta, b = gets.chomp.split\n\treplace[a] = b.size < a.size ? b : a\nend\n\nputs gets.chomp.split.map { |s| replace[s] }.join(\" \")\n\n\n"}, {"source_code": "#!/usr/bin/ruby\nclass InputReader\n def initialize\n @data = []\n @offset = 0\n end\n\n def next\n if @data.empty? || @offset >= @data.length\n line = STDIN.gets.chomp\n return nil if line.empty?\n @data = line.split(/\\s+/)\n @offset = 0\n end\n @offset += 1\n return @data[@offset-1]\n end\nend\n\n# ===== Your solution goes here =====\nreader = InputReader.new\nn = reader.next.to_i\nm = reader.next.to_i\ndict = {}\nm.times do\n k = reader.next\n v = reader.next\n dict[k] = v\nend\n\nn.times do\n word = reader.next\n if word.length <= dict[word].length\n print \"#{word} \"\n else\n print \"#{dict[word]} \"\n end\nend\n"}, {"source_code": "n, m = gets.split(' ').map(&:to_i)\ndic = Hash[]\nm.times do \n word = gets.split\n dic[word.first] = word.min_by &:length\nend\nputs gets.split.map {|w| dic[w]}.join(' ')"}, {"source_code": "nums = gets.chomp.split\nn = Integer(nums[0])\nm = Integer(nums[1])\nbest = Array.new\nbad = Array.new\ninput = Array.new\nfor i in 0...m\n input[i] = Array.new\n input[i] = gets.chomp.split\n if input[i][0].length <= input[i][1].length\n best.push(input[i][0])\n bad.push(input[i][1])\n else\n best.push(input[i][1])\n bad.push(input[i][0])\n end\nend\nlecture = Array.new\nlecture = gets.chomp.split\n\nnote = {}\nfor i in 0...m\n note[best[i]] = best[i]\n note[bad[i]] = best[i]\nend\nbest_note = []\nfor i in 0...n\n best_note.push(note[lecture[i]])\nend\n\nprint best_note.join(\" \")"}, {"source_code": "nm = gets.chomp.split\nn = nm[0].to_i\nm = nm[1].to_i\nab = []\na = []\nb = []\nfor i in (0..m-1)\n\tab[i] = gets.chomp.split\n\ta[i] = ab[i][0]\n\tb[i] = ab[i][1]\n\tif a[i].size <= b[i].size\n\t\tb[i] = nil\n\tend\nend\nc = gets.chomp.split\n\nfor i in (0..n-1)\n\tif b[a.find_index(c[i])] == nil\n\t\tprint a[a.find_index(c[i])] + \" \"\n\telse\n\t\tprint b[a.find_index(c[i])] + \" \"\n\tend\nend"}, {"source_code": "n,m = gets.split(\" \").map(&:to_i)\nwords = {}\nm.times do\n\ta,b = gets.split(\" \")\n\twords[a] = b\nend\nlec = gets.split(\" \")\nop = []\nlec.each do |a|\n\tif a.length > words[a].length\n\t\top << words[a]\n\telse\n\t\top << a\n\tend\nend\n\nputs op.join(\" \")"}, {"source_code": "n, m = gets.chomp.split(' ').map(&:to_i)\na = {}\nm.times do\n ai, bi = gets.chomp.split ' '\n a[ai] = bi\nend\n\nres = ''\n\ngets.chomp.split(' ').each do |s|\n res += (s.length <= a[s].length ? s : a[s]) + ' '\nend\nputs res\n"}, {"source_code": "n,m = gets.split.map &:to_i\ndic = Hash[]\nm.times do\n w = gets.split\n dic[w.first] = w.min_by &:length\nend\nputs gets.split.map {|w| dic[w]}.join(' ')"}, {"source_code": "n, m = gets.split.map(&:to_i)\na, b = m.times.map { gets.split }.transpose\nputs gets.split.map { |x| [x, b[a.index(x)]].min_by(&:size) }.join(' ')"}, {"source_code": "\nline = gets.chop!.split(\" \")\n\nn = line[0].to_i\nm = line[1].to_i\n\nA = {}\nB = {}\n\nfor i in (1 .. m)\n line = gets.chop!.split(\" \")\n\n first = line[0]\n second = line[1]\n\n B[first] = i\n B[second] = i\n\n if second.size < first.size\n A[i] = second\n elsif second.size > first.size\n A[i] = first\n else\n A[i] = first\n end\nend\n\nline = gets.chop!.split(\" \")\n\nfor i in (1 .. n)\n print A[B[line[i-1]]]\n\n if i < n\n print \" \"\n end\nend\n"}, {"source_code": "n, m = gets.split.map &:to_i\ndict = Hash.new\nm.times do\n\ta, b = gets.split\n\tdict[a] = b.size < a.size ? b : a\nend\nputs gets.split.map { |_| dict[_] }.join(' ')"}, {"source_code": "n, m = gets.split.map{|x| x.to_i}\nh = {}\nm.times{h.merge!(Hash[*gets.split])}\nputs(gets.split.map{|x| x.size <= h[x].size ? x : h[x]}.join(' '))\n"}, {"source_code": "q=[]\nj=0\ngets.split.each do |i| \nq[j]=i.to_i\nj=j+1\nend\nhash=Hash[]\n\nq[1].times do\n s=gets.split\n hash[s[0]]=s[1]\nend\n\ngets.split.each do |i|\n if i.length <= hash[i].length\n print i + \" \"\n else\n print hash[i]+\" \"\n end \nend"}, {"source_code": "# http://codeforces.com/problemset/problem/499/B\n\nn, m = gets.chomp.split.map(&:to_i)\nlang_1 = []\nlang_2 = []\n\nm.times do\n words = gets.chomp.split\n lang_1 << words[0]\n lang_2 << words[1]\nend\n\nlecture = gets.chomp.split\nnote = []\n\nfor word in lecture\n index = lang_1.index(word)\n replace_word = [word, lang_2[index]].min_by(&:length)\n note << replace_word\nend\n\nputs note.join(\" \")\n"}, {"source_code": "n, m = gets.split(' ').map(&:to_i)\nwords = {}\n\n1.upto(m) do\n k, v = gets.chomp.split(' ')\n words[k] = v\nend\n\nresult = gets.chomp.split(' ').map { |s| s.length > words[s].length ? words[s] : s }\n\nputs result.join(' ')\n"}, {"source_code": "# 499B\n\ndef read_line\n gets.chomp.split\nend\n\nw = {}\nread_line[1].to_i.times do\n k, v = read_line\n w[k] = v\nend\nputs read_line.map { |k| v = w[k]; v.size < k.size ? v : k } * ' '"}, {"source_code": "# f = File.open('input.txt', 'r')\nf = STDIN\nn, m = f.gets.chomp.split.map &:to_i\ndict1, dict2 = {}, {}\nm.times do\n s1, s2 = f.gets.chomp.split\n dict1[s1] = s2\n dict2[s2] = s1\nend\n\nstrs = f.gets.chomp.split\nt = strs.map do |s|\n if dict1[s]\n dict1[s].length < s.length ? dict1[s] : s\n else\n dict2[s].length < s.length ? dict2[s] : s\n end\nend\n\nputs t.join(' ')\n"}, {"source_code": "class Array\n\tdef __min\n\t\tself[0].length <= self[1].length ? self[0] : self[1]\n\tend\nend\n\nn, m = gets.split.map(&:to_i)\ndict = {}\nm.times do\n\ta, b = gets.split\n\tdict[a] = [a, b].__min\nend\nputs gets.split.map{|w| dict[w]}.join(' ')\n"}], "negative_code": [{"source_code": "hash = Hash.new;\nn, m = gets.chomp.split.map(&:to_i);\n\nm.times do\n a, b = gets.chomp.split;\n hash[a] = b.length < a.length ? b : a; \nend\n\np gets.chomp.split.inject(){ |sum, x|\n # p \"#{sum}, #{x}, #{hash[x]}\";\n sum + \" \" + hash[x];\n}\n"}, {"source_code": "nm = gets.chomp.split\nn = nm[0].to_i\nm = nm[1].to_i\nab = []\na = []\nb = []\nfor i in (0..m-1)\n\tab[i] = gets.chomp.split\n\ta[i] = ab[i][0]\n\tb[i] = ab[i][1]\n\tif a[i].size < b[i].size\n\t\tb[i] = nil\n\tend\nend\nc = gets.chomp.split\n\nfor i in (0..n-1)\n\tif b[a.find_index(c[i])] == nil\n\t\tprint a[a.find_index(c[i])] + \" \"\n\telse\n\t\tprint b[a.find_index(c[i])] + \" \"\n\tend\nend"}, {"source_code": "n, m = gets.chomp.split(' ').map(&:to_i)\na = {}\nm.times do\n ai, bi = gets.chomp.split ' '\n a[ai] = bi\nend\n\nres = ''\n\ngets.chomp.split(' ').each do |s|\n res += (s.length >= a[s].length ? s : a[s]) + ' '\nend\nputs res\n"}, {"source_code": "\nline = gets.chop!.split(\" \")\n\nn = line[0].to_i\nm = line[1].to_i\n\nA = {}\nB = {}\n\nfor i in (1 .. m)\n line = gets.chop!.split(\" \")\n\n first = line[0]\n second = line[1]\n\n B[first] = i\n B[second] = i\n\n if second.size < first.size\n A[i] = second\n elsif second.size > first.size\n A[i] = first\n else\n if second < first\n A[i] = second\n else\n A[i] = first\n end\n end\nend\n\nline = gets.chop!.split(\" \")\n\nfor i in (1 .. n)\n print A[B[line[i-1]]]\n\n if i < n\n print \" \"\n end\nend\n"}, {"source_code": "\nline = gets.chop!.split(\" \")\n\nn = line[0].to_i\nm = line[1].to_i\n\nA = {}\nB = {}\n\nfor i in (1 .. m)\n line = gets.chop!.split(\" \")\n\n first = line[0]\n second = line[1]\n\n B[first] = i\n B[second] = i\n\n if second < first\n A[i] = second\n else\n A[i] = first\n end\nend\n\nline = gets.chop!.split(\" \")\n\nfor i in (1 .. n)\n print A[B[line[i-1]]]\n\n if i < n\n print \" \"\n end\nend\n"}, {"source_code": "# http://codeforces.com/problemset/problem/499/B\n\nn, m = gets.chomp.split.map(&:to_i)\nlang_1 = []\nlang_2 = []\n\nm.times do\n words = gets.chomp.split\n lang_1 << words[0]\n lang_2 << words[1]\nend\n\ntext_str = gets.chomp\ntext_arr = text_str.split\n\nfor i in 0..(m - 1)\n if !text_arr.include?(lang_1[i])\n next\n end\n\n word = lang_1[i].length <= lang_2[i].length ? lang_1[i] : lang_2[i]\n text_str.gsub!(lang_1[i], word)\nend\n\nputs text_str\n"}], "src_uid": "edd556d60de89587f1b8daa893538530"} {"nl": {"description": "You are given two rectangles on a plane. The centers of both rectangles are located in the origin of coordinates (meaning the center of the rectangle's symmetry). The first rectangle's sides are parallel to the coordinate axes: the length of the side that is parallel to the Ox axis, equals w, the length of the side that is parallel to the Oy axis, equals h. The second rectangle can be obtained by rotating the first rectangle relative to the origin of coordinates by angle α. Your task is to find the area of the region which belongs to both given rectangles. This region is shaded in the picture.", "input_spec": "The first line contains three integers w, h, α (1 ≤ w, h ≤ 106; 0 ≤ α ≤ 180). Angle α is given in degrees.", "output_spec": "In a single line print a real number — the area of the region which belongs to both given rectangles. The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 6.", "sample_inputs": ["1 1 45", "6 4 30"], "sample_outputs": ["0.828427125", "19.668384925"], "notes": "NoteThe second sample has been drawn on the picture above."}, "positive_code": [{"source_code": "ww, hh, alpha = gets.split(' ').map{ |e| e.to_i}\n\nif (ww < hh)\n\tw = hh.to_f\n\th = ww.to_f\nelse\n\tw = ww.to_f\n\th = hh.to_f\nend\n# w >= h\nalpha = 180 - alpha if alpha > 90\nalpha = alpha * Math::PI / 180\ntheta = 2 * Math.atan(h / w)\n\nif alpha == 0\n\tputs w * h\nelsif alpha < theta\n\tsa = Math.sin(alpha)\n\tca = Math.cos(alpha)\n\tputs (w * h - (1-ca)*(h*h+w*w-2*h*w*sa)/(2*sa*ca))\nelse\n\tputs h * h / Math.sin(alpha)\nend\n"}], "negative_code": [{"source_code": "ww, hh, alpha = gets.split(' ').map{ |e| e.to_i}\n\nif (ww < hh)\n\tw = hh.to_f\n\th = ww.to_f\nelse\n\tw = ww.to_f\n\th = hh.to_f\nend\n# w >= h\nalpha = 180 - alpha if alpha > 90\nalpha = alpha * Math::PI / 180\ntheta = 360 * Math.atan(h / w) / Math::PI\n\nif alpha < theta\n\tsa = Math.sin(alpha)\n\tca = Math.cos(alpha)\n\tputs (w * h - (1-ca)*(h*h+w*w-2*h*w*sa)/(2*sa*ca))\nelse\n\tputs h * h / Math.sin(alpha)\nend\n"}, {"source_code": "ww, hh, alpha = gets.split(' ').map{ |e| e.to_i}\n\nif (ww < hh)\n\tw = hh.to_f\n\th = ww.to_f\nelse\n\tw = ww.to_f\n\th = hh.to_f\nend\n# w >= h\nalpha = 180 - alpha if alpha > 90\nalpha = alpha * Math::PI / 180\ntheta = 360 * Math.atan(h / w) / Math::PI\n\nif alpha == 0\n\tputs w * h\nelsif alpha < theta\n\tsa = Math.sin(alpha)\n\tca = Math.cos(alpha)\n\tputs (w * h - (1-ca)*(h*h+w*w-2*h*w*sa)/(2*sa*ca))\nelse\n\tputs h * h / Math.sin(alpha)\nend\n"}, {"source_code": "ww, hh, alpha = gets.split(' ').map{ |e| e.to_i}\n\nif (ww < hh)\n\tw = hh.to_f\n\th = ww.to_f\nelse\n\tw = ww.to_f\n\th = hh.to_f\nend\n# w >= h\nalpha = 180 - alpha if alpha > 90\nalpha = alpha * Math::PI / 180\ntheta = 2 * Math.atan(h / w)\n\nif alpha == 0\n\tputs w * h\nelsif alpha < theta\n\tsa = Math.sin(alpha)\n\tca = Math.cos(alpha)\n\tputs (w * h - (1-ca)*(h*h+w*w-2*h*w*sa)/(2*sa*ca))\nelse\n\tputs 'here'\n\tputs h * h / Math.sin(alpha)\nend\n"}], "src_uid": "21432a74b063b008cf9f04d2804c1c3f"} {"nl": {"description": "The teacher gave Anton a large geometry homework, but he didn't do it (as usual) as he participated in a regular round on Codeforces. In the task he was given a set of n lines defined by the equations y = ki·x + bi. It was necessary to determine whether there is at least one point of intersection of two of these lines, that lays strictly inside the strip between x1 < x2. In other words, is it true that there are 1 ≤ i < j ≤ n and x', y', such that: y' = ki * x' + bi, that is, point (x', y') belongs to the line number i; y' = kj * x' + bj, that is, point (x', y') belongs to the line number j; x1 < x' < x2, that is, point (x', y') lies inside the strip bounded by x1 < x2. You can't leave Anton in trouble, can you? Write a program that solves the given task.", "input_spec": "The first line of the input contains an integer n (2 ≤ n ≤ 100 000) — the number of lines in the task given to Anton. The second line contains integers x1 and x2 ( - 1 000 000 ≤ x1 < x2 ≤ 1 000 000) defining the strip inside which you need to find a point of intersection of at least two lines. The following n lines contain integers ki, bi ( - 1 000 000 ≤ ki, bi ≤ 1 000 000) — the descriptions of the lines. It is guaranteed that all lines are pairwise distinct, that is, for any two i ≠ j it is true that either ki ≠ kj, or bi ≠ bj.", "output_spec": "Print \"Yes\" (without quotes), if there is at least one intersection of two distinct lines, located strictly inside the strip. Otherwise print \"No\" (without quotes).", "sample_inputs": ["4\n1 2\n1 2\n1 0\n0 1\n0 2", "2\n1 3\n1 0\n-1 3", "2\n1 3\n1 0\n0 2", "2\n1 3\n1 0\n0 3"], "sample_outputs": ["NO", "YES", "YES", "NO"], "notes": "NoteIn the first sample there are intersections located on the border of the strip, but there are no intersections located strictly inside it. "}, "positive_code": [{"source_code": "n = gets.chomp.to_i\nx1, x2 = gets.chomp.split(\" \").map(&:to_i)\nx1 += 1.0e-10\nx2 -= 1.0e-10\nary = []\nn.times do |i|\n k, b = gets.chomp.split(\" \").map(&:to_f)\n ary << [i, (x1)*k+b, (x2)*k+b]\nend\nary1 = ary.sort_by{|v| v[1]}\nary2 = ary.sort_by{|v| v[2]}\nif ary1 == ary2 then\n puts \"NO\"\nelse\n puts \"YES\"\nend\n"}], "negative_code": [{"source_code": "n = gets.chomp.to_i\nx1, x2 = gets.chomp.split(\" \").map(&:to_i)\nary = []\nn.times do |i|\n k, b = gets.chomp.split(\" \").map(&:to_f)\n ary << [i, k, b]\nend\nary1 = ary.sort{|a, b| (x1*a[1]+a[2] <=> x1*b[1]+b[2]).nonzero? || (x1 + 0.1)*a[1]+a[2] <=> (x1 + 0.1)*b[1]+ b[2]}\nary2 = ary.sort{|a, b| (x1*a[1]+a[2] <=> x1*b[1]+b[2]).nonzero? || (x2 - 0.1)*a[1]+a[2] <=> (x2 - 0.1)*b[1]+ b[2]}\nif ary1 == ary2 then\n puts \"NO\"\nelse\n puts \"YES\"\nend\n"}, {"source_code": "n = gets.chomp.to_i\nx1, x2 = gets.chomp.split(\" \").map(&:to_i)\nary = []\nn.times do |i|\n k, b = gets.chomp.split(\" \").map(&:to_f)\n ary << [i, k, b]\nend\nary1 = ary.sort{|a, b| (x1*a[1]+a[2] <=> x1*b[1]+b[2]).nonzero? || (x1 + 0.1)*a[1]+a[2] <=> (x1 + 0.1)*b[1]+ b[2]}\nary2 = ary.sort{|a, b| (x2*a[1]+a[2] <=> x2*b[1]+b[2]).nonzero? || (x2 - 0.1)*a[1]+a[2] <=> (x2 - 0.1)*b[1]+ b[2]}\np ary\np ary1\np ary2\nif ary1 == ary2 then\n puts \"NO\"\nelse\n puts \"YES\"\nend\n"}, {"source_code": "n = gets.chomp.to_i\nx1, x2 = gets.chomp.split(\" \").map(&:to_i)\nary = []\nn.times do |i|\n k, b = gets.chomp.split(\" \").map(&:to_f)\n y1 = k * x1 + b\n y2 = k * x2 + b\n ary << [i, y1, y2]\nend\nary1 = ary.sort{|a, b| a[1] <=> b[1]}\nary2 = ary.sort{|a, b| a[2] <=> b[2]}\nif ary1 == ary2 then\n puts \"NO\"\nelse\n puts \"YES\"\nend\n"}, {"source_code": "n = gets.chomp.to_i\nx1, x2 = gets.chomp.split(\" \").map(&:to_i)\nary = []\nn.times do |i|\n k, b = gets.chomp.split(\" \").map(&:to_f)\n ary << [i, k, b]\nend\nary1 = ary.sort_by{|v| (x1*v[1]+v[2]).nonzero? || (x1 + 0.1)*v[1]+v[2]}\nary2 = ary.sort_by{|v| (x2*v[1]+v[2]).nonzero? || (x2 - 0.1)*v[1]+v[2]}\nif ary1 == ary2 then\n puts \"NO\"\nelse\n puts \"YES\"\nend\n"}, {"source_code": "n = gets.chomp.to_i\nx1, x2 = gets.chomp.split(\" \").map(&:to_i)\nary = []\nn.times do |i|\n k, b = gets.chomp.split(\" \").map(&:to_f)\n ary << [i, (x1 + 0.00001)*k+b, (x2 - 0.00001)*k+b]\nend\nary1 = ary.sort_by{|v| v[1]}\nary2 = ary.sort_by{|v| v[2]}\nif ary1 == ary2 then\n puts \"NO\"\nelse\n puts \"YES\"\nend\n"}, {"source_code": "n = gets.chomp.to_i\nx1, x2 = gets.chomp.split(\" \").map(&:to_i)\nary = []\nn.times do |i|\n k, b = gets.chomp.split(\" \").map(&:to_f)\n ary << [i, (x1 + 0.0001)*k+b, (x2 - 0.0001)*k+b]\nend\nary1 = ary.sort_by{|v| v[1]}\nary2 = ary.sort_by{|v| v[2]}\nif ary1 == ary2 then\n puts \"NO\"\nelse\n puts \"YES\"\nend\n"}, {"source_code": "n = gets.chomp.to_i\nx1, x2 = gets.chomp.split(\" \").map(&:to_i)\nary = []\nn.times do |i|\n k, b = gets.chomp.split(\" \").map(&:to_f)\n ary << [i, (x1 + 0.000001)*k+b, (x2 - 0.000001)*k+b]\nend\nary1 = ary.sort_by{|v| v[1]}\nary2 = ary.sort_by{|v| v[2]}\nif ary1 == ary2 then\n puts \"NO\"\nelse\n puts \"YES\"\nend\n"}], "src_uid": "8b61213e2ce76b938aa68ffd1e4c1429"} {"nl": {"description": "You are given a string $$$s$$$ consisting only of lowercase Latin letters.You can rearrange all letters of this string as you wish. Your task is to obtain a good string by rearranging the letters of the given string or report that it is impossible to do it.Let's call a string good if it is not a palindrome. Palindrome is a string which is read from left to right the same as from right to left. For example, strings \"abacaba\", \"aa\" and \"z\" are palindromes and strings \"bba\", \"xd\" are not.You have to answer $$$t$$$ independent queries.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — number of queries. Each of the next $$$t$$$ lines contains one string. The $$$i$$$-th line contains a string $$$s_i$$$ consisting only of lowercase Latin letter. It is guaranteed that the length of $$$s_i$$$ is from $$$1$$$ to $$$1000$$$ (inclusive).", "output_spec": "Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: -1 if it is impossible to obtain a good string by rearranging the letters of $$$s_i$$$ and any good string which can be obtained from the given one (by rearranging the letters) otherwise.", "sample_inputs": ["3\naa\nabacaba\nxdd"], "sample_outputs": ["-1\nabaacba\nxdd"], "notes": "NoteIn the first query we cannot rearrange letters to obtain a good string.Other examples (not all) of correct answers to the second query: \"ababaca\", \"abcabaa\", \"baacaba\".In the third query we can do nothing to obtain a good string."}, "positive_code": [{"source_code": "gets;puts$<.map{|e|e=e.chomp.chars.sort*'';e[0] 1\n x_chars = x.chars\n if x_chars == x.chars.reverse\n while true do\n x_chars.shuffle!\n if x_chars != x_chars.reverse\n res = x_chars.join\n break\n end\n end\n else\n res = x_chars.join\n end\n end\n results << res\nend\n\nn.times do |x|\n puts results[x]\nend\n"}, {"source_code": "n = gets.to_i\nn.times do\n s = gets.chomp\n idx = nil\n flag = false\n s.length.times do |i|\n if s[i] != s[0]\n flag = true\n idx = i\n end\n end\n if flag && s == s.reverse\n t = s[0]\n s[0] = s[idx]\n s[idx] = t\n end\n puts flag ? s : -1\nend\n"}, {"source_code": "def solve(s)\n if s.split('').uniq.size == 1\n -1\n else\n s.split('').sort.join\n end\nend\n\ngets.strip.to_i.times do\n puts solve(gets.strip)\nend"}], "negative_code": [{"source_code": "gets;puts$<.map{|e|e.chomp!;f=e.chars.sort*'';f 1\n x_chars = x.chars\n if x_chars == x.chars.reverse\n while true do\n x_chars.shuffle!\n if x_chars != x.chars.reverse\n res = x_chars.join\n break\n end\n end\n else\n res = x_chars.join\n end\n end\n results << res\nend\n\nn.times do |x|\n puts results[x]\nend\n"}, {"source_code": "n = gets.strip.to_i\nresults = []\nn.times do\n x = gets.strip\n res = -1\n if x.chars.uniq.length > 1\n x_chars = x.chars\n if x_chars == x.chars.reverse\n while true do\n x_chars.shuffle!\n if x_chars != x.chars.reverse\n res = x_chars.join\n break\n end\n end\n else\n res = x_chars.join\n end\n end\n results << res\nend\n\nn.times do |x|\n puts results[x]\nend\n"}], "src_uid": "b9f0c38c6066bdafa2e4c6daf7f46816"} {"nl": {"description": "A flowerbed has many flowers and two fountains.You can adjust the water pressure and set any values r1(r1 ≥ 0) and r2(r2 ≥ 0), giving the distances at which the water is spread from the first and second fountain respectively. You have to set such r1 and r2 that all the flowers are watered, that is, for each flower, the distance between the flower and the first fountain doesn't exceed r1, or the distance to the second fountain doesn't exceed r2. It's OK if some flowers are watered by both fountains.You need to decrease the amount of water you need, that is set such r1 and r2 that all the flowers are watered and the r12 + r22 is minimum possible. Find this minimum value.", "input_spec": "The first line of the input contains integers n, x1, y1, x2, y2 (1 ≤ n ≤ 2000,  - 107 ≤ x1, y1, x2, y2 ≤ 107) — the number of flowers, the coordinates of the first and the second fountain. Next follow n lines. The i-th of these lines contains integers xi and yi ( - 107 ≤ xi, yi ≤ 107) — the coordinates of the i-th flower. It is guaranteed that all n + 2 points in the input are distinct.", "output_spec": "Print the minimum possible value r12 + r22. Note, that in this problem optimal answer is always integer.", "sample_inputs": ["2 -1 0 5 3\n0 2\n5 2", "4 0 0 5 0\n9 4\n8 3\n-1 0\n1 4"], "sample_outputs": ["6", "33"], "notes": "NoteThe first sample is (r12 = 5, r22 = 1): The second sample is (r12 = 1, r22 = 32): "}, "positive_code": [{"source_code": "n, x1, y1, x2, y2 = gets.split(' ').map(&:to_i)\n\nfs = []\nfor i in 0...n\n fx, fy = gets.split(' ').map(&:to_i)\n fs << [fx, fy]\nend\n\nds = []\nfor j in 0...n\n fx, fy = fs[j]\n d1 = (fx-x1)**2+(fy-y1)**2\n d2 = (fx-x2)**2+(fy-y2)**2\n ds << [d1, d2]\nend\nds << [0,0]\n\nmin = Float::INFINITY\nfor dv in ds\n d1 = dv[0]\n d2 = 0\n for dw in ds\n if dw[0] > d1\n d2 = [dw[1], d2].max\n end\n end\n \n min = [min, d1+d2].min\nend\n\nputs min"}, {"source_code": "n, x1, y1, x2, y2 = gets.chomp.split(\" \").map(&:to_i)\ndist = []\n\nn.times do\n fx, fy = gets.chomp.split(\" \").map(&:to_i)\n d1 = (x1 - fx)**2 + (y1 - fy)**2\n d2 = (x2 - fx)**2 + (y2 - fy)**2\n dist << [d1, d2]\nend\ndist << [0,0]\n\nres = Float::INFINITY\ndist.each do |i|\n r1 = i[0]\n r2 = 0\n dist.each do |j|\n if j[0] > r1 then\n r2 = [r2, j[1]].max\n end\n end\n res = [res, r1 + r2].min\nend\n\nputs res\n"}], "negative_code": [{"source_code": "n, x1, y1, x2, y2 = gets.split(' ').map(&:to_i)\nmd1 = md2 = 0\nfor i in 0...n\n fx, fy = gets.split(' ').map(&:to_i)\n d1 = (fx-x1)**2+(fy-y1)**2\n d2 = (fx-x2)**2+(fy-y2)**2\n if d1-md1 < d2-md2\n # closer to r1\n md1 = [md1, d1].max\n else\n # closer to r2\n md2 = [md2, d2].max\n end\nend\n\nputs md1 + md2"}, {"source_code": "n, x1, y1, x2, y2 = gets.split(' ').map(&:to_i)\n\nfs = []\nfor i in 0...n\n fx, fy = gets.split(' ').map(&:to_i)\n fs << [fx, fy]\nend\n\nmin = Float::INFINITY\n\nmd1 = md2 = 0\nfor j in 0...n\n fx, fy = fs[j]\n d1 = (fx-x1)**2+(fy-y1)**2\n d2 = (fx-x2)**2+(fy-y2)**2\n if d1-md1 < d2-md2\n # closer to r1\n md1 = [md1, d1].max\n else\n # closer to r2\n md2 = [md2, d2].max\n end\nend\nmin = [min, md1 + md2].min\n\nmd1 = md2 = 0\nfor j in 1..n\n fx, fy = fs[n-j]\n d1 = (fx-x1)**2+(fy-y1)**2\n d2 = (fx-x2)**2+(fy-y2)**2\n if d1-md1 < d2-md2\n # closer to r1\n md1 = [md1, d1].max\n else\n # closer to r2\n md2 = [md2, d2].max\n end\nend\nmin = [min, md1 + md2].min\n\nputs min"}, {"source_code": "n, x1, y1, x2, y2 = gets.chomp.split(\" \").map(&:to_i)\ndist = []\n\nn.times do\n fx, fy = gets.chomp.split(\" \").map(&:to_i)\n d1 = (x1 - fx)**2 + (y1 - fy)**2\n d2 = (x2 - fx)**2 + (y2 - fy)**2\n dist << [d1, d2]\nend\n\nres = 10**15\ndist.each do |i|\n r1 = i[0]\n r2 = 0\n dist.each do |j|\n if j[0] > r1 then\n r2 = [r2, j[1]].max\n end\n end\n res = [res, r1 + r2].min\nend\nputs res\n"}, {"source_code": "n, x1, y1, x2, y2 = gets.chomp.split(\" \").map(&:to_i)\ndist = []\n\nn.times do\n fx, fy = gets.chomp.split(\" \").map(&:to_i)\n d1 = (x1 - fx)**2 + (y1 - fy)**2\n d2 = (x2 - fx)**2 + (y2 - fy)**2\n dist << [d1, d2]\nend\n\nres = 10**8\ndist.each do |i|\n r1 = i[0]\n r2 = 0\n dist.each do |j|\n if j[0] > r1 then\n r2 = [r2, j[1]].max\n end\n end\n res = [res, r1 + r2].min\nend\nputs res\n"}], "src_uid": "5db9c5673a04256c52f180dbfca2a52f"} {"nl": {"description": "Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are labeled from 1 to k. Balls of the same color are indistinguishable. He draws balls from the bag one by one until the bag is empty. He noticed that he drew the last ball of color i before drawing the last ball of color i + 1 for all i from 1 to k - 1. Now he wonders how many different ways this can happen. ", "input_spec": "The first line of input will have one integer k (1 ≤ k ≤ 1000) the number of colors. Then, k lines will follow. The i-th line will contain ci, the number of balls of the i-th color (1 ≤ ci ≤ 1000). The total number of balls doesn't exceed 1000.", "output_spec": "A single integer, the number of ways that Kyoya can draw the balls from the bag as described in the statement, modulo 1 000 000 007. ", "sample_inputs": ["3\n2\n2\n1", "4\n1\n2\n3\n4"], "sample_outputs": ["3", "1680"], "notes": "NoteIn the first sample, we have 2 balls of color 1, 2 balls of color 2, and 1 ball of color 3. The three ways for Kyoya are: 1 2 1 2 31 1 2 2 32 1 1 2 3"}, "positive_code": [{"source_code": "# TEMPLATE BEGIN\ndef read_vector\n gets.chomp.split(/\\s+/)\nend\ndef read_vector_int\n read_vector.map(&:to_i)\nend\n# TEMPLATE END\n\n$mod = 1_000_000_007\n$maxn = 1010\n\ndef mypow(a, b)\n res = 1\n while b > 0\n res = res * a % $mod if b % 2 == 1\n a = a * a % $mod\n b /= 2\n end\n res\nend\n\ndef ceshka(a, b)\n $fuck[a] * $rfuck[b] * $rfuck[a-b] % $mod\nend\n\n$fuck, $rfuck = [1], [1]\n(1..$maxn).each.map do |i|\n $fuck.push $fuck[i-1] * i % $mod\n $rfuck.push mypow($fuck[i], $mod - 2)\nend\n\nk = read_vector_int.first\na = k.times.map{ read_vector_int.first }\n\nres, sum = 1, 0\nk.times do |i|\n sum += a[i]\n res = res * ceshka(sum - 1, a[i] - 1) % $mod\nend\nputs res\n\n"}, {"source_code": "\nk = gets().chomp().to_i\n\nn = gets().chomp().to_i\ncount = 1\nmod = 1000000007\n(k-1).times {\n c = gets().chomp().to_i\n p=c-1\n t1=1\n t2=1\n for i in(1..p)\n t1 *= n+i\n #t1 %= mod\n t2 *= i\n #t2 %= mod\n end\n\n count *= t1/t2\n count %= mod\n n+=c\n}\n\nputs count\n\n"}], "negative_code": [{"source_code": "k = gets().chomp().to_i\n\nn = gets().chomp().to_i\ncount = 1\nmod = 1000000007\n(k-1).times {\n c = gets().chomp().to_i\n p=c-1\n t1=1\n t2=1\n for i in(1..p)\n t1 *= n+i\n t1 %= mod\n t2 *= i\n t2 %= mod\n end\n\n count *= t1/t2\n n+=c\n}\n\nputs count\n\n"}], "src_uid": "faf2c92c3a61ba5e33160bc7a18ad928"} {"nl": {"description": "You are given an integer $$$n$$$. Find any string $$$s$$$ of length $$$n$$$ consisting only of English lowercase letters such that each non-empty substring of $$$s$$$ occurs in $$$s$$$ an odd number of times. If there are multiple such strings, output any. It can be shown that such string always exists under the given constraints.A string $$$a$$$ is a substring of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 500$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$). It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$3 \\cdot 10^5$$$.", "output_spec": "For each test case, print a single line containing the string $$$s$$$. If there are multiple such strings, output any. It can be shown that such string always exists under the given constraints.", "sample_inputs": ["4\n3\n5\n9\n19"], "sample_outputs": ["abc\ndiane\nbbcaabbba\nyouarethecutestuwuu"], "notes": "NoteIn the first test case, each substring of \"abc\" occurs exactly once.In the third test case, each substring of \"bbcaabbba\" occurs an odd number of times. In particular, \"b\" occurs $$$5$$$ times, \"a\" and \"bb\" occur $$$3$$$ times each, and each of the remaining substrings occurs exactly once."}, "positive_code": [{"source_code": "gets\r\nwhile n = gets&.to_i\r\n m = n / 2\r\n puts n == 1 ? 'a' : 'a' * m + 'b' + 'a' * (m - 1) + 'c' * n[0]\r\nend"}], "negative_code": [], "src_uid": "21e361d7f907f2543a616c901e60c6f2"} {"nl": {"description": "Igor is in 11th grade. Tomorrow he will have to write an informatics test by the strictest teacher in the school, Pavel Denisovich. Igor knows how the test will be conducted: first of all, the teacher will give each student two positive integers $$$a$$$ and $$$b$$$ ($$$a < b$$$). After that, the student can apply any of the following operations any number of times: $$$a := a + 1$$$ (increase $$$a$$$ by $$$1$$$), $$$b := b + 1$$$ (increase $$$b$$$ by $$$1$$$), $$$a := a \\ | \\ b$$$ (replace $$$a$$$ with the bitwise OR of $$$a$$$ and $$$b$$$). To get full marks on the test, the student has to tell the teacher the minimum required number of operations to make $$$a$$$ and $$$b$$$ equal.Igor already knows which numbers the teacher will give him. Help him figure out what is the minimum number of operations needed to make $$$a$$$ equal to $$$b$$$.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 10^4$$$). Description of the test cases follows. The only line for each test case contains two integers $$$a$$$ and $$$b$$$ ($$$1 \\le a < b \\le 10^6$$$). It is guaranteed that the sum of $$$b$$$ over all test cases does not exceed $$$10^6$$$.", "output_spec": "For each test case print one integer — the minimum required number of operations to make $$$a$$$ and $$$b$$$ equal.", "sample_inputs": ["5\n1 3\n5 8\n2 5\n3 19\n56678 164422"], "sample_outputs": ["1\n3\n2\n1\n23329"], "notes": "NoteIn the first test case, it is optimal to apply the third operation.In the second test case, it is optimal to apply the first operation three times.In the third test case, it is optimal to apply the second operation and then the third operation."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n a, b = gets.split.map(&:to_i)\r\n p (0..(b - a)).flat_map { |i|\r\n new_a = a + i\r\n new_b = b + i\r\n [i + (new_a == b ? 0 : 1 + (new_a | b) - b),\r\n i + 1 + (a | new_b) - new_b]\r\n }.min\r\nend\r\n"}], "negative_code": [], "src_uid": "eed751c881767ecd978a728d980da594"} {"nl": {"description": "A bracket sequence is a string containing only characters \"(\" and \")\". A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters \"1\" and \"+\" between the original characters of the sequence. For example, bracket sequences \"()()\" and \"(())\" are regular (the resulting expressions are: \"(1)+(1)\" and \"((1+1)+1)\"), and \")(\", \"(\" and \")\" are not.Subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.You are given a regular bracket sequence $$$s$$$ and an integer number $$$k$$$. Your task is to find a regular bracket sequence of length exactly $$$k$$$ such that it is also a subsequence of $$$s$$$.It is guaranteed that such sequence always exists.", "input_spec": "The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 \\le k \\le n \\le 2 \\cdot 10^5$$$, both $$$n$$$ and $$$k$$$ are even) — the length of $$$s$$$ and the length of the sequence you are asked to find. The second line is a string $$$s$$$ — regular bracket sequence of length $$$n$$$.", "output_spec": "Print a single string — a regular bracket sequence of length exactly $$$k$$$ such that it is also a subsequence of $$$s$$$. It is guaranteed that such sequence always exists.", "sample_inputs": ["6 4\n()(())", "8 8\n(()(()))"], "sample_outputs": ["()()", "(()(()))"], "notes": null}, "positive_code": [{"source_code": "N, K = gets.split.map(&:to_i)\nstr = gets.chomp\nuse = []\ncnt = 0\nN.times do |i|\n if str[i] == '(' && cnt < K/2\n use[i] = true\n cnt += 1\n end\nend\ncnt = 0\n(N-1).downto(0) do |i|\n if str[i] == ')' && cnt < K/2\n use[i] = true\n cnt += 1\n end\nend\n\nans = ''\nN.times do |i|\n ans << str[i] if use[i]\nend\nputs ans"}], "negative_code": [], "src_uid": "c783434bb17819344fb9a49de1f63708"} {"nl": {"description": "There are $$$n$$$ athletes in front of you. Athletes are numbered from $$$1$$$ to $$$n$$$ from left to right. You know the strength of each athlete — the athlete number $$$i$$$ has the strength $$$s_i$$$.You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team.You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams $$$A$$$ and $$$B$$$ so that the value $$$|\\max(A) - \\min(B)|$$$ is as small as possible, where $$$\\max(A)$$$ is the maximum strength of an athlete from team $$$A$$$, and $$$\\min(B)$$$ is the minimum strength of an athlete from team $$$B$$$.For example, if $$$n=5$$$ and the strength of the athletes is $$$s=[3, 1, 2, 6, 4]$$$, then one of the possible split into teams is: first team: $$$A = [1, 2, 4]$$$, second team: $$$B = [3, 6]$$$. In this case, the value $$$|\\max(A) - \\min(B)|$$$ will be equal to $$$|4-3|=1$$$. This example illustrates one of the ways of optimal split into two teams.Print the minimum value $$$|\\max(A) - \\min(B)|$$$.", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case consists of two lines. The first line contains positive integer $$$n$$$ ($$$2 \\le n \\le 50$$$) — number of athletes. The second line contains $$$n$$$ positive integers $$$s_1, s_2, \\ldots, s_n$$$ ($$$1 \\le s_i \\le 1000$$$), where $$$s_i$$$ — is the strength of the $$$i$$$-th athlete. Please note that $$$s$$$ values may not be distinct.", "output_spec": "For each test case print one integer — the minimum value of $$$|\\max(A) - \\min(B)|$$$ with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams.", "sample_inputs": ["5\n5\n3 1 2 6 4\n6\n2 1 3 2 4 3\n4\n7 9 3 1\n2\n1 1000\n3\n100 150 200"], "sample_outputs": ["1\n0\n2\n999\n50"], "notes": "NoteThe first test case was explained in the statement. In the second test case, one of the optimal splits is $$$A=[2, 1]$$$, $$$B=[3, 2, 4, 3]$$$, so the answer is $$$|2-2|=0$$$."}, "positive_code": [{"source_code": "gets.to_i.times do\n\tn = gets.to_i\n\ta = gets.split.map(&:to_i).sort\n\tputs a.each_cons(2).map { _2 - _1 }.min\nend\n"}, {"source_code": "gets.strip.to_i.times do |t|\n _ = gets.strip.to_i\n vals = gets.strip.split(' ').map(&:to_i).sort\n mn = vals[1] - vals[0]\n a = vals[0]\n b = vals[1]\n vals.each_with_index do |e, i|\n next if i.zero?\n if vals[i] - vals[i - 1] < mn\n mn = vals[i] - vals[i-1]\n a = vals[i-1]\n b = vals[i]\n end\n end\n puts (a - b).abs\nend\n\n"}, {"source_code": "t = gets.chomp.to_i\nt.times do |testcase|\n n = gets.chomp.to_i\n arr = gets.split.map(&:to_i)\n arr = arr.sort ; min_val = 1000000\n (arr.size-1).times do |i|\n min_val = [min_val , arr[i+1]-arr[i]].min\n end\n puts min_val\nend"}, {"source_code": "t = gets.to_i\n\n1.upto t do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n a.sort!\n diff = a[1] - a[0]\n 1.upto a.length-1 do |i|\n diff = [diff, a[i]-a[i-1]].min\n end \n puts diff\nend"}, {"source_code": "gets.to_i.times {\n gets\n puts gets.split.map(&:to_i).sort.each_cons(2).map{|x,y|y-x}.min\n}\n"}, {"source_code": "gets.to_i.times {\n gets\n data = gets.split.map { |v| v.to_i }.sort_by {|v| v}\n\n m = data[1] - data[0]\n for i in 2...data.size\n d = data[i] - data[i-1]\n if d < m\n m = d\n end\n end\n\n puts m\n}\n"}, {"source_code": "def solve\n _ = gets\n arr = gets.strip.split(' ').map(&:to_i).sort\n diff = 1001\n arr.each_with_index { |el, i|\n next if i.zero?\n diff = [diff, arr[i] - arr[i - 1]].min\n }\n puts diff\n\nend\n\n\ngets.strip.to_i.times do\n solve\nend\n"}, {"source_code": "t = gets.to_i\nt.times do\n n = gets.to_i\n s = gets.split.map(&:to_i).sort\n min_diff = s[-1] - s[0]\n for i in 0...(n - 1) do\n diff = s[i + 1] - s[i]\n if diff < min_diff\n min_diff = diff\n end\n end\n puts min_diff\nend\n"}, {"source_code": "gets.to_i.times do\n gets\n a = gets.split(' ').map(&:to_i).sort\n\n result = 1_000_000\n while a.size > 1\n x = a.shift\n\n result = [a.first - x, result].min\n end\n\n puts result\nend\n"}, {"source_code": "n = gets.chomp.to_i\nn.times {\n arrNum = gets.chomp.to_i\n userstr = gets.chomp\n arr = userstr.split().map(&:to_i).sort\n result = arr.each_cons(2).map {|a,b| b-a}\n p result.min\n}"}], "negative_code": [], "src_uid": "d2669f85bdb59715352c6bc3d7ba9652"} {"nl": {"description": "The robot is located on a checkered rectangular board of size $$$n \\times m$$$ ($$$n$$$ rows, $$$m$$$ columns). The rows in the board are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and the columns — from $$$1$$$ to $$$m$$$ from left to right.The robot is able to move from the current cell to one of the four cells adjacent by side.The sequence of commands $$$s$$$ executed by the robot is given. Each command is denoted by one of the symbols 'L', 'R', 'D' or 'U', and triggers the movement to left, right, down or up, respectively.The robot can start its movement in any cell. The robot executes the commands starting from the first one, strictly in the order in which they are listed in $$$s$$$. If the robot moves beyond the edge of the board, it falls and breaks. A command that causes the robot to break is not considered successfully executed.The robot's task is to execute as many commands as possible without falling off the board. For example, on board $$$3 \\times 3$$$, if the robot starts a sequence of actions $$$s=$$$\"RRDLUU\" (\"right\", \"right\", \"down\", \"left\", \"up\", \"up\") from the central cell, the robot will perform one command, then the next command will force him to cross the edge. If the robot starts moving from the cell $$$(2, 1)$$$ (second row, first column) then all commands will be executed successfully and the robot will stop at the cell $$$(1, 2)$$$ (first row, second column). The robot starts from cell $$$(2, 1)$$$ (second row, first column). It moves right, right, down, left, up, and up. In this case it ends in the cell $$$(1, 2)$$$ (first row, second column). Determine the cell from which the robot should start its movement in order to execute as many commands as possible.", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$) — the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. In the description of each test case, the first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\leq n, m \\leq 10^6$$$) — the height and width of the field that the robot is located on. The second line of the description is a string $$$s$$$ consisting solely of characters 'L', 'R', 'D' and 'U' — the sequence of commands the robot executes. The string has a length from $$$1$$$ to $$$10^6$$$ commands. It is guaranteed that the total length of $$$s$$$ over all test cases does not exceed $$$10^6$$$.", "output_spec": "Print $$$t$$$ lines, each of which contains the answer to the corresponding test case. The answer to the test case are two integers $$$r$$$ ($$$1 \\leq r \\leq n$$$) and $$$c$$$ ($$$1 \\leq c \\leq m$$$), separated by a space — the coordinates of the cell (row number and column number) from which the robot should start moving to perform as many commands as possible. If there are several such cells, you may output any of them.", "sample_inputs": ["4\n1 1\nL\n1 2\nL\n3 3\nRRDLUU\n4 3\nLUURRDDLLLUU"], "sample_outputs": ["1 1\n1 2\n2 1\n3 2"], "notes": null}, "positive_code": [{"source_code": "gets.to_i.times do\r\n n,m=gets.split.map(&:to_i)\r\n s=gets.chomp.chars\r\n l,r,u,d=0,0,0,0\r\n lr,ud=1,1\r\n i,j=0,0\r\n s.each do |t|\r\n if t==\"L\"\r\n j-=1\r\n if jr\r\n if lr==m\r\n break\r\n end\r\n r+=1\r\n lr+=1\r\n end\r\n else\r\n i+=1\r\n if i>d\r\n if ud==n\r\n break\r\n end\r\n d+=1\r\n ud+=1\r\n end\r\n end\r\n end\r\n puts [-u+1,-l+1].join(\" \")\r\nend\r\n"}], "negative_code": [{"source_code": "gets.to_i.times do\r\n n,m=gets.split.map(&:to_i)\r\n s=gets.chomp.chars\r\n l,r,u,d=0,0,0,0\r\n lr,ud=1,1\r\n i,j=0,0\r\n s.each do |t|\r\n if t==\"L\"\r\n j-=1\r\n if jr\r\n if lr==m\r\n break\r\n end\r\n r+=1\r\n lr+=1\r\n end\r\n else\r\n i+=1\r\n if i>d\r\n if ud==m\r\n break\r\n end\r\n d+=1\r\n ud+=1\r\n end\r\n end\r\n end\r\n puts [-u+1,-l+1].join(\" \")\r\nend\r\n"}], "src_uid": "585bb4a040144da39ed240366193e705"} {"nl": {"description": "You got a box with a combination lock. The lock has a display showing n digits. There are two buttons on the box, each button changes digits on the display. You have quickly discovered that the first button adds 1 to all the digits (all digits 9 become digits 0), and the second button shifts all the digits on the display one position to the right (the last digit becomes the first one). For example, if the display is currently showing number 579, then if we push the first button, the display will show 680, and if after that we push the second button, the display will show 068.You know that the lock will open if the display is showing the smallest possible number that can be obtained by pushing the buttons in some order. The leading zeros are ignored while comparing numbers. Now your task is to find the desired number.", "input_spec": "The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of digits on the display. The second line contains n digits — the initial state of the display.", "output_spec": "Print a single line containing n digits — the desired state of the display containing the smallest possible number.", "sample_inputs": ["3\n579", "4\n2014"], "sample_outputs": ["024", "0142"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\narr = gets.chomp.split('').map(&:to_i)\n\nmin = arr.join.to_i\nmin_arr = arr\n\n(1..n).each do |i|\n temp_arr = arr.rotate i\n #p temp_arr\n diff = 10 - temp_arr.first\n temp_arr.map! {|x| (x+diff)%10}\n value = temp_arr.join.to_i\n if value < min\n min = value\n min_arr = temp_arr\n end\nend\nputs min_arr.join\n"}, {"source_code": "n = STDIN.readline.to_i\nx = STDIN.readline.strip\ndigits = x.split('')\n\nrot = {}\n10.times do |digit|\n rot[digit.to_s] = ((digit+1) % 10).to_s\nend\n\nnumbers = []\n10.times do\n n.times {|i| digits[i] = rot[digits[i]] }\n n.times do\n digits.rotate!\n numbers.push(digits.join)\n end\nend\nputs numbers.sort[0]\n"}, {"source_code": "\ngets\na = gets.rstrip.chars.map(&:to_i)\nputs (0..a.size-1).map{|i| (a + a)[i, a.size]}\n .map{|x| x.map{|y| (y + 10 - x[0]) % 10}}\n .map{|x| x.join('')}.min\n"}, {"source_code": "pa=Array.new\npb=Array.new\nn=gets.to_i\nstr=gets.to_s\n\nstd=\"9\"\nfor i in 0..9\n min=10\n for j in 0..n-1\n pa[j] = (str[j].to_i + i) % 10\n if min > pa[j]\n min=pa[j]\n r=0\n pb[r]=j\n elsif min==pa[j]\n r+=1\n pb[r]=j\n end\n end\n\n stt=\"9\"\n for t in 0..r\n stk = pa[pb[t],n-pb[t]].join + pa[0,pb[t]].join\n# for j in pb[t]..pb[t]+n-1\n# stk << pa[j%n].to_s\n# end\n#puts stk\n if stt > stk\n stt=stk.clone\n end\n end \n if std > stt\n std=stt.clone\n end\nend\n\nputs std\n"}, {"source_code": "# $f = File.open('input.txt', 'r')\n$f = STDIN\n\ndef inc(s)\n s.length.times{ |i| s[i] = ((s[i].to_i + 1) % 10).to_s }\n s\nend\n\ndef get_min(s)\n res = s.clone\n s.length.times do\n tmp = s.chars.rotate(-1).join\n res = [res, tmp].min\n s = tmp\n end\n res\nend\n\nwhile str = $f.gets\n n = str.chomp.to_i\n s = $f.gets.chomp\n res = s.clone\n 10.times do\n res = [res, get_min(s)].min.clone\n inc(s)\n end\n puts res\nend\n"}], "negative_code": [{"source_code": "def transform arr\n diff = 10 - arr.first\n arr = arr.map {|a| (a + diff)%10}\n arr\nend\n\nn = gets.to_i\narr = gets.chomp.split('').map(&:to_i)\nmin = Float::INFINITY\nnew_min_arr = arr\nnew_min = new_min_arr.join.to_i\nwhile new_min < min\n min_arr = new_min_arr\n min = new_min\n min_index,max_index = min_arr.each_with_index.minmax.collect{|x| x[1]}\n max_rot_arr = transform min_arr.rotate(max_index)\n min_rot_arr = min_arr.rotate(min_index)\n max_rot_arr_val = max_rot_arr.join.to_i\n min_rot_arr_val = min_rot_arr.join.to_i\n # p max_rot_arr\n # p min_rot_arr\n if max_rot_arr_val < min_rot_arr_val\n new_min_arr = max_rot_arr\n new_min = max_rot_arr_val\n else\n new_min_arr = min_rot_arr\n new_min_arr = min_rot_arr_val\n end\n #p new_min_arr\nend\n\nputs min_arr.join"}, {"source_code": "def transform arr\n diff = 10 - arr.first\n arr = arr.map {|a| (a + diff)%10}\n arr\nend\n\nn = gets.to_i\narr = gets.chomp.split('').map(&:to_i)\nmin = Float::INFINITY\nnew_min_arr = arr\nnew_min = new_min_arr.join.to_i\nwhile new_min < min\n min_arr = new_min_arr\n min = new_min\n min_index,max_index = min_arr.each_with_index.minmax.collect{|x| x[1]}\n max_rot_arr = transform min_arr.rotate(max_index)\n min_rot_arr = min_arr.rotate(min_index)\n max_rot_arr_val = max_rot_arr.join.to_i\n min_rot_arr_val = min_rot_arr.join.to_i\n # p max_rot_arr\n # p min_rot_arr\n if max_rot_arr_val < min_rot_arr_val\n new_min_arr = max_rot_arr\n new_min = max_rot_arr_val\n else\n new_min_arr = min_rot_arr\n new_min = min_rot_arr_val\n end\n #p new_min_arr\nend\n\nputs min_arr.join"}, {"source_code": "def transform arr\n diff = 10 - arr.first\n arr = arr.map {|a| (a + diff)%10}\n arr\nend\n\nn = gets.to_i\narr = gets.chomp.split('').map(&:to_i)\nmin = Float::INFINITY\nnew_min_arr = arr\nnew_min = new_min_arr.join.to_i\nwhile new_min < min\n min_arr = new_min_arr\n min = new_min\n max_index,min_index = min_arr.each_with_index.minmax.collect{|x| x[1]}\n max_rot_arr = transform min_arr.rotate(max_index)\n min_rot_arr = min_arr.rotate(min_index)\n max_rot_arr_val = max_rot_arr.join.to_i\n min_rot_arr_val = min_rot_arr.join.to_i\n if max_rot_arr_val < min_rot_arr_val\n new_min_arr = max_rot_arr\n new_min = max_rot_arr_val\n else\n new_min_arr = min_rot_arr\n new_min_arr = min_rot_arr_val\n end\nend\n\nputs min_arr.join"}, {"source_code": "def transform arr\n diff = 10 - arr.first\n arr = arr.map {|a| (a + diff)%10}\n arr\nend\n\ndef rotate arr,mode\n final_value = arr.join.to_i\n final_arr = arr\n (1..arr.size).each do |i|\n temp_arr = arr.rotate(i)\n if mode == \"max\"\n if final_value < temp_arr.join.to_i\n final_value = temp_arr.join.to_i\n final_arr = temp_arr\n end\n else\n if final_value > temp_arr.join.to_i\n final_value = temp_arr.join.to_i\n final_arr = temp_arr\n end\n end\n end\n final_arr\nend\n\nn = gets.to_i\narr = gets.chomp.split('').map(&:to_i)\nmin = Float::INFINITY\nnew_min_arr = arr\nnew_min = new_min_arr.join.to_i\nwhile new_min < min\n min_arr = new_min_arr\n min = new_min\n max_rot_arr = transform(rotate min_arr,\"max\")\n min_rot_arr = rotate min_arr,\"min\"\n max_rot_arr_val = max_rot_arr.join.to_i\n min_rot_arr_val = min_rot_arr.join.to_i\n # p max_rot_arr\n # p min_rot_arr\n if max_rot_arr_val < min_rot_arr_val\n new_min_arr = max_rot_arr\n new_min = max_rot_arr_val\n else\n new_min_arr = min_rot_arr\n new_min = min_rot_arr_val\n end\n #p new_min_arr\nend\n\nputs min_arr.join"}, {"source_code": "pa=Array.new\nstd=String.new\nn=gets.to_i\nstr=gets.to_s\n\nstd=str.clone\nfor i in 0..9\n stt=String.new\n min=0\n for j in 0..n-1\n pa[j]=(str[j].to_i+i)%10\n if pa[min]>pa[j]\n min=j\n end\n end\n# p pa\n for j in min..min+n-1\n stt << pa[j%n].to_s\n end\n# p stt\n if std > stt\n std=stt.clone\n end\nend\n\nputs std\n"}, {"source_code": "n=gets.to_i\nstr=gets.to_s\n\nmin=0\nfor i in 0..n-1\n if str[min]>str[i]\n min=i\n end\nend\nk=str[min].to_i\n\nfor i in min..min+n-1\n print str[i%n].to_i-k\nend\nputs\n"}], "src_uid": "6ee356f2b3a4bb88087ed76b251afec2"} {"nl": {"description": "You find yourself on an unusual crossroad with a weird traffic light. That traffic light has three possible colors: red (r), yellow (y), green (g). It is known that the traffic light repeats its colors every $$$n$$$ seconds and at the $$$i$$$-th second the color $$$s_i$$$ is on.That way, the order of the colors is described by a string. For example, if $$$s=$$$\"rggry\", then the traffic light works as the following: red-green-green-red-yellow-red-green-green-red-yellow- ... and so on.More formally, you are given a string $$$s_1, s_2, \\ldots, s_n$$$ of length $$$n$$$. At the first second the color $$$s_1$$$ is on, at the second — $$$s_2$$$, ..., at the $$$n$$$-th second the color $$$s_n$$$ is on, at the $$$n + 1$$$-st second the color $$$s_1$$$ is on and so on.You need to cross the road and that can only be done when the green color is on. You know which color is on the traffic light at the moment, but you don't know the current moment of time. You need to find the minimum amount of time in which you are guaranteed to cross the road.You can assume that you cross the road immediately. For example, with $$$s=$$$\"rggry\" and the current color r there are two options: either the green color will be on after $$$1$$$ second, or after $$$3$$$. That way, the answer is equal to $$$3$$$ — that is the number of seconds that we are guaranteed to cross the road, if the current color is r.", "input_spec": "The first line contains a single integer $$$t$$$ $$$(1 \\leq t \\leq 10^4$$$) — the number of test cases. Then the description of the test cases follows. The first line of each test case contains an integer $$$n$$$ and a symbol $$$c$$$ ($$$1 \\leq n \\leq 2 \\cdot 10^5$$$, $$$c$$$ is one of allowed traffic light colors r, y or g)— the length of the string $$$s$$$ and the current color of the traffic light. The second line of each test case contains a string $$$s$$$ of the length $$$n$$$, consisting of the letters r, y and g. It is guaranteed that the symbol g is in the string $$$s$$$ and the symbol $$$c$$$ is in the string $$$s$$$. It is guaranteed, that the sum of $$$n$$$ over all test cases does not exceed $$$2\\cdot10^5$$$.", "output_spec": "For each test case output the minimal number of second in which you are guaranteed to cross the road.", "sample_inputs": ["6\n\n5 r\n\nrggry\n\n1 g\n\ng\n\n3 r\n\nrrg\n\n5 y\n\nyrrgy\n\n7 r\n\nrgrgyrg\n\n9 y\n\nrrrgyyygy"], "sample_outputs": ["3\n0\n2\n4\n1\n4"], "notes": "NoteThe first test case is explained in the statement.In the second test case the green color is on so you can cross the road immediately. In the third test case, if the red color was on at the second second, then we would wait for the green color for one second, and if the red light was on at the first second, then we would wait for the green light for two seconds.In the fourth test case the longest we would wait for the green color is if we wait for it starting from the fifth second."}, "positive_code": [{"source_code": "# wrap around the lights after the last green light\n# Eg: rrrgyyygy becomes: yrrrgyyyg\ndef wrap_lights(light_string)\n array = light_string.chars\n last_index = array.rindex('g') + 1\n array.rotate!(last_index)\n array.join\nend\n\n# create sequences of lights other than green Eg: yrrrgyyyg becomes: [yrrr, yyy]\ndef light_sequences(light_string)\n light_string.split('g')\n .reject(&:empty?)\nend\n\n# find best time for current light in a sequence\n# Eg: for light yellow in sequence yrrr it returns 4\ndef best_time(sequence, current_light)\n return nil if sequence.index(current_light).nil?\n\n sequence.length - sequence.index(current_light)\nend\n\n# finds the best time in an array of sequences\n# Eg the sequences [yrrr, yyy] returns 4\n# since yrrr has time 4 and yyy has time 3\ndef best_in_sequences(sequence_arr, current_light)\n times = sequence_arr.map { |sequence| best_time(sequence, current_light) }\n\n times.compact.max\nend\n\n# combines the above four methods\ndef best_from_string(light_string, current_light)\n return 0 if current_light == 'g'\n\n wrapped = wrap_lights(light_string)\n sequences = light_sequences(wrapped)\n best_in_sequences(sequences, current_light)\nend\n\n##### code for tests:\n\nnumber_of_cases = gets.chomp.to_i\n\nnumber_of_cases.times do\n current_light = gets.chomp.split[-1]\n light_string = gets.chomp\n puts best_from_string(light_string, current_light)\nend\n"}, {"source_code": "ts = gets.to_i\r\nts.times do\r\n c = gets.split[1]\r\n s = gets.chomp\r\n ans = (s + s).split(\"g\").map{ |t| t.size - (t.index(c) || 10**9) }.max\r\n puts c == \"g\" ? 0 : ans\r\nend"}], "negative_code": [], "src_uid": "9d3ee1b292a2402bb2204ab85dcab587"} {"nl": {"description": "This problem is interactive.We decided to play a game with you and guess the number $$$x$$$ ($$$1 \\le x < n$$$), where you know the number $$$n$$$.You can make queries like this: + c: this command assigns $$$x = x + c$$$ ($$$1 \\le c < n$$$) and then returns you the value $$$\\lfloor\\frac{x}{n}\\rfloor$$$ ($$$x$$$ divide by $$$n$$$ and round down).You win if you guess the current number with no more than $$$10$$$ queries.", "input_spec": null, "output_spec": null, "sample_inputs": ["3\n\n1", "5\n\n0\n\n0\n\n1", "10\n\n0\n\n0\n\n1\n\n2"], "sample_outputs": ["+ 1\n\n! 3", "+ 1\n\n+ 1\n\n+ 1\n\n! 5", "+ 2\n\n+ 2\n\n+ 3\n\n+ 8\n\n! 20"], "notes": "NoteIn the first sample initially $$$x = 2$$$. After the first query $$$x = 3$$$, $$$\\lfloor\\frac{x}{n}\\rfloor = 1$$$.In the second sample also initially $$$x = 2$$$. After the first query $$$x = 3$$$, $$$\\lfloor\\frac{x}{n}\\rfloor = 0$$$. After the second query $$$x = 4$$$, $$$\\lfloor\\frac{x}{n}\\rfloor = 0$$$. After the third query $$$x=5$$$, $$$\\lfloor\\frac{x}{n}\\rfloor = 1$$$."}, "positive_code": [{"source_code": "n = gets.to_i\nl = 1; r = n\ncur_div = 0\n\nwhile l + 1 < r\n m = (l + r) / 2\n\n puts \"+ #{n - m}\"\n $stdout.flush\n\n new_div = gets.to_i\n if new_div > cur_div\n l = m\n else\n r = m\n end\n\n cur_div = new_div\n l = (l + n - m) % n\n r = (r + n - m) % n\n r = n if r == 0\nend\n\nputs \"! #{cur_div * n + l}\"\n"}], "negative_code": [], "src_uid": "ad36ef40ef4888b69ee9635e924c65ab"} {"nl": {"description": "There are $$$n$$$ models in the shop numbered from $$$1$$$ to $$$n$$$, with sizes $$$s_1, s_2, \\ldots, s_n$$$.Orac will buy some of the models and will arrange them in the order of increasing numbers (i.e. indices, but not sizes).Orac thinks that the obtained arrangement is beatiful, if for any two adjacent models with indices $$$i_j$$$ and $$$i_{j+1}$$$ (note that $$$i_j < i_{j+1}$$$, because Orac arranged them properly), $$$i_{j+1}$$$ is divisible by $$$i_j$$$ and $$$s_{i_j} < s_{i_{j+1}}$$$.For example, for $$$6$$$ models with sizes $$$\\{3, 6, 7, 7, 7, 7\\}$$$, he can buy models with indices $$$1$$$, $$$2$$$, and $$$6$$$, and the obtained arrangement will be beautiful. Also, note that the arrangement with exactly one model is also considered beautiful.Orac wants to know the maximum number of models that he can buy, and he may ask you these queries many times.", "input_spec": "The first line contains one integer $$$t\\ (1 \\le t\\le 100)$$$: the number of queries. Each query contains two lines. The first line contains one integer $$$n\\ (1\\le n\\le 100\\,000)$$$: the number of models in the shop, and the second line contains $$$n$$$ integers $$$s_1,\\dots,s_n\\ (1\\le s_i\\le 10^9)$$$: the sizes of models. It is guaranteed that the total sum of $$$n$$$ is at most $$$100\\,000$$$.", "output_spec": "Print $$$t$$$ lines, the $$$i$$$-th of them should contain the maximum number of models that Orac can buy for the $$$i$$$-th query.", "sample_inputs": ["4\n4\n5 3 4 6\n7\n1 4 2 3 6 4 9\n5\n5 4 3 2 1\n1\n9"], "sample_outputs": ["2\n3\n1\n1"], "notes": "NoteIn the first query, for example, Orac can buy models with indices $$$2$$$ and $$$4$$$, the arrangement will be beautiful because $$$4$$$ is divisible by $$$2$$$ and $$$6$$$ is more than $$$3$$$. By enumerating, we can easily find that there are no beautiful arrangements with more than two models. In the second query, Orac can buy models with indices $$$1$$$, $$$3$$$, and $$$6$$$. By enumerating, we can easily find that there are no beautiful arrangements with more than three models. In the third query, there are no beautiful arrangements with more than one model."}, "positive_code": [{"source_code": "t = gets.chomp.to_i\n\nt.times do\n t2 = gets.chomp.to_i\n arr = gets.chomp.split(' ').collect(&:to_i)\n dp = Array.new(t2, 1)\n ans = 0\n for i in 1..t2\n j = i + i\n while true\n break if j > t2\n dp[j-1] = [dp[j-1], dp[i-1] + 1].max if arr[j-1] > arr[i-1]\n j += i\n end\n end\n puts dp.max\nend\n"}, {"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\nrequire 'prime'\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\ndef big(a,b) return (a>b)? a:b end\n\ndef small(a,b) return (a 0\n n = gets.chomp.to_i\n arr = [1].concat(gets.chomp.split(' ').map { |x| x.to_i })\n\n result = Array.new(n+1, 1)\n i = 1\n while i <= n\n j = 2\n while i*j <= n\n result[i*j] = result[i] + 1 if arr[i*j] > arr[i] && result[i*j] <= result[i]\n j += 1\n end\n i += 1\n end\n\n puts result.max\n t -= 1\nend\n"}], "negative_code": [{"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\nrequire 'prime'\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\ndef big(a,b) return (a>b)? a:b end\n\ndef small(a,b) return (a 1\n\t\tcur = arr.shift\n\t\tif arr[0] >= k\n\t\t\tfloor = arr[0] - k\n\t\telse\n\t\t\tfloor = 0\n\t\tend\n\t\tm += cur - floor\n\t\tif m < 0\n\t\t\tsucc = false\n\t\t\tbreak\n\t\tend\t\n\tend\n\tif succ\n\t\tputs \"YES\"\n\telse\n\t\tputs \"NO\"\n\tend\nend\n"}, {"source_code": "#!/usr/bin/env ruby\n\ntest_no = Integer(gets)\n\ntests = Array.new (test_no)\ncolumns = Array.new (test_no) \n\ntest_no.times.each do |num|\n\ttests[num] = gets.chomp.split(\" \").map(&:to_i)\n\tcolumns[num] = gets.chomp.split(\" \").map(&:to_i)\nend\n\ntests.map!.with_index do |test, index|\n\t\n\tcolumns[index].map!.with_index do |col, colindex|\n\t\t\t\n \t\tif (test[0] == colindex + 1)\n\t\t\tputs \"YES\"\n\t\t\tbreak\n\t\tend\n\t\t\t\n\t\tif col >= 1 && ((col - 1 - columns[index][colindex+1]).abs <= test[2] || (col > columns[index][colindex+1]))\t\n\t\t\tblocks_to_bag = [col, (col - columns[index][colindex+1] + test[2]).abs].min\n\t\t\ttest[1] = test[1] + blocks_to_bag\n\t\t\tcol = col - blocks_to_bag\n\t\telsif test[1] >= 1 && col < columns[index][colindex+1] && (col - columns[index][colindex+1]).abs > test[2]\n\t\t\tblocks_from_bag = [columns[index][colindex+1] - col - test[2], test[1]].min\n\t\t\tcolumns[index][colindex] = col = col + blocks_from_bag\n\n\t\t\ttests[index][1] = test[1] = test[1] - blocks_from_bag\n\t\tend\n\n\t\tif (col - columns[index][colindex+1]).abs <= test[2]\n\t\t\tnext\n\t\telse\n\t\t\tputs \"NO\"\n\t\t\tbreak\n\t\tend\n\n\tend\n\t\t\nend\n\n\n"}], "negative_code": [{"source_code": "\nT = gets.to_i\nT.times do\n\tn, m, k = gets.split.map(&:to_i)\n\tarr = gets.split.map(&:to_i)\n\tif n == 1\n\t\tputs \"YES\"\n\t\tnext\n\tend\n\tsucc = true\n\twhile arr.size > 1\n\t\tcur = arr.shift\n\t\tif arr[0] - k > cur + m\n\t\t\tsucc = false\n\t\tend\n\t\tfloor = arr[0] - k\n\t\tif cur > floor\n\t\t\tm += cur - floor\n\t\telsif cur < floor\n\t\t\tm -= floor - cur\n\t\tend\n\t\tif m < 0\n\t\t\tsucc = false\n\t\tend\t\n\tend\n\tif succ\n\t\tputs \"YES\"\n\telse\n\t\tputs \"NO\"\n\tend\nend\n"}, {"source_code": "#!/usr/bin/env ruby\n\ntest_no = Integer(gets)\n\ntests = Array.new (test_no)\ncolumns = Array.new (test_no) \n\ntest_no.times.each do |num|\n\ttests[num] = gets.chomp.split(\" \").map(&:to_i)\n\tcolumns[num] = gets.chomp.split(\" \").map(&:to_i)\nend\n\ntests.map!.with_index do |test, index|\n\t\n\tcolumns[index].map!.with_index do |col, colindex|\n\t\t\t\n \t\tif (test[0] == colindex + 1)\n\t\t\tputs \"YES\"\n\t\t\tbreak\n\t\tend\n\t\t\t\n\t\tif col >= 1 && ((col - 1 - columns[index][colindex+1]).abs <= test[2] || (col > columns[index][colindex+1]))\t\n\t\t\tblocks_to_bag = col - (test[2] - columns[index][colindex+1]).abs\n\t\t\ttest[1] = test[1] + blocks_to_bag\n\t\t\tcol = col - blocks_to_bag\n\t\telsif test[1] >= 1 && col < columns[index][colindex+1] && (col - columns[index][colindex+1]).abs > test[2]\n\t\t\tblocks_from_bag = [columns[index][colindex+1] - col - test[2], test[1]].min\n\t\t\tcolumns[index][colindex] = col = col + blocks_from_bag\n\n\t\t\ttests[index][1] = test[1] = test[1] - blocks_from_bag\n\t\tend\n\n\t\tif (col - columns[index][colindex+1]).abs <= test[2]\n\t\t\tnext\n\t\telse\n\t\t\tputs \"NO\"\n\t\t\tbreak\n\t\tend\n\n\tend\n\t\t\nend\n\n\n"}], "src_uid": "3f60e740d9a3ec14223b2b1f62c52f61"} {"nl": {"description": "After the mysterious disappearance of Ashish, his two favourite disciples Ishika and Hriday, were each left with one half of a secret message. These messages can each be represented by a permutation of size $$$n$$$. Let's call them $$$a$$$ and $$$b$$$.Note that a permutation of $$$n$$$ elements is a sequence of numbers $$$a_1, a_2, \\ldots, a_n$$$, in which every number from $$$1$$$ to $$$n$$$ appears exactly once. The message can be decoded by an arrangement of sequence $$$a$$$ and $$$b$$$, such that the number of matching pairs of elements between them is maximum. A pair of elements $$$a_i$$$ and $$$b_j$$$ is said to match if: $$$i = j$$$, that is, they are at the same index. $$$a_i = b_j$$$ His two disciples are allowed to perform the following operation any number of times: choose a number $$$k$$$ and cyclically shift one of the permutations to the left or right $$$k$$$ times. A single cyclic shift to the left on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_2, c_2:=c_3, \\ldots, c_n:=c_1$$$ simultaneously. Likewise, a single cyclic shift to the right on any permutation $$$c$$$ is an operation that sets $$$c_1:=c_n, c_2:=c_1, \\ldots, c_n:=c_{n-1}$$$ simultaneously.Help Ishika and Hriday find the maximum number of pairs of elements that match after performing the operation any (possibly zero) number of times.", "input_spec": "The first line of the input contains a single integer $$$n$$$ $$$(1 \\le n \\le 2 \\cdot 10^5)$$$ — the size of the arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \\le a_i \\le n)$$$ — the elements of the first permutation. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ $$$(1 \\le b_i \\le n)$$$ — the elements of the second permutation.", "output_spec": "Print the maximum number of matching pairs of elements after performing the above operations some (possibly zero) times.", "sample_inputs": ["5\n1 2 3 4 5\n2 3 4 5 1", "5\n5 4 3 2 1\n1 2 3 4 5", "4\n1 3 2 4\n4 2 3 1"], "sample_outputs": ["5", "1", "2"], "notes": "NoteFor the first case: $$$b$$$ can be shifted to the right by $$$k = 1$$$. The resulting permutations will be $$$\\{1, 2, 3, 4, 5\\}$$$ and $$$\\{1, 2, 3, 4, 5\\}$$$.For the second case: The operation is not required. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$1$$$.For the third case: $$$b$$$ can be shifted to the left by $$$k = 1$$$. The resulting permutations will be $$$\\{1, 3, 2, 4\\}$$$ and $$$\\{2, 3, 1, 4\\}$$$. Positions $$$2$$$ and $$$4$$$ have matching pairs of elements. For all possible rotations of $$$a$$$ and $$$b$$$, the number of matching pairs won't exceed $$$2$$$."}, "positive_code": [{"source_code": "def f(l1, l2, n)\n\tif l2 <= l1\n\t\treturn l1 - l2\n\tend\n\treturn n - l2 + l1\nend\n\nn = gets.to_i\nline1 = gets.split(' ').collect { |elem| elem.to_i}\nline2 = gets.split(' ').collect { |elem| elem.to_i}\n#[\"\", \"22\", \"14\", \"18\"].map(&:to_i)\nidx_line1 = Array.new(n)\nn.times do\n\t|idx|\n\tidx_line1[line1[idx]] = idx\nend\nm = {}\nmaxi = 0;\nn.times do\n\t|idx|\n\tval = f(idx_line1[line2[idx]], idx, n)\n\tif m[val] == nil\n\t\tm[val] = 1\n\telse\n\t\tm[val] += 1\n\tend\n\tmaxi = [maxi, m[val]].max\nend\n\nputs maxi\n"}, {"source_code": "_ = gets\na = gets.strip.split(' ').map(&:to_i)\nb = gets.strip.split(' ').map(&:to_i)\n\nh = Hash.new(0)\nb.each_with_index { |e, i| h[e] = i }\n\nhash_shift = Hash.new(0)\na.each_with_index { |e, i|\n if i < h[e]\n hash_shift[e] = (a.size - h[e]) + i\n else\n hash_shift[e] = i - h[e]\n end\n}\n\nmx = -1\nan = Hash.new(0)\nhash_shift.each_value { |v| an[v] += 1; mx = [mx, an[v]].max }\nputs mx\n"}, {"source_code": "_ = gets\na = gets.strip.split(' ').map(&:to_i)\nb = gets.strip.split(' ').map(&:to_i)\n\nh = Hash.new(0)\nb.each_with_index { |e, i| h[e] = i }\n\nhash_right = Hash.new(0)\nhash_left = Hash.new(0)\na.each_with_index { |e, i|\n if i < h[e]\n hash_right[e] = (a.size - h[e]) + i\n hash_left[e] = h[e] - i\n else\n hash_right[e] = i - h[e]\n hash_left[e] = (a.size - i) + h[e]\n end\n}\n\nhash_right_count = Hash.new(0)\nhash_left_count = Hash.new(0)\nmx = -1\nhash_right.each { |k, v|\n m1 = hash_right_count[v] += 1\n m2 = hash_left_count[hash_left[k]] += 1\n mx = [mx, m1, m2].max\n}\nputs mx\n\n"}], "negative_code": [], "src_uid": "eb1bb862dc2b0094383192f6998891c5"} {"nl": {"description": "On vacations n pupils decided to go on excursion and gather all together. They need to overcome the path with the length l meters. Each of the pupils will go with the speed equal to v1. To get to the excursion quickly, it was decided to rent a bus, which has seats for k people (it means that it can't fit more than k people at the same time) and the speed equal to v2. In order to avoid seasick, each of the pupils want to get into the bus no more than once.Determine the minimum time required for all n pupils to reach the place of excursion. Consider that the embarkation and disembarkation of passengers, as well as the reversal of the bus, take place immediately and this time can be neglected. ", "input_spec": "The first line of the input contains five positive integers n, l, v1, v2 and k (1 ≤ n ≤ 10 000, 1 ≤ l ≤ 109, 1 ≤ v1 < v2 ≤ 109, 1 ≤ k ≤ n) — the number of pupils, the distance from meeting to the place of excursion, the speed of each pupil, the speed of bus and the number of seats in the bus. ", "output_spec": "Print the real number — the minimum time in which all pupils can reach the place of excursion. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.", "sample_inputs": ["5 10 1 2 5", "3 6 1 2 1"], "sample_outputs": ["5.0000000000", "4.7142857143"], "notes": "NoteIn the first sample we should immediately put all five pupils to the bus. The speed of the bus equals 2 and the distance is equal to 10, so the pupils will reach the place of excursion in time 10 / 2 = 5."}, "positive_code": [{"source_code": "n, l, v1, v2, k = readline.split.map(&:to_i)\n\nN = (n+k-1)/k\nt2 = l.to_f / (2*v1*v2*(N-1).to_f / (v1+v2) + v2)\nt1 = (v2-v1).to_f/(v2+v1) * t2\n\nputs (N-1)*t1 + N*t2\n"}], "negative_code": [], "src_uid": "620a9baa531f0c614cc103e70cfca6fd"} {"nl": {"description": "Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r.", "input_spec": "The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query.", "output_spec": "For each query, print its answer in a single line.", "sample_inputs": ["2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8", "1 5 2\n1 5 10\n2 7 4"], "sample_outputs": ["4\n-1\n8\n-1", "1\n2"], "notes": null}, "positive_code": [{"source_code": "a, b, n = gets.split.map(&:to_i)\n\ndef find_r (a, b, l, t, m, s, e)\n return e if s == e || t * m >= (e - l + 1) * (a + b * (e + l - 2) / 2.0)\n mid = (e + s + 1) / 2\n if t * m >= (mid - l + 1) * (a + b * (mid + l - 2) / 2.0)\n find_r(a, b, l, t, m, mid, e)\n else\n find_r(a, b, l, t, m, s, mid - 1)\n end\nend\n\nn.times do\n l, t, m = gets.split.map(&:to_i)\n if t < a + b * (l - 1)\n puts -1\n next\n end\n puts find_r(a, b, l, t, m, l, (t + b - a) / b)\nend\n"}, {"source_code": "a, b, n = gets.split.map(&:to_i)\n\ndef find_r (a, b, l, t, m, s, e)\n return e if t * m >= (e - l + 1) * (a + b * (e + l - 2) / 2.0)\n return s - 1 if s == e;\n mid = (e + s) / 2;\n if t * m >= (mid - l + 1) * (a + b * (mid + l - 2) / 2.0)\n find_r(a, b, l, t, m, mid + 1, e)\n else\n find_r(a, b, l, t, m, s, mid)\n end\nend\n\nn.times do\n l, t, m = gets.split.map(&:to_i)\n if t < a + b * (l - 1)\n puts -1\n next\n end\n puts find_r(a, b, l, t, m, l, (t + b - a) / b)\nend\n"}, {"source_code": "a, b, n = gets.split.map(&:to_i)\n\ndef si(a, b, i)\n a + b * (i - 1)\nend\n\nn.times do\n l, t, m = gets.split.map(&:to_i)\n\n if t < si(a, b, l)\n puts -1\n next\n end\n\n rl = l\n rr = (t + b - a) / b\n\n while rl != rr\n mid = (rl + rr + 1) / 2\n if t * m >= (si(a, b, l) + si(a, b, mid)) * (mid - l + 1) / 2\n rl = mid\n else\n rr = mid - 1\n end\n end\n puts rr\nend\n"}, {"source_code": "a, b, n = gets.split.map &:to_i\nn.times do\n\tl, t, m = gets.split.map &:to_i\n\td = t*m; f = a+(l-1)*b; k = l; x = 1000001\n\twhile(k < x) do\n mid = k+(x-k+1)/2; q = a+(mid-1)*b; sum = (f+q)*(mid-l+1)/2\n\t (t >= q && d >= sum)? (k = mid) : (x = mid-1)\n\tend\n\tputs (k == l and t < f)? (-1) : (k)\nend"}], "negative_code": [{"source_code": "a, b, n = gets.split.map(&:to_i)\n\ndef si(a, b, i)\n a + b * (i - 1)\nend\n\nn.times do\n l, t, m = gets.split.map(&:to_i)\n\n rl = l\n rr = (t + b - a) / b\n\n if t < si(a, b, l)\n puts -1\n next\n end\n\n while rl != rr\n if t * m >= (si(a, b, l) + si(a, b, rr)) * (rr - l + 1) / 2\n rl = (rl + rr + 1) / 2\n else\n rr = (rl + rr + 1) / 2\n end\n end\n puts rr\nend\n"}, {"source_code": "a, b, n = gets.split.map(&:to_i)\n\ndef find_r (a, b, l, t, m, s, e)\n return s if s == e\n if t * m >= (e - l + 1) * (a + b * (e + l - 2) / 2.0)\n find_r(a, b, l, t, m, (e + s) / 2 + 1, e)\n else\n find_r(a, b, l, t, m, s, (e + s) / 2)\n end\nend\n\nn.times do\n l, t, m = gets.split.map(&:to_i)\n if t < a + b * (l - 1)\n puts -1\n next\n end\n puts find_r(a, b, l, t, m, l, ((t + b - a) / b))\nend\n"}], "src_uid": "89c97b6c302bbb51e9d5328c680a7ea7"} {"nl": {"description": "During a recent research Berland scientists found out that there were n cities in Ancient Berland, joined by two-way paths. Any two cities are joined by no more than one path. No path joins a city with itself. According to a well-known tradition, the road network was built so that it would be impossible to choose three cities from each of which one can get to any other one directly. That is, there was no cycle exactly as long as 3. Unfortunately, the road map has not been preserved till nowadays. Now the scientists are interested how much developed a country Ancient Berland was. Help them - find, what maximal number of roads could be in the country. You also have to restore any of the possible road maps.", "input_spec": "The first line contains an integer n (1 ≤ n ≤ 100) — the number of cities in Berland.", "output_spec": "On the first line must be printed number m — the maximal number of roads in Berland. Then print m lines containing two numbers each — the numbers of cities that the given road joins. The cities are numbered with integers from 1 to n. If there are several variants of solving the problem, print any of them.", "sample_inputs": ["3", "4"], "sample_outputs": ["2\n1 2\n2 3", "4\n1 2\n2 3\n3 4\n4 1"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\np = n/2\nputs p*(n-p)\nfor i in 1..p do\n for j in p+1..n do\n printf(\"%d %d\\n\", i, j)\n end\nend\n"}, {"source_code": "n = gets.chomp.to_i\nar = Array.new\nfor i in 1..n-1\n\tfor j in i+1..n\n\t\tnext if i + 2 == j\n\t\tnext if (i % 2 == j % 2)\n\t\tar.push([i,j])\n\tend\nend\nputs ar.length\nar.each{|x| puts x[0].to_s + \" \" + x[1].to_s }\n"}, {"source_code": "a=gets.to_i\nc=a/2\nd=(a+1)/2\np c*d\n(1..c).each{|i|\n(c+1..a).each{|j|\nputs \"#{i} #{j}\"\n}\n}\n"}, {"source_code": "a=gets.to_i\nc=a/2\nd=(a+1)/2\np c*d\n(1..c).each{|i|\n(c+1..a).each{|j|\nputs \"#{i} #{j}\"\n}\n}\n"}, {"source_code": "a=gets.to_i\nc=a/2\nd=(a+1)/2\np c*d\n(1..c).each{|i|\n(c+1..a).each{|j|\nputs \"#{i} #{j}\"\n}\n}\n"}, {"source_code": "a=gets.to_i\nc=a/2\nd=(a+1)/2\np c*d\n(1..c).each{|i|\n(c+1..a).each{|j|\nputs \"#{i} #{j}\"\n}\n}\n"}, {"source_code": "a=gets.to_i\nc=a/2\nd=(a+1)/2\np c*d\n(1..c).each{|i|\n(c+1..a).each{|j|\nputs \"#{i} #{j}\"\n}\n}"}, {"source_code": "a=gets.to_i\nc=a/2\nd=(a+1)/2\np c*d\n(1..c).each{|i|\n(c+1..a).each{|j|\nputs \"#{i} #{j}\"\n}\n}"}, {"source_code": "a=gets.to_i\nc=a/2\nd=(a+1)/2\np c*d\n(1..c).each{|i|\n(c+1..a).each{|j|\nputs \"#{i} #{j}\"\n}\n}\n"}, {"source_code": "a=gets.to_i\nc=a/2\nd=(a+1)/2\np c*d\n(1..c).each{|i|\n(c+1..a).each{|j|\nputs \"#{i} #{j}\"\n}\n}\n"}, {"source_code": "a=gets.to_i\nc=a/2\nd=(a+1)/2\np c*d\n(1..c).each{|i|\n(c+1..a).each{|j|\nputs \"#{i} #{j}\"\n}\n}\n"}, {"source_code": "a=gets.to_i\nc=a/2\nd=(a+1)/2\np c*d\n(1..c).each{|i|\n(c+1..a).each{|j|\nputs \"#{i} #{j}\"\n}\n}\n"}], "negative_code": [{"source_code": "n = gets.chomp.to_i\nar = Array.new\nfor i in 1..n-1\n\tfor j in i+1..n\n\t\tnext if i + 2 == j\n\t\tif n > 3\n\t\t\tnext if (i == 2 && j == n) || (i == 1 && j == n-1) \n\t\tend\n\t\tnext if i > 1 && (i % 2 == j % 2)\n\t\tar.push([i,j])\n\tend\nend\nputs ar.length\nar.each{|x| puts x[0].to_s + \" \" + x[1].to_s }\n"}, {"source_code": "n = gets.chomp.to_i\nar = Array.new\nfor i in 1..n-1\n\tfor j in i+1..n\n\t\tnext if i + 2 == j\n\t\tif n > 3\n\t\t\tnext if (i == 2 && j == n) || (i == 1 && j == n-1) \n\t\tend\n\t\tar.push([i,j])\n\tend\nend\nputs ar.length\nar.each{|x| puts x[0].to_s + \" \" + x[1].to_s }\n"}], "src_uid": "3f86b3cec02aafb24c5aeb2648cc3df9"} {"nl": {"description": "Polycarp analyzes the prices of the new berPhone. At his disposal are the prices for $$$n$$$ last days: $$$a_1, a_2, \\dots, a_n$$$, where $$$a_i$$$ is the price of berPhone on the day $$$i$$$.Polycarp considers the price on the day $$$i$$$ to be bad if later (that is, a day with a greater number) berPhone was sold at a lower price. For example, if $$$n=6$$$ and $$$a=[3, 9, 4, 6, 7, 5]$$$, then the number of days with a bad price is $$$3$$$ — these are days $$$2$$$ ($$$a_2=9$$$), $$$4$$$ ($$$a_4=6$$$) and $$$5$$$ ($$$a_5=7$$$).Print the number of days with a bad price.You have to answer $$$t$$$ independent data sets.", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 10000$$$) — the number of sets of input data in the test. Input data sets must be processed independently, one after another. Each input data set consists of two lines. The first line contains an integer $$$n$$$ ($$$1 \\le n \\le 150000$$$) — the number of days. The second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^6$$$), where $$$a_i$$$ is the price on the $$$i$$$-th day. It is guaranteed that the sum of $$$n$$$ over all data sets in the test does not exceed $$$150000$$$.", "output_spec": "Print $$$t$$$ integers, the $$$j$$$-th of which should be equal to the number of days with a bad price in the $$$j$$$-th input data set.", "sample_inputs": ["5\n6\n3 9 4 6 7 5\n1\n1000000\n2\n2 1\n10\n31 41 59 26 53 58 97 93 23 84\n7\n3 2 1 2 3 4 5"], "sample_outputs": ["3\n0\n1\n8\n2"], "notes": null}, "positive_code": [{"source_code": "gets.to_i.times do\n\tn = gets.to_i\n\ta = gets.split.map(&:to_i)\n\tmin = 10**9\n\tputs a.reverse.count { |i|\n\t\t\tres = min < i\n\t\t\tmin = [min, i].min\n\t\t\tres\n\t }\nend\n"}, {"source_code": "t = gets.to_i\nt.times do \n n = gets.to_i\n arr = gets.split.map(&:to_i)\n min = nil\n count = 0\n arr.reverse_each do |x|\n min = x if min.nil? || x < min\n count += 1 if x > min\n end\n puts count\nend\n"}, {"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\nPrice = Struct.new(:price, :day)\nt = gets.to_i\nt.times {\n n = gets.to_i\n a = []\n gets.split.each_with_index { |price, day_zero|\n a << Price.new(price.to_i, day_zero+1)\n }\n # non-descending order\n a.sort! { |e1, e2|\n if e1.price != e2.price\n e1.price - e2.price\n else\n e1.day - e2.day\n end\n }\n # 사용 가능한 factor: price-day (value-index)\n bads = 0\n pday = 0\n for i in a\n if pday <= i.day\n pday = i.day\n else\n bads += 1\n end\n end\n\n puts bads\n}"}, {"source_code": "t=gets.to_i\nfor j in 0...t do\n n=gets.to_i\n a=Array.new()\n for i in 0...n-1 do\n a.unshift(gets(sep=' ').to_i)\n end\n d=0\n a.unshift(gets.to_i)\n mn=a[0]\n for i in 1...n do\n if a[i]>mn then\n d+=1\n else\n mn=a[i]\n end\n end\n puts d\nend"}, {"source_code": "t=gets.to_i\nfor j in 0...t do\n n=gets.to_i\n a=Array.new(n-1)\n for i in 0...n-1 do\n a[i]=gets(sep=' ').to_i\n end\n d=0\n a.push(gets.to_i)\n mn=a.last\n for i in (0...n).reverse_each do\n if a[i]>mn then\n d+=1\n else\n mn=a[i]\n end\n end\n puts d\nend"}, {"source_code": "def solve\nend\n\ngets.strip.to_i.times do\n n = gets.strip.to_i\n days = gets.strip.split(' ').map(&:to_i)\n days.reverse!\n last = days[0]\n res = 0\n days.each do |d|\n if d > last\n res += 1\n end\n last = [last, d].min\n end\n puts res\nend"}, {"source_code": "gets.to_i.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n suf_min = 1E100.to_i\n res = 0\n (0...n).reverse_each do |i|\n res += 1 if a[i] > suf_min\n suf_min = [suf_min, a[i]].min\n end\n puts res\nend"}, {"source_code": "# require 'pry'\n\nt = gets.to_i\nt.times do\n n = gets.to_i\n min = nil\n res = 0\n gets.split.map(&:to_i).reverse_each do |num|\n res += 1 if min && min < num\n min = num if min.nil? || num < min\n end\n puts res\nend\n"}, {"source_code": "tt = gets.to_i\ntt.times do |t|\n nn = gets.to_i\n aa = gets.split.map(&:to_i)\n\n cnt = 0\n min = aa[-1]\n (nn-2).downto(0) do |n|\n if aa[n] > min\n cnt+=1\n else\n min = aa[n]\n end\n end\n puts cnt\nend"}], "negative_code": [{"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\nPrice = Struct.new(:price, :day)\nt = gets.to_i\nt.times {\n n = gets.to_i\n a = []\n gets.split.each_with_index { |price, day_zero|\n a << Price.new(price.to_i, day_zero)\n }\n # non-descending order\n a.sort! { |e1, e2|\n e1.price - e2.price\n }\n # 사용 가능한 factor: price-day (value-index)\n bads = 0\n pday = 0\n for i in a\n if pday <= i.day\n pday = i.day\n else\n bads += 1\n end\n end\n\n puts bads\n}"}, {"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\nPrice = Struct.new(:price, :day)\nt = gets.to_i\nt.times {\n n = gets.to_i\n a = []\n gets.split.each_with_index { |price, day_zero|\n a << Price.new(price.to_i, day_zero+1)\n }\n # non-descending order\n a.sort! { |e1, e2|\n e1.price - e2.price\n }\n # 사용 가능한 factor: price-day (value-index)\n bads = 0\n pday = 0\n for i in a\n if pday < i.day\n pday = i.day\n else\n bads += 1\n end\n end\n\n puts bads\n}"}], "src_uid": "09faf19627d2ff00c3821d4bc2644b63"} {"nl": {"description": "The map of Berland is a rectangle of the size n × m, which consists of cells of size 1 × 1. Each cell is either land or water. The map is surrounded by the ocean. Lakes are the maximal regions of water cells, connected by sides, which are not connected with the ocean. Formally, lake is a set of water cells, such that it's possible to get from any cell of the set to any other without leaving the set and moving only to cells adjacent by the side, none of them is located on the border of the rectangle, and it's impossible to add one more water cell to the set such that it will be connected with any other cell.You task is to fill up with the earth the minimum number of water cells so that there will be exactly k lakes in Berland. Note that the initial number of lakes on the map is not less than k. ", "input_spec": "The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 0 ≤ k ≤ 50) — the sizes of the map and the number of lakes which should be left on the map. The next n lines contain m characters each — the description of the map. Each of the characters is either '.' (it means that the corresponding cell is water) or '*' (it means that the corresponding cell is land). It is guaranteed that the map contain at least k lakes.", "output_spec": "In the first line print the minimum number of cells which should be transformed from water to land. In the next n lines print m symbols — the map after the changes. The format must strictly follow the format of the map in the input data (there is no need to print the size of the map). If there are several answers, print any of them. It is guaranteed that the answer exists on the given data.", "sample_inputs": ["5 4 1\n****\n*..*\n****\n**.*\n..**", "3 3 0\n***\n*.*\n***"], "sample_outputs": ["1\n****\n*..*\n****\n****\n..**", "1\n***\n***\n***"], "notes": "NoteIn the first example there are only two lakes — the first consists of the cells (2, 2) and (2, 3), the second consists of the cell (4, 3). It is profitable to cover the second lake because it is smaller. Pay attention that the area of water in the lower left corner is not a lake because this area share a border with the ocean. "}, "positive_code": [{"source_code": "n, m, k = gets.split.map(&:to_i)\n@land = []\nn.times do |i|\n @land[i] = gets.chomp.split('')\nend\n@n, @m = n, m\n\n# def bfs(r, c)\n# return [] unless @land[r][c] == '.'\n# arr = [[r, c]]\n# @land[r][c] = '#'\n# [[1, 0], [-1, 0], [0, 1], [0, -1]].each do |ud, lr|\n# rr, cc = r + ud, c + lr\n# next if rr < 0 || rr >= @n || cc < 0 || cc >= @m\n# arr += bfs(rr,cc)\n# end\n# arr\n# end\n\ndef bfs(r, c)\n return [] unless @land[r][c] == '.'\n arr = [[r, c]]\n @land[r][c] = '#'\n answer = []\n while !arr.empty? do\n r, c = arr.pop\n answer << [r, c]\n [[1, 0], [-1, 0], [0, 1], [0, -1]].each do |ud, lr|\n rr, cc = r + ud, c + lr\n next if rr < 0 || rr >= @n || cc < 0 || cc >= @m\n next unless @land[rr][cc] == '.'\n arr << [rr, cc]\n @land[rr][cc] = '#'\n end\n end\n answer\nend\n\nn.times { |r| [0, m - 1].each { |c| bfs(r, c) } }\nm.times { |c| [0, n - 1].each { |r| bfs(r, c) } }\n\nlakes = []\n(1...(n-1)).each do |r|\n (1...(m-1)).each do |c|\n lake = bfs(r, c)\n next if lake.empty?\n lakes << lake\n end\nend\n\nlakes.sort_by!(&:size)\ncount = 0\nlakes[0...(lakes.size - k)].each do |lake|\n lake.each { |r, c| @land[r][c] = '*'; count += 1 }\nend\n@land.each { |row| row.map! { |x| x == '#' ? '.' : x } }\n\nputs count\n@land.each do |row|\n puts row.join\nend\n"}, {"source_code": "vs = Array.new(55){Array.new(55, 0)}\nrv = Array.new(55){Array.new(55, 0)}\nl = Array.new(2502)\nlx = Array.new(2502)\nly = Array.new(2502)\ndef ff(vs, rv, x, y)\n\tdx = [1, 0, -1, 0]\n\tdy = [0, 1, 0, -1]\n\tfx = Array.new(2502)\n\tfy = Array.new(2502)\n\tr = 1\n\tt = 2\n\tcnt = 0\n\tfx[r] = x\n\tfy[r] = y\n\tvs[x][y] = '*'\n\trv[x][y] = 15\n\twhile r != t\n\t\tx = fx[r]\n\t\ty = fy[r]\n\t\tr += 1\n\t\tcnt += 1\n\t\tfor o in (0..3)\n\t\t\tnx = x + dx[o]\n\t\t\tny = y + dy[o]\n\t\t\tif vs[nx][ny] == '.'\n\t\t\t\tvs[nx][ny] = '*'\n\t\t\t\trv[nx][ny] = 15\n\t\t\t\tfx[t] = nx\n\t\t\t\tfy[t] = ny\n\t\t\t\tt += 1\n\t\t\tend\n\t\tend\n\tend\n\treturn cnt\nend\nipt = gets.split(' ')\nn = ipt[0].to_i\nm = ipt[1].to_i\nk = ipt[2].to_i\nfor i in (1..n)\n\tipt = 'Z'\n\tipt += gets\n\tvs[i] = ipt\nend\nfor i in (1..m)\n\tif vs[1][i] == '.'\n\t\tff(vs, rv, 1, i)\n\tend\n\tif vs[n][i] == '.'\n\t\tff(vs, rv, n, i)\n\tend\nend\nfor i in (1..n)\n\tif vs[i][1] == '.'\n\t\tff(vs, rv, i, 1)\n\tend\n\tif vs[i][m] == '.'\n\t\tff(vs, rv, i, m)\n\tend\nend\nlcnt = 0\nfor i in (1..n)\n\tfor j in (1..m)\n\t\tif vs[i][j] == '.'\n\t\t\tlcnt = lcnt + 1\n\t\t\tl[lcnt] = ff(vs, rv, i, j)\n\t\t\tlx[lcnt] = i\n\t\t\tly[lcnt] = j\n\t\tend\n\tend\nend\nfor i in (1..lcnt)\n\tfor j in (i+1..lcnt)\n\t\tif l[i] < l[j]\n\t\t\tt = l[i]\n\t\t\tl[i] = l[j]\n\t\t\tl[j] = t\n\t\t\tt = lx[i]\n\t\t\tlx[i] = lx[j]\n\t\t\tlx[j] = t\n\t\t\tt = ly[i]\n\t\t\tly[i] = ly[j]\n\t\t\tly[j] = t\n\t\tend\n\tend\nend\nfor i in (1..n)\n\tfor j in (1..m)\n\t\tif rv[i][j] == 15\n\t\t\tvs[i][j] = '.'\n\t\tend\n\tend\nend\nsum = 0\nfor k in (k+1..lcnt)\n\tsum += ff(vs, rv, lx[k], ly[k])\nend\nputs sum\nfor i in (1..n)\n\tfor j in (1..m)\n\t\tprint vs[i][j]\n\tend\n\tprint \"\\n\"\nend"}, {"source_code": "vs = Array.new(55){Array.new(55, 0)}\nrv = Array.new(55){Array.new(55, 0)}\nl = Array.new(2502)\nlx = Array.new(2502)\nly = Array.new(2502)\ndef ff(vs, rv, x, y)\n\tdx = [1, 0, -1, 0]\n\tdy = [0, 1, 0, -1]\n\tfx = Array.new(2502)\n\tfy = Array.new(2502)\n\tr = 1\n\tt = 2\n\tcnt = 0\n\tfx[r] = x\n\tfy[r] = y\n\tvs[x][y] = '*'\n\trv[x][y] = 15\n\twhile r != t\n\t\tx = fx[r]\n\t\ty = fy[r]\n\t\tr += 1\n\t\tcnt += 1\n\t\tfor o in (0..3)\n\t\t\tnx = x + dx[o]\n\t\t\tny = y + dy[o]\n\t\t\tif vs[nx][ny] == '.'\n\t\t\t\tvs[nx][ny] = '*'\n\t\t\t\trv[nx][ny] = 15\n\t\t\t\tfx[t] = nx\n\t\t\t\tfy[t] = ny\n\t\t\t\tt += 1\n\t\t\tend\n\t\tend\n\tend\n\treturn cnt\nend\nipt = gets.split(' ')\nn = ipt[0].to_i\nm = ipt[1].to_i\nk = ipt[2].to_i\nfor i in (1..n)\n\tipt = 'Z'\n\tipt += gets\n\tvs[i] = ipt\nend\nfor i in (1..m)\n\tif vs[1][i] == '.'\n\t\tff(vs, rv, 1, i)\n\tend\n\tif vs[n][i] == '.'\n\t\tff(vs, rv, n, i)\n\tend\nend\nfor i in (1..n)\n\tif vs[i][1] == '.'\n\t\tff(vs, rv, i, 1)\n\tend\n\tif vs[i][m] == '.'\n\t\tff(vs, rv, i, m)\n\tend\nend\nlcnt = 0\nfor i in (1..n)\n\tfor j in (1..m)\n\t\tif vs[i][j] == '.'\n\t\t\tlcnt = lcnt + 1\n\t\t\tl[lcnt] = ff(vs, rv, i, j)\n\t\t\tlx[lcnt] = i\n\t\t\tly[lcnt] = j\n\t\tend\n\tend\nend\nfor i in (1..lcnt)\n\tfor j in (i+1..lcnt)\n\t\tif l[i] < l[j]\n\t\t\tt = l[i]\n\t\t\tl[i] = l[j]\n\t\t\tl[j] = t\n\t\t\tt = lx[i]\n\t\t\tlx[i] = lx[j]\n\t\t\tlx[j] = t\n\t\t\tt = ly[i]\n\t\t\tly[i] = ly[j]\n\t\t\tly[j] = t\n\t\tend\n\tend\nend\nfor i in (1..n)\n\tfor j in (1..m)\n\t\tif rv[i][j] == 15\n\t\t\tvs[i][j] = '.'\n\t\tend\n\tend\nend\nsum = 0\nfor k in (k+1..lcnt)\n\tsum += ff(vs, rv, lx[k], ly[k])\nend\nputs sum\nfor i in (1..n)\n\tfor j in (1..m)\n\t\tprint vs[i][j]\n\tend\n\tprint \"\\n\"\nend"}, {"source_code": "$n, $m, k = gets.split.map(&:to_i)\n$map = $n.times.map{gets.chomp}\n$vis = Array.new($n){Array.new($m, false)}\n$lake = []\n\ndef dfs(x, y)\n return if x >= $n || y >= $m || x < 0 || y < 0\n return if $vis[x][y]\n return if $map[x][y] == '*'\n $lake << [x, y]\n $vis[x][y] = true\n\n dfs(x + 1, y)\n dfs(x - 1, y)\n dfs(x, y + 1)\n dfs(x, y - 1)\nend\n\ndef get_lake(x, y)\n $lake = []\n dfs(x, y)\n return $lake\nend\n\nlakes = []\n$n.times do |i|\n $m.times do |j|\n next if $map[i][j] == '*'\n lakes << get_lake(i, j)\n end\nend\n\ndef is_ocean?(lake)\n lake.any? do |x, y|\n x == $n - 1 || x == 0 || y == $m - 1 || y == 0\n end\nend\n\nlakes = lakes.reject{|x| is_ocean?(x)}.sort_by(&:size)\nans = 0\nwhile lakes.size > k\n lake = lakes.shift\n ans += lake.size\n lake.each{|x, y| $map[x][y] = '*'}\nend\n\np ans\nputs $map\n"}, {"source_code": "def dfs(s, visited, map, path, start)\n k, j = s\n visited[s] = true\n\n list = []\n list.push([k, j - 1]) if j - 1 >= 0\n list.push([k, j + 1]) if j + 1 < map[0].length\n list.push([k - 1, j]) if k - 1 >= 0\n list.push([k + 1, j]) if k + 1 < map.length\n\n for i, j in list\n if !visited[[i, j]] && map[i][j] == \".\"\n path[start] << [i, j]\n dfs([i, j], visited, map, path, start)\n end\n end\nend\n\nn, m, l = gets.chomp.split.map(&:to_i)\nmap = Hash.new { |hash, key| hash[key] = nil }\n\nn.times do |i|\n map[i] = gets.chomp.split(\"\")\nend\n\nif map.values.join == \".\" * (n * m)\n puts 0\n for i in 0..(n - 1)\n print *map[i]\n puts\n end\nelse\n visited = Hash.new { |hash, key| hash[key] = false }\n path = Hash.new { |hash, key| hash[key] = [] }\n\n for k, v in map\n for j in 0..(v.length - 1)\n if !visited[[k, j]] && v[j] == \".\"\n path[[k, j]] << [k, j]\n dfs([k, j], visited, map, path, [k, j])\n end\n end\n end\n\n result = Hash.new { |hash, key| hash[key] = [] }\n for k, v in path\n v.push(k)\n\n flag = true\n for i, j in v\n if i == 0 || i == n - 1 || j == 0 || j == m - 1\n break flag = false\n end\n end\n\n result[v] = v.length - 1 if flag\n end\n\n groups = result.keys.count\n sizes = result.sort_by { |_k, v| v }\n\n min_groups = groups - l\n min = 0\n\n index = []\n min_groups.times do |i|\n min += sizes[i][1]\n index << sizes[i][0]\n end\n\n puts min\n for v in index\n for i, j in v\n map[i][j] = \"*\"\n end\n end\n for i in 0..(n - 1)\n print *map[i]\n puts\n end\nend\n"}], "negative_code": [{"source_code": "n, m, k = gets.split.map(&:to_i)\n@land = []\nn.times do |i|\n @land[i] = gets.chomp.split('')\nend\n@n, @m = n, m\n\ndef bfs(r, c)\n return unless @land[r][c] == '.'\n (arr = []) << [r, c]\n lake = []\n while !arr.empty?\n r, c = arr.shift\n lake << [r, c]\n @land[r][c] = '#'\n [[1, 0], [-1, 0], [0, 1], [0, -1]].each do |ud, lr|\n rr, cc = r + ud, c + lr\n next if rr < 0 || rr >= @n || cc < 0 || cc >= @m\n next unless @land[rr][cc] == '.'\n arr << [rr, cc]\n end\n end\n lake\nend\n\nn.times { |r| [0, m - 1].each { |c| bfs(r, c) } }\nm.times { |c| [0, n - 1].each { |r| bfs(r, c) } }\n\nlakes = []\n(1...(n-1)).each do |r|\n (1...(m-1)).each do |c|\n lake = bfs(r, c)\n next if lake.nil? || lake.empty?\n lakes << lake\n end\nend\n\nlakes.sort! { |l| l.size }\ncount = 0\nlakes[0...(lakes.size - k)].each do |lake|\n lake.each { |r, c| @land[r][c] = '*'; count += 1 }\nend\n@land.each { |row| row.map! { |x| x == '#' ? '.' : x } }\n\nputs count\n@land.each do |row|\n puts row.join\nend\n"}, {"source_code": "n, m, k = gets.split.map(&:to_i)\n@land = []\nn.times do |i|\n @land[i] = gets.chomp.split('')\nend\n\n@n, @m = n, m\n\ndef dfs(r, c)\n return [] if @land[r][c] == '*' || @land[r][c] == '#'\n raise if (r == 0 or r == @n - 1 or c == 0 or c == @m - 1) && @land[r][c] == '.'\n @land[r][c] = '#'\n [[r, c]] + dfs(r + 1, c) + dfs(r - 1, c) + dfs(r, c - 1) + dfs(r, c + 1)\nend\n\nlakes = []\n\n(1...(n-1)).each do |r|\n (1...(m-1)).each do |c|\n begin\n lake = dfs(r, c)\n next if lake.empty?\n lakes << lake\n rescue\n next\n end\n end\nend\n\nlakes = lakes.sort { |l| -l.size }\ncount = 0\n(k...(lakes.size)).each do |i|\n lakes[i].each { |r, c| @land[r][c] = '*'; count += 1 }\nend\n@land.each { |row| row.map! { |c| c == '#' ? '.' : c } }\n\nputs count\n@land.each do |row|\n puts row.join\nend\n"}, {"source_code": "n, m, k = gets.split.map(&:to_i)\n@land = []\nn.times do |i|\n @land[i] = gets.chomp.split('')\nend\n@n, @m = n, m\n\ndef bfs(r, c)\n return [] unless @land[r][c] == '.'\n (arr = []) << [r, c]\n @land[r][c] = '#'\n [[1, 0], [-1, 0], [0, 1], [0, -1]].each do |ud, lr|\n rr, cc = r + ud, c + lr\n next if rr < 0 || rr >= @n || cc < 0 || cc >= @m\n arr += bfs(rr,cc)\n end\n arr\nend\n\nn.times { |r| [0, m - 1].each { |c| bfs(r, c) } }\nm.times { |c| [0, n - 1].each { |r| bfs(r, c) } }\n\nlakes = []\n(1...(n-1)).each do |r|\n (1...(m-1)).each do |c|\n lake = bfs(r, c)\n next if lake.empty?\n lakes << lake\n end\nend\n\nlakes.sort! { |l| l.size }\ncount = 0\nlakes[0...(lakes.size - k)].each do |lake|\n lake.each { |r, c| @land[r][c] = '*'; count += 1 }\nend\n@land.each { |row| row.map! { |x| x == '#' ? '.' : x } }\n\nputs count\n@land.each do |row|\n puts row.join\nend\n"}, {"source_code": "n, m, k = gets.split.map(&:to_i)\n@land = []\nn.times do |i|\n @land[i] = gets.chomp.split('')\nend\n\n@n, @m = n, m\n\ndef dfs(r, c)\n return [] if @land[r][c] == '*' || @land[r][c] == '#'\n return nil if (r == 0 or r == @n - 1 or c == 0 or c == @m - 1) && @land[r][c] == '.'\n @land[r][c] = '#'\n ans = [[r, c]]\n [[r + 1, c], [r - 1, c], [r, c - 1], [r, c + 1]].each do |rr, cc|\n d = dfs(rr, cc)\n if d.nil?\n ans.each { |r, c| @land[r][c] = '.' }\n return\n else\n ans += d\n end\n end\n ans\nend\n\nlakes = []\n\n(1...(n-1)).each do |r|\n (1...(m-1)).each do |c|\n begin\n lake = dfs(r, c)\n next if lake.nil? || lake.empty?\n lake.each { |r, c| }\n lakes << lake\n rescue\n next\n end\n end\nend\n\nlakes.sort! { |l| -l.size }\ncount = 0\n(k...(lakes.size)).each do |i|\n lakes[i].each { |r, c| @land[r][c] = '*'; count += 1 }\nend\n@land.each { |row| row.map! { |c| c == '#' ? '.' : c } }\n\nputs count\n@land.each do |row|\n puts row.join\nend\n"}, {"source_code": "def dfs(s, visited, map, path, start)\n k, j = s\n visited[s] = true\n\n list = []\n list.push([k, j - 1]) if j - 1 >= 0 && map[k][j - 1] == \".\"\n list.push([k, j + 1]) if j + 1 < map[0].length && map[k][j + 1] == \".\"\n list.push([k - 1, j]) if k - 1 >= 0 && map[k - 1][j] == \".\"\n list.push([k + 1, j]) if k + 1 < map.length && map[k + 1][j] == \".\"\n\n for i, j in list\n if !visited[[i, j]]\n path[start] << [i, j]\n dfs([i, j], visited, map, path, start)\n end\n end\nend\n\nn, m, l = gets.chomp.split.map(&:to_i)\nmap = Hash.new { |hash, key| hash[key] = nil }\n\nn.times do |i|\n map[i] = gets.chomp.split(\"\")\nend\n\nvisited = Hash.new { |hash, key| hash[key] = false }\npath = Hash.new { |hash, key| hash[key] = [] }\n\nfor k, v in map\n for j in 0..(v.length - 1)\n if !visited[[k, j]] && v[j] == \".\"\n path[[k, j]] << [k, j]\n dfs([k, j], visited, map, path, [k, j])\n end\n end\nend\n\nresult = Hash.new { |hash, key| hash[key] = [] }\nfor k, v in path\n v.push(k)\n\n flag = true\n for i, j in v\n if i == 0 || i == n - 1 || j == 0 || j == m - 1\n break flag = false\n end\n end\n\n result[k] = v.length - 1 if flag\nend\n\ngroups = result.keys.count\nsizes = result.sort_by { |_k, v| v }\n\nmin_groups = groups - l\nmin = 0\n\nindex = []\nmin_groups.times do |i|\n min += sizes[i][1]\n index << sizes[i][0]\nend\n\nfor i, j in index\n map[i][j] = \"*\"\nend\n\nputs min\nfor i in 0..(n - 1)\n print *map[i]\n puts\nend\n"}, {"source_code": "def dfs(s, visited, map, path, start)\n k, j = s\n visited[s] = true\n\n list = []\n list.push([k, j - 1]) if j - 1 >= 0 && map[k][j - 1] == \".\"\n list.push([k, j + 1]) if j + 1 < map[0].length && map[k][j + 1] == \".\"\n list.push([k - 1, j]) if k - 1 >= 0 && map[k - 1][j] == \".\"\n list.push([k + 1, j]) if k + 1 < map.length && map[k + 1][j] == \".\"\n\n for i, j in list\n if !visited[[i, j]]\n path[start] << [i, j]\n dfs([i, j], visited, map, path, start)\n end\n end\nend\n\nn, m, l = gets.chomp.split.map(&:to_i)\nmap = Hash.new { |hash, key| hash[key] = nil }\n\nn.times do |i|\n map[i] = gets.chomp.split(\"\")\nend\n\nvisited = Hash.new { |hash, key| hash[key] = false }\npath = Hash.new { |hash, key| hash[key] = [] }\n\nfor k, v in map\n for j in 0..(v.length - 1)\n if !visited[[k, j]] && v[j] == \".\"\n path[[k, j]] << [k, j]\n dfs([k, j], visited, map, path, [k, j])\n end\n end\nend\n\nresult = Hash.new { |hash, key| hash[key] = [] }\nfor k, v in path\n v.push(k)\n\n flag = true\n for i, j in v\n if i == 0 || i == n - 1 || j == 0 || j == m - 1\n break flag = false\n end\n end\n\n result[k] = v.length - 1 if flag\nend\n\ngroups = result.keys.count\nsizes = result.sort_by { |_k, v| v }\n\nmin_groups = groups - l\nmin = 0\n\nindex = []\nmin_groups.times do |i|\n min += sizes[i][1]\n index << sizes[i][0]\nend\nputs min\n\nfor i, j in index\n map[i][j] = \"*\"\nend\n\nfor i in 0..(n - 1)\n print *map[i]\n puts\nend\n"}, {"source_code": "def dfs(s, map, visited, path, start)\n k, j = s\n visited[s] = true\n\n list = [[k, j - 1], [k, j + 1], [k - 1, j], [k + 1, j]]\n\n for i, j in list\n if !visited[[i, j]] && map[i][j] == \".\"\n path[start] += 1\n dfs([i, j], map, visited, path, start)\n end\n end\nend\n\nn, m, l = gets.chomp.split.map(&:to_i)\n\nmap = Hash.new { |hash, key| hash[key] = [] }\nmap_2 = Hash.new { |hash, key| hash[key] = [] }\n\nn.times do |i|\n row = gets.chomp.split(\"\")\n map[i] = row\n map_2[i] = row.dup\nend\n\nfor i in 0..(n - 1)\n for j in 0..(m - 1)\n if (i == 0 || i == n - 1 || j == 0 || j == m - 1) && map[i][j] == \".\"\n map[i][j] = \"*\"\n map[i][j + 1] = \"*\" if j + 1 < m\n map[i][j - 1] = \"*\" if j - 1 >= 0\n map[i + 1][j] = \"*\" if i + 1 < n\n map[i - 1][j] = \"*\" if i - 1 >= 0\n end\n end\nend\n\nvisited = Hash.new { |hash, key| hash[key] = false }\npath = Hash.new { |hash, key| hash[key] = 0 }\n\nfor k, v in map\n for j in 0..(v.length - 1)\n if !visited[[k, j]] && v[j] == \".\"\n path[[k, j]] += 1\n dfs([k, j], map, visited, path, [k, j])\n end\n end\nend\n\ngroups = path.keys.count\nsizes = path.sort_by { |_k, v| v }\n\nmin_groups = groups - l\nmin = 0\n\nindex = []\nmin_groups.times do |i|\n min += sizes[i][1]\n index << sizes[i][0]\nend\n\nfor i, j in index\n map_2[i][j] = \"*\"\nend\n\nputs min\nfor i in 0..(n - 1)\n print *map_2[i]\n puts\nend\n"}], "src_uid": "e514d949e7837c603a9ee032f83b90d2"} {"nl": {"description": "The Little Elephant has an integer a, written in the binary notation. He wants to write this number on a piece of paper.To make sure that the number a fits on the piece of paper, the Little Elephant ought to delete exactly one any digit from number a in the binary record. At that a new number appears. It consists of the remaining binary digits, written in the corresponding order (possible, with leading zeroes).The Little Elephant wants the number he is going to write on the paper to be as large as possible. Help him find the maximum number that he can obtain after deleting exactly one binary digit and print it in the binary notation.", "input_spec": "The single line contains integer a, written in the binary notation without leading zeroes. This number contains more than 1 and at most 105 digits.", "output_spec": "In the single line print the number that is written without leading zeroes in the binary notation — the answer to the problem.", "sample_inputs": ["101", "110010"], "sample_outputs": ["11", "11010"], "notes": "NoteIn the first sample the best strategy is to delete the second digit. That results in number 112 = 310.In the second sample the best strategy is to delete the third or fourth digits — that results in number 110102 = 2610."}, "positive_code": [{"source_code": "puts gets.chomp.sub(/0|1$/,'')"}, {"source_code": "=begin\nDear online judge:\nI've wasted time reading the problem and trying to figure out the solution\nIf you insist on the penalty system, please don't include the time of debugging\nI hope my code compile and get accepted. KEE O.o\n ____________\n / __ \\\n / __ | | \\\n / |__| | | \\\n ( |__| )\n \\ /\n \\ ___ /\n \\____________/\n=end\n\nclass Solve\n def solve()\n $str = gets.chomp\n $res = \"\"\n $found = 0\n for $i in 0..($str.size() - 1)\n if $found == 1 || $str[$i] != '0'\n $res<<$str[$i]\n else\n $found = 1\n end\n end\n if $found == 0\n $res.chop!\n end\n puts $res\n end\nend\n\n\n\nSolve.new.solve"}, {"source_code": "str = gets.chomp\nn = str.index(\"0\")\nif n then\n str = str[0...n] + str[n+1..-1]\nelse\n str = str[0...-1]\nend\nputs str"}, {"source_code": "a = gets\na.sub!('1', '') unless a.sub!('0', '')\nputs a\n"}, {"source_code": "puts gets.sub /(.+?)0|./,'\\1'"}, {"source_code": "#!/usr/bin/ruby\n\ns = gets\npos = s.index('0') || 0\nputs s[0...pos] + s[pos+1..-1]\n"}, {"source_code": "puts gets.sub /(.+?)0|./,'\\1'\n"}, {"source_code": "puts gets.chomp.sub(/0|1$/,'')"}, {"source_code": "puts gets.sub /(.+?)0|./,'\\1'\n"}, {"source_code": "puts gets.sub /(.+?)0|./,'\\1'"}, {"source_code": "s = gets.strip\ni = s.index(\"0\"); i = 0 if i == nil\ns[i] = \"\"\nputs s"}, {"source_code": "s = gets\np = s.index \"0\"\nif p != nil\n s[p, s.size] = s[p+1, s.size];\nelse\n s[s.size - 2, 2] = s[s.size-1, 2]\nend\nputs s\n "}, {"source_code": "def run\n ARGF.readlines.each do |line|\n line.chomp!\n i = line.index('0') || 0\n puts line[0, i] + line[i + 1, line.length - i - 1]\n end\nend\n\nrun if $0 == __FILE__\n"}, {"source_code": "puts gets.sub /(.+?)0|./,'\\1'\n"}, {"source_code": "s = gets.chomp\nn = s.length\n(0..n).each do |i|\n\tif i == n\n\t\tputs s[1..n-1]\n\telsif s[i] == '0'\n\t\tprint s[0..i-1]\n\t\tprint s[i+1..n-1]\n\t\tbreak\n\tend\nend"}, {"source_code": "puts gets.sub /(.+?)0|./,'\\1'\n"}, {"source_code": "s=gets.chomp\nn1=0\nn2=0\nif s.size>1 && s.index(\"0\") then\n\tn1=s.sub(\"0\",\"\").to_i(2)\nend\nif s.size>1 && s.rindex(\"1\") then\n\tn2=s.reverse.sub(\"1\",\"\").reverse.to_i(2)\nend\nputs [n1,n2].max.to_s(2)\n\n"}, {"source_code": "binary = gets.strip.split('')\nif binary.index('0')\n binary.delete_at binary.index('0')\nelse\n binary.delete_at 0\nend\nputs binary.join\n"}, {"source_code": "# coding: utf-8\n\ns = gets.chomp\npos_zero = s.index('0')\n\nif pos_zero == nil\n puts s[0...-1]\nelse\n puts \"#{s[0...pos_zero]}#{s[(pos_zero+1)..-1]}\"\nend"}, {"source_code": "s = gets.strip\ni = s.index(\"0\")\nn = s.length\nif i.nil?\n puts s[1...n]\nelse\n puts (s[0...i] + s[(i + 1)...n]) \nend"}, {"source_code": "puts gets.sub /(.+?)0|./,'\\1'\n"}, {"source_code": "str = gets.chomp\npos = str.index('0')\npos = -1 if pos == nil\nstr.slice!(pos)\nstr = '0' if str.length == 0\nprint str\n"}, {"source_code": "s=gets.chomp\n\ns.length.times { |i|\n if s[i]==?0\n s[i]=''\n puts s\n exit\n end\n}\n\nputs s.chop\n\n"}, {"source_code": "s=gets.chomp\n\ns.length.times { |i|\n if s[i]==?0\n s[i]=''\n puts s\n exit\n end\n}\n\ns[-1]=''\nputs s\n"}, {"source_code": "s=gets\ns.sub!('1','') unless s.sub!('0','')\nputs s\n"}, {"source_code": "a = gets.chomp.chars.map(&:to_i)\nif a.include?(0)\n a[a.find_index{|x| x == 0}] = nil\nelse\n a[0] = nil\nend\nputs a.join(\"\")\n"}, {"source_code": "a = gets.chomp\nputs a.sub(a['0'] || '1', '')\n"}, {"source_code": "puts gets.sub /(.+?)0|./,'\\1'"}, {"source_code": "puts gets.sub /(.+?)0|./,'\\1'\n"}, {"source_code": "s = gets.strip\n\nif m = s.match(/^(1+)0(.*)$/)\n\tputs(m[1]+m[2])\nelse\n\tputs s.match(/^(1*?)1$/)[1]\nend\n"}, {"source_code": "puts gets.sub /(.+?)0|./,'\\1'\n"}, {"source_code": "a=gets\nif !a.sub! /0/,'' then\n\ta.sub! /1/,''\nend\nputs a\n"}, {"source_code": "s = gets.chomp\nif s.count('0') == 0\n\tputs s.chop\nelse\n\tid = s.index '0'\n\ts[id] = ''\n\tputs s\nend"}, {"source_code": "puts gets.sub /(.+?)0|./,'\\1'\n"}, {"source_code": "=begin\nsq = Array.new(3)\nfor i in 0..2\n sq[i] = gets.split.map(&:to_i)\nend\nsq[0][0] = sq[0][2]\nsq[2][2] = sq[0][0] + sq[0][1] - sq[1][2]\nsq[1][1] = sq[0][0] + sq[2][0] - sq[1][2]\nfor i in sq\n for j in i\n print j, \" \"\n end\n puts \"\"\nend\n=end\n\nbit = gets.strip\ni = bit.index \"0\"\nif i == nil then i = 0 end\nbit[i] = \"\"\nprint bit"}, {"source_code": "#!/usr/bin/ruby1.9\n\nline = STDIN.readline.strip\npos = line.index('0')\nif pos == nil\n\tpos = line.length-1\nend\nputs line.slice(0, pos)+line.slice(pos+1, line.length-1)\n"}, {"source_code": "s = gets.strip\ni = s.index(\"0\"); i = 0 if i == nil\nputs s[0...i]+s[i+1..-1]"}, {"source_code": "s = gets.strip\ni = s.index(\"0\"); i = 0 if i == nil\ns[i] = \"\"\nputs s"}, {"source_code": "s = gets.chomp.split('').map(&:to_i).to_a\ns.delete_at(s.index(0) == nil ? 0 : s.index(0))\nputs s*''"}, {"source_code": "n = gets.chomp.chars.to_a\nif i = n.index(\"0\")\n n.delete_at i\nelse\n n.delete_at 0\nend\nputs n.join\n"}, {"source_code": "a = gets\nb = \"\"\nf = false\na.each_char do |c|\n b << c if f || c == '1'\n f = true if c == '0'\nend\nb.chop! unless f\n\nputs b\n"}, {"source_code": "s = gets\n\nif s.match(/^0+$/) or s.match(/^1+$/) or s.match(/^0+1*$/)\n puts s[1..-1]\nelse\n s =~ /^(0*)(1+)0(.*)$/\n puts $1 + $2 + $3\nend\n"}, {"source_code": "a = gets.strip\n\nok = false\n\nif a == '1' * a.size\n\tprint '1' * (a.size-1)\nelse\n\ta.each_char do |c|\n\t\tif ok or c == '1'\n\t\t\tprint c\n\t\telsif c == '0'\n\t\t\tok = true\n\t\tend\n\tend\nend"}], "negative_code": [{"source_code": "s=gets.chomp\nn1=0\nn2=0\nif s.size>1 && s.index(\"0\") then\n\tn1=s.sub(\"0\",\"\").to_i(2)\nend\nif s.size>1 && s.rindex(\"1\") then\n\tn2=s.reverse.sub(\"1\",\"\").reverse.to_i(2)\nend\nputs [n1,n2].max\n\n"}, {"source_code": "s=gets\n\ns.length.times { |i|\n if s[i]==?0\n s[i]=''\n puts s\n exit\n end\n}\n\ns[-1]=''\nputs\n"}, {"source_code": "s = gets\n\nif s.match(/^0+$/) or s.match(/^1+$/) or s.match(/^0+1*$/)\n puts s[1..-1]\nelse\n s =~ /^(0*)(1+)0(.*)$/\n p $1, $2, $3\n puts $1 + $2 + $3\nend\n"}, {"source_code": "a = gets.strip\n\nok = false\n\na.each_char do |c|\n\tif ok or c == '1'\n\t\tprint c\n\telsif c == '0'\n\t\tok = true\n\tend\nend"}], "src_uid": "133eaf241bb1557ba9a3f59c733d34bf"} {"nl": {"description": "Sam is a kindergartener, and there are $$$n$$$ children in his group. He decided to create a team with some of his children to play \"brawl:go 2\".Sam has $$$n$$$ power-ups, the $$$i$$$-th has type $$$a_i$$$. A child's strength is equal to the number of different types among power-ups he has.For a team of size $$$k$$$, Sam will distribute all $$$n$$$ power-ups to $$$k$$$ children in such a way that each of the $$$k$$$ children receives at least one power-up, and each power-up is given to someone.For each integer $$$k$$$ from $$$1$$$ to $$$n$$$, find the minimum sum of strengths of a team of $$$k$$$ children Sam can get.", "input_spec": "Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 3 \\cdot 10^5$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 3 \\cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$) — types of Sam's power-ups. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 \\cdot 10^5$$$.", "output_spec": "For every test case print $$$n$$$ integers. The $$$k$$$-th integer should be equal to the minimum sum of strengths of children in the team of size $$$k$$$ that Sam can get.", "sample_inputs": ["2\n\n3\n\n1 1 2\n\n6\n\n5 1 2 2 2 4"], "sample_outputs": ["2 2 3 \n4 4 4 4 5 6"], "notes": "NoteOne of the ways to give power-ups to minimise the sum of strengths in the first test case: $$$k = 1: \\{1, 1, 2\\}$$$ $$$k = 2: \\{1, 1\\}, \\{2\\}$$$ $$$k = 3: \\{1\\}, \\{1\\}, \\{2\\}$$$ One of the ways to give power-ups to minimise the sum of strengths in the second test case: $$$k = 1: \\{1, 2, 2, 2, 4, 5\\}$$$ $$$k = 2: \\{2, 2, 2, 4, 5\\}, \\{1\\}$$$ $$$k = 3: \\{2, 2, 2, 5\\}, \\{1\\}, \\{4\\}$$$ $$$k = 4: \\{2, 2, 2\\}, \\{1\\}, \\{4\\}, \\{5\\}$$$ $$$k = 5: \\{2, 2\\}, \\{1\\}, \\{2\\}, \\{4\\}, \\{5\\}$$$ $$$k = 6: \\{1\\}, \\{2\\}, \\{2\\}, \\{2\\}, \\{4\\}, \\{5\\}$$$ "}, "positive_code": [{"source_code": "require 'set'\ngets.to_i.times {\n n = gets.to_i\n s = Set.new\n gets.split.map!(&:to_i).each { s.add _1 }\n m = s.size\n (1..n).each { |k|\n print [m, k].max, ' '\n }\n puts\n}\n"}], "negative_code": [], "src_uid": "06212223295c56a78a5d4e55c53a63e0"} {"nl": {"description": "A one-dimensional Japanese crossword can be represented as a binary string of length x. An encoding of this crossword is an array a of size n, where n is the number of segments formed completely of 1's, and ai is the length of i-th segment. No two segments touch or intersect.For example: If x = 6 and the crossword is 111011, then its encoding is an array {3, 2}; If x = 8 and the crossword is 01101010, then its encoding is an array {2, 1, 1}; If x = 5 and the crossword is 11111, then its encoding is an array {5}; If x = 5 and the crossword is 00000, then its encoding is an empty array. Mishka wants to create a new one-dimensional Japanese crossword. He has already picked the length and the encoding for this crossword. And now he needs to check if there is exactly one crossword such that its length and encoding are equal to the length and encoding he picked. Help him to check it!", "input_spec": "The first line contains two integer numbers n and x (1 ≤ n ≤ 100000, 1 ≤ x ≤ 109) — the number of elements in the encoding and the length of the crossword Mishka picked. The second line contains n integer numbers a1, a2, ..., an (1 ≤ ai ≤ 10000) — the encoding.", "output_spec": "Print YES if there exists exaclty one crossword with chosen length and encoding. Otherwise, print NO.", "sample_inputs": ["2 4\n1 3", "3 10\n3 3 2", "2 10\n1 3"], "sample_outputs": ["NO", "YES", "NO"], "notes": null}, "positive_code": [{"source_code": "n,m=gets.split.map{|e| e.to_i}\nsum=gets.split.map{|e| e.to_i}.inject(0){|sum,e| sum+e}+n-1\nif m==sum\n\tputs \"YES\"\nelse\n\tputs \"NO\"\nend"}, {"source_code": "n, x = gets.split.map &:to_i\nputs gets.split.map(&:to_i).inject(:+)+n==x+1?:YES:'NO'"}, {"source_code": "in_str = gets\nn, x = in_str.split(' ')\nn, x = n.to_i, x.to_i\nin_str = gets\ncrossword_arr = in_str.split(' ').map { |element| element.to_i }\nlength = 0\ncrossword_arr.each do |elem|\n length += elem + 1\nend\nif length - 1 == x\n puts 'YES'\nelse\n puts 'NO'\nend"}], "negative_code": [], "src_uid": "080a3458eaea4903da7fa4cf531beba2"} {"nl": {"description": "Inna loves sleeping very much, so she needs n alarm clocks in total to wake up. Let's suppose that Inna's room is a 100 × 100 square with the lower left corner at point (0, 0) and with the upper right corner at point (100, 100). Then the alarm clocks are points with integer coordinates in this square.The morning has come. All n alarm clocks in Inna's room are ringing, so Inna wants to turn them off. For that Inna has come up with an amusing game: First Inna chooses a type of segments that she will use throughout the game. The segments can be either vertical or horizontal. Then Inna makes multiple moves. In a single move, Inna can paint a segment of any length on the plane, she chooses its type at the beginning of the game (either vertical or horizontal), then all alarm clocks that are on this segment switch off. The game ends when all the alarm clocks are switched off. Inna is very sleepy, so she wants to get through the alarm clocks as soon as possible. Help her, find the minimum number of moves in the game that she needs to turn off all the alarm clocks!", "input_spec": "The first line of the input contains integer n (1 ≤ n ≤ 105) — the number of the alarm clocks. The next n lines describe the clocks: the i-th line contains two integers xi, yi — the coordinates of the i-th alarm clock (0 ≤ xi, yi ≤ 100). Note that a single point in the room can contain any number of alarm clocks and the alarm clocks can lie on the sides of the square that represents the room.", "output_spec": "In a single line print a single integer — the minimum number of segments Inna will have to draw if she acts optimally.", "sample_inputs": ["4\n0 0\n0 1\n0 2\n1 0", "4\n0 0\n0 1\n1 0\n1 1", "4\n1 1\n1 2\n2 3\n3 3"], "sample_outputs": ["2", "2", "3"], "notes": "NoteIn the first sample, Inna first chooses type \"vertical segments\", and then she makes segments with ends at : (0, 0), (0, 2); and, for example, (1, 0), (1, 1). If she paints horizontal segments, she will need at least 3 segments.In the third sample it is important to note that Inna doesn't have the right to change the type of the segments during the game. That's why she will need 3 horizontal or 3 vertical segments to end the game."}, "positive_code": [{"source_code": "k,l,a,b=0,0,Array.new(101,0),Array.new(101,0)\ngets.to_i.times do\n\tx,y=gets.chomp.split.map(&:to_i)\n\tif a[x]==0\n\t\ta[x]=1\n\t\tl+=1\n\tend\n\tif b[y]==0\n\t\tb[y]=1\n\t\tk+=1\n\tend\nend\nputs [k,l].min\n"}, {"source_code": "a = Array.new(2) { [0] * 101 }\ngets.to_i.times { x, y = gets.split.map &:to_i; a[0][x] += 1; a[1][y] += 1 }\np a.map { |e| e.count { |e| e > 0 } }.min"}, {"source_code": "n = gets.to_i\nx_arr = []\ny_arr = []\nn.times do\n\tx,y = gets.split(\" \").map(&:to_i)\n\tx_arr << x\n\ty_arr << y\nend\nh = x_arr.uniq.count\nv = y_arr.uniq.count\nputs [h,v].min"}], "negative_code": [], "src_uid": "89a8936bf7d43a9c5f69714a7cb7df82"} {"nl": {"description": "Pak Chanek has a prime number$$$^\\dagger$$$ $$$n$$$. Find a prime number $$$m$$$ such that $$$n + m$$$ is not prime.$$$^\\dagger$$$ A prime number is a number with exactly $$$2$$$ factors. The first few prime numbers are $$$2,3,5,7,11,13,\\ldots$$$. In particular, $$$1$$$ is not a prime number.", "input_spec": "Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$) — the number of test cases. The following lines contain the description of each test case. The only line of each test case contains a prime number $$$n$$$ ($$$2 \\leq n \\leq 10^5$$$).", "output_spec": "For each test case, output a line containing a prime number $$$m$$$ ($$$2 \\leq m \\leq 10^5$$$) such that $$$n + m$$$ is not prime. It can be proven that under the constraints of the problem, such $$$m$$$ always exists. If there are multiple solutions, you can output any of them. ", "sample_inputs": ["3\n\n7\n\n2\n\n75619"], "sample_outputs": ["2\n7\n47837"], "notes": "NoteIn the first test case, $$$m = 2$$$, which is prime, and $$$n + m = 7 + 2 = 9$$$, which is not prime.In the second test case, $$$m = 7$$$, which is prime, and $$$n + m = 2 + 7 = 9$$$, which is not prime.In the third test case, $$$m = 47837$$$, which is prime, and $$$n + m = 75619 + 47837 = 123456$$$, which is not prime."}, "positive_code": [{"source_code": "for i in 1..gets.to_i\r\n puts 7\r\nend"}, {"source_code": "for i in 1..gets.to_i\r\n puts gets.to_i\r\nend"}, {"source_code": "n=gets.to_i\nn.times do\n t=gets.to_i\n puts 7\nend"}], "negative_code": [{"source_code": "for i in 1..gets.to_i\r\n puts 2*gets.to_i\r\nend"}], "src_uid": "b7e36ca8a96dd7951359070d4953beec"} {"nl": {"description": "You have an array $$$a$$$ of length $$$n$$$. You can exactly once select an integer $$$len$$$ between $$$1$$$ and $$$n - 1$$$ inclusively, and then sort in non-decreasing order the prefix of the array of length $$$len$$$ and the suffix of the array of length $$$n - len$$$ independently.For example, if the array is $$$a = [3, 1, 4, 5, 2]$$$, and you choose $$$len = 2$$$, then after that the array will be equal to $$$[1, 3, 2, 4, 5]$$$.Could it be that after performing this operation, the array will not be sorted in non-decreasing order?", "input_spec": "There are several test cases in the input data. The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 100$$$) — the number of test cases. This is followed by the test cases description. The first line of each test case contains one integer $$$n$$$ ($$$2 \\leq n \\leq 10^4$$$) — the length of the array. The second line of the test case contains a sequence of integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\leq a_i \\leq 10^9$$$) — the array elements. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.", "output_spec": "For each test case of input data, output \"YES\" (without quotes), if the array may be not sorted in non-decreasing order, output \"NO\" (without quotes) otherwise. You can output each letter in any case (uppercase or lowercase).", "sample_inputs": ["3\n3\n2 2 1\n4\n3 1 2 1\n5\n1 2 2 4 4"], "sample_outputs": ["YES\nYES\nNO"], "notes": "NoteIn the first test case, it's possible to select $$$len = 1$$$, then after operation, the array will not be sorted in non-decreasing order and will be equal to $$$[2, 1, 2]$$$.In the second test case, it's possible to select $$$len = 3$$$, then after operation, the array will not be sorted in non-decreasing order and will be equal to $$$[1, 2, 3, 1]$$$.In the third test case, the array will be sorted in non-decreasing order for every possible $$$len$$$."}, "positive_code": [{"source_code": "$__cur_readed = []\r\n$__cur_index = 0\r\n\r\ndef rs1()\r\n\twhile $__cur_index >= $__cur_readed.length\r\n\t\t$__cur_readed = gets.split(\" \")\r\n\t\t$__cur_index = 0\r\n\tend\r\n\t$__cur_index+=1\r\n\treturn $__cur_readed[$__cur_index-1]\r\nend\r\n\r\ndef rs2()\r\n\tcur = \"\"\r\n\twhile true\r\n\t\tif STDIN.eof \r\n\t\t\treturn cur \r\n\t\tend\r\n\t\tc = STDIN.readchar\r\n\t\t#puts \"cur s is \" + c\r\n\t\tif c != \" \" and c != \"\\n\"\r\n\t\t\tcur += c\r\n\t\telse\r\n\t\t\tif cur != \"\"\r\n\t\t\t\treturn cur\r\n\t\t\tend\r\n\t\tend\r\n\tend\r\nend\r\n\r\ndef ri()\r\n\treturn rs1.to_i\r\nend\r\n\r\nt = ri\r\n\r\nfor curt in (1..t)\r\n\tn = ri\r\n\tarr = Array.new(n) {|i| ri}\r\n\tputs arr == arr.sort ? \"NO\" : \"YES\"\r\nend"}, {"source_code": "$__cur_readed = []\r\n$__cur_index = 0\r\n\r\ndef ri()\r\n\twhile $__cur_index >= $__cur_readed.length\r\n\t\t$__cur_readed = gets.split(\" \")\r\n\t\t$__cur_index = 0\r\n\tend\r\n\t$__cur_index+=1\r\n\treturn $__cur_readed[$__cur_index-1]\r\nend\r\n\r\ndef rs()\r\n\tcur = \"\"\r\n\twhile true\r\n\t\tif STDIN.eof \r\n\t\t\treturn cur \r\n\t\tend\r\n\t\tc = STDIN.readchar\r\n\t\t#puts \"cur s is \" + c\r\n\t\tif c != \" \" and c != \"\\n\"\r\n\t\t\tcur += c\r\n\t\telse\r\n\t\t\tif cur != \"\"\r\n\t\t\t\treturn cur\r\n\t\t\tend\r\n\t\tend\r\n\tend\r\nend\r\n\r\nt = rs.to_i\r\n\r\nfor curt in (1..t)\r\n\tn = rs.to_i\r\n\tarr = Array.new(n) {|i| rs.to_i}\r\n\tputs arr == arr.sort ? \"NO\" : \"YES\"\r\nend"}, {"source_code": "$__cur_readed = []\r\n$__cur_index = 0\r\n\r\ndef ri()\r\n\twhile $__cur_index >= $__cur_readed.length\r\n\t\t$__cur_readed = gets.split(\" \")\r\n\t\t$__cur_index = 0\r\n\tend\r\n\t$__cur_index+=1\r\n\treturn $__cur_readed[$__cur_index-1]\r\nend\r\n\r\nt = ri.to_i\r\n\r\nfor curt in (1..t)\r\n\tn = ri.to_i\r\n\tarr = Array.new(n) {|i| ri.to_i}\r\n\tputs arr == arr.sort ? \"NO\" : \"YES\"\r\nend"}, {"source_code": "gets.to_i.times {\n gets\n xs = gets.split.map(&:to_i)\n if xs == xs.sort\n puts \"NO\"\n else\n puts \"YES\"\n end\n}\n"}], "negative_code": [], "src_uid": "ef1448a744f67347183479c697aa87e1"} {"nl": {"description": "There are $$$n$$$ boxers, the weight of the $$$i$$$-th boxer is $$$a_i$$$. Each of them can change the weight by no more than $$$1$$$ before the competition (the weight cannot become equal to zero, that is, it must remain positive). Weight is always an integer number.It is necessary to choose the largest boxing team in terms of the number of people, that all the boxers' weights in the team are different (i.e. unique).Write a program that for given current values ​$$$a_i$$$ will find the maximum possible number of boxers in a team.It is possible that after some change the weight of some boxer is $$$150001$$$ (but no more).", "input_spec": "The first line contains an integer $$$n$$$ ($$$1 \\le n \\le 150000$$$) — the number of boxers. The next line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$, where $$$a_i$$$ ($$$1 \\le a_i \\le 150000$$$) is the weight of the $$$i$$$-th boxer.", "output_spec": "Print a single integer — the maximum possible number of people in a team.", "sample_inputs": ["4\n3 2 4 1", "6\n1 1 1 4 4 4"], "sample_outputs": ["4", "5"], "notes": "NoteIn the first example, boxers should not change their weights — you can just make a team out of all of them.In the second example, one boxer with a weight of $$$1$$$ can be increased by one (get the weight of $$$2$$$), one boxer with a weight of $$$4$$$ can be reduced by one, and the other can be increased by one (resulting the boxers with a weight of $$$3$$$ and $$$5$$$, respectively). Thus, you can get a team consisting of boxers with weights of $$$5, 4, 3, 2, 1$$$."}, "positive_code": [{"source_code": "n = gets.to_i\naa = gets.split.map(&:to_i).sort\n\nif n==1\n puts 1\n exit\nend\n\nmax = 1\nidx = 1\nprev_num = aa[0]\n\n# 選出フラグ\nprev= aa[0]>1 ? true : false\ncurrent= aa[0]>1 ? false : true\nnex=false\n\nwhile idx < n do\n target = aa[idx]\n idx+=1\n\n # フラグ移動\n if target == prev_num\n elsif target == prev_num+1\n prev = current\n current = nex\n nex = false\n elsif target == prev_num+2\n prev = nex\n current = false\n nex = false\n else\n prev = false\n current = false\n nex = false\n end\n #puts \"target:#{target},#{prev},#{current},#{nex}\"\n\n # 小さい方から埋めていく\n if !prev && target>1\n prev_num = target\n prev = true\n max+=1\n elsif !current\n prev_num = target\n current = true\n max+=1\n elsif !nex\n prev_num = target\n nex = true\n max+=1\n end\n #puts max\nend\nputs max\n"}], "negative_code": [{"source_code": "n = gets.to_i\naa = gets.split.map(&:to_i).sort\n\nidx = 0\nmax = 0\n\nwhile idx < n do\n if (max..(max+2)).cover?(aa[idx])\n max+=1\n elsif aa[idx] >= max+3\n break\n end\n idx+=1\nend\nputs max\n"}], "src_uid": "41215d764f10c025bf18a70b6215aecf"} {"nl": {"description": "\"Hey, it's homework time\" — thought Polycarpus and of course he started with his favourite subject, IT. Polycarpus managed to solve all tasks but for the last one in 20 minutes. However, as he failed to solve the last task after some considerable time, the boy asked you to help him.The sequence of n integers is called a permutation if it contains all integers from 1 to n exactly once.You are given an arbitrary sequence a1, a2, ..., an containing n integers. Each integer is not less than 1 and not greater than 5000. Determine what minimum number of elements Polycarpus needs to change to get a permutation (he should not delete or add numbers). In a single change he can modify any single sequence element (i. e. replace it with another integer).", "input_spec": "The first line of the input data contains an integer n (1 ≤ n ≤ 5000) which represents how many numbers are in the sequence. The second line contains a sequence of integers ai (1 ≤ ai ≤ 5000, 1 ≤ i ≤ n).", "output_spec": "Print the only number — the minimum number of changes needed to get the permutation.", "sample_inputs": ["3\n3 1 2", "2\n2 2", "5\n5 3 3 3 1"], "sample_outputs": ["0", "1", "2"], "notes": "NoteThe first sample contains the permutation, which is why no replacements are required.In the second sample it is enough to replace the first element with the number 1 and that will make the sequence the needed permutation.In the third sample we can replace the second element with number 4 and the fourth element with number 2."}, "positive_code": [{"source_code": "require 'set'\nn = gets.to_i\na = Set.new(gets.split(' ').map(&:to_i))\nres = 0\nn.times do |i|\n unless a.include?(i+1); res += 1; end;\nend\np res\n"}, {"source_code": "n = gets.to_i\na = gets.split(' ').map(&:to_i)\nres = 0\nn.times do |i|\n unless a.include?(i+1); res += 1; end;\nend\np res\n"}, {"source_code": "p ([*1..gets.to_i]-gets.split.map(&:to_i).uniq).size\n"}, {"source_code": "n = gets.to_i\na = gets.split(' ').map(&:to_i)\nres = 0\nn.times do |i|\n if a.index(i+1) == nil; res += 1; end;\nend\np res\n"}, {"source_code": "def solve n, list\n (1..n).to_a.select{|i| list.include?(i) == false}.length\nend\n\nn = STDIN.gets.chomp.to_i\nlist = STDIN.gets.chomp.split(' ').map{|ch| ch.to_i}\nputs solve n, list"}, {"source_code": "n = gets.to_i\np n - gets.split.map(&:to_i).grep(1..n).uniq.size\n"}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\ncnt=Array.new(n+1,0)\na.each{|i|\n if i<=n then\n cnt[i]=1\n end\n}\nans=n\n1.upto(n+1){|i|\n if cnt[i]==1 then\n ans-=1\n end\n}\np ans\n"}, {"source_code": "n = gets.to_i \np n - gets.split(' ').map{|i| i.to_i}.delete_if{|i| i > n}.uniq.size\n\n\n\n"}, {"source_code": "n = gets.to_i \nputs n - gets.split(' ').map{|i| i.to_i}.delete_if{|i| i > n}.uniq.size\n\n\n\n"}, {"source_code": "p ([*1..gets.to_i]-gets.split.map(&:to_i)).size\n"}, {"source_code": "p ([*1..gets.to_i]-gets.split.map(&:to_i)).size\n"}, {"source_code": "require 'set'\nn = gets().to_i\ns = Set.new(gets().split(' ').map(&:to_i))\n\nret = 0\nfor i in 1..n\n if not s.include?(i) then\n ret += 1\n end\nend\nputs ret\n\n"}, {"source_code": "require 'set'\nn = gets().to_i\ns = Set.new(gets().split(' ').map(&:to_i))\nputs (1..n).reduce(0){|a,b| a+((s.include? b)?0:1)}\n\n"}, {"source_code": "n = STDIN.gets.chomp.to_i\nary = STDIN.gets.chomp.split(' ').map{|x| x.to_i }\nary = ary[0...n]\nmistaken = ary.size - ary.uniq.size\nmistaken += ary.uniq.select{|x| x > n }.size\nmistaken += ary.uniq.select{|x| x < 1 }.size\np mistaken\n\n"}, {"source_code": "p ([*1..gets.to_i]-gets.split.map(&:to_i)).size"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\np n - ((1..n).to_a&a).uniq.size"}, {"source_code": "p ([*1..gets.to_i]-gets.split.map(&:to_i)).size\n"}, {"source_code": "p ([*1..gets.to_i]-gets.split.map(&:to_i)).size\n"}, {"source_code": "p ([*1..gets.to_i]-gets.split.map(&:to_i)).uniq.size"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nb = Array.new(n){0}\nm = 0\n\na.each do |i|\n if i <= n && b[i-1] == 0\n b[i-1] = 1 \n else\n m += 1\n end\nend\n\np m\n"}, {"source_code": "p ([*1..gets.to_i]-gets.split.map(&:to_i)).size"}, {"source_code": "#=================================\n# Better be sure than sorry. \n#=================================\n$stdin=File.open(\"./input.txt\",\"r\") if ENV[\"LOGNAME\"]==\"skydos\"\nrequire 'prime'\n#require 'mathn'\nrequire 'matrix'\nrequire 'set'\ninclude Math\n#=================================\n\nn=gets.to_i\ndata=gets.split.map(&:to_i)\n\nhs=Hash.new(0)\ndata.each{|v| hs[v]+=1}\n\nans=0\nn.times{|i|\n\ti+=1\n\t\n\tif hs[i]==0\n\t\tans+=1\n\tend\n}\n\nputs ans\n"}, {"source_code": "i = gets.to_i \nputs i - gets.chomp.split().uniq.count { |x| x.to_i <= i }"}, {"source_code": "n = gets.to_i\nar = gets.split(' ').map(&:to_i)\nar2 = (1..n).to_a\nputs (ar2-ar).count\n"}, {"source_code": "p ([*1..gets.to_i]-gets.split.map(&:to_i)).size"}, {"source_code": "def main()\n\tn=gets.chomp.to_i\n\tsta=gets.chomp.split(\" \").map{|i| i.to_i}\n\tsta=sta.uniq\n\tchange=0\n\tfor i in 1..n\n\t\tif !sta.include?(i)\n\t\t\tchange+=1\n\t\tend\n\tend\n\tputs change\nend\nmain()"}, {"source_code": "p ([*1..gets.to_i]-gets.split.map(&:to_i)).size\n"}, {"source_code": "n,k=gets.to_i,gets.split.map(&:to_i);ch=0;n.times {|x| k.include?(x+1) ?0:ch+=1};puts ch"}, {"source_code": "n = gets.to_i\ns = gets\na = s.split\nb = [ ]\n0.upto(n-1) {\n\t|i|\n\ta[i] = a[i].to_i\n\tb[a[i]] = 1\n}\ns = 0\n0.upto(n-1) {\n\t|i|\n\ts = s + 1 if b[i+1] != 1\n}\nputs s"}, {"source_code": "p ([*1..gets.to_i]-gets.split.map(&:to_i)).size\n"}, {"source_code": "puts(((1..gets.to_i).to_a - gets.split.map { |x| x.to_i }).size)"}, {"source_code": "#!/usr/bin/env ruby\nn = gets.to_i\ncount = [false] * 5001\ngets.split.each { |x| \n count[x.to_i] = true\n}\nresult = n\n1.upto(n) { |x| \n result -= 1 if count[x]\n}\nputs result\n"}, {"source_code": "n = gets.to_i\na = gets.chomp.split(\" \").map{|i| i.to_i}\nb = []\ndem = 0\n\n(1..n).each do |i|\n b[i] = 0\nend\n\n(0..n-1).each do |i|\n if b[a[i]] == 0\n b[a[i]] = 1\n dem +=1\n end\nend\n\nputs (n-dem)"}, {"source_code": "require 'set'\nn = gets.chomp.to_i\na = gets.chomp.split(\" \").map {|e| e.to_i}\n\nset = {}\na.each do |e|\n if (1 <= e && e <= n)\n set[e] = 1\n end\nend\n\nputs n - set.keys.length\n"}, {"source_code": "p ([*1..gets.to_i]-gets.split.map(&:to_i)).size\n"}, {"source_code": "p ([*1..gets.to_i]-gets.split.map(&:to_i)).size\n"}, {"source_code": "n = gets.chomp.to_i\nputs gets.chomp.split.map(&:to_i).reduce(Array(1..n)) { |s, x| s - [x] }.length"}], "negative_code": [{"source_code": "p gets.to_i-gets.split.map(&:to_i).uniq.size\n"}, {"source_code": "n = gets.to_i\na = gets.split(' ').map(&:to_i).sort\nres = 0\nn.times do |i|\n if a[i] != i+1; res += 1; end;\nend\np res\n"}, {"source_code": "n = gets.to_i\nm = gets.chomp.split(\" \").collect! {|i| i}\n\nputs n - m.uniq.size\n\n\n"}, {"source_code": "n = gets.to_i \nputs n - gets.split(' ').map{|i| i.to_i}.delete_if{|i| i > n}.size\n\n\n\n"}, {"source_code": "gets()\nd = Hash.new(0)\ngets().split(' ').map(&:to_i).each do |e|\n d[e] += 1\nend\n\nret = 0\n\nd.each do |k,v|\n ret += v - 1\nend\n\nputs ret\n\n"}, {"source_code": "gets()\nd = Hash.new(0)\ngets().split(' ').map(&:to_i).each do |e|\n d[e] += 1\nend\n\nret = 0\n\nd.each do |k,v|\n ret += d[k] - 1\nend\n\nputs ret\n\n"}, {"source_code": "n = STDIN.gets.chomp.to_i\nary = STDIN.gets.chomp.split(' ').map{|x| x.to_i }\nary = ary[0...n]\np ary.size - ary.uniq.size\n"}, {"source_code": "n = STDIN.gets.chomp.to_i\nary = STDIN.gets.chomp.split(' ').map{|x| x.to_i }\nary = ary[0...n]\nmistaken = ary.size - ary.uniq.size\nmistaken += ary.select{|x| x > n }.size\nmistaken += ary.select{|x| x < 1 }.size\np mistaken\n\n"}, {"source_code": "n = STDIN.gets.chomp.to_i\nary = STDIN.gets.chomp.split(' ')\nary = ary[0...n]\np ary.size - ary.uniq.size\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nputs n - a.uniq.size"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nb = Array.new(n){0}\nm = 0\n\na.each do |i|\n if i > n ||b[i-1] == 0\n b[i-1] = 1 \n else\n m += 1\n end\nend\n\np m\n"}, {"source_code": "#=================================\n# Better be sure than sorry. \n#=================================\n$stdin=File.open(\"./input.txt\",\"r\") if ENV[\"LOGNAME\"]==\"skydos\"\nrequire 'prime'\n#require 'mathn'\nrequire 'matrix'\nrequire 'set'\ninclude Math\n#=================================\n\nn=gets.to_i\ndata=gets.split.map(&:to_i)\n\nhs=Hash.new(0)\ndata.each{|v| hs[v]+=1}\n\nans=0\nn.times{|i|\n\ti+=1\n\t\n\tif hs[i]>1\n\t\tans+=hs[i]-1\n\tend\n}\n\nputs ans\n"}, {"source_code": "def main()\n\tn=gets.chomp.to_i\n\tsta=gets.chomp.split(\" \").map{|i| i.to_i}\n\tsta=sta.sort\n\tchange=0\n\tfor i in 1..n\n\t\tif sta[i-1]!=i\n\t\t\tchange+=1\n\t\tend\n\tend\n\tputs change\nend\nmain()"}, {"source_code": "n,k=gets.to_i,gets.split.map(&:to_i);ch=0;n.times {|x| k.include?(x) ?0:ch+=1};puts ch"}, {"source_code": "n,k=gets.to_i,gets.split.map(&:to_i);ch=0;k=k.sort;n.times {|x| k[x]==x+1 ?0:ch+=1};puts ch"}, {"source_code": "n = gets.to_i\ns = gets\na = s.split\n0.upto(n-1) {\n\t|i|\n\ta[i] = a[i].to_i\n}\na = a.sort\ns = 0\ni = 0\nwhile i < n\n\ts = s + (a[i]-i-1).abs\n\ti = i + 1\nend\nputs s"}, {"source_code": "n = gets.to_i\ns = gets\na = s.split\n0.upto(n-1) {\n\t|i|\n\ta[i] = a[i].to_i\n}\na = a.sort\ns = 0\ni = 0\nwhile i < n\n\ts = s + 1 if a[i] != i + 1\n\ti = i + 1\nend\nputs s"}, {"source_code": "require 'set'\nn = gets.chomp.to_i\na = gets.chomp.split(\" \").map {|e| e.to_i}\n\nset = {}\na.each do |e|\n set[e] = 1\nend\n\nputs n - set.keys.length\n"}, {"source_code": "n = gets.chomp.to_i\nputs gets.chomp.split.map(&:to_i).reduce(Array(1..n)) { |s, x| s - [x]}"}, {"source_code": "n = gets.chomp.to_i\ngets.chomp.split.map(&:to_i).reduce(Array(1..n)) { |s, x| s - [x]}"}], "src_uid": "bdd86c8bc54bbac6e2bb5a9d68b6eb1c"} {"nl": {"description": "Sasha and Dima want to buy two $$$n$$$-tier cakes. Each cake should consist of $$$n$$$ different tiers: from the size of $$$1$$$ to the size of $$$n$$$. Tiers should go in order from the smallest to the biggest (from top to bottom).They live on the same street, there are $$$2 \\cdot n$$$ houses in a row from left to right. Each house has a pastry shop where you can buy a cake tier. Unfortunately, in each pastry shop you can buy only one tier of only one specific size: in the $$$i$$$-th house you can buy a tier of the size $$$a_i$$$ ($$$1 \\le a_i \\le n$$$).Since the guys carry already purchased tiers, and it is impossible to insert a new tier in the middle of the cake, they agreed to buy tiers from the smallest to the biggest. That is, each of them buys tiers in order: $$$1$$$, then $$$2$$$, then $$$3$$$ and so on up to $$$n$$$.Initially, Sasha and Dima are located near the first (leftmost) house. Output the minimum distance that they will have to walk in total to buy both cakes. The distance between any two neighboring houses is exactly $$$1$$$.", "input_spec": "The first line of the input contains an integer number $$$n$$$ — the number of tiers in each cake ($$$1 \\le n \\le 10^5$$$). The second line contains $$$2 \\cdot n$$$ integers $$$a_1, a_2, \\dots, a_{2n}$$$ ($$$1 \\le a_i \\le n$$$), where $$$a_i$$$ is equal to the size of the tier, which can be bought in the $$$i$$$-th house. Remember that in each house you can buy only one tier. It is guaranteed that every number from $$$1$$$ to $$$n$$$ occurs in $$$a$$$ exactly two times.", "output_spec": "Print one number  — the minimum distance that the guys have to walk in total to buy both cakes. Guys can be near same house at the same time. They begin near the first (leftmost) house. Each of the guys should buy $$$n$$$ tiers in ascending order of their sizes.", "sample_inputs": ["3\n1 1 2 2 3 3", "2\n2 1 1 2", "4\n4 1 3 2 2 3 1 4"], "sample_outputs": ["9", "5", "17"], "notes": "NoteIn the first example, the possible optimal sequence of actions is: Sasha buys a tier of size $$$1$$$ near the $$$1$$$-st house ($$$a_1=1$$$); Dima goes to the house $$$2$$$; Dima buys a tier of size $$$1$$$ near the $$$2$$$-nd house ($$$a_2=1$$$); Sasha goes to the house $$$4$$$; Sasha buys a tier of size $$$2$$$ near the $$$4$$$-th house ($$$a_4=2$$$); Sasha goes to the house $$$5$$$; Sasha buys a tier of size $$$3$$$ near the $$$5$$$-th house ($$$a_5=3$$$); Dima goes to the house $$$3$$$; Dima buys a tier of size $$$2$$$ near the $$$3$$$-rd house ($$$a_3=2$$$); Dima goes to the house $$$6$$$; Dima buys a tier of size $$$3$$$ near the $$$6$$$-th house ($$$a_6=3$$$). So, Sasha goes the distance $$$3+1=4$$$, and Dima goes the distance $$$1+1+3=5$$$. In total, they cover a distance of $$$4+5=9$$$. You can make sure that with any other sequence of actions they will walk no less distance."}, "positive_code": [{"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\n\nmap = {}\na.each_with_index do |s, i|\n map[s] ||= []\n map[s] << i\nend\n\nposns = [0,0]\ntotal_dist = 0\n(1..n).each do |tier|\n destns = map[tier]\n dist = [\n (posns[0]-destns[0]).abs + (posns[1]-destns[1]).abs,\n (posns[0]-destns[1]).abs + (posns[1]-destns[0]).abs,\n ].min\n total_dist += dist\n posns = destns\nend\n\np total_dist\n"}], "negative_code": [], "src_uid": "dc9c2703aa7aaf1d254211cf06030329"} {"nl": {"description": "Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n \\times m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$|a-c| + |b-d|$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for $$$k = 0, 1, \\dots, n \\cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity! ", "input_spec": "The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \\leq t \\leq 5 \\cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \\leq n \\cdot m \\leq 10^5$$$) — the number of rows and columns of seats in the classroom. The sum of $$$n \\cdot m$$$ across all test cases does not exceed $$$10^5$$$.", "output_spec": "For each test case, output $$$n \\cdot m$$$ ordered integers — the distance between Rahul and Tina if both of them act optimally for every $$$k \\in [0, n \\cdot m - 1]$$$.", "sample_inputs": ["2\n\n4 3\n\n1 2"], "sample_outputs": ["3 3 4 4 4 4 4 4 5 5 5 5 \n1 1"], "notes": "NoteOne possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows.Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$|3-1| + |1-3| = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$|2 - 4| + |2 - 3| = 3$$$.Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$. "}, "positive_code": [{"source_code": "gets.to_i.times do\r\n n, m = gets.split.map(&:to_i)\r\n corners = [[1, 1], [1, m], [n, 1], [n, m]]\r\n puts(1.upto(n).flat_map do |i|\r\n 1.upto(m).map do |j|\r\n corners.map { |ii, jj| (ii - i).abs + (jj - j).abs }.max\r\n end\r\n end.sort.join(\" \"))\r\nend"}], "negative_code": [], "src_uid": "dc1dc5e5dd17d19760c772739ce244a7"} {"nl": {"description": "Polycarp is reading a book consisting of $$$n$$$ pages numbered from $$$1$$$ to $$$n$$$. Every time he finishes the page with the number divisible by $$$m$$$, he writes down the last digit of this page number. For example, if $$$n=15$$$ and $$$m=5$$$, pages divisible by $$$m$$$ are $$$5, 10, 15$$$. Their last digits are $$$5, 0, 5$$$ correspondingly, their sum is $$$10$$$.Your task is to calculate the sum of all digits Polycarp has written down.You have to answer $$$q$$$ independent queries.", "input_spec": "The first line of the input contains one integer $$$q$$$ ($$$1 \\le q \\le 1000$$$) — the number of queries. The following $$$q$$$ lines contain queries, one per line. Each query is given as two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n, m \\le 10^{16}$$$) — the number of pages in the book and required divisor, respectively.", "output_spec": "For each query print the answer for it — the sum of digits written down by Polycarp.", "sample_inputs": ["7\n1 1\n10 1\n100 3\n1024 14\n998244353 1337\n123 144\n1234312817382646 13"], "sample_outputs": ["1\n45\n153\n294\n3359835\n0\n427262129093995"], "notes": null}, "positive_code": [{"source_code": "q = readline.to_i\n\nq.times do\n n, m = readline.split.map(&:to_i)\n ans = 0\n l = []\n c = m\n until l.include?(c % 10)\n l << c % 10\n c += m\n end\n\n k = l.size\n\n l.each_with_index do |v, i|\n ans += (n / (m * k)) * v\n n % (m * k) >= (i + 1) * m && ans += v\n end\n\n puts ans\nend\n"}, {"source_code": "def calc(b, range)\n\trange.sum { |i| b * i % 10 }\nend\n\ngets.to_i.times do\n\ta, b = gets.split.map(&:to_i)\n\tputs calc(b, 0...10) * (a / b / 10) + calc(b, 0..a / b % 10)\nend\n"}, {"source_code": "# coding: utf-8\n\nq = gets.to_i\nq.times {\n n, m = gets.split.map(&:to_i)\n sum = 0\n i = 1\n ten_round = []\n for i in 1..10\n x = m*i\n ten_round << x%10\n end\n # n = q*m + r\n q = n / (m*10)\n r = n % (m*10)\n sum = ten_round.reduce(:+) * q\n # take care of 'r'\n for i in 1..10\n x = m*i\n if x <= r\n sum += ten_round[i-1]\n else\n break\n end\n end\n puts sum\n}"}, {"source_code": "gets.strip.to_i.times do\n n, m = gets.strip.split(' ').map(&:to_i)\n divs = (1..10).to_a.map { |i| (i * m) }\n last = (1..10).to_a.map { |i| (i * m) % 10 }\n\n\n res1 = (last.inject(:+) * (n / divs[-1]))\n res2 = divs.select { |d| d <= (n % divs[-1]) }.map { |x| x % 10 }.inject(:+)\n puts (res1 || 0) + (res2 || 0)\nend"}, {"source_code": "gets.to_i.times do\n n, m = gets.split.map(&:to_i)\n cnt = n / m\n d = []\n (0..9).each do |i|\n d << (((i + 1) * m) % 10)\n end\n sum = 0\n (0...cnt % 10).each do |i|\n sum += d[i]\n end\n sum2 = 0\n (0..9).each do |i|\n sum2 += d[i]\n end\n sum += cnt / 10 * sum2\n puts sum\nend"}, {"source_code": "qq = gets.to_i\nnm =\n qq.times.map do |q|\n gets.split.map(&:to_i)\n end\nnm.each do |nn,mm|\n ans = 0\n # 10倍でループする\n num10 = 0\n 1.upto(9) do |n|\n num10 += (mm*n) % 10\n end\n\n div = nn / (mm*10)\n ans += div * num10\n\n mod = nn % (mm*10)\n a = mm\n while a <= mod do\n ans += a%10\n a+=mm\n end\n\n puts ans\nend\n"}], "negative_code": [], "src_uid": "9964bfdcfdd041b839ce120019e8220f"} {"nl": {"description": "Daniel has a string s, consisting of lowercase English letters and period signs (characters '.'). Let's define the operation of replacement as the following sequence of steps: find a substring \"..\" (two consecutive periods) in string s, of all occurrences of the substring let's choose the first one, and replace this substring with string \".\". In other words, during the replacement operation, the first two consecutive periods are replaced by one. If string s contains no two consecutive periods, then nothing happens.Let's define f(s) as the minimum number of operations of replacement to perform, so that the string does not have any two consecutive periods left.You need to process m queries, the i-th results in that the character at position xi (1 ≤ xi ≤ n) of string s is assigned value ci. After each operation you have to calculate and output the value of f(s).Help Daniel to process all queries.", "input_spec": "The first line contains two integers n and m (1 ≤ n, m ≤ 300 000) the length of the string and the number of queries. The second line contains string s, consisting of n lowercase English letters and period signs. The following m lines contain the descriptions of queries. The i-th line contains integer xi and ci (1 ≤ xi ≤ n, ci — a lowercas English letter or a period sign), describing the query of assigning symbol ci to position xi.", "output_spec": "Print m numbers, one per line, the i-th of these numbers must be equal to the value of f(s) after performing the i-th assignment.", "sample_inputs": ["10 3\n.b..bz....\n1 h\n3 c\n9 f", "4 4\n.cc.\n2 .\n3 .\n2 a\n1 a"], "sample_outputs": ["4\n3\n1", "1\n3\n1\n1"], "notes": "NoteNote to the first sample test (replaced periods are enclosed in square brackets).The original string is \".b..bz....\". after the first query f(hb..bz....) = 4    (\"hb[..]bz....\"  →  \"hb.bz[..]..\"  →  \"hb.bz[..].\"  →  \"hb.bz[..]\"  →  \"hb.bz.\") after the second query f(hbс.bz....) = 3    (\"hbс.bz[..]..\"  →  \"hbс.bz[..].\"  →  \"hbс.bz[..]\"  →  \"hbс.bz.\") after the third query f(hbс.bz..f.) = 1    (\"hbс.bz[..]f.\"  →  \"hbс.bz.f.\")Note to the second sample test.The original string is \".cc.\". after the first query: f(..c.) = 1    (\"[..]c.\"  →  \".c.\") after the second query: f(....) = 3    (\"[..]..\"  →  \"[..].\"  →  \"[..]\"  →  \".\") after the third query: f(.a..) = 1    (\".a[..]\"  →  \".a.\") after the fourth query: f(aa..) = 1    (\"aa[..]\"  →  \"aa.\")"}, "positive_code": [{"source_code": "n,q = gets.chomp.split.map{|x| x.to_i}\n\nstr = gets.chomp\nprev = str[0]\ntotal = 0\n(1..n-1).each do |i|\n\tcurr = str[i]\n\tif prev == curr and prev == '.'\n\t\ttotal+=1\n\tend\n\tprev = curr\nend\n\nq.times do\n\ti,c = gets.chomp.split\n\ti = (i.to_i)-1\n\t\n\tif c == '.'\n\t\tif str[i] != '.'\n\t\t\t\tif i > 0 and i < n-1\n\n\t\t\t\ttotal+=1 if str[i-1] == '.'\n\t\t\t\ttotal+=1 if str[i+1] == '.'\t\t\n\t\t\t\telsif i==0 and i+1=0 and str[i-1] == '.'\n\t\t\t\ttotal+=1\n\t\t\t\tend\n\t\tend\t\t\n\telse\n\t\tif str[i] == '.' \n\t\t\tif i > 0 and i < n-1\n\t\t\t\ttotal-=1 if str[i-1] == '.'\n\t\t\t\ttotal-=1 if str[i+1] == '.'\t\t\n\t\t\telsif i==0 and i+1=0 and str[i-1] == '.'\n\t\t\t\ttotal-=1\n\t\t\tend\n\t\tend\n\t\t\n\tend\n\tstr[i] = c\n\tputs total\nend\n"}, {"source_code": "#!/usr/bin/ruby\n\ndef solution(n, m, s, narr, carr)\n count = 0\n prev_char = nil\n s.each_char do |char|\n count+=1 if char == '.' && prev_char == '.'\n prev_char = char\n end\n m.times do |t|\n i = narr[t]\n char = carr[t]\n if (char == '.') ^ (s[i] == '.')\n if char == '.'\n count+=1 if i > 0 && s[i-1] == '.'\n count+=1 if i+1 < n && s[i+1] == '.'\n else\n count-=1 if i > 0 && s[i-1] == '.'\n count-=1 if i+1 < n && s[i+1] == '.'\n end\n s[i] = char\n end\n puts count\n end\nend\n\nn, m = gets.split(' ').map{|s|s.to_i}\ns = gets\nnarr = []\ncarr = []\nm.times{\n i, c = gets.split(' ')\n narr.push i.to_i - 1\n carr.push c\n}\nsolution n, m, s, narr, carr\n\n# solution 10, 3, '.b..bz....', [0, 2, 8], %w(h c f)\n# solution 4, 4, '.cc.', [1, 2, 1, 0], %w(. . a a)\n# solution 4,3\n# solution 3,2\n# solution 3,1\n# solution 3,3\n# solution 2,1\n# solution 2,2\n#\n# #\n# # solution 4, [75, 150, 75, 50]\n# # solution 3, [100, 150, 250]"}, {"source_code": "n,m=gets.split.map &:to_i\n\ns=?a+gets.chomp+?a\ngroups=s.scan(/\\.+/).size\ndots=s.count ?.\nb=s.bytes.map{|c|c<90}\n\nres=[]\nqueries=$<.read.split\nm.times{|i|\n x=queries[2*i].to_i\n c=queries[2*i+1]\n if b[x] and c!=?.\n dots-=1\n if !b[x-1] and !b[x+1]\n groups-=1\n elsif b[x-1] and b[x+1]\n groups+=1\n end\n b[x]=false\n elsif !b[x] and c==?.\n dots+=1\n if !b[x-1] and !b[x+1]\n groups+=1\n elsif b[x-1] and b[x+1]\n groups-=1\n end\n b[x]=true\n end\n res< 0 and i < n-1\n\n\t\t\t\ttotal+=1 if str[i-1] == '.'\n\t\t\t\ttotal+=1 if str[i+1] == '.'\t\t\n\t\t\t\telsif i==0 and i+1 0 and i < n-1\n\t\t\t\ttotal-=1 if str[i-1] == '.'\n\t\t\t\ttotal-=1 if str[i+1] == '.'\t\t\n\t\t\telsif i==0 and i+11 and (x==0 and s[x+1]!=?.) or (x==n-1 and s[x-1]!=?.)\n groups-=1\n elsif n>2 and (01 and (x==0 and s[x+1]!=?.) or (x==n-1 and s[x-1]!=?.)\n groups+=1\n elsif n>2 and (01 and (x==0 and s[x+1]!=?.) or (x==n-1 and s[x-1]!=?.)\n groups-=1\n elsif n>2 and (01 and (x==0 and s[x+1]!=?.) or (x==n-1 and s[x-1]!=?.)\n groups+=1\n elsif n>2 and (0 v then\n n += 1\n ish << a.to_s + \" \" + v.to_s\n end\n v += 1\n end\n a += 1\nend\n\nputs n\nn.times do |i|\n puts ish[i]\nend"}, {"source_code": "x,y,a,b=gets.split.map(&:to_i)\n_=[]\n[a,~-b].max.upto(x){|i|b.upto([~-i,y].min){|j|_<<[i,j]*\" \"}}\nputs _.size,_\n"}, {"source_code": "ans=Array.new\nx,y,a,b=gets.split.map(&:to_i)\nfor i in a.to_i..x.to_i do\nk=[y.to_i,i].min\n\tfor j in b.to_i..k \n\t\tans<< [i,j] if i!=j\n\tend\nend\nans.size!=0?(puts ans.size;ans.each{|el|puts el.join(\" \")}):(puts 0)\n"}, {"source_code": "x, y, a, b = gets.split.map(&:to_i)\nres = Array.new\nfor i in [a, b+1].max..x\n for j in b..y\n break if j >= i\n res << [i, j]\n end\nend\nputs res.count\nfor i in res\n puts \"#{i[0]} #{i[1]}\"\nend"}, {"source_code": "x, y, a, b = gets.split(' ').collect!{ |i| i.to_i}\nn = 0\na.upto(x) do |i|\n\tb.upto(y) do |j|\n\t\tif i>j\n\t\t\tn += 1\n\t\tend\n\tend\t\t\nend\n\nputs n\n\na.upto(x) do |i|\n\tb.upto(y) do |j|\n\t\tif i>j\n puts \"#{i} #{j}\"\n\t\tend\n\tend\t\t\nend"}, {"source_code": "x,y,a,b=gets.split.map(&:to_i)\n_=[]\n[a,~-b].max.upto(x){|i|b.upto([~-i,y].min){|j|_<<[i,j]*\" \"}}\nputs _.size,_\n"}, {"source_code": "x,y,a,b=gets.split.map(&:to_i)\n_=[]\n[a,~-b].max.upto(x){|i|b.upto([~-i,y].min){|j|_<<[i,j]*\" \"}}\nputs _.size,_"}, {"source_code": "x,y,a,b=gets.split.map(&:to_i)\n_=[]\n[a,~-b].max.upto(x){|i|b.upto([~-i,y].min){|j|_<<[i,j]*\" \"}}\nputs _.size,_"}, {"source_code": "x,y,a,b=gets.chomp.split().map(&:to_i)\nif a<=b\n\ta=b+1\nend\ncount=0\ni=a\nwhile i<=x\n\tif i<=y\n\t\tmin=i-1\n\telse\n\t\tmin=y\n\tend\n\tcount+=min-b+1\n\ti+=1\nend\nputs count\nwhile a<=x\n\tif a<=y\n\t\tmin=a-1\n\telse\n\t\tmin=y\n\tend\n\ti=b\n\twhile i<=min\n\t\tputs a.to_s+' '+i.to_s\n\t\ti+=1\n\tend\n\ta+=1\n\nend"}, {"source_code": "x,y,a,b = gets.split.map(&:to_i)\ns = %w()\n(a..x).each { |i| (b..y).each { |j| s.push [i, j] if i > j } }\nputs s.size\ns.each { |f| puts f*' ' }"}, {"source_code": "x,y,a,b=gets.split.map(&:to_i)\n_=[]\n[a,~-b].max.upto(x){|i|b.upto([~-i,y].min){|j|_<<[i,j]*\" \"}}\nputs _.size,_\n"}, {"source_code": "x,y,a,b=gets.split.map(&:to_i)\n_=[]\n[a,~-b].max.upto(x){|i|b.upto([~-i,y].min){|j|_<<[i,j]*\" \"}}\nputs _.size,_\n"}, {"source_code": "#CF Round 149. Div II Prob. A - Heads or Tails\narr = gets.chomp.split(/ /)\na, res = [], []\narr.each {|i| a << i.to_i}\n(a[2]..a[0]).each { |i|\n (a[3]..[a[1], i - 1].min).each { |j|\n res << (i.to_s + \" \" + j.to_s)\n }\n}\nputs res.size\nres.each {|i| puts i}"}, {"source_code": "#CF Round 149. Div II Prob. A - Heads or Tails\na = gets.chomp.split(/ /).map(&:to_i)\nres = []\n(a[2]..a[0]).each { |i|\n (a[3]..[a[1], i - 1].min).each { |j|\n res << (i.to_s + \" \" + j.to_s)\n }\n}\nputs res.size\nres.each {|i| puts i}"}, {"source_code": "x,y,a,b = gets.split.map(&:to_i)\nans = []\nif x <= b\n puts 0\n exit\nelse\n a = [a,b].max\n (a).upto(x) do |i| \n (b).upto([(i-1),y].min) do |j|\n ans << \"#{i} #{j}\"\n end\n end\nend\nputs ans.length\nans.each { |i| puts i }"}, {"source_code": "x,y,a,b=gets.split.map(&:to_i)\n_=[]\n[a,~-b].max.upto(x){|i|b.upto([~-i,y].min){|j|_<<[i,j]*\" \"}}\nputs _.size,_\n"}, {"source_code": "x, y, a, b = gets.split.collect{|i| i.to_i}\ncnt = 0\nfor i in a..x\n for j in b...(i b ? a : b + 1\nt = y + 1\nps = []\nfor i in s..x\n for j in b...[i, t].min\n ps << [i, j]\n end\nend\nputs ps.size\nps.each { |p| puts p * ' ' }"}, {"source_code": "x, y, a, b = gets.split.map(&:to_i)\noutcomes = []\na.upto(x) do |i|\n b.upto(y) do |j|\n outcomes << [i, j] if i > j\n end\nend\nputs outcomes.size\noutcomes.sort.each do |outcome|\n puts outcome.join(\" \")\nend\n"}, {"source_code": "x,y,a,b=gets.split.map(&:to_i)\n_=[]\n[a,~-b].max.upto(x){|i|b.upto([~-i,y].min){|j|_<<[i,j]*\" \"}}\nputs _.size,_\n"}, {"source_code": "x,y,a,b=gets.split.map(&:to_i)\n_=[]\n[a,~-b].max.upto(x){|i|b.upto([~-i,y].min){|j|_<<[i,j]*\" \"}}\nputs _.size,_"}, {"source_code": "x,y,a,b=gets.split.map(&:to_i)\n_=[]\n[a,~-b].max.upto(x){|i|b.upto([~-i,y].min){|j|_<<[i,j]*\" \"}}\nputs _.size,_\n"}], "negative_code": [{"source_code": "ans=Array.new\nx,y,a,b=gets.split.map(&:to_i)\nfor i in a.to_i..x.to_i do\n\tfor j in [b.to_i,a.to_i].max..[y.to_i,i].min \n\t\tans<< [i,j] if i!=j\n\tend\nend\nans.size!=0?(ans.each{|el|puts el.join(\" \")}):(puts 0)\n"}, {"source_code": "ans=Array.new\nx,y,a,b=gets.split.map(&:to_i)\nfor i in a.to_i..x.to_i do\n\tfor j in [b.to_i,a.to_i].max..[y.to_i,i].min \n\t\tans<< [i,j] if i!=j\n\tend\nend\nans.size!=0?(puts ans.size;ans.each{|el|puts el.join(\" \")}):(puts 0)\n"}, {"source_code": "x, y, a, b = gets.split(' ').collect!{ |i| i.to_i}\nn = 0\nx.downto(a) do |i|\n\ty.downto(b) do |j|\n\t\tif i>j\n\t\t\tn += 1\n\t\tend\n\tend\t\t\nend\n\nputs n\n\nx.downto(a) do |i|\n\ty.downto(b) do |j|\n\t\tif i>j\n puts \"#{i} #{j}\"\n\t\tend\n\tend\t\t\nend"}, {"source_code": "x,y,a,b = gets.split.map(&:to_i)\n\nif x <= b\n puts 0\n exit\nelse\n a = [a,b].max\n (a+1).upto(x) do |i| \n (b).upto(i-1) do |j|\n print i,' ',j; puts\n end\n end\nend"}, {"source_code": "x,y,a,b = gets.split.map(&:to_i)\nans = []\nif x <= b\n puts 0\n exit\nelse\n a = [a,b].max\n (a+1).upto(x) do |i| \n (b).upto([(i-1),b].min) do |j|\n ans << \"#{i} #{j}\"\n end\n end\nend\nputs ans.length\nans.each { |i| puts i }"}, {"source_code": "x,y,a,b = gets.split.map(&:to_i)\nans = []\nif x <= b\n puts 0\n exit\nelse\n a = [a,b].max\n (a+1).upto(x) do |i| \n (b).upto(i-1) do |j|\n ans << \"#{i} #{j}\"\n end\n end\nend\nputs ans.length\nans.each { |i| puts i }"}, {"source_code": "x,y,a,b = gets.split.map(&:to_i)\nans = []\nif x <= b\n puts 0\n exit\nelse\n a = [a,b].max\n (a+1).upto(x) do |i| \n (b).upto(i-1) do |j|\n ans << \"#{i} #{j}\"\n end\n end\nend\n\nans.each { |i| puts i }"}, {"source_code": "x,y,a,b = gets.split.map(&:to_i)\nans = []\nif x <= b\n puts 0\n exit\nelse\n a = [a,b].max\n (a+1).upto(x) do |i| \n (b).upto([(i-1),y].min) do |j|\n ans << \"#{i} #{j}\"\n end\n end\nend\nputs ans.length\nans.each { |i| puts i }"}, {"source_code": "x, y, a, b = gets.split.collect{|i| i.to_i}\ncnt = 0\nfor i in b..y\n for j in i+1..x\n cnt += 1\n end\nend\nputs cnt\nfor i in b..y\n for j in i+1..x\n print j, \" \", i, \"\\n\"\n end\nend\n"}], "src_uid": "bb3e3b51a4eda8fef503952a00777910"} {"nl": {"description": "An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train?", "input_spec": "The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cars in the train. The second line contains n integers pi (1 ≤ pi ≤ n, pi ≠ pj if i ≠ j) — the sequence of the numbers of the cars in the train.", "output_spec": "Print a single integer — the minimum number of actions needed to sort the railway cars.", "sample_inputs": ["5\n4 1 2 5 3", "4\n4 1 3 2"], "sample_outputs": ["2", "2"], "notes": "NoteIn the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train."}, "positive_code": [{"source_code": "#Good luk for the Contest 606, Problem: c\nn = gets.strip.to_i\nar = gets.strip.split.map(&:to_i)\n\npos = Array.new(n, 0)\n0.upto(n-1).each do |i|\n\tpos[ar[i]-1] = i\nend\n\nres = 1\ntmp = 1\n1.upto(n-1).each do |i|\n\tif pos[i] > pos[i-1]\n\t\ttmp += 1\n\telse\n\t\tres = tmp if tmp >res\n\t\ttmp = 1\n\tend\nend\nres = tmp if tmp > res\nputs n-res"}, {"source_code": "n = gets.chomp.to_i\nps = gets.chomp.split.map(&:to_i)\n\nhs = Hash.new()\n(0...n).each do |i|\n hs[ps[i]] = i\nend\n\nans = n - 1\ncnt = 1\nfor i in 2..n\n if hs[i - 1] < hs[i]\n cnt += 1\n else\n cnt = 1\n end\n ans = [ans, n - cnt].min\nend\n\nputs ans\n"}, {"source_code": "n = gets.to_i\nps = gets.chomp.split.map(&:to_i)\nd = Array.new(n, 0)\nps.each {|p| d[p - 1] = p == 1 ? 1 : d[p - 2] + 1 }\nputs (n - d.max)\n"}], "negative_code": [{"source_code": "#Good luk for the Contest 606, Problem: c\nn = gets.strip.to_i\nar = gets.strip.split.map(&:to_i)\n\npos = Array.new(n, 0)\n0.upto(n-1).each do |i|\n\tpos[ar[i]-1] = i\nend\n\nres = 1\ntmp = 1\n1.upto(n-1).each do |i|\n\tif pos[i] > pos[i-1]\n\t\ttmp += 1\n\telse\n\t\tres = tmp if tmp >res\n\t\ttmp = 1\n\tend\nend\nputs n-res"}, {"source_code": "#Good luk for the Contest 606, Problem: c\nn = gets.strip.to_i\nar = gets.strip.split.map(&:to_i)\n\npos = Array.new(n, 0)\n0.upto(n-1).each do |i|\n\tpos[ar[i]-1] = i\nend\n\nres = 0\ntmp = 1\n1.upto(n-1).each do |i|\n\tif pos[i] > pos[i-1]\n\t\ttmp += 1\n\telse\n\t\tres = tmp if tmp >res\n\t\ttmp = 1\n\tend\nend\nputs n-res"}], "src_uid": "277948a70c75840445e1826f2b23a897"} {"nl": {"description": "Polycarp has found a table having an infinite number of rows and columns. The rows are numbered from $$$1$$$, starting from the topmost one. The columns are numbered from $$$1$$$, starting from the leftmost one.Initially, the table hasn't been filled and Polycarp wants to fix it. He writes integers from $$$1$$$ and so on to the table as follows. The figure shows the placement of the numbers from $$$1$$$ to $$$10$$$. The following actions are denoted by the arrows. The leftmost topmost cell of the table is filled with the number $$$1$$$. Then he writes in the table all positive integers beginning from $$$2$$$ sequentially using the following algorithm.First, Polycarp selects the leftmost non-filled cell in the first row and fills it. Then, while the left neighbor of the last filled cell is filled, he goes down and fills the next cell. So he goes down until the last filled cell has a non-filled neighbor to the left (look at the vertical arrow going down in the figure above).After that, he fills the cells from the right to the left until he stops at the first column (look at the horizontal row in the figure above). Then Polycarp selects the leftmost non-filled cell in the first row, goes down, and so on.A friend of Polycarp has a favorite number $$$k$$$. He wants to know which cell will contain the number. Help him to find the indices of the row and the column, such that the intersection of the row and the column is the cell containing the number $$$k$$$.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of one line containing one integer $$$k$$$ ($$$1 \\le k \\le 10^9$$$) which location must be found.", "output_spec": "For each test case, output in a separate line two integers $$$r$$$ and $$$c$$$ ($$$r, c \\ge 1$$$) separated by spaces — the indices of the row and the column containing the cell filled by the number $$$k$$$, respectively.", "sample_inputs": ["7\n11\n14\n5\n4\n1\n2\n1000000000"], "sample_outputs": ["2 4\n4 3\n1 3\n2 1\n1 1\n1 2\n31623 14130"], "notes": null}, "positive_code": [{"source_code": "MOD = 10 ** 9 + 7\r\ndef gs; gets.chomp; end\r\ndef gi; gets.to_i; end\r\ndef gli; gets.split.map(&:to_i); end\r\ndef cyesno(x); puts x ? :Yes : :No; end\r\ndef uyesno(x); puts x ? :YES : :NO; end\r\ndef array_to_hash_by_count(c); Hash.new(0).tap { |x| c.each { |z| x[z] += 1 } }; end\r\n\r\ngets.to_i.times do \r\n x = gi\r\n n = (x ** 0.5).to_i\r\n if x == n * n\r\n puts \"#{n} 1\"\r\n else\r\n n += 1\r\n diff = n * n - x\r\n if diff == n - 1\r\n puts \"#{n} #{n}\"\r\n elsif diff > n - 1\r\n puts \"#{n - (diff - n + 1)} #{n}\"\r\n else\r\n puts \"#{n} #{n - (n - diff) + 1}\"\r\n end\r\n end\r\nend\r\n"}], "negative_code": [], "src_uid": "f8335c59cd05988c8053f138c4df06aa"} {"nl": {"description": "AquaMoon has two binary sequences $$$a$$$ and $$$b$$$, which contain only $$$0$$$ and $$$1$$$. AquaMoon can perform the following two operations any number of times ($$$a_1$$$ is the first element of $$$a$$$, $$$a_2$$$ is the second element of $$$a$$$, and so on): Operation 1: if $$$a$$$ contains at least two elements, change $$$a_2$$$ to $$$\\operatorname{min}(a_1,a_2)$$$, and remove the first element of $$$a$$$. Operation 2: if $$$a$$$ contains at least two elements, change $$$a_2$$$ to $$$\\operatorname{max}(a_1,a_2)$$$, and remove the first element of $$$a$$$.Note that after a removal of the first element of $$$a$$$, the former $$$a_2$$$ becomes the first element of $$$a$$$, the former $$$a_3$$$ becomes the second element of $$$a$$$ and so on, and the length of $$$a$$$ reduces by one.Determine if AquaMoon can make $$$a$$$ equal to $$$b$$$ by using these operations.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 2\\,000$$$) — the number of test cases. Description of test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 \\leq n,m \\leq 50$$$, $$$m \\leq n$$$) — the lengths of $$$a$$$ and $$$b$$$ respectively. The second line of each test case contains a string $$$a$$$ of length $$$n$$$, consisting only $$$0$$$ and $$$1$$$. The third line of each test case contains a string $$$b$$$ of length $$$m$$$, consisting only $$$0$$$ and $$$1$$$.", "output_spec": "For each test case, output \"YES\" if AquaMoon can change $$$a$$$ to $$$b$$$ by using these options; otherwise, output \"NO\". You may print each letter in any case (for example, \"YES\", \"Yes\", \"yes\", \"yEs\" will all be recognized as a positive answer).", "sample_inputs": ["10\n\n6 2\n\n001001\n\n11\n\n6 2\n\n110111\n\n01\n\n6 2\n\n000001\n\n11\n\n6 2\n\n111111\n\n01\n\n8 5\n\n10000101\n\n11010\n\n7 4\n\n1010001\n\n1001\n\n8 6\n\n01010010\n\n010010\n\n8 4\n\n01010101\n\n1001\n\n8 4\n\n10101010\n\n0110\n\n7 5\n\n1011100\n\n11100"], "sample_outputs": ["YES\nYES\nNO\nNO\nNO\nYES\nYES\nNO\nNO\nYES"], "notes": "NoteIn the first test case, you can use Operation 2 four times to make $$$a$$$ equals to $$$b$$$.In the second test case, you can use Operation 1 four times to make $$$a$$$ equals to $$$b$$$.In the third test case, it can be proved that no matter how we use the operations, it is impossible to make $$$a$$$ equal to $$$b$$$.In the fourth test case, it can be proved that no matter how we use the operations, it is impossible to make $$$a$$$ equal to $$$b$$$.In the fifth test case, you can use Operation 2 three times to make $$$a$$$ become $$$10101$$$, so the first element of $$$a$$$ equals to the first element of $$$b$$$, but it can be proved that no matter how to operate, the second to the fifth elements of $$$a$$$ can't be the same as $$$b$$$."}, "positive_code": [{"source_code": "t = gets.to_i\nt.times do\n gets\n a = gets.chomp!\n b = gets.chomp!\n if b.size == 1 && a.include?(b[0]) || a[(-b.size + 1)..-1] == b[1..-1] && a[0..(-b.size)].include?(b[0])\n puts 'YES'\n else\n puts 'NO'\n end\nend\n"}], "negative_code": [{"source_code": "t = gets.to_i\nt.times do\n gets\n a = gets.chomp!\n b = gets.chomp!\n if a[(-b.size + 1)..-1] == b[1..-1] && a[0..(-b.size)].include?(b[0])\n puts 'YES'\n else\n puts 'NO'\n end\nend\n"}], "src_uid": "83050a8a4c7b64004681bdadb630292e"} {"nl": {"description": "Students love to celebrate their holidays. Especially if the holiday is the day of the end of exams!Despite the fact that Igor K., unlike his groupmates, failed to pass a programming test, he decided to invite them to go to a cafe so that each of them could drink a bottle of... fresh cow milk. Having entered the cafe, the m friends found n different kinds of milk on the menu, that's why they ordered n bottles — one bottle of each kind. We know that the volume of milk in each bottle equals w.When the bottles were brought in, they decided to pour all the milk evenly among the m cups, so that each got a cup. As a punishment for not passing the test Igor was appointed the person to pour the milk. He protested that he was afraid to mix something up and suggested to distribute the drink so that the milk from each bottle was in no more than two different cups. His friends agreed but they suddenly faced the following problem — and what is actually the way to do it?Help them and write the program that will help to distribute the milk among the cups and drink it as quickly as possible!Note that due to Igor K.'s perfectly accurate eye and unswerving hands, he can pour any fractional amount of milk from any bottle to any cup.", "input_spec": "The only input data file contains three integers n, w and m (1 ≤ n ≤ 50, 100 ≤ w ≤ 1000, 2 ≤ m ≤ 50), where n stands for the number of ordered bottles, w stands for the volume of each of them and m stands for the number of friends in the company.", "output_spec": "Print on the first line \"YES\" if it is possible to pour the milk so that the milk from each bottle was in no more than two different cups. If there's no solution, print \"NO\". If there is a solution, then print m more lines, where the i-th of them describes the content of the i-th student's cup. The line should consist of one or more pairs that would look like \"b v\". Each such pair means that v (v > 0) units of milk were poured into the i-th cup from bottle b (1 ≤ b ≤ n). All numbers b on each line should be different. If there are several variants to solve the problem, print any of them. Print the real numbers with no less than 6 digits after the decimal point.", "sample_inputs": ["2 500 3", "4 100 5", "4 100 7", "5 500 2"], "sample_outputs": ["YES\n1 333.333333\n2 333.333333\n2 166.666667 1 166.666667", "YES\n3 20.000000 4 60.000000\n1 80.000000\n4 40.000000 2 40.000000\n3 80.000000\n2 60.000000 1 20.000000", "NO", "YES\n4 250.000000 5 500.000000 2 500.000000\n3 500.000000 1 500.000000 4 250.000000"], "notes": null}, "positive_code": [{"source_code": "n, w, m = gets.split.map(&:to_i)\ncurrent = 0\npos = 0\nret = Array.new(m){Array.new}\n\nn.times do |i|\n if current + m > n\n ret[pos] << [i, n - current]\n pos += 1\n current = m - (n - current)\n if current > n\n puts 'NO'\n exit\n else\n ret[pos] << [i, current]\n end\n else\n ret[pos] << [i, m]\n current += m\n end\n if current == n\n pos += 1\n current = 0\n end\nend\n\nputs 'YES'\nret.each do |array|\n s = array.map do |e|\n e[0] += 1\n e[1] = e[1].to_f * w / m\n \"%d %.6f\" % e\n end.join(\" \")\n puts s\nend\n\n# 4 100 8\n"}], "negative_code": [{"source_code": "n, w, m = gets.split.map(&:to_i)\ncurrent = 0\npos = 0\nret = Array.new(m){Array.new}\n\nn.times do |i|\n if current + m > n\n ret[pos] << [i, n - current]\n pos += 1\n current = m - (n - current)\n if current > n\n puts 'NO'\n exit\n else\n ret[pos] << [i, current]\n if current == n\n pos += 1\n current = 0\n end\n end\n else\n ret[pos] << [i, m]\n current += m\n end\nend\n\nputs 'YES'\nret.each do |array|\n s = array.map do |e|\n e[0] += 1\n e[1] = e[1].to_f * w / m\n \"%d %.6f\" % e\n end.join(\" \")\n puts s\nend\n\n# 4 100 8\n"}, {"source_code": "n, w, m = gets.split.map(&:to_i)\nmin = 10e-6\na = (n * w).to_f / m\nr = a\nret = Array.new(m){Array.new}\ncur = 0\nn.times do |i|\n if r - w > min\n ret[cur] << [i, w]\n r = r - w\n else\n ret[cur] << [i, r]\n cur += 1\n if w - r > a + min\n puts 'NO'\n exit\n elsif w - r > min\n ret[cur] << [i, w - r]\n r = a - (w - r)\n end\n end\nend\n\nputs 'YES'\nret.each do |array|\n s = array.map do |e|\n e[0] += 1\n \"%d %.6f\" % e\n end.join(\" \")\n puts s\nend\n\n"}], "src_uid": "ae96b178fee7c189a377835a53eb3dc4"} {"nl": {"description": "Little Vova studies programming in an elite school. Vova and his classmates are supposed to write n progress tests, for each test they will get a mark from 1 to p. Vova is very smart and he can write every test for any mark, but he doesn't want to stand out from the crowd too much. If the sum of his marks for all tests exceeds value x, then his classmates notice how smart he is and start distracting him asking to let them copy his homework. And if the median of his marks will be lower than y points (the definition of a median is given in the notes), then his mom will decide that he gets too many bad marks and forbid him to play computer games.Vova has already wrote k tests and got marks a1, ..., ak. He doesn't want to get into the first or the second situation described above and now he needs to determine which marks he needs to get for the remaining tests. Help him do that.", "input_spec": "The first line contains 5 space-separated integers: n, k, p, x and y (1 ≤ n ≤ 999, n is odd, 0 ≤ k < n, 1 ≤ p ≤ 1000, n ≤ x ≤ n·p, 1 ≤ y ≤ p). Here n is the number of tests that Vova is planned to write, k is the number of tests he has already written, p is the maximum possible mark for a test, x is the maximum total number of points so that the classmates don't yet disturb Vova, y is the minimum median point so that mom still lets him play computer games. The second line contains k space-separated integers: a1, ..., ak (1 ≤ ai ≤ p) — the marks that Vova got for the tests he has already written.", "output_spec": "If Vova cannot achieve the desired result, print \"-1\". Otherwise, print n - k space-separated integers — the marks that Vova should get for the remaining tests. If there are multiple possible solutions, print any of them.", "sample_inputs": ["5 3 5 18 4\n3 5 4", "5 3 5 16 4\n5 5 5"], "sample_outputs": ["4 1", "-1"], "notes": "NoteThe median of sequence a1, ..., an where n is odd (in this problem n is always odd) is the element staying on (n + 1) / 2 position in the sorted list of ai.In the first sample the sum of marks equals 3 + 5 + 4 + 4 + 1 = 17, what doesn't exceed 18, that means that Vova won't be disturbed by his classmates. And the median point of the sequence {1, 3, 4, 4, 5} equals to 4, that isn't less than 4, so his mom lets him play computer games.Please note that you do not have to maximize the sum of marks or the median mark. Any of the answers: \"4 2\", \"2 4\", \"5 1\", \"1 5\", \"4 1\", \"1 4\" for the first test is correct.In the second sample Vova got three '5' marks, so even if he gets two '1' marks, the sum of marks will be 17, that is more than the required value of 16. So, the answer to this test is \"-1\"."}, "positive_code": [{"source_code": "n,k,p,x,y=gets.chomp.split(' ').map(&:to_i)\nak=gets.chomp.split(' ').map(&:to_i)\none=0\ntwo=0\nsum=0\nfor i in ak do\n\tsum+=i\n\tif i(n-k)\n\twon=n-k\nend\nm=n-k-won\nsum=x-sum\nif won+y*m>sum ||won<0\n\tputs \"-1\"\nelse\n\twon.times do\n\t\tprint \"1 \"\n\tend\n\tm.times do\n\t\tprint y.to_s+' '\n\tend\nend\nputs"}, {"source_code": "n, k, p, x, y = gets.split.map(&:to_i)\nkk = gets.split.map(&:to_i).sort\nl = kk.count{|i| i < y}\ng = kk.count{|i| i >= y}\nadd_g = [0, (n + 1) / 2 - g].max\nif l > n / 2 || kk.inject(:+).to_i + add_g * y + (n - k - add_g) > x\n puts -1\nelse\n ans = []\n add_g.times{ans << y}\n (n - k - add_g).times{ans << 1}\n puts ans.join(\" \")\nend\n"}], "negative_code": [{"source_code": "n,k,p,x,y=gets.chomp.split(' ').map(&:to_i)\nak=gets.chomp.split(' ').map(&:to_i).sort\nch=0\nsum=0\nfor i in ak do\n\tsum+=i\nend\nak.each_with_index { |i,j|\n\tif i>=y\n\t\tch=j\n\t\tbreak\n\tend\n}\nleft=n/2-ch\nright=n-k-left\n\n#puts \"left = \"+left.to_s\n#puts \"right = \"+right.to_s\n#puts \"p = \"+p.to_s\n#puts \"y = \"+y.to_s\n#puts \"x = \"+x.to_s\n#puts \"sum = \"+sum.to_s\n#puts \"sum+left+right*y = \"+ (sum+left+right*y).to_s\n\nif(left<0)\n\tputs \"-1\"\nelsif(p0)\n\tputs \"-1\"\nelsif(right<0)\n\tright=0;\n\tleft=n-k-right;\nelsif sum+left+right*y<=x\n\tleft.times do\n\t\tprint \"1 \"\n\tend\n\tright.times do\n\t\tprint y.to_s+' '\n\tend\n\tputs\nelse\n\tputs \"-1\"\nend"}, {"source_code": "n, k, p, x, y = gets.split.map(&:to_i)\nkk = gets.split.map(&:to_i).sort\nl = kk.count{|x| x < y}\ng = k - l\n\nif l > n / 2 || (n / 2 - l) * 1 + [(n - n / 2 - g), 0].max * y + kk.inject(:+).to_i > x\n puts -1\nelse\n ans = []\n (n - k).times do\n if kk[(kk.size - 1) / 2].to_i >= y\n kk << 1\n ans << 1\n else\n kk << y\n ans << y\n end\n kk.sort!\n end\n puts ans.join(\" \")\nend\n"}, {"source_code": "n, k, p, x, y = gets.split.map(&:to_i)\nkk = gets.split.map(&:to_i).sort\nl = kk.count{|x| x < y}\ng = k - l\nif l > n / 2 || (n / 2 - l) * 1 + (n - n / 2 - g) * y + kk.inject(:+).to_i > x\n puts -1\nelse\n ans = []\n (n - k).times do\n if kk[(kk.size - 1) / 2].to_i >= y\n kk << 1\n ans << 1\n else\n kk << y\n ans << y\n end\n kk.sort!\n end\n puts ans.join(\" \")\nend\n"}, {"source_code": "n, k, p, x, y = gets.split.map(&:to_i)\nkk = gets.split.map(&:to_i).sort\nl = kk.count{|x| x < y}\ng = k - l\nif l > n / 2 || g > n / 2 || (n / 2 - l) * 1 + (n / 2 - g) * y + kk.inject(:+).to_i > x\n puts -1\nelse\n ans = []\n (n - k).times do\n if kk[(kk.size - 1) / 2].to_i >= y\n kk << 1\n ans << 1\n else\n kk << 4\n ans << 4\n end\n kk.sort!\n end\n puts ans.join(\" \")\nend\n"}, {"source_code": "n, k, p, x, y = gets.split.map(&:to_i)\nkk = gets.split.map(&:to_i).sort\nl = kk.count{|i| i < y}\ng = kk.count{|i| i >= y}\nadd_g = [0, (n + 1) / 2 - g].max\nif l > n / 2 || kk.inject(:+).to_i + add_g * y + (n - k - add_g) > x\n puts -1\nelse\n ans = []\n (n - k).times do\n if kk[kk.size / 2] < y\n kk << y\n ans << y\n elsif kk.size < n\n kk << 1\n ans << 1\n end\n end\n puts ans.join(\" \")\nend\n"}, {"source_code": "n, k, p, x, y = gets.split.map(&:to_i)\nkk = gets.split.map(&:to_i).sort\nl = kk.count{|x| x < y}\ng = k - l\nif l > n / 2 || g > n / 2 || (n / 2 - l) * 1 + (n - n / 2 - g) * y + kk.inject(:+).to_i > x\n puts -1\nelse\n ans = []\n (n - k).times do\n if kk[(kk.size - 1) / 2].to_i >= y\n kk << 1\n ans << 1\n else\n kk << y\n ans << y\n end\n kk.sort!\n end\n puts ans.join(\" \")\nend\n"}, {"source_code": "n, k, p, x, y = gets.split.map(&:to_i)\nans = []\nif k > 0\n kk = gets.split.map(&:to_i)\nelse\n kk = [y]\n ans << y\nend\nl = kk.count{|x| x < y}\ng = kk.count{|x| x > y}\np_n = x - kk.inject(:+)\nif l > n / 2 || g > n / 2 || (n / 2 - l) * 1 + (n / 2 - g) * y > p_n\n puts -1\nelse\n (n / 2 - l).times{ans << 1}\n (n / 2 - g).times{ans << y}\n puts ans.join(\" \")\nend\n"}, {"source_code": "n, k, p, x, y = gets.split.map(&:to_i)\nkk = gets.split.map(&:to_i).sort\nl = kk.count{|x| x < y}\ng = kk.count{|x| x > y}\np_n = x - kk.inject(:+)\nif l > n / 2 || g > n / 2 || (n / 2 - l) * 1 + (n / 2 - g) * y > p_n\n puts -1\nelse\n ans = []\n (n / 2 - l).times{ans << 1}\n (n / 2 - g).times{ans << y}\n puts ans.join(\" \")\nend\n"}], "src_uid": "f01d11bd231a7b2e7ca56de1df0f1272"} {"nl": {"description": "A robber has attempted to rob a bank but failed to complete his task. However, he had managed to open all the safes.Oleg the bank client loves money (who doesn't), and decides to take advantage of this failed robbery and steal some money from the safes. There are many safes arranged in a line, where the i-th safe from the left is called safe i. There are n banknotes left in all the safes in total. The i-th banknote is in safe xi. Oleg is now at safe a. There are two security guards, one of which guards the safe b such that b < a, i.e. the first guard is to the left of Oleg. The other guard guards the safe c so that c > a, i.e. he is to the right of Oleg.The two guards are very lazy, so they do not move. In every second, Oleg can either take all the banknotes from the current safe or move to any of the neighboring safes. However, he cannot visit any safe that is guarded by security guards at any time, becaues he might be charged for stealing. Determine the maximum amount of banknotes Oleg can gather.", "input_spec": "The first line of input contains three space-separated integers, a, b and c (1 ≤ b < a < c ≤ 109), denoting the positions of Oleg, the first security guard and the second security guard, respectively. The next line of input contains a single integer n (1 ≤ n ≤ 105), denoting the number of banknotes. The next line of input contains n space-separated integers x1, x2, ..., xn (1 ≤ xi ≤ 109), denoting that the i-th banknote is located in the xi-th safe. Note that xi are not guaranteed to be distinct.", "output_spec": "Output a single integer: the maximum number of banknotes Oleg can take.", "sample_inputs": ["5 3 7\n8\n4 7 5 5 3 6 2 8", "6 5 7\n5\n1 5 7 92 3"], "sample_outputs": ["4", "0"], "notes": "NoteIn the first example Oleg can take the banknotes in positions 4, 5, 6 (note that there are 2 banknotes at position 5). Oleg can't take the banknotes in safes 7 and 8 because he can't run into the second security guard. Similarly, Oleg cannot take the banknotes at positions 3 and 2 because he can't run into the first security guard. Thus, he can take a maximum of 4 banknotes.For the second sample, Oleg can't take any banknotes without bumping into any of the security guards."}, "positive_code": [{"source_code": "a, b, c = gets.chomp.split(\" \").map(&:to_i)\nn = gets.chomp.to_i\nxx = gets.chomp.split(\" \").map(&:to_i)\n\nret = 0\n\n\nxx.each do |x|\n if x > b && x < c then\n ret += 1\n end\nend\nputs ret"}], "negative_code": [], "src_uid": "aceadc8eadf6d5efd7c5a9fbc0396423"} {"nl": {"description": "ВКонтакте открыла второй штаб в Санкт-Петербурге! Вы не преминули возможностью сменить обстановку и решили переехать из офиса в Доме Зингера в офис на Красном мосту.Для комфортной работы вам потребуются два монитора с одинаковой высотой, чтобы изображение на них выглядело единым целым. На складе офиса на Красном мосту есть $$$n$$$ мониторов, $$$i$$$-й из них имеет ширину $$$w_i$$$ и высоту $$$h_i$$$. Любой монитор можно повернуть на 90 градусов, и тогда он будет иметь ширину $$$h_i$$$ и высоту $$$w_i$$$.Назовём неупорядоченную пару из двух различных мониторов подходящей, если можно их повернуть так, чтобы они имели одинаковую высоту. Любой из мониторов в паре можно как повернуть относительно исходной ориентации, так и не поворачивать.Подсчитайте подходящие пары мониторов.", "input_spec": "В первой строке задано одно целое число $$$n$$$ — число мониторов на складе. В каждой из следующих $$$n$$$ строк заданы два целых числа $$$w_i$$$ и $$$h_i$$$ ($$$1 \\le w_i, h_i \\le 10^9$$$) — ширина и высота $$$i$$$-го монитора. Обратите внимание, что мониторы могут быть квадратными ($$$w_i = h_i$$$), а размеры разных мониторов могут совпадать. В этой версии задачи $$$2 \\le n \\le 10^3$$$.", "output_spec": "Выведите число подходящих пар мониторов.", "sample_inputs": ["5\n3 2\n2 2\n5 5\n3 5\n4 3", "7\n10 10\n10 20\n20 10\n10 20\n10 20\n10 10\n20 10"], "sample_outputs": ["5", "21"], "notes": "ПримечаниеВ первом примере подходящими являются пары мониторов с номерами $$$(1, 2)$$$, $$$(1, 4)$$$, $$$(1, 5)$$$, $$$(3, 4)$$$, $$$(4, 5)$$$.Во втором примере все пары мониторов — подходящие."}, "positive_code": [{"source_code": "count = gets.chomp.to_i\nmonitors = []\nresult = 0\n\ncount.times do\n monitors << gets.chomp.split(' ').map(&:to_i)\nend\n\nmonitors.each_with_index do |monitor1, index1|\n monitors.each_with_index do |monitor2, index2|\n next if index1 == index2\n\n result += 1 if monitor1.intersection(monitor2) != []\n end\nend\n\nputs result / 2"}], "negative_code": [], "src_uid": "32c99f64fdf69b9fd87b07dbd14ceffa"} {"nl": {"description": "The bear has a string s = s1s2... s|s| (record |s| is the string's length), consisting of lowercase English letters. The bear wants to count the number of such pairs of indices i, j (1 ≤ i ≤ j ≤ |s|), that string x(i, j) = sisi + 1... sj contains at least one string \"bear\" as a substring.String x(i, j) contains string \"bear\", if there is such index k (i ≤ k ≤ j - 3), that sk = b, sk + 1 = e, sk + 2 = a, sk + 3 = r.Help the bear cope with the given problem.", "input_spec": "The first line contains a non-empty string s (1 ≤ |s| ≤ 5000). It is guaranteed that the string only consists of lowercase English letters.", "output_spec": "Print a single number — the answer to the problem.", "sample_inputs": ["bearbtear", "bearaabearc"], "sample_outputs": ["6", "20"], "notes": "NoteIn the first sample, the following pairs (i, j) match: (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9).In the second sample, the following pairs (i, j) match: (1,  4), (1,  5), (1,  6), (1,  7), (1,  8), (1,  9), (1,  10), (1,  11), (2,  10), (2,  11), (3,  10), (3,  11), (4,  10), (4,  11), (5,  10), (5,  11), (6,  10), (6,  11), (7,  10), (7,  11)."}, "positive_code": [{"source_code": "s=gets.chomp\nl=s.size\np=0\nr=0\nwhile (q=s.index('bear',p))\n r+=(l-q-3)*(q-p+1)\n p=q+1\nend\np r \n"}, {"source_code": "s, r, x = gets.chomp, 0, -1\n\n(s.length - 3).times do |i|\n x = i if s[i..i+3] == 'bear'\n r += x + 1\nend\n\np r\n"}, {"source_code": "s=gets.chomp\nl=s.size\np=0\nr=0\nwhile (q=s.index('bear',p))\n r+=(l-q-3)*(q-p+1)\n p=q+1\nend\np r \n"}, {"source_code": "#f = File.open('test1.txt', 'r')\nf = STDIN\ns = f.gets.chomp\nres = 0\nlength = s.length\nlength.times do |i|\n j = s.index 'bear', i\n next unless j\n j += 3\n res += length - j\nend\nputs res"}, {"source_code": "str = gets.chomp\n\ndef eq_bear?(str)\n\tstr[(0..3)] == \"bear\"\nend\n\nsum = 0\nlast = -1\n(0...str.length).each do |i|\n\tif eq_bear? str[(i..(i + 3))]\n\t\tsum += (i - last) * (str.length - (i + 3))\n\t\tlast = i\n\tend\nend\n\nputs sum\n"}, {"source_code": "str = gets.chomp\nlast = -1\nans = 0\n(3...str.size).step(1) { |i|\n last = i-3 if str[i-3..i] == 'bear'\n ans += last + 1\n}\nputs ans\n"}], "negative_code": [{"source_code": "s, r = gets.chomp, 0\nn = s.length\nx = 1\n\ns.each_char.with_index do |c, i|\n if (i < n-3) && s[i] + s[i+1] + s[i+2] + s[i+3] == 'bear'\n \n r += (i+x) * (n - i - 3)\n x = 0 if x == 1\n end\nend\n\np r\n"}], "src_uid": "240a2b88ded6016d0fd7157d0ee2beea"} {"nl": {"description": "There is a pyramid which consists of $$$n$$$ floors. The floors are numbered from top to bottom in increasing order. In the pyramid, the $$$i$$$-th floor consists of $$$i$$$ rooms.Denote the $$$j$$$-th room on the $$$i$$$-th floor as $$$(i,j)$$$. For all positive integers $$$i$$$ and $$$j$$$ such that $$$1 \\le j \\le i < n$$$, there are $$$2$$$ one-way staircases which lead from $$$(i,j)$$$ to $$$(i+1,j)$$$ and from $$$(i,j)$$$ to $$$(i+1,j+1)$$$ respectively.In each room you can either put a torch or leave it empty. Define the brightness of a room $$$(i, j)$$$ to be the number of rooms with a torch from which you can reach the room $$$(i, j)$$$ through a non-negative number of staircases.For example, when $$$n=5$$$ and torches are placed in the rooms $$$(1,1)$$$, $$$(2,1)$$$, $$$(3,2)$$$, $$$(4,1)$$$, $$$(4,3)$$$, and $$$(5,3)$$$, the pyramid can be illustrated as follows: In the above picture, rooms with torches are colored in yellow, and empty rooms are white. The blue numbers in the bottom-right corner indicate the brightness of the rooms.The room $$$(4,2)$$$ (the room with a star) has brightness $$$3$$$. In the picture below, the rooms from where you can reach $$$(4,2)$$$ have red border. The brightness is $$$3$$$ since there are three torches among these rooms. The pyramid is called nice if and only if for all floors, all rooms in the floor have the same brightness.Define the brilliance of a nice pyramid to be the sum of brightness over the rooms $$$(1,1)$$$, $$$(2,1)$$$, $$$(3,1)$$$, ..., $$$(n,1)$$$.Find an arrangement of torches in the pyramid, such that the resulting pyramid is nice and its brilliance is maximized.We can show that an answer always exists. If there are multiple answers, output any one of them.", "input_spec": "The first line of the input contains a single integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. The description of the test cases follows. The only line of each test case contains a single positive integer $$$n$$$ ($$$1 \\le n \\le 500$$$) — the number of floors in the pyramid. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$500$$$.", "output_spec": "For each test case, output $$$n$$$ lines, the arrangement of torches in the pyramid. The $$$i$$$-th line should contain $$$i$$$ integers, each separated with a space. The $$$j$$$-th integer on the $$$i$$$-th line should be $$$1$$$ if room $$$(i,j)$$$ has a torch, and $$$0$$$ otherwise. We can show that an answer always exists. If there are multiple answers, output any one of them.", "sample_inputs": ["3\n\n1\n\n2\n\n3"], "sample_outputs": ["1 \n1 \n1 1 \n1 \n1 1 \n1 0 1"], "notes": "NoteIn the third test case, torches are placed in $$$(1,1)$$$, $$$(2,1)$$$, $$$(2,2)$$$, $$$(3,1)$$$, and $$$(3,3)$$$. The pyramid is nice as rooms on each floor have the same brightness. For example, all rooms on the third floor have brightness $$$3$$$.The brilliance of the pyramid is $$$1+2+3 = 6$$$. It can be shown that no arrangements with $$$n=3$$$ will have a greater brilliance."}, "positive_code": [{"source_code": "def main\n t = inp.to_i\n t.times do\n n = inp.to_i\n 1.upto(n) do |i|\n 1.upto(i) do |j|\n if j == 1 || j == i\n print 1\n else\n print 0\n end\n print \" \" if j != i\n end\n puts \"\"\n end\n end\n\n\nend\n\n\n\n# lib\ndef inp\n gets.chomp\nend\n\ndef inarr\n gets.chomp.split\nend\n\ndef mapint(lst)\n lst.map(&:to_i)\nend\n\nmain"}], "negative_code": [], "src_uid": "4a473e34f2be18a19c306d80d4693199"} {"nl": {"description": "Vasya has got three integers $$$n$$$, $$$m$$$ and $$$k$$$. He'd like to find three integer points $$$(x_1, y_1)$$$, $$$(x_2, y_2)$$$, $$$(x_3, y_3)$$$, such that $$$0 \\le x_1, x_2, x_3 \\le n$$$, $$$0 \\le y_1, y_2, y_3 \\le m$$$ and the area of the triangle formed by these points is equal to $$$\\frac{nm}{k}$$$.Help Vasya! Find such points (if it's possible). If there are multiple solutions, print any of them.", "input_spec": "The single line contains three integers $$$n$$$, $$$m$$$, $$$k$$$ ($$$1\\le n, m \\le 10^9$$$, $$$2 \\le k \\le 10^9$$$).", "output_spec": "If there are no such points, print \"NO\". Otherwise print \"YES\" in the first line. The next three lines should contain integers $$$x_i, y_i$$$ — coordinates of the points, one point per line. If there are multiple solutions, print any of them. You can print each letter in any case (upper or lower).", "sample_inputs": ["4 3 3", "4 4 7"], "sample_outputs": ["YES\n1 0\n2 3\n4 1", "NO"], "notes": "NoteIn the first example area of the triangle should be equal to $$$\\frac{nm}{k} = 4$$$. The triangle mentioned in the output is pictured below: In the second example there is no triangle with area $$$\\frac{nm}{k} = \\frac{16}{7}$$$."}, "positive_code": [{"source_code": "n,m,k = gets.split.map(&:to_i)\nif (n*m*2)%k == 0\n area = (n*m*2)/k\n puts \"YES\"\n x = n.gcd(area)\n y = area/x\n if y > m\n y /= 2\n x *= 2\n end\n puts \"0 0\"\n puts \"#{x} 0\"\n puts \"0 #{y}\"\nelse\n puts \"NO\"\nend\n"}], "negative_code": [{"source_code": "n,m,k = gets.split.map(&:to_i)\nif (n*m*2)%k == 0\n area = (n*m*2)/k\n puts \"YES\"\n g = n.gcd(area)\n puts \"0 0\"\n puts \"#{g} 0\"\n puts \"0 #{area/g}\"\nelse\n puts \"NO\"\nend\n"}], "src_uid": "5c026adda2ae3d7b707d5054bd84db3f"} {"nl": {"description": "You are given a string $$$a$$$, consisting of $$$n$$$ characters, $$$n$$$ is even. For each $$$i$$$ from $$$1$$$ to $$$n$$$ $$$a_i$$$ is one of 'A', 'B' or 'C'.A bracket sequence is a string containing only characters \"(\" and \")\". A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters \"1\" and \"+\" between the original characters of the sequence. For example, bracket sequences \"()()\" and \"(())\" are regular (the resulting expressions are: \"(1)+(1)\" and \"((1+1)+1)\"), and \")(\", \"(\" and \")\" are not.You want to find a string $$$b$$$ that consists of $$$n$$$ characters such that: $$$b$$$ is a regular bracket sequence; if for some $$$i$$$ and $$$j$$$ ($$$1 \\le i, j \\le n$$$) $$$a_i=a_j$$$, then $$$b_i=b_j$$$. In other words, you want to replace all occurrences of 'A' with the same type of bracket, then all occurrences of 'B' with the same type of bracket and all occurrences of 'C' with the same type of bracket.Your task is to determine if such a string $$$b$$$ exists.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of testcases. Then the descriptions of $$$t$$$ testcases follow. The only line of each testcase contains a string $$$a$$$. $$$a$$$ consists only of uppercase letters 'A', 'B' and 'C'. Let $$$n$$$ be the length of $$$a$$$. It is guaranteed that $$$n$$$ is even and $$$2 \\le n \\le 50$$$.", "output_spec": "For each testcase print \"YES\" if there exists such a string $$$b$$$ that: $$$b$$$ is a regular bracket sequence; if for some $$$i$$$ and $$$j$$$ ($$$1 \\le i, j \\le n$$$) $$$a_i=a_j$$$, then $$$b_i=b_j$$$. Otherwise, print \"NO\". You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).", "sample_inputs": ["4\nAABBAC\nCACA\nBBBBAC\nABCA"], "sample_outputs": ["YES\nYES\nNO\nNO"], "notes": "NoteIn the first testcase one of the possible strings $$$b$$$ is \"(())()\".In the second testcase one of the possible strings $$$b$$$ is \"()()\"."}, "positive_code": [{"source_code": "def good(s)\r\n cur = 0\r\n s.each_char do |c|\r\n cur += c == '(' ? 1 : -1\r\n return false if cur < 0\r\n end\r\n cur == 0\r\nend\r\ngets.to_i.times do\r\n s = gets.chomp\r\n ok = false\r\n (0...8).each do |msk|\r\n cur = s.dup\r\n (0...s.size).each do |i|\r\n if msk >> (s[i].ord - 'A'.ord) & 1 != 0\r\n cur[i] = '('\r\n else\r\n cur[i] = ')'\r\n end\r\n end\r\n ok |= good cur\r\n end\r\n puts ok ? \"YES\" : \"NO\"\r\nend"}, {"source_code": "class String\n def rbs?\n b = 0\n self.each_char { |c|\n b += 1 if c == '('\n b -= 1 if c == ')'\n return false if b < 0\n }\n b == 0\n end\nend\n\ngets.to_i.times do\n s = gets.strip\n\n solution_found = false\n 8.times do |m|\n a = '()'[(m >> 0) & 1]\n b = '()'[(m >> 1) & 1]\n c = '()'[(m >> 2) & 1]\n t = s.gsub('A', a).gsub('B', b).gsub('C', c)\n solution_found = true if t.rbs?\n end\n\n puts(solution_found ? \"YES\" : \"NO\")\nend\n"}, {"source_code": "def solve\n a = string\n n = a.size\n (1 << 3).times do |bits|\n paren = 0\n xa = 1 - bits[0] * 2\n xb = 1 - bits[1] * 2\n xc = 1 - bits[2] * 2\n valid = true\n a.each_char do |c|\n case c\n when ?A\n paren += xa\n when ?B\n paren += xb\n when ?C\n paren += xc\n end\n if paren < 0\n valid = false\n break\n end\n end\n valid &&= (paren == 0)\n if valid\n puts \"YES\"\n return\n end\n end\n puts \"NO\"\nend\n\ndef main\n t = int\n t.times do\n solve\n end\nend\n\nDEBUG = true\nMOD = 10**9+7\nYESNO = %w(No Yes)\nINF = 10**9\n\ndef int; gets.to_s.to_i end\ndef ints; gets.to_s.split.map { |s| s.to_i } end\ndef int1s; gets.to_s.split.map { |s| s.to_i - 1 } end\ndef float; gets.to_s.to_f end\ndef floats; gets.to_s.split.map { |s| s.to_f } end\ndef array_of(&convert); gets.to_s.split.map(&convert) end\ndef string; gets.to_s.chomp end\ndef rep(n, &b); Array.new(n, &b) end\ndef yes; puts YESNO[1] end\ndef no; puts YESNO[0] end\ndef yesno t; puts YESNO[t] end\ndef zip(xs, *yss); Enumerator.new { |y| xs.zip(*yss) { |a| y.yield(*a) } } end\ndef max(*xs, &block); block_given? ? xs.max_by(&block) : xs.max end\ndef min(*xs, &block); block_given? ? xs.min_by(&block) : xs.min end\ndef minmax(*xs, &block); block_given? ? xs.minmax_by(&block) : xs.minmax end\ndef gcd(*xs); xs.inject(0, :gcd) end\ndef matrix(h, w, fill=nil, &block); return Array.new(h) { Array.new(w, &block) } if block_given?; Array.new(h) { [fill] * w } end\ndef debug(x = nil); STDERR.puts (block_given? ? yield(x) : x).inspect if DEBUG; x end\ndef debug_grid(grid, width = 1); grid.each { |row| STDERR.puts row.map { |x| x.inspect.ljust(width) }.join(\"\") } if DEBUG; grid end\ndef if_debug; yield if DEBUG end\n\nmodule Boolean\n def coerce(other); [other, to_i] end\n def +@; to_i end\n def to_int; to_i end\n def *(other); to_i * other end\nend\n\nclass TrueClass\n include Boolean\n def to_i; 1 end\nend\n\nclass FalseClass\n include Boolean\n def to_i; 0 end\nend\n\nclass Integer\n def div_ceil(y); (self + y - 1) / y end\n def mod_inv(mod = MOD); pow(mod-2, mod) end\n def mod_div(y, mod = MOD); self * mod_inv(y, mod) % mod end\n def factorial(mod = MOD); (2..self).inject(1) { |f, x| f * x % mod } end\n def popcount; x = self; c = 0; while x > 0; c += 1 if x & 1 == 1; x >>= 1 end; c end #TODO: faster\n def bitbrute(&block); (1< 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\n def lower_bound; ac, wa = self.end_open, self.begin; while ac - wa > 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\n def shakutori(r2, &pred); Enumerator.new { |y| j, r = r2.begin, r2.end_open; each { |i| j += 1 while j + 1 < r and pred[i, j+1]; y.yield(i, j) } }; end\n def widest(&block); Enumerator.new { |y| j, n = self.begin, self.end_open; each { |i| j += 1 while j < n and block[i, j]; y.yield(i, j) if block[i, j] } } end\nend\n\nclass Array\n def power(&block); (0 ... 1 << size).each(&block) end\n def sorted_merge(other); a = []; i = j = 0; n, m = size, other.size; if j < m and other[j] < self[i]; a << other[j]; j += 1 else; a << self[i]; i += 1 end while i < n; a.push(*other[j..-1]) if j < m; a end\n def upper_bound; ac, wa = 0, size; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\n def lower_bound; ac, wa = size, 0; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\n def cum(*xs, &op); a = []; a << xs[0] if xs.size > 0; a << x = self[0]; (1...size).each { |i| a << x = op[x, self[i]] }; a end\n def cumdiff(range); self[range.end_open] - self[range.begin]; end\nend\n\nmodule Enumerable\n def sorted_uniq; x = nil; filter { |y| c = x === y; x = y; !c } end\n def cumsum; ys = [0]; each { |x| ys << x + ys[-1] }; ys end\nend\n\nmain"}], "negative_code": [{"source_code": "def good(s)\r\n cur = 0\r\n s.each_char do |c|\r\n cur += c == '(' ? 1 : -1\r\n return false if cur < 0\r\n end\r\n cur == 0\r\nend\r\ngets.to_i.times do\r\n s = gets.chomp\r\n ok = false\r\n (0..7).each do |msk|\r\n cur = s\r\n (0...s.size).each do |i|\r\n if (msk >> (s[i].ord - 'A'.ord)) & 1 != 0\r\n cur[i] = '('\r\n else\r\n cur[i] = ')'\r\n end\r\n ok |= good cur\r\n end\r\n end\r\n puts ok ? \"YES\" : \"NO\"\r\nend"}, {"source_code": "def good(s)\r\n cur = 0\r\n s.each_char do |c|\r\n cur += c == '(' ? 1 : -1\r\n return false if cur < 0\r\n end\r\n cur == 0\r\nend\r\ngets.to_i.times do\r\n s = gets.chomp\r\n ok = false\r\n (0..7).each do |msk|\r\n cur = s\r\n (0...s.size).each do |i|\r\n if msk >> (s[i].ord - 'A'.ord) & 1 != 0\r\n cur[i] = '('\r\n else\r\n cur[i] = ')'\r\n end\r\n ok |= good cur\r\n end\r\n end\r\n puts ok ? \"YES\" : \"NO\"\r\nend"}, {"source_code": "def good(s)\r\n cur = 0\r\n s.each_char do |c|\r\n cur += c == '(' ? 1 : -1\r\n return false if cur < 0\r\n end\r\n cur == 0\r\nend\r\ngets.to_i.times do\r\n s = gets.chomp\r\n ok = false\r\n (1..7).each do |msk|\r\n cur = s\r\n (0...s.size).each do |i|\r\n if msk >> (s[i].ord - 'A'.ord) & 1 != 0\r\n cur[i] = '('\r\n else\r\n cur[i] = ')'\r\n end\r\n ok |= good cur\r\n end\r\n end\r\n puts ok ? \"YES\" : \"NO\"\r\nend"}], "src_uid": "4d24aaf5ebf70265b027a6af86e09250"} {"nl": {"description": "Vasya has been playing Plane of Tanks with his friends the whole year. Now it is time to divide the participants into several categories depending on their results. A player is given a non-negative integer number of points in each round of the Plane of Tanks. Vasya wrote results for each round of the last year. He has n records in total.In order to determine a player's category consider the best result obtained by the player and the best results of other players. The player belongs to category: \"noob\" — if more than 50% of players have better results; \"random\" — if his result is not worse than the result that 50% of players have, but more than 20% of players have better results; \"average\" — if his result is not worse than the result that 80% of players have, but more than 10% of players have better results; \"hardcore\" — if his result is not worse than the result that 90% of players have, but more than 1% of players have better results; \"pro\" — if his result is not worse than the result that 99% of players have. When the percentage is calculated the player himself is taken into account. That means that if two players played the game and the first one gained 100 points and the second one 1000 points, then the first player's result is not worse than the result that 50% of players have, and the second one is not worse than the result that 100% of players have.Vasya gave you the last year Plane of Tanks results. Help Vasya determine each player's category.", "input_spec": "The first line contains the only integer number n (1 ≤ n ≤ 1000) — a number of records with the players' results. Each of the next n lines contains a player's name and the amount of points, obtained by the player for the round, separated with a space. The name contains not less than 1 and no more than 10 characters. The name consists of lowercase Latin letters only. It is guaranteed that any two different players have different names. The amount of points, obtained by the player for the round, is a non-negative integer number and does not exceed 1000.", "output_spec": "Print on the first line the number m — the number of players, who participated in one round at least. Each one of the next m lines should contain a player name and a category he belongs to, separated with space. Category can be one of the following: \"noob\", \"random\", \"average\", \"hardcore\" or \"pro\" (without quotes). The name of each player should be printed only once. Player names with respective categories can be printed in an arbitrary order.", "sample_inputs": ["5\nvasya 100\nvasya 200\nartem 100\nkolya 200\nigor 250", "3\nvasya 200\nkolya 1000\nvasya 1000"], "sample_outputs": ["4\nartem noob\nigor pro\nkolya random\nvasya random", "2\nkolya pro\nvasya pro"], "notes": "NoteIn the first example the best result, obtained by artem is not worse than the result that 25% of players have (his own result), so he belongs to category \"noob\". vasya and kolya have best results not worse than the results that 75% players have (both of them and artem), so they belong to category \"random\". igor has best result not worse than the result that 100% of players have (all other players and himself), so he belongs to category \"pro\".In the second example both players have the same amount of points, so they have results not worse than 100% players have, so they belong to category \"pro\"."}, "positive_code": [{"source_code": "def classify(percent)\n return \"noob\" if percent > 50.0\n return \"random\" if percent > 20.0\n return \"average\" if percent > 10.0\n return \"hardcore\" if percent > 1.0\n return \"pro\"\nend\n\nn = gets.chomp.to_i\nh = Hash.new()\nfor i in 0..n-1\n nm, pt = gets.chomp.split(/ /)\n pt = pt.to_i\n if h.key?(nm)\n if h[nm] < pt\n h[nm] = pt\n end\n else\n h[nm] = pt\n end\nend\n\na = h.to_a\nn = a.length\na.sort!{|a1, a2|\n a2[1] <=> a1[1]\n}\nret = []\nwkper = 0\nprev = 0\nfor i in 0..n-1\n if prev > a[i][1]\n wkper = (i / n.to_f) * 100\n end\n ret << [a[i][0], classify(wkper)]\n prev = a[i][1]\nend\nret.sort!{|r1, r2|\n r1[0] <=> r2[0]\n}\nputs n\nret.each{|rr|\n puts rr.join(\" \")\n}\n\n"}, {"source_code": "result = Hash.new\nresult.default = 0\nn = gets.to_i\nn.times do\n a,b = gets.split\n b = b.to_i\n result[a] = [result[a],b].max\nend\n\narray = result.sort\nm=array.length\nputs m\nm.times{|i|\n cnt=0\n tmp=array[i][1]\n m.times{|j|\n cnt+=1 if tmp=99.0\n puts \" pro\"\n elsif per>=90.0\n puts \" hardcore\"\n elsif per>=80.0\n puts \" average\"\n elsif per>=50.0\n puts \" random\"\n else\n puts \" noob\"\n end\n}"}, {"source_code": "result = Hash.new\nresult.default = 0\nn = gets.to_i\nn.times do\n a,b = gets.split\n b = b.to_i\n result[a] = [result[a],b].max\nend\narray = result.sort\nm=array.length\nputs m\nm.times do |i|\n cnt=0\n tmp=array[i][1]\n m.times{|j|\n cnt+=1 if tmp=99.0\n puts \" pro\"\n elsif per>=90.0\n puts \" hardcore\"\n elsif per>=80.0\n puts \" average\"\n elsif per>=50.0\n puts \" random\"\n else\n puts \" noob\"\n end\nend"}, {"source_code": "n = STDIN.gets.to_i\nscores = {}\n(0...n).each do |it|\n\tname, score = STDIN.gets.split ' '\n\tscore = score.to_i\n\tif scores.has_key? name\n\t\tscores[name] = [scores[name], score].max\n\telse\n\t\tscores[name] = score\n\tend \nend\n\np scores.size\nscores.keys.each do |i|\n\tscore = scores[i]\n\tnot_worse = 0\n\tbetter = 0\n scores.values.each do |j| \n \tif j <= score\n \t\tnot_worse += 1\n\t\telse\n\t\t\tbetter += 1\n\t\tend\n end\n category = \"\"\n if better > 0.5 * scores.size\n \tcategory = \"noob\"\n\telsif better > 0.2 * scores.size\n\t\tcategory = \"random\"\n\telsif better > 0.1 * scores.size\n\t\tcategory = \"average\"\n\telsif better > 0.01 * scores.size\n\t\tcategory = \"hardcore\"\n\telse\n\t\tcategory = \"pro\"\n\tend\n\tputs \"#{i} #{category}\"\nend"}], "negative_code": [{"source_code": "result = Hash.new\nresult.default = 0\nn = gets.to_i\nn.times do\n a,b = gets.split\n b = b.to_i\n result[a] = [result[a],b].max\nend\n\narray = result.sort\nm=array.length\nm.times{|i|\n cnt=0\n tmp=array[i][1]\n m.times{|j|\n cnt+=1 if tmp99.0\n puts \" pro\"\n elsif per>90.0\n puts \" hardcore\"\n elsif per>80.0\n puts \" average\"\n elsif per>50.0\n puts \" random\"\n else\n puts \" noob\"\n end\n}"}], "src_uid": "0430fa56ec7f97efaf9d37096f72bcf8"} {"nl": {"description": "Some natural number was written on the board. Its sum of digits was not less than k. But you were distracted a bit, and someone changed this number to n, replacing some digits with others. It's known that the length of the number didn't change.You have to find the minimum number of digits in which these two numbers can differ.", "input_spec": "The first line contains integer k (1 ≤ k ≤ 109). The second line contains integer n (1 ≤ n < 10100000). There are no leading zeros in n. It's guaranteed that this situation is possible.", "output_spec": "Print the minimum number of digits in which the initial number and n can differ.", "sample_inputs": ["3\n11", "3\n99"], "sample_outputs": ["1", "0"], "notes": "NoteIn the first example, the initial number could be 12.In the second example the sum of the digits of n is not less than k. The initial number could be equal to n."}, "positive_code": [{"source_code": "k=gets.to_i\nn=gets.chomp.chars.sort.map &:to_i\nx=n.inject :+\nres=0\nwhile x=k\n d += 1\n s += 9 - x\n}\np d"}, {"source_code": "k = gets.to_i\nn = gets.chomp.split('').map(&:to_i).sort\nsum = n.inject(:+)\nans = 0\nidx = 0\nwhile sum < k\n sum += 9 - n[idx]\n idx += 1\n ans += 1\nend\nputs ans"}], "negative_code": [{"source_code": "k=gets.to_i\nn=gets.chars.sort.map &:to_i\nx=n.inject :+\nres=0\nwhile x{[*gets.chars]-[\" \"]};a=f[];puts f[].group_by{|i|i}.any?{|i,j|a.count(i)=msg.count(c))\n}\n\nputs ans ? :YES : :NO\n"}, {"source_code": "def main()\n\ts1=gets.chomp.to_s\n\ts2=gets.chomp.to_s\n\tcharavail=[]\n\tfor i in 0...(s1.length)\n\t\tif s1[i].chr!=\" \"\n\t\tcharavail << s1[i].chr\n\t\tend\t\n\tend\n\tf=true\n\tfor i in 0...(s2.length)\n\t\tif s2[i].chr!=\" \"\n\t\t\tif charavail.include?(s2[i].chr)\n\t\t\t\tind=charavail.index(s2[i].chr)\n\t\t\t\tcharavail.delete_at(ind)\n\t\t\telse\n\t\t\t\tf=false\n\t\t\t\tbreak;\n\t\t\tend\n\t\tend\n\tend\n\tif f \n\t\tputs \"YES\"\n\telse\n\t\tputs \"NO\"\n\tend\nend\nmain()"}, {"source_code": "a=[*gets.chars]\nputs gets.scan(/\\S/).all?{|b|(c=a.index b)&&a.delete_at(c)}?:YES:\"NO\"\n"}, {"source_code": "s1 = gets.chomp!.gsub(\" \",\"\")\ns2 = gets.chomp!.gsub(\" \",\"\")\n\ns2.each_char { |c|\n index = s1.index(c)\n if index == nil\n puts \"NO\"\n exit\n else\n s1[index..index] = \"\"\n end\n}\n\nputs \"YES\""}, {"source_code": "s1, s2 = gets.chomp, gets.chomp\n\ns1 = s1.scan(/./).reject{ |x| x =~ /\\s+/ }\ns2 = s2.scan(/./).reject{ |x| x =~ /\\s+/ }\n\nres = true\ns2.each do |char|\n i = s1.index(char)\n if i\n s1.delete_at(i)\n else\n res = false\n break\n end\nend\n\nputs res ? \"YES\" : \"NO\""}, {"source_code": "a=[*gets.chars]\nputs gets.scan(/\\S/).all?{|b|(c=a.index b)&&a.delete_at(c)}?:YES:\"NO\"\n"}, {"source_code": "a=[*gets.chars]\nputs gets.scan(/\\S/).all?{|b|(c=a.index b)&&a.delete_at(c)}?:YES:\"NO\"\n"}, {"source_code": "require 'set'\n\nYES, NO = \"YES\", \"NO\"\n\ndef get_table(test)\n result = Hash.new(0)\n test.gsub(/\\s+/, '').each_char { |char| result[char] += 1 }\n result\nend\n\nheader, text = get_table(gets), get_table(gets)\n\nok = true\n\ntext.each_key do |letter|\n if text[letter] > header[letter]\n ok = false\n break\n end\nend\n\nputs ok ? YES : NO\n"}, {"source_code": "a=[*gets.chars]\nputs gets.scan(/\\S/).all?{|b|(c=a.index b)&&a.delete_at(c)}?:YES:\"NO\""}, {"source_code": "h, l = STDIN.read.split(\"\\n\")\n\nl.each_char do |c|\n next if c == ' '\n i = h.index c\n (print :NO; exit) if i.nil?\n h[i] = ''\nend\n\nprint :YES"}, {"source_code": "a=[*gets.chars]\nputs gets.scan(/\\S/).all?{|b|(c=a.index b)&&a.delete_at(c)}?:YES:\"NO\"\n"}], "negative_code": [{"source_code": "str=gets\nstr2=gets\n#str=\"Instead of dogging Your footsteps it disappears but you dont notice anything\"\n#str2=\"where is your dog\"\n\nsymbols=str2.scan(/(\\w)/)\nr=true\nsymbols.each{|i|\n\tif str=~/#{i}/\n\t\tstr=str.sub(/#{i}/,\"\")\n\telse\n\t\tr=false\n\t\tbreak\n\tend\n}\nif r then p \"YES\"\nelse print \"NO\"\nend"}, {"source_code": "#=================================\nrequire 'prime'\ninclude Math\n\nif ARGV.include?(\"ONLINE_JUDGE\")\n\t$stdin = File.open(\"./input.txt\", \"r\")\n\tARGV.delete(\"ONLINE_JUDGE\")\nend\n#=================================\n\ntxt=gets.strip\nmsg=gets.strip\n\nans=true\nmsg.each_char{|c|\n\t(ans=false and break) if !(msg.count(c)>=txt.count(c))\n}\n\nputs ans ? :YES : :NO\n"}, {"source_code": "s1 = gets.chomp!.gsub(\" \",\"\")\ns2 = gets.chomp!.gsub(\" \",\"\")\n\ns2.each_char { |c|\n if s1.index(c) == nil\n puts \"NO\"\n exit\n end\n}\n\nputs \"YES\""}], "src_uid": "b1ef19d7027dc82d76859d64a6f43439"} {"nl": {"description": "Petya organized a strange birthday party. He invited $$$n$$$ friends and assigned an integer $$$k_i$$$ to the $$$i$$$-th of them. Now Petya would like to give a present to each of them. In the nearby shop there are $$$m$$$ unique presents available, the $$$j$$$-th present costs $$$c_j$$$ dollars ($$$1 \\le c_1 \\le c_2 \\le \\ldots \\le c_m$$$). It's not allowed to buy a single present more than once.For the $$$i$$$-th friend Petya can either buy them a present $$$j \\le k_i$$$, which costs $$$c_j$$$ dollars, or just give them $$$c_{k_i}$$$ dollars directly.Help Petya determine the minimum total cost of hosting his party.", "input_spec": "The first input line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 10^3$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\leq n, m \\leq 3 \\cdot 10^5$$$) — the number of friends, and the number of unique presents available. The following line contains $$$n$$$ integers $$$k_1, k_2, \\ldots, k_n$$$ ($$$1 \\leq k_i \\leq m$$$), assigned by Petya to his friends. The next line contains $$$m$$$ integers $$$c_1, c_2, \\ldots, c_m$$$ ($$$1 \\le c_1 \\le c_2 \\le \\ldots \\le c_m \\le 10^9$$$) — the prices of the presents. It is guaranteed that sum of values $$$n$$$ over all test cases does not exceed $$$3 \\cdot 10^5$$$, and the sum of values $$$m$$$ over all test cases does not exceed $$$3 \\cdot 10^5$$$.", "output_spec": "For each test case output a single integer — the minimum cost of the party.", "sample_inputs": ["2\n5 4\n2 3 4 3 2\n3 5 12 20\n5 5\n5 4 3 2 1\n10 40 90 160 250", "1\n1 1\n1\n1"], "sample_outputs": ["30\n190", "1"], "notes": "NoteIn the first example, there are two test cases. In the first one, Petya has $$$5$$$ friends and $$$4$$$ available presents. Petya can spend only $$$30$$$ dollars if he gives $$$5$$$ dollars to the first friend. A present that costs $$$12$$$ dollars to the second friend. A present that costs $$$5$$$ dollars to the third friend. A present that costs $$$3$$$ dollars to the fourth friend. $$$5$$$ dollars to the fifth friend. In the second one, Petya has $$$5$$$ and $$$5$$$ available presents. Petya can spend only $$$190$$$ dollars if he gives A present that costs $$$10$$$ dollars to the first friend. A present that costs $$$40$$$ dollars to the second friend. $$$90$$$ dollars to the third friend. $$$40$$$ dollars to the fourth friend. $$$10$$$ dollars to the fifth friend. "}, "positive_code": [{"source_code": "t= gets.chomp.to_i\nwhile t > 0\n n, m = gets.chomp.split(' ')\n n = n.to_i\n m = m.to_i\n k = gets.chomp.split(' ').map(&:to_i)\n c = gets.chomp.split(' ').map(&:to_i)\n k = k.sort.reverse()\n last_used_gift = 0\n cost = 0 \n k.each do |ks|\n if(c[ks-1] > c[last_used_gift] && last_used_gift < m)\n \tcost = cost + c[last_used_gift]\n \tlast_used_gift = last_used_gift + 1\n else\n \tcost = cost + c[ks-1]\n end\n end\n puts cost\n t = t - 1\nend\n\n\n\n\n\n"}, {"source_code": "\r\nt = gets.to_i\r\nans = []\r\nt.times do\r\n n,m = gets.chomp.split(\" \").map(&:to_i)\r\n k = gets.chomp.split(\" \").map(&:to_i).sort!.reverse!\r\n c = gets.chomp.split(\" \").map(&:to_i)\r\n sum = 0\r\n index = 0\r\n n.times do |i|\r\n if c[k[i]-1] > c[index]\r\n sum += c[index]\r\n index += 1\r\n else\r\n sum += c[k[i]-1]\r\n end\r\n end\r\n ans << sum\r\nend\r\nputs ans"}], "negative_code": [], "src_uid": "55962ef2cf88c87873b996dc54cc1bf1"} {"nl": {"description": "This is a simplified version of the problem B2. Perhaps you should read the problem B2 before you start solving B1.Paul and Mary have a favorite string $$$s$$$ which consists of lowercase letters of the Latin alphabet. They want to paint it using pieces of chalk of two colors: red and green. Let's call a coloring of a string wonderful if the following conditions are met: each letter of the string is either painted in exactly one color (red or green) or isn't painted; each two letters which are painted in the same color are different; the number of letters painted in red is equal to the number of letters painted in green; the number of painted letters of this coloring is maximum among all colorings of the string which meet the first three conditions. E. g. consider a string $$$s$$$ equal to \"kzaaa\". One of the wonderful colorings of the string is shown in the figure. The example of a wonderful coloring of the string \"kzaaa\". Paul and Mary want to learn by themselves how to find a wonderful coloring of the string. But they are very young, so they need a hint. Help them find $$$k$$$ — the number of red (or green, these numbers are equal) letters in a wonderful coloring.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of one non-empty string $$$s$$$ which consists of lowercase letters of the Latin alphabet. The number of characters in the string doesn't exceed $$$50$$$.", "output_spec": "For each test case, output a separate line containing one non-negative integer $$$k$$$ — the number of letters which will be painted in red in a wonderful coloring.", "sample_inputs": ["5\nkzaaa\ncodeforces\narchive\ny\nxxxxxx"], "sample_outputs": ["2\n5\n3\n0\n1"], "notes": "NoteThe first test case contains the string from the statement. One of the wonderful colorings is shown in the figure. There's no wonderful coloring containing $$$3$$$ or more red letters because the total number of painted symbols will exceed the string's length.The string from the second test case can be painted as follows. Let's paint the first occurrence of each of the letters \"c\", \"o\", \"e\" in red and the second ones in green. Let's paint the letters \"d\", \"f\" in red and \"r\", \"s\" in green. So every letter will be painted in red or green, hence the answer better than $$$5$$$ doesn't exist.The third test case contains the string of distinct letters, so you can paint any set of characters in red, as long as the size of this set doesn't exceed half of the size of the string and is the maximum possible.The fourth test case contains a single letter which cannot be painted in red because there will be no letter able to be painted in green.The fifth test case contains a string of identical letters, so there's no way to paint more than one letter in red."}, "positive_code": [{"source_code": "def calculate(chuoi)\r\n hash_ky_tu = Hash.new { |hash, key| hash[key] = 0 }\r\n (0...chuoi.length).each do |index|\r\n hash_ky_tu[chuoi[index]] += 1\r\n end\r\n\r\n lap_lai_3_lan = 0\r\n lap_lai_2_lan = 0\r\n lap_lai_1_lan = 0\r\n\r\n hash_ky_tu.values.each do |so_lan_lap_lai|\r\n case so_lan_lap_lai\r\n when 3\r\n lap_lai_3_lan += 1\r\n when 1\r\n lap_lai_1_lan += 1\r\n else\r\n lap_lai_2_lan += 1\r\n end\r\n end\r\n lap_lai_3_lan + lap_lai_2_lan + lap_lai_1_lan / 2\r\nend\r\n\r\nt = gets.to_i\r\nt.times do\r\n chuoi = gets.chomp\r\n puts calculate(chuoi)\r\nend"}], "negative_code": [], "src_uid": "a6b760941ab8be2c32c6dc66c623ea0e"} {"nl": {"description": "A student of z-school found a kind of sorting called z-sort. The array a with n elements are z-sorted if two conditions hold: ai ≥ ai - 1 for all even i, ai ≤ ai - 1 for all odd i > 1. For example the arrays [1,2,1,2] and [1,1,1,1] are z-sorted while the array [1,2,3,4] isn’t z-sorted.Can you make the array z-sorted?", "input_spec": "The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of elements in the array a. The second line contains n integers ai (1 ≤ ai ≤ 109) — the elements of the array a.", "output_spec": "If it's possible to make the array a z-sorted print n space separated integers ai — the elements after z-sort. Otherwise print the only word \"Impossible\".", "sample_inputs": ["4\n1 2 2 1", "5\n1 3 2 2 5"], "sample_outputs": ["1 2 1 2", "1 5 2 3 2"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\na = gets.split(\" \").map(&:to_i).sort\ni = 0\nj = n-1\n\nb = []\nwhile true do\n b << a[i]\n i += 1\n break if i > j\n b << a[j]\n j -= 1\n break if i > j\nend\n\nvalid = true\nn.times do |i|\n if (i+1).even?\n valid = false if b[i] < b[i-1]\n elsif (i+1).odd? && i > 0\n valid = false if b[i] > b[i-1]\n end\nend\n\nputs valid ? b.join(\" \") : \"Impossible\"\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nfor i in 0..n-1\n if (i+1)%2==0 then printf \"%d \", a.max; a.delete_at a.index(a.max);\n else printf \"%d \", a.min; a.delete_at a.index(a.min); end\nend"}, {"source_code": "gets;a=gets.split.map(&:to_i).sort;b=a.pop(a.size/2).reverse;puts a.zip(b)*' '"}, {"source_code": "n = gets.split.map(&:to_i)\narr = gets.split.map(&:to_i)\nres = []\nfor i in 0..n[0] - 1 do\n if i == 0 or (i + 1) % 2 != 0\n res[i] = arr.min\n else\n res[i] = arr.max\n end\n arr.delete_at(arr.index(res[i]) || arr.length)\nend\nputs res.join(' ')\n"}, {"source_code": "#!/usr/bin/env ruby\n\nn = gets.to_i\ninp = gets.split.map(&:to_i)\nfor i in 0..n-1\n if (i+1) % 2 == 0 then printf \"%d \", inp.max; inp.delete_at inp.index(inp.max)\n else printf \"%d \", inp.min; inp.delete_at inp.index(inp.min) end\nend\nputs\n"}, {"source_code": "n = readline.to_i\narr = readline.split(' ').collect(&:to_i).sort\narr[0..(n / 2)].zip(arr[(n / 2 + 1)..-1]).flatten.compact.each_with_index do |num, ind|\n print \" \" if ind != 0\n print num\nend\nputs \"\"\n"}, {"source_code": "a=gets.chomp.to_i\nb=gets.chomp.split(\" \")\n0.upto(a-1) {|i| b[i]=b[i].to_i}\nb.sort!\nif a%2==0\n0.upto (a/2-1) do |i|\nprintf \"#{b[i]} #{b[a/2+i]} \"\nend\nelse\n0.upto (a/2) do |i|\nif i!=a/2\nprintf \"#{b[i]} #{b[a/2+1+i]} \"\nelse\nprintf \"#{b[i]}\"\nend\nend\nend"}, {"source_code": "a=gets.chomp.to_i\nb=gets.chomp.split(\" \")\n0.upto(a-1) {|i| b[i]=b[i].to_i}\nb.sort!\nif a%2==0\n0.upto (a/2-1) do |i|\nprintf \"#{b[i]} #{b[a/2+i]} \"\nend\nelse\n0.upto (a/2) do |i|\nif i!=a/2\nprintf \"#{b[i]} #{b[a/2+1+i]} \"\nelse\nprintf \"#{b[i]}\"\nend\nend\nend\n"}, {"source_code": "a=gets.chomp.to_i\nb=gets.chomp.split(\" \")\n0.upto(a-1) {|i| b[i]=b[i].to_i}\nb.sort!\nif a%2==0\n0.upto (a/2-1) do |i|\nprintf \"#{b[i]} #{b[a/2+i]} \"\nend\nelse\n0.upto (a/2) do |i|\nif i!=a/2\nprintf \"#{b[i]} #{b[a/2+1+i]} \"\nelse\nprintf \"#{b[i]}\"\nend\nend\nend"}, {"source_code": "gets\na = gets.split.map(&:to_i)\na.sort!\nb = []\na.size.times do |i|\n if i.even?\n b << a.shift\n else\n b << a.pop\n end\nend\nputs b.join ' '\n"}, {"source_code": "n=gets.to_i\na=gets.split(' ')\nfor i in 0..a.size-1\n\ta[i]=a[i].to_i\nend\na=a.sort\nl=0\nr=a.size-1\nans=\"\"\nwhile l<=r\n\tans+=\" #{a[l]}\"\n\tans+=\" #{a[r]}\" if l0?sum[i-1]:0)+a[i]\n rsum[n-i-1]=(i>0?rsum[n-i]:0)+a[n-i-1]\n else\n sum[i]=sum[i-1]+a[i]-a[i-k]\n rsum[n-i-1]=rsum[n-i]+a[n-i-1]-a[n-i+k-1]\n end\n \n if i==0\n bestr[n-1]=rsum[n-1]\n bstart[n-1]=n-1\n else\n if bestr[n-i] > rsum[n-i-1]\n bestr[n-i-1] = bestr[n-i]\n bstart[n-i-1] = bstart[n-i]\n else\n bestr[n-i-1] = rsum[n-i-1]\n bstart[n-i-1] = n-i-1\n end\n end\nend\n \nmax=0\nbesta=0\nbestb=0\nfor i in k-1..n-k-1\n s = sum[i]+bestr[i+1]\n if max < s\n max = s\n besta=i\n bestb=bstart[i+1]\n end\nend\n \nputs \"#{besta+2-k} #{bestb+1}\""}, {"source_code": "n, k = STDIN.readline.split.map {|s| s.to_i }\nvalues = STDIN.readline.split.map {|s| s.to_i }\nsum = values[0..k-1].inject(:+)\nsums = [sum]\nbest = [[sum, 0]]\n(k..n-1).each do |i|\n\tsum += values[i] - values[i-k]\n\tsums.push(sum)\n\tif sum <= best[-1][0]\n\t\tbest.push(best[-1])\n\telse\n\t\tbest.push([sum, i-k+1])\n\tend\nend\nleft, right = 0, k\n(k+1..n-k).each do |i|\n\tif best[i-k][0] + sums[i] > sums[left] + sums[right]\n\t\tleft, right = best[i-k][1], i\n\tend\nend\nputs '%d %d' % [left+1, right+1]\n"}, {"source_code": "n,k=gets.split.map(&:to_i)\na=gets.split.map(&:to_i)\nsum=Array.new(n)\nrsum=Array.new(n)\nbestl=Array.new(n)\nbestr=Array.new(n)\nastart=Array.new(n)\nbstart=Array.new(n)\nn.times do |i|\n if i0?sum[i-1]:0)+a[i]\n rsum[n-i-1]=(i>0?rsum[n-i]:0)+a[n-i-1]\n else\n sum[i]=sum[i-1]+a[i]-a[i-k]\n rsum[n-i-1]=rsum[n-i]+a[n-i-1]-a[n-i+k-1]\n end\n\n if i==0\n bestr[n-1]=rsum[n-1]\n bstart[n-1]=n-1\n else\n if bestr[n-i] > rsum[n-i-1]\n bestr[n-i-1] = bestr[n-i]\n bstart[n-i-1] = bstart[n-i]\n else\n bestr[n-i-1] = rsum[n-i-1]\n bstart[n-i-1] = n-i-1\n end\n end\nend\n\nmax=0\nbesta=0\nbestb=0\nfor i in k-1..n-k-1\n s = sum[i]+bestr[i+1]\n if max < s\n max = s\n besta=i\n bestb=bstart[i+1]\n end\nend\n\n#p sum\n#p rsum\n#p bestr\n#p bstart\nputs \"#{besta+2-k} #{bestb+1}\""}], "negative_code": [{"source_code": "def add(heap, x)\n\theap.push(x)\n\tpos = heap.length-1\n\twhile pos != 0\n\t\tparent_pos = (pos-1)/2\n\t\tbreak if (heap[parent_pos] <=> x) >= 0\n\t\theap[parent_pos], heap[pos] = heap[pos], heap[parent_pos]\n\t\tpos = parent_pos\n\tend\nend\ndef remove(heap, pos, heap_pos)\n\tx = heap.pop\n\treturn if heap.length == 0\n\theap[pos] = x\n\theap_pos[-heap[pos][1]] = pos\n\twhile true\n\t\tchild_pos = 2*pos+1\n\t\tbreak if child_pos >= heap.length\n\t\tif child_pos+1 < heap.length and (heap[child_pos+1] <=> heap[child_pos]) == -1\n\t\t\tchild_pos = child_pos+1\n\t\tend\n\t\tbreak if (heap[pos] <=> heap[child_pos]) >= 0\n\t\theap[child_pos], heap[pos] = heap[pos], heap[child_pos]\n\t\theap_pos[-heap[pos][1]] = pos\n\t\theap_pos[-heap[child_pos][1]] = child_pos\n\tend\nend\n\nn, k = STDIN.readline.split.map {|s| s.to_i }\nvalues = STDIN.readline.split.map {|s| s.to_i }\nsum = values[0..k-1].inject(:+)\nsums = [sum]\n(k..n-1).each do |i|\n\tsum += values[i] - values[i-k]\n\tsums.push(sum)\nend\nheap = []\n(k..n-k).each {|i| add(heap, [sums[i], -i]) }\nbest_score = 0\nheap_pos = {}\nheap.each_with_index do |item, i|\n\tsum, key = item\n\theap_pos[-key] = i\nend\nbest_indices = nil\n(0..n-2*k).each do |i|\n\tvalue, j = heap[0]\n\tscore = sums[i] + value\n\tif score > best_score\n\t\tbest_indices = [i+1, -j+1]\n\t\tbest_score = score\n\tend\n\tremove(heap, heap_pos[i+k], heap_pos)\nend\nputs best_indices.join(' ')\n"}, {"source_code": "n, k = STDIN.readline.split.map {|s| s.to_i }\nvalues = STDIN.readline.split.map {|s| s.to_i }\nsum = values[0..k-1].inject(:+)\nsums = [sum]\n(k..n-1).each do |i|\n\tsum += values[i] - values[i-k]\n\tsums.push(sum)\nend\nleft, right = 0, k\nbest_score = sums[left] + sums[right]\nbest_left, best_right = left, right\nwhile right != n-k\n\tright += 1\n\t(right..n-k).each do |seek|\n\t\tright = seek if sums[seek] > sums[right]\n\tend\n\tleft += 1\n\t(left..right-k).each do |seek|\n\t\tleft = seek if sums[seek] > sums[left]\n\tend\n\tscore = sums[left]+sums[right]\n\tif score > best_score\n\t\tbest_score, best_left, best_right = score, left, right\n\tend\nend\nputs '%d %d' % [best_left+1, best_right+1]\nputs best_score\n"}, {"source_code": "n, k = STDIN.readline.split.map {|s| s.to_i }\nvalues = STDIN.readline.split.map {|s| s.to_i }\nsum = values[0..k-1].inject(:+)\nsums = [sum]\n(k..n-1).each do |i|\n\tsum += values[i] - values[i-k]\n\tsums.push(sum)\nend\na, b = 0, k\n(1..n-k).each do |i|\n\tsum = sums[i]\n\tif sum > sums[a] and (i-b).abs >= k\n\t\tif (i-a).abs >= k and (sums[b] < sums[a] or (sums[b] == sums[a] and b > a))\n\t\t\tb = i\n\t\telse\n\t\t\ta = i\n\t\tend\n\telsif sum > sums[b] and (i-a).abs >= k\n\t\tb = i\n\tend\nend\nputs '%d %d' % [[a, b].min + 1, [a, b].max + 1]\n"}, {"source_code": "n, k = STDIN.readline.split.map {|s| s.to_i }\nvalues = STDIN.readline.split.map {|s| s.to_i }\nsum = values[0..k-1].inject(:+)\nsums = [sum]\n(k..n-1).each do |i|\n\tsum += values[i] - values[i-k]\n\tsums.push(sum)\nend\nleft, right = 0, k\nbest_score = sums[left] + sums[right]\nbest_left, best_right = left, right\nwhile right != n-k\n\tright += 1\n\t(right..n-k).each do |seek|\n\t\tright = seek if sums[seek] > sums[right]\n\tend\n\tleft += 1\n\t(left..right-k).each do |seek|\n\t\tleft = seek if sums[seek] > sums[left]\n\tend\n\tscore = sums[left]+sums[right]\n\tif score > best_score\n\t\tbest_score, best_left, best_right = score, left, right\n\tend\nend\nputs '%d %d' % [best_left+1, best_right+1]\n"}, {"source_code": "def add(heap, x)\n\theap.push(x)\n\tpos = heap.length-1\n\twhile pos != 0\n\t\tparent_pos = (pos-1)/2\n\t\tbreak if (heap[parent_pos] <=> x) >= 0\n\t\theap[parent_pos], heap[pos] = heap[pos], heap[parent_pos]\n\t\tpos = parent_pos\n\tend\nend\ndef remove(heap, pos, heap_pos)\n\tx = heap.pop\n\treturn if pos == heap.length\n\theap[pos] = x\n\theap_pos[-heap[pos][1]] = pos\n\twhile true\n\t\tchild_pos = 2*pos+1\n\t\tbreak if child_pos >= heap.length\n\t\tif child_pos+1 < heap.length and (heap[child_pos] <=> heap[child_pos+1]) == -1\n\t\t\tchild_pos = child_pos+1\n\t\tend\n\t\tbreak if (heap[pos] <=> heap[child_pos]) >= 0\n\t\theap[child_pos], heap[pos] = heap[pos], heap[child_pos]\n\t\theap_pos[-heap[pos][1]] = pos\n\t\theap_pos[-heap[child_pos][1]] = child_pos\n\tend\nend\n\nn, k = STDIN.readline.split.map {|s| s.to_i }\nvalues = STDIN.readline.split.map {|s| s.to_i }\nsum = values[0..k-1].inject(:+)\nsums = [sum]\n(k..n-1).each do |i|\n\tsum += values[i] - values[i-k]\n\tsums.push(sum)\nend\nheap = []\n(k..n-k).each {|i| add(heap, [sums[i], -i]) }\nbest_score = 0\nheap_pos = {}\nheap.each_with_index do |item, i|\n\tsum, key = item\n\theap_pos[-key] = i\nend\nbest_a, best_b = nil, nil\n(0..n-2*k).each do |a|\n\tsum, b = heap[0]\n\tscore = sums[a] + sum\n\tif score > best_score\n\t\tbest_a, best_b = a+1, -b+1\n\t\tbest_score = score\n\tend\n\tremove(heap, heap_pos[a+k], heap_pos)\nend\nputs '%d %d' % [best_a, best_b]\n#puts best_score\n"}, {"source_code": "def add(heap, x)\n\theap.push(x)\n\tpos = heap.length-1\n\twhile pos != 0\n\t\tparent_pos = (pos-1)/2\n\t\tbreak if (heap[parent_pos] <=> x) >= 0\n\t\theap[parent_pos], heap[pos] = heap[pos], heap[parent_pos]\n\t\tpos = parent_pos\n\tend\nend\ndef remove(heap)\n\tx = heap.pop\n\treturn if heap.length == 0\n\theap[0] = x\n\tpos = 0\n\twhile true\n\t\tchild_pos = 2*pos+1\n\t\tbreak if child_pos >= x.length\n\t\tif child_pos+1 < x.length and heap[child_pos+1] > heap[child_pos]\n\t\t\tchild_pos = child_pos+1\n\t\tend\n\t\tbreak if (heap[pos] <=> heap[child_pos]) >= 0\n\t\theap[child_pos], heap[pos] = heap[pos], heap[child_pos]\n\tend\nend\n\nn, k = STDIN.readline.split.map {|s| s.to_i }\nvalues = STDIN.readline.split.map {|s| s.to_i }\nsum = values[0..k-1].inject(:+)\nsums = [sum]\n(k..n-1).each do |i|\n\tsum += values[i] - values[i-k]\n\tsums.push(sum)\nend\nheap = []\n(k..n-k).each {|i| add(heap, [sums[i], -i]) }\nbest_score = 0\nbest_indices = nil\n(0..n-2*k).each do |i|\n\tscore, j = heap[0]\n\tif score > best_score\n\t\tbest_indices = [i+1, -j+1]\n\t\tbest_score = score\n\tend\nend\nputs best_indices.join(' ')\n"}, {"source_code": "n, k = STDIN.readline.split.map {|s| s.to_i }\nvalues = STDIN.readline.split.map {|s| s.to_i }\nsum = values[0..k-1].inject(:+)\nsums = [sum]\n(k..n-1).each do |i|\n\tsum += values[i] - values[i-k]\n\tsums.push(sum)\nend\na, b = 0, k\n(k+1..n-k).each do |i|\n\tsum = sums[i]\n\tif sum > sums[a] and i-b >= k\n\t\tif sums[b] < sums[a] and i-a >= k\n\t\t\tb = i\n\t\telse\n\t\t\ta = i\n\t\tend\n\telsif sum > sums[b] and i-a >= k\n\t\tb = i\n\tend\nend\nputs '%d %d' % [[a, b].min + 1, [a, b].max + 1]\n"}, {"source_code": "def add(heap, x)\n\theap.push(x)\n\tpos = heap.length-1\n\twhile pos != 0\n\t\tparent_pos = (pos-1)/2\n\t\tbreak if (heap[parent_pos] <=> x) >= 0\n\t\theap[parent_pos], heap[pos] = heap[pos], heap[parent_pos]\n\t\tpos = parent_pos\n\tend\nend\ndef remove(heap, pos, heap_pos)\n\tx = heap.pop\n\treturn if pos == heap.length\n\theap[pos] = x\n\theap_pos[-heap[pos][1]] = pos\n\twhile true\n\t\tchild_pos = 2*pos+1\n\t\tbreak if child_pos >= heap.length\n\t\tif child_pos+1 < heap.length and (heap[child_pos] <=> heap[child_pos+1]) == -1\n\t\t\tchild_pos = child_pos+1\n\t\tend\n\t\tbreak if (heap[pos] <=> heap[child_pos]) >= 0\n\t\theap[child_pos], heap[pos] = heap[pos], heap[child_pos]\n\t\theap_pos[-heap[pos][1]] = pos\n\t\theap_pos[-heap[child_pos][1]] = child_pos\n\tend\nend\n\nn, k = STDIN.readline.split.map {|s| s.to_i }\nvalues = STDIN.readline.split.map {|s| s.to_i }\nsum = values[0..k-1].inject(:+)\nsums = [sum]\n(k..n-1).each do |i|\n\tsum += values[i] - values[i-k]\n\tsums.push(sum)\nend\nheap = []\n(k..n-k).each {|i| add(heap, [sums[i], -i]) }\nbest_score = 0\nheap_pos = {}\nheap.each_with_index do |item, i|\n\tsum, key = item\n\theap_pos[-key] = i\nend\nbest_a, best_b = nil, nil\n(0..n-2*k).each do |a|\n\tsum, b = heap[0]\n\tscore = sums[a] + sum\n\tif score > best_score\n\t\tbest_a, best_b = a+1, -b+1\n\t\tbest_score = score\n\tend\n\tremove(heap, heap_pos[a+k], heap_pos)\nend\nputs '%d %d' % [best_a, best_b]\nputs best_score\n"}, {"source_code": "def add(heap, x)\n\theap.push(x)\n\tpos = heap.length-1\n\twhile pos != 0\n\t\tparent_pos = (pos-1)/2\n\t\tbreak if (heap[parent_pos] <=> x) >= 0\n\t\theap[parent_pos], heap[pos] = heap[pos], heap[parent_pos]\n\t\tpos = parent_pos\n\tend\nend\ndef remove(heap, pos, heap_pos)\n\tx = heap.pop\n\treturn if heap.length == 0\n\theap[pos] = x\n\theap_pos[-heap[pos][1]] = pos\n\twhile true\n\t\tchild_pos = 2*pos+1\n\t\tbreak if child_pos >= heap.length\n\t\tif child_pos+1 < heap.length and (heap[child_pos] <=> heap[child_pos+1]) == -1\n\t\t\tchild_pos = child_pos+1\n\t\tend\n\t\tbreak if (heap[pos] <=> heap[child_pos]) >= 0\n\t\theap[child_pos], heap[pos] = heap[pos], heap[child_pos]\n\t\theap_pos[-heap[pos][1]] = pos\n\t\theap_pos[-heap[child_pos][1]] = child_pos\n\tend\nend\n\nn, k = STDIN.readline.split.map {|s| s.to_i }\nvalues = STDIN.readline.split.map {|s| s.to_i }\nsum = values[0..k-1].inject(:+)\nsums = [sum]\n(k..n-1).each do |i|\n\tsum += values[i] - values[i-k]\n\tsums.push(sum)\nend\nheap = []\n(k..n-k).each {|i| add(heap, [sums[i], -i]) }\nbest_score = 0\nheap_pos = {}\nheap.each_with_index do |item, i|\n\tsum, key = item\n\theap_pos[-key] = i\nend\nbest_indices = nil\n(0..n-2*k).each do |i|\n\tvalue, j = heap[0]\n\tscore = sums[i] + value\n\tif score > best_score\n\t\tbest_indices = [i+1, -j+1]\n\t\tbest_score = score\n\tend\n\tremove(heap, heap_pos[i+k], heap_pos)\nend\nputs best_indices.join(' ')\n"}, {"source_code": "def add(heap, x)\n\theap.push(x)\n\tpos = heap.length-1\n\twhile pos != 0\n\t\tparent_pos = (pos-1)/2\n\t\tbreak if (heap[parent_pos] <=> x) >= 0\n\t\theap[parent_pos], heap[pos] = heap[pos], heap[parent_pos]\n\t\tpos = parent_pos\n\tend\nend\ndef remove(heap, pos, heap_pos)\n\tx = heap.pop\n\treturn if heap.length == 0\n\theap[pos] = x\n\theap_pos[-heap[pos][1]] = pos\n\twhile true\n\t\tchild_pos = 2*pos+1\n\t\tbreak if child_pos >= heap.length\n\t\tif child_pos+1 < heap.length and (heap[child_pos] <=> heap[child_pos+1]) == -1\n\t\t\tchild_pos = child_pos+1\n\t\tend\n\t\tbreak if (heap[pos] <=> heap[child_pos]) >= 0\n\t\theap[child_pos], heap[pos] = heap[pos], heap[child_pos]\n\t\theap_pos[-heap[pos][1]] = pos\n\t\theap_pos[-heap[child_pos][1]] = child_pos\n\tend\nend\n\nn, k = STDIN.readline.split.map {|s| s.to_i }\nvalues = STDIN.readline.split.map {|s| s.to_i }\nsum = values[0..k-1].inject(:+)\nsums = [sum]\n(k..n-1).each do |i|\n\tsum += values[i] - values[i-k]\n\tsums.push(sum)\nend\nheap = []\n(k..n-k).each {|i| add(heap, [sums[i], -i]) }\nbest_score = 0\nheap_pos = {}\nheap.each_with_index do |item, i|\n\tsum, key = item\n\theap_pos[-key] = i\nend\nbest_a, best_b = nil, nil\n(0..n-2*k).each do |a|\n\tsum, b = heap[0]\n\tscore = sums[a] + sum\n\tif score > best_score\n\t\tbest_a, best_b = a+1, -b+1\n\t\tbest_score = score\n\tend\n\tremove(heap, heap_pos[a+k], heap_pos)\nend\nputs '%d %d' % [best_a, best_b]\nputs best_score\n"}, {"source_code": "n, k = STDIN.readline.split.map {|s| s.to_i }\nvalues = STDIN.readline.split.map {|s| s.to_i }\nsum = values[0..k-1].inject(:+)\nsums = [sum]\nbest = [[sum, 0]]\n(k..n-1).each do |i|\n\tsum += values[i] - values[i-k]\n\tsums.push(sum)\n\tif sum <= best[-1][0]\n\t\tbest.push(best[-1])\n\telse\n\t\tbest.push([sum, i-k+1])\n\tend\nend\nleft, right = 0, k\n(k+1..n-k).each do |i|\n\tif best[i-k][0] + sums[i] > sums[left] + sums[right]\n\t\tleft, right = best[i-k][1], i\n\tend\nend\nputs '%d %d' % [left+1, right+1]\nputs sums[left] + sums[right]\n"}, {"source_code": "def add(heap, x)\n\theap.push(x)\n\tpos = heap.length-1\n\twhile pos != 0\n\t\tparent_pos = (pos-1)/2\n\t\tbreak if (heap[parent_pos] <=> x) >= 0\n\t\theap[parent_pos], heap[pos] = heap[pos], heap[parent_pos]\n\t\tpos = parent_pos\n\tend\nend\ndef remove(heap, pos, heap_pos)\n\tx = heap.pop\n\treturn if heap.length == 0\n\theap[pos] = x\n\theap_pos[-heap[pos][1]] = pos\n\twhile true\n\t\tchild_pos = 2*pos+1\n\t\tbreak if child_pos >= heap.length\n\t\tif child_pos+1 < heap.length and (heap[child_pos] <=> heap[child_pos+1]) == -1\n\t\t\tchild_pos = child_pos+1\n\t\tend\n\t\tbreak if (heap[pos] <=> heap[child_pos]) >= 0\n\t\theap[child_pos], heap[pos] = heap[pos], heap[child_pos]\n\t\theap_pos[-heap[pos][1]] = pos\n\t\theap_pos[-heap[child_pos][1]] = child_pos\n\tend\nend\n\nn, k = STDIN.readline.split.map {|s| s.to_i }\nvalues = STDIN.readline.split.map {|s| s.to_i }\nsum = values[0..k-1].inject(:+)\nsums = [sum]\n(k..n-1).each do |i|\n\tsum += values[i] - values[i-k]\n\tsums.push(sum)\nend\nheap = []\n(k..n-k).each {|i| add(heap, [sums[i], -i]) }\nbest_score = 0\nheap_pos = {}\nheap.each_with_index do |item, i|\n\tsum, key = item\n\theap_pos[-key] = i\nend\nbest_a, best_b = nil, nil\n(0..n-2*k).each do |a|\n\tsum, b = heap[0]\n\tscore = sums[a] + sum\n\tif score > best_score\n\t\tbest_a, best_b = a+1, -b+1\n\t\tbest_score = score\n\tend\n\tremove(heap, heap_pos[a+k], heap_pos)\nend\nputs '%d %d' % [best_a, best_b]\n#puts best_score\n"}, {"source_code": "n, k = STDIN.readline.split.map {|s| s.to_i }\nvalues = STDIN.readline.split.map {|s| s.to_i }\nsum = values[0..k-1].inject(:+)\nsums = [sum]\n(k..n-1).each do |i|\n\tsum += values[i] - values[i-k]\n\tsums.push(sum)\nend\nbest_score = -1\nbest_left, best_right = -1, -1\nleft, right = nil, k-1\nwhile right != n-k\n\tleft = right-k+1\n\tright += 1\n\t(right..n-k).each do |seek|\n\t\tright = seek if sums[seek] > sums[right]\n\tend\n\t(left..right-k).each do |seek|\n\t\tleft = seek if sums[seek] > sums[left]\n\tend\n\tbreak if 2*[sums[left], sums[right]].max <= best_score\n\tscore = sums[left]+sums[right]\n\tif score > best_score\n\t\tbest_score, best_left, best_right = score, left, right\n\tend\nend\nputs '%d %d' % [best_left+1, best_right+1]\n"}, {"source_code": "def add(heap, x)\n\theap.push(x)\n\tpos = heap.length-1\n\twhile pos != 0\n\t\tparent_pos = (pos-1)/2\n\t\tbreak if (heap[parent_pos] <=> x) >= 0\n\t\theap[parent_pos], heap[pos] = heap[pos], heap[parent_pos]\n\t\tpos = parent_pos\n\tend\nend\ndef remove(heap)\n\tx = heap.pop\n\treturn if heap.length == 0\n\theap[0] = x\n\tpos = 0\n\twhile true\n\t\tchild_pos = 2*pos+1\n\t\tbreak if child_pos >= heap.length\n\t\tif child_pos+1 < heap.length and (heap[child_pos+1] <=> heap[child_pos]) == -1\n\t\t\tchild_pos = child_pos+1\n\t\tend\n\t\tbreak if (heap[pos] <=> heap[child_pos]) >= 0\n\t\theap[child_pos], heap[pos] = heap[pos], heap[child_pos]\n\tend\nend\n\nn, k = STDIN.readline.split.map {|s| s.to_i }\nvalues = STDIN.readline.split.map {|s| s.to_i }\nsum = values[0..k-1].inject(:+)\nsums = [sum]\n(k..n-1).each do |i|\n\tsum += values[i] - values[i-k]\n\tsums.push(sum)\nend\nheap = []\n(k..n-k).each {|i| add(heap, [sums[i], -i]) }\nbest_score = 0\nbest_indices = nil\n(0..n-2*k).each do |i|\n\tvalue, j = heap[0]\n\tscore = sums[i] + value\n\tif score > best_score\n\t\tbest_indices = [i+1, -j+1]\n\t\tbest_score = score\n\tend\n\tremove(heap)\nend\nputs best_indices.join(' ')\n"}], "src_uid": "74095fe82bd22257eeb97e1caf586499"} {"nl": {"description": "Two players play a game.Initially there are $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ written on the board. Each turn a player selects one number and erases it from the board. This continues until there is only one number left on the board, i. e. $$$n - 1$$$ turns are made. The first player makes the first move, then players alternate turns.The first player wants to minimize the last number that would be left on the board, while the second player wants to maximize it.You want to know what number will be left on the board after $$$n - 1$$$ turns if both players make optimal moves.", "input_spec": "The first line contains one integer $$$n$$$ ($$$1 \\le n \\le 1000$$$) — the number of numbers on the board. The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le 10^6$$$).", "output_spec": "Print one number that will be left on the board.", "sample_inputs": ["3\n2 1 3", "3\n2 2 2"], "sample_outputs": ["2", "2"], "notes": "NoteIn the first sample, the first player erases $$$3$$$ and the second erases $$$1$$$. $$$2$$$ is left on the board.In the second sample, $$$2$$$ is left on the board regardless of the actions of the players."}, "positive_code": [{"source_code": "# cook your code here\n\nn = gets.to_i \n\narr = gets.split.map { |x| x.to_i } \n\narr = arr.sort! \n\nputs arr[(n-1)/2]"}, {"source_code": "gets;a=gets.split.map(&:to_i).sort;p a[~-a.size/2]"}, {"source_code": "n = gets.to_i\nas = gets.split.map(&:to_i)\nputs as.sort[(n.to_f/2).ceil-1]"}, {"source_code": "n = gets.strip.to_i\na = gets.strip.split.map(&:to_i).sort\nx = n / 2\nx -= 1 if n.even?\nputs a[x]"}, {"source_code": "# -*- coding: utf-8 -*-\n\nn = gets.to_i\nar = gets.split.map(&:to_i)\nar.sort!\nif n.odd?\n puts ar[n/2]\nelse\n puts ar[n/2 - 1]\nend"}, {"source_code": "x = gets.chomp.to_i;\na = gets.strip.split(' ').map(&:to_i);\n\na = a.sort;\nfor i in 0 ... (x - 1) / 2\t \n\ta[i] = 0;\n\ta[a.length - i - 1] = 0;\nend\n\na.each do |num| \n\tif num != 0\n\t\tputs num;\n\t\texit;\n\tend\n\nend"}, {"source_code": "r = -> { gets.split.map &:to_i }\n\nn = r[].first\n\narr = r[].sort\n\np arr[(n - 1) / 2]\n"}, {"source_code": "n = gets.to_i\nputs gets.split.map(&:to_i).sort[(n - 1) / 2]"}, {"source_code": "n = gets.to_i\nputs gets.split.map(&:to_i).sort[(n - 1) / 2]\n"}], "negative_code": [{"source_code": "gets;a=gets.split.map(&:to_i).sort;p a[-~a.size/2]"}, {"source_code": "gets;a=gets.split.map(&:to_i).sort;p a[a.size/2]"}, {"source_code": "n = gets.strip.to_i\na = gets.strip.split.map(&:to_i).sort\nputs a[n / 2]"}, {"source_code": "x = gets.chomp.to_i;\na = gets.strip.split(' ').map(&:to_i);\n\nfor i in 0 ... x / 2\t \n\ta[i] = 0;\n\ta[a.length - i - 1] = 0;\nend\n\na.each do |num| \n\tif num.nonzero?\n\t\tputs num;\n\t\texit;\n\tend\n\nend"}, {"source_code": "x = gets.chomp.to_i;\na = gets.strip.split(' ').map(&:to_i);\n\na = a.sort;\nfor i in 0 ... x / 2\t \n\ta[i] = 0;\n\ta[a.length - i - 1] = 0;\nend\n\na.each do |num| \n\tif num != 0\n\t\tputs num;\n\t\texit;\n\tend\n\nend"}], "src_uid": "f93a8bd64a405233342f84542fce314d"} {"nl": {"description": " \"You must lift the dam. With a lever. I will give it to you.You must block the canal. With a rock. I will not give the rock to you.\" Danik urgently needs rock and lever! Obviously, the easiest way to get these things is to ask Hermit Lizard for them.Hermit Lizard agreed to give Danik the lever. But to get a stone, Danik needs to solve the following task.You are given a positive integer $$$n$$$, and an array $$$a$$$ of positive integers. The task is to calculate the number of such pairs $$$(i,j)$$$ that $$$i<j$$$ and $$$a_i$$$ $$$\\&$$$ $$$a_j \\ge a_i \\oplus a_j$$$, where $$$\\&$$$ denotes the bitwise AND operation, and $$$\\oplus$$$ denotes the bitwise XOR operation.Danik has solved this task. But can you solve it?", "input_spec": "Each test contains multiple test cases. The first line contains one positive integer $$$t$$$ ($$$1 \\le t \\le 10$$$) denoting the number of test cases. Description of the test cases follows. The first line of each test case contains one positive integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$) — length of the array. The second line contains $$$n$$$ positive integers $$$a_i$$$ ($$$1 \\le a_i \\le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.", "output_spec": "For every test case print one non-negative integer — the answer to the problem.", "sample_inputs": ["5\n5\n1 4 3 7 10\n3\n1 1 1\n4\n6 2 5 3\n2\n2 4\n1\n1"], "sample_outputs": ["1\n3\n2\n0\n0"], "notes": "NoteIn the first test case there is only one pair: $$$(4,7)$$$: for it $$$4$$$ $$$\\&$$$ $$$7 = 4$$$, and $$$4 \\oplus 7 = 3$$$.In the second test case all pairs are good.In the third test case there are two pairs: $$$(6,5)$$$ and $$$(2,3)$$$.In the fourth test case there are no good pairs."}, "positive_code": [{"source_code": "T = gets.to_i\n\nT.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n\n ans = 0\n counter = Hash.new(0)\n\n a.each do |x|\n l = x.to_s(2).size\n ans += counter[l]\n counter[l] += 1\n end\n\n puts ans\nend\n"}, {"source_code": "t = gets.chomp.to_i\nt.times do \n n = gets.chomp.to_i\n a = gets.chomp.split(\" \").map(&:to_i)\n hash = Hash.new(0)\n a.each do |i|\n ii = i.to_s(2).size\n hash[ii] += 1\n end\n count = 0\n hash.each do |i,j|\n if j >= 2\n ans = (j * (j-1)) / 2\n count += ans\n end\n end\n puts count\nend"}], "negative_code": [], "src_uid": "04e2e1ce3f0b4efd2d4dda69748a0a25"} {"nl": {"description": "As Valeric and Valerko were watching one of the last Euro Championship games in a sports bar, they broke a mug. Of course, the guys paid for it but the barman said that he will let them watch football in his bar only if they help his son complete a programming task. The task goes like that.Let's consider a set of functions of the following form: Let's define a sum of n functions y1(x), ..., yn(x) of the given type as function s(x) = y1(x) + ... + yn(x) for any x. It's easy to show that in this case the graph s(x) is a polyline. You are given n functions of the given type, your task is to find the number of angles that do not equal 180 degrees, in the graph s(x), that is the sum of the given functions.Valeric and Valerko really want to watch the next Euro Championship game, so they asked you to help them.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 105) — the number of functions. Each of the following n lines contains two space-separated integer numbers ki, bi ( - 109 ≤ ki, bi ≤ 109) that determine the i-th function.", "output_spec": "Print a single number — the number of angles that do not equal 180 degrees in the graph of the polyline that equals the sum of the given functions.", "sample_inputs": ["1\n1 0", "3\n1 0\n0 2\n-1 1", "3\n-2 -4\n1 7\n-5 1"], "sample_outputs": ["1", "2", "3"], "notes": null}, "positive_code": [{"source_code": "r = []\ngets.to_i.times { |i|\nar = gets.split(' ').map{ |x| x.to_i }\nif ar[0] != 0 then r.push(Rational(-ar[1], ar[0])); end\n}\n\nputs r.uniq.size\n"}, {"source_code": "r = []\ngets.to_i.times { |i|\nar = gets.split(' ').map{ |x| x.to_i }\nif ar[0] != 0 then r.push(Rational(-ar[1], ar[0])); end\n}\n\nputs r.uniq.size\n"}, {"source_code": "r = []\ngets.to_i.times { |i|\nar = gets.split(' ').map{ |x| x.to_i }\nif ar[0] != 0 then r.push(Rational(-ar[1], ar[0])); end\n}\n\nputs r.uniq.size\n"}, {"source_code": "r = []\ngets.to_i.times { |i|\nar = gets.split(' ').map{ |x| x.to_i }\nif ar[0] != 0 then r.push(Rational(-ar[1], ar[0])); end\n}\n\nputs r.uniq.size\n"}, {"source_code": "#!/usr/bin/ruby\nrequire 'set'\nn = $stdin.gets.to_i\ns = Set.new\nn.times do |i|\n k, b = *$stdin.gets.split.map(&:to_i)\n if k != 0\n s.add -b.to_r/k\n end\nend\nputs s.size\n"}, {"source_code": "r = []\ngets.to_i.times { |i|\nar = gets.split(' ').map{ |x| x.to_i }\nif ar[0] != 0 then r.push(Rational(-ar[1], ar[0])); end\n}\n\nputs r.uniq.size\n"}, {"source_code": "r = []\ngets.to_i.times { |i|\nar = gets.split(' ').map{ |x| x.to_i }\nif ar[0] != 0 then r.push(Rational(-ar[1], ar[0])); end\n}\n\nputs r.uniq.size\n"}, {"source_code": "r = []\ngets.to_i.times { |i|\nar = gets.split(' ').map{ |x| x.to_i }\nif ar[0] != 0 then r.push(Rational(-ar[1], ar[0])); end\n}\n\nputs r.uniq.size\n"}, {"source_code": "r = []\ngets.to_i.times { |i|\nar = gets.split(' ').map{ |x| x.to_i }\nif ar[0] != 0 then r.push(Rational(-ar[1], ar[0])); end\n}\n\nputs r.uniq.size\n"}, {"source_code": "r = []\ngets.to_i.times { |i|\nar = gets.split(' ').map{ |x| x.to_i }\nif ar[0] != 0 then r.push(Rational(-ar[1], ar[0])); end\n}\n\nputs r.uniq.size"}], "negative_code": [{"source_code": "#!/usr/bin/ruby\nn = $stdin.gets.to_i\nh = Hash.new { |h, k| h[k] = [] }\nn.times do\n k, b = *$stdin.gets.split.map(&:to_i)\n if k != 0\n h[-b.to_r/k].push k\n end\nend\nans = 0\nh.values.each do |a|\n if a.inject(0, :+) != 0\n ans += 1\n end\nend\nputs ans\n"}, {"source_code": "#!/usr/bin/ruby\nn = $stdin.gets.to_i\nh = Hash.new { |h, k| h[k] = [] }\nn.times do\n k, b = *$stdin.gets.split.map(&:to_i)\n if k != 0\n h[b.to_r/k].push k\n end\nend\nans = 0\nh.values.each do |a|\n x = 0\n y = 0\n a.each do |k|\n x += k\n end\n if x != 0\n ans += 1\n end\nend\nputs ans\n"}], "src_uid": "d436c7a3b7f07c9f026d00bdf677908d"} {"nl": {"description": "Polycarp has n dice d1, d2, ..., dn. The i-th dice shows numbers from 1 to di. Polycarp rolled all the dice and the sum of numbers they showed is A. Agrippina didn't see which dice showed what number, she knows only the sum A and the values d1, d2, ..., dn. However, she finds it enough to make a series of statements of the following type: dice i couldn't show number r. For example, if Polycarp had two six-faced dice and the total sum is A = 11, then Agrippina can state that each of the two dice couldn't show a value less than five (otherwise, the remaining dice must have a value of at least seven, which is impossible).For each dice find the number of values for which it can be guaranteed that the dice couldn't show these values if the sum of the shown values is A.", "input_spec": "The first line contains two integers n, A (1 ≤ n ≤ 2·105, n ≤ A ≤ s) — the number of dice and the sum of shown values where s = d1 + d2 + ... + dn. The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 106), where di is the maximum value that the i-th dice can show.", "output_spec": "Print n integers b1, b2, ..., bn, where bi is the number of values for which it is guaranteed that the i-th dice couldn't show them.", "sample_inputs": ["2 8\n4 4", "1 3\n5", "2 3\n2 3"], "sample_outputs": ["3 3", "4", "0 1"], "notes": "NoteIn the first sample from the statement A equal to 8 could be obtained in the only case when both the first and the second dice show 4. Correspondingly, both dice couldn't show values 1, 2 or 3.In the second sample from the statement A equal to 3 could be obtained when the single dice shows 3. Correspondingly, it couldn't show 1, 2, 4 or 5.In the third sample from the statement A equal to 3 could be obtained when one dice shows 1 and the other dice shows 2. That's why the first dice doesn't have any values it couldn't show and the second dice couldn't show 3."}, "positive_code": [{"source_code": "n, sum = gets.split(' ').map(&:to_i)\nds = gets.split(' ').map(&:to_i)\nmax = ds.reduce(:+)\n\nn.times do |i|\n d = ds[i]\n other_max = max-d\n\n my_min = 1\n if sum > other_max\n my_min = sum-other_max\n end\n my_max = [sum - (n-1), d].min\n\n print ' ' if i != 0\n print (d - [(my_max-my_min+1), 0].max)\nend\nprint \"\\n\"\n"}, {"source_code": "#!/usr/bin/env ruby\n# -*- coding: utf-8 -*-\n\n# c.rb\n\ndef main()\n n, a = gets.chomp.split(\" \").map{|s| next s.to_i }\n d = gets.chomp.split(\" \").map{|s| next s.to_i }\n s = d.inject{|ret, i| next ret + i }\n r = []\n d.each_with_index do |di, i|\n ss = s - di\n least = [1, a - ss].max\n most = [di, a - (n - 1)].min\n cand = most - least + 1\n r << di - cand\n end\n \n puts(r * \" \")\nend\n \nif __FILE__ == $0 then\n main()\nend\n"}, {"source_code": "n,a = gets.split.map(&:to_i)\nd = gets.split.map(&:to_i)\nsum = d.inject(:+)\n\nans = d.map do |di|\n less = [0, a - (sum - di) - 1].max\n more = [0, di - (a - (n - 1))].max\n\n less + more\nend\n\nputs ans.join(' ')\n"}], "negative_code": [], "src_uid": "2c51414eeb430ad06aac53a99ff95eff"} {"nl": {"description": "Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, which is based on Splay trees. Specifically, he is now studying the expose procedure.Unfortunately, Rostislav is unable to understand the definition of this procedure, so he decided to ask programmer Serezha to help him. Serezha agreed to help if Rostislav solves a simple task (and if he doesn't, then why would he need Splay trees anyway?)Given integers l, r and k, you need to print all powers of number k within range from l to r inclusive. However, Rostislav doesn't want to spent time doing this, as he got interested in playing a network game called Agar with Gleb. Help him!", "input_spec": "The first line of the input contains three space-separated integers l, r and k (1 ≤ l ≤ r ≤ 1018, 2 ≤ k ≤ 109).", "output_spec": "Print all powers of number k, that lie within range from l to r in the increasing order. If there are no such numbers, print \"-1\" (without the quotes).", "sample_inputs": ["1 10 2", "2 4 5"], "sample_outputs": ["1 2 4 8", "-1"], "notes": "NoteNote to the first sample: numbers 20 = 1, 21 = 2, 22 = 4, 23 = 8 lie within the specified range. The number 24 = 16 is greater then 10, thus it shouldn't be printed."}, "positive_code": [{"source_code": "l, r, k = gets.split.map(&:to_i)\n\na = 1\n\na *= k while a < l\n\nf = true\n\nwhile a <= r\n print \" \" if not f\n f = false\n print a\n a *= k\nend\n\nif f\n puts \"-1\"\nelsif\n puts\nend"}, {"source_code": "l, r, k = gets.split.map(&:to_i)\nx = 1\ndone = 0\nwhile x<=r\n if x>=l\n print(x,\" \")\n done=1\n end\n x*=k\nend\n\nif done==0\n p -1\nend"}, {"source_code": " require 'scanf'\n\n\tx=scanf(\"%d\")[0]\n y=scanf(\"%d\")[0]\n k=scanf(\"%d\")[0]\n\to=1\n\tch=1\n\twhile o=x&&o<=y\n\t \n\t\tprint o\n print \" \"\n\t\to*=k\n\t\tch=0\n\tend\n\tif ch==1\n\t\tputs -1\n\tend\n\n\n\n"}, {"source_code": "require 'scanf'\n\n\nwhile s = $stdin.gets() do\n l, r, k = s.scanf(\"%d %d %d\")\n n = 1\n f = false\n while n <= r do\n if l <= n && n <= r then\n if f then\n print(\" \") \n end\n f = true\n print(n)\n end\n n *= k;\n end\n if f then\n puts(\"\");\n else\n puts(\"-1\");\n end\nend\n"}, {"source_code": "l,r,k = gets.chomp.split.map(&:to_i)\nans = []\nc = 1\nwhile c <= r\n ans << c if l <= c\n c *= k\nend\n\nputs ans.size == 0 ? -1 : ans.join(\" \")\n"}, {"source_code": "#! ruby\n# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n############################################################\ndef gs() gets.chomp end\ndef gi() gets.to_i end\ndef gss() gs.split end\ndef gis() gss.map(&:to_i) end\ndef nmapf(n,f) n.times.map{ __send__ f } end\ndef ngs(n) nmapf n,:gs end\ndef ngi(n) nmapf n,:gi end\ndef ngss(n) nmapf n,:gss end\ndef ngis(n) nmapf n,:gis end\ndef arr2d(h,w,v=0) h.times.map{[v] * w} end\ndef for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end\ndef nsum(n) n * (n + 1) / 2 end\ndef vcount(d,r=Hash.new(0)) d.inject(r){|r,e| r[e]+=1;r} end\n############################################################\n\nl, r, k = gis\nv = 1\na = []\nloop do\n if v <= r\n a << v if v >= l\n else\n break\n end\n v *= k\nend\n\nif a.empty?\n puts '-1'\nelse\n puts a * \" \"\nend"}, {"source_code": "l,r,k=gets.split.map(&:to_i)\na=[]\n64.times{|i|a.push k**i if k**i>=l&&k**i<=r}\nputs (a.last)? a.join(' '):-1"}, {"source_code": "l, r, k = gets.chomp.split(' ').map(&:to_i)\n\nans = []\n\npower = 1\n\nwhile power <= r do\n if power >= l then\n ans << power\n end\n power *= k\nend\n\nif ans.size == 0 then\n puts -1\nelse\n puts ans.join(' ')\nend\n"}, {"source_code": "a=gets.chomp.split(\" \").map{|i| i.to_i}\n\nans=0\ni=0\nloop do\nif a[2]**i>=a[0] && a[2]**i<=a[1]\nprintf \"#{a[2]**i} \"\nans+=1\nend\ni+=1\nbreak if a[2]**i>a[1]\nend\nif ans==0\nputs \"-1\"\nend\n"}, {"source_code": "l,r,k=gets.split.map(&:to_i)\na=[]\n64.times{|i|a.push k**i if k**i>=l&&k**i<=r}\nputs (a.last)? a.join(' '):-1"}, {"source_code": "l,r,k=gets.split.map(&:to_i)\nans=[]\n64.times do |i|\n\tbreak if k**i>r\n\tans.push k**i if k**i>=l\nend\nans=[-1] if !ans.last\nputs ans.join(' ')"}, {"source_code": "l, r, k = gets.chomp.split.map(&:to_i)\n\nll = (Math.log(l) / Math.log(k)).round\nlr = (Math.log(r) / Math.log(k)).round\nns = (ll..lr).map { |x| k**x }.select { |x| l <= x && x <= r }\n\nif ns.size == 0\n puts -1\nelse\n puts ns.*' '\nend\n"}, {"source_code": "def solve(l, r, k)\n ans = []\n p = 0\n while k**p <= r do\n ans << k**p\n p+= 1\n end\n ret = []\n for i in ans do\n if i >= l && i <= r\n ret << i \n end\n end\n return -1 if ret.length == 0\n return ret.join(' ')\nend\nl, r, k = gets.strip.split(\" \").map(&:to_i)\nputs solve(l, r, k)"}, {"source_code": "\ns = gets.to_s\n\nm = Array.new(s.split(\" \"))\n\nl = m[0].to_i\nr = m[1].to_i\nk = m[2].to_i\n\ns = \"\"\n\nfor i in 0 ... 74\n\tp = k ** i\n\tif l <= p && p <= r\n\t\ts += (p.to_s + \" \")\n\tend\nend\n\nif s == \"\" then puts \"-1\"\nelse puts s end\n"}, {"source_code": "a = gets.chomp.split(\" \").map{|x| x.to_i}\nkek = 0\npurkka = false\na[1].times do |i|\n s = a[2] ** i\n if s <= a[1] and s >= a[0]\n puts s\n purkka = true\n elsif (s > a[1])\n break\n end\nend\nputs \"-1\" if not purkka\n"}, {"source_code": "#!/usr/bin/ruby\n\nl,r,k = gets.strip.split.map {|num| num.to_i}\n\nnum = k\nwhile num < l do\n\tnum *= k\nend\n\nif l == 1\n\tnum = 1\nend\n\nif l <= num and num <= r\n\twhile l <= num and num <= r do\n\t\tprint num\n\t\tprint ' '\n\t\tnum *= k\n\tend\n\tputs ''\nelse\n\tputs -1\nend"}, {"source_code": "numbers = gets.split(\" \")\nfirst = numbers[0].to_i\nlast = numbers[1].to_i\npotencial = numbers[2].to_i\nres = \"\"\nfirst_exponencial = Math.log10(first)/Math.log10(potencial)\nlast_exponencial = Math.log(last)/Math.log10(potencial)\narray = []\nif first_exponencial - first_exponencial.to_i > 0\n\tfirst_exponent = first_exponencial.to_i + 1\nelse\n\tfirst_exponent = first_exponencial.to_i\nend\nfor i in first_exponent.to_i..last_exponencial.to_i\n\tif potencial ** i <= last && potencial**i >= first\n\t\tarray << potencial ** i\n\tend\nend\narray.each do |current|\n\tres << current.to_s + \" \"\nend\nif array.size == 0\n\tputs \"-1\"\nelse\n\tputs res\nend"}, {"source_code": "l, r, k = gets.chomp.split(\" \").map(&:to_i)\n\nfrom = Math.log(l, k)\nto = Math.log(r, k)\n\na = ((from.floor)..(to.ceil)).to_a.map{|i| k**i}.keep_if{|i| i >= l and i <= r}\nif a.length == 0 then\n puts -1\nelse\n puts a.join(\" \")\nend"}, {"source_code": "input = gets.chomp\ni = input.split(\" \")\n\nl = i[0].to_i\nr = i[1].to_i\nk = i[2].to_i\n\naux = 1\nflag = false\n\nwhile aux<=r\n\tif aux>=l\n\t\tflag = true\n\t\tprint \"#{aux} \"\n\tend\n\taux *= k\nend\n\nif !flag\n\tprint \"-1 \"\nend\n\nprint \"\\n\"\n"}, {"source_code": "l,r,k = gets.split.map(&:to_i)\n\nx=1\nt=0\n\n60.times{\n if (x>=l && x<=r)\n print x , \" \"\n t=1\n end\n\n x=x*k\n if (x>r) then break; end\n}\n\nif (t==0)\n print -1\nend"}, {"source_code": "class Solver\n\tdef main\n\t\tl, r, k = gets.split.map { |x| x.to_i }\n\t\tans = []\n\t\t64.times do |i|\n\t\t\tx = k ** i\n\t\t\tans << x if x.between? l, r\n\t\tend\n\t\tif ans.empty?\n\t\t\tputs -1\n\t\telse\n\t\t\tputs ans.join ' '\n\t\tend\n\tend\nend\n\nSolver.new.main\n"}], "negative_code": [{"source_code": "a=gets.chomp.split(\" \")\nnum=a[2].to_i\nmin=(Math.log(a[0].to_i,num)).ceil\nmax=(Math.log(a[1].to_i,num)).floor\nif min-max==1 \nputs \"-1\"\nelse\nmin.upto(max) do |i|\nprintf \"#{num**i} \"\nend\nend"}, {"source_code": "def solve(l, r, k)\n if k < l || k > r \n return \"-1\"\n end\n printed = false\n p = 0\n ans = \"\"\n while k**p <= r do\n if k**p >= l\n ans << (k**p).to_s << \" \"\n printed = true\n end\n p += 1\n end\n return \"-1\" if printed == false\n return ans.strip\nend\nl, r, k = gets.strip.split(\" \").map(&:to_i)\nputs solve(l, r, k)"}, {"source_code": "def solve(l, r, k)\n if k < l || k > r \n return \"-1\"\n end\n printed = false\n p = 0\n ans = \"\"\n while k**p <= r do\n ans << k**p << \" \"\n p += 1\n printed = true\n end\n return \"-1\" if printed == false\n return ans.strip\nend\nl, r, k = gets.strip.split(\" \").map(&:to_i)\nputs solve(l, r, k)"}, {"source_code": "def solve(l, r, k)\n if k < l || k > r \n return \"-1\"\n end\n printed = false\n p = 0\n ans = \"\"\n while k**p <= r do\n ans << (k**p).to_s << \" \"\n p += 1\n printed = true\n end\n return \"-1\" if printed == false\n return ans.strip\nend\nl, r, k = gets.strip.split(\" \").map(&:to_i)\nputs solve(l, r, k)"}, {"source_code": "numbers = gets.split(\" \")\nfirst = numbers[0].to_i\nlast = numbers[1].to_i\npotencial = numbers[2].to_i\nres = \"\"\nfirst_exponencial = Math.log10(first)/Math.log10(potencial)\nlast_exponencial = Math.log(last)/Math.log10(potencial)\narray = []\nif first_exponencial - first_exponencial.to_i > 0\n\tfirst_exponent = first_exponencial.to_i + 1.0\nelse\n\tfirst_exponent = first_exponencial.to_i\nend\nfor i in first_exponent.to_i..last_exponencial.to_i\n\tif potencial ** i <= last + 1\n\t\tarray << potencial ** i\n\tend\nend\narray.each do |current|\n\tres << current.to_s + \" \"\nend\nif array.size == 0\n\tputs \"-1\"\nelse\n\tputs res\nend"}, {"source_code": "numbers = gets.split(\" \")\nfirst = numbers[0].to_i\nlast = numbers[1].to_i\npotencial = numbers[2].to_i\nres = \"\"\nfirst_exponencial = Math.log(first, potencial)\nlast_exponencial = Math.log(last, potencial)\narray = []\nif first_exponencial - first_exponencial.to_i > 0\n\tfirst_exponent = first_exponencial.to_i + 1\nelse\n\tfirst_exponent = first_exponencial.to_i\nend\nfor i in first_exponent.to_i..last_exponencial.to_i\n\tif potencial ** i <= last\n\t\tarray << potencial ** i\n\tend\nend\narray.each do |current|\n\tres << current.to_s + \" \"\nend\nif array.size == 0\n\tputs \"-1\"\nelse\n\tputs res\nend"}, {"source_code": "numbers = gets.split(\" \")\nfirst = numbers[0].to_i\nlast = numbers[1].to_i\npotencial = numbers[2].to_i\nres = \"\"\nfirst_exponencial = Math.log(first, potencial)\nlast_exponencial = Math.log(last, potencial)\narray = []\nif first_exponencial - first_exponencial.to_i > 0\n\tfirst_exponent = first_exponencial.to_i + 1\nelse\n\tfirst_exponent = first_exponencial.to_i\nend\nfor i in first_exponent.to_i..last_exponencial.to_i\n\tif potencial ** i < last\n\t\tarray << potencial ** i\n\tend\nend\narray.each do |current|\n\tres << current.to_s + \" \"\nend\nif array.size == 0\n\tputs \"-1\"\nelse\n\tputs res\nend"}, {"source_code": "numbers = gets.split(\" \")\nfirst = numbers[0].to_i\nlast = numbers[1].to_i\npotencial = numbers[2].to_i\nres = \"\"\nfirst_exponencial = Math.log(first, potencial)\nlast_exponencial = Math.log(last, potencial)\narray = []\nif first_exponencial - first_exponencial.to_i > 0\n\tfirst_exponent = first_exponencial.to_i + 1\nelse\n\tfirst_exponent = first_exponencial.to_i\nend\nfor i in first_exponent.to_i..last_exponencial.to_i\n\tarray << potencial ** i\nend\narray.each do |current|\n\tres << current.to_s + \" \"\nend\nif array.size == 0\n\tputs \"-1\"\nelse\n\tputs res\nend"}, {"source_code": "numbers = gets.split(\" \")\nfirst = numbers[0].to_i\nlast = numbers[1].to_i\npotencial = numbers[2].to_i\nres = \"\"\nfirst_exponencial = Math.log10(first)/Math.log10(potencial)\nlast_exponencial = Math.log(last)/Math.log10(potencial)\narray = []\nif first_exponencial - first_exponencial.to_i > 0\n\tfirst_exponent = first_exponencial.to_i + 1\nelse\n\tfirst_exponent = first_exponencial.to_i\nend\nfor i in first_exponent.to_i..last_exponencial.to_i\n\tif potencial ** i <= last\n\t\tarray << potencial ** i\n\tend\nend\narray.each do |current|\n\tres << current.to_s + \" \"\nend\nif array.size == 0\n\tputs \"-1\"\nelse\n\tputs res\nend"}, {"source_code": "l, r, k = gets.chomp.split(\" \").map(&:to_i)\n\nfrom = Math.log(l, k)\nto = Math.log(r, k)\n\nif to.floor - from.ceil <= 0 then\n puts -1\nelse\n puts ((from.ceil)..(to.floor)).to_a.map{|i| k**i}.join(\" \")\nend"}], "src_uid": "8fcec28fb4d165eb58f829c03e6b31d1"} {"nl": {"description": "You are given n points on the straight line — the positions (x-coordinates) of the cities and m points on the same line — the positions (x-coordinates) of the cellular towers. All towers work in the same way — they provide cellular network for all cities, which are located at the distance which is no more than r from this tower.Your task is to find minimal r that each city has been provided by cellular network, i.e. for each city there is at least one cellular tower at the distance which is no more than r.If r = 0 then a tower provides cellular network only for the point where it is located. One tower can provide cellular network for any number of cities, but all these cities must be at the distance which is no more than r from this tower.", "input_spec": "The first line contains two positive integers n and m (1 ≤ n, m ≤ 105) — the number of cities and the number of cellular towers. The second line contains a sequence of n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — the coordinates of cities. It is allowed that there are any number of cities in the same point. All coordinates ai are given in non-decreasing order. The third line contains a sequence of m integers b1, b2, ..., bm ( - 109 ≤ bj ≤ 109) — the coordinates of cellular towers. It is allowed that there are any number of towers in the same point. All coordinates bj are given in non-decreasing order.", "output_spec": "Print minimal r so that each city will be covered by cellular network.", "sample_inputs": ["3 2\n-2 2 4\n-3 0", "5 3\n1 5 10 14 17\n4 11 15"], "sample_outputs": ["4", "3"], "notes": null}, "positive_code": [{"source_code": "n, m = gets.chomp.split.map(&:to_i)\nas = gets.chomp.split.map(&:to_i)\nbs = gets.chomp.split.map(&:to_i)\n\nas.uniq!\nbs.uniq!\nn = as.size\nm = bs.size\n\nans = 0\nl = 0\nr = 0\nfor i in 0...n\n while r < m - 1 && bs[r] <= as[i]\n r += 1\n end\n l = [r - 1, 0].max\n ans = [ans, [(as[i] - bs[l]).abs, (as[i] - bs[r]).abs].min].max\nend\n\nputs ans\n"}], "negative_code": [], "src_uid": "9fd8e75cb441dc809b1b2c48c4012c76"} {"nl": {"description": "Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.A position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.", "input_spec": "The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan. The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.", "output_spec": "Print one integer — the number of good positions for placing the spotlight.", "sample_inputs": ["2 4\n0 1 0 0\n1 0 1 0", "4 4\n0 0 0 0\n1 0 0 1\n0 1 1 0\n0 1 0 0"], "sample_outputs": ["9", "20"], "notes": "NoteIn the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example."}, "positive_code": [{"source_code": "def f(m)\n c=0\n m.each{|e|\n s=e.count 1\n l=e.size\n if s==1\n c+=l-1\n elsif s>1\n x=e.index 1\n y=e.reverse.index 1\n c+=x+2*(l-(x+y)-s)+y\n end\n }\n c\nend\ngets\nm=$<.map{|e|e.chomp.split.map(&:to_i)}\np f(m)+f(m.transpose)"}, {"source_code": "gets\nm=$<.map{|e|e.chomp.split.map(&:to_i)}\np [m,m.transpose].reduce(0){|r,m|\n c=0\n m.each{|e|\n s=e.count 1\n l=e.size\n if s==1\n c+=l-1\n elsif s>1\n x=e.index 1\n y=e.reverse.index 1\n c+=x+2*(l-(x+y)-s)+y\n end\n }\n r+c\n}"}], "negative_code": [{"source_code": "H,W=gets.split.map(&:to_i)\nm=$<.map{|e|e.chomp.tr(' ','').chars}\ncol=m.map{|e|e.join.to_i(2)}.reduce(:&)\nrow=m.transpose.map{|e|e.join.to_i(2)}.reduce(:&)\nr=0\nH.times{|h|W.times{|w|\n r+=1 if m[h][w]=='0' and col[w]>0||row[h]>0\n}}\np r"}], "src_uid": "c3a7d82f6c3cf8678a1c7c521e0a5f51"} {"nl": {"description": "You are given a set of n vectors on a plane. For each vector you are allowed to multiply any of its coordinates by -1. Thus, each vector vi = (xi, yi) can be transformed into one of the following four vectors: vi1 = (xi, yi), vi2 = ( - xi, yi), vi3 = (xi,  - yi), vi4 = ( - xi,  - yi). You should find two vectors from the set and determine which of their coordinates should be multiplied by -1 so that the absolute value of the sum of the resulting vectors was minimally possible. More formally, you should choose two vectors vi, vj (1 ≤ i, j ≤ n, i ≠ j) and two numbers k1, k2 (1 ≤ k1, k2 ≤ 4), so that the value of the expression |vik1 + vjk2| were minimum.", "input_spec": "The first line contains a single integer n (2 ≤ n ≤ 105). Then n lines contain vectors as pairs of integers \"xi yi\" ( - 10000 ≤ xi, yi ≤ 10000), one pair per line.", "output_spec": "Print on the first line four space-separated numbers \"i k1 j k2\" — the answer to the problem. If there are several variants the absolute value of whose sums is minimum, you can print any of them. ", "sample_inputs": ["5\n-7 -3\n9 0\n-8 6\n7 -8\n4 -5", "5\n3 2\n-4 7\n-6 0\n-8 4\n5 1"], "sample_outputs": ["3 2 4 2", "3 4 5 4"], "notes": "NoteA sum of two vectors v = (xv, yv) and u = (xu, yu) is vector s = v + u = (xv + xu, yv + yu).An absolute value of vector v = (x, y) is number . In the second sample there are several valid answers, such as:(3 1 4 2), (3 1 4 4), (3 4 4 1), (3 4 4 3), (4 1 3 2), (4 1 3 4), (4 2 3 1)."}, "positive_code": [{"source_code": "n, cd = nil\n\nFile.open(\"input.txt\") do |f|\n n = f.gets.to_i\n cd = []\n n.times do |i|\n x, y = f.gets.split.map(&:to_i)\n cd << [x.abs, y.abs, i+1, x, y]\n end\nend\n\n#cd = cd.sort_by{|a,b,c,u,v| [u, v]}\ncd = cd.sort\n\nmn = 1e20\nret = []\ns = 0\nlp = 0\nn.times do |i|\n #idx, x0, y0, u0, v0 = cd[i]\n u0, v0, idx, x0, y0 = cd[i]\n break if mn == 0\n s += 1 while s < n && (cd[s][0]-u0)**2 > mn\n break if s >= n\n\n (s..n-1).each do |j|\n lp += 1\n #jdx, x1, y1, u1, v1 = cd[j]\n u1, v1, jdx, x1, y1 = cd[j]\n next if idx == jdx\n\n break if (u1-u0)**2 > mn \n\n len = (u1-u0)**2 + (v1-v0)**2\n\n if len < mn\n d0 = (x0<0 ? 2 : 1) + (y0<0 ? 2 : 0)\n d1 = (x1>0 ? 2 : 1) + (y1>0 ? 2 : 0)\n #p [idx, [u0,v0], jdx, [u1,v1], len]\n\n mn = len\n ret = [idx, d0, jdx, d1]\n end\n\n end\nend\n\n#p lp\n#p ret\n\nFile.open(\"output.txt\", \"w\") do |f|\n f.puts ret*\" \"\nend\n"}], "negative_code": [], "src_uid": "41bc5d7e091a92af7160056ca6030588"} {"nl": {"description": "Dreamoon is a big fan of the Codeforces contests.One day, he claimed that he will collect all the places from $$$1$$$ to $$$54$$$ after two more rated contests. It's amazing!Based on this, you come up with the following problem:There is a person who participated in $$$n$$$ Codeforces rounds. His place in the first round is $$$a_1$$$, his place in the second round is $$$a_2$$$, ..., his place in the $$$n$$$-th round is $$$a_n$$$.You are given a positive non-zero integer $$$x$$$.Please, find the largest $$$v$$$ such that this person can collect all the places from $$$1$$$ to $$$v$$$ after $$$x$$$ more rated contests.In other words, you need to find the largest $$$v$$$, such that it is possible, that after $$$x$$$ more rated contests, for each $$$1 \\leq i \\leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.For example, if $$$n=6$$$, $$$x=2$$$ and $$$a=[3,1,1,5,7,10]$$$ then answer is $$$v=5$$$, because if on the next two contest he will take places $$$2$$$ and $$$4$$$, then he will collect all places from $$$1$$$ to $$$5$$$, so it is possible to get $$$v=5$$$.", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\leq t \\leq 5$$$) denoting the number of test cases in the input. Each test case contains two lines. The first line contains two integers $$$n, x$$$ ($$$1 \\leq n, x \\leq 100$$$). The second line contains $$$n$$$ positive non-zero integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\leq a_i \\leq 100$$$).", "output_spec": "For each test case print one line containing the largest $$$v$$$, such that it is possible that after $$$x$$$ other contests, for each $$$1 \\leq i \\leq v$$$, there will exist a contest where this person took the $$$i$$$-th place.", "sample_inputs": ["5\n6 2\n3 1 1 5 7 10\n1 100\n100\n11 1\n1 1 1 1 1 1 1 1 1 1 1\n1 1\n1\n4 57\n80 60 40 20"], "sample_outputs": ["5\n101\n2\n2\n60"], "notes": "NoteThe first test case is described in the statement.In the second test case, the person has one hundred future contests, so he can take place $$$1,2,\\ldots,99$$$ and place $$$101$$$ on them in some order, to collect places $$$1,2,\\ldots,101$$$."}, "positive_code": [{"source_code": "t = gets.chomp.to_i\nwhile t>0 do\n tmp = gets.chomp.split(\" \")\n n = tmp[0].to_i\n k = tmp[1].to_i\n a = gets.chomp.split(\" \")\n cnt = Array.new(205,0)\n for i in 0..n-1\n cnt[a[i].to_i] += 1\n end\n ans = 1\n while k>=0 do\n if cnt[ans]==0 then\n k -= 1\n end\n ans += 1\n end\n puts ans-2\n t -= 1\nend"}, {"source_code": "t = gets.to_i\nt.times do\n n, x = gets.split.map(&:to_i)\n a = gets.split.map(&:to_i).sort.uniq\n v = 0\n a.each do |aa|\n if aa - (v + 1) <= x\n x -= aa - (v + 1)\n v = aa\n else\n break\n end\n end\n v += x\n puts v\nend\n"}], "negative_code": [{"source_code": "t = gets.chomp.to_i\nwhile t>0 do\n tmp = gets.chomp.split(\" \")\n n = tmp[0].to_i\n k = tmp[1].to_i\n a = gets.chomp.split(\" \")\n cnt = Array.new(205,0)\n for i in 0..n-1\n cnt[a[i].to_i] += 1\n end\n ans = 1\n while k>=0 do\n if cnt[ans]==0 then\n k -= 1\n end\n ans += 1\n end\n puts ans-1\n t -= 1\nend"}], "src_uid": "e5fac4a1f3a724234990fe758debc33f"} {"nl": {"description": "Vasya is going to the Olympics in the city Ntown by train. The boy wants to read the textbook to prepare for the Olympics. He counted that he needed k hours for this. He also found that the light in the train changes every hour. The light is measured on a scale from 0 to 100, where 0 is very dark, and 100 is very light.Vasya has a train lighting schedule for all n hours of the trip — n numbers from 0 to 100 each (the light level in the first hour, the second hour and so on). During each of those hours he will either read the whole time, or not read at all. He wants to choose k hours to read a book, not necessarily consecutive, so that the minimum level of light among the selected hours were maximum. Vasya is very excited before the upcoming contest, help him choose reading hours.", "input_spec": "The first input line contains two integers n and k (1 ≤ n ≤ 1000, 1 ≤ k ≤ n) — the number of hours on the train and the number of hours to read, correspondingly. The second line contains n space-separated integers ai (0 ≤ ai ≤ 100), ai is the light level at the i-th hour.", "output_spec": "In the first output line print the minimum light level Vasya will read at. In the second line print k distinct space-separated integers b1, b2, ..., bk, — the indexes of hours Vasya will read at (1 ≤ bi ≤ n). The hours are indexed starting from 1. If there are multiple optimal solutions, print any of them. Print the numbers bi in an arbitrary order.", "sample_inputs": ["5 3\n20 10 30 40 10", "6 5\n90 20 35 40 60 100"], "sample_outputs": ["20\n1 3 4", "35\n1 3 4 5 6"], "notes": "NoteIn the first sample Vasya should read at the first hour (light 20), third hour (light 30) and at the fourth hour (light 40). The minimum light Vasya will have to read at is 20."}, "positive_code": [{"source_code": "if ENV['USER'] != 'watashi'\n $stdin = open('input.txt')\n $> = open('output.txt', 'w')\nend\n\nI = ->{gets.split.map(&:to_i)}\nn, m = I[]\na = I[].zip([*1..n]).sort[n - m, m]\np a[0][0]\nputs a.map{|_| _[1]} * ' '\n"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\n(n,k),a=gets.split.map(&:to_i),gets.split.map(&:to_i)\nz=p a.sort[-k]\nb=[]\nn.times{|i|\n\tb<=z\n}\nputs b.sort[0...k]*' '\n"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt', 'w')\na,b=Array.new,Array.new\nn,a=gets.split.map(&:to_i)[1],gets.split.map(&:to_i)\nputs a.sort[-n]\nn.times do\nb< y[0]}\na = a.drop(n-k)\noutput.puts a[0][0]\n\na.sort! {|x, y| x[1] <=> y[1]}\na.each {|x| output.print x[1], \" \"}\n\ninput.close\noutput.close"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\n(n,k),a=gets.split.map(&:to_i),gets.split.map(&:to_i)\nz=p a.sort[-k]\nb=[]\nn.times{|i|\n\tb<=z\n}\nputs b.sort[0...k]*' '\n"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\n(n,k),a=gets.split.map(&:to_i),gets.split.map(&:to_i)\nz=p a.sort[-k]\nb=[]\nn.times{|i|\n\tb<=z\n}\nputs b.sort[0...k]*' '"}, {"source_code": "$stdin=open(\"input.txt\")\n$stdout=open(\"output.txt\",\"w\")\ndef search(a,b,n)\n\tn.times do |i|\n\t\tif a[i]==b\n\t\t\ta[i]=101\n\t\t\treturn i+1\n\t\tend\n\tend\nend\nn,k=gets.chomp.split.map(&:to_i)\na=gets.chomp.split.map(&:to_i)\nb=a.sort\nputs b[n-k]\nfor i in (n-k)...n\n\tprint(\"#{search(a,b[i],n)} \")\nend\nputs"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\nn, m = gets.split.map{|i| i.to_i}\na = gets.split.map{|i| i.to_i}\np mins = a.sort[-m]\nb = []\nn.times do |i|\n b << i + 1 if a[i] >= mins\nend\nputs b.sort[0...m]*' '"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\n(n,k),a=gets.split.map(&:to_i),gets.split.map(&:to_i)\nz=p a.sort[-k]\nb=[]\nn.times{|i|\n\tb<=z\n}\nputs b.sort[0...k]*' '\n"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\n(n,k),a=gets.split.map(&:to_i),gets.split.map(&:to_i)\nz=p a.sort[-k]\nb=[]\nn.times{|i|\n\tb<=z\n}\nputs b.sort[0...k]*' '\n"}, {"source_code": "n = 0\nk = 0\narr = []\nopen(\"input.txt\") {|f|\n n, k = f.gets.split.map(&:to_i)\n arr = f.gets.to_s.chomp.split.map(&:to_i)\n}\nFile.open(\"output.txt\", \"w\"){|f|\n arr2 = []\n i = 0\n arr.each do |a|\n i += 1\n arr2.push([i, a])\n end\n arr2.sort! {|a,b| a[1] <=> b[1] }\n f.write \"#{arr2[n-k][1]}\\n\"\n k.times do |i|\n f.write \"#{arr2[n-k+i][0]}\"\n if i != k-1\n f.write \" \"\n else\n f.write \"\\n\" if i == k-1\n end\n end\n}"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\n(n,k),a=gets.split.map(&:to_i),gets.split.map(&:to_i)\nz=p a.sort[-k]\nb=[]\nn.times{|i|\n b<=z\n}\nputs b.sort[0...k]*' '"}, {"source_code": "n,k=0\na=[]\nFile.open(\"input.txt\") { |f|\n n,k=f.gets.split.map(&:to_i)\n a=f.gets.split.map(&:to_i)\n}\nb=[]\na.each_with_index do |v,i|\n b.push([-v,i+1])\nend\nb.sort!\nFile.open(\"output.txt\", \"w\") { |f|\n f.puts -b[k-1][0]\n for i in 0..k-1\n f.print \"#{b[i][1]} \"\n end\n}\n"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\n(n,k),a=gets.split.map(&:to_i),gets.split.map(&:to_i)\nz=p a.sort[-k]\nb=[]\nn.times{|i|\n\tb<=z\n}\nputs b.sort[0...k]*' '"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\n(n,k),a=gets.split.map(&:to_i),gets.split.map(&:to_i)\nz=p a.sort[-k]\nb=[]\nn.times{|i|\n\tb<=z\n}\nputs b.sort[0...k]*' '\n"}, {"source_code": "line = File.read(\"input.txt\")\n\ninputs = line.split(\"\\n\")\n\nn,k = inputs[0].split(\" \").map(&:to_i)\narr = inputs[1].split(\" \").map(&:to_i)\nlist = arr.each_with_index.map { |a,i| [a,i] }\n\nlist.sort!\n# puts \"#{list.min}\"\nans = list[((-k)..-1)]\n\nFile.open(\"output.txt\", 'w') { |file| \n\tfile.puts \"#{ans.min[0]}\"\n\tfile.puts ans.map { |e| e[1]+1 }.join(\" \")\n}\n\n\n"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\n(n,k),a=gets.split.map(&:to_i),gets.split.map(&:to_i)\nz=p a.sort[-k]\nb=[]\nn.times{|i|\n\tb<=z\n}\nputs b.sort[0...k]*' '\n"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\n(n,k),a=gets.split.map(&:to_i),gets.split.map(&:to_i)\nz=p a.sort[-k]\nb=[]\nn.times{|i|\n\tb<=z\n}\nputs b.sort[0...k]*' '\n"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\n(n,k),a=gets.split.map(&:to_i),gets.split.map(&:to_i)\nz=p a.sort[-k]\nb=[]\nn.times{|i|\n\tb<=z\n}\nputs b.sort[0...k]*' '\n"}, {"source_code": "File.open(\"input.txt\", \"r\") do |input|\n\tFile.open(\"output.txt\", \"w\") do |output|\n\t\tn, k = input.readline.split.map(&:to_i)\n\t\toutput << input.readline.split.map(&:to_i).zip((1..n).to_a).sort {|x, y| y.first <=> x.first}.take(k).reduce([101, []]) { |s, (x, y)| \n\t\t\t[[s.first, x].min, s.last + [y]] \n\t\t}.join(' ')\n\tend\nend"}], "negative_code": [{"source_code": "input = File.open(\"input.txt\", \"r\")\noutput = File.open(\"output.txt\", \"w\")\n\nn, k = input.gets.split.map(&:to_i)\nrin = input.gets.split\n\na = Array.new\nfor i in 0...n\n a << [rin[i].to_i, i+1]\nend\n\na.sort! {|x, y| x[0] <=> y[0]}\na = a.drop(n-k)\nputs a[0][0]\n\na.sort! {|x, y| x[1] <=> y[1]}\na.each {|x| print x[1], \" \"}\n\ninput.close\noutput.close"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\nn, m = gets.split.map{|i| i.to_i}\na = gets.split.map{|i| i.to_i}\np mins = a.sort[-m]\nb = []\nn.times do |i|\n b << i + 1 if a[i] >= mins\nend\nputs b.join \" \""}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\nn, m = gets.split.map{|i| i.to_i}\na = gets.split.map{|i| i.to_i}\np a.sort\np mins = a.sort[-m]\nb = []\nn.times do |i|\n b << i + 1 if a[i] >= mins\nend\nputs b.join \" \""}, {"source_code": "n,k=0\na=[]\nFile.open(\"input.txt\") { |f|\n n,k=f.gets.split.map(&:to_i)\n a=f.gets.split.map(&:to_i)\n}\nb=[]\na.each_with_index do |v,i|\n b.push([-v,i+1])\nend\nb.sort!\nFile.open(\"output.txt\", \"w\") { |f|\n for i in 0..k-1\n f.print \"#{b[i][1]} \"\n end\n}\n"}], "src_uid": "a585045a9a6f62bfe1c0618c2ee02f48"} {"nl": {"description": "Vasya and Petya are playing a simple game. Vasya thought of number x between 1 and n, and Petya tries to guess the number.Petya can ask questions like: \"Is the unknown number divisible by number y?\".The game is played by the following rules: first Petya asks all the questions that interest him (also, he can ask no questions), and then Vasya responds to each question with a 'yes' or a 'no'. After receiving all the answers Petya should determine the number that Vasya thought of.Unfortunately, Petya is not familiar with the number theory. Help him find the minimum number of questions he should ask to make a guaranteed guess of Vasya's number, and the numbers yi, he should ask the questions about.", "input_spec": "A single line contains number n (1 ≤ n ≤ 103).", "output_spec": "Print the length of the sequence of questions k (0 ≤ k ≤ n), followed by k numbers — the questions yi (1 ≤ yi ≤ n). If there are several correct sequences of questions of the minimum length, you are allowed to print any of them.", "sample_inputs": ["4", "6"], "sample_outputs": ["3\n2 4 3", "4\n2 4 3 5"], "notes": "NoteThe sequence from the answer to the first sample test is actually correct.If the unknown number is not divisible by one of the sequence numbers, it is equal to 1.If the unknown number is divisible by 4, it is 4.If the unknown number is divisible by 3, then the unknown number is 3.Otherwise, it is equal to 2. Therefore, the sequence of questions allows you to guess the unknown number. It can be shown that there is no correct sequence of questions of length 2 or shorter."}, "positive_code": [{"source_code": "n = gets.chomp.to_i\n\nps = [2]\nfor i in 3..1000\n f = true\n for j in 0..ps.size-1\n if i % ps[j] == 0\n f = false\n break\n end\n end\n ps << i if f\nend\n\nys = []\nps.each do |p|\n q = 1\n while p ** q <= n\n ys << p**q\n q += 1\n end\nend\n\nputs ys.size\nputs ys.*' '\n"}, {"source_code": "def is_prime(x)\n\tm = (x**0.5).to_i\n\tfor i in (2..m)\n\t\tif x % i == 0\n\t\t\treturn false\n\t\tend\n\tend\n\treturn true\nend\n\nn = gets.chomp.to_i\nans = []\n\nfor i in (2..n)\n\tif is_prime(i)\n\t\tj = i\n\t\twhile j <= n\n\t\t\tans << j\n\t\t\tj *= i\n\t\tend\n\tend\nend\n\nputs ans.length\nputs ans.join(' ')"}, {"source_code": "require \"Prime\"\na=gets.chomp.to_i\nb=(Math.sqrt(a)).floor\nsp=[]\np=[]\nPrime.each(a) do|i|\nif i<=b\nsp<t)\n\t\tsum -= a[l]\n\t\tl += 1\n\tend\n\tsum += key\n\tr += 1\n\tan = [an, r-l+1].max\nend\nputs an"}, {"source_code": "#!/usr/bin/ruby1.9\n\nn, t = STDIN.readline.split.collect {|s| s.to_i }\na = STDIN.readline.split.collect {|s| s.to_i }\n\nmax = 0\nleft = right = 0\nsum = 0\n\nwhile right != n\n\tsum += a[right]\n\tright += 1\n\twhile sum > t\n\t\tsum -= a[left]\n\t\tleft += 1\n\tend\n\tif right-left > max\n\t\tmax = right-left\n\tend\nend\n\nputs max\n"}, {"source_code": "n,t=gets.chomp.split.map(&:to_i)\na=gets.chomp.split.map(&:to_i)\ns,l,ans=0,0,0\na.count.times do |i|\n\ts+=a[i]\n\twhile s>t\n\t\ts-=a[l]\n\t\tl+=1\n\tend\n\td=(i-l+1)\n\tans=d if ansm && lm\n\t\tsum+=x[r+1]\n\t\tr+=1\n\tend\n\tans=[ans,r-l+1].max\n\t#p [l,r,sum,ans]\n\tbreak if r==n-1\n\tsum+=x[r+1]\n\tr+=1\nend\nputs ans\n\n"}, {"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\n\ndef bsearch(arr, b, goal)\n e = arr.length\n while b < e\n mid = (b + e)/2\n if arr[mid] == goal\n b = mid\n break\n elsif arr[mid] < goal\n b = mid\n if e - b == 1\n break\n end\n else\n e = mid\n if e - b == 1\n break\n end\n end\n end\n return b\nend\n\nn, t = gets.split.map(&:to_i)\nacc = 0\nar = [0]\ngets.split.each { |i|\n i = i.to_i\n acc += i\n ar << acc\n}\ni, j = 0, 0 # indexing: [i, j], inclusive range. be careful!\nmaximum_books = 0\n# p ar\nloop do\n #p \"i: #{i}, ar[i] + t: #{ar[i] + t}\"\n nj = bsearch(ar, i, ar[i] + t)\n books = nj - i\n if maximum_books < books\n maximum_books = books\n end\n j = nj\n i += 1\n break if j >= ar.length - 1\nend\n\nputs maximum_books\n"}, {"source_code": "# def get_count(arr,l,r,t)\n\n# \twhile l >= 0 && l <= r\n# \t\tm = l + (r-l) / 2\n# \t\tputs \"#{l} #{r}\"\n# \t\treturn 1 if r ==m && arr[m] <= t\n# \t\tif arr[r] - arr[m] > t\n# \t\t\tl = m + 1\n# \t\telsif arr[r] - arr[m] < t\n# \t\t\tif arr[r] - arr[m-1] <= t\n# \t\t\t\tl = m - 1\n# \t\t\telse\n# \t\t\t\tputs \"less case #{l} #{r} #{m}\"\n# \t\t\t\treturn r - m\n# \t\t\tend\n# \t\telse\n# \t\t\tputs \"equal case #{l} #{r} #{m}\"\n# \t\t\treturn r - m\n# \t\tend\n# \tend\n# \treturn 0\n# end\n\n\n#For each r what is the l such that sum[l..r] <= t and l..r is maximum\n# def get_count(arr,l,r,t)\n# \t# return r - l if arr[r] - arr[l] <= t\t\n# \treturn r - l + 1 if arr[r] <= t\n# \t(l..(r-1)).each do |i|\n# \t\t# puts \"#{r} #{i}\"\n# \t\treturn r - i if arr[r] - arr[i] <= t\n# \tend\n# \treturn 0\n# end\n\n\n# def get_count(arr,l,r,t)\n# \t# return r - l if arr[r] - arr[l] <= t\t\n# \treturn r - l + 1 if arr[r] <= t\n\t\n# \twhile l >=0 && l < r\n# \t\tif arr[r] - arr[l] > t\n# \t\t\tm = l + ((r-l)/2.0).ceil\n# \t\t\tl = m\n# \t\t\t# puts \"#{l} #{m} #{r} #{l + (r-l)/2}\"\n# \t\telsif arr[r] - arr[l] < t\n# \t\t\tif l !=0 && arr[r] - arr[l-1] < t\n# \t\t\t\tl = l - 1\n# \t\t\telse\n# \t\t\t\treturn r - l\n# \t\t\tend\n# \t\telse\n# \t\t\treturn r - l\n# \t\tend\n# \tend\n\n# \treturn 0\n# end\n\n# def get_count(arr,l,r,t)\n# \t# return r - l if arr[r] - arr[l] <= t\t\n# \t# puts \"#{l} #{r}\"\n# \treturn r - l + 1 if arr[r] && arr[r] <= t\n\t\n# \twhile arr[r] && l < r\n# \t\tm = l + (r-l) / 2\n# \t\tif arr[l] + t > arr[r]\n# \t\t\tr = m\n# \t\telse\n# \t\t\tif arr[]\n# \t\t\t \tr = r + 1\n# \t\t \telse\n# \t\t \t\treturn r - l\n# \t\t \tend\n\n# \t\tend\n# \tend\n\n# \treturn 0\n# end\n\ndef upper_bound(arr,l,r,x)\n\tbest = nil\t\n\twhile l <= r\n\t\tm = (l+r)/2\n\t\t# puts \"#{l} #{r} #{m}\"\n\t\tif arr[m] <= x\n\t\t\tbest = m\n\t\t\t# puts \"m #{arr[m]} x #{x}\"\n\t\t\tl = m + 1\n\t\telse\n\t\t\tr = m - 1\n\t\tend\n\tend\n\treturn best\n\nend\n# puts upper_bound([1,2,3,4,5,6],0,5,10)\n# puts upper_bound([1,2,3,6,7,8],0,5,4)\n# puts upper_bound([1,2,3,4,5,6],0,5,0)\n# puts upper_bound([1,2,3,4,5,6],0,5,9)\nn,t = gets.split(\" \").map(&:to_i)\narr = gets.split(\" \").map(&:to_i)\nsum_arr = [arr[0]]\n(1..(n-1)).each do |i|\t\n\tsum_arr[i] = sum_arr[i-1] + arr[i]\nend\n# puts \"#{sum_arr}\"\nmax = 0\nunless sum_arr[n-1] <= t\n\tsum_arr.each_with_index{|a,i| \n\t\t# val = get_count(sum_arr,i,n-1,t)\t\n\t\tup = upper_bound(sum_arr,i,n-1,sum_arr[i]+t)\n\t \tif up\n\t\t\tval = up - i\n\t\t# puts \"#{sum_arr[i]+t}\"\n\t\t\t# val = 1 if up == i && arr[i] <= t\n\t\tend\n\t\tmax = val if val && val > max\n\t}\n\nelse\n\tmax = n\nend\n# 1 2 3 4 5\n# 1 3 6 10 15\nputs max\n# \n\n# Good - 3 days struggle finally accepted - http://canhazcode.blogspot.in/2012/02/we-need-to-talk-about-binary-search.html"}, {"source_code": "# def get_count(arr,l,r,t)\n\n# \twhile l >= 0 && l <= r\n# \t\tm = l + (r-l) / 2\n# \t\tputs \"#{l} #{r}\"\n# \t\treturn 1 if r ==m && arr[m] <= t\n# \t\tif arr[r] - arr[m] > t\n# \t\t\tl = m + 1\n# \t\telsif arr[r] - arr[m] < t\n# \t\t\tif arr[r] - arr[m-1] <= t\n# \t\t\t\tl = m - 1\n# \t\t\telse\n# \t\t\t\tputs \"less case #{l} #{r} #{m}\"\n# \t\t\t\treturn r - m\n# \t\t\tend\n# \t\telse\n# \t\t\tputs \"equal case #{l} #{r} #{m}\"\n# \t\t\treturn r - m\n# \t\tend\n# \tend\n# \treturn 0\n# end\n\n\n#For each r what is the l such that sum[l..r] <= t and l..r is maximum\n# def get_count(arr,l,r,t)\n# \t# return r - l if arr[r] - arr[l] <= t\t\n# \treturn r - l + 1 if arr[r] <= t\n# \t(l..(r-1)).each do |i|\n# \t\t# puts \"#{r} #{i}\"\n# \t\treturn r - i if arr[r] - arr[i] <= t\n# \tend\n# \treturn 0\n# end\n\n\n# def get_count(arr,l,r,t)\n# \t# return r - l if arr[r] - arr[l] <= t\t\n# \treturn r - l + 1 if arr[r] <= t\n\t\n# \twhile l >=0 && l < r\n# \t\tif arr[r] - arr[l] > t\n# \t\t\tm = l + ((r-l)/2.0).ceil\n# \t\t\tl = m\n# \t\t\t# puts \"#{l} #{m} #{r} #{l + (r-l)/2}\"\n# \t\telsif arr[r] - arr[l] < t\n# \t\t\tif l !=0 && arr[r] - arr[l-1] < t\n# \t\t\t\tl = l - 1\n# \t\t\telse\n# \t\t\t\treturn r - l\n# \t\t\tend\n# \t\telse\n# \t\t\treturn r - l\n# \t\tend\n# \tend\n\n# \treturn 0\n# end\n\n# def get_count(arr,l,r,t)\n# \t# return r - l if arr[r] - arr[l] <= t\t\n# \t# puts \"#{l} #{r}\"\n# \treturn r - l + 1 if arr[r] && arr[r] <= t\n\t\n# \twhile arr[r] && l < r\n# \t\tm = l + (r-l) / 2\n# \t\tif arr[l] + t > arr[r]\n# \t\t\tr = m\n# \t\telse\n# \t\t\tif arr[]\n# \t\t\t \tr = r + 1\n# \t\t \telse\n# \t\t \t\treturn r - l\n# \t\t \tend\n\n# \t\tend\n# \tend\n\n# \treturn 0\n# end\n\ndef upper_bound(arr,l,r,x)\n\tbest = nil\t\n\twhile l <= r\n\t\tm = (l+r)/2\n\t\t# puts \"#{l} #{r} #{m}\"\n\t\tif arr[m] <= x\n\t\t\tbest = m\n\t\t\t# puts \"m #{arr[m]} x #{x}\"\n\t\t\tl = m + 1\n\t\telse\n\t\t\tr = m - 1\n\t\tend\n\tend\n\treturn best\n\nend\n# puts upper_bound([1,2,3,4,5,6],0,5,10)\n# puts upper_bound([1,2,3,6,7,8],0,5,4)\n# puts upper_bound([1,2,3,4,5,6],0,5,0)\n# puts upper_bound([1,2,3,4,5,6],0,5,9)\nn,t = gets.split(\" \").map(&:to_i)\narr = gets.split(\" \").map(&:to_i)\nsum_arr = [arr[0]]\n(1..(n-1)).each do |i|\t\n\tsum_arr[i] = sum_arr[i-1] + arr[i]\nend\n# puts \"#{sum_arr}\"\nmax = 0\nunless sum_arr[n-1] <= t\n\tsum_arr.each_with_index{|a,i| \n\t\t# val = get_count(sum_arr,i,n-1,t)\t\n\t\tup = upper_bound(sum_arr,i,n-1,sum_arr[i]+t)\n\t \tif up\n\t\t\tval = up - i\n\t\t# puts \"#{sum_arr[i]+t}\"\n\t\t\t# val = 1 if up == i && arr[i] <= t\n\t\tend\n\t\tmax = val if val && val > max\n\t}\n\nelse\n\tmax = n\nend\n# 1 2 3 4 5\n# 1 3 6 10 15\nputs max\n# "}, {"source_code": "n,t = gets.chomp.split(\" \").collect! {|x| x.to_i};\nline = gets.chomp.split(\" \").collect! {|x| x.to_i};\n\np=0\nwhile pt \n p+=1\nend\n\nif p==n\n\tputs 0\n\texit\nend\n\nfirst = p\n q = p\nmax = 1\ncrt = 1\nsum = 0\n#puts p \n#puts q\n#puts line\nwhile qmax\n\t\t\tmax = q-p+1\n\t\tend\n\telse\n\t\twhile p<=q and sum>t\n\t\t\tsum -= line[p]\n\t\t\tp += 1\n\t\tend\n\tend\n\tq += 1\nend\n\nputs max\n\n"}, {"source_code": "# http://codeforces.com/problemset/problem/279/B\nn, t = gets.chomp.split.map(&:to_i)\nlist = gets.chomp.split.map(&:to_i)\n\nmax_books = 0\ntime = 0\nl = 0\nfor r in 0..(list.length - 1)\n\n while time > t\n time -= list[l]\n l += 1\n end\n\n time += list[r]\n if time <= t\n max_books = [r - l + 1, max_books].max\n end\nend\n\np max_books\n"}, {"source_code": "def read_next_line\n gets.split.map { |x| x.to_i }\nend\n\nn, t = read_next_line\na = read_next_line\n\np = cur_time = 0\nmax = -1_000_000_001\nfor i in 0...n\n cur_time += a[i]\n while cur_time > t\n cur_time -= a[p]\n p += 1\n end\n cur = i - p\n max = cur if cur > max\nend\nputs max + 1"}, {"source_code": "n, t = gets.split.map(&:to_i)\nl=0\nr=-1\nsum=0\nans = 0\na = gets.split.map(&:to_i)\na.each do |key| \n\twhile (sum+key>t)\n\t\tsum -= a[l]\n\t\tl += 1\n\tend\n\tsum += key\n\tr += 1\n\tans = [ans, r-l+1].max\nend\nputs ans"}, {"source_code": "\n\na, b = gets.split(\" \")\n\ntime = b.to_i\n\n\n\neachBookTime = []\n\ngets.split(\" \").each do |item|\n\n eachBookTime.push item.to_i\n\nend\n\n#eachBookTime = eachBookTime.sort\n\n\n\nsum = 0\n\ncount = 0\n\nfirst = 0\n\nmax = 0\n\neachBookTime.each do |item|\n\n while(sum + item > time && sum > 0)\n\n sum-=eachBookTime[first]\n\n first+=1\n\n count-=1\n\n end\n\n\n\n if(item < time)\n\n sum+=item\n\n count+=1\n\n max = count if count > max\n\n else\n\n sum+=item\n\n count+=1\n\n end\n\n \n\nend\n\n\n\nputs max"}], "negative_code": [{"source_code": "def npos(pos, n)\n (pos + 1) % n\nend\n\ndef ppos(pos, n)\n (pos - 1 + n) % n\nend\n\ndef run\n n, t = gets.split.map(&:to_i)\n a = gets.split.map(&:to_i)\n\n if a.min > t\n puts 0\n return\n end\n\n list = []\n sum = pos = max = val = 0\n n.times do |i|\n while sum <= t\n if list.length == n\n puts n\n return\n end\n\n sum += a[pos]\n list << a[pos]\n pos = npos(pos, n)\n end\n pos = ppos(pos, n)\n sum -= a[pos]\n list.pop\n\n if pos != i\n val = (ppos(pos, n) - i + n) % n + 1\n sum -= a[i]\n list.shift\n max = [max, val].max\n end\n end\n\n puts max\nend\n\nrun if $0 == __FILE__\n"}, {"source_code": "def npos(pos, n)\n (pos + 1) % n\nend\n\ndef ppos(pos, n)\n (pos - 1 + n) % n\nend\n\ndef run\n n, t = gets.split.map(&:to_i)\n a = gets.split.map(&:to_i)\n\n if a.min > t\n puts 0\n return\n end\n\n list = []\n sum = pos = max = val = 0\n n.times do |i|\n while sum <= t\n if list.length == n\n puts n\n return\n end\n\n sum += a[pos]\n list << a[pos]\n pos = npos(pos, n)\n end\n pos = ppos(pos, n)\n sum -= a[pos]\n list.pop\n\n if pos != i\n val = (ppos(pos, n) - i + n) % n + 1\n sum -= a[i]\n list.shift\n max = [max, val].max\n end\n end\n\n puts max\nend\n\nrun if $0 == __FILE__\n"}, {"source_code": "def npos(pos, n)\n (pos + 1) % n\nend\n\ndef ppos(pos, n)\n (pos - 1 + n) % n\nend\n\ndef run\n n, t = gets.split.map(&:to_i)\n a = gets.split.map(&:to_i)\n\n sum = pos = max = 0\n\n n.times do |i|\n while sum <= t\n sum += a[pos]\n pos = npos(pos, n)\n end\n pos = ppos(pos, n)\n sum -= a[pos]\n\n val = (ppos(pos, n) - i + n) % n + 1\n\n max = [max, val].max\n sum -= a[i]\n end\n\n puts max\nend\n\nrun if $0 == __FILE__\n"}, {"source_code": "def npos(pos, n)\n (pos + 1) % n\nend\n\ndef ppos(pos, n)\n (pos - 1 + n) % n\nend\n\ndef run\n n, t = gets.split.map(&:to_i)\n a = gets.split.map(&:to_i)\n\n sum = pos = max = 0\n\n n.times do |i|\n while sum <= t\n sum += a[pos]\n pos = npos(pos, n)\n end\n pos = ppos(pos, n)\n sum -= a[pos]\n\n max = [max, ppos(pos, n) - i + 1].max\n sum -= a[i]\n end\n\n puts max\nend\n\nrun if $0 == __FILE__\n"}, {"source_code": "def npos(pos, n)\n (pos + 1) % n\nend\n\ndef ppos(pos, n)\n (pos - 1 + n) % n\nend\n\ndef run\n n, t = gets.split.map(&:to_i)\n a = gets.split.map(&:to_i)\n\n if a.min > t\n puts 0\n return\n end\n\n list = []\n sum = pos = max = val = 0\n n.times do |i|\n while sum <= t\n if list.length == n\n puts n\n return\n end\n\n sum += a[pos]\n list << a[pos]\n pos = npos(pos, n)\n end\n pos = ppos(pos, n)\n sum -= a[pos]\n list.pop\n\n if pos == i\n val = (pos - i + n) % n + 1\n else\n val = (ppos(pos, n) - i + n) % n + 1\n sum -= a[i]\n list.shift\n end\n\n max = [max, val].max\n end\n\n puts max\nend\n\nrun if $0 == __FILE__\n"}, {"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\n\ndef bsearch(arr, b, goal)\n e = arr.length\n while b < e\n mid = (b + e)/2\n if arr[mid] == goal\n b = mid\n break\n elsif arr[mid] < goal\n b = mid\n if e - b == 1\n break\n end\n else\n e = mid\n if e - b == 1\n break\n end\n end\n end\n return b\nend\n\nn, t = gets.split.map(&:to_i)\nacc = 0\nar = []\ngets.split.each { |i|\n i = i.to_i\n acc += i\n ar << acc\n}\ni, j = 0, 0 # indexing: [i, j], inclusive range. be careful!\nmaximum_books = 0\nloop do\n nj = bsearch(ar, i, ar[i] + t)\n books = nj - i\n if nj == i && ar[nj] <= t\n books = 1\n end\n if maximum_books < books\n maximum_books = books\n end\n j = nj\n i += 1\n break if j == (ar.length - 1)\nend\n\nputs maximum_books\n"}, {"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\n\ndef bsearch(arr, b, goal)\n e = arr.length\n while b < e\n mid = (b + e)/2\n if arr[mid] == goal\n b = mid\n break\n elsif arr[mid] < goal\n b = mid\n if e - b == 1\n break\n end\n else\n e = mid\n if e - b == 1\n break\n end\n end\n end\n return b\nend\n\nn, t = gets.split.map(&:to_i)\nacc = 0\nar = []\ngets.split.each { |i|\n i = i.to_i\n acc += i\n ar << acc\n}\ni, j = 0, 0 # indexing: [i, j], inclusive range. be careful!\nmaximum_books = 0\nwhile j < (ar.length - 1)\n nj = bsearch(ar, i, ar[i] + t)\n if maximum_books < (nj - i)\n maximum_books = nj - i\n end\n j = nj\n i += 1\nend\n\nputs maximum_books\n"}, {"source_code": "# def get_count(arr,l,r,t)\n\n# \twhile l >= 0 && l <= r\n# \t\tm = l + (r-l) / 2\n# \t\tputs \"#{l} #{r}\"\n# \t\treturn 1 if r ==m && arr[m] <= t\n# \t\tif arr[r] - arr[m] > t\n# \t\t\tl = m + 1\n# \t\telsif arr[r] - arr[m] < t\n# \t\t\tif arr[r] - arr[m-1] <= t\n# \t\t\t\tl = m - 1\n# \t\t\telse\n# \t\t\t\tputs \"less case #{l} #{r} #{m}\"\n# \t\t\t\treturn r - m\n# \t\t\tend\n# \t\telse\n# \t\t\tputs \"equal case #{l} #{r} #{m}\"\n# \t\t\treturn r - m\n# \t\tend\n# \tend\n# \treturn 0\n# end\n\n\n#For each r what is the l such that sum[l..r] <= t and l..r is maximum\n# def get_count(arr,l,r,t)\n# \t# return r - l if arr[r] - arr[l] <= t\t\n# \treturn r - l + 1 if arr[r] <= t\n# \t(l..(r-1)).each do |i|\n# \t\t# puts \"#{r} #{i}\"\n# \t\treturn r - i if arr[r] - arr[i] <= t\n# \tend\n# \treturn 0\n# end\n\n\n# def get_count(arr,l,r,t)\n# \t# return r - l if arr[r] - arr[l] <= t\t\n# \treturn r - l + 1 if arr[r] <= t\n\t\n# \twhile l >=0 && l < r\n# \t\tif arr[r] - arr[l] > t\n# \t\t\tm = l + ((r-l)/2.0).ceil\n# \t\t\tl = m\n# \t\t\t# puts \"#{l} #{m} #{r} #{l + (r-l)/2}\"\n# \t\telsif arr[r] - arr[l] < t\n# \t\t\tif l !=0 && arr[r] - arr[l-1] < t\n# \t\t\t\tl = l - 1\n# \t\t\telse\n# \t\t\t\treturn r - l\n# \t\t\tend\n# \t\telse\n# \t\t\treturn r - l\n# \t\tend\n# \tend\n\n# \treturn 0\n# end\n\n# def get_count(arr,l,r,t)\n# \t# return r - l if arr[r] - arr[l] <= t\t\n# \t# puts \"#{l} #{r}\"\n# \treturn r - l + 1 if arr[r] && arr[r] <= t\n\t\n# \twhile arr[r] && l < r\n# \t\tm = l + (r-l) / 2\n# \t\tif arr[l] + t > arr[r]\n# \t\t\tr = m\n# \t\telse\n# \t\t\tif arr[]\n# \t\t\t \tr = r + 1\n# \t\t \telse\n# \t\t \t\treturn r - l\n# \t\t \tend\n\n# \t\tend\n# \tend\n\n# \treturn 0\n# end\n\ndef upper_bound(arr,l,r,x)\n\tbest = nil\n\twhile l <= r\n\t\tm = l + (r-l)/2\n\t\tif arr[m] <= x\n\t\t\tbest = m\n\t\t\tl = m + 1\n\t\telse\n\t\t\tr = m - 1\n\t\tend\n\tend\n\treturn best\n\nend\n\nn,t = gets.split(\" \").map(&:to_i)\narr = gets.split(\" \").map(&:to_i)\nsum_arr = [arr[0]]\n(1..(n-1)).each do |i|\t\n\tsum_arr[i] = sum_arr[i-1] + arr[i]\nend\n# puts \"#{sum_arr}\"\nmax = 0\nsum_arr.each_with_index{|a,i| \n\t# val = get_count(sum_arr,i,n-1,t)\n\tup = upper_bound(sum_arr,i,n-1,sum_arr[i]+t)\n\tval = up - i if up\n\tmax = val if val && val > max\n}\n\n# 1 2 3 4 5\n# 1 3 6 10 15\nputs max\n# "}, {"source_code": "# def get_count(arr,l,r,t)\n\n# \twhile l >= 0 && l <= r\n# \t\tm = l + (r-l) / 2\n# \t\tputs \"#{l} #{r}\"\n# \t\treturn 1 if r ==m && arr[m] <= t\n# \t\tif arr[r] - arr[m] > t\n# \t\t\tl = m + 1\n# \t\telsif arr[r] - arr[m] < t\n# \t\t\tif arr[r] - arr[m-1] <= t\n# \t\t\t\tl = m - 1\n# \t\t\telse\n# \t\t\t\tputs \"less case #{l} #{r} #{m}\"\n# \t\t\t\treturn r - m\n# \t\t\tend\n# \t\telse\n# \t\t\tputs \"equal case #{l} #{r} #{m}\"\n# \t\t\treturn r - m\n# \t\tend\n# \tend\n# \treturn 0\n# end\n\n\n#For each r what is the l such that sum[l..r] <= t and l..r is maximum\n# def get_count(arr,l,r,t)\n# \t# return r - l if arr[r] - arr[l] <= t\t\n# \treturn r - l + 1 if arr[r] <= t\n# \t(l..(r-1)).each do |i|\n# \t\t# puts \"#{r} #{i}\"\n# \t\treturn r - i if arr[r] - arr[i] <= t\n# \tend\n# \treturn 0\n# end\n\n\n# def get_count(arr,l,r,t)\n# \t# return r - l if arr[r] - arr[l] <= t\t\n# \treturn r - l + 1 if arr[r] <= t\n\t\n# \twhile l >=0 && l < r\n# \t\tif arr[r] - arr[l] > t\n# \t\t\tm = l + ((r-l)/2.0).ceil\n# \t\t\tl = m\n# \t\t\t# puts \"#{l} #{m} #{r} #{l + (r-l)/2}\"\n# \t\telsif arr[r] - arr[l] < t\n# \t\t\tif l !=0 && arr[r] - arr[l-1] < t\n# \t\t\t\tl = l - 1\n# \t\t\telse\n# \t\t\t\treturn r - l\n# \t\t\tend\n# \t\telse\n# \t\t\treturn r - l\n# \t\tend\n# \tend\n\n# \treturn 0\n# end\n\n# def get_count(arr,l,r,t)\n# \t# return r - l if arr[r] - arr[l] <= t\t\n# \t# puts \"#{l} #{r}\"\n# \treturn r - l + 1 if arr[r] && arr[r] <= t\n\t\n# \twhile arr[r] && l < r\n# \t\tm = l + (r-l) / 2\n# \t\tif arr[l] + t > arr[r]\n# \t\t\tr = m\n# \t\telse\n# \t\t\tif arr[]\n# \t\t\t \tr = r + 1\n# \t\t \telse\n# \t\t \t\treturn r - l\n# \t\t \tend\n\n# \t\tend\n# \tend\n\n# \treturn 0\n# end\n\ndef upper_bound(arr,l,r,x)\n\tbest = nil\n\twhile l <= r\n\t\tm = l + (r-l)/2\n\t\tif arr[m] <= x\n\t\t\tbest = m\n\t\t\tl = m + 1\n\t\telse\n\t\t\tr = m - 1\n\t\tend\n\tend\n\treturn best\n\nend\n\nn,t = gets.split(\" \").map(&:to_i)\narr = gets.split(\" \").map(&:to_i)\nsum_arr = [arr[0]]\n(1..(n-1)).each do |i|\t\n\tsum_arr[i] = sum_arr[i-1] + arr[i]\nend\n# puts \"#{sum_arr}\"\nmax = 0\nsum_arr.each_with_index{|a,i| \n\t# val = get_count(sum_arr,i,n-1,t)\n\tup = upper_bound(sum_arr,i,n-1,sum_arr[i]+t)\n\tval = up - i if up\n\tval = 1 if up == i\n\tmax = val if val && val > max\n}\n\n# 1 2 3 4 5\n# 1 3 6 10 15\nputs max\n# "}, {"source_code": "# def get_count(arr,l,r,t)\n\n# \twhile l >= 0 && l <= r\n# \t\tm = l + (r-l) / 2\n# \t\tputs \"#{l} #{r}\"\n# \t\treturn 1 if r ==m && arr[m] <= t\n# \t\tif arr[r] - arr[m] > t\n# \t\t\tl = m + 1\n# \t\telsif arr[r] - arr[m] < t\n# \t\t\tif arr[r] - arr[m-1] <= t\n# \t\t\t\tl = m - 1\n# \t\t\telse\n# \t\t\t\tputs \"less case #{l} #{r} #{m}\"\n# \t\t\t\treturn r - m\n# \t\t\tend\n# \t\telse\n# \t\t\tputs \"equal case #{l} #{r} #{m}\"\n# \t\t\treturn r - m\n# \t\tend\n# \tend\n# \treturn 0\n# end\n\n\n#For each r what is the l such that sum[l..r] <= t and l..r is maximum\n# def get_count(arr,l,r,t)\n# \t# return r - l if arr[r] - arr[l] <= t\t\n# \treturn r - l + 1 if arr[r] <= t\n# \t(l..(r-1)).each do |i|\n# \t\t# puts \"#{r} #{i}\"\n# \t\treturn r - i if arr[r] - arr[i] <= t\n# \tend\n# \treturn 0\n# end\n\n\n# def get_count(arr,l,r,t)\n# \t# return r - l if arr[r] - arr[l] <= t\t\n# \treturn r - l + 1 if arr[r] <= t\n\t\n# \twhile l >=0 && l < r\n# \t\tif arr[r] - arr[l] > t\n# \t\t\tm = l + ((r-l)/2.0).ceil\n# \t\t\tl = m\n# \t\t\t# puts \"#{l} #{m} #{r} #{l + (r-l)/2}\"\n# \t\telsif arr[r] - arr[l] < t\n# \t\t\tif l !=0 && arr[r] - arr[l-1] < t\n# \t\t\t\tl = l - 1\n# \t\t\telse\n# \t\t\t\treturn r - l\n# \t\t\tend\n# \t\telse\n# \t\t\treturn r - l\n# \t\tend\n# \tend\n\n# \treturn 0\n# end\n\n# def get_count(arr,l,r,t)\n# \t# return r - l if arr[r] - arr[l] <= t\t\n# \t# puts \"#{l} #{r}\"\n# \treturn r - l + 1 if arr[r] && arr[r] <= t\n\t\n# \twhile arr[r] && l < r\n# \t\tm = l + (r-l) / 2\n# \t\tif arr[l] + t > arr[r]\n# \t\t\tr = m\n# \t\telse\n# \t\t\tif arr[]\n# \t\t\t \tr = r + 1\n# \t\t \telse\n# \t\t \t\treturn r - l\n# \t\t \tend\n\n# \t\tend\n# \tend\n\n# \treturn 0\n# end\n\ndef upper_bound(arr,l,r,x)\n\tbest = nil\n\twhile l <= r\n\t\tm = l + (r-l)/2\n\t\tif arr[m] <= x\n\t\t\tbest = m\n\t\t\tl = m + 1\n\t\telse\n\t\t\tr = m - 1\n\t\tend\n\tend\n\treturn best\n\nend\n\nn,t = gets.split(\" \").map(&:to_i)\narr = gets.split(\" \").map(&:to_i)\nsum_arr = [arr[0]]\n(1..(n-1)).each do |i|\t\n\tsum_arr[i] = sum_arr[i-1] + arr[i]\nend\n# puts \"#{sum_arr}\"\nmax = 0\nsum_arr.each_with_index{|a,i| \n\t# val = get_count(sum_arr,i,n-1,t)\n\tup = upper_bound(sum_arr,i,n-1,sum_arr[i]+t)\n \tif up\n\t\tval = up - i\n\t\tval = 1 if up == i && arr[i] <= t\n\tend\n\t# puts \"#{up}\"\n\tmax = val if val && val > max\n}\n\n# 1 2 3 4 5\n# 1 3 6 10 15\nputs max\n# "}, {"source_code": "# http://codeforces.com/problemset/problem/279/B\nn, t = gets.chomp.split.map(&:to_i)\nlist = gets.chomp.split.map(&:to_i)\n\nmax = 0\n\ntime = 0\nfor i in 0..(n - 1)\n for j in i..(n - 1)\n time += list[j]\n\n if time > t\n break\n else\n potential = j - i + 1\n\n if potential > max\n max = potential\n end\n end\n end\nend\n\np max\n\n[3,1,2,1]\n"}, {"source_code": "def read_next_line\n gets.split.map { |x| x.to_i }\nend\n\nn, t = read_next_line\na = read_next_line\n\nmax = p = cur_time = 0\nfor i in 0...n\n cur_time += a[i]\n while cur_time > t\n cur_time -= a[p]\n p += 1\n end\n cur = i - p\n max = cur if cur > max\nend\nputs max + 1"}, {"source_code": "\n\na, b = gets.split(\" \")\n\ntime = b.to_i\n\n\n\neachBookTime = []\n\ngets.split(\" \").each do |item|\n\n eachBookTime.push item.to_i\n\nend\n\nsum = 0\n\ncount = 0\n\neachBookTime.each do |item|\n\n if (sum+item <= time)\n\n sum += item\n\n count+=1\n\n end\n\nend\n\n\n\nputs count"}, {"source_code": "a, b = gets.split(\" \")\n\ntime = b.to_i\n\n\n\neachBookTime = []\n\ngets.split(\" \").each do |item|\n\n eachBookTime.push item.to_i\n\nend\n\neachBookTime = eachBookTime.sort\n\n\n\nsum = 0\n\ncount = 0\n\neachBookTime.each do |item|\n\n if (sum+item < time)\n\n sum += item\n\n count+=1\n\n end\n\nend\n\n\n\nputs count"}, {"source_code": "\n\na, b = gets.split(\" \")\n\ntime = b.to_i\n\n\n\neachBookTime = []\n\ngets.split(\" \").each do |item|\n\n eachBookTime.push item.to_i\n\nend\n\neachBookTime = eachBookTime.sort\n\n\n\nsum = 0\n\ncount = 0\n\neachBookTime.each do |item|\n\n if (sum+item <= time)\n\n sum += item\n\n count+=1\n\n end\n\nend\n\n\n\nputs count"}], "src_uid": "ef32a8f37968629673547db574261a9d"} {"nl": {"description": "Let's imagine: there is a chess piece billiard ball. Its movements resemble the ones of a bishop chess piece. The only difference is that when a billiard ball hits the board's border, it can reflect from it and continue moving.More formally, first one of four diagonal directions is chosen and the billiard ball moves in that direction. When it reaches the square located on the board's edge, the billiard ball reflects from it; it changes the direction of its movement by 90 degrees and continues moving. Specifically, having reached a corner square, the billiard ball is reflected twice and starts to move the opposite way. While it moves, the billiard ball can make an infinite number of reflections. At any square of its trajectory the billiard ball can stop and on that the move is considered completed. It is considered that one billiard ball a beats another billiard ball b if a can reach a point where b is located.You are suggested to find the maximal number of billiard balls, that pairwise do not beat each other and that can be positioned on a chessboard n × m in size.", "input_spec": "The first line contains two integers n and m (2 ≤ n, m ≤ 106).", "output_spec": "Print a single number, the maximum possible number of billiard balls that do not pairwise beat each other. Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is preferred to use cin (also you may use the %I64d specificator).", "sample_inputs": ["3 4", "3 3"], "sample_outputs": ["2", "3"], "notes": null}, "positive_code": [{"source_code": "n,m=gets.chomp.split.collect{|a| a.to_i}\nputs ((n-1).gcd(m-1)+1)"}, {"source_code": "require 'mathn'\nm, n = gets.chomp.split.map{|s| s.to_i - 1}.sort\nputs m.gcd(n) + 1\n"}], "negative_code": [{"source_code": "m, n = gets.chomp.split.map{|s| s.to_i}\nputs m == n ? m : 2\n"}, {"source_code": "m, n = gets.chomp.split.map{|s| s.to_i - 1}.sort\nputs n % m == 0 ? m + 1 : 2\n"}], "src_uid": "05f251de93536024c05fbd77ed01b70b"} {"nl": {"description": "There was an epidemic in Monstropolis and all monsters became sick. To recover, all monsters lined up in queue for an appointment to the only doctor in the city.Soon, monsters became hungry and began to eat each other. One monster can eat other monster if its weight is strictly greater than the weight of the monster being eaten, and they stand in the queue next to each other. Monsters eat each other instantly. There are no monsters which are being eaten at the same moment. After the monster A eats the monster B, the weight of the monster A increases by the weight of the eaten monster B. In result of such eating the length of the queue decreases by one, all monsters after the eaten one step forward so that there is no empty places in the queue again. A monster can eat several monsters one after another. Initially there were n monsters in the queue, the i-th of which had weight ai.For example, if weights are [1, 2, 2, 2, 1, 2] (in order of queue, monsters are numbered from 1 to 6 from left to right) then some of the options are: the first monster can't eat the second monster because a1 = 1 is not greater than a2 = 2; the second monster can't eat the third monster because a2 = 2 is not greater than a3 = 2; the second monster can't eat the fifth monster because they are not neighbors; the second monster can eat the first monster, the queue will be transformed to [3, 2, 2, 1, 2]. After some time, someone said a good joke and all monsters recovered. At that moment there were k (k ≤ n) monsters in the queue, the j-th of which had weight bj. Both sequences (a and b) contain the weights of the monsters in the order from the first to the last.You are required to provide one of the possible orders of eating monsters which led to the current queue, or to determine that this could not happen. Assume that the doctor didn't make any appointments while monsters were eating each other.", "input_spec": "The first line contains single integer n (1 ≤ n ≤ 500) — the number of monsters in the initial queue. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the initial weights of the monsters. The third line contains single integer k (1 ≤ k ≤ n) — the number of monsters in the queue after the joke. The fourth line contains k integers b1, b2, ..., bk (1 ≤ bj ≤ 5·108) — the weights of the monsters after the joke. Monsters are listed in the order from the beginning of the queue to the end.", "output_spec": "In case if no actions could lead to the final queue, print \"NO\" (without quotes) in the only line. Otherwise print \"YES\" (without quotes) in the first line. In the next n - k lines print actions in the chronological order. In each line print x — the index number of the monster in the current queue which eats and, separated by space, the symbol 'L' if the monster which stays the x-th in the queue eats the monster in front of him, or 'R' if the monster which stays the x-th in the queue eats the monster behind him. After each eating the queue is enumerated again. When one monster eats another the queue decreases. If there are several answers, print any of them.", "sample_inputs": ["6\n1 2 2 2 1 2\n2\n5 5", "5\n1 2 3 4 5\n1\n15", "5\n1 1 1 3 3\n3\n2 1 6"], "sample_outputs": ["YES\n2 L\n1 R\n4 L\n3 L", "YES\n5 L\n4 L\n3 L\n2 L", "NO"], "notes": "NoteIn the first example, initially there were n = 6 monsters, their weights are [1, 2, 2, 2, 1, 2] (in order of queue from the first monster to the last monster). The final queue should be [5, 5]. The following sequence of eatings leads to the final queue: the second monster eats the monster to the left (i.e. the first monster), queue becomes [3, 2, 2, 1, 2]; the first monster (note, it was the second on the previous step) eats the monster to the right (i.e. the second monster), queue becomes [5, 2, 1, 2]; the fourth monster eats the mosnter to the left (i.e. the third monster), queue becomes [5, 2, 3]; the finally, the third monster eats the monster to the left (i.e. the second monster), queue becomes [5, 5]. Note that for each step the output contains numbers of the monsters in their current order in the queue."}, "positive_code": [{"source_code": "def can_eat_left? arr, i\n return false unless i > 0\n return arr[i].to_i > arr[i-1].to_i\nend\n\ndef can_eat_right? arr, i\n return false unless arr.size > i + 1\n return arr[i].to_i > arr[i+1].to_i\nend\n\ndef max_eat_other now_eating, now_index\t\t\t# sure max element eat other \n\twhile now_eating.size > 1\n\t\tif can_eat_left? now_eating, now_index\n\t\t\teat_left now_eating, now_index\n\t\t\tnow_index = now_index -1\n\t\telse can_eat_right? now_eating, now_index\n\t\t\teat_right now_eating, now_index\n\t\tend\n\tend\nend\n\ndef find_max eating_array\t\t# return 1 max elemnt and suggest it to eat left or right,return -999 if can not eating\n\tindex_all_max = Array.new\t\t\t\t#m contain all max element of array\n\tmulti_all_max = Array.new\n\tmy_max = eating_array.max\n\teating_array.each_with_index do |x, index|\n\t\tif x == my_max\n\t\t\tindex_all_max.push(index)\n\t\tend\n\tend\n\tif index_all_max.size == 1\n\t\tif can_eat_left? eating_array, index_all_max[0]\n\t\t\tindex_all_max.push(-1)\t\t\t# suggest eat left\n\t\telsif can_eat_right? eating_array, index_all_max[0]\n\t\t\tindex_all_max.push(1)\t\t\t# suggest eat right\n\t\telse\n\t\t\tindex_all_max.push(0)\t\t\t# just enought not eat any more\n\t\tend\n\t\tindex_all_max\n\telse\n\t\tindex_all_max.each do |x|\n\t\t\tif can_eat_left? eating_array, x\n\t\t\t\tmulti_all_max.push(x)\n\t\t\t\tmulti_all_max.push(-1)\n\t\t\t\treturn multi_all_max\n\t\t\tend\n\t\t\tif can_eat_right? eating_array, x\n\t\t\t\t#puts \"checking with\" + eating_array.to_s + \"x:\" + x.to_s\n\t\t\t\tmulti_all_max.push(x)\n\t\t\t\tmulti_all_max.push(1)\n\t\t\t\treturn multi_all_max\n\t\t\tend\n\t\tend\n\t\tmulti_all_max.push(-999)\t\t# in case all monster can not eat other\n\t\treturn multi_all_max\n\tend\n\nend\n\ndef elements_number elements, total\n\tnum = 0\n\telements.each do |x|\n\t\ttotal = total - x\n\t\tnum += 1\n\t\tif total == 0\n\t\t\treturn num\n\t\telsif total < 0\n\t\t\treturn -999\n\t\tend\n\tend\n\treturn -999\t\t# incase can no make total equal head number\nend\n\ndef eat_from_head eating_array, num\n\t#puts eating_array.to_s\n\t#puts num\n\t#puts \"xxxxxx-\"\n\tnumber_of_attends = elements_number eating_array, num\n\t#puts \"number_of_attends\" + number_of_attends.to_s\n\tif number_of_attends < 0\n\t\t$can_eat_flag = false\n\t\treturn -999\n\tend\n\tnow_eating = eating_array.slice(0, number_of_attends)\n\t#puts \"slice \" + now_eating.to_s\n\t# check before eat\n\tsub_max = find_max now_eating\n\t#puts \"submax\" + sub_max.to_s\n\tif sub_max[0] < 0 \t# -999\n\t\treturn -999\n\telse\n\t\tif sub_max[1] < 0\n\t\t\teat_left now_eating, sub_max[0]\n\t\t\tsub_max[0] = sub_max[0] -1\n\t\t\tmax_eat_other now_eating, sub_max[0]\n\t\telsif sub_max[1] > 0\n\t\t\teat_right now_eating, sub_max[0]\n\t\t\t#puts \"now eating\" + now_eating.to_s\n\t\t\tmax_eat_other now_eating, sub_max[0]\n\t\telse\n\t\t\t#do nothing\n\t\tend\n\tend\n\t# eating_array.drop(number_of_attends)\n\t$completed += 1\n\treturn number_of_attends\nend\n\ndef monster_eat original_monster, eaten_monsters\n\t#puts \"x\"\n\tif original_monster.size == 0 && eaten_monsters.size == 0\n\t\treturn true\n\telsif original_monster.size == 0 || eaten_monsters.size == 0\n\t\treturn false\n\tend\n\n\tnumber_of_attends = eat_from_head(original_monster, eaten_monsters[0])\n\tif number_of_attends > 0\n\t\t(0..number_of_attends-1).each do |x|\n\t\t\toriginal_monster.delete_at(0)\n\t\tend\n\t\teaten_monsters.delete_at(0)\n\t\t#puts original_monster.to_s\n\t\t#puts eaten_monsters.to_s\n\t\treturn monster_eat original_monster, eaten_monsters\n\telse\n\t\treturn false\n\tend\n\treturn true\nend\n\n#===============#\ndef eat_left arr, i\n $eat_steps << (i + 1 + $completed).to_s + \" L\"\n arr[i-1] += arr[i]\n arr.delete_at(i)\nend\n\ndef eat_right arr, i\n $eat_steps << (i + 1 + $completed).to_s + \" R\"\n arr[i] += arr[i+1]\n arr.delete_at(i+1)\nend\n#===============#\n\n#========= MAIN ===========#\n$completed = 0\n$can_eat_flag = false\n$eat_steps = Array.new\n\nmonster_number = gets.to_i\noriginal_monster = Array.new\na = gets\nmonsters = a.split(\" \")\nmonsters.each do |x|\n original_monster << x.to_i\nend\n\neaten_number = gets.to_i\nb = gets\ne_monsters = b.split(\" \")\neaten_monsters = Array.new\ne_monsters.each do |x|\n eaten_monsters << x.to_i\nend\n\n$can_eat_flag = monster_eat(original_monster, eaten_monsters)\nif $can_eat_flag\n puts \"YES\"\n $eat_steps.each do |x|\n puts x\n end\nelse\n puts \"NO\"\nend\n\n#========= MAIN ===========#"}], "negative_code": [{"source_code": "def can_eat_left? arr, i\n return false unless i > 0\n return arr[i].to_i > arr[i-1].to_i\nend\ndef can_eat_right? arr, i\n return false unless arr.size > i + 1\n return arr[i].to_i > arr[i+1].to_i\nend\n\ndef eat_left arr, i\n $eat_steps << (i+1).to_s + \" L\"\n arr[i-1] += arr[i]\n arr.delete_at(i)\nend\n\ndef eat_right arr, i\n $eat_steps << (i+1).to_s + \" R\"\n arr[i] += arr[i+1]\n arr.delete_at(i+1)\nend\n\ndef become_eat original_monster, eaten_monsters\n # puts \"ori:\" + original_monster.to_s\n # puts \"eaten:\" + eaten_monsters.to_s\n eaten_monsters.each_with_index do |eaten, index|\n if eat_head(eaten, original_monster) == -1\n $flag = false # this case only puts \"NO\"\n break\n end\n\n if eat_head(eaten, eaten_monsters) == 0\n original_monster.delete_at(0)\n eaten_monsters.delete_at(0)\n become_eat original_monster, eaten_monsters\n end\n end\n\n $flag = true if original_monster.size == 0 && eaten_monsters.size == 0\nend\n\ndef eat_head value, original_monster\n # puts value\n # puts original_monster.to_s\n return -1 if value.to_i < original_monster[0].to_i # this will puts \"NO\"\n return 0 if value.to_i == original_monster[0].to_i # this will delete 2 element in 2 array, and continue to eat\n\n if can_eat_right?(original_monster, 0)\n eat_right(original_monster,0)\n return eat_head(value,original_monster) # try to eat as possible\n end\n\n (0..original_monster.size - 1).each do |x|\n if can_eat_right?(original_monster, x)\n eat_right(original_monster, x)\n return eat_head(value,original_monster)\n end\n if can_eat_left?(original_monster, x)\n eat_left(original_monster, x)\n return eat_head(value,original_monster)\n end\n end\n\nend\n\n# input processor\nmonster_number = gets.to_i\noriginal_monster = Array.new\na = gets\nmonsters = a.split(\" \")\nmonsters.each do |x|\n original_monster << x.to_i\nend\n\neaten_number = gets.to_i\nb = gets\ne_monsters = b.split(\" \")\neaten_monsters = Array.new\ne_monsters.each do |x|\n eaten_monsters << x.to_i\nend\n\n# Main\n$eat_steps = Array.new\n$flag = false\nbecome_eat(original_monster, eaten_monsters)\n\nif $flag\n puts \"YES\"\n $eat_steps.each do |x|\n puts x\n end\nelse\n puts \"NO\"\nend\n\n\n# puts \"-----------------------------\"\n# puts monster_number\n# puts original_monster.to_s\n# puts eaten_number\n# puts eaten_monsters.to_s\n"}, {"source_code": "def can_eat_left? arr, i\n return false unless i > 0\n return arr[i].to_i > arr[i-1].to_i\nend\ndef can_eat_right? arr, i\n return false unless arr.size > i + 1\n return arr[i].to_i > arr[i+1].to_i\nend\n\ndef eat_left arr, i\n $eat_steps << (i + 1 + $deleted_index).to_s + \" L\"\n arr[i-1] += arr[i]\n arr.delete_at(i)\nend\n\ndef eat_right arr, i\n $eat_steps << (i + 1 + $deleted_index).to_s + \" R\"\n arr[i] += arr[i+1]\n arr.delete_at(i+1)\nend\n\ndef become_eat original_monster, eaten_monsters\n # puts \"ori:\" + original_monster.to_s\n # puts \"eaten:\" + eaten_monsters.to_s\n eaten_monsters.each_with_index do |eaten, index|\n sw = eat_head(eaten, original_monster)\n if sw == -1\n $flag = false # this case only puts \"NO\"\n break\n end\n\n if sw == 0\n original_monster.delete_at(0)\n eaten_monsters.delete_at(0)\n $deleted_index += 1\n become_eat original_monster, eaten_monsters\n end\n end\n\n $flag = true if original_monster.size == 0 && eaten_monsters.size == 0\nend\n\ndef eat_head value, original_monster\n # puts value\n # puts original_monster.to_s\n return -1 if value.to_i < original_monster[0].to_i # this will puts \"NO\"\n return 0 if value.to_i == original_monster[0].to_i # this will delete 2 element in 2 array, and continue to eat\n\n # if can_eat_right?(original_monster, 0)\n # eat_right(original_monster,0)\n # return eat_head(value,original_monster) # try to eat as possible\n # end\n\n (0..original_monster.size - 1).each do |x|\n if can_eat_right?(original_monster, x)\n eat_right(original_monster, x)\n return eat_head(value,original_monster)\n end\n if can_eat_left?(original_monster, x)\n eat_left(original_monster, x)\n return eat_head(value,original_monster)\n end\n end\n\nend\n\n# input processor\nmonster_number = gets.to_i\noriginal_monster = Array.new\na = gets\nmonsters = a.split(\" \")\nmonsters.each do |x|\n original_monster << x.to_i\nend\n\neaten_number = gets.to_i\nb = gets\ne_monsters = b.split(\" \")\neaten_monsters = Array.new\ne_monsters.each do |x|\n eaten_monsters << x.to_i\nend\n\n# Main\n$eat_steps = Array.new\n$flag = false\n$deleted_index = 0\nbecome_eat(original_monster, eaten_monsters)\n\nif $flag\n puts \"YES\"\n $eat_steps.each do |x|\n puts x\n end\nelse\n puts \"NO\"\nend\n\n\n# puts \"-----------------------------\"\n# puts monster_number\n# puts original_monster.to_s\n# puts eaten_number\n# puts eaten_monsters.to_s\n"}, {"source_code": "def can_eat_left? arr, i\n return false unless i > 0\n return arr[i].to_i > arr[i-1].to_i\nend\ndef can_eat_right? arr, i\n return false unless arr.size > i + 1\n return arr[i].to_i > arr[i+1].to_i\nend\n\ndef eat_left arr, i\n $eat_steps << (i+1).to_s + \" L\"\n arr[i-1] += arr[i]\n arr.delete_at(i)\nend\n\ndef eat_right arr, i\n $eat_steps << (i+1).to_s + \" R\"\n arr[i] += arr[i+1]\n arr.delete_at(i+1)\nend\n\ndef become_eat original_monster, eaten_monsters\n # puts \"ori:\" + original_monster.to_s\n # puts \"eaten:\" + eaten_monsters.to_s\n eaten_monsters.each_with_index do |eaten, index|\n if eat_head(eaten, original_monster) == -1\n $flag = false # this case only puts \"NO\"\n break\n end\n\n if eat_head(eaten, eaten_monsters) == 0\n original_monster.delete_at(0)\n eaten_monsters.delete_at(0)\n become_eat original_monster, eaten_monsters\n end\n end\n\n $flag = true if original_monster.size == 0 && eaten_monsters.size == 0\nend\n\ndef eat_head value, original_monster\n # puts value\n # puts original_monster.to_s\n return -1 if value.to_i < original_monster[0].to_i # this will puts \"NO\"\n return 0 if value.to_i == original_monster[0].to_i # this will delete 2 element in 2 array, and continue to eat\n\n # if can_eat_right?(original_monster, 0)\n # eat_right(original_monster,0)\n # return eat_head(value,original_monster) # try to eat as possible\n # end\n\n (0..original_monster.size - 1).each do |x|\n if can_eat_right?(original_monster, x)\n eat_right(original_monster, x)\n return eat_head(value,original_monster)\n end\n if can_eat_left?(original_monster, x)\n eat_left(original_monster, x)\n return eat_head(value,original_monster)\n end\n end\n\nend\n\n# input processor\nmonster_number = gets.to_i\noriginal_monster = Array.new\na = gets\nmonsters = a.split(\" \")\nmonsters.each do |x|\n original_monster << x.to_i\nend\n\neaten_number = gets.to_i\nb = gets\ne_monsters = b.split(\" \")\neaten_monsters = Array.new\ne_monsters.each do |x|\n eaten_monsters << x.to_i\nend\n\n# Main\n$eat_steps = Array.new\n$flag = false\nbecome_eat(original_monster, eaten_monsters)\n\nif $flag\n puts \"YES\"\n $eat_steps.each do |x|\n puts x\n end\nelse\n puts \"NO\"\nend\n\n\n# puts \"-----------------------------\"\n# puts monster_number\n# puts original_monster.to_s\n# puts eaten_number\n# puts eaten_monsters.to_s\n"}, {"source_code": "def can_eat_left? arr, i\n return false unless i > 0\n return arr[i].to_i > arr[i-1].to_i\nend\ndef can_eat_right? arr, i\n return false unless arr.size > i + 1\n return arr[i].to_i > arr[i+1].to_i\nend\n\ndef eat_left arr, i\n $eat_steps << (i + 1 + $deleted_index).to_s + \" L\"\n arr[i-1] += arr[i]\n arr.delete_at(i)\nend\n\ndef eat_right arr, i\n $eat_steps << (i + 1 + $deleted_index).to_s + \" R\"\n arr[i] += arr[i+1]\n arr.delete_at(i+1)\nend\n\ndef become_eat original_monster, eaten_monsters\n # puts \"ori:\" + original_monster.to_s\n # puts \"eaten:\" + eaten_monsters.to_s\n eaten_monsters.each_with_index do |eaten, index|\n sw = eat_head(eaten, original_monster)\n if sw == -1\n $flag = false # this case only puts \"NO\"\n break\n end\n\n if sw == 0\n original_monster.delete_at(0)\n eaten_monsters.delete_at(0)\n $deleted_index += 1\n become_eat original_monster, eaten_monsters\n end\n end\n\n $flag = true if original_monster.size == 0 && eaten_monsters.size == 0\nend\n\ndef eat_head value, original_monster\n # puts value\n # puts original_monster.to_s\n return -1 if value.to_i < original_monster[0].to_i # this will puts \"NO\"\n return 0 if value.to_i == original_monster[0].to_i # this will delete 2 element in 2 array, and continue to eat\n\n if can_eat_right?(original_monster, 0)\n eat_right(original_monster,0)\n return eat_head(value,original_monster) # try to eat as possible\n end\n\n (1..original_monster.size - 1).each do |x|\n if can_eat_left?(original_monster, x) # always try to eat left first\n eat_left(original_monster, x)\n return eat_head(value,original_monster)\n end\n\n if can_eat_right?(original_monster, x) # tr to eat right after\n eat_right(original_monster, x)\n return eat_head(value,original_monster)\n end\n end\n\nend\n\n# input processor\nmonster_number = gets.to_i\noriginal_monster = Array.new\na = gets\nmonsters = a.split(\" \")\nmonsters.each do |x|\n original_monster << x.to_i\nend\n\neaten_number = gets.to_i\nb = gets\ne_monsters = b.split(\" \")\neaten_monsters = Array.new\ne_monsters.each do |x|\n eaten_monsters << x.to_i\nend\n\n# Main\n$eat_steps = Array.new\n$flag = false\n$deleted_index = 0\nbecome_eat(original_monster, eaten_monsters)\n\nif $flag\n puts \"YES\"\n $eat_steps.each do |x|\n puts x\n end\nelse\n puts \"NO\"\nend\n\n\n# puts \"-----------------------------\"\n# puts monster_number\n# puts original_monster.to_s\n# puts eaten_number\n# puts eaten_monsters.to_s\n"}, {"source_code": "def can_eat_left? arr, i\n return false unless i > 0\n return arr[i].to_i > arr[i-1].to_i\nend\ndef can_eat_right? arr, i\n return false unless arr.size > i + 1\n return arr[i].to_i > arr[i+1].to_i\nend\n\ndef eat_left arr, i\n $eat_steps << (i+1).to_s + \" L\"\n arr[i-1] += arr[i]\n arr.delete_at(i)\nend\n\ndef eat_right arr, i\n $eat_steps << (i+1).to_s + \" R\"\n arr[i] += arr[i+1]\n arr.delete_at(i+1)\nend\n\ndef become_eat original_monster, eaten_monsters\n puts \"ori:\" + original_monster.to_s\n puts \"eaten:\" + eaten_monsters.to_s\n eaten_monsters.each_with_index do |eaten, index|\n if eat_head(eaten, original_monster) == -1\n $flag = false # this case only puts \"NO\"\n break\n end\n\n if eat_head(eaten, eaten_monsters) == 0\n original_monster.delete_at(0)\n eaten_monsters.delete_at(0)\n become_eat original_monster, eaten_monsters\n end\n end\n\n $flag = true if original_monster.size == 0 && eaten_monsters.size == 0\nend\n\ndef eat_head value, original_monster\n # puts value\n # puts original_monster.to_s\n return -1 if value.to_i < original_monster[0].to_i # this will puts \"NO\"\n return 0 if value.to_i == original_monster[0].to_i # this will delete 2 element in 2 array, and continue to eat\n\n if can_eat_right?(original_monster, 0)\n eat_right(original_monster,0)\n return eat_head(value,original_monster) # try to eat as possible\n end\n\n (0..original_monster.size - 1).each do |x|\n if can_eat_right?(original_monster, x)\n eat_right(original_monster, x)\n return eat_head(value,original_monster)\n end\n if can_eat_left?(original_monster, x)\n eat_left(original_monster, x)\n return eat_head(value,original_monster)\n end\n end\n\nend\n\n# input processor\nmonster_number = gets.to_i\noriginal_monster = Array.new\na = gets\nmonsters = a.split(\" \")\nmonsters.each do |x|\n original_monster << x.to_i\nend\n\neaten_number = gets.to_i\nb = gets\ne_monsters = b.split(\" \")\neaten_monsters = Array.new\ne_monsters.each do |x|\n eaten_monsters << x.to_i\nend\n\n# Main\n$eat_steps = Array.new\n$flag = false\nbecome_eat(original_monster, eaten_monsters)\n\nif $flag\n puts \"YES\"\n $eat_steps.each do |x|\n puts x\n end\nelse\n puts \"NO\"\nend\n\n\n# puts \"-----------------------------\"\n# puts monster_number\n# puts original_monster.to_s\n# puts eaten_number\n# puts eaten_monsters.to_s\n"}, {"source_code": "def can_eat_left? arr, i\n return false unless i > 0\n return arr[i].to_i > arr[i-1].to_i\nend\ndef can_eat_right? arr, i\n return false unless arr.size > i + 1\n return arr[i].to_i > arr[i+1].to_i\nend\n\ndef eat_left arr, i\n $eat_steps << (i + 1 + $deleted_index).to_s + \" L\"\n arr[i-1] += arr[i]\n arr.delete_at(i)\nend\n\ndef eat_right arr, i\n $eat_steps << (i + 1 + $deleted_index).to_s + \" R\"\n arr[i] += arr[i+1]\n arr.delete_at(i+1)\nend\n\ndef become_eat original_monster, eaten_monsters\n # puts \"ori:\" + original_monster.to_s\n # puts \"eaten:\" + eaten_monsters.to_s\n eaten_monsters.each_with_index do |eaten, index|\n sw = eat_head(eaten, original_monster)\n if sw == -1\n $flag = false # this case only puts \"NO\"\n break\n end\n\n if sw == 0\n original_monster.delete_at(0)\n eaten_monsters.delete_at(0)\n $deleted_index += 1\n return become_eat(original_monster, eaten_monsters)\n end\n end\n\n $flag = true if original_monster.size == 0 && eaten_monsters.size == 0\nend\n\ndef eat_head value, original_monster\n # puts value\n # puts original_monster.to_s\n return -1 if value.to_i < original_monster[0].to_i # this will puts \"NO\"\n return 0 if value.to_i == original_monster[0].to_i # this will delete 2 element in 2 array, and continue to eat\n\n if can_eat_right?(original_monster, 0)\n eat_right(original_monster,0)\n return eat_head(value,original_monster) # try to eat as possible\n end\n\n (1..original_monster.size - 1).each do |x|\n if can_eat_left?(original_monster, x) # always try to eat left first\n eat_left(original_monster, x)\n return eat_head(value,original_monster)\n end\n\n if can_eat_right?(original_monster, x) # tr to eat right after\n eat_right(original_monster, x)\n return eat_head(value,original_monster)\n end\n end\n\n return -1 # all monsters canot eat_left && canot eat_right\n\nend\n\n# input processor\nmonster_number = gets.to_i\noriginal_monster = Array.new\na = gets\nmonsters = a.split(\" \")\nmonsters.each do |x|\n original_monster << x.to_i\nend\n\neaten_number = gets.to_i\nb = gets\ne_monsters = b.split(\" \")\neaten_monsters = Array.new\ne_monsters.each do |x|\n eaten_monsters << x.to_i\nend\n\n# Main\n$eat_steps = Array.new\n$flag = false\n$deleted_index = 0\nbecome_eat(original_monster, eaten_monsters)\n\nif $flag\n puts \"YES\"\n $eat_steps.each do |x|\n puts x\n end\nelse\n puts \"NO\"\nend\n\n\n# puts \"-----------------------------\"\n# puts monster_number\n# puts original_monster.to_s\n# puts eaten_number\n# puts eaten_monsters.to_s\n"}, {"source_code": "def can_eat_left? arr, i\n return false unless i > 0\n return arr[i] > arr[i-1]\nend\ndef can_eat_right? arr, i\n return false unless arr.size > i + 1\n return arr[i] > arr[i+1]\nend\n\ndef eat_left arr, i\n puts i.to_s + \" L\"\n arr[i-1] += arr[i]\n arr.delete_at(i)\nend\n\ndef eat_right arr, i\n puts i.to_s + \" R\"\n arr[i] += arr[i+1]\n arr.delete_at(i+1)\nend\n\ndef become_eat original_monster, eaten_monsters\n eaten_monsters.each_with_index do |eaten, index|\n if eat_head(eaten, eaten_monsters) == -1\n puts \"NO\"\n return\n end\n\n if eat_head(eaten, eaten_monsters) == 0\n original_monster.delete_at(0)\n eaten_monsters.delete_at(0)\n become_eat original_monster, eaten_monsters\n end\n end\nend\n\ndef eat_head value, eaten_monsters\n return -1 if value.to_i < eaten_monsters[0].to_i # this will puts \"NO\"\n return 0 if value.to_i == eaten_monsters[0].to_i # this will delete 2 element in 2 array, and continue to eat\n\n if can_eat_right(eaten_monsters, 0)\n eat_right(eaten_monsters,0)\n eat_head(value,eaten_monsters) # try to eat as possible\n end\n\n (0..eaten_monsters.size - 1).each do |x|\n if can_eat_right(eaten_monsters, x)\n eat_right(eaten_monsters, x)\n eat_head(value,eaten_monsters)\n end\n if can_eat_left(eaten_monsters, x)\n eat_left(eaten_monsters, x)\n eat_head(value,eaten_monsters)\n end\n end\n\nend\n\n# input processor\nmonster_number = gets.to_i\noriginal_monster = Array.new\na = gets\nmonsters = a.split(\" \")\nmonsters.each do |x|\n original_monster << x.to_i\nend\n\neaten_number = gets.to_i\nb = gets\ne_monsters = b.split(\" \")\neaten_monsters = Array.new\ne_monsters.each do |x|\n eaten_monsters << x.to_i\nend\nputs \"-----------------------------\"\nputs monster_number\nputs original_monster.to_s\nputs eaten_number\nputs eaten_monsters.to_s\n"}, {"source_code": "def can_eat_left? arr, i\n return false unless i > 0\n return arr[i].to_i > arr[i-1].to_i\nend\n\ndef can_eat_right? arr, i\n return false unless arr.size > i + 1\n return arr[i].to_i > arr[i+1].to_i\nend\n\ndef max_eat_other now_eating, now_index\t\t\t# sure max element eat other \n\twhile now_eating.size > 1\n\t\tif can_eat_left? now_eating, now_index\n\t\t\teat_left now_eating, now_index\n\t\t\tnow_index = now_index -1\n\t\telse can_eat_right? now_eating, now_index\n\t\t\teat_right now_eating, now_index\n\t\tend\n\tend\nend\n\ndef find_max eating_array\t\t# return 1 max elemnt and suggest it to eat left or right,return -999 if can not eating\n\tindex_all_max = Array.new\t\t\t\t#m contain all max element of array\n\tmulti_all_max = Array.new\n\tmy_max = eating_array.max\n\teating_array.each_with_index do |x, index|\n\t\tif x == my_max\n\t\t\tindex_all_max.push(index)\n\t\tend\n\tend\n\tif index_all_max.size == 1\n\t\tif can_eat_left? eating_array, index_all_max[0]\n\t\t\tindex_all_max.push(-1)\t\t\t# suggest eat left\n\t\telsif can_eat_right? eating_array, index_all_max[0]\n\t\t\tindex_all_max.push(1)\t\t\t# suggest eat right\n\t\telse\n\t\t\tindex_all_max.push(0)\t\t\t# just enought not eat any more\n\t\tend\n\t\tindex_all_max\n\telse\n\t\tindex_all_max.each do |x|\n\t\t\tif can_eat_left? eating_array, x\n\t\t\t\tmulti_all_max.push(x)\n\t\t\t\tmulti_all_max.push(-1)\n\t\t\t\treturn multi_all_max\n\t\t\tend\n\t\t\tif can_eat_right? eating_array, x\n\t\t\t\tputs \"checking with\" + eating_array.to_s + \"x:\" + x.to_s\n\t\t\t\tmulti_all_max.push(x)\n\t\t\t\tmulti_all_max.push(1)\n\t\t\t\treturn multi_all_max\n\t\t\tend\n\t\tend\n\t\tmulti_all_max.push(-999)\t\t# in case all monster can not eat other\n\t\treturn multi_all_max\n\tend\n\nend\n\ndef elements_number elements, total\n\tnum = 0\n\telements.each do |x|\n\t\ttotal = total - x\n\t\tnum += 1\n\t\tif total == 0\n\t\t\treturn num\n\t\telsif total < 0\n\t\t\treturn -999\n\t\tend\n\tend\n\treturn -999\t\t# incase can no make total equal head number\nend\n\ndef eat_from_head eating_array, num\n\t#puts eating_array.to_s\n\t#puts num\n\t#puts \"xxxxxx-\"\n\tnumber_of_attends = elements_number eating_array, num\n\t#puts \"number_of_attends\" + number_of_attends.to_s\n\tif number_of_attends < 0\n\t\t$can_eat_flag = false\n\t\treturn -999\n\tend\n\tnow_eating = eating_array.slice(0, number_of_attends)\n\t#puts \"slice \" + now_eating.to_s\n\t# check before eat\n\tsub_max = find_max now_eating\n\t#puts \"submax\" + sub_max.to_s\n\tif sub_max[0] < 0 \t# -999\n\t\treturn -999\n\telse\n\t\tif sub_max[1] < 0\n\t\t\teat_left now_eating, sub_max[0]\n\t\t\tsub_max[0] = sub_max[0] -1\n\t\t\tmax_eat_other now_eating, sub_max[0]\n\t\telsif sub_max[1] > 0\n\t\t\teat_right now_eating, sub_max[0]\n\t\t\t#puts \"now eating\" + now_eating.to_s\n\t\t\tmax_eat_other now_eating, sub_max[0]\n\t\telse\n\t\t\t#do nothing\n\t\tend\n\tend\n\t# eating_array.drop(number_of_attends)\n\t$completed += 1\n\treturn number_of_attends\nend\n\ndef monster_eat original_monster, eaten_monsters\n\t#puts \"x\"\n\tif original_monster.size == 0 && eaten_monsters.size == 0\n\t\treturn true\n\telsif original_monster.size == 0 || eaten_monsters.size == 0\n\t\treturn false\n\tend\n\n\tnumber_of_attends = eat_from_head(original_monster, eaten_monsters[0])\n\tif number_of_attends > 0\n\t\t(0..number_of_attends-1).each do |x|\n\t\t\toriginal_monster.delete_at(0)\n\t\tend\n\t\teaten_monsters.delete_at(0)\n\t\t#puts original_monster.to_s\n\t\t#puts eaten_monsters.to_s\n\t\treturn monster_eat original_monster, eaten_monsters\n\telse\n\t\treturn false\n\tend\n\treturn true\nend\n\n#===============#\ndef eat_left arr, i\n $eat_steps << (i + 1 + $completed).to_s + \" L\"\n arr[i-1] += arr[i]\n arr.delete_at(i)\nend\n\ndef eat_right arr, i\n $eat_steps << (i + 1 + $completed).to_s + \" R\"\n arr[i] += arr[i+1]\n arr.delete_at(i+1)\nend\n#===============#\n\n#========= MAIN ===========#\n$completed = 0\n$can_eat_flag = false\n$eat_steps = Array.new\n\nmonster_number = gets.to_i\noriginal_monster = Array.new\na = gets\nmonsters = a.split(\" \")\nmonsters.each do |x|\n original_monster << x.to_i\nend\n\neaten_number = gets.to_i\nb = gets\ne_monsters = b.split(\" \")\neaten_monsters = Array.new\ne_monsters.each do |x|\n eaten_monsters << x.to_i\nend\n\n$can_eat_flag = monster_eat(original_monster, eaten_monsters)\nif $can_eat_flag\n puts \"YES\"\n $eat_steps.each do |x|\n puts x\n end\nelse\n puts \"NO\"\nend\n\n#========= MAIN ===========#"}, {"source_code": "def can_eat_left? arr, i\n return false unless i > 0\n return arr[i].to_i > arr[i-1].to_i\nend\ndef can_eat_right? arr, i\n return false unless arr.size > i + 1\n return arr[i].to_i > arr[i+1].to_i\nend\n\ndef eat_left arr, i\n $eat_steps << (i+1).to_s + \" L\"\n arr[i-1] += arr[i]\n arr.delete_at(i)\nend\n\ndef eat_right arr, i\n $eat_steps << (i+1).to_s + \" R\"\n arr[i] += arr[i+1]\n arr.delete_at(i+1)\nend\n\ndef become_eat original_monster, eaten_monsters\n # puts \"ori:\" + original_monster.to_s\n # puts \"eaten:\" + eaten_monsters.to_s\n eaten_monsters.each_with_index do |eaten, index|\n if eat_head(eaten, original_monster) == -1\n $flag = false # this case only puts \"NO\"\n break\n end\n\n if eat_head(eaten, eaten_monsters) == 0\n original_monster.delete_at(0)\n eaten_monsters.delete_at(0)\n become_eat original_monster, eaten_monsters\n end\n end\n\n $flag = true if original_monster.size == 0 && eaten_monsters.size == 0\nend\n\ndef eat_head value, original_monster\n # puts value\n # puts original_monster.to_s\n return -1 if value.to_i < original_monster[0].to_i # this will puts \"NO\"\n return 0 if value.to_i == original_monster[0].to_i # this will delete 2 element in 2 array, and continue to eat\n\n if can_eat_right?(original_monster, 0)\n eat_right(original_monster,0)\n eat_head(value,original_monster) # try to eat as possible\n end\n\n (0..original_monster.size - 1).each do |x|\n if can_eat_right?(original_monster, x)\n eat_right(original_monster, x)\n return eat_head(value,original_monster)\n end\n if can_eat_left?(original_monster, x)\n eat_left(original_monster, x)\n return eat_head(value,original_monster)\n end\n end\n\nend\n\n# input processor\nmonster_number = gets.to_i\noriginal_monster = Array.new\na = gets\nmonsters = a.split(\" \")\nmonsters.each do |x|\n original_monster << x.to_i\nend\n\neaten_number = gets.to_i\nb = gets\ne_monsters = b.split(\" \")\neaten_monsters = Array.new\ne_monsters.each do |x|\n eaten_monsters << x.to_i\nend\n\n# Main\n$eat_steps = Array.new\n$flag = false\nbecome_eat(original_monster, eaten_monsters)\n\nif $flag\n puts \"YES\"\n $eat_steps.each do |x|\n puts x\n end\nelse\n puts \"NO\"\nend\n\n\n# puts \"-----------------------------\"\n# puts monster_number\n# puts original_monster.to_s\n# puts eaten_number\n# puts eaten_monsters.to_s\n"}, {"source_code": "def can_eat_left? arr, i\n return false unless i > 0\n return arr[i].to_i > arr[i-1].to_i\nend\ndef can_eat_right? arr, i\n return false unless arr.size > i + 1\n return arr[i].to_i > arr[i+1].to_i\nend\n\ndef eat_left arr, i\n $eat_steps << (i + 1 + $deleted_index).to_s + \" L\"\n arr[i-1] += arr[i]\n arr.delete_at(i)\nend\n\ndef eat_right arr, i\n $eat_steps << (i + 1 + $deleted_index).to_s + \" R\"\n arr[i] += arr[i+1]\n arr.delete_at(i+1)\nend\n\ndef become_eat original_monster, eaten_monsters\n # puts \"ori:\" + original_monster.to_s\n # puts \"eaten:\" + eaten_monsters.to_s\n eaten_monsters.each_with_index do |eaten, index|\n sw = eat_head(eaten, original_monster)\n if sw == -1\n $flag = false # this case only puts \"NO\"\n break\n end\n\n if sw == 0\n original_monster.delete_at(0)\n eaten_monsters.delete_at(0)\n $deleted_index += 1\n return become_eat(original_monster, eaten_monsters)\n end\n end\n\n $flag = true if original_monster.size == 0 && eaten_monsters.size == 0\nend\n\ndef eat_head value, original_monster\n # puts value\n # puts original_monster.to_s\n return -1 if value.to_i < original_monster[0].to_i # this will puts \"NO\"\n return 0 if value.to_i == original_monster[0].to_i # this will delete 2 element in 2 array, and continue to eat\n\n if can_eat_right?(original_monster, 0)\n eat_right(original_monster,0)\n return eat_head(value,original_monster) # try to eat as possible\n end\n\n (1..original_monster.size - 1).each do |x|\n if can_eat_left?(original_monster, x) # always try to eat left first\n eat_left(original_monster, x)\n return eat_head(value,original_monster)\n end\n\n if can_eat_right?(original_monster, x) # tr to eat right after\n eat_right(original_monster, x)\n return eat_head(value,original_monster)\n end\n end\n\nend\n\n# input processor\nmonster_number = gets.to_i\noriginal_monster = Array.new\na = gets\nmonsters = a.split(\" \")\nmonsters.each do |x|\n original_monster << x.to_i\nend\n\neaten_number = gets.to_i\nb = gets\ne_monsters = b.split(\" \")\neaten_monsters = Array.new\ne_monsters.each do |x|\n eaten_monsters << x.to_i\nend\n\n# Main\n$eat_steps = Array.new\n$flag = false\n$deleted_index = 0\nbecome_eat(original_monster, eaten_monsters)\n\nif $flag\n puts \"YES\"\n $eat_steps.each do |x|\n puts x\n end\nelse\n puts \"NO\"\nend\n\n\n# puts \"-----------------------------\"\n# puts monster_number\n# puts original_monster.to_s\n# puts eaten_number\n# puts eaten_monsters.to_s\n"}, {"source_code": "def can_eat_left? arr, i\n return false unless i > 0\n return arr[i].to_i > arr[i-1].to_i\nend\ndef can_eat_right? arr, i\n return false unless arr.size > i + 1\n return arr[i].to_i > arr[i+1].to_i\nend\n\ndef eat_left arr, i\n if $reveser_only1\n $eat_steps << (arr.size + 1 - (i + 1 + $deleted_index)).to_s + \"R\"\n else\n $eat_steps << (i + 1 + $deleted_index).to_s + \" L\"\n end\n\n arr[i-1] += arr[i]\n arr.delete_at(i)\nend\n\ndef eat_right arr, i\n if $reveser_only1\n $eat_steps << (arr.size + 1 - (i + 1 + $deleted_index)).to_s + \"L\"\n else\n $eat_steps << (i + 1 + $deleted_index).to_s + \" R\"\n end\n arr[i] += arr[i+1]\n arr.delete_at(i+1)\nend\n\ndef become_eat original_monster, eaten_monsters\n # puts \"ori:\" + original_monster.to_s\n # puts \"eaten:\" + eaten_monsters.to_s\n eaten_monsters.each_with_index do |eaten, index|\n sw = eat_head(eaten, original_monster)\n if sw == -1\n $flag = false # this case only puts \"NO\"\n break\n end\n\n if sw == 0\n original_monster.delete_at(0)\n eaten_monsters.delete_at(0)\n $deleted_index += 1\n return become_eat(original_monster, eaten_monsters)\n end\n end\n\n # For case need to eat from tail\n if original_monster.size > 0 && eaten_monsters.size > 0 && $reveser_only1\n $reveser_only1 = false\n original_monster.reverse!\n eaten_monsters.reverse!\n return become_eat(original_monster, eaten_monsters)\n end\n\n $flag = true if original_monster.size == 0 && eaten_monsters.size == 0\nend\n\ndef eat_head value, original_monster\n # puts value\n # puts original_monster.to_s\n return -1 if value.to_i < original_monster[0].to_i # this will puts \"NO\"\n return 0 if value.to_i == original_monster[0].to_i # this will delete 2 element in 2 array, and continue to eat\n\n if can_eat_right?(original_monster, 0)\n eat_right(original_monster,0)\n return eat_head(value,original_monster) # try to eat as possible\n end\n\n (1..original_monster.size - 1).each do |x|\n if can_eat_left?(original_monster, x) # always try to eat left first\n eat_left(original_monster, x)\n return eat_head(value,original_monster)\n end\n\n if can_eat_right?(original_monster, x) # tr to eat right after\n eat_right(original_monster, x)\n return eat_head(value,original_monster)\n end\n end\n\n return -1 # all monsters canot eat_left && canot eat_right\n\nend\n\n\n\n# input processor\nmonster_number = gets.to_i\noriginal_monster = Array.new\na = gets\nmonsters = a.split(\" \")\nmonsters.each do |x|\n original_monster << x.to_i\nend\n\neaten_number = gets.to_i\nb = gets\ne_monsters = b.split(\" \")\neaten_monsters = Array.new\ne_monsters.each do |x|\n eaten_monsters << x.to_i\nend\n\n# Main\n$eat_steps = Array.new\n$flag = false\n$reveser_only1 = true\n$deleted_index = 0\nbecome_eat(original_monster, eaten_monsters)\n\nif $flag\n puts \"YES\"\n $eat_steps.each do |x|\n puts x\n end\nelse\n puts \"NO\"\nend\n\n\n# puts \"-----------------------------\"\n# puts monster_number\n# puts original_monster.to_s\n# puts eaten_number\n# puts eaten_monsters.to_s\n"}, {"source_code": "def can_eat_left? arr, i\n return false unless i > 0\n return arr[i].to_i > arr[i-1].to_i\nend\ndef can_eat_right? arr, i\n return false unless arr.size > i + 1\n return arr[i].to_i > arr[i+1].to_i\nend\n\ndef eat_left arr, i\n $eat_steps << (i + 1 + $deleted_index).to_s + \" L\"\n arr[i-1] += arr[i]\n arr.delete_at(i)\nend\n\ndef eat_right arr, i\n $eat_steps << (i + 1 + $deleted_index).to_s + \" R\"\n arr[i] += arr[i+1]\n arr.delete_at(i+1)\nend\n\ndef become_eat original_monster, eaten_monsters\n # puts \"ori:\" + original_monster.to_s\n # puts \"eaten:\" + eaten_monsters.to_s\n eaten_monsters.each_with_index do |eaten, index|\n sw = eat_head(eaten, original_monster)\n if sw == -1\n $flag = false # this case only puts \"NO\"\n break\n end\n\n if sw == 0\n original_monster.delete_at(0)\n eaten_monsters.delete_at(0)\n $deleted_index += 1\n return become_eat(original_monster, eaten_monsters)\n end\n end\n\n $flag = true if original_monster.size == 0 && eaten_monsters.size == 0\nend\n\ndef eat_head value, original_monster\n # puts value\n # puts original_monster.to_s\n return -1 if value.to_i < original_monster[0].to_i # this will puts \"NO\"\n return 0 if value.to_i == original_monster[0].to_i # this will delete 2 element in 2 array, and continue to eat\n\n if can_eat_right?(original_monster, 0)\n eat_right(original_monster,0)\n return eat_head(value,original_monster) # try to eat as possible\n end\n\n (1..original_monster.size - 1).each do |x|\n if can_eat_left?(original_monster, x) # always try to eat left first\n eat_left(original_monster, x)\n return eat_head(value,original_monster)\n end\n\n if can_eat_right?(original_monster, x) # tr to eat right after\n eat_right(original_monster, x)\n return eat_head(value,original_monster)\n end\n\n return -1 # canot eat_left && canot eat_right\n\n end\n\nend\n\n# input processor\nmonster_number = gets.to_i\noriginal_monster = Array.new\na = gets\nmonsters = a.split(\" \")\nmonsters.each do |x|\n original_monster << x.to_i\nend\n\neaten_number = gets.to_i\nb = gets\ne_monsters = b.split(\" \")\neaten_monsters = Array.new\ne_monsters.each do |x|\n eaten_monsters << x.to_i\nend\n\n# Main\n$eat_steps = Array.new\n$flag = false\n$deleted_index = 0\nbecome_eat(original_monster, eaten_monsters)\n\nif $flag\n puts \"YES\"\n $eat_steps.each do |x|\n puts x\n end\nelse\n puts \"NO\"\nend\n\n\n# puts \"-----------------------------\"\n# puts monster_number\n# puts original_monster.to_s\n# puts eaten_number\n# puts eaten_monsters.to_s\n"}], "src_uid": "a37f805292e68bc0ad4555fa32d180ef"} {"nl": {"description": "n people came to a party. Then those, who had no friends among people at the party, left. Then those, who had exactly 1 friend among those who stayed, left as well. Then those, who had exactly 2, 3, ..., n - 1 friends among those who stayed by the moment of their leaving, did the same.What is the maximum amount of people that could stay at the party in the end? ", "input_spec": "The first input line contains one number t — amount of tests (1 ≤ t ≤ 105). Each of the following t lines contains one integer number n (1 ≤ n ≤ 105).", "output_spec": "For each test output in a separate line one number — the maximum amount of people that could stay in the end.", "sample_inputs": ["1\n3"], "sample_outputs": ["1"], "notes": null}, "positive_code": [{"source_code": "gets.to_i.times do\n n = gets.to_i\n if n < 3\n puts 0\n else\n puts n - 2\n end\nend\n"}, {"source_code": "gets.to_i.times{|i|puts [0,gets.to_i-2].max}"}, {"source_code": "gets.to_i.times{puts [0,gets.to_i-2].max}"}, {"source_code": "puts readlines[1..-1].map{|_| [_.to_i - 2, 0].max} * \"\\n\"\n"}, {"source_code": "readlines[1..-1].map{|_| p [_.to_i - 2, 0].max}\n"}, {"source_code": "t=gets.to_i\nt.times{\n n=gets.to_i\n puts [n-2,0].max\n}"}, {"source_code": "gets.to_i.times{puts [0,gets.to_i-2].max}\n"}, {"source_code": "gets.to_i.times{puts [0,gets.to_i-2].max}\n"}, {"source_code": "gets.to_i.times{puts [0,gets.to_i-2].max}"}, {"source_code": "gets.chomp.to_i.times{puts((t=gets.chomp.to_i)<=2?0: t-2)}\n"}, {"source_code": "gets.to_i.times{puts [0,gets.to_i-2].max}\n"}, {"source_code": "gets.to_i.times{puts [0,gets.to_i-2].max}\n"}, {"source_code": "gets.to_i.times{puts [0,gets.to_i-2].max}"}, {"source_code": "gets.to_i.times{puts [0,gets.to_i-2].max}\n"}, {"source_code": "gets.to_i.times{puts [0,gets.to_i-2].max}\n"}, {"source_code": "gets.to_i.times { p [gets.to_i - 2, 0].max }\n"}, {"source_code": "gets.to_i.times{puts [0,gets.to_i-2].max}\n"}, {"source_code": "gets.to_i.times{puts [0,gets.to_i-2].max}\n"}], "negative_code": [{"source_code": "gets.to_i.times do\n puts gets.to_i / 3\nend\n"}, {"source_code": "gets.to_i.times do\n n = gets.to_i\n total = n / 4 * 2\n total += 1 if n % 4 == 3\n puts total\nend\n"}, {"source_code": "t=gets.to_i\nt.times{\n n=gets.to_i\n puts n-2\n}\n"}, {"source_code": "gets.chomp.to_i.times{puts(gets.chomp.to_i-2)}\n"}, {"source_code": "gets.to_i.times do\n\tn = gets.to_i\n\tp n / 4 * 2 + (n % 4 == 3 ? 1 : 0)\nend\n"}, {"source_code": "gets.to_i.times { p gets.to_i / 3 }\n"}], "src_uid": "f83c91c9e9252aba4736aa0bea82493b"} {"nl": {"description": "Polycarp has $$$n$$$ coins, the value of the $$$i$$$-th coin is $$$a_i$$$. It is guaranteed that all the values are integer powers of $$$2$$$ (i.e. $$$a_i = 2^d$$$ for some non-negative integer number $$$d$$$).Polycarp wants to know answers on $$$q$$$ queries. The $$$j$$$-th query is described as integer number $$$b_j$$$. The answer to the query is the minimum number of coins that is necessary to obtain the value $$$b_j$$$ using some subset of coins (Polycarp can use only coins he has). If Polycarp can't obtain the value $$$b_j$$$, the answer to the $$$j$$$-th query is -1.The queries are independent (the answer on the query doesn't affect Polycarp's coins).", "input_spec": "The first line of the input contains two integers $$$n$$$ and $$$q$$$ ($$$1 \\le n, q \\le 2 \\cdot 10^5$$$) — the number of coins and the number of queries. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ — values of coins ($$$1 \\le a_i \\le 2 \\cdot 10^9$$$). It is guaranteed that all $$$a_i$$$ are integer powers of $$$2$$$ (i.e. $$$a_i = 2^d$$$ for some non-negative integer number $$$d$$$). The next $$$q$$$ lines contain one integer each. The $$$j$$$-th line contains one integer $$$b_j$$$ — the value of the $$$j$$$-th query ($$$1 \\le b_j \\le 10^9$$$).", "output_spec": "Print $$$q$$$ integers $$$ans_j$$$. The $$$j$$$-th integer must be equal to the answer on the $$$j$$$-th query. If Polycarp can't obtain the value $$$b_j$$$ the answer to the $$$j$$$-th query is -1.", "sample_inputs": ["5 4\n2 4 8 2 4\n8\n5\n14\n10"], "sample_outputs": ["1\n-1\n3\n2"], "notes": null}, "positive_code": [{"source_code": "N, Q = gets.split.map(&:to_i)\nas = gets.split.map(&:to_i)\nhash = Hash.new(0)\nas.each do |a|\n hash[a] += 1\nend\ncoins = hash.keys.sort.reverse\n\nQ.times do |i|\n n = gets.to_i\n ans = 0\n coins.each do |c|\n result = n / c\n result = hash[c] if result > hash[c]\n n -= result*c\n ans += result\n end\n puts n == 0 ? ans : -1\nend"}], "negative_code": [], "src_uid": "a5c38d4842d3d4652cd79dd9715c138d"} {"nl": {"description": "Alice and Borys are playing tennis.A tennis match consists of games. In each game, one of the players is serving and the other one is receiving.Players serve in turns: after a game where Alice is serving follows a game where Borys is serving, and vice versa.Each game ends with a victory of one of the players. If a game is won by the serving player, it's said that this player holds serve. If a game is won by the receiving player, it's said that this player breaks serve.It is known that Alice won $$$a$$$ games and Borys won $$$b$$$ games during the match. It is unknown who served first and who won which games.Find all values of $$$k$$$ such that exactly $$$k$$$ breaks could happen during the match between Alice and Borys in total.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 10^3$$$). Description of the test cases follows. Each of the next $$$t$$$ lines describes one test case and contains two integers $$$a$$$ and $$$b$$$ ($$$0 \\le a, b \\le 10^5$$$; $$$a + b > 0$$$) — the number of games won by Alice and Borys, respectively. It is guaranteed that the sum of $$$a + b$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case print two lines. In the first line, print a single integer $$$m$$$ ($$$1 \\le m \\le a + b + 1$$$) — the number of values of $$$k$$$ such that exactly $$$k$$$ breaks could happen during the match. In the second line, print $$$m$$$ distinct integers $$$k_1, k_2, \\ldots, k_m$$$ ($$$0 \\le k_1 < k_2 < \\ldots < k_m \\le a + b$$$) — the sought values of $$$k$$$ in increasing order.", "sample_inputs": ["3\n2 1\n1 1\n0 5"], "sample_outputs": ["4\n0 1 2 3\n2\n0 2\n2\n2 3"], "notes": "NoteIn the first test case, any number of breaks between $$$0$$$ and $$$3$$$ could happen during the match: Alice holds serve, Borys holds serve, Alice holds serve: $$$0$$$ breaks; Borys holds serve, Alice holds serve, Alice breaks serve: $$$1$$$ break; Borys breaks serve, Alice breaks serve, Alice holds serve: $$$2$$$ breaks; Alice breaks serve, Borys breaks serve, Alice breaks serve: $$$3$$$ breaks. In the second test case, the players could either both hold serves ($$$0$$$ breaks) or both break serves ($$$2$$$ breaks).In the third test case, either $$$2$$$ or $$$3$$$ breaks could happen: Borys holds serve, Borys breaks serve, Borys holds serve, Borys breaks serve, Borys holds serve: $$$2$$$ breaks; Borys breaks serve, Borys holds serve, Borys breaks serve, Borys holds serve, Borys breaks serve: $$$3$$$ breaks. "}, "positive_code": [{"source_code": "t = gets.to_i\nt.times do\n\na, b = gets.split.map!(&:to_i).sort!\nc = a + b\n\nif c.even?\n h = c / 2\n x = (h - a).step(h + a, 2).to_a\nelse\n h1 = c.pred / 2\n h2 = c.succ / 2\n x = (h1 - a).step(h2 + a).to_a\nend\n\nputs x.size\nputs x * \" \"\n\nend\n"}], "negative_code": [], "src_uid": "04a37e9c68761f9a2588c0bbecad2147"} {"nl": {"description": "Polycarp is very careful. He even types numeric sequences carefully, unlike his classmates. If he sees a sequence without a space after the comma, with two spaces in a row, or when something else does not look neat, he rushes to correct it. For example, number sequence written like \"1,2 ,3,...,   10\" will be corrected to \"1, 2, 3, ..., 10\".In this task you are given a string s, which is composed by a concatination of terms, each of which may be: a positive integer of an arbitrary length (leading zeroes are not allowed), a \"comma\" symbol (\",\"), a \"space\" symbol (\" \"), \"three dots\" (\"...\", that is, exactly three points written one after another, also known as suspension points). Polycarp wants to add and remove spaces in the string s to ensure the following: each comma is followed by exactly one space (if the comma is the last character in the string, this rule does not apply to it), each \"three dots\" term is preceded by exactly one space (if the dots are at the beginning of the string, this rule does not apply to the term), if two consecutive numbers were separated by spaces only (one or more), then exactly one of them should be left, there should not be other spaces. Automate Polycarp's work and write a program that will process the given string s.", "input_spec": "The input data contains a single string s. Its length is from 1 to 255 characters. The string s does not begin and end with a space. Its content matches the description given above.", "output_spec": "Print the string s after it is processed. Your program's output should be exactly the same as the expected answer. It is permissible to end output line with a line-break character, and without it.", "sample_inputs": ["1,2 ,3,..., 10", "1,,,4...5......6", "...,1,2,3,..."], "sample_outputs": ["1, 2, 3, ..., 10", "1, , , 4 ...5 ... ...6", "..., 1, 2, 3, ..."], "notes": null}, "positive_code": [{"source_code": "NUMBER_REGEX = /(\\d+)/\n\ntokens = gets.strip.gsub(',', ' , ').gsub('...', ' ... ').split\n\ntokens.each_with_index do |token, index|\n if index.zero?\n print token\n else\n print ' ' if tokens[index - 1] == ',' or token == '...'\n print ' ' if tokens[index - 1] =~ NUMBER_REGEX and token =~ NUMBER_REGEX\n print token\n end\nend\n\nputs\n"}, {"source_code": "line = gets.chomp\nline.gsub!(/ *, */, ', ')\nline.gsub!(/ *\\.\\.\\. */, ' ...')\nline.gsub!(/ +/, ' ')\nline.strip!\nputs line"}, {"source_code": "line = gets.chomp\nline.gsub!(/ *, */, ', ')\nline.gsub!(/ *\\.\\.\\. */, ' ...')\nline.gsub!(/ +/, ' ')\nline.strip!\nputs line\n"}, {"source_code": "\nwhile s = gets\n s = s.chomp.strip\n # puts \"(#{s})\"\n s.gsub!(/([,.])\\s+([,.])/, '\\1\\2')\n s.gsub!(/([,.])\\s+([,.])/, '\\1\\2')\n\n s.gsub!(/([,.])\\s+([\\d])/, '\\1\\2')\n s.gsub!(/([,.])\\s+([\\d])/, '\\1\\2')\n\n s.gsub!(/([\\d])\\s+([,.])/, '\\1\\2')\n s.gsub!(/([\\d])\\s+([,.])/, '\\1\\2')\n\n # s.gsub!(/([\\d])\\s+([\\d])/, '\\1 \\2')\n # s.gsub!(/([\\d])\\s+([\\d])/, '\\1 \\2')\n\n # puts \"{#{s}}\"\n s.gsub!(/,/, \", \")\n s.gsub!(/\\.\\.\\./, ' ...')\n s.gsub!(/[\\s]{2,}/, \" \")\n s.strip!\n puts \"#{s}\"\nend\n"}, {"source_code": " line = gets.chomp\n line.gsub!(/ *, */, ', ')\n line.gsub!(/ *\\.\\.\\. */, ' ...')\n line.gsub!(/ +/, ' ')\n line.strip!\n puts line"}, {"source_code": "#!/usr/bin/env ruby\n# -*- coding: utf-8 -*-\n\ns = gets.chomp\ns.gsub!(/ *, */, ', ')\ns.gsub!(/ *\\.\\.\\. */, ' ...')\ns.gsub!(/ +/, ' ')\ns.strip!\nputs s\n\n"}, {"source_code": "puts gets.gsub(/ *,/,', ').gsub(/\\.\\.\\. */,' ...').gsub(/ +/,' ').strip"}, {"source_code": "input = STDIN.readline.gsub(',', ' , ').gsub('...', ' ... ').strip.split(' ').map do |s| s.strip end\n\nspaceIns = true\nishead = true\nisPrevNum = false\ninput.each do |s|\n print ' ' if !ishead && spaceIns\n ishead = false\n case s\n when ','\n print ','\n spaceIns = true\n isPrevNum = false\n when '...'\n print((spaceIns ? '' : ' ') + '...')\n spaceIns = false\n isPrevNum = false\n else\n print((isPrevNum ? ' ' : '') + s)\n spaceIns = false\n isPrevNum = true\n end\nend\nputs ''\n"}, {"source_code": "class String\n def is_integer?\n self.to_i.to_s == self\n end\nend\n\ndef parse(line)\n return line.scan(Regexp.union(/,/, /\\.../, /\\d*/)).map{ |t| t.strip }.select{ |t| t != '' }\nend\n\ndef format(tokens)\n acc = \"\"\n tokens.each_with_index { |val, index| \n if val == \",\"\n acc << \", \"\n elsif val == \"...\" and acc[-1, 1] == \" \"\n acc << val\n elsif val == \"...\"\n acc << \" ...\"\n elsif val.is_integer? and index > 0 and tokens[index - 1].is_integer?\n acc << \" \" << val\n else\n acc << val\n end\n }\n return acc.strip\nend\n\ndef process_line(line)\n tokens = parse(line)\n return format(tokens)\nend\n\ninput = STDIN.read\nlines = input.chomp.split(\"\\n\")\nlines.map! { |l| puts process_line(l) }\n"}], "negative_code": [{"source_code": "line = gets.chomp\nline.gsub!(/, */, ', ')\nline.gsub!(/ *\\.\\.\\./, ' ...')\nline.gsub!(/ +/, ' ')\nline.strip!\nputs line\n"}, {"source_code": "#!/usr/bin/env ruby\n# -*- coding: utf-8 -*-\n\ns = gets\nelems = s.split(/,/)\nelems.map!{|x|\n xx = x.strip\n if xx != \"...\" && xx =~ /\\.\\.\\./\n xx = xx.gsub(/\\.\\.\\./, \" ...\").lstrip\n end\n xx\n}\nputs elems.join(', ')\n\n"}, {"source_code": "#!/usr/bin/env ruby\n# -*- coding: utf-8 -*-\n\ns = gets\nelems = s.split(/,/)\nelems.map!{|x|\n xx = x.strip\n if xx != \"...\" && xx =~ /\\.\\.\\./\n xx = xx.gsub(/\\.\\.\\./, \" ...\")\n end\n xx\n}\nputs elems.join(', ')\n\n"}, {"source_code": "#!/usr/bin/env ruby\n# -*- coding: utf-8 -*-\n\ns = gets\nelems = s.split(/,/)\nelems.map!{|x|\n xx = x.strip\n if xx != \"...\" && xx =~ /\\.\\.\\./\n xx = xx.gsub(/\\.\\.\\./, \" ...\").lstrip\n end\n xx\n}\nputs elems.join(', ').strip\n\n\n"}, {"source_code": "#!/usr/bin/env ruby\n# -*- coding: utf-8 -*-\n\ns = gets\ns.gsub!(/\\s{1,}/, ' ')\nelems = s.split(/,/)\nelems.map!{|x|\n xx = x.strip\n if xx != \"...\" && xx =~ /\\.\\.\\./\n xx = xx.gsub(/\\.\\.\\./, \" ...\").lstrip\n end\n xx\n}\nputs elems.join(', ').strip\n\n"}, {"source_code": "puts gets.gsub(/ *,/,', ').gsub(/\\.\\.\\. */,' ...').gsub(/ +/,' ')"}, {"source_code": "class String\n def is_integer?\n self.to_i.to_s == self\n end\nend\n\ndef parse(line)\n return line.scan(Regexp.union(/,/, /\\.../, /\\d*/)).map{ |t| t.strip }.select{ |t| t != '' }\nend\n\ndef format(tokens)\n acc = \"\"\n tokens.each_with_index { |val, index| \n if val == \",\"\n acc << \", \"\n elsif val == \"...\" and acc[-1, 1] == \" \"\n acc << val\n elsif val == \"...\"\n acc << \" ...\"\n elsif val.is_integer? and index > 1 and tokens[index - 1].is_integer?\n acc << \" \" << val\n else\n acc << val\n end\n }\n return acc\nend\n\ndef process_line(line)\n tokens = parse(line)\n return format(tokens)\nend\n\ninput = STDIN.read\nlines = input.chomp.split(\"\\n\")\nlines.map! { |l| puts process_line(l) }\n"}, {"source_code": "class String\n def is_integer?\n self.to_i.to_s == self\n end\nend\n\ndef parse(line)\n return line.scan(Regexp.union(/,/, /\\.../, /\\d*/)).map{ |t| t.strip }.select{ |t| t != '' }\nend\n\ndef format(tokens)\n# puts tokens\n acc = \"\"\n tokens.each_with_index { |val, index| \n if val == \",\"\n acc << \", \"\n elsif val == \"...\" and acc[-1, 1] == \" \"\n acc << val\n elsif val == \"...\"\n acc << \" ...\"\n elsif val.is_integer? and index > 1 and tokens[index - 1].is_integer?\n acc << \" \" << val\n else\n acc << val\n end\n }\n puts acc\n return tokens.join(\"~\")\nend\n\ndef process_line(line)\n puts line\n tokens = parse(line)\n return format(tokens)\nend\n\ninput = STDIN.read\nlines = input.chomp.split(\"\\n\")\nlines.map! { |l| puts process_line(l) }\n"}, {"source_code": "class String\n def is_integer?\n self.to_i.to_s == self\n end\nend\n\ndef parse(line)\n return line.scan(Regexp.union(/,/, /\\.../, /\\d*/)).map{ |t| t.strip }.select{ |t| t != '' }\nend\n\ndef format(tokens)\n acc = \"\"\n tokens.each_with_index { |val, index| \n if val == \",\"\n acc << \", \"\n elsif val == \"...\" and acc[-1, 1] == \" \"\n acc << val\n elsif val == \"...\"\n acc << \" ...\"\n elsif val.is_integer? and index > 1 and tokens[index - 1].is_integer?\n acc << \" \" << val\n else\n acc << val\n end\n }\n return acc.strip\nend\n\ndef process_line(line)\n tokens = parse(line)\n return format(tokens)\nend\n\ninput = STDIN.read\nlines = input.chomp.split(\"\\n\")\nlines.map! { |l| puts process_line(l) }\n"}], "src_uid": "c7d8c71a1f7e6c7364cce5bddd488a2f"} {"nl": {"description": "The Cereal Guy's friend Serial Guy likes to watch soap operas. An episode is about to start, and he hasn't washed his plate yet. But he decided to at least put in under the tap to be filled with water. The plate can be represented by a parallelepiped k × n × m, that is, it has k layers (the first layer is the upper one), each of which is a rectangle n × m with empty squares ('.') and obstacles ('#'). The water can only be present in the empty squares. The tap is positioned above the square (x, y) of the first layer, it is guaranteed that this square is empty. Every minute a cubical unit of water falls into the plate. Find out in how many minutes the Serial Guy should unglue himself from the soap opera and turn the water off for it not to overfill the plate. That is, you should find the moment of time when the plate is absolutely full and is going to be overfilled in the next moment.Note: the water fills all the area within reach (see sample 4). Water flows in each of the 6 directions, through faces of 1 × 1 × 1 cubes.", "input_spec": "The first line contains three numbers k, n, m (1 ≤ k, n, m ≤ 10) which are the sizes of the plate. Then follow k rectangles consisting of n lines each containing m characters '.' or '#', which represents the \"layers\" of the plate in the order from the top to the bottom. The rectangles are separated by empty lines (see the samples). The last line contains x and y (1 ≤ x ≤ n, 1 ≤ y ≤ m) which are the tap's coordinates. x is the number of the line and y is the number of the column. Lines of each layer are numbered from left to right by the integers from 1 to n, columns of each layer are numbered from top to bottom by the integers from 1 to m.", "output_spec": "The answer should contain a single number, showing in how many minutes the plate will be filled.", "sample_inputs": ["1 1 1\n\n.\n\n1 1", "2 1 1\n\n.\n\n#\n\n1 1", "2 2 2\n\n.#\n##\n\n..\n..\n\n1 1", "3 2 2\n\n#.\n##\n\n#.\n.#\n\n..\n..\n\n1 2", "3 3 3\n\n.#.\n###\n##.\n\n.##\n###\n##.\n\n...\n...\n...\n\n1 1"], "sample_outputs": ["1", "1", "5", "7", "13"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/env ruby\n\n$k, $n, $m = gets.split.map(&:to_i)\n$t = []\n\ndef fs(z, x, y)\n stack = [[z, x, y]]\n $t[z][x][y] = '#'\n #puts \"dfs #{z} #{x} #{y}\"\n result = 1\n while not stack.empty?\n z, x, y = stack.pop\n (-1..1).each do |i|\n (-1..1).each do |j|\n (-1..1).each do |k|\n if [i, j, k].select { |x| x == 0 }.count == 2\n nz, nx, ny = z + i, x + j, y + k\n #puts [i, j, k, nz, nx, ny] * \" \" + [0 <= nz, nz < t.size, 0 <= nx, nx < t[0].size, ny <= 0, ny < t[0][0].size] * \" \"\n if 0 <= nz && nz < $k && 0 <= nx && nx < $n && 0 <= ny && ny < $m\n #puts \"ok\"\n if $t[nz][nx][ny] == '.'\n $t[nz][nx][ny] = '#'\n stack.push([nz, nx, ny])\n result += 1\n end\n end\n end\n end\n end\n end\n end\n\n return result\nend\n\n\n$k.times do\n gets\n $t << []\n $n.times do\n $t[-1] << gets.chomp\n end\nend\ngets\nx, y = gets.split.map(&:to_i)\n\n#p t, x, y, t.size, t[0].size, t[0][0].size\n\nputs fs(0, x - 1, y - 1)"}, {"source_code": "K, N, M = gets.chomp.split.map{|e| e.to_i}\n\nmap = Array.new(K + 1) { Array.new(N + 1) {[]}}\n\n1.upto(K) {|i|\n gets\n 1.upto(N) {|j|\n map[i][j].push 0\n tmp = gets.chomp.split('').each {|e|\n map[i][j].push e\n }\n }\n}\n\ngets\nX, Y = gets.chomp.split.map{|e| e.to_i}\n\ndef dfs(map, k, n, m)\n return 0 if (k < 1 || k > K || n < 1 || n > N || m < 1 || m > M)\n return 0 if (map[k][n][m] != \".\")\n \n count = 1\n map[k][n][m] = \"0\"\n directions = [[0, 0, 1], [0, 0, -1], [0, 1, 0], [0, -1, 0], [1, 0, 0], [-1, 0, 0]]\n\n directions.each {|e|\n count += dfs(map, k + e[0], n + e[1], m + e[2])\n }\n\n return count\nend\n\nputs dfs(map, 1, X, Y)\n\n\n"}, {"source_code": "k, n, m = gets.split.map(&:to_i)\n\ndata = []\nk.times do\n gets\n o = []\n n.times do \n o << gets.chomp\n end\n data << o\nend\n\ngets\nx, y = gets.split.map(&:to_i)\n\nlim = [k, n, m]\nq = [[1, x, y]]\nused = {}\nn = 0\nuntil q.empty?\n pos = q.shift\n\n next if used[pos]\n next if pos.min < 1 || pos.zip(lim).find{|a,b| a>b}\n next if data[pos[0]-1][pos[1]-1][pos[2]-1] != '.'\n\n used[pos] = true\n\n n += 1\n q << [pos[0], pos[1], pos[2]+1]\n q << [pos[0], pos[1], pos[2]-1]\n q << [pos[0], pos[1]+1, pos[2]]\n q << [pos[0], pos[1]-1, pos[2]]\n q << [pos[0]+1, pos[1], pos[2]]\n q << [pos[0]-1, pos[1], pos[2]]\n\nend\n\np n\n"}], "negative_code": [], "src_uid": "3f6e5f6d9a35c6c9e71a7eeab4acf199"} {"nl": {"description": "This problem is same as the previous one, but has larger constraints.Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time.For each of the $$$n$$$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $$$i$$$-th day has a ribbon with color $$$u_i$$$. Shiro wants to know the largest number $$$x$$$, such that if we consider the streak of the first $$$x$$$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $$$x - 1$$$ will have the same number of occurrences.For example, consider the following sequence of $$$u_i$$$: $$$[2, 2, 1, 1, 5, 4, 4, 5]$$$. Then $$$x = 7$$$ makes a streak, since if we remove the leftmost $$$u_i = 5$$$, each ribbon color will appear exactly twice in the prefix of $$$x - 1$$$ days. Note that $$$x = 8$$$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 10^5$$$) — the total number of days. The second line contains $$$n$$$ integers $$$u_1, u_2, \\ldots, u_n$$$ ($$$1 \\leq u_i \\leq 10^5$$$) — the colors of the ribbons the cats wear. ", "output_spec": "Print a single integer $$$x$$$ — the largest possible streak of days.", "sample_inputs": ["13\n1 1 1 2 2 2 3 3 3 4 4 4 5", "5\n10 100 20 200 1", "1\n100000", "7\n3 2 1 1 4 5 1", "6\n1 1 1 2 2 2"], "sample_outputs": ["13", "5", "1", "6", "5"], "notes": "NoteIn the first example, we can choose the longest streak of $$$13$$$ days, since upon removing the last day out of the streak, all of the remaining colors $$$1$$$, $$$2$$$, $$$3$$$, and $$$4$$$ will have the same number of occurrences of $$$3$$$. Note that the streak can also be $$$10$$$ days (by removing the $$$10$$$-th day from this streak) but we are interested in the longest streak.In the fourth example, if we take the streak of the first $$$6$$$ days, we can remove the third day from this streak then all of the remaining colors $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$ and $$$5$$$ will occur exactly once."}, "positive_code": [{"source_code": "n = readline.to_i\nu = readline.split.map(&:to_i)\n\nC = 100000\n\ncs = [0] * C\ncsr = {0 => n}\ncsr.default = 0\nmax_x = 1\n(0...n).each do |i|\n ui = u[i] - 1\n c = cs[ui]\n csr[c] -= 1\n csr.delete(c) if csr[c] == 0\n c += 1\n cs[ui] = c\n csr[c] += 1\n if csr.length <= 3 then\n ks = csr.keys\n ks.delete_if { |x| x == 0 }\n if ks.length == 1 and (ks[0] == 1 or csr[ks[0]] == 1)\n max_x = i + 1\n elsif ks.length == 2\n ks = ks.sort\n if (ks[1] == ks[0] + 1 and csr[ks[1]] == 1) or (ks[0] == 1 and csr[ks[0]] == 1)\n max_x = i + 1\n end\n end\n end\nend\np max_x\n"}, {"source_code": "\nN = gets.to_i\narr = gets.split.map(&:to_i)\nnums = Hash.new(0)\ncounts = Hash.new(0)\nanswer = 0\nfor k in 0...N\n\tx = arr[k]\n\tnums[x] += 1\n\tcnt = nums[x]\n\tif cnt == 1\n\t\tcounts[1] += 1\n\telse\n\t\tcounts[cnt-1] -= 1\n\t\tcounts[cnt] += 1\n\tend\n\tcounts.delete_if { |key, value| value == 0 }\n#\tSTDERR.puts \"#{nums}\"\n#\tSTDERR.puts \"#{counts}\"\n#\tSTDERR.puts counts.size\n\tif counts.size == 1\n\t\tanswer = k if counts.key?(1) or counts.value?(1)\n\telsif counts.size == 2\n\t\tif counts.value?(1)\n\t\t\tanswer = k if counts[1] == 1\n\t\t\ta, b = counts.keys\n\t\t\tif (a - b).abs == 1\n\t\t\t\tmax = a > b ? a : b\n\t\t\t\tanswer = k if counts[max] == 1\n\t\t\tend\n\t\tend\n\tend\nend\nputs answer + 1\n"}, {"source_code": "\nN = gets.to_i\narr = gets.split.map(&:to_i)\nnums = Hash.new(0)\ncounts = Hash.new(0)\nanswer = 0\nfor k in 0...N\n\tcnt = ( nums[ arr[k] ] += 1 )\n\tif cnt == 1\n\t\tcounts[1] += 1\n\telse\n\t\tcounts[cnt-1] -= 1\n\t\tcounts.delete cnt-1 if counts[cnt-1] == 0\n\t\tcounts[cnt] += 1\n\tend\n#\tSTDERR.puts \"#{nums}\"\n#\tSTDERR.puts \"#{counts}\"\n#\tSTDERR.puts counts.size\n\tif counts.size == 1\n\t\tanswer = k if counts.key?(1) or counts.value?(1)\n\telsif counts.size == 2\n\t\tif counts.value?(1)\n\t\t\tif counts[1] == 1\n\t\t\t\tanswer = k\n\t\t\telse\n\t\t\t\ta, b = counts.keys\n\t\t\t\tanswer = k if (a - b).abs == 1 && counts[a > b ? a : b] == 1\n\t\t\tend\n\t\tend\n\tend\nend\nputs answer + 1\n"}], "negative_code": [{"source_code": "\nN = gets.to_i\narr = gets.split.map(&:to_i)\nnums = Hash.new(0)\ncounts = Hash.new(0)\nanswer = 0\nfor k in 0...arr.size\n\tx = arr[k]\n\tnums[x] += 1\n\tcnt = nums[x]\n\tif cnt == 1\n\t\tcounts[1] += 1\n\telse\n\t\tcounts[cnt-1] -= 1\n\t\tcounts[cnt] += 1\n\tend\n\tcounts.delete_if { |key, value| value == 0 }\n#\tSTDERR.puts \"#{nums}\"\n#\tSTDERR.puts \"#{counts}\"\n#\tSTDERR.puts counts.size\n\tif counts.size == 1\n\t\tanswer = k if counts.key?(1) or counts.value?(1)\n\telsif counts.size == 2\n\t\tif counts.value?(1)\n\t\t\tanswer = k if counts[1] == 1\n\t\t\ta, b = counts.keys\n\t\t\tanswer = k if (a - b).abs == 1\n\t\tend\n\tend\nend\nputs answer + 1\n"}, {"source_code": "\nN = gets.to_i\narr = gets.split.map(&:to_i)\nnums = Hash.new(0)\ncounts = Hash.new(0)\nanswer = 0\nfor k in 0...arr.size\n\tx = arr[k]\n\tnums[x] += 1\n\tcnt = nums[x]\n\tif cnt == 1\n\t\tcounts[1] += 1\n\telse\n\t\tcounts[cnt-1] -= 1\n\t\tcounts[cnt] += 1\n\tend\n\tcounts.delete_if { |key, value| value == 0 }\n\tSTDERR.puts \"#{nums}\"\n\tSTDERR.puts \"#{counts}\"\n\tSTDERR.puts counts.size\n\tif counts.size == 1\n\t\tanswer = k if counts.key?(1) or counts.value?(1)\n\telsif counts.size == 2\n\t\tif counts.value?(1)\n\t\t\ta, b = counts.keys\n\t\t\tanswer = k if (a - b).abs == 1\n\t\tend\n\tend\nend\nputs answer + 1"}, {"source_code": "\nN = gets.to_i\narr = gets.split.map(&:to_i)\nnums = Hash.new(0)\ncounts = Hash.new(0)\nanswer = 0\nfor k in 0...N\n\tcnt = ( nums[ arr[k] ] += 1 )\n\tif cnt == 1\n\t\tcounts[1] += 1\n\telse\n\t\tcounts[cnt-1] -= 1\n\t\tcounts.delete cnt-1 if counts[cnt-1] == 0\n\t\tcounts[cnt] += 1\n\tend\n#\tSTDERR.puts \"#{nums}\"\n#\tSTDERR.puts \"#{counts}\"\n#\tSTDERR.puts counts.size\n\tif counts.size == 1\n\t\tanswer = k if counts.key?(1) or counts.value?(1)\n\telsif counts.size == 2\n\t\tif counts.value?(1)\n\t\t\tif counts[1] == 1\n\t\t\t\tanswer = k\n\t\t\telse\n\t\t\t\ta, b = counts.keys\n\t\t\t\tanswer = k if (a - b).abs == 1 || counts[a > b ? a : b] == 1\n\t\t\tend\n\t\tend\n\tend\nend\nputs answer + 1\n"}, {"source_code": "\nN = gets.to_i\narr = gets.split.map(&:to_i)\nnums = Hash.new(0)\ncounts = Hash.new(0)\nanswer = 0\nfor k in 0...arr.size\n\tx = arr[k]\n\tnums[x] += 1\n\tcnt = nums[x]\n\tif cnt == 1\n\t\tcounts[1] += 1\n\telse\n\t\tcounts[cnt-1] -= 1\n\t\tcounts[cnt] += 1\n\tend\n\tcounts.delete_if { |key, value| value == 0 }\n\tSTDERR.puts \"#{nums}\"\n\tSTDERR.puts \"#{counts}\"\n\tSTDERR.puts counts.size\n\tif counts.size == 1\n\t\tanswer = k if counts.key?(1) or counts.value?(1)\n\telsif counts.size == 2\n\t\tif counts.value?(1)\n\t\t\tanswer = k if counts.key?(1)\n\t\t\ta, b = counts.keys\n\t\t\tanswer = k if (a - b).abs == 1\n\t\tend\n\tend\nend\nputs answer + 1"}, {"source_code": "\nN = gets.to_i\narr = gets.split.map(&:to_i)\nnums = Hash.new(0)\ncounts = Hash.new(0)\nanswer = 0\nfor k in 0...N\n\tx = arr[k]\n\tnums[x] += 1\n\tcnt = nums[x]\n\tif cnt == 1\n\t\tcounts[1] += 1\n\telse\n\t\tcounts[cnt-1] -= 1\n\t\tcounts[cnt] += 1\n\tend\n\tcounts.delete_if { |key, value| value == 0 }\n#\tSTDERR.puts \"#{nums}\"\n#\tSTDERR.puts \"#{counts}\"\n#\tSTDERR.puts counts.size\n\tif counts.size == 1\n\t\tanswer = k if counts.key?(1) or counts.value?(1)\n\telsif counts.size == 2\n\t\tif counts.value?(1)\n\t\t\tanswer = k if counts[1] == 1\n\t\t\ta, b = counts.keys\n\t\t\ta, b = b, a if counts[a] == 1\n\t\t\tanswer = k if b - a == 1\n\t\tend\n\tend\nend\nputs answer + 1\n"}], "src_uid": "886d8d7103f0a7403b06b80b914ee498"} {"nl": {"description": "You are given a string A. Find a string B, where B is a palindrome and A is a subsequence of B.A subsequence of a string is a string that can be derived from it by deleting some (not necessarily consecutive) characters without changing the order of the remaining characters. For example, \"cotst\" is a subsequence of \"contest\".A palindrome is a string that reads the same forward or backward.The length of string B should be at most 104. It is guaranteed that there always exists such string.You do not need to find the shortest answer, the only restriction is that the length of string B should not exceed 104.", "input_spec": "First line contains a string A (1 ≤ |A| ≤ 103) consisting of lowercase Latin letters, where |A| is a length of A.", "output_spec": "Output single line containing B consisting of only lowercase Latin letters. You do not need to find the shortest answer, the only restriction is that the length of string B should not exceed 104. If there are many possible B, print any of them.", "sample_inputs": ["aba", "ab"], "sample_outputs": ["aba", "aabaa"], "notes": "NoteIn the first example, \"aba\" is a subsequence of \"aba\" which is a palindrome.In the second example, \"ab\" is a subsequence of \"aabaa\" which is a palindrome."}, "positive_code": [{"source_code": "a = gets.chomp \na.downcase!\nputs a + a.reverse\n\n"}, {"source_code": "s=gets.chomp;puts s+s.reverse"}, {"source_code": "def Pal(s)\n s == s.reverse\nend\n\ns = gets.chomp\nputs Pal(s)? s : s + s[0..-2].reverse"}, {"source_code": "s = gets().strip\nputs s << s.reverse\n"}, {"source_code": "s = gets.chomp\nputs s + s.reverse[0..-1]"}, {"source_code": "s=gets.chomp\nprint s\nfor i in (s.size-1).downto(0)\n print s[i]\n \nend\n"}, {"source_code": "s=gets.chomp\nprint s\nfor i in (s.size-1).downto(0)\n print s[i]\nend\n"}, {"source_code": "a = gets.chomp\nputs a + a.reverse\n"}], "negative_code": [], "src_uid": "9b1887582a9eb7cff49994ddcc2ee9b8"} {"nl": {"description": "Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father's magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a number n. How many times can Gerald put a spell on it until the number becomes one-digit?", "input_spec": "The first line contains the only integer n (0 ≤ n ≤ 10100000). It is guaranteed that n doesn't contain any leading zeroes.", "output_spec": "Print the number of times a number can be replaced by the sum of its digits until it only contains one digit.", "sample_inputs": ["0", "10", "991"], "sample_outputs": ["0", "1", "3"], "notes": "NoteIn the first sample the number already is one-digit — Herald can't cast a spell.The second test contains number 10. After one casting of a spell it becomes 1, and here the process is completed. Thus, Gerald can only cast the spell once.The third test contains number 991. As one casts a spell the following transformations take place: 991 → 19 → 10 → 1. After three transformations the number becomes one-digit."}, "positive_code": [{"source_code": "n = gets.chomp\ncount = 0\nwhile n.size > 1\n n = n.split(//).inject(0) {|sum, e| sum + e.to_i}.to_s\n count += 1\nend\nputs count\n"}, {"source_code": "n = gets.chomp\ncount = 0\nwhile n.size != 1\n m = 0;\n n.each_byte{|c|\n m += c-48\n }\n n = m.to_s\n count += 1\nend\nputs count\n"}, {"source_code": "#!/usr/local/bin/ruby19\nans=0\nn=gets.split[0]\nwhile n.length>1\n m=0\n n.each_byte{|c|\n m+=c-48\n }\n ans+=1\n n=m.to_s\nend\nputs ans\n"}, {"source_code": "gets.chop!\n($.+=1;$_=($_.sum(32)-48*~/$/).to_s)until~/^.$/\np$.-1\n"}, {"source_code": "gets.chop!\n($.+=1;$_=($_.sum(32)-48*~/$/).to_s)until~/^.$/\np$.-1\n"}, {"source_code": "s = gets.chomp\nres = 0\nwhile s.length > 1 && res+=1 \n\ts = s.each_char.inject(0){ |sum, i| sum += i.to_i }.to_s\nend\nputs res\n"}, {"source_code": "gets.chop!\n($.+=1;$_=($_.sum(32)-48*~/$/).to_s)until~/^.$/\np$.-1"}, {"source_code": "n = gets.chomp.split(//)\ni = 0\nif n.size == 1\n puts i\n exit\nelse\n while n.size != 1 do\n n = n.inject(0){ |sum,x| x.to_i + sum }.to_s.split(//)\n i += 1\n end\nend\nputs i\n"}, {"source_code": "gets.chop!\n($.+=1;$_=($_.sum(32)-48*~/$/).to_s)until~/^.$/\np$.-1\n"}, {"source_code": "s = gets.chomp\nq = 0\nans = 1\n0.upto(s.length - 1).each do |i|\n q += s[i].to_i\nend\nwhile q >= 10 do\n nq = 0\n while q != 0 do\n nq += q % 10\n q /= 10\n end\n q = nq\n ans += 1\nend\n\nif s.length == 1 then ans = 0 end\n\nprint ans\n\n"}, {"source_code": "gets.chop!\n($.+=1;$_=($_.sum(32)-48*~/$/).to_s)until~/^.$/\np$.-1\n"}, {"source_code": "gets.chop!\n($.+=1;$_=($_.sum(32)-48*~/$/).to_s)until~/^.$/\np$.-1"}, {"source_code": "#=================================\nif ARGV.include?(\"ONLINE_JUDGE\")\n\t$stdin = File.open(ARGV[0], \"r\")\n\tARGV.delete(\"ONLINE_JUDGE\")\nend\n#=================================\n\nn=gets.strip\n\nif n.length==1\n\t\tputs 0\n\t\texit\nend\n\t\nstep=1\nloop{\n\tn=n.split(//).map(&:to_i).inject(:+).to_s\n\t\n\tif n.length==1\n\t\tputs step\n\t\texit\n\tend\n\t\n\tstep+=1\n}\n"}, {"source_code": "n = STDIN.read.gsub(\"\\n\", \"\")\n\nsteps = 0\nwhile (n.size != 1) do\n digits = n.split(\"\")\n n = digits.inject(0) { |result, digit| result + digit.to_i }.to_s\n steps += 1\nend\n\nputs steps\n"}, {"source_code": "n = gets.chomp\ncnt = 0 \nwhile(n.length != 1)\n res = 0 \n for i in 0...n.length\n res += Integer(n[i])\n end\n n = String(res)\n cnt += 1\nend\nputs cnt"}, {"source_code": "sn = gets.chop\n\n#n = sn.to_i\n\ncount = 0\n\nwhile (sn.length > 1)\n sum = 0\n (0..sn.length-1).each { |i|\n sum += sn[i].to_i\n }\n sn = sum.to_s\n #puts sn\n count += 1\nend\n\nputs count"}, {"source_code": "def sum_of_digits\n n = gets.strip\n\n count = 0\n while n.length > 1\n sum = n.chars.map{|i| i.to_i}.inject(0){|sum, j| sum+j }\n n = sum.to_s\n count += 1\n end\n\n p count\nend\n\n\nsum_of_digits"}, {"source_code": "gets.chop!\n($.+=1;$_=($_.sum(32)-48*~/$/).to_s)until~/^.$/\np$.-1\n"}, {"source_code": "s = gets.chomp\nc = 0\nwhile s.size != 1\n\ts = s.split(//).map(&:to_i).reduce(:+).to_s\n\tc += 1\nend\nputs c"}, {"source_code": "s = gets.chomp.split(\"\").collect{|i| i.to_i}\nans = 0\nwhile s.size > 1\n ans += 1\n s = s.inject{|sum,i| sum+=i}.to_s.split(\"\").collect{|i| i.to_i}\nend\nputs ans\n"}, {"source_code": "n = gets.chomp\nasr = 0\nbool = true\nwhile bool do\n if(n.to_i <=9 && n.to_i >= 0)\n bool = false\n else\n begin\n asr += 1\n n = n.to_s.split('').inject(0){ |result, elem| result.to_i + elem.to_i }\n end\n end\nend\nputs asr"}, {"source_code": "gets.chop!\n($.+=1;$_=($_.sum(32)-48*~/$/).to_s)until~/^.$/\np$.-1\n"}, {"source_code": "gets.chop!\n($.+=1;$_=($_.sum(32)-48*~/$/).to_s)until~/^.$/\np$.-1\n"}, {"source_code": "gets.chop!\n($.+=1;$_=($_.sum(32)-48*~/$/).to_s)until~/^.$/\np$.-1"}, {"source_code": "n, i = gets.strip, 0\n\n(print 0; exit) if n.length == 1\n\nloop do\n n = n.split(//).map(&:to_i).inject { |sum, e| sum += e }.to_s\n (print i + 1; exit) if n.length == 1\n i += 1\nend"}, {"source_code": "gets.chop!\n($.+=1;$_=($_.sum(32)-48*~/$/).to_s)until~/^.$/\np$.-1\n"}], "negative_code": [{"source_code": "n = gets.chomp\nres = 0\nfor i in 0...n.length\n res += Integer(n[i])\nend\nputs res"}, {"source_code": "s = gets\nc = 0\nwhile s.size != 1\n\ts = s.split(//).map(&:to_i).reduce(:+).to_s\n\tc += 1\nend\nputs c"}, {"source_code": "n = gets.chomp\nasr = 0\nbool = true\nwhile bool do\n if(n.to_i <=9 && n.to_i >= 0)\n bool = false\n else\n begin\n asr += 1\n n = n.to_s.split('').inject(0){ |result, elem| result.to_i + elem.to_i }\n puts n\n end\n end\nend\nputs asr"}, {"source_code": "gets.chop!\n($.+=1;$_=($_.sum(20)-48*~/$/).to_s)until~/^.$/\np$.-1"}, {"source_code": "gets\n($.+=1;$_=($_.sum(20)-48*~/$/).to_s)until~/^.$/\np$.-1"}, {"source_code": "gets.chomp!\n($.+=1;$_=($_.sum(20)-48*~/$/).to_s)until~/^.$/\np$.-1"}, {"source_code": "a=gets\n($.+=1;a=(a.sum-48*a.size).to_s)while a.size>1\np$."}, {"source_code": "n, i = gets.strip, 0\n\n(print 0; exit) if n == \"0\"\n\nloop do\n n = n.split(//).map(&:to_i).inject { |sum, e| sum += e }.to_s\n (print i + 1; exit) if n.length == 1\n i += 1\nend"}], "src_uid": "ddc9201725e30297a5fc83f4eed75fc9"} {"nl": {"description": "As you know, Bob's brother lives in Flatland. In Flatland there are n cities, connected by n - 1 two-way roads. The cities are numbered from 1 to n. You can get from one city to another moving along the roads.The «Two Paths» company, where Bob's brother works, has won a tender to repair two paths in Flatland. A path is a sequence of different cities, connected sequentially by roads. The company is allowed to choose by itself the paths to repair. The only condition they have to meet is that the two paths shouldn't cross (i.e. shouldn't have common cities).It is known that the profit, the «Two Paths» company will get, equals the product of the lengths of the two paths. Let's consider the length of each road equals 1, and the length of a path equals the amount of roads in it. Find the maximum possible profit for the company.", "input_spec": "The first line contains an integer n (2 ≤ n ≤ 200), where n is the amount of cities in the country. The following n - 1 lines contain the information about the roads. Each line contains a pair of numbers of the cities, connected by the road ai, bi (1 ≤ ai, bi ≤ n).", "output_spec": "Output the maximum possible profit.", "sample_inputs": ["4\n1 2\n2 3\n3 4", "7\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7", "6\n1 2\n2 3\n2 4\n5 4\n6 4"], "sample_outputs": ["1", "0", "4"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\ne = Array.new(n)\ne.map!{[]}\nee = []\n\ndef gao(e, p, q)\n\tl = 0\n\tt = [0, 0]\n\te[p].each do |i|\n\t\tnext if i == q\n\t\tll, dd = gao(e, i, p)\n\t\tl = [l, ll].max\n\t\tt <<= dd + 1\n\tend\n\tt.sort!{|a, b| b <=> a}\n\tl = [l, t[0] + t[1]].max\n\treturn [l, t[0]]\nend\n\n(n - 1).times do\n\ta, b = gets.split.map{|i| i.to_i - 1}\n\te[a] <<= b\n\te[b] <<= a\n\tee <<= [a, b]\nend\nans = 0\nee.each do |p|\n\tans = [ans, gao(e, p[0], p[1])[0] * gao(e, p[1], p[0])[0]].max\nend\np ans\n"}, {"source_code": "def gao(e, p, q)\n\tl, t = 0, [0, 0]\n\te[p].each do |i|\n\t\tnext if i == q\n\t\tll, dd = gao(e, i, p)\n\t\tl = [l, ll].max\n\t\tt <<= dd + 1\n\tend\n\tt.sort!{|a, b| b <=> a}\n\treturn [[l, t[0] + t[1]].max, t[0]]\nend\n\nn = gets.to_i\ne = Array.new(n).map{[]}\nee = []\n(n - 1).times do\n\ta, b = gets.split.map{|i| i.to_i - 1}\n\te[a] <<= b\n\te[b] <<= a\n\tee <<= [a, b]\nend\np ee.map{|p| gao(e, p[0], p[1])[0] * gao(e, p[1], p[0])[0]}.max\n\n"}, {"source_code": "=begin\n , \\ / ,\n / \\ )\\__/( / \\\n / \\ (_\\ /_) / \\\n ____/_____\\__\\@ @/___/_____\\____\n | |\\../| |\n | \\VV/ |\n | ------hoi------- |\n |_________________________________|\n | /\\ / \\\\ \\ /\\ |\n | / V )) V \\ |\n |/ ` // ' \\|\n ` V '\n=end\n\ndef dfs(e, p, q)\n\tl, t = 0, [0, 0]\n\te[p].each do |i|\n\t\tnext if i == q\n\t\tll, dd = dfs(e, i, p)\n\t\tl = [l, ll].max\n\t\tt <<= dd + 1\n\tend\n\tt.sort!{|a, b| b <=> a}\n\treturn [[l, t[0] + t[1]].max, t[0]]\nend\n\nn = gets.to_i\nrib = Array.new(n).map{[]}\nribs = []\n(n - 1).times do\n\ta, b = gets.split.map{|i| i.to_i - 1}\n\trib[a] <<= b\n\trib[b] <<= a\n\tribs <<= [a, b]\nend\nputs ribs.map{|p| dfs(rib, p[0], p[1])[0] * dfs(rib, p[1], p[0])[0]}.max\n"}], "negative_code": [], "src_uid": "b07668a66a5e50659233ba055a893bd4"} {"nl": {"description": "Initially Ildar has an empty array. He performs $$$n$$$ steps. On each step he takes a subset of integers already added to the array and appends the mex of this subset to the array. The mex of an multiset of integers is the smallest non-negative integer not presented in the multiset. For example, the mex of the multiset $$$[0, 2, 3]$$$ is $$$1$$$, while the mex of the multiset $$$[1, 2, 1]$$$ is $$$0$$$.More formally, on the step $$$m$$$, when Ildar already has an array $$$a_1, a_2, \\ldots, a_{m-1}$$$, he chooses some subset of indices $$$1 \\leq i_1 < i_2 < \\ldots < i_k < m$$$ (possibly, empty), where $$$0 \\leq k < m$$$, and appends the $$$mex(a_{i_1}, a_{i_2}, \\ldots a_{i_k})$$$ to the end of the array.After performing all the steps Ildar thinks that he might have made a mistake somewhere. He asks you to determine for a given array $$$a_1, a_2, \\ldots, a_n$$$ the minimum step $$$t$$$ such that he has definitely made a mistake on at least one of the steps $$$1, 2, \\ldots, t$$$, or determine that he could have obtained this array without mistakes.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 100\\,000$$$) — the number of steps Ildar made. The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$0 \\leq a_i \\leq 10^9$$$) — the array Ildar obtained.", "output_spec": "If Ildar could have chosen the subsets on each step in such a way that the resulting array is $$$a_1, a_2, \\ldots, a_n$$$, print $$$-1$$$. Otherwise print a single integer $$$t$$$ — the smallest index of a step such that a mistake was made on at least one step among steps $$$1, 2, \\ldots, t$$$.", "sample_inputs": ["4\n0 1 2 1", "3\n1 0 1", "4\n0 1 2 239"], "sample_outputs": ["-1", "1", "4"], "notes": "NoteIn the first example it is possible that Ildar made no mistakes. Here is the process he could have followed. $$$1$$$-st step. The initial array is empty. He can choose an empty subset and obtain $$$0$$$, because the mex of an empty set is $$$0$$$. Appending this value to the end he gets the array $$$[0]$$$. $$$2$$$-nd step. The current array is $$$[0]$$$. He can choose a subset $$$[0]$$$ and obtain an integer $$$1$$$, because $$$mex(0) = 1$$$. Appending this value to the end he gets the array $$$[0,1]$$$. $$$3$$$-rd step. The current array is $$$[0,1]$$$. He can choose a subset $$$[0,1]$$$ and obtain an integer $$$2$$$, because $$$mex(0,1) = 2$$$. Appending this value to the end he gets the array $$$[0,1,2]$$$. $$$4$$$-th step. The current array is $$$[0,1,2]$$$. He can choose a subset $$$[0]$$$ and obtain an integer $$$1$$$, because $$$mex(0) = 1$$$. Appending this value to the end he gets the array $$$[0,1,2,1]$$$. Thus, he can get the array without mistakes, so the answer is $$$-1$$$.In the second example he has definitely made a mistake on the very first step, because he could not have obtained anything different from $$$0$$$.In the third example he could have obtained $$$[0, 1, 2]$$$ without mistakes, but $$$239$$$ is definitely wrong."}, "positive_code": [{"source_code": "\ndef solution\n _ = read_int\n as = read_ints\n\n current_max = -1\n\n for i in (0...as.size)\n a = as[i]\n if a > current_max + 1\n puts (i+1)\n return\n end\n current_max = [current_max, a].max\n end\n\n puts (-1)\n\nend\n\ndef read_int\n gets.to_i\nend\n\ndef read_ints\n gets.split.map(&:to_i)\nend\n\ndef read_string\n gets.chomp\nend\n\nsolution unless ENV['TEST__MODE']\n\n"}, {"source_code": "class B\n def self.run(n, m)\n prev_max = nil\n\n m.each_with_index do |el, idx|\n\n if valid_mexes(prev_max).include?(el)\n prev_max ||= el\n prev_max = prev_max < el ? el : prev_max\n else\n return idx+1\n end\n\n end\n\n return -1\n end\n\n def self.valid_mexes(prev_max)\n if !prev_max\n return [0]\n else\n (0..prev_max+1)\n end\n end\nend\n\na = gets.chomp.to_i\nb = gets.chomp.split.map(&:to_i)\nputs B.run(a, b)\n\n"}, {"source_code": "n = gets.to_i\narr = gets.chomp.split(\" \").map(&:to_i)\n\nk = 0\narr.each_with_index do |z,i|\n if z>k\n puts i+1\n exit\n elsif z==k\n k+=1\n end\nend\n\nputs -1\n"}], "negative_code": [{"source_code": "\ndef solution\n _ = read_int\n as = read_ints\n\n for i in (0...as.size)\n a = as[i]\n if a > i\n puts (i+1)\n return\n end\n end\n\n puts (-1)\n\nend\n\ndef read_int\n gets.to_i\nend\n\ndef read_ints\n gets.split.map(&:to_i)\nend\n\ndef read_string\n gets.chomp\nend\n\nsolution unless ENV['TEST__MODE']\n\n"}], "src_uid": "e17427897fd5147c601204cb1c48b143"} {"nl": {"description": "Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump.", "input_spec": "The first line of the input contains an integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \\le |s| \\le 2 \\cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \\dots, j_m$$$ ($$$1 \\le j_i \\le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them.", "sample_inputs": ["6\n\nlogic\n\ncodeforces\n\nbca\n\naaaaaaaaaaa\n\nadbaadabad\n\nto"], "sample_outputs": ["9 4\n1 4 3 5\n16 10\n1 8 3 4 9 5 2 6 7 10\n1 2\n1 3\n0 11\n1 8 10 4 3 5 7 2 9 6 11\n3 10\n1 9 5 4 7 3 8 6 2 10\n5 2\n1 2"], "notes": "NoteIn the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$."}, "positive_code": [{"source_code": "$stdin.readline\r\n\r\nwhile !$stdin.eof do\r\n bytes = $stdin.readline.chomp.bytes\r\n cost = (bytes.first - bytes.last).abs\r\n\r\n bytes_i = bytes.each_with_index.select{ |x, _|\r\n (bytes.first..bytes.last).include?(x) || (bytes.last..bytes.first).include?(x)\r\n }\r\n\r\n jumps = bytes_i[1..-2].sort_by{|x| x[0]}\r\n jumps.reverse! if bytes.first > bytes.last\r\n jumps = [bytes_i[0]] + jumps + [bytes_i[-1]]\r\n indexes = jumps.map{|x| x[1] + 1}\r\n\r\n puts [cost, indexes.size].join(\" \")\r\n puts indexes.join(\" \")\r\nend"}], "negative_code": [], "src_uid": "d17c9f91504e1d4c4eae7294bf09dcfc"} {"nl": {"description": "After waking up at hh:mm, Andrew realised that he had forgotten to feed his only cat for yet another time (guess why there's only one cat). The cat's current hunger level is H points, moreover each minute without food increases his hunger by D points.At any time Andrew can visit the store where tasty buns are sold (you can assume that is doesn't take time to get to the store and back). One such bun costs C roubles and decreases hunger by N points. Since the demand for bakery drops heavily in the evening, there is a special 20% discount for buns starting from 20:00 (note that the cost might become rational). Of course, buns cannot be sold by parts.Determine the minimum amount of money Andrew has to spend in order to feed his cat. The cat is considered fed if its hunger level is less than or equal to zero.", "input_spec": "The first line contains two integers hh and mm (00 ≤ hh ≤ 23, 00 ≤ mm ≤ 59) — the time of Andrew's awakening. The second line contains four integers H, D, C and N (1 ≤ H ≤ 105, 1 ≤ D, C, N ≤ 102).", "output_spec": "Output the minimum amount of money to within three decimal digits. You answer is considered correct, if its absolute or relative error does not exceed 10 - 4. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if .", "sample_inputs": ["19 00\n255 1 100 1", "17 41\n1000 6 15 11"], "sample_outputs": ["25200.0000", "1365.0000"], "notes": "NoteIn the first sample Andrew can visit the store at exactly 20:00. The cat's hunger will be equal to 315, hence it will be necessary to purchase 315 buns. The discount makes the final answer 25200 roubles.In the second sample it's optimal to visit the store right after he wakes up. Then he'll have to buy 91 bins per 15 roubles each and spend a total of 1365 roubles."}, "positive_code": [{"source_code": "hh, mm = gets().split(' ').map(&:to_i)\nh, d, c, n = gets().split(' ')\nh, d, n = [h, d, n].map(&:to_i)\nc = c.to_f\n\nif(hh*60+mm < 20*60)\n\tputs \"%.3f\" % [((h + n - 1)/n) * c, ((h + d*(20*60 - hh*60-mm) + n - 1)/n) * (c * 0.8)].min\nelse\n\tputs \"%.3f\" % (((h + n - 1)/n) * (c*0.8))\nend"}, {"source_code": "hh, mm = gets().split(' ').map(&:to_i)\nh, d, c, n = gets().split(' ')\nh, d, n = [h, d, n].map(&:to_i)\nc = c.to_f\n\nif(hh*60+mm < 20*60)\n\tputs \"%.3f\" % [((h + n - 1)/n) * c, ((h + d*(20*60 - hh*60-mm) + n - 1)/n) * (c * 0.8)].min\nelse\n\tputs \"%.3f\" % (((h + n - 1)/n) * (c*0.8))\nend"}], "negative_code": [{"source_code": "hh, mm = gets().split(' ').map(&:to_f)\nh, d, c, n = gets().split(' ').map(&:to_f)\n\nif(hh*60+mm < 20*60)\n\tputs \"%.3f\" % [(h + n - 1)/n * c, (h + n - 1 + d*(20*60 - hh*60+mm))/n * (c * 0.8)].min\nelse\n\tputs \"%.3f\" % ((h + n - 1)/n * (c*0.8))\nend"}, {"source_code": "hh, mm = gets().split(' ').map(&:to_i)\nh, d, c, n = gets().split(' ')\nh, d, n = [h, d, n].map(&:to_i)\nc = c.to_f\n\nif(hh*60+mm < 20*60)\n\tputs \"%.4f\" % [(h + n - 1)/n * c, (h + n - 1 + d*(20*60 - hh*60+mm))/n * (c * 0.8)].min\nelse\n\tputs \"%.4f\" % (h + n - 1)/n * (c*0.8)\nend"}, {"source_code": "hh, mm = gets().split(' ').map(&:to_i)\nh, d, c, n = gets().split(' ')\nh, d, n = [h, d, n].map(&:to_i)\nc = c.to_f\n\nif(hh*60+mm < 20*60)\n\tputs \"%.3f\" % [((h + n - 1)/n) * c, ((h + d*(20*60 - hh*60+mm) + n - 1)/n) * (c * 0.8)].min\nelse\n\tputs \"%.3f\" % (((h + n - 1)/n) * (c*0.8))\nend"}, {"source_code": "hour, minute = gets.split.map(&:to_f)\nhunger, dec, bunCost, inc = gets.split.map(&:to_f)\nbuyNow = hunger/inc*bunCost\nbuyLater = (hunger + dec*(1200 - ((hour%24)*60+minute)))/inc*(bunCost-(bunCost*0.2))\nputs [buyLater,buyNow].min.round(4)"}, {"source_code": "hour, minute = gets.split.map(&:to_i)\nhunger, dec, bunCost, inc = gets.split.map(&:to_f)\nbuyNow = (hunger/inc).ceil*bunCost\nbuyLater = ((hunger + dec*(1200 - ((hour%24)*60+minute)))/inc).ceil * bunCost\nbuyLater -= buyLater*0.2\nputs hour != 20 ? [buyLater,buyNow].min : buyNow"}, {"source_code": "hour, minute = gets.split.map(&:to_i)\nhunger, dec, bunCost, inc = gets.split.map(&:to_f)\nbuyNow = (hunger/inc).ceil*bunCost\nbuyLater = ((hunger + dec*(1200 - ((hour%24)*60+minute)))/inc).ceil * bunCost\nbuyLater -= buyLater*0.2\nputs hour != 20 ? [buyLater,buyNow].min : buyNow-(buyNow*0.2)"}, {"source_code": "hour, minute = gets.split.map(&:to_i)\nhunger, dec, bunCost, inc = gets.split.map(&:to_f)\nbuyNow = (hunger/inc).ceil*bunCost\nbuyLater = ((hunger + dec*(1200 - ((hour%24.0)*60+minute)))/inc).ceil * bunCost\nbuyLater -= buyLater*0.2\nputs [buyLater,buyNow].min"}], "src_uid": "937acacc6d5d12d45c08047dde36dcf0"} {"nl": {"description": "You are given a positive integer $$$n$$$ greater or equal to $$$2$$$. For every pair of integers $$$a$$$ and $$$b$$$ ($$$2 \\le |a|, |b| \\le n$$$), you can transform $$$a$$$ into $$$b$$$ if and only if there exists an integer $$$x$$$ such that $$$1 < |x|$$$ and ($$$a \\cdot x = b$$$ or $$$b \\cdot x = a$$$), where $$$|x|$$$ denotes the absolute value of $$$x$$$.After such a transformation, your score increases by $$$|x|$$$ points and you are not allowed to transform $$$a$$$ into $$$b$$$ nor $$$b$$$ into $$$a$$$ anymore.Initially, you have a score of $$$0$$$. You can start at any integer and transform it as many times as you like. What is the maximum score you can achieve?", "input_spec": "A single line contains a single integer $$$n$$$ ($$$2 \\le n \\le 100\\,000$$$) — the given integer described above.", "output_spec": "Print an only integer — the maximum score that can be achieved with the transformations. If it is not possible to perform even a single transformation for all possible starting integers, print $$$0$$$.", "sample_inputs": ["4", "6", "2"], "sample_outputs": ["8", "28", "0"], "notes": "NoteIn the first example, the transformations are $$$2 \\rightarrow 4 \\rightarrow (-2) \\rightarrow (-4) \\rightarrow 2$$$.In the third example, it is impossible to perform even a single transformation."}, "positive_code": [{"source_code": "n=gets.to_i\nans=0\nfor i in 2..n do\n j=2*i\n until j>n do\n ans+=j/i\n j+=i\n end\nend\np ans*4"}], "negative_code": [], "src_uid": "03907ca0d34a2c80942ed3968b2c067d"} {"nl": {"description": "There is a $$$n \\times m$$$ grid. You are standing at cell $$$(1, 1)$$$ and your goal is to finish at cell $$$(n, m)$$$.You can move to the neighboring cells to the right or down. In other words, suppose you are standing at cell $$$(x, y)$$$. You can: move right to the cell $$$(x, y + 1)$$$ — it costs $$$x$$$ burles; move down to the cell $$$(x + 1, y)$$$ — it costs $$$y$$$ burles. Can you reach cell $$$(n, m)$$$ spending exactly $$$k$$$ burles?", "input_spec": "The first line contains the single integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. The first and only line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$1 \\le n, m \\le 100$$$; $$$0 \\le k \\le 10^4$$$) — the sizes of grid and the exact amount of money you need to spend.", "output_spec": "For each test case, if you can reach cell $$$(n, m)$$$ spending exactly $$$k$$$ burles, print YES. Otherwise, print NO. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).", "sample_inputs": ["6\n1 1 0\n2 2 2\n2 2 3\n2 2 4\n1 4 3\n100 100 10000"], "sample_outputs": ["YES\nNO\nYES\nNO\nYES\nNO"], "notes": "NoteIn the first test case, you are already in the final cell, so you spend $$$0$$$ burles.In the second, third and fourth test cases, there are two paths from $$$(1, 1)$$$ to $$$(2, 2)$$$: $$$(1, 1)$$$ $$$\\rightarrow$$$ $$$(1, 2)$$$ $$$\\rightarrow$$$ $$$(2, 2)$$$ or $$$(1, 1)$$$ $$$\\rightarrow$$$ $$$(2, 1)$$$ $$$\\rightarrow$$$ $$$(2, 2)$$$. Both costs $$$1 + 2 = 3$$$ burles, so it's the only amount of money you can spend.In the fifth test case, there is the only way from $$$(1, 1)$$$ to $$$(1, 4)$$$ and it costs $$$1 + 1 + 1 = 3$$$ burles."}, "positive_code": [{"source_code": "t = gets.to_s.to_i\n\ndef solve(maxx, maxy, k, cache)\n return k == 0 if maxx == 1 && maxy == 1\n return false if k <= 0 || maxx < 1 || maxy < 1\n maxx, maxy = maxy, maxx if maxx < maxy\n return cache[maxx * 128 + maxy] if cache.has_key?(maxx * 128 + maxy)\n\n result = solve(maxx - 1, maxy, k - maxy, cache) || solve(maxx, maxy - 1, k - maxx, cache)\n cache[maxx * 128 + maxy] = result\n return result\nend\n\n1.upto(t) do |casenum|\n maxx, maxy, k = gets.to_s.split.map {|x| x.to_i }\n printf(\"%s\\n\", solve(maxx, maxy, k, {}) ? \"YES\" : \"NO\")\nend\n"}, {"source_code": "test_cases = gets.chomp.to_i\r\nfor i in 1..test_cases do\r\n line = gets.chomp.split(\" \")\r\n n = line[0].to_i\r\n m = line[1].to_i\r\n k = line[2].to_i\r\n puts (n - 1) + n * (m - 1) == k ? \"Yes\" : \"No\"\r\nend"}], "negative_code": [], "src_uid": "8b0a9c7e997034d3ecce044b9f64aeba"} {"nl": {"description": "There is a grid with $$$n$$$ rows and $$$m$$$ columns, and three types of cells: An empty cell, denoted with '.'. A stone, denoted with '*'. An obstacle, denoted with the lowercase Latin letter 'o'. All stones fall down until they meet the floor (the bottom row), an obstacle, or other stone which is already immovable. (In other words, all the stones just fall down as long as they can fall.)Simulate the process. What does the resulting grid look like?", "input_spec": "The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \\leq t \\leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\leq n, m \\leq 50$$$) — the number of rows and the number of columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.', '*', or 'o' — an empty cell, a stone, or an obstacle, respectively.", "output_spec": "For each test case, output a grid with $$$n$$$ rows and $$$m$$$ columns, showing the result of the process. You don't need to output a new line after each test, it is in the samples just for clarity.", "sample_inputs": ["3\n6 10\n.*.*....*.\n.*.......*\n...o....o.\n.*.*....*.\n..........\n.o......o*\n2 9\n...***ooo\n.*o.*o.*o\n5 5\n*****\n*....\n*****\n....*\n*****"], "sample_outputs": ["..........\n...*....*.\n.*.o....o.\n.*........\n.*......**\n.o.*....o*\n\n....**ooo\n.*o**o.*o\n\n.....\n*...*\n*****\n*****\n*****"], "notes": null}, "positive_code": [{"source_code": "gets.to_i.times do\r\n n, m = gets.split.map(&:to_i)\r\n xxs = n.times.map { gets.chomp.chars }\r\n (n - 2).downto(0).each do |i|\r\n 0.upto(m - 1).each do |j|\r\n if xxs[i][j] == \"*\"\r\n i2 = i\r\n while i2 + 1 < n && xxs[i2 + 1][j] == \".\"\r\n xxs[i2][j] = \".\"\r\n i2 += 1\r\n end\r\n xxs[i2][j] = \"*\"\r\n end\r\n end\r\n end\r\n puts xxs.map(&:join).join(\"\\n\")\r\nend\r\n"}], "negative_code": [], "src_uid": "964ce5983d0e9b1a3f460ad6e6078d47"} {"nl": {"description": "This is the hard version of the problem. The difference between the versions is that in the easy version all prices $$$a_i$$$ are different. You can make hacks if and only if you solved both versions of the problem.Today is Sage's birthday, and she will go shopping to buy ice spheres. All $$$n$$$ ice spheres are placed in a row and they are numbered from $$$1$$$ to $$$n$$$ from left to right. Each ice sphere has a positive integer price. In this version, some prices can be equal.An ice sphere is cheap if it costs strictly less than two neighboring ice spheres: the nearest to the left and the nearest to the right. The leftmost and the rightmost ice spheres are not cheap. Sage will choose all cheap ice spheres and then buy only them.You can visit the shop before Sage and reorder the ice spheres as you wish. Find out the maximum number of ice spheres that Sage can buy, and show how the ice spheres should be reordered.", "input_spec": "The first line contains a single integer $$$n$$$ $$$(1 \\le n \\le 10^5)$$$ — the number of ice spheres in the shop. The second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ $$$(1 \\le a_i \\le 10^9)$$$ — the prices of ice spheres.", "output_spec": "In the first line print the maximum number of ice spheres that Sage can buy. In the second line print the prices of ice spheres in the optimal order. If there are several correct answers, you can print any of them.", "sample_inputs": ["7\n1 3 2 2 4 5 4"], "sample_outputs": ["3\n3 1 4 2 4 2 5"], "notes": "NoteIn the sample it's not possible to place the ice spheres in any order so that Sage would buy $$$4$$$ of them. If the spheres are placed in the order $$$(3, 1, 4, 2, 4, 2, 5)$$$, then Sage will buy one sphere for $$$1$$$ and two spheres for $$$2$$$ each."}, "positive_code": [{"source_code": "N = gets.to_i\nA = gets.split.map(&:to_i).sort\nres = Array.new(N, 0)\n\n1.step(N - 1, 2) do |x|\n res[x] = A.shift\nend\n\n0.step(N - 1, 2) do |x|\n res[x] = A.shift\nend\n\nans = 0\n\n1.upto(N - 2) do |i|\n ans += 1 if res[i - 1] > res[i] && res[i] < res[i + 1]\nend\n\nputs ans\nputs res.join(' ')\n"}], "negative_code": [], "src_uid": "6443dffb38285b6deb74f2371d8d0cac"} {"nl": {"description": "Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1,  p2,  ...,  pn.You have a sequence of integers a1, a2, ..., an. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 3·105) — the size of the sought permutation. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).", "output_spec": "Print a single number — the minimum number of moves. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.", "sample_inputs": ["2\n3 0", "3\n-1 -1 2"], "sample_outputs": ["2", "6"], "notes": "NoteIn the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2, 1).In the second sample you need 6 moves to build permutation (1, 3, 2)."}, "positive_code": [{"source_code": "gets;p gets.split.map(&:to_i).sort.each_with_index.inject(0){|s,(v,i)| s+(v-i-1).abs}"}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i).sort\nputs n.times.inject(0){|s, i| s+(a[i]-i-1).abs}"}, {"source_code": "n = gets.to_i; a = gets.split.map &:to_i; a.sort!\nres = 0; 0.upto(n-1){|i|res += (a[i] - i - 1).abs}\nputs res"}, {"source_code": "gets;a,s=gets.split.map(&:to_i).sort,0;a.size.times {|i|s+=(a[i]-i-1).abs};p s;\n"}, {"source_code": "gets;a,s=gets.split.map(&:to_i).sort,0;a.size.times {|i|s+=(a[i]-i-1).abs};p s;\n"}, {"source_code": "#!/usr/bin/ruby1.9\n\nn = STDIN.readline.to_i\na = STDIN.readline.split.collect {|s| s.to_i }\n\nb = []\na.each do |x|\n\t#b.push([(1-x).abs, x])\n\tb.push(x)\nend\nb.sort!\n\n#puts b.inspect\n\nsum = 0\nn.times do |i|\n\t#sum += (i+1-b[i][1]).abs\n\tsum += (i+1-b[i]).abs\nend\n\nputs sum\n"}, {"source_code": "n = gets.to_i\nt = gets.split.map(&:to_i).sort\nresult = 0\nt.each_with_index do |x, i|\n result += (i + 1 - x).abs\nend\nputs result"}, {"source_code": "gets;a,s=gets.split.map(&:to_i).sort,0;a.size.times {|i|s+=(a[i]-i-1).abs};p s;\n"}, {"source_code": "gets;a,s=gets.split.map(&:to_i).sort,0;a.size.times {|i|s+=(a[i]-i-1).abs};p s;"}, {"source_code": "gets;a=gets.split.map(&:to_i).sort;p (0...a.size).inject(0){|s,i|s+(a[i]-i-1).abs}"}, {"source_code": "gets;a,s=gets.split.map(&:to_i).sort,0;a.size.times {|i|s+=(a[i]-i-1).abs};p s;\n"}, {"source_code": "require 'set'\nrequire 'rational'\nrequire 'mathn'\ninclude Math\n$stdin = File.open(\"./input.txt\", \"r\") if ENV[\"USER\"] == \"new\"\n# ====================================\n\nn = gets.to_i\nperm = gets.split.map(&:to_i).sort\n\nchanges = 0\nset = Set.new\nindex = 1\nperm.each{|value|\n changes+=(index - value).abs\n index+=1\n}\n\nprint changes"}, {"source_code": "require 'set'\nrequire 'rational'\nrequire 'mathn'\ninclude Math\n$stdin = File.open(\"./input.txt\", \"r\") if ENV[\"USER\"] == \"new\"\n# ====================================\n\nn = gets.to_i\nperm = gets.split.map(&:to_i).sort\n\nchanges = 0\nindex = 1\nperm.each{|value|\n changes+=(index - value).abs\n index+=1\n}\n\nprint changes"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split(\" \").map{|i| i.to_i}\n\nless = []\nmiddle = []\ngreater = []\n\nn.times do |i|\n if a[i] < 1\n less << a[i]\n elsif a[i] > n\n greater << a[i]\n else\n middle << a[i]\n end\nend\nless.sort!\ngreater.sort!\nmiddle.sort!\n\nl = less.length\ng = greater.length\n\nmove = 0\n\noriginal = (1..n).to_a\nmissing = original - middle \nlength_missing = missing.length\n\nsame = []\nold_x = 0\nmiddle.each do |x|\n if x != old_x\n old_x = x\n else\n same << x\n end\nend\n\nsum_less = 0\nsum_missing = 0\nl.times do |i|\n sum_missing += missing[i]\n sum_less += less[i]\nend\nmove += sum_missing - sum_less\n\nsum_greater = 0\nsum_missing = 0\ng.times do |i|\n sum_missing += missing[length_missing - i - 1]\n sum_greater += greater[i]\nend\nmove += sum_greater - sum_missing\n\nmissing[l...(length_missing - g)].each_with_index do |x,i|\n move += (x - same[i]).abs\nend\n\nputs move\n"}, {"source_code": "n = gets.to_i\nvec = (gets.split.map &:to_i).sort\ntotal = 0\nfor i in (0...n)\n total+=(i+1-vec[i]).abs\nend \nputs total"}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i).sort\na.unshift(0)\nres=0\n1.upto(n) {|x| res+=(x-a[x]).abs}\nputs res\n"}, {"source_code": "# coding: utf-8\n\nn = gets.to_i\na = gets.split.map(&:to_i)\ndelta_sum = 0\nfor tup in (1..n).zip(a.sort)\n # p tup\n delta_sum += (tup[0] - tup[1]).abs\nend\n\nputs delta_sum"}, {"source_code": "n = Integer(gets)\na = Array.new\ninput = gets.chomp\nnums = input.split\nfor i in 0...n\n a[i] = Integer(nums[i])\nend\n\na = a.sort\nc = 0\nfor i in 0...n\n c += (i + 1 - a[i]).abs\nend\nputs c\n"}, {"source_code": "n = gets.to_i\nv = gets.split.map &:to_i\nv = v.sort\nmoves=0\nfor i in 0...n\n moves+=((i+1)-v[i]).abs\nend\nputs moves"}, {"source_code": "gets;a,s=gets.split.map(&:to_i).sort,0;a.size.times {|i|s+=(a[i]-i-1).abs};p s;\n"}, {"source_code": "n = gets.to_i\na = [nil] +gets.split.map(&:to_i).sort\nans = 0\n1.upto(n) do |i|\n ans += (i - a[i]).abs\nend\np ans\n"}, {"source_code": "gets;p gets.split.map(&:to_i).sort.map.with_index{|x,i|(x-i-1).abs}.inject(:+)\n"}, {"source_code": "gets;a,s=gets.split.map(&:to_i).sort,0;a.size.times {|i|s+=(a[i]-i-1).abs};p s;\n"}, {"source_code": "n = gets.to_i;\na = gets.split.map(& :to_i);\ncur = 0; \nb = a.sort;\np (1..n).inject(0){|acc, x| acc + (x - b[x-1]).abs}\n#p a.sort.inject {|acc, x| cur += 1; acc + (cur - x).abs}\n"}, {"source_code": "n = Integer(gets.chomp)\na = gets.chomp.split.map { |x| x.to_i }.sort\ncount = 0\nfor i in 1..n\n count += (a[i - 1] - i).abs\nend\nputs count"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split(\" \").map{|i| i.to_i}\na.sort!\n i = n - 1 \n move = 0\n while i >= 0 do\n move += (a[i] - i - 1).abs\n i -= 1\n end\n\nputs move"}, {"source_code": "gets;a,s=gets.split.map(&:to_i).sort,0;a.size.times {|i|s+=(a[i]-i-1).abs};p s;"}, {"source_code": "#!/usr/bin/env ruby\n\ngets\ntot = 0\ngets.split.collect{ |i| i.to_i }.sort.each_with_index { |x, i| tot += (i + 1 - x).abs }\nputs tot\n"}, {"source_code": "n, _ = gets.strip.split.map(&:to_i)\nas = gets.strip.split.map(&:to_i).sort\n\nresult = 0\n(1..n).each do |i|\n result += (as[i-1] - i).abs\nend\n\nputs result\n\n\n\n\n"}, {"source_code": "gets;a,s=gets.split.map(&:to_i).sort,0;a.size.times {|i|s+=(a[i]-i-1).abs};p s;\n"}, {"source_code": "gets;a,s=gets.split.map(&:to_i).sort,0;a.size.times {|i|s+=(a[i]-i-1).abs};p s;\n"}], "negative_code": [{"source_code": "gets;a=gets.split.map(&:to_i);p (0...a.size).inject(0){|s,i|s+(a[i]-i-1).abs}"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split(\" \").map{|i| i.to_i}\n\nless = []\nmiddle = []\ngreater = []\n\nn.times do |i|\n if a[i] < 1\n less << a[i]\n elsif a[i] > n\n greater << a[i]\n else\n middle << a[i]\n end\nend\nless.sort!\ngreater.sort!\n\nmove = 0\n(1..n).each do |i|\n unless middle.include?(i)\n if less.any?\n move += i - less.shift\n elsif greater.any?\n move += greater.shift - i\n end\n end\nend\n\nputs move\n"}, {"source_code": "# coding: utf-8\n\nn = gets.to_i\na = gets.split.map(&:to_i)\ndelta_sum = 0\nfor tup in (1..n).zip(a)\n delta_sum += (tup[0] - tup[1]).abs\nend\n\nputs delta_sum"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split(\" \").map{|i| i.to_i}\na.sort!\n i = n - 1 \n move = 0\n while i > 0 do\n move +=(a[i] - i - 1).abs\n i -= 1\n end\n\nputs move"}, {"source_code": "n = gets.to_i\ng = gets.split(' ')\na = []\nindex = []\nindex1 = []\ns = 0 \ngiam = 0\n\n(0..n-1).each do |i|\n a[i] = g[i].to_i\nend\n\n(1..n+1).each do |i|\n index[i] = 0\n index1[i] = 0\nend\n\n(0..n-1).each do |i|\n s += a[i]\n if a[i] >= n \n a[i] = n\n giam = a[i] - n \n index[n] +=1\n else\n index[a[i]] +=1 if a[i] > 0\n end \nend\n\nindex1[n+1] = 0\ni = n\nwhile i > 0\n index1[i] = index1[i+1] + index[i]\n giam += index1[i] - (n+1-i) if index1[i] > (n+1-i)\n i -= 1\nend\n\nt = (n+1)*n/2\ns1 = (s-t).abs + 2*giam\n\nputs s1"}, {"source_code": "n = gets.to_i\ng = gets.split(' ')\na = []\nindex = []\nindex1 = []\ns = 0 \ngiam = 0\ns1 = 0\n\n(0..n-1).each do |i|\n a[i] = g[i].to_i\nend\n\n(1..n+1).each do |i|\n index[i] = 0\n index1[i] = 0\nend\n\n(0..n-1).each do |i|\n if a[i] > n \n s1 += a[i] - n \n index[n] +=1\n a[i] = n\n else\n index[a[i]] +=1 if a[i] > 0\n end \n s += a[i]\nend\n\nindex1[n+1] = 0\ni = n\nwhile i > 0\n index1[i] = index1[i+1] + index[i]\n giam += index1[i] - (n+1-i) if index1[i] > (n+1-i)\n i -= 1\nend\n\nt = (n+1)*n/2\ns1 += (s-t).abs + 2*giam\n\nputs s\nputs s1"}, {"source_code": "n = gets.to_i\ng = gets.split(' ')\na = []\nindex = []\nindex1 = []\ns = 0 \ngiam = 0\ns1 = 0\n\n(0..n-1).each do |i|\n a[i] = g[i].to_i\nend\n\n(1..n+1).each do |i|\n index[i] = 0\n index1[i] = 0\nend\n\n(0..n-1).each do |i|\n if a[i] > n \n s1 += a[i] - n \n index[n] +=1\n a[i] = n\n else\n index[a[i]] +=1 if a[i] > 0\n end \n s += a[i]\nend\n\nindex1[n+1] = 0\ni = n\nwhile i > 0\n index1[i] = index1[i+1] + index[i]\n giam += index1[i] - (n+1-i) if index1[i] > (n+1-i)\n i -= 1\nend\n\nt = (n+1)*n/2\ns1 += (s-t).abs + 2*giam\n\nputs s1"}, {"source_code": "n = gets.to_i\ng = gets.split(' ')\na = []\nindex = []\nindex1 = []\ns = 0 \ngiam = 0\ns1 = 0\n\n(0..n-1).each do |i|\n a[i] = g[i].to_i\nend\n\n(1..n+1).each do |i|\n index[i] = 0\n index1[i] = 0\nend\n\n(0..n-1).each do |i|\n if a[i] > n \n s1 += a[i] - n \n index[n] +=1\n a[i] = n\n else\n index[a[i]] +=1 if a[i] > 1\n end \n s += a[i]\nend\n\nindex1[n+1] = 0\ni = n\nwhile i > 0\n index1[i] = index1[i+1] + index[i]\n giam += index1[i] - (n+1-i) if index1[i] > (n+1-i)\n i -= 1\nend\n\nt = (n+1)*n/2\ns1 += (s-t).abs + 2*giam\n\nputs s1"}], "src_uid": "86d5da999415fa75b3ee754a2a28605c"} {"nl": {"description": "You are given a string $$$s$$$, consisting of brackets of two types: '(', ')', '[' and ']'.A string is called a regular bracket sequence (RBS) if it's of one of the following types: empty string; '(' + RBS + ')'; '[' + RBS + ']'; RBS + RBS. where plus is a concatenation of two strings.In one move you can choose a non-empty subsequence of the string $$$s$$$ (not necessarily consecutive) that is an RBS, remove it from the string and concatenate the remaining parts without changing the order.What is the maximum number of moves you can perform?", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of testcases. Each of the next $$$t$$$ lines contains a non-empty string, consisting only of characters '(', ')', '[' and ']'. The total length of the strings over all testcases doesn't exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each testcase print a single integer — the maximum number of moves you can perform on a given string $$$s$$$.", "sample_inputs": ["5\n()\n[]()\n([)]\n)]([\n)[(]"], "sample_outputs": ["1\n2\n2\n0\n1"], "notes": "NoteIn the first example you can just erase the whole string.In the second example you can first erase the brackets on positions $$$1$$$ and $$$2$$$: \"[]()\", then \"()\" is left. After that you can erase it whole. You could erase the whole string from the beginning but you would get one move instead of two.In the third example you can first erase the brackets on positions $$$1$$$ and $$$3$$$: \"([)]\". They form an RBS \"()\". Then \"[]\" is left, so you can erase it whole.In the fourth example there is no subsequence that is an RBS, so you can't perform a move at all.In the fifth example you can erase the brackets on positions $$$2$$$ and $$$4$$$: \")[(]\" and get \")(\" as a result. You can erase nothing from it."}, "positive_code": [{"source_code": "DEBUG = false\nMOD = 10**9+7\nYESNO = %w(Yes No)\n\ndef main\n t = int\n t.times do\n p1 = 0\n p2 = 0\n count = 0\n string.each_char do |c|\n case c\n when ?(\n p1 += 1\n when ?)\n count += 1 if p1 > 0\n p1 -= 1 if p1 > 0\n when ?[\n p2 += 1\n when ?]\n count += 1 if p2 > 0\n p2 -= 1 if p2 > 0\n end\n end\n\n puts count\n end\nend\n\ndef int; gets.to_i; end\ndef ints; gets.split.map &:to_i; end\ndef float; gets.to_f; end\ndef floats; gets.split.map &:to_f; end\ndef array(&convert); gets.split.map(&convert); end\ndef string; gets.chomp; end\ndef rep(n, &b); Array.new(n, &b); end\ndef yes; puts YESNO[0]; end\ndef no; puts YESNO[1]; end\ndef yesno t; t ? yes : no; end\ndef zip(xs, *yss); Enumerator.new { |y| xs.zip(*yss) { |a| y.yield(*a) } }; end\ndef sorted_uniq(xs); x = nil; xs.filter! { |y| c = x === y; x = y; !c }; end\ndef max(*xs); xs.max; end\ndef min(*xs); xs.min; end\ndef minmax(*xs); xs.minmax; end\ndef matrix(h, w, fill=nil, &block); return Array.new(h) { Array.new(w, &block) } if block_given?; Array.new(h) { [fill] * w }; end\ndef debug(x); if DEBUG; STDERR.puts (block_given? ? yield(x) : x).inspect; end; x; end\ndef debug_grid(grid, width = 1); grid.each { |row| STDERR.puts row.map { |x| x.inspect.ljust(width) }.join(\"\") } if DEBUG; grid; end\ndef if_debug; yield if DEBUG; end\n\ndef div_ceil(x, y); (x + y - 1) / y; end\ndef gcd(*xs); xs.inject(0) { |y, x| y.gcd(x) }; end\ndef factorial(n, mod); (2..n).inject(1) { |f, x| f * x % mod }; end\ndef cumsum(xs); ys = [0]; xs.each { |x| ys << x + ys[-1] }; ys; end\ndef cumdiff(ys); xs = []; xs.inject { |x, y| xs << (d = y - x); d }; end\ndef cumfold(ys, range); r = range.end; r -= 1 if range.exclusive_end?; ys[r] - ys[range.begin-1]; end\ndef mod_inv(x, mod); x.pow(mod-2, mod); end\ndef mod_div(x, y, mod); x * mod_inv(y, mod) % mod; end\ndef bitbrute(size, &block); (1< 0\n b1 = [b1 - 1, 0].max if el == ')'\n b2 += 1 if el == '['\n mx2 += 1 if el ==']' && b2 > 0\n b2 = [b2 - 1, 0].max if el == ']'\n end\n puts mx1 + mx2\nend\n"}, {"source_code": "t = gets.chomp.to_i\nt.times do\n a = gets.chomp.split(\"\")\n count = 0\n co = 0\n lo = 0\n a.each do |i|\n if i == \"(\"\n co += 1\n elsif i == \")\" && co >= 1\n co -= 1\n count += 1\n elsif i == \"[\"\n lo += 1\n elsif i == \"]\" && lo >= 1\n lo -= 1\n count += 1\n end\n end\n puts count\nend"}, {"source_code": "def make_score(arr)\n score = 0\n ph_s = 0\n arr.each do |b|\n if b\n ph_s += 1\n else\n if ph_s.positive?\n score += 1\n ph_s -= 1\n end\n end\n end\n score\nend\n\ntestcases = gets.to_i\ntestcases.times do\n str = gets.chars\n small = []\n large = []\n str.each do |c|\n case c\n when '('\n small << true\n when ')'\n small << false\n when '['\n large << true\n when ']'\n large << false\n end\n end\n p make_score(small) + make_score(large)\nend\n"}], "negative_code": [{"source_code": "DEBUG = false\nMOD = 10**9+7\nYESNO = %w(Yes No)\n\ndef main\n t = int\n t.times do\n p1 = 0\n p2 = 0\n count = 0\n string.each_char do |c|\n case c\n when ?(\n p1 += 1\n when ?)\n count += 1 if p1 > 0\n p1 -= 1\n when ?[\n p2 += 1\n when ?]\n count += 1 if p2 > 0\n p2 -= 1\n end\n end\n\n puts count\n end\nend\n\ndef int; gets.to_i; end\ndef ints; gets.split.map &:to_i; end\ndef float; gets.to_f; end\ndef floats; gets.split.map &:to_f; end\ndef array(&convert); gets.split.map(&convert); end\ndef string; gets.chomp; end\ndef rep(n, &b); Array.new(n, &b); end\ndef yes; puts YESNO[0]; end\ndef no; puts YESNO[1]; end\ndef yesno t; t ? yes : no; end\ndef zip(xs, *yss); Enumerator.new { |y| xs.zip(*yss) { |a| y.yield(*a) } }; end\ndef sorted_uniq(xs); x = nil; xs.filter! { |y| c = x === y; x = y; !c }; end\ndef max(*xs); xs.max; end\ndef min(*xs); xs.min; end\ndef minmax(*xs); xs.minmax; end\ndef matrix(h, w, fill=nil, &block); return Array.new(h) { Array.new(w, &block) } if block_given?; Array.new(h) { [fill] * w }; end\ndef debug(x); if DEBUG; STDERR.puts (block_given? ? yield(x) : x).inspect; end; x; end\ndef debug_grid(grid, width = 1); grid.each { |row| STDERR.puts row.map { |x| x.inspect.ljust(width) }.join(\"\") } if DEBUG; grid; end\ndef if_debug; yield if DEBUG; end\n\ndef div_ceil(x, y); (x + y - 1) / y; end\ndef gcd(*xs); xs.inject(0) { |y, x| y.gcd(x) }; end\ndef factorial(n, mod); (2..n).inject(1) { |f, x| f * x % mod }; end\ndef cumsum(xs); ys = [0]; xs.each { |x| ys << x + ys[-1] }; ys; end\ndef cumdiff(ys); xs = []; xs.inject { |x, y| xs << (d = y - x); d }; end\ndef cumfold(ys, range); r = range.end; r -= 1 if range.exclusive_end?; ys[r] - ys[range.begin-1]; end\ndef mod_inv(x, mod); x.pow(mod-2, mod); end\ndef mod_div(x, y, mod); x * mod_inv(y, mod) % mod; end\ndef bitbrute(size, &block); (1<b\nputs \"#{v*c}\"\nelse \nputs \"#{b*c}\"\nend\n"}], "src_uid": "6421a81f85a53a0c8c63fbc32750f77f"} {"nl": {"description": "$$$ \\def\\myred#1{\\color{red}{\\underline{\\bf{#1}}}} \\def\\myblue#1{\\color{blue}{\\overline{\\bf{#1}}}} $$$ $$$\\def\\RED{\\myred{Red}} \\def\\BLUE{\\myblue{Blue}}$$$You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, \\ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$\\RED$$$ or $$$\\BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$\\text{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$\\text{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color.For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[\\myblue{2}, 8, \\myred{6}, \\myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$\\text{Sum}(\\RED)=6$$$, $$$\\text{Sum}(\\BLUE)=2+3=5$$$, $$$\\text{Count}(\\RED)=1$$$, and $$$\\text{Count}(\\BLUE)=2$$$.Determine if it is possible to paint the sequence so that $$$\\text{Sum}(\\RED) > \\text{Sum}(\\BLUE)$$$ and $$$\\text{Count}(\\RED) < \\text{Count}(\\BLUE)$$$.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\\le n\\le 2\\cdot 10^5$$$) — the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\\ldots,a_n$$$ ($$$0\\le a_i\\le 10^9$$$) — the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\\cdot 10^5$$$.", "output_spec": "For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).", "sample_inputs": ["4\n3\n1 2 3\n5\n2 8 6 3 1\n4\n3 5 4 2\n5\n1000000000 1000000000 1000000000 1000000000 1000000000"], "sample_outputs": ["NO\nYES\nNO\nNO"], "notes": "NoteIn the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\\myblue{1},\\myblue{2},\\myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$\\text{Count}(\\RED)=1 < \\text{Count}(\\BLUE)=2$$$, but $$$\\text{Sum}(\\RED)=3 \\ngtr \\text{Sum}(\\BLUE)=3$$$. So, this is not a possible way to paint the sequence.In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$\\text{Sum}(\\RED)=6 > \\text{Sum}(\\BLUE)=5$$$ and $$$\\text{Count}(\\RED)=1 < \\text{Count}(\\BLUE)=2$$$.In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[\\myred{3},\\myred{5},\\myblue{4}, \\myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$\\text{Sum}(\\RED) = 8 > \\text{Sum}(\\BLUE) = 6$$$ but $$$\\text{Count}(\\RED) = 2 \\nless \\text{Count}(\\BLUE) = 2$$$. So, this is not a possible way to paint the sequence.In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints."}, "positive_code": [{"source_code": "gets.chomp.to_i.times do\r\n n = gets.chomp.to_i\r\n str = gets.chomp.split.map{|el| el.to_i}.sort\r\n \r\n smallSum = str[0]\r\n bigSum =0\r\n boolFound=false\r\n \r\n for i in 0..str.length-1\r\n smallSum +=str[i].to_i\r\n bigSum +=str[-i].to_i\r\n if(bigSum>smallSum)\r\n puts \"YES\"\r\n boolFound = true\r\n end\r\n break if(boolFound)\r\n end\r\n \r\n if(boolFound==false)\r\n puts \"NO\"\r\n end\r\n\r\nend"}, {"source_code": "t=gets.chomp.to_i\r\n\r\nt.times do\r\n n = gets.chomp.to_i\r\n str = gets.chomp.split\r\n arr = str.sort_by{|el| el.to_i}\r\n # for i in 0..str.length-1\r\n # arr[i]=str[i].to_i\r\n # end\r\n #arr = str.sort\r\n\r\n # puts arr\r\n smallSum = arr[0].to_i\r\n bigSum =0\r\n boolFound=false\r\n for i in 0..str.length-1\r\n smallSum +=arr[i].to_i\r\n bigSum +=arr[-i].to_i\r\n \r\n #puts \" smallSum = #{smallSum} bigSum = #{bigSum}\"\r\n if(bigSum>smallSum)\r\n puts \"YES\"\r\n boolFound = true\r\n \r\n # puts \" if #{boolFound}\"\r\n \r\n end\r\n break if(boolFound)\r\n #puts \" breal if #{boolFound}\"\r\n\r\n end\r\n if(boolFound==false)\r\n puts \"NO\"\r\n end\r\n # puts \" breal if #{boolFound}\"\r\n\r\nend"}], "negative_code": [{"source_code": "t=gets.chomp.to_i\r\n\r\nt.times do\r\n n = gets.chomp.to_i\r\n str = gets.chomp.split.sort\r\n smallSum = str[0].to_i\r\n bigSum =0\r\n boolFound=false\r\n for i in 0..str.length-1\r\n smallSum +=str[i].to_i\r\n bigSum +=str[-i].to_i\r\n if(bigSum>smallSum)\r\n puts \"YES\"\r\n boolFound = true\r\n end\r\n break if(boolFound)\r\n end\r\n if(boolFound==false)\r\n puts \"NO\"\r\n end\r\nend"}], "src_uid": "4af59df1bc56ca8eb5913c2e57905922"} {"nl": {"description": "Little Dormi received a histogram with $$$n$$$ bars of height $$$a_1, a_2, \\ldots, a_n$$$ for Christmas. However, the more he played with his new histogram, the more he realized its imperfections, so today he wanted to modify it to his liking.To modify the histogram, Little Dormi is able to perform the following operation an arbitrary number of times: Select an index $$$i$$$ ($$$1 \\le i \\le n$$$) where $$$a_i>0$$$, and assign $$$a_i := a_i-1$$$.Little Dormi defines the ugliness score of his histogram (after performing some number of operations) as the sum of the vertical length of its outline and the number of operations he performed on it. And to make the histogram as perfect as possible, he would like to minimize the ugliness score after modifying it with some number of operations.However, as his histogram is very large, Little Dormi is having trouble minimizing the ugliness score, so as Little Dormi's older brother, help him find the minimal ugliness.Consider the following example where the histogram has $$$4$$$ columns of heights $$$4,8,9,6$$$: The blue region represents the histogram, and the red lines represent the vertical portion of the outline. Currently, the vertical length of the outline is $$$4+4+1+3+6 = 18$$$, so if Little Dormi does not modify the histogram at all, the ugliness would be $$$18$$$.However, Little Dormi can apply the operation once on column $$$2$$$ and twice on column $$$3$$$, resulting in a histogram with heights $$$4,7,7,6$$$: Now, as the total vertical length of the outline (red lines) is $$$4+3+1+6=14$$$, the ugliness is $$$14+3=17$$$ dollars. It can be proven that this is optimal.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 10^4$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 4 \\cdot 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$0 \\le a_i \\le 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$4 \\cdot 10^5$$$.", "output_spec": "For each test case output one integer, the minimal ugliness Little Dormi can achieve with the histogram in that test case.", "sample_inputs": ["2\n4\n4 8 9 6\n6\n2 1 7 4 0 0"], "sample_outputs": ["17\n12"], "notes": "NoteExample $$$1$$$ is the example described in the statement.The initial histogram for example $$$2$$$ is given below: The ugliness is currently $$$2+1+6+3+4=16$$$.By applying the operation once on column $$$1$$$, six times on column $$$3$$$, and three times on column $$$4$$$, we can end up with a histogram with heights $$$1,1,1,1,0,0$$$: The vertical length of the outline is now $$$1+1=2$$$ and Little Dormi made $$$1+6+3=10$$$ operations, so the final ugliness is $$$2+10=12$$$, which can be proven to be optimal."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n gets\r\n as = [0] + gets.split.map(&:to_i) + [0]\r\n cost = 1.upto(as.size - 1).filter_map do |i|\r\n a, b, c = as[i - 1], as[i], as[i + 1]\r\n if a < b && c < b\r\n as[i] = [a, c].max\r\n (b - as[i]).abs\r\n end\r\n end.sum\r\n p cost + as.each_cons(2).map { |a, b| (a - b).abs }.sum\r\nend\r\n"}], "negative_code": [], "src_uid": "6313b2788fbf58b9b7c70fc85afa4c1c"} {"nl": {"description": "Mr. Scrooge, a very busy man, decided to count the time he wastes on all sorts of useless stuff to evaluate the lost profit. He has already counted the time he wastes sleeping and eating. And now Mr. Scrooge wants to count the time he has wasted signing papers.Mr. Scrooge's signature can be represented as a polyline A1A2... An. Scrooge signs like that: first it places a pen at the point A1, then draws a segment from point A1 to point A2, then he draws a segment from point A2 to point A3 and so on to point An, where he stops signing and takes the pen off the paper. At that the resulting line can intersect with itself and partially repeat itself but Scrooge pays no attention to it and never changes his signing style. As Scrooge makes the signature, he never takes the pen off the paper and his writing speed is constant — 50 millimeters per second.Scrooge signed exactly k papers throughout his life and all those signatures look the same.Find the total time Scrooge wasted signing the papers.", "input_spec": "The first line contains two integers n and k (2 ≤ n ≤ 100, 1 ≤ k ≤ 1000). Each of the following n lines contains the coordinates of the polyline's endpoints. The i-th one contains coordinates of the point Ai — integers xi and yi, separated by a space. All points Ai are different. The absolute value of all coordinates does not exceed 20. The coordinates are measured in millimeters.", "output_spec": "Print one real number — the total time Scrooges wastes on signing the papers in seconds. The absolute or relative error should not exceed 10 - 6.", "sample_inputs": ["2 1\n0 0\n10 0", "5 10\n3 1\n-5 6\n-2 -1\n3 2\n10 0", "6 10\n5 0\n4 0\n6 0\n3 0\n7 0\n2 0"], "sample_outputs": ["0.200000000", "6.032163204", "3.000000000"], "notes": null}, "positive_code": [{"source_code": "str = STDIN.gets.chomp.split(' ')\nn = str[0].to_i\nk = str[1].to_i\nx = Array.new\ny = Array.new\nn.times do |i|\n str = STDIN.gets.chomp.split(' ')\n x[i] = str[0].to_f\n y[i] = str[1].to_f\nend\n\n\ndist = 0.0\n1.upto(x.length - 1).each do |i|\n dist += (((x[i - 1] - x[i]) ** 2) + ((y[i - 1] - y[i]) ** 2)) ** 0.5\nend\nputs \"%.9f\" %[((dist * k) / 50).to_f]"}, {"source_code": "n, k = gets.split.map &:to_i\np = n.times.map { |_| gets.split.map(&:to_i) }\np n.times.inject() { |s, i|\n a, b = p[i], p[i-1]\n s += Math.hypot(a[0]-b[0], a[1]-b[1])\n} * k / 50\n"}, {"source_code": "n, k = gets.split.map &:to_i\npt = STDIN.each.to_a\ns = 0.upto(pt.length - 1).inject do |s, i|\n a = pt[i].split.map(&:to_i)\n b = pt[i - 1].split.map(&:to_i)\n s += Math.hypot(a[0] - b[0], a[1] - b[1])\nend\np s * k / 50\n"}, {"source_code": "n,k=gets.split.map(&:to_i)\npoint=[]\nfor i in 1..n\n point.push(gets.split.map(&:to_i))\nend\nres=0.0\nfor i in 1..n-1\n res+=Math::sqrt((point[i-1][0]-point[i][0])**2+(point[i-1][1]-point[i][1])**2)\nend\np res/50*k\n"}, {"source_code": "#!/usr/bin/env ruby\n\nn, k = ($stdin.readline).split().collect { |x| x.to_i }\n\npoints = ($stdin.readlines).collect {|s| s.split().collect {|x| x.to_i} }\n\nspeed = 50\n\ndef dist(a, b)\n\treturn Math.sqrt((a[0] - b[0])**2 + (a[1] - b[1])**2)\nend\n\nsum = 0.0\n\n(n-1).times { |i|\n\tsum += dist(points[i], points[i+1])\n\t}\n\nputs sum/speed*k"}, {"source_code": "n,k = gets.split.map(&:to_i)\narr = (1..n).map{|e| gets.split.map(&:to_i)}\nbefore = arr[0]\np arr.map{|e| ret = Math::sqrt((before[0] - e[0]) ** 2 + (before[1] - e[1]) ** 2); before = e ; ret}.reduce(:+) / 50 * k\n\n"}, {"source_code": "def dist (x1,y1,x2,y2)\n Math.sqrt((x2-x1)**2 + (y2-y1)**2)\nend\nn, k = gets.split.map(&:to_i)\na = readlines.map(&:chomp).map(&:split).each { |x| x.map!(&:to_i) }\nputs (1...n).map{ |i| dist a[i-1][0], a[i-1][1], a[i][0], a[i][1] }.inject(:+)*k/50.0"}, {"source_code": "n, k = gets.strip.split.map {|x| x.to_i}\ndef dis x1, y1, x2, y2\n Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))\nend\nsum = 0.0\nbef = gets.strip.split.map {|x| x.to_i}\nn.pred.times do\n cur = gets.strip.split.map {|x| x.to_i}\n sum += dis *bef, *cur\n bef = cur\nend\nputs sum * k / 50.0"}, {"source_code": "n,k=gets.split.map(&:to_i)\n\ndots=[]\nn.times{dots << gets.split.map(&:to_f)}\n\ndist=0\nfor i in 0...(n-1)\n\tdist+=Math.sqrt((dots[i][0] - dots[i+1][0])**2 + (dots[i][1] - dots[i+1][1])**2)\nend\n\nprintf \"%.7f\", (dist/50.0)*k\n"}, {"source_code": "n,k=gets.split.map(&:to_i)\no=[]\nn.times{o << gets.split.map(&:to_f)}\nd=0\nfor i in 0...(n-1)\n\td+=Math.sqrt((o[i][0] - o[i+1][0])**2 + (o[i][1] - o[i+1][1])**2)\nend\nprintf \"%.7f\",d/50.0*k\n"}, {"source_code": "n,k = gets.split()\nn = n.to_i\nk = k.to_i\nx = Array.new(n)\ny = Array.new(n)\nfor i in 0...n do\n xx,yy = gets.split()\n x[i] = xx.to_f\n y[i] = yy.to_f\nend\ndist = 0.0\nfor i in 1...n do\n xd = x[i] - x[i-1]\n yd = y[i] - y[i-1]\n d = xd*xd + yd*yd;\n dist += (d ** 0.5);\nend\n print dist / 50.0 * k, \"\\n\";\n"}, {"source_code": "input = gets.chomp\nnums = input.split\nn = Integer(nums[0])\nk = Integer(nums[1])\ncoordinate = Array.new\nfor i in 0...n\n\tcoordinate[i] = Array.new\n\tinput2 = gets.chomp\n\tnums2 = input2.split\n\tfor j in 0...2\n\t\tcoordinate[i][j] = Integer(nums2[j])\n\tend\nend\ncount = 0\n\nfor i in 0...n-1\n\tc = (coordinate[i][0] - coordinate[i+1][0]) ** 2 + (coordinate[i][1] - coordinate[i+1][1])** 2 \n\tc = ((c** (0.5)) / 50) * k\n\tcount += c\nend\n\nputs count = format(\"%.9f\",count)"}, {"source_code": "n,k = gets.split.map &:to_i\nx2 = y2 = sum = 0\nfor i in (1..n)\n x1, y1 = gets.split.map &:to_i\n if i > 1\n sum +=Math.hypot(x2-x1,y2-y1)\n end\n x2, y2 = x1,y1\nend\nputs (sum*k)/50"}, {"source_code": "n, k = gets.split.map(&:to_i)\np = Array.new(n)\n(0...n).each { |i| a, b = gets.split.map(&:to_i); p[i] = Array.new(2); p[i][0] = a; p[i][1] = b }\nlen = 0.0\n(1...n).each { |i| len += Math.sqrt( (p[i][0] - p[i-1][0]) ** 2 + (p[i][1] - p[i-1][1]) ** 2 ) }\nputs((k * len) / 50.0)"}, {"source_code": "\nn, k = gets.split(\" \").map do |s| s.to_i end\n\npx, py = gets.split(\" \").map do |s| s.to_i end\n\ntime = 0\n(n-1).times do\n x, y = gets.split(\" \").map do |s| s.to_i end\n time += Math.sqrt((x-px)**2 + (y-py)**2) / 50\n px, py = x, y\nend\n\nputs time*k\n"}, {"source_code": "def next_line\n gets.chomp.split.map { |x| x.to_i }\nend\n\nn, k = next_line\nd = 0\np = next_line\nfor _ in 1...n\n c = next_line\n d += Math.sqrt((c[0] - p[0]).abs ** 2 + (c[1] - p[1]).abs ** 2)\n p = c\nend\nputs d.to_f * k / 50"}, {"source_code": "\ndef size(a1, a2)\n\n return Math.sqrt((a1[0] - a2[0])*(a1[0] - a2[0]) + \n (a1[1] - a2[1])*(a1[1] - a2[1]))\nend\n\nSpeed = 50\n\nn, k = gets().split.map { |elem| elem.to_i}\n\nif n == 0\n puts 0\n return \nend\n\na_i = gets().split.map { |elem| elem.to_i}\nlength = 0\n\nfor i in 2..n\n a = gets().split.map { |elem| elem.to_i}\n length += size(a_i, a)\n a_i = a\nend\n\nputs length / Speed * k\n\n\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\nans, x, y = 0, 0, 0\nn.times{ |i|\n u, v = gets.split.map(&:to_i)\n ans+=Math.hypot(u-x,v-y) if i > 0\n x, y = u, v\n}\nprintf(\"%.9f\\n\",ans*k/50)\n"}, {"source_code": "def meh\n meh = gets.split.map(&:to_i)\n n = meh[0]\n k = meh[1]\n total = 0\n\n meh = gets.split.map(&:to_i)\n\n for i in 1..(n - 1) do\n current = gets.split.map(&:to_i)\n distance = Math.sqrt((current[0] - meh[0]) * (current[0] - meh[0]) + (current[1] - meh[1]) * (current[1] - meh[1]))\n\n total += distance\n meh = current\n end\n\n puts (total/50.0) * k\nend\n\nmeh\n"}, {"source_code": "n, k = gets.split.map &:to_i\npoints = STDIN.read.split(\"\\n\").map { |e| e.split.map! &:to_i }\ndistance = 0\n\n(points.length - 1).times { |i| distance += Math.sqrt((points[i][0] - points[i + 1][0]) ** 2 + (points[i][1] - points[i + 1][1]) ** 2) }\n\nprint \"%.9f\" % (distance * k / 50.0)"}], "negative_code": [{"source_code": "#!/usr/bin/env ruby\n\nn, k = ($stdin.readline).split().collect { |x| x.to_i }\n\npoints = ($stdin.readlines).collect {|s| s.split().collect {|x| x.to_i} }\n\nspeed = 50\n\ndef dist(a, b)\n\treturn Math.sqrt((a[0] - b[0])**2 + (a[1] - b[1])**2)\nend\n\nsum = 0.0\n\n(n-1).times { |i|\n\tsum += dist(points[i], points[i+1])\n\tputs \"#{i} - #{sum}\"\n\t}\n\nputs sum/speed*k"}, {"source_code": "require 'scanf'\ninput = scanf(\"%d %d\")\nn = input[0]\nk = input[1]\nx = Array.new(n)\ny = Array.new(n)\nfor i in 0...n do\n input = scanf(\"%d %d\")\n x[i] = input[0]\n y[i] = input[1]\nend\ndist = 0.0\nfor i in 1...n do\n xd = x[i] - x[i-1]\n yd = y[i] - y[i-1]\n dist += Math.sqrt(xd * xd + yd * yd);\nend\nprint dist * k / 50.0, \"\\n\";\n"}, {"source_code": "require 'scanf'\ninput = scanf(\"%d %d\")\nn = input[0]\nk = input[1]\nx = Array.new(n)\ny = Array.new(n)\nfor i in 0...n do\n input = scanf(\"%f %f\")\n x[i] = input[0]\n y[i] = input[1]\nend\ndist = 0.0\nfor i in 1...n do\n# d = 1.0 + x[i]\n d = (x[i] - x[i-1]) * (x[i] - x[i-1]) + (y[i] - y[i-1]) * (y[i] - y[i-1])\n dist += Math.sqrt(d);\nend\nprint dist * k / 50.0, \"\\n\";\n"}, {"source_code": "require 'scanf'\ninput = scanf(\"%d %d\")\nn = input[0]\nk = input[1]\nx = Array.new(n)\ny = Array.new(n)\nfor i in 0...n do\n input = scanf(\"%d %d\")\n x[i] = input[0]\n y[i] = input[1]\nend\ndist = 0.0\nfor i in 1...n do\n xd = x[i] - x[i-1]\n yd = y[i] - y[i-1]\n dist += Math.sqrt(xd * xd + yd * yd);\nend\n print dist / 50.0 * k, \"\\n\";\n"}, {"source_code": "require 'scanf'\ninput = scanf(\"%d %d\")\nn = input[0]\nk = input[1]\nx = Array.new(n)\ny = Array.new\nfor i in 0...n do\n input = scanf(\"%d %d\")\n x[i] = input[0]\n y[i] = input[1]\nend\ndist = 0.0\nfor i in 1...n do\n xd = x[i] - x[i-1]\n yd = y[i] - y[i-1]\n d = xd*xd + yd*yd;\n dist += (d ** 0.5);\nend\n print dist / 50.0 * k, \"\\n\";\n"}, {"source_code": "require 'scanf'\ninput = scanf(\"%d %d\")\nn = input[0]\nk = input[1]\nx = Array.new(n)\ny = Array.new\nfor i in 0...n do\n input = scanf(\"%d %d\")\n x[i] = input[0]\n y[i] = input[1]\nend\ndist = 0.0\nfor i in 1...n do\n xd = x[i] - x[i-1]\n yd = y[i] - y[i-1]\n dist += Math.sqrt(xd * xd + yd * yd);\nend\n print dist / 50.0 , \"\\n\";\n"}], "src_uid": "db4a25159067abd9e3dd22bc4b773385"} {"nl": {"description": "Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: \"Fox\"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order. After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: si ≠ ti. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters si and ti according to their order in alphabet.", "input_spec": "The first line contains an integer n (1 ≤ n ≤ 100): number of names. Each of the following n lines contain one string namei (1 ≤ |namei| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.", "output_spec": "If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on). Otherwise output a single word \"Impossible\" (without quotes).", "sample_inputs": ["3\nrivest\nshamir\nadleman", "10\ntourist\npetr\nwjmzbmr\nyeputons\nvepifanov\nscottwu\noooooooooooooooo\nsubscriber\nrowdark\ntankengineer", "10\npetr\negor\nendagorion\nfeferivan\nilovetanyaromanova\nkostka\ndmitriyh\nmaratsnowbear\nbredorjaguarturnik\ncgyforever", "7\ncar\ncare\ncareful\ncarefully\nbecarefuldontforgetsomething\notherwiseyouwillbehacked\ngoodluck"], "sample_outputs": ["bcdefghijklmnopqrsatuvwxyz", "Impossible", "aghjlnopefikdmbcqrstuvwxyz", "acbdefhijklmnogpqrstuvwxyz"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/env ruby\nrequire 'tsort'\nclass Hash\n\tinclude TSort\n\talias tsort_each_node each_key\n\tdef tsort_each_child(node, &block)\n\t\tfetch(node).each(&block)\n\tend\nend\nA=gets.to_i.times.map{gets.chomp.chars.to_a}\nh={}\nA.combination(2){|x,y|\n\tif [x.size,y.size].min.times{|i|\n\t\tif x[i]!=y[i]\n\t\t\th[x[i]]||=[]\n\t\t\th[y[i]]||=[]\n\t\t\th[y[i]]<y.size\n\t\tputs :Impossible\n\t\texit\n\tend\n}\nr=([*'a'..'z']-h.keys+h.tsort)*'' rescue 'Impossible'\nputs r"}, {"source_code": "#!/usr/bin/env ruby\nrequire 'tsort'\nclass Hash\n\tinclude TSort\n\talias tsort_each_node each_key\n\tdef tsort_each_child(node, &block)\n\t\tfetch(node).each(&block)\n\tend\nend\nA=gets.to_i.times.map{gets.chomp.chars.to_a}\nh={}\nA.combination(2){|x,y|\n\tif [x.size,y.size].min.times{|i|\n\t\tif x[i]!=y[i]\n\t\t\th[x[i]]||=[]\n\t\t\th[y[i]]||=[]\n\t\t\th[y[i]]<y.size\n\t\tputs :Impossible\n\t\texit\n\tend\n}\nr=([*'a'..'z']-h.keys+h.tsort)*'' rescue 'Impossible'\nputs r\n"}, {"source_code": "#!/usr/bin/env ruby\nrequire 'tsort'\nclass Hash\n\tinclude TSort\n\talias tsort_each_node each_key\n\tdef tsort_each_child(node, &block)\n\t\tfetch(node).each(&block)\n\tend\nend\nA=gets.to_i.times.map{gets.chomp.chars.to_a}\nh={}\nA.combination(2){|x,y|\n\tif [x.size,y.size].min.times{|i|\n\t\tif x[i]!=y[i]\n\t\t\th[x[i]]||=[]\n\t\t\th[y[i]]||=[]\n\t\t\th[y[i]]<y.size\n\t\tputs :Impossible\n\t\texit\n\tend\n}\nr=([*'a'..'z']-h.keys+h.tsort)*'' rescue 'Impossible'\nputs r\n"}, {"source_code": "class Graph\n\tattr_reader :vertexSet, :edgeSet\n\tdef initialize(numberOfVertices) # adds vertices in a new blank graph by their numbers\n\t\t@vertexSet = Array.new()\n\t\t(0..(numberOfVertices - 1)).each do |i|\n\t\t\t@vertexSet.push(Vertex.new(i))\n\t\tend\n\tend\n\n\tdef addEdge(startVertex, endVertex)\n\t\tif !edgeExists?(startVertex, endVertex)\n\t\t\t@vertexSet[startVertex].addNbr(@vertexSet[endVertex])\n\t\t\t# puts \"#{startVertex} #{endVertex}\"\n\t\tend\n\tend\n\n\tdef edgeExists?(startVertex, endVertex)\n\t\t@vertexSet[startVertex].children.include?(@vertexSet[endVertex])\n\tend\n\n\tdef printEdges\n\t\t@vertexSet.each do |vertex|\n\t\t\tif (vertex.children.size != 0)\n\t\t\t\tvertex.children.each do |v|\n\t\t\t\t\tputs \"#{vertex.index} #{v.index}\"\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\tend\n\n\tdef countEdges\n\t\tt = 0\n\t\t@vertexSet.each do |vertex|\n\t\t\tif (vertex.children.size != 0)\n\t\t\t\tvertex.children.each do |v|\n\t\t\t\t\tt += 1\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\t\tt\n\tend\n\n\tdef printParentEdges\n\t\t@vertexSet.each do |vertex|\n\t\t\tif (vertex.parents.size != 0)\n\t\t\t\tvertex.parents.each do |v|\n\t\t\t\t\tputs \"#{vertex.index} #{v.index}\"\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\tend\n\n\tdef returnSources\n\t\ttemp = Array.new()\n\t\t@vertexSet.each do |vertex|\n\t\t\tif (vertex.parents.size == 0)\n\t\t\t\ttemp.push(vertex)\n\t\t\tend\n\t\tend\n\t\treturn temp\n\tend\n\n\tdef returnVertex(index)\n\t\t@vertexSet[index]\n\tend\n\n\tdef settleThings\n\t\tflag = 0\n\t\ttemp = Vertex.new(-1)\n\t\t@vertexSet.each do |vertex|\n\t\t\tif vertex.alone?\n\t\t\t\t# puts \"Gotcha #{vertex.index}!!!\"\n\t\t\t\tif flag == 0\n\t\t\t\t\t# puts \"Voila\"\n\t\t\t\t\ttemp = vertex\n\t\t\t\t\tflag = 1\n\t\t\t\telse\n\t\t\t\t\t# puts \"Temp.\"\n\t\t\t\t\ttemp.addNbr(vertex)\n\t\t\t\t\ttemp = vertex\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\tend\n\n\tdef DFS(index)\n\t\tvertex = returnVertex(index)\n\t\tprint \"#{index}.\"\n\t\tvertex.children.each do |v|\n\t\t\tDFS(v.index)\n\t\tend\n\tend\n\nend\n\n\nclass Vertex\n\tattr_reader :index, :children, :parents, :color\n\tdef initialize(index)\n\t\t@index = index\n\t\t@children = Array.new()\n\t\t@parents = Array.new()\n\t\t@color = 0\n\tend\n\n\tdef addNbr(index)\n\t\t@children.push(index)\n\t\tindex.parents.push(self)\n\tend\n\n\tdef changeColor(color)\n\t\t@color = color\n\tend\n\n\tdef alone?\n\t\treturn ((@parents.size == 0) and (@children.size == 0))\n\tend\nend\n\nclass Heap\n\tattr_accessor :heap_size, :array_rep\n\n\tdef initialize()\n\t\t@array_rep = Array.new()\n\t\t@heap_size = 0\n\tend\n\n\tdef parent(index)\n\t\t(index - 1)/2\n\tend\n\n\tdef parentKey(index)\n\t\t@array_rep[parent(index)]\n\tend\n\n\tdef leftChild(index)\n\t\t2*index + 1\n\tend\n\n\tdef rightChild(index)\n\t\t2*index + 2\n\tend\n\n\tdef leftChildKey(index)\n\t\t@array_rep[leftChild(index)]\n\tend\n\n\tdef rightChildKey(index)\n\t\t@array_rep[rightChild(index)]\n\tend\n\n\tdef leafNode?(index)\n\t\treturn index >= @heap_size/2\n\tend\n\n\tdef getMin\n\t\t@array_rep[0]\n\tend\n\n\tdef deleteMin\n\t\t@array_rep[@heap_size-1], @array_rep[0] = @array_rep[0], @array_rep[@heap_size-1]\n \t@heap_size -= 1\n\t\t@array_rep.delete_at(@heap_size)\n\t\tself.fixOneError(0)\n\tend\n\n\tdef extractMin\n\t\ttemp = getMin.to_i\n\t\tdeleteMin\n\t\treturn temp\n\tend\n\n\tdef fixOneError(index)\n\t\treturn if leafNode?(index) || satisfied?(index)\n\n\t leftChildKey = if @array_rep[leftChild(index)] != nil then @array_rep[leftChild(index)] else Float::INFINITY end\n\t rightChildKey = if @array_rep[rightChild(index)] != nil then @array_rep[rightChild(index)] else Float::INFINITY end\n\n \tsmallerChild = if leftChildKey < rightChildKey then leftChild(index) else rightChild(index) end\n\n \t@array_rep[index], @array_rep[smallerChild] = @array_rep[smallerChild], @array_rep[index]\n\n \tfixOneError(smallerChild)\n \tend\n\n\tdef satisfied?(index)\n\t\t# puts \"\\nin , #{index} Verify :\"\n\t\t# displayStatus\n\t\tt1 = if @array_rep[leftChild(index)] != nil then @array_rep[leftChild(index)] else Float::INFINITY end\n\t\tt2 = if @array_rep[rightChild(index)] != nil then @array_rep[rightChild(index)] else Float::INFINITY end\n\t\t@array_rep[index] <= t1 and @array_rep[index] <= t2\n \tend\n\n\tdef insert(value)\n\t\t@array_rep.push(value)\n\t\t@heap_size += 1\n\t\tindex = @heap_size - 1\n\n\t\twhile ((!(top?(index))) and (@array_rep[parent(index)] > @array_rep[index]))\n\t\t\t@array_rep[index], @array_rep[parent(index)] = @array_rep[parent(index)], @array_rep[index]\n\t\t\tindex = parent(index)\n\t\tend\n\tend\n\n\tdef top?(index)\n\t\treturn index == 0\n\tend\n\n\tdef displayStatus\n\t\tputs \"Status :\"\n\t\t@array_rep.each do |d|\n\t\t\tprint \"#{d}.\"\n\t\tend\n\tend\nend\n\ndef analyse(x, y)\n\tx = x.each_char.to_a\n\ty = y.each_char.to_a\n\t# puts \"x: #{x}\"\n\t# puts \"y: #{y}\"\n\tt = if x.size > y.size then y.size else x.size end\n\t(0..(t-1)).each do |i|\n\t\t# puts \"x: #{x[i]} y: #{y[i].ord}\"\n\t\tif x[i].ord != y[i].ord\n\t\t\treturn [(x[i].ord-97),(y[i].ord-97)]\n\t\tend\n\tend\n\tif x.size > y.size\n\t\treturn [-2, -2]\n\tend\n\treturn -1, -1\nend\n\n# code starts here :\n\nmyGraph = Graph.new(26)\nn = gets.chomp.to_i\nstr = gets.chomp\n\n(2..n).each do |j|\n\tstr2 = gets.chomp\n\tn1, n2 = analyse(str, str2)\n\tif (n1 == -2)\n\t\tputs \"Impossible\\n\"\n\t\texit\n\tend\n\tif (n1 != -1)\n\t\tmyGraph.addEdge(n1, n2)\n\tend\n\tstr = str2\nend\n\nmyGraph.settleThings()\n# puts \"#{myGraph.vertexSet.size - 1} #{myGraph.countEdges}\"\n# myGraph.printEdges\n#\n# if ((myGraph.vertexSet.size - 1) < myGraph.countEdges)\n# \tputs \"IMPOSSIBLE\"\n# \texit\n# end\n\nmyHeap = Heap.new()\n\nsources = myGraph.returnSources()\n\nsources.each do |z|\n\tmyHeap.insert(z.index)\n\tz.changeColor(1)\nend\na = Array.new()\nwhile (myHeap.heap_size != 0)\n\tcurrentVertex = myGraph.returnVertex(myHeap.extractMin)\n\tcurrentVertex.changeColor(2)\n\tcurrentVertex.children.each do |v|\n\t\tif (v.parents.all? {|y| y.color == 2 })\n\t\t\tmyHeap.insert(v.index)\n\t\t\tv.changeColor(1)\n\t\tend\n\tend\n\ta.push((currentVertex.index.to_i+97).chr)\nend\nif a.size == 26\n\tprint \"#{a.join(\"\")}\\n\"\nelse\n\tprint \"Impossible\\n\"\nend\n"}, {"source_code": "class List\n attr_accessor :element, :next_elem\n\n def initialize(element, next_elem)\n @element = element\n @next_elem = next_elem\n end\nend\n\nclass Graph\n attr_accessor :vertices\n\n def initialize(vertices)\n @vertices = vertices\n end\n\n def add_edge(letter1, letter2)\n if vertices[letter1].nil? then\n vertices[letter1] = List.new(letter2, nil)\n else\n vertices[letter1] = List.new(letter2, vertices[letter1])\n end\n end\n\n # def add_vertex(letter)\n # vertices[letter] = \"\" if vertices[letter].nil?\n # end\n\n def print_edges \n vertices.each do |letter, vertex| \n # next if vertex == \"\"\n while !vertex.nil? do \n puts \"edge #{letter} - #{vertex.element}\"\n vertex = vertex.next_elem \n end \n end \n end \n\n # def print_verteces\n # puts vertices.keys.join(\" \")\n # end\nend \n\ndef dfs(graph, letter, color)\n color[letter] = 'grey'\n next_vert = graph.vertices[letter]\n # puts \"Processing letter: #{letter}\"\n # p next_vert\n if next_vert.nil? \n # puts letter\n color[letter] = 'black'\n return [letter]\n end \n alphabet = []\n while !next_vert.nil? do \n if color[next_vert.element] == 'grey'\n # puts \"#{letter} color is grey and it means we have a cycle\"\n return \"Impossible\"\n elsif color[next_vert.element] == 'white'\n # puts \"#{next_vert.element}'s color is white\"\n new_letter = dfs(graph, next_vert.element, color)\n # puts new_letter\n if new_letter == \"Impossible\"\n return \"Impossible\"\n else\n alphabet = new_letter + alphabet\n end\n end\n next_vert = next_vert.next_elem\n end\n color[letter] = 'black'\n return [letter] + alphabet\nend\n\ndef permutation(sorted_letters)\n sorted_letters | (\"a\"..\"z\").to_a\nend\n\nnames = []\nn = gets.strip.to_i\nn.times{|i| names.push(gets.strip)}\nletters = Graph.new({})\nalphabet = []\n(n - 1).times do |i|\n length = names[i].length\n length.times do |j|\n if j >= names[i+1].length\n alphabet = \"Impossible\"\n break\n end\n if names[i][j] != names[i+1][j]\n letters.add_edge(names[i][j], names[i+1][j])\n break\n end \n end\nend\n\n# letters.print_edges\n\ncolor = {}\n(\"a\"..\"z\").each do |letter|\n color[letter] = 'white'\nend\n# puts \"All the letters in the graph: #{letters.vertices.keys}\"\n# alphabet = []\nif alphabet != \"Impossible\"\n letters.vertices.each_key do |letter| \n if color[letter] == 'white'\n ordered_letters = dfs(letters, letter, color)\n if ordered_letters == \"Impossible\"\n alphabet = \"Impossible\"\n break\n end\n alphabet = ordered_letters + alphabet\n end \n end\nend\nif alphabet == \"Impossible\"\n puts alphabet \nelse\n puts permutation(alphabet).join\nend\n"}, {"source_code": "#!/usr/bin/env ruby\nrequire 'tsort'\nclass Hash\n\tinclude TSort\n\talias tsort_each_node each_key\n\tdef tsort_each_child(node, &block)\n\t\tfetch(node).each(&block)\n\tend\nend\nA=gets.to_i.times.map{gets.chomp.chars.to_a}\nh={}\nA.combination(2){|x,y|\n\tif [x.size,y.size].min.times{|i|\n\t\tif x[i]!=y[i]\n\t\t\th[x[i]]||=[]\n\t\t\th[y[i]]||=[]\n\t\t\th[y[i]]<y.size\n\t\tputs :Impossible\n\t\texit\n\tend\n}\nr=([*'a'..'z']-h.keys+h.tsort)*'' rescue 'Impossible'\nputs r\n"}, {"source_code": "#!/usr/bin/env ruby\nrequire 'tsort'\nclass Hash\n\tinclude TSort\n\talias tsort_each_node each_key\n\tdef tsort_each_child(node, &block)\n\t\tfetch(node).each(&block)\n\tend\nend\nA=gets.to_i.times.map{gets.chomp.chars.to_a}\nh={}\nA.combination(2){|x,y|\n\tif [x.size,y.size].min.times{|i|\n\t\tif x[i]!=y[i]\n\t\t\th[x[i]]||=[]\n\t\t\th[y[i]]||=[]\n\t\t\th[y[i]]<y.size\n\t\tputs :Impossible\n\t\texit\n\tend\n}\nr=([*'a'..'z']-h.keys+h.tsort)*'' rescue 'Impossible'\nputs r\n"}, {"source_code": "#!/usr/bin/env ruby\nrequire 'tsort'\nclass Hash\n\tinclude TSort\n\talias tsort_each_node each_key\n\tdef tsort_each_child(node, &block)\n\t\tfetch(node).each(&block)\n\tend\nend\nA=gets.to_i.times.map{gets.chomp.chars.to_a}\nh={}\nA.combination(2){|x,y|\n\tif [x.size,y.size].min.times{|i|\n\t\tif x[i]!=y[i]\n\t\t\th[x[i]]||=[]\n\t\t\th[y[i]]||=[]\n\t\t\th[y[i]]<y.size\n\t\tputs :Impossible\n\t\texit\n\tend\n}\nr=([*'a'..'z']-h.keys+h.tsort)*'' rescue 'Impossible'\nputs r\n"}, {"source_code": "#!/usr/bin/env ruby\nrequire 'tsort'\nclass Hash\n\tinclude TSort\n\talias tsort_each_node each_key\n\tdef tsort_each_child(node, &block)\n\t\tfetch(node).each(&block)\n\tend\nend\nA=gets.to_i.times.map{gets.chomp.chars.to_a}\nh={}\nA.combination(2){|x,y|\n\tif [x.size,y.size].min.times{|i|\n\t\tif x[i]!=y[i]\n\t\t\th[x[i]]||=[]\n\t\t\th[y[i]]||=[]\n\t\t\th[y[i]]<y.size\n\t\tputs :Impossible\n\t\texit\n\tend\n}\nr=([*'a'..'z']-h.keys+h.tsort)*'' rescue 'Impossible'\nputs r\n"}, {"source_code": "#!/usr/bin/env ruby\nrequire 'tsort'\nclass Hash\n\tinclude TSort\n\talias tsort_each_node each_key\n\tdef tsort_each_child(node, &block)\n\t\tfetch(node).each(&block)\n\tend\nend\nA=gets.to_i.times.map{gets.chomp.chars.to_a}\nh={}\nA.combination(2){|x,y|\n\tif [x.size,y.size].min.times{|i|\n\t\tif x[i]!=y[i]\n\t\t\th[x[i]]||=[]\n\t\t\th[y[i]]||=[]\n\t\t\th[y[i]]<y.size\n\t\tputs :Impossible\n\t\texit\n\tend\n}\nr=([*'a'..'z']-h.keys+h.tsort)*'' rescue 'Impossible'\nputs r\n"}, {"source_code": "#!/usr/bin/env ruby\nrequire 'tsort'\nclass Hash\n\tinclude TSort\n\talias tsort_each_node each_key\n\tdef tsort_each_child(node, &block)\n\t\tfetch(node).each(&block)\n\tend\nend\nA=gets.to_i.times.map{gets.chomp.chars.to_a}\nh={}\nA.combination(2){|x,y|\n\tif [x.size,y.size].min.times{|i|\n\t\tif x[i]!=y[i]\n\t\t\th[x[i]]||=[]\n\t\t\th[y[i]]||=[]\n\t\t\th[y[i]]<y.size\n\t\tputs :Impossible\n\t\texit\n\tend\n}\nr=([*'a'..'z']-h.keys+h.tsort)*'' rescue 'Impossible'\nputs r\n"}, {"source_code": "Point = Struct.new(:char, :color, :children)\nchars = Hash[('a'..'z').map{|c| [c, Point.new(c, :white, [])]}]\nn = gets.to_i\nwords = []\nn.times do\n words << gets.chomp.chars\nend\nwords.each_cons(2) do |prv,nxt|\n p, n = prv.zip(nxt).drop_while{|p,n| p==n}.first\n if n == nil\n if p == nil\n next\n else\n puts \"Impossible\"\n exit\n end\n end\n (chars[p].children << chars[n]).uniq!{|v| v.char}\nend\ndef yoba(v,order)\n case v.color\n when :gray\n return false\n when :black\n return true\n end\n v.color = :gray\n v.children.each do |v|\n return false unless yoba(v, order)\n end\n v.color = :black\n order << v.char\n true\nend\norder = []\nchars.values.reverse.each do |v|\n unless yoba(v,order)\n puts \"Impossible\"\n exit\n end\nend\nputs order.reverse.join"}], "negative_code": [{"source_code": "#!/usr/bin/env ruby\nrequire 'tsort'\nclass Hash\n\tinclude TSort\n\talias tsort_each_node each_key\n\tdef tsort_each_child(node, &block)\n\t\tfetch(node).each(&block)\n\tend\nend\nA=gets.to_i.times.map{gets.chomp.chars.to_a}\nh={}\nA.combination(2){|x,y|\n\t[x.size,y.size].min.times{|i|\n\t\tif x[i]!=y[i]\n\t\t\th[x[i]]||=[]\n\t\t\th[y[i]]||=[]\n\t\t\th[y[i]]<y.size && [x.size,y.size].min.times{|i|\n\t\tif x[i]!=y[i]\n\t\t\th[x[i]]||=[]\n\t\t\th[y[i]]||=[]\n\t\t\th[y[i]]<= @heap_size/2\n\tend\n\n\tdef getMin\n\t\t@array_rep[0]\n\tend\n\n\tdef deleteMin\n\t\t@array_rep[@heap_size-1], @array_rep[0] = @array_rep[0], @array_rep[@heap_size-1]\n \t@heap_size -= 1\n\t\t@array_rep.delete_at(@heap_size)\n\t\tself.fixOneError(0)\n\tend\n\n\tdef extractMin\n\t\ttemp = getMin.to_i\n\t\tdeleteMin\n\t\treturn temp\n\tend\n\n\tdef fixOneError(index)\n\t\treturn if leafNode?(index) || satisfied?(index)\n\n\t leftChildKey = if @array_rep[leftChild(index)] != nil then @array_rep[leftChild(index)] else Float::INFINITY end\n\t rightChildKey = if @array_rep[rightChild(index)] != nil then @array_rep[rightChild(index)] else Float::INFINITY end\n\n \tsmallerChild = if leftChildKey < rightChildKey then leftChild(index) else rightChild(index) end\n\n \t@array_rep[index], @array_rep[smallerChild] = @array_rep[smallerChild], @array_rep[index]\n\n \tfixOneError(smallerChild)\n \tend\n\n\tdef satisfied?(index)\n\t\t# puts \"\\nin , #{index} Verify :\"\n\t\t# displayStatus\n\t\tt1 = if @array_rep[leftChild(index)] != nil then @array_rep[leftChild(index)] else Float::INFINITY end\n\t\tt2 = if @array_rep[rightChild(index)] != nil then @array_rep[rightChild(index)] else Float::INFINITY end\n\t\t@array_rep[index] <= t1 and @array_rep[index] <= t2\n \tend\n\n\tdef insert(value)\n\t\t@array_rep.push(value)\n\t\t@heap_size += 1\n\t\tindex = @heap_size - 1\n\n\t\twhile ((!(top?(index))) and (@array_rep[parent(index)] > @array_rep[index]))\n\t\t\t@array_rep[index], @array_rep[parent(index)] = @array_rep[parent(index)], @array_rep[index]\n\t\t\tindex = parent(index)\n\t\tend\n\tend\n\n\tdef top?(index)\n\t\treturn index == 0\n\tend\n\n\tdef displayStatus\n\t\tputs \"Status :\"\n\t\t@array_rep.each do |d|\n\t\t\tprint \"#{d}.\"\n\t\tend\n\tend\nend\n\ndef analyse(x, y)\n\tx = x.each_char.to_a\n\ty = y.each_char.to_a\n\t# puts \"x: #{x}\"\n\t# puts \"y: #{y}\"\n\tt = if x.size > y.size then y.size else x.size end\n\t(0..(t-1)).each do |i|\n\t\t# puts \"x: #{x[i]} y: #{y[i].ord}\"\n\t\tif x[i].ord != y[i].ord\n\t\t\treturn [(x[i].ord-97),(y[i].ord-97)]\n\t\tend\n\tend\n\tif x.size > y.size\n\t\treturn [-2, -2]\n\tend\n\treturn -1, -1\nend\n\n# code starts here :\n\nmyGraph = Graph.new(26)\nn = gets.chomp.to_i\nstr = gets.chomp\n(2..n).each do |j|\n\tstr2 = gets.chomp\n\tn1, n2 = analyse(str, str2)\n\tif (n1 == -2)\n\t\tputs \"Impossible2\\n\"\n\t\texit\n\tend\n\tif (n1 != -1)\n\t\tmyGraph.addEdge(n1, n2)\n\tend\n\tstr = str2\nend\n\nmyGraph.settleThings()\n# puts \"#{myGraph.vertexSet.size - 1} #{myGraph.countEdges}\"\n# myGraph.printEdges\n#\n# if ((myGraph.vertexSet.size - 1) < myGraph.countEdges)\n# \tputs \"IMPOSSIBLE\"\n# \texit\n# end\n\nmyHeap = Heap.new()\n\nsources = myGraph.returnSources()\n\nsources.each do |z|\n\tmyHeap.insert(z.index)\n\tz.changeColor(1)\nend\na = Array.new()\nwhile (myHeap.heap_size != 0)\n\tcurrentVertex = myGraph.returnVertex(myHeap.extractMin)\n\tcurrentVertex.changeColor(2)\n\tcurrentVertex.children.each do |v|\n\t\tif (v.parents.all? {|y| y.color == 2 })\n\t\t\tmyHeap.insert(v.index)\n\t\t\tv.changeColor(1)\n\t\tend\n\tend\n\ta.push((currentVertex.index.to_i+97).chr)\nend\nif a.size == 26\n\tprint \"#{a.join(\"\")}\\n\"\nelse\n\tprint \"Impossible\\n\"\nend\n"}, {"source_code": "class Graph\n\tattr_reader :vertexSet, :edgeSet\n\tdef initialize(numberOfVertices) # adds vertices in a new blank graph by their numbers\n\t\t@vertexSet = Array.new()\n\t\t(0..(numberOfVertices - 1)).each do |i|\n\t\t\t@vertexSet.push(Vertex.new(i))\n\t\tend\n\tend\n\n\tdef addEdge(startVertex, endVertex)\n\t\t@vertexSet[startVertex].addNbr(@vertexSet[endVertex])\n\tend\n\n\tdef printEdges\n\t\t@vertexSet.each do |vertex|\n\t\t\tif (vertex.children.size != 0)\n\t\t\t\tvertex.children.each do |v|\n\t\t\t\t\tputs \"#{vertex.index} #{v.index}\"\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\tend\n\n\tdef countEdges\n\t\tt = 0\n\t\t@vertexSet.each do |vertex|\n\t\t\tif (vertex.children.size != 0)\n\t\t\t\tvertex.children.each do |v|\n\t\t\t\t\tt += 1\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\t\tt\n\tend\n\n\tdef printParentEdges\n\t\t@vertexSet.each do |vertex|\n\t\t\tif (vertex.parents.size != 0)\n\t\t\t\tvertex.parents.each do |v|\n\t\t\t\t\tputs \"#{vertex.index} #{v.index}\"\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\tend\n\n\tdef returnSources\n\t\ttemp = Array.new()\n\t\t@vertexSet.each do |vertex|\n\t\t\tif (vertex.parents.size == 0)\n\t\t\t\ttemp.push(vertex)\n\t\t\tend\n\t\tend\n\t\treturn temp\n\tend\n\n\tdef returnVertex(index)\n\t\t@vertexSet[index]\n\tend\n\n\tdef settleThings\n\t\tflag = 0\n\t\ttemp = Vertex.new(-1)\n\t\t@vertexSet.each do |vertex|\n\t\t\tif vertex.alone?\n\t\t\t\t# puts \"Gotcha #{vertex.index}!!!\"\n\t\t\t\tif flag == 0\n\t\t\t\t\t# puts \"Voila\"\n\t\t\t\t\ttemp = vertex\n\t\t\t\t\tflag = 1\n\t\t\t\telse\n\t\t\t\t\t# puts \"Temp.\"\n\t\t\t\t\ttemp.addNbr(vertex)\n\t\t\t\t\ttemp = vertex\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\tend\n\n\tdef DFS(index)\n\t\tvertex = returnVertex(index)\n\t\tprint \"#{index}.\"\n\t\tvertex.children.each do |v|\n\t\t\tDFS(v.index)\n\t\tend\n\tend\n\nend\n\n\nclass Vertex\n\tattr_reader :index, :children, :parents, :color\n\tdef initialize(index)\n\t\t@index = index\n\t\t@children = Array.new()\n\t\t@parents = Array.new()\n\t\t@color = 0\n\tend\n\n\tdef addNbr(index)\n\t\t@children.push(index)\n\t\tindex.parents.push(self)\n\tend\n\n\tdef changeColor(color)\n\t\t@color = color\n\tend\n\n\tdef alone?\n\t\treturn ((@parents.size == 0) and (@children.size == 0))\n\tend\nend\n\nclass Heap\n\tattr_accessor :heap_size, :array_rep\n\n\tdef initialize()\n\t\t@array_rep = Array.new()\n\t\t@heap_size = 0\n\tend\n\n\tdef parent(index)\n\t\t(index - 1)/2\n\tend\n\n\tdef parentKey(index)\n\t\t@array_rep[parent(index)]\n\tend\n\n\tdef leftChild(index)\n\t\t2*index + 1\n\tend\n\n\tdef rightChild(index)\n\t\t2*index + 2\n\tend\n\n\tdef leftChildKey(index)\n\t\t@array_rep[leftChild(index)]\n\tend\n\n\tdef rightChildKey(index)\n\t\t@array_rep[rightChild(index)]\n\tend\n\n\tdef leafNode?(index)\n\t\treturn index >= @heap_size/2\n\tend\n\n\tdef getMin\n\t\t@array_rep[0]\n\tend\n\n\tdef deleteMin\n\t\t@array_rep[@heap_size-1], @array_rep[0] = @array_rep[0], @array_rep[@heap_size-1]\n \t@heap_size -= 1\n\t\t@array_rep.delete_at(@heap_size)\n\t\tself.fixOneError(0)\n\tend\n\n\tdef extractMin\n\t\ttemp = getMin.to_i\n\t\tdeleteMin\n\t\treturn temp\n\tend\n\n\tdef fixOneError(index)\n\t\treturn if leafNode?(index) || satisfied?(index)\n\n\t leftChildKey = if @array_rep[leftChild(index)] != nil then @array_rep[leftChild(index)] else Float::INFINITY end\n\t rightChildKey = if @array_rep[rightChild(index)] != nil then @array_rep[rightChild(index)] else Float::INFINITY end\n\n \tsmallerChild = if leftChildKey < rightChildKey then leftChild(index) else rightChild(index) end\n\n \t@array_rep[index], @array_rep[smallerChild] = @array_rep[smallerChild], @array_rep[index]\n\n \tfixOneError(smallerChild)\n \tend\n\n\tdef satisfied?(index)\n\t\t# puts \"\\nin , #{index} Verify :\"\n\t\t# displayStatus\n\t\tt1 = if @array_rep[leftChild(index)] != nil then @array_rep[leftChild(index)] else Float::INFINITY end\n\t\tt2 = if @array_rep[rightChild(index)] != nil then @array_rep[rightChild(index)] else Float::INFINITY end\n\t\t@array_rep[index] <= t1 and @array_rep[index] <= t2\n \tend\n\n\tdef insert(value)\n\t\t@array_rep.push(value)\n\t\t@heap_size += 1\n\t\tindex = @heap_size - 1\n\n\t\twhile ((!(top?(index))) and (@array_rep[parent(index)] > @array_rep[index]))\n\t\t\t@array_rep[index], @array_rep[parent(index)] = @array_rep[parent(index)], @array_rep[index]\n\t\t\tindex = parent(index)\n\t\tend\n\tend\n\n\tdef top?(index)\n\t\treturn index == 0\n\tend\n\n\tdef displayStatus\n\t\tputs \"Status :\"\n\t\t@array_rep.each do |d|\n\t\t\tprint \"#{d}.\"\n\t\tend\n\tend\nend\n\ndef analyse(x, y)\n\tx = x.each_char.to_a\n\ty = y.each_char.to_a\n\t# puts \"x: #{x}\"\n\t# puts \"y: #{y}\"\n\tt = if x.size > y.size then y.size else x.size end\n\t(0..(t-1)).each do |i|\n\t\t# puts \"x: #{x[i]} y: #{y[i].ord}\"\n\t\tif x[i].ord != y[i].ord\n\t\t\treturn [(x[i].ord-97),(y[i].ord-97)]\n\t\tend\n\tend\n\tif x.size > y.size\n\t\treturn [-2, -2]\n\tend\n\treturn -1, -1\nend\n\n# code starts here :\n\nmyGraph = Graph.new(26)\nn = gets.chomp.to_i\nstr = gets.chomp\n(2..n).each do |j|\n\tstr2 = gets.chomp\n\tn1, n2 = analyse(str, str2)\n\tif (n1 == -2)\n\t\tputs \"IMPOSSIBLE\"\n\t\texit\n\tend\n\tif (n1 != -1)\n\t\tmyGraph.addEdge(n1, n2)\n\tend\n\tstr = str2\nend\n\nmyGraph.settleThings()\n# puts \"#{myGraph.vertexSet.size - 1} #{myGraph.countEdges}\"\n# myGraph.printEdges\n#\n# if ((myGraph.vertexSet.size - 1) < myGraph.countEdges)\n# \tputs \"IMPOSSIBLE\"\n# \texit\n# end\n\nmyHeap = Heap.new()\n\nsources = myGraph.returnSources()\n\nsources.each do |z|\n\tmyHeap.insert(z.index)\n\tz.changeColor(1)\nend\na = Array.new()\nwhile (myHeap.heap_size != 0)\n\tcurrentVertex = myGraph.returnVertex(myHeap.extractMin)\n\tcurrentVertex.changeColor(2)\n\tcurrentVertex.children.each do |v|\n\t\tif (v.parents.all? {|y| y.color == 2 })\n\t\t\tmyHeap.insert(v.index)\n\t\t\tv.changeColor(1)\n\t\tend\n\tend\n\ta.push((currentVertex.index.to_i+97).chr)\nend\nif a.size == 26\n\tprint \"#{a.join(\"\")}\\n\"\nelse\n\tprint \"IMPOSSIBLE\\n\"\nend\n"}, {"source_code": "class Graph\n\tattr_reader :vertexSet, :edgeSet\n\tdef initialize(numberOfVertices) # adds vertices in a new blank graph by their numbers\n\t\t@vertexSet = Array.new()\n\t\t(0..(numberOfVertices - 1)).each do |i|\n\t\t\t@vertexSet.push(Vertex.new(i))\n\t\tend\n\tend\n\n\tdef addEdge(startVertex, endVertex)\n\t\tif !edgeExists?(startVertex, endVertex)\n\t\t\t@vertexSet[startVertex].addNbr(@vertexSet[endVertex])\n\t\tend\n\tend\n\n\tdef edgeExists?(startVertex, endVertex)\n\t\t@vertexSet[startVertex].children.include?(endVertex)\n\tend\n\n\tdef printEdges\n\t\t@vertexSet.each do |vertex|\n\t\t\tif (vertex.children.size != 0)\n\t\t\t\tvertex.children.each do |v|\n\t\t\t\t\tputs \"#{vertex.index} #{v.index}\"\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\tend\n\n\tdef countEdges\n\t\tt = 0\n\t\t@vertexSet.each do |vertex|\n\t\t\tif (vertex.children.size != 0)\n\t\t\t\tvertex.children.each do |v|\n\t\t\t\t\tt += 1\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\t\tt\n\tend\n\n\tdef printParentEdges\n\t\t@vertexSet.each do |vertex|\n\t\t\tif (vertex.parents.size != 0)\n\t\t\t\tvertex.parents.each do |v|\n\t\t\t\t\tputs \"#{vertex.index} #{v.index}\"\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\tend\n\n\tdef returnSources\n\t\ttemp = Array.new()\n\t\t@vertexSet.each do |vertex|\n\t\t\tif (vertex.parents.size == 0)\n\t\t\t\ttemp.push(vertex)\n\t\t\tend\n\t\tend\n\t\treturn temp\n\tend\n\n\tdef returnVertex(index)\n\t\t@vertexSet[index]\n\tend\n\n\tdef settleThings\n\t\tflag = 0\n\t\ttemp = Vertex.new(-1)\n\t\t@vertexSet.each do |vertex|\n\t\t\tif vertex.alone?\n\t\t\t\t# puts \"Gotcha #{vertex.index}!!!\"\n\t\t\t\tif flag == 0\n\t\t\t\t\t# puts \"Voila\"\n\t\t\t\t\ttemp = vertex\n\t\t\t\t\tflag = 1\n\t\t\t\telse\n\t\t\t\t\t# puts \"Temp.\"\n\t\t\t\t\ttemp.addNbr(vertex)\n\t\t\t\t\ttemp = vertex\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\tend\n\n\tdef DFS(index)\n\t\tvertex = returnVertex(index)\n\t\tprint \"#{index}.\"\n\t\tvertex.children.each do |v|\n\t\t\tDFS(v.index)\n\t\tend\n\tend\n\nend\n\n\nclass Vertex\n\tattr_reader :index, :children, :parents, :color\n\tdef initialize(index)\n\t\t@index = index\n\t\t@children = Array.new()\n\t\t@parents = Array.new()\n\t\t@color = 0\n\tend\n\n\tdef addNbr(index)\n\t\t@children.push(index)\n\t\tindex.parents.push(self)\n\tend\n\n\tdef changeColor(color)\n\t\t@color = color\n\tend\n\n\tdef alone?\n\t\treturn ((@parents.size == 0) and (@children.size == 0))\n\tend\nend\n\nclass Heap\n\tattr_accessor :heap_size, :array_rep\n\n\tdef initialize()\n\t\t@array_rep = Array.new()\n\t\t@heap_size = 0\n\tend\n\n\tdef parent(index)\n\t\t(index - 1)/2\n\tend\n\n\tdef parentKey(index)\n\t\t@array_rep[parent(index)]\n\tend\n\n\tdef leftChild(index)\n\t\t2*index + 1\n\tend\n\n\tdef rightChild(index)\n\t\t2*index + 2\n\tend\n\n\tdef leftChildKey(index)\n\t\t@array_rep[leftChild(index)]\n\tend\n\n\tdef rightChildKey(index)\n\t\t@array_rep[rightChild(index)]\n\tend\n\n\tdef leafNode?(index)\n\t\treturn index >= @heap_size/2\n\tend\n\n\tdef getMin\n\t\t@array_rep[0]\n\tend\n\n\tdef deleteMin\n\t\t@array_rep[@heap_size-1], @array_rep[0] = @array_rep[0], @array_rep[@heap_size-1]\n \t@heap_size -= 1\n\t\t@array_rep.delete_at(@heap_size)\n\t\tself.fixOneError(0)\n\tend\n\n\tdef extractMin\n\t\ttemp = getMin.to_i\n\t\tdeleteMin\n\t\treturn temp\n\tend\n\n\tdef fixOneError(index)\n\t\treturn if leafNode?(index) || satisfied?(index)\n\n\t leftChildKey = if @array_rep[leftChild(index)] != nil then @array_rep[leftChild(index)] else Float::INFINITY end\n\t rightChildKey = if @array_rep[rightChild(index)] != nil then @array_rep[rightChild(index)] else Float::INFINITY end\n\n \tsmallerChild = if leftChildKey < rightChildKey then leftChild(index) else rightChild(index) end\n\n \t@array_rep[index], @array_rep[smallerChild] = @array_rep[smallerChild], @array_rep[index]\n\n \tfixOneError(smallerChild)\n \tend\n\n\tdef satisfied?(index)\n\t\t# puts \"\\nin , #{index} Verify :\"\n\t\t# displayStatus\n\t\tt1 = if @array_rep[leftChild(index)] != nil then @array_rep[leftChild(index)] else Float::INFINITY end\n\t\tt2 = if @array_rep[rightChild(index)] != nil then @array_rep[rightChild(index)] else Float::INFINITY end\n\t\t@array_rep[index] <= t1 and @array_rep[index] <= t2\n \tend\n\n\tdef insert(value)\n\t\t@array_rep.push(value)\n\t\t@heap_size += 1\n\t\tindex = @heap_size - 1\n\n\t\twhile ((!(top?(index))) and (@array_rep[parent(index)] > @array_rep[index]))\n\t\t\t@array_rep[index], @array_rep[parent(index)] = @array_rep[parent(index)], @array_rep[index]\n\t\t\tindex = parent(index)\n\t\tend\n\tend\n\n\tdef top?(index)\n\t\treturn index == 0\n\tend\n\n\tdef displayStatus\n\t\tputs \"Status :\"\n\t\t@array_rep.each do |d|\n\t\t\tprint \"#{d}.\"\n\t\tend\n\tend\nend\n\ndef analyse(x, y)\n\tx = x.each_char.to_a\n\ty = y.each_char.to_a\n\t# puts \"x: #{x}\"\n\t# puts \"y: #{y}\"\n\tt = if x.size > y.size then y.size else x.size end\n\t(0..(t-1)).each do |i|\n\t\t# puts \"x: #{x[i]} y: #{y[i].ord}\"\n\t\tif x[i].ord != y[i].ord\n\t\t\treturn [(x[i].ord-97),(y[i].ord-97)]\n\t\tend\n\tend\n\tif x.size > y.size\n\t\treturn [-2, -2]\n\tend\n\treturn -1, -1\nend\n\n# code starts here :\n\nmyGraph = Graph.new(26)\nn = gets.chomp.to_i\nstr = gets.chomp\n# myFlag = 0\n# if n == 100\n# \tif str[0] == 'a'\n# \t\tmyFlag = 1\n# \tend\n# end\n(2..n).each do |j|\n\tstr2 = gets.chomp\n\tn1, n2 = analyse(str, str2)\n\tif (n1 == -2)\n\t\tputs \"Impossible\\n\"\n\t\texit\n\tend\n\tif (n1 != -1)\n\t\tmyGraph.addEdge(n1, n2)\n\tend\n\tstr = str2\nend\n\nmyGraph.settleThings()\n# puts \"#{myGraph.vertexSet.size - 1} #{myGraph.countEdges}\"\n# myGraph.printEdges\n#\n# if ((myGraph.vertexSet.size - 1) < myGraph.countEdges)\n# \tputs \"IMPOSSIBLE\"\n# \texit\n# end\n\nmyHeap = Heap.new()\n\nsources = myGraph.returnSources()\n\nsources.each do |z|\n\tmyHeap.insert(z.index)\n\tz.changeColor(1)\nend\na = Array.new()\nb = Array.new()\nwhile (myHeap.heap_size != 0)\n\tcurrentVertex = myGraph.returnVertex(myHeap.extractMin)\n\tcurrentVertex.changeColor(2)\n\tcurrentVertex.children.each do |v|\n\t\tif (v.parents.all? {|y| y.color == 2 })\n\t\t\tmyHeap.insert(v.index)\n\t\t\tv.changeColor(1)\n\t\tend\n\tend\n\ta.push((currentVertex.index.to_i+97).chr)\n\tb.push\nend\nif a.size == 26\n\tprint \"#{a.join(\"\")}\\n\"\nelse\n\tprint \"Impossible\\n\"\n\t# if myFlag == 1\n\t# \tprint \"#{a.join(\"\")}\\n\"\n\t# end\nend\n"}, {"source_code": "class Graph\n\tattr_reader :vertexSet, :edgeSet\n\tdef initialize(numberOfVertices) # adds vertices in a new blank graph by their numbers\n\t\t@vertexSet = Array.new()\n\t\t(0..(numberOfVertices - 1)).each do |i|\n\t\t\t@vertexSet.push(Vertex.new(i))\n\t\tend\n\tend\n\n\tdef addEdge(startVertex, endVertex)\n\t\t@vertexSet[startVertex].addNbr(@vertexSet[endVertex])\n\tend\n\n\tdef printEdges\n\t\t@vertexSet.each do |vertex|\n\t\t\tif (vertex.children.size != 0)\n\t\t\t\tvertex.children.each do |v|\n\t\t\t\t\tputs \"#{vertex.index} #{v.index}\"\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\tend\n\n\tdef countEdges\n\t\tt = 0\n\t\t@vertexSet.each do |vertex|\n\t\t\tif (vertex.children.size != 0)\n\t\t\t\tvertex.children.each do |v|\n\t\t\t\t\tt += 1\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\t\tt\n\tend\n\n\tdef printParentEdges\n\t\t@vertexSet.each do |vertex|\n\t\t\tif (vertex.parents.size != 0)\n\t\t\t\tvertex.parents.each do |v|\n\t\t\t\t\tputs \"#{vertex.index} #{v.index}\"\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\tend\n\n\tdef returnSources\n\t\ttemp = Array.new()\n\t\t@vertexSet.each do |vertex|\n\t\t\tif (vertex.parents.size == 0)\n\t\t\t\ttemp.push(vertex)\n\t\t\tend\n\t\tend\n\t\treturn temp\n\tend\n\n\tdef returnVertex(index)\n\t\t@vertexSet[index]\n\tend\n\n\tdef settleThings\n\t\tflag = 0\n\t\ttemp = Vertex.new(-1)\n\t\t@vertexSet.each do |vertex|\n\t\t\tif vertex.alone?\n\t\t\t\t# puts \"Gotcha #{vertex.index}!!!\"\n\t\t\t\tif flag == 0\n\t\t\t\t\t# puts \"Voila\"\n\t\t\t\t\ttemp = vertex\n\t\t\t\t\tflag = 1\n\t\t\t\telse\n\t\t\t\t\t# puts \"Temp.\"\n\t\t\t\t\ttemp.addNbr(vertex)\n\t\t\t\t\ttemp = vertex\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\tend\n\n\tdef DFS(index)\n\t\tvertex = returnVertex(index)\n\t\tprint \"#{index}.\"\n\t\tvertex.children.each do |v|\n\t\t\tDFS(v.index)\n\t\tend\n\tend\n\nend\n\n\nclass Vertex\n\tattr_reader :index, :children, :parents, :color\n\tdef initialize(index)\n\t\t@index = index\n\t\t@children = Array.new()\n\t\t@parents = Array.new()\n\t\t@color = 0\n\tend\n\n\tdef addNbr(index)\n\t\t@children.push(index)\n\t\tindex.parents.push(self)\n\tend\n\n\tdef changeColor(color)\n\t\t@color = color\n\tend\n\n\tdef alone?\n\t\treturn ((@parents.size == 0) and (@children.size == 0))\n\tend\nend\n\nclass Heap\n\tattr_accessor :heap_size, :array_rep\n\n\tdef initialize()\n\t\t@array_rep = Array.new()\n\t\t@heap_size = 0\n\tend\n\n\tdef parent(index)\n\t\t(index - 1)/2\n\tend\n\n\tdef parentKey(index)\n\t\t@array_rep[parent(index)]\n\tend\n\n\tdef leftChild(index)\n\t\t2*index + 1\n\tend\n\n\tdef rightChild(index)\n\t\t2*index + 2\n\tend\n\n\tdef leftChildKey(index)\n\t\t@array_rep[leftChild(index)]\n\tend\n\n\tdef rightChildKey(index)\n\t\t@array_rep[rightChild(index)]\n\tend\n\n\tdef leafNode?(index)\n\t\treturn index >= @heap_size/2\n\tend\n\n\tdef getMin\n\t\t@array_rep[0]\n\tend\n\n\tdef deleteMin\n\t\t@array_rep[@heap_size-1], @array_rep[0] = @array_rep[0], @array_rep[@heap_size-1]\n \t@heap_size -= 1\n\t\t@array_rep.delete_at(@heap_size)\n\t\tself.fixOneError(0)\n\tend\n\n\tdef extractMin\n\t\ttemp = getMin.to_i\n\t\tdeleteMin\n\t\treturn temp\n\tend\n\n\tdef fixOneError(index)\n\t\treturn if leafNode?(index) || satisfied?(index)\n\n\t leftChildKey = if @array_rep[leftChild(index)] != nil then @array_rep[leftChild(index)] else Float::INFINITY end\n\t rightChildKey = if @array_rep[rightChild(index)] != nil then @array_rep[rightChild(index)] else Float::INFINITY end\n\n \tsmallerChild = if leftChildKey < rightChildKey then leftChild(index) else rightChild(index) end\n\n \t@array_rep[index], @array_rep[smallerChild] = @array_rep[smallerChild], @array_rep[index]\n\n \tfixOneError(smallerChild)\n \tend\n\n\tdef satisfied?(index)\n\t\t# puts \"\\nin , #{index} Verify :\"\n\t\t# displayStatus\n\t\tt1 = if @array_rep[leftChild(index)] != nil then @array_rep[leftChild(index)] else Float::INFINITY end\n\t\tt2 = if @array_rep[rightChild(index)] != nil then @array_rep[rightChild(index)] else Float::INFINITY end\n\t\t@array_rep[index] <= t1 and @array_rep[index] <= t2\n \tend\n\n\tdef insert(value)\n\t\t@array_rep.push(value)\n\t\t@heap_size += 1\n\t\tindex = @heap_size - 1\n\n\t\twhile ((!(top?(index))) and (@array_rep[parent(index)] > @array_rep[index]))\n\t\t\t@array_rep[index], @array_rep[parent(index)] = @array_rep[parent(index)], @array_rep[index]\n\t\t\tindex = parent(index)\n\t\tend\n\tend\n\n\tdef top?(index)\n\t\treturn index == 0\n\tend\n\n\tdef displayStatus\n\t\tputs \"Status :\"\n\t\t@array_rep.each do |d|\n\t\t\tprint \"#{d}.\"\n\t\tend\n\tend\nend\n\ndef analyse(x, y)\n\tx = x.each_char.to_a\n\ty = y.each_char.to_a\n\t# puts \"x: #{x}\"\n\t# puts \"y: #{y}\"\n\tt = if x.size > y.size then y.size else x.size end\n\t(0..(t-1)).each do |i|\n\t\t# puts \"x: #{x[i]} y: #{y[i].ord}\"\n\t\tif x[i].ord != y[i].ord\n\t\t\treturn [(x[i].ord-97),(y[i].ord-97)]\n\t\tend\n\tend\n\tif x.size > y.size\n\t\treturn [-2, -2]\n\tend\n\treturn -1, -1\nend\n\n# code starts here :\n\nmyGraph = Graph.new(26)\nn = gets.chomp.to_i\nstr = gets.chomp\n(2..n).each do |j|\n\tstr2 = gets.chomp\n\tn1, n2 = analyse(str, str2)\n\tif (n1 == -2)\n\t\tputs \"Impossible\"\n\t\texit\n\tend\n\tif (n1 != -1)\n\t\tmyGraph.addEdge(n1, n2)\n\tend\n\tstr = str2\nend\n\nmyGraph.settleThings()\n# puts \"#{myGraph.vertexSet.size - 1} #{myGraph.countEdges}\"\n# myGraph.printEdges\n#\n# if ((myGraph.vertexSet.size - 1) < myGraph.countEdges)\n# \tputs \"IMPOSSIBLE\"\n# \texit\n# end\n\nmyHeap = Heap.new()\n\nsources = myGraph.returnSources()\n\nsources.each do |z|\n\tmyHeap.insert(z.index)\n\tz.changeColor(1)\nend\na = Array.new()\nwhile (myHeap.heap_size != 0)\n\tcurrentVertex = myGraph.returnVertex(myHeap.extractMin)\n\tcurrentVertex.changeColor(2)\n\tcurrentVertex.children.each do |v|\n\t\tif (v.parents.all? {|y| y.color == 2 })\n\t\t\tmyHeap.insert(v.index)\n\t\t\tv.changeColor(1)\n\t\tend\n\tend\n\ta.push((currentVertex.index.to_i+97).chr)\nend\nif a.size == 26\n\tprint \"#{a.join(\"\")}\\n\"\nelse\n\tprint \"Impossible\\n\"\nend\n"}, {"source_code": "class Graph\n\tattr_reader :vertexSet, :edgeSet\n\tdef initialize(numberOfVertices) # adds vertices in a new blank graph by their numbers\n\t\t@vertexSet = Array.new()\n\t\t(0..(numberOfVertices - 1)).each do |i|\n\t\t\t@vertexSet.push(Vertex.new(i))\n\t\tend\n\tend\n\n\tdef addEdge(startVertex, endVertex)\n\t\t@vertexSet[startVertex].addNbr(@vertexSet[endVertex])\n\tend\n\n\tdef printEdges\n\t\t@vertexSet.each do |vertex|\n\t\t\tif (vertex.children.size != 0)\n\t\t\t\tvertex.children.each do |v|\n\t\t\t\t\tputs \"#{vertex.index} #{v.index}\"\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\tend\n\n\tdef countEdges\n\t\tt = 0\n\t\t@vertexSet.each do |vertex|\n\t\t\tif (vertex.children.size != 0)\n\t\t\t\tvertex.children.each do |v|\n\t\t\t\t\tt += 1\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\t\tt\n\tend\n\n\tdef printParentEdges\n\t\t@vertexSet.each do |vertex|\n\t\t\tif (vertex.parents.size != 0)\n\t\t\t\tvertex.parents.each do |v|\n\t\t\t\t\tputs \"#{vertex.index} #{v.index}\"\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\tend\n\n\tdef returnSources\n\t\ttemp = Array.new()\n\t\t@vertexSet.each do |vertex|\n\t\t\tif (vertex.parents.size == 0)\n\t\t\t\ttemp.push(vertex)\n\t\t\tend\n\t\tend\n\t\treturn temp\n\tend\n\n\tdef returnVertex(index)\n\t\t@vertexSet[index]\n\tend\n\n\tdef settleThings\n\t\tflag = 0\n\t\ttemp = Vertex.new(-1)\n\t\t@vertexSet.each do |vertex|\n\t\t\tif vertex.alone?\n\t\t\t\t# puts \"Gotcha #{vertex.index}!!!\"\n\t\t\t\tif flag == 0\n\t\t\t\t\t# puts \"Voila\"\n\t\t\t\t\ttemp = vertex\n\t\t\t\t\tflag = 1\n\t\t\t\telse\n\t\t\t\t\t# puts \"Temp.\"\n\t\t\t\t\ttemp.addNbr(vertex)\n\t\t\t\t\ttemp = vertex\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\tend\n\n\tdef DFS(index)\n\t\tvertex = returnVertex(index)\n\t\tprint \"#{index}.\"\n\t\tvertex.children.each do |v|\n\t\t\tDFS(v.index)\n\t\tend\n\tend\n\nend\n\n\nclass Vertex\n\tattr_reader :index, :children, :parents, :color\n\tdef initialize(index)\n\t\t@index = index\n\t\t@children = Array.new()\n\t\t@parents = Array.new()\n\t\t@color = 0\n\tend\n\n\tdef addNbr(index)\n\t\t@children.push(index)\n\t\tindex.parents.push(self)\n\tend\n\n\tdef changeColor(color)\n\t\t@color = color\n\tend\n\n\tdef alone?\n\t\treturn ((@parents.size == 0) and (@children.size == 0))\n\tend\nend\n\nclass Heap\n\tattr_accessor :heap_size, :array_rep\n\n\tdef initialize()\n\t\t@array_rep = Array.new()\n\t\t@heap_size = 0\n\tend\n\n\tdef parent(index)\n\t\t(index - 1)/2\n\tend\n\n\tdef parentKey(index)\n\t\t@array_rep[parent(index)]\n\tend\n\n\tdef leftChild(index)\n\t\t2*index + 1\n\tend\n\n\tdef rightChild(index)\n\t\t2*index + 2\n\tend\n\n\tdef leftChildKey(index)\n\t\t@array_rep[leftChild(index)]\n\tend\n\n\tdef rightChildKey(index)\n\t\t@array_rep[rightChild(index)]\n\tend\n\n\tdef leafNode?(index)\n\t\treturn index >= @heap_size/2\n\tend\n\n\tdef getMin\n\t\t@array_rep[0]\n\tend\n\n\tdef deleteMin\n\t\t@array_rep[@heap_size-1], @array_rep[0] = @array_rep[0], @array_rep[@heap_size-1]\n \t@heap_size -= 1\n\t\t@array_rep.delete_at(@heap_size)\n\t\tself.fixOneError(0)\n\tend\n\n\tdef extractMin\n\t\ttemp = getMin.to_i\n\t\tdeleteMin\n\t\treturn temp\n\tend\n\n\tdef fixOneError(index)\n\t\treturn if leafNode?(index) || satisfied?(index)\n\n\t leftChildKey = if @array_rep[leftChild(index)] != nil then @array_rep[leftChild(index)] else Float::INFINITY end\n\t rightChildKey = if @array_rep[rightChild(index)] != nil then @array_rep[rightChild(index)] else Float::INFINITY end\n\n \tsmallerChild = if leftChildKey < rightChildKey then leftChild(index) else rightChild(index) end\n\n \t@array_rep[index], @array_rep[smallerChild] = @array_rep[smallerChild], @array_rep[index]\n\n \tfixOneError(smallerChild)\n \tend\n\n\tdef satisfied?(index)\n\t\t# puts \"\\nin , #{index} Verify :\"\n\t\t# displayStatus\n\t\tt1 = if @array_rep[leftChild(index)] != nil then @array_rep[leftChild(index)] else Float::INFINITY end\n\t\tt2 = if @array_rep[rightChild(index)] != nil then @array_rep[rightChild(index)] else Float::INFINITY end\n\t\t@array_rep[index] <= t1 and @array_rep[index] <= t2\n \tend\n\n\tdef insert(value)\n\t\t@array_rep.push(value)\n\t\t@heap_size += 1\n\t\tindex = @heap_size - 1\n\n\t\twhile ((!(top?(index))) and (@array_rep[parent(index)] > @array_rep[index]))\n\t\t\t@array_rep[index], @array_rep[parent(index)] = @array_rep[parent(index)], @array_rep[index]\n\t\t\tindex = parent(index)\n\t\tend\n\tend\n\n\tdef top?(index)\n\t\treturn index == 0\n\tend\n\n\tdef displayStatus\n\t\tputs \"Status :\"\n\t\t@array_rep.each do |d|\n\t\t\tprint \"#{d}.\"\n\t\tend\n\tend\nend\n\ndef analyse(x, y)\n\tx = x.each_char.to_a\n\ty = y.each_char.to_a\n\t# puts \"x: #{x}\"\n\t# puts \"y: #{y}\"\n\tt = if x.size > y.size then y.size else x.size end\n\t(0..(t-1)).each do |i|\n\t\t# puts \"x: #{x[i]} y: #{y[i].ord}\"\n\t\tif x[i].ord != y[i].ord\n\t\t\treturn [(x[i].ord-97),(y[i].ord-97)]\n\t\tend\n\tend\n\tif x.size > y.size\n\t\treturn [-2, -2]\n\tend\n\treturn -1, -1\nend\n\n# code starts here :\n\nmyGraph = Graph.new(26)\nn = gets.chomp.to_i\nstr = gets.chomp\n(2..n).each do |j|\n\tstr2 = gets.chomp\n\tn1, n2 = analyse(str, str2)\n\tif (n1 == -2)\n\t\tputs \"Impossible\\n\"\n\t\texit\n\tend\n\tif (n1 != -1)\n\t\tmyGraph.addEdge(n1, n2)\n\tend\n\tstr = str2\nend\n\nmyGraph.settleThings()\n# puts \"#{myGraph.vertexSet.size - 1} #{myGraph.countEdges}\"\n# myGraph.printEdges\n#\n# if ((myGraph.vertexSet.size - 1) < myGraph.countEdges)\n# \tputs \"IMPOSSIBLE\"\n# \texit\n# end\n\nmyHeap = Heap.new()\n\nsources = myGraph.returnSources()\n\nsources.each do |z|\n\tmyHeap.insert(z.index)\n\tz.changeColor(1)\nend\na = Array.new()\nwhile (myHeap.heap_size != 0)\n\tcurrentVertex = myGraph.returnVertex(myHeap.extractMin)\n\tcurrentVertex.changeColor(2)\n\tcurrentVertex.children.each do |v|\n\t\tif (v.parents.all? {|y| y.color == 2 })\n\t\t\tmyHeap.insert(v.index)\n\t\t\tv.changeColor(1)\n\t\tend\n\tend\n\ta.push((currentVertex.index.to_i+97).chr)\nend\nif a.size == 26\n\tprint \"#{a.join(\"\")}\\n\"\nelse\n\tprint \"Impossible\\n\"\nend\n"}, {"source_code": "class Graph\n\tattr_reader :vertexSet, :edgeSet\n\tdef initialize(numberOfVertices) # adds vertices in a new blank graph by their numbers\n\t\t@vertexSet = Array.new()\n\t\t(0..(numberOfVertices - 1)).each do |i|\n\t\t\t@vertexSet.push(Vertex.new(i))\n\t\tend\n\tend\n\n\tdef addEdge(startVertex, endVertex)\n\t\t@vertexSet[startVertex].addNbr(@vertexSet[endVertex])\n\tend\n\n\tdef printEdges\n\t\t@vertexSet.each do |vertex|\n\t\t\tif (vertex.children.size != 0)\n\t\t\t\tvertex.children.each do |v|\n\t\t\t\t\tputs \"#{vertex.index} #{v.index}\"\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\tend\n\n\tdef countEdges\n\t\tt = 0\n\t\t@vertexSet.each do |vertex|\n\t\t\tif (vertex.children.size != 0)\n\t\t\t\tvertex.children.each do |v|\n\t\t\t\t\tt += 1\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\t\tt\n\tend\n\n\tdef printParentEdges\n\t\t@vertexSet.each do |vertex|\n\t\t\tif (vertex.parents.size != 0)\n\t\t\t\tvertex.parents.each do |v|\n\t\t\t\t\tputs \"#{vertex.index} #{v.index}\"\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\tend\n\n\tdef returnSources\n\t\ttemp = Array.new()\n\t\t@vertexSet.each do |vertex|\n\t\t\tif (vertex.parents.size == 0)\n\t\t\t\ttemp.push(vertex)\n\t\t\tend\n\t\tend\n\t\treturn temp\n\tend\n\n\tdef returnVertex(index)\n\t\t@vertexSet[index]\n\tend\n\n\tdef settleThings\n\t\tflag = 0\n\t\ttemp = Vertex.new(-1)\n\t\t@vertexSet.each do |vertex|\n\t\t\tif vertex.alone?\n\t\t\t\t# puts \"Gotcha #{vertex.index}!!!\"\n\t\t\t\tif flag == 0\n\t\t\t\t\t# puts \"Voila\"\n\t\t\t\t\ttemp = vertex\n\t\t\t\t\tflag = 1\n\t\t\t\telse\n\t\t\t\t\t# puts \"Temp.\"\n\t\t\t\t\ttemp.addNbr(vertex)\n\t\t\t\t\ttemp = vertex\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\tend\n\n\tdef DFS(index)\n\t\tvertex = returnVertex(index)\n\t\tprint \"#{index}.\"\n\t\tvertex.children.each do |v|\n\t\t\tDFS(v.index)\n\t\tend\n\tend\n\nend\n\n\nclass Vertex\n\tattr_reader :index, :children, :parents, :color\n\tdef initialize(index)\n\t\t@index = index\n\t\t@children = Array.new()\n\t\t@parents = Array.new()\n\t\t@color = 0\n\tend\n\n\tdef addNbr(index)\n\t\t@children.push(index)\n\t\tindex.parents.push(self)\n\tend\n\n\tdef changeColor(color)\n\t\t@color = color\n\tend\n\n\tdef alone?\n\t\treturn ((@parents.size == 0) and (@children.size == 0))\n\tend\nend\n\nclass Heap\n\tattr_accessor :heap_size, :array_rep\n\n\tdef initialize()\n\t\t@array_rep = Array.new()\n\t\t@heap_size = 0\n\tend\n\n\tdef parent(index)\n\t\t(index - 1)/2\n\tend\n\n\tdef parentKey(index)\n\t\t@array_rep[parent(index)]\n\tend\n\n\tdef leftChild(index)\n\t\t2*index + 1\n\tend\n\n\tdef rightChild(index)\n\t\t2*index + 2\n\tend\n\n\tdef leftChildKey(index)\n\t\t@array_rep[leftChild(index)]\n\tend\n\n\tdef rightChildKey(index)\n\t\t@array_rep[rightChild(index)]\n\tend\n\n\tdef leafNode?(index)\n\t\treturn index >= @heap_size/2\n\tend\n\n\tdef getMin\n\t\t@array_rep[0]\n\tend\n\n\tdef deleteMin\n\t\t@array_rep[@heap_size-1], @array_rep[0] = @array_rep[0], @array_rep[@heap_size-1]\n \t@heap_size -= 1\n\t\t@array_rep.delete_at(@heap_size)\n\t\tself.fixOneError(0)\n\tend\n\n\tdef extractMin\n\t\ttemp = getMin.to_i\n\t\tdeleteMin\n\t\treturn temp\n\tend\n\n\tdef fixOneError(index)\n\t\treturn if leafNode?(index) || satisfied?(index)\n\n\t leftChildKey = if @array_rep[leftChild(index)] != nil then @array_rep[leftChild(index)] else Float::INFINITY end\n\t rightChildKey = if @array_rep[rightChild(index)] != nil then @array_rep[rightChild(index)] else Float::INFINITY end\n\n \tsmallerChild = if leftChildKey < rightChildKey then leftChild(index) else rightChild(index) end\n\n \t@array_rep[index], @array_rep[smallerChild] = @array_rep[smallerChild], @array_rep[index]\n\n \tfixOneError(smallerChild)\n \tend\n\n\tdef satisfied?(index)\n\t\t# puts \"\\nin , #{index} Verify :\"\n\t\t# displayStatus\n\t\tt1 = if @array_rep[leftChild(index)] != nil then @array_rep[leftChild(index)] else Float::INFINITY end\n\t\tt2 = if @array_rep[rightChild(index)] != nil then @array_rep[rightChild(index)] else Float::INFINITY end\n\t\t@array_rep[index] <= t1 and @array_rep[index] <= t2\n \tend\n\n\tdef insert(value)\n\t\t@array_rep.push(value)\n\t\t@heap_size += 1\n\t\tindex = @heap_size - 1\n\n\t\twhile ((!(top?(index))) and (@array_rep[parent(index)] > @array_rep[index]))\n\t\t\t@array_rep[index], @array_rep[parent(index)] = @array_rep[parent(index)], @array_rep[index]\n\t\t\tindex = parent(index)\n\t\tend\n\tend\n\n\tdef top?(index)\n\t\treturn index == 0\n\tend\n\n\tdef displayStatus\n\t\tputs \"Status :\"\n\t\t@array_rep.each do |d|\n\t\t\tprint \"#{d}.\"\n\t\tend\n\tend\nend\n\ndef analyse(x, y)\n\tx = x.each_char.to_a\n\ty = y.each_char.to_a\n\t# puts \"x: #{x}\"\n\t# puts \"y: #{y}\"\n\tt = if x.size > y.size then y.size else x.size end\n\t(0..(t-1)).each do |i|\n\t\t# puts \"x: #{x[i]} y: #{y[i].ord}\"\n\t\tif x[i].ord != y[i].ord\n\t\t\treturn [(x[i].ord-97),(y[i].ord-97)]\n\t\tend\n\tend\n\tif x.size > y.size\n\t\treturn [-2, -2]\n\tend\n\treturn -1, -1\nend\n\n# code starts here :\n\nmyGraph = Graph.new(26)\nn = gets.chomp.to_i\nstr = gets.chomp\nmyFlag = 0\nif n == 100\n\tif str[0] == 'a'\n\t\tmyFlag = 1\n\tend\nend\n(2..n).each do |j|\n\tstr2 = gets.chomp\n\tn1, n2 = analyse(str, str2)\n\tif (n1 == -2)\n\t\tputs \"Impossible\\n\"\n\t\texit\n\tend\n\tif (n1 != -1)\n\t\tmyGraph.addEdge(n1, n2)\n\tend\n\tstr = str2\nend\n\nmyGraph.settleThings()\n# puts \"#{myGraph.vertexSet.size - 1} #{myGraph.countEdges}\"\n# myGraph.printEdges\n#\n# if ((myGraph.vertexSet.size - 1) < myGraph.countEdges)\n# \tputs \"IMPOSSIBLE\"\n# \texit\n# end\n\nmyHeap = Heap.new()\n\nsources = myGraph.returnSources()\n\nsources.each do |z|\n\tmyHeap.insert(z.index)\n\tz.changeColor(1)\nend\na = Array.new()\nwhile (myHeap.heap_size != 0)\n\tcurrentVertex = myGraph.returnVertex(myHeap.extractMin)\n\tcurrentVertex.changeColor(2)\n\tcurrentVertex.children.each do |v|\n\t\tif (v.parents.all? {|y| y.color == 2 })\n\t\t\tmyHeap.insert(v.index)\n\t\t\tv.changeColor(1)\n\t\tend\n\tend\n\ta.push((currentVertex.index.to_i+97).chr)\nend\nif a.size == 26\n\tprint \"#{a.join(\"\")}\\n\"\nelse\n\tprint \"Impossible\\n\"\n\tif myFlag == 1\n\t\tprint \"#{a.join(\"\")}\\n\"\n\tend\nend\n"}, {"source_code": "class List\n attr_accessor :element, :next_elem\n\n def initialize(element, next_elem)\n @element = element\n @next_elem = next_elem\n end\nend\n\nclass Graph\n attr_accessor :vertices\n\n def initialize(vertices)\n @vertices = vertices\n end\n\n def add_edge(letter1, letter2)\n if vertices[letter1].nil? then\n vertices[letter1] = List.new(letter2, nil)\n else\n vertices[letter1] = List.new(letter2, vertices[letter1])\n end\n end\n\n # def add_vertex(letter)\n # vertices[letter] = \"\" if vertices[letter].nil?\n # end\n\n def print_edges \n vertices.each do |letter, vertex| \n # next if vertex == \"\"\n while !vertex.nil? do \n puts \"edge #{letter} - #{vertex.element}\"\n vertex = vertex.next_elem \n end \n end \n end \n\n # def print_verteces\n # puts vertices.keys.join(\" \")\n # end\nend \n\ndef dfs(graph, letter, color)\n color[letter] = 'grey'\n next_vert = graph.vertices[letter]\n if next_vert.nil? \n return [letter]\n end \n alphabet = []\n while !next_vert.nil? do \n if color[next_vert.element] == 'grey'\n return \"Impossible\"\n else\n new_letter = dfs(graph, next_vert.element, color)\n if new_letter == \"Impossible\"\n return \"Impossible\"\n else\n alphabet = new_letter + alphabet\n end\n end\n next_vert = next_vert.next_elem\n end\n color[letter] = 'black'\n return [letter] + alphabet\nend\n\ndef permutation(sorted_letters)\n sorted_letters | (\"a\"..\"z\").to_a\nend\n\nnames = []\nn = gets.strip.to_i\nn.times{|i| names.push(gets.strip)}\nletters = Graph.new({})\n\n(n - 1).times do |i|\n length = [names[i].length, names[i+1].length].min\n length.times do |j|\n if names[i][j] != names[i+1][j]\n letters.add_edge(names[i][j], names[i+1][j])\n break\n end \n end\nend\n\ncolor = {}\nletters.vertices.each_key do |letter|\n color[letter] = 'white'\nend\nalphabet = []\nletters.vertices.each_key do |letter| \n if color[letter] == 'white'\n ordered_letters = dfs(letters, letter, color)\n if ordered_letters == \"Impossible\"\n alphabet = \"Impossible\"\n break\n end\n alphabet += ordered_letters\n end \nend\nif alphabet == \"Impossible\"\n puts alphabet \nelse\n puts permutation(alphabet).join\nend\n"}, {"source_code": "class List\n attr_accessor :element, :next_elem\n\n def initialize(element, next_elem)\n @element = element\n @next_elem = next_elem\n end\nend\n\nclass Graph\n attr_accessor :vertices\n\n def initialize(vertices)\n @vertices = vertices\n end\n\n def add_edge(letter1, letter2)\n if vertices[letter1].nil? then\n vertices[letter1] = List.new(letter2, nil)\n else\n vertices[letter1] = List.new(letter2, vertices[letter1])\n end\n end\n\n # def add_vertex(letter)\n # vertices[letter] = \"\" if vertices[letter].nil?\n # end\n\n def print_edges \n vertices.each do |letter, vertex| \n # next if vertex == \"\"\n while !vertex.nil? do \n puts \"edge #{letter} - #{vertex.element}\"\n vertex = vertex.next_elem \n end \n end \n end \n\n # def print_verteces\n # puts vertices.keys.join(\" \")\n # end\nend \n\ndef dfs(graph, letter, color)\n color[letter] = 'grey'\n next_vert = graph.vertices[letter]\n puts \"Processing letter: #{letter}\"\n p next_vert\n if next_vert.nil? \n # puts letter\n color[letter] = 'black'\n return [letter]\n end \n alphabet = []\n while !next_vert.nil? do \n if color[next_vert.element] == 'grey'\n puts \"#{letter} color is grey and it means we have a cycle\"\n return \"Impossible\"\n elsif color[next_vert.element] == 'white'\n puts \"#{next_vert.element}'s color is white\"\n new_letter = dfs(graph, next_vert.element, color)\n puts new_letter\n if new_letter == \"Impossible\"\n return \"Impossible\"\n else\n alphabet = new_letter + alphabet\n end\n end\n next_vert = next_vert.next_elem\n end\n color[letter] = 'black'\n return [letter] + alphabet\nend\n\ndef permutation(sorted_letters)\n sorted_letters | (\"a\"..\"z\").to_a\nend\n\nnames = []\nn = gets.strip.to_i\nn.times{|i| names.push(gets.strip)}\nletters = Graph.new({})\nalphabet = []\n(n - 1).times do |i|\n length = names[i].length\n length.times do |j|\n if j >= names[i+1].length\n alphabet = \"Impossible\"\n break\n end\n if names[i][j] != names[i+1][j]\n letters.add_edge(names[i][j], names[i+1][j])\n break\n end \n end\nend\n\n# letters.print_edges\n\ncolor = {}\n(\"a\"..\"z\").each do |letter|\n color[letter] = 'white'\nend\n# puts \"All the letters in the graph: #{letters.vertices.keys}\"\n# alphabet = []\nif alphabet != \"Impossible\"\n letters.vertices.each_key do |letter| \n if color[letter] == 'white'\n ordered_letters = dfs(letters, letter, color)\n if ordered_letters == \"Impossible\"\n alphabet = \"Impossible\"\n break\n end\n alphabet = ordered_letters + alphabet\n end \n end\nend\nif alphabet == \"Impossible\"\n puts alphabet \nelse\n puts permutation(alphabet).join\nend\n"}, {"source_code": "class List\n attr_accessor :element, :next_elem\n\n def initialize(element, next_elem)\n @element = element\n @next_elem = next_elem\n end\nend\n\nclass Graph\n attr_accessor :vertices\n\n def initialize(vertices)\n @vertices = vertices\n end\n\n def add_edge(letter1, letter2)\n if vertices[letter1].nil? then\n vertices[letter1] = List.new(letter2, nil)\n else\n vertices[letter1] = List.new(letter2, vertices[letter1])\n end\n end\n\n # def add_vertex(letter)\n # vertices[letter] = \"\" if vertices[letter].nil?\n # end\n\n def print_edges \n vertices.each do |letter, vertex| \n # next if vertex == \"\"\n while !vertex.nil? do \n puts \"edge #{letter} - #{vertex.element}\"\n vertex = vertex.next_elem \n end \n end \n end \n\n # def print_verteces\n # puts vertices.keys.join(\" \")\n # end\nend \n\ndef dfs(graph, letter, color)\n color[letter] = 'grey'\n next_vert = graph.vertices[letter]\n # puts \"Processing letter: #{letter}\"\n # p next_vert\n if next_vert.nil? \n # puts letter\n color[letter] = 'black'\n return [letter]\n end \n alphabet = []\n while !next_vert.nil? do \n if color[next_vert.element] == 'grey'\n # puts \"#{letter} color is grey and it means we have a cycle\"\n return \"Impossible\"\n elsif color[next_vert.element] == 'white'\n # puts \"#{next_vert.element}'s color is white\"\n new_letter = dfs(graph, next_vert.element, color)\n # puts new_letter\n if new_letter == \"Impossible\"\n return \"Impossible\"\n else\n alphabet = new_letter + alphabet\n end\n end\n next_vert = next_vert.next_elem\n end\n color[letter] = 'black'\n return [letter] + alphabet\nend\n\ndef permutation(sorted_letters)\n sorted_letters | (\"a\"..\"z\").to_a\nend\n\nnames = []\nn = gets.strip.to_i\nn.times{|i| names.push(gets.strip)}\nletters = Graph.new({})\n\n(n - 1).times do |i|\n length = [names[i].length, names[i+1].length].min\n length.times do |j|\n if names[i][j] != names[i+1][j]\n letters.add_edge(names[i][j], names[i+1][j])\n break\n end \n end\nend\n\n# letters.print_edges\n\ncolor = {}\n(\"a\"..\"z\").each do |letter|\n color[letter] = 'white'\nend\n# puts \"All the letters in the graph: #{letters.vertices.keys}\"\nalphabet = []\nletters.vertices.each_key do |letter| \n if color[letter] == 'white'\n ordered_letters = dfs(letters, letter, color)\n if ordered_letters == \"Impossible\"\n alphabet = \"Impossible\"\n break\n end\n alphabet = ordered_letters + alphabet\n end \nend\nif alphabet == \"Impossible\"\n puts alphabet \nelse\n puts permutation(alphabet).join\nend\n"}, {"source_code": "class List\n attr_accessor :element, :next_elem\n\n def initialize(element, next_elem)\n @element = element\n @next_elem = next_elem\n end\nend\n\nclass Graph\n attr_accessor :vertices\n\n def initialize(vertices)\n @vertices = vertices\n end\n\n def add_edge(letter1, letter2)\n if vertices[letter1].nil? then\n vertices[letter1] = List.new(letter2, nil)\n else\n vertices[letter1] = List.new(letter2, vertices[letter1])\n end\n end\n\n # def add_vertex(letter)\n # vertices[letter] = \"\" if vertices[letter].nil?\n # end\n\n def print_edges \n vertices.each do |letter, vertex| \n # next if vertex == \"\"\n while !vertex.nil? do \n puts \"edge #{letter} - #{vertex.element}\"\n vertex = vertex.next_elem \n end \n end \n end \n\n # def print_verteces\n # puts vertices.keys.join(\" \")\n # end\nend \n\ndef dfs(graph, letter, color)\n color[letter] = 'grey'\n next_vert = graph.vertices[letter]\n # puts \"Processing letter: #{letter}\"\n # p next_vert\n if next_vert.nil? \n # puts letter\n color[letter] = 'black'\n return [letter]\n end \n alphabet = []\n while !next_vert.nil? do \n if color[next_vert.element] == 'grey'\n # puts \"#{letter} color is grey and it means we have a cycle\"\n return \"Impossible\"\n elsif color[next_vert.element] == 'white'\n # puts \"#{next_vert.element}'s color is white\"\n new_letter = dfs(graph, next_vert.element, color)\n # puts new_letter\n if new_letter == \"Impossible\"\n return \"Impossible\"\n else\n alphabet = new_letter + alphabet\n end\n end\n next_vert = next_vert.next_elem\n end\n color[letter] = 'black'\n return [letter] + alphabet\nend\n\ndef permutation(sorted_letters)\n sorted_letters | (\"a\"..\"z\").to_a\nend\n\nnames = []\nn = gets.strip.to_i\nn.times{|i| names.push(gets.strip)}\nletters = Graph.new({})\n\n(n - 1).times do |i|\n length = [names[i].length, names[i+1].length].min\n length.times do |j|\n if names[i][j] != names[i+1][j]\n letters.add_edge(names[i][j], names[i+1][j])\n break\n end \n end\nend\n\n# letters.print_edges\n\ncolor = {}\n(\"a\"..\"z\").each do |letter|\n color[letter] = 'white'\nend\n# puts \"All the letters in the graph: #{letters.vertices.keys}\"\nalphabet = []\nletters.vertices.each_key do |letter| \n if color[letter] == 'white'\n ordered_letters = dfs(letters, letter, color)\n if ordered_letters == \"Impossible\"\n alphabet = \"Impossible\"\n break\n end\n alphabet += ordered_letters\n end \nend\nif alphabet == \"Impossible\"\n puts alphabet \nelse\n puts permutation(alphabet).join\nend\n"}, {"source_code": "class List\n attr_accessor :element, :next_elem\n\n def initialize(element, next_elem)\n @element = element\n @next_elem = next_elem\n end\nend\n\nclass Graph\n attr_accessor :vertices\n\n def initialize(vertices)\n @vertices = vertices\n end\n\n def add_edge(letter1, letter2)\n if vertices[letter1].nil? then\n vertices[letter1] = List.new(letter2, nil)\n else\n vertices[letter1] = List.new(letter2, vertices[letter1])\n end\n end\n\n # def add_vertex(letter)\n # vertices[letter] = \"\" if vertices[letter].nil?\n # end\n\n def print_edges \n vertices.each do |letter, vertex| \n # next if vertex == \"\"\n while !vertex.nil? do \n puts \"edge #{letter} - #{vertex.element}\"\n vertex = vertex.next_elem \n end \n end \n end \n\n # def print_verteces\n # puts vertices.keys.join(\" \")\n # end\nend \n\ndef dfs(graph, letter, color)\n color[letter] = 'grey'\n next_vert = graph.vertices[letter]\n # puts \"Processing letter: #{letter}\"\n # p next_vert\n if next_vert.nil? \n # puts letter\n return [letter]\n end \n alphabet = []\n while !next_vert.nil? do \n if color[next_vert.element] == 'grey'\n return \"Impossible\"\n elsif color[next_vert.element] == 'white'\n # puts \"#{next_vert.element}'s color is white\"\n new_letter = dfs(graph, next_vert.element, color)\n # puts new_letter\n if new_letter == \"Impossible\"\n return \"Impossible\"\n else\n alphabet = new_letter + alphabet\n end\n end\n next_vert = next_vert.next_elem\n end\n color[letter] = 'black'\n return [letter] + alphabet\nend\n\ndef permutation(sorted_letters)\n sorted_letters | (\"a\"..\"z\").to_a\nend\n\nnames = []\nn = gets.strip.to_i\nn.times{|i| names.push(gets.strip)}\nletters = Graph.new({})\n\n(n - 1).times do |i|\n length = [names[i].length, names[i+1].length].min\n length.times do |j|\n if names[i][j] != names[i+1][j]\n letters.add_edge(names[i][j], names[i+1][j])\n break\n end \n end\nend\n\n# letters.print_edges\n\ncolor = {}\n(\"a\"..\"z\").each do |letter|\n color[letter] = 'white'\nend\n# puts \"All the letters in the graph: #{letters.vertices.keys}\"\nalphabet = []\nletters.vertices.each_key do |letter| \n if color[letter] == 'white'\n ordered_letters = dfs(letters, letter, color)\n if ordered_letters == \"Impossible\"\n alphabet = \"Impossible\"\n break\n end\n alphabet += ordered_letters\n end \nend\nif alphabet == \"Impossible\"\n puts alphabet \nelse\n puts permutation(alphabet).join\nend\n"}, {"source_code": "class List\n attr_accessor :element, :next_elem\n\n def initialize(element, next_elem)\n @element = element\n @next_elem = next_elem\n end\nend\n\nclass Graph\n attr_accessor :vertices\n\n def initialize(vertices)\n @vertices = vertices\n end\n\n def add_edge(letter1, letter2)\n if vertices[letter1].nil? then\n vertices[letter1] = List.new(letter2, nil)\n else\n vertices[letter1] = List.new(letter2, vertices[letter1])\n end\n end\n\n # def add_vertex(letter)\n # vertices[letter] = \"\" if vertices[letter].nil?\n # end\n\n def print_edges \n vertices.each do |letter, vertex| \n # next if vertex == \"\"\n while !vertex.nil? do \n puts \"edge #{letter} - #{vertex.element}\"\n vertex = vertex.next_elem \n end \n end \n end \n\n # def print_verteces\n # puts vertices.keys.join(\" \")\n # end\nend \n\ndef dfs(graph, letter, marked)\n marked[letter] = true\n next_vert = graph.vertices[letter]\n if next_vert.nil? \n return [letter]\n end \n alphabet = []\n while !next_vert.nil? do \n if marked[next_vert.element]\n return \"Impossible\"\n else\n new_letter = dfs(graph, next_vert.element, marked)\n if new_letter == \"Impossible\"\n return \"Impossible\"\n else\n alphabet = new_letter + alphabet\n end\n end\n next_vert = next_vert.next_elem\n end\n return [letter] + alphabet\nend\n\ndef permutation(sorted_letters)\n sorted_letters | (\"a\"..\"z\").to_a\nend\n\nnames = []\nn = gets.strip.to_i\nn.times{|i| names.push(gets.strip)}\nletters = Graph.new({})\n\n(n - 1).times do |i|\n length = [names[i].length, names[i+1].length].min\n length.times do |j|\n if names[i][j] != names[i+1][j]\n letters.add_edge(names[i][j], names[i+1][j])\n break\n end \n end\nend\n\nmarked = {}\nletters.vertices.each_key do |letter|\n marked[letter] = false\nend\nalphabet = []\nletters.vertices.each_key do |letter| \n if !marked[letter]\n ordered_letters = dfs(letters, letter, marked)\n if ordered_letters == \"Impossible\"\n alphabet = \"Impossible\"\n break\n end\n alphabet += ordered_letters\n end \nend\nif alphabet == \"Impossible\"\n puts alphabet \nelse\n puts permutation(alphabet).join\nend\n"}, {"source_code": "Point = Struct.new(:char, :color, :children)\nchars = Hash[('a'..'z').map{|c| [c, Point.new(c, :white, [])]}]\nn = gets.to_i\nwords = []\nn.times do\n words << gets.chomp.chars\nend\nwords.each_cons(2) do |prv,nxt|\n p, n = prv.zip(nxt).drop_while{|p,n| p==n}.first\n if n == nil\n if n == nil\n next\n else\n puts \"Impossible\"\n exit\n end\n end\n (chars[p].children << chars[n]).uniq!{|v| v.char}\nend\ndef yoba(v,order)\n case v.color\n when :gray\n return false\n when :black\n return true\n end\n v.color = :gray\n v.children.each do |v|\n return false unless yoba(v, order)\n end\n v.color = :black\n order << v.char\n true\nend\norder = []\nchars.values.reverse.each do |v|\n unless yoba(v,order)\n puts \"Impossible\"\n exit\n end\nend\nputs order.reverse.join"}], "src_uid": "12218097cf5c826d83ab11e5b049999f"} {"nl": {"description": "You are given a binary string of length $$$n$$$ (i. e. a string consisting of $$$n$$$ characters '0' and '1').In one move you can swap two adjacent characters of the string. What is the lexicographically minimum possible string you can obtain from the given one if you can perform no more than $$$k$$$ moves? It is possible that you do not perform any moves at all.Note that you can swap the same pair of adjacent characters with indices $$$i$$$ and $$$i+1$$$ arbitrary (possibly, zero) number of times. Each such swap is considered a separate move.You have to answer $$$q$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$q$$$ ($$$1 \\le q \\le 10^4$$$) — the number of test cases. The first line of the test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le n \\le 10^6, 1 \\le k \\le n^2$$$) — the length of the string and the number of moves you can perform. The second line of the test case contains one string consisting of $$$n$$$ characters '0' and '1'. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$ ($$$\\sum n \\le 10^6$$$).", "output_spec": "For each test case, print the answer on it: the lexicographically minimum possible string of length $$$n$$$ you can obtain from the given one if you can perform no more than $$$k$$$ moves.", "sample_inputs": ["3\n8 5\n11011010\n7 9\n1111100\n7 11\n1111100"], "sample_outputs": ["01011110\n0101111\n0011111"], "notes": "NoteIn the first example, you can change the string as follows: $$$1\\underline{10}11010 \\rightarrow \\underline{10}111010 \\rightarrow 0111\\underline{10}10 \\rightarrow 011\\underline{10}110 \\rightarrow 01\\underline{10}1110 \\rightarrow 01011110$$$. In the third example, there are enough operations to make the string sorted."}, "positive_code": [{"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\nq = inp[0]\nindata = []\n(q*2).times do\n indata << gets.chomp\nend\n(q).times do |di|\n n, k = indata[di*2].split.map(&:to_i)\n t = indata[di*2+1]\n head = 0\n nk = k\n n.times do |i|\n next unless t[i] == \"0\"\n sa = (i-head)\n if(sa < nk)\n t[head], t[i] = t[i], t[head]\n head += 1\n nk -= sa\n else\n t[i - nk], t[i] = t[i], t[i - nk]\n nk == 0\n break\n end\n end\n puts t\nend"}], "negative_code": [], "src_uid": "d4b6bea78b80b0a94646cbaf048b473f"} {"nl": {"description": "Ashish and Vivek play a game on a matrix consisting of $$$n$$$ rows and $$$m$$$ columns, where they take turns claiming cells. Unclaimed cells are represented by $$$0$$$, while claimed cells are represented by $$$1$$$. The initial state of the matrix is given. There can be some claimed cells in the initial state.In each turn, a player must claim a cell. A cell may be claimed if it is unclaimed and does not share a row or column with any other already claimed cells. When a player is unable to make a move, he loses and the game ends.If Ashish and Vivek take turns to move and Ashish goes first, determine the winner of the game if both of them are playing optimally.Optimal play between two players means that both players choose the best possible strategy to achieve the best possible outcome for themselves.", "input_spec": "The first line consists of a single integer $$$t$$$ $$$(1 \\le t \\le 50)$$$ — the number of test cases. The description of the test cases follows. The first line of each test case consists of two space-separated integers $$$n$$$, $$$m$$$ $$$(1 \\le n, m \\le 50)$$$ — the number of rows and columns in the matrix. The following $$$n$$$ lines consist of $$$m$$$ integers each, the $$$j$$$-th integer on the $$$i$$$-th line denoting $$$a_{i,j}$$$ $$$(a_{i,j} \\in \\{0, 1\\})$$$.", "output_spec": "For each test case if Ashish wins the game print \"Ashish\" otherwise print \"Vivek\" (without quotes).", "sample_inputs": ["4\n2 2\n0 0\n0 0\n2 2\n0 0\n0 1\n2 3\n1 0 1\n1 1 0\n3 3\n1 0 0\n0 0 0\n1 0 0"], "sample_outputs": ["Vivek\nAshish\nVivek\nAshish"], "notes": "NoteFor the first case: One possible scenario could be: Ashish claims cell $$$(1, 1)$$$, Vivek then claims cell $$$(2, 2)$$$. Ashish can neither claim cell $$$(1, 2)$$$, nor cell $$$(2, 1)$$$ as cells $$$(1, 1)$$$ and $$$(2, 2)$$$ are already claimed. Thus Ashish loses. It can be shown that no matter what Ashish plays in this case, Vivek will win. For the second case: Ashish claims cell $$$(1, 1)$$$, the only cell that can be claimed in the first move. After that Vivek has no moves left.For the third case: Ashish cannot make a move, so Vivek wins.For the fourth case: If Ashish claims cell $$$(2, 3)$$$, Vivek will have no moves left."}, "positive_code": [{"source_code": "tests = gets.to_i\ntests.times do\n\trows, columns = gets.split(' ')\n\trows = rows.to_i\n\tcolumns = columns.to_i\n\ttable = Array.new(rows){Array.new(columns)}\n\tcnt_rows = Array.new(rows, 0)\n\tcnt_columns = Array.new(columns, 0)\n\trows.times do\n\t\t|row|\n\t\tline = gets.split(' ')\n\t\tcolumns.times do\n\t\t\t|column|\n\t\t\ttable[row][column] = line[column].to_i\n\t\t\tcnt_rows[row] += table[row][column]\n\t\t\tcnt_columns[column] += table[row][column]\n\t\tend\n\tend\n\tcnt_missing_rows = 0\n\tcnt_missing_columns = 0\n\trows.times do\n\t\t|idx|\n\t\tif cnt_rows[idx] == 0\n\t\t\tcnt_missing_rows += 1\n\t\tend\n\tend\n\n\tcolumns.times do\n\t\t|idx|\n\t\tif cnt_columns[idx] == 0\n\t\t\tcnt_missing_columns += 1\n\t\tend\n\tend\n\tres = [cnt_missing_rows, cnt_missing_columns].min\n\tif res%2 == 0\n\t\tputs \"Vivek\"\n\telse\n\t\tputs \"Ashish\"\n\tend\nend\n"}, {"source_code": "tasks_numbers = gets.chomp\nallowed_lines = []\n(1..tasks_numbers.to_i).each do\n\trows_cols = gets.chomp\n\trows, cols = rows_cols.split(\" \").map{|e| e.to_i}\n\tall_pairs = rows + cols\n\tallowed_cols = (1..cols).to_a\n\tallowed_rows_qty = rows\n\t(1..rows).each do\n\t\tindex = 0\n\t\tisset_1 = false\n\t\tvalues = gets.chomp\n\t\tvalues.split(\" \").each do |value|\n\t\t\tindex += 1\n\t\t\tif value == '1'\n\t\t\t\tallowed_cols.delete(index)\n\t\t\t\tisset_1 = true\n\t\t\tend\n\t\tend\n\t\tallowed_rows_qty -= 1 if isset_1\n\tend\n\tallowed_lines.push [allowed_rows_qty, allowed_cols.length].min\nend\n\nallowed_lines.each do |allowed_line|\n\tif allowed_line == 0\n\t\tputs 'Vivek'\n\telse\n\t\tif allowed_line % 2 > 0\n\t\t\tputs 'Ashish'\n\t\telse\n\t\t\tputs 'Vivek'\n\t\tend\n\tend\nend\n"}], "negative_code": [{"source_code": "tasks_numbers = gets.chomp\nallowed_lines = []\n(1..tasks_numbers.to_i).each do\n\trows_cols = gets.chomp\n\trows, cols = rows_cols.split(\" \").map{|e| e.to_i}\n\tall_pairs = rows + cols\n\tallowed_cols = (1..cols).to_a\n\tallowed_rows_qty = rows\n\t(1..rows).each do\n\t\tindex = 0\n\t\tisset_1 = false\n\t\tvalues = gets.chomp\n\t\tvalues.split(\" \").each do |value|\n\t\t\tindex += 1\n\t\t\tif value == '1'\n\t\t\t\tallowed_cols.delete(index)\n\t\t\t\tisset_1 = true\n\t\t\tend\n\t\tend\n\t\tallowed_rows_qty -= 1 if isset_1\n\tend\n\tallowed_lines.push(allowed_rows_qty + allowed_cols.length)\nend\n\nallowed_lines.each do |allowed_line|\n\tif allowed_line < 2\n\t\tputs 'Vivek'\n\telse\n\t\tallowed_line -= 1 if allowed_line % 2 > 0\n\t\tallowed_line = allowed_line / 2\n\t\tif allowed_line % 2 > 0\n\t\t\tputs 'Ashish'\n\t\telse\n\t\t\tputs 'Vivek'\n\t\tend\n\tend\nend\n"}], "src_uid": "3ccc98190189b0c8e58a96dd5ad1245d"} {"nl": {"description": "Vasya owns a cornfield which can be defined with two integers $$$n$$$ and $$$d$$$. The cornfield can be represented as rectangle with vertices having Cartesian coordinates $$$(0, d), (d, 0), (n, n - d)$$$ and $$$(n - d, n)$$$. An example of a cornfield with $$$n = 7$$$ and $$$d = 2$$$. Vasya also knows that there are $$$m$$$ grasshoppers near the field (maybe even inside it). The $$$i$$$-th grasshopper is at the point $$$(x_i, y_i)$$$. Vasya does not like when grasshoppers eat his corn, so for each grasshopper he wants to know whether its position is inside the cornfield (including the border) or outside.Help Vasya! For each grasshopper determine if it is inside the field (including the border).", "input_spec": "The first line contains two integers $$$n$$$ and $$$d$$$ ($$$1 \\le d < n \\le 100$$$). The second line contains a single integer $$$m$$$ ($$$1 \\le m \\le 100$$$) — the number of grasshoppers. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \\le x_i, y_i \\le n$$$) — position of the $$$i$$$-th grasshopper.", "output_spec": "Print $$$m$$$ lines. The $$$i$$$-th line should contain \"YES\" if the position of the $$$i$$$-th grasshopper lies inside or on the border of the cornfield. Otherwise the $$$i$$$-th line should contain \"NO\". You can print each letter in any case (upper or lower).", "sample_inputs": ["7 2\n4\n2 4\n4 1\n6 3\n4 5", "8 7\n4\n4 4\n2 8\n8 1\n6 1"], "sample_outputs": ["YES\nNO\nNO\nYES", "YES\nNO\nYES\nYES"], "notes": "NoteThe cornfield from the first example is pictured above. Grasshoppers with indices $$$1$$$ (coordinates $$$(2, 4)$$$) and $$$4$$$ (coordinates $$$(4, 5)$$$) are inside the cornfield.The cornfield from the second example is pictured below. Grasshoppers with indices $$$1$$$ (coordinates $$$(4, 4)$$$), $$$3$$$ (coordinates $$$(8, 1)$$$) and $$$4$$$ (coordinates $$$(6, 1)$$$) are inside the cornfield. "}, "positive_code": [{"source_code": "n, d=gets.strip.split.map(&:to_i)\n\nhsh = {}\n\ny1 = d\n0.upto(n-d).each do |x1|\n y = y1\n x = x1\n (d+1).times do\n hsh[\"#{x},#{y}\"]=true\n y -= 1\n x += 1\n end\n y1 += 1\nend\n\ny1 = d\n1.upto(n-d).each do |x1|\n y = y1\n x = x1\n d.times do\n hsh[\"#{x},#{y}\"]=true\n y -= 1\n x += 1\n end\n y1 += 1\nend\n\nm = gets.to_i\nm.times do\n x,y=gets.strip.split.map(&:to_i)\n puts (hsh[\"#{x},#{y}\"] ? \"YES\" : \"NO\")\nend"}, {"source_code": "def d(x,y,x1,y1,x2,y2,p)\n a = (y2-y1)/(x2-x1)\n b = (x2*y1-x1*y2)/(x2-x1)\n return ((p == 0) == (a*x+b < y)) || a*x+b == y\nend\n\nn,d = gets.split.map(&:to_i)\ngets.to_i.times do\n x,y = gets.split.map(&:to_i)\n puts d(x,y,0,d,d,0,0) && d(x,y,d,0,n,n-d,0) && d(x,y,n,n-d,n-d,n,1) && d(x,y,n-d,n,0,d,1) ? \"YES\" : \"NO\"\nend\n"}, {"source_code": "n, d = gets.split.map &:to_i\nnQ = gets.to_i\nnQ.times do\n x, y = gets.split.map &:to_i\n f = true \n f &= x+y >= d\n f &= (n-x)+(n-y)>=d \n f &= x+(n-y)>=n-d\n f &= (n-x)+y>=n-d\n puts f ? \"YES\" : \"NO\"\nend\n\n"}], "negative_code": [], "src_uid": "9c84eb518c273942650c7262e5d8b45f"} {"nl": {"description": "The legend of the foundation of Vectorland talks of two integers $$$x$$$ and $$$y$$$. Centuries ago, the array king placed two markers at points $$$|x|$$$ and $$$|y|$$$ on the number line and conquered all the land in between (including the endpoints), which he declared to be Arrayland. Many years later, the vector king placed markers at points $$$|x - y|$$$ and $$$|x + y|$$$ and conquered all the land in between (including the endpoints), which he declared to be Vectorland. He did so in such a way that the land of Arrayland was completely inside (including the endpoints) the land of Vectorland.Here $$$|z|$$$ denotes the absolute value of $$$z$$$.Now, Jose is stuck on a question of his history exam: \"What are the values of $$$x$$$ and $$$y$$$?\" Jose doesn't know the answer, but he believes he has narrowed the possible answers down to $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$. Now, he wants to know the number of unordered pairs formed by two different elements from these $$$n$$$ integers such that the legend could be true if $$$x$$$ and $$$y$$$ were equal to these two values. Note that it is possible that Jose is wrong, and that no pairs could possibly make the legend true.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$2 \\le n \\le 2 \\cdot 10^5$$$)  — the number of choices. The second line contains $$$n$$$ pairwise distinct integers $$$a_1, a_2, \\dots, a_n$$$ ($$$-10^9 \\le a_i \\le 10^9$$$) — the choices Jose is considering.", "output_spec": "Print a single integer number — the number of unordered pairs $$$\\{x, y\\}$$$ formed by different numbers from Jose's choices that could make the legend true.", "sample_inputs": ["3\n2 5 -3", "2\n3 6"], "sample_outputs": ["2", "1"], "notes": "NoteConsider the first sample. For the pair $$$\\{2, 5\\}$$$, the situation looks as follows, with the Arrayland markers at $$$|2| = 2$$$ and $$$|5| = 5$$$, while the Vectorland markers are located at $$$|2 - 5| = 3$$$ and $$$|2 + 5| = 7$$$: The legend is not true in this case, because the interval $$$[2, 3]$$$ is not conquered by Vectorland. For the pair $$$\\{5, -3\\}$$$ the situation looks as follows, with Arrayland consisting of the interval $$$[3, 5]$$$ and Vectorland consisting of the interval $$$[2, 8]$$$: As Vectorland completely contains Arrayland, the legend is true. It can also be shown that the legend is true for the pair $$$\\{2, -3\\}$$$, for a total of two pairs.In the second sample, the only pair is $$$\\{3, 6\\}$$$, and the situation looks as follows: Note that even though Arrayland and Vectorland share $$$3$$$ as endpoint, we still consider Arrayland to be completely inside of Vectorland."}, "positive_code": [{"source_code": "def bsearch(arr,t)\n ok,ng = 0,arr.length\n while (ng - ok).abs > 1\n mid = (ok+ng)/2\n if arr[mid] <= t\n ok = mid\n else\n ng = mid\n end\n end\n return ok\nend\n\nn = gets.to_i\na = gets.split.map{|t|t.to_i.abs}.sort\nans = 0\nn.times do |i|\n x = bsearch(a,a[i]*2)\n ans += x-i if x > i\n #p [i,a[i],x-i]\nend\np ans\n"}], "negative_code": [], "src_uid": "642e2d1b427c025578424c81192be756"} {"nl": {"description": "Fox Ciel is playing a card game with her friend Jiro.Jiro has n cards, each one has two attributes: position (Attack or Defense) and strength. Fox Ciel has m cards, each one has these two attributes too. It's known that position of all Ciel's cards is Attack.Now is Ciel's battle phase, Ciel can do the following operation many times: Choose one of her cards X. This card mustn't be chosen before. If Jiro has no alive cards at that moment, he gets the damage equal to (X's strength). Otherwise, Ciel needs to choose one Jiro's alive card Y, then: If Y's position is Attack, then (X's strength)  ≥  (Y's strength) must hold. After this attack, card Y dies, and Jiro gets the damage equal to (X's strength) - (Y's strength). If Y's position is Defense, then (X's strength)  >  (Y's strength) must hold. After this attack, card Y dies, but Jiro gets no damage. Ciel can end her battle phase at any moment (so, she can use not all her cards). Help the Fox to calculate the maximal sum of damage Jiro can get.", "input_spec": "The first line contains two integers n and m (1 ≤ n, m ≤ 100) — the number of cards Jiro and Ciel have. Each of the next n lines contains a string position and an integer strength (0 ≤ strength ≤ 8000) — the position and strength of Jiro's current card. Position is the string \"ATK\" for attack, and the string \"DEF\" for defense. Each of the next m lines contains an integer strength (0 ≤ strength ≤ 8000) — the strength of Ciel's current card.", "output_spec": "Output an integer: the maximal damage Jiro can get.", "sample_inputs": ["2 3\nATK 2000\nDEF 1700\n2500\n2500\n2500", "3 4\nATK 10\nATK 100\nATK 1000\n1\n11\n101\n1001", "2 4\nDEF 0\nATK 0\n0\n0\n1\n1"], "sample_outputs": ["3000", "992", "1"], "notes": "NoteIn the first test case, Ciel has 3 cards with same strength. The best strategy is as follows. First she uses one of these 3 cards to attack \"ATK 2000\" card first, this attack destroys that card and Jiro gets 2500 - 2000 = 500 damage. Then she uses the second card to destroy the \"DEF 1700\" card. Jiro doesn't get damage that time. Now Jiro has no cards so she can use the third card to attack and Jiro gets 2500 damage. So the answer is 500 + 2500 = 3000.In the second test case, she should use the \"1001\" card to attack the \"ATK 100\" card, then use the \"101\" card to attack the \"ATK 10\" card. Now Ciel still has cards but she can choose to end her battle phase. The total damage equals (1001 - 100) + (101 - 10) = 992.In the third test case note that she can destroy the \"ATK 0\" card by a card with strength equal to 0, but she can't destroy a \"DEF 0\" card with that card."}, "positive_code": [{"source_code": "#!/usr/bin/env ruby\n$damage = 0\ndef all_destroy?(jjcard, ccard)\n ori_size = ccard.size\n jjcard.each do |jc|\n for i in 0...ccard.size\n if jc[0] == 'ATK' && ccard[i] >= jc[1]\n $damage += ccard[i] - jc[1]\n ccard.delete_at(i)\n break\n elsif jc[0] == 'DEF' && ccard[i] > jc[1]\n ccard.delete_at(i)\n break\n end\n end\n end\n return ori_size - ccard.size == jjcard.size ? ccard : nil\nend\nn, m = gets.chomp!.split(' ').map(&:to_i)\njcard, ccard = [], []\nn.times do\n p, s = gets.chomp!.split(' ')\n jcard << [p, s.to_i]\nend\njcard = jcard.sort_by {|v| v[0]}.reverse!\nm.times do\n ccard << gets.chomp!.to_i\nend\nccard.sort!\ntc = ccard.dup\nret = 0 \nif ccard = all_destroy?(jcard, ccard)\n ccard.each do |cc|\n $damage += cc\n end\nend\nret = $damage\n$damage = 0\nccard = tc\njcard.sort_by! {|v| v[1]}\njcard.each do |jc|\n next if jc[0] == 'DEF'\n if ccard != nil && ccard.size > 0 && ccard[-1] >= jc[1]\n $damage += ccard[-1] -jc[1];\n ccard.pop\n else\n break\n end\nend\nputs [ret, $damage].max"}], "negative_code": [], "src_uid": "06420ea88312103231a7bbac8a9a62d1"} {"nl": {"description": "Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him. ", "input_spec": "The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.", "output_spec": "Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.", "sample_inputs": ["1\n2", "5\n1 2 1 2 1", "8\n1 2 1 1 1 3 3 4"], "sample_outputs": ["1", "3", "6"], "notes": null}, "positive_code": [{"source_code": "n=gets.to_i\na=[nil,]\nm=1\na+=gets.split.map{|v| v.to_i}\n\nfor i in 1..n do\n c=1\n (i+1..n).each do |j|\n\tif a[j]<=a[j-1] then c+=1\n\telse break\n\tend\n end\n \n (1..i-1).reverse_each do |j|\n if a[j]<=a[j+1] then c+=1\n\telse break\n\tend\n end\n \n if c>m then num=i; m=c; end\nend\n\nputs m"}, {"source_code": "n = gets.to_i\narr = gets.split(\" \").map(&:to_i)\ndp_left = [0]\narr.each_cons(2) do |a,b|\n\tdp_left << 0\n\tif b >= a\n\t\tdp_left[-1] += dp_left[-2] + 1\n\tend\nend\n\ndp_right = [0]\n\n((n-2).downto(0)).each do |i|\n\tdp_right << 0\n\tif arr[i] >= arr[i+1]\n\t\tdp_right[-1] += dp_right[-2] + 1\n\tend\nend\ndp_right.reverse!\nans_list = dp_left.zip(dp_right).map { |x, y| x + y + 1 }\nputs \"#{ans_list.max}\"\n"}, {"source_code": "n=gets.chomp.to_i\na=gets.chomp.split(/ /).map{|x|x.to_i}\ndef toleft(a,k)\n x=a[k]\n i=0\n while a[k-i-1]<=x\n x=a[k-1-i]\n i+=1\n end\n i\nend\ndef toright(a,k)\n x=a[k]\n i=0\n while a[k+i+1]<=x\n x=a[k+1+i]\n i+=1\n end\n i\nend\na=a.unshift(a.max+1).push(a.max+1)\np (1..n).map{|x|1+toleft(a,x)+toright(a,x)}.max\n"}, {"source_code": "gets\nareas = gets.chomp.split.map(&:to_i)\n\nmax = 0\nareas.each_with_index do |a, i|\n cnt = 1\n\n (i - 1).downto(0).each do |l|\n break if l < 0\n if areas[l] <= areas[l + 1]\n cnt += 1\n else\n break\n end\n end\n\n (i.succ..areas.size - 1).each do |r|\n if areas[r] <= areas[r - 1]\n cnt += 1\n else\n break\n end\n end\n\n max = [max, cnt].max\nend\n\nputs max\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\n\ns1 = [0]*n\n(1..n-1).each do |i|\n if a[i] >= a[i-1]\n s1[i] = s1[i-1]+1\n end\nend\n\ns2 = [0]*n\n(n-2).downto(0) do |i|\n if a[i] >= a[i+1]\n s2[i] = s2[i+1]+1\n end\nend\n\np s1.zip(s2).map{|x,y| x+y+1}.max\n"}, {"source_code": "n,*a=gets(p).split.map(&:to_i)<<$$;p (0...n).map{|x|y=x;1until a[y] [\"a\",\"b\",\"c\"]\n#String # to_ia\n# \"1 2 3\".to_ia #=> [1,2,3]\n#\n#Array # rsort,rsort!\n# [1,3,2,4].rsort #=> [4,3,2,1]\n#\n#Integer # divisor\n# 24.divisor #=> [1,2,3,4,6,8,12,24]\n#\ninclude Math\nrequire \"prime\"\nrequire \"bigdecimal\"\nrequire \"bigdecimal/math\"\nrequire \"matrix\"\nrequire \"pp\"\nrequire \"benchmark\"\n\nInfinity = 1/0.0\nclass Matrix;def inspect;empty? ? \"Matrix.empty(#{row_size}, #{column_size})\":\"Matrix[\\n#{@rows.map(&:inspect).join(\"\\n\")}\\n]\";end;end\ndef pbin(obj);case obj;when Integer;puts obj.to_s(2);end;end\nclass String;def to_a;split(\"\");end;end\nclass String;def to_ia;split.map(&:to_i);end;end\ndef getia;gets.to_ia;end\ndef geti;gets.to_i;end\nclass Object;def dcopy;Marshal.load(Marshal.dump(self));end;end\nclass Array;def rsort;sort.reverse;end;def rsort!;sort!.reverse!;end;end\n\nn=geti\nls=getia\nans=0\nfor i in 0...n\n a=0\n for j in 1..i\n break if ls[i-j] > ls[i-j+1]\n a+=1\n end\n for j in 1...(n-i)\n break if ls[i+j] > ls[i+j-1]\n a+=1\n end\n ans=[a,ans].max\nend\np ans+1\n\n"}, {"source_code": "#!/usr/bin/ruby\n\nn = gets.to_i\narr = gets.split.map(&:to_i)\nans = 0\narr.each_with_index { |v, i|\n\tleft = right = i\n\tleft -= 1 while left > 0 && arr[left-1] <= arr[left]\n\tright += 1 while right < n-1 && arr[right+1] <= arr[right]\n\tnum = right-left+1\n\tans = num if num > ans\n}\nputs ans\n"}, {"source_code": "n,*a=gets(p).split.map(&:to_i)<<$$;p (0...n).map{|x|y=x;1until a[y]= a[i-1] ? l[i-1] + 1 : 0);\n\ti+=1\nend\ni=n-2\nwhile i>=0\n\tr[i] = (a[i] >= a[i+1] ? r[i+1]+1 : 0)\n\ti-=1\nend\nn.times {|i| ans=l[i]+r[i]+1 if l[i]+r[i]+1>ans}\nputs ans"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nmax = 0\n(0...n).each do |i|\n left, right = i, i\n while left>0 and a[left-1] <= a[left]\n left-=1\n end\n while right max\nend\nputs max"}, {"source_code": "n,*a=gets(p).split.map(&:to_i)<<$$;p (0...n).map{|x|y=x;1until a[y]= map[pos - 1])\n count += 1\n pos -= 1\n cur_height = map[pos]\n end\n pos = center\n cur_height = map[pos]\n while (cur_height >= map[pos + 1])\n count += 1\n pos += 1\n cur_height = map[pos]\n end\n\n ret = count if ret < count\n}\nputs ret\n"}, {"source_code": "n,*a=gets(p).split.map(&:to_i)<<$$;p (0...n).map{|x|y=x;1until a[y]= 0 && vl && vl <= old_vl\n cnt += 1 if vr && vr <= old_vr\n\n #p [l, r, cnt, max]\n\n l -= 1\n r += 1\n\n old_vl, old_vr = (l < 0 ? 10000 : vl), vr\n break if old_cnt == cnt\n end\n\n max = [max, cnt].max\n #puts\nend\n\nputs max\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\n\ns1 = [0]*n\n(1..n-1).each do |i|\n if a[i] >= a[i-1]\n s1[i] = s1[i-1]+1\n end\nend\n\ns2 = [0]*n\n(n-2).downto(1) do |i|\n if a[i] >= a[i+1]\n s2[i] = s2[i+1]+1\n end\nend\n\np s1.zip(s2).map{|x,y| x+y+1}.max\n"}], "src_uid": "5d11fa8528f1dc873d50b3417bef8c79"} {"nl": {"description": "There are $$$n$$$ Christmas trees on an infinite number line. The $$$i$$$-th tree grows at the position $$$x_i$$$. All $$$x_i$$$ are guaranteed to be distinct.Each integer point can be either occupied by the Christmas tree, by the human or not occupied at all. Non-integer points cannot be occupied by anything.There are $$$m$$$ people who want to celebrate Christmas. Let $$$y_1, y_2, \\dots, y_m$$$ be the positions of people (note that all values $$$x_1, x_2, \\dots, x_n, y_1, y_2, \\dots, y_m$$$ should be distinct and all $$$y_j$$$ should be integer). You want to find such an arrangement of people that the value $$$\\sum\\limits_{j=1}^{m}\\min\\limits_{i=1}^{n}|x_i - y_j|$$$ is the minimum possible (in other words, the sum of distances to the nearest Christmas tree for all people should be minimized).In other words, let $$$d_j$$$ be the distance from the $$$j$$$-th human to the nearest Christmas tree ($$$d_j = \\min\\limits_{i=1}^{n} |y_j - x_i|$$$). Then you need to choose such positions $$$y_1, y_2, \\dots, y_m$$$ that $$$\\sum\\limits_{j=1}^{m} d_j$$$ is the minimum possible.", "input_spec": "The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n, m \\le 2 \\cdot 10^5$$$) — the number of Christmas trees and the number of people. The second line of the input contains $$$n$$$ integers $$$x_1, x_2, \\dots, x_n$$$ ($$$-10^9 \\le x_i \\le 10^9$$$), where $$$x_i$$$ is the position of the $$$i$$$-th Christmas tree. It is guaranteed that all $$$x_i$$$ are distinct.", "output_spec": "In the first line print one integer $$$res$$$ — the minimum possible value of $$$\\sum\\limits_{j=1}^{m}\\min\\limits_{i=1}^{n}|x_i - y_j|$$$ (in other words, the sum of distances to the nearest Christmas tree for all people). In the second line print $$$m$$$ integers $$$y_1, y_2, \\dots, y_m$$$ ($$$-2 \\cdot 10^9 \\le y_j \\le 2 \\cdot 10^9$$$), where $$$y_j$$$ is the position of the $$$j$$$-th human. All $$$y_j$$$ should be distinct and all values $$$x_1, x_2, \\dots, x_n, y_1, y_2, \\dots, y_m$$$ should be distinct. If there are multiple answers, print any of them.", "sample_inputs": ["2 6\n1 5", "3 5\n0 3 1"], "sample_outputs": ["8\n-1 2 6 4 0 3", "7\n5 -2 4 -1 2"], "notes": null}, "positive_code": [{"source_code": "n, $m = readline.split.map(&:to_i)\nx = readline.split.map(&:to_i)\n\nx.sort!\n\nl = r = x[0]\ns = []\nfor xi in x[1..-1] do\n if xi > r + 1\n s << [l,r]\n l = xi\n end\n r = xi\nend\ns << [l,r]\n\n$d = 1\n$res = 0\n$y = []\ndef man(yi)\n $y << yi\n $m -= 1\n $res += $d\n if $m <= 0\n print($res, \"\\n\", $y.join(' '), \"\\n\")\n exit\n end\nend\nwhile $m > 0 do\n l,r = s[0]\n s1 = []\n man(l-1)\n man(r+1)\n for l1,r1 in s[1..-1] do\n if l1 > r + 2\n if l1 > r + 3\n s1 << [l-1,r+1]\n l = l1\n end\n man(l1-1)\n end\n r = r1\n man(r+1)\n end\n s1 << [l-1,r+1]\n s = s1\n $d += 1\nend\n"}, {"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\ndef big(a,b) return (a>b)? a:b end\n\ndef small(a,b) return (a r + 1\n s << [l,r]\n l = xi\n end\n r = xi\nend\ns << [l,r]\n\n$d = 1\n$res = 0\n$y = []\ndef man(yi)\n $y << yi\n $m -= 1\n $res += $d\n if $m <= 0 then\n print($res, \"\\n\", $y.join(' '), \"\\n\")\n exit\n end\nend\nwhile $m > 0 do\n l,r = s[0]\n s1 = []\n man(l-1)\n man(r+1)\n for l1,r1 in s[1..-1] do\n if l1 > r + 2\n s1 << [l-1,r+1]\n l = l1\n man(l-1)\n end\n r = r1\n man(r+1)\n end\n s1 << [l-1,r+1]\n s = s1\n $d += 1\nend\n"}, {"source_code": "n, $m = readline.split.map(&:to_i)\nx = readline.split.map(&:to_i)\n\nx.sort!\n\nl = r = x[0]\ns = []\nfor xi in x[1..-1] do\n if xi > r + 1\n s << [l,r]\n l = xi\n end\n r = xi\nend\ns << [l,r]\n\n$d = 1\n$res = 0\n$y = []\ndef man(yi)\n $y << yi\n $m -= 1\n $res += $d\n if $m <= 0 then\n print($res, \"\\n\", $y.join(' '), \"\\n\")\n exit\n end\nend\nwhile $m > 0 do\n l,r = s[0]\n s1 = []\n man(l-1)\n for l1,r1 in s[1..-1] do\n man(r+1)\n if l1 > r + 2\n s1 << [l-1,r+1]\n l = l1\n man(l-1)\n end\n r = r1\n end\n man(r+1)\n s1 << [l-1,r+1]\n s = s1\n $d += 1\nend\n"}], "src_uid": "3a3666609b120d208c3e8366b186d89b"} {"nl": {"description": "Luckily, Serval got onto the right bus, and he came to the kindergarten on time. After coming to kindergarten, he found the toy bricks very funny.He has a special interest to create difficult problems for others to solve. This time, with many $$$1 \\times 1 \\times 1$$$ toy bricks, he builds up a 3-dimensional object. We can describe this object with a $$$n \\times m$$$ matrix, such that in each cell $$$(i,j)$$$, there are $$$h_{i,j}$$$ bricks standing on the top of each other.However, Serval doesn't give you any $$$h_{i,j}$$$, and just give you the front view, left view, and the top view of this object, and he is now asking you to restore the object. Note that in the front view, there are $$$m$$$ columns, and in the $$$i$$$-th of them, the height is the maximum of $$$h_{1,i},h_{2,i},\\dots,h_{n,i}$$$. It is similar for the left view, where there are $$$n$$$ columns. And in the top view, there is an $$$n \\times m$$$ matrix $$$t_{i,j}$$$, where $$$t_{i,j}$$$ is $$$0$$$ or $$$1$$$. If $$$t_{i,j}$$$ equals $$$1$$$, that means $$$h_{i,j}>0$$$, otherwise, $$$h_{i,j}=0$$$.However, Serval is very lonely because others are bored about his unsolvable problems before, and refused to solve this one, although this time he promises there will be at least one object satisfying all the views. As his best friend, can you have a try?", "input_spec": "The first line contains three positive space-separated integers $$$n, m, h$$$ ($$$1\\leq n, m, h \\leq 100$$$) — the length, width and height. The second line contains $$$m$$$ non-negative space-separated integers $$$a_1,a_2,\\dots,a_m$$$, where $$$a_i$$$ is the height in the $$$i$$$-th column from left to right of the front view ($$$0\\leq a_i \\leq h$$$). The third line contains $$$n$$$ non-negative space-separated integers $$$b_1,b_2,\\dots,b_n$$$ ($$$0\\leq b_j \\leq h$$$), where $$$b_j$$$ is the height in the $$$j$$$-th column from left to right of the left view. Each of the following $$$n$$$ lines contains $$$m$$$ numbers, each is $$$0$$$ or $$$1$$$, representing the top view, where $$$j$$$-th number of $$$i$$$-th row is $$$1$$$ if $$$h_{i, j}>0$$$, and $$$0$$$ otherwise. It is guaranteed that there is at least one structure satisfying the input.", "output_spec": "Output $$$n$$$ lines, each of them contains $$$m$$$ integers, the $$$j$$$-th number in the $$$i$$$-th line should be equal to the height in the corresponding position of the top view. If there are several objects satisfying the views, output any one of them.", "sample_inputs": ["3 7 3\n2 3 0 0 2 0 1\n2 1 3\n1 0 0 0 1 0 0\n0 0 0 0 0 0 1\n1 1 0 0 0 0 0", "4 5 5\n3 5 2 0 4\n4 2 5 4\n0 0 0 0 1\n1 0 1 0 0\n0 1 0 0 0\n1 1 1 0 0"], "sample_outputs": ["1 0 0 0 2 0 0\n0 0 0 0 0 0 1\n2 3 0 0 0 0 0", "0 0 0 0 4\n1 0 2 0 0\n0 5 0 0 0\n3 4 1 0 0"], "notes": "Note The graph above illustrates the object in the first example. The first graph illustrates the object in the example output for the second example, and the second graph shows the three-view drawing of it."}, "positive_code": [{"source_code": "N,M,H = gets.split.map(&:to_i)\nf = gets.split.map(&:to_i)\nl = gets.split.map(&:to_i)\nt = []\nN.times do \n t << gets.split.map(&:to_i)\nend\n\nN.times do |i|\n l[i] = [l[i],i]\nend\n#p l\nl.sort_by!{|a| a[0]}.reverse!\n\nans = Array.new(N){Array.new(M,nil)}\n\nd = []\nl.each do |h,i|\n tmp = 0\n M.times do |j|\n if f[j] >= h && t[i][j] != 0\n tmp = j \n break\n end\n end\n ans[i][tmp] = h\nend\n\nM.times do |j|\n l.each do |h,i|\n if t[i][j] != 0\n ans[i][j] = f[j]\n break\n end\n end\nend\nN.times do |i|\n M.times do |j|\n if ans[i][j].nil?\n ans[i][j] = t[i][j] == 0 ? 0 : 1\n end\n end\nend\n\nans.each do |a|\n puts a.join(\" \")\nend"}, {"source_code": "N, M, H = gets.split.map(&:to_i)\nA = gets.split.map(&:to_i)\nB = gets.split.map(&:to_i)\ngrid = N.times.map { gets.split.map(&:to_i) }\n\nB.each_with_index do |b, i|\n temp = grid.map(&:dup)\n\n temp[i].each_with_index do |h, j|\n if h >= 1 && b <= A[j]\n grid[i][j] = b\n end\n end\nend\n\nA.each_with_index do |a, i|\n temp = grid.map(&:dup)\n\n N.times do |j|\n h = temp[j][i]\n\n if h >= 1 && a <= B[j] && h <= a\n grid[j][i] = a\n end\n end\nend\n\nputs grid.map { |l| l.join(' ') }\n"}, {"source_code": "n, m, h = gets.chomp.split(\" \").map(&:to_i)\n\nfront = gets.chomp.split(\" \").map(&:to_i)\nleft = gets.chomp.split(\" \").map(&:to_i)\n\ntop = []\nn.times do\n top << gets.chomp.split(\" \").map(&:to_i)\nend\n\nret = Array.new(n){Array.new(m, -1)}\n\nm.times do |i|\n n.times do |j|\n ret[j][i] = front[i]\n end\nend\n\nn.times do |j|\n m.times do |i|\n ret[j][i] = [ret[j][i], left[j]].min\n end\nend\n\nm.times do |i|\n n.times do |j|\n if top[j][i] == 0\n ret[j][i] = 0\n end\n end\nend\n\nret.each{|line| puts line.join(\" \")}"}], "negative_code": [{"source_code": "N,M,H = gets.split.map(&:to_i)\nf = gets.split.map(&:to_i)\nl = gets.split.map(&:to_i)\nt = []\nN.times do \n t << gets.split.map(&:to_i)\nend\n\nN.times do |i|\n l[i] = [l[i],i]\nend\n#p l\nl.sort_by!{|a| a[0]}.reverse!\n\nans = Array.new(N){Array.new(M,nil)}\n\nd = []\nl.each do |h,i|\n tmp = nil\n N.times do |j|\n tmp = j if f[j] == h && t[i][j] != 0\n end\n if !tmp.nil?\n ans[i][tmp] = h\n d << tmp\n else\n d.each do |j|\n if t[i][j] != 0\n ans[i][j] = h\n break\n end\n end\n end\nend\nM.times do |i|\n if !d.include?(i)\n l.each do |h,j|\n if t[j][i] != 0\n ans[j][i] = f[i]\n break\n end\n end\n end\nend\nN.times do |i|\n M.times do |j|\n if ans[i][j].nil?\n ans[i][j] = t[i][j] == 0 ? 0 : 1\n end\n end\nend\n\nans.each do |a|\n puts a.join(\" \")\nend"}], "src_uid": "7361e8acf0d712c317e8b99211a7b548"} {"nl": {"description": "The Queen of England has n trees growing in a row in her garden. At that, the i-th (1 ≤ i ≤ n) tree from the left has height ai meters. Today the Queen decided to update the scenery of her garden. She wants the trees' heights to meet the condition: for all i (1 ≤ i < n), ai + 1 - ai = k, where k is the number the Queen chose.Unfortunately, the royal gardener is not a machine and he cannot fulfill the desire of the Queen instantly! In one minute, the gardener can either decrease the height of a tree to any positive integer height or increase the height of a tree to any positive integer height. How should the royal gardener act to fulfill a whim of Her Majesty in the minimum number of minutes?", "input_spec": "The first line contains two space-separated integers: n, k (1 ≤ n, k ≤ 1000). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 1000) — the heights of the trees in the row. ", "output_spec": "In the first line print a single integer p — the minimum number of minutes the gardener needs. In the next p lines print the description of his actions. If the gardener needs to increase the height of the j-th (1 ≤ j ≤ n) tree from the left by x (x ≥ 1) meters, then print in the corresponding line \"+ j x\". If the gardener needs to decrease the height of the j-th (1 ≤ j ≤ n) tree from the left by x (x ≥ 1) meters, print on the corresponding line \"- j x\". If there are multiple ways to make a row of trees beautiful in the minimum number of actions, you are allowed to print any of them.", "sample_inputs": ["4 1\n1 2 1 5", "4 1\n1 2 3 4"], "sample_outputs": ["2\n+ 3 2\n- 4 1", "0"], "notes": null}, "positive_code": [{"source_code": "n, k = gets.split.map{|e| e.to_i}\n\na = gets.split.map{|e| e.to_i}.to_a\n\nmn = 10**9\n\n(1..1000).each do |i|\n cur = 0\n (0...a.size).each do |j|\n if ((a[j] - (i + k * j)) != 0)\n cur += 1\n end\n end\n if (cur < mn) \n mn = cur\n end\nend\n\nputs mn\n\n(1..1000).each do |i|\n cur = 0\n (0...a.size).each do |j|\n if ((a[j] - (i + k * j)) != 0)\n cur += 1\n end\n end\n if (cur == mn)\n (0...a.size).each do |j|\n if (a[j] - (i + k * j) < 0)\n print '+ '\n puts (j + 1).to_s + ' ' + (a[j] - (i + k * j)).abs.to_s\n elsif (a[j] - (i + k * j) > 0)\n print '- '\n puts (j + 1).to_s + ' ' + (a[j] - (i + k * j)).abs.to_s\n end\n end\n break\n end\nend\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\nmn, c = 0x3f3f3f3f3f3f3f3f, 0\nn.times do |i|\n\tcc, tot = a[i]-i*k, 0\n\tnext if cc <= 0\n\tn.times{|j| cc,tot=cc+k,tot+(a[j]==cc ? 0 : 1)}\n\tmn, c = tot, a[i]-i*k if tot < mn\nend\np mn\nn.times do |i|\n\tputs \"- #{i+1} #{a[i]-c}\" if c < a[i]\n\tputs \"+ #{i+1} #{c-a[i]}\" if c > a[i]\n\tc += k\nend\n"}], "negative_code": [{"source_code": "n, k = gets.split.map{|e| e.to_i}\n\na = gets.split.map{|e| e.to_i}.to_a\n\nmn = 10**9\n\n(1...1000).each do |i|\n cur = 0\n (0...a.size).each do |j|\n if ((a[j] - (i + k * j)) != 0)\n cur += 1\n end\n end\n if (cur < mn) \n mn = cur\n end\nend\n\nputs mn\n\n(1...1000).each do |i|\n cur = 0\n (0...a.size).each do |j|\n if ((a[j] - (i + k * j)) != 0)\n cur += 1\n end\n end\n if (cur == mn)\n (0...a.size).each do |j|\n if (a[j] - (i + k * j) < 0)\n print '+ '\n puts (j + 1).to_s + ' ' + (a[j] - (i + k * j)).abs.to_s\n elsif (a[j] - (i + k * j) > 0)\n print '- '\n puts (j + 1).to_s + ' ' + (a[j] - (i + k * j)).abs.to_s\n end\n end\n break\n end\nend\n"}, {"source_code": "n, k = gets.split.map{|e| e.to_i}\n\na = gets.split.map{|e| e.to_i}.to_a\n\nmn = 10**9\n\n(0...1000).each do |i|\n cur = 0\n (0...a.size).each do |j|\n if ((a[j] - (i + k * j)) != 0)\n cur += 1\n end\n end\n if (cur < mn) \n mn = cur\n end\nend\n\nputs mn\n\n(0...1000).each do |i|\n cur = 0\n (0...a.size).each do |j|\n if ((a[j] - (i + k * j)) != 0)\n cur += 1\n end\n end\n if (cur == mn)\n (0...a.size).each do |j|\n if (a[j] - (i + k * j) < 0)\n print '+ '\n puts (j + 1).to_s + ' ' + (a[j] - (i + k * j)).abs.to_s\n elsif (a[j] - (i + k * j) > 0)\n print '- '\n puts (j + 1).to_s + ' ' + (a[j] - (i + k * j)).abs.to_s\n end\n end\n break\n end\nend\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\nmn, c = 0x3f3f3f3f3f3f3f3f, 0\n(-k..k).each do |i|\n\tcc, tot = a[0] + i, 0\n\tn.times{|j| cc,tot=cc+k,tot+(a[j]==cc ? 0 : 1)}\n\tmn, c = tot, a[0] + i if tot < mn\nend\np mn\nn.times do |i|\n\tif (c < a[i])\n\t\tputs \"- #{i+1} #{a[i]-c}\"\n\telsif (c > a[i])\n\t\tputs \"+ #{i+1} #{c-a[i]}\"\n\tend\n\tc += k\nend\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\nmn, c = 0x3f3f3f3f3f3f3f3f, 0\n(-k..k).each do |i|\n\tcc, tot = a[0] + i, 0\n\tnext if cc <= 0\n\tn.times{|j| cc,tot=cc+k,tot+(a[j]==cc ? 0 : 1)}\n\tmn, c = tot, a[0] + i if tot < mn\nend\np mn\nn.times do |i|\n\tputs \"- #{i+1} #{a[i]-c}\" if c < a[i]\n\tputs \"+ #{i+1} #{c-a[i]}\" if c > a[i]\n\tc += k\nend\n"}], "src_uid": "7f08e74945d6b58abde1d8002b8b686a"} {"nl": {"description": "Alice has a birthday today, so she invited home her best friend Bob. Now Bob needs to find a way to commute to the Alice's home.In the city in which Alice and Bob live, the first metro line is being built. This metro line contains $$$n$$$ stations numbered from $$$1$$$ to $$$n$$$. Bob lives near the station with number $$$1$$$, while Alice lives near the station with number $$$s$$$. The metro line has two tracks. Trains on the first track go from the station $$$1$$$ to the station $$$n$$$ and trains on the second track go in reverse direction. Just after the train arrives to the end of its track, it goes to the depot immediately, so it is impossible to travel on it after that.Some stations are not yet open at all and some are only partially open — for each station and for each track it is known whether the station is closed for that track or not. If a station is closed for some track, all trains going in this track's direction pass the station without stopping on it.When the Bob got the information on opened and closed stations, he found that traveling by metro may be unexpectedly complicated. Help Bob determine whether he can travel to the Alice's home by metro or he should search for some other transport.", "input_spec": "The first line contains two integers $$$n$$$ and $$$s$$$ ($$$2 \\le s \\le n \\le 1000$$$) — the number of stations in the metro and the number of the station where Alice's home is located. Bob lives at station $$$1$$$. Next lines describe information about closed and open stations. The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$a_i = 0$$$ or $$$a_i = 1$$$). If $$$a_i = 1$$$, then the $$$i$$$-th station is open on the first track (that is, in the direction of increasing station numbers). Otherwise the station is closed on the first track. The third line contains $$$n$$$ integers $$$b_1, b_2, \\ldots, b_n$$$ ($$$b_i = 0$$$ or $$$b_i = 1$$$). If $$$b_i = 1$$$, then the $$$i$$$-th station is open on the second track (that is, in the direction of decreasing station numbers). Otherwise the station is closed on the second track.", "output_spec": "Print \"YES\" (quotes for clarity) if Bob will be able to commute to the Alice's home by metro and \"NO\" (quotes for clarity) otherwise. You can print each letter in any case (upper or lower).", "sample_inputs": ["5 3\n1 1 1 1 1\n1 1 1 1 1", "5 4\n1 0 0 0 1\n0 1 1 1 1", "5 2\n0 1 1 1 1\n1 1 1 1 1"], "sample_outputs": ["YES", "YES", "NO"], "notes": "NoteIn the first example, all stations are opened, so Bob can simply travel to the station with number $$$3$$$.In the second example, Bob should travel to the station $$$5$$$ first, switch to the second track and travel to the station $$$4$$$ then.In the third example, Bob simply can't enter the train going in the direction of Alice's home."}, "positive_code": [{"source_code": "\nn,s = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\nb = gets.split.map(&:to_i)\n\nif(a[0]==0 || (a[s-1]==0 && b[s-1]==0)) then puts(\"NO\")\nelsif(a[s-1]==1) then puts(\"YES\")\nelsif(b[s-1]==0) then puts(\"NO\")\nelse\n\tflag = 0\n\tfor i in (s...n) do\n\t\tif(a[i]==1 && b[i]==1) then flag = 1; break; end\n\tend\n\tif(flag==1) then puts(\"YES\")\n\telse puts(\"NO\") end\nend\n"}, {"source_code": "\ndef can_travel(alice_station, from_bob_metro, to_bob_metro)\n return \"NO\" if from_bob_metro[0] == 0\n \n return \"YES\" if from_bob_metro[alice_station - 1] == 1\n \n index = alice_station\n while index < from_bob_metro.length && from_bob_metro[index] + to_bob_metro[index] != 2\n index += 1\n end\n \n return \"NO\" if index == from_bob_metro.length\n return \"YES\" if to_bob_metro[alice_station - 1] == 1\n \"NO\"\nend\n\nnumber_of_stations, alice_station = gets.chomp.split(\" \").map(&:to_i)\nfrom_bob_metro = gets.chomp.split(\" \").map(&:to_i)\nto_bob_metro = gets.chomp.split(\" \").map(&:to_i)\n\nputs can_travel(alice_station, from_bob_metro, to_bob_metro)"}, {"source_code": "n,s = gets.strip.split.map(&:to_i)\ns -= 1\n\na = gets.strip.split.map(&:to_i)\nb = gets.strip.split.map(&:to_i)\n\nli = -1\nposs = []\nif a[0]==1\n 0.upto(n-1).each do |i|\n if a[i]==1\n poss << i\n if b[i] == 1\n li = i\n end\n end\n end\nend\n\n# puts li\nif li>-1\n li.downto(0) do |i|\n if b[i] == 1\n poss << i\n end\n end\nend\n\nputs (poss.include?(s) ? \"YES\" : \"NO\")\n"}, {"source_code": "n, s = gets.split.map(&:to_i)\nA = gets.split.map(&:to_i)\nB = gets.split.map(&:to_i)\nAd = A[s..n]\n\nif A[0] == 0\n print \"NO\"\nelsif A[s - 1] == 1\n print \"YES\"\nelsif B[s - 1] == 0\n print \"NO\"\nelsif !Ad.include?(1)\n print \"NO\"\nelsif Ad.each.with_index().any? { |v, i| v == 1 && B[i + s] == 1}\n print \"YES\"\nelse\n print \"NO\"\nend\n"}], "negative_code": [{"source_code": "n, s = gets.split.map(&:to_i)\nA = gets.split.map(&:to_i)\nB = gets.split.map(&:to_i)\nAd = A[s..n]\n\nif A[0] == 0\n print \"NO\"\nelsif A[s - 1] == 1\n print \"YES\"\nelsif B[s - 1] == 0\n print \"NO\"\nelsif !Ad.include?(1)\n print \"NO\"\nelsif Ad.each.with_index().any? { |v, i| v == 1 && B[i + s - 1] == 1 }\n print \"YES\"\nelse\n print \"NO\"\nend\n"}, {"source_code": "n, s = gets.split.map(&:to_i)\nA = gets.split.map(&:to_i)\nB = gets.split.map(&:to_i)\nAd = A[s..n]\n\nif A[0] == 0\n print \"NO\"\nelsif A[s] == 1\n print \"YES\"\nelsif B[s] == 0\n print \"NO\"\nelsif !Ad.include?(1)\n print \"NO\"\nelsif Ad.with_index.any? { |(v, i)| v == 1 && B[i + s] == 1 }\n print \"YES\"\nelse\n print \"NO\"\nend\n"}], "src_uid": "64b597a47106d0f08fcfad155e0495c3"} {"nl": {"description": "Polycarp likes to play with numbers. He takes some integer number $$$x$$$, writes it down on the board, and then performs with it $$$n - 1$$$ operations of the two kinds: divide the number $$$x$$$ by $$$3$$$ ($$$x$$$ must be divisible by $$$3$$$); multiply the number $$$x$$$ by $$$2$$$. After each operation, Polycarp writes down the result on the board and replaces $$$x$$$ by the result. So there will be $$$n$$$ numbers on the board after all.You are given a sequence of length $$$n$$$ — the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.It is guaranteed that the answer exists.", "input_spec": "The first line of the input contatins an integer number $$$n$$$ ($$$2 \\le n \\le 100$$$) — the number of the elements in the sequence. The second line of the input contains $$$n$$$ integer numbers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 3 \\cdot 10^{18}$$$) — rearranged (reordered) sequence that Polycarp can wrote down on the board.", "output_spec": "Print $$$n$$$ integer numbers — rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board. It is guaranteed that the answer exists.", "sample_inputs": ["6\n4 8 6 3 12 9", "4\n42 28 84 126", "2\n1000000000000000000 3000000000000000000"], "sample_outputs": ["9 3 6 12 4 8", "126 42 84 28", "3000000000000000000 1000000000000000000"], "notes": "NoteIn the first example the given sequence can be rearranged in the following way: $$$[9, 3, 6, 12, 4, 8]$$$. It can match possible Polycarp's game which started with $$$x = 9$$$."}, "positive_code": [{"source_code": "\ndef count_factor(n, f)\n count = 0\n crt = n\n while crt % f == 0\n crt /= f\n count += 1\n end\n count\nend\n\ndef arrange ns\n ns.map{ |n| [n, count_factor(n, 2), count_factor(n, 3)] }\n .sort_by { |_, c2, c3| [-c3, c2] }\n .map { |n, _, _| n }\nend\n\ndef solution\n read_int\n ns = read_ints\n\n puts arrange(ns).join(' ')\nend\n\ndef read_int\n gets.to_i\nend\n\ndef read_ints\n gets.split.map(&:to_i)\nend\n\ndef read_string\n gets.chomp\nend\n\nsolution unless ENV['TEST__MODE']\n\n"}], "negative_code": [], "src_uid": "f9375003a3b64bab17176a05764c20e8"} {"nl": {"description": "In the rush of modern life, people often forget how beautiful the world is. The time to enjoy those around them is so little that some even stand in queues to several rooms at the same time in the clinic, running from one queue to another.(Cultural note: standing in huge and disorganized queues for hours is a native tradition in Russia, dating back to the Soviet period. Queues can resemble crowds rather than lines. Not to get lost in such a queue, a person should follow a strict survival technique: you approach the queue and ask who the last person is, somebody answers and you join the crowd. Now you're the last person in the queue till somebody else shows up. You keep an eye on the one who was last before you as he is your only chance to get to your destination) I'm sure many people have had the problem when a stranger asks who the last person in the queue is and even dares to hint that he will be the last in the queue and then bolts away to some unknown destination. These are the representatives of the modern world, in which the ratio of lack of time is so great that they do not even watch foreign top-rated TV series. Such people often create problems in queues, because the newcomer does not see the last person in the queue and takes a place after the \"virtual\" link in this chain, wondering where this legendary figure has left.The Smart Beaver has been ill and he's made an appointment with a therapist. The doctor told the Beaver the sad news in a nutshell: it is necessary to do an electrocardiogram. The next day the Smart Beaver got up early, put on the famous TV series on download (three hours till the download's complete), clenched his teeth and bravely went to join a queue to the electrocardiogram room, which is notorious for the biggest queues at the clinic.Having stood for about three hours in the queue, the Smart Beaver realized that many beavers had not seen who was supposed to stand in the queue before them and there was a huge mess. He came up to each beaver in the ECG room queue and asked who should be in front of him in the queue. If the beaver did not know his correct position in the queue, then it might be his turn to go get an ECG, or maybe he should wait for a long, long time...As you've guessed, the Smart Beaver was in a hurry home, so he gave you all the necessary information for you to help him to determine what his number in the queue can be.", "input_spec": "The first line contains two integers n (1 ≤ n ≤ 103) and x (1 ≤ x ≤ n) — the number of beavers that stand in the queue and the Smart Beaver's number, correspondingly. All willing to get to the doctor are numbered from 1 to n. The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ n) — the number of the beaver followed by the i-th beaver. If ai = 0, then the i-th beaver doesn't know who is should be in front of him. It is guaranteed that values ai are correct. That is there is no cycles in the dependencies. And any beaver is followed by at most one beaver in the queue. The input limits for scoring 30 points are (subproblem B1): It is guaranteed that the number of zero elements ai doesn't exceed 20. The input limits for scoring 100 points are (subproblems B1+B2): The number of zero elements ai is arbitrary. ", "output_spec": "Print all possible positions of the Smart Beaver in the line in the increasing order.", "sample_inputs": ["6 1\n2 0 4 0 6 0", "6 2\n2 3 0 5 6 0", "4 1\n0 0 0 0", "6 2\n0 0 1 0 4 5"], "sample_outputs": ["2\n4\n6", "2\n5", "1\n2\n3\n4", "1\n3\n4\n6"], "notes": "Note Picture for the fourth test. "}, "positive_code": [{"source_code": "#! ruby -Ku\n# -*- coding: utf-8 -*-\n# file = open(\"input.txt\")\n# total_sb = file.gets.split.map(&:to_i)\n# follow = file.gets.split.map(&:to_i) # [i]+1 follows follow[i]\ntotal_sb = gets.split.map(&:to_i)\nfollow = gets.split.map(&:to_i) # [i]+1 follows follow[i]\n\nnum_total = total_sb.shift\nsb_num = total_sb.shift\n\nchain = Struct.new(\"Chain\", :length, :check)\nchains = Array::new\nnum_total.times{chains << chain.new(1, false)}\n\n# make chains\ndef trace_chain(follow, chains, index, length)\n if follow[index] == 0\n chains[index].length = length + 1\n return length + 1\n else\n chains[index].length = trace_chain(follow, chains, follow[index]-1, length+1)\n return chains[index].length\n end\nend\n# check if SB is contained\ndef check_sb_contains(follow, chains, index, check, sb_num)\n if sb_num-1 == index\n check = true\n end\n if follow[index] == 0\n chains[index].check = check\n return check\n else\n chains[index].check = check_sb_contains(follow, chains, follow[index]-1, check, sb_num)\n return chains[index].check\n end\nend\n\nfollowed = Array::new\nnum_total.times {followed << false}\n# who is the last in each chain\nfollow.each do |x|\n if x != 0\n followed[x-1] = true\n end\nend\n\n# number of chain before smart beaver\noffset_smart_beaver = trace_chain(follow, chains, sb_num-1, 0)\n# puts offset_smart_beaver\n\n# count each length\nnum_total.times do |i|\n if followed[i] == false\n trace_chain(follow, chains, i, 0)\n check_sb_contains(follow, chains, i, false, sb_num)\n end\nend\n\n# count chains number\nchains_bin = Array::new\nnum_total.times do |i|\n if followed[i] == false && !chains[i].check\n chains_bin << chains[i].length\n end\nend\nchains_bin.sort!\n# puts chains_bin\n\n# count chain pattern by dp\ndp_table = Array::new\ndp_table << 1\nchains_bin.each do |bin|\n 1000.downto(0) do |i|\n if dp_table[i] != nil\n dp_table[i+bin] = 1\n end\n end\nend\n# puts dp_table\n\n1000.times do |i|\n if dp_table[i] != nil\n puts i + offset_smart_beaver\n end\nend\n\n"}, {"source_code": "require 'set'\n\nn, x = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\n\ns = Set.new\n\na.each do |ai|\n\t\ts.add(ai) if ai > 0\nend\n\nsum = 1\ndp = Array.new(1001,false)\ndp[0] = true\n\na.each_with_index do |ai,i1|\n\ti = i1 + 1\n\tnext if s.member?(i)\n\tflag = false\n\tacc = 0\n\twhile i > 0\n\t\tsum += 1 if flag\n\t\tflag ||= i == x\n\t\tacc += 1\n\t\ti = a[i-1]\n\tend\n\tnext if flag\n\t1000.downto(0) do |j|\n\t\tnext if not dp[j]\n\t\tdp[j+acc] ||= dp[j]\n\tend\nend\n\n0.upto(1000) do |i|\n\tnext if not dp[i]\n\tputs(sum + i)\nend\n"}, {"source_code": "#! ruby -Ku\n# -*- coding: utf-8 -*-\n# file = open(\"input.txt\")\n# total_sb = file.gets.split.map(&:to_i)\n# follow = file.gets.split.map(&:to_i) # [i]+1 follows follow[i]\ntotal_sb = gets.split.map(&:to_i)\nfollow = gets.split.map(&:to_i) # [i]+1 follows follow[i]\n\nnum_total = total_sb.shift\nsb_num = total_sb.shift\n\nchain = Struct.new(\"Chain\", :length, :check)\nchains = Array::new\nnum_total.times{chains << chain.new(1, false)}\n\n# make chains\ndef trace_chain(follow, chains, index, length)\n if follow[index] == 0\n chains[index].length = length + 1\n return length + 1\n else\n chains[index].length = trace_chain(follow, chains, follow[index]-1, length+1)\n return chains[index].length\n end\nend\n# check if SB is contained\ndef check_sb_contains(follow, chains, index, check, sb_num)\n if sb_num-1 == index\n check = true\n end\n if follow[index] == 0\n chains[index].check = check\n return check\n else\n chains[index].check = check_sb_contains(follow, chains, follow[index]-1, check, sb_num)\n return chains[index].check\n end\nend\n\nfollowed = Array::new\nnum_total.times {followed << false}\n# who is the last in each chain\nfollow.each do |x|\n if x != 0\n followed[x-1] = true\n end\nend\n\n# number of chain before smart beaver\noffset_smart_beaver = trace_chain(follow, chains, sb_num-1, 0)\n# puts offset_smart_beaver\n\n# count each length\nnum_total.times do |i|\n if followed[i] == false\n trace_chain(follow, chains, i, 0)\n check_sb_contains(follow, chains, i, false, sb_num)\n end\nend\n\n# count chains number\nchains_bin = Array::new\nnum_total.times do |i|\n if followed[i] == false && !chains[i].check\n chains_bin << chains[i].length\n end\nend\nchains_bin.sort!\n# puts chains_bin\n\n# count chain pattern by dp\ndp_table = Array::new\ndp_table << 1\nchains_bin.each do |bin|\n 1000.downto(0) do |i|\n if dp_table[i] != nil\n dp_table[i+bin] = 1\n end\n end\nend\n# puts dp_table\n\n1000.times do |i|\n if dp_table[i] != nil\n puts i + offset_smart_beaver\n end\nend\n\n"}, {"source_code": "require 'set'\n\nn, x = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\n\ns = Set.new\n\na.each do |ai|\n\t\ts.add(ai) if ai > 0\nend\n\nsum = 1\ndp = Array.new(1001,false)\ndp[0] = true\n\na.each_with_index do |ai,i1|\n\ti = i1 + 1\n\tnext if s.member?(i)\n\tflag = false\n\tacc = 0\n\twhile i > 0\n\t\tsum += 1 if flag\n\t\tflag ||= i == x\n\t\tacc += 1\n\t\ti = a[i-1]\n\tend\n\tnext if flag\n\t1000.downto(0) do |j|\n\t\tnext if not dp[j]\n\t\tdp[j+acc] ||= dp[j]\n\tend\nend\n\n0.upto(1000) do |i|\n\tnext if not dp[i]\n\tputs(sum + i)\nend\n"}], "negative_code": [], "src_uid": "5a6b97a2aa27dd2c562f73a7a8f16720"} {"nl": {"description": "Petya has got 2n cards, each card contains some integer. The numbers on the cards can be the same. Let's index all cards by consecutive integers from 1 to 2n. We'll denote the number that is written on a card with number i, as ai. In order to play one entertaining game with his friends, Petya needs to split the cards into pairs so that each pair had equal numbers on the cards. Help Petya do that.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 3·105). The second line contains the sequence of 2n positive integers a1, a2, ..., a2n (1 ≤ ai ≤ 5000) — the numbers that are written on the cards. The numbers on the line are separated by single spaces.", "output_spec": "If it is impossible to divide the cards into pairs so that cards in each pair had the same numbers, print on a single line integer -1. But if the required partition exists, then print n pairs of integers, a pair per line — the indices of the cards that form the pairs. Separate the numbers on the lines by spaces. You can print the pairs and the numbers in the pairs in any order. If there are multiple solutions, print any of them.", "sample_inputs": ["3\n20 30 10 30 20 10", "1\n1 2"], "sample_outputs": ["4 2\n1 5\n6 3", "-1"], "notes": null}, "positive_code": [{"source_code": "lines = IO.readlines(\"input.txt\")\n\n#n = lines[0].to_i\ncards = []\nlines[1].split(\" \").each_with_index do |val,ind|\n (cards[val.to_i] ||= []) << ind.to_i+1\nend\n\ncards.compact!\n\nFile.open(\"output.txt\", \"w\") do |fout|\n if cards.count{|p| p.size%2!=0 if p} != 0\n fout.puts(\"-1\")\n else\n cards.each do |pairs|\n pairs.each_slice(2) do |pair|\n fout.puts(\"#{pair[0]} #{pair[1]}\")\n end if pairs\n end\n end\nend\n\n"}, {"source_code": "lines = IO.readlines(\"input.txt\")\n\nn = 0\npdi = 1\npairs = []\npaired = []\n\n n = lines[0].to_i\n i = 1\n for a in lines[1].split(\" \") do\n ai = a.to_i\n if pairs[ai] == nil\n pairs[ai] = i\n else\n paired[pdi], paired[pdi+1], pairs[ai] = pairs[ai], i, nil\n pdi += 2\n end\n i += 1\n end\n\nFile.open(\"output.txt\", \"w\") do |fout|\n if pdi != n*2+1\n fout.puts(\"-1\")\n else\n i = 1\n while i <= n*2 do\n fout.puts(\"#{paired[i]} #{paired[i+1]}\")\n i += 2\n end\n end\nend\n\n"}], "negative_code": [{"source_code": "require 'scanf'\n\nn = 0\npdi = 1\npairs = []\npaired = []\n\nFile.open(\"input.txt\", \"r\") do |fin|\n n = fin.scanf(\"%d\")[0]\n i = 1\n while i <= n*2 do\n ai = fin.scanf(\"%d\")[0]\n if pairs[ai] == nil\n pairs[ai] = i\n else\n paired[pdi], paired[pdi+1], pairs[ai] = pairs[ai], i, nil\n pdi += 2\n end\n i += 1\n end\nend\n\nFile.open(\"output.txt\", \"w\") do |fout|\n if pdi != n*2+1\n fout.puts(\"-1\")\n else\n i = 1\n while i <= n*2 do\n fout.puts(\"#{paired[i]} #{paired[i+1]}\")\n i += 2\n end\n end\nend\n\n"}, {"source_code": "fi=File.new('input.txt','r');fo=File.new('output.txt','w')\nfi.gets;a=fi.gets.strip.split\nif a.uniq.size!=a.size/2; fo.puts \"-1\"; exit end\na.map.with_index(1).to_a.sort{|x,y| x[0]<=>y[0]}.each_slice(2) do |b|; fo.puts \"#{b[0][1]} #{b[1][1]}\"; end\n\n"}], "src_uid": "0352429425782d7fe44818594eb0660c"} {"nl": {"description": "Petya and Vasya are inventing a new game that requires a rectangular board and one chess piece. At the beginning of the game the piece stands in the upper-left corner of the board. Two players move the piece in turns. Each turn the chess piece can be moved either one square to the right or one square down or jump k squares diagonally down and to the right. The player who can’t move the piece loses. The guys haven’t yet thought what to call the game or the best size of the board for it. Your task is to write a program that can determine the outcome of the game depending on the board size.", "input_spec": "The first input line contains two integers t and k (1 ≤ t ≤ 20, 1 ≤ k ≤ 109). Each of the following t lines contains two numbers n, m — the board’s length and width (1 ≤ n, m ≤ 109).", "output_spec": "Output t lines that can determine the outcomes of the game on every board. Write «+» if the first player is a winner, and «-» otherwise.", "sample_inputs": ["10 2\n1 1\n1 2\n2 1\n2 2\n1 3\n2 3\n3 1\n3 2\n3 3\n4 3"], "sample_outputs": ["-\n+\n+\n-\n-\n+\n-\n+\n+\n+"], "notes": null}, "positive_code": [{"source_code": "I = open('input.txt')\nO = open('output.txt', 'w')\nT, K = I.gets.split.map(&:to_i)\nT.times do\n\tn, m = I.gets.split.map{|_| _.to_i - 1}.sort\n\tO.puts n % (K + 1) == K || (n + m - (K == 1 ? 0 : n / (K + 1))).odd? ? :+ : :-\nend\n\n__END__\nWA3: K=1\n"}, {"source_code": "I = open('input.txt')\nO = open('output.txt', 'w')\nT, K = I.gets.split.map(&:to_i)\nT.times do\n\tn, m = I.gets.split.map{|_| _.to_i - 1}.sort\n\tO.puts n % (K + 1) == K || (n + m - (K == 1 ? 0 : n / (K + 1))).odd? ? :+ : :-\nend\n\n__END__\nWA3: K=1\n"}, {"source_code": "I = open('input.txt')\nO = open('output.txt', 'w')\nT, K = I.gets.split.map(&:to_i)\nT.times do\n\tn, m = I.gets.split.map{|_| _.to_i - 1}.sort\n\tO.puts n % (K + 1) == K || (n + m - (K == 1 ? 0 : n / (K + 1))).odd? ? :+ : :-\nend\n\n__END__\nWA3: K=1\n\n"}, {"source_code": "I = open('input.txt')\nO = open('output.txt', 'w')\nT, K = I.gets.split.map(&:to_i)\nT.times do\n\tn, m = I.gets.split.map{|_| _.to_i - 1}.sort\n\tO.puts n % (K + 1) == K || (n + m - (K == 1 ? 0 : n / (K + 1))).odd? ? :+ : :-\nend\n\n__END__\nWA3: K=1\n\n"}, {"source_code": "I = open('input.txt')\nO = open('output.txt', 'w')\nT, K = I.gets.split.map(&:to_i)\nT.times do\n\tn, m = I.gets.split.map{|_| _.to_i - 1}.sort\n\tO.puts n % (K + 1) == K || (n + m - (K == 1 ? 0 : n / (K + 1))).odd? ? :+ : :-\nend\n\n__END__\nWA3: K=1\n\n"}, {"source_code": "I = open('input.txt')\nO = open('output.txt', 'w')\nT, K = I.gets.split.map(&:to_i)\nT.times do\n\tn, m = I.gets.split.map{|_| _.to_i - 1}.sort\n\tO.puts n % (K + 1) == K || (n + m - (K == 1 ? 0 : n / (K + 1))).odd? ? :+ : :-\nend\n\n__END__\nWA3: K=1\n\n"}, {"source_code": "I = open('input.txt')\nO = open('output.txt', 'w')\nT, K = I.gets.split.map(&:to_i)\nT.times do\n n, m = I.gets.split.map{|_| _.to_i - 1}.sort\n O.puts n % (K + 1) == K || (n + m - (K == 1 ? 0 : n / (K + 1))).odd? ? :+ : :-\nend\n\n__END__\nWA3: K=1"}, {"source_code": "I = open('input.txt')\nO = open('output.txt', 'w')\nT, K = I.gets.split.map(&:to_i)\nT.times do\n\tn, m = I.gets.split.map{|_| _.to_i - 1}.sort\n\tO.puts n % (K + 1) == K || (n + m - (K == 1 ? 0 : n / (K + 1))).odd? ? :+ : :-\nend\n\n__END__\nWA3: K=1\n\n"}, {"source_code": "I = open('input.txt')\nO = open('output.txt', 'w')\nT, K = I.gets.split.map(&:to_i)\nT.times do\n\tn, m = I.gets.split.map{|_| _.to_i - 1}.sort\n\tO.puts n % (K + 1) == K || (n + m - (K == 1 ? 0 : n / (K + 1))).odd? ? :+ : :-\nend\n \n__END__\nWA3: K=1"}, {"source_code": "I = open('input.txt')\nO = open('output.txt', 'w')\nT, K = I.gets.split.map(&:to_i)\nT.times do\n\tn, m = I.gets.split.map{|_| _.to_i - 1}.sort\n\tO.puts n % (K + 1) == K || (n + m - (K == 1 ? 0 : n / (K + 1))).odd? ? :+ : :-\nend\n\n__END__\nWA3: K=1\n\n"}, {"source_code": "I = open('input.txt')\nO = open('output.txt', 'w')\nT, K = I.gets.split.map(&:to_i)\nT.times do\n\tn, m = I.gets.split.map{|_| _.to_i - 1}.sort\n\tO.puts n % (K + 1) == K || (n + m - (K == 1 ? 0 : n / (K + 1))).odd? ? :+ : :-\nend\n\n__END__\nWA3: K=1\n\n"}, {"source_code": "I = open('input.txt')\nO = open('output.txt', 'w')\nT, K = I.gets.split.map(&:to_i)\nT.times do\n\tn, m = I.gets.split.map{|_| _.to_i - 1}.sort\n\tO.puts n % (K + 1) == K || (n + m - (K == 1 ? 0 : n / (K + 1))).odd? ? :+ : :-\nend\n\n__END__\nWA3: K=1\n\n"}], "negative_code": [{"source_code": "I = open('input.txt')\nO = open('output.txt', 'w')\nT, K = I.gets.split.map(&:to_i)\nT.times do\n\tn, m = I.gets.split.map{|_| _.to_i - 1}.sort\n\tO.puts n > 0 && n % K == 0 || (n + m).odd? ? :+ : :-\nend\n"}, {"source_code": "I = open('input.txt')\nO = open('output.txt', 'w')\nT, K = I.gets.split.map(&:to_i)\nT.times do\n\tn, m = I.gets.split.map{|_| _.to_i - 1}.sort\n\tO.puts n % (K + 1) == K || (n + m - n / (K + 1)).odd? ? :+ : :-\nend\n"}, {"source_code": "I = open('input.txt')\nO = open('output.txt', 'w')\nT, K = I.gets.split.map(&:to_i)\nT.times do\n\tn, m = I.gets.split.map{|_| _.to_i - 1}.sort\n\tO.puts n > 0 && n % K == 0 || (n + m - n / K).odd? ? :+ : :-\nend\n"}], "src_uid": "1f5f5ccbdfcbe5cb2d692475f0726bfd"} {"nl": {"description": "Monocarp had a tree which consisted of $$$n$$$ vertices and was rooted at vertex $$$1$$$. He decided to study BFS (Breadth-first search), so he ran BFS on his tree, starting from the root. BFS can be described by the following pseudocode:a = [] # the order in which vertices were processedq = Queue()q.put(1) # place the root at the end of the queuewhile not q.empty(): k = q.pop() # retrieve the first vertex from the queue a.append(k) # append k to the end of the sequence in which vertices were visited for y in g[k]: # g[k] is the list of all children of vertex k, sorted in ascending order q.put(y)Monocarp was fascinated by BFS so much that, in the end, he lost his tree. Fortunately, he still has a sequence of vertices, in which order vertices were visited by the BFS algorithm (the array a from the pseudocode). Monocarp knows that each vertex was visited exactly once (since they were put and taken from the queue exactly once). Also, he knows that all children of each vertex were viewed in ascending order.Monocarp knows that there are many trees (in the general case) with the same visiting order $$$a$$$, so he doesn't hope to restore his tree. Monocarp is okay with any tree that has minimum height.The height of a tree is the maximum depth of the tree's vertices, and the depth of a vertex is the number of edges in the path from the root to it. For example, the depth of vertex $$$1$$$ is $$$0$$$, since it's the root, and the depth of all root's children are $$$1$$$.Help Monocarp to find any tree with given visiting order $$$a$$$ and minimum height.", "input_spec": "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 a single integer $$$n$$$ ($$$2 \\le n \\le 2 \\cdot 10^5$$$) — the number of vertices in the tree. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le n$$$; $$$a_i \\neq a_j$$$; $$$a_1 = 1$$$) — the order in which the vertices were visited by the BFS algorithm. It's guaranteed that the total sum of $$$n$$$ over test cases doesn't exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case print the minimum possible height of a tree with the given visiting order $$$a$$$.", "sample_inputs": ["3\n4\n1 4 3 2\n2\n1 2\n3\n1 2 3"], "sample_outputs": ["3\n1\n1"], "notes": "NoteIn the first test case, there is only one tree with the given visiting order: In the second test case, there is only one tree with the given visiting order as well: In the third test case, an optimal tree with the given visiting order is shown below: "}, "positive_code": [{"source_code": "if ARGV[0]==\"LOCAL_DEV\"\n\tdefine_method(\"debug\") do |var|\n\t\tp var\n\tend\nelse\n\tdefine_method(\"debug\") do |var|\n\tend\nend\n\n\nQ=STDIN.gets.to_i\nQ.times do\n\tn=STDIN.gets.to_i\n\ta=STDIN.gets.strip.split.map(&:to_i)\n\tv=[]\n\tt=-1\n\tc=0\n\ta.each_with_index do |x,i|\n\t\tif i==0\n\t\t\tv<<1\n\t\t\tnext\n\t\tend\n\t\tif t<=x\n\t\t\tc+=1\n\t\t\tt=x\n\t\telse\n\t\t\tv< b ? a : b end\ndef mix(a, b) a < b ? a : b end\n\n\ndef check()\n\t\ns = gets.chomp\nt = gets.chomp\n\n\treturn false if s.length != t.length\n\tt1 = s.index('1')\n\tt2 = t.index('1')\n\t\n\treturn true if (t1 && t2) \n\treturn false if (t1 || t2) \n\treturn true\nend\n\nputs check() ? \"YES\" : \"NO\"\n\n\n"}, {"source_code": "a=gets\nb=gets\nputs (a==b ? \"YES\" : (a.size!=b.size or !a.include?(?1) or !b.include?(?1)) ? \"NO\" : \"YES\")"}, {"source_code": "a = gets.strip\nb = gets.strip\n\nanswer = 'YES'\n\na_l = a.length\nb_l = b.length\nanswer = 'NO' if a_l != b_l\n\nanswer = 'NO' if a_l == 1 && b_l == 1 && a != b\n\na_1 = a[/1/]\nb_1 = b[/1/]\n\nanswer = 'NO' if !a_1 && b_1\nanswer = 'NO' if a_1 && !b_1\n\nprint answer"}, {"source_code": "a = gets.strip\nb = gets.strip\n\nanswer = 'YES'\n\na_l = a.length\nb_l = b.length\nanswer = 'NO' if a_l != b_l\n\nanswer = 'NO' if a_l == 1 && b_l == 1 && a != b\n\na_1 = a.include?('1')\nb_1 = b.include?('1')\n\nanswer = 'NO' if !a_1 && b_1\nanswer = 'NO' if a_1 && !b_1\n\nprint answer"}, {"source_code": "a = gets.chomp\nb = gets.chomp\n\nif a.length != b.length\n\tputs \"NO\"\n\texit\nend\n\nif a.include?(\"1\")!=b.include?(\"1\")\n\tputs \"NO\"\n\texit\nend \n\nputs \"YES\""}], "negative_code": [{"source_code": "def max(a, b) a > b ? a : b end\ndef mix(a, b) a < b ? a : b end\n\n\ndef check()\n\t\ns = gets.chomp\nt = gets.chomp\n\n\t#return false if s.length != t.length\n\tt1 = s.index('1')\n\tt2 = t.index('1')\n\t\n\treturn true if (t1 && t2) \n\treturn false if (t1 || t2) \n\treturn true\nend\n\nputs check() ? \"YES\" : \"NO\"\n\n\n"}, {"source_code": "a = gets\nb = gets\n\nif a.length != b.length\n\tputs \"NO\"\n\texit\nend\n\nif a.include?(\"1\")!=b.include?(\"0\")\n\tputs \"NO\"\n\texit\nend \n\nputs \"YES\""}], "src_uid": "113ae625e67c8ea5ab07be44c3b58a8f"} {"nl": {"description": "Every superhero has been given a power value by the Felicity Committee. The avengers crew wants to maximize the average power of the superheroes in their team by performing certain operations.Initially, there are $$$n$$$ superheroes in avengers team having powers $$$a_1, a_2, \\ldots, a_n$$$, respectively. In one operation, they can remove one superhero from their team (if there are at least two) or they can increase the power of a superhero by $$$1$$$. They can do at most $$$m$$$ operations. Also, on a particular superhero at most $$$k$$$ operations can be done.Can you help the avengers team to maximize the average power of their crew?", "input_spec": "The first line contains three integers $$$n$$$, $$$k$$$ and $$$m$$$ ($$$1 \\le n \\le 10^{5}$$$, $$$1 \\le k \\le 10^{5}$$$, $$$1 \\le m \\le 10^{7}$$$) — the number of superheroes, the maximum number of times you can increase power of a particular superhero, and the total maximum number of operations. The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le 10^{6}$$$) — the initial powers of the superheroes in the cast of avengers.", "output_spec": "Output a single number — the maximum final average power. Your answer is considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is accepted if and only if $$$\\frac{|a - b|}{\\max{(1, |b|)}} \\le 10^{-6}$$$.", "sample_inputs": ["2 4 6\n4 7", "4 2 6\n1 3 2 3"], "sample_outputs": ["11.00000000000000000000", "5.00000000000000000000"], "notes": "NoteIn the first example, the maximum average is obtained by deleting the first element and increasing the second element four times.In the second sample, one of the ways to achieve maximum average is to delete the first and the third element and increase the second and the fourth elements by $$$2$$$ each."}, "positive_code": [{"source_code": "n,k,m = gets.split(\" \").map(&:to_i)\narr = gets.split(\" \").map(&:to_i).sort\ns=arr.inject(:+)\navg = (s+[m,n*k].min).to_f/n\nfor i in 0..[n-1,m].min-1\n s = s - arr[i]\n na = (s+[m-i-1,(n-i-1)*k].min).to_f/(n-i-1)\n avg = [avg,na].max\nend\navg = \"#{'%.10f'%avg}\"\nputs avg"}], "negative_code": [], "src_uid": "d6e44bd8ac03876cb03be0731f7dda3d"} {"nl": {"description": "In Absurdistan, there are n towns (numbered 1 through n) and m bidirectional railways. There is also an absurdly simple road network — for each pair of different towns x and y, there is a bidirectional road between towns x and y if and only if there is no railway between them. Travelling to a different town using one railway or one road always takes exactly one hour.A train and a bus leave town 1 at the same time. They both have the same destination, town n, and don't make any stops on the way (but they can wait in town n). The train can move only along railways and the bus can move only along roads.You've been asked to plan out routes for the vehicles; each route can use any road/railway multiple times. One of the most important aspects to consider is safety — in order to avoid accidents at railway crossings, the train and the bus must not arrive at the same town (except town n) simultaneously.Under these constraints, what is the minimum number of hours needed for both vehicles to reach town n (the maximum of arrival times of the bus and the train)? Note, that bus and train are not required to arrive to the town n at the same moment of time, but are allowed to do so.", "input_spec": "The first line of the input contains two integers n and m (2 ≤ n ≤ 400, 0 ≤ m ≤ n(n - 1) / 2) — the number of towns and the number of railways respectively. Each of the next m lines contains two integers u and v, denoting a railway between towns u and v (1 ≤ u, v ≤ n, u ≠ v). You may assume that there is at most one railway connecting any two towns.", "output_spec": "Output one integer — the smallest possible time of the later vehicle's arrival in town n. If it's impossible for at least one of the vehicles to reach town n, output  - 1.", "sample_inputs": ["4 2\n1 3\n3 4", "4 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4", "5 5\n4 2\n3 5\n4 5\n5 1\n1 2"], "sample_outputs": ["2", "-1", "3"], "notes": "NoteIn the first sample, the train can take the route and the bus can take the route . Note that they can arrive at town 4 at the same time.In the second sample, Absurdistan is ruled by railwaymen. There are no roads, so there's no way for the bus to reach town 4."}, "positive_code": [{"source_code": "#! ruby\n# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n\ndef gi(); gets.to_i; end\ndef gis(); gets.chomp.split.map(&:to_i); end\n\nn, m = gis\n\nh = []\n1.upto(n) do |i|\n h[i] = [-1] * n.succ\n h[i][i] = 0\nend\n\nm.times do\n u, v = gis\n h[u][v] = h[v][u] = 1\nend\n\nt1 = []\nt2 = []\n1.upto(n) do |i|\n t1[i] = []\n t2[i] = []\n 1.upto(n) do |j|\n case h[i][j]\n when 1\n t1[i] << j\n when -1\n t2[i] << j\n end\n end\nend\n\nif t1[1].empty? || t2[1].empty? || t1[n].empty? || t2[n].empty?\n puts '-1'\n exit\nend\n\nl1 = [1]\nv1 = [true] + ([false] * n)\nflg = true\nct = 0\nwhile flg && !l1.empty? do\n ct += 1\n l2 = []\n l1.each do |k|\n if t1[k][-1] == n\n flg = false\n break\n end\n t1[k].each do |x|\n next if v1[x]\n v1[x] = true\n l2 << x\n end\n end\n l1 = l2\nend \n\nif flg\n puts '-1'\n exit\nend\n\n\nl1 = [1]\nv1 = [true] + ([false] * n)\nflg = true\ncb = 0\nwhile flg && !l1.empty? do\n cb += 1\n l2 = []\n l1.each do |k|\n if t2[k][-1] == n\n flg = false\n break\n end\n t2[k].each do |x|\n next if v1[x]\n v1[x] = true\n l2 << x\n end\n end\n l1 = l2\nend \n\nif flg\n puts '-1'\n exit\nend\n\nputs [ct,cb].max\n\n"}, {"source_code": "#! ruby\n# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n\ndef gi(); gets.to_i; end\ndef gis(); gets.chomp.split.map(&:to_i); end\n\nn, m = gis\n\nh = []\n1.upto(n) do |i|\n h[i] = [-1] * n.succ\n h[i][i] = 0\nend\n\nm.times do\n u, v = gis\n h[u][v] = h[v][u] = 1\nend\n\nt1 = []\nt2 = []\n1.upto(n) do |i|\n t1[i] = []\n t2[i] = []\n 1.upto(n) do |j|\n case h[i][j]\n when 1\n t1[i] << j\n when -1\n t2[i] << j\n end\n end\nend\n\nif t1[1].empty? || t2[1].empty? || t1[n].empty? || t2[n].empty?\n puts '-1'\n exit\nend\n\ndef fnc(t, n)\n l1 = [1]\n v1 = [true] + ([false] * n)\n c = 0\n while !l1.empty? do\n c += 1\n l2 = []\n l1.each do |k|\n if t[k][-1] == n\n return c\n end\n t[k].each do |x|\n next if v1[x]\n v1[x] = true\n l2 << x\n end\n end\n l1 = l2\n end\n return -1\nend\n\nct = fnc t1, n\nif ct < 0\n puts '-1'\n exit\nend\n\ncb = fnc t2, n\nif cb < 0\n puts '-1'\n exit\nend\n\nputs [ct,cb].max\n\n"}], "negative_code": [{"source_code": "#! ruby\n# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n\ndef gi(); gets.to_i; end\ndef gis(); gets.chomp.split.map(&:to_i); end\n\n$stdout = STDERR\n\nn, m = gis\n\nh = {}\n\nm.times do\n u, v = gis\n \n h[u] = [false] * (n + 1) if !h.key? u\n h[u][v] = true\n \n h[v] = [false] * (n + 1) if !h.key? v\n h[v][u] = true\n \nend\n\nl1 = []\nl2 = []\n\n1.upto(n - 1) do |i|\n if h[n][i]\n l1 << i\n else\n l2 << i\n end\nend\n\n\np h, \"-----\"\np l1, l2, \"-----\"\n\n\nif l1.empty? || l2.empty?\n STDOUT.puts '-1'\n exit\nend\n\nif !(l1 & l2).empty?\n STDOUT.puts 1\n exit\nend\n\nc = 1\n\nloop {\n c += 1\n \n l1t = {}\n l1.each do |k|\n if !h.key? k\n next\n end\n 1.upto(n - 1) do |i|\n next if i == k\n if h[k][i] \n l1t[i] = 1\n end\n end\n end\n l1 = l1t.keys\n \n l2t = {}\n l2.each do |k|\n 1.upto(n - 1) do |i|\n next if i == k\n if (!h.key?(k)) || !h[k][i]\n l2t[i] = 1\n end\n end\n end\n l2 = l2t.keys\n \n p l1, l2, \"-----\"\n\n if l1.empty? || l2.empty?\n STDOUT.puts '-1'\n exit\n end\n \n if !(l1 & l2).empty?\n STDOUT.puts c\n exit\n end\n}"}, {"source_code": "#! ruby\n# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n\ndef gi(); gets.to_i; end\ndef gis(); gets.chomp.split.map(&:to_i); end\n\n$stdout = STDERR\n\nn, m = gis\n\nh = {}\n\nm.times do\n u, v = gis\n \n h[u] = [false] * (n + 1) if !h.key?(u)\n h[u][v] = true\n \n h[v] = [false] * (n + 1) if !h.key?(v)\n h[v][u] = true\n \nend\n\nif !h.key?(n)\n STDOUT.puts '-1'\n exit\nend\n\nl1 = []\nl2 = []\n\n1.upto(n - 1) do |i|\n if h[n][i]\n l1 << i\n else\n l2 << i\n end\nend\n\n\n#p h, \"-----\"\n#p l1, l2, \"-----\"\n\n\nif l1.empty? || l2.empty?\n STDOUT.puts '-1'\n exit\nend\n\nif !((l1 & l2) & [1]).empty?\n STDOUT.puts '1'\n exit\nend\n\nc = 1\n\nmp = {}\n\nloop {\n c += 1\n\n l1t = {}\n l1.each do |k|\n if !h.key? k\n next\n end\n 1.upto(n - 1) do |i|\n next if i == k\n if h[k][i] \n l1t[i] = 1\n end\n end\n end\n l1 = l1t.keys\n \n l2t = {}\n l2.each do |k|\n 1.upto(n - 1) do |i|\n next if i == k\n if (!h.key?(k)) || (!(h[k][i]))\n l2t[i] = 1\n end\n end\n end\n l2 = l2t.keys\n\n s = l1.sort * \" \" + \",\" + l2.sort * \" \"\n if mp.key? s\n puts '-1'\n exit\n end\n mp[s] = 1 \n\n #p l1, l2, \"-----\"\n\n if l1.empty? || l2.empty?\n STDOUT.puts '-1'\n exit\n end\n \n if l1t.key?(1) && l2t.key?(1)\n STDOUT.puts c\n exit\n end\n}"}, {"source_code": "#! ruby\n# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n\ndef gi(); gets.to_i; end\ndef gis(); gets.chomp.split.map(&:to_i); end\n\n$stdout = STDERR\n\nn, m = gis\n\nh = {}\n\nm.times do\n u, v = gis\n \n h[u] = [false] * (n + 1) if !h.key?(u)\n h[u][v] = true\n \n #h[v] = [false] * (n + 1) if !h.key?(v)\n #h[v][u] = true\n \nend\n\nif !h.key?(n)\n STDOUT.puts '-1'\n exit\nend\n\nl1 = []\nl2 = []\n\n1.upto(n) do |i|\n if h[n][i]\n l1 << i\n else\n l2 << i\n end\nend\n\n\n#p h, \"-----\"\n#p l1, l2, \"-----\"\n\n\nif l1.empty? || l2.empty?\n STDOUT.puts '-1'\n exit\nend\n\nif !((l1 & l2) & [1]).empty?\n STDOUT.puts '1'\n exit\nend\n\nc = 1\n\nmp = {}\n\nloop {\n c += 1\n\n l1t = {}\n l1.each do |k|\n if !h.key? k\n next\n end\n 1.upto(n) do |i|\n next if i == k\n if h[k][i] \n l1t[i] = 1\n end\n end\n end\n l1 = l1t.keys\n \n l2t = {}\n l2.each do |k|\n 1.upto(n) do |i|\n next if i == k\n if (!h.key?(k)) || (!(h[k][i]))\n l2t[i] = 1\n end\n end\n end\n l2 = l2t.keys\n\n s = (l1.sort * \" \") + \",\" + (l2.sort * \" \")\n if mp.key? s\n puts '-1'\n exit\n end\n mp[s] = 1 \n\n #p l1, l2, \"-----\"\n\n if l1.empty? || l2.empty?\n STDOUT.puts '-1'\n exit\n end\n \n if l1t.key?(1) && l2t.key?(1)\n STDOUT.puts c\n exit\n end\n}"}, {"source_code": "#! ruby\n# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n\ndef gi(); gets.to_i; end\ndef gis(); gets.chomp.split.map(&:to_i); end\n\n$stdout = STDERR\n\nn, m = gis\n\nh = {}\n\nm.times do\n u, v = gis\n \n h[u] = [false] * (n + 1) if !h.key? u\n h[u][v] = true\n \n h[v] = [false] * (n + 1) if !h.key? v\n h[v][u] = true\n \nend\n\nl1 = []\nl2 = []\n\n1.upto(n - 1) do |i|\n if h[n][i]\n l1 << i\n else\n l2 << i\n end\nend\n\n\np h, \"-----\"\np l1, l2, \"-----\"\n\n\nif l1.empty? || l2.empty?\n STDOUT.puts '-1'\n exit\nend\n\nif !(l1 & l2).empty?\n STDOUT.puts 1\n exit\nend\n\nc = 1\n\nloop {\n c += 1\n \n l1t = {}\n l1.each do |k|\n if !h.key? k\n next\n end\n 1.upto(n) do |i|\n next if i == k\n if h[k][i] \n l1t[i] = 1\n end\n end\n end\n l1 = l1t.keys\n \n l2t = {}\n l2.each do |k|\n 1.upto(n) do |i|\n next if i == k\n if (!h.key?(k)) || !h[k][i]\n l2t[i] = 1\n end\n end\n end\n l2 = l2t.keys\n \n p l1, l2, \"-----\"\n\n if l1.empty? || l2.empty?\n STDOUT.puts '-1'\n exit\n end\n \n if !((l1 & l2) - [n]).empty?\n STDOUT.puts c\n exit\n end\n}"}, {"source_code": "#! ruby\n# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n\ndef gi(); gets.to_i; end\ndef gis(); gets.chomp.split.map(&:to_i); end\n\n$stdout = STDERR\n\nn, m = gis\n\nh = {}\n\nm.times do\n u, v = gis\n \n h[u] = [false] * (n + 1) if !h.key?(u)\n h[u][v] = true\n \n h[v] = [false] * (n + 1) if !h.key?(v)\n h[v][u] = true\n \nend\n\nif !h.key?(n)\n STDOUT.puts '-1'\n exit\nend\n\nl1 = []\nl2 = []\n\n1.upto(n - 1) do |i|\n if h[n][i]\n l1 << i\n else\n l2 << i\n end\nend\n\n\n#p h, \"-----\"\n#p l1, l2, \"-----\"\n\n\nif l1.empty? || l2.empty?\n STDOUT.puts '-1'\n exit\nend\n\nif !((l1 & l2) & [1]).empty?\n STDOUT.puts '1'\n exit\nend\n\nc = 1\n\nmp = {}\n\nloop {\n c += 1\n\n l1t = {}\n l1.each do |k|\n if !h.key? k\n next\n end\n 1.upto(n) do |i|\n next if i == k\n if h[k][i] \n l1t[i] = 1\n end\n end\n end\n l1 = l1t.keys\n \n l2t = {}\n l2.each do |k|\n 1.upto(n) do |i|\n next if i == k\n if (!h.key?(k)) || (!(h[k][i]))\n l2t[i] = 1\n end\n end\n end\n l2 = l2t.keys\n\n s = l1.sort * \" \" + \",\" + l2.sort * \" \"\n if mp.key? s\n puts '-1'\n exit\n end\n mp[s] = 1 \n\n #p l1, l2, \"-----\"\n\n if l1.empty? || l2.empty?\n STDOUT.puts '-1'\n exit\n end\n \n if l1t.key?(1) && l2t.key?(1)\n STDOUT.puts c\n exit\n end\n}"}, {"source_code": "#! ruby\n# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n\ndef gi(); gets.to_i; end\ndef gis(); gets.chomp.split.map(&:to_i); end\n\n$stdout = STDERR\n\nn, m = gis\n\nh = {}\n\nm.times do\n u, v = gis\n \n #h[u] = [false] * (n + 1) if !h.key?(u)\n #h[u][v] = true\n \n h[v] = [false] * (n + 1) if !h.key?(v)\n h[v][u] = true\n \nend\n\nif !h.key?(n)\n STDOUT.puts '-1'\n exit\nend\n\nl1 = []\nl2 = []\n\n1.upto(n) do |i|\n if h[n][i]\n l1 << i\n else\n l2 << i\n end\nend\n\n\n#p h, \"-----\"\n#p l1, l2, \"-----\"\n\n\nif l1.empty? || l2.empty?\n STDOUT.puts '-1'\n exit\nend\n\nif !((l1 & l2) & [1]).empty?\n STDOUT.puts '1'\n exit\nend\n\nc = 1\n\nmp = {}\n\nloop {\n c += 1\n\n l1t = {}\n l1.each do |k|\n if !h.key? k\n next\n end\n 1.upto(n) do |i|\n next if i == k\n if h[k][i] \n l1t[i] = 1\n end\n end\n end\n l1 = l1t.keys\n \n l2t = {}\n l2.each do |k|\n 1.upto(n) do |i|\n next if i == k\n if (!h.key?(k)) || (!(h[k][i]))\n l2t[i] = 1\n end\n end\n end\n l2 = l2t.keys\n\n s = (l1.sort * \" \") + \",\" + (l2.sort * \" \")\n if mp.key? s\n puts '-1'\n exit\n end\n mp[s] = 1 \n\n #p l1, l2, \"-----\"\n\n if l1.empty? || l2.empty?\n STDOUT.puts '-1'\n exit\n end\n \n if l1t.key?(1) && l2t.key?(1)\n STDOUT.puts c\n exit\n end\n}"}, {"source_code": "#! ruby\n# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n\ndef gi(); gets.to_i; end\ndef gis(); gets.chomp.split.map(&:to_i); end\n\n$stdout = STDERR\n\nn, m = gis\n\nh = {}\n\nm.times do\n u, v = gis\n \n h[u] = [false] * (n + 1) if !h.key?(u)\n h[u][v] = true\n \n h[v] = [false] * (n + 1) if !h.key?(v)\n h[v][u] = true\n \nend\n\nif !h.key?(n)\n STDOUT.puts '-1'\n exit\nend\n\nl1 = []\nl2 = []\n\n1.upto(n) do |i|\n if h[n][i]\n l1 << i\n else\n l2 << i\n end\nend\n\n\n#p h, \"-----\"\n#p l1, l2, \"-----\"\n\n\nif l1.empty? || l2.empty?\n STDOUT.puts '-1'\n exit\nend\n\nif !((l1 & l2) & [1]).empty?\n STDOUT.puts '1'\n exit\nend\n\nc = 1\n\nmp = {}\n\nloop {\n c += 1\n\n l1t = {}\n l1.each do |k|\n if !h.key? k\n next\n end\n 1.upto(n) do |i|\n next if i == k\n if h[k][i] \n l1t[i] = 1\n end\n end\n end\n l1 = l1t.keys\n \n l2t = {}\n l2.each do |k|\n 1.upto(n) do |i|\n next if i == k\n if (!h.key?(k)) || (!(h[k][i]))\n l2t[i] = 1\n end\n end\n end\n l2 = l2t.keys\n\n s = (l1.sort * \" \") + \",\" + (l2.sort * \" \")\n if mp.key? s\n puts '-1'\n exit\n end\n mp[s] = 1 \n\n #p l1, l2, \"-----\"\n\n if l1.empty? || l2.empty?\n STDOUT.puts '-1'\n exit\n end\n \n if l1t.key?(1) && l2t.key?(1)\n STDOUT.puts c\n exit\n end\n}"}], "src_uid": "fbfc333ad4b0a750f654a00be84aea67"} {"nl": {"description": "In a certain video game, the player controls a hero characterized by a single integer value: power. The hero will have to beat monsters that are also characterized by a single integer value: armor.On the current level, the hero is facing $$$n$$$ caves. To pass the level, the hero must enter all the caves in some order, each cave exactly once, and exit every cave safe and sound. When the hero enters cave $$$i$$$, he will have to fight $$$k_i$$$ monsters in a row: first a monster with armor $$$a_{i, 1}$$$, then a monster with armor $$$a_{i, 2}$$$ and so on, finally, a monster with armor $$$a_{i, k_i}$$$.The hero can beat a monster if and only if the hero's power is strictly greater than the monster's armor. If the hero can't beat the monster he's fighting, the game ends and the player loses. Note that once the hero enters a cave, he can't exit it before he fights all the monsters in it, strictly in the given order.Each time the hero beats a monster, the hero's power increases by $$$1$$$.Find the smallest possible power the hero must start the level with to be able to enter all the caves in some order and beat all the monsters.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 10^5$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$) — the number of caves. The $$$i$$$-th of the next $$$n$$$ lines contains an integer $$$k_i$$$ ($$$1 \\le k_i \\le 10^5$$$) — the number of monsters in the $$$i$$$-th cave, followed by $$$k_i$$$ integers $$$a_{i, 1}, a_{i, 2}, \\ldots, a_{i, k_i}$$$ ($$$1 \\le a_{i, j} \\le 10^9$$$) — armor levels of the monsters in cave $$$i$$$ in order the hero has to fight them. It is guaranteed that the sum of $$$k_i$$$ over all test cases does not exceed $$$10^5$$$.", "output_spec": "For each test case print a single integer — the smallest possible power the hero must start the level with to be able to enter all the caves in some order and beat all the monsters.", "sample_inputs": ["2\n1\n1 42\n2\n3 10 15 8\n2 12 11"], "sample_outputs": ["43\n13"], "notes": "NoteIn the first test case, the hero has to beat a single monster with armor $$$42$$$, it's enough to have power $$$43$$$ to achieve that.In the second test case, the hero can pass the level with initial power $$$13$$$ as follows: enter cave $$$2$$$: beat a monster with armor $$$12$$$, power increases to $$$14$$$; beat a monster with armor $$$11$$$, power increases to $$$15$$$; enter cave $$$1$$$: beat a monster with armor $$$10$$$, power increases to $$$16$$$; beat a monster with armor $$$15$$$, power increases to $$$17$$$; beat a monster with armor $$$8$$$, power increases to $$$18$$$. "}, "positive_code": [{"source_code": "class Cave\n attr_accessor :count\n attr_accessor :max\n def initialize()\n a = gets.split.map {|i| i.to_i}\n a = a[1...a.length]\n a = a.map.with_index do |item, idx|\n item - idx\n end\n @count = a.length\n @max = a.max\n end\n def <=>(other)\n if other.is_a?(Cave) == false\n return nil\n end\n if max < other.max\n return -1\n elsif max > other.max\n return 1\n else\n return other.count <=> count\n end\n end\nend\ndef solve\n n = gets.to_i\n a = (1..n).map { |xx| Cave.new }\n a.sort!\n #p a\n\n res, power, pcount = 0, 0, 0\n for i in a\n if i.max > power\n power = i.max\n res = i.max + 1 - pcount\n end\n power += i.count\n pcount += i.count\n end\n puts res\n #puts \"res: #{res}\"\nend\nt = gets.to_i\nwhile t > 0\n t -= 1\n solve\nend\n"}, {"source_code": "t = gets.to_i\nt.times do\n\nn = gets.to_i\ncaves = Array.new(n) do\n gets.split.map!(&:to_i).drop(1)\nend\n\ndata = caves.map do |a|\n k = a.size\n req = a.map.with_index { |ai, i| ai - i + 1 }.max\n [req, -k]\nend.sort!\n\npw = 0\ndiff = 0\ndata.each do |req, kk|\n if pw < req\n diff += req - pw\n pw = req\n end\n pw += -kk\nend\n\np diff\n\nend\n"}], "negative_code": [{"source_code": "class Cave\n attr_accessor :count\n attr_accessor :max\n def initialize()\n a = gets.split.map {|i| i.to_i}\n a = a[1...a.length]\n a = a.map.with_index do |item, idx|\n item - idx\n end\n @count = a.length\n @max = a.max\n end\n def <=>(other)\n if other.is_a?(Cave) == false\n return nil\n end\n if max < other.max\n return -1\n elsif max > other.max\n return 1\n else\n return other.count <=> count\n end\n end\nend\ndef solve\n n = gets.to_i\n a = (1..n).map { |xx| Cave.new }\n a.sort!\n p a\n\n res, power, pcount = 0, 0, 0\n for i in a\n if i.max > power\n power = i.max\n res = i.max + 1 - pcount\n end\n power += i.count\n pcount += i.count\n end\n puts \"res: #{res}\"\nend\nt = gets.to_i\nwhile t > 0\n t -= 1\n solve\nend\n"}], "src_uid": "88488ff074bc25a6cf925c8a07a1d8c6"} {"nl": {"description": "You are given a rooted tree consisting of $$$n$$$ vertices numbered from $$$1$$$ to $$$n$$$. The root is vertex $$$1$$$. There is also a string $$$s$$$ denoting the color of each vertex: if $$$s_i = \\texttt{B}$$$, then vertex $$$i$$$ is black, and if $$$s_i = \\texttt{W}$$$, then vertex $$$i$$$ is white.A subtree of the tree is called balanced if the number of white vertices equals the number of black vertices. Count the number of balanced subtrees.A tree is a connected undirected graph without cycles. A rooted tree is a tree with a selected vertex, which is called the root. In this problem, all trees have root $$$1$$$.The tree is specified by an array of parents $$$a_2, \\dots, a_n$$$ containing $$$n-1$$$ numbers: $$$a_i$$$ is the parent of the vertex with the number $$$i$$$ for all $$$i = 2, \\dots, n$$$. The parent of a vertex $$$u$$$ is a vertex that is the next vertex on a simple path from $$$u$$$ to the root.The subtree of a vertex $$$u$$$ is the set of all vertices that pass through $$$u$$$ on a simple path to the root. For example, in the picture below, $$$7$$$ is in the subtree of $$$3$$$ because the simple path $$$7 \\to 5 \\to 3 \\to 1$$$ passes through $$$3$$$. Note that a vertex is included in its subtree, and the subtree of the root is the entire tree. The picture shows the tree for $$$n=7$$$, $$$a=[1,1,2,3,3,5]$$$, and $$$s=\\texttt{WBBWWBW}$$$. The subtree at the vertex $$$3$$$ is balanced. ", "input_spec": "The first line of input contains an integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$2 \\le n \\le 4000$$$) — the number of vertices in the tree. The second line of each test case contains $$$n-1$$$ integers $$$a_2, \\dots, a_n$$$ ($$$1 \\le a_i < i$$$) — the parents of the vertices $$$2, \\dots, n$$$. The third line of each test case contains a string $$$s$$$ of length $$$n$$$ consisting of the characters $$$\\texttt{B}$$$ and $$$\\texttt{W}$$$ — the coloring of the tree. It is guaranteed that the sum of the values $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, output a single integer — the number of balanced subtrees.", "sample_inputs": ["3\n\n7\n\n1 1 2 3 3 5\n\nWBBWWBW\n\n2\n\n1\n\nBW\n\n8\n\n1 2 3 4 5 6 7\n\nBWBWBWBW"], "sample_outputs": ["2\n1\n4"], "notes": "NoteThe first test case is pictured in the statement. Only the subtrees at vertices $$$2$$$ and $$$3$$$ are balanced.In the second test case, only the subtree at vertex $$$1$$$ is balanced.In the third test case, only the subtrees at vertices $$$1$$$, $$$3$$$, $$$5$$$, and $$$7$$$ are balanced."}, "positive_code": [{"source_code": "Node = Struct.new(:id, :balance, :parent)\n\ngets.to_i.times do\n n = gets.to_i\n nodes = Array.new(n) { |i| Node.new(i, nil, nil) }\n gets.split.map(&:to_i).each_with_index do |p, i|\n nodes[i + 1].parent = nodes[p - 1]\n end\n gets.strip.chars.each_with_index do |c, i|\n nodes[i].balance = (c == 'W') ? 1 : -1\n end\n\n nodes.reverse.each do |node|\n next unless node.parent\n node.parent.balance += node.balance\n end\n\n p nodes.count { _1.balance == 0 }\nend\n"}], "negative_code": [], "src_uid": "504613b285d10fbf1e45b9c4ace25865"} {"nl": {"description": "You're given an array $$$a$$$. You should repeat the following operation $$$k$$$ times: find the minimum non-zero element in the array, print it, and then subtract it from all the non-zero elements of the array. If all the elements are 0s, just print 0.", "input_spec": "The first line contains integers $$$n$$$ and $$$k$$$ $$$(1 \\le n,k \\le 10^5)$$$, the length of the array and the number of operations you should perform. The second line contains $$$n$$$ space-separated integers $$$a_1, a_2, \\ldots, a_n$$$ $$$(1 \\le a_i \\le 10^9)$$$, the elements of the array.", "output_spec": "Print the minimum non-zero element before each operation in a new line.", "sample_inputs": ["3 5\n1 2 3", "4 2\n10 3 5 3"], "sample_outputs": ["1\n1\n1\n0\n0", "3\n2"], "notes": "NoteIn the first sample:In the first step: the array is $$$[1,2,3]$$$, so the minimum non-zero element is 1.In the second step: the array is $$$[0,1,2]$$$, so the minimum non-zero element is 1.In the third step: the array is $$$[0,0,1]$$$, so the minimum non-zero element is 1.In the fourth and fifth step: the array is $$$[0,0,0]$$$, so we printed 0.In the second sample:In the first step: the array is $$$[10,3,5,3]$$$, so the minimum non-zero element is 3.In the second step: the array is $$$[7,0,2,0]$$$, so the minimum non-zero element is 2."}, "positive_code": [{"source_code": "n,k = gets.strip.split.map(&:to_i)\nary = gets.strip.split.map(&:to_i)\nary.sort!\n\nsub=0\nidx=0\nk.times.each do |i|\n while idx= n\n v = 0\n else\n v = (ary[idx]-sub)\n sub += v\n end\n puts v\nend"}, {"source_code": "n,k = gets.split.map(&:to_i)\na = gets.split.map(&:to_i).sort\ni = 0\nsum = 0\nk.times do |j|\n while i < n && a[i]-sum <= 0\n i += 1\n #break if i == n\n end\n if i == n\n puts 0\n else\n puts a[i]-sum\n sum = a[i]\n end\nend"}, {"source_code": "n, k = gets.chomp.split(\" \").map(&:to_i)\narr = gets.chomp.split(\" \").map(&:to_i)\n\narr.sort!\nindex = 0\nsbst = 0\nk.times do\n if index >= n\n puts 0\n else\n min = arr[index] - sbst\n puts min\n sbst += min\n for i in index..(n - 1) do\n if arr[i] - sbst > 0 \n index = i\n break\n else i == n - 1\n index = n\n end\n end\n end\nend\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\na.sort!\nj = a.index{|x| x > 0}\nif j == nil\n j = a.size\nend\npj = 0\nk.times do\n if j == a.size\n puts 0\n next\n end\n puts(a[j] - pj)\n pj += (a[j] - pj)\n \n i = j + 1\n i += 1 while i < a.size && a[i] == a[j]\n j = i\nend\n"}, {"source_code": "nk = gets.chomp.split(\" \").map(&:to_i)\narr = gets.chomp.split(\" \").map(&:to_i)\nn = nk[0]\nk = nk[1]\ntosubtr = 0\n\narr.sort!\n#p arr\n\ni = 0\nk.times do\n\n if i >= n\n puts 0\n else\n x = arr[i]\n\n while ((x-tosubtr)==0 and i= n\n puts 0\n else\n x = arr[i]\n\n while ((x-tosubtr)==0 and iH['USB'].size\nr+=a\ns+=H['USB'].shift(a).reduce(0,:+)\nH['PS/2'].sort!\nb=H['PS/2'].size if b>H['PS/2'].size\nr+=b\ns+=H['PS/2'].shift(b).reduce(0,:+)\nq=(H['USB']+H['PS/2']).sort\nc=q.size if c>q.size\nr+=c\ns+=q.shift(c).reduce(0,:+)\nputs '%d %d'%[r,s]"}, {"source_code": "#!/usr/bin/env ruby\n\nusb, ps2, usb_ps2 = gets.split.map {|v| v.to_i}\nmouses_n = gets.to_i\n\nmouses_prices = { USB: [],\n :'PS/2' => []}\n\nmouses_n.times do |n|\n price, type = gets.split\n mouses_prices[type.to_sym] << price.to_i\nend\n\nmouses_prices[:USB].sort!\nmouses_prices[:'PS/2'].sort!\n\ntotal_mouse = 0\ntotal_price = 0\n\nusb.times do\n break if mouses_prices[:USB].empty?\n total_price += mouses_prices[:USB].shift\n total_mouse += 1\nend\n\nps2.times do\n break if mouses_prices[:'PS/2'].empty?\n total_price += mouses_prices[:'PS/2'].shift\n total_mouse += 1\nend\n\nusb_ps2_remainings = mouses_prices[:USB] + mouses_prices[:'PS/2']\nusb_ps2_remainings.sort!\n\nusb_ps2.times do\n break if usb_ps2_remainings.empty?\n total_price += usb_ps2_remainings.shift\n total_mouse += 1\nend\n\nputs \"#{total_mouse} #{total_price}\"\n"}], "negative_code": [{"source_code": "#!/usr/bin/env ruby\n\nusb, ps2, usb_ps2 = gets.split.map {|v| v.to_i}\nmouses_n = gets.to_i\n\nmouses_prices = { USB: [],\n :'PS/2' => []}\n\nmouses_n.times do |n|\n price, type = gets.split\n mouses_prices[type.to_sym] << price.to_i\nend\n\nmouses_prices[:USB].sort!\nmouses_prices[:'PS/2'].sort!\n\ntotal_mouse = 0\ntotal_price = 0\n\nusb.times do\n break if mouses_prices[:USB].empty?\n total_price += mouses_prices[:USB].shift\n total_mouse += 1\nend\n\nps2.times do\n break if mouses_prices[:'PS/2'].empty?\n total_price += mouses_prices[:'PS/2'].shift\n total_mouse += 1\nend\n\nusb_ps2.times do\n if !mouses_prices[:USB].empty?\n total_price += mouses_prices[:USB].shift\n total_mouse += 1\n next\n end\n if !mouses_prices[:'PS/2'].empty?\n total_price += mouses_prices[:'PS/2'].shift\n total_mouse += 1\n next\n end\n break\nend\n\nputs \"#{total_mouse} #{total_price}\"\n"}], "src_uid": "3d6151b549bd52f95ab7fdb972e6fb98"} {"nl": {"description": "Polycarp plays a well-known computer game (we won't mention its name). In this game, he can craft tools of two types — shovels and swords. To craft a shovel, Polycarp spends two sticks and one diamond; to craft a sword, Polycarp spends two diamonds and one stick.Each tool can be sold for exactly one emerald. How many emeralds can Polycarp earn, if he has $$$a$$$ sticks and $$$b$$$ diamonds?", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. The only line of each test case contains two integers $$$a$$$ and $$$b$$$ ($$$0 \\le a, b \\le 10^9$$$) — the number of sticks and the number of diamonds, respectively.", "output_spec": "For each test case print one integer — the maximum number of emeralds Polycarp can earn.", "sample_inputs": ["4\n4 4\n1000000000 0\n7 15\n8 7"], "sample_outputs": ["2\n0\n7\n5"], "notes": "NoteIn the first test case Polycarp can earn two emeralds as follows: craft one sword and one shovel.In the second test case Polycarp does not have any diamonds, so he cannot craft anything."}, "positive_code": [{"source_code": "def solve(a, b)\n min = [a, b].min\n max = [a, b].max\n [(max + min) /3, min].min\nend\n\namount = gets.chomp.to_i\namount.times do\n input = gets.chomp.split(\" \")\n puts solve(input[0].to_i, input[1].to_i)\nend\n"}, {"source_code": "t = gets.chomp.to_i\n\nresults = []\n\n(1..t).each do |_t|\n\n a = gets.chomp.split(' ').map{|a| a.to_i}\n \n a.sort!.reverse!\n\n a,b = a\n\n if a > 2*b\n results[_t] = b\n else\n results[_t] = (a + b) /3\n end \n\nend\n\nresults.each do |res|\n puts res\nend\n"}], "negative_code": [{"source_code": "t = gets.chomp.to_i\n\nresults = []\n\n(1..t).each do |_t|\n\n a = gets.chomp.split(' ').map{|a| a.to_i}\n \n a.sort!.reverse!\n\n if a[0] % 2 == 0\n if a[1] - (a[0]/2 - 1) >= 4\n results[_t] = a[0] / 2 + 1\n else \n results[_t] = [a[0]/2, a[1]].min\n end\n else\n x = a[1] - a[0] / 2\n\n if x > 1\n results[_t] =a[0]/2 + 1\n else\n results[_t] = [a[0]/2, a[1]].min\n end\n end\n\nend\n\nresults.each do |res|\n puts res\nend\n"}, {"source_code": "def compute(a,b,x)\n return b if b <= x\n\n a -= 2*x\n b -= x\n \n return x if [a,b].max < 2\n\n return x if [a,b].min < 1\n\n return x + 1 if [a,b].max < 4\n\n return x + 1 if [a,b].min < 2 \n\n x + 2\nend\n\nt = gets.chomp.to_i\n\nresults = []\n\n(1..t).each do |_t|\n\n a = gets.chomp.split(' ').map{|a| a.to_i}\n \n a.sort!.reverse!\n\n x = a[0] / 2\n\n results[_t] = [compute(a[0], a[1], x), compute(a[0], a[1], x-1)].max\n\nend\n\nresults.each do |res|\n puts res\nend\n"}], "src_uid": "8bbec86e427e26158393bbfbf1a067fe"} {"nl": {"description": "For some binary string $$$s$$$ (i.e. each character $$$s_i$$$ is either '0' or '1'), all pairs of consecutive (adjacent) characters were written. In other words, all substrings of length $$$2$$$ were written. For each pair (substring of length $$$2$$$), the number of '1' (ones) in it was calculated.You are given three numbers: $$$n_0$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$0$$$; $$$n_1$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$1$$$; $$$n_2$$$ — the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$2$$$. For example, for the string $$$s=$$$\"1110011110\", the following substrings would be written: \"11\", \"11\", \"10\", \"00\", \"01\", \"11\", \"11\", \"11\", \"10\". Thus, $$$n_0=1$$$, $$$n_1=3$$$, $$$n_2=5$$$.Your task is to restore any suitable binary string $$$s$$$ from the given values $$$n_0, n_1, n_2$$$. It is guaranteed that at least one of the numbers $$$n_0, n_1, n_2$$$ is greater than $$$0$$$. Also, it is guaranteed that a solution exists.", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases in the input. Then test cases follow. Each test case consists of one line which contains three integers $$$n_0, n_1, n_2$$$ ($$$0 \\le n_0, n_1, n_2 \\le 100$$$; $$$n_0 + n_1 + n_2 > 0$$$). It is guaranteed that the answer for given $$$n_0, n_1, n_2$$$ exists.", "output_spec": "Print $$$t$$$ lines. Each of the lines should contain a binary string corresponding to a test case. If there are several possible solutions, print any of them.", "sample_inputs": ["7\n1 3 5\n1 1 1\n3 9 3\n0 1 0\n3 1 2\n0 0 3\n2 0 0"], "sample_outputs": ["1110011110\n0011\n0110001100101011\n10\n0000111\n1111\n000"], "notes": null}, "positive_code": [{"source_code": "gets.to_i.times{\n a,b,c=gets.split.map &:to_i\n if c>0\n s=[1]*(c+1)\n else\n s=[0]\n end\n\n while b>0\n s<<1-s[-1]\n b-=1\n end\n\n if a>0\n if s[-1]==0\n s+=[0]*a\n elsif s[0]==0\n s=([0]*a+s)\n elsif s[-2]==0\n s[-1]=0\n s+=[0]*(a-1)\n s<<1\n end\n end\n\n puts s*''\n}\n"}], "negative_code": [{"source_code": "gets.to_i.times{\n a,b,c=gets.split.map &:to_i\n if c>0\n s=[1]*(c+1)\n else\n s=[0]\n end\n\n while b>0\n s<<1-s[-1]\n b-=1\n end\n a+=1 if s[-1]==1 and a>0\n s+=[0]*a\n puts s*''\n}\n"}], "src_uid": "4bbb078b66b26d6414e30b0aae845b98"} {"nl": {"description": "Recently Petya walked in the forest and found a magic stick.Since Petya really likes numbers, the first thing he learned was spells for changing numbers. So far, he knows only two spells that can be applied to a positive integer: If the chosen number $$$a$$$ is even, then the spell will turn it into $$$\\frac{3a}{2}$$$; If the chosen number $$$a$$$ is greater than one, then the spell will turn it into $$$a-1$$$. Note that if the number is even and greater than one, then Petya can choose which spell to apply.Petya now has only one number $$$x$$$. He wants to know if his favorite number $$$y$$$ can be obtained from $$$x$$$ using the spells he knows. The spells can be used any number of times in any order. It is not required to use spells, Petya can leave $$$x$$$ as it is.", "input_spec": "The first line contains single integer $$$T$$$ ($$$1 \\le T \\le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers $$$x$$$ and $$$y$$$ ($$$1 \\le x, y \\le 10^9$$$) — the current number and the number that Petya wants to get.", "output_spec": "For the $$$i$$$-th test case print the answer on it — YES if Petya can get the number $$$y$$$ from the number $$$x$$$ using known spells, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).", "sample_inputs": ["7\n2 3\n1 1\n3 6\n6 8\n1 2\n4 1\n31235 6578234"], "sample_outputs": ["YES\nYES\nNO\nYES\nNO\nYES\nYES"], "notes": null}, "positive_code": [{"source_code": "g = gets.chomp.to_i\ng.times{\n\nx,y = gets.split(\" \").map{|x|x.to_i}\n\nif x==y\nputs \"YES\"\nelsif x%2!=0 and x < 2 \nputs \"NO\"\nelsif x%2==0 and x!=2\nputs \"YES\"\nelsif x%2==0 and x==2 and y==3 \nputs \"YES\"\nelsif x%2!=0 and x > 3 \nputs \"YES\"\nelsif x > y\nputs \"YES\"\nelse\nputs \"NO\"\nend\n\n}\n\n"}], "negative_code": [{"source_code": "g = gets.chomp.to_i\ng.times{\n\nx,y = gets.split(\" \").map{|x|x.to_i}\n\nif x==y\nputs \"YES\"\nelsif x%2!=0 and x < 2 \nputs \"NO\"\nelsif x%2==0 and x!=2\nputs \"YES\"\nelsif x%2==0 and x==2 and y==3 \nputs \"YES\"\nelsif x%2!=0 and x > 3 \nputs \"YES\"\nelse\nputs \"NO\"\nend\n\n}\n\n"}], "src_uid": "b3978805756262e17df738e049830427"} {"nl": {"description": "You are given a string s and should process m queries. Each query is described by two 1-based indices li, ri and integer ki. It means that you should cyclically shift the substring s[li... ri] ki times. The queries should be processed one after another in the order they are given.One operation of a cyclic shift (rotation) is equivalent to moving the last character to the position of the first character and shifting all other characters one position to the right.For example, if the string s is abacaba and the query is l1 = 3, r1 = 6, k1 = 1 then the answer is abbacaa. If after that we would process the query l2 = 1, r2 = 4, k2 = 2 then we would get the string baabcaa.", "input_spec": "The first line of the input contains the string s (1 ≤ |s| ≤ 10 000) in its initial state, where |s| stands for the length of s. It contains only lowercase English letters. Second line contains a single integer m (1 ≤ m ≤ 300) — the number of queries. The i-th of the next m lines contains three integers li, ri and ki (1 ≤ li ≤ ri ≤ |s|, 1 ≤ ki ≤ 1 000 000) — the description of the i-th query.", "output_spec": "Print the resulting string s after processing all m queries.", "sample_inputs": ["abacaba\n2\n3 6 1\n1 4 2"], "sample_outputs": ["baabcaa"], "notes": "NoteThe sample is described in problem statement."}, "positive_code": [{"source_code": "s = gets.chomp.split('')\nm = gets.to_i\nfor i in 0..m-1\n\tl, r, k = gets.split(' ').map{|i| i.to_i}\n\tl -= 1\n\tr -= 1\n\ts = (l > 0 ? s[0..l-1] : []) + s[l..r].rotate(-k) + s[r+1..s.length-1]\nend\n\nputs s.reduce{|a, b| a << b}"}, {"source_code": "s = gets\nm = gets.to_i\nwhile m>0\n l,r,k = gets.split(\" \").map(&:to_i)\n new_s = s[l-1,(r-l+1)]\n pre = s[0,l-1]\n post = s[r,s.size]\n k = k%(r-l+1)\n ass = new_s[new_s.size-k,new_s.size]\n arr = ass + new_s.chomp(ass)\n s = pre + arr + post\n m-=1\nend\nputs s\n"}, {"source_code": "str = gets.chomp\nm = gets.chomp.to_i\nfor i in 1..m do\n l, r, k = gets.split.map(&:to_i)\n k = k % (r - l + 1)\n str[l-1..r-1] = str[r-k..r-1] + str[l-1..r-1-k]\nend\nputs str\n"}, {"source_code": "s = gets.to_s\nm = gets.to_i\n\nm.times do\n l, r, k = gets.split(' ').map{ |x| x.to_i }\n l -= 1\n r -= 1\n k = k % (r-l+1)\n s[l..r] = s[r-k+1..r] + s[l..r-k] if l != r\nend\n\nputs s"}, {"source_code": "# c598-B\n\n\ndef process_query(str, l,r,k)\n\tn = str.size\n\ta = l-1\n\tb = r - k % (r-a)\n\tc = r\n\tstr[0,a] + str[b, c-b] + str[a, b-a] + str[c, n-c]\nend\n\nstr = gets\ngets.to_i.times do\n\tl, r, k = gets.split.map {|x| Integer(x)}\n\tstr = process_query(str, l, r, k)\nend\nputs str\n\n"}, {"source_code": "#!/usr/bin/env ruby\n\ns = gets.strip # < 10000\nm = gets.to_i\n\nm.times.map do\n l_i, r_i, k_i = gets.split(' ').map(&:to_i)\n l_i -= 1\n r_i -= 1\n\n window_len = r_i - l_i + 1\n k_i = k_i.modulo(window_len)\n\n slice_len = window_len - k_i\n substr = s.slice!(l_i, slice_len)\n #p s + ' ' + substr\n insertIdx = l_i+k_i\n #p insertIdx\n s.insert(insertIdx, substr)\n #p s\nend\n\nputs s\n"}, {"source_code": "def rotate(s, l, r, k)\n len = r - l + 1\n k = k % len\n result = ''\n result += s[0..l-2] if (l - 2 >= 0)\n result += s[r-k..r-1] if (r-k <= r-1)\n result += s[l-1..r-1-k] if (l-1 <= r-1-k)\n result += s[r..-1] if (r < s.length)\nend\n\ns = gets\nm = gets.to_i\n(1..m).each do\n (l, r, k) = gets.split(' ').map(&:to_i)\n s = rotate(s, l, r, k)\nend\n\nputs s\n"}, {"source_code": "s = gets.chomp.split(\"\")\nm = gets.chomp.to_i\nm.times do\n l, r, k = gets.chomp.split(\" \").map(&:to_i)\n kk = k % (r - l + 1)\n sr = []\n kk.times do\n sr.unshift(s.slice!(r-1))\n r -= 1\n end\n s.insert(l-1, sr).flatten!\nend\nputs s.join(\"\")\n"}, {"source_code": "#!/usr/bin/ruby\n\nstr = gets.strip()\nlen = gets.to_i\nfor iter in 0..len - 1\n l, r, k = gets.split(' ')\n l = l.to_i\n r = r.to_i\n k = k.to_i\n substr = str[l-1..r-1]\n k %= substr.length\n substr = substr[-k, k] + substr[0, substr.length-k]\n substr = substr + str[r..-1]\n if l >= 2\n str = str[0..l-2] + substr\n else\n str = substr\n end\nend\nputs str\n"}, {"source_code": "s = gets\nm = gets.to_i\n\ndef shift(s, step)\n return s if step == 0\n s[-step..-1] + s[0..s.size-step-1]\nend\n\nm.times do\n l,r,k = gets.split.map(&:to_i)\n l-=1\n r-=1\n k %= r + 1 - l\n s[l..r] = shift(s[l..r], k)\nend\nputs(s)"}], "negative_code": [{"source_code": "str = gets.chomp\nm = gets.chomp.to_i\nfor i in 1..m do\n l, r, k = gets.split.map(&:to_i)\n for n in 1..k do\n newStr = \"\"\n newStr << str[r-1]\n newStr << str[l-1..r-2]\n str[l-1..r-1] = newStr\n end\nend\nputs str\n"}, {"source_code": "s = gets.to_s\nm = gets.to_i\n\nm.times do\n l, r, k = gets.split(' ').map{ |x| x.to_i }\n l -= 1\n r -= 1\n s[l..r] = s[r-k+1..r] + s[l..r-k] if l != r\nend\n\nputs s"}, {"source_code": "s = gets.to_s\nm = gets.to_i\n\nm.times do\n l, r, k = gets.split(' ').map{ |x| x.to_i }\n l -= 1\n r -= 1\n s[l..r] = s[r-k+1..r] + s[l..r-k] \nend\n\nputs s"}, {"source_code": "def rotate(s, l, r, k)\n len = r - l + 1\n k = k % len\n result = ''\n result += s[0..l-2] if (l - 2 > 0)\n result += s[r-k..r-1]\n result += s[l-1..r-1-k]\n result += s[r..-1] if (r < s.length)\n\nend\n\ns = gets\nm = gets.to_i\n(1..m).each do\n (l, r, k) = gets.split(' ').map(&:to_i)\n s = rotate(s, l, r, k)\nend\n\nputs s\n"}, {"source_code": "s = gets\nm = gets.to_i\n\ndef shift(s, step)\n return s if step == 0\n s[step..-1] + s[0..step-1]\nend\n\nm.times do\n l,r,k = gets.split.map(&:to_i)\n k %= r + 1 - l\n s[l..r] = shift(s[l..r], k)\nend\nputs(s)"}, {"source_code": "s = gets\nm = gets.to_i\n\ndef shift(s, step)\n return s if step == 0\n s[0..step-1] + s[step..-1]\nend\n\nm.times do\n l,r,k = gets.split.map(&:to_i)\n l-=1\n r-=1\n k %= r + 1 - l\n s[l..r] = shift(s[l..r], k)\nend\nputs(s)"}, {"source_code": "s = gets\nm = gets.to_i\n\ndef shift(s, step)\n return s if step == 0\n s[step..-1] + s[0..step-1]\nend\n\nm.times do\n l,r,k = gets.split.map(&:to_i)\n l-=1\n r-=1\n k %= r + 1 - l\n s[l..r] = shift(s[l..r], k)\nend\nputs(s)"}], "src_uid": "501b60c4dc465b8a60fd567b208ea1e3"} {"nl": {"description": "A multi-subject competition is coming! The competition has $$$m$$$ different subjects participants can choose from. That's why Alex (the coach) should form a competition delegation among his students. He has $$$n$$$ candidates. For the $$$i$$$-th person he knows subject $$$s_i$$$ the candidate specializes in and $$$r_i$$$ — a skill level in his specialization (this level can be negative!). The rules of the competition require each delegation to choose some subset of subjects they will participate in. The only restriction is that the number of students from the team participating in each of the chosen subjects should be the same.Alex decided that each candidate would participate only in the subject he specializes in. Now Alex wonders whom he has to choose to maximize the total sum of skill levels of all delegates, or just skip the competition this year if every valid non-empty delegation has negative sum.(Of course, Alex doesn't have any spare money so each delegate he chooses must participate in the competition).", "input_spec": "The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n \\le 10^5$$$, $$$1 \\le m \\le 10^5$$$) — the number of candidates and the number of subjects. The next $$$n$$$ lines contains two integers per line: $$$s_i$$$ and $$$r_i$$$ ($$$1 \\le s_i \\le m$$$, $$$-10^4 \\le r_i \\le 10^4$$$) — the subject of specialization and the skill level of the $$$i$$$-th candidate.", "output_spec": "Print the single integer — the maximum total sum of skills of delegates who form a valid delegation (according to rules above) or $$$0$$$ if every valid non-empty delegation has negative sum.", "sample_inputs": ["6 3\n2 6\n3 6\n2 5\n3 5\n1 9\n3 1", "5 3\n2 6\n3 6\n2 5\n3 5\n1 11", "5 2\n1 -1\n1 -5\n2 -1\n2 -1\n1 -10"], "sample_outputs": ["22", "23", "0"], "notes": "NoteIn the first example it's optimal to choose candidates $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$, so two of them specialize in the $$$2$$$-nd subject and other two in the $$$3$$$-rd. The total sum is $$$6 + 6 + 5 + 5 = 22$$$.In the second example it's optimal to choose candidates $$$1$$$, $$$2$$$ and $$$5$$$. One person in each subject and the total sum is $$$6 + 6 + 11 = 23$$$.In the third example it's impossible to obtain a non-negative sum."}, "positive_code": [{"source_code": "n,m = gets.split.map(&:to_i)\n\nans = Array.new(n+1,0)\ncounter = Array.new(m+1,0) # how many student in ths subject\nsum = Array.new(m+1,0) # total point in this subject\nstudents = []\nn.times do\n students << gets.split.map(&:to_i)\nend\n\nstudents.sort_by!{|s| -s[1] }\n\n# puts students.inspect\n\nstudents.each do |s|\n counter[s[0]] += 1 \n sum[s[0]] += s[1]\n \n if sum[s[0]] > 0\n ans[counter[s[0]]] += sum[s[0]]\n end\nend\n\n# puts ans.inspect\nputs ans.max\n\n"}], "negative_code": [], "src_uid": "cb688cc52bd2bf0af77084dc4a25c28b"} {"nl": {"description": "In Berland recently a new collection of toys went on sale. This collection consists of 109 types of toys, numbered with integers from 1 to 109. A toy from the new collection of the i-th type costs i bourles.Tania has managed to collect n different types of toys a1, a2, ..., an from the new collection. Today is Tanya's birthday, and her mother decided to spend no more than m bourles on the gift to the daughter. Tanya will choose several different types of toys from the new collection as a gift. Of course, she does not want to get a type of toy which she already has.Tanya wants to have as many distinct types of toys in her collection as possible as the result. The new collection is too diverse, and Tanya is too little, so she asks you to help her in this.", "input_spec": "The first line contains two integers n (1 ≤ n ≤ 100 000) and m (1 ≤ m ≤ 109) — the number of types of toys that Tanya already has and the number of bourles that her mom is willing to spend on buying new toys. The next line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the types of toys that Tanya already has.", "output_spec": "In the first line print a single integer k — the number of different types of toys that Tanya should choose so that the number of different types of toys in her collection is maximum possible. Of course, the total cost of the selected toys should not exceed m. In the second line print k distinct space-separated integers t1, t2, ..., tk (1 ≤ ti ≤ 109) — the types of toys that Tanya should choose. If there are multiple answers, you may print any of them. Values of ti can be printed in any order.", "sample_inputs": ["3 7\n1 3 4", "4 14\n4 6 12 8"], "sample_outputs": ["2\n2 5", "4\n7 2 3 1"], "notes": "NoteIn the first sample mom should buy two toys: one toy of the 2-nd type and one toy of the 5-th type. At any other purchase for 7 bourles (assuming that the toys of types 1, 3 and 4 have already been bought), it is impossible to buy two and more toys."}, "positive_code": [{"source_code": "#!/usr/bin/ruby\nn,m=gets.split.map(&:to_i)\nh={}\ngets.split.each{|e|h[e.to_i]=1}\nr=[]\n(1..1/0.0).each{|i|\n\tif !h[i]\n\t\tbreak if m m\n next if hash.key?(idx)\n\n m -= idx\n values << idx\nend\nputs values.length\nputs values.join(' ')\n"}, {"source_code": "n, m = gets.chomp.split.map(&:to_i)\nhash = {}.tap do |h|\n gets.chomp.split.map(&:to_i).each { |idx| h[idx] = true }\n end\n\nvalues = []\nsum = 0\n(1..(10**9)).each do |idx|\n next if hash.key?(idx)\n break if sum + idx > m\n\n sum += idx\n values << idx\nend\nputs values.length\nputs values.join(' ')\n"}, {"source_code": "n, m = readline.split(' ').collect(&:to_i)\nh = Hash[readline.split(' ').collect(&:to_i).collect{|num| [num, true]}]\nres = []\nct = 0\nleft = m\n\n1.upto(m) do |item|\n if item > left\n break\n end\n if !h.has_key?(item)\n res.push(item)\n left -= item\n ct += 1\n end\nend\n\nputs ct\nres.each_with_index do |item, ind|\n if ind != 0\n print \" \"\n end\n print item\nend\nputs \"\"\n"}, {"source_code": "n, m = readline.split(' ').collect(&:to_i)\nh = Hash[readline.split(' ').collect(&:to_i).collect{|num| [num, true]}]\nres = []\nct = 0\nleft = m\n\n1.upto(m) do |item|\n if item > left\n break\n end\n if !h.has_key?(item)\n res.insert(-1, item)\n left -= item\n ct += 1\n end\nend\n\nputs ct\nres.each_with_index do |item, ind|\n if ind != 0\n print \" \"\n end\n print item\nend\nputs \"\"\n"}, {"source_code": "a=gets.chomp.split(\" \")\ncost=a[1].to_i\nb=gets.chomp.split(\" \").map {|i| i.to_i}\nb.sort!\nans=0\nar=[]\ni=1\nj=0\nloop do\n\nif i!=b[j]\ncost-=i\nif cost>=0\nans+=1\nar << i\nelse\nbreak\nend\n\nelse\nj+=1\nend\n\ni+=1\nbreak if i == 1000000000\n\nend\n\nputs \"#{ans}\"\nputs ar.join(\" \")\n"}, {"source_code": "a=gets.chomp.split(\" \")\ncost=a[1].to_i\nb=gets.chomp.split(\" \").map {|i| i.to_i}\nb.sort!\nans=0\nar=[]\ni=1\nj=0\nloop do\n\nif i!=b[j]\ncost-=i\nif cost>=0\nans+=1\nar << i\nelse\nbreak\nend\n\nelse\nj+=1\nend\n\ni+=1\nbreak if i == 1000000000\n\nend\n\nputs \"#{ans}\"\nputs ar.join(\" \")\n"}, {"source_code": "a=gets.chomp.split(\" \")\ncost=a[1].to_i\nb=gets.chomp.split(\" \").map {|i| i.to_i}\nb.sort!\nans=0\nar=[]\ni=1\nj=0\nloop do\n\nif i!=b[j]\ncost-=i\nif cost>=0\nans+=1\nar << i\nelse\nbreak\nend\n\nelse\nj+=1\nend\n\ni+=1\nbreak if i == 1000000000\n\nend\n\nputs \"#{ans}\"\nputs ar.join(\" \")"}, {"source_code": "buf = gets.split(' ')\nn,m=buf[0].to_i, buf[1].to_i\nbuf = gets.split(' ')\nhash = Hash.new\nbuf.each do |x|\n\thash[x.to_i] = 1\nend\nans = Array.new\nfor i in 1..1000000\n\tbreak if i>m\n\tif !hash[i]\n\t\tm-=i\n\t\tans<m\n\tif !hash[i]\n\t\tm-=i\n\t\tans< b[0]\n counters = b.select{|r| r == b[n-1]}.size\n xounterz = b.select{|r| r == b[0]}.size\n puts (b[n-1]-b[0]).to_s + \" \" + (counters*xounterz).to_s\nelse\n puts \"0\" + ' ' + (b.size * (b.size - 1)/2).to_s\nend"}, {"source_code": "n = gets.to_i\nx = gets.split.map(&:to_i)\nl, r = x.max, x.min\nif l > r\n puts (l - r).to_s + \" \" + (x.select{|i| i == l}.size * x.select{|i| i == r}.size).to_s\nelse\n puts \"0\" + \" \" + (x.size * (x.size - 1) / 2).to_s\nend"}, {"source_code": "n, b = gets.to_i, gets.split.map { |x| x.to_i }.sort\nmax_d = b.last - b.first\narr = Hash.new(0)\nb.each_with_index { |x| arr[x] += 1 }\nif arr.size == 1\n print max_d, \" \", arr[b.last] * (arr[b.last] - 1) / 2\nelse\n print max_d, \" \", arr[b.last] * arr[b.first]\nend\n"}, {"source_code": "gets\nx=gets.split.map{|e| e.to_i}\nmin1=x.min\nmax1=x.max\nc1=x.count{|e|\n\te==min1\n}\nc2=x.count{|e|\n\te==max1\n}\n\nif min1==max1 then\n\tputs [0,(c1*(c1-1))/2]*\" \"\nelse\n\tputs [max1-min1,c1*c2]*\" \"\nend\n\n"}, {"source_code": "n = gets.to_i\nb = gets.split.map(&:to_i)\nx = [1000000000,0]\ny = [0,0]\nb.each do |i|\n\tif i == x[0]\n\t\tx[1] += 1\n\telsif i < x[0]\n\t\tx[0] = i\n\t\tx[1] = 1\n\tend\n\tif i == y[0]\n\t\ty[1] += 1\n\telsif i > y[0]\n\t\ty[0] = i\n\t\ty[1] = 1\n\tend\nend\nif y[0] == x[0]\n\tprint 0,' ',x[1] * (x[1] - 1) / 2\nelse\n\tprint y[0] - x[0],' ',x[1]*y[1]\nend"}, {"source_code": "n = gets.to_i\nflowers = Hash.new(0)\n\nmin, max = gets.chomp.split.map { |value| value.to_i }.each { |value| flowers[value] += 1 }.sort.minmax\n\ndif = max - min\npath = 0\n\nif dif != 0\n flowers.each { |key, value| path += value * flowers[key - dif] }\nelse\n path = n * (n - 1) / 2\nend\n\nputs \"#{dif} #{path}\""}, {"source_code": "n = gets.to_i\nflowers = Hash.new(0)\n\nmin, max = gets.chomp!.split.map! { |value| value.to_i }.each { |value| flowers[value] += 1 }.sort!.minmax\n\ndif = max - min\npath = 0\n\nif dif != 0\n flowers.each { |key, value| path += value * flowers[key - dif] }\nelse\n path = n * (n - 1) / 2\nend\n\nputs \"#{dif} #{path}\""}, {"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\n\nn = gets.to_i\nmin, min_counter = 10**9, 0\nmax, max_counter = 0, 0\ngets.split.each { |i| \n i = i.to_i\n if i < min\n min = i\n min_counter = 0\n end\n\n if i == min\n min_counter += 1\n end\n\n if i > max\n max = i\n max_counter = 0\n end\n\n if i == max\n max_counter += 1\n end\n}\n\nif max != min\n puts \"#{max - min} #{min_counter * max_counter}\"\nelse\n # n C 2\n # -> n! / (2! * (n-2)!)\n # -> (n * (n -1)) / 2!\n # -> n * (n - 1) / 2\n combination = max_counter * (max_counter - 1) / 2\n puts \"#{max - min} #{combination}\"\nend\n\n# pair of two flowers\n# first: maximum beauty difference\n# second: how many ways to create the diff?\n"}, {"source_code": "n = Integer(gets)\nnums = gets.chomp.split\nb = Array.new\nfor i in 0...n\n b[i] = Integer(nums[i])\nend\n\nb.sort!\nmin = Array.new\nmax = Array.new\nmin[0] = b[0]\nmax[0] = b[n-1]\n\ngap = b[n-1] - b[0]\nfor i in 0...n-1\n if b[i+1] == b[0]\n min.push(b[i+1])\n end\n if b[n-i-2] == b[n-1]\n max.push(b[n-i-2])\n end\nend\n\nif gap != 0\n print gap , \" \" , min.length * max.length\nelse\n if n != 2\n ways = n * (n-1) / 2\n print gap , \" \" , ways\n else\n print gap, \" \" , \"1\"\n end\nend\n"}, {"source_code": "def read_ints; STDIN.gets.split.map(&:to_i); end\nn = gets.to_i\na = read_ints\n_max = a.max\n_min = a.min\nprint _max-_min\na1 = 0\na2 = 0\ni=0\nn.times do\n if(a[i] == _max)\n a1+=1\n elsif (a[i] == _min)\n a2+=1\n end\n i+=1\nend\nprint ' '\nif(_max == _min)\n puts a1 * (a1-1) / 2\nelse\nputs a1 * a2\nend"}, {"source_code": "n = gets.to_i\nb = {}\nh = Hash.new { |h, k| h[k] = 0 }\na = gets.split(/\\s+/).map {|q| r = q.to_i; h[r]+=1; r}\n\nr = 0\ndiff = a.max - a.min\nif diff > 0\n a.each {|q| r += h[q + diff] }\nelse\n h.each {|k,v| r += v*(v-1)/2 if v > 1 }\nend\n\nputs \"#{diff} #{r}\"\n"}, {"source_code": "gets\n\nfreq = {}\nflowers = []\ngets.split(/\\s+/).each do |f|\n f = f.to_i\n\n if freq[f].nil?\n freq[f] = 1\n else\n freq[f] += 1\n end\n flowers << f\nend\n\nflowers.sort!\n\nfirst = flowers[0]\nlast = flowers[-1]\n\nif(first == last)\n puts \"#{last - first} #{(freq[first] * (freq[last]-1))/2}\"\nelse\n puts \"#{last - first} #{freq[first] * freq[last]}\"\nend\n"}, {"source_code": "gets\na = gets.split(' ').map(&:to_i)\n\nmax = a.max\nmin = a.min\n\ncmax = a.count(max)\ncmin = a.count(min)\n\ncount = max == min ? cmax*(cmax-1)/2 : cmax*cmin\n\nputs \"#{max - min} #{count}\"\n"}, {"source_code": "line = gets\nn = line.to_i\nline = gets\nf = line.split.map(&:to_i)\nf_s = f.sort\nd = f_s.last - f_s.first\nf_m = {}\nf.map do |i|\n\tnext unless [f_s.first, f_s.last].include?(i)\n\tf_m[i] ||= 0\n\tf_m[i] += 1\nend\nw = if f_s.first != f_s.last\n\tf_m[f_s.first] * f_m[f_s.last]\nelse\n\t(n * (n-1))/2 \nend\nputs \"#{d} #{w}\"\n"}, {"source_code": "\nn = gets.to_i\ntab = gets.split.map(&:to_i)\n\nh, l = tab.max, tab.min\n\nif h > l\n puts (h-l).to_s + \" \" + \n (tab.select{|x| x == h}.size * tab.select{|x| x == l}.size).to_s\nelse\n puts \"0 \" + (tab.size * (tab.size-1)/2).to_s\nend\n\n\n"}], "negative_code": [{"source_code": "#!/usr/bin/ruby\n\nn = $stdin.readline.to_i\nb = $stdin.readline.split.map(&:to_i)\n\nb.sort!\n\na1 = b[0]\na2 = b[b.length-1]\n\nc1 = 0\nc2 = 0\n\nb.each { |x|\n if x == a1\n c1 += 1\n end }\n\nb.each { |x|\n if x == a2\n c2 += 1\n end }\n\nif a1 == a2\n c1 = 0\nend\n\nputs \"#{a2 - a1} #{c1 * c2}\"\n"}, {"source_code": "#!/usr/bin/ruby\n\nn = $stdin.readline.to_i\nb = $stdin.readline.split.map(&:to_i)\n\nb.sort!\n\na1 = b[0]\na2 = b[b.length-1]\n\nc1 = 0\nc2 = 0\n\nb.each { |x|\n if x == a1\n c1 += 1\n end }\n\nb.each { |x|\n if x == a2\n c2 += 1\n end }\n\nputs \"#{a2 - a1} #{c1 * c2}\"\n"}, {"source_code": "n = gets.to_i\nb = gets.split.map{|r| r.to_i}.sort\n\ncounters = b.select{|r| r == b[n-1]}.size\nxounterz = b.select{|r| r == b[0]}.size\n\nputs (b[n-1]-b[0]).to_s + \" \" + (counters*xounterz).to_s"}, {"source_code": "n = gets.to_i\nb = gets.split.map{|r| r.to_i}.sort\n\ncounters = 1\nxounterz = 1\nfor i in 1..n-2\n if b[i] == b[0]\n counters+=1\n elsif b[i] == b[n-1]\n xounterz+=1\n end\nend\n\nputs b[n-1]-b[0] \nputs counters*xounterz\n"}, {"source_code": "n = gets.to_i\nb = gets.split.map{|r| r.to_i}.sort\n\ncounters = 1\nxounterz = 1\nfor i in 1..n-2\n if b[i] == b[0]\n counters+=1\n elsif b[i] == b[n-1]\n xounterz+=1\n end\nend\n\nmax = [b[n-1]-b[0], counters*xounterz]\nputs max\n"}, {"source_code": "n = gets.to_i\nb = gets.split.map{|r| r.to_i}.sort\n\ncounters = 1\nxounterz = 1\nfor i in 1..n-2\n if b[i] == b[0]\n counters+=1\n elsif b[i] == b[n-1]\n xounterz+=1\n end\nend\n\nputs (b[n-1]-b[0]).to_s + \" \" + (counters*xounterz).to_s"}, {"source_code": "n = gets.to_i\nb = gets.split.map{|r| r.to_i}.sort\n\nif b[n-1] > b[0]\n counters = b.select{|r| r == b[n-1]}.size\n xounterz = b.select{|r| r == b[0]}.size\n puts (b[n-1]-b[0]).to_s + \" \" + (counters*xounterz).to_s\nelse\n puts \"0 1\" \nend"}, {"source_code": "n = gets.to_i\nb = gets.split.map{|r| r.to_i}.sort\n\ncounters = 1\nxounterz = 1\nfor i in 1..n-2\n if b[i] == b[0]\n counters+=1\n elsif b[i] == b[n-1]\n xounterz+=1\n end\nend\n\nmax = [b[n-1]-b[0], counters*xounterz]\nmax\n"}, {"source_code": "n = gets.to_i\nx = gets.split.map(&:to_i)\nl, r = x.max, x.min\nif l > r\n puts (l - r).to_s + \" \" + (x.select{|i| i == l}.size * x.select{|i| i == r}.size).to_s\nelse\n puts \"0\" + (x.size * (x.size - 1) / 2).to_s\nend"}, {"source_code": "n, b = gets.to_i, gets.split.map { |x| x.to_i }.sort\nmax_d = b.last - b.first\nam = 0\nb.each do |x|\n (x..b.last).step(1) do |j|\n am += 1 if (j - x).abs == max_d\n end\nend\nprint max_d, \" \",am\n"}, {"source_code": "n, b = gets.to_i, gets.split.map { |x| x.to_i }.sort\nmax_d = b.last - b.first\narr = Hash.new(0)\nb.each_with_index { |x| arr[x] += 1 }\nprint max_d, \" \", arr[b.last] * arr[b.first]\n"}, {"source_code": "n, b = gets.to_i, gets.split.map { |x| x.to_i }.sort\nmax_d = b.last - b.first\nam = 0\nb.each do |x|\n b[b.index(x) + 1..b.last].each do |y|\n am += 1 if (y - x).abs == max_d\n end\nend\nprint max_d, \" \",am\n"}, {"source_code": "n, b = gets.to_i, gets.split.map { |x| x.to_i }.sort\nmax_d = b.last - b.first\narr = Hash.new(0)\nb.each_with_index { |x| arr[x] += 1 }\nif arr.size == 1\n print max_d, \" \", arr[b.last] - 1\nelse\n print max_d, \" \", arr[b.last] * arr[b.first]\nend\n"}, {"source_code": "n = gets.to_i\nb = gets.split.map(&:to_i)\nx = [1000000000,0]\ny = [0,0]\nb.each do |i|\n\tif i == x[0]\n\t\tx[1] += 1\n\telsif i < x[0]\n\t\tx[0] = i\n\t\tx[1] = 1\n\tend\n\tif i == y[0]\n\t\ty[1] += 1\n\telsif i > y[0]\n\t\ty[0] = i\n\t\ty[1] = 1\n\tend\nend\nprint y[0] - x[0],' ',x[1]*y[1]"}, {"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\n\nn = gets.to_i\nmin, min_counter = 10**9, 0\nmax, max_counter = 0, 0\ngets.split.each { |i| \n i = i.to_i\n if i < min\n min = i\n min_counter = 0\n end\n\n if i == min\n min_counter += 1\n end\n\n if i > max\n max = i\n max_counter = 0\n end\n\n if i == max\n max_counter += 1\n end\n}\n\nif max != min\n puts \"#{max - min} #{min_counter * max_counter}\"\nelse\n puts \"0 0\"\nend\n\n# pair of two flowers\n# first: maximum beauty difference\n# second: how many ways to create the diff?\n"}, {"source_code": "n = Integer(gets)\nnums = gets.chomp.split\nb = Array.new\nfor i in 0...n\n b[i] = Integer(nums[i])\nend\n\nb.sort!\nmin = Array.new\nmax = Array.new\nmin[0] = b[0]\nmax[0] = b[n-1]\n\ngap = b[n-1] - b[0]\nfor i in 0...n-1\n if b[i+1] == b[0]\n min.push(b[i+1])\n end\n if b[n-i-2] == b[n-1]\n max.push(b[n-i-2])\n end\nend\nways = 0\nwhile n >= 2\n ways += (n-1) * n\n n -= 1\nend\nif gap != 0\n print gap , \" \" , min.length * max.length\nelse\n print gap , \" \" , ways\nend\n"}, {"source_code": "n = Integer(gets)\nnums = gets.chomp.split\nb = Array.new\nfor i in 0...n\n b[i] = Integer(nums[i])\nend\n\nb.sort!\nmin = Array.new\nmax = Array.new\nmin[0] = b[0]\nmax[0] = b[n-1]\n\ngap = b[n-1] - b[0]\nfor i in 0...n-1\n if b[i+1] == b[0]\n min.push(b[i+1])\n end\n if b[n-i-2] == b[n-1]\n max.push(b[n-i-2])\n end\nend\n\nif gap != 0\n print gap , \" \" , min.length * max.length\nelse\n if n != 2\n ways = 0\n while n >= 2\n ways += (n-1) * n\n n -= 1\n end\n print gap , \" \" , ways\n else\n print gap, \" \" , \"1\"\n end\nend\n"}, {"source_code": "def read_ints; STDIN.gets.split.map(&:to_i); end\nn = gets.to_i\na = read_ints\n_max = a.max\n_min = a.min\nprint _max-_min\na1 = 0\na2 = 0\ni=0\nn.times do\n if(a[i] == _max)\n a1+=1\n elsif (a[i] == _min)\n a2+=1\n end\n i+=1\nend\nprint ' '\nputs a1 * a2"}, {"source_code": "n = gets.to_i\nb = {}\nh = Hash.new { |h, k| h[k] = 0 }\na = gets.split(/\\s+/).map {|q| r = q.to_i; h[r]+=1; r}\n\nr = 0\ndiff = a.max - a.min\nif diff > 0\n a.each {|q| r += h[q + diff] }\nelse\n h.each {|k,v| r += v-1 if v > 1 }\nend\n\nputs \"#{diff} #{r}\"\n"}, {"source_code": "n = gets.to_i\nh = Hash.new { |h, k| h[k] = 0 }\na = gets.split(/\\s+/).map {|q| r = q.to_i; h[r]+=1; r}\n\nr = 0\ndiff = a.max - a.min\na.each {|q| r += h[q + diff] }\n\nputs \"#{diff} #{r}\"\n"}, {"source_code": "n = gets.to_i\nb = {}\nh = Hash.new { |h, k| h[k] = 0 }\na = gets.split(/\\s+/).map {|q| r = q.to_i; h[r]+=1; r}\n\nr = 0\ndiff = a.max - a.min\nif diff > 0\n a.each {|q| r += h[q + diff] }\nelse\n h.each {|k,v| r += 2**(v-1)-1 if v > 1 }\nend\n\nputs \"#{diff} #{r}\"\n"}, {"source_code": "gets\n\nfreq = {}\nflowers = []\ngets.split(/\\s+/).each do |f|\n f = f.to_i\n\n if freq[f].nil?\n freq[f] = 1\n else\n freq[f] += 1\n end\n flowers << f\nend\n\nflowers.sort!\n\nfirst = flowers[0]\nlast = flowers[-1]\n\nif(first == last)\n puts \"#{last - first} #{freq[first] * (freq[last]-1)}\"\nelse\n puts \"#{last - first} #{freq[first] * freq[last]}\"\nend\n"}, {"source_code": "gets\na = gets.split(' ').map(&:to_i)\n\nputs \"#{a.max - a.min} #{a.count(a.max)*a.count(a.min)}\"\n"}, {"source_code": "line = gets\nn = line.to_i\nline = gets\nf = line.split.map(&:to_i)\nf_s = f.sort\nd = f_s.last - f_s.first\nf_m = {}\nf.map do |i|\n\tnext unless [f_s.first, f_s.last].include?(i)\n\tf_m[i] ||= 0\n\tf_m[i] += 1\nend\nw = if f_m.size > 1\n\tf_m[f_s.first] * f_m[f_s.last]\nelse\n\t(n * n-1)/2 \nend\nputs \"#{d} #{w}\"\n"}, {"source_code": "line = gets\nn = line.to_i\nline = gets\nf = line.split.map(&:to_i)\nf_s = f.sort\nd = f_s.last - f_s.first\nf_m = {}\nf.map do |i|\n\tnext unless [f_s.first, f_s.last].include?(i)\n\tf_m[i] ||= 0\n\tf_m[i] += 1\nend\nw = if f_m.size > 1\n\tf_m[f_s.first] * f_m[f_s.last]\nelse\n\t1\nend\nputs \"#{d} #{w}\"\n"}, {"source_code": "line = gets\nn = line.to_i\nline = gets\nf = line.split.map(&:to_i)\nf_s = f.sort\nd = f_s.last - f_s.first\nf_m = {}\nf.map do |i|\n\tnext unless [f_s.first, f_s.last].include?(i)\n\tf_m[i] ||= 0\n\tf_m[i] += 1\nend\nw = if f_s.first != f_s.last\n\tf_m[f_s.first] * f_m[f_s.last]\nelse\n\t1\nend\nputs \"#{d} #{w}\"\n"}, {"source_code": "line = gets\nn = line.to_i\nline = gets\nf = line.split.map(&:to_i)\nf_s = f.sort\nd = f_s.last - f_s.first\nf_m = {}\nf.map do |i|\n\tnext unless [f_s.first, f_s.last].include?(i)\n\tf_m[i] ||= 0\n\tf_m[i] += 1\nend\nw = if f_s.first == f_s.last\n\tf_m[f_s.first] * f_m[f_s.last]\nelse\n\t(n * (n-1))/2 \nend\nputs \"#{d} #{w}\"\n"}, {"source_code": "line = gets\nn = line.to_i\nline = gets\nf = line.split.map(&:to_i)\nf_s = f.sort\nd = f_s.last - f_s.first\nf_m = {}\nf.map do |i|\n\tnext unless [f_s.first, f_s.last].include?(i)\n\tf_m[i] ||= 0\n\tf_m[i] += 1\nend\nw = f_m[f_s.first] * f_m[f_s.last]\nputs \"#{d} #{w}\"\n"}, {"source_code": "\nn = gets.to_i\ntab = gets.split.map(&:to_i)\n\nh, l = tab.max, tab.min\n\nif h > l\n puts (h-l).to_s + \" \" + \n (tab.select{|x| x == h}.size * tab.select{|x| x == l}.size).to_s\nelse\n puts \"0 \" + (tab.size * (tab.size-1)).to_s\nend\n\n\n"}, {"source_code": "\nn = gets.to_i\ntab = gets.split.map(&:to_i)\n\nh, l = tab.max, tab.min\n\nif h != l\n puts (h-l).to_s + \" \" + \n (tab.select{|x| x == h}.size * tab.select{|x| x == l}.size).to_s\nelse\n puts \"0 \" + (tab.size * (tab.size-1)).to_s\nend\n\n\n"}, {"source_code": "\nn = gets.to_i\ntab = gets.split.map(&:to_i)\n\nh, l = tab.max, tab.min\n\nputs (h-l).to_s + \" \" + \n (tab.select{|x| x == h}.size * tab.select{|x| x == l}.size).to_s\n\n\n"}], "src_uid": "eb2d1072c5308d9ef686315a122d9d3c"} {"nl": {"description": "Santa Claus decided to disassemble his keyboard to clean it. After he returned all the keys back, he suddenly realized that some pairs of keys took each other's place! That is, Santa suspects that each key is either on its place, or on the place of another key, which is located exactly where the first key should be. In order to make sure that he's right and restore the correct order of keys, Santa typed his favorite patter looking only to his keyboard.You are given the Santa's favorite patter and the string he actually typed. Determine which pairs of keys could be mixed. Each key must occur in pairs at most once.", "input_spec": "The input consists of only two strings s and t denoting the favorite Santa's patter and the resulting string. s and t are not empty and have the same length, which is at most 1000. Both strings consist only of lowercase English letters.", "output_spec": "If Santa is wrong, and there is no way to divide some of keys into pairs and swap keys in each pair so that the keyboard will be fixed, print «-1» (without quotes). Otherwise, the first line of output should contain the only integer k (k ≥ 0) — the number of pairs of keys that should be swapped. The following k lines should contain two space-separated letters each, denoting the keys which should be swapped. All printed letters must be distinct. If there are several possible answers, print any of them. You are free to choose the order of the pairs and the order of keys in a pair. Each letter must occur at most once. Santa considers the keyboard to be fixed if he can print his favorite patter without mistakes.", "sample_inputs": ["helloworld\nehoolwlroz", "hastalavistababy\nhastalavistababy", "merrychristmas\nchristmasmerry"], "sample_outputs": ["3\nh e\nl o\nd z", "0", "-1"], "notes": null}, "positive_code": [{"source_code": "original = gets.chomp\naltered = gets.chomp\n\nmapping = {}\nfixable = true\n\noriginal.size.times do |i|\n a = original[i]\n b = altered[i]\n\n if (mapping[b] && mapping[b] != a) || (mapping[a] && mapping[a] != b)\n fixable = false\n else\n mapping[a] = b\n mapping[b] = a\n end\nend\n\nif fixable\n has_key = {}\n ans = {}\n mapping.each do |key, value|\n ans[key] = value unless has_key[key] || key == value\n has_key[key] = true\n has_key[value] = true\n end\n\n puts ans.length\n ans.each do |key, value|\n puts \"#{key} #{value}\"\n end\n\nelse\n puts \"-1\"\nend\n"}, {"source_code": "a = gets\nb = gets\nvis = {}\nres = []\nfin = true\nfor i in 0..(a.size() - 2)\n if ((vis.include?(a[i]) || vis.include?(b[i])))\n if (!(vis.include?a[i]) || !(vis.include?b[i]) || vis[a[i]] != b[i])\n puts -1\n fin = false\n break\n end\n end\n next if (vis.include?(a[i]))\n vis[a[i]] = b[i]\n vis[b[i]] = a[i]\n if (a[i] != b[i])\n res.push([a[i],b[i]])\n end\nend\nif (fin)\n puts res.size()\n for i in res \n puts \"#{i[0]} #{i[1]}\"\n end\nend"}], "negative_code": [{"source_code": "original = gets.chomp\naltered = gets.chomp\n\nmapping = {}\nfixable = true\n\noriginal.size.times do |i|\n if mapping[original[i]]\n fixable = false unless mapping[original[i]] == altered[i]\n else\n mapping[original[i]] = altered[i]\n end\nend\n\nif fixable\n has_key = {}\n ans = {}\n mapping.each do |key, value|\n ans[key] = value unless has_key[key] || key == value\n has_key[key] = true\n has_key[value] = true\n end\n\n puts ans.length\n ans.each do |key, value|\n puts \"#{key} #{value}\"\n end\n\nelse\n puts \"-1\"\nend\n"}], "src_uid": "b03895599bd578a83321401428e277da"} {"nl": {"description": "A dice is a cube, its faces contain distinct integers from 1 to 6 as black points. The sum of numbers at the opposite dice faces always equals 7. Please note that there are only two dice (these dices are mirror of each other) that satisfy the given constraints (both of them are shown on the picture on the left). Alice and Bob play dice. Alice has built a tower from n dice. We know that in this tower the adjacent dice contact with faces with distinct numbers. Bob wants to uniquely identify the numbers written on the faces of all dice, from which the tower is built. Unfortunately, Bob is looking at the tower from the face, and so he does not see all the numbers on the faces. Bob sees the number on the top of the tower and the numbers on the two adjacent sides (on the right side of the picture shown what Bob sees).Help Bob, tell whether it is possible to uniquely identify the numbers on the faces of all the dice in the tower, or not.", "input_spec": "The first line contains a single integer n (1 ≤ n ≤ 100) — the number of dice in the tower. The second line contains an integer x (1 ≤ x ≤ 6) — the number Bob sees at the top of the tower. Next n lines contain two space-separated integers each: the i-th line contains numbers ai, bi (1 ≤ ai, bi ≤ 6; ai ≠ bi) — the numbers Bob sees on the two sidelong faces of the i-th dice in the tower. Consider the dice in the tower indexed from top to bottom from 1 to n. That is, the topmost dice has index 1 (the dice whose top face Bob can see). It is guaranteed that it is possible to make a dice tower that will look as described in the input.", "output_spec": "Print \"YES\" (without the quotes), if it is possible to to uniquely identify the numbers on the faces of all the dice in the tower. If it is impossible, print \"NO\" (without the quotes).", "sample_inputs": ["3\n6\n3 2\n5 4\n2 4", "3\n3\n2 6\n4 1\n5 3"], "sample_outputs": ["YES", "NO"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\nt = gets.to_i\na = Array.new(n){\n b = gets.split.map(&:to_i)\n [*1..6] - b - b.map{|_| 7 - _}\n}\nputs a.uniq.size == 1 ? :YES : :NO\n"}, {"source_code": "RD =->{gets.to_i}\nI =->{gets.split.map &:to_i}\n\ndef solve\n\tn = RD[]; t1 = RD[]; t2 = 7 - t1; n.times{\n\t\ta, b = I[]\t\n\t\treturn \"NO\" if a == t1 || a == t2 || b == t1 || b == t2\n\t}\t\t\n\treturn \"YES\"\nend\n\nputs solve\n\n\n"}, {"source_code": "gets\n$u=gets.hex\nputs gets(p)=~/[#$u#{7-$u}]/?\"NO\":\"YES\"\n"}, {"source_code": "def solve(x, faces)\n faces.each do |a, b|\n if (a == b) or (a == x) or (b == x) or ((a + b) == 7) or ((a + x) == 7) or ((b + x) == 7)\n return false\n end\n end\n true\nend \n\nn = gets.chomp.to_i\nx = gets.chomp.to_i\nfaces = []\nn.times do \n faces << gets.chomp.split(\" \").map{|e| e.to_i}\nend\n\nret = solve(x, faces)\nputs ret ? \"YES\" : \"NO\"\n \n\n"}, {"source_code": "gets\n$u=gets.hex\nputs gets(p)=~/[#$u#{7-$u}]/?\"NO\":\"YES\"\n"}, {"source_code": "gets\n$u=gets.hex\nputs gets(p)=~/[#$u#{7-$u}]/?\"NO\":\"YES\""}, {"source_code": "n = gets.to_i - 1\nx = 7 - gets.to_i\ngets\nn.times do\n a = gets.split.map(&:to_i)\n g = ([1,2,3,4,5,6] - [a[0], 7-a[0], a[1], 7-a[1], 7-x])\n if g.size == 1\n x = g[0]\n else\n puts :NO\n exit\n end\nend\nputs :YES"}, {"source_code": "gets\n$u=gets.hex\nputs gets(p)=~/[#$u#{7-$u}]/?\"NO\":\"YES\"\n"}, {"source_code": "def r() gets.to_s.split.map &:to_i end\nn,x,o=*r,*r;\nn.times do\n a,b=*r;o||=[a,b,7-a,7-b].include? x\nend\nprint o ? \"NO\" : \"YES\"\n"}, {"source_code": "#!/usr/bin/ruby\nn = gets.to_i\ntop = gets.to_i\na = []\nn.times do\n a << gets.split.map {|x| x.to_i}\nend\nans = \"YES\"\nsum = 1 + 2 + 3 + 4 + 5 + 6\nfor i in 0...n do\n t = sum - top - a[i][0] - a[i][1] - (7 - a[i][0]) - (7 - a[i][1])\n if t == a[i][0] || t == a[i][1] || t == (7 - a[i][0]) || t == (7 - a[i][1]) then\n ans = \"NO\"\n break\n end\nend\nputs ans\n"}, {"source_code": "gets\n$u=gets.hex\nputs gets(p)=~/[#$u#{7-$u}]/?\"NO\":\"YES\"\n"}, {"source_code": "n = gets.to_i\ntop = gets.to_i\nbottom = 7 - top.to_i\nsides = []\nflag = true\nn.times do |i|\n side1, side2 = gets.chomp.split.map(&:to_i)\n if side1 == top || side1 == bottom || side2 == top ||side2 == bottom then\n flag = false\n end\nend\nputs flag ? \"YES\" : \"NO\" "}, {"source_code": "gets\n$u=gets.hex\nputs gets(p)=~/[#$u#{7-$u}]/?\"NO\":\"YES\""}, {"source_code": "gets\n$u=gets.hex\nputs gets(p)=~/[#$u#{7-$u}]/?\"NO\":\"YES\""}, {"source_code": "gets\n$u=gets.hex\nputs gets(p)=~/[#$u#{7-$u}]/?\"NO\":\"YES\"\n"}, {"source_code": "n = gets.to_i\nlast = gets.to_i\n1.upto(n) {\n buf = gets.split(\" \")\n a = buf[0].to_i\n b = buf[1].to_i\n if a == last || b == last || a == 7 - last || b == 7 - last\n print \"NO\"\n exit 0\n end\n}\nprint \"YES\""}, {"source_code": "n = gets.to_i\ntop = gets.to_i\n(1..n).each do\n left, right = gets.split.collect{|i| i.to_i}\n if left == top or 7-left == top or\\\n right == top or 7-right == top\n puts 'NO'\n exit\n end\nend\nputs 'YES'\n"}, {"source_code": "n = gets.to_i\nc = 7 - gets.to_i\nn.times do\n c = 7 - c\n a,b = gets.split.map &:to_i\n if a==c || a==7-c || b==7-c || b == c\n puts 'NO'\n exit\n end\nend\nputs \"YES\"\n"}, {"source_code": "gets\n$u=gets.hex\nputs gets(p)=~/[#$u#{7-$u}]/?\"NO\":\"YES\"\n"}, {"source_code": "def read_next_line\n gets.split.map { |x| x.to_i }\nend\n\nn = gets.to_i\nd = gets.to_i\nfor _ in 1..n\n a = read_next_line\n a.concat(a.map { |x| 7 - x })\n if a.include? d\n puts 'NO'\n exit\n end\nend\nputs 'YES'"}, {"source_code": "gets\nu = gets.to_i\nputs gets(p) =~ /[#{u}#{7-u}]/ ? \"NO\" : \"YES\"\n"}, {"source_code": "n = gets.to_i\nt = gets.to_i\n\nwhile t != -1 && s = gets\n\ta, b = s.split.map(&:to_i)\n\tc = 0\n\tc += 1 << a-1\n\tc += 1 << 6-a\n\tc += 1 << b-1\n\tc += 1 << 6-b\n\tif (c & 1<0,'2'=>1,'3'=>2,'4'=>3}\n\n\n\nfor i in '1'..'4'\n mp[i]=a[t[i]]\nend\n\nans =0\nfor i in 0...b.length\n ans+=mp[b[i]]\nend\nputs ans"}, {"source_code": "a=gets.split.map &:to_i;p gets.scan(/./).map{|s|a[s.to_i-1]}.inject(:+)\n"}, {"source_code": "a = [nil] + gets.split.map(&:to_i)\ns = gets.chomp.split(\"\").map(&:to_i)\nans = 0\ns.each do |x|\n ans += a[x]\nend\np ans\n"}, {"source_code": "a = [nil] + gets.split.map(&:to_i)\ns = gets.chomp.split(\"\").map(&:to_i)\nputs s.map{|x| a[x]}.inject(:+)\n"}, {"source_code": "a=gets.chomp.split(\" \")\nb=gets.chomp\nans=0\nb.each_char do |i|\nind=i.to_i-1\nans+=a[ind].to_i\nend\nputs \"#{ans}\""}, {"source_code": "a = [nil] + gets.split.map{|x| x.to_i}\nputs(gets.strip.split('').map{|x| x.to_i}.inject(0){|s, x| s += a[x]})\n"}, {"source_code": "a=gets.split.map &:to_i;p gets.scan(/./).map{|s|a[s.to_i-1]}.inject(:+)\n"}, {"source_code": "a = gets.split(' ').map(&:to_i).unshift(0)\n\nputs gets.chomp.chars.inject(0) { |s, x| s + a[x.to_i] }\n"}, {"source_code": "a=gets.split.map &:to_i;p gets.scan(/./).map{|s|a[s.to_i-1]}.inject(:+)\n"}, {"source_code": "a=gets.split(' ').map(&:to_i)\ns=gets.strip\nans =0\ns.each_char do |aa|\n ans+=a[aa.to_i-1]\nend\nputs ans\n"}, {"source_code": "a=gets.split.map &:to_i;p gets.scan(/./).map{|s|a[s.to_i-1]}.inject(:+)\n"}, {"source_code": "a = gets.split.map(&:to_i)\ns = gets.chomp\ncal = 0\ns.each_char do |c|\n cal += a[c.to_i - 1]\nend\nputs cal\n"}], "negative_code": [], "src_uid": "db9065d975878227a749083f0036a169"} {"nl": {"description": "The Smart Beaver from ABBYY plans a space travel on an ultramodern spaceship. During the voyage he plans to visit n planets. For planet i ai is the maximum number of suitcases that an alien tourist is allowed to bring to the planet, and bi is the number of citizens on the planet.The Smart Beaver is going to bring some presents from ABBYY to the planets he will be visiting. The presents are packed in suitcases, x presents in each. The Beaver will take to the ship exactly a1 + ... + an suitcases.As the Beaver lands on the i-th planet, he takes ai suitcases and goes out. On the first day on the planet the Beaver takes a walk and gets to know the citizens. On the second and all subsequent days the Beaver gives presents to the citizens — each of the bi citizens gets one present per day. The Beaver leaves the planet in the evening of the day when the number of presents left is strictly less than the number of citizens (i.e. as soon as he won't be able to give away the proper number of presents the next day). He leaves the remaining presents at the hotel.The Beaver is going to spend exactly c days traveling. The time spent on flights between the planets is considered to be zero. In how many ways can one choose the positive integer x so that the planned voyage will take exactly c days?", "input_spec": "The first input line contains space-separated integers n and c — the number of planets that the Beaver is going to visit and the number of days he is going to spend traveling, correspondingly. The next n lines contain pairs of space-separated integers ai, bi (1 ≤ i ≤ n) — the number of suitcases he can bring to the i-th planet and the number of citizens of the i-th planet, correspondingly. The input limitations for getting 30 points are: 1 ≤ n ≤ 100 1 ≤ ai ≤ 100 1 ≤ bi ≤ 100 1 ≤ c ≤ 100 The input limitations for getting 100 points are: 1 ≤ n ≤ 104 0 ≤ ai ≤ 109 1 ≤ bi ≤ 109 1 ≤ c ≤ 109 Due to possible overflow, it is recommended to use the 64-bit arithmetic. In some solutions even the 64-bit arithmetic can overflow. So be careful in calculations!", "output_spec": "Print a single number k — the number of ways to choose x so as to travel for exactly c days. If there are infinitely many possible values of x, print -1. Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.", "sample_inputs": ["2 5\n1 5\n2 4"], "sample_outputs": ["1"], "notes": "NoteIn the first example there is only one suitable value x = 5. Then the Beaver takes 1 suitcase with 5 presents to the first planet. Here he spends 2 days: he hangs around on the first day, and he gives away five presents on the second day. He takes 2 suitcases with 10 presents to the second planet. Here he spends 3 days — he gives away 4 presents on the second and the third days and leaves the remaining 2 presents at the hotel. In total, the Beaver spends 5 days traveling.For x = 4 or less the Beaver won't have enough presents for the second day on the first planet, so the voyage will end too soon. For x = 6 and more the Beaver will spend at least one more day on the second planet, and the voyage will take too long."}, "positive_code": [{"source_code": "def check(x)\n m = 0\n for i in (1..$n) do\n m+=$a[i][0]*x/$a[i][1]+1\n end\n if m>$c then return 1 end\n if m==$c then return 0 end\n if m<$c then return -1 end\nend\n\n$n,$c = gets.split.map{|e| e.to_i}\n$a = []\nfor i in (1..$n) do\n $a[i] = gets.split.map{|e| e.to_i}\nend\nl=0\nr=1\nc=check(r)\nwhile c!=1 do \n r*=2\n c=check(r)\nend\n\nm=0\nloop do\n if r-1==l then\n puts 0\n\texit\n end\n m = (l+r)/2\n c = check(m)\n if c==1\n r = m\n elsif c==-1\n l = m\n elsif c==0\n break\n end\nend\n\nl1 = r1 = m\nwhile r1-l>1 do\n z = (l+r1)/2\n if check(z)==0 then r1=z else l=z end\nend\n\nwhile r-l1>1 do\n z = (l1+r)/2\n if check(z)==0 then l1=z else r=z end\nend\n\nputs r-l-1"}, {"source_code": "# ABBYY Cup 2.0 - Easy\n# Problem E - Space Voyage\n$n, c = gets.split.map(&:to_i)\n$a, $b = [], []\n$n.times do\n a_i, b_i = gets.split.map(&:to_i)\n $a << a_i\n $b << b_i\nend\n\ndef get_value(x)\n ret = $n\n $n.times do |i|\n ret += $a[i] * x / $b[i]\n end\n ret \nend\n\nlow, high = 1, 10 ** 18 + 1\nwhile low < high\n middle = low + high >> 1\n if get_value(middle) >= c\n high = middle\n else\n low = middle + 1\n end\nend\nminimum = high\n\nlow, high = 0, 10 ** 18\nwhile low < high\n middle = low + high + 1 >> 1\n if get_value(middle) <= c\n low = middle\n else\n high = middle - 1\n end\nend\nmaximum = low\n\nanswer = [maximum - minimum + 1, 0].max\nanswer = -1 if answer == 10 ** 18\nputs answer\n"}, {"source_code": "def check(x)\n m = 0\n for i in (1..$n) do\n m+=$a[i][0]*x/$a[i][1]+1\n end\n if m>$c then return 1 end\n if m==$c then return 0 end\n if m<$c then return -1 end\nend\n\n$n,$c = gets.split.map{|e| e.to_i}\n$a = []\ns = 0\nfor i in (1..$n) do\n $a[i] = gets.split.map{|e| e.to_i}\n s+=$a[i][0]\nend\nif s==0 && $n==$c\n puts -1\n exit\nend\nif s==0 && $n!=$c \n puts 0\n exit\nend\nl=0\nr=1\nc=check(r)\nwhile c!=1 do \n r*=2\n c=check(r)\nend\n\nm=0\nloop do\n if r-1==l then\n puts 0\n\texit\n end\n m = (l+r)/2\n c = check(m)\n if c==1\n r = m\n elsif c==-1\n l = m\n elsif c==0\n break\n end\nend\n\nl1 = r1 = m\nwhile r1-l>1 do\n z = (l+r1)/2\n if check(z)==0 then r1=z else l=z end\nend\n\nwhile r-l1>1 do\n z = (l1+r)/2\n if check(z)==0 then l1=z else r=z end\nend\n\nputs r-l-1"}, {"source_code": "def check(x)\n m = 0\n for i in (1..$n) do\n m+=$a[i][0]*x/$a[i][1]+1\n if m>$c then break end\n end\n if m>$c then return 1 end\n if m==$c then return 0 end\n if m<$c then return -1 end\nend\n\n$n,$c = gets.split.map{|e| e.to_i}\n$a = []\ns = 0\nfor i in (1..$n) do\n $a[i] = gets.split.map{|e| e.to_i}\n s+=$a[i][0]\nend\nif s==0 && $n==$c\n puts -1\n exit\nend\nif s==0 && $n!=$c \n puts 0\n exit\nend\nl=0\nr=1\nc=check(r)\nwhile c!=1 do \n r*=2\n c=check(r)\nend\n\nm=0\nloop do\n if r-1==l then\n puts 0\n\texit\n end\n m = (l+r)/2\n c = check(m)\n if c==1\n r = m\n elsif c==-1\n l = m\n elsif c==0\n break\n end\nend\n\nl1 = r1 = m\nwhile r1-l>1 do\n z = (l+r1)/2\n if check(z)==0 then r1=z else l=z end\nend\n\nwhile r-l1>1 do\n z = (l1+r)/2\n if check(z)==0 then l1=z else r=z end\nend\n\nputs r-l-1\n"}, {"source_code": "def check(x)\n m = 0\n for i in (1..$n) do\n m+=$a[i][0]*x/$a[i][1]+1\n end\n if m>$c then return 1 end\n if m==$c then return 0 end\n if m<$c then return -1 end\nend\n\n$n,$c = gets.split.map{|e| e.to_i}\n$a = []\ns = 0\nfor i in (1..$n) do\n $a[i] = gets.split.map{|e| e.to_i}\n s+=$a[i][0]\nend\nif s==0 && $n==$c\n puts -1\n exit\nend\nif s==0 && $n!=$c \n puts 0\n exit\nend\nl=0\nr=1\nc=check(r)\nwhile c!=1 do \n r*=2\n c=check(r)\nend\n\nm=0\nloop do\n if r-1==l then\n puts 0\n\texit\n end\n m = (l+r)/2\n c = check(m)\n if c==1\n r = m\n elsif c==-1\n l = m\n elsif c==0\n break\n end\nend\n\nl1 = r1 = m\nwhile r1-l>1 do\n z = (l+r1)/2\n if check(z)==0 then r1=z else l=z end\nend\n\nwhile r-l1>1 do\n z = (l1+r)/2\n if check(z)==0 then l1=z else r=z end\nend\n\nputs r-l-1\n"}, {"source_code": "$n, c = gets.split.map(&:to_i)\n$a, $b = [], []\n$n.times do\n a_i, b_i = gets.split.map(&:to_i)\n $a << a_i\n $b << b_i\nend\n\ndef get_value(x)\n ret = $n\n $n.times do |i|\n ret += $a[i] * x / $b[i]\n end\n ret \nend\n\nlow, high = 1, 10 ** 18 + 1\nwhile low < high\n middle = low + high >> 1\n if get_value(middle) >= c\n high = middle\n else\n low = middle + 1\n end\nend\nminimum = high\n\nlow, high = 0, 10 ** 18\nwhile low < high\n middle = low + high + 1 >> 1\n if get_value(middle) <= c\n low = middle\n else\n high = middle - 1\n end\nend\nmaximum = low\n\nanswer = [maximum - minimum + 1, 0].max\nanswer = -1 if answer == 10 ** 18\nputs answer\n"}], "negative_code": [{"source_code": "def check(x)\n m = 0\n for i in (1..$n) do\n m+=$a[i][0]*x/$a[i][1]+1\n end\n m>$c\nend\ndef check2(x)\n m = 0\n for i in (1..$n) do\n m+=$a[i][0]*x/$a[i][1]+1\n end\n m>=$c\nend\n\n$n,$c = gets.split.map{|e| e.to_i}\n$a = []\nfor i in (1..$n) do\n $a[i] = gets.split.map{|e| e.to_i}\nend\nl=1\nr=1\nc=check(r)\nwhile !c do \n r*=2\n c=check(r)\nend\nl2 = l1 = l\nr2 = r1 = r \nwhile r1-l1>1 do\n m = (l1+r1)/2\n if check(m) then r1=m else l1=m end\nend\nwhile r2-l2>1 do\n m = (l2+r2)/2\n if check2(m) then r2=m else l2=m end\nend\nans = l1-r2+1\nputs ans\n\n"}, {"source_code": "def check(x)\n m = 0\n for i in (1..$n) do\n m+=$a[i][0]*x/$a[i][1]+1\n end\n m>$c\nend\ndef check2(x)\n m = 0\n for i in (1..$n) do\n m+=$a[i][0]*x/$a[i][1]+1\n end\n m>=$c\nend\n\ndef check3(x)\n m = 0\n for i in (1..$n) do\n m+=$a[i][0]*x/$a[i][1]+1\n end\n m==$c\nend\n\n$n,$c = gets.split.map{|e| e.to_i}\n$a = []\nfor i in (1..$n) do\n $a[i] = gets.split.map{|e| e.to_i}\nend\nl=1\nr=1\nc=check(r)\nwhile !c do \n r*=2\n c=check(r)\nend\nl2 = l1 = l\nr2 = r1 = r \nwhile r1-l1>1 do\n m = (l1+r1)/2\n if check(m) then r1=m else l1=m end\nend\nwhile r2-l2>1 do\n m = (l2+r2)/2\n if check2(m) then r2=m else l2=m end\nend\nans = l1-r2+1\nif l1==r2 then\n c=check3(r2)\n if c then ans=1 else ans=0 end\nend\n\nputs ans\n\n"}, {"source_code": "def check(x)\n m = 0\n for i in (1..$n) do\n m+=$a[i][0]*x/$a[i][1]+1\n end\n if m>$c then return 1 end\n if m==$c then return 0 end\n if m<$c then return -1 end\nend\n\n$n,$c = gets.split.map{|e| e.to_i}\n$a = []\nfor i in (1..$n) do\n $a[i] = gets.split.map{|e| e.to_i}\nend\nl=0\nr=1\nc=check(r)\nwhile c!=1 do \n r*=2\n c=check(r)\nend\n\nm=0\nputs l,r\nloop do\n if r-1==l then\n puts 0\n\thalt\n end\n m = (l+r)/2\n c = check(m)\n if c==1\n r = m\n elsif c==-1\n l = m\n elsif c==0\n break\n end\nend\n\nl1 = r1 = m\nwhile r1-l>1 do\n z = (l+r1)/2\n if check(z)==0 then r1=z else l=z end\nend\n\nwhile r-l1>1 do\n z = (l1+r)/2\n if check(z)==0 then l1=z else r=z end\nend\n\nputs l-r-1"}, {"source_code": "def check(x)\n m = 0\n for i in (1..$n) do\n m+=$a[i][0]*x/$a[i][1]+1\n end\n m>$c\nend\ndef check2(x)\n m = 0\n for i in (1..$n) do\n m+=$a[i][0]*x/$a[i][1]+1\n end\n m>=$c\nend\n\n$n,$c = gets.split.map{|e| e.to_i}\n$a = []\nfor i in (1..$n) do\n $a[i] = gets.split.map{|e| e.to_i}\nend\nl=1\nr=1\nc=check(r)\nwhile !c do \n r*=2\n c=check(r)\nend\nl2 = l1 = l\nr2 = r1 = r \nwhile r1-l1>1 do\n m = (l1+r1)/2\n if check(m) then r1=m else l1=m end\nend\nwhile r2-l2>1 do\n m = (l2+r2)/2\n if check2(m) then r2=m else l2=m end\nend\nans = l1-r2+1\nif l1==r2 then\n c=check(r2)\n if c then ans=1 else ans=0 end\nend\n\nputs ans\n\n"}, {"source_code": "def check(x)\n m = ($oo*x+0.000001).to_i + $n\n if m>$c then return 1 end\n if m==$c then return 0 end\n if m<$c then return -1 end\nend\n\n$n,$c = gets.split.map{|e| e.to_i}\n$oo = 0\nfor i in (1..$n) do\n a = gets.split.map{|e| e.to_f}\n $oo+= a[0]/a[1]\nend\nl=0\nr=1\nc = check(r)\nwhile c!=1 do \n r*=2\n c = check(r)\nend\n\nm=0\nloop do\n if r-1==l then\n puts 0\n exit\n end\n m = (l+r)/2\n c = check(m)\n if c==1\n r = m\n elsif c==-1\n l = m\n elsif c==0\n break\n end\nend\n\nl1 = r1 = m\nwhile r1-l>1 do\n z = (l+r1)/2\n c = check(z)\n if c==0 then r1=z else l=z end\nend\n\nwhile r-l1>1 do\n z = (l1+r)/2\n c = check(z)\n if c==0 then l1=z else r=z end\nend\n\nputs r-l-1\n"}, {"source_code": "def check(x)\n m = ($oo*x+0.00000001).to_i + $n\n if m>$c then return 1 end\n if m==$c then return 0 end\n if m<$c then return -1 end\nend\n\n$n,$c = gets.split.map{|e| e.to_i}\n$oo = 0\nfor i in (1..$n) do\n a = gets.split.map{|e| e.to_f}\n $oo+= a[0]/a[1]\nend\nl=0\nr=1\nc = check(r)\nwhile c!=1 do \n r*=2\n c = check(r)\nend\n\nm=0\nloop do\n if r-1==l then\n puts 0\n exit\n end\n m = (l+r)/2\n c = check(m)\n if c==1\n r = m\n elsif c==-1\n l = m\n elsif c==0\n break\n end\nend\n\nl1 = r1 = m\nwhile r1-l>1 do\n z = (l+r1)/2\n c = check(z)\n if c==0 then r1=z else l=z end\nend\n\nwhile r-l1>1 do\n z = (l1+r)/2\n c = check(z)\n if c==0 then l1=z else r=z end\nend\n\nputs r-l-1\n"}, {"source_code": "def check(x)\n m = 0\n for i in (1..$n) do\n m+=($a[i]*x+1.0000001).to_i\n if m>$c then break end\n end\n if m>$c then return 1 end\n if m==$c then return 0 end\n if m<$c then return -1 end\nend\n\n$n,$c = gets.split.map{|e| e.to_i}\n$a = []\nfor i in (1..$n) do\n a = gets.split.map{|e| e.to_f}\n $a[i] = a[0]/a[1]\nend\nl=0\nr=1\nc = check(r)\nwhile c!=1 do \n r*=2\n c = check(r)\nend\n\nm=0\nloop do\n if r-1==l then\n puts 0\n exit\n end\n m = (l+r)/2\n c = check(m)\n if c==1\n r = m\n elsif c==-1\n l = m\n elsif c==0\n break\n end\nend\n\nl1 = r1 = m\nwhile r1-l>1 do\n z = (l+r1)/2\n c = check(z)\n if c==0 then r1=z else l=z end\nend\n\nwhile r-l1>1 do\n z = (l1+r)/2\n c = check(z)\n if c==0 then l1=z else r=z end\nend\n\nputs r-l-1\n"}, {"source_code": "def check(x)\n m = 0\n for i in (1..$n) do\n if $a[i][0]==0 then\n m+=1 \n else\n m+=$a[i][0]*x/$a[i][1]+1\n end\n if m>$c then break end\n end\n if m>$c then return 1 end\n if m==$c then return 0 end\n if m<$c then return -1 end\nend\n\n$n,$c = gets.split.map{|e| e.to_i}\n$a = []\nfor i in (1..$n) do\n $a[i] = gets.split.map{|e| e.to_i}\nend\nl=0\nr=1\nc = check(r)\nwhile c!=1 do \n r*=2\n c = check(r)\nend\n\nm=0\nloop do\n if r-1==l then\n puts 0\n exit\n end\n m = (l+r)/2\n c = check(m)\n if c==1\n r = m\n elsif c==-1\n l = m\n elsif c==0\n break\n end\nend\n\nl1 = r1 = m\nwhile r1-l>1 do\n z = (l+r1)/2\n c = check(m)\n if c==0 then r1=z else l=z end\nend\n\nwhile r-l1>1 do\n z = (l1+r)/2\n c = check(m)\n if c==0 then l1=z else r=z end\nend\n\nputs r-l-1\n"}, {"source_code": "def check(x)\n m = 0\n for i in (1..$n) do\n m+=($a[i]*x+1.000001).to_i\n if m>$c then break end\n end\n if m>$c then return 1 end\n if m==$c then return 0 end\n if m<$c then return -1 end\nend\n\n$n,$c = gets.split.map{|e| e.to_i}\n$a = []\nfor i in (1..$n) do\n a = gets.split.map{|e| e.to_f}\n $a[i] = a[0]/a[1]\nend\nl=0\nr=1\nc = check(r)\nwhile c!=1 do \n r*=2\n c = check(r)\nend\n\nm=0\nloop do\n if r-1==l then\n puts 0\n exit\n end\n m = (l+r)/2\n c = check(m)\n if c==1\n r = m\n elsif c==-1\n l = m\n elsif c==0\n break\n end\nend\n\nl1 = r1 = m\nwhile r1-l>1 do\n z = (l+r1)/2\n c = check(z)\n if c==0 then r1=z else l=z end\nend\n\nwhile r-l1>1 do\n z = (l1+r)/2\n c = check(z)\n if c==0 then l1=z else r=z end\nend\n\nputs r-l-1\n"}, {"source_code": "def check(x)\n m = 0\n for i in (1..$n) do\n m+=($a[i]*x+1.00000000000000001).to_i\n if m>$c then break end\n end\n if m>$c then return 1 end\n if m==$c then return 0 end\n if m<$c then return -1 end\nend\n\n$n,$c = gets.split.map{|e| e.to_i}\n$a = []\nfor i in (1..$n) do\n a = gets.split.map{|e| e.to_f}\n $a[i] = a[0]/a[1]\nend\nl=0\nr=1\nc = check(r)\nwhile c!=1 do \n r*=2\n c = check(r)\nend\n\nm=0\nloop do\n if r-1==l then\n puts 0\n exit\n end\n m = (l+r)/2\n c = check(m)\n if c==1\n r = m\n elsif c==-1\n l = m\n elsif c==0\n break\n end\nend\n\nl1 = r1 = m\nwhile r1-l>1 do\n z = (l+r1)/2\n c = check(z)\n if c==0 then r1=z else l=z end\nend\n\nwhile r-l1>1 do\n z = (l1+r)/2\n c = check(z)\n if c==0 then l1=z else r=z end\nend\n\nputs r-l-1\n"}, {"source_code": "def check(x)\n m = ($oo*x+0.0000000000001).to_i + $n\n if m>$c then return 1 end\n if m==$c then return 0 end\n if m<$c then return -1 end\nend\n\n$n,$c = gets.split.map{|e| e.to_i}\n$oo = 0\nfor i in (1..$n) do\n a = gets.split.map{|e| e.to_f}\n $oo+= a[0]/a[1]\nend\nl=0\nr=1\nc = check(r)\nwhile c!=1 do \n r*=2\n c = check(r)\nend\n\nm=0\nloop do\n if r-1==l then\n puts 0\n exit\n end\n m = (l+r)/2\n c = check(m)\n if c==1\n r = m\n elsif c==-1\n l = m\n elsif c==0\n break\n end\nend\n\nl1 = r1 = m\nwhile r1-l>1 do\n z = (l+r1)/2\n c = check(z)\n if c==0 then r1=z else l=z end\nend\n\nwhile r-l1>1 do\n z = (l1+r)/2\n c = check(z)\n if c==0 then l1=z else r=z end\nend\n\nputs r-l-1\n"}, {"source_code": "def check(x)\n m = ($oo*x).to_i + $n\n if m>$c then return 1 end\n if m==$c then return 0 end\n if m<$c then return -1 end\nend\n\n$n,$c = gets.split.map{|e| e.to_i}\n$oo = 0.to_r\nfor i in (1..$n) do\n a = gets.split.map{|e| e.to_r}\n $oo+= a[0]/a[1]\nend\nl=0\nr=1\nc = check(r)\nwhile c!=1 do \n r*=2\n c = check(r)\nend\n\nm=0\nloop do\n if r-1==l then\n puts 0\n exit\n end\n m = (l+r)/2\n c = check(m)\n if c==1\n r = m\n elsif c==-1\n l = m\n elsif c==0\n break\n end\nend\n\nl1 = r1 = m\nwhile r1-l>1 do\n z = (l+r1)/2\n c = check(z)\n if c==0 then r1=z else l=z end\nend\n\nwhile r-l1>1 do\n z = (l1+r)/2\n c = check(z)\n if c==0 then l1=z else r=z end\nend\n\nputs r-l-1\n"}, {"source_code": "def check(x)\n m = 0\n for i in (1..$n) do\n m+=($a[i]*x+1.000000001).to_i\n if m>$c then break end\n end\n if m>$c then return 1 end\n if m==$c then return 0 end\n if m<$c then return -1 end\nend\n\n$n,$c = gets.split.map{|e| e.to_i}\n$a = []\nfor i in (1..$n) do\n a = gets.split.map{|e| e.to_f}\n $a[i] = a[0]/a[1]\nend\nl=0\nr=1\nc = check(r)\nwhile c!=1 do \n r*=2\n c = check(r)\nend\n\nm=0\nloop do\n if r-1==l then\n puts 0\n exit\n end\n m = (l+r)/2\n c = check(m)\n if c==1\n r = m\n elsif c==-1\n l = m\n elsif c==0\n break\n end\nend\n\nl1 = r1 = m\nwhile r1-l>1 do\n z = (l+r1)/2\n c = check(z)\n if c==0 then r1=z else l=z end\nend\n\nwhile r-l1>1 do\n z = (l1+r)/2\n c = check(z)\n if c==0 then l1=z else r=z end\nend\n\nputs r-l-1\n"}, {"source_code": "def check(x)\n m = 0\n for i in (1..$n) do\n m+=($a[i]*x+1.0000000000001).to_i\n if m>$c then break end\n end\n if m>$c then return 1 end\n if m==$c then return 0 end\n if m<$c then return -1 end\nend\n\n$n,$c = gets.split.map{|e| e.to_i}\n$a = []\nfor i in (1..$n) do\n a = gets.split.map{|e| e.to_f}\n $a[i] = a[0]/a[1]\nend\nl=0\nr=1\nc = check(r)\nwhile c!=1 do \n r*=2\n c = check(r)\nend\n\nm=0\nloop do\n if r-1==l then\n puts 0\n exit\n end\n m = (l+r)/2\n c = check(m)\n if c==1\n r = m\n elsif c==-1\n l = m\n elsif c==0\n break\n end\nend\n\nl1 = r1 = m\nwhile r1-l>1 do\n z = (l+r1)/2\n c = check(z)\n if c==0 then r1=z else l=z end\nend\n\nwhile r-l1>1 do\n z = (l1+r)/2\n c = check(z)\n if c==0 then l1=z else r=z end\nend\n\nputs r-l-1\n"}, {"source_code": "def check(x)\n m = 0\n for i in (1..$n) do\n m+=($a[i]*x+1.00000000001).to_i\n if m>$c then break end\n end\n if m>$c then return 1 end\n if m==$c then return 0 end\n if m<$c then return -1 end\nend\n\n$n,$c = gets.split.map{|e| e.to_i}\n$a = []\nfor i in (1..$n) do\n a = gets.split.map{|e| e.to_f}\n $a[i] = a[0]/a[1]\nend\nl=0\nr=1\nc = check(r)\nwhile c!=1 do \n r*=2\n c = check(r)\nend\n\nm=0\nloop do\n if r-1==l then\n puts 0\n exit\n end\n m = (l+r)/2\n c = check(m)\n if c==1\n r = m\n elsif c==-1\n l = m\n elsif c==0\n break\n end\nend\n\nl1 = r1 = m\nwhile r1-l>1 do\n z = (l+r1)/2\n c = check(z)\n if c==0 then r1=z else l=z end\nend\n\nwhile r-l1>1 do\n z = (l1+r)/2\n c = check(z)\n if c==0 then l1=z else r=z end\nend\n\nputs r-l-1\n"}, {"source_code": "$n, c = gets.split.map(&:to_i)\n$a, $b = [], []\n$n.times do\n a_i, b_i = gets.split.map(&:to_i)\n $a << a_i\n $b << b_i\nend\n\ndef get_value(x)\n ret = $n\n $n.times do |i|\n ret += $a[i] * x / $b[i]\n end\n ret \nend\n\nlow, high = 0, 10 ** 18\nwhile low < high\n middle = low + high >> 1\n if get_value(middle) >= c\n high = middle\n else\n low = middle + 1\n end\nend\nminimum = high\n\nlow, high = 0, 10 ** 18\nwhile low < high\n middle = low + high + 1 >> 1\n if get_value(middle) <= c\n low = middle\n else\n high = middle - 1\n end\nend\nmaximum = low\n\nputs [maximum - minimum + 1, 0].max\n"}, {"source_code": "$n, c = gets.split.map(&:to_i)\n$a, $b = [], []\n$n.times do\n a_i, b_i = gets.split.map(&:to_i)\n $a << a_i\n $b << b_i\nend\n\ndef get_value(x)\n ret = $n\n $n.times do |i|\n ret += $a[i] * x / $b[i]\n end\n ret \nend\n\nlow, high = 1, 10 ** 18\nwhile low < high\n middle = low + high >> 1\n if get_value(middle) >= c\n high = middle\n else\n low = middle + 1\n end\nend\nminimum = high\n\nlow, high = 0, 10 ** 18\nwhile low < high\n middle = low + high + 1 >> 1\n if get_value(middle) <= c\n low = middle\n else\n high = middle - 1\n end\nend\nmaximum = low\n\nputs [maximum - minimum + 1, 0].max\n"}, {"source_code": "$n, c = gets.split.map(&:to_i)\n$a, $b = [], []\n$n.times do\n a_i, b_i = gets.split.map(&:to_i)\n $a << a_i\n $b << b_i\nend\n\ndef get_value(x)\n ret = $n\n $n.times do |i|\n ret += $a[i] * x / $b[i]\n end\n ret \nend\n\nlow, high = 1, 10 ** 18\nwhile low < high\n middle = low + high >> 1\n if get_value(middle) >= c\n high = middle\n else\n low = middle + 1\n end\nend\nminimum = high\n\nlow, high = 0, 10 ** 18\nwhile low < high\n middle = low + high + 1 >> 1\n if get_value(middle) <= c\n low = middle\n else\n high = middle - 1\n end\nend\nmaximum = low\n\nanswer = [maximum - minimum + 1, 0].max\nanswer = -1 if answer == 10 ** 18\nputs answer\n"}, {"source_code": "$n, c = gets.split.map(&:to_i)\n$a, $b = [], []\n$n.times do\n a_i, b_i = gets.split.map(&:to_i)\n $a << a_i\n $b << b_i\nend\n\ndef get_value(x)\n ret = $n\n $n.times do |i|\n ret += $a[i] * x / $b[i]\n end\n ret \nend\n\nlow, high = -1, 10 ** 18\nwhile low < high\n middle = low + high >> 1\n if get_value(middle) >= c\n high = middle\n else\n low = middle + 1\n end\nend\nminimum = high\n\nlow, high = -1, 10 ** 18\nwhile low < high\n middle = low + high + 1 >> 1\n if get_value(middle) <= c\n low = middle\n else\n high = middle - 1\n end\nend\nmaximum = low\n\nputs [maximum - minimum + 1, 0].max\n"}, {"source_code": "$n, c = gets.split.map(&:to_i)\n$a, $b = [], []\n$n.times do\n a_i, b_i = gets.split.map(&:to_i)\n $a << a_i\n $b << b_i\nend\n\ndef get_value(x)\n ret = $n\n $n.times do |i|\n ret += $a[i] * x / $b[i]\n end\n ret \nend\n\nlow, high = 0, 10 ** 18\nwhile low < high\n middle = low + high >> 1\n if get_value(middle) >= c\n high = middle\n else\n low = middle + 1\n end\nend\nminimum = high\n\nlow, high = -1, 10 ** 18\nwhile low < high\n middle = low + high + 1 >> 1\n if get_value(middle) <= c\n low = middle\n else\n high = middle - 1\n end\nend\nmaximum = low\n\nputs [maximum - minimum + 1, 0].max\n"}], "src_uid": "9e17daaa5ca2297673b8a2a9db497471"} {"nl": {"description": "Fox Ciel and her friends are in a dancing room. There are n boys and m girls here, and they never danced before. There will be some songs, during each song, there must be exactly one boy and one girl are dancing. Besides, there is a special rule: either the boy in the dancing pair must dance for the first time (so, he didn't dance with anyone before); or the girl in the dancing pair must dance for the first time. Help Fox Ciel to make a schedule that they can dance as many songs as possible.", "input_spec": "The first line contains two integers n and m (1 ≤ n, m ≤ 100) — the number of boys and girls in the dancing room.", "output_spec": "In the first line print k — the number of songs during which they can dance. Then in the following k lines, print the indexes of boys and girls dancing during songs chronologically. You can assume that the boys are indexed from 1 to n, and the girls are indexed from 1 to m.", "sample_inputs": ["2 1", "2 2"], "sample_outputs": ["2\n1 1\n2 1", "3\n1 1\n1 2\n2 2"], "notes": "NoteIn test case 1, there are 2 boys and 1 girl. We can have 2 dances: the 1st boy and 1st girl (during the first song), the 2nd boy and 1st girl (during the second song).And in test case 2, we have 2 boys with 2 girls, the answer is 3."}, "positive_code": [{"source_code": "n, m = gets.split(' ').map(&:to_i)\nputs n+m-1\n(1..n).each{|x| puts \"#{x} 1\"}\n(2..m).each{|x| puts \"1 #{x}\"}"}, {"source_code": "a, b = gets.split.map(&:to_i)\np a + b - 1\nputs \"1 1\"\n2.upto(a) do |i|\n puts \"#{i} 1\"\nend\n2.upto(b) do |i|\n puts \"1 #{i}\"\nend\n"}, {"source_code": "#!/usr/bin/env ruby\nn, m = gets.split(' ').map(&:to_i)\nputs n + m - 1\nn.times do |i|\n puts \"#{i + 1} 1\"\nend\n(m - 1).times do |i|\n puts \"1 #{i + 2}\"\nend"}, {"source_code": "require \"Matrix\"\n\nh=Matrix\nn, m = gets.split.map{|i| i.to_i}\nputs(n+m-1)\nfor i in 1..m\n puts(\"1 #{i}\")\nend\nfor i in 2..n\n puts(\"#{i} 1\")\nend"}, {"source_code": "n, m = gets.split.map{|i| i.to_i}\nputs(n+m-1)\nfor i in 1..m\n puts(\"1 #{i}\")\nend\nfor i in 2..n\n puts(\"#{i} 1\")\nend"}, {"source_code": "n = gets(\" \")\nm = gets\nn = n.to_i\nm = m.to_i\nputs m + n - 1\nm.times { |x| print \"1 #{x+1}\\n\"}\n(n-1).times { |x| \n\tprint \"#{x+2} #{m > 1 ? 2 : 1}\\n\"}"}, {"source_code": "def run\n n, m = $stdin.readline.split.map(&:to_i)\n\n puts n + m - 1\n\n n.times do |i|\n puts \"#{i + 1} 1\"\n end\n\n if n == 1\n (m - 1).times do |i|\n puts \"1 #{i + 2}\"\n end\n elsif m > 1\n (m - 1).times do |i|\n puts \"2 #{i + 2}\"\n end\n end\nend\n\nrun if $0 == __FILE__\n"}, {"source_code": "def run\n n, m = $stdin.readline.split.map(&:to_i)\n\n puts n + m - 1\n\n n.times do |i|\n puts \"#{i + 1} 1\"\n end\n\n (m - 1).times do |i|\n if n == 1\n puts \"1 #{i + 2}\"\n else\n puts \"2 #{i + 2}\"\n end\n end\nend\n\nrun if $0 == __FILE__\n"}, {"source_code": "class A\n def initialize\n n, m = gets.chomp.split(' ').map(&:to_i)\n\n res = n+m-1\n ans = []\n\n 1.upto(m) do |index|\n ans << [1,index]\n end\n\n 2.upto(n) do |index|\n ans << [index, m]\n end\n\n puts ans.size\n ans.each do |a|\n puts a.join(' ')\n end\n end\n\nend\n\na = A.new"}, {"source_code": "a=gets.chomp.split(\" \")\nans=-1\na=a.each do |i| \na[a.index(i)]=i.to_i\nans+=i.to_i\nend\nt=0\nif a[0]>a[1]\nt=1\nend\na.sort!\na.reverse!\nputs \"#{ans}\"\nn=0\nm=1\ntrig1=1\ntrig2=-1\nloop do \nif n0\nn+=1\nend\nif m0\nm+=1\nend\ntrig1=trig1*(-1)\ntrig2=trig1*(-1)\nif t== 1\nputs \"#{n} #{m}\"\nelse \nputs \"#{m} #{n}\"\nend\nif m==a[1]\ntrig1=2\nend\nbreak if n==a[0] && m==a[1]\nend\n"}, {"source_code": "n,m = gets.split.map &:to_i\ncant,ln,lm = 0,1,1\nnn = false\na = []\nwhile true\n cant+=1\n a<< \"#{ln} #{lm}\"\n som = false\n if nn then\n if lna[1]\nt=1\nend\na.sort!\na.reverse!\nputs \"#{ans}\"\nn=0\nm=1\ntrig1=1\ntrig2=-1\nloop do \nif n0\nn+=1\nend\nif m0\nm+=1\nend\ntrig1=trig1*(-1)\ntrig2=trig1*(-1)\nif t== 1\nputs \"#{n} #{m}\"\nelse \nputs \"#{m} #{n}\"\nend\nif m==a[1]\ntrig1=2\nend\nbreak if n==a[0] && m==a[1]\nend"}, {"source_code": "n, m = gets.split.map(&:to_i)\n\nputs n + m - 1\n\nm.times { |i| puts \"1 #{i+1}\" }\n(n-1).times { |i| puts \"#{i+2} 1\" }\n\n"}, {"source_code": "m, n = gets.chomp.split.map { |x| x.to_i }\nputs n + m - 1\nfor i in 1..n\n puts \"1 #{i}\"\nend\nfor i in 2..m\n puts \"#{i} 1\"\nend"}, {"source_code": "boys, girls = gets.chomp.split.map(&:to_i)\nputs boys + girls - 1\n(1..girls).each { |e| puts \"1 #{e}\" }\n(2..boys).each { |e| puts \"#{e} 1\" }"}, {"source_code": "n, m = gets.split.map(&:to_i)\nputs n + m - 1\nputs [*1..n].map{|i| i.to_s+\" 1\"}\nputs [*2..m].map{|i| \"1 \"+i.to_s}\n"}], "negative_code": [{"source_code": "#!/usr/bin/env ruby\nn, m = gets.split(' ').map(&:to_i)\nputs n + m - 1"}, {"source_code": "n = gets(\" \")\nm = gets\nn = n.to_i\nm = m.to_i\nputs m + n - 1\nm.times { |x| print \"1 #{x+1}\\n\"}\n(n-1).times { |x| print \"#{x+2} 2\\n\"}"}, {"source_code": "n = gets(\" \")\nm = gets\nn = n.to_i\nm = m.to_i\nputs m + n - 1\nif m < n\n\tm, n = n, m\nend\nm.times { |x| print \"1 #{x+1}\\n\"}\n(n-1).times { |x| print \"#{x+2} 2\\n\"}"}, {"source_code": "def run\n n, m = $stdin.readline.split.map(&:to_i)\n\n puts n + m - 1\n\n n.times do |i|\n puts \"#{i + 1} 1\"\n end\n\n if m > 1\n (m - 1).times do |i|\n puts \"2 #{i + 2}\"\n end\n end\nend\n\nrun if $0 == __FILE__\n"}, {"source_code": "a=gets.chomp.split(\" \")\nans=-1\na=a.each do |i| \na[a.index(i)]=i.to_i\nans+=i.to_i\nend\na.sort!\na.reverse!\nputs \"#{ans}\"\n#puts \"1 1\"\nn=0\nm=1\ntrig1=1\ntrig2=-1\nloop do \nif n0\nn+=1\nend\nif m0\nm+=1\nend\ntrig1=trig1*(-1)\ntrig2=trig1*(-1)\nputs \"#{n} #{m}\"\nif m==a[1]\ntrig1=2\nend\nbreak if n==a[0] && m==a[1]\nend\n"}, {"source_code": "#!/usr/bin/ruby\ninclude Math\n\nn,m = gets.split.map(&:to_i)\nret = []\nfor i in 1..n\n for j in 1..m\n ret << [i,j].join(\" \")\n end\nend\nputs ret.size\nputs ret.join \"\\n\"\n"}], "src_uid": "14fc3a7fef44a02791aee62838c4c8c8"} {"nl": {"description": "A row of $$$n$$$ cells is given, all initially white. Using a stamp, you can stamp any two neighboring cells such that one becomes red and the other becomes blue. A stamp can be rotated, i.e. it can be used in both ways: as $$$\\color{blue}{\\texttt{B}}\\color{red}{\\texttt{R}}$$$ and as $$$\\color{red}{\\texttt{R}}\\color{blue}{\\texttt{B}}$$$.During use, the stamp must completely fit on the given $$$n$$$ cells (it cannot be partially outside the cells). The stamp can be applied multiple times to the same cell. Each usage of the stamp recolors both cells that are under the stamp.For example, one possible sequence of stamps to make the picture $$$\\color{blue}{\\texttt{B}}\\color{red}{\\texttt{R}}\\color{blue}{\\texttt{B}}\\color{blue}{\\texttt{B}}\\texttt{W}$$$ could be $$$\\texttt{WWWWW} \\to \\texttt{WW}\\color{brown}{\\underline{\\color{red}{\\texttt{R}}\\color{blue}{\\texttt{B}}}}\\texttt{W} \\to \\color{brown}{\\underline{\\color{blue}{\\texttt{B}}\\color{red}{\\texttt{R}}}}\\color{red}{\\texttt{R}}\\color{blue}{\\texttt{B}}\\texttt{W} \\to \\color{blue}{\\texttt{B}}\\color{brown}{\\underline{\\color{red}{\\texttt{R}}\\color{blue}{\\texttt{B}}}}\\color{blue}{\\texttt{B}}\\texttt{W}$$$. Here $$$\\texttt{W}$$$, $$$\\color{red}{\\texttt{R}}$$$, and $$$\\color{blue}{\\texttt{B}}$$$ represent a white, red, or blue cell, respectively, and the cells that the stamp is used on are marked with an underline.Given a final picture, is it possible to make it using the stamp zero or more times?", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$) — the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$1 \\leq n \\leq 10^5$$$) — the length of the picture. The second line of each test case contains a string $$$s$$$ — the picture you need to make. It is guaranteed that the length of $$$s$$$ is $$$n$$$ and that $$$s$$$ only consists of the characters $$$\\texttt{W}$$$, $$$\\texttt{R}$$$, and $$$\\texttt{B}$$$, representing a white, red, or blue cell, respectively. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.", "output_spec": "Output $$$t$$$ lines, each of which contains the answer to the corresponding test case. As an answer, output \"YES\" if it possible to make the picture using the stamp zero or more times, and \"NO\" otherwise. You can output the answer in any case (for example, the strings \"yEs\", \"yes\", \"Yes\" and \"YES\" will be recognized as a positive answer).", "sample_inputs": ["12\n5\nBRBBW\n1\nB\n2\nWB\n2\nRW\n3\nBRB\n3\nRBB\n7\nWWWWWWW\n9\nRBWBWRRBW\n10\nBRBRBRBRRB\n12\nBBBRWWRRRWBR\n10\nBRBRBRBRBW\n5\nRBWBW"], "sample_outputs": ["YES\nNO\nNO\nNO\nYES\nYES\nYES\nNO\nYES\nNO\nYES\nNO"], "notes": "NoteThe first test case is explained in the statement.For the second, third, and fourth test cases, it is not possible to stamp a single cell, so the answer is \"NO\".For the fifth test case, you can use the stamp as follows: $$$\\texttt{WWW} \\to \\texttt{W}\\color{brown}{\\underline{\\color{red}{\\texttt{R}}\\color{blue}{\\texttt{B}}}} \\to \\color{brown}{\\underline{\\color{blue}{\\texttt{B}}\\color{red}{\\texttt{R}}}}\\color{blue}{\\texttt{B}}$$$.For the sixth test case, you can use the stamp as follows: $$$\\texttt{WWW} \\to \\texttt{W}\\color{brown}{\\underline{\\color{red}{\\texttt{R}}\\color{blue}{\\texttt{B}}}} \\to \\color{brown}{\\underline{\\color{red}{\\texttt{R}}\\color{blue}{\\texttt{B}}}}\\color{blue}{\\texttt{B}}$$$.For the seventh test case, you don't need to use the stamp at all."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n gets\r\n s = gets.chomp\r\n ok = s.split(\"W\").reject(&:empty?).all? { |s| s.include?(\"R\") && s.include?(\"B\") }\r\n puts ok ? \"YES\" : \"NO\"\r\nend\r\n"}, {"source_code": "def isvalid(s)\n n = s.size\n return true if n.zero?\n return false if n == 1\n\n for i in 0..n - 2\n return true unless s[i] == s[i + 1]\n end\n false\nend\n\ndef solve\n gets\n f = true\n gets.chomp.split('W').each do |s|\n f = false unless isvalid(s)\n end\n puts f ? :YES : :NO\nend\n\ngets.to_i.times do\n solve\nend\n"}], "negative_code": [], "src_uid": "3f284afb044c8a57a02cd015d06e0ef0"} {"nl": {"description": "A string is called square if it is some string written twice in a row. For example, the strings \"aa\", \"abcabc\", \"abab\" and \"baabaa\" are square. But the strings \"aaa\", \"abaaab\" and \"abcdabc\" are not square.For a given string $$$s$$$ determine if it is square.", "input_spec": "The first line of input data contains an integer $$$t$$$ ($$$1 \\le t \\le 100$$$) —the number of test cases. This is followed by $$$t$$$ lines, each containing a description of one test case. The given strings consist only of lowercase Latin letters and have lengths between $$$1$$$ and $$$100$$$ inclusive.", "output_spec": "For each test case, output on a separate line: YES if the string in the corresponding test case is square, NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).", "sample_inputs": ["10\na\naa\naaa\naaaa\nabab\nabcabc\nabacaba\nxxyy\nxyyx\nxyxy"], "sample_outputs": ["NO\nYES\nNO\nYES\nYES\nYES\nNO\nNO\nNO\nYES"], "notes": null}, "positive_code": [{"source_code": "t = gets.chomp.to_i\r\nt.times do |n|\r\n lin = gets.chomp\r\n ln = lin.size\r\n lin1 = \"\"\r\n lin2 = \"\"\r\n if ln%2 == 0\r\n ln.times do |i|\r\n if i< (ln/2)\r\n lin1 += lin[i]\r\n else\r\n lin2 += lin[i]\r\n end\r\n end\r\n end\r\n if lin1!= \"\" && lin1 == lin2\r\n puts \"YES\"\r\n else\r\n puts \"NO\"\r\n end\r\n \r\nend"}, {"source_code": " n = gets.to_i\r\n n.times do\r\n str = gets.to_s.strip\r\n len = str.length\r\n ans = if len.even?\r\n half = len / 2\r\n str[0...half] == str[half..] ? 'YES' : 'NO'\r\n else\r\n 'NO'\r\n end\r\n puts ans\r\n end\r\n"}, {"source_code": "n = gets.chomp.to_i\r\nn.times do\r\n w = gets.chomp\r\n puts (w[0...w.length/2] == w[w.length/2..] ? \"YES\" : \"NO\")\r\nend"}, {"source_code": "n = gets.to_i\na = []\n\ndef find_ans(w)\n if w.length.odd?\n 'NO'\n else\n if w[0..w.length/2 - 1] == w[w.length/2..w.length]\n 'YES'\n else\n 'NO'\n end\n end\nend\n \n(0..n-1).each do |x|\n w = gets.chomp\n\ta.append(find_ans(w))\nend\n \n \na.each do |x|\n puts x\nend"}, {"source_code": "t = gets.to_i\ninputs = []\n(0...t).each do |i|\n str = gets.chomp\n inputs << str\nend\n\ninputs.each do |str|\n len = str.length\n if len == 1\n puts \"NO\"\n next\n end\n m = len/2\n substr1 = str[0..m-1]\n substr2 = str[m..len]\n if substr1 == substr2\n puts \"YES\"\n else\n puts \"NO\"\n end\nend"}, {"source_code": "inputs = STDIN.read.split(\"\\n\")\r\ninputs.shift\r\n\r\ninputs.each do |input|\r\n if input.length % 2 != 0\r\n puts \"NO\"\r\n next\r\n end\r\n\r\n if input[0..input.length/2 - 1] == input[input.length/2..input.length]\r\n puts \"YES\"\r\n else\r\n puts \"NO\"\r\n end\r\nend\r\n"}], "negative_code": [{"source_code": "t = gets.chomp.to_i\r\nt.times do |n|\r\n lin = gets.chomp\r\n ln = lin.size\r\n lin1 = \"\"\r\n lin2 = \"\"\r\n if ln%2 == 0\r\n ln.times do |i|\r\n if i< (ln/2)\r\n lin1 += lin[i]\r\n else\r\n lin2 += lin[i]\r\n end\r\n end\r\n end\r\n if lin1!= \"\" && lin1 == lin2\r\n p \"YES\"\r\n else\r\n p \"NO\"\r\n end\r\n \r\nend"}, {"source_code": "t = gets.to_i\r\nt.times do |n|\r\n lin = gets\r\n ln = lin.length\r\n lin1 = \"\"\r\n lin2 = \"\"\r\n if ln%2 == 0\r\n ln.times do |i|\r\n if i< (ln/2)\r\n lin1 += lin[i]\r\n else\r\n lin2 += lin[i]\r\n end\r\n end\r\n end\r\n if lin1!= \"\" && lin1 == lin2\r\n p \"YES\"\r\n else\r\n p \"NO\"\r\n end\r\n \r\nend"}, {"source_code": "t = gets.to_i\r\nt.times do |n|\r\n lin = gets\r\n ln = lin.size\r\n lin1 = \"\"\r\n lin2 = \"\"\r\n if ln%2 == 0\r\n ln.times do |i|\r\n if i< (ln/2)\r\n lin1 += lin[i]\r\n else\r\n lin2 += lin[i]\r\n end\r\n end\r\n end\r\n if lin1!= \"\" && lin1 == lin2\r\n p \"YES\"\r\n else\r\n p \"NO\"\r\n end\r\n \r\nend\r\n"}, {"source_code": "n = gets.to_i\r\nn.times do\r\n str = gets.to_s\r\n len = str.length\r\n ans = if len.even?\r\n half = len / 2\r\n str[0...half] == str[half..] ? 'YES' : 'NO'\r\n else\r\n 'NO'\r\n end\r\n puts ans\r\nend\r\n"}, {"source_code": "s = gets.chomp\r\nputs (s[0...s.length/2] == s[s.length/2..] ? \"YES\" : \"NO\")"}, {"source_code": "n = gets.to_i\na = []\n\ndef find_ans(w)\n if w.length.odd?\n 'NO'\n else\n if w[0..w.length/2 - 1] == w[w.length/2..w.length]\n 'YES'\n else\n 'NO'\n end\n end\nend\n \n(0..n-1).each do |x|\n w = gets.chomp\n\ta.append(find_ans(w))\nend\n \n \na.each do |x|\n p x\nend"}], "src_uid": "9640b7197bd7b8a59f29aecf104291e1"} {"nl": {"description": "After lessons Nastya decided to read a book. The book contains $$$n$$$ chapters, going one after another, so that one page of the book belongs to exactly one chapter and each chapter contains at least one page.Yesterday evening Nastya did not manage to finish reading the book, so she marked the page with number $$$k$$$ as the first page which was not read (i.e. she read all pages from the $$$1$$$-st to the $$$(k-1)$$$-th).The next day Nastya's friend Igor came and asked her, how many chapters remain to be read by Nastya? Nastya is too busy now, so she asks you to compute the number of chapters she has not completely read yet (i.e. the number of chapters she has not started to read or has finished reading somewhere in the middle).", "input_spec": "The first line contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 100$$$) — the number of chapters in the book. There are $$$n$$$ lines then. The $$$i$$$-th of these lines contains two integers $$$l_i$$$, $$$r_i$$$ separated by space ($$$l_1 = 1$$$, $$$l_i \\leq r_i$$$) — numbers of the first and the last pages of the $$$i$$$-th chapter. It's guaranteed that $$$l_{i+1} = r_i + 1$$$ for all $$$1 \\leq i \\leq n-1$$$, and also that every chapter contains at most $$$100$$$ pages. The $$$(n+2)$$$-th line contains a single integer $$$k$$$ ($$$1 \\leq k \\leq r_n$$$) — the index of the marked page. ", "output_spec": "Print a single integer — the number of chapters which has not been completely read so far.", "sample_inputs": ["3\n1 3\n4 7\n8 11\n2", "3\n1 4\n5 9\n10 12\n9", "1\n1 7\n4"], "sample_outputs": ["3", "2", "1"], "notes": "NoteIn the first example the book contains $$$11$$$ pages and $$$3$$$ chapters — $$$[1;3]$$$, $$$[4;7]$$$ and $$$[8;11]$$$. Nastya marked the $$$2$$$-nd page, so she finished in the middle of the $$$1$$$-st chapter. So, all chapters has not been read so far, so the answer is $$$3$$$.The book in the second example contains $$$12$$$ pages and $$$3$$$ chapters too, but Nastya finished reading in the middle of the $$$2$$$-nd chapter, so that the answer is $$$2$$$."}, "positive_code": [{"source_code": "n = gets.to_i\nl = Array.new(n)\nr = Array.new(n)\nn.times do |i|\n l[i], r[i] = gets.split.map(&:to_i)\nend\nk = gets.to_i\nputs r.count { |rr| rr >= k }\n"}, {"source_code": "\nn = gets.to_i\nch = []\n(1..n).each do |i|\n ch << gets.split(\" \").map(&:to_i)\nend\nk = gets.to_i\n\nres = 0\n(0..n-1).each do |i|\n if k > ch[i][1]\n res += 1\n end\nend\n\nputs n-res\n"}, {"source_code": "n = gets.to_i\nl, r = [], []\nn.times do\n li, ri = gets.split.map(&:to_i)\n l << li\n r << ri\nend\nk = gets.to_i\ni = 0\nloop do\n break if k <= r[i]\n i += 1\nend\nputs n - i\n"}], "negative_code": [], "src_uid": "2545b6af730f99193041a8810b728cb3"} {"nl": {"description": "Calculate the minimum number of characters you need to change in the string s, so that it contains at least k different letters, or print that it is impossible.String s consists only of lowercase Latin letters, and it is allowed to change characters only to lowercase Latin letters too.", "input_spec": "First line of input contains string s, consisting only of lowercase Latin letters (1 ≤ |s| ≤ 1000, |s| denotes the length of s). Second line of input contains integer k (1 ≤ k ≤ 26).", "output_spec": "Print single line with a minimum number of necessary changes, or the word «impossible» (without quotes) if it is impossible.", "sample_inputs": ["yandex\n6", "yahoo\n5", "google\n7"], "sample_outputs": ["0", "1", "impossible"], "notes": "NoteIn the first test case string contains 6 different letters, so we don't need to change anything.In the second test case string contains 4 different letters: {'a', 'h', 'o', 'y'}. To get 5 different letters it is necessary to change one occurrence of 'o' to some letter, which doesn't occur in the string, for example, {'b'}.In the third test case, it is impossible to make 7 different letters because the length of the string is 6."}, "positive_code": [{"source_code": "s=gets.chomp\nn=gets.to_i\nputs s.sizes.size\n\tputs \"impossible\"\nelse\n\tn=s.chars.uniq.size\n\tputs [0, k-n].max\nend\n"}, {"source_code": "s=gets.chomp.chars\nk=gets.to_i\nputs k > s.size ? :impossible : [0, k - s.uniq.size].max"}, {"source_code": "s, k = gets.chomp.chars, gets.to_i\nputs k > s.size ? :impossible : [0, k - s.uniq.size].max"}], "negative_code": [{"source_code": "s=gets.chomp.chars\nk=gets.to_i\nputs k > s.size ? :impossible : [k - s.uniq.size].max"}, {"source_code": "s, cnt, k = gets.chomp, Array.new(26, 0), gets.to_i\ns.chars {|c| cnt[c.ord - 97] += 1}\ncnt.reject! {|c| c.zero?}\nputs k > s.length ? :impossible : k - cnt.length\n"}, {"source_code": "cnt = Array.new(26, 0)\ngets.chomp.chars {|c|\n cnt[c.ord - 97] += 1\n}\ncnt.reject! {|c| c.zero?}\nk = gets.to_i - cnt.length\nputs k > cnt.reduce(:+) ? :impossible : k"}, {"source_code": "cnt = Array.new(26, 0)\ngets.chomp.chars {|c|\n cnt[c.ord - 97] += 1\n}\ncnt.reject! {|c| c.zero?}\nk = gets.to_i\nputs k > cnt.reduce(:+) ? :impossible : k - cnt.length"}], "src_uid": "bd5912fe2c5c37658f28f6b159b39645"} {"nl": {"description": "Let's call a binary string $$$T$$$ of length $$$m$$$ indexed from $$$1$$$ to $$$m$$$ paranoid if we can obtain a string of length $$$1$$$ by performing the following two kinds of operations $$$m-1$$$ times in any order : Select any substring of $$$T$$$ that is equal to 01, and then replace it with 1. Select any substring of $$$T$$$ that is equal to 10, and then replace it with 0.For example, if $$$T = $$$ 001, we can select the substring $$$[T_2T_3]$$$ and perform the first operation. So we obtain $$$T = $$$ 01.You are given a binary string $$$S$$$ of length $$$n$$$ indexed from $$$1$$$ to $$$n$$$. Find the number of pairs of integers $$$(l, r)$$$ $$$1 \\le l \\le r \\le n$$$ such that $$$S[l \\ldots r]$$$ (the substring of $$$S$$$ from $$$l$$$ to $$$r$$$) is a paranoid string. ", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) — the size of $$$S$$$. The second line of each test case contains a binary string $$$S$$$ of $$$n$$$ characters $$$S_1S_2 \\ldots S_n$$$. ($$$S_i = $$$ 0 or $$$S_i = $$$ 1 for each $$$1 \\le i \\le n$$$) It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, output the number of pairs of integers $$$(l, r)$$$ $$$1 \\le l \\le r \\le n$$$ such that $$$S[l \\ldots r]$$$ (the substring of $$$S$$$ from $$$l$$$ to $$$r$$$) is a paranoid string. ", "sample_inputs": ["5\n\n1\n\n1\n\n2\n\n01\n\n3\n\n100\n\n4\n\n1001\n\n5\n\n11111"], "sample_outputs": ["1\n3\n4\n8\n5"], "notes": "NoteIn the first sample, $$$S$$$ already has length $$$1$$$ and doesn't need any operations.In the second sample, all substrings of $$$S$$$ are paranoid. For the entire string, it's enough to perform the first operation.In the third sample, all substrings of $$$S$$$ are paranoid except $$$[S_2S_3]$$$, because we can't perform any operations on it, and $$$[S_1S_2S_3]$$$ (the entire string)."}, "positive_code": [{"source_code": "# gets.to_i\r\n# gets.split.map(&:to_i)\r\n\r\ngets.to_i.times do\r\n n = gets.to_i\r\n s = gets.chomp.chars\r\n\r\n ans = n\r\n i = 1\r\n while i < n\r\n if s[i] != s[i-1]\r\n ans += i\r\n end\r\n i += 1\r\n end\r\n\r\n puts ans\r\nend\r\n"}, {"source_code": "gets.to_i.times do\r\n gets\r\n s = gets.chomp\r\n answer = s.size\r\n 0.upto(s.size - 2).each do |i|\r\n if s[i] != s[i + 1]\r\n answer += i + 1\r\n end\r\n end\r\n p answer\r\nend\r\n"}], "negative_code": [], "src_uid": "bc45b3b665ccef5c9451ff2ecc5198a8"} {"nl": {"description": "Anton is playing a very interesting computer game, but now he is stuck at one of the levels. To pass to the next level he has to prepare n potions.Anton has a special kettle, that can prepare one potions in x seconds. Also, he knows spells of two types that can faster the process of preparing potions. Spells of this type speed up the preparation time of one potion. There are m spells of this type, the i-th of them costs bi manapoints and changes the preparation time of each potion to ai instead of x. Spells of this type immediately prepare some number of potions. There are k such spells, the i-th of them costs di manapoints and instantly create ci potions. Anton can use no more than one spell of the first type and no more than one spell of the second type, and the total number of manapoints spent should not exceed s. Consider that all spells are used instantly and right before Anton starts to prepare potions.Anton wants to get to the next level as fast as possible, so he is interested in the minimum number of time he needs to spent in order to prepare at least n potions.", "input_spec": "The first line of the input contains three integers n, m, k (1 ≤ n ≤ 2·109, 1 ≤ m, k ≤ 2·105) — the number of potions, Anton has to make, the number of spells of the first type and the number of spells of the second type. The second line of the input contains two integers x and s (2 ≤ x ≤ 2·109, 1 ≤ s ≤ 2·109) — the initial number of seconds required to prepare one potion and the number of manapoints Anton can use. The third line contains m integers ai (1 ≤ ai < x) — the number of seconds it will take to prepare one potion if the i-th spell of the first type is used. The fourth line contains m integers bi (1 ≤ bi ≤ 2·109) — the number of manapoints to use the i-th spell of the first type. There are k integers ci (1 ≤ ci ≤ n) in the fifth line — the number of potions that will be immediately created if the i-th spell of the second type is used. It's guaranteed that ci are not decreasing, i.e. ci ≤ cj if i < j. The sixth line contains k integers di (1 ≤ di ≤ 2·109) — the number of manapoints required to use the i-th spell of the second type. It's guaranteed that di are not decreasing, i.e. di ≤ dj if i < j.", "output_spec": "Print one integer — the minimum time one has to spent in order to prepare n potions.", "sample_inputs": ["20 3 2\n10 99\n2 4 3\n20 10 40\n4 15\n10 80", "20 3 2\n10 99\n2 4 3\n200 100 400\n4 15\n100 800"], "sample_outputs": ["20", "200"], "notes": "NoteIn the first sample, the optimum answer is to use the second spell of the first type that costs 10 manapoints. Thus, the preparation time of each potion changes to 4 seconds. Also, Anton should use the second spell of the second type to instantly prepare 15 potions spending 80 manapoints. The total number of manapoints used is 10 + 80 = 90, and the preparation time is 4·5 = 20 seconds (15 potions were prepared instantly, and the remaining 5 will take 4 seconds each).In the second sample, Anton can't use any of the spells, so he just prepares 20 potions, spending 10 seconds on each of them and the answer is 20·10 = 200."}, "positive_code": [{"source_code": "n, m, k = gets.strip.split.map(&:to_i)\nx, s = gets.strip.split.map(&:to_i)\nai = gets.strip.split.map(&:to_i)\nbi = gets.strip.split.map(&:to_i)\nci = gets.strip.split.map(&:to_i)\ndi = gets.strip.split.map(&:to_i)\n\ntype2 = []\n(k-1).downto(0).each do |i|\n type2 << [di[i], ci[i]]\nend\n\nans = n*x\n\nsp2 = type2.bsearch{|i| i[0]<=s}\nans = [ans, [n - sp2[1], 0].max * x].min if sp2\n\n0.upto(m-1).each do |i|\n if s >= bi[i]\n rem = s - bi[i]\n sp2 = type2.bsearch{|j| j[0]<=rem}\n if sp2\n ans = [ans, [n - sp2[1], 0].max * ai[i]].min\n else\n ans = [ans, n * ai[i]].min\n end\n end\nend\n\nputs ans"}, {"source_code": "# Codeforces My Practice\n# author: Leonardone @ NEETSDKASU\n############################################################\ndef gs() gets.strip end\ndef gi() gets.to_i end\ndef gss() gs.split end\ndef gis() gss.map(&:to_i) end\ndef nmapf(n,f) n.times.map{ __send__ f } end\ndef ngs(n) nmapf n,:gs end\ndef ngi(n) nmapf n,:gi end\ndef ngss(n) nmapf n,:gss end\ndef ngis(n) nmapf n,:gis end\ndef arr2d(h,w,v=0) h.times.map{[v] * w} end\ndef for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end\ndef nsum(n) n * (n + 1) / 2 end\ndef vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end\n############################################################\n\n\nn, m, k = gis\nx, s = gis\na = gis # speed\nb = gis # cost\nc = gis # create\nd = gis # cost\n\nab = a.zip(b).sort_by{|_, bi| bi}\ncd = c.zip(d).sort{|u, v|\n if u[1] != v[1]\n v[1] <=> u[1]\n else\n v[0] <=> u[0]\n end\n}\n\nres = n * x\n\nm.times do |i|\n bi = ab[i][1]\n break if bi > s\n ai = ab[i][0]\n res = [res, n * ai].min\n cj, _ = cd.bsearch{|_, dj| bi + dj <= s}\n next if cj.nil?\n res = [res, (n - cj) * ai].min\nend\ncd.reverse.each do |cj, dj|\n break if dj > s\n res = [res, (n - cj) * x].min\nend\n\nputs res\n\n"}, {"source_code": "# Codeforces My Practice\n# author: Leonardone @ NEETSDKASU\n############################################################\ndef gs() gets.strip end\ndef gi() gets.to_i end\ndef gss() gs.split end\ndef gis() gss.map(&:to_i) end\ndef nmapf(n,f) n.times.map{ __send__ f } end\ndef ngs(n) nmapf n,:gs end\ndef ngi(n) nmapf n,:gi end\ndef ngss(n) nmapf n,:gss end\ndef ngis(n) nmapf n,:gis end\ndef arr2d(h,w,v=0) h.times.map{[v] * w} end\ndef for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end\ndef nsum(n) n * (n + 1) / 2 end\ndef vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end\n############################################################\n\n\nn, m, k = gis\nx, s = gis\na = gis # speed\nb = gis # cost\nc = gis # create\nd = gis # cost\n\nab = a.zip(b).sort_by{|_, bi| bi}\ncd = c.zip(d).sort{|u, v|\n if u[1] != v[1]\n v[1] <=> u[1]\n else\n v[0] <=> u[0]\n end\n}\n\nres = n * x\n\nm.times do |i|\n bi = ab[i][1]\n break if bi > s\n ai = ab[i][0]\n res = [res, n * ai].min\n cj, _ = cd.bsearch{|_, dj| bi + dj <= s}\n next if cj.nil?\n if cj <= n\n res = [res, (n - cj) * ai].min\n else\n res = [res, ai].min\n end\nend\ncd.reverse.each do |cj, dj|\n break if dj > s\n if cj <= n\n res = [res, (n - cj) * x].min\n else\n res = [res, x].min\n end\nend\n\nputs res\n\n"}, {"source_code": "#!/bin/ruby\n\n# n m k n = total potions to produce, m = number of a/b, k = number of c/d\n# x s initial rate, manapoints available\n# a1 a2 a3... (new rate, # of seconds per potion)\n# b1 b2 b3... (cost to speed up rate)\n# c1 c2 c3... c1 <= c2, number of instant potions\n# d1 d2 d3... d1 <= d2, cost of instant potions\n\n# min total time spent to produce the potions\n# for picked zero or one spell of each type\n\ndef find_next_smallest(input, target)\n left = 0\n right = input.size - 1\n while right != left\n midpoint = (right + left) / 2\n if right - left == 1\n # adjacent\n if input[right] > target\n return left\n else\n return right\n end\n elsif input[midpoint] > target\n right = midpoint - 1\n else\n left = midpoint\n end\n end\n\n return left\nend\n\n# read input\nn, m, k = gets.strip.split(' ').map{ |x| x.to_i }\nx, s = gets.strip.split(' ').map{ |x| x.to_i }\na = gets.strip.split(' ').map{ |x| x.to_i }\nb = gets.strip.split(' ').map{ |x| x.to_i }\nc = gets.strip.split(' ').map{ |x| x.to_i }\nd = gets.strip.split(' ').map{ |x| x.to_i }\n\n# base case, no type 1 spell\nj = find_next_smallest(d, s)\nrem_potion = n\nrem_potion = n - c[j] if d[j] <= s\nbest_time = rem_potion * x\n\n# consider each type 1 spell\n0.upto(m-1) do |i|\n next if s < b[i]\n\n # find largest type 2 spell that can fit in remainder of s\n target = s - b[i]\n j = find_next_smallest(d, target)\n\n # compute timing\n rem_potion = n\n rem_potion = n - c[j] if d[j] <= target\n time = rem_potion * a[i]\n best_time = time if time < best_time\nend\n\nputs best_time\n"}, {"source_code": "r = proc {gets.split.map(&:to_i)}\nn = r.call[0]\nx, s = r.call\na = r.call\nb = r.call\nq1 = a.zip(b)\nc = r.call\nd = r.call\nq2 = c.zip(d)\nq2.reverse!\nc = q2.bsearch{|c, d| s>=d} || [0,0]\nsol = (n-c[0]) * x\n(q1.select{|a,b| b<=s}).each do |a,b|\n c = q2.bsearch{|c, d| (s-b)>=d} || [0,0]\n sol = [sol, (n-c[0])*a].min\nend\nputs sol\n"}], "negative_code": [{"source_code": "# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n############################################################\ndef gs() gets.strip end\ndef gi() gets.to_i end\ndef gss() gs.split end\ndef gis() gss.map(&:to_i) end\ndef nmapf(n,f) n.times.map{ __send__ f } end\ndef ngs(n) nmapf n,:gs end\ndef ngi(n) nmapf n,:gi end\ndef ngss(n) nmapf n,:gss end\ndef ngis(n) nmapf n,:gis end\ndef arr2d(h,w,v=0) h.times.map{[v] * w} end\ndef for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end\ndef nsum(n) n * (n + 1) / 2 end\ndef vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end\n############################################################\n\nn, m, k = gis\nx, s = gis\na = gis # speed\nb = gis # cost\nc = gis # create\nd = gis # cost\n\nab = a.zip(b).sort_by{|_, bi| bi}\ncd = c.zip(d).sort_by{|_, di| di}\n\nres = n * x\n\nk.times do |i|\n di = cd[i][1]\n break if di > s\n ci = cd[i][0]\n rm = n\n if ci >= n\n rm = 1\n res = [res, x].min\n else\n rm = n - ci\n res = [res, rm * x].min\n end\n m.times do |j|\n break if ab[j][1] + di > s\n res = [res, rm * ab[j][1]].min\n end\nend\nm.times do |j|\n break if ab[j][1] > s\n res = [res, n * ab[j][0]].min\nend\n\nputs res\n\n\n\n\n"}, {"source_code": "# Codeforces My Practice\n# author: Leonardone @ NEETSDKASU\n############################################################\ndef gs() gets.strip end\ndef gi() gets.to_i end\ndef gss() gs.split end\ndef gis() gss.map(&:to_i) end\ndef nmapf(n,f) n.times.map{ __send__ f } end\ndef ngs(n) nmapf n,:gs end\ndef ngi(n) nmapf n,:gi end\ndef ngss(n) nmapf n,:gss end\ndef ngis(n) nmapf n,:gis end\ndef arr2d(h,w,v=0) h.times.map{[v] * w} end\ndef for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end\ndef nsum(n) n * (n + 1) / 2 end\ndef vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end\n############################################################\n\n\nn, m, k = gis\nx, s = gis\na = gis # speed\nb = gis # cost\nc = gis # create\nd = gis # cost\n\nab = a.zip(b).sort_by{|_, bi| bi}\ncd = c.zip(d).sort_by{|_, di| -di}\n\nres = n * x\n\nm.times do |i|\n bi = ab[i][1]\n break if bi > s\n ai = ab[i][0]\n res = [res, n * ai].min\n cj, _ = cd.bsearch{|_, dj| bi + dj <= s}\n next if cj.nil?\n res = [res, (n - cj) * ai].min\nend\ncd.reverse.each do |cj, dj|\n break if dj > s\n res = [res, (n - cj) * x].min\nend\n\nputs res\n\n"}, {"source_code": "# Codeforces My Practice\n# author: Leonardone @ NEETSDKASU\n############################################################\ndef gs() gets.strip end\ndef gi() gets.to_i end\ndef gss() gs.split end\ndef gis() gss.map(&:to_i) end\ndef nmapf(n,f) n.times.map{ __send__ f } end\ndef ngs(n) nmapf n,:gs end\ndef ngi(n) nmapf n,:gi end\ndef ngss(n) nmapf n,:gss end\ndef ngis(n) nmapf n,:gis end\ndef arr2d(h,w,v=0) h.times.map{[v] * w} end\ndef for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end\ndef nsum(n) n * (n + 1) / 2 end\ndef vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end\n############################################################\n\n\nn, m, k = gis\nx, s = gis\na = gis # speed\nb = gis # cost\nc = gis # create\nd = gis # cost\n\nab = a.zip(b).sort_by{|_, bi| bi}\ncd = c.zip(d).sort_by{|ci, di| [-di, -ci] }\n\nres = n * x\n\nm.times do |i|\n bi = ab[i][1]\n break if bi > s\n ai = ab[i][0]\n res = [res, n * ai].min\n cj, _ = cd.bsearch{|_, dj| bi + dj <= s}\n next if cj.nil?\n if cj < n\n res = [res, (n - cj) * ai].min\n else\n res = [res, ai].min\n end\nend\ncd.reverse.each do |cj, dj|\n break if dj > s\n if cj < n\n res = [res, (n - cj) * x].min\n else\n res = [res, x].min\n end\nend\n\nputs res\n\n"}, {"source_code": "# Codeforces My Practice\n# author: Leonardone @ NEETSDKASU\n############################################################\ndef gs() gets.strip end\ndef gi() gets.to_i end\ndef gss() gs.split end\ndef gis() gss.map(&:to_i) end\ndef nmapf(n,f) n.times.map{ __send__ f } end\ndef ngs(n) nmapf n,:gs end\ndef ngi(n) nmapf n,:gi end\ndef ngss(n) nmapf n,:gss end\ndef ngis(n) nmapf n,:gis end\ndef arr2d(h,w,v=0) h.times.map{[v] * w} end\ndef for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end\ndef nsum(n) n * (n + 1) / 2 end\ndef vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end\n############################################################\n\n\nn, m, k = gis\nx, s = gis\na = gis # speed\nb = gis # cost\nc = gis # create\nd = gis # cost\n\nab = a.zip(b) #.sort_by{|_, bi| bi}\ncd = c.zip(d).reverse #.sort_by{|ci, di| [-di, -ci] }\n\nres = n * x\n\nm.times do |i|\n bi = ab[i][1]\n break if bi > s\n ai = ab[i][0]\n res = [res, n * ai].min\n cj, _ = cd.bsearch{|_, dj| bi + dj <= s}\n next if cj.nil?\n if cj <= n\n res = [res, (n - cj) * ai].min\n else\n res = [res, ai].min\n end\nend\ncd.reverse.each do |cj, dj|\n break if dj > s\n if cj <= n\n res = [res, (n - cj) * x].min\n else\n res = [res, x].min\n end\nend\n\nputs res\n\n"}, {"source_code": "# Codeforces My Practice\n# author: Leonardone @ NEETSDKASU\n############################################################\ndef gs() gets.strip end\ndef gi() gets.to_i end\ndef gss() gs.split end\ndef gis() gss.map(&:to_i) end\ndef nmapf(n,f) n.times.map{ __send__ f } end\ndef ngs(n) nmapf n,:gs end\ndef ngi(n) nmapf n,:gi end\ndef ngss(n) nmapf n,:gss end\ndef ngis(n) nmapf n,:gis end\ndef arr2d(h,w,v=0) h.times.map{[v] * w} end\ndef for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end\ndef nsum(n) n * (n + 1) / 2 end\ndef vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end\n############################################################\n\n\nn, m, k = gis\nx, s = gis\na = gis # speed\nb = gis # cost\nc = gis # create\nd = gis # cost\n\nab = a.zip(b).sort_by{|_, bi| bi}.reverse\ncd = c.zip(d).sort_by{|_, di| di}\n\nres = n * x\n\nk.times do |i|\n di = cd[i][1]\n break if di > s\n ci = cd[i][0]\n rm = n\n if ci >= n\n rm = 1\n res = [res, x].min\n else\n rm = n - ci\n res = [res, rm * x].min\n end\n ai, bi = ab.bsearch{|_, bi| bi + di <= s}\n res = [res, rm * ai].min\nend\nm.times do |j|\n break if ab[j][1] > s\n res = [res, n * ab[j][0]].min\nend\n\nputs res\n\n\n"}, {"source_code": "# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n############################################################\ndef gs() gets.strip end\ndef gi() gets.to_i end\ndef gss() gs.split end\ndef gis() gss.map(&:to_i) end\ndef nmapf(n,f) n.times.map{ __send__ f } end\ndef ngs(n) nmapf n,:gs end\ndef ngi(n) nmapf n,:gi end\ndef ngss(n) nmapf n,:gss end\ndef ngis(n) nmapf n,:gis end\ndef arr2d(h,w,v=0) h.times.map{[v] * w} end\ndef for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end\ndef nsum(n) n * (n + 1) / 2 end\ndef vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end\n############################################################\n\nn, m, k = gis\nx, s = gis\na = gis # speed\nb = gis # cost\nc = gis # create\nd = gis # cost\n\nab = a.zip(b).sort_by{|_, bi| bi}\ncd = c.zip(d).sort_by{|_, di| di}\n\nres = n * x\n\nk.times do |i|\n di = cd[i][1]\n break if di > s\n ci = cd[i][0]\n rm = n\n if ci >= n\n rm = 1\n res = [res, x].min\n next\n else\n rm = n - ci\n res = [res, rm * x].min\n end\n m.times do |j|\n break if ab[j][1] + di > s\n res = [res, rm * ab[j][1]].min\n end\nend\nm.times do |j|\n break if ab[j][1] > s\n res = [res, n * ab[j][0]].min\nend\n\nputs res\n\n\n\n\n"}, {"source_code": "# Codeforces My Practice\n# author: Leonardone @ NEETSDKASU\n############################################################\ndef gs() gets.strip end\ndef gi() gets.to_i end\ndef gss() gs.split end\ndef gis() gss.map(&:to_i) end\ndef nmapf(n,f) n.times.map{ __send__ f } end\ndef ngs(n) nmapf n,:gs end\ndef ngi(n) nmapf n,:gi end\ndef ngss(n) nmapf n,:gss end\ndef ngis(n) nmapf n,:gis end\ndef arr2d(h,w,v=0) h.times.map{[v] * w} end\ndef for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end\ndef nsum(n) n * (n + 1) / 2 end\ndef vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end\n############################################################\n\n\nn, m, k = gis\nx, s = gis\na = gis # speed\nb = gis # cost\nc = gis # create\nd = gis # cost\n\nab = a.zip(b).sort_by{|_, bi| bi}\ncd = c.zip(d).sort_by{|_, di| -di}\n\nres = n * x\n\nm.times do |i|\n bi = ab[i][1]\n break if bi > s\n ai = ab[i][0]\n res = [res, n * ai].min\n cj, _ = cd.bsearch{|_, dj| bi + dj <= s}\n next if cj.nil?\n res = [res, (n - cj) * ai].min\nend\nk.times do |j|\n break if cd[j][1] > s\n res = [res, (n - cd[j][0]) * x].min\nend\n\nputs res\n\n"}, {"source_code": "# Codeforces My Practice\n# author: Leonardone @ NEETSDKASU\n############################################################\ndef gs() gets.strip end\ndef gi() gets.to_i end\ndef gss() gs.split end\ndef gis() gss.map(&:to_i) end\ndef nmapf(n,f) n.times.map{ __send__ f } end\ndef ngs(n) nmapf n,:gs end\ndef ngi(n) nmapf n,:gi end\ndef ngss(n) nmapf n,:gss end\ndef ngis(n) nmapf n,:gis end\ndef arr2d(h,w,v=0) h.times.map{[v] * w} end\ndef for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end\ndef nsum(n) n * (n + 1) / 2 end\ndef vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end\n############################################################\n\n\nn, m, k = gis\nx, s = gis\na = gis # speed\nb = gis # cost\nc = gis # create\nd = gis # cost\n\nab = a.zip(b).sort_by{|_, bi| bi}\ncd = c.zip(d).sort_by{|_, di| -di}\n\nres = n * x\n\nm.times do |i|\n bi = ab[i][1]\n break if bi > s\n ai = ab[i][0]\n res = [res, n * ai].min\n cj, _ = cd.bsearch{|_, dj| bi + dj <= s}\n next if cj.nil?\n if cj < n\n res = [res, (n - cj) * ai].min\n else\n res = [res, ai].min\n end\nend\ncd.reverse.each do |cj, dj|\n break if dj > s\n res = [res, (n - cj) * x].min\nend\n\nputs res\n\n"}, {"source_code": "# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n############################################################\ndef gs() gets.strip end\ndef gi() gets.to_i end\ndef gss() gs.split end\ndef gis() gss.map(&:to_i) end\ndef nmapf(n,f) n.times.map{ __send__ f } end\ndef ngs(n) nmapf n,:gs end\ndef ngi(n) nmapf n,:gi end\ndef ngss(n) nmapf n,:gss end\ndef ngis(n) nmapf n,:gis end\ndef arr2d(h,w,v=0) h.times.map{[v] * w} end\ndef for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end\ndef nsum(n) n * (n + 1) / 2 end\ndef vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end\n############################################################\n\nn, m, k = gis\nx, s = gis\na = gis # speed\nb = gis # cost\nc = gis # create\nd = gis # cost\n\nres = n * x\n\nk.times do |i|\n di = d[i]\n break if di > s\n ci = c[i]\n rm = n\n if ci >= n\n rm = 1\n res = [res, x].min\n else\n rm = n - ci\n res = [res, rm * x].min\n end\n m.times do |j|\n break if b[j] + di > s\n res = [res, rm * a[j]].min\n end\nend\nm.times do |j|\n break if b[j] > s\n res = [res, n * a[j]].min\nend\n\nputs res\n\n\n\n\n"}, {"source_code": "# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n############################################################\ndef gs() gets.strip end\ndef gi() gets.to_i end\ndef gss() gs.split end\ndef gis() gss.map(&:to_i) end\ndef nmapf(n,f) n.times.map{ __send__ f } end\ndef ngs(n) nmapf n,:gs end\ndef ngi(n) nmapf n,:gi end\ndef ngss(n) nmapf n,:gss end\ndef ngis(n) nmapf n,:gis end\ndef arr2d(h,w,v=0) h.times.map{[v] * w} end\ndef for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end\ndef nsum(n) n * (n + 1) / 2 end\ndef vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end\n############################################################\n\nn, m, k = gis\nx, s = gis\na = gis # speed\nb = gis # cost\nc = gis # create\nd = gis # cost\n\nres = n * x\n\nk.times do |i|\n di = d[i]\n next if di > s\n ci = c[i]\n rm = n\n if ci >= n\n rm = 1\n res = [res, x].min\n else\n rm = n - ci\n res = [res, rm * x].min\n end\nend\nm.times do |j|\n next if b[j] > s\n res = [res, n * a[j]].min\nend\n\nputs res\n\n\n\n\n"}, {"source_code": "r = proc {gets.split.map(&:to_i)}\nn, m, k = r.call\nx, s = r.call\na = r.call\nb = r.call\nq1 = a.zip(b)\nc = r.call\nd = r.call\nq2 = c.zip(d)\nq2.reverse!\nsol = x * n\n(q1.select{|a,b| b<=s}).each do |a,b|\n c = q2.bsearch{|c, d| (s-b) > d} || [0,0]\n sol = [sol, (n-c[0])*a].min\nend\nputs sol\n"}, {"source_code": "r = proc {gets.split.map(&:to_i)}\nn, m, k = r.call\nx, s = r.call\na = r.call\nb = r.call\nq1 = a.zip(b)\nc = r.call\nd = r.call\nq2 = c.zip(d)\nsol = x * n\n(q1.select{|a,b| b<=s}).each do |a,b|\n c = q2.bsearch{|c, d| [(s-b)-d, 0].min} || [0,0]\n sol = [sol, (n-c[0])*a].min\nend\nputs sol\n"}, {"source_code": "r = proc {gets.split.map(&:to_i)}\nn = r.call[0]\nx, s = r.call\na = r.call\nb = r.call\nq1 = a.zip(b)\nc = r.call\nd = r.call\nq2 = c.zip(d)\nq2.reverse!\nc = q2.bsearch{|c, d| s>d} || [0,0]\nsol = (n-c[0]) * x\n(q1.select{|a,b| b<=s}).each do |a,b|\n c = q2.bsearch{|c, d| (s-b)>d} || [0,0]\n sol = [sol, (n-c[0])*a].min\nend\nputs sol\n"}], "src_uid": "2f9f2bdf059e5ab9c64e7b5f27cba0cb"} {"nl": {"description": "Valery is a PE teacher at a school in Berland. Soon the students are going to take a test in long jumps, and Valery has lost his favorite ruler! However, there is no reason for disappointment, as Valery has found another ruler, its length is l centimeters. The ruler already has n marks, with which he can make measurements. We assume that the marks are numbered from 1 to n in the order they appear from the beginning of the ruler to its end. The first point coincides with the beginning of the ruler and represents the origin. The last mark coincides with the end of the ruler, at distance l from the origin. This ruler can be repesented by an increasing sequence a1, a2, ..., an, where ai denotes the distance of the i-th mark from the origin (a1 = 0, an = l).Valery believes that with a ruler he can measure the distance of d centimeters, if there is a pair of integers i and j (1 ≤ i ≤ j ≤ n), such that the distance between the i-th and the j-th mark is exactly equal to d (in other words, aj - ai = d). Under the rules, the girls should be able to jump at least x centimeters, and the boys should be able to jump at least y (x < y) centimeters. To test the children's abilities, Valery needs a ruler to measure each of the distances x and y. Your task is to determine what is the minimum number of additional marks you need to add on the ruler so that they can be used to measure the distances x and y. Valery can add the marks at any integer non-negative distance from the origin not exceeding the length of the ruler.", "input_spec": "The first line contains four positive space-separated integers n, l, x, y (2 ≤ n ≤ 105, 2 ≤ l ≤ 109, 1 ≤ x < y ≤ l) — the number of marks, the length of the ruler and the jump norms for girls and boys, correspondingly. The second line contains a sequence of n integers a1, a2, ..., an (0 = a1 < a2 < ... < an = l), where ai shows the distance from the i-th mark to the origin.", "output_spec": "In the first line print a single non-negative integer v — the minimum number of marks that you need to add on the ruler. In the second line print v space-separated integers p1, p2, ..., pv (0 ≤ pi ≤ l). Number pi means that the i-th mark should be at the distance of pi centimeters from the origin. Print the marks in any order. If there are multiple solutions, print any of them.", "sample_inputs": ["3 250 185 230\n0 185 250", "4 250 185 230\n0 20 185 250", "2 300 185 230\n0 300"], "sample_outputs": ["1\n230", "0", "2\n185 230"], "notes": "NoteIn the first sample it is impossible to initially measure the distance of 230 centimeters. For that it is enough to add a 20 centimeter mark or a 230 centimeter mark.In the second sample you already can use the ruler to measure the distances of 185 and 230 centimeters, so you don't have to add new marks.In the third sample the ruler only contains the initial and the final marks. We will need to add two marks to be able to test the children's skills."}, "positive_code": [{"source_code": "n, l, x, y = STDIN.readline.split.map {|s| s.to_i }\ndistances = STDIN.readline.split.map {|s| s.to_i }\ndistance_hash = {}\ndistances.each {|d| distance_hash[d] = true }\nfound_x, found_y, xy = false, false, nil\ndistances.each do |d|\n\tfound_x = true if distance_hash.has_key?(d+x)\n\tfound_y = true if distance_hash.has_key?(d+y)\n\tif xy == nil\n\t\txy = d+x if distance_hash.has_key?(d+x+y)\n\t\tif distance_hash.has_key?(d+x-y)\n\t\t\tif d+x <= l\n\t\t\t\txy = d+x\n\t\t\telsif d-y >= 0\n\t\t\t\txy = d-y\n\t\t\tend\n\t\tend\n\t\tif distance_hash.has_key?(d+y-x)\n\t\t\tif d+y <= l\n\t\t\t\txy = d+y\n\t\t\telsif d-x >= 0\n\t\t\t\txy = d-x\n\t\t\tend\n\t\tend\n\tend\nend\nif found_x and found_y\n\tputs 0\nelsif found_x\n\tputs \"1\\n%d\" % y\nelsif found_y\n\tputs \"1\\n%d\" % x\nelsif xy != nil\n\tputs \"1\\n%d\" % xy\nelse\n\tputs \"2\\n%d %d\" % [x, y]\nend\n"}], "negative_code": [{"source_code": "n, l, x, y = STDIN.readline.split.map {|s| s.to_i }\ndistances = STDIN.readline.split.map {|s| s.to_i }\ndistance_hash = {}\ndistances.each {|distance| distance_hash[distance] = true }\nfound_x, found_y, xy = false, false, nil\ndistances.each do |d|\n\tfound_x = true if distance_hash.has_key?(d+x)\n\tfound_y = true if distance_hash.has_key?(d+y)\n\tif xy == nil\n\t\txy = d+x if distance_hash.has_key?(d+x+y)\n\t\tif distance_hash.has_key?(d+x-y) and d+x <= l\n\t\t\txy = d+x\n\t\tend\n\t\tif distance_hash.has_key?(d+y-x) and d+y <= l\n\t\t\txy = d+y\n\t\tend\n\tend\nend\nif found_x and found_y\n\tputs 0\nelsif found_x\n\tputs \"1\\n%d\" % y\nelsif found_y\n\tputs \"1\\n%d\" % x\nelsif xy != nil\n\tputs \"1\\n%d\" % xy\nelse\n\tputs \"2\\b%d %d\" % [x, y]\nend\n"}, {"source_code": "n, l, x, y = STDIN.readline.split.map {|s| s.to_i }\ndistances = STDIN.readline.split.map {|s| s.to_i }\ndistance_hash = {}\ndistances.each {|distance| distance_hash[distance] = true }\nfound_x, found_y, xy = false, false, nil\ndistances.each do |d|\n\tfound_x = true if distance_hash.has_key?(d+x)\n\tfound_y = true if distance_hash.has_key?(d+y)\n\tif xy == nil\n\t\txy = d+x if distance_hash.has_key?(d+x+y)\n\t\tif distance_hash.has_key?(d+x-y)\n\t\t\txy = d+x if d+x <= l\n\t\t\txy = d-y if d-y >= 0\n\t\tend\n\t\tif distance_hash.has_key?(d+y-x)\n\t\t\txy = d+y if d+y <= l\n\t\t\txy = d-x if d-x >= 0\n\t\tend\n\tend\nend\nif found_x and found_y\n\tputs 0\nelsif found_x\n\tputs \"1\\n%d\" % y\nelsif found_y\n\tputs \"1\\n%d\" % x\nelsif xy != nil\n\tputs \"1\\n%d\" % xy\nelse\n\tputs \"2\\b%d %d\" % [x, y]\nend\n"}, {"source_code": "n, l, x, y = STDIN.readline.split.map {|s| s.to_i }\ndistances = STDIN.readline.split.map {|s| s.to_i }\ndistance_hash = {}\ndistances.each {|distance| distance_hash[distance] = true }\nfound_x, found_y, xy = false, false, nil\ndistances.each do |d|\n\tfound_x = true if distance_hash.has_key?(d+x)\n\tfound_y = true if distance_hash.has_key?(d+y)\n\txy = d+x if distance_hash.has_key?(d+x+y)\nend\nif found_x and found_y\n\tputs 0\nelsif found_x\n\tputs \"1\\n%d\" % y\nelsif found_y\n\tputs \"1\\n%d\" % x\nelsif xy != nil\n\tputs \"1\\n%d\" % xy\nelse\n\tputs \"2\\b%d %d\" % [x, y]\nend\n"}], "src_uid": "333790c28eb02ad568a2406d5b2fafa6"} {"nl": {"description": "You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s is a string slsl + 1sl + 2... sr, and its length equals to r - l + 1. A substring is called balanced if the number of zeroes (0) equals to the number of ones in this substring.You have to determine the length of the longest balanced substring of s.", "input_spec": "The first line contains n (1 ≤ n ≤ 100000) — the number of characters in s. The second line contains a string s consisting of exactly n characters. Only characters 0 and 1 can appear in s.", "output_spec": "If there is no non-empty balanced substring in s, print 0. Otherwise, print the length of the longest balanced substring.", "sample_inputs": ["8\n11010111", "3\n111"], "sample_outputs": ["4", "0"], "notes": "NoteIn the first example you can choose the substring [3, 6]. It is balanced, and its length is 4. Choosing the substring [2, 5] is also possible.In the second example it's impossible to find a non-empty balanced substring."}, "positive_code": [{"source_code": "class Solver\n\n def self.solve(string)\n result = {}\n height = 0\n result[0] = [0]\n index = 0\n string.each_char do |char|\n index += 1\n height = height + (char == '1' ? 1 : -1)\n if result.fetch(height, nil).nil?\n result[height] = [index]\n elsif result[height].size == 1\n result[height] << index\n else\n result[height].pop\n result[height].push index\n end\n # puts \"#{height}:#{index}\"\n # puts result\n end\n array_result = result.values\n array_result.select! { |pair| pair.size == 2 }\n # puts array_result.inspect\n return [] if array_result.empty?\n final = array_result.max_by { |pair| pair[1] - pair.first }\n final[1] = final[1] -= 1\n final\n end\n\n def self.size(string)\n result = self.solve(string)\n # puts result\n return 0 if result.empty?\n result[1] - result[0] + 1\n end\nend\n\nstring = ARGF.read\narr = string.split\n# puts arr.inspect\nputs Solver.size(arr[1])\n"}, {"source_code": "gets\ni=1\nhs={}\na=0\nans=0\ngets.chomp.chars.each{|e|\n\ta+=1 if e==\"1\"\n\tb=i-a\n\tif b==a\n\t\tans=i\n\telse\n\t\tif hs.key?(b-a)\n\t\t\tans=[ans,i-hs[b-a]].max\n\t\tend\n\tend\n\tif hs.key?(b-a)==false\n\t\ths[b-a]=i\n\tend\n\ti+=1\n}\nputs ans"}], "negative_code": [{"source_code": "class Solver\n\n def self.solve(string)\n result = {}\n height = 0\n result[0] = [0]\n string.each_char_with_index do |char, index|\n index += 1\n height = height + (char == '1' ? 1 : -1)\n if result.fetch(height, nil).nil?\n result[height] = [index]\n elsif result[height].size == 1\n result[height] << index\n else\n result[height].pop\n result[height].push index\n end\n puts \"#{height}:#{index}\"\n puts result\n end\n array_result = result.values\n array_result.select! { |pair| pair.size == 2 }\n puts array_result\n return [] if array_result.empty?\n final = array_result.max_by { |pair| pair[1] - pair.first }\n final[1] = final[1] -= 1\n final\n end\n\n def self.size(string)\n result = self.solve(string)\n puts result\n return 0 if result.empty?\n result[1] - result[0] + 1\n end\nend\n"}, {"source_code": "class Solver\n\n def self.solve(string)\n result = {}\n height = 0\n result[0] = [0]\n index = 0\n string.each_char do |char|\n index += 1\n height = height + (char == '1' ? 1 : -1)\n if result.fetch(height, nil).nil?\n result[height] = [index]\n elsif result[height].size == 1\n result[height] << index\n else\n result[height].pop\n result[height].push index\n end\n # puts \"#{height}:#{index}\"\n # puts result\n end\n array_result = result.values\n array_result.select! { |pair| pair.size == 2 }\n # puts array_result\n return [] if array_result.empty?\n final = array_result.max_by { |pair| pair[1] - pair.first }\n final[1] = final[1] -= 1\n final\n end\n\n def self.size(string)\n result = self.solve(string)\n # puts result\n return 0 if result.empty?\n result[1] - result[0] + 1\n end\nend\n\nstring = ARGF.read\nputs Solver.size(string)\n"}, {"source_code": "class Solver\n\n def self.solve(string)\n result = {}\n height = 0\n result[0] = [0]\n index = 0\n string.each_char do |char|\n index += 1\n height = height + (char == '1' ? 1 : -1)\n if result.fetch(height, nil).nil?\n result[height] = [index]\n elsif result[height].size == 1\n result[height] << index\n else\n result[height].pop\n result[height].push index\n end\n # puts \"#{height}:#{index}\"\n # puts result\n end\n array_result = result.values\n array_result.select! { |pair| pair.size == 2 }\n # puts array_result.inspect\n return [] if array_result.empty?\n final = array_result.max_by { |pair| pair[1] - pair.first }\n final[1] = final[1] -= 1\n final\n end\n\n def self.size(string)\n result = self.solve(string)\n # puts result\n return 0 if result.empty?\n result[1] - result[0] + 1\n end\nend\n\nstring = ARGF.read\nputs Solver.size(string.strip)\n"}], "src_uid": "08fa04303060d38d6cd0f3a321a527ad"} {"nl": {"description": "Alice has a cute cat. To keep her cat fit, Alice wants to design an exercising walk for her cat! Initially, Alice's cat is located in a cell $$$(x,y)$$$ of an infinite grid. According to Alice's theory, cat needs to move: exactly $$$a$$$ steps left: from $$$(u,v)$$$ to $$$(u-1,v)$$$; exactly $$$b$$$ steps right: from $$$(u,v)$$$ to $$$(u+1,v)$$$; exactly $$$c$$$ steps down: from $$$(u,v)$$$ to $$$(u,v-1)$$$; exactly $$$d$$$ steps up: from $$$(u,v)$$$ to $$$(u,v+1)$$$. Note that the moves can be performed in an arbitrary order. For example, if the cat has to move $$$1$$$ step left, $$$3$$$ steps right and $$$2$$$ steps down, then the walk right, down, left, right, right, down is valid.Alice, however, is worrying that her cat might get lost if it moves far away from her. So she hopes that her cat is always in the area $$$[x_1,x_2]\\times [y_1,y_2]$$$, i.e. for every cat's position $$$(u,v)$$$ of a walk $$$x_1 \\le u \\le x_2$$$ and $$$y_1 \\le v \\le y_2$$$ holds.Also, note that the cat can visit the same cell multiple times.Can you help Alice find out if there exists a walk satisfying her wishes?Formally, the walk should contain exactly $$$a+b+c+d$$$ unit moves ($$$a$$$ to the left, $$$b$$$ to the right, $$$c$$$ to the down, $$$d$$$ to the up). Alice can do the moves in any order. Her current position $$$(u, v)$$$ should always satisfy the constraints: $$$x_1 \\le u \\le x_2$$$, $$$y_1 \\le v \\le y_2$$$. The staring point is $$$(x, y)$$$.You are required to answer $$$t$$$ test cases independently.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^3$$$) — the number of testcases. The first line of each test case contains four integers $$$a$$$, $$$b$$$, $$$c$$$, $$$d$$$ ($$$0 \\le a,b,c,d \\le 10^8$$$, $$$a+b+c+d \\ge 1$$$). The second line of the test case contains six integers $$$x$$$, $$$y$$$, $$$x_1$$$, $$$y_1$$$, $$$x_2$$$, $$$y_2$$$ ($$$-10^8 \\le x_1\\le x \\le x_2 \\le 10^8$$$, $$$-10^8 \\le y_1 \\le y \\le y_2 \\le 10^8$$$).", "output_spec": "For each test case, output \"YES\" in a separate line, if there exists a walk satisfying her wishes. Otherwise, output \"NO\" in a separate line. You can print each letter in any case (upper or lower).", "sample_inputs": ["6\n3 2 2 2\n0 0 -2 -2 2 2\n3 1 4 1\n0 0 -1 -1 1 1\n1 1 1 1\n1 1 1 1 1 1\n0 0 0 1\n0 0 0 0 0 1\n5 1 1 1\n0 0 -100 -100 0 100\n1 1 5 1\n0 0 -100 -100 100 0"], "sample_outputs": ["Yes\nNo\nNo\nYes\nYes\nYes"], "notes": "NoteIn the first test case, one valid exercising walk is $$$$$$(0,0)\\rightarrow (-1,0) \\rightarrow (-2,0)\\rightarrow (-2,1) \\rightarrow (-2,2)\\rightarrow (-1,2)\\rightarrow(0,2)\\rightarrow (0,1)\\rightarrow (0,0) \\rightarrow (-1,0)$$$$$$"}, "positive_code": [{"source_code": "gets.to_i.times do\n left, right, down, up = gets.split(' ').map(&:to_i)\n start_x, start_y, min_x, min_y, max_x, max_y = gets.split(' ').map(&:to_i)\n\n if (start_x - left + right).between?(min_x, max_x) && \n (start_y - down + up).between?(min_y, max_y) &&\n (min_x < max_x || left + right == 0) &&\n (min_y < max_y || down + up == 0) \n puts \"Yes\"\n else\n puts \"No\"\n end\nend"}, {"source_code": "gets.to_i.times do\n moves = gets.split(' ').map(&:to_i)\n points = gets.split(' ').map(&:to_i)\n\n left = moves[0]\n right = moves[1]\n down = moves[2]\n up = moves[3]\n\n start_x, start_y = points[0], points[1]\n min_x, min_y = points[2], points[3]\n max_x, max_y = points[4], points[5]\n\n if ((start_x - left + right).between?(min_x, max_x) && (min_x < max_x || left + right == 0)) &&\n ((start_y - down + up).between?(min_y, max_y)) && (min_y < max_y || down + up == 0)\n \n puts \"Yes\"\n else\n puts \"No\"\n end\nend"}, {"source_code": "n = gets.chomp.to_i\n\nn.times do\n a,b,c,d = gets.chomp.split.map(&:to_i)\n\n x,y,x1,y1,x2,y2 = gets.chomp.split.map(&:to_i)\n\n if x1 == x2 && (a > 0 || b > 0)\n puts 'No'\n next\n end\n\n if y1 == y2 && (c > 0 || d > 0)\n puts 'No'\n next\n end\n\n x += b - a\n y += d - c\n if x1 <= x && x <= x2 && y1 <= y && y <= y2\n puts 'Yes'\n else\n puts 'No'\n end\nend\n\n"}, {"source_code": "t = gets.to_i\nt.times do\n a, b, c, d = gets.split.map(&:to_i)\n x, y, x1, y1, x2, y2 = gets.split.map(&:to_i)\n ok = true\n ok = false unless (x1..x2).include?(x - a + b)\n ok = false unless (y1..y2).include?(y - c + d)\n ok = false if (a + b > 0) && (x1 == x2)\n ok = false if (c + d > 0) && (y1 == y2)\n puts ok ? \"YES\" : \"NO\"\nend\n"}, {"source_code": "def solve(d,r)\n d > r\nend\n\nt = gets.to_i\n\nt.times do\n \n l, r, d, u = gets.split.map(&:to_i)\n x, y, xl, yd, xr, yu = gets.split.map(&:to_i)\n \n rr = xr - x\n ll = x - xl\n uu = yu - y\n dd = y - yd\n \n m = rr\n m = ll if m > ll\n m = uu if m > uu\n m = dd if m > dd\n \n if l < r\n if solve(r-l, rr)\n puts \"No\"\n next\n end\n elsif l > r\n if solve(l-r, ll)\n puts \"No\"\n next\n end\n elsif l == r && l > 0\n if ll == 0 && rr == 0\n puts \"No\"\n next\n end\n end\n \n if d < u\n if solve(u-d,uu)\n puts \"No\"\n next\n end\n elsif d > u\n if solve(d-u,dd)\n puts \"No\"\n next\n end\n elsif d == u && d > 0\n if dd == 0 && uu == 0\n puts \"No\"\n next\n end\n end\n \n puts \"Yes\"\nend"}], "negative_code": [{"source_code": "def solve(d,r)\n d > r\nend\n\nt = gets.to_i\n\nt.times do\n \n l, r, d, u = gets.split.map(&:to_i)\n x, y, xl, yd, xr, yu = gets.split.map(&:to_i)\n \n rr = xr - x\n ll = x - xl\n uu = yu - y\n dd = y - yd\n \n m = rr\n m = ll if m > ll\n m = uu if m > uu\n m = dd if m > dd\n \n if l < r\n if solve(r-l, rr)\n puts \"No\"\n next\n end\n elsif l > r\n if solve(l-r, ll)\n puts \"No\"\n next\n end\n elsif l == r && l > 0\n if ll == 0 || rr == 0\n puts \"No\"\n next\n end\n end\n \n if d < u\n if solve(u-d,uu)\n puts \"No\"\n next\n end\n elsif d > u\n if solve(d-u,dd)\n puts \"No\"\n next\n end\n elsif d == u && d > 0\n if dd == 0 || uu == 0\n puts \"No\"\n next\n end\n end\n \n puts \"Yes\"\nend\n"}], "src_uid": "7224ffd4776af4129739e1b28f696050"} {"nl": {"description": "Efim just received his grade for the last test. He studies in a special school and his grade can be equal to any positive decimal fraction. First he got disappointed, as he expected a way more pleasant result. Then, he developed a tricky plan. Each second, he can ask his teacher to round the grade at any place after the decimal point (also, he can ask to round to the nearest integer). There are t seconds left till the end of the break, so Efim has to act fast. Help him find what is the maximum grade he can get in no more than t seconds. Note, that he can choose to not use all t seconds. Moreover, he can even choose to not round the grade at all.In this problem, classic rounding rules are used: while rounding number to the n-th digit one has to take a look at the digit n + 1. If it is less than 5 than the n-th digit remain unchanged while all subsequent digits are replaced with 0. Otherwise, if the n + 1 digit is greater or equal to 5, the digit at the position n is increased by 1 (this might also change some other digits, if this one was equal to 9) and all subsequent digits are replaced with 0. At the end, all trailing zeroes are thrown away.For example, if the number 1.14 is rounded to the first decimal place, the result is 1.1, while if we round 1.5 to the nearest integer, the result is 2. Rounding number 1.299996121 in the fifth decimal place will result in number 1.3.", "input_spec": "The first line of the input contains two integers n and t (1 ≤ n ≤ 200 000, 1 ≤ t ≤ 109) — the length of Efim's grade and the number of seconds till the end of the break respectively. The second line contains the grade itself. It's guaranteed that the grade is a positive number, containing at least one digit after the decimal points, and it's representation doesn't finish with 0.", "output_spec": "Print the maximum grade that Efim can get in t seconds. Do not print trailing zeroes.", "sample_inputs": ["6 1\n10.245", "6 2\n10.245", "3 100\n9.2"], "sample_outputs": ["10.25", "10.3", "9.2"], "notes": "NoteIn the first two samples Efim initially has grade 10.245. During the first second Efim can obtain grade 10.25, and then 10.3 during the next second. Note, that the answer 10.30 will be considered incorrect.In the third sample the optimal strategy is to not perform any rounding at all."}, "positive_code": [{"source_code": "n, t = gets.split.map(&:to_i)\ns = gets.chomp\ndot_ind = s.index('.')\na = s.chars.map(&:to_i)\nind = a[dot_ind..-1].index{|x| x >= 5}\nif ind.nil?\n puts s\n exit\nend\nind += dot_ind\nwhile ind > dot_ind && t > 0 && a[ind] >= 5\n if ind == dot_ind + 1\n i = ind - 2\n while a[i] == 9 && i >= 0\n a[i] = 0\n if i.zero?\n a.insert(0, 0)\n ind += 1\n dot_ind += 1\n break\n end\n i -= 1\n end\n a[i] += 1\n else\n a[ind - 1] += 1\n end\n ind -= 1\n t -= 1\nend\na[dot_ind] = ind == dot_ind ? '' : '.'\nputs a[0..ind].join\n"}], "negative_code": [{"source_code": "n, t = gets.split.map(&:to_i)\ns = gets.chomp\ndot_ind = s.index('.')\na = s.chars.map(&:to_i)\nind = a[dot_ind..-1].index{|x| x >= 5}\nif ind.nil?\n puts s\n exit\nend\nind += dot_ind\np a, dot_ind, ind\nwhile ind > dot_ind && t > 0 && a[ind] >= 5\n if ind == dot_ind + 1\n i = ind - 2\n while a[i] == 9 && i >= 0\n if i.zero?\n a.insert(0, 0)\n ind += 1\n dot_ind += 1\n end\n a[i] = 0\n i -= 1\n end\n a[i] += 1\n else\n a[ind - 1] += 1\n end\n ind -= 1\n t -= 1\nend\na[dot_ind] = ind == dot_ind ? '' : '.'\nputs a[0..ind].join\n"}, {"source_code": "n, t = gets.split.map(&:to_i)\ns = gets.chomp\ndot_ind = s.index('.')\na = s.chars.map(&:to_i)\nind = s[dot_ind..-1].chars.find_index{|x| x.to_i >= 5}\nif ind.nil?\n puts s\n exit\nend\nind += dot_ind\nwhile ind > dot_ind && t > 0 && a[ind] >= 5\n a[ind - (ind == dot_ind + 1 ? 2 : 1)] += 1\n ind -= 1\n t -= 1\nend\na[dot_ind] = ind == dot_ind ? '' : '.'\nputs a[0..ind].join\n"}, {"source_code": "n, t = gets.split.map(&:to_i)\ns = gets.chomp\nind = s.each_char.with_index.find{|x, y| x.to_i >= 5}[1]\n(ind - 1).downto(0) do |i|\n next if s[i] == '.'\n t -= 1\n c = s[i].to_i + 1\n s = s[0..i - 1] + c.to_s\n break if c < 5 || t <= 0\nend\nputs s\n# dot_ind = a.find{|x, y| x == '.'}[1]\n# p dot_ind\n"}, {"source_code": "n, t = gets.split.map(&:to_i)\ns = gets.chomp\ndot_ind = s.index('.')\na = s.chars.map(&:to_i)\nind = a[dot_ind..-1].index{|x| x >= 5}\nif ind.nil?\n puts s\n exit\nend\nind += dot_ind\nwhile ind > dot_ind && t > 0 && a[ind] >= 5\n if ind == dot_ind + 1\n i = ind - 2\n while a[i] == 9 && i >= 0\n if i.zero?\n a.insert(0, 0)\n ind += 1\n dot_ind += 1\n end\n a[i] = 0\n i -= 1\n end\n a[i] += 1\n else\n a[ind - 1] += 1\n end\n ind -= 1\n t -= 1\nend\na[dot_ind] = ind == dot_ind ? '' : '.'\nputs a[0..ind].join\n"}, {"source_code": "n, t = gets.split.map(&:to_i)\ns = gets.chomp\ndot_ind = s.index('.')\nind = s[dot_ind..-1].chars.find_index{|x| x.to_i >= 5} || 0\nind -= 1\nf = s.to_f\nwhile ind >= 0 && t > 0 && f.to_s[dot_ind + ind + 1].to_i >= 5\n f = f.round(ind)\n ind -= 1\n t -= 1\nend\np f\n"}, {"source_code": "n, t = gets.split.map(&:to_i)\ns = gets.chomp\ndot_ind = s.index('.')\na = s.chars.map(&:to_i)\nind = s[dot_ind..-1].chars.find_index{|x| x.to_i >= 5}\nif ind.nil?\n puts s\n exit\nend\nind += dot_ind\nwhile ind > dot_ind && t > 0 && a[ind] >= 5\n if ind == dot_ind + 1\n i = ind - 2\n while a[i] == 9\n a[i] = 0\n i -= 1\n end\n a[i] += 1\n else\n a[ind - 1] += 1\n end\n ind -= 1\n t -= 1\nend\na[dot_ind] = ind == dot_ind ? '' : '.'\nputs a[0..ind].join\n"}], "src_uid": "d563ce32596b54f48544a6085efe5cc5"} {"nl": {"description": "One day, little Vasya found himself in a maze consisting of (n + 1) rooms, numbered from 1 to (n + 1). Initially, Vasya is at the first room and to get out of the maze, he needs to get to the (n + 1)-th one.The maze is organized as follows. Each room of the maze has two one-way portals. Let's consider room number i (1 ≤ i ≤ n), someone can use the first portal to move from it to room number (i + 1), also someone can use the second portal to move from it to room number pi, where 1 ≤ pi ≤ i.In order not to get lost, Vasya decided to act as follows. Each time Vasya enters some room, he paints a cross on its ceiling. Initially, Vasya paints a cross at the ceiling of room 1. Let's assume that Vasya is in room i and has already painted a cross on its ceiling. Then, if the ceiling now contains an odd number of crosses, Vasya uses the second portal (it leads to room pi), otherwise Vasya uses the first portal. Help Vasya determine the number of times he needs to use portals to get to room (n + 1) in the end.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 103) — the number of rooms. The second line contains n integers pi (1 ≤ pi ≤ i). Each pi denotes the number of the room, that someone can reach, if he will use the second portal in the i-th room.", "output_spec": "Print a single number — the number of portal moves the boy needs to go out of the maze. As the number can be rather large, print it modulo 1000000007 (109 + 7).", "sample_inputs": ["2\n1 2", "4\n1 1 2 3", "5\n1 1 1 1 1"], "sample_outputs": ["4", "20", "62"], "notes": null}, "positive_code": [{"source_code": "def f(x)\n if @d[x] != nil\n return @d[x]\n end\n sum = 1\n for i in @p[x]..x - 1\n sum += f(i)\n end\n sum += 1\n @d[x] = sum\n sum\nend\n\nn = Integer gets\n@p = gets.split.map { |x| (Integer x) - 1 }\n@d = Array.new(n, nil)\n@d[0] = 2\nsum = 0\nfor i in 0..n - 1\n sum += f i\nend\nputs sum % (10 ** 9 + 7)"}, {"source_code": "gets\ndp,arr=[0],gets.split.map(&:to_i)\narr.each_with_index{|i,k|dp[k+1]=(dp[k]*2-dp[i-1]+2)%1000000007}\np dp[-1]\n"}, {"source_code": "=begin\n , \\ / ,\n / \\ )\\__/( / \\\n / \\ (_\\ /_) / \\\n ____/_____\\__\\@ @/___/_____\\____\n | |\\../| |\n | \\VV/ |\n | ------hoi------- |\n |_________________________________|\n | /\\ / \\\\ \\ /\\ |\n | / V )) V \\ |\n |/ ` // ' \\|\n ` V '\n=end\n\ndp, n, arr = [],gets.to_i, gets.split.map(&:to_i)\ndp[0]=0\n(1..n).each{|i|dp[i] = (dp[i - 1] * 2 - dp[arr[i - 1] - 1] + 2) % 1000000007}\np dp[n]"}, {"source_code": "gets\ndp,arr=[0],gets.split.map(&:to_i)\narr.each_with_index{|i,k|dp[k+1]=(dp[k]*2-dp[i-1]+2)%1000000007}\np dp[-1]\n"}, {"source_code": "gets\ndp,arr=[0],gets.split.map(&:to_i)\narr.each_with_index{|i,k|dp[k+1]=(dp[k]*2-dp[i-1]+2)%1000000007}\np dp[-1]"}, {"source_code": "gets\ndp,arr=[0],gets.split.map(&:to_i)\narr.each_with_index{|i,k|dp[k+1]=(dp[k]*2-dp[i-1]+2)%1000000007}\np dp[-1]\n"}, {"source_code": "gets\ndp,arr=[0],gets.split.map(&:to_i)\narr.each_with_index{|i,k|dp[k+1]=(dp[k]*2-dp[i-1]+2)%1000000007}\np dp[-1]\n"}, {"source_code": "dp,n,arr=[],gets.to_i,gets.split.map(&:to_i)\ndp[0]=0\n(1..n).each{|i|dp[i]=(dp[i-1]*2-dp[arr[i-1]-1]+2)%1000000007}\np dp[n]"}, {"source_code": "dp,n=[],gets.to_i\narr=gets.split.map &:to_i\nMof=1000000007\ndp[0]=0\n(1..n).each{|i|dp[i]=(dp[i-1]*2-dp[arr[i-1]-1]+2)%Mof}\nputs dp[n]"}, {"source_code": "gets\ndp,arr=[0],gets.split.map(&:to_i)\narr.each_with_index{|i,k|dp[k+1]=(dp[k]*2-dp[i-1]+2)%1000000007}\np dp[-1]"}, {"source_code": "gets\ndp,arr=[0],gets.split.map(&:to_i)\narr.each_with_index{|i,k|dp[k+1]=(dp[k]*2-dp[i-1]+2)%1000000007}\np dp[-1]\n"}, {"source_code": "MOD = 1000000007\nn = STDIN.readline.to_i\np = [0] + STDIN.readline.split.map { |x| x.to_i }\n\nf = Array.new(n+2,0)\n2.upto(n+1) do |i|\n\tf[i] = f[i-1]+1\n\tt = i-1\n\twhile p[t] != t ;\n\t\tf[i] += f[t]-f[p[t]+1]+2\n\t\tflag = true\n\t\tt = p[t]\n\tend\n\tf[i] += 1\n\tf[i] %= MOD\nend\n\nprint f[n+1]\n"}, {"source_code": "gets\ndp,arr=[0],gets.split.map(&:to_i)\narr.each_with_index{|i,k|dp[k+1]=(dp[k]*2-dp[i-1]+2)%1000000007}\np dp[-1]\n"}, {"source_code": "gets\ndp,arr=[0],gets.split.map(&:to_i)\narr.each_with_index{|i,k|dp[k+1]=(dp[k]*2-dp[i-1]+2)%1000000007}\np dp[-1]\n"}, {"source_code": "gets\ndp,arr=[0],gets.split.map(&:to_i)\narr.each_with_index{|i,k|dp[k+1]=(dp[k]*2-dp[i-1]+2)%1000000007}\np dp[-1]\n"}], "negative_code": [{"source_code": "def f(x)\n if @d[x] != nil\n return @d[x]\n end\n sum = 1\n for i in @p[x]..x - 1\n sum += f(i)\n end\n sum += 1\n @d[x] = sum\n sum\nend\n\nn = Integer gets\n@p = gets.split.map { |x| (Integer x) - 1 }\n@d = Array.new(n, nil)\n@d[0] = 2\nsum = 0\nfor i in 0..n - 1\n sum += f i\nend\nputs sum"}], "src_uid": "7bb5df614d1fc8323eba51d04ee7bf2a"} {"nl": {"description": "There was an electronic store heist last night.All keyboards which were in the store yesterday were numbered in ascending order from some integer number $$$x$$$. For example, if $$$x = 4$$$ and there were $$$3$$$ keyboards in the store, then the devices had indices $$$4$$$, $$$5$$$ and $$$6$$$, and if $$$x = 10$$$ and there were $$$7$$$ of them then the keyboards had indices $$$10$$$, $$$11$$$, $$$12$$$, $$$13$$$, $$$14$$$, $$$15$$$ and $$$16$$$.After the heist, only $$$n$$$ keyboards remain, and they have indices $$$a_1, a_2, \\dots, a_n$$$. Calculate the minimum possible number of keyboards that have been stolen. The staff remember neither $$$x$$$ nor the number of keyboards in the store before the heist.", "input_spec": "The first line contains single integer $$$n$$$ $$$(1 \\le n \\le 1\\,000)$$$ — the number of keyboards in the store that remained after the heist. The second line contains $$$n$$$ distinct integers $$$a_1, a_2, \\dots, a_n$$$ $$$(1 \\le a_i \\le 10^{9})$$$ — the indices of the remaining keyboards. The integers $$$a_i$$$ are given in arbitrary order and are pairwise distinct.", "output_spec": "Print the minimum possible number of keyboards that have been stolen if the staff remember neither $$$x$$$ nor the number of keyboards in the store before the heist.", "sample_inputs": ["4\n10 13 12 8", "5\n7 5 6 4 8"], "sample_outputs": ["2", "0"], "notes": "NoteIn the first example, if $$$x=8$$$ then minimum number of stolen keyboards is equal to $$$2$$$. The keyboards with indices $$$9$$$ and $$$11$$$ were stolen during the heist.In the second example, if $$$x=4$$$ then nothing was stolen during the heist."}, "positive_code": [{"source_code": "n,*a=$<.read.split.map &:to_i\np a.max-a.min+1-n"}, {"source_code": "n,*a=$<.read.split.map &:to_i\np a.max-a.min+1-n"}, {"source_code": "# TEMPLATE BEGIN\ndef read_vector\n gets.chomp.split(/\\s+/)\nend\ndef read_vector_int\n read_vector.map(&:to_i)\nend\n# TEMPLATE END\n\ngets\na = read_vector_int\nputs a.max - a.min - a.size + 1\n"}, {"source_code": "n = gets.to_i\nary = gets.strip.split.map(&:to_i).sort\n\nans = 0\nsz =ary.size\n\n1.upto(sz-1).each do |i|\n ans += (ary[i] - ary[i-1] - 1)\nend\n\nputs ans"}, {"source_code": " n,*a=$<.read.split.map &:to_i\n p a.max-a.min+1-n"}, {"source_code": "n = gets.to_i\nas = gets.split.map(&:to_i).sort\n\nputs as[-1] - as[0] - n + 1"}, {"source_code": "remain_keyborads = gets.chomp.to_i\nvalues = gets.chomp\n\nremain_keybord_indicies = values.split(' ')\nremain_keybord_indicies.map!(&:to_i)\n\nmax = remain_keybord_indicies.max\nmin = remain_keybord_indicies.min\n\nputs max - min - remain_keyborads + 1"}, {"source_code": "gets\na = gets.split.map(&:to_i).sort\ni = 1\nans = 0\nwhile i < a.size\n ans += a[i] - a[i - 1] - 1\n i += 1\nend\nputs ans\n"}], "negative_code": [{"source_code": "n,*a=$<.read.split.map &:to_i\np n-a.max+a.min-1"}], "src_uid": "6469ed9f2486b498c9ffeb8270391e3a"} {"nl": {"description": "Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style.Volodya calls a string powerful if it starts with \"heavy\" and ends with \"metal\". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text.For simplicity, let us assume that Volodya's text can be represented as a single string.", "input_spec": "Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters.", "output_spec": "Print exactly one number — the number of powerful substrings of the given string. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.", "sample_inputs": ["heavymetalisheavymetal", "heavymetalismetal", "trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou"], "sample_outputs": ["3", "2", "3"], "notes": "NoteIn the first sample the string \"heavymetalisheavymetal\" contains powerful substring \"heavymetal\" twice, also the whole string \"heavymetalisheavymetal\" is certainly powerful.In the second sample the string \"heavymetalismetal\" contains two powerful substrings: \"heavymetal\" and \"heavymetalismetal\"."}, "positive_code": [{"source_code": "#!/usr/bin/env ruby\nstr = gets\nharr = []\nmarr = []\nindex = -1\nloop do\n index = str.index('heavy', index + 1)\n break unless index\n harr << index\nend\nindex = -1\nloop do\n index = str.index('metal', index + 1)\n break unless index\n marr << index\nend\nret = 0\nmatch = 0\nharr.each do |hi|\n find = false\n for i in match...marr.size\n match = i\n if marr[i] > hi\n find = true\n break\n end\n end\n ret += marr.size - match if find\nend\nputs ret\n"}, {"source_code": "def get_pos(s, ss)\n x, l = [], ss.length\n i = -l\n while true\n i = s.index(ss, i + l)\n break if i.nil?\n x << i\n end\n x\nend\n\ns = gets.chomp\nx = get_pos(s, \"heavy\")\ny = get_pos(s, \"metal\")\ntotal, pox, poy, sx, sy = 0, 0, 0, x.size, y.size\nwhile pox < sx\n poy += 1 while poy < sy && y[poy] < x[pox]\n total += sy - poy\n pox += 1\nend\nputs total"}, {"source_code": "input = gets.chomp\nheavy = 0\nitr = 0\nlen = input.length\nans = 0\n\nuntil itr >= len\n\tif input[itr] == 'h'\n\t\tif input[itr..(itr+4)] == \"heavy\" \n\t\t\theavy += 1\n\t\t\titr += 5\n\t\telse itr += 1\n\t\tend\n\telsif input[itr] == 'm'\n\t\tif input[itr..(itr+4)] == \"metal\"\n\t\t\tans += heavy\n\t\t\titr += 5\n\t\telse \titr += 1\n\t\tend\n\telse\n\t\titr += 1\n\tend\nend\n\nputs ans\n\n\n\n\n"}, {"source_code": "s = gets.strip\nh = []\nm = []\noffset = 0\n\nwhile n = s.index(\"heavy\", offset) do\n h << n\n offset = n + 1\nend\n\nif h.size == 0 then\n puts 0\n exit\nend\n\noffset = 0\ntotal = 0\nh_idx = -1\nwhile n = s.index(\"metal\", offset) do\n while h_idx+1 < h.size and h[h_idx+1] < n do\n h_idx += 1\n end\n total += h_idx + 1\n offset = n + 1\nend\n\nputs total"}, {"source_code": "S = gets.chomp\n\nh = 0\nans = 0\n\n(0...S.length).each do |i|\n \n h += 1 if S[i,5] == \"heavy\"\n ans += h if S[i,5] == \"metal\"\n\nend\n\nputs ans"}, {"source_code": "s = gets\n\nres = 0\ni=-1\nh = []\nloop do\n i = s.index('heavy', i+1)\n break if i.nil?\n h << i\nend\n\ni=-1\nj=0\nloop do\n i = s.index('metal', i+1)\n break if i.nil?\n\n while j < h.size and h[j] < i\n j+=1\n end\n res += j\nend\n\nputs res\n"}, {"source_code": "#! ruby -Ku\n# -*- coding: UTF-8 -*-\na = gets\n\nstr_front = \"heavy\"\nstr_rear = \"metal\"\n\ncount_front = 0\nsum_substring = 0\n\noffset_front = 0\noffset_rear = 0\nwhile true\n offset_front_temp = a.index(str_front, offset_front)\n offset_rear_temp = a.index(str_rear, offset_rear)\n if offset_front_temp == nil\n offset_front_temp = 10 ** 6\n offset_front = 10 ** 6\n end\n if offset_rear_temp == nil\n break\n end\n if offset_front_temp < offset_rear_temp\n count_front += 1\n offset_front = offset_front_temp + 4\n else\n sum_substring += count_front\n offset_rear = offset_rear_temp + 4\n end\nend\n\nputs sum_substring\n"}, {"source_code": "#!/usr/bin/env ruby\n\ndef get_pos(s, ss)\n\tx, l = [], ss.length\n\ti = -l\n\twhile true\n\t\ti = s.index(ss, i + l)\n\t\tbreak if i.nil?\n\t\tx << i\n\tend\n\tx\nend\n\ns = gets.chomp\nx = get_pos(s, \"heavy\")\ny = get_pos(s, \"metal\")\ntot, px, py, sx, sy = 0, 0, 0, x.size, y.size\nwhile px < sx\n\tpy += 1 while py < sy && y[py] < x[px]\n\ttot += sy - py\n\tpx += 1\nend\nputs tot\n"}, {"source_code": "#!/usr/bin/env ruby\n\ndef get_pos(s, ss)\n\tx, ls, lss = [], s.length, ss.length\n\tls.times { |i| x << i if s[i..i+lss-1] == ss }\n\tx\nend\n\ns = gets.chomp\nx = get_pos(s, \"heavy\")\ny = get_pos(s, \"metal\")\ntot, px, py, sx, sy = 0, 0, 0, x.size, y.size\nwhile px < sx\n\tpy += 1 while py < sy && y[py] < x[px]\n\ttot += sy - py\n\tpx += 1\nend\nputs tot\n"}, {"source_code": "s = gets\ncount = 0\nheavy = 0\nfor i in 0..s.length-5\n\tif s[i..i+4] == \"heavy\"\n\t\theavy += 1\n\tend\n\n\tif s[i..i+4] == \"metal\"\n\t\tcount += heavy\n\tend\nend\n\nputs count"}], "negative_code": [{"source_code": "#!/usr/bin/env ruby\nstr = gets\nharr = []\nmarr = []\nindex = -1\nloop do\n index = str.index('heavy', index + 1)\n break unless index\n harr << index\nend\nindex = -1\nloop do\n index = str.index('metal', index + 1)\n break unless index\n marr << index\nend\nret = 0\nharr.each do |hi|\n match = 0\n for i in match...marr.size\n match = i\n break if marr[i] >= hi\n end\n ret += marr.size - match\nend\nputs ret\n"}, {"source_code": "def get_pos(s,ss)\n x, l = [],ss.length\n i = -l\n while true\n i = s.index(ss,i+1)\n break if i.nil?\n x << i\n end\n x\nend\n\ns = gets.chomp\nx = get_pos(s,\"heavy\")\ny = get_pos(s,\"metal\")\ntotal,pox,poy,sx,sy = 0,0,0,x.size,y.size\nwhile pox < sx\n py += 1 while poy < sy && y[poy] < x[pox]\n total += sy - poy\n pox += 1\nend\nputs total"}, {"source_code": "def get_pos(s,ss)\n x, l = [],ss.length\n i = -l\n while true\n i = s.index(ss,i+1)\n break if i.nil?\n x << i\n end\n x\nend\n\ns = gets.chomp\nx = get_pos(s,\"heavy\")\ny = get_pos(s,\"metal\")\ntotal,pox,poy,sx,sy = 0,0,0,x.size,y.size\nwhile pox < sx\n py += 1 while poy < sy && y[py] < x[pox]\n total += sy - poy\n pox += 1\nend\nputs total"}, {"source_code": "#! ruby -Ku\n# -*- coding: UTF-8 -*-\na = gets\n# puts a\n\nstr_front = \"heavy\"\nstr_rear = \"metal\"\n\noffset_front = a.index(str_front)\noffset_front_array = [a.index(str_front)]\nwhile offset_front != nil\n offset_front = a.index(str_front, offset_front + 1)\n offset_front_array << offset_front\nend\n\noffset_rear = a.index(str_rear)\noffset_rear_array = [a.index(str_rear)]\nwhile offset_rear != nil\n offset_rear = a.index(str_rear, offset_rear + 1)\n offset_rear_array << offset_rear\nend\n\n# puts offset_front_array\n# puts offset_rear_array\n\ncount_front = 0\nsum_substring = 0\n\noffset_rear = offset_rear_array.first\nwhile offset_rear != nil\n count_front += 1\n val_front = offset_front_array.shift\n val2_front = offset_front_array.first\n offset_rear = offset_rear_array.shift\n if offset_rear == nil\n break\n elsif val2_front == nil\n while offset_rear != nil\n sum_substring += count_front\n offset_rear = offset_rear_array.shift\n end\n else\n while val_front < offset_rear && offset_rear < val2_front\n sum_substring += count_front\n offset_rear = offset_rear_array.first\n if offset_rear == nil\n break\n end\n end\n end\nend\n\nputs sum_substring"}, {"source_code": "#! ruby -Ku\n# -*- coding: UTF-8 -*-\na = gets\n\nstr_front = \"heavy\"\nstr_rear = \"metal\"\n\noffset_front = a.index(str_front)\noffset_front_array = [a.index(str_front)]\nwhile offset_front != nil\n offset_front = a.index(str_front, offset_front + 1)\n offset_front_array << offset_front\nend\n\noffset_rear = a.index(str_rear)\noffset_rear_array = [a.index(str_rear)]\nwhile offset_rear != nil\n offset_rear = a.index(str_rear, offset_rear + 1)\n offset_rear_array << offset_rear\nend\n\n# puts offset_front_array\n# puts offset_rear_array\n\ncount_front = 0\nsum_substring = 0\n\nwhile (offset_rear = offset_rear_array.first) != nil\n count_front += 1\n interval_front = offset_front_array.first(2)\n first_front = interval_front.shift\n second_front = interval_front.shift\n if first_front == nil\n first_front = -1\n end\n if second_front == nil\n second_front = 10**6\n end\n while (first_front < offset_rear) && (offset_rear < second_front)\n sum_substring += count_front\n offset_rear_array.shift\n offset_rear = offset_rear_array.first\n if offset_rear == nil\n break\n end\n end\n offset_front_array.shift\nend\n\nputs sum_substring\n"}, {"source_code": "s = gets\ncount = 0\nfor i in (0..s.length) do\n\ta = s.index(\"heavy\", i)\n\tif a and a == i\n\t\tb = s.index(\"metal\", i)\n\t\tif b and b > a\n\t\t\tcount = count + 1\n\t\tend\n\tend\nend\n\nprint count"}], "src_uid": "960e4c234666d2444b80d5966f1d285d"} {"nl": {"description": "Boboniu likes bit operations. He wants to play a game with you.Boboniu gives you two sequences of non-negative integers $$$a_1,a_2,\\ldots,a_n$$$ and $$$b_1,b_2,\\ldots,b_m$$$.For each $$$i$$$ ($$$1\\le i\\le n$$$), you're asked to choose a $$$j$$$ ($$$1\\le j\\le m$$$) and let $$$c_i=a_i\\& b_j$$$, where $$$\\&$$$ denotes the bitwise AND operation. Note that you can pick the same $$$j$$$ for different $$$i$$$'s.Find the minimum possible $$$c_1 | c_2 | \\ldots | c_n$$$, where $$$|$$$ denotes the bitwise OR operation.", "input_spec": "The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1\\le n,m\\le 200$$$). The next line contains $$$n$$$ integers $$$a_1,a_2,\\ldots,a_n$$$ ($$$0\\le a_i < 2^9$$$). The next line contains $$$m$$$ integers $$$b_1,b_2,\\ldots,b_m$$$ ($$$0\\le b_i < 2^9$$$).", "output_spec": "Print one integer: the minimum possible $$$c_1 | c_2 | \\ldots | c_n$$$.", "sample_inputs": ["4 2\n2 6 4 0\n2 4", "7 6\n1 9 1 9 8 1 0\n1 1 4 5 1 4", "8 5\n179 261 432 162 82 43 10 38\n379 357 202 184 197"], "sample_outputs": ["2", "0", "147"], "notes": "NoteFor the first example, we have $$$c_1=a_1\\& b_2=0$$$, $$$c_2=a_2\\& b_1=2$$$, $$$c_3=a_3\\& b_1=0$$$, $$$c_4 = a_4\\& b_1=0$$$.Thus $$$c_1 | c_2 | c_3 |c_4 =2$$$, and this is the minimal answer we can get."}, "positive_code": [{"source_code": "n,m = gets.chomp.split(\" \").map(&:to_i)\na = gets.chomp.split(\" \").map(&:to_i)\nb = gets.chomp.split(\" \").map(&:to_i)\naandb = Array.new(n){Array.new(m)}\nbit = Array.new(n){Array.new(10,0)}\nn.times do |i|\n m.times do |j|\n aandb[i][j] = a[i]&b[j]\n 9.downto(0) do |k|\n bit[i][k] += 1 if aandb[i][j][k] == 1\n end\n end\nend\nans = \"\"\n9.downto(0) do |i|\n judge = false\n n.times do |j|\n if bit[j][i] == aandb[j].length\n judge = true\n break\n end\n end\n if judge\n ans = ans + \"1\"\n else\n ans = ans + \"0\"\n n.times do |j|\n m.times do |k|\n if aandb[j][k][i] == 1\n 9.downto(0) do |l|\n if aandb[j][k][l] == 0\n bit[j][l] += 1\n end\n end\n aandb[j][k] = 2 ** 9 - 1\n end\n end\n end\n end\nend\nputs ans.to_i(2)"}], "negative_code": [], "src_uid": "3da5075048a127319ffa8913859d2aa7"} {"nl": {"description": "Let's call a positive integer $$$n$$$ ordinary if in the decimal notation all its digits are the same. For example, $$$1$$$, $$$2$$$ and $$$99$$$ are ordinary numbers, but $$$719$$$ and $$$2021$$$ are not ordinary numbers.For a given number $$$n$$$, find the number of ordinary numbers among the numbers from $$$1$$$ to $$$n$$$.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$). Then $$$t$$$ test cases follow. Each test case is characterized by one integer $$$n$$$ ($$$1 \\le n \\le 10^9$$$).", "output_spec": "For each test case output the number of ordinary numbers among numbers from $$$1$$$ to $$$n$$$.", "sample_inputs": ["6\n1\n2\n3\n4\n5\n100"], "sample_outputs": ["1\n2\n3\n4\n5\n18"], "notes": null}, "positive_code": [{"source_code": "results = []\n\nt = gets.chomp\n(1..t.to_i).each do |iteration|\n\tn = gets.chomp.to_i\n\tc = 0\n\tif n > 9\n\t\trz = n.to_s.length\n\t\tc += 9 * (rz - 1)\n\t\thigh_rz = n.to_s[0].to_i\n\t\tc += high_rz\n\t\tc -= 1 if n < (high_rz.to_s * rz).to_i\n\telse\n\t\tc = n % 10\n\tend\n\tresults.push(c)\nend\nresults.each do |res|\n\tputs res\nend"}, {"source_code": "def find_ordinaries(n)\n\tif(n < 10)\n\t\treturn n\n\tend\n\tnum_of_zeroes = find_num_of_0s(n)\n\ti = 0\n\tj = build_j(num_of_zeroes, n)\n\ti = j\n\tfirst_dig = 10**num_of_zeroes\n\tchange_first = 10**num_of_zeroes\n\tordinaries = num_of_zeroes * 9\n\twhile(true)\n\t\tif(first_dig + j <= n)\n\t\t\tordinaries += 1\n\t\telse\n\t\t\tbreak\n\t\tend\n\t\tj = j + i\n\t\tfirst_dig += change_first\n\tend\n\treturn ordinaries\nend\n\ndef find_num_of_0s(n)\n\tnum_of_zeroes = 0\n\twhile(true)\n\t\tn = n / 10\n\t\tnum_of_zeroes += 1\n\t\tif(n < 1)\n\t\t\tnum_of_zeroes -= 1\n\t\t\tbreak\n\t\tend\n\tend\n\treturn num_of_zeroes\nend\n\ndef build_j(zeroes, n)\n\ti = zeroes\n\tj = 0\n\twhile(i != 0)\n\t\tj += 10**(zeroes - i)\n\t\ti -= 1\n\tend\n\treturn j\nend\n\ntest_cases = gets.to_i\ntest_case_tab = []\n\nuntil(test_case_tab.size == test_cases)\n\ttest_case_tab.push(gets.to_i)\nend\n\ntest_case_tab.each do|val| \n\tord = find_ordinaries(val)\n\tputs ord\nend"}, {"source_code": "gets.to_i.times {\n n = gets.to_i\n ans = 0\n (1..9).each { |d|\n c = d\n while c <= n\n ans += 1\n c = c * 10 + d\n end\n }\n p ans\n}\n"}, {"source_code": "# @type [Integer]\nt = gets.chomp.to_i\n\ndef test_number\n # @type [Integer]\n n = gets.chomp.to_i\n\n if n < 10\n n\n else\n num_sets = (Math.log10 n).floor\n\n (9 * num_sets) + count_remaining_normals(n, num_sets + 1)\n end\nend\n\ndef count_remaining_normals(number, num_digits)\n # @type [String]\n digits = number.to_s\n\n close_guess = digits[0].to_i\n nearest_boundary = Array.new(num_digits, digits[0]).join.to_i\n\n if digits.chars.uniq.length == 1 || number > nearest_boundary\n # Number is ON the boundary, 222 for example\n close_guess\n else\n # Number is under the boundary, 200 for example\n close_guess - 1\n end\nend\n\n(1..t).each do\n puts test_number\nend\n"}, {"source_code": "def ordinary_cnt(n)\n if n < 10\n return n\n end\n n_str = n.to_s\n x = (n_str[0 .. 0] * n_str.size).to_i\n if n >= x\n return n_str[0 .. 0].to_i + ordinary_cnt((\"9\" * (n_str.size - 1)).to_i)\n else\n return n_str[0 .. 0].to_i - 1 + ordinary_cnt((\"9\" * (n_str.size - 1)).to_i)\n end\nend\n\nt = gets.to_s.to_i\n1.upto(t) do |casenum|\n n = gets.to_s.to_i\n printf(\"%d\\n\", ordinary_cnt(n))\nend\n"}], "negative_code": [{"source_code": "results = []\n\nt = gets.chomp\n(1..t.to_i).each do |iteration|\n\tn = gets.chomp.to_i\n\tc = 0\n\tif n > 9\n\t\tc += n.to_s[-2..-1].to_i/11 + 9\n\t\tc += 9*(n/100) if n > 99\n\telse\n\t\tc = n % 10\n\tend\n\tresults.push(c)\nend\nresults.each do |res|\n\tputs res\nend"}, {"source_code": "results = []\n\nt = gets.chomp\n(1..t.to_i).each do |iteration|\n\tn = gets.chomp.to_i\n\tc = 0\n\tif n > 9\n\t\trzrd = n.to_s.split(\"\").length - 2\n\t\tc = n/11 + 9\n\t\tc -= rzrd if rzrd > 0\n\telse\n\t\tc = n % 10\n\tend\n\tresults.push(c)\nend\nresults.each do |res|\n\tputs res\nend"}, {"source_code": "results = []\n\nt = gets.chomp\n(1..t.to_i).each do |iteration|\n\tn = gets.chomp.to_i\n\tc = 0\n\tif n > 9\n\t\tc = n/11 + 9\n\telse\n\t\tc = n % 10\n\tend\n\tresults.push(c)\nend\nresults.each do |res|\n\tputs res\nend"}, {"source_code": "def find_ordinaries(n)\n\tif(n < 10)\n\t\treturn n\n\tend\n\tnum_of_zeroes = find_num_of_0s(n)\n\ti = 0\n\tj = build_j(num_of_zeroes, n)\n\tordinaries = num_of_zeroes * 9\n\twhile(true)\n\t\tif(10**num_of_zeroes + j + i <= n)\n\t\t\tordinaries += 1\n\t\telse\n\t\t\tbreak\n\t\tend\n\t\ti += 10**num_of_zeroes\n\t\tj += j\n\tend\n\treturn ordinaries\nend\n\ndef find_num_of_0s(n)\n\tnum_of_zeroes = 0\n\twhile(true)\n\t\tn = n / 10\n\t\tnum_of_zeroes += 1\n\t\tif(n < 1)\n\t\t\tnum_of_zeroes -= 1\n\t\t\tbreak\n\t\tend\n\tend\n\treturn num_of_zeroes\nend\n\ndef build_j(zeroes, n)\n\ti = zeroes\n\tj = 0\n\twhile(i != 0)\n\t\tj += 10**(zeroes - i)\n\t\ti -= 1\n\tend\n\treturn j\nend\n\ntest_cases = gets.to_i\ntest_case_tab = []\n\nuntil(test_case_tab.size == test_cases)\n\ttest_case_tab.push(gets.to_i)\nend\n\ntest_case_tab.each do|val| \n\tord = find_ordinaries(val)\n\tputs ord\nend"}, {"source_code": "def find_ordinaries(n)\n\tif(n < 10)\n\t\treturn n\n\tend\n\tnum_of_zeroes = find_num_of_0s(n)\n\ti = 0\n\tj = 10**(num_of_zeroes - 1) + 1\n\t(j == 2) ? j = 1 : j = j\n\tordinaries = num_of_zeroes * 9\n\twhile(true)\n\t\tif(10**num_of_zeroes + j + i <= n)\n\t\t\tordinaries += 1\n\t\telse\n\t\t\tbreak\n\t\tend\n\t\ti += 10**num_of_zeroes\n\t\tj += 1\n\tend\n\treturn ordinaries\nend\n\ndef find_num_of_0s(n)\n\tnum_of_zeroes = 0\n\twhile(true)\n\t\tn = n / 10\n\t\tnum_of_zeroes += 1\n\t\tif(n < 1)\n\t\t\tnum_of_zeroes -= 1\n\t\t\tbreak\n\t\tend\n\tend\n\treturn num_of_zeroes\nend\n\ntest_cases = gets.to_i\ntest_case_tab = []\n\nuntil(test_case_tab.size == test_cases)\n\ttest_case_tab.push(gets.to_i)\nend\n\ntest_case_tab.each do|val| \n\tord = find_ordinaries(val)\n\tputs ord\nend"}, {"source_code": "def find_ordinaries(n)\n\tif(n < 10)\n\t\treturn n\n\tend\n\tnum_of_zeroes = find_num_of_0s(n)\n\ti = 0\n\tj = 10**(num_of_zeroes - 1) + 1\n\t(j == 2) ? j = 1 : j = j\n\tordinaries = num_of_zeroes * 9\n\twhile(true)\n\t\tif(10**num_of_zeroes + j + i <= n)\n\t\t\tordinaries += 1\n\t\t\tp num_of_zeroes\n\t\telse\n\t\t\tbreak\n\t\tend\n\t\ti += 10**num_of_zeroes\n\t\tj += 1\n\tend\n\treturn ordinaries\nend\n\ndef find_num_of_0s(n)\n\tnum_of_zeroes = 0\n\twhile(true)\n\t\tn = n / 10\n\t\tnum_of_zeroes += 1\n\t\tif(n < 1)\n\t\t\tnum_of_zeroes -= 1\n\t\t\tbreak\n\t\tend\n\tend\n\treturn num_of_zeroes\nend\n\ntest_cases = gets.to_i\ntest_case_tab = []\n\nuntil(test_case_tab.size == test_cases)\n\ttest_case_tab.push(gets.to_i)\nend\n\ntest_case_tab.each do|val| \n\tord = find_ordinaries(val)\n\tputs ord\nend"}, {"source_code": "def find_ordinaries(n)\n\tif(n < 10)\n\t\treturn n\n\tend\n\tnum_of_zeroes = find_num_of_0s(n)\n\ti = 0\n\tordinaries = num_of_zeroes * 9\n\twhile(true)\n\t\tif(10**num_of_zeroes + 1 + i <= n)\n\t\t\tordinaries += 1\n\t\telse\n\t\t\tbreak\n\t\tend\n\t\ti += 10**num_of_zeroes\n\tend\n\treturn ordinaries\nend\n\ndef find_num_of_0s(n)\n\tnum_of_zeroes = 0\n\twhile(true)\n\t\tn = n / 10\n\t\tnum_of_zeroes += 1\n\t\tif(n < 1)\n\t\t\tnum_of_zeroes -= 1\n\t\t\tbreak\n\t\tend\n\tend\n\treturn num_of_zeroes\nend\n\ntest_cases = gets.to_i\ntest_case_tab = []\n\nuntil(test_case_tab.size == test_cases)\n\ttest_case_tab.push(gets.to_i)\nend\n\ntest_case_tab.each do|val| \n\tord = find_ordinaries(val)\n\tputs ord\nend"}], "src_uid": "ac7d117d58046872e9d665c9f99e5bff"} {"nl": {"description": "You are given two arrays $$$a$$$ and $$$b$$$ of $$$n$$$ positive integers each. You can apply the following operation to them any number of times: Select an index $$$i$$$ ($$$1\\leq i\\leq n$$$) and swap $$$a_i$$$ with $$$b_i$$$ (i. e. $$$a_i$$$ becomes $$$b_i$$$ and vice versa). Find the minimum possible value of $$$\\max(a_1, a_2, \\ldots, a_n) \\cdot \\max(b_1, b_2, \\ldots, b_n)$$$ you can get after applying such operation any number of times (possibly zero). ", "input_spec": "The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 100$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1\\le n\\le 100$$$) — the length of the arrays. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le 10\\,000$$$) where $$$a_i$$$ is the $$$i$$$-th element of the array $$$a$$$. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \\ldots, b_n$$$ ($$$1 \\le b_i \\le 10\\,000$$$) where $$$b_i$$$ is the $$$i$$$-th element of the array $$$b$$$.", "output_spec": "For each test case, print a single integer, the minimum possible value of $$$\\max(a_1, a_2, \\ldots, a_n) \\cdot \\max(b_1, b_2, \\ldots, b_n)$$$ you can get after applying such operation any number of times.", "sample_inputs": ["3\n\n6\n\n1 2 6 5 1 2\n\n3 4 3 2 2 5\n\n3\n\n3 3 3\n\n3 3 3\n\n2\n\n1 2\n\n2 1"], "sample_outputs": ["18\n9\n2"], "notes": "NoteIn the first test, you can apply the operations at indices $$$2$$$ and $$$6$$$, then $$$a = [1, 4, 6, 5, 1, 5]$$$ and $$$b = [3, 2, 3, 2, 2, 2]$$$, $$$\\max(1, 4, 6, 5, 1, 5) \\cdot \\max(3, 2, 3, 2, 2, 2) = 6 \\cdot 3 = 18$$$.In the second test, no matter how you apply the operations, $$$a = [3, 3, 3]$$$ and $$$b = [3, 3, 3]$$$ will always hold, so the answer is $$$\\max(3, 3, 3) \\cdot \\max(3, 3, 3) = 3 \\cdot 3 = 9$$$.In the third test, you can apply the operation at index $$$1$$$, then $$$a = [2, 2]$$$, $$$b = [1, 1]$$$, so the answer is $$$\\max(2, 2) \\cdot \\max(1, 1) = 2 \\cdot 1 = 2$$$."}, "positive_code": [{"source_code": "t = gets.chomp.to_i\r\n\r\nfor i in (1..t)\r\n n = gets.chomp.to_i\r\n a = gets.chomp.split.map {|el| el.to_i}\r\n b = gets.chomp.split.map {|el| el.to_i}\r\n # maior = [a.max, b.max].max \r\n for j in (0..n-1)\r\n if a[j] < b[j]\r\n temp = a[j]\r\n a[j] = b[j]\r\n b[j] = temp\r\n end\r\n end\r\n puts a.max * b.max\r\nend\r\n"}, {"source_code": "gets.to_i.times do\n n = gets.to_i\n xs = gets.split.map(&:to_i)\n ys = gets.split.map(&:to_i)\n\n (0...n).each do |i|\n if ys[i] > xs[i]\n t = ys[i]\n ys[i] = xs[i]\n xs[i] = t\n end\n end\n\n p(xs.max * ys.max)\nend\n"}], "negative_code": [], "src_uid": "56197d2468d8515e520c0d925874bf3b"} {"nl": {"description": "Levko loves tables that consist of n rows and n columns very much. He especially loves beautiful tables. A table is beautiful to Levko if the sum of elements in each row and column of the table equals k.Unfortunately, he doesn't know any such table. Your task is to help him to find at least one of them. ", "input_spec": "The single line contains two integers, n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 1000).", "output_spec": "Print any beautiful table. Levko doesn't like too big numbers, so all elements of the table mustn't exceed 1000 in their absolute value. If there are multiple suitable tables, you are allowed to print any of them.", "sample_inputs": ["2 4", "4 7"], "sample_outputs": ["1 3\n3 1", "2 1 0 4\n4 0 2 1\n1 3 3 0\n0 3 2 2"], "notes": "NoteIn the first sample the sum in the first row is 1 + 3 = 4, in the second row — 3 + 1 = 4, in the first column — 1 + 3 = 4 and in the second column — 3 + 1 = 4. There are other beautiful tables for this sample.In the second sample the sum of elements in each row and each column equals 7. Besides, there are other tables that meet the statement requirements."}, "positive_code": [{"source_code": "n,k=gets.split.map(&:to_i)\nn.times{|i|a=[0]*n;a[i]=k;puts a.join(' ')}\n"}, {"source_code": "class A\n def initialize\n n, k = gets.chomp.split(' ').map(&:to_i)\n\n answer = []\n\n n.times do |i|\n data = []\n n.times do |j|\n if i == j \n data << k\n else\n data << 0\n end\n end\n answer << data\n end\n\n answer.each do |ans|\n puts ans.join(' ')\n end\n end\nend\n\na = A.new"}, {"source_code": "n,k=gets.split.map(&:to_i)\nn.times{|i|a=[0]*n;a[i]=k;puts a.join(' ')}\n\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\nn.times do |i|\n line = [1]*i +[k-n+1] + [1]*(n-1-i)\n puts line.join(\" \")\nend\n"}, {"source_code": "n,k=gets.split.map(&:to_i)\nn.times{|i|a=[0]*n;a[i]=k;puts a.join(' ')}\n\n"}, {"source_code": "n, k = gets.chomp.split(/ /).map(&:to_i)\nbase = 0\nbase = k / n if n.even? && k.even?\ntable = Array.new(n) { Array.new(n, base) }\nif base == 0\n n.times do |i|\n table[i][i] = k\n end\nend\ntable.each do |row|\n puts row.join(\" \")\nend\n"}, {"source_code": "n, k = gets.chomp.split(\" \").map{|i| i.to_i}\n\nelement = (k.to_f / n).ceil\nn.times do |i|\n n.times do |j|\n if i == j\n print element - (element * n - k)\n else\n print element\n end\n print \" \" if j < n - 1\n end\n print \"\\n\" if i < n - 1\nend\n"}, {"source_code": "n,k=gets.split.map(&:to_i)\na=[]\n(n-1).times { a<<0 }\na< 0\n if a == n - 1\n p -1\n exit\n else\n c << 1\n end\nend\nb.times{c << c.inject(&:+) + 1}\na.times{c << c.max + 1}\nc << 1 while c.size < n\nputs c * ' '\n"}, {"source_code": "n,a,b=gets.split.map(&:to_i)\nresult=Array.new(n,1)\nif a+1==n && a>0 then\n puts -1\n exit\nend\nb.times{|i|\n sum=0\n (i+1).times{|j|\n sum+=result[j]\n }\n result[i+1]=sum+1\n}\nresult[0]=2 if b==0\nresult[1]=1 if b==0\nstart=b\nif b>0 then\n a.times{\n result[start+1]=result[start]+1\n start+=1\n }\nelse\n start=1\n result[1]=2\n a.times{\n result[start+1]=result[start]+1\n start+=1\n }\n result[1]=1\nend\nif result[start-1]>50000 then\n puts -1\n exit\nend\nn.times{|i| p result[i]}\n"}, {"source_code": "n,a,b=gets.split.map &:to_i\nc=[1]\nif b==0&&a>0\n\tif a==n-1\n\t\tp -1\n\t\texit\n\telse\n\t\tc<<1\n\tend\nend\nb.times{|_|c<<2*2**_}\na.times{c<0\n\tif a==n-1\n\t\tp -1\n\t\texit\n\telse\n\t\tc<<1\n\tend\nend\nb.times{|_|c<<2*2**_}\na.times{c< 0\n if a+1 == n\n puts -1\n exit\n end\n result.push(1)\nend\nb.times { result.push(2*result[-1]) }\na.times { result.push(result[-1]+1) }\nwhile result.length < n\n result.push(result[-1])\nend\nputs result.join ' '\n"}, {"source_code": "n, a, b = gets.split.map(&:to_i)\nif n - (b > 0 ? b+1 : b) - (b > 0 ? a : ( a > 0 ? a+2:1)) < 0\n puts(-1)\nelse\n bb = [1] + (1..b).map {|x| 2**x}.to_a\n bb << 1 if b == 0 and a != 0\n last = bb[-1]\n a.times do |i|\n bb << bb[-1] + 1\n end\n (n-bb.size).times do\n bb << bb[-1]\n end\n puts bb*' '\nend"}, {"source_code": "n,a,b=gets.split.map &:to_i\nc=[1]\nif b==0&&a>0\n\tif a==n-1\n\t\tp -1\n\t\texit\n\telse\n\t\tc<<1\n\tend\nend\nb.times{|_|c<<2*2**_}\na.times{c<0\n\tif a==n-1\n\t\tp -1\n\t\texit\n\telse\n\t\tc<<1\n\tend\nend\nb.times{|_|c<<2*2**_}\na.times{c<0\n if a==n-1\n p -1\n exit\n else\n c<<1\n end\nend\nb.times{|_|c<<2*2**_}\na.times{c<0\n\tif a==n-1\n\t\tp -1\n\t\texit\n\telse\n\t\tc<<1\n\tend\nend\nb.times{|_|c<<2*2**_}\na.times{c<0\n\tif a==n-1\n\t\tp -1\n\t\texit\n\telse\n\t\tc<<1\n\tend\nend\nb.times{|_|c<<2*2**_}\na.times{c< currSum)\n\t seq[i] = 1\n\t currSum += 1\n\telse\n\t seq[i] = currMax + 1\n\t currSum += (currMax + 1)\n\t currMax += 1\n\t numA += 1\n\tend\n else\n seq[i] = 1\n\tcurrSum += 1\n end\n if(seq[i] > 50000)\n can = false\n\tbreak\n end\nend\n\nif(can && numA == a && numB == b)\n puts seq.join(\" \")\nelse\n puts -1\nend"}, {"source_code": "n,a,b=gets.split.map &:to_i\nc=[1]\nif b==0&&a>0\n\tif a==n-1\n\t\tp -1\n\t\texit\n\telse\n\t\tc<<1\n\tend\nend\nb.times{|_|c<<2*2**_}\na.times{c<0\n\tif a==n-1\n\t\tp -1\n\t\texit\n\telse\n\t\tc<<1\n\tend\nend\nb.times{|_|c<<2*2**_}\na.times{c<0\n\tif a==n-1\n\t\tp -1\n\t\texit\n\telse\n\t\tc<<1\n\tend\nend\nb.times{|_|c<<2*2**_}\na.times{c<0 then\n a.times{\n result[start+1]=result[start]+1\n start+=1\n }\nelse\n start=1\n result[1]=2\n a.times{\n result[start+1]=result[start]+1\n start+=1\n }\n result[1]=1\nend\nif result[start-1]>50000 then\n puts -1\n exit\nend\nresult.each{|i| p i}\n"}, {"source_code": "n,a,b=gets.split.map(&:to_i)\nresult=Array.new(n,1)\nif a+1==n && a>0 then\n puts -1\n exit\nend\nb.times{|i|\n sum=0\n (i+1).times{|j|\n sum+=result[j]\n }\n result[i+1]=sum+1\n}\nresult[0]=2 if b==0\nresult[1]=1 if b==0\nstart=b\nif b>0 then\n a.times{\n result[start+1]=result[start]+1\n start+=1\n }\nelse\n start=1\n result[1]=2\n a.times{\n result[start+1]=result[start]+1\n start+=1\n }\n result[1]=1\nend\nif result[start-1]>50000 then\n puts -1\n exit\nend\nresult.each{|i| p i}\n"}, {"source_code": "\nn,a,b=gets.split.map(&:to_i)\nresult=Array.new(n,1)\nb.times{|i|\n sum=0\n (i+1).times{|j|\n sum+=result[j]\n }\n result[i+1]=sum+1\n}\nresult[0]=2 if b==0\nstart=b\na.times{\n result[start+1]=result[start]+1\n start+=1\n}\nif result[start-1]>50000 then\n puts -1\n exit\nend\nresult.each{|i| p i}\n"}, {"source_code": "n,a,b=gets.split.map(&:to_i)\nresult=Array.new(n,1)\nb.times{|i|\n sum=0\n (i+1).times{|j|\n sum+=result[j]\n }\n result[i+1]=sum+1\n}\nstart=b\na.times{\n result[start+1]=result[start]+1\n start+=1\n}\nif result[start-1]>50000 then\n puts -1\n exit\nend\nresult.each{|i| p i}\n"}, {"source_code": "n, a, b = STDIN.readline.split.map {|s| s.to_i }\nif a > 0 and b == 0\n puts -1\n exit\nend\nresult = [1]\nb.times { result.push(2*result[-1]) }\na.times { result.push(result[-1]+1) }\nwhile result.length < n\n result.push(result[-1])\nend\nputs result.join ' '\n"}, {"source_code": "n, a, b = STDIN.readline.split.map {|s| s.to_i }\nif a > 0 and b == 0\n puts -1\n exit\nend\nresult = [1]\nb.times { result.push(2**result[-1]) }\na.times { result.push(result[-1]+1) }\nwhile result.length < n\n result.push(result[-1])\nend\nputs result.join ' '\n"}, {"source_code": "n, a, b = STDIN.readline.split.map {|s| s.to_i }\nresult = [1]\nb.times { result.push(2**result[-1]) }\na.times { result.push(result[-1]+1) }\nwhile result.length < n\n result.push(result[-1])\nend\nputs result.join ' '\n"}, {"source_code": "n, a, b = gets.split.map(&:to_i)\nif n - (b > 0 ? b+1 : b) - (b > 0 ? a : ( a > 0 ? a+2:1)) < 0\n puts(-1)\nelse\n bb = [1] + (1..b).map {|x| 2**x}.to_a\n last = bb[-1]\n a.times do |i|\n bb << bb[-1] + 1\n end\n (n-a-b-1).times do\n bb << bb[-1]\n end\n puts bb*' '\nend\n"}, {"source_code": "n, a, b = gets.split.map(&:to_i)\naa = [1] + (1..a+1).to_a\n(n-a-b-2).times do\n aa.unshift(1)\nend\nb.times do\n aa << (aa.inject(:+) + 1)\nend\nputs aa*' '"}, {"source_code": "n, a, b = gets.split.map(&:to_i)\naa = [1] + (1..a+1).to_a\n(n-a-b-2).times do\n aa.unshift(1)\nend\nb.times do\n aa << (aa.inject(:+) + 1)\nend\np aa"}, {"source_code": "n, a, b = gets.split.map(&:to_i)\nif a+b+2 > n\n puts(-1)\nelse\n bb = [1] + (1..b).map {|x| 2**x}.to_a\n last = bb[-1]\n a.times do |i|\n bb << bb[-1] + 1\n end\n (n-a-b-1).times do\n bb << bb[-1]\n end\n puts bb*' '\nend"}, {"source_code": "n, a, b = gets.split.map(&:to_i)\nif n - (b > 0 ? b+1 : b) - (b > 0 ? a : a+2) < 0\n puts(-1)\nelse\n bb = [1] + (1..b).map {|x| 2**x}.to_a\n last = bb[-1]\n a.times do |i|\n bb << bb[-1] + 1\n end\n (n-a-b-1).times do\n bb << bb[-1]\n end\n puts bb*' '\nend"}, {"source_code": "n, a, b = gets.split.map(&:to_i)\nif n - (b > 0 ? b+1 : b) - (b > 0 ? a : ( a > 0 ? a+2:1)) < 0\n puts(-1)\nelse\n bb = [1] + (1..b).map {|x| 2**x}.to_a\n bb << 1 if b == 0\n last = bb[-1]\n a.times do |i|\n bb << bb[-1] + 1\n end\n (n-bb.size).times do\n bb << bb[-1]\n end\n puts bb*' '\nend"}, {"source_code": "n, a, b = gets.split.map(&:to_i)\nbb = [1] + (1..b).map {|x| 2**x}.to_a\nlast = bb[-1]\na.times do |i|\n bb << bb[-1] + 1\nend\n(n-a-b-1).times do\n bb << bb[-1]\nend\nputs bb*' '"}, {"source_code": "line = gets.split\nn = line[0].to_i\na = line[1].to_i\nb = line[2].to_i\n\nseq = Array.new(n)\nseq[0] = 1\n\ncurrSum = 1\ncurrMax = 1\nnumA = 0\nnumB = 0\ncan = true\n\nfor i in 1..(n-1)\n if(numB < b)\n seq[i] = currSum + 1\n\tcurrSum = (2*currSum) + 1 \n\tcurrMax = seq[i]\n\tnumB += 1\n elsif (numA < a)\n seq[i] = currMax + 1\n\tcurrSum += (currMax + 1)\n\tcurrMax += 1\n\tnumA += 1\n else\n seq[i] = 1\n\tcurrSum += 1\n end\n if(seq[i] > 50000)\n can = false\n\tbreak\n end\nend\n\nif(can && numA == a && numB == b)\n puts seq.join(\" \")\nelse\n puts -1\nend"}, {"source_code": "line = gets.split\nn = line[0].to_i\na = line[1].to_i\nb = line[2].to_i\n\nseq = Array.new(n)\nseq[0] = 1\n\ncurrSum = 1\ncurrMax = 1\nnumA = 0\nnumB = 0\ncan = true\n\nfor i in 1..(n-1)\n if(numB < b)\n seq[i] = currSum + 1\n\tcurrSum = (2*currSum) + 1 \n\tcurrMax = seq[i]\n\tnumB += 1\n elsif (numA < a)\n if(currMax + 1 > currSum)\n\t can = false\n\tend\n seq[i] = currMax + 1\n\tcurrSum += (currMax + 1)\n\tcurrMax += 1\n\tnumA += 1\n else\n seq[i] = 1\n\tcurrSum += 1\n end\n if(seq[i] > 50000)\n can = false\n\tbreak\n end\nend\n\nif(can && numA == a && numB == b)\n puts seq.join(\" \")\nelse\n puts -1\nend"}, {"source_code": "line = gets.split\nn = line[0].to_i\na = line[1].to_i\nb = line[2].to_i\n\nseq = Array.new(n)\nseq[0] = 1\n\ncurrSum = 1\ncurrMax = 1\nnumA = 0\nnumB = 0\ncan = true\n\nfor i in 1..(n-1)\n if(numB < b)\n seq[i] = currSum + 1\n\tcurrSum = (2*currSum) + 1 \n\tcurrMax = seq[i]\n\tnumB += 1\n elsif (numA < a)\n seq[i] = currMax + 1\n\tcurrSum += (currMax + 1)\n\tcurrMax += 1\n\tnumA += 1\n else\n seq[i] = 1\n\tcurrSum += 1\n end\n if(seq[i] > 50000)\n can = false\n\tbreak\n end\nend\n\nif(can)\n puts seq.join(\" \")\nelse\n puts -1\nend"}], "src_uid": "573995cbae2a7b28ed92d71c48cd9039"} {"nl": {"description": "Haha, try to solve this, SelectorUnlimited!— antontrygubO_oYour friends Alice and Bob practice fortune telling.Fortune telling is performed as follows. There is a well-known array $$$a$$$ of $$$n$$$ non-negative integers indexed from $$$1$$$ to $$$n$$$. The tellee starts with some non-negative number $$$d$$$ and performs one of the two operations for each $$$i = 1, 2, \\ldots, n$$$, in the increasing order of $$$i$$$. The possible operations are: replace their current number $$$d$$$ with $$$d + a_i$$$ replace their current number $$$d$$$ with $$$d \\oplus a_i$$$ (hereinafter $$$\\oplus$$$ denotes the bitwise XOR operation)Notice that the chosen operation may be different for different $$$i$$$ and for different tellees.One time, Alice decided to start with $$$d = x$$$ and Bob started with $$$d = x + 3$$$. Each of them performed fortune telling and got a particular number in the end. Notice that the friends chose operations independently of each other, that is, they could apply different operations for the same $$$i$$$.You learnt that either Alice or Bob ended up with number $$$y$$$ in the end, but you don't know whose of the two it was. Given the numbers Alice and Bob started with and $$$y$$$, find out who (Alice or Bob) could get the number $$$y$$$ after performing the operations. It is guaranteed that on the jury tests, exactly one of your friends could have actually gotten that number.HacksYou cannot make hacks in this problem.", "input_spec": "On the first line of the input, you are given one number $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. The following $$$2 \\cdot t$$$ lines contain test cases. The first line of each test case contains three numbers $$$n$$$, $$$x$$$, $$$y$$$ ($$$1 \\le n \\le 10^5$$$, $$$0 \\le x \\le 10^9$$$, $$$0 \\le y \\le 10^{15}$$$) — the length of array $$$a$$$, Alice's initial number (Bob's initial number is therefore $$$x+3$$$), and the number that one of the two friends got in the end. The second line of each test case contains $$$n$$$ numbers — the array $$$a$$$ ($$$0 \\le a_i \\le 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.", "output_spec": "For each test case, print the name of the friend who could get the number $$$y$$$: \"Alice\" or \"Bob\".", "sample_inputs": ["4\n\n1 7 9\n\n2\n\n2 0 2\n\n1 3\n\n4 0 1\n\n1 2 3 4\n\n2 1000000000 3000000000\n\n1000000000 1000000000"], "sample_outputs": ["Alice\nAlice\nBob\nAlice"], "notes": "NoteIn the first test case, Alice could get $$$9$$$ using the following operations: $$$7 + 2 = 9$$$.In the second test case, Alice could get $$$2$$$ using this operations: $$$(0 + 1) \\oplus 3 = 2$$$.In the third test case, Bob started with $$$x+3 = 0+3=3$$$ and could get $$$1$$$ this way: $$$(((3 + 1) + 2) \\oplus 3) \\oplus 4 = 1$$$."}, "positive_code": [{"source_code": "case_count = gets.to_i\r\n\r\ncase_count.times do\r\n n, x, y = gets.chomp.split(\" \").map(&:to_i)\r\n a = gets.chomp.split(\" \").map(&:to_i)\r\n\r\n result = x & 1\r\n\r\n a.each { |el| result ^= el & 1 }\r\n\r\n puts y & 1 == result ? \"Alice\" : \"Bob\"\r\nend\r\n"}], "negative_code": [], "src_uid": "d17752513405fc68d838e9b3792c7bef"} {"nl": {"description": "You are given an array $$$a$$$ of length $$$n$$$. We define the equality of the array as the number of indices $$$1 \\le i \\le n - 1$$$ such that $$$a_i = a_{i + 1}$$$. We are allowed to do the following operation: Select two integers $$$i$$$ and $$$x$$$ such that $$$1 \\le i \\le n - 1$$$ and $$$1 \\le x \\le 10^9$$$. Then, set $$$a_i$$$ and $$$a_{i + 1}$$$ to be equal to $$$x$$$. Find the minimum number of operations needed such that the equality of the array is less than or equal to $$$1$$$.", "input_spec": "Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$2 \\le n \\le 2 \\cdot 10 ^ 5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10 ^ 5$$$", "output_spec": "For each test case, print the minimum number of operations needed.", "sample_inputs": ["4\n\n5\n\n1 1 1 1 1\n\n5\n\n2 1 1 1 2\n\n6\n\n1 1 2 3 3 4\n\n6\n\n1 2 1 4 5 4"], "sample_outputs": ["2\n1\n2\n0"], "notes": "NoteIn the first test case, we can select $$$i=2$$$ and $$$x=2$$$ to form $$$[1, 2, 2, 1, 1]$$$. Then, we can select $$$i=3$$$ and $$$x=3$$$ to form $$$[1, 2, 3, 3, 1]$$$.In the second test case, we can select $$$i=3$$$ and $$$x=100$$$ to form $$$[2, 1, 100, 100, 2]$$$."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n a = gets.chomp.split.map(&:to_i)\r\n found = false\r\n i = 1\r\n first = -1\r\n last = -1\r\n while i < n do\r\n if a[i] == a[i-1] then\r\n first = i-1 if !found\r\n last = i\r\n found = true\r\n end\r\n i += 1\r\n end\r\n if last <0 then\r\n puts 0\r\n else\r\n if last - first < 2 then\r\n puts 0\r\n else\r\n puts (last - first == 2) ? 1 : (last - first - 2)\r\n end\r\n end\r\nend"}], "negative_code": [{"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n a = gets.chomp.split.map(&:to_i)\r\n found = false\r\n i = 1\r\n ans = 0\r\n l = 0\r\n while i < n do\r\n c = 1\r\n while a[i] == a[i-1] do\r\n c += 1\r\n i += 1\r\n end\r\n if c>1 then\r\n if found then\r\n ans += i - l - 1\r\n else\r\n ans += (c<3)? 0 : ((c>3) ? (c-3) : 1)\r\n end\r\n if !found\r\n found = true\r\n l = i\r\n end\r\n end\r\n i += 1\r\n end\r\n puts ans\r\nend"}, {"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n a = gets.chomp.split.map(&:to_i)\r\n found = false\r\n i = 1\r\n ans = 0\r\n l = 0\r\n while i < n do\r\n c = 1\r\n while a[i] == a[i-1] do\r\n c += 1\r\n i += 1\r\n end\r\n if c>1 then\r\n if found then\r\n ans += i - l - 1\r\n else\r\n ans += (c<3)? 0 : ((c>3) ? (c-3) : 1)\r\n end\r\n found = true\r\n l = i\r\n end\r\n i += 1\r\n end\r\n puts ans\r\nend"}], "src_uid": "a91be662101762bcb2c58b8db9ff61e0"} {"nl": {"description": "Memory is performing a walk on the two-dimensional plane, starting at the origin. He is given a string s with his directions for motion: An 'L' indicates he should move one unit left. An 'R' indicates he should move one unit right. A 'U' indicates he should move one unit up. A 'D' indicates he should move one unit down.But now Memory wants to end at the origin. To do this, he has a special trident. This trident can replace any character in s with any of 'L', 'R', 'U', or 'D'. However, because he doesn't want to wear out the trident, he wants to make the minimum number of edits possible. Please tell Memory what is the minimum number of changes he needs to make to produce a string that, when walked, will end at the origin, or if there is no such string.", "input_spec": "The first and only line contains the string s (1 ≤ |s| ≤ 100 000) — the instructions Memory is given.", "output_spec": "If there is a string satisfying the conditions, output a single integer — the minimum number of edits required. In case it's not possible to change the sequence in such a way that it will bring Memory to to the origin, output -1.", "sample_inputs": ["RRU", "UDUR", "RUUR"], "sample_outputs": ["-1", "1", "2"], "notes": "NoteIn the first sample test, Memory is told to walk right, then right, then up. It is easy to see that it is impossible to edit these instructions to form a valid walk.In the second sample test, Memory is told to walk up, then down, then up, then right. One possible solution is to change s to \"LDUR\". This string uses 1 edit, which is the minimum possible. It also ends at the origin."}, "positive_code": [{"source_code": "s=gets.chomp\np s.size%2==1 ? -1 : (s.count(?L)-s.count(?R)).abs+(s.count(?U)-s.count(?D)).abs>>1"}, {"source_code": "s = gets\nx, y = 0, 0\ns.each_char do |c|\n\tif c == \"U\"\n\t\ty += 1\n\telsif c == \"D\"\n\t\ty -= 1\n\telsif c == \"L\"\n\t\tx -= 1\n\telsif c == \"R\"\n\t\tx += 1\n\tend\nend\nx, y = x.abs, y.abs\nputs ((x + y) % 2 == 0) ? (x + y) / 2 : -1\n\n"}, {"source_code": "q = gets.chomp.split(//)\nx,y = 0,0\n\nif q.size.even?\n q.each do |e|\n x += 1 if e == 'R'\n x -= 1 if e == 'L'\n y += 1 if e == 'U'\n y -= 1 if e == 'D'\n end\n puts (x.abs + y.abs) / 2\nelse\n puts -1\nend\n\n\n\n"}, {"source_code": "line = gets.strip.split('')\napsis = 0\nordinat = 0\nline.each do |x|\n if x == 'L'\n apsis -= 1\n elsif x == 'R'\n apsis += 1\n elsif x == 'U'\n ordinat += 1 \n else\n ordinat -= 1\n end\nend\nif (apsis + ordinat).even?\n puts (apsis.abs + ordinat.abs) / 2\nelse\n puts -1\nend"}, {"source_code": "a=b=0\ns=gets.chomp\ns.chars {|c|\n a+=1 if c==?L\n a-=1 if c==?R\n b+=1 if c==?U\n b-=1 if c==?D\n}\np s.size.odd? ? -1 : (a.abs+b.abs)/2\n"}, {"source_code": "s = gets.split('')\nlr = 0\nud = 0\ns.each do |c|\n lr -= 1 if c=='L'\n lr += 1 if c=='R'\n ud -= 1 if c=='D'\n ud += 1 if c=='U'\nend\n\nx = (lr.abs-ud.abs).abs\nputs x%2!=0 ? -1 : x/2+[lr.abs,ud.abs].min\n"}, {"source_code": "s = gets.chomp.chars\na = [0, 0]\ns.each do |x|\n case x\n when 'L'\n a[0] -= 1\n when 'R'\n a[0] += 1\n when 'U'\n a[1] += 1\n when 'D'\n a[1] -= 1\n end\nend\nc = a.map(&:abs).inject(&:+)\nputs s.size.even? && c.even? ? c / 2 : -1\n"}, {"source_code": "route = gets.chomp\nif route.length % 2 == 1\n puts -1\nelse\n no_u = 0\n no_r = 0\n route.split(\"\").each do |c|\n case c\n when \"U\"\n no_u = no_u + 1\n when \"D\"\n no_u = no_u - 1\n when \"R\"\n no_r = no_r + 1\n when \"L\"\n no_r = no_r - 1\n end\n end\n puts (no_u.abs + no_r.abs)/2\nend\n"}, {"source_code": "s=readline.strip\n\nif s.size%2 == 1\n puts -1\nelse\n lr = 0\n ud = 0\n (0...s.size).each do |i|\n if s[i] == 'L'\n lr += 1\n elsif s[i] == 'R'\n lr -= 1\n elsif s[i] == 'U'\n ud += 1\n elsif s[i] == 'D'\n ud -= 1\n end\n end\n sum = lr.abs + ud.abs\n puts sum/2\nend\n"}, {"source_code": "str = gets.chomp.split('')\n\nx = 0\ny = 0\n\nstr.each do |s|\n case s\n when 'L' then x -= 1\n when 'R' then x += 1\n when 'U' then y += 1\n when 'D' then y -= 1\n end\nend\n\nabsX = x.abs\nabsY = y.abs\n\nif absX == absY\n puts absX\nelse\n rest = [absX, absY].max - [absX, absY].min\n \n if rest % 2 == 0\n puts (rest / 2) + [absX, absY].min\n else\n puts -1\n end\nend"}], "negative_code": [{"source_code": "s=gets.chomp\np s.size%2==1 ? -1 : (s.count(?L)-s.count(?R)).abs+(s.count(?U)-s.count(?D)).abs/2"}, {"source_code": "s=gets.chomp\np s.size%2==1 ? -1 : (s.count(?L)-s.count(?R)).abs+(s.count(?U)-s.count(?D)).abs"}, {"source_code": "line = gets.strip.split('')\napsis = 0\nordinat = 0\nline.each do |x|\n if x == 'L'\n apsis -= 1\n elsif x == 'R'\n apsis += 1\n elsif x == 'U'\n ordinat += 1 \n else\n ordinat -= 1\n end\nend\nif ((apsis.even? and ordinat.even?) or (not apsis.even? and not ordinat.even?))\n puts (apsis + ordinat) / 2\nelse\n puts -1\nend"}, {"source_code": "route = gets.chomp\nif route.length % 2 == 1\n puts -1\nelse\n no_u = 0\n no_r = 0\n route.split(\"\").each do |c|\n case c\n when \"U\"\n no_u = no_u + 1\n when \"D\"\n no_u = no_u - 1\n when \"R\"\n no_r = no_r + 1\n when \"L\"\n no_r = no_r - 1\n end\n end\n puts (no_u + no_r)/2\nend\n"}, {"source_code": "route = gets.chomp\nif route.length % 2 == 1\n puts -1\nelse\n no_u = 0\n no_r = 0\n route.split(\"\").each do |c|\n case c\n when \"U\"\n no_u = no_u + 1\n when \"D\"\n no_u = no_u - 1\n when \"R\"\n no_r = no_r + 1\n when \"L\"\n no_r = no_r - 1\n end\n end\n puts (no_u + no_r).abs/2\nend\n"}, {"source_code": "str = gets.chomp.split('')\n\nx = 0\ny = 0\n\nstr.each do |s|\n case s\n when 'L' then x -= 1\n when 'R' then x += 1\n when 'U' then y += 1\n when 'D' then y -= 1\n end\nend\n\nabsX = x.abs\nabsY = y.abs\n\nif absX == absY\n puts absX\nelse\n puts -1\nend"}], "src_uid": "8237ac3f3c2e79f5f70be1595630207e"} {"nl": {"description": "Little beaver is a beginner programmer, so informatics is his favorite subject. Soon his informatics teacher is going to have a birthday and the beaver has decided to prepare a present for her. He planted n flowers in a row on his windowsill and started waiting for them to grow. However, after some time the beaver noticed that the flowers stopped growing. The beaver thinks it is bad manners to present little flowers. So he decided to come up with some solutions. There are m days left to the birthday. The height of the i-th flower (assume that the flowers in the row are numbered from 1 to n from left to right) is equal to ai at the moment. At each of the remaining m days the beaver can take a special watering and water w contiguous flowers (he can do that only once at a day). At that each watered flower grows by one height unit on that day. The beaver wants the height of the smallest flower be as large as possible in the end. What maximum height of the smallest flower can he get?", "input_spec": "The first line contains space-separated integers n, m and w (1 ≤ w ≤ n ≤ 105; 1 ≤ m ≤ 105). The second line contains space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109).", "output_spec": "Print a single integer — the maximum final height of the smallest flower.", "sample_inputs": ["6 2 3\n2 2 2 2 1 1", "2 5 1\n5 8"], "sample_outputs": ["2", "9"], "notes": "NoteIn the first sample beaver can water the last 3 flowers at the first day. On the next day he may not to water flowers at all. In the end he will get the following heights: [2, 2, 2, 3, 2, 2]. The smallest flower has height equal to 2. It's impossible to get height 3 in this test."}, "positive_code": [{"source_code": "#!/usr/bin/ruby\n\nn, m, w = STDIN.readline.split.map {|s| s.to_i }\nheights = STDIN.readline.split.map {|s| s.to_i }\n\ndef feasible(n, m, w, bumps)\n\tpressure = 0\n\tloss = (n+w).times.collect { 0 }\n\tn.times do |i|\n\t\tpressure -= loss[i]\n\t\tbumps[i] -= pressure\n\t\tif bumps[i] > 0\n\t\t\tpressure += bumps[i]\n\t\t\tloss[i+w] += bumps[i]\n\t\t\tm -= bumps[i]\n\t\t\treturn false if m < 0\n\t\tend\n\tend\n\treturn true\nend\n\nlow = heights.min\nhigh = heights.max + m\nwhile low != high\n\tmid = (low+high+1)/2\n\tdeficits = heights.map {|height| mid - height }\n\tif feasible(n, m, w, deficits)\n\t\tlow = mid\n\telse\n\t\thigh = mid-1\n\tend\nend\nputs low\n"}, {"source_code": "to_i_proc = Proc.new { |n| n.to_i }\nn, m, w = *gets.chomp.split.map!(&to_i_proc)\na = gets.chomp.split.map!(&to_i_proc)\n# n, m, w = *[1, 100000, 1]\n# a = [1000000000]\n\nclass Present\n def initialize(n, m, w, a)\n @n = n\n @m = m\n @w = w\n @a = a\n @record = Array.new(@n)\n solve\n puts @ans\n end\n\nprivate\n def solve\n upperbound = @a.max + @m\n lowerbound = @a.min\n while lowerbound <= upperbound do\n @mid = (lowerbound + upperbound) / 2\n if test\n @ans = @mid\n lowerbound = @mid + 1\n else\n upperbound = @mid - 1\n end\n end\n end\n\n def test\n sum = 0\n remain = @m\n for i in 0...@n do\n # 维护record和sum\n @record[i] = 0\n sum -= @record[i - @w] if i - @w >= 0\n\n return false if sum + remain + @a[i] < @mid\n if @a[i] + sum < @mid\n @record[i] = @mid - sum - @a[i]\n sum += @record[i]\n remain -= @record[i]\n end\n end\n return true\n end\nend\n\nPresent.new(n, m, w, a)"}, {"source_code": "to_i_proc = Proc.new { |n| n.to_i }\nn, m, w = *gets.chomp.split.map!(&to_i_proc)\na = gets.chomp.split.map!(&to_i_proc)\n# n, m, w = *[1, 100000, 1]\n# a = [1000000000]\n\nclass Present\n def initialize(n, m, w, a)\n @n = n\n @m = m\n @w = w\n @a = a\n @record = Array.new(@n)\n solve\n puts @ans\n end\n\nprivate\n def solve\n upperbound = @a.max + @m\n lowerbound = @a.min\n while lowerbound <= upperbound do\n @mid = (lowerbound + upperbound) / 2\n if test\n @ans = @mid\n lowerbound = @mid + 1\n else\n upperbound = @mid - 1\n end\n end\n end\n\n def solvex(mid)\n @mid = mid\n puts test\n end\n\n def test\n sum = 0\n remain = @m\n for i in 0...@n do\n # 维护record和sum\n @record[i] = 0\n sum -= @record[i - @w] if i - @w >= 0\n\n return false if sum + remain + @a[i] < @mid\n if @a[i] + sum < @mid\n @record[i] = @mid - sum - @a[i]\n sum += @record[i]\n remain -= @record[i]\n end\n end\n return true\n end\nend\n\nPresent.new(n, m, w, a)\n"}], "negative_code": [{"source_code": "to_i_proc = Proc.new { |n| n.to_i }\nn, m, w = *gets.chomp.split.map!(&to_i_proc)\na = gets.chomp.split.map!(&to_i_proc)\n# n, m, w = *[6, 2, 3]\n# a = [2, 2, 2, 2, 1, 1]\n# n, m, w = *[2, 5, 1]\n# a = [5, 8]\n\nclass Present\n def initialize(n, m, w, a)\n @n = n\n @m = m\n @w = w\n @a = a\n @record = Array.new(@n)\n solve\n @ans = 766 if @ans = 352\n puts @ans\n end\n\nprivate\n def solve\n upperbound = @a.max + @w\n lowerbound = @a.min\n while lowerbound <= upperbound do\n @mid = (lowerbound + upperbound) / 2\n if test\n @ans = @mid\n lowerbound = @mid + 1\n else\n upperbound = @mid - 1\n end\n end\n end\n\n def test\n sum = 0\n remain = @m\n for i in 0...@n do\n # 维护record和sum\n @record[i] = 0\n sum -= @record[i - @w] if i - @w >= 0\n\n return false if sum + remain + @a[i] < @mid\n if @a[i] + sum < @mid\n @record[i] = @mid - sum - @a[i]\n sum += @record[i]\n remain -= @record[i]\n end\n end\n return true\n end\nend\n\nPresent.new(n, m, w, a)\n"}, {"source_code": "to_i_proc = Proc.new { |n| n.to_i }\nn, m, w = *gets.chomp.split.map!(&to_i_proc)\na = gets.chomp.split.map!(&to_i_proc)\n# n, m, w = *[6, 2, 3]\n# a = [2, 2, 2, 2, 1, 1]\n# n, m, w = *[2, 5, 1]\n# a = [5, 8]\n\nclass Present\n def initialize(n, m, w, a)\n @n = n\n @m = m\n @w = w\n @a = a\n @record = Array.new(@n)\n solve\n @ans = 766 if @ans == 352\n puts @ans\n end\n\nprivate\n def solve\n upperbound = @a.max + @w\n lowerbound = @a.min\n while lowerbound <= upperbound do\n @mid = (lowerbound + upperbound) / 2\n if test\n @ans = @mid\n lowerbound = @mid + 1\n else\n upperbound = @mid - 1\n end\n end\n end\n\n def test\n sum = 0\n remain = @m\n for i in 0...@n do\n # 维护record和sum\n @record[i] = 0\n sum -= @record[i - @w] if i - @w >= 0\n\n return false if sum + remain + @a[i] < @mid\n if @a[i] + sum < @mid\n @record[i] = @mid - sum - @a[i]\n sum += @record[i]\n remain -= @record[i]\n end\n end\n return true\n end\nend\n\nPresent.new(n, m, w, a)\n"}, {"source_code": "to_i_proc = Proc.new { |n| n.to_i }\nn, m, w = *gets.chomp.split.map!(&to_i_proc)\na = gets.chomp.split.map!(&to_i_proc)\n# n, m, w = *[6, 2, 3]\n# a = [2, 2, 2, 2, 1, 1]\n# n, m, w = *[2, 5, 1]\n# a = [5, 8]\n\nclass Present\n def initialize(n, m, w, a)\n @n = n\n @m = m\n @w = w\n @a = a\n @record = Array.new(@n)\n solve\n puts @ans\n end\n\nprivate\n def solve\n upperbound = @a.max + @w\n lowerbound = @a.min\n while lowerbound <= upperbound do\n @mid = (lowerbound + upperbound) / 2\n if test\n @ans = @mid\n lowerbound = @mid + 1\n else\n upperbound = @mid - 1\n end\n end\n end\n \n def test\n sum = 0\n remain = @m\n for i in 0...@n do\n # 维护record和sum\n @record[i] = 0\n sum -= @record[i - @w] if i - @w >= 0\n\n return false if sum + remain + @a[i] < @mid\n if @a[i] + sum < @mid\n @record[i] = @mid - sum - @a[i]\n sum += @record[i]\n remain -= @record[i]\n end\n end\n return true\n end\nend\n\nPresent.new(n, m, w, a)\n"}], "src_uid": "48c8dc603511fd3f0e6ad00dfc801a7e"} {"nl": {"description": "Kuroni has $$$n$$$ daughters. As gifts for them, he bought $$$n$$$ necklaces and $$$n$$$ bracelets: the $$$i$$$-th necklace has a brightness $$$a_i$$$, where all the $$$a_i$$$ are pairwise distinct (i.e. all $$$a_i$$$ are different), the $$$i$$$-th bracelet has a brightness $$$b_i$$$, where all the $$$b_i$$$ are pairwise distinct (i.e. all $$$b_i$$$ are different). Kuroni wants to give exactly one necklace and exactly one bracelet to each of his daughters. To make sure that all of them look unique, the total brightnesses of the gifts given to each daughter should be pairwise distinct. Formally, if the $$$i$$$-th daughter receives a necklace with brightness $$$x_i$$$ and a bracelet with brightness $$$y_i$$$, then the sums $$$x_i + y_i$$$ should be pairwise distinct. Help Kuroni to distribute the gifts.For example, if the brightnesses are $$$a = [1, 7, 5]$$$ and $$$b = [6, 1, 2]$$$, then we may distribute the gifts as follows: Give the third necklace and the first bracelet to the first daughter, for a total brightness of $$$a_3 + b_1 = 11$$$. Give the first necklace and the third bracelet to the second daughter, for a total brightness of $$$a_1 + b_3 = 3$$$. Give the second necklace and the second bracelet to the third daughter, for a total brightness of $$$a_2 + b_2 = 8$$$. Here is an example of an invalid distribution: Give the first necklace and the first bracelet to the first daughter, for a total brightness of $$$a_1 + b_1 = 7$$$. Give the second necklace and the second bracelet to the second daughter, for a total brightness of $$$a_2 + b_2 = 8$$$. Give the third necklace and the third bracelet to the third daughter, for a total brightness of $$$a_3 + b_3 = 7$$$. This distribution is invalid, as the total brightnesses of the gifts received by the first and the third daughter are the same. Don't make them this upset!", "input_spec": "The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 100$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 100$$$)  — the number of daughters, necklaces and bracelets. The second line of each test case contains $$$n$$$ distinct integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 1000$$$)  — the brightnesses of the necklaces. The third line of each test case contains $$$n$$$ distinct integers $$$b_1, b_2, \\dots, b_n$$$ ($$$1 \\le b_i \\le 1000$$$)  — the brightnesses of the bracelets.", "output_spec": "For each test case, print a line containing $$$n$$$ integers $$$x_1, x_2, \\dots, x_n$$$, representing that the $$$i$$$-th daughter receives a necklace with brightness $$$x_i$$$. In the next line print $$$n$$$ integers $$$y_1, y_2, \\dots, y_n$$$, representing that the $$$i$$$-th daughter receives a bracelet with brightness $$$y_i$$$. The sums $$$x_1 + y_1, x_2 + y_2, \\dots, x_n + y_n$$$ should all be distinct. The numbers $$$x_1, \\dots, x_n$$$ should be equal to the numbers $$$a_1, \\dots, a_n$$$ in some order, and the numbers $$$y_1, \\dots, y_n$$$ should be equal to the numbers $$$b_1, \\dots, b_n$$$ in some order. It can be shown that an answer always exists. If there are multiple possible answers, you may print any of them.", "sample_inputs": ["2\n3\n1 8 5\n8 4 5\n3\n1 7 5\n6 1 2"], "sample_outputs": ["1 8 5\n8 4 5\n5 1 7\n6 2 1"], "notes": "NoteIn the first test case, it is enough to give the $$$i$$$-th necklace and the $$$i$$$-th bracelet to the $$$i$$$-th daughter. The corresponding sums are $$$1 + 8 = 9$$$, $$$8 + 4 = 12$$$, and $$$5 + 5 = 10$$$.The second test case is described in the statement."}, "positive_code": [{"source_code": "T = gets.to_i\n\nT.times do\n n = gets.to_i\n a = gets.split.map(&:to_i).sort\n b = gets.split.map(&:to_i).sort\n\n puts a.join(' ')\n puts b.join(' ')\nend\n"}, {"source_code": "def main\n t = gets.chomp.to_i\n t.times { solve }\nend\n\ndef solve\n n = gets.chomp.to_i # not used\n a = gets.chomp.split.map(&:to_i)\n b = gets.chomp.split.map(&:to_i)\n # S = gets.chomp.chars\n a.sort!\n b.sort!\n print a.join(\" \") + \"\\n\"\n print b.join(\" \") + \"\\n\"\nend\n\nmain\n"}, {"source_code": "t = gets.to_i\nwhile t > 0\n t -= 1\n n = gets.to_i\n a = gets.split.map(&:to_i).sort!\n b = gets.split.map(&:to_i).sort!\n puts a.join(' ')\n puts b.join(' ')\nend\n"}], "negative_code": [], "src_uid": "5c6af8ced2c4b8999abccfac5c4d0057"} {"nl": {"description": "You are given a sequence $$$s$$$ consisting of $$$n$$$ digits from $$$1$$$ to $$$9$$$.You have to divide it into at least two segments (segment — is a consecutive sequence of elements) (in other words, you have to place separators between some digits of the sequence) in such a way that each element belongs to exactly one segment and if the resulting division will be represented as an integer numbers sequence then each next element of this sequence will be strictly greater than the previous one.More formally: if the resulting division of the sequence is $$$t_1, t_2, \\dots, t_k$$$, where $$$k$$$ is the number of element in a division, then for each $$$i$$$ from $$$1$$$ to $$$k-1$$$ the condition $$$t_{i} < t_{i + 1}$$$ (using numerical comparing, it means that the integer representations of strings are compared) should be satisfied.For example, if $$$s=654$$$ then you can divide it into parts $$$[6, 54]$$$ and it will be suitable division. But if you will divide it into parts $$$[65, 4]$$$ then it will be bad division because $$$65 > 4$$$. If $$$s=123$$$ then you can divide it into parts $$$[1, 23]$$$, $$$[1, 2, 3]$$$ but not into parts $$$[12, 3]$$$.Your task is to find any suitable division for each of the $$$q$$$ independent queries.", "input_spec": "The first line of the input contains one integer $$$q$$$ ($$$1 \\le q \\le 300$$$) — the number of queries. The first line of the $$$i$$$-th query contains one integer number $$$n_i$$$ ($$$2 \\le n_i \\le 300$$$) — the number of digits in the $$$i$$$-th query. The second line of the $$$i$$$-th query contains one string $$$s_i$$$ of length $$$n_i$$$ consisting only of digits from $$$1$$$ to $$$9$$$.", "output_spec": "If the sequence of digits in the $$$i$$$-th query cannot be divided into at least two parts in a way described in the problem statement, print the single line \"NO\" for this query. Otherwise in the first line of the answer to this query print \"YES\", on the second line print $$$k_i$$$ — the number of parts in your division of the $$$i$$$-th query sequence and in the third line print $$$k_i$$$ strings $$$t_{i, 1}, t_{i, 2}, \\dots, t_{i, k_i}$$$ — your division. Parts should be printed in order of the initial string digits. It means that if you write the parts one after another without changing their order then you'll get the string $$$s_i$$$. See examples for better understanding.", "sample_inputs": ["4\n6\n654321\n4\n1337\n2\n33\n4\n2122"], "sample_outputs": ["YES\n3\n6 54 321\nYES\n3\n1 3 37\nNO\nYES\n2\n21 22"], "notes": null}, "positive_code": [{"source_code": "q = gets.to_i\n\nq.times do\n\n\tn = gets.to_i\n\n\ts = gets.chomp\n\n\tif n == 2 && s[0].to_i >= s[1].to_i\n\t\tputs \"NO\"\n\telse\n\t\tputs \"YES\"\n\t\tputs 2\n\t\tputs \"#{s[0]} #{s[1..s.length-1]}\"\n\tend\t\n\nend"}, {"source_code": "n=gets.to_i\nn.times{gets;s=gets.chomp;puts s[0].to_i>=s[1..-1].to_i ? :NO : \"YES\\n2\\n%s %s\"%[s[0],s[1..-1]]}"}, {"source_code": "def solve(s)\n if s.size == 1\n puts \"YES\"\n puts \"1\"\n puts s\n end\n if s[0].to_i < s.slice(1, s.size).to_i\n puts \"YES\"\n puts 2\n puts \"#{s[0]} #{s.slice(1, s.size)}\"\n else\n puts \"NO\"\n end\nend\n\ngets.strip.to_i.times do\n _ = gets.strip\n solve(gets.strip)\nend"}, {"source_code": "n=gets.to_i\nn.times{gets;s=gets.chomp;puts s[0].to_i>=s[1..-1].to_i ? :NO : \"YES\\n2\\n%s %s\"%[s[0],s[1..-1]]}"}, {"source_code": "N = gets.chomp.to_i\n\nN.times do\n L = gets.chomp.to_i\n seq = gets.chomp\n if L == 2 and seq[1].to_i <= seq[0].to_i\n puts \"NO\"\n next\n end\n (0..L-2).each do |l|\n sub_a = seq[0..l]\n sub_b = seq[l+1..-1]\n if sub_b.to_i > sub_a.to_i\n puts \"YES\"\n puts 2\n puts \"#{sub_a} #{sub_b}\"\n break\n end\n end\nend"}, {"source_code": "gets.to_i.times do\n l = gets.to_i\n s = gets.chomp\n if l == 2 && s[0] >= s[1]\n puts \"NO\"\n next\n end\n puts \"YES\",2,\"#{s[0]} #{s[1..-1]}\"\nend"}, {"source_code": "if File.exists?(\"input\")\n $stdin = File.open(\"input\")\n $stdout = File.open(\"output\",\"w\")\nend\n\nt = gets.to_i\nt.times do\n\tn = gets.to_i\n\tnum = gets.to_s\n\tif n>2 or num[1]>num[0]\n\t\tputs \"YES\"\n\t\tputs 2\n\t\tputs num[0]+\" \"+num[1..-1]\n\telse\n\t\tputs \"NO\"\n\tend\nend"}, {"source_code": "t=gets.to_i\nfor _ in 1..t\n n=gets.to_i\n s=gets.chomp\n if n==2&&s[0]>=s[1]\n puts 'NO'\n else\n puts 'YES'\n puts 2\n puts [s[0], s[1..-1]]*' '\n end\nend\n"}], "negative_code": [{"source_code": "q = gets.to_i\n\nq.times do\n\n\tn = gets.to_i\n\n\ts = gets.chomp\n\n\tif n == 2 && s[0].to_i >= s[1].to_i\n\t\tputs \"NO\"\n\telse\n\t\tputs \"YES\"\n\t\tputs \"#{s[0]} #{s[1..s.length-1]}\"\n\tend\t\n\nend"}, {"source_code": "N = gets.chomp.to_i\n\nN.times do\n L = gets.chomp.to_i\n seq = gets.chomp\n for l in (0..L-2) do\n sub_a = seq[0..l]\n sub_b = seq[l+1..-1]\n if sub_b.to_i > sub_a.to_i\n p \"YES\"\n p 2\n p \"#{sub_a} #{sub_b}\"\n break\n end\n end\nend"}, {"source_code": "N = gets.chomp.to_i\n\nN.times do\n L = gets.chomp.to_i\n seq = gets.chomp\n for l in (0..L-2) do\n sub_a = seq[0..l]\n sub_b = seq[l+1..-1]\n if sub_b.to_i > sub_a.to_i\n puts \"YES\"\n puts 2\n puts \"#{sub_a} #{sub_b}\"\n break\n end\n end\nend"}], "src_uid": "9f87a89c788bd7c7b66e51db9fe47e46"} {"nl": {"description": "You are given a string $$$s$$$, consisting only of characters '0' or '1'. Let $$$|s|$$$ be the length of $$$s$$$.You are asked to choose some integer $$$k$$$ ($$$k > 0$$$) and find a sequence $$$a$$$ of length $$$k$$$ such that: $$$1 \\le a_1 < a_2 < \\dots < a_k \\le |s|$$$; $$$a_{i-1} + 1 < a_i$$$ for all $$$i$$$ from $$$2$$$ to $$$k$$$. The characters at positions $$$a_1, a_2, \\dots, a_k$$$ are removed, the remaining characters are concatenated without changing the order. So, in other words, the positions in the sequence $$$a$$$ should not be adjacent.Let the resulting string be $$$s'$$$. $$$s'$$$ is called sorted if for all $$$i$$$ from $$$2$$$ to $$$|s'|$$$ $$$s'_{i-1} \\le s'_i$$$.Does there exist such a sequence $$$a$$$ that the resulting string $$$s'$$$ is sorted?", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of testcases. Then the descriptions of $$$t$$$ testcases follow. The only line of each testcase contains a string $$$s$$$ ($$$2 \\le |s| \\le 100$$$). Each character is either '0' or '1'.", "output_spec": "For each testcase print \"YES\" if there exists a sequence $$$a$$$ such that removing the characters at positions $$$a_1, a_2, \\dots, a_k$$$ and concatenating the parts without changing the order produces a sorted string. Otherwise, print \"NO\". You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).", "sample_inputs": ["5\n10101011011\n0000\n11111\n110\n1100"], "sample_outputs": ["YES\nYES\nYES\nYES\nNO"], "notes": "NoteIn the first testcase you can choose a sequence $$$a=[1,3,6,9]$$$. Removing the underlined letters from \"10101011011\" will produce a string \"0011111\", which is sorted.In the second and the third testcases the sequences are already sorted.In the fourth testcase you can choose a sequence $$$a=[3]$$$. $$$s'=$$$ \"11\", which is sorted.In the fifth testcase there is no way to choose a sequence $$$a$$$ such that $$$s'$$$ is sorted."}, "positive_code": [{"source_code": "def solve(s)\r\n n = s.size\r\n n0, n1, r0, r1 = true, false, false, false\r\n s.each do |c|\r\n if c\r\n n0, n1, r0, r1 = false, n0 | n1 | r0 | r1, n0, n1\r\n else\r\n n0, n1, r0, r1 = n0 | r0, false, n0, n1\r\n end\r\n end\r\n n0 | n1 | r0 | r1\r\nend\r\n\r\ndef main\r\n t = int\r\n t.times do\r\n s = chars.map { |c| c == ?1 }\r\n yesno solve(s)\r\n end\r\nend\r\n\r\nDEBUG = true\r\nMOD = 10**9+7\r\nYESNO = %w(NO YES)\r\nINF = 10**9\r\n\r\ndef int; gets.to_s.to_i end\r\ndef ints; gets.to_s.split.map { |s| s.to_i } end\r\ndef int1s; gets.to_s.split.map { |s| s.to_i - 1 } end\r\ndef float; gets.to_s.to_f end\r\ndef floats; gets.to_s.split.map { |s| s.to_f } end\r\ndef array_of(&convert); gets.to_s.split.map(&convert) end\r\ndef string; gets.to_s.chomp end\r\ndef chars; gets.to_s.chomp.chars; end\r\ndef rep(n, &b); Array.new(n, &b) end\r\ndef yes; puts YESNO[1] end\r\ndef no; puts YESNO[0] end\r\ndef yesno t; puts YESNO[t] end\r\ndef zip(xs, *yss); Enumerator.new { |y| xs.zip(*yss) { |a| y.yield(*a) } } end\r\ndef max(*xs, &block); block_given? ? xs.max_by(&block) : xs.max end\r\ndef min(*xs, &block); block_given? ? xs.min_by(&block) : xs.min end\r\ndef minmax(*xs, &block); block_given? ? xs.minmax_by(&block) : xs.minmax end\r\ndef gcd(*xs); xs.inject(0, :gcd) end\r\ndef matrix(h, w, fill=nil, &block); return Array.new(h) { Array.new(w, &block) } if block_given?; Array.new(h) { [fill] * w } end\r\ndef debug(x = nil); STDERR.puts (block_given? ? yield(x) : x).inspect if DEBUG; x end\r\ndef debug_grid(grid, width = 1); grid.each { |row| STDERR.puts row.map { |x| x.inspect.ljust(width) }.join(\"\") } if DEBUG; grid end\r\ndef if_debug; yield if DEBUG end\r\n\r\nmodule Boolean\r\n def coerce(other); [other, to_i] end\r\n def +@; to_i end\r\n def to_int; to_i end\r\n def *(other); to_i * other end\r\nend\r\n\r\nclass TrueClass\r\n include Boolean\r\n def to_i; 1 end\r\nend\r\n\r\nclass FalseClass\r\n include Boolean\r\n def to_i; 0 end\r\nend\r\n\r\nclass Integer\r\n def div_ceil(y); (self + y - 1) / y end\r\n def mod_inv(mod = MOD); pow(mod-2, mod) end\r\n def mod_div(y, mod = MOD); self * mod_inv(y, mod) % mod end\r\n def factorial(mod = MOD); (2..self).inject(1) { |f, x| f * x % mod } end\r\n def popcount; x = self; c = 0; while x > 0; c += 1 if x & 1 == 1; x >>= 1 end; c end #TODO: faster\r\n def bitbrute(&block); (1< 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\r\n def lower_bound; ac, wa = self.end_open, self.begin; while ac - wa > 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\r\n def shakutori(r2, &pred); Enumerator.new { |y| j, r = r2.begin, r2.end_open; each { |i| j += 1 while j + 1 < r and pred[i, j+1]; y.yield(i, j) } }; end\r\n def widest(&block); Enumerator.new { |y| j, n = self.begin, self.end_open; each { |i| j += 1 while j < n and block[i, j]; y.yield(i, j) if block[i, j] } } end\r\nend\r\n\r\nclass Array\r\n def power(&block); (0 ... 1 << size).each(&block) end\r\n def sorted_merge(other); a = []; i = j = 0; n, m = size, other.size; if j < m and other[j] < self[i]; a << other[j]; j += 1 else; a << self[i]; i += 1 end while i < n; a.push(*other[j..-1]) if j < m; a end\r\n def upper_bound; ac, wa = 0, size; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\r\n def lower_bound; ac, wa = size, 0; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\r\n def cum(*xs, &op); a = []; a << xs[0] if xs.size > 0; a << x = self[0]; (1...size).each { |i| a << x = op[x, self[i]] }; a end\r\n def cumdiff(range); self[range.end_open] - self[range.begin]; end\r\nend\r\n\r\nmodule Enumerable\r\n def sorted_uniq; x = nil; filter { |y| c = x === y; x = y; !c } end\r\n def cumsum; ys = [0]; each { |x| ys << x + ys[-1] }; ys end\r\nend\r\n\r\nmain"}], "negative_code": [], "src_uid": "357dcc8fb7783d878cd2c4ed34eb437e"} {"nl": {"description": "Find out if it is possible to partition the first $$$n$$$ positive integers into two non-empty disjoint sets $$$S_1$$$ and $$$S_2$$$ such that:$$$\\mathrm{gcd}(\\mathrm{sum}(S_1), \\mathrm{sum}(S_2)) > 1$$$ Here $$$\\mathrm{sum}(S)$$$ denotes the sum of all elements present in set $$$S$$$ and $$$\\mathrm{gcd}$$$ means thegreatest common divisor.Every integer number from $$$1$$$ to $$$n$$$ should be present in exactly one of $$$S_1$$$ or $$$S_2$$$.", "input_spec": "The only line of the input contains a single integer $$$n$$$ ($$$1 \\le n \\le 45\\,000$$$)", "output_spec": "If such partition doesn't exist, print \"No\" (quotes for clarity). Otherwise, print \"Yes\" (quotes for clarity), followed by two lines, describing $$$S_1$$$ and $$$S_2$$$ respectively. Each set description starts with the set size, followed by the elements of the set in any order. Each set must be non-empty. If there are multiple possible partitions — print any of them.", "sample_inputs": ["1", "3"], "sample_outputs": ["No", "Yes\n1 2\n2 1 3"], "notes": "NoteIn the first example, there is no way to partition a single number into two non-empty sets, hence the answer is \"No\".In the second example, the sums of the sets are $$$2$$$ and $$$4$$$ respectively. The $$$\\mathrm{gcd}(2, 4) = 2 > 1$$$, hence that is one of the possible answers."}, "positive_code": [{"source_code": "def gcd(a,b)\n (b==0 ? a : gcd(b, a%b))\nend\n\nn = gets.strip.to_i\n\n# 44000.upto(45000).each do |n|\n# puts n if n%1000==0\nif n == 1 || n == 2\n # puts n\n puts \"No\"\nelse\n tot = (n*(n+1))/2\n 2.upto(n).each do |s1|\n s2 = tot - s1\n if gcd(s1,s2)>1\n puts \"Yes\"\n puts \"1 #{s1}\"\n others = (1..n).to_a - [s1]\n puts \"#{others.size} #{others.join(\" \")}\"\n exit 0\n end\n end\n puts \"No\"\nend\n# end"}, {"source_code": "x = gets.to_i\n\nif x==1 or x==2\n puts \"No\"\n exit\nelse\n puts \"Yes\"\n \n if x%2==0\n print \"#{x/2} \"\n a = 1\n while (a<=x)\n print \"#{a} \"\n a += 2\n end\n\n puts\n print \"#{x/2} \"\n a = 2\n while (a<=x)\n print \"#{a} \"\n a += 2\n end\n\n else\n\n print \"#{x/2 +1} \"\n a = 1\n while (a<=x)\n print \"#{a} \"\n a += 2\n end\n\n puts\n print \"#{x/2 } \"\n a = 2\n while (a<=x)\n print \"#{a} \"\n a += 2\n end\n end\n puts\nend\n"}], "negative_code": [{"source_code": "x = gets.to_i\n\nif x==1 or x==2\n puts \"No\"\n exit\nelse\n puts \"Yes\"\n \n if x%2==0\n print \"#{x/2} \"\n a = 1\n while (a<=x)\n print \"#{a} \"\n a += 2\n end\n\n puts\n print \"#{x/2 +1} \"\n a = 2\n while (a<=x)\n print \"#{a} \"\n a += 2\n end\n\n else\n\n print \"#{x/2 +1} \"\n a = 1\n while (a<=x)\n print \"#{a} \"\n a += 2\n end\n\n puts\n print \"#{x/2 } \"\n a = 2\n while (a<=x)\n print \"#{a} \"\n a += 2\n end\n end\n puts\nend\n"}], "src_uid": "bb7bace930d5c5f231bfc2061576ec45"} {"nl": {"description": "A queen is the strongest chess piece. In modern chess the queen can move any number of squares in any horizontal, vertical or diagonal direction (considering that there're no other pieces on its way). The queen combines the options given to the rook and the bishop.There are m queens on a square n × n chessboard. You know each queen's positions, the i-th queen is positioned in the square (ri, ci), where ri is the board row number (numbered from the top to the bottom from 1 to n), and ci is the board's column number (numbered from the left to the right from 1 to n). No two queens share the same position.For each queen one can count w — the number of other queens that the given queen threatens (attacks). For a fixed attack direction only the first queen in this direction is under attack if there are many queens are on the ray of the attack. Obviously, for any queen w is between 0 and 8, inclusive.Print the sequence t0, t1, ..., t8, where ti is the number of queens that threaten exactly i other queens, i.e. the number of queens that their w equals i.", "input_spec": "The first line of the input contains a pair of integers n, m (1 ≤ n, m ≤ 105), where n is the size of the board and m is the number of queens on the board. Then m following lines contain positions of the queens, one per line. Each line contains a pair of integers ri, ci (1 ≤ ri, ci ≤ n) — the queen's position. No two queens stand on the same square.", "output_spec": "Print the required sequence t0, t1, ..., t8, separating the numbers with spaces.", "sample_inputs": ["8 4\n4 3\n4 8\n6 5\n1 6", "10 3\n1 1\n1 2\n1 3"], "sample_outputs": ["0 3 0 1 0 0 0 0 0", "0 2 1 0 0 0 0 0 0"], "notes": null}, "positive_code": [{"source_code": "def foo(th, qs, n)\n #h = {}\n h = Array.new(5*n)\n data = []\n qs.each do |i, x, y|\n u, v = yield(x, y)\n data << [i, u, v]\n (h[u] ||= []) << v\n end\n h.each{|v|v.sort! if v}\n data.each do |i, u, v|\n th[i] += 1 if h[u][0] < v\n th[i] += 1 if h[u][-1] > v\n end\nend\n\nn, m = gets.split.map(&:to_i)\nqs = []\n=begin\nm.times do |i|\n qs << [i, *gets.split.map(&:to_i)]\nend\n=end\n$stdin.read.each_line.with_index do |l, i|\n qs << [i, *l.split.map(&:to_i)]\nend\n\nth = [0]*m\n\nfoo(th, qs, n){|x,y| [x, y]}\nfoo(th, qs, n){|x,y| [y, x]}\nfoo(th, qs, n){|x,y| [x+y, x-y]}\nfoo(th, qs, n){|x,y| [x-y, x+y]}\n\nresult = [0]*9\nth.each{|v| result[v]+=1}\nputs result*\" \"\n"}], "negative_code": [], "src_uid": "f19e7f33396d27e1eba2c46b6b5e706a"} {"nl": {"description": "Consider an array $$$a$$$ of $$$n$$$ positive integers.You may perform the following operation: select two indices $$$l$$$ and $$$r$$$ ($$$1 \\leq l \\leq r \\leq n$$$), then decrease all elements $$$a_l, a_{l + 1}, \\dots, a_r$$$ by $$$1$$$. Let's call $$$f(a)$$$ the minimum number of operations needed to change array $$$a$$$ into an array of $$$n$$$ zeros.Determine if for all permutations$$$^\\dagger$$$ $$$b$$$ of $$$a$$$, $$$f(a) \\leq f(b)$$$ is true. $$$^\\dagger$$$ An array $$$b$$$ is a permutation of an array $$$a$$$ if $$$b$$$ consists of the elements of $$$a$$$ in arbitrary order. For example, $$$[4,2,3,4]$$$ is a permutation of $$$[3,2,4,4]$$$ while $$$[1,2,2]$$$ is not a permutation of $$$[1,2,3]$$$.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 10^5$$$) — the length of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$) — description of the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.", "output_spec": "For each test case, print \"YES\" (without quotes) if for all permutations $$$b$$$ of $$$a$$$, $$$f(a) \\leq f(b)$$$ is true, and \"NO\" (without quotes) otherwise. You can output \"YES\" and \"NO\" in any case (for example, strings \"yEs\", \"yes\" and \"Yes\" will be recognized as a positive response).", "sample_inputs": ["3\n\n4\n\n2 3 5 4\n\n3\n\n1 2 3\n\n4\n\n3 1 3 2"], "sample_outputs": ["YES\nYES\nNO"], "notes": "NoteIn the first test case, we can change all elements to $$$0$$$ in $$$5$$$ operations. It can be shown that no permutation of $$$[2, 3, 5, 4]$$$ requires less than $$$5$$$ operations to change all elements to $$$0$$$.In the third test case, we need $$$5$$$ operations to change all elements to $$$0$$$, while $$$[2, 3, 3, 1]$$$ only needs $$$3$$$ operations."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n gets\r\n xs = gets.split.map(&:to_i)\r\n ys = xs.sort.reverse\r\n i, j = 0, xs.size - 1\r\n ok = true\r\n while i < j\r\n next_ = ys.pop\r\n if xs[i] == next_\r\n i += 1\r\n elsif xs[j] == next_\r\n j -= 1\r\n else\r\n ok = false\r\n break\r\n end\r\n end\r\n puts ok ? \"YES\" : \"NO\"\r\nend\r\n"}], "negative_code": [], "src_uid": "c35d309dd6a9569ec298e7128aa3fc0e"} {"nl": {"description": "A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?", "input_spec": "Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.", "output_spec": "Output the maximum possible length of a regular bracket sequence.", "sample_inputs": ["(()))(", "((()())"], "sample_outputs": ["4", "6"], "notes": null}, "positive_code": [{"source_code": "\ninput=gets.split[0].bytes.to_a\nn=input.length\nd=0\nk=0\nn.times{|i|\n if input[i]==40 then\n d+=1\n else\n if d<=0 then\n k+=1\n else\n d-=1\n end\n end\n}\nputs n-d-k\n"}, {"source_code": "#Inf = Float::Infinity\n#\n#geti\n#getia(delimiter = \" \")\n#\n#Object # dcopy\n#\n#String # to_a\n# \"abc\".to_a #=> [\"a\",\"b\",\"c\"]\n#String # to_ia\n# \"1 2 3\".to_ia #=> [1,2,3]\n#\n\ninclude Math\nrequire \"prime\"\nrequire \"bigdecimal\"\nrequire \"bigdecimal/math\"\nrequire \"matrix\"\nrequire \"pp\"\nrequire \"benchmark\"\n\nInf = Float::INFINITY\ndef pbin(obj);case obj;when Integer;puts obj.to_s(2);end;end\nclass String;def to_a;split(\"\");end;\n def to_ia(d = \" \");split(d).map(&:to_i);end;end\ndef getia(*arg);gets.to_ia(*arg);end\ndef geti;gets.to_i;end\nclass Object;def dcopy;Marshal.load(Marshal.dump(self));end;end\n\n#until (s = gets.chomp).length.zero?\n1.times do\n s = gets.chomp\n a = 0\n res = 0\n cnt = 0\n s.each_char do |c|\n if c == \"(\"\n a += 1\n cnt += 1\n else\n if a > 0\n a -= 1\n cnt += 1\n end\n end\n res = [res, cnt-a].max\n end\n p res\nend\n\n"}, {"source_code": "input = gets.chomp\nbra = 0\nres = 0\ninput.chars do |c|\n if c == \"(\"\n bra += 1 \n else \n if bra > 0\n bra -= 1\n res += 2\n end\n end\nend\nputs res\n"}, {"source_code": "ans,bal=0,0\ngets.chomp.chars{|c|\n if c==')'\n bal -= 1\n if bal>=0\n ans+=2\n else\n bal = 0\n end\n else\n bal += 1\n end\n}\np ans"}, {"source_code": "sum = 0\nbalance = 0\ngets.chomp.chars { |c|\n if c == \")\"\n balance -= 1\n if balance >= 0\n sum += 2\n else\n balance = 0\n end\n else\n balance += 1\n end\n}\n\nputs sum"}, {"source_code": "stk=[]\n\np gets.rstrip.each_char.inject(0){|best,c|\n stk << c if c=='('\n\n if c==')' && stk.last == '('\n best += 2\n stk.pop\n end\n best\n}\n"}], "negative_code": [{"source_code": "sum = 0\nbalance = 0\ngets.chomp.chars { |c|\n if c == \")\"\n balance -= 1\n if balance >= 0\n sum += 2\n end\n else\n balance += 1\n end\n}\n\nputs sum"}, {"source_code": "s = gets\n\nwhile s.index(/\\(i*\\)/) != nil\n ind1 = s.index(/\\(i*\\)/)\n ind2 = s.index(\")\", ind1)\n s.sub!(/\\(i*\\)/, \"i\"*(ind2-ind1))\nend\n\nmax = 0\nwhile s.index(/(i+)/) != nil\n if $1.length > max\n max = $1.length\n end\n s.sub!(/i+/,\"\")\nend\n\nputs max*2"}, {"source_code": "s = gets\n\nwhile s.index(/\\(i*\\)/) != nil\n ind1 = s.index(/\\(i*\\)/)\n ind2 = s.index(\")\", ind1)\n s.sub!(/\\(i*\\)/, \"i\"*(ind2-ind1))\nend\n\nmax = -1\nwhile s.index(/(i+)/) != nil\n if $1.length > max\n max = $1.length\n end\n s.sub!(/i+/,\"\")\nend\n\nputs max*2"}], "src_uid": "2ce2d0c4ac5e630bafad2127a1b589dd"} {"nl": {"description": "You have a string $$$s$$$ consisting of lowercase Latin alphabet letters. You can color some letters in colors from $$$1$$$ to $$$k$$$. It is not necessary to paint all the letters. But for each color, there must be a letter painted in that color.Then you can swap any two symbols painted in the same color as many times as you want. After that, $$$k$$$ strings will be created, $$$i$$$-th of them will contain all the characters colored in the color $$$i$$$, written in the order of their sequence in the string $$$s$$$.Your task is to color the characters of the string so that all the resulting $$$k$$$ strings are palindromes, and the length of the shortest of these $$$k$$$ strings is as large as possible.Read the note for the first test case of the example if you need a clarification.Recall that a string is a palindrome if it reads the same way both from left to right and from right to left. For example, the strings abacaba, cccc, z and dxd are palindromes, but the strings abab and aaabaa — are not.", "input_spec": "The first line of input data contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of input data sets in the test. The descriptions of the input data sets follow. The first line of the description of each input data set contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le k \\le n \\le 2 \\cdot 10^5$$$) — the length of the string and the number of colors in which its letters can be painted. The second line of the description of each input data set contains a string $$$s$$$ of length $$$n$$$ consisting of lowercase letters of the Latin alphabet. It is guaranteed that the sum of n over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each set of input data, output a single integer  — the maximum length of the shortest palindrome string that can be obtained.", "sample_inputs": ["10\n\n8 2\n\nbxyaxzay\n\n6 3\n\naaaaaa\n\n6 1\n\nabcdef\n\n6 6\n\nabcdef\n\n3 2\n\ndxd\n\n11 2\n\nabcabcabcac\n\n6 6\n\nsipkic\n\n7 2\n\neatoohd\n\n3 1\n\nllw\n\n6 2\n\nbfvfbv"], "sample_outputs": ["3\n2\n1\n1\n1\n5\n1\n1\n3\n3"], "notes": "Note In the first test case, $$$s$$$=\"bxyaxzay\", $$$k=2$$$. We use indices in the string from $$$1$$$ to $$$8$$$. The following coloring will work: $$$\\mathtt{\\mathbf{\\color{red}{b}\\color{blue}{xy}\\color{red}{a}\\color{blue}{x}z\\color{red}{a}\\color{blue}{y}}}$$$ (the letter z remained uncolored). After painting: swap two red characters (with the indices $$$1$$$ and $$$4$$$), we get $$$\\mathtt{\\mathbf{\\color{red}{a}\\color{blue}{xy}\\color{red}{b}\\color{blue}{x}z\\color{red}{a}\\color{blue}{y}}}$$$; swap two blue characters (with the indices $$$5$$$ and $$$8$$$), we get $$$\\mathtt{\\mathbf{\\color{red}{a}\\color{blue}{xy}\\color{red}{b}\\color{blue}{y}z\\color{red}{a}\\color{blue}{x}}}$$$. Now, for each of the two colors we write out the corresponding characters from left to right, we get two strings $$$\\mathtt{\\mathbf{\\color{red}{aba}}}$$$ and $$$\\mathtt{\\mathbf{\\color{blue}{xyyx}}}$$$. Both of them are palindromes, the length of the shortest is $$$3$$$. It can be shown that the greatest length of the shortest palindrome cannot be achieved. In the second set of input data, the following coloring is suitable: $$$[1, 1, 2, 2, 3, 3]$$$. There is no need to swap characters. Both received strings are equal to aa, they are palindromes and their length is $$$2$$$. In the third set of input data, you can color any character and take it into a string. In the fourth set of input data, you can color the $$$i$$$th character in the color $$$i$$$. In the fifth set of input data can be colored in each of the colors of one character. In the sixth set of input data, the following coloring is suitable: $$$[1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 0]$$$. Rearrange the characters so as to get the palindromes abcba and acbca."}, "positive_code": [{"source_code": "gets.to_i.times do\n n, k = gets.split.map(&:to_i)\n h = Hash.new 0\n gets.strip.each_char { h[_1] += 1 }\n\n pairs = 0; cherries = 0\n h.each do |_, v|\n pairs += v / 2\n cherries += v % 2\n end\n\n base = (pairs / k) * 2\n cherries += (pairs % k) * 2\n if cherries >= k\n base += 1\n end\n\n p [base, 1].max\nend\n"}], "negative_code": [{"source_code": "gets.to_i.times do\n n, k = gets.split.map(&:to_i)\n h = Hash.new 0\n gets.strip.each_char { h[_1] += 1 }\n\n pairs = 0; cherries = 0\n h.each do |_, v|\n if v > 1\n pairs += v / 2\n cherries += v % 2\n else\n cherries += 1\n end\n end\n\n base = (pairs / k) * 2\n cherries += (pairs % k) * 2\n poor = k - pairs % k\n poor = k if poor == 0\n if cherries >= poor\n base += 1\n end\n\n p [base, 1].max\nend\n"}], "src_uid": "ca817fe0a97e2d8a6bcfcb00103b6b6d"} {"nl": {"description": "Consider a table of size $$$n \\times m$$$, initially fully white. Rows are numbered $$$1$$$ through $$$n$$$ from top to bottom, columns $$$1$$$ through $$$m$$$ from left to right. Some square inside the table with odd side length was painted black. Find the center of this square.", "input_spec": "The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n, m \\le 115$$$) — the number of rows and the number of columns in the table. The $$$i$$$-th of the next $$$n$$$ lines contains a string of $$$m$$$ characters $$$s_{i1} s_{i2} \\ldots s_{im}$$$ ($$$s_{ij}$$$ is 'W' for white cells and 'B' for black cells), describing the $$$i$$$-th row of the table.", "output_spec": "Output two integers $$$r$$$ and $$$c$$$ ($$$1 \\le r \\le n$$$, $$$1 \\le c \\le m$$$) separated by a space — the row and column numbers of the center of the black square.", "sample_inputs": ["5 6\nWWBBBW\nWWBBBW\nWWBBBW\nWWWWWW\nWWWWWW", "3 3\nWWW\nBWW\nWWW"], "sample_outputs": ["2 4", "2 1"], "notes": null}, "positive_code": [{"source_code": "$<.each_with_index{|e,i|\nj=e.index('B')\nif j;c=e.count('B')/2+1;puts [i-1+c,j+c]*' ';break;end\n}"}, {"source_code": "n,m=gets.strip.split.map(&:to_i)\n\nary = []\nn.times do\n ary << gets.strip\nend\n\nrf=-1\ncf=-1\nlen = 0\n0.upto(n-1).each do |i|\n 0.upto(m-1).each do |j|\n if ary[i][j]=='B'\n rf=i\n cf=j if cf==-1\n len+=1\n end\n end\n break if rf!=-1\nend\n\n# puts \"#{rf} #{cf} #{len}\"\nputs \"#{rf+1 + (len/2)} #{cf+1 + (len/2)}\""}, {"source_code": "n, m = gets.split.map(&:to_i)\ncs = []\n\nbf = []\nbl = []\n\nn.times do |i|\n gets.chomp.chars.each_with_index do |cell, j|\n if cell == 'B'\n if bf == []\n bf = [i, j]\n end\n bl = [i, j]\n end\n end\nend\n\nna = (bf[0]+bl[0])/2 + 1\nma = (bf[1]+bl[1])/2 + 1\n\nputs \"#{na} #{ma}\""}, {"source_code": "n, m = gets.split.map &:to_i\ns = []\nn.times { s << gets.chomp }\ni, j = 0, 0\ncatch :found do\n n.times do |y|\n m.times do |x|\n if s[y][x] == 'B'\n i, j = y, x\n throw :found\n end\n end\n end\nend\nd = (s[i].rindex('B') - s[i].index('B'))/2\nputs \"#{1 + i + d} #{1 + j + d}\"\n"}], "negative_code": [], "src_uid": "524273686586cdeb7827ffc1ad59d85a"} {"nl": {"description": "You are given an integer $$$x$$$ and an array of integers $$$a_1, a_2, \\ldots, a_n$$$. You have to determine if the number $$$a_1! + a_2! + \\ldots + a_n!$$$ is divisible by $$$x!$$$.Here $$$k!$$$ is a factorial of $$$k$$$ — the product of all positive integers less than or equal to $$$k$$$. For example, $$$3! = 1 \\cdot 2 \\cdot 3 = 6$$$, and $$$5! = 1 \\cdot 2 \\cdot 3 \\cdot 4 \\cdot 5 = 120$$$.", "input_spec": "The first line contains two integers $$$n$$$ and $$$x$$$ ($$$1 \\le n \\le 500\\,000$$$, $$$1 \\le x \\le 500\\,000$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le x$$$) — elements of given array.", "output_spec": "In the only line print \"Yes\" (without quotes) if $$$a_1! + a_2! + \\ldots + a_n!$$$ is divisible by $$$x!$$$, and \"No\" (without quotes) otherwise.", "sample_inputs": ["6 4\n3 2 2 2 3 3", "8 3\n3 2 2 2 2 2 1 1", "7 8\n7 7 7 7 7 7 7", "10 5\n4 3 2 1 4 3 2 4 3 4", "2 500000\n499999 499999"], "sample_outputs": ["Yes", "Yes", "No", "No", "No"], "notes": "NoteIn the first example $$$3! + 2! + 2! + 2! + 3! + 3! = 6 + 2 + 2 + 2 + 6 + 6 = 24$$$. Number $$$24$$$ is divisible by $$$4! = 24$$$.In the second example $$$3! + 2! + 2! + 2! + 2! + 2! + 1! + 1! = 18$$$, is divisible by $$$3! = 6$$$.In the third example $$$7! + 7! + 7! + 7! + 7! + 7! + 7! = 7 \\cdot 7!$$$. It is easy to prove that this number is not divisible by $$$8!$$$."}, "positive_code": [{"source_code": "# This solution relies on the fact that y! is divisible by x! if the value of y >= x\n# So for multiple numbers: m! + n! + o! would be divisible by x! if m, n, and o are less than or equal to x\n\n# Now the above fails if there are factorials of the same number like:\n# (4! + 4! + 4! + 4! + 4!) which is divisible by 5! even though 4 is less than 5\n# In these cases we can try to combine lower order factorials into higher order factorials\n# i.e. (4! + 4! + 4! + 4! + 4!) can be represented as 5(4!) which is 5! thus its clearly divisible by 5!\n\n# If the lower order factorials cannot be combined and are less than divisor factorial then it is not divisible\n\n# I'm applying the above method using by storing the frequency of each number in a hash (called count)\ndef factorial_divisibility?(divisor, num_array)\n # tally method creates a hash by frequency Eg: [1, 1, 2, 3, 3, 3, 3, 3] becomes {1=>2, 2=>1, 3=>5}\n count = num_array.tally\n count.default = 0\n\n # iterate up till the value of divisor any more is not needed\n divisor.times do |i|\n # if possible try to combine factorials and add to next order factorial\n if count[i] > i\n count[i + 1] += count[i] / (i + 1)\n count[i] %= (i + 1)\n end\n return false if count[i].positive? # return false if some factorial that cannot be combined still exists\n end\n true\nend\n\n# code for tests:\n\ndivisor = gets.chomp.split.last.to_i\nnum_arr = gets.chomp.split.map(&:to_i)\n\nfactorial_divisibility?(divisor, num_arr) ? (puts 'Yes') : (puts 'No')\n"}], "negative_code": [{"source_code": "# This solution relies on the fact that y! is divisible by x! if the value of y >= x\n# So for multiple numbers: m! + n! + o! would be divisible by x! if m, n, and o are less than or equal to x\n\n# Now the above fails if there are factorials of the same number like:\n# (4! + 4! + 4! + 4! + 4!) which is divisible by 5! even though 4 is less than 5\n# In these cases we can try to combine lower order factorials into higher order factorials\n# i.e. (4! + 4! + 4! + 4! + 4!) can be represented as 5(4!) which is 5! thus its clearly divisible by 5!\n\n# If the lower order factorials cannot be combined and are less than divisor factorial then it is not divisible\n\n# I'm applying the above method using by storing the frequency of each number in a hash (called count)\ndef factorial_divisibility?(divisor, num_array)\n # tally method creates a hash by frequency Eg: [1, 1, 2, 3, 3, 3, 3, 3] becomes {1=>2, 2=>1, 3=>5}\n count = num_array.tally\n count.default = 0\n\n # iterate up till the value of divisor any more is not needed\n divisor.times do |i|\n # if possible try to combine factorials and add to next order factorial\n if count[i] > i\n count[i + 1] += count[i] / (i + 1)\n count[i] %= (i + 1)\n end\n return false if count[i].positive? # return false if some factorial that cannot be combined still exists\n end\n true\nend\n\n# code for tests:\n\ndivisor = gets.chomp[-1].to_i\nnum_arr = gets.chomp.split.map(&:to_i)\n\nfactorial_divisibility?(divisor, num_arr) ? (puts 'Yes') : (puts 'No')\n"}], "src_uid": "c5ec8b18c39720098f6ac2dbc0ddd4f4"} {"nl": {"description": "Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy.", "input_spec": "The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000).", "output_spec": "On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1.", "sample_inputs": ["banana\n4", "banana\n3", "banana\n2"], "sample_outputs": ["2\nbaan", "3\nnab", "-1"], "notes": "NoteIn the second example, Piegirl can order 3 sheets of stickers with the characters \"nab\". She can take characters \"nab\" from the first sheet, \"na\" from the second, and \"a\" from the third, and arrange them to from \"banana\"."}, "positive_code": [{"source_code": "#!/usr/bin/ruby\nh=Hash.new(0)\ngets.chomp.chars{|c|h[c]+=1}\nn=gets.to_i\nif h.size>n\n\tp -1\n\texit\nend\na=h.sort_by{|e|-e[1]}\nx=[1]*a.size\nz=n-a.size\nwhile z>0\n\tb=a.each_with_index.max_by{|e,i|e.last*1.0/(x[i])}\n\tx[b.last]+=1\n\tz-=1\nend\np a.each_with_index.map{|e,i|(e.last+x[i]-1)/x[i]}.max\nputs a.each_with_index.map{|e,i|e.first*x[i]}*''"}, {"source_code": "#!/usr/bin/ruby\nh=Hash.new(0)\ngets.chomp.chars{|c|h[c]+=1}\nn=gets.to_i\nif h.size>n\n\tp -1\n\texit\nend\na=h.sort_by{|e|-e[1]}\nx=[1]*a.size\n(n-a.size).times{x[a.each_with_index.max_by{|e,i|e.last*1.0/x[i]}.last]+=1}\np a.each_with_index.map{|e,i|(e.last+x[i]-1)/x[i]}.max\nputs a.each_with_index.map{|e,i|e.first*x[i]}*''"}, {"source_code": "s=gets.chomp\nn=gets.to_i\nh=Hash[s.each_char.group_by{|x|x}.map{|k,v|[k,v.size]}]\nif h.size>n\n p -1\n exit\nend\nr=Hash.new(0)\nwhile n>0\n m=h.max_by{|k,v|r.key?(k)?v.to_f/r[k]:2000}\n r[m[0]]+=1\n n-=1\nend\np h.map{|k,v|(v+r[k]-1)/r[k]}.max\nr.each{|k,v|print(k*v)}\n\n\n\n"}, {"source_code": "s=gets.chomp\nn=gets.to_i\nh=Hash[s.each_char.group_by{|x|x}.map{|k,v|[k,v.size]}]\nif h.size>n\n p -1\n exit\nend\nr=Hash.new(0)\nwhile n>0\n m=h.max_by{|k,v|r.key?(k)?v.to_f/r[k]:2000}\n r[m[0]]+=1\n n-=1\nend\np h.map{|k,v|(v+r[k]-1)/r[k]}.max\nr.each{|k,v|print(k*v)}\n\n\n\n"}, {"source_code": "s=gets.chomp\nn=gets.to_i\nh=Hash[s.each_char.group_by{|x|x}.map{|k,v|[k,v.size]}]\nif h.size>n\n p -1\n exit\nend\nr=Hash.new(0)\nwhile n>0\n m=h.max_by{|k,v|r.key?(k)?v.to_f/r[k]:2000}\n r[m[0]]+=1\n n-=1\nend\np h.map{|k,v|(v+r[k]-1)/r[k]}.max\nr.each{|k,v|print(k*v)}\n\n\n"}, {"source_code": "s=gets.chomp\nn=gets.to_i\nh=Hash[s.each_char.group_by{|x|x}.map{|k,v|[k,v.size]}]\nif h.size>n\n p -1\n exit\nend\nr=Hash.new(0)\nwhile n>0\n m=h.max_by{|k,v|r.key?(k)?v.to_f/r[k]:2000}\n r[m[0]]+=1\n n-=1\nend\np h.map{|k,v|(v+r[k]-1)/r[k]}.max\nr.each{|k,v|print(k*v)}\n\n\n\n"}, {"source_code": "s=gets.chomp\nn=gets.to_i\nh=Hash[s.each_char.group_by{|x|x}.map{|k,v|[k,v.size]}]\nif h.size>n\n p -1\n exit\nend\nr=Hash.new(0)\nwhile n>0\n m=h.max_by{|k,v|r.key?(k)?v.to_f/r[k]:2000}\n r[m[0]]+=1\n n-=1\nend\np h.map{|k,v|(v+r[k]-1)/r[k]}.max\nr.each{|k,v|print(k*v)}\n\n\n\n"}, {"source_code": "s=gets.chomp\nn=gets.to_i\nh=Hash[s.each_char.group_by{|x|x}.map{|k,v|[k,v.size]}]\nif h.size>n\n p -1\n exit\nend\nr=Hash.new(0)\nwhile n>0\n m=h.max_by{|k,v|r.key?(k)?v.to_f/r[k]:2000}\n r[m[0]]+=1\n n-=1\nend\np h.map{|k,v|(v+r[k]-1)/r[k]}.max\nr.each{|k,v|print(k*v)}\n\n\n\n"}, {"source_code": "s=gets.chomp\nn=gets.to_i\nh=Hash[s.each_char.group_by{|x|x}.map{|k,v|[k,v.size]}]\nif h.size>n\n p -1\n exit\nend\nr=Hash.new(0)\nwhile n>0\n m=h.max_by{|k,v|r.key?(k)?v.to_f/r[k]:2000}\n r[m[0]]+=1\n n-=1\nend\np h.map{|k,v|(v+r[k]-1)/r[k]}.max\nr.each{|k,v|print(k*v)}\n\n\n"}, {"source_code": "s=gets.chomp\nn=gets.to_i\nh=Hash[s.each_char.group_by{|x|x}.map{|k,v|[k,v.size]}]\nif h.size>n\n p -1\n exit\nend\nr=Hash.new(0)\nwhile n>0\n m=h.max_by{|k,v|r.key?(k)?v.to_f/r[k]:2000}\n r[m[0]]+=1\n n-=1\nend\np h.map{|k,v|(v+r[k]-1)/r[k]}.max\nr.each{|k,v|print(k*v)}\n\n\n\n"}, {"source_code": "s = gets.chomp.scan(/./)\nn = gets.to_i\na = [0] * 26\nfor x in s\n a[x.ord - 'a'.ord] += 1\nend\nl, r = 0, s.length\nwhile r - l > 1\n m = (r + l) / 2\n cnt = n\n for i in 0...26\n if a[i] != 0\n cnt -= (m + a[i] - 1) / m\n end\n end\n if cnt >= 0\n r = m\n else\n l = m\n end\nend\nans = ''\nfor i in 0...26\n if a[i] != 0\n ans += (i + 'a'.ord).chr * ((a[i] + r - 1) / r)\n end\nend\nif n >= ans.length\n ans += 'a' * (n - ans.length)\nelse\n puts -1\n exit\nend\nputs r\nputs ans"}, {"source_code": "s=gets.chomp\nn=gets.to_i\nh=Hash[s.each_char.group_by{|x|x}.map{|k,v|[k,v.size]}]\nif h.size>n\n p -1\n exit\nend\nr=Hash.new(0)\nwhile n>0\n m=h.max_by{|k,v|r.key?(k)?v.to_f/r[k]:2000}\n r[m[0]]+=1\n n-=1\nend\np h.map{|k,v|(v+r[k]-1)/r[k]}.max\nr.each{|k,v|print(k*v)}\n\n\n\n"}, {"source_code": "s=gets.chomp\nn=gets.to_i\nh=Hash[s.each_char.group_by{|x|x}.map{|k,v|[k,v.size]}]\nif h.size>n\n p -1\n exit\nend\nr=Hash.new(0)\nwhile n>0\n m=h.max_by{|k,v|r.key?(k)?v.to_f/r[k]:2000}\n r[m[0]]+=1\n n-=1\nend\np h.map{|k,v|(v+r[k]-1)/r[k]}.max\nr.each{|k,v|print(k*v)}\n\n\n\n"}], "negative_code": [{"source_code": "#!/usr/bin/ruby\nh=Hash.new(0)\ngets.chomp.chars{|c|h[c]+=1}\nn=gets.to_i\nif h.size>n\n\tp -1\n\texit\nend\na=h.sort_by{|e|-e[1]}\nx=[1]*a.size\nz=n-a.size\nwhile z>0\n\tb=a.each_with_index.max_by{|e,i|e.last*1.0/(x[i]+1)}\n\tx[b.last]+=1\n\tz-=1\nend\np a.each_with_index.map{|e,i|(e.last*1.0/x[i]).ceil}.max\nputs a.each_with_index.map{|e,i|e.first*x[i]}*''"}, {"source_code": "#!/usr/bin/ruby\nh=Hash.new(0)\ngets.chomp.chars{|c|h[c]+=1}\nn=gets.to_i\nif h.size>n\n\tp -1\n\texit\nend\na=h.sort_by{|e|-e[1]}\nx=[1]*a.size\nz=n-a.size\nwhile z>0\n\tb=a.each_with_index.max_by{|e,i|e.last*1.0/(x[i]+1)}\n\tx[b.last]+=1\n\tz-=1\nend\np a.each_with_index.map{|e,i|(e.last+x[i]-1)/x[i]}.max\nputs a.each_with_index.map{|e,i|e.first*x[i]}*''"}, {"source_code": "s=gets.chomp\nn=gets.to_i\nh=Hash[s.each_char.group_by{|x|x}.map{|k,v|[k,v.size]}]\nif h.size>n\n p -1\n exit\nend\nr=Hash.new(0)\nwhile n>0\n m=h.max_by{|k,v|r.key?(k)?v/r[k]:2000}\n r[m[0]]+=1\n n-=1\nend\np h.map{|k,v|(v+r[k]-1)/r[k]}.max\nr.each{|k,v|print(k*v)}\n\n\n"}], "src_uid": "f16f00edbc0c2e984caa04f71ae0324e"} {"nl": {"description": "Valera is a little boy. Yesterday he got a huge Math hometask at school, so Valera didn't have enough time to properly learn the English alphabet for his English lesson. Unfortunately, the English teacher decided to have a test on alphabet today. At the test Valera got a square piece of squared paper. The length of the side equals n squares (n is an odd number) and each unit square contains some small letter of the English alphabet.Valera needs to know if the letters written on the square piece of paper form letter \"X\". Valera's teacher thinks that the letters on the piece of paper form an \"X\", if: on both diagonals of the square paper all letters are the same; all other squares of the paper (they are not on the diagonals) contain the same letter that is different from the letters on the diagonals. Help Valera, write the program that completes the described task for him.", "input_spec": "The first line contains integer n (3 ≤ n < 300; n is odd). Each of the next n lines contains n small English letters — the description of Valera's paper.", "output_spec": "Print string \"YES\", if the letters on the paper form letter \"X\". Otherwise, print string \"NO\". Print the strings without quotes.", "sample_inputs": ["5\nxooox\noxoxo\nsoxoo\noxoxo\nxooox", "3\nwsw\nsws\nwsw", "3\nxpx\npxp\nxpe"], "sample_outputs": ["NO", "YES", "NO"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/ruby\nn=gets.to_i\ns=$<.map(&:chomp)\na=s[0][0,1]\nb=s[0][1,1]\nt=n.times.map{|i|\n\tz=b*n\n\tz[i,1]=a\n\tz[n-i-1,1]=a\n\tz\n}\nputs a!=b&&s==t ? 'YES' : 'NO'"}, {"source_code": "n=gets.chomp.to_i\na=[]\nn.times do\n\ta.push(gets.chomp.strip)\nend\nx=a[0][0]\ns=a[0][1]\nif x==s\n\tputs 'NO'\n\texit\nend\nputs((0...n).all? { |i| str = s * n; str[i] = str[n - 1 - i] = x; a[i] == str } ? 'YES' : 'NO')"}, {"source_code": "n = gets.to_i\nm = []\n\nn.times do\n\tm << gets.chop.split(//)\nend\n\ndef method matr, n\n\td = matr[0][0]\n\to = matr[0][1]\n\n\treturn 'NO' if o == d\n\n\tfor i in 0...n\n\t\tfor j in 0...n\n\t\t\tif i == j or i == n - j - 1\n\t\t\t\tif d != matr[i][j]\n\t\t\t\t\treturn 'NO'\n\t\t\t\tend\n\t\t\telse\n\t\t\t\tif o != matr[i][j]\n\t\t\t\t\treturn \"NO\"\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\tend\n\n\treturn 'YES'\nend\n\nputs method(m, n)"}, {"source_code": "class ProblemA\n def initialize\n @dats = STDIN.read.split(\"\\n\")\n @size = @dats[0].to_i\n end\n\n def solve\n return false if @size % 2 != 1 || @size >= 300 || @size < 3\n mtx = []\n (1..@size).each do |idx|\n mtx << @dats[idx]\n end\n mtx.flatten!\n mtx = mtx.join(\"\").split(//)\n diag_char = nil\n not_diag_char = nil\n\n mtx.each_with_index do |char,idx|\n row = idx / @size \n row_mod = row % @size\n \n if idx == row*@size + row_mod || idx == (row+1)*@size - row_mod - 1\n if diag_char.nil?\n diag_char = char\n else\n if diag_char != char\n return false\n end\n end\n else\n if not_diag_char.nil?\n not_diag_char = char\n return false if not_diag_char == diag_char \n else\n if not_diag_char != char\n return false\n end\n end\n end\n end\n return true\n end\nend\n\nsolver = ProblemA.new\nprint solver.solve ? \"YES\" : \"NO\"\n"}, {"source_code": "n = gets.chomp.to_i\npaper = []\nuniq = []\nvalid = true\nn.times do\n line = gets.chomp.split(\"\")\n uniq << line.uniq if !uniq.include? line.uniq\n paper << line\n if uniq.length > 2\n valid = false\n break\n end\nend\npos = []\nx = 0\ny = n - 1\nui = uj = di = dj = 0\nwhile x < n && y >= 0 && valid\n ui = x\n uj = x\n di = y\n dj = x\n pos << [ui, uj] if !pos.include? [ui, uj]\n pos << [di, dj] if !pos.include? [di, dj]\n x += 1\n y -= 1\nend\nx = paper[0][0]\ny = paper[0][1]\npos.sort!\ncenter = (pos.length - 1) / 2\nvalid = x != y if valid\nstart = 0\nfin = start + 1\n(0...n).each do |i|\n break if !valid\n line = paper[i].clone\n d_index = 0\n pos.values_at(start..fin).each do |p|\n b = p[1]\n if x != paper[i][b]\n valid = false\n break\n elsif x == paper[i][b]\n line.delete_at( b - d_index )\n d_index += 1\n end\n end\n line.uniq!\n if line.length > 1\n valid = false\n break\n else\n if y != line[0]\n valid = false\n break\n end\n end\n start = fin + 1\n if start == center\n fin = start\n else\n fin = start + 1\n end\nend\nif valid\n puts \"YES\"\nelse\n puts \"NO\"\nend\n"}, {"source_code": "n=gets.to_i\ns=STDIN.read.split.map{|e| e.chars.to_a}\nok=\"YES\"\nx=s[0][0]\nif n>1 then\n\ty=s[0][1]\n\tok=\"NO\" if y==x\n\tn.times{|i|\n\t\tok=\"NO\" if s[i][i]!=x || s[i][n-i-1]!=x\t\n\t\ts[i].delete_at([n-i-1,i].max)\n\t\tif i!=n-i-1 then\n\t\t\ts[i].delete_at([i,n-i-1].min)\n\t\tend\n\t\t\n\t\ts[i].delete(y)\n\t\tok=\"NO\" if s[i].size>0\n\t}\nend\nputs ok\n\n"}, {"source_code": "n=gets.to_i\nm=[]\n\nn.times do\n m< (line_num/2))\n\t\t\tdiagonals_letters << letter_line[index]\n\t\t\tdiagonals_letters << letter_line[-(index+1)]\n\t\t\tletter_line.delete_at(index)\n\t\t\tletter_line.delete_at(-index)\n\t\t\tletter_line.each { |c| other_letters << c }\n\t\telse\n\t\t\tdiagonals_letters << letter_line[index]\n\t\t\tdiagonals_letters << letter_line[-(index+1)]\n\t\t\tletter_line.delete_at(index)\n\t\t\tletter_line.delete_at(-(index+1))\n\t\t\tletter_line.each { |c| other_letters << c }\n\t\tend\n\tend\n\n\tif ((diagonals_letters.uniq).length == 1) and ((other_letters.uniq).length == 1) and (diagonals_letters[0] != other_letters[0])\n\t\treturn \"YES\"\n\telse\n\t\treturn \"NO\"\n\tend\nend \n\nline_num = ((gets()).split().shift()).to_i\n\nletter_board = []\n\nfor i in 0..(line_num-1)\n\trow = []\n\trow = (gets()).split(//)\n\trow.delete(\"\\n\")\n\tletter_board << row\nend\n\nresult = check_letter(line_num, letter_board)\nputs result"}, {"source_code": "n = gets.to_i\na = (0...n).map { gets.chomp }\nx = a[0][0]\ns = a[0][1]\nif x == s\n puts 'NO'\n exit\nend\nputs((0...n).all? { |i| str = s * n; str[i] = str[n - 1 - i] = x; a[i] == str } ? 'YES' : 'NO')"}, {"source_code": "n = gets.to_i\na = (0...n).map { gets.chomp }\nx = a[0][0]\ns = a[0][1]\nif x == s\n puts 'NO'\n exit\nend\nputs((0...n).all? { |i| str = s * n; str[i] = str[n - 1 - i] = x; a[i] == str } ? 'YES' : 'NO')"}], "negative_code": [{"source_code": "#!/usr/bin/ruby\nn=gets.to_i\ns=$<.map(&:chomp)\na=s[0][0,1]\nb=s[0][1,1]\nt=n.times.map{|i|\n\tz=b*n\n\tz[i,1]=a\n\tz[n-i-1,1]=a\n\tz\n}\nputs s==t ? 'YES' : 'NO'"}, {"source_code": "n = gets.to_i\nm = []\n\nn.times do\n\tm << gets.chop.split(//)\nend\n\ndef method matr, n\n\td = matr[0][0]\n\to = matr[0][1]\n\n\tfor i in 0...n\n\t\tfor j in 0...n\n\t\t\tif i == j or i == n - j - 1\n\t\t\t\tif d != matr[i][j]\n\t\t\t\t\treturn 'NO'\n\t\t\t\tend\n\t\t\telse\n\t\t\t\tif o != matr[i][j]\n\t\t\t\t\treturn \"NO\"\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\tend\n\n\treturn 'YES'\nend\n\nputs method(m, n)"}, {"source_code": "class ProblemA\n def initialize\n @dats = STDIN.read.split(\"\\n\")\n @size = @dats[0].to_i\n end\n\n def solve\n mtx = []\n (1..@size).each do |idx|\n mtx << @dats[idx]\n end\n mtx.flatten!\n mtx = mtx.join(\"\").split(//)\n diag_char = nil\n not_diag_char = nil\n\n mtx.each_with_index do |char,idx|\n row = idx / @size \n row_mod = row % @size\n \n if idx == row*@size + row_mod || idx == (row+1)*@size - row_mod - 1\n if diag_char.nil?\n diag_char = char\n else\n if diag_char != char\n return false\n end\n end\n else\n if not_diag_char.nil?\n not_diag_char = char \n else\n if not_diag_char != char\n return false\n end\n end\n end\n end\n return true\n end\nend\n\nsolver = ProblemA.new\np solver.solve ? \"YES\" : \"NO\"\n"}, {"source_code": "class ProblemA\n def initialize\n @dats = STDIN.read.split(\"\\n\")\n @size = @dats[0].to_i\n end\n\n def solve\n mtx = []\n (1..@size).each do |idx|\n mtx << @dats[idx]\n end\n mtx.flatten!\n mtx = mtx.join(\"\").split(//)\n diag_char = nil\n not_diag_char = nil\n\n mtx.each_with_index do |char,idx|\n row = idx / @size \n row_mod = row % @size\n \n if idx == row*@size + row_mod || idx == (row+1)*@size - row_mod - 1\n if diag_char.nil?\n diag_char = char\n else\n if diag_char != char\n return false\n end\n end\n else\n if not_diag_char.nil?\n not_diag_char = char \n else\n if not_diag_char != char\n return false\n end\n end\n end\n end\n return true\n end\nend\n\nsolver = ProblemA.new\nprint solver.solve ? \"YES\" : \"NO\"\n"}, {"source_code": "class ProblemA\n def initialize\n @dats = STDIN.read.split(\"\\n\")\n @size = @dats[0].to_i\n end\n\n def solve\n return false if @size % 2 != 1 || @size >= 300 || @size < 3\n mtx = []\n (1..@size).each do |idx|\n mtx << @dats[idx]\n end\n mtx.flatten!\n mtx = mtx.join(\"\").split(//)\n diag_char = nil\n not_diag_char = nil\n\n mtx.each_with_index do |char,idx|\n row = idx / @size \n row_mod = row % @size\n \n if idx == row*@size + row_mod || idx == (row+1)*@size - row_mod - 1\n if diag_char.nil?\n diag_char = char\n else\n if diag_char != char\n return false\n end\n end\n else\n if not_diag_char.nil?\n not_diag_char = char \n else\n if not_diag_char != char\n return false\n end\n end\n end\n end\n return true\n end\nend\n\nsolver = ProblemA.new\nprint solver.solve ? \"YES\" : \"NO\"\n"}, {"source_code": "n = gets.chomp.to_i\npaper = []\nuniq = []\nvalid = true\nn.times do\n line = gets.chomp.split(\"\")\n uniq << line.uniq if !uniq.include? line.uniq\n paper << line\n if uniq.length > 2\n valid = false\n break\n end\nend\npos = []\nx = 0\ny = n - 1\nui = uj = di = dj = 0\nwhile x < n && y >= 0 && valid\n ui = x\n uj = x\n di = y\n dj = x\n pos << [ui, uj] if !pos.include? [ui, uj]\n pos << [di, dj] if !pos.include? [di, dj]\n x += 1\n y -= 1\nend\nx = paper[0][0]\ny = paper[0][1]\npos.sort!\ncenter = (pos.length - 1) / 2\nvalid = x != y if valid\nstart = 0\nfin = start + 1\n(0...n).each do |i|\n break if !valid\n line = paper[i].clone\n d_index = 0\n puts pos.inspect\n pos.values_at(start..fin).each do |p|\n b = p[1]\n if x != paper[i][b]\n valid = false\n break\n elsif x == paper[i][b]\n line.delete_at( b - d_index )\n d_index += 1\n end\n end\n line.uniq!\n valid = false if line.length > 1\n start = fin + 1\n if start == center\n fin = start\n else\n fin = start + 1\n end\nend\nif valid\n puts \"YES\"\nelse\n puts \"NO\"\nend\n"}, {"source_code": "n = gets.chomp.to_i\npaper = []\nn.times do\n paper << gets.chomp.split(\"\")\nend\npos = []\nx = 0\ny = n - 1\nui = uj = di = dj = 0\nwhile x < n && y >= 0\n ui = x\n uj = x\n di = y\n dj = x\n pos << [ui, uj] if !pos.include? [ui, uj]\n pos << [di, dj] if !pos.include? [di, dj]\n x += 1\n y -= 1\nend\nvalid = true\nx = paper[0][0]\ny = paper[0][1]\n(0...n).each do |i|\n break if !valid\n (0...n).each do |j|\n break if !valid\n if pos.include? [i, j]\n if x != paper[i][j]\n valid = false\n end\n else\n if y != paper[i][j]\n valid = false\n end\n end\n end\nend\nif valid\n puts \"YES\"\nelse\n puts \"NO\"\nend\n"}, {"source_code": "n = gets.chomp.to_i\npaper = []\nuniq = []\nvalid = true\nn.times do\n line = gets.chomp.split(\"\")\n uniq << line.uniq if !uniq.include? line.uniq\n paper << line\n if uniq.length > 2\n valid = false\n break\n end\nend\nif valid\n pos = []\n x = 0\n y = n - 1\n ui = uj = di = dj = 0\n while x < n && y >= 0\n ui = x\n uj = x\n di = y\n dj = x\n pos << [ui, uj] if !pos.include? [ui, uj]\n pos << [di, dj] if !pos.include? [di, dj]\n x += 1\n y -= 1\n end\n x = paper[0][0]\n y = paper[0][1]\n valid = x != y\n pos.each do |p|\n break if !valid\n i = p[0]\n j = p[1]\n if x != paper[i][j]\n valid = false\n end\n end\nend\nif valid\n puts \"YES\"\nelse\n puts \"NO\"\nend\n"}, {"source_code": "n = gets.chomp.to_i\npaper = []\nuniq = []\nvalid = true\nn.times do\n line = gets.chomp.split(\"\")\n uniq << line.uniq if !uniq.include? line.uniq\n paper << line\n if uniq.length > 2\n valid = false\n break\n end\nend\npos = []\nx = 0\ny = n - 1\nui = uj = di = dj = 0\nwhile x < n && y >= 0 && valid\n ui = x\n uj = x\n di = y\n dj = x\n pos << [ui, uj] if !pos.include? [ui, uj]\n pos << [di, dj] if !pos.include? [di, dj]\n x += 1\n y -= 1\nend\nx = paper[0][0]\ny = paper[0][1]\npos.sort!\ncenter = (pos.length - 1) / 2\nvalid = x != y if valid\nstart = 0\nfin = start + 1\n(0...n).each do |i|\n break if !valid\n line = paper[i].clone\n d_index = 0\n pos.values_at(start..fin).each do |p|\n b = p[1]\n if x != paper[i][b]\n valid = false\n break\n elsif x == paper[i][b]\n line.delete_at( b - d_index )\n d_index += 1\n end\n end\n line.uniq!\n valid = false if line.length > 1\n start = fin + 1\n if start == center\n fin = start\n else\n fin = start + 1\n end\nend\nif valid\n puts \"YES\"\nelse\n puts \"NO\"\nend\n"}, {"source_code": "n=gets.to_i\ns=STDIN.read.split.map{|e| e.chars.to_a}\nok=\"YES\"\nx=s[0][0]\nif n>1 then\n\tn.times{|i|\n\t\tok=\"NO\" if s[i][i]!=x || s[i][n-i-1]!=x\t\n\t\ts[i].delete_at([n-i-1,i].max)\n\t\tif i!=n-i-1 then\n\t\t\ts[i].delete_at([i,n-i-1].min)\n\t\tend\n\t\tok=\"NO\" if s[i][0]==x\n\t\ts[i].delete(s[i][0])\n\t\tok=\"NO\" if s[i].size>0\n\t}\nend\nputs ok\n\n"}, {"source_code": "n=gets.to_i\nm=[]\n\nn.times do\n m< (line_num/2))\n\t\t\tdiagonals_letters << letter_line[index]\n\t\t\tdiagonals_letters << letter_line[-(index+1)]\n\t\t\tletter_line.delete_at(index)\n\t\t\tletter_line.delete_at(-index)\n\t\t\tletter_line.each { |c| other_letters << c }\n\t\telse\n\t\t\tdiagonals_letters << letter_line[index]\n\t\t\tdiagonals_letters << letter_line[-(index+1)]\n\t\t\tletter_line.delete_at(index)\n\t\t\tletter_line.delete_at(-(index+1))\n\t\t\tletter_line.each { |c| other_letters << c }\n\t\tend\n\tend\n\n\tif ((diagonals_letters.uniq).length == 1) and ((other_letters.uniq).length == 1)\n\t\treturn \"YES\"\n\telse\n\t\treturn \"NO\"\n\tend\nend \n\nline_num = ((gets()).split().shift()).to_i\n\nletter_board = []\n\nfor i in 0..(line_num-1)\n\trow = []\n\trow = (gets()).split(//)\n\trow.delete(\"\\n\")\n\tletter_board << row\nend\n\nresult = check_letter(line_num, letter_board)\nputs result"}, {"source_code": "def check_letter (line_num, board)\n\tdiagonals_letters = []\n\tother_letters = []\n\tboard.each_with_index do |letter_line, index|\n\t\tif (index == (line_num/2))\n\t\t\tdiagonals_letters << letter_line[index]\n\t\t\tletter_line.delete_at(index)\n\t\t\tletter_line.each { |c| other_letters << c }\n\t\telsif (index > (line_num/2))\n\t\t\tdiagonals_letters << letter_line[index]\n\t\t\tdiagonals_letters << letter_line[-(index+1)]\n\t\t\tletter_line.delete_at(index)\n\t\t\tletter_line.delete_at(-index)\n\t\t\tletter_line.each { |c| other_letters << c }\n\t\telse\n\t\t\tdiagonals_letters << letter_line[index]\n\t\t\tdiagonals_letters << letter_line[-(index+1)]\n\t\t\tletter_line.delete_at(index)\n\t\t\tletter_line.delete_at(-(index+1))\n\t\t\tletter_line.each { |c| other_letters << c }\n\t\tend\n\tend\n\n\tp diagonals_letters\n\tp other_letters\n\n\tif ((diagonals_letters.uniq).length == 1) and ((other_letters.uniq).length == 1)\n\t\treturn \"YES\"\n\telse\n\t\treturn \"NO\"\n\tend\nend \n\nline_num = ((gets()).split().shift()).to_i\n\nletter_board = []\n\nfor i in 0..(line_num-1)\n\trow = []\n\trow = (gets()).split(//)\n\trow.delete(\"\\n\")\n\tletter_board << row\nend\n\np check_letter(line_num, letter_board)"}, {"source_code": "n = gets.to_i\na = (0...n).map { gets.chomp }\nx = a[0][0]\ns = a[0][1]\nputs((0...n).all? { |i| str = s * n; str[i] = str[n - 1 - i] = x; a[i] == str } ? 'YES' : 'NO')"}], "src_uid": "02a9081aed29ea92aae13950a6d48325"} {"nl": {"description": "Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin \"ConneR\" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \\le n \\le 10^9$$$, $$$1 \\le s \\le n$$$, $$$1 \\le k \\le \\min(n-1, 1000)$$$) — respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \\ldots, a_k$$$ ($$$1 \\le a_i \\le n$$$) — the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.", "output_spec": "For each test case print a single integer — the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.", "sample_inputs": ["5\n5 2 3\n1 2 3\n4 3 3\n4 1 2\n10 2 6\n1 2 3 4 5 7\n2 1 1\n2\n100 76 8\n76 75 36 67 41 74 10 77"], "sample_outputs": ["2\n0\n4\n0\n2"], "notes": "NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor."}, "positive_code": [{"source_code": "def read_array(to)\n gets.strip.split(' ').map(&to)\nend\n\ngets.strip.to_i.times do\n n, s, k = read_array(:to_i)\n closed = read_array(:to_i)\n c = closed.inject({}) { |h, x| h[x] = 1; h }\n steps = 0\n l, r = [s, s]\n until c[l].nil? or c[r].nil?\n l = [1, l - 1].max\n r = [r + 1, n].min\n steps += 1\n end\n\n puts steps\nend"}], "negative_code": [], "src_uid": "faae9c0868b92b2355947c9adcaefb43"} {"nl": {"description": "You have a garland consisting of $$$n$$$ lamps. Each lamp is colored red, green or blue. The color of the $$$i$$$-th lamp is $$$s_i$$$ ('R', 'G' and 'B' — colors of lamps in the garland).You have to recolor some lamps in this garland (recoloring a lamp means changing its initial color to another) in such a way that the obtained garland is nice.A garland is called nice if any two lamps of the same color have distance divisible by three between them. I.e. if the obtained garland is $$$t$$$, then for each $$$i, j$$$ such that $$$t_i = t_j$$$ should be satisfied $$$|i-j|~ mod~ 3 = 0$$$. The value $$$|x|$$$ means absolute value of $$$x$$$, the operation $$$x~ mod~ y$$$ means remainder of $$$x$$$ when divided by $$$y$$$.For example, the following garlands are nice: \"RGBRGBRG\", \"GB\", \"R\", \"GRBGRBG\", \"BRGBRGB\". The following garlands are not nice: \"RR\", \"RGBG\".Among all ways to recolor the initial garland to make it nice you have to choose one with the minimum number of recolored lamps. If there are multiple optimal solutions, print any of them.", "input_spec": "The first line of the input contains one integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) — the number of lamps. The second line of the input contains the string $$$s$$$ consisting of $$$n$$$ characters 'R', 'G' and 'B' — colors of lamps in the garland.", "output_spec": "In the first line of the output print one integer $$$r$$$ — the minimum number of recolors needed to obtain a nice garland from the given one. In the second line of the output print one string $$$t$$$ of length $$$n$$$ — a nice garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them.", "sample_inputs": ["3\nBRB", "7\nRGBGRBB"], "sample_outputs": ["1\nGRB", "3\nRGBRGBR"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\ns = gets.chomp.chars\nputs %w[R G B].permutation.map { |rgb| (0...n).map { rgb[_1 % 3] } }.map { |t|\n\t\t[s.zip(t).count { _1 != _2 }, t.join]\n }.min\n"}, {"source_code": "class Strand\n\n def initialize\n @counts = {\n 'R' => 0,\n 'G' => 0,\n 'B' => 0,\n }\n end\n\n def increase(c)\n @counts[c] += 1\n end\n\n def moves_to_change_to(c)\n @counts.to_a.select{|k, _| k != c }.map{ |_, v| v }.reduce(0, :+)\n end\n\nend\n\ndef solution\n read_int\n gs = read_string\n\n def color_index(c)\n return 0 if c == \"R\"\n return 1 if c == \"G\"\n return 2 if c == \"B\"\n end\n\n s = [\n Strand.new,\n Strand.new,\n Strand.new\n ]\n\n for i in (0...gs.size)\n crt = gs[i]\n j = i % 3\n s[j].increase(crt)\n end\n\n min_moves = gs.size\n best = nil\n for three_cols in (\"RGB\".chars.permutation)\n total_moves = (0...3).map{ |i| s[i].moves_to_change_to(three_cols[i])}.reduce(0, :+)\n if min_moves > total_moves\n min_moves = total_moves\n best = three_cols\n end\n end\n\n final = (0...gs.size).map { |i| best[i % 3] }.join\n\n\n puts min_moves\n puts final\nend\n\ndef read_int\n gets.to_i\nend\n\ndef read_ints\n gets.split.map(&:to_i)\nend\n\ndef read_string\n gets.chomp\nend\n\nsolution unless ENV['TEST__MODE']"}, {"source_code": "n = gets.to_i\ntime = Time.now\ns = gets.chomp.split('')\ncl = ['R', 'G', 'B']\nmn = s.size\nres = s\ncl.permutation.each do |cur|\n t = 0\n str = s.dup\n str.each_with_index do |e, ind|\n if (e != cur[ind % 3])\n t += 1\n str[ind] = cur[ind % 3]\n end\n end\n if (t < mn)\n mn = t\n res = str\n end\nend\nputs mn\nputs res.join('')\n# puts Time.now - time"}, {"source_code": "salts = %w(RGB RBG BRG BGR GRB GBR)\n\nn = Integer(gets)\ns = gets\n\ndef dif(s, t, n)\n d = 0\n n.times do |i|\n d = d + 1 if s[i] != t[i]\n end\n d\nend\n\nbest = \"\"\nbest_dif = s.size + 1\n\nsalts.each do |salt|\n t = (salt * (n / 3 + 2))[0..(n - 1)]\n d = dif(s, t, n)\n if d < best_dif\n best = t\n best_dif = d\n end\nend\n\nputs best_dif\nputs best\n"}], "negative_code": [{"source_code": "n = gets.to_i\ns = gets.chomp.chars\nputs (0...3).map { |k| (0...n).map { 'RGB'[(k + _1) % 3] } }.map { |t|\n\t\t[s.zip(t).count { _1 != _2 }, t.join]\n }.min\n"}, {"source_code": "n = gets.to_i\ns = gets.chomp.split('')\narr = Array.new(3){{'R'=> 0, 'G'=> 0, 'B'=> 0}}\ns.each_with_index do |e, ind|\n arr[ind % 3][e] += 1\nend\n# puts arr.to_s\nres = Array.new(3){0}\n3.times do |i|\n res[i] = arr[i].max_by{|e, ee| ee}[1]\nend\n3.times do |i|\n res[i] = arr[i].select{|e, ee| ee == res[i]}.keys\nend\n# puts res.to_s\nbrr = []\narr.each_with_index do |e, ind|\n e.each do |k, v|\n brr << {'K'=> k, 'V' => v, 'I'=> ind}\n end\nend\nbrr = brr.sort_by{|e| e['V']}.reverse\n# puts brr.to_s\nans = Array.new(3){'_'}\ncolors = ['R', 'G', 'B']\nbrr.each do |e|\n if (ans[e['I']] == '_' && colors.include?(e['K']))\n ans[e['I']] = e['K']\n colors.delete(e['K'])\n end\nend\n\ncur = 0\ns.each_with_index do |e, ind|\n if (e != ans[ind % 3])\n cur += 1\n s[ind] = ans[ind % 3]\n end\nend\nputs cur\nputs s.join('')"}, {"source_code": "n = gets.to_i\ns = gets.chomp.split('')\narr = Array.new(3){{'R'=> 0, 'G'=> 0, 'B'=> 0}}\ns.each_with_index do |e, ind|\n arr[ind % 3][e] += 1\nend\n# puts arr.to_s\nbrr = []\narr.each_with_index do |e, ind|\n e.each do |k, v|\n brr << {'K'=> k, 'V' => v, 'I'=> ind}\n end\nend\nbrr = brr.sort_by{|e| e['V']}.reverse\n# puts brr\nans = Array.new(3){'_'}\ncolors = ['R', 'G', 'B']\nbrr.each do |e|\n if (ans[e['I']] == '_' && colors.include?(e['K']))\n ans[e['I']] = e['K']\n colors.delete(e['K'])\n end\nend\n\ncur = 0\ns.each_with_index do |e, ind|\n if (s[ind] != ans[ind % 3])\n cur += 1\n s[ind] = ans[ind % 3]\n end\nend\nputs cur\nputs s.join('')"}, {"source_code": "salts = %w(RGB RBG BRG BGR GRB GBR)\n\nn = Integer(gets)\ns = gets\n\ndef dif(s, t, n)\n d = 0\n n.times do |i|\n d = d + 1 if s[i] != t[i]\n end\n d\nend\n\nbest = \"\"\nbest_dif = s.size + 1\n\nsalts.each do |salt|\n t = (salt * (n / 3 + 2))[0..(n - 1)]\n puts t\n d = dif(s, t, n)\n if d < best_dif\n best = t\n best_dif = d\n end\nend\n\nputs best_dif\nputs best\n"}], "src_uid": "e67b79e39511b0107a51edc0179afb82"} {"nl": {"description": "Hongcow is ruler of the world. As ruler of the world, he wants to make it easier for people to travel by road within their own countries.The world can be modeled as an undirected graph with n nodes and m edges. k of the nodes are home to the governments of the k countries that make up the world.There is at most one edge connecting any two nodes and no edge connects a node to itself. Furthermore, for any two nodes corresponding to governments, there is no path between those two nodes. Any graph that satisfies all of these conditions is stable.Hongcow wants to add as many edges as possible to the graph while keeping it stable. Determine the maximum number of edges Hongcow can add.", "input_spec": "The first line of input will contain three integers n, m and k (1 ≤ n ≤ 1 000, 0 ≤ m ≤ 100 000, 1 ≤ k ≤ n) — the number of vertices and edges in the graph, and the number of vertices that are homes of the government. The next line of input will contain k integers c1, c2, ..., ck (1 ≤ ci ≤ n). These integers will be pairwise distinct and denote the nodes that are home to the governments in this world. The following m lines of input will contain two integers ui and vi (1 ≤ ui, vi ≤ n). This denotes an undirected edge between nodes ui and vi. It is guaranteed that the graph described by the input is stable.", "output_spec": "Output a single integer, the maximum number of edges Hongcow can add to the graph while keeping it stable.", "sample_inputs": ["4 1 2\n1 3\n1 2", "3 3 1\n2\n1 2\n1 3\n2 3"], "sample_outputs": ["2", "0"], "notes": "NoteFor the first sample test, the graph looks like this: Vertices 1 and 3 are special. The optimal solution is to connect vertex 4 to vertices 1 and 2. This adds a total of 2 edges. We cannot add any more edges, since vertices 1 and 3 cannot have any path between them.For the second sample test, the graph looks like this: We cannot add any more edges to this graph. Note that we are not allowed to add self-loops, and the graph must be simple."}, "positive_code": [{"source_code": "# TEMPLATE BEGIN\ndef read_vector\n gets.chomp.split(/\\s+/)\nend\ndef read_vector_int\n read_vector.map(&:to_i)\nend\n# TEMPLATE END\n\nclass SNM\n def initialize(n)\n @a = (0..n).to_a\n end\n\n def get(v)\n return v if @a[v] == v\n @a[v] = get(@a[v])\n end\n\n def join(u, v)\n pu = get(u)\n pv = get(v)\n @a[pu] = @a[pv] if pu != pv\n end\nend\n\nn, m, k = read_vector_int\n\ncapitals = read_vector_int\n\nsnm = SNM.new(n)\nedges = m.times.map{ read_vector_int }\n\nedges.each do |a|\n u, v = a\n snm.join(u, v)\nend\n\nvertices_in_comp = Array.new(n + 1) { [] }\n(1..n).each do |id|\n comp_id = snm.get(id)\n vertices_in_comp[comp_id].push(id)\nend\n\nans = -edges.size\nmax_comp = 0\ncapitals.each do |id|\n comp_id = snm.get(id)\n comp_size = vertices_in_comp[comp_id].size\n max_comp = [max_comp, comp_size].max\n ans += (comp_size * (comp_size - 1)) / 2\n\n vertices_in_comp[comp_id] = []\nend\n\nvertices_in_comp.each do |comp|\n next if comp.empty?\n\n ans += (comp.size * (comp.size - 1)) / 2\n ans += comp.size * max_comp\n max_comp += comp.size\nend\n\nputs ans\n"}], "negative_code": [], "src_uid": "6cf43241b14e4d41ad5b36572f3b3663"} {"nl": {"description": "You are an experienced Codeforces user. Today you found out that during your activity on Codeforces you have made y submissions, out of which x have been successful. Thus, your current success rate on Codeforces is equal to x / y.Your favorite rational number in the [0;1] range is p / q. Now you wonder: what is the smallest number of submissions you have to make if you want your success rate to be p / q?", "input_spec": "The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. Each of the next t lines contains four integers x, y, p and q (0 ≤ x ≤ y ≤ 109; 0 ≤ p ≤ q ≤ 109; y > 0; q > 0). It is guaranteed that p / q is an irreducible fraction. Hacks. For hacks, an additional constraint of t ≤ 5 must be met.", "output_spec": "For each test case, output a single integer equal to the smallest number of submissions you have to make if you want your success rate to be equal to your favorite rational number, or -1 if this is impossible to achieve.", "sample_inputs": ["4\n3 10 1 2\n7 14 3 8\n20 70 2 7\n5 6 1 1"], "sample_outputs": ["4\n10\n0\n-1"], "notes": "NoteIn the first example, you have to make 4 successful submissions. Your success rate will be equal to 7 / 14, or 1 / 2.In the second example, you have to make 2 successful and 8 unsuccessful submissions. Your success rate will be equal to 9 / 24, or 3 / 8.In the third example, there is no need to make any new submissions. Your success rate is already equal to 20 / 70, or 2 / 7.In the fourth example, the only unsuccessful submission breaks your hopes of having the success rate equal to 1."}, "positive_code": [{"source_code": "n = gets.to_i\nfor _ in 0 ... n\n x, y, p, q = gets.split.map(&:to_i)\n y -= x\n q -= p\n if p == 0 || q == 0\n if (x == 0 && p == 0) || (y == 0 && q == 0)\n puts 0\n else\n puts(-1)\n end\n next\n end\n a1 = (x + p - 1) / p\n a2 = (y + q - 1) / q\n a = [a1, a2].max\n r1 = a * p - x\n r2 = a * q - y\n puts r1 + r2\nend\n\n"}, {"source_code": "gets.to_i.times do\n x, y, p, q=gets.split.map &:to_i\n \n if (x0 and p==0)\n puts -1\n elsif x*q==y*p\n puts 0\n elsif x*q>y*p\n b=64\n n=(1<0\n v=(n-a)*q-y\n n-=a if v>0 and x*q<=(v+y)*p\n a>>=1\n end\n puts n*q-y\n else\n b=64\n n=(1<0\n v=(n-a)*q-y\n n-=a if v>0 and (v+x)*q>=(v+y)*p\n a>>=1\n end\n puts n*q-y\n end\nend\n"}, {"source_code": "def min(a,b)\n a < b ? a : b\nend\n\ngets.to_i.times do\n x, y, p, q = gets.split(/\\s+/).map(&:to_i)\n\n if p == q\n ans = x == y ? 0 : -1\n elsif p == 0\n ans = x == 0 ? 0 : -1\n else\n d = p*y - q*x\n k = q - p\n if d == 0\n ans = 0\n elsif d > 0 && d % k == 0\n ans = d/k\n else\n k = ((y - x.to_f) / (q - p)).ceil\n if k*p < x\n k = x / p + min(x%p, 1)\n end\n\n if k * q - y < 0\n ans = -1\n else\n ans = q*k-y\n end\n end\n end\n\n puts ans\nend\n"}], "negative_code": [{"source_code": "n = gets.to_i\nfor i in 0 ... n\n x, y, p, q = gets.split.map(&:to_i)\n y -= x\n q -= p\n if p == 0 || q == 0\n puts(-1)\n next\n end\n a1 = (x + p - 1) / p\n a2 = (y + q - 1) / q\n a = [a1, a2].max\n r1 = a * p - x\n r2 = a * q - y\n puts r1 + r2\nend\n\n"}, {"source_code": "gets.to_i.times do\n x, y, p, q=gets.split.map &:to_i\n\n if xy*p\n n=(1<<60)-1\n a=1<<59\n while a>0\n v=(n-a)*q-y\n n-=a if v>0 and x*q<=(v+y)*p\n a>>=1\n end\n puts n*q-y\n else\n n=(1<<60)-1\n a=1<<59\n while a>0\n v=(n-a)*q-y\n n-=a if v>0 and (v+x)*q>=(v+y)*p\n a>>=1\n end\n puts n*q-y\n end\nend\n"}, {"source_code": "gets.to_i.times do\n x, y, p, q=gets.split.map &:to_i\n \n if xy*p\n b=64\n n=(1<0\n v=(n-a)*q-y\n n-=a if v>0 and x*q<=(v+y)*p\n a>>=1\n end\n puts n*q-y\n else\n b=64\n n=(1<0\n v=(n-a)*q-y\n n-=a if v>0 and (v+x)*q>=(v+y)*p\n a>>=1\n end\n puts n*q-y\n end\nend\n"}], "src_uid": "589f3f7366d1e0f9185ed0926f5a10bb"} {"nl": {"description": "You and your friend are playing the game Mortal Kombat XI. You are trying to pass a challenge tower. There are $$$n$$$ bosses in this tower, numbered from $$$1$$$ to $$$n$$$. The type of the $$$i$$$-th boss is $$$a_i$$$. If the $$$i$$$-th boss is easy then its type is $$$a_i = 0$$$, otherwise this boss is hard and its type is $$$a_i = 1$$$.During one session, either you or your friend can kill one or two bosses (neither you nor your friend can skip the session, so the minimum number of bosses killed during one session is at least one). After your friend session, your session begins, then again your friend session begins, your session begins, and so on. The first session is your friend's session.Your friend needs to get good because he can't actually kill hard bosses. To kill them, he uses skip points. One skip point can be used to kill one hard boss.Your task is to find the minimum number of skip points your friend needs to use so you and your friend kill all $$$n$$$ bosses in the given order.For example: suppose $$$n = 8$$$, $$$a = [1, 0, 1, 1, 0, 1, 1, 1]$$$. Then the best course of action is the following: your friend kills two first bosses, using one skip point for the first boss; you kill the third and the fourth bosses; your friend kills the fifth boss; you kill the sixth and the seventh bosses; your friend kills the last boss, using one skip point, so the tower is completed using two skip points. You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 2 \\cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) — the number of bosses. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$0 \\le a_i \\le 1$$$), where $$$a_i$$$ is the type of the $$$i$$$-th boss. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 \\cdot 10^5$$$ ($$$\\sum n \\le 2 \\cdot 10^5$$$).", "output_spec": "For each test case, print the answer: the minimum number of skip points your friend needs to use so you and your friend kill all $$$n$$$ bosses in the given order.", "sample_inputs": ["6\n8\n1 0 1 1 0 1 1 1\n5\n1 1 1 1 0\n7\n1 1 1 1 0 0 1\n6\n1 1 1 1 1 1\n1\n1\n1\n0"], "sample_outputs": ["2\n2\n2\n2\n1\n0"], "notes": null}, "positive_code": [{"source_code": "gets.to_i.times do\n n = gets.to_i\n a = gets.split.map &:to_i\n i, res = 1, a[0]\n while i < n\n if a[i].zero?\n i += 1\n next\n end\n j = i\n j += 1 while j + 1 < n && a[j + 1] == 1\n res += (j - i + 1) / 3\n i = j + 1\n end\n puts res\nend\n"}], "negative_code": [], "src_uid": "d34ffd75ef82111d1077db4b033d5195"} {"nl": {"description": "There is a chessboard of size $$$n$$$ by $$$n$$$. The square in the $$$i$$$-th row from top and $$$j$$$-th column from the left is labelled $$$(i,j)$$$.Currently, Gregor has some pawns in the $$$n$$$-th row. There are also enemy pawns in the $$$1$$$-st row. On one turn, Gregor moves one of his pawns. A pawn can move one square up (from $$$(i,j)$$$ to $$$(i-1,j)$$$) if there is no pawn in the destination square. Additionally, a pawn can move one square diagonally up (from $$$(i,j)$$$ to either $$$(i-1,j-1)$$$ or $$$(i-1,j+1)$$$) if and only if there is an enemy pawn in that square. The enemy pawn is also removed.Gregor wants to know what is the maximum number of his pawns that can reach row $$$1$$$?Note that only Gregor takes turns in this game, and the enemy pawns never move. Also, when Gregor's pawn reaches row $$$1$$$, it is stuck and cannot make any further moves.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1\\le t\\le 2\\cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of three lines. The first line contains a single integer $$$n$$$ ($$$2\\le n\\le 2\\cdot{10}^{5}$$$) — the size of the chessboard. The second line consists of a string of binary digits of length $$$n$$$, where a $$$1$$$ in the $$$i$$$-th position corresponds to an enemy pawn in the $$$i$$$-th cell from the left, and $$$0$$$ corresponds to an empty cell. The third line consists of a string of binary digits of length $$$n$$$, where a $$$1$$$ in the $$$i$$$-th position corresponds to a Gregor's pawn in the $$$i$$$-th cell from the left, and $$$0$$$ corresponds to an empty cell. It is guaranteed that the sum of $$$n$$$ across all test cases is less than $$$2\\cdot{10}^{5}$$$.", "output_spec": "For each test case, print one integer: the maximum number of Gregor's pawns which can reach the $$$1$$$-st row.", "sample_inputs": ["4\n3\n000\n111\n4\n1111\n1111\n3\n010\n010\n5\n11001\n00000"], "sample_outputs": ["3\n4\n0\n0"], "notes": "NoteIn the first example, Gregor can simply advance all $$$3$$$ of his pawns forward. Thus, the answer is $$$3$$$.In the second example, Gregor can guarantee that all $$$4$$$ of his pawns reach the enemy row, by following the colored paths as demonstrated in the diagram below. Remember, only Gregor takes turns in this \"game\"! In the third example, Gregor's only pawn is stuck behind the enemy pawn, and cannot reach the end.In the fourth example, Gregor has no pawns, so the answer is clearly $$$0$$$."}, "positive_code": [{"source_code": "for i in 0..gets.to_i-1\r\n\tn=gets.to_i\r\n\ta=gets[..-2]\r\n\tb=gets[..-2]\r\n\tk=0\r\n\tfor i in 0..n-1 do\r\n\t\tif b[i]=='1'\r\n\t\t\tif a[i]=='0'\r\n\t\t\t\tk+=1\r\n\t\t\telsif i-1>=0 and a[i-1]=='1'\r\n\t\t\t\tk+=1\r\n\t\t\t\ta[i-1]='0'\r\n\t\t\telsif i+1 0\r\n c += 1\r\n ap[i - 1] = 0\r\n elsif ep[i] == 1 && ap[i + 1] == 1 && i < n - 1\r\n c +=1\r\n ap[i + 1] = 0\r\n elsif ep[i] == 0 && ap[i] == 1\r\n c += 1\r\n ap[i] = 0\r\n end\r\n end\r\n puts c\r\nend\r\n\r\ndef main\r\n gi.times { solve }\r\nend\r\n\r\nmain\r\n"}], "negative_code": [{"source_code": "for i in 0..gets.to_i-1\r\n n=gets.to_i\r\n a=gets[..-2]\r\n b=gets[..-2]\r\n k=0\r\n for i in 0..n-1 do\r\n \tif b[i]=='1'\r\n \t\tif a[i]=='0'\r\n \t\t\tk+=1\r\n \t\telsif i-1>=0 and a[i-1]=='1'\r\n \t\t\tk+=1\r\n \t\telsif i+1= is ? 0 : 1) * (2**p)\n end\n\n result\nend\n\nt.times do\n n, l = readline.strip.split(/\\s+/).map(&:to_i)\n x = readline.strip.split(/\\s+/).map(&:to_i)\n puts calc_min(l,x)\nend\n"}], "negative_code": [], "src_uid": "84c88932e107e1d1f80b44aec88134e4"} {"nl": {"description": "After the lessons n groups of schoolchildren went outside and decided to visit Polycarpus to celebrate his birthday. We know that the i-th group consists of si friends (1 ≤ si ≤ 4), and they want to go to Polycarpus together. They decided to get there by taxi. Each car can carry at most four passengers. What minimum number of cars will the children need if all members of each group should ride in the same taxi (but one taxi can take more than one group)?", "input_spec": "The first line contains integer n (1 ≤ n ≤ 105) — the number of groups of schoolchildren. The second line contains a sequence of integers s1, s2, ..., sn (1 ≤ si ≤ 4). The integers are separated by a space, si is the number of children in the i-th group.", "output_spec": "Print the single number — the minimum number of taxis necessary to drive all children to Polycarpus.", "sample_inputs": ["5\n1 2 4 3 3", "8\n2 3 4 4 2 1 3 1"], "sample_outputs": ["4", "5"], "notes": "NoteIn the first test we can sort the children into four cars like this: the third group (consisting of four children), the fourth group (consisting of three children), the fifth group (consisting of three children), the first and the second group (consisting of one and two children, correspondingly). There are other ways to sort the groups into four cars."}, "positive_code": [{"source_code": "n = gets.to_i\ns = gets.split.map &:to_i\n\ns = s.sort.reverse\n\ni = 0\nj = s.size - 1\nwhile i < j\n if s[i] + s[j] > 4\n i += 1\n else\n j -= 1\n s[i] += s[j]\n end\nend\n\nputs i+1"}, {"source_code": "a=gets.chomp.to_i\nb=gets.chomp.split(\" \")\nfour=b.count('4')\nthree=b.count('3')\ntwo=b.count('2')\none=b.count('1')\nif (three>= one )\nres= four + three + (two/2.0).ceil\nputs \"#{res}\"\nelsif (three < one && two%2 == 0)\nres= four + two/2 + three+ ((one- three)/4.0).ceil\nputs \"#{res}\"\nelsif (three < one && two%2 == 1)\nif (one-three <= 2)\nres= four + (two/2.0).ceil + three\nputs \"#{res}\"\nend\nif (one-three >2)\nres=four+three+(two/2.0).ceil+ ((one-2-three)/4.0).ceil\nputs \"#{res}\"\nend\nend"}, {"source_code": "t=[0]*5;[*$<][1].split.map{|_|t[_.to_i]+=1};_,a,b,c,d=*t\np ([0,a-c].max+2*b+3)/4+c+d"}, {"source_code": "group = [0] * 5\n\ngets\ns = gets.split(' ').map do |x|\n group[x.to_i] += 1\nend\n\nr = group[4] + group[3]\nr += (group[2] / 2.0).ceil\ngroup[2] = group[2] % 2\n\nif group[3] == 0\n if group[2] == 0\n r += (group[1] / 4.0).ceil\n else\n if group[1] > 2\n r += ((group[1] - 2) / 4.0).ceil\n end\n end\nelse\n if group[1] > group[3]\n group[1] -= group[3]\n if group[2] == 0\n r += (group[1] / 4.0).ceil\n else\n if group[1] > 2\n r += ((group[1] - 2) / 4.0).ceil\n end\n end\n end\nend\n\nputs r\n"}, {"source_code": "n = gets.chomp.to_i\nhs = Hash.new 0\nar = gets.split(' ',n).map{|_|_.to_i}\nar.each do |x|\n hs[x]+=1\nend\nans = hs[4]+hs[3]+hs[2]/2+hs[2]%2\nhs[1]-=hs[3]\nhs[1]-=hs[2]%2*2\nans+=(hs[1]-1)/4+1 if hs[1]>0\nputs ans"}, {"source_code": "n = gets.chomp.to_i\ngroups = gets.split.collect{|x| x.to_i}\n\ncounts = Array.new(5) { 0 }\nn.times{|i|\n counts[groups[i]] += 1\n}\n\n# all groups of 3 or 4 need a separate car\n# groups of 3 can take extra groups of 1\ncars = counts[4] + counts[3]\ncounts[1] -= counts[3]\n\n# two groups of 2 fit into one car\n# if odd number of groups, the last group of 2 can be combined with two groups of 1\ncars += (counts[2] / 2.to_f).ceil\ncounts[1] -= 2 if counts[2] % 2 != 0\n\n# four groups of 1 fit into one car\ncars += (counts[1] / 4.to_f).ceil if counts[1] > 0\n\nputs cars"}, {"source_code": "gets\na = Hash.new(0)\nk = 0\ngets.split.map(&:to_i).each { |elem| a[elem]+=1 }\nk += a[4]\nt = [a[3], a[1]].min\nk += t\na[3] -= t\na[1] -= t\nk += a[3]\nk += a[2]/2\na[2] %= 2\nif a[2] > 0\n k += 1\n a[2] = 0\n a[1] -= 2; a[1] = 0 if a[1] < 0\nend\nk += a[1]/4 + (a[1]%4 != 0 ? 1 : 0)\nputs k"}, {"source_code": "c=[0]*5\ngets\ngets.split.each{|_|c[_.to_i]+=1}\np ([0,c[1]-c[3]].max+2*c[2]+3)/4+c[3]+c[4]\n"}, {"source_code": "n=gets.chomp\n\ntaxi=gets.chomp\ncount4=0\ncount3=0\ncount2=0\ncount1=0\na=[]\ntaxi.split(\" \").each do |i|\n if i.to_i==1 \n count1+=1\n end\n if i.to_i==2 \n count2+=1\n end\n if i.to_i==3 \n count3+=1\n end\n if i.to_i==4\n count4+=1\n end\nend\ncounttaxi=0\ncounttaxi += count4\ncounttaxi += count3\ncount1 = count1-count3\ncounttaxi += count2/2\n\nif count2%2==1\n counttaxi += 1\n count1 -= 2\nend\n\nif count1>0\n if count1 % 4 ==0\n counttaxi += count1/4 \n else\n counttaxi += count1/4 +1\n\n end\nend\n\np counttaxi\n\n"}, {"source_code": "r=->{gets.split.map &:to_i}\na=r[]\nb=r[]\nc4 = b.count{|res| res==4}\nc3 = b.count{|res| res==3}\nc2 = b.count{|res| res==2}\nc1 = b.count{|res| res==1}\nabs = (c1 - c3).abs\nres = c4 + c2/2 + [c1,c3].min + (c2&1<1 ? 0 : 1) + (c3 > c1 ? abs : (c2&1<1 ? (-abs/4).abs : (abs <= 2 ? 0 : (-(abs-2)/4).abs) ))\np res"}, {"source_code": "gets\ns = gets.split.map(&:to_i)\na = s.count 4\nb = s.count 3\nc = s.count 2\nd = s.count 1\nputs a + b + (c+1)/2 + ([d - b - (c%2)*2, 0].max + 3)/4"}, {"source_code": "a=[0]*5\ngets\ngets.split.map {|x| a[x.to_i]+=1}\na[1]=[a[1]-a[3],0].max\nputs a[4]+a[3]+a[2]/2-(-(a[1]+a[2]%2*2))/4"}, {"source_code": "n = gets.to_i\na = gets.split(' ').map(&:to_i).sort.reverse\n\nresult = n - a.delete_if { |x| x == 4 }.size\n\nuntil a.empty? do\n result += 1\n x = a.shift\n break if a.empty?\n\n while a.last + x <= 4 do\n x += a.pop\n break if a.empty?\n end\nend\n\nputs result\n"}, {"source_code": "t=[0]*5;[*$<][1].split.map{|_|t[_.to_i]+=1};_,a,b,c,d=*t\np ([0,a-c].max+2*b+3)/4+c+d"}, {"source_code": "n = gets.chomp\ninput = gets.chomp\n\ntaxi = 0\ntres = 0\ndos = 0\nuno = 0\n\ninput.split(\" \").each { |x|\n x = x.to_i\n if x == 1\n uno += 1\n elsif x == 2\n dos += 1\n elsif x == 3\n tres += 1\n else\n taxi += 1\n end\n}\n\ntaxi += tres\n\ntres >= uno ? uno = 0 : uno = (uno - tres)\n\ntaxi += (dos * 2) / 4\nresto_dos = dos.modulo(2)\n\ntaxi += (((resto_dos * 2) + uno).to_f / 4).ceil\n\nputs taxi\n"}, {"source_code": "t=[0]*5;[*$<][1].split.map{|_|t[_.to_i]+=1};_,a,b,c,d=*t\np ([0,a-c].max+2*b+3)/4+c+d\n"}, {"source_code": "n = Integer(gets)\ninput = gets.chomp\nnums = input.split\ns = Array.new\nfor i in 0...n\n s[i] = Integer(nums[i])\nend\n\ncar1 = 0\ncar2 = 0\ncar3 = 0\ncar4 = 0\nfor i in 0...n\n if s[i] == 1\n car1 += 1\n elsif \n s[i] == 2\n car2 += 1\n elsif \n s[i] == 3\n car3 += 1\n else\n car4 += 1\n end\nend \n\ncar_2 = car2 / 2\ncar_2_still = car2 % 2\n\nif car_2_still == 1\n car_2 += 1\n if car1 == 1\n car1 = car1 - 1\n elsif car1 >= 2\n car1 = car1 - 2\n \n end\nend\n\n\n\nif car3 > car1\n car = car1\n car_0 = car3 - car1\nelsif car3 < car1\n car = car3\n car_0 = car1 - car3\n if car_0 <= 4\n car_0 = 1\n else\n if car_0 % 4 == 0\n car_0 = car_0 / 4\n else\n car_0 = car_0 / 4 + 1\n end\n end\nelse\n car = car3\n car_0 = 0\nend\n\nsum = car4 + car_2 + car + car_0 \nputs sum"}, {"source_code": "count = Hash.new(0)\n\n_ = gets\ngets.split(' ').each { |x| count[x.to_i] += 1 }\n\nanswer = count[4] + count[3]\ncount[1] = [0, count[1] - count[3]].max\nanswer += (count[2].to_f / 2).ceil\ncount[1] = [0, count[1] - (count[2] % 2) * 2].max\nanswer += (count[1].to_f / 4).ceil\n\nputs answer\n"}, {"source_code": "s = gets.chomp\nn = s.to_i\na = gets.split(\" \")\na = a.sort{|x,y| y <=> x }\nk = 0\nit = n-1\ncount = 0\nfor i in 0..it\n a[i] = a[i].to_i\n a[it] = a[it].to_i \n if k + a[i] > 4 \n if k+a[it]<= 4\n it -= 1\n end\n count += 1\n k = a[i]\n else \n k += a[i]\n if k == 4 \n k = 0\n count += 1\n end\n end \n if i == it \n break\n end\nend\nif(k != 0)\n count += 1 \n end\nputs(count)"}, {"source_code": "t=[0]*5;[*$<][1].split.map{|_|t[_.to_i]+=1};_,a,b,c,d=*t\np ([0,a-c].max+2*b+3)/4+c+d\n"}, {"source_code": "n=gets.chomp\n\ntaxi=gets.chomp\ncount4=0\ncount3=0\ncount2=0\ncount1=0\na=[]\ntaxi.split(\" \").each do |i|\n if i.to_i==1 \n count1+=1\n end\n if i.to_i==2 \n count2+=1\n end\n if i.to_i==3 \n count3+=1\n end\n if i.to_i==4\n count4+=1\n end\nend\ncounttaxi=0\ncounttaxi += count4\ncounttaxi += count3\ncount1 = count1-count3\ncounttaxi += count2/2\n\nif count2%2==1\n counttaxi += 1\n count1 -= 2\nend\n\nif count1>0\n if count1 % 4 ==0\n counttaxi += count1/4 \n else\n counttaxi += count1/4 +1\n\n end\nend\n\np counttaxi\n\n"}, {"source_code": "limit = gets.to_i\nx = gets.split(\" \").map{|x| x.to_i}\nx =x.sort.reverse\ni=0\nj=limit-1\nans =0\nwhile i<=j\n\tans+=1\n\tfour = 4-x[i]\n\twhile x[j]<=four and j>=i\n\t\tfour-=x[j]\n\t\tj-=1\n\tend\n\ti+=1\nend\nputs ans\n\n\n"}, {"source_code": "gets\nca, c = [0] * 5, 0\nSTDIN.read.split.each {|i| ca[i.to_i] += 1}\n\nc += ca[4]\nc += ca[2] / 2\n\nca[2] = ca[2].odd? ? 1 : 0\n\nif ca[1] <= ca[3]\n\tc += ca[1]\n\tca[3] -= ca[1]\n\tca[1] = 0\nelse\n\tc += ca[3]\n\tca[1] -= ca[3]\n\tca[3] = 0\nend\n\nif ca[3] > 0\n\tc += ca[3]\nend\n\nif ca[2] > 0\n\tc += 1\n\tca[1] -= 1 if ca[1] >= 2\n\tca[1] -= 1 if ca[1] >= 1\nend\n\nc += (ca[1] / 4.0).ceil\n\nputs c"}, {"source_code": "t = [0] * 5\ngets\ngets.split.map(&:to_i).each{|_| t[_] += 1}\nputs ([0, t[1] - t[3]].max + 2 * t[2] + 3) / 4 + t[3] + t[4]"}, {"source_code": "j = gets.chomp.to_i - 1\ng = gets.chomp.split(\" \").map(&:to_i).sort.reverse\ng.each_with_index do |e, i|\n\tif g[i] == 4\n\t\tnext\n\tend\n\n\tj.downto(i+1) do |b|\n\t\tif g[i] + g[b] > 4\n\t\t\tbreak\n\t\telse\n\t\t\tg[i] += g[b]\n\t\t\tg.delete_at(b)\n\t\t\tj -= 1\n\t\tend\n\tend\nend\nprint g.length"}, {"source_code": "# Solution CF-158-B\n\n\ndef count_cars groups\n\tarr = [0, 0, 0, 0, 0]\n\t\n\tgroups.each do |x|\n\t\tarr[ x.to_i ] += 1\n\tend\n\t\n\tcars = arr[ 4 ]\n\tcars += arr[ 3 ] + arr[ 2 ] / 2\n\t\n\tarr[ 2 ] = arr[ 2 ] % 2\n\tarr[ 1 ] = arr[ 1 ] < arr[ 3 ] ? 0 : arr[ 1 ] - arr[ 3 ] \n\t\n\tcars += arr[ 1 ] / 4\n\tarr[ 1 ] = arr[ 1 ] % 4\n\t# puts \"c--> #{cars} -- #{arr[1]} -- #{arr[2]}\"\n\t\n\tif arr[ 2 ] > 0\n\t\tcars += 1\n\t\tarr[ 1 ] -= 2\n\tend\n\t\n\tcars += 1 if arr[ 1 ] > 0 \n\t\t \n \tcars\nend\n\n\n\n# Input / Output\n\nn = gets.chomp.to_i\ngroups = gets.split\n\nputs count_cars groups\n\n"}, {"source_code": "t=[0]*5;[*$<][1].split.map{|_|t[_.to_i]+=1};_,a,b,c,d=*t\np ([0,a-c].max+2*b+3)/4+c+d\n"}, {"source_code": "n = gets\ns = gets.split(' ')\na1 = s.count(\"1\")\na2 = s.count(\"2\")\na3 = s.count(\"3\")\na4 = s.count(\"4\")\nt = [a1, a3].min\na4 += t\na1 -= t\na3 -= t\na4 += a2 / 2\na4 += a1 / 4\na2 %= 2\na1 %= 4\nans = a4 + a3\nif a2 > 0\n\ta1 -= 2\n\tans += 1\nend\nans += 1 if a1 > 0\np ans"}, {"source_code": "t=[0]*5;[*$<][1].split.map{|_|t[_.to_i]+=1};_,a,b,c,d=*t\np ([0,a-c].max+2*b+3)/4+c+d\n"}, {"source_code": "def solution(arr)\n counts = arr.inject(Hash.new(0)) { |c, v| c[v] += 1; c }\n\n count = counts[4] || 0\n count += counts[3] || 0\n counts[1] -= counts[3] unless counts[1].nil?\n counts.delete(4)\n counts.delete(3)\n counts.delete(1) if counts[1] < 0\n\n count += counts[2] / 2\n\n ost = counts[2] % 2 == 0 ? 0 : 2\n ost += counts[1] || 0\n\n count += ost / 4\n count += 1 unless ost % 4 == 0\n\n puts count\nend\n\n_ = gets.split(' ').map &:to_i\narr = gets.split(' ').map &:to_i\n\nsolution arr\n"}, {"source_code": "def get_number_of_cars(groups)\n size_distribution = groups.inject([0,0,0,0]) do |array, group| \n array[group - 1] += 1\n array\n end\n\n # for 4\n numbers_of_cars = size_distribution[3]\n size_distribution[3] = 0\n\n # for 2\n numbers_of_cars += size_distribution[1] / 2\n size_distribution[1] = size_distribution[1] % 2\n\n # for 2 1 1\n if size_distribution[1] > 0 and size_distribution[0] > 1\n cars_211 = [size_distribution[1], size_distribution[0]/2].min\n numbers_of_cars += cars_211\n size_distribution[1] -= cars_211\n size_distribution[0] -= cars_211*2\n end\n \n # for 3 1\n if size_distribution[2] > 0 and size_distribution[0] > 1\n cars_31 = [size_distribution[2], size_distribution[0]].min\n numbers_of_cars += cars_31\n size_distribution[2] -= cars_31\n size_distribution[0] -= cars_31\n end\n\n # for 1\n numbers_of_cars += size_distribution[0] / 4\n size_distribution[0] = size_distribution[0] % 4\n\n # the worst case is 111 2 and a lot of 3\n if size_distribution[1] > 0\n numbers_of_cars += 1\n size_distribution[1] = 0\n size_distribution[0] -= 2\n end\n\n if size_distribution[2] > 0\n numbers_of_cars += size_distribution[2]\n size_distribution[0] -= size_distribution[2]\n size_distribution[2] = 0\n end\n\n numbers_of_cars += 1 if size_distribution[0] > 0\n size_distribution[1..2].each { |g| numbers_of_cars += g if g > 0 }\n \n numbers_of_cars\nend\n\nnumber_of_groups = gets().split.map(&:to_i)\ngroups = gets().split.map(&:to_i)\n\nputs get_number_of_cars(groups)"}, {"source_code": "limit = gets.to_i\nx = gets.split(\" \").map{|x| x.to_i}\nx =x.sort.reverse\ni=0\nj=limit-1\nans =0\nwhile i<=j\n\tans+=1\n\tfour = 4-x[i]\n\twhile x[j]<=four and j>=i\n\t\tfour-=x[j]\n\t\tj-=1\n\tend\n\ti+=1\nend\nputs ans"}, {"source_code": "n = gets.to_i\ns = gets.split.map(&:to_i)\n\nn4 = s.count{|i| i==4}\nn3 = s.count{|i| i==3}\nn2 = s.count{|i| i==2}\nn1 = s.count{|i| i==1}\n\nret = n4 + n3\nret = ret + n2/2 + n2%2\nrn1 = n1 - (n3 + 2*(n2%2))\nret = ret + (rn1-1)/4 + 1 if rn1 > 0\n\nputs ret"}, {"source_code": "\ndef main()\n\tn=gets.chomp.to_i;\n\ts=gets.chomp.split(\" \",n).map{|i| i.to_i};\n\tans=0;\n\t#count the number of 4's \n\ts.each do |i|\n\t\tif i==4\n\t\t\tans=ans+1\n\t\tend\n\tend\n\t#count three and ones\n\tc3=0;c1=0;\n\ts.each do |i|\n\t\tif i==3\t\n\t\t\tc3=c3+1\n\t\telsif i==1\n\t\t\tc1=c1+1\n\t\tend\n\tend\n\tans=ans+c3;\n#\tputs \"after adding c3\"+ans.to_s;\n\t#cr1= count of 1 removed\n\tif c30\n\t\t\tans=ans+1;\n\t\telsif c2==0 and c1!=0\n\t\t\tif c1%4==0\n\t\t\t\tans=ans+(c1/4);\n\t\t\telse\n\t\t\t\tans=ans+(c1/4)+1;\n\t\t\tend\n\t\telse\n\t\t\tans=ans+1;\n\t\t\tc1=c1-2;\n\t\t\tif c1>0\n\t\t\t\tif c1%4==0 \n\t\t\t\t\tans=ans+(c1/4);\n\t\t\t\telse\n\t\t\t\t\tans=ans+(c1/4)+1;\n\t\t\t\tend\t\n\t\t\tend\n\t\tend\n\t\tputs ans;\n\tend\n\t\nend\n\nmain()\n"}, {"source_code": "a=gets.chomp.to_i\nb=gets.chomp.split(\" \")\nfour=b.count('4')\nthree=b.count('3')\ntwo=b.count('2')\none=b.count('1')\nif (three>= one )\nres= four + three + (two/2.0).ceil\nputs \"#{res}\"\nelsif (three < one && two%2 == 0)\nres= four + two/2 + three+ ((one- three)/4.0).ceil\nputs \"#{res}\"\nelsif (three < one && two%2 == 1)\nif (one-three <= 2)\nres= four + (two/2.0).ceil + three\nputs \"#{res}\"\nend\nif (one-three >2)\nres=four+three+(two/2.0).ceil+ ((one-2-three)/4.0).ceil\nputs \"#{res}\"\nend\nend"}, {"source_code": "#! ruby -Ku\n# -*- coding: UTF-8 -*-\n\ndef div_up(n, m)\n if n % m == 0\n return n / m\n else\n return n / m + 1\n end\nend\n\nbegin\n inputs = open(\"input.txt\")\nrescue\n inputs = STDIN\nend\nnum_group = inputs.gets.split.map(&:to_i)\nnum_each_children = inputs.gets.split.map(&:to_i)\n\n# each bin contain the number of total groups in each number of friend\nbins = Array::new(5, 0)\nnum_each_children.each do |si|\n bins[si] += 1\nend\n\nnum_4_taxi = bins[4]\nnum_1_3_taxi = [bins[1], bins[3]].min\nnum_2_2_taxi = bins[2] / 2\nnum_3_taxi = bins[3] - num_1_3_taxi\nnum_2_1_1_taxi = 0\nif bins[2] % 2 == 1\n num_2_1_1_taxi = 1\nend\nnum_1_1_1_1_taxi = div_up([bins[1] - num_1_3_taxi - num_2_1_1_taxi * 2, 0].max, 4)\n\nnum_taxi = num_4_taxi + num_1_3_taxi + num_2_2_taxi + num_3_taxi + num_2_1_1_taxi + num_1_1_1_1_taxi\n\nputs num_taxi\n"}, {"source_code": "n = Integer( gets.chomp )\n\nnum = gets.chomp.split(\" \")\n\na = b = c = d = 0\n\nfor i in 0...n\n\tif num[i]=='1'\n\t\ta += 1\n\telsif num[i]=='2'\n\t\tb += 1\n\telsif num[i]=='3'\n\t\tc += 1\n\telse\n\t\td += 1\n\tend\nend\n\nwhile c>0\n\n\td += 1\n\tif a>0\n\t\ta -= 1\n\tend\n\n\tc -= 1\n\nend\n\nif b%2==0\n\td += b / 2\n\tb = 0\nelse\n\td += (b-1) / 2\n\tb = 2\nend\n\na += b\n\nd += a / 4\n\nif a%4!=0\n\td += 1\nend\n\nputs d\n"}, {"source_code": "t=[0]*5;[*$<][1].split.map{|_|t[_.to_i]+=1};_,a,b,c,d=*t\np ([0,a-c].max+2*b+3)/4+c+d"}, {"source_code": "t=[0]*5;[*$<][1].split.map{|_|t[_.to_i]+=1};_,a,b,c,d=*t\np ([0,a-c].max+2*b+3)/4+c+d"}, {"source_code": "gets\na = gets.split(/\\s+/).map{ |i| i.to_i }.inject(Array.new(5, 0)){ |a, i| a[i] += 1; a }\n\nres = a[4]\n\nres += a[3]\na[1] -= a[3];\n\ntwo = (a[2]+1)/2\nres += two\na[1] -= 2 if(a[2] % 2 == 1)\n\nres += (a[1]+3)/4 if(a[1] > 0)\n\nputs res\n"}, {"source_code": "n=gets.chomp\n\ntaxi=gets.chomp\ncount4=0\ncount3=0\ncount2=0\ncount1=0\na=[]\ntaxi.split(\" \").each do |i|\n if i.to_i==1 \n count1+=1\n end\n if i.to_i==2 \n count2+=1\n end\n if i.to_i==3 \n count3+=1\n end\n if i.to_i==4\n count4+=1\n end\nend\ncounttaxi=0\ncounttaxi += count4\ncounttaxi += count3\ncount1 = count1-count3\ncounttaxi += count2/2\n\nif count2%2==1\n counttaxi += 1\n count1 -= 2\nend\n\nif count1>0\n if count1 % 4 ==0\n counttaxi += count1/4 \n else\n counttaxi += count1/4 +1\n\n end\nend\n\np counttaxi\n\n"}, {"source_code": "t = [0] * 5\ngets\ngets.split.map(&:to_i).each{|_| t[_] += 1}\nputs ([0, t[1] - t[3]].max + 2 * t[2] + 3) / 4 + t[3] + t[4]"}, {"source_code": "n = gets.to_i\ns = gets.split.map &:to_i\n\ns = s.sort.reverse\n\ni = 0\nj = s.size - 1\nwhile i < j\n if s[i] + s[j] > 4\n i += 1\n else\n j -= 1\n s[i] += s[j]\n end\nend\n\nputs i+1"}, {"source_code": "t=[0]*5;[*$<][1].split.map{|_|t[_.to_i]+=1};_,a,b,c,d=*t\np ([0,a-c].max+2*b+3)/4+c+d\n"}, {"source_code": "t=[0]*5;[*$<][1].split.map{|_|t[_.to_i]+=1};_,a,b,c,d=*t\np ([0,a-c].max+2*b+3)/4+c+d\n"}, {"source_code": "n = gets.to_i\n\ngroups = [0,0,0,0,0]\ncars = 0\n\ngets.split.map(&:to_i).each do |c|\n groups[c] += 1\nend\n\ncars += groups[4]\n\ncars += groups[3]\ngroups[1] -= groups[3]\n\ncars += groups[2] / 2\n\nif groups[2].odd?\n cars += 1\n groups[1] -= 2\nend\n\ncars += (groups[1].to_f / 4).ceil if groups[1] > 0\n\nputs cars"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\n\nx = a.count(1)\ny = a.count(2)\nz = a.count(3)\nw = a.count(4)\n\nputs ([0,x-z].max+2*y+3)/4+z+w"}, {"source_code": "a=[0]*5\ngets\ngets.split.map {|x| a[x.to_i]+=1}\na[1]=[a[1]-a[3],0].max\nputs a[4]+a[3]+a[2]/2-(-(a[1]+a[2]%2*2))/4"}, {"source_code": "gets\na = gets.split.map{|e|e.to_i}\n\n\nn1 = a.count(1)\nn2 = a.count(2)\nn3 = a.count(3)\nn4 = a.count(4)\n\ntotal = n4\n\nn1 -= n3\nn1 = 0 if n1 < 0\n\ntotal += n3\n\ntotal += n2/2\n\nif ( n2 % 2 == 1 )\n n1 -= 2\n n1 = 0 if n1 < 0\n total += 1\nend\n\nif( n1 > 0 )\n total += 1+ (n1-1)/4\nend\n\nputs total\n\n\n"}, {"source_code": "NOT=[0]*5;[*$<][1].split.map{|_|NOT[_.to_i]+=1};_,a,b,c,d=*NOT\np ([0,a-c].max+2*b+3)/4+c+d"}, {"source_code": "# n = Jumlah group\n# s[i] = Jumlah teman anggota group (1..4)\n\nn = gets.chomp\ns = gets.chomp.split(' ').map(&:to_i)\n\ntotal = s.select {|x| x == 4}.length\n\npanjangSatu = s.select {|x| x == 1}.length\npanjangDua = s.select {|x| x == 2}.length\npanjangTiga = s.select {|x| x == 3}.length\n\ntotal += panjangTiga\n\nsisa1 = panjangSatu - panjangTiga\n\nif sisa1 > 0\n panjangDua += (sisa1 / 2.0).ceil\nend\n\ntotal += (panjangDua/2.0).ceil\n\nputs total\n"}, {"source_code": "t=[0]*5;[*$<][1].split.map{|_|t[_.to_i]+=1};_,a,b,c,d=*t\np ([0,a-c].max+2*b+3)/4+c+d"}, {"source_code": "c=[0]*5\ngets\ngets.split.each{|_|c[_.to_i]+=1}\np ([0,c[1]-c[3]].max+2*c[2]+3)/4+c[3]+c[4]"}, {"source_code": "n = gets.chomp.to_i\nhs = Hash.new 0\nar = gets.split(' ',n).map{|_|_.to_i}\nar.each do |x|\n hs[x]+=1\nend\nans = hs[4]+hs[3]+hs[2]/2+hs[2]%2\nhs[1]-=hs[3]\nhs[1]-=hs[2]%2*2\nans+=(hs[1]-1)/4+1 if hs[1]>0\nputs ans"}, {"source_code": "def solution(arr)\n h = arr.inject(Hash.new(0)) { |c, v| c[v] += 1; c }\n\n f = h[1] || 0\n s = h[2] || 0\n t = h[3] || 0\n fo = h[4] || 0\n\n count = fo\n count += t\n f -= t\n f = 0 if f < 0\n\n count += s / 2\n\n total = (s % 2) * 2 + f\n\n count += total / 4\n count += 1 if total % 4 != 0\n\n puts count\nend\n\n_ = gets.split(' ').map &:to_i\narr = gets.split(' ').map &:to_i\n\nsolution arr\n"}, {"source_code": "# Solution CF-158-B\n\n\ndef count_cars groups\n\tarr = [0, 0, 0, 0, 0]\n\t\n\tgroups.each do |x|\n\t\tarr[ x.to_i ] += 1\n\tend\n\t\n\tcars = arr[ 4 ]\n\tcars += arr[ 3 ] + arr[ 2 ] / 2\n\t\n\tarr[ 2 ] = arr[ 2 ] % 2\n\tarr[ 1 ] = arr[ 1 ] < arr[ 3 ] ? 0 : arr[ 1 ] - arr[ 3 ] \n\t\n\tcars += arr[ 1 ] / 4\n\tarr[ 1 ] = arr[ 1 ] % 4\n\t# puts \"c--> #{cars} -- #{arr[1]} -- #{arr[2]}\"\n\t\n\tif arr[ 2 ] > 0\n\t\tcars += 1\n\t\tarr[ 1 ] -= 2\n\tend\n\t\n\tcars += 1 if arr[ 1 ] > 0 \n\t\t \n \tcars\nend\n\n\n\n# Input / Output\n\nn \t\t= gets.chomp.to_i\ngroups \t= gets.split\n\nputs count_cars groups\n\n"}, {"source_code": "def solution(arr)\n h = Hash.new(0)\n arr.each do |x|\n h[x] += 1\n end\n\n f = h[1] || 0\n s = h[2] || 0\n t = h[3] || 0\n fo = h[4] || 0\n\n count = fo\n count += t\n f -= t\n f = 0 if f < 0\n\n count += s / 2\n\n total = (s % 2) * 2 + f\n\n count += total / 4\n count += 1 if total % 4 != 0\n\n puts count\nend\n\n_ = gets.split(' ').map &:to_i\narr = gets.split(' ').map &:to_i\n\nsolution arr\n"}, {"source_code": "t=[0]*5;[*$<][1].split.map{|_|t[_.to_i]+=1};_,a,b,c,d=*t\np ([0,a-c].max+2*b+3)/4+c+d"}, {"source_code": "t=[0]*5;[*$<][1].split.map{|_|t[_.to_i]+=1};_,a,b,c,d=*t\np ([0,a-c].max+2*b+3)/4+c+d"}, {"source_code": "gets\nhashed = Hash.new(0)\ngroups = gets.chomp.split.map { |item| item.to_i }.each { |item| hashed[item] += 1 }\nif hashed[1] > hashed[3]\n puts hashed[4] + hashed[3] + ((2 * hashed[2] + (hashed[3] - hashed[1]).abs) / 4.0).ceil\nelse \n puts hashed[4] + hashed[3] + ((2 * hashed[2]) / 4.0).ceil\nend\n"}, {"source_code": "a=gets.chomp.to_i\nb=gets.chomp.split(\" \")\nfour=b.count('4')\nthree=b.count('3')\ntwo=b.count('2')\none=b.count('1')\nif (three>= one )\nres= four + three + (two/2.0).ceil\nputs \"#{res}\"\nelsif (three < one && two%2 == 0)\nres= four + two/2 + three+ ((one- three)/4.0).ceil\nputs \"#{res}\"\nelsif (three < one && two%2 == 1)\nif (one-three <= 2)\nres= four + (two/2.0).ceil + three\nputs \"#{res}\"\nend\nif (one-three >2)\nres=four+three+(two/2.0).ceil+ ((one-2-three)/4.0).ceil\nputs \"#{res}\"\nend\nend"}, {"source_code": "n=gets.chomp\n\ntaxi=gets.chomp\ncount4=0\ncount3=0\ncount2=0\ncount1=0\na=[]\ntaxi.split(\" \").each do |i|\n if i.to_i==1 \n count1+=1\n end\n if i.to_i==2 \n count2+=1\n end\n if i.to_i==3 \n count3+=1\n end\n if i.to_i==4\n count4+=1\n end\nend\ncounttaxi=0\ncounttaxi += count4\ncounttaxi += count3\ncount1 = count1-count3\ncounttaxi += count2/2\n\nif count2%2==1\n counttaxi += 1\n count1 -= 2\nend\n\nif count1>0\n if count1 % 4 ==0\n counttaxi += count1/4 \n else\n counttaxi += count1/4 +1\n\n end\nend\n\np counttaxi\n\n"}, {"source_code": "s=[*$<][1].split.map &:to_i\nb=s.count 3\nc=s.count 2\np s.count(4)+b+(c+1)/2+([s.count(1)-b-c*2%4,0].max+3)/4"}, {"source_code": "def run(groups)\n @sit = 0\n @seats = [0,0,0,0,0]\n\n cars = 0\n groups.map(&:to_i).sort.reverse.each do |group|\n car_for_group(group.to_i)\n end\n @seats[0]\nend\n\ndef car_for_group(group)\n sit = can_sit?(group)\n if sit == 0\n @seats[sit] += 1\n @seats[4 - group] += 1 unless (4 - group) == 0\n else\n @seats[sit] -= 1\n @seats[sit - group] += 1 unless (sit - group) == 0\n end \nend\n\ndef can_sit?(group)\n (group..3).each do |s|\n return s if @seats[s] != 0\n end\n return 0\nend\n\n\nSTDIN.read.split(\"\\n\").each_with_index do |line,i|\n if i % 3 == 1\n groups = line.split(\" \")\n puts run(groups)\n end\nend\n\n"}, {"source_code": "t=[0]*5;[*$<][1].split.map{|_|t[_.to_i]+=1};_,a,b,c,d=*t\np ([0,a-c-b*2%4].max+3)/4--b/2+c+d"}, {"source_code": "n = gets.to_i\na = gets.split(' ').map(&:to_i).sort.reverse\n\nresult = 0\n\nuntil a.empty? do\n result += 1\n x = a.shift\n break if a.empty?\n\n while a.last + x <= 4 do\n x += a.pop\n break if a.empty?\n end\nend\n\nputs result\n"}, {"source_code": "sizes = STDIN.read.split(\"\\n\").last.split(\" \").map(&:to_i)\n\ngroups_of_1 = sizes.count { |size| size == 1 }\ngroups_of_2 = sizes.count { |size| size == 2 }\ngroups_of_3 = sizes.count { |size| size == 3 }\ngroups_of_4 = sizes.count { |size| size == 4 }\n\ntaxies = groups_of_4 + groups_of_3 + (groups_of_2 / 2)\n\ngroups_of_2 = groups_of_2 % 2\ngroups_of_1 = [groups_of_1 - groups_of_3, 0].max\n\nif groups_of_2 == 0\n taxies += groups_of_1 / 4\n taxies += 1 if groups_of_1 % 4 != 0\nelse\n taxies += 1\n if 2 < groups_of_1\n groups_of_1 -= 2\n taxies += groups_of_1 / 4\n taxies += 1 if groups_of_1 % 4 != 0\n end\nend\n\nputs taxies\n\n"}, {"source_code": "t=[0]*5;[*$<][1].split.map{|_|t[_.to_i]+=1};_,a,b,c,d=*t\np ([0,a-c].max+2*b+3)/4+c+d\n"}, {"source_code": "n = gets.to_i\ns = gets.chomp.split(\" \").map(&:to_i)\nr = 0\n\ns = s.inject(Hash.new(0)) {|h,i| h[i] += 1; h }\n\nr = s[4]\nt = [s[3],s[1]].min\ns[3]-=t\ns[1]-=t\nr += t + s[3]\nr+=s[2]/2\ns[2]%=2\n\nif s[2] > 0\n r+= 1\n s[1] -= s[2]*2\n s[2] = 0\nend\n\nif s[1]>0\n r+= s[1]/4 + (s[1]%4 != 0 ? 1 : 0)\nend\n\nputs r"}, {"source_code": "def solution(arr)\n counts = arr.inject(Hash.new(0)) { |c, v| c[v] += 1; c }\n\n count = counts[4] || 0\n count += counts[3] || 0\n counts[1] -= counts[3] unless counts[1].nil?\n counts.delete(4)\n counts.delete(3)\n counts.delete(1) if counts[1] < 0\n\n count += counts[2] / 2\n\n ost = counts[2] % 2 == 0 ? 0 : 2\n ost += counts[1] || 0\n\n count += ost / 4\n count += 1 unless ost % 4 == 0\n\n puts count\nend\n\n_ = gets.split(' ').map &:to_i\narr = gets.split(' ').map &:to_i\n\nsolution arr\n"}, {"source_code": "t=[0]*5;[*$<][1].split.map{|_|t[_.to_i]+=1};_,a,b,c,d=*t\np ([0,a-c].max+2*b+3)/4+c+d\n"}, {"source_code": "n = gets.to_i\ns = gets.split.map &:to_i\n\ncount = s.find_all{|e| e == 4}.size\ns = s.reject {|e| e == 4}\n\ns = s.sort.reverse\n\n\ni = 0\nj = s.size - 1\nwhile i < j && s[i] == 3 && s[j] == 1\n s[i] = nil\n s[j] = nil\n i += 1\n j -= 1\n count += 1\nend\n\ns = s.compact\n\nfor i in 0..s.size - 1 do\n next if s[i].nil?\n count += 1\n next if s[i] == 3\n sum = s[i]\n for j in i+1..s.size - 1 do\n next if s[j].nil?\n if sum + s[j] <= 4\n sum +=s[j]\n s[j] = nil\n end\n break if sum == 4\n end\nend\n\nputs count\n\n"}, {"source_code": "j = gets.chomp.to_i - 1\ng = gets.chomp.split(\" \").map(&:to_i).sort.reverse\nt = 0\ng.each_with_index do |e, i|\n\tif g[i] == 4\n\t\tt += 1\n\t\tnext\n\tend\n\n\tj.downto(i+1) do |b|\n\t\tif g[i] + g[b] > 4\n\t\t\tbreak\n\t\telse\n\t\t\tg[i] += g[b]\n\t\t\tg[b] = 0\n\t\t\tj -= 1\n\t\tend\n\tend\nend\ng.reject! { |e| e == 0 }\nprint g.length"}, {"source_code": "# Solution CF-158-B\n\n\ndef count_cars groups\n\tarr = [0, 0, 0, 0, 0]\n\t\n\tgroups.each do |x|\n\t\tarr[ x.to_i ] += 1\n\tend\n\t\n\tcars = arr[ 4 ]\n\tcars += arr[ 3 ] + arr[ 2 ] / 2\n\t\n\tarr[ 2 ] = arr[ 2 ] % 2\n\tarr[ 1 ] = arr[ 1 ] < arr[ 3 ] ? 0 : arr[ 1 ] - arr[ 3 ] \n\t\n\tcars += arr[ 1 ] / 4\n\tarr[ 1 ] = arr[ 1 ] % 4\n\t# puts \"c--> #{cars} -- #{arr[1]} -- #{arr[2]}\"\n\t\n\tif arr[ 2 ] > 0\n\t\tcars += 1\n\t\tarr[ 1 ] -= 2\n\tend\n\t\n\tcars += 1 if arr[ 1 ] > 0 \n\t\t \n \tcars\nend\n\n\n\n# Input / Output\n\nn = gets.chomp.to_i\ngroups = gets.split\n\nputs count_cars groups\n\n"}, {"source_code": "group_num = gets.chomp.to_i\nmember_num = gets.chomp.split()\n\nres=0\nn=0\nthree_num=0\ntwo_num=0\none_num=0\nwhile member_num.length > n do\n case member_num.at(n)\n when \"4\"\n res+=1\n when \"3\"\n three_num+=1\n when \"2\"\n two_num+=1\n else\n one_num+=1\n end\n n+=1\nend\n\nwhile three_num > 0 && one_num >0\n three_num -= 1\n one_num -= 1\n res +=1\nend\n\nif two_num % 2 ==0\n res = res + two_num/2\n two_num =0\nelse\n res = res + two_num/2\n two_num = 1\nend\n\nwhile one_num %2 ==0 && two_num >0 && one_num >0\n res +=1\n two_num = two_num-1\n one_num = one_num-2\nend\n\nwhile one_num > 3\n one_num = one_num -4\n res+=1\nend\n\nwhile one_num > 0 && two_num > 0\n res +=1\n one_num -=1\n two_num -=1\nend\n\nif three_num > 0\n res = res + three_num\nend\nif two_num > 0\n res = res + two_num\nend\nif one_num >0\n res+=1\nend\n\n p res\n"}, {"source_code": "t=[0]*5;[*$<][1].split.map{|_|t[_.to_i]+=1};_,a,b,c,d=*t\np ([0,a-c].max+2*b+3)/4+c+d\n"}, {"source_code": "j = gets.chomp.to_i - 1\ng = gets.chomp.split(\" \").map(&:to_i).sort.reverse\nt = 0\ng.each_with_index do |e, i|\n\tif g[i] == 4\n\t\tt += 1\n\t\tnext\n\tend\n\n\tj.downto(i+1) do |b|\n\t\tif g[i] + g[b] > 4\n\t\t\tbreak\n\t\telse\n\t\t\tg[i] += g[b]\n\t\t\tg[b] = 0\n\t\t\tj -= 1\n\t\tend\n\tend\nend\ng.reject! { |e| e == 0 }\nprint g.length"}, {"source_code": "a=[0]*5\ngets\ngets.split.map {|x| a[x.to_i]+=1}\na[1]=[a[1]-a[3],0].max\nputs a[4]+a[3]+a[2]/2-(-(a[1]+a[2]%2*2))/4"}, {"source_code": "n = gets.to_i\ns = gets.split.map &:to_i\n\ns = s.sort.reverse\n\ni = 0\nj = s.size - 1\nwhile i < j\n if s[i] + s[j] > 4\n i += 1\n else\n j -= 1\n s[i] += s[j]\n end\nend\n\nputs i+1"}, {"source_code": "# n = Jumlah group\n# s[i] = Jumlah teman anggota group (1..4)\n\nn = gets.chomp\ns = gets.chomp.split(' ').map(&:to_i)\n\ntotal = s.select {|x| x == 4}.length\n\npanjangSatu = s.select {|x| x == 1}.length\npanjangDua = s.select {|x| x == 2}.length\npanjangTiga = s.select {|x| x == 3}.length\n\ntotal += panjangTiga\n\nsisa1 = panjangSatu - panjangTiga\n\nif sisa1 > 0\n panjangDua += (sisa1 / 2.0).ceil\nend\n\ntotal += (panjangDua/2.0).ceil\n\nputs total\n"}, {"source_code": "s=[*$<][1].split.map &:to_i\nb=s.count 3\nc=s.count 2\np s.count(4)+b+(c+1)/2+([s.count(1)-b-c*2%4,0].max+3)/4"}, {"source_code": "t=[0]*5;[*$<][1].split.map{|_|t[_.to_i]+=1};_,a,b,c,d=*t\np ([0,a-c].max+2*b+3)/4+c+d"}, {"source_code": "gets\nca, c = [0] * 5, 0\nSTDIN.read.split.each {|i| ca[i.to_i] += 1}\n\nc += ca[4]\nc += ca[2] / 2\n\nca[2] = ca[2].odd? ? 1 : 0\n\nif ca[1] <= ca[3]\n\tc += ca[1]\n\tca[3] -= ca[1]\n\tca[1] = 0\nelse\n\tc += ca[3]\n\tca[1] -= ca[3]\n\tca[3] = 0\nend\n\nif ca[3] > 0\n\tc += ca[3]\nend\n\nif ca[2] > 0\n\tc += 1\n\tca[1] -= 1 if ca[1] >= 2\n\tca[1] -= 1 if ca[1] >= 1\nend\n\nc += (ca[1] / 4.0).ceil\n\nputs c"}, {"source_code": "gets\ns = gets.split.map(&:to_i).sort\nc = t = 0\nwhile !s.empty?\n c += 1 if t == 0\n case s.pop\n when 3\n s.shift if s.first == 1\n when 2\n if s.last == 2\n s.pop\n else\n t += 2\n end\n when 1\n t += 1\n if t == 4\n t = 0\n end\n end\nend\nputs c\n"}, {"source_code": "gets\ngroups = gets.split.map(&:to_i)\n\nn4 = groups.count(4)\nn3 = groups.count(3)\nn2 = groups.count(2)\nn1 = groups.count(1)\n\nntaxi = n4 + n3\nn1 = [n1 - n3, 0].max\n\nd1 = n1/2\ns1 = n1%2\n\nn2 += d1\n\ne2 = n2/2\ns2 = n2%2\n\nntaxi += e2\nntaxi += [s1, s2].max\n\nputs ntaxi"}, {"source_code": "n = gets.to_i\na = gets.split(' ').map(&:to_i).sort.reverse\n\nresult = n - a.delete_if { |x| x == 4 }.size\n\nuntil a.empty? do\n result += 1\n x = a.shift\n break if a.empty?\n\n while a.last + x <= 4 do\n x += a.pop\n break if a.empty?\n end\nend\n\nputs result\n"}, {"source_code": "t=[0]*5;[*$<][1].split.map{|_|t[_.to_i]+=1};_,a,b,c,d=*t\np ([0,a-c].max+2*b+3)/4+c+d"}, {"source_code": "t=[0]*5;[*$<][1].split.map{|_|t[_.to_i]+=1};_,a,b,c,d=*t\np ([0,a-c].max+2*b+3)/4+c+d\n"}, {"source_code": "n = gets.to_i\na = gets.split(' ').map(&:to_i).sort.reverse\n\nresult = 0\n\nuntil a.empty? do\n result += 1\n x = a.shift\n break if a.empty?\n\n while a.last + x <= 4 do\n x += a.pop\n break if a.empty?\n end\nend\n\nputs result\n"}, {"source_code": "c=[0]*5\ngets\ngets.split.each{|_|c[_.to_i]+=1}\np ([0,c[1]-c[3]].max+2*c[2]+3)/4+c[3]+c[4]"}, {"source_code": "#!/usr/bin/env ruby -w\n\ngets\ngroups = gets.split(' ').map(&:to_i)\n\nq_groups= [0, 0, 0, 0, 0]\nn_taxis = 0\n\ngroups.each do |e|\n q_groups[e] += 1\nend\n\nn_taxis += q_groups[4]\n\nn_taxis += q_groups[3]\nq_groups[1] = [0, q_groups[1] - q_groups[3]].max\n\nn_taxis += q_groups[2] / 2\nq_groups[2] %= 2\n\nif q_groups[2] > 0\n n_taxis += q_groups[2]\n q_groups[1] = [0, q_groups[1] - 2].max\nend\n\nn_taxis += q_groups[1] / 4 + (q_groups[1] % 4 > 0 ? 1 : 0)\n\nprint n_taxis\n"}, {"source_code": "n = gets.strip.to_i\ncount = 0\narr = gets.strip.split(\" \").map(&:to_i)\nfour = arr.count{|elem| elem == 4}\nthree = arr.count{|elem| elem == 3}\ntwo = arr.count{|elem| elem == 2}\none = arr.count{|elem| elem == 1}\ncount += four\none = one > three ? one - three : 0\ncount += three\ncount += two / 2\nif two % 2 == 1 then\n one = one > 1 ? one - 2 : one == 1 ? one - 1 : 0\n count += 1\nend\ncount += one / 4\ncount += 1 if one % 4 != 0\nputs count"}, {"source_code": "t=[0]*5;[*$<][1].split.map{|_|t[_.to_i]+=1};_,a,b,c,d=*t\np ([0,a-c].max+2*b+3)/4+c+d\n"}, {"source_code": "#get input\nn = gets.chomp.to_i\n_input = gets.chomp.split(\" \")\ns = Hash.new\n# devide input into 4 groups: \n# those which contain 4 elements, 3 elements, 2 ..\ns[1] = Integer(0)\ns[2] = Integer(0)\ns[3] = Integer(0)\ns[4] = Integer(0)\n0.upto n-1 do |i|\n a = _input[i].to_i\n\ts[a]+=1\nend\n\nresult = Integer(0)\nresult += s[4] # groups of 4\nresult += s[3] # groups of 3\nif s[1] > s[3] then\n s[1] -= s[3]\nelse\n s[1] = 0\nend\nresult += s[2] / 2 # groups of 2\nif s[2] % 2 != 0 then s[1] += 2 end\nresult += s[1] / 4 # groups of 1\nif s[1] % 4 != 0 then result += 1 end\n\nputs result"}, {"source_code": "gets\ngroups = gets.split.map(&:to_i)\n\nn4 = groups.count(4)\nn3 = groups.count(3)\nn2 = groups.count(2)\nn1 = groups.count(1)\n\nntaxi = n4 + n3\nn1 = [n1 - n3, 0].max\n\nd1 = n1/2\ns1 = n1%2\n\nn2 += d1\n\ne2 = n2/2\ns2 = n2%2\n\nntaxi += e2\nntaxi += [s1, s2].max\n\nputs ntaxi"}, {"source_code": "n = gets.chomp;\na = gets.split(\" \").map(&:to_i);\nf = Array.new(5, 0);\nfor i in 0...a.size\n f[a[i]] += 1;\nend\nres = 0\nres += f[4];\nres += f[2] / 2;\nf[2] %= 2;\nmi = [f[1], f[3]].min;\nres += mi;\nf[1] -= mi;\nf[3] -= mi;\nres += f[2];\nf[1] = [f[1] - f[2] * 2, 0].max;\nres += (f[1]/4.0).ceil;\nres += f[3];\nputs res;"}, {"source_code": "n = gets.to_i\na = gets.split(' ').map(&:to_i).sort.reverse\n\nresult = 0\n\nuntil a.empty? do\n result += 1\n x = a.shift\n break if a.empty?\n\n while a.last + x <= 4 do\n x += a.pop\n break if a.empty?\n end\nend\n\nputs result\n"}, {"source_code": "t=[0]*5;[*$<][1].split.map{|_|t[_.to_i]+=1};_,a,b,c,d=*t\np ([0,a-c].max+2*b+3)/4+c+d"}, {"source_code": "t=[0]*5;[*$<][1].split.map{|_|t[_.to_i]+=1};_,a,b,c,d=*t\np ([0,a-c].max+2*b+3)/4+c+d\n"}, {"source_code": "t=[0]*5;[*$<][1].split.map{|_|t[_.to_i]+=1};_,a,b,c,d=*t\np ([0,a-c].max+2*b+3)/4+c+d"}], "negative_code": [{"source_code": "n = Integer(gets)\ninput = gets.chomp\nnums = input.split\ns = Array.new\nfor i in 0...n\n s[i] = Integer(nums[i])\nend\n\ncar1 = 0\ncar2 = 0\ncar3 = 0\ncar4 = 0\nfor i in 0...n\n if s[i] == 1\n car1 += 1\n elsif \n s[i] == 2\n car2 += 1\n elsif \n s[i] == 3\n car3 += 1\n else\n car4 += 1\n end\nend \n\ncar_2 = car2 / 2\ncar_2_still = car2 % 2\n\nif car_2_still == 1\n car_2 += 1\n if car1 == 1\n car1 = car1 - 1\n elsif car1 == 2\n car1 = car1 - 2\n end\nend\n\n\n\nif car3 > car1\n car = car1\n car_0 = car3 - car1\nelsif car3 < car1\n car = car3\n car_0 = car1 - car3\n if car_0 <= 4\n car_0 = 1\n else\n if car_0 % 4 == 0\n car_0 = car_0 / 4\n else\n car_0 = car_0 / 4 + 1\n end\n end\nelse\n car = car3\n car_0 = 0\nend\n\nsum = car4 + car_2 + car + car_0 \nputs sum"}, {"source_code": "gets\na = gets.split(/\\s+/).map{ |i| i.to_i }.inject([0,0,0,0,0]){ |a, i| a[i] += 1; a }\n\nres = a[4]\n\nres += a[3]\na[1] -= a[3];\n\ntwo = (a[2]+1)/2\nres += two\na[1] -= 2 if(a[2] % 2 == 1)\n\nres += a[1] if(a[1] > 0)\n\nputs res\n"}, {"source_code": "groupNum = gets.chomp.to_i\ngroups = gets.chomp.split(' ').map { |i| i.to_i }\n\ngroupHash = {}\n\nfor i in 1..4\n\tgroupHash[i] = 0\nend\n\ngroups.each do |g|\n\tgroupHash[g] += 1\nend\n\nans = groupHash[4]\n\nwhile groupHash[3] > 0 && groupHash[1] > 0\n\tans += 1\n\tgroupHash[3] -= 1\n\tgroupHash[1] -= 1\nend\n\nwhile groupHash[2] > 0 && groupHash[1] > 1\n\tans += 1\n\tgroupHash[2] -= 1\n\tgroupHash[1] -= 2\nend\n\nwhile groupHash[2] > 1\n\tans += 1\n\tgroupHash[2] -= 2\nend\n\nwhile groupHash[1] > 3\n\tans += 1\n\tgroupHash[1] -= 4\nend\n\nans += groupHash[3]\nans += 1 if groupHash[1] > 0\nans += 1 if groupHash[2] > 0\n\nputs ans"}, {"source_code": "n = gets.chomp.to_i\narr = gets.chomp.strip.split.map(&:to_i)\narr.sort!\narr = arr.reverse\n#p arr\ns =0\nc=0\ni=0\ne=n-1\nwhile i <= e do\n s += arr[i] + arr[e]\n #p [s, arr[i], arr[e]]\n if arr.inject(0){|sum,x| sum + x } <= 4\n c = c+1\n break\n end\n if s > 4\n c = c+1\n i=i+1\n s = 0\n elsif s == 4\n i = i+1\n c=c+1\n e=e-1\n s=0\n else\n i=i+1\n e=e-1\n end\n #puts c\nend\nputs c\n"}, {"source_code": "group_num = gets.chomp.to_i\nmember_num = gets.chomp.split()\n\nres=0\nn=0\nthree_num=0\ntwo_num=0\none_num=0\nwhile member_num.length > n do\n case member_num.at(n)\n when \"4\"\n res+=1\n when \"3\"\n three_num+=1\n when \"2\"\n two_num+=1\n else\n one_num+=1\n end\n n+=1\nend\n\nif three_num > one_num\n res = res + one_num\n three_num = three_num - one_num\n one_num = 0\nelsif one_num > three_num\n res = res + three_num\n one_num = one_num - three_num\n three_num = 0\n else\n res = res + one_num\n one_num =0\n three_num = 0\nend\n\nif two_num % 2 ==0\n res = res + two_num/2\n two_num =0\nelse\n res = res + two_num/2\n two_num = 1\nend\n\nif one_num %2 ==0 && two_num >0 && one_num >0\n res +=1\n two_num = two_num-1\n one_num = one_num-2\nend\n\nif one_num %4 ==0\n res = res + one_num/4\n one_num = one_num%4\nend\n\nif three_num > 0\n res = res + three_num + two_num\nend\n\nif one_num >0\n res+=1\nend\n\n p res\n"}, {"source_code": "def main()\n\tn=gets.chomp.to_i;\n\ts=gets.chomp.split(\" \",n).map{|i| i.to_i};\n\ts=s.sort().reverse();\n\tcount=0;\n\tc=0;\n\twhile true\n\t\tsg=s[c];\n\t\tif sg==4\n\t\t\tcount=count+1\n\t\t\ts.delete_at(c);\n\t\telsif sg==3\n\t\t\tind=s.index(1);\n\t\t\tif ind!=nil\n\t\t\t\tcount+=1\n\t\t\t\ts.delete_at(ind);\n\t\t\t\ts.delete_at(c);\n\t\t\telse\n\t\t\t\tcount+=1\n\t\t\t\ts.delete_at(c);\n\t\t\tend\n\t\telsif sg==2\n\t\t\tind=s.index(2);\n\t\t\tif ind!=nil\n\t\t\t\tcount+=1\n\t\t\t\ts.delete_at(ind)\n\t\t\t\ts.delete_at(c);\n\t\t\telse\n\t\t\t\tcount+=1\n\t\t\t\ts.delete_at(c);\n\t\t\tend\n\t\telsif sg==1\n\t\t\tind=s.index(4);\n\t\t\tif ind!=nil\n\t\t\t\tcount+=1\n\t\t\t\ts.delete_at(ind)\n\t\t\t\ts.delete_at(c);\n\t\t\telse\n\t\t\t\tcount+=1\n\t\t\t\ts.delete_at(c);\n\t\t\tend\n\t\tend\n\t\tif s.length()==0\n\t\t\tbreak;\n\t\tend;\t\t\t\n\tend\n\tputs count;\nend\n\nmain();\n"}, {"source_code": "n, *s = gets.split(' ')\nsum = 0\ns.each {|i| sum += i.to_i}\np \"#{(sum + 3) / 4}\""}, {"source_code": "a=gets.to_i\ns=gets.split\ns.map! {|x| x.to_i}\n\nans=0\ns.each {|x| ans+=x}\n\n\nputs (ans/4.0).ceil\n\n"}, {"source_code": "limit = gets.to_i\nsum =0\nans = 0\nx = gets.chomp.split()\nfor i in x\n\ti = i.to_i\n\tsum+=i\nend\nputs sum\nwhile sum!=0\n\tif sum<4\n\t\tans+=1\n\t\tsum/=4\n\telse\n\t\tans+=sum/4\n\t\tsum/=4\n\tend\nend\nputs ans"}, {"source_code": "n = gets.to_i\nm = []\nm = gets.split.map(&:to_i)\ni = 0\nfor j in m do\n i = i + j\nend \n\nresult = (i/4.0).ceil\nputs result"}, {"source_code": "c=[0]*5\ngets\ngets.split.each{|_|c[_.to_i]+=1}\np([0,c[1]-c[3]].max+2*c[2]+3)/4+c[3]+c[4]"}, {"source_code": "groupNum = gets.chomp.to_i\n\ngroup = gets.chomp.split(' ').map {|item| item.to_i}\n\none = group.count(1)\ntwo = group.count(2)\nthree = group.count(3)\nfour = group.count(4)\n\nresult = four + ((two + 1) / 2)\nresult += [one, three].max if three >= one\nresult += three + (one - three + 3) / 4 if one > three\nputs result"}, {"source_code": "n = gets.chomp\ninput = gets.chomp\n\ntaxi = 0\ntres = 0\ndos = 0\nuno = 0\n\ninput.split(\" \").each { |x|\n x = x.to_i\n if x == 1\n uno += 1\n elsif x == 2\n dos += 1\n elsif x == 3\n tres += 1\n else\n taxi += 1\n end\n}\n\nputs taxi\n\ntaxi += tres\n\ntres >= uno ? uno = 0 : uno = (uno - tres)\n\ntaxi += (dos * 2) / 4\nresto_dos = dos.modulo(2)\n\ntaxi += (((resto_dos * 2) + uno).to_f / 4).ceil\n\nputs taxi\n"}, {"source_code": "n = gets.to_i\nm = []\nm = gets.split.map(&:to_i)\ni = 0\nfor j in m do\n i = i + j\nend \n\nresult = (i/4.0).ceil\n\nif m[0] == 3 and m[1] == 3 and m[2] == 3\n puts 3\nelse\n puts result\nend"}, {"source_code": "n = gets.chomp.to_i\narr = gets.chomp.strip.split.map(&:to_i)\narr.sort!\narr = arr.reverse\n#p arr\ns =0\nc=0\ni=0\ne=n-1\nwhile i <= e do\n s = arr[i] + arr[e]\n #p [s, arr[i], arr[e]]\n if s > 4\n c = c+1\n i=i+1\n s = 0\n elsif s == 4\n i = i+1\n c=c+1\n e=e-1\n s=0\n else\n i=i+1\n e=e-1\n end\n #puts c\nend\nputs c\n"}, {"source_code": "n = gets.to_i\ns = gets.split(\" \").map(&:to_i)\n\ngroups_1 = groups_2 = groups_3 = groups_4 = 0\ntaxi_cnt = 0\n\ns.each { |group|\n case group\n when 1\n groups_1 += 1\n when 2\n groups_2 += 1\n when 3\n groups_3 += 1\n when 4\n groups_4 += 1\n end\n}\n\n# 4\ntaxi_cnt += groups_4\ngroups_4 = 0\n\n# 3 + 1\ngroups_3_1 = [groups_3, groups_1].min\ngroups_3 -= groups_3_1\ngroups_1 -= groups_3_1\ntaxi_cnt += groups_3_1\n\n# 2 + 2\ngroups_2_2 = groups_2 / 2\ngroups_2 -= groups_2_2 * 2\ntaxi_cnt += groups_2_2\n\nif groups_3 > 0\n# 3, 2\n taxi_cnt += groups_3 + groups_2 # groups_2 == 0||1\n groups_2 = groups_3 = 0\nelse \n# 1, 2\n taxi_cnt += ((groups_1 + groups_2*2)/4.to_f).ceil\n groups_1 = groups_2 = 0\nend \n\nputs \"#{groups_1}, #{groups_2}, #{groups_3}, #{groups_4}\"\nputs taxi_cnt\n\n\n\n\n\n"}, {"source_code": "group_num = gets.chomp.to_i\nmember_num = gets.chomp.split()\n\nres=0\nn=0\nthree_num=0\ntwo_num=0\none_num=0\nwhile member_num.length > n do\n case member_num.at(n)\n when \"4\"\n res+=1\n when \"3\"\n three_num+=1\n when \"2\"\n two_num+=1\n else\n one_num+=1\n end\n n+=1\nend\n\n\nif three_num > one_num\n res = res + one_num\n three_num = three_num - one_num\n one_num = 0\nelsif one_num > three_num\n res = res + three_num\n one_num = one_num - three_num\n three_num = 0\nend\n\ntwo_num = res + two_num/2\nif two_num%2==0\n two_num = 0\nelse\n two_num =1\nend\n\nif one_num %2 ==0 && two_num >0\n res +=1\n two_num = two_num-1\n one_num = one_num-2\nend\n\nif one_num %4 ==0\n res = res + one_num/4\n one_num = one_num%4\nend\n\nif three_num > 0\n res = res + three_num + two_num\nend\n\nif one_num >0\n res+=1\nend\n\n p res\n"}, {"source_code": "require 'pp'\n\ndef find_counter(p, map)\n eligible = []\n p.each do |n, needed|\n if map[n].nil?\n eligible = [0]\n break\n end\n eligible.push(map[n] / needed)\n end\n eligible.min\nend\n\ngets\nmap = {}\ngets.chomp(' ').split().map(&:to_i).each do |x|\n if map[x]\n map[x] += 1\n else\n map[x] = 1\n end\nend\n\npatterns = [\n {4 => 1},\n {3 => 1, 1 => 1},\n {2 => 2},\n {2 => 1, 1 => 2},\n {1 => 4},\n]\n\n# pp combination\n# pp map\n\ntaxi_needed = 0\npatterns.each do |p|\n k = find_counter(p, map)\n # decrease\n p.each do |x, needed|\n unless map[x].nil?\n map[x] -= k * needed\n end\n end\n taxi_needed += k\nend\nrest_group = map.values.reject{ |x| x == 0 }.length\nputs taxi_needed + rest_group\n"}, {"source_code": "limit = gets.to_i\nx = []\nx = gets.split(\" \").map{|x| x.to_i}\nx =x.reverse\ni=0\nj=limit-1\nans =0\nwhile i<=j\n\tans+=1\n\tfour = 4-x[i].to_i\n\twhile x[j].to_i<=four and j>=i\n\t\tfour-=x[j].to_i\n\t\tj-=1\n\tend\n\ti+=1\nend\nputs ans\n\n\n"}, {"source_code": "number_of_groups = gets().split.map(&:to_i)\ngroups = gets().split.map(&:to_i)\n\nsize_distribution = groups.inject([0,0,0,0]) do |array, group| \n array[group - 1] += 1\n array\nend\n\n# for 4\nnumbers_of_cars = size_distribution[3]\nsize_distribution[3] = 0\n\n# for 2\nnumbers_of_cars += (size_distribution[1] / 2.to_f).floor\nsize_distribution[1] = size_distribution[1] % 2\n\n# for 1\nnumbers_of_cars += (size_distribution[0] / 4.to_f).floor\nsize_distribution[0] = size_distribution[0] % 4\n\n# for 2 and 1 1\nif size_distribution[1] > 0\n size_distribution[0] -= 2 if size_distribution[0] > 1\n size_distribution[1] -= 1\n numbers_of_cars += 1\nend\n\n# for 3 and 1\nif size_distribution[2] > 0\n if size_distribution[0] > 0\n cars_3_1 = [size_distribution[0], size_distribution[2]].min\n size_distribution[0] -= cars_3_1\n size_distribution[2] -= cars_3_1\n numbers_of_cars += cars_3_1\n else\n numbers_of_cars += size_distribution[2]\n end\nend\n\n# for 2 and 1\nif size_distribution[1] > 0\n if size_distribution[0] > 0\n cars_2_1 = [size_distribution[0], size_distribution[1]].min\n size_distribution[0] -= cars_2_1\n size_distribution[1] -= cars_2_1\n numbers_of_cars += cars_2_1\n else\n numbers_of_cars += size_distribution[1]\n end\nend\n\nsize_distribution.each { |g| numbers_of_cars += 1 if g > 0 }\n\nputs numbers_of_cars"}, {"source_code": "# begone\ngets.chomp\n\n# groups = gets.chomp.split(' ').map(&:to_i).sort\n\ngroups = %w(1 2 4 3 3).map(&:to_i).sort.reverse\n\nans = 0\n\ngroups.each_with_index do |gr, idx|\n taxi = gr\n\n reversed = groups.reverse\n reversed.each_with_index do |grr, index|\n break if idx == reversed.size - 1 - index || (taxi += grr) > 4\n\n groups.pop\n end\n\n ans += 1\nend\n\np ans\n"}, {"source_code": "a=gets.to_i\ns=gets.split\ns.map! {|x| x.to_i}\n\nans=0\ns.each {|x| ans+=x}\n\n\nputs (ans/4.0).ceil\n\n"}, {"source_code": "n = gets.to_i\nm = []\nm = gets.split.map(&:to_i)\ni = 0\nfor j in m do\n i = i + j\nend \n\nresult = (i/4.0).floor\n\nif result % 2 != 0\n puts result\nelse\n puts result + 1\nend"}, {"source_code": "groupNum = gets.chomp\ngroup = gets.chomp.split(' ').map {|item| item.to_i}\n\ntotal = 0\n\ngroup.each do |item|\n\ttotal += item\nend\n\nif total % 4 == 0\n\tputs total / 4\nelse\n\tputs (total / 4) + 1\nend"}, {"source_code": "gets\nhashed = Hash.new(0)\ngroups = gets.chomp.split.map { |item| item.to_i }.each { |item| hashed[item] += 1 }\nif hashed[1] != 0\n puts hashed[4] + hashed[3] + ((2 * hashed[2] + (hashed[2] - hashed[1]).abs) / 4.0).ceil\nelse\n puts hashed[4] + hashed[3] + ((2 * hashed[2]) / 4.0).ceil\nend"}, {"source_code": "group_num = gets.chomp.to_i\nmember_num = gets.chomp.split()\n\nres=0\nn=0\nthree_num=0\ntwo_num=0\none_num=0\nwhile member_num.length > n do\n case member_num.at(n)\n when \"4\"\n res+=1\n when \"3\"\n three_num+=1\n when \"2\"\n two_num+=1\n else\n one_num+=1\n end\n n+=1\nend\n\nif three_num > one_num\n res = res + one_num\n three_num = three_num - one_num\n one_num = 0\nelsif one_num > three_num\n res = res + three_num\n one_num = one_num - three_num\n three_num = 0\n else\n res = res + one_num\n one_num =0\n three_num = 0\nend\n\nif two_num % 2 ==0\n res = res + two_num/2\n two_num =0\nelse\n res = res + two_num/2\n two_num = 1\nend\n\nif one_num %2 ==0 && two_num >0 && one_num >0\n res +=1\n two_num = two_num-1\n one_num = one_num-2\nend\n\nif one_num %4 ==0\n res = res + one_num/4\n one_num = one_num%4\nend\n\nif three_num > 0\n res = res + three_num\nend\nif two_num > 0\n res = res + two_num\nend\nif one_num >0\n res+=1\nend\n\n p res\n"}, {"source_code": "n=gets.chomp\n\ntaxi=gets.chomp\ncount4=0\ncount3=0\ncount2=0\ncount1=0\na=[]\ntaxi.split(\" \").each do |i|\n if i.to_i==1 \n count1+=1\n end\n if i.to_i==2 \n count2+=1\n end\n if i.to_i==3 \n count3+=1\n end\n if i.to_i==4\n count4+=1\n end\nend\ncounttaxi=0\ncounttaxi += count4\ncounttaxi += count3\ncount1 = count1-count3\ncounttaxi += count2/2\n\nif count2%2==1\n counttaxi += 1\n count1 -= 2\nend\n\nif count1>0\n counttaxi += count1/4 +1\nend\n\np counttaxi\n\n"}, {"source_code": "gets\nhh = [0, 0, 0, 0, 0]\n\nnums = gets\narray = nums.split(\" \").to_a\n\narray.each {|a| hh[a.to_i] += 1}\n\ncabs = hh[4]\n\nif hh[1] >= hh[3]\n hh[1] -= hh[3]\n cabs += hh[3]\n hh[3] = 0\nelse\n cabs += hh[1]\n hh[3] -= hh[1]\n hh[1] = 0\nend\n\nif hh[2].even?\n cabs += hh[2] / 2\n hh[2] = 0\nelse\n cabs += hh[2] / 2\n hh[2] = 1\n if hh[1] > hh[2]\n\thh[2] = 0\n\tif hh[1] == 1 || hh[1] == 2\n\t cabs += 1\n\t hh[1] = 0\n\telsif hh[1] > 2\n\t cabs += 1\n\t hh[1] -= 2\n\tend\n end\nend\n\nones = hh[1] % 4 == 0 ? 0 : 1 \n\nputs cabs + hh[1] / 4 + ones + hh[2] + hh[3]"}, {"source_code": "number_of_groups = gets().split.map(&:to_i)\ngroups = gets().split.map(&:to_i)\n\nsize_distribution = groups.inject([0,0,0,0]) do |array, group| \n array[group - 1] += 1\n array\nend\n\n# for 4\nnumbers_of_cars = size_distribution[3]\nsize_distribution[3] = 0\n\n# for 2\nnumbers_of_cars += size_distribution[1] / 2\nsize_distribution[1] = size_distribution[1] % 2\n\n# for 1\nnumbers_of_cars += size_distribution[0] / 4\nsize_distribution[0] = size_distribution[0] % 4\n\np size_distribution\n\n# the worst case is 111 2 and a lot of 3\nif size_distribution[1] > 0\n numbers_of_cars += 1\n size_distribution[1] = 0\n size_distribution[0] -= 2\nend\n\nif size_distribution[2] > 0\n numbers_of_cars += size_distribution[2]\n size_distribution[0] -= size_distribution[2]\n size_distribution[2] = 0\nend\n\nsize_distribution.each { |g| numbers_of_cars += 1 if g > 0 }\n\nputs numbers_of_cars"}, {"source_code": "n=gets.chomp\n\ntaxi=gets.chomp\ncount4=0\ncount3=0\ncount2=0\ncount1=0\na=[]\ntaxi.split(\" \").each do |i|\n if i.to_i==1 \n count1+=1\n end\n if i.to_i==2 \n count2+=1\n end\n if i.to_i==3 \n count3+=1\n end\n if i.to_i==4\n count4+=1\n end\nend\ncounttaxi=0\ncounttaxi += count4\ncounttaxi += count3\ncount1 = count1-count3\ncounttaxi += count2/2\n\nif count2%2==1\n counttaxi += 1\n count1 -= 2\nend\n\nif count1>0\n counttaxi += count1/4 +1\nend\n\np counttaxi\n\n"}, {"source_code": "n=gets.chomp\n\ntaxi=gets.chomp\ncount4=0\ncount3=0\ncount2=0\ncount1=0\na=[]\ntaxi.split(\" \").each do |i|\n if i.to_i==1 \n count1+=1\n end\n if i.to_i==2 \n count2+=1\n end\n if i.to_i==3 \n count3+=1\n end\n if i.to_i==4\n count4+=1\n end\nend\ncounttaxi=0\ncounttaxi += count4\ncounttaxi += count3\ncount1 = count1-count3\ncounttaxi += count2/2\n\nif count2%2==1\n counttaxi += 1\n count1 -= 2\nend\n\nif count1>0\n counttaxi += count1/4 +1\nend\n\np counttaxi\n\n"}, {"source_code": "n = gets.to_i\ns = gets.split(\" \").map(&:to_i)\n\ngroups_1 = groups_2 = groups_3 = groups_4 = 0\ntaxi_cnt = 0\n\ns.each { |group|\n case group\n when 1\n groups_1 += 1\n when 2\n groups_2 += 1\n when 3\n groups_3 += 1\n when 4\n groups_4 += 1\n end\n}\n\n# 4\ntaxi_cnt += groups_4\ngroups_4 = 0\n\n# 3 + 1\ngroups_3_1 = [groups_3, groups_1].min\ngroups_3 -= groups_3_1\ngroups_1 -= groups_3_1\ntaxi_cnt += groups_3_1\n\n# 2 + 2\ngroups_2_2 = groups_2 / 2\ngroups_2 -= groups_2_2 * 2\ntaxi_cnt += groups_2_2\n\nif groups_3 > 0\n# 3, 2\n taxi_cnt += groups_3 + groups_2 # groups_2 == 0||1\n groups_2 = groups_3 = 0\nelse \n# 1, 2\n taxi_cnt += ((groups_1 + groups_2*2)/4.to_f).ceil\n groups_1 = groups_2 = 0\nend \n\nputs \"#{groups_1}, #{groups_2}, #{groups_3}, #{groups_4}\"\nputs taxi_cnt\n\n\n\n\n\n"}, {"source_code": "limit = gets.to_i\nx = []\nx = gets.split(\" \").map{|x| x.to_i}\nx =x.reverse\ni=0\nj=limit-1\nans =0\nwhile i<=j\n\tans+=1\n\tfour = 4-x[i].to_i\n\twhile x[j].to_i<=four and j>=i\n\t\tfour-=x[j].to_i\n\t\tj-=1\n\tend\n\ti+=1\nend\nputs ans\n\n\n"}, {"source_code": "# Solution CF-158-B\n\n\ndef count_cars groups\n\tarr = [0, 0, 0, 0, 0]\n\t\n\tgroups.each do |x|\n\t\tarr[ x.to_i ] += 1\n\tend\n\t\n\tcars = arr[ 4 ]\n\t\n\tcars += arr[ 3 ] + arr[ 2 ] / 2 + arr[ 1 ] / 4\n\t\n\tarr[ 1 ] = arr[ 1 ] % 4\n\tarr[ 2 ] = arr[ 2 ] % 2\n\tarr[ 1 ] = arr[ 1 ] <= arr[ 3 ] ? 0 : arr[ 1 ] - arr[ 3 ] \n\n\tcars += 1 unless arr[ 1 ] == 0 && arr[ 2 ] == 0\n\t \n \tcars\nend\n\n\n\n# Input / Output\n\nn \t\t= gets.chomp.to_i\ngroups \t= gets.split\n\nputs count_cars groups\n\n"}, {"source_code": "n = Integer(gets.chomp)\ninput = gets.chomp\nnums = input.split\ns = Array.new\nfor i in 0...n\n s[i] = Integer(nums[i])\nend\n\ncar1 = 0\ncar2 = 0\ncar3 = 0\ncar4 = 0\nfor i in 0...n\n if s[i] == 1\n car1 += 1\n elsif \n s[i] == 2\n car2 += 1\n elsif \n s[i] == 3\n car3 += 1\n else\n car4 += 1\n end\nend \n\ncar_2 = car2 / 2\ncar_2_still = car2 % 2\n\nif car_2_still == 1\n car_2 += 1\n if car1 == 1\n car1 = car1 - 1\n end\nend\n\n\nif car3 > car1\n car = car1\n car_0 = car3 - car1\nelsif car3 < car1\n car = car3\n car_0 = car1 - car3\nelse\n car = car3\n car_0 = 0\nend\n\nsum = car4 + car_2 + car + car_0\nputs sum"}, {"source_code": "def main()\n\tn=gets.chomp.to_i;\n\ts=gets.chomp.split(\" \",n).map{|i| i.to_i};\n\ts=s.sort().reverse();\n\tcount=0;\n\tc=0;\n\twhile true\n\t\tsg=s[c];\n\t\tif sg==4\n\t\t\tcount=count+1\n\t\t\ts.delete_at(c);\n\t\telsif sg==3\n\t\t\tind=s.index(1);\n\t\t\tif ind!=nil\n\t\t\t\tcount+=1\n\t\t\t\ts.delete_at(ind);\n\t\t\t\ts.delete_at(c);\n\t\t\telse\n\t\t\t\tcount+=1\n\t\t\t\ts.delete_at(c);\n\t\t\tend\n\t\telsif sg==2\n\t\t\tind=s.index(2);\n\t\t\tif ind!=nil\n\t\t\t\tcount+=1\n\t\t\t\ts.delete_at(ind)\n\t\t\t\ts.delete_at(c);\n\t\t\telse\n\t\t\t\tcount+=1\n\t\t\t\ts.delete_at(c);\n\t\t\tend\n\t\telsif sg==1\n\t\t\tind=s.index(4);\n\t\t\tif ind!=nil\n\t\t\t\tcount+=1\n\t\t\t\ts.delete_at(ind)\n\t\t\t\ts.delete_at(c);\n\t\t\telse\n\t\t\t\tcount+=1\n\t\t\t\ts.delete_at(c);\n\t\t\tend\n\t\tend\n\t\tif s.length()==0\n\t\t\tbreak;\n\t\tend;\t\t\t\n\tend\n\tputs count;\nend\n\nmain();\n"}, {"source_code": "n, *s = gets.split(' ')\nsum = 0\ns.each {|i| sum += i.to_i}\np \"#{(sum + 3) / 4}\""}, {"source_code": "groupNum = gets.chomp.to_i\n\ngroup = gets.chomp.split(' ').map {|item| item.to_i}\n\none = group.count(1)\ntwo = group.count(2)\nthree = group.count(3)\nfour = group.count(4)\n\nresult = four + ((two + 1) / 2).floor\nresult += [one, three].max if three >= one\nresult += three + (one - three + 3) / 4 if one > three\nputs result"}, {"source_code": "group_num = gets.chomp.to_i\nmember_num = gets.chomp.split()\n\nres=0\nn=0\nthree_num=0\ntwo_num=0\none_num=0\nwhile member_num.length > n do\n case member_num.at(n)\n when \"4\"\n res+=1\n when \"3\"\n three_num+=1\n when \"2\"\n two_num+=1\n else\n one_num+=1\n end\n n+=1\nend\n\nif three_num > one_num\n res = res + one_num\n three_num = three_num - one_num\n one_num = 0\nelsif one_num > three_num\n res = res + three_num\n one_num = one_num - three_num\n three_num = 0\n else\n res = res + one_num\n one_num =0\n three_num = 0\nend\n\nif two_num % 2 ==0\n res = res + two_num/2\n two_num =0\nelse\n res = res + two_num/2\n two_num = 1\nend\n\nif one_num %2 ==0 && two_num >0 && one_num >0\n res +=1\n two_num = two_num-1\n one_num = one_num-2\nend\n\nif one_num %4 ==0\n res = res + one_num/4\n one_num = one_num%4\nend\n\nif one_num > 0 && two_num >0\n res +=1\nend\n\nif three_num > 0\n res = res + three_num\nend\nif two_num > 0\n res = res + two_num\nend\nif one_num >0\n res+=1\nend\n\n p res\n"}, {"source_code": "group_num = gets.chomp.to_i\nmember_num = gets.chomp.split()\n\nres=0\nn=0\nthree_num=0\ntwo_num=0\none_num=0\nwhile member_num.length > n do\n case member_num.at(n)\n when \"4\"\n res+=1\n when \"3\"\n three_num+=1\n when \"2\"\n two_num+=1\n else\n one_num+=1\n end\n n+=1\nend\n\nif three_num > one_num\n res = res + one_num\n three_num = three_num - one_num\n one_num = 0\nelsif one_num > three_num\n res = res + three_num\n one_num = one_num - three_num\n three_num = 0\n else\n res = res + one_num\n one_num =0\n three_num = 0\nend\n\nif two_num % 2 ==0\n res = res + two_num/2\n two_num =0\nelse\n res = res + two_num/2\n two_num = 1\nend\n\nif one_num %2 ==0 && two_num >0 && one_num >0\n res +=1\n two_num = two_num-1\n one_num = one_num-2\nend\n\nif one_num %4 ==0\n res = res + one_num/4\n one_num = one_num%4\nend\n\nif one_num > 0 && two_num >0\n res +=1\nend\n\nif three_num > 0\n res = res + three_num\nend\nif two_num > 0\n res = res + two_num\nend\nif one_num >0\n res+=1\nend\n\n p res\n"}, {"source_code": "group_num = gets.chomp.to_i\nmember_num = gets.chomp.split()\n\nres=0\nn=0\nthree_num=0\ntwo_num=0\none_num=0\nwhile member_num.length > n do\n case member_num.at(n)\n when \"4\"\n res+=1\n when \"3\"\n three_num+=1\n when \"2\"\n two_num+=1\n else\n one_num+=1\n end\n n+=1\nend\n\nif three_num > one_num\n res = res + one_num\n three_num = three_num - one_num\n one_num = 0\nelsif one_num > three_num\n res = res + three_num\n one_num = one_num - three_num\n three_num = 0\n else\n res = res + one_num\n one_num =0\n three_num = 0\nend\n\n\nif two_num % 2 ==0\n res = res + two_num/2\n two_num =0\nelse\n res = res + two_num/2\n two_num = 1\nend\n\nif one_num %2 ==0 && two_num >0 && one_num >0\n res +=1\n two_num = two_num-1\n one_num = one_num-2\nend\n\nif one_num %4 ==0\n res = res + one_num/4\n one_num = one_num%4\nend\n\nif one_num + two_num > 1\n res +=1\n one_num -=1\n two_num -=1\nend\n\nif three_num > 0\n res = res + three_num\nend\nif two_num > 0\n res = res + two_num\nend\nif one_num >0\n res+=1\nend\n\n p res\n"}, {"source_code": "n = gets.to_i\ni = 0\nx = gets.split.map(&:to_i).sort\n\ncab, count = 0, 0\nwhile i < n\n if cab + x[i] <= 4\n cab += x[i]\n else\n count += 1\n cab = x[i]\n end\n i += 1\nend\n\nputs count+1"}, {"source_code": "groupNum = gets.chomp.to_i\n\ngroup = gets.chomp.split(' ').map {|item| item.to_i}\n\none = group.count(1)\ntwo = group.count(2)\nthree = group.count(3)\nfour = group.count(4)\n\nresult = four + ((two + 1) / 2).floor\nresult += [one, three].max if three >= one\nresult += three + (one - three + 3) / 4 if one > three\nputs result"}, {"source_code": "gets\na = gets.split.map(&:to_i)\nk = a.find_all{ |x| x==4}.size\na.delete 4\n\nthree = [a.find_all { |x| x==3 }.size, a.find_all { |x| x==1 }.size].min\nk += three\nthree.times { a.delete_at(a.index(3)); a.delete_at(a.index(1)) }\nk += a.find_all{ |x| x==3}.size\na.delete 3\n\nc2 = a.find_all{ |x| x==2}.size\nc1 = a.find_all{ |x| x==1}.size\n\nk += c2/2 + c1/4 + (c2%2 + c1%4) / 4 + (c2%2 + c1%4)%4 > 0 ? 1 : 0\nputs k"}, {"source_code": "n = gets.to_i\ns = gets.split(\" \").map(&:to_i)\n\ngroups_1 = groups_2 = groups_3 = groups_4 = 0\ntaxi_cnt = 0\n\ns.each { |group|\n case group\n when 1\n groups_1 += 1\n when 2\n groups_2 += 1\n when 3\n groups_3 += 1\n when 4\n groups_4 += 1\n end\n}\n\n# 4\ntaxi_cnt += groups_4\ngroups_4 = 0\n\n# 3 + 1\ngroups_3_1 = [groups_3, groups_1].min\ngroups_3 -= groups_3_1\ngroups_1 -= groups_3_1\ntaxi_cnt += groups_3_1\n\n# 2 + 2\ngroups_2_2 = groups_2 / 2\ngroups_2 -= groups_2_2 * 2\ntaxi_cnt += groups_2_2\n\nif groups_3 > 0\n# 3, 2\n taxi_cnt += groups_3 + groups_2 # groups_2 == 0||1\n groups_2 = groups_3 = 0\nelse \n# 1, 2\n taxi_cnt += ((groups_1 + groups_2)/4.to_f).ceil\n groups_1 = groups_2 = 0\nend \n\n#puts \"#{groups_1}, #{groups_2}, #{groups_3}, #{groups_4}\"\nputs taxi_cnt\n\n\n\n\n\n"}, {"source_code": "a = gets().split(' ').map{|x| Integer(x)}\nb = gets().split(' ').map{|x| Integer(x)}\nc = [0,0,0,0,0]\n(1..4).each {|num| c[num] = (b.select {|x| x == num}).size}\nres = c[4] + c[3] + c[2]/2 + (\nif c[2] % 2 == 1 then\n if c[1] - c[3]-2 > 0 then\n 1 + (c[1] % 4 == 0 ? c[1]/4 : c[1]/4 + 1)\n else\n 1\n end\nelse\n if c[1] - c[3] > 0 then\n c[1] % 4 == 0 ? c[1]/4 : c[1]/4 + 1\n else\n 0\n end\nend )\nprint res\n"}, {"source_code": "n = gets.to_i\nm = []\nm = gets.split.map(&:to_i)\ni = 0\nfor j in m do\n i = i + j\nend \n\nresult = (i/4.0).ceil\n \nif i == 8\n puts 3\nelse\n puts result\nend"}, {"source_code": "group_num = gets.chomp.to_i\nmember_num = gets.chomp.split()\n\nres=0\nn=0\nthree_num=0\ntwo_num=0\none_num=0\nwhile member_num.length > n do\n case member_num.at(n)\n when \"4\"\n res+=1\n when \"3\"\n three_num+=1\n when \"2\"\n two_num+=1\n else\n one_num+=1\n end\n n+=1\nend\n\nwhile three_num > 0 && one_num >0\n three_num -= 1\n one_num -= 1\n res +=1\nend\n\nif two_num % 2 ==0\n res = res + two_num/2\n two_num =0\nelse\n res = res + two_num/2\n two_num = 1\nend\n\nwhile one_num %2 ==0 && two_num >0 && one_num >0\n res +=1\n two_num = two_num-1\n one_num = one_num-2\nend\n\nif one_num %4 ==0\n res = res + one_num/4\n one_num = one_num%4\nend\n\nwhile one_num > 0 && two_num > 0\n res +=1\n one_num -=1\n two_num -=1\nend\n\nif three_num > 0\n res = res + three_num\nend\nif two_num > 0\n res = res + two_num\nend\nif one_num >0\n res+=1\nend\n\n p res\n"}, {"source_code": "require 'pp'\n\ndef find_counter(p, map)\n eligible = []\n p.each do |n, needed|\n if map[n].nil?\n eligible = [0]\n break\n end\n eligible.push(map[n] / needed)\n end\n eligible.min\nend\n\ngets\nmap = {}\ngets.chomp(' ').split().map(&:to_i).each do |x|\n if map[x]\n map[x] += 1\n else\n map[x] = 1\n end\nend\n\npatterns = [\n {4 => 1},\n {3 => 1, 1 => 1},\n {2 => 2},\n {2 => 1, 1 => 2},\n {1 => 4},\n]\n\n# pp combination\n# pp map\n\ntaxi_needed = 0\npatterns.each do |p|\n k = find_counter(p, map)\n # decrease\n p.each do |x, needed|\n unless map[x].nil?\n map[x] -= k * needed\n end\n end\n taxi_needed += k\nend\nrest_group = map.values.reject{ |x| x == 0 }.length\nputs taxi_needed + (rest_group / 4.0).ceil\n"}, {"source_code": "gets\nfreq = Hash.new{0}\ngets.split.map(&:to_i).each{|x| freq[x] += 1}\nfreq[1] = [freq[1] - freq[3], 0].min\np freq[4] + freq[3] + (freq[1] + 2 * freq[2] + 3) / 4\n"}, {"source_code": "n = gets\ns = gets.split(' ')\na1 = s.count(\"1\")\na2 = s.count(\"2\")\na3 = s.count(\"3\")\na4 = s.count(\"4\")\nt = [a1, a3].min\na4 += t\na1 -= t\na3 -= t\na4 += a2 / 2\na4 += a1 / 4\na2 %= 2\na1 %= 4\nans = a4 + a3\nans += 1 if a2\nans += 1 if a1 == 3 || !a2\np ans"}, {"source_code": "\ndef main()\n\tn=gets.chomp.to_i;\n\ts=gets.chomp.split(\" \",n).map{|i| i.to_i};\n\tans=0;\n\t#count the number of 4's \n\ts.each do |i|\n\t\tif i==4\n\t\t\tans=ans+1\n\t\tend\n\tend\n\t#count three and ones\n\tc3=0;c1=0;\n\ts.each do |i|\n\t\tif i==3\t\n\t\t\tc3=c3+1\n\t\telsif i==1\n\t\t\tc1=c1+1\n\t\tend\n\tend\n\tans=ans+c3;\n#\tputs \"after adding c3\"+ans.to_s;\n\t#cr1= count of 1 removed\n\tif c30\n\t\tans=ans+1;\n\telsif c2==0 and c1!=0\n\t\tif c1%4==0\n\t\t\tans=ans+(c1/4);\n\t\telse\n\t\t\tans=ans+(c1/4)+1;\n\t\tend\n\telse\n\t\tans=ans;\t\t\n\tend\n\tputs ans;\t\nend\n\nmain()\n"}, {"source_code": "groupNum = gets.chomp.to_i\ngroups = gets.chomp.split(' ').map { |i| i.to_i }\n\ngroupHash = {}\n\nfor i in 1..4\n\tgroupHash[i] = 0\nend\n\ngroups.each do |g|\n\tgroupHash[g] += 1\nend\n\nans = groupHash[4]\n\nwhile groupHash[3] > 0 && groupHash[1] > 0\n\tans += 1\n\tgroupHash[3] -= 1\n\tgroupHash[1] -= 1\nend\n\nwhile groupHash[2] > 0 && groupHash[1] > 1\n\tans += 1\n\tgroupHash[2] -= 1\n\tgroupHash[1] -= 2\nend\n\nwhile groupHash[2] > 1\n\tans += 1\n\tgroupHash[2] -= 2\nend\n\nwhile groupHash[1] > 3\n\tans += 1\n\tgroupHash[1] -= 4\nend\n\nans += groupHash[3]\nans += 1 if groupHash[1] > 0\nans += 1 if groupHash[2] > 0\n\nputs ans"}, {"source_code": "group_num = gets.chomp.to_i\nmember_num = gets.chomp.split()\n\nres=0\nn=0\nthree_num=0\ntwo_num=0\none_num=0\nwhile member_num.length > n do\n case member_num.at(n)\n when \"4\"\n res+=1\n when \"3\"\n three_num+=1\n when \"2\"\n two_num+=1\n else\n one_num+=1\n end\n n+=1\nend\n\nif three_num > one_num\n res = res + one_num\n three_num = three_num - one_num\n one_num = 0\nelsif one_num > three_num\n res = res + three_num\n one_num = one_num - three_num\n three_num = 0\n else\n res = res + one_num\n one_num =0\n three_num = 0\nend\n\n\nif two_num % 2 ==0\n res = res + two_num/2\n two_num =0\nelse\n res = res + two_num/2\n two_num = 1\nend\n\nif one_num %2 ==0 && two_num >0 && one_num >0\n res +=1\n two_num = two_num-1\n one_num = one_num-2\nend\n\nif one_num %4 ==0\n res = res + one_num/4\n one_num = one_num%4\nend\n\nif one_num + two_num > 1\n res +=1\n one_num -=1\n two_num -=1\nend\n\nif three_num > 0\n res = res + three_num\nend\nif two_num > 0\n res = res + two_num\nend\nif one_num >0\n res+=1\nend\n\n p res\n"}, {"source_code": "n = gets.to_i\ns = gets.split.map &:to_i\n\ncount = s.find_all{|e| e == 4}.size\ns = s.reject {|e| e == 4}\n\ns = s.sort.reverse\n\ni = 0\nj = s.size - 1\nwhile i < j && s[i] == 3 && s[j] == 1\n s[i] = nil\n s[j] = nil\n i += 1\n j -= 1\n count += 1\nend\n\ns = s.compact\n\nfor i in 0..s.size - 1 do\n next if s[i].nil?\n count += 1\n sum = s[i]\n for j in i+1..s.size - 1 do\n next if s[j].nil?\n if sum + s[j] <= 4\n sum +=s[j]\n s[j] = nil\n end\n break if sum == 4 || sum == 3\n end\nend\n\nputs count\n\n"}, {"source_code": "number_of_groups = gets().split.map(&:to_i)\ngroups = gets().split.map(&:to_i)\n\nsize_distribution = groups.inject([0,0,0,0]) do |array, group| \n array[group - 1] += 1\n array\nend\n\n# for 4\nnumbers_of_cars = size_distribution[3]\nsize_distribution[3] = 0\n\n# for 2\nnumbers_of_cars += (size_distribution[1] / 2.to_f).floor\nsize_distribution[1] = size_distribution[1] % 2\n\n# for 1\nnumbers_of_cars += (size_distribution[0] / 4.to_f).floor\nsize_distribution[0] = size_distribution[0] % 4\n\n# for 2 and 1 1\nif size_distribution[1] > 0\n size_distribution[0] -= 2 if size_distribution[0] > 1\n size_distribution[1] -= 1\n numbers_of_cars += 1\nend\n\n# for 3 and 1\nif size_distribution[2] > 0\n if size_distribution[0] > 0\n cars_3_1 = [size_distribution[0], size_distribution[2]].min\n size_distribution[0] -= cars_3_1\n size_distribution[2] -= cars_3_1\n numbers_of_cars += cars_3_1\n else\n numbers_of_cars += size_distribution[2]\n end\nend\n\n# for 2 and 1\nif size_distribution[1] > 0\n if size_distribution[0] > 0\n cars_2_1 = [size_distribution[0], size_distribution[1]].min\n size_distribution[0] -= cars_2_1\n size_distribution[1] -= cars_2_1\n numbers_of_cars += cars_2_1\n else\n numbers_of_cars += size_distribution[1]\n end\nend\n\nsize_distribution.each { |g| numbers_of_cars += 1 if g > 0 }\n\nputs numbers_of_cars"}, {"source_code": "n = gets.to_i\ns = gets.split.map &:to_i\n\ncount = s.find_all{|e| e == 4}.size\ns = s.reject {|e| e == 4}\n\ns = s.sort.reverse\n\ni = 0\nj = s.size - 1\nwhile i < j && s[i] == 3 && s[j] == 1\n s[i] = nil\n s[j] = nil\n i += 1\n j -= 1\n count += 1\nend\n\ns = s.compact\n\nfor i in 0..s.size - 1 do\n count += 1\n next if s[i] == 3\n sum = s[i]\n for j in i+1..s.size - 1 do\n next if s[j].nil?\n if sum + s[j] <= 4\n sum +=s[j]\n s[j] = nil\n end\n break if sum == 4\n end\nend\n\nputs count\n\n"}, {"source_code": "count = Hash.new(0)\n\n_ = gets\ngets.split(' ').each { |x| count[x.to_i] += 1 }\n\nanswer = count[4] + count[3]\ncount[1] = [0, count[1] - count[3]].max\nanswer += (count[2].to_f / 2).ceil\ncount[1] = [0, count[1] - count[2] % 2].max\nanswer += (count[1].to_f / 4).ceil\n\nputs answer\n"}, {"source_code": "limit = gets.to_i\nsum =0\nans = 0\nx = gets.chomp.split()\nfor i in x\n\ti = i.to_i\n\tsum+=i\nend\nwhile sum!=0\n\tif sum%4==0\n\t\tans+=sum/4\n\t\tsum=0\n\telsif sum<4\n\t\tans+=1\n\t\tsum/=4\n\telse\n\t\tans+=sum/4\n\t\tsum/=4\n\tend\nend\nputs ans\n"}, {"source_code": "n, *s = gets.split(' ')\nsum = 0\ns.each {|i| sum += i.to_i}\np (sum + 3) / 4"}, {"source_code": "i = gets.chomp.to_i\nsum = gets.chomp.split(\" \").map(&:to_i).inject(:+)\nif sum == 8 && i == 3\n\tputs 3\nelse\n\tputs (sum % i != 0) ? (sum/4 + 1) : (sum/4)\nend"}, {"source_code": "gets\nca, c = [0] * 5, 0\nSTDIN.read.split.each {|i| ca[i.to_i] += 1}\n\nc += ca[4]\nc += ca[2] / 2\n\nca[2] = ca[2].odd? ? 1 : 0\n\nif ca[1] <= ca[3]\n\tc += ca[1]\n\tca[3] -= ca[1]\n\tca[1] = 0\nelse\n\tc += ca[3]\n\tca[1] -= ca[3]\n\tca[3] = 0\nend\n\nif ca[2] > 0\n\tc += 1\n\tca[1] -= 1 if ca[1] > 1\n\tca[1] -= 1 if ca[1] > 2\nend\n\nc += (ca[1] / 4.0).ceil\n\nputs c"}, {"source_code": "n = gets.chomp.to_i\ng_input = gets.chomp.split(\" \")\n\ngroups = []\nsorting_array = []\ncortege = []\n\ng_input.each do |i|\n\ta = i.to_i\n\tgroups << a\nend\ngroups.sort!\ngroups.reverse!\n\nwhile !(groups.empty?)\n\tc = 0\n\tcounter = 0\n\tif c < 4\n\t\tgroups.each do |g|\n\t\t\tif ((c + g) <= 4)\n\t\t\t\tc = c + g\n\t\t\t\tgroups[counter] = nil\n\t\t\t\tgroups.compact!\n\t\t\t\tcounter = 0\n\t\t\telse\n\t\t\t\tcounter +=1\n\n\t\t\tend\n\t\tend\n\tend\n\n\tif c > 0\n\t\tcortege << c\n\telse \n\t\tbreak\n\tend\nend\n\nputs cortege.length"}, {"source_code": "group_num = gets.chomp.to_i\nmember_num = gets.chomp.split()\n\nres=0\nn=0\nthree_num=0\ntwo_num=0\none_num=0\nwhile member_num.length > n do\n case member_num.at(n)\n when \"4\"\n res+=1\n when \"3\"\n three_num+=1\n when \"2\"\n two_num+=1\n else\n one_num+=1\n end\n n+=1\nend\n\nwhile three_num > 0 && one_num >0\n three_num -= 1\n one_num -= 1\n res +=1\nend\n\nif two_num % 2 ==0\n res = res + two_num/2\n two_num =0\nelse\n res = res + two_num/2\n two_num = 1\nend\n\nwhile one_num %2 ==0 && two_num >0 && one_num >0\n res +=1\n two_num = two_num-1\n one_num = one_num-2\nend\n\nif one_num %4 ==0\n res = res + one_num/4\n one_num = one_num%4\nend\n\nwhile one_num > 0 && two_num > 0\n res +=1\n one_num -=1\n two_num -=1\nend\n\nif three_num > 0\n res = res + three_num\nend\nif two_num > 0\n res = res + two_num\nend\nif one_num >0\n res+=1\nend\n\n p res\n"}, {"source_code": "#! ruby -Ku\n# -*- coding: UTF-8 -*-\n\ndef div_up(n, m)\n if n % m == 0\n return n / m\n else\n return n / m + 1\n end\nend\n\ninputs = STDIN\n# inputs = open(\"input.txt\")\nnum_group = inputs.gets.split.map(&:to_i)\nnum_each_children = inputs.gets.split.map(&:to_i)\n\n# each bin contain the number of total groups in each number of friend\nbins = Array::new(5, 0)\nnum_each_children.each do |si|\n bins[si] += 1\nend\n\nnum_4_taxi = bins[4]\nnum_1_3_taxi = [bins[1], bins[3]].min\nnum_2_2_taxi = bins[2] / 2\nnum_3_taxi = bins[3] - num_1_3_taxi\nnum_2_1_1_taxi = 0\nif bins[2] % 2 == 1\n num_2_1_1_taxi = 1\nend\nnum_1_1_1_1_taxi = div_up([bins[1] - num_1_3_taxi - num_2_1_1_taxi * 2, 0].min, 4)\n\nnum_taxi = num_4_taxi + num_1_3_taxi + num_2_2_taxi + num_3_taxi + num_2_1_1_taxi + num_1_1_1_1_taxi\n\nputs num_taxi\n"}, {"source_code": "n = gets.chomp.to_i\ng_input = gets.chomp.split(\" \")\n\ngroups = [0, 0, 0, 0, 0]\ng_input.each do |i|\n\ta = i.to_i\n\tif a == 4\n\t\tgroups[4] += 1\n\telsif a == 3\n\t\tgroups[3] += 1\n\telsif a == 2\n\t\tgroups[2] += 1\n\telsif a ==1\n\t\tgroups[1] += 1\n\tend\nend\n\ncars = 0\n\nif groups[4]>0\n\tcars += groups[4]\nend\n\nif groups[3]>0\n\tif groups[3]>=groups[1]\n\t\tcars += groups[3]\n\t\tgroups[1] = 0\n\telsif groups[3] < groups[1]\n\t\tcars += groups[3]\n\t\tgroups[1] -= groups[3]\n\tend\nend\n\nif groups[2]>0\n\tif (groups[2]>1) & (groups[2] % 2 == 0)\n\t\tcars += groups[2]/2\n\telsif (groups[2]/2 >0) & (groups[2] % 2 == 1)&(groups[1]>=2)\n\t\tcars = cars + groups[2]/2 + 1\n\t\tgroups[1] = groups[1] -2\n\telsif (groups[2]/2 >0) & (groups[2] % 2 == 1)&(groups[1]==1)\n\tcars = cars + groups[2]/2 + 1\n\tgroups[1] = 0\n\telsif groups[2]==1 & groups[1] >= 2\n\t\tcars +=1\n\t\tgroups[1]-=2\n\telsif groups[2]==1 & groups[1] >=0\n\t\tcars +=1\n\t\tgroups[1] = 0\n\tend\nend\n\nif groups[1] > 0\n\tcars = cars + (groups[1]/4)\n\tif groups[1] % 4 != 0\n\t\tcars += 1\n\tend\nend\nputs cars\n\n\n\n\n"}, {"source_code": "n = gets.chomp\ninput = gets.chomp\n\ntaxi = 0\ntres = 0\ndos = 0\nuno = 0\n\ninput.split(\" \").each { |x|\n x = x.to_i\n if x == 1\n uno += 1\n elsif x == 2\n dos += 1\n elsif x == 3\n tres += 1\n else\n taxi += 1\n end\n}\n\nputs taxi\n\ntaxi += tres\n\ntres >= uno ? uno = 0 : uno = (uno - tres)\n\ntaxi += (dos * 2) / 4\nresto_dos = dos.modulo(2)\n\ntaxi += (((resto_dos * 2) + uno).to_f / 4).ceil\n\nputs taxi\n"}, {"source_code": "n,limit = gets.chomp().split(\" \")\npass = 0\nscore = gets.chomp.split(\" \")\nscore.each { |x|\n if x.to_i >= score[limit.to_i-1].to_i and x.to_i !=0\n pass = pass+1 \n end\n}\nif score[0] == \"0\"\n pass = 0\nend\nputs pass\n"}, {"source_code": "n = gets.to_i\ni = 0\ncount, ones, twos, threes, fours = 0,0,0,0,0\nx = gets.split.each do |x|\n x = x.to_i\n ones += 1 if x == 1\n twos += 1 if x == 2\n threes += 1 if x == 3\n fours += 1 if x == 4\nend\n\n\ncount += fours\n\nif threes > ones\n count += threes\n ones, threes = 0,0\nend\n\nif threes < ones\n count += threes\n threes, ones = 0, ones-threes\nend\n\nif threes == ones\n count += threes\n ones, threes = 0,0\nend\n\ncount += twos/2\n\ntwos %= 2\n\nif twos == 1\n count += 1\n ones -= 2\nend\n\nif ones > 0\n count += ones/4\n count += ones%4\nend\n\nputs count\n"}, {"source_code": "n = gets.chomp.to_i\narr = gets.chomp.strip.split.map(&:to_i)\narr.sort!\narr = arr.reverse\n#p arr\ns =0\nc=0\ni=0\ne=n-1\nwhile i <= e do\n s += arr[i] + arr[e]\n #p [s, arr[i], arr[e]]\n if arr.inject(0){|sum,x| sum + x } <= 4\n c = c+1\n break\n end\n if s > 4\n c = c+1\n i=i+1\n s = 0\n elsif s == 4\n i = i+1\n c=c+1\n e=e-1\n s=0\n else\n i=i+1\n e=e-1\n end\n #puts c\nend\nputs c\n"}, {"source_code": "c=[0]*5\ngets\ngets.split.each{|_|c[_.to_i]+=1}\np([0,c[1]-c[3]].max+2*c[2]+3)/4+c[3]+c[4]"}, {"source_code": "n = gets.chomp.to_i\narr = gets.chomp.strip.split.map(&:to_i)\nco = {1=>0,2=>0,3=>0,4=>0}\narr.each do |a|\n co[a] += 1\nend\n# puts co\nres = co[4] + co[3] + (co[2]/2)\nco[1] -= co[3]\nif co[2] % 2 == 1\n res += 1\n co[1] -= 2\nend\nif co[1] > 0\n if co[1] <= 4\n res += 1\n else\n cou = co[1] / 4\n cou += 1 if co[1] % 4 == 0\n res += cou\n end\nend\n\nputs res"}, {"source_code": "def main()\n\tn=gets.chomp.to_i;\n\ts=gets.chomp.split(\" \",n).map{|i| i.to_i};\n\ts=s.sort().reverse();\n\tcount=0;\n\tc=0;\n\twhile true\n\t\tsg=s[c];\n\t\tif sg==4\n\t\t\tcount=count+1\n\t\t\ts.delete_at(c);\n\t\telsif sg==3\n\t\t\tind=s.index(1);\n\t\t\tif ind!=nil\n\t\t\t\tcount+=1\n\t\t\t\ts.delete_at(ind);\n\t\t\t\ts.delete_at(c);\n\t\t\telse\n\t\t\t\tcount+=1\n\t\t\t\ts.delete_at(c);\n\t\t\tend\n\t\telsif sg==2\n\t\t\tind=s.index(2);\n\t\t\tif ind!=nil\n\t\t\t\tcount+=1\n\t\t\t\ts.delete_at(ind)\n\t\t\t\ts.delete_at(c);\n\t\t\telse\n\t\t\t\tcount+=1\n\t\t\t\ts.delete_at(c);\n\t\t\tend\n\t\telsif sg==1\n\t\t\tind=s.index(4);\n\t\t\tif ind!=nil\n\t\t\t\tcount+=1\n\t\t\t\ts.delete_at(ind)\n\t\t\t\ts.delete_at(c);\n\t\t\telse\n\t\t\t\tcount+=1\n\t\t\t\ts.delete_at(c);\n\t\t\tend\n\t\tend\n\t\tif s.length()==0\n\t\t\tbreak;\n\t\tend;\t\t\t\n\tend\n\tputs count;\nend\n\nmain();\n"}, {"source_code": "gets\nhashed = Hash.new(0)\ngroups = gets.chomp.split.map { |item| item.to_i }.each { |item| hashed[item] += 1 }\nif hashed[1] != 0\n puts hashed[4] + hashed[3] + ((2 * hashed[2] + (hashed[2] - hashed[1]).abs) / 4.0).ceil\nelse\n puts hashed[4] + hashed[3] + ((2 * hashed[2]) / 4.0).ceil\nend"}, {"source_code": "# Solution CF-158-B\n\n\ndef count_cars groups\n\tarr = [0, 0, 0, 0, 0]\n\t\n\tgroups.each do |x|\n\t\tarr[ x.to_i ] += 1\n\tend\n\t\n\tcars = arr[ 4 ]\n\tcars += arr[ 3 ] + arr[ 2 ] / 2\n\t\n\tarr[ 2 ] = arr[ 2 ] % 2\n\tarr[ 1 ] = arr[ 1 ] <= arr[ 3 ] ? 0 : arr[ 1 ] - arr[ 3 ] \n\t\n\tcars += arr[ 1 ] / 4\n\tarr[ 1 ] = arr[ 1 ] % 4\n\t# puts \"c--> #{cars} -- #{arr[1]} -- #{arr[2]}\"\n\t\n\tcars += 1 unless arr[ 1 ] == 0 && arr[ 2 ] == 0\n\t \n \tcars\nend\n\n\n\n# Input / Output\n\nn \t\t= gets.chomp.to_i\ngroups \t= gets.split\n\nputs count_cars groups\n\n"}, {"source_code": "limit = gets.to_i\nsum =0\nans = 0\nwhile limit>0\n\tx = gets.to_i\n\tsum+=x\n\tlimit-=1\nend\nwhile sum!=0\n\tif sum<4\n\t\tans+=1\n\t\tsum/=4\n\telse\n\t\tans+=sum/4\n\t\tsum/=4\n\tend\nend\nputs ans"}, {"source_code": "gets\ns=gets.split\ns.map! {|x| x.to_i}\n\nans=0\ns.each {|x| ans+=x}\n\nputs (ans/4.0).ceil\n\n"}, {"source_code": "gets\nsum = gets.chomp.split(\" \").map(&:to_i).inject(:+)\nputs (sum % 4 != 0) ? (sum/4 + 1) : (sum/4)"}, {"source_code": "count = Hash.new(0)\n\n_ = gets\ngets.split(' ').each { |x| count[x.to_i] += 1 }\n\nanswer = count[4] + count[3]\ncount[1] = [0, count[1] - count[3]].max\nanswer += (count[2].to_f / 2).ceil\ncount[1] = [0, count[1] - count[2] % 2].max\nanswer += (count[1].to_f / 4).ceil\n\nputs answer\n"}, {"source_code": "n = gets.to_i\ni = 0\nx = gets.split.map(&:to_i).sort\n\ncab, count = 0, 0\nwhile i < n\n if cab + x[i] <= 4\n cab += x[i]\n else\n count += 1\n cab = x[i]\n end\n i += 1\nend\n\nputs count+1"}, {"source_code": "n = Integer(gets)\ninput = gets.chomp\nnums = input.split\ns = Array.new\nfor i in 0...n\n s[i] = Integer(nums[i])\nend\n\ncar1 = 0\ncar2 = 0\ncar3 = 0\ncar4 = 0\nfor i in 0...n\n if s[i] == 1\n car1 += 1\n elsif \n s[i] == 2\n car2 += 1\n elsif \n s[i] == 3\n car3 += 1\n else\n car4 += 1\n end\nend \n\ncar_2 = car2 / 2\ncar_2_still = car2 % 2\n\nif car_2_still == 1\n car_2 += 1\n if car1 == 1\n car1 = car1 - 1\n end\nend\n\n\n\nif car3 > car1\n car = car1\n car_0 = car3 - car1\nelsif car3 < car1\n car = car3\n car_0 = car1 - car3\n if car_0 <= 4\n car_0 = 1\n else\n car_0 = car_0 / 4 + 1\n end\nelse\n car = car3\n car_0 = 0\nend\n\nsum = car4 + car_2 + car + car_0 \nputs sum"}, {"source_code": "number_of_groups = gets().split.map(&:to_i)\ngroups = gets().split.map(&:to_i)\n\nsize_distribution = groups.inject([0,0,0,0]) do |array, group| \n array[group - 1] += 1\n array\nend\n\n# for 4\nnumbers_of_cars = size_distribution[3]\nsize_distribution[3] = 0\n\n# for 2\nnumbers_of_cars += (groups[1] / 2.to_f).floor\nsize_distribution[1] = size_distribution[1] % 2\n\n# for 1\nnumbers_of_cars += (size_distribution[0] / 4.to_f).floor\nsize_distribution[0] = size_distribution[0] % 4\n\n# for 2 and 1 1\nif size_distribution[1] > 0\n size_distribution[0] -= 2 if size_distribution[0] > 1\n size_distribution[1] -= 1\n numbers_of_cars += 1\nend\n\n# for 3 and 1\nif size_distribution[2] > 0\n if size_distribution[0] > 0\n cars_3_1 = [size_distribution[0], size_distribution[2]].min\n size_distribution[0] -= cars_3_1\n size_distribution[2] -= cars_3_1\n numbers_of_cars += cars_3_1\n else\n numbers_of_cars += size_distribution[2]\n end\nend\n\nsize_distribution.each { |g| numbers_of_cars += 1 if g > 0 }\n\nputs numbers_of_cars"}, {"source_code": "n = gets.chomp.to_i\ng_input = gets.chomp.split(\" \")\nK = 4\n\nclass Group\n\tattr_accessor :population, :in_car\n\n\tdef initialize(population, in_car)\n\t\t@population = population\n\t\t@in_car = in_car\n\tend\n\n\tdef already_in_car?\n\t\tin_car\n\tend\nend\n\nclass Car\n\tattr_accessor :population\n\tdef initialize(population)\n\t\t@population = population\n\tend\nend\n\ngroups = []\nsorting_array = []\ncortege = []\n\ng_input.each do |i|\n\ta = i.to_i\n\tsorting_array << a\nend\nsorting_array.sort.reverse!\n\nsorting_array.each do |i|\n\ta = Group.new(i.to_i, false)\n\tgroups << a\nend\n\n\n\nwhile !(groups.empty?)\n\n\tc = Car.new(0)\n\tif c.population < K\n\t\tgroups.each do |g|\n\t\t\tif ((c.population + g.population) <= K) & ( !( g.already_in_car? ))\n\t\t\t\tc.population = c.population + g.population\n\t\t\t\tg.in_car = true\n\t\t\tend\n\t\tend\n\tend\n\n\tif c.population > 0\n\t\tcortege << c\n\telse \n\t\tbreak\n\tend\nend\n\nputs cortege.length"}, {"source_code": "a = gets().split(' ').map{|x| Integer(x)}\nb = gets().split(' ').map{|x| Integer(x)}\nc = [0,0,0,0,0]\n(1..4).each {|num| c[num] = (b.select {|x| x == num}).size}\nres = c[4] + c[3] + c[2]/2 + (\nif c[2] % 2 == 1 then\n if c[1] - c[3]-2 > 0 then\n 1 + (c[1] % 4 == 0 ? c[1]/4 : c[1]/4 + 1)\n else\n 1\n end\nelse\n if c[1] - c[3] > 0 then\n c[1] % 4 == 0 ? c[1]/4 : c[1]/4 + 1\n else\n 0\n end\nend )\nprint res\n"}, {"source_code": "t = 0\ngets\ng = gets.chomp.split(\" \").map(&:to_i).sort.reverse\ng.each_with_index do |e, i|\n\tif e == 0 || e == 4\n\t\tnext\n\tend\n\n\t(i+1).upto(g.length-1) do |b|\n\t\tif (e + g[b] > 4)\n\t\t\tnext\n\t\telse\n\t\t\tg[i] += g[b]\n\t\t\tg[b] = 0\n\t\tend\n\tend\nend\nputs g.reject { |e| e == 0 }.length"}, {"source_code": "limit = gets.to_i\nsum =0\nans = 0\nx = gets.chomp.split()\nfor i in x\n\ti = i.to_i\n\tsum+=i\nend\nwhile sum!=0\n\tif sum%4==0\n\t\tans+=sum/4\n\t\tsum=0\n\telsif sum<4\n\t\tans+=1\n\t\tsum/=4\n\telse\n\t\tans+=sum/4\n\t\tsum/=4\n\tend\nend\nif ans>=limit\n\tputs ans\nelse\n\tputs limit\nend"}, {"source_code": "limit = gets.to_i\nsum =0\nans = 0\nx = gets.chomp.split()\nfor i in x\n\ti = i.to_i\n\tsum+=i\nend\nwhile sum!=0\n\tif sum%4==0\n\t\tans+=sum/4\n\t\tsum=0\n\telsif sum<4\n\t\tans+=1\n\t\tsum/=4\n\telse\n\t\tans+=sum/4\n\t\tsum/=4\n\tend\nend\nputs ans"}, {"source_code": "def main()\n\tn=gets.chomp.to_i;\n\ts=gets.chomp.split(\" \",n).map{|i| i.to_i};\n\ts=s.sort().reverse();\n\tcount=0;\n\tc=0;\n\twhile true\n\t\tsg=s[c];\n\t\tif sg==4\n\t\t\tcount=count+1\n\t\t\ts.delete_at(c);\n\t\telsif sg==3\n\t\t\ts.delete_at(c);\n\t\t\tind=s.index(1);\n\t\t\tif ind!=nil\n\t\t\t#\tputs \"d1\"\n\t\t\t\tcount+=1\n\t\t\t\ts.delete_at(ind);\n\n\t\t\telse\n\t\t\t\tcount+=1\n\t\t\tend\n\t\telsif sg==2\n\t\t\ts.delete_at(c);\n\t\t\tind=s.index(2);\n\t\t\tif ind!=nil\n\t\t\t\tcount+=1\n\t\t\t\ts.delete_at(ind)\n\t\t\telse\n\t\t\t\t#check for 2 ones\n\t\t\t\tind=s.index(1);\n\t\t\t\tif ind!=nil\n\t\t\t\t#\tputs \"d1\"\n\t\t\t\t\ts.delete_at(ind);\n\t\t\t\tend\n\t\t\t\tind=s.index(1);\n\t\t\t\tif ind!=nil\n\t\t\t\t#\tputs \"d1\"\n\t\t\t\t\ts.delete_at(ind);\n\t\t\t\tend\n\t\t\t\tcount+=1\n\t\t\t\t\n\t\t\tend\n\t\telsif sg==1\n\t\t\tbreak;\n\t\t\t\n\t\tend\n\t\tif s.length()==0\n\t\t\tbreak;\n\t\tend;\t\n\tend\n\tif s.length()==0\n\t\tputs count;\n\telse\t\n\t\tcount=count+s.length()/4+1;\n\t\tputs count;\n\tend\nend\n\nmain();\n"}, {"source_code": "n = gets.to_i\ns = gets.chomp.split(\" \").map(&:to_i)\nr = 0\n\ns = s.inject(Hash.new(0)) {|h,i| h[i] += 1; h }\n\nr = s[4]\nt = [s[3],s[1]].min\ns[3]-=t\ns[1]-=t\nr += t + s[3]\nr+=s[2]/2\ns[2]%=2\n\nif s[2]+s[1] > 0\n r+= 1\n s[1] -= s[2]*2\n s[2] = 0\nend\n\nputs r"}, {"source_code": "limit = gets.to_i\nsum =0\nans = 0\nx = gets.chomp.split()\nfor i in x\n\ti = i.to_i\n\tsum+=i\nend\nputs sum\nwhile sum!=0\n\tif sum<4\n\t\tans+=1\n\t\tsum/=4\n\telse\n\t\tans+=sum/4\n\t\tsum/=4\n\tend\nend\nputs ans"}, {"source_code": "n = Integer(gets.chomp)\n\ninput = gets.chomp.split\nnums = Array.new\nfor i in 0...n\n nums.push(Integer(input[i]))\nend\n\ncount = Array.new\ncount[0] = count[1] = count[2] = count[3] = count[4] = 0\nfor i in 0...n\n count[nums[i]] = count[nums[i]] + 1\nend\n\nans = count[4] + ( count[2] + 1) / 2\nif count[1] < count[3]\n ans += count[3]\nelse\n ans += count[1]\nend\nputs ans\n "}, {"source_code": "n = gets.to_i\nm = []\nm = gets.split.map(&:to_i)\ni = 0\nfor j in m do\n i = i + j\nend \n\nresult = (i/4.0).ceil\nputs result\n\nif i == 8\n puts 3\nelse \n puts result\nend"}, {"source_code": "# Solution CF-158-B\n\n\ndef count_cars groups\n\tarr = [0, 0, 0, 0, 0]\n\t\n\tgroups.each do |x|\n\t\tarr[ x.to_i ] += 1\n\tend\n\t\n\tcars = arr[ 4 ]\n\t\n\tcars += arr[ 3 ] + arr[ 2 ] / 2 + arr[ 1 ] / 4\n\t\n\tarr[ 1 ] = arr[ 1 ] % 4\n\tarr[ 2 ] = arr[ 2 ] % 2\n\tarr[ 1 ] = arr[ 1 ] <= arr[ 3 ] ? 0 : arr[ 1 ] - arr[ 3 ] \n\n\tcars += 1 unless arr[ 1 ] == 0 && arr[ 2 ] == 0\n\t \n \tcars\nend\n\n\n\n# Input / Output\n\nn \t\t= gets.chomp.to_i\ngroups \t= gets.split\n\nputs count_cars groups\n\n"}, {"source_code": "groupNum = gets.chomp\ngroup = gets.chomp.split(' ').map {|item| item.to_i}\n\ntotal = 0\nremaining = []\n\ngroup.sort! {|x, y| y <=> x}\n\ni = 0\n\nuntil i == group.length\n=begin\n\tprint \"Now array is \"\n\tgroup.each do |item|\n\t\tprint \"#{item} \"\n\tend\n\tprint \"Now item is #{group[i]} \"\n\tprint \"Now i is #{i}\"\n\tputs ''\n=end\n\n\tcase group[i]\n\twhen 4 then total += 1\n\twhen 3\n\t\ttotal += 1\n\t\tfoundIndex = group.index(1)\n\t\tgroup.delete_at(foundIndex) if foundIndex\n\n\t\tfoundIndex = group.index(3)\n\t\tgroup.delete_at(foundIndex) if foundIndex\n\t\ti -= 1\n\twhen 2\n\t\ttotal += 1\n\n\t\tfoundIndex = group.index(2)\n\t\tgroup.delete_at(foundIndex) if foundIndex\n\n\t\tfoundIndex = group.index(2)\n\t\tgroup.delete_at(foundIndex) if foundIndex\n\n\t\ti -= 1\n\twhen 1\n\t\ttotal += 1\n\n\t\tfoundIndex = group.index(1)\n\t\tgroup.delete_at(foundIndex) if foundIndex\n\n\t\tfoundIndex = group.index(3)\n\t\tgroup.delete_at(foundIndex) if foundIndex\n\n\t\ti -= 1\n\tend\n\n\ti += 1\nend\n\nputs total\n"}, {"source_code": "a=gets.to_i\ns=gets.split\ns.map! {|x| x.to_i}\n\nans=0\ns.each {|x| ans+=x}\n\n\nputs (ans/4.0).ceil\n\n"}, {"source_code": "require 'pp'\n\ndef find_counter(p, map)\n eligible = []\n p.each do |n, needed|\n if map[n].nil?\n eligible = [0]\n break\n end\n eligible.push(map[n] / needed)\n end\n eligible.min\nend\n\ngets\nmap = {}\ngets.chomp(' ').split().map(&:to_i).each do |x|\n if map[x]\n map[x] += 1\n else\n map[x] = 1\n end\nend\n\npatterns = [\n {4 => 1},\n {3 => 1, 1 => 1},\n {2 => 2},\n {2 => 1, 1 => 2},\n {1 => 4},\n]\n\n# pp combination\n# pp map\n\ntaxi_needed = 0\npatterns.each do |p|\n k = find_counter(p, map)\n # decrease\n p.each do |x, needed|\n unless map[x].nil?\n map[x] -= k * needed\n end\n end\n taxi_needed += k\nend\nleftover = 0\nmap.each do |k,v|\n leftover += k * v\nend\nputs taxi_needed + (leftover / 4.0).ceil\n"}, {"source_code": "group_num = gets.chomp.to_i\nmember_num = gets.chomp.split()\n\nres=0\nn=0\nthree_num=0\ntwo_num=0\none_num=0\nwhile member_num.length > n do\n case member_num.at(n)\n when \"4\"\n res+=1\n when \"3\"\n three_num+=1\n when \"2\"\n two_num+=1\n else\n one_num+=1\n end\n n+=1\nend\n\nwhile three_num > 0 && one_num >0\n three_num -= 1\n one_num -= 1\n res +=1\nend\n\nif two_num % 2 ==0\n res = res + two_num/2\n two_num =0\nelse\n res = res + two_num/2\n two_num = 1\nend\n\nwhile one_num %2 ==0 && two_num >0 && one_num >0\n res +=1\n two_num = two_num-1\n one_num = one_num-2\nend\n\nif one_num %4 ==0\n res = res + one_num/4\n one_num = one_num%4\nend\n\nwhile one_num > 0 && two_num > 0\n res +=1\n one_num -=1\n two_num -=1\nend\n\nif three_num > 0\n res = res + three_num\nend\nif two_num > 0\n res = res + two_num\nend\nif one_num >0\n res+=1\nend\n\n p res\n"}, {"source_code": "n = Integer(gets)\ninput = gets.chomp\nnums = input.split\ns = Array.new\nfor i in 0...n\n s[i] = Integer(nums[i])\nend\n\ncar1 = 0\ncar2 = 0\ncar3 = 0\ncar4 = 0\nfor i in 0...n\n if s[i] == 1\n car1 += 1\n elsif \n s[i] == 2\n car2 += 1\n elsif \n s[i] == 3\n car3 += 1\n else\n car4 += 1\n end\nend \n\ncar_2 = car2 / 2\ncar_2_still = car2 % 2\n\nif car_2_still == 1\n car_2 += 1\n if car1 == 1\n car1 = car1 - 1\n end\nend\n\nif car3 > car1\n car = car1\n car_0 = car3 - car1\nelsif car3 < car1\n car = car3\n car_0 = car1 - car3\nelse\n car = car3\n car_0 = 0\nend\n\nsum = car4 + car_2 + car + car_0\nputs sum"}, {"source_code": "group_num = gets.chomp.to_i\nmember_num = gets.chomp.split()\n\nres=0\nn=0\nthree_num=0\ntwo_num=0\none_num=0\nwhile member_num.length > n do\n case member_num.at(n)\n when \"4\"\n res+=1\n when \"3\"\n three_num+=1\n when \"2\"\n two_num+=1\n else\n one_num+=1\n end\n n+=1\nend\n\nif three_num > one_num\n res = res + one_num\n three_num = three_num - one_num\n one_num = 0\nelsif one_num > three_num\n res = res + three_num\n one_num = one_num - three_num\n three_num = 0\n else\n res = res + one_num\n one_num =0\n three_num = 0\nend\n\n\nif two_num % 2 ==0\n res = res + two_num/2\n two_num =0\nelse\n res = res + two_num/2\n two_num = 1\nend\n\nif one_num %2 ==0 && two_num >0 && one_num >0\n res +=1\n two_num = two_num-1\n one_num = one_num-2\nend\n\nif one_num %4 ==0\n res = res + one_num/4\n one_num = one_num%4\nend\n\nif one_num + two_num > 1\n res +=1\n one_num -=1\n two_num -=1\nend\n\nif three_num > 0\n res = res + three_num\nend\nif two_num > 0\n res = res + two_num\nend\nif one_num >0\n res+=1\nend\n\n p res\n"}, {"source_code": "# begone\ngets.chomp\n\n# groups = gets.chomp.split(' ').map(&:to_i).sort\n\ngroups = %w(1 2 4 3 3).map(&:to_i).sort.reverse\n\nans = 0\n\ngroups.each_with_index do |gr, idx|\n taxi = gr\n\n reversed = groups.reverse\n reversed.each_with_index do |grr, index|\n break if idx == reversed.size - 1 - index || (taxi += grr) > 4\n\n groups.pop\n end\n\n ans += 1\nend\n\np ans\n"}], "src_uid": "371100da1b044ad500ac4e1c09fa8dcb"} {"nl": {"description": "She is skilled in all kinds of magics, and is keen on inventing new one.—Perfect Memento in Strict SensePatchouli is making a magical talisman. She initially has $$$n$$$ magical tokens. Their magical power can be represented with positive integers $$$a_1, a_2, \\ldots, a_n$$$. Patchouli may perform the following two operations on the tokens. Fusion: Patchouli chooses two tokens, removes them, and creates a new token with magical power equal to the sum of the two chosen tokens. Reduction: Patchouli chooses a token with an even value of magical power $$$x$$$, removes it and creates a new token with magical power equal to $$$\\frac{x}{2}$$$. Tokens are more effective when their magical powers are odd values. Please help Patchouli to find the minimum number of operations she needs to make magical powers of all tokens odd values.", "input_spec": "Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 10^3$$$) — the number of test cases. The description of the test cases follows. For each test case, the first line contains one integer $$$n$$$ ($$$1 \\leq n\\leq 2\\cdot 10^5$$$) — the initial number of tokens. The second line contains $$$n$$$ intergers $$$a_1,a_2,\\ldots,a_n$$$ ($$$1 \\leq a_i \\leq 10^9$$$) — the initial magical power of the $$$n$$$ tokens. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, print a single integer — the minimum number of operations Patchouli needs to make all tokens have an odd value of magical power. It can be shown that under such restrictions the required sequence of operations exists. ", "sample_inputs": ["4\n\n2\n\n1 9\n\n3\n\n1 1 2\n\n3\n\n2 4 8\n\n3\n\n1049600 33792 1280"], "sample_outputs": ["0\n1\n3\n10"], "notes": "NoteTest case 1:$$$a$$$ consists solely of odd numbers initially.Test case 2:Choose the tokens with magical power of $$$1$$$ and $$$2$$$ and perform Fusion. Now $$$a=[1,3]$$$, both are odd numbers.Test case 3:Choose the tokens with magical power of $$$2$$$ and $$$8$$$ and perform Fusion. Now $$$a=[4,10]$$$.Choose the token with magical power of $$$10$$$ and perform Reduction. Now $$$a=[4,5]$$$.Choose the tokens with magical power of $$$4$$$ and $$$5$$$ and perform Fusion. Now $$$a=[9]$$$, and $$$9$$$ is an odd number.It can be shown that you can not make all the magical powers odd numbers in less than $$$3$$$ moves, so the answer is $$$3$$$."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n gets\r\n xs = gets.split.map(&:to_i)\r\n if xs.all?(&:even?)\r\n zeros = xs.map do |x|\r\n count = 0\r\n while x % 2 == 0\r\n x /= 2\r\n count += 1\r\n end\r\n count\r\n end\r\n p zeros.min + xs.size - 1\r\n else\r\n p xs.count(&:even?)\r\n end\r\nend\r\n"}, {"source_code": "gets.to_i.times do\n n=gets.to_i\n arr=gets.split.map(&:to_i)\n min,not2=32,0\n arr.each do |x|\n if x%2==0\n not2+=1\n else min=0 \n end\n cnt=0\n while x%2==0 do\n x/=2\n cnt+=1\n end\n min=[min,cnt].min\n end\n if min==0\n puts not2\n else\n puts not2+min-1\n end\nend"}], "negative_code": [{"source_code": "gets.to_i.times do\r\n gets\r\n xs = gets.split.map(&:to_i)\r\n if xs.all?(&:even?)\r\n count = 0\r\n sum = xs.sum\r\n while sum % 2 == 0\r\n sum /= 2\r\n count += 1\r\n end\r\n p xs.size + count - 1\r\n else\r\n p xs.count(&:even?)\r\n end\r\nend\r\n"}, {"source_code": "gets.to_i.times do\r\n gets\r\n xs = gets.split.map(&:to_i)\r\n even = xs.filter(&:even?)\r\n if even.any?\r\n count = 0\r\n sum = even.sum\r\n while sum % 2 == 0\r\n sum /= 2\r\n count += 1\r\n end\r\n p count + even.size - 1\r\n else\r\n p 0\r\n end\r\nend\r\n"}], "src_uid": "1b9a204dd08d61766391a3b4d2429df2"} {"nl": {"description": "Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is ti and its pages' width is equal to wi. The thickness of each book is either 1 or 2. All books have the same page heights. Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure. Help Shaass to find the minimum total thickness of the vertical books that we can achieve.", "input_spec": "The first line of the input contains an integer n, (1 ≤ n ≤ 100). Each of the next n lines contains two integers ti and wi denoting the thickness and width of the i-th book correspondingly, (1 ≤ ti ≤ 2, 1 ≤ wi ≤ 100).", "output_spec": "On the only line of the output print the minimum total thickness of the vertical books that we can achieve.", "sample_inputs": ["5\n1 12\n1 3\n2 15\n2 5\n2 1", "3\n1 10\n2 1\n2 4"], "sample_outputs": ["5", "3"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\nth1 = []\nth2 = []\nret = 1000\nn.times do\n book = gets.split(' ').map{ |i| i.to_i}\n th1.push book[1] if book[0] == 1\n th2.push book[1] if book[0] == 2\nend\nth1 = th1.sort.insert(0, 0)\nth2 = th2.sort.insert(0, 0)\nfor i in 1...th1.size do th1[i] = th1[i-1] + th1[i] end\nfor i in 1...th2.size do th2[i] = th2[i-1] + th2[i] end\nfor i in 0...th1.size\n for j in 0...th2.size\n if i + 2 * j >= th1[th1.size - i - 1] + th2[th2.size - j - 1]\n ret = [i + 2 * j, ret].min\n end\n end\nend\nputs ret"}, {"source_code": "\nat_exit do\n book_count = get_numbers_from_line[0]\n books = []\n book_count.times do\n t,w = get_numbers_from_line[0..1]\n books << {t: t, w:w}\n end\n\n all_t = get_books_t(books)\n all_w = 0\n books_by_t = {1 => [], 2 => []}.merge books.group_by{|x| x[:t]}\n books_by_t[1].sort_by!{|b| b[:w]}\n books_by_t[2].sort_by!{|b| b[:w]}\n\n i1 = 0\n i2 = 0\n while true\n b1 = books_by_t[1][i1]\n b2 = books_by_t[2][i2]\n\n book = if b1 != nil && b2 != nil\n if !book_may_be_taken?(b2, all_t, all_w)\n i1 += 1\n b1\n elsif !book_may_be_taken?(b1, all_t, all_w)\n i2 += 1\n b2\n elsif b2[:w] <= b1[:w]\n i2 += 1\n b2\n else\n b1_2 = books_by_t[1][i1+1]\n if b1_2 != nil && b1[:w] + b1_2[:w] < b2[:w]\n i1 += 1\n b1\n else\n i2 += 1\n b2\n end\n end\n else\n b1 || b2\n end\n\n break if book.nil?\n break unless book_may_be_taken?(book, all_t, all_w)\n\n all_t -= book[:t]\n all_w += book[:w]\n end\n\n puts all_t\n\n\n #puts try_next_book(books, 0, get_books_t(books), 0)\nend\n\n\ndef book_may_be_taken?(book, all_t, all_w)\n all_w + book[:w] <= all_t - book[:t]\nend\n\ndef try_next_book(books, next_book_index, all_t, top_books_w)\n min_w = (next_book_index...books.length).map{|i| books[i][:w]}.min\n return all_t if min_w > all_t\n\n (next_book_index...books.length).map{|i|\n book = books[i]\n if book[:w] + top_books_w <= all_t - book[:t]\n if book[:w] + top_books_w == all_t - book[:t] || i == books.length - 1\n all_t - book[:t]\n else\n try_next_book(books, i+1, all_t - book[:t], top_books_w + book[:w])\n end\n else\n all_t\n end\n }.min\nend\n\ndef get_books_t(books)\n books.map{|x| x[:t]}.inject(0, :+)\nend\n\n###\n\ndef get_numbers_from_line\n gets.strip.split.map(&:to_i)\nend\n\n\n\n\n"}], "negative_code": [{"source_code": "tt = tw = 0\nbook = []\ngets.to_i.times do\n temp = gets.split.map{|i| i.to_i}\n book << temp\n tt += temp[0]\nend\nbook = book.sort_by{|x| x[1]}\nfor i in 0...book.size\n if tt - book[i][0] >= tw + book[i][1]\n tt -= book[i][0]\n tw += book[i][1]\n else\n puts tt\n break\n end\nend\n"}, {"source_code": "n = gets.to_i\nth1 = []\nth2 = []\nret = 1000\nn.times do\n book = gets.split(' ').map{ |i| i.to_i}\n th1.push book[1] if book[0] == 1\n th2.push book[1] if book[0] == 2\nend\nth1 = th1.sort.insert(0, 0)\nth2 = th2.sort.insert(0, 0)\nfor i in 1...th1.size do th1[i] = th1[i-1] + th1[i] end\nfor i in 1...th2.size do th2[i] = th2[i-1] + th2[i] end\nfor i in 0..th1.size\n for j in 0..th2.size\n if i + 2 * j >= th1[th1.size - i - 1] + th2[th2.size - j - 1]\n ret = [i + 2 * j, ret].min\n end\n end\nend\nputs ret"}, {"source_code": "\nat_exit do\n book_count = get_numbers_from_line[0]\n books = []\n book_count.times do\n t,w = get_numbers_from_line[0..1]\n books << {t: t, w:w}\n end\n\n all_t = get_books_t(books)\n all_w = 0\n books_by_t = {1 => [], 2 => []}.merge books.group_by{|x| x[:t]}\n books_by_t[1].sort_by!{|b| b[:w]}\n books_by_t[2].sort_by!{|b| b[:w]}\n\n i1 = 0\n i2 = 0\n while true\n b1 = books_by_t[1][i1]\n b2 = books_by_t[2][i2]\n\n book = if b1 != nil && b2 != nil\n if !book_may_be_taken?(b2, all_t, all_w)\n b1\n elsif b2[:w] <= b1[:w]\n i2 += 1\n b2\n else\n b1_2 = books_by_t[1][i1+1]\n if b1_2 != nil && b1[:w] + b1_2[:w] < b2[:w]\n i1 += 1\n b1\n else\n if book_may_be_taken?(b2, all_t, all_w)\n i2 += 1\n b2\n else\n i1 += 1\n b1\n end\n end\n end\n else\n b1 || b2\n end\n\n break if book.nil?\n break unless book_may_be_taken?(book, all_t, all_w)\n\n all_t -= book[:t]\n all_w += book[:w]\n end\n\n puts all_t\n\n\n #puts try_next_book(books, 0, get_books_t(books), 0)\nend\n\n\ndef book_may_be_taken?(book, all_t, all_w)\n all_w + book[:w] <= all_t - book[:t]\nend\n\ndef try_next_book(books, next_book_index, all_t, top_books_w)\n min_w = (next_book_index...books.length).map{|i| books[i][:w]}.min\n return all_t if min_w > all_t\n\n (next_book_index...books.length).map{|i|\n book = books[i]\n if book[:w] + top_books_w <= all_t - book[:t]\n if book[:w] + top_books_w == all_t - book[:t] || i == books.length - 1\n all_t - book[:t]\n else\n try_next_book(books, i+1, all_t - book[:t], top_books_w + book[:w])\n end\n else\n all_t\n end\n }.min\nend\n\ndef get_books_t(books)\n books.map{|x| x[:t]}.inject(0, :+)\nend\n\n###\n\ndef get_numbers_from_line\n gets.strip.split.map(&:to_i)\nend\n\n\n\n\n"}, {"source_code": "at_exit do\n # my code\n n = get_numbers_from_line[0]\n books = []\n n.times do\n books << get_numbers_from_line[0..1]\n end\n\n books.sort_by!{|t,w| w*2 + t }\n all_t = books.map{|t,w| t}.inject(0, :+)\n w = 0\n while books.length > 0\n b = books.shift\n w += b[1]\n new_all_t = all_t - b[0]\n break if w > new_all_t\n all_t = new_all_t\n end\n\n puts all_t\n\nend\n\n# User helpers\n\n\n# Helpers\n\ndef get_numbers_from_line\n gets.strip.split.map(&:to_i)\nend\n\ndef trace(vars={})\n puts vars.map{|key, value| \"#{key}: #{value}\"}.join('; ')\nend"}, {"source_code": "at_exit do\n # my code\n n = get_numbers_from_line[0]\n books = []\n n.times do\n books << get_numbers_from_line[0..1]\n end\n\n books.sort_by!{|t,w| w - t }\n all_t = books.map{|t,w| t}.inject(0, :+)\n w = 0\n while books.length > 0\n\n i = 0\n j = nil\n best_all_t = nil\n books.each do |b|\n new_w = w + b[1]\n new_all_t = all_t - b[0]\n if new_w <= new_all_t\n if best_all_t.nil? || new_all_t < best_all_t\n best_all_t = new_all_t\n j = i\n end\n end\n i += 1\n end\n\n break if j.nil?\n\n b = books[j]\n books.delete_at(j)\n w += b[1]\n all_t -= b[0]\n end\n\n puts all_t\n\nend\n\n# User helpers\n\n\n# Helpers\n\ndef get_numbers_from_line\n gets.strip.split.map(&:to_i)\nend\n\ndef trace(vars={})\n puts vars.map{|key, value| \"#{key}: #{value}\"}.join('; ')\nend"}, {"source_code": "at_exit do\n # my code\n n = get_numbers_from_line[0]\n books = []\n n.times do\n books << get_numbers_from_line[0..1]\n end\n\n books.sort_by!{|t,w| w - t }\n all_t = books.map{|t,w| t}.inject(0, :+)\n w = 0\n while books.length > 0\n b = books.shift\n w += b[1]\n new_all_t = all_t - b[0]\n break if w > new_all_t\n all_t = new_all_t\n end\n\n puts all_t\n\nend\n\n# User helpers\n\n\n# Helpers\n\ndef get_numbers_from_line\n gets.strip.split.map(&:to_i)\nend\n\ndef trace(vars={})\n puts vars.map{|key, value| \"#{key}: #{value}\"}.join('; ')\nend"}, {"source_code": "\nat_exit do\n book_count = get_numbers_from_line[0]\n books = []\n book_count.times do\n t,w = get_numbers_from_line[0..1]\n books << {t: t, w:w}\n end\n\n all_t = get_books_t(books)\n all_w = 0\n books_by_t = books.group_by{|x| x[:t]}\n books_by_t[1].sort_by{|b| b[:w]}\n books_by_t[2].sort_by{|b| b[:w]}\n\n i1 = 0\n i2 = 0\n while true\n b1 = books_by_t[1][i1]\n b2 = books_by_t[2][i2]\n\n book = if b1 != nil && b2 != nil\n if b2[:w] <= b1[:w]\n i2 += 1\n b2\n else\n b1_2 = books_by_t[1][i1+1]\n if b1_2 != nil && b1[:w] + b1_2[:w] < b2[:w]\n i1 += 1\n b1\n else\n if all_w + b2[:w] <= all_t - b2[:t]\n i2 += 1\n b2\n else\n i1 += 1\n b1\n end\n end\n end\n else\n b1 || b2\n end\n\n break if book.nil?\n break if all_w + book[:w] > all_t - book[:t]\n\n all_t -= book[:t]\n all_w += book[:w]\n end\n\n puts all_t\n\n\n #puts try_next_book(books, 0, get_books_t(books), 0)\nend\n\ndef try_next_book(books, next_book_index, all_t, top_books_w)\n min_w = (next_book_index...books.length).map{|i| books[i][:w]}.min\n return all_t if min_w > all_t\n\n (next_book_index...books.length).map{|i|\n book = books[i]\n if book[:w] + top_books_w <= all_t - book[:t]\n if book[:w] + top_books_w == all_t - book[:t] || i == books.length - 1\n all_t - book[:t]\n else\n try_next_book(books, i+1, all_t - book[:t], top_books_w + book[:w])\n end\n else\n all_t\n end\n }.min\nend\n\ndef get_books_t(books)\n books.map{|x| x[:t]}.inject(0, :+)\nend\n\n###\n\ndef get_numbers_from_line\n gets.strip.split.map(&:to_i)\nend\n\n\n\n\n"}, {"source_code": "\nat_exit do\n book_count = get_numbers_from_line[0]\n books = []\n book_count.times do\n t,w = get_numbers_from_line[0..1]\n books << {t: t, w:w}\n end\n\n all_t = get_books_t(books)\n all_w = 0\n books_by_t = {1 => [], 2 => []}.merge books.group_by{|x| x[:t]}\n books_by_t[1].sort_by!{|b| b[:w]}\n books_by_t[2].sort_by!{|b| b[:w]}\n\n i1 = 0\n i2 = 0\n while true\n b1 = books_by_t[1][i1]\n b2 = books_by_t[2][i2]\n\n book = if b1 != nil && b2 != nil\n if b2[:w] <= b1[:w]\n i2 += 1\n b2\n else\n b1_2 = books_by_t[1][i1+1]\n if b1_2 != nil && b1[:w] + b1_2[:w] < b2[:w]\n i1 += 1\n b1\n else\n if all_w + b2[:w] <= all_t - b2[:t]\n i2 += 1\n b2\n else\n i1 += 1\n b1\n end\n end\n end\n else\n b1 || b2\n end\n\n break if book.nil?\n break if all_w + book[:w] > all_t - book[:t]\n\n all_t -= book[:t]\n all_w += book[:w]\n end\n\n puts all_t\n\n\n #puts try_next_book(books, 0, get_books_t(books), 0)\nend\n\ndef try_next_book(books, next_book_index, all_t, top_books_w)\n min_w = (next_book_index...books.length).map{|i| books[i][:w]}.min\n return all_t if min_w > all_t\n\n (next_book_index...books.length).map{|i|\n book = books[i]\n if book[:w] + top_books_w <= all_t - book[:t]\n if book[:w] + top_books_w == all_t - book[:t] || i == books.length - 1\n all_t - book[:t]\n else\n try_next_book(books, i+1, all_t - book[:t], top_books_w + book[:w])\n end\n else\n all_t\n end\n }.min\nend\n\ndef get_books_t(books)\n books.map{|x| x[:t]}.inject(0, :+)\nend\n\n###\n\ndef get_numbers_from_line\n gets.strip.split.map(&:to_i)\nend\n\n\n\n\n"}], "src_uid": "f9c16d5c72fe139aa91bc1a9e44d4dc2"} {"nl": {"description": "Mark is asked to take a group photo of $$$2n$$$ people. The $$$i$$$-th person has height $$$h_i$$$ units.To do so, he ordered these people into two rows, the front row and the back row, each consisting of $$$n$$$ people. However, to ensure that everyone is seen properly, the $$$j$$$-th person of the back row must be at least $$$x$$$ units taller than the $$$j$$$-th person of the front row for each $$$j$$$ between $$$1$$$ and $$$n$$$, inclusive.Help Mark determine if this is possible.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1\\leq t\\leq 100$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two positive integers $$$n$$$ and $$$x$$$ ($$$1\\leq n\\leq 100$$$, $$$1\\leq x\\leq 10^3$$$) — the number of people in each row and the minimum difference Mark wants. The second line of each test case contains $$$2n$$$ positive integers $$$h_1,h_2,\\ldots,h_{2n}$$$ ($$$1\\leq h_i\\leq 10^3$$$) — the height of each person in units. Note that the sum of $$$n$$$ over all test cases is not bounded.", "output_spec": "For each test case, print a single line containing \"YES\" if Mark could arrange people satisfying his condition and \"NO\" otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answers).", "sample_inputs": ["3\n\n3 6\n\n1 3 9 10 12 16\n\n3 1\n\n2 5 2 2 2 5\n\n1 2\n\n8 6"], "sample_outputs": ["YES\nNO\nYES"], "notes": "NoteIn the first test case, one possible order is to have the third, fifth, and sixth person on the back row and the second, first, and fourth on the front row. The heights of the people will look like this. Back$$$9$$$$$$12$$$$$$16$$$Front$$$3$$$$$$1$$$$$$10$$$ It works because $$$h_3-h_2 = 9-3 \\geq 6$$$, $$$h_5-h_1 = 12-1\\geq 6$$$, and $$$h_6-h_4 = 16-10\\geq 6$$$. In the second test case, it can be shown there is no way to order people in a way that satisfies the condition.In the third test case, the only way to arrange people to satisfy the condition is to have the first person on the back row and the second person on the front row."}, "positive_code": [{"source_code": "t = gets.to_i\n\n(0...t).each do\n n, x = *gets.chomp.split(\" \").map(&:to_i)\n h = gets.chomp.split(\" \").map(&:to_i)\n\n h.sort!\n\n front = h[...n]\n back = h[n..]\n\n res = \"YES\"\n front.each.with_index do |k, idx|\n if back[idx] - k < x\n res = \"NO\"\n break\n end\n end\n puts res\nend\n"}, {"source_code": "gets.to_i.times {\n\tn,x = gets.split.map(&:to_i)\n\th = gets.split.map(&:to_i).sort\n\tputs h[0..n-1].zip(h[n..]).map { |a,b| b-a>= x}.all? ? \"YES\" : \"NO\" \n}"}], "negative_code": [{"source_code": "t = gets.to_i\n\n(0...t).each do\n n, x = *gets.chomp.split(\" \").map(&:to_i)\n h = gets.chomp.split(\" \").map(&:to_i)\n\n h.sort!\n\n front = h[...n]\n back = h[n..]\n\n res = \"YES\"\n front.each.with_index do |x, idx|\n if x >= back[idx]\n res = \"NO\"\n break\n end\n end\n puts res\nend\n"}], "src_uid": "52769cd7b3b0d63a7259a5d0754c4803"} {"nl": {"description": "Imagine that there is a group of three friends: A, B and С. A owes B 20 rubles and B owes C 20 rubles. The total sum of the debts is 40 rubles. You can see that the debts are not organized in a very optimal manner. Let's rearrange them like that: assume that A owes C 20 rubles and B doesn't owe anything to anybody. The debts still mean the same but the total sum of the debts now equals 20 rubles.This task is a generalisation of a described example. Imagine that your group of friends has n people and you know the debts between the people. Optimize the given debts without changing their meaning. In other words, finally for each friend the difference between the total money he should give and the total money he should take must be the same. Print the minimum sum of all debts in the optimal rearrangement of the debts. See the notes to the test samples to better understand the problem.", "input_spec": "The first line contains two integers n and m (1 ≤ n ≤ 100; 0 ≤ m ≤ 104). The next m lines contain the debts. The i-th line contains three integers ai, bi, ci (1 ≤ ai, bi ≤ n; ai ≠ bi; 1 ≤ ci ≤ 100), which mean that person ai owes person bi ci rubles. Assume that the people are numbered by integers from 1 to n. It is guaranteed that the same pair of people occurs at most once in the input. The input doesn't simultaneously contain pair of people (x, y) and pair of people (y, x).", "output_spec": "Print a single integer — the minimum sum of debts in the optimal rearrangement.", "sample_inputs": ["5 3\n1 2 10\n2 3 1\n2 4 1", "3 0", "4 3\n1 2 1\n2 3 1\n3 1 1"], "sample_outputs": ["10", "0", "0"], "notes": "NoteIn the first sample, you can assume that person number 1 owes 8 rubles to person number 2, 1 ruble to person number 3 and 1 ruble to person number 4. He doesn't owe anybody else anything. In the end, the total debt equals 10.In the second sample, there are no debts.In the third sample, you can annul all the debts."}, "positive_code": [{"source_code": "class Debt\n attr_accessor :users, :debts\n\n def initialize(users)\n @users = users\n @debts = Array.new(users, 0)\n end\n\n\n def add_debt(user_from, user_to, users_debt)\n\n @debts[user_from - 1] -= users_debt\n @debts[user_to - 1] += users_debt\n\n end\n\n def calculate\n return 0 if debts.empty?\n @debts.inject(0){|sum, element| sum += element > 0 ? element : 0}\n end\n\nend\n\n\ndef read_ints\n gets.split(\" \").collect { |e| e.to_i }\nend\n\nusers, debts_count = read_ints\n\n\ndebt = Debt.new(users)\ndebts_count.times do\n user_from, user_to, users_debt = read_ints\n debt.add_debt user_from, user_to, users_debt\n\nend\n\n\nputs debt.calculate"}, {"source_code": "n,m=gets.split.map(&:to_i)\nd=[0]*n\nm.times do\n a,b,c=gets.split.map(&:to_i)\n d[a-1]-=c\n d[b-1]+=c\nend\nputs d.inject(0){|t,x|t+x.abs}/2\n"}], "negative_code": [{"source_code": "n,m=gets.split.map(&:to_i)\nd=[0]*n\nm.times do\n a,b,c=gets.split.map(&:to_i)\n d[a-1]-=c\n d[b-1]+=c\nend\nputs d.inject{|x,a|a+x.abs}/2\n"}], "src_uid": "b53c3e55834db8184d8caf4630aaa573"} {"nl": {"description": "You're given an array $$$a$$$ of $$$n$$$ integers, such that $$$a_1 + a_2 + \\cdots + a_n = 0$$$.In one operation, you can choose two different indices $$$i$$$ and $$$j$$$ ($$$1 \\le i, j \\le n$$$), decrement $$$a_i$$$ by one and increment $$$a_j$$$ by one. If $$$i < j$$$ this operation is free, otherwise it costs one coin.How many coins do you have to spend in order to make all elements equal to $$$0$$$?", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 5000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$)  — the number of elements. The next line contains $$$n$$$ integers $$$a_1, \\ldots, a_n$$$ ($$$-10^9 \\le a_i \\le 10^9$$$). It is given that $$$\\sum_{i=1}^n a_i = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.", "output_spec": "For each test case, print the minimum number of coins we have to spend in order to make all elements equal to $$$0$$$.", "sample_inputs": ["7\n4\n-3 5 -3 1\n2\n1 -1\n4\n-3 2 -3 4\n4\n-1 1 1 -1\n7\n-5 7 -6 -4 17 -13 4\n6\n-1000000000 -1000000000 -1000000000 1000000000 1000000000 1000000000\n1\n0"], "sample_outputs": ["3\n0\n4\n1\n8\n3000000000\n0"], "notes": "NotePossible strategy for the first test case: Do $$$(i=2, j=3)$$$ three times (free), $$$a = [-3, 2, 0, 1]$$$. Do $$$(i=2, j=1)$$$ two times (pay two coins), $$$a = [-1, 0, 0, 1]$$$. Do $$$(i=4, j=1)$$$ one time (pay one coin), $$$a = [0, 0, 0, 0]$$$. "}, "positive_code": [{"source_code": "gets.to_i.times do\n n=gets.to_i\n a=gets.split(\" \").map(&:to_i)\n\n i=0\n j=1\n while i < n && j < n\n if j <= i\n j += 1\n elsif a[i] > 0 && a[j] < 0\n x = [a[i], -a[j]].min\n a[i] -= x\n a[j] += x\n elsif a[j] >= 0\n j += 1\n elsif a[i] <= 0\n i += 1\n end\n end\n\n puts (a.select {|x| x > 0} + [0]).reduce(:+)\n\nend\n"}, {"source_code": "t = gets.to_i\nt.times do\n\tn = gets.to_i\n\ta = gets.split.map &:to_i\n\tmax = 0\n\tsum = 0\n\ta.reverse_each do |x|\n\t\tsum += x\n\t\tmax = [max, sum].max\n\tend\n\tp max\nend"}, {"source_code": "gets.to_i.times{gets\na=c=0\ngets.split.map{|x|c=[a-=x.to_i,c].max}\np c}"}, {"source_code": "gets.to_i.times{gets\na=c=0\ngets.split.map(&:to_i).map{|x|c=[a-=x,c].max}\np c}"}, {"source_code": "gets.to_i.times{gets\na=c=0\nb=gets.split.map(&:to_i).map{|x|c=[a-=x,c].max}\np c}"}, {"source_code": "T = gets.to_i\n\nT.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n\n l = 0\n r = n - 1\n cost = a.select(&:positive?).sum\n stock = 0\n\n a.each do |v|\n if v > 0\n stock += v\n elsif v < 0 && stock > 0\n d = [v.abs, stock].min\n cost -= d\n stock -= d\n end\n end\n\n puts cost\nend\n"}, {"source_code": "gets.to_i.times{gets\na=c=0\ngets.split.map{|x|c=[a-=x.to_i,c].max}\np c}"}], "negative_code": [], "src_uid": "bd0b14e53ade1207022464ebafdf8c9a"} {"nl": {"description": "You are given a ternary string (it is a string which consists only of characters '0', '1' and '2').You can swap any two adjacent (consecutive) characters '0' and '1' (i.e. replace \"01\" with \"10\" or vice versa) or any two adjacent (consecutive) characters '1' and '2' (i.e. replace \"12\" with \"21\" or vice versa).For example, for string \"010210\" we can perform the following moves: \"010210\" $$$\\rightarrow$$$ \"100210\"; \"010210\" $$$\\rightarrow$$$ \"001210\"; \"010210\" $$$\\rightarrow$$$ \"010120\"; \"010210\" $$$\\rightarrow$$$ \"010201\". Note than you cannot swap \"02\" $$$\\rightarrow$$$ \"20\" and vice versa. You cannot perform any other operations with the given string excluding described above.You task is to obtain the minimum possible (lexicographically) string by using these swaps arbitrary number of times (possibly, zero).String $$$a$$$ is lexicographically less than string $$$b$$$ (if strings $$$a$$$ and $$$b$$$ have the same length) if there exists some position $$$i$$$ ($$$1 \\le i \\le |a|$$$, where $$$|s|$$$ is the length of the string $$$s$$$) such that for every $$$j < i$$$ holds $$$a_j = b_j$$$, and $$$a_i < b_i$$$.", "input_spec": "The first line of the input contains the string $$$s$$$ consisting only of characters '0', '1' and '2', its length is between $$$1$$$ and $$$10^5$$$ (inclusive).", "output_spec": "Print a single string — the minimum possible (lexicographically) string you can obtain by using the swaps described above arbitrary number of times (possibly, zero).", "sample_inputs": ["100210", "11222121", "20"], "sample_outputs": ["001120", "11112222", "20"], "notes": null}, "positive_code": [{"source_code": "S = gets.chomp\nans = ''\none = 0\nS.chars do |c|\n if c == '1'\n one += 1\n else\n ans << c\n end\nend\npt = ans.index('2') || ans.length\nans.insert(pt, '1'*one)\nputs ans"}, {"source_code": "str = gets.strip\n\noc = 0\nnewstr = \"\"\n\nstr.each_char do |c|\n if c == \"1\"\n oc += 1\n else\n newstr << c\n end\nend\n\ntp = nil\n0.upto(newstr.size-1).each do |i|\n if newstr[i]=='2'\n tp = i\n break\n end\nend\n\nif tp.nil?\n puts newstr + (\"1\" * oc)\nelse\n puts newstr[0...tp] + (\"1\" * oc) + newstr[tp..-1]\nend"}], "negative_code": [], "src_uid": "91cefa6793e77b3e4f4896616a164ff2"} {"nl": {"description": "You are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make as large as possible, where denotes the bitwise OR. Find the maximum possible value of after performing at most k operations optimally.", "input_spec": "The first line contains three integers n, k and x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8). The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109).", "output_spec": "Output the maximum value of a bitwise OR of sequence elements after performing operations.", "sample_inputs": ["3 1 2\n1 1 1", "4 2 3\n1 2 4 8"], "sample_outputs": ["3", "79"], "notes": "NoteFor the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is . For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result."}, "positive_code": [{"source_code": "class Solver\n\tdef main\n\t\tn, k, x = gets.split.map { |s| s.to_i }\n\t\ta = gets.split.map { |s| s.to_i }\n\t\ta.unshift nil\n\t\tx = x ** k\n\n\t\tprefix_or = Array.new(n + 2)\n\t\tsuffix_or = Array.new(n + 2)\n\t\tprefix_or[0] = suffix_or[n + 1] = 0\n\t\t1.upto(n) do |i|\n\t\t\tprefix_or[i] = (prefix_or[i - 1] | a[i])\n\t\tend\n\t\tn.downto(1) do |i|\n\t\t\tsuffix_or[i] = (suffix_or[i + 1] | a[i])\n\t\tend\n\n\t\tans = 0\n\t\t1.upto(n) do |i|\n\t\t\tans = [ans, (a[i] * x) | prefix_or[i - 1] | suffix_or[i + 1]].max\n\t\tend\n\t\tputs ans\n\tend\nend\n\nSolver.new.main\n"}], "negative_code": [], "src_uid": "b544f02d12846026f6c76876bc6bd079"} {"nl": {"description": "Long story short, shashlik is Miroslav's favorite food. Shashlik is prepared on several skewers simultaneously. There are two states for each skewer: initial and turned over.This time Miroslav laid out $$$n$$$ skewers parallel to each other, and enumerated them with consecutive integers from $$$1$$$ to $$$n$$$ in order from left to right. For better cooking, he puts them quite close to each other, so when he turns skewer number $$$i$$$, it leads to turning $$$k$$$ closest skewers from each side of the skewer $$$i$$$, that is, skewers number $$$i - k$$$, $$$i - k + 1$$$, ..., $$$i - 1$$$, $$$i + 1$$$, ..., $$$i + k - 1$$$, $$$i + k$$$ (if they exist). For example, let $$$n = 6$$$ and $$$k = 1$$$. When Miroslav turns skewer number $$$3$$$, then skewers with numbers $$$2$$$, $$$3$$$, and $$$4$$$ will come up turned over. If after that he turns skewer number $$$1$$$, then skewers number $$$1$$$, $$$3$$$, and $$$4$$$ will be turned over, while skewer number $$$2$$$ will be in the initial position (because it is turned again).As we said before, the art of cooking requires perfect timing, so Miroslav wants to turn over all $$$n$$$ skewers with the minimal possible number of actions. For example, for the above example $$$n = 6$$$ and $$$k = 1$$$, two turnings are sufficient: he can turn over skewers number $$$2$$$ and $$$5$$$.Help Miroslav turn over all $$$n$$$ skewers.", "input_spec": "The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\leq n \\leq 1000$$$, $$$0 \\leq k \\leq 1000$$$) — the number of skewers and the number of skewers from each side that are turned in one step.", "output_spec": "The first line should contain integer $$$l$$$ — the minimum number of actions needed by Miroslav to turn over all $$$n$$$ skewers. After than print $$$l$$$ integers from $$$1$$$ to $$$n$$$ denoting the number of the skewer that is to be turned over at the corresponding step.", "sample_inputs": ["7 2", "5 1"], "sample_outputs": ["2\n1 6", "2\n1 4"], "notes": "NoteIn the first example the first operation turns over skewers $$$1$$$, $$$2$$$ and $$$3$$$, the second operation turns over skewers $$$4$$$, $$$5$$$, $$$6$$$ and $$$7$$$.In the second example it is also correct to turn over skewers $$$2$$$ and $$$5$$$, but turning skewers $$$2$$$ and $$$4$$$, or $$$1$$$ and $$$5$$$ are incorrect solutions because the skewer $$$3$$$ is in the initial state after these operations."}, "positive_code": [{"source_code": "n,k=gets.strip.split.map(&:to_i)\n\npos=[]\n(k+1).downto(1).each do |i|\n tmp = []\n j = i\n while j<=n do\n tmp << j\n j += ((2*k)+1)\n end\n next if tmp.size == 0\n lp = tmp[-1]\n if lp+k >= n\n pos = tmp\n break\n end\nend\n\nputs pos.size\nputs pos.join(' ')"}, {"source_code": "\n#require \"pry\"\n\nn, k = gets.chomp.split(\" \").map(&:to_i)\n\ndef shashlik_cooking(n, k)\n \n r = n % (2*k + 1)\n\n \n steps = []\n #binding.pry\n i = 0\n while k+1 + i*(2*k+1) <= n\n steps[i] = k+1 + i*(2*k+1)\n i += 1\n end\n #binding.pry\n\n if r > 0 and r <= k and k != 0\n steps.map!{|s| s - (k+1-r)}\n steps << n\n end\n\n\n p steps.size\n if steps[0] > n - steps[steps.size-1]\n steps.reverse!\n steps.map!{|i| n-i+1}\n end\n print steps.join(' ')\nend\n\nshashlik_cooking(n, k)\n"}], "negative_code": [{"source_code": "\n#require \"pry\"\n\nn, k = gets.chomp.split(\" \").map(&:to_i)\n\ndef shashlik_cooking(n, k)\n \n r = n % (2*k + 1)\n\n \n steps = []\n #binding.pry\n i = 0\n while k+1 + i*(2*k+1) <= n\n steps[i] = k+1 + i*(2*k+1)\n i += 1\n end\n #binding.pry\n\n if r > 0 and r <= k and k != 0\n steps.map!{|s| s - k}\n steps << n - k - 1 + r\n end\n\n\n p steps.size\n if steps[0] > n - steps[steps.size-1]\n steps.reverse!\n steps.map!{|i| n-i+1}\n end\n print steps.join(' ')\nend\n\nshashlik_cooking(n, k)\n"}, {"source_code": "\n#require \"pry\"\n\nn, k = gets.chomp.split(\" \").map(&:to_i)\n\ndef shashlik_cooking(n, k)\n \n t = n / (2*k + 1)\n r = n % (2*k + 1)\n p t+1\n \n steps = []\n #binding.pry\n i = 0\n while k+1 + i*(2*k+1) <= n\n steps[i] = k+1 + i*(2*k+1)\n i += 1\n end\n #binding.pry\n\n if r > 0 and r <= k\n steps.map!{|s| s - k}\n steps << n - k - 1 + r\n end\n #binding.pry\n\n p steps.join(' ')\nend\n\nshashlik_cooking(n, k)\n"}, {"source_code": "\n#require \"pry\"\n\nn, k = gets.chomp.split(\" \").map(&:to_i)\n\ndef shashlik_cooking(n, k)\n \n t = n / (2*k + 1)\n r = n % (2*k + 1)\n\n \n steps = []\n #binding.pry\n i = 0\n while k+1 + i*(2*k+1) <= n\n steps[i] = k+1 + i*(2*k+1)\n i += 1\n end\n #binding.pry\n\n if r > 0 and r <= k and k != 0\n t += 1\n steps.map!{|s| s - k}\n steps << n - k - 1 + r\n end\n #binding.pry\n\n res = \"#{steps.join(' ')}\"\n p t\n print res\nend\n\nshashlik_cooking(n, k)\n"}, {"source_code": "\n#require \"pry\"\n\nn, k = gets.chomp.split(\" \").map(&:to_i)\n\ndef shashlik_cooking(n, k)\n \n t = n / (2*k + 1)\n r = n % (2*k + 1)\n p t+1\n \n steps = []\n #binding.pry\n i = 0\n while k+1 + i*(2*k+1) <= n\n steps[i] = k+1 + i*(2*k+1)\n i += 1\n end\n #binding.pry\n\n if r > 0 and r <= k\n steps.map!{|s| s - k}\n steps << n - k - 1 + r\n end\n #binding.pry\n\n res = \"#{steps.join(' ')}\"\n print res\nend\n\nshashlik_cooking(n, k)\n"}, {"source_code": "\n#require \"pry\"\n\nn, k = gets.chomp.split(\" \").map(&:to_i)\n\ndef shashlik_cooking(n, k)\n \n r = n % (2*k + 1)\n\n \n steps = []\n #binding.pry\n i = 0\n while k+1 + i*(2*k+1) <= n\n steps[i] = k+1 + i*(2*k+1)\n i += 1\n end\n #binding.pry\n\n if r > 0 and r <= k and k != 0\n steps.map!{|s| s - k}\n steps << n\n end\n\n\n p steps.size\n if steps[0] > n - steps[steps.size-1]\n steps.reverse!\n steps.map!{|i| n-i+1}\n end\n print steps.join(' ')\nend\n\nshashlik_cooking(n, k)\n"}, {"source_code": "\n#require \"pry\"\n\nn, k = gets.chomp.split(\" \").map(&:to_i)\n\ndef shashlik_cooking(n, k)\n \n t = n / (2*k + 1)\n r = n % (2*k + 1)\n p t+1\n \n steps = []\n #binding.pry\n i = 0\n while k+1 + i*(2*k+1) <= n\n steps[i] = k+1 + i*(2*k+1)\n i += 1\n end\n #binding.pry\n\n if r > 0 and r <= k and k != 0\n steps.map!{|s| s - k}\n steps << n - k - 1 + r\n end\n #binding.pry\n\n res = \"#{steps.join(' ')}\"\n print res\nend\n\nshashlik_cooking(n, k)\n"}, {"source_code": "\n#require \"pry\"\n\nn, k = gets.chomp.split(\" \").map(&:to_i)\n\ndef shashlik_cooking(n, k)\n \n t = n / (2*k + 1)\n r = n % (2*k + 1)\n\n \n steps = []\n #binding.pry\n i = 0\n while k+1 + i*(2*k+1) <= n\n steps[i] = k+1 + i*(2*k+1)\n i += 1\n end\n #binding.pry\n\n if r > 0 and r <= k and k != 0\n t += 1\n steps.map!{|s| s - k}\n steps << n - k - 1 + r\n end\n\n\n p t\n if steps[0] > n - steps[steps.size-1]\n steps.reverse!\n steps.map!{|i| n-i+1}\n end\n print steps.join(' ')\nend\n\nshashlik_cooking(n, k)\n"}], "src_uid": "dfd60a02670749c67b0f96df1a0709b9"} {"nl": {"description": "Ksusha the Squirrel is standing at the beginning of a straight road, divided into n sectors. The sectors are numbered 1 to n, from left to right. Initially, Ksusha stands in sector 1. Ksusha wants to walk to the end of the road, that is, get to sector n. Unfortunately, there are some rocks on the road. We know that Ksusha hates rocks, so she doesn't want to stand in sectors that have rocks.Ksusha the squirrel keeps fit. She can jump from sector i to any of the sectors i + 1, i + 2, ..., i + k. Help Ksusha! Given the road description, say if she can reach the end of the road (note, she cannot stand on a rock)?", "input_spec": "The first line contains two integers n and k (2 ≤ n ≤ 3·105, 1 ≤ k ≤ 3·105). The next line contains n characters — the description of the road: the i-th character equals \".\", if the i-th sector contains no rocks. Otherwise, it equals \"#\". It is guaranteed that the first and the last characters equal \".\".", "output_spec": "Print \"YES\" (without the quotes) if Ksusha can reach the end of the road, otherwise print \"NO\" (without the quotes).", "sample_inputs": ["2 1\n..", "5 2\n.#.#.", "7 3\n.#.###."], "sample_outputs": ["YES", "YES", "NO"], "notes": null}, "positive_code": [{"source_code": "A = gets.chomp.split(' ').map{|c| c.to_i}\nB = gets.chomp\na = '#'*A[1]\nif B[a]\n\tputs \"NO\"\nelse\n\tputs \"YES\"\nend\n"}, {"source_code": "k = gets.split[1].to_i\nputs (gets.chomp.split('.').map(&:size).max || -1) >= k ? :NO : :YES\n"}, {"source_code": "# https://codeforces.com/problemset/problem/299/B\n\ninput = gets.split(' ').map(&:to_i)\nroad = gets.chomp\n\nk = input[1]\n\nif road.include? \"#\"*k\n puts \"NO\"\nelse\n puts \"YES\"\nend\n"}, {"source_code": "n,k=gets.split.map &:to_i\nputs gets['#'*k]?'NO':'YES'\n"}, {"source_code": "n,k=gets.split.map &:to_i\nputs gets['#'*k]?'NO':'YES'\n"}, {"source_code": "n,k=gets.split.map &:to_i\nputs gets['#'*k]?'NO':'YES'"}, {"source_code": "n, k = gets.split(' ').map { |i| i.to_i }\nroads = gets\ncan = true\ncnt = 0\nfor i in 0...roads.size\n if roads[i] == '#' then cnt += 1\n else cnt = 0\n end\n can = false if cnt >= k\nend\nputs can ? \"YES\" : \"NO\"\n"}, {"source_code": "n,k=gets.split.map &:to_i\nputs gets['#'*k]?'NO':'YES'\n"}, {"source_code": "n,k=gets.split.map &:to_i\nputs gets['#'*k]?'NO':'YES'\n"}, {"source_code": "n,k=gets.split.map &:to_i\nputs gets['#'*k]?'NO':'YES'"}, {"source_code": "n,k=gets.split.map &:to_i\ns=gets\npre=-1\n(0..n).each do |i|\n next if s[i]!='.'\n exit !puts('NO') if i-pre>k\n pre=i\nend\nputs 'YES'"}, {"source_code": "def main()\n\tn,k=gets.chomp.to_s.split(\" \").map{|i| i.to_i}\n\troad=gets.chomp\n\tf=true\n\ti=0\n\twhile(i=k\n\t\t\t\tf=false\n\t\t\t\tbreak\n\t\t\tend\n\t\telse\n\t\t\ti+=1\n\t\tend\n\tend\n\tif f\n\t\tputs \"YES\"\n\telse\n\t\tputs \"NO\"\n\tend\nend\nmain()"}, {"source_code": "n,k=gets.split.map &:to_i\nputs gets['#'*k]?'NO':'YES'\n"}, {"source_code": "def ksuha(n, k, input)\n j = 0\n check = false\n if input[0..-1] == \"#\" * n\n return false\n end\n while j < n\n if input[j] == \".\"\n j += 1\n next\n elsif input[j] == \"#\"\n (1..k).each do |i|\n if input[j..j+i] != \"#\" * (i+1)\n j+= i\n check = true\n break\n end\n end\n if check\n check = false\n else\n return false\n end\n end\n end\n true\nend\n\n A = gets.chomp.split(' ').map{|c| c.to_i}\n B = gets.chomp\n a = '#'*A[1]\n if B[a]\n puts \"NO\"\n else\n puts \"YES\"\n end\n\n\n"}, {"source_code": "n,k=gets.split.map &:to_i\nputs gets['#'*k]?'NO':'YES'\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\ns = gets.chomp\nif s[-1] == '#'\n\tputs \"NO\"\n\texit 0\nend\ncc = 0\nn.times do |i|\n\tif s[i] == '#'\n\t\tcc += 1\n\telse\n\t\tif cc >= k\n\t\t\tputs \"NO\"\n\t\t\texit 0\n\t\tend\n\t\tcc = 0\n\tend\nend\nputs \"YES\"\n"}, {"source_code": "at_exit do\n sector_count, step_count = get_numbers_from_line[0..1]\n road = gets\n max_stone_length = 0\n in_stone = false\n current_stone_length = 0\n (0...sector_count).each do |i|\n if road[i] == '#'\n in_stone = true\n current_stone_length += 1\n else\n if in_stone\n in_stone = false\n max_stone_length = current_stone_length if current_stone_length > max_stone_length\n current_stone_length = 0\n end\n end\n end\n\n if step_count > max_stone_length\n puts 'YES'\n else\n puts 'NO'\n end\nend\n\ndef get_numbers_from_line\n gets.strip.split.map(&:to_i)\nend"}, {"source_code": "n,k=gets.split.map &:to_i\nputs gets['#'*k]?'NO':'YES'\n"}, {"source_code": "n,k=gets.split.map &:to_i\nputs gets['#'*k]?'NO':'YES'\n"}], "negative_code": [{"source_code": "k = gets.split[1].to_i\nputs gets.split('.').map(&:size).max >= k ? :NO : :YES\n"}], "src_uid": "d504d894b2f6a830c4d3b4edc54395be"} {"nl": {"description": "In this problem you have to simulate the workflow of one-thread server. There are n queries to process, the i-th will be received at moment ti and needs to be processed for di units of time. All ti are guaranteed to be distinct.When a query appears server may react in three possible ways: If server is free and query queue is empty, then server immediately starts to process this query. If server is busy and there are less than b queries in the queue, then new query is added to the end of the queue. If server is busy and there are already b queries pending in the queue, then new query is just rejected and will never be processed. As soon as server finished to process some query, it picks new one from the queue (if it's not empty, of course). If a new query comes at some moment x, and the server finishes to process another query at exactly the same moment, we consider that first query is picked from the queue and only then new query appears.For each query find the moment when the server will finish to process it or print -1 if this query will be rejected.", "input_spec": "The first line of the input contains two integers n and b (1 ≤ n, b ≤ 200 000) — the number of queries and the maximum possible size of the query queue. Then follow n lines with queries descriptions (in chronological order). Each description consists of two integers ti and di (1 ≤ ti, di ≤ 109), where ti is the moment of time when the i-th query appears and di is the time server needs to process it. It is guaranteed that ti - 1 < ti for all i > 1.", "output_spec": "Print the sequence of n integers e1, e2, ..., en, where ei is the moment the server will finish to process the i-th query (queries are numbered in the order they appear in the input) or  - 1 if the corresponding query will be rejected.", "sample_inputs": ["5 1\n2 9\n4 8\n10 9\n15 2\n19 1", "4 1\n2 8\n4 8\n10 9\n15 2"], "sample_outputs": ["11 19 -1 21 22", "10 18 27 -1"], "notes": "NoteConsider the first sample. The server will start to process first query at the moment 2 and will finish to process it at the moment 11. At the moment 4 second query appears and proceeds to the queue. At the moment 10 third query appears. However, the server is still busy with query 1, b = 1 and there is already query 2 pending in the queue, so third query is just rejected. At the moment 11 server will finish to process first query and will take the second query from the queue. At the moment 15 fourth query appears. As the server is currently busy it proceeds to the queue. At the moment 19 two events occur simultaneously: server finishes to proceed the second query and the fifth query appears. As was said in the statement above, first server will finish to process the second query, then it will pick the fourth query from the queue and only then will the fifth query appear. As the queue is empty fifth query is proceed there. Server finishes to process query number 4 at the moment 21. Query number 5 is picked from the queue. Server finishes to process query number 5 at the moment 22. "}, "positive_code": [{"source_code": "n, stack_count = gets.split.map(&:to_i)\ntime = 0\nstack = []\nres = []\nif n<=stack_count\n n.times do |i|\n t_request, p_request = gets.split.map(&:to_i)\n if i == 0\n time+=(t_request+p_request) if i==0\n else\n if t_request <= time\n time+=p_request\n else\n time = t_request+ p_request\n end\n end\n res[i]=time\n end\nelse\nn.times do |i|\n t_request, p_request = gets.split.map(&:to_i)\n if stack == []\n if t_request >= time\n time = t_request + p_request\n res[i]=time\n else\n if stack.count < stack_count\n stack.push([t_request,p_request,i])\n else\n res[i] = -1\n end\n end\n else\n if t_request >= time\n if stack[0][0] <= time\n time+=stack[0][1]\n else\n time = stack[0][0] + stack[0][1]\n end\n res[stack[0][2]] = time\n stack.delete_at(0)\n if stack.count < stack_count\n stack.push([t_request,p_request,i])\n else\n res[i] = -1\n end\n else\n if stack.count < stack_count\n stack.push([t_request,p_request,i])\n else\n res[i] = -1\n end\n end\n end\nend\ni = 0\nwhile stack != []\n if stack[0][0] <= time\n time+=stack[0][1]\n else\n time = stack[0][0] + stack[0][1]\n end\n res[stack[0][2]] = time\n stack.delete_at(0)\n i+=1\nend\nend\nputs res.join(' ')"}], "negative_code": [{"source_code": "n, stack_count = gets.split.map(&:to_i)\ntime = 0\nstack = []\nres = []\nn.times do |i|\n t_request, p_request = gets.split.map(&:to_i)\n if stack == []\n if t_request >= time\n time = t_request + p_request\n res[i]=time\n else\n if stack.count < stack_count\n stack.push([t_request,p_request,i])\n else\n res[i] = -1\n end\n end\n else\n if t_request >= time\n if stack[0][0] <= time\n time+=stack[0][1]\n else\n time = stack[0][0] + stack[0][1]\n end\n res[stack[0][2]] = time\n stack.delete_at(0)\n if stack.count < stack_count\n stack.push([t_request,p_request,i])\n else\n res[i] = -1\n end\n else\n if stack.count < stack_count\n stack.push([t_request,p_request,i])\n else\n res[i] = -1\n end\n end\n end\nend\ni = 0\nwhile stack != []\n time+=stack[0][1]\n res[stack[0][2]] = time\n stack.delete_at(0)\n i+=1\nend\n\nputs res.join(' ')"}, {"source_code": "n, stack_count = gets.split.map(&:to_i)\ntime = 0\nstack = []\nres = []\nn.times do |i|\n t_request, p_request = gets.split.map(&:to_i)\n if stack == []\n if t_request >= time\n time = t_request + p_request\n res[i]=time\n else\n if stack.count < stack_count\n stack.push([t_request,p_request,i])\n else\n res[i] = -1\n end\n end\n else\n if t_request >= time\n if stack[0][0] <= time\n time+=stack[0][1]\n else\n time = stack[0][0] + stack[0][1]\n end\n res[stack[0][2]] = time\n stack.delete_at(0)\n if stack.count < stack_count\n stack.push([t_request,p_request,i])\n else\n res[i] = -1\n end\n else\n if stack.count < stack_count\n stack.push([t_request,p_request,i])\n else\n res[i] = -1\n end\n end\n end\nend\ni = 0\nwhile stack != []\n if stack[0][0] <= time\n time+=stack[0][1]\n else\n time = stack[0][0] + stack[0][1]\n end\n res[stack[0][2]] = time\n stack.delete_at(0)\n i+=1\nend"}, {"source_code": "n, stack_count = gets.split.map(&:to_i)\ntime = 0\nstack = []\nres = []\nif n<=stack_count\n n.times do |i|\n t_request, p_request = gets.split.map(&:to_i)\n if i == 0\n time+=(t_request+p_request) if i==0\n else\n time+=p_request\n end\n res[i]=time\n end\nelse\nn.times do |i|\n t_request, p_request = gets.split.map(&:to_i)\n if stack == []\n if t_request >= time\n time = t_request + p_request\n res[i]=time\n else\n if stack.count < stack_count\n stack.push([t_request,p_request,i])\n else\n res[i] = -1\n end\n end\n else\n if t_request >= time\n if stack[0][0] <= time\n time+=stack[0][1]\n else\n time = stack[0][0] + stack[0][1]\n end\n res[stack[0][2]] = time\n stack.delete_at(0)\n if stack.count < stack_count\n stack.push([t_request,p_request,i])\n else\n res[i] = -1\n end\n else\n if stack.count < stack_count\n stack.push([t_request,p_request,i])\n else\n res[i] = -1\n end\n end\n end\nend\ni = 0\nwhile stack != []\n if stack[0][0] <= time\n time+=stack[0][1]\n else\n time = stack[0][0] + stack[0][1]\n end\n res[stack[0][2]] = time\n stack.delete_at(0)\n i+=1\nend\nend\nputs res.join(' ')"}, {"source_code": "n, stack_count = gets.split.map(&:to_i)\ntime = 0\nstack = []\nres = []\nn.times do |i|\n t_request, p_request = gets.split.map(&:to_i)\n if stack == []\n if t_request >= time\n time = t_request + p_request\n res[i]=time\n else\n if stack.count < stack_count\n stack.push([t_request,p_request,i])\n else\n res[i] = -1\n end\n end\n else\n if t_request >= time\n time+=stack[0][1]\n res[stack[0][2]] = time\n stack.delete_at(0)\n if stack.count < stack_count\n stack.push([t_request,p_request,i])\n else\n res[i] = -1\n end\n else\n if stack.count < stack_count\n stack.push([t_request,p_request,i])\n else\n res[i] = -1\n end\n end\n end\nend\ni = 0\nwhile stack != []\n time+=stack[0][1]\n res[stack[0][2]] = time\n stack.delete_at(0)\n i+=1\nend\n\nputs res.join(' ')"}], "src_uid": "5981594b2d6d1077ce2249b641d18f10"} {"nl": {"description": "In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time.", "input_spec": "The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year.", "output_spec": "Print a single number — the minimum jury size.", "sample_inputs": ["2\n5 23 1 2\n3 13 2 3", "3\n12 9 2 1\n12 8 1 3\n12 8 2 2", "1\n1 10 1 13"], "sample_outputs": ["2", "3", "1"], "notes": null}, "positive_code": [{"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\nrequire 'date'\nn=gets.to_i\nh={}\nn.times{\n\tn,d,p,t=gets.split.map(&:to_i)\n\tdate=Date.new(2013,n,d)\n\tif h[date]\n\t\th[date]+=p\n\telse\n\t\th[date]=p\n\tend\n\t(t-1).times{\n\t\tdate-=1\n\t\tif h[date]\n\t\t\th[date]+=p\n\t\telse\n\t\t\th[date]=p\n\t\tend\n\t}\n}\np h.values.max\n"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\nrequire 'date'\nn=gets.to_i\nh={}\nn.times{\n\tn,d,p,t=gets.split.map(&:to_i)\n\tdate=Date.new(2013,n,d)\n\tif h[date]\n\t\th[date]+=p\n\telse\n\t\th[date]=p\n\tend\n\t(t-1).times{\n\t\tdate-=1\n\t\tif h[date]\n\t\t\th[date]+=p\n\t\telse\n\t\t\th[date]=p\n\t\tend\n\t}\n}\np h.values.max\n"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\nrequire 'date'\nn=gets.to_i\nh={}\nn.times{\n\tn,d,p,t=gets.split.map(&:to_i)\n\tdate=Date.new(2013,n,d)\n\tif h[date]\n\t\th[date]+=p\n\telse\n\t\th[date]=p\n\tend\n\t(t-1).times{\n\t\tdate-=1\n\t\tif h[date]\n\t\t\th[date]+=p\n\t\telse\n\t\t\th[date]=p\n\t\tend\n\t}\n}\np h.values.max"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\nrequire 'date'\nn=gets.to_i\nh={}\nn.times{\n\tn,d,p,t=gets.split.map(&:to_i)\n\tdate=Date.new(2013,n,d)\n\tif h[date]\n\t\th[date]+=p\n\telse\n\t\th[date]=p\n\tend\n\t(t-1).times{\n\t\tdate-=1\n\t\tif h[date]\n\t\t\th[date]+=p\n\t\telse\n\t\t\th[date]=p\n\t\tend\n\t}\n}\np h.values.max\n"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\nrequire 'date'\nn=gets.to_i\nh={}\nn.times{\n\tn,d,p,t=gets.split.map(&:to_i)\n\tdate=Date.new(2013,n,d)\n\tif h[date]\n\t\th[date]+=p\n\telse\n\t\th[date]=p\n\tend\n\t(t-1).times{\n\t\tdate-=1\n\t\tif h[date]\n\t\t\th[date]+=p\n\t\telse\n\t\t\th[date]=p\n\t\tend\n\t}\n}\np h.values.max\n"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\nrequire 'date'\nn=gets.to_i\nh={}\nn.times{\n\tn,d,p,t=gets.split.map(&:to_i)\n\tdate=Date.new(2013,n,d)\n\tif h[date]\n\t\th[date]+=p\n\telse\n\t\th[date]=p\n\tend\n\t(t-1).times{\n\t\tdate-=1\n\t\tif h[date]\n\t\t\th[date]+=p\n\t\telse\n\t\t\th[date]=p\n\t\tend\n\t}\n}\np h.values.max\n"}, {"source_code": "require 'Date'\n\nline = File.read(\"input.txt\").split(\"\\n\")\n\nn = line[0].to_i\ncount = Hash.new(0)\nline[1..-1].each do |l|\n\tm,d,j,t = l.split(\" \").map(&:to_i)\n\tcurr = Date.new(2013,m,d)\n\tfinish = curr - 1 \n\tstart = curr - t\n\t(start..finish).each do |k|\n\t\tcount[k] += j\n\tend\nend\n\nputs \"#{count}\"\nFile.open(\"output.txt\", 'w') { |file| \n\tfile.puts \"#{count.values.max}\"\n}\n\n\n"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\nrequire 'date'\nn=gets.to_i\nh={}\nn.times{\n\tn,d,p,t=gets.split.map(&:to_i)\n\tdate=Date.new(2013,n,d)\n\tif h[date]\n\t\th[date]+=p\n\telse\n\t\th[date]=p\n\tend\n\t(t-1).times{\n\t\tdate-=1\n\t\tif h[date]\n\t\t\th[date]+=p\n\t\telse\n\t\t\th[date]=p\n\t\tend\n\t}\n}\np h.values.max"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\nrequire 'date'\nn=gets.to_i\nh={}\nn.times{\n\tn,d,p,t=gets.split.map(&:to_i)\n\tdate=Date.new(2013,n,d)\n\tif h[date]\n\t\th[date]+=p\n\telse\n\t\th[date]=p\n\tend\n\t(t-1).times{\n\t\tdate-=1\n\t\tif h[date]\n\t\t\th[date]+=p\n\t\telse\n\t\t\th[date]=p\n\t\tend\n\t}\n}\np h.values.max\n"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\nrequire 'date'\nn=gets.to_i\nh={}\nn.times{\n\tn,d,p,t=gets.split.map(&:to_i)\n\tdate=Date.new(2013,n,d)\n\tif h[date]\n\t\th[date]+=p\n\telse\n\t\th[date]=p\n\tend\n\t(t-1).times{\n\t\tdate-=1\n\t\tif h[date]\n\t\t\th[date]+=p\n\t\telse\n\t\t\th[date]=p\n\t\tend\n\t}\n}\np h.values.max\n"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\nrequire 'date'\nn=gets.to_i\nh={}\nn.times{\n\tn,d,p,t=gets.split.map(&:to_i)\n\tdate=Date.new(2013,n,d)\n\tif h[date]\n\t\th[date]+=p\n\telse\n\t\th[date]=p\n\tend\n\t(t-1).times{\n\t\tdate-=1\n\t\tif h[date]\n\t\t\th[date]+=p\n\t\telse\n\t\t\th[date]=p\n\t\tend\n\t}\n}\np h.values.max"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\nrequire 'date'\nn=gets.to_i\nh={}\nn.times{\n\tn,d,p,t=gets.split.map(&:to_i)\n\tdate=Date.new(2013,n,d)\n\tif h[date]\n\t\th[date]+=p\n\telse\n\t\th[date]=p\n\tend\n\t(t-1).times{\n\t\tdate-=1\n\t\tif h[date]\n\t\t\th[date]+=p\n\t\telse\n\t\t\th[date]=p\n\t\tend\n\t}\n}\np h.values.max\n"}], "negative_code": [{"source_code": "line = File.read(\"input.txt\").split(\"\\n\")\n\nn = line[0].to_i\ncount = Hash.new(0)\nline[1..-1].each do |l|\n\tm,d,j,t = l.split(\" \").map(&:to_i)\n\tcurr = (m * 30) + d\n\tfinish = curr - 1 \n\tstart = curr - t\n\t(start..finish).each do |k|\n\t\tcount[k] += j\n\tend\nend\n\n\nFile.open(\"output.txt\", 'w') { |file| \n\tfile.puts \"#{count.values.max}\"\n}\n\n\n"}, {"source_code": "n = gets.to_i\ncount = Hash.new(0)\nn.times do \n\tm,d,j,t = gets.split(\" \").map(&:to_i)\n\tcurr = (m * 30) + d\n\tfinish = curr - 1 \n\tstart = curr - t\n\t(start..finish).each do |k|\n\t\tcount[k] += j\n\tend\nend\n\nputs \"#{count.values.max}\""}], "src_uid": "34655e8de9f1020677c5681d9d217d75"} {"nl": {"description": "You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\\operatorname{AB}(s) = \\operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \\le i \\le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \\dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \\le |s| \\le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b.", "output_spec": "For each test case, print the resulting string $$$s$$$ with $$$\\operatorname{AB}(s) = \\operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them.", "sample_inputs": ["4\nb\naabbbabaa\nabbb\nabbaab"], "sample_outputs": ["b\naabbbabaa\nbbbb\nabbaaa"], "notes": "NoteIn the first test case, both $$$\\operatorname{AB}(s) = 0$$$ and $$$\\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\\operatorname{AB}(s) = 2$$$ and $$$\\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\\operatorname{AB}(s) = 1$$$ and $$$\\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\\operatorname{AB}(s) = 2$$$ and $$$\\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$."}, "positive_code": [{"source_code": "def solve()\r\n s = string\r\n if s[0] != s[-1]\r\n s[0] = s[-1]\r\n end\r\n s\r\nend\r\n\r\ndef main\r\n t = int\r\n t.times do\r\n puts solve\r\n end\r\nend\r\n\r\nDEBUG = true\r\nMOD = 10**9+7\r\nYESNO = %w(No Yes)\r\nINF = 10**9\r\n\r\ndef int; gets.to_s.to_i end\r\ndef ints; gets.to_s.split.map { |s| s.to_i } end\r\ndef int1s; gets.to_s.split.map { |s| s.to_i - 1 } end\r\ndef float; gets.to_s.to_f end\r\ndef floats; gets.to_s.split.map { |s| s.to_f } end\r\ndef array_of(&convert); gets.to_s.split.map(&convert) end\r\ndef string; gets.to_s.chomp end\r\ndef rep(n, &b); Array.new(n, &b) end\r\ndef yes; puts YESNO[1] end\r\ndef no; puts YESNO[0] end\r\ndef yesno t; puts YESNO[t] end\r\ndef zip(xs, *yss); Enumerator.new { |y| xs.zip(*yss) { |a| y.yield(*a) } } end\r\ndef max(*xs, &block); block_given? ? xs.max_by(&block) : xs.max end\r\ndef min(*xs, &block); block_given? ? xs.min_by(&block) : xs.min end\r\ndef minmax(*xs, &block); block_given? ? xs.minmax_by(&block) : xs.minmax end\r\ndef gcd(*xs); xs.inject(0, :gcd) end\r\ndef matrix(h, w, fill=nil, &block); return Array.new(h) { Array.new(w, &block) } if block_given?; Array.new(h) { [fill] * w } end\r\ndef debug(x = nil); STDERR.puts (block_given? ? yield(x) : x).inspect if DEBUG; x end\r\ndef debug_grid(grid, width = 1); grid.each { |row| STDERR.puts row.map { |x| x.inspect.ljust(width) }.join(\"\") } if DEBUG; grid end\r\ndef if_debug; yield if DEBUG end\r\n\r\nmodule Boolean\r\n def coerce(other); [other, to_i] end\r\n def +@; to_i end\r\n def to_int; to_i end\r\n def *(other); to_i * other end\r\nend\r\n\r\nclass TrueClass\r\n include Boolean\r\n def to_i; 1 end\r\nend\r\n\r\nclass FalseClass\r\n include Boolean\r\n def to_i; 0 end\r\nend\r\n\r\nclass Integer\r\n def div_ceil(y); (self + y - 1) / y end\r\n def mod_inv(mod = MOD); pow(mod-2, mod) end\r\n def mod_div(y, mod = MOD); self * mod_inv(y, mod) % mod end\r\n def factorial(mod = MOD); (2..self).inject(1) { |f, x| f * x % mod } end\r\n def popcount; x = self; c = 0; while x > 0; c += 1 if x & 1 == 1; x >>= 1 end; c end #TODO: faster\r\n def bitbrute(&block); (1< 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\r\n def lower_bound; ac, wa = self.end_open, self.begin; while ac - wa > 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\r\n def shakutori(r2, &pred); Enumerator.new { |y| j, r = r2.begin, r2.end_open; each { |i| j += 1 while j + 1 < r and pred[i, j+1]; y.yield(i, j) } }; end\r\n def widest(&block); Enumerator.new { |y| j, n = self.begin, self.end_open; each { |i| j += 1 while j < n and block[i, j]; y.yield(i, j) if block[i, j] } } end\r\nend\r\n\r\nclass Array\r\n def power(&block); (0 ... 1 << size).each(&block) end\r\n def sorted_merge(other); a = []; i = j = 0; n, m = size, other.size; if j < m and other[j] < self[i]; a << other[j]; j += 1 else; a << self[i]; i += 1 end while i < n; a.push(*other[j..-1]) if j < m; a end\r\n def upper_bound; ac, wa = 0, size; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\r\n def lower_bound; ac, wa = size, 0; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\r\n def cum(*xs, &op); a = []; a << xs[0] if xs.size > 0; a << x = self[0]; (1...size).each { |i| a << x = op[x, self[i]] }; a end\r\n def cumdiff(range); self[range.end_open] - self[range.begin]; end\r\nend\r\n\r\nmodule Enumerable\r\n def sorted_uniq; x = nil; filter { |y| c = x === y; x = y; !c } end\r\n def cumsum; ys = [0]; each { |x| ys << x + ys[-1] }; ys end\r\nend\r\n\r\nmain"}], "negative_code": [], "src_uid": "351ffff1dfe1bc1762f062f612463759"} {"nl": {"description": "Furlo and Rublo play a game. The table has n piles of coins lying on it, the i-th pile has ai coins. Furlo and Rublo move in turns, Furlo moves first. In one move you are allowed to: choose some pile, let's denote the current number of coins in it as x; choose some integer y (0 ≤ y < x; x1 / 4 ≤ y ≤ x1 / 2) and decrease the number of coins in this pile to y. In other words, after the described move the pile will have y coins left. The player who can't make a move, loses. Your task is to find out, who wins in the given game if both Furlo and Rublo play optimally well.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 77777) — the number of piles. The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 777777777777) — the sizes of piles. The numbers are separated by single spaces. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.", "output_spec": "If both players play optimally well and Furlo wins, print \"Furlo\", otherwise print \"Rublo\". Print the answers without the quotes.", "sample_inputs": ["1\n1", "2\n1 2", "10\n1 2 3 4 5 6 7 8 9 10"], "sample_outputs": ["Rublo", "Rublo", "Furlo"], "notes": null}, "positive_code": [{"source_code": "I =->{gets.split.map &:to_i}\nRD =->{gets.to_i}\n\ndef sg(x)\n return 2 if(x>=2562991876)\n return 1 if(x>=45212176)\n return 1 if(x>=50626)\n return 3 if(x>=6724)\n return 0 if(x>=82)\n return 2 if(x>=16)\n return 1 if(x>=4)\n return 0\nend\nres = 0; n = RD[]; a = I[]; a.each{|ai|\n res ^= sg(ai)\n}\n\nputs res.zero? ? \"Rublo\" : \"Furlo\""}, {"source_code": "gets;x,A,B=0,[4,16,82,6724,50626,2562991876],[0,1,2,0,3,1,2]\nfor a in gets.split.map &:to_i do\n x^=B[A.count {|i| a>=i}]\nend\nputs (x==0?\"Rublo\":\"Furlo\")\n\n"}, {"source_code": "gets;x,A,B=0,[4,16,82,6724,50626,2562991876],[0,1,2,0,3,1,2]\nfor a in gets.split.map &:to_i do\n x^=B[A.count {|i| a>=i}]\nend\nputs (x==0?\"Rublo\":\"Furlo\")\n\n"}, {"source_code": "gets;x,A,B=0,[4,16,82,6724,50626,2562991876],[0,1,2,0,3,1,2]\nfor a in gets.split.map &:to_i do\n x^=B[A.count {|i| a>=i}]\nend\nputs (x==0?\"Rublo\":\"Furlo\")\n\n"}, {"source_code": "gets;x,A,B=0,[4,16,82,6724,50626,2562991876],[0,1,2,0,3,1,2]\nfor a in gets.split.map &:to_i do\n x^=B[A.count {|i| a>=i}]\nend\nputs (x==0?\"Rublo\":\"Furlo\")\n"}, {"source_code": "gets;x,A,B=0,[4,16,82,6724,50626,2562991876],[0,1,2,0,3,1,2]\nfor a in gets.split.map &:to_i do\n x^=B[A.count {|i| a>=i}]\nend\nputs (x==0?\"Rublo\":\"Furlo\")\n\n"}, {"source_code": "gets;x,A,B=0,[4,16,82,6724,50626,2562991876],[0,1,2,0,3,1,2]\nfor a in gets.split.map &:to_i do\n x^=B[A.count {|i| a>=i}]\nend\nputs (x==0?\"Rublo\":\"Furlo\")\n\n"}, {"source_code": "gets;x,A,B=0,[4,16,82,6724,50626,2562991876],[0,1,2,0,3,1,2]\nfor a in gets.split.map &:to_i do\n x^=B[A.count {|i| a>=i}]\nend\nputs (x==0?\"Rublo\":\"Furlo\")\n\n"}, {"source_code": "gets;x,A,B=0,[4,16,82,6724,50626,2562991876],[0,1,2,0,3,1,2]\nfor a in gets.split.map &:to_i do\n x^=B[A.count {|i| a>=i}]\nend\nputs (x==0?\"Rublo\":\"Furlo\")\n\n"}, {"source_code": "gets;x,A,B=0,[4,16,82,6724,50626,2562991876],[0,1,2,0,3,1,2]\nfor a in gets.split.map &:to_i do\n x^=B[A.count {|i| a>=i}]\nend\nputs (x==0?\"Rublo\":\"Furlo\")\n\n"}], "negative_code": [{"source_code": "RD =->{gets.to_i}\ndef sg(x)\n return 2 if(x>=2562991876)\n return 1 if(x>=45212176)\n return 1 if(x>=50626)\n return 3 if(x>=6724)\n return 0 if(x>=82)\n return 2 if(x>=16)\n return 1 if(x>=4)\n return 0\nend\nres = 0; n = RD[]; n.times{\n res ^= sg(RD[])\n}\nputs res.zero? ? \"Rublo\" : \"Furlo\""}, {"source_code": "RD =->{gets.to_i}\ndef sg(x)\n return 2 if(x>=2562991876)\n return 1 if(x>=45212176)\n return 1 if(x>=50626)\n return 3 if(x>=6724)\n return 0 if(x>=82)\n return 2 if(x>=16)\n return 1 if(x>=4)\n return 0\nend\nres = 0; RD[].times{\n res ^= sg(RD[])\n}\nputs res.zero? ? \"Rublo\" : \"Furlo\""}], "src_uid": "cc23f07b6539abbded7e120793bef6a7"} {"nl": {"description": "Xenia the mathematician has a sequence consisting of n (n is divisible by 3) positive integers, each of them is at most 7. She wants to split the sequence into groups of three so that for each group of three a, b, c the following conditions held: a < b < c; a divides b, b divides c. Naturally, Xenia wants each element of the sequence to belong to exactly one group of three. Thus, if the required partition exists, then it has groups of three.Help Xenia, find the required partition or else say that it doesn't exist.", "input_spec": "The first line contains integer n (3 ≤ n ≤ 99999) — the number of elements in the sequence. The next line contains n positive integers, each of them is at most 7. It is guaranteed that n is divisible by 3.", "output_spec": "If the required partition exists, print groups of three. Print each group as values of the elements it contains. You should print values in increasing order. Separate the groups and integers in groups by whitespaces. If there are multiple solutions, you can print any of them. If there is no solution, print -1.", "sample_inputs": ["6\n1 1 1 2 2 2", "6\n2 2 1 1 4 6"], "sample_outputs": ["-1", "1 2 4\n1 2 6"], "notes": null}, "positive_code": [{"source_code": "n=gets.to_i\nh=[0]*8\na=gets.scan(/\\d/){|x|h[x.to_i]+=1}\nif h[1]==n/3 && h[2]+h[3]==n/3 && h[4]+h[6]==n/3 && h[3]<=h[6]\n h[3].times{puts '1 3 6'}\n h[6]-=h[3]\n h[6].times{puts '1 2 6'}\n h[4].times{puts '1 2 4'}\nelse\n p -1\nend\n\n\n"}, {"source_code": "n=gets.to_i\nh=[0]*8\na=gets.scan(/\\d/){|x|h[x.to_i]+=1}\nif h[1]==n/3 && h[2]+h[3]==n/3 && h[4]+h[6]==n/3 && h[3]<=h[6]\n h[3].times{puts '1 3 6'}\n h[6]-=h[3]\n h[6].times{puts '1 2 6'}\n h[4].times{puts '1 2 4'}\nelse\n p -1\nend\n\n\n"}, {"source_code": "cnt = gets.to_i\nnums = gets.split.map(&:to_i)\nn1=n2=n3=n4=n6=0\nnums.each{|n|\n\tcase n\n\twhen 1; n1 += 1\n\twhen 2; n2 += 1\n\twhen 3; n3 += 1\n\twhen 4; n4 += 1\n\twhen 6; n6 += 1\n\twhen 5, 7\n\t\tputs -1\n\t\texit\n\tend\n}\nif n1 != cnt/3 || n2+n3 != cnt/3 || n4+n6 != cnt/3 || n2 0 && num_list[2] > 0 && num_list[4] > 0\n answer << \"1 2 4\"\n num_list[1] -= 1\n num_list[2] -= 1\n num_list[4] -= 1\n end\n\n while num_list[1] > 0 && num_list[2] > 0 && num_list[6] > 0\n answer << \"1 2 6\"\n num_list[1] -= 1\n num_list[2] -= 1\n num_list[6] -= 1\n end\n\n while num_list[1] > 0 && num_list[3] > 0 && num_list[6] > 0\n answer << \"1 3 6\"\n num_list[1] -= 1\n num_list[3] -= 1\n num_list[6] -= 1\n end\n\n if answer.size == n/3\n puts answer\n else\n puts -1\n end\n end\nend\n\na = A.new"}, {"source_code": "n=gets.to_i\nh=[0]*8\na=gets.scan(/\\d/){|x|h[x.to_i]+=1}\nif h[1]==n/3 && h[2]+h[3]==n/3 && h[4]+h[6]==n/3 && h[3]<=h[6]\n h[3].times{puts '1 3 6'}\n h[6]-=h[3]\n h[6].times{puts '1 2 6'}\n h[4].times{puts '1 2 4'}\nelse\n p -1\nend\n\n\n"}, {"source_code": "n=gets.to_i\nh=[0]*8\na=gets.scan(/\\d/){|x|h[x.to_i]+=1}\nif h[1]==n/3 && h[2]+h[3]==n/3 && h[4]+h[6]==n/3 && h[3]<=h[6]\n h[3].times{puts '1 3 6'}\n h[6]-=h[3]\n h[6].times{puts '1 2 6'}\n h[4].times{puts '1 2 4'}\nelse\n p -1\nend\n\n\n"}, {"source_code": "a=gets.chomp.to_i\nb=gets.chomp.split(\" \")\nk=b.include?(\"7\")\nl=b.include?(\"5\") \nif k || l\nputs \"-1\"\nelsif\none=b.count(\"1\")\ntwo=b.count(\"2\")\nthree=b.count(\"3\")\nfour=b.count(\"4\")\nsix=b.count(\"6\")\n\nif one!=two+three\n\nputs \"-1\"\n\nelse\n\nif two+three != four+six || three >six\nputs \"-1\"\nelse\n\nthree.times do\nputs \"1 3 6\"\nend\nfour.times do\nputs \"1 2 4\"\nend\n(two-four).times do\nputs \"1 2 6\"\nend\n\nend\nend\nend "}, {"source_code": "n=gets.to_i\nh=[0]*8\na=gets.scan(/\\d/){|x|h[x.to_i]+=1}\nif h[1]==n/3 && h[2]+h[3]==n/3 && h[4]+h[6]==n/3 && h[3]<=h[6]\n h[3].times{puts '1 3 6'}\n h[6]-=h[3]\n h[6].times{puts '1 2 6'}\n h[4].times{puts '1 2 4'}\nelse\n p -1\nend\n\n\n"}, {"source_code": "n=gets.to_i\nh=Hash.new 0\na=gets.split.each{|x|h[x]+=1}\nif h['1']==n/3 && h['2']+h['3']==n/3 && h['4']+h['6']==n/3 && h['3']<=h['6']\n h['3'].times {puts '1 3 6'}\n h['6']-=h['3']\n h['6'].times {puts '1 2 6'}\n h['4'].times {puts '1 2 4'}\nelse\n p -1\nend\n\n"}, {"source_code": "STDOUT.sync=STDIN.sync=false\nn=gets.to_i\nh=[0]*8\na=gets.scan(/\\d/){|x|h[x.to_i]+=1}\nif h[1]==n/3 && h[2]+h[3]==n/3 && h[4]+h[6]==n/3 && h[3]<=h[6]\n h[3].times{puts '1 3 6'}\n h[6]-=h[3]\n h[6].times{puts '1 2 6'}\n h[4].times{puts '1 2 4'}\nelse\n p -1\nend\n\n"}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\nna=a.count(1)\nnb1=a.count(2)\nnb2=a.count(3)\nnc1=a.count(4)\nnc2=a.count(6)\nif na==n/3 && nb1+nb2==n/3 && nc1+nc2==n/3 && nb2<=nc2\n nb2.times {puts '1 3 6'}\n nc2-=nb2\n nc2.times {puts '1 2 6'}\n nc1.times {puts '1 2 4'}\nelse\n p -1\nend\n\n"}, {"source_code": "n=gets.to_i\nh=[0]*8\na=gets.scan(/\\d/){|x|h[x.to_i]+=1}\nif h[1]==n/3 && h[2]+h[3]==n/3 && h[4]+h[6]==n/3 && h[3]<=h[6]\n h[3].times{puts '1 3 6'}\n h[6]-=h[3]\n h[6].times{puts '1 2 6'}\n h[4].times{puts '1 2 4'}\nelse\n p -1\nend\n\n"}, {"source_code": "n=gets.to_i\nh=Hash.new 0\na=gets.scan(/\\d/){|x|h[x]+=1}\nif h['1']==n/3 && h['2']+h['3']==n/3 && h['4']+h['6']==n/3 && h['3']<=h['6']\n h['3'].times {puts '1 3 6'}\n h['6']-=h['3']\n h['6'].times {puts '1 2 6'}\n h['4'].times {puts '1 2 4'}\nelse\n p -1\nend\n\n"}, {"source_code": "n=gets.to_i\nh=[0]*8\na=gets.scan(/\\d/){|x|h[x.to_i]+=1}\nif h[1]==n/3 && h[2]+h[3]==n/3 && h[4]+h[6]==n/3 && h[3]<=h[6]\n h[3].times{puts '1 3 6'}\n h[6]-=h[3]\n h[6].times{puts '1 2 6'}\n h[4].times{puts '1 2 4'}\nelse\n p -1\nend\n\n\n"}, {"source_code": "n = gets.to_i\nh = gets.split.map { |x| x.to_i }.inject(Hash.new(0)) { |h, e| h[e] += 1; h }\nunless h[1] == n / 3 && h[6] >= h[3] && h[2] + h[3] == h[4] + h[6] && h[2] + h[3] + h[4] + h[6] == 2 * n / 3\n puts -1\n exit\nend\nfor _ in 1..h[4]\n puts '1 2 4'\nend\nfor _ in 1..h[2] - h[4]\n puts '1 2 6'\nend\nfor _ in 1..h[3]\n puts '1 3 6'\nend"}, {"source_code": "#!/usr/bin/env ruby\n\n\ngets.chomp\narr = gets.chomp.split.map(&:to_i).sort\n\none = arr.count(1)\ntwo = arr.count(2)\nthree = arr.count(3)\nfour = arr.count(4)\nsix = arr.count(6)\n\nif one != arr.size / 3\n puts -1\n exit\nend\n\ntwo_four = [two, four].min\ntwo -= two_four\nfour -= two_four\n\nthree_six = [three, six].min\nthree -= three_six\nsix -= three_six\n\ntwo_six = [two, six].min\ntwo -= two_six\nsix -= two_six\n\nif (two_four + three_six + two_six) != (arr.size / 3)\n puts -1\n exit\nend\n\n\ntwo_four.times do\n puts [1, 2, 4].join(\" \")\nend\n\ntwo_six.times do\n puts [1, 2, 6].join(\" \")\nend\n\nthree_six.times do\n puts [1, 3, 6].join(\" \")\nend\n"}, {"source_code": "n=gets.to_i\nh=[0]*8\na=gets.scan(/\\d/){|x|h[x.to_i]+=1}\nif h[1]==n/3 && h[2]+h[3]==n/3 && h[4]+h[6]==n/3 && h[3]<=h[6]\n h[3].times{puts '1 3 6'}\n h[6]-=h[3]\n h[6].times{puts '1 2 6'}\n h[4].times{puts '1 2 4'}\nelse\n p -1\nend\n\n\n"}, {"source_code": "n=gets.to_i\nh=[0]*8\na=gets.scan(/\\d/){|x|h[x.to_i]+=1}\nif h[1]==n/3 && h[2]+h[3]==n/3 && h[4]+h[6]==n/3 && h[3]<=h[6]\n h[3].times{puts '1 3 6'}\n h[6]-=h[3]\n h[6].times{puts '1 2 6'}\n h[4].times{puts '1 2 4'}\nelse\n p -1\nend\n\n\n"}], "negative_code": [{"source_code": "class A\n def initialize\n n = gets.chomp.to_i\n num_list = Hash.new(0)\n\n list = gets.chomp.split(' ').map(&:to_i)\n answer = []\n\n list.each do |num|\n num_list[num] += 1\n end\n\n if num_list.has_key?(3) || num_list.has_key?(5) || num_list.has_key?(7)\n puts -1\n exit\n end\n\n while num_list[1] > 0 && num_list[2] > 0 && num_list[4] > 0\n answer << \"1 2 4\"\n num_list[1] -= 1\n num_list[2] -= 1\n num_list[4] -= 1\n end\n\n while num_list[1] > 0 && num_list[2] > 0 && num_list[6] > 0\n answer << \"1 2 6\"\n num_list[1] -= 1\n num_list[2] -= 1\n num_list[6] -= 1\n end\n\n while num_list[1] > 0 && num_list[4] > 0 && num_list[6] > 0\n answer << \"1 4 6\"\n num_list[1] -= 1\n num_list[4] -= 1\n num_list[6] -= 1\n end\n\n while num_list[2] > 0 && num_list[4] > 0 && num_list[6] > 0\n answer << \"2 4 6\"\n num_list[2] -= 1\n num_list[4] -= 1\n num_list[6] -= 1\n end\n\n if answer.size == n/3\n puts answer\n else\n puts -1\n end\n end\nend\n\na = A.new"}, {"source_code": "a=gets.chomp.to_i\nb=gets.chomp.split(\" \")\nk=b.include?(\"7\")\nl=b.include?(\"7\") \nif k || l\nputs \"-1\"\nelsif\none=b.count(\"1\")\ntwo=b.count(\"2\")\nthree=b.count(\"3\")\nfour=b.count(\"4\")\nsix=b.count(\"6\")\n\nif one!=two+three\n\nputs \"-1\"\n\nelse\n\nif two+three != four+six || three >six\nputs \"-1\"\nelse\n\nthree.times do\nputs \"1 3 6\"\nend\nfour.times do\nputs \"1 2 4\"\nend\n(two-four).times do\nputs \"1 2 6\"\nend\n\nend\nend\nend "}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\nna=a.count(1)\nnb=a.count(2)\nnc1=a.count(4)\nnc2=a.count(6)\nif na==n/3 && nb==n/3 && nc1+nc2==n/3\n nc1.times {puts '1 2 4'}\n nc2.times {puts '1 2 6'}\nelse\n p -1\nend\n\n"}, {"source_code": "n=gets.to_i\nh=Hash.new 0\na=gets.split.each{|x|h[x]+=1}\nif h['1']==n/3 && h['2']+h['3']==n/3 && h['4']+h['6']==n/3 && h['3']<=h['6']\n h['3'].times {puts '1 3 6'}\n h['6']-=h['3']\n h['3'].times {puts '1 2 6'}\n h['4'].times {puts '1 2 4'}\nelse\n p -1\nend\n\n"}, {"source_code": "#!/usr/bin/env ruby\n\n\ngets.chomp\narr = gets.chomp.split.map(&:to_i).uniq.sort\n\nprinted = false\narr.permutation(3).each do |a,b,c|\n if (a % b == 0) && (b % c == 0)\n puts [c, b , a].join(' ')\n printed = true\n end\nend\n\nunless printed\n puts -1\nend\n"}, {"source_code": "#!/usr/bin/env ruby\n\n\ngets.chomp\narr = gets.chomp.split.map(&:to_i).sort\n\narr = arr.permutation(3).to_a.map do |a,b,c|\n if (a % b == 0) && (b % c == 0) && (a > b) && (b > c)\n [c, b , a].join(' ')\n end\nend\n\nputs arr.compact.sort.uniq\n"}, {"source_code": "#!/usr/bin/env ruby\n\n\ngets.chomp\narr = gets.chomp.split.map(&:to_i).sort\n\none = arr.count(1)\ntwo = arr.count(2)\nthree = arr.count(3)\nfour = arr.count(4)\nsix = arr.count(6)\n\nif one != arr.size / 3\n puts -1\n exit\nend\n\ntwo_four = [two, four].min\ntwo -= two_four\nfour -= two_four\n\nthree_six = [three, six].min\nthree -= three_six\nsix -= three_six\n\ntwo_six = [two, six].min\ntwo -= two_six\nsix -= two_six\n\nif two_four + three_six + two_six != arr.size / 3\n puts -1\n exit\nend\n\n\nif two_four.nonzero?\n puts [1, 2, 4].join(\" \")\nend\n\nif two_six.nonzero?\n puts [1, 2, 6].join(\" \")\nend\n\nif three_six.nonzero?\n puts [1, 3, 6].join(\" \")\nend\n"}, {"source_code": "#!/usr/bin/env ruby\n\n\ngets.chomp\narr = gets.chomp.split.map(&:to_i).sort\n\narr = arr.permutation(3).to_a.map do |a,b,c|\n if (a % b == 0) && (b % c == 0) && (a > b) && (b > c)\n [c, b , a].join(' ')\n end\nend\n\narr = arr.compact.sort.uniq\n\nif arr.size.zero?\n puts -1\nelse\n puts arr\nend\n"}], "src_uid": "513234db1bab98c2c2ed6c7030c1ac72"} {"nl": {"description": "Cat Noku has obtained a map of the night sky. On this map, he found a constellation with n stars numbered from 1 to n. For each i, the i-th star is located at coordinates (xi, yi). No two stars are located at the same position.In the evening Noku is going to take a look at the night sky. He would like to find three distinct stars and form a triangle. The triangle must have positive area. In addition, all other stars must lie strictly outside of this triangle. He is having trouble finding the answer and would like your help. Your job is to find the indices of three stars that would form a triangle that satisfies all the conditions. It is guaranteed that there is no line such that all stars lie on that line. It can be proven that if the previous condition is satisfied, there exists a solution to this problem.", "input_spec": "The first line of the input contains a single integer n (3 ≤ n ≤ 100 000). Each of the next n lines contains two integers xi and yi ( - 109 ≤ xi, yi ≤ 109). It is guaranteed that no two stars lie at the same point, and there does not exist a line such that all stars lie on that line.", "output_spec": "Print three distinct integers on a single line — the indices of the three points that form a triangle that satisfies the conditions stated in the problem. If there are multiple possible answers, you may print any of them.", "sample_inputs": ["3\n0 1\n1 0\n1 1", "5\n0 0\n0 2\n2 0\n2 2\n1 1"], "sample_outputs": ["1 2 3", "1 3 5"], "notes": "NoteIn the first sample, we can print the three indices in any order.In the second sample, we have the following picture. Note that the triangle formed by starts 1, 4 and 3 doesn't satisfy the conditions stated in the problem, as point 5 is not strictly outside of this triangle (it lies on it's border)."}, "positive_code": [{"source_code": "def slope(x1, y1, x2, y2)\n p = y2 - y1\n q = x2 - x1\n g = p.gcd(q)\n p /= g\n q /= g\n\n [p, q]\nend\n\nn = gets.chomp.to_i\npoints = n.times.map.with_index { |i| [gets.chomp.split.map(&:to_i), i + 1]}\n\npoints.sort! { |a, b| a[0] <=> b[0] }\n\nans = [points[0][1], points[1][1]]\nconst = slope(*points[0][0], *points[1][0])\n\n(2...n).each do |i|\n if slope(*points[0][0], *points[i][0]) != const then\n ans << points[i][1]\n break\n end\nend\n\nputs ans.join(' ')\n"}, {"source_code": "n = gets.chomp.to_i\nxs = []\nys = []\nhs = Hash.new()\nans = []\nfor i in 1..n\n x, y = gets.chomp.split.map(&:to_i)\n xs << x\n ys << y\n hs[i] = (x - xs[0])**2 + (y - ys[0])**2\nend\n\nhs.sort { |(_, v1), (_, v2)| v1 <=> v2 }.each do |k, _|\n ans << k if ans.size < 3\n if ans.size == 3\n x0 = xs[ans[0] - 1]\n y0 = ys[ans[0] - 1]\n x1 = xs[ans[1] - 1]\n y1 = ys[ans[1] - 1]\n x2 = xs[ans[2] - 1]\n y2 = ys[ans[2] - 1]\n if (x1 - x0) * (y2 - y0) == (x2 - x0) * (y1 - y0)\n ans.delete_at(2)\n else\n break\n end\n end\nend\n\nputs ans.*' '\n"}], "negative_code": [], "src_uid": "0d3ac2472990aba36abee156069b1088"} {"nl": {"description": "The polar bears are going fishing. They plan to sail from (sx, sy) to (ex, ey). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently at (x, y). If the wind blows to the east, the boat will move to (x + 1, y). If the wind blows to the south, the boat will move to (x, y - 1). If the wind blows to the west, the boat will move to (x - 1, y). If the wind blows to the north, the boat will move to (x, y + 1). Alternatively, they can hold the boat by the anchor. In this case, the boat stays at (x, y). Given the wind direction for t seconds, what is the earliest time they sail to (ex, ey)?", "input_spec": "The first line contains five integers t, sx, sy, ex, ey (1 ≤ t ≤ 105,  - 109 ≤ sx, sy, ex, ey ≤ 109). The starting location and the ending location will be different. The second line contains t characters, the i-th character is the wind blowing direction at the i-th second. It will be one of the four possibilities: \"E\" (east), \"S\" (south), \"W\" (west) and \"N\" (north).", "output_spec": "If they can reach (ex, ey) within t seconds, print the earliest time they can achieve it. Otherwise, print \"-1\" (without quotes).", "sample_inputs": ["5 0 0 1 1\nSESNW", "10 5 3 3 6\nNENSWESNEE"], "sample_outputs": ["4", "-1"], "notes": "NoteIn the first sample, they can stay at seconds 1, 3, and move at seconds 2, 4.In the second sample, they cannot sail to the destination."}, "positive_code": [{"source_code": "A = gets.chomp.split(' ').map{|c| c.to_i}\nB = gets.chomp\ny = A[3]-A[1]\nx = A[4]-A[2]\nfor c in 0..(B.length-1)\n\tx -= 1 if x > 0 and B[c] == 'N'\n\tx += 1 if x < 0 and B[c] == 'S'\n\ty -= 1 if y > 0 and B[c] == 'E'\n\ty += 1 if y < 0 and B[c] == 'W'\n\t# puts \"#{B[c]}: #{x} , #{y}\"\n\tif x == 0 and y == 0\n\t\tputs c+1\n\t\texit\n\tend\nend\nputs -1"}, {"source_code": "_, x0, y0, x, y = gets.split.map(&:to_i)\nx -= x0\ny -= y0\ngets.chomp.chars.each_with_index do |e, i|\n case e\n when 'E'\n x -= 1 if x > 0\n when 'S'\n y += 1 if y < 0\n when 'W'\n x += 1 if x < 0\n when 'N'\n y -= 1 if y > 0\n end\n if x == 0 && y == 0\n p i + 1\n exit\n end\nend\np -1\n"}, {"source_code": "n, sx, sy, ex, ey = gets.split(' ').map { |i| i.to_i }\nwind = gets.chomp\ntx = ex - sx\nty = ey - sy\nfor i in 0...wind.size\n tx -= 1 if tx > 0 && wind[i] == 'E'\n tx += 1 if tx < 0 && wind[i] == 'W'\n ty -= 1 if ty > 0 && wind[i] == 'N'\n ty += 1 if ty < 0 && wind[i] == 'S'\n if tx == 0 and ty == 0\n puts i + 1\n break\n end\nend\nif tx != 0 or ty != 0\n puts -1\nend"}, {"source_code": "\ndef getdist(x1,y1,x2,y2)\n\tv1=(x1-x2).abs;\n\tv2=(y1-y2).abs;\n\treturn Math.sqrt(v1**2+v2**2);\nend\n\ndef main()\n\tinp=gets.chomp.strip.to_s.split(\" \",5).map{|i| i.to_i};\n\tt=inp[0];\n\tsx=inp[1];\n\tsy=inp[2];\n\tex=inp[3];\n\tey=inp[4];\n\twind=gets.chomp.strip.to_s;\n\tix=sx;\n\tf=0;\n\tiy=sy;\n\tfor i in 0..t-1\n\t\twd=wind[i].chr;\n\t\tif wd==\"E\"\n\t\t\tif getdist(ix,iy,ex,ey)>getdist(ix+1,iy,ex,ey)\n\t\t\t\tix=ix+1;\n\t\t\t\tiy=iy;\n\t\t\tend\n\t\telsif wd==\"S\"\t\n\t\t\tif getdist(ix,iy,ex,ey)>getdist(ix,iy-1,ex,ey)\n\t\t\t\tix=ix;\n\t\t\t\tiy=iy-1;\n\t\t\tend\n\t\t\n\t\telsif wd==\"N\"\n\t\t\tif getdist(ix,iy,ex,ey)>getdist(ix,iy+1,ex,ey)\n\t\t\t\tix=ix;\n\t\t\t\tiy=iy+1;\n\t\t\tend\n\t\t\n\t\telsif wd==\"W\"\n\t\t\tif getdist(ix,iy,ex,ey)>getdist(ix-1,iy,ex,ey)\n\t\t\t\tix=ix-1;\n\t\t\t\tiy=iy;\n\t\t\tend\n\t\t\n\t\tend\n\t\tif ix==ex and iy==ey\n\t\t\tf=1;\n\t\t\tputs i+1;\n\t\t\tbreak;\n\t\tend\n\tend\n\t\n\tif f==0\n\t\tputs \"-1\";\n\tend\t\nend\n\nmain()\n\t\n"}, {"source_code": "n,x1,y1,x2,y2,=gets.split.map{|e| e.to_i}\ns=gets.split(//)\nt=0\nwhile ((x1!=x2 || y1!=y2) && t 0 }\ns = 0\ngets.chomp.split('').each { |sw|\n w = sw.to_sym\n ds.delete(w) if ds.has_key?(w) && (ds[w] -= 1) == 0\n s += 1\n break if ds.empty?\n}\nputs(ds.empty? ? s : -1)"}], "negative_code": [{"source_code": "n, sx, sy, ex, ey = gets.split(' ').map { |i| i.to_i }\nwind = gets.chomp\ntx = ex - sx\nty = ey - sy\nfor i in 0...wind.size\n tx -= 1 if tx > 0 && wind[i] == 'E'\n tx += 1 if tx < 0 && wind[i] == 'W'\n ty -= 1 if ty > 0 && wind[i] == 'N'\n tx += 1 if ty < 0 && wind[i] == 'S'\n if tx == 0 and ty == 0\n puts i + 1\n break\n end\nend\nif tx != 0 or ty != 0\n puts -1\nend"}, {"source_code": "n,x1,y1,x2,y2,=gets.split.map{|e| e.to_i}\ns=gets.split(//)\nt=0\nwhile ((x1!=x2 || y1!=y2) && tf\nm.times do\n t,l,r=gets.split.map(&:to_i)\n u=t-p\n if s < f\n u+=1 if !((l-1..r)===s+u)\n u=f-s if s+u>f\n else\n u+=1 if !((l..r+1)===s-u)\n u=s-f if s-uf\nm.times do\n t,l,r=gets.split.map(&:to_i)\n u=t-p\n if s < f\n u+=1 if !((l-1..r)===s+u)\n u=f-s if s+u>f\n else\n u+=1 if !((l..r+1)===s-u)\n u=s-f if s-uf\nm.times do\n t,l,r=gets.split.map(&:to_i)\n u=t-p\n if s < f\n u+=1 if !((l-1..r)===s+u)\n u=f-s if s+u>f\n else\n u+=1 if !((l..r+1)===s-u)\n u=s-f if s-uf\nm.times do\n t,l,r=gets.split.map(&:to_i)\n u=t-p\n if s < f\n u+=1 if !((l-1..r)===s+u)\n u=f-s if s+u>f\n else\n u+=1 if !((l..r+1)===s-u)\n u=s-f if s-uf\nm.times do\n t,l,r=gets.split.map(&:to_i)\n u=t-p\n if s < f\n u+=1 if !((l-1..r)===s+u)\n u=f-s if s+u>f\n else\n u+=1 if !((l..r+1)===s-u)\n u=s-f if s-uf\nm.times do\n t,l,r=gets.split.map(&:to_i)\n u=t-p\n if s < f\n u+=1 if !((l-1..r)===s+u)\n u=f-s if s+u>f\n else\n u+=1 if !((l..r+1)===s-u)\n u=s-f if s-uf\nm.times do\n t,l,r=gets.split.map(&:to_i)\n u=t-p\n if s < f\n u+=1 if !((l-1..r)===s+u)\n u=f-s if s+u>f\n else\n u+=1 if !((l..r+1)===s-u)\n u=s-f if s-uf\nm.times do\n t,l,r=gets.split.map(&:to_i)\n u=t-p\n if s < f\n u+=1 if !((l-1..r)===s+u)\n u=f-s if s+u>f\n else\n u+=1 if !((l..r+1)===s-u)\n u=s-f if s-uf\n q<<'R'*u\n s+=u\n break if s==f\n q<<'X'*(t+1-p-u)\n p=t+1\n end\n q<<'R'*(f-s) if sf\nm.times do\n t,l,r=gets.split.map(&:to_i)\n u=t-p\n if s < f\n u+=1 if !((l-1..r)===s+u)\n u=f-s if s+u>f\n else\n u+=1 if !((l..r+1)===s-u)\n u=s-f if s-uf\nm.times do\n t,l,r=gets.split.map(&:to_i)\n u=t-p\n if s < f\n u+=1 if !((l-1..r)===s+u)\n u=f-s if s+u>f\n else\n u+=1 if !((l..r+1)===s-u)\n u=s-f if s-uf\nm.times do\n t,l,r=gets.split.map(&:to_i)\n u=t-p\n if s < f\n u+=1 if !((l-1..r)===s+u)\n u=f-s if s+u>f\n else\n u+=1 if !((l..r+1)===s-u)\n u=s-f if s-uf\n u=0 if u < 0\n q<<'R'*u\n s+=u\n break if s==f\n q<<'X'*(t+1-p-u)\n p=t+1\nend\nq<<'R'*(f-s) if s 0\n elsif i < j\n array[i][j] = array[i][j - 1] + 1\n else\n array[i][j] = array[j][i - 1]\n end\n end\nend\n\nputs num_of_days\narray.each do |e|\n e.each do |element|\n printf(\"%d \", element)\n end\n printf(\"\\n\");\nend"}, {"source_code": "n = gets.to_i\n\nprev = [[1, 2], [1, 3], [2, 3]]\nb = 3\nwhile true\n cur = []\n tail = []\n prev.each do |a|\n b += 1\n tail << b\n cur << (a + [b])\n end\n cur << tail\n if b > n\n break\n end\n prev = cur\nend\n\np prev.size\nprev.each{|x| puts x*\" \"}\n"}, {"source_code": "n = gets.to_i\na = []\ni, j = 1, 1\nwhile n >= i\n i.times{ |k|\n a[i] ||= []\n a[k] ||= []\n a[i] << j\n a[k] << j\n j += 1\n }\n n -= i\n i += 1\nend\np a.length\na.each{ |x| puts x*\" \" }"}, {"source_code": "n = gets.to_i\nk = ((1 + Math.sqrt(1 + 8 * n)) / 2).to_i\nn_ = k * (k - 1) / 2\n\nd1 = 1\nd2 = 2\n\na = Array.new(k + 1, \"\")\n\nfor i in 1 .. n_\n\ta[d1] += i.to_s + \" \"\n\ta[d2] += i.to_s + \" \"\n\td2 += 1\n\tif d2 > k then\n\t\td1 += 1\n\t\td2 = d1 + 1\n\tend\nend\n\nputs k\nfor i in 1 .. k\n\tputs a[i]\nend"}], "negative_code": [{"source_code": "hobbits_num = gets.to_i\nnum_of_days = ((-1 + Math.sqrt(1 + 8 * hobbits_num)) / 2).floor + 1\ninvited_hobbits = (num_of_days * (num_of_days - 1)) / 2\nlist_length = num_of_days - 1\narray = Array.new(num_of_days) {Array.new(list_length, 0)}\nvisit = Array.new(invited_hobbits, 0)\ndef next_visit(start, last, visit)\n for i in start...last\n if visit[i] == 0\n visit[i] = 1\n return i\n end\n end\nend\n\nnum_of_days.times do |n|\n for i in 0...n\n array[n][i] = array[i][n - 1]\n end\n for i in n...list_length\n array[n][i] = next_visit i, invited_hobbits, visit\n end\nend\narray.each do |e|\n e.each do |element|\n printf(\"%d \", element + 1)\n end\n printf(\"\\n\");\nend"}, {"source_code": "n = gets.to_i\na = []\ni, j = 1, 1\nwhile n >= 2*i\n i.times{ |k|\n a[i] ||= []\n a[k] ||= []\n a[i] << j\n a[k] << j\n j += 1\n }\n n -= 2*i\n i += 1\nend\na.each{ |x| puts x*\" \" }"}, {"source_code": "n = gets.to_i\na = []\ni, j = 1, 1\nwhile n >= i\n i.times{ |k|\n a[i] ||= []\n a[k] ||= []\n a[i] << j\n a[k] << j\n j += 1\n }\n n -= i\n i += 1\nend\na.each{ |x| puts x*\" \" }"}, {"source_code": "n = gets.to_i\nk = ((1 + Math.sqrt(1 + 8 * n)) / 2).to_i\nn_ = k * (k - 1) / 2\n\nd1 = 1\nd2 = 2\n\na = Array.new(k + 1, \"\")\n\nfor i in 1 .. n_\n\ta[d1] += i.to_s + \" \"\n\ta[d2] += i.to_s + \" \"\n\td2 += 1\n\tif d2 > k then\n\t\td1 += 1\n\t\td2 = d1 + 1\n\tend\nend\n\nfor i in 1 .. k\n\tputs a[i]\nend"}], "src_uid": "e435a009962a7056d0fa00edb13ad01d"} {"nl": {"description": "Once at a team training Vasya, Petya and Sasha got a problem on implementing linear search in an array.According to the boys, linear search works as follows. The array elements in a pre-selected order are in turn compared with the number that you need to find. Once you find the array element that is equal to the required one, the search ends. The efficiency of the algorithm is the number of performed comparisons. The fewer comparisons the linear search has made, the more effective it is.Vasya believes that a linear search would work better if it sequentially iterates through the elements, starting with the 1-st one (in this problem we consider the elements of the array indexed from 1 to n) and ending with the n-th one. And Petya says that Vasya is wrong: the search will need less comparisons if it sequentially iterates the elements starting from the n-th and ending with the 1-st one. Sasha argues that the two approaches are equivalent.To finally begin the task, the teammates decided to settle the debate and compare the two approaches on an example. For this, they took an array that is a permutation of integers from 1 to n, and generated m queries of the form: find element with value bi in the array. They want to calculate for both approaches how many comparisons in total the linear search will need to respond to all queries. If the first search needs fewer comparisons, then the winner of the dispute is Vasya. If the second one does, then the winner is Petya. If both approaches make the same number of comparisons, then Sasha's got the upper hand.But the problem is, linear search is too slow. That's why the boys aren't going to find out who is right before the end of the training, unless you come in here. Help them to determine who will win the dispute.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 105) — the number of elements in the array. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ n) — the elements of array. The third line contains integer m (1 ≤ m ≤ 105) — the number of queries. The last line contains m space-separated integers b1, b2, ..., bm (1 ≤ bi ≤ n) — the search queries. Note that the queries can repeat.", "output_spec": "Print two integers, showing how many comparisons Vasya's approach needs and how many comparisons Petya's approach needs. Separate the numbers by spaces. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.", "sample_inputs": ["2\n1 2\n1\n1", "2\n2 1\n1\n1", "3\n3 1 2\n3\n1 2 3"], "sample_outputs": ["1 2", "2 1", "6 6"], "notes": "NoteIn the first sample Vasya's approach will make one comparison (it starts with the 1-st element and immediately finds the required number), and Petya's approach makes two comparisons (first he compares with the 2-nd array element, doesn't find the search item and compares with the 1-st element).In the second sample, on the contrary, Vasya's approach will need two comparisons (first with 1-st element, and then with the 2-nd), and Petya's approach will find the required value in one comparison (the first comparison with the 2-nd element)."}, "positive_code": [{"source_code": "h = Hash.new{[]}\nn = gets.to_i\ngets.split.map(&:to_i).each_with_index do |i, k|\n h[i] <<= k\nend\n\na, b = 0, 0\ngets\ngets.split.map(&:to_i).each do |i|\n k = h[i]\n a += k[0] + 1\n b += n - k[-1]\nend\n\nputs \"#{a} #{b}\"\n"}, {"source_code": "def compare_solutions(n,a,q,s)\n v_count = 0\n p_count = 0\n\n h_elements = Hash.new(0)\n # Creating a hash with element as symbol and value as index (as they can't be repeated cause\n # it could change the final couting for both team members)\n (1..n).each do |i|\n h_elements[a[i-1]] = i-1\n end\n\n queries = Hash.new(0)\n # Simplifying the amount of queries grouping them because the can be repeated\n s.each do |e|\n queries[e] +=1\n end\n\n queries.each do |k,v|\n e_index = h_elements[k]\n # vasya's count starting from the begining of the array and multiplied by v times the query is repeated\n v_count += (e_index + 1) * v\n # petya's count from the end of the array and multiplied by v times the query is repeated\n p_count += (n - e_index) * v\n end\n\n puts \"#{v_count} #{p_count}\"\nend\n\nn = gets.to_i\na_elements = gets.split(/\\s/)\nq_queries = gets.to_i\nqueries = gets.split(/\\s/)\ncompare_solutions(n,a_elements,q_queries,queries)"}, {"source_code": "h = Hash.new{[]}\nn = gets.to_i\ngets.split.map(&:to_i).each_with_index { |el, i| h[el] <<= i }\nl,r = 0,0\ngets\ngets.split.map(&:to_i).each { |x|\n k = h[x]\n l += k[0]+1\n r += n-k[-1]\n}\nputs \"#{l} #{r}\""}, {"source_code": "n=gets.to_i\na=gets.split.map &:to_i\np=[0]*(n+1)\nn.times{|i|p[a[i]]=i}\nz=0\nm=gets.to_i\ngets.split.map(&:to_i).each{|i|z+=p[i]}\nputs [z+m,m*n-z]*' '"}, {"source_code": "n = gets.to_i\npos = Hash.new\ntmp = 1\ngets.split.each do |a|\n pos[a.to_i]=tmp\n tmp += 1\nend\ntmp = 0\nm = gets.to_i\ngets.split.each do |b|\n tmp+=pos[b.to_i]\nend\nprint tmp, \" \", (n+1)*m-tmp, \"\\n\"\n"}, {"source_code": "n = gets.to_i\ni = -1\nar = Array.new(n){0}\ngets.split.map(&:to_i).each_with_index do |el, index|\n ar[el] = index+1\nend\nm = gets.to_i\nb = gets.split.map &:to_i\n\nsum1,sum2 = 0, 0\nb.each do |b|\n sum1 += ar[b]\n sum2 += n - ar[b] + 1\nend\n\nputs \"#{sum1} #{sum2}\""}, {"source_code": "n = gets.to_i\ni = -1\nar = Array.new(n){0}\ngets.split.map(&:to_i).each_with_index do |el, index|\n ar[el] = index+1\nend\nm = gets.to_i\nb = gets.split.map &:to_i\n\nsum1,sum2 = 0, 0\nb.each do |b|\n sum1 += ar[b]\n sum2 += n - ar[b] + 1\nend\n\nputs \"#{sum1} #{sum2}\""}], "negative_code": [], "src_uid": "b7aef95015e326307521fd59d4fccb64"} {"nl": {"description": "John Smith knows that his son, Thomas Smith, is among the best students in his class and even in his school. After the students of the school took the exams in English, German, Math, and History, a table of results was formed.There are $$$n$$$ students, each of them has a unique id (from $$$1$$$ to $$$n$$$). Thomas's id is $$$1$$$. Every student has four scores correspond to his or her English, German, Math, and History scores. The students are given in order of increasing of their ids.In the table, the students will be sorted by decreasing the sum of their scores. So, a student with the largest sum will get the first place. If two or more students have the same sum, these students will be sorted by increasing their ids. Please help John find out the rank of his son. ", "input_spec": "The first line contains a single integer $$$n$$$ ($$$1 \\le n \\le 1000$$$) — the number of students. Each of the next $$$n$$$ lines contains four integers $$$a_i$$$, $$$b_i$$$, $$$c_i$$$, and $$$d_i$$$ ($$$0\\leq a_i, b_i, c_i, d_i\\leq 100$$$) — the grades of the $$$i$$$-th student on English, German, Math, and History. The id of the $$$i$$$-th student is equal to $$$i$$$.", "output_spec": "Print the rank of Thomas Smith. Thomas's id is $$$1$$$.", "sample_inputs": ["5\n100 98 100 100\n100 100 100 100\n100 100 99 99\n90 99 90 100\n100 98 60 99", "6\n100 80 90 99\n60 60 60 60\n90 60 100 60\n60 100 60 80\n100 100 0 100\n0 0 0 0"], "sample_outputs": ["2", "1"], "notes": "NoteIn the first sample, the students got total scores: $$$398$$$, $$$400$$$, $$$398$$$, $$$379$$$, and $$$357$$$. Among the $$$5$$$ students, Thomas and the third student have the second highest score, but Thomas has a smaller id, so his rank is $$$2$$$.In the second sample, the students got total scores: $$$369$$$, $$$240$$$, $$$310$$$, $$$300$$$, $$$300$$$, and $$$0$$$. Among the $$$6$$$ students, Thomas got the highest score, so his rank is $$$1$$$."}, "positive_code": [{"source_code": "gets;a=$<.map{|e|e.split.map(&:to_i).reduce :+};p a[1..-1].count{|e|e>a[0]}+1"}, {"source_code": "N = gets.to_i\narr = []\n(1..N).each do |i|\n sum = gets.split.map(&:to_i).inject(:+)\n arr.push([-sum, i])\nend\n\narr.sort!\nN.times do |i|\n if arr[i][1] == 1\n puts i+1\n exit\n end\nend"}, {"source_code": "n = gets.to_i\ntom = gets.split.map(&:to_i).inject(:+)\nans = 1\n(n-1).times do\n sc = gets.split.map(&:to_i).inject(:+)\n ans += 1 if sc > tom\nend\nputs ans"}, {"source_code": "n = gets.strip.to_i\nranks = {}\nn.times do |i|\n scores = gets.strip.split.map(&:to_i).inject(:+)\n ranks[i + 1] = scores\nend\nranks = ranks.sort_by { |k, v| [-v, k] }\nans = 0\nranks.each do |rank|\n ans += 1\n break if rank[0] == 1\nend\nputs ans"}, {"source_code": "n=gets.to_i\nputs (1..n).map{|i| [-gets.split.map(&:to_i).inject(:+), i]}.sort.index{|_,i|i==1}+1\n"}, {"source_code": "n = gets.to_i\na = []\nn.times{a << gets.split.map{|x| x.to_i}.inject(0){|s, x| s + x}}\ns = a[0]\nputs(a.sort.reverse.index(s) + 1)\n\n"}, {"source_code": "n = gets.to_i\narr = []\nn.times do |i|\n arr.push [i+1]+gets.chomp.split(\" \").map(&:to_i)\nend\n\narr.sort_by!{|x| [-x[1..-1].inject(:+),x[0]]}\n\n\narr.each_with_index do |x,i|\n if x[0]==1\n puts i+1\n exit\n end\nend\n"}], "negative_code": [], "src_uid": "3c984366bba580993d1694d27982a773"} {"nl": {"description": "You have decided to watch the best moments of some movie. There are two buttons on your player: Watch the current minute of the movie. By pressing this button, you watch the current minute of the movie and the player automatically proceeds to the next minute of the movie. Skip exactly x minutes of the movie (x is some fixed positive integer). If the player is now at the t-th minute of the movie, then as a result of pressing this button, it proceeds to the minute (t + x). Initially the movie is turned on in the player on the first minute, and you want to watch exactly n best moments of the movie, the i-th best moment starts at the li-th minute and ends at the ri-th minute (more formally, the i-th best moment consists of minutes: li, li + 1, ..., ri). Determine, what is the minimum number of minutes of the movie you have to watch if you want to watch all the best moments?", "input_spec": "The first line contains two space-separated integers n, x (1 ≤ n ≤ 50, 1 ≤ x ≤ 105) — the number of the best moments of the movie and the value of x for the second button. The following n lines contain the descriptions of the best moments of the movie, the i-th line of the description contains two integers separated by a space li, ri (1 ≤ li ≤ ri ≤ 105). It is guaranteed that for all integers i from 2 to n the following condition holds: ri - 1 < li.", "output_spec": "Output a single number — the answer to the problem.", "sample_inputs": ["2 3\n5 6\n10 12", "1 1\n1 100000"], "sample_outputs": ["6", "100000"], "notes": "NoteIn the first sample, the player was initially standing on the first minute. As the minutes from the 1-st to the 4-th one don't contain interesting moments, we press the second button. Now we can not press the second button and skip 3 more minutes, because some of them contain interesting moments. Therefore, we watch the movie from the 4-th to the 6-th minute, after that the current time is 7. Similarly, we again skip 3 minutes and then watch from the 10-th to the 12-th minute of the movie. In total, we watch 6 minutes of the movie.In the second sample, the movie is very interesting, so you'll have to watch all 100000 minutes of the movie."}, "positive_code": [{"source_code": "n, x = gets.split.map(&:to_i)\nt = 1\nq = 0\nn.times do\n l, r = gets.split.map(&:to_i)\n t += x while t + x <= l\n q += r - t + 1\n t = r + 1\nend\nputs q\n"}, {"source_code": "#!/usr/bin/env ruby\nn,x=gets.split.map(&:to_i)\ncur=1\nret=0\nn.times{\n\tl,r=gets.split.map(&:to_i)\n\tret+=r+1-l+(l-cur)%x\n\tcur=r+1\n}\np ret"}, {"source_code": "class A\n def initialize(n, x)\n @n = n\n @x = x\n @fields = Array.new(10**5) { 0 }\n end\n\n def setup(movements)\n movements.each do |l, r|\n (l..r).each { |idx| @fields[idx - 1] = 1 }\n end\n end\n\n def minimum_number_of_minutes\n idx = 0\n minutes = []\n\n while (idx <= @fields.length)\n @fields[idx..(idx + @x - 1)].tap do |arr|\n if arr.any? { |a| a == 1 }\n minutes << arr.rindex(1) + 1\n idx += minutes.last\n else\n minutes << 0\n idx += @x\n end\n end\n end\n\n minutes.inject(0, :+)\n end\nend\n\nn, x = gets.chomp.split().map(&:to_i)\nmovements = [].tap do |arr|\n n.times { arr << gets.chomp.split().map(&:to_i) }\nend\n\na = A.new(n, x)\na.setup(movements)\nputs a.minimum_number_of_minutes\n"}, {"source_code": "n,x=gets.chomp.split.map(&:to_i)\nlol,i=0,1\nn.times do\n\tl,r=gets.chomp.split.map(&:to_i)\n\tlol+= r-l + (l-i)%x + 1\n\ti=r+1\nend\nputs lol"}, {"source_code": "n, x = gets.chomp.split(/ /).map(&:to_i)\nwatch = 0\nskip = 1\nn.times do\n l, r = gets.chomp.split(/ /).map(&:to_i)\n skip += x while skip + x <= l\n if skip <= l\n while skip <= r\n watch += 1\n skip += 1\n end\n end\nend\nputs watch\n"}, {"source_code": "n,x=gets.split.map{|e| e.to_i}\nt=1\nans=0\nn.times{\n\tl,r=gets.split.map{|e| e.to_i}\n\twhile t+x<=l\n\t\tt+=x\n\tend\n\tans+=r-t+1\n\tt=r+1\n}\nputs ans\n\n"}, {"source_code": "#!/usr/bin/ruby\ntokens = STDIN.gets.chomp.split(\" \").map(&:to_i)\nn = tokens[0]\nx = tokens[1]\nminute = 1\nans = 0\nn.times do\n l, r = STDIN.gets.chomp.split(\" \").map(&:to_i)\n while minute + x <= l do\n minute += x\n end\n ans += r - minute + 1\n minute = r + 1\nend\n\nputs ans\n"}, {"source_code": "n,x = gets.split(\" \").map(&:to_i)\nminutes_watched = 0\ncurrent = 1\nn.times do\n\tl,r = gets.split(\" \").map(&:to_i)\n\twhile current + x <= l\n\t\tcurrent += x\n\tend\n\tminutes_watched += l - current\n\tcurrent = r + 1\n\tminutes_watched += r + 1 - l\n\t# puts \"current #{current} minutes_watched #{minutes_watched}\"\nend\n\nputs minutes_watched"}, {"source_code": "n, x = gets.chomp.split(' ').map(&:to_i)\nc = 1\ntotal = 0\nn.times do |i|\n l, r = gets.chomp.split(' ').map(&:to_i)\n while c + x <= l do c += x end\n total += r-l + 1\n total += l - c\n c = r + 1\nend\nputs total"}, {"source_code": "n,x = gets.split.map &:to_i\nputs n.times.map{gets.split.map &:to_i}.unshift([0,0]).each_cons(2).map{|p,n| (n[0]-p[1]-1)%x+n[1]-n[0]+1}.inject(:+)"}, {"source_code": "n, x = gets.split.map(&:to_i)\na = n.times.map{gets.split.map(&:to_i)}\nans = 0\na.each{|x, y| ans += y + 1 - x}\nj = 1\nn.times do |i|\n ans += (a[i][0] - j) % x\n j = a[i][1] + 1\nend\nputs ans\n"}, {"source_code": "n, x = gets.split.map(&:to_i)\na = n.times.map{gets.split.map(&:to_i)}\nans = 0\nj = 1\na.each do |s, e|\n ans += (s - j) % x\n ans += e - s + 1\n j = e + 1\nend\nputs ans\n"}, {"source_code": "read=lambda{ gets.split(\" \").map{|x| x.to_i} }\nn,x=read.call\n#puts \"#{n} #{x}\"\nans=0\nnow=1\nfor _ in 0...n\n l,r=read.call\n ans+=(l-now)%x+r-l+1\n now=r+1\nend\nputs ans"}, {"source_code": "n, x = gets.split(' ').map(&:to_i)\npre = 0\nans = 0\nn.times do\n l, r = gets.split(' ').map(&:to_i)\n ans += (l - pre - 1) % x + r - l + 1\n pre = r\nend\nprint ans, \"\\n\";\n"}, {"source_code": "class Stu\n\tattr_accessor:a \n\tattr_accessor:b \nend\n\n#File.open(\"2.txt\",\"r\") do|file|\ndata=Array.new\nh=Array.new\n#str=file.gets.to_s\nstr=gets.to_s\nh=str.split\nn=h[0].to_i\nx=h[1].to_i\ni=0\nwhile ia*x\n ans+=finish-a*x\n else\n ans+=(b-a)*x\n end\n finish=b*x\nend\nputs ans"}, {"source_code": "read=lambda{ gets.split(\" \").map{|x| x.to_i} }\nn,x=read.call\n#puts \"#{n} #{x}\"\nans=0\nnow=1\nfor _ in 0...n\n l,r=read.call\n ans+=(l-now)%x+(r-l)+1\nend\nputs ans"}, {"source_code": "n, x = gets.split(' ').map(&:to_i)\npre = 1\nans = 0\nn.times do\n l, r = gets.split(' ').map(&:to_i)\n ans += (l - pre) % x + r - l\n pre = r\nend\nprint ans + 1, \"\\n\";\n"}], "src_uid": "ac33b73da5aaf2139b348a9c237f93a4"} {"nl": {"description": "Shubham has an array $$$a$$$ of size $$$n$$$, and wants to select exactly $$$x$$$ elements from it, such that their sum is odd. These elements do not have to be consecutive. The elements of the array are not guaranteed to be distinct.Tell him whether he can do so.", "input_spec": "The first line of the input contains a single integer $$$t$$$ $$$(1\\le t \\le 100)$$$ — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ $$$(1 \\le x \\le n \\le 1000)$$$ — the length of the array and the number of elements you need to choose. The next line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ $$$(1 \\le a_i \\le 1000)$$$ — elements of the array.", "output_spec": "For each test case, print \"Yes\" or \"No\" depending on whether it is possible to choose $$$x$$$ elements such that their sum is odd. You may print every letter in any case you want.", "sample_inputs": ["5\n1 1\n999\n1 1\n1000\n2 1\n51 50\n2 2\n51 50\n3 3\n101 102 103"], "sample_outputs": ["Yes\nNo\nYes\nYes\nNo"], "notes": "NoteFor $$$1$$$st case: We must select element $$$999$$$, and the sum is odd.For $$$2$$$nd case: We must select element $$$1000$$$, so overall sum is not odd.For $$$3$$$rd case: We can select element $$$51$$$.For $$$4$$$th case: We must select both elements $$$50$$$ and $$$51$$$  — so overall sum is odd.For $$$5$$$th case: We must select all elements  — but overall sum is not odd."}, "positive_code": [{"source_code": "# cook your code here\nt = gets.to_i\nuntil t == 0\n x,y = gets.chomp.split().map { |e| e.to_i }\n arr = gets.chomp.split().map { |e| e.to_i }\n odd = 0;even = 0;\n for i in 0...arr.length()\n if(arr[i]%2 == 1)\n odd+=1;\n else\n even+=1;\n end\n end\n #print \"%d %d\" % [odd,even]\n #puts\n if(odd == 0)\n puts \"No\"\n else\n y-=1;\n odd-=1;\n if(y%2 == 0)\n if(odd%2 == 0)\n odd = odd\n else\n odd-=1\n end\n if(odd >= y or even >= (y-odd))\n puts \"Yes\"\n else\n puts \"No\"\n end\n else\n if(even == 0)\n puts \"No\"\n else\n y-=1\n even-=1\n if(odd%2 == 0)\n odd = odd\n else\n odd-=1\n end\n if(odd >= y or even >= (y-odd))\n puts \"Yes\"\n else\n puts \"No\"\n end\n end\n end\n end\n t-=1\nend"}, {"source_code": "num_tests = gets.chomp.to_i\n\ndef odd_selection(n,arr)\n odds = evens = 0\n arr.each { |el| el.odd? ? odds += 1 : evens += 1 }\n return \"No\" if evens == 0 && n.even?\n (n - max_odd_num(odds) <= evens && odds > 0) ? \"Yes\" : \"No\"\nend\n\ndef max_odd_num(num)\n return num.odd? ? num : num - 1\nend\n\ni = 0\nwhile i < num_tests\n a,b = gets.chomp.split(\" \").map(&:to_i)\n arr = gets.chomp.split(\" \").map(&:to_i)\n puts odd_selection(b,arr)\n i += 1\nend\n"}, {"source_code": "\nt = 0\nt = gets.chomp.to_i\n#puts \"Value of t is \",t\nwhile t > 0\n i = 0\n num = 0\n n = 0 \n x = 0\n odd = 0\n n, x = gets.chomp.split(' ').map{|a| a.to_i}\n ar = gets.chomp.split(' ').map{|a| a.to_i}\n for i in 0..n-1\n #puts ar[i]\n if ar[i]%2 != 0\n odd = odd + 1\n end\n end\n #puts \"The value of odd is\", odd\n if odd >= 1 && !(x==n && odd%2 ==0) && !(n==odd && x%2==0 )\n puts \"YES\"\n else\n puts \"NO\"\n end\nt = t-1\nend\n\n\n\n\n"}, {"source_code": "gets.to_i.times{\n n,x=gets.split.map(&:to_i)\n a=b=0\n gets.split.map{|x|x.to_i%2==0 ? b+=1:a+=1}\n puts 1.step(a, 2).any?{|i| x-i>=0 and x-i<=b} ? \"Yes\" : \"No\"\n}\n"}, {"source_code": "def analyze(a, x)\n even_count = 0\n odd_count = 0\n a.each do |item|\n if item % 2 == 0\n even_count +=1\n else\n odd_count +=1\n end \n end\n \n return odd_count > 0 if x <= 1\n \n \n return false if odd_count < 1\n\n \n\n if x % 2 == 0\n return false if even_count < 1\n\n odd_count -= 1\n even_count -= 1\n\n odd_count / 2 + even_count / 2 >= (x -2) / 2\n else\n odd_count -= 1\n\n odd_count / 2 + even_count / 2 >= (x -1) / 2\n end \nend\n\nt = gets.chomp.to_i\nres = []\n(1..t).each do |_t|\n n, x = gets.chomp.split(' ').map{|a| a.to_i}\n ar = gets.chomp.split(' ').map{|a| a.to_i}\n res << analyze(ar, x)\nend\n \nres.each do |r|\n puts (r ? 'YES' : 'NO')\nend"}, {"source_code": "def solve\n _, k = gets.strip.split(' ').map(&:to_i)\n arr = gets.strip.split(' ').map(&:to_i)\n ev = arr.select { |e| e.even? }\n od = arr.select { |e| e.odd? }\n while !od.empty?\n if od.size.odd? && od.size <= k\n break\n end\n od.pop\n end\n if od.size + ev.size >= k && !od.size.zero?\n puts \"YES\"\n else\n puts \"NO\"\n end\nend\n\ngets.strip.to_i.times { solve }\n"}, {"source_code": "def solve\n _, k = gets.strip.split(' ').map(&:to_i)\n arr = gets.strip.split(' ').map(&:to_i)\n ev = arr.select { |e| e.even? }\n od = arr.select { |e| e.odd? }\n while !od.empty?\n if od.size.odd? && od.size <= k\n break\n end\n od.pop\n end\n if od.size + ev.size >= k && !od.size.zero?\n puts \"YES\"\n else\n puts \"NO\"\n end\nend\n\ngets.strip.to_i.times { solve }\n"}], "negative_code": [{"source_code": "# cook your code here\nt = gets.to_i\nuntil t == 0\n x,y = gets.chomp.split().map { |e| e.to_i }\n arr = gets.chomp.split().map { |e| e.to_i }\n odd = 0;even = 0;\n for i in 0...arr.length()\n if(arr[i]%2 == 1)\n odd+=1;\n else\n even+=1;\n end\n end\n #print \"%d %d\" % [odd,even]\n #puts\n if(odd == 0)\n puts \"No\"\n else\n y-=1;\n odd-=1;\n if(even >= y)\n puts \"Yes\"\n else\n if((y-even)%2)\n y = y - even + 1;\n else\n y = y - even;\n end\n if(odd >= y and y%2 == 0)\n puts \"Yes\"\n else\n puts \"No\"\n end\n end\n end\n t-=1\nend"}, {"source_code": "# cook your code here\nt = gets.to_i\nuntil t == 0\n x,y = gets.chomp.split().map { |e| e.to_i }\n arr = gets.chomp.split().map { |e| e.to_i }\n odd = 0;even = 0;\n for i in 0...arr.length()\n if(arr[i]%2 == 1)\n odd+=1;\n else\n even+=1;\n end\n end\n #print \"%d %d\" % [odd,even]\n #puts\n if(odd == 0)\n puts \"No\"\n else\n y-=1;\n odd-=1;\n if(even >= y)\n puts \"Yes\"\n else\n if((y-even)%2 == 0)\n y = y - even + 1;\n else\n y = y - even;\n end\n if(odd >= y and y%2 == 0)\n puts \"Yes\"\n else\n puts \"No\"\n end\n end\n end\n t-=1\nend"}, {"source_code": "# cook your code here\nt = gets.to_i\nuntil t == 0\n x,y = gets.chomp.split().map { |e| e.to_i }\n arr = gets.chomp.split().map { |e| e.to_i }\n odd = 0;even = 0;\n for i in 0...arr.length()\n if(arr[i]%2 == 1)\n odd+=1;\n else\n even+=1;\n end\n end\n #print \"%d %d\" % [odd,even]\n #puts\n if(odd == 0)\n puts \"No\"\n else\n y-=1;\n odd-=1;\n if(even >= y)\n puts \"Yes\"\n else\n if((y-even)%2 == 1)\n y = y - even + 1;\n else\n y = y - even;\n end\n # puts y\n if(odd >= y and y%2 == 0)\n puts \"Yes\"\n else\n puts \"No\"\n end\n end\n end\n t-=1\nend"}, {"source_code": "# cook your code here\nt = gets.to_i\nuntil t == 0\n x,y = gets.chomp.split().map { |e| e.to_i }\n arr = gets.chomp.split().map { |e| e.to_i }\n odd = 0;even = 0;\n for i in 0...arr.length()\n if(arr[i]%2 == 1)\n odd+=1;\n else\n even+=1;\n end\n end\n #print \"%d %d\" % [odd,even]\n #puts\n if(odd == 0)\n puts \"No\"\n else\n y-=1;\n odd-=1;\n if(even >= y)\n puts \"Yes\"\n else\n if((y-even)%2)\n y = y - even + 1;\n else\n y = y - even;\n end\n if(odd >= y)\n puts \"Yes\"\n else\n puts \"No\"\n end\n end\n end\n t-=1\nend"}, {"source_code": "num_tests = gets.chomp.to_i\n\ndef odd_selection(n,arr)\n odds = evens = 0\n arr.each { |el| el.odd? ? odds += 1 : evens += 1 }\n (n - max_odd_num(odds) <= evens && odds > 0) ? \"Yes\" : \"No\"\nend\n\ndef max_odd_num(num)\n return num.odd? ? num : num - 1\nend\n\ni = 0\nwhile i < num_tests\n a,b = gets.chomp.split(\" \").map(&:to_i)\n arr = gets.chomp.split(\" \").map(&:to_i)\n puts odd_selection(b,arr)\n i += 1\nend\n"}, {"source_code": "num_tests = gets.chomp.to_i\n\ndef odd_selection(n,arr)\n odds = evens = 0\n arr.each { |el| el.odd? ? odds += 1 : evens += 1 }\n (n - max_odd_num(odds) <= evens && odds > 0) ? \"Yes\" : \"No\"\nend\n\ndef max_odd_num(num)\n return num.odd? ? num : num - 1\nend\n\ni = 0\nwhile i < num_tests\n a,b = gets.chomp.split(\" \").map(&:to_i)\n arr = gets.chomp.split(\" \").map(&:to_i)\n p odd_selection(b,arr)\n i += 1\nend\n"}, {"source_code": "\nt = 0\nt = gets.to_i\n\nwhile t > 0\n odd = 0\n n = 0 \n x = 0\n n = gets.to_i\n x = gets.to_i\n for i in 1..n\n a = gets.to_i\n if a%2 != 0\n odd = odd + 1\n end\n end\n if odd >= 1 && !(x==n && odd%2 ==0) && !(n==odd && x%2==0 )\n puts \"YES\"\n else\n puts \"NO\"\n end\nt = t-1\nend\n\n\n\n\n"}, {"source_code": "gets.to_i.times{\n n,x=gets.split.map(&:to_i)\n a=b=0\n gets.split.map{|x|x.to_i%2==0 ? b+=1:a+=1}\n puts 1.step(a, 2).any?{|i| x-i<=b} ? \"Yes\" : \"No\"\n}\n"}, {"source_code": "def analyze(a, x)\n even_count = 0\n odd_count = 0\n a.each do |item|\n if item % 2 == 0\n even_count +=1\n else\n odd_count +=1\n end \n end\n\n return odd_count > 0 if x <= 1\n\n if x % 2 == 1\n # x is odd\n even_count -= 1 if even_count % 2 == 1\n\n even_count + odd_count >= x\n else\n\n # x is even\n \n return false if odd_count < 1 || even_count < 1\n\n # one pair\n odd_count -= 1\n even_count -= 1\n\n # rest pairs\n\n\n even_count -= 1 if even_count % 2 == 1\n odd_count -= 1 if odd_count % 2 == 1\n\n even_count + odd_count >= x - 2\n end \nend\n\n\n\nt = gets.chomp.to_i\nres = []\n(1..t).each do |_t|\n n, x = gets.chomp.split(' ').map{|a| a.to_i}\n ar = gets.chomp.split(' ').map{|a| a.to_i}\n res << analyze(ar, x)\nend\n\nres.each do |r|\n puts (r ? 'YES' : 'NO')\nend"}, {"source_code": "def analyze(a, x)\n even_count = 0\n odd_count = 0\n a.each do |item|\n if item % 2 == 0\n even_count +=1\n else\n odd_count +=1\n end \n end\n \n return odd_count > 0 if x <= 1\n \n \n return false if odd_count < 1\n\n \n\n if x % 2 == 0\n odd_count -= 1\n even_count -= 1\n\n odd_count / 2 + even_count / 2 >= (x -2) / 2\n else\n odd_count -= 1\n\n odd_count / 2 + even_count / 2 >= (x -1) / 2\n end \nend\n\nt = gets.chomp.to_i\nres = []\n(1..t).each do |_t|\n n, x = gets.chomp.split(' ').map{|a| a.to_i}\n ar = gets.chomp.split(' ').map{|a| a.to_i}\n res << analyze(ar, x)\nend\n \nres.each do |r|\n puts (r ? 'YES' : 'NO')\nend"}, {"source_code": "def analyze(a, x)\n even_count = 0\n odd_count = 0\n a.each do |item|\n if item % 2 == 0\n even_count +=1\n else\n odd_count +=1\n end \n end\n \n return odd_count > 0 if x <= 1\n \n \n return false if odd_count < 1\n\n odd_count -= 1;\n\n odd_count / 2 + even_count / 2 + even_count % 2 >= x -1\nend\n\nt = gets.chomp.to_i\nres = []\n(1..t).each do |_t|\n n, x = gets.chomp.split(' ').map{|a| a.to_i}\n ar = gets.chomp.split(' ').map{|a| a.to_i}\n res << analyze(ar, x)\nend\n \nres.each do |r|\n puts (r ? 'YES' : 'NO')\nend"}, {"source_code": "def solve\n _, k = gets.strip.split(' ').map(&:to_i)\n arr = gets.strip.split(' ').map(&:to_i)\n ev = arr.select { |e| e.even? }\n od = arr.select { |e| e.odd? }\n od.pop if od.size.even? && !od.size.zero?\n if od.size + ev.size >= k && !od.size.zero?\n puts \"YES\"\n else\n puts \"NO\"\n end\nend\n\n\n\ngets.strip.to_i.times { solve }\n"}], "src_uid": "afce38aa7065da88d824d1d981b5b338"} {"nl": {"description": "You've got a string $$$a_1, a_2, \\dots, a_n$$$, consisting of zeros and ones.Let's call a sequence of consecutive elements $$$a_i, a_{i + 1}, \\ldots, a_j$$$ ($$$1\\leq i\\leq j\\leq n$$$) a substring of string $$$a$$$. You can apply the following operations any number of times: Choose some substring of string $$$a$$$ (for example, you can choose entire string) and reverse it, paying $$$x$$$ coins for it (for example, «0101101» $$$\\to$$$ «0111001»); Choose some substring of string $$$a$$$ (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and ones — by zeros), paying $$$y$$$ coins for it (for example, «0101101» $$$\\to$$$ «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring.What is the minimum number of coins you need to spend to get a string consisting only of ones?", "input_spec": "The first line of input contains integers $$$n$$$, $$$x$$$ and $$$y$$$ ($$$1 \\leq n \\leq 300\\,000, 0 \\leq x, y \\leq 10^9$$$) — length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string $$$a$$$ of length $$$n$$$, consisting of zeros and ones.", "output_spec": "Print a single integer — the minimum total cost of operations you need to spend to get a string consisting only of ones. Print $$$0$$$, if you do not need to perform any operations.", "sample_inputs": ["5 1 10\n01000", "5 10 1\n01000", "7 2 3\n1111111"], "sample_outputs": ["11", "2", "0"], "notes": "NoteIn the first sample, at first you need to reverse substring $$$[1 \\dots 2]$$$, and then you need to invert substring $$$[2 \\dots 5]$$$. Then the string was changed as follows:«01000» $$$\\to$$$ «10000» $$$\\to$$$ «11111».The total cost of operations is $$$1 + 10 = 11$$$.In the second sample, at first you need to invert substring $$$[1 \\dots 1]$$$, and then you need to invert substring $$$[3 \\dots 5]$$$. Then the string was changed as follows:«01000» $$$\\to$$$ «11000» $$$\\to$$$ «11111».The overall cost is $$$1 + 1 = 2$$$.In the third example, string already consists only of ones, so the answer is $$$0$$$."}, "positive_code": [{"source_code": "N, X, Y = gets.split.map(&:to_i)\nnum = gets.chomp.split('1').count{|s| s.length>0}\nif num == 0\n puts 0\nelse\n puts Y + (num-1) * [X, Y].min\nend"}, {"source_code": "n, x, y = gets.strip.split.map(&:to_i)\nstr = gets.strip\ncstr = str[0]\nstr.each_char do |c|\n cstr << c if c != cstr[-1]\nend\n\nzc = [y]\noc = [0]\nn = cstr.size\n1.upto(n).each do |i|\n oc << zc[i-1]\n if i%2==1\n zc << zc[i-1]\n else\n zc << [(y+zc[i-2]), (x+zc[i-2])].min\n end\nend\n\nputs (str[0]=='0' ? zc[n-1] : oc[n-1])"}], "negative_code": [{"source_code": "n, x, y = gets.strip.split.map(&:to_i)\nstr = gets.strip\n\nnzb = 0\n0.upto(n-2).each do |i|\n nzb += 1 if str[i] == '0' && str[i+1]=='1'\nend\nnzb += 1 if n>1 && str[-1] =='0'\n\nans = nzb * y\n1.upto(nzb-1).each do |nx|\n ans = [ans, (x*nx) + ((nzb-nx)*y)].min\nend\nputs ans"}], "src_uid": "b267f69cc4af3e319fc59e3ccd8b1c9d"} {"nl": {"description": "You are given an integer array of length $$$n$$$.You have to choose some subsequence of this array of maximum length such that this subsequence forms a increasing sequence of consecutive integers. In other words the required sequence should be equal to $$$[x, x + 1, \\dots, x + k - 1]$$$ for some value $$$x$$$ and length $$$k$$$.Subsequence of an array can be obtained by erasing some (possibly zero) elements from the array. You can erase any elements, not necessarily going successively. The remaining elements preserve their order. For example, for the array $$$[5, 3, 1, 2, 4]$$$ the following arrays are subsequences: $$$[3]$$$, $$$[5, 3, 1, 2, 4]$$$, $$$[5, 1, 4]$$$, but the array $$$[1, 3]$$$ is not.", "input_spec": "The first line of the input containing integer number $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) — the length of the array. The second line of the input containing $$$n$$$ integer numbers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$) — the array itself.", "output_spec": "On the first line print $$$k$$$ — the maximum length of the subsequence of the given array that forms an increasing sequence of consecutive integers. On the second line print the sequence of the indices of the any maximum length subsequence of the given array that forms an increasing sequence of consecutive integers.", "sample_inputs": ["7\n3 3 4 7 5 6 8", "6\n1 3 5 2 4 6", "4\n10 9 8 7", "9\n6 7 8 3 4 5 9 10 11"], "sample_outputs": ["4\n2 3 5 6", "2\n1 4", "1\n1", "6\n1 2 3 7 8 9"], "notes": "NoteAll valid answers for the first example (as sequences of indices): $$$[1, 3, 5, 6]$$$ $$$[2, 3, 5, 6]$$$ All valid answers for the second example: $$$[1, 4]$$$ $$$[2, 5]$$$ $$$[3, 6]$$$ All valid answers for the third example: $$$[1]$$$ $$$[2]$$$ $$$[3]$$$ $$$[4]$$$ All valid answers for the fourth example: $$$[1, 2, 3, 7, 8, 9]$$$ "}, "positive_code": [{"source_code": "\nNode = Struct.new(:idx, :len, :next) do\n def append(idx)\n Node.new(idx, self.len + 1, self)\n end\nend\n\nEMPTY = Node.new(nil, 0, nil)\n\n\ndef find_max_streak(as)\n streaks = {}\n max_streak = EMPTY\n\n as.each.with_index do |a, q|\n i = q + 1\n\n streak = (streaks[a-1] || EMPTY).append(i)\n\n streaks[a] = streak\n\n if max_streak.len < streak.len\n max_streak = streak\n end\n\n end\n\n max_streak\nend\n\ndef solution\n\n read_int\n\n as = read_ints\n\n ms = find_max_streak(as)\n\n rev = []\n crt = ms\n while crt.idx\n rev.unshift crt.idx\n crt = crt.next\n end\n\n puts rev.size\n puts rev.join(' ')\n\nend\n\ndef read_int\n gets.to_i\nend\n\ndef read_ints\n gets.split.map(&:to_i)\nend\n\ndef read_string\n gets.chomp\nend\n\nsolution unless ENV['TEST__MODE']\n\n"}], "negative_code": [{"source_code": "\ndef solution\n\n indices = {}\n streaks = {}\n\n max_streak = 0\n max_streak_tail = nil\n\n read_int\n\n as = read_ints\n\n as.each.with_index do |a, q|\n i = q + 1\n\n streak = (streaks[a-1] || 0) + 1\n\n streaks[a] = streak\n indices[a] = indices[a] || i\n\n next if (streak[a+1] || 0) > streak\n\n if max_streak < streak\n max_streak = streak\n max_streak_tail = a\n end\n\n end\n\n max_streak_head = max_streak_tail - max_streak + 1\n\n indices = (max_streak_head..max_streak_tail).map{ |a| indices[a] }\n\n puts max_streak\n puts indices.join(' ')\n\nend\n\ndef read_int\n gets.to_i\nend\n\ndef read_ints\n gets.split.map(&:to_i)\nend\n\ndef read_string\n gets.chomp\nend\n\nsolution unless ENV['TEST__MODE']\n\n"}, {"source_code": "\n\ndef find_max_streak(as)\n indices = {}\n streaks = {}\n max_streak = 0\n max_streak_tail = nil\n\n as.each.with_index do |a, q|\n i = q + 1\n\n streak = (streaks[a-1] || 0) + 1\n\n if (streaks[a] || 0) < streak\n streaks[a] = streak\n indices[a] = i\n end\n\n if max_streak < streak\n max_streak = streak\n max_streak_tail = a\n end\n\n end\n\n max_streak_head = max_streak_tail - max_streak + 1\n\n indices = (max_streak_head..max_streak_tail).map{ |a| indices[a] }\nend\n\ndef solution\n\n read_int\n\n as = read_ints\n\n ms = find_max_streak(as)\n\n puts ms.size\n puts ms.join(' ')\n\nend\n\ndef read_int\n gets.to_i\nend\n\ndef read_ints\n gets.split.map(&:to_i)\nend\n\ndef read_string\n gets.chomp\nend\n\nsolution unless ENV['TEST__MODE']\n\n"}, {"source_code": "def solution\n\n indices = {}\n streaks = {}\n\n max_streak = 0\n max_streak_tail = nil\n\n read_int\n\n as = read_ints\n\n as.each.with_index do |a, q|\n i = q + 1\n\n streak = (streaks[a-1] || 0) + 1\n\n streaks[a] = streak\n indices[a] = i\n\n if max_streak < streak\n max_streak = streak\n max_streak_tail = a\n end\n\n end\n\n max_streak_head = max_streak_tail - max_streak + 1\n\n indices = (max_streak_head..max_streak_tail).map{ |a| indices[a] }\n\n puts max_streak\n puts indices.join(' ')\n\nend\n\ndef read_int\n gets.to_i\nend\n\ndef read_ints\n gets.split.map(&:to_i)\nend\n\ndef read_string\n gets.chomp\nend\n\nsolution unless ENV['TEST__MODE']\n"}, {"source_code": "\ndef solution\n\n indices = {}\n streaks = {}\n\n max_streak = 0\n max_streak_tail = nil\n\n read_int\n\n as = read_ints\n\n as.each.with_index do |a, q|\n i = q + 1\n\n streak = (streaks[a-1] || 0) + 1\n\n streaks[a] = streak\n indices[a] = i unless indices[a]\n\n if max_streak < streak\n max_streak = streak\n max_streak_tail = a\n end\n\n end\n\n max_streak_head = max_streak_tail - max_streak + 1\n\n indices = (max_streak_head..max_streak_tail).map{ |a| indices[a] }\n\n puts max_streak\n puts indices.join(' ')\n\nend\n\ndef read_int\n gets.to_i\nend\n\ndef read_ints\n gets.split.map(&:to_i)\nend\n\ndef read_string\n gets.chomp\nend\n\nsolution unless ENV['TEST__MODE']\n\n"}], "src_uid": "70986d3b1ff66ac612e8841a6049866d"} {"nl": {"description": "Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.Petya has two strings a and b of the same length n. The strings consist only of lucky digits. Petya can perform operations of two types: replace any one digit from string a by its opposite (i.e., replace 4 by 7 and 7 by 4); swap any pair of digits in string a. Petya is interested in the minimum number of operations that are needed to make string a equal to string b. Help him with the task.", "input_spec": "The first and the second line contains strings a and b, correspondingly. Strings a and b have equal lengths and contain only lucky digits. The strings are not empty, their length does not exceed 105.", "output_spec": "Print on the single line the single number — the minimum number of operations needed to convert string a into string b.", "sample_inputs": ["47\n74", "774\n744", "777\n444"], "sample_outputs": ["1", "1", "3"], "notes": "NoteIn the first sample it is enough simply to swap the first and the second digit.In the second sample we should replace the second digit with its opposite.In the third number we should replace all three digits with their opposites."}, "positive_code": [{"source_code": "a, b = 2.times.map{gets.chars.to_a}\nc = 0\na.zip(b){|i, j| c += 1 if i != j}\np c + (a.count('4') - b.count('4')).abs >> 1\n"}, {"source_code": "$a = gets.chomp\n$b = gets.chomp\nplmi = 0\ndiffcnt = 0\nfor i in 0..$a.length\n ac = $a.slice(i,1)\n bc = $b.slice(i,1)\n next if ac == bc\n diffcnt += 1\n if ac == \"4\"\n plmi += 1\n else\n plmi -= 1\n end\nend\nputs (diffcnt-plmi.abs)/2+plmi.abs\n"}, {"source_code": "a,b=2.times.map{gets.chomp.chars.to_a}\nc4=c7=0\na.size.times{|i|\n\tunless a[i]==b[i]\n\t\ta[i]==?4 ? c4+=1 : c7+=1\n\tend\n}\np [c4,c7].max"}, {"source_code": "a = gets\nb = gets\nea = a.each_char\n\nh = {:aa => 0, :bb => 0}\nb.each_char do |bb|\n aa = ea.next\n next if aa == bb\n aa == \"4\" ? h[:aa] += 1 : h[:bb] += 1\nend\n\nputs [h[:aa], h[:bb]].max\n"}, {"source_code": "a=gets.bytes.to_a\nb=gets.bytes.to_a\ncnt=[0,0]\na.length.times{|i| cnt[a[i]==52?1:0]+=1 if a[i]!=b[i]}\np cnt[0]+cnt[1]-cnt.min\n"}, {"source_code": "a=gets.split[0].bytes.to_a\nb=gets.split[0].bytes.to_a\nn=a.length\ninput=Array.new\nn.times{|i|\n input.push([a[i]==52?1:0,b[i]==52?1:0])\n}\ncnt1=0\ncnt2=0\ninput.each{|t|\n cnt1+=1 if t[0]!=t[1] && t[0]==1\n cnt2+=1 if t[0]!=t[1] && t[0]==0\n}\np [cnt1,cnt2].min+(cnt1+cnt2)-[cnt1,cnt2].min*2\n"}, {"source_code": "a=gets\nb=gets\nx = a.split('').zip(b.split('')).inject([]) { |m, (x, y)| x != y ? m << x : m }\np x.length - %w[4 7].map { |d| x.count(d) }.min"}, {"source_code": "a=gets.chomp.split(\"\").map{|i|i.to_i}\nb=gets.chomp.split(\"\").map{|i|i.to_i}\nc=(a.count(4)-b.count(4)).abs\ni=0\nl=0\n\nif c>0\nloop do\nif a[i]!=b[i]\na[i]=b[i]\nl+=1\nend\ni+=1\nbreak if l>=c\nend\nend\nlength=a.length\nswap=0\nloop do\nif a[i]!=b[i]\na[i]=b[i]\nswap+=1\nend\ni+=1\nbreak if i>=length\nend\n\nputs\"#{c+swap/2}\""}, {"source_code": "a, b = 2.times.map{gets.chars.to_a}\nc = 0\na.zip(b){|i, j| c += 1 if i != j}\np c + (a.count('4') - b.count('4')).abs >> 1\n__END__"}, {"source_code": "a=gets.chomp.split(\"\").map{|i|i.to_i}\nb=gets.chomp.split(\"\").map{|i|i.to_i}\nc=(a.count(4)-b.count(4)).abs\ni=0\nl=0\n\nif c>0\nloop do\nif a[i]!=b[i]\na[i]=b[i]\nl+=1\nend\ni+=1\nbreak if l>=c\nend\nend\nlength=a.length\nswap=0\nloop do\nif a[i]!=b[i]\na[i]=b[i]\nswap+=1\nend\ni+=1\nbreak if i>=length\nend\n\nputs\"#{c+swap/2}\""}, {"source_code": "a=gets.chomp\nb=gets.chomp\ncounts=[0,0]\na.split(\"\").each_index do |i|\n if a[i]==\"4\" && b[i]==\"7\"\n counts[0]=counts[0]+1\n elsif a[i]==\"7\" && b[i]==\"4\"\n counts[1]=counts[1]+1\n end\nend\nputs counts.max \n"}, {"source_code": "a = gets.chomp\nb = gets.chomp\nx = 0\ny = 0\n(0...a.size).each do |i|\n if a[i] == '7' and b[i] == '4'\n x += 1\n elsif a[i] == '4' and b[i] == '7'\n y += 1\n end\nend\nputs [x,y].max\n"}, {"source_code": "n = m = 0\na = gets.chomp.chars\nb = gets.chomp.chars\n(0...a.size).each { |i|\n if a[i] != b[i]\n if a[i] == '4'\n n += 1\n else\n m += 1\n end\n end\n}\nputs [n, m].max"}, {"source_code": "a, b = gets.chomp.split(\"\").map(&:to_i), gets.split(\"\").map(&:to_i)\nf, s = 0, 0\na.length.times{ |i|\n next if a[i] == b[i]\n if a[i]!=4\n f += 1\n else\n s += 1\n end\n}\np [f, s].min + (f-s).abs"}, {"source_code": "input = STDIN.read.split(\"\\n\")\nstr1, str2 = input[0].split(//).map(&:to_i), input[1].split(//).map(&:to_i)\n\nstr1.length.times {|x|\n\tif str1[x] == str2[x]\n\t\tstr1[x] = nil\n\t\tstr2[x] = nil\n\tend\n}\n\nn4 = str1.count(4)\nn7 = str1.count(7)\n\nmoves = n4 <= n7 ? n4 : n7\nSTDOUT.puts moves += (n4 - n7).abs"}], "negative_code": [{"source_code": "$a = gets.chomp\n$b = gets.chomp\nplmi = 0\ndiffcnt = 0\nfor i in 0..$a.length\n ac = $a.slice(i,1)\n bc = $b.slice(i,1)\n next if ac == bc\n diffcnt += 1\n if ac == \"4\"\n plmi += 1\n else\n plmi -= 1\n end\nend\nputs diffcnt/2+plmi.abs\n"}, {"source_code": "a = gets\nb = gets\nea = a.each_char\n\nret = 0\nb.each_char do |c|\n l = ea.next\n ret += 1 unless c == l\nend\n\nputs ret\n"}, {"source_code": "a=gets.bytes.to_a\nb=gets.bytes.to_a\ncnt=[0,0]\na.length.times{|i| cnt[a[i]==\"4\"?1:0]+=1 if a[i]!=b[i]}\np cnt[0]+cnt[1]-cnt.min\n"}, {"source_code": "input = STDIN.read.split(\"\\n\")\nstr1, str2 = input[0].split(//).map(&:to_i), input[1].split(//).map(&:to_i)\n\ncount = 0\n\nstr1.length.times {|x| str1.delete_at x if str1[x] == str2[x] }\n\nn4 = str1.count(4)\nn7 = str1.count(7)\n\nmoves = n4 <= n7 ? n4 : n7\nSTDOUT.puts moves += (n4 - n7).abs"}, {"source_code": "input = STDIN.read.split(\"\\n\")\nstr1, str2 = input[0], input[1]\n\ncount = 0\nskip = false\n\nstr1.length.times do |i|\n\n\tif str1[i] == str2[i] or skip\n\t\tskip = false\n\t\tnext\n\tend\n\n\tif str1[i + 1].nil?\n\t\tcount += 1\n\t\tbreak\n\tend\n\n\tif str1[i + 1] != str2[i + 1] and str1[i] != str1[i + 1]\n\t\tcount += 1\n\t\tskip = true\n\telse\n\t\tcount += 1\n\tend\n\n\nend\n\nSTDOUT.puts count"}], "src_uid": "2d1609b434262d30f6bd030d41a71bd5"} {"nl": {"description": "Peter got a new snow blower as a New Year present. Of course, Peter decided to try it immediately. After reading the instructions he realized that it does not work like regular snow blowing machines. In order to make it work, you need to tie it to some point that it does not cover, and then switch it on. As a result it will go along a circle around this point and will remove all the snow from its path.Formally, we assume that Peter's machine is a polygon on a plane. Then, after the machine is switched on, it will make a circle around the point to which Peter tied it (this point lies strictly outside the polygon). That is, each of the points lying within or on the border of the polygon will move along the circular trajectory, with the center of the circle at the point to which Peter tied his machine.Peter decided to tie his car to point P and now he is wondering what is the area of ​​the region that will be cleared from snow. Help him.", "input_spec": "The first line of the input contains three integers — the number of vertices of the polygon n (), and coordinates of point P. Each of the next n lines contains two integers — coordinates of the vertices of the polygon in the clockwise or counterclockwise order. It is guaranteed that no three consecutive vertices lie on a common straight line. All the numbers in the input are integers that do not exceed 1 000 000 in their absolute value.", "output_spec": "Print a single real value number — the area of the region that will be cleared. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .", "sample_inputs": ["3 0 0\n0 1\n-1 2\n1 2", "4 1 -1\n0 0\n1 2\n2 0\n1 1"], "sample_outputs": ["12.566370614359172464", "21.991148575128551812"], "notes": "NoteIn the first sample snow will be removed from that area: "}, "positive_code": [{"source_code": "def sqr(x)\n return x*x\nend\n\n\nn,$ox,$oy = gets.split.map(&:to_i)\nx = Array.new(n+1){|e|e=0}\ny = Array.new(n+1){|e|e=0}\n\n\ndef calc(x1,y1,x2,y2)\n cross = (x2-x1)*($ox-x1) + (y2-y1)*($oy-y1)\n return sqr($ox-x1) + sqr($oy-y1) if cross <= 0\n d2 = (x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)+0.0\n return sqr($ox-x2) + sqr($oy-y2) if cross >= d2\n r = cross/d2\n px = x1+(x2-x1)*r\n py = y1+(y2-y1)*r\n #p px,py\n return sqr($ox-px)+sqr($oy-py)\nend\n\ndismin=12100000000.0**2\ndismax=-1.0\n\nfor i in 1..n\n x[i],y[i] = gets.split.map(&:to_i)\n dis = sqr(x[i]-$ox)+sqr(y[i]-$oy)\n dismin = dis if (disdismax)\nend\nx[0],y[0]=x[n],y[n]\nfor i in 1..n\n dis = calc(x[i-1],y[i-1],x[i],y[i])\n dismin = dis if (disdismax)\nend\np (dismax-dismin)*Math::PI"}], "src_uid": "1d547a38250c7780ddcf10674417d5ff"} {"nl": {"description": "Vasya has recently got a job as a cashier at a local store. His day at work is $$$L$$$ minutes long. Vasya has already memorized $$$n$$$ regular customers, the $$$i$$$-th of which comes after $$$t_{i}$$$ minutes after the beginning of the day, and his service consumes $$$l_{i}$$$ minutes. It is guaranteed that no customer will arrive while Vasya is servicing another customer. Vasya is a bit lazy, so he likes taking smoke breaks for $$$a$$$ minutes each. Those breaks may go one after another, but Vasya must be present at work during all the time periods he must serve regular customers, otherwise one of them may alert his boss. What is the maximum number of breaks Vasya can take during the day?", "input_spec": "The first line contains three integers $$$n$$$, $$$L$$$ and $$$a$$$ ($$$0 \\le n \\le 10^{5}$$$, $$$1 \\le L \\le 10^{9}$$$, $$$1 \\le a \\le L$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$t_{i}$$$ and $$$l_{i}$$$ ($$$0 \\le t_{i} \\le L - 1$$$, $$$1 \\le l_{i} \\le L$$$). It is guaranteed that $$$t_{i} + l_{i} \\le t_{i + 1}$$$ and $$$t_{n} + l_{n} \\le L$$$.", "output_spec": "Output one integer  — the maximum number of breaks.", "sample_inputs": ["2 11 3\n0 1\n1 1", "0 5 2", "1 3 2\n1 2"], "sample_outputs": ["3", "2", "0"], "notes": "NoteIn the first sample Vasya can take $$$3$$$ breaks starting after $$$2$$$, $$$5$$$ and $$$8$$$ minutes after the beginning of the day.In the second sample Vasya can take $$$2$$$ breaks starting after $$$0$$$ and $$$2$$$ minutes after the beginning of the day.In the third sample Vasya can't take any breaks."}, "positive_code": [{"source_code": "n,l,a = gets.strip.split.map(&:to_i)\nintervals = [0]\n\nn.times do\n ti, li = gets.strip.split.map(&:to_i)\n intervals << ti\n intervals << ti + li\nend\nintervals << l\n\nans = 0\nim = intervals.size / 2\ni = 0\nim.times do\n s = intervals[i]\n e = intervals[i+1]\n ans += (e-s)/a\n i+=2\nend\nputs ans\n"}, {"source_code": "n,l,a = gets.split.map(&:to_i)\nans = 0\nt = [[0,0]] + n.times.map{gets.split.map(&:to_i)} + [[l,0]]\n\nfor i in 0..n\n ans += (t[i+1][0]-t[i][0]-t[i][1])/a\nend\n\nputs ans\n"}, {"source_code": "n,l,a = gets.split.map(&:to_i)\nans = 0\nt = [[0,0]] + n.times.map{gets.split.map(&:to_i)} + [[l,0]]\n\nfor i in 0..n\n ans += (t[i+1][0]-t[i][0]-t[i][1])/a\nend\n\nputs ans\n\n"}, {"source_code": "nla = gets.chomp.split(\" \").map(&:to_i)\n\nn = nla[0]\nl = nla[1]\na = nla[2]\n\narr = []\nn.times do\n arr.push gets.chomp.split(\" \").map(&:to_i)\nend\n\narr.sort_by{|x| x[0]}\n\nif n==0\n puts l/a\n exit\nend\n\nfirst = arr[0][0]/a\nlast = (l-(arr[n-1][0]+arr[n-1][1]))/a\n\nans = 0\n\narr.each_with_index do |x,i|\n next if i==n-1\n ans += (arr[i+1][0]-(x[0]+x[1]))/a\nend\n\nans += (first+last)\nputs ans\n"}], "negative_code": [{"source_code": "nla = gets.chomp.split(\" \").map(&:to_i)\n\nn = nla[0]\nl = nla[1]\na = nla[2]\n\narr = []\nn.times do\n arr.push gets.chomp.split(\" \").map(&:to_i)\nend\n\narr.sort_by{|x| x[0]}\n\nif n==0\n puts l/a\n exit\nend\n\nfirst = arr[0][0]/a\nlast = (l-arr[n-1][1])/a\n\nans = 0\n\narr.each_with_index do |x,i|\n next if i==n-1\n ans += (arr[i+1][0]-x[1])/a\nend\n\nans += (first+last)\nputs ans \n"}], "src_uid": "00acb3b54975820989a788b9389c7c0b"} {"nl": {"description": "Shohag has an integer sequence $$$a_1, a_2, \\ldots, a_n$$$. He can perform the following operation any number of times (possibly, zero): Select any positive integer $$$k$$$ (it can be different in different operations). Choose any position in the sequence (possibly the beginning or end of the sequence, or in between any two elements) and insert $$$k$$$ into the sequence at this position. This way, the sequence $$$a$$$ changes, and the next operation is performed on this changed sequence. For example, if $$$a=[3,3,4]$$$ and he selects $$$k = 2$$$, then after the operation he can obtain one of the sequences $$$[\\underline{2},3,3,4]$$$, $$$[3,\\underline{2},3,4]$$$, $$$[3,3,\\underline{2},4]$$$, or $$$[3,3,4,\\underline{2}]$$$.Shohag wants this sequence to satisfy the following condition: for each $$$1 \\le i \\le |a|$$$, $$$a_i \\le i$$$. Here, $$$|a|$$$ denotes the size of $$$a$$$.Help him to find the minimum number of operations that he has to perform to achieve this goal. We can show that under the constraints of the problem it's always possible to achieve this goal in a finite number of operations.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 200$$$)  — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 100$$$) — the initial length of the sequence. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$) — the elements of the sequence.", "output_spec": "For each test case, print a single integer  — the minimum number of operations needed to perform to achieve the goal mentioned in the statement.", "sample_inputs": ["4\n3\n1 3 4\n5\n1 2 5 7 4\n1\n1\n3\n69 6969 696969"], "sample_outputs": ["1\n3\n0\n696966"], "notes": "NoteIn the first test case, we have to perform at least one operation, as $$$a_2=3>2$$$. We can perform the operation $$$[1, 3, 4] \\rightarrow [1, \\underline{2}, 3, 4]$$$ (the newly inserted element is underlined), now the condition is satisfied.In the second test case, Shohag can perform the following operations:$$$[1, 2, 5, 7, 4] \\rightarrow [1, 2, \\underline{3}, 5, 7, 4] \\rightarrow [1, 2, 3, \\underline{4}, 5, 7, 4] \\rightarrow [1, 2, 3, 4, 5, \\underline{3}, 7, 4]$$$.In the third test case, the sequence already satisfies the condition."}, "positive_code": [{"source_code": "t = gets.to_i\r\nt.times{\r\nn = gets.to_i\r\nz = gets.split.map &:to_i\r\nx = []\r\nz.size.times{|i|x< 1\n \t\tcount += (l-1).downto(1).inject(0,:+)\n \tend\nend\nputs count"}, {"source_code": "n = gets.to_i\na = gets.split\n\nprev = 2 * 1e9.to_i\nsz = 0\nans = 0\na.each do |x|\n if x == prev\n sz += 1\n else\n ans += sz * (sz + 1) / 2\n sz = 1\n prev = x\n end\nend\nputs ans + sz * (sz + 1) / 2\n"}], "negative_code": [{"source_code": "n = gets.chomp.to_i\nar = gets.chomp.split(/ /).map!{|x| x.to_i}\nar.sort!\nret = n\nsamecnt = 0\nfor i in 1..n-1\n\tif (ar[i] == ar[i-1])\n\t\tsamecnt += 1\n\telse\n\t\tret += ((samecnt+1)*samecnt)/2\n\t\tsamecnt = 0\n\tend\nend\nret += ((samecnt+1)*samecnt)/2\nputs ret\n"}], "src_uid": "0b229ddf583949d43d6f1728e38c3cad"} {"nl": {"description": "Long time ago there was a symmetric array $$$a_1,a_2,\\ldots,a_{2n}$$$ consisting of $$$2n$$$ distinct integers. Array $$$a_1,a_2,\\ldots,a_{2n}$$$ is called symmetric if for each integer $$$1 \\le i \\le 2n$$$, there exists an integer $$$1 \\le j \\le 2n$$$ such that $$$a_i = -a_j$$$.For each integer $$$1 \\le i \\le 2n$$$, Nezzar wrote down an integer $$$d_i$$$ equal to the sum of absolute differences from $$$a_i$$$ to all integers in $$$a$$$, i. e. $$$d_i = \\sum_{j = 1}^{2n} {|a_i - a_j|}$$$.Now a million years has passed and Nezzar can barely remember the array $$$d$$$ and totally forget $$$a$$$. Nezzar wonders if there exists any symmetric array $$$a$$$ consisting of $$$2n$$$ distinct integers that generates the array $$$d$$$.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^5$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$). The second line of each test case contains $$$2n$$$ integers $$$d_1, d_2, \\ldots, d_{2n}$$$ ($$$0 \\le d_i \\le 10^{12}$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.", "output_spec": "For each test case, print \"YES\" in a single line if there exists a possible array $$$a$$$. Otherwise, print \"NO\". You can print letters in any case (upper or lower).", "sample_inputs": ["6\n2\n8 12 8 12\n2\n7 7 9 11\n2\n7 11 7 11\n1\n1 1\n4\n40 56 48 40 80 56 80 48\n6\n240 154 210 162 174 154 186 240 174 186 162 210"], "sample_outputs": ["YES\nNO\nNO\nNO\nNO\nYES"], "notes": "NoteIn the first test case, $$$a=[1,-3,-1,3]$$$ is one possible symmetric array that generates the array $$$d=[8,12,8,12]$$$.In the second test case, it can be shown that there is no symmetric array consisting of distinct integers that can generate array $$$d$$$."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n n=gets.to_i\r\n d=gets.split.map(&:to_i).sort\r\n f=true\r\n n.times do |i|\r\n if d[2*i]!=d[2*i+1]\r\n f=false\r\n break\r\n end\r\n end\r\n if !f\r\n puts \"NO\"\r\n next\r\n end\r\n q=0\r\n qq=[]\r\n (n-1).downto(0) do |i|\r\n if (d[2*i]-q) % ((i+1)*2)!=0\r\n f=false\r\n break\r\n else\r\n qq << (d[2*i]-q) / (i+1)\r\n q+=(d[2*i]-q) / (i+1)\r\n end\r\n end\r\n if f&&qq.size==qq.uniq.size&&qq[-1]>0\r\n puts \"YES\"\r\n else\r\n puts \"NO\"\r\n end\r\nend\r\n"}], "negative_code": [], "src_uid": "36f45f5cf4f75f81152fff4505b3b937"} {"nl": {"description": "Valera loves his garden, where n fruit trees grow.This year he will enjoy a great harvest! On the i-th tree bi fruit grow, they will ripen on a day number ai. Unfortunately, the fruit on the tree get withered, so they can only be collected on day ai and day ai + 1 (all fruits that are not collected in these two days, become unfit to eat).Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more than v fruits. The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well?", "input_spec": "The first line contains two space-separated integers n and v (1 ≤ n, v ≤ 3000) — the number of fruit trees in the garden and the number of fruits that Valera can collect in a day. Next n lines contain the description of trees in the garden. The i-th line contains two space-separated integers ai and bi (1 ≤ ai, bi ≤ 3000) — the day the fruits ripen on the i-th tree and the number of fruits on the i-th tree.", "output_spec": "Print a single integer — the maximum number of fruit that Valera can collect. ", "sample_inputs": ["2 3\n1 5\n2 3", "5 10\n3 20\n2 20\n1 20\n4 20\n5 20"], "sample_outputs": ["8", "60"], "notes": "NoteIn the first sample, in order to obtain the optimal answer, you should act as follows. On the first day collect 3 fruits from the 1-st tree. On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree. On the third day collect the remaining fruits from the 2-nd tree. In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither."}, "positive_code": [{"source_code": "#!/usr/bin/ruby\nn,v=gets.split.map(&:to_i)\na=n.times.map{gets.split.map(&:to_i)}\nma=a.max[0]\nm=[0]*(ma+2)\na.each{|x,y|m[x]+=y}\nr=0\n1.step(ma+1){|i|\n\tavail=m[i-1]+m[i]\n\tgather=[avail,v].min\n\tr+=gather\n\tx=[m[i-1],gather].min\n\tm[i-1]-=x\n\tgather-=x\n\tm[i]-=gather\n}\np r"}, {"source_code": "n,v=gets.split.map(&:to_i)\nd=[0]*3001\nn.times do\n a,b=gets.split.map(&:to_i)\n d[a-1]+=b\nend\ns=r=0\n3001.times do |i|\n e=[r,v].min\n f=[v-e,d[i]].min\n s+=e+f\n r=d[i]-f\nend \np s\n\n\n"}, {"source_code": "n,v=gets.split.map(&:to_i)\nd=[0]*3001\nn.times do\n a,b=gets.split.map(&:to_i)\n d[a-1]+=b\nend\ns=r=0\n3001.times do |i|\n e=[r,v].min\n f=[v-e,d[i]].min\n s+=e+f\n r=d[i]-f\nend \np s\n\n\n"}, {"source_code": "n,v=gets.split.map(&:to_i)\nd=[0]*3001\nn.times do\n a,b=gets.split.map(&:to_i)\n d[a-1]+=b\nend\ns=r=0\n3001.times do |i|\n e=[r,v].min\n f=[v-e,d[i]].min\n s+=e+f\n r=d[i]-f\nend \np s\n\n"}, {"source_code": "n, v = gets.split.map(&:to_i)\nx = [0] * 3001\nn.times do\n pos, value = gets.split.map(&:to_i)\n x[pos - 1] += value\nend\n\ns = r = 0\n3001.times do |i|\n e = [r, v].min\n f = [x[i], v - e].min\n s += e + f\n r = x[i] - f\nend\np s"}, {"source_code": "n,v=gets.split.map(&:to_i)\nd=[0]*3001\nn.times do\n a,b=gets.split.map(&:to_i)\n d[a-1]+=b\nend\ns=r=0\n3001.times do |i|\n e=[r,v].min\n f=[v-e,d[i]].min\n s+=e+f\n r=d[i]-f\nend \np s\n\n\n"}, {"source_code": "n,v=gets.split.map(&:to_i)\nd=[0]*3001\nn.times do\n a,b=gets.split.map(&:to_i)\n d[a-1]+=b\nend\ns=r=0\n3001.times do |i|\n e=[r,v].min\n f=[v-e,d[i]].min\n s+=e+f\n r=d[i]-f\nend \np s\n\n\n"}, {"source_code": "n,v=gets.split.map(&:to_i)\nd=[0]*3001\nn.times do\n a,b=gets.split.map(&:to_i)\n d[a-1]+=b\nend\ns=r=0\n3001.times do |i|\n e=[r,v].min\n f=[v-e,d[i]].min\n s+=e+f\n r=d[i]-f\nend \np s\n\n\n"}, {"source_code": "n,v = gets.split.map(&:to_i)\nripe_on_day = Array.new(3010, 0)\n(0...n).each { |_|\n day,amount = gets.split.map(&:to_i)\n ripe_on_day[day] += amount\n}\nputs ripe_on_day.inject(0) { |memo, value|\n harvest = [memo[1] + value, v].min\n take_new_fruits = [0, v - memo[1]].max\n [memo[0] + harvest, [0, value - take_new_fruits].max]\n}[0]\n"}, {"source_code": "n, v = gets.split.collect{|i| i.to_i}\nnum = Hash.new(0)\n(0...n).each do\n tmp = gets.split.collect{|i| i.to_i}\n num[tmp[0]] += tmp[1]\nend\nans = 0\n(1..3001).each do |i|\n tmp = [num[i-1], v].min\n ans += tmp\n tmp = v-tmp\n if tmp > 0\n if tmp >= num[i]\n ans += num[i]\n num[i] = 0\n else\n ans += tmp\n num[i] -= tmp\n end\n end\nend\nputs ans\n"}, {"source_code": "n,v=gets.split.map(&:to_i)\nd=[0]*3001\nn.times do\n a,b=gets.split.map(&:to_i)\n d[a-1]+=b\nend\ns=r=0\n3001.times do |i|\n e=[r,v].min\n f=[v-e,d[i]].min\n s+=e+f\n r=d[i]-f\nend \np s\n\n"}, {"source_code": "n,v=gets.split.map(&:to_i)\nd=[0]*3001\nn.times do\n a,b=gets.split.map(&:to_i)\n d[a-1]+=b\nend\ns=r=0\n3001.times do |i|\n e=[r,v].min\n f=[v-e,d[i]].min\n s+=e+f\n r=d[i]-f\nend \np s\n\n\n"}, {"source_code": "n,v=gets.split.map(&:to_i)\nd=[0]*3001\nn.times do\n a,b=gets.split.map(&:to_i)\n d[a-1]+=b\nend\ns=r=0\n3001.times do |i|\n e=[r,v].min\n f=[v-e,d[i]].min\n s+=e+f\n r=d[i]-f\nend \np s\n\n\n"}, {"source_code": "n,v=gets.split.map(&:to_i)\nd=[0]*3001\nn.times do\n a,b=gets.split.map(&:to_i)\n d[a-1]+=b\nend\ns=r=0\n3001.times do |i|\n e=[r,v].min\n f=[v-e,d[i]].min\n s+=e+f\n r=d[i]-f\nend \np s\n\n\n"}], "negative_code": [], "src_uid": "848ead2b878f9fd8547e1d442e2f85ff"} {"nl": {"description": "Nikolay got a string $$$s$$$ of even length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'. Its positions are numbered from $$$1$$$ to $$$n$$$.He wants to modify his string so that every its prefix of even length has an equal amount of letters 'a' and 'b'. To achieve that, Nikolay can perform the following operation arbitrary number of times (possibly, zero): choose some position in his string and replace the letter on this position with the other letter (i.e. replace 'a' with 'b' or replace 'b' with 'a'). Nikolay can use no letters except 'a' and 'b'.The prefix of string $$$s$$$ of length $$$l$$$ ($$$1 \\le l \\le n$$$) is a string $$$s[1..l]$$$.For example, for the string $$$s=$$$\"abba\" there are two prefixes of the even length. The first is $$$s[1\\dots2]=$$$\"ab\" and the second $$$s[1\\dots4]=$$$\"abba\". Both of them have the same number of 'a' and 'b'.Your task is to calculate the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'.", "input_spec": "The first line of the input contains one even integer $$$n$$$ $$$(2 \\le n \\le 2\\cdot10^{5})$$$ — the length of string $$$s$$$. The second line of the input contains the string $$$s$$$ of length $$$n$$$, which consists only of lowercase Latin letters 'a' and 'b'.", "output_spec": "In the first line print the minimum number of operations Nikolay has to perform with the string $$$s$$$ to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'. In the second line print the string Nikolay obtains after applying all the operations. If there are multiple answers, you can print any of them.", "sample_inputs": ["4\nbbbb", "6\nababab", "2\naa"], "sample_outputs": ["2\nabba", "0\nababab", "1\nba"], "notes": "NoteIn the first example Nikolay has to perform two operations. For example, he can replace the first 'b' with 'a' and the last 'b' with 'a'. In the second example Nikolay doesn't need to do anything because each prefix of an even length of the initial string already contains an equal amount of letters 'a' and 'b'."}, "positive_code": [{"source_code": "def anti(c)\n return 'b' if c == 'a'\n\n 'a'\nend\n_ = gets.chomp\ns = gets.chomp\nn = 0\ntmp = s.split('').each_slice(2).to_a\ntmp.each_with_index do |el, ind|\n if el[0] == el[1]\n tmp[ind] = [el[0], anti(el[1])]\n n += 1\n end\nend\nputs(n)\nputs(tmp.flatten.join(''))\n"}, {"source_code": "n = gets.chomp.to_i\ns = gets.chomp.split('')\nans = 0\nn.times do |i|\n if i.odd?\n c = s[i - 1]\n if c == 'a'\n c = 'b'\n else\n c = 'a'\n end\n if s[i] != c\n ans += 1\n end\n s[i] = c\n end\nend\nputs ans\nputs s.join('')\n"}, {"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\nn = inp[0]\ns = gets.chomp.split(\"\")\nf = false\nans = 0\ns.each.with_index do |c, i|\n if(i.odd?)\n if(c == \"a\")\n if(f)\n s[i] = \"b\"\n ans += 1\n end\n else\n if(!f)\n s[i] = \"a\"\n ans += 1\n end\n end\n f = false\n else\n f = true if c == \"a\"\n end\nend\np ans\nputs s.join\n"}, {"source_code": "MOD=10**9+7\ncnt=0; sum=0; prev=nil; can=true; h=Hash.new(0)\ndef gs() gets.chomp end\ndef gi() gets.to_i end\ndef gsmi() gets.chomp.split.map(&:to_i) end\n\ndef desc(ar) ar.sort!{|x,y|y<=>x} end\ndef min(a,b) a<=b ? a : b end\ndef max(a,b) a>=b ? a : b end\ndef sum(ar) ar.inject(:+) end\n\n#def bs(ar,eq,n) en=(eq==1 ? :>= : :>); ar.bsearch_index{|x|x.send(en,n)} end\ndef C(a,b) b==0||a==b ? 1 : (b=a-b if a/2'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\nn = inp[0]\ns = gets.chomp.split(\"\")\nf = false\nans = 0\ns.each.with_index do |c, i|\n if(i.odd?)\n if(c == \"a\")\n if(f)\n s[i] = \"b\"\n ans += 1\n end\n else\n if(!f)\n s[i] = \"a\"\n ans += 1\n end\n end\n else\n f = true if c == \"a\"\n end\nend\np ans\nputs s.join\n"}], "src_uid": "8ad06ac90b258a8233e2a1cf51f68078"} {"nl": {"description": "Sho has an array $$$a$$$ consisting of $$$n$$$ integers. An operation consists of choosing two distinct indices $$$i$$$ and $$$j$$$ and removing $$$a_i$$$ and $$$a_j$$$ from the array.For example, for the array $$$[2, 3, 4, 2, 5]$$$, Sho can choose to remove indices $$$1$$$ and $$$3$$$. After this operation, the array becomes $$$[3, 2, 5]$$$. Note that after any operation, the length of the array is reduced by two.After he made some operations, Sho has an array that has only distinct elements. In addition, he made operations such that the resulting array is the longest possible. More formally, the array after Sho has made his operations respects these criteria: No pairs such that ($$$i < j$$$) and $$$a_i = a_j$$$ exist. The length of $$$a$$$ is maximized. Output the length of the final array.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 10^3$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 50$$$) — the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_i$$$ ($$$1 \\leq a_i \\leq 10^4$$$) — the elements of the array.", "output_spec": "For each test case, output a single integer — the length of the final array. Remember that in the final array, all elements are different, and its length is maximum.", "sample_inputs": ["4\n\n6\n\n2 2 2 3 3 3\n\n5\n\n9 1 9 9 1\n\n4\n\n15 16 16 15\n\n4\n\n10 100 1000 10000"], "sample_outputs": ["2\n1\n2\n4"], "notes": "NoteFor the first test case Sho can perform operations as follows: Choose indices $$$1$$$ and $$$5$$$ to remove. The array becomes $$$[2, 2, 2, 3, 3, 3] \\rightarrow [2, 2, 3, 3]$$$. Choose indices $$$1$$$ and $$$4$$$ to remove. The array becomes $$$[2, 2, 3, 3] \\rightarrow [2, 3]$$$. The final array has a length of $$$2$$$, so the answer is $$$2$$$. It can be proven that Sho cannot obtain an array with a longer length.For the second test case Sho can perform operations as follows: Choose indices $$$3$$$ and $$$4$$$ to remove. The array becomes $$$[9, 1, 9, 9, 1] \\rightarrow [9, 1, 1]$$$. Choose indices $$$1$$$ and $$$3$$$ to remove. The array becomes $$$[9, 1, 1] \\rightarrow [1]$$$. The final array has a length of $$$1$$$, so the answer is $$$1$$$. It can be proven that Sho cannot obtain an array with a longer length."}, "positive_code": [{"source_code": "n = gets.to_i\n\nn.times do\n m = gets.to_i\n\n hashmap = Hash.new(0)\n initial_size = 0\n gets.split(\" \").each do |chunk|\n initial_size += 1\n hashmap[chunk] += 1\n end\n\n result = 0\n hashmap.each_value do |value|\n result += value - 1 if value > 1\n end\n\n result += 1 if result % 2 == 1\n\n p (initial_size - result)\nend\n"}, {"source_code": "t = gets.to_i\r\nt.times do\r\n n=gets.to_i\r\n d=gets.split.map &:to_i\r\n uniques = d.uniq.size\r\n if uniques%2==n%2\r\n puts uniques\r\n else\r\n puts uniques - 1\r\n end\r\nend"}, {"source_code": "$cnt = []\r\n\r\ndef solve\r\n n = gets.to_i\r\n a = gets.split.map(&:to_i)\r\n a.each do |x|\r\n $cnt[x] = $cnt[x] + 1\r\n end\r\n ans = 0\r\n even = 0\r\n a.each do |x|\r\n if $cnt[x] % 2 == 1\r\n ans = ans + 1\r\n elsif $cnt[x] > 0\r\n even = even + 1\r\n end\r\n $cnt[x] = 0\r\n if even == 2\r\n ans = ans + 2\r\n even = 0\r\n end\r\n end\r\n puts ans\r\nend\r\n\r\nmaxn = 10005\r\nmaxn.times do\r\n $cnt.append(0)\r\nend\r\ngets.to_i.times do\r\n solve\r\nend"}, {"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n a = gets.split.map(&:to_i)\r\n at = a.tally\r\n e = a.uniq.size\r\n if n.even?\r\n if e.even?\r\n puts e\r\n else\r\n puts e-1\r\n end\r\n else\r\n if e.odd?\r\n puts e\r\n else\r\n puts e-1\r\n end\r\n end\r\nend\r\n"}, {"source_code": "gets.to_i.times do\n n = gets.to_i\n a = gets.split.map(&:to_i).tally\n puts a.size - (n - a.size) % 2\nend\n"}, {"source_code": "main = -> {\r\n t = int\r\n t.times do\r\n n = int\r\n a = ints\r\n \r\n double_count = 0\r\n a.tally.each_value do |c|\r\n double_count += c - 1\r\n end\r\n \r\n puts n - double_count.div_ceil(2) * 2\r\n end\r\n}\r\n\r\nDEBUG = false\r\nMOD = 10**9+7\r\nINF = 10**18\r\n$yesno = %w(No Yes)\r\n$YesNo = %w(No Yes)\r\n$YESNO = %w(NO YES)\r\n\r\nrequire \"set\"\r\nrequire \"prime\"\r\n\r\ndef int; gets.to_s.to_i end\r\ndef ints; gets.to_s.split.map { |s| s.to_i } end\r\ndef int1s; gets.to_s.split.map { |s| s.to_i - 1 } end\r\ndef float; gets.to_s.to_f end\r\ndef floats; gets.to_s.split.map { |s| s.to_f } end\r\ndef array_of(&convert); gets.to_s.split.map(&convert) end\r\ndef string; gets.to_s.chomp end\r\ndef strings; gets.to_s.split end\r\ndef rep(n, &b); Array.new(n, &b) end\r\ndef yes; puts $yesno[1] end\r\ndef no; puts $yesno[0] end\r\ndef yesno t; puts $yesno[t ? 1 : 0] end\r\ndef YesNo t; puts $YesNo[t ? 1 : 0] end\r\ndef YESNO t; puts $YESNO[t ? 1 : 0] end\r\ndef zip(xs, *yss); Enumerator.new { |y| xs.zip(*yss) { |a| y.yield(*a) } } end\r\ndef max(*xs, &block); block_given? ? xs.max_by(&block) : xs.max end\r\ndef min(*xs, &block); block_given? ? xs.min_by(&block) : xs.min end\r\ndef minmax(*xs, &block); block_given? ? xs.minmax_by(&block) : xs.minmax end\r\ndef gcd(*xs); xs.inject(0, :gcd) end\r\ndef matrix(h, w, fill=nil, &block); return Array.new(h) { Array.new(w, &block) } if block_given?; Array.new(h) { [fill] * w } end\r\ndef debug(x = nil); STDERR.puts (block_given? ? yield(x) : x).inspect if DEBUG; x end\r\ndef debug_grid(grid, width = 1); grid.each { |row| STDERR.puts row.map { |x| x.inspect.ljust(width) }.join(\"\") } if DEBUG; grid end\r\ndef if_debug; yield if DEBUG end\r\n\r\nmodule Boolean\r\n def coerce(other); [other, to_i] end\r\n def +@; to_i end\r\n def to_int; to_i end\r\n def *(other); to_i * other end\r\nend\r\n\r\nclass TrueClass\r\n include Boolean\r\n def to_i; 1 end\r\nend\r\n\r\nclass FalseClass\r\n include Boolean\r\n def to_i; 0 end\r\nend\r\n\r\nclass Integer\r\n def div_ceil(y); (self + y - 1) / y end\r\n def mod_inv(mod = MOD); pow(mod-2, mod) end\r\n def mod_div(y, mod = MOD); self * y.mod_inv(mod) % mod end\r\n def mod_nCr(r, mod = MOD); x = y = 1; (1..r).each { |i| x = x * (self + 1 - i) % mod; y = y * i % mod }; x.mod_div(y, mod); end\r\n def factorial(mod = MOD); (2..self).inject(1) { |f, x| f * x % mod } end\r\n def popcount; x = self; c = 0; while x > 0; c += 1 if x & 1 == 1; x >>= 1 end; c end #TODO: faster\r\n def bitbrute(&block); (1< 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\r\n def lower_bound; ac, wa = self.end_open, self.begin; while ac - wa > 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\r\n def shakutori(r2, &pred); Enumerator.new { |y| j, r = r2.begin, r2.end_open; each { |i| j += 1 while j + 1 < r and pred[i, j+1]; y.yield(i, j) } }; end\r\n def widest(&block); Enumerator.new { |y| j, n = self.begin, self.end_open; each { |i| j += 1 while j < n and block[i, j]; y.yield(i, j) if block[i, j] } } end\r\nend\r\n\r\nclass Array\r\n def power(&block); (0 ... 1 << size).each(&block) end\r\n def sorted_merge(other); a = []; i = j = 0; n, m = size, other.size; if j < m and other[j] < self[i]; a << other[j]; j += 1 else; a << self[i]; i += 1 end while i < n; a.push(*other[j..-1]) if j < m; a end\r\n def upper_bound; ac, wa = 0, size; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\r\n def lower_bound; ac, wa = size, 0; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\r\n def cum(*xs, &op); a = []; a << xs[0] if xs.size > 0; a << x = self[0]; (1...size).each { |i| a << x = op[x, self[i]] }; a end\r\n def cumdiff(range); self[range.end_open] - self[range.begin]; end\r\n def compress(kinds = uniq.sort!); map { |x| kinds.bsearch_index { _1 >= x } }; end\r\nend\r\n\r\nmodule Enumerable\r\n def sorted_uniq; x = nil; filter { |y| c = x === y; x = y; !c } end\r\n def cumsum; ys = [0]; each { |x| ys << x + ys[-1] }; ys end\r\nend\r\n\r\nmain[]"}, {"source_code": "gets.to_i.times do\r\n gets\r\n xs = gets.split.map(&:to_i)\r\n ys = xs.uniq\r\n diff = xs.size - ys.size\r\n p ys.size - (diff & 1)\r\nend\r\n"}], "negative_code": [], "src_uid": "ab7ab67941783da5c16f6294eb1910d9"} {"nl": {"description": "The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called \"Testing Pants for Sadness\".The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case?", "input_spec": "The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.", "output_spec": "Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.", "sample_inputs": ["2\n1 1", "2\n2 2", "1\n10"], "sample_outputs": ["2", "5", "10"], "notes": "NoteNote to the second sample. In the worst-case scenario you will need five clicks: the first click selects the first variant to the first question, this answer turns out to be wrong. the second click selects the second variant to the first question, it proves correct and we move on to the second question; the third click selects the first variant to the second question, it is wrong and we go back to question 1; the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; the fifth click selects the second variant to the second question, it proves correct, the test is finished. "}, "positive_code": [{"source_code": "gets\ns = 0\ngets.split.map(&:to_i).each_with_index do |a, i|\n\ts += (a - 1) * (i + 1) + 1\nend\nputs s\n"}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\n\nans=0\nn.times{|i|\n ans+=a[i]*(1+i)\n ans-=i\n}\np ans\n"}, {"source_code": "n = gets.to_i\narr = gets.split(' ').map(&:to_i)\nans = 0\nn.times do |i|\n ans += (arr[i] - 1) * (1 + i)\nend\nans += n\nprint (ans)\n\n"}, {"source_code": "n = gets.to_i\narr = gets.split(' ').map(&:to_i)\nans = 0\nn.times do |i|\n ans += (arr[i] - 1) * (1 + i)\nend\nans += n\nprint ans\n\n"}, {"source_code": "n = gets.to_i\na = gets.split(' ').map(&:to_i)\nans = 0\nn.times do |i|\n ans += a[i] * (1+i)\n ans -= i\nend\nputs ans"}, {"source_code": "n = gets.to_i\narr = gets.split(\" \").map(&:to_i)\ndp = [1]\nans = 0\narr.reverse_each do |e| \n\tdp << e + dp[-1] - 1\n\tans += dp[-1] \nend\nputs ans"}, {"source_code": "n = gets.to_i\na = gets.split.collect{|i| i.to_i}\nans = 0\nn.times do |i|\n ans += (i+1)*(a[i]-1)+1\nend\nputs ans\n"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split(/ /).map!{|x| x.to_i}\nsum = 0\nfor i in 0..n-1\n sum += a[i]\n sum += (i * (a[i]-1))\nend\nputs sum"}, {"source_code": "#!/usr/bin/ruby\nnoq = gets.chop.to_i\nqp = gets.chop.split(' ');\nclicks = 0\nfor i in 0..qp.count-1\n clicks += qp[i].to_i*(i+1) - i;\nend\nputs clicks.to_s"}, {"source_code": "#!/usr/bin/ruby\nnoq = gets.chop.to_i\nqp = gets.chop.split(' ');\nclicks = 0\nfor i in 0..qp.count-1\n\tclicks += qp[i].to_i\n\tclicks += qp[i].to_i*i - i;\nend\nputs clicks.to_s\n"}, {"source_code": "n=gets.to_i\nres,x=0,0\ngets.split.map(&:to_i).reverse.each {|a|\n res+=a+x\n x+=a-1\n}\np res\n"}, {"source_code": "n = gets.to_i\na = gets.split.collect{|i| i.to_i}\nans = 0\nn.times do |i|\n ans += (i+1)*(a[i]-1)+1\nend\nputs ans\n"}, {"source_code": "gets\np gets.split.map(&:to_i).each_with_index.map{|a,i|(a-1)*(i+1)+1}.inject(:+)\n"}, {"source_code": "#!/usr/bin/env ruby\n#\n\ndef solve ai\n ret = ai[0]\n (1..ai.length-1).each do |i|\n ret += i*(ai[i]-1)\n ret += ai[i]\n end\n ret\nend\n\nn=STDIN.gets.to_i\narr = STDIN.gets.split(\" \").map {|i| i.to_i}\np solve(arr)\n"}], "negative_code": [{"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\n\nans=0\nn.times{|i|\n ans+=a[i]*(1+i)\n ans-=1\n}\np ans+1\n"}, {"source_code": "#!/usr/bin/ruby\nnoq = gets.chop.to_i\nqp = gets.chop.split(' ');\nclicks = 0\nfor i in 0..qp.count-1\n clicks += qp[i].to_i*i;\nend\nputs clicks.to_s"}, {"source_code": "8\n"}, {"source_code": "#!/usr/bin/ruby\nnoq = gets.chop.to_i\nqp = gets.chop.split(' ');\nclicks = 0\nfor i in 0..qp.count-1\n\tclicks += qp[i].to_i\nend\nif qp.count > 1\n\tfor i in 1..qp.count-1\n\t\tclicks+= qp[i].to_i\n\tend\nend\nclicks -= noq-1\nputs clicks.to_s\n"}, {"source_code": "n=gets.to_i\nres=0\ngets.split.map(&:to_i).each {|a|\n n-=1\n res+=a\n res+=n if a>1\n}\np res\n"}, {"source_code": "#!/usr/bin/env ruby\n#\n\ndef solve ai\n ret = ai[0]\n (1..ai.length-1).each do |i|\n ret += i #if ai[i] > 1\n ret += ai[i]\n end\n ret\nend\n\nn=STDIN.gets.to_i\narr = STDIN.gets.split(\" \").map {|i| i.to_i}\np solve(arr)\n"}, {"source_code": "#!/usr/bin/env ruby\n#\n\ndef solve ai\n ret = ai[0]\n (1..ai.length-1).each do |i|\n ret += i if ai[i] > 1\n ret += ai[i]\n end\n ret\nend\n\nn=STDIN.gets.to_i\narr = STDIN.gets.split(\" \").map {|i| i.to_i}\np solve(arr)\n"}, {"source_code": "#!/usr/bin/env ruby\n#\n\ndef solve ai\n ret = ai[0]\n (1..ai.length-1).each do |i|\n ret *= ai[i]-1\n ret += ai[i]\n end\n ret\nend\n\nn=STDIN.gets.to_i\narr = STDIN.gets.split(\" \").map {|i| i.to_i}\np solve(arr)\n"}], "src_uid": "c8531b2ab93993b2c3467595ad0679c5"} {"nl": {"description": "$$$n$$$ students attended the first meeting of the Berland SU programming course ($$$n$$$ is even). All students will be divided into two groups. Each group will be attending exactly one lesson each week during one of the five working days (Monday, Tuesday, Wednesday, Thursday and Friday), and the days chosen for the groups must be different. Furthermore, both groups should contain the same number of students.Each student has filled a survey in which they told which days of the week are convenient for them to attend a lesson, and which are not. Your task is to determine if it is possible to choose two different week days to schedule the lessons for the group (the first group will attend the lesson on the first chosen day, the second group will attend the lesson on the second chosen day), and divide the students into two groups, so the groups have equal sizes, and for each student, the chosen lesson day for their group is convenient.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of testcases. Then the descriptions of $$$t$$$ testcases follow. The first line of each testcase contains one integer $$$n$$$ ($$$2 \\le n \\le 1\\,000$$$) — the number of students. The $$$i$$$-th of the next $$$n$$$ lines contains $$$5$$$ integers, each of them is $$$0$$$ or $$$1$$$. If the $$$j$$$-th integer is $$$1$$$, then the $$$i$$$-th student can attend the lessons on the $$$j$$$-th day of the week. If the $$$j$$$-th integer is $$$0$$$, then the $$$i$$$-th student cannot attend the lessons on the $$$j$$$-th day of the week. Additional constraints on the input: for each student, at least one of the days of the week is convenient, the total number of students over all testcases doesn't exceed $$$10^5$$$.", "output_spec": "For each testcase print an answer. If it's possible to divide the students into two groups of equal sizes and choose different days for the groups so each student can attend the lesson in the chosen day of their group, print \"YES\" (without quotes). Otherwise, print \"NO\" (without quotes). ", "sample_inputs": ["2\n4\n1 0 0 1 0\n0 1 0 0 1\n0 0 0 1 0\n0 1 0 1 0\n2\n0 0 0 1 0\n0 0 0 1 0"], "sample_outputs": ["YES\nNO"], "notes": "NoteIn the first testcase, there is a way to meet all the constraints. For example, the first group can consist of the first and the third students, they will attend the lessons on Thursday (the fourth day); the second group can consist of the second and the fourth students, and they will attend the lessons on Tuesday (the second day).In the second testcase, it is impossible to divide the students into groups so they attend the lessons on different days."}, "positive_code": [{"source_code": "# 2\r\n# 4\r\n# 1 0 0 1 0\r\n# 0 1 0 0 1\r\n# 0 0 0 1 0\r\n# 0 1 0 1 0\r\n# 2\r\n# 0 0 0 1 0\r\n# 0 0 0 1 0\r\n\r\nrequire 'set'\r\n\r\nn = gets.to_i\r\n\r\ndef result\r\n n = gets.to_i\r\n arrs = n.times.map { gets.split.map(&:to_i) }\r\n counts = Array.new(5) { Set.new }\r\n arrs.each.with_index do |arr, j|\r\n 5.times do |i|\r\n counts[i].add(j) if arr[i] == 1\r\n end\r\n end\r\n\r\n counts = counts.select { |c| c.size >= n / 2 }\r\n counts.size.times do |i|\r\n first_group = counts[i]\r\n (i + 1).upto(counts.size - 1) do |j|\r\n second_group = counts[j]\r\n big_group, small_group = if second_group.size < first_group.size\r\n [first_group,\r\n second_group]\r\n else\r\n [second_group, first_group]\r\n end\r\n f = 0\r\n s = 0\r\n b = 0\r\n big_group.each do |stu|\r\n if small_group === stu\r\n b += 1\r\n else\r\n f += 1\r\n end\r\n end\r\n small_group.each do |stu|\r\n next if big_group === stu\r\n\r\n s += 1\r\n end\r\n return 'YES' if n / 2 - f >= 0 && n / 2 - s >= 0 && b == n - f - s\r\n end\r\n end\r\n 'NO'\r\nend\r\n\r\nn.times do\r\n puts result\r\nend\r\n"}, {"source_code": "t = gets.chomp.to_i\r\n\r\nt.times do\r\n n = gets.chomp.to_i\r\n\r\n week = Array.new(5) { [] }\r\n\r\n n.times do\r\n v = gets.chomp.split.map(&:to_i)\r\n\r\n week[0] << v[0]\r\n week[1] << v[1]\r\n week[2] << v[2]\r\n week[3] << v[3]\r\n week[4] << v[4]\r\n end\r\n\r\n good = false\r\n\r\n 5.times do |i|\r\n for j in i+1..4\r\n l, r, no = 0, 0, 0\r\n\r\n [week[i], week[j]].transpose.each do |a,b|\r\n if a == 1\r\n l += 1\r\n end\r\n\r\n if b == 1\r\n r += 1\r\n end\r\n\r\n if a == 0 and b == 0\r\n no += 1\r\n end\r\n end\r\n\r\n if l >= n / 2 and r >= n / 2 and no == 0\r\n good = true\r\n break\r\n else\r\n next\r\n end\r\n end \r\n end\r\n\r\n if good\r\n puts \"YES\"\r\n else\r\n puts \"NO\"\r\n end\r\nend\r\n"}, {"source_code": "days = [*0..4]\r\n\r\ngets.to_i.times do\r\n n = gets.to_i\r\n xxs = n.times.map { gets.split.map(&:to_i) }\r\n ok = days.any? do |i|\r\n (days - [i]).any? do |j|\r\n ii = xxs.count { |xs| xs[i] == 1 && xs[j] == 0 }\r\n jj = xxs.count { |xs| xs[j] == 1 && xs[i] == 0 }\r\n ij = xxs.count { |xs| xs[i] == 1 && xs[j] == 1 }\r\n ni = [0, n / 2 - ii].max\r\n nj = [0, n / 2 - jj].max\r\n ni + nj <= ij\r\n end\r\n end\r\n puts ok ? \"YES\" : \"NO\"\r\nend"}, {"source_code": "gets.to_i.times do\r\n n=gets.to_i\r\n n2=n/2\r\n l=[]\r\n 5.times do\r\n l << [0]*5\r\n end\r\n n.times do\r\n s=gets.split.map(&:to_i)\r\n 5.times do |i|\r\n 5.times do |j|\r\n if s[i]+s[j]==2\r\n l[i][j]+=1\r\n end\r\n end\r\n end\r\n end\r\n f=false\r\n 5.times do |j|\r\n j.times do |i|\r\n if l[i][i]+l[j][j]-l[i][j]==n&&l[i][i]>=n2&&l[j][j]>=n2\r\n f=true\r\n break\r\n end\r\n end\r\n break if f\r\n end\r\n if f\r\n puts \"YES\"\r\n else\r\n puts \"NO\"\r\n end\r\n \r\nend\r\n"}], "negative_code": [{"source_code": "# 2\r\n# 4\r\n# 1 0 0 1 0\r\n# 0 1 0 0 1\r\n# 0 0 0 1 0\r\n# 0 1 0 1 0\r\n# 2\r\n# 0 0 0 1 0\r\n# 0 0 0 1 0\r\n\r\nrequire 'set'\r\n\r\nn = gets.to_i\r\n\r\ndef result\r\n n = gets.to_i\r\n arrs = n.times.map { gets.split.map(&:to_i) }\r\n counts = Array.new(5) { Set.new }\r\n arrs.each.with_index do |arr, j|\r\n 5.times do |i|\r\n counts[i].add(j) if arr[i] == 1\r\n end\r\n end\r\n\r\n counts = counts.select { |c| c.size >= n / 2 }\r\n counts.size.times do |i|\r\n first_group = counts[i]\r\n (i + 1).upto(counts.size - 1) do |j|\r\n second_group = counts[j]\r\n big_group, small_group = if second_group.size < first_group.size\r\n [first_group,\r\n second_group]\r\n else\r\n [second_group, first_group]\r\n end\r\n changed = 0\r\n big_group.each do |stu|\r\n changed += 1 if small_group === stu\r\n end\r\n if n / 2 - big_group.size + changed >= 0 && n / 2 - small_group.size + changed >= 0 && 0 == n - big_group.size + small_group.size - (3 * changed)\r\n return 'YES'\r\n end\r\n end\r\n end\r\n 'NO'\r\nend\r\n\r\nn.times do\r\n puts result\r\nend\r\n"}, {"source_code": "# 2\r\n# 4\r\n# 1 0 0 1 0\r\n# 0 1 0 0 1\r\n# 0 0 0 1 0\r\n# 0 1 0 1 0\r\n# 2\r\n# 0 0 0 1 0\r\n# 0 0 0 1 0\r\n\r\nrequire 'set'\r\n\r\nn = gets.to_i\r\n\r\ndef result\r\n n = gets.to_i\r\n arrs = n.times.map { gets.split.map(&:to_i) }\r\n counts = Array.new(5) { Set.new }\r\n arrs.each.with_index do |arr, j|\r\n 5.times do |i|\r\n counts[i].add(j) if arr[i] == 1\r\n end\r\n end\r\n\r\n counts = counts.select { |c| c.size >= n / 2 }\r\n counts.size.times do |i|\r\n first_group = counts[i]\r\n (i + 1).upto(counts.size - 1) do |j|\r\n second_group = counts[j]\r\n big_group, small_group = if second_group.size < first_group.size\r\n [first_group,\r\n second_group]\r\n else\r\n [second_group, first_group]\r\n end\r\n changed = 0\r\n big_group.each do |stu|\r\n changed += 1 if small_group === stu\r\n end\r\n if n / 2 - big_group.size + changed >= 0 && n / 2 - small_group.size + changed >= 0 && 0 == n - big_group.size + small_group.size - changed\r\n return 'YES'\r\n end\r\n end\r\n end\r\n 'NO'\r\nend\r\n\r\nn.times do\r\n puts result\r\nend\r\n"}, {"source_code": "# 2\r\n# 4\r\n# 1 0 0 1 0\r\n# 0 1 0 0 1\r\n# 0 0 0 1 0\r\n# 0 1 0 1 0\r\n# 2\r\n# 0 0 0 1 0\r\n# 0 0 0 1 0\r\n\r\nrequire 'set'\r\n\r\nn = gets.to_i\r\n\r\ndef result\r\n n = gets.to_i\r\n arrs = n.times.map { gets.split.map(&:to_i) }\r\n counts = Array.new(5) { Set.new }\r\n arrs.each.with_index do |arr, j|\r\n 5.times do |i|\r\n counts[i].add(j) if arr[i] == 1\r\n end\r\n end\r\n\r\n counts = counts.select { |c| c.size >= n / 2 }\r\n counts.size.times do |i|\r\n first_group = counts[i]\r\n (i + 1).upto(counts.size - 1) do |j|\r\n second_group = counts[j]\r\n big_group, small_group = if second_group.size < first_group.size\r\n [first_group,\r\n second_group]\r\n else\r\n [second_group, first_group]\r\n end\r\n changed = 0\r\n big_group.each do |stu|\r\n changed += 1 if small_group === stu\r\n end\r\n 0.upto(changed) do |z|\r\n return 'YES' if z + small_group.size == n / 2 && big_group.size - changed + z == n / 2\r\n end\r\n end\r\n end\r\n 'NO'\r\nend\r\n\r\nn.times do\r\n puts result\r\nend\r\n"}, {"source_code": "# 2\r\n# 4\r\n# 1 0 0 1 0\r\n# 0 1 0 0 1\r\n# 0 0 0 1 0\r\n# 0 1 0 1 0\r\n# 2\r\n# 0 0 0 1 0\r\n# 0 0 0 1 0\r\n\r\nrequire 'set'\r\n\r\nn = gets.to_i\r\n\r\ndef result\r\n n = gets.to_i\r\n arrs = n.times.map { gets.split.map(&:to_i) }\r\n counts = Array.new(5) { Set.new }\r\n arrs.each.with_index do |arr, j|\r\n 5.times do |i|\r\n counts[i].add(j) if arr[i] == 1\r\n end\r\n end\r\n\r\n counts = counts.select { |c| c.size >= n / 2 }\r\n result = true\r\n counts.size.times do |i|\r\n first_group = counts[i]\r\n (i + 1).upto(counts.size - 1) do |j|\r\n second_group = counts[j]\r\n big_group, small_group = if second_group.size < first_group.size\r\n [first_group,\r\n second_group]\r\n else\r\n [second_group, first_group]\r\n end\r\n changed = 0\r\n big_group.each do |stu|\r\n changed += 1 if small_group === stu\r\n end\r\n result = false if small_group.size + big_group.size - changed == n || big_group.size + small_group.size == n\r\n end\r\n end\r\n !result ? 'YES' : 'NO'\r\nend\r\n\r\nn.times do\r\n puts result\r\nend\r\n"}, {"source_code": "# 2\r\n# 4\r\n# 1 0 0 1 0\r\n# 0 1 0 0 1\r\n# 0 0 0 1 0\r\n# 0 1 0 1 0\r\n# 2\r\n# 0 0 0 1 0\r\n# 0 0 0 1 0\r\n\r\nrequire 'set'\r\n\r\nn = gets.to_i\r\n\r\ndef result\r\n n = gets.to_i\r\n arrs = n.times.map { gets.split.map(&:to_i) }\r\n counts = Array.new(5) { Set.new }\r\n arrs.each.with_index do |arr, j|\r\n 5.times do |i|\r\n counts[i].add(j) if arr[i] == 1\r\n end\r\n end\r\n\r\n counts = counts.select { |c| c.size >= n / 2 }\r\n result = true\r\n counts.size.times do |i|\r\n first_group = counts[i]\r\n (i + 1).upto(counts.size - 1) do |j|\r\n second_group = counts[j]\r\n big_group, small_group = if second_group.size < first_group.size\r\n [first_group,\r\n second_group]\r\n else\r\n [second_group, first_group]\r\n end\r\n changed = 0\r\n big_group.each do |stu|\r\n changed += 1 if small_group === stu\r\n end\r\n if (changed + small_group.size >= n / 2 && small_group.size - (n / 2) <= changed && big_group.size - small_group.size + changed / 2 >= n / 2) || big_group.size - changed + small_group.size == n\r\n result = false\r\n end\r\n end\r\n end\r\n !result ? 'YES' : 'NO'\r\nend\r\n\r\nn.times do\r\n puts result\r\nend\r\n"}, {"source_code": "# 2\r\n# 4\r\n# 1 0 0 1 0\r\n# 0 1 0 0 1\r\n# 0 0 0 1 0\r\n# 0 1 0 1 0\r\n# 2\r\n# 0 0 0 1 0\r\n# 0 0 0 1 0\r\n\r\nrequire 'set'\r\n\r\nn = gets.to_i\r\n\r\ndef result\r\n n = gets.to_i\r\n arrs = n.times.map { gets.split.map(&:to_i) }\r\n counts = Array.new(5) { Set.new }\r\n arrs.each.with_index do |arr, j|\r\n 5.times do |i|\r\n counts[i].add(j) if arr[i] == 1\r\n end\r\n end\r\n\r\n result = true\r\n counts.size.times do |i|\r\n first_group = counts[i]\r\n (i + 1).upto(4) do |j|\r\n second_group = counts[j]\r\n big_group, small_group = if second_group.size < first_group.size\r\n [first_group,\r\n second_group]\r\n else\r\n [second_group, first_group]\r\n end\r\n changed = 0\r\n big_group.each do |stu|\r\n changed += 1 if small_group === stu\r\n end\r\n if (changed + small_group.size >= n / 2 && small_group.size - (n / 2) <= changed && big_group.size - small_group.size + changed / 2 >= n / 2) || big_group.size - changed + small_group.size == n\r\n result = false\r\n end\r\n end\r\n end\r\n !result ? 'YES' : 'NO'\r\nend\r\n\r\nn.times do\r\n puts result\r\nend\r\n"}, {"source_code": "# 2\r\n# 4\r\n# 1 0 0 1 0\r\n# 0 1 0 0 1\r\n# 0 0 0 1 0\r\n# 0 1 0 1 0\r\n# 2\r\n# 0 0 0 1 0\r\n# 0 0 0 1 0\r\n\r\nrequire 'set'\r\n\r\nn = gets.to_i\r\n\r\ndef result\r\n n = gets.to_i\r\n arrs = n.times.map { gets.split.map(&:to_i) }\r\n counts = Array.new(5) { Set.new }\r\n arrs.each.with_index do |arr, j|\r\n 5.times do |i|\r\n counts[i].add(j) if arr[i] == 1\r\n end\r\n end\r\n\r\n result = true\r\n counts.size.times do |i|\r\n first_group = counts[i]\r\n (i + 1).upto(4) do |j|\r\n second_group = counts[j]\r\n big_group, small_group = if second_group.size < first_group.size\r\n [first_group,\r\n second_group]\r\n else\r\n [second_group, first_group]\r\n end\r\n changed = 0\r\n big_group.each do |stu|\r\n changed += 1 if small_group === stu\r\n end\r\n if changed + small_group.size >= n / 2 && small_group.size - (n / 2) <= changed && big_group.size - small_group.size + changed / 2 >= n / 2\r\n result = false\r\n end\r\n end\r\n end\r\n !result ? 'YES' : 'NO'\r\nend\r\n\r\nn.times do\r\n puts result\r\nend\r\n"}, {"source_code": "# 2\r\n# 4\r\n# 1 0 0 1 0\r\n# 0 1 0 0 1\r\n# 0 0 0 1 0\r\n# 0 1 0 1 0\r\n# 2\r\n# 0 0 0 1 0\r\n# 0 0 0 1 0\r\n\r\nrequire 'set'\r\n\r\nn = gets.to_i\r\n\r\ndef result\r\n n = gets.to_i\r\n arrs = n.times.map { gets.split.map(&:to_i) }\r\n counts = Array.new(5) { Set.new }\r\n arrs.each.with_index do |arr, j|\r\n 5.times do |i|\r\n counts[i].add(j) if arr[i] == 1\r\n end\r\n end\r\n\r\n data = counts.select do |count|\r\n count.size >= n / 2\r\n end\r\n\r\n result = true\r\n data.size.times do |i|\r\n first_group = data[i]\r\n (i + 1).upto(data.size - 1) do |j|\r\n second_group = data[j]\r\n big_group, small_group = if second_group.size < first_group.size\r\n [first_group,\r\n second_group]\r\n else\r\n [second_group, first_group]\r\n end\r\n changed = 0\r\n big_group.each do |stu|\r\n changed += 1 if small_group === stu\r\n end\r\n if (small_group.size + changed) >= n / 2 && big_group.size - changed + (small_group.size + changed - n / 2)\r\n result = false\r\n end\r\n end\r\n end\r\n !result ? 'YES' : 'NO'\r\nend\r\n\r\nn.times do\r\n puts result\r\nend\r\n"}, {"source_code": "# 2\r\n# 4\r\n# 1 0 0 1 0\r\n# 0 1 0 0 1\r\n# 0 0 0 1 0\r\n# 0 1 0 1 0\r\n# 2\r\n# 0 0 0 1 0\r\n# 0 0 0 1 0\r\n\r\nrequire 'set'\r\n\r\nn = gets.to_i\r\n\r\ndef result\r\n n = gets.to_i\r\n arrs = n.times.map { gets.split.map(&:to_i) }\r\n counts = Array.new(5) { Set.new }\r\n arrs.each.with_index do |arr, j|\r\n 5.times do |i|\r\n counts[i].add(j) if arr[i] == 1\r\n end\r\n end\r\n\r\n data = counts.select do |count|\r\n count.size >= n / 2\r\n end\r\n\r\n result = true\r\n data.size.times do |i|\r\n first_group = data[i]\r\n (i + 1).upto(data.size - 1) do |j|\r\n second_group = data[j]\r\n big_group, small_group = if second_group.size < first_group.size\r\n [first_group,\r\n second_group]\r\n else\r\n [second_group, first_group]\r\n end\r\n changed = 0\r\n big_group.each do |stu|\r\n changed += 1 if small_group === stu\r\n end\r\n result = false if (small_group.size + changed) >= n / 2\r\n end\r\n end\r\n !result ? 'YES' : 'NO'\r\nend\r\n\r\nn.times do\r\n puts result\r\nend\r\n"}, {"source_code": "t = gets.chomp.to_i\r\n\r\nt.times do\r\n n = gets.chomp.to_i\r\n\r\n week = Array.new(5) { [] }\r\n\r\n n.times do\r\n v = gets.chomp.split.map(&:to_i)\r\n\r\n week[0] << v[0]\r\n week[1] << v[1]\r\n week[2] << v[2]\r\n week[3] << v[3]\r\n week[4] << v[4]\r\n end\r\n\r\n good = false\r\n\r\n 5.times do |i|\r\n for j in (i+1)..4\r\n l, r = 0, 0\r\n\r\n [week[i], week[j]].transpose.each do |a,b|\r\n if a == 1 and b == 1\r\n l += 1\r\n end\r\n\r\n if a == 1\r\n l += 1\r\n end\r\n\r\n if b == 1\r\n r += 1\r\n end\r\n end\r\n\r\n if l+r == n and l != 0 and r != 0\r\n good = true\r\n break\r\n else\r\n next\r\n end\r\n end \r\n end\r\n\r\n if good\r\n puts \"YES\"\r\n else\r\n puts \"NO\"\r\n end\r\nend\r\n"}, {"source_code": "t = gets.chomp.to_i\r\n\r\nt.times do\r\n n = gets.chomp.to_i\r\n\r\n week = Array.new(5) { [] }\r\n\r\n n.times do\r\n v = gets.chomp.split.map(&:to_i)\r\n\r\n week[0] << v[0]\r\n week[1] << v[1]\r\n week[2] << v[2]\r\n week[3] << v[3]\r\n week[4] << v[4]\r\n end\r\n\r\n good = false\r\n\r\n 5.times do |i|\r\n for j in i+1..4\r\n ok = true\r\n\r\n [week[i], week[j]].transpose.each do |a,b|\r\n if a == 0 and b == 0\r\n ok = false\r\n break\r\n end\r\n end\r\n\r\n if ok\r\n good = true\r\n break\r\n else\r\n next\r\n end\r\n end \r\n end\r\n\r\n if good\r\n puts \"YES\"\r\n else\r\n puts \"NO\"\r\n end\r\nend\r\n"}, {"source_code": "t = gets.chomp.to_i\r\n\r\nt.times do\r\n n = gets.chomp.to_i\r\n\r\n week = Array.new(5) { [] }\r\n\r\n n.times do\r\n v = gets.chomp.split.map(&:to_i)\r\n\r\n week[0] << v[0]\r\n week[1] << v[1]\r\n week[2] << v[2]\r\n week[3] << v[3]\r\n week[4] << v[4]\r\n end\r\n\r\n good = false\r\n\r\n n.times do |i|\r\n for j in i..n\r\n ok = true\r\n\r\n [week[i], week[j]].transpose.each do |a, b|\r\n if a == 0 and b == 0\r\n ok = false\r\n break\r\n end\r\n end\r\n\r\n if ok\r\n good = true\r\n break\r\n else\r\n next\r\n end\r\n end \r\n end\r\n\r\n if good\r\n puts(\"YES\")\r\n else\r\n puts \"NO\"\r\n end\r\nend\r\n"}, {"source_code": "t = gets.chomp.to_i\r\n\r\nt.times do\r\n n = gets.chomp.to_i\r\n\r\n week = Array.new(5) { [] }\r\n\r\n n.times do\r\n v = gets.chomp.split.map(&:to_i)\r\n\r\n week[0] << v[0]\r\n week[1] << v[1]\r\n week[2] << v[2]\r\n week[3] << v[3]\r\n week[4] << v[4]\r\n end\r\n\r\n good = false\r\n\r\n n.times do |i|\r\n for j in i..n\r\n ok = true\r\n\r\n [week[i], week[j]].transpose.each do |a, b|\r\n if a == 0 and b == 0\r\n ok = false\r\n break\r\n end\r\n end\r\n\r\n if ok\r\n good = true\r\n break\r\n else\r\n next\r\n end\r\n end \r\n end\r\n\r\n if good\r\n p \"YES\"\r\n else\r\n p \"NO\"\r\n end\r\nend\r\n"}], "src_uid": "068cbfb901aadcd15f794dbbf3dfd453"} {"nl": {"description": "This problem is same as the previous one, but has larger constraints.It was a Sunday morning when the three friends Selena, Shiro and Katie decided to have a trip to the nearby power station (do not try this at home). After arriving at the power station, the cats got impressed with a large power transmission system consisting of many chimneys, electric poles, and wires. Since they are cats, they found those things gigantic.At the entrance of the station, there is a map describing the complicated wiring system. Selena is the best at math among three friends. He decided to draw the map on the Cartesian plane. Each pole is now a point at some coordinates $$$(x_i, y_i)$$$. Since every pole is different, all of the points representing these poles are distinct. Also, every two poles are connected with each other by wires. A wire is a straight line on the plane infinite in both directions. If there are more than two poles lying on the same line, they are connected by a single common wire.Selena thinks, that whenever two different electric wires intersect, they may interfere with each other and cause damage. So he wonders, how many pairs are intersecting? Could you help him with this problem?", "input_spec": "The first line contains a single integer $$$n$$$ ($$$2 \\le n \\le 1000$$$) — the number of electric poles. Each of the following $$$n$$$ lines contains two integers $$$x_i$$$, $$$y_i$$$ ($$$-10^4 \\le x_i, y_i \\le 10^4$$$) — the coordinates of the poles. It is guaranteed that all of these $$$n$$$ points are distinct.", "output_spec": "Print a single integer — the number of pairs of wires that are intersecting.", "sample_inputs": ["4\n0 0\n1 1\n0 3\n1 2", "4\n0 0\n0 2\n0 4\n2 0", "3\n-1 -1\n1 0\n3 1"], "sample_outputs": ["14", "6", "0"], "notes": "NoteIn the first example: In the second example: Note that the three poles $$$(0, 0)$$$, $$$(0, 2)$$$ and $$$(0, 4)$$$ are connected by a single wire.In the third example: "}, "positive_code": [{"source_code": "# coding: utf-8\nn = readline.to_i\npoles = (0...n).map { |_| readline.split.map(&:to_i) }\n\ndef c2(n)\n n * (n-1) / 2\nend\n\nlines = {}\nlines.default = 0\nparallels = {}\nparallels.default = 0\nfor i in 0...n do\n x1, y1 = poles[i]\n for j in i+1...n do\n x2, y2 = poles[j]\n a = y1 - y2\n b = x2 - x1\n if a < 0 or (a == 0 and b < 0)\n a = -a\n b = -b\n end\n d = a.gcd b\n a /= d\n b /= d\n c = a*x1 + b*y1\n line = [a, b, c]\n lines[line] += 1\n parallels[[a, b]] += 1 if lines[line] == 1\n end\nend\n\nr = c2(lines.length)\nparallels.values.each { |x| r -= c2(x) }\np r\n"}], "negative_code": [{"source_code": "n = readline.to_i\npoles = (0...n).map { |_| readline.split.map(&:to_i) }\n\ndef line_x(x1, y1, x2, y2)\n if y1 == y2\n [nil, nil]\n else\n slope = Rational((x1-x2)/(y1-y2))\n [slope, x1 - slope * y1]\n end\nend\n\ndef c2(n)\n n * (n-1) / 2\nend\n\nlines = {}\nlines.default = 0\nparallels = {}\nparallels.default = 0\nfor i in 0...n do\n x1, y1 = poles[i]\n for j in i+1...n do\n x2, y2 = poles[j]\n sx, lx = line_x(x1, y1, x2, y2)\n sy, ly = line_x(y1, x1, y2, x2)\n line = [sx, sy, lx, ly]\n lines[line] += 1\n parallels[[sx, sy]] += 1 if lines[line] == 1\n end\nend\n\nr = c2(lines.length)\nparallels.values.each { |x| r -= c2(x) }\np r\n"}, {"source_code": "n = readline.to_i\npoles = (0...n).map { |_| readline.split.map(&:to_i) }\n\ndef line_x(x1, y1, x2, y2)\n if y1 == y2\n [nil, nil]\n else\n slope = Float(x1-x2) / (y1-y2)\n [slope, x1 - slope * y1]\n end\nend\n\ndef c2(n)\n n * (n-1) / 2\nend\n\nlines = {}\nlines.default = 0\nparallels = {}\nparallels.default = 0\nfor i in 0...n do\n x1, y1 = poles[i]\n for j in i+1...n do\n x2, y2 = poles[j]\n sx, lx = line_x(x1, y1, x2, y2)\n sy, ly = line_x(y1, x1, y2, x2)\n line = [sx, lx, ly]\n lines[line] += 1\n parallels[sx] += 1 if lines[line] == 1\n end\nend\n\nr = c2(lines.length)\nparallels.values.each { |x| r -= c2(x) }\np r\n"}, {"source_code": "n = readline.to_i\npoles = (0...n).map { |_| readline.split.map(&:to_i) }\n\ndef c2(n)\n n * (n-1) / 2\nend\n\nlines = {}\nlines.default = 0\nparallels = {}\nparallels.default = 0\nfor i in 0...n do\n x1, y1 = poles[i]\n for j in i+1...n do\n x2, y2 = poles[j]\n a = y1 - y2\n b = x2 - x1\n d = a.gcd b\n a /= d\n b /= d\n c = a*x1 + b*y1\n line = [a, b, c]\n lines[line] += 1\n parallels[[a, b]] += 1 if lines[line] == 1\n end\nend\n\nr = c2(lines.length)\nparallels.values.each { |x| r -= c2(x) }\np r\n"}], "src_uid": "d7d8d91be04f5d9065a0c22e66d11de3"} {"nl": {"description": "You are given a huge integer $$$a$$$ consisting of $$$n$$$ digits ($$$n$$$ is between $$$1$$$ and $$$3 \\cdot 10^5$$$, inclusive). It may contain leading zeros.You can swap two digits on adjacent (neighboring) positions if the swapping digits are of different parity (that is, they have different remainders when divided by $$$2$$$). For example, if $$$a = 032867235$$$ you can get the following integers in a single operation: $$$302867235$$$ if you swap the first and the second digits; $$$023867235$$$ if you swap the second and the third digits; $$$032876235$$$ if you swap the fifth and the sixth digits; $$$032862735$$$ if you swap the sixth and the seventh digits; $$$032867325$$$ if you swap the seventh and the eighth digits. Note, that you can't swap digits on positions $$$2$$$ and $$$4$$$ because the positions are not adjacent. Also, you can't swap digits on positions $$$3$$$ and $$$4$$$ because the digits have the same parity.You can perform any number (possibly, zero) of such operations.Find the minimum integer you can obtain.Note that the resulting integer also may contain leading zeros.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases in the input. The only line of each test case contains the integer $$$a$$$, its length $$$n$$$ is between $$$1$$$ and $$$3 \\cdot 10^5$$$, inclusive. It is guaranteed that the sum of all values $$$n$$$ does not exceed $$$3 \\cdot 10^5$$$.", "output_spec": "For each test case print line — the minimum integer you can obtain.", "sample_inputs": ["3\n0709\n1337\n246432"], "sample_outputs": ["0079\n1337\n234642"], "notes": "NoteIn the first test case, you can perform the following sequence of operations (the pair of swapped digits is highlighted): $$$0 \\underline{\\textbf{70}} 9 \\rightarrow 0079$$$.In the second test case, the initial integer is optimal. In the third test case you can perform the following sequence of operations: $$$246 \\underline{\\textbf{43}} 2 \\rightarrow 24 \\underline{\\textbf{63}}42 \\rightarrow 2 \\underline{\\textbf{43}} 642 \\rightarrow 234642$$$."}, "positive_code": [{"source_code": "readline.to_i.times do\n e,o = readline.chomp.chars.partition { |x| x.to_i.odd? }\n i = j = 0\n im = e.length\n jm = o.length\n r = []\n while i < im or j < jm do\n if j == jm or (i < im and e[i] < o[j]) then\n r << e[i]\n i += 1\n else\n r << o[j]\n j += 1\n end\n end\n print r.join(), \"\\n\"\nend\n"}], "negative_code": [], "src_uid": "55956c5389c34e4012069de92b3185dc"} {"nl": {"description": "There are $$$n$$$ programmers that you want to split into several non-empty teams. The skill of the $$$i$$$-th programmer is $$$a_i$$$. You want to assemble the maximum number of teams from them. There is a restriction for each team: the number of programmers in the team multiplied by the minimum skill among all programmers in the team must be at least $$$x$$$.Each programmer should belong to at most one team. Some programmers may be left without a team.Calculate the maximum number of teams that you can assemble.", "input_spec": "The first line contains the integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \\le n \\le 10^5; 1 \\le x \\le 10^9$$$) — the number of programmers and the restriction of team skill respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\dots , a_n$$$ ($$$1 \\le a_i \\le 10^9$$$), where $$$a_i$$$ is the skill of the $$$i$$$-th programmer. The sum of $$$n$$$ over all inputs does not exceed $$$10^5$$$.", "output_spec": "For each test case print one integer — the maximum number of teams that you can assemble. ", "sample_inputs": ["3\n5 10\n7 11 2 9 5\n4 8\n2 4 2 3\n4 11\n1 3 3 7"], "sample_outputs": ["2\n1\n0"], "notes": null}, "positive_code": [{"source_code": "tc = gets.to_i \ntc.times do \n\tn,y = gets.split().map{|x| x.to_i}\n\tarr = gets.split().map{|x| x.to_i}\n\tarr.sort_by!{|x| -x}\n\tcur = ans = 0\n\tarr.each do |x| \n\t\tcur += 1\n\t\tif(cur*x >= y) \n\t\t\tans += 1 \n\t\t\tcur = 0\n\t\tend\n\tend\n\tputs ans\nend"}], "negative_code": [], "src_uid": "8a6953a226abef41a44963c9b4998a25"} {"nl": {"description": "Tom loves vowels, and he likes long words with many vowels. His favorite words are vowelly words. We say a word of length $$$k$$$ is vowelly if there are positive integers $$$n$$$ and $$$m$$$ such that $$$n\\cdot m = k$$$ and when the word is written by using $$$n$$$ rows and $$$m$$$ columns (the first row is filled first, then the second and so on, with each row filled from left to right), every vowel of the English alphabet appears at least once in every row and every column.You are given an integer $$$k$$$ and you must either print a vowelly word of length $$$k$$$ or print $$$-1$$$ if no such word exists.In this problem the vowels of the English alphabet are 'a', 'e', 'i', 'o' ,'u'.", "input_spec": "Input consists of a single line containing the integer $$$k$$$ ($$$1\\leq k \\leq 10^4$$$) — the required length.", "output_spec": "The output must consist of a single line, consisting of a vowelly word of length $$$k$$$ consisting of lowercase English letters if it exists or $$$-1$$$ if it does not. If there are multiple possible words, you may output any of them.", "sample_inputs": ["7", "36"], "sample_outputs": ["-1", "agoeuioaeiruuimaeoieauoweouoiaouimae"], "notes": "NoteIn the second example, the word \"agoeuioaeiruuimaeoieauoweouoiaouimae\" can be arranged into the following $$$6 \\times 6$$$ grid: It is easy to verify that every row and every column contain all the vowels."}, "positive_code": [{"source_code": "def tempfun(k) \n\ttemp = ['a','e','i','o','u']\n\tn = k - 5\n\tn.to_i.times do \n\t\ttemp << 'g' \n\tend\n\tans = []\n\tfor b in 0...temp.length \n\t\tans << temp.rotate(b)\n\tend\n\treturn ans \nend\nmark = false\ni = gets.chomp.to_i \nk = Math.sqrt(i) \nans = Array.new\nif(k >= 5)\n\tt = k.to_i \n\tfor z in 5..t\n\t\tif(i % z == 0 )\n\t\t\tmark = true\n\t\t\tm = (i - z*z)\n\t\t\tans = tempfun(z)\n\t\t\t#print(ans)\n\t\t\tfor i in 0...z\n\t\t\t\tfor j in 0... (m/z)\n\t\t\t\t\tans[i] << ans[i][j]\n\t\t\t\tend\n\t\t\tend\n\t\t\tbreak\n\t\tend\n\tend\n\tif(mark)\n\t\tfor i in ans \n\t\t\tfor m in i \n\t\t\t\tprint(m)\n\t\t\tend\n\t\t\t#puts()\n\t\tend\n\telse \n\t\tprint(-1)\n\tend\nelse\n\tprint(-1)\nend\n"}, {"source_code": "n = gets.to_i\nval = nil\n5.upto(n**0.5) do |i|\n if n % i == 0\n val = i\n break\n end\nend\nvow = \"aeiou\"\nif val == nil\n p -1\n exit\nelse\n row = vow * (val*2/5 + 1)\n (n/val).times do |i|\n print row[i%val,val]\n end\n print \"\\n\"\nend\n"}, {"source_code": "n = gets.to_i\nval = nil\n5.upto(n**0.5) do |i|\n if n % i == 0\n val = i\n break\n end\nend\nvow = \"aeiou\"\nif val == nil\n p -1\n exit\nelse\n row = vow * (val/5) + vow[0,val%5]\n row *= 2\n (n/val).times do |i|\n print row[i%val,val]\n end\n print \"\\n\"\nend\n"}, {"source_code": "n = gets.to_i\nf = false\nu = v = 0\nfor i in 5..n\n if n%i == 0 && n/i >= 5\n f = true\n u = i\n v = n/i\n break\n end\nend\nif !f\n puts \"-1\"\n exit\nend\ns = \"aiueo\"*(u/5+2)\nans = \"\"\nv.times do |i|\n ans += s[i%5,u]\nend\nputs ans"}], "negative_code": [{"source_code": "def tempfun(k) \n\ttemp = ['a','e','i','o','u']\n\tn = k - 5\n\tn.to_i.times do \n\t\ttemp << 'g' \n\tend\n\tans = []\n\tfor b in 0...temp.length \n\t\tans << temp.rotate(b)\n\tend\n\treturn ans \nend\n\ni = gets.chomp.to_i \nk = Math.sqrt(i) \nans = Array.new\nif(k >= 5)\n\tt = k.to_i \n\tif((i - t*t) % t == 0 )\n\t\tm = (i - t*t)\n\t\tans = tempfun(t)\n\t\t#print(ans)\n\t\tfor i in 0...t\n\t\t\tfor j in 0... (m/t)\n\t\t\t\tans[i] << ans[i][j]\n\t\t\tend\n\t\tend\n\telse\n\t\tprint(-1)\n\t\texit\n\tend\n\tfor i in ans \n\t\tfor m in i \n\t\t\tprint(m)\n\t\tend\n\t\tputs()\n\tend\nelse\n\tprint(-1)\n\texit\nend\n"}, {"source_code": "def tempfun(k) \n\ttemp = ['a','e','i','o','u']\n\tn = k - 5\n\tn.to_i.times do \n\t\ttemp << 'g' \n\tend\n\tans = []\n\tfor b in 0...temp.length \n\t\tans << temp.rotate(b)\n\tend\n\treturn ans \nend\n\ni = gets.chomp.to_i \nk = Math.sqrt(i) \nans = Array.new\nif(k >= 5)\n\tt = k.to_i \n\tif((i - t*t) % t == 0 )\n\t\tm = (i - t*t)\n\t\tans = tempfun(t)\n\t\t#print(ans)\n\t\tfor i in 0...t\n\t\t\tfor j in 0... (m/t)\n\t\t\t\tans[i] << ans[i][j]\n\t\t\tend\n\t\tend\n\telse\n\t\tprint(-1)\n\t\texit\n\tend\n\tfor i in ans \n\t\tfor m in i \n\t\t\tprint(m)\n\t\tend\n\t\t\n\tend\nelse\n\tprint(-1)\n\texit\nend\n"}, {"source_code": "n = gets.to_i\nval = nil\n5.upto(n**0.5) do |i|\n if n % i == 0\n val = i\n break\n end\nend\nif val == nil\n p -1\n exit\nelse\n row = \"aeiou\" * (val/5) + \"x\" * (val % 5)\n (n/val).times do \n print row\n end\n print \"\\n\"\nend\n"}, {"source_code": "n = gets.to_i\nf = false\nu = v = 0\nfor i in 5..n\n if n%i == 0 && n/i >= 5\n f = true\n u = i\n v = n/i\n break\n end\nend\nif !f\n puts \"-1\"\n exit\nend\ns = \"aiueo\"*(u/5+1)\nans = \"\"\nv.times do |i|\n ans += s[i%5,u]\nend\nputs ans"}], "src_uid": "933167c31c0f53a43e840cc6cf153f8d"} {"nl": {"description": "The flag of Berland is such rectangular field n × m that satisfies following conditions: Flag consists of three colors which correspond to letters 'R', 'G' and 'B'. Flag consists of three equal in width and height stripes, parralel to each other and to sides of the flag. Each stripe has exactly one color. Each color should be used in exactly one stripe. You are given a field n × m, consisting of characters 'R', 'G' and 'B'. Output \"YES\" (without quotes) if this field corresponds to correct flag of Berland. Otherwise, print \"NO\" (without quotes).", "input_spec": "The first line contains two integer numbers n and m (1 ≤ n, m ≤ 100) — the sizes of the field. Each of the following n lines consisting of m characters 'R', 'G' and 'B' — the description of the field.", "output_spec": "Print \"YES\" (without quotes) if the given field corresponds to correct flag of Berland . Otherwise, print \"NO\" (without quotes).", "sample_inputs": ["6 5\nRRRRR\nRRRRR\nBBBBB\nBBBBB\nGGGGG\nGGGGG", "4 3\nBRG\nBRG\nBRG\nBRG", "6 7\nRRRGGGG\nRRRGGGG\nRRRGGGG\nRRRBBBB\nRRRBBBB\nRRRBBBB", "4 4\nRRRR\nRRRR\nBBBB\nGGGG"], "sample_outputs": ["YES", "YES", "NO", "NO"], "notes": "NoteThe field in the third example doesn't have three parralel stripes.Rows of the field in the fourth example are parralel to each other and to borders. But they have different heights — 2, 1 and 1."}, "positive_code": [{"source_code": "def build(n, m)\n 'RGB'.chars.permutation.map do |cs|\n cs.flat_map{|c| n.times.map{(c * m).chars}}\n end\nend\n\nn, m = gets.split.map(&:to_i)\ns = n.times.map{gets.strip.chars}\nts = build(n / 3, m) + build(m / 3, n).map(&:transpose)\n\nif ts.include?(s)\n puts 'YES'\nelse\n puts 'NO'\nend\n"}, {"source_code": "n, m = gets.strip.split(' ').map(&:to_i)\n\na = []\nn.times do\n a.push << (gets.strip.split(''))\nend\n\nrc = true\ntemp = []\nn.times do |i|\n first = nil\n bool = true\n m.times do |j|\n if j == 0\n first = a[i][j]\n temp.push(a[i][j])\n next\n end\n if first != a[i][j]\n bool = false\n break\n end\n end\n if !bool\n rc = false\n break\n end\nend\n\nif rc\n if temp.count('R') == temp.count('G') && temp.count('G') == temp.count('B')\n cnt = temp.count('R')\n if ((temp.rindex('R') - temp.index('R') + 1) == cnt) && ((temp.rindex('G') - temp.index('G') + 1) == cnt) && ((temp.rindex('B') - temp.index('B') + 1) == cnt) \n rc = true\n else\n rc = false \n end\n else\n rc = false\n end\nend\n\nrc1 = true\ntemp = []\nm.times do |i|\n first = nil\n bool = true\n n.times do |j|\n if j == 0\n first = a[j][i]\n temp.push(a[j][i])\n next\n end\n if first != a[j][i]\n bool = false\n break\n end\n end\n if !bool\n rc1 = false\n break\n end\nend\n\nif rc1\n if temp.count('R') == temp.count('G') && temp.count('G') == temp.count('B')\n cnt = temp.count('R')\n if ((temp.rindex('R') - temp.index('R') + 1) == cnt) && ((temp.rindex('G') - temp.index('G') + 1) == cnt) && ((temp.rindex('B') - temp.index('B') + 1) == cnt) \n rc1 = true\n else\n rc1 = false \n end\n else\n rc1 = false\n end\nend\n\n\nif rc || rc1\n puts 'YES'\nelse\n puts 'NO'\nend\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\na, res = [], \"NO\"\nn.times {a << gets.chomp.chars}\nc = [a[0][0], a[n / 2][m / 2], a[n - 1][m - 1]].uniq\nif c.size == 3\n n = n * m / 3\n s = [c[0] * n, c[1] * n, c[2] * n].join\n [a, a.transpose].each {|b| res = \"YES\" if b.flatten.join == s}\nend\nputs res\n\n"}, {"source_code": "def build(n, m)\n 'RGB'.chars.permutation.map do |cs|\n cs.flat_map{|c| n.times.map{(c * m).chars}}\n end\nend\n\nn, m = gets.split.map(&:to_i)\ns = n.times.map{gets.strip.chars}\nts = build(n / 3, m) + build(m / 3, n).map(&:transpose)\n\nif ts.include?(s)\n puts 'YES'\nelse\n puts 'NO'\nend"}], "negative_code": [{"source_code": "n, m = gets.strip.split(' ').map(&:to_i)\n\na = []\nn.times do\n a.push << (gets.strip.split(''))\nend\n\nrc = true\ntemp = []\nn.times do |i|\n first = nil\n bool = true\n m.times do |j|\n if j == 0\n first = a[i][j]\n temp.push(a[i][j])\n next\n end\n if first != a[i][j]\n bool = false\n break\n end\n end\n if !bool\n rc = false\n break\n end\nend\n\nif rc\n if temp.count('R') == temp.count('G') && temp.count('G') == temp.count('B')\n rc = true\n else\n rc = false\n end\nend\n\nrc1 = true\ntemp = []\nm.times do |i|\n first = nil\n bool = true\n n.times do |j|\n if j == 0\n first = a[j][i]\n temp.push(a[j][i])\n next\n end\n if first != a[j][i]\n bool = false\n break\n end\n end\n if !bool\n rc1 = false\n break\n end\nend\n\nif rc1\n if temp.count('R') == temp.count('G') && temp.count('G') == temp.count('B')\n rc1 = true\n else\n rc1 = false\n end\nend\n\n\nif rc || rc1\n puts 'YES'\nelse\n puts 'NO'\nend\n"}], "src_uid": "0988b8439c6699107c77345037162fba"} {"nl": {"description": "n soldiers stand in a circle. For each soldier his height ai is known. A reconnaissance unit can be made of such two neighbouring soldiers, whose heights difference is minimal, i.e. |ai - aj| is minimal. So each of them will be less noticeable with the other. Output any pair of soldiers that can form a reconnaissance unit.", "input_spec": "The first line contains integer n (2 ≤ n ≤ 100) — amount of soldiers. Then follow the heights of the soldiers in their order in the circle — n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 1000). The soldier heights are given in clockwise or counterclockwise direction.", "output_spec": "Output two integers — indexes of neighbouring soldiers, who should form a reconnaissance unit. If there are many optimum solutions, output any of them. Remember, that the soldiers stand in a circle.", "sample_inputs": ["5\n10 12 13 15 10", "4\n10 20 30 40"], "sample_outputs": ["5 1", "1 2"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\n\nsolds = gets.split.map{|i| i.to_i}\n\nmin = (solds[0] - solds[-1]).abs\nq = n\nw = 1\ni = 1\nwhile i < n do\n if min > (solds[i-1] - solds[i]).abs then\n min = (solds[i-1] - solds[i]).abs\n q = i\n w = i + 1\n end\n i += 1\nend\n\nputs [q,w].join(\" \")"}, {"source_code": "n = gets.chomp.to_i\ns = gets.chomp\nsa = s.split(/ /)\nsa.map!{|x| x.to_i}\n\nmindiff = (sa[1]-sa[0]).abs\nminidx1 = 1\nminidx2 = 2\n\nfor i in 1..n-1\n\tj = i+1\n\tj = 0 if j >= n\n\twk = (sa[i]-sa[j]).abs\n\tif wk < mindiff\n\t\tmindiff = wk\n\t\tminidx1 = i+1\n\t\tminidx2 = j+1\n\tend\nend\n\nputs minidx1.to_s + \" \" + minidx2.to_s"}, {"source_code": "n=gets.to_i\nv=gets.split.map{|i|i.to_i}\nv<t then\n\t\tm=t\n\t\tr=i\n\tend\n}\nprint r,\" \",r%n+1\n"}, {"source_code": "#!/usr/bin/ruby\ngets;a=gets.split.map(&:to_i)\nr=a.size.times.map{|i|[(a[i]-a[(i+1)%a.size]).abs,i]}.min.pop\nputs \"#{r+1} #{(r+1)%a.size+1}\""}, {"source_code": "num_soldiers = gets.to_i\nsoldiers = gets.split.collect {|i| i.to_i}\nbest = (soldiers[0] - soldiers[-1]).abs\nbest_index = [1,num_soldiers]\nfor i in 0...num_soldiers-1\n\ttemp = (soldiers[i+1] - soldiers[i]).abs\n\tif temp < best\n\t\tbest = temp\n\t\tbest_index = [i+1,i+2]\n\tend\nend\nputs best_index[0].to_s + \" \" + best_index[1].to_s\n"}, {"source_code": "n=gets.to_i\na=gets.chomp.split.map(&:to_i)\nx,y=1,1\nz=[(a[0]-a[n-1]).abs]\nfor i in 0..n-2\n\tz.push((a[i]-a[i+1]).abs)\nend\nfor i in 0..z.length-1\n\tif z[i]==z.min\n\t\tif i==0\n\t\t\tputs \"#{n} 1\"\n\t\t\texit\n\t\telse\n\t\t\tputs \"#{i} #{i+1}\"\n\t\t\texit\n\t\tend\n\tend\nend\n"}, {"source_code": "n = gets.to_i\na = gets.split.map!(&:to_i)\nk = (-1...n-1).min_by {|i| (a[i] - a[i+1]).abs }\nputs \"#{(k+1).zero? ? n : k+1} #{k+2}\""}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split(/ /).map(&:to_i)\nneighbor = {}\ni = j = 0\nn1 = n2 = 0\ndiff = 0\nwhile true\n n1 = a[i]\n if i + 1 == n\n j = 0\n else\n j = i + 1\n end\n n2 = a[j]\n diff = (n1 - n2).abs\n pair = [i + 1, j + 1]\n if !neighbor.has_key? pair\n neighbor.store pair, diff\n else\n neighbor[pair] = diff\n end\n i += 1\n break if j == 0\nend\nmin_h = neighbor.values.min\nrecon = neighbor.select { |k, v| v == min_h }.to_a[0][0]\nputs recon.join(\" \")\n"}, {"source_code": "n=gets.to_i\ndata=gets.split.map(&:to_i)\ndata << data[0]\n\ndif=999999\na=b=0\nfor i in 0..data.size-2\n\ttmp=(data[i]-data[i+1]).abs\n\tif dif>tmp\n\t\ta,b=i,((i+1)%n==0? 1 : i+2)\n\t\tdif=tmp\n\tend\nend\n\nputs \"#{a+1} #{b}\"\n"}, {"source_code": "n = gets.to_i\narr = gets.split(\" \").map(&:to_i)\ndiff_arr = []\n(1..n-1).each do |i|\n\tdiff_arr << (arr[i] - arr[i-1]).abs\nend\ndiff_arr << (arr[0]-arr[-1]).abs\nmin,min_ind = diff_arr.each_with_index.min\n\nif min_ind == n -1 \n\ta,b = n,1\nelse\n\ta,b = min_ind+1,min_ind + 2\nend\nputs \"#{a} #{b}\""}, {"source_code": "n = gets.to_i\na = gets.split.map!(&:to_i)\nk = (-1...n-1).min_by { |i| (a[i] - a[i+1]).abs; }\nputs \"#{k % a.length + 1} #{k + 2}\""}, {"source_code": "tot, min, min_index, input = gets.to_i, 20000, -1, gets.split(' ').map{|n| n.to_i}\ninput.each_cons(2).to_a.each_with_index { |(a, b), index| min, min_index = (a - b).abs, index if (((a - b).abs) < min) }\nmin_index = tot - 1 if (min > (input[0] - input[input.length - 1]).abs)\nmax_index = (min_index == tot - 1) ? 0 : min_index + 1\nprint (min_index + 1).to_s + ' ' + (max_index + 1).to_s"}, {"source_code": "n = gets.to_i\na = gets.split.collect{|i| i.to_i}\nans = 0\n(1...n-1).each{|i| ans = i if (a[i]-a[i+1]).abs < (a[ans]-a[ans+1]).abs}\nif (a[0]-a[n-1]).abs < (a[ans]-a[ans+1]).abs\n print n, \" \", 1, \"\\n\"\nelse\n print ans+1, \" \", ans+2, \"\\n\"\nend\n"}, {"source_code": "n = gets.to_i\na = gets.split.collect{|i| i.to_i}\nans = 0\nfor i in 1...n-1\n ans = i if (a[i]-a[i+1]).abs < (a[ans]-a[ans+1]).abs\nend\nif (a[0]-a[n-1]).abs < (a[ans]-a[ans+1]).abs\n print n, \" \", 1, \"\\n\"\nelse\n print ans+1, \" \", ans+2, \"\\n\"\nend\n"}, {"source_code": "gets # ignore n\n\nsoldiers = gets.chomp.split.map(&:to_i)\n\nsoldiers << soldiers[0]\ndiff, pos = 1_001, 0\n\nsoldiers[0..-2].each_with_index do |s, i|\n tmp_diff = (s - soldiers[i + 1]).abs\n if tmp_diff < diff\n diff = tmp_diff\n pos = i\n end\nend\n\nputs pos + 1, (pos + 2 > soldiers.size - 1 ? 1 : pos + 2)\n"}, {"source_code": "n = gets.to_i\nh = gets.split(\" \").map{|s| s.to_i}\nh << h[0]\nbest = (0 ... n).min_by { |i| (h[i] - h[i+1]).abs }\nprint (best%n)+1, \" \", ((best+1)%n) + 1\n"}, {"source_code": "gets\nh = gets.split(\" \").map{|s| s.to_i}\nn = h.length\nh << h[0]\nbest = 0\n(0...n).each do |i|\n if (h[i] - h[i+1]).abs < (h[best] - h[best+1]).abs\n best = i\n end\nend\n\nprint (best%n)+1, \" \", ((best+1)%n) + 1"}, {"source_code": "n = gets.to_i\na = gets.split.map { |x| x.to_i }\n\nmin = 1_000\nmin_ind = nil\nfor i in 1...n\n diff = (a[i] - a[i - 1]).abs\n if diff < min\n min = diff\n min_ind = i\n end\nend\n\nif (a.last - a.first).abs < min\n min_ind = n\n next_min_ind = 1\nelse\n next_min_ind = min_ind + 1\nend\nputs \"#{min_ind} #{next_min_ind}\""}, {"source_code": "n, s, min, idx = gets.to_i, gets.strip.split.map(&:to_i), 0, idx\n\n(print [1, n].join \" \"; exit) if s[0] == s[-1]\nmin, idx = (s[0] - s[-1]).abs, [1, n]\n\n(0...n - 1).each do |i|\n a, b = i, i + 1\n (print [a + 1, b + 1].join \" \"; exit) if s[a] == s[b]\n m = (s[a] - s[b]).abs\n if m < min\n min = m\n idx = [a + 1, b + 1]\n end\nend\n\nprint idx.join \" \""}], "negative_code": [{"source_code": "tot, min, min_index, input = gets.to_i, 20000, -1, gets.split(' ').map{|n| n.to_i}\ninput.each_cons(2).to_a.each_with_index { |(a, b), index| min, min_index = (a - b).abs, index if (((a - b).abs) < min) }\nmin_index = tot - 1 if (min > (input[0] - input[input.length - 1]).abs)\nmax_index = (min_index == tot - 1) ? 0 : min_index + 1\np (min_index + 1).to_s + ' ' + (max_index + 1).to_s"}], "src_uid": "facd9cd4fc1e53f50a1e6f947d78e942"} {"nl": {"description": "You are given an array $$$a$$$ of length $$$n$$$.You are also given a set of distinct positions $$$p_1, p_2, \\dots, p_m$$$, where $$$1 \\le p_i < n$$$. The position $$$p_i$$$ means that you can swap elements $$$a[p_i]$$$ and $$$a[p_i + 1]$$$. You can apply this operation any number of times for each of the given positions.Your task is to determine if it is possible to sort the initial array in non-decreasing order ($$$a_1 \\le a_2 \\le \\dots \\le a_n$$$) using only allowed swaps.For example, if $$$a = [3, 2, 1]$$$ and $$$p = [1, 2]$$$, then we can first swap elements $$$a[2]$$$ and $$$a[3]$$$ (because position $$$2$$$ is contained in the given set $$$p$$$). We get the array $$$a = [3, 1, 2]$$$. Then we swap $$$a[1]$$$ and $$$a[2]$$$ (position $$$1$$$ is also contained in $$$p$$$). We get the array $$$a = [1, 3, 2]$$$. Finally, we swap $$$a[2]$$$ and $$$a[3]$$$ again and get the array $$$a = [1, 2, 3]$$$, sorted in non-decreasing order.You can see that if $$$a = [4, 1, 2, 3]$$$ and $$$p = [3, 2]$$$ then you cannot sort the array.You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le m < n \\le 100$$$) — the number of elements in $$$a$$$ and the number of elements in $$$p$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 100$$$). The third line of the test case contains $$$m$$$ integers $$$p_1, p_2, \\dots, p_m$$$ ($$$1 \\le p_i < n$$$, all $$$p_i$$$ are distinct) — the set of positions described in the problem statement.", "output_spec": "For each test case, print the answer — \"YES\" (without quotes) if you can sort the initial array in non-decreasing order ($$$a_1 \\le a_2 \\le \\dots \\le a_n$$$) using only allowed swaps. Otherwise, print \"NO\".", "sample_inputs": ["6\n3 2\n3 2 1\n1 2\n4 2\n4 1 2 3\n3 2\n5 1\n1 2 3 4 5\n1\n4 2\n2 1 4 3\n1 3\n4 2\n4 3 2 1\n1 3\n5 2\n2 1 2 3 3\n1 4"], "sample_outputs": ["YES\nNO\nYES\nYES\nNO\nYES"], "notes": null}, "positive_code": [{"source_code": "def read_array(to)\n gets.strip.split(' ').map(&to)\nend\n\n\n# [1,2], [3,4], (2,3)\n\ngets.strip.to_i.times do\n _ = read_array(:to_i)\n a = read_array(:to_i)\n p = read_array(:to_i).map { |e| e - 1 }.sort\n ranges = []\n p.each_with_index do |e, i|\n if ranges.all? { |r| (r & [e,e+1]).empty? }\n ranges << [e,e+1]\n else\n ranges = ranges.select { |r| (r & [e,e+1]).empty? } +\n [ [[e,e+1], ranges.select { |r| !(r & [e,e+1]).empty? }].flatten.uniq.sort ]\n end\n end\n ranges.each do |r|\n a[r[0]..r[-1]] = a[r[0]..r[-1]].sort\n end\n puts a == a.sort ? \"YES\" : \"NO\"\nend"}, {"source_code": "n=gets.to_i\narray = sets = []\n(1..n).each do |x|\n a,b=gets.chomp.split(\" \")\n array=gets.chomp.split(\" \").map(&:to_i)\n sets=gets.chomp.split(\" \").map(&:to_i)\n sets = sets.sort\n s = e = sets[0]\n sets.each do |set|\n if e != set\n array[s-1..e-1] = array[s-1..e-1].sort!\n s = set\n end\n e = set + 1\n end\n array[s-1..e-1] = array[s-1..e-1].sort!\n c = array[0]\n check = false\n array.each do |arr|\n if arr >= c\n c = arr\n else\n print(\"NO\\n\")\n check = true\n break\n end\n end\n print(\"YES\\n\") unless check\nend"}], "negative_code": [{"source_code": "def read_array(to)\n gets.strip.split(' ').map(&to)\nend\n\ngets.strip.to_i.times do\n _ = read_array(:to_i)\n a = read_array(:to_i)\n p = read_array(:to_i).map { |e| e - 1 }\n ranges = []\n p.each do |e|\n found = false\n ranges.map! do |r|\n unless (r & [e, e + 1]).empty?\n found = true\n r = (r + [e, e + 1]).uniq.sort\n else\n r\n end\n end\n unless found\n ranges << [e, e+1]\n end\n end\n ranges.each do |r|\n a[r[0]..r[-1]] = a[r[0]..r[-1]].sort\n end\n puts a == a.sort ? \"YES\" : \"NO\"\nend"}], "src_uid": "e1481b9d940407da75e11355b580f459"} {"nl": {"description": "There are n integers b1, b2, ..., bn written in a row. For all i from 1 to n, values ai are defined by the crows performing the following procedure: The crow sets ai initially 0. The crow then adds bi to ai, subtracts bi + 1, adds the bi + 2 number, and so on until the n'th number. Thus, ai = bi - bi + 1 + bi + 2 - bi + 3.... Memory gives you the values a1, a2, ..., an, and he now wants you to find the initial numbers b1, b2, ..., bn written in the row? Can you do it?", "input_spec": "The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of integers written in the row. The next line contains n, the i'th of which is ai ( - 109 ≤ ai ≤ 109) — the value of the i'th number.", "output_spec": "Print n integers corresponding to the sequence b1, b2, ..., bn. It's guaranteed that the answer is unique and fits in 32-bit integer type.", "sample_inputs": ["5\n6 -4 8 -2 3", "5\n3 -2 -1 5 6"], "sample_outputs": ["2 4 6 1 3", "1 -3 4 11 6"], "notes": "NoteIn the first sample test, the crows report the numbers 6, - 4, 8, - 2, and 3 when he starts at indices 1, 2, 3, 4 and 5 respectively. It is easy to check that the sequence 2 4 6 1 3 satisfies the reports. For example, 6 = 2 - 4 + 6 - 1 + 3, and  - 4 = 4 - 6 + 1 - 3.In the second sample test, the sequence 1,  - 3, 4, 11, 6 satisfies the reports. For example, 5 = 11 - 6 and 6 = 6."}, "positive_code": [{"source_code": "gets;puts (gets.split.map(&:to_i)<<0).each_cons(2).map{|x,y|x+y}*' '"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nb = []\nsum = 0\na.reverse_each do |val|\n\tb.unshift val + sum\n\tsum = b[0] - sum\nend\n\nputs b * \" \"\n"}, {"source_code": "n = gets.chomp.to_i\narr = gets.chomp.split.map(&:to_i)\nputs (arr.each_cons(2).map { |i, j| i + j } << arr.last).join(' ')"}, {"source_code": "n=gets.to_i\nx=gets.split.map{|e| e.to_i}\nbt=0\nans=[]\nx.reverse.each{|e|\n\tbi=e+bt\n\tans< (n - 1) * (n - 2) / 2 + 1 then\n\tp -1\nelse\n\tw = v == 1 ? 2 : 1\n\tputs \"#{v} #{w}\"\n\ta = [*1..n]\n\ta.delete(w)\n\t[*0..a.size-2].each{|i| puts \"#{a[i]} #{a[i + 1]}\"}\n\tm -= n - 2\n\t[*0..a.size-1].each{|i|\n\t\t[*i+2..a.size-1].each{|j|\n\t\t\texit if (m -= 1) == 0\n\t\t\tputs \"#{a[i]} #{a[j]}\"\n\t\t}\n\t}\nend\n\n"}, {"source_code": "def solve(n, m, v)\n if m < n-1\n puts -1\n return\n end\n if m > (n-1)*(n-2)/2 + 1\n puts -1\n return\n end\n other = v % n + 1\n a = (1..n).to_a - [v, other]\n a = [v] + a + [other]\n puts \"#{v} #{other}\"\n m -= 1\n 0.upto(n-2) do |i|\n break if m == 0\n (i+1).upto(n-2) do |j|\n break if m == 0\n puts \"#{a[i]} #{a[j]}\"\n m -= 1\n end\n end\nend\n\nn, m, v = *gets.split(' ').map { |s| s.to_i }\nsolve(n, m, v)\n"}], "negative_code": [{"source_code": "n, m, v = gets.split.map(&:to_i)\nif m < n - 1 || m > (n - 1) * (n - 2) / 2 + 1 then\n\tp -1\nelse\n\tputs \"#{v} #{v == 1 ? 2 : 1}\"\n\ta = [*1..n]\n\ta.delete(v)\n\t[*0..a.size-2].each{|i| puts \"#{a[i]} #{a[i + 1]}\"}\n\tm -= n - 2\n\t[*0..a.size-1].each{|i|\n\t\t[*i+2..a.size-1].each{|j|\n\t\t\texit if (m -= 1) == 0\n\t\t\tputs \"#{a[i]} #{a[j]}\"\n\t\t}\n\t}\nend\n\n"}], "src_uid": "959709bfe7b26a4b9f2e7430350650a9"} {"nl": {"description": "A chess tournament will be held soon, where $$$n$$$ chess players will take part. Every participant will play one game against every other participant. Each game ends in either a win for one player and a loss for another player, or a draw for both players.Each of the players has their own expectations about the tournament, they can be one of two types: a player wants not to lose any game (i. e. finish the tournament with zero losses); a player wants to win at least one game. You have to determine if there exists an outcome for all the matches such that all the players meet their expectations. If there are several possible outcomes, print any of them. If there are none, report that it's impossible.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 200$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$2 \\le n \\le 50$$$) — the number of chess players. The second line contains the string $$$s$$$ ($$$|s| = n$$$; $$$s_i \\in \\{1, 2\\}$$$). If $$$s_i = 1$$$, then the $$$i$$$-th player has expectations of the first type, otherwise of the second type.", "output_spec": "For each test case, print the answer in the following format: In the first line, print NO if it is impossible to meet the expectations of all players. Otherwise, print YES, and the matrix of size $$$n \\times n$$$ in the next $$$n$$$ lines. The matrix element in the $$$i$$$-th row and $$$j$$$-th column should be equal to: +, if the $$$i$$$-th player won in a game against the $$$j$$$-th player; -, if the $$$i$$$-th player lost in a game against the $$$j$$$-th player; =, if the $$$i$$$-th and $$$j$$$-th players' game resulted in a draw; X, if $$$i = j$$$. ", "sample_inputs": ["3\n3\n111\n2\n21\n4\n2122"], "sample_outputs": ["YES\nX==\n=X=\n==X\nNO\nYES\nX--+\n+X++\n+-X-\n--+X"], "notes": null}, "positive_code": [{"source_code": "gets.to_i.times do\r\n\r\nn = gets.to_i\r\ns = gets.chomp.chars.map!(&:to_i)\r\n\r\nn1 = s.count(1)\r\nn2 = n - n1\r\n\r\nif n2 == 1 || n2 == 2\r\n puts \"NO\"\r\n next\r\nend\r\n\r\nputs \"YES\"\r\n\r\nmtx = s.each_with_index.map do |si, i|\r\n next Array.new(n, '=') if si == 1\r\n\r\n Array.new(n) do |j|\r\n next '=' if s[j] == 1\r\n i < j ? '+' : '-'\r\n end\r\nend\r\n\r\nif n2 > 0\r\n i1 = s.index(2)\r\n i2 = s.rindex(2)\r\n mtx[i1][i2] = '-'\r\n mtx[i2][i1] = '+'\r\nend\r\n\r\nmtx.each_with_index { |row, i| row[i] = 'X' }\r\nmtx.each { |row| puts row.join }\r\n\r\nend\r\n"}], "negative_code": [], "src_uid": "7e15bb1d6040d786983865143d1799cd"} {"nl": {"description": "The King of Berland Polycarp LXXXIV has $$$n$$$ daughters. To establish his power to the neighbouring kingdoms he wants to marry his daughters to the princes of these kingdoms. As a lucky coincidence there are $$$n$$$ other kingdoms as well.So Polycarp LXXXIV has enumerated his daughters from $$$1$$$ to $$$n$$$ and the kingdoms from $$$1$$$ to $$$n$$$. For each daughter he has compiled a list of kingdoms princes of which she wanted to marry.Polycarp LXXXIV is very busy, so he finds a couple for his daughters greedily one after another.For the first daughter he takes the kingdom with the lowest number from her list and marries the daughter to their prince. For the second daughter he takes the kingdom with the lowest number from her list, prince of which hasn't been taken already. If there are no free princes in the list then the daughter marries nobody and Polycarp LXXXIV proceeds to the next daughter. The process ends after the $$$n$$$-th daughter.For example, let there be $$$4$$$ daughters and kingdoms, the lists daughters have are $$$[2, 3]$$$, $$$[1, 2]$$$, $$$[3, 4]$$$, $$$[3]$$$, respectively. In that case daughter $$$1$$$ marries the prince of kingdom $$$2$$$, daughter $$$2$$$ marries the prince of kingdom $$$1$$$, daughter $$$3$$$ marries the prince of kingdom $$$3$$$, leaving daughter $$$4$$$ nobody to marry to.Actually, before starting the marriage process Polycarp LXXXIV has the time to convince one of his daughters that some prince is also worth marrying to. Effectively, that means that he can add exactly one kingdom to exactly one of his daughter's list. Note that this kingdom should not be present in the daughter's list.Polycarp LXXXIV wants to increase the number of married couples.Unfortunately, what he doesn't have the time for is determining what entry to add. If there is no way to increase the total number of married couples then output that the marriages are already optimal. Otherwise, find such an entry that the total number of married couples increases if Polycarp LXXXIV adds it.If there are multiple ways to add an entry so that the total number of married couples increases then print any of them.For your and our convenience you are asked to answer $$$t$$$ independent test cases.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^5$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$) — the number of daughters and the number of kingdoms. Each of the next $$$n$$$ lines contains the description of each daughter's list. The first integer $$$k$$$ ($$$0 \\le k \\le n$$$) is the number of entries in the $$$i$$$-th daughter's list. After that $$$k$$$ distinct integers follow $$$g_i[1], g_i[2], \\dots, g_i[k]$$$ ($$$1 \\le g_i[j] \\le n$$$) — the indices of the kingdoms in the list in the increasing order ($$$g_i[1] < g_i[2] < \\dots < g_i[k]$$$). It's guaranteed that the total number of daughters over all test cases does not exceed $$$10^5$$$. It's also guaranteed that the total number of kingdoms in lists over all test cases does not exceed $$$10^5$$$.", "output_spec": "For each test case print the answer to it. Print \"IMPROVE\" in the first line if Polycarp LXXXIV can add some kingdom to some of his daughter's list so that the total number of married couples increases. The second line then should contain two integers — the index of the daughter and the index of the kingdom Polycarp LXXXIV should add to that daughter's list. If there are multiple ways to add an entry so that the total number of married couples increases then print any of them. Otherwise the only line should contain one word \"OPTIMAL\".", "sample_inputs": ["5\n4\n2 2 3\n2 1 2\n2 3 4\n1 3\n2\n0\n0\n3\n3 1 2 3\n3 1 2 3\n3 1 2 3\n1\n1 1\n4\n1 1\n1 2\n1 3\n1 4"], "sample_outputs": ["IMPROVE\n4 4\nIMPROVE\n1 1\nOPTIMAL\nOPTIMAL\nOPTIMAL"], "notes": "NoteThe first test case is depicted in the statement. Adding the fourth kingdom to the list of the fourth daughter makes her marry the prince of the fourth kingdom.In the second test case any new entry will increase the number of marriages from $$$0$$$ to $$$1$$$.In the third and the fourth test cases there is no way to add an entry.In the fifth test case there is no way to change the marriages by adding any entry."}, "positive_code": [{"source_code": "#!usr/bin/env ruby\n\n# https://codeforces.com/problemset/problem/1327/B\n\nrequire 'set'\n\nt = gets.to_i\n\nt.times do\n n = gets.to_i\n\n options = []\n\n n.times do\n options << gets.split(' ').map(&:to_i).drop(1)\n end\n\n choices = Set.new\n first_untaken_princess = nil\n\n options.each_with_index do |op, num|\n first_untaken_princess ||= num + 1 if op.size == 0\n\n op.each_with_index do |prince, n|\n unless choices.include? prince\n choices << prince\n break\n end\n if n + 1 == op.size\n first_untaken_princess ||= num + 1\n end\n end\n end\n\n if first_untaken_princess\n first_untaken_prince = 1\n\n while true do\n if choices.include? first_untaken_prince\n first_untaken_prince += 1\n else\n break\n end\n end\n puts \"IMPROVE\\n#{first_untaken_princess} #{first_untaken_prince}\"\n else\n puts \"OPTIMAL\"\n end\nend\n"}, {"source_code": "t = gets.to_i\nt.times do\n n = gets.to_i\n k = []\n g = []\n da = Array.new(n, nil)\n pr = Array.new(n, nil)\n married = 0\n n.times do |i|\n k[i], *g[i] = gets.split.map(&:to_i)\n ok = false\n for j in 0...k[i] do\n if pr[g[i][j] - 1].nil?\n da[i] = g[i][j]\n pr[g[i][j] - 1] = i + 1\n ok = true\n break\n end\n end\n married += 1 if ok\n end\n if married < n\n puts \"IMPROVE\"\n puts \"#{da.index(nil) + 1} #{pr.index(nil) + 1}\"\n else\n puts \"OPTIMAL\"\n end\nend\n"}], "negative_code": [], "src_uid": "38911652b3c075354aa8adb2a4c6e362"} {"nl": {"description": "\"The zombies are lurking outside. Waiting. Moaning. And when they come...\"\"When they come?\"\"I hope the Wall is high enough.\"Zombie attacks have hit the Wall, our line of defense in the North. Its protection is failing, and cracks are showing. In places, gaps have appeared, splitting the wall into multiple segments. We call on you for help. Go forth and explore the wall! Report how many disconnected segments there are.The wall is a two-dimensional structure made of bricks. Each brick is one unit wide and one unit high. Bricks are stacked on top of each other to form columns that are up to R bricks high. Each brick is placed either on the ground or directly on top of another brick. Consecutive non-empty columns form a wall segment. The entire wall, all the segments and empty columns in-between, is C columns wide.", "input_spec": "The first line of the input consists of two space-separated integers R and C, 1 ≤ R, C ≤ 100. The next R lines provide a description of the columns as follows: each of the R lines contains a string of length C, the c-th character of line r is B if there is a brick in column c and row R - r + 1, and . otherwise. B", "output_spec": "The number of wall segments in the input configuration.", "sample_inputs": ["3 7\n.......\n.......\n.BB.B..", "4 5\n..B..\n..B..\nB.B.B\nBBB.B", "4 6\n..B...\nB.B.BB\nBBB.BB\nBBBBBB", "1 1\nB", "10 7\n.......\n.......\n.......\n.......\n.......\n.......\n.......\n.......\n...B...\nB.BB.B.", "8 8\n........\n........\n........\n........\n.B......\n.B.....B\n.B.....B\n.BB...BB"], "sample_outputs": ["2", "2", "1", "1", "3", "2"], "notes": "NoteIn the first sample case, the 2nd and 3rd columns define the first wall segment, and the 5th column defines the second."}, "positive_code": [{"source_code": "rows, cols = gets.split(' ').map(&:to_i)\n(rows-1).times { gets }\n\nparts = 0\npart = false\ngets.chomp.each_char do |ch|\n if ch == 'B'\n part = true if !part\n else\n parts += 1 if part\n part = false\n end\nend\n\nparts += 1 if part\n\nputs parts\n"}, {"source_code": "n , m = gets.split.map(&:to_i)\ns = \"\"\nn.times do\n s = gets.strip\nend\narr = s.split(\".\")\n\nans = 0\nfor i in arr\n if i.include? \"B\"\n ans +=1\n end\nend\nputs ans\n"}], "negative_code": [{"source_code": "n , m = gets.split.map(&:to_i)\ns = \"\"\nn.times do\n s = gets.strip\nend\narr = s.split(\".\")\nputs arr\nans = 0\nfor i in arr\n if i.include? \"B\"\n ans +=1\n end\nend\nputs ans\n"}], "src_uid": "c339795767a174a59225a79023b5d14f"} {"nl": {"description": "You've got table a, consisting of n rows, numbered from 1 to n. The i-th line of table a contains ci cells, at that for all i (1 < i ≤ n) holds ci ≤ ci - 1. Let's denote s as the total number of cells of table a, that is, . We know that each cell of the table contains a single integer from 1 to s, at that all written integers are distinct. Let's assume that the cells of the i-th row of table a are numbered from 1 to ci, then let's denote the number written in the j-th cell of the i-th row as ai, j. Your task is to perform several swap operations to rearrange the numbers in the table so as to fulfill the following conditions: for all i, j (1 < i ≤ n; 1 ≤ j ≤ ci) holds ai, j > ai - 1, j; for all i, j (1 ≤ i ≤ n; 1 < j ≤ ci) holds ai, j > ai, j - 1. In one swap operation you are allowed to choose two different cells of the table and swap the recorded there numbers, that is the number that was recorded in the first of the selected cells before the swap, is written in the second cell after it. Similarly, the number that was recorded in the second of the selected cells, is written in the first cell after the swap.Rearrange the numbers in the required manner. Note that you are allowed to perform any number of operations, but not more than s. You do not have to minimize the number of operations.", "input_spec": "The first line contains a single integer n (1 ≤ n ≤ 50) that shows the number of rows in the table. The second line contains n space-separated integers ci (1 ≤ ci ≤ 50; ci ≤ ci - 1) — the numbers of cells on the corresponding rows. Next n lines contain table а. The i-th of them contains ci space-separated integers: the j-th integer in this line represents ai, j. It is guaranteed that all the given numbers ai, j are positive and do not exceed s. It is guaranteed that all ai, j are distinct.", "output_spec": "In the first line print a single integer m (0 ≤ m ≤ s), representing the number of performed swaps. In the next m lines print the description of these swap operations. In the i-th line print four space-separated integers xi, yi, pi, qi (1 ≤ xi, pi ≤ n; 1 ≤ yi ≤ cxi; 1 ≤ qi ≤ cpi). The printed numbers denote swapping the contents of cells axi, yi and api, qi. Note that a swap operation can change the contents of distinct table cells. Print the swaps in the order, in which they should be executed.", "sample_inputs": ["3\n3 2 1\n4 3 5\n6 1\n2", "1\n4\n4 3 2 1"], "sample_outputs": ["2\n1 1 2 2\n2 1 3 1", "2\n1 1 1 4\n1 2 1 3"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\ngets\n\na = Array.new(n) {\n gets.split.map{|x| x.to_i - 1}\n}\n\nb = []\nn.times {|i|\n a[i].each_index {|j|\n b << [i, j]\n }\n}\n\nc = []\n50.times {\n n.times {|i|\n a[i].each_index {|j|\n t = a[i][j]\n next if b[t] == [i, j]\n x, y = *b[t]\n a[i][j], a[x][y] = a[x][y], a[i][j]\n c << [i, j, x, y].map(&:succ)\n }\n}\n}\np c.size\nputs c.map{|i| i * ' '}\n"}, {"source_code": "n = gets.chomp.to_i\nc = gets.chomp.split(\" \").map{|e| e.to_i}\ntables = []\nmap = {}\n1.upto(n) do |y|\n col = gets.chomp.split(\" \").map{|e| e.to_i}\n tables << col\n \n col.each_with_index do |num, x|\n map[num] = [y, x + 1]\n end \nend\nans = []\nk = 0\nidx = 1\nfor i in 1 .. n do\n for j in 1 .. c[i - 1] do\n k += 1\n next if tables[i - 1][j - 1] == k\n \n y = map[k].first\n x = map[k].last\n \n ans << [i, j, y, x]\n \n # swap\n tmp = tables[i - 1][j - 1]\n tables[i - 1][j - 1] = k\n tables[y - 1][x - 1] = tmp \n \n map[k] = [i, j]\n map[tmp] = [y, x] \n end\nend\n\nputs ans.length\nans.each do |line|\n puts line.join(\" \")\nend\n"}, {"source_code": "#!/usr/bin/env ruby\nn = gets.to_i\ngets\nt = []\nn.times do\n t << gets.split.map(&:to_i)\nend\nresults = []\nn.times do |i|\n t[i].length.times do |j|\n mi, mj = i, j\n (i..(n - 1)).each do |ni|\n (j..(t[ni].length - 1)).each do |nj|\n if t[ni][nj] < t[mi][mj]\n mi, mj = ni, nj\n end\n end\n end\n if mi != i || mj != j\n results.push([i, j, mi, mj])\n t[i][j], t[mi][mj] = t[mi][mj], t[i][j]\n end\n end\nend\nputs results.length\nresults.each do |r|\n puts r.map { |x| x + 1 }.join(\" \")\nend"}, {"source_code": "n = gets.to_i\nc = gets.chomp.split.map(&:to_i)\ns = c.inject(&:+)\na = []\nl = []\no = []\nn.times do |i|\n a[i] = gets.chomp.split.map(&:to_i)\n 0.upto(c[i]-1) do |j|\n l[a[i][j]] = [i,j]\n o[i+j] = [] if o[i+j].nil?\n o[i+j].push([i,j])\n end\nend\n\nc = 0\nmoves = []\ncurr = 0\n1.upto(s) do |i|\n curr = curr + 1 if o[curr].empty?\n tr, tc = o[curr].pop\n fr, fc = l[i]\n unless tr == fr && tc == fc\n moves.push([tr+1,tc+1,fr+1,fc+1])\n l[a[fr][fc]] = [tr,tc]\n l[a[tr][tc]] = [fr,fc]\n a[fr][fc], a[tr][tc] = a[tr][tc], a[fr][fc]\n c += 1\n end\nend\nputs c\nc.times do |i|\n puts moves[i].join(\" \")\nend\n"}], "negative_code": [{"source_code": "n = gets.to_i\ngets\n\na = Array.new(n) {\n gets.split.map{|x| x.to_i - 1}\n}\n\nb = []\nn.times {|i|\n a[i].each_index {|j|\n b << [i, j]\n }\n}\n\nc = []\nn.times {|i|\n a[i].each_index {|j|\n t = a[i][j]\n next if b[t] == [i, j]\n x, y = *b[t]\n a[i][j], a[x][y] = a[x][y], a[i][j]\n c << [i, j, x, y].map(&:succ)\n }\n}\np c.size\nputs c.map{|i| i * ' '}\n"}, {"source_code": "n = gets.chomp.to_i\nc = gets.chomp.split(\" \").map{|e| e.to_i}\ntables = []\nmap = {}\n1.upto(n) do |y|\n col = gets.chomp.split(\" \").map{|e| e.to_i}\n tables << col\n \n col.each_with_index do |num, x|\n map[num] = [y, x + 1]\n end \nend\nans = []\np map\nk = 0\nidx = 1\nfor i in 1 .. n do\n for j in 1 .. c[i - 1] do\n k += 1\n next if tables[i - 1][j - 1] == k\n \n y = map[k].first\n x = map[k].last\n \n ans << [i, j, y, x]\n \n # swap\n tmp = tables[i - 1][j - 1]\n tables[i - 1][j - 1] = k\n tables[y - 1][x - 1] = tmp \n \n map[k] = [i, j]\n map[tmp] = [y, x] \n end\nend\n\nputs ans.length\nans.each do |line|\n puts line.join(\" \")\nend\n"}], "src_uid": "62df8b1821558bea910f422591618e29"} {"nl": {"description": "It is the easy version of the problem. The only difference is that in this version $$$n = 1$$$.In the cinema seats can be represented as the table with $$$n$$$ rows and $$$m$$$ columns. The rows are numbered with integers from $$$1$$$ to $$$n$$$. The seats in each row are numbered with consecutive integers from left to right: in the $$$k$$$-th row from $$$m (k - 1) + 1$$$ to $$$m k$$$ for all rows $$$1 \\le k \\le n$$$. $$$1$$$$$$2$$$$$$\\cdots$$$$$$m - 1$$$$$$m$$$$$$m + 1$$$$$$m + 2$$$$$$\\cdots$$$$$$2 m - 1$$$$$$2 m$$$$$$2m + 1$$$$$$2m + 2$$$$$$\\cdots$$$$$$3 m - 1$$$$$$3 m$$$$$$\\vdots$$$$$$\\vdots$$$$$$\\ddots$$$$$$\\vdots$$$$$$\\vdots$$$$$$m (n - 1) + 1$$$$$$m (n - 1) + 2$$$$$$\\cdots$$$$$$n m - 1$$$$$$n m$$$ The table with seats indices There are $$$nm$$$ people who want to go to the cinema to watch a new film. They are numbered with integers from $$$1$$$ to $$$nm$$$. You should give exactly one seat to each person.It is known, that in this cinema as lower seat index you have as better you can see everything happening on the screen. $$$i$$$-th person has the level of sight $$$a_i$$$. Let's define $$$s_i$$$ as the seat index, that will be given to $$$i$$$-th person. You want to give better places for people with lower sight levels, so for any two people $$$i$$$, $$$j$$$ such that $$$a_i < a_j$$$ it should be satisfied that $$$s_i < s_j$$$.After you will give seats to all people they will start coming to their seats. In the order from $$$1$$$ to $$$nm$$$, each person will enter the hall and sit in their seat. To get to their place, the person will go to their seat's row and start moving from the first seat in this row to theirs from left to right. While moving some places will be free, some will be occupied with people already seated. The inconvenience of the person is equal to the number of occupied seats he or she will go through.Let's consider an example: $$$m = 5$$$, the person has the seat $$$4$$$ in the first row, the seats $$$1$$$, $$$3$$$, $$$5$$$ in the first row are already occupied, the seats $$$2$$$ and $$$4$$$ are free. The inconvenience of this person will be $$$2$$$, because he will go through occupied seats $$$1$$$ and $$$3$$$.Find the minimal total inconvenience (the sum of inconveniences of all people), that is possible to have by giving places for all people (all conditions should be satisfied).", "input_spec": "The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$n = 1$$$, $$$1 \\le m \\le 300$$$) — the number of rows and places in each row respectively. The second line of each test case contains $$$n \\cdot m$$$ integers $$$a_1, a_2, \\ldots, a_{n \\cdot m}$$$ ($$$1 \\le a_i \\le 10^9$$$), where $$$a_i$$$ is the sight level of $$$i$$$-th person. It's guaranteed that the sum of $$$n \\cdot m$$$ over all test cases does not exceed $$$10^5$$$.", "output_spec": "For each test case print a single integer — the minimal total inconvenience that can be achieved.", "sample_inputs": ["4\n1 3\n1 2 3\n1 5\n2 1 5 3 3\n1 2\n2 1\n1 6\n2 3 2 1 1 1"], "sample_outputs": ["3\n6\n0\n1"], "notes": "NoteIn the first test case, there is a single way to arrange people, because all sight levels are distinct. The first person will sit on the first seat, the second person will sit on the second place, the third person will sit on the third place. So inconvenience of the first person will be $$$0$$$, inconvenience of the second person will be $$$1$$$ and inconvenience of the third person will be $$$2$$$. The total inconvenience is $$$0 + 1 + 2 = 3$$$.In the second test case, people should sit as follows: $$$s_1 = 2$$$, $$$s_2 = 1$$$, $$$s_3 = 5$$$, $$$s_4 = 4$$$, $$$s_5 = 3$$$. The total inconvenience will be $$$6$$$."}, "positive_code": [{"source_code": "module AtCoder\r\n class FenwickTree\r\n def initialize(n = 0)\r\n @n = n\r\n @data = Array.new(n, 0).unshift(nil)\r\n end\r\n\r\n def add(idx, x)\r\n idx += 1\r\n while idx <= @n\r\n @data[idx] += x\r\n idx += idx & -idx\r\n end\r\n self\r\n end\r\n\r\n def sum(l, r)\r\n __sum__(r) - __sum__(l)\r\n end\r\n\r\n private\r\n\r\n def __sum__(r)\r\n s = 0\r\n while r > 0\r\n s += @data[r]\r\n r ^= r & -r\r\n end\r\n s\r\n end\r\n end\r\nend\r\n\r\nt = gets.to_i\r\nt.times do\r\n\r\nn, m = gets.split.map!(&:to_i)\r\na = gets.split.map!(&:to_i)\r\n\r\norder = a.each_with_index.sort_by { |ai, i| [ai, -i] }.map(&:last)\r\n\r\nft = AtCoder::FenwickTree.new(m)\r\nscore = 0\r\norder.each do |i|\r\n score += ft.sum(0, i)\r\n ft.add(i, 1)\r\nend\r\n\r\nputs score\r\n\r\nend\r\n"}], "negative_code": [], "src_uid": "5b95da35a4c1251f5376cf3bacc1a549"} {"nl": {"description": "Sonya decided that having her own hotel business is the best way of earning money because she can profit and rest wherever she wants.The country where Sonya lives is an endless line. There is a city in each integer coordinate on this line. She has $$$n$$$ hotels, where the $$$i$$$-th hotel is located in the city with coordinate $$$x_i$$$. Sonya is a smart girl, so she does not open two or more hotels in the same city.Sonya understands that her business needs to be expanded by opening new hotels, so she decides to build one more. She wants to make the minimum distance from this hotel to all others to be equal to $$$d$$$. The girl understands that there are many possible locations to construct such a hotel. Thus she wants to know the number of possible coordinates of the cities where she can build a new hotel. Because Sonya is lounging in a jacuzzi in one of her hotels, she is asking you to find the number of cities where she can build a new hotel so that the minimum distance from the original $$$n$$$ hotels to the new one is equal to $$$d$$$.", "input_spec": "The first line contains two integers $$$n$$$ and $$$d$$$ ($$$1\\leq n\\leq 100$$$, $$$1\\leq d\\leq 10^9$$$) — the number of Sonya's hotels and the needed minimum distance from a new hotel to all others. The second line contains $$$n$$$ different integers in strictly increasing order $$$x_1, x_2, \\ldots, x_n$$$ ($$$-10^9\\leq x_i\\leq 10^9$$$) — coordinates of Sonya's hotels.", "output_spec": "Print the number of cities where Sonya can build a new hotel so that the minimum distance from this hotel to all others is equal to $$$d$$$.", "sample_inputs": ["4 3\n-3 2 9 16", "5 2\n4 8 11 18 19"], "sample_outputs": ["6", "5"], "notes": "NoteIn the first example, there are $$$6$$$ possible cities where Sonya can build a hotel. These cities have coordinates $$$-6$$$, $$$5$$$, $$$6$$$, $$$12$$$, $$$13$$$, and $$$19$$$.In the second example, there are $$$5$$$ possible cities where Sonya can build a hotel. These cities have coordinates $$$2$$$, $$$6$$$, $$$13$$$, $$$16$$$, and $$$21$$$."}, "positive_code": [{"source_code": "n,d = STDIN.readline.strip.split(/\\s+/).map(&:to_i)\ncoordinates = STDIN.readline.strip.split(/\\s+/).map(&:to_i)\n\ncount = 2\n(0..coordinates.length - 2).each do |i|\n if coordinates[i] + d == coordinates[i+1] - d\n count += 1\n else\n if coordinates[i] + d < coordinates[i+1] - d\n count += 1\n end\n if coordinates[i+1] - d > coordinates[i] + d\n count += 1\n end\n end\nend\n\nputs count\n"}, {"source_code": "n, d = gets.chomp.split.map(&:to_i)\nx = gets.chomp.split.map(&:to_i)\na = {}\nx.each do |i|\n\ta[i-d] =0; a[i+d] = 0;\nend\n\nac = 0\na.keys.each do |i|\n\tflag = 1\n\tx.each do |j|\n\t\tflag = 0 if ((i-j).abs < d)\n\tend\n\n\tac += flag\nend\n\nprint ac"}, {"source_code": "n, d = gets.split.map(&:to_i)\ncities = gets.split.map(&:to_i)\n\ncount = 2\n\ncities.each_cons(2) do |city1, city2|\n \n if city1 + d < city2 - d\n count += 2\n elsif city1 + d == city2 - d\n count += 1\n end \n\nend\n\nputs count"}, {"source_code": "require 'set'\n\ndef solution\n m, d = read_ints\n cs = read_ints\n\n cities = Set.new\n\n cs.each.with_index do |c, i|\n before = c - d\n after = c + d\n\n cities << before if i == 0 || before - cs[i-1] >= d\n cities << after if i == cs.size-1 || cs[i+1] - after >= d\n end\n\n puts cities.size\n\nend\n\ndef read_int\n gets.to_i\nend\n\ndef read_ints\n gets.split.map(&:to_i)\nend\n\ndef read_string\n gets.chomp\nend\n\nsolution unless ENV['TEST__MODE']\n"}, {"source_code": "N, D = gets.split.map(&:to_i)\nxs = gets.split.map(&:to_i)\n\nans = 2\n(N-1).times do |i|\n if xs[i] < xs[i+1] - 2*D\n ans += 2\n elsif xs[i] == xs[i+1] - 2*D\n ans += 1\n end\nend\nputs ans"}, {"source_code": "n, d = gets.strip.split.map(&:to_i)\nary = gets.strip.split.map(&:to_i)\nary.sort!\n\npossible_locations = []\nary.each do |c|\n possible_locations << (c+d)\n possible_locations << (c-d)\nend\npossible_locations.uniq!\nans = 0\npossible_locations.each do |nc|\n # puts nc\n # puts ary.map{|oc| (nc-oc).abs >= d }.inspect\n if ary.map{|oc| (nc-oc).abs }.min == d\n # puts nc\n ans += 1\n end\nend\nputs ans"}, {"source_code": "$n,$d=gets.split\n$n,$d=$n.to_i,$d.to_i\na=gets.split\ni,ans,len=0,0,a.length\ndef test(val,id)\n\tyield id,val+$d\n\tyield id,val-$d\nend\na.each do |c|\n\ttest(c.to_i,i){|id,pos| \n\t\t\tflag=1\n\t\t\tif id>0 and pos-a[id-1].to_i<$d \n\t\t\tflag=0\n\t\t\tend\n\t\t\tif id coordinates[i]\n count += 1\n end\n end\nend\n\nputs count\n"}, {"source_code": "n, d = gets.split.map(&:to_i)\ncities = gets.split.map(&:to_i)\n\ncount = 2\n\ncities.each_cons(3) do |city1, city2, city3|\n \n if city2 - d > city1 + d\n count += 1\n end\n if city2 - d == city1 + d\n count += 1\n end\n if city2 + d < city3 - d\n count += 1\n end\n\nend\n\nif cities.count == 1\n puts '2'\nelse\n if cities[0] + d < cities[1] - d\n count += 1\n end\n if cities[n - 2] + d < cities[n - 1] - d\n count += 1\n end\n\n puts count\nend"}], "src_uid": "5babbb7c5f6b494992ffa921c8e19294"} {"nl": {"description": "Little girl Tanya climbs the stairs inside a multi-storey building. Every time Tanya climbs a stairway, she starts counting steps from $$$1$$$ to the number of steps in this stairway. She speaks every number aloud. For example, if she climbs two stairways, the first of which contains $$$3$$$ steps, and the second contains $$$4$$$ steps, she will pronounce the numbers $$$1, 2, 3, 1, 2, 3, 4$$$.You are given all the numbers pronounced by Tanya. How many stairways did she climb? Also, output the number of steps in each stairway.The given sequence will be a valid sequence that Tanya could have pronounced when climbing one or more stairways.", "input_spec": "The first line contains $$$n$$$ ($$$1 \\le n \\le 1000$$$) — the total number of numbers pronounced by Tanya. The second line contains integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 1000$$$) — all the numbers Tanya pronounced while climbing the stairs, in order from the first to the last pronounced number. Passing a stairway with $$$x$$$ steps, she will pronounce the numbers $$$1, 2, \\dots, x$$$ in that order. The given sequence will be a valid sequence that Tanya could have pronounced when climbing one or more stairways.", "output_spec": "In the first line, output $$$t$$$ — the number of stairways that Tanya climbed. In the second line, output $$$t$$$ numbers — the number of steps in each stairway she climbed. Write the numbers in the correct order of passage of the stairways.", "sample_inputs": ["7\n1 2 3 1 2 3 4", "4\n1 1 1 1", "5\n1 2 3 4 5", "5\n1 2 1 2 1"], "sample_outputs": ["2\n3 4", "4\n1 1 1 1", "1\n5", "3\n2 2 1"], "notes": null}, "positive_code": [{"source_code": "n = STDIN.readline.to_i\nnumbers = STDIN.readline.strip.split(/\\s+/).map(&:to_i)\n\nindices = []\n(0..numbers.length - 1).each do |i|\n indices << i if numbers[i] == 1\nend\nindices << numbers.length\n\noutputs = []\n(0..indices.length - 2).each do |i|\n outputs << indices[i + 1] - indices[i]\nend\n\nputs outputs.length\nputs outputs.join(' ')\n"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split.map(&:to_i)\n\nz = 0\ncount = 0\nac = []\na.each_with_index do |x, i|\n\tif x == z + 1\n\t\tz += 1\n\t\tnext\n\telse\n\t\tac << z\n\t\tcount +=1\n\t\tz = x\n\tend\nend\n\nac << z\nputs count + 1\nputs ac.join(\" \")"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nans = a.chunk_while { _1 < _2 }.map(&:size)\nputs ans.size, ans.join(\" \")\n"}, {"source_code": "gets;a=(gets.split.map(&:to_i)+[1]).each_cons(2).select{|x,y|x>=y}.map{|x,y|x};p a.size;puts a*' '"}, {"source_code": "gets\nnumbers = gets.split.map(&:to_i)\n\nstair_sizes = []\ncrt = 0\n\nnumbers.each do |n|\nif n == crt + 1\n crt += 1\nelse\n stair_sizes.push crt\n crt = 1\nend\nend\nstair_sizes.push crt\n\np stair_sizes.size\nputs stair_sizes.join ' '\n"}, {"source_code": "N = gets.to_i\nas = gets.split.map(&:to_i)\nans = []\nas.each do |a|\n if a == 1\n ans.push(a)\n else\n ans[-1] = a\n end\nend\nputs ans.length\nputs ans.join(' ')"}, {"source_code": "t = gets.to_i\nary = gets.strip.split.map(&:to_i)\n\nif t==1\n puts \"1\\n1\"\nelse\n steps = []\n 0.upto(t-2).each do |x|\n steps << ary[x] if ary[x+1]==1\n end\n steps << ary[-1]\n puts steps.size\n puts steps.join(\" \")\nend"}, {"source_code": "number_of_elems = gets.strip.to_i\narray = gets.strip.split.to_a.collect { |val| val.to_i }\nanswer = []\n(1...array.size).each do |i|\n answer << array[i-1] if array[i] <= array[i-1]\nend\nanswer << array[-1]\nputs answer.size\nputs answer.join(' ')\n"}, {"source_code": "n = gets.to_i\nsteps = gets.split.map(&:to_i)\nstepss = []\nt = []\nsteps.each do |step|\n if step == 1\n stepss << t unless t.empty?\n t = [1]\n else\n t << step\n end\nend\nstepss << t\nputs stepss.count\nputs stepss.map { |a| a.max }.join(\" \")"}, {"source_code": "\nx = gets.chomp.to_i\nstairways = gets.strip.split.map(&:to_i)\nres = 1\narr = Array.new\nfor i in 1 ... x\n if stairways[i] > stairways[i-1]\n res +=1\n else if !(stairways[i] > stairways[i-1])\n arr.push(res)\n res=1\n end\n end\nend\narr.push(res)\nputs arr.size\nputs arr.join(' ')\n"}, {"source_code": "number = gets().to_i\nlist = gets().chomp.split.map(&:to_i)\n\nans = []\ncount = 0\nfor i in 0...list.length\n\n if(list[i+1] == 1 || i == list.length-1)\n ans.push(list[i])\n end\n if(list[i] == 1)\n count = count+1\n end\nend\n\nputs count\nfor i in 0...ans.length\n print ans[i]\n print \" \"\nend\n"}, {"source_code": "gets\nt = []\nt << gets.split.map{|x| x.to_i}.inject(0) do |s, x|\n t << s if x == 1 && s > 0\n x\nend\nputs(t.size, t.join(' '))\n\n"}, {"source_code": "n = gets\ninput = gets\n\n#TEST INPUTS\n# input = \"1 2 3 1 2 3 4\"\n#2\n#3 4 \n\n# input = \"1 1 1 1\"\n# 4\n# 1 1 1 1 \n\n# input = \"1 2 3 4 5\"\n# 1\n# 5 \n\n# input = \"1 2 1 2 1\"\n# 3\n# 2 2 1 \n\ncounts = input.split(\" \").map(&:to_i)\n\nlast = 0\noutput = []\ncounts.each.with_index do |a_count, index|\n\tif a_count <= last\n\t\t#a new max step is added\n\t\toutput << counts[index - 1]\n\tend\n\tlast = a_count\nend\n\noutput << counts.last\n\nputs output.count\nputs output.join(\" \")"}, {"source_code": "t = gets.to_i\nstairs = gets.chomp.split(\" \").map(&:to_i)\n\nno = 1\narr = []\nprev = 0\n\nstairs.each do |x|\n if x<=prev\n no+=1\n arr.push prev\n end\n\n prev = x\nend\n\narr.push stairs[-1]\nputs no\nprint arr*\" \"\nputs\n"}, {"source_code": "gets\nn = gets.split.map { |x| x.to_i }\nk = []\nc = 0\nn[1, n.size-1].each_with_index do |x,i|\n if x == 1\n k << n[i]\n c += 1\n end\nend\nk.push(n.last)\nc += 1\nputs c\nputs(k.join ' ')\n"}], "negative_code": [{"source_code": "gets;puts (gets.split.map(&:to_i)+[1]).each_cons(2).select{|x,y|x>=y}.map{|x,y|x}*' '"}, {"source_code": "N = gets.to_i\nas = gets.split.map(&:to_i)\nans = []\nas.each do |a|\n if a == 1\n ans.push(a)\n else\n ans[-1] = a\n end\nend\nputs ans.join(' ')"}], "src_uid": "0ee86e75ff7a47a711c9eb41b34ad1a5"} {"nl": {"description": "There are $$$n$$$ block towers in a row, where tower $$$i$$$ has a height of $$$a_i$$$. You're part of a building crew, and you want to make the buildings look as nice as possible. In a single day, you can perform the following operation: Choose two indices $$$i$$$ and $$$j$$$ ($$$1 \\leq i, j \\leq n$$$; $$$i \\neq j$$$), and move a block from tower $$$i$$$ to tower $$$j$$$. This essentially decreases $$$a_i$$$ by $$$1$$$ and increases $$$a_j$$$ by $$$1$$$. You think the ugliness of the buildings is the height difference between the tallest and shortest buildings. Formally, the ugliness is defined as $$$\\max(a)-\\min(a)$$$. What's the minimum possible ugliness you can achieve, after any number of days?", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\leq t \\leq 1000$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \\leq n \\leq 100$$$) — the number of buildings. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\leq a_i \\leq 10^7$$$) — the heights of the buildings.", "output_spec": "For each test case, output a single integer — the minimum possible ugliness of the buildings.", "sample_inputs": ["3\n3\n10 10 10\n4\n3 2 1 2\n5\n1 2 3 1 5"], "sample_outputs": ["0\n0\n1"], "notes": "NoteIn the first test case, the ugliness is already $$$0$$$.In the second test case, you should do one operation, with $$$i = 1$$$ and $$$j = 3$$$. The new heights will now be $$$[2, 2, 2, 2]$$$, with an ugliness of $$$0$$$.In the third test case, you may do three operations: with $$$i = 3$$$ and $$$j = 1$$$. The new array will now be $$$[2, 2, 2, 1, 5]$$$, with $$$i = 5$$$ and $$$j = 4$$$. The new array will now be $$$[2, 2, 2, 2, 4]$$$, with $$$i = 5$$$ and $$$j = 3$$$. The new array will now be $$$[2, 2, 3, 2, 3]$$$. The resulting ugliness is $$$1$$$. It can be proven that this is the minimum possible ugliness for this test."}, "positive_code": [{"source_code": "def find_ugliness(heights, building_num)\r\n sum = heights.sum\r\n remainder = sum.modulo(building_num)\r\n\r\n # move makes no effect when difference is 0 or 1\r\n if remainder == 0\r\n return 0\r\n else\r\n return 1\r\n end\r\n\r\nend\r\n\r\ntest_case_num = gets.chomp.to_i\r\n\r\nugliness_array = []\r\n\r\ntest_case_num.times do |idx|\r\n # read input\r\n building_num = gets.chomp.to_i\r\n heights = gets.chomp.strip.split(\" \").map!(&:to_i)\r\n\r\n ugliness_array.append(find_ugliness(heights, building_num))\r\n\r\nend\r\n\r\nugliness_array.each do |ugliness_single|\r\n puts ugliness_single\r\nend\r\n"}, {"source_code": "t = gets.chomp\r\nfor i in 0..t.to_i-1\r\n n = gets.chomp.to_i\r\n arr = gets.chomp.split(\" \")\r\n arr = arr.map{|n| n.to_i}\r\n if arr.sum % n == 0\r\n puts 0\r\n else\r\n puts 1\r\n end\r\nend"}], "negative_code": [], "src_uid": "644ef17fe304b090e0cf33a84ddc546a"} {"nl": {"description": "You are given $$$n$$$ segments on a number line; each endpoint of every segment has integer coordinates. Some segments can degenerate to points. Segments can intersect with each other, be nested in each other or even coincide.The intersection of a sequence of segments is such a maximal set of points (not necesserily having integer coordinates) that each point lies within every segment from the sequence. If the resulting set isn't empty, then it always forms some continuous segment. The length of the intersection is the length of the resulting segment or $$$0$$$ in case the intersection is an empty set.For example, the intersection of segments $$$[1;5]$$$ and $$$[3;10]$$$ is $$$[3;5]$$$ (length $$$2$$$), the intersection of segments $$$[1;5]$$$ and $$$[5;7]$$$ is $$$[5;5]$$$ (length $$$0$$$) and the intersection of segments $$$[1;5]$$$ and $$$[6;6]$$$ is an empty set (length $$$0$$$).Your task is to remove exactly one segment from the given sequence in such a way that the intersection of the remaining $$$(n - 1)$$$ segments has the maximal possible length.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$2 \\le n \\le 3 \\cdot 10^5$$$) — the number of segments in the sequence. Each of the next $$$n$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$0 \\le l_i \\le r_i \\le 10^9$$$) — the description of the $$$i$$$-th segment.", "output_spec": "Print a single integer — the maximal possible length of the intersection of $$$(n - 1)$$$ remaining segments after you remove exactly one segment from the sequence.", "sample_inputs": ["4\n1 3\n2 6\n0 4\n3 3", "5\n2 6\n1 3\n0 4\n1 20\n0 4", "3\n4 5\n1 2\n9 20", "2\n3 10\n1 5"], "sample_outputs": ["1", "2", "0", "7"], "notes": "NoteIn the first example you should remove the segment $$$[3;3]$$$, the intersection will become $$$[2;3]$$$ (length $$$1$$$). Removing any other segment will result in the intersection $$$[3;3]$$$ (length $$$0$$$).In the second example you should remove the segment $$$[1;3]$$$ or segment $$$[2;6]$$$, the intersection will become $$$[2;4]$$$ (length $$$2$$$) or $$$[1;3]$$$ (length $$$2$$$), respectively. Removing any other segment will result in the intersection $$$[2;3]$$$ (length $$$1$$$).In the third example the intersection will become an empty set no matter the segment you remove.In the fourth example you will get the intersection $$$[3;10]$$$ (length $$$7$$$) if you remove the segment $$$[1;5]$$$ or the intersection $$$[1;5]$$$ (length $$$4$$$) if you remove the segment $$$[3;10]$$$."}, "positive_code": [{"source_code": "#idea_copied_From_ToErr_Vaia\n#but_not_sorted\ndef call(ind,a) \n\tr = 30**9\n\tl = -2 \n\tfor i in 0...a.size \n\t\tif(i != ind)\n\t\t\tr = [r,a[i].r].min\n\t\t\tl = [l,a[i].l].max\n\t\tend\n\tend\n\treturn r-l\nend \n\npair = Struct.new(:l,:r)\na = []\nn = STDIN.readline.to_i\nfor i in 1..n \n\tp = STDIN.readline.split(\" \")\n\ts = pair.new(p[0].to_i,p[1].to_i)\n\ta << s \nend\n# a.sort! {|z,b| z.r <=> b.r}\n\nr = 30**9 \nl = -1 \nfor i in 0...n \n\tif a[i].r < r \n\t\tr = a[i].r \n\t\tri = i \n\tend\n\tif a[i].l > l \n\t\tl = a[i].l \n\t\tli = i \n\tend\nend\n\nans = 0\nans = [ans,call(li,a)].max\nans = [ans,call(ri,a)].max \n\nprint ans "}, {"source_code": "#idea_copied_From_ToErr_Vaia\n\ndef call(ind,a) \n\tr = 30**9\n\tl = -2 \n\tfor i in 0...a.size \n\t\tif(i != ind)\n\t\t\tr = [r,a[i].r].min\n\t\t\tl = [l,a[i].l].max\n\t\tend\n\tend\n\treturn r-l\nend \n\npair = Struct.new(:l,:r)\na = []\nn = STDIN.readline.to_i\nfor i in 1..n \n\tp = STDIN.readline.split(\" \")\n\ts = pair.new(p[0].to_i,p[1].to_i)\n\ta << s \nend\na.sort! {|z,b| z.r <=> b.r}\n\nr = 30**9 \nl = -1 \nfor i in 0...n \n\tif a[i].r < r \n\t\tr = a[i].r \n\t\tri = i \n\tend\n\tif a[i].l > l \n\t\tl = a[i].l \n\t\tli = i \n\tend\nend\n\nans = 0\nans = [ans,call(li,a)].max\nans = [ans,call(ri,a)].max \n\nprint ans "}], "negative_code": [], "src_uid": "b50df0e6d91d538cd7db8dfdc7cd5266"} {"nl": {"description": "Vasya has got two number: a and b. However, Vasya finds number a too short. So he decided to repeat the operation of lengthening number a n times.One operation of lengthening a number means adding exactly one digit to the number (in the decimal notation) to the right provided that the resulting number is divisible by Vasya's number b. If it is impossible to obtain the number which is divisible by b, then the lengthening operation cannot be performed.Your task is to help Vasya and print the number he can get after applying the lengthening operation to number a n times.", "input_spec": "The first line contains three integers: a, b, n (1 ≤ a, b, n ≤ 105).", "output_spec": "In a single line print the integer without leading zeros, which Vasya can get when he applies the lengthening operations to number a n times. If no such number exists, then print number -1. If there are multiple possible answers, print any of them.", "sample_inputs": ["5 4 5", "12 11 1", "260 150 10"], "sample_outputs": ["524848", "121", "-1"], "notes": null}, "positive_code": [{"source_code": "# your code goes here\na, b, n = gets.split(' ').map { |_| _.to_i }\nfor i in 0..9\n d=a*10+i\n if d%b == 0\n print \"#{d}\"\n for j in 0..n-2\n print 0\n end\n exit\n end\nend\nputs -1"}, {"source_code": "st = gets.split\na = st[0].to_i\nb = st[1].to_i\nn = st[2].to_i\n\ni = 1\nwhile i <= n && (a != -1)\n if a % b == 0 then\n a = (a.to_s + \"0\"*(n-i+1)).to_i\n i = n + 1 \n else\n t = true\n k = 0 \n while k < 10 && t do\n q = (a.to_s + k.to_s).to_i\n if q % b == 0 then\n t = false\n a = q \n end \n k += 1 \n end \n if t then\n a = -1\n end \n end \n i += 1 \nend\n\nputs a\n"}, {"source_code": "# -*- encoding: utf-8 -*-\n\n\ndef op(a, b)\n for i in 0 .. 9 do\n c = a * 10 + i\n return c if c % b == 0\n end\n \n -1\nend\n\na, b, n = gets.chomp.split(\" \").map{|n| n.to_i}\n\nc = op(a, b)\n\nif c == -1\n puts -1\nelse\n puts c.to_s + \"0\" * (a.to_s.length + n - c.to_s.length)\nend\n"}, {"source_code": "a, b, n = gets.split.map(&:to_i)\nuntil n == 0\n a *= 10\n if a%b != 0\n if b-(a%b) > 9\n print -1\n exit\n else\n a += b-(a%b)\n end\n else\n a *= 10**(n-1)\n break\n end\n n -= 1\nend\nprint a"}, {"source_code": "#!/usr/bin/ruby1.9\n\nin_file = STDIN\n\na, b, n = in_file.readline.split.collect {|s| s.to_i }\n\n(0..9).each do |d|\n\tcandidate = 10*a+d\n\tif candidate%b == 0\n\t\tpadding = Array.new(n-1, '0').join('')\n\t\tputs '%s%s' % [candidate, padding]\n\t\texit\n\tend\nend\nputs -1\n\n"}, {"source_code": "infile = STDIN\na, b, n = infile.readline.split.map { |x| x.to_i }\n\nappend = (0..9).find { |i| (a*10+i)%b == 0 }\nif append\n puts a.to_s + append.to_s + \"0\"*(n-1)\nelse\n puts -1\nend"}, {"source_code": "def run(line)\n a, b, n = line.split(\" \").map {|x| x.to_i}\n\n prev_mod = a % b\n val = [a]\n n.times do |i|\n flag = true\n 10.times do |j|\n mod = (prev_mod * 10 + j) % b\n if mod == 0\n flag = false\n prev_mod = mod\n val << j\n break\n end\n end\n\n return \"-1\" if flag\n end\n\n val.join\nend\n\nif $0 == __FILE__\n ARGF.readlines.each do |line|\n puts run(line)\n end\nend\n"}, {"source_code": "a, b, n = gets.split.map(&:to_i)\ni = (0..9).find{ |x| (a*10+x)%b == 0}\nputs i ? \"#{a*10+i}#{'0'*(n-1)}\" : -1"}, {"source_code": "a, b, n = gets.chop!.split.map(&:to_i)\n\nret = -1\n(0..9).each do |d|\n if (a * 10 + d) % b == 0 then\n ret = d\n end\nend\n\nif ret == -1 then\n puts \"-1\"\nelse\n ret = (a * 10 + ret).to_s + \"0\" * (n - 1)\n puts ret\nend"}, {"source_code": "a=gets.chomp.split(\" \")\nf=a[0]\nb=a[1].to_i\nn=a[2].to_i\ntrig=0\n0.upto(9) do |i|\nif ((f+\"#{i}\").to_i)%b==0\nf=(f+\"#{i}\")\nn-=1\ntrig=1\nbreak\nend\nend\nif trig==1\nf=f+\"0\"*n\nputs f\nelse \nputs \"-1\"\nend"}, {"source_code": "a, b, n = gets.split.map{|i| i.to_i}\n\ni = (0..9).find {|i| (a*10+i)%b == 0 }\nans = i ? \"#{a*10+i}#{'0'*(n-1)}\" : \"-1\"\n\nputs ans\n"}, {"source_code": "a, b, n = gets.split.map(&:to_i)\n\nappend = (0..9).find { |i| (a * 10 + i) % b == 0 }\nif append\n puts (a * 10 + append).to_s + \"0\" * (n - 1)\nelse\n puts -1\nend"}, {"source_code": "a,b,n = gets.chomp.split(\" \").collect {|x| x.to_i}\n\nif b>10\n\tp = -1\n\t(0..9).each{|k|\n\t\tif (10*a+k)%b == 0\n\t\t\tp=k\n\t\tend\n\t}\n\tif p==-1\n\t\tputs -1\n\telse\n\t ss = \"0\"*(n-1)\n\t\tputs \"#{10*a+p}#{ss}\"\n\tend\nend\n\nif b<=10\n\tp = -1\n\t(0..9).each{|k|\n\t\tif (10*a+k)%b == 0\n\t\t\tp=k\n\t\tend\n\t}\n\ta=10*a+p\n\ts = (b%10).to_s*(n-1)\n\tputs \"#{a}#{s}\"\nend"}, {"source_code": "a, b, n = gets.split.collect{|i| i.to_i}\n(0...10).each do |i|\n if (a*10+i)%b==0\n print a, i, \"0\"*(n-1), \"\\n\"\n exit\n end\nend\nputs -1\n"}, {"source_code": "a,b,n=gets.split.map {|i| i.to_i}\n\na*=10\n\nunless a%b==0\n if b-a%b<10\n a+=b-a%b\n else\n puts '-1'\n exit\n end\nend\n\na*=(10**(n-1))\nputs a\n"}, {"source_code": "c=gets.chomp.scan(/\\w+/)\nt=-1\nfor i in 0..9 do\n if (c[0].to_i*10+i)%c[1].to_i==0 then t=i end\nend\nif (t==-1) then puts \"-1\" \nelse \nprint c[0],t\n2.upto(c[2].to_i) {print 0}\nputs\nend"}, {"source_code": "a,b,n = gets.split\na = a.to_i\nb = b.to_i\nn = n.to_i\n\ndef solve(a,b,n)\n\treturn \"\" << (a.to_s + \"0\"*n) if a*10%b==0\n\n\tif (a*10 + (b - a*10%b) ).to_s.start_with? a.to_s then\n\t\treturn solve(a*10+(b-a*10%b),b,n-1);\n\telse\n\t\treturn -1;\n\tend\nend\n\nputs solve(a,b,n)\n"}, {"source_code": "a,b,n = gets.split\na = a.to_i\nb = b.to_i\nn = n.to_i\n\nw = (b-a*10%b) %b\n\nif w < 10 then\n\tputs \"\" << a.to_s << w.to_s << \"0\"*(n-1)\nelse\n\tputs -1\nend\nexit\n\n"}, {"source_code": "a,b,n = gets.split.collect{|t| t.to_i}\n\n\nsolved = false\nfor i in 0..9\n\tsolved |= (10*a + i) % b == 0\n\tif solved\n\t\tprint a, i\n\t\tbreak\n\tend\nend\n\nif solved\n\t(n-1).times do |i|\n\t\tprint 0\n\tend\nelse\n\tprint -1\nend\nputs"}, {"source_code": "a, b, n = gets.split.map { |x| x.to_i }\n\nbegin\n\t10.times do |i|\n\t\tif (a * 10 + i) % b == 0\n\t\t\traise i.to_s\n\t\tend\n\tend\nrescue Exception => x\n\tputs a.to_s + x.message + '0' * (n - 1)\nelse\n\tputs -1\nend"}], "negative_code": [{"source_code": "st = gets.split\na = st[0].to_i\nb = st[1].to_i\nn = st[2].to_i\n\ni = 1\nwhile i <= n && (a != -1)\n t = true\n k = 1 \n while k < 10 && t do\n q = (a.to_s + k.to_s).to_i\n if q % b == 0 then\n t = false\n a = q \n end \n k += 1 \n end \n if t then\n a = -1\n end \n i += 1 \nend\n\nputs a"}, {"source_code": "st = gets.split\na = st[0].to_i\nb = st[1].to_i\nn = st[2].to_i\n\ni = 1\nwhile i <= n && (a != -1)\n if a % b == 0 then\n a = (a.to_s + \"0\"*(n-i+1)).to_i\n i = n + 1 \n else\n t = true\n k = 1 \n while k < 10 && t do\n q = (a.to_s + k.to_s).to_i\n if q % b == 0 then\n t = false\n a = q \n end \n k += 1 \n end \n if t then\n a = -1\n end \n end \n i += 1 \nend\n\nputs a\n"}, {"source_code": "st = gets.split\na = st[0].to_i\nb = st[1].to_i\nn = st[2].to_i\n\ni = 1\nwhile i <= n && (a != -1)\n if a % b == 0 then\n a = (a.to_s + \"0\"*(n-i)).to_i\n i = n + 1 \n else\n t = true\n k = 1 \n while k < 10 && t do\n q = (a.to_s + k.to_s).to_i\n if q % b == 0 then\n t = false\n a = q \n end \n k += 1 \n end \n if t then\n a = -1\n end \n end \n i += 1 \nend\n\nputs a\n"}, {"source_code": "a, b, n = gets.split.map(&:to_i)\nuntil n == 0\n a *= 10\n if a%b != 0\n if b-(a%b) > 9\n print -1\n exit\n else\n a += b-(a%b)\n end\n else\n a *= 10**n\n break\n end\n n -= 1\nend\nprint a"}, {"source_code": "a, b, n = gets.split.map(&:to_i)\nuntil n == 0\n a *= 10\n if b-(a%b) > 9\n print -1\n exit\n else\n a += b-(a%b)\n end\n n -= 1\nend\nprint a"}, {"source_code": "#!/usr/bin/ruby1.9\n\nin_file = STDIN\n\na, b, n = in_file.readline.split.collect {|s| s.to_i }\n\nleft = n\n\n(1..n).each do |i|\n\tbest = -1\n\t(0..9).each do |d|\n\t\tcandidate = 10*a + d\n\t\tif candidate%b == 0\n\t\t\tbest = candidate\n\t\t\tbreak\n\t\tend\n\tend\n\tif best == -1\n\t\tputs -1\n\t\texit\n\tend\n\ta = best\n\tleft -= 1\n\tif i == 5040\n\t\ts = a.to_s\n\t\ttail = s[-5040..-1]\n\t\twhile left >= 5040\n\t\t\ts += tail\n\t\t\tleft -= 5040\n\t\tend\n\t\tif left > 0\n\t\t\ts += tail[-left..-1]\n\t\tend\n\t\tputs s\n\t\texit\n\tend\nend\nputs a\n"}, {"source_code": "infile = STDIN\n# infile = File.open(\"1.txt\")\na, b, n = infile.readline.split.map { |x| x.to_i }\nappend = (0..9).find { |i| (a*10+i)%b == 0 }\nputs a.to_s + append.to_s + \"0\"*(n-1); exit if append\nputs -1"}, {"source_code": "a, b, n = gets.split.map(&:to_i)\no = a\ni = 1\n8.times do\n break if a%b == 0\n d = (a.to_s + i.to_s).to_i\n a = d if d%b == 0\n i += 1\nend\nputs a%b == 0 ? a.to_s.ljust(n+1, \"0\") : -1"}, {"source_code": "a, b, n = gets.split.map(&:to_i)\no = a\ni = 1\n8.times do\n break if a%b == 0\n d = (a.to_s + i.to_s).to_i\n a = d if d%b == 0\n i += 1\nend\nputs a%b == 0 ? a * (10**(n-1)) : -1"}, {"source_code": "a, b, n = gets.chop!.split.map(&:to_i)\n\nret = -1\n(0..9).each do |d|\n if (a * 10 + d) % b == 0 then\n ret = d\n end\nend\n\nif ret == -1 then\n puts \"-1\"\nelsif b <= 5 or b == 10 or n == 1 then\n ret = (a * 10 + ret).to_s + b.to_s * (n - 1)\n puts ret\nelse\n puts \"-1\"\nend\n"}, {"source_code": "a, b, n = gets.chop!.split.map(&:to_i)\n\nret = -1\n(0..9).each do |d|\n if (a * 10 + d) % b == 0 then\n ret = d\n end\nend\n\nif ret == -1 then\n puts \"-1\"\nelsif b <= 5 or b == 10 or n == 1 then\n b = b % 10\n ret = (a * 10 + ret).to_s + b.to_s * (n - 1)\n puts ret\nelse\n puts \"-1\"\nend\n"}, {"source_code": "a, b, n = gets.chop!.split.map(&:to_i)\n\nret = -1\n(0..9).each do |d|\n if (a * 10 + d) % b == 0 then\n ret = d\n end\nend\n\nif ret == -1 then\n puts \"-1\"\nelsif b <= 5 or n == 1 then\n ret = (a * 10 + ret).to_s + b.to_s * (n - 1)\n puts ret\nelse\n puts \"-1\"\nend"}, {"source_code": "a, b, n = gets.split.map{|i| i.to_i}\n\ni = (0...9).find {|i| (a*10+i)%b == 0 }\nputs i ? \"#{a*10+i}#{'0'*(n-1)}\" : \"-1\"\n"}, {"source_code": "a, b, n = gets.split.map(&:to_i)\n\nn.times do\n found = 1.upto(9).find do |i|\n if (a * 10 + i) % b == 0\n a = a * 10 + i\n end\n end\n if !found\n puts -1\n exit\n end\nend\n\np a\n"}, {"source_code": "a,b,n = gets.chomp.split(\" \").collect {|x| x.to_i}\nif b>10 and n>1\n puts -1\n exit\nend\n\nif b>10\n\tp = -1\n\t(0..9).each{|k|\n\t\tif (10*a+k)%b == 0\n\t\t\tp=k\n\t\tend\n\t}\n\tif p==-1\n\t\tputs -1\n\telse\n\t\tputs (10*a+p)\n\tend\nend\n\nif b<=10\n\tp = -1\n\t(0..9).each{|k|\n\t\tif (10*a+k)%b == 0\n\t\t\tp=k\n\t\tend\n\t}\n\ta=10*a+p\n\ts = (b%10).to_s*(n-1)\n\tputs \"#{a}#{s}\"\nend"}, {"source_code": "a,b,n=gets.split.map {|i| i.to_i}\n\na*=10\n\nunless a%b==0\n if a-a%b<10\n a+=a-a%b\n else\n puts '-1'\n exit\n end\nend\n\na*=(10**(n-1))\nputs a\n"}, {"source_code": "a,b,n=gets.split.map {|i| i.to_i}\n\n(1..n).each do\n a*=10\n unless a%b==0\n if b-a%b<10\n a+=b-a%b\n else\n puts '-1'\n exit!(0)\n end\n end\nend\n\nputs a\n"}, {"source_code": "a,b,n = gets.split\na = a.to_i\nb = b.to_i\nn = n.to_i\n\ndef solve(a,b,n)\n\treturn \"\" << (a.to_s + \"0\"*n) if a%b==0\n\n\tif (a*10 + (b - a*10%b) ).to_s.start_with? a.to_s then\n\t\treturn solve(a*10+(b-a*10%b),b,n-1);\n\telse\n\t\treturn -1;\n\tend\nend\n\nputs solve(a,b,n)\n"}, {"source_code": "a,b,n = gets.split\na = a.to_i\nb = b.to_i\nn = n.to_i\n\nw = (b-a*10%b) %b\n\nif w < 10 then\n\tputs a.to_s << w <<\"0\"*(n-1) \nelse\n\tputs -1\nend\n"}, {"source_code": "a,b,n = gets.split\na = a.to_i\nb = b.to_i\nn = n.to_i\n\ndef solve(a,b,n)\n\treturn \"\" << (a.to_s + \"0\"*n) if a%b==0\n\n\tif (a*10 + (b - a%10%b) ).to_s.start_with? a.to_s then\n\t\treturn solve(a*10+(b-a*10%b),b,n-1);\n\telse\n\t\treturn -1;\n\tend\nend\n\nputs solve(a,b,n)\n"}, {"source_code": "a,b,n = gets.split\nn.to_i.times do |i|\n\t0.upto(9) do |u|\n\t\ta << u.to_s\n\t\tif a.to_i%b.to_i==0 then\n\t\t\tputs a\n\t\t\texit\n\t\tend\n\tend\nend\nputs \"-1\"\n"}, {"source_code": "a,b,n = gets.split.collect{|t| t.to_i}\n\n\nsolved = false\nfor i in 0..9\n\tsolved |= (10*a + i) % b == 0\n\tif solved\n\t\tprint a, i\n\t\tbreak\n\tend\nend\n\nif solved\n\tn.times do |i|\n\t\tprint 0\n\tend\nelse\n\tprint -1\nend\nputs"}], "src_uid": "206eb67987088843ef42923b0c3939d4"} {"nl": {"description": "A string is called a k-string if it can be represented as k concatenated copies of some string. For example, the string \"aabaabaabaab\" is at the same time a 1-string, a 2-string and a 4-string, but it is not a 3-string, a 5-string, or a 6-string and so on. Obviously any string is a 1-string.You are given a string s, consisting of lowercase English letters and a positive integer k. Your task is to reorder the letters in the string s in such a way that the resulting string is a k-string.", "input_spec": "The first input line contains integer k (1 ≤ k ≤ 1000). The second line contains s, all characters in s are lowercase English letters. The string length s satisfies the inequality 1 ≤ |s| ≤ 1000, where |s| is the length of string s.", "output_spec": "Rearrange the letters in string s in such a way that the result is a k-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print \"-1\" (without quotes).", "sample_inputs": ["2\naazz", "3\nabcabcabz"], "sample_outputs": ["azaz", "-1"], "notes": null}, "positive_code": [{"source_code": "hs = Hash.new 0\nn = gets.to_i\ngets.chomp.split(//).map{|x| hs[x]+=1}\nif hs.any?{|k,v| v%n!=0}\n puts -1\nelse\n puts hs.map{|k,v| k * (v/n)}.join * n\nend"}, {"source_code": "k = gets.to_i\nstr = gets.chomp\ns = str.split(\"\")\ns = s.uniq\n\nnum = {}\nt = true\ns.each do |i|\n n = -1\n num[i] = 0\n while n = str.index(i, n+1) do\n num[i] += 1\n end\n if num[i] % k != 0 then\n t = false\n else\n num[i] = num[i]/k\n end\nend\n\nif t then\n ans = \"\"\n s.each do |i|\n ans += i*num[i]\n end\n puts ans*k\nelse\n puts \"-1\"\nend"}, {"source_code": "k, s, a = gets.to_i, gets.strip, {}\n\ns.each_char { |c|\n a[c] = 0 unless a[c]\n a[c] += 1\n}\n\nif a.all?{ |i, j| j % k == 0 }\n t = a.map { |i, j| i * (j / k) }.join\n puts t * k\nelse\n puts -1\nend"}, {"source_code": "n = gets.to_i\ns = gets.strip\na = Array.new(26, 0)\nL = s.length\nfor i in 0...L\n a[s[i].ord - 97] += 1\nend\n\nfor i in 0...26\n if a[i]%n != 0\n print -1\n exit\n end\nend\n\ns = String.new\nfor i in 0...26\n (a[i]/n).times do\n s << (i+97).chr\n end\nend\n\nprint s*n"}, {"source_code": "def k_string? k, s\n s.split('').uniq.map { |c| s.count c }.all? { |c| c%k == 0 }\nend\nk, s = gets.to_i, gets.chomp\nputs k_string?(k,s) ? s.split('').uniq.map {|c| [c, s.count(c) ] }.inject(\"\") {|acc, a| acc + (a[0]*(a[1]/k)) }*k : \"-1\""}, {"source_code": "k = gets.to_i\ns = gets.chomp!\n\ncnt = []\n\ns.each_byte { |x|\n if cnt[x - 97] == nil then\n cnt[x - 97] = 1\n else\n cnt[x - 97] += 1\n end \n}\n\nflg = true;\ncnt.each { |x|\n if x && x % k != 0 then\n flg = false\n end\n}\n\nif !flg then\n puts -1\nelse\n t = \"\"\n cnt.each_index { |ix|\n if cnt[ix] then\n t += (97 + ix).chr * (cnt[ix] / k)\n end\n }\n puts t * k\nend"}, {"source_code": "def getunit(s,k)\n return s.join(\"\") if k == 1\n u = s.uniq\n c = {}\n ans = []\n u.each do |uu|\n c[uu] = s.count(uu)\n return -1 if c[uu] % k != 0\n (c[uu]/k).times { ans.push(uu) }\n end\n return ans.join(\"\")*k\nend\n\nk = gets.to_i\ns = gets.chomp.split(\"\")\nputs getunit(s,k)\n"}, {"source_code": "hs=Hash.new(0)\nn=gets.to_i\ngets.chomp.chars{|e|\n\ths[e]+=1\n}\ns=\"\"\nbad=false\nhs.each{|k,v|\n\tif v%n!=0 then\n\t\tbad=true\n\telse\n\t\ts+=(k*(v/n))\n\tend\n}\nif bad==true then\n\tputs -1\nelse\n\tputs s*n\nend"}, {"source_code": "k=gets.to_i\ncount_hash=Hash.new(0)\ngets.strip.split(\"\").each{|letter|count_hash[letter]+=1}\nflag=0\ncount_hash.each_value { |x| flag=1 if x%k!=0 }\nif flag==0\n string=''\n count_hash.each_key{|letter| string+=letter*(count_hash[letter]/k)}\n puts string*k\nelse\n puts'-1'\nend"}, {"source_code": "require 'set'\nrequire 'rational'\nrequire 'mathn'\ninclude Math\n$stdin = File.open(\"./input.txt\", \"r\") if ENV[\"USER\"] == \"new\"\n# ====================================\n\nk = gets.to_i\nchars = gets.strip\nhash = Hash.new( 0 )\n\nchars.split(//).each do |char|\n hash[char]+=1\nend\n\nans = \"\"\nhash.to_a.each do |char, count|\n (puts -1) & exit if count%k != 0\n \n ans += char*(count/k)\nend\n\nputs ans*k"}, {"source_code": "k = gets.to_i\ns = gets.chomp\n\nh = Hash.new(0)\ns.each_char do |c|\n h[c] += 1\nend\n\nr = ''\nok = true\nh.each_pair do |a, v|\n ok = false unless v % k == 0\n \n r += a * (v / k)\nend\n\n\nputs ok ? r*k : -1"}, {"source_code": "class Fixnum\n def multiple_of?(other)\n self % other == 0\n end\nend\n\nclass String\n def can_be_k_string?(k)\n chars_count.values.all? { |value| value.multiple_of?(k) }\n end\n\n def to_k_string(k)\n chars_count.inject(\"\") { |accum, elem| accum += elem.first * (elem.last / k) } * k\n end\n\n private\n\n def chars_count\n @chars_count ||= chars.inject(Hash.new(0)) { |counter, char| counter[char] += 1; counter }\n end\nend\n\nk, string = STDIN.read.split\nk = k.to_i\n\nputs string.can_be_k_string?(k) ? string.to_k_string(k) : -1\n"}, {"source_code": "#!/usr/bin/ruby\n# coding: utf-8\n\nn = $stdin.gets.to_i\ns = $stdin.gets.chomp\n\nif s.size % n != 0\n puts -1\n exit 0\nend\n\nh = Hash.new { 0 }\ns.chars.each do |c|\n h[c] += 1\nend\nans = ''\nh.each do |k, v|\n if v % n != 0\n puts -1\n exit 0\n end\n ans += k * (v/n)\nend\nputs ans*n\n"}, {"source_code": "k = gets.to_i\n\nstring = gets.chomp.split(//)\n\nmap = Hash.new(0)\n\nstring.each { |c| map[c] += 1 }\n\nif map.all? { |i, j| j%k == 0 }\n print map.map { |i, j| i * (j/k) }.join * k\n puts\nelse\n puts \"-1\"\nend\n\n"}, {"source_code": "require 'set'\n\nif $DEBUG \n $stdin = File.new('input', 'r')\n $stdout = File.new('output', 'w') \nend\n\n$k, $s = gets.to_i, gets.chop\n$h = Hash.new\n\ndef f()\n $h.keys.map { |e| if $h[e] % $k != 0 then return false end }\n t = 0\n $h.keys.map { |e| t += ( $h[e] / $k ) }\n if t*$k != $s.size then return false end\n return true\nend\n\n0.upto($s.size-1) { |i| if $h.include?($s[i].chr) then $h[ $s[i].chr ] += 1 else $h[ $s[i].chr ] = 1 end }\n\nif !f \n puts \"-1\"\nelse\n str = \"\"\n $h.keys.map{ |e| str += e*( $h[e] / $k ) }\n str *= $k\n puts str\nend\n"}, {"source_code": "k=gets.to_i\nstr=[]\ngets.chomp.each_char{|a| str.push a}\nu=str.uniq\nif !u.collect{|a| str.count a}.select{|a| a%k !=0 }.empty?\n puts -1\nelse\n out=\"\"\n repeat={}\n u.each{|a| repeat[a]= (str.count a)/k} \n for i in 1..k\n u.each do |a|\n for i in 1..repeat[a]\n out=out+a\n end\n end\n end\n puts out\nend\n"}, {"source_code": "\nn = gets.chomp!.to_i\ns = gets.chomp!\n\nh = Hash.new(0)\n(0...s.length).each do |i|\n h[s[i]] += 1\n #puts s[i], h[s[i]]\nend\nflag = 1\nc = ''\nh.each do |i,j|\n if flag == 1 and j % n == 0 \n (j/n).times do c += i.chr end\n else\n flag = -1\n end\nend\nif flag == -1 \n puts -1\nelse \n res = ''\n n.times do res += c end\n puts res\nend"}, {"source_code": "k, s = gets.to_i, gets.strip\n\na = {}\ns.split(//).each do |c|\n a[c] = 0 unless a.include? c\n a[c] += 1 \nend\n\nif a.all?{|i, j| j % k == 0}\n t = a.map{|i, j| i * (j / k)}.join\n puts t * k\nelse\n puts -1\nend\n"}, {"source_code": "k = gets.to_i\ns = gets\n\narr = {}\ns.chomp.split('').each do |val|\n arr[val] = 0 unless arr[val]\n arr[val] += 1\nend\n\nans = ''\n\nflag = true\narr.each do |key, val|\n if (val % k) != 0\n flag = false\n break\n end\n ans += (key * (val / k))\nend\n\nif flag\n puts ans * k\nelse\n puts -1\nend\n"}, {"source_code": "# coding: utf-8\n\nn = gets.to_i\ns = gets.chomp.split(//)\ncounter = {}\nfor c in s\n counter[c] ||= 0\n counter[c] += 1\nend\n\nbroken = false\nfor (k, v) in counter\n if v % n != 0\n broken = true\n break\n end\nend\n\nif broken\n puts \"-1\"\nelse\n base = \"\"\n for (k, v) in counter\n base += k * (v/n)\n end\n puts base*n\nend\n"}, {"source_code": "t = gets.to_i\ns = gets.chomp\nh = s.split(//).reduce(Hash.new(0)) { |h, x| h[x] += 1; h}\nunless h.all? { |x, y| (y % t).zero? }\n puts -1\nelse\n puts h.to_a.map { |x, y| x * (y / t) }.join * t\nend"}, {"source_code": "k = gets.to_i\ns = gets.chomp\nh = Hash.new(0)\ns.each_char{|x| h[x] += 1}\na = []\nif h.all?{|i, j| j % k == 0}\n h.each do |i, j|\n (j / k).times{a << i}\n end\n puts a.join * k\nelse\n puts -1\nend\n"}, {"source_code": "k = gets.to_i\ns = gets.chomp\nh = Hash.new(0)\ns.each_char{|x| h[x] += 1}\nif h.all?{|i, j| j % k == 0}\n puts h.map{|i, j| i * (j / k)}.join * k\nelse\n puts -1\nend\n"}, {"source_code": "k = gets.to_i\ns = gets.strip.split //\nif s.length%k != 0\n puts -1\n exit\nend\nletters = {}\n\ns.each do |letter|\n if letters.has_key? letter\n letters[letter] += 1\n else\n letters[letter] = 1\n end\nend\n\nletters.each do |letter, value|\n if value%k != 0\n puts -1\n exit\n end\nend\n\npattern = []\nletters.sort.each do |letter,value|\n pattern << letter*(value/k)\nend\nputs pattern.join*k\n"}, {"source_code": "k = gets[0..-2].to_i\ns = gets[0..-2]\nhsh = Hash.new(0)\ns.each_char { |ch| hsh[ch] = hsh[ch] + 1 }\nok = true\nfor i in ('a'.ord)..('z'.ord)\n if hsh[i.chr] > 0 and (hsh[i.chr] % k) != 0\n ok = false\n break\n end\nend\nif !ok\n print -1\nelse\n 1.upto(k) do |i|\n (('a'.ord)..('z'.ord)).each do |x|\n print Array.new(hsh[x.chr]/k,x.chr).join('')\n end\n end\nend"}, {"source_code": "i = gets.to_i\ns = gets.chomp\n\n\ndef fnc(s,i)\n schar = s.split(//).uniq\n snum = []\n schar.map{|k| snum << s.count(k)}\n snum.each do |l|\n return puts '-1' if l % i != 0\n end\n result = ''\n schar.each_with_index do |k,index|\n (snum[index]/i).times { result += k }\n end\n res = result\n (i-1).times {res += result}\n puts res\nend\n\nfnc(s,i)"}], "negative_code": [{"source_code": "def k_string? k, s\n s.split('').uniq.map { |c| s.count c }.all? { |c| c%k == 0 }\nend\nk, s = gets.to_i, gets.chomp\np k_string?(k,s) ? s.split('').uniq.map {|c| [c, s.count(c) ] }.inject(\"\") {|acc, a| acc + (a[0]*(a[1]/k)) }*k : \"-1\""}, {"source_code": "k=gets.to_i\ncount_hash=Hash.new(0)\ngets.strip.split(\"\").each{|letter|count_hash[letter]+=1}\nflag=0\ncount_hash.each_value { |x| flag=1 if x%k!=0 }\nif flag==0\n string=''\n count_hash.each_key{|letter| string+=letter}\n puts string*k\nelse\n puts'-1'\nend"}, {"source_code": "require 'set'\nrequire 'rational'\nrequire 'mathn'\ninclude Math\n$stdin = File.open(\"./input.txt\", \"r\") if ENV[\"USER\"] == \"new\"\n# ====================================\n\nk = gets.to_i\nchars = gets.strip\nhash = Hash.new( 0 )\n\nchars.split(//).each do |char|\n hash[char]+=1\nend\n\nans = \"\"\nhash.to_a.each do |char, count|\n (puts -1) & exit if count%k != 0\n \n ans += char\nend\n\nputs ans*k"}, {"source_code": "k = gets.to_i\ns = gets.chomp\n\nh = Hash.new(0)\ns.each_char do |c|\n h[c] += 1\nend\n\nok = true\nh.values.each do |c|\n ok = false unless c % k == 0\nend\n\n\nr = s.scan(/./).uniq.sort.join\n\n\nputs ok ? r*k : -1"}, {"source_code": "k = gets.to_i\ns = gets.chomp\nr = s.gsub(/(.)(?=.*\\1)/, '')\n\nputs r.length % k == 0 ? r*k : -1"}, {"source_code": "k = gets.to_i\ns = gets.chomp\n\na = s.scan(/./)\nr = a.uniq * k\n\nputs r.sort == a.sort ? r.join : -1"}, {"source_code": "#!/usr/bin/ruby\n# coding: utf-8\n\nn = $stdin.gets.to_i\ns = $stdin.gets.chomp\n\nif s.size % n != 0\n puts -1\n exit 0\nend\n\nh = Hash.new { 0 }\ns.chars.each do |c|\n h[c] += 1\nend\nans = ''\nh.each do |k, v|\n if v % n != 0\n puts -1\n exit 0\n end\n ans += k\nend\nputs ans*n\n"}, {"source_code": "k = gets.to_i\nstr = gets.chomp.split(//)\nn = str.length/k\n\nx = str.permutation.map(&:join).uniq\n\nx.each do |string|\n if string == string[0...k]*n\n puts string\n exit\n end\nend\nputs '-1'\n"}, {"source_code": "k = gets.to_i\nstr = gets.chomp.split(//)\nary = []\n(0...str.length-k).map do |i|\n ary << str[i...i+k].join\nend\nlen = str.length/k\nflag = nil\nary.uniq.each do |substr|\n if str.join =~ /(?0\n raise \"we're screwed #{r}\"\n end\n #return 0 if r==0\n res = with_memo(FMemo, k*101+r) do\n (st..[r,S[k]].min).reduce(0) do |sum, x|\n ps = comb(S[k], x)\n #puts \"pre ps=#{ps}\"\n if r-x>0\n ps *= f(k+1, r-x)\n end\n #puts \"k=#{k} r=#{r} x=#{x} ps=#{ps}\"\n sum + ps\n end\n end\n #puts \"k=#{k} r=#{r} res=#{res}\"\n res\nend\n\ntot = f(0,n)\n#puts tot\nFMemo.clear\ngood = f(0,n,1)\n#puts good\nif tot==0\n puts \"-1\"\nelse\n puts 1_000_000_000*good/tot*1e-9\nend\n"}, {"source_code": "#!/usr/bin/ruby\n\nn, m, h = gets.split.map(&:to_i)\ns = gets.split.map(&:to_i)\n\nsum = s.inject(&:+)-1\nsubsum = sum - s[h-1] + 1\n\nif sum < n-1\n\tp -1\nelse\n\tprob = 1.0\n\t(n-1).times { |i| prob *= (subsum-i).to_f/(sum-i) }\n\tp 1-prob\nend\n"}, {"source_code": "\narg = STDIN.gets.split(\" \").map {|i| i.to_i} # players, departments, Herr's department\nstudents = STDIN.gets.split(\" \").map {|i| i.to_i}\nothers = 0\n(0..arg[1]-1).each do |i|\n others += students[i] unless i==(arg[2]-1)\nend\ntotal = others + students[arg[2]-1]-1\n\n\n\n\nif total+1 < arg[0]\n puts -1.0\n exit\nend\nif students[arg[2]-1] == 1\n puts 0.0\n exit\nend\n\n\nif others < arg[0]-1\n puts 1.0\n exit\nend\n# 1-C(others,players)/C(total,players)\nprob = 1.0\n(0..arg[0]-2).each do |i|\n prob *= (others-i)\n prob /= (total-i)\nend\nputs 1.0 - prob\n"}], "negative_code": [{"source_code": "n,m,h = readline.split(' ').map &:to_i\ns = readline.split(' ').map &:to_i\ns[h-1]-=1\nS=[s[h-1], s.reduce(&:+)-s[h-1]]\nM=1+(S[1]==0?0:1)\n#S[0],S[h-1]=S[h-1],S[0]\n#S[0]-=1\nn-=1\n\nCombMemo = {}\nFMemo = {}\n\ndef with_memo(memo, ix, &block)\n return memo[ix] if memo[ix]\n res = block.call\n memo[ix] = res\n res\nend\n\ndef comb(n,k)\n return 1 if k==0\n with_memo(CombMemo, n*101+k) do\n (n-k+1)*comb(n,k-1)/k\n end\nend\n\ndef f(k, r, st=0)\n if k==M\n return 1 if r==0\n return 0 if r>0\n raise \"we're screwed #{r}\"\n end\n #return 0 if r==0\n res = with_memo(FMemo, k*101+r) do\n (st..[r,S[k]].min).reduce(0) do |sum, x|\n ps = comb(S[k], x)\n #puts \"pre ps=#{ps}\"\n if r-x>0\n ps *= f(k+1, r-x)\n end\n #puts \"k=#{k} r=#{r} x=#{x} ps=#{ps}\"\n sum + ps\n end\n end\n #puts \"k=#{k} r=#{r} res=#{res}\"\n res\nend\n\ntot = f(0,n)\n#puts tot\nFMemo.clear\ngood = f(0,n,1)\n#puts good\nif tot==0\n puts \"-1\"\nelse\n puts 1.0*good/tot\nend\n"}, {"source_code": "\narg = STDIN.gets.split(\" \").map {|i| i.to_i} # players, departments, Herr's department\nstudents = STDIN.gets.split(\" \").map {|i| i.to_i}\nif arg[0] == 65 and arg[1] == 173 and arg[2] == 136\n i=0\n begin\n i += 1\n puts students[(i-1)*10..[i*10,students.length].min-1]\n end while i*10 < students.length\nend\nothers = 0\n(0..arg[1]-1).each do |i|\n others += students[i] unless i==(arg[2]-1)\nend\ntotal = others + students[arg[2]-1]-1\n\n\n\n\nif total+1 < arg[0]\n puts -1.0\n exit\nend\nif students[arg[2]-1] == 1\n puts 0.0\n exit\nend\n\n\nif others < arg[0]-1\n puts 1.0\n exit\nend\n# 1-C(others,players)/C(total,players)\nprob = 1.0\n(0..arg[0]-2).each do |i|\n prob *= (others-i)\n prob /= (total-i)\nend\nputs 1.0 - prob\n"}, {"source_code": "\narg = STDIN.gets.split(\" \").map {|i| i.to_i} # players, departments, Herr's department\nstudents = STDIN.gets.split(\" \").map {|i| i.to_i}\nif arg[0] == 65 and arg[1] == 173 and arg[2] == 136\n i=15\n begin\n i += 1\n p students[(i-1)*10..[i*10,students.length].min-1]\n end while i*10 < students.length\nend\nothers = 0\n(0..arg[1]-1).each do |i|\n others += students[i] unless i==(arg[2]-1)\nend\ntotal = others + students[arg[2]-1]-1\n\n\n\n\nif total+1 < arg[0]\n puts -1.0\n exit\nend\nif students[arg[2]-1] == 1\n puts 0.0\n exit\nend\n\n\nif others < arg[0]-1\n puts 1.0\n exit\nend\n# 1-C(others,players)/C(total,players)\nprob = 1.0\n(0..arg[0]-2).each do |i|\n prob *= (others-i)\n prob /= (total-i)\nend\nputs 1.0 - prob\n"}, {"source_code": "\narg = STDIN.gets.split(\" \").map {|i| i.to_i} # players, departments, Herr's department\nstudents = STDIN.gets.split(\" \").map {|i| i.to_i}\nif arg[0] == 65 and arg[1] == 173 and arg[2] == 136\n i=10\n begin\n i += 1\n p students[(i-1)*10..[i*10,students.length].min-1]\n end while i*10 < students.length\nend\nothers = 0\n(0..arg[1]-1).each do |i|\n others += students[i] unless i==(arg[2]-1)\nend\ntotal = others + students[arg[2]-1]-1\n\n\n\n\nif total+1 < arg[0]\n puts -1.0\n exit\nend\nif students[arg[2]-1] == 1\n puts 0.0\n exit\nend\n\n\nif others < arg[0]-1\n puts 1.0\n exit\nend\n# 1-C(others,players)/C(total,players)\nprob = 1.0\n(0..arg[0]-2).each do |i|\n prob *= (others-i)\n prob /= (total-i)\nend\nputs 1.0 - prob\n"}, {"source_code": "\narg = STDIN.gets.split(\" \").map {|i| i.to_i} # players, departments, Herr's department\nstudents = STDIN.gets.split(\" \").map {|i| i.to_i}\nif arg[0] == 65 and arg[1] == 173 and arg[2] == 136\n i=0\n begin\n i += 1\n p students[(i-1)*10..[i*10,students.length].min-1]\n end while i*10 < students.length\nend\nothers = 0\n(0..arg[1]-1).each do |i|\n others += students[i] unless i==(arg[2]-1)\nend\ntotal = others + students[arg[2]-1]-1\n\n\n\n\nif total+1 < arg[0]\n puts -1.0\n exit\nend\nif students[arg[2]-1] == 1\n puts 0.0\n exit\nend\n\n\nif others < arg[0]-1\n puts 1.0\n exit\nend\n# 1-C(others,players)/C(total,players)\nprob = 1.0\n(0..arg[0]-2).each do |i|\n prob *= (others-i)\n prob /= (total-i)\nend\nputs 1.0 - prob\n"}, {"source_code": "\narg = STDIN.gets.split(\" \").map {|i| i.to_i} # players, departments, Herr's department\nstudents = STDIN.gets.split(\" \").map {|i| i.to_i}\nif arg[0] == 65 and arg[1] == 173 and arg[2] == 136\n i=5\n begin\n i += 1\n p students[(i-1)*10..[i*10,students.length].min-1]\n end while i*10 < students.length\nend\nothers = 0\n(0..arg[1]-1).each do |i|\n others += students[i] unless i==(arg[2]-1)\nend\ntotal = others + students[arg[2]-1]-1\n\n\n\n\nif total+1 < arg[0]\n puts -1.0\n exit\nend\nif students[arg[2]-1] == 1\n puts 0.0\n exit\nend\n\n\nif others < arg[0]-1\n puts 1.0\n exit\nend\n# 1-C(others,players)/C(total,players)\nprob = 1.0\n(0..arg[0]-2).each do |i|\n prob *= (others-i)\n prob /= (total-i)\nend\nputs 1.0 - prob\n"}, {"source_code": "\narg = STDIN.gets.split(\" \").map {|i| i.to_i} # players, departments, Herr's department\nstudents = STDIN.gets.split(\" \").map {|i| i.to_i}\nif students[arg[2]-1] == 1\n puts -1.0\n exit\nend\n\nothers = 0\n(0..arg[1]-1).each do |i|\n others += students[i] unless i==(arg[2]-1)\nend\nif others < arg[0]-1\n puts 1.0\nend\ntotal = others + students[arg[2]-1]-1\n# 1-C(others,players)/C(total,players)\nprob = 1.0\n(0..arg[0]-2).each do |i|\n prob *= (others-i)\n prob /= (total-i)\nend\nputs 1.0 - prob\n"}, {"source_code": "\narg = STDIN.gets.split(\" \").map {|i| i.to_i} # players, departments, Herr's department\nstudents = STDIN.gets.split(\" \").map {|i| i.to_i}\nif arg[0] == 65 and arg[1] == 173 and arg[2] == 136\n puts students\nend\nothers = 0\n(0..arg[1]-1).each do |i|\n others += students[i] unless i==(arg[2]-1)\nend\ntotal = others + students[arg[2]-1]-1\n\n\n\n\nif total+1 < arg[0]\n puts -1.0\n exit\nend\nif students[arg[2]-1] == 1\n puts 0.0\n exit\nend\n\n\nif others < arg[0]-1\n puts 1.0\n exit\nend\n# 1-C(others,players)/C(total,players)\nprob = 1.0\n(0..arg[0]-2).each do |i|\n prob *= (others-i)\n prob /= (total-i)\nend\nputs 1.0 - prob\n"}, {"source_code": "\narg = STDIN.gets.split(\" \").map {|i| i.to_i} # players, departments, Herr's department\nstudents = STDIN.gets.split(\" \").map {|i| i.to_i}\nif arg[0] == 65 and arg[1] == 173 and arg[2] == 136\n i=4\n begin\n i += 1\n p students[(i-1)*10..[i*10,students.length].min-1]\n end while i*10 < students.length\nend\nothers = 0\n(0..arg[1]-1).each do |i|\n others += students[i] unless i==(arg[2]-1)\nend\ntotal = others + students[arg[2]-1]-1\n\n\n\n\nif total+1 < arg[0]\n puts -1.0\n exit\nend\nif students[arg[2]-1] == 1\n puts 0.0\n exit\nend\n\n\nif others < arg[0]-1\n puts 1.0\n exit\nend\n# 1-C(others,players)/C(total,players)\nprob = 1.0\n(0..arg[0]-2).each do |i|\n prob *= (others-i)\n prob /= (total-i)\nend\nputs 1.0 - prob\n"}], "src_uid": "ffafd385ec79aa28b8d30224baf6bcfe"} {"nl": {"description": "Leha somehow found an array consisting of n integers. Looking at it, he came up with a task. Two players play the game on the array. Players move one by one. The first player can choose for his move a subsegment of non-zero length with an odd sum of numbers and remove it from the array, after that the remaining parts are glued together into one array and the game continues. The second player can choose a subsegment of non-zero length with an even sum and remove it. Loses the one who can not make a move. Who will win if both play optimally?", "input_spec": "First line of input data contains single integer n (1 ≤ n ≤ 106) — length of the array. Next line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109).", "output_spec": "Output answer in single line. \"First\", if first player wins, and \"Second\" otherwise (without quotes).", "sample_inputs": ["4\n1 3 2 3", "2\n2 2"], "sample_outputs": ["First", "Second"], "notes": "NoteIn first sample first player remove whole array in one move and win.In second sample first player can't make a move and lose."}, "positive_code": [{"source_code": "n = gets.to_i\na = []\na = gets.split.map &:to_i\ncnt = 0\n(0...n).each do |i|\n if (a[i] % 2 == 1)\n cnt += 1\n end\nend\n\nif (cnt == 0)\n puts \"Second\"\nelse\n puts \"First\"\nend"}, {"source_code": "gets\nfor i in gets.split.map(&:to_i)\nif i%2==1\n puts 'First'\n exit\nend\nend\nputs 'Second'"}, {"source_code": "n = gets.strip.to_i\na = gets.strip.split.map(&:to_i)\nsum_a = count_odd = count_even = 0\nn.times do |i|\n curr = a[i]\n count_odd += 1 if curr.odd?\n count_even += 1 if curr.even?\n sum_a += curr\nend\nif sum_a.odd?\n puts \"First\"\nelse\n if count_odd >= 2 && count_odd.even?\n puts \"First\"\n else\n puts \"Second\"\n end\nend\n"}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\n\nx=true\na.each{|e| x&=e%2==0}\nif x\n puts 'Second'\nelse\n puts 'First'\nend\n\n"}], "negative_code": [{"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\n\nx=true\na.each{|e| x&=e%2==0}\nif x\n puts 'Second'\n exit 0\nend\n\ns=a.inject(:+)\nif s%2==1\n puts 'First'\nelse\n puts 'Second'\nend\n"}], "src_uid": "808611f86c70659a1d5b8fc67875de31"} {"nl": {"description": "Anton likes to play chess, and so does his friend Danik.Once they have played n games in a row. For each game it's known who was the winner — Anton or Danik. None of the games ended with a tie.Now Anton wonders, who won more games, he or Danik? Help him determine this.", "input_spec": "The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of games played. The second line contains a string s, consisting of n uppercase English letters 'A' and 'D' — the outcome of each of the games. The i-th character of the string is equal to 'A' if the Anton won the i-th game and 'D' if Danik won the i-th game.", "output_spec": "If Anton won more games than Danik, print \"Anton\" (without quotes) in the only line of the output. If Danik won more games than Anton, print \"Danik\" (without quotes) in the only line of the output. If Anton and Danik won the same number of games, print \"Friendship\" (without quotes).", "sample_inputs": ["6\nADAAAA", "7\nDDDAADA", "6\nDADADA"], "sample_outputs": ["Anton", "Danik", "Friendship"], "notes": "NoteIn the first sample, Anton won 6 games, while Danik — only 1. Hence, the answer is \"Anton\".In the second sample, Anton won 3 games and Danik won 4 games, so the answer is \"Danik\".In the third sample, both Anton and Danik won 3 games and the answer is \"Friendship\"."}, "positive_code": [{"source_code": "n = gets.chomp\nword = gets.chomp\n\nanton = word.count(\"A\")\ndanik = word.count(\"D\")\n\nif anton > danik\n puts \"Anton\"\nelsif anton < danik\n puts \"Danik\"\nelse\n puts \"Friendship\"\nend\n"}, {"source_code": "\n# Anton and Danik\n# https://codeforces.com/problemset/problem/734/A\nclass ChessMatch\n def initialize(matches, result)\n @matches = matches\n @result = result\n end\n\n def winner\n wins_anton = @result.count('A')\n wins_danik = @matches - wins_anton\n\n if wins_anton > wins_danik\n 'Anton'\n elsif wins_anton < wins_danik\n 'Danik'\n else\n 'Friendship'\n end\n end\nend\n\n# Test case\n# 6\n# ADAAAA\n# Anton\nmatches = gets.to_i\nret = gets\n\nmatch = ChessMatch.new(matches, ret)\nputs match.winner\n"}, {"source_code": "s=$<.read;puts [:Friendship,:Anton,:Danik][s.count(?A)<=>s.count(?D)]"}, {"source_code": "# input from user\ndef Get_Input\n number_game = gets.chomp\n str = gets.chomp\n return str\nend\n\ndef check_char(str)\n anton=0\n danik=0\n #get count of char\n str.split('').each do |char|\n if char == 'A'\n anton +=1\n elsif char == 'D'\n danik +=1\n end\n end\n# to check who's win\n if anton > danik\n puts 'Anton'\n elsif anton < danik\n puts 'Danik'\n elsif anton == danik\n puts 'Friendship'\n end\nend\n\nstr = Get_Input()\ncheck_char(str)\n"}, {"source_code": "n = gets.chomp.to_i\nchars = gets.chomp.chars\n\nans = chars.count('A') - chars.count('D')\nif ans == 0\n puts \"Friendship\"\nelsif ans > 0\n puts \"Anton\"\nelse\n puts \"Danik\"\nend\n"}, {"source_code": "gets\na = gets().chomp.split(//)\ncnt = 0\na.map{|x| x == 'A'? cnt += 1 : cnt -= 1}\nputs cnt > 0 ? \"Anton\" : cnt == 0 ? \"Friendship\" : \"Danik\"\n"}, {"source_code": "#!/usr/bin/env ruby \ngets\na = gets().chomp.split(//)\ncnt = 0\na.map{|x| x == 'A'? cnt += 1 : cnt -= 1}\nputs cnt > 0 ? \"Anton\" : cnt == 0 ? \"Friendship\" : \"Danik\""}, {"source_code": "#!/usr/bin/env ruby \ngets \na = gets().strip.split(//) \ncnt = a.count(\"A\") - a.count(\"D\") \nputs cnt > 0 ? \"Anton\" : cnt == 0 ? \"Friendship\" : \"Danik\" "}, {"source_code": "#!/usr/bin/env ruby \ngets \na = gets().chomp.split(//) \ncnt = a.count(\"A\") - a.count(\"D\") \nputs cnt > 0 ? \"Anton\" : cnt == 0 ? \"Friendship\" : \"Danik\" "}, {"source_code": "s=$<.read;puts [:Friendship,:Anton,:Danik][s.count(?A)<=>s.count(?D)]"}, {"source_code": "x = gets.chomp.to_i\ns = gets\na = 0\nd = 0\nwhile x >= 0 \n if s[x]== 'A'\n a = a + 1\n elsif s[x]== 'D'\n d = d + 1\n end\n x = x - 1\nend\n\nif a > d \n puts \"Anton\"\nelsif a < d \n puts \"Danik\"\nelse\n puts \"Friendship\"\nend "}, {"source_code": "num = gets.chomp.to_i\ntabs = gets.chomp\nA = tabs.count(\"A\")\nD = tabs.count(\"D\")\nif A > D \n\tputs \"Anton\"\nelsif D > A\n\tputs \"Danik\"\nelse puts \"Friendship\"\nend\n"}, {"source_code": "total_play = gets.strip.to_i\nplay_string = gets.strip\nplay = play_string.split('')\n\nanton_counter = 0\ndanik_counter = 0\n\n0.upto(total_play - 1).each do |index|\n if play[index].eql?('A')\n anton_counter += 1\n else\n danik_counter += 1\n end\nend\n\nif anton_counter > danik_counter\n puts 'Anton'\nelsif danik_counter > anton_counter\n puts 'Danik'\nelse\n puts 'Friendship'\nend\n"}, {"source_code": "n = gets.to_i\narr = gets.chomp.chars.to_a\nd = a = 0\narr.each{|c|\n if c == 'D'\n d+=1\n else\n a+=1\n end\n}\nif a > d\n puts 'Anton'\nelsif d > a\n puts 'Danik'\nelse\n puts 'Friendship'\nend"}, {"source_code": "n = gets.to_i\nstr = gets.strip\nac = str.count(\"A\")\ndc = str.count(\"D\")\n\nputs \"Anton\" if ac > dc\nputs \"Danik\" if ac < dc\nputs \"Friendship\" if ac == dc\n"}, {"source_code": "class A\n\n def run\n n = $stdin.gets.to_i\n s = $stdin.gets\n a = d = 0\n n.times.each do |i|\n c = s[i] \n a += 1 if c == 'A'\n d += 1 if c == 'D'\n end\n puts 'Anton' if a > d\n puts 'Danik' if a < d\n puts 'Friendship' if a == d\n end\n\nend\n\nA.new.run"}, {"source_code": "# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n############################################################\ndef gs() gets.strip end\ndef gi() gets.to_i end\ndef gss() gs.split end\ndef gis() gss.map(&:to_i) end\ndef nmapf(n,f) n.times.map{ __send__ f } end\ndef ngs(n) nmapf n,:gs end\ndef ngi(n) nmapf n,:gi end\ndef ngss(n) nmapf n,:gss end\ndef ngis(n) nmapf n,:gis end\ndef arr2d(h,w,v=0) h.times.map{[v] * w} end\ndef for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end\ndef nsum(n) n * (n + 1) / 2 end\ndef vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end\n############################################################\n\nn = gi\ns = gs\n\nanton_won = s.count('A')\ndanik_won = n - anton_won\n\ncase\nwhen anton_won > danik_won\n puts :Anton\nwhen anton_won < danik_won\n puts :Danik\nelse\n puts :Friendship\nend"}, {"source_code": "# cook your code here\nn = gets.to_i \nresult = gets \nputs \"Anton\" if result.count(\"A\") > result.count(\"D\")\nputs \"Friendship\" if result.count(\"A\") == result.count(\"D\")\nputs \"Danik\" if result.count(\"D\") > result.count(\"A\")"}, {"source_code": "gets.chomp\nrounds = gets.chomp\n\nanton = rounds.count 'A'\ndanik = rounds.count 'D'\n\nputs \"Anton\" if anton > danik\nputs \"Danik\" if danik > anton \nputs \"Friendship\" if anton == danik"}, {"source_code": "n = gets.to_i\ngames = gets.chomp\nac = games.count('A')\ndc = n - ac\nif ac == dc\n puts 'Friendship'\nelsif ac > dc\n puts 'Anton'\nelse\n puts 'Danik'\nend"}, {"source_code": "s=$<.read;puts [:Friendship,:Anton,:Danik][s.count(?A)<=>s.count(?D)]\n"}, {"source_code": "n=gets.to_i\nv=gets.chars.count{|e|\n\te==\"A\"\n}\nans=\"\"\n\nif n%2==0 then\n\tif n/2 s.count('D')) + 1]\n"}, {"source_code": "n = gets.chomp.to_i\narr = gets.chomp.split('')\n# p arr\nco = {'A'=>0,'D'=>0}\n\narr.each do |el|\n co[el] += 1\nend\n\n# p co\nif co['A'] > co['D']\n puts \"Anton\"\nelsif co['A'] < co['D']\n puts \"Danik\"\nelse\n puts \"Friendship\"\nend\n"}, {"source_code": "gets\ns = gets.chomp\nn = 0\ns.each_char do |i| \n\tif i == 'A'\n\t\tn += 1\n\telse\n\t\tn -= 1\n\tend\nend\nif n == 0\n\tputs 'Friendship'\nelsif n > 0\n\tputs 'Anton'\nelse\n\tputs 'Danik'\nend\n\t\n"}, {"source_code": "gets\ns=gets.chomp\na=s.count ?A\nd=s.count ?D\nputs ad ? \"Anton\" : \"Friendship\"\n"}, {"source_code": "a= gets.chomp.to_i\nstr = gets.chomp \nstr = str.split('')\nif str.count(\"A\") > str.count(\"D\")\n puts \"Anton\"\nelsif str.count(\"D\") > str.count(\"A\")\n puts \"Danik\"\nelse\n puts \"Friendship\"\nend"}, {"source_code": "n = gets.to_i\ns = gets\na = s.count('A')\nb = s.count('D')\nputs a>b ? \"Anton\" : b>a ? \"Danik\" : \"Friendship\"\n"}, {"source_code": "numResults = gets.chomp.to_i\nresults = gets.chomp\n\nanton = 0\ndanik = 0\n\nresults.each_byte do |b|\n\tif b.chr == 'A'\n\t\tanton = anton + 1\n\telsif b.chr == 'D'\n\t\tdanik = danik + 1\n\tend\nend\n\nif anton == danik\n\tputs \"Friendship\"\nelsif anton > danik\n\tputs \"Anton\"\nelse\n\tputs \"Danik\"\nend\n"}, {"source_code": "games = gets.chomp\nw = gets.chomp\nwins = w.split(\"\")\n\ncount = 0\nwins.each do |win|\n\tif win == \"A\"\n\t\tcount += 1\n\telse\n\t\tcount -= 1\n\tend\nend\n\nif count > 0\n\tprint \"Anton\"\nelsif count < 0\n\tprint \"Danik\"\nelse\n\tprint \"Friendship\"\nend"}, {"source_code": "#puts \"Enter the number of game played\\n\"\nn = gets.to_i\ns = gets.to_s\na = d = 0\nfor i in 0...n do\n if s[i] == 'A'\n a += 1\n else\n d += 1\n end\nend\nif a > d\n puts \"Anton\\n\"\nelsif d > a\n puts \"Danik\\n\"\nelse\n puts \"Friendship\\n\"\nend"}, {"source_code": "int_value = gets.chomp\nstring_input = gets.chomp\nstring = string_input.split('')\nanton = 0\ndanik = 0\nstring.each do |char|\n if char == 'A'\n anton += 1\n else\n danik += 1\n end\nend\nif anton == danik\n puts 'Friendship'\nelsif anton > danik\n puts 'Anton'\nelse\n puts 'Danik'\nend"}, {"source_code": "gets\ns = gets\nputs s.count(\"A\") > s.count(\"D\") ? \"Anton\" : (s.count(\"A\") < s.count(\"D\") ? \"Danik\" : \"Friendship\")\n"}, {"source_code": "s=$<.read;puts [:Friendship,:Anton,:Danik][s.count(?A)<=>s.count(?D)]\n"}, {"source_code": "n = gets.to_i\ns = gets\na = s.count('A')\nb = s.count('D')\nputs a>b ? \"Anton\" : b>a ? \"Danik\" : \"Friendship\"\n"}, {"source_code": "#!/usr/bin/env ruby\n\ngets\ns = gets.strip\na = s.count('A')\nb = s.count('D')\n\nif a > b\n puts 'Anton'\nelsif a < b\n puts 'Danik'\nelse\n puts 'Friendship'\nend\n"}, {"source_code": "gets\ns = gets\nputs (a = s.count(\"A\")) > (d = s.count(\"D\")) ? \"Anton\" : a == d ? \"Friendship\" : \"Danik\"\n"}, {"source_code": "gets\ns = gets\nputs s.count(\"A\") > s.count(\"D\") ? \"Anton\" : s.count(\"A\") == s.count(\"D\") ? \"Friendship\" : \"Danik\"\n"}, {"source_code": "n = gets\nn = gets\na = n.count 'A'\nd= n.count 'D'\nif a > d\n puts \"Anton\"\nelsif a < d\n puts \"Danik\"\nelse\n puts \"Friendship\"\nend"}, {"source_code": "n = gets\ndata = gets.chomp\n\nan = data.chars.count { |c| c == 'A' }\ndn = data.chars.count { |c| c == 'D' }\n\nif an > dn\n puts 'Anton'\nelsif an < dn\n puts 'Danik'\nelse\n puts 'Friendship'\nend\n"}, {"source_code": "s=$<.read;puts [:Friendship,:Anton,:Danik][s.count(?A)<=>s.count(?D)]\n"}, {"source_code": "n = gets.to_i\ns = gets.strip\nr = s.count('A') * 2\nif r > n\n printf(\"Anton\\n\")\nelsif r == n\n printf(\"Friendship\\n\")\nelse\n printf(\"Danik\\n\")\nend\n"}, {"source_code": "n = gets.chomp().to_i\ns = gets.chomp().split('')\nanton = s.select{|c| c if c=='A'}.length\ndanik = s.select{|c| c if c=='D'}.length\nputs ( anton == danik ) ? \"Friendship\" : (anton > danik) ? \"Anton\" : \"Danik\""}, {"source_code": "require 'set'\nparse_int = lambda{ gets.split.map{|el| el.to_i} }\nsign = lambda {|el| (el>0?1:0)-(el<0?1:0)}\n\ngets\nn = gets\na,d = n.count('A'), n.count('D')\n\nputs 'Anton' if a>d\nputs 'Danik' if d>a\nputs 'Friendship' if d==a\n\n\n\n\n\n\n\n\n\n\n"}], "negative_code": [{"source_code": "gets\na = gets().chomp.split(//)\nprint a.inspect\ncnt = 0\na.map{|x| x == 'A'? cnt += 1 : cnt -= 1}\nputs cnt > 0 ? \"Anton\" : cnt == 0 ? \"Friendship\" : \"Danik\"\n"}, {"source_code": "gets\na = gets().split(//)\ncnt = 0\na.map{|x| x == 'A'? cnt += 1 : cnt -= 1} \nputs cnt > 0 ? \"Anton\" : (cnt == 0 ? \"Friendship\" : \"Danik\")"}, {"source_code": "num = gets.chomp.to_i\ntabs = gets.chomp\nA_count = 0\nD_count = 0\nfor i in 0...num\n\tputs \"check\"\n\tif tabs[i] == \"A\"\n\t\tA_count += 1\n\telse\n\t\tD_count += 1\n\tend\nend\nif A_count > D_count \n\tputs \"Anton\"\nelsif D_count > A_count\n\tputs \"Danik\"\nelse puts \"Friendship\"\nend\n"}, {"source_code": "gets\ns = gets.chomp\nn = 0\ns.each_char do |i| \n\tif i == 'A'\n\t\tn += 1\n\telse\n\t\tn -= 1\n\tend\nend\nif n == 0\n\tputs 'Friendship'\nelsif n > 1\n\tputs 'Anton'\nelse\n\tputs 'Danik'\nend"}, {"source_code": "numResults = gets.to_i\nresults = gets\n\nanton = 0\ndanik = 0\n\nputs results.size\n\nresults.each_byte do |b|\n\tif b.chr == 'A'\n\t\tanton = anton + 1\n\telsif b.chr == 'D'\n\t\tdanik = danik + 1\n\tend\nend\n\nif anton == danik\n\tputs \"Friendship\"\nelsif anton > danik\n\tputs \"Anton\"\nelse\n\tputs \"Danik\"\nend\n"}, {"source_code": "input = $stdin.readlines\n\nnumResults = input[0].to_i\nresults = input[1]\n\nanton = 0\ndanik = 0\n\nputs results.size\n\nresults.each_byte do |b|\n\tif b.chr == 'A'\n\t\tanton = anton + 1\n\telsif b.chr == 'D'\n\t\tdanik = danik + 1\n\tend\nend\n\nif anton == danik\n\tputs \"Friendship\"\nelsif anton > danik\n\tputs \"Anton\"\nelse\n\tputs \"Danik\"\nend\n"}, {"source_code": "int_value = gets\nstring_input = gets\nstring = string_input.split('')\nanton = 0\ndanik = 0\nstring.each do |char|\n if char == 'A'\n anton += 1\n else\n danik += 1\n end\nend\nif anton == danik\n puts 'Friendship'\nelsif anton > danik\n puts 'Anton'\nelse\n puts 'Danik'\nend"}, {"source_code": "s = gets\nputs s.count(\"A\") > s.count(\"D\") ? \"Anton\" : (s.count(\"A\") < s.count(\"D\") ? \"Danik\" : \"Friendship\")\n"}, {"source_code": "#gets\n#s = gets\n#puts (a = s.count(\"A\")) > (d = s.count(\"D\")) ? \"Anton\" : a == d ? \"Friendship\" : \"Danik\"\n\ns=$<.read\np [:Friendship,:Anton,:Danik][s.count(?A)<=>s.count(?D)]\n"}, {"source_code": "s=$<.read;p [:Friendship,:Anton,:Danik][s.count(?A)<=>s.count(?D)]"}, {"source_code": "require 'set'\nparse_int = lambda{ gets.split.map{|el| el.to_i} }\nsign = lambda {|el| (el>0?1:0)-(el<0?1:0)}\n\ngets\nn = gets\na,d = n.count('A'), n.count('D')\n\nputs 'Antin' if a>d\nputs 'Danik' if d>a\nputs 'Friendship' if d==a\n\n\n\n\n\n\n\n\n\n\n"}], "src_uid": "0de32a7ccb08538a8d88239245cef50b"} {"nl": {"description": "You are given a tree that consists of $$$n$$$ nodes. You should label each of its $$$n-1$$$ edges with an integer in such way that satisfies the following conditions: each integer must be greater than $$$0$$$; the product of all $$$n-1$$$ numbers should be equal to $$$k$$$; the number of $$$1$$$-s among all $$$n-1$$$ integers must be minimum possible. Let's define $$$f(u,v)$$$ as the sum of the numbers on the simple path from node $$$u$$$ to node $$$v$$$. Also, let $$$\\sum\\limits_{i=1}^{n-1} \\sum\\limits_{j=i+1}^n f(i,j)$$$ be a distribution index of the tree.Find the maximum possible distribution index you can get. Since answer can be too large, print it modulo $$$10^9 + 7$$$.In this problem, since the number $$$k$$$ can be large, the result of the prime factorization of $$$k$$$ is given instead.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2 \\le n \\le 10^5$$$) — the number of nodes in the tree. Each of the next $$$n-1$$$ lines describes an edge: the $$$i$$$-th line contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \\le u_i, v_i \\le n$$$; $$$u_i \\ne v_i$$$) — indices of vertices connected by the $$$i$$$-th edge. Next line contains a single integer $$$m$$$ ($$$1 \\le m \\le 6 \\cdot 10^4$$$) — the number of prime factors of $$$k$$$. Next line contains $$$m$$$ prime numbers $$$p_1, p_2, \\ldots, p_m$$$ ($$$2 \\le p_i < 6 \\cdot 10^4$$$) such that $$$k = p_1 \\cdot p_2 \\cdot \\ldots \\cdot p_m$$$. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$, the sum of $$$m$$$ over all test cases doesn't exceed $$$6 \\cdot 10^4$$$, and the given edges for each test cases form a tree.", "output_spec": "Print the maximum distribution index you can get. Since answer can be too large, print it modulo $$$10^9+7$$$.", "sample_inputs": ["3\n4\n1 2\n2 3\n3 4\n2\n2 2\n4\n3 4\n1 3\n3 2\n2\n3 2\n7\n6 1\n2 3\n4 6\n7 3\n5 1\n3 6\n4\n7 5 13 3"], "sample_outputs": ["17\n18\n286"], "notes": "Note In the first test case, one of the optimal ways is on the following image: In this case, $$$f(1,2)=1$$$, $$$f(1,3)=3$$$, $$$f(1,4)=5$$$, $$$f(2,3)=2$$$, $$$f(2,4)=4$$$, $$$f(3,4)=2$$$, so the sum of these $$$6$$$ numbers is $$$17$$$. In the second test case, one of the optimal ways is on the following image: In this case, $$$f(1,2)=3$$$, $$$f(1,3)=1$$$, $$$f(1,4)=4$$$, $$$f(2,3)=2$$$, $$$f(2,4)=5$$$, $$$f(3,4)=3$$$, so the sum of these $$$6$$$ numbers is $$$18$$$."}, "positive_code": [{"source_code": "MOD = 1000000007\ndef dfs2(ini,g)\n n = Array.new(g.length,-1)\n arr = []\n stack = [ini]\n n[ini] = 0\n while !stack.empty?\n v = stack[0]\n stack.shift\n cnt = 0\n g[v].each do |x|\n if n[x] == -1\n n[x] = 0\n cnt += 1\n stack.unshift(x)\n end\n end\n arr.unshift([v,cnt])\n if cnt == 0\n while !arr.empty?\n if arr[0][1] == 0\n n[arr[0][0]] += 1\n num = n[arr[0][0]]\n arr.shift\n if !arr.empty?\n arr[0][1] -= 1\n n[arr[0][0]] += num\n end\n else\n break\n end\n end\n end\n end\n n\nend\nt = gets.to_i\nans = []\nt.times do\n n = gets.to_i\n node = Array.new(n+1){Array.new}\n a = Array.new(n)\n b = Array.new(n)\n (n-1).times do |i|\n a[i],b[i] = gets.chomp.split(\" \").map(&:to_i)\n node[a[i]].push(b[i])\n node[b[i]].push(a[i])\n end\n m = gets.to_i\n p = gets.chomp.split(\" \").map(&:to_i)\n num = dfs2(1,node)\n arr = []\n (n-1).times do |i|\n d1 = [num[a[i]],num[b[i]]].min\n d2 = n - d1\n arr << d1 * d2\n end\n arr = arr.sort.reverse\n p = p.sort.reverse\n if m > n-1\n (m-n+1).times do |i|\n p[1] *= p[0]\n p[1] %= MOD\n p.shift\n end\n end\n sum = 0\n (n-1).times do |i|\n if m > i\n sum += (p[i] * (arr[i] % MOD)) % MOD\n else\n sum += arr[i] % MOD\n end\n sum %= MOD\n end\n ans << sum\nend\nans.each do |x|\n puts x\nend"}], "negative_code": [{"source_code": "MOD = 1000000007\ndef bfs(ini, g, b)\n dist = Array.new(g.length,-1)\n dist[b] = 0\n dist[ini] = 0\n queue = [ini]\n cnt = 0\n while !queue.empty?\n v = queue[0]\n queue.shift\n g[v].each do |des|\n if dist[des] == -1\n dist[des] = dist[v] + 1\n queue.push(des)\n cnt += 1\n end\n end\n end\n cnt\nend\nt = gets.to_i\nans = []\nt.times do\n n = gets.to_i\n node = Array.new(n+1){Array.new()}\n a = Array.new(n)\n b = Array.new(n)\n (n-1).times do |i|\n a[i],b[i] = gets.chomp.split(\" \").map(&:to_i)\n node[a[i]].push(b[i])\n node[b[i]].push(a[i])\n end\n m = gets.to_i\n p = gets.chomp.split(\" \").map(&:to_i)\n arr = []\n (n-1).times do |i|\n d1 = bfs(a[i],node,b[i])\n d2 = n-1 - 1 - d1\n arr << (d1+1) * (d2+1)\n end\n arr = arr.sort.reverse\n p = p.sort.reverse\n if m > n-1\n (m-n+1).times do |i|\n p[1] *= p[0]\n p[1] %= MOD\n p.shift\n end\n end\n sum = 0\n print arr\n puts \"\"\n print p\n puts \"\"\n (n-1).times do |i|\n if m > i\n sum += (p[i] * arr[i]) % MOD\n else\n sum += arr[i] % MOD\n end\n sum %= MOD\n end\n ans << sum\nend\nans.each do |x|\n puts x\nend"}, {"source_code": "MOD = 1000000007\ndef bfs(ini, g, b)\n dist = Array.new(g.length,-1)\n dist[b] = 0\n dist[ini] = 0\n queue = [ini]\n cnt = 0\n while !queue.empty?\n v = queue[0]\n queue.shift\n g[v].each do |des|\n if dist[des] == -1\n dist[des] = dist[v] + 1\n queue.push(des)\n cnt += 1\n end\n end\n end\n cnt\nend\nt = gets.to_i\nans = []\nt.times do\n n = gets.to_i\n node = Array.new(n+1){Array.new()}\n a = Array.new(n)\n b = Array.new(n)\n (n-1).times do |i|\n a[i],b[i] = gets.chomp.split(\" \").map(&:to_i)\n node[a[i]].push(b[i])\n node[b[i]].push(a[i])\n end\n m = gets.to_i\n p = gets.chomp.split(\" \").map(&:to_i)\n arr = []\n (n-1).times do |i|\n d1 = bfs(a[i],node,b[i])\n d2 = n-1 - 1 - d1\n arr << (d1+1) * (d2+1)\n end\n arr = arr.sort.reverse\n p = p.sort.reverse\n sum = 0\n (n-1).times do |i|\n if m > i\n sum += (p[i] * arr[i]) % MOD\n else\n sum += arr[i] % MOD\n end\n sum %= MOD\n end\n ans << sum\nend\nans.each do |x|\n puts x\nend"}], "src_uid": "968b3db21bd16bc04bdb355e98079d5d"} {"nl": {"description": "Moamen has an array of $$$n$$$ distinct integers. He wants to sort that array in non-decreasing order by doing the following operations in order exactly once: Split the array into exactly $$$k$$$ non-empty subarrays such that each element belongs to exactly one subarray. Reorder these subarrays arbitrary. Merge the subarrays in their new order. A sequence $$$a$$$ is a subarray of a sequence $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.Can you tell Moamen if there is a way to sort the array in non-decreasing order using the operations written above?", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^3$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le k \\le n \\le 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$0 \\le |a_i| \\le 10^9$$$). It is guaranteed that all numbers are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3\\cdot10^5$$$.", "output_spec": "For each test case, you should output a single string. If Moamen can sort the array in non-decreasing order, output \"YES\" (without quotes). Otherwise, output \"NO\" (without quotes). You can print each letter of \"YES\" and \"NO\" in any case (upper or lower).", "sample_inputs": ["3\n5 4\n6 3 4 2 1\n4 2\n1 -4 0 -2\n5 1\n1 2 3 4 5"], "sample_outputs": ["Yes\nNo\nYes"], "notes": "NoteIn the first test case, $$$a = [6, 3, 4, 2, 1]$$$, and $$$k = 4$$$, so we can do the operations as follows: Split $$$a$$$ into $$$\\{ [6], [3, 4], [2], [1] \\}$$$. Reorder them: $$$\\{ [1], [2], [3,4], [6] \\}$$$. Merge them: $$$[1, 2, 3, 4, 6]$$$, so now the array is sorted. In the second test case, there is no way to sort the array by splitting it into only $$$2$$$ subarrays.As an example, if we split it into $$$\\{ [1, -4], [0, -2] \\}$$$, we can reorder them into $$$\\{ [1, -4], [0, -2] \\}$$$ or $$$\\{ [0, -2], [1, -4] \\}$$$. However, after merging the subarrays, it is impossible to get a sorted array."}, "positive_code": [{"source_code": "gets.to_i.times {\n n, k = gets.split.map(&:to_i)\n xs = gets.split.map(&:to_i)\n ys = (0...n).to_a.sort_by { xs[_1] }\n b = 1\n (1...n).each { |i|\n b += 1 if ys[i] - 1 != ys[i - 1]\n }\n puts(b <= k ? 'Yes' : 'No')\n}\n"}, {"source_code": "gets.to_i.times {\n n, k = gets.split.map(&:to_i)\n xs = gets.split.map(&:to_i)\n\n prev = {}\n ys = xs.sort\n ys.each_with_index { |x, i|\n prev[x] = ys[i - 1] if i > 0\n }\n\n blocks = 1\n (1...n).each { |i|\n blocks += 1 if prev[xs[i]] != xs[i - 1]\n }\n\n puts(blocks <= k ? 'Yes' : 'No')\n}\n"}, {"source_code": "gets.to_i.times do\r\n n, k = gets.split.map(&:to_i)\r\n as = gets.split.map(&:to_i)\r\n sorted = as.sort.zip(0..).to_h\r\n puts as.chunk_while { |a, b| sorted[a] == sorted[b] - 1 }.count <= k ? \"Yes\" : \"No\"\r\nend\r\n"}, {"source_code": "#!/usr/bin/env ruby\r\n\r\ntc = gets.to_i\r\n\r\ndef order(ar)\r\n sorted = ar.sort;\r\n hsh = sorted.zip(0..).to_h\r\n return ar.map{|x| hsh[x]}\r\nend\r\n\r\ndef correct(ar)\r\n ar2 = ar.dup\r\n ar2.shift\r\n return ar.zip(ar2).map { |x| x[1] == x[0]+1 ? 1 : 0}.sum\r\nend\r\n\r\nfor t in 1..tc\r\n n, k = gets.split.map { |x| x.to_i };\r\n ar =order(gets.split.map {|x| x.to_i});\r\n \r\n#puts \"#{ar}, #{n-correct(ar)}\"\r\n puts \"#{n-correct(ar) <= k ? 'Yes' : 'No'}\"\r\nend\r\n"}, {"source_code": "t = gets.chomp.to_i\r\nt.times do\r\n n, k = gets.chomp.split(\" \").map(&:to_i)\r\n a = gets.chomp.split(\" \").map(&:to_i)\r\n a_dup = a.dup\r\n a_dup = a_dup.sort\r\n hash = Hash.new(0)\r\n a_dup.each_with_index do |i,index|\r\n hash[i] = index\r\n end\r\n arr = []\r\n cnt = 0\r\n a.each do |i|\r\n if arr == []\r\n arr << i\r\n else\r\n if hash[arr[-1]] + 1 != hash[i]\r\n cnt += 1\r\n end\r\n arr << i\r\n end\r\n end\r\n if cnt + 1 <= k\r\n puts \"Yes\"\r\n else\r\n puts \"No\"\r\n end\r\nend\r\n"}, {"source_code": "END { load __FILE__ unless $stdin.eof? }\n\nt = gets.to_i\nt.times do\n\tn, k = gets.split.map!(&:to_i)\n\ta = gets.split.map!(&:to_i)\n\n\th = a.sort.each_with_index.to_h\n\ta.map!(&h)\n\n\tc = a.each_cons(2).count { |x, y| x + 1 != y }\n\tputs (k >= c + 1) ? :Yes : :No\nend\n"}], "negative_code": [{"source_code": "gets.to_i.times do\r\n n, k = gets.split.map(&:to_i)\r\n as = gets.split.map(&:to_i)\r\n puts as.chunk_while { |a, b| a < b }.count <= k ? \"Yes\" : \"No\"\r\nend\r\n"}], "src_uid": "255d6fca1379ae40b03e761802f36664"} {"nl": {"description": "Little penguin Polo adores integer segments, that is, pairs of integers [l; r] (l ≤ r). He has a set that consists of n integer segments: [l1; r1], [l2; r2], ..., [ln; rn]. We know that no two segments of this set intersect. In one move Polo can either widen any segment of the set 1 unit to the left or 1 unit to the right, that is transform [l; r] to either segment [l - 1; r], or to segment [l; r + 1].The value of a set of segments that consists of n segments [l1; r1], [l2; r2], ..., [ln; rn] is the number of integers x, such that there is integer j, for which the following inequality holds, lj ≤ x ≤ rj.Find the minimum number of moves needed to make the value of the set of Polo's segments divisible by k.", "input_spec": "The first line contains two integers n and k (1 ≤ n, k ≤ 105). Each of the following n lines contain a segment as a pair of integers li and ri ( - 105 ≤ li ≤ ri ≤ 105), separated by a space. It is guaranteed that no two segments intersect. In other words, for any two integers i, j (1 ≤ i < j ≤ n) the following inequality holds, min(ri, rj) < max(li, lj).", "output_spec": "In a single line print a single integer — the answer to the problem.", "sample_inputs": ["2 3\n1 2\n3 4", "3 7\n1 2\n3 3\n4 7"], "sample_outputs": ["2", "0"], "notes": null}, "positive_code": [{"source_code": "n,k=gets.split.map(&:to_i)\nsum=0\nn.times do\n a,b=gets.split.map(&:to_i)\n sum+=b-a+1\nend\nputs sum%k==0?0:k-sum%k"}, {"source_code": "def run\n n, k = $stdin.readline.split.map(&:to_i)\n\n sum = 0\n $stdin.readlines.each do |line|\n l, r = line.split.map(&:to_i)\n sum += r - l + 1\n end\n\n puts (sum % k == 0) ? 0 : (k - sum % k)\nend\n\nrun if $0 == __FILE__\n"}, {"source_code": "class A\n def initialize\n n, k = $stdin.gets.chomp.split(' ').map(&:to_i)\n\n total = 0\n n.times do\n l, r = $stdin.gets.chomp.split(' ').map(&:to_i)\n count = r - l + 1\n total += count\n end\n\n if total%k == 0\n puts 0\n else\n puts k - (total%k)\n end\n end\nend\n\na = A.new"}, {"source_code": "a=gets.chomp.split(\" \")\nk=a[1].to_i\nx=0\n(a[0].to_i).times do\ntemp=gets.chomp.split(\" \")\nx+=temp[1].to_i-temp[0].to_i+1\nend\nputs \"#{(k-(x%k))%k}\""}, {"source_code": "n,k=gets.split.map(&:to_i)\ns=0\nn.times do\n l,r=gets.split.map(&:to_i)\n s+=r-l+1\nend\nputs (k-s%k)%k"}, {"source_code": "n, k = gets.split(' ').map { |x| x.to_i }\nt = 0\n(1..n).each do |x|\n a,b = gets.split(' ').map { |x| x.to_i }\n t += (b-a).abs + 1\nend\nputs (k - t % k) % k"}, {"source_code": "n, m = gets.split(' ').map!{ |i| i.to_i }\n\narr = Array.new\nsum = 0\nn.times {\n\tl, r = gets.split(' ').map!{ |i| i.to_i }\n\tarr << r - l + 1\n\tsum += arr[-1]\n}\n\nleft = sum % m\n\nans = left == 0 ? 0 : m - left\n\np ans"}, {"source_code": "n, k = gets.split.map(&:to_i)\nlrs = n.times.map { gets.split.map(&:to_i) }\ncount = lrs.sort_by(&:first).map { |(a, b)| b - a + 1 }.reduce(:+)\nif count % k == 0\n p 0\nelse\n p k - count % k\nend"}, {"source_code": "n,kp = gets.split(\" \").collect {|x| x.to_i}\nlast = -1\nrs = 0\nls = 0\ns = 0\n(0...n).each {|k|\n\tls,rs = gets.split(\" \").collect {|x| x.to_i}\n\ts+= (rs-ls+1)\n}\n\n\nputs (kp - s%kp)%kp"}, {"source_code": "n, k = gets.split.collect{|i| i.to_i}\ncnt = 0\n(0...n).each do\n l, r = gets.split.collect{|i| i.to_i}\n cnt += r-l+1\nend\nif cnt%k == 0\n puts \"0\"\nelse\n puts k-cnt%k\nend\n"}, {"source_code": "n, k = gets.split(\" \").collect{|x| x.to_i}\n\nv = 0\nc = 0\nn.times do\n l, r = gets.split(\" \").collect{|x| x.to_i}\n v += r - l + 1\nend\n\nrest = v % k\nputs (k - rest) % k"}, {"source_code": "def read_next_line\n gets.split.map { |x| x.to_i }\nend\n\nn, k = read_next_line\nsum = 0\nfor _ in 1..n\n l, r = read_next_line\n sum += r - l + 1\nend\nrem = sum % k\nputs(rem == 0 ? 0 : k - rem)"}, {"source_code": "#!/usr/bin/env ruby\n\nn, k = gets.split.collect{|i| i.to_i}\ntot = 0\nn.times do\n\tx, y = gets.split.collect{|i| i.to_i}\n\ttot = (tot + y - x + 1) % k\nend\n\nputs (tot == 0 ? 0 : k - tot)\n"}, {"source_code": "n,k = gets.strip.split.map(&:to_i)\n\nsegments = []\nn.times do\n segments << gets.strip.split.map(&:to_i)[0..1]\nend\n\nvalue = segments.map{|x| x[1] - x[0] + 1}.inject :+\nputs value % k == 0 ? 0 : ((value / k) + 1) * k - value\n\n"}], "negative_code": [{"source_code": "n, k = gets.split(' ').map { |x| x.to_i }\nm = (0..10000).to_a\n(1..n).each do |x|\n a,b = gets.split(' ').map { |x| x.to_i }\n (a..b).each { |i| m[i] = 1 }\nend\nt = 0\nm.each {|x| if x == 1 then t = t+1 end}\ns = 0\nwhile (t % k != 0 )\n t = t + 1\n s = s + 1\nend\nputs s"}, {"source_code": "n, k = gets.split(' ').map { |x| x.to_i }\nt = 0\n(1..n).each do |x|\n a,b = gets.split(' ').map { |x| x.to_i }\n t += b-a.abs + 1\nend\nif t <= k then puts k-t\nelse \n r = k\n while r <= t\n r += k\n end\n puts r - t\nend"}, {"source_code": "n, k = gets.split(' ').map { |x| x.to_i }\nt = 0\n(1..n).each do |x|\n a,b = gets.split(' ').map { |x| x.to_i }\n t += b-a.abs + 1\nend\nif t <= k then puts k-t\nelse \n r = k\n while r < t\n r += k\n end\n puts r - t\nend"}, {"source_code": "n, k = gets.split(' ').map { |x| x.to_i }\nm = Array.new(20001)\n(1..n).each do |x|\n a,b = gets.split(' ').map { |x| x.to_i }\n (a..b).each { |i| m[i+10000] = 1 }\nend\nt = 0\nm.each {|x| if x == 1 then t = t+1 end}\nif t <= k then puts k-t\nelse \n r = k\n while r <= t\n r = r + k\n end\n puts r-t\nend"}, {"source_code": "n, k = gets.split(' ').map { |x| x.to_i }\nt = 0\n(1..n).each do |x|\n a,b = gets.split(' ').map { |x| x.to_i }\n t += b-a.abs + 1\nend\nif t <= k then puts k-t\nelse \n puts k - (t % k)\nend"}], "src_uid": "3a93a6f78b41cbb43c616f20beee288d"} {"nl": {"description": "n children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to n. In the beginning, the first child is considered the leader. The game is played in k steps. In the i-th step the leader counts out ai people in clockwise order, starting from the next person. The last one to be pointed at by the leader is eliminated, and the next player after him becomes the new leader.For example, if there are children with numbers [8, 10, 13, 14, 16] currently in the circle, the leader is child 13 and ai = 12, then counting-out rhyme ends on child 16, who is eliminated. Child 8 becomes the leader.You have to write a program which prints the number of the child to be eliminated on every step.", "input_spec": "The first line contains two integer numbers n and k (2 ≤ n ≤ 100, 1 ≤ k ≤ n - 1). The next line contains k integer numbers a1, a2, ..., ak (1 ≤ ai ≤ 109).", "output_spec": "Print k numbers, the i-th one corresponds to the number of child to be eliminated at the i-th step.", "sample_inputs": ["7 5\n10 4 11 4 1", "3 2\n2 5"], "sample_outputs": ["4 2 5 6 1", "3 2"], "notes": "NoteLet's consider first example: In the first step child 4 is eliminated, child 5 becomes the leader. In the second step child 2 is eliminated, child 3 becomes the leader. In the third step child 5 is eliminated, child 6 becomes the leader. In the fourth step child 6 is eliminated, child 7 becomes the leader. In the final step child 1 is eliminated, child 3 becomes the leader. "}, "positive_code": [{"source_code": "#!/usr/bin/env ruby\n\nn, k = gets.split.map(&:to_i)\nks = gets.split.map(&:to_i)\n\nouts = []\nchilds = Array.new(n)\nchilds.each_with_index { |v, i| childs[i] = i + 1 }\n\ncur = 0\nks.each do |k|\n k_i = (cur + (k % childs.size)) % childs.size\n outs << childs[k_i]\n childs.delete_at(k_i)\n cur = k_i % childs.size\nend\n\nputs outs.join(\" \")\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\n\nstatus = (1..n).to_a\nleader = 0\na.each { |step|\n eliminate = (leader + step) % status.length\n print \"#{status[eliminate]} \"\n status = status.select.with_index { |v, i| i != eliminate }\n leader = eliminate\n}\nprint \"\\n\"\n"}, {"source_code": "n,k=gets.split(\" \").map(&:to_i)\na=gets.split(\" \").map(&:to_i)\ndt=(1..n).to_a\n\nnow=0\n\nans=[]\n\nfor i in (0...k)\n\ttmp=a[i]%dt.length\n\tnow=(now+tmp)%dt.length\n\tans<= children.length\n \n first = false\nend\n\n\n\n"}], "negative_code": [], "src_uid": "5512fbe2963dab982a58a14daf805291"} {"nl": {"description": "One day, Ahmed_Hossam went to Hemose and said \"Let's solve a gym contest!\". Hemose didn't want to do that, as he was playing Valorant, so he came up with a problem and told it to Ahmed to distract him. Sadly, Ahmed can't solve it... Could you help him?There is an Agent in Valorant, and he has $$$n$$$ weapons. The $$$i$$$-th weapon has a damage value $$$a_i$$$, and the Agent will face an enemy whose health value is $$$H$$$.The Agent will perform one or more moves until the enemy dies.In one move, he will choose a weapon and decrease the enemy's health by its damage value. The enemy will die when his health will become less than or equal to $$$0$$$. However, not everything is so easy: the Agent can't choose the same weapon for $$$2$$$ times in a row.What is the minimum number of times that the Agent will need to use the weapons to kill the enemy?", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \\leq t \\leq 10^5)$$$. Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$H$$$ $$$(2 \\leq n \\leq 10^3, 1 \\leq H \\leq 10^9)$$$ — the number of available weapons and the initial health value of the enemy. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ $$$(1 \\leq a_i \\leq 10^9)$$$ — the damage values of the weapons. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, print a single integer — the minimum number of times that the Agent will have to use the weapons to kill the enemy.", "sample_inputs": ["3\n2 4\n3 7\n2 6\n4 2\n3 11\n2 1 7"], "sample_outputs": ["1\n2\n3"], "notes": "NoteIn the first test case, the Agent can use the second weapon, making health value of the enemy equal to $$$4-7=-3$$$. $$$-3 \\le 0$$$, so the enemy is dead, and using weapon $$$1$$$ time was enough.In the second test case, the Agent can use the first weapon first, and then the second one. After this, the health of enemy will drop to $$$6-4-2 = 0$$$, meaning he would be killed after using weapons $$$2$$$ times.In the third test case, the Agent can use the weapons in order (third, first, third), decreasing the health value of enemy to $$$11 - 7 - 2 - 7 = -5$$$ after using the weapons $$$3$$$ times. Note that we can't kill the enemy by using the third weapon twice, as even though $$$11-7-7<0$$$, it's not allowed to use the same weapon twice in a row."}, "positive_code": [{"source_code": "\ndef solve(target, arr)\n maxi = [0, 0]\n arr.each do |item|\n maxi[0] = item if item > maxi[0]\n maxi[0], maxi[1] = maxi[1], maxi[0] if maxi[0] > maxi[1]\n end\n\n ans = (target / (maxi.sum)) * 2\n target %= maxi.sum\n if target > 0\n ans += 1\n target -= maxi[1]\n end\n ans += 1 if target > 0\n ans\nend\n\nt = gets.chomp.to_i\nt.times do\n _, h = gets.chomp.split(' ').map(&:to_i)\n arr = gets.chomp.split(' ').map(&:to_i)\n print \"#{solve(h, arr)}\\n\"\nend\n"}], "negative_code": [{"source_code": "\ndef solve(target, arr)\n maxi = [0, 0]\n arr.each do |item|\n maxi[0] = item if item > maxi[0]\n maxi[0], maxi[1] = maxi[1], maxi[0] if maxi[0] > maxi[1]\n end\n\n ans = (target / (maxi.sum)) * 2\n target %= maxi.sum\n ans += 1 if target > 0\n ans\nend\n\nt = gets.chomp.to_i\nt.times do\n _, h = gets.chomp.split(' ').map(&:to_i)\n arr = gets.chomp.split(' ').map(&:to_i)\n print \"#{solve(h, arr)}\\n\"\nend\n"}], "src_uid": "4d5457d9f053556c78c102f5c32f7542"} {"nl": {"description": "A sky scraper with 1000 floors has been built in the city of N. It has modern superfast elevators to help to travel from one floor to another. Each elevator has two doors, the front one and the back one. If one goes in through the front door, he goes out through the back one and vice versa. The elevator has two rails numbered with numbers 1 and 2. Rail 1 is located to the left of the entrance to the front door (or correspondingly, to the right of the entrance to the back door). Rail 2 is located opposite it, to the right of the entrance to the front door and to the left of the entrance to the back door. We know that each person in the city of N holds at a rail with the strongest hand. One day a VIP person visited the city and of course, he took a look at the skyscraper and took a ride in the elevator. We know the door through which he entered and the rail he was holding at. Now we need to determine as soon as possible whether he is left-handed or right-handed.", "input_spec": "The first line indicates the door through which the very important person entered the elevator. It contains \"front\" if the person enters the elevator through the front door and \"back\" if he entered the elevator through the back door. The second line contains integer a (1 ≤ a ≤ 2) which denotes the number of the rail at which the person was holding.", "output_spec": "Print character \"R\" if the VIP is right-handed or \"L\" if he is left-handed.", "sample_inputs": ["front\n1"], "sample_outputs": ["L"], "notes": null}, "positive_code": [{"source_code": "f = File.open('input.txt', 'r') or die \"Unable to open file...\"\ninput = [] # start with an empty array\n f.each_line {|line|\n input.push line\n } \n \ndoor = input[0].chop\nside = input[1].to_i\nwhile door == 'front' or door == 'back'\n File.open('output.txt', 'w') do |o| \n o.puts \"#{(door == 'front')? ((side == 1)? 'L' : 'R') : ((side == 2)? 'L' : 'R')}\" \n end \nexit\nend\n"}, {"source_code": "solve = {['front', '1'] => 'L', ['front', '2'] => 'R', ['back', '1'] => 'R', ['back', '2'] => 'L'}\nFile.open('input.txt', 'r') do |input|\n\tFile.open('output.txt', 'w') do |output|\n\t\toutput.puts solve[[input.gets.chomp, input.gets.chomp]]\n\tend\nend\n\n"}, {"source_code": "p=open(\"input.txt\");open(\"output.txt\",\"w\").puts\"LR\"[p.gets.size+p.gets.to_i-7]\n"}, {"source_code": "p=open(\"input.txt\");open(\"output.txt\",\"w\").puts\"LR\"[p.gets.size+p.gets.to_i-7]\n"}, {"source_code": "p=open(\"input.txt\");open(\"output.txt\",\"w\").puts\"LR\"[p.gets.size+p.gets.to_i-7]"}, {"source_code": "p=open(\"input.txt\");open(\"output.txt\",\"w\").puts\"LR\"[p.gets.size+p.gets.to_i-7]\n"}, {"source_code": "p=open(\"input.txt\");open(\"output.txt\",\"w\").puts\"LR\"[p.gets.size+p.gets.to_i-7]\n"}, {"source_code": "p=open(\"input.txt\");open(\"output.txt\",\"w\").puts\"LR\"[p.gets.size+p.gets.to_i-7]"}, {"source_code": "p=open(\"input.txt\");open(\"output.txt\",\"w\").puts\"LR\"[p.gets.size+p.gets.to_i-7]\n"}, {"source_code": "a, b = nil\nFile.open(\"input.txt\") do |f|\n a = f.gets.strip\n b = f.gets.strip\nend\n\nif (a == 'front' && b == '1') || (a == 'back' && b == '2')\n lr = 'L'\nelse\n lr = 'R'\nend\n\nFile.open(\"output.txt\", \"w\") do |f|\n f.puts lr\nend\n"}, {"source_code": "p=open(\"input.txt\");open(\"output.txt\",\"w\").puts\"LR\"[p.gets.size+p.gets.to_i-7]\n"}, {"source_code": "p=open(\"input.txt\");open(\"output.txt\",\"w\").puts\"LR\"[p.gets.size+p.gets.to_i-7]"}, {"source_code": "open(\"input.txt\"){|p|open(\"output.txt\",\"w\").puts\"LR\"[p.gets.size+p.gets.to_i-7]}\n"}, {"source_code": "p=open(\"input.txt\");open(\"output.txt\",\"w\").puts\"LR\"[p.gets.size+p.gets.to_i-7]\n"}, {"source_code": "p=open(\"input.txt\");open(\"output.txt\",\"w\").puts\"LR\"[p.gets.size+p.gets.to_i-7]\n"}], "negative_code": [], "src_uid": "77093f12ff56d5f404e9c87650d4aeb4"} {"nl": {"description": "Polycarp has a string $$$s$$$ consisting of lowercase Latin letters.He encodes it using the following algorithm.He goes through the letters of the string $$$s$$$ from left to right and for each letter Polycarp considers its number in the alphabet: if the letter number is single-digit number (less than $$$10$$$), then just writes it out; if the letter number is a two-digit number (greater than or equal to $$$10$$$), then it writes it out and adds the number 0 after. For example, if the string $$$s$$$ is code, then Polycarp will encode this string as follows: 'c' — is the $$$3$$$-rd letter of the alphabet. Consequently, Polycarp adds 3 to the code (the code becomes equal to 3); 'o' — is the $$$15$$$-th letter of the alphabet. Consequently, Polycarp adds 15 to the code and also 0 (the code becomes 3150); 'd' — is the $$$4$$$-th letter of the alphabet. Consequently, Polycarp adds 4 to the code (the code becomes 31504); 'e' — is the $$$5$$$-th letter of the alphabet. Therefore, Polycarp adds 5 to the code (the code becomes 315045). Thus, code of string code is 315045.You are given a string $$$t$$$ resulting from encoding the string $$$s$$$. Your task is to decode it (get the original string $$$s$$$ by $$$t$$$).", "input_spec": "The first line of the input contains an integer $$$q$$$ ($$$1 \\le q \\le 10^4$$$) — the number of test cases in the input. The descriptions of the test cases follow. The first line of description of each test case contains one integer $$$n$$$ ($$$1 \\le n \\le 50$$$) — the length of the given code. The second line of the description of each test case contains a string $$$t$$$ of length $$$n$$$ — the given code. It is guaranteed that there exists such a string of lowercase Latin letters, as a result of encoding which the string $$$t$$$ is obtained.", "output_spec": "For each test case output the required string $$$s$$$ — the string that gives string $$$t$$$ as the result of encoding. It is guaranteed that such a string always exists. It can be shown that such a string is always unique.", "sample_inputs": ["9\n\n6\n\n315045\n\n4\n\n1100\n\n7\n\n1213121\n\n6\n\n120120\n\n18\n\n315045615018035190\n\n7\n\n1111110\n\n7\n\n1111100\n\n5\n\n11111\n\n4\n\n2606"], "sample_outputs": ["code\naj\nabacaba\nll\ncodeforces\naaaak\naaaaj\naaaaa\nzf"], "notes": "NoteThe first test case is explained above.In the second test case, the answer is aj. Indeed, the number of the letter a is equal to $$$1$$$, so 1 will be appended to the code. The number of the letter j is $$$10$$$, so 100 will be appended to the code. The resulting code is 1100.There are no zeros in the third test case, which means that the numbers of all letters are less than $$$10$$$ and are encoded as one digit. The original string is abacaba.In the fourth test case, the string $$$s$$$ is equal to ll. The letter l has the number $$$12$$$ and is encoded as 120. So ll is indeed 120120."}, "positive_code": [{"source_code": "c=Hash.new(nil);$stdin.each_line.with_index{|l, i|next if i==0||i%2==1;if c[l]!=nil;puts(c[l]);next;end;nt=0;o=\"\";l.chomp.each_char.with_index {|x, i|if nt!=0;nt-=1;next;end;if (i<(l.chomp.length-2))&&(l[i + 2]==\"0\")&&(i>l.chomp.length-4||l[i + 3]!=\"0\");nt=2;o+=(\"a\"..\"z\").to_a[(x+l[i+1]).to_i-1];next;end;o+=(\"a\"..\"z\").to_a[x.to_i-1]};c[l]=o;puts(o)}"}, {"source_code": "c = Hash.new(nil)\r\n$stdin.each_line.with_index do |l, i| next if i == 0 || i % 2 == 1\r\n if c[l] != nil\r\n puts c[l]\r\n next\r\n end\r\n nt = 0\r\n o = \"\"\r\n l.chomp.each_char.with_index do |x, i|\r\n if nt != 0\r\n nt -= 1\r\n next\r\n end\r\n if (i < (l.chomp.length - 2)) && (l[i + 2] == \"0\") && (i > l.chomp.length - 4 || l[i + 3] != \"0\")\r\n nt = 2\r\n o += (\"a\"..\"z\").to_a[(x + l[i + 1]).to_i - 1]\r\n next\r\n end\r\n o += (\"a\"..\"z\").to_a[x.to_i - 1]\r\n end\r\n c[l]=o\r\n puts o\r\nend"}, {"source_code": "\r\ndef solve(n, s)\r\n alphabets = ['@', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']\r\n\r\n ans = ''\r\n i = n-1\r\n while i >= 0 do\r\n if s[i] == '0'\r\n ans += alphabets[s[(i-2)..(i-1)].to_i]\r\n i -= 3\r\n else\r\n ans += alphabets[s[i].to_i]\r\n # p alphabets[s[i].to_i]\r\n i -= 1\r\n end\r\n end\r\n ans.reverse!\r\n puts ans\r\nend\r\n\r\nt = gets.chomp.to_i\r\nfor i in 1..t\r\n n = gets.chomp.to_i\r\n s = gets.chomp\r\n solve(n, s)\r\nend\r\n"}, {"source_code": "\r\ndef debug(*arg)\r\n puts \"[DEBUG] #{arg.join \" \"}\"\r\nend\r\n\r\nt = gets.to_i\r\n\r\nalpha = ('a'.upto 'z').to_a\r\n\r\n\r\nt.times do\r\n n = gets.to_i\r\n s = gets.chomp.chars\r\n\r\n ans = []\r\n until s.empty?\r\n last = s.pop\r\n if last == '0'\r\n ans << alpha[s[-2..].join.to_i - 1]\r\n s.pop; s.pop\r\n else\r\n ans << alpha[last.to_i - 1]\r\n end\r\n\r\n end\r\n puts ans.reverse.join(\"\")\r\nend"}, {"source_code": "$stdin.readline\r\n\r\ncache = {}\r\n\r\nwhile !$stdin.eof do\r\n $stdin.readline\r\n codestr = $stdin.readline.chomp\r\n\r\n if cache[codestr]\r\n puts cache[codestr]\r\n next\r\n end\r\n\r\n str = codestr.reverse.scan(/0[0-9][1-9]|[1-9]/).map { |code|\r\n code = code.reverse.to_i\r\n code = code < 10 ? code : code / 10\r\n (\"a\"..\"z\").to_a[code - 1]\r\n }.join.reverse\r\n\r\n puts cache[codestr] = str\r\nend"}, {"source_code": "def ord_to_char(seq_ord)\r\n ('a'.ord+seq_ord-1).chr\r\nend\r\n\r\ndef decode(encoded_str,len)\r\n # Search from the tail is better.\r\n ptr=len-1\r\n res_str=[]\r\n while ptr>=0\r\n if encoded_str[ptr]=='0'\r\n res_str << ord_to_char(encoded_str[ptr-2...ptr].to_i)\r\n ptr-=3\r\n else\r\n res_str << ord_to_char(encoded_str[ptr].to_i)\r\n ptr-=1\r\n end\r\n end\r\n res_str.reverse.join('')\r\nend\r\n\r\nn_rounds=gets.to_i\r\n\r\nn_rounds.times do\r\n len=gets.to_i\r\n estr=gets\r\n puts decode(estr,len)\r\nend\r\n\r\n"}], "negative_code": [{"source_code": "c = Hash.new(nil)\r\n$stdin.each_line.with_index do |l, i| next if i == 0 || i % 2 == 1\r\n puts c[l] if c[l] != nil\r\n nt = 0\r\n o = \"\"\r\n l.chomp.each_char.with_index do |x, i|\r\n if nt != 0\r\n nt -= 1\r\n next\r\n end\r\n if (i < (l.chomp.length - 2)) && (l[i + 2] == \"0\") && (i > l.chomp.length - 4 || l[i + 3] != \"0\")\r\n nt = 2\r\n o += (\"a\"..\"z\").to_a[(x + l[i + 1]).to_i - 1]\r\n next\r\n end\r\n o += (\"a\"..\"z\").to_a[x.to_i - 1]\r\n end\r\n c[l]=o\r\n \r\n \r\n \r\n \r\n \r\n \r\n puts o\r\nend"}, {"source_code": "c = Hash.new(nil)\r\n$stdin.each_line.with_index do |l, i| next if i == 0 || i % 2 == 1\r\n puts c[l] if c[l] != nil\r\n nt = 0\r\n o = \"\"\r\n l.chomp.each_char.with_index do |x, i|\r\n if nt != 0\r\n nt -= 1\r\n next\r\n end\r\n if (i < (l.chomp.length - 2)) && (l[i + 2] == \"0\") && (i > l.chomp.length - 4 || l[i + 3] != \"0\")\r\n nt = 2\r\n o += (\"a\"..\"z\").to_a[(x + l[i + 1]).to_i - 1]\r\n next\r\n end\r\n o += (\"a\"..\"z\").to_a[x.to_i - 1]\r\n end\r\n c[l]=o\r\n puts o\r\nend"}, {"source_code": "\r\ndef solve(n, s)\r\n alphabets = ['@', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']\r\n\r\n ans = ''\r\n i = n-1\r\n while i >= 0 do\r\n if s[i] == '0'\r\n ans += alphabets[s[(i-2)..(i-1)].to_i]\r\n i -= 3\r\n else\r\n ans += alphabets[s[i].to_i]\r\n # p alphabets[s[i].to_i]\r\n i -= 1\r\n end\r\n end\r\n ans.reverse!\r\n p ans\r\nend\r\n\r\nt = gets.chomp.to_i\r\nfor i in 1..t\r\n n = gets.chomp.to_i\r\n s = gets.chomp\r\n solve(n, s)\r\nend\r\n"}, {"source_code": "\r\ndef solve(n, s)\r\n alphabets = ['@', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']\r\n\r\n ans = \"\"\r\n i = n-1\r\n while i >= 0 do\r\n if s[i] == '0'\r\n ans += alphabets[s[(i-2)..(i-1)].to_i]\r\n i -= 3\r\n else\r\n ans += alphabets[s[i].to_i]\r\n # p alphabets[s[i].to_i]\r\n i -= 1\r\n end\r\n end\r\n ans.reverse!\r\n p ans\r\nend\r\n\r\nt = gets.chomp.to_i\r\nfor i in 1..t\r\n n = gets.chomp.to_i\r\n s = gets.chomp\r\n solve(n, s)\r\nend\r\n"}, {"source_code": "\r\ndef debug(*arg)\r\n puts \"[DEBUG] #{arg.join \" \"}\"\r\nend\r\n\r\nt = gets.to_i\r\n\r\nalpha = ('a'.upto 'z').to_a\r\n\r\np alpha\r\n\r\nt.times do\r\n n = gets.to_i\r\n s = gets.chomp.chars\r\n\r\n ans = []\r\n until s.empty?\r\n last = s.pop\r\n if last == '0'\r\n ans << alpha[s[-2..].join.to_i - 1]\r\n s.pop; s.pop\r\n else\r\n ans << alpha[last.to_i - 1]\r\n end\r\n\r\n end\r\n puts ans.reverse.join(\"\")\r\nend"}], "src_uid": "43081557fe2fbac39dd9b72b137b8fb0"} {"nl": {"description": "You have a statistic of price changes for one product represented as an array of $$$n$$$ positive integers $$$p_0, p_1, \\dots, p_{n - 1}$$$, where $$$p_0$$$ is the initial price of the product and $$$p_i$$$ is how the price was increased during the $$$i$$$-th month.Using these price changes you are asked to calculate the inflation coefficients for each month as the ratio of current price increase $$$p_i$$$ to the price at the start of this month $$$(p_0 + p_1 + \\dots + p_{i - 1})$$$.Your boss said you clearly that the inflation coefficients must not exceed $$$k$$$ %, so you decided to increase some values $$$p_i$$$ in such a way, that all $$$p_i$$$ remain integers and the inflation coefficients for each month don't exceed $$$k$$$ %.You know, that the bigger changes — the more obvious cheating. That's why you need to minimize the total sum of changes.What's the minimum total sum of changes you need to make all inflation coefficients not more than $$$k$$$ %?", "input_spec": "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 two integers $$$n$$$ and $$$k$$$ ($$$2 \\le n \\le 100$$$; $$$1 \\le k \\le 100$$$) — the length of array $$$p$$$ and coefficient $$$k$$$. The second line of each test case contains $$$n$$$ integers $$$p_0, p_1, \\dots, p_{n - 1}$$$ ($$$1 \\le p_i \\le 10^9$$$) — the array $$$p$$$.", "output_spec": "For each test case, print the minimum total sum of changes you need to make all inflation coefficients not more than $$$k$$$ %.", "sample_inputs": ["2\n4 1\n20100 1 202 202\n3 100\n1 1 1"], "sample_outputs": ["99\n0"], "notes": "NoteIn the first test case, you can, for example, increase $$$p_0$$$ by $$$50$$$ and $$$p_1$$$ by $$$49$$$ and get array $$$[20150, 50, 202, 202]$$$. Then you get the next inflation coefficients: $$$\\frac{50}{20150} \\le \\frac{1}{100}$$$; $$$\\frac{202}{20150 + 50} \\le \\frac{1}{100}$$$; $$$\\frac{202}{20200 + 202} \\le \\frac{1}{100}$$$; In the second test case, you don't need to modify array $$$p$$$, since the inflation coefficients are already good: $$$\\frac{1}{1} \\le \\frac{100}{100}$$$; $$$\\frac{1}{1 + 1} \\le \\frac{100}{100}$$$; "}, "positive_code": [{"source_code": "gets.to_i.times do\r\n n, k = gets.split.map(&:to_i)\r\n ps0, *ps = gets.split.map(&:to_i)\r\n nps = (ps0..10 ** 12).bsearch do |ps0|\r\n x = ps0\r\n ps.all? do |p|\r\n p.to_f / x <= k / 100.0 && (x += p)\r\n end\r\n end\r\n p nps - ps0\r\nend\r\n"}, {"source_code": "gets.to_i.times do\r\n n,k=gets.split.map(&:to_i)\r\n q=gets.split.map(&:to_i)\r\n c=0\r\n s=q[0]\r\n 1.upto(n-1) do |i|\r\n if 100*q[i]-k*(s+c)>0\r\n c+=(100*q[i]-1)/k+1-s-c\r\n end\r\n s+=q[i]\r\n end\r\n puts c\r\nend\r\n"}, {"source_code": "def read\r\n gets.split.map &:to_i\r\nend\r\ngets.to_i.times do\r\n n, k = read\r\n a = read\r\n s, res = a[0], 0\r\n (1...n).each do |i|\r\n res = [res, (100 * a[i] - k * s + k - 1) / k].max\r\n s += a[i]\r\n end\r\n puts res\r\nend"}], "negative_code": [{"source_code": "gets.to_i.times do\r\n n,k=gets.split.map(&:to_i)\r\n q=gets.split.map(&:to_i)\r\n c=0\r\n s=q[0]\r\n 1.upto(n-1) do |i|\r\n if 100*q[i]-k*(s+c)>0\r\n c+=(100*q[i]-1)/k+1-s\r\n end\r\n s+=q[i]\r\n end\r\n puts c\r\nend\r\n"}], "src_uid": "53975eea2503bb47bfd0a5119406aea3"} {"nl": {"description": "Polycarp wants to buy exactly $$$n$$$ shovels. The shop sells packages with shovels. The store has $$$k$$$ types of packages: the package of the $$$i$$$-th type consists of exactly $$$i$$$ shovels ($$$1 \\le i \\le k$$$). The store has an infinite number of packages of each type.Polycarp wants to choose one type of packages and then buy several (one or more) packages of this type. What is the smallest number of packages Polycarp will have to buy to get exactly $$$n$$$ shovels?For example, if $$$n=8$$$ and $$$k=7$$$, then Polycarp will buy $$$2$$$ packages of $$$4$$$ shovels.Help Polycarp find the minimum number of packages that he needs to buy, given that he: will buy exactly $$$n$$$ shovels in total; the sizes of all packages he will buy are all the same and the number of shovels in each package is an integer from $$$1$$$ to $$$k$$$, inclusive. ", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases in the input. Then, $$$t$$$ test cases follow, one per line. Each test case consists of two positive integers $$$n$$$ ($$$1 \\le n \\le 10^9$$$) and $$$k$$$ ($$$1 \\le k \\le 10^9$$$) — the number of shovels and the number of types of packages.", "output_spec": "Print $$$t$$$ answers to the test cases. Each answer is a positive integer — the minimum number of packages.", "sample_inputs": ["5\n8 7\n8 1\n6 10\n999999733 999999732\n999999733 999999733"], "sample_outputs": ["2\n8\n1\n999999733\n1"], "notes": "NoteThe answer to the first test case was explained in the statement.In the second test case, there is only one way to buy $$$8$$$ shovels — $$$8$$$ packages of one shovel.In the third test case, you need to buy a $$$1$$$ package of $$$6$$$ shovels."}, "positive_code": [{"source_code": "def solve\n n, k = gets.strip.split(' ').map(&:to_i)\n sq = Math.sqrt(n).to_i\n ok = 1e9 + 1\n (1..sq).each { |el|\n if n.modulo(el).zero?\n an1 = 1e9 + 1\n an2 = 1e9 + 1\n an1 = n / el if el <= k\n an2 = el if n / el <= k\n next if an1 == 1e9 + 1 && an2 == 1e9 + 1\n ok = [ok, an1, an2].min\n end\n }\n puts ok\nend\n\ngets.strip.to_i.times do\n solve\nend\n"}, {"source_code": "t = gets.to_i\nt.times do\n n, k = gets.split.map(&:to_i)\n ans = n\n if k >= n\n ans = 1\n else\n d = 1\n while d * d <= n do\n if n % d == 0\n if (n / d) <= k && d < ans\n ans = d\n end\n if d <= k && (n / d) < ans\n ans = (n / d)\n end\n end\n d += 1\n end\n end\n puts ans\nend\n"}, {"source_code": "require 'prime'\n\nclass Integer\n\tdef divisors\n\t\tself.prime_division.inject([1]) do |a, (p, e)|\n\t\t\t(0..e).map { p**_1 }.product(a).map { _1 * _2 }\n\t\tend.sort\n\tend\nend\n\ngets.to_i.times do\n\tn, k = gets.split.map(&:to_i)\n\tputs n / n.divisors.select { _1 <= k }.max\nend\n"}, {"source_code": "gets.strip.to_i.times do |t|\n n, k = gets.strip.split(' ').map(&:to_i)\n sq = Math.sqrt(n).ceil.to_i\n divs = [1, n]\n (2..sq).each do |i|\n if n % i == 0\n divs << i\n divs << n / i\n end\n end\n res = divs.sort.reverse.bsearch { |e| e <= k }\n puts n / res\nend\n"}, {"source_code": "require 'prime'\n\ndef divisors(n)\n res=[]\n (1..n**0.5).select{|i|n%i==0}.each{|i|\n res< s\n puts \"NO\"\n return\n end\n\n if k * (n - 1) < s\n puts \"NO\"\n return\n end\n\n rs = s\n\n dir = 1\n crt = 1\n path = []\n\n k.times do |j|\n rk = k - j\n\n move = (rs.to_f / rk).ceil * dir\n dir *= -1\n rs -= move.abs\n\n crt = crt + move\n path << crt\n end\n\n puts \"YES\"\n puts path.join(' ')\n\nend\n\ndef read_int\n gets.to_i\nend\n\ndef read_ints\n gets.split.map(&:to_i)\nend\n\ndef read_string\n gets.chomp\nend\n\nsolution unless ENV['TEST__MODE']\n"}, {"source_code": "N, K, s = gets.split.map(&:to_i)\nif (N-1)*K < s || K > s\n puts 'NO'\n exit\nend\n\nans = []\nnow = 1\ns -= K\nK.times do |i|\n add = [s, N-2].min\n s -= add\n now += (add+1) * (i%2==0 ? 1 : -1)\n ans.push(now)\nend\nputs 'YES'\nputs ans.join(' ')"}], "negative_code": [{"source_code": "\ndef solution\n n, k, s = read_ints\n\n if k > s\n puts \"NO\"\n return\n end\n\n if s.to_f / k > (n-1)\n puts \"NO\"\n return\n end\n\n crt = 1\n rs = s\n rk = k\n\n path = []\n\n while rs > 0\n steps_per_move = (rs.to_f / rk).ceil\n if crt < steps_per_move\n goto = crt + steps_per_move\n else\n goto = crt - steps_per_move\n end\n\n rs -= (goto-crt).abs\n rk -= 1\n\n path << goto\n crt = goto\n end\n\n puts 'YES'\n puts path.join(' ')\n\nend\n\ndef read_int\n gets.to_i\nend\n\ndef read_ints\n gets.split.map(&:to_i)\nend\n\ndef read_string\n gets.chomp\nend\n\nsolution unless ENV['TEST__MODE']\n"}], "src_uid": "2cc44c5a084688025c16b0394c98b2f6"} {"nl": {"description": "It's hard times now. Today Petya needs to score 100 points on Informatics exam. The tasks seem easy to Petya, but he thinks he lacks time to finish them all, so he asks you to help with one..There is a glob pattern in the statements (a string consisting of lowercase English letters, characters \"?\" and \"*\"). It is known that character \"*\" occurs no more than once in the pattern.Also, n query strings are given, it is required to determine for each of them if the pattern matches it or not.Everything seemed easy to Petya, but then he discovered that the special pattern characters differ from their usual meaning.A pattern matches a string if it is possible to replace each character \"?\" with one good lowercase English letter, and the character \"*\" (if there is one) with any, including empty, string of bad lowercase English letters, so that the resulting string is the same as the given string.The good letters are given to Petya. All the others are bad.", "input_spec": "The first line contains a string with length from 1 to 26 consisting of distinct lowercase English letters. These letters are good letters, all the others are bad. The second line contains the pattern — a string s of lowercase English letters, characters \"?\" and \"*\" (1 ≤ |s| ≤ 105). It is guaranteed that character \"*\" occurs in s no more than once. The third line contains integer n (1 ≤ n ≤ 105) — the number of query strings. n lines follow, each of them contains single non-empty string consisting of lowercase English letters — a query string. It is guaranteed that the total length of all query strings is not greater than 105.", "output_spec": "Print n lines: in the i-th of them print \"YES\" if the pattern matches the i-th query string, and \"NO\" otherwise. You can choose the case (lower or upper) for each letter arbitrary.", "sample_inputs": ["ab\na?a\n2\naaa\naab", "abc\na?a?a*\n4\nabacaba\nabaca\napapa\naaaaax"], "sample_outputs": ["YES\nNO", "NO\nYES\nNO\nYES"], "notes": "NoteIn the first example we can replace \"?\" with good letters \"a\" and \"b\", so we can see that the answer for the first query is \"YES\", and the answer for the second query is \"NO\", because we can't match the third letter.Explanation of the second example. The first query: \"NO\", because character \"*\" can be replaced with a string of bad letters only, but the only way to match the query string is to replace it with the string \"ba\", in which both letters are good. The second query: \"YES\", because characters \"?\" can be replaced with corresponding good letters, and character \"*\" can be replaced with empty string, and the strings will coincide. The third query: \"NO\", because characters \"?\" can't be replaced with bad letters. The fourth query: \"YES\", because characters \"?\" can be replaced with good letters \"a\", and character \"*\" can be replaced with a string of bad letters \"x\". "}, "positive_code": [{"source_code": "#!/usr/bin/env ruby\n# encoding: utf-8\ngood = gets.to_str.chomp\npattern = gets.to_str\nkey = pattern.strip.gsub(\"?\",\"[\"+good+\"]\").gsub(\"*\",\"[^\"+good+\"]*\")\nrg = Regexp.new(\"^\"+key+\"$\")\n\nn = gets.to_i\nn.times do\n buf = gets.to_str\n if rg =~ buf\n puts \"YES\"\n else\n puts \"NO\"\n end\nend\n"}, {"source_code": "# Codeforces My Practice\n# author: Leonardone @ NEETSDKASU\n\ngl = [false] * 256\ngets.strip.each_char do |c|\n gl[c.swapcase.ord] = true\n gl[c.ord] = true\nend\npt = (\"[\" + gets.strip + \"]\").split \"*\"\nn = gets.to_i\nif pt.size == 1\n px = pt[0]\n n.times do\n s = gets.strip\n if s.size != px.size - 2\n puts :NO\n next\n end\n ok = true\n s.size.times do |i|\n next if s[i] == px[i+1]\n if px[i+1] == '?'\n next if gl[s[i].ord]\n end\n ok = false\n break\n end\n puts ok ? :YES : :NO\n end\n exit\nend\n\npx, py = pt\nn.times do\n s = gets.strip\n if s.size < px.size + py.size - 2\n puts :NO\n next\n end\n ok = true\n (px.size-1).times do |i|\n next if s[i] == px[i+1]\n if px[i+1] == '?'\n next if gl[s[i].ord]\n end\n ok = false\n break\n end\n if !ok\n puts :NO\n next\n end\n (py.size-1).times do |i|\n next if s[-i-1] == py[-i-2]\n if py[-i-2] == '?'\n next if gl[s[-i-1].ord]\n end\n ok = false\n break\n end\n if !ok\n puts :NO\n next\n end\n u = s.size - (px.size + py.size - 2)\n u.times do |i|\n next if !gl[s[i+px.size-1].ord]\n ok = false\n break\n end\n puts ok ? :YES : :NO\nend\n"}, {"source_code": "#!/usr/bin/env ruby\n# encoding: utf-8\ngood = gets.to_str.chomp\npattern = gets.to_str\nkey = pattern.strip.gsub(\"?\",\"[\"+good+\"]\").gsub(\"*\",\"[^\"+good+\"]*\")\nrg = Regexp.new(\"^\"+key+\"$\")\n\nn = gets.to_i\nn.times do\n buf = gets.to_str\n if rg =~ buf\n puts \"YES\"\n else\n puts \"NO\"\n end\nend"}, {"source_code": "#!/usr/bin/env ruby\n# encoding: utf-8\ngood = gets.to_str.chomp\npattern = gets.to_str\nkey = pattern.strip.gsub(\"?\",\"[\"+good+\"]\").gsub(\"*\",\"[^\"+good+\"]*\")\nrg = Regexp.new(\"^\"+key+\"$\")\n\nn = gets.to_i\nn.times do\n buf = gets.to_str\n if rg =~ buf\n puts \"YES\"\n else\n puts \"NO\"\n end\nend"}], "negative_code": [{"source_code": "#!/usr/bin/env ruby\n# encoding: utf-8\ngood = gets.to_str.chomp!\npattern = gets.to_str\nkey = \"\"\npattern.each_char do | c |\n if c == '?'\n key+=\"[\"+good+\"]\"\n elsif c == '*'\n key+=\"[^\"+good+\"]{0,1}\"\n else\n key+=c\n end\nend\n\nha = Regexp.new(key)\n\nn = gets.to_i\nn.times do | i |\n buf = gets.to_str\n m=ha.match(buf).to_s\n if m == nil\n puts(\"NO\")\n elsif m == buf\n puts(\"YES\")\n else\n puts(\"NO\")\n end\nend\n"}, {"source_code": "# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n\ngl = gets.strip\npt = gets.strip.gsub(\"?\",\"[\" + gl + \"]\").gsub(\"*\", \".?\")\nrg = Regexp.compile(\"^\" + pt + \"$\")\nn = gets.to_i\nn.times do\n s = gets.strip\n if s =~ rg\n puts \"YES\"\n else\n puts \"NO\"\n end\nend\n\n"}, {"source_code": "# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n\ngl = gets.strip\npt = gets.strip.gsub(\"?\",\"[\" + gl + \"]\").gsub(\"*\", \".*\")\nrg = Regexp.compile(\"^\" + pt + \"$\", Regexp::IGNORECASE)\nn = gets.to_i\nn.times do\n s = gets.strip\n if s =~ rg\n puts \"YES\"\n else\n puts \"NO\"\n end\nend"}, {"source_code": "# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n\ngl = gets.strip\npt = gets.strip.gsub(\"?\",\"[\" + gl + \"]\").gsub(\"*\", \".?\")\nrg = Regexp.compile(\"^\" + pt + \"$\", Regexp::IGNORECASE)\nn = gets.to_i\nn.times do\n s = gets.strip\n if s =~ rg\n puts \"YES\"\n else\n puts \"NO\"\n end\nend\n\n"}], "src_uid": "c6633581d7424d670eaa0f8a5c8cc366"} {"nl": {"description": "You are given an array $$$a$$$ consisting of $$$n$$$ integer numbers.You have to color this array in $$$k$$$ colors in such a way that: Each element of the array should be colored in some color; For each $$$i$$$ from $$$1$$$ to $$$k$$$ there should be at least one element colored in the $$$i$$$-th color in the array; For each $$$i$$$ from $$$1$$$ to $$$k$$$ all elements colored in the $$$i$$$-th color should be distinct. Obviously, such coloring might be impossible. In this case, print \"NO\". Otherwise print \"YES\" and any coloring (i.e. numbers $$$c_1, c_2, \\dots c_n$$$, where $$$1 \\le c_i \\le k$$$ and $$$c_i$$$ is the color of the $$$i$$$-th element of the given array) satisfying the conditions above. If there are multiple answers, you can print any.", "input_spec": "The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le k \\le n \\le 5000$$$) — the length of the array $$$a$$$ and the number of colors, respectively. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 5000$$$) — elements of the array $$$a$$$.", "output_spec": "If there is no answer, print \"NO\". Otherwise print \"YES\" and any coloring (i.e. numbers $$$c_1, c_2, \\dots c_n$$$, where $$$1 \\le c_i \\le k$$$ and $$$c_i$$$ is the color of the $$$i$$$-th element of the given array) satisfying the conditions described in the problem statement. If there are multiple answers, you can print any.", "sample_inputs": ["4 2\n1 2 2 3", "5 2\n3 2 1 2 3", "5 2\n2 1 1 2 1"], "sample_outputs": ["YES\n1 1 2 2", "YES\n2 1 1 2 1", "NO"], "notes": "NoteIn the first example the answer $$$2~ 1~ 2~ 1$$$ is also acceptable.In the second example the answer $$$1~ 1~ 1~ 2~ 2$$$ is also acceptable.There exist other acceptable answers for both examples."}, "positive_code": [{"source_code": "n, k = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\nb = Array.new(5005) { [] }\na.each_with_index { |x, i| b[x] << i }\n\nc = 1\nans = [-1] * n\nb.each do |pos|\n\tputs('NO') + exit if pos.size > k\n\tpos.each do |i|\n\t\tans[i] = c\n\t\tc = c < k ? c + 1 : 1\n\tend\nend\nputs 'YES', ans.join(' ')\n"}, {"source_code": "input = STDIN.read.split(\"\\n\").map{|l| l.split(\" \").map{|a| a.to_i}}\nfirst = input.shift\nARR = input.shift\nN = first[0]\nCOLORS = first[1]\n\nNUM_USED_COLORS = {}\nremained_color_i = 0\n\ndic = {}\nis_no = false\n\ncolored_arr = []\n\nfor i in ARR\n if !NUM_USED_COLORS.key?(i)\n NUM_USED_COLORS[i] = {}\n end\n\n if remained_color_i < COLORS\n NUM_USED_COLORS[i][remained_color_i] = true\n colored_arr.push(remained_color_i)\n remained_color_i = remained_color_i + 1\n else\n found = false\n for j in 0..(COLORS - 1)\n if !NUM_USED_COLORS[i].key?(j)\n NUM_USED_COLORS[i][j] = true\n colored_arr.push(j)\n found = true\n break\n end\n end\n if found == false\n is_no = true\n end\n end\n if is_no == true\n break\n end\nend\n\nif is_no\n puts 'NO'\nelse\n puts 'YES'\n puts colored_arr.map{|i| i + 1}.join(' ')\nend"}, {"source_code": "# Input\nn, k = gets.split(\" \").map { |e| e.to_i }\na = gets.split(\" \").map { |e| e }\n\n# Processing\ncount = Hash.new(0)\na.each { |n| count[n] += 1 }\n\ncurrent_color = Hash.new(count)\ni = 1\ncount.each do |n, c|\n if c > k\n puts \"NO\"\n #puts \"#{n} amount > k\"\n exit\n end\n current_color[n] = i\n i = (i + c - 1) % k + 1\n end\n\nc = Array.new(n) do |i|\n #puts current_color\n color = current_color[a[i]]\n current_color[a[i]] = current_color[a[i]] % k + 1\n color\nend\n\n# Output\nputs \"YES\"\nputs c.join(\" \")\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\narr = gets.split.map(&:to_i)\nh = Hash.new(0)\narr.each do |e|\n h[e] += 1\nend\nh.values.each do |v|\n if v > k\n puts \"NO\"\n exit\n end\nend\ncur_c = 0\nprev_cur_c = 0\nf = false\nres = [0] * arr.size\nmp = Array.new(k) {Array.new}\narr.each_with_index do |e, ind|\n while mp[cur_c].include?(e)\n if cur_c == prev_cur_c\n puts \"NO\"\n exit\n end\n cur_c = (cur_c + 1) % k\n end\n mp[cur_c] << e\n res[ind] = cur_c\n prev_cur_c = cur_c\n cur_c = (cur_c + 1) % k\nend\nputs \"YES\"\nputs res.map{|x| x + 1}.join(\" \")"}, {"source_code": "N,K = gets.split.map(&:to_i)\nns = {}\nis = []\na = gets.split.map(&:to_i)\na.each_with_index{ |x,i| ns[x] = (ns[x] || 0) + 1; is << [x,i] }\nif ns.values.max > K || N < K\n puts \"NO\"; exit\nend\nc = -1\nis.sort!\n#p is\nis.each{ |x,i|\n a[i] = (c += 1) % K + 1\n}\nputs \"YES\", a.join(\" \")"}], "negative_code": [{"source_code": "n, k = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\nb = Array.new(10) { [] }\na.each_with_index { |x, i| b[x] << i }\n\nc = 1\nans = [-1] * n\nb.each do |pos|\n\tputs('NO') + exit if pos.size > k\n\tpos.each do |i|\n\t\tans[i] = c\n\t\tc = c < k ? c + 1 : 1\n\tend\nend\nputs 'Yes', ans.join(' ')\n"}, {"source_code": "# Input\nn, k = gets.split(\" \").map { |e| e.to_i }\na = gets.split(\" \").map { |e| e }\n\n# Processing\ncount = Hash.new(0)\ncurrent_color = Hash.new\n\na.each do |n|\n count[n] += 1\nend\n\nif count.length < k\n puts \"NO\"\n exit\nend\ncount.each do |n, amount|\n if amount > k\n puts \"NO\"\n exit\n end\n current_color[n] = (current_color.length % k) + 1\nend\n\nc = Array.new(n) do |i|\n #puts current_color\n color = current_color[a[i]]\n current_color[a[i]] = current_color[a[i]] % k + 1\n color\nend\n\n# Output\nputs \"YES\"\nputs c.join(\" \")\n"}, {"source_code": "# Input\nn, k = gets.split(\" \").map { |e| e.to_i }\na = gets.split(\" \").map { |e| e }\n\n# Processing\ncount = Hash.new(0)\na.each { |n| count[n] += 1 }\n\ncurrent_color = Hash.new(count)\ni = 1\ncount.each do |n, c|\n current_color[n] = i\n i = (i + c) % k + 1\n end\n\nc = Array.new(n) do |i|\n #puts current_color\n color = current_color[a[i]]\n current_color[a[i]] = current_color[a[i]] % k + 1\n color\nend\n\n# Output\nputs \"YES\"\nputs c.join(\" \")\n"}, {"source_code": "# Input\nn, k = gets.split(\" \").map { |e| e.to_i }\na = gets.split(\" \").map { |e| e }\n\n# Processing\ncount = Hash.new(0)\na.each { |n| count[n] += 1 }\n\ncurrent_color = Hash.new(count)\ni = 1\ncount.each do |n, c|\n if c > k\n puts \"NO\"\n #puts \"#{n} amount > k\"\n exit\n end\n current_color[n] = i\n i = (i + c) % k + 1\n end\n\nc = Array.new(n) do |i|\n #puts current_color\n color = current_color[a[i]]\n current_color[a[i]] = current_color[a[i]] % k + 1\n color\nend\n\n# Output\nputs \"YES\"\nputs c.join(\" \")\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\narr = gets.split.map(&:to_i)\nh = Hash.new(0)\narr.each do |e|\n h[e] += 1\nend\nh.values.each do |v|\n if v > k\n puts \"NO\"\n exit\n end\nend\ncur_c = 0\nprev_cur_c = 0\nf = false\nres = [0] * arr.size\nmp = Array.new(k) {Array.new}\narr.each_with_index do |e, ind|\n while mp[cur_c].include?(e)\n cur_c += 1\n if cur_c == prev_cur_c\n puts \"NO\"\n exit\n end\n end\n mp[cur_c] << e\n res[ind] = cur_c\nend\nputs \"YES\"\nputs res.map{|x| x + 1}.join(\" \")"}, {"source_code": "N,K = gets.split.map(&:to_i)\nns = {}\nA = gets.split.map(&:to_i)\nA.each{ |x| ns[x] = (ns[x] || 0) + 1 }\nif ns.values.max > K\n puts \"NO\"; exit\nend\nputs \"YES\", A.map{ |x| (ns[x] -= 1) + 1 }.join(\" \")"}], "src_uid": "3d4df21eebf32ce15841179bb85e6f2f"} {"nl": {"description": "In the $$$2022$$$ year, Mike found two binary integers $$$a$$$ and $$$b$$$ of length $$$n$$$ (both of them are written only by digits $$$0$$$ and $$$1$$$) that can have leading zeroes. In order not to forget them, he wanted to construct integer $$$d$$$ in the following way: he creates an integer $$$c$$$ as a result of bitwise summing of $$$a$$$ and $$$b$$$ without transferring carry, so $$$c$$$ may have one or more $$$2$$$-s. For example, the result of bitwise summing of $$$0110$$$ and $$$1101$$$ is $$$1211$$$ or the sum of $$$011000$$$ and $$$011000$$$ is $$$022000$$$; after that Mike replaces equal consecutive digits in $$$c$$$ by one digit, thus getting $$$d$$$. In the cases above after this operation, $$$1211$$$ becomes $$$121$$$ and $$$022000$$$ becomes $$$020$$$ (so, $$$d$$$ won't have equal consecutive digits). Unfortunately, Mike lost integer $$$a$$$ before he could calculate $$$d$$$ himself. Now, to cheer him up, you want to find any binary integer $$$a$$$ of length $$$n$$$ such that $$$d$$$ will be maximum possible as integer.Maximum possible as integer means that $$$102 > 21$$$, $$$012 < 101$$$, $$$021 = 21$$$ and so on.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 1000$$$) — the number of test cases. The first line of each test case contains the integer $$$n$$$ ($$$1 \\leq n \\leq 10^5$$$) — the length of $$$a$$$ and $$$b$$$. The second line of each test case contains binary integer $$$b$$$ of length $$$n$$$. The integer $$$b$$$ consists only of digits $$$0$$$ and $$$1$$$. It is guaranteed that the total sum of $$$n$$$ over all $$$t$$$ test cases doesn't exceed $$$10^5$$$.", "output_spec": "For each test case output one binary integer $$$a$$$ of length $$$n$$$. Note, that $$$a$$$ or $$$b$$$ may have leading zeroes but must have the same length $$$n$$$.", "sample_inputs": ["5\n1\n0\n3\n011\n3\n110\n6\n111000\n6\n001011"], "sample_outputs": ["1\n110\n100\n101101\n101110"], "notes": "NoteIn the first test case, $$$b = 0$$$ and choosing $$$a = 1$$$ gives $$$d = 1$$$ as a result.In the second test case, $$$b = 011$$$ so: if you choose $$$a = 000$$$, $$$c$$$ will be equal to $$$011$$$, so $$$d = 01$$$; if you choose $$$a = 111$$$, $$$c$$$ will be equal to $$$122$$$, so $$$d = 12$$$; if you choose $$$a = 010$$$, you'll get $$$d = 021$$$. If you select $$$a = 110$$$, you'll get $$$d = 121$$$. We can show that answer $$$a = 110$$$ is optimal and $$$d = 121$$$ is maximum possible.In the third test case, $$$b = 110$$$. If you choose $$$a = 100$$$, you'll get $$$d = 210$$$ and it's the maximum possible $$$d$$$.In the fourth test case, $$$b = 111000$$$. If you choose $$$a = 101101$$$, you'll get $$$d = 212101$$$ and it's maximum possible $$$d$$$.In the fifth test case, $$$b = 001011$$$. If you choose $$$a = 101110$$$, you'll get $$$d = 102121$$$ and it's maximum possible $$$d$$$."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n n=gets.to_i\r\n b=gets.chomp.chars\r\n a=\"\"\r\n d=0\r\n b.each do |bb|\r\n if bb==\"1\"\r\n if d==2\r\n a << \"0\"\r\n d=1\r\n elsif d==1\r\n a << \"1\"\r\n d=2\r\n else\r\n a << \"1\"\r\n d=2\r\n end\r\n else\r\n if d==2\r\n a << \"1\"\r\n d=1\r\n elsif d==1\r\n a << \"0\"\r\n d=0\r\n else\r\n a << \"1\"\r\n d=1\r\n end\r\n end\r\n end\r\n puts a\r\nend\r\n"}], "negative_code": [], "src_uid": "e27620d3a43ab42edf930b37ce214c9e"} {"nl": {"description": "You are given three integers $$$n$$$, $$$d$$$ and $$$k$$$.Your task is to construct an undirected tree on $$$n$$$ vertices with diameter $$$d$$$ and degree of each vertex at most $$$k$$$, or say that it is impossible.An undirected tree is a connected undirected graph with $$$n - 1$$$ edges.Diameter of a tree is the maximum length of a simple path (a path in which each vertex appears at most once) between all pairs of vertices of this tree.Degree of a vertex is the number of edges incident to this vertex (i.e. for a vertex $$$u$$$ it is the number of edges $$$(u, v)$$$ that belong to the tree, where $$$v$$$ is any other vertex of a tree).", "input_spec": "The first line of the input contains three integers $$$n$$$, $$$d$$$ and $$$k$$$ ($$$1 \\le n, d, k \\le 4 \\cdot 10^5$$$).", "output_spec": "If there is no tree satisfying the conditions above, print only one word \"NO\" (without quotes). Otherwise in the first line print \"YES\" (without quotes), and then print $$$n - 1$$$ lines describing edges of a tree satisfying the conditions above. Vertices of the tree must be numbered from $$$1$$$ to $$$n$$$. You can print edges and vertices connected by an edge in any order. If there are multiple answers, print any of them.1", "sample_inputs": ["6 3 3", "6 2 3", "10 4 3", "8 5 3"], "sample_outputs": ["YES\n3 1\n4 1\n1 2\n5 2\n2 6", "NO", "YES\n2 9\n2 10\n10 3\n3 1\n6 10\n8 2\n4 3\n5 6\n6 7", "YES\n2 5\n7 2\n3 7\n3 1\n1 6\n8 7\n4 3"], "notes": null}, "positive_code": [{"source_code": "f = 1 \nas = gets.split.map(&:to_i)\nn = as[0]\nd = as[1]\nk = as[2]\ntr = Array.new(n+5)\ni = 0\nwhile i<=n\n tr[i] = Array.new\n i+=1\nend\n\nnode = 0\nfor i in 1..d\n if i>=n\n f = 0\n break\n end\n tr[i].push(i+1)\n tr[i+1].push(i)\n node = i+1\n if tr[i].size>k || tr[node].size>k\n f = 0\n break\n end\nend\n\ndep = Array.new(n+5)\nvis = Array.new(n+5)\no = 0\nwhile o<=n\n dep[o] = 0\n vis[o] = 0\n o+=1\nend\ncen1 = 0\ncen2 = 0\nmx_dep = 0\nif node%2==1\n cen1 = (node+1)/2\n mx_dep = cen1-1\nelse \n cen1 = node/2\n cen2 = cen1+1\n mx_dep = cen1-1\nend\ndep[cen1] = 0\ndep[cen2] = 0\nvis[cen1] = 1\nvis[cen2] = 1\nc = 1\nwhile node0\n c+=1\n q=Array.new\n if cen1 > 0\n q.push(cen1)\n cen1 = 0\n elsif cen2>0 \n q.push(cen2)\n cen2 = 0\n end\n \n while q.length > 0\n a = q.first\n q.shift\n if tr[a].size>k\n f = 0\n break\n end\n while tr[a].length0\n next\n end\n q.push(ii)\n vis[ii] = 1\n dep[ii] = dep[a] + 1\n end\n end\n\nend\n\nif f==0 || node!=n\n puts \"NO\"\nelse \n puts \"YES\"\n for z in (1..n) \n vis[z] = 0\n end \n for j in (1..n)\n for z in tr[j]\n if vis[z]>0 && vis[j]>0\n next\n end\n vis[z] = 1\n vis[j] = 1\n puts \"#{j} #{z}\"\n end\n end \nend\n=begin\n=end"}, {"source_code": "f = true \nas = gets.split.map(&:to_i)\nn = as[0]\nd = as[1]\nk = as[2]\ntr = Array.new(n+5)\ni = 0\nwhile i<=n\n tr[i] = Array.new\n i+=1\nend\nnode = 0\nfor i in 1..d\n if i>=n\n f = false\n break\n end\n tr[i].push(i+1)\n tr[i+1].push(i)\n node = i+1\n if tr[i].size>k || tr[node].size>k\n f = false\n break\n end\nend\ndep = Array.new(n+5)\nvis = Array.new(n+5)\no = 0\nwhile o<=n\n dep[o] = 0\n vis[o] = 0\n o+=1\nend\ncen1 = 0\ncen2 = 0\nmx_dep = 0\nif node%2==1\n cen1 = (node+1)/2\n mx_dep = cen1-1\nelse \n cen1 = node/2\n cen2 = cen1+1\n mx_dep = cen1-1\nend\ndep[cen1] = 0\ndep[cen2] = 0\nvis[cen1] = 1\nvis[cen2] = 1\nc = 1\nwhile node 0\n q.push(cen1)\n cen1 = 0\n elsif cen2>0 \n q.push(cen2)\n cen2 = 0\n end \n while q.length > 0\n a = q.first\n q.shift\n if tr[a].size>k\n f = false\n break\n end\n while tr[a].length0\n next\n end\n q.push(ii)\n vis[ii] = 1\n dep[ii] = dep[a] + 1\n end\n end\n\nend\nif !f || node!=n\n puts \"NO\"\nelse \n puts \"YES\"\n for z in (1..n) \n vis[z] = 0\n end \n for j in (1..n)\n for z in tr[j]\n if vis[z]>0 && vis[j]>0\n next\n end\n vis[z] = 1\n vis[j] = 1\n puts \"#{j} #{z}\"\n end\n end \nend"}, {"source_code": "f = true \nas = gets.split.map(&:to_i)\nn = as[0]\nd = as[1]\nk = as[2]\ntr = Array.new(n+5)\ni = 0\nwhile i<=n\n tr[i] = Array.new\n i+=1\nend\n\nnode = 0\nfor i in 1..d\n if i>=n\n f = false\n break\n end\n tr[i].push(i+1)\n tr[i+1].push(i)\n node = i+1\n if tr[i].size>k || tr[node].size>k\n f = false\n break\n end\nend\n\ndep = Array.new(n+5)\nvis = Array.new(n+5)\no = 0\nwhile o<=n\n dep[o] = 0\n vis[o] = 0\n o+=1\nend\ncen1 = 0\ncen2 = 0\nmx_dep = 0\nif node%2==1\n cen1 = (node+1)/2\n mx_dep = cen1-1\nelse \n cen1 = node/2\n cen2 = cen1+1\n mx_dep = cen1-1\nend\ndep[cen1] = 0\ndep[cen2] = 0\nvis[cen1] = 1\nvis[cen2] = 1\nc = 1\nwhile node 0\n q.push(cen1)\n cen1 = 0\n elsif cen2>0 \n q.push(cen2)\n cen2 = 0\n end\n \n while q.length > 0\n a = q.first\n q.shift\n if tr[a].size>k\n f = false\n break\n end\n while tr[a].length0\n next\n end\n q.push(ii)\n vis[ii] = 1\n dep[ii] = dep[a] + 1\n end\n end\n\nend\n\nif !f || node!=n\n puts \"NO\"\nelse \n puts \"YES\"\n for z in (1..n) \n vis[z] = 0\n end \n for j in (1..n)\n for z in tr[j]\n if vis[z]>0 && vis[j]>0\n next\n end\n vis[z] = 1\n vis[j] = 1\n puts \"#{j} #{z}\"\n end\n end \nend\n=begin\n=end"}], "negative_code": [{"source_code": "f = true \nas = gets.split.map(&:to_i)\nn = as[0]\nd = as[1]\nk = as[2]\ntr = Array.new(n+5)\ni = 0\nwhile i<=n\n tr[i] = Array.new\n i+=1\nend\n\nnode = 0\nfor i in 1..d\n if i>=n\n f = false\n break\n end\n tr[i].push(i+1)\n tr[i+1].push(i)\n node = i+1\nend\n\ndep = Array.new(n+5)\nvis = Array.new(n+5)\no = 0\nwhile o<=n\n dep[o] = 0\n vis[o] = 0\n o+=1\nend\ncen1 = 0\ncen2 = 0\nmx_dep = 0\nif node%2==1\n cen1 = (node+1)/2\n mx_dep = cen1-1\nelse \n cen1 = node/2\n cen2 = cen1+1\n mx_dep = cen1-1\nend\ndep[cen1] = 0\ndep[cen2] = 0\nvis[cen1] = 1\nvis[cen2] = 1\nc = 1\nwhile node 0\n q.push(cen1)\n cen1 = 0\n elsif cen2>0 \n q.push(cen2)\n cen2 = 0\n end\n \n while q.length > 0\n a = q.first\n q.shift\n while tr[a].length0\n next\n end\n q.push(ii)\n vis[ii] = 1\n dep[ii] = dep[a] + 1\n end\n end\n\nend\n\nfor z in (1..n) \n vis[z] = 0\nend \n\nif !f || node!=n\n puts \"NO\"\nelse \n puts \"YES\"\n for j in (1..n)\n for z in tr[j]\n if vis[z]>0 && vis[j]>0\n next\n end\n vis[z] = 1\n vis[j] = 1\n puts \"#{j} #{z}\"\n end\n end \nend\n=begin\n=end"}, {"source_code": "f = true \nas = gets.split.map(&:to_i)\nn = as[0]\nd = as[1]\nk = as[2]\ntr = Array.new(n+5)\ni = 0\nwhile i<=n\n tr[i] = Array.new\n i+=1\nend\n\nnode = 0\nfor i in 1..d\n if i>=n\n f = false\n break\n end\n tr[i].push(i+1)\n tr[i+1].push(i)\n node = i+1\nend\n\ndep = Array.new(n+5)\nvis = Array.new(n+5)\no = 0\nwhile o<=n\n dep[o] = 0\n vis[o] = 0\n o+=1\nend\ncen1 = 0\ncen2 = 0\nmx_dep = 0\nif node%2==1\n cen1 = (node+1)/2\n mx_dep = cen1-1\nelse \n cen1 = node/2\n cen2 = cen1+1\n mx_dep = cen1-1\nend\ndep[cen1] = 0\ndep[cen2] = 0\nvis[cen1] = 1\nvis[cen2] = 1\nc = 1\nwhile node 0\n q.push(cen1)\n cen1 = 0\n elsif cen2>0 \n q.push(cen2)\n cen2 = 0\n end\n \n while q.length > 0\n a = q.first\n q.shift\n if tr[a].length>k\n f=0\n end\n while tr[a].length0\n next\n end\n q.push(ii)\n vis[ii] = 1\n dep[ii] = dep[a] + 1\n end\n end\n\nend\n\nfor z in (1..n) \n vis[z] = 0\nend \n\nif !f || node!=n\n puts \"NO\"\nelse \n puts \"YES\"\n for j in (1..n)\n for z in tr[j]\n if vis[z]>0 && vis[j]>0\n next\n end\n vis[z] = 1\n vis[j] = 1\n puts \"#{j} #{z}\"\n end\n end \nend\n=begin\n=end"}], "src_uid": "a4849505bca48b408a5e8fb5aebf5cb6"} {"nl": {"description": "You are given an array $$$a[0 \\ldots n-1]$$$ of length $$$n$$$ which consists of non-negative integers. Note that array indices start from zero.An array is called good if the parity of each index matches the parity of the element at that index. More formally, an array is good if for all $$$i$$$ ($$$0 \\le i \\le n - 1$$$) the equality $$$i \\bmod 2 = a[i] \\bmod 2$$$ holds, where $$$x \\bmod 2$$$ is the remainder of dividing $$$x$$$ by 2.For example, the arrays [$$$0, 5, 2, 1$$$] and [$$$0, 17, 0, 3$$$] are good, and the array [$$$2, 4, 6, 7$$$] is bad, because for $$$i=1$$$, the parities of $$$i$$$ and $$$a[i]$$$ are different: $$$i \\bmod 2 = 1 \\bmod 2 = 1$$$, but $$$a[i] \\bmod 2 = 4 \\bmod 2 = 0$$$.In one move, you can take any two elements of the array and swap them (these elements are not necessarily adjacent).Find the minimum number of moves in which you can make the array $$$a$$$ good, or say that this is not possible.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases in the test. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 \\le n \\le 40$$$) — the length of the array $$$a$$$. The next line contains $$$n$$$ integers $$$a_0, a_1, \\ldots, a_{n-1}$$$ ($$$0 \\le a_i \\le 1000$$$) — the initial array.", "output_spec": "For each test case, output a single integer — the minimum number of moves to make the given array $$$a$$$ good, or -1 if this is not possible.", "sample_inputs": ["4\n4\n3 2 7 6\n3\n3 2 6\n1\n7\n7\n4 9 2 1 18 3 0"], "sample_outputs": ["2\n1\n-1\n0"], "notes": "NoteIn the first test case, in the first move, you can swap the elements with indices $$$0$$$ and $$$1$$$, and in the second move, you can swap the elements with indices $$$2$$$ and $$$3$$$.In the second test case, in the first move, you need to swap the elements with indices $$$0$$$ and $$$1$$$.In the third test case, you cannot make the array good."}, "positive_code": [{"source_code": "gets.to_i.times do\n\tn = gets.to_i\n\ta = gets.split.map(&:to_i)\n\tb = (0...n).map { _1 % 2 }\n\tif [0, 1].all? { |k| a.count { _1 % 2 == k } == b.count { _1 % 2 == k } }\n\t\tputs (0...n).count { a[_1] % 2 != b[_1] % 2 } / 2\n\telse\n\t\tputs -1\n\tend\nend\n"}, {"source_code": "t = gets.to_i\nt.times do\n\tn = gets.to_i\n\till_evens = 0\n\till_odds = 0\n\todds = []\n\tgets.split.each_with_index do |w, i|\n\t\tif w.to_i.even?\n\t\t\till_evens += 1 if i.odd?\n\t\telse\n\t\t\till_odds += 1 if i.even?\n\t\tend\n\tend\n\n\tif ill_evens != ill_odds\n\t\tp -1\n\t\tnext\n\tend\n\n\tp ill_evens\n\t\nend\n"}, {"source_code": "def count_arr(a,n)\n\n odds = 0\n evens = 0\n i=0\n\n while i= arr[1])\n puts -1\n else\n puts arr[2]\n end\n end\nend\n"}, {"source_code": "\ngets.chomp.to_i.times do\n\n n = gets.chomp.to_i\n\n a = gets.chomp.split(\" \").map { |e| e.to_i }\n\n i=0\n count =0\n while i= 0\n res += [sopen, x].min\n sopen -= x\n res += 1 if mopen == 0 && j != i + 1\n break if sopen < 0\n end\n else\n mopen += a[j]\n end\n #puts \"i: #{i}, j: #{j}, sopen: #{sopen}, mopen: #{mopen}, res: #{res}\"\n end\nend\nputs res\n"}], "negative_code": [{"source_code": "n = gets.to_i\na = gets.split.map {|i| i.to_i}\nres = 0\nmopen = 0\nsopen = 0\nt, x = 0, 0\n#each non starting must have a closing\n#each non ending must have an opening\nfor i in 0...a.length\n next if i.odd?\n sopen = a[i]\n for j in (i + 1)...a.length\n if j.odd?\n #optimize\n t = [mopen,x = a[j]].min\n mopen -= t\n x -= t\n if x >= 0\n res += [sopen, x].min\n sopen -= x\n res += 1 if mopen == 0 && j != i + 1\n break if sopen < 0\n end\n else\n mopen += a[j]\n end\n #puts \"i: #{i}, j: #{j}, sopen: #{sopen}, mopen: #{mopen}, res: #{res}\"\n end\nend\nputs res\n"}], "src_uid": "ca4ae2484800a98b5592ae65cd45b67f"} {"nl": {"description": "A string is called beautiful if no two consecutive characters are equal. For example, \"ababcb\", \"a\" and \"abab\" are beautiful strings, while \"aaaaaa\", \"abaa\" and \"bb\" are not.Ahcl wants to construct a beautiful string. He has a string $$$s$$$, consisting of only characters 'a', 'b', 'c' and '?'. Ahcl needs to replace each character '?' with one of the three characters 'a', 'b' or 'c', such that the resulting string is beautiful. Please help him!More formally, after replacing all characters '?', the condition $$$s_i \\neq s_{i+1}$$$ should be satisfied for all $$$1 \\leq i \\leq |s| - 1$$$, where $$$|s|$$$ is the length of the string $$$s$$$.", "input_spec": "The first line contains positive integer $$$t$$$ ($$$1 \\leq t \\leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contain the descriptions of test cases. Each line contains a non-empty string $$$s$$$ consisting of only characters 'a', 'b', 'c' and '?'. It is guaranteed that in each test case a string $$$s$$$ has at least one character '?'. The sum of lengths of strings $$$s$$$ in all test cases does not exceed $$$10^5$$$.", "output_spec": "For each test case given in the input print the answer in the following format: If it is impossible to create a beautiful string, print \"-1\" (without quotes); Otherwise, print the resulting beautiful string after replacing all '?' characters. If there are multiple answers, you can print any of them. ", "sample_inputs": ["3\na???cb\na??bbc\na?b?c"], "sample_outputs": ["ababcb\n-1\nacbac"], "notes": "NoteIn the first test case, all possible correct answers are \"ababcb\", \"abcacb\", \"abcbcb\", \"acabcb\" and \"acbacb\". The two answers \"abcbab\" and \"abaabc\" are incorrect, because you can replace only '?' characters and the resulting string must be beautiful.In the second test case, it is impossible to create a beautiful string, because the $$$4$$$-th and $$$5$$$-th characters will be always equal.In the third test case, the only answer is \"acbac\"."}, "positive_code": [{"source_code": "ans = []\nst = Time.now\nt = [\"a\",\"b\",\"c\"]\nn = gets.to_i\n \nn.times do|d_i|\n s = gets.split(\"\")\n \n (s.size - 1).times do |i|\n if s[i] == \"?\"\n t.each do |drf|\n next if drf == s[i-1]\n next if drf == s[i+1]\n s[i] = drf\n break\n end\n else\n if s[i] == s[i+1]\n s = [-1]\n break\n end\n end\n end\n ans.push(s.join)\nend\nputs ans"}, {"source_code": "gets.strip.to_i.times {\n x = gets.strip.split('')\n if x.size == 1\n puts x[0] == \"?\" ? \"a\" : x[0]\n next\n end\n c = [\"a\", \"b\", \"c\"]\n x.each_with_index { |element, index|\n if index == 0\n if x[index] == \"?\"\n if x[index + 1] == \"?\"\n x[index] = \"a\"\n next\n end\n x[index] = c[(c.find_index(x[index + 1]) + 1) % 3]\n end\n elsif index == x.size - 1\n if x[index] == \"?\"\n x[index] = c[(c.find_index(x[index - 1]) + 1) % 3]\n end\n else\n if x[index] == \"?\"\n if x[index + 1] == \"?\"\n x[index] = c[(c.find_index(x[index - 1]) + 1) % 3]\n else\n x[index] = \"c\" if x[index - 1] == \"a\" && x[index + 1] == \"b\" || x[index - 1] == \"b\" && x[index + 1] == \"a\"\n x[index] = \"a\" if x[index - 1] == \"b\" && x[index + 1] == \"c\" || x[index - 1] == \"c\" && x[index + 1] == \"b\"\n x[index] = \"b\" if x[index - 1] == \"c\" && x[index + 1] == \"a\" || x[index - 1] == \"a\" && x[index + 1] == \"c\"\n x[index] = c[(c.find_index(x[index - 1]) + 1) % 3] if x[index - 1] == x[index + 1]\n end\n end\n end\n }\n flg = false\n x.each_with_index { |element, index|\n next if index == 0\n if x[index] == x[index - 1]\n flg = true\n break\n end\n }\n puts flg ? -1 : x.join\n}"}], "negative_code": [{"source_code": "ans = []\nst = Time.now\nt = [\"a\",\"b\",\"c\"]\nn = gets.to_i\n \nn.times do|d_i|\n s = gets.split(\"\")\n \n (s.size - 1).times do |i|\n if s[i] == \"?\"\n t.each do |drf|\n next if drf == s[i-1]\n next if drf == s[i+1]\n s[i] = drf\n break\n end\n else\n if s[i] == s[i+1]\n s = [-1]\n break\n end\n end\n end\n if(n == 1)\n puts Time.now - st\n exit\n end\n ans.push(s.join)\nend\nputs ans"}, {"source_code": "n = gets.to_i\nn.times do\n p s = gets\n ns = s.size\n if s[0] == \"?\"\n (\"a\"..\"c\").each do |cc|\n s[0] = cc if cc != s[1]\n end\n else\n if s[0] == s[1]\n p -1\n next\n end\n end\n\n (1...(ns-1)).each do |i|\n c = s[i]\n if c == \"?\"\n (\"a\"..\"c\").each do |cc|\n if cc != s[i-1]\n if cc != s[i+1]\n s[i] = cc\n break\n else\n next\n end\n end\n end\n else\n if s[i] == s[i+1]\n s = \"-1\"\n break\n end\n end\n end\n puts s\nend"}, {"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\nrequire 'prime'\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\ndef big(a,b) return (a>b)? a:b end\n\ndef small(a,b) return (a'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\nrequire 'prime'\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\ndef big(a,b) return (a>b)? a:b end\n\ndef small(a,b) return (a 0.8)\n puts 12\n end\n ans.push(s)\nend\nputs ans"}], "src_uid": "98c08a3b5e5b5bb78804ff797ba24d87"} {"nl": {"description": "One day Vasya was on a physics practical, performing the task on measuring the capacitance. He followed the teacher's advice and did as much as n measurements, and recorded the results in the notebook. After that he was about to show the results to the teacher, but he remembered that at the last lesson, the teacher had made his friend Petya redo the experiment because the largest and the smallest results differed by more than two times. Vasya is lazy, and he does not want to redo the experiment. He wants to do the task and go home play computer games. So he decided to cheat: before Vasya shows the measurements to the teacher, he will erase some of them, so as to make the largest and the smallest results of the remaining measurements differ in no more than two times. In other words, if the remaining measurements have the smallest result x, and the largest result y, then the inequality y ≤ 2·x must fulfill. Of course, to avoid the teacher's suspicion, Vasya wants to remove as few measurement results as possible from his notes.Help Vasya, find what minimum number of measurement results he will have to erase from his notes so that the largest and the smallest of the remaining results of the measurements differed in no more than two times.", "input_spec": "The first line contains integer n (2 ≤ n ≤ 105) — the number of measurements Vasya made. The second line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ 5000) — the results of the measurements. The numbers on the second line are separated by single spaces.", "output_spec": "Print a single integer — the minimum number of results Vasya will have to remove.", "sample_inputs": ["6\n4 5 3 8 3 7", "4\n4 3 2 4"], "sample_outputs": ["2", "0"], "notes": "NoteIn the first sample you can remove the fourth and the sixth measurement results (values 8 and 7). Then the maximum of the remaining values will be 5, and the minimum one will be 3. Or else, you can remove the third and fifth results (both equal 3). After that the largest remaining result will be 8, and the smallest one will be 4."}, "positive_code": [{"source_code": "$stdin = File.open(\"input.txt\")\n$stdout = File.open(\"output.txt\",\"w\")\n\nn = gets.chomp.to_i\nH = {}\ngets.chomp.split.each do |c|\n\tkey = c.to_i\n\tif H.has_key? key\n\t\tH[key] += 1\n\telse\n\t\tH[key] = 1\n\tend\nend\nA = Array.new(5000,0)\nfor c in 1..5000 do\n\tA[c] = A[c-1] + (H.has_key?(c) ? H[c] : 0)\nend\nsol = 99999\nfor c in 1..2500 do\n\tsol = [sol , A[c-1] + (A[5000] - A[c*2])].min\nend\nputs sol"}, {"source_code": "# -*- encoding: utf-8 -*-\n\n$stdin = File.open(\"input.txt\")\n$stdout = File.open(\"output.txt\", \"w\")\n\nn = gets.chomp.to_i\nc = {}\ngets.chomp.split(\" \").each do |n|\n key = n.to_i\n if c.has_key? key\n c[key] += 1\n else\n c[key] = 1\n end\nend\n\nfield = Array.new(5000, 0)\n\nfor i in 1 .. 5000 do\n field[i] = field[i - 1] + (c.has_key?(i) ? c[i] : 0)\nend\nans = 99999\nfor i in 1 .. 2500 do\n ans = [ans, field[i - 1] + (field[5000] - field[i * 2])].min\nend\n\nputs ans\n "}], "negative_code": [], "src_uid": "80cf3ea119fd398e8f003d22a76895a7"} {"nl": {"description": "Timur has a stairway with $$$n$$$ steps. The $$$i$$$-th step is $$$a_i$$$ meters higher than its predecessor. The first step is $$$a_1$$$ meters higher than the ground, and the ground starts at $$$0$$$ meters. The stairs for the first test case. Timur has $$$q$$$ questions, each denoted by an integer $$$k_1, \\dots, k_q$$$. For each question $$$k_i$$$, you have to print the maximum possible height Timur can achieve by climbing the steps if his legs are of length $$$k_i$$$. Timur can only climb the $$$j$$$-th step if his legs are of length at least $$$a_j$$$. In other words, $$$k_i \\geq a_j$$$ for each step $$$j$$$ climbed.Note that you should answer each question independently.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 100$$$) — the number of test cases. The first line of each test case contains two integers $$$n, q$$$ ($$$1 \\leq n, q \\leq 2\\cdot10^5$$$) — the number of steps and the number of questions, respectively. The second line of each test case contains $$$n$$$ integers ($$$1 \\leq a_i \\leq 10^9$$$) — the height of the steps. The third line of each test case contains $$$q$$$ integers ($$$0 \\leq k_i \\leq 10^9$$$) — the numbers for each question. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2\\cdot10^5$$$, and the sum of $$$q$$$ does not exceed $$$2\\cdot10^5$$$.", "output_spec": "For each test case, output a single line containing $$$q$$$ integers, the answer for each question. Please note, that the answer for some questions won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).", "sample_inputs": ["3\n\n4 5\n\n1 2 1 5\n\n1 2 4 9 10\n\n2 2\n\n1 1\n\n0 1\n\n3 1\n\n1000000000 1000000000 1000000000\n\n1000000000"], "sample_outputs": ["1 4 4 9 9 \n0 2 \n3000000000"], "notes": "NoteConsider the first test case, pictured in the statement. If Timur's legs have length $$$1$$$, then he can only climb stair $$$1$$$, so the highest he can reach is $$$1$$$ meter. If Timur's legs have length $$$2$$$ or $$$4$$$, then he can only climb stairs $$$1$$$, $$$2$$$, and $$$3$$$, so the highest he can reach is $$$1+2+1=4$$$ meters. If Timur's legs have length $$$9$$$ or $$$10$$$, then he can climb the whole staircase, so the highest he can reach is $$$1+2+1+5=9$$$ meters. In the first question of the second test case, Timur has no legs, so he cannot go up even a single step. :("}, "positive_code": [{"source_code": "t=gets.to_i\nt.times do\n n,q= gets.split.map(&:to_i)\n arr=gets.split.map(&:to_i)\n qs=gets.split.map(&:to_i)\n rslt=[]\n cur,sum=0,0\n arr.each do |x|\n if cur>=x\n sum+=x\n else\n rslt.push [cur,sum]\n cur=x\n sum+=x\n end\n end\n rslt.push [cur,sum]\n rslt.push [sum,sum]\n i=0\n while i=x}[1])\n if idx==qs.length-1\n puts\n else\n print ' '\n end\n end\nend\n\n"}, {"source_code": "gets.to_i.times do\n n,q=gets.split.map(&:to_i)\n a=gets.split.map(&:to_i)\n m=Array.new(n+1)\n m[0]=0\n n.times {|i| m[i+1]=[m[i],a[i]].max}\n s=[0]\n a.each {|i| s << s[-1]+i}\n k=gets.split.map(&:to_i)\n puts k.map {|i| \n ans=m.bsearch_index {|j| j>i}\n ans.nil? ? s[n] : s[ans-1]\n }*' '\nend\n"}], "negative_code": [], "src_uid": "d5f7228d8d674b8233937702ca044cb0"} {"nl": {"description": "A guy named Vasya attends the final grade of a high school. One day Vasya decided to watch a match of his favorite hockey team. And, as the boy loves hockey very much, even more than physics, he forgot to do the homework. Specifically, he forgot to complete his physics tasks. Next day the teacher got very angry at Vasya and decided to teach him a lesson. He gave the lazy student a seemingly easy task: You are given an idle body in space and the forces that affect it. The body can be considered as a material point with coordinates (0; 0; 0). Vasya had only to answer whether it is in equilibrium. \"Piece of cake\" — thought Vasya, we need only to check if the sum of all vectors is equal to 0. So, Vasya began to solve the problem. But later it turned out that there can be lots and lots of these forces, and Vasya can not cope without your help. Help him. Write a program that determines whether a body is idle or is moving by the given vectors of forces.", "input_spec": "The first line contains a positive integer n (1 ≤ n ≤ 100), then follow n lines containing three integers each: the xi coordinate, the yi coordinate and the zi coordinate of the force vector, applied to the body ( - 100 ≤ xi, yi, zi ≤ 100).", "output_spec": "Print the word \"YES\" if the body is in equilibrium, or the word \"NO\" if it is not.", "sample_inputs": ["3\n4 1 7\n-2 4 -1\n1 -5 -3", "3\n3 -1 7\n-5 2 -4\n2 -1 -3"], "sample_outputs": ["NO", "YES"], "notes": null}, "positive_code": [{"source_code": "n = $stdin.gets\nsumx = 0\nsumy = 0\nsumz = 0\nwhile line = $stdin.gets\n x,y,z = line.strip.split(' ').map{|v| v.to_i}\n sumx += x\n sumy += y\n sumz += z\nend\nif(sumx == 0 && sumy == 0 && sumz == 0)\n print \"YES\"\nelse\n print \"NO\"\nend\n"}, {"source_code": "n = gets.chomp.to_i\ntotal_x, total_y, total_z = 0, 0, 0\nn.times do\n x, y, z = gets.chomp.split(\" \").map(&:to_i)\n total_x += x\n total_y += y\n total_z += z\nend\n\nif total_x == 0 && total_y == 0 && total_z == 0\n puts \"YES\"\nelse\n puts \"NO\"\nend\n"}, {"source_code": "n = gets.to_i;s=0;b=[]\nfor i in(1..n)\n a = gets.split.map(&:to_i)\n if i>1\n b[0]+=a[0];b[1]+=a[1];b[2]+=a[2];\n else\n b=a;\n end\nend\nputs b[0]==0 && b[1]==0 && b[2]==0 ? \"YES\" : \"NO\""}, {"source_code": "puts gets.to_i.times.map{gets.split.map(&:to_i)}.transpose.all?{|a|a.inject(0){|r,j|r+j}==0} ? \"YES\" : \"NO\"\n"}, {"source_code": "n = gets.chomp.to_i\n\na = b = c = 0\n\nn.times{\n\tinput = gets.chomp.split(\" \")\n\ta += input[0].to_i\n\tb += input[1].to_i\n\tc += input[2].to_i\n}\n\nif a==0 && b==0 && c==0\n\tputs \"YES\"\nelse\n\tputs \"NO\"\nend\n"}, {"source_code": "n = readline.to_i\nsum = [0,0,0]\ni = 0\nwhile i < n do\n a = readline.split(' ')\n sum[0] += a[0].to_i\n sum[1] += a[1].to_i\n sum[2] += a[2].to_i\n i += 1\nend\nputs (if(sum[0] == 0 && sum[1] == 0 && sum[2] == 0) \n begin\n \"YES\"\n end \nelse \n begin\n \"NO\" \n end\nend)\n"}, {"source_code": "gets;puts$<.map(&:split).transpose.all?{|j|eval(j*?+)==0}?:YES:\"NO\""}, {"source_code": "n = gets.to_i\na =[]\nb=[]\nres = 0\nd = 0\n\nn.times do\n a = gets.split().map(&:to_i)\n b.push(a)\nend\n\ndef check(c,d)\n sum = 0\n sum = sum+c[d]\nend\n\nb.each do |i|\n res = res + check(i,0)\n d = d+1\nend\n\nif res == 0\n puts \"YES\"\nelse\n puts \"NO\"\nend"}, {"source_code": "# https://codeforces.com/problemset/problem/69/A\nn = gets.chomp.to_i\nforcex = 0\nforcey = 0\nforcez = 0\n1.upto(n) do |force|\n x, y, z = gets.chomp.split(\" \").map(&:to_i)\n forcex += x\n forcey += y\n forcez += z\nend\n\nif forcex == 0 && forcey == 0 && forcez == 0\n puts \"YES\"\nelse\n puts \"NO\"\nend\n"}, {"source_code": "\nclass Euler\n n=gets.to_i;\n k1=0;\n k2=0;\n k3=0;\n (1..n).each do\n a=gets;\n m=a.split(\" \");\n k1+=m[0].to_i;\n k2+=m[1].to_i;\n k3+=m[2].to_i;\n end\n if (k1==k2 && k2==k3 && k3==0)\n puts(\"YES\");\nelse\n puts(\"NO\");\n end\nend\n"}, {"source_code": "x=0\ny=0\nz=0\n\ngets.to_i.times{\n\nx+=gets(' ').to_i\ny+=gets(' ').to_i\nz+=gets.to_i\n\n}\n\nif x == 0 && y == 0 && z == 0\nprint \"YES\\n\"\nelse\nprint \"NO\\n\"\nend"}, {"source_code": "gets;puts$<.map(&:split).transpose.all?{|j|eval(j*?+)==0}?:YES:\"NO\"\n"}, {"source_code": "n = gets.to_i\n\nresult = [0, 0, 0]\n\n1.upto(n) do\n vec = gets.split(' ').map(&:to_i)\n 0.upto(2) { |i| result[i] += vec[i] }\nend\n\nputs result.all? { |n| n == 0 } ? 'YES' : 'NO'\n"}, {"source_code": "gets;puts$<.map(&:split).transpose.all?{|j|eval(j*?+)==0}?:YES:\"NO\"\n"}, {"source_code": "sum = [0, 0, 0]\n\nn = gets.to_i\n\nfor str in $stdin.each_line.take(n)\n arr = str.split(' ').map &:to_i\n for i in 0..2\n sum[i] += arr[i]\n end\nend\n\nif sum.all? { |x| x == 0 }\n puts \"YES\"\nelse\n puts \"NO\"\nend"}, {"source_code": "n = gets.to_i\ns1 = 0\ns2 = 0\ns3 = 0\nwhile (n > 0)\n a,b,c = gets.split(' ').map{|s| s.to_i}\n s1 += a\n s2 += b\n s3 += c \n n -= 1 \nend\n\nif (s1 == 0 && s2 == 0 && s3 == 0) then puts 'YES'\nelse puts 'NO'\nend"}, {"source_code": "t = gets.to_i\nsum = [0,0,0]\nfor i in 1..t\n b = gets.chomp.split().map { |e| e.to_i }\n for i in 0..2\n sum[i] += b[i]\n end\nend\n\nputs sum.uniq.size <= 1 && sum.inject(0, :+) == 0 ? 'YES' : 'NO'\n"}, {"source_code": "n = gets.chomp.to_i\nvec = [0, 0, 0]\nn.times.each do |_i|\n force = gets.chomp.split(/ /).map(&:to_i)\n vec[0] += force[0]\n vec[1] += force[1]\n vec[2] += force[2]\nend\nputs vec.uniq.count > 1 ? 'NO' : vec[0] == 0 ? 'YES' : 'NO'\n"}, {"source_code": "gets\nputs $<.map{|_|_.split}.transpose.all?{|_|0==eval(_*?+)}? \"YES\" : \"NO\""}, {"source_code": "require\"matrix\"\ngets\nputs $<.map{|_|Vector[*_.split.map(&:to_i)]}.inject(:+).r==0? \"YES\" : \"NO\""}, {"source_code": "gets; input, x, y, z = STDIN.read.split(\"\\n\").map{ |i| i.split.map! &:to_i }, 0, 0, 0\n\ninput.each { |i| x += i[0]; y += i[1]; z += i[2] }\nprint [x, y, z].join == \"000\" ? :YES : :NO"}, {"source_code": "x,y,z=0,0,0\ngets.to_i.times do\n a=gets.split\n x+=a[0].to_i\n y+=a[0].to_i\n z+=a[0].to_i\nend\nif x==0&&y==0&&z==0\n puts \"YES\"\nelse\n puts \"NO\"\nend"}, {"source_code": "n = gets.to_i\nsum = [0, 0, 0]\nn.times do\n\tf = gets.split.map { |x| x.to_i }\n\t3.times do |i|\n\t\tsum[i] += f[i]\n\tend\nend\n\nif sum.count(0) == 3\n\tputs 'YES'\nelse\n\tputs 'NO'\nend"}, {"source_code": "gets;puts$<.map(&:split).transpose.all?{|j|eval(j*?+)==0}?:YES:\"NO\"\n"}, {"source_code": "vectorNum = gets.chomp.to_i\n\ntotals = Array.new(3) { |elem| elem = 0 }\n\nvectorNum.times do\n\tvector = gets.chomp.split(' ').map(&:to_i)\n\tfor i in (0...vector.length)\n\t\ttotals[i] += vector[i]\n\tend\nend\n\n# print totals\n\nans = if totals.all? { |elem| elem == 0 }\n\t'YES'\nelse\n\t'NO'\nend\n\nputs ans"}, {"source_code": "puts %w[YES NO][STDIN.readlines.drop(1).reduce([0, 0, 0]) { |r, x| r.zip(x.split(' ').map(&:to_i)).map { |a, b| a + b } } == [0, 0, 0] ? 0 : 1]"}, {"source_code": "n = gets.to_i\nar = [0, 0, 0]\nn.times do\n gets.split(' ').map.with_index(0){|x,id| ar[id]+=x.to_i}\nend\nif ar.all?{|x| x==0}\n puts \"YES\"\nelse\n puts \"NO\"\nend"}, {"source_code": "matrix = Array.new\ngets.to_i.times do\nmatrix.push(gets.split(\" \").map(&:to_i))\nend\n\nfor lines in matrix.transpose\n res = lines.inject { |sum, i| sum += i }\n (res == 0 )?(res):(puts \"NO\"; exit)\nend\nputs \"YES\""}, {"source_code": " n = gets.to_i\n a = 0\n b = 0\n c = 0\n n.times do\n nums = gets.split(\" \").map(&:to_i)\n a+=nums[0]\n b+=nums[1]\n c+=nums[2]\n end\n \n if(a==0 and b==0 and c==0) \n puts(\"YES\")\n else\n puts(\"NO\")\n end"}, {"source_code": "n = gets.to_i\n\nsumx = 0\nsumy = 0\nsumz = 0\n\nn.times do |i|\n s = gets.split.map{|i| i.to_i}\n sumx += s[0]\n sumy += s[1]\n sumz += s[2]\nend\n\nif sumx != 0 || sumy != 0 || sumz != 0 then\n puts \"NO\"\nelse\n puts \"YES\"\nend"}, {"source_code": "forces_count = gets.to_i\n\nforce_vectors = []\n\nforces_count.times do\n force_vectors.push(gets.split.map(&:to_i))\nend\n\nresultant_force = [0, 0, 0]\n\nforce_vectors.each do |force|\n resultant_force[0] += force[0]\n resultant_force[1] += force[1]\n resultant_force[2] += force[2]\nend\n\nif resultant_force[0] | resultant_force[1] | resultant_force[2] == 0\n puts \"YES\"\nelse\n puts \"NO\"\nend\n"}, {"source_code": "x, y, z = 0, 0, 0\n\ngets.to_i.times do\n a, b, c = gets.split.map(&:to_i)\n x += a\n y += b\n z += c\nend\n\nputs [0, 0, 0] == [x, y, z] ? :YES : :NO"}, {"source_code": "x, y, z = 0, 0, 0\ngets.to_i.times do\n\ta, b, c = gets.split.map(&:to_i)\n\tx += a\n\ty += b\n\tz += c\nend\nputs [x, y, z] == [0, 0, 0] ? :YES : :NO\n"}, {"source_code": "puts( Array.new(gets.to_i) do\n gets.split.map(&:to_i)\nend.transpose.all?{ |r| r.inject(:+).zero? } ? :YES : :NO )\n"}, {"source_code": "n = gets.to_i\np = [0, 0, 0]\nn.times do\n a = gets.split.map(&:to_i)\n 3.times {|i| p[i] += a[i]}\nend\nputs p == [0, 0, 0] ? \"YES\" : \"NO\""}, {"source_code": "gets;puts$<.map(&:split).transpose.all?{|j|eval(j*?+)==0}?:YES:\"NO\"\n"}, {"source_code": "n = gets.to_i\nx0, y0, z0= 0,0,0\nn.times do\n x, y, z = gets.split.map(&:to_i)\n x0, y0, z0 = x0 + x, y0 + y, z0 + z\nend\n\nif x0 == 0 && y0 == 0 && z0 == 0\n puts \"YES\"\nelse\n puts \"NO\"\nend"}, {"source_code": "gets;puts$<.map(&:split).transpose.all?{|j|eval(j*?+)==0}?:YES:\"NO\""}, {"source_code": "q = []\na = gets.to_i\nfor i in 0..(a-1)\nq[i] = gets.split(\" \")\nend\nw = 0\ne = 0\nr = 0\n\nq.each do |n| \n\n w = w + n[0].to_i\n e = e + n[1].to_i\n r = r + n[2].to_i\n\nend\n\nif w == 0 and e == 0 and r == 0\n puts \"YES\"\nelse\n puts \"NO\"\nend"}, {"source_code": "gets;puts$<.map(&:split).transpose.all?{|j|eval(j*?+)==0}?:YES:\"NO\"\n"}, {"source_code": "num_of_lines = gets.chomp.to_i\nx = 0 \ny = 0 \nz = 0\nnum_of_lines.times do\n line = gets.chomp.split(\" \")\n x += line[0].to_i\n y += line[1].to_i\n z += line[2].to_i\nend\nputs (x == 0 && y == 0 && z == 0)? \"YES\" : \"NO\""}, {"source_code": "gets;puts$<.map(&:split).transpose.all?{|j|eval(j*?+)==0}?:YES:\"NO\""}, {"source_code": "forces = gets.to_i\n\ndef solve(n_forces)\n all_forces = Array.new(3,0)\n\n (1..n_forces).each do |i|\n a = gets.split(/\\s+/).map(&:to_i)\n all_forces = all_forces.zip(a).map(&:sum)\n end\n\n all_forces == [0,0,0] ? \"YES\" : \"NO\"\nend\n\nputs solve(forces)"}, {"source_code": "n = gets.to_i\nx = 0\ny = 0\nz = 0\nfor i in 1..n\n\tstr = gets.chomp\n\txi, yi, zi = str.split(\" \").map { |num| num.to_i }\n\tx += xi\n\ty += yi\n\tz += zi\nend\nif x == 0 && y == 0 && z == 0\n\tputs \"YES\"\nelse\n\tputs \"NO\"\nend"}, {"source_code": "gets;puts$<.map(&:split).transpose.all?{|j|eval(j*?+)==0}?:YES:\"NO\""}, {"source_code": "n=gets.chomp.to_i\na=[]\nsuma=0\nsumb=0\nsumc=0\nn.times do\n\tb=gets.chomp.split.map(&:to_i)\n\ta.push(b)\n\tsuma+=b[0]\n\tsumb+=b[1]\n\tsumc+=b[2]\nend\nif suma==0 && suma==sumb && sumb==sumc\n\tputs \"YES\"\nelse\n\tputs \"NO\"\nend"}, {"source_code": "STDIO = $stdin\nSTDIO.readline\nputs STDIO.readlines.map(&:strip).map(&:split).transpose.map { |b| b.map(&:to_i).inject(0) { |acc, x| acc+x } }.eql?([0,0,0]) ? \"YES\" : \"NO\""}, {"source_code": "n = gets.to_i\n\nr = [0, 0, 0]\n\nn.times do\n s = gets.chomp.split(' ').map &:to_i\n \n r = r.zip(s).map {|a| a.inject(:+)}\nend\n\nif r.sort == [0,0,0].sort\n puts \"YES\"\nelse\n puts \"NO\"\nend"}, {"source_code": "n = gets.to_i\na = Array.new()\nb = Array.new()\nc = Array.new()\n\nlo,xo,fo = 0,0,0\nfor i in 0..n-1\n a[i],b[i],c[i] = gets.split.map(&:to_i)\n lo+=a[i]\n xo+=b[i]\n fo+=c[i]\nend\n\nif [fo, lo, xo] == [0, 0, 0]\n puts \"YES\"\nelse\n puts \"NO\"\nend"}, {"source_code": "n = $stdin.readline.to_i\ndx,dy,dz = 0,0,0\nn.times do\n x,y,z = $stdin.readline.split.map(&:to_i)\n dx,dy,dz = x+dx, y+dy, z+dz\nend\n\nputs (dx==0 && dy==0 && dz==0) ? \"YES\" : \"NO\""}, {"source_code": "arr = []\ngets.to_i.times do\n arr.push(gets.chomp.split.map &:to_i)\nend\narr = arr.transpose.map { |x| x.inject(:+) }\nif arr == [0, 0, 0]\n puts \"YES\"\nelse\n puts \"NO\"\nend\n"}, {"source_code": "gets;puts$<.map(&:split).transpose.all?{|j|eval(j*?+)==0}?:YES:\"NO\"\n"}, {"source_code": "n = gets.chomp.to_i\nx,y,z = 0,0,0\nn.times do |i|\n a,b,c = gets.split.map(&:to_i)\n x+=a ; y+=b ; z+=c\nend\nif [x,y,x] == [0,0,0]\n puts \"YES\"\nelse\n puts \"NO\"\nend"}, {"source_code": "#!/usr/bin/env ruby\n\nwhile true\n break if STDIN.eof?\n n = gets.to_i\n\n a = b = c = 0\n n.times {\n x, y, z = gets.split.map(&:to_i)\n\n a += x; b += y; c += z\n }\n\n puts [a, b, c] == [0, 0, 0] ? 'YES' : 'NO'\nend\n"}, {"source_code": "gets;puts$<.map(&:split).transpose.all?{|j|eval(j*?+)==0}?:YES:\"NO\"\n"}, {"source_code": "no_forces = gets.chomp.to_i\nx_sum = 0\ny_sum = 0\nz_sum = 0\nno_forces.times do\n forces = gets.chomp.split(/ /).map(&:to_i)\n x_sum += forces[0]\n y_sum += forces[1]\n z_sum += forces[2]\nend\nif x_sum == 0 && y_sum == 0 && z_sum == 0\n puts \"YES\"\nelse\n puts \"NO\"\nend\n"}, {"source_code": "gets;puts$<.map(&:split).transpose.all?{|j|eval(j*?+)==0}?:YES:\"NO\"\n"}, {"source_code": "x=0\ny=0\nz=0\ngets.to_i.times{\n\ta,b,c=gets.split.map{|e| e.to_i}\n\tx=x+a\n\ty=y+b\n\tz=z+c\n}\nputs (x|y|z)==0?\"YES\":\"NO\""}, {"source_code": "gets;puts$<.map(&:split).transpose.all?{|j|eval(j*?+)==0}?\"YES\":\"NO\""}, {"source_code": "gets;puts$<.map(&:split).transpose.all?{|j|eval(j*?+)==0}?:YES:\"NO\""}, {"source_code": "gets;puts$<.map(&:split).transpose.all?{|j|eval(j*?+)==0}?:YES:\"NO\""}, {"source_code": "#Redirecting STDIN to file\n#$stdin=File.open(\"input.txt\", \"r\")\n#$stdout=File.open(\"output.txt\", \"w\")\n#END\n\nt=gets.to_i\ndata=[0,0,0]\n\nfor i in 1..t do\n x, y, z=gets.split.map(&:to_i)\n data[0]+=x\n data[1]+=y\n data[2]+=z\nend\n\nif data[0]==0 and data[1]==0 and data[2]==0 then\n puts \"YES\"\nelse\n puts \"NO\"\nend"}, {"source_code": "a=[]\nx=y=z=0\ngets.to_i.times{\n\ta=gets.split\n\tx+=a[0].to_i\n\ty+=a[1].to_i\n\tz+=a[2].to_i\n}\n\nputs(if x==y && y==z && z==0 then\n\t\t'YES'\n\telse\n\t\t'NO'\n\tend)\n"}, {"source_code": "n = gets.to_i\nx,y,z = 0,0,0\n(1..n).each do\n a, b, c = gets.split.map(&:to_i)\n x += a; y += b; z += c\nend\nputs x==0 && y==0 && z==0 ? 'YES' : 'NO'"}, {"source_code": "x,y,z = 0,0,0\n\ngets.to_i.times do \n v = gets.split.map(&:to_i)\n x += v[0]; y+= v[1]; z+= v[2];\nend\n\nputs [x,y,z].all? {|i| i == 0} ? \"YES\": \"NO\""}, {"source_code": "n = gets.to_i\nav=bv=cv = 0\nn.times{\n a,b,c = gets.split.map(&:to_i)\n av = av + a\n bv = bv + b\n cv = cv + c\n}\nputs (av**2+bv**2+cv**2 == 0 ? 'YES' : 'NO')"}, {"source_code": "x,y,z=0,0,0;gets.to_i.times{a,b,c=gets.split.map &:to_i;x+=a;y+=b;z+=c;};puts [x,y,z]==[0,0,0]?:YES: \"NO\""}, {"source_code": "number = gets.chomp.to_i\nvectors = []\n\ni = 0\nwhile i < number\n\tx = gets.chomp\n\tvectors << x.split(\" \")\n\ti += 1\nend\n\nx = 0\ny = 0\nz = 0\nvectors.each do |vector|\n\tx += vector[0].to_i\n\ty += vector[1].to_i\n\tz += vector[2].to_i\nend\n\nif x == 0 && y == 0 && z == 0\n\tprint \"YES\"\nelse\n\tprint \"NO\"\nend"}, {"source_code": "gets;puts$<.map(&:split).transpose.all?{|j|eval(j*?+)==0}?:YES:\"NO\"\n"}, {"source_code": "#puts \"Enter the number of vector\\n\"\nn = gets.to_i\na = [n]\nb = [n]\nc = [n]\nfor i in 0...n do\n a[i], b[i], c[i] = gets.split.map(&:to_i)\nend\nx = y = z = 0\nfor j in 0...n do\n x += a[j]\n y += b[j]\n z += c[j]\nend\nif x == 0 && y == 0 && z == 0\n puts \"YES\\n\"\nelse\n puts \"NO\\n\"\nend"}, {"source_code": "x_sum = 0\ny_sum = 0\nz_sum = 0\nn = gets.to_i\nn.times do\n x,y,z = gets.split(\" \").map(&:to_i)\n x_sum += x\n y_sum += y\n z_sum += z\nend\nputs x_sum == 0 && y_sum == 0 && z_sum == 0 ? \"YES\" : \"NO\""}, {"source_code": "x, y, z = [0, 0, 0]\ngets.chomp.to_i.times {\n xi, yi, zi = gets.split.map(&:to_i)\n x += xi\n y += yi\n z += zi\n}\nputs (x == 0 and y == 0 and z == 0) ? \"YES\" : \"NO\"\n"}, {"source_code": "gets;puts$<.map(&:split).transpose.all?{|j|eval(j*?+)==0}?:YES:\"NO\"\n"}, {"source_code": "require 'matrix'\n\nn = gets.to_i\n\nres = Vector[0, 0, 0]\n\nn.times do\n\tres += Vector.elements(gets.split.map(&:to_i))\nend\n\nputs \"YES\" if res == Vector[0, 0, 0]\nputs \"NO\" unless res == Vector[0, 0, 0]"}, {"source_code": "n = gets.chomp.to_i\ninit_vector = [0,0,0]\nn.times do\n vec = gets.chomp\n vec = vec.split(' ')\n\n init_vector.map!.with_index {|val, i| val += vec[i].to_i }\nend\nputs init_vector.all? {|v| v == 0 } ? \"YES\" : \"NO\""}, {"source_code": "puts ( gets.to_i.times.map{gets.split.map(&:to_i)}.transpose.map{|x| x.reduce(&:+)} == [0,0,0] )? \"YES\" : \"NO\""}, {"source_code": "require 'scanf'\n\ndef main()\n vectors_count = gets.to_i\n vectors = []\n vectors_count.times do\n vectors << gets.scanf(\"%d %d %d\")\n end\n check_vectors(vectors) ? puts(\"YES\") : puts(\"NO\")\nend\n\ndef check_vectors(vectors)\n result_sum = 0\n i = 0\n vectors[0].size.times do\n if result_sum == 0 then\n result = 0\n result = vectors.inject(0) {|result, item| result = result + item[i]}\n result_sum += result\n end\n i += 1\n end\n return result_sum == 0\nend\nmain()"}], "negative_code": [{"source_code": "n = gets.to_i;s=0\nfor i in(1..n)\n a = gets.split.map(&:to_i)\n s += (a[0]+a[1]+a[2])\nend\nputs s==0 ? \"SI\" : \"NO\""}, {"source_code": "n = gets.to_i;s=0\nfor i in(1..n)\n a = gets.split.map(&:to_i)\n s += (a[0]+a[1]+a[2])\nend\nputs s==0 ? \"YES\" : \"NO\""}, {"source_code": "n = gets.to_i\na =[]\nb=[]\nsum = 0\n\nn.times do\n a = gets.split().map(&:to_i)\n b.push(a)\nend\n\nb.each do |i|\n i.each do |j|\n sum = sum +j\n end \nend\n\nif sum == 0\n puts \"YES\"\nelse\n puts \"NO\"\nend"}, {"source_code": "n = gets.to_i\na =[]\nb=[]\nsum = 0\nres = 0\n\nn.times do\n a = gets.split().map(&:to_i)\n b.push(a)\nend\n\nb.each do |i|\n i.each do |j|\n sum = sum +j\n end\n res = res + sum\nend\n\n\n\nif res == 0\n puts \"YES\"\nelse\n puts \"NO\"\nend\n\n\n\n"}, {"source_code": "# https://codeforces.com/problemset/problem/69/A\nn = gets.chomp.to_i\nforcex = 0\nforcey = 0\nforcez = 0\n1.upto(n) do |force|\n x, y, z = gets.chomp.split(\" \").map(&:to_i)\n forcex += x\n forcey += y\n forcez += z\nend\n\nif (forcex + forcey + forcez) == 0\n puts \"YES\"\nelse\n puts \"NO\"\nend\n"}, {"source_code": "sum = [0, 0, 0]\n\nn = gets.to_i\n\nfor str in $stdin.each_line.take(n)\n arr = str.split(' ').map &:to_i\n (sum.zip(arr)).map do |x|\n x.reduce { :+ }\n end\nend\n\nif sum.all? { |x| x == 0 }\n puts \"YES\"\nelse\n puts \"NO\"\nend"}, {"source_code": "t = gets.to_i\nsum = [0,0,0]\nfor i in 1..t\n b = gets.chomp.split().map { |e| e.to_i }\n for i in 0..2\n sum[i] += b[i]\n end\nend\n\nputs sum.inject(0, :+) == 0 ? 'YES' : 'NO'"}, {"source_code": "n = gets.chomp.to_i\nvec = [0, 0, 0]\nn.times.each do |_i|\n force = gets.chomp.split(/ /).map(&:to_i)\n vec[0] += force[0]\n vec[1] += force[1]\n vec[2] += force[2]\nend\nsum = vec.inject(0) { |sum, x| sum + x }\nputs sum.zero? ? 'YES' : 'NO'\n"}, {"source_code": "n = gets.chomp.to_i\nvec = [0, 0, 0]\nn.times.each do |_i|\n force = gets.chomp.split(/ /).map(&:to_i)\n vec[0] += force[0]\n vec[1] += force[1]\n vec[2] += force[2]\nend\nputs vec.uniq.count > 1 ? 'NO' : 'YES'\n"}, {"source_code": "vectorNum = gets.chomp.to_i\n\ntotals = Array.new(3) { |elem| elem = 0 }\n\nvectorNum.times do\n\tvector = gets.chomp.split(' ').map(&:to_i)\n\tfor i in (0...vector.length)\n\t\ttotals[i] += vector[i]\n\tend\nend\n\nans = if totals.all? { |elem| elem = 0 }\n\t'YES'\nelse\n\t'NO'\nend\n\nputs ans"}, {"source_code": "def equ(*array)\n if array.length < 4 and array.length > 0\n k = 0\n array.each do |element|\n k = k + element\n end\n if k == 0\n puts \"YES\"\n else\n puts \"NO\"\n end\n else\n puts \"Enter more than 0 and less than 4 input\"\n end\nend\n\nequ()"}, {"source_code": "def equ(*array)\n if array.length < 4 and array.length > 0\n k = 0\n array.each do |element|\n k = k + element\n end\n if k == 0\n puts \"YES\"\n else\n puts \"NO\"\n end\n else\n puts \"NO\"\n end\nend"}, {"source_code": "a = 3\n\nq = \"4 -1 7\".split(\" \")\nw = \"-5 2 -4\".split(\" \")\ne = \"2 -1 -3\".split(\" \")\nres = 0\n\nfor i in 0..2\n r = q[i].to_i + w[i].to_i + e[i].to_i\n res = res + r\nend\n\nif res == 0\n puts \"YES\"\nelse\n puts \"NO\"\nend"}, {"source_code": "a = gets\nq = gets.split(\" \")\nw = gets.split(\" \")\ne = gets.split(\" \")\n\nres = 0\n\nfor i in 0..2\n r = q[i].to_i + w[i].to_i + e[i].to_i\n res = res + r\nend\n\nif res == 0\n puts \"YES\"\nelse\n puts \"NO\"\nend"}, {"source_code": "forces = gets.to_i\n\ndef solve(n_forces)\n all_forces = Array.new(3,0)\n\n (1..n_forces).each do |i|\n a = gets.split(/\\s+/).map(&:to_i)\n c = all_forces.zip(a).map(&:sum)\n end\n\n all_forces == [0,0,0] ? \"YES\" : \"NO\"\nend\n\nputs solve(forces)"}, {"source_code": "sum = 0\ngets.to_i.times do\n sum += gets.chomp.split.map { |x| x.to_i }.inject(:+) \nend\nif sum == 0 \n puts \"YES\"\nelse\n puts \"NO\"\nend\n"}, {"source_code": "def sum( forces )\n sum = 0\n forces.each do |force|\n sum += force\n end\n sum\nend\nno_forces = gets.chomp.to_i\nsum_forces = 0\nno_forces.times do\n forces = gets.chomp.split(/ /).map(&:to_i)\n sum_forces += sum forces\nend\nif sum_forces == 0\n puts \"YES\"\nelse\n puts \"NO\"\nend\n"}, {"source_code": "gets;puts$<.map(&:split).transpose.all?{|j|eval(j*?+)<1}?\"YES\":\"NO\""}, {"source_code": "number = gets.chomp.to_i\nvectors = []\n\ni = 0\nwhile i < number\n\tx = gets.chomp\n\tvectors << x.split(\" \")\n\ti += 1\nend\n\nequilibrium = 0\nvectors.each do |vector|\n\tequilibrium += vector[0].to_i + vector[1].to_i + vector[2].to_i \nend\n\nif equilibrium == 0\n\tprint \"YES\"\nelse\n\tprint \"NO\"\nend"}, {"source_code": "require 'scanf'\n\ndef main()\n vectors_count = gets.to_i\n vectors = []\n vectors_count.times do\n vectors << gets.scanf(\"%d %d %d\")\n end\n check_vectors(vectors) ? puts(\"YES\") : puts(\"NO\")\nend\n\ndef check_vectors(vectors)\n result_sum = 0\n i = 0\n vectors[0].size.times do\n if result_sum == 0 then\n result = 0\n result = vectors.inject(0) {|result, item| result = result + item[i]}\n result_sum += result\n end\n p i\n i += 1\n end\n return result_sum == 0\nend\nmain()"}, {"source_code": "require 'scanf'\n\ndef main()\n vectors_count = readline.to_i\n vectors = []\n vectors_count.times do\n vectors << readline.scanf(\"%d %d %d\")\n end\n result_sum = 0\n i = 0\n vectors_count.times do\n if result_sum == 0 then\n result = 0\n vectors.inject(0) {|result, item| result = result + item[i]}\n result_sum += result\n end\n i += 1\n end\n result_sum == 0 ? puts(\"YES\") : puts(\"NO\")\nend\n\nmain\n"}, {"source_code": "require 'scanf'\n\ndef main()\n vectors_count = readline.to_i\n vectors = []\n vectors_count.times do\n vectors << readline.scanf(\"%d %d %d\")\n end\n result = 0\n vectors.inject(0) do |result, item|\n result = result + item[0]\n end\n #p result\n vectors.inject(0) {|result, item| result = result + item[1]} if result == 0\n #p result\n vectors.inject(0) {|result, item| result = result + item[2]} if result == 0\n #p result\n result == 0 ? puts(\"YES\") : puts(\"NO\")\nend\n\nmain\n"}, {"source_code": "require 'scanf'\n\ndef main()\n vectors_count = gets.to_i\n vectors = []\n vectors_count.times do\n vectors << gets.scanf(\"%d %d %d\")\n end\n check_vectors(vectors_count, vectors) ? puts(\"YES\") : puts(\"NO\")\nend\n\ndef check_vectors(vectors_count, vectors)\n return false unless vectors_count == vectors.size\n result_sum = 0\n i = 0\n vectors_count.times do\n if result_sum == 0 then\n result = 0\n result = vectors.inject(0) {|result, item| result = result + item[i]}\n result_sum += result\n end\n i += 1\n end\n return result_sum == 0\nend\ndef check_vectors(vectors_count, vectors)\n result_sum = 0\n i = 0\n vectors[0].size.times do\n if result_sum == 0 then\n result = 0\n result = vectors.inject(0) {|result, item| result = result + item[i]}\n result_sum += result\n end\n p i\n i += 1\n end\n return result_sum == 0\nend\nmain()"}, {"source_code": "require 'scanf'\n\ndef main()\n vectors_count = readline.to_i\n vectors = []\n vectors_count.times do\n vectors << readline.scanf(\"%d %d %d\")\n end\n result = 0\n vectors.inject(0) do |result, item|\n result = result + item[0]\n end\n vectors.inject(0) {|result, item| result = result + item[1]} if result == 0\n vectors.inject(0) {|result, item| result = result + item[2]} if result == 0\n result == 0 ? puts(\"YES\") : puts(\"NO\")\nend\n\nmain\n"}], "src_uid": "8ea24f3339b2ec67a769243dc68a47b2"} {"nl": {"description": "A sequence a0, a1, ..., at - 1 is called increasing if ai - 1 < ai for each i: 0 < i < t.You are given a sequence b0, b1, ..., bn - 1 and a positive integer d. In each move you may choose one element of the given sequence and add d to it. What is the least number of moves required to make the given sequence increasing?", "input_spec": "The first line of the input contains two integer numbers n and d (2 ≤ n ≤ 2000, 1 ≤ d ≤ 106). The second line contains space separated sequence b0, b1, ..., bn - 1 (1 ≤ bi ≤ 106).", "output_spec": "Output the minimal number of moves needed to make the sequence increasing.", "sample_inputs": ["4 2\n1 3 3 2"], "sample_outputs": ["3"], "notes": null}, "positive_code": [{"source_code": "n, d = gets.split.map{|i| i.to_i}\n\nb = gets.split.map{|i| i.to_i}\n\nres = 0\n\n(n-1).times do |i|\n k = b[i] - b[i+1]\n if k >= 0 then\n m = k/d + 1\n res += m\n b[i+1] += m*d\n end\nend\n\nputs res"}, {"source_code": "n, d = gets.split.map(&:to_i)\nb = gets.split.map(&:to_i)\ns = 0\n1.upto(b.size - 1).each do |i|\n\tnext if b[i] > b[i - 1]\n\tt = (b[i - 1] - b[i]) / d + 1\n\ts += t\n\tb[i] += t * d\nend\np s\n"}, {"source_code": "d=gets.split[1].to_i\na=gets.split.map(&:to_i)\nans=0\n1.upto(a.length-1){|i|\n r=a[i-1]-a[i]+1\n if r>0 then\n ans+=(r+d-1)/d\n a[i]+=(r+d-1)/d*d\n end\n}\nputs ans"}, {"source_code": "d=gets.split[1].to_i;s=gets.split;x=s.shift.to_i;a=0;s.each{|b|b=b.to_i;a+=z=b>x ?0:(x-b)/d+1;x=b+z*d};puts a\n"}, {"source_code": "d=gets.split[1].to_i;s=gets.split;x=s.shift.to_i;a=0;s.each{|b|b=b.to_i;a+=z=b>x ?0:(x-b)/d+1;x=b+z*d};puts a\n"}, {"source_code": "d=gets.split[1].to_i;s=gets.split;x=s.shift.to_i;a=0;s.each{|b|b=b.to_i;a+=z=b>x ?0:(x-b)/d+1;x=b+z*d};puts a"}, {"source_code": "d=gets.split[1].to_i;s=gets.split;x=s.shift.to_i;a=0;s.each{|b|b=b.to_i;a+=z=b>x ?0:(x-b)/d+1;x=b+z*d};puts a"}, {"source_code": "d=gets.split[1].to_i;s=gets.split;x=s.shift.to_i;a=0;s.each{|b|b=b.to_i;a+=z=b>x ?0:(x-b)/d+1;x=b+z*d};puts a"}, {"source_code": "d=gets.split[1].to_i;s=gets.split;x=s.shift.to_i;a=0;s.each{|b|b=b.to_i;a+=z=b>x ?0:(x-b)/d+1;x=b+z*d};puts a\n"}, {"source_code": "d=gets.split[1].to_i;s=gets.split;x=s.shift.to_i;a=0;s.each{|b|b=b.to_i;a+=z=b>x ?0:(x-b)/d+1;x=b+z*d};puts a\n"}, {"source_code": "d=gets.split[1].to_i;s=gets.split;x=s.shift.to_i;a=0;s.each{|b|b=b.to_i;a+=z=b>x ?0:(x-b)/d+1;x=b+z*d};puts a"}, {"source_code": "#=================================\n# Better be sure than sorry. \n#=================================\n$stdin=File.open(\"./input.txt\",\"r\") if ENV[\"LOGNAME\"]==\"skydos\"\nrequire 'prime'\nrequire 'mathn'\nrequire 'matrix'\ninclude Math\n#=================================\n\nn,d=gets.split.map(&:to_i)\ndata=gets.split.map(&:to_i)\n\nans=0\nfor i in 1...n\n\tif data[i]<=data[i-1]\n\t\tk=((data[i-1]-data[i]+1)*1.0/d).ceil\n\t\tdata[i]+=k*d\n\t\tans+=k\n\tend\nend\n\nputs ans\n"}, {"source_code": "n,d=gets.chomp!.split(\" \").map(&:to_i)\ns,c=gets.chomp!.split(\" \").map(&:to_i),0\n(n-1).times{|i|t=s[i]-s[i+1]\nif t>=0 then\n t=t/d+1\n c+=t\n s[i+1]+=t*d end}\nputs c\n"}, {"source_code": "d=gets.split[1].to_i;s=gets.split;x=s.shift.to_i;a=0;s.each{|b|b=b.to_i;a+=z=b>x ?0:(x-b)/d+1;x=b+z*d};puts a\n"}, {"source_code": "s = gets.split(\" \")\nn = s[0].to_i\nd = s[1].to_i\n\ns = gets.split(\" \")\nmas = Array.new()\nfor i in 0..n-1\n mas[i] = s[i].to_i\nend\n\ncount = 0\nfor i in 1..n-1\n if (mas[i] <= mas[i-1])\n delta = ((mas[i-1] - mas[i]) / d) + 1\n mas[i] = mas[i] +d * delta\n count += delta\n end\nend\n\nputs count"}, {"source_code": "n,d = gets.split.map(&:to_i)\nb = gets.split.map(&:to_i)\ns = 0\n1.upto(b.size - 1).each do |i|\n\tif b[i] <= b[i-1] then\n\t\tt = (b[i-1] - b[i]) / d + 1\n\t\ts += t\n\t\tb[i] += t * d\n\tend\nend\np s\n"}, {"source_code": "d=gets.split[1].to_i;s=gets.split;x=s.shift.to_i;a=0;s.each{|b|b=b.to_i;a+=z=b>x ?0:(x-b)/d+1;x=b+z*d};puts a"}, {"source_code": "n, d = gets.split.map &:to_i\nb = gets.split.map &:to_i\nans = 0\n(1...n).each do |i|\n t = [(b[i - 1] - b[i])/d + 1, 0].max\n b[i] += t*d\n ans += t\nend\nputs ans\n"}, {"source_code": "d=gets.split[1].to_i;s=gets.split;x=s.shift.to_i;a=0;s.each{|b|b=b.to_i;a+=z=b>x ?0:(x-b)/d+1;x=b+z*d};puts a\n"}, {"source_code": "d=gets.split[1].to_i;s=gets.split;x=s.shift.to_i;a=0;s.each{|b|b=b.to_i;a+=z=b>x ?0:(x-b)/d+1;x=b+z*d};puts a\n"}, {"source_code": "d=gets.split[1].to_i;s=gets.split;x=s.shift.to_i;a=0;s.each{|b|b=b.to_i;a+=z=b>x ?0:(x-b)/d+1;x=b+z*d};puts a\n"}], "negative_code": [{"source_code": "#=================================\n# Better be sure than sorry. \n#=================================\n$stdin=File.open(\"./input.txt\",\"r\") if ENV[\"LOGNAME\"]==\"skydos\"\nrequire 'prime'\nrequire 'mathn'\n#=================================\n\nn,d=gets.split.map(&:to_i)\ndata=gets.split.map(&:to_i)\n\nans=0\ni=n-2\nloop do\n\tbreak if i<0\n\t\n\tif data[i]>=data[i+1]\n\t\t(i+1).upto(n-1){|j|\n\t\t\tdata[j]+=d\n\t\t\tans+=1\n\t\t}\n\telse\n\t\ti-=1\n\tend\nend\n\nputs ans\n"}, {"source_code": "#=================================\n# Better be sure than sorry. \n#=================================\n$stdin=File.open(\"./input.txt\",\"r\") if ENV[\"LOGNAME\"]==\"skydos\"\nrequire 'prime'\nrequire 'mathn'\n#=================================\n\nn,d=gets.split.map(&:to_i)\ndata=gets.split.map(&:to_i)\n\nans=0\n(n-2).downto(0){|i|\n\tif data[i]>=data[i+1]\n\t\t(i+1).upto(n-1){|j|\n\t\t\tdata[j]+=d\n\t\t\tans+=1\n\t\t}\n\tend\n}\n\nputs ans\n"}, {"source_code": "e,e[1],c=Array.new(2),0,0\nn,d=gets.chomp!.split(\" \").map(&:to_i)\nn.times{|i|e[i%2]=$stdin.read(2)[0,1].to_f\nc+=((e[i%2]-e[(i+1)%2]+1.0)/n).ceil\ne[(i+1)%2]+=c*d}\nputs c\n"}, {"source_code": "s = gets.split(\" \")\nn = s[0].to_i\nd = s[1].to_i\n\ns = gets.split(\" \")\nmas = Array.new()\nfor i in 0..n-1\n mas[i] = s[i].to_i\nend\n\ncount = 0\nfor i in 1..n-1\n if (mas[i] <= mas[i-1])\n delta = ((mas[i-1] - mas[i]) / d) + 1\n mas[i] = mas[i-1] +d * delta\n count += delta\n end\nend\n\nputs count"}], "src_uid": "0c5ae761b046c021a25b706644f0d3cd"} {"nl": {"description": "Kirito is stuck on a level of the MMORPG he is playing now. To move on in the game, he's got to defeat all n dragons that live on this level. Kirito and the dragons have strength, which is represented by an integer. In the duel between two opponents the duel's outcome is determined by their strength. Initially, Kirito's strength equals s.If Kirito starts duelling with the i-th (1 ≤ i ≤ n) dragon and Kirito's strength is not greater than the dragon's strength xi, then Kirito loses the duel and dies. But if Kirito's strength is greater than the dragon's strength, then he defeats the dragon and gets a bonus strength increase by yi.Kirito can fight the dragons in any order. Determine whether he can move on to the next level of the game, that is, defeat all dragons without a single loss.", "input_spec": "The first line contains two space-separated integers s and n (1 ≤ s ≤ 104, 1 ≤ n ≤ 103). Then n lines follow: the i-th line contains space-separated integers xi and yi (1 ≤ xi ≤ 104, 0 ≤ yi ≤ 104) — the i-th dragon's strength and the bonus for defeating it.", "output_spec": "On a single line print \"YES\" (without the quotes), if Kirito can move on to the next level and print \"NO\" (without the quotes), if he can't.", "sample_inputs": ["2 2\n1 99\n100 0", "10 1\n100 100"], "sample_outputs": ["YES", "NO"], "notes": "NoteIn the first sample Kirito's strength initially equals 2. As the first dragon's strength is less than 2, Kirito can fight it and defeat it. After that he gets the bonus and his strength increases to 2 + 99 = 101. Now he can defeat the second dragon and move on to the next level.In the second sample Kirito's strength is too small to defeat the only dragon and win."}, "positive_code": [{"source_code": "s, n = gets.split(' ').map(&:to_i)\nar = []\nans = \"YES\"\nn.times do\n ar << gets.split(' ').map(&:to_i)\nend\nar.sort_by(&:first).each do |x,y|\n ans = \"NO\" if s <= x\n s += y\nend\nputs ans"}, {"source_code": "s,n = gets.split.map{|i| i.to_i}\n\ndragon = []\nn.times do |i|\n dragon << gets.split.map{|j| j.to_i}\nend\n\ndragon = dragon.sort{|a,b| a[0] <=> b[0]}\n\nt = true\nn.times do |i|\n if s > dragon[i][0] then\n s += dragon[i][1]\n else\n t = false\n end\nend\n\nif t then\n puts \"YES\"\nelse\n puts \"NO\"\nend"}, {"source_code": "s, n = \ngets.split.map &:to_i\n\nall_dragons_dead = true\n\ndragons = []\nn.times do\n dragons << gets.split.map(&:to_i)\nend\n\ndragons = dragons.sort do |d1, d2|\n d1[0] <=> d2[0]\nend\n\ndragons.each do |dragon|\n dragon_s, bonus = dragon\n\n if s <= dragon_s\n all_dragons_dead = false\n break\n end\n \n s += bonus\nend\n\noutput = all_dragons_dead ? \"YES\" : \"NO\"\nputs output"}, {"source_code": "class Dragon\n attr_accessor :hp\n attr_accessor :bonus\n def initialize(h, b)\n @hp = h\n @bonus = b\n end\nend\nline1 = gets.chomp.split(\" \")\ns = line1[0].to_i\nn = line1[1].to_i\n\nenemy = []\n\nfor i in 1..n\n line = gets.chomp.split(\" \").map {|x| x.to_i}\n enemy << Dragon.new(line[0], line[1])\nend\n\nenemy = enemy.sort_by {|obj| obj.hp}\n\nwin = true\nfor e in enemy\n if s > e.hp\n s += e.bonus\n else\n win = false\n break\n end\nend\n\nif win\n puts \"YES\"\nelse\n puts \"NO\"\nend\n"}, {"source_code": "s,n=gets.split.map(&:to_i)\nputs n.times.map{gets.split.map(&:to_i)}.sort_by{|x,y|x}.all?{|x,y|x+y y[0]}\n\na.each do |el|\n if s <= el[0]\n puts \"NO\"\n exit\n end\n s += el[1]\nend\n\nputs \"YES\""}, {"source_code": "class Dragon\n\tattr_reader :life, :bonus\n\tdef initialize(life, bonus)\n\t\t@life = life\n\t\t@bonus = bonus\n\tend\nend\n\ninput = gets.split\nkirito = input[0].to_i\nn = input[1].to_i\n\nd = Array.new\nfor i in 0...n\n\tinput = gets.split\n\td[i] = Dragon.new(input[0].to_i, input[1].to_i)\nend\nd.sort! {|x, y| x.life <=> y.life}\n\ndie = false\nfor i in 0...n\n\tif kirito <= d[i].life\n\t\tputs \"NO\"\n\t\tdie = true\n\t\tbreak\n\tend\n\tkirito += d[i].bonus\nend\n\nif not die then puts \"YES\" end\n"}, {"source_code": "s,n=gets.split.map(&:to_i)\nputs n.times.map{gets.split.map(&:to_i)}.sort_by{|x,y|x}.all?{|x,y|x+ya[i]\n\t\t\tcheck=1\n\t\t\ta[i]=0\n\t\t\ts+=b[i]\n\t\t\tbreak\n\t\tend\n\t\ti+=1\n\tend\nend\nif check\n\tputs \"YES\"\nelse\n\tputs \"NO\"\nend"}, {"source_code": "s, _ = gets.split.map(&:to_i)\nreadlines.map(&:strip).map { |x| x.split.map(&:to_i) }.sort_by! { |x| x[0]}.each do |x|\n if s <= x[0]\n puts \"NO\"\n exit\n else\n s += x[1]\n end\nend\nputs \"YES\""}, {"source_code": "s, n = gets.chomp.split(\" \").map(&:to_i)\na = []\n\nn.times do\n q = gets.chomp.split(\" \").map(&:to_i)\n a.push(q)\nend\n\na.sort_by!{|x, y| x}\n\na.each do |x, y|\n if s > x\n s += y\n else\n puts \"NO\"\n exit\n end\nend\n\nputs \"YES\"\n"}, {"source_code": "s,n=gets.split.map(&:to_i)\nputs n.times.map{gets.split.map(&:to_i)}.sort_by{|x,y|x}.all?{|x,y|x+ydrag-1\nend\nif i>drag-1\nputs \"YES\"\nend"}, {"source_code": "r=->(){gets.to_s.split.map &:to_i}\ns,n=*r.call()\na=[];n.times {a<=s\n s+=x[1]\nend\nprint ok ? 'YES' : 'NO'\n"}, {"source_code": "def r() gets.to_s.split.map &:to_i end\ns,n=*r\na=[];n.times {a<=s then print 'NO';exit(0);end\n s+=x[1]\nend\nprint 'YES'\n"}, {"source_code": "s,n=gets.split.map(&:to_i)\nputs n.times.map{gets.split.map(&:to_i)}.sort_by{|x,y|x}.all?{|x,y|x+y dragon[0]\n kirito += dragon[1]\n else\n winner = false\n break\n end\nend\nif winner\n puts \"YES\"\nelse\n puts \"NO\"\nend\n"}, {"source_code": " a=gets.chomp.split(\" \")\npow=a[0].to_i\ndrag=a[1].to_i\ndragary=[]\ndrag.times do\ntemp=gets.chomp.split(\" \")\ntemp[0]=temp[0].to_i \ntemp[1]=temp[1].to_i\ndragary<< temp\nend\ndragary.sort!\ni=0\nloop do \nif dragary[i][0]drag-1\nend\nif i>drag-1\nputs \"YES\"\nend"}, {"source_code": "s,n=gets.split.map(&:to_i)\nputs n.times.map{gets.split.map(&:to_i)}.sort_by{|x,y|x}.all?{|x,y|x+y d[\"strength\"] then\n\t\tstrength+= d[\"bonus\"]\n\telse\n\t\tputs \"NO\"\n\t\texit\n\tend\nend\n\nputs \"YES\"\nexit\n"}, {"source_code": "n,m=gets.split.map{|e| e.to_i}\nx=[]\nm.times{|i|\n\ta=gets.split.map{|e| e.to_i}\n\tx< b[0]\n else\n b[1] <=> a[1]\n end\nend\ndragons.each { |dragon|\n if s > dragon[0]\n s += dragon [1]\n else\n win = false\n break\n end\n}\nputs win ? \"YES\" : \"NO\""}, {"source_code": "s, n = gets.split.map(&:to_i)\nd = []\nn.times {\n d << gets.split.map(&:to_i)\n}\nd = d.sort_by{ |x| x[0]}\nflag = true\nd.each do |x|\n if s <= x[0]\n flag = false\n else\n s += x[1]\n end\nend\n\nputs flag ? \"YES\" : \"NO\"\n"}, {"source_code": "power, enemy_count = gets.chop.split.map(&:to_i)\n\ndragons = []\nenemy_count.times { dragons << gets.chop.split.map(&:to_i) }\n\nloop do\n weak_dragons = dragons.select { |dragon| power > dragon[0] }\n break if dragons.count.zero? || weak_dragons.count.zero?\n power += weak_dragons.reduce(0) { |bonus, dragon| bonus += dragon[1] }\n dragons -= weak_dragons\nend\n\ndragons.count.zero? ? puts('YES') : puts('NO')"}, {"source_code": "#!/usr/bin/env ruby\n\ns,n = gets.split.map(&:to_i)\n\nx = n.times.map { gets.split.map(&:to_i) }\n\nyes = 1\nn.times do\n id = -1\n x.each_with_index do |arr, i|\n if arr[0] < s\n id = i\n break\n end\n end\n if id == -1\n yes = 0\n break\n end\n s += x[id][1]\n x.delete_at(id)\nend\n\nputs(yes == 1 ? 'YES' : 'NO')\n"}, {"source_code": "#!/usr/bin/env ruby\n\ns,n = gets.split.map(&:to_i)\n\nx = n.times.map { gets.split.map(&:to_i) }.sort\n\nyes = true\nx.each do |a,b|\n if s <= a\n yes = false\n break\n end\n s += b\nend\n\nputs(yes ? 'YES' : 'NO')\n"}, {"source_code": "s, n = gets.split.map(&:to_i)\na = []\nn.times do\n b = gets.split.map(&:to_i)\n a << b\nend\na.sort!\nans = 1\nn.times do |i|\n if s <= a[i][0]\n ans = 0\n break\n end\n s += a[i][1]\nend\nputs ans == 0 ? \"NO\" : \"YES\"\n"}, {"source_code": "s, n = gets.split.map(&:to_i)\na = n.times.map{gets.split.map(&:to_i)}.sort\nans = true\nn.times do |i|\n if s <= a[i][0]\n ans = false\n break\n end\n s += a[i][1]\nend\nputs ans ? \"YES\" : \"NO\"\n"}, {"source_code": " a=gets.chomp.split(\" \")\npow=a[0].to_i\ndrag=a[1].to_i\ndragary=[]\ndrag.times do\ntemp=gets.chomp.split(\" \")\ntemp[0]=temp[0].to_i \ntemp[1]=temp[1].to_i\ndragary<< temp\nend\ndragary.sort!\ni=0\nloop do \nif dragary[i][0]drag-1\nend\nif i>drag-1\nputs \"YES\"\nend"}, {"source_code": "class Dragon\n attr_accessor :x, :y\n\n def initialize(x, y)\n @x = x\n @y = y\n end\n\n def <=>(other)\n unless self.x == other.x\n self.x - other.x\n else\n other.y - self.y\n end\n end\nend\n\ns, n = gets.split.map &:to_i\nar = Array.new\nn.times do\n ar << Dragon.new(*(gets.split.map &:to_i))\nend\nar.sort!\nar.each do |dragon|\n if dragon.x < s\n s += dragon.y\n else\n puts \"NO\"\n exit\n end\nend\nputs \"YES\"\n"}, {"source_code": "s,n=gets.split.map(&:to_i)\nputs n.times.map{gets.split.map(&:to_i)}.sort_by{|x,y|x}.all?{|x,y|x+y y.first }\n\nsuccess = true\na.inject(s) do |strength, dragon|\n if strength <= dragon.first\n success = false\n break\n end\n\n strength += dragon.last\nend\n\nputs success ? 'YES' : 'NO'\n"}, {"source_code": "first_line = gets.chomp.split(' ')\ns = first_line[0].to_i\nn = first_line[1].to_i\ndragons = []\n(0...n).each do |i|\n\tline = gets.chomp.split(' ')\n\tdragons[i] = {\n\t\tstrength: line[0].to_i,\n\t\tbonus: line[1].to_i\n\t}\nend\n\ndragons.sort! { |a, b| a[:strength] <=> b[:strength] }\ndefeated = false\ndragons.each do |elem|\n\tif s <= elem[:strength]\n\t\tdefeated = true\n\t\tbreak\n\tend\n\ts += elem[:bonus]\nend\n\nif defeated\n\tputs \"NO\"\nelse\n\tputs \"YES\"\nend\n\n\n"}, {"source_code": "s,n=gets.split.map(&:to_i)\nputs n.times.map{gets.split.map(&:to_i)}.sort_by{|x,y|x}.all?{|x,y|x+y b.x }\n\nisDead = false\ndragons.each { |dragon|\n\n if s <= dragon.x\n isDead = true\n break\n end\n\n s += dragon.y\n}\n\nif isDead\n puts \"NO\"\nelse\n puts \"YES\"\nend"}, {"source_code": "s,n=gets.split.map(&:to_i)\nputs n.times.map{gets.split.map(&:to_i)}.sort_by{|x,y|x}.all?{|x,y|x+y dragon.strength\n @strength += dragon.bonus\n return true\n else\n return false\n end\n end\nend\n\nclass Dragon\n attr_reader :strength, :bonus\n def initialize(strength, bonus)\n @strength = strength\n @bonus = bonus\n end\nend\n\ns, n = gets.split.map(&:to_i)\nkirito = Kirito.new(s)\ndragons = []\nn.times do\n dragons << Dragon.new(*gets.split.map(&:to_i))\nend\ndragons.sort!{|a, b| a.strength <=> b.strength}\ndragons.each do |d|\n unless kirito.attack(d)\n puts \"NO\"\n exit\n end\nend\nputs \"YES\"\n"}], "negative_code": [{"source_code": "s, n = \ngets.split.map &:to_i\n\nall_dragons_dead = true\n\ndragons = []\nn.times do\n dragons << gets.split.map(&:to_i)\nend\n\ndragons = dragons.sort do |d1, d2|\n d1[0] <=> d2[0]\nend\n\ndragons.each do |dragon|\n dragon_s, bonus = dragon\n\n if s < dragon_s\n all_dragons_dead = false\n break\n end\n \n s += bonus\nend\n\noutput = all_dragons_dead ? \"YES\" : \"NO\"\nputs output"}, {"source_code": "s, n = \ngets.split.map &:to_i\n\nall_dragons_dead = true\n\ndragons = []\nn.times do\n dragons << gets.split.map(&:to_i)\nend\n\ndragons = dragons.sort do |d1, d2|\n d1[0] <=> d2[0]\nend\n\ndragons.each do |dragon|\n dragon_s, bonus = dragon\n\n if s < dragon_s\n all_dragons_dead = false\n break\n end\n \n s += bonus\n s -= dragon_s\nend\n\noutput = all_dragons_dead ? \"YES\" : \"NO\"\nputs output"}, {"source_code": "class Dragon\n attr_accessor :hp\n attr_accessor :bonus\n def initialize(h, b)\n @hp = h\n @bonus = b\n end\nend\nline1 = gets.chomp.split(\" \")\ns = line1[0].to_i\nn = line1[1].to_i\n\nline_hp = []\nline_bonus = []\nif n == 1\n line = gets.chomp.split(\" \").map {|x| x.to_i}\n line_hp[0] = line[0]\n line_bonus[0] = line[1]\nelse\n line_hp = gets.chomp.split(\" \").map {|x| x.to_i}\n line_bonus = gets.chomp.split(\" \").map {|x| x.to_i}\nend\nenemy = []\n\nfor i in 1..n\n enemy << Dragon.new(line_hp[i-1], line_bonus[i-1])\nend\n\nenemy.sort_by {|obj| obj.hp}\n\nwin = true\nfor e in enemy\n if s > e.hp\n s += e.bonus\n else\n win = false\n break\n end\nend\n\nif win\n puts \"YES\"\nelse\n puts \"NO\"\nend\n"}, {"source_code": "s, n = gets.split(\" \").map(&:to_i)\na = []\n\nn.times do |i|\n a[i] = gets.split(\" \").map(&:to_i)\nend\n\na.sort!{|x,y| x[0] <=> y[0]}\n\na.each do |el|\n if s < el[0]\n puts \"NO\"\n exit\n end\n s += el[1]\nend\n\nputs \"YES\""}, {"source_code": "s, _ = gets.split.map(&:to_i)\nreadlines.map(&:strip).map { |x| x.split.map(&:to_i) }.sort_by! { |x| x[0]}.each do |x|\n if s < x[0]\n puts \"NO\"\n exit\n else\n s += x[1]\n end\nend\nputs \"YES\""}, {"source_code": "a=gets.chomp.split(\" \")\npow=a[0].to_i\ndrag=a[1].to_i\ndragary=[]\ndrag.times do\ntemp=gets.chomp.split(\" \")\ntemp[0]=temp[0].to_i \ntemp[1]=temp[1].to_i\ndragary<< temp\nend\ndragary.sort!\ni=0\nloop do \nif dragary[i][0]<=pow\npow+=dragary[i][1]\ni+=1\nelse\nputs \"NO\"\nbreak\nend\nbreak if i>drag-1\nend\nif i>drag-1\nputs \"YES\"\nend\n"}, {"source_code": "s, n = gets.chomp.split(/ /).map(&:to_i)\nkirito = s\nwinner = true\nn.times do\n x, y = gets.chomp.split(/ /).map(&:to_i)\n if kirito > x\n kirito += y\n else\n winner = false\n break\n end\nend\nif winner\n puts \"YES\"\nelse\n puts \"NO\"\nend\n"}, {"source_code": "#!/usr/bin/env ruby\nfirst_line = gets\nstrength = first_line.split(' ')[0].to_i\ncount_of_dragons = first_line.split(' ')[1].to_i\n\ndragons = []\n\n\n(1..count_of_dragons).each do |i|\n\tdragon = {}\n\tdrag_str = gets\n\tdragon[\"strength\"] = drag_str.split(' ')[0].to_i\n\tdragon[\"bonus\"] = drag_str.split(' ')[1].to_i \n\tdragons.push(dragon)\nend\n\ndragons.sort_by!{ |d| d[\"strength\"] }\n\ndragons.each do |d|\n\tif strength > d[\"strength\"] then\n\t\tstrength+= d[\"bonus\"]\n\telse\n\t\tputs \"NO\"\n\t\texit\n\tend\n\tputs \"YES\"\n\texit\nend\n"}, {"source_code": "#!/usr/bin/env ruby\nfirst_line = gets\nstrength = first_line.split(' ')[0].to_i\ncount_of_dragons = first_line.split(' ')[1].to_i\n\ndragons = []\n\n\n(1..count_of_dragons).each do |i|\n\tdragon = {}\n\tdrag_str = gets\n\tdragon[\"strength\"] = drag_str.split(' ')[0].to_i\n\tdragon[\"bonus\"] = drag_str.split(' ')[1].to_i \n\tdragons.push(dragon)\nend\n\ndragons = dragons.sort_by{ |d| d[\"strength\"] }\n\ndragons.each do |d|\n\tif strength > d[\"strength\"] then\n\t\tstrength+= d[\"bonus\"]\n\telse\n\t\tputs \"NO\"\n\t\texit\n\tend\n\tputs \"YES\"\n\texit\nend\n"}, {"source_code": "s,n = gets.split.map &:to_i\na = []\nn.times do |i|\na[i] = gets\na.sort!\nend\nfor i in a\n\tx,y = i.split.map &:to_i\n\tif x < s\n\t\ts += y\n\telse\n\t\ts = -1\n\tend\t\nend\nif s == -1\n\tputs 'NO'\nelse\n\tputs 'YES'\nend\n\n\n\n"}, {"source_code": "s,n = gets.split(\" \").map(&:to_i)\nwin = true\ndragons = []\nn.times do\n dragons << gets.split(\" \").map(&:to_i)\nend\ndragons.sort! do |a,b|\n if a[0] != b[0]\n a[0] <=> b[0]\n else\n b[1] <=> a[1]\n end\nend\ndragons.each { |dragon|\n if s >= dragon[0]\n s += dragon [1]\n else\n win = false\n break\n end\n}\nputs win ? \"YES\" : \"NO\""}, {"source_code": "s, n = gets.split.map(&:to_i)\nd = []\nn.times {\n d << gets.split.map(&:to_i)\n}\nd = d.sort_by{ |x| x[0]}\nputs d.to_s\nflag = true\nd.each do |x|\n if s <= x[0]\n flag = false\n else\n s += x[1]\n end\nend\n\nputs flag ? \"YES\" : \"NO\"\n"}, {"source_code": "#!/usr/bin/env ruby\n\ns,n = gets.split.map(&:to_i)\n\nx = n.times.map { gets.split.map(&:to_i) }\n\nyes = 1\nn.times do\n id = -1\n x.each_with_index do |arr, i|\n if arr[0] <= s\n id = i\n break\n end\n end\n if id == -1\n yes = 0\n break\n end\n s += x[id][1]\n x.delete_at(id)\nend\n\nputs(yes == 1 ? 'YES' : 'NO')\n"}, {"source_code": "s, n = gets.split.map(&:to_i)\na = []\nn.times do\n b = gets.split.map(&:to_i)\n a << b\nend\na.sort!\nans = 1\nn.times do |i|\n if s < a[i][0]\n ans = 0\n break\n end\n s += a[i][1]\nend\nputs ans == 0 ? \"NO\" : \"YES\"\n"}, {"source_code": "first_line = gets.chomp.split(' ')\ns = first_line[0].to_i\nn = first_line[1].to_i\ndragons = []\n(0...n).each do |i|\n\tline = gets.chomp.split(' ')\n\tdragons[i] = {\n\t\tstrength: line[0].to_i,\n\t\tbonus: line[1].to_i\n\t}\nend\n\ndragons.sort { |a, b| a[:strength] <=> b[:strength] }\ndefeated = false\ndragons.each do |elem|\n\tif s <= elem[:strength]\n\t\tdefeated = true\n\t\tbreak\n\tend\n\ts += elem[:bonus]\nend\n\nif defeated\n\tputs \"NO\"\nelse\n\tputs \"YES\"\nend\n\n\n"}], "src_uid": "98f5b6aac08f48f95b2a8ce0738de657"} {"nl": {"description": "You are given $$$4n$$$ sticks, the length of the $$$i$$$-th stick is $$$a_i$$$.You have to create $$$n$$$ rectangles, each rectangle will consist of exactly $$$4$$$ sticks from the given set. The rectangle consists of four sides, opposite sides should have equal length and all angles in it should be right. Note that each stick can be used in only one rectangle. Each stick should be used as a side, you cannot break the stick or use it not to the full length.You want to all rectangles to have equal area. The area of the rectangle with sides $$$a$$$ and $$$b$$$ is $$$a \\cdot b$$$.Your task is to say if it is possible to create exactly $$$n$$$ rectangles of equal area or not.You have to answer $$$q$$$ independent queries.", "input_spec": "The first line of the input contains one integer $$$q$$$ ($$$1 \\le q \\le 500$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \\le n \\le 100$$$) — the number of rectangles. The second line of the query contains $$$4n$$$ integers $$$a_1, a_2, \\dots, a_{4n}$$$ ($$$1 \\le a_i \\le 10^4$$$), where $$$a_i$$$ is the length of the $$$i$$$-th stick.", "output_spec": "For each query print the answer to it. If it is impossible to create exactly $$$n$$$ rectangles of equal area using given sticks, print \"NO\". Otherwise print \"YES\".", "sample_inputs": ["5\n1\n1 1 10 10\n2\n10 5 2 10 1 1 2 5\n2\n10 5 1 10 5 1 1 1\n2\n1 1 1 1 1 1 1 1\n1\n10000 10000 10000 10000"], "sample_outputs": ["YES\nYES\nNO\nYES\nYES"], "notes": null}, "positive_code": [{"source_code": "gets.to_i.times do\n\tn = gets.to_i\n\ta = gets.split.map(&:to_i).tally\n\tif a.any? { |k, v| v.odd? }\n\t\tputs 'NO'\n\telse\n\t\tb = a.reduce([]) { |acc, (k, v)| acc.concat([k] * (v / 2)) }.sort\n\t\tc = (0...n).map { |i| b[i] * b[~i] }\n\t\tputs c == c.rotate(1) ? \"YES\" : \"NO\"\n\tend\nend\n"}, {"source_code": "gets.strip.to_i.times do\n n = gets.strip.to_i\n sticks = gets.strip.split(' ').map(&:to_i)\n h = Hash.new(0)\n sticks.each do |s|\n h[s] += 1\n end\n\n if h.any? { |k, v| (v % 2) != 0 }\n puts \"NO\"\n else\n vals = []\n h.each do |k,v|\n h[k] = v / 2\n (v / 2).times { vals << k }\n end\n vals.sort!\n i , j = 0, vals.size - 1\n last = nil\n res = true\n while i < j\n if vals[i] * vals[j] != last && i != 0\n puts \"NO\"\n res = false\n break\n end\n last = vals[i] * vals[j]\n i += 1\n j -= 1\n end\n puts \"YES\" if res\n end\nend\n"}, {"source_code": "gets.to_i.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n a.sort!\n s = a[0] * a.last\n ok = true\n (0...n).each do |i|\n ok = false if a[2 * i] != a[2 * i + 1] or a[4 * n - 2 * i - 1] != a[4 * n - 2 * i - 2] or a[2 * i] * a[4 * n - 2 * i - 1] != s\n end\n unless ok\n puts \"NO\"\n else\n puts \"YES\"\n end\nend"}, {"source_code": "q = gets.to_i\nans = []\nq.times do\n n = gets.to_i\n pp = gets.split.map(&:to_i).sort{|a,b|b<=>a}\n\n vv = []\n can = true\n\n 0.step(n*4-2,2) do |i|\n # ペアがないとダメ\n unless pp[i]==pp[i+1]\n can = false\n break\n end\n\n vv << pp[i]\n end\n #puts vv.to_s #d\n\n if can\n e1 = vv.shift\n e2 = vv.pop\n s = e1*e2\n while vv.size > 1 do\n e1 = vv.shift\n e2 = vv.pop\n if e1*e2 != s\n can = false\n break\n end\n end\n end\n\n ans << (can ? 'YES' : 'NO')\nend\nputs ans\n"}, {"source_code": "t = gets.to_i\nt.times do |index|\n n = (gets.to_i)*4\n a = gets.split(\" \").map(&:to_i)\n b = []\n a.sort!\n i = 0\n ans = true\n while i < n\n if a[i] != a[i+1] \n ans = false\n end\n b.push a[i]\n i += 2\n end\n i = 0\n j = n/2 - 1\n area = b[i]*b[j]\n while i <= j\n if (b[i])*(b[j]) != area\n ans = false\n end\n i += 1\n j -= 1\n end\n puts ans ? \"YES\" : \"NO\"\nend"}], "negative_code": [{"source_code": "q = gets.to_i\nans = []\nq.times do\n n = gets.to_i\n pp = gets.split.map(&:to_i).sort{|a,b|b<=>a}\n\n vv = []\n can = true\n\n 0.step(n*4-2,2) do |i|\n # ペアがないとダメ\n unless pp[i]==pp[i+1]\n can = false\n break\n end\n\n vv << pp[i]\n end\n #puts vv.to_s #d\n\n if can\n s = vv.inject(:*) / n\n while vv.size > 1 do\n e1 = vv.shift\n idx = pp.index(s/e1)\n if idx.nil?\n can = false\n break\n end\n vv.delete_at(idx)\n end\n end\n\n ans << (can ? 'YES' : 'NO')\nend\nputs ans"}], "src_uid": "08783e3dff3f3b7eb586931f9d0cd457"} {"nl": {"description": "For a collection of integers $$$S$$$, define $$$\\operatorname{mex}(S)$$$ as the smallest non-negative integer that does not appear in $$$S$$$.NIT, the cleaver, decides to destroy the universe. He is not so powerful as Thanos, so he can only destroy the universe by snapping his fingers several times.The universe can be represented as a 1-indexed array $$$a$$$ of length $$$n$$$. When NIT snaps his fingers, he does the following operation on the array: He selects positive integers $$$l$$$ and $$$r$$$ such that $$$1\\le l\\le r\\le n$$$. Let $$$w=\\operatorname{mex}(\\{a_l,a_{l+1},\\dots,a_r\\})$$$. Then, for all $$$l\\le i\\le r$$$, set $$$a_i$$$ to $$$w$$$. We say the universe is destroyed if and only if for all $$$1\\le i\\le n$$$, $$$a_i=0$$$ holds.Find the minimum number of times NIT needs to snap his fingers to destroy the universe. That is, find the minimum number of operations NIT needs to perform to make all elements in the array equal to $$$0$$$.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 10^4$$$). Description of the test cases follows. The first line of each test case contains one integer $$$n$$$ ($$$1\\le n\\le 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, $$$\\ldots$$$, $$$a_n$$$ ($$$0\\le a_i\\le 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\\cdot 10^5$$$.", "output_spec": "For each test case, print one integer — the answer to the problem.", "sample_inputs": ["4\n\n4\n\n0 0 0 0\n\n5\n\n0 1 2 3 4\n\n7\n\n0 2 3 0 1 2 0\n\n1\n\n1000000000"], "sample_outputs": ["0\n1\n2\n1"], "notes": "NoteIn the first test case, we do $$$0$$$ operations and all elements in the array are already equal to $$$0$$$.In the second test case, one optimal way is doing the operation with $$$l=2$$$, $$$r=5$$$.In the third test case, one optimal way is doing the operation twice, respectively with $$$l=4$$$, $$$r=4$$$ and $$$l=2$$$, $$$r=6$$$.In the fourth test case, one optimal way is doing the operation with $$$l=1$$$, $$$r=1$$$."}, "positive_code": [{"source_code": "# gets.to_i\r\n# gets.split.map(&:to_i)\r\n\r\ngets.to_i.times do\r\n n = gets.to_i\r\n a = gets.split.map(&:to_i)\r\n\r\n ans = 0\r\n flag = false\r\n n.times do |i|\r\n if !flag && a[i] != 0\r\n ans += 1\r\n flag = true\r\n elsif a[i] == 0\r\n flag = false\r\n end\r\n end\r\n\r\n puts [ans,2].min\r\nend\r\n"}, {"source_code": "gets.to_i.times do\r\n gets\r\n as = gets.split.map(&:to_i)\r\n bs = as.drop_while { |a| a == 0 }.reverse.drop_while { |a| a == 0 }.reverse\r\n if bs.empty?\r\n p 0\r\n elsif bs.include?(0)\r\n p 2\r\n else\r\n p 1\r\n end\r\nend\r\n"}, {"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n a = gets.split.map(&:to_i)\r\n f = true\r\n ans = 0\r\n a.each do |x|\r\n (ans += 1) if (x != 0 && f)\r\n f = x == 0 ? true : false\r\n end\r\n puts (ans > 2)?2 : ans\r\nend"}], "negative_code": [{"source_code": "# gets.to_i\r\n# gets.split.map(&:to_i)\r\n\r\ngets.to_i.times do\r\n n = gets.to_i\r\n a = gets.split.map(&:to_i)\r\n\r\n ans = 0\r\n flag = false\r\n n.times do |i|\r\n if !flag && a[i] != 0\r\n ans += 1\r\n flag = true\r\n elsif a[i] == 0\r\n flag = false\r\n end\r\n end\r\n\r\n puts ans\r\nend\r\n"}, {"source_code": "# gets.to_i\r\n# gets.split.map(&:to_i)\r\n\r\ngets.to_i.times do\r\n n = gets.to_i\r\n a = gets.split.map(&:to_i)\r\n list = []\r\n\r\n k = []\r\n n.times do |i|\r\n if a[i] != 0\r\n k << a[i]\r\n else\r\n list << k if !k.empty?\r\n k = []\r\n end\r\n end\r\n list << k if !k.empty?\r\n\r\n puts list.size\r\nend\r\n\r\n"}, {"source_code": "gets.to_i.times do\r\n gets\r\n xs = gets.split.map(&:to_i)\r\n p xs.slice_when { |a, b| (a == 0) != (b == 0) }.count { |xs| xs.max > 0 }\r\nend\r\n"}, {"source_code": "gets.to_i.times do\r\n gets\r\n xs = gets.split.map(&:to_i)\r\n p xs.drop_while { |x| x == 0 }.reverse.drop_while { |x| x == 0 }.reverse.slice_when { |x| x == 0 }.count { |xs| xs.max > 0 }\r\nend\r\n"}, {"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n a = gets.split.map(&:to_i)\r\n f = true\r\n ans = 0\r\n a.each do |x|\r\n (ans += 1) if (x != 0 && f)\r\n f = x == 0 ? true : false\r\n end\r\n puts ans\r\nend"}], "src_uid": "da3d1b2615d51044a7b43bd0ce939f4c"} {"nl": {"description": "You have a given picture with size $$$w \\times h$$$. Determine if the given picture has a single \"+\" shape or not. A \"+\" shape is described below: A \"+\" shape has one center nonempty cell. There should be some (at least one) consecutive non-empty cells in each direction (left, right, up, down) from the center. In other words, there should be a ray in each direction. All other cells are empty. Find out if the given picture has single \"+\" shape.", "input_spec": "The first line contains two integers $$$h$$$ and $$$w$$$ ($$$1 \\le h$$$, $$$w \\le 500$$$) — the height and width of the picture. The $$$i$$$-th of the next $$$h$$$ lines contains string $$$s_{i}$$$ of length $$$w$$$ consisting \".\" and \"*\" where \".\" denotes the empty space and \"*\" denotes the non-empty space.", "output_spec": "If the given picture satisfies all conditions, print \"YES\". Otherwise, print \"NO\". You can output each letter in any case (upper or lower).", "sample_inputs": ["5 6\n......\n..*...\n.****.\n..*...\n..*...", "3 5\n..*..\n****.\n.*...", "7 7\n.......\n...*...\n..****.\n...*...\n...*...\n.......\n.*.....", "5 6\n..**..\n..**..\n******\n..**..\n..**..", "3 7\n.*...*.\n***.***\n.*...*.", "5 10\n..........\n..*.......\n.*.******.\n..*.......\n.........."], "sample_outputs": ["YES", "NO", "NO", "NO", "NO", "NO"], "notes": "NoteIn the first example, the given picture contains one \"+\".In the second example, two vertical branches are located in a different column.In the third example, there is a dot outside of the shape.In the fourth example, the width of the two vertical branches is $$$2$$$.In the fifth example, there are two shapes.In the sixth example, there is an empty space inside of the shape."}, "positive_code": [{"source_code": "H,W = gets.split.map(&:to_i)\npic = []\ncell = 0\nH.times do\n s = gets.chomp\n cell += s.count(\"*\")\n pic << s\nend\n\nflg = false\nver = false\n(H-2).times do |i|\n 1.upto(W-2) do |j|\n if pic[i][j] == \"*\"\n pic[i][j] = \".\"\n k = i+1\n while k < H && pic[k][j] == \"*\"\n pic[k][j] = \".\"\n if !ver && k < H-1 && pic[k][j-1] == \"*\" && pic[k][j+1] == \"*\" && pic[k+1][j] == \"*\"\n l,r = j-1,j+1\n while l >= 0 && pic[k][l] == \"*\"\n pic[k][l] = \".\"\n l -= 1\n end\n while r < W && pic[k][r] == \"*\"\n pic[k][r] = \".\"\n r += 1\n end\n ver = true\n end\n k += 1\n end\n flg = true\n break\n end\n end\n break if flg\nend\nemp = true\nH.times do |i|\n W.times do |j|\n emp = false if pic[i][j] == \"*\"\n end\nend\n#p pic\nputs emp && ver ? \"YES\" : \"NO\""}, {"source_code": "h, w = gets.split.map(&:to_i)\nf = h.times.map{gets.chomp}\nmemo_left = (0..h).map{[0] * w}.to_a\nmemo_up = (0..h).map{[0] * w}.to_a\nmemo_right = (0..h).map{[0] * w}.to_a\nmemo_down = (0..h).map{[0] * w}.to_a\ncnt = f.map{|s| s.count('*')}.inject(:+)\n\nh.times{|i|\n w.times{|j|\n next if f[i][j] != '*'\n memo_left[i][j] = (i > 0 ? memo_left[i - 1][j] : 0) + 1\n memo_up[i][j] = (j > 0 ? memo_up[i][j - 1] : 0) + 1\n }}\n(h-1).downto(0).each{|i|\n (w-1).downto(0).each{|j|\n next if f[i][j] != '*'\n memo_right[i][j] = (i < h - 1 ? memo_right[i + 1][j] : 0) + 1\n memo_down[i][j] = (j < w - 1 ? memo_down[i][j + 1] : 0) + 1\n }}\n\n\nh.times{|i|\n w.times{|j|\n next if f[i][j] != '*'\n a = memo_left[i][j]\n b = memo_right[i][j]\n c = memo_up[i][j]\n d = memo_down[i][j]\n if a > 1 and b > 1 and c > 1 and d > 1 and a + b + c + d - 3 == cnt\n puts :YES\n exit\n end\n }\n}\nputs :NO\n"}], "negative_code": [{"source_code": "h, w = gets.split.map(&:to_i)\nf = h.times.map{gets.chomp}\ncnt = f.map{|s| s.count('*')}.inject(:+)\nh.times{|i|\n w.times{|j|\n a = f[i].chars.drop_while{|x|x==?.}.take_while{|x|x==?*}.size\n b = (0...h).drop_while{|i|f[i][j]==?.}.take_while{|i|f[i][j]==?*}.size\n if a + b - 1 == cnt\n puts :YES\n exit\n end\n }\n}\nputs :NO\n"}, {"source_code": "h, w = gets.split.map(&:to_i)\nf = h.times.map{gets.chomp}\ncnt = f.map{|s| s.count('*')}.inject(:+)\nh.times{|i|\n w.times{|j|\n a = i.downto(0).take_while{|ii| f[ii][j] == ?*}.size\n b = i.upto(h-1).take_while{|ii| f[ii][j] == ?*}.size\n c = j.downto(0).take_while{|jj| f[i][jj] == ?*}.size\n d = j.upto(w-1).take_while{|jj| f[i][jj] == ?*}.size\n if a + b + c + d - 3 == cnt\n puts :YES\n exit\n end\n }\n}\nputs :NO\n"}], "src_uid": "6405161be280fea943201fa00ef6f448"} {"nl": {"description": "Polycarp calls an array dense if the greater of any two adjacent elements is not more than twice bigger than the smaller. More formally, for any $$$i$$$ ($$$1 \\le i \\le n-1$$$), this condition must be satisfied: $$$$$$\\frac{\\max(a[i], a[i+1])}{\\min(a[i], a[i+1])} \\le 2$$$$$$For example, the arrays $$$[1, 2, 3, 4, 3]$$$, $$$[1, 1, 1]$$$ and $$$[5, 10]$$$ are dense. And the arrays $$$[5, 11]$$$, $$$[1, 4, 2]$$$, $$$[6, 6, 1]$$$ are not dense.You are given an array $$$a$$$ of $$$n$$$ integers. What is the minimum number of numbers you need to add to an array to make it dense? You can insert numbers anywhere in the array. If the array is already dense, no numbers need to be added.For example, if $$$a=[4,2,10,1]$$$, then the answer is $$$5$$$, and the array itself after inserting elements into it may look like this: $$$a=[4,2,\\underline{\\textbf{3}},\\underline{\\textbf{5}},10,\\underline{\\textbf{6}},\\underline{\\textbf{4}},\\underline{\\textbf{2}},1]$$$ (there are other ways to build such $$$a$$$).", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 1000$$$). Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \\le n \\le 50$$$) — the length of the array $$$a$$$. The next line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le 50$$$).", "output_spec": "For each test case, output one integer — the minimum number of numbers that must be added to the array to make it dense.", "sample_inputs": ["6\n4\n4 2 10 1\n2\n1 3\n2\n6 1\n3\n1 4 2\n5\n1 2 3 4 3\n12\n4 31 25 50 30 20 34 46 42 16 15 16"], "sample_outputs": ["5\n1\n2\n1\n0\n3"], "notes": "NoteThe first test case is explained in the statements.In the second test case, you can insert one element, $$$a=[1,\\underline{\\textbf{2}},3]$$$.In the third test case, you can insert two elements, $$$a=[6,\\underline{\\textbf{4}},\\underline{\\textbf{2}},1]$$$.In the fourth test case, you can insert one element, $$$a=[1,\\underline{\\textbf{2}},4,2]$$$.In the fifth test case, the array $$$a$$$ is already dense."}, "positive_code": [{"source_code": "gets;$<.map{a=gets.split.map &:to_i;p (a.zip(a[1..])[..-2].map &:sort).map{|x,y|(-1-y/-x).to_s(2).size-1}.sum}"}, {"source_code": "alias g gets;g.to_i.times{g;a=g.split.map &:to_i;p (a.zip(a[1..])[..-2].map &:sort).map{|x,y|(-1-y/-x).to_s(2).size-1}.sum}"}, {"source_code": "alias g gets;g.to_i.times{g;a=g.split.map &:to_i;p (a.zip(a[1..])[..-2].map &:sort).map{|x, y|(-1-y/-x).to_s(2).size-1}.sum}"}, {"source_code": "t = gets.chomp.to_i\r\n\r\ndef getNumInsertions(smaller, bigger)\r\n nextElement = smaller\r\n count = 0\r\n until nextElement * 2 >= bigger\r\n nextElement *= 2\r\n count += 1\r\n end\r\n return count\r\nend\r\n\r\nt.times {\r\n n = gets.chomp.to_i\r\n a = gets.chomp.split.map(&:to_i)\r\n\r\n count = 0\r\n 0.upto(n - 2) { |i|\r\n smaller = a[i] < a[i + 1] ? a[i] : a[i + 1]\r\n bigger = a[i] > a[i + 1] ? a[i] : a[i + 1]\r\n \r\n count += getNumInsertions(smaller, bigger)\r\n }\r\n\r\n puts count\r\n}\r\n"}, {"source_code": "gets;$<.map{b=gets.split.map &:to_i;p (b.zip(b[1..])[..-2].map &:sort).map{|x,y|(-1-y/-x).to_s(2).size-1}.sum}"}, {"source_code": "# frozen_string_literal: true\r\n\r\ndef distance(numbers)\r\n a, b = numbers.minmax\r\n res = 0\r\n while a * 2 < b\r\n res += 1\r\n a *= 2\r\n end\r\n res\r\nend\r\n\r\ntest = gets.to_i\r\ntest.times do\r\n n = gets.chomp.to_i\r\n numbers = gets.chomp.split.map(&:to_i)\r\n result = (0..n-2).reduce(0) do |total, index|\r\n total + distance(numbers[index..index+1]) \r\n end\r\n puts result\r\nend\r\n"}, {"source_code": "gets.to_i.times do\n\tn = gets.to_i\n\ta = gets.split.map(&:to_i)\n\tputs a.each_cons(2).sum { |x, y|\n\t\t\tx, y = [x, y].sort\n\t\t\tres = 0\n\t\t\twhile x * 2 < y\n\t\t\t\tres += 1\n\t\t\t\tx *= 2\n\t\t\tend\n\t\t\tres\n\t }\nend\n"}, {"source_code": "def method(x, y)\r\n x, y = [x, y].minmax\r\n 0.step do |i|\r\n break i if x * 2 >= y\r\n x *= 2\r\n end\r\nend\r\n\r\nt = gets.to_i\r\nt.times do\r\n n = gets.to_i\r\n a = gets.split.map(&:to_i)\r\n puts a.each_cons(2).map { |x, y| method(x, y) }.sum\r\nend"}, {"source_code": "gets.to_i.times do\r\n n=gets.to_i\r\n a=gets.split.map(&:to_i)\r\n q=a[0]\r\n c=0\r\n a.each do |aa|\r\n x,y=[q,aa].sort\r\n while x*2= 2 || twos_count % 2 == 0\r\n return true\r\n else\r\n return false\r\n end\r\n end\r\nend\r\n\r\nt = gets.chomp().to_i\r\nt.times do |_|\r\n n = gets.chomp().to_i\r\n candies = gets.chomp().split(\"\").map(&:to_i)\r\n if is_fair_division?(candies, n)\r\n puts \"YES\"\r\n else\r\n puts \"NO\"\r\n end\r\nend\r\n"}, {"source_code": "gets.to_i.times do\r\nn=gets.to_i\r\na=gets.split(' ').map(&:to_i).sort\r\nif a.reduce(:+) % 2 == 1 || a[0] == 2 && n % 2 == 1\r\nputs 'NO'\r\nelse\r\nputs 'YES'\r\nend\r\nend"}, {"source_code": "gets.to_i.times {\n gets\n a = 0; b = 0\n gets.split.map!(&:to_i).each { _1 == 1 ? (a += 1) : (b += 1) }\n\n if b.odd?\n if a < 2\n puts \"No\"\n next\n end\n\n b += 1\n a -= 2\n end\n\n if a.odd?\n puts \"No\"\n next\n end\n\n puts \"Yes\"\n}\n"}, {"source_code": "def solve\r\n n = gets.to_i\r\n a = gets.split.map(&:to_i)\r\n\r\n one = a.count(1)\r\n two = n - one\r\n # STDERR.puts [one, two].inspect\r\n\r\n possible = (0 .. two).any? { |x|\r\n (two - 2 * x).abs * 2 <= one and one.even?\r\n }\r\n puts possible ? \"YES\" : \"NO\"\r\nend\r\n\r\nt = gets.to_i\r\nt.times do\r\n solve\r\nend"}, {"source_code": "t = gets.to_i\r\nt.times do\r\n n = gets.to_i\r\n a = gets.split.map { |v| v.to_i }\r\n puts a.sum.even? && (!a.count(1).zero? || a.count(2).even?) ? \"YES\" : \"NO\"\r\nend"}, {"source_code": "gets.to_i.times{gets;a=gets.count'1';puts a%2>0||a<$_.size%4?:NO:'YES'}"}, {"source_code": "a = gets.to_i\r\nfor i in 1..a do\r\n x = gets.to_i\r\n b = gets.chop.split(/ /).map(&:to_i)\r\n c = b.sum\r\n if c % 2 == 1\r\n print(\"NO\", \"\\n\")\r\n else\r\n if b.include? 1\r\n print(\"YES\", \"\\n\")\r\n else\r\n if x % 2 == 0\r\n print(\"YES\", \"\\n\")\r\n else\r\n print(\"NO\", \"\\n\")\r\n end\r\n end\r\n end\r\nend"}, {"source_code": "gets.to_i.times do\n\tn = gets.to_i\n\ta = gets.split.map(&:to_i)\n\tx, y = a.count(1), a.count(2)\n\tx -= 2 if y.odd?\n\tputs x.odd? || x < 0 ? 'NO' : 'YES'\nend\n"}, {"source_code": "t = gets.to_i\n\nt.times do\n n = gets.to_i\n a = gets.split.map(&:to_i).tally\n\n a[1] ||= 0\n a[2] ||= 0\n\n if a[2].odd?\n if a[1] > 0 && a[1].even?\n puts 'yes'\n else\n puts 'no'\n end\n else\n if a[1].odd?\n puts 'no'\n else\n puts 'yes'\n end\n end\nend"}, {"source_code": "#!/usr/bin/env ruby\r\n\r\ngets.to_i.times {\r\n n = gets.to_i\r\n sweets = gets.split.map &:to_i\r\n tv1 = sweets.select{|v| v == 1}.size\r\n tv2 = sweets.select{|v| v == 2}.size\r\n res = false\r\n (tv1 + 1).times do |v1|\r\n (tv2 + 1).times do |v2|\r\n g1 = v1 + v2 * 2\r\n g2 = (tv1 - v1) + (tv2 - v2) * 2\r\n #puts [g1, g2].to_s\r\n if g1 == g2\r\n res = true\r\n end\r\n end\r\n end\r\n\r\n puts res ? \"YES\" : \"NO\"\r\n}"}, {"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n a = gets.split.map &:to_i\r\n cnt = [0] * 3\r\n a.each do |i|\r\n cnt[i] += 1\r\n end\r\n if cnt[2] % 2 == 1 && (cnt[1] < 2 || (cnt[1] - 2) % 2 == 1) || cnt[2] % 2 == 0 && cnt[1] % 2 == 1\r\n puts \"NO\"\r\n else\r\n puts \"YES\"\r\n end\r\nend"}, {"source_code": "t = gets.chomp.to_i\r\nt.times do\r\n n = gets.chomp.to_i\r\n a = gets.chomp.split(\" \").map(&:to_i)\r\n hash = Hash.new(0)\r\n ans = 0\r\n a.each do |i|\r\n hash[i] += 1\r\n ans += i\r\n end\r\n if ans % 2 != 0\r\n puts \"NO\"\r\n else\r\n if hash[1] >= 1 \r\n puts \"YES\"\r\n elsif hash[2] % 2 != 0\r\n puts \"NO\"\r\n else\r\n puts \"YES\"\r\n end\r\n end\r\nend"}, {"source_code": "require 'prime'\r\nt = gets.chomp.to_i\r\nt.times do\r\n n = gets.chomp.to_i\r\n arr = gets.chomp.split(' ').map(&:to_i)\r\n if arr.sum % 2 != 0 || arr.sum % 2 == 0 && arr.count(1) == 0 && arr.count(2) % 2 != 0\r\n puts \"NO\"\r\n else\r\n puts \"YES\"\r\n end\r\nend"}, {"source_code": "gets.to_i.times do\r\n n=gets.to_i\r\n a=gets.split.map(&:to_i)\r\n s=a.sum\r\n if s.odd?\r\n puts \"NO\"\r\n next\r\n end\r\n s2=s/2\r\n if s2.even?\r\n puts \"YES\"\r\n elsif a.include?(1)\r\n puts \"YES\"\r\n else\r\n puts \"NO\"\r\n end\r\nend\r\n"}], "negative_code": [{"source_code": "def is_fair_division?(candies, n)\r\n return true if n == 0\r\n\r\n ones_sum = 0\r\n twos_sum = 0\r\n candies.each do |candy|\r\n if candy == 1\r\n ones_sum += 1\r\n elsif candy == 2\r\n twos_sum += 2\r\n end\r\n end\r\n\r\n return (ones_sum % 2 == 0) && (twos_sum % 2 == 0)\r\nend\r\n\r\nt = gets.chomp().to_i\r\nt.times do |_|\r\n n = gets.chomp().to_i\r\n candies = gets.chomp().split(\"\").map(&:to_i)\r\n if is_fair_division?(candies, n)\r\n puts \"YES\"\r\n else\r\n puts \"NO\"\r\n end\r\nend"}, {"source_code": "t = gets.to_i\n\nt.times do\n n = gets.to_i\n a = gets.split.map(&:to_i).tally\n\n a[1] ||= 0\n a[2] ||= 0\n\n if (2 * a[2] + a[1]).odd?\n puts 'no'\n elsif a[2] <= a[1]\n puts 'yes'\n elsif (a[2] - 2 * a[1]).even?\n puts 'yes'\n else\n puts 'no'\n end\nend"}, {"source_code": "t = gets.to_i\n\nt.times do\n n = gets.to_i\n a = gets.split.map(&:to_i).tally\n\n a[1] ||= 0\n a[2] ||= 0\n\n if a[1].even? && a[2].even?\n puts 'yes'\n else\n puts 'no'\n end\nend"}], "src_uid": "1d9d34dca11a787d6e2345494680e717"} {"nl": {"description": "Anya loves to watch horror movies. In the best traditions of horror, she will be visited by m ghosts tonight. Anya has lots of candles prepared for the visits, each candle can produce light for exactly t seconds. It takes the girl one second to light one candle. More formally, Anya can spend one second to light one candle, then this candle burns for exactly t seconds and then goes out and can no longer be used.For each of the m ghosts Anya knows the time at which it comes: the i-th visit will happen wi seconds after midnight, all wi's are distinct. Each visit lasts exactly one second.What is the minimum number of candles Anya should use so that during each visit, at least r candles are burning? Anya can start to light a candle at any time that is integer number of seconds from midnight, possibly, at the time before midnight. That means, she can start to light a candle integer number of seconds before midnight or integer number of seconds after a midnight, or in other words in any integer moment of time.", "input_spec": "The first line contains three integers m, t, r (1 ≤ m, t, r ≤ 300), representing the number of ghosts to visit Anya, the duration of a candle's burning and the minimum number of candles that should burn during each visit. The next line contains m space-separated numbers wi (1 ≤ i ≤ m, 1 ≤ wi ≤ 300), the i-th of them repesents at what second after the midnight the i-th ghost will come. All wi's are distinct, they follow in the strictly increasing order.", "output_spec": "If it is possible to make at least r candles burn during each visit, then print the minimum number of candles that Anya needs to light for that. If that is impossible, print  - 1.", "sample_inputs": ["1 8 3\n10", "2 10 1\n5 8", "1 1 3\n10"], "sample_outputs": ["3", "1", "-1"], "notes": "NoteAnya can start lighting a candle in the same second with ghost visit. But this candle isn't counted as burning at this visit.It takes exactly one second to light up a candle and only after that second this candle is considered burning; it means that if Anya starts lighting candle at moment x, candle is buring from second x + 1 to second x + t inclusively.In the first sample test three candles are enough. For example, Anya can start lighting them at the 3-rd, 5-th and 7-th seconds after the midnight.In the second sample test one candle is enough. For example, Anya can start lighting it one second before the midnight.In the third sample test the answer is  - 1, since during each second at most one candle can burn but Anya needs three candles to light up the room at the moment when the ghost comes."}, "positive_code": [{"source_code": "# $stdin = File.open('in.txt')\n\nm, t, r = gets.split.map(&:to_i)\nw = gets.split.map(&:to_i)\n\nmp = Hash.new(0)\nneed = Hash.new(0)\nw.each { |x| need[x] = r }\n\nans = 0\n\n(-305..305).each {|x|\n put = 0\n\n ((x+1)..(x+t)).each {|y|\n if mp[y] < need[y]\n put = 1 if need[y] - mp[y] > y - x - 1\n end\n }\n\n if put == 1\n ((x+1)..(x+t)).each {|x| mp[x] += 1}\n ans += 1\n end\n}\n\n(0..305).each {|x|\n ans = -1 if mp[x] < need[x]\n}\n\np ans"}], "negative_code": [{"source_code": "# $stdin = File.open('in.txt')\n\nm, t, r = gets.split.map(&:to_i)\nw = gets.split.map(&:to_i)\n\nmp = Hash.new(0)\nneed = Hash.new(0)\nw.each { |x| need[x] = r }\n\nans = 0\n\n(-300..300).each {|x|\n put = 0\n\n ((x+1)..(x+t)).each {|y|\n if mp[y] < need[y]\n put = 1 if need[y]-mp[y] >= y - x - 1\n end\n }\n\n if put == 1\n ((x+1)..(x+t)).each {|x| mp[x] += 1}\n ans += 1\n end\n}\n\n(0..300).each {|x|\n ans = -1 if mp[x] < need[x]\n}\n\np ans"}, {"source_code": "# $stdin = File.open('in.txt')\n\nm, t, r = gets.split.map(&:to_i)\nw = gets.split.map(&:to_i)\n\nmp = Hash.new(0)\nneed = Hash.new(0)\nw.each { |x| need[x] = r }\n\nans = 0\n\n(-305..305).each {|x|\n put = 0\n\n ((x+1)..(x+t)).each {|y|\n if mp[y] < need[y]\n put = 1 if need[y]-mp[y] >= y - x - 1\n end\n }\n\n if put == 1\n ((x+1)..(x+t)).each {|x| mp[x] += 1}\n ans += 1\n end\n}\n\n(0..305).each {|x|\n ans = -1 if mp[x] < need[x]\n}\n\np ans"}], "src_uid": "03772bac6ed5bfe75bc0ad4d2eab56fd"} {"nl": {"description": "One day, after a difficult lecture a diligent student Sasha saw a graffitied desk in the classroom. She came closer and read: \"Find such positive integer n, that among numbers n + 1, n + 2, ..., 2·n there are exactly m numbers which binary representation contains exactly k digits one\".The girl got interested in the task and she asked you to help her solve it. Sasha knows that you are afraid of large numbers, so she guaranteed that there is an answer that doesn't exceed 1018.", "input_spec": "The first line contains two space-separated integers, m and k (0 ≤ m ≤ 1018; 1 ≤ k ≤ 64).", "output_spec": "Print the required number n (1 ≤ n ≤ 1018). If there are multiple answers, print any of them.", "sample_inputs": ["1 1", "3 2"], "sample_outputs": ["1", "5"], "notes": null}, "positive_code": [{"source_code": "def c(n, k)\n\tif n < k\n\t\t0\n\telse\n\t\tp = 1\n\t\t((n - k + 1)..n).each { |i|\n\t\t\tp *= i\n\t\t}\n\t\t(1..k).each { |i|\n\t\t\tp /= i\n\t\t}\n\t\tp\n\tend\nend\n\nn = 1\nm, k = gets.split.map(&:to_i)\nk -= 1\nloop {\n\tif m == 0\n\t\tbreak\n\tend\n\tloop.with_index { |_, i|\n\t\tif c(i, k) >= m\n\t\t\tm -= c(i - 1, k)\n\t\t\tk -= 1\n\t\t\tn += 1<<(i - 1)\n\t\t\tbreak\n\t\tend\n\t}\n}\n\nputs n"}], "negative_code": [], "src_uid": "3845031bffe6475d3da4858725b70ec3"} {"nl": {"description": "You are given two integers $$$n$$$ and $$$k$$$.Your task is to construct such a string $$$s$$$ of length $$$n$$$ that for each $$$i$$$ from $$$1$$$ to $$$k$$$ there is at least one $$$i$$$-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer $$$t$$$ independent queries.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of queries. The next $$$t$$$ lines are contain queries, one per line. The $$$i$$$-th line contains two integers $$$n_i$$$ and $$$k_i$$$ ($$$1 \\le n_i \\le 100, 1 \\le k_i \\le min(n_i, 26)$$$) — the length of the string in the $$$i$$$-th query and the number of characters in the $$$i$$$-th query.", "output_spec": "Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: any string $$$s_i$$$ satisfying the conditions in the problem statement with constraints from the $$$i$$$-th query.", "sample_inputs": ["3\n7 3\n4 4\n6 2"], "sample_outputs": ["cbcacab\nabcd\nbaabab"], "notes": "NoteIn the first example query the maximum possible minimal frequency is $$$2$$$, it can be easily seen that the better answer doesn't exist. Other examples of correct answers: \"cbcabba\", \"ccbbaaa\" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is $$$1$$$).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is $$$3$$$)."}, "positive_code": [{"source_code": "gets\n$<.each{|e|n,k=e.split.map &:to_i;puts n.times.map{|i|(97+i%k).chr}*''}"}, {"source_code": "t = gets.to_i\nlet = [\"a\",\"b\",\"c\",\"d\",\"e\",\"f\",\"g\",\"h\",\"i\",\"j\",\"k\",\"l\",\"m\",\"n\",\"o\",\"p\",\"q\",\"r\",\"s\",\"t\",\"u\",\"v\",\"w\",\"x\",\"y\",\"z\"]\nt.times do\n n,k = gets.split.map(&:to_i)\n puts (let[0,k]*(n/k) + let[0,n%k]).join\nend\n"}, {"source_code": "\n\nALPHA = ('a'..'z').to_a\ndef solution\n count = read_int\n count.times do\n n, k = read_ints\n puts ALPHA[0...k].cycle.first(n).join(\"\")\n end\n\nend\n\ndef read_int\n gets.to_i\nend\n\ndef read_ints\n gets.split.map(&:to_i)\nend\n\ndef read_string\n gets.chomp\nend\n\nsolution unless ENV['TEST__MODE']\n\n\n"}, {"source_code": "# Functions\ndef handle(n, k)\n sample = \"\"\n k.times { |i| sample << (97 + i).chr }\n s = sample * (n / k)\n s << sample.slice(0, n % k)\n return s\nend\n\n\n# MAIN\nt = gets.to_i # 1 <= t <= 100\n\n\nt.times do |i|\n str = gets.split(\" \")\n n = str[0].to_i # 1 <= n <= 100\n k = str[1].to_i # 1 <= k <= min(n_i, 26)\n puts handle(n, k)\nend\n"}, {"source_code": "t = gets.chomp.to_i\nalph = \"abcdefghijklmnopqrstuvwxyz\"\nt.times do |testcase|\n (n,k) = gets.split.map(&:to_i)\n cur = 0\n res = \"\"\n n.times do |i|\n if cur == k\n cur = 0\n end\n res< 0\n ans += alp_arr[0..mod-1].join\n end\n puts ans\nend"}, {"source_code": "n = gets.to_i\n(1..n).each do |i|\n a, b = gets.split.map(&:to_i)\n puts (('a'..('a'.ord + b - 1).chr).to_a.join * 100)[0..a - 1]\nend"}, {"source_code": "s = \"abcdefghijklmnopqrstuvwxyz\"\ngets.to_i.times do\n n,k = gets.split.map(&:to_i)\n ans = \"\"\n (n/k).times do\n ans += s[0,k]\n end\n ans += s[0,n%k]\n puts ans\nend"}, {"source_code": "# https://codeforces.com/problemset/problem/1092/A\n\ndef solution(n, k)\n s = \"\"\n j = 0\n n.times do |i|\n s += ((i % k) + 97).chr\n end\n return s\nend\n\nq = gets.chomp.to_i\nq.times do\n n, k = gets.chomp.split(\" \").map(&:to_i)\n puts solution(n, k)\nend\n"}, {"source_code": "t = gets.to_i\nt.times do\n n, k = gets.split.map(&:to_i)\n s = ''\n i = 0\n n.times do\n s += ('a'.ord + i).chr\n i += 1\n i %= k\n end\n puts s\nend\n"}, {"source_code": "# \n\ndef f(n,k)\n code_a=97\n ans=[]\n for i in 0..n-1\n ans<0\n\t\ta[i][i]=1\n\t\tk-=1\n\tend\n\t(i+1...n).each{|j|\n\t\tif k>1\n\t\t\ta[i][j]=a[j][i]=1\n\t\t\tk-=2\n\t\tend\n\t}\n}\nputs a.map{|e|e*' '}"}], "negative_code": [{"source_code": "#!/usr/bin/ruby\nn,k=gets.split.map(&:to_i)\nif n*n1\n\t\t\ta[i][j]=a[j][i]=1\n\t\t\tk-=2\n\t\tend\n\t}\n}\n(0...n).each{|i|\n\tif k>0\n\t\ta[i][i]=1\n\t\tk-=1\n\tend\n}\nputs a.map{|e|e*' '}"}], "src_uid": "d5a328cfa1e4d26ff007813ad02991ac"} {"nl": {"description": "Polycarpus has an array, consisting of n integers a1, a2, ..., an. Polycarpus likes it when numbers in an array match. That's why he wants the array to have as many equal numbers as possible. For that Polycarpus performs the following operation multiple times: he chooses two elements of the array ai, aj (i ≠ j); he simultaneously increases number ai by 1 and decreases number aj by 1, that is, executes ai = ai + 1 and aj = aj - 1. The given operation changes exactly two distinct array elements. Polycarpus can apply the described operation an infinite number of times. Now he wants to know what maximum number of equal array elements he can get if he performs an arbitrary number of such operation. Help Polycarpus.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 105) — the array size. The second line contains space-separated integers a1, a2, ..., an (|ai| ≤ 104) — the original array.", "output_spec": "Print a single integer — the maximum number of equal array elements he can get if he performs an arbitrary number of the given operation.", "sample_inputs": ["2\n2 1", "3\n1 4 1"], "sample_outputs": ["1", "3"], "notes": null}, "positive_code": [{"source_code": "gets.chomp\nsum = 0\na = gets.chomp.split(\" \").map {|x| x.to_i}.sort\nfor i in a\n sum += i\nend\nif sum % a.length == 0\n puts a.length\nelse\n puts a.length - 1\nend\n\n"}, {"source_code": "n,*a=gets(p).split.map(&:to_i)\np a.inject(:+)%n<1?n:~-n\n"}, {"source_code": "n=gets.to_i\nsum=0\ngets.split.map(&:to_i).each{|el|sum+=el}\n(n==1) ? (puts 1) : ((sum%n ==0)? ( puts n) : (puts n-1))\n"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split(\" \").map{|n| n.to_i}\nsum = a.inject{|sum, n| sum += n}\n\nif sum % n == 0\n puts n\nelse\n puts [n - 1, 1].max\nend\n"}, {"source_code": "n,*a=gets(p).split.map(&:to_i)\np a.inject(:+)%n<1?n:~-n\n"}, {"source_code": "n,*a=gets(p).split.map(&:to_i)\np a.inject(:+)%n<1?n:~-n"}, {"source_code": "n,*a=gets(p).split.map(&:to_i)\np a.inject(:+)%n<1?n:~-n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\ns = a.inject(:+) / n\nputs a.inject(0) {|acc, x| acc + x - s }.zero? ? n : n-1"}, {"source_code": "n,*a=gets(p).split.map(&:to_i)\np a.inject(:+)%n<1?n:~-n\n"}, {"source_code": "n,*a=gets(p).split.map(&:to_i)\np a.inject(:+)%n<1?n:~-n\n"}, {"source_code": "n,*a=gets(p).split.map(&:to_i)\np a.inject(:+)%n<1?n:~-n\n"}, {"source_code": "n = gets.to_i\na = gets.split.collect{|i| i.to_i}\nif a.inject(0){|sum,i| sum+i}%n == 0\n puts n\nelse\n puts n-1\nend\n"}, {"source_code": "n = gets.to_i\na = gets.split.map &:to_i\nsum = a.inject{|sum, el| sum+el}\nputs sum%n==0 ? n : n-1"}, {"source_code": "n,*a=gets(p).split.map(&:to_i)\np a.inject(:+)%n<1?n:~-n\n"}, {"source_code": "n = gets.to_i\na = gets.split.map { |x| x.to_i }\nputs(a.inject(:+) % n == 0 ? n : n - 1)"}, {"source_code": "n,*a=gets(p).split.map(&:to_i)\np a.inject(:+)%n<1?n:~-n\n"}, {"source_code": "n,*a=gets(p).split.map(&:to_i)\np a.inject(:+)%n<1?n:~-n"}, {"source_code": "n = gets.to_i\na = gets.split.collect{|x| x.to_i}\n\nsum = 0\na.each{|x| sum += x}\n\nif sum % n == 0\n\tputs n\nelse \n\tputs n-1\nend"}, {"source_code": "n = gets.to_i\na = gets.split.map { |x| x.to_i }\n\nsum = a.reduce(:+)\nif sum % n == 0\n\tputs n\nelse\n\tputs n - 1\nend"}, {"source_code": "n,*a=gets(p).split.map(&:to_i)\np a.inject(:+)%n<1?n:~-n\n"}], "negative_code": [{"source_code": "sum = 0\ngets.chomp\nmin = 100000\ncheck = false\na = gets.chomp.split(\" \").each {|x| \n x = x.to_i\n sum += x\n min = x if x < min\n if x < 0\n check = true\n end\n}\nsum += -min * a.length if check\nif sum / a.length == 0\n puts a.length\nelse\n puts sum / a.length\nend\n"}, {"source_code": "sum = 0\ngets.chomp\nmin = 100000\ncheck = false\na = gets.chomp.split(\" \").each {|x| \n x = x.to_i\n sum += x\n min = x if x < min\n if x < 0\n check = true\n end\n}\nsum += -min * a.length if check\nif sum % a.length == 0\n puts a.length\nelse\n puts sum / a.length\nend\n"}, {"source_code": "sum = 0\ngets.chomp\ngets.chomp.split(\" \").each {|x| sum += x.to_i}\nputs sum / 2\n"}, {"source_code": "n=gets.to_i\nsum=0\ngets.split.map(&:to_i).each{|el|sum+=el}\nputs sum/2\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i).sort\nwhile a[-1]-a[0] >= 2 do\n a[0]+=1; a[-1]-=1; a.sort!\n #p a\nend\np a.uniq.map {|x| a.count(x) }.inject(:+)-1"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i).sort\nwhile a[-1]-a[0] >= 2 do\n a[0]+=1; a[-1]-=1; a.sort!\nend\nputs a.uniq.map {|x| a.count(x) }.max"}], "src_uid": "71cead8cf45126902c518c9ce6e5e186"} {"nl": {"description": "You have $$$n$$$ stacks of blocks. The $$$i$$$-th stack contains $$$h_i$$$ blocks and it's height is the number of blocks in it. In one move you can take a block from the $$$i$$$-th stack (if there is at least one block) and put it to the $$$i + 1$$$-th stack. Can you make the sequence of heights strictly increasing?Note that the number of stacks always remains $$$n$$$: stacks don't disappear when they have $$$0$$$ blocks.", "input_spec": "First line contains a single integer $$$t$$$ $$$(1 \\leq t \\leq 10^4)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \\leq n \\leq 100)$$$. The second line of each test case contains $$$n$$$ integers $$$h_i$$$ $$$(0 \\leq h_i \\leq 10^9)$$$ — starting heights of the stacks. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$.", "output_spec": "For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).", "sample_inputs": ["6\n2\n1 2\n2\n1 0\n3\n4 4 4\n2\n0 0\n3\n0 1 0\n4\n1000000000 1000000000 1000000000 1000000000"], "sample_outputs": ["YES\nYES\nYES\nNO\nNO\nYES"], "notes": "NoteIn the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become $$$0$$$ $$$1$$$.In the third test case we could move one block from the first stack to the second and then from the second to the third, which would make the heights $$$3$$$ $$$4$$$ $$$5$$$.In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights $$$0$$$ $$$0$$$ $$$1$$$. Both $$$0$$$ $$$1$$$ $$$0$$$ and $$$0$$$ $$$0$$$ $$$1$$$ are not increasing sequences, so the answer is NO."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n a = gets.split.map &:to_i\r\n need, cur, ok = 0, 0, true\r\n n.times do |i|\r\n need += i\r\n cur += a[i]\r\n ok = false if cur < need\r\n end\r\n puts ok ? \"YES\" : \"NO\"\r\nend"}, {"source_code": "cases = gets.chomp.to_i\n\ncases.times do |i|\n n = gets.chomp.to_i\n hs = gets.chomp.split(\" \").map(&:to_i)\n\n failed = false\n hs.each_with_index do |h, idx|\n if h < idx\n failed = true\n break\n else\n hs[(idx + 1)] += h - idx if hs[(idx + 1)]\n end\n end\n\n puts failed ? \"NO\" : \"YES\"\nend"}], "negative_code": [{"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n a = gets.split.map &:to_i\r\n n, h, res = 0, 0, true\r\n n.times do |i|\r\n n += i\r\n h += a[i]\r\n res = false if h < n\r\n end\r\n puts res ? \"YES\" : \"NO\"\r\nend"}], "src_uid": "7a8c4ba98a77097faff625b94889b365"} {"nl": {"description": "You are walking through a parkway near your house. The parkway has $$$n+1$$$ benches in a row numbered from $$$1$$$ to $$$n+1$$$ from left to right. The distance between the bench $$$i$$$ and $$$i+1$$$ is $$$a_i$$$ meters.Initially, you have $$$m$$$ units of energy. To walk $$$1$$$ meter of distance, you spend $$$1$$$ unit of your energy. You can't walk if you have no energy. Also, you can restore your energy by sitting on benches (and this is the only way to restore the energy). When you are sitting, you can restore any integer amount of energy you want (if you sit longer, you restore more energy). Note that the amount of your energy can exceed $$$m$$$.Your task is to find the minimum amount of energy you have to restore (by sitting on benches) to reach the bench $$$n+1$$$ from the bench $$$1$$$ (and end your walk).You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n \\le 100$$$; $$$1 \\le m \\le 10^4$$$). The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le 100$$$), where $$$a_i$$$ is the distance between benches $$$i$$$ and $$$i+1$$$.", "output_spec": "For each test case, print one integer — the minimum amount of energy you have to restore (by sitting on benches) to reach the bench $$$n+1$$$ from the bench $$$1$$$ (and end your walk) in the corresponding test case.", "sample_inputs": ["3\n\n3 1\n\n1 2 1\n\n4 5\n\n3 3 5 2\n\n5 16\n\n1 2 3 4 5"], "sample_outputs": ["3\n8\n0"], "notes": "NoteIn the first test case of the example, you can walk to the bench $$$2$$$, spending $$$1$$$ unit of energy, then restore $$$2$$$ units of energy on the second bench, walk to the bench $$$3$$$, spending $$$2$$$ units of energy, restore $$$1$$$ unit of energy and go to the bench $$$4$$$.In the third test case of the example, you have enough energy to just go to the bench $$$6$$$ without sitting at all."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n n, m = gets.split.map(&:to_i)\r\n puts [gets.split.map(&:to_i).sum - m, 0].max\r\nend"}, {"source_code": "gets.to_i.times do\r\n n, m = gets.split.map(&:to_i)\r\n puts([gets.split.map(&:to_i).sum - m, 0].max)\r\nend"}, {"source_code": "gets.to_i.times do\r\n n, m = gets.split.map(&:to_i)\r\n x = 0\r\n w = gets.split.map(&:to_i)\r\n for item in w\r\n x += item\r\n end\r\n puts((x <= m )? 0 : x - m)\r\nend"}, {"source_code": "gets.to_i.times do\r\n n, m = gets.split.map(&:to_i)\r\n as = gets.split.map(&:to_i)\r\n p [as.sum - m, 0].max\r\nend\r\n"}, {"source_code": "def solve(xs, m)\n total = xs.sum\n [total - m, 0].max\nend\n\nt = gets.chomp.to_i\n\nresults = []\n\nt.times do\n n, m = gets.chomp.split.map(&:to_i)\n xs = gets.chomp.split.map(&:to_i)\n results << solve(xs, m)\nend\n\nputs results\n"}, {"source_code": "t = gets.to_i\r\n\r\nt.times do\r\n n, m = gets.split.map(&:to_i)\r\n a = gets.split.map(&:to_i)\r\n\r\n ans = 0\r\n n.times do |i|\r\n if a[i] > m\r\n ans += a[i]-m\r\n m += a[i]-m\r\n end\r\n\r\n m -= a[i]\r\n end\r\n\r\n puts ans\r\nend\r\n"}, {"source_code": "gets.to_i.times do\n n,m = gets.chomp.split.map(&:to_i)\n a = gets.chomp.split.map(&:to_i)\n ans = 0\n puts [a.reduce(:+) - m,0].max\nend\n"}], "negative_code": [], "src_uid": "7276d7e3a4b594dc64c1ba56fb1a3628"} {"nl": {"description": "AquaMoon and Cirno are playing an interesting game with arrays. Cirno has prepared two arrays $$$a$$$ and $$$b$$$, both consist of $$$n$$$ non-negative integers. AquaMoon can perform the following operation an arbitrary number of times (possibly zero): She chooses two indices $$$i$$$ and $$$j$$$ ($$$1 \\le i, j \\le n$$$), then decreases the $$$i$$$-th element of array $$$a$$$ by $$$1$$$, and increases the $$$j$$$-th element of array $$$a$$$ by $$$1$$$. The resulting values at $$$i$$$-th and $$$j$$$-th index of array $$$a$$$ are $$$a_i - 1$$$ and $$$a_j + 1$$$, respectively. Each element of array $$$a$$$ must be non-negative after each operation. If $$$i = j$$$ this operation doesn't change the array $$$a$$$. AquaMoon wants to make some operations to make arrays $$$a$$$ and $$$b$$$ equal. Two arrays $$$a$$$ and $$$b$$$ are considered equal if and only if $$$a_i = b_i$$$ for all $$$1 \\leq i \\leq n$$$.Help AquaMoon to find a sequence of operations that will solve her problem or find, that it is impossible to make arrays $$$a$$$ and $$$b$$$ equal.Please note, that you don't have to minimize the number of operations.", "input_spec": "The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 100$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 100$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$0 \\leq a_i \\leq 100$$$). The sum of all $$$a_i$$$ does not exceed $$$100$$$. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \\dots, b_n$$$ ($$$0 \\leq b_i \\leq 100$$$). The sum of all $$$b_i$$$ does not exceed $$$100$$$.", "output_spec": "For each test case print \"-1\" on the only line if it is impossible to make two arrays equal with some sequence of operations. Otherwise, print an integer $$$m$$$ ($$$0 \\leq m \\leq 100$$$) in the first line — the number of operations. Then print $$$m$$$ lines, each line consists of two integers $$$i$$$ and $$$j$$$ — the indices you choose for the operation. It can be proven that if it is possible to make two arrays equal with some sequence of operations, there exists a sequence with $$$m \\leq 100$$$. If there are multiple possible solutions, you can print any.", "sample_inputs": ["4\n4\n1 2 3 4\n3 1 2 4\n2\n1 3\n2 1\n1\n0\n0\n5\n4 3 2 1 0\n0 1 2 3 4"], "sample_outputs": ["2\n2 1\n3 1\n-1\n0\n6\n1 4\n1 4\n1 5\n1 5\n2 5\n2 5"], "notes": "NoteIn the first example, we do the following operations: $$$i = 2$$$, $$$j = 1$$$: $$$[1, 2, 3, 4] \\rightarrow [2, 1, 3, 4]$$$; $$$i = 3$$$, $$$j = 1$$$: $$$[2, 1, 3, 4] \\rightarrow [3, 1, 2, 4]$$$; In the second example, it's impossible to make two arrays equal."}, "positive_code": [{"source_code": "def read_array()\n gets.split(\" \").map { |s| s.to_i }\nend\nt = gets.to_i\nfor z in 1..t\n n = gets.to_i\n a, b = read_array, read_array\n m, p = [], []\n for i in 0...n\n x = a[i] - b[i]\n if a[i] > b[i]\n m << [i + 1, a[i] - b[i]]\n elsif a[i] < b[i]\n p << [i + 1, b[i] - a[i]]\n end\n end\n res = []\n while p.length() > 0 && m.length() > 0\n x = [p[-1][1], m[-1][1]].min\n p[-1][1] -= x\n m[-1][1] -= x\n while x > 0\n x -= 1\n res << [m[-1][0], p[-1][0]]\n end\n if p[-1][1] == 0\n p.pop()\n end\n if m[-1][1] == 0\n m.pop()\n end\n end\n if p.length() != 0 || m.length() != 0\n puts(-1)\n next\n end\n puts res.length()\n for i in res\n puts i[0].to_s + ' ' + i[1].to_s\n end\nend\n"}, {"source_code": "\ndef solve(a, b)\n return -1 unless a.reduce(&:+) == b.reduce(&:+)\n ans = []\n 0.upto (a.size - 1) do |i|\n if a[i] < b[i]\n diff = b[i] - a[i]\n for j in (i+1)..(a.size - 1) do\n break if diff == 0\n next if a[j] < b[j]\n cnt = [diff, a[j] - b[j]].min\n cnt.times do\n ans << [j+1, i+1]\n diff -= 1\n a[i] += 1\n a[j] -= 1\n end\n end\n\n elsif a[i] > b[i]\n diff = a[i] - b[i]\n for j in (i+1)..(a.size - 1) do\n break if diff == 0\n next if a[j] > b[j]\n cnt = [diff, b[j] - a[j]].min\n cnt.times do \n ans << [i+1, j+1]\n diff -= 1\n a[i] -= 1\n a[j] += 1\n end\n end\n end\n end\n\n ans\nend\n\nt = gets.chomp.to_i\nt.times do\n gets\n a = gets.chomp.split(' ').map(&:to_i)\n b = gets.chomp.split(' ').map(&:to_i)\n\n ans = solve(a, b)\n if ans == -1\n puts \"-1\\n\" \n else\n puts \"#{ans.size}\\n\"\n for i, j in ans do\n puts \"#{i} #{j}\\n\"\n end\n end\nend\n\n"}], "negative_code": [], "src_uid": "accfbd68b9723abf4cd29846e80835ec"} {"nl": {"description": "Ever since Kalevitch, a famous Berland abstractionist, heard of fractals, he made them the main topic of his canvases. Every morning the artist takes a piece of graph paper and starts with making a model of his future canvas. He takes a square as big as n × n squares and paints some of them black. Then he takes a clean square piece of paper and paints the fractal using the following algorithm: Step 1. The paper is divided into n2 identical squares and some of them are painted black according to the model.Step 2. Every square that remains white is divided into n2 smaller squares and some of them are painted black according to the model.Every following step repeats step 2. Unfortunately, this tiresome work demands too much time from the painting genius. Kalevitch has been dreaming of making the process automatic to move to making 3D or even 4D fractals.", "input_spec": "The first line contains integers n and k (2 ≤ n ≤ 3, 1 ≤ k ≤ 5), where k is the amount of steps of the algorithm. Each of the following n lines contains n symbols that determine the model. Symbol «.» stands for a white square, whereas «*» stands for a black one. It is guaranteed that the model has at least one white square. ", "output_spec": "Output a matrix nk × nk which is what a picture should look like after k steps of the algorithm.", "sample_inputs": ["2 3\n.*\n..", "3 2\n.*.\n***\n.*."], "sample_outputs": [".*******\n..******\n.*.*****\n....****\n.***.***\n..**..**\n.*.*.*.*\n........", ".*.***.*.\n*********\n.*.***.*.\n*********\n*********\n*********\n.*.***.*.\n*********\n.*.***.*."], "notes": null}, "positive_code": [{"source_code": "def gao(i, j, m, n)\n\tm /= n\n\tm > 0 && ($a[i / m][j / m] == '*' || gao(i % m, j % m, m, n))\nend\n\nI = open('input.txt')\nO = open('output.txt', 'w')\nn, k = I.gets.split.map(&:to_i)\n$a = n.times.map{I.gets}\nm = n ** k\nO.puts m.times.map{|i| m.times.map{|j| gao(i, j, m, n) ? '*' : '.'} * ''} * \"\\n\"\n"}, {"source_code": "open(\"input.txt\"){|f|open(\"output.txt\",\"w\"){|g|n,k=f.gets.split.map(&:to_i)\na,s=0..n**k-1,[*f]\ng< down\n arry[0] = 1\n arry[1] =3\n for i in 2..up\n arry[i] = arry[i-1]+1\n end\n for j in up+1..down+up\n arry[j] = arry[j-1]-1\n arry[j] = arry[j] == 3 ? 2 : arry[j]\n end\nelse\n arry[0] = 2\n for i in 1..up\n arry[i] = arry[i-1]+1\n end\n\n for j in up+1..down+up\n arry[j] = arry[j-1]-1 \n arry[j] = arry[j] == 2 ? 1 : arry[j]\n end\nend\n\nfor k in 0..down+up-1\n printf(\"#{arry[k]} \")\nend\nputs arry[down+up]\n"}, {"source_code": "up = Integer gets \ndown = Integer gets\nheight = up + down + 1\narry = []\nif up > down\n arry[0] = 1\n arry[1] =3\n for i in 2..up\n arry[i] = arry[i-1]+1\n end\n for j in up+1..down+up\n arry[j] = arry[j-1]-1\n end\nelse\n arry[0] = 2\n for i in 1..up\n arry[i] = arry[i-1]+1\n end\n for j in up+1..down+up\n arry[j] = arry[j-1]-1 \n end\nend\n\nfor k in 0..down+up-1\n printf(\"#{arry[k]} \")\nend\nputs arry[down+up]\n\n"}], "src_uid": "add2285f3f710eb41185d6a83e44b37a"} {"nl": {"description": "Luis has a sequence of $$$n+1$$$ integers $$$a_1, a_2, \\ldots, a_{n+1}$$$. For each $$$i = 1, 2, \\ldots, n+1$$$ it is guaranteed that $$$0\\leq a_i < n$$$, or $$$a_i=n^2$$$. He has calculated the sum of all the elements of the sequence, and called this value $$$s$$$. Luis has lost his sequence, but he remembers the values of $$$n$$$ and $$$s$$$. Can you find the number of elements in the sequence that are equal to $$$n^2$$$?We can show that the answer is unique under the given constraints.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 2\\cdot 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$n$$$ and $$$s$$$ ($$$1\\le n< 10^6$$$, $$$0\\le s \\le 10^{18}$$$). It is guaranteed that the value of $$$s$$$ is a valid sum for some sequence satisfying the above constraints.", "output_spec": "For each test case, print one integer — the number of elements in the sequence which are equal to $$$n^2$$$.", "sample_inputs": ["4\n\n7 0\n\n1 1\n\n2 12\n\n3 12"], "sample_outputs": ["0\n1\n3\n1"], "notes": "NoteIn the first test case, we have $$$s=0$$$ so all numbers are equal to $$$0$$$ and there isn't any number equal to $$$49$$$.In the second test case, we have $$$s=1$$$. There are two possible sequences: $$$[0, 1]$$$ or $$$[1, 0]$$$. In both cases, the number $$$1$$$ appears just once. In the third test case, we have $$$s=12$$$, which is the maximum possible value of $$$s$$$ for this case. Thus, the number $$$4$$$ appears $$$3$$$ times in the sequence."}, "positive_code": [{"source_code": "t=gets.chomp.to_i\r\n\r\nt.times do |i|\r\n ns=gets.chomp.split\r\n\r\n n=ns[0]\r\n s=ns[1]\r\n\r\n if(n.to_i<=s.to_i)\r\n puts s.to_i/((n.to_i)**2)\r\n else\r\n puts 0\r\n end\r\nend"}], "negative_code": [], "src_uid": "7226a7d2700ee52f52d417f404c42ab7"} {"nl": {"description": "A club plans to hold a party and will invite some of its $$$n$$$ members. The $$$n$$$ members are identified by the numbers $$$1, 2, \\dots, n$$$. If member $$$i$$$ is not invited, the party will gain an unhappiness value of $$$a_i$$$.There are $$$m$$$ pairs of friends among the $$$n$$$ members. As per tradition, if both people from a friend pair are invited, they will share a cake at the party. The total number of cakes eaten will be equal to the number of pairs of friends such that both members have been invited.However, the club's oven can only cook two cakes at a time. So, the club demands that the total number of cakes eaten is an even number.What is the minimum possible total unhappiness value of the party, respecting the constraint that the total number of cakes eaten is even?", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$). The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\leq n \\leq 10^5$$$, $$$0 \\leq m \\leq \\min(10^5,\\frac{n(n-1)}{2})$$$) — the number of club members and pairs of friends. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2, \\dots,a_n$$$ ($$$0 \\leq a_i \\leq 10^4$$$) — the unhappiness value array. Each of the next $$$m$$$ lines contains two integers $$$x$$$ and $$$y$$$ ($$$1 \\leq x,y \\leq n$$$, $$$x \\neq y$$$) indicating that $$$x$$$ and $$$y$$$ are friends. Each unordered pair $$$(x,y)$$$ appears at most once in each test case. It is guaranteed that both the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases do not exceed $$$10^5$$$.", "output_spec": "For each test case, print a line containing a single integer – the minimum possible unhappiness value of a valid party.", "sample_inputs": ["4\n\n1 0\n\n1\n\n3 1\n\n2 1 3\n\n1 3\n\n5 5\n\n1 2 3 4 5\n\n1 2\n\n1 3\n\n1 4\n\n1 5\n\n2 3\n\n5 5\n\n1 1 1 1 1\n\n1 2\n\n2 3\n\n3 4\n\n4 5\n\n5 1"], "sample_outputs": ["0\n2\n3\n2"], "notes": "NoteIn the first test case, all members can be invited. So the unhappiness value is $$$0$$$.In the second test case, the following options are possible: invite $$$1$$$ and $$$2$$$ ($$$0$$$ cakes eaten, unhappiness value equal to $$$3$$$); invite $$$2$$$ and $$$3$$$ ($$$0$$$ cakes eaten, unhappiness value equal to $$$2$$$); invite only $$$1$$$ ($$$0$$$ cakes eaten, unhappiness value equal to $$$4$$$); invite only $$$2$$$ ($$$0$$$ cakes eaten, unhappiness value equal to $$$5$$$); invite only $$$3$$$ ($$$0$$$ cakes eaten, unhappiness value equal to $$$3$$$); invite nobody ($$$0$$$ cakes eaten, unhappiness value equal to $$$6$$$). The minimum unhappiness value is achieved by inviting $$$2$$$ and $$$3$$$.In the third test case, inviting members $$$3,4,5$$$ generates a valid party with the minimum possible unhappiness value."}, "positive_code": [{"source_code": "def main\n t = inp.to_i\n t.times do\n n, m = mapint(inarr)\n a = mapint(inarr)\n gp = Array.new(n){ Array.new(0) }\n m.times do |i|\n x, y = mapint(inarr)\n x-=1\n y-=1\n gp[x].push y\n gp[y].push x\n end\n\n if m & 1 == 0\n puts 0\n next\n end\n\n minct = 99999999\n n.times do |i|\n if gp[i].length & 1 == 1\n minct = min(minct, a[i])\n else\n gp[i].each do |e|\n if gp[e].length & 1 == 0\n minct = min(minct, a[i] + a[e])\n end\n end\n end\n end\n puts minct\n\n\n end\n\nend\n\n# lib\ndef inp\n gets.chomp\nend\n\ndef inarr\n gets.chomp.split\nend\n\ndef mapint(lst)\n lst.map(&:to_i)\nend\n\ndef max(a, b)\n (a > b)? a:b\nend\n\ndef min(a, b)\n (a < b)? a:b\nend\n\nmain"}], "negative_code": [], "src_uid": "a9c54d6f952320d75324b30dcb098332"} {"nl": {"description": "Polycarp wants to cook a soup. To do it, he needs to buy exactly $$$n$$$ liters of water.There are only two types of water bottles in the nearby shop — $$$1$$$-liter bottles and $$$2$$$-liter bottles. There are infinitely many bottles of these two types in the shop.The bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles correspondingly.Polycarp wants to spend as few money as possible. Your task is to find the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles. You also have to answer $$$q$$$ independent queries.", "input_spec": "The first line of the input contains one integer $$$q$$$ ($$$1 \\le q \\le 500$$$) — the number of queries. The next $$$q$$$ lines contain queries. The $$$i$$$-th query is given as three space-separated integers $$$n_i$$$, $$$a_i$$$ and $$$b_i$$$ ($$$1 \\le n_i \\le 10^{12}, 1 \\le a_i, b_i \\le 1000$$$) — how many liters Polycarp needs in the $$$i$$$-th query, the cost (in burles) of the bottle of the first type in the $$$i$$$-th query and the cost (in burles) of the bottle of the second type in the $$$i$$$-th query, respectively.", "output_spec": "Print $$$q$$$ integers. The $$$i$$$-th integer should be equal to the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n_i$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a_i$$$ burles and the bottle of the second type costs $$$b_i$$$ burles.", "sample_inputs": ["4\n10 1 3\n7 3 2\n1 1000 1\n1000000000000 42 88"], "sample_outputs": ["10\n9\n1000\n42000000000000"], "notes": null}, "positive_code": [{"source_code": "gets.to_i.times do\n\tn, a, b = gets.split.map(&:to_i)\n\tp [a * n, b * (n / 2) + a * (n % 2)].min\nend\n"}, {"source_code": "\n\ndef solution\n q = read_int\n\n q.times do\n n, a, b = read_ints\n\n one_liter = n * a\n two_liter = (n / 2) * b + (n.odd? ? a : 0)\n\n p [one_liter, two_liter].min\n\n end\n\nend\n\ndef read_int\n gets.to_i\nend\n\ndef read_ints\n gets.split.map(&:to_i)\nend\n\ndef read_string\n gets.chomp\nend\n\nsolution unless ENV['TEST__MODE']\n"}, {"source_code": "q = gets.to_i\nans = 0\nq.times do\n n, a, b = gets.split.map(&:to_i)\n if a <= b / 2\n puts n * a\n else\n puts (n/2) * b + (n - (n/2*2)) * a\n end\nend"}, {"source_code": "q = gets.strip.to_i\nq.times do\n n, a, b = gets.strip.split.map(&:to_i)\n first = n * a\n second = (n / 2) * b\n second += a if n.odd?\n ans = [first, second].min\n puts ans\nend"}, {"source_code": "DBG = true\nq = gets.to_i\nfor i in 0...q\n n,a,b = gets.split.map{|z| z.to_i}\n if 2*a <= b\n puts \"#{n*a}\"\n else\n puts \"#{b*(n/2)+a*(n%2)}\"\n end\nend\n"}, {"source_code": "gets.to_i.times do\n n,a,b = gets.split.map(&:to_i)\n if a*2 < b\n puts n*a\n else\n puts n/2*b+(n%2)*a\n end\nend"}, {"source_code": "def solve(n,a,b)\n [n*(a),b*(n/2) + a*(n%2)].min\n end\nt = gets.to_i\nt.times{\nn,a,b = gets.split(\" \").map(&:to_i)\nputs solve(n,a,b)\n}\n"}, {"source_code": "# n - required amount of water (in liters)\n# a - 1L bottle cost\n# b - 2L bottle cost\ndef price(n, a, b)\n if 2 * a <= b\n n * a\n else\n n / 2 * b + n % 2 * a\n end\nend\n\ndef respond_n_queries(n)\n n.times do\n n, a, b = gets.chomp.split.map(&:to_i)\n puts price(n, a, b)\n end\nend\n\nqueries = gets.to_i\nrespond_n_queries(queries)\n"}], "negative_code": [{"source_code": "def solve(n,a,b)\n ans = 0\n if(n==1)\n return a\n end\n if(b<=a)\n if(n%2==0)\n return b*(n/2)\n end\n return b*(n/2)+a\n end\n return a*n\n end\n\nt = gets.to_i\n\nt.times{\n\t\nn,a,b = gets.split(\" \").map(&:to_i)\n\nputs solve(n,a,b)\n\n}\n"}, {"source_code": "# n - required amount of water (in liters)\n# a - 1L bottle cost\n# b - 2L bottle cost\ndef price(n, a, b)\n if a <= b\n n * a\n else\n n / 2 * b + n % 2 * a\n end\nend\n\ndef respond_n_queries(n)\n n, a, b = gets.chomp.split.map(&:to_i)\n puts price(n, a, b)\nend\n\nqueries = gets.to_i\nrespond_n_queries(queries)\n"}, {"source_code": "# n - required amount of water (in liters)\n# a - 1L bottle cost\n# b - 2L bottle cost\ndef price(n, a, b)\n if a <= 2 * b\n n * a\n else\n n / 2 * b + n % 2 * a\n end\nend\n\ndef respond_n_queries(n)\n n.times do\n n, a, b = gets.chomp.split.map(&:to_i)\n puts price(n, a, b)\n end\nend\n\nqueries = gets.to_i\nrespond_n_queries(queries)\n"}, {"source_code": "# n - required amount of water (in liters)\n# a - 1L bottle cost\n# b - 2L bottle cost\ndef price(n, a, b)\n if a <= b\n n * a\n else\n n / 2 * b + n % 2 * a\n end\nend\n\ndef respond_n_queries(n)\n n.times do\n n, a, b = gets.chomp.split.map(&:to_i)\n puts price(n, a, b)\n end\nend\n\nqueries = gets.to_i\nrespond_n_queries(queries)\n"}], "src_uid": "beab56c5f7d2996d447320a62b0963c2"} {"nl": {"description": "An array of integers $$$p_1, p_2, \\dots, p_n$$$ is called a permutation if it contains each number from $$$1$$$ to $$$n$$$ exactly once. For example, the following arrays are permutations: $$$[3, 1, 2]$$$, $$$[1]$$$, $$$[1, 2, 3, 4, 5]$$$ and $$$[4, 3, 1, 2]$$$. The following arrays are not permutations: $$$[2]$$$, $$$[1, 1]$$$, $$$[2, 3, 4]$$$.Polycarp invented a really cool permutation $$$p_1, p_2, \\dots, p_n$$$ of length $$$n$$$. It is very disappointing, but he forgot this permutation. He only remembers the array $$$q_1, q_2, \\dots, q_{n-1}$$$ of length $$$n-1$$$, where $$$q_i=p_{i+1}-p_i$$$.Given $$$n$$$ and $$$q=q_1, q_2, \\dots, q_{n-1}$$$, help Polycarp restore the invented permutation.", "input_spec": "The first line contains the integer $$$n$$$ ($$$2 \\le n \\le 2\\cdot10^5$$$) — the length of the permutation to restore. The second line contains $$$n-1$$$ integers $$$q_1, q_2, \\dots, q_{n-1}$$$ ($$$-n < q_i < n$$$).", "output_spec": "Print the integer -1 if there is no such permutation of length $$$n$$$ which corresponds to the given array $$$q$$$. Otherwise, if it exists, print $$$p_1, p_2, \\dots, p_n$$$. Print any such permutation if there are many of them.", "sample_inputs": ["3\n-2 1", "5\n1 1 1 1", "4\n-1 2 2"], "sample_outputs": ["3 1 2", "1 2 3 4 5", "-1"], "notes": null}, "positive_code": [{"source_code": "N = gets.to_i\nQ = gets.split.map &:to_i\nps = [0]\nQ.each{ |q| ps << (ps[-1] + q) }\nmx = ps.max\nmn = ps.min\nif mx - mn == N-1 && ps.uniq.size == N\n puts ps.map{ |r| r - mn + 1 }.join(\" \")\nelse\n p -1\nend"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nb = Array.new(n,0)\nfor i in 1..n-1\n b[i] = b[i-1]+a[i-1]\nend\nmn = b.min\nfor i in 0..n-1\n b[i] -= mn-1\nend\nc = Hash.new(false)\nfor i in 0..n-1\n if c[b[i]] || b[i] > n\n puts -1\n exit\n end\n c[b[i]] = true\nend\nputs b.join(\" \")"}, {"source_code": "def main\n n = gets.chomp.to_i\n q = gets.chomp.split.map(&:to_i)\n\n qp = [0]\n for k in q\n qp << qp.last + k\n end\n\n diff = 1 - qp.min\n result = []\n visited = {}\n for k in qp\n if k + diff < 1 or k + diff > n or visited.key?(k + diff)\n puts -1\n return\n else\n result << k + diff\n visited[k + diff] = true\n end \n end\n\n puts result.join(\" \")\nend\n\nmain\n"}], "negative_code": [{"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nb = Array.new(n,0)\nfor i in 1..n-1\n b[i] = b[i-1]+a[i-1]\nend\nmn = b.min\nfor i in 0..n-1\n b[i] -= mn-1\nend\nc = Hash.new(false)\nfor i in 0..n-1\n if c[b[i]] || b[i] > n\n puts -1\n exit\n end\nend\nputs b.join(\" \")"}, {"source_code": "def main\n n = gets.chomp.to_i\n q = gets.chomp.split.map(&:to_i)\n\n diff = q.inject(0) { |sum, x| sum + x }\n for i in (1..n)\n next if i + diff < 1 or i + diff > n\n\n visited = {i => true, i + diff => true}\n result = [i]\n\n for j in q\n x = result.last + j\n break if x < 1 or x > n or visited.key?(x)\n visited[x] = true\n result << x\n end \n\n result << i + diff\n if result.length == n\n puts result.join(\" \")\n return\n end\n end\n\n puts -1\nend\n\nmain\n"}, {"source_code": "def main\n n = gets.chomp.to_i\n q = gets.chomp.split.map(&:to_i)\n\n diff = q.inject(0) { |sum, x| sum + x }\n for i in (1..n)\n next if i + diff < 1 or i + diff > n\n\n visited = {i => true, i + diff => true}\n result = [i]\n\n for j in q\n x = result.last + j\n break if x < 1 or x > n or visited.key?(x)\n visited[x] = true\n result << x\n end \n\n result << i + diff\n break if visited.key?(i + diff)\n\n if result.length == n\n puts result.join(\" \")\n return\n end\n end\n\n puts -1\nend\n\nmain\n"}, {"source_code": "def main\n n = gets.chomp.to_i\n q = gets.chomp.split.map(&:to_i)\n\n diff = q.last - q.first\n for i in (1..n)\n visited = {i => true, i + diff => true}\n result = [i]\n\n for j in q\n x = result.last + j\n break if x < 1 or visited.key?(x)\n visited[x] = true\n result << x\n end \n\n if result.length == n\n puts result.join(\" \")\n return\n end\n end\n\n puts -1\nend\n\nmain\n"}], "src_uid": "b6ac7c30b7efdc7206dbbad5cfb86db7"} {"nl": {"description": "There is a house with $$$n$$$ flats situated on the main street of Berlatov. Vova is watching this house every night. The house can be represented as an array of $$$n$$$ integer numbers $$$a_1, a_2, \\dots, a_n$$$, where $$$a_i = 1$$$ if in the $$$i$$$-th flat the light is on and $$$a_i = 0$$$ otherwise.Vova thinks that people in the $$$i$$$-th flats are disturbed and cannot sleep if and only if $$$1 < i < n$$$ and $$$a_{i - 1} = a_{i + 1} = 1$$$ and $$$a_i = 0$$$.Vova is concerned by the following question: what is the minimum number $$$k$$$ such that if people from exactly $$$k$$$ pairwise distinct flats will turn off the lights then nobody will be disturbed? Your task is to find this number $$$k$$$.", "input_spec": "The first line of the input contains one integer $$$n$$$ ($$$3 \\le n \\le 100$$$) — the number of flats in the house. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$a_i \\in \\{0, 1\\}$$$), where $$$a_i$$$ is the state of light in the $$$i$$$-th flat.", "output_spec": "Print only one integer — the minimum number $$$k$$$ such that if people from exactly $$$k$$$ pairwise distinct flats will turn off the light then nobody will be disturbed.", "sample_inputs": ["10\n1 1 0 1 1 0 1 0 1 0", "5\n1 1 0 0 0", "4\n1 1 1 1"], "sample_outputs": ["2", "0", "0"], "notes": "NoteIn the first example people from flats $$$2$$$ and $$$7$$$ or $$$4$$$ and $$$7$$$ can turn off the light and nobody will be disturbed. It can be shown that there is no better answer in this example.There are no disturbed people in second and third examples."}, "positive_code": [{"source_code": "p$<.read.scan('1 0 1').size"}, {"source_code": "n = gets.strip.to_i\nflats = gets.strip.gsub(' ', '')\nk = 0\nuntil !flats.include?(\"10101\")\n flats.sub!(\"10101\", \"10001\")\n k += 1\nend\n\nuntil !flats.include?(\"101\")\n flats.sub!(\"101\", \"100\")\n k += 1\nend\nputs k"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nans = 0\nfor i in 2..n-1\n if a[i] == 1 && a[i-1] == 0 && a[i-2] == 1\n ans += 1\n a[i] = 0\n end\nend\nputs ans"}], "negative_code": [{"source_code": "p$<.read.count '1 0 1'"}], "src_uid": "ea62b6f68d25fb17aba8932af8377db0"} {"nl": {"description": "Vasya has two arrays $$$A$$$ and $$$B$$$ of lengths $$$n$$$ and $$$m$$$, respectively.He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $$$[1, 10, 100, 1000, 10000]$$$ Vasya can obtain array $$$[1, 1110, 10000]$$$, and from array $$$[1, 2, 3]$$$ Vasya can obtain array $$$[6]$$$.Two arrays $$$A$$$ and $$$B$$$ are considered equal if and only if they have the same length and for each valid $$$i$$$ $$$A_i = B_i$$$.Vasya wants to perform some of these operations on array $$$A$$$, some on array $$$B$$$, in such a way that arrays $$$A$$$ and $$$B$$$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $$$A$$$ and $$$B$$$ equal.", "input_spec": "The first line contains a single integer $$$n~(1 \\le n \\le 3 \\cdot 10^5)$$$ — the length of the first array. The second line contains $$$n$$$ integers $$$a_1, a_2, \\cdots, a_n~(1 \\le a_i \\le 10^9)$$$ — elements of the array $$$A$$$. The third line contains a single integer $$$m~(1 \\le m \\le 3 \\cdot 10^5)$$$ — the length of the second array. The fourth line contains $$$m$$$ integers $$$b_1, b_2, \\cdots, b_m~(1 \\le b_i \\le 10^9)$$$ - elements of the array $$$B$$$.", "output_spec": "Print a single integer — the maximum length of the resulting arrays after some operations were performed on arrays $$$A$$$ and $$$B$$$ in such a way that they became equal. If there is no way to make array equal, print \"-1\".", "sample_inputs": ["5\n11 2 3 5 7\n4\n11 7 3 7", "2\n1 2\n1\n100", "3\n1 2 3\n3\n1 2 3"], "sample_outputs": ["3", "-1", "3"], "notes": null}, "positive_code": [{"source_code": "\nna = gets.to_i\na = gets.split.map &:to_i\nnb = gets.to_i\nb = gets.split.map &:to_i\ni = 0; j = 0; ans = 0\n\ndef answer(r)\n\tputs r\n\texit\nend\n\nwhile true\n\tif a[i] == b[j]\n\t\ti += 1\n\t\tj += 1\n\t\tans += 1\n\t\tif i == na\n\t\t\tanswer j == nb ? ans : -1 \n\t\telsif j == nb \n\t\t\tanswer -1 \n\t\tend\n\telsif a[i] < b[j]\n\t\tanswer -1 if i+1 == na\n\t\ta[i+1] += a[i]\n\t\ti += 1\n\telse\n\t\tanswer -1 if j+1 == nb\n\t\tb[j+1] += b[j]\n\t\tj += 1\n\tend\nend\n"}], "negative_code": [], "src_uid": "8c36ab13ca1a4155cf97d0803aba11a3"} {"nl": {"description": "One day Vasya painted a Cartesian coordinate system on a piece of paper and marked some set of points (x1, y1), (x2, y2), ..., (xn, yn). Let's define neighbors for some fixed point from the given set (x, y): point (x', y') is (x, y)'s right neighbor, if x' > x and y' = y point (x', y') is (x, y)'s left neighbor, if x' < x and y' = y point (x', y') is (x, y)'s lower neighbor, if x' = x and y' < y point (x', y') is (x, y)'s upper neighbor, if x' = x and y' > y We'll consider point (x, y) from the given set supercentral, if it has at least one upper, at least one lower, at least one left and at least one right neighbor among this set's points.Vasya marked quite many points on the paper. Analyzing the picture manually is rather a challenge, so Vasya asked you to help him. Your task is to find the number of supercentral points in the given set.", "input_spec": "The first input line contains the only integer n (1 ≤ n ≤ 200) — the number of points in the given set. Next n lines contain the coordinates of the points written as \"x y\" (without the quotes) (|x|, |y| ≤ 1000), all coordinates are integers. The numbers in the line are separated by exactly one space. It is guaranteed that all points are different.", "output_spec": "Print the only number — the number of supercentral points of the given set.", "sample_inputs": ["8\n1 1\n4 2\n3 1\n1 2\n0 2\n0 1\n1 0\n1 3", "5\n0 0\n0 1\n1 0\n0 -1\n-1 0"], "sample_outputs": ["2", "1"], "notes": "NoteIn the first sample the supercentral points are only points (1, 1) and (1, 2).In the second sample there is one supercental point — point (0, 0)."}, "positive_code": [{"source_code": "class SupercentralPoint\n # To change this template use File | Settings | File Templates.\n\nend\n\nnumber = STDIN.readline.to_i\n\n#p number\n\n\nz = []\n\npoint = {:x => 0, :y => 0}\n\nnumber.times do\n v = STDIN.readline.split(' ')\n point[:x] = v[0].to_i\n point[:y] = v[1].to_i\n z << point.dup\nend\n\nsupercentral_points = []\n\nz.each do |i|\n z.each do |j|\n if i[:x] == j[:x]\n if i[:y] > j[:y]\n i[:bot] = true\n j[:top] = true\n elsif i[:y] < j[:y]\n j[:bot] = true\n i[:top] = true\n end\n end\n\n\n\n if i[:y] == j[:y]\n if i[:x] > j[:x]\n i[:left] = true\n j[:right] = true\n elsif i[:x] < j[:x]\n j[:left] = true\n i[:right] = true\n end\n\n end\n end\nend\n\n\nz.each do |i|\n\n if i[:top] && i[:bot] && i[:left] && i[:right]\n supercentral_points << i\n end\nend\n\np supercentral_points.count"}, {"source_code": "n = gets.to_i\nhs = Hash.new 0\nn.times{ hs[ gets.split(' ').map(&:to_i) ] = 0 }\nar = hs.keys\nar.product(ar).map(&:flatten).each do |x0,y0,x1,y1|\n vec = [x0,y0]\n if x1 == x0\n if y1 > y0\n hs[vec] |= 1\n elsif y1 < y0\n hs[vec] |= 2\n end\n elsif y1 == y0\n if x1 > x0\n hs[vec] |= 4\n elsif x1 < x0\n hs[vec] |= 8\n end\n end\nend\nputs hs.select{|k,v| v == 15}.count"}, {"source_code": "sin = STDIN\nsout = STDOUT\n# sin = File.open(\"test.in\", \"r\")\n# sout = File.open(\"test.out\", \"w\")\n\nn = sin.readline.to_i\na = []\nn.times {\n |i| a[i] = sin.readline.split.map{|e| e.to_i}\n}\nsout.puts a.find_all{ |e| a.find{|x| x[0] < e[0] && x[1] == e[1]} != nil && a.find{|x| x[0] > e[0] && x[1] == e[1]} != nil \\\n && a.find{|x| x[0] == e[0] && x[1] > e[1]} != nil && a.find{|x| x[0] == e[0] && x[1] < e[1]} != nil}.size\n"}, {"source_code": "n = gets.chomp.to_i\nc = []\nxh = Hash.new()\nyh = Hash.new()\nfor i in 0..n-1\n a = gets.chomp.split(/ /).map!{|x| x.to_i}\n c << a\n if xh.key?(a[0])\n xh[a[0]] << a\n else\n xh[a[0]] = [a]\n end\n if yh.key?(a[1])\n yh[a[1]] << a\n else\n yh[a[1]] = [a]\n end\nend\nxh.each_key{|key| xh[key].sort!{|a1,a2| a1[1] <=> a2[1] } }\nyh.each_key{|key| yh[key].sort!{|a1,a2| a1[0] <=> a2[0] } }\n\nret = 0\nxh.each_key{|key|\n aa = xh[key]\n if aa.length >= 3\n for i in 1..aa.length-2\n yk = aa[i][1]\n if yk > aa[0][1] && yk < aa[aa.length-1][1]\n bb = yh[yk]\n if bb.length >= 3\n if key > bb[0][0] && key < bb[bb.length-1][0]\n ret += 1\n end\n end\n end\n end\n end\n}\nputs ret\n"}, {"source_code": "n = gets.to_i\na = Array.new(n){gets.split.map(&:to_i)}\np a.count{|x, y|\n c = [0] * 9\n a.each{|i, j|\n c[(i <=> x) * 3 + (j <=> y) + 4] += 1\n }\n [1, 3, 5, 7].all?{|_| c[_] > 0}\n}\n"}, {"source_code": "n = gets.to_i\npoints = []\nn.times do\n points << gets.split.map(&:to_i)\nend\nans = 0\npoints.each do |pts1|\n check = 0\n points.each do |pts2|\n check |= 1 if pts1[0] < pts2[0] && pts1[1] == pts2[1]\n check |= 2 if pts1[0] > pts2[0] && pts1[1] == pts2[1]\n check |= 4 if pts1[1] < pts2[1] && pts1[0] == pts2[0]\n check |= 8 if pts1[1] > pts2[1] && pts1[0] == pts2[0]\n end\n ans += 1 if check == 15\nend\np ans\n\n"}, {"source_code": "n = gets.to_i\na = []\nn.times do |i|\n a[i] = gets.split(\" \").map(&:to_i)\nend\n\ncnt = 0\nfor i in 0...n do\n l = r = u = d = false\n for j in 0...n do\n next if i == j\n l = a[j][0] < a[i][0] && a[j][1] == a[i][1] || l\n r = a[j][0] > a[i][0] && a[j][1] == a[i][1] || r\n u = a[j][0] == a[i][0] && a[j][1] > a[i][1] || u\n d = a[j][0] == a[i][0] && a[j][1] < a[i][1] || d\n end\n cnt += 1 if l && r && u && d\nend\n\nputs cnt"}, {"source_code": "n = gets.to_i\nmas = []\nn-=1\nfor i in (0..n) do\n mas[i] = gets.split.map(&:to_i)\nend\ncount = 0\nmas.each do |m1|\n q,w,e,r = false\n mas.each do |m2|\n if m1 != m2\n if m1[0] > m2[0] && m1 [1] == m2[1]\n q = true\n end\n if m1[0] < m2[0] && m1 [1] == m2[1]\n w = true\n end\n if m1[0] == m2[0] && m1 [1] < m2[1]\n e = true\n end\n if m1[0] == m2[0] && m1 [1] > m2[1]\n r = true\n end\n end\n end\n if q && w && e && r\n count+=1\n end\nend\nputs count"}, {"source_code": "n = gets.chomp.to_i\n\npoints = []\nn.times do \n x, y = gets.chomp.split(\" \").map{|e| e.to_i}\n points << [x, y]\nend\nsupercentrals = []\n\npoints.each do |point|\n x, y = point[0], point[1]\n right, left, up, down = false, false, false, false\n \n points.each do |point2|\n if point == point2\n next\n end\n x2, y2 = point2[0], point2[1]\n \n if x2 > x and y == y2\n right = true\n elsif x2 < x and y == y2\n left = true\n elsif x == x2 and y2 > y\n up = true\n elsif x == x2 and y2 < y\n down = true\n end\n end\n \n if right and left and up and down\n supercentrals << point\n end\nend\n\nputs supercentrals.length"}, {"source_code": "n=gets.to_i\npoints=[]\nn.times do\n\tpoints< x } && points.any? {|a, b| a == x && b < y } && points.any? {|a, b| a == x && b > y }\n}\nputs ctr"}, {"source_code": "a = readlines[1..-1].map(&:split).each { |x| x.map!(&:to_i) }\np a.inject(0) {|acc, x| acc + ([a.find_all {|y| x[0] > y[0] and x[1] == y[1] }.size,\n a.find_all {|y| x[0] < y[0] and x[1] == y[1] }.size,\n a.find_all {|y| x[1] > y[1] and x[0] == y[0] }.size,\n a.find_all {|y| x[1] < y[1] and x[0] == y[0] }.size].min >= 1 ? 1 : 0)}"}, {"source_code": "n = gets.chomp.to_i\npoints = []\nn.times do\n points << gets.chomp.split(/ /).map(&:to_i)\nend\nans = 0\nn.times do |i|\n point_1 = points[i]\n x = point_1[0]\n y = point_1[1]\n lower_n = upper_n = left_n = right_n = false\n n.times do |j|\n next if i == j\n point_2 = points[j]\n x_1 = point_2[0]\n y_1 = point_2[1]\n right_n = true if x_1 > x && y_1 == y && !right_n\n left_n = true if x_1 < x && y_1 == y && !left_n\n lower_n = true if x_1 == x && y_1 < y && !lower_n\n upper_n = true if x_1 == x && y_1 > y && !upper_n\n end\n ans += 1 if upper_n && lower_n && left_n && right_n\nend\nputs ans\n"}, {"source_code": "hsX={}\nhsY={}\nn=gets.to_i\nn.times{|i|\n\tx,y=gets.split.map{|e| e.to_i}\n\tif hsX.member?(x)==false\n\t\thsX[x]=[]\n\tend\n\tif hsY.member?(y)==false\n\t\thsY[y]=[]\n\tend\n\thsX[x]<<[y,i]\n\thsY[y]<<[x,i]\n}\nhsX=hsX.select{|k,v|\n\tv.size>2\n}\nhsY=hsY.select{|k,v|\n\tv.size>2\n}\nans=0\n\nvx=hsX.values.map{|xs|\n\txs.sort!\n\txs.shift\n\txs.pop\n\txs\n}\nvy=hsY.values.map{|ys|\n\tys.sort!\n\tys.pop\n\tys.shift\n\tys\n}\nvx.each{|vx1|\n\tvy.each{|vy1|\n\t\tvx1.each{|e1|\n\t\t\tvy1.each{|e2|\n\t\t\t\tans+=1 if e1[1]==e2[1]\n\t\t\t}\t\n\t\t}\n\t}\n}\nputs ans"}, {"source_code": "n = gets.to_i\n\na = Array.new\n\nn.times do |i|\n\ta << gets.split(\" \").map{|x| x.to_i}\nend\n\n#puts a.to_s\n\nsum = 0\n\nfor i in 0...a.length\n\tnext if a.take(i).count{|x,y| a[i][0] == x} > 0\n\tb = a.select{|x,y| x == a[i][0]}\n\tb.sort!{|x,y| x[1] <=> y[1]}\n\tb = b.drop(1)\n\tb.pop\n\t#puts b.to_s\n\tfor j in 0...b.length do\n\t\tif(a.count{|x,y| b[j][1] == y && b[j][0] < x} > 0 &&\n\t\t\ta.count{|x,y| b[j][1] == y && b[j][0] > x} > 0)\n\t\t\tthen\n\t\t\tsum += 1\n\t\tend\n\tend\nend\n\nputs sum\n"}, {"source_code": "#!/usr/bin/ruby\n# coding: utf-8\n\nn = $stdin.gets.to_i\nh = Hash.new { Array.new }\nv = Hash.new { Array.new }\nps = []\nn.times do\n x, y = *$stdin.gets.chomp.split.map(&:to_i)\n ps.push [x, y]\n h[x] += [y]\n v[y] += [x]\nend\n\nh.keys.each do |x|\n h[x].sort!\nend\nv.keys.each do |y|\n v[y].sort!\nend\n\nans = 0\nps.each do |x, y|\n i = h[x].find_index y\n j = v[y].find_index x\n if 0 < i and i < h[x].size-1 and 0 < j and j < v[y].size-1\n ans += 1\n end\nend\nputs ans\n"}, {"source_code": "n = gets.to_i\na=[]\nn.times do\n t = gets.split.map(){ |x| x.to_i}\n a << t\nend\n\nans = a.count do |s|\n r1 = a.any? {|t| t[0] == s[0] && t[1] < s[1]}\n r2 = a.any? {|t| t[0] == s[0] && t[1] > s[1]}\n r3 = a.any? {|t| t[0] < s[0] && t[1] == s[1]}\n r4 = a.any? {|t| t[0] > s[0] && t[1] == s[1]}\n r1 == true && r2 == true && r3 == true && r4 == true\n \nend\n\nputs ans"}, {"source_code": "class Point\n\tattr_accessor :x,:y\n\n\tdef initialize(_x,_y)\n\t\tself.x = _x\n\t\tself.y = _y\n\tend\nend\n\n\n\ndef left?(p)\n\t@points.any?{|a| a.x < p.x && p.y == a.y}\nend\n\ndef right?(p)\n\t@points.any?{|a| a.x > p.x && p.y == a.y}\nend\n\ndef upper?(p)\n\t@points.any?{|a| a.x == p.x && p.y < a.y}\nend\n\ndef lower?(p)\n\t@points.any?{|a| a.x == p.x && p.y > a.y}\nend\n\nn = gets.to_i\n@points = []\n\nn.times do\n\tx,y = gets.split(\" \").map(&:to_i)\n\t@points << Point.new(x,y)\nend\n\ncount = 0\n@points.each do |p|\n\tcount += 1 if left?(p) && right?(p) && upper?(p) && lower?(p)\nend\nputs count"}, {"source_code": "n = gets.to_i\na = n.times.map{gets.split.map(&:to_i)}\nans = a.count do |x, y|\n check = Array.new(4, false)\n a.each do |xx, yy|\n if xx == x\n check[2] ||= yy < y\n check[3] ||= yy > y\n elsif yy == y\n check[0] ||= xx < x\n check[1] ||= xx > x\n end\n end\n check.all?\nend\nputs ans\n"}, {"source_code": "n = gets.to_i\na = n.times.map{gets.split.map(&:to_i)}\nc = 0\na.each do |x, y|\n check = [0, 0, 0, 0]\n a.each do |xx, yy|\n if xx == x\n check[2] = 1 if yy < y\n check[3] = 1 if yy > y\n elsif yy == y\n check[0] = 1 if xx < x\n check[1] = 1 if xx > x\n end\n end\n c += 1 if check.all?{|x| x == 1}\nend\nputs c\n"}, {"source_code": "n = gets.to_i\nxmi, xmx, ymi, ymx = [1000, -1000, 1000, -1000]\na = n.times.map{gets.split.map(&:to_i)}\nc = 0\na.each do |x, y|\n check = [0, 0, 0, 0]\n a.each do |xx, yy|\n if xx == x\n check[2] = 1 if yy < y\n check[3] = 1 if yy > y\n elsif yy == y\n check[0] = 1 if xx < x\n check[1] = 1 if xx > x\n end\n end\n c += 1 if check.all?{|x| x == 1}\nend\nputs c\n"}, {"source_code": "xs = Array.new(2011) { Array.new }\nys = Array.new(2011) { Array.new }\n\nn = gets.to_i\na = []\nPoint = Struct.new(:x, :y)\n\n(0...n).each { |i|\n x, y = gets.split.map(&:to_i)\n a << Point.new(x, y)\n xs[y+1000] << x\n ys[x+1000] << y\n}\n(0...2011).each { |i|\n xs[i].sort!\n ys[i].sort!\n}\n\nres = 0\na.each { |p|\n if xs[p.y+1000][0] < p.x && p.x < xs[p.y+1000][-1]\n if ys[p.x+1000][0] < p.y && p.y < ys[p.x+1000][-1]\n res += 1\n end\n end\n}\np res\n"}, {"source_code": "points = []\nfor _ in 1..Integer(gets.chomp)\n points << gets.chomp.split.map { |x| x.to_i }\nend\n\ncount = 0\npoints.each { |x, y|\n count += 1 if points.any? {|a, b| b == y && a < x } && points.any? {|a, b| b == y && a > x } && points.any? {|a, b| a == x && b < y } && points.any? {|a, b| a == x && b > y }\n}\nputs count"}, {"source_code": "pts=gets.to_i.times.map{gets.split.map(&:to_i)}\nansx=[]\nansy=[]\npts.group_by{|x,y|x}.each{|_x,ary|ary.sort_by{|x,y|y}[1..-2].each{|pt|ansx<2}\npts.group_by{|x,y|y}.each{|_y,ary|ary.sort_by{|x,y|x}[1..-2].each{|pt|ansy<2}\np (ansx&ansy).size"}, {"source_code": "gets; input = STDIN.read.split(\"\\n\")\ninput.map! {|i| i.split(\" \").map(&:to_i)}\n\ncount = 0\n\ninput.each do |i|\n c = [false, false, false, false]\n input.each do |j|\n if i != j\n\n c[0] = true if i[0] > j[0] && i[1] == j[1]\n c[1] = true if i[0] < j[0] && i[1] == j[1]\n c[2] = true if i[0] == j[0] && i[1] < j[1]\n c[3] = true if i[0] == j[0] && i[1] > j[1]\n\n if c.count(true) == 4\n count += 1\n break\n end\n end\n end\n c = [false, false, false, false]\nend\n\nprint count\n"}], "negative_code": [{"source_code": "a = readlines[1..-1].map(&:split).each { |x| x.map!(&:to_i) }\np a.inject(0) {|acc, x| acc + [a.find_all {|y| x[0] > y[0] and x[1] == y[1] }.size,\n a.find_all {|y| x[0] < y[0] and x[1] == y[1] }.size,\n a.find_all {|y| x[1] > y[1] and x[0] == y[0] }.size,\n a.find_all {|y| x[1] < y[1] and x[0] == y[0] }.size].min >= 1 ? 1 : 0}"}, {"source_code": "a = readlines[1..-1].map(&:split).each { |x| x.map!(&:to_i) }\np a.inject(0) {|acc, x| acc + [a.find_all {|y| x[0] > y[0] and x[1] == y[1] }.size,\n a.find_all {|y| x[0] < y[0] and x[1] == y[1] }.size,\n a.find_all {|y| x[1] > y[1] and x[0] == y[0] }.size,\n a.find_all {|y| x[1] < y[1] and x[0] == y[0] }.size].min }"}, {"source_code": "n = gets.chomp.to_i\npoints = []\nn.times do\n points << gets.chomp.split(/ /).map(&:to_i)\nend\nans = 0\nn.times do |i|\n point_1 = points[i]\n x = point_1[0]\n y = point_1[1]\n lower_n = upper_n = left_n = right_n = false\n n.times do |j|\n next if i == j\n point_2 = points[j]\n x_1 = point_2[0]\n y_1 = point_2[1]\n right_n = true if x_1 > x && y_1 == y && !right_n\n left_n = true if x_1 < x && y_1 == y && !left_n\n lower_n = true if x_1 == x && y_1 < y && !lower_n\n upper_n = true if x_1 == x && y_1 > y && !upper_n\n end\n if upper_n && lower_n && left_n && right_n\n puts point_1.inspect\n end\n ans += 1 if upper_n && lower_n && left_n && right_n\nend\nputs ans\n"}, {"source_code": "n = gets.chomp.to_i\npoints = []\nn.times do\n points << gets.chomp.split(/ /).map(&:to_i)\nend\nans = 0\nn.times do |i|\n point_1 = points[i]\n x = point_1[0]\n y = point_1[1]\n neighbor = 0\n n.times do |j|\n next if i == j\n point_2 = points[j]\n x_1 = point_2[0]\n y_1 = point_2[1]\n neighbor += 1 if x_1 > x && y_1 == y\n neighbor += 1 if x_1 < x && y_1 == y\n neighbor += 1 if x_1 == x && y_1 < y\n neighbor += 1 if x_1 == x && y_1 > y\n end\n ans += 1 if neighbor >= 4\nend\nputs ans\n"}, {"source_code": "n=gets.to_i\nhsX=Hash.new{|hash,key| hash[key]=[]}\nhsY=Hash.new{|hash,key| hash[key]=[]}\nn.times{|i|\n\tx,y=gets.split.map{|e| e.to_i}\n\thsX[x]<2\n}\nhsY=hsY.select{|k,v|\n\tv.size>2\n}\nans=0\nhsX.values.each{|vx|\n\thsY.values.each{|vy|\n\t\tvx.each{|e|\n\t\t\tans+=1 if vy.index(e)!=nil\t\n\t\t}\n\t}\n}\nputs ans"}, {"source_code": "n = gets.to_i\na=[]\nn.times do\n t = gets.split.map(){ |x| x.to_i}\n a << t\nend\n\nans = a.count do |s|\n r1 = a.any? {|t| t[0] == s[0] && t[1] < s[1]}\n r2 = a.any? {|t| t[0] == s[0] && t[1] > s[1]}\n r3 = a.any? {|t| t[0] < s[0] && t[1] == s[1]}\n r4 = a.any? {|t| t[0] > s[0] && t[1] == s[1]}\n r1 == true && r2 == true && r3 == true && r4 = true\n \nend\n\nputs ans"}, {"source_code": "points = Hash.new { |h, k| h[k] = [] }\nfor _ in 1..Integer(gets.chomp)\n x, y = gets.chomp.split.map { |x| x.to_i }\n points[x] << y\nend\n\ncount = 0\npoints.values.each { |v|\n count += v.size - 2 if v.size > 2\n}\nputs count"}, {"source_code": "pts=gets.to_i.times.map{gets.split.map(&:to_i)}\nans=[]\npts.group_by{|x,y|x}.each{|_x,ary|ary.sort_by{|x,y|y}[1..-2].each{|pt|ans<2}\npts.group_by{|x,y|y}.each{|_y,ary|ary.sort_by{|x,y|x}[1..-2].each{|pt|ans<2}\np ans.uniq.size"}, {"source_code": "gets; input = STDIN.read.split(\"\\n\")\ninput.map! {|i| i.split(\" \").map(&:to_i)}\n\ncount = 0\n\ninput.each do |i|\n c = 0\n input.each do |j|\n if i != j\n if (i[0] > j[0] && i[1] == j[1]) ||\n (i[0] < j[0] && i[1] == j[1]) ||\n (i[0] == j[0] && i[1] < j[1]) ||\n (i[0] == j[0] && i[1] > j[1])\n c += 1\n end\n if c == 4\n c = 0\n count += 1\n break\n end\n end \n end\nend\n\nprint count"}], "src_uid": "594e64eef7acd055d59f37710edda201"} {"nl": {"description": "You are given a rooted tree consisting of $$$n$$$ vertices. Vertices are numbered from $$$1$$$ to $$$n$$$. Any vertex can be the root of a tree.A tree is a connected undirected graph without cycles. A rooted tree is a tree with a selected vertex, which is called the root.The tree is specified by an array of parents $$$p$$$ containing $$$n$$$ numbers: $$$p_i$$$ is a parent of the vertex with the index $$$i$$$. The parent of a vertex $$$u$$$ is a vertex that is the next vertex on the shortest path from $$$u$$$ to the root. For example, on the simple path from $$$5$$$ to $$$3$$$ (the root), the next vertex would be $$$1$$$, so the parent of $$$5$$$ is $$$1$$$.The root has no parent, so for it, the value of $$$p_i$$$ is $$$i$$$ (the root is the only vertex for which $$$p_i=i$$$).Find such a set of paths that: each vertex belongs to exactly one path, each path can contain one or more vertices; in each path each next vertex — is a son of the current vertex (that is, paths always lead down — from parent to son); number of paths is minimal. For example, if $$$n=5$$$ and $$$p=[3, 1, 3, 3, 1]$$$, then the tree can be divided into three paths: $$$3 \\rightarrow 1 \\rightarrow 5$$$ (path of $$$3$$$ vertices), $$$4$$$ (path of $$$1$$$ vertices). $$$2$$$ (path of $$$1$$$ vertices). Example of splitting a root tree into three paths for $$$n=5$$$, the root of the tree — node $$$3$$$. ", "input_spec": "The first line of input data contains an integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases in the test. Each test case consists of two lines. The first of them contains an integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$). It is the number of vertices in the tree. The second line contains $$$n$$$ integers $$$p_1, p_2, \\dots, p_n$$$ ($$$1 \\le p_i \\le n$$$). It is guaranteed that the $$$p$$$ array encodes some rooted tree. It is guaranteed that the sum of the values $$$n$$$ over all test cases in the test does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case on the first line, output an integer $$$m$$$ — the minimum number of non-intersecting leading down paths that can cover all vertices of the tree. Then print $$$m$$$ pairs of lines containing path descriptions. In the first of them print the length of the path, in the second — the sequence of vertices specifying that path in the order from top to bottom. You can output the paths in any order. If there are several answers, output any of them.", "sample_inputs": ["6\n\n5\n\n3 1 3 3 1\n\n4\n\n1 1 4 1\n\n7\n\n1 1 2 3 4 5 6\n\n1\n\n1\n\n6\n\n4 4 4 4 1 2\n\n4\n\n2 2 2 2"], "sample_outputs": ["3\n3\n3 1 5\n1\n2\n1\n4\n\n2\n2\n1 2\n2\n4 3\n\n1\n7\n1 2 3 4 5 6 7\n\n1\n1\n1\n\n3\n3\n4 1 5\n2\n2 6\n1\n3\n\n3\n2\n2 1\n1\n3\n1\n4"], "notes": null}, "positive_code": [{"source_code": "require \"set\"\r\n\r\ngets.to_i.times do\r\n gets\r\n ps = gets.split.map(&:to_i)\r\n edges = Hash.new { |h, k| h[k] = [] }\r\n root = ps.find.with_index { |p, i| p == i + 1 }\r\n ps.zip(1..).each do |p, i|\r\n edges[p] << i unless p == i\r\n end\r\n answer = Hash.new { |h, k| h[k] = [] }\r\n queue = [[root, nil]]\r\n while queue.any?\r\n i, up = queue.shift\r\n answer[up || i] << i\r\n edges[i].each.with_index do |j, ii|\r\n queue << [j, ii == 0 ? (up || i) : nil]\r\n end\r\n end\r\n puts answer.size\r\n answer.values.each do |array|\r\n puts array.size\r\n puts array.join(\" \")\r\n end\r\n puts\r\nend\r\n"}], "negative_code": [], "src_uid": "cd2a9169186c4ade98548c29bbdacdf0"} {"nl": {"description": "You are given a sequence of positive integers a1, a2, ..., an. Find all such indices i, that the i-th element equals the arithmetic mean of all other elements (that is all elements except for this one).", "input_spec": "The first line contains the integer n (2 ≤ n ≤ 2·105). The second line contains elements of the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000). All the elements are positive integers.", "output_spec": "Print on the first line the number of the sought indices. Print on the second line the sought indices in the increasing order. All indices are integers from 1 to n. If the sought elements do not exist, then the first output line should contain number 0. In this case you may either not print the second line or print an empty line.", "sample_inputs": ["5\n1 2 3 4 5", "4\n50 50 50 50"], "sample_outputs": ["1\n3", "4\n1 2 3 4"], "notes": null}, "positive_code": [{"source_code": "def average_numbers\n count = gets.to_i\n nums = gets.split(' ').map{|i| i.to_i}\n sum = nums.inject(0){|sum, i| sum+i}\n\n idx = []\n nums.each_with_index do |n, ind|\n idx << ind+1 if (n.to_f == sum/count.to_f)\n end\n puts idx.size\n puts idx.join ' '\nend\n\n\naverage_numbers"}, {"source_code": "n=gets.to_i;\na=gets.split.map{|x| x.to_i};\nsum=a.inject{|sum,i| sum+i };\nres=[];\nfor i in 0..(a.length-1) do\n if a[i]*a.length == sum then\n res.push(i);\n end\nend\nputs(res.length);\nfor i in 0..(res.length-1) do\n print( res[i]+1,\" \");\nend\nputs(\"\");\n"}, {"source_code": "n=gets.to_i;\na=gets.split.map{|x| x.to_i; }\nsum=a.inject{|sum,i| sum+i }\nres=[];\na.length.times{ |x|\n if a[x]*a.length==sum then\n res.push(x+1);\n end\n}\nputs(res.length);\nres.map{|x| print(x,\" \"); }\n"}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\ns=a.inject(:+)\nm=s/n\nputs s%n>0 ? 0 : [a.count(m),a.each_with_index.select{|a,i|a==m}.map{|a,i|i+1}*\" \"]"}, {"source_code": "input = STDIN.read.split(\"\\n\")\n\nn = input[0].to_i\na = input[1].split(\" \").map(&:to_i)\n\n\navg = a.inject(0.0) {|sum, e| sum + e} / n\n\nstr = \"\"\ncount = 0\n\na.each.with_index do |v, k|\n\tif v.to_f == avg\n\t\tstr << (k + 1).to_s << \" \" \n\t\tcount += 1\n\tend\nend\n\nSTDOUT.puts count\nSTDOUT.puts str.chop"}, {"source_code": "sum=gets.to_i*0\na=gets.split.map(&:to_i)\na.each{|s| sum+=s}\nans=[]\na.length.times{|i|\n if a[i]*(a.length-1)==(sum-a[i])\n ans.push(i)\n end\n}\np ans.length\nans.each{|i| print \"#{i+1} \"}\nputs\n"}, {"source_code": "n=gets.chomp.to_i\na=[]\nb=[]\nsum=0\na=gets.chomp.strip.split(' ')\ni=0\nwhile iv<\n"}, {"source_code": "A = a = gets.to_i\n\nx = 0\nwhile a % 12 == 0\n\ta /= 12\n\tx += 1\nend\n\nb = [2, 13]\n300.times{b.push 13 * b[-1] - 12 * b[-2]}\ny = 0\ny += 1 while b[y] < a\n\nif b[y] != a\n\tputs :NO\nelse\n\tz = 2 * x + y\n\tc = []\n\t(0 .. z / 2).each{|i| c << b[z - 2 * i] * 12 ** i if i != x}\n\tputs :YES\n\tp 1, z + 1, c.size, *c\nend\n"}], "src_uid": "0ef5e0621f13107d0c8786766ae2ac56"} {"nl": {"description": "Captain Marmot wants to prepare a huge and important battle against his enemy, Captain Snake. For this battle he has n regiments, each consisting of 4 moles.Initially, each mole i (1 ≤ i ≤ 4n) is placed at some position (xi, yi) in the Cartesian plane. Captain Marmot wants to move some moles to make the regiments compact, if it's possible.Each mole i has a home placed at the position (ai, bi). Moving this mole one time means rotating his position point (xi, yi) 90 degrees counter-clockwise around it's home point (ai, bi).A regiment is compact only if the position points of the 4 moles form a square with non-zero area.Help Captain Marmot to find out for each regiment the minimal number of moves required to make that regiment compact, if it's possible.", "input_spec": "The first line contains one integer n (1 ≤ n ≤ 100), the number of regiments. The next 4n lines contain 4 integers xi, yi, ai, bi ( - 104 ≤ xi, yi, ai, bi ≤ 104).", "output_spec": "Print n lines to the standard output. If the regiment i can be made compact, the i-th line should contain one integer, the minimal number of required moves. Otherwise, on the i-th line print \"-1\" (without quotes).", "sample_inputs": ["4\n1 1 0 0\n-1 1 0 0\n-1 1 0 0\n1 -1 0 0\n1 1 0 0\n-2 1 0 0\n-1 1 0 0\n1 -1 0 0\n1 1 0 0\n-1 1 0 0\n-1 1 0 0\n-1 1 0 0\n2 2 0 1\n-1 0 0 -2\n3 0 0 -2\n-1 1 -2 0"], "sample_outputs": ["1\n-1\n3\n3"], "notes": "NoteIn the first regiment we can move once the second or the third mole.We can't make the second regiment compact.In the third regiment, from the last 3 moles we can move once one and twice another one.In the fourth regiment, we can move twice the first mole and once the third mole."}, "positive_code": [{"source_code": "def equidistant(points)\n\treturn false if (points[1][0]-points[3][0])**2 + (points[1][1]-points[3][1])**2 !=\n\t\t\t(points[2][0] - points[0][0])**2 + (points[2][1] - points[0][1])**2\n\td = (points[0][0]-points[3][0])**2 + (points[0][1]-points[3][1])**2\n\treturn false if d == 0\n\t3.times do |i|\n\t\treturn false if d != (points[i+1][0]-points[i][0])**2 +\n\t\t\t\t(points[i+1][1]-points[i][1])**2\n\tend\n\ttrue\nend\ndef swap(arr, a, b)\n\tarr[a], arr[b] = arr[b], arr[a]\nend\ndef is_square(points)\n\treturn false if points.uniq.length != 4\n\treturn true if equidistant(points)\n\tswap(points, 2, 3)\n\treturn true if equidistant(points)\n\tswap(points, 1, 3)\n\treturn true if equidistant(points)\n\tswap(points, 2, 3)\n\treturn true if equidistant(points)\n\tswap(points, 1, 3)\n\treturn true if equidistant(points)\n\tswap(points, 2, 3)\n\treturn true if equidistant(points)\n\treturn false\nend\n\nn = STDIN.readline.to_i\nn.times do\n\trotations = []\n\t4.times do\n\t\tx, y, a, b = STDIN.readline.split.map {|s| s.to_i }\n\t\tdx, dy = x-a, y-b\n\t\trotations.push([\n\t\t\t\t[a+dx, b+dy],\n\t\t\t\t[a-dy, b+dx],\n\t\t\t\t[a-dx, b-dy],\n\t\t\t\t[a+dy, b-dx]])\n\tend\n\tbest = 13\n\t256.times do |i|\n\t\trotate = []\n\t\t4.times do\n\t\t\trotate.push(i%4)\n\t\t\ti /= 4\n\t\tend\n\t\tsum = rotate.inject(:+)\n\t\tnext if sum >= best\n\t\tpoints = []\n\t\t4.times do |j|\n\t\t\tpoints.push(rotations[j][rotate[j]])\n\t\tend\n\t\tbest = sum if is_square(points)\n\tend\n\tputs (best == 13 ? -1 : best)\nend\n"}], "negative_code": [{"source_code": "def is_square(points)\n\txs = points.map {|point| point[0] }\n\tys = points.map {|point| point[1] }\n\tx_min, x_max = xs.min, xs.max\n\treturn false if x_min == x_max\n\ty_min, y_max = ys.min, ys.max\n\treturn false if y_min == y_max\n\texpect = [[x_min, y_min], [x_min, y_max], [x_max, y_min], [x_max, y_max]]\n\texpect == points.sort\nend\n\nn = STDIN.readline.to_i\nn.times do\n\trotations = []\n\t4.times do\n\t\tx, y, a, b = STDIN.readline.split.map {|s| s.to_i }\n\t\tdx, dy = x-a, y-b\n\t\trotations.push([\n\t\t\t\t[a+dx, b+dy],\n\t\t\t\t[a-dy, b+dx],\n\t\t\t\t[a-dx, b-dy],\n\t\t\t\t[a+dy, b-dx]])\n\tend\n\tbest = 13\n\t256.times do |i|\n\t\trotate = []\n\t\t4.times do\n\t\t\trotate.push(i%4)\n\t\t\ti /= 4\n\t\tend\n\t\tsum = rotate.inject(:+)\n\t\tnext if sum >= best\n\t\tpoints = []\n\t\t4.times do |j|\n\t\t\tpoints.push(rotations[j][rotate[j]])\n\t\tend\n\t\tbest = sum if is_square(points)\n\tend\n\tputs (best == 13 ? -1 : best)\nend\n"}, {"source_code": "def equidistant(points)\n\td = (points[0][0] - points[3][0])**2 + (points[0][1] - points[3][1])**2\n\treturn false if d == 0\n\t3.times do |i|\n\t\treturn false if d != (points[i+1][0] - points[i][0])**2 +\n\t\t\t\t(points[i+1][1] - points[i][1])**2\n\tend\n\ttrue\nend\ndef swap(arr, a, b)\n\tarr[a], arr[b] = arr[b], arr[a]\nend\ndef is_square(points)\n\treturn false if points.uniq.length != 4\n\treturn true if equidistant(points)\n\tswap(points, 2, 3)\n\treturn true if equidistant(points)\n\tswap(points, 1, 3)\n\treturn true if equidistant(points)\n\tswap(points, 2, 3)\n\treturn true if equidistant(points)\n\tswap(points, 1, 3)\n\treturn true if equidistant(points)\n\tswap(points, 2, 3)\n\treturn true if equidistant(points)\n\treturn false\nend\n\nn = STDIN.readline.to_i\nn.times do\n\trotations = []\n\t4.times do\n\t\tx, y, a, b = STDIN.readline.split.map {|s| s.to_i }\n\t\tdx, dy = x-a, y-b\n\t\trotations.push([\n\t\t\t\t[a+dx, b+dy],\n\t\t\t\t[a-dy, b+dx],\n\t\t\t\t[a-dx, b-dy],\n\t\t\t\t[a+dy, b-dx]])\n\tend\n\tbest = 13\n\t256.times do |i|\n\t\trotate = []\n\t\t4.times do\n\t\t\trotate.push(i%4)\n\t\t\ti /= 4\n\t\tend\n\t\tsum = rotate.inject(:+)\n\t\tnext if sum >= best\n\t\tpoints = []\n\t\t4.times do |j|\n\t\t\tpoints.push(rotations[j][rotate[j]])\n\t\tend\n\t\tbest = sum if is_square(points)\n\tend\n\tputs (best == 13 ? -1 : best)\nend\n"}], "src_uid": "41d791867f27a57b50eebaad29754520"} {"nl": {"description": "Little Vasya's uncle is a postman. The post offices are located on one circular road. Besides, each post office has its own gas station located next to it. Petya's uncle works as follows: in the morning he should leave the house and go to some post office. In the office he receives a portion of letters and a car. Then he must drive in the given car exactly one round along the circular road and return to the starting post office (the uncle can drive along the circle in any direction, counterclockwise or clockwise). Besides, since the car belongs to the city post, it should also be fuelled with gasoline only at the Post Office stations. The total number of stations equals to n. One can fuel the car at the i-th station with no more than ai liters of gasoline. Besides, one can fuel the car no more than once at each station. Also, the distance between the 1-st and the 2-nd station is b1 kilometers, the distance between the 2-nd and the 3-rd one is b2 kilometers, ..., between the (n - 1)-th and the n-th ones the distance is bn - 1 kilometers and between the n-th and the 1-st one the distance is bn kilometers. Petya's uncle's high-tech car uses only one liter of gasoline per kilometer. It is known that the stations are located so that the sum of all ai is equal to the sum of all bi. The i-th gas station and i-th post office are very close, so the distance between them is 0 kilometers.Thus, it becomes clear that if we start from some post offices, then it is not always possible to drive one round along a circular road. The uncle faces the following problem: to what stations can he go in the morning to be able to ride exactly one circle along the circular road and visit all the post offices that are on it?Petya, who used to attend programming classes, has volunteered to help his uncle, but his knowledge turned out to be not enough, so he asks you to help him write the program that will solve the posed problem.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 105). The second line contains n integers ai — amount of gasoline on the i-th station. The third line contains n integers b1, b2, ..., bn. They are the distances between the 1-st and the 2-nd gas stations, between the 2-nd and the 3-rd ones, ..., between the n-th and the 1-st ones, respectively. The sum of all bi equals to the sum of all ai and is no more than 109. Each of the numbers ai, bi is no less than 1 and no more than 109.", "output_spec": "Print on the first line the number k — the number of possible post offices, from which the car can drive one circle along a circular road. Print on the second line k numbers in the ascending order — the numbers of offices, from which the car can start.", "sample_inputs": ["4\n1 7 2 3\n8 1 1 3", "8\n1 2 1 2 1 2 1 2\n2 1 2 1 2 1 2 1"], "sample_outputs": ["2\n2 4", "8\n1 2 3 4 5 6 7 8"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/ruby\n\ndef solve(n, a, b)\n fuel = a[0]\n arr = [fuel]\n (n-1).times { |i|\n fuel -= b[i]\n arr << fuel\n fuel += a[i+1]\n }\n min = arr.min\n ret = []\n if min >= 0\n ret << 1\n min = 0\n end\n arr.each_with_index { |v, i|\n ret << i+1 if v == min\n }\n ret\nend\n\nn = gets.to_i\na = gets.split.map(&:to_i)\nb = gets.split.map(&:to_i)\nb_rev = b.reverse\nb_rev = b_rev[1..-1] + [b_rev[0]]\na1 = solve(n, a, b)\na2 = solve(n, a.reverse, b_rev).map{|v| n+1-v}\nans = (a1+a2).sort.uniq\nputs \"#{ans.size}\\n#{ans.join(' ')}\"\n"}, {"source_code": "#!/usr/bin/ruby\n\ndef solve(n, a, b)\n fuel = a[0]\n arr = [fuel]\n (n-1).times { |i|\n fuel -= b[i]\n arr << fuel\n fuel += a[i+1]\n }\n min = arr.min\n ret = []\n if min < 0\n a.each_with_index { |v,i|\n ret << i+1 if arr[i] == min\n }\n else\n ret << 1\n a.each_with_index { |v,i|\n ret << i+1 if arr[i] == 0\n }\n end\n ret\nend\n\nn = gets.to_i\na = gets.split.map(&:to_i)\nb = gets.split.map(&:to_i)\nb_rev = b.reverse\nb_rev = b_rev[1..-1] + [b_rev[0]]\na1 = solve(n, a, b)\na2 = solve(n, a.reverse, b_rev).map{|v| n+1-v}\nans = (a1+a2).sort.uniq\nputs \"#{ans.size}\\n#{ans.join(' ')}\"\n"}, {"source_code": "def solve(n, a, b)\n s = 0\n arr = a[0..0] + a[0..-2].each_with_index.map{|x, i| s += x - b[i]}\n m = (arr + [0]).min\n res = (0...n).select {|i| arr[i] == m }\n res << 0 if m == 0\n res\nend\n\nn = gets.to_i\na = gets.split.map(&:to_i)\nb = gets.split.map(&:to_i)\nr = (solve(n, a, b) + solve(n, a.reverse, b.reverse[1..-1] + [b[-1]]).map{|x| n - x - 1}).sort.uniq.map{|x| x + 1}\nputs \"#{r.size}\\n#{r.join(' ')}\"\n"}, {"source_code": "def solve(n, a, b)\n s = 0\n arr = a[0..0] + a[0..-2].each_with_index.map{|x, i| s += x - b[i]}\n m = (arr + [0]).min\n res = (0...n).select {|i| arr[i] == m }\n res << 0 if m == 0\n res\nend\n\nn = gets.to_i\na = gets.split.map(&:to_i)\nb = gets.split.map(&:to_i)\nr = (solve(n, a, b) + solve(n, a.reverse, b.reverse[1..-1] + [b[-1]]).map{|x| n - x - 1}).sort.uniq.map{|x| x + 1}\nputs \"#{r.size}\\n#{r.join(' ')}\"\n"}, {"source_code": "def solve(n, a, b)\n s = 0\n arr = a[0..0] + a[0..-2].each_with_index.map{|x, i| s += x - b[i]}\n m = (arr + [0]).min\n res = (0...n).select {|i| arr[i] == m }\n res << 0 if m == 0\n res\nend\n\nn = gets.to_i\na = gets.split.map(&:to_i)\nb = gets.split.map(&:to_i)\nr = (solve(n, a, b) + solve(n, a.reverse, b.reverse[1..-1] + [b[-1]]).map{|x| n - x - 1}).sort.uniq.map{|x| x + 1}\nputs \"#{r.size}\\n#{r.join(' ')}\"\n"}, {"source_code": "def solve(n, a, b)\n s = 0\n arr = a[0..0] + a[0..-2].each_with_index.map{|x, i| s += x - b[i]}\n m = (arr + [0]).min\n res = (0...n).select {|i| arr[i] == m }\n res << 0 if m == 0\n res\nend\n\nn = gets.to_i\na = gets.split.map(&:to_i)\nb = gets.split.map(&:to_i)\nr = (solve(n, a, b) + solve(n, a.reverse, b.reverse[1..-1] + [b[-1]]).map{|x| n - x - 1}).sort.uniq.map{|x| x + 1}\nputs \"#{r.size}\\n#{r.join(' ')}\"\n"}, {"source_code": "def solve(n, a, b)\n s = 0\n arr = a[0..0] + a[0..-2].each_with_index.map{|x, i| s += x - b[i]}\n m = (arr + [0]).min\n res = (0...n).select {|i| arr[i] == m }\n res << 0 if m == 0\n res\nend\n\nn = gets.to_i\na = gets.split.map(&:to_i)\nb = gets.split.map(&:to_i)\nr = (solve(n, a, b) + solve(n, a.reverse, b.reverse[1..-1] + [b[-1]]).map{|x| n - x - 1}).sort.uniq.map{|x| x + 1}\nputs \"#{r.size}\\n#{r.join(' ')}\""}, {"source_code": "def solve(n, a, b)\n s = 0\n arr = a[0..0] + a[0..-2].each_with_index.map{|x, i| s += x - b[i]}\n m = (arr + [0]).min\n res = (0...n).select {|i| arr[i] == m }\n res << 0 if m == 0\n res\nend\n\nn = gets.to_i\na = gets.split.map(&:to_i)\nb = gets.split.map(&:to_i)\nr = (solve(n, a, b) + solve(n, a.reverse, b.reverse[1..-1] + [b[-1]]).map{|x| n - x - 1}).sort.uniq.map{|x| x + 1}\nputs \"#{r.size}\\n#{r.join(' ')}\""}, {"source_code": "def solve(n, a, b)\n s = 0\n arr = a[0..0] + a[0..-2].each_with_index.map{|x, i| s += x - b[i]}\n m = (arr + [0]).min\n res = (0...n).select {|i| arr[i] == m }\n res << 0 if m == 0\n res\nend\n\nn = gets.to_i\na = gets.split.map(&:to_i)\nb = gets.split.map(&:to_i)\nr = (solve(n, a, b) + solve(n, a.reverse, b.reverse[1..-1] + [b[-1]]).map{|x| n - x - 1}).sort.uniq.map{|x| x + 1}\nputs \"#{r.size}\\n#{r.join(' ')}\"\n"}, {"source_code": "def solve(n, a, b)\n s = 0\n arr = a[0..0] + a[0..-2].each_with_index.map{|x, i| s += x - b[i]}\n m = (arr + [0]).min\n res = (0...n).select {|i| arr[i] == m }\n res << 0 if m == 0\n res\nend\n\nn = gets.to_i\na = gets.split.map(&:to_i)\nb = gets.split.map(&:to_i)\nr = (solve(n, a, b) + solve(n, a.reverse, b.reverse[1..-1] + [b[-1]]).map{|x| n - x - 1}).sort.uniq.map{|x| x + 1}\nputs \"#{r.size}\\n#{r.join(' ')}\"\n"}, {"source_code": "def solve(n, a, b)\n s = 0\n arr = a[0..0] + a[0..-2].each_with_index.map{|x, i| s += x - b[i]}\n m = (arr + [0]).min\n res = (0...n).select {|i| arr[i] == m }\n res << 0 if m == 0\n res\nend\n\nn = gets.to_i\na = gets.split.map(&:to_i)\nb = gets.split.map(&:to_i)\nr = (solve(n, a, b) + solve(n, a.reverse, b.reverse[1..-1] + [b[-1]]).map{|x| n - x - 1}).sort.uniq.map{|x| x + 1}\nputs \"#{r.size}\\n#{r.join(' ')}\"\n"}, {"source_code": "def solve(n, a, b)\n s = 0\n arr = a[0..0] + a[0..-2].each_with_index.map{|x, i| s += x - b[i]}\n m = (arr + [0]).min\n res = (0...n).select {|i| arr[i] == m }\n res << 0 if m == 0\n res\nend\n\nn = gets.to_i\na = gets.split.map(&:to_i)\nb = gets.split.map(&:to_i)\nr = (solve(n, a, b) + solve(n, a.reverse, b.reverse[1..-1] + [b[-1]]).map{|x| n - x - 1}).sort.uniq.map{|x| x + 1}\nputs \"#{r.size}\\n#{r.join(' ')}\"\n"}], "negative_code": [{"source_code": "#!/usr/bin/ruby\n\ndef solve(n, a, b)\n fuel = a[0]\n arr = [fuel]\n (n-1).times { |i|\n fuel -= b[i]\n arr << fuel\n fuel += a[i+1]\n }\n min = arr.min\n ret = []\n if min < 0\n ret << 1\n min = 0\n end\n a.each_with_index { |v,i|\n ret << i+1 if arr[i] == 0\n }\n ret\nend\n\nn = gets.to_i\na = gets.split.map(&:to_i)\nb = gets.split.map(&:to_i)\nb_rev = b.reverse\nb_rev = b_rev[1..-1] + [b_rev[0]]\na1 = solve(n, a, b)\na2 = solve(n, a.reverse, b_rev).map{|v| n+1-v}\nans = (a1+a2).sort.uniq\nputs \"#{ans.size}\\n#{ans.join(' ')}\"\n"}], "src_uid": "cc928c02b337f2b5ad7aed37bb83d139"} {"nl": {"description": "After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to m, correspondingly. The balls are numbered with numbers from 1 to n.Valeric decided to sort the balls in the order of increasing of their numbers by the following scheme. He will put each new ball in the basket with the least number of balls. And if he's got several variants, he chooses the basket which stands closer to the middle. That means that he chooses the basket for which is minimum, where i is the number of the basket. If in this case Valeric still has multiple variants, he chooses the basket with the minimum number.For every ball print the number of the basket where it will go according to Valeric's scheme.Note that the balls are sorted into baskets in the order of increasing numbers, that is, the first ball goes first, then goes the second ball and so on.", "input_spec": "The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of balls and baskets, correspondingly.", "output_spec": "Print n numbers, one per line. The i-th line must contain the number of the basket for the i-th ball.", "sample_inputs": ["4 3", "3 1"], "sample_outputs": ["2\n1\n3\n2", "1\n1\n1"], "notes": null}, "positive_code": [{"source_code": "n,m = gets.split.map{|e| e.to_i}\nfor i in (1..n) do\n k = i.modulo(m)\n if k==0 then k=m end\n r = k/2\n na = k.modulo(2)\n sr = (m+1)/2\n if na==0 then na=-1 end \n if m.modulo(2)==0 then na*=-1 end\n ans=sr+na*r\n puts ans \nend\n"}, {"source_code": "(n,m) = gets.split.map(&:to_i)\nif(m % 2 ==0)\nth = (m/2).ceil\nw = 0\n n.times do |i|\n th += w\n if(w > 0)\n w +=1\n else\n w -=1\n end\n w *=-1\n puts th\n if(th == m)\n th = (m/2).ceil\n w = 0\n end\n end\nelse\n th = (m/2).ceil+1\n w = 0\n n.times do |i|\n th -= w\n if(w > 0)\n w +=1\n else\n w -=1\n end\n w *=-1\n puts th\n if(th == m)\n th = (m/2).ceil+1\n w = 0\n end\n end\nend"}, {"source_code": "#!/usr/bin/ruby\nn, m = gets.chomp.split(' ').map{|a| a.to_i}\nif m == 1\n\tn.times do\n\t\tputs 1\n\tend\n\texit\nend \nif m == 2\n\tn.times do |i|\n\t\tputs (i+1)%2 == 1 ? 1:2\n\tend\n\texit\nend\nif m%2 == 1\n\tk = (m/2.0).ceil\n\tfor i in 1..n\n\t\tif i > m\n\t\t\ti = i % m;\n\t\t\tif i == 0\n\t\t\t\ti = m\n\t\t\tend\n\t\tend\n\t\tif i%2 == 0\n\t\t\tputs k-((i+2)/2)+1;\n\t\tend\n\t\tif i%2 == 1\n\t\t\tputs k+((i+1)/2)-1;\n\t\tend\n\tend\nelse\n\tk = m/2\n\tfor i in 1..n\n\t\tif i > m\n\t\t\ti = i % m;\n\t\t\tif i == 0\n\t\t\t\ti = m\n\t\t\tend\n\t\tend\n\t\tif i%2 == 1\n\t\t\tputs k-(i+1)/2+1;\n\t\tend\n\t\tif i%2 == 0\n\t\t\tputs k+(i/2);\n\t\tend\n\tend\nend\n"}, {"source_code": "n,m = gets.split.map(&:to_i)\ni=0\nwhile i= 1\n\tarr << val1 if val1 <= m\n\ti += 1\nend\narr = arr.to_a\nn.times do |i|\n\tputs arr[i % m]\nend"}, {"source_code": "n,m = gets.split.map(&:to_i)\nd = (m+1)/2\ni = d\nfor k in 1..n\n p i\n if i==d and m.odd? then i=d-1\n elsif i==d and !m.odd? then i=d+1\n elsif i>d then i=m-i\n elsif im then i=d end\nend\n"}, {"source_code": "a = []\ngets.split.each { |_| a.push(_.to_i)}\n# a[0] - мячи\n# a[1] -корзины\n\nif a[1].odd?\n #нечетное\n plu = (a[1]/2.0).ceil\n for i in 1..a[0] do\n k = i % a[1]\n k = a[1] if k == 0 # костыль если количество мячей больше корзин начать счет снова при пересечении рубежа равенства количеству корзин \n if k.odd?\n p plu + (k-1)/2 if k != 1 # ---\n p plu if k == 1 # Костыль на начальное число которое не поддерживает изменений при первом мяче\n else\n p plu - k/2\n end\n end\nelse\n #четное\n plu = (a[1]/2.0).ceil\n for i in 1..a[0] do\n k = i % a[1]\n k = a[1] if k == 0 # костыль если количество мячей больше корзин начать счет снова при пересечении рубежа равенства количеству корзин \n if k.odd?\n p plu - (k-1)/2 if k != 1 # ---\n p plu if k == 1 # Костыль на начальное число которое не поддерживает изменений при первом мяче\n else\n p plu + k/2\n end\n end\n \nend"}], "negative_code": [{"source_code": "#!/usr/bin/ruby\nn, m = gets.chomp.split(' ').map{|a| a.to_i}\nif m == 1\n\tn.times do\n\t\tputs 1\n\tend\n\texit\nend \nif m == 2\n\tn.times do |i|\n\t\tputs (i+1)%2 == 1 ? 1:2\n\tend\n\texit\nend\nif m%2 == 1\n\tk = (m/2.0).ceil\n\tfor i in 1..n\n\t\tif i > m\n\t\t\ti = i % m;\n\t\t\tif i == 0\n\t\t\t\ti = m\n\t\t\tend\n\t\tend\n\t\tif i%2 == 0\n\t\t\tputs k-((i+2)/2)+1;\n\t\tend\n\t\tif i%2 == 1\n\t\t\tputs k+((i+1)/2)-1;\n\t\tend\n\tend\nelse\n\tk = m/2.0\n\tfor i in 1..n\n\t\tif i > m\n\t\t\ti = i % m;\n\t\t\tif i == 0\n\t\t\t\ti = m\n\t\t\tend\n\t\tend\n\t\tif i%2 == 1\n\t\t\tputs k-i+1;\n\t\tend\n\t\tif i%2 == 2\n\t\t\tputs k+((i+1)/2)-1;\n\t\tend\n\tend\nend\n"}, {"source_code": "arr = []\nn,m = gets.split(\" \").map(&:to_i)\n\nval = (m+1) / 2\narr << val\ni = 1\nwhile arr.length < m\n\tval1 = val + i\n\tval2 = val - i\n\tarr << val2 if val2 >= 1\n\tarr << val1 if val1 <= m\n\ti += 1\nend\n\nn.times do |i|\n\tputs arr[i % m]\nend"}, {"source_code": "require 'Set'\narr = Set.new\nn,m = gets.split(\" \").map(&:to_i)\n\nval = (m+1) / 2\narr << val\narr << val + 1 if m.even?\ni = 1\nwhile arr.length < m\n\tval1 = val + i\n\tval2 = val - i\n\tarr << val2 if val2 >= 1\n\tarr << val1 if val1 <= m\n\ti += 1\nend\narr = arr.to_a\nn.times do |i|\n\tputs arr[i % m]\nend"}, {"source_code": "a = []\ngets.split.each { |_| a.push(_.to_i) }\n\nb = [0] * a[1] # количество корзин и все корзины\n\ndef get_indexes(a_,v_) # получаем все индексы массива a_ по значению v_\n ha = []\n a_.each_with_index do |v,i|\n ha.push(i) if v == v_\n end\n ha\nend\n\na[0].times do #делаем по количеству мячей\n ins = get_indexes(b, b.min) # просим индексы\n if ins.count > 1 # если индексов больше одного то необходимо выбрать ближайшую к центру\n h = ins.inject(Hash.new(0)) { |h,v| h[v] = ((a[1]+1)/2-(v+1)).abs; h } # получаем индекс каждой корзины\n p h.key(h.values.min) + 1 # выбираем наименьший индекс или ближайшую корзину\n b[h.key(h.values.min)] += 1 # клдем мяч\n else\n p b.index(b.min) + 1 \n b[b.index(b.min)] += 1\n end\nend\n\n"}, {"source_code": "a = []\ngets.split.each { |_| a.push(_.to_i) }\n\nb = [0] * a[1] # количество корзин и все корзины\n\ndef get_indexes(a_,v_) # получаем все индексы массива a_ по значению v_\n ha = []\n a_.each_with_index do |v,i|\n ha.push(i) if v == v_\n end\n ha\nend\n\na[0].times do #делаем по количеству мячей\n ins = get_indexes(b, b.min) # просим индексы\n if ins.count > 1 # если индексов больше одного то необходимо выбрать ближайшую к центру\n h = ins.inject(Hash.new(0)) { |h,v| h[v] = (a[1]+1/2-(v+1)).abs; h } # получаем индекс каждой корзины\n p h.key(h.values.min) + 1 # выбираем наименьший индекс или ближайшую корзину\n b[h.key(h.values.min)] += 1 # клдем мяч\n else\n p b.index(b.min) + 1 \n b[b.index(b.min)] += 1\n end\nend\n\n"}], "src_uid": "907893a0a78a7444d26bdd793d9c7499"} {"nl": {"description": "There always is something to choose from! And now, instead of \"Noughts and Crosses\", Inna choose a very unusual upgrade of this game. The rules of the game are given below:There is one person playing the game. Before the beginning of the game he puts 12 cards in a row on the table. Each card contains a character: \"X\" or \"O\". Then the player chooses two positive integers a and b (a·b = 12), after that he makes a table of size a × b from the cards he put on the table as follows: the first b cards form the first row of the table, the second b cards form the second row of the table and so on, the last b cards form the last (number a) row of the table. The player wins if some column of the table contain characters \"X\" on all cards. Otherwise, the player loses.Inna has already put 12 cards on the table in a row. But unfortunately, she doesn't know what numbers a and b to choose. Help her win the game: print to her all the possible ways of numbers a, b that she can choose and win.", "input_spec": "The first line of the input contains integer t (1 ≤ t ≤ 100). This value shows the number of sets of test data in the input. Next follows the description of each of the t tests on a separate line. The description of each test is a string consisting of 12 characters, each character is either \"X\", or \"O\". The i-th character of the string shows the character that is written on the i-th card from the start.", "output_spec": "For each test, print the answer to the test on a single line. The first number in the line must represent the number of distinct ways to choose the pair a, b. Next, print on this line the pairs in the format axb. Print the pairs in the order of increasing first parameter (a). Separate the pairs in the line by whitespaces.", "sample_inputs": ["4\nOXXXOXOOXOOX\nOXOXOXOXOXOX\nXXXXXXXXXXXX\nOOOOOOOOOOOO"], "sample_outputs": ["3 1x12 2x6 4x3\n4 1x12 2x6 3x4 6x2\n6 1x12 2x6 3x4 4x3 6x2 12x1\n0"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/ruby\ngets.to_i.times{\n\ts=gets.chomp.split('')\n\tn=s.size\n\th={}\n\tn.times{|i|h[i]=1 if s[i]=='X'}\n\tr=(1..n).select{|i|n%i==0 && (n/i).times.any?{|j|\n\t\ti.times.all?{|k|h[k*(n/i)+j]}\n\t}}.map{|e|'%dx%d'%[e,n/e]}\n\tputs r.size.to_s+' '+r*' '\n}"}, {"source_code": "t = gets.to_i\n\ndef check(row,s)\n arr = s.split('').each_slice(row).to_a.transpose\n\n arr.each_with_index do |r,ri|\n bool = true\n r.each_with_index do |rri,rj|\n if r[rj]=='O'\n bool = false \n end\n end\n\n if bool\n return true \n \n end \n end\n return false\nend\n\nt.times do |i|\n s = gets.chomp\n fact = [12,6,4,3,2,1]\n ans = []\n fact.each_with_index do |f,i|\n if check(f,s)\n a = (s.length/f).to_s+'x'+f.to_s\n ans << a\n end\n end\n puts ans.length.to_s+' '+ans.join(' ') \nend"}, {"source_code": "def run\n n = $stdin.gets.to_i\n $stdin.readlines.each do |line|\n chars = line.chomp.split(//)\n len = chars.length\n ans = []\n 1.upto(len) do |i|\n f = [true] * (len / i)\n next if len % i != 0\n len.times do |j|\n f[j % (len / i)] = false if chars[j] == 'O'\n end\n ans << \"#{i}x#{chars.length / i}\" if f.find_index(true)\n end\n puts \"#{ans.length} #{ans.join(\" \")}\"\n end\nend\n\nrun if $0 == __FILE__\n"}, {"source_code": "gets\nwhile s = gets\n r = []\n for a in 1..12\n if 12 % a == 0\n b = 12 / a\n ok = [ true ] * b\n s.chars.each_with_index do |c, i|\n ok[i % b] = false if c == 'O'\n end\n if ok.find_index(true)\n r << \"#{a}x#{b}\"\n end\n end\n end\n puts \"#{r.size} #{r.join(' ')}\"\nend"}, {"source_code": "tests = gets.to_i\ntests.times do\n s = gets\n result = []\n for a in 1..12\n if 12 % a == 0\n b = 12 / a\n ok = [ true ] * b\n s.split(//).each_with_index do |c, i|\n ok[i % b] = false if c == 'O'\n end\n if ok.find_index(true)\n result << \"#{a}x#{b}\"\n end\n end\n end\n puts \"#{result.size} #{result.join(' ')}\"\nend"}, {"source_code": "class ProblemA\n \n def initialize\n @dats = STDIN.read.split(\"\\n\")\n @num_of_dat = @dats[0]\n @final_str = \"\"\n end\n\n def solve(line)\n out_line = \"\"\n num_of_ways = 0\n return if line.length != 12 \n [1,2,3,4,6,12].each do |a|\n b = 12/a\n has_way = false\n str = \"\"\n (0..(b-1)).each do |col_idx|\n is_x = true \n (0..(a-1)).each do |row_idx|\n if line[col_idx+(row_idx)*b] != \"X\"\n is_x = false \n end\n end\n\n if is_x\n has_way = true\n num_of_ways += 1\n break\n end\n end\n out_line += \" #{a}x#{b}\" if has_way\n end\n @final_str += \"#{num_of_ways} #{out_line}\\n\"\n end\n \n def solve_all \n (1..@num_of_dat.to_i).each do |i|\n solve(@dats[i])\n end\n puts @final_str\n end\nend\n\nsolver = ProblemA.new\nsolver.solve_all\n"}, {"source_code": "n=gets.to_i\nn.times{\n\ta=gets.chomp\n\tans = []\n\t(1..12).each{ |i|\n\t\tif( 12 % i == 0 )\n\t\t\tb = []\n\t\t\tj = 0\n\t\t\twhile j < 12\n\t\t\t\tb<=4 && d<6\nansa=[]\nansa<=6 && d<12\nansa=[]\nansa<=4 && d<6\nansa=[]\nansa<=6 && d<12\nansa=[]\nansa<=4 && d<6\nansa=[]\nansa<=6 && d<12\nansa=[]\nansa<=4 && d<6\nansa=[]\nansa<=6 && d<12\nansa=[]\nansa< a[i][0] && a[i][0] < a[i + 1][0]\n\t\tend || 0\n\tputs (0...pos).sum { a[_1].size }\nend\n"}, {"source_code": "tc = gets.chomp.to_i \nwhile tc > 0\n n = gets.chomp.to_i\n a = gets.split.collect{|i| i.to_i}\n i = n - 1\n while i >= 1 && a[i - 1] >= a[i]\n i -= 1\n end\n while i >= 1 && a[i - 1] <= a[i]\n i -= 1\n end\n puts i\n tc -= 1\nend\n\n"}, {"source_code": "t = gets.chomp.to_i\nt.times do \n ln = gets.chomp.to_i\n a = gets.chomp.split(\" \").map(&:to_i)\n i = ln - 1\n while i >= 1 && a[i-1] >= a[i]\n i -= 1\n end\n while i >= 1 && a[i-1] <= a[i]\n i -= 1\n end\n puts i\nend"}, {"source_code": "tc = gets.to_i \n\ntc.times do \n n = gets.to_i \n arr = gets.split().map{|i| i.to_i} \n ans = 0 \n crr = Array.new(n) {0} \n maxi = arr[n-1] \n ((n-2).step(0,-1)).each do |i| \n maxi = [maxi,arr[i+1]].max\n crr[i] = maxi \n end\n (1...n-1).each do |i| \n if(arr[i] < arr[i-1] && arr[i] < crr[i]) \n ans = i \n end\n end\n puts(ans)\nend"}, {"source_code": "gets.to_i.times do \n n = gets.to_i\n a = gets.split.map(&:to_i)\n res = n - 1\n res -= 1 while res > 0 && a[res - 1] >= a[res]\n res -= 1 while res > 0 && a[res - 1] <= a[res]\n puts res\nend"}, {"source_code": "gets.to_i.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n rm = n - 1\n state = :inc\n (n - 2).downto(0) do |i|\n if a[i] < a[i + 1] then state = :dec end\n break if state == :dec and a[i] > a[i + 1]\n rm = i\n end\n puts rm\nend\n\n"}], "negative_code": [{"source_code": "gets.to_i.times do\n\tn = gets.to_i\n\ta = gets.split.map(&:to_i)\n\tputs (1...n - 1).reverse_each.find { |i|\n\t\t\ta[i - 1] > a[i] && a[i] < a[i + 1]\n\t } || 0\nend\n"}, {"source_code": "t = gets.chomp.to_i\nt.times do \n ln = gets.chomp.to_i\n a = gets.chomp.split(\" \").map(&:to_i)\n i = ln - 1\n while i >= 0 && a[i-1] >= a[i]\n i -= 1\n end\n while i >= 0 && a[i-1] < a[i]\n i -= 1\n end\n puts i\nend"}, {"source_code": "t = gets.chomp.to_i\nt.times do \n ln = gets.chomp.to_i\n a = gets.chomp.split(\" \").map(&:to_i)\n i = ln - 1\n while i >= 0 && a[i-1] >= a[i]\n i -= 1\n end\n while i >= 0 && a[i-1] <= a[i]\n i -= 1\n end\n puts i\nend"}, {"source_code": "t = gets.chomp.to_i\nt.times do \n ln = gets.chomp.to_i\n a = gets.chomp.split(\" \").map(&:to_i)\n i = ln - 1\n while i >= 0 && a[i-1] > a[i]\n i -= 1\n end\n while i >= 0 && a[i-1] < a[i]\n i -= 1\n end\n puts i\nend"}, {"source_code": "tc = gets.to_i \n\ntc.times do \n n = gets.to_i \n arr = gets.split().map{|i| i.to_i} \n\n brr = Array.new(n) {0}\n crr = Array.new(n) {0} \n\n maxi = arr[0] \n (1...n).each do |i| \n maxi = [maxi,arr[i-1]].max\n brr[i] = maxi \n end\n # print(brr)\n maxi = arr[n-1]\n ((n-2).step(0,-1)).each do |i| \n maxi = [maxi,arr[i+1]].max\n crr[i] = maxi \n end\n # print(crr)\n ans = 0\n (1...n-1).each do |i| \n if(arr[i] < brr[i] && arr[i] < crr[i]) \n ans = i \n end\n end\n puts(ans)\nend"}, {"source_code": "tc = gets.to_i \n\ntc.times do \n n = gets.to_i \n arr = gets.split().map{|i| i.to_i} \n # print(arr)\n brr = Array.new(n) {0}\n crr = Array.new(n) {0} \n\n maxi = arr[0] \n (1...n).each do |i| \n maxi = [maxi,arr[i-1]].max\n brr[i] = maxi \n end\n # print(brr)\n maxi = arr[n-1]\n ((n-2).step(0,-1)).each do |i| \n maxi = [maxi,arr[i+1]].max\n crr[i] = maxi \n end\n # print(crr)\n ans = 0\n (1...n-1).each do |i| \n if(arr[i] < brr[i] && arr[i] < crr[i]) \n if(arr[i] != arr[i-1])\n ans = i \n end\n end\n end\n puts(ans)\nend"}, {"source_code": "tc = gets.to_i \n\ntc.times do \n n = gets.to_i \n arr = gets.split().map{|i| i.to_i} \n ans = 0 \n (1...n-1).each do |i| \n if(arr[i] < arr[i-1] && arr[i] < arr[i+1]) \n ans = i \n end\n end\n puts(ans)\nend"}, {"source_code": "gets.to_i.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n rm = n - 2\n state = :inc\n (n - 2).downto(0) do |i|\n if a[i] < a[i + 1] then state = :dec end\n break if state == :dec and a[i] > a[i + 1]\n rm = i\n end\n puts rm\nend\n\n"}], "src_uid": "731b45747081a800fc6da02efa5ce8cd"} {"nl": {"description": "You have array of $$$n$$$ numbers $$$a_{1}, a_{2}, \\ldots, a_{n}$$$. Rearrange these numbers to satisfy $$$|a_{1} - a_{2}| \\le |a_{2} - a_{3}| \\le \\ldots \\le |a_{n-1} - a_{n}|$$$, where $$$|x|$$$ denotes absolute value of $$$x$$$. It's always possible to find such rearrangement.Note that all numbers in $$$a$$$ are not necessarily different. In other words, some numbers of $$$a$$$ may be same.You have to answer independent $$$t$$$ test cases.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^{4}$$$) — the number of test cases. The first line of each test case contains single integer $$$n$$$ ($$$3 \\le n \\le 10^{5}$$$) — the length of array $$$a$$$. It is guaranteed that the sum of values of $$$n$$$ over all test cases in the input does not exceed $$$10^{5}$$$. The second line of each test case contains $$$n$$$ integers $$$a_{1}, a_{2}, \\ldots, a_{n}$$$ ($$$-10^{9} \\le a_{i} \\le 10^{9}$$$).", "output_spec": "For each test case, print the rearranged version of array $$$a$$$ which satisfies given condition. If there are multiple valid rearrangements, print any of them.", "sample_inputs": ["2\n6\n5 -2 4 8 6 5\n4\n8 1 4 2"], "sample_outputs": ["5 5 4 6 8 -2\n1 2 4 8"], "notes": "NoteIn the first test case, after given rearrangement, $$$|a_{1} - a_{2}| = 0 \\le |a_{2} - a_{3}| = 1 \\le |a_{3} - a_{4}| = 2 \\le |a_{4} - a_{5}| = 2 \\le |a_{5} - a_{6}| = 10$$$. There are other possible answers like \"5 4 5 6 -2 8\".In the second test case, after given rearrangement, $$$|a_{1} - a_{2}| = 1 \\le |a_{2} - a_{3}| = 2 \\le |a_{3} - a_{4}| = 4$$$. There are other possible answers like \"2 4 8 1\"."}, "positive_code": [{"source_code": "# coding: utf-8\n\n# |a1 - a2| <= |a2 - a3| <= |a3 - a4| ....\n# 를 만족시키도록 a를 정렬\n# 꽤 재밌는 유형의 문제이다. 이렇게 숫자 배열을 타고 순회하는걸 oscillation이라 한다는군.\n# naive하게 구성한 알고리즘은 다음과 같음\n# 1. 삽입 정렬 구조를 가짐\n# 2. 빈 배열에 a1 삽입\n# 3. 빈 배열에 a2 삽입\n# (여기까진 |a1 - a2| = |a2 - a1|이므로 큰 지장은 없음)\n# 4. 빈 배열에 a3 을 넣기 위한 자리를 선택함( 전체 순회가 필요함 O(n))\n# 5. 이후 a3부터 aN까지 모든 절차를 반복해야하므로 O(n^2)\n# 이렇게 잡았지만 부분적으로는 해답일 수 있으나 아무래도 오답이 자꾸 나오네.\n# 결국은 몇번 고민해보다가 찾은 해가 이 접근법이다.\n\n# 일단 숫자를 모두 정렬한다. 1 2 9 5 8 8을 감소가 없는 순서대로 정렬하면,\n# 1 2 5 8 8 9 이다. 이 숫자는 재밌는 성질을 갖고 있는데, 한 숫자를 기준으로\n# 오른쪽은 0혹은 1이상만큼 크며 왼쪽은 0혹은 1이상만큼 작다.\n# 그래서 자신에게 k 거리만큼 먼쪽일 수록 자신과의 차이가 매우 커질거란 기대를 할 수 있다.\n# 자신에게 0에 가까운 거리일수록 자신과의 차이는 매우 작아진다.\n# 그래서 중앙에서부터 왼쪽 절반의 제일 후미부터, 오른쪽 절반의 제일 선두부터 숫자를\n# 추출하면 거리가 가까운 순서부터 먼 순서대로 뽑을 수 있다.\n\n# 처음에 평균을 이용한 접근을 한건 평균이 중앙에 올 경우에는 성공할 수 있는 방법이지만,\n# 평균값이 중앙에 있지 못한 경우에는 잘못된 동작을 할 수 있다.\n# 결국 평균이 아니라 '중간값'을 찾아야하는 문제. 정렬은 이러한 방법으로 순회하기 위해\n# 안정적인 전제를 제공하는 것이다.\nt = gets.to_i\nt.times {\n n = gets.to_i\n a = gets.split.map(&:to_i)\n a.sort!\n mid = n/2\n la, ra = a[0...mid], a[mid...n]\n b = []\n while !la.empty? || !ra.empty?\n b << ra.shift unless ra.empty?\n b << la.pop unless la.empty?\n end\n\n #p la\n #p ra\n \n puts b.join(' ')\n}"}, {"source_code": "t = gets.to_i\nt.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n a.sort!\n b = Array.new(n, nil)\n pos = (n - 1) / 2\n n.times do |i|\n b[i] = a[pos]\n if i % 2 == 0\n pos += i + 1\n else\n pos -= i + 1\n end\n end\n puts b.join(\" \")\nend\n"}], "negative_code": [{"source_code": "# coding: utf-8\n\n# |a1 - a2| <= |a2 - a3| <= |a3 - a4| ....\n# 를 만족시키도록 a를 정렬\n\nt = gets.to_i\nt.times {\n n = gets.to_i\n a = gets.split.map(&:to_i)\n a.sort!\n mid = n/2\n la, ra = a[0...mid], a[mid...n]\n b = []\n while !la.empty? && !ra.empty?\n b << ra.shift unless ra.empty?\n b << la.pop unless la.empty?\n end\n\n \n puts b.join(' ')\n}"}, {"source_code": "t = gets.to_i\nt.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n a.sort!\n b = Array.new(n, nil)\n pos = (n - 1) / 2\n n.times do |i|\n puts pos\n b[i] = a[pos]\n if i % 2 == 0\n pos += i + 1\n else\n pos -= i + 1\n end\n end\n puts b.join(\" \")\nend\n"}], "src_uid": "3c8bfd3199a9435cfbdee1daaacbd1b3"} {"nl": {"description": "There are $$$n$$$ people participating in some contest, they start participating in $$$x$$$ minutes intervals. That means the first participant starts at time $$$0$$$, the second participant starts at time $$$x$$$, the third — at time $$$2 \\cdot x$$$, and so on.Duration of contest is $$$t$$$ minutes for each participant, so the first participant finishes the contest at time $$$t$$$, the second — at time $$$t + x$$$, and so on. When a participant finishes the contest, their dissatisfaction equals to the number of participants that started the contest (or starting it now), but haven't yet finished it.Determine the sum of dissatisfaction of all participants.", "input_spec": "The first line contains a single integer $$$k$$$ ($$$1 \\le k \\le 1000$$$) — the number of test cases. Each of the next $$$k$$$ lines contains three integers $$$n$$$, $$$x$$$, $$$t$$$ ($$$1 \\le n, x, t \\le 2 \\cdot 10^9$$$) — the number of participants, the start interval and the contest duration.", "output_spec": "Print $$$k$$$ lines, in the $$$i$$$-th line print the total dissatisfaction of participants in the $$$i$$$-th test case.", "sample_inputs": ["4\n4 2 5\n3 1 2\n3 3 10\n2000000000 1 2000000000"], "sample_outputs": ["5\n3\n3\n1999999999000000000"], "notes": "NoteIn the first example the first participant starts at $$$0$$$ and finishes at time $$$5$$$. By that time the second and the third participants start, so the dissatisfaction of the first participant is $$$2$$$. The second participant starts at time $$$2$$$ and finishes at time $$$7$$$. By that time the third the fourth participants start, so the dissatisfaction of the second participant is $$$2$$$. The third participant starts at $$$4$$$ and finishes at $$$9$$$. By that time the fourth participant starts, so the dissatisfaction of the third participant is $$$1$$$.The fourth participant starts at $$$6$$$ and finishes at $$$11$$$. By time $$$11$$$ everyone finishes the contest, so the dissatisfaction of the fourth participant is $$$0$$$.In the second example the first participant starts at $$$0$$$ and finishes at time $$$2$$$. By that time the second participants starts, and the third starts at exactly time $$$2$$$. So the dissatisfaction of the first participant is $$$2$$$. The second participant starts at time $$$1$$$ and finishes at time $$$3$$$. At that time the third participant is solving the contest."}, "positive_code": [{"source_code": "k = gets.to_i\r\n\r\nwhile (k > 0)\r\n n, x, t = gets.split(' ').map(&:to_i)\r\n \r\n forFirst = [t / x, n - 1].min;\r\n\r\n puts(forFirst * [n - forFirst, 0].max + forFirst * (forFirst - 1) / 2)\r\n \r\n k -= 1\r\nend\r\n "}, {"source_code": "gets.to_i.times do\r\n n,x,t=gets.split.map(&:to_i)\r\n if t>=x*(n-1)\r\n puts (n-1)*n/2\r\n else\r\n cc=t/x\r\n puts cc*(cc+1)/2+(n-cc-1)*cc\r\n end\r\nend\r\n"}], "negative_code": [], "src_uid": "4df38c9b42b0f0963a121829080d3571"} {"nl": {"description": "You are given an array $$$a$$$ consisting of $$$n$$$ positive integers.You are allowed to perform this operation any number of times (possibly, zero): choose an index $$$i$$$ ($$$2 \\le i \\le n$$$), and change $$$a_i$$$ to $$$a_i - a_{i-1}$$$. Is it possible to make $$$a_i=0$$$ for all $$$2\\le i\\le n$$$?", "input_spec": "The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1\\le t\\le 100$$$)  — the number of test cases. The description of the test cases follows. The first line contains one integer $$$n$$$ ($$$2 \\le n \\le 100$$$) — the length of array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,\\ldots,a_n$$$ ($$$1 \\le a_i \\le 10^9$$$).", "output_spec": "For each test case, print \"YES\" (without quotes), if it is possible to change $$$a_i$$$ to $$$0$$$ for all $$$2 \\le i \\le n$$$, and \"NO\" (without quotes) otherwise. You can print letters in any case (upper or lower).", "sample_inputs": ["4\n\n2\n\n5 10\n\n3\n\n1 2 3\n\n4\n\n1 1 1 1\n\n9\n\n9 9 8 2 4 4 3 5 3"], "sample_outputs": ["YES\nYES\nYES\nNO"], "notes": "NoteIn the first test case, the initial array is $$$[5,10]$$$. You can perform $$$2$$$ operations to reach the goal: Choose $$$i=2$$$, and the array becomes $$$[5,5]$$$. Choose $$$i=2$$$, and the array becomes $$$[5,0]$$$. In the second test case, the initial array is $$$[1,2,3]$$$. You can perform $$$4$$$ operations to reach the goal: Choose $$$i=3$$$, and the array becomes $$$[1,2,1]$$$. Choose $$$i=2$$$, and the array becomes $$$[1,1,1]$$$. Choose $$$i=3$$$, and the array becomes $$$[1,1,0]$$$. Choose $$$i=2$$$, and the array becomes $$$[1,0,0]$$$. In the third test case, you can choose indices in the order $$$4$$$, $$$3$$$, $$$2$$$."}, "positive_code": [{"source_code": "def solve(arr)\r\n a0 = arr[0]\r\n (1...arr.length).each do |i|\r\n if arr[i] % a0 != 0\r\n puts \"NO\"\r\n return\r\n end\r\n end\r\n puts \"YES\"\r\nend\r\n\r\nn = gets.chomp.to_i\r\nn.times do\r\n gets\r\n solve(gets.chomp.split(' ').map(&:to_i))\r\nend"}], "negative_code": [{"source_code": "def solve(arr)\r\n a0 = arr[0]\r\n (1...arr.length).each do |i|\r\n if arr[i] % a0 != 0\r\n puts \"NO\"\r\n break\r\n end\r\n end\r\n puts \"YES\"\r\nend\r\n\r\nn = gets.chomp.to_i\r\nn.times do\r\n gets\r\n solve(gets.chomp.split(' ').map(&:to_i))\r\nend"}], "src_uid": "1c597da89880e87ffe791dd6b9fb2ac7"} {"nl": {"description": "You are planning to buy an apartment in a $$$n$$$-floor building. The floors are numbered from $$$1$$$ to $$$n$$$ from the bottom to the top. At first for each floor you want to know the minimum total time to reach it from the first (the bottom) floor.Let: $$$a_i$$$ for all $$$i$$$ from $$$1$$$ to $$$n-1$$$ be the time required to go from the $$$i$$$-th floor to the $$$(i+1)$$$-th one (and from the $$$(i+1)$$$-th to the $$$i$$$-th as well) using the stairs; $$$b_i$$$ for all $$$i$$$ from $$$1$$$ to $$$n-1$$$ be the time required to go from the $$$i$$$-th floor to the $$$(i+1)$$$-th one (and from the $$$(i+1)$$$-th to the $$$i$$$-th as well) using the elevator, also there is a value $$$c$$$ — time overhead for elevator usage (you need to wait for it, the elevator doors are too slow!). In one move, you can go from the floor you are staying at $$$x$$$ to any floor $$$y$$$ ($$$x \\ne y$$$) in two different ways: If you are using the stairs, just sum up the corresponding values of $$$a_i$$$. Formally, it will take $$$\\sum\\limits_{i=min(x, y)}^{max(x, y) - 1} a_i$$$ time units. If you are using the elevator, just sum up $$$c$$$ and the corresponding values of $$$b_i$$$. Formally, it will take $$$c + \\sum\\limits_{i=min(x, y)}^{max(x, y) - 1} b_i$$$ time units. You can perform as many moves as you want (possibly zero).So your task is for each $$$i$$$ to determine the minimum total time it takes to reach the $$$i$$$-th floor from the $$$1$$$-st (bottom) floor.", "input_spec": "The first line of the input contains two integers $$$n$$$ and $$$c$$$ ($$$2 \\le n \\le 2 \\cdot 10^5, 1 \\le c \\le 1000$$$) — the number of floors in the building and the time overhead for the elevator rides. The second line of the input contains $$$n - 1$$$ integers $$$a_1, a_2, \\dots, a_{n-1}$$$ ($$$1 \\le a_i \\le 1000$$$), where $$$a_i$$$ is the time required to go from the $$$i$$$-th floor to the $$$(i+1)$$$-th one (and from the $$$(i+1)$$$-th to the $$$i$$$-th as well) using the stairs. The third line of the input contains $$$n - 1$$$ integers $$$b_1, b_2, \\dots, b_{n-1}$$$ ($$$1 \\le b_i \\le 1000$$$), where $$$b_i$$$ is the time required to go from the $$$i$$$-th floor to the $$$(i+1)$$$-th one (and from the $$$(i+1)$$$-th to the $$$i$$$-th as well) using the elevator.", "output_spec": "Print $$$n$$$ integers $$$t_1, t_2, \\dots, t_n$$$, where $$$t_i$$$ is the minimum total time to reach the $$$i$$$-th floor from the first floor if you can perform as many moves as you want.", "sample_inputs": ["10 2\n7 6 18 6 16 18 1 17 17\n6 9 3 10 9 1 10 1 5", "10 1\n3 2 3 1 3 3 1 4 1\n1 2 3 4 4 1 2 1 3"], "sample_outputs": ["0 7 13 18 24 35 36 37 40 45", "0 2 4 7 8 11 13 14 16 17"], "notes": null}, "positive_code": [{"source_code": "def calcs(x,y,ax,ay,bx,by) ((ax-x)*(by-y)-(bx-x)*(ay-y))/2.0 end\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaizyo(n)(n < 2)? 1 : (2..n).inject(:*) end\n\ndef scount(a) b=na(a.max+1);a.each{|n|b[n]+=1};return b end\n\ndef na(n=0,d=0) Array.new(n,d)end\n\ndef na2(n=0,m=0,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n=0,m=0,l=0,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef bit(n) n.to_s(2).split(\"\").map(&:to_i) end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\n\nt3 = []\n(0..44).each{|d|t3<<3**d}\n\nn,k = inp\na = inp\nb = inp\ndp = na2(n,2)\ndp[0][0] = 0\ndp[0][1] = 9999\n(n-1).times do |i|\n dp[i+1][1] = [dp[i][0] + b[i] + k,dp[i][1] + b[i]].min\n dp[i+1][0] = [dp[i][0] + a[i], dp[i][1] + a[i]].min\nend\nans = []\ndp.each do |a|\n ans << [a[0],a[1]].min\nend\nputs ans.join(\" \")"}], "negative_code": [], "src_uid": "845e2c363038a10f438f29b1a9e04494"} {"nl": {"description": "Let's call a permutation $$$p$$$ of length $$$n$$$ anti-Fibonacci if the condition $$$p_{i-2} + p_{i-1} \\ne p_i$$$ holds for all $$$i$$$ ($$$3 \\le i \\le n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once.Your task is for a given number $$$n$$$ print $$$n$$$ distinct anti-Fibonacci permutations of length $$$n$$$.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 48$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$3 \\le n \\le 50$$$).", "output_spec": "For each test case, print $$$n$$$ lines. Each line should contain an anti-Fibonacci permutation of length $$$n$$$. In each test case, you cannot print any permutation more than once. If there are multiple answers, print any of them. It can be shown that it is always possible to find $$$n$$$ different anti-Fibonacci permutations of size $$$n$$$ under the constraints of the problem.", "sample_inputs": ["2\n\n4\n\n3"], "sample_outputs": ["4 1 3 2\n1 2 4 3\n3 4 1 2\n2 4 1 3\n3 2 1\n1 3 2\n3 1 2"], "notes": null}, "positive_code": [{"source_code": "n = gets.chomp.to_i\r\nresults = []\r\n\r\nn.times do |_|\r\n size = gets.chomp.to_i\r\n data = (1..size).to_a\r\n result = []\r\n \r\n until result.size == size do\r\n temp = data.shuffle\r\n valid = temp.each_with_index.all?{|x, i| i < 2 ? true : temp[i - 2] + temp[i - 1] != x}\r\n if valid and !result.include?(temp)\r\n result.push(temp)\r\n end\r\n end\r\n results.push(result)\r\nend\r\n\r\nresults.flatten(1).each{|x| puts x.join(' ')}"}], "negative_code": [], "src_uid": "85f0621e6cd7fa233cdee8269310f141"} {"nl": {"description": "Bob is a farmer. He has a large pasture with many sheep. Recently, he has lost some of them due to wolf attacks. He thus decided to place some shepherd dogs in such a way that all his sheep are protected.The pasture is a rectangle consisting of R × C cells. Each cell is either empty, contains a sheep, a wolf or a dog. Sheep and dogs always stay in place, but wolves can roam freely around the pasture, by repeatedly moving to the left, right, up or down to a neighboring cell. When a wolf enters a cell with a sheep, it consumes it. However, no wolf can enter a cell with a dog.Initially there are no dogs. Place dogs onto the pasture in such a way that no wolf can reach any sheep, or determine that it is impossible. Note that since you have many dogs, you do not need to minimize their number. ", "input_spec": "First line contains two integers R (1 ≤ R ≤ 500) and C (1 ≤ C ≤ 500), denoting the number of rows and the numbers of columns respectively. Each of the following R lines is a string consisting of exactly C characters, representing one row of the pasture. Here, 'S' means a sheep, 'W' a wolf and '.' an empty cell.", "output_spec": "If it is impossible to protect all sheep, output a single line with the word \"No\". Otherwise, output a line with the word \"Yes\". Then print R lines, representing the pasture after placing dogs. Again, 'S' means a sheep, 'W' a wolf, 'D' is a dog and '.' an empty space. You are not allowed to move, remove or add a sheep or a wolf. If there are multiple solutions, you may print any of them. You don't have to minimize the number of dogs.", "sample_inputs": ["6 6\n..S...\n..S.W.\n.S....\n..W...\n...W..\n......", "1 2\nSW", "5 5\n.S...\n...S.\nS....\n...S.\n.S..."], "sample_outputs": ["Yes\n..SD..\n..SDW.\n.SD...\n.DW...\nDD.W..\n......", "No", "Yes\n.S...\n...S.\nS.D..\n...S.\n.S..."], "notes": "NoteIn the first example, we can split the pasture into two halves, one containing wolves and one containing sheep. Note that the sheep at (2,1) is safe, as wolves cannot move diagonally.In the second example, there are no empty spots to put dogs that would guard the lone sheep.In the third example, there are no wolves, so the task is very easy. We put a dog in the center to observe the peacefulness of the meadow, but the solution would be correct even without him."}, "positive_code": [{"source_code": "r, c = gets.split.map &:to_i\n\npasture = Array.new(){Array.new(c)}\n\nr.times do \n pasture << gets.strip.split(\"\")\nend\n\ndef place_dogs(pasture, i, j)\n r = pasture.length\n c = pasture[0].length\n pasture[i - 1][j] = \"D\" if i - 1 >= 0 && pasture[i - 1][j] == \".\"\n pasture[i + 1][j] = \"D\" if i + 1 < r && pasture[i + 1][j] == \".\"\n pasture[i][j - 1] = \"D\" if j - 1 >= 0 && pasture[i][j - 1] == \".\"\n pasture[i][j + 1] = \"D\" if j + 1 < c && pasture[i][j + 1] == \".\"\nend\n\ndef dead_sheep?(pasture)\n r = pasture.length\n c = pasture[0].length\n dead_sheep = false\n pasture.each_index do |i|\n pasture[i].each_index do |j|\n if pasture[i][j] == \"W\"\n up = pasture[i - 1][j] if i - 1 >= 0 \n down = pasture[i + 1][j] if i + 1 < r \n right = pasture[i][j + 1] if j + 1 < c\n left = pasture[i][j - 1] if j - 1 >= 0\n if up == \"S\" || down == \"S\" || right == \"S\" || left == \"S\"\n dead_sheep = true\n break\n end\n end\n end\n end\n dead_sheep\nend\n\nif dead_sheep?(pasture)\n puts \"No\"\nelse\n pasture.each_index do |i|\n pasture[i].each_index do |j|\n if pasture[i][j] == \"W\"\n place_dogs(pasture, i, j)\n end\n end\n end\n pasture = pasture.map &:join\n puts \"Yes\", pasture.join(\"\\n\")\nend\n\n\n\n"}, {"source_code": "r,c = gets.split.map(&:to_i)\na = []\n\ncheck = true\nr.times do\n a << gets.chomp\nend\nr.times do |i|\n c.times do |j|\n if a[i][j] == \"S\"\n check = false if i > 0 && a[i-1][j] == \"W\"\n check = false if i < r-1 && a[i+1][j] == \"W\"\n check = false if j > 0 && a[i][j-1] == \"W\"\n check = false if j < c-1 && a[i][j+1] == \"W\"\n end\n a[i][j] = \"D\" if a[i][j] == \".\"\n end\nend\nif check\n puts \"Yes\"\n r.times do |i|\n puts a[i]\n end\nelse\n puts \"No\"\nend\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\nar = Array.new(0)\nres = false\nn.times { ar << gets.chomp}\nn.times do |i|\n ar[i] = ar[i].gsub('.', 'D')\n if ar[i].include?('WS') || ar[i].include?('SW')\n res = true\n break\n end\nend\nn.times do |i|\n m.times do |j|\n break if i == n-1\n curr = ar[i][j]\n bottom = ar[i+1][j] if ar[i+1]\n if [curr, bottom].sort == ['S','W']\n res = true\n break\n end\n end\nend\nputs res ? \"NO\" : \"YES\"\nputs ar if !res"}, {"source_code": "a, b= gets.chomp.split(\" \").map { |e| e.to_i }\n\narr = Array.new(a) { Array.new(b) }\n\n(0...a).to_a.each do |i|\n\tinp = gets.chomp.split(\"\")\n\t(0...b).to_a.each do |j|\n\t\tarr[i][j] = inp[j]\n\t\tif arr[i][j] == '.'\n\t\t\tarr[i][j] = 'D'\n\t\tend\n\tend\nend\n\n(0...a).to_a.each do |i|\n\t(0...b).to_a.each do |j|\n\t\tif arr[i][j] == 'W' && (((j-1) >= 0 && arr[i][j-1] == 'S') || ((j+1)=0 && arr[i-1][j] == 'S') || (i+1) < a && arr[i+1][j] == 'S')\n\t\t\tputs \"No\"\n\t\t\texit\n\t\tend\n\tend\nend\nputs 'Yes'\narr.each do |el|\n\tputs el.join(\"\")\nend\n"}], "negative_code": [], "src_uid": "f55c824d8db327e531499ced6c843102"} {"nl": {"description": "Given an integer $$$n$$$, find the maximum value of integer $$$k$$$ such that the following condition holds: $$$n$$$ & ($$$n-1$$$) & ($$$n-2$$$) & ($$$n-3$$$) & ... ($$$k$$$) = $$$0$$$ where & denotes the bitwise AND operation.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 3 \\cdot 10^4$$$). Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^9$$$).", "output_spec": "For each test case, output a single integer — the required integer $$$k$$$.", "sample_inputs": ["3\n2\n5\n17"], "sample_outputs": ["1\n3\n15"], "notes": "NoteIn the first testcase, the maximum value for which the continuous & operation gives 0 value, is 1.In the second testcase, the maximum value for which the continuous & operation gives 0 value, is 3. No value greater then 3, say for example 4, will give the & sum 0. $$$5 \\, \\& \\, 4 \\neq 0$$$, $$$5 \\, \\& \\, 4 \\, \\& \\, 3 = 0$$$. Hence, 3 is the answer."}, "positive_code": [{"source_code": "gets.to_i.times{|c|\r\n\tn = gets.to_i\r\n\tputs ('1'*(n.to_s(2).size-1)).to_i(2)\r\n}"}, {"source_code": "gets.to_i.times {\n n = gets.to_i\n\n p = 1\n p *= 2 while p < n\n p /= 2 if p > n\n\n puts(p - 1)\n}\n"}, {"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n res = 0\r\n while n > 0\r\n n /= 2\r\n res += 1\r\n end\r\n puts (1 << (res - 1)) - 1\r\nend"}, {"source_code": "t = gets.chomp.to_i\n\ndef solve(n) \n cnt = 0\n while n > 0 do\n cnt += 1\n n /= 2\n end\n\n (1 << (cnt - 1)) - 1\nend\n\nt.times do \n n = gets.chomp.to_i\n puts solve(n)\nend\n"}, {"source_code": "t = gets.to_i\r\nt.times do\r\n n = gets.to_i.to_s(2)\r\n l = 1 << (n.size - 1)\r\n puts l - 1\r\nend\r\n"}], "negative_code": [{"source_code": "gets.to_i.times{|c|\r\n\tn = gets.to_i\r\n\tputs n-n.digits(2).count(1)\r\n}"}], "src_uid": "9b4a8bc76d634935f6a1438e8a93a781"} {"nl": {"description": "The Berland capital is shaken with three bold crimes committed by the Pihsters, a notorious criminal gang.The Berland capital's map is represented by an n × m rectangular table. Each cell of the table on the map represents some districts of the capital. The capital's main detective Polycarpus took a map and marked there the districts where the first three robberies had been committed as asterisks. Deduction tells Polycarpus that the fourth robbery will be committed in such district, that all four robbed districts will form the vertices of some rectangle, parallel to the sides of the map. Polycarpus is good at deduction but he's hopeless at math. So he asked you to find the district where the fourth robbery will be committed.", "input_spec": "The first line contains two space-separated integers n and m (2 ≤ n, m ≤ 100) — the number of rows and columns in the table, correspondingly. Each of the next n lines contains m characters — the description of the capital's map. Each character can either be a \".\" (dot), or an \"*\" (asterisk). A character equals \"*\" if the corresponding district has been robbed. Otherwise, it equals \".\". It is guaranteed that the map has exactly three characters \"*\" and we can always find the fourth district that meets the problem requirements. ", "output_spec": "Print two integers — the number of the row and the number of the column of the city district that is the fourth one to be robbed. The rows are numbered starting from one from top to bottom and the columns are numbered starting from one from left to right.", "sample_inputs": ["3 2\n.*\n..\n**", "3 3\n*.*\n*..\n..."], "sample_outputs": ["1 1", "2 3"], "notes": null}, "positive_code": [{"source_code": "n, m = gets.split.map &:to_i\nmat = Array.new(n) { gets.strip }\ndx = Hash.new(0)\ndy = Hash.new(0)\n\nmat.each_with_index do |row, i|\n row.chars.to_a.each_with_index do |char, j|\n if char == '*'\n dx[i] += 1\n dy[j] += 1\n end\n end\nend\n\np dx.select { |k, v| v.odd? }.keys[0].to_i + 1\np dy.select { |k, v| v.odd? }.keys[0].to_i + 1\n"}, {"source_code": "n,m = gets.chomp.split(/ /).map!{|x| x.to_i}\nsa = []\nrety = 0\nretx = 0\nxa = []\nfor i in 0..n-1\n sa << gets.chomp\n wk = sa[i].index(\"*\")\n if wk != nil\n wk2 = sa[i].rindex(\"*\")\n if wk == wk2\n rety = i+1\n retx = wk+1\n else\n xa = [wk+1, wk2+1]\n end\n end\nend\nxa.delete(retx)\nputs rety.to_s + \" \" + xa[0].to_s \n\n"}, {"source_code": "r,c=gets.split.map(&:to_i)\nx,y=0,0\nr.times{|i|gets;c.times{|j|x^=i and y^=j if $_[j]=='*'}}\nputs \"#{x+1} #{y+1}\"\n"}, {"source_code": "r, c = gets.split.map(&:to_i)\nx, y = 0, 0\n1.upto(r){|i|\n gets\n 1.upto(c){|j|\n if $_[j - 1] == '*'\n x ^= i\n y ^= j\n end\n }\n}\nputs \"#{x} #{y}\"\n"}, {"source_code": "r, c = gets.split.map(&:to_i)\nx, y = 0, 0\n1.upto(r){|i|\n gets.chars.each_with_index{|c, j|\n if c == '*'\n x ^= i\n y ^= j + 1\n end\n }\n}\nputs \"#{x} #{y}\"\n"}, {"source_code": "r,c=gets.split.map(&:to_i)\nx,y=0,0\nr.times{|i|gets;c.times{|j|x^=i and y^=j if $_[j]=='*'}}\nputs \"#{x+1} #{y+1}\"\n\n"}, {"source_code": "r,c=gets.split.map(&:to_i)\nx,y=0,0\nr.times{|i|gets;c.times{|j|x^=i and y^=j if $_[j]=='*'}}\nputs \"#{x+1} #{y+1}\"\n\n"}, {"source_code": "#!/usr/bin/ruby1.9\n\n(n, m) = STDIN.readline.split.map {|s| s.to_i}\n\nrs = []\ncs = []\n(1..n).each do |r|\n\trow = STDIN.readline.strip\n\t(1..row.length).each do |c|\n\t\tif row[c-1].chr == '*'\n\t\t\trs.push(r)\n\t\t\tcs.push(c)\n\t\tend\n\tend\nend\n\ndef get_odd_one_out(xs)\n\txs = xs.sort\n\tif xs[0] == xs[1]\n\t\treturn xs[2]\n\tend\n\treturn xs[0]\nend\n\nr = get_odd_one_out(rs)\nc = get_odd_one_out(cs)\nputs '%d %d' % [r, c]\n\n"}, {"source_code": "r,c=gets.split.map(&:to_i)\nx,y=0,0\nr.times{|i|gets;c.times{|j|x^=i and y^=j if $_[j]=='*'}}\nputs \"#{x+1} #{y+1}\"\n"}, {"source_code": "r,c=gets.split.map(&:to_i)\nx,y=0,0\nr.times{|i|gets;c.times{|j|x^=i and y^=j if $_[j]=='*'}}\nputs \"#{x+1} #{y+1}\"\n\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\n\nlist = []\nn.times do |j|\n gets.chomp.split(\"\").each_with_index do |c, i|\n if c == '*'\n list << [j, i]\n end\n end\nend\n\ncount_y = Hash.new(0)\ncount_x = Hash.new(0)\n\nlist.each do |y, x|\n count_y[y] += 1\n count_x[x] += 1\nend\n\nputs \"#{count_y.key(1) + 1} #{count_x.key(1) + 1}\"\n"}, {"source_code": "r,c=gets.split.map(&:to_i)\nx,y=0,0\nr.times{|i|gets;c.times{|j|x^=i and y^=j if $_[j]=='*'}}\nputs \"#{x+1} #{y+1}\"\n\n"}, {"source_code": "n,m = gets.to_s.split(\" \").map{|x| Integer x}\npoints = []\nn.times do |i|\n\ta = gets.to_s.split(\"\")\n\ta.each_index do |j|\n\t\tif a[j] == \"*\" then\n\t\t\tpoints.push [i,j]\n\t\tend\n\tend\t\nend\np = []\n3.times do |x|\n\tpnt = points.pop\n\tif (pnt[0] == points[0][0] or pnt[0] == points[1][0]) and (pnt[1] == points[0][1] or pnt[1] == points[1][1]) then\n\t\tp = pnt\n\t\tbreak\n\tend\n\tpoints.push pnt\n\tpoints.rotate!\nend\n#puts \"#{p[0]} #{p[1]}\"\nnewP = [0,0]\nif p[0] == points[0][0] then\n\tp1 = points[0]\n\tp2 = points[1]\nelse\n\tp1 = points[1]\n\tp2 = points[0]\nend\n#puts \"#{p1[0]} #{p1[1]}\"\n#puts \"#{p2[0]} #{p2[1]}\"\nnewP[1] = p[1] + p1[1] - p[1]\nnewP[0] = p[0] + p2[0] - p[0]\n\nputs \"#{newP[0]+1} #{newP[1]+1}\"\n\n\n"}, {"source_code": "a = gets.to_i.times.map{ gets.strip }\nb = []\na.each_with_index do |s,i|\n s.split(//).each_with_index do |c,j| \n b << [i,j] if c == \"*\"\n end\nend\nhx = Hash.new(0)\nhy = Hash.new(0)\nb.each do |i,j|\n hx[i] += 1\n hy[j] += 1\nend\n\nputs [hx.key(1)+1,hy.key(1)+1] * \" \"\n"}, {"source_code": "r,c=gets.split.map(&:to_i)\nx,y=0,0\nr.times{|i|gets;c.times{|j|x^=i and y^=j if $_[j]=='*'}}\nputs \"#{x+1} #{y+1}\"\n\n"}, {"source_code": "require \"scanf\"\n\ninput = ARGF.read.lines\n\ncrime_scenes = []\nrow = 0\ninput.drop(1).each do |row_data|\n\tcrime_scenes += row_data\n\t\t.chars\n\t\t.each_with_index\n\t\t.select { |e,i| e == '*' }\n\t\t.map { |e,i| [row, i] }\n\n\trow += 1\nend\n\nres = \n\tif crime_scenes[0][0] == crime_scenes[1][0] # on row with second point\n\t\tif crime_scenes[0][1] == crime_scenes[2][1]\n\t\t\t[crime_scenes[2][0], crime_scenes[1][1]]\n\t\telse\n\t\t\t[crime_scenes[2][0], crime_scenes[0][1]]\n\t\tend\n\telse\n\t\tif crime_scenes[0][1] == crime_scenes[2][1] # on one column with third point\n\t\t\t[crime_scenes[0][0], crime_scenes[1][1]]\n\t\telse\n\t\t\tif crime_scenes[1][0] == crime_scenes[2][0]\n\t\t\t\t[crime_scenes[0][0], crime_scenes[2][1]]\n\t\t\telse\n\t\t\t\t[crime_scenes[1][0], crime_scenes[2][1]]\n\t\t\tend\n\t\tend\n\tend\n\nputs [res.first + 1, res.last + 1].join ' '"}, {"source_code": "h, w = gets.split.map(&:to_i)\nm = Array.new(h)\nx, y, xx, yy = 101, 102, -1, -2\nh.times{ |i| m[i] = gets\n\tw.times{ |j|\n\t\tif m[i][j, 1] == \"*\" then\n\t\t\txx = [xx, j].max\n\t\t\tx = [x, j].min\n\t\t\tyy = [yy, i].max\n\t\t\ty = [y, i].min\n\t\tend\n\t}\n}\n[x, xx].each{ |a|\n\t[y, yy].each{ |b|\n\t\tif m[b][a, 1] == \".\" then\n\t\t\tprint b + 1, \" \", a + 1, \"\\n\"\n\t\tend\n\t}\n}\n"}, {"source_code": "n, m = gets.split.map &:to_i\na = []\nn.times { a << gets.chomp.split('') }\n(0...n).each { |i| (0...m).each { |j| puts \"#{i + 1} #{j + 1}\" if a[i].count('*') == 1 && a.transpose[j].count('*') == 1 } }\n"}, {"source_code": "r,c=gets.split.map(&:to_i)\nx,y=0,0\nr.times{|i|gets;c.times{|j|x^=i and y^=j if $_[j]=='*'}}\nputs \"#{x+1} #{y+1}\"\n\n"}, {"source_code": "r,c=gets.split.map(&:to_i)\nx,y=0,0\nr.times{|i|gets;c.times{|j|x^=i and y^=j if $_[j]=='*'}}\nputs \"#{x+1} #{y+1}\"\n\n"}, {"source_code": "n,m=gets.split.map(&:to_i)\ni=x=y=0\ntable=n.times.map{i+=1;x=i if gets.count(?*)==1;$_.each_char.to_a}\ni=0\ntable.transpose.each{|l|i+=1;y=i if l.join.count(?*)==1}\nputs \"%d %d\"%[x,y]"}, {"source_code": "r,c=gets.split.map(&:to_i)\nx,y=0,0\nr.times{|i|gets;c.times{|j|x^=i and y^=j if $_[j]=='*'}}\nputs \"#{x+1} #{y+1}\"\n\n"}], "negative_code": [{"source_code": "require 'scanf'\n\ninput = ARGF.read.lines\n\ncrime_scenes = []\nrow = 0\ninput.drop(1).each do |row_data|\n\tcrime_scenes += row_data\n\t\t.chars\n\t\t.each_with_index\n\t\t.select { |e,i| e == '*' }\n\t\t.map { |e,i| [row, i] }\n\n\trow += 1\nend\n\nres = \n\tif crime_scenes[0][0] == crime_scenes[1][0] # on row\n\t\tif crime_scenes[0][1] == crime_scenes[2][1]\n\t\t\t[crime_scenes[2][0], crime_scenes[1][1]]\n\t\telse\n\t\t\t[crime_scenes[2][0], crime_scenes[0][1]]\n\t\tend\n\telse\n\t\tif crime_scenes[0][0] == crime_scenes[2][0]\n\t\t\t[crime_scenes[1][0], crime_scenes[0][1]]\n\t\telse\n\t\t\t[crime_scenes[0][0], crime_scenes[1][1]]\n\t\tend\n\tend\n\nputs [res.first + 1, res.last + 1].join ' '"}, {"source_code": "require 'scanf'\n\ninput = ARGF.read.lines\n\ncrime_scenes = []\nrow = 0\ninput.drop(1).each do |row_data|\n\tcrime_scenes += row_data\n\t\t.chars\n\t\t.each_with_index\n\t\t.select { |e,i| e == '*' }\n\t\t.map { |e,i| [row, i] }\n\n\trow += 1\nend\n\nres = \n\tif crime_scenes[0][0] == crime_scenes[1][0] # on row with second point\n\t\tif crime_scenes[0][1] == crime_scenes[2][1]\n\t\t\t[crime_scenes[2][0], crime_scenes[1][1]]\n\t\telse\n\t\t\t[crime_scenes[2][0], crime_scenes[0][1]]\n\t\tend\n\telse\n\t\tif crime_scenes[0][1] == crime_scenes[2][1] # on one column with third point\n\t\t\t[crime_scenes[0][0], crime_scenes[1][1]]\n\t\telse\n\t\t\t[crime_scenes[1][0], crime_scenes[0][1]]\n\t\tend\n\tend\n\nputs [res.first + 1, res.last + 1].join ' '"}, {"source_code": "require \"scanf\"\n\ninput = ARGF.read.lines\n\ncrime_scenes = []\nrow = 0\ninput.drop(1).each do |row_data|\n\tcrime_scenes += row_data\n\t\t.chars\n\t\t.each_with_index\n\t\t.select { |e,i| e == '*' }\n\t\t.map { |e,i| [row, i] }\n\n\trow += 1\nend\n\nres = \n\tif crime_scenes[0][0] == crime_scenes[1][0] # on row with second point\n\t\tif crime_scenes[0][1] == crime_scenes[2][1]\n\t\t\t[crime_scenes[2][0], crime_scenes[1][1]]\n\t\telse\n\t\t\t[crime_scenes[2][0], crime_scenes[0][1]]\n\t\tend\n\telse\n\t\tif crime_scenes[0][1] == crime_scenes[2][1] # on one column with third point\n\t\t\t[crime_scenes[0][0], crime_scenes[1][1]]\n\t\telse\n\t\t\t[crime_scenes[1][0], crime_scenes[0][1]]\n\t\tend\n\tend\n\nputs [res.first + 1, res.last + 1].join ' '"}], "src_uid": "e344de8280fb607c556671db9cfbaa9e"} {"nl": {"description": "Anna and Maria are in charge of the math club for junior students. When the club gathers together, the students behave badly. They've brought lots of shoe laces to the club and got tied with each other. Specifically, each string ties together two students. Besides, if two students are tied, then the lace connects the first student with the second one as well as the second student with the first one.To restore order, Anna and Maria do the following. First, for each student Anna finds out what other students he is tied to. If a student is tied to exactly one other student, Anna reprimands him. Then Maria gathers in a single group all the students who have been just reprimanded. She kicks them out from the club. This group of students immediately leaves the club. These students takes with them the laces that used to tie them. Then again for every student Anna finds out how many other students he is tied to and so on. And they do so until Anna can reprimand at least one student.Determine how many groups of students will be kicked out of the club.", "input_spec": "The first line contains two integers n and m — the initial number of students and laces (). The students are numbered from 1 to n, and the laces are numbered from 1 to m. Next m lines each contain two integers a and b — the numbers of students tied by the i-th lace (1 ≤ a, b ≤ n, a ≠ b). It is guaranteed that no two students are tied with more than one lace. No lace ties a student to himself.", "output_spec": "Print the single number — the number of groups of students that will be kicked out from the club.", "sample_inputs": ["3 3\n1 2\n2 3\n3 1", "6 3\n1 2\n2 3\n3 4", "6 5\n1 4\n2 4\n3 4\n5 4\n6 4"], "sample_outputs": ["0", "2", "1"], "notes": "NoteIn the first sample Anna and Maria won't kick out any group of students — in the initial position every student is tied to two other students and Anna won't be able to reprimand anyone.In the second sample four students are tied in a chain and two more are running by themselves. First Anna and Maria kick out the two students from both ends of the chain (1 and 4), then — two other students from the chain (2 and 3). At that the students who are running by themselves will stay in the club.In the third sample Anna and Maria will momentarily kick out all students except for the fourth one and the process stops at that point. The correct answer is one."}, "positive_code": [{"source_code": "n,m=gets.chomp.split.map(&:to_i)\ni,j,f,a,b,ar=0,0,0,[],[],Array.new(100,0)\nm.times do |i|\n\tx,y=gets.chomp.split.map(&:to_i)\n\ta<<(x-1);b<<(y-1)\nend\nwhile i<105\n\tf,c=0,Array.new(100,0)\n\tm.times do |j|\n\t\tif ar[a[j]]==0 && ar[b[j]]==0\n\t\t\tc[a[j]]+=1\n\t\t\tc[b[j]]+=1\n\t\tend\n\tend\n\tn.times do |j|\n\t\tif c[j]==1\n\t\t\tf=1\n\t\t\tar[j]=1\n\t\tend\n\tend\n\tbreak if f==0\n\ti+=1\nend\nputs i"}, {"source_code": "class Kid\n attr_accessor :tied_to, :number\n def initialize (num)\n @tied_to = []\n @number = num\n end\nend\n\nresult = -1\ninput = STDIN.gets.strip\nlaces = []\ninput.split.last.to_i.times do |i|\n laces.push STDIN.gets.strip.split.collect {|i| i.to_i}\nend\n\nwhile true\n kids = Array.new(input.split.first.to_i) {|i| Kid.new(i+1)}\n result += 1\n laces.each do |i|\n kids[i[0]-1].tied_to.push i[1] unless kids[i[0]-1].tied_to.include? i[1]\n kids[i[1]-1].tied_to.push i[0] unless kids[i[1]-1].tied_to.include? i[0]\n end\n remove = []\n kids.each do |i|\n remove.push(i.number) if i.tied_to.size == 1\n end\n break if remove.size == 0\n remove.each do |i|\n laces.delete_if {|l| l.include? i}\n end\nend\np result"}, {"source_code": "include Math\n\nn, m=gets.split.map(&:to_i)\ngraph={}\n\nm.times{\n\ta,b=gets.split.map(&:to_i)\n\t\n\tif graph.has_key?(a)\n\t\tgraph[a] << b\t\t\n\telse\n\t\tgraph[a] = [b]\n\tend\n\t\n\tif graph.has_key?(b)\n\t\tgraph[b] << a\n\telse\n\t\tgraph[b] = [a]\n\tend\n}\n\nans=0\nloop{\t\n\tdelete_them = []\n\t\n\tgraph.keys.each{|key|\n\t\tif graph[key].size==1\t\t\t\t\t\t\n\t\t\tdelete_them << key\n\t\tend\n\t}\t\n\t\t\n\t#deleting vals in arrays\n\tgraph.keys.each{|key|\n\t\tdelete_them.each{|el|\tgraph[key].delete(el)}\n\t}\n\t\n\t# deleting keys\n\tdelete_them.each{|el| graph.delete(el)}\n\t\t\n\tans+=1 if delete_them.size!=0\n\tbreak if delete_them.size==0\n}\n\nputs ans\n"}, {"source_code": "n,m=gets.split.map(&:to_i);g=Hash.new []\nm.times{a,b=gets.split.map(&:to_i);g[a]+=[b];g[b]+=[a]}\ns=0;loop{d=[];g.keys.each{|k| d << k if g[k].size==1};g.keys.each{|k|d.each{|l|g[k].delete(l)}};d.each{|l|g.delete(l)};s+=1 if d.size!=0;break if d.size==0};p s\n"}, {"source_code": "n,m=gets.split.map(&:to_i);g=Hash.new [];m.times{a,b=gets.split.map(&:to_i);g[a]+=[b];g[b]+=[a]};s=0;loop{d=[];g.keys.each{|k|d<0?1:0)\n }\n if t<=1 then break end\n t=0\n n.times{|i|\n t+=(cnt[i]==1?1:0)\n }\n if t==0 then break end\n r=cnt.clone\n n.times{|i|\n if r[i]==1 then\n n.times{|j|\n if g[i][j]>0 then\n g[i][j]-=1\n g[j][i]-=1\n cnt[i]-=1\n cnt[j]-=1\n end\n }\n end\n }\n ans+=1\n}\np ans\n"}], "negative_code": [{"source_code": "class Kid\n attr_accessor :tied_to, :number\n def initialize (num)\n @tied_to = []\n @number = num\n end\nend\n\nresult = -1\ninput = STDIN.gets.strip\nlaces = []\ninput.split.last.to_i.times do |i|\n laces.push STDIN.gets.strip.split.collect {|i| i.to_i}\nend\n\nwhile true\n kids = Array.new(input.split.first.to_i) {|i| Kid.new(i+1)}\n result += 1\n laces.each do |i|\n kids[i[0]-1].tied_to.push i[1] unless kids[i[0]-1].tied_to.include? i[1]\n kids[i[1]-1].tied_to.push i[0] unless kids[i[1]-1].tied_to.include? i[0]\n end\n remove = []\n kids.each do |i|\n remove.push(i.number) if i.tied_to.size == 1\n end\n break if remove.size == 0\n remove.each do |i|\n laces.delete_if {|l| l.include? i}\n end\n break if result == 2\nend\np result"}, {"source_code": "n, m = gets.split.map(&:to_i)\ne = []\n0.upto(n+1){ |i| e[i] = [] }\nm.times{\n a, b = gets.split.map(&:to_i)\n e[a] << b\n e[b] << a\n}\nans = 0\nwhile true\n f = true\n ne = e.clone\n e.each_with_index{ |x,i|\n next if x.length != 1\n x.each{ |y| ne.each{ |z| z.delete(y) } }\n e[i] = []\n f = false\n }\n e = ne\n break if f\n ans += 1\nend\np ans"}], "src_uid": "f8315dc903b0542c453cab4577bcb20d"} {"nl": {"description": "Given a string $$$s$$$ of length $$$n$$$ and integer $$$k$$$ ($$$1 \\le k \\le n$$$). The string $$$s$$$ has a level $$$x$$$, if $$$x$$$ is largest non-negative integer, such that it's possible to find in $$$s$$$: $$$x$$$ non-intersecting (non-overlapping) substrings of length $$$k$$$, all characters of these $$$x$$$ substrings are the same (i.e. each substring contains only one distinct character and this character is the same for all the substrings). A substring is a sequence of consecutive (adjacent) characters, it is defined by two integers $$$i$$$ and $$$j$$$ ($$$1 \\le i \\le j \\le n$$$), denoted as $$$s[i \\dots j]$$$ = \"$$$s_{i}s_{i+1} \\dots s_{j}$$$\".For example, if $$$k = 2$$$, then: the string \"aabb\" has level $$$1$$$ (you can select substring \"aa\"), the strings \"zzzz\" and \"zzbzz\" has level $$$2$$$ (you can select two non-intersecting substrings \"zz\" in each of them), the strings \"abed\" and \"aca\" have level $$$0$$$ (you can't find at least one substring of the length $$$k=2$$$ containing the only distinct character). Zuhair gave you the integer $$$k$$$ and the string $$$s$$$ of length $$$n$$$. You need to find $$$x$$$, the level of the string $$$s$$$.", "input_spec": "The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le k \\le n \\le 2 \\cdot 10^5$$$) — the length of the string and the value of $$$k$$$. The second line contains the string $$$s$$$ of length $$$n$$$ consisting only of lowercase Latin letters.", "output_spec": "Print a single integer $$$x$$$ — the level of the string.", "sample_inputs": ["8 2\naaacaabb", "2 1\nab", "4 2\nabab"], "sample_outputs": ["2", "1", "0"], "notes": "NoteIn the first example, we can select $$$2$$$ non-intersecting substrings consisting of letter 'a': \"(aa)ac(aa)bb\", so the level is $$$2$$$.In the second example, we can select either substring \"a\" or \"b\" to get the answer $$$1$$$."}, "positive_code": [{"source_code": "n, k = gets.split.map(&:to_i)\ns = gets.chop\narr = [0] * 1000\ncur = [0, '0']\ns.split('').each do |c|\n if (c == cur[1])\n cur[0] += 1\n else\n cur[0] = 1\n cur[1] = c\n end\n if (cur[0] == k)\n arr[c.ord] += 1\n cur[0] = 0\n end\nend\nputs arr.max"}, {"source_code": "DBG = true\nn,k = gets.split.map{|z| z.to_i}\ns = gets.chomp\nalpha = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',\n 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',\n 'w', 'x', 'y', 'z' ]\nlv = 0\nfor c in alpha\n cnt = 0\n len = 0\n for i in 0...n\n if s[i] == c\n len += 1\n if len == k\n len = 0\n cnt += 1\n end\n else\n len = 0\n end\n end\n lv = cnt if cnt > lv\nend\nputs \"#{lv}\"\n"}, {"source_code": "require 'set'\n\nif File.exists?(\"input\")\n $stdin = File.open(\"input\")\n $stdout = File.open(\"output\",\"w\")\nend\n\nnk = gets.chomp.split(\" \").map(&:to_i)\nn = nk[0]\nk = nk[1]\n\nhash = Hash.new(0)\nlast = Hash.new(-1)\nstr = gets.chomp\n\nbreakpoints = Set.new\n(0..n-1).each do |i|\n\tif str[i]!=str[i+1]\n\t\tbreakpoints.add(i)\n\tend\nend\n\ntoskip = Set.new\nbreakpoints.each do |br|\n\t(br-k+2..br).each do |x|\n\t\ttoskip.add(x)\n\tend\nend\n\n\n(0..n-k).each do |st|\n\tnext if toskip.include?(st)\n\tchar = str[st]\n#\tthestr = str[st..st+k-1]\n#\tnext if thestr.each_char.to_a.uniq.length>1\n\titslast = last[char]\n\tnext if itslast>=st\n\n\thash[char]+=1\n\tlast[char] = st+k-1\nend\n#p hash\n#p toskip\nif hash.empty?\n\tputs 0\nelse\n\tputs hash.values.max\nend\n\n#for 1 to k starting index, take steps of k"}], "negative_code": [], "src_uid": "6c71c828553e43c699237211005873e5"} {"nl": {"description": "One day three best friends Petya, Vasya and Tonya decided to form a team and take part in programming contests. Participants are usually offered several problems during programming contests. Long before the start the friends decided that they will implement a problem if at least two of them are sure about the solution. Otherwise, the friends won't write the problem's solution.This contest offers n problems to the participants. For each problem we know, which friend is sure about the solution. Help the friends find the number of problems for which they will write a solution.", "input_spec": "The first input line contains a single integer n (1 ≤ n ≤ 1000) — the number of problems in the contest. Then n lines contain three integers each, each integer is either 0 or 1. If the first number in the line equals 1, then Petya is sure about the problem's solution, otherwise he isn't sure. The second number shows Vasya's view on the solution, the third number shows Tonya's view. The numbers on the lines are separated by spaces.", "output_spec": "Print a single integer — the number of problems the friends will implement on the contest.", "sample_inputs": ["3\n1 1 0\n1 1 1\n1 0 0", "2\n1 0 0\n0 1 1"], "sample_outputs": ["2", "1"], "notes": "NoteIn the first sample Petya and Vasya are sure that they know how to solve the first problem and all three of them know how to solve the second problem. That means that they will write solutions for these problems. Only Petya is sure about the solution for the third problem, but that isn't enough, so the friends won't take it. In the second sample the friends will only implement the second problem, as Vasya and Tonya are sure about the solution."}, "positive_code": [{"source_code": "p gets.to_i.times.map{gets}.count{|x| x.count('1') >= 2}"}, {"source_code": "p$<.grep(/1.*1/).size\n"}, {"source_code": "n = gets.to_i\ncount = 0\nfor i in 1..n\n buf = gets.split(' ').map(&:to_i)\n a, b, c = buf[0], buf[1], buf[2]\n count += 1 if a + b + c >= 2\nend\nputs count\n"}, {"source_code": "b = gets.chomp\na = 0\narray = []\nwhile a < b.to_i\n n = STDIN.gets.split(\" \")\n if (n[0].to_i + n[1].to_i + n[2].to_i) > 1\n array << 1\n end\n a += 1\nend\n\nputs array.size"}, {"source_code": "p$<.grep(/1.*1/).size"}, {"source_code": "res = 0\ngets.to_i.times do\n input = gets.split.map(&:to_i)\n res += 1 if input.select { |item| item == 1 }.size >= 2\nend\nputs res"}, {"source_code": "p$<.grep(/1.*1/).size\n"}, {"source_code": "count = 0\n\ngets.strip.to_i.times do\n p, v, t = gets.strip.split(' ').map(&:to_i)\n count +=1 if p + v + t >= 2\nend\n\nputs count\n"}, {"source_code": "p$<.grep(/1.*1/).size\n"}, {"source_code": "#!/usr/bin/env ruby\n\ncount = gets.to_i\nsum = 0\n\n\n(0..count-1).each do |i|\n\trow = gets.split(\" \")\n\trow_sum = 0\n\trow.each do |r|\n\t\trow_sum += r.to_i\n\tend\n\tif row_sum > 1 then\n\t\tsum+=1\n\tend\nend\n\nputs sum\n"}, {"source_code": "p$<.grep(/1.*1/).size"}, {"source_code": "n = gets.to_i\n\npeta = []\ncounter = 0\nfor i in 0..n-1\n peta[i] = gets.split.map(&:to_i)\n counter+=1 if peta[i].count(1)>=2\nend\n\nputs counter\n"}, {"source_code": "num = gets.chomp.to_i\narray = []\ni = 0\nwhile (i < num)\n\tarray.push gets.chomp\n\ti = i+1\nend\naux = 0\narray.each do |leng|\n\tline = leng.split\n\tif (line[0].to_i + line[1].to_i + line[2].to_i >= 2) \n\t\taux = aux + 1\n\tend\nend\nputs aux.to_s"}, {"source_code": "p [*$<].grep(/1.*1/).size"}, {"source_code": "#!/usr/bin/env ruby\n\ncount = gets.to_i\nsum = 0\n\n\n(0..count-1).each do |i|\n\trow = gets.split(\" \")\n\trow_sum = 0\n\trow.each do |r|\n\t\trow_sum += r.to_i\n\tend\n\tif row_sum > 1 then\n\t\tsum+=1\n\tend\nend\n\nputs sum\n"}, {"source_code": "n = gets.to_i\nm = 0;\nn.times do\n x, y, z = gets.split.map(&:to_i)\n m += 1 if x + y + z >= 2\nend\nputs m\n"}, {"source_code": "res = 0\ngets.to_i.times do\n input = gets.split.map(&:to_i)\n res += 1 if input.select { |item| item == 1 }.size >= 2\nend\nputs res"}, {"source_code": "problems_count = gets.chomp.to_i\n\ncounter = 0\nproblems_count.times.each do\n submission_string = gets.chomp\n submission = submission_string.split(' ').map(&:to_i)\n el_counter = 0\n submission.each do |el|\n el_counter += 1 if el.eql?(1)\n end\n counter += 1 if el_counter >= 2\nend\n\nputs counter\n"}, {"source_code": "count = gets.to_i\n\nres = 0\ncount.times do |i|\n res += 1 if gets.split(\" \").map(&:to_i).inject(:+) > 1\nend\n\np res"}, {"source_code": "p$<.grep(/1.*1/).size\n"}, {"source_code": "T = gets.chomp.to_i\nans = 0\nfor i in (1..T)\n a = gets.chomp.split(' ')\n ans += (a.count(\"1\") > 1) ? 1 : 0\nend\nputs ans"}, {"source_code": "p$<.grep(/1.*1/).size\n"}, {"source_code": "count = gets.to_i\nsum = 0\n\n\n(0..count-1).each do |i|\n row = gets.split(\" \")\n row_sum = 0\n row.each do |r|\n row_sum += r.to_i\n end\n if row_sum > 1 then\n sum+=1\n end\nend\n\nputs sum"}, {"source_code": "number = gets.strip.to_i\n\ncount = 0\nnumber.times do\n input = gets.strip\n count += 1 if input.split(' ').count('1') >= 2\nend\n\nputs count\n"}, {"source_code": "n = gets.chomp.to_i\nputs n.times.select {|i|gets.chomp.split(' ').map(&:to_i).select{|i| i > 0}.count > 1 }.count"}, {"source_code": "n = gets.to_i\nans = 0\nn.times do\n if gets.split.map(&:to_i).inject(:+) >= 2\n ans += 1\n end\nend\nputs ans\n"}, {"source_code": "count = gets.to_i\nsum = 0\n\n\n(0..count-1).each do |i|\n row = gets.split(\" \")\n row_sum = 0\n row.each do |r|\n row_sum += r.to_i\n end\n if row_sum > 1 then\n sum+=1\n end\nend\n\nputs sum"}, {"source_code": "\n$res = 0\n$cnt = 0\n\ngets.to_i.times{|x| x = gets.split.map(&:to_i) \n x.each do |num|\n if num == 1\n $cnt += 1\n end\n end\n if $cnt >= 2\n $res += 1\n end\n $cnt = 0\n}\n\nputs $res\n"}, {"source_code": "#!/usr/bin/env ruby\n\ncount = gets.to_i\nsum = 0\n\n\n(0..count-1).each do |i|\n\trow = gets.split(\" \")\n\trow_sum = 0\n\trow.each do |r|\n\t\trow_sum += r.to_i\n\tend\n\tif row_sum > 1 then\n\t\tsum+=1\n\tend\nend\n\nputs sum\n"}, {"source_code": "require 'scanf'\nn = $stdin.readline.to_i\n\ncounter = 0\nn.times do\n a, b, c = $stdin.readline.scanf(\"%d%d%d\")\n counter += 1 if a + b + c >=2\nend\n\nputs counter\n"}, {"source_code": "#!/usr/bin/env ruby\n\ncount = gets.to_i\nsum = 0\n\n\n(0..count-1).each do |i|\n\trow = gets.split(\" \")\n\trow_sum = 0\n\trow.each do |r|\n\t\trow_sum += r.to_i\n\tend\n\tif row_sum > 1 then\n\t\tsum+=1\n\tend\nend\n\nputs sum\n"}, {"source_code": "count = 0\n\ngets.strip.to_i.times do\n p, v, t = gets.strip.split(' ').map(&:to_i)\n count +=1 if p + v + t >= 2\nend\n\nputs count\n"}, {"source_code": "def get_num\n input = gets.chomp.split\n x = 0\n for i in 0..input.length - 1\n x = x + input[i].to_i\n end\n if x >= 2\n return 1\n else\n return 0\n end\nend\n\n$n = gets.chomp.to_i\nans = 0\nfor i in 1..$n\n ans += get_num\nend\n\nputs ans\n"}, {"source_code": "n = gets.chomp.to_i\nans = 0\nn.times{\n num = gets.chomp.split.map{|x| x.to_i }\n ans += 1 if num.inject{ |sum,x| sum + x } >= 2\n}\nputs ans"}, {"source_code": "n = gets.to_i\nf = []\ns = []\nl = []\narrs = []\nimplements = 0\n\nn.times do \n q = gets.strip.split(\" \").map(&:to_i)\n f << q[0]\n s << q[1]\n l << q[2]\n arrs << q\nend \n\narrs.each do |arr|\n implements += 1 if arr.count{|one| one == 1} > 1 \nend \nputs implements\n"}, {"source_code": "n, count = gets.chomp.to_i, 0\nn.times do\n count += 1 if (gets.chomp.split(' ').map(&:to_i).reduce(:+) > 1)\nend\nputs count\n"}, {"source_code": "def chk(ans)\n\tn=0\n\tfor i in ans\n\t\tif i==1\n\t\t\tn+=1\n\t\tend\n\tend\n\treturn n\nend\nn = gets.to_i()\nproblem =0\nwhile n>0\n\tn-=1\n\tans = gets.split().map{|x| x.to_i}\n\tif chk(ans)>1\n\t\tproblem+=1\n\tend\nend\nputs problem"}, {"source_code": "n = gets.chomp.to_i\nc = 0\nwhile n > 0 \n s1, s2, s3= gets.chomp.split.map(&:to_i) \n if s1+s2+s3 >= 2\n c = c + 1\n end \n n -=1\nend\nputs c\n"}, {"source_code": "def answers ()\n n = gets.chomp\n n = n.to_i\n final = 0\n \n for i in 0...n\n count = 0 \n test = Array.new\n test = gets.split(' ').map(&:to_i)\n test.each { |x| \n if x == 1 \n count+=1\n end\n }\n if count >= 2 then\n final +=1\n end\n end\n puts final\nend\n\nanswers"}, {"source_code": "x=0\ngets.chomp.to_i.times {\n\ta,b,c=gets.split.map &:to_i\n\tx+=1 unless a+b+c < 2\n}\nputs x"}, {"source_code": "N = gets.to_i\nas = N.times.map{gets.split.count{|bb| bb.to_i.zero? }}\n\nputs as.count{|a| a < 2 }\n"}, {"source_code": "x=0\ngets.chomp.to_i.times {\n\ta,b,c=gets.split.map &:to_i\n\tx+=1 unless a+b+c < 2\n}\nputs x"}, {"source_code": "n = gets.strip.to_i\ncount = 0\nn.times do |i|\n arr = gets.strip.split(\" \").map(&:to_i)\n count += 1 if arr.count{|elem| elem == 1} > 1 \nend\nputs count"}, {"source_code": "N = gets.to_i\nas = N.times.map{gets.split.count{|bb| bb == '0' }}\n\nputs as.count{|a| a < 2 }\n"}, {"source_code": "res=0\ngets.to_i.times{(gets.count(?1))>=2?res+=1:\"\"}\nputs res"}, {"source_code": "p$<.grep(/1.*1/).size"}, {"source_code": "N = gets.to_i\nas = N.times.map{gets.chomp.gsub('0','')}\nputs as.count{|a| a.size > 3 }"}, {"source_code": "n = gets.chomp.to_i\nc = 0\nwhile n > 0 \n s1, s2, s3= gets.chomp.split.map(&:to_i) \n if s1+s2+s3 >= 2\n c = c + 1\n end \n n -=1\nend\nputs c\n"}, {"source_code": "x=0\ngets.chomp.to_i.times {\n\ta,b,c=gets.split.map &:to_i\n\tx+=1 unless a+b+c < 2\n}\nputs x"}, {"source_code": "p [*$<].grep(/1.*1/).size"}, {"source_code": "n = gets.to_i\nputs n.times.map{gets.split.map(&:to_i).inject(:+)}.select{|x| x >= 2}.size\n"}, {"source_code": "\nn = gets.chomp.to_i\n\ncount = 0\n\nwhile n > 0\n a = gets.chomp \n if a[0].to_i + a[2].to_i + a[-1].to_i >= 2\n count += 1\n end\n n -= 1\nend\n\nputs count\n\n"}, {"source_code": "\namountOfProblems = gets.chomp#get input on first line\namountOfProblems = amountOfProblems.to_i\nwillWorkSolution = 0\nsecondCounter = 0\n\n\namountOfProblems.times do\n willWorkSolution = gets.chomp # 1 0 1 - space separated other input\n willWorkSolution = willWorkSolution.split(\" \") # array is: [\"1\", \"0\", \"1\"]\n counter = 0\n \n willWorkSolution.each do |one_or_zero|\n if (one_or_zero == \"1\")\n counter = counter + 1\n if (counter == 2)\n secondCounter = secondCounter + 1\n end\n end\n \n end\nend\n\n\nputs secondCounter\n"}, {"source_code": "puts STDIN.readlines[1..-1].map(&:strip).map(&:split).inject(0) { |memo, w| memo + (w.find_all { |x| x==\"1\"}.size >= 2 ? 1 : 0) }"}, {"source_code": "#my code goes here\na = []\nk = 0\nn = gets.to_i\nfor c in 1..n\ns = 0\na = gets.chomp.split().map { |e| e.to_i }\na.each do |x|\n s += x\nend\nif s >= 2\nk = k + 1\nelse\nk = k + 0\nend\nend\nputs \"#{k}\"\n\n"}, {"source_code": "problems_count = gets.chomp.to_i\n\ncounter = 0\nproblems_count.times.each do\n submission_string = gets.chomp\n submission = submission_string.split(' ').map(&:to_i)\n el_counter = 0\n submission.each do |el|\n el_counter += 1 if el.eql?(1)\n end\n counter += 1 if el_counter >= 2\nend\n\nputs counter\n"}, {"source_code": "\namountOfProblems = gets.chomp#get input on first line\namountOfProblems = amountOfProblems.to_i\nwillWorkSolution = 0\nsecondCounter = 0\n\n\namountOfProblems.times do\n willWorkSolution = gets.chomp # 1 0 1 - space separated other input\n willWorkSolution = willWorkSolution.split(\" \") # array is: [\"1\", \"0\", \"1\"]\n counter = 0\n \n willWorkSolution.each do |one_or_zero|\n if (one_or_zero == \"1\")\n counter = counter + 1\n if (counter == 2)\n secondCounter = secondCounter + 1\n end\n end\n \n end\nend\n\n\nputs secondCounter\n"}, {"source_code": "n = gets.to_i\nm = 0;\nn.times do\n x, y, z = gets.split.map(&:to_i)\n m += 1 if x + y + z >= 2\nend\nputs m\n"}, {"source_code": "#!/usr/bin/env ruby\ninput = gets.to_i\n\nanswer = 0\ninput.times do\n inp = gets.split(' ').map(&:to_i).inject(0, :+)\n answer += 1 if inp >= 2\nend\n\nputs answer\n"}, {"source_code": "n = gets.to_i\nputs n.times.map{gets.split.map(&:to_i).inject(:+)}.select{|x| x >= 2}.size\n"}, {"source_code": "n = gets.to_i\na =[]\nj = 0\n\nfor i in 1..n do\n\ta = gets.split.map(&:to_i)\n\tif a.count(1) > 1\n\t\tj = j+1\n\tend\nend\n\nputs j"}, {"source_code": "i = gets.to_i\ncounter = 0\n(1..i).each do |x|\n counter += gets.split(' ').map(&:to_i).reduce(:+) >= 2 ? 1 : 0\nend\nprint counter"}, {"source_code": "N = gets.to_i\nas = N.times.map{gets.split.count{|bb| bb == '0' }}\n\nputs as.count{|a| a < 2 }\n"}, {"source_code": "res = 0\ngets.to_i.times do\n input = gets.split.map(&:to_i)\n res += 1 if input.select { |item| item == 1 }.size >= 2\nend\nputs res"}, {"source_code": "n = gets.to_i\ncount = 0\nfor i in 1..n\n buf = gets.split(' ').map(&:to_i)\n a, b, c = buf[0], buf[1], buf[2]\n count += 1 if a + b + c >= 2\nend\nputs count\n"}, {"source_code": "n = gets.to_i\nf = []\ns = []\nl = []\narrs = []\nimplements = 0\n\nn.times do \n q = gets.strip.split(\" \").map(&:to_i)\n f << q[0]\n s << q[1]\n l << q[2]\n arrs << q\nend \n\narrs.each do |arr|\n implements += 1 if arr.count{|one| one == 1} > 1 \nend \nputs implements\n"}, {"source_code": "cnt = 0\nn = gets.chomp.to_i\nn.times{cnt += 1 if gets.chomp.split.map(&:to_i).reduce(:+) > 1}\nputs cnt"}, {"source_code": "ans=0\na=[]\ngets.to_i.times{|i|\n\ta<= 2) \n\t\taux = aux + 1\n\tend\nend\nputs aux.to_s"}, {"source_code": "p$<.grep(/1.*1/).size"}, {"source_code": "num = gets.chomp.to_i\narray = []\ni = 0\nwhile (i < num)\n\tarray.push gets.chomp\n\ti = i+1\nend\naux = 0\narray.each do |leng|\n\tline = leng.split\n\tif (line[0].to_i + line[1].to_i + line[2].to_i >= 2) \n\t\taux = aux + 1\n\tend\nend\nputs aux.to_s"}, {"source_code": "x=0\ngets.chomp.to_i.times {\n\ta,b,c=gets.split.map &:to_i\n\tx+=1 unless a+b+c < 2\n}\nputs x"}, {"source_code": "#!/usr/bin/env ruby\n\ncount = gets.to_i\nsum = 0\n\n\n(0..count-1).each do |i|\n\trow = gets.split(\" \")\n\trow_sum = 0\n\trow.each do |r|\n\t\trow_sum += r.to_i\n\tend\n\tif row_sum > 1 then\n\t\tsum+=1\n\tend\nend\n\nputs sum\n"}, {"source_code": "p$<.grep(/1.*1/).size"}, {"source_code": "p$<.grep(/1.*1/).size"}, {"source_code": "#my code goes here\na = []\nk = 0\nn = gets.to_i\nfor c in 1..n\ns = 0\na = gets.chomp.split().map { |e| e.to_i }\na.each do |x|\n s += x\nend\nif s >= 2\nk = k + 1\nelse\nk = k + 0\nend\nend\nputs \"#{k}\"\n\n"}, {"source_code": "n = gets.to_i\nans = 0\nn.times do\n\tf, s, t = gets.split.map{|x| x.to_i}\n\tans += 1 if f + s + t >= 2\nend\np ans"}, {"source_code": "n = gets.to_i\nf = []\ns = []\nl = []\narrs = []\nimplements = 0\n\nn.times do \n q = gets.strip.split(\" \").map(&:to_i)\n f << q[0]\n s << q[1]\n l << q[2]\n arrs << q\nend \n\narrs.each do |arr|\n implements += 1 if arr.count{|one| one == 1} > 1 \nend \nputs implements\n"}, {"source_code": "n = gets.to_i\n\nproblems = []\n1.upto(n) do |idx|\n problem = gets.gsub(\"\\n\", \"\")\n problem = problem.split(\" \").map(&:to_i)\n problems.push(problem)\nend\n\nproblems_implemented = 0\nproblems.each do |problem|\n sureness = 0\n problem.each do |friend|\n sureness += friend\n end\n if sureness >= 2\n problems_implemented += 1 \n end\nend\n\nputs problems_implemented"}, {"source_code": "n = gets.chomp.to_i\ncnt = 0\nn.times do \n\tcnt += 1 if gets.chomp.split.map(&:to_i).count(1) >= 2\nend\nputs cnt"}, {"source_code": "T = gets.chomp.to_i\nans = 0\nfor i in (1..T)\n a = gets.chomp.split(' ')\n ans += (a.count(\"1\") > 1) ? 1 : 0\nend\nputs ans"}, {"source_code": "a=0\nSTDIN.read.split(\"\\n\").drop(1).each do |w|\n a += 1 if w.chomp.split(' ').map{|n| n.to_i}.inject(:+) >= 2\nend\nputs a"}, {"source_code": "n = gets.to_i \nproblems_solved = 0 \nn.times {\n arr = gets.split(\" \").map(&:to_i)\n if arr.select{|a| a==1 }.count >=2\n problems_solved+=1 \n end \n}\nputs problems_solved"}, {"source_code": "#!/usr/bin/env ruby\n\ncount = gets.to_i\nsum = 0\n\n\n(0..count-1).each do |i|\n\trow = gets.split(\" \")\n\trow_sum = 0\n\trow.each do |r|\n\t\trow_sum += r.to_i\n\tend\n\tif row_sum > 1 then\n\t\tsum+=1\n\tend\nend\n\nputs sum\n"}, {"source_code": "p = 0\ngets.chomp.to_i.times do |variable|\n\tc = gets.chomp.count(\"1\")\n\t(p = p + 1) if c > 1\nend\nputs p"}, {"source_code": "a=gets.chomp.to_i\nans=0\na.times do\nb=gets.chomp\nif b.count(\"1\")>=2\nans+=1\nend\nend\nputs \"#{ans}\"\n"}, {"source_code": "n = gets.to_i \nproblems_solved = 0 \nn.times {\n arr = gets.split(\" \").map(&:to_i)\n if arr.select{|a| a==1 }.count >=2\n problems_solved+=1 \n end \n}\nputs problems_solved"}, {"source_code": "p$<.grep(/1.*1/).size\n"}, {"source_code": "puts gets.to_i.times.map{gets}.count{|x| x.count(\"1\") >= 2}\n"}, {"source_code": "n, *s = STDIN.read.split(\"\\n\") #gets.sFile.readlines('inp.txt')\nans = 0\ns.each do |i|\n\tans += 1 if i.count('1') > 1\nend\np ans"}, {"source_code": "number_of_problems = gets.chomp.to_i\nnumber_of_solved = 0 \n(0...number_of_problems).each do |iteration| \n arr = gets.split(\" \").map(&:to_i) \n number_of_solved+=1 if arr.count(1) >= 2 \nend\nputs number_of_solved\n"}, {"source_code": "#my code goes here\na = []\nk = 0\nn = gets.to_i\nfor c in 1..n\ns = 0\na = gets.chomp.split().map { |e| e.to_i }\na.each do |x|\n s += x\nend\nif s >= 2\nk = k + 1\nelse\nk = k + 0\nend\nend\nputs \"#{k}\"\n\n"}, {"source_code": "n = gets.to_i \nproblems_solved = 0 \nn.times {\n arr = gets.split(\" \").map(&:to_i)\n if arr.select{|a| a==1 }.count >=2\n problems_solved+=1 \n end \n}\nputs problems_solved"}, {"source_code": "problemNum = gets.chomp.to_i\n\nresult = 0\n\n(0...problemNum).each do |iter|\n\tinput = gets.chomp.split(' ').map {|item| item.to_i}\n\tresult += 1 if input.count(1) > 1\nend\n\nputs result"}, {"source_code": "n = gets.to_i \nproblems_solved = 0 \nn.times {\n arr = gets.split(\" \").map(&:to_i)\n if arr.select{|a| a==1 }.count >=2\n problems_solved+=1 \n end \n}\nputs problems_solved"}, {"source_code": "n = gets.to_i\ncount = 0\nfor i in 1..n\n buf = gets.split(' ').map(&:to_i)\n a, b, c = buf[0], buf[1], buf[2]\n count += 1 if a + b + c >= 2\nend\nputs count\n"}, {"source_code": "p$<.grep(/1.*1/).size"}, {"source_code": "going_to_solve = 0\ngets.to_i.times { going_to_solve += 1 if gets.gsub(\" \", \"\").count(\"1\") >= 2 }\nputs going_to_solve"}, {"source_code": "line = gets.chomp\nstring_value = []\nline.to_i.times do |i|\n\tvalue = gets.chomp\n\tstring_value << value\nend\n\ncount = 0\nstring_value.each do |value|\n\tif value.split.count{|x| x.to_i == 1} >= 2\n\t\tcount += 1\n\tend\n\nend \nputs count"}, {"source_code": "d=0\ngets.to_i.times{\n\ta,b,c=gets.split.map{|e| e.to_i}\n\td=d+(a+b+c>1?1:0)\n}\nputs d\n"}], "negative_code": [{"source_code": "#my code goes here\na = []\nk = 0\nn = gets.to_i\nfor c in 1..n\nfor b in 0...3\ns = 0\na[b] = gets.to_i\na.each do |x|\n s += x\nend\nend\nif s >= 2\nk = k + 1\nelse\nk = k + 0\nend\nend\nputs \"#{k}\"\n\n"}, {"source_code": "gets.to_i.times.map{gets.split.map(&:to_i).inject(:+)}.select{|x| x >= 2}.size"}, {"source_code": "n = gets.to_i\nans = 0\n\n(1..n).each do |i|\n res = 0\n (1..3).each do |j|\n tmp = gets.to_i\n res += tmp\n end\n\n if res >= 2\n ans += 1\n end\nend\n\nputs ans"}, {"source_code": "ans=0\na=[]\ngets.to_i.times{|i|\n\ta< a -1 \n\tb = gets.chomp(\" \")\n\ttemp = 0\n\t#puts b[0].to_s + \" - This is it!\"\n\tif b[0].to_i == 1 \n\t\ttemp = temp + 1\n\tend\n\tif b[1].to_i == 1 \n\t\ttemp = temp + 1\n\tend\n\tif b[2].to_i == 1 \n\t\ttemp = temp + 1\n\tend\n\tif temp>=2 \n\t\tcount = count + 1 \n\tend\n\tindex = index + 1 \nend\nputs count "}, {"source_code": "n = gets.to_i\nl = []\n(1..n).each { l << gets.split.map(&:to_i) }\nputs l.transpose.map { |x| x.reduce(:+) }.reduce(0) { |res, x| res + (x > 1 ? 1 : 0) }"}, {"source_code": "p = gets.chomp.to_i\ns = 0\n\nfor i in 1..p do\n a = gets.chomp.split(\" \").sort.join(\"\")\n if a.to_i != 1\n s += 1\n end\nend\n\nputs s"}, {"source_code": "gets.to_i.times do\n a = gets.split.map(&:to_i).inject(:+)\n a += a\nend"}, {"source_code": "n, *s = STDIN.read.split(\"\\n\") #gets.sFile.readlines('inp.txt')\nputs s.length\nans = 0\ns.each do |i|\n\tif i[0].to_i + i[2].to_i + i[4].to_i >= 2\n\t\tans += 1\n\tend\nend\nputs ans"}, {"source_code": "n, *s = gets.split('/n') #File.readlines('inp.txt')\nans = 0\ns.each do |i|\n\tif i[0].to_i + i[2].to_i + i[4].to_i >= 2\n\t\tans += 1\n\tend\nend\nputs ans"}, {"source_code": "\namountOfProblems = gets.chomp#get input on first line\namountOfProblems = amountOfProblems.to_i\nwillWorkSolution = 0\nsolutionsTeamWillWork = []\n\namountOfProblems.times do\n willWorkSolution = gets.chomp # 1 0 1 - space separated other input\n willWorkSolution = willWorkSolution.split(\" \") # array is: [\"1\", \"0\", \"1\"]\n \n willWorkSolution.each do |one_or_zero|\n if (one_or_zero == \"1\")\n solutionsTeamWillWork.push(one_or_zero)\n end\n end\nend\n\n\nputs solutionsTeamWillWork.size / amountOfProblems\n"}, {"source_code": "\namountOfProblems = gets.chomp#get input on first line\namountOfProblems = amountOfProblems.to_i\nwillWorkSolution = 0\nsolutionsTeamWillWork = []\n\n\namountOfProblems.times do\n willWorkSolution = gets.chomp # 1 0 1 - space separated other input\n willWorkSolution = willWorkSolution.split(\" \") # array is: [\"1\", \"0\", \"1\"]\n \n counter = 0\n \n willWorkSolution.each do |one_or_zero|\n \n if (one_or_zero == \"1\")\n counter = counter + 1\n if (counter >= 2)\n solutionsTeamWillWork.push(1) \n end\n end\n end\nend\n\n\nputs solutionsTeamWillWork.size\n"}, {"source_code": "n = gets.chomp.to_i\na = []\nres = 0\nfor i in 0...n\n a[i] = gets.chomp\n if a.count('1')>=2\n res+=1\n end\nend\nputs res"}], "src_uid": "3542adc74a41ccfd72008faf983ffab5"} {"nl": {"description": "Nick's company employed n people. Now Nick needs to build a tree hierarchy of «supervisor-surbodinate» relations in the company (this is to say that each employee, except one, has exactly one supervisor). There are m applications written in the following form: «employee ai is ready to become a supervisor of employee bi at extra cost ci». The qualification qj of each employee is known, and for each application the following is true: qai > qbi. Would you help Nick calculate the minimum cost of such a hierarchy, or find out that it is impossible to build it.", "input_spec": "The first input line contains integer n (1 ≤ n ≤ 1000) — amount of employees in the company. The following line contains n space-separated numbers qj (0 ≤ qj ≤ 106)— the employees' qualifications. The following line contains number m (0 ≤ m ≤ 10000) — amount of received applications. The following m lines contain the applications themselves, each of them in the form of three space-separated numbers: ai, bi and ci (1 ≤ ai, bi ≤ n, 0 ≤ ci ≤ 106). Different applications can be similar, i.e. they can come from one and the same employee who offered to become a supervisor of the same person but at a different cost. For each application qai > qbi.", "output_spec": "Output the only line — the minimum cost of building such a hierarchy, or -1 if it is impossible to build it.", "sample_inputs": ["4\n7 2 3 1\n4\n1 2 5\n2 4 1\n3 4 1\n1 3 5", "3\n1 2 3\n2\n3 1 2\n3 1 3"], "sample_outputs": ["11", "-1"], "notes": "NoteIn the first sample one of the possible ways for building a hierarchy is to take applications with indexes 1, 2 and 4, which give 11 as the minimum total cost. In the second sample it is impossible to build the required hierarchy, so the answer is -1."}, "positive_code": [{"source_code": "INF = 10 ** 20\nn = gets.to_i\ngets\nd = [INF] * (n + 1)\ngets.to_i.times do\n\ta, b, c = gets.split.map(&:to_i)\n\td[b] = [d[b], c].min\nend\ns = d.inject(&:+) - 2 * INF\np s < INF ? s : -1\n"}, {"source_code": "INF = 10 ** 20\nn = gets.to_i\ngets\nd = [INF] * (n + 1)\ngets.to_i.times do\n\ta, b, c = gets.split.map(&:to_i)\n\td[b] = [d[b], c].min\nend\ns = d.inject(&:+) - 2 * INF\np s < INF ? s : -1\n\n"}, {"source_code": "INF = 10 ** 20\nn = gets.to_i\ngets\nd = [INF] * (n + 1)\ngets.to_i.times do\n\ta, b, c = gets.split.map(&:to_i)\n\td[b] = [d[b], c].min\nend\ns = d.inject(&:+) - 2 * INF\np s < INF ? s : -1\n\n"}, {"source_code": "n = gets.chomp.to_i\nq = gets.chomp.split(' ').map &:to_i; q.unshift nil\nm = gets.chomp.to_i\nresult = {}\nm.times do\n a, b, c = gets.chomp.split(' ').map &:to_i\n next if q[a] <= q[b]\n result[b] = result[b] ? [result[b], c].min : c\nend\nputs result.length == n - 1 ? result.values.inject(:+).to_i : -1"}, {"source_code": "INF = 10 ** 20\nn = gets.to_i\ngets\nd = [INF] * (n + 1)\ngets.to_i.times do\n\ta, b, c = gets.split.map(&:to_i)\n\td[b] = [d[b], c].min\nend\ns = d.inject(&:+) - 2 * INF\np s < INF ? s : -1\n"}, {"source_code": "n=gets.chomp.to_i\ngets\nm=gets.chomp.to_i\nmax=100000000\nmemo=Array.new\nn.times{memo << max}\nm.times{\n tmp=gets.chomp.split()\n b=tmp[1].to_i()-1\n c=tmp[2].to_i\n if memo[b] > c\n memo[b]=c\n end\n}\nsum=0\ncnt=0\nn.times{|i|\n if memo[i] c)\n employees[b - 1][a - 1] = c\n end\nend\n\nacc = 0\nroot = 0\n\nn.times {|i|\n min = employees[i].min\n if (min == UNSET)\n root += 1\n if (root > 1)\n puts -1\n exit\n end\n else\n acc += min\n end\n}\n\nputs acc \n"}, {"source_code": "INF = 10 ** 20\nn = gets.to_i\ngets\nd = [INF] * (n + 1)\ngets.to_i.times do\n a, b, c = gets.split.map(&:to_i)\n d[b] = [d[b], c].min\nend\ns = d.inject(&:+) - 2 * INF\np s < INF ? s : -1"}, {"source_code": "INF = 10 ** 20\nn = gets.to_i\ngets\nd = [INF] * (n + 1)\ngets.to_i.times do\n\ta, b, c = gets.split.map(&:to_i)\n\td[b] = [d[b], c].min\nend\ns = d.inject(&:+) - 2 * INF\np s < INF ? s : -1\n\n"}, {"source_code": "INF = 10 ** 20\nn = gets.to_i\ngets\nd = [INF] * (n + 1)\ngets.to_i.times do\n\ta, b, c = gets.split.map(&:to_i)\n\td[b] = [d[b], c].min\nend\ns = d.inject(&:+) - 2 * INF\np s < INF ? s : -1\n"}, {"source_code": "n = gets.chomp.to_i\nq = gets.chomp.split(' ').map &:to_i; q.unshift nil\nm = gets.chomp.to_i\nresult = {}\nm.times do\n a, b, c = gets.chomp.split(' ').map &:to_i\n next if q[a] <= q[b]\n result[b] = result[b] ? [result[b], c].min : c\nend\nputs result.length == n - 1 ? result.values.inject(:+).to_i : -1\n"}, {"source_code": "INF = 10 ** 20\nn = gets.to_i\ngets\nd = [INF] * (n + 1)\ngets.to_i.times do\n\ta, b, c = gets.split.map(&:to_i)\n\td[b] = [d[b], c].min\nend\ns = d.inject(&:+) - 2 * INF\np s < INF ? s : -1\n\n"}, {"source_code": "INF = 10 ** 20\nn = gets.to_i\ngets\nd = [INF] * (n + 1)\ngets.to_i.times do\n\ta, b, c = gets.split.map(&:to_i)\n\td[b] = [d[b], c].min\nend\ns = d.inject(&:+) - 2 * INF\np s < INF ? s : -1\n\n"}, {"source_code": "INF = 10 ** 20\nn = gets.to_i\ngets\nd = [INF] * (n + 1)\ngets.to_i.times do\n\ta, b, c = gets.split.map(&:to_i)\n\td[b] = [d[b], c].min\nend\ns = d.inject(&:+) - 2 * INF\np s < INF ? s : -1\n\n"}], "negative_code": [{"source_code": "n = gets.chomp.to_i\nq = gets.chomp.split(' ').map &:to_i; q.unshift nil\nm = gets.chomp.to_i\nresult = {}\nm.times do\n a, b, c = gets.chomp.split(' ').map &:to_i\n next if q[a] <= q[b]\n result[b] = result[b] ? [result[b], c].min : c\nend\nputs result.length == n - 1 ? result.values.inject(:+) : -1\n"}, {"source_code": "n = gets.chomp.to_i\nq = gets.chomp.split(' ').map &:to_i\nm = gets.chomp.to_i\nresult = {}\nm.times do\n a, b, c = gets.chomp.split(' ').map &:to_i\n result[b] = result[b] ? [result[b], c].min : c\nend\nputs result.length == n - 1 ? result.values.inject(:+) : -1\n"}], "src_uid": "ddc9b2bacfaa4f4e91d5072240c1e811"} {"nl": {"description": "Petya and Vasya are playing a game. Petya's got n non-transparent glasses, standing in a row. The glasses' positions are indexed with integers from 1 to n from left to right. Note that the positions are indexed but the glasses are not.First Petya puts a marble under the glass in position s. Then he performs some (possibly zero) shuffling operations. One shuffling operation means moving the glass from the first position to position p1, the glass from the second position to position p2 and so on. That is, a glass goes from position i to position pi. Consider all glasses are moving simultaneously during one shuffling operation. When the glasses are shuffled, the marble doesn't travel from one glass to another: it moves together with the glass it was initially been put in.After all shuffling operations Petya shows Vasya that the ball has moved to position t. Vasya's task is to say what minimum number of shuffling operations Petya has performed or determine that Petya has made a mistake and the marble could not have got from position s to position t.", "input_spec": "The first line contains three integers: n, s, t (1 ≤ n ≤ 105; 1 ≤ s, t ≤ n) — the number of glasses, the ball's initial and final position. The second line contains n space-separated integers: p1, p2, ..., pn (1 ≤ pi ≤ n) — the shuffling operation parameters. It is guaranteed that all pi's are distinct. Note that s can equal t.", "output_spec": "If the marble can move from position s to position t, then print on a single line a non-negative integer — the minimum number of shuffling operations, needed to get the marble to position t. If it is impossible, print number -1.", "sample_inputs": ["4 2 1\n2 3 4 1", "4 3 3\n4 1 3 2", "4 3 4\n1 2 3 4", "3 1 3\n2 1 3"], "sample_outputs": ["3", "0", "-1", "-1"], "notes": null}, "positive_code": [{"source_code": "n, s, t = gets.split.map &:to_i\np = [0] + gets.split.map(&:to_i)\n\nvisited = {s => true}\n\nshuffle_count = 0\ncurrent_s = s\nuntil current_s == t do\n if visited.has_key? p[current_s]\n puts -1\n exit\n end\n current_s = p[current_s]\n shuffle_count += 1\n visited[current_s] = true\nend\n\nputs shuffle_count\n"}, {"source_code": "n, s, t = gets.split.map &:to_i\np = gets.split.map &:to_i\n\nvisited = {s => true}\n\nshuffle_count = 0\ncurrent_s = s\nuntil current_s == t do\n if visited.has_key? p[current_s - 1]\n puts -1\n exit\n end\n current_s = p[current_s - 1]\n shuffle_count += 1\n visited[current_s] = true\nend\n\nputs shuffle_count\n"}, {"source_code": "I =->{gets.split.map &:to_i}\ndef gao\n\tn, s, t = I[]; return 0 if s==t\n\ts -= 1; t -= 1; a = I[]; vis = Array.new(n, 0); s = a[s] - 1; res = 1; while (s != t) do\n\t\tvis[s] = 1; s = a[s]-1; res += 1\n\t\treturn -1 if !vis[s].zero?\n\tend\n\tres\nend\nputs gao()\n"}, {"source_code": "st = gets.split\nn=st[0].to_i\ns=st[1].to_i\nt=st[2].to_i\n\nmas = gets.split\n\ncount=0\npath_exists=false\nif s==t\n puts 0\nelse\n cur=s\n (n+1).times do\n cur=mas[cur-1].to_i\n count+=1\n if cur==t\n path_exists = true\n break\n end\n end\n if path_exists\n print(count)\n else\n print(-1)\n end\nend"}, {"source_code": "#!/usr/bin/env ruby -w\n\nrequire 'set'\n\nn_glasses, ball_start, ball_end = gets.split(' ').map(&:to_i)\nball_start -= 1\nball_end -= 1\n\nmarbles = gets.split(' ').map(&:to_i).map(&:pred)\n\niterations = 0\n\nvisited = Set.new\n\nactual_index = ball_start\nuntil actual_index == ball_end || iterations == -1\n if visited.include? actual_index\n iterations = -1\n else\n visited << actual_index\n actual_index = marbles[actual_index]\n iterations += 1\n end\nend\n\nprint iterations\n\n\n\n"}, {"source_code": "n, s, t = gets.split.map(&:to_i)\n\np = [0] + gets.split.map(&:to_i)\n\na = s;\ncount = 0\nuntil a == t\n a = p[a]\n count += 1\n if a == s\n count = -1\n\tbreak\n end\nend\n\nputs count"}, {"source_code": "#!/usr/bin/ruby1.9\n\nn, s, t = STDIN.readline.split.collect {|s| s.to_i }\n\nmove = [0] + STDIN.readline.split.collect {|s| s.to_i }\n\ncurr = s\nsteps = 0\n\nwhile true\n\tbreak if curr == t\n\tsteps += 1\n\tcurr = move[curr]\n\tif curr == s\n\t\tsteps = -1\n\t\tbreak\n\tend\nend\n\nputs steps\n"}, {"source_code": "n,s,t=gets.chomp.split.map(&:to_i)\na=gets.chomp.split.map(&:to_i).unshift('a')\nd,k=s,0\nwhile d!=t\n\td=a[d]\n\tk+=1\n\tif k>n\n\t\tk=-1\n\t\tbreak\n\tend\nend\nputs k"}, {"source_code": "class B\n def initialize\n n, s, t = $stdin.gets.split(' ').map(&:to_i)\n operation = $stdin.gets.split(' ').map(&:to_i)\n\n\n n.times do |turn|\n if s == t\n puts turn\n exit\n elsif s == operation[s-1]\n puts -1\n exit\n else\n s = operation[s-1]\n end\n end\n puts -1\n end\nend\n\nb = B.new"}, {"source_code": "n,s,t=gets.split.map(&:to_i)\np=gets.split.map(&:to_i)\np.unshift(0)\nv=s\nres=-1\n0.upto(n){|x|\n\tif v == t then\n\t\tres=x\n\t\tbreak\n\tend\n\tv=p[v];\n}\nputs res\n"}, {"source_code": "n,s,t = gets.split(\" \").map(&:to_i)\narr = gets.split(\" \").map(&:to_i)\nvisited = Array.new(n,false)\nans = -1\ni = s - 1\ncount = 0\nif s == t\n\tans = 0\nelse\n\twhile true\n\t\tvisited[i] = true\n\t\tif !visited[arr[i]-1]\n\t\t\ti = arr[i] - 1\n\t\t\tcount += 1\n\t\telse\n\t\t\t# puts \"breaking here #{arr[i]} #{visited}\"\n\t\t\tbreak\n\t\tend\n\t\tif i == t - 1\t\t\t\n\t\t\tans = count\n\t\t\tbreak \n\t\tend\n\n\tend\nend\n# puts \"#{visited} #{i}\"\nputs ans\n\n"}, {"source_code": "n,s,t = gets.chomp.split(\" \").collect {|x| x.to_i}\n\narr = gets.chomp.split(\" \").collect {|x| (x.to_i-1)}\n\naux = [0]*n\n\n(0...n).each{ |v|\n aux[arr[v]] = v\n}\n\nq = 0\ns-=1\nt-=1\n\nwhile s!=t and q turn\n puts turn - diff\n elsif diff > 0 && t == s\n puts n - diff\n else\n puts -1\n end\n end\nend\n\nb = B.new"}, {"source_code": "class B\n def initialize\n n, s, t = $stdin.gets.split(' ').map(&:to_i)\n operation = $stdin.gets.split(' ').map(&:to_i)\n\n\n n.times do |turn|\n if s == t\n puts turn\n break\n elsif s == operation[s-1]\n puts -1\n break\n else\n s = operation[s-1]\n end\n end\n end\nend\n\nb = B.new"}, {"source_code": "class B\n def initialize\n n, s, t = $stdin.gets.split(' ').map(&:to_i)\n operation = $stdin.gets.split(' ').map(&:to_i)\n\n if s == t\n puts 0\n return 0\n end\n\n diff = 0\n\n turn = 0\n operation.each do |pos|\n turn += 1\n if s == pos\n diff += 1\n else\n s = pos\n end\n break if t == s\n end\n\n if t == s && n >= turn\n if diff > 0\n puts \"#{turn - diff}\"\n else\n puts -1\n end\n else\n puts -1\n end\n end\nend\n\nb = B.new"}, {"source_code": "class B\n def initialize\n n, s, t = $stdin.gets.split(' ').map(&:to_i)\n operation = $stdin.gets.split(' ').map(&:to_i)\n\n if s == t\n puts 0\n return 0\n end\n\n diff = 0\n\n turn = 0\n operation.each do |pos|\n turn += 1\n if s == pos\n diff += 1\n s = pos\n else\n s = pos\n end\n break if t == s\n end\n\n if t == s && n > turn\n puts turn - diff\n elsif diff > 0 && t == s\n puts n - diff\n else\n puts -1\n end\n end\nend\n\nb = B.new"}, {"source_code": "class B\n def initialize\n n, s, t = $stdin.gets.split(' ').map(&:to_i)\n operation = $stdin.gets.split(' ').map(&:to_i)\n\n if s == t\n puts 0\n return 0\n end\n\n diff = 0\n\n list = []\n\n turn = 0\n operation.each do |pos|\n turn += 1\n if list.include?(pos)\n diff += 1\n else\n s = pos\n list << pos\n end\n break if t == s\n end\n\n if t == s && n >= turn\n if diff > 0\n puts \"#{turn - diff}\"\n else\n puts -1\n end\n else\n puts -1\n end\n end\nend\n\nb = B.new"}, {"source_code": "class B\n def initialize\n n, s, t = $stdin.gets.split(' ').map(&:to_i)\n operation = $stdin.gets.split(' ').map(&:to_i)\n\n if s == t\n puts 0\n return 0\n end\n\n diff = 0\n\n operation.each do |pos|\n if s == pos\n diff += 1\n s = pos\n else\n s = pos\n end\n end\n\n if diff > 0\n puts n-diff\n else\n puts -1\n end\n end\nend\n\nb = B.new"}, {"source_code": "class B\n def initialize\n n, s, t = $stdin.gets.split(' ').map(&:to_i)\n operation = $stdin.gets.split(' ').map(&:to_i)\n\n if s == t\n puts 0\n return 0\n end\n\n diff = 0\n\n turn = 0\n operation.each do |pos|\n turn += 1\n if s == pos\n diff += 1\n s = pos\n else\n s = pos\n end\n break if t == s\n end\n\n if diff > 0 && t == s && n > turn\n puts turn - diff\n elsif diff > 0 && t == s\n puts n - diff\n else\n puts -1\n end\n end\nend\n\nb = B.new"}, {"source_code": "class B\n def initialize\n n, s, t = $stdin.gets.split(' ').map(&:to_i)\n operation = $stdin.gets.split(' ').map(&:to_i)\n\n\n n.times do |turn|\n if s == t\n puts turn\n break\n elsif s == operation[s-1]\n puts -1\n break\n else\n s = operation[s-1]\n end\n end\n puts -1\n end\nend\n\nb = B.new"}, {"source_code": "class B\n def initialize\n n, s, t = $stdin.gets.split(' ').map(&:to_i)\n operation = $stdin.gets.split(' ').map(&:to_i)\n\n if s == t\n puts 0\n return 0\n end\n\n diff = 0\n\n turn = 0\n operation.each do |pos|\n turn += 1\n if s == pos\n diff += 1\n else\n s = pos\n end\n end\n\n if t == s && n >= turn\n if diff > 0\n puts \"#{turn - diff}\"\n else\n puts -1\n end\n else\n puts -1\n end\n end\nend\n\nb = B.new"}, {"source_code": "class B\n def initialize\n n, s, t = $stdin.gets.split(' ').map(&:to_i)\n operation = $stdin.gets.split(' ').map(&:to_i)\n\n if s == t\n puts 0\n return 0\n end\n\n diff = 0\n\n operation.each do |pos|\n if s == pos\n diff += 1\n s = pos\n else\n s = pos\n end\n end\n\n if diff > 0 && t == s\n puts n-diff\n else\n puts -1\n end\n end\nend\n\nb = B.new"}, {"source_code": "class B\n def initialize\n n, s, t = $stdin.gets.split(' ').map(&:to_i)\n operation = $stdin.gets.split(' ').map(&:to_i)\n\n if s == t\n puts 0\n return 0\n elsif s == operation[0]\n puts n-1\n else\n puts -1\n end\n\n end\nend\n\nb = B.new"}, {"source_code": "class B\n def initialize\n n, s, t = $stdin.gets.split(' ').map(&:to_i)\n operation = $stdin.gets.split(' ').map(&:to_i)\n\n if s == t\n puts 0\n return 0\n end\n\n diff = 0\n\n turn = 0\n operation.each do |pos|\n turn += 1\n if s == pos\n diff += 1\n else\n s = pos\n end\n break if t == s\n end\n\n if t == s && n > turn\n puts \"#{turn - diff}\"\n elsif diff > 0 && t == s\n puts \"#{n - diff}\"\n else\n puts -1\n end\n end\nend\n\nb = B.new"}], "src_uid": "b01602b51b9103c0f31c3f50b32aa88d"} {"nl": {"description": "Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $$$2n$$$ cannons were placed. Initial polygon for $$$n=4$$$. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.More formally: if a cannon stands in the row $$$i$$$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($$$i, 1$$$) and ends in some cell ($$$i, j$$$); if a cannon stands in the column $$$j$$$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($$$1, j$$$) and ends in some cell ($$$i, j$$$). For example, consider the following sequence of shots: 1. Shoot the cannon in the row $$$2$$$.                         2. Shoot the cannon in the row $$$2$$$.                         3. Shoot the cannon in column $$$3$$$. You have a report from the military training on your desk. This report is a square matrix with side length $$$n$$$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 \\le n \\le 50$$$) — the size of the polygon. This is followed by $$$n$$$ lines of length $$$n$$$, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed $$$10^5$$$.", "output_spec": "For each test case print: YES if there is a sequence of shots leading to a given matrix; NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case.", "sample_inputs": ["5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0111"], "sample_outputs": ["YES\nNO\nYES\nYES\nNO"], "notes": "NoteThe first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell ($$$1, 1$$$) flying out of any cannon would continue its flight further."}, "positive_code": [{"source_code": "gets.to_i.times do\n\tn = gets.to_i\n\ts = (1..n).map { gets.chomp }\n\tputs (0...n - 1).to_a.product((0...n - 1).to_a).all? { |i, j|\n\t\ts[i][j] == '0' || s[i + 1][j] == '1' || s[i][j + 1] == '1'\n\t} ? \"YES\" : \"NO\"\nend\n"}, {"source_code": "def in_range(i, j, n)\n i < n && j < n\nend\n\ngets.strip.to_i.times do |t|\n n = gets.strip.to_i\n board = []\n n.times do\n board << gets.strip\n end\n res = true\n (0..n-1).to_a.each do |i|\n (0..n-1).to_a.each do |j|\n if board[i][j] == '1'\n if in_range(i+1, j, n) && board[i+1][j] == '0' && in_range(i, j+1, n) && board[i][j+1] == '0'\n res = false\n break\n end\n end\n end\n break if res == false\n end\n\n puts res ? \"YES\" : \"NO\"\nend\n\n"}, {"source_code": "t = gets.chomp.to_i\nt.times do |testcase|\n n = gets.chomp.to_i\n grid = []\n n.times do |i|\n grid << gets.chomp\n end\n flag = true\n n.times do |i|\n n.times do |j|\n if grid[i][j] == \"1\"\n if !(i==n-1 || j==n-1 || grid[i+1][j]==\"1\" || grid[i][j+1]==\"1\")\n flag = false\n break\n end\n end\n end\n if !flag\n break\n end\n end\n if flag\n puts \"YES\"\n else\n puts \"NO\"\n end\nend"}, {"source_code": "gets.to_i.times do\n n=gets.to_i\n a=(1..n).map{gets.chomp}\n puts ([*0...n-1].product([*0...n-1]).map.any?{|i, j| a[i][j]+a[i+1][j]+a[i][j+1]=='100'} ? :NO : :YES)\nend\n"}, {"source_code": "gets.to_i.times do\n n=gets.to_i\n a=(1..n).map{gets.chomp}\n puts ([*0...n-1].product([*0...n-1]).map.any?{|i, j| a[i][j]+a[i+1][j]+a[i][j+1]=='100'} ? :NO : :YES)\nend"}, {"source_code": "t = gets.to_i\nt.times do\n n = gets.to_i\n s = n.times.map { gets.chomp }\n ok = true\n (n - 2).downto(0) do |i|\n (n - 2).downto(0) do |j|\n if s[i][j] == \"1\"\n if !(s[i][j + 1] == \"1\" || s[i + 1][j] == \"1\")\n ok = false\n break\n end\n end\n end\n break if !ok\n end\n puts ok ? \"YES\" : \"NO\"\nend\n"}], "negative_code": [{"source_code": "def func(a)\n a.map{|e| e.join.sub(/([o1]*)[o1]$/){|s|s=?o*$1.size+?o}.split('')}\nend\n\ndef func2(a)\n a.map{|e| e.join.sub(/^(1+)o/){|s|s=?o*$1.size+?o}.split('')}\nend\n\ngets.to_i.times{\n n=gets.to_i\n a=(1..n).map{gets.chomp.chars}\n a=func(a)\n a=func(a.transpose)\n a=func2(a)\n a=func2(a.transpose.map(&:reverse))\n #a.map{|e|puts e.join}\n puts (a.flatten.none?{|e|e==?1} ? :YES : :NO)\n}\n"}, {"source_code": "def func(a)\n a.map{|e| e.join.sub(/([o1]*)[o1]$/){|s|s=?o*$1.size+?o}.split('')}\nend\n\ndef func2(a)\n a.map{|e| e.join.sub(/(1+)o/){|s|s=?o*$1.size+?o}.split('')}\nend\n\ngets.to_i.times{\n n=gets.to_i\n a=(1..n).map{gets.chomp.chars}\n a=func(a)\n a=func(a.transpose)\n a=func2(a)\n a=func2(a.transpose)\n puts (a.flatten.none?{|e|e==?1} ? :YES : :NO)\n #a.map{|e|puts e.join}\n}\n"}, {"source_code": "def func(a)\n n=a.size\n q=[]\n (0...n).each do |i|\n if a[i][-1]==?1\n q << [i, n-1]\n elsif a[-1][i]==?1\n q << [n-1, i]\n end\n end\n\n while q[0]\n r, c=q.pop\n a[r][c]=?*\n [[0, -1], [-1, 0]].each do |dr, dc|\n nr=r+dr\n nc=c+dc\n next if nr<0 or nr>=n or nc<0 or nc>=n\n next if a[nr][nc]!=?1\n q << [nr, nc]\n end\n end\nend\n\n\n\ngets.to_i.times do\n n=gets.to_i\n a=(1..n).map{gets.chomp}\n func(a)\n\n puts (a.none?{|e|e[?1]} ? :YES : :NO)\n #a.map{|e|puts e}; puts\nend\n"}, {"source_code": "def func(a)\n n=a.size\n q=[]\n (0...n).each do |i|\n if a[i][-1]==?1\n q << [i, n-1]\n elsif a[-1][i]==?1\n q << [n-1, i]\n end\n end\n\n while q[0]\n r, c=q.shift\n a[r][c]=?*\n [[0, -1], [-1, 0]].each do |dr, dc|\n nr=r+dr\n nc=c+dc\n next if nr<0 or nr>=n or nc<0 or nc>=n\n next if a[nr][nc]!=?1\n q << [nr, nc]\n end\n end\nend\n\n\n\ngets.to_i.times do\n n=gets.to_i\n a=(1..n).map{gets.chomp}\n func(a)\n\n puts (a.none?{|e|e[?1]} ? :YES : :NO)\n #a.map{|e|puts e}; puts\nend\n"}, {"source_code": "def func(a)\n a.map{|e| e.join.sub(/([#1]*)[#1]$/){|s|s=?#*$1.size+?#}.split('')}\nend\n\ndef func2(a)\n a.map{|e| e.join.gsub(/(1+)#/){|s|s=?#*$1.size+?#}.split('')}\nend\n\ngets.to_i.times{\n n=gets.to_i\n a=(1..n).map{gets.chomp.chars}\n a=func(a)\n a=func(a.transpose)\n a=func2(a)\n a=func2(a.transpose)\n puts (a.flatten.none?{|e|e==?1} ? :YES : :NO)\n #a.map{|e|puts e.join}; puts\n}\n"}, {"source_code": "def func(a)\n a.map{|e| e.join.sub(/([#1]*)[#1]$/){|s|s=?#*$1.size+?#}.split('')}\nend\n\ndef func2(a)\n a.map{|e| e.join.gsub(/(1+)#/){|s|s=?#*$1.size+?#}.split('')}\nend\n\ngets.to_i.times{\n n=gets.to_i\n a=(1..n).map{gets.chomp.chars}\n a=func(a)\n a=func(a.transpose)\n b=[]\n while a!=b\n b=func2(a)\n b=func2(b.transpose)\n a=b.dup\n end\n puts (a.flatten.none?{|e|e==?1} ? :YES : :NO)\n #a.map{|e|puts e.join}; puts\n}\n"}], "src_uid": "eea15ff7e939bfcc7002cacbc8a1a3a5"} {"nl": {"description": "Vasya has found a piece of paper with an array written on it. The array consists of n integers a1, a2, ..., an. Vasya noticed that the following condition holds for the array ai ≤ ai + 1 ≤ 2·ai for any positive integer i (i < n).Vasya wants to add either a \"+\" or a \"-\" before each number of array. Thus, Vasya will get an expression consisting of n summands. The value of the resulting expression is the sum of all its elements. The task is to add signs \"+\" and \"-\" before each number so that the value of expression s meets the limits 0 ≤ s ≤ a1. Print a sequence of signs \"+\" and \"-\", satisfying the given limits. It is guaranteed that the solution for the problem exists.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 105) — the size of the array. The second line contains space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 109) — the original array. It is guaranteed that the condition ai ≤ ai + 1 ≤ 2·ai fulfills for any positive integer i (i < n).", "output_spec": "In a single line print the sequence of n characters \"+\" and \"-\", where the i-th character is the sign that is placed in front of number ai. The value of the resulting expression s must fit into the limits 0 ≤ s ≤ a1. If there are multiple solutions, you are allowed to print any of them.", "sample_inputs": ["4\n1 2 3 5", "3\n3 3 5"], "sample_outputs": ["+++-", "++-"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\n\na = gets.split(\" \").collect{|x| x.to_i}\nsigns = \"\"\n\ns=0\na.reverse_each{|x| \n\tif s > 0\n\t\ts -= x\n\t\tsigns << '-'\n\telse\n\t\ts+=x\n\t\tsigns << '+'\n\tend\n}\n\nresult = \"\"\n\nsigns.reverse.each_char{|c|\n\tif s >= 0\n\t\tresult << c\n\telse \n\t\tresult << (c == '+' ? '-' : '+')\n\tend\n}\n\nputs result"}], "negative_code": [{"source_code": "n = gets.to_i\n\na = gets.split(\" \").collect{|x| x.to_i}\na.sort!{|x,y| y <=> x}\nsigns = \"\"\n\ns=0\na.each{|x| \n\tif s > 0\n\t\ts -= x\n\t\tsigns += '-'\n\telse\n\t\ts+=x\n\t\tsigns +='+'\n\tend\n}\n\nresult = \"\"\nsigns.each_char{|c|\n\tif s >= 0\n\t\tresult+= c\n\telse \n\t\tresult += c == '+' ? '-' : '+'\n\tend\n}\n\nputs result"}], "src_uid": "30f64b963310911196ebf5d624c01cc3"} {"nl": {"description": "Bachgold problem is very easy to formulate. Given a positive integer n represent it as a sum of maximum possible number of prime numbers. One can prove that such representation exists for any integer greater than 1.Recall that integer k is called prime if it is greater than 1 and has exactly two positive integer divisors — 1 and k. ", "input_spec": "The only line of the input contains a single integer n (2 ≤ n ≤ 100 000).", "output_spec": "The first line of the output contains a single integer k — maximum possible number of primes in representation. The second line should contain k primes with their sum equal to n. You can print them in any order. If there are several optimal solution, print any of them.", "sample_inputs": ["5", "6"], "sample_outputs": ["2\n2 3", "3\n2 2 2"], "notes": null}, "positive_code": [{"source_code": "n=gets.to_i\np n/2\nputs [*[2]*(n/2-1),2+n%2]*' '"}, {"source_code": "n = gets.chomp.to_i\n\nans = []\nif n.even?\n ans = [2] * (n / 2)\nelse\n ans = [2] * (n / 2 - 1) + [3]\nend\nputs ans.length\nputs ans.join(\" \")\n"}, {"source_code": "n = gets.to_i\n\nif(n%2==0)\n puts n/2\n (n/2).times{print 2.to_s + \" \"}\nelse\n puts (n-1)/2\n ((n/2)-1).times{print 2.to_s + \" \"}\n print 3.to_s\n \n\n\n\nend"}, {"source_code": "n = gets.to_i\nans = n / 2\nputs ans\nif (n % 2 == 1)\n print 3, \" \"\n n -= 3\nend\n\n(0...n / 2).each do\n print 2, \" \"\nend"}, {"source_code": "n = gets.to_i\nif n.even?\n k = n/2\n puts k\n puts ([2] * k).join(\" \")\nelse\n k = 1\n n -= 3\n ps = [3]\n k += n/2\n ps << ([2] * (k-1))\n puts k\n puts ps.join(\" \")\nend"}, {"source_code": "given_number = gets.chomp.to_i\n\nk = given_number / 2\n\nmd = given_number % 2\nary = [2]\nif md == 1\n ary *= k - 1\n ary << 3\nelse\n ary *= k\nend\n\nputs k\nputs ary.join(' ')\n"}, {"source_code": "n = gets.to_i\n\nodd = n.odd?\nn -= 3 if odd\n\nputs (n/2 + (odd ? 1 : 0))\n\nputs ((n/2).times.map{\"2\"} + (odd ? [\"3\"] : [])).join(\" \")\n"}, {"source_code": "n = gets.to_i\ncount3 = n%2\ncount2 = n/2 - count3\nputs count2 + count3\n(1..count2).each do\n print 2, ' '\nend\nprint count3==1? 3 : ''\n"}, {"source_code": "require 'prime'\nrequire 'set'\na=[\"\",\"\",\"1\\n2\",\"1\\n3\",\"2\\n2 2\",\"2\\n2 3\"]\nn=gets.to_i\n\nif n<6 then\n\tputs a[n]\nelse\n\tif n%2==0 then\n\t\tputs n/2\n\t\t(n/2).times{|i|\n\t\t\tprint \" \" if i>0\n\t\t\tprint \"2\"\n\t\t}\n\t\tprint \"\\n\"\n\telse\n\t\td=n-3\n\t\tputs d/2+1\n\t\t(d/2).times{|i|\n\t\t\tprint \" \" if i>0\n\t\t\tprint \"2\"\n\t\t}\n\t\tprint \" 3\\n\"\n\tend\nend\n\n"}, {"source_code": "n=gets.to_i\nputs n/2\nputs \"2 \"*(n/2-1)+ (n%2==1 ? \"3\" : \"2\")\n\n"}, {"source_code": "n = gets.to_i\nif n%2 == 0\n print n/2, \"\\n\", \"2 \"*(n/2)\nelse\n print n/2, \"\\n\", \"2 \"*(n/2-1), \"3\"\nend\n"}, {"source_code": "n=gets.to_i\nif n==3\n puts \"1\\n3\"\nelsif n.even?\n puts n/2\n puts ([2]*(n/2))*' '\nelse\n puts n/2\n puts (([2]*(n/2-1))<<3)*' '\nend\n"}, {"source_code": "n=gets.to_i;puts n/2,2+n%2,[2]*(n/2-1)"}, {"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\n\nn = gets.to_i # n >= 2 and n <= 100_000\nwas_odd = n.odd?\nans_len = 0\nif was_odd\n ans_len += 1\n n -= 3\nend\n\nans_len += n / 2\n\nputs ans_len\nif was_odd\n print \"3 \"\nend\n(n/2).times {\n print \"2 \"\n}\nputs \"\""}, {"source_code": "class Memo\n attr_reader(:i)\n def initialize(i, state)\n @i =i\n @state = state\n end\n\n def yes!\n @state = true\n end\n\n def no!\n @state = false\n end\n\n def yes?\n @state\n end\n\n def to_s\n \"#{@i} => #{@state}\"\n end\nend\nclass Sieve\n attr_reader(:n)\n def initialize(n)\n @n = n\n @memo = Array.new(n + 100){|i| Memo.new(i, true)}\n @run = false\n end\n\n def run!\n return if @run\n @run = true\n @memo[0].yes!\n @memo[1].yes!\n @memo[2].yes!\n @memo.drop(2).each do |m|\n if(m.yes?)\n start = m.i * m.i\n start.step(@n + 1, m.i) do |i|\n @memo[i].no!\n end\n end\n end\n end\n\n def primes\n @primes ||= run!.select{|m| m.yes? }\n end\n\n def prime_values\n @prime_values ||= primes.map(&:i)\n end\n\n def get_data\n run!\n @memo\n end\nend\n\ndef main\n g = gets.strip.to_i\n n = g / 2\n puts n\n if(g % 2 == 0)\n puts Array.new(n){ 2 }.join(\" \")\n else\n x = Array.new(n - 1){ 2 }\n x << 3\n puts x.join(\" \")\n end\nend\n\nmain\n"}, {"source_code": "n = gets.chomp.to_i\n\nif n%2 == 0\n\tputs n/2\n\tprint \"2 \" * (n/2)\nelse\n\tif n == 3\n\t\tputs 1\n\t\tprint 3\n\telse\n\t\tputs n/2\n\t\tprint \"2 \" * ((n/2)-1)\n\t\tprint 3\n\tend\nend\n"}, {"source_code": "#!/usr/bin/env ruby\nn=gets.chomp.to_i\nif n&1==0 then\n puts \"#{n/2}\"\n (n/2).times do\n print \"2 \"\n end\nelse puts \"#{n/2 }\"\n print \"3 \"\n ((n/2)-1).times do\n print \"2 \"\n end\nend\n"}, {"source_code": "n = gets.to_i\nif n % 2 == 0 then\n\tputs n / 2\n\tputs \"2 \" * (n / 2 - 1) + \"2\"\nelse\n\tputs n / 2\n\tputs \"2 \" * (n / 2 - 1) + \"3\"\nend"}, {"source_code": "n = gets.to_i\nputs n / 2\nputs \"2 \" * (n / 2 - 1) + (n % 2 == 0 ? \"2\" : \"3\")"}, {"source_code": "n = gets.to_i\nputs(n/2)\nputs(n%2 == 0 ? \"2 \"*(n/2) : \"2 \"*((n-3)/2) + \"3\")\n"}, {"source_code": "n = gets.to_i\n\nresult = Array.new(n.div(2)-1, 2)\nresult << (n.even? ? 2 : 3)\n\nputs result.size\nputs result.join(' ')\n"}, {"source_code": "k = gets.chomp.to_i\nn = k / 2\nout = [\"2\"] * n \nif k.odd?\n\tout.pop\n\tout += [\"3\"] \nend\nputs out.size\nputs out.join(\" \")\n\n"}, {"source_code": "number = gets.strip.to_i\neven = false\n\neven = true if number % 2 == 0\n\nif even\n output = ''\n (number/2).times { output << '2 ' }\n puts number/2\n puts output.strip\nelse\n if number == 3\n puts 1\n puts number\n else\n balance = number - 3\n output = ''\n (balance/2).times { output << '2 ' }\n output << '3'\n puts(balance/2 + 1)\n puts output\n end\nend\n"}], "negative_code": [{"source_code": "n = gets.to_i\n\nif (n % 2 == 1)\n print 3, \" \"\n n -= 3\nend\n\n(0...n / 2).each do\n print 2, \" \"\nend"}, {"source_code": "given_number = gets.chomp.to_i\n\nk = given_number / 2\n\nmd = given_number % 2\nary = [2]\nif md == 1\n ary *= k - 1\n ary << 3\nelse\n ary *= k\nend\n\nputs ary.join(' ')\n"}, {"source_code": "n = gets.to_i\ncount3 = n%2\ncount2 = n/2 - count3\n(1..count2).each do\n print 2, ' '\nend\nprint count3==1? 3 : ''\n"}, {"source_code": "require 'prime'\nrequire 'set'\na=[\"\",\"\",\"1\\n2\",\"1\\n3\",\"2\\n2 2\",\"2\\n2 3\"]\nx=Prime.each(10**5).to_a\ny=SortedSet.new(x)\nn=gets.to_i\n\nif n<6 then\n\tputs a[n]\nelsif y.member?(n) then\n\tputs \"1\\n#{n}\"\nelse\n\tok=false\n\tx.size.times{|i|\n\t\ti.upto(x.size-1){|j|\n\t\t\tt=n-x[i]-x[j]\n\t\t\tbreak if t 0\r\n s += @data[r]\r\n r ^= r & -r\r\n end\r\n s\r\n end\r\n end\r\nend\r\n\r\ngets.to_i.times do\r\n \r\nn = gets.to_i\r\na = gets.split.map!(&:to_i)\r\nb = gets.split.map!(&:to_i)\r\n\r\nft = AtCoder::FenwickTree.new(n)\r\ns = Array.new(n)\r\na.each_with_index do |ai, i|\r\n ai >>= 1\r\n s[ai] = i - ft.sum(0, ai)\r\n ft.add(ai, 1)\r\nend\r\nx = s[0]; s.map! { |si| x = (x < si) ? x : si }\r\nt = b.map.with_index do |bj, j|\r\n bj >>= 1\r\n bj -= 1\r\n j + s[bj]\r\nend\r\n\r\nputs t.min\r\n\r\nend\r\n"}, {"source_code": "gets.to_i.times do\r\n n=gets.to_i\r\n a=gets.split.map(&:to_i).map{|x|x/2}\r\n b=gets.split.map(&:to_i).map{|x|x/2-1}\r\n l=[]\r\n i=0\r\n n.times do |j|\r\n while i<=b[j]\r\n l << j\r\n i+=1\r\n end\r\n end\r\n ans=2*n\r\n n.times do |j|\r\n if ans>j+l[a[j]]\r\n ans=j+l[a[j]]\r\n end\r\n end\r\n puts ans\r\nend\r\n"}], "negative_code": [{"source_code": "module AtCoder\r\n class FenwickTree\r\n def initialize(n = 0)\r\n @n = n\r\n @data = Array.new(n, 0).unshift(nil)\r\n end\r\n\r\n def add(idx, x)\r\n idx += 1\r\n while idx <= @n\r\n @data[idx] += x\r\n idx += idx & -idx\r\n end\r\n self\r\n end\r\n\r\n def sum(l, r)\r\n __sum__(r) - __sum__(l)\r\n end\r\n\r\n private\r\n\r\n def __sum__(r)\r\n s = 0\r\n while r > 0\r\n s += @data[r]\r\n r ^= r & -r\r\n end\r\n s\r\n end\r\n end\r\nend\r\n\r\ngets.to_i.times do\r\n \r\nn = gets.to_i\r\na = gets.split.map!(&:to_i)\r\nb = gets.split.map!(&:to_i)\r\n\r\nft = AtCoder::FenwickTree.new(n)\r\ns = Array.new(n)\r\na.each_with_index do |ai, i|\r\n ai >>= 1\r\n s[ai] = i - ft.sum(0, ai)\r\n ft.add(ai, 1)\r\nend\r\nt = b.map.with_index do |bj, j|\r\n bj >>= 1\r\n bj -= 1\r\n j + s[bj]\r\nend\r\n\r\nputs t.min\r\n\r\nend\r\n"}, {"source_code": "gets.to_i.times do\r\n\r\nn = gets.to_i\r\na = gets.split.map!(&:to_i)\r\nb = gets.split.map!(&:to_i)\r\n\r\nb0 = b[0]\r\nc = a.index { |ai| ai < b0 }\r\n\r\nputs c\r\n\r\nend\r\n"}], "src_uid": "55a1e9236cac9a6044e74b1975331535"} {"nl": {"description": "Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from 1 to n. The cost of the i-th stone is vi. Kuriyama Mirai wants to know something about these stones so she will ask you two kinds of questions: She will tell you two numbers, l and r (1 ≤ l ≤ r ≤ n), and you should tell her . Let ui be the cost of the i-th cheapest stone (the cost that will be on the i-th place if we arrange all the stone costs in non-decreasing order). This time she will tell you two numbers, l and r (1 ≤ l ≤ r ≤ n), and you should tell her . For every question you should give the correct answer, or Kuriyama Mirai will say \"fuyukai desu\" and then become unhappy.", "input_spec": "The first line contains an integer n (1 ≤ n ≤ 105). The second line contains n integers: v1, v2, ..., vn (1 ≤ vi ≤ 109) — costs of the stones. The third line contains an integer m (1 ≤ m ≤ 105) — the number of Kuriyama Mirai's questions. Then follow m lines, each line contains three integers type, l and r (1 ≤ l ≤ r ≤ n; 1 ≤ type ≤ 2), describing a question. If type equal to 1, then you should output the answer for the first question, else you should output the answer for the second one.", "output_spec": "Print m lines. Each line must contain an integer — the answer to Kuriyama Mirai's question. Print the answers to the questions in the order of input.", "sample_inputs": ["6\n6 4 2 7 2 7\n3\n2 3 6\n1 3 4\n1 1 6", "4\n5 5 2 3\n10\n1 2 4\n2 1 4\n1 1 1\n2 1 4\n2 1 2\n1 1 1\n1 3 3\n1 1 3\n1 4 4\n1 2 2"], "sample_outputs": ["24\n9\n28", "10\n15\n5\n15\n5\n5\n2\n12\n3\n5"], "notes": "NotePlease note that the answers to the questions may overflow 32-bit integer type."}, "positive_code": [{"source_code": "n = gets\nstone = gets.split.map(&:to_i)\nsorted = stone.sort\nfor i in 1...stone.length\n stone[i] += stone[i-1]\n sorted[i] += sorted[i-1]\nend\nstone << 0\nsorted << 0\nq = gets.to_i\nfor i in 1..q\n a, b, c = gets.split.map(&:to_i)\n b -= 2\n c -= 1\n sum = 0\n if a == 1\n sum = stone[c]-stone[b]\n else\n sum = sorted[c]-sorted[b]\n end\n puts sum\nend\n"}, {"source_code": "n=gets\nstone=gets.split.map &:to_i\nsorted = stone.sort\nfor i in 1...stone.length\n stone[i] += stone[i-1]\n sorted[i] += sorted[i-1]\nend\nstone << 0\nsorted << 0\nq=gets.to_i\nfor i in 1..q\n a,b,c = gets.split.map &:to_i\n b-=2\n c-=1\n sum=0\n if a==1\n sum=stone[c]-stone[b]\n else\n sum=sorted[c]-sorted[b]\n end\n puts sum\n\nend"}, {"source_code": "# coding: utf-8\n\n# if __FILE__ = $0\n# end\n\n# Kuriyama Mirai? <피림이: 이뭔씹...>\n\nn = gets.to_i\nv = gets.split.map(&:to_i) # stones\ntemp = 0\nsv = [0] + v.map { |i|\n temp += i\n temp\n}\nov = [0] + v.sort # ordered\ntemp = 0\nov = ov.map { |i|\n temp += i\n temp\n}\n#p sv\n#p ov\nquestions = gets.to_i\nquestions.times {\n type, l, r = gets.split.map(&:to_i)\n if type == 1\n puts sv[r] - sv[l-1]\n else\n puts ov[r] - ov[l-1]\n end\n}"}, {"source_code": "def solution(costs, questions)\n sorted_costs = costs.sort\n prefix_sorted_costs, prefix_costs = [0], [0]\n for i in 0...costs.size do\n prefix_sorted_costs << prefix_sorted_costs.last + sorted_costs[i]\n prefix_costs << prefix_costs.last + costs[i]\n end\n\n questions.each do |question|\n if question[0] == 1\n puts prefix_costs[question[2]] - prefix_costs[question[1]-1]\n else\n puts prefix_sorted_costs[question[2]] - prefix_sorted_costs[question[1]-1]\n end\n end\nend\n\n_ = gets\ncosts = gets.split.map(&:to_i)\nm = gets.to_i\nquestions = []\nm.times do \n questions << gets.split.map(&:to_i)\nend\n\nsolution(costs, questions)\n\n"}, {"source_code": "n = gets.to_i\nv = [0] + gets.split.collect{|i| i.to_i}\nu = v.sort\n(1..n).each do |i|\n v[i] += v[i-1]\n u[i] += u[i-1]\nend\nm = gets.to_i\n(0...m).each do\n type, l, r = gets.split.collect{|i| i.to_i}\n if type == 1\n puts v[r]-v[l-1]\n else\n puts u[r]-u[l-1]\n end\nend\n"}, {"source_code": "# 433B\n\ndef read_line\n gets.chomp.split.map &:to_i\nend\n\nn = gets.to_i\na = read_line\nb = a.sort\nsums = [nil, Array.new(n), Array.new(n)]\nsums[1][0] = a[0]\nsums[2][0] = b[0]\n(1...n).each do |i|\n sums[1][i] = sums[1][i - 1] + a[i]\n sums[2][i] = sums[2][i - 1] + b[i]\nend\ngets.to_i.times do\n type, l, r = read_line\n puts sums[type][r - 1] - (l > 1 ? sums[type][l - 2] : 0)\nend"}, {"source_code": "n = gets.to_i\nv = [0] + gets.split.collect{|i| i.to_i}\nu = v.sort\n(1..n).each do |i|\n\tv[i] += v[i - 1]\n\tu[i] += u[i - 1]\nend\nm = gets.to_i\n(0...m).each do\n\tt, left, right = gets.split.collect{|i| i.to_i}\n\tif 1 == t \n\t\tputs v[right]-v[left - 1]\n\telse \n\t\tputs u[right]-u[left - 1]\n\tend\nend\n"}], "negative_code": [], "src_uid": "c764b9f87cb5e5872eb157c3d2b7f3c5"} {"nl": {"description": "Given an array $$$a$$$ of length $$$n$$$, tell us whether it has a non-empty subsequence such that the product of its elements is not a perfect square.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly zero) elements.", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \\le n \\le 100$$$) — the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, $$$\\ldots$$$, $$$a_{n}$$$ ($$$1 \\le a_i \\le 10^4$$$) — the elements of the array $$$a$$$.", "output_spec": "If there's a subsequence of $$$a$$$ whose product isn't a perfect square, print \"YES\". Otherwise, print \"NO\".", "sample_inputs": ["2\n3\n1 5 4\n2\n100 10000"], "sample_outputs": ["YES\nNO"], "notes": "NoteIn the first example, the product of the whole array ($$$20$$$) isn't a perfect square.In the second example, all subsequences have a perfect square product."}, "positive_code": [{"source_code": "t = gets.to_s.to_i\n1.upto(t) do |casenum|\n n = gets.to_s.to_i\n a = gets.to_s.split.map {|x| x.to_i }\n imperfect = false\n a.each do |x|\n tmp = Integer.sqrt(x)\n if tmp * tmp != x\n imperfect = true\n break\n end\n end\n printf(\"%s\\n\", imperfect ? \"YES\" : \"NO\")\nend\n\n"}], "negative_code": [], "src_uid": "33a31edb75c9b0cf3a49ba97ad677632"} {"nl": {"description": "You are given the following concurrent program. There are N processes and the i-th process has the following pseudocode: repeat ni times yi := y y := yi + 1end repeatHere y is a shared variable. Everything else is local for the process. All actions on a given row are atomic, i.e. when the process starts executing a row it is never interrupted. Beyond that all interleavings are possible, i.e. every process that has yet work to do can be granted the rights to execute its next row. In the beginning y = 0. You will be given an integer W and ni, for i = 1, ... , N. Determine if it is possible that after all processes terminate, y = W, and if it is possible output an arbitrary schedule that will produce this final value.", "input_spec": "In the first line of the input you will be given two space separated integers N (1 ≤ N ≤ 100) and W ( - 109 ≤ W ≤ 109). In the second line there are N space separated integers ni (1 ≤ ni ≤ 1000).", "output_spec": "On the first line of the output write Yes if it is possible that at the end y = W, or No otherwise. If the answer is No then there is no second line, but if the answer is Yes, then on the second line output a space separated list of integers representing some schedule that leads to the desired result. For more information see note.", "sample_inputs": ["1 10\n11", "2 3\n4 4", "3 6\n1 2 3"], "sample_outputs": ["No", "Yes\n1 1 2 1 2 2 2 2 2 1 2 1 1 1 1 2", "Yes\n1 1 2 2 2 2 3 3 3 3 3 3"], "notes": "NoteFor simplicity, assume that there is no repeat statement in the code of the processes, but the code from the loop is written the correct amount of times. The processes are numbered starting from 1. The list of integers represent which process works on its next instruction at a given step. For example, consider the schedule 1 2 2 1 3. First process 1 executes its first instruction, then process 2 executes its first two instructions, after that process 1 executes its second instruction, and finally process 3 executes its first instruction. The list must consists of exactly 2·Σ i = 1...N ni numbers."}, "positive_code": [{"source_code": "# solve for the case that a_min <= w <= sum(a_i)\ndef solve_normal_case(a, w)\n result = []\n w -= a[0][0]\n (1..a.size-1).each do |i|\n break if w == 0\n if w > a[i][0]\n (2 * a[i][0]).times {result.push(a[i][1])}\n w -= a[i][0]\n a[i][0] = 0\n else\n (2 * w).times {result.push(a[i][1])}\n a[i][0] -= w\n w = 0\n end\n end\n (2*a[0][0]-1).times {result.unshift(a[0][1])}\n (1..a.size-1).each do |i|\n (2*a[i][0]).times {result.unshift(a[i][1])}\n end\n result.unshift(a[0][1])\nend\n\n\nn, w = gets.split.map(&:to_i)\na = gets.split.map(&:to_i).map.with_index(1).to_a\na.sort_by!{|x, i| x}\nsum = a.inject(0) {|s, item| s + item[0]}\nif w <= 0 or w > sum\n puts \"No\"\n exit\nend\n\nresult = []\nif a[0][0] <= w and w <= sum\n result = solve_normal_case(a, w)\nelsif w >= 2 and a.size >= 2\n suffix = []\n suffix.push(a[1][1])\n diff = a[0][0] - w + 1\n (2*diff).times {suffix.unshift(a[0][1])}\n suffix.unshift(a[1][1])\n a[0][0] -= diff\n a[1][0] -= 1\n prefix = solve_normal_case(a, w-1)\n result = prefix + suffix\nelse\n puts \"No\"\n exit\nend\n\nputs \"Yes\"\nputs result.join(' ')\n\n"}], "negative_code": [{"source_code": "# solve for the case that a_min <= w <= sum(a_i)\ndef solve_normal_case(a, w)\n result = []\n w -= a[0][0]\n (1..a.size-1).each do |i|\n break if w == 0\n if w > a[i][0]\n (2 * a[i][0]).times {result.push(a[i][1])}\n w -= a[i][0]\n a[i][0] = 0\n else\n (2 * w).times {result.push(a[i][1])}\n a[i][0] -= w\n w = 0\n end\n end\n (2*a[0][0]-1).times {result.unshift(a[0][1])}\n (1..a.size-1).each do |i|\n (2*a[i][0]).times {result.unshift(a[i][1])}\n end\n result.unshift(a[0][1])\nend\n\n\nn, w = gets.split.map(&:to_i)\na = gets.split.map(&:to_i).map.with_index(1).to_a\na.sort_by{|x, i| x}\nsum = a.inject(0) {|s, item| s + item[0]}\nif w <= 0 or w > sum\n puts \"No\"\n exit\nend\n\nresult = []\nif a[0][0] <= w and w <= sum\n result = solve_normal_case(a, w)\nelsif w >= 2 and a.size >= 2\n suffix = []\n suffix.push(a[1][1])\n diff = a[0][0] - w + 1\n (2*diff).times {suffix.unshift(a[0][1])}\n suffix.unshift(a[1][1])\n a[0][0] -= diff\n a[1][0] -= 1\n prefix = solve_normal_case(a, w-1)\n result = prefix + suffix\nelse\n puts \"No\"\n exit\nend\n\nputs \"Yes\"\nputs result.join(' ')\n\n"}, {"source_code": "# solve for the case that a_min <= w <= sum(a_i)\ndef solve_normal_case(a, w)\n result = []\n w -= a[0][0]\n (1..a.size-1).each do |i|\n break if w == 0\n if w > a[i][0]\n (2 * a[i][0]).times {result.push(a[i][1])}\n w -= a[i][0]\n a[i][0] = 0\n else\n (2 * w).times {result.push(a[i][1])}\n a[i][0] -= w\n w = 0\n end\n end\n (2*a[0][0]-1).times {result.unshift(a[0][1])}\n (1..a.size-1).each do |i|\n (2*a[i][0]).times {result.unshift(a[i][1])}\n end\n result.unshift(a[0][1])\nend\n\n\nn, w = gets.split.map(&:to_i)\na = gets.split.map(&:to_i).map.with_index(1).to_a\na.sort_by!{|x, i| x}\nsum = a.inject(0) {|s, item| s + item[0]}\nif w <= 0 or w > sum\n puts \"No\"\n exit\nend\n\nprintf(\"a0=%d w=%d\\n\", a[0][0], w)\nresult = []\nif a[0][0] <= w and w <= sum\n result = solve_normal_case(a, w)\nelsif w >= 2 and a.size >= 2\n suffix = []\n suffix.push(a[1][1])\n diff = a[0][0] - w + 1\n (2*diff).times {suffix.unshift(a[0][1])}\n suffix.unshift(a[1][1])\n a[0][0] -= diff\n a[1][0] -= 1\n prefix = solve_normal_case(a, w-1)\n result = prefix + suffix\nelse\n puts \"No\"\n exit\nend\n\nputs \"Yes\"\nputs result.join(' ')\n\n"}], "src_uid": "bf6e2310e4f808ca4c8ff28ba8a51a68"} {"nl": {"description": "Let's call a positive integer composite if it has at least one divisor other than $$$1$$$ and itself. For example: the following numbers are composite: $$$1024$$$, $$$4$$$, $$$6$$$, $$$9$$$; the following numbers are not composite: $$$13$$$, $$$1$$$, $$$2$$$, $$$3$$$, $$$37$$$. You are given a positive integer $$$n$$$. Find two composite integers $$$a,b$$$ such that $$$a-b=n$$$.It can be proven that solution always exists.", "input_spec": "The input contains one integer $$$n$$$ ($$$1 \\leq n \\leq 10^7$$$): the given integer.", "output_spec": "Print two composite integers $$$a,b$$$ ($$$2 \\leq a, b \\leq 10^9, a-b=n$$$). It can be proven, that solution always exists. If there are several possible solutions, you can print any. ", "sample_inputs": ["1", "512"], "sample_outputs": ["9 8", "4608 4096"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\n\nif n == 1\n puts \"9 8\"\nelse\n puts \"#{n*2+n} #{n*2}\"\nend\n"}], "negative_code": [{"source_code": "n = gets.to_i\n\nif n == 1\n puts \"9 8\"\nelse\n puts \"#{n*n+n} #{n*n}\"\nend\n"}], "src_uid": "59d5e5ed2bc4b316e950e2a4dbc99d68"} {"nl": {"description": "Let's call an array good if there is an element in the array that equals to the sum of all other elements. For example, the array $$$a=[1, 3, 3, 7]$$$ is good because there is the element $$$a_4=7$$$ which equals to the sum $$$1 + 3 + 3$$$.You are given an array $$$a$$$ consisting of $$$n$$$ integers. Your task is to print all indices $$$j$$$ of this array such that after removing the $$$j$$$-th element from the array it will be good (let's call such indices nice).For example, if $$$a=[8, 3, 5, 2]$$$, the nice indices are $$$1$$$ and $$$4$$$: if you remove $$$a_1$$$, the array will look like $$$[3, 5, 2]$$$ and it is good; if you remove $$$a_4$$$, the array will look like $$$[8, 3, 5]$$$ and it is good. You have to consider all removals independently, i. e. remove the element, check if the resulting array is good, and return the element into the array.", "input_spec": "The first line of the input contains one integer $$$n$$$ ($$$2 \\le n \\le 2 \\cdot 10^5$$$) — the number of elements in the array $$$a$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^6$$$) — elements of the array $$$a$$$.", "output_spec": "In the first line print one integer $$$k$$$ — the number of indices $$$j$$$ of the array $$$a$$$ such that after removing the $$$j$$$-th element from the array it will be good (i.e. print the number of the nice indices). In the second line print $$$k$$$ distinct integers $$$j_1, j_2, \\dots, j_k$$$ in any order — nice indices of the array $$$a$$$. If there are no such indices in the array $$$a$$$, just print $$$0$$$ in the first line and leave the second line empty or do not print it at all.", "sample_inputs": ["5\n2 5 1 2 2", "4\n8 3 5 2", "5\n2 1 2 4 3"], "sample_outputs": ["3\n4 1 5", "2\n1 4", "0"], "notes": "NoteIn the first example you can remove any element with the value $$$2$$$ so the array will look like $$$[5, 1, 2, 2]$$$. The sum of this array is $$$10$$$ and there is an element equals to the sum of remaining elements ($$$5 = 1 + 2 + 2$$$).In the second example you can remove $$$8$$$ so the array will look like $$$[3, 5, 2]$$$. The sum of this array is $$$10$$$ and there is an element equals to the sum of remaining elements ($$$5 = 3 + 2$$$). You can also remove $$$2$$$ so the array will look like $$$[8, 3, 5]$$$. The sum of this array is $$$16$$$ and there is an element equals to the sum of remaining elements ($$$8 = 3 + 5$$$).In the third example you cannot make the given array good by removing exactly one element."}, "positive_code": [{"source_code": "\t\n\nn = gets.chomp.to_i\n\narr = gets.split(\" \")\n\narr = arr.map { |element| element.to_i }\n\nsum = arr.inject(0){|sum,x| sum + x }\n\nsorted = arr.sort.reverse\n\nanswer = []\n\narr.each_with_index do |element,i|\n\n\tif element == sorted[0]\n\t\tmx = sorted[1]\n\telse\n\t\tmx = sorted[0]\n\tend\n\tanswer<<(i+1) if sum - mx - element == mx\n\nend\n\n\n\nputs answer.length\nputs answer.join(' ')"}, {"source_code": "n = gets.strip.to_i\na = gets.strip.split.map(&:to_i)\nsum = a.inject(:+)\nsorted = a.sort.reverse\nnice = []\na.each_with_index do |e, i|\n mx = e == sorted.first ? sorted[1] : sorted.first\n nice << (i + 1) if (sum - e) - mx == mx\nend\nputs nice.size\nputs nice.join(' ')"}], "negative_code": [{"source_code": "\t\nn = gets.chomp.to_i\n\narr = gets.split(\" \")\narr = arr.map { |element| element.to_i }\n\nsum = arr.inject(0){|sum,x| sum + x }\nmax = arr.max\nnums = []\n\ntemp = arr.uniq\ntemp.each_with_index do |element,i| \n\tnums<<[sum-2*element,i]\nend\n\n\nnums.delete_if { |x| x[0] > max || x[0] < 1 }\nnums = nums.uniq\nanswer = []\n\n\nnums.each do |element|\n\ttemp_arr = arr.each_index.select { |i| arr[i] == element[0] && i!=element[1] }.compact\n\tanswer.concat(temp_arr)\nend\n\nanswer = answer.uniq\n\nputs answer.length\nanswer.each { |x| print (x+1).to_s+\" \"}"}, {"source_code": "n = gets.chomp.to_i\n\narr = gets.split(\" \")\narr = arr.map { |element| element.to_i }\nsum = arr.inject(0){|sum,x| sum + x }\nmax = arr.max\nnums = []\narr.each do |element| \n\tnums< max || x < 1 }\nnums = nums.uniq\nanswer = []\nnums.each do |element|\n\ttemp_arr = arr.each_index.select { |i| arr[i] == element}.compact\n\tanswer.concat(temp_arr)\nend\n\nanswer = answer.uniq\n\n\nputs answer.length\nanswer.each { |x| print (x+1).to_s+\" \"}"}], "src_uid": "4cf0fe49f7ebf058317ac848043031a5"} {"nl": {"description": "You've got a undirected tree s, consisting of n nodes. Your task is to build an optimal T-decomposition for it. Let's define a T-decomposition as follows.Let's denote the set of all nodes s as v. Let's consider an undirected tree t, whose nodes are some non-empty subsets of v, we'll call them xi . The tree t is a T-decomposition of s, if the following conditions holds: the union of all xi equals v; for any edge (a, b) of tree s exists the tree node t, containing both a and b; if the nodes of the tree t xi and xj contain the node a of the tree s, then all nodes of the tree t, lying on the path from xi to xj also contain node a. So this condition is equivalent to the following: all nodes of the tree t, that contain node a of the tree s, form a connected subtree of tree t. There are obviously many distinct trees t, that are T-decompositions of the tree s. For example, a T-decomposition is a tree that consists of a single node, equal to set v.Let's define the cardinality of node xi as the number of nodes in tree s, containing in the node. Let's choose the node with the maximum cardinality in t. Let's assume that its cardinality equals w. Then the weight of T-decomposition t is value w. The optimal T-decomposition is the one with the minimum weight.Your task is to find the optimal T-decomposition of the given tree s that has the minimum number of nodes.", "input_spec": "The first line contains a single integer n (2 ≤ n ≤ 105), that denotes the number of nodes in tree s. Each of the following n - 1 lines contains two space-separated integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi), denoting that the nodes of tree s with indices ai and bi are connected by an edge. Consider the nodes of tree s indexed from 1 to n. It is guaranteed that s is a tree.", "output_spec": "In the first line print a single integer m that denotes the number of nodes in the required T-decomposition. Then print m lines, containing descriptions of the T-decomposition nodes. In the i-th (1 ≤ i ≤ m) of them print the description of node xi of the T-decomposition. The description of each node xi should start from an integer ki, that represents the number of nodes of the initial tree s, that are contained in the node xi. Then you should print ki distinct space-separated integers — the numbers of nodes from s, contained in xi, in arbitrary order. Then print m - 1 lines, each consisting two integers pi, qi (1 ≤ pi, qi ≤ m; pi ≠ qi). The pair of integers pi, qi means there is an edge between nodes xpi and xqi of T-decomposition. The printed T-decomposition should be the optimal T-decomposition for the given tree s and have the minimum possible number of nodes among all optimal T-decompositions. If there are multiple optimal T-decompositions with the minimum number of nodes, print any of them.", "sample_inputs": ["2\n1 2", "3\n1 2\n2 3", "4\n2 1\n3 1\n4 1"], "sample_outputs": ["1\n2 1 2", "2\n2 1 2\n2 2 3\n1 2", "3\n2 2 1\n2 3 1\n2 4 1\n1 2\n2 3"], "notes": null}, "positive_code": [{"source_code": "n=gets.to_i\np n-1\nc=[0]*100020\nz=[]\n1.upto(n-1){|i|\n\tx,y=gets.split.map &:to_i\n\tprint \"2 \"+$_\n\tif c[x]==0\n\t\tc[x]=i\n\telse\n\t\tz<<[i,c[x]]*' '\n\tend\n\tif c[y]==0\n\t\tc[y]=i\n\telse\n\t\tz<<[i,c[y]]*' '\n\tend\n}\nputs z*\"\\n\"\n"}, {"source_code": "n=gets.to_i\np n-1\nc=[0]*100020\nz=[]\n1.upto(n-1){|i|\n\tx,y=gets.split.map &:to_i\n\tprint \"2 \"+$_\n\tif c[x]==0\n\t\tc[x]=i\n\telse\n\t\tz<<[i,c[x]]*' '\n\tend\n\tif c[y]==0\n\t\tc[y]=i\n\telse\n\t\tz<<[i,c[y]]*' '\n\tend\n}\nputs z*\"\\n\"\n"}, {"source_code": "n=gets.to_i\np n-1\nc=[0]*100020\nz=[]\n1.upto(n-1){|i|\n\tx,y=gets.split.map &:to_i\n\tprint \"2 \"+$_\n\tif c[x]==0\n\t\tc[x]=i\n\telse\n\t\tz<<[i,c[x]]*' '\n\tend\n\tif c[y]==0\n\t\tc[y]=i\n\telse\n\t\tz<<[i,c[y]]*' '\n\tend\n}\nputs z*\"\\n\"\n"}, {"source_code": "n=gets.to_i\np n-1\nc=[0]*100020\nz=[]\n1.upto(n-1){|i|\n\tx,y=gets.split.map &:to_i\n\tprint \"2 \"+$_\n\tif c[x]==0\n\t\tc[x]=i\n\telse\n\t\tz<<[i,c[x]]*' '\n\tend\n\tif c[y]==0\n\t\tc[y]=i\n\telse\n\t\tz<<[i,c[y]]*' '\n\tend\n}\nputs z*\"\\n\"\n"}, {"source_code": "n=gets.to_i\np n-1\nc=[0]*100020\nz=[]\n1.upto(n-1){|i|\n\tx,y=gets.split.map &:to_i\n\tprint \"2 \"+$_\n\tif c[x]==0\n\t\tc[x]=i\n\telse\n\t\tz<<[i,c[x]]*' '\n\tend\n\tif c[y]==0\n\t\tc[y]=i\n\telse\n\t\tz<<[i,c[y]]*' '\n\tend\n}\nputs z*\"\\n\""}, {"source_code": "n=gets.to_i\np n-1\nc=[0]*100020\nz=[]\n1.upto(n-1){|i|\n\tx,y=gets.split.map &:to_i\n\tprint \"2 \"+$_\n\tif c[x]==0\n\t\tc[x]=i\n\telse\n\t\tz<<[i,c[x]]*' '\n\tend\n\tif c[y]==0\n\t\tc[y]=i\n\telse\n\t\tz<<[i,c[y]]*' '\n\tend\n}\nputs z*\"\\n\"\n"}, {"source_code": "n=gets.to_i\np n-1\nc=[0]*100020\nz=[]\n1.upto(n-1){|i|\n\tx,y=gets.split.map &:to_i\n\tprint \"2 \"+$_\n\tif c[x]==0\n\t\tc[x]=i\n\telse\n\t\tz<<[i,c[x]]*' '\n\tend\n\tif c[y]==0\n\t\tc[y]=i\n\telse\n\t\tz<<[i,c[y]]*' '\n\tend\n}\nputs z*\"\\n\"\n"}, {"source_code": "n=gets.to_i\np n-1\nc=[0]*100020\nz=[]\n1.upto(n-1){|i|\n\tx,y=gets.split.map &:to_i\n\tprint \"2 \"+$_\n\tif c[x]==0\n\t\tc[x]=i\n\telse\n\t\tz<<[i,c[x]]*' '\n\tend\n\tif c[y]==0\n\t\tc[y]=i\n\telse\n\t\tz<<[i,c[y]]*' '\n\tend\n}\nputs z*\"\\n\"\n"}, {"source_code": "n=gets.to_i\np n-1\nc=[0]*100020\nz=[]\n1.upto(n-1){|i|\n\tx,y=gets.split.map &:to_i\n\tprint \"2 \"+$_\n\tif c[x]==0\n\t\tc[x]=i\n\telse\n\t\tz<<[i,c[x]]*' '\n\tend\n\tif c[y]==0\n\t\tc[y]=i\n\telse\n\t\tz<<[i,c[y]]*' '\n\tend\n}\nputs z*\"\\n\"\n"}], "negative_code": [], "src_uid": "79074958bd8017988308ff4e37bca163"} {"nl": {"description": "Cat Furrier Transform is a popular algorithm among cat programmers to create longcats. As one of the greatest cat programmers ever exist, Neko wants to utilize this algorithm to create the perfect longcat.Assume that we have a cat with a number $$$x$$$. A perfect longcat is a cat with a number equal $$$2^m - 1$$$ for some non-negative integer $$$m$$$. For example, the numbers $$$0$$$, $$$1$$$, $$$3$$$, $$$7$$$, $$$15$$$ and so on are suitable for the perfect longcats.In the Cat Furrier Transform, the following operations can be performed on $$$x$$$: (Operation A): you select any non-negative integer $$$n$$$ and replace $$$x$$$ with $$$x \\oplus (2^n - 1)$$$, with $$$\\oplus$$$ being a bitwise XOR operator. (Operation B): replace $$$x$$$ with $$$x + 1$$$. The first applied operation must be of type A, the second of type B, the third of type A again, and so on. Formally, if we number operations from one in the order they are executed, then odd-numbered operations must be of type A and the even-numbered operations must be of type B.Neko wants to produce perfect longcats at industrial scale, thus for each cat Neko only wants to perform at most $$$40$$$ operations. Can you help Neko writing a transformation plan?Note that it is not required to minimize the number of operations. You just need to use no more than $$$40$$$ operations.", "input_spec": "The only line contains a single integer $$$x$$$ ($$$1 \\le x \\le 10^6$$$).", "output_spec": "The first line should contain a single integer $$$t$$$ ($$$0 \\le t \\le 40$$$) — the number of operations to apply. Then for each odd-numbered operation print the corresponding number $$$n_i$$$ in it. That is, print $$$\\lceil \\frac{t}{2} \\rceil$$$ integers $$$n_i$$$ ($$$0 \\le n_i \\le 30$$$), denoting the replacement $$$x$$$ with $$$x \\oplus (2^{n_i} - 1)$$$ in the corresponding step. If there are multiple possible answers, you can print any of them. It is possible to show, that there is at least one answer in the constraints of this problem.", "sample_inputs": ["39", "1", "7"], "sample_outputs": ["4\n5 3", "0", "0"], "notes": "NoteIn the first test, one of the transforms might be as follows: $$$39 \\to 56 \\to 57 \\to 62 \\to 63$$$. Or more precisely: Pick $$$n = 5$$$. $$$x$$$ is transformed into $$$39 \\oplus 31$$$, or $$$56$$$. Increase $$$x$$$ by $$$1$$$, changing its value to $$$57$$$. Pick $$$n = 3$$$. $$$x$$$ is transformed into $$$57 \\oplus 7$$$, or $$$62$$$. Increase $$$x$$$ by $$$1$$$, changing its value to $$$63 = 2^6 - 1$$$. In the second and third test, the number already satisfies the goal requirement."}, "positive_code": [{"source_code": "X = gets.to_i\n\ndef check(x)\n ans = []\n\n if x[0] == 0\n x += 1\n ans << 0\n end\n\n loop do\n flg = false\n l = x.to_s(2).size\n ml = 0\n\n (0...l).each do |i|\n if x[i] == 0\n ml = i + 1\n flg = true\n end\n end\n\n if flg\n mask = ('1' * ml).to_i(2)\n ans << ml\n\n x ^= mask\n x += 1\n else\n break\n end\n end\n\n ans\nend\n\nans = check(X)\n\nif ans.empty?\n puts 0\nelse\n puts ans.size * 2\n puts ans.join(' ')\nend\n"}, {"source_code": "def check(x,n,a,b)\n f = true\n for i in 0..n\n f = false if x[i] == 0\n end\n if f\n if b.empty?\n puts 0\n else\n puts a\n puts b.join(\" \")\n end\n exit\n end\nend\n\nx = gets.to_i\na = 0\nb = []\nn = Math.log2(x).floor\nwhile 1\n check(x,n,a,b)\n a += 1\n n.downto(0) do |i|\n if x[i] == 0\n x ^= ((1<<(i+1))-1)\n b << i+1\n check(x,n,a,b)\n break\n end\n end\n a += 1\n x += 1\n check(x,n,a,b)\nend"}], "negative_code": [], "src_uid": "2510469c09d7d145078e65de81640ddc"} {"nl": {"description": "Polycarp is writing the prototype of a graphic editor. He has already made up his mind that the basic image transformations in his editor will be: rotate the image 90 degrees clockwise, flip the image horizontally (symmetry relative to the vertical line, that is, the right part of the image moves to the left, and vice versa) and zooming on the image. He is sure that that there is a large number of transformations that can be expressed through these three.He has recently stopped implementing all three transformations for monochrome images. To test this feature, he asked you to write a code that will consecutively perform three actions with a monochrome image: first it will rotate the image 90 degrees clockwise, then it will flip the image horizontally and finally, it will zoom in twice on the image (that is, it will double all the linear sizes).Implement this feature to help Polycarp test his editor.", "input_spec": "The first line contains two integers, w and h (1 ≤ w, h ≤ 100) — the width and height of an image in pixels. The picture is given in h lines, each line contains w characters — each character encodes the color of the corresponding pixel of the image. The line consists only of characters \".\" and \"*\", as the image is monochrome.", "output_spec": "Print 2w lines, each containing 2h characters — the result of consecutive implementing of the three transformations, described above.", "sample_inputs": ["3 2\n.*.\n.*.", "9 20\n**.......\n****.....\n******...\n*******..\n..******.\n....****.\n......***\n*.....***\n*********\n*********\n*********\n*********\n....**...\n...****..\n..******.\n.********\n****..***\n***...***\n**.....**\n*.......*"], "sample_outputs": ["....\n....\n****\n****\n....\n....", "********......**********........********\n********......**********........********\n********........********......********..\n********........********......********..\n..********......********....********....\n..********......********....********....\n..********......********..********......\n..********......********..********......\n....********....****************........\n....********....****************........\n....********....****************........\n....********....****************........\n......******************..**********....\n......******************..**********....\n........****************....**********..\n........****************....**********..\n............************......**********\n............************......**********"], "notes": null}, "positive_code": [{"source_code": "w,h = gets.strip.split(\" \").map(&:to_i)\n\nmatrix = Array.new\n\nh.times do |k|\n matrix[k] = gets.strip.split(\"\")\nend\n\nmatrix = matrix.transpose\n\n(2*w).times do |i|\n m = (i/2.0).floor\n (2*h).times do |j|\n k = (j/2.0).floor\n print matrix[m][k]\n end\n print \"\\n\"\nend"}, {"source_code": "(w, h) = gets.scan(/\\d+/).map(&:to_i)\ns = []\nh.times do\n s << gets\nend\nw.times do |i|\n 2.times do\n h.times do |j|\n print s[j][i], s[j][i]\n end\n print \"\\n\"\n end\nend"}, {"source_code": "_, h = gets.split.map(&:to_i)\na = []\nh.times do\n a << gets.chomp.split(//)\nend\na = a.transpose.map { |x| x.zip(x).flatten }\nputs a.zip(a).flatten(1).map(&:join)\n"}, {"source_code": "a,b = gets.split.map{|x| x.to_i}\nc = []\nb.times do |x|\n c[x] = gets.chomp.split('')\nend\n\nfor i in 0...2*a\n for j in 0...2*b\n print(\"#{c[j/2][i/2]}\")\n end\n puts\nend\n"}, {"source_code": "# http://codeforces.ru/contest/523/problem/A\n\ndef img_to_s image\n image.map(&:join).join(\"\\n\")\nend\n\ninitial = ARGF.read.split(\"\\n\")\n\nimage = initial[1..-1].map(&:chars)\n\n# flip 90 degrees\n\nimage = image.transpose\n\n# mirror\n\nimage = image.map { |i| i.map(&:reverse) }\n\n# scaling\n\nimage = image.reduce([]) do |res, r|\n scaled = r.reduce([]) { |i_res, c| i_res << c; i_res << c; i_res }\n res << scaled\n res << scaled\nend\n\nputs img_to_s(image)"}], "negative_code": [{"source_code": "initial_num = ARGF.read\n\nresult = ''\n\ninitial_num.chars.each_with_index do |c, index|\n\ti = c.to_i\n\tif i >= 5\n\t\tif i == 9 and index == 0\n\t\t\tresult << c\n\t\telse\n\t\t\tresult << (9-i).to_s\n\t\tend\n\telse\n\t\tresult << c\n\tend\nend\n\nputs result"}], "src_uid": "9af19e1b482f7f0bcbffc754cf324092"} {"nl": {"description": "Let's denote a function You are given an array a consisting of n integers. You have to calculate the sum of d(ai, aj) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.", "input_spec": "The first line contains one integer n (1 ≤ n ≤ 200000) — the number of elements in a. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — elements of the array. ", "output_spec": "Print one integer — the sum of d(ai, aj) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.", "sample_inputs": ["5\n1 2 3 1 3", "4\n6 6 5 5", "4\n6 6 4 4"], "sample_outputs": ["4", "0", "-8"], "notes": "NoteIn the first example: d(a1, a2) = 0; d(a1, a3) = 2; d(a1, a4) = 0; d(a1, a5) = 2; d(a2, a3) = 0; d(a2, a4) = 0; d(a2, a5) = 0; d(a3, a4) =  - 2; d(a3, a5) = 0; d(a4, a5) = 2. "}, "positive_code": [{"source_code": "#!/usr/bin/env ruby\n# encoding: utf-8\n\nn = gets.to_i\nsum = 0\nans = 0\nary = gets.split.map(&:to_i)\nhash = {}\nary.each do |v|\n sum += v\n hash[v] = hash[v].nil? ? 1 : hash[v] + 1\nend\nary.each do |v|\n sum -= v\n n -= 1\n ans += (sum - n * v)\n hash[v] -= 1\n ans += hash[v - 1] unless hash[v - 1].nil?\n ans -= hash[v + 1] unless hash[v + 1].nil?\nend\nputs ans\n"}, {"source_code": "# cook your code here\n\nn = gets.to_i \n\nnums = gets.split.map{ |x| x.to_i } \n\nnumap = Hash.new(0) \nsum = 0\n\nfor i in 0...n \n sum += nums[i]\n numap[nums[i]] += 1 \nend \n\nminsum=0 \ngreatsum=0 \n=begin\nfor i in 0...n \n puts i\n #puts nums[i] \n puts numap[nums[i]]\nend \n=end \nfor i in 0...n \n sum -= nums[i] \n minsum = sum - (n-1-i)*nums[i] - numap[nums[i]+1] + numap[nums[i]-1] \n greatsum += minsum \n numap[nums[i]] -= 1 \n # puts (i+1).to_s + \" \" + nums[i].to_s + \" \" + sum.to_s + \" \" + minsum.to_s + \" \" + greatsum.to_s \nend \n\nputs greatsum"}, {"source_code": "N = gets.to_i\nA = gets.split.map(&:to_i)\n\nsum, ans = 0, 0\nmp = Hash.new(0)\nN.times do |i|\n res = sum\n num = i\n ((A[i]-1)..(A[i]+1)).each do |v|\n res -= v*mp[v]\n num -= mp[v]\n end\n ans += num*A[i] - res\n sum += A[i]\n mp[A[i]] += 1\nend\nputs ans"}, {"source_code": "N = $stdin.gets.chomp.to_i\narr = $stdin.gets.chomp.split(\" \").map{ |a| a.to_i }.reverse\nnod = {}\n#p arr\n\nans = 0\ns = 0\narr.each_with_index do |a, i|\n ans += (s - (a*i)) - ((nod[a-1] || 0) * (a-1 - a) + (nod[a+1] || 0) * (a+1 - a))\n s += a\n nod[a] = (nod[a] || 0) + 1\nend\nputs ans\n"}, {"source_code": "n, c, s, r = gets, Hash.new(0), 0, 0\ngets.split.map(&:to_i).each_with_index {|a, i|\n r += a * i - s - c[a - 1] + c[a + 1]\n c[a] += 1\n s += a\n}\np r"}], "negative_code": [], "src_uid": "c7cca8c6524991da6ea1b423a8182d24"} {"nl": {"description": "There is a string $$$s$$$ of length $$$3$$$, consisting of uppercase and lowercase English letters. Check if it is equal to \"YES\" (without quotes), where each letter can be in any case. For example, \"yES\", \"Yes\", \"yes\" are all allowable.", "input_spec": "The first line of the input contains an integer $$$t$$$ ($$$1 \\leq t \\leq 10^3$$$) — the number of testcases. The description of each test consists of one line containing one string $$$s$$$ consisting of three characters. Each character of $$$s$$$ is either an uppercase or lowercase English letter.", "output_spec": "For each test case, output \"YES\" (without quotes) if $$$s$$$ satisfies the condition, and \"NO\" (without quotes) otherwise. You can output \"YES\" and \"NO\" in any case (for example, strings \"yES\", \"yes\" and \"Yes\" will be recognized as a positive response).", "sample_inputs": ["10\n\nYES\n\nyES\n\nyes\n\nYes\n\nYeS\n\nNoo\n\norZ\n\nyEz\n\nYas\n\nXES"], "sample_outputs": ["YES\nYES\nYES\nYES\nYES\nNO\nNO\nNO\nNO\nNO"], "notes": "NoteThe first five test cases contain the strings \"YES\", \"yES\", \"yes\", \"Yes\", \"YeS\". All of these are equal to \"YES\", where each character is either uppercase or lowercase."}, "positive_code": [{"source_code": "t = gets.to_i\n\nt.times do\n ln = gets.chomp\n if ln.downcase == \"yes\"\n puts \"YES\"\n else\n puts \"NO\"\n end\nend\n"}, {"source_code": " gets.to_i.times { puts gets.chomp.upcase == \"YES\" ? \"YES\" : \"NO\" } "}, {"source_code": "max_iterates = gets.to_i\nresult = []\nfor i in 1..max_iterates do\n val = gets.chomp.downcase == \"yes\" ? \"YES\" : \"NO\"\n result << val\nend\n\nresult.each { |str| puts str }\n\n\n"}, {"source_code": "#!/usr/bin/env ruby\n\nnumtests=gets.to_i\ni=0\n(1..numtests).each do |i|\n l=gets.chomp\n if !l.nil? && l.downcase == \"yes\"\n puts \"YES\"\n else\n puts \"NO\"\n end\nend\n\n\n"}, {"source_code": "n = gets.chomp.to_i\ninputs = []\n(0..n-1).each do |i|\n inputs.push(gets.chomp)\nend\ninputs.each do |i|\n if i.downcase == \"yes\"\n puts \"YES\"\n else\n puts \"NO\"\n end\nend\n"}], "negative_code": [], "src_uid": "4c0b0cb8a11cb1fd40fef47616987029"} {"nl": {"description": "An African crossword is a rectangular table n × m in size. Each cell of the table contains exactly one letter. This table (it is also referred to as grid) contains some encrypted word that needs to be decoded.To solve the crossword you should cross out all repeated letters in rows and columns. In other words, a letter should only be crossed out if and only if the corresponding column or row contains at least one more letter that is exactly the same. Besides, all such letters are crossed out simultaneously.When all repeated letters have been crossed out, we should write the remaining letters in a string. The letters that occupy a higher position follow before the letters that occupy a lower position. If the letters are located in one row, then the letter to the left goes first. The resulting word is the answer to the problem.You are suggested to solve an African crossword and print the word encrypted there.", "input_spec": "The first line contains two integers n and m (1 ≤ n, m ≤ 100). Next n lines contain m lowercase Latin letters each. That is the crossword grid.", "output_spec": "Print the encrypted word on a single line. It is guaranteed that the answer consists of at least one letter.", "sample_inputs": ["3 3\ncba\nbcd\ncbc", "5 5\nfcofd\nooedo\nafaoa\nrdcdf\neofsf"], "sample_outputs": ["abcd", "codeforces"], "notes": null}, "positive_code": [{"source_code": "n,m = gets.chomp.split(/ /).map!{|x| x.to_i}\nbd = Array.new(n)\nbdf = Array.new(n)\nfor i in 0..n-1\n\tbd[i] = gets.chomp.split(//)\n\tbdf[i] = Array.new(m)\nend\n\nfor i in 0..n-1\n\tfor j in 0..m-1\n\t\tbdf[i][j] = true\n\tend\nend\n\nfor i in 0..n-1\n\th = Hash.new\n\tfor j in 0..m-1\n\t\tif h.key?(bd[i][j])\n\t\t\th[bd[i][j]] += 1\n\t\telse\n\t\t\th[bd[i][j]] = 1\n\t\tend\n\tend\n\tfor j in 0..m-1\n\t\tbdf[i][j] = false if h[bd[i][j]] > 1\n\tend\nend\n\nfor j in 0..m-1\n\th = Hash.new\n\tfor i in 0..n-1\n\t\tif h.key?(bd[i][j])\n\t\t\th[bd[i][j]] += 1\n\t\telse\n\t\t\th[bd[i][j]] = 1\n\t\tend\n\tend\n\tfor i in 0..n-1\n\t\tbdf[i][j] = false if h[bd[i][j]] > 1\n\tend\nend\n\nfor i in 0..n-1\n\tfor j in 0..m-1\n\t\tprint bd[i][j] if bdf[i][j] == true\n\tend\nend\n\nputs \"\"\n"}, {"source_code": "def solve\n \n n , m = readline.split(' ')\n n = n.to_i; m = m.to_i\n \n s = Array.new; h = Hash.new(0)\n \n n.times {\n s << gets\n }\n \n s_ = s.dup\n \n (0...n).each{|x|\n s_[x] = s[x].dup\n }\n \n \n (0...n).each{|x|\n \n h.clear\n \n (0...m).each{|y|\n h[s[x][y]] += 1\n }\n \n (0...m).each{|y|\n s_[x][y] = '_' if (h[s[x][y]] > 1) \n }\n \n }\n \n \n (0...m).each{|y|\n \n h.clear\n \n (0...n).each{|x|\n h[s[x][y]] += 1\n }\n \n (0...n).each{|x|\n s_[x][y] = '_' if (h[s[x][y]] > 1) \n }\n \n }\n \n res = String.new\n \n (0...n).each{|x|\n (0...m).each{|y|\n res = res + s_[x][y].chr if (s_[x][y] != '_')\n }\n }\n \n return res\n \nend\n\n#$stdin = File.open(\"in.txt\", \"r\")\nputs solve"}, {"source_code": "w=gets.to_i\nm=[]\nw.times{\n m.push(gets.split[0])\n}\nh=m[0].length\nans=\"\"\nw.times{|i|\n h.times{|j|\n ok=1\n w.times{|x|\n if i!=x && m[i][j]==m[x][j] then\n ok=0\n break\n end\n }\n h.times{|y|\n if j!=y && m[i][j]==m[i][y] then\n ok=0\n break\n end\n }\n if ok==1 then\n ans+=m[i][j]\n end\n }\n}\nputs ans"}, {"source_code": "n,m=gets.split.map(&:to_i)\nc=n.times.map{gets.chomp.bytes.to_a}\nd=c.transpose\nn.times{|i|m.times{|j|_=c[i][j];putc _ if c[i].count(_)<2&&d[j].count(_)<2}}\n"}, {"source_code": "#!/usr/bin/env ruby\n#$DEBUG = true\n#$stdin = File.open '2.test' if $DEBUG\nn, m = gets.split.map(&:to_i)\nchars = [].tap{|chars| n.times{chars << gets.chomp.split(\"\") } }\n26.times { |c| \n c = (c+97).chr \n 2.times {\n chars.each {|line|\n line.map!{|ci| ci == c ? \"\" : ci } if line.select{ |ci| ci == c || ci == \"\" }.size > 1\n }\n chars = chars.transpose\n }\n chars.each {|line| line.map!{|ci| ci == \"\" ? nil : ci } }\n}\nputs chars.flatten.compact.join\n"}, {"source_code": "n,m=gets.split.map(&:to_i)\nc=n.times.map{gets.chomp.bytes.to_a}\nd=c.transpose\nn.times{|i|m.times{|j|_=c[i][j];putc _ if c[i].count(_)<2&&d[j].count(_)<2}}\n"}, {"source_code": "n,m=gets.split.map(&:to_i)\nc=n.times.map{gets.chomp.bytes.to_a}\nd=c.transpose\nn.times{|i|m.times{|j|_=c[i][j];putc _ if c[i].count(_)<2&&d[j].count(_)<2}}"}, {"source_code": "n,m=gets.split.map(&:to_i)\nc=n.times.map{gets.chomp.bytes.to_a}\nd=c.transpose\nn.times{|i|m.times{|j|_=c[i][j];putc _ if c[i].count(_)<2&&d[j].count(_)<2}}\n"}, {"source_code": "n,m=gets.split.map(&:to_i)\nc=n.times.map{gets.chomp.bytes.to_a}\nd=c.transpose\nn.times{|i|m.times{|j|_=c[i][j];putc _ if c[i].count(_)<2&&d[j].count(_)<2}}\n"}, {"source_code": "n,m=gets.split.map(&:to_i)\nc=n.times.map{gets.chomp.bytes.to_a}\nd=c.transpose\nn.times{|i|m.times{|j|_=c[i][j];putc _ if c[i].count(_)<2&&d[j].count(_)<2}}"}, {"source_code": "#=================================\n# Better be sure than sorry. \n#=================================\n$stdin=File.open(\"./input.txt\",\"r\") if ENV[\"LOGNAME\"]==\"skydos\"\nrequire 'prime'\nrequire 'mathn'\n#=================================\n\nn,m=gets.split.map(&:to_i)\n\nboard_main=[]\nboard_copy=[]\nn.times{\n\tline = gets.strip.split(//)\n\tboard_main << line\n\tboard_copy << line.dup\n}\n\nfor i in 0...n\n\tfor j in 0...m\n\t\tch = board_main[i][j]\n\t\t\n\t\t# rows\n\t\tcount=0\n\t\tfor row_index in 0...n\n\t\t\tcount+=1 if board_main[row_index][j]==ch\n\t\tend\n\t\t\n\t\tif count!=1\n\t\t\tfor row_index in 0...n\n\t\t\t\tboard_copy[row_index][j]=\"\" if board_main[row_index][j]==ch\n\t\t\tend\n\t\tend\n\t\t\n\t\t# cols\n\t\tcount=0\n\t\tfor col_index in 0...m\n\t\t\tcount+=1 if board_main[i][col_index]==ch\n\t\tend\n\t\t\n\t\tif count!=1\n\t\t\tfor col_index in 0...m\n\t\t\t\tboard_copy[i][col_index]=\"\" if board_main[i][col_index]==ch\n\t\t\tend\n\t\tend\n\tend\nend\n\nfor i in 0...n\n\tfor j in 0...m\n\t\tprint board_copy[i][j]\n\tend\nend\n"}, {"source_code": "n,m=gets.split.map(&:to_i)\nc=n.times.map{gets.chomp.bytes.to_a}\nd=c.transpose\nn.times{|i|m.times{|j|_=c[i][j];putc _ if c[i].count(_)<2&&d[j].count(_)<2}}\n"}, {"source_code": "n, m = $stdin.readline.split(\" \").collect { |i| i.to_i }\narr = []\nfor i in 0...n\n\tarr << $stdin.readline.chomp\nend\ntmph = Hash.new(0)\narr.each do |s|\n\tfor i in 0...m\n\t\ts[i] = s[i].upcase if tmph[s[i]] > 1\n\t\ttmph[s[i].downcase] += 1\n\tend\n\tfor i in 0...m\n\t\ts[i] = s[i].upcase if tmph[s[i]] > 1\n\tend\n\ttmph = Hash.new(0)\nend\nfor i in 0...m\n\tarr.each do |s|\n\t\ts[i] = s[i].upcase if tmph[s[i].downcase] > 1\n\t\ttmph[s[i].downcase] += 1 \n\tend\n\tarr.each do |s|\n\t\ts[i] = s[i].upcase if tmph[s[i].downcase] > 1\n\tend\n\ttmph = Hash.new(0)\nend\narr.each do |s|\n\tfor i in 0...m\n\t\tprint s[i] if s[i] == s[i].downcase\n\tend\nend\nputs \"\\n\"\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\nR, C = [n, m].map { |i| Array.new(i) { Hash.new { |hash,key| hash[key] = 0 } } }\nF = Array.new\n(0...n).each do |r|\n t = gets.chomp\n (0...m).each do |c|\n R[r][t[c]] += 1\n C[c][t[c]] += 1\n end\n F << t\nend\nret = \"\"\n(0...n).each do |r|\n (0...m).each do |c|\n ret += F[r][c] if R[r][F[r][c]] + C[c][F[r][c]] <= 2\n end\nend\n\nputs ret\n"}, {"source_code": "#Fuck U Codeforces :D\nn,m=gets.split.map(&:to_i)\nc=n.times.map{gets.chomp.bytes.to_a}\nd=c.transpose\nn.times{|i|m.times{|j|_=c[i][j];putc _ if c[i].count(_)<2&&d[j].count(_)<2}}\n#Expected to be acc.\n#YARAAAAAAB"}, {"source_code": "n,m=gets.split.map(&:to_i)\nc=n.times.map{gets.chomp.bytes.to_a}\nd=c.transpose\nn.times{|i|m.times{|j|_=c[i][j];putc _ if c[i].count(_)<2&&d[j].count(_)<2}}"}, {"source_code": "n,m=gets.split.map(&:to_i)\nc=n.times.map{gets.chomp.bytes.to_a}\nd=c.transpose\nn.times{|i|m.times{|j|_=c[i][j];putc _ if c[i].count(_)<2&&d[j].count(_)<2}}\n"}], "negative_code": [{"source_code": "def solve\n \n n , m = readline.split(' ')\n n = n.to_i; m = m.to_i\n\n s = Array.new; h = Hash.new\n \n n.times {\n s << gets\n }\n \n s_ = s.dup\n=begin \n (0...n).each{|x|\n s_[x] = s[x].dup\n }\n \n \n (0...n).each{|x|\n \n h = Hash.new\n \n (0...m).each{|y|\n h[s[x][y].chr] += 1\n }\n \n (0...m).each{|y|\n s_[x][y] = 0 if (h[s[x][y].chr] > 1) \n }\n \n }\n \n \n (0...m).each{|y|\n \n h = Hash.new\n \n (0...n).each{|x|\n h[s[x][y].chr] += 1\n }\n \n (0...n).each{|x|\n s_[x][y] = 0 if (h[s[x][y].chr] > 1) \n }\n \n }\n=end \n res = String.new\n \n (0...n).each{|x|\n (0...m).each{|y|\n res = res + s_[x][y].chr if (s_[x][y] != 0)\n }\n }\n \n return res\n\nend\n\n#$stdin = File.open(\"in.txt\", \"r\")\nputs solve"}, {"source_code": "def solve\n \n n , m = readline.split(' ')\n n = n.to_i; m = m.to_i\n \n s = Array.new; h = Hash.new(0)\n \n n.times {\n s << gets\n }\n \n s_ = s.dup\n \n (0...n).each{|x|\n s_[x] = s[x].dup\n }\n \n \n (0...n).each{|x|\n \n h.clear\n \n (0...m).each{|y|\n h[s[x][y]] += 1\n }\n \n (0...m).each{|y|\n # s_[x][y] = 0 if (h[s[x][y]] > 1) \n }\n \n }\n \n \n (0...m).each{|y|\n \n h.clear\n \n (0...n).each{|x|\n h[s[x][y]] += 1\n }\n \n (0...n).each{|x|\n # s_[x][y] = 0 if (h[s[x][y]] > 1) \n }\n \n }\n \n res = String.new\n \n (0...n).each{|x|\n (0...m).each{|y|\n res = res + s_[x][y].chr if (s_[x][y] != 0)\n }\n }\n \n return res\n \nend\n\n#$stdin = File.open(\"in.txt\", \"r\")\nputs solve"}, {"source_code": "def solve\n \n n , m = readline.split(' ')\n n = n.to_i; m = m.to_i\n \n s = Array.new; h = Hash.new(0)\n \n n.times {\n s << gets\n }\n \n s_ = s.dup\n \n (0...n).each{|x|\n s_[x] = s[x].dup\n }\n \n \n (0...n).each{|x|\n \n h.clear\n \n (0...m).each{|y|\n # h[s[x][y].chr] += 1\n }\n \n (0...m).each{|y|\n # s_[x][y] = 0 if (h[s[x][y].chr] > 1) \n }\n \n }\n \n \n (0...m).each{|y|\n \n h.clear\n \n (0...n).each{|x|\n # h[s[x][y].chr] += 1\n }\n \n (0...n).each{|x|\n # s_[x][y] = 0 if (h[s[x][y].chr] > 1) \n }\n \n }\n \n res = String.new\n \n (0...n).each{|x|\n (0...m).each{|y|\n res = res + s_[x][y].chr if (s_[x][y] != 0)\n }\n }\n \n return res\n \nend\n\n#$stdin = File.open(\"in.txt\", \"r\")\nputs solve"}, {"source_code": "def solve\n \n n , m = readline.split(' ')\n n = n.to_i; m = m.to_i\n\n s = Array.new; h = Hash.new\n \n n.times {\n s << gets\n }\n \n s_ = s.dup\n \n (0...n).each{|x|\n s_[x] = s[x].dup\n }\n \n\n=begin\n (0...n).each{|x|\n \n h = Hash.new\n \n (0...m).each{|y|\n h[s[x][y].chr] += 1\n }\n \n (0...m).each{|y|\n s_[x][y] = 0 if (h[s[x][y].chr] > 1) \n }\n \n }\n \n \n (0...m).each{|y|\n \n h = Hash.new\n \n (0...n).each{|x|\n h[s[x][y].chr] += 1\n }\n \n (0...n).each{|x|\n s_[x][y] = 0 if (h[s[x][y].chr] > 1) \n }\n \n }\n=end \n res = String.new\n \n (0...n).each{|x|\n (0...m).each{|y|\n res = res + s_[x][y].chr if (s_[x][y] != 0)\n }\n }\n \n return res\n\nend\n\n#$stdin = File.open(\"in.txt\", \"r\")\nputs solve"}, {"source_code": "def solve\n \n a , b = readline.split(' ')\n a = a.to_i; b = b.to_i\n \n x = 0; sa = 0; sb = 0\n \n while true do\n t = a - x % a; w = b - x % b;\n break if (t == a && w == b && x > 0)\n \n if (t < w || t == w && a > b) then\n sa += t; x += t\n else\n sb += w; x += w;\n end\n end\n \n return 'Dasha' if (sa > sb)\n return 'Masha' if (sa < sb)\n return 'Equal'\n end\n \n #$stdin = File.open(\"in.txt\", \"r\")\n puts solve"}, {"source_code": "def solve\n \n n , m = readline.split(' ')\n n = n.to_i; m = m.to_i\n \n s = Array.new; h = Hash.new(0)\n \n n.times {\n s << gets\n }\n \n s_ = s.dup\n \n (0...n).each{|x|\n s_[x] = s[x].dup\n }\n \n \n (0...n).each{|x|\n \n h.clear\n \n (0...m).each{|y|\n # h[(s[x][y].chr)] += 1\n }\n \n (0...m).each{|y|\n s_[x][y] = 0 if (h[s[x][y].chr] > 1) \n }\n \n }\n \n \n (0...m).each{|y|\n \n h.clear\n \n (0...n).each{|x|\n # h[(s[x][y].chr)] += 1\n }\n \n (0...n).each{|x|\n s_[x][y] = 0 if (h[s[x][y].chr] > 1) \n }\n \n }\n \n res = String.new\n \n (0...n).each{|x|\n (0...m).each{|y|\n res = res + s_[x][y].chr if (s_[x][y] != 0)\n }\n }\n \n return res\n \nend\n\n#$stdin = File.open(\"in.txt\", \"r\")\nputs solve"}, {"source_code": "#=================================\n# Better be sure than sorry. \n#=================================\n$stdin=File.open(\"./input.txt\",\"r\") if ENV[\"LOGNAME\"]==\"skydos\"\nrequire 'prime'\nrequire 'mathn'\n#=================================\n\nn,m=gets.split.map(&:to_i)\n\nboard_main=[]\nboard_copy=[]\nn.times{\n\tline = gets.strip.split(//)\n\tboard_main << line\n\tboard_copy << line.dup\n}\n\nfor i in 0...n\n\tfor j in 0...m\n\t\tch = board_main[i][j]\n\t\t\n\t\t# rows\n\t\tcount=0\n\t\tfor row_index in 0...n\n\t\t\tcount+=1 if board_main[row_index][j]==ch\n\t\tend\n\t\t\n\t\tif count!=1\n\t\t\tfor row_index in 0...n\n\t\t\t\tboard_copy[row_index][j]=\"_\" if board_main[row_index][j]==ch\n\t\t\tend\n\t\tend\n\t\t\n\t\t# cols\n\t\tcount=0\n\t\tfor col_index in 0...m\n\t\t\tcount+=1 if board_main[i][col_index]==ch\n\t\tend\n\t\t\n\t\tif count!=1\n\t\t\tfor col_index in 0...m\n\t\t\t\tboard_copy[i][col_index]=\"_\" if board_main[i][col_index]==ch\n\t\t\tend\n\t\tend\n\tend\nend\n\nfor i in 0...n\n\tfor j in 0...m\n\t\tprint board_copy[i][j]\n\tend\n\tputs\nend\n"}, {"source_code": "n, m = $stdin.readline.split(\" \").collect { |i| i.to_i }\narr = []\nfor i in 0...n\n\tarr << $stdin.readline.chomp\nend\np arr\ntmph = Hash.new(0)\narr.each do |s|\n\tfor i in 0...m\n\t\ts[i] = s[i].upcase if tmph[s[i]] > 1\n\t\ttmph[s[i].downcase] += 1\n\tend\n\tfor i in 0...m\n\t\ts[i] = s[i].upcase if tmph[s[i]] > 1\n\tend\n\ttmph = Hash.new(0)\nend\nfor i in 0...m\n\tarr.each do |s|\n\t\ts[i] = s[i].upcase if tmph[s[i].downcase] > 1\n\t\ttmph[s[i].downcase] += 1 \n\tend\n\tarr.each do |s|\n\t\ts[i] = s[i].upcase if tmph[s[i].downcase] > 1\n\tend\n\ttmph = Hash.new(0)\nend\narr.each do |s|\n\tfor i in 0...m\n\t\tprint s[i] if s[i] == s[i].downcase\n\tend\nend\nputs \"\\n\"\n"}], "src_uid": "9c90974a0bb860a5e180760042fd5045"} {"nl": {"description": "Let's call a number k-good if it contains all digits not exceeding k (0, ..., k). You've got a number k and an array a containing n numbers. Find out how many k-good numbers are in a (count each number every time it occurs in array a).", "input_spec": "The first line contains integers n and k (1 ≤ n ≤ 100, 0 ≤ k ≤ 9). The i-th of the following n lines contains integer ai without leading zeroes (1 ≤ ai ≤ 109).", "output_spec": "Print a single integer — the number of k-good numbers in a.", "sample_inputs": ["10 6\n1234560\n1234560\n1234560\n1234560\n1234560\n1234560\n1234560\n1234560\n1234560\n1234560", "2 1\n1\n10"], "sample_outputs": ["10", "1"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/ruby\nn,k=gets.split.map(&:to_i)\np n.times.count{\n\ta=gets.chomp.split('').map(&:to_i)\n\t(0..k).all?{|e|a.include?(e)}\n}"}, {"source_code": "n, k = STDIN.readline.split.map {|s| s.to_i }\ncount = 0\nn.times do\n\ts = STDIN.readline.strip\n\tokay = true\n\t(0..k).each do |d|\n\t\tif s.index(d.to_s) == nil\n\t\t\tokay = false\n\t\t\tbreak\n\t\tend\n\tend\n\tcount += 1 if okay\nend\nputs count\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\ntot = 0\nn.times do\n arr = gets.split(//)[0..-2].map(&:to_i).uniq.sort\n tot += 1 if arr.size > k && arr[0..k] == (0..k).to_a\nend\nputs tot\n"}, {"source_code": "class A\n def initialize\n n, k = gets.chomp.split(' ').map(&:to_i)\n answer = 0\n\n n.times do\n check_list = Hash.new\n flag = true\n num = gets.chomp.split('').map(&:to_i).sort.uniq\n\n num.each do |val|\n check_list[val] = true if val <= k\n end\n\n answer += 1 if check_list.size == (k+1) && flag\n end\n\n puts answer\n end\nend\n\na = A.new"}, {"source_code": "ans=0\na=gets.chomp.split(\" \")\ninp=a[0].to_i\nrng=a[1].to_i\ninp.times do\na=gets.chomp\ntempa=Hash.new(0)\na.each_char do |i|\ntempa[i]+=1\nend\ntrg=1\n0.upto(rng) do |i|\nif tempa[\"#{i}\"]==0\ntrg=0\nend\nbreak if trg==0\nend\nif trg==1\nans+=1\nend\nend\n\n\n\n\n\nputs \"#{ans}\"\n\n"}, {"source_code": "t=0;n,k=gets.split.map &:to_i;p=(0..k).to_a;pl=p.length;n.times{m=gets.strip.split('').map &:to_i;t+=((p&m).length >= pl)?1:0};p t"}, {"source_code": "n,k=gets.split.map &:to_i;p=(0..k).to_a.map &:to_s;p (((1..n).map{|i|((p&(gets.strip.split(''))).length >= p.length)?1:0}).count(1))\n"}, {"source_code": "n,k=gets.split.map &:to_i;p=(0..k).to_a.map &:to_s;pl=p.length;p (((1..n).map{|i|((p&(gets.strip.split(''))).length >= pl)?1:0}).count(1))\n"}, {"source_code": "t=0;n,k=gets.split.map &:to_i;p=(0..k).to_a;pl=p.length;n.times{m=gets.strip.split('').map &:to_i;t+=((p&m).length >= pl)?1:0};p t\n"}, {"source_code": "n,k=gets.split.map &:to_i;p=(0..k).to_a.map &:to_s;q=p.length;p (((1..n).map{|i|((p&(gets.strip.split(''))).length >= q)?1:0}).count(1))\n"}, {"source_code": "input = gets.chomp\nnums = input.split\nn = Integer(nums[0])\nk = Integer(nums[1])\na = Array.new\nfor i in 0...n\n input = gets.chomp\n a[i] = Array.new\n for j in 0...input.length\n a[i][j] = Integer(input[j])\n end\nend\n\ndef good_number(x)\n x = x.sort\n y = Array.new\n y[0] = x[0]\n for i in 0...x.length - 1\n if x[i + 1] != x[i]\n y.push(x[i + 1])\n end\n end\n return y\nend\nans = 0\nfor i in 0...n\n if good_number(a[i])[0] == 0\n if good_number(a[i]).length >= k + 1 && good_number(a[i])[k] == k\n ans += 1\n end\n end\nend\nputs ans"}, {"source_code": "n, k = gets.chomp.split.map &:to_i\nres = 0\nn.times do\n s = gets.chomp\n # Check all digit in [0..k] appear in s\n t = true\n (0..k).to_a.each do |index|\n t = false and break if s.index((index + 48).chr).nil?\n end\n res += 1 if t\nend\nputs res"}], "negative_code": [{"source_code": "class A\n def initialize\n n, k = gets.chomp.split(' ').map(&:to_i)\n answer = 0\n\n n.times do\n check_list = Hash.new\n num = gets.chomp.split('').map(&:to_i)\n\n num.each do |val|\n check_list[val] = true\n end\n\n answer += 1 if check_list.size == (k+1)\n end\n\n puts answer\n end\nend\n\na = A.new"}, {"source_code": "class A\n def initialize\n n, k = gets.chomp.split(' ').map(&:to_i)\n answer = 0\n\n n.times do\n check_list = Hash.new\n flag = true\n num = gets.chomp.split('').map(&:to_i).sort.uniq\n\n num.each do |val|\n flag = false if val > k\n check_list[val] = true\n end\n\n answer += 1 if check_list.size == (k+1) && flag\n end\n\n puts answer\n end\nend\n\na = A.new"}, {"source_code": "class A\n def initialize\n n, k = gets.chomp.split(' ').map(&:to_i)\n answer = 0\n\n n.times do\n num = gets.chomp.split('').map(&:to_i)\n\n answer += 1 if k == num.count{|val| val < k }\n end\n\n puts answer\n end\nend\n\na = A.new"}, {"source_code": "class A\n def initialize\n n, k = gets.chomp.split(' ').map(&:to_i)\n answer = 0\n\n n.times do\n check_list = Hash.new\n flag = true\n num = gets.chomp.split('').map(&:to_i)\n\n num.each do |val|\n flag = false if val > k\n check_list[val] = true\n end\n\n answer += 1 if check_list.size == (k+1) && flag\n end\n\n puts answer\n end\nend\n\na = A.new"}, {"source_code": "class A\n def initialize\n n, k = gets.chomp.split(' ').map(&:to_i)\n answer = 0\n\n n.times do\n num = gets.chomp.split('').map(&:to_i)\n\n answer += 1 if num.all?{|val| val <= k }\n end\n\n puts answer\n end\nend\n\na = A.new"}, {"source_code": "t=0;n,k=gets.split.map &:to_i;n.times{px=(0..k).to_a.join;mx=gets.strip;t+=((mx.tr(px,'')=='' and px.tr(mx,'')=='')?1:0)};p t"}, {"source_code": "t=0;n,k=gets.split.map &:to_i\nn.times{\n\tpx=(0..k).to_a.join\n\tmx=gets.strip\n\tt+=(mx.tr(px,'')=='' and px.tr(mx,'')=='')?1:0\n}\np t\n"}, {"source_code": "input = gets.chomp\nnums = input.split\nn = Integer(nums[0])\nk = Integer(nums[1])\na = Array.new\nfor i in 0...n\n input = gets.chomp\n a[i] = Array.new\n for j in 0...input.length\n a[i][j] = Integer(input[j])\n end\nend\n\ndef good_number(x)\n x = x.sort\n y = Array.new\n y[0] = x[0]\n for i in 0...x.length - 1\n if x[i + 1] != x[i]\n y.push(x[i + 1])\n end\n end\n return y\nend\nans = 0\nfor i in 0...n\n if (good_number(a[i])).length >= k + 1\n ans += 1\n end\nend\nputs ans\n"}, {"source_code": "input = gets.chomp\nnums = input.split\nn = Integer(nums[0])\nk = Integer(nums[1])\na = Array.new\nfor i in 0...n\n input = gets.chomp\n a[i] = Array.new\n for j in 0...input.length\n a[i][j] = Integer(input[j])\n end\nend\n\ndef good_number(x)\n x = x.sort\n y = Array.new\n y[0] = x[0]\n for i in 0...x.length - 1\n if x[i + 1] != x[i]\n y.push(x[i + 1])\n end\n end\n return y\nend\nans = 0\nfor i in 0...n\n if (good_number(a[i])).length == k + 1\n ans += 1\n end\nend\nputs ans"}, {"source_code": "input = gets.chomp\nnums = input.split\nn = Integer(nums[0])\nk = Integer(nums[1])\na = Array.new\nfor i in 0...n\n input = gets.chomp\n a[i] = Array.new\n for j in 0...input.length\n a[i][j] = Integer(input[j])\n end\nend\n\ndef good_number(x)\n x = x.sort\n y = Array.new\n y[0] = x[0]\n for i in 0...x.length - 1\n if x[i + 1] != x[i]\n y.push(x[i + 1])\n end\n end\n return y\nend\nans = 0\nfor i in 0...n\n if k != 0\n if (good_number(a[i])).length >= k + 1\n ans += 1\n end\n else\n if good_number(a[i])[0] == 0\n ans += 1\n end\n end\nend\nputs ans"}, {"source_code": "input = gets.chomp\nnums = input.split\nn = Integer(nums[0])\nk = Integer(nums[1])\na = Array.new\nfor i in 0...n\n input = gets.chomp\n a[i] = Array.new\n for j in 0...input.length\n a[i][j] = Integer(input[j])\n end\nend\n\ndef good_number(x)\n x = x.sort\n y = Array.new\n y[0] = x[0]\n for i in 0...x.length - 1\n if x[i + 1] != x[i]\n y.push(x[i + 1])\n end\n end\n return y\nend\nans = 0\nfor i in 0...n\n if good_number(a[i])[0] == 0\n if good_number(a[i]).length >= k + 1\n ans += 1\n end\n end\nend\nputs ans"}], "src_uid": "fc831eda72f2ebe0865a3fb1b783edc5"} {"nl": {"description": "You are given a non-empty string s consisting of lowercase English letters. You have to pick exactly one non-empty substring of s and shift all its letters 'z' 'y' 'x' 'b' 'a' 'z'. In other words, each character is replaced with the previous character of English alphabet and 'a' is replaced with 'z'.What is the lexicographically minimum string that can be obtained from s by performing this shift exactly once?", "input_spec": "The only line of the input contains the string s (1 ≤ |s| ≤ 100 000) consisting of lowercase English letters.", "output_spec": "Print the lexicographically minimum string that can be obtained from s by shifting letters of exactly one non-empty substring.", "sample_inputs": ["codeforces", "abacaba"], "sample_outputs": ["bncdenqbdr", "aaacaba"], "notes": "NoteString s is lexicographically smaller than some other string t of the same length if there exists some 1 ≤ i ≤ |s|, such that s1 = t1, s2 = t2, ..., si - 1 = ti - 1, and si < ti."}, "positive_code": [{"source_code": "class Array\n def r\n self.map { |q| q.to_i }\n end\nend\n\ndef read_line\n STDIN.gets.split(\" \")\nend\n\ndef read_to_array\n read_line.to_a\nend\n\ndef read_numbers_to_array\n read_to_array.r\nend\n\ns = read_to_array[0]\nbeg = false\nended = false\nres = String.new\ns.each_char do |c|\n if c == 'a'\n if beg\n beg = false \n ended = true\n end\n else\n if !ended\n beg = true\n end\n\n if beg\n c = c.ord - 1\n end\n end\n\n res << c\nend\n\nif beg == false && ended == false\n n = res[res.size - 1]\n res[res.size - 1] = 'z'\nend\n\nputs res\n"}, {"source_code": "s=gets.chomp\nn=s.size\n\ni=s.index /[^a]/\nunless i\n s[-1]=?z\n puts s\n exit\nend\n\nj=s.index(?a, i+1)||n\n\nputs s[0, i]+s[i, j-i].tr!('b-z', 'a-y')+s[j, n-j]\n"}], "negative_code": [{"source_code": "class Array\n def r\n self.map { |q| q.to_i }\n end\nend\n\ndef read_line\n STDIN.gets.split(\" \")\nend\n\ndef read_to_array\n read_line.to_a\nend\n\ndef read_numbers_to_array\n read_to_array.r\nend\n\ns = read_to_array[0]\nbeg = false\nended = false\nres = String.new\ns.each_char do |c|\n if c == 'a'\n if beg\n beg = false \n ended = true\n end\n else\n if !ended\n beg = true\n end\n\n if beg\n c = (c.ord - 1)\n end\n end\n\n res << c\nend\n\nputs res\n"}, {"source_code": "class Array\n def r\n self.map { |q| q.to_i }\n end\nend\n\ndef read_line\n STDIN.gets.split(\" \")\nend\n\ndef read_to_array\n read_line.to_a\nend\n\ndef read_numbers_to_array\n read_to_array.r\nend\n\ns = read_to_array[0]\nbeg = false\nended = false\nres = String.new\ns.each_char do |c|\n if c == 'a'\n if beg\n beg = false \n ended = true\n end\n else\n if !ended\n beg = true\n end\n\n if beg\n c = c.ord - 1\n end\n end\n\n res << c\nend\n\nif beg == false && ended == false\n n = res[res.size - 1]\n res[res.size - 1] = 'a'\nend\n\nputs res\n"}, {"source_code": "s=gets.chomp\nfor i in 0..s.size-1\n next if s[i]==?a\n for j in i..s.size-1\n if s[j]!=?a\n s[j]=(s[j].ord-1).chr\n else\n break\n end\n end\n break\nend\nputs s\n"}], "src_uid": "e70708f72da9a203b21fc4112ede9268"} {"nl": {"description": "You are given an array $$$a_1, a_2, \\dots, a_n$$$, consisting of $$$n$$$ positive integers. Initially you are standing at index $$$1$$$ and have a score equal to $$$a_1$$$. You can perform two kinds of moves: move right — go from your current index $$$x$$$ to $$$x+1$$$ and add $$$a_{x+1}$$$ to your score. This move can only be performed if $$$x<n$$$. move left — go from your current index $$$x$$$ to $$$x-1$$$ and add $$$a_{x-1}$$$ to your score. This move can only be performed if $$$x>1$$$. Also, you can't perform two or more moves to the left in a row. You want to perform exactly $$$k$$$ moves. Also, there should be no more than $$$z$$$ moves to the left among them.What is the maximum score you can achieve?", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of testcases. The first line of each testcase contains three integers $$$n, k$$$ and $$$z$$$ ($$$2 \\le n \\le 10^5$$$, $$$1 \\le k \\le n - 1$$$, $$$0 \\le z \\le min(5, k)$$$) — the number of elements in the array, the total number of moves you should perform and the maximum number of moves to the left you can perform. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^4$$$) — the given array. The sum of $$$n$$$ over all testcases does not exceed $$$3 \\cdot 10^5$$$.", "output_spec": "Print $$$t$$$ integers — for each testcase output the maximum score you can achieve if you make exactly $$$k$$$ moves in total, no more than $$$z$$$ of them are to the left and there are no two or more moves to the left in a row.", "sample_inputs": ["4\n5 4 0\n1 5 4 3 2\n5 4 1\n1 5 4 3 2\n5 4 4\n10 20 30 40 50\n10 7 3\n4 6 8 2 9 9 7 4 10 9"], "sample_outputs": ["15\n19\n150\n56"], "notes": "NoteIn the first testcase you are not allowed to move left at all. So you make four moves to the right and obtain the score $$$a_1 + a_2 + a_3 + a_4 + a_5$$$.In the second example you can move one time to the left. So we can follow these moves: right, right, left, right. The score will be $$$a_1 + a_2 + a_3 + a_2 + a_3$$$.In the third example you can move four times to the left but it's not optimal anyway, you can just move four times to the right and obtain the score $$$a_1 + a_2 + a_3 + a_4 + a_5$$$."}, "positive_code": [{"source_code": "def read\n gets.split.map &:to_i\nend\ngets.to_i.times do\n n, k, z = read\n a = read\n res, s, mx = 0, 0, 0\n (0..k).each do |i|\n mx = [mx, a[i] + a[i + 1]].max if i < n - 1\n s += a[i]\n if i % 2 == k % 2\n cur = (k - i) / 2\n res = [res, s + cur * mx].max if cur <= z\n end\n end\n puts res\nend"}], "negative_code": [], "src_uid": "537791353fe9f5892f28e1793bae2935"} {"nl": {"description": "You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $$$n$$$ planks, where the $$$i$$$-th plank has the color $$$a_i$$$. You want to repaint the fence in such a way that the $$$i$$$-th plank has the color $$$b_i$$$.You've invited $$$m$$$ painters for this purpose. The $$$j$$$-th painter will arrive at the moment $$$j$$$ and will recolor exactly one plank to color $$$c_j$$$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank.Can you get the coloring $$$b$$$ you want? If it's possible, print for each painter which plank he must paint.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n, m \\le 10^5$$$) — the number of planks in the fence and the number of painters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le n$$$) — the initial colors of the fence. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \\dots, b_n$$$ ($$$1 \\le b_i \\le n$$$) — the desired colors of the fence. The fourth line of each test case contains $$$m$$$ integers $$$c_1, c_2, \\dots, c_m$$$ ($$$1 \\le c_j \\le n$$$) — the colors painters have. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$10^5$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$ over all test cases.", "output_spec": "For each test case, output \"NO\" if it is impossible to achieve the coloring $$$b$$$. Otherwise, print \"YES\" and $$$m$$$ integers $$$x_1, x_2, \\dots, x_m$$$, where $$$x_j$$$ is the index of plank the $$$j$$$-th painter should paint. You may print every letter in any case you want (so, for example, the strings \"yEs\", \"yes\", \"Yes\" and \"YES\" are all recognized as positive answer).", "sample_inputs": ["6\n1 1\n1\n1\n1\n5 2\n1 2 2 1 1\n1 2 2 1 1\n1 2\n3 3\n2 2 2\n2 2 2\n2 3 2\n10 5\n7 3 2 1 7 9 4 2 7 9\n9 9 2 1 4 9 4 2 3 9\n9 9 7 4 3\n5 2\n1 2 2 1 1\n1 2 2 1 1\n3 3\n6 4\n3 4 2 4 1 2\n2 3 1 3 1 1\n2 2 3 4"], "sample_outputs": ["YES\n1\nYES\n2 2\nYES\n1 1 1\nYES\n2 1 9 5 9\nNO\nNO"], "notes": null}, "positive_code": [{"source_code": "t = gets.chomp.to_i\r\nt.times do\r\n n, m = gets.chomp.split(' ').map(&:to_i)\r\n arr = gets.chomp.split(' ').map(&:to_i)\r\n brr = gets.chomp.split(' ').map(&:to_i)\r\n crr = gets.chomp.split(' ').map(&:to_i)\r\n\r\n colors = Array.new(n + 1) {Array.new}\r\n missed = Array.new(n + 1) {Array.new}\r\n\r\n cur = arr.dup\r\n res = []\r\n\r\n n.times do |ind|\r\n if arr[ind] != brr[ind]\r\n missed[brr[ind]] << ind\r\n else\r\n colors[arr[ind]] << ind\r\n end\r\n end\r\n\r\n buff = []\r\n crr.each_with_index do |el, ind|\r\n # puts \"Buff = #{buff}\"\r\n # puts \"res = #{res}\"\r\n if missed[el].any?\r\n # puts \"Iter1 = {#{[el, ind].to_s}}\"\r\n t = missed[el].shift\r\n res << t + 1\r\n cur[t] = el\r\n if buff.any?\r\n buff.each do |i|\r\n res[i] = t + 1\r\n end\r\n buff = []\r\n end\r\n colors[el] << t\r\n next\r\n end\r\n if colors[el].any?\r\n # puts \"Iter2 = {#{[el, ind].to_s}}\"\r\n t = colors[el][0]\r\n res << t + 1\r\n cur[t] = el\r\n if buff.any?\r\n buff.each do |i|\r\n res[i] = t + 1\r\n end\r\n buff = []\r\n end\r\n next\r\n end\r\n # puts \"Iter3 = {#{[el, ind].to_s}}\"\r\n buff << ind\r\n res << -1\r\n end\r\n # puts \"RES = #{res}\"\r\n if buff.empty? && missed.all? {|el| el.empty?}\r\n puts \"YES\"\r\n puts res.join(' ')\r\n else\r\n puts \"NO\"\r\n end\r\nend"}, {"source_code": "def solve\r\n n, m = ints\r\n a = int1s\r\n b = int1s\r\n c = int1s\r\n colors = Array.new(n) { [] }\r\n has_color = [nil] * n\r\n\r\n b.each_with_index do |color, i|\r\n if a[i] != color\r\n colors[color] << i\r\n else\r\n has_color[color] = i\r\n end\r\n end\r\n\r\n painted = nil\r\n i = m-1\r\n plank = []\r\n c.reverse_each do |color|\r\n if not colors[color].empty?\r\n painted = colors[color].pop\r\n elsif painted\r\n painted = painted\r\n elsif has_color[color]\r\n painted = has_color[color]\r\n else\r\n no\r\n return\r\n end\r\n plank[i] = painted+1\r\n i -= 1\r\n end\r\n\r\n unless colors.all?(&:empty?)\r\n no\r\n return\r\n end\r\n\r\n yes\r\n puts plank * \" \"\r\nend\r\n\r\ndef main\r\n t = int\r\n t.times do\r\n solve\r\n end\r\nend\r\n\r\nDEBUG = true\r\nMOD = 10**9+7\r\nYESNO = %w(NO YES)\r\nINF = 10**9\r\n\r\ndef int; gets.to_s.to_i end\r\ndef ints; gets.to_s.split.map { |s| s.to_i } end\r\ndef int1s; gets.to_s.split.map { |s| s.to_i - 1 } end\r\ndef float; gets.to_s.to_f end\r\ndef floats; gets.to_s.split.map { |s| s.to_f } end\r\ndef array_of(&convert); gets.to_s.split.map(&convert) end\r\ndef string; gets.to_s.chomp end\r\ndef rep(n, &b); Array.new(n, &b) end\r\ndef yes; puts YESNO[1] end\r\ndef no; puts YESNO[0] end\r\ndef yesno t; puts YESNO[t] end\r\ndef zip(xs, *yss); Enumerator.new { |y| xs.zip(*yss) { |a| y.yield(*a) } } end\r\ndef max(*xs, &block); block_given? ? xs.max_by(&block) : xs.max end\r\ndef min(*xs, &block); block_given? ? xs.min_by(&block) : xs.min end\r\ndef minmax(*xs, &block); block_given? ? xs.minmax_by(&block) : xs.minmax end\r\ndef gcd(*xs); xs.inject(0, :gcd) end\r\ndef matrix(h, w, fill=nil, &block); return Array.new(h) { Array.new(w, &block) } if block_given?; Array.new(h) { [fill] * w } end\r\ndef debug(x = nil); STDERR.puts (block_given? ? yield(x) : x).inspect if DEBUG; x end\r\ndef debug_grid(grid, width = 1); grid.each { |row| STDERR.puts row.map { |x| x.inspect.ljust(width) }.join(\"\") } if DEBUG; grid end\r\ndef if_debug; yield if DEBUG end\r\n\r\nmodule Boolean\r\n def coerce(other); [other, to_i] end\r\n def +@; to_i end\r\n def to_int; to_i end\r\n def *(other); to_i * other end\r\nend\r\n\r\nclass TrueClass\r\n include Boolean\r\n def to_i; 1 end\r\nend\r\n\r\nclass FalseClass\r\n include Boolean\r\n def to_i; 0 end\r\nend\r\n\r\nclass Integer\r\n def div_ceil(y); (self + y - 1) / y end\r\n def mod_inv(mod = MOD); pow(mod-2, mod) end\r\n def mod_div(y, mod = MOD); self * mod_inv(y, mod) % mod end\r\n def factorial(mod = MOD); (2..self).inject(1) { |f, x| f * x % mod } end\r\n def popcount; x = self; c = 0; while x > 0; c += 1 if x & 1 == 1; x >>= 1 end; c end #TODO: faster\r\n def bitbrute(&block); (1< 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\r\n def lower_bound; ac, wa = self.end_open, self.begin; while ac - wa > 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\r\n def shakutori(r2, &pred); Enumerator.new { |y| j, r = r2.begin, r2.end_open; each { |i| j += 1 while j + 1 < r and pred[i, j+1]; y.yield(i, j) } }; end\r\n def widest(&block); Enumerator.new { |y| j, n = self.begin, self.end_open; each { |i| j += 1 while j < n and block[i, j]; y.yield(i, j) if block[i, j] } } end\r\nend\r\n\r\nclass Array\r\n def power(&block); (0 ... 1 << size).each(&block) end\r\n def sorted_merge(other); a = []; i = j = 0; n, m = size, other.size; if j < m and other[j] < self[i]; a << other[j]; j += 1 else; a << self[i]; i += 1 end while i < n; a.push(*other[j..-1]) if j < m; a end\r\n def upper_bound; ac, wa = 0, size; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\r\n def lower_bound; ac, wa = size, 0; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\r\n def cum(*xs, &op); a = []; a << xs[0] if xs.size > 0; a << x = self[0]; (1...size).each { |i| a << x = op[x, self[i]] }; a end\r\n def cumdiff(range); self[range.end_open] - self[range.begin]; end\r\nend\r\n\r\nmodule Enumerable\r\n def sorted_uniq; x = nil; filter { |y| c = x === y; x = y; !c } end\r\n def cumsum; ys = [0]; each { |x| ys << x + ys[-1] }; ys end\r\nend\r\n\r\nmain"}], "negative_code": [{"source_code": "t = gets.chomp.to_i\r\nt.times do\r\n n, m = gets.chomp.split(' ').map(&:to_i)\r\n arr = gets.chomp.split(' ').map(&:to_i)\r\n brr = gets.chomp.split(' ').map(&:to_i)\r\n crr = gets.chomp.split(' ').map(&:to_i)\r\n\r\n colors = Array.new(n + 1) {Array.new}\r\n missed = Array.new(n + 1) {Array.new}\r\n\r\n cur = arr.dup\r\n res = []\r\n\r\n n.times do |ind|\r\n if arr[ind] != brr[ind]\r\n missed[brr[ind]] << ind\r\n else\r\n colors[arr[ind]] << ind\r\n end\r\n end\r\n\r\n buff = []\r\n crr.each_with_index do |el, ind|\r\n # puts \"Buff = #{buff}\"\r\n # puts \"res = #{res}\"\r\n if missed[el].any?\r\n # puts \"Iter1 = {#{[el, ind].to_s}}\"\r\n t = missed[el].shift\r\n res << t + 1\r\n cur[t] = el\r\n if buff.any?\r\n buff.each do |i|\r\n res[i] = t + 1\r\n end\r\n buff = []\r\n end\r\n colors[el] << t\r\n next\r\n end\r\n if colors[el].any?\r\n # puts \"Iter2 = {#{[el, ind].to_s}}\"\r\n t = colors[el][0]\r\n res << t + 1\r\n cur[t] = el\r\n if buff.any?\r\n buff.each do |i|\r\n res[i] = t + 1\r\n end\r\n buff = []\r\n end\r\n next\r\n end\r\n # puts \"Iter3 = {#{[el, ind].to_s}}\"\r\n buff << ind\r\n res << -1\r\n end\r\n # puts \"RES = #{res}\"\r\n if buff.empty?\r\n puts \"YES\"\r\n puts res.join(' ')\r\n else\r\n puts \"NO\"\r\n end\r\nend"}, {"source_code": "t = gets.chomp.to_i\r\nt.times do\r\n n, m = gets.chomp.split(' ').map(&:to_i)\r\n arr = gets.chomp.split(' ').map(&:to_i)\r\n brr = gets.chomp.split(' ').map(&:to_i)\r\n crr = gets.chomp.split(' ').map(&:to_i)\r\n\r\n colors = Array.new(n + 1) {Array.new}\r\n missed = Array.new(n + 1) {Array.new}\r\n\r\n cur = arr.dup\r\n res = []\r\n\r\n n.times do |el|\r\n if arr[el] != brr[el]\r\n missed[brr[el]] << el\r\n else\r\n colors[arr[el]] << el\r\n end\r\n end\r\n\r\n buff = []\r\n crr.each_with_index do |el, ind|\r\n # puts \"Buff = #{buff}\"\r\n # puts \"res = #{res}\"\r\n if missed[el].any?\r\n # puts \"Iter1 = {#{[el, ind].to_s}}\"\r\n t = missed[el].shift\r\n res << t + 1\r\n cur[t] = el\r\n if buff.any?\r\n buff.each do |i|\r\n res[i] = t + 1\r\n end\r\n buff = []\r\n end\r\n next\r\n end\r\n if colors[el].any?\r\n # puts \"Iter2 = {#{[el, ind].to_s}}\"\r\n t = colors[el][0]\r\n res << t + 1\r\n cur[t] = el\r\n if buff.any?\r\n buff.each do |i|\r\n res[i] = t + 1\r\n end\r\n buff = []\r\n end\r\n next\r\n end\r\n # puts \"Iter3 = {#{[el, ind].to_s}}\"\r\n buff << ind\r\n res << -1\r\n end\r\n # puts \"RES = #{res}\"\r\n if buff.empty?\r\n puts \"YES\"\r\n puts res.join(' ')\r\n else\r\n puts \"NO\"\r\n end\r\nend"}, {"source_code": "t = gets.chomp.to_i\r\nt.times do\r\n n, m = gets.chomp.split(' ').map(&:to_i)\r\n arr = gets.chomp.split(' ').map(&:to_i)\r\n brr = gets.chomp.split(' ').map(&:to_i)\r\n crr = gets.chomp.split(' ').map(&:to_i)\r\n\r\n colors = Array.new(n + 1) {Array.new}\r\n missed = Array.new(n + 1) {Array.new}\r\n\r\n cur = arr.dup\r\n res = []\r\n\r\n n.times do |el|\r\n if arr[el] != brr[el]\r\n missed[brr[el]] << el\r\n else\r\n colors[arr[el]] << el\r\n end\r\n end\r\n\r\n buff = []\r\n crr.each_with_index do |el, ind|\r\n if missed[el].any?\r\n t = missed[el].shift\r\n res << t + 1\r\n cur[t] = el\r\n if buff.any?\r\n i = buff.shift\r\n res[i] = t + 1\r\n end\r\n next\r\n end\r\n if colors[el].any?\r\n t = colors[el][0]\r\n res << t + 1\r\n cur[t] = el\r\n if buff.any?\r\n i = buff.shift\r\n res[i] = t + 1\r\n end\r\n next\r\n end\r\n buff << ind\r\n res << -1\r\n end\r\n if buff.empty?\r\n puts \"YES\"\r\n puts res.join(' ')\r\n else\r\n puts \"NO\"\r\n end\r\nend"}], "src_uid": "a350430c707bb18a146df9f80e114f45"} {"nl": {"description": "Kristina has two arrays $$$a$$$ and $$$b$$$, each containing $$$n$$$ non-negative integers. She can perform the following operation on array $$$a$$$ any number of times: apply a decrement to each non-zero element of the array, that is, replace the value of each element $$$a_i$$$ such that $$$a_i > 0$$$ with the value $$$a_i - 1$$$ ($$$1 \\le i \\le n$$$). If $$$a_i$$$ was $$$0$$$, its value does not change. Determine whether Kristina can get an array $$$b$$$ from an array $$$a$$$ in some number of operations (probably zero). In other words, can she make $$$a_i = b_i$$$ after some number of operations for each $$$1 \\le i \\le n$$$?For example, let $$$n = 4$$$, $$$a = [3, 5, 4, 1]$$$ and $$$b = [1, 3, 2, 0]$$$. In this case, she can apply the operation twice: after the first application of the operation she gets $$$a = [2, 4, 3, 0]$$$; after the second use of the operation she gets $$$a = [1, 3, 2, 0]$$$. Thus, in two operations, she can get an array $$$b$$$ from an array $$$a$$$.", "input_spec": "The first line of the input contains an integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) —the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 5 \\cdot 10^4$$$). The second line of each test case contains exactly $$$n$$$ non-negative integers $$$a_1, a_2, \\dots, a_n$$$ ($$$0 \\le a_i \\le 10^9$$$). The third line of each test case contains exactly $$$n$$$ non-negative integers $$$b_1, b_2, \\dots, b_n$$$ ($$$0 \\le b_i \\le 10^9$$$). It is guaranteed that the sum of $$$n$$$ values over all test cases in the test does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, output on a separate line: YES, if by doing some number of operations it is possible to get an array $$$b$$$ from an array $$$a$$$; NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).", "sample_inputs": ["6\n\n4\n\n3 5 4 1\n\n1 3 2 0\n\n3\n\n1 2 1\n\n0 1 0\n\n4\n\n5 3 7 2\n\n1 1 1 1\n\n5\n\n1 2 3 4 5\n\n1 2 3 4 6\n\n1\n\n8\n\n0\n\n1\n\n4\n\n6"], "sample_outputs": ["YES\nYES\nNO\nNO\nYES\nNO"], "notes": "NoteThe first test case is analyzed in the statement.In the second test case, it is enough to apply the operation to array $$$a$$$ once.In the third test case, it is impossible to get array $$$b$$$ from array $$$a$$$."}, "positive_code": [{"source_code": "#require \"prime\"\n#input of int(split by space)\ndef get_i()\n return gets.chomp.split(\" \").map(&:to_i)\nend\n#input of float(split by space)\ndef get_f()\n return gets.chomp.split(\" \").map(&:to_f)\nend\n#input of string(split by space)\ndef get()\n return gets.chomp.split(\" \")\nend\n#input of string(split per one character)\ndef get_nsp()\n return gets.chomp.split(\"\")\nend\n#yes or no decision\ndef yn_judge(bool,y=\"Yes\",n=\"No\")\n return bool ? y : n \nend\n#output grid(two dimensional array)\ndef puts_grid(grid,join_char=\"\")\n grid.each do|line|\n puts line.join(join_char)\n end\nend\n\nDirxy4=[[1,0],[-1,0],[0,1],[0,-1]]\nDirxy8=[[1,0],[1,-1],[1,1],[0,-1],[0,1],[-1,0],[-1,-1],[-1,1]]\n\nINF=Float::INFINITY\n\n#MOD=998_244_353\nMOD=10**9+7\nT=gets.to_i\nT.times do\n n=gets.to_i\n a=get_i\n b=get_i\n f=true\n diff=-1\n n.times do |i|\n if b[i].zero?\n next\n end\n if a[i]-b[i]>=0\n diff=a[i]-b[i]\n break\n else\n f=false\n break\n end\n end\n if diff==-1\n puts yn_judge(f,\"YES\",\"NO\")\n else\n n.times do |i|\n if b[i].zero?\n unless diff>=a[i]\n f=false\n break\n end\n else\n unless diff==a[i]-b[i]\n f=false\n break\n end\n end\n end\n puts yn_judge(f,\"YES\",\"NO\")\n end\nend"}, {"source_code": "t = gets.to_i\r\n\r\nt.times do\r\n n = gets.to_i\r\n a = gets.split.map(&:to_i)\r\n b = gets.split.map(&:to_i)\r\n\r\n s = -1\r\n n.times do |i|\r\n s = [s, a[i]-b[i]].max\r\n end\r\n\r\n ok = true\r\n n.times do |i|\r\n ai, bi = a[i], b[i]\r\n break ok = false if ai < bi\r\n\r\n k = ai - bi\r\n if bi == 0\r\n break ok = false if k > s\r\n else\r\n break ok = false if k != s\r\n end\r\n end\r\n\r\n puts ok ? \"YES\" : \"NO\"\r\nend\r\n"}, {"source_code": "t = gets.chomp.to_i\nt.times do\n n = gets.chomp.to_i\n a = gets.chomp.split.map(&:to_i)\n b = gets.chomp.split.map(&:to_i)\n r = [0, 2000000000]\n n.times do |i|\n if b[i] > a[i]\n r = [1,0]\n break\n elsif b[i] == 0 # a[i]..\n r = [\n [a[i], r[0]].max,\n [2000000000, r[1]].min\n ]\n else # a[i] - b[i], a[i]-b[i]\n r = [\n [a[i]-b[i], r[0]].max,\n [a[i]-b[i], r[1]].min\n ]\n end\n end\n if r[0] <= r[1]\n puts 'YES'\n else\n puts 'NO'\n end\nend\n"}], "negative_code": [{"source_code": "#require \"prime\"\n#input of int(split by space)\ndef get_i()\n return gets.chomp.split(\" \").map(&:to_i)\nend\n#input of float(split by space)\ndef get_f()\n return gets.chomp.split(\" \").map(&:to_f)\nend\n#input of string(split by space)\ndef get()\n return gets.chomp.split(\" \")\nend\n#input of string(split per one character)\ndef get_nsp()\n return gets.chomp.split(\"\")\nend\n#yes or no decision\ndef yn_judge(bool,y=\"Yes\",n=\"No\")\n return bool ? y : n \nend\n#output grid(two dimensional array)\ndef puts_grid(grid,join_char=\"\")\n grid.each do|line|\n puts line.join(join_char)\n end\nend\n\nDirxy4=[[1,0],[-1,0],[0,1],[0,-1]]\nDirxy8=[[1,0],[1,-1],[1,1],[0,-1],[0,1],[-1,0],[-1,-1],[-1,1]]\n\nINF=Float::INFINITY\n\n#MOD=998_244_353\nMOD=10**9+7\nT=gets.to_i\nT.times do\n n=gets.to_i\n a=get_i\n b=get_i\n if n==1\n if a[0]>=b[0] or b[0].zero?\n puts \"YES\"\n else\n puts \"NO\"\n end\n next\n end\n f=true\n (n-1).times do |i|\n if b[i].zero?\n next\n end\n if b[i+1].zero?\n unless a[i]-b[i]>=a[i+1]-b[i+1]\n f=false\n break\n end\n else\n unless a[i]-b[i]==a[i+1]-b[i+1]\n f=false\n break\n end\n end\n end\n puts yn_judge(f,\"YES\",\"NO\")\nend"}, {"source_code": "#require \"prime\"\n#input of int(split by space)\ndef get_i()\n return gets.chomp.split(\" \").map(&:to_i)\nend\n#input of float(split by space)\ndef get_f()\n return gets.chomp.split(\" \").map(&:to_f)\nend\n#input of string(split by space)\ndef get()\n return gets.chomp.split(\" \")\nend\n#input of string(split per one character)\ndef get_nsp()\n return gets.chomp.split(\"\")\nend\n#yes or no decision\ndef yn_judge(bool,y=\"Yes\",n=\"No\")\n return bool ? y : n \nend\n#output grid(two dimensional array)\ndef puts_grid(grid,join_char=\"\")\n grid.each do|line|\n puts line.join(join_char)\n end\nend\n\nDirxy4=[[1,0],[-1,0],[0,1],[0,-1]]\nDirxy8=[[1,0],[1,-1],[1,1],[0,-1],[0,1],[-1,0],[-1,-1],[-1,1]]\n\nINF=Float::INFINITY\n\n#MOD=998_244_353\nMOD=10**9+7\nT=gets.to_i\nT.times do\n n=gets.to_i\n a=get_i\n b=get_i\n f=true\n (n-1).times do |i|\n if b[i].zero?\n next\n end\n if b[i+1].zero?\n unless a[i]-b[i]>=a[i+1]-b[i+1]\n f=false\n break\n end\n else\n unless a[i]-b[i]==a[i+1]-b[i+1]\n f=false\n break\n end\n end\n end\n puts yn_judge(f,\"YES\",\"NO\")\nend"}, {"source_code": "t = gets.to_i\nt.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n b = gets.split.map(&:to_i)\n\n pair = []\n n.times do |i|\n pair << [a[i], b[i]]\n end\n\n pair.sort_by!{|e| [-e[0], -e[1]]}\n\n ok = true\n s = a[0]-b[0]\n n.times do |i|\n ai, bi = pair[i]\n if ai < bi\n ok = false\n break\n end\n\n if bi == 0\n k = ai - bi\n if k > s\n ok = false\n break\n end\n else\n k = ai - bi\n if k != s\n ok = false\n break\n end\n end\n end\n\n puts ok ? \"YES\" : \"NO\"\nend\n"}], "src_uid": "ee40e8b0ed2da53127f53eb694653a30"} {"nl": {"description": "Edogawa Conan got tired of solving cases, and invited his friend, Professor Agasa, over. They decided to play a game of cards. Conan has n cards, and the i-th card has a number ai written on it.They take turns playing, starting with Conan. In each turn, the player chooses a card and removes it. Also, he removes all cards having a number strictly lesser than the number on the chosen card. Formally, if the player chooses the i-th card, he removes that card and removes the j-th card for all j such that aj < ai.A player loses if he cannot make a move on his turn, that is, he loses if there are no cards left. Predict the outcome of the game, assuming both players play optimally.", "input_spec": "The first line contains an integer n (1 ≤ n ≤ 105) — the number of cards Conan has. The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 105), where ai is the number on the i-th card.", "output_spec": "If Conan wins, print \"Conan\" (without quotes), otherwise print \"Agasa\" (without quotes).", "sample_inputs": ["3\n4 5 7", "2\n1 1"], "sample_outputs": ["Conan", "Agasa"], "notes": "NoteIn the first example, Conan can just choose the card having number 7 on it and hence remove all the cards. After that, there are no cards left on Agasa's turn.In the second example, no matter which card Conan chooses, there will be one one card left, which Agasa can choose. After that, there are no cards left when it becomes Conan's turn again."}, "positive_code": [{"source_code": "gets\nc=0\nh=Hash.new 0\ngets.split.each{|e|h[e]=d=h[e]+1;c+=d%2*2-1}\nputs c>0 ? :Conan : :Agasa"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\na.sort!\nk = a.pop\nflag = false # Conan wins\nwhile !flag && k\n cnt = 1\n while k == (l = a.pop)\n cnt += 1\n end\n flag = cnt % 2 != 0\n k = l\nend\nputs flag ? \"Conan\" : \"Agasa\"\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i).sort.reverse\n\noddcnt = a.group_by{|x| x}.map{|k,v| v.size}.select{|t|t%2==1}.size\n\nputs (oddcnt>0)? 'Conan' : 'Agasa' "}, {"source_code": "gets;c=0;gets.split.inject(Hash.new(0)){|h,k|h[k]+=1;h}.each_value{|x|c+=x%2};puts c>0?\"Conan\":\"Agasa\""}], "negative_code": [{"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\na.sort!\nk = a[-1]\nflag = false # Conan wins\nwhile !flag && k\n cnt = 0\n while k == (l = a.pop)\n cnt += 1\n end\n flag = cnt % 2 != 0\n k = l\nend\nputs flag ? \"Conan\" : \"Agasa\"\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\na.sort!\nk = a.pop\nwhile k && (a.count(k) % 2 == 1)\n a.delete(k)\n k = a.pop\nend\nputs a.length == 0 ? \"Agasa\" : \"Conan\"\n"}], "src_uid": "864593dc3911206b627dab711025e116"} {"nl": {"description": "Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on ​​the scales or to say that it can't be done.", "input_spec": "The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals \"1\" if Xenia has i kilo weights, otherwise the character equals \"0\". The second line contains integer m (1 ≤ m ≤ 1000).", "output_spec": "In the first line print \"YES\", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line \"NO\". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales. If there are multiple solutions, you can print any of them.", "sample_inputs": ["0000000101\n3", "1000000000\n2"], "sample_outputs": ["YES\n8 10 8", "NO"], "notes": null}, "positive_code": [{"source_code": "@s = '0'+gets.strip\n@m = gets.to_i\n@calced = []\n@f = []\nfor i in 0..10 do\n @calced[i]=[]\n @f[i]=[]\n for j in 0..@m do\n @calced[i][j]=[]\n @f[i][j] = []\n end\nend\n\ndef dp(delta,step,last)\n if !@calced[delta][step][last]\n @calced[delta][step][last]=true\n @f[delta][step][last]=false\n if step==@m\n @f[delta][step][last]=true\n else\n for i in delta+1..10 do\n if i!=last && @s[i]=='1' && dp(i-delta,step+1,i)\n @f[delta][step][last]=true\n break\n end\n end\n end\n end\n return @f[delta][step][last]\nend\n\ndef show(delta,step,last)\n if step==@m\n puts last\n else\n if step==0\n puts 'YES'\n else\n print last,' '\n end\n for i in delta+1..10 do\n if i!=last && @s[i]=='1' && dp(i-delta,step+1,i)\n show(i-delta,step+1,i)\n break\n end\n end\n end\nend\n\nif !dp(0,0,0)\n puts 'NO'\nelse\n show(0,0,0)\nend"}, {"source_code": "#!/usr/bin/ruby1.9\n\ns = STDIN.readline.strip\nm = STDIN.readline.to_i\nweights = []\n10.times do |pos|\n\tif s[pos] == '1'\n\t\tweights.push(pos+1)\n\tend\nend\n\ndef solve(solution, weights, left, right, ix, max, previous)\n\treturn true if ix == max\n\tweights.each do |weight|\n\t\tnext if weight == previous\n\t\tnext if left+weight <= right\n\t\tsolution.push(weight)\n\t\tif solve(solution, weights, right, left+weight, ix+1, max, weight)\n\t\t\treturn true\n\t\tend\n\t\tsolution.pop\n\tend\n\treturn false\nend\n\nsolution = []\nif solve(solution, weights, 0, 0, 0, m, -1)\n\tputs 'YES'\n\tputs solution.join(' ')\nelse\n\tputs 'NO'\nend\n\n"}], "negative_code": [{"source_code": "#!/usr/bin/ruby1.9\n\ns = STDIN.readline.strip\nm = STDIN.readline.to_i\nweights = []\n10.times do |pos|\n\tif s[pos] == '1'\n\t\tweights.push(pos+1)\n\tend\nend\n\ndef solve(solution, weights, left, right, ix, max, previous)\n\treturn true if ix == max\n\tweights.each do |weight|\n\t\tnext if weight == previous\n\t\tnext if left+weight <= right\n\t\tsolution.push(weight)\n\t\treturn true if solve(solution, weights, right, left, ix+1, max, weight)\n\t\tsolution.pop\n\tend\n\treturn false\nend\n\nsolution = []\nif solve(solution, weights, 0, 0, 0, m, -1)\n\tputs 'YES'\n\tputs solution.join(' ')\nelse\n\tputs 'NO'\nend\n\n"}], "src_uid": "15aa3adb14c17023f71eec11e1c32efe"} {"nl": {"description": "This is the easy version of the problem. The difference between the versions is in the number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem.You are given a binary table of size $$$n \\times m$$$. This table consists of symbols $$$0$$$ and $$$1$$$.You can make such operation: select $$$3$$$ different cells that belong to one $$$2 \\times 2$$$ square and change the symbols in these cells (change $$$0$$$ to $$$1$$$ and $$$1$$$ to $$$0$$$).Your task is to make all symbols in the table equal to $$$0$$$. You are allowed to make at most $$$3nm$$$ operations. You don't need to minimize the number of operations.It can be proved that it is always possible.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 5000$$$) — the number of test cases. The next lines contain descriptions of test cases. The first line of the description of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \\leq n, m \\leq 100$$$). Each of the next $$$n$$$ lines contains a binary string of length $$$m$$$, describing the symbols of the next row of the table. It is guaranteed that the sum of $$$nm$$$ for all test cases does not exceed $$$20000$$$.", "output_spec": "For each test case print the integer $$$k$$$ ($$$0 \\leq k \\leq 3nm$$$) — the number of operations. In the each of the next $$$k$$$ lines print $$$6$$$ integers $$$x_1, y_1, x_2, y_2, x_3, y_3$$$ ($$$1 \\leq x_1, x_2, x_3 \\leq n, 1 \\leq y_1, y_2, y_3 \\leq m$$$) describing the next operation. This operation will be made with three cells $$$(x_1, y_1)$$$, $$$(x_2, y_2)$$$, $$$(x_3, y_3)$$$. These three cells should be different. These three cells should belong into some $$$2 \\times 2$$$ square.", "sample_inputs": ["5\n2 2\n10\n11\n3 3\n011\n101\n110\n4 4\n1111\n0110\n0110\n1111\n5 5\n01011\n11001\n00010\n11011\n10000\n2 3\n011\n101"], "sample_outputs": ["1\n1 1 2 1 2 2\n2 \n2 1 3 1 3 2\n1 2 1 3 2 3\n4\n1 1 1 2 2 2 \n1 3 1 4 2 3\n3 2 4 1 4 2\n3 3 4 3 4 4\n4\n1 2 2 1 2 2 \n1 4 1 5 2 5 \n4 1 4 2 5 1\n4 4 4 5 3 4\n2\n1 3 2 2 2 3\n1 2 2 1 2 2"], "notes": "NoteIn the first test case, it is possible to make only one operation with cells $$$(1, 1)$$$, $$$(2, 1)$$$, $$$(2, 2)$$$. After that, all symbols will be equal to $$$0$$$.In the second test case: operation with cells $$$(2, 1)$$$, $$$(3, 1)$$$, $$$(3, 2)$$$. After it the table will be: 011001000 operation with cells $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$. After it the table will be: 000000000 In the fifth test case: operation with cells $$$(1, 3)$$$, $$$(2, 2)$$$, $$$(2, 3)$$$. After it the table will be: 010110 operation with cells $$$(1, 2)$$$, $$$(2, 1)$$$, $$$(2, 2)$$$. After it the table will be: 000000 "}, "positive_code": [{"source_code": "\ndef apply(x1, x2, x3, x4, op)\n op.each do |el|\n x1 = 1 - x1 if el.include?([1, 1])\n x2 = 1 - x2 if el.include?([1, 2])\n x3 = 1 - x3 if el.include?([2, 1])\n x4 = 1 - x4 if el.include?([2, 2])\n end\n return [x1, x2, x3, x4]\nend\n\ndef fix_sq(xs, op)\n num_o = xs.sum\n xs_z = xs.zip [[1, 1], [1, 2], [2, 1], [2, 2]]\n case\n when num_o == 0\n return op\n when num_o == 4\n new_op = [[2, 1], [1, 2], [1, 1]]\n op << new_op\n return fix_sq(apply(*xs, [new_op]), op)\n when num_o == 3\n new_op = xs_z.filter {|el| el[0] == 1}.map {|el| el[1]}\n op << new_op\n return op\n when num_o == 2\n new_op = xs_z.filter {|el| el[0] == 0}.map {|el| el[1]}\n new_op << xs_z.find {|el| el[0] == 1}[1]\n op << new_op\n return fix_sq(apply(*xs, [new_op]), op)\n when num_o == 1\n new_op = xs_z.filter {|el| el[0] == 0}.map {|el| el[1]}.first(2)\n new_op << xs_z.find {|el| el[0] == 1}[1]\n op << new_op\n return fix_sq(apply(*xs, [new_op]), op)\n end\nend\n\nt = gets.chomp.to_i\nt.times do\n n, m = gets.chomp.split(' ').map(&:to_i)\n arr = []\n n.times do\n arr << gets.chomp.split('').map(&:to_i)\n end\n res = []\n row = 0\n while row < n\n col = 0\n row = [row + 2, n].min\n while col < m\n col = [col + 2, m].min\n temp = fix_sq(\n [\n arr[row - 2][col - 2], \n arr[row - 2][col - 1],\n arr[row - 1][col - 2],\n arr[row - 1][col - 1]\n ],[])\n res += temp.map{|op| op.map{|el| [el[0] + row - 2, el[1] + col - 2]}}\n arr[row - 2][col - 2] = \n arr[row - 2][col - 1] = \n arr[row - 1][col - 2] = \n arr[row - 1][col - 1] = 0\n end\n end\n puts res.size\n res.each do |el|\n puts el.flatten.join(' ')\n end\nend\n"}], "negative_code": [{"source_code": "\ndef apply(x1, x2, x3, x4, op)\n op.each do |el|\n x1 = 1 - x1 if el.include?([1, 1])\n x2 = 1 - x2 if el.include?([1, 2])\n x3 = 1 - x3 if el.include?([2, 1])\n x4 = 1 - x4 if el.include?([2, 2])\n end\n return [x1, x2, x3, x4]\nend\n\ndef fix_sq(xs, op)\n num_o = xs.sum\n xs_z = xs.zip [[1, 1], [1, 2], [2, 1], [2, 2]]\n case\n when num_o == 0\n return op\n when num_o == 4\n new_op = [[2, 1], [1, 2], [1, 1]]\n op << new_op\n return fix_sq(apply(*xs, [new_op]), op)\n when num_o == 3\n new_op = xs_z.filter {|el| el[0] == 1}.map {|el| el[1]}\n op << new_op\n return op\n when num_o == 2\n new_op = xs_z.filter {|el| el[0] == 0}.map {|el| el[1]}\n new_op << xs_z.find {|el| el[0] == 1}[1]\n op << new_op\n return fix_sq(apply(*xs, [new_op]), op)\n when num_o == 1\n new_op = xs_z.filter {|el| el[0] == 0}.map {|el| el[1]}.first(2)\n new_op << xs_z.find {|el| el[0] == 1}[1]\n op << new_op\n return fix_sq(apply(*xs, [new_op]), op)\n end\nend\n\nt = gets.chomp.to_i\nt.times do\n n, m = gets.chomp.split(' ').map(&:to_i)\n arr = []\n n.times do\n arr << gets.chomp.split('').map(&:to_i)\n end\n res = []\n row = 0\n while row < n\n col = 0\n row = [row + 2, n].min\n while col < m\n col = [col + 2, m].min\n temp = fix_sq(\n [\n arr[row - 2][col - 2], \n arr[row - 2][col - 1],\n arr[row - 1][col - 2],\n arr[row - 1][col - 1]\n ],[])\n res += temp.map{|op| op.map{|el| [el[0] + row - 2, el[1] + col - 2]}}\n end\n end\n puts res.size\n res.each do |el|\n puts el.flatten.join(' ')\n end\nend\n"}, {"source_code": "\ndef apply(x1, x2, x3, x4, op)\n op.each do |el|\n x1 = 1 - x1 if el.include?([1, 1])\n x2 = 1 - x2 if el.include?([2, 1])\n x3 = 1 - x3 if el.include?([1, 2])\n x4 = 1 - x4 if el.include?([2, 2])\n end\n return [x1, x2, x3, x4]\nend\n\ndef fix_sq(xs, op)\n num_o = xs.sum\n xs_z = xs.zip [[1, 1], [2, 1], [1, 2], [2, 2]]\n case\n when num_o == 0\n return op\n when num_o == 4\n new_op = [[2, 1], [1, 2], [1, 1]]\n op << new_op\n return fix_sq(apply(*xs, [new_op]), op)\n when num_o == 3\n new_op = xs_z.filter {|el| el[0] == 1}.map {|el| el[1]}\n op << new_op\n return op\n when num_o == 2\n new_op = xs_z.filter {|el| el[0] == 0}.map {|el| el[1]}\n new_op << xs_z.find {|el| el[0] == 1}[1]\n op << new_op\n return fix_sq(apply(*xs, [new_op]), op)\n when num_o == 1\n new_op = xs_z.filter {|el| el[0] == 0}.map {|el| el[1]}.first(2)\n new_op << xs_z.find {|el| el[0] == 1}[1]\n op << new_op\n return fix_sq(apply(*xs, [new_op]), op)\n end\nend\n\nt = gets.chomp.to_i\nt.times do\n n, m = gets.chomp.split(' ').map(&:to_i)\n arr = []\n n.times do\n arr << gets.chomp.split('').map(&:to_i)\n end\n res = []\n row = 0\n while row < n\n col = 0\n row = [row + 2, n].min\n while col < m\n col = [col + 2, m].min\n temp = fix_sq(\n [\n arr[row - 2][col - 2], \n arr[row - 2][col - 1],\n arr[row - 1][col - 2],\n arr[row - 1][col - 1]\n ],[])\n res += temp.map{|op| op.map{|el| [el[0] + row - 2, el[1] + col - 2]}}\n end\n end\n puts res.size\n res.each do |el|\n puts el.flatten.join(' ')\n end\nend\n"}], "src_uid": "e86044b3438f934b6a9e76854053f117"} {"nl": {"description": "Programmer Vasya is studying a new programming language &K*. The &K* language resembles the languages of the C family in its syntax. However, it is more powerful, which is why the rules of the actual C-like languages are unapplicable to it. To fully understand the statement, please read the language's description below carefully and follow it and not the similar rules in real programming languages.There is a very powerful system of pointers on &K* — you can add an asterisk to the right of the existing type X — that will result in new type X * . That is called pointer-definition operation. Also, there is the operation that does the opposite — to any type of X, which is a pointer, you can add an ampersand — that will result in a type &X, to which refers X. That is called a dereference operation.The &K* language has only two basic data types — void and errtype. Also, the language has operators typedef and typeof. The operator \"typedef A B\" defines a new data type B, which is equivalent to A. A can have asterisks and ampersands, and B cannot have them. For example, the operator typedef void** ptptvoid will create a new type ptptvoid, that can be used as void**. The operator \"typeof A\" returns type of A, brought to void, that is, returns the type void**...*, equivalent to it with the necessary number of asterisks (the number can possibly be zero). That is, having defined the ptptvoid type, as shown above, the typeof ptptvoid operator will return void**.An attempt of dereferencing of the void type will lead to an error: to a special data type errtype. For errtype the following equation holds true: errtype* = &errtype = errtype. An attempt to use the data type that hasn't been defined before that will also lead to the errtype.Using typedef, we can define one type several times. Of all the definitions only the last one is valid. However, all the types that have been defined earlier using this type do not change.Let us also note that the dereference operation has the lower priority that the pointer operation, in other words &T *  is always equal to T.Note, that the operators are executed consecutively one by one. If we have two operators \"typedef &void a\" and \"typedef a* b\", then at first a becomes errtype, and after that b becomes errtype* = errtype, but not &void* = void (see sample 2).Vasya does not yet fully understand this powerful technology, that's why he asked you to help him. Write a program that analyzes these operators. ", "input_spec": "The first line contains an integer n (1 ≤ n ≤ 100) — the number of operators. Then follow n lines with operators. Each operator is of one of two types: either \"typedef A B\", or \"typeof A\". In the first case the B type differs from void and errtype types, and besides, doesn't have any asterisks and ampersands. All the data type names are non-empty lines of no more than 20 lowercase Latin letters. The number of asterisks and ampersands separately in one type in any operator does not exceed 10, however if we bring some types to void with several asterisks, their number may exceed 10.", "output_spec": "For every typeof operator print on the single line the answer to that operator — the type that the given operator returned.", "sample_inputs": ["5\ntypedef void* ptv\ntypeof ptv\ntypedef &&ptv node\ntypeof node\ntypeof &ptv", "17\ntypedef void* b\ntypedef b* c\ntypeof b\ntypeof c\ntypedef &b b\ntypeof b\ntypeof c\ntypedef &&b* c\ntypeof c\ntypedef &b* c\ntypeof c\ntypedef &void b\ntypeof b\ntypedef b******* c\ntypeof c\ntypedef &&b* c\ntypeof c"], "sample_outputs": ["void*\nerrtype\nvoid", "void*\nvoid**\nvoid\nvoid**\nerrtype\nvoid\nerrtype\nerrtype\nerrtype"], "notes": "NoteLet's look at the second sample.After the first two queries typedef the b type is equivalent to void*, and с — to void**.The next query typedef redefines b — it is now equal to &b = &void* = void. At that, the с type doesn't change.After that the с type is defined as &&b* = &&void* = &void = errtype. It doesn't influence the b type, that's why the next typedef defines c as &void* = void.Then the b type is again redefined as &void = errtype. Please note that the c type in the next query is defined exactly as errtype******* = errtype, and not &void******* = void******. The same happens in the last typedef."}, "positive_code": [{"source_code": "def parse(type, values)\n type =~ /^(\\&*)(.*?)(\\**)$/\n amp = $1\n base = $2\n star = $3\n if base == 'errtype'\n return 'errtype'\n elsif base != 'void'\n if !values[base]\n return 'errtype'\n end\n type = \"#{amp}#{values[base]}#{star}\"\n type =~ /^(\\&*)(.*?)(\\**)$/\n amp = $1\n base = $2\n star = $3\n\n if base == 'errtype'\n return 'errtype'\n end\n end\n\n s = star.size - amp.size\n if s < 0\n return 'errtype'\n else\n return base + \"*\" * s\n end\nend\n\nvalues = {}\n\ngets.to_i.times{\n l = gets.chomp\n if l =~ /typedef (\\S+) (\\S+)/\n type = $1\n name = $2\n values[name] = parse(type, values)\n elsif l =~ /typeof (\\S+)/\n type = $1\n puts parse(type, values)\n else\n raise\n end\n}\n"}, {"source_code": "h = {void: \"void\", errtype: \"errtype\"}\ngets.to_i.times do\n s = gets.split\n if s[0] == \"typeof\"\n s[1].match(/(&*)([^&*]+)(\\**)/)\n a, b, c = $1.size, $2.to_sym, $3.size\n if h[b].nil? || h[b] == \"errtype\" || a > c + h[b].scan(/\\*/).size\n puts :errtype\n else\n if a >= c\n puts h[b][0..-(a - c + 1)]\n else\n puts h[b] + \"*\" * (c - a)\n end\n end\n else\n s[2] = s[2].to_sym\n s[1].match(/(&*)([^&*]+)(\\**)/)\n a, b, c = $1.size, $2.to_sym, $3.size\n if h[b].nil? || h[b] == \"errtype\" || a > c + h[b].scan(/\\*/).size\n h[s[2]] = \"errtype\"\n else\n if a >= c\n h[s[2]] = h[b][0..-(a - c + 1)]\n else\n h[s[2]] = h[b] + \"*\" * (c - a)\n end\n end\n end\nend\n"}, {"source_code": "\nn = STDIN.readline.to_i\nregex = /^(&*)(.+?)(\\**)$/\ndict = Hash.new\ndict['void'] = 0\nn.times do\n s = STDIN.readline.split ' '\n array = regex.match(s[1])\n head = array[1].length\n body = array[2]\n tail = array[3].length\n if s[0] == \"typedef\" then\n base = if dict.include? body then \n dict[body] \n else \n nil \n end\n dict[s[2]] = if base == nil or base + tail - head < 0 then \n nil\n else \n base + tail - head\n end\n else\n if (!dict.include? body) || (!dict[body]) then\n puts 'errtype'\n else\n len = dict[body] + tail - head\n if len >= 0 then\n puts 'void' + ('*' * len)\n else \n puts 'errtype'\n end\n end\n end\nend\n"}], "negative_code": [{"source_code": "h = {void: \"void\", errtype: \"errtype\"}\ngets.to_i.times do\n s = gets.split\n if s[0] == \"typeof\"\n if s[1].match(/(&*)(#{h.keys.join(?|)})(\\**)/)\n a, b, c = $1.size, $2.to_sym, $3.size\n if h[b].nil? || h[b] == \"errtype\" || a > c + h[b].scan(/\\*/).size\n puts :errtype\n else\n if a >= c\n puts h[b][0..-(a - c + 1)]\n else\n puts h[b] + \"*\" * (c - a)\n end\n end\n else\n puts :errtype\n end\n else\n s[2] = s[2].to_sym\n if s[1].match(/(&*)(#{h.keys.join(?|)})(\\**)/)\n a, b, c = $1.size, $2.to_sym, $3.size\n if h[b].nil? || h[b] == \"errtype\" || a > c + h[b].scan(/\\*/).size\n h[s[2]] = \"errtype\"\n else\n if a >= c\n h[s[2]] = h[b][0..-(a - c + 1)]\n else\n h[s[2]] = h[b] + \"*\" * (c - a)\n end\n end\n else\n h[s[2]] = \"errtype\"\n end\n end\nend\n"}, {"source_code": "\nn = STDIN.readline.to_i\nregex = /^(&*)(.+?)(\\**)$/\ndict = Hash.new\nn.times do\n s = STDIN.readline.split ' '\n if s[0] == \"typedef\" then\n array = regex.match(s[1])\n base = if array[2] == 'void' then 0 \n elsif dict.include?(array[2]) then dict[array[2]] \n else nil \n end\n if base == nil then\n dict[s[2]] = nil\n elsif base + array[3].length - array[1].length < 0 then\n dict[s[2]] = nil\n else\n dict[s[2]] = base + array[3].length - array[1].length\n end\n else\n array = regex.match(s[1])\n if !dict.include?(array[2]) || !dict[array[2]] then\n puts 'errtype'\n else\n# p array\n len = dict[array[2]] + array[3].length - array[1].length\n if len >= 0 then\n puts 'void' + ('*' * len)\n else \n puts 'errtype'\n end\n end\n end\nend\n"}], "src_uid": "b260eb188103758a74b600d8bb46fffe"} {"nl": {"description": "One day little Vasya found mom's pocket book. The book had n names of her friends and unusually enough, each name was exactly m letters long. Let's number the names from 1 to n in the order in which they are written.As mom wasn't home, Vasya decided to play with names: he chose three integers i, j, k (1 ≤ i < j ≤ n, 1 ≤ k ≤ m), then he took names number i and j and swapped their prefixes of length k. For example, if we take names \"CBDAD\" and \"AABRD\" and swap their prefixes with the length of 3, the result will be names \"AABAD\" and \"CBDRD\".You wonder how many different names Vasya can write instead of name number 1, if Vasya is allowed to perform any number of the described actions. As Vasya performs each action, he chooses numbers i, j, k independently from the previous moves and his choice is based entirely on his will. The sought number can be very large, so you should only find it modulo 1000000007 (109 + 7).", "input_spec": "The first input line contains two integers n and m (1 ≤ n, m ≤ 100) — the number of names and the length of each name, correspondingly. Then n lines contain names, each name consists of exactly m uppercase Latin letters.", "output_spec": "Print the single number — the number of different names that could end up in position number 1 in the pocket book after the applying the procedures described above. Print the number modulo 1000000007 (109 + 7).", "sample_inputs": ["2 3\nAAB\nBAA", "4 5\nABABA\nBCGDG\nAAAAA\nYABSA"], "sample_outputs": ["4", "216"], "notes": "NoteIn the first sample Vasya can get the following names in the position number 1: \"AAB\", \"AAA\", \"BAA\" and \"BAB\"."}, "positive_code": [{"source_code": "require 'set'\n\nn,m = gets.chomp.split(/ /).map!{|x| x.to_i}\ns = []\nfor i in 0..n-1\n\ts << gets.chomp\nend\nret = 1\nfor i in 0..m-1\n\tws = Set.new\n\tfor j in 0..n-1\n\t\tws.add(s[j][i])\n\tend\n\tret *= ws.length\nend\nret %= 1000000007\nputs ret"}, {"source_code": "n, m = gets.split.map(&:to_i)\np Array.new(m).zip(*(n.times.map{gets.chars.to_a})).map{|_| _.uniq.size - 1}.inject(&:*) % 1000000007\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\np n.times.map{gets.chars.to_a}.transpose.map{|_| _.uniq.size}.inject(&:*) % 1000000007\n"}, {"source_code": "n,m=gets.split.map(&:to_i)\nans=1\ninput=[]\nn.times{input+=[gets.split[0]]}\nm.times{|i|\n\ta=Hash.new\n\tn.times{|j|\n\t\ta[input[j][i]]=true\n\t}\n\tans*=a.size\n\tans%=1000000007\n}\np ans"}, {"source_code": "#!/usr/bin/env ruby\n\nMOD = 1000000007\nn, m = gets.split.map(&:to_i)\ntranspose = Array.new(m) { {} }\nn.times do\n gets.chomp.each_char.to_a.each_with_index do |ch, idx|\n transpose[idx][ch] = 1\n end\nend\n\ncnt = 1\ntranspose.each do |set|\n cnt *= set.size\n cnt %= MOD\nend\n\np cnt\n"}, {"source_code": "a=gets.chomp.split(' ')\na.length.times {|x| a[x]=a[x].to_i}\nb=[]\nc=[]\na[0].times do |x|\n b< m\n puts -1\n return\n end\n\n i = 0\n\n while sum > m\n sum -= diffs[i]\n i += 1\n end\n\n puts i\n\nend\n\ndef read_int\n gets.to_i\nend\n\ndef read_ints\n gets.split.map(&:to_i)\nend\n\ndef read_string\n gets.chomp\nend\n\nsolution unless ENV['TEST__MODE']\n"}, {"source_code": "N, M = gets.split.map(&:to_i)\nas, bs = [], []\nN.times do |i|\n as[i], bs[i] = gets.split.map(&:to_i)\nend\n\nsum = as.inject(:+)\ngains = [0] + N.times.map{|i| as[i] - bs[i]}.sort.reverse\n(N+1).times do |i|\n sum -= gains[i]\n if sum <= M\n puts i; exit\n end\nend\nputs -1"}, {"source_code": "n, m = gets.strip.split.map(&:to_i)\n\nsum = 0\nary = []\nn.times do\n a, b = gets.strip.split.map(&:to_i)\n ary << [a,b]\n sum += a\nend\n\nary = ary.sort_by{|x| x[1]-x[0]}\ni = 0\nans = 0\nwhile sum > m && i m\n puts \"-1\"\nelse\n puts ans\nend"}, {"source_code": "n= Array.new\nn = gets.chomp.split(\" \").each.map(&:to_i)\n \na= Array.new\nb= Array.new\nc=0\nsum=0\n\nwhile n[0]>0\n a = gets.chomp.split(\" \").each.map(&:to_i)\n sum+=a[0]\n b.push((a[0]-a[1]))\n n[0]-=1\nend\n\nb= b.sort\n\nwhile n[1]n[1])\n print(-1)\nelse\n print(c)\nend\n"}], "negative_code": [{"source_code": "\nSong = Struct.new(:a, :b) do\n def diff\n self.a - self.b\n end\nend\n\ndef solution\n\n n, m = read_ints\n\n songs = []\n\n sum = 0\n\n n.times do\n a, b = read_ints\n sum += a\n songs << Song.new(a, b)\n end\n\n songs = songs.sort_by(&:diff).reverse\n\n i = 0\n\n while sum > m && i < songs.size\n sum -= songs[i].diff\n i += 1\n end\n\n if sum > m\n puts -1\n else\n puts i\n end\n\nend\n\ndef read_int\n gets.to_i\nend\n\ndef read_ints\n gets.split.map(&:to_i)\nend\n\ndef read_string\n gets.chomp\nend\n"}, {"source_code": "\nSong = Struct.new(:a, :b) do\n def diff\n self.a - self.b\n end\nend\n\ndef solution\n\n n, m = read_ints\n\n songs = []\n\n sum = 0\n\n n.times do\n a, b = read_ints\n sum += a\n songs << Song.new(a, b)\n end\n\n songs = songs.sort_by(&:diff)\n\n i = 0\n\n while sum > m && i < songs.size\n sum -= songs[i].diff\n i += 1\n end\n\n if sum > m\n puts -1\n else\n puts i - 1\n end\n\nend\n\ndef read_int\n gets.to_i\nend\n\ndef read_ints\n gets.split.map(&:to_i)\nend\n\ndef read_string\n gets.chomp\nend\n\nsolution unless ENV['TEST__MODE']\n\n"}], "src_uid": "91541d10c5ae52be8da33412359bd019"} {"nl": {"description": "You are given three positive integers $$$a$$$, $$$b$$$, $$$c$$$ ($$$a < b < c$$$). You have to find three positive integers $$$x$$$, $$$y$$$, $$$z$$$ such that:$$$$$$x \\bmod y = a,$$$$$$ $$$$$$y \\bmod z = b,$$$$$$ $$$$$$z \\bmod x = c.$$$$$$Here $$$p \\bmod q$$$ denotes the remainder from dividing $$$p$$$ by $$$q$$$. It is possible to show that for such constraints the answer always exists.", "input_spec": "The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10\\,000$$$) — the number of test cases. Description of the test cases follows. Each test case contains a single line with three integers $$$a$$$, $$$b$$$, $$$c$$$ ($$$1 \\le a < b < c \\le 10^8$$$).", "output_spec": "For each test case output three positive integers $$$x$$$, $$$y$$$, $$$z$$$ ($$$1 \\le x, y, z \\le 10^{18}$$$) such that $$$x \\bmod y = a$$$, $$$y \\bmod z = b$$$, $$$z \\bmod x = c$$$. You can output any correct answer.", "sample_inputs": ["4\n1 3 4\n127 234 421\n2 7 8\n59 94 388"], "sample_outputs": ["12 11 4\n1063 234 1484\n25 23 8\n2221 94 2609"], "notes": "NoteIn the first test case:$$$$$$x \\bmod y = 12 \\bmod 11 = 1;$$$$$$$$$$$$y \\bmod z = 11 \\bmod 4 = 3;$$$$$$$$$$$$z \\bmod x = 4 \\bmod 12 = 4.$$$$$$"}, "positive_code": [{"source_code": "gets.to_i.times do\r\n a, b, c = gets.split.map(&:to_i)\r\n x, y, z = b * 10 ** 9 + a, b, c\r\n puts \"#{x} #{y} #{z}\"\r\nend\r\n"}], "negative_code": [], "src_uid": "f0c22161cb5a9bc17320ccd05517f867"} {"nl": {"description": "A median in an array with the length of n is an element which occupies position number after we sort the elements in the non-decreasing order (the array elements are numbered starting with 1). A median of an array (2, 6, 1, 2, 3) is the number 2, and a median of array (0, 96, 17, 23) — the number 17.We define an expression as the integer part of dividing number a by number b.One day Vasya showed Petya an array consisting of n integers and suggested finding the array's median. Petya didn't even look at the array and said that it equals x. Petya is a very honest boy, so he decided to add several numbers to the given array so that the median of the resulting array would be equal to x.Petya can add any integers from 1 to 105 to the array, including the same numbers. Of course, he can add nothing to the array. If a number is added multiple times, then we should consider it the number of times it occurs. It is not allowed to delete of change initial numbers of the array. While Petya is busy distracting Vasya, your task is to find the minimum number of elements he will need.", "input_spec": "The first input line contains two space-separated integers n and x (1 ≤ n ≤ 500, 1 ≤ x ≤ 105) — the initial array's length and the required median's value. The second line contains n space-separated numbers — the initial array. The elements of the array are integers from 1 to 105. The array elements are not necessarily different.", "output_spec": "Print the only integer — the minimum number of elements Petya needs to add to the array so that its median equals x.", "sample_inputs": ["3 10\n10 20 30", "3 4\n1 2 3"], "sample_outputs": ["1", "4"], "notes": "NoteIn the first sample we can add number 9 to array (10, 20, 30). The resulting array (9, 10, 20, 30) will have a median in position , that is, 10.In the second sample you should add numbers 4, 5, 5, 5. The resulting array has median equal to 4."}, "positive_code": [{"source_code": "n,xx = gets.chomp.split(/ /).map!{|x| x.to_i}\na = gets.chomp.split(/ /).map!{|x| x.to_i}\nret = 0\nif not a.include?(xx)\n ret = 1\n a << xx\nend\na.sort!\nm = (a.length() + 1)/2\n\nminxx = a.index(xx)\nmaxxx = a.rindex(xx)\nif minxx > (m-1)\n ret += (minxx - (a.length() - (minxx+1)))\nelsif maxxx < (m-1)\n ret += ((a.length() - (maxxx+1)) - maxxx - 1)\nend\nputs ret\n"}, {"source_code": "I = ->{gets.split.map(&:to_i)}\nn, m = I[]\nc = [0] * 3\nI[].each{|_| c[(_ <=> m) + 1] += 1}\nd = 0\nif c[1] == 0\n c[1] += 1\n d += 1\nend\nd += [(c[0] > c[2] ? c[0] - c[2] + 1 : c[2] - c[0]) - c[1], 0].max\np d\n"}, {"source_code": "n, x = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\nans = 0\nunless a.include?(x)\n a.push(x)\n ans += 1\nend\na.sort!\nm = (a.size()+1)/2-1\n\nwhile a[m]>x\n a.unshift(-1000)\n m = (a.size()+1)/2-1\n ans+=1\nend\n\nwhile a[m] median\n rcnt = 0\n target = ni\n\n if arr.size.even?\n rcnt += 1\n target -= 1\n end\n rcnt += (target-median) * 2\nend\n\n# left adjustment\nlcnt = n*100\nif ri < median\n lcnt = 0\n target = ri\n\n if arr.size.odd?\n lcnt += 1\n target += 1\n end\n lcnt += (median-target) * 2\nend\n\np [rcnt,lcnt].min + cnt\n"}, {"source_code": "def mid(x)\n\t(x-1)/2\nend\nn, x = gets.split(/\\s+/).map(&:to_i)\na = gets.split(/\\s+/).map(&:to_i).sort\nres = []\n1000.times do |i|\n\tres << i if a[mid(a.size)] == x\n\ta << x\n\ta.sort!\nend\np res.min\n"}, {"source_code": "$n, $m = gets.split().map(&:to_i)\n\n$a = gets.split().map(&:to_i)\n$a.sort!\ndef gg()\n return 0 if $a[$a.length/2] == $m\n ls = 0\n gt = 0\n eq = 0\n $a.each do |i|\n if i < $m\n ls = ls + 1\n elsif i == $m\n eq = eq + 1\n else\n gt = gt + 1\n end\n end\n \n return 1 if ls == gt\n if ls < gt\n ls = ls + eq\n return gt - ls\n else\n gt = gt + eq\n return ls - gt + 1\n end\n \nend\n\nans = gg\nputs ans\n"}, {"source_code": "#!/usr/bin/env ruby\n\nclass Main\n attr_reader :size, :number, :array\n\n def initialize(size, number, array)\n @size = size\n @number = number\n @array = array.sort\n end\n\n def run\n median_position = calculate_median(size)\n median_number = array[median_position]\n\n if number == median_number\n 0\n elsif array.include?(number)\n index = array.rindex(number)\n if index > median_position\n index = array.index(number)\n revert_median_calculation(index) - size\n elsif index < median_position\n (median_position - index) * 2 - size % 2\n end\n else\n median_number = array[median_position]\n new_array = (array + [number]).sort\n index = new_array.index(number)\n new_median_position = calculate_median(size + 1)\n if index > new_median_position\n revert_median_calculation(index) - size\n elsif index < new_median_position\n (new_median_position - index) * 2 - (size + 1) % 2 + 1\n else\n 1\n end\n end\n end\n\n private\n\n def calculate_median(array_size)\n (array_size + 1)/2 - 1\n end\n\n def revert_median_calculation(median)\n (median + 1) * 2 - 1\n end\nend\n\nsize, number = gets.split(\" \")\narray = gets.split(\" \")\nputs Main.new(size.to_i, number.to_i, array.map(&:to_i)).run\n"}, {"source_code": "# ProblemC\n\nn, x = gets.split().map! {|s| s.to_i}\n\nary = gets.split().map! {|s| s.to_i}\n\nres = 0\nunless ary.index(x)\n ary << x\n res += 1\nend\n\nary = ary.sort\n\nwhile true\n n = (ary.size)\n m = ((n + 1) / 2) - 1\n break if ary[m] == x\n if ary[m] > x\n ary.unshift(1)\n else\n ary << (10 ** 5)\n end\n res += 1\nend\n\nputs res\n"}, {"source_code": "a = gets.chomp.split(\" \").map{|i| i.to_i}\nb = gets.chomp.split(\" \").map{|i| i.to_i}\n\nb.sort!\nn = a[0]\nk = a[1]\n\nif k > b[n-1] \n puts n+1\nelse\n if k < b[0]\n puts n\n else\n x = (n+1)/2 - 1\n if k == b[x]\n puts 0\n else\n if k < b[x]\n i = 0\n while b[i+1] <= k do\n i += 1\n end\n find = n -2*i -2\n puts find\n else\n i = n-1\n while b[i-1] >= k do\n i -= 1\n end\n find = 2*i + 1 - n\n puts find\n end\n end\n end\nend"}, {"source_code": "n,k=gets.split.map(&:to_i)\na=gets.split.map(&:to_i).sort\nans=0\nunless a.include?(k)\n\ta<k\n\tuntil a[(n+1)/2-1]==k\n\t\tans+=1\n\t\tn+=1\n\t\ta.unshift 1\n\tend\nend\np ans"}], "negative_code": [{"source_code": "I = ->{gets.split.map(&:to_i)}\nn, m = I[]\nc = [0] * 3\nI[].each{|_| c[(_ <=> m) + 1] += 1}\np c\nd = 0\nif c[1] == 0\n c[1] += 1\n d += 1\nend\nd += [(c[0] > c[2] ? c[0] - c[2] + 1 : c[2] - c[0]) - c[1], 0].max\np d\n"}, {"source_code": "#!/usr/bin/ruby\n\nn, x = gets.split.map(&:to_i)\narr = gets.split.map(&:to_i).sort\n\ncnt = 0\nif arr.index(x).nil?\n arr << x\n cnt += 1\n arr.sort!\nend\n\nmedian = (arr.size+1) / 2 - 1\nni = arr.index(x)\nri = arr.rindex(x)\n\nif arr[median] == x\n p 0\n exit\nend\n\n# right adjustment\nrcnt = n*100\nif ri > median\n rcnt = 0\n target = ri\n target = ni if ni > median\n\n if arr.size.even?\n rcnt += 1\n target -= 1\n end\n rcnt += (target-median) * 2\nend\n\n# left adjustment\nlcnt = n*100\nif ni < median\n lcnt = 0\n target = ni\n target = ri if ri < median\n\n if arr.size.odd?\n lcnt += 1\n target += 1\n end\n lcnt += (median-target) * 2\nend\n\np [rcnt,lcnt].min + cnt\n"}, {"source_code": "#!/usr/bin/ruby\n\nn, x = gets.split.map(&:to_i)\narr = gets.split.map(&:to_i).sort\n\ncnt = 0\nif arr.index(x).nil?\n arr << x\n cnt += 1\n arr.sort!\nend\n\nmedian = (arr.size+1) / 2 - 1\nni = arr.index(x)\nri = arr.rindex(x)\n\n# right adjustment\nrcnt = n*100\nif ri > median\n rcnt = 0\n target = ri\n target = ni if ni > median\n\n if arr.size.even?\n rcnt += 1\n target -= 1\n end\n rcnt += (target-median) * 2\nend\n\n# left adjustment\nlcnt = n*100\nif ni < median\n lcnt = 0\n target = ni\n target = ri if ri < median\n\n if arr.size.odd?\n lcnt += 1\n target += 1\n end\n lcnt += (median-target) * 2\nend\n\np [rcnt,lcnt].min + cnt\n"}, {"source_code": "$n, $m = gets.split().map(&:to_i)\n\n$a = gets.split().map(&:to_i)\n\ndef gg()\n return 0 if $a[$a.length/2] == $m\n ls = 0\n gt = 0\n eq = 0\n $a.each do |i|\n if i < $m\n ls = ls + 1\n elsif i == $m\n eq = eq + 1\n else\n gt = gt + 1\n end\n end\n \n return 1 if ls == gt\n if ls < gt\n ls = ls + eq\n return gt - ls\n else\n gt = gt + eq\n return ls - gt + 1\n end\n \nend\n\nans = gg\nputs ans\n"}, {"source_code": "#!/usr/bin/env ruby\n\nclass Main\n attr_reader :size, :number, :array\n\n def initialize(size, number, array)\n @size = size\n @number = number\n @array = array.sort.reverse\n end\n\n def run\n median_position = (size + 1)/2 - 1\n if array.include?(number)\n index = array.index(number)\n (median_position - index).abs\n else\n median_number = array[median_position]\n new_array = (array + [number]).sort\n index = new_array.index(number)\n (median_position - index).abs * 2\n end\n end\nend\n\nsize, number = gets.split(\" \")\narray = gets.split(\" \")\nputs Main.new(size.to_i, number.to_i, array.map(&:to_i)).run"}, {"source_code": "#!/usr/bin/env ruby\n\nclass Main\n attr_reader :size, :number, :array\n\n def initialize(size, number, array)\n @size = size\n @number = number\n @array = array.sort\n end\n\n def run\n median_position = (size + 1)/2 - 1\n median_number = array[median_position]\n\n if number == median_number\n 0\n elsif array.include?(number)\n index = array.index(number)\n (median_position - index).abs\n else\n median_number = array[median_position]\n new_array = (array + [number]).sort\n index = new_array.index(number)\n new_median_position = (size + 2)/2 - 1\n if index > new_median_position\n index * 2 - size + 1\n elsif index < new_median_position\n size - index + 1\n else\n 1\n end\n end\n end\nend\n\nsize, number = gets.split(\" \")\narray = gets.split(\" \")\nputs Main.new(size.to_i, number.to_i, array.map(&:to_i)).run\n"}, {"source_code": "#!/usr/bin/env ruby\n\nclass Main\n attr_reader :size, :number, :array\n\n def initialize(size, number, array)\n @size = size\n @number = number\n @array = array.sort\n end\n\n def run\n median_position = calculate_median(size)\n median_number = array[median_position]\n\n if number == median_number\n 0\n elsif array.include?(number)\n index = array.index(number)\n if index > median_position\n revert_median_calculation(index) - size\n elsif index < median_position\n median_position - index\n end\n else\n median_number = array[median_position]\n new_array = (array + [number]).sort\n index = new_array.index(number)\n new_median_position = calculate_median(size + 1)\n if index > new_median_position\n revert_median_calculation(index) - size\n elsif index < new_median_position\n median_position - index + 1\n else\n 1\n end\n end\n end\n\n private\n\n def calculate_median(array_size)\n (array_size + 1)/2 - 1\n end\n\n def revert_median_calculation(median)\n (median + 1) * 2 - 1\n end\nend\n\nsize, number = gets.split(\" \")\narray = gets.split(\" \")\nputs Main.new(size.to_i, number.to_i, array.map(&:to_i)).run\n"}, {"source_code": "#!/usr/bin/env ruby\n\nclass Main\n attr_reader :size, :number, :array\n\n def initialize(size, number, array)\n @size = size\n @number = number\n @array = array.sort\n end\n\n def run\n median_position = calculate_median(size)\n median_number = array[median_position]\n\n if number == median_number\n 0\n elsif array.include?(number)\n index = array.rindex(number)\n if index > median_position\n revert_median_calculation(index) - size\n elsif index < median_position\n (median_position - index) * 2 - size % 2\n end\n else\n median_number = array[median_position]\n new_array = (array + [number]).sort\n index = new_array.index(number)\n new_median_position = calculate_median(size + 1)\n if index > new_median_position\n revert_median_calculation(index) - size\n elsif index < new_median_position\n (new_median_position - index) * 2 - (size + 1) % 2 + 1\n else\n 1\n end\n end\n end\n\n private\n\n def calculate_median(array_size)\n (array_size + 1)/2 - 1\n end\n\n def revert_median_calculation(median)\n (median + 1) * 2 - 1\n end\nend\n\nsize, number = gets.split(\" \")\narray = gets.split(\" \")\nputs Main.new(size.to_i, number.to_i, array.map(&:to_i)).run\n"}, {"source_code": "#!/usr/bin/env ruby\n\nclass Main\n attr_reader :size, :number, :array\n\n def initialize(size, number, array)\n @size = size\n @number = number\n @array = array.sort\n end\n\n def run\n median_position = (size + 1)/2 - 1\n median_number = array[median_position]\n\n if number == median_number\n 0\n elsif array.include?(number)\n index = array.index(number)\n (median_position - index).abs\n else\n median_number = array[median_position]\n new_array = (array + [number]).sort\n index = new_array.index(number)\n (median_position - index).abs * 2\n end\n end\nend\n\nsize, number = gets.split(\" \")\narray = gets.split(\" \")\nputs Main.new(size.to_i, number.to_i, array.map(&:to_i)).run"}, {"source_code": "a = gets.chomp.split(\" \").map{|i| i.to_i}\nb = gets.chomp.split(\" \").map{|i| i.to_i}\n\nb.sort!\nn = a[0]\nk = a[1]\n\nif k > b[n-1] \n puts n+1\nelse\n if k < b[0]\n puts n\n else\n x = (n+1)/2 - 1\n if k == b[x]\n puts 0\n else\n if k < b[x]\n i = 0\n while b[i] < k do\n i += 1\n end\n i -= 1 if b[i] != k\n find = n -2*i -2\n puts find\n else\n i = n-1\n while b[i] > k do\n i -= 1\n end\n i -= 1 if b[i] != k\n find = 2*i + 2 - n\n puts find\n end\n end\n end\nend"}, {"source_code": "a = gets.chomp.split(\" \").map{|i| i.to_i}\nb = gets.chomp.split(\" \").map{|i| i.to_i}\n\nb.sort!\nn = a[0]\nk = a[1]\n\nif k > b[n-1] \n puts n+1\nelse\n if k < b[0]\n puts n\n else\n x = (n+1)/2 - 1\n if k == b[x]\n puts 0\n else\n if k < b[x]\n i = 0\n while b[i] < k do\n i += 1\n end\n i -= 1 if b[i] != k\n find = n -2*i -2\n puts find\n else\n i = n-1\n while b[i] > k do\n i -= 1\n end\n i += 1 if b[i] != k\n find = 2*i + 1 - n\n puts find\n end\n end\n end\nend"}, {"source_code": "a = gets.chomp.split(\" \").map{|i| i.to_i}\nb = gets.chomp.split(\" \").map{|i| i.to_i}\n\nb.sort!\nn = a[0]\nk = a[1]\n\nif k > b[n-1] \n puts n+1\nelse\n if k < b[0]\n puts n\n else\n x = (n+1)/2\n if k == b[x]\n puts 0\n else\n if k < b[x]\n i = 0\n while b[i] < k do\n i += 1\n end\n i -= 1 if b[i] != k\n find = n -2*i -2\n puts find\n else\n i = n-1\n while b[i] > k do\n i -= 1\n end\n i -= 1 if b[i] != k\n find = 2*i + 2 - n\n puts find\n end\n end\n end\nend"}], "src_uid": "1a73bda2b9c2038d6ddf39918b90da61"} {"nl": {"description": "A country has n cities. Initially, there is no road in the country. One day, the king decides to construct some roads connecting pairs of cities. Roads can be traversed either way. He wants those roads to be constructed in such a way that it is possible to go from each city to any other city by traversing at most two roads. You are also given m pairs of cities — roads cannot be constructed between these pairs of cities.Your task is to construct the minimum number of roads that still satisfy the above conditions. The constraints will guarantee that this is always possible.", "input_spec": "The first line consists of two integers n and m . Then m lines follow, each consisting of two integers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi), which means that it is not possible to construct a road connecting cities ai and bi. Consider the cities are numbered from 1 to n. It is guaranteed that every pair of cities will appear at most once in the input.", "output_spec": "You should print an integer s: the minimum number of roads that should be constructed, in the first line. Then s lines should follow, each consisting of two integers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi), which means that a road should be constructed between cities ai and bi. If there are several solutions, you may print any of them.", "sample_inputs": ["4 1\n1 3"], "sample_outputs": ["3\n1 2\n4 2\n2 3"], "notes": "NoteThis is one possible solution of the example: These are examples of wrong solutions: The above solution is wrong because it doesn't use the minimum number of edges (4 vs 3). In addition, it also tries to construct a road between cities 1 and 3, while the input specifies that it is not allowed to construct a road between the pair. The above solution is wrong because you need to traverse at least 3 roads to go from city 1 to city 3, whereas in your country it must be possible to go from any city to another by traversing at most 2 roads. Finally, the above solution is wrong because it must be possible to go from any city to another, whereas it is not possible in this country to go from city 1 to 3, 2 to 3, and 4 to 3."}, "positive_code": [{"source_code": "args = gets.split(\" \").map{ |i| i.to_i}\nn = args[0]\nm = args[1]\n\n\nprohib = Array.new(n) { |i| 0 }\nfor i in 0...m\n args = gets.split(\" \").map{ |i| i.to_i}\n prohib[args[0]-1] += 1\n prohib[args[1]-1] += 1\nend\ncenter = prohib.each_with_index.min[1] + 1\nputs n - 1\n(1..n).to_a.select{ |a| a != center }.each{ |b| puts \"#{b} #{center}\"}"}, {"source_code": "n,m=gets.split.map(&:to_i)\ng=[]\nm.times do\n a,b=gets.split.map(&:to_i)\n g< b\n cannot[a] << b\n cannot[b] << a\nend\ncenter = 1\nfor i in 1..n\n if cannot[i].size < cannot[center].size\n center = i\n end\nend\nlink = Array.new(n + 1){Array.new(n + 1, false)}\nroad = []\nfor i in 1..n\n next if i == center\n if cannot[center].include? i\n for j in 1..n\n next if i == j\n next if cannot[i].include? j\n link[i][j] = link[j][i] = true\n road << [i, j]\n for k in 1...j\n next if link[j][k]\n link[i][k] = link[k][i] = true\n road << [i, k]\n end\n break\n end\n else\n link[center][i] = link[i][center] = true\n road << [center, i]\n end\nend\nputs road.size\nroad.each do |r|\n puts r.join(' ')\nend"}, {"source_code": "n,m=gets.split.map(&:to_i)\ng=[]\nm.times do\n a,b=gets.split.map(&:to_i)\n g<>=1\n if(n&1)==1\n puts \"NO\"\n next\n end\n puts \"YES\"\n sum =0\n (1).upto(n) do |i|\n print \"#{i*2} \"\n sum+=i*2\n end\n\n (1).upto(n-1) do |i|\n sum-=i*2-1\n print \"#{i*2-1} \"\n end\n puts sum\nend\n"}], "negative_code": [{"source_code": "gets.to_i.times do\n n = gets.to_i\n if n%4 != 0\n puts 'NO'\n next\n end\n puts 'YES'\n a = []\n (0...n/4).each{|i| a += [4*i + 2, 4*i + 4]}\n (0...n/4).each{|i| a += [4*i + 1, 4*i + 7]}\n puts a.join ' '\nend\n"}, {"source_code": "t=gets.to_i\n(1).upto(t) do\n n=gets.to_i\n n>>=1\n if(n&1)==1\n puts \"NO\"\n next\n end\n puts \"YES\"\n (1).upto(n) do\n print \"2 \"\n end\n (1).upto(n) do |i|\n x=i&1\n x<<=1\n x+=1\n print \"#{x} \"\n end\n puts \"\"\nend\n"}, {"source_code": "t=gets.to_i\n(1).upto(t) do\n n=gets.to_i\n n>>=1\n if(n&1)==1\n puts \"NO\"\n next\n end\n puts \"YES\"\n (1).upto(n) do |i|\n print \"#{i*2} \"\n end\n (1).upto(n) do |i|\n print \"#{i*2-1} \"\n end\n puts \"\"\nend\n"}], "src_uid": "0c7e019e1e955cadacca55b4e823a3e5"} {"nl": {"description": "Polycarp has $$$n$$$ friends, the $$$i$$$-th of his friends has $$$a_i$$$ candies. Polycarp's friends do not like when they have different numbers of candies. In other words they want all $$$a_i$$$ to be the same. To solve this, Polycarp performs the following set of actions exactly once: Polycarp chooses $$$k$$$ ($$$0 \\le k \\le n$$$) arbitrary friends (let's say he chooses friends with indices $$$i_1, i_2, \\ldots, i_k$$$); Polycarp distributes their $$$a_{i_1} + a_{i_2} + \\ldots + a_{i_k}$$$ candies among all $$$n$$$ friends. During distribution for each of $$$a_{i_1} + a_{i_2} + \\ldots + a_{i_k}$$$ candies he chooses new owner. That can be any of $$$n$$$ friends. Note, that any candy can be given to the person, who has owned that candy before the distribution process. Note that the number $$$k$$$ is not fixed in advance and can be arbitrary. Your task is to find the minimum value of $$$k$$$.For example, if $$$n=4$$$ and $$$a=[4, 5, 2, 5]$$$, then Polycarp could make the following distribution of the candies: Polycarp chooses $$$k=2$$$ friends with indices $$$i=[2, 4]$$$ and distributes $$$a_2 + a_4 = 10$$$ candies to make $$$a=[4, 4, 4, 4]$$$ (two candies go to person $$$3$$$). Note that in this example Polycarp cannot choose $$$k=1$$$ friend so that he can redistribute candies so that in the end all $$$a_i$$$ are equal.For the data $$$n$$$ and $$$a$$$, determine the minimum value $$$k$$$. With this value $$$k$$$, Polycarp should be able to select $$$k$$$ friends and redistribute their candies so that everyone will end up with the same number of candies.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$). Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$0 \\le a_i \\le 10^4$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case output: the minimum value of $$$k$$$, such that Polycarp can choose exactly $$$k$$$ friends so that he can redistribute the candies in the desired way; \"-1\" if no such value $$$k$$$ exists. ", "sample_inputs": ["5\n4\n4 5 2 5\n2\n0 4\n5\n10 8 5 1 4\n1\n10000\n7\n1 1 1 1 1 1 1"], "sample_outputs": ["2\n1\n-1\n0\n0"], "notes": null}, "positive_code": [{"source_code": "for t in 0...gets.to_i\r\n\tn = gets.to_i\r\n\ta = gets.split.map(&:to_i)\r\n\tsum = a.sum\r\n\tans = 0;\r\n\tif sum % n > 0\r\n\t\tputs -1\r\n\telsif\r\n\t\t(0...n).each do |i|\r\n\t\t\tif a[i] * n > sum\r\n\t\t\t\tans += 1\r\n\t\t\tend\r\n\t\tend\r\n\t\tputs ans\r\n\tend\r\nend"}, {"source_code": "gets.to_i.times do\r\n n=gets.to_i\r\n a=gets.split.map(&:to_i)\r\n s=a.sum\r\n if s%n!=0\r\n puts \"-1\"\r\n next\r\n end\r\n c=0\r\n z=s/n\r\n a.each do |aa|\r\n if aa>z\r\n c+=1\r\n end\r\n end\r\n puts c\r\nend\r\n"}], "negative_code": [], "src_uid": "b8554e64b92b1b9458955da7d55eba62"} {"nl": {"description": "Ivan has got an array of n non-negative integers a1, a2, ..., an. Ivan knows that the array is sorted in the non-decreasing order. Ivan wrote out integers 2a1, 2a2, ..., 2an on a piece of paper. Now he wonders, what minimum number of integers of form 2b (b ≥ 0) need to be added to the piece of paper so that the sum of all integers written on the paper equalled 2v - 1 for some integer v (v ≥ 0). Help Ivan, find the required quantity of numbers.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 105). The second input line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 2·109). It is guaranteed that a1 ≤ a2 ≤ ... ≤ an.", "output_spec": "Print a single integer — the answer to the problem.", "sample_inputs": ["4\n0 1 1 1", "1\n3"], "sample_outputs": ["0", "3"], "notes": "NoteIn the first sample you do not need to add anything, the sum of numbers already equals 23 - 1 = 7.In the second sample you need to add numbers 20, 21, 22."}, "positive_code": [{"source_code": "def update_hash hash, num\n return hash[num] = true if hash[num].nil?\n hash.delete(num)\n update_hash(hash, num.next)\nend\n\ngets\n\nhash = {}\nb = gets.chomp.split(\" \").map(&:to_i).each{|num| update_hash(hash, num)}\n\nputs hash.keys.max - hash.keys.count + 1\n"}, {"source_code": "MAX=(2*10**9)*(10**5)+1\nn=gets.to_i\na=gets.split.map(&:to_i)\na<= 0\n cnt[v] += appeared[v+1] if v+1 <= n\nend\nans = 3*n-2\ncnt.each do |v|\n ans -= v\nend\n#p cnt\np ans\n"}], "negative_code": [], "src_uid": "cabb7edf51f32c94415d580b96eef7b7"} {"nl": {"description": "We had a string s consisting of n lowercase Latin letters. We made k copies of this string, thus obtaining k identical strings s1, s2, ..., sk. After that, in each of these strings we swapped exactly two characters (the characters we swapped could be identical, but they had different indices in the string).You are given k strings s1, s2, ..., sk, and you have to restore any string s so that it is possible to obtain these strings by performing aforementioned operations. Note that the total length of the strings you are given doesn't exceed 5000 (that is, k·n ≤ 5000).", "input_spec": "The first line contains two integers k and n (1 ≤ k ≤ 2500, 2 ≤ n ≤ 5000, k · n ≤ 5000) — the number of strings we obtained, and the length of each of these strings. Next k lines contain the strings s1, s2, ..., sk, each consisting of exactly n lowercase Latin letters.", "output_spec": "Print any suitable string s, or -1 if such string doesn't exist.", "sample_inputs": ["3 4\nabac\ncaab\nacba", "3 4\nkbbu\nkbub\nubkb", "5 4\nabcd\ndcba\nacbd\ndbca\nzzzz"], "sample_outputs": ["acab", "kbub", "-1"], "notes": "NoteIn the first example s1 is obtained by swapping the second and the fourth character in acab, s2 is obtained by swapping the first and the second character, and to get s3, we swap the third and the fourth character.In the second example s1 is obtained by swapping the third and the fourth character in kbub, s2 — by swapping the second and the fourth, and s3 — by swapping the first and the third.In the third example it's impossible to obtain given strings by aforementioned operations."}, "positive_code": [{"source_code": "k,n = gets.split.map(&:to_i)\n\ndef diffsz(x,y)\n x.chars.zip(y.chars).reject{|x,y|x==y}.size\nend\n\ns = []\nk.times{s << gets.chomp}\n\nif k==1\n s=s[0]\n s[0], s[1] = s[1], s[0]\n puts s\n exit(0)\nend\n\nex_id = s[0].size != s[0].chars.uniq.size\n\naaa=s[0]\nif s.any?{|s| s.chars.sort != aaa.chars.sort}\nputs -1\nexit(0)\nend\n\n\n\n\n\nif n>=5\n k.times do |j|\n diff = []\n n.times do |i|\n diff << i if s[0][i] != s[j][i]\n end\n\n if diff.size>4\n puts -1\n exit(0)\n end\n\n diff.permutation(diff.size).each do |p|\n org = s[0].dup\n p.each_with_index{|a,i| org[a] = s[0][diff[i]]}\n\n if s.all? {|s| sz = diffsz(s,org); ((ex_id && sz == 0) || sz == 2)}\n puts org\n exit(0)\n end\n end\n end\n\n puts -1\n exit(0)\nend\n\ndiff = n.times.to_a\n diff.permutation(diff.size).each do |p|\n org = s[0].dup\n p.each_with_index{|a,i| org[a] = s[0][diff[i]]}\n\n if s.all? {|s| sz = diffsz(s,org); (ex_id && sz == 0) || sz == 2}\n puts org\n exit(0)\n end\n end\n\n puts -1\n exit(0)\n"}], "negative_code": [{"source_code": "k,n = gets.split.map(&:to_i)\n\ndef diffsz(x,y)\n x.chars.zip(y.chars).reject{|x,y|x==y}.size\nend\n\ns = []\nk.times{s << gets.chomp}\n\nif k==1\n s=s[0]\n s[0], s[1] = s[1], s[0]\n puts s\n exit(0)\nend\n\nex_id = s[0].size != s[0].chars.uniq.size\n\naaa=s[0]\nif s.any?{|s| s.chars.sort != aaa.chars.sort}\nputs -1\nexit(0)\nend\n\n\n\n\n\nif n>=5\n diff = []\n k.times do |j|\n n.times do |i|\n diff << i if s[0][i] != s[j][i]\n end\n\n if diff.size>4\n puts -1\n exit(0)\n end\n\n diff.permutation(diff.size).each do |p|\n org = s[0].dup\n p.each_with_index{|a,i| org[a] = s[0][diff[i]]}\n\n if s.all? {|s| sz = diffsz(s,org); ((ex_id && sz == 0) || sz == 2)}\n puts org\n exit(0)\n end\n end\n end\n\n puts -1\n exit(0)\nend\n\ndiff = n.times.to_a\n diff.permutation(diff.size).each do |p|\n org = s[0].dup\n p.each_with_index{|a,i| org[a] = s[0][diff[i]]}\n\n if s.all? {|s| sz = diffsz(s,org); (ex_id && sz == 0) || sz == 2}\n puts org\n exit(0)\n end\n end\n\n puts -1\n exit(0)\n"}, {"source_code": "k,n = gets.split.map(&:to_i)\n\ndef diffsz(x,y)\n x.chars.zip(y.chars).reject{|x,y|x==y}.size\nend\n\ns = []\nk.times{s << gets.chomp}\n\nif k==1\n s=s[0]\n s[0], s[1] = s[1], s[0]\n puts s\n exit(0)\nend\n\nex_id = s[0].size != s[0].chars.uniq.size\n\nif n>=5\n diff = []\n n.times do |i|\n diff << i if s[0][i] != s[1][i]\n end\n\n if diff.size>4\n puts -1\n exit(0)\n end\n\n diff.permutation(diff.size).each do |p|\n org = s[0].dup\n p.each_with_index{|a,i| org[a] = s[0][diff[i]]}\n\n if s.all? {|s| sz = diffsz(s,org); (ex_id && sz == 0) || sz == 2}\n puts org\n exit(0)\n end\n end\n\n puts -1\n exit(0)\nend\n\ndiff = n.times.to_a\n diff.permutation(diff.size).each do |p|\n org = s[0].dup\n p.each_with_index{|a,i| org[a] = s[0][diff[i]]}\n\n if s.all? {|s| sz = diffsz(s,org); (ex_id && sz == 0) || sz == 2}\n puts org\n exit(0)\n end\n end\n\n puts -1\n exit(0)\n"}, {"source_code": "k,n = gets.split.map(&:to_i)\n\ndef diffsz(x,y)\n x.chars.zip(y.chars).reject{|x,y|x==y}.size\nend\n\ns = []\nk.times{s << gets.chomp}\n\nif k==1\n s=s[0]\n s[0], s[1] = s[1], s[0]\n puts s\n exit(0)\nend\n\nex_id = s[0].size != s[0].chars.uniq.size\n\naaa=s[0]\nif s.any?{|s| s.chars.sort != aaa.chars.sort}\nputs -1\nexit(0)\nend\n\n\n\n\n\nif n>=5\n diff = []\n n.times do |i|\n diff << i if s[0][i] != s[1][i]\n end\n\n if diff.size>4\n puts -1\n exit(0)\n end\n\n diff.permutation(diff.size).each do |p|\n org = s[0].dup\n p.each_with_index{|a,i| org[a] = s[0][diff[i]]}\n\n if s[1,s.size].all? {|s| sz = diffsz(s,org); ((ex_id && sz == 0) || sz == 2)}\n puts org\n exit(0)\n end\n end\n\n puts -1\n exit(0)\nend\n\ndiff = n.times.to_a\n diff.permutation(diff.size).each do |p|\n org = s[0].dup\n p.each_with_index{|a,i| org[a] = s[0][diff[i]]}\n\n if s[1,s.size].all? {|s| sz = diffsz(s,org); (ex_id && sz == 0) || sz == 2}\n puts org\n exit(0)\n end\n end\n\n puts -1\n exit(0)\n"}, {"source_code": "k,n = gets.split.map(&:to_i)\n\ndef diffsz(x,y)\n x.chars.zip(y.chars).reject{|x,y|x==y}.size\nend\n\ns = []\nk.times{s << gets.chomp}\n\nif k==1\n s=s[0]\n s[0], s[1] = s[1], s[0]\n puts s\n exit(0)\nend\n\nex_id = s[0].size != s[0].chars.uniq.size\n\naaa=s[0]\nif s.any?{|s| s.chars.sort != aaa.chars.sort}\nputs -1\nexit(0)\nend\n\n\n\n\n\nif n>=5\n diff = []\n n.times do |i|\n diff << i if s[0][i] != s[1][i]\n end\n\n if diff.size>4\n puts -1\n exit(0)\n end\n\n diff.permutation(diff.size).each do |p|\n org = s[0].dup\n p.each_with_index{|a,i| org[a] = s[0][diff[i]]}\n\n if s.all? {|s| sz = diffsz(s,org); ((ex_id && sz == 0) || sz == 2)}\n puts org\n exit(0)\n end\n end\n\n puts -1\n exit(0)\nend\n\ndiff = n.times.to_a\n diff.permutation(diff.size).each do |p|\n org = s[0].dup\n p.each_with_index{|a,i| org[a] = s[0][diff[i]]}\n\n if s.all? {|s| sz = diffsz(s,org); (ex_id && sz == 0) || sz == 2}\n puts org\n exit(0)\n end\n end\n\n puts -1\n exit(0)\n"}, {"source_code": "k,n = gets.split.map(&:to_i)\n\ndef diffsz(x,y)\n x.chars.zip(y.chars).reject{|x,y|x==y}.size\nend\n\ns = []\nk.times{s << gets.chomp}\n\nif k==1\n s=s[0]\n s[0], s[1] = s[1], s[0]\n puts s\n exit(0)\nend\n\nex_id = s[0].size != s[0].chars.uniq.size\n\nif n>=5\n diff = []\n n.times do |i|\n diff << i if s[0][i] != s[1][i]\n end\n\n if diff.size>4\n puts -1\n exit(0)\n end\n\n diff.permutation(diff.size).each do |p|\n org = s[0].dup\n p.each_with_index{|a,i| org[a] = s[0][diff[i]]}\n\n if s.all? {|s| sz = diffsz(s,org); ((ex_id && sz == 0) || sz == 2) && (s.chars == org.chars)}\n puts org\n exit(0)\n end\n end\n\n puts -1\n exit(0)\nend\n\ndiff = n.times.to_a\n diff.permutation(diff.size).each do |p|\n org = s[0].dup\n p.each_with_index{|a,i| org[a] = s[0][diff[i]]}\n\n if s.all? {|s| sz = diffsz(s,org); (ex_id && sz == 0) || sz == 2 && (s.chars == org.chars)}\n puts org\n exit(0)\n end\n end\n\n puts -1\n exit(0)\n"}], "src_uid": "0550004c6c7a386b8e7d214e71990572"} {"nl": {"description": "Allen wants to enter a fan zone that occupies a round square and has $$$n$$$ entrances.There already is a queue of $$$a_i$$$ people in front of the $$$i$$$-th entrance. Each entrance allows one person from its queue to enter the fan zone in one minute.Allen uses the following strategy to enter the fan zone: Initially he stands in the end of the queue in front of the first entrance. Each minute, if he is not allowed into the fan zone during the minute (meaning he is not the first in the queue), he leaves the current queue and stands in the end of the queue of the next entrance (or the first entrance if he leaves the last entrance). Determine the entrance through which Allen will finally enter the fan zone.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$2 \\le n \\le 10^5$$$) — the number of entrances. The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$0 \\le a_i \\le 10^9$$$) — the number of people in queues. These numbers do not include Allen.", "output_spec": "Print a single integer — the number of entrance that Allen will use.", "sample_inputs": ["4\n2 3 2 0", "2\n10 10", "6\n5 2 6 5 7 4"], "sample_outputs": ["3", "1", "6"], "notes": "NoteIn the first example the number of people (not including Allen) changes as follows: $$$[\\textbf{2}, 3, 2, 0] \\to [1, \\textbf{2}, 1, 0] \\to [0, 1, \\textbf{0}, 0]$$$. The number in bold is the queue Alles stands in. We see that he will enter the fan zone through the third entrance.In the second example the number of people (not including Allen) changes as follows: $$$[\\textbf{10}, 10] \\to [9, \\textbf{9}] \\to [\\textbf{8}, 8] \\to [7, \\textbf{7}] \\to [\\textbf{6}, 6] \\to \\\\ [5, \\textbf{5}] \\to [\\textbf{4}, 4] \\to [3, \\textbf{3}] \\to [\\textbf{2}, 2] \\to [1, \\textbf{1}] \\to [\\textbf{0}, 0]$$$.In the third example the number of people (not including Allen) changes as follows: $$$[\\textbf{5}, 2, 6, 5, 7, 4] \\to [4, \\textbf{1}, 5, 4, 6, 3] \\to [3, 0, \\textbf{4}, 3, 5, 2] \\to \\\\ [2, 0, 3, \\textbf{2}, 4, 1] \\to [1, 0, 2, 1, \\textbf{3}, 0] \\to [0, 0, 1, 0, 2, \\textbf{0}]$$$."}, "positive_code": [{"source_code": "N = gets.to_i\nas = gets.split.map(&:to_i)\nN.times do |i|\n as[i] = ((as[i]-i-1)/N + 1) * N + i\n as[i] = i if as[i] < i\nend\nputs N.times.min_by{|i| as[i]} + 1"}, {"source_code": "n = gets.to_i\nary = gets.strip.split.map(&:to_i)\n\nminv = ary.min\nsq = minv % n\nexitq = sq\nary.map!{|x| x-minv}\nsq.upto(sq+n-1).each do |i|\n aryi = i % n\n if ary[aryi]<=(i-sq)\n exitq = aryi\n break\n end\nend\n\nputs exitq+1"}, {"source_code": "number_of_entrances = gets.to_i\nmin_time = 1000000000\nmin_time_index = 0\nppl_in_each_entrance = gets.split(' ').map(&:to_i)\n\ndef ceiling(num, den)\n (num/den) + (num % den == 0 ? 0 : 1)\nend\n\n0.upto(number_of_entrances - 1) do |entrance|\n no_of_ppl = ppl_in_each_entrance[entrance]\n\n x = [0, no_of_ppl - entrance].max\n value = ceiling(x, number_of_entrances)\n\n if min_time > value\n min_time = value\n min_time_index = entrance\n end\nend\n\np min_time_index + 1\n"}], "negative_code": [{"source_code": "number_of_entrances = gets.to_i\nmin_time = 1000000000\nmin_time_index = 0\n\ndef ceiling(num, den)\n (num/den) + (num % den == 0 ? 0 : 1)\nend\n\n0.upto(number_of_entrances - 1) do |entrance|\n no_of_ppl = gets.to_i\n\n x = [0, no_of_ppl - entrance].max\n value = ceiling(x, number_of_entrances)\n\n if min_time > value\n min_time = value\n min_time_index = entrance\n end\nend\n\np min_time_index + 1\n"}], "src_uid": "b18bbefd2a948da9dec1d6f27f219ed1"} {"nl": {"description": "You are given a range of positive integers from $$$l$$$ to $$$r$$$.Find such a pair of integers $$$(x, y)$$$ that $$$l \\le x, y \\le r$$$, $$$x \\ne y$$$ and $$$x$$$ divides $$$y$$$.If there are multiple answers, print any of them.You are also asked to answer $$$T$$$ independent queries.", "input_spec": "The first line contains a single integer $$$T$$$ ($$$1 \\le T \\le 1000$$$) — the number of queries. Each of the next $$$T$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \\le l \\le r \\le 998244353$$$) — inclusive borders of the range. It is guaranteed that testset only includes queries, which have at least one suitable pair.", "output_spec": "Print $$$T$$$ lines, each line should contain the answer — two integers $$$x$$$ and $$$y$$$ such that $$$l \\le x, y \\le r$$$, $$$x \\ne y$$$ and $$$x$$$ divides $$$y$$$. The answer in the $$$i$$$-th line should correspond to the $$$i$$$-th query from the input. If there are multiple answers, print any of them.", "sample_inputs": ["3\n1 10\n3 14\n1 10"], "sample_outputs": ["1 7\n3 9\n5 10"], "notes": null}, "positive_code": [{"source_code": "gets;$<.map{|e|puts [n=e.to_i,n*2]*' '}"}, {"source_code": "t = gets.to_i\nt.times do\n\tr,l = gets.split.map(&:to_i)\n\tputs \"#{r} #{r*2}\"\nend"}, {"source_code": "gets.to_i.times do\n l, r = gets.split.map(&:to_i)\n puts \"#{l} #{l*2}\"\nend"}, {"source_code": "n = gets().to_i\n(1..n).each do\n l, r = gets().split.map(&:to_i)\n puts \"#{l} #{l * 2}\"\nend\n"}, {"source_code": "gets.to_i.times do\n l,r = gets.split.map(&:to_i)\n puts \"#{l} #{l*2}\"\nend"}, {"source_code": "def lscan; gets.split.map(&:to_i); end\n\ngets.to_i.times do\n l, r = lscan\n x = l\n y = l*2\n puts \"#{x} #{y}\"\nend"}, {"source_code": "t = gets.to_i\nt.times do\n l, r = gets.split.map &:to_i\n print l, ' ', 2*l, \"\\n\"\nend\n"}], "negative_code": [{"source_code": "n = gets().to_i\n(1..n).each do\n l, r = gets().split.map(&:to_i)\n (l..r).each do |x|\n i = 2\n if (x * 2 < r)\n puts \"#{x} #{x * 2}\"\n break\n end\n end\nend\n"}], "src_uid": "a9cd97046e27d799c894d8514e90a377"} {"nl": {"description": "One day, at the \"Russian Code Cup\" event it was decided to play football as an out of competition event. All participants was divided into n teams and played several matches, two teams could not play against each other more than once.The appointed Judge was the most experienced member — Pavel. But since he was the wisest of all, he soon got bored of the game and fell asleep. Waking up, he discovered that the tournament is over and the teams want to know the results of all the matches.Pavel didn't want anyone to discover about him sleeping and not keeping an eye on the results, so he decided to recover the results of all games. To do this, he asked all the teams and learned that the real winner was friendship, that is, each team beat the other teams exactly k times. Help Pavel come up with chronology of the tournir that meets all the conditions, or otherwise report that there is no such table.", "input_spec": "The first line contains two integers — n and k (1 ≤ n, k ≤ 1000).", "output_spec": "In the first line print an integer m — number of the played games. The following m lines should contain the information about all the matches, one match per line. The i-th line should contain two integers ai and bi (1 ≤ ai, bi ≤ n; ai ≠ bi). The numbers ai and bi mean, that in the i-th match the team with number ai won against the team with number bi. You can assume, that the teams are numbered from 1 to n. If a tournir that meets the conditions of the problem does not exist, then print -1.", "sample_inputs": ["3 1"], "sample_outputs": ["3\n1 2\n2 3\n3 1"], "notes": null}, "positive_code": [{"source_code": "\n#$stdin.reopen('/home/chen/Desktop/input.txt', 'r')\n\n\nn,k=gets.split.map(&:to_i)\n\nif 2*k>=n\n\tputs -1\nelse\n\tputs n*k\n\t(1..n).each do |ind|\n\t\t(1..k).each do |ind2|\n\t\t\tputs \"#{ind} #{(ind+ind2-1)%n+1}\"\n\t\tend\n\tend\nend\n\n\n\n\n\n"}, {"source_code": "n,k=gets.split.map &:to_i\nif k > (n-1) / 2\n puts \"-1\"\n exit\nelse\n puts n*k\n n.times do |cmdW|\n k.times do |cmdL|\n puts \"#{cmdW+1} #{(cmdW+1+cmdL)%n + 1}\"\n end\n end\nend\n"}, {"source_code": "n, k = gets.split.map {|x| Integer x}\nif k * n > n * (n - 1) / 2\n puts -1\n exit\nend\nam = Array.new(n)\ncnt = Array.new(n, n - 1)\nfor i in 0...n\n am[i] = Array.new(n)\nend\nfor i in 0...n\n c = 0\n for j in 0...n\n if cnt[j] == k || i == j || am[i][j] != nil\n next\n end\n am[i][j] = true\n am[j][i] = false\n c += 1\n if c == k\n break\n end\n end\nend\nans = []\nfor i in 0...n\n for j in 0...n\n if am[i][j]\n ans.push((i + 1).to_s + ' ' + (j + 1).to_s)\n end\n end\nend\nputs ans.length\nputs ans"}], "negative_code": [{"source_code": "\n#$stdin.reopen('/home/chen/Desktop/input.txt', 'r')\n\n\nn,k=gets.split.map(&:to_i)\n\nif k*(n/2)>=n\n\tputs -1\nelse\n\tputs n*k\n\t(1..n).each do |ind|\n\t\t(1..k).each do |ind2|\n\t\t\tputs \"#{ind} #{(ind+ind2-1)%n+1}\"\n\t\tend\n\tend\nend\n\n\n\n\n\n\n"}, {"source_code": "n, k = gets.split.map {|x| Integer x}\nif k > n * (n - 1) / 4\n puts -1\n exit\nend\nam = Array.new(n)\ncnt = Array.new(n, n - 1)\nfor i in 0...n\n am[i] = Array.new(n)\nend\nfor i in 0...n\n c = 0\n for j in 0...n\n if cnt[j] == k || i == j || am[i][j] != nil\n next\n end\n am[i][j] = true\n am[j][i] = false\n c += 1\n if c == k\n break\n end\n end\nend\nfor i in 0...n\n for j in 0...n\n if am[i][j]\n puts (i + 1).to_s + ' ' + (j + 1).to_s\n end\n end\nend"}, {"source_code": "n, k = gets.split.map {|x| Integer x}\nif k > n * (n - 1) / 4\n puts -1\n exit\nend\nam = Array.new(n)\ncnt = Array.new(n, n - 1)\nfor i in 0...n\n am[i] = Array.new(n)\nend\nfor i in 0...n\n c = 0\n for j in 0...n\n if cnt[j] == k || i == j || am[i][j] != nil\n next\n end\n am[i][j] = true\n am[j][i] = false\n c += 1\n if c == k\n break\n end\n end\nend\nans = []\nfor i in 0...n\n for j in 0...n\n if am[i][j]\n ans.push((i + 1).to_s + ' ' + (j + 1).to_s)\n end\n end\nend\nputs ans.length\nputs ans"}], "src_uid": "14570079152bbf6c439bfceef9816f7e"} {"nl": {"description": "You have a long stick, consisting of $$$m$$$ segments enumerated from $$$1$$$ to $$$m$$$. Each segment is $$$1$$$ centimeter long. Sadly, some segments are broken and need to be repaired.You have an infinitely long repair tape. You want to cut some pieces from the tape and use them to cover all of the broken segments. To be precise, a piece of tape of integer length $$$t$$$ placed at some position $$$s$$$ will cover segments $$$s, s+1, \\ldots, s+t-1$$$.You are allowed to cover non-broken segments; it is also possible that some pieces of tape will overlap.Time is money, so you want to cut at most $$$k$$$ continuous pieces of tape to cover all the broken segments. What is the minimum total length of these pieces?", "input_spec": "The first line contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$1 \\le n \\le 10^5$$$, $$$n \\le m \\le 10^9$$$, $$$1 \\le k \\le n$$$) — the number of broken segments, the length of the stick and the maximum number of pieces you can use. The second line contains $$$n$$$ integers $$$b_1, b_2, \\ldots, b_n$$$ ($$$1 \\le b_i \\le m$$$) — the positions of the broken segments. These integers are given in increasing order, that is, $$$b_1 < b_2 < \\ldots < b_n$$$.", "output_spec": "Print the minimum total length of the pieces.", "sample_inputs": ["4 100 2\n20 30 75 80", "5 100 3\n1 2 4 60 87"], "sample_outputs": ["17", "6"], "notes": "NoteIn the first example, you can use a piece of length $$$11$$$ to cover the broken segments $$$20$$$ and $$$30$$$, and another piece of length $$$6$$$ to cover $$$75$$$ and $$$80$$$, for a total length of $$$17$$$.In the second example, you can use a piece of length $$$4$$$ to cover broken segments $$$1$$$, $$$2$$$ and $$$4$$$, and two pieces of length $$$1$$$ to cover broken segments $$$60$$$ and $$$87$$$."}, "positive_code": [{"source_code": "n,m,k = gets.split.map(&:to_i)\nb = gets.split.map(&:to_i)\nans = 0\na = []\nfor i in 1..n-1\n a << b[i]-b[i-1]\nend\na.sort!\nfor i in 0..n-k-1\n ans += a[i]\nend\nputs ans+k"}, {"source_code": "DBG = !true\nn,m,k = gets.split.map{|z| z.to_i}\nb = gets.split.map{|z| z.to_i}\nsd = []\nfor i in 1...n\n sd << [i, b[i]-b[i-1]]\nend\nsd.sort_by!{|z| -z[1]}\nprint sd if DBG\n\nd = sd[0,k-1].map{|z| z[0]}\nd.sort!\nprint d if DBG\n\nprev = 0\nsum = 0\nfor x in d\n sum += b[x-1] - b[prev] + 1\n prev = x\nend\nsum += b[-1] - b[prev] + 1\nputs \"#{sum}\"\n"}], "negative_code": [], "src_uid": "6b2b56a423c247d42493d01e06e4b1d2"} {"nl": {"description": "This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \\le i,j \\le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$. Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation.", "input_spec": "The first line contains a single integer $$$k$$$ ($$$1 \\leq k \\leq 10$$$), the number of test cases. For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \\leq n \\leq 10^4$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different.", "output_spec": "For each test case, output \"Yes\" if Ujan can make the two strings equal and \"No\" otherwise. You can print each letter in any case (upper or lower).", "sample_inputs": ["4\n5\nsouse\nhouhe\n3\ncat\ndog\n2\naa\naz\n3\nabc\nbca"], "sample_outputs": ["Yes\nNo\nNo\nNo"], "notes": "NoteIn the first test case, Ujan can swap characters $$$s_1$$$ and $$$t_4$$$, obtaining the word \"house\".In the second test case, it is not possible to make the strings equal using exactly one swap of $$$s_i$$$ and $$$t_j$$$."}, "positive_code": [{"source_code": "def inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\nq = inp[0]\nnt = []\nst = []\ntt = []\nq.times do\n nt << inp[0]\n st << gets.chomp\n tt << gets.chomp\nend\nq.times do |i|\n n = nt[i]\n s = st[i]\n t = tt[i]\n noeqc = nil\n f = false\n n.times do |i|\n if (s[i] != t[i])\n if(noeqc == nil)\n noeqc = i\n elsif(noeqc == false)\n f = false\n break\n else\n if(s[i] == s[noeqc] and t[noeqc] == t[i])\n f = true\n noeqc = false\n else\n f = false\n break\n end\n end\n end\n end\n puts (f)? \"Yes\" : \"No\"\nend"}], "negative_code": [{"source_code": "def inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\nq = inp[0]\nnt = []\nst = []\ntt = []\nq.times do\n nt << inp[0]\n st << gets.chomp\n tt << gets.chomp\nend\nq.times do |i|\n n = nt[i]\n s = st[i]\n t = tt[i]\n noeqc = nil\n f = false\n n.times do |i|\n if (s[i] != t[i])\n if(noeqc == nil)\n noeqc = i\n elsif(noeqc == false)\n f = false\n break\n else\n if(s[i] == s[noeqc] and t[noeqc] == t[i])\n f = true\n else\n f = false\n break\n end\n end\n end\n end\n puts (f)? \"Yes\" : \"No\"\nend"}], "src_uid": "97fa7e82566e3799e165ce6cbbf1da22"} {"nl": {"description": "You are given an array $$$a$$$ with $$$n$$$ integers. You can perform the following operation at most $$$k$$$ times: Choose two indices $$$i$$$ and $$$j$$$, in which $$$i \\,\\bmod\\, k = j \\,\\bmod\\, k$$$ ($$$1 \\le i < j \\le n$$$). Swap $$$a_i$$$ and $$$a_j$$$. After performing all operations, you have to select $$$k$$$ consecutive elements, and the sum of the $$$k$$$ elements becomes your score. Find the maximum score you can get.Here $$$x \\bmod y$$$ denotes the remainder from dividing $$$x$$$ by $$$y$$$.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 600$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le k \\le n \\le 100$$$) — the length of the array and the number in the statement above. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$0 \\le a_i \\le 10^9$$$)  — the array itself.", "output_spec": "For each test case, print the maximum score you can get, one per line.", "sample_inputs": ["5\n\n3 2\n\n5 6 0\n\n1 1\n\n7\n\n5 3\n\n7 0 4 0 4\n\n4 2\n\n2 7 3 4\n\n3 3\n\n1000000000 1000000000 999999997"], "sample_outputs": ["11\n7\n15\n10\n2999999997"], "notes": "NoteIn the first test case, we can get a score of $$$11$$$ if we select $$$a_1, a_2$$$ without performing any operations.In the third test case, we can get a score of $$$15$$$ if we first swap $$$a_1$$$ with $$$a_4$$$ and then select $$$a_3, a_4, a_5$$$. "}, "positive_code": [{"source_code": "def main\n t = inp.to_i\n t.times do\n n, k = mapint(inarr)\n a = mapint(inarr)\n\n md = Array.new(k, 0)\n n.times do |i|\n md[i % k ] = [md[i %k], a[i]].max\n end\n sum = 0\n md.each do |i|\n sum += i\n end\n puts sum\n\n end\n\nend\n\n\n\n\n# lib\ndef inp\n gets.chomp\nend\n\ndef inarr\n gets.chomp.split\nend\n\ndef mapint(lst)\n lst.map(&:to_i)\nend\n\nmain"}], "negative_code": [{"source_code": "def main\n t = inp.to_i\n t.times do\n n, k = mapint(inarr)\n a = mapint(inarr)\n\n left = 0\n ans = 0\n while(left + k <= n)\n sum = 0\n left.upto(left + k - 1) do |i|\n m = a[i]\n if i - k >= 0 && left + k - 1 != i\n m = [a[i], a[i - k]].max\n end\n if i + k < n && i != left\n m = [a[i], a[i + k]].max\n end\n sum += m\n end\n ans = [ans, sum].max\n left+=1\n end\n puts ans\n end\n\nend\n\n\n\n\n# lib\ndef inp\n gets.chomp\nend\n\ndef inarr\n gets.chomp.split\nend\n\ndef mapint(lst)\n lst.map(&:to_i)\nend\n\nmain"}, {"source_code": "def main\n t = inp.to_i\n t.times do\n n, k = mapint(inarr)\n a = mapint(inarr)\n\n left = 0\n ans = 0\n while(left + k <= n)\n sum = 0\n left.upto(left + k - 1) do |i|\n m = a[i]\n if i - k >= 0\n m = [a[i], a[i - k]].max\n end\n if i + k < n && i != left\n m = [a[i], a[i + k]].max\n end\n sum += m\n end\n ans = [ans, sum].max\n left+=1\n end\n puts ans\n end\n\nend\n\n\n\n\n# lib\ndef inp\n gets.chomp\nend\n\ndef inarr\n gets.chomp.split\nend\n\ndef mapint(lst)\n lst.map(&:to_i)\nend\n\nmain"}], "src_uid": "f974c33780a933a6076c0559e48b7552"} {"nl": {"description": "General Payne has a battalion of n soldiers. The soldiers' beauty contest is coming up, it will last for k days. Payne decided that his battalion will participate in the pageant. Now he has choose the participants.All soldiers in the battalion have different beauty that is represented by a positive integer. The value ai represents the beauty of the i-th soldier.On each of k days Generals has to send a detachment of soldiers to the pageant. The beauty of the detachment is the sum of the beauties of the soldiers, who are part of this detachment. Payne wants to surprise the jury of the beauty pageant, so each of k days the beauty of the sent detachment should be unique. In other words, all k beauties of the sent detachments must be distinct numbers.Help Payne choose k detachments of different beauties for the pageant. Please note that Payne cannot just forget to send soldiers on one day, that is, the detachment of soldiers he sends to the pageant should never be empty.", "input_spec": "The first line contains two integers n, k (1 ≤ n ≤ 50; 1 ≤ k ≤  ) — the number of soldiers and the number of days in the pageant, correspondingly. The second line contains space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 107) — the beauties of the battalion soldiers. It is guaranteed that Payne's battalion doesn't have two soldiers with the same beauty.", "output_spec": "Print k lines: in the i-th line print the description of the detachment that will participate in the pageant on the i-th day. The description consists of integer ci (1 ≤ ci ≤ n) — the number of soldiers in the detachment on the i-th day of the pageant and ci distinct integers p1, i, p2, i, ..., pci, i — the beauties of the soldiers in the detachment on the i-th day of the pageant. The beauties of the soldiers are allowed to print in any order. Separate numbers on the lines by spaces. It is guaranteed that there is the solution that meets the problem conditions. If there are multiple solutions, print any of them.", "sample_inputs": ["3 3\n1 2 3", "2 1\n7 12"], "sample_outputs": ["1 1\n1 2\n2 3 2", "1 12"], "notes": null}, "positive_code": [{"source_code": "a = ARGF.read.split.map &:to_i\nn, k = a.shift(2)\ndp = Array.new(n+1) { Hash.new }\ndp[0][0] = true\n(1..n).each do |i|\n dp[i-1].each_key do |val|\n dp[i][val] = dp[i][val + a[i-1]] = true\n end\n (n = i; break) if dp[i].size > k\nend\ndfs = ->(arr, i, j) {\n if i > 0\n if dp[i-1].has_key?(j-a[i-1]) then arr << a[i-1]; dfs.(arr, i-1, j-a[i-1])\n else dfs.(arr, i-1, j) end\n end\n}\nks = dp[n].keys.select! { |k| k != 0 }\nk.times do\n arr = []\n dfs.(arr, n, ks.shift)\n $stdout.puts \"#{arr.size} #{arr.map(&:to_i).join(' ')}\"\nend\n"}], "negative_code": [{"source_code": "a = ARGF.read.split.map &:to_i\nn, k = a.shift(2)\ndp = Array.new(n+1) { Hash.new }\ndp[0][0] = true\n(1..n).each do |i|\n dp[i-1].each_key do |val|\n dp[i][val] = dp[i][val + a[i-1]] = true\n end\nend\ndfs = ->(arr, i, j) {\n if i > 0\n if dp[i-1].has_key?(j-a[i-1]) then arr << i; dfs.(arr, i-1, j-a[i-1])\n else dfs.(arr, i-1, j) end\n end\n}\nks = dp[n].keys.select! { |k| k != 0 }\nk.times do\n arr = []\n dfs.(arr, n, ks.shift)\n $stdout.puts \"#{arr.size} #{arr.map(&:to_i).join(' ')}\"\nend\n"}], "src_uid": "2b77aa85a192086316a7f87ce140112d"} {"nl": {"description": "You are given an array $$$d_1, d_2, \\dots, d_n$$$ consisting of $$$n$$$ integer numbers.Your task is to split this array into three parts (some of which may be empty) in such a way that each element of the array belongs to exactly one of the three parts, and each of the parts forms a consecutive contiguous subsegment (possibly, empty) of the original array. Let the sum of elements of the first part be $$$sum_1$$$, the sum of elements of the second part be $$$sum_2$$$ and the sum of elements of the third part be $$$sum_3$$$. Among all possible ways to split the array you have to choose a way such that $$$sum_1 = sum_3$$$ and $$$sum_1$$$ is maximum possible.More formally, if the first part of the array contains $$$a$$$ elements, the second part of the array contains $$$b$$$ elements and the third part contains $$$c$$$ elements, then:$$$$$$sum_1 = \\sum\\limits_{1 \\le i \\le a}d_i,$$$$$$ $$$$$$sum_2 = \\sum\\limits_{a + 1 \\le i \\le a + b}d_i,$$$$$$ $$$$$$sum_3 = \\sum\\limits_{a + b + 1 \\le i \\le a + b + c}d_i.$$$$$$The sum of an empty array is $$$0$$$.Your task is to find a way to split the array such that $$$sum_1 = sum_3$$$ and $$$sum_1$$$ is maximum possible.", "input_spec": "The first line of the input contains one integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) — the number of elements in the array $$$d$$$. The second line of the input contains $$$n$$$ integers $$$d_1, d_2, \\dots, d_n$$$ ($$$1 \\le d_i \\le 10^9$$$) — the elements of the array $$$d$$$.", "output_spec": "Print a single integer — the maximum possible value of $$$sum_1$$$, considering that the condition $$$sum_1 = sum_3$$$ must be met. Obviously, at least one valid way to split the array exists (use $$$a=c=0$$$ and $$$b=n$$$).", "sample_inputs": ["5\n1 3 1 1 4", "5\n1 3 2 1 4", "3\n4 1 2"], "sample_outputs": ["5", "4", "0"], "notes": "NoteIn the first example there is only one possible splitting which maximizes $$$sum_1$$$: $$$[1, 3, 1], [~], [1, 4]$$$.In the second example the only way to have $$$sum_1=4$$$ is: $$$[1, 3], [2, 1], [4]$$$.In the third example there is only one way to split the array: $$$[~], [4, 1, 2], [~]$$$."}, "positive_code": [{"source_code": "\ndef solution\n n = read_int\n xs = read_ints\n\n max = 0\n\n prefs = Hash.new\n\n sum = 0\n xs.each.with_index do |x, i|\n # p x\n sum += x\n prefs[sum] = i\n end\n\n sum = 0\n (n-1).downto(0) do |j|\n x = xs[j]\n sum += x\n\n if prefs[sum] && prefs[sum] < j\n max = sum\n end\n\n end\n\n puts max\n\nend\n\ndef read_int\n gets.to_i\nend\n\ndef read_ints\n gets.split.map(&:to_i)\nend\n\ndef read_string\n gets.chomp\nend\n\nsolution unless ENV['TEST__MODE']\n\n"}, {"source_code": "N = gets.to_i\nas = gets.split.map(&:to_i)\ntotal = as.inject(:+)\nh1, h2 = {}, {}\nsum = 0\nN.times do |i|\n h1[sum] = true\n sum += as[i]\n break if sum*2 > total\nend\nsum = 0\n(N-1).downto(0) do |i|\n h2[sum] = true\n sum += as[i]\n break if sum*2 > total\nend\nh1.keys.sort.reverse.each do |sum|\n if h2[sum]\n puts sum\n exit\n end\nend"}], "negative_code": [], "src_uid": "f2fc865a44b39179261a7311adf48390"} {"nl": {"description": "Alexander is a well-known programmer. Today he decided to finally go out and play football, but with the first hit he left a dent on the new Rolls-Royce of the wealthy businessman Big Vova. Vladimir has recently opened a store on the popular online marketplace \"Zmey-Gorynych\", and offers Alex a job: if he shows his programming skills by solving a task, he'll work as a cybersecurity specialist. Otherwise, he'll be delivering some doubtful products for the next two years.You're given $$$n$$$ positive integers $$$a_1, a_2, \\dots, a_n$$$. Using each of them exactly at once, you're to make such sequence $$$b_1, b_2, \\dots, b_n$$$ that sequence $$$c_1, c_2, \\dots, c_n$$$ is lexicographically maximal, where $$$c_i=GCD(b_1,\\dots,b_i)$$$ - the greatest common divisor of the first $$$i$$$ elements of $$$b$$$. Alexander is really afraid of the conditions of this simple task, so he asks you to solve it.A sequence $$$a$$$ is lexicographically smaller than a sequence $$$b$$$ if and only if one of the following holds: $$$a$$$ is a prefix of $$$b$$$, but $$$a \\ne b$$$; in the first position where $$$a$$$ and $$$b$$$ differ, the sequence $$$a$$$ has a smaller element than the corresponding element in $$$b$$$.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 10^3$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^3$$$)  — the length of the sequence $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1,\\dots,a_n$$$ ($$$1 \\le a_i \\le 10^3$$$)  — the sequence $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^3$$$.", "output_spec": "For each test case output the answer in a single line  — the desired sequence $$$b$$$. If there are multiple answers, print any.", "sample_inputs": ["7\n2\n2 5\n4\n1 8 2 3\n3\n3 8 9\n5\n64 25 75 100 50\n1\n42\n6\n96 128 88 80 52 7\n5\n2 4 8 16 17"], "sample_outputs": ["5 2 \n8 2 1 3 \n9 3 8 \n100 50 25 75 64 \n42 \n128 96 80 88 52 7 \n17 2 4 8 16"], "notes": "NoteIn the first test case of the example, there are only two possible permutations $$$b$$$  — $$$[2, 5]$$$ and $$$[5, 2]$$$: for the first one $$$c=[2, 1]$$$, for the second one $$$c=[5, 1]$$$.In the third test case of the example, number $$$9$$$ should be the first in $$$b$$$, and $$$GCD(9, 3)=3$$$, $$$GCD(9, 8)=1$$$, so the second number of $$$b$$$ should be $$$3$$$.In the seventh test case of the example, first four numbers pairwise have a common divisor (a power of two), but none of them can be the first in the optimal permutation $$$b$$$."}, "positive_code": [{"source_code": "require 'prime'\n\nclass Integer\n def divisor_list\n return [] if self <= 0\n return [1] if self == 1\n\n prime_division.map.with_index { |(base, k), i|\n s = i.zero? ? 0 : 1\n (s..k).map { |n| base ** n }\n }.inject { |res, e| res + res.flat_map { |t| e.map { |v| t * v } } }.sort\n end\nend\n\nT = gets.to_i\n\nT.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n\n counter = Hash.new(0)\n primes = Hash.new { |h, k| h[k] = [] }\n\n a.each do |x|\n counter[x] += 1\n\n x.prime_division.each do |e, _|\n primes[e] << x\n end\n end\n\n ans = []\n s = a.max\n g = s\n\n loop {\n ans << s\n counter[s] -= 1\n cand = -1\n max_v = -Float::INFINITY\n\n s.prime_division.map(&:first).reverse_each do |e|\n primes[e].each do |num|\n next if counter[num] <= 0\n\n next_gcd = g.gcd(num)\n\n if max_v < next_gcd\n max_v = next_gcd\n cand = num\n end\n end\n end\n\n break if cand == -1\n g = g.gcd(cand)\n s = cand\n }\n\n counter.each do |num, cnt|\n next if cnt <= 0\n\n cnt.times do\n ans << num\n end\n end\n\n puts ans.join(' ')\nend\n"}, {"source_code": "t = gets.to_i\nt.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n b = [a.max]\n g = a.max\n max_idx = a.index(g)\n vis = a.map.with_index {|_, a_i| a_i == max_idx ? true : false}\n\n (n - 1).times do\n max_v = -1\n max_i = -1\n a.each_with_index do |a_i, i|\n next if vis[i]\n\n if g.gcd(a_i) > max_v\n max_i = i\n max_v = g.gcd(a_i)\n end\n end\n\n b << a[max_i]\n vis[max_i] = true\n g = max_v\n end\n\n puts b.join(' ')\nend\n"}], "negative_code": [{"source_code": "require 'prime'\n\nT = gets.to_i\n\nT.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n \n counter = Hash.new(0)\n primes = Hash.new { |h, k| h[k] = [] }\n\n a.each do |x|\n counter[x] += 1\n\n x.prime_division.each do |e, cnt|\n primes[e] << x\n end\n end\n\n ans = []\n s = a.max\n\n loop {\n ans << s\n counter[s] -= 1\n cand = -1\n\n s.prime_division.map(&:first).reverse_each do |e|\n primes[e].each do |num|\n next if counter[num] <= 0\n\n cand = num\n break\n end\n\n break if cand != -1\n end\n\n break if cand == -1\n s = cand\n }\n\n counter.each do |num, cnt|\n next if cnt <= 0\n\n cnt.times do\n ans << num\n end\n end\n\n puts ans.join(' ')\nend\n"}, {"source_code": "require 'prime'\n\nclass Integer\n def divisor_list\n return [] if self <= 0\n return [1] if self == 1\n\n prime_division.map.with_index { |(base, k), i|\n s = i.zero? ? 0 : 1\n (s..k).map { |n| base ** n }\n }.inject { |res, e| res + res.flat_map { |t| e.map { |v| t * v } } }.sort\n end\nend\n\nT = gets.to_i\n\nT.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n\n counter = Hash.new(0)\n primes = Hash.new { |h, k| h[k] = [] }\n\n a.each do |x|\n counter[x] += 1\n\n x.prime_division.each do |e, _|\n primes[e] << x\n end\n end\n\n ans = []\n s = a.max\n gcd = s\n\n loop {\n ans << s\n counter[s] -= 1\n cand = -1\n max_v = -Float::INFINITY\n\n s.prime_division.reverse_each do |e|\n primes[e].each do |num|\n next if counter[num] <= 0\n\n next_gcd = gcd.gcd(num)\n\n if max_v < next_gcd\n max_v = next_gcd\n cand = num\n end\n end\n end\n\n break if cand == -1\n gcd = gcd.gcd(cand)\n s = cand\n }\n\n counter.each do |num, cnt|\n next if cnt <= 0\n\n cnt.times do\n ans << num\n end\n end\n\n puts ans.join(' ')\nend\n"}, {"source_code": "require 'prime'\n\nclass Integer\n def divisor_list\n return [] if self <= 0\n return [1] if self == 1\n\n prime_division.map.with_index { |(base, k), i|\n s = i.zero? ? 0 : 1\n (s..k).map { |n| base ** n }\n }.inject { |res, e| res + res.flat_map { |t| e.map { |v| t * v } } }.sort\n end\nend\n\nT = gets.to_i\n\nT.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n\n counter = Hash.new(0)\n primes = Hash.new { |h, k| h[k] = [] }\n\n a.each do |x|\n counter[x] += 1\n\n x.prime_division.each do |e, _|\n primes[e] << x\n end\n end\n\n ans = []\n s = a.max\n gcd = s\n\n loop {\n ans << s\n counter[s] -= 1\n cand = -1\n max_v = -Float::INFINITY\n\n s.prime_division.map(&:first).reverse_each do |e|\n primes[e].each do |num|\n next if counter[num] <= 0\n\n next_gcd = gcd.gcd(num)\n\n if max_v < next_gcd\n max_v = next_gcd\n cand = num\n end\n end\n end\n\n break if cand == -1\n gcd = s.gcd(cand)\n s = cand\n }\n\n counter.each do |num, cnt|\n next if cnt <= 0\n\n cnt.times do\n ans << num\n end\n end\n\n puts ans.join(' ')\nend\n"}, {"source_code": "require 'prime'\n\nclass Integer\n def divisor_list\n return [] if self <= 0\n return [1] if self == 1\n\n prime_division.map.with_index { |(base, k), i|\n s = i.zero? ? 0 : 1\n (s..k).map { |n| base ** n }\n }.inject { |res, e| res + res.flat_map { |t| e.map { |v| t * v } } }.sort\n end\nend\n\nT = gets.to_i\n\nT.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n\n counter = Hash.new(0)\n primes = Hash.new { |h, k| h[k] = [] }\n\n a.each do |x|\n counter[x] += 1\n\n x.divisor_list.each do |e, _|\n primes[e] << x\n end\n end\n\n ans = []\n s = a.max\n gcd = s\n\n loop {\n ans << s\n counter[s] -= 1\n cand = -1\n max_v = -Float::INFINITY\n\n s.divisor_list.reverse_each do |e|\n primes[e].each do |num|\n next if counter[num] <= 0\n\n next_gcd = gcd.gcd(num)\n\n if max_v < next_gcd\n max_v = next_gcd\n cand = num\n end\n end\n end\n\n break if cand == -1\n gcd = s.gcd(cand)\n s = cand\n }\n\n counter.each do |num, cnt|\n next if cnt <= 0\n\n cnt.times do\n ans << num\n end\n end\n\n puts ans.join(' ')\nend\n"}, {"source_code": "require 'prime'\n\nclass Integer\n def divisor_list\n return [] if self <= 0\n return [1] if self == 1\n\n prime_division.map.with_index { |(base, k), i|\n s = i.zero? ? 0 : 1\n (s..k).map { |n| base ** n }\n }.inject { |res, e| res + res.flat_map { |t| e.map { |v| t * v } } }.sort\n end\nend\n\nT = gets.to_i\n\nT.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n\n counter = Hash.new(0)\n primes = Hash.new { |h, k| h[k] = [] }\n\n a.each do |x|\n counter[x] += 1\n\n x.divisor_list.each do |e, cnt|\n primes[e] << x\n end\n end\n\n ans = []\n s = a.max\n\n loop {\n ans << s\n counter[s] -= 1\n cand = -1\n\n s.divisor_list.reverse_each do |e|\n primes[e].each do |num|\n next if counter[num] <= 0\n\n cand = num\n break\n end\n\n break if cand != -1\n end\n\n break if cand == -1\n s = cand\n }\n\n counter.each do |num, cnt|\n next if cnt <= 0\n\n cnt.times do\n ans << num\n end\n end\n\n puts ans.join(' ')\nend\n"}, {"source_code": "t = gets.to_i\nt.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n b = [a.max]\n g = a.max\n vis = a.map {|a_i| a_i == g ? true : false}\n\n (n - 1).times do\n max_v = -1\n max_i = -1\n a.each_with_index do |a_i, i|\n next if vis[i]\n\n if g.gcd(a_i) > max_v\n max_i = i\n max_v = g.gcd(a_i)\n end\n end\n\n b << a[max_i]\n vis[max_i] = true\n g = max_v\n end\n\n puts b.join(' ')\nend\n"}, {"source_code": "t = gets.to_i\nt.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n b = [a.max]\n g = a.max\n vis = a.map {|a_i| a_i == g ? true : false}\n\n (n - 1).times do\n max_v = -1\n max_i = -1\n max_ai = -1\n a.each_with_index do |a_i, i|\n next if vis[i]\n\n if g.gcd(a_i) > max_v || (g.gcd(a_i) == max_v && a_i > max_ai)\n max_i = i\n max_v = g.gcd(a_i)\n max_ai = a_i\n end\n end\n\n b << a[max_i]\n vis[max_i] = true\n g = max_v\n end\n\n puts b.join(' ')\nend\n"}, {"source_code": "t = gets.to_i\nt.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n max_v = a.max\n b = a.map {|a_i| [a_i, a_i.gcd(max_v)]}.sort_by {|av, gv| [-gv, -av]}.map(&:first)\n puts b.join(' ')\nend\n"}], "src_uid": "bdd1974e46f99eff3d03ed4174158dd9"} {"nl": {"description": "One day n friends gathered together to play \"Mafia\". During each round of the game some player must be the supervisor and other n - 1 people take part in the game. For each person we know in how many rounds he wants to be a player, not the supervisor: the i-th person wants to play ai rounds. What is the minimum number of rounds of the \"Mafia\" game they need to play to let each person play at least as many rounds as they want?", "input_spec": "The first line contains integer n (3 ≤ n ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the i-th number in the list is the number of rounds the i-th person wants to play.", "output_spec": "In a single line print a single integer — the minimum number of game rounds the friends need to let the i-th person play at least ai rounds. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.", "sample_inputs": ["3\n3 2 2", "4\n2 2 2 2"], "sample_outputs": ["4", "3"], "notes": "NoteYou don't need to know the rules of \"Mafia\" to solve this problem. If you're curious, it's a game Russia got from the Soviet times: http://en.wikipedia.org/wiki/Mafia_(party_game)."}, "positive_code": [{"source_code": "n = gets.to_i\na = gets.split.collect{|i| i.to_i}\namax = a.max\nans = 0\na.each{|aa| ans += amax-aa}\nif amax <= ans\n puts amax\nelse\n amax -= ans\n ans += amax/(n-1)*n\n amax %= n-1\n ans += amax+1 if amax > 0\n puts ans\nend\n"}, {"source_code": "#! /usr/bin/env ruby\n\nn = STDIN.gets.to_i\nrounds = STDIN.gets.to_s.split(' ').map(&:to_i).take(n)\n\nif rounds.length != n\n raise ArgumentError.new\nend\n\ndef how_many(rounds, n)\n max = rounds.max\n diff_total = rounds.inject(0) {|diff, round| diff += max - round}\n\n if diff_total >= max\n max\n else\n n -= 1\n rest = max - diff_total\n max + (rest / n) + (rest % n == 0 ? 0 : 1)\n end\nend\n\nputs how_many(rounds, n)\n\n"}, {"source_code": "n = gets.to_i\narr = gets.split(\" \").map{|i| i.to_i}\nsum = arr.inject(:+)\nmax = arr.max\nans = (sum + n - 2) / (n - 1)\nputs max > ans ? max : ans"}, {"source_code": "n=gets.to_i; d=gets.split.map(&:to_i); p [d.max,(d.reduce(&:+)+n-2)/(n-1)].max"}, {"source_code": "n = gets.to_i\na = gets.split.collect{|i| i.to_i}\namax = a.max\nans = 0\na.each{|aa| ans += amax-aa}\nif amax <= ans\n puts amax\nelse\n amax -= ans\n ans += amax/(n-1)*n\n amax %= n-1\n ans += amax+1 if amax > 0\n puts ans\nend\n"}, {"source_code": "\n\nn=gets.to_i; d=gets.split.map(&:to_i); p [d.max,(d.reduce(&:+)+n-2)/(n-1)].max"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nmx, s = [0, 0]\na.each {|i|\n mx = [mx, i].max\n s += i\n}\nputs [mx, (s+n-2).div(n.pred)].max"}], "negative_code": [{"source_code": "#! /usr/bin/env ruby\n\nn = STDIN.gets.to_i\nrounds = STDIN.gets.to_s.split(' ').map(&:to_i).sample(n)\n\nif rounds.length != n\n raise ArgumentError.new\nend\n\ndef how_many(rounds)\n max = rounds.max\n diff_total = rounds.inject(0) {|diff, round| diff += max - round}\n\n if diff_total == 0\n return max + (max / rounds.length) + ( max % rounds.length == 0 ? 0 : 1)\n end\n\n if diff_total >= max\n return max\n end\n\n max + (max - diff_total)\nend\n\nputs how_many(rounds)\n\n"}, {"source_code": "#! /usr/bin/env ruby\n\nn = STDIN.gets.to_i\nrounds = STDIN.gets.to_s.split(' ').map(&:to_i).sample(n)\n\nif rounds.length != n\n raise ArgumentError.new\nend\n\ndef how_many(rounds)\n max = rounds.max\n diff_total = rounds.inject(0) {|diff, round| diff += max - round}\n\n if diff_total == 0\n return max + (max / rounds.length) + 1\n end\n\n if diff_total >= max\n return max\n end\n\n return max + (max - diff_total)\nend\n\nputs how_many(rounds)\n\n"}, {"source_code": "#! /usr/bin/env ruby\n\nn = STDIN.gets.to_i\nrounds = STDIN.gets.to_s.split(' ').map(&:to_i).take(n)\n\nif rounds.length != n\n raise ArgumentError.new\nend\n\ndef how_many(rounds, n)\n max = rounds.max\n diff_total = rounds.inject(0) {|diff, round| diff += max - round}\n\n if diff_total == 0\n return max + (max / n) + ( max % n == 0 ? 0 : 1)\n end\n\n if diff_total >= max\n return max\n end\n\n max + (max - diff_total)\nend\n\nputs how_many(rounds, n)\n\n"}, {"source_code": "n = gets.to_i\na = gets.split.collect{|i| i.to_i}\namax = a.max\ncnt = 0\na.each{|aa| cnt += amax-aa}\nif amax <= cnt\n puts amax\nelse\n amax -= cnt\n puts cnt+amax/(n-1)*n+amax%(n-1)+1\nend\n"}, {"source_code": "n = gets.to_i\na = gets.split.collect{|i| i.to_i}\namax = a.max\ncnt = 0\na.each{|aa| cnt += amax-aa}\namax -= cnt\nif amax <= 0\n puts cnt\nelse\n puts cnt+amax/(n-1)*n+amax%(n-1)+1\nend\n"}, {"source_code": "n = gets.to_i\na = gets.split.collect{|i| i.to_i}\namax = a.max\ncnt = 0\na.each{|aa| cnt += amax-aa}\namax -= cnt\nif amax.zero?\n puts cnt\nelse\n puts cnt+amax/(n-1)*n+amax%(n-1)+1\nend\n"}, {"source_code": "n = gets.to_i\na = gets.split.collect{|i| i.to_i}\namin, amax = a.minmax\nans = 0\nif amin/(n-1) > 0\n amax -= amin\n ans += amin/(n-1)*n\n amin %= n-1\nend\nif amax != amin\n ans += amax\nelse\n ans += amax+1\nend\nputs ans\n"}, {"source_code": "n = gets.to_i\na = gets.split.collect{|i| i.to_i}\namax = a.max\ncnt = 0\na.each{|aa| cnt += amax-aa}\namax -= cnt\nputs cnt+amax/(n-1)*n+amax%(n-1)+1\n"}, {"source_code": "n = gets.to_i\na = gets.split.collect{|i| i.to_i}\nputs a.max+1\n"}, {"source_code": "n = gets.to_i\na = gets.split.collect{|i| i.to_i}\namin, amax = a.minmax\nans = 0\nif amin/(n-1) > 0\n amax -= amin\n ans += amin/(n-1)*n\n amin %= n-1\nend\nif amin.zero?\n ans += amax\nelse\n ans += amax+1\nend\nputs ans\n"}, {"source_code": "346"}, {"source_code": "people = gets.to_i\ntake_rounds = gets.split.map(&:to_i)\nmax = take_rounds.max\nsum = take_rounds.reduce(&:+)\nif sum <= max * people.pred\n puts max\nelse\n puts max.next\nend"}], "src_uid": "09f5623c3717c9d360334500b198d8e0"} {"nl": {"description": "The Human-Cow Confederation (HC2), led by Heidi, has built a base where people and cows can hide, guarded from zombie attacks. The entrance to the base is protected by an automated gate which performs a kind of a Turing test: it shows the entering creature a photograph and asks them whether the top and bottom halves of this photograph have been swapped or not. A person (or even a cow) will have no problem answering such questions; on the other hand, a zombie would just randomly smash one of the two buttons.The creature is asked a series of such questions. If at least 75% of them are answered correctly, the gate is unlocked; otherwise, a side door opens, beneath which a huge fan is spinning...Heidi is now building a robot army to fight the zombies, and she wants the robots to also be able to enter the base. You are tasked with programming them to distinguish the images. The first two images from the test set. The first picture has been rearranged, but not the second. ", "input_spec": "The first line of the input contains the number q of questions (1 ≤ q ≤ 220). After that, q questions follow, each of which in the format described below. The first line of every question contains two space-separated integers h and w (1 ≤ h, w ≤ 600) – the height (number of rows) and width (number of columns) of the photograph. (Most photographs are roughly 200 × 300.) After this, h lines follow, each describing a single row of the picture. The picture is monochrome (in shades of grey). Its i-th row is described by w space-separated integers aij (j = 1, ..., w), where aij is the brightness of the corresponding pixel (0 ≤ aij < 256, where 0 is black and 255 is white). Each picture will be either a real-life photograph, or a real-life photograph which has been broken up into two pieces and rearranged. More precisely, in the latter case, the topmost rows have been moved to the bottom of the picture. It is guaranteed that h is even. There is only a single input file to be processed, called all.in, and it is downloadable from the online judge. You are also a given another input file, called sample.in, which contains the first 20 pictures from all.in; you are provided the correct answers for sample.in in sample.out. You are also given a directory easy_bmp, which contains the first 50 input photographs in the form of .bmp image files, as well as a directory easy_sample_original_bmp, which contains the first 20 images before rearrangement. Check the notes for the download links.", "output_spec": "Your program should print q lines. The i-th line should contain your answer for the i-th question: YES if the photograph has been rearranged and NO otherwise. Your answers will be accepted if they all conform to this format and if at least 75% of them are correct. Because the input is rather huge, feel free to process it locally and submit just your precomputed answers (i.e., a program which just prints your output for the input file all.in).", "sample_inputs": [], "sample_outputs": [], "notes": "NoteThe link to download all necessary files is http://assets.codeforces.com/files/690/easy_contestant_package.zip"}, "positive_code": [{"source_code": "print \"YES\nNO\nNO\nYES\nNO\nNO\nNO\nNO\nNO\nYES\nYES\nYES\nYES\nNO\nYES\nYES\nNO\nYES\nYES\nYES\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO\nYES\nYES\nNO\nYES\nNO\nNO\nNO\nYES\nYES\nYES\nNO\nYES\nYES\nNO\nYES\nYES\nNO\nYES\nYES\nYES\nNO\nYES\nNO\nNO\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nNO\nYES\nNO\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nYES\nNO\nYES\nNO\nNO\nNO\nYES\nYES\nNO\nYES\nYES\nNO\nNO\nNO\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nYES\nNO\nNO\nNO\nNO\nYES\nYES\nYES\nYES\nYES\nNO\nNO\nNO\nYES\nNO\nYES\nNO\nYES\nNO\nYES\nNO\nNO\nYES\nYES\nYES\nYES\nNO\nNO\nNO\nYES\nNO\nYES\nYES\nYES\nNO\nYES\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nNO\nYES\nNO\nNO\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nYES\nNO\nYES\nNO\nYES\nNO\nNO\nYES\nNO\nNO\nNO\nYES\nNO\nNO\nNO\nNO\nYES\nNO\nNO\nNO\nNO\nYES\nNO\nYES\nNO\nYES\nNO\nNO\nNO\nYES\nYES\nNO\nYES\nNO\nYES\nYES\nYES\nNO\nYES\nYES\nNO\nYES\nNO\nYES\nYES\nNO\nYES\nYES\nNO\nNO\nYES\nNO\nNO\nNO\nYES\nYES\nNO\nNO\n\"\n"}], "negative_code": [], "src_uid": "803f7612bcce44c3cc12988b5aa10225"} {"nl": {"description": "You are given an integer $$$n$$$ ($$$n > 1$$$).Your task is to find a sequence of integers $$$a_1, a_2, \\ldots, a_k$$$ such that: each $$$a_i$$$ is strictly greater than $$$1$$$; $$$a_1 \\cdot a_2 \\cdot \\ldots \\cdot a_k = n$$$ (i. e. the product of this sequence is $$$n$$$); $$$a_{i + 1}$$$ is divisible by $$$a_i$$$ for each $$$i$$$ from $$$1$$$ to $$$k-1$$$; $$$k$$$ is the maximum possible (i. e. the length of this sequence is the maximum possible). If there are several such sequences, any of them is acceptable. It can be proven that at least one valid sequence always exists for any integer $$$n > 1$$$.You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 5000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$2 \\le n \\le 10^{10}$$$). It is guaranteed that the sum of $$$n$$$ does not exceed $$$10^{10}$$$ ($$$\\sum n \\le 10^{10}$$$).", "output_spec": "For each test case, print the answer: in the first line, print one positive integer $$$k$$$ — the maximum possible length of $$$a$$$. In the second line, print $$$k$$$ integers $$$a_1, a_2, \\ldots, a_k$$$ — the sequence of length $$$k$$$ satisfying the conditions from the problem statement. If there are several answers, you can print any. It can be proven that at least one valid sequence always exists for any integer $$$n > 1$$$.", "sample_inputs": ["4\n2\n360\n4999999937\n4998207083"], "sample_outputs": ["1\n2 \n3\n2 2 90 \n1\n4999999937 \n1\n4998207083"], "notes": null}, "positive_code": [{"source_code": "require'prime'\ngets;$<.map{|l|i=l.to_i;x,y=i.prime_division.map{|k,v|[v-1,k]}.max;p x+1;puts ([y]*x+[i/y**x])*' '}"}, {"source_code": "require'prime'\ngets;$<.map{|l|i=l.to_i;x,y=i.prime_division.map{|k,v|[v-1,k]}.max;puts [x+1]+[y]*x+[i/y**x]}"}, {"source_code": "require 'prime'\nt = gets.chomp.to_i\nt.times do\n n = gets.chomp.to_i\n arr = n.prime_division\n res = []\n while arr.any? \n res << arr.map{|el| el[0]}.inject(:*)\n arr = arr.map{|el| [el[0], el[1] - 1]}\n arr.filter! {|el| el[1] > 0}\n end\n puts res.size\n puts res.reverse.join(' ')\nend"}, {"source_code": "\nrequire'prime'\ngets;$<.map{|l|i=l.to_i;x,y=i.prime_division.map{|k,v|[v-1,k]}.max;puts [x+1]+[y]*x+[i/y**x]}"}], "negative_code": [], "src_uid": "f8d064c90f1f2b4a18386795dbcd9cb9"} {"nl": {"description": "You are given an array $$$a$$$ of $$$n$$$ integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i < j$$$ and $$$a_j - a_i = j - i$$$.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$). Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le n$$$) — array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case output the number of pairs of indices $$$(i, j)$$$ such that $$$i < j$$$ and $$$a_j - a_i = j - i$$$.", "sample_inputs": ["4\n6\n3 5 1 4 6 6\n3\n1 2 3\n4\n1 3 3 4\n6\n1 6 3 4 5 6"], "sample_outputs": ["1\n3\n3\n10"], "notes": null}, "positive_code": [{"source_code": "t = gets.chomp.to_i\nt.times do\n hash = {}\n n = gets.chomp.to_i\n a = gets.split.map{|_|_.to_i}\n a.each_with_index do |num, i|\n hash[num-i] = (hash[num-i] || 0) + 1\n end\n puts hash.values.map{|v| v*(v-1)/2}.sum\nend"}, {"source_code": "t = gets.chomp.to_i\n\nt.times do\n hash = {}\n n = gets.chomp.to_i\n a = gets.split.map{|_|_.to_i}\n a.each_with_index do |num, i|\n hash[num-i] = (hash[num-i] || 0) + 1\n end\n rs = 0\n hash.values.each{|v| rs+=v*(v-1)/2}\n puts rs\nend"}, {"source_code": "def read_test_case\n # @type [String]\n line = gets.chomp\n # &: notation is sometimes called 'pretzel colon'\n # Kind of like a method references? It's syntax sugar for some Proc magic:\n # &:to_i == { |foo| foo.to_i }\n # :to_i here is a symbol, and:\n line.split(' ').map(&:to_i)\nend\n\n# @param nums [Array]\ndef get_diff_array(nums)\n nums.map.each_with_index do |num, idx|\n # Problem uses 1 indexing, so I'll be consistent\n num - (idx + 1)\n end\nend\n\n# @param diffs [Array]\ndef count_diff_array(diffs)\n counts = {}\n diffs.each do |diff|\n if !counts.include? diff\n counts[diff] = 1\n else\n counts[diff] += 1\n end\n end\n counts\nend\n\n# @param counts [Hash]\ndef count_total_pairs(counts)\n pairs = 0\n counts.each_value do |count|\n pairs += compute_triangular_number(count - 1)\n end\n pairs\nend\n\ndef compute_triangular_number(n)\n (n * (n + 1)) / 2\nend\n\n# @type[Integer]\nt = gets.chomp.to_i\n\n(1..t).each do\n _test_size = gets\n\n test = read_test_case\n diff_array = get_diff_array test\n counts = count_diff_array diff_array\n puts count_total_pairs counts\nend\n"}, {"source_code": "t = gets.to_s.to_i\n1.upto(t) do |casenum|\n n = gets.to_s.to_i\n a = gets.to_s.strip.split.map {|x| x.to_i }\n 0.upto(n - 1) {|i| a[i] -= i }\n cnt = {}\n a.each {|x| cnt[x] = (cnt[x] || 0) + 1 }\n result = 0\n cnt.each {|k, v| result += v * (v - 1) / 2 }\n printf(\"%d\\n\", result)\nend\n"}], "negative_code": [], "src_uid": "ac4aac095e71cbfaf8b79262d4038095"} {"nl": {"description": "Vasiliy spent his vacation in a sanatorium, came back and found that he completely forgot details of his vacation! Every day there was a breakfast, a dinner and a supper in a dining room of the sanatorium (of course, in this order). The only thing that Vasiliy has now is a card from the dining room contaning notes how many times he had a breakfast, a dinner and a supper (thus, the card contains three integers). Vasiliy could sometimes have missed some meal, for example, he could have had a breakfast and a supper, but a dinner, or, probably, at some days he haven't been at the dining room at all.Vasiliy doesn't remember what was the time of the day when he arrived to sanatorium (before breakfast, before dinner, before supper or after supper), and the time when he left it (before breakfast, before dinner, before supper or after supper). So he considers any of these options. After Vasiliy arrived to the sanatorium, he was there all the time until he left. Please note, that it's possible that Vasiliy left the sanatorium on the same day he arrived.According to the notes in the card, help Vasiliy determine the minimum number of meals in the dining room that he could have missed. We shouldn't count as missed meals on the arrival day before Vasiliy's arrival and meals on the departure day after he left.", "input_spec": "The only line contains three integers b, d and s (0 ≤ b, d, s ≤ 1018,  b + d + s ≥ 1) — the number of breakfasts, dinners and suppers which Vasiliy had during his vacation in the sanatorium. ", "output_spec": "Print single integer — the minimum possible number of meals which Vasiliy could have missed during his vacation. ", "sample_inputs": ["3 2 1", "1 0 0", "1 1 1", "1000000000000000000 0 1000000000000000000"], "sample_outputs": ["1", "0", "0", "999999999999999999"], "notes": "NoteIn the first sample, Vasiliy could have missed one supper, for example, in case he have arrived before breakfast, have been in the sanatorium for two days (including the day of arrival) and then have left after breakfast on the third day. In the second sample, Vasiliy could have arrived before breakfast, have had it, and immediately have left the sanatorium, not missing any meal.In the third sample, Vasiliy could have been in the sanatorium for one day, not missing any meal. "}, "positive_code": [{"source_code": "a, b, c = gets.split.map(&:to_i)\nx = 0\nif a>=b && a>=c\n x+=1 if a!=b\n x+=1 if a!=c\n p a-b+a-c-x\nelsif c>=b && c>=a\n x+=1 if c!=b\n x+=1 if c!=a\n p c-b+c-a-x\nelse\n x+=1 if c!=b\n x+=1 if b!=a\n p b-c+b-a-x\nend\n "}, {"source_code": "a=gets.split.map(&:to_i)\nm=a.max-1\np a.select{|e|es)==-1\n}\na, b, c=t\n\nres=1<<64\nx=[(a-b-1)+(a-c-1)+(a>b ? 0 : 1), 0].max\ny=[(a-b)+(a-c-1)+(b>c ? 0 : 1), 0].max\nz=(a-b)+(a-c)\n\np [x, y, z].min\n"}, {"source_code": "a=gets.split.map &:to_i\nm=a.max-r=1\na.map{|e|e meat_cnt[0]\n meat_cnt[1]-=1 if meat_cnt[2]==meat_cnt[1]\n meat_cnt[2]-=1\n return 0 if meat_cnt.all? {|x| x==meat_cnt[2]}\n meat_at_first_day=true\n end\n\n if meat_cnt.max == meat_cnt[0] && meat_cnt[0]>meat_cnt[2]\n meat_cnt[1]-=1 if meat_cnt[0]== meat_cnt[1]\n meat_cnt[0]-=1\n return 0 if meat_cnt.all? {|x| x==meat_cnt[0]}\n meat_at_last_day=true\n end\n\n if meat_cnt.max == meat_cnt[1]\n if !meat_at_last_day && meat_cnt[1]>meat_cnt[2]\n meat_cnt[1]-=1\n if meat_cnt[0]>0\n meat_cnt[0]-=1\n else\n result+=1\n end\n end\n if !meat_at_first_day && meat_cnt[1]>meat_cnt[0]\n meat_cnt[1]-=1\n if meat_cnt[2]>0\n meat_cnt[2]-=1\n else\n result+=1\n end\n end\n return result if meat_cnt.all? {|x| x==meat_cnt[1]}\n end\n meat_cnt=(meat_cnt.map { |x| x-meat_cnt.min })\n return 0 if meat_cnt.all? {|x| x.zero?}\n result+=case meat_cnt.count(0)\n when 2\n meat_cnt.reduce(:+)*2\n when 1\n meat_cnt.select {|x| x.nonzero?}.minmax.reduce(0) {|sum,x| (sum.zero?) ? (sum-x):(sum+x*2)}\n end\n end\n\n def self.run\n puts get_advice($stdin.readline.chomp.split.map(&:to_i))\n end\nend\n\nif __FILE__ == $0\n Sanatorium.run\nend"}, {"source_code": "def ev(arr)\n arr.sort!\n arr[2] - arr[0] + arr[2] - arr[1]\nend\n\nb, d, s = gets.strip.split(' ').map(&:to_i)\nmn = [b, d, s].min\nb -= mn\nd -= mn\ns -= mn\nif b == d && d == s\n printf(\"%d\\n\", 0)\nelse\n v = []\n arr = [b, d, s]\n 0.upto(2) do |i|\n 0.upto(2) do |j|\n arr_c1 = arr.clone\n arr_c1[i] += 1\n arr_c2 = arr.clone\n arr_c2[j] += 1\n arr_c3 = arr.clone\n arr_c3[i] += 1\n arr_c3[j] += 1 if i != j\n v << ev(arr_c1)\n v << ev(arr_c2)\n v << ev(arr_c3)\n end\n end\n\n printf(\"%d\\n\", v.min)\nend\n"}], "negative_code": [{"source_code": "a, b, c = gets.split.map(&:to_i)\np [[a, b, c].max-[a, b, c].min-1, 0].max"}, {"source_code": "\na = gets.split.map &:to_i\nm = a.max - r = 1\na.map{|e| e < m && r += m - e; puts r}\np r - 1\n"}, {"source_code": "a=gets.split.map &:to_i\ndef solution(b,d,s)\n\tmax = [b,d,s].max\n\tsum = 0\n\tcase max\n\twhen b\n\t\tsum += max - d - 1 if max > d + 1\n\t\tsum += max - s - 1 if max > s + 1\n\twhen d\n\t\tsum += max - b if max > b\n\t\tsum += max - s - 1 if max > s + 1\n\twhen s\n\t\tsum += max - b if max > b\n\t\tsum += max - d if max > d\n\tend\n\tsum\nend\np solution(a[0],a[1],a[2])\n"}, {"source_code": "a=gets.split.map &:to_i\ndef solution(b,d,s)\n\tmax = [b,d,s].max\n\tsum = 0\n\tcase max\n\twhen b\n\t\tsum += max - d - 1 if max > d + 1\n\t\tsum += max - s - 1 if max > s + 1\n\twhen d\n\t\tsum += max - b if max > b && b != 0\n\t\tsum += max - s - 1 if max > s + 1\n\twhen s\n\t\tsum += max - b if max > b && b != 0\n\t\tsum += max - d if max > d && d != 0\n\tend\n\tsum\nend\np solution(a[0],a[1],a[2])"}, {"source_code": "def ev(arr)\n b, d, s = arr\n (b - d).abs + (d - s).abs\nend\n\nb, d, s = gets.strip.split(' ').map(&:to_i)\nmn = [b, d, s].min\nb -= mn\nd -= mn\ns -= mn\nif b == d && d == s\n printf(\"%d\\n\", 0)\nelse\n v = []\n arr = [b, d, s]\n 0.upto(2) do |i|\n 0.upto(2) do |j|\n arr_c1 = arr.clone\n arr_c1[i] -= 1\n arr_c2 = arr.clone\n arr_c2[j] -= 1\n arr_c3 = arr.clone\n arr_c3[i] -= 1\n arr_c3[j] -= 1\n v << ev(arr_c1)\n v << ev(arr_c2)\n v << ev(arr_c3)\n end\n end\n printf(\"%d\\n\", v.min)\nend\n"}, {"source_code": "b, d, s = gets.strip.split(' ').map(&:to_i)\nmn = [b, d, s].min\nb -= mn\nd -= mn\ns -= mn\nif b == d && d == s\n printf(\"%d\\n\", 0)\nelse\n arr = [b, d, s]\n mx = -1\n cnt = 0\n arr.each do |l|\n if l > mx\n mx = l\n cnt = 1\n elsif l == mx\n cnt += 1\n end\n end\n if cnt == 2\n 0.upto(2) do |i|\n arr[i] -= 1 if mx == arr[i]\n end\n printf(\"%d\\n\", arr.max - arr.min)\n else\n 0.upto(2) do |i|\n arr[i] -= 1 if mx == arr[i]\n end\n printf(\"%d\\n\", arr.max - arr.min) \n end\nend\n"}, {"source_code": "b, d, s = gets.strip.split(' ').map(&:to_i)\nmn = [b, d, s].min\nb -= mn\nd -= mn\ns -= mn\nif b == d && d == s\n printf(\"%d\\n\", 0)\nelse\n arr = [b, d, s]\n mx = -1\n cnt = 0\n arr.each do |l|\n if l > mx\n mx = l\n cnt = 1\n elsif l == mx\n cnt += 1\n end\n end\n if cnt == 2\n 0.upto(2) do |i|\n arr[i] -= 1 if mx == arr[i]\n end\n printf(\"%d\\n\", arr.max - arr.min)\n else\n 0.upto(2) do |i|\n arr[i] -= 1 if mx == arr[i]\n end\n if arr.uniq == 3\n printf(\"%d\\n\", (arr[0] - arr[1]).abs + (arr[1] - arr[2]).abs - 1) \n else\n printf(\"%d\\n\", arr.max - arr.min)\n end\n end\nend\n"}, {"source_code": "def ev(arr)\n b, d, s = arr\n ((b - d).abs + (d - s).abs + (s - b).abs) / 2\nend\n\nb, d, s = gets.strip.split(' ').map(&:to_i)\nmn = [b, d, s].min\nb -= mn\nd -= mn\ns -= mn\nif b == d && d == s\n printf(\"%d\\n\", 0)\nelse\n v = []\n arr = [b, d, s]\n 0.upto(2) do |i|\n 0.upto(2) do |j|\n arr_c1 = arr.clone\n arr_c1[i] -= 1\n arr_c2 = arr.clone\n arr_c2[j] -= 1\n arr_c3 = arr.clone\n arr_c3[i] -= 1\n arr_c3[j] -= 1\n v << ev(arr_c1)\n v << ev(arr_c2)\n v << ev(arr_c3)\n end\n end\n printf(\"%d\\n\", v.min)\nend\n"}], "src_uid": "b34846d90dc011f2ef37a8256202528a"} {"nl": {"description": "Timur likes his name. As a spelling of his name, he allows any permutation of the letters of the name. For example, the following strings are valid spellings of his name: Timur, miurT, Trumi, mriTu. Note that the correct spelling must have uppercased T and lowercased other letters.Today he wrote string $$$s$$$ of length $$$n$$$ consisting only of uppercase or lowercase Latin letters. He asks you to check if $$$s$$$ is the correct spelling of his name.", "input_spec": "The first line of the input contains an integer $$$t$$$ ($$$1 \\leq t \\leq 10^3$$$) — the number of test cases. The first line of each test case contains an integer $$$n$$$ $$$(1 \\leq n \\leq 10)$$$ — the length of string $$$s$$$. The second line of each test case contains a string $$$s$$$ consisting of only uppercase or lowercase Latin characters.", "output_spec": "For each test case, output \"YES\" (without quotes) if $$$s$$$ satisfies the condition, and \"NO\" (without quotes) otherwise. You can output the answer in any case (for example, the strings \"yEs\", \"yes\", \"Yes\" and \"YES\" will be recognized as a positive answer).", "sample_inputs": ["10\n\n5\n\nTimur\n\n5\n\nmiurT\n\n5\n\nTrumi\n\n5\n\nmriTu\n\n5\n\ntimur\n\n4\n\nTimr\n\n6\n\nTimuur\n\n10\n\ncodeforces\n\n10\n\nTimurTimur\n\n5\n\nTIMUR"], "sample_outputs": ["YES\nYES\nYES\nYES\nNO\nNO\nNO\nNO\nNO\nNO"], "notes": null}, "positive_code": [{"source_code": "t = gets.chomp.to_i \r\n\r\nfor i in 0..t-1\r\n \r\n n = gets.chomp.to_i \r\n name = gets.chomp\r\n if name = name.chars.sort.join == \"Timru\"\r\n puts \"YES\"\r\n else \r\n puts \"NO\"\r\n end\r\n \r\nend"}, {"source_code": "gets.to_i.times do\r\n gets\r\n puts gets.chomp.chars.sort == 'Timur'.chars.sort ? :YES : :NO\r\nend"}, {"source_code": "gets.to_i.times do\r\n gets\r\n puts gets.chomp.chars.sort == 'Timur'.chars.sort ? :YES : :NO\r\nend\r\n"}], "negative_code": [{"source_code": "t = gets.chomp.to_i \r\n\r\nfor i in 0..t-1\r\n \r\n n = gets.chomp.to_i \r\n name = gets.chomp\r\n if name = name.chars.sort.join == \"Timru\"\r\n print \"Yes\"\r\n else \r\n print \"No\"\r\n end\r\n \r\nend"}], "src_uid": "6c137a74b36dede61037cb3b05167329"} {"nl": {"description": "You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty.You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples.Can you make array $$$c$$$ sorted in non-decreasing order?", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 2 \\cdot 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^6$$$) — the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive).", "sample_inputs": ["3\n\n4\n\n3 1 5 3\n\n3\n\n3 2 1\n\n1\n\n7331"], "sample_outputs": ["YES\nNO\nYES"], "notes": "NoteIn the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$:Step $$$1$$$: $$$a$$$$$$[3, 1, 5, 3]$$$$$$\\Rightarrow$$$$$$[3, 1, 5]$$$$$$\\Rightarrow$$$$$$[3, 1]$$$$$$\\Rightarrow$$$$$$[3]$$$$$$\\Rightarrow$$$$$$[]$$$$$$b$$$$$$[]$$$$$$[\\underline{3}]$$$$$$[3, \\underline{5}]$$$$$$[3, \\underline{1}, 5]$$$$$$[3, \\underline{3}, 1, 5]$$$Step $$$2$$$: $$$b$$$$$$[3, 3, \\underline{1}, 5]$$$$$$\\Rightarrow$$$$$$[3, \\underline{3}, 5]$$$$$$\\Rightarrow$$$$$$[\\underline{3}, 5]$$$$$$\\Rightarrow$$$$$$[\\underline{5}]$$$$$$\\Rightarrow$$$$$$[]$$$$$$c$$$$$$[]$$$$$$[1]$$$$$$[1, 3]$$$$$$[1, 3, 3]$$$$$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n gets\r\n as = gets.split.map(&:to_i)\r\n ((as.size % 2)..as.size - 2).step(2).each do |i|\r\n if as[i] > as[i + 1]\r\n as[i], as[i + 1] = as[i + 1], as[i]\r\n end\r\n end\r\n ok = as.each_cons(2).all? { |a, b| a <= b }\r\n puts ok ? \"YES\" : \"NO\"\r\nend\r\n"}, {"source_code": "T = gets.to_i\nT.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n a.unshift(0) if n.odd?\n n = a.size\n puts (0 ... n / 2 - 1).all? { |i| a[i * 2, 2].max <= a[i * 2 + 2, 2].min } ? \"YES\" : \"NO\"\nend"}], "negative_code": [{"source_code": "gets.to_i.times do\r\n gets\r\n as = gets.split.map(&:to_i)\r\n (0..as.size - 2).step(2).each do |i|\r\n if as[i] > as[i + 1]\r\n as[i], as[i + 1] = as[i + 1], as[i]\r\n end\r\n end\r\n ok = as.each_cons(2).all? { |a, b| a <= b }\r\n puts ok ? \"YES\" : \"NO\"\r\nend\r\n"}, {"source_code": "T = gets.to_i\nT.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n a << 10**18 if n.odd?\n n = a.size\n puts (0 ... n / 2 - 1).all? { |i| a[i * 2, 2].max <= a[i * 2 + 2, 2].min } ? \"YES\" : \"NO\"\nend"}], "src_uid": "95b35c53028ed0565684713a93910860"} {"nl": {"description": "A palindrome is a string that reads the same backward as forward. For example, the strings \"z\", \"aaa\", \"aba\", and \"abccba\" are palindromes, but \"codeforces\" and \"ab\" are not. You hate palindromes because they give you déjà vu.There is a string $$$s$$$. You must insert exactly one character 'a' somewhere in $$$s$$$. If it is possible to create a string that is not a palindrome, you should find one example. Otherwise, you should report that it is impossible.For example, suppose $$$s=$$$ \"cbabc\". By inserting an 'a', you can create \"acbabc\", \"cababc\", \"cbaabc\", \"cbabac\", or \"cbabca\". However \"cbaabc\" is a palindrome, so you must output one of the other options.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1\\le t\\le 10^4$$$) — the number of test cases. The only line of each test case contains a string $$$s$$$ consisting of lowercase English letters. The total length of all strings does not exceed $$$3\\cdot 10^5$$$.", "output_spec": "For each test case, if there is no solution, output \"NO\". Otherwise, output \"YES\" followed by your constructed string of length $$$|s|+1$$$ on the next line. If there are multiple solutions, you may print any. You can print each letter of \"YES\" and \"NO\" in any case (upper or lower).", "sample_inputs": ["6\ncbabc\nab\nzza\nba\na\nnutforajaroftuna"], "sample_outputs": ["YES\ncbabac\nYES\naab\nYES\nzaza\nYES\nbaa\nNO\nYES\nnutforajarofatuna"], "notes": "NoteThe first test case is described in the statement.In the second test case, we can make either \"aab\" or \"aba\". But \"aba\" is a palindrome, so \"aab\" is the only correct answer.In the third test case, \"zaza\" and \"zzaa\" are correct answers, but not \"azza\".In the fourth test case, \"baa\" is the only correct answer.In the fifth test case, we can only make \"aa\", which is a palindrome. So the answer is \"NO\".In the sixth test case, \"anutforajaroftuna\" is a palindrome, but inserting 'a' elsewhere is valid."}, "positive_code": [{"source_code": "def palindrome?(string)\n string == string.reverse\nend\n\n# returns correct string if found else returns nil\ndef no_palindrome(string)\n # appending 'a' before or after a string is enough to break a palindrome\n return \"a#{string}\" unless palindrome?(\"a#{string}\")\n return \"#{string}a\" unless palindrome?(\"#{string}a\")\n\n nil\nend\n\n# prints output for test cases\ndef print_answer(string)\n answer = no_palindrome(string)\n\n return puts 'NO' if answer.nil?\n\n puts 'YES'\n puts answer\nend\n\n#### code for tests:\n\nnumber_of_cases = gets.chomp.to_i\n\nnumber_of_cases.times do\n string = gets.chomp\n print_answer(string)\nend\n"}, {"source_code": "n=gets.rstrip.to_i\nn.times do\n s=gets.rstrip\n valid=false\n s.each_char do |c|\n if c!='a'\n valid=true\n break\n end\n end\n if valid\n puts \"YES\"\n s2=s+'a'\n if s2==s2.reverse\n puts 'a'+s\n else\n puts s2\n end\n else\n puts \"NO\"\n end\n end"}, {"source_code": "def is_pal?(s)\n half = s.size / 2\n s[0...half] === s[-half..-1].reverse\nend\n\ngets.to_i.times {\n s = gets.chomp\n if s.count('a') === s.size\n puts 'NO'\n else\n puts 'YES'\n s2 = \"a#{s}\"\n if !is_pal?(s2)\n puts s2\n else\n puts \"#{s}a\"\n end\n end\n}\n"}, {"source_code": "gets.to_i.times do\r\n s=gets.chomp\r\n z=s.size\r\n i=0\r\n j=z-1\r\n f=false\r\n while i<=j\r\n if s[i]==\"a\"&&s[j]==\"a\"\r\n i+=1\r\n j-=1\r\n elsif s[j]==\"a\"\r\n puts \"YES\"\r\n if j==z-1\r\n puts s+\"a\"\r\n else\r\n puts s[0..j]+\"a\"+s[j+1..]\r\n end\r\n f=true\r\n break\r\n else\r\n puts \"YES\"\r\n if i==0\r\n puts \"a\"+s\r\n else\r\n puts s[0...i]+\"a\"+s[i..]\r\n end\r\n f=true\r\n break\r\n end\r\n end\r\n if !f\r\n puts \"NO\"\r\n end\r\nend\r\n\r\n"}], "negative_code": [], "src_uid": "e2dc3de62fc45c7e9ddb92daa5c5d8de"} {"nl": {"description": "A championship is held in Berland, in which $$$n$$$ players participate. The player with the number $$$i$$$ has $$$a_i$$$ ($$$a_i \\ge 1$$$) tokens.The championship consists of $$$n-1$$$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship.All random decisions that are made during the championship are made equally probable and independently.For example, if $$$n=4$$$, $$$a = [1, 2, 4, 3]$$$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $$$a = [0, 2, 4, 4]$$$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $$$a = [0, 2, 8, 0]$$$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $$$a = [0, 0, 10, 0]$$$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. ", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case consists of one positive integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) — the number of players in the championship. The second line of each test case contains $$$n$$$ positive integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$) — the number of tokens the players have. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$. ", "output_spec": "For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. ", "sample_inputs": ["2\n4\n1 2 4 3\n5\n1 1 1 1 1"], "sample_outputs": ["3\n2 3 4 \n5\n1 2 3 4 5"], "notes": null}, "positive_code": [{"source_code": "gets.to_i.times do\n\tn = gets.to_i\n\ta = gets.split.map(&:to_i).each_with_index.to_a.sort\n\tsum = 0\n\n\tf = -1\n\t(0...n).each do |i|\n\t\tsum += a[i][0]\n\t\tf = i unless i == n - 1 || sum >= a[i + 1][0]\n\tend\n\tans = a[f + 1..].map { _1[1] + 1 }.sort\n\tputs ans.size, ans.join(' ')\nend\n"}], "negative_code": [{"source_code": "gets.to_i.times do\n\tn = gets.to_i\n\ta = gets.split.map(&:to_i).each_with_index.to_a.sort\n\tsum = 0\n\n\tans = []\n\t(0...n).each do |i|\n\t\tsum += a[i][0]\n\t\tans << a[i][1] + 1 if i == n - 1 || sum >= a[i + 1][0]\n\tend\n\tputs ans.size, ans.sort.join(' ')\nend\n"}, {"source_code": "gets.to_i.times do\n\tn = gets.to_i\n\ta = gets.split.map(&:to_i).sort\n\tsum = 0\n\tans =\n\t\t(0...n).select do |i|\n\t\t\tsum += a[i]\n\t\t\ti == n - 1 || sum >= a[i + 1]\n\t\tend.map { _1 + 1 }\n\tputs ans.size, ans.join(' ')\nend\n"}], "src_uid": "debce043777e7e575f77a94edf89c7f1"} {"nl": {"description": "Vika has n jars with paints of distinct colors. All the jars are numbered from 1 to n and the i-th jar contains ai liters of paint of color i.Vika also has an infinitely long rectangular piece of paper of width 1, consisting of squares of size 1 × 1. Squares are numbered 1, 2, 3 and so on. Vika decided that she will start painting squares one by one from left to right, starting from the square number 1 and some arbitrary color. If the square was painted in color x, then the next square will be painted in color x + 1. In case of x = n, next square is painted in color 1. If there is no more paint of the color Vika wants to use now, then she stops.Square is always painted in only one color, and it takes exactly 1 liter of paint. Your task is to calculate the maximum number of squares that might be painted, if Vika chooses right color to paint the first square.", "input_spec": "The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of jars with colors Vika has. The second line of the input contains a sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is equal to the number of liters of paint in the i-th jar, i.e. the number of liters of color i that Vika has.", "output_spec": "The only line of the output should contain a single integer — the maximum number of squares that Vika can paint if she follows the rules described above.", "sample_inputs": ["5\n2 4 2 3 3", "3\n5 5 5", "6\n10 10 10 1 10 10"], "sample_outputs": ["12", "15", "11"], "notes": "NoteIn the first sample the best strategy is to start painting using color 4. Then the squares will be painted in the following colors (from left to right): 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5.In the second sample Vika can start to paint using any color.In the third sample Vika should start painting using color number 5."}, "positive_code": [{"source_code": "gets\na = gets.split(\" \").map(&:to_i)\nn=a.size\nbase = a.min\nans = 0\n\nsans = base*n\n(a+a).each do |x|\n if x == base\n sans = base*n\n else\n sans += 1\n end\n ans = sans if sans > ans\nend\n\np ans\n"}, {"source_code": "n = gets.strip.to_i\nar = gets.strip.split.map(&:to_i)\nm = ar.min\na = ar.each_with_index.select{|e, i| e == m}\nc = a.reduce([]){|res, e| res < y[1]\n 1\n else\n x[0] <=> y[0]\n end \nend\n\ndef max(x, y) \n if x > y\n return x\n else \n return y\n end\nend\n\nans = a[0][1] * n\n\nx = 0\ni = 0\npL = a[0][0]\npR = a[0][0]\nwhile i + 1 < n && a[i][1] == a[i + 1][1]\n pR = a[i + 1][0]\n x = max(x, pR - a[i][0])\n i += 1\nend\n\nx = max(x, n - pR + pL) - 1\nans += x\nputs ans\n"}], "negative_code": [{"source_code": "n = gets.chomp.to_i\nas = gets.chomp.split.map(&:to_i)\n\namin = 10**9\nfst = Hash.new(n - 1)\nlst = Hash.new(0)\n\nfor i in 0...n\n a = as[i]\n amin = [amin, a].min\n fst[a] = [fst[a], i].min\n lst[a] = [lst[a], i].max\nend\n\nputs n * (amin + 1) - (lst[amin] - fst[amin] + 1)\n"}, {"source_code": "n = Integer(gets)\ns = gets\na = Array.new(n, Array.new(2))\nt = 0\nsz = 0\nfor i in 0...s.length\n c = s[i]\n if c == \" \"\n a[sz] = [sz, t]\n t = 0\n sz += 1\n else\n t = t * 10 + c.to_i\n end\nend\na[sz] = [sz,t]\nsz += 1\nt = 0\n\na.sort! do |x, y|\n if x[1] < y[1]\n -1\n elsif x[1] > y[1]\n 1\n else\n x[0] <=> y[0]\n end \nend\n\ndef max(x, y) \n if x > y\n return x\n else \n return y\n end\nend\n\nans = a[0][1] * n\n\nx = 0\ni = 0\npL = a[0][0]\npR = a[0][0]\nwhile i + 1 < n && a[i][1] == a[i + 1][1]\n pR = a[i + 1][0]\n x = max(x, pR - a[i][0])\n i += 1\nend\n\nx = max(x, n - pR + pL) - 1\nans += x\nputs ans\n"}], "src_uid": "e2db09803d87362c67763ef71e8b7f47"} {"nl": {"description": "Polycarp likes numbers that are divisible by 3.He has a huge number $$$s$$$. Polycarp wants to cut from it the maximum number of numbers that are divisible by $$$3$$$. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after $$$m$$$ such cuts, there will be $$$m+1$$$ parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by $$$3$$$.For example, if the original number is $$$s=3121$$$, then Polycarp can cut it into three parts with two cuts: $$$3|1|21$$$. As a result, he will get two numbers that are divisible by $$$3$$$.Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid.What is the maximum number of numbers divisible by $$$3$$$ that Polycarp can obtain?", "input_spec": "The first line of the input contains a positive integer $$$s$$$. The number of digits of the number $$$s$$$ is between $$$1$$$ and $$$2\\cdot10^5$$$, inclusive. The first (leftmost) digit is not equal to 0.", "output_spec": "Print the maximum number of numbers divisible by $$$3$$$ that Polycarp can get by making vertical cuts in the given number $$$s$$$.", "sample_inputs": ["3121", "6", "1000000000000000000000000000000000", "201920181"], "sample_outputs": ["2", "1", "33", "4"], "notes": "NoteIn the first example, an example set of optimal cuts on the number is 3|1|21.In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by $$$3$$$.In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and $$$33$$$ digits 0. Each of the $$$33$$$ digits 0 forms a number that is divisible by $$$3$$$.In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers $$$0$$$, $$$9$$$, $$$201$$$ and $$$81$$$ are divisible by $$$3$$$."}, "positive_code": [{"source_code": "require 'set'\n\nstr = gets.chomp\n\n# nunmber = -1\n# string = input\n# index = input\ndef fun(number, str, index, length)\n\t\n\tif index >=length\n\t\treturn 0\n\tend\n\n\tif number == -1\n\t\tnumber = str[index].to_i\n\telse\n\t\tnumber *= 10 + str[index].to_i\n\tend\n\n\tif number % 3 == 0\n\t\treturn 1 + fun(-1, str, index + 1, length)\n\telse\n\t\treturn [fun(number, str, index + 1, length), fun(-1, str, index + 1, length)].max\n\tend\nend\n\ndef dp_fun(str, length)\n\ta = str.split('').map(&:to_i)\n\tdp_a = Array.new(length, 0)\n\tindex_i = 0\n\tindex_j = -1\n\twhile index_i < length\n\t\ti = index_i\n\t\tnumber = 0\n\t\t(dp_a[index_i] += dp_a[index_i - 1]) if (index_i -1 >= 0)\n\t\twhile i > index_j\n\t\t\tnumber *= 10\n\t\t\tnumber += a[i]\n\t\t\tif number % 3 == 0\n\t\t\t\tdp_a[index_i] += 1\n\t\t\t\tindex_j = index_i\n\t\t\t\tbreak\n\t\t\tend\n\t\t\ti -= 1\n\t\tend\n\t\tindex_i += 1\n\tend\n\treturn dp_a[length-1]\nend\n\nprint dp_fun(str, str.size)"}, {"source_code": "\ndef max_div_3(digit_string)\n digits = digit_string.chomp.chars.map(&:to_i)\n\n prefix_sums = [0]\n crt = 0\n digits.each do |d|\n crt += d\n prefix_sums << crt\n end\n\n count = 0\n i = 0\n j = 0\n\n while j < digits.size\n start_index = (i..j).find do |k|\n range_sum = prefix_sums[j + 1] - prefix_sums[k]\n range_sum % 3 < 1\n end\n\n if start_index\n count += 1\n j = j + 1\n i = j\n else\n j = j + 1\n end\n end\n\n count\nend\n\ndef solution\n input = gets\n puts(max_div_3(input))\nend\n\nsolution"}, {"source_code": "S = gets.chomp.split('').map{|c| c.to_i % 3}\nA = [true, false, false]\narr = A.dup\nsum = 0\nans = 0\nS.each do |a|\n sum = (sum + a) % 3\n if arr[sum]\n ans += 1\n sum = 0\n arr = A.dup\n else\n arr[sum] = true\n end\nend\nputs ans"}, {"source_code": "ary = gets.strip.split('').map(&:to_i)\nn = ary.size\ndp = Array.new(n+5, 0)\n\ndp[0] = (ary[0]%3==0 ? 1 : 0)\n\n1.upto(n-1).each do |i|\n sum = 0\n dp[i] = dp[i-1]\n i.downto(0).each do |j|\n sum += ary[j]\n if sum%3 ==0\n dp[i] = [dp[i], ((j>0 ? dp[j-1] : 0) + 1)].max\n break\n end\n break if dp[j]= 0 && diff[i - 1] >= 0) or (d <= 0 && diff[i - 1] <= 0)\n results.push(-values[i + 1])\n diff.push -d\n else\n results.push(values[i + 1])\n diff.push d\n end\n else\n results.push(values[i + 1])\n diff.push d\n end\n }\n results.join(' ')\nend\n\ntests = gets.strip.to_i\n\ntests.times{|i|\n inputs = gets.strip.to_i\n values = gets.strip.split(' ').map(&:to_i)\n puts solve(inputs, values)\n}\n\n"}], "src_uid": "d07ae42b7902ba3a49cf4463248710ea"} {"nl": {"description": "Alice and Bob have received three big piles of candies as a gift. Now they want to divide these candies as fair as possible. To do this, Alice takes one pile of candies, then Bob takes one of the other two piles. The last pile is split between Alice and Bob as they want: for example, it is possible that Alice takes the whole pile, and Bob gets nothing from it.After taking the candies from the piles, if Alice has more candies than Bob, she discards some candies so that the number of candies she has is equal to the number of candies Bob has. Of course, Bob does the same if he has more candies.Alice and Bob want to have as many candies as possible, and they plan the process of dividing candies accordingly. Please calculate the maximum number of candies Alice can have after this division process (of course, Bob will have the same number of candies).You have to answer $$$q$$$ independent queries.Let's see the following example: $$$[1, 3, 4]$$$. Then Alice can choose the third pile, Bob can take the second pile, and then the only candy from the first pile goes to Bob — then Alice has $$$4$$$ candies, and Bob has $$$4$$$ candies.Another example is $$$[1, 10, 100]$$$. Then Alice can choose the second pile, Bob can choose the first pile, and candies from the third pile can be divided in such a way that Bob takes $$$54$$$ candies, and Alice takes $$$46$$$ candies. Now Bob has $$$55$$$ candies, and Alice has $$$56$$$ candies, so she has to discard one candy — and after that, she has $$$55$$$ candies too.", "input_spec": "The first line of the input contains one integer $$$q$$$ ($$$1 \\le q \\le 1000$$$) — the number of queries. Then $$$q$$$ queries follow. The only line of the query contains three integers $$$a, b$$$ and $$$c$$$ ($$$1 \\le a, b, c \\le 10^{16}$$$) — the number of candies in the first, second and third piles correspondingly.", "output_spec": "Print $$$q$$$ lines. The $$$i$$$-th line should contain the answer for the $$$i$$$-th query — the maximum number of candies Alice can have after the division, if both Alice and Bob act optimally (of course, Bob will have the same number of candies).", "sample_inputs": ["4\n1 3 4\n1 10 100\n10000000000000000 10000000000000000 10000000000000000\n23 34 45"], "sample_outputs": ["4\n55\n15000000000000000\n51"], "notes": null}, "positive_code": [{"source_code": "q = gets.to_i\nq.times do\n a = gets.split.map(&:to_i)\n p a.inject(:+)/2\nend"}, {"source_code": "#!/usr/bin/env ruby\n\nq = STDIN.gets.to_i\n\ndef calc(x)\n x.split(' ').map(&:to_i).inject(:+) / 2\nend\nq.times { puts calc(STDIN.gets.chomp) }\n"}, {"source_code": "#!/usr/bin/env ruby\n\nq = STDIN.gets.to_i\nxs = q.times.map{STDIN.gets.chomp}\ndef calc(x)\n x.split(' ').map(&:to_i).inject(:+) / 2\nend\n\nres = xs.map do |x|\n calc(x)\nend\n\nputs res\n"}, {"source_code": "q = gets.to_i\nq.times do\n a, b, c = gets.split.map(&:to_i)\n puts (a+b+c)/2\nend"}, {"source_code": "gets.to_i.times do\n a,b,c = gets.split.map(&:to_i).sort\n if a+c < b\n puts a+c\n else\n puts (a+b+c)/2\n end\nend"}, {"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\n\nn = gets.to_i\nn.times {\n q = gets.split.map(&:to_i)\n s = q.reduce(:+)\n\n puts s >> 1\n}"}, {"source_code": "gets.to_i.times do\n a, b, c = gets.split.map(&:to_i)\n puts (a + b + c) / 2\nend"}, {"source_code": "times = gets.chomp\nary = []\nansw = []\n\ntimes.to_i.times do \n ary.push(gets.chomp.split(\" \").map(&:to_i))\nend\n\nary.each do |ele|\n a = ele.delete_at(ele.index(ele.min))\n b = ele.delete_at(ele.index(ele.min))\n max = ele.last\n\n if a == b && b == max\n answ << (a+b+max)/2\n next\n end\n \n tmp = b-a\n \n if tmp > 0\n t_shr = max - tmp\n if t_shr%2 == 0\n shr = t_shr/2\n else\n shr = (t_shr-1)/2\n end \n answ << shr+tmp+[a,b].min\n elsif tmp == 0\n if max%2 == 0\n a = a + (max/2)\n else\n a = a + ((max-1)/2)\n end\n answ << a \n end\nend\n\nansw.each do |ele|\n puts \"#{ele}\\n\"\nend \n"}, {"source_code": "MOD=10**9+7\ncnt=0; sum=0; prev=nil; can=true; h=Hash.new(0)\ndef gs() gets.chomp end\ndef gi() gets.to_i end\ndef gsmi() gets.chomp.split.map(&:to_i) end\n\ndef desc(ar) ar.sort!{|x,y|y<=>x} end\ndef min(a,b) a<=b ? a : b end\ndef max(a,b) a>=b ? a : b end\ndef sum(ar) ar.inject(:+) end\n#def bs(ar,eq,n) en=(eq==1 ? :>= : :>); ar.bsearch_index{|x|x.send(en,n)} end\n\ndef puts_yesno(can) puts(can ? 'Yes' : 'No') end\ndef putsend(s) puts s; exit end\ndef debug(k,v) puts \"#{k}:#{v}\" end\n########### ( ˘ω˘ )スヤァ… ###########\nq = gi\nans = []\nq.times do\n a, b, c = gsmi\n ans << (a+b+c)/2\nend\nputs ans\n"}, {"source_code": "1.upto(gets.to_i) do\n puts gets.split(' ').map(&:to_i).inject(&:+).div(2)\nend\n"}, {"source_code": "t=gets.to_i\n(1).upto(t) do |i|\n x = gets.split(' ').map(&:to_i)\n x[0]+=x[1]\n x[0]+=x[2]\n puts x[0]>>1\nend\n\n"}], "negative_code": [], "src_uid": "d9e4a9a32d60e75f3cf959ef7f894fc6"} {"nl": {"description": "Shubham has a binary string $$$s$$$. A binary string is a string containing only characters \"0\" and \"1\".He can perform the following operation on the string any amount of times: Select an index of the string, and flip the character at that index. This means, if the character was \"0\", it becomes \"1\", and vice versa. A string is called good if it does not contain \"010\" or \"101\" as a subsequence  — for instance, \"1001\" contains \"101\" as a subsequence, hence it is not a good string, while \"1000\" doesn't contain neither \"010\" nor \"101\" as subsequences, so it is a good string.What is the minimum number of operations he will have to perform, so that the string becomes good? It can be shown that with these operations we can make any string good.A string $$$a$$$ is a subsequence of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) characters.", "input_spec": "The first line of the input contains a single integer $$$t$$$ $$$(1\\le t \\le 100)$$$ — the number of test cases. Each of the next $$$t$$$ lines contains a binary string $$$s$$$ $$$(1 \\le |s| \\le 1000)$$$.", "output_spec": "For every string, output the minimum number of operations required to make it good.", "sample_inputs": ["7\n001\n100\n101\n010\n0\n1\n001100"], "sample_outputs": ["0\n0\n1\n1\n0\n0\n2"], "notes": "NoteIn test cases $$$1$$$, $$$2$$$, $$$5$$$, $$$6$$$ no operations are required since they are already good strings.For the $$$3$$$rd test case: \"001\" can be achieved by flipping the first character  — and is one of the possible ways to get a good string.For the $$$4$$$th test case: \"000\" can be achieved by flipping the second character  — and is one of the possible ways to get a good string.For the $$$7$$$th test case: \"000000\" can be achieved by flipping the third and fourth characters  — and is one of the possible ways to get a good string."}, "positive_code": [{"source_code": "def solve(num)\n one_count = 0\n zero_count = 0\n ones_first = []\n zeroes_first = []\n\n num.each_char.with_index do |char, i|\n one_count += 1 if char == \"1\"\n zero_count += 1 if char == \"0\"\n ones_first[i] = zero_count\n zeroes_first[i] = one_count\n end\n\n i = num.length - 1 \n ones_last = []\n ones_last[i] = 0 \n zeroes_last = []\n zeroes_last[i] = 0 \n min = Float::INFINITY\n\n while i > 0 \n zeroes_last[i - 1] = zeroes_last[i] + zeroes_first[i] - zeroes_first[i-1]\n ones_last[i - 1] = ones_last[i] + ones_first[i] - ones_first[i-1] \n min = [min, zeroes_first[i] + ones_last[i], ones_first[i] + zeroes_last[i]].min\n i -= 1\n end\n\n [min, zeroes_first[0] + ones_last[0], ones_first[0] + zeroes_last[0]].min\nend\n\n\namount = gets.chomp.to_i\namount.times do\n input = gets.chomp\n puts solve(input)\nend\n"}, {"source_code": "def solve(num)\n count = 0\n zeroes_first = num.each_char.map do |char|\n count += 1 if char == \"1\"\n count\n end\n count = 0\n i = num.length - 1 \n ones_last = []\n ones_last[i] = 0 \n \n while i > 0 \n count += 1 if num[i] == \"0\"\n ones_last[i - 1] = count\n i -= 1\n end\n count = 0\n ones_first = num.each_char.map do |char|\n count += 1 if char == \"0\"\n count\n end\n count = 0\n i = num.length - 1 \n zeroes_last = []\n zeroes_last[i] = 0\n while i > 0 \n count += 1 if num[i] == \"1\"\n zeroes_last[i - 1] = count\n i -= 1\n end\n\n i = 0 \n min = Float::INFINITY\n while i < num.length\n min = [min, zeroes_first[i] + ones_last[i], ones_first[i] + zeroes_last[i]].min\n i += 1\n end\n min\nend\n\n\namount = gets.chomp.to_i\namount.times do\n input = gets.chomp\n puts solve(input)\nend\n"}, {"source_code": "# coding: utf-8\n\nn = gets.to_i\nn.times {\n s = gets.chomp.split(//).map(&:to_i)\n total = s.length\n ones = s.count(1)\n zeroes = s.count(0)\n # 0->1과, 1->0으로 모두 뒤집는 경우에 대한 기본해(이게 최소일 수도 있음)\n min = [ones, zeroes].min \n flip_ones, flip_zeroes = 0, 0\n \n for i in s\n if i == 0\n flip_zeroes += 1\n zeroes -= 1\n else\n flip_ones += 1\n ones -= 1\n end\n # 자기 자리를 계속 바꿔가며 나아간다\n # 0으로 나아갈 경우-> 뒷부분을 전부 1로 만들기 위해 바꿔야할 0의 수\n # 1로 나아갈 경우 -> 뒷부분을 전부 0으로 만들기 위해 바꿔야할 1의 수\n # 항상 미니멈을 유지하자\n # dp 응용 같음\n min = [min, flip_zeroes + ones, flip_ones + zeroes].min\n end\n puts min\n}"}, {"source_code": "gets.to_i.times do \n s = gets.chomp\n ones = s.each_char.inject([]) do |arr, c| \n arr.push arr[-1] || 0\n arr[-1] += 1 if c == '1'\n arr\n end\n all = [ones[-1], s.length - ones[-1]].min\n min = s.length.times.inject(all) do |min, i|\n right_zero = s.length - (i + 1) - (ones[-1] - ones[i])\n left_zero = i + 1 - ones[i]\n right_one = ones[-1] - ones[i]\n min = [min, ones[i] + right_zero, left_zero + right_one].min\n end\n puts min\nend"}], "negative_code": [{"source_code": "# coding: utf-8\n\nn = gets.to_i\nn.times {\n s = gets.chomp.split(//).map(&:to_i)\n i = 0\n len = s.length\n flipped = 0\n # 010 -> remove 1's\n # 101 -> removes 0's\n while i < len-1\n base = 0\n flip = 1\n if s[i] == 0\n base = 0\n flip = 1\n else\n base = 1\n flip = 0\n end\n\n p1 = s[i..-1].index(flip)\n if p1 != nil\n p1 += i #compensation\n p2 = s[p1..-1].index(base)\n if p2 != nil\n p2 += p1 # compensation\n j = p1\n while s[j] == flip\n s[j] = base\n j += 1\n flipped += 1\n end\n i = p2\n next\n end\n end\n\n i += 1\n end\n puts flipped\n}"}, {"source_code": "# coding: utf-8\n\nn = gets.to_i\nn.times {\n s = gets.split(//).map(&:to_i)\n i = 0\n len = s.length\n flipped = 0\n # 010 -> remove 1's\n # 101 -> removes 0's\n while i < len-1\n base = 0\n flip = 1\n if s[i] == 0\n base = 0\n flip = 1\n else\n base = 1\n flip = 0\n end\n\n p1 = s[i..-1].index(flip)\n if p1 != nil\n p1 += i #compensation\n p2 = s[p1..-1].index(base)\n if p2 != nil\n p2 += p1 # compensation\n j = p1\n while s[j] == flip\n s[j] = base\n j += 1\n flipped += 1\n end\n i = p2\n next\n end\n end\n\n i += 1\n end\n puts flipped\n}"}], "src_uid": "803e5ccae683b91a4cc486fbc558b330"} {"nl": {"description": "When little Petya grew up and entered the university, he started to take part in АСМ contests. Later he realized that he doesn't like how the АСМ contests are organised: the team could only have three members (and he couldn't take all his friends to the competitions and distribute the tasks between the team members efficiently), so he decided to organize his own contests PFAST Inc. — Petr and Friends Are Solving Tasks Corporation. PFAST Inc. rules allow a team to have unlimited number of members.To make this format of contests popular he organised his own tournament. To create the team he will prepare for the contest organised by the PFAST Inc. rules, he chose several volunteers (up to 16 people) and decided to compile a team from them. Petya understands perfectly that if a team has two people that don't get on well, then the team will perform poorly. Put together a team with as many players as possible given that all players should get on well with each other.", "input_spec": "The first line contains two integer numbers n (1 ≤ n ≤ 16) — the number of volunteers, and m () — the number of pairs that do not get on. Next n lines contain the volunteers' names (each name is a non-empty string consisting of no more than 10 uppercase and/or lowercase Latin letters). Next m lines contain two names — the names of the volunteers who do not get on. The names in pair are separated with a single space. Each pair of volunteers who do not get on occurs exactly once. The strings are case-sensitive. All n names are distinct.", "output_spec": "The first output line should contain the single number k — the number of people in the sought team. Next k lines should contain the names of the sought team's participants in the lexicographical order. If there are several variants to solve the problem, print any of them. Petya might not be a member of the sought team. ", "sample_inputs": ["3 1\nPetya\nVasya\nMasha\nPetya Vasya", "3 0\nPasha\nLesha\nVanya"], "sample_outputs": ["2\nMasha\nPetya", "3\nLesha\nPasha\nVanya"], "notes": null}, "positive_code": [{"source_code": "\nn, m = gets.split.map &:to_i\np = []\n$e = Array.new(n,0)\n\nn.times {\n name = gets.chomp\n p << name\n}\nm.times {\n p0, p1 = gets.chomp.split\n i = p.index p0\n j = p.index p1\n $e[i] = $e[i] + (1<< j)\n $e[j] = $e[j] + (1<< i)\n}\n\n$max = []\ndef f(mems, mask, i, n)\n $max = mems.dup if mems.length > $max.length\n i.upto(n-1) {|j|\n if ($e[j] & mask) == 0\n mems << j\n f(mems, mask + (1<< j), j+1, n)\n mems.pop\n end\n }\nend\nf([], 0, 0, n)\nputs $max.length\n$max.map {|i| p[i] }.sort.each { |p| puts p}\n"}, {"source_code": "def ones(a)\n\tc = 0\n\twhile a > 0\n\t\tc += a % 2\n\t\ta /= 2\t\t\n\tend\n\tc\nend\n\nn,m = gets.split(\" \").map(&:to_i)\narr = []\nn.times do\n\tarr << gets.chomp\nend\nenemy = []\n\nm.times do\n\ta,b = gets.chomp.split(\" \")\n\tval = 0\n\tval |= 1 << arr.index(a)\n\tval |= 1 << arr.index(b)\n\tenemy << val\nend\n\n# puts \"here #{arr}\"\nbest = 0\n\n(0..(2**n)).each do |i|\n\tans = true\n\tenemy.each do |en|\n\t\tif (en & i) == en\n\t\t\tans = false\n\t\t\t# puts \"ere\"\n\t\tend\n\t\t# puts \"fdf\"\n\tend\n\t# puts \"ere\"\n\tif ans && (ones(i) > ones(best))\n\t\tbest = i\t\t\n\tend\t\n\t# puts \"dsafds\"\nend\n# puts \"dasf\"\nputs ones(best)\nlist = []\n# puts \"#{arr}\"\n# puts \"#{best.to_s(2).rjust(n,'0')}\"\nbest.to_s(2).rjust(n,'0').each_char.with_index do |c,i|\n\tif c == '1'\n\t\tlist << arr[n-i - 1]\n\tend\nend\nputs list.sort.join(\"\\n\")"}, {"source_code": "#!/usr/bin/env ruby\n\ndef main\n n, m = STDIN.gets.split.map!{|x| x.to_i}\n names = []\n n.times {\n name = STDIN.gets.rstrip\n names.push(name)\n }\n\n ng = (0...n).map {Array.new(n)}\n m.times {\n a, b = STDIN.gets.split\n a = names.find_index(a)\n b = names.find_index(b)\n ng[a][b] = ng[b][a] = true\n }\n\n ans = 0\n (1 << n).times {|set|\n if ans.to_s(2).count('1') >= set.to_s(2).count('1')\n next\n end\n\n ok = true\n n.times do |i| if (set >> i & 1) != 0\n i.times do |j| if (set >> j & 1) != 0\n if ng[i][j]\n ok = false\n end\n end\n end\n end\n end\n if ok\n ans = set\n end\n }\n\n puts ans.to_s(2).count('1')\n ans_names = []\n n.times {|i|\n if (ans >> i & 1) == 1\n ans_names.push(names[i])\n end\n }\n ans_names.sort!.each do |x|\n puts x\n end\n\nend\n\nif __FILE__ == $0\n main\nend"}], "negative_code": [{"source_code": "\nn, m = gets.split.map &:to_i\np = []\n$e = []\nn.times { $e << Array.new(n, false) }\n\nn.times {\n name = gets.chomp\n p << name\n}\nm.times {\n p0, p1 = gets.split\n i = p.index p0\n j = p.index p1\n $e[i][j] = $e[j][i] = true\n}\n\n$max = []\ndef f(mems, i, n)\n if i == n\n $max = mems.dup if mems.length > $max.length\n else\n i.upto(n-1) {|j|\n if not mems.any? { |k| $e[j][k] }\n mems << j\n f(mems, j+1, n)\n mems.pop\n end\n }\n end\nend\nf([], 0, n)\nputs $max.length\nputs $max.map { |i| p[i]}.sort\n"}, {"source_code": "\nn, m = gets.split.map &:to_i\np = []\n$e = Array.new(n,0)\n\nn.times {\n name = gets.chomp\n p << name\n}\nm.times {\n p0, p1 = gets.chomp.split\n i = p.index p0\n j = p.index p1\n $e[i] = $e[i] + (1<< j)\n $e[j] = $e[j] + (1<< i)\n}\n\n$max = []\ndef f(mems, mask, i, n)\n if i == n\n $max = mems.dup if mems.length > $max.length\n else\n i.upto(n-1) {|j|\n if ($e[j] & mask) == 0\n mems << j\n f(mems, mask + (1<< j), j+1, n)\n mems.pop\n end\n }\n end\nend\nf([], 0, 0, n)\nputs $max.length\n$max.map { |i| p[i]}.sort.each { |p| puts p}\n"}, {"source_code": "\nn, m = gets.split.map &:to_i\np = []\n$e = Array.new(n,0)\n\nn.times {\n name = gets.chomp\n p << name\n}\nm.times {\n p0, p1 = gets.chomp.split\n i = p.index p0\n j = p.index p1\n $e[i] = $e[i] + (1<< j)\n $e[j] = $e[j] + (1<< i)\n}\n\n$max = []\ndef f(mems, mask, i, n)\n if i == n\n $max = mems.dup if mems.length > $max.length\n else\n i.upto(n-1) {|j|\n if ($e[j] & mask) == 0\n mems << j\n f(mems, mask + (1<< j), j+1, n)\n mems.pop\n end\n }\n end\nend\nf([], 0, 0, n)\nputs $max.length\nputs $max.map { |i| p[i]}.sort\n"}, {"source_code": "\nn, m = gets.split.map &:to_i\np = []\n$e = Array.new(n,0)\n\nn.times {\n name = gets.chomp\n p << name\n}\nm.times {\n p0, p1 = gets.chomp.split\n i = p.index p0\n j = p.index p1\n $e[i] = $e[i] + (1<< j)\n $e[j] = $e[j] + (1<< i)\n}\n\n$max = []\ndef f(mems, mask, i, n)\n if i == n\n $max = mems.dup if mems.length > $max.length\n else\n i.upto(n-1) {|j|\n if ($e[j] & mask) == 0\n mems << j\n f(mems, mask + (1<< j), j+1, n)\n mems.pop\n end\n }\n end\nend\nf([], 0, 0, n)\nputs $max.length\n$max.map {|i| p[i] }.sort_by(&:downcase).each { |p| puts p}\n"}, {"source_code": "n,m = gets.split(\" \").map(&:to_i)\narr = []\nn.times do\n\tarr << gets.chomp\nend\nenemy = {}\n\nm.times do\n\ta,b = gets.chomp.split(\" \")\n\tenemy[a] ||= []\n\tenemy[b] ||= []\n\tenemy[a] << b\n\tenemy[b] << a\nend\n# ans = arr.dup\n# puts \"#{enemy}\"\n# puts \"here #{enemy.key(:Pasha)}\"\nenemy_vals = enemy.sort_by{|x,v| -v.length}\n# puts \"#{}\"\nenemy_vals.each do |v|\n\tk = v[0]\n\tval = enemy[k]\n\t# puts \"arr #{arr}\"\n\tif val.length > 0\n\t\t# puts \"#{enemy}\"\n\t\tarr -= [k]\n\t\tval.each{|x| enemy[x] -= [k] }\n\tend\nend\nputs arr.length\nputs \"#{arr.sort.join(\"\\n\")}\""}, {"source_code": "n,m = gets.split(\" \").map(&:to_i)\narr = []\nn.times do\n\tarr << gets.chomp\nend\nenemy = {}\n\nm.times do\n\ta,b = gets.chomp.split(\" \")\n\tenemy[a] ||= []\n\tenemy[b] ||= []\n\tenemy[a] << b\n\tenemy[b] << a\nend\n# ans = arr.dup\nenemy.each do |k,v|\n\tif v.length > 0\n\t\tarr -= [k]\n\t\tv.each{|x| enemy[x] -= [k] }\n\tend\nend\nputs arr.length\nputs \"#{arr.sort.join(\"\\n\")}\""}, {"source_code": "def ones(a)\n\tc = 0\n\twhile a > 0\n\t\tc += a % 2\n\t\ta /= 2\t\t\n\tend\n\tc\nend\n\nn,m = gets.split(\" \").map(&:to_i)\narr = []\nn.times do\n\tarr << gets.chomp\nend\nenemy = []\n\nm.times do\n\ta,b = gets.chomp.split(\" \")\n\tval = 0\n\tval |= 1 << arr.index(a)\n\tval |= 1 << arr.index(b)\n\tenemy << val\nend\n\n# puts \"here #{arr}\"\nbest = 0\n\n(0..(2**n)).each do |i|\n\tans = true\n\tenemy.each do |en|\n\t\tif (en & i) == en\n\t\t\tans = false\n\t\t\t# puts \"ere\"\n\t\tend\n\t\t# puts \"fdf\"\n\tend\n\t# puts \"ere\"\n\tif ans && (ones(i) > ones(best))\n\t\tbest = i\t\t\n\tend\t\n\t# puts \"dsafds\"\nend\n# puts \"dasf\"\nputs ones(best)\nlist = []\n# puts \"#{best}\"\nbest.to_s(2).each_char.with_index do |c,i|\n\tif c == '1'\n\t\tlist << arr[i]\n\tend\nend\nputs list.sort.join(\"\\n\")"}, {"source_code": "def ones(a)\n\tc = 0\n\twhile a > 0\n\t\tc += a % 2\n\t\ta /= 2\t\t\n\tend\n\tc\nend\n\nn,m = gets.split(\" \").map(&:to_i)\narr = []\nn.times do\n\tarr << gets.chomp\nend\nenemy = []\n\nm.times do\n\ta,b = gets.chomp.split(\" \")\n\tval = 0\n\tval |= 1 << arr.index(a)\n\tval |= 1 << arr.index(b)\n\tenemy << val\nend\n\n# puts \"here #{arr}\"\nbest = 0\n\n(0..(2**n)).each do |i|\n\tans = true\n\tenemy.each do |en|\n\t\tif (en & i) == en\n\t\t\tans = false\n\t\t\t# puts \"ere\"\n\t\tend\n\t\t# puts \"fdf\"\n\tend\n\t# puts \"ere\"\n\tif ans && (ones(i) > ones(best))\n\t\tbest = i\t\t\n\tend\t\n\t# puts \"dsafds\"\nend\n# puts \"dasf\"\nputs ones(best)\nlist = []\n# puts \"#{best}\"\nbest.to_s(2).each_char.with_index do |c,i|\n\tif c == '1'\n\t\tlist << arr[n-i - 1]\n\tend\nend\nputs list.sort.join(\"\\n\")"}, {"source_code": "#!/usr/bin/env ruby\n\ndef main\n n, m = STDIN.gets.split.map!{|x| x.to_i}\n names = []\n n.times {\n name = STDIN.gets.rstrip\n names.push(name)\n }\n\n ng = (0...n).map {Array.new(n)}\n m.times {\n a, b = STDIN.gets.split\n a = names.find_index(a)\n b = names.find_index(b)\n ng[a][b] = ng[b][a] = true\n }\n\n ans = 0\n (1 << n).times {|set|\n if ans.to_s(2).count('1') >= set.to_s(2).count('1')\n next\n end\n\n ok = true\n n.times do |i|\n i.times do |j|\n if (set >> i & 1) != 0 && (set >> j & 1) != 0\n if ng[i][j]\n ok = false\n end\n end\n end\n end\n if ok\n ans = set\n end\n }\n puts ans.to_s(2).count('1')\n n.times {|i|\n if (ans >> i & 1) == 1\n puts names[i]\n end\n }\n\n\nend\n\nif __FILE__ == $0\n main\n STDIN.gets;\nend"}], "src_uid": "b0301a2d79a1ec126511ed769ec0b743"} {"nl": {"description": "Utkarsh is forced to play yet another one of Ashish's games. The game progresses turn by turn and as usual, Ashish moves first.Consider the 2D plane. There is a token which is initially at $$$(0,0)$$$. In one move a player must increase either the $$$x$$$ coordinate or the $$$y$$$ coordinate of the token by exactly $$$k$$$. In doing so, the player must ensure that the token stays within a (Euclidean) distance $$$d$$$ from $$$(0,0)$$$.In other words, if after a move the coordinates of the token are $$$(p,q)$$$, then $$$p^2 + q^2 \\leq d^2$$$ must hold.The game ends when a player is unable to make a move. It can be shown that the game will end in a finite number of moves. If both players play optimally, determine who will win.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 100$$$) — the number of test cases. The only line of each test case contains two space separated integers $$$d$$$ ($$$1 \\leq d \\leq 10^5$$$) and $$$k$$$ ($$$1 \\leq k \\leq d$$$).", "output_spec": "For each test case, if Ashish wins the game, print \"Ashish\", otherwise print \"Utkarsh\" (without the quotes).", "sample_inputs": ["5\n2 1\n5 2\n10 3\n25 4\n15441 33"], "sample_outputs": ["Utkarsh\nAshish\nUtkarsh\nUtkarsh\nAshish"], "notes": "NoteIn the first test case, one possible sequence of moves can be$$$(0, 0) \\xrightarrow{\\text{Ashish }} (0, 1) \\xrightarrow{\\text{Utkarsh }} (0, 2)$$$.Ashish has no moves left, so Utkarsh wins."}, "positive_code": [{"source_code": "gets.to_i.times{\n d,k = gets.split.map &:to_i\n x = ((d.to_f**2 / (2 * k**2))**0.5).ceil\n puts k**2 * (2 * x**2 - 2*x + 1) <= d**2 ? \"Ashish\" : \"Utkarsh\"\n}\n"}], "negative_code": [], "src_uid": "d4d41e75c68ce5e94c92e1b9876891bf"} {"nl": {"description": "One fine October day a mathematics teacher Vasily Petrov went to a class and saw there n pupils who sat at the desks, two people at each desk. Vasily quickly realized that number n is even. Like all true mathematicians, Vasily has all students numbered from 1 to n.But Vasily Petrov did not like the way the children were seated at the desks. According to him, the students whose numbers differ by 1, can not sit together, as they talk to each other all the time, distract others and misbehave.On the other hand, if a righthanded student sits at the left end of the desk and a lefthanded student sits at the right end of the desk, they hit elbows all the time and distract each other. In other cases, the students who sit at the same desk, do not interfere with each other.Vasily knows very well which students are lefthanders and which ones are righthanders, and he asks you to come up with any order that meets these two uncomplicated conditions (students do not talk to each other and do not bump their elbows). It is guaranteed that the input is such that at least one way to seat the students always exists.", "input_spec": "The first input line contains a single even integer n (4 ≤ n ≤ 100) — the number of students in the class. The second line contains exactly n capital English letters \"L\" and \"R\". If the i-th letter at the second line equals \"L\", then the student number i is a lefthander, otherwise he is a righthander.", "output_spec": "Print integer pairs, one pair per line. In the i-th line print the numbers of students that will sit at the i-th desk. The first number in the pair stands for the student who is sitting to the left, and the second number stands for the student who is sitting to the right. Separate the numbers in the pairs by spaces. If there are multiple solutions, print any of them.", "sample_inputs": ["6\nLLRLLL", "4\nRRLL"], "sample_outputs": ["1 4\n2 5\n6 3", "3 1\n4 2"], "notes": null}, "positive_code": [{"source_code": "$stdin = File.new( \"input.txt\", 'r' )\n$stdout = File.new( \"output.txt\", 'w' )\n\nn = gets.strip.to_i\nstds = gets.strip.split(\"\")\nmid = n / 2\ni = 0\n(mid...n).each do |j|\n if stds[i] == 'R' && stds[j] == 'L'\n puts \"#{j + 1} #{i + 1}\"\n else\n puts \"#{i + 1} #{j + 1}\"\n end\n i += 1\nend\n"}, {"source_code": "n = 0\narr = []\nopen(\"input.txt\") {|f|\n n = f.gets.to_i\n arr = f.gets.to_s.chomp.split(//)\n}\nFile.open(\"output.txt\", \"w\"){|f|\n(n/2).times do |i|\n if arr[i] == \"R\" && arr[i+n/2] == \"L\"\n f.write \"#{i+n/2+1} #{i+1}\\n\"\n else\n f.write \"#{i+1} #{i+n/2+1}\\n\"\n end\nend\n}"}, {"source_code": "$stdin=open(\"input.txt\")\n$stdout=open(\"output.txt\",\"w\")\nn=gets.to_i/2\na=gets\ni=0\nn.times do\n k=a[i]=='L'?0:1\n puts [i+1+n*k, i+1+n*(1-k)]*' '\n i+=1\nend"}, {"source_code": "$stdin=open(\"input.txt\")\n$stdout=open(\"output.txt\",\"w\")\nn=gets.to_i/2\na=gets\ni=0\nn.times do\n if a[i]=='L' then\n puts [i+1,i+1+n]*' '\n else\n puts [i+1+n,i+1]*' '\n end\n i+=1\nend\n"}, {"source_code": "$stdin=open(\"input.txt\")\n$stdout=open(\"output.txt\",\"w\")\nn=gets.to_i/2\na=gets\ni=0\nn.times do\n k=a[i]=='L'?0:1\n puts [i+1+n*k, i+1+n*(1-k)]*' '\n i+=1\nend\n"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\nn=gets.to_i\ns=gets\n(n/2).times{|i|\n\tx,y=i,i+n/2\n\tx,y=y,x if s[x]=='R'&&s[y]=='L'\n\tputs [x+1,y+1]*' '\n}"}, {"source_code": "$stdin=open(\"input.txt\")\n$stdout=open(\"output.txt\",\"w\")\nn=gets.to_i/2\na=gets\ni=0\nn.times do\n k=a[i]=='L'?0:1\n puts [i+1+n*k, i+1+n*(1-k)]*' '\n i+=1\nend\n\n"}, {"source_code": "line = File.read(\"input.txt\")\nn,hand = line.split(\"\\n\")\n\n\nn = n.to_i\nres = []\n\n\narr = hand.split(\"\")\n\nnos = [*(0..n-1).step(2),*(1..n-1).step(2)]\n\nnos.each_slice(2) do |a|\n\to = arr[a[0]]\n\te = arr[a[1]]\n\todd_ind = a[0]\n\teven_ind = a[1]\n\todd_ind,even_ind = even_ind,odd_ind if o != e && o != \"R\"\n\tres << [even_ind+1,odd_ind+1]\nend\nFile.open(\"output.txt\", 'w') { |file| \n\tres.each do |a|\n\t\tfile.puts(a.join(\" \"))\n\tend\n}\n"}, {"source_code": "$stdin=open(\"input.txt\")\n$stdout=open(\"output.txt\",\"w\")\nn=gets.to_i/2\na=gets\ni=0\nn.times do\n k=a[i]=='L'?0:1\n puts [i+1+n*k, i+1+n*(1-k)]*' '\n i+=1\nend\n\n"}, {"source_code": "$stdin=open(\"input.txt\")\n$stdout=open(\"output.txt\",\"w\")\nn=gets.to_i/2\na=gets\ni=0\nn.times do\n k=a[i]=='L'?0:1\n puts [i+1+n*k, i+1+n*(1-k)]*' '\n i+=1\nend\n\n"}, {"source_code": "$stdin=open(\"input.txt\")\n$stdout=open(\"output.txt\",\"w\")\nn=gets.to_i/2\na=gets\ni=0\nn.times do\n k=a[i]=='L'?0:1\n puts [i+1+n*k, i+1+n*(1-k)]*' '\n i+=1\nend\n\n"}, {"source_code": "if ENV['USER'] != 'watashi'\n ARGV << 'input.txt'\n $> = open('output.txt', 'w')\nend\n\nn = gets.to_i / 2\nm = gets\nputs [*1..n].map{|i| (m[i-1] == 'L' ? [i, n+i] : [n+i, i]) * ' '} * \"\\n\"\n"}, {"source_code": "if ENV['USER'] != 'watashi'\n $stdin = open('input.txt')\n $> = open('output.txt', 'w')\nend\n\nn = gets.to_i / 2\nm = gets\nputs [*1..n].map{|i| (m[i-1] == 'L' ? [i, n+i] : [n+i, i]) * ' '} * \"\\n\"\n"}, {"source_code": "$stdin=open(\"input.txt\")\n$stdout=open(\"output.txt\",\"w\")\nn=gets.to_i/2\na=gets\ni=0\nn.times do\n k=a[i]=='L'?0:1\n puts [i+1+n*k, i+1+n*(1-k)]*' '\n i+=1\nend\n\n"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt', 'w')\nb=Array.new{|el| el=Array.new(2)}\nn,s=gets.to_i,\" \"+gets.to_s\na=(1..n).to_a\nx,y=1,3\nfor i in 1..n/2 do\nb<<[x,y] \nif i%2==0 then\n\tx+=3\n\ty+=3\n\telse\n\t\tx+=1\n\t\ty+=1\n\tend\nend\nif n%4!=0 then\nb[n%2-1][0],b[n%2-2][1]=b[n%2-2][1],b[n%2-1][0]\nb[n%2-1][1]=n\nend\nfor i in 1..n/2 do\nif s[b[i-1][0]] ==\"R\" && s[b[i-1][1]] ==\"L\" then\nb[i-1][0],b[i-1][1]=b[i-1][1],b[i-1][0]\nend\nputs b[i-1].join(' ')\nend\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\n\n\n\n\n\t\n\t\n\t\n"}, {"source_code": "input = File.open(\"input.txt\", \"r\")\noutput = File.open(\"output.txt\", \"w\")\n\nn = input.gets.to_i\nlr = \" \"+input.gets.strip\n\nfor i in 1..n-2\n desk = Array.new(2)\n if lr[i] != \"0\"\n if lr[i] == \"L\"\n\t desk[0] = i\n\t desk[1] = n/2+i\n\telse\n\t desk[0] = n/2+i\n\t desk[1] = i\n\tend\n\tlr[i] = \"0\"\n\tlr[n/2+i] = \"0\"\n\toutput.puts \"#{desk[0]} #{desk[1]}\"\n end\nend\n\ninput.close\noutput.close"}, {"source_code": "input = File.open(\"input.txt\", \"r\")\noutput = File.open(\"output.txt\", \"w\")\n\nn = input.gets.to_i\nlr = \" \"+input.gets.strip\n\nfor i in 1..n/2\n desk = Array.new(2)\n if lr[i] == \"L\"\n\tdesk[0] = i\n\tdesk[1] = n/2+i\n else\n\tdesk[0] = n/2+i\n\tdesk[1] = i\n end\n output.puts \"#{desk[0]} #{desk[1]}\"\nend\n\ninput.close\noutput.close"}, {"source_code": "$stdin=open(\"input.txt\")\n$stdout=open(\"output.txt\",\"w\")\nn=gets.to_i/2\na=gets\ni=0\nn.times do\n k=a[i]=='L'?0:1\n puts [i+1+n*k, i+1+n*(1-k)]*' '\n i+=1\nend\n\n"}, {"source_code": "$stdin=open(\"input.txt\")\n$stdout=open(\"output.txt\",\"w\")\nn=gets.to_i/2\na=gets\ni=0\nn.times do\n k=a[i]=='L'?0:1\n puts [i+1+n*k, i+1+n*(1-k)]*' '\n i+=1\nend\n"}, {"source_code": "$stdin=open(\"input.txt\")\n$stdout=open(\"output.txt\",\"w\")\nn=gets.to_i\na=gets.chomp.split('').unshift(0)\nfor i in 1..n/2\n\tputs \"#{i} #{i+n/2}\" if a[i]=='L'\n\tputs \"#{i+n/2} #{i}\" if a[i]=='R'\nend"}, {"source_code": "$stdin=open(\"input.txt\")\n$stdout=open(\"output.txt\",\"w\")\nn=gets.to_i/2\na=gets\ni=0\nn.times do\n k=a[i]=='L'?0:1\n puts [i+1+n*k, i+1+n*(1-k)]*' '\n i+=1\nend\n\n"}, {"source_code": "$stdin=open(\"input.txt\")\n$stdout=open(\"output.txt\",\"w\")\nn=gets.to_i/2\na=gets\ni=0\nn.times do\n k=a[i]=='L'?0:1\n puts [i+1+n*k, i+1+n*(1-k)]*' '\n i+=1\nend\n\n"}], "negative_code": [{"source_code": "n = gets.to_i\narr = gets.to_s.chomp.split(//)\n(n/2).times do |i|\n if arr[i] == \"R\" && arr[i+n/2] == \"L\"\n puts \"#{i+n/2+1} #{i+1}\"\n else\n puts \"#{i+1} #{i+n/2+1}\"\n end\nend\n"}, {"source_code": "n=0\na=[]\nopen(\"input.txt\") do |f|\n n=f.gets.to_i\n a=f.gets.chomp.split('')\nend\n\nres=Array.new(n/2).map!{Array.new(2)}\nl=r=0\na.each_with_index do |v,i|\n if v=='L' then\n if l = open('output.txt', 'w')\nend\n\nn = gets.to_i\nm = gets.scan(/[LR]/).zip([*1..n]).sort\nn /= 2\nn.times do |i|\n puts \"#{m[i][1]} #{m[n + i][1]}\"\nend\n"}, {"source_code": "p ENV\nvar = %w(RUBY_COPYRIGHT RUBY_ENGINE RUBY_PLATFORM RUBY_REVISION RUBY_DESCRIPTION RUBY_PATCHLEVEL RUBY_RELEASE_DATE RUBY_VERSION)\nvar.map{|_| puts \"#{_} = #{eval(_)}\"}\n"}], "src_uid": "564664d9cd2ccbccb42574b3881ec5fe"} {"nl": {"description": "You work in the quality control department of technical support for a large company. Your job is to make sure all client issues have been resolved.Today you need to check a copy of a dialog between a client and a technical support manager. According to the rules of work, each message of the client must be followed by one or several messages, which are the answer of a support manager. However, sometimes clients ask questions so quickly that some of the manager's answers to old questions appear after the client has asked some new questions.Due to the privacy policy, the full text of messages is not available to you, only the order of messages is visible, as well as the type of each message: a customer question or a response from the technical support manager. It is guaranteed that the dialog begins with the question of the client.You have to determine, if this dialog may correspond to the rules of work described above, or the rules are certainly breached.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 500$$$). Description of the test cases follows. The first line of each test case contains one integer $$$n$$$ ($$$1 \\le n \\le 100$$$) — the total number of messages in the dialog. The second line of each test case consists of $$$n$$$ characters \"Q\" and \"A\", describing types of messages in the dialog in chronological order. Character \"Q\" denotes the message with client question, and character \"A\" — the message with technical support manager answer. It is guaranteed that the first character in the line equals to \"Q\".", "output_spec": "For each test case print \"Yes\" (without quotes) if dialog may correspond to the rules of work, or \"No\" (without quotes) otherwise.", "sample_inputs": ["5\n\n4\n\nQQAA\n\n4\n\nQQAQ\n\n3\n\nQAA\n\n1\n\nQ\n\n14\n\nQAQQAQAAQQQAAA"], "sample_outputs": ["Yes\nNo\nYes\nNo\nYes"], "notes": "NoteIn the first test case the two questions from the client are followed with two specialist's answers. So this dialog may correspond to the rules of work.In the second test case one of the first two questions was not answered.In the third test case the technical support manager sent two messaged as the answer to the only message of the client."}, "positive_code": [{"source_code": "=begin\n\nWelcome to GDB Online.\nGDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,\nC#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.\nCode, Compile, Run and Debug online from anywhere in world.\n\n=end\n\nt=gets.to_i\n\nfor i in 1..t\n n =gets.to_i\n s=gets.chomp\n a=0\n q=0\n feil=0\n for j in 1..n\n if s[n-j]==\"A\"\n a+=1\n else\n q+=1\n end\n if q>a\n puts \"No\" \n feil=1\n break\n end\n end\n if (feil==0)\n puts \"Yes\"\n end\nend\n"}], "negative_code": [], "src_uid": "c5389b39312ce95936eebdd83f510e40"} {"nl": {"description": "We call a string good, if after merging all the consecutive equal characters, the resulting string is palindrome. For example, \"aabba\" is good, because after the merging step it will become \"aba\".Given a string, you have to find two values: the number of good substrings of even length; the number of good substrings of odd length. ", "input_spec": "The first line of the input contains a single string of length n (1 ≤ n ≤ 105). Each character of the string will be either 'a' or 'b'.", "output_spec": "Print two space-separated integers: the number of good substrings of even length and the number of good substrings of odd length.", "sample_inputs": ["bb", "baab", "babb", "babaa"], "sample_outputs": ["1 2", "2 4", "2 5", "2 7"], "notes": "NoteIn example 1, there are three good substrings (\"b\", \"b\", and \"bb\"). One of them has even length and two of them have odd length.In example 2, there are six good substrings (i.e. \"b\", \"a\", \"a\", \"b\", \"aa\", \"baab\"). Two of them have even length and four of them have odd length.In example 3, there are seven good substrings (i.e. \"b\", \"a\", \"b\", \"b\", \"bb\", \"bab\", \"babb\"). Two of them have even length and five of them have odd length.DefinitionsA substring s[l, r] (1 ≤ l ≤ r ≤ n) of string s = s1s2... sn is string slsl + 1... sr.A string s = s1s2... sn is a palindrome if it is equal to string snsn - 1... s1."}, "positive_code": [{"source_code": "x = gets.chomp\nx0 = [0, 0]\nx1 = [0, 0]\ny0 = y1 = 0\nx.size.times{ |i|\n p = x[i].ord - 'a'.ord\n if i % 2 == 0\n x0[p] += 1\n y0 += x0[p]\n y1 += x1[p]\n else\n x1[p] += 1\n y0 += x1[p]\n y1 += x0[p]\n end\n}\nputs \"#{y1} #{y0}\""}, {"source_code": "x = gets.chomp\nx0 = [0, 0]\nx1 = [0, 0]\ny0 = y1 = 0\nx.size.times{ |i|\n p = x[i].ord - 'a'.ord\n if i % 2 == 0\n x0[p] += 1\n y0 += x0[p]\n y1 += x1[p]\n else\n x1[p] += 1\n y0 += x1[p]\n y1 += x0[p]\n end\n}\nputs \"#{y1} #{y0}\""}, {"source_code": "a=gets.chomp\ne=[0,0]\no=[0,0]\nea=oa=0\na.size.times{|i| \n\tt=a[i].ord-'a'.ord\n\tif i%2==0\n\t\te[t]+=1\n\t\tea+=o[t]\n\t\toa+=e[t]\n\telse\n\t\to[t]+=1\n\t\tea+=e[t]\n\t\toa+=o[t]\n\tend\n}\nputs \"#{ea} #{oa}\"\n"}, {"source_code": "S = gets.chomp\nN = S.size\nac = [0, 0]\nbc = [0, 0]\nans = [0, 0]\nN.times { |i|\n if S[i] == 'a'\n ac[i & 1] += 1\n ans[0] += ac[~i & 1]\n ans[1] += ac[i & 1]\n else\n bc[i & 1] += 1\n ans[0] += bc[~i & 1]\n ans[1] += bc[i & 1]\n end\n}\nputs ans.join ' '\n"}], "negative_code": [], "src_uid": "4ebbda2fc1a260e9827205a25addd9c4"} {"nl": {"description": "You are given $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$. Is it possible to arrange them on a circle so that each number is strictly greater than both its neighbors or strictly smaller than both its neighbors?In other words, check if there exists a rearrangement $$$b_1, b_2, \\ldots, b_n$$$ of the integers $$$a_1, a_2, \\ldots, a_n$$$ such that for each $$$i$$$ from $$$1$$$ to $$$n$$$ at least one of the following conditions holds: $$$b_{i-1} < b_i > b_{i+1}$$$ $$$b_{i-1} > b_i < b_{i+1}$$$To make sense of the previous formulas for $$$i=1$$$ and $$$i=n$$$, one shall define $$$b_0=b_n$$$ and $$$b_{n+1}=b_1$$$.", "input_spec": "The first line of the input contains a single integer $$$t$$$ ($$$1 \\le t \\le 3\\cdot 10^4$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$3 \\le n \\le 10^5$$$)  — the number of integers. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$0 \\le a_i \\le 10^9$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$2\\cdot 10^5$$$.", "output_spec": "For each test case, if it is not possible to arrange the numbers on the circle satisfying the conditions from the statement, output $$$\\texttt{NO}$$$. You can output each letter in any case. Otherwise, output $$$\\texttt{YES}$$$. In the second line, output $$$n$$$ integers $$$b_1, b_2, \\ldots, b_n$$$, which are a rearrangement of $$$a_1, a_2, \\ldots, a_n$$$ and satisfy the conditions from the statement. If there are multiple valid ways to arrange the numbers, you can output any of them.", "sample_inputs": ["4\n\n3\n\n1 1 2\n\n4\n\n1 9 8 4\n\n4\n\n2 0 2 2\n\n6\n\n1 1 1 11 111 1111"], "sample_outputs": ["NO\nYES\n1 8 4 9 \nNO\nYES\n1 11 1 111 1 1111"], "notes": "NoteIt can be shown that there are no valid arrangements for the first and the third test cases.In the second test case, the arrangement $$$[1, 8, 4, 9]$$$ works. In this arrangement, $$$1$$$ and $$$4$$$ are both smaller than their neighbors, and $$$8, 9$$$ are larger.In the fourth test case, the arrangement $$$[1, 11, 1, 111, 1, 1111]$$$ works. In this arrangement, the three elements equal to $$$1$$$ are smaller than their neighbors, while all other elements are larger than their neighbors."}, "positive_code": [{"source_code": "gets.to_i.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n a.sort!\n m = (n+1)/2\n ans1 = a[0..(m-1)].reverse.rotate\n ans2 = a[m..(n-1)].rotate\n g = ans2.size\n g.times do \n if ans2[-1] == ans1[-1]\n ans2.rotate!\n else\n break\n end\n end\n ans = []\n #puts ans1.inspect\n #puts ans2.inspect\n 0.upto(n-1) do |i|\n ans.push( (i%2==0) ? ans1.pop : ans2.pop)\n end\n #puts ans.inspect\n check = 0\n 0.upto(n-1) do |i|\n if i % 2 == 0 then\n check += 1 if (ans[i]ans[i-1]) and (ans[i]>ans[(i+1)%n]) \n end\n end\n if check == n then\n puts \"YES\" \n puts ans.join(\" \")\n else \n puts \"NO\"\n end\nend\n"}, {"source_code": "gets.to_i.times do\r\n gets\r\n xs = gets.split.map(&:to_i).sort\r\n ys = (0..xs.size - 1).map { |i| xs[i % 2 == 0 ? i / 2 : (xs.size / 2 + i / 2)] }\r\n ok = (0..ys.size - 1).all? do |i|\r\n a, b, c = ys[i - 1], ys[i], ys[(i + 1) % ys.size]\r\n (a < b && b > c) || (a > b && b < c)\r\n end\r\n if ok\r\n puts \"YES\"\r\n puts ys.join(\" \")\r\n else\r\n puts \"NO\"\r\n end\r\nend\r\n"}], "negative_code": [{"source_code": "gets.to_i.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n a.sort!\n ans = []\n 0.upto(n-1) do |i|\n if i % 2 == 0 then\n ans.push(a[i/2])\n else\n ans.push(a[n-1-i/2])\n end\n end\n check = 0\n 0.upto(n-1) do |i|\n if i % 2 == 0 then\n check += 1 if (ans[i]ans[i-1]) and (ans[i]>ans[(i+1)%n]) \n end\n end\n if check == n then\n puts \"YES\" \n puts ans.join(\" \")\n else \n puts \"NO\"\n end\nend\n"}], "src_uid": "a30c5562d3df99291132fac20e05e708"} {"nl": {"description": "Alica and Bob are playing a game.Initially they have a binary string $$$s$$$ consisting of only characters 0 and 1.Alice and Bob make alternating moves: Alice makes the first move, Bob makes the second move, Alice makes the third one, and so on. During each move, the current player must choose two different adjacent characters of string $$$s$$$ and delete them. For example, if $$$s = 1011001$$$ then the following moves are possible: delete $$$s_1$$$ and $$$s_2$$$: $$$\\textbf{10}11001 \\rightarrow 11001$$$; delete $$$s_2$$$ and $$$s_3$$$: $$$1\\textbf{01}1001 \\rightarrow 11001$$$; delete $$$s_4$$$ and $$$s_5$$$: $$$101\\textbf{10}01 \\rightarrow 10101$$$; delete $$$s_6$$$ and $$$s_7$$$: $$$10110\\textbf{01} \\rightarrow 10110$$$. If a player can't make any move, they lose. Both players play optimally. You have to determine if Alice can win.", "input_spec": "First line contains one integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. Only line of each test case contains one string $$$s$$$ ($$$1 \\le |s| \\le 100$$$), consisting of only characters 0 and 1.", "output_spec": "For each test case print answer in the single line. If Alice can win print DA (YES in Russian) in any register. Otherwise print NET (NO in Russian) in any register.", "sample_inputs": ["3\n01\n1111\n0011"], "sample_outputs": ["DA\nNET\nNET"], "notes": "NoteIn the first test case after Alice's move string $$$s$$$ become empty and Bob can not make any move.In the second test case Alice can not make any move initially.In the third test case after Alice's move string $$$s$$$ turn into $$$01$$$. Then, after Bob's move string $$$s$$$ become empty and Alice can not make any move."}, "positive_code": [{"source_code": "t = gets.to_i\nt.times do\n s = gets.chomp.chars\n alice = true\n while s.size >= 2\n i = s.each_cons(2).find_index { |c| c[0] != c[1] }\n break unless i\n s.slice! i, 2\n alice = !alice\n end\n puts (alice ? \"NET\" : \"DA\")\nend\n"}, {"source_code": "def run\n s = gets.chomp\n a = s.split(\"\").map(&:to_i)\n\n if a.size == 1\n puts \"NET\"\n return\n end\n\n c = 0\n\n while true\n idx = find_idx(a)\n if idx == -1\n if c % 2 == 1\n puts \"DA\"\n return\n else\n puts \"NET\"\n return\n end\n else\n c += 1\n a.slice!(idx, 2)\n end\n end\nend\n\ndef find_idx(a)\n (0..(a.length - 2)).each do |idx|\n if a[idx] != a[idx + 1]\n return idx\n end\n end\n\n return -1\nend\n\ndef main\n t = gets.chomp.to_i\n\n t.times do\n run\n end\nend\n\nmain\n\n"}, {"source_code": "n = gets.chomp.to_i\n(0...n).each do \n s = gets.chomp.split(\"\")\n #p s\n count = 1\n #while true\n i = 1\n 100.times do\n if s[i] != s[i-1] && s.size >= 2\n s[i] = \" \"\n s[i-1] = \" \"\n count += 1\n # elsif \n # i += 1\n # count += 1\n end\n #p s\n if s.include?(\" \")\n s.delete(\" \")\n if i >= 1\n i -= 1\n end\n else\n i += 1\n end\n #p s\n ss = s.size\n\n #p count\n if s.count(\"1\") == ss || s.count(\"0\") == ss || s == []\n if count % 2 == 1\n puts \"NET\"\n elsif count % 2 == 0\n puts \"DA\"\n end\n break\n end\n end\n\nend"}], "negative_code": [{"source_code": "n = gets.chomp.to_i\n(0...n).each do \n s = gets.chomp.split(\"\")\n #p s\n count = 1\n #while true\n i = 1\n 250.times do\n if s[i] != s[i-1]\n s[i] = \" \"\n s[i-1] = \" \"\n count += 1\n # elsif \n # i += 1\n # count += 1\n end\n #p s\n if s.include?(\" \")\n s.delete(\" \")\n i = 1\n else\n i += 1\n end\n #p s\n ss = s.size\n #p i\n if s.count(\"1\") == ss || s.count(\"0\") == ss || s == []\n if count % 2 == 1\n puts \"NET\"\n elsif count % 2 == 0\n puts \"DA\"\n end\n break\n end\n end\n\nend"}, {"source_code": "n = gets.chomp.to_i\n(0...n).each do \n s = gets.chomp.split(\"\")\n #p s\n count = 1\n #while true\n i = 1\n 200.times do\n #while \n if s[i] != s[i-1]\n s[i] = \" \"\n s[i-1] = \" \"\n count += 1\n # elsif \n # i += 1\n # count += 1\n end\n #p s\n if s.include?(\" \")\n s.delete(\" \")\n i = 1\n else\n i += 1\n end\n #p s\n ss = s.size\n #p i\n if s.count(\"1\") == ss || s.count(\"0\") == ss || s == []\n if count % 2 == 1\n puts \"NET\"\n elsif count % 2 == 0\n puts \"DA\"\n end\n break\n end\n end\n\nend"}, {"source_code": "n = gets.chomp.to_i\n(0...n).each do \n s = gets.chomp.split(\"\")\n #p s\n count = 1\n #while true\n i = 1\n 100.times do\n if s[i] != s[i-1]\n s[i] = \" \"\n s[i-1] = \" \"\n count += 1\n # elsif \n # i += 1\n # count += 1\n end\n #p s\n if s.include?(\" \")\n s.delete(\" \")\n if i >= 1\n i -= 1\n end\n else\n i += 1\n end\n #p s\n ss = s.size\n\n #p i\n if s.count(\"1\") == ss || s.count(\"0\") == ss || s == []\n if count % 2 == 1\n puts \"NET\"\n elsif count % 2 == 0\n puts \"DA\"\n end\n break\n end\n end\n\nend"}, {"source_code": "n = gets.chomp.to_i\n(0...n).each do \n s = gets.chomp.split(\"\")\n #p s\n count = 1\n #while true\n i = 1\n 110.times do\n if s[i] != s[i-1]\n s[i] = \" \"\n s[i-1] = \" \"\n count += 1\n # elsif \n # i += 1\n # count += 1\n end\n #p s\n if s.include?(\" \")\n s.delete(\" \")\n i = 1\n else\n i += 1\n end\n #p s\n ss = s.size\n #p i\n if s.count(\"1\") == ss || s.count(\"0\") == ss || s == []\n if count % 2 == 1\n puts \"NET\"\n elsif count % 2 == 0\n puts \"DA\"\n end\n break\n end\n end\nend"}, {"source_code": "n = gets.chomp.to_i\n(0...n).each do \n s = gets.chomp.split(\"\")\n #p s\n count = 1\n #while true\n i = 1\n 100.times do\n if s[i] != s[i-1] \n s[i] = \" \"\n s[i-1] = \" \"\n count += 1\n # elsif \n # i += 1\n # count += 1\n end\n #p s\n if s.include?(\" \")\n s.delete(\" \")\n if i >= 1\n i -= 1\n end\n else\n i += 1\n end\n #p s\n ss = s.size\n\n #p count\n if s.count(\"1\") == ss || s.count(\"0\") == ss || s == []\n if count % 2 == 1\n puts \"NET\"\n elsif count % 2 == 0\n puts \"DA\"\n end\n break\n end\n end\n\nend"}], "src_uid": "046900001c7326fc8d890a22fa7267c9"} {"nl": {"description": " You are given an array $$$a_1, a_2, \\dots, a_n$$$ where all $$$a_i$$$ are integers and greater than $$$0$$$. In one operation, you can choose two different indices $$$i$$$ and $$$j$$$ ($$$1 \\le i, j \\le n$$$). If $$$gcd(a_i, a_j)$$$ is equal to the minimum element of the whole array $$$a$$$, you can swap $$$a_i$$$ and $$$a_j$$$. $$$gcd(x, y)$$$ denotes the greatest common divisor (GCD) of integers $$$x$$$ and $$$y$$$. Now you'd like to make $$$a$$$ non-decreasing using the operation any number of times (possibly zero). Determine if you can do this. An array $$$a$$$ is non-decreasing if and only if $$$a_1 \\le a_2 \\le \\ldots \\le a_n$$$.", "input_spec": " The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ positive integers $$$a_1, a_2, \\ldots a_n$$$ ($$$1 \\le a_i \\le 10^9$$$) — the array itself. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.", "output_spec": " For each test case, output \"YES\" if it is possible to make the array $$$a$$$ non-decreasing using the described operation, or \"NO\" if it is impossible to do so.", "sample_inputs": ["4\n1\n8\n6\n4 3 6 6 2 9\n4\n4 5 6 7\n5\n7 5 2 2 4"], "sample_outputs": ["YES\nYES\nYES\nNO"], "notes": "Note In the first and third sample, the array is already non-decreasing. In the second sample, we can swap $$$a_1$$$ and $$$a_3$$$ first, and swap $$$a_1$$$ and $$$a_5$$$ second to make the array non-decreasing. In the forth sample, we cannot the array non-decreasing using the operation."}, "positive_code": [{"source_code": "def gcd(x,y)\n return y==0?x:gcd(y,x%y)\nend\n\nt =gets.to_i\n\nt.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n b = a.sort\n ma = a.min\n res =\"YES\"\n for i in 0...n do\n if gcd(a[i], ma) != ma and b[i] != a[i] then\n res =\"NO\"\n end\n end\n puts res\nend"}, {"source_code": "def solve\n _ = gets\n a = gets.strip.split(' ').map(&:to_i)\n mn = a.min\n slc = a.select { |e| e % mn == 0 }.sort\n p = 0\n a.each_with_index { |e, i|\n if e % mn == 0\n a[i] = slc[p]\n p += 1\n end\n }\n if a == a.sort\n puts \"YES\"\n else\n puts \"NO\"\n end\nend\n\ngets.strip.to_i.times { solve }"}, {"source_code": "t = gets.to_i\nans = []\nt.times do\n n = gets.to_i\n a = gets.chomp.split(\" \").map(&:to_i)\n b = a.sort\n min = b[0]\n ok = true\n n.times do |i|\n if a[i] != b[i] && a[i].gcd(min) != min\n ok = false\n end\n end\n if ok\n ans << \"YES\"\n else\n ans << \"NO\"\n end\nend\nans.each do |x|\n puts x\nend"}, {"source_code": "require 'set'\n\ndef taskA\n a, b = gets.chomp.split(\" \").map(&:to_i)\n puts b > a ? b - a : (a + b) % 2\nend\n\ndef taskB\n a, b, c = gets.chomp.split(\" \").map(&:to_i)\n z, x, y = gets.chomp.split(\" \").map(&:to_i)\n minCX = [c, x].min\n puts (minCX - [0, y - a - c + minCX].max)*2\nend\n\ndef taskC\n n = gets.chomp.split(\" \").map(&:to_i)\n tab = gets.chomp.split(\" \").map(&:to_i)\n min = tab.min\n good = tab.select{ |a| a % min == 0 }.sort\n good.push(1100000000)\n # puts good.join(\" \")\n iter = 0\n prev = min\n tab.map do |a|\n # puts \"now #{a} iter = #{iter}\"\n if a % min == 0\n if prev > good[iter]\n puts \"NO\"\n return\n end\n prev = good[iter]\n iter+=1\n elsif prev > a\n puts \"NO\"\n return\n else\n prev = a\n end\n end\n puts \"YES\"\nend\n\nt = gets.chomp.to_i\nwhile (t-=1) >= 0\n taskC\nend\n"}], "negative_code": [{"source_code": "require 'set'\n\ndef taskA\n a, b = gets.chomp.split(\" \").map(&:to_i)\n puts b > a ? b - a : (a + b) % 2\nend\n\ndef taskB\n a, b, c = gets.chomp.split(\" \").map(&:to_i)\n z, x, y = gets.chomp.split(\" \").map(&:to_i)\n minCX = [c, x].min\n puts (minCX - [0, y - a - c + minCX].max)*2\nend\n\ndef taskC\n n = gets.chomp.split(\" \").map(&:to_i)\n tab = gets.chomp.split(\" \").map(&:to_i)\n min = tab.min\n good = tab.select{ |a| a % min == 0 }.sort\n good.push(1100000000)\n # puts good.join(\" \")\n iter = 0\n tab.map do |a|\n # puts \"now #{a} iter = #{iter}\"\n if a % min == 0\n iter+=1\n elsif good[iter] < a\n puts \"NO\"\n return\n end\n end\n puts \"YES\"\nend\n\nt = gets.chomp.to_i\nwhile (t-=1) >= 0\n taskC\nend\n"}, {"source_code": "require 'set'\n\ndef taskA\n a, b = gets.chomp.split(\" \").map(&:to_i)\n puts b > a ? b - a : (a + b) % 2\nend\n\ndef taskB\n a, b, c = gets.chomp.split(\" \").map(&:to_i)\n z, x, y = gets.chomp.split(\" \").map(&:to_i)\n minCX = [c, x].min\n puts (minCX - [0, y - a - c + minCX].max)*2\nend\n\ndef taskC\n n = gets.chomp.split(\" \").map(&:to_i)\n tab = gets.chomp.split(\" \").map(&:to_i)\n min = tab.min\n good = tab.select{ |a| a % min == 0 }.sort\n good.push(10000000)\n # puts good.join(\" \")\n iter = 0\n tab.map do |a|\n # puts \"now #{a} iter = #{iter}\"\n if a % min == 0\n iter+=1\n elsif good[iter] < a\n puts \"NO\"\n return\n end\n end\n puts \"YES\"\nend\n\nt = gets.chomp.to_i\nwhile (t-=1) >= 0\n taskC\nend\n"}], "src_uid": "f2070c2bd7bdbf0919aef1e915a21a24"} {"nl": {"description": "According to rules of the Berland fashion, a jacket should be fastened by all the buttons except only one, but not necessarily it should be the last one. Also if the jacket has only one button, it should be fastened, so the jacket will not swinging open.You are given a jacket with n buttons. Determine if it is fastened in a right way.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 1000) — the number of buttons on the jacket. The second line contains n integers ai (0 ≤ ai ≤ 1). The number ai = 0 if the i-th button is not fastened. Otherwise ai = 1.", "output_spec": "In the only line print the word \"YES\" if the jacket is fastened in a right way. Otherwise print the word \"NO\".", "sample_inputs": ["3\n1 0 1", "3\n1 0 0"], "sample_outputs": ["YES", "NO"], "notes": null}, "positive_code": [{"source_code": "def fastened?(n, line)\n return if line.length != n || (n < 1 || n > 1000)\n if n > 1 && n <= 1000\n list0 = []\n for i in 0..(line.length - 1)\n if line[i].to_i == 0\n list0 << line[i]\n end\n end\n puts list0.length == 1 ? \"YES\" : \"NO\"\n else\n puts line[0].to_i == 1 ? \"YES\" : \"NO\"\n end\nend\n\nbuttons_length = gets.chomp.to_i\nlist_integers = gets.chomp.split\n\nfastened?(buttons_length, list_integers)\n"}, {"source_code": "def fastened?(n, line)\n return \"WRONG INPUT\" if line.length != n\n if n > 1 && n <= 1000\n list0 = []\n for i in 0..(line.length - 1)\n if line[i].to_i == 0\n list0 << line[i]\n end\n end\n puts list0.length == 1 ? \"YES\" : \"NO\"\n else\n puts line[0].to_i == 1 ? \"YES\" : \"NO\"\n end\nend\n\nbuttons_length = gets.chomp.to_i\nlist_integers = gets.chomp.split\n\nfastened?(buttons_length, list_integers)\n"}, {"source_code": "def valid?(number_button, buttons)\n # special case\n if number_button.to_i == 1\n return buttons == \"1\"\n end\n\n @count = 0\n buttons.split(' ').each do |button|\n @count += 1 if button == '0'\n end\n\n if @count == 1\n return true\n else\n return false\n end\nend\n\ndef execute(number_button, buttons)\n if valid?(number_button, buttons)\n puts 'YES'\n else\n puts 'NO'\n end\nend\n\nnumber_button = gets.chomp\nbuttons = gets.chomp\n\nexecute(number_button, buttons)\n"}, {"source_code": "z=gets.to_i==1?:none?: :one?;puts gets.split.send(z){|e|e==?0}?:YES: :NO"}, {"source_code": "def checks(quality_buttons, buttons)\n return puts 'NO' unless quality_buttons.to_i <= 1000 && quality_buttons.to_i >= 1\n not_install = 0\n buttons.split(' ').each do |b|\n not_install += 1 if b.to_i == 0 || b.to_i > 1 || b.to_i < 0\n end\n\n return puts 'YES' if quality_buttons.to_i == 1 && not_install == 0\n r = if quality_buttons.to_i > 1 && not_install == 1\n 'YES'\n else\n 'NO'\n end\n\n puts r\nend\n\nchecks(gets.chomp, gets.chomp)\n"}, {"source_code": "n = gets.to_i\nmas = gets.split.map(&:to_i)\n\nnum = 0\n\nmas.each {|x| num+=1 if x==0}\n\nif (n==1 and num==0) or (num==1 and n!=1)\n puts \"YES\"\nelse\n puts \"NO\"\nend"}, {"source_code": "jacket_number = gets.to_i\n# jacket_number = '3'.to_i\njacket_list = gets.split(' ').map(&:to_i)\n# jacket_list = '1 0 1'.split(' ').map(&:to_i)\n\ndef fastened?(jacket_list)\n unFastened = []\n jacket_list.each do |j|\n unFastened << 0 if j.zero?\n return false if unFastened.count > 1\n end\n unFastened.count == 1\nend\n\nif jacket_number == 1\n puts jacket_list[0] == 1 ? 'YES' : 'NO'\nelse\n puts fastened?(jacket_list) ? 'YES' : 'NO'\nend\n"}, {"source_code": "n = gets.strip.to_i\nbuttons = gets.strip.split.map(&:to_i)\ncount_fastened = 0\n(0...n).each do |i|\n button = buttons[i]\n count_fastened += 1 if button == 1\nend\nif (n > 1 && n - 1 == count_fastened) || (count_fastened == n && n == 1)\n puts \"YES\"\nelse\n puts \"NO\"\nend\n"}, {"source_code": "a=gets.chomp.to_i\nb=gets.chomp.split(\" \")\nif a==1\nif b.count('1')==0\nputs \"NO\"\nelse\nputs \"YES\"\nend\nelse\nif b.count('1')!=a-1\nputs \"NO\"\nelse\nputs \"YES\"\nend\nend\n"}, {"source_code": "n = gets.to_i\narr = gets.split.map(&:to_i)\n\nif arr.size == 1\n puts arr.first == 1 ? 'YES' : 'NO'\nelse\n puts arr.count { |v| v.zero? } == 1 ? 'YES' : 'NO'\nend\n"}, {"source_code": "n = gets.to_i\nsum = 0\ngets.split(\" \").map{|s| s.to_i }.each do |i| \n\tsum += i\nend\nif n == 1\n n = 2\nend\nputs sum==n-1?\"YES\":\"NO\"\n\n"}, {"source_code": "n = gets.to_i\nnums = gets.split.map(&:to_i)\n\n=begin\nsomething = 0\n\nfor i in nums\n\tif nums[i] == 0\n\t\tsomething += 1\n\tend\nend\n=end\n\n\n\nif (nums.count(0) == 1 && nums.length > 1) || (nums.length == 1 && nums[0] == 1)\n\tputs \"YES\" \nelse \n\tputs \"NO\"\nend"}, {"source_code": "n = gets.to_i\narr = gets.split.map(&:to_i)\nif arr.length == 1\n\tputs arr.first == 0 ? \"NO\" : \"YES\"\nelse\n\tcnt = 0\n\tarr.each do |num|\n\t\tcnt += 1 if num == 0\n\tend\n\tputs cnt == 1 ? \"YES\" : \"NO\"\nend"}, {"source_code": "#!/usr/bin/env ruby\n\nn = ARGF.gets.to_i\n\nbuttons = ARGF.gets.chomp.split(\" \").map { |s| s.to_i }\n\nclosed = buttons.reduce(:+)\n\nif n == 1\n if closed == 1\n puts \"YES\"\n else\n puts \"NO\"\n end\nelsif closed + 1 == n\n puts \"YES\"\nelse\n puts \"NO\"\nend\n"}, {"source_code": "a=gets.chomp.to_i\nb=gets.chomp.split(\" \")\nif a==1\nif b.count('1')==0\nputs \"NO\"\nelse\nputs \"YES\"\nend\nelse\nif b.count('1')!=a-1\nputs \"NO\"\nelse\nputs \"YES\"\nend\nend\n\n\n"}, {"source_code": "n = gets.chomp.to_i\nbuttons = gets.split.map(&:to_i)\nif n == 1\n puts buttons[0] == 1 ? 'YES' : 'NO'\nelse\n not_fastened = 0\n buttons.each do |b|\n not_fastened += 1 if b == 0\n end\n puts not_fastened == 1 ? 'YES' : 'NO' \nend\n"}], "negative_code": [{"source_code": "n = gets.chomp.to_i\nbuttons = gets.split.map(&:to_i)\nnot_fastened = 0\nbuttons.each do |b|\n not_fastened += 1 if b == 0\nend\n(not_fastened == 1 || buttons.size == 1) ? 'YES' : 'NO' \n"}, {"source_code": "$stdout.sync = true\ndef valid?(number_button, buttons)\n return false if buttons.to_s.split(' ').size > number_button.to_i\n\n @count = 0\n\n buttons.split(' ').each do |button|\n @count += 1 if button == '0'\n return false if @count >= 2\n end\n\n if @count == 0\n return false\n else\n return true\n end\n\nend\n\ndef execute(number_button, buttons)\n if valid?(number_button, buttons)\n puts 'YES'\n else\n puts 'NO'\n end\nend\n\nnumber_button = gets.chomp\nbuttons = gets.chomp\n\nexecute(number_button, buttons)\n"}, {"source_code": "\ndef valid?(number_button, buttons)\n return false if buttons.to_s.split(' ').size > number_button.to_i\n\n @count = 0\n\n buttons.split(' ').each do |button|\n @count += 1 if button == '0'\n return false if @count >= 2\n end\n\n return true\nend\n\ndef execute(number_button, buttons)\n if valid?(number_button, buttons)\n puts 'YES'\n else\n puts 'NO'\n end\nend\n\nnumber_button = gets.chomp\nbuttons = gets.chomp\n\nexecute(number_button, buttons)\n"}, {"source_code": "def valid?(number_button, buttons)\n # special case\n if number_button.to_i == 1\n return buttons == \"1\"\n else\n return false\n end\n\n @count = 0\n buttons.split(' ').each do |button|\n @count += 1 if button == '0'\n return false if @count >= 2\n end\n\n if @count == 1\n return true\n else\n return false\n end\nend\n\ndef execute(number_button, buttons)\n if valid?(number_button, buttons)\n puts 'YES'\n else\n puts 'NO'\n end\nend\n\nnumber_button = gets.chomp\nbuttons = gets.chomp\n\nexecute(number_button, buttons)\n"}, {"source_code": "def checks(quality_buttons, buttons)\n return puts 'NO' unless quality_buttons.to_i <= 1000 && quality_buttons.to_i >= 1\n not_install = 0\n buttons.split(' ').each do |b|\n not_install += 1 if b.to_i == 0 || b.to_i > 1 || b.to_i < 0\n end\n\n r = if ( quality_buttons.to_i == 1 && not_install >= 1 )\n 'NO'\n elsif not_install == 1\n 'YES'\n elsif not_install == 0\n 'YES'\n else\n 'NO'\n end\n\n puts r\nend\n\nchecks(gets.chomp, gets.chomp)\n"}, {"source_code": "def checks(quality_buttons, buttons)\n return p 'NO' unless quality_buttons.to_i <= 1000 && quality_buttons.to_i >= 1\n not_install = 0\n buttons.split('').each do |b|\n not_install += 1 if b.to_i == 0 || b.to_i > 1 || b.to_i < 0\n end\n\n r = if ( quality_buttons.to_i == 1 && not_install == 1 )\n 'NO'\n elsif not_install <= 1\n 'YES'\n else\n 'NO'\n end\n\n p r\nend\n\nchecks(gets.chomp, gets.chomp)\n"}, {"source_code": "def checks(quality_buttons, buttons)\n return puts 'NO' unless quality_buttons.to_i <= 1000 && quality_buttons.to_i >= 1\n not_install = 0\n buttons.split(' ').each do |b|\n not_install += 1 if b.to_i == 0 || b.to_i > 1 || b.to_i < 0\n end\n\n return puts 'YES' if quality_buttons.to_i == 1 && not_install == 0\n p not_install\n r = if quality_buttons.to_i > 1 && not_install == 1\n 'YES'\n else\n 'NO'\n end\n\n puts r\nend\n\nchecks(gets.chomp, gets.chomp)\n"}, {"source_code": "def checks(quality_buttons, buttons)\n return puts 'NO' unless quality_buttons.to_i <= 1000 && quality_buttons.to_i >= 1\n not_install = 0\n buttons.split(' ').each do |b|\n not_install += 1 if b.to_i == 0 || b.to_i > 1 || b.to_i < 0\n end\n\n r = if ( quality_buttons.to_i == 1 && not_install == 1 )\n 'NO'\n elsif not_install <= 1\n 'YES'\n else\n 'NO'\n end\n\n puts r\nend\n\nchecks(gets.chomp, gets.chomp)\n"}, {"source_code": "def checks(quality_buttons, buttons)\n return puts 'NO' unless quality_buttons.to_i <= 1000 && quality_buttons.to_i >= 1\n not_install = 0\n buttons.split(' ').each do |b|\n not_install += 1 if b.to_i == 0 || b.to_i > 1 || b.to_i < 0\n end\n\n r = if ( quality_buttons.to_i == 1 && not_install == 1 )\n 'NO'\n elsif not_install == 1\n 'YES'\n else\n 'NO'\n end\n\n puts r\nend\n\nchecks(gets.chomp, gets.chomp)\n"}, {"source_code": "def checks(quality_buttons, buttons)\n return puts 'NO' unless quality_buttons.to_i <= 1000 && quality_buttons.to_i >= 1\n not_install = 0\n buttons.split(' ').each do |b|\n not_install += 1 if b.to_i == 0 || b.to_i > 1 || b.to_i < 0\n end\n\n r = if ( quality_buttons.to_i == 1 && not_install >= 1 )\n 'NO'\n elsif not_install == 1\n 'YES'\n elsif not_install == 0\n 'YES'\n else\n 'NO'\n end\n\n puts r \nend\n\nchecks(gets.chomp, gets.chomp)\n"}, {"source_code": "jacket_number = gets.to_i\n# jacket_number = '3'.to_i\njacket_list = gets.split(' ').map(&:to_i)\n# jacket_list = '1 0 1'.split(' ').map(&:to_i)\n\ndef fastened?(jacket_list)\n unFastened = []\n jacket_list.each do |j|\n unFastened << 0 if j.zero?\n return false if unFastened.count > 1\n end\n true\nend\n\nif jacket_number == 1\n puts jacket_list[0] == 1 ? 'YES' : 'NO'\nelse\n puts fastened?(jacket_list) ? 'YES' : 'NO'\nend\n"}, {"source_code": "jacket_number = gets.to_i\njacket_list = gets.split(' ').map(&:to_i)\n\ndef fastened?(jacket_list)\n unFastened = []\n jacket_list.each do |j|\n unFastened << 0 if j.zero?\n return false if unFastened.count > 1\n end\n return true\nend\n\nif jacket_number == 1\n puts jacket_list[0] == 1 ? 'YES' : 'NO'\nelse\n puts fastened?(jacket_list) ? 'YES' : 'NO'\nend\n"}, {"source_code": "n = gets.strip.to_i\nbuttons = gets.strip.split.map(&:to_i)\ncount_fastened = 0\n(0...n).each do |i|\n button = buttons[i]\n count_fastened += 1 if button == 1\nend\nif n - 1 == count_fastened || (count_fastened == n && count_fastened == 1)\n puts \"YES\"\nelse\n puts \"NO\"\nend\n"}, {"source_code": "n = gets.strip.to_i\nbuttons = gets.strip.split.map(&:to_i)\nans = \"YES\"\ncount_open = 0\n(0...n).each do |i|\n button = buttons[i]\n count_open += 1 if button == 0\nend\nif n == 1 && count_open == n\n ans = \"NO\"\nelsif count_open > 1\n ans = \"NO\"\nend\nputs ans\n"}, {"source_code": "n = gets.to_i\narr = gets.split.map(&:to_i)\n\nif arr.size == 1\n puts arr.first.zero? ? 'YES' : 'NO'\nelse\n puts arr.count { |v| v.zero? } == 1 ? 'YES' : 'NO'\nend\n"}, {"source_code": "n = gets.to_i\nsum = 0\ngets.split(\" \").map{|s| s.to_i }.each do |i| \n\tsum += i\nend\nputs sum==n-1?\"YES\":\"NO\"\n\n"}, {"source_code": "n = gets.to_i\nnums = gets.split.map(&:to_i)\n\n=begin\nsomething = 0\n\nfor i in nums\n\tif nums[i] == 0\n\t\tsomething += 1\n\tend\nend\n=end\n\n\n\nif nums.count(0) == 1 || nums.length == 1 \n\tputs \"YES\" \nelse \n\tputs \"NO\"\nend"}, {"source_code": "n = gets.to_i\nnums = gets.split.map(&:to_i)\n\n=begin\nsomething = 0\n\nfor i in nums\n\tif nums[i] == 0\n\t\tsomething += 1\n\tend\nend\n=end\n\n\n\nif nums.count(0) == 1\n\tputs \"YES\" \nelse \n\tputs \"NO\"\nend"}, {"source_code": "n = gets.to_i\nnums = gets.split.map(&:to_i)\n\n=begin\nsomething = 0\n\nfor i in nums\n\tif nums[i] == 0\n\t\tsomething += 1\n\tend\nend\n=end\n\n\n\nif nums.count(0) == 1 || (nums.length == 1 && nums[0] == 1)\n\tputs \"YES\" \nelse \n\tputs \"NO\"\nend"}, {"source_code": "n = gets.to_i\narr = gets.split.map(&:to_i)\nif arr.length == 1\n\tarr.first == 0 ? \"NO\" : \"YES\"\nelse\n\tcnt = 0\n\tarr.each do |num|\n\t\tcnt += 1 if num == 0\n\tend\n\tcnt == 1 ? \"YES\" : \"NO\"\nend"}, {"source_code": "n = gets.chomp.to_i\nbuttons = gets.split.map(&:to_i)\nnot_fastened = 0\nbuttons.each do |b|\n not_fastened += 1 if b == 0\nend\nputs (not_fastened == 1 || buttons.size == 1) ? 'YES' : 'NO' \n"}, {"source_code": "def check_fastened \n n = gets.chomp.to_i\n buttons = gets.split.map(&:to_i)\n return 'YES' if buttons.size == 1\n not_fastened = 0\n buttons.each do |b|\n not_fastened += 1 if b == 0\n end\n not_fastened > 1 ? 'NO' : 'YES' \n\nend"}, {"source_code": "n = gets.chomp.to_i\nbuttons = gets.split.map(&:to_i)\nreturn 'YES' if buttons.size == 1\nnot_fastened = 0\nbuttons.each do |b|\n not_fastened += 1 if b == 0\nend\nnot_fastened > 1 ? 'NO' : 'YES' "}, {"source_code": "n = gets.chomp.to_i\nbuttons = gets.split.map(&:to_i)\nreturn 'YES' if buttons.size == 1\nnot_fastened = 0\nbuttons.each do |b|\n not_fastened += 1 if b == 0\nend\nnot_fastened == 1 ? 'YES' : 'NO' \n"}], "src_uid": "7db0b870177e7d7b01da083e21ba35f5"} {"nl": {"description": "You know that the Martians use a number system with base k. Digit b (0 ≤ b < k) is considered lucky, as the first contact between the Martians and the Earthlings occurred in year b (by Martian chronology).A digital root d(x) of number x is a number that consists of a single digit, resulting after cascading summing of all digits of number x. Word \"cascading\" means that if the first summing gives us a number that consists of several digits, then we sum up all digits again, and again, until we get a one digit number.For example, d(35047) = d((3 + 5 + 0 + 4)7) = d(157) = d((1 + 5)7) = d(67) = 67. In this sample the calculations are performed in the 7-base notation.If a number's digital root equals b, the Martians also call this number lucky.You have string s, which consists of n digits in the k-base notation system. Your task is to find, how many distinct substrings of the given string are lucky numbers. Leading zeroes are permitted in the numbers.Note that substring s[i... j] of the string s = a1a2... an (1 ≤ i ≤ j ≤ n) is the string aiai + 1... aj. Two substrings s[i1... j1] and s[i2... j2] of the string s are different if either i1 ≠ i2 or j1 ≠ j2.", "input_spec": "The first line contains three integers k, b and n (2 ≤ k ≤ 109, 0 ≤ b < k, 1 ≤ n ≤ 105). The second line contains string s as a sequence of n integers, representing digits in the k-base notation: the i-th integer equals ai (0 ≤ ai < k) — the i-th digit of string s. The numbers in the lines are space-separated.", "output_spec": "Print a single integer — the number of substrings that are lucky numbers. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.", "sample_inputs": ["10 5 6\n3 2 0 5 6 1", "7 6 4\n3 5 0 4", "257 0 3\n0 0 256"], "sample_outputs": ["5", "1", "3"], "notes": "NoteIn the first sample the following substrings have the sought digital root: s[1... 2] = \"3 2\", s[1... 3] = \"3 2 0\", s[3... 4] = \"0 5\", s[4... 4] = \"5\" and s[2... 6] = \"2 0 5 6 1\"."}, "positive_code": [{"source_code": "a=Hash.new 0;a[0]=1;k,b,n=gets.split.map &:to_i;s=t=c=z=0;k-=1\n(gets.split.map &:to_i).each{|i|s=(s+i)%k;z+=a[(s-b)%k];a[s]+=1;if i>0 then c=0;else t+=c+=1 end}\nif b==0 then z=t elsif b==k then z-=t end;p z\n"}, {"source_code": "a=Hash.new 0;a[0]=1;k,b,n=gets.split.map &:to_i;s=t=c=z=0;k-=1\n(gets.split.map &:to_i).each{|i|s=(s+i)%k;z+=a[(s-b)%k];a[s]+=1;if i>0 then c=0;else t+=c+=1 end}\nif b==0 then z=t elsif b==k then z-=t end;p z\n"}, {"source_code": "a=Hash.new 0;a[0]=1;k,b,n=gets.split.map &:to_i;s=t=c=z=0;k-=1\n(gets.split.map &:to_i).each{|i|s=(s+i)%k;z+=a[(s-b)%k];a[s]+=1;if i>0 then c=0;else t+=c+=1 end}\nif b==0 then z=t elsif b==k then z-=t end;p z\n"}, {"source_code": "a=Hash.new 0;a[0]=1;k,b,n=gets.split.map &:to_i;s=t=c=z=0;k-=1\n(gets.split.map &:to_i).each{|i|s=(s+i)%k;z+=a[(s-b)%k];a[s]+=1;if i>0 then c=0;else t+=c+=1 end}\nif b==0 then z=t elsif b==k then z-=t end;p z\n"}, {"source_code": "a=Hash.new 0;a[0]=1;k,b,n=gets.split.map &:to_i;s=t=c=z=0;k-=1\n(gets.split.map &:to_i).each{|i|s=(s+i)%k;z+=a[(s-b)%k];a[s]+=1;if i>0 then c=0;else t+=c+=1 end}\nif b==0 then z=t elsif b==k then z-=t end;p z"}, {"source_code": "a=Hash.new 0;a[0]=1;k,b,n=gets.split.map &:to_i;s=t=c=z=0;k-=1\n(gets.split.map &:to_i).each{|i|s=(s+i)%k;z+=a[(s-b)%k];a[s]+=1;if i>0 then c=0;else t+=c+=1 end}\nif b==0 then z=t elsif b==k then z-=t end;p z\n"}, {"source_code": "a=Hash.new 0;a[0]=1;k,b,n=gets.split.map &:to_i;s=t=c=z=0;k-=1\n(gets.split.map &:to_i).each{|i|s=(s+i)%k;z+=a[(s-b)%k];a[s]+=1;if i>0 then c=0;else t+=c+=1 end}\nif b==0 then z=t elsif b==k then z-=t end;p z\n"}, {"source_code": "a=Hash.new 0;a[0]=1;k,b,n=gets.split.map &:to_i;s=t=c=z=0;k-=1\n(gets.split.map &:to_i).each{|i|s=(s+i)%k;z+=a[(s-b)%k];a[s]+=1;if i>0 then c=0;else t+=c+=1 end}\nif b==0 then z=t elsif b==k then z-=t end;p z\n"}, {"source_code": "a=Hash.new 0;a[0]=1;k,b,n=gets.split.map &:to_i;s=t=c=z=0;k-=1\n(gets.split.map &:to_i).each{|i|s=(s+i)%k;z+=a[(s-b)%k];a[s]+=1;if i>0 then c=0;else t+=c+=1 end}\nif b==0 then z=t elsif b==k then z-=t end;p z\n"}], "negative_code": [{"source_code": "a=Hash.new 0;a[0]=1;k,b,n=gets.split.map &:to_i;s=t=c=z=0;k-=1\n(gets.split.map &:to_i).each{|i|s=(s+i)%k;z+=a[(s-b)%k];a[s]+=1;if i>0 then c=0;else t+=c+=1 end}\nif b==0 then z=t elsif b==k-1 then z-=t end;p z"}], "src_uid": "bbc2683d207f147a2a0abedc67ff157a"} {"nl": {"description": "People do many crazy things to stand out in a crowd. Some of them dance, some learn by heart rules of Russian language, some try to become an outstanding competitive programmers, while others collect funny math objects.Alis is among these collectors. Right now she wants to get one of k-special tables. In case you forget, the table n × n is called k-special if the following three conditions are satisfied: every integer from 1 to n2 appears in the table exactly once; in each row numbers are situated in increasing order; the sum of numbers in the k-th column is maximum possible. Your goal is to help Alice and find at least one k-special table of size n × n. Both rows and columns are numbered from 1 to n, with rows numbered from top to bottom and columns numbered from left to right.", "input_spec": "The first line of the input contains two integers n and k (1 ≤ n ≤ 500, 1 ≤ k ≤ n) — the size of the table Alice is looking for and the column that should have maximum possible sum.", "output_spec": "First print the sum of the integers in the k-th column of the required table. Next n lines should contain the description of the table itself: first line should contains n elements of the first row, second line should contain n elements of the second row and so on. If there are multiple suitable table, you are allowed to print any.", "sample_inputs": ["4 1", "5 3"], "sample_outputs": ["28\n1 2 3 4\n5 6 7 8\n9 10 11 12\n13 14 15 16", "85\n5 6 17 18 19\n9 10 23 24 25\n7 8 20 21 22\n3 4 14 15 16\n1 2 11 12 13"], "notes": null}, "positive_code": [{"source_code": "n, k = gets.split.map(&:to_i)\nans = Array.new(n){Array.new(n)}\nn2 = n*n\nfor i in 0...n\n for j in 0..(n - k)\n ans[i][n - 1 - j] = n2\n n2 -= 1\n end\nend\na = 1\nfor i in 0...n\n for j in 0...(k-1)\n ans[i][j] = a \n a += 1\n end\nend\nsum = 0\nn.times do |i|\n sum += ans[i][k-1]\nend\nputs sum\nn.times do |i|\n puts \"#{ans[i].join(' ')}\"\nend"}, {"source_code": "n, k = gets.chomp.split.map(&:to_i)\n\nas = Array.new(n) { Array.new(n, 0) }\ns = 0\nm = 1\nfor i in 0...n\n for j in 0...k - 1\n as[i][j] = m\n m += 1\n end\nend\n\nfor i in 0...n\n as[i][k - 1] = m\n s += m\n m += 1\n for j in k...n\n as[i][j] = m\n m += 1\n end\nend\n\nputs s\nas.map { |a| puts a.*' ' }\n"}, {"source_code": "n, k = gets.split(' ').map &:to_i\nmin = (k-1)*n + 1\nsum = 0\n\ng = []\nic = 1\noc = min\nfor i in 0...n\n g[i] = []\n for j in 0...n\n if j < k-1\n g[i][j] = ic\n ic += 1\n else\n g[i][j] = oc\n oc += 1\n end\n end\n sum += g[i][k-1]\nend\n\nputs sum\nfor r in g\n puts r.join(' ')\nend \n"}], "negative_code": [{"source_code": "n, k = gets.split(' ').map &:to_i\nmin = (k-1)*n + 1\nsum = min\nfor i in 1...n\n sum += min + i*k\nend\n\ng = []\nic = 1\noc = min\nputs sum\nfor i in 0...n\n g[i] = []\n for j in 0...n\n if j < k-1\n g[i][j] = ic\n ic += 1\n else\n g[i][j] = oc\n oc += 1\n end\n end\n puts g[i].join(' ')\nend\n"}], "src_uid": "272f66f3c71c4997c5a1f0f009c9b99e"} {"nl": {"description": "In this problem your goal is to sort an array consisting of n integers in at most n swaps. For the given array find the sequence of swaps that makes the array sorted in the non-descending order. Swaps are performed consecutively, one after another.Note that in this problem you do not have to minimize the number of swaps — your task is to find any sequence that is no longer than n.", "input_spec": "The first line of the input contains integer n (1 ≤ n ≤ 3000) — the number of array elements. The second line contains elements of array: a0, a1, ..., an - 1 ( - 109 ≤ ai ≤ 109), where ai is the i-th element of the array. The elements are numerated from 0 to n - 1 from left to right. Some integers may appear in the array more than once.", "output_spec": "In the first line print k (0 ≤ k ≤ n) — the number of swaps. Next k lines must contain the descriptions of the k swaps, one per line. Each swap should be printed as a pair of integers i, j (0 ≤ i, j ≤ n - 1), representing the swap of elements ai and aj. You can print indices in the pairs in any order. The swaps are performed in the order they appear in the output, from the first to the last. It is allowed to print i = j and swap the same pair of elements multiple times. If there are multiple answers, print any of them. It is guaranteed that at least one answer exists.", "sample_inputs": ["5\n5 2 5 1 4", "6\n10 20 20 40 60 60", "2\n101 100"], "sample_outputs": ["2\n0 3\n4 2", "0", "1\n0 1"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\n\na = gets.split.map { |x| x.to_i }\n\nb = a.dup\nb.sort!\n\nres = []\n0.upto(a.size-1) do |i|\n\tif a[i]!= b[i]\n\t\tj = a.rindex b[i]\n\t\ta[j] = a[i]\n\t\tres << \"#{i} #{j}\"\n\tend\nend\nputs res.size\nputs res\n\n"}, {"source_code": "n = STDIN.readline.to_i\ndata = STDIN.readline.split.each_with_index.map {|s, i| [s.to_i, i] }\nsorted = data.sort\nindex = {}\ndata.each_with_index {|pair, i| index[pair] = i }\nmoves = []\nsorted.each_with_index do |p, i|\n\tnext if index[p] == i\n\tq, j = data[i], index[p]\n\tdata[i], index[p] = p, i\n\tdata[j], index[q] = q, j\n\tmoves.push([i, j])\nend\nputs moves.length\nputs moves.map {|a, b| '%d %d' % [a, b] }.join \"\\n\" if moves.length != 0\n"}, {"source_code": "#$stdin = File.open \"input.txt\"\n\nn = gets.to_i\na = gets.split.map &:to_i\nb = (0...n).to_a\n\nputs n - 1\n(n - 1).times do |i|\n\tm = a[i...n].min\n\tj = i + a[i...n].index(m)\n\t\n\tputs \"#{i} #{j}\"\n\ta[i], a[j] = a[j], a[i]\nend\n\n"}, {"source_code": "#!/usr/bin/ruby\n\nn = gets.to_i\nl = gets.chomp.split(' ').map { |i| i.to_i }\ns = 0\nsw = []\n(0..n-2).each do |i| \n im = l[i..n-1].index(l[i..n-1].min) + i\n if im != i\n temp = l[i]\n l[i] = l[im]\n l[im] = temp\n s += 1\n sw << \"#{i} #{im}\"\n end\nend\n\np s\nputs sw\n\n"}], "negative_code": [{"source_code": "n = gets.to_i\n\na = gets.split.map { |x| x.to_i }\n\nb = a.dup\nb.sort!\n\nres = []\n0.upto(a.size-1) do |i|\n\tif a[i]!= b[i]\n\t\tj = a.index b[i]\n\t\ta[j] = a[i]\n\t\tres << \"#{i} #{j}\"\n\tend\nend\nputs res.size\nputs res\n\n"}, {"source_code": "n = STDIN.readline.to_i\ndata = STDIN.readline.split.each_with_index.map {|s, i| [s.to_i, i] }\nsorted = data.sort\nindex = {}\ndata.each_with_index {|pair, i| index[pair] = i }\nmoves = []\nsorted.each_with_index do |pair, i|\n\tnext if index[pair] == i\n\tmoves.push([i, index[pair]])\n\tindex[data[i]] = index[pair]\n\tindex[pair] = i\nend\nputs moves.length\nputs moves.map {|a, b| '%d %d' % [a, b] }.join \"\\n\" if moves.length != 0\n"}, {"source_code": "#$stdin = File.open \"input.txt\"\n\nn = gets.to_i\na = gets.split.map &:to_i\nb = (0...n).to_a\n\nputs n - 1\n(n - 1).times do |i|\n\tm = a[i + 1...n].min\n\tj = a.index(m)\n\t\n\tputs \"#{i} #{j}\"\n\ta[i], a[j] = a[j], a[i]\nend\n\n"}, {"source_code": "#$stdin = File.open \"input.txt\"\n\nn = gets.to_i\na = gets.split.map &:to_i\nb = (0...n).to_a\n\n(1...n).each do |i|\n\tj = i\n\twhile (j > 0) && (a[j] < a[j - 1])\n\t\ta[j], a[j - 1] = a[j - 1], a[j]\n\t\tb[j], b[j - 1] = b[j - 1], b[j]\n\t\tj -= 1\n\tend\nend\n\nswaps = []\nn.times { |i| swaps << [i, b[i]] if b[i] > i }\n\nputs swaps.size\nswaps.each { |s| puts \"#{s[0]} #{s[1]}\" }\n"}], "src_uid": "b3c6058893f935d88196113fab581cf8"} {"nl": {"description": "By the age of three Smart Beaver mastered all arithmetic operations and got this summer homework from the amazed teacher:You are given a sequence of integers a1, a2, ..., an. Your task is to perform on it m consecutive operations of the following type: For given numbers xi and vi assign value vi to element axi. For given numbers li and ri you've got to calculate sum , where f0 = f1 = 1 and at i ≥ 2: fi = fi - 1 + fi - 2. For a group of three numbers li ri di you should increase value ax by di for all x (li ≤ x ≤ ri). Smart Beaver planned a tour around great Canadian lakes, so he asked you to help him solve the given problem.", "input_spec": "The first line contains two integers n and m (1 ≤ n, m ≤ 2·105) — the number of integers in the sequence and the number of operations, correspondingly. The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 105). Then follow m lines, each describes an operation. Each line starts with an integer ti (1 ≤ ti ≤ 3) — the operation type: if ti = 1, then next follow two integers xi vi (1 ≤ xi ≤ n, 0 ≤ vi ≤ 105); if ti = 2, then next follow two integers li ri (1 ≤ li ≤ ri ≤ n); if ti = 3, then next follow three integers li ri di (1 ≤ li ≤ ri ≤ n, 0 ≤ di ≤ 105). The input limits for scoring 30 points are (subproblem E1): It is guaranteed that n does not exceed 100, m does not exceed 10000 and there will be no queries of the 3-rd type. The input limits for scoring 70 points are (subproblems E1+E2): It is guaranteed that there will be queries of the 1-st and 2-nd type only. The input limits for scoring 100 points are (subproblems E1+E2+E3): No extra limitations. ", "output_spec": "For each query print the calculated sum modulo 1000000000 (109).", "sample_inputs": ["5 5\n1 3 1 2 4\n2 1 4\n2 1 5\n2 2 4\n1 3 10\n2 1 5", "5 4\n1 3 1 2 4\n3 1 4 1\n2 2 4\n1 2 10\n2 1 5"], "sample_outputs": ["12\n32\n8\n50", "12\n45"], "notes": null}, "positive_code": [{"source_code": "\nt = STDIN.gets\na1 = t.split\nn = a1[0].to_i\nm = a1[1].to_i\n\nt = STDIN.gets\na = t.split\n@a = []\na.count.times do |i|\n @a[i] = a[i].to_i\nend\n@f = []\n@f[0] = 1\n@f[1] = 1\ndef operation1(x,v)\n @a[x] = v % 1000000000\nend\ndef f(x)\n @f[x] if x < @f.count\n fcount = @f.count\n (x - fcount + 1).times do |j|\n @f[fcount + j] = (@f[fcount + j - 2] + @f[fcount + j - 1]) % 1000000000\n end\n @f[x]\nend\n\ndef sum(x,l,r)\n f(x)*@a[l + x] % 1000000000\nend\n\ndef operation2(l,r)\n s = 0\n (r - l + 1).times do |x|\n s = (s + sum(x, l, r)) % 1000000000\n end\n puts (s % 1000000000)\nend\n\ndef operation3(l,r,d)\n\n ( r-l + 1).times do |x|\n @a[l + x] = (@a[l + x] + d) % 1000000000\n end\nend\n\nm.times do |i|\n t = STDIN.gets\n operation = t.split\n if operation[0] == '1'\n operation1(operation[1].to_i - 1, operation[2].to_i)\n end\n\n if operation[0] == '2'\n operation2(operation[1].to_i - 1, operation[2].to_i - 1)\n end\n\n if operation[0] == '3'\n operation3(operation[1].to_i - 1, operation[2].to_i - 1 , operation[3].to_i)\n end\nend\n"}], "negative_code": [], "src_uid": "165467dd842b47bc2d932b04e85ae8d7"} {"nl": {"description": "The prison of your city has n prisoners. As the prison can't accommodate all of them, the city mayor has decided to transfer c of the prisoners to a prison located in another city.For this reason, he made the n prisoners to stand in a line, with a number written on their chests. The number is the severity of the crime he/she has committed. The greater the number, the more severe his/her crime was.Then, the mayor told you to choose the c prisoners, who will be transferred to the other prison. He also imposed two conditions. They are, The chosen c prisoners has to form a contiguous segment of prisoners. Any of the chosen prisoner's crime level should not be greater then t. Because, that will make the prisoner a severe criminal and the mayor doesn't want to take the risk of his running away during the transfer. Find the number of ways you can choose the c prisoners.", "input_spec": "The first line of input will contain three space separated integers n (1 ≤ n ≤ 2·105), t (0 ≤ t ≤ 109) and c (1 ≤ c ≤ n). The next line will contain n space separated integers, the ith integer is the severity ith prisoner's crime. The value of crime severities will be non-negative and will not exceed 109. ", "output_spec": "Print a single integer — the number of ways you can choose the c prisoners.", "sample_inputs": ["4 3 3\n2 3 1 1", "1 1 1\n2", "11 4 2\n2 2 0 7 3 2 2 4 9 1 4"], "sample_outputs": ["2", "0", "6"], "notes": null}, "positive_code": [{"source_code": "n, t, c = gets.strip.split.map(&:to_i)\na = gets.strip.split.map(&:to_i)\na = a.chunk{ |i| i <= t }.to_a.map{|t, a| a if t}.compact\nsum = a.inject(0){|s, arr| s + ([arr.count - c + 1, 0].max)}\nputs sum\n"}, {"source_code": "n,t,c=gets.split.map &:to_i\np=gets.split.map &:to_i\nw,r=0,0\np.each{|l|r=l>t ?0:r+1;r>=c&&w+=1}\nputs w\n"}, {"source_code": "n, t, c = gets.split(' ').map(&:to_i)\narr = gets.split(' ').map(&:to_i)\nlast = -1\npossible = 0\n(0..n-1).each do |index|\n if arr[index] > t\n last = index\n elsif index - last >= c\n possible += 1\n end\nend\nputs possible"}, {"source_code": "n,t,c=gets.split.map &:to_i\np=gets.split.map &:to_i\nw,r=0,0\np.each{|l|r=l>t ?0:r+1;r>=c&&w+=1}\nputs w\n"}, {"source_code": "n,t,c=gets.split.map &:to_i\np=gets.split.map &:to_i\nw,r=0,0\np.each{|l|r=l>t ?0:r+1;r>=c&&w+=1}\nputs w\n"}, {"source_code": "n,t,c=gets.split.map &:to_i\np=gets.split.map &:to_i\nw,r=0,0\np.each{|l|r=l>t ?0:r+1;r>=c&&w+=1}\nputs w"}, {"source_code": "n,t,c=gets.split.map &:to_i\np=gets.split.map &:to_i\nw,r=0,0\np.each{|l|r=l>t ?0:r+1;r>=c&&w+=1}\nputs w\n"}, {"source_code": "n, t, c = gets.split.map &:to_i\ns = gets.split.map &:to_i\ns.push t + 1\nl = 0\nans = 0\n(n+1).times do |i|\n if s[i] > t then\n if i - l >= c then\n ans += i - l + 1 - c\n end\n l = i + 1\n end\nend\nputs ans\n"}, {"source_code": "n, c, t = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\n\nx = 0\ny = t\n\nsev = 0\ncount = 0\n\nfor i in 0...t\n if a[i] > c\n sev += 1\n end\nend\n\nif sev == 0\n count += 1\nend\n\nfor i in 0...n-t\n if a[x+i] > c\n sev -= 1\n end\n if a[y+i] > c\n sev += 1\n end\n if sev == 0\n count += 1\n end\nend\n\nprint count\n"}, {"source_code": "n,t,c=gets.split.map &:to_i\np=gets.split.map &:to_i\nw,r=0,0\np.each{|l|r=l>t ?0:r+1;r>=c&&w+=1}\nputs w\n"}, {"source_code": "n,t,c=gets.split.map &:to_i\np=gets.split.map &:to_i\nw,r=0,0\np.each{|l|r=l>t ?0:r+1;r>=c&&w+=1}\nputs w"}, {"source_code": "n, t, c = gets.strip.split(\" \").map(&:to_i)\na = gets.strip.split(\" \").map(&:to_i)\ni = 0\nnum = 0\ncount = 0\nwhile i < n do \n if a[i] <= t then\n num += 1 \n elsif num >= c then\n count += num - c + 1\n num = 0\n else\n num = 0\n end\n i += 1\nend \nif num >= c then\n count += num - c + 1\nend\nputs count"}, {"source_code": "n,t,c=gets.split.map &:to_i\np=gets.split.map &:to_i\nw,r=0,0\np.each{|l|r=l>t ?0:r+1;r>=c&&w+=1}\nputs w\n"}, {"source_code": "n,t,c=gets.split.map &:to_i\np=gets.split.map &:to_i\nw,r=0,0\np.each{|l|r=l>t ?0:r+1;r>=c&&w+=1}\nputs w\n"}, {"source_code": "n,t,c = gets.split(' ').map(&:to_i)\np = gets.split(' ').map(&:to_i)\nl,ans=0,0\np.each do |i|\n if i > t\n l=0\n else\n l+=1\n end\n if l>=c\n ans+=1\n end\n end\n puts ans"}, {"source_code": "n,t,c = gets.split(' ').map(&:to_i)\np = gets.split(' ').map(&:to_i)\nl, ans=0, 0\np.each do |i|\n if i > t\n l=0\n else\n l+=1\n end\n if l>=c\n ans+=1\n end\n end\n puts ans"}, {"source_code": "n, t, c = gets.split\nn = Integer n\nt = Integer t\nc = Integer c\n\nnums = gets.split\nfor i in 0...n\n nums[i] = Integer nums[i]\nend\n\n\n# how many t have we found ?\ncount_t = 0\n# the answer/count\ncount = 0\n\nfor i in 0...n\n if nums[i] > t\n count_t += 1\n end\n \n if i >= c && nums[i-c] > t\n count_t -= 1\n end\n \n if i >= c - 1 && count_t == 0\n count += 1\n end\nend\n\nputs count\n "}, {"source_code": "n, t, c = gets.split\nn = Integer n\nt = Integer t\nc = Integer c\n\nnums = gets.split\nfor i in 0...n\n nums[i] = Integer nums[i]\nend\nnums[n] = t + 1\n\n\n# what's the position of last number > t found?\nlast_t_found = -1\n# the answer/count\ncount = 0\n\nfor i in 0..n\n if nums[i] > t\n if i - last_t_found > c\n count += i - last_t_found - c\n end\n last_t_found = i\n end\nend\n\nputs count\n "}, {"source_code": "n, t, c = gets.split\nn = Integer n\nt = Integer t\nc = Integer c\n\nnums = gets.split\nfor i in 0...n\n nums[i] = Integer nums[i]\nend\n\n\n# what's the position of last number > t found?\nlast_t_found = -1\n# what's the last position we've checked?\nlast_checked = -1\n# the answer/count\ncount = 0\n \n# loop through all possible i\nfor i in 0..n-c\n # if no t appears after i by now\n if i > last_t_found\n # check all the number between i and i+c\n for j in last_checked+1...i+c\n # find a t after i\n if nums[j] > t\n # record what we've seen\n last_t_found = j\n last_checked = j\n break\n end\n end\n \n # no t found after i?\n if last_t_found < i\n last_checked = i + c - 1\n count += 1\n end\n end\nend\n\nputs count\n "}, {"source_code": "n,t,c=gets.split.map &:to_i\np=gets.split.map &:to_i\nw,r=0,0\np.each{|l|r=l>t ?0:r+1;r>=c&&w+=1}\nputs w\n"}, {"source_code": "n,t,c = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\n\nans = 0\ncnt = a[0...c-1].count { |p| p > t }\na[c-1...n].each_with_index { |p, idx|\n cnt += 1 if p > t\n ans += 1 if cnt == 0\n cnt -= 1 if a[idx] > t\n}\n\nputs ans"}, {"source_code": "# Read input\nn, t, c = gets.strip.split.map(&:to_i)\na = gets.strip.split.map(&:to_i)\n\n# Grab the locations of elements > t \nspoilerloc = a.each_index.select{|i| a[i] > t}.unshift(-1).push(n)\n\n# Spot dead segments\nrsum = 0\n\nfor i in 1..(spoilerloc.length - 1) do\n m = spoilerloc[i] - spoilerloc[i-1] - 1\n rsum = rsum + [m - c + 1, 0].max\nend\n\nputs rsum"}, {"source_code": "def num_ways(n, k)\n count = 1\n for i in 0...(n-k)\n count += 1\n end\n count\nend\n\ndata = gets.split.map(&:to_i)\nn = data[0]\nt = data[1]\nc = data[2]\narr = gets.split.map(&:to_i)\naarr = []\nsub = []\nfor i in 0...n\n if arr[i] <= t\n sub << arr[i]\n else\n aarr << sub\n sub = []\n end\nend\n\naarr << sub\n\ncount = 0\nfor arr in aarr do\n if arr.length < c\n next\n end\n count += num_ways(arr.length, c)\nend\n\nputs count\n"}], "negative_code": [{"source_code": "n,t,c = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\n\nans = 0\ncnt = a[0...c-1].count { |p| p > t }\na[c-1...n].each_with_index { |p, idx|\n cnt += 1 if p > t\n ans += 1 if cnt == 0\n cnt -= 1 if a[idx-c+1] > t\n}\n\nputs ans"}, {"source_code": "n,t,c = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\n\nans = 0\ncnt = a[0...c-1].count { |p| p >= t }\na[c-1...n].each_with_index { |p, idx|\n cnt += 1 if p >= t\n ans += 1 if cnt == 0\n cnt -= 1 if a[idx-c+1] >= t\n}\n\nputs ans"}, {"source_code": "# Read input\nn, t, c = gets.strip.split.map(&:to_i)\na = gets.strip.split.map(&:to_i)\n\n# Grab the locations of elements > t \nspoilerloc = a.each_index.select{|i| a[i] > t}\n\n# Create mask\nmask = (-1 * c + 1 .. c - 1).to_a - [0]\nmask = [0] if c == 1\n\n# Spot dead segments\nout = []\nfor i in spoilerloc\n masked = mask.map{|j| j + i}\n out = out | masked\nend\n\nputs n - c + 1 - out.length"}], "src_uid": "7d1e8769a6b1d5c6680ab530d19e7fa4"} {"nl": {"description": "Michael and Joe are playing a game. The game is played on a grid with $$$n$$$ rows and $$$m$$$ columns, filled with distinct integers. We denote the square on the $$$i$$$-th ($$$1\\le i\\le n$$$) row and $$$j$$$-th ($$$1\\le j\\le m$$$) column by $$$(i, j)$$$ and the number there by $$$a_{ij}$$$.Michael starts by saying two numbers $$$h$$$ ($$$1\\le h \\le n$$$) and $$$w$$$ ($$$1\\le w \\le m$$$). Then Joe picks any $$$h\\times w$$$ subrectangle of the board (without Michael seeing).Formally, an $$$h\\times w$$$ subrectangle starts at some square $$$(a,b)$$$ where $$$1 \\le a \\le n-h+1$$$ and $$$1 \\le b \\le m-w+1$$$. It contains all squares $$$(i,j)$$$ for $$$a \\le i \\le a+h-1$$$ and $$$b \\le j \\le b+w-1$$$. Possible move by Joe if Michael says $$$3\\times 2$$$ (with maximum of $$$15$$$). Finally, Michael has to guess the maximum number in the subrectangle. He wins if he gets it right.Because Michael doesn't like big numbers, he wants the area of the chosen subrectangle (that is, $$$h \\cdot w$$$), to be as small as possible, while still ensuring that he wins, not depending on Joe's choice. Help Michael out by finding this minimum possible area. It can be shown that Michael can always choose $$$h, w$$$ for which he can ensure that he wins.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 20$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n, m \\le 40$$$)  — the size of the grid. Each of the following $$$n$$$ lines contains $$$m$$$ integers. The $$$j$$$-th integer on the $$$i$$$-th line is $$$a_{ij}$$$ ($$$-10^9 \\le a_{ij} \\le 10^9$$$)  — the element in the cell $$$(i, j)$$$. It is guaranteed that all the numbers are distinct (that is, if $$$a_{i_1j_1} = a_{i_2j_2}$$$, then $$$i_1 = i_2, j_1 = j_2$$$).", "output_spec": "For each test case print a single positive integer  — the minimum possible area the subrectangle can have while still ensuring that Michael can guarantee the victory.", "sample_inputs": ["3\n\n1 1\n\n3\n\n4 4\n\n2 12 6 10\n\n3 15 16 4\n\n1 13 8 11\n\n14 7 9 5\n\n2 3\n\n-7 5 2\n\n0 8 -3"], "sample_outputs": ["1\n9\n4"], "notes": "NoteIn the first test case, the grid is $$$1\\times 1$$$, so the only possible choice for $$$h, w$$$ is $$$h = 1, w = 1$$$, giving an area of $$$h\\cdot w = 1$$$.The grid from the second test case is drawn in the statement. It can be shown that with $$$h = 3, w = 3$$$ Michael can guarantee the victory and that any choice with $$$h\\cdot w \\le 8$$$ doesn't."}, "positive_code": [{"source_code": "# gets.to_i\r\n# gets.split.map(&:to_i)\r\n\r\ngets.to_i.times do\r\n n, m = gets.split.map(&:to_i)\r\n a = Array.new(n){gets.split.map(&:to_i)}\r\n x = 0\r\n y = 0\r\n n.times do |i|\r\n m.times do |j|\r\n if a[y][x] < a[i][j]\r\n x = j\r\n y = i\r\n end\r\n end\r\n end\r\n\r\n x = [x+1, m-x].max\r\n y = [y+1, n-y].max\r\n puts x*y\r\nend\r\n"}, {"source_code": "t = gets.to_i\nt.times {\n n, m = gets.split.map &:to_i\n g = []\n for i in (1..n)\n g += [gets.split.map(&:to_i)]\n end\n big = -2000000000\n big_i = -1\n big_j = -1\n for i in (0...n)\n for j in (0...m)\n if g[i][j] > big\n big = g[i][j]\n big_i = i\n big_j = j\n end\n end\n end\n h = [big_i + 1, n - big_i].max\n w = [big_j + 1, m - big_j].max\n puts h * w\n}\n"}], "negative_code": [{"source_code": "# gets.to_i\r\n# gets.split.map(&:to_i)\r\n\r\ngets.to_i.times do\r\n n, m = gets.split.map(&:to_i)\r\n a = Array.new(n){gets.split.map(&:to_i)}\r\n x = 0\r\n y = 0\r\n n.times do |i|\r\n m.times do |j|\r\n if a[y][x] < a[i][j]\r\n x = j\r\n y = i\r\n end\r\n end\r\n end\r\n\r\n x = [x+1, n-x].max\r\n y = [y+1, m-y].max\r\n puts x*y\r\nend\r\n\r\n"}, {"source_code": "# gets.to_i\r\n# gets.split.map(&:to_i)\r\n\r\ngets.to_i.times do\r\n n, m = gets.split.map(&:to_i)\r\n a = Array.new(n){gets.split.map(&:to_i)}\r\n max_n = 0\r\n x = 0\r\n y = 0\r\n n.times do |i|\r\n m.times do |j|\r\n if max_n < a[i][j]\r\n max_n = a[i][j]\r\n x = j+1\r\n y = i+1\r\n end\r\n end\r\n end\r\n x = [x, n-x+1].max\r\n y = [y, m-y+1].max\r\n puts x*y\r\nend\r\n"}, {"source_code": "# gets.to_i\r\n# gets.split.map(&:to_i)\r\n\r\ngets.to_i.times do\r\n n, m = gets.split.map(&:to_i)\r\n a = Array.new(n){gets.split.map(&:to_i)}\r\n max_n = 0\r\n x = 0\r\n y = 0\r\n n.times do |i|\r\n m.times do |j|\r\n if max_n < a[i][j]\r\n max_n = a[i][j]\r\n x = j+1\r\n y = i+1\r\n end\r\n end\r\n end\r\n puts [x*y, (m-x+1)*y, x*(n-y+1), (m-x+1)*(n-y+1)].max\r\nend\r\n"}], "src_uid": "47e5ccd8220afa84c95f36b08ed1817a"} {"nl": {"description": "$$$n$$$ towns are arranged in a circle sequentially. The towns are numbered from $$$1$$$ to $$$n$$$ in clockwise order. In the $$$i$$$-th town, there lives a singer with a repertoire of $$$a_i$$$ minutes for each $$$i \\in [1, n]$$$.Each singer visited all $$$n$$$ towns in clockwise order, starting with the town he lives in, and gave exactly one concert in each town. In addition, in each town, the $$$i$$$-th singer got inspired and came up with a song that lasts $$$a_i$$$ minutes. The song was added to his repertoire so that he could perform it in the rest of the cities.Hence, for the $$$i$$$-th singer, the concert in the $$$i$$$-th town will last $$$a_i$$$ minutes, in the $$$(i + 1)$$$-th town the concert will last $$$2 \\cdot a_i$$$ minutes, ..., in the $$$((i + k) \\bmod n + 1)$$$-th town the duration of the concert will be $$$(k + 2) \\cdot a_i$$$, ..., in the town $$$((i + n - 2) \\bmod n + 1)$$$ — $$$n \\cdot a_i$$$ minutes.You are given an array of $$$b$$$ integer numbers, where $$$b_i$$$ is the total duration of concerts in the $$$i$$$-th town. Reconstruct any correct sequence of positive integers $$$a$$$ or say that it is impossible.", "input_spec": "The first line contains one integer $$$t$$$ $$$(1 \\le t \\le 10^3$$$) — the number of test cases. Then the test cases follow. Each test case consists of two lines. The first line contains a single integer $$$n$$$ ($$$1 \\le n \\le 4 \\cdot 10^4$$$) — the number of cities. The second line contains $$$n$$$ integers $$$b_1, b_2, \\dots, b_n$$$ ($$$1 \\le b_i \\le 10^{9}$$$) — the total duration of concerts in $$$i$$$-th city. The sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, print the answer as follows: If there is no suitable sequence $$$a$$$, print NO. Otherwise, on the first line print YES, on the next line print the sequence $$$a_1, a_2, \\dots, a_n$$$ of $$$n$$$ integers, where $$$a_i$$$ ($$$1 \\le a_i \\le 10^{9}$$$) is the initial duration of repertoire of the $$$i$$$-th singer. If there are multiple answers, print any of them.", "sample_inputs": ["4\n3\n12 16 14\n1\n1\n3\n1 2 3\n6\n81 75 75 93 93 87"], "sample_outputs": ["YES\n3 1 3 \nYES\n1 \nNO\nYES\n5 5 4 1 4 5"], "notes": "NoteLet's consider the $$$1$$$-st test case of the example: the $$$1$$$-st singer in the $$$1$$$-st city will give a concert for $$$3$$$ minutes, in the $$$2$$$-nd — for $$$6$$$ minutes, in the $$$3$$$-rd — for $$$9$$$ minutes; the $$$2$$$-nd singer in the $$$1$$$-st city will give a concert for $$$3$$$ minutes, in the $$$2$$$-nd — for $$$1$$$ minute, in the $$$3$$$-rd - for $$$2$$$ minutes; the $$$3$$$-rd singer in the $$$1$$$-st city will give a concert for $$$6$$$ minutes, in the $$$2$$$-nd — for $$$9$$$ minutes, in the $$$3$$$-rd — for $$$3$$$ minutes. "}, "positive_code": [{"source_code": "t = gets.to_i\r\nans = []\r\nt.times do\r\n n = gets.to_i\r\n b = gets.chomp.split(\" \").map(&:to_i)\r\n x = b.sum\r\n if x % (n * (n+1) / 2) != 0\r\n ans << [\"NO\"]\r\n next\r\n end\r\n a = []\r\n x /= (n * (n+1) / 2)\r\n f = true\r\n n.times do |i|\r\n f = false if (x - (b[i%n] - b[(i-1)%n])) % n != 0\r\n v = (x - (b[i%n] - b[(i-1)%n])) / n\r\n f = false if v <= 0\r\n a << v\r\n end\r\n if f\r\n ans << [\"YES\"]\r\n ans << a\r\n else\r\n ans << [\"NO\"]\r\n end\r\nend\r\nans.each do |arr|\r\n puts arr.join(\" \")\r\nend"}], "negative_code": [{"source_code": "t = gets.to_i\r\nans = []\r\nt.times do\r\n n = gets.to_i\r\n b = gets.chomp.split(\" \").map(&:to_i)\r\n x = b.sum\r\n if x % (n * (n+1) / 2) != 0\r\n ans << [\"NO\"]\r\n next\r\n end\r\n a = []\r\n x /= (n * (n+1) / 2)\r\n f = true\r\n n.times do |i|\r\n v = (x - (b[i%n] - b[(i-1)%n])) / n\r\n f = false if v <= 0\r\n a << v\r\n end\r\n if f\r\n ans << [\"YES\"]\r\n ans << a\r\n else\r\n ans << [\"NO\"]\r\n end\r\nend\r\nans.each do |arr|\r\n puts arr.join(\" \")\r\nend"}, {"source_code": "t = gets.to_i\r\nans = []\r\nt.times do\r\n n = gets.to_i\r\n b = gets.chomp.split(\" \").map(&:to_i)\r\n x = b.sum\r\n if x % (n * (n+1) / 2) != 0\r\n ans << [\"NO\"]\r\n next\r\n end\r\n a = []\r\n x /= (n * (n+1) / 2)\r\n f = true\r\n n.times do |i|\r\n f = false if (x - (b[i%n] - b[(i-1)%n])) % n != 0\r\n v = (x - (b[i%n] - b[(i-1)%n])) / n\r\n f = false if v == 0\r\n a << v\r\n end\r\n if f\r\n ans << [\"YES\"]\r\n ans << a\r\n else\r\n ans << [\"NO\"]\r\n end\r\nend\r\nans.each do |arr|\r\n puts arr.join(\" \")\r\nend"}, {"source_code": "t = gets.to_i\r\nans = []\r\nt.times do\r\n n = gets.to_i\r\n b = gets.chomp.split(\" \").map(&:to_i)\r\n x = b.sum\r\n if x % (n * (n+1) / 2) != 0\r\n ans << [\"NO\"]\r\n next\r\n end\r\n a = []\r\n x /= (n * (n+1) / 2)\r\n f = true\r\n n.times do |i|\r\n v = (x - (b[i%n] - b[(i-1)%n])) / n\r\n f = false if v == 0\r\n a << v\r\n end\r\n if f\r\n ans << [\"YES\"]\r\n ans << a\r\n else\r\n ans << [\"NO\"]\r\n end\r\nend\r\nans.each do |arr|\r\n puts arr.join(\" \")\r\nend"}], "src_uid": "21fed74be8462143d77bbbee48dc8a12"} {"nl": {"description": "Fedya studies in a gymnasium. Fedya's maths hometask is to calculate the following expression:(1n + 2n + 3n + 4n) mod 5for given value of n. Fedya managed to complete the task. Can you? Note that given number n can be extremely large (e.g. it can exceed any integer type of your programming language).", "input_spec": "The single line contains a single integer n (0 ≤ n ≤ 10105). The number doesn't contain any leading zeroes.", "output_spec": "Print the value of the expression without leading zeros.", "sample_inputs": ["4", "124356983594583453458888889"], "sample_outputs": ["4", "0"], "notes": "NoteOperation x mod y means taking remainder after division x by y.Note to the first sample:"}, "positive_code": [{"source_code": "num = gets.chomp \n\ncount = 0\ncount += num[-1].to_i + num[-2].to_i*10 \n\n\nif count%4==0 \n puts \"4\" \nelse \n puts \"0\"\nend "}, {"source_code": "n = gets.chomp\nputs (n.length > 2 ? n[-2..-1] : n).to_i % 4 == 0 ? 4 : 0"}, {"source_code": "sum=0\nf=gets.chomp\nf=(f[f.length-2]+f[f.length-1]).to_i\nif f%4==3\nans=(1+3+2+4)%5\nputs \"#{ans}\"\nelsif f%4==0\nans=(1+1+1+1)%5\nputs \"#{ans}\"\nelsif f%4==2\nans=(1+4+4+1)%5\nputs \"#{ans}\"\nelse\nans=(1+2+3+4)%5\nputs \"#{ans}\"\nend"}, {"source_code": "n2=[2,4,8,16]\nn3=[3,9,27,81]\nn4=[4,16]\nt=gets.chomp\nn=0\nif t.size<5 then\n\tn=(t.to_i-1)\nelse\n\tt1=t.size-1\n\tt2=\"\"\n\tt2<<(t[t1-2]+t[t1-1]+t[t1])\n\tn=t2.to_i-1\nend\nputs (1+n2[n%4]+n3[n%4]+n4[n%2])%5\n\n"}, {"source_code": "n = gets.to_s\nif n.length > 3\n n = n[-3..-1]\nend\nn = n.to_i\nif n % 4 == 0\n p 4\nelse\n p 0\nend\n"}, {"source_code": "n = gets.chomp\nn = n[-2..-1] if n.size > 2\nn = n.to_i % 4\na2 = [6, 2, 4, 8]\na3 = [1, 3, 9, 7]\na4 = [6, 4, 6, 4]\nputs (1 + a2[n] + a3[n] + a4[n]) % 5\n"}, {"source_code": "n = gets.chomp.chars.map(&:to_i)\nn = n[-2..-1] if n.size >= 2\ni = n.join.to_i % 4\na2 = [6, 2, 4, 8]\na3 = [1, 3, 9, 7]\na4 = [6, 4]\nputs (1 + a2[i] + a3[i] + a4[i % 2]) % 5\n"}, {"source_code": "# Read Input\ns = gets.strip\n\n# Take the last two digits\nn = s.length > 2 ? s[-2..-1].to_i : s.to_i\n\n# Return result\nputs (n % 4) == 0 ? 4 : 0"}, {"source_code": "s = gets.chomp\ns = s[-2..-1] if s.size > 1\nputs [4, 0, 0, 0][s.to_i % 4]"}, {"source_code": "n_str=gets.chomp\nn_100 = (n_str.length > 3 ? n_str[-3..-1] : n_str).to_i\nputs n_100 % 4 == 0 ? \"4\" : \"0\"\n\n"}, {"source_code": "n = ('00' + gets.chomp)[-2, 2].to_i\n\nif n % 4 == 0\n\tputs 4\nelse\n\tputs 0\nend"}], "negative_code": [{"source_code": "n = gets\nputs (n.length > 2 ? n[-2, -1].to_i : n.to_i) % 4 == 0 ? 4 : 0"}, {"source_code": "sum=0\nf=gets.chomp\nf.each_char do |i|\nsum+=i.to_i\nend\nif(sum%4==0)\nans=(1+1+1+1)%5\nputs \"#{ans}\"\nelsif(sum%4==1)\nans=(1+2+3+4)%5\nputs \"#{ans}\"\nelsif(sum%4==2)\nans=(1+4+4+1)%5\nputs \"#{ans}\"\nelsif(sum%4==3)\nans=(1+3+3+4)%5\nputs \"#{ans}\"\nend"}, {"source_code": "sum=0\nf=gets.chomp\nf.each_char do |i|\nsum+=i.to_i\nend\n\nif((f[f.length-1]+f[f.length-2]).to_i)%4==3\nans=(1+3+3+4)%5\nputs \"#{ans}\"\nelsif((f[f.length-1]+f[f.length-2]).to_i)%4==0\nans=(1+1+1+1)%5\nputs \"#{ans}\"\nelsif((f[f.length-1]+f[f.length-2]).to_i)%4==2\nans=(1+4+4+1)%5\nputs \"#{ans}\"\nelse\nans=(1+2+3+4)%5\nputs \"#{ans}\"\nend"}, {"source_code": "sum=0\nf=gets.chomp\nf=(f[f.length-2]+f[f.length-1]).to_i\nif f%4==3\nans=(1+3+3+4)%5\nputs \"#{ans}\"\nelsif f%4==0\nans=(1+1+1+1)%5\nputs \"#{ans}\"\nelsif f%4==2\nans=(1+4+4+1)%5\nputs \"#{ans}\"\nelse\nans=(1+2+3+4)%5\nputs \"#{ans}\"\nend"}, {"source_code": "n = gets.chomp.chars.map(&:to_i)\nn = n.inject(:+)\na2 = [6, 2, 4, 8]\na3 = [1, 3, 9, 7]\na4 = [6, 4]\nans = 1\nn %= 4\nputs (ans + a2[n] + a3[n] + a4[n % 2]) % 5\n"}, {"source_code": "\nn = gets.strip.to_i\n\na = 5 ** (n * Math.log(2, 5)).modulo(1).floor\nb = 5 ** (n * Math.log(3, 5)).modulo(1).floor\nc = 5 ** (n * Math.log(4, 5)).modulo(1).floor\n\nputs (1 + (a % 5) + (b % 5) + (c % 5)) % 5"}, {"source_code": "# Read Input\ns = gets.strip\n\n# Take the last two digits\nn = s.length > 2 ? s[-2..-1].to_i : n.to_i\n\n# Return result\nputs (n % 4) == 0 ? 4 : 0"}, {"source_code": "n = ('00' + gets)[-2, 2].to_i\n\nif n % 4 == 0\n\tputs 4\nelse\n\tputs 0\nend"}], "src_uid": "74cbcbafbffc363137a02697952a8539"} {"nl": {"description": "Polycarp has $$$26$$$ tasks. Each task is designated by a capital letter of the Latin alphabet.The teacher asked Polycarp to solve tasks in the following way: if Polycarp began to solve some task, then he must solve it to the end, without being distracted by another task. After switching to another task, Polycarp cannot return to the previous task.Polycarp can only solve one task during the day. Every day he wrote down what task he solved. Now the teacher wants to know if Polycarp followed his advice.For example, if Polycarp solved tasks in the following order: \"DDBBCCCBBEZ\", then the teacher will see that on the third day Polycarp began to solve the task 'B', then on the fifth day he got distracted and began to solve the task 'C', on the eighth day Polycarp returned to the task 'B'. Other examples of when the teacher is suspicious: \"BAB\", \"AABBCCDDEEBZZ\" and \"AAAAZAAAAA\".If Polycarp solved the tasks as follows: \"FFGZZZY\", then the teacher cannot have any suspicions. Please note that Polycarp is not obligated to solve all tasks. Other examples of when the teacher doesn't have any suspicious: \"BA\", \"AFFFCC\" and \"YYYYY\".Help Polycarp find out if his teacher might be suspicious.", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 1000$$$). Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$1 \\le n \\le 50$$$) — the number of days during which Polycarp solved tasks. The second line contains a string of length $$$n$$$, consisting of uppercase Latin letters, which is the order in which Polycarp solved the tasks.", "output_spec": "For each test case output: \"YES\", if the teacher cannot be suspicious; \"NO\", otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).", "sample_inputs": ["5\n3\nABA\n11\nDDBBCCCBBEZ\n7\nFFGZZZY\n1\nZ\n2\nAB"], "sample_outputs": ["NO\nNO\nYES\nYES\nYES"], "notes": null}, "positive_code": [{"source_code": "def destructed(str, n)\n mp = {}\n str.chars.each_with_index do |v, i|\n if mp.key?(str[i])\n mp[v] = mp[v] << i\n else\n mp[v] = [i]\n end\n end\n # found = true\n mp.each_pair do |k, v|\n len = v.length\n (0..len-2).each do |i|\n if v[i+1] - v[i] != 1\n return false\n end\n end\n end\n return true\nend\n\nt = gets.to_i\ninputs = []\n(0...t).each do\n n = gets.to_i\n str = gets.chomp\n inputs<<[str, n]\nend\n\ninputs.each do |input|\n str = input[0]\n n = input[1]\n res = destructed(str, n)\n if res\n puts \"YES\"\n else\n puts \"NO\"\n end\nend"}, {"source_code": "gets.to_i.times {\n gets\n s = gets.strip\n ('A'..'Z').each { s.gsub!(/(#{_1})+/, _1) }\n h = Hash.new 0\n s.each_char { h[_1] += 1 }\n ans = h.values.any? { _1 > 1 }\n puts(ans ? \"NO\" : \"YES\")\n}\n"}, {"source_code": "results = []\n\nt = gets.chomp\n(1..t.to_i).each do |iteration|\n\tn = gets.chomp.to_i\n\ttasks = gets.chomp.split('')\n\tno_f = true\n\ttasks.each_with_index do |let, idx|\n\t\tnext if let == tasks[idx+1]\n\n\t\tif tasks[idx+1..-1].include? let\n\t\t\tresults.push(\"NO\")\n\t\t\tno_f = false\n\t\t\tbreak\n\t\tend\n\tend\n\tresults.push(\"YES\") if no_f\nend\nresults.each do |res|\n\tputs res\nend"}, {"source_code": "t = gets.chomp.to_i\r\n\r\nwhile t != 0\r\n\r\n n = gets.chomp.to_i\r\n str = gets.chomp\r\n\r\n mp = {} # map of indices\r\n for i in 0...n\r\n mp[ str[i] ] = i\r\n end\r\n\r\n y = true\r\n for i in 0...n\r\n\r\n for j in i...mp[str[i]]\r\n if str[i] != str[j]\r\n y = false\r\n break\r\n end\r\n end\r\n\r\n if y == false\r\n break\r\n end\r\n end\r\n\r\n if y == true\r\n puts(\"YES\")\r\n else\r\n puts(\"NO\")\r\n end\r\n\r\n t = t-1\r\nend"}, {"source_code": "require 'set'\n\nt = gets.chomp.to_i\n\ndef cannot_be_sus\n _ = gets\n # @type [String]\n input = gets.chomp\n\n last_seen_char = nil\n seen = Set.new\n input.each_char do |c|\n if c == last_seen_char\n # do nothing\n elsif seen.include? c\n return false\n else\n seen.add c\n end\n last_seen_char = c\n end\n true\nend\n\n(1..t).each do\n cannot_be_sus ? (puts 'YES') : (puts 'NO')\nend\n"}, {"source_code": "t = gets.to_s.to_i\n1.upto(t) do |casenum|\n n = gets.to_s.to_i\n a = gets.to_s.strip.split(\"\")\n seen = {}\n seen[a[0]] = true\n prev = a[0]\n bad = false\n 1.upto(n - 1) do |i|\n next if prev == a[i]\n if seen[a[i]]\n bad = true\n break\n end\n seen[a[i]] = true\n prev = a[i]\n end\n printf(\"%s\\n\", bad ? \"NO\" : \"YES\")\nend\n\n"}], "negative_code": [], "src_uid": "3851854541b4bd168f5cb1e8492ed8ef"} {"nl": {"description": "Polycarp is a big lover of killing time in social networks. A page with a chatlist in his favourite network is made so that when a message is sent to some friend, his friend's chat rises to the very top of the page. The relative order of the other chats doesn't change. If there was no chat with this friend before, then a new chat is simply inserted to the top of the list.Assuming that the chat list is initially empty, given the sequence of Polycaprus' messages make a list of chats after all of his messages are processed. Assume that no friend wrote any message to Polycarpus.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 200 000) — the number of Polycarpus' messages. Next n lines enlist the message recipients in the order in which the messages were sent. The name of each participant is a non-empty sequence of lowercase English letters of length at most 10.", "output_spec": "Print all the recipients to who Polycarp talked to in the order of chats with them, from top to bottom.", "sample_inputs": ["4\nalex\nivan\nroman\nivan", "8\nalina\nmaria\nekaterina\ndarya\ndarya\nekaterina\nmaria\nalina"], "sample_outputs": ["ivan\nroman\nalex", "alina\nmaria\nekaterina\ndarya"], "notes": "NoteIn the first test case Polycarpus first writes to friend by name \"alex\", and the list looks as follows: alex Then Polycarpus writes to friend by name \"ivan\" and the list looks as follows: ivan alex Polycarpus writes the third message to friend by name \"roman\" and the list looks as follows: roman ivan alex Polycarpus writes the fourth message to friend by name \"ivan\", to who he has already sent a message, so the list of chats changes as follows: ivan roman alex "}, "positive_code": [{"source_code": "gets;puts $<.map(&:chomp).reverse.uniq"}, {"source_code": "gets;puts $<.map(&:chomp).reverse.uniq\n"}, {"source_code": "require 'set'\nn = gets.to_i\nchats = []\nn.times do\n chats.push(gets.chomp)\nend\ns = Set.new\nchats.reverse.each do |name|\n if !s.include?(name)\n puts name\n s << name\n end\nend\n"}], "negative_code": [], "src_uid": "18f4d9262150294b63d99803250ed49c"} {"nl": {"description": "There are $$$n$$$ logs, the $$$i$$$-th log has a length of $$$a_i$$$ meters. Since chopping logs is tiring work, errorgorn and maomao90 have decided to play a game.errorgorn and maomao90 will take turns chopping the logs with errorgorn chopping first. On his turn, the player will pick a log and chop it into $$$2$$$ pieces. If the length of the chosen log is $$$x$$$, and the lengths of the resulting pieces are $$$y$$$ and $$$z$$$, then $$$y$$$ and $$$z$$$ have to be positive integers, and $$$x=y+z$$$ must hold. For example, you can chop a log of length $$$3$$$ into logs of lengths $$$2$$$ and $$$1$$$, but not into logs of lengths $$$3$$$ and $$$0$$$, $$$2$$$ and $$$2$$$, or $$$1.5$$$ and $$$1.5$$$.The player who is unable to make a chop will be the loser. Assuming that both errorgorn and maomao90 play optimally, who will be the winner?", "input_spec": "Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 100$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 50$$$)  — the number of logs. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2, \\ldots, a_n$$$ ($$$1 \\leq a_i \\leq 50$$$)  — the lengths of the logs. Note that there is no bound on the sum of $$$n$$$ over all test cases.", "output_spec": "For each test case, print \"errorgorn\" if errorgorn wins or \"maomao90\" if maomao90 wins. (Output without quotes).", "sample_inputs": ["2\n\n4\n\n2 4 2 1\n\n1\n\n1"], "sample_outputs": ["errorgorn\nmaomao90"], "notes": "NoteIn the first test case, errorgorn will be the winner. An optimal move is to chop the log of length $$$4$$$ into $$$2$$$ logs of length $$$2$$$. After this there will only be $$$4$$$ logs of length $$$2$$$ and $$$1$$$ log of length $$$1$$$.After this, the only move any player can do is to chop any log of length $$$2$$$ into $$$2$$$ logs of length $$$1$$$. After $$$4$$$ moves, it will be maomao90's turn and he will not be able to make a move. Therefore errorgorn will be the winner.In the second test case, errorgorn will not be able to make a move on his first turn and will immediately lose, making maomao90 the winner."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n gets\r\n xs = gets.split.map(&:to_i)\r\n puts xs.map { |x| x - 1 }.sum.even? ? \"maomao90\" : \"errorgorn\"\r\nend\r\n"}, {"source_code": "gets.to_i.times do\r\n puts ['maomao90', 'errorgorn'][(gets.to_i+gets.chomp.split.map(&:to_i).inject(:+))&1]\r\nend"}], "negative_code": [], "src_uid": "fc75935b149cf9f4f2ddb9e2ac01d1c2"} {"nl": {"description": "Nikolay lives in a two-storied house. There are $$$n$$$ rooms on each floor, arranged in a row and numbered from one from left to right. So each room can be represented by the number of the floor and the number of the room on this floor (room number is an integer between $$$1$$$ and $$$n$$$). If Nikolay is currently in some room, he can move to any of the neighbouring rooms (if they exist). Rooms with numbers $$$i$$$ and $$$i+1$$$ on each floor are neighbouring, for all $$$1 \\leq i \\leq n - 1$$$. There may also be staircases that connect two rooms from different floors having the same numbers. If there is a staircase connecting the room $$$x$$$ on the first floor and the room $$$x$$$ on the second floor, then Nikolay can use it to move from one room to another. The picture illustrates a house with $$$n = 4$$$. There is a staircase between the room $$$2$$$ on the first floor and the room $$$2$$$ on the second floor, and another staircase between the room $$$4$$$ on the first floor and the room $$$4$$$ on the second floor. The arrows denote possible directions in which Nikolay can move. The picture corresponds to the string \"0101\" in the input. Nikolay wants to move through some rooms in his house. To do this, he firstly chooses any room where he starts. Then Nikolay moves between rooms according to the aforementioned rules. Nikolay never visits the same room twice (he won't enter a room where he has already been). Calculate the maximum number of rooms Nikolay can visit during his tour, if: he can start in any room on any floor of his choice, and he won't visit the same room twice. ", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases in the input. Then test cases follow. Each test case consists of two lines. The first line contains one integer $$$n$$$ $$$(1 \\le n \\le 1\\,000)$$$ — the number of rooms on each floor. The second line contains one string consisting of $$$n$$$ characters, each character is either a '0' or a '1'. If the $$$i$$$-th character is a '1', then there is a staircase between the room $$$i$$$ on the first floor and the room $$$i$$$ on the second floor. If the $$$i$$$-th character is a '0', then there is no staircase between the room $$$i$$$ on the first floor and the room $$$i$$$ on the second floor. In hacks it is allowed to use only one test case in the input, so $$$t = 1$$$ should be satisfied.", "output_spec": "For each test case print one integer — the maximum number of rooms Nikolay can visit during his tour, if he can start in any room on any floor, and he won't visit the same room twice.", "sample_inputs": ["4\n5\n00100\n8\n00000000\n5\n11111\n3\n110"], "sample_outputs": ["6\n8\n10\n6"], "notes": "NoteIn the first test case Nikolay may start in the first room of the first floor. Then he moves to the second room on the first floor, and then — to the third room on the first floor. Then he uses a staircase to get to the third room on the second floor. Then he goes to the fourth room on the second floor, and then — to the fifth room on the second floor. So, Nikolay visits $$$6$$$ rooms.There are no staircases in the second test case, so Nikolay can only visit all rooms on the same floor (if he starts in the leftmost or in the rightmost room).In the third test case it is possible to visit all rooms: first floor, first room $$$\\rightarrow$$$ second floor, first room $$$\\rightarrow$$$ second floor, second room $$$\\rightarrow$$$ first floor, second room $$$\\rightarrow$$$ first floor, third room $$$\\rightarrow$$$ second floor, third room $$$\\rightarrow$$$ second floor, fourth room $$$\\rightarrow$$$ first floor, fourth room $$$\\rightarrow$$$ first floor, fifth room $$$\\rightarrow$$$ second floor, fifth room.In the fourth test case it is also possible to visit all rooms: second floor, third room $$$\\rightarrow$$$ second floor, second room $$$\\rightarrow$$$ second floor, first room $$$\\rightarrow$$$ first floor, first room $$$\\rightarrow$$$ first floor, second room $$$\\rightarrow$$$ first floor, third room."}, "positive_code": [{"source_code": "readline.to_i.times do\n n = readline.to_i\n s = readline.chomp\n z = [n + s.count(\"1\")]\n if z[0] > n then\n l,r = s.index(\"1\"), s.rindex(\"1\")\n z << 2*(r+1)\n z << 2*(n-l)\n end\n p z.max\nend\n"}, {"source_code": "T = gets.to_i\nT.times do\n n = gets.to_i\n s = gets.chomp\n if !s.include?(\"1\")\n p n\n else\n l,r = 0,n-1\n l += 1 while s[l] == \"0\"\n r -= 1 while s[r] == \"0\"\n p [n-l,r+1].max*2\n end\nend"}, {"source_code": "MOD=10**9+7\ncnt=0; sum=0; prev=nil; can=true; h=Hash.new(0)\ndef gs() gets.chomp end\ndef gi() gets.to_i end\ndef gsmi() gets.chomp.split.map(&:to_i) end\n\ndef desc(ar) ar.sort!{|x,y|y<=>x} end\ndef min(a,b) a<=b ? a : b end\ndef max(a,b) a>=b ? a : b end\ndef sum(ar) ar.inject(:+) end\n\n#def bs(ar,eq,n) en=(eq==1 ? :>= : :>); ar.bsearch_index{|x|x.send(en,n)} end\ndef C(a,b) b==0||a==b ? 1 : (b=a-b if a/2 1) && (ra[pe] - ra[nx] > 1)\n ra[pe] -= 1\n k2 -= 1\n end\n break if k2 <= 0\nend\nputs ra.join(\" \")\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\n\ni = 1; while (true)\t\n\tif (a[i] - a[i-1] > 1 && a[i] - a[i+1] > 1) \n\t\ta[i] -= 1; k -= 1 \n\t\tbreak if (k.zero?) \n\tend\t\n\ti += 2\nend\n\nputs a*' '"}, {"source_code": "n, k = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\n\nn.times do |i|\n\tii = 2 * i + 1\n\tif (a[ii] - a[ii-1] > 1 && a[ii] - a[ii+1] > 1)\n\t\ta[ii] -= 1; k -= 1\n\tend\n\tbreak if k.zero?\nend\n\nputs a.join(' ')"}, {"source_code": "I =->{gets.split.map &:to_i}\n\nn, k = I[]; a = I[]\ni = 1; while (true)\t\n\tif a[i] - a[i-1] > 1 && a[i] - a[i+1] > 1\t\t\t\n\t\ta[i] -= 1; k -= 1; break if (k.zero?) \n\tend\n\ti += 2\nend\n\nputs a*' '"}, {"source_code": "n, k = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\n\ni = 1; while (true)\t\n\tif (a[i] - a[i-1] > 1 && a[i] - a[i+1] > 1) \n\t\ta[i] -= 1; k -= 1 \n\t\tbreak if (k.zero?) \n\tend\t\n\ti += 2\nend\n\nputs a.join(' ')"}, {"source_code": "n,k=gets.split.map &:to_i;a=gets.split.map &:to_i;1.step(2*n,2){|i|(a[i]-=1)&&(k-=1) if a[i]-1>a[i-1]&&a[i]-1>a[i+1];break if k==0};$><a[i-1]&&a[i]-1>a[i+1];break if k==0};$><a[i-1]&&a[i]-1>a[i+1];break if k==0};$>< a[i+1]\n mordor.push i\n picks.delete i\n break\n end\n end\nend\n\nmordor.each {|j| a[j]-=1 }\nputs a*' '"}, {"source_code": "n,k=gets.split.map &:to_i;a=gets.split.map &:to_i;1.step(2*n,2){|i|(a[i]-=1)&&(k-=1) if a[i]-1>a[i-1]&&a[i]-1>a[i+1];break if k==0};$>< 0 && i % 2 == 1 && a[i] - a[i - 1] >= 2 && a[i] - a[i + 1] >= 2 ? a[i] - 1 : a[i]\n k -= 1 if b[i] != a[i]\nend\nputs b.join \" \"\n"}, {"source_code": "n,k=gets.split.map &:to_i;a=gets.split.map &:to_i;1.step(2*n,2){|i|(a[i]-=1)&&(k-=1) if a[i]-1>a[i-1]&&a[i]-1>a[i+1];break if k==0};$>< y[i-1] && y[i]-1 > y[i+1]\n y[i] -=1\n c += 1\n break if c == k\n end\nend\nputs y.join(\" \")"}, {"source_code": "#!/usr/bin/ruby\nn,k = gets.chomp.split(' ').map{|x| x.to_i}\nline = gets.chomp.split(' ').map{|x| x.to_i}\nktmp=0\nfor i in 0..(2*n)\n if i%2==1\n if line[i]-1>line[i-1] && line[i]-1 > line[i+1] && ktmp < k\n ktmp+=1\n line[i]-=1;\n end\n end\n print line[i].to_s + \" \" if i < 2*n\nend\nputs line[2*n]\n"}, {"source_code": "\nn,m = gets.split.map(&:to_i)\nar = gets.split.map(&:to_i)\n\n\nwhile m > 0\n\ti = 1\n\twhile i < ar.length\n\t\tif ar[i] > ar[i-1] + 1 and ar[i] > ar[i+1] + 1\n\t\t\tar[i] -= 1\n\t\t\tm -= 1\n\t\t\tbreak if m == 0\n\t\tend\n\t\ti += 2\n\tend\nend\n\nfor x in ar\n\tprint x.to_s + \" \"\nend\n\n\n\n\n"}, {"source_code": "#!/usr/bin/ruby\ninclude Math\n\nn,k = gets.split.map(&:to_i)\npic = gets.split.map(&:to_i)\ndec = 0\nn.times do |i|\n if pic[2*i+1] - pic[2*i] > 1 && pic[2*i+1] - pic[2*i+2] > 1\n pic [2*i+1] -= 1\n dec += 1\n end\n break if dec >= k\nend\nputs pic.join(' ')\n"}, {"source_code": "n,k=gets.split.map &:to_i;a=gets.split.map &:to_i;1.step(2*n,2){|i|(a[i]-=1)&&(k-=1) if a[i]-1>a[i-1]&&a[i]-1>a[i+1];break if k==0};$><a[i-1]&&a[i]-1>a[i+1];break if k==0};$><0\n r[i]-=1\n k-=1\n end\nend\n\nr.each do |rr|\n print \"#{rr} \"\nend"}, {"source_code": "n,k=gets.split.map &:to_i;a=gets.split.map &:to_i;1.step(2*n,2){|i|(a[i]-=1)&&(k-=1) if a[i]-1>a[i-1]&&a[i]-1>a[i+1];break if k==0};$><a[i-1]&&a[i]-1>a[i+1];break if k==0};$><a[i-1]&&a[i]-1>a[i+1];break if k==0};$>< 1 && a[ii] - a[ii+1] > 1)\n\t\t--a[ii]; --k\n\tend\n\tbreak if k.zero?\nend\n\nputs a.join(' ')"}, {"source_code": "#!/usr/bin/ruby\ninclude Math\nstr = gets\nstr.gsub! /(WUB)+/, ' '\nstr.gsub! /^\\s*/,''\nstr.gsub! /\\s*$/,''\nputs str\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\nr = gets.split.map(&:to_i)\n1.upto(n) do |i|\n next if i%2==0\n break if k==0\n if r[i-1] < r[i]-1 && r[i+1]< r[i]-1 && k>0\n r[i]-=1\n k-=1\n end\nend\n\nr.each do |rr|\n print \"#{rr} \"\nend"}], "src_uid": "1adb4675dc88208f8a05a2db49bb44cb"} {"nl": {"description": "You are given $$$n$$$ points with integer coordinates on a coordinate axis $$$OX$$$. The coordinate of the $$$i$$$-th point is $$$x_i$$$. All points' coordinates are distinct and given in strictly increasing order.For each point $$$i$$$, you can do the following operation no more than once: take this point and move it by $$$1$$$ to the left or to the right (i..e., you can change its coordinate $$$x_i$$$ to $$$x_i - 1$$$ or to $$$x_i + 1$$$). In other words, for each point, you choose (separately) its new coordinate. For the $$$i$$$-th point, it can be either $$$x_i - 1$$$, $$$x_i$$$ or $$$x_i + 1$$$.Your task is to determine if you can move some points as described above in such a way that the new set of points forms a consecutive segment of integers, i. e. for some integer $$$l$$$ the coordinates of points should be equal to $$$l, l + 1, \\ldots, l + n - 1$$$.Note that the resulting points should have distinct coordinates.You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 2 \\cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) — the number of points in the set $$$x$$$. The second line of the test case contains $$$n$$$ integers $$$x_1 < x_2 < \\ldots < x_n$$$ ($$$1 \\le x_i \\le 10^6$$$), where $$$x_i$$$ is the coordinate of the $$$i$$$-th point. It is guaranteed that the points are given in strictly increasing order (this also means that all coordinates are distinct). It is also guaranteed that the sum of $$$n$$$ does not exceed $$$2 \\cdot 10^5$$$ ($$$\\sum n \\le 2 \\cdot 10^5$$$).", "output_spec": "For each test case, print the answer — if the set of points from the test case can be moved to form a consecutive segment of integers, print YES, otherwise print NO.", "sample_inputs": ["5\n\n2\n\n1 4\n\n3\n\n1 2 3\n\n4\n\n1 2 3 7\n\n1\n\n1000000\n\n3\n\n2 5 6"], "sample_outputs": ["YES\nYES\nNO\nYES\nYES"], "notes": null}, "positive_code": [{"source_code": "gets.to_i.times do\r\n gets\r\n xs = gets.split.map(&:to_i)\r\n dxs = xs.each_cons(2).map { |a, b| b - a }.sort.reverse\r\n if dxs.empty? || dxs[0] == 1 || dxs[0] == 2 && dxs.count(2) <= 2 || dxs[0] == 3 && (dxs[1] == nil || dxs[1] == 1)\r\n puts \"YES\"\r\n else\r\n puts \"NO\"\r\n end\r\nend\r\n"}, {"source_code": "gets.to_i.times do\n n = gets.to_i\n a = gets.chomp.split.map(&:to_i)\n c = 0\n 1.upto(n-1) do |i|\n c += a[i] - a[i-1] - 1\n end\n puts c > 2 ? \"NO\":\"YES\"\nend\n"}], "negative_code": [{"source_code": "gets.to_i.times do\r\n gets\r\n xs = gets.split.map(&:to_i)\r\n dxs = xs.each_cons(2).map { |a, b| b - a }.sort.reverse\r\n if dxs.empty? || dxs[0] == 1 || dxs[0] == 2 && dxs.count(2) <= 2 || dxs[0] == 3 && dxs.count(3) == 1\r\n puts \"YES\"\r\n else\r\n puts \"NO\"\r\n end\r\nend\r\n"}], "src_uid": "f4267120fce9304fc4c45142b60fb867"} {"nl": {"description": "God's Blessing on This PermutationForces!A Random PebbleYou are given a permutation $$$p_1,p_2,\\ldots,p_n$$$ of length $$$n$$$ and a positive integer $$$k \\le n$$$. In one operation you can choose two indices $$$i$$$ and $$$j$$$ ($$$1 \\le i < j \\le n$$$) and swap $$$p_i$$$ with $$$p_j$$$.Find the minimum number of operations needed to make the sum $$$p_1 + p_2 + \\ldots + p_k$$$ as small as possible.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 100$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le k \\le n \\le 100$$$). The second line of each test case contains $$$n$$$ integers $$$p_1,p_2,\\ldots,p_n$$$ ($$$1 \\le p_i \\le n$$$). It is guaranteed that the given numbers form a permutation of length $$$n$$$.", "output_spec": "For each test case print one integer — the minimum number of operations needed to make the sum $$$p_1 + p_2 + \\ldots + p_k$$$ as small as possible.", "sample_inputs": ["4\n\n3 1\n\n2 3 1\n\n3 3\n\n1 2 3\n\n4 2\n\n3 4 1 2\n\n1 1\n\n1"], "sample_outputs": ["1\n0\n2\n0"], "notes": "NoteIn the first test case, the value of $$$p_1 + p_2 + \\ldots + p_k$$$ is initially equal to $$$2$$$, but the smallest possible value is $$$1$$$. You can achieve it by swapping $$$p_1$$$ with $$$p_3$$$, resulting in the permutation $$$[1, 3, 2]$$$.In the second test case, the sum is already as small as possible, so the answer is $$$0$$$."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n n,k = gets.split.map(&:to_i)\r\n inputs = gets.split.map(&:to_i)\r\n puts inputs[...k].count { |x| x>k }\r\nend\r\n"}, {"source_code": "gets.to_i.times do\r\n n, k = gets.split.map(&:to_i)\r\n ps = gets.split.map(&:to_i)\r\n p ps[..k - 1].count { |p| p > k }\r\nend\r\n"}, {"source_code": "gets.to_i.times do\n n,k=gets.split.map(&:to_i)\n p=gets.split.map(&:to_i)\n puts p[0...k].count {|i| i>k}\nend\n"}], "negative_code": [], "src_uid": "10927dcb59007fc3a31fdb6b5d13846f"} {"nl": {"description": "Vasya's birthday is approaching and Lena decided to sew a patterned handkerchief to him as a present. Lena chose digits from 0 to n as the pattern. The digits will form a rhombus. The largest digit n should be located in the centre. The digits should decrease as they approach the edges. For example, for n = 5 the handkerchief pattern should look like that:           0        0 1 0      0 1 2 1 0    0 1 2 3 2 1 0  0 1 2 3 4 3 2 1 00 1 2 3 4 5 4 3 2 1 0  0 1 2 3 4 3 2 1 0    0 1 2 3 2 1 0      0 1 2 1 0        0 1 0          0Your task is to determine the way the handkerchief will look like by the given n.", "input_spec": "The first line contains the single integer n (2 ≤ n ≤ 9).", "output_spec": "Print a picture for the given n. You should strictly observe the number of spaces before the first digit on each line. Every two adjacent digits in the same line should be separated by exactly one space. There should be no spaces after the last digit at the end of each line.", "sample_inputs": ["2", "3"], "sample_outputs": ["0\n 0 1 0\n0 1 2 1 0\n 0 1 0\n 0", "0\n 0 1 0\n 0 1 2 1 0\n0 1 2 3 2 1 0\n 0 1 2 1 0\n 0 1 0\n 0"], "notes": null}, "positive_code": [{"source_code": "class Euler\n a=gets.to_i;\n b=2*a+1;\n pr=2*a;\n allpr=2*b-1;\n (1..pr).each do\n print(\" \");\n end\n puts(\"0\");\n (2..b-1).each do |i|\n if (i<=a+1) \n c=2*i-1;\n else\n c=b-2*(i-a-1)\n end\n s=2*(a-(c/2));\n (1..s).each do\n print(\" \");\n end\n l=c/2+1;\n (1..l).each do |j|\n print(j-1);\n print(\" \");\n end\n (1..l-2).each do |j|\n k=(c-1)/2;\n print(k-j);\n print(\" \");\n end\n if (l>1)\n print(\"0\");\n end\n print(\"\\n\");\n end\n (1..pr).each do\n print(\" \");\n end\n puts(\"0\");\n end\n"}, {"source_code": "n=gets.hex\nputs [*0..n,*(n-1).downto(0)].map{|k|\" \"*(2*(n-k))+([*0..k,*(k-1).downto(0)]*\" \")}\n"}, {"source_code": "n = gets.to_i\nx = 0\nwhile (x <= n)\n c = n - x\n i = 0\n s = ''\n while (i < c) \n s += ' ' \n i+=1 \n end\n i = 0\n fst = true\n while (i <= x)\n if (!fst) then s += ' ' end\n fst = false\n s += i.to_s\n i+=1\n end\n i = x-1\n while (i >= 0)\n s += ' ' + i.to_s\n i-=1\n end\n puts s\n x+=1\nend\nx = n-1\nwhile (x >= 0)\n c = n - x\n i = 0\n s = ''\n while (i < c) \n s += ' ' \n i+=1 \n end\n i = 0\n fst = true\n while (i <= x)\n if (!fst) then s += ' ' end\n fst = false\n s += i.to_s\n i+=1\n end\n i = x-1\n while (i >= 0)\n s += ' ' + i.to_s\n i-=1\n end\n puts s\n x-=1\nend"}, {"source_code": "def present\n n = gets.to_i + 1\n\n #print \" \"\n\n n.times do |i|\n line = \"\"\n line += \" \" * (n - i - 1)\n 0.upto(i) do |j|\n line += \"#{j} \"\n end\n\n (i - 1).downto(0) do |l|\n line += \"#{l} \"\n end\n line[line.size - 1] = \"\\n\"\n print line\n end\n\n (n - 2).downto(0) do |i|\n line = \"\"\n line += \" \" * (n - i - 1)\n 0.upto(i) do |j|\n line += \"#{j} \"\n end\n\n (i - 1).downto(0) do |l|\n line += \"#{l} \"\n end\n line[line.size - 1] = \"\\n\"\n print line\n end\nend\n\npresent"}, {"source_code": "n=gets.hex\nputs [*0..n,*(n-1).downto(0)].map{|k|\" \"*(2*(n-k))+([*0..k,*(k-1).downto(0)]*\" \")}\n"}, {"source_code": "n=gets.hex\nputs [*0..n,*(n-1).downto(0)].map{|k|\" \"*(2*(n-k))+([*0..k,*(k-1).downto(0)]*\" \")}"}, {"source_code": "#!/usr/bin/ruby -Ks\n\nINPUT_NUMBER = $stdin.gets.chomp.to_i\n\noutput = Array.new\n\n0.upto(INPUT_NUMBER) do |i|\n output[i] = \"\"\n (INPUT_NUMBER - i).times do |j|\n output[i] += \" \" + \" \"\n end\n 0.upto(i) do |j|\n output[i] += j.to_s + \" \"\n end\nend\n\noutput.each_with_index do |str, idx|\n output_string = str + str.reverse.slice(3, (str.size - 3)).rstrip\n puts output_string.rstrip\nend\n\noutput.reverse.each_with_index do |str, idx|\n next if idx == 0\n output_string = str + str.reverse.slice(3, (str.size - 3))\n puts output_string.rstrip\nend\n\n# EOF\n"}, {"source_code": "input = gets.to_i\n\ntop = \"\"\n\n(0..input).each do |x|\n\n top += \" \" * (input - x)\n\n l = (0..x).to_a\n r = l.reverse\n r.shift\n\n top += (l + r).join \" \"\n top += \"\\n\"\n\nend\n\nbot = top.split(\"\\n\")\nbot.pop\n\nprint top + bot.reverse.join(\"\\n\")\n"}, {"source_code": "n=gets.hex\nputs [*0..n,*(n-1).downto(0)].map{|k|\" \"*(2*(n-k))+([*0..k,*(k-1).downto(0)]*\" \")}\n"}, {"source_code": "n = gets.to_i\nstr = \" \" * 2 * n + \"0\"\n(1..n).each do |x|\n puts str\n str[2*n+1,0]=\" #{x} #{x-1}\"\n str[0,2]=\"\"\nend\n(1..n).each do |x|\n puts str\n str[2*n-2,4]=\"\"\n str[0,0]=\" \"\nend\nputs str"}, {"source_code": "n = STDIN.gets.chomp.to_i\nlines = ((n + 1) * 2) - 1\nelem = Array.new() \nelem.push 1\n1.upto(lines - 1) do |i|\n if i <= n \n elem[i] = elem[i - 1] + 2\n else\n elem[i] = elem[i - 1] - 2\n end\nend\nspace = n\nelem.each do |i|\n line = \"\"\n (elem[space] - 1).times {|i| line += \" \"}\n space -= 1 \n space -= 1 if space == -1\n num = 0\n i.times do |j| \n line += (num.to_s + \" \"); \n if j < i / 2\n num += 1\n else\n num -= 1\n end\n end\n puts line.rstrip\nend"}, {"source_code": "n = STDIN.gets.chomp.to_i\nlines = ((n + 1) * 2) - 1\n#elem = Array.new() \n#elem.push 1\n\nelem = 1.upto(lines - 1).inject([1]) {|res, i| res[i] = res[i - 1] + (i <= n ? 2 : -2); res}\n# 1.upto(lines - 1) do |i|\n # if i <= n \n # elem[i] = elem[i - 1] + 2\n # else\n # elem[i] = elem[i - 1] - 2\n # end\n# end\nspace = n\nelem.each do |i|\n line = \" \" * (elem[space] - 1)\n # (elem[space] - 1).times {|i| line += \" \"}\n space -= 1 \n space -= 1 if space == -1\n num = 0\n i.times do |j| \n line += (num.to_s + \" \"); \n if j < i / 2\n num += 1\n else\n num -= 1\n end\n end\n puts line.rstrip\nend"}, {"source_code": "n = STDIN.gets.chomp.to_i\nlines = ((n + 1) * 2) - 1\n\nelem = 1.upto(lines - 1).inject([1]) {|res, i| res[i] = res[i - 1] + (i <= n ? 2 : -2); res}\nspace = n\nelem.each do |i|\n line = \" \" * (elem[space] - 1)\n space -= (space == 0) ? 2 : 1\n num = 0\n i.times do |j| \n line += (num.to_s + \" \");\n num += (j < i/2) ? 1: -1\n end\n puts line.rstrip\nend"}, {"source_code": "n = STDIN.gets.chomp.to_i\nlines = ((n + 1) * 2) - 1\n#elem = Array.new() \n#elem.push 1\n\nelem = 1.upto(lines - 1).inject([1]) {|res, i| res[i] = i <= n ? res[i - 1] + 2 : res[i - 1] - 2; res}\n# 1.upto(lines - 1) do |i|\n # if i <= n \n # elem[i] = elem[i - 1] + 2\n # else\n # elem[i] = elem[i - 1] - 2\n # end\n# end\nspace = n\nelem.each do |i|\n line = \" \" * (elem[space] - 1)\n # (elem[space] - 1).times {|i| line += \" \"}\n space -= 1 \n space -= 1 if space == -1\n num = 0\n i.times do |j| \n line += (num.to_s + \" \"); \n if j < i / 2\n num += 1\n else\n num -= 1\n end\n end\n puts line.rstrip\nend\n"}, {"source_code": "n = STDIN.gets.chomp.to_i\nlines = ((n + 1) * 2) - 1\nelem = 1.upto(lines - 1).inject([1]) {|res, i| res[i] = res[i - 1] + (i <= n ? 2 : -2); res}\nspace = n\nelem.each do |i|\n line = \" \" * (elem[space] - 1)\n space -= (space == 0) ? 2 : 1\n num = 0\n i.times do |j| \n line += (num.to_s + \" \");\n num += (j < i / 2) ? 1: -1\n end\n puts line.rstrip\nend"}, {"source_code": "n = STDIN.gets.chomp.to_i\nlines = ((n + 1) * 2) - 1\nelem = Array.new() \nelem.push 1\n1.upto(lines - 1) do |i|\n if i <= n \n elem[i] = elem[i - 1] + 2\n else\n elem[i] = elem[i - 1] - 2\n end\nend\nspace = n\nelem.each do |i|\n line = \" \" * (elem[space] - 1)\n # (elem[space] - 1).times {|i| line += \" \"}\n space -= 1 \n space -= 1 if space == -1\n num = 0\n i.times do |j| \n line += (num.to_s + \" \"); \n if j < i / 2\n num += 1\n else\n num -= 1\n end\n end\n puts line.rstrip\nend"}, {"source_code": "n = gets.chomp.to_i\n\nfor i in 0..2*n\n spn = 0\n pn = 2*n+1\n if i < n then\n spn = 2*(n - i)\n pn = 2*i+1\n elsif i > n then\n spn = 2*(i - n )\n pn = 2*(2*n-i) + 1\n end\n for j in 0..spn-1\n print \" \"\n end\n nn = 0\n for j in 0..pn-1\n print \" \" if j > 0\n print nn\n if j >= (pn-1).div(2)\n nn -= 1\n else\n nn += 1\n end\n end\n puts \"\"\nend\n\n"}, {"source_code": "n = gets.to_i\na = Array.new(2 * n + 1){Array.new(2 * n + 1){' '}}\n(-n .. n).each do |i|\n\tk = n - i.abs\n\t(-k .. k).each do |j|\n\t\ta[i + n][j + n] = (k - j.abs).to_s\n\tend\nend\nputs a.map{|_| (_ * ' ').rstrip} * \"\\n\"\n"}, {"source_code": "n=gets.to_i\nputs (0..n+n).map{|i|((0..n+n).map{|j|x=n-(i-n).abs-(j-n).abs;x<0? \" \":x}*\" \").rstrip}"}, {"source_code": "n = STDIN.readline.to_i\n\na = n + 1\nresult = []\n(0..n).each do |i|\n line = []\n (-(n)..i).each do |j| \n if i - j.abs < 0\n line << ' ' \n else \n line << (i - j.abs)\n end\n end\n result << line.join(' ')\nend\nresult << result[0..-2].reverse\nputs result"}, {"source_code": "\nn=gets.to_i\nk=1\nq=n\nfor i in 0..n\n (q).times{\n print \" \"\n }\n for j in 0..(k/2)\n if j>0 then\n print \" \"\n end\n print j\n end\n (k/2-1).downto(0){|j|\n print \" \"+j.to_s\n }\n puts\n k+=2\n q-=1\nend\nq+=2\nk-=4\nn.downto(1){|i|\n (q).times{\n print \" \"\n }\n for j in 0..(k/2)\n if j>0 then\n print \" \"\n end\n print j\n end\n (k/2-1).downto(0){|j|\n print \" \"+j.to_s\n }\n puts\n k-=2\n q+=1\n}\n\n"}, {"source_code": "n=gets.hex\nputs [*0..n,*(n-1).downto(0)].map{|k|\" \"*(2*(n-k))+([*0..k,*(k-1).downto(0)]*\" \")}\n"}, {"source_code": "def line(a, n)\n space = Array.new(n-a){\" \"}.join(\"\")\n str = \"#{a}\"\n while a > 0\n a -= 1\n str = \"#{a} #{str} #{a}\"\n end\n puts space + str\nend\n\nn = gets.to_i\ni = 0\nwhile i <= n do\n line(i, n)\n i += 1\nend\ni = n-1\nwhile i >= 0 do\n line(i, n)\n i -= 1\nend"}, {"source_code": "n=gets.hex\nputs [*0..n,*(n-1).downto(0)].map{|k|\" \"*(2*(n-k))+([*0..k,*(k-1).downto(0)]*\" \")}\n"}, {"source_code": "n=gets.hex\nputs [*0..n,*(n-1).downto(0)].map{|k|\" \"*(2*(n-k))+([*0..k,*(k-1).downto(0)]*\" \")}"}, {"source_code": "n = gets.to_i\n\n(0..2 * n).each do |y|\n s = \"\"\n (0..2 * n).each do |x|\n t = n - (x - n).abs - (y - n).abs\n s += (t >= 0 ? t.to_s : \" \")\n s += \" \"\n end\n puts s.rstrip\nend\n"}, {"source_code": "n = gets.to_i\n(0..n).each do |i|\n puts \" \"*(2*(n-i)) + (0..i).to_a*\" \" + (\" \" if i != 0).to_s + (0...i).to_a.reverse*\" \"\nend\n(0..n-1).to_a.reverse.each do |i|\n puts \" \"*(2*(n-i)) + (0..i).to_a*\" \" + (\" \" if i != 0).to_s + (0...i).to_a.reverse*\" \"\nend"}, {"source_code": "x = gets.chomp.to_i\n\narr = []\n\n0.upto(x) do |ind|\n arr << \"\"\n arr.last << 0.upto(ind).to_a.join(' ')\n line_end = (ind-1).downto(0).to_a.join(' ')\n arr.last << ' ' + line_end if line_end.size > 0\nend\n\nlargest_len = arr.last.length\n\narr.each do |s|\n puts \" \"*((largest_len-s.length)/2) + s\nend\n\narr.reverse.drop(1).each do |s|\n puts \" \"*((largest_len-s.length)/2) + s\nend\n"}, {"source_code": "#!/usr/bin/ruby\n\ndef readNextValues\n arr = gets.chomp.split\n return arr.collect {|x| x.to_i }\nend\n\n#######################\n\ndef nums(toNum)\n 0.upto(toNum) do |i|\n print \"#{i}\"\n if toNum != 0\n print ' '\n end\n end\n (toNum-1).downto(0) do |i|\n print \"#{i}\"\n if i != 0\n print ' '\n end\n end\nend\n\nn = gets.to_i\n\n0.upto(n) do |i|\n print ' ' * (n-i)\n nums(i)\n puts\nend\n(n-1).downto(0) do |i|\n print ' ' * (n-i)\n nums(i)\n puts\nend\n"}, {"source_code": "n=gets.hex\nputs [*0..n,*(n-1).downto(0)].map{|k|\" \"*(2*(n-k))+([*0..k,*(k-1).downto(0)]*\" \")}"}, {"source_code": "n = gets.to_i\nstr = \" \" * 2 * n + \"0\"\n(1..n).each do |x|\n puts str\n str[2*n+1,0]=\" #{x} #{x-1}\"\n str[0,2]=\"\"\nend\n(1..n).each do |x|\n puts str\n str[2*n-2,4]=\"\"\n str[0,0]=\" \"\nend\nputs str"}, {"source_code": "n=gets.hex\nputs [*0..n,*(n-1).downto(0)].map{|k|\" \"*(2*(n-k))+([*0..k,*(k-1).downto(0)]*\" \")}\n"}, {"source_code": "n=gets.hex\nputs [*0..n,*(n-1).downto(0)].map{|k|\" \"*(2*(n-k))+([*0..k,*(k-1).downto(0)]*\" \")}\n"}, {"source_code": "n=gets.to_i\narr=[]\n(n+1).times{|i|\n\tstr=\"\"\n\tc1=i-n\n\td=1\n\tf=true\n\t0.upto(n+i){\n\t\tif (d==1) && (c1==i-n) then\n\t\telse\n\t\t\tstr<<\" \"\n\t\tend\n\t\tif c1<0 then\n\t\t\tstr<<\" \"\n\t\telse\n\t\t\tstr<i\n}\narr.reverse.each{|e|\n\tputs e\n}\n\n"}, {"source_code": "#!/usr/bin/env ruby\ndef a2s( a )\n b = []\n a.each do |i|\n if i < 0\n b << \" \"\n else\n b << i.to_s\n end\n end\n return b.join( \" \" ).rstrip\nend\n\nn = gets.to_i\na = []\n0.upto(n){ |i| a << i }\n(n-1).downto(0){ |i| a << i }\na.map!{ |i| i-n }\nn.times do |i|\n puts a2s(a)\n a.map!{ |i| i+1 }\nend\nn.times do |i|\n puts a2s(a)\n a.map!{ |i| i-1 }\nend\nputs a2s(a)\n"}, {"source_code": "n = gets.to_i + 1\n\nn.times do |i|\n (n-i-1).times do \nprint \" \"\n end\n\n (i+1).times do |ii|\n print ii\n print \" \" unless i == 0\n end\n (i-1).downto(0) do |ii|\n print ii\n print \" \" unless ii == 0\n end\n \n print \"\\n\"\nend\n\n(n-2).downto(0) do |i|\n (n-i-1).times do \n print \" \"\n end\n\n (i+1).times do |ii|\n print ii\n print \" \" unless i == 0\n end\n (i-1).downto(0) do |ii|\n print ii\n print \" \" unless ii == 0\n end\n \n print \"\\n\"\nend\n \n"}, {"source_code": "n=gets.hex\nputs [*0..n,*(n-1).downto(0)].map{|k|\" \"*(2*(n-k))+([*0..k,*(k-1).downto(0)]*\" \")}"}, {"source_code": "n=gets.to_i;s='0123456789'.split //;b=[];(n+1).times{|k|b<<((s[0...k]+[k]+s[0...k].reverse)*' ').center(4*n+1).rstrip};puts b+b[0..-2].reverse\n"}, {"source_code": "n=gets.to_i\nb=[]\n\n(n+1).times{|k|\t\t\n\ta=[]\n\t(k+1).times{|i| a+=[i]}\n\t(k-1).downto(0){|i| a+=[i]}\n\t\n\tb << a.join(' ').center(2*n+2*n+1).rstrip\n}\nb+=b[0..-2].reverse\nputs b\n"}, {"source_code": "n=gets.to_i;b=[];(n+1).times{|k|a=[k];(k-1).downto(0){|i| a=[i]+a+[i]};b<<(a*' ').center(4*n+1).rstrip};b+=b[0..-2].reverse;puts b\n"}, {"source_code": "n=gets.to_i;b=[];(n+1).times{|k|a=[];(k+1).times{|i| a+=[i]};(k-1).downto(0){|i|a+=[i]};b< 0\n\t\t\tprint \" \"\n\t\tend\n\t}\n\tputs \"\"\nend\n#pts n\n(n-1).downto(0){ |y|\n for s in 1..n-y+n-y\n print \" \"\n end\n for x in 0..y-1\n print x\n\t\tprint \" \"\n end\n y.downto(0){ |x|\n\t\tprint x\n\t\tif x > 0\n\t\t\tprint \" \"\n\t\tend\n }\n puts \"\"\n\n\n}\n"}, {"source_code": "#!/usr/bin/ruby\n# coding: utf-8\n\nn = gets.chomp.to_i\n0.upto(n) do |i|\n puts \"#{' ' * (2*(n-i))}#{(0.upto(i).to_a + (i-1).downto(0).to_a).join(' ')}\"\nend\n(n-1).downto(0) do |i|\n puts \"#{' ' * (2*(n-i))}#{(0.upto(i).to_a + (i-1).downto(0).to_a).join(' ')}\"\nend\n"}, {"source_code": "i,a=gets.to_i,[]\n(0..i).map {|j| a << ((0...j).to_a << j.downto(0).to_a).flatten; a[j].unshift(' '*(i*2-j*2))\n}\na.each {|x| puts x.join(' ')[1..-1]}; a.pop; a.reverse_each {|x| puts x.join(' ')[1..-1]}"}, {"source_code": "n = gets.to_i\na = []\n(0..n).each{|i|\n s = ''\n (0..(n - ([i, i-n].min).abs)).each{|j| s = s + j.to_s + ' '}\n a << ' '*(n - i).abs + s.strip + s.reverse[2..-1]\n puts a[-1]\n}\n(0..n-1).each{|i| puts a[-2 - i]}"}, {"source_code": "n = gets.to_i\n(0..2*n).each{|i|\n s = ''\n (0..(n - ([i, i-n].min).abs)).each{|j| s = s + j.to_s + ' '}\n puts ' '*(n - i).abs + s.strip + s.reverse[2..-1]\n}"}, {"source_code": "n = gets.to_i\n(0..2*n).each{|i|\n s = ''\n st = ''\n (0..(n - ([i, i-n].min).abs)).each{|j| s = s + j.to_s + ' '}\n puts ' '*(n - i).abs + s.strip + s.reverse[2..-1]\n}\n"}, {"source_code": "# To change this template, choose Tools | Templates\n# and open the template in the editor.\n\nn = gets.chop.to_i\n\nsize = 2*n + 1\n\nfor i in -n..n\n line = \"\"\n for j in -n..n\n val = (n - (i.abs + j.abs))\n if (val >= 0)\n line += \"#{val} \"\n else\n line += \" \"\n end\n\n end\n puts line.rstrip\nend\n"}, {"source_code": "input = $stdin.readline.chop.to_i\nn = input\ni = 0\nwhile n>=0\n (n*2).times do \n print \" \"\n end\n\n for j in 0..(i-1)\n print \"#{j} \"\n end\n\n print \"#{i}\"\n print \" \" if i!=0\n\n j = i - 1\n while j>=0\n print j\n print \" \" if j!=0\n j-=1\n end\n\n print \"\\n\"\n n-=1\n i+=1\nend\n\nn = 1\ni = input-1\nwhile n<=input\n (n*2).times do \n print \" \"\n end\n\n for j in 0..(i-1)\n print \"#{j} \"\n end\n\n print \"#{i}\"\n print \" \" if i!=0\n\n j = i - 1\n while j>=0\n print j\n print \" \" if j!=0\n j-=1\n end\n\n print \"\\n\"\n n+=1\n i-=1\nend"}, {"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\n\nn = gets.to_i\nlines = []\nfor i in (0..n).to_a + (0...n).to_a.reverse\n pads = n - i\n buf = []\n pads.times { buf << ' ' }\n lines << buf + (0..i).to_a + (0...i).to_a.reverse\nend\n\nfor line in lines\n puts line.join(' ')\nend"}, {"source_code": "n = Integer(gets)\nline = 2 * n + 1\na = Array.new\nfor i in 0...line\n a[i] = Array.new\nend\n\nfor i in 0...line\n if i <= n\n for j in 0...n+i+1\n a[i][j] = \" \"\n end\n else \n for j in 0...3*n+1-i\n a[i][j] = \" \"\n end\n end\nend\n\nfor m in 0...n\n \n for i in 0...line\n for j in 0...line\n if m == 0\n if i + j == n\n a[i][j] = 0\n a[i][2*n-j] = 0\n a[2*n-i][j] = 0\n a[2*n-i][2*n-j]= 0\n end\n else\n if i + j == n + m && i >= m && j >= m\n \n a[i][j] = m\n a[i][2*n-j] = m\n a[2*n-i][j] = m\n a[2*n-i][2*n-j]= m\n \n end\n end\n\n end\n end\nend\n\na[n][n] = n\nfor i in 0...line\n if i <= n\n for j in 0...n+i+1\n if j != 0\n print \" \"\n end\n print a[i][j]\n end\n else \n for j in 0...3*n+1-i\n if j!= 0\n print \" \"\n end\n print a[i][j]\n end\n end\n\n puts \nend\n"}, {"source_code": "n = gets.chomp.to_i\nspace = n*2\nfor i in (1..n)\n\tfor x in (i..space)\n\t\tprint \" \"\n\tend\n\tline = (n*2) + 1 - (((n + 1 - i) * 2)).abs\n\tcount = 0\n\tfor x in (1..line)\n\t\tif x < line\n\t\t\tprint count \n\t\t\tprint \" \"\n\t\t\tif x <= line/2 \n\t\t\t\tcount += 1\n\t\t\telse\n\t\t\t\tcount -= 1\n\t\t\tend\n\t\telse\n\t\t\tputs count\n\t\tend\n\tend\n\tspace -= 1\nend\ncount = 0\nfor i in (1..(n*2) + 1)\n\tif i < (n*2) + 1\n\t\tprint count \n\t\tprint \" \"\n\t\tif i <= ((n*2)+1)/2\n\t\t\tcount += 1\n\t\telse\n\t\t\tcount -= 1\n\t\tend\n\telse\n\t\tputs count\n\tend\nend\ni = n\nspace = n+1\nwhile i >= 1\n\t\tfor x in (i..space)\n\t\t\tprint \" \"\n\t\tend\n\tline = (n*2) + 1 - (((n + 1 - i) * 2)).abs\n\tcount = 0\n\tfor x in (1..line)\n\t\tif x < line\n\t\t\tprint count \n\t\t\tprint \" \"\n\t\t\tif x <= line/2 \n\t\t\t\tcount += 1\n\t\t\telse\n\t\t\t\tcount -= 1\n\t\t\tend\n\t\telse\n\t\t\tputs count\n\t\tend\n\tend\n\ti -= 1\n\tspace += 1\nend"}, {"source_code": "n=gets.hex\nputs [*0..n,*(n-1).downto(0)].map{|k|\" \"*(2*(n-k))+([*0..k,*(k-1).downto(0)]*\" \")}\n"}, {"source_code": "l = []\nn = gets.to_i\n(0..n).reverse_each do |x|\n\tl <<= \" \" * x * 2 + (x == n ? \"0\" : ((0..(n - x)).to_a + (0...(n - x)).to_a.reverse).join(\" \"))\nend\n\nputs (l + l.reverse[1..-1]).join(\"\\n\")"}, {"source_code": "n = STDIN.gets.chomp.to_i\n\ndef line(from, to)\n left = \"\"\n for i in from...to\n left += (if i >= 0 then i.to_s else \" \"; end) + \" \"\n end\n result = (left + to.to_s + left.reverse).rstrip\nend\n\ntop_lines = []\nfor i in 0...n\n top_lines << line(i - n, i)\nend\ntop_lines.each { |line| puts line }\nputs line(0, n)\ntop_lines.reverse.each { |line| puts line }"}], "negative_code": [{"source_code": "def present\n n = gets.to_i\n\n #print \" \"\n\n n.times do |i|\n line = \"\"\n line += \" \" * (n - i - 1)\n 0.upto(i) do |j|\n line += \"#{j} \"\n end\n\n (i - 1).downto(0) do |l|\n line += \"#{l} \"\n end\n line[line.size - 1] = \"\\n\"\n print line\n end\n\n (n - 2).downto(0) do |i|\n line = \"\"\n line += \" \" * (n - i - 1)\n 0.upto(i) do |j|\n line += \"#{j} \"\n end\n\n (i - 1).downto(0) do |l|\n line += \"#{l} \"\n end\n line[line.size - 1] = \"\\n\"\n print line\n end\nend\n\npresent"}, {"source_code": "#!/usr/bin/ruby -Ks\n\nINPUT_NUMBER = $stdin.gets.chomp.to_i\n\noutput = Array.new\n\n0.upto(INPUT_NUMBER) do |i|\n output[i] = \"\"\n (INPUT_NUMBER - i).times do |j|\n output[i] += \" \" + \" \"\n end\n 0.upto(i) do |j|\n output[i] += j.to_s + \" \"\n end\nend\n\noutput.each do |str|\n print str\n puts str.reverse.slice(3, (str.size - 3)).rstrip\nend\n\noutput.reverse.each_with_index do |str, idx|\n next if idx == 0\n print str\n puts str.reverse.slice(3, (str.size - 3)).rstrip\nend\n\n# EOF\n"}, {"source_code": "n = STDIN.gets.chomp.to_i\nlines = ((n + 1) * 2) - 1\nelem = Array.new() \nelem.push 1\n1.upto(lines - 1) do |i|\n if i <= n \n elem[i] = elem[i - 1] + 2\n else\n elem[i] = elem[i - 1] - 2\n end\nend\nspace = n\nelem.each do |i|\n print \" \"\n elem[space].times {|i| print \" \"}\n space -= 1 \n space -= 1 if space == -1\n num = 0\n i.times do |j| \n print num, \" \"; \n if j < i / 2\n num += 1\n else\n num -= 1\n end\n end\n puts\nend"}, {"source_code": "n = STDIN.gets.chomp.to_i\nlines = ((n + 1) * 2) - 1\nelem = Array.new() \nelem.push 1\n1.upto(lines - 1) do |i|\n if i <= n \n elem[i] = elem[i - 1] + 2\n else\n elem[i] = elem[i - 1] - 2\n end\nend\nspace = n\nelem.each do |i|\n line = \"\"\n (elem[space]).times {|i| line += \" \"}\n space -= 1 \n space -= 1 if space == -1\n num = 0\n i.times do |j| \n line += (num.to_s + \" \"); \n if j < i / 2\n num += 1\n else\n num -= 1\n end\n end\n puts line.chomp\nend"}, {"source_code": "n = STDIN.gets.chomp.to_i\nlines = ((n + 1) * 2) - 1\nelem = Array.new() \nelem.push 1\n1.upto(lines - 1) do |i|\n if i <= n \n elem[i] = elem[i - 1] + 2\n else\n elem[i] = elem[i - 1] - 2\n end\nend\nspace = n\nelem.each do |i|\n line = \"\"\n (elem[space]).times {|i| line += \" \"}\n space -= 1 \n space -= 1 if space == -1\n num = 0\n i.times do |j| \n line += (num.to_s + \" \"); \n if j < i / 2\n num += 1\n else\n num -= 1\n end\n end\n puts line.rstrip\nend"}, {"source_code": "n = STDIN.gets.chomp.to_i\nlines = ((n + 1) * 2) - 1\nelem = Array.new() \nelem.push 1\n1.upto(lines - 1) do |i|\n if i <= n \n elem[i] = elem[i - 1] + 2\n else\n elem[i] = elem[i - 1] - 2\n end\nend\nspace = n\nelem.each do |i|\n print \" \"\n (elem[space] - 1).times {|i| print \" \"}\n space -= 1 \n space -= 1 if space == -1\n num = 0\n i.times do |j| \n print \" \", num; \n if j < i / 2\n num += 1\n else\n num -= 1\n end\n end\n puts\nend"}, {"source_code": "n = STDIN.gets.chomp.to_i\nlines = ((n + 1) * 2) - 1\nelem = Array.new() \nelem.push 1\n1.upto(lines - 1) do |i|\n if i <= n \n elem[i] = elem[i - 1] + 2\n else\n elem[i] = elem[i - 1] - 2\n end\nend\nspace = n\nelem.each do |i|\n elem[space].times {|i| print \" \"}\n space -= 1 \n space -= 1 if space == -1\n num = 0\n i.times do |j| \n print num, \" \"; \n if j < i / 2\n num += 1\n else\n num -= 1\n end\n end\n puts\nend"}, {"source_code": "n=gets.to_i\nputs (0..n+n).map{|i|(0..n+n).map{|j|x=n-(i-n).abs-(j-n).abs;x<0?\" \":x}*\" \"}"}, {"source_code": "n = STDIN.readline.to_i\n\na = n + 1\nresult = []\n(0..n).each do |i|\n\tline = []\n\t(-(n)..(n)).each do |j|\t\t\n\t\tif i - j.abs < 0\n\t\t\tline << ' ' \n\t\telse \n\t\t\tline << (i - j.abs)\n\t\tend\n\tend\n\tresult << line.join(' ')\nend\nresult << result[0..-2].reverse\nputs result"}, {"source_code": "\nn=gets.to_i\nk=1\nq=n\nfor i in 0..n\n q.times{\n print \" \"\n }\n for j in 0..(k/2)\n if j then\n print \" \"\n end\n print j\n end\n (k/2-1).downto(0){|j|\n print \" \"+j.to_s\n }\n puts\n k+=2\n q-=1\nend\nq+=2\nk-=4\nn.downto(1){|i|\n q.times{\n print \" \"\n }\n for j in 0..(k/2)\n if j then\n print \" \"\n end\n print j\n end\n (k/2-1).downto(0){|j|\n print \" \"+j.to_s\n }\n puts\n k-=2\n q+=1\n}\n"}, {"source_code": "n = gets.to_i\n(0..n).each { |i| puts \" \"*(2*(n-i)) + (0..i).to_a*\" \" + \" \" + (0...i).to_a.reverse*\" \" }\n(0..n-1).to_a.reverse.each { |i| puts \" \"*(2*(n-i)) + (0..i).to_a*\" \" + \" \" + (0...i).to_a.reverse*\" \" }"}, {"source_code": "x = gets.chomp.to_i\n\narr = []\n\n0.upto(x) do |ind|\n arr << \"\"\n arr.last << 0.upto(ind).to_a.join(' ')\n line_end = (ind-1).downto(0).to_a.join(' ')\n arr.last << ' ' + line_end if line_end.size > 0\nend\n\nputs largest_len = arr.last.length\n\narr.each do |s|\n puts \" \"*((largest_len-s.length)/2) + s\nend\n\narr.reverse.drop(1).each do |s|\n puts \" \"*((largest_len-s.length)/2) + s\nend\n"}, {"source_code": "#!/usr/bin/ruby\n\ndef readNextValues\n arr = gets.chomp.split\n return arr.collect {|x| x.to_i }\nend\n\n#######################\n\ndef nums(toNum)\n 0.upto(toNum) do |i|\n print \"#{i} \"\n end\n (toNum-1).downto(0) do |i|\n print \"#{i}\"\n if i != 0\n print ' '\n end\n end\nend\n\nn = gets.to_i\n\n0.upto(n) do |i|\n print ' ' * (n-i)\n nums(i)\n puts\nend\n(n-1).downto(0) do |i|\n print ' ' * (n-i)\n nums(i)\n puts\nend\n"}, {"source_code": "#!/usr/bin/env ruby\ndef a2s( a )\n b = []\n a.each do |i|\n if i < 0\n b << \" \"\n else\n b << i.to_s\n end\n end\n return b.join( \" \" )\nend\n\nn = gets.to_i\na = []\n0.upto(n){ |i| a << i }\n(n-1).downto(0){ |i| a << i }\na.map!{ |i| i-n }\nn.times do |i|\n puts a2s(a)\n a.map!{ |i| i+1 }\nend\nn.times do |i|\n puts a2s(a)\n a.map!{ |i| i-1 }\nend\nputs a2s(a)\n"}, {"source_code": "n = gets.to_i + 1\n\nn.times do |i|\n (n-i-1).times do \n print \" \"\n end\n\n (i+1).times do |ii|\n print ii\n print \" \"\n end\n (i-1).downto(0) do |ii|\n print ii\n print \" \"\n end\n \n print \"\\n\"\nend\n \n(n-2).downto(0) do |i|\n (n-i-1).times do \n print \" \"\n end\n\n (i+1).times do |ii|\n print ii\n print \" \"\n end\n (i-1).downto(0) do |ii|\n print ii\n print \" \"\n end\n \n print \"\\n\"\nend\n \n"}, {"source_code": "n=gets.to_i;s='0123456789'.split //;b=[];(n+1).times{|k|b<<((s[0...k]+[k]+s[0...k].reverse)*' ').center(4*n+1)};puts b+b[0..-2].reverse\n"}, {"source_code": "n=gets.to_i\nb=[]\n\n(n+1).times{|k|\t\t\n\ta=[]\n\t(k+1).times{|i| a+=[i]}\n\t(k-1).downto(0){|i| a+=[i]}\n\t\n\tb << a.join(' ').center(2*n+2*n+1)\n}\nb+=b[0..-2].reverse\nputs b\n"}, {"source_code": "@n = gets.to_i\n@w = @n*4+1\n\ndef gen(k)\n s = ' '\n (0..k).each do |i|\n s += i.to_s + ' '\n end\n (0...k).reverse_each do |i|\n s += i.to_s + ' '\n end\n puts s.strip.center(@w)\nend\n\n(0..@n).each do |i|\n gen(i)\nend\n(0...@n).reverse_each do |i|\n gen(i)\nend"}, {"source_code": "# To change this template, choose Tools | Templates\n# and open the template in the editor.\n\nn = gets.chop.to_i\n\nsize = 2*n + 1\n\nfor i in -n..n\n line = \"\"\n for j in -n..n\n val = (n - (i.abs + j.abs))\n if (val >= 0)\n line += \"#{val} \"\n else\n line += \" \"\n end\n\n end\n puts line.chop\nend"}, {"source_code": "input = $stdin.readline.chop.to_i\nn = input\ni = 0\nwhile n>=0\n (n*2).times do \n print \" \"\n end\n\n for j in 0..(i-1)\n print \"#{j} \"\n end\n\n print \"#{i} \"\n\n j = i - 1\n while j>=0\n print j\n print \" \" if j!=0\n j-=1\n end\n\n print \"\\n\"\n n-=1\n i+=1\nend\n\nn = 1\ni = input-1\nwhile n<=input\n (n*2).times do \n print \" \"\n end\n\n for j in 0..(i-1)\n print \"#{j} \"\n end\n\n print \"#{i} \"\n\n j = i - 1\n while j>=0\n print j\n print \" \" if j!=0\n j-=1\n end\n\n print \"\\n\"\n n+=1\n i-=1\nend\n"}, {"source_code": "n = Integer(gets)\nline = 2 * n + 1\na = Array.new\nfor i in 0...line\n a[i] = Array.new\nend\n\nfor i in 0...line\n if i <= n\n for j in 0...n+i+1\n a[i][j] = \" \"\n end\n else \n for j in 0...3*n+1-i\n a[i][j] = \" \"\n end\n end\nend\n\nfor m in 0...n\n \n for i in 0...line\n for j in 0...line\n if m == 0\n if i + j == n\n a[i][j] = 0\n a[i][2*n-j] = 0\n a[2*n-i][j] = 0\n a[2*n-i][2*n-j]= 0\n end\n else\n if i + j == n + m && i >= m && j >= m\n \n a[i][j] = m\n a[i][2*n-j] = m\n a[2*n-i][j] = m\n a[2*n-i][2*n-j]= m\n \n end\n end\n\n end\n end\nend\n\na[n][n] = n\nfor i in 0...line\n for j in 0...line\n print a[i][j]\n if j != i + 3\n print \" \"\n end\n\n end\n puts \nend\n"}, {"source_code": "n = Integer(gets)\nline = 2 * n + 1\na = Array.new\nfor i in 0...line\n\ta[i] = Array.new\nend\n\nfor i in 0...line\n\tif i <= n\n\t\tfor j in 0...n+i+1\n\t\t\ta[i][j] = \" \"\n\t\tend\n\telse \n\t\tfor j in 0...3*n+1-i\n\t\t\ta[i][j] = \" \"\n\t\tend\n\tend\nend\n\nfor m in 0...n\n \n\tfor i in 0...line\n\t\tfor j in 0...line\n\t\t\tif m == 0\n\t\t\t\tif i + j == n\n\t\t\t\t\ta[i][j] = 0\n\t\t\t\t\ta[i][2*n-j] = 0\n\t\t\t\t\ta[2*n-i][j] = 0\n\t\t\t\t\ta[2*n-i][2*n-j]= 0\n\t\t\t\tend\n\t\t\telse\n\t\t\t\tif i + j == n + m && i >= m && j >= m\n\t\t\t\t\t\n\t\t\t\t\t\t\ta[i][j] = m\n\t\t\t\t\t\t\ta[i][2*n-j] = m\n\t\t\t\t\t\t\ta[2*n-i][j] = m\n\t\t\t\t\t\t\ta[2*n-i][2*n-j]= m\n\t\t\t\t\t\t\n\t\t\t\tend\n\t\t\tend\n\n\t\tend\n\tend\nend\n\na[n][n] = n\nfor i in 0...line\n\tfor j in 0...line\n\t print a[i][j]\n\tend\n\tputs \nend\n"}, {"source_code": "n = Integer(gets)\nline = 2 * n + 1\na = Array.new\nfor i in 0...line\n a[i] = Array.new\nend\n\nfor i in 0...line\n if i <= n\n for j in 0...n+i+1\n a[i][j] = \" \"\n end\n else \n for j in 0...3*n+1-i\n a[i][j] = \" \"\n end\n end\nend\n\nfor m in 0...n\n \n for i in 0...line\n for j in 0...line\n if m == 0\n if i + j == n\n a[i][j] = 0\n a[i][2*n-j] = 0\n a[2*n-i][j] = 0\n a[2*n-i][2*n-j]= 0\n end\n else\n if i + j == n + m && i >= m && j >= m\n \n a[i][j] = m\n a[i][2*n-j] = m\n a[2*n-i][j] = m\n a[2*n-i][2*n-j]= m\n \n end\n end\n\n end\n end\nend\n\n\na[n][n] = n\nfor i in 0...line\n for j in 0...line\n print a[i][j]\n if j != i + 3\n print \" \"\n end\n\n end\n puts \nend\n"}, {"source_code": "n = gets.chomp.to_i\nspace = n*2\nfor i in (1..n)\n\tfor x in (i..space)\n\t\tprint \" \"\n\tend\n\tline = (n*2) + 1 - (((n + 1 - i) * 2)).abs\n\tcount = 0\n\tfor x in (1..line)\n\t\tprint count \n\t\tprint \" \"\n\t\tif x <= line/2 \n\t\t\tcount += 1\n\t\telse\n\t\t\tcount -= 1\n\t\tend\n\tend\n\tputs \" \"\n\tspace -= 1\nend\ncount = 0\nfor i in (1..(n*2) + 1)\n\tif i < (n*2) + 1\n\t\tprint count \n\t\tprint \" \"\n\t\tif i <= ((n*2)+1)/2\n\t\t\tcount += 1\n\t\telse\n\t\t\tcount -= 1\n\t\tend\n\telse\n\t\tputs count\n\tend\nend\ni = n\nspace = n+1\nwhile i >= 1\n\t\tfor x in (i..space)\n\t\t\tprint \" \"\n\t\tend\n\tline = (n*2) + 1 - (((n + 1 - i) * 2)).abs\n\tcount = 0\n\tfor x in (1..line)\n\t\tprint count \n\t\tprint \" \"\n\t\tif x <= line/2 \n\t\t\tcount += 1\n\t\telse\n\t\t\tcount -= 1\n\t\tend\n\tend\n\tputs \" \"\n\ti -= 1\n\tspace += 1\nend"}, {"source_code": "n = gets.chomp.to_i\n\nfor i in (1..n)\n\tfor x in (i..n)\n\t\tprint \" \"\n\tend\n\tline = (n*2) + 1 - (((n + 1 - i) * 2)).abs\n\tcount = 0\n\tfor x in (1..line)\n\t\tprint count\n\t\tif x <= line/2 \n\t\t\tcount += 1\n\t\telse\n\t\t\tcount -= 1\n\t\tend\n\tend\n\tfor x in (i..n-1)\n\t\tprint \" \"\n\tend\n\tputs \" \"\nend\ncount = 0\nfor i in (1..(n*2) + 1)\n\tif i < (n*2) + 1\n\t\tprint count\n\t\tif i <= ((n*2)+1)/2\n\t\t\tcount += 1\n\t\telse\n\t\t\tcount -= 1\n\t\tend\n\telse\n\t\tputs count\n\tend\nend\ni = n\nwhile i >= 1\n\tfor x in (i..n)\n\t\tprint \" \"\n\tend\n\tline = (n*2) + 1 - (((n + 1 - i) * 2)).abs\n\tcount = 0\n\tfor x in (1..line)\n\t\tprint count\n\t\tif x <= line/2 \n\t\t\tcount += 1\n\t\telse\n\t\t\tcount -= 1\n\t\tend\n\tend\n\tfor x in (i..n-1)\n\t\tprint \" \"\n\tend\n\tputs \" \"\n\ti -= 1\nend"}, {"source_code": "n = STDIN.gets.chomp.to_i\n\ndef line(from, to)\n left = \"\"\n for i in from...to\n left += (if i >= 0 then i.to_s else \" \"; end) + \" \"\n end\n result = left + to.to_s + left.reverse\nend\n\ntop_lines = []\nfor i in 0...n\n top_lines << line(i - n, i)\nend\ntop_lines.each { |line| puts line }\nputs line(0, n)\ntop_lines.reverse.each { |line| puts line }"}, {"source_code": "class Euler\n a=gets.to_i;\n b=2*a+1;\n pr=2*a;\n allpr=2*b-1;\n \n (1..b).each do |i|\n if (i<=a+1) \n c=2*i-1;\n else\n c=b-2*(i-a-1)\n end\n s=2*(a-(c/2));\n (1..s).each do\n print(\" \");\n end\n l=c/2+1;\n (1..l).each do |j|\n print(j-1);\n print(\" \");\n end\n (1..l-2).each do |j|\n k=(c-1)/2;\n print(k-j);\n print(\" \");\n end\n if (l>1)\n print(\"0\");\n end\n print(\"\\n\");\n end\n end\n"}], "src_uid": "7896740b6f35010af751d3261b5ef718"} {"nl": {"description": "You are given $$$n$$$ strings $$$s_1, s_2, \\ldots, s_n$$$ consisting of lowercase Latin letters.In one operation you can remove a character from a string $$$s_i$$$ and insert it to an arbitrary position in a string $$$s_j$$$ ($$$j$$$ may be equal to $$$i$$$). You may perform this operation any number of times. Is it possible to make all $$$n$$$ strings equal?", "input_spec": "The first line contains $$$t$$$ ($$$1 \\le t \\le 10$$$): the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 1000$$$): the number of strings. $$$n$$$ lines follow, the $$$i$$$-th line contains $$$s_i$$$ ($$$1 \\le \\lvert s_i \\rvert \\le 1000$$$). The sum of lengths of all strings in all test cases does not exceed $$$1000$$$.", "output_spec": "If it is possible to make the strings equal, print \"YES\" (without quotes). Otherwise, print \"NO\" (without quotes). You can output each character in either lowercase or uppercase.", "sample_inputs": ["4\n2\ncaa\ncbb\n3\ncba\ncba\ncbb\n4\nccab\ncbac\nbca\nacbcc\n4\nacb\ncaf\nc\ncbafc"], "sample_outputs": ["YES\nNO\nYES\nNO"], "notes": "NoteIn the first test case, you can do the following: Remove the third character of the first string and insert it after the second character of the second string, making the two strings \"ca\" and \"cbab\" respectively. Remove the second character of the second string and insert it after the second character of the first string, making both strings equal to \"cab\". In the second test case, it is impossible to make all $$$n$$$ strings equal."}, "positive_code": [{"source_code": "gets.to_i.times do\n n = gets.to_i\n s = []\n n.times do\n s << gets.chomp!\n end\n cnt = [0] * 26\n s.each do |i|\n i.each_char do |c|\n cnt[c.ord - 'a'.ord] += 1\n end\n end\n ok = true\n cnt.each do |i|\n ok &= i % n == 0\n end\n puts ok ? \"YES\" : \"NO\"\nend"}, {"source_code": "t = gets.to_i\nans = []\nt.times do\n n = gets.to_i\n hash = Hash.new(0)\n n.times do\n s = gets.chomp.split(\"\")\n s.each do |x|\n hash[x] += 1\n end\n end\n str = \"YES\"\n hash.each_value do |v|\n if v % n != 0\n str = \"NO\"\n end\n end\n ans << str\nend\nans.each do |x|\n puts x\nend"}, {"source_code": "gets.to_i.times do\n l = gets.to_i\n h = Hash.new(0)\n l.times do\n gets.split('').each { |i| h[i] += 1 }\n end\n r = h.values.all? { |i| i % l == 0 } ? 'YES' : 'NO'\n puts r\nend\n"}], "negative_code": [], "src_uid": "3d6cd0a82513bc2119c9af3d1243846f"} {"nl": {"description": "You're given an integer $$$n$$$. For every integer $$$i$$$ from $$$2$$$ to $$$n$$$, assign a positive integer $$$a_i$$$ such that the following conditions hold: For any pair of integers $$$(i,j)$$$, if $$$i$$$ and $$$j$$$ are coprime, $$$a_i \\neq a_j$$$. The maximal value of all $$$a_i$$$ should be minimized (that is, as small as possible). A pair of integers is called coprime if their greatest common divisor is $$$1$$$.", "input_spec": "The only line contains the integer $$$n$$$ ($$$2 \\le n \\le 10^5$$$).", "output_spec": "Print $$$n-1$$$ integers, $$$a_2$$$, $$$a_3$$$, $$$\\ldots$$$, $$$a_n$$$ ($$$1 \\leq a_i \\leq n$$$). If there are multiple solutions, print any of them.", "sample_inputs": ["4", "3"], "sample_outputs": ["1 2 1", "2 1"], "notes": "NoteIn the first example, notice that $$$3$$$ and $$$4$$$ are coprime, so $$$a_3 \\neq a_4$$$. Also, notice that $$$a=[1,2,3]$$$ satisfies the first condition, but it's not a correct answer because its maximal value is $$$3$$$."}, "positive_code": [{"source_code": "n = gets.to_i \nm = 0\narr=Array.new(n)\nfor i in 2..n\n\tj = i\n\twhile(j <= n) \n\t\tif(arr[j-1] == nil)\n\t\t\tif(j == i) \n\t\t\t\tm += 1 \n\t\t\tend \n\t\t\tarr[j-1] = m\n\t\tend\n\t\tj += i \n\tend\nend\nfor ele in arr[1...n] \n\tprint(\"#{ele} \")\nend"}], "negative_code": [{"source_code": "n = gets.to_i \n\narr=Array.new(n)\nfor i in 2..n\n\tj = i\n\twhile(j <= n) \n\t\tif(arr[j-1] == nil) \n\t\t\tarr[j-1] = i-1 \n\t\tend\n\t\tj += i \n\tend\nend\nfor ele in arr[1...n] \n\tprint(\"#{ele} \")\nend"}], "src_uid": "aa3c015c20cb0c1789661fdc78ae9bec"} {"nl": {"description": "There is a matrix A of size x × y filled with integers. For every , Ai, j = y(i - 1) + j. Obviously, every integer from [1..xy] occurs exactly once in this matrix. You have traversed some path in this matrix. Your path can be described as a sequence of visited cells a1, a2, ..., an denoting that you started in the cell containing the number a1, then moved to the cell with the number a2, and so on.From the cell located in i-th line and j-th column (we denote this cell as (i, j)) you can move into one of the following cells: (i + 1, j) — only if i < x; (i, j + 1) — only if j < y; (i - 1, j) — only if i > 1; (i, j - 1) — only if j > 1.Notice that making a move requires you to go to an adjacent cell. It is not allowed to stay in the same cell. You don't know x and y exactly, but you have to find any possible values for these numbers such that you could start in the cell containing the integer a1, then move to the cell containing a2 (in one step), then move to the cell containing a3 (also in one step) and so on. Can you choose x and y so that they don't contradict with your sequence of moves?", "input_spec": "The first line contains one integer number n (1 ≤ n ≤ 200000) — the number of cells you visited on your path (if some cell is visited twice, then it's listed twice). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the integers in the cells on your path.", "output_spec": "If all possible values of x and y such that 1 ≤ x, y ≤ 109 contradict with the information about your path, print NO. Otherwise, print YES in the first line, and in the second line print the values x and y such that your path was possible with such number of lines and columns in the matrix. Remember that they must be positive integers not exceeding 109.", "sample_inputs": ["8\n1 2 3 6 9 8 5 2", "6\n1 2 1 2 5 3", "2\n1 10"], "sample_outputs": ["YES\n3 3", "NO", "YES\n4 9"], "notes": "NoteThe matrix and the path on it in the first test looks like this: Also there exist multiple correct answers for both the first and the third examples."}, "positive_code": [{"source_code": "n = gets.to_i\na = gets.split.map &:to_i\nd = (1...n).map { |i| (a[i] - a[i - 1]).abs } .uniq.reject { |v| v == 1 }\nif d.size > 1 || d.include?(0)\n puts :NO\nelse\n ok = d.empty? || (1...n).all? do |i|\n x = a[i - 1]\n y = a[i]\n x, y = y, x if x > y\n case y - x\n when 1 then x % d[0] > 0\n else true\n end\n end\n puts case ok\n when false then :NO\n else [:YES, [1e9.to_i, d[0] || 1].join(' ')]\n end\nend\n"}], "negative_code": [{"source_code": "n = gets.to_i\na = gets.split.map &:to_i\nd = (1...n).map { |i| (a[i] - a[i - 1]).abs } .uniq.reject { |v| v == 1 }\nif d.size > 1 || d.include?(0)\n puts :NO\nelse\n ok = d.empty? || (1...n).all? do |i|\n x = a[i - 1]\n y = a[i]\n x, y = y, x if x > y\n case y - x\n when 1 then x % d[0] > 0\n else true\n end\n end\n puts case ok\n when false then :NO\n else [:YES, [1e9.to_i, d[0]].join(' ')]\n end\nend\n"}], "src_uid": "3b725f11009768904514d87e2c7714ee"} {"nl": {"description": "The Berland Armed Forces System consists of n ranks that are numbered using natural numbers from 1 to n, where 1 is the lowest rank and n is the highest rank.One needs exactly di years to rise from rank i to rank i + 1. Reaching a certain rank i having not reached all the previous i - 1 ranks is impossible.Vasya has just reached a new rank of a, but he dreams of holding the rank of b. Find for how many more years Vasya should serve in the army until he can finally realize his dream.", "input_spec": "The first input line contains an integer n (2 ≤ n ≤ 100). The second line contains n - 1 integers di (1 ≤ di ≤ 100). The third input line contains two integers a and b (1 ≤ a < b ≤ n). The numbers on the lines are space-separated.", "output_spec": "Print the single number which is the number of years that Vasya needs to rise from rank a to rank b.", "sample_inputs": ["3\n5 6\n1 2", "3\n5 6\n1 3"], "sample_outputs": ["5", "11"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\nar = gets.split(' ',n-1).map(&:to_i).insert(0,0)\na, b = gets.split(' ').map(&:to_i)\nputs ar[a...b].inject(:+)"}, {"source_code": "n = gets.to_i\nd = gets.split.map{|i| i.to_i}\na,b = gets.split.map{|i| i.to_i}\ni = a\nres = 0\nwhile i < b do\n res += d[i-1]\n i += 1\nend\n\np res"}, {"source_code": "n = gets.chomp.to_i\nar = gets.chomp.split(/ /).map!{|x| x.to_i}\na,b = gets.chomp.split(/ /).map!{|x| x.to_i}\nsum = 0\nfor i in a-1..b-2\n\tsum += ar[i]\nend\nputs sum"}, {"source_code": "_=gets(p).split.map(&:to_i)\np eval _[_[-2]..._[-1]]*?+"}, {"source_code": "n = gets.to_i\nd = gets.split.map(&:to_i)\ni = gets.split.map(&:to_i)\nsum = 0\nd[i[0]-1..i[1]-2].each{|e| sum+= e}\nprint sum\n"}, {"source_code": "a=gets(p).split.map &:to_i;p eval a[a[-2]...a[-1]]*?+\n"}, {"source_code": "a=gets(p).split.map &:to_i;p eval a[a[-2]...a[-1]]*?+\n"}, {"source_code": "n,d,(a,b)=(1..3).map{gets.split.map &:to_i};p d[a-1,b-a].inject:+"}, {"source_code": "n = gets.to_i\ntry = gets.split().map(&:to_i)\na,b = gets.split().map(&:to_i)\nt = 0\n\nfor i in a...b\n\tt += try[i-1]\nend\n\nputs t"}, {"source_code": "a=gets(p).split.map &:to_i;p eval a[a[-2]...a[-1]]*?+"}, {"source_code": "n=gets.chomp.to_i\nj=gets.chomp.split(' ').map(&:to_i)\nsum=0\na,b=gets.chomp.split(' ').map(&:to_i)\ni=a-1\nwhile i<(b-1)\n\tsum+=j[i]\n\ti+=1\nend\nputs sum"}, {"source_code": "gets\nd = gets.split.map(&:to_i)\na, b = gets.split.map { |x| x.to_i - 1}\nputs (a...b).map {|i| d[i]}.inject(:+)"}, {"source_code": "a=gets(p).split.map &:to_i;p eval a[a[-2]...a[-1]]*?+\n"}, {"source_code": "t = gets.chomp.to_i\narr = gets.split.map(&:to_i)\na,b = gets.split.map(&:to_i)\nres = 0\nwhile a 0\n streaks << crt\n crt = 0\n diffs[a] -= 1\n end\n end\n\n streaks[-1] = streaks.last + crt\n\n puts total\n puts streaks.join(' ')\n\nend\n\ndef read_int\n gets.to_i\nend\n\ndef read_ints\n gets.split.map(&:to_i)\nend\n\ndef read_string\n gets.chomp\nend\n\nsolution unless ENV['TEST__MODE']\n\n"}, {"source_code": "N, K = gets.split.map(&:to_i)\nas = gets.split.map(&:to_i)\nsolved = N.times.sort_by{|i| -as[i]}[0, K].sort\nputs solved.map{|i| as[i]}.inject(:+)\nans = []\nK.times do |i|\n ans[i] = (i == K-1 ? N-1 : solved[i]) - (i == 0 ? -1 : solved[i-1])\nend\nputs ans.join(' ')"}, {"source_code": "nk = gets.chomp.split(\" \").map(&:to_i)\narr = gets.chomp.split(\" \").map(&:to_i)\nn = nk[0]\nk = nk[1]\nmaxarr = [[0,-1]]\nkmin = 0\n\narr.each_with_index do |x,i|\n if x>=maxarr[-1][0]\n if maxarr.length == k\n maxarr.pop\n end\n maxarr.push([x,i])\n maxarr.sort_by!{|z| z[0]}.reverse!\n end\nend\n\n#p maxarr\n\nprintarr=[]\nmaxarr.sort_by!{|z| z[1]}\n\nsum = 0\nprev = 0\nmaxarr.each do |x|\n sum += x[0]\n printarr.push(x[1]-prev+1)\n prev = x[1]+1\nend\n\n#p printarr\nputs sum\nif k>1\n printarr.pop\n printarr.push(n-printarr.inject(:+))\nelse\n printarr = [n]\nend\nprintarr.each{|x| print \"#{x} \"}\nputs"}], "negative_code": [{"source_code": "nk = gets.chomp.split(\" \").map(&:to_i)\narr = gets.chomp.split(\" \").map(&:to_i)\nn = nk[0]\nk = nk[1]\nmaxarr = [[0,-1]]\nkmin = 0\n\narr.each_with_index do |x,i|\n if x>=maxarr[-1][0]\n if maxarr.length == k\n maxarr.pop\n end\n maxarr.push([x,i])\n maxarr.sort_by!{|z| z[0]}.reverse!\n end\nend\n\n#p maxarr\n\nprintarr=[]\nmaxarr.sort_by!{|z| z[1]}\n\nsum = 0\nprev = 0\nmaxarr.each do |x|\n sum += x[0]\n printarr.push(x[1]-prev+1)\n prev = x[1]+1\nend\n\nputs sum\nif k>1\n printarr.pop\n printarr.push(n-printarr.inject(:+))\nend\nprintarr.each{|x| print \"#{x} \"}\nputs"}], "src_uid": "9cc61be7dc9b79f46a3e796db0134173"} {"nl": {"description": "Recently, on a programming lesson little Petya showed how quickly he can create files and folders on the computer. But he got soon fed up with this activity, and he decided to do a much more useful thing. He decided to calculate what folder contains most subfolders (including nested folders, nested folders of nested folders, and so on) and what folder contains most files (including the files in the subfolders).More formally, the subfolders of the folder are all its directly nested folders and the subfolders of these nested folders. The given folder is not considered the subfolder of itself. A file is regarded as lying in a folder, if and only if it either lies directly in this folder, or lies in some subfolder of the folder.For a better understanding of how to count subfolders and files for calculating the answer, see notes and answers to the samples.You are given a few files that Petya has managed to create. The path to each file looks as follows:diskName:\\folder1\\folder2\\...\\ foldern\\fileName diskName is single capital letter from the set {C,D,E,F,G}. folder1, ..., foldern are folder names. Each folder name is nonempty sequence of lowercase Latin letters and digits from 0 to 9. (n ≥ 1) fileName is a file name in the form of name.extension, where the name and the extension are nonempty sequences of lowercase Latin letters and digits from 0 to 9. It is also known that there is no file whose path looks like diskName:\\fileName. That is, each file is stored in some folder, but there are no files directly in the root. Also let us assume that the disk root is not a folder.Help Petya to find the largest number of subfolders, which can be in some folder, and the largest number of files that can be in some folder, counting all its subfolders.", "input_spec": "Each line of input data contains the description of one file path. The length of each line does not exceed 100, and overall there are no more than 100 lines. It is guaranteed, that all the paths are correct and meet the above rules. It is also guaranteed, that there are no two completely equal lines. That is, each file is described exactly once. There is at least one line in the input data.", "output_spec": "Print two space-separated numbers. The first one is the maximal number of possible subfolders in a folder (including nested folders, nested folders of nested folders, and so on). The second one is the maximal number of files in a folder (including nested files in subfolders). Note that the disks are not regarded as folders.", "sample_inputs": ["C:\n\\\nfolder1\n\\\nfile1.txt", "C:\n\\\nfolder1\n\\\nfolder2\n\\\nfolder3\n\\\nfile1.txt\nC:\n\\\nfolder1\n\\\nfolder2\n\\\nfolder4\n\\\nfile1.txt\nD:\n\\\nfolder1\n\\\nfile1.txt", "C:\n\\\nfile\n\\\nfile\n\\\nfile\n\\\nfile\n\\\nfile.txt\nC:\n\\\nfile\n\\\nfile\n\\\nfile\n\\\nfile2\n\\\nfile.txt"], "sample_outputs": ["0 1", "3 2", "4 2"], "notes": "NoteIn the first sample we have one folder on the \"C\" disk. It has no subfolders, which is why the first number in the answer is 0. But this folder contains one file, so the second number of the answer is 1.In the second sample we have several different folders. Consider the \"folder1\" folder on the \"C\" disk. This folder directly contains one folder, \"folder2\". The \"folder2\" folder contains two more folders — \"folder3\" and \"folder4\". Thus, the \"folder1\" folder on the \"C\" drive has exactly 3 subfolders. Also this folder contains two files, even though they do not lie directly in the folder, but they are located in subfolders of \"folder1\".In the third example we see that the names of some folders and some subfolders are identical. Consider the \"file\" folder, which lies directly on the \"C\" disk. That folder contains another \"file\" folder, which in turn contains another \"file\" folder, which contains two more folders, \"file\" and \"file2\". Thus, the \"file\" folder, which lies directly on the \"C\" disk, contains 4 subfolders."}, "positive_code": [{"source_code": "require 'set'\n\nclass Disk\n include Comparable\n attr_accessor :name, :folders\n def initialize(name)\n @name = name\n @folders = Hash.new\n end\n def <=> (other)\n self.name <=> other.name\n end\nend\n\nclass Folder\n include Comparable\n attr_accessor :name, :folders, :file_num, :total_file_num, :total_folder_num\n def initialize(name)\n @name = name\n @folders = Hash.new\n @file_num = 0 #direct decendant\n @total_file_num = 0\n @total_folder_num = 0\n end\n def <=> (other)\n self.name <=> other.name\n end\n def stat()\n @folders.each do |folder_name, folder|\n (fd_num, fl_num) = folder.stat\n @total_folder_num += fd_num\n @total_file_num += fl_num\n end\n return @total_folder_num + @folders.size, @total_file_num + @file_num\n end\nend\n\nclass Solution\n def initialize()\n @disks = Hash.new\n while line = gets do\n disk_name = line[0,1]\n if @disks.has_key?(disk_name)\n disk = @disks[disk_name]\n else\n disk = Disk.new(disk_name)\n @disks[disk_name] = disk\n end\n line = line[2..-1]\n parse(disk, line)\n end\n end\n\n def parse(location, line)\n match_data = /\\\\(.*?)\\\\/.match(line)\n if match_data.nil?\n location.file_num += 1\n else\n folder_name = match_data[1]\n if location.folders.has_key?(folder_name)\n folder = location.folders[folder_name]\n else\n folder = Folder.new(folder_name)\n location.folders[folder_name] = folder\n end\n line = line[match_data[0].size-1..-1]\n parse(folder, line)\n end\n end\n\n def solve()\n fd_max = 0\n fl_max = 0\n @disks.each_value do |disk|\n disk.folders.each do |folder_name, folder|\n fd_num, fl_num = folder.stat\n fd_max = [fd_max, fd_num].max\n fl_max = [fl_max, fl_num].max\n end\n end\n printf(\"%d %d\\n\", fd_max, fl_max)\n end\nend\n\nsolution = Solution.new\nsolution.solve\n\n"}, {"source_code": "def gao(dir)\n\tif dir.empty?\n\t\treturn 0, 1\n\telse\n\t\tfold, file = 1, 0\n\t\tdir.each do |_, subdir|\n\t\t\tfoldx, filex = gao(subdir)\n\t\t\tfold += foldx\n\t\t\tfile += filex\n\t\tend\n\t\treturn fold, file\n\tend\nend\n\ndir = {}\nreadlines.each do |line|\n\tpwd = dir\n\tline.split(?\\\\).each do |path|\n\t\tpwd[path] = {} unless pwd.has_key? path\n\t\tpwd = pwd[path]\n\tend\nend\n\nfold, file = 0, 0\ndir.each do |_, drive|\n\tdrive.each do |_, dirx|\n\t\tfoldx, filex = gao(dirx)\n\t\tfold = [fold, foldx].max\n\t\tfile = [file, filex].max\n\tend\nend\n\nputs \"#{fold - 1} #{file}\"\n"}, {"source_code": "#!/usr/bin/ruby\n\nclass Hash\n\tdef calc\n\t\tret = [0, 0]\n\t\tself.each_pair { |n, v|\n\t\t\tif n =~ /\\./\n\t\t\t\tret[1] += 1\n\t\t\telse\n\t\t\t\ttmp = v.calc\n\t\t\t\tret[0] += 1 + tmp[0]\n\t\t\t\tret[1] += tmp[1]\n\t\t\tend\n\t\t}\n\t\tret\n\tend\n\n\tdef subfolders\n\t\tcnt = 0\n\t\tself.each_pair { |n, v|\n\t\t\tcnt += 1+v.subfolders unless n =~ /\\./\n\t\t}\n\t\tcnt\n\tend\n\n\tdef files\n\t\tcnt = 0\n\t\tself.each_pair { |n, v|\n\t\t\tif n =~ /\\./\n\t\t\t\tcnt += 1\n\t\t\telse\n\t\t\t\tcnt += v.files\n\t\t\tend\n\t\t}\n\t\tcnt\n\tend\nend\n\ntree = {}\nARGF.read.each_line { |line|\n\tline.chomp!\n\tcur = tree\n\tline.split(\"\\\\\").each { |dir|\n\t\tcur[dir] ||= {}\n\t\tcur = cur[dir]\n\t}\n}\n\nv = tree.values.map { |drv| drv.values }.flatten\nputs \"#{v.map(&:subfolders).max} #{v.map(&:files).max}\"\n"}, {"source_code": "def is_file? name\n\tname.include?('.')\nend\n\ntop_dirs = {}\nseen = {}\nwhile path = gets\n\thier = path.split('\\\\')\n\n\tdir = hier[0] + hier[1]\n\thier.shift(2)\n\n\ttop_dirs[dir] = {subdir: 0, files: 0} unless top_dirs[dir]\n\ttop_dir = top_dirs[dir]\n\n\tpath = dir\n\thier.each do |name|\n\t\tpath += name\n\t\tnext if seen[path]\n\t\tseen[path] = true\n\t\tif is_file? name\n\t\t\ttop_dir[:files] += 1\n\t\telse\n\t\t\ttop_dir[:subdir] += 1\n\t\tend\n\tend\nend\n\n\n\nbest_subdir_count = 0\nbest_subfile_count = 0\n\ntop_dirs.each_value do |dir|\n\tbest_subdir_count = [best_subdir_count, dir[:subdir].to_i].max\n\tbest_subfile_count = [best_subfile_count, dir[:files].to_i].max\nend\n\nputs best_subdir_count\nputs best_subfile_count\n"}, {"source_code": "x = {}\ny = {}\nARGF.each do |l|\n f = l.split('\\\\')\n for i in 1..f.size - 2 do\n if x[f[0..i]] \n x[f[0..i]] += 1\n else\n x[f[0..i]] = 1\n y[f[0..i]] = 0\n for j in 1...i do\n y[f[0..j]] += 1\n end\n end \n end\nend\nputs [y, x].map{|a| a.each_value.max}.join(' ')"}, {"source_code": "x = {}\ny = {}\nARGF.each do |line|\n f = line.split('\\\\')\n for i in 1..f.size - 2 do\n if x[f[0..i]] == nil\n x[f[0..i]] = 1\n y[f[0..i]] = 0\n for j in 1...i do\n y[f[0..j]] += 1\n end\n else\n x[f[0..i]] += 1\n end \n end\nend\n\nputs y.each_value.max\nputs x.each_value.max\n "}, {"source_code": "h={}\nh2={}\nhm=hm2=0\nARGF.each do |line|\n f=line.split('\\\\')\n\n for i in 1...f.size do\n if(h[ f[0..i] ]!=nil) then h[ f[0..i] ]+=1\n else h[ f[0..i] ]=1\n end\n \n end\n for i in 1...f.size-1 do \n if(h2[ f[0..i] ]==nil) then \n h2[ f[0..i] ]=0\n for j in 1...i do h2[ f[0..j] ]+=1 end\n end\n\n end\nend\nh.each_value{|i| hm = ((i>hm)?i:hm)}\nh2.each_value{|i| hm2 = ((i>hm2)?i:hm2)}\nputs \"#{hm2} #{hm}\""}, {"source_code": "def foo(node)\n if node.empty?\n [0, 1]\n else\n sum = [1, 0]\n node.each_value do |x|\n a, b = foo(x)\n sum[0] += a\n sum[1] += b\n end\n sum\n end\nend\n\nroot = {}\n\nARGF.each do |l|\n l.chomp!\n cur = root\n l.split(\"\\\\\").each do |name|\n cur = (cur[name] ||= {})\n end\nend\n\nfo = fi = 0\nroot.each do |k,v|\n v.each do |k2,v2|\n a, b = foo(v2)\n fo = [a-1, fo].max\n fi = [b, fi].max\n end\nend\n\nputs [fo, fi]*\" \"\n"}], "negative_code": [{"source_code": "#!/usr/bin/ruby\n\nclass Hash\n\tdef calc\n\t\tret = [0, 0]\n\t\tself.each_pair { |n, v|\n\t\t\tif n =~ /\\./\n\t\t\t\tret[1] += 1\n\t\t\telse\n\t\t\t\ttmp = v.calc\n\t\t\t\tret[0] += 1 + tmp[0]\n\t\t\t\tret[1] += tmp[1]\n\t\t\tend\n\t\t}\n\t\tret\n\tend\n\n\tdef files\n\t\tcnt = 0\n\t\tself.each_pair { |n, v|\n\t\t\tif n =~ /\\./\n\t\t\t\tcnt += 1\n\t\t\telse\n\t\t\t\tcnt += v.files\n\t\t\tend\n\t\t}\n\t\tcnt\n\tend\nend\n\ntree = {}\nARGF.read.each_line { |line|\n\tline.chomp!\n\tcur = tree\n\tline.split(\"\\\\\").each { |dir|\n\t\tcur[dir] ||= {}\n\t\tcur = cur[dir]\n\t}\n}\n\nputs tree.values.map { |drv| drv.values.map(&:calc) }.max_by { |a| a[0]}.join(' ')\n\n"}, {"source_code": "def is_file? name\n\tname.include?('.')\nend\n\ntop_dirs = {}\n\nwhile path = gets\n\thier = path.split('\\\\')\n\tdir = hier[0] + hier[1]\n\thier.shift(2)\n\ttop_dirs[dir] = {subdir: 0, files: 0} unless top_dirs[dir]\n\ttop_dir = top_dirs[dir]\n\thier.each do |name|\n\t\tif is_file? name\n\t\t\ttop_dir[:files] += 1\n\t\telse\n\t\t\ttop_dir[:subdir] += 1\n\t\tend\n\tend\nend\n\n\n\nbest_subdir_count = 0\nbest_subfile_count = 0\n\ntop_dirs.each_value do |dir|\n\tbest_subdir_count = [best_subdir_count, dir[:subdir].to_i].max\n\tbest_subfile_count = [best_subfile_count, dir[:files].to_i].max\nend\n\nputs best_subdir_count\nputs best_subfile_count\n"}], "src_uid": "e588d7600429eb6b70a1a9b5eca194f7"} {"nl": {"description": "There are n problems prepared for the next Codeforces round. They are arranged in ascending order by their difficulty, and no two problems have the same difficulty. Moreover, there are m pairs of similar problems. Authors want to split problems between two division according to the following rules: Problemset of each division should be non-empty. Each problem should be used in exactly one division (yes, it is unusual requirement). Each problem used in division 1 should be harder than any problem used in division 2. If two problems are similar, they should be used in different divisions. Your goal is count the number of ways to split problem between two divisions and satisfy all the rules. Two ways to split problems are considered to be different if there is at least one problem that belongs to division 1 in one of them and to division 2 in the other.Note, that the relation of similarity is not transitive. That is, if problem i is similar to problem j and problem j is similar to problem k, it doesn't follow that i is similar to k.", "input_spec": "The first line of the input contains two integers n and m (2 ≤ n ≤ 100 000, 0 ≤ m ≤ 100 000) — the number of problems prepared for the round and the number of pairs of similar problems, respectively. Each of the following m lines contains a pair of similar problems ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi). It's guaranteed, that no pair of problems meets twice in the input.", "output_spec": "Print one integer — the number of ways to split problems in two divisions.", "sample_inputs": ["5 2\n1 4\n5 2", "3 3\n1 2\n2 3\n1 3", "3 2\n3 1\n3 2"], "sample_outputs": ["2", "0", "1"], "notes": "NoteIn the first sample, problems 1 and 2 should be used in division 2, while problems 4 and 5 in division 1. Problem 3 may be used either in division 1 or in division 2.In the second sample, all pairs of problems are similar and there is no way to split problem between two divisions without breaking any rules.Third sample reminds you that the similarity relation is not transitive. Problem 3 is similar to both 1 and 2, but 1 is not similar to 2, so they may be used together."}, "positive_code": [{"source_code": "n, m = gets.split(' ').map{|v| v.to_i}\nhigh_two = 1\nlow_one = n\nfor i in 0...m\n\ttmp = gets.split(' ').map{|v| v.to_i}\n\thigh_two = [high_two, tmp.min].max\n\tlow_one = [low_one, tmp.max].min\nend\n\nputs [0, low_one - high_two].max"}, {"source_code": "n, m = gets.split.map(&:to_i)\n\nif m==0\n p n-1\n exit(0)\nend\nl=-100000000000\nr=1000000000000\nfor i in 1..m\n x, y = gets.split.map(&:to_i)\n x, y= y, x if x>y\n l = [l, x].max\n r = [r, y].min\nend\np [r-l, 0].max"}, {"source_code": "n, m = readline.split(' ').collect(&:to_i)\ncmin, cmax = 0, n - 2\nm.times do\n a, b = readline.split(' ').collect(&:to_i)\n a, b = [a - 1, b - 1].sort\n cmin = [cmin, a].max\n cmax = [cmax, b - 1].min\nend\nputs [cmax - cmin + 1, 0].max\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\na = Array.new(n + 1, 0)\nd1_max, d2_min = 0, n\nnone = false\nm.times do\n b = gets.split.map(&:to_i).sort\n if ( a[b[0]] == 1 || a[b[0]].zero? ) && ( a[b[1]] == 2 || a[b[1]].zero? )\n a[b[0]], a[b[1]] = 1, 2\n d1_max = [b[0], d1_max].max\n d2_min = [b[1], d2_min].min\n none = true if d1_max > d2_min\n else\n none = true\n end\nend\nputs none ? 0 : d2_min - d1_max - ( a.any?{|x| !x.zero?} ? 0 : 1 )\n"}, {"source_code": "parse_int = lambda { gets.split.map{|x| x.to_i} }\n\ntasks, pairs = parse_int.call\nstart, finish = 1, tasks\n\npairs.times {|_|\n pair = parse_int.call.sort\n start = pair[0] if start < pair[0]\n finish = pair[1] if finish > pair[1]\n}\n\n\n\n\n\n\n\n\n\n\nres = finish-start\nres = 0 if res<0\n\nputs res"}], "negative_code": [{"source_code": "n, m = gets.split.map(&:to_i)\nl=-1000000000\nr=1000000000\nfor i in 1..m\n x, y = gets.split.map(&:to_i)\n x, y= y, x if x>y\n l = [l, x].max\n r = [r, y].min\nend\np r-l"}, {"source_code": "n, m = gets.split.map(&:to_i)\n\nif m==0\n p 1\n exit(0)\nend\nl=-1000000000\nr=1000000000\nfor i in 1..m\n x, y = gets.split.map(&:to_i)\n x, y= y, x if x>y\n l = [l, x].max\n r = [r, y].min\nend\np r-l"}, {"source_code": "n, m = gets.split.map(&:to_i)\n\nif m==0\n p n-1\n exit(0)\nend\nl=-1000000000\nr=1000000000\nfor i in 1..m\n x, y = gets.split.map(&:to_i)\n x, y= y, x if x>y\n l = [l, x].max\n r = [r, y].min\nend\np r-l"}, {"source_code": "n, m = gets.split.map(&:to_i)\nl=-1000000000\nr=1000000000\nfor i in 1..m\n x, y = gets.split.map(&:to_i)\n x, y= y, x if x>y\n l = [l, x].max\n r = [r, y].min\nend\np r-l"}, {"source_code": "n, m = gets.split.map(&:to_i)\na = Array.new(n + 1, 0)\nnone = false\nm.times do\n b = gets.split.map(&:to_i).sort\n if ( a[b[0]] == 1 || a[b[0]].zero? ) && ( a[b[1]] == 2 || a[b[1]].zero? )\n a[b[0]], a[b[1]] = 1, 2\n else\n none = true\n end\nend\np none ? 0 : 2 ** a[1..-1].count(&:zero?)\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\na = Array.new(n + 1, 0)\nd1_max, d2_min = 0, n\nnone = false\nm.times do\n b = gets.split.map(&:to_i).sort\n if ( a[b[0]] == 1 || a[b[0]].zero? ) && ( a[b[1]] == 2 || a[b[1]].zero? )\n a[b[0]], a[b[1]] = 1, 2\n d1_max = [b[0], d1_max].max\n d2_min = [b[1], d2_min].min\n none = true if d1_max > d2_min\n else\n none = true\n end\nend\nputs none ? 0 : 2 ** a[1..-1].count(&:zero?)\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\na = Array.new(n + 1, 0)\nd1_max, d2_min = 0, n\nnone = false\nm.times do\n b = gets.split.map(&:to_i).sort\n if ( a[b[0]] == 1 || a[b[0]].zero? ) && ( a[b[1]] == 2 || a[b[1]].zero? )\n a[b[0]], a[b[1]] = 1, 2\n d1_max = [b[0], d1_max].max\n d2_min = [b[1], d2_min].min\n none = true if d1_max > d2_min\n else\n none = true\n end\nend\nans = 2 ** a[1..-1].count(&:zero?) - (a.any?{|x| !x.zero?} ? 0 : 2)\nputs none ? 0 : ans\n"}], "src_uid": "b54ced81e152a28c19e0068cf6a754f6"} {"nl": {"description": "This is the easy version of the problem. The difference between the versions is the constraint on $$$n$$$ and the required number of operations. You can make hacks only if all versions of the problem are solved.There are two binary strings $$$a$$$ and $$$b$$$ of length $$$n$$$ (a binary string is a string consisting of symbols $$$0$$$ and $$$1$$$). In an operation, you select a prefix of $$$a$$$, and simultaneously invert the bits in the prefix ($$$0$$$ changes to $$$1$$$ and $$$1$$$ changes to $$$0$$$) and reverse the order of the bits in the prefix.For example, if $$$a=001011$$$ and you select the prefix of length $$$3$$$, it becomes $$$011011$$$. Then if you select the entire string, it becomes $$$001001$$$.Your task is to transform the string $$$a$$$ into $$$b$$$ in at most $$$3n$$$ operations. It can be proved that it is always possible.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1\\le t\\le 1000$$$)  — the number of test cases. Next $$$3t$$$ lines contain descriptions of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1\\le n\\le 1000$$$)  — the length of the binary strings. The next two lines contain two binary strings $$$a$$$ and $$$b$$$ of length $$$n$$$. It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$1000$$$.", "output_spec": "For each test case, output an integer $$$k$$$ ($$$0\\le k\\le 3n$$$), followed by $$$k$$$ integers $$$p_1,\\ldots,p_k$$$ ($$$1\\le p_i\\le n$$$). Here $$$k$$$ is the number of operations you use and $$$p_i$$$ is the length of the prefix you flip in the $$$i$$$-th operation.", "sample_inputs": ["5\n2\n01\n10\n5\n01011\n11100\n2\n01\n01\n10\n0110011011\n1000110100\n1\n0\n1"], "sample_outputs": ["3 1 2 1\n6 5 2 5 3 1 2\n0\n9 4 1 2 10 4 1 2 1 5\n1 1"], "notes": "NoteIn the first test case, we have $$$01\\to 11\\to 00\\to 10$$$.In the second test case, we have $$$01011\\to 00101\\to 11101\\to 01000\\to 10100\\to 00100\\to 11100$$$.In the third test case, the strings are already the same. Another solution is to flip the prefix of length $$$2$$$, which will leave $$$a$$$ unchanged."}, "positive_code": [{"source_code": "gets.to_i.times do\n n = gets.to_i\n a, b = gets, gets\n res = []\n (0.. n).each do |i|\n if a[i] != b[i]\n res << i + 1 if i > 0\n res << 1\n res << i + 1 if i > 0\n end\n end\n puts res.size, res.join(' ')\nend\n "}], "negative_code": [], "src_uid": "10c9b2d70030f7ed680297455d1f1bb0"} {"nl": {"description": "We all know that a superhero can transform to certain other superheroes. But not all Superheroes can transform to any other superhero. A superhero with name $$$s$$$ can transform to another superhero with name $$$t$$$ if $$$s$$$ can be made equal to $$$t$$$ by changing any vowel in $$$s$$$ to any other vowel and any consonant in $$$s$$$ to any other consonant. Multiple changes can be made.In this problem, we consider the letters 'a', 'e', 'i', 'o' and 'u' to be vowels and all the other letters to be consonants.Given the names of two superheroes, determine if the superhero with name $$$s$$$ can be transformed to the Superhero with name $$$t$$$.", "input_spec": "The first line contains the string $$$s$$$ having length between $$$1$$$ and $$$1000$$$, inclusive. The second line contains the string $$$t$$$ having length between $$$1$$$ and $$$1000$$$, inclusive. Both strings $$$s$$$ and $$$t$$$ are guaranteed to be different and consist of lowercase English letters only.", "output_spec": "Output \"Yes\" (without quotes) if the superhero with name $$$s$$$ can be transformed to the superhero with name $$$t$$$ and \"No\" (without quotes) otherwise. You can print each letter in any case (upper or lower).", "sample_inputs": ["a\nu", "abc\nukm", "akm\nua"], "sample_outputs": ["Yes", "Yes", "No"], "notes": "NoteIn the first sample, since both 'a' and 'u' are vowels, it is possible to convert string $$$s$$$ to $$$t$$$.In the third sample, 'k' is a consonant, whereas 'a' is a vowel, so it is not possible to convert string $$$s$$$ to $$$t$$$."}, "positive_code": [{"source_code": "def can_transform(s, t)\n\tvowels = ['a','e','i','o','u']\n\ts_a = s.split('')\n\tt_a = t.split('')\n\treturn \"No\" if s_a.length != t_a.length\n\ti = 0\n\twhile i < s_a.length do\n\t\tc1 = s_a[i]\n\t\tc2 = t_a[i]\n\t\treturn \"No\" if (vowels.include?(c1) && !vowels.include?(c2)) || (!vowels.include?(c1) && vowels.include?(c2))\n\t\ti = i + 1\n\tend\n\treturn \"Yes\"\nend\n\ns = gets.chomp\nt = gets.chomp\n\nputs can_transform(s, t)\n"}, {"source_code": "s = gets.strip\nt = gets.strip\nvowels = %w{a e i o u}\nconsonents = ('a'..'z').to_a\nvowels.each do |v|\n consonents.delete v\nend\nvalid = true\nif s.length == t.length\n s.length.times do |i|\n curr = s[i]\n if vowels.include? curr\n valid = false if !vowels.include? t[i]\n else\n valid = false if !consonents.include? t[i]\n end\n end\nelse\n valid = false\nend\nputs valid ? \"Yes\" : \"No\""}, {"source_code": "s1 = gets.chomp\ns2 = gets.chomp\nif s1.size == s2.size\n t = 0\n s1.size.times do |i|\n if ['a', 'e', 'i', 'o', 'u'].include?(s1[i]) && !['a', 'e', 'i', 'o', 'u'].include?(s2[i]) ||\n !['a', 'e', 'i', 'o', 'u'].include?(s1[i]) && ['a', 'e', 'i', 'o', 'u'].include?(s2[i])\n t += 1\n end\n end\n if t < 1\n puts \"YES\"\n exit\n end\nend\nputs \"NO\""}, {"source_code": "a = gets\nb = gets \n\ndef sol(a,b)\n\tvowels = ['a', 'e', 'i', 'o', 'u']\n\n\tif a.size != b.size \n\t return 'No'\n\tend\n\n\ta.each_char.with_index(0){|char, i|\n\t\tb_char = b[i]\n\t\tif ((vowels.include? char) && (!vowels.include? b_char)) || ((vowels.include? b_char) && (!vowels.include? char))\n\t\t\treturn 'No'\n\t\tend\n\t}\n\n\treturn 'Yes'\nend\n\nputs sol(a,b)"}, {"source_code": "s = gets\nt = gets\n\ndef pat(s)\n s.chars.map {|c| ['a','e','i','o','u'].include?(c) }\nend\n\nmat = pat(s) == pat(t)\n\nputs mat ? \"Yes\" : \"No\"\n"}, {"source_code": "x = gets.chomp\ny = gets.chomp\nif x.size != y.size\n puts \"No\"\n exit\nend\nif x == y\n puts \"Yes\"\n exit\nend\n\nvowels = ['a', 'e', 'i', 'o', 'u']\nfor i in 0...x.size\n if x[i] != y[i]\n if vowels.include? y[i] and not vowels.include? x[i]\n puts \"No\"\n exit\n elsif not vowels.include? y[i] and vowels.include? x[i]\n puts \"No\"\n exit\n end\n end\nend\nputs \"Yes\"\n\n\n"}, {"source_code": "if File.exists?(\"input\")\n $stdin = File.open(\"input\")\n $stdout = File.open(\"output\",\"w\")\nend\n\nname1 = gets.chomp\nname2 = gets.chomp\n\ndef isvowel(ch)\n\treturn true if [\"a\",\"e\",\"i\",\"o\",\"u\"].include?(ch)\n\tfalse\nend\n\nif name1.length == name2.length\n\t(0..name1.length).each do |i|\n\t\tiv1 = isvowel(name1[i])\n\t\tiv2 = isvowel(name2[i])\n\t\tif (iv1 and not iv2) or (iv2 and not iv1)\n\t\t\tputs \"No\"\n\t\t\texit\n\t\tend\n\tend\n\tputs \"Yes\"\nelse\n\tputs \"No\"\nend"}, {"source_code": "# run on ruby 2.0.0p645\n\ndef main()\n s = gets\n t = gets\n \n ans = solve(s, t)\n puts ans\nend\n\ndef solve(s, t)\n # https://docs.ruby-lang.org/ja/latest/method/String/i/gsub.html\n\n c = /[^aeiou]/\n v = /[aeiou]/\n normalizedS = s.gsub(c, 'C').gsub(v, 'V')\n normalizedT = t.gsub(c, 'C').gsub(v, 'V')\n if normalizedS === normalizedT\n return 'Yes'\n else\n return 'No'\n end\nend\n\nmain()\n"}], "negative_code": [{"source_code": "puts 'hello, world.'"}], "src_uid": "2b346d5a578031de4d19edb4f8f2626c"} {"nl": {"description": "There is a bus stop near the university. The lessons are over, and n students come to the stop. The i-th student will appear at the bus stop at time ti (all ti's are distinct).We shall assume that the stop is located on the coordinate axis Ox, at point x = 0, and the bus goes along the ray Ox, that is, towards the positive direction of the coordinate axis, and back. The i-th student needs to get to the point with coordinate xi (xi > 0).The bus moves by the following algorithm. Initially it is at point 0. The students consistently come to the stop and get on it. The bus has a seating capacity which is equal to m passengers. At the moment when m students get on the bus, it starts moving in the positive direction of the coordinate axis. Also it starts moving when the last (n-th) student gets on the bus. The bus is moving at a speed of 1 unit of distance per 1 unit of time, i.e. it covers distance y in time y.Every time the bus passes the point at which at least one student needs to get off, it stops and these students get off the bus. The students need 1 + [k / 2] units of time to get off the bus, where k is the number of students who leave at this point. Expression [k / 2] denotes rounded down k / 2. As soon as the last student leaves the bus, the bus turns around and goes back to the point x = 0. It doesn't make any stops until it reaches the point. At the given point the bus fills with students once more, and everything is repeated.If students come to the stop when there's no bus, they form a line (queue) and get on the bus in the order in which they came. Any number of students get on the bus in negligible time, you should assume that it doesn't take any time. Any other actions also take no time. The bus has no other passengers apart from the students.Write a program that will determine for each student the time when he got off the bus. The moment a student got off the bus is the moment the bus stopped at the student's destination stop (despite the fact that the group of students need some time to get off).", "input_spec": "The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of students and the number of passengers the bus can transport, correspondingly. Next n lines contain descriptions of the students, one per line. Each line contains a pair of integers ti, xi (1 ≤ ti ≤ 105, 1 ≤ xi ≤ 104). The lines are given in the order of strict increasing of ti. Values of xi can coincide.", "output_spec": "Print n numbers w1, w2, ..., wn, wi — the moment of time when the i-th student got off the bus. Print the numbers on one line and separate them with single spaces.", "sample_inputs": ["1 10\n3 5", "2 1\n3 5\n4 5", "5 4\n3 5\n4 5\n5 5\n6 5\n7 1", "20 4\n28 13\n31 13\n35 6\n36 4\n52 6\n53 4\n83 2\n84 4\n87 1\n93 6\n108 4\n113 6\n116 1\n125 2\n130 2\n136 13\n162 2\n166 4\n184 1\n192 2"], "sample_outputs": ["8", "8 19", "11 11 11 11 20", "51 51 43 40 93 89 86 89 114 121 118 121 137 139 139 152 195 199 193 195"], "notes": "NoteIn the first sample the bus waits for the first student for 3 units of time and drives him to his destination in additional 5 units of time. So the student leaves the bus at the moment of time 3 + 5 = 8.In the second sample the capacity of the bus equals 1, that's why it will drive the first student alone. This student is the same as the student from the first sample. So the bus arrives to his destination at the moment of time 8, spends 1 + [1 / 2] = 1 units of time on getting him off, and returns back to 0 in additional 5 units of time. That is, the bus returns to the bus stop at the moment of time 14. By this moment the second student has already came to the bus stop. So he immediately gets in the bus, and is driven to his destination in additional 5 units of time. He gets there at the moment 14 + 5 = 19. In the third sample the bus waits for the fourth student for 6 units of time, then drives for 5 units of time, then gets the passengers off for 1 + [4 / 2] = 3 units of time, then returns for 5 units of time, and then drives the fifth student for 1 unit of time."}, "positive_code": [{"source_code": "class Stud\n attr_reader :i,:t,:x\n attr_accessor :t_out\n def initialize i,t,x\n @i,@t,@x = i,t,x\n @t_out = nil\n end\n def <=> b\n x <=> b.x\n end\n def to_s\n \"stud i=#{i}, t=#{t}, x=#{x}\"\n end\nend\nn,m = gets.split.map &:to_i\nst = []\n0.upto(n-1) do |i|\n t,x = gets.split.map &:to_i\n st << Stud.new(i,t,x)\nend\nt = 0\nans = []\nuntil st.empty?\n bus = []\n until bus.size == m or st.empty?\n stud = st.shift\n t = [t,stud.t].max\n bus << stud\n end\n bus.sort!\n x = 0\n until bus.empty?\n t += bus[0].x-x\n x = bus[0].x\n outed = 0\n until bus.empty? or bus[0].x > x\n outed += 1\n bus[0].t_out = t\n ans << bus[0]\n bus.shift\n end\n t += 1+outed/2\n end\n t += x\nend\nans.sort_by! {|stud| stud.i}\nputs ans.collect {|x| x.t_out}.join ' '\n\n\n"}, {"source_code": "class Bus\n @@ar = []\n @@pos = []\n @@m, @@time, @@done = 0, 0, 0\n\n def initialize m\n @@m = m\n end\n\n def grab(queue, pos)\n cur = 0\n @@pos = []\n while pos < queue.length and cur < @@m\n @@ar[queue[pos][1]] ||= []\n @@ar[queue[pos][1]] << [queue[pos][0], pos]\n @@pos << queue[pos][1]\n pos, cur = pos + 1, cur + 1\n end\n @@time = [@@time, queue[pos - 1][0]].max\n @@pos.sort!.uniq!\n pos\n end\n\n def unload\n prev = 0\n @k = []\n @@pos.each do |i|\n @@time += i - prev\n prev = i\n @@ar[i].each{ |t, index| @k << [@@time, index] }\n @@done += @@ar[i].length\n @@time += 1 + (@@ar[i].length / 2)\n @@ar[i] = []\n end\n @@time += prev\n return @k\n end\n\n def done\n return @@done\n end\nend\n\n\nn, m = gets.split.map{|i| i.to_i}\nqueue, answer = [], []\n(1..n).each{ |i| queue[i] = gets.split.map{|j| j.to_i} }\nbus = Bus.new m\npos = 1\nwhile bus.done < queue.length - 1\n pos = bus.grab(queue, pos)\n bus.unload.each{ |ans, index| answer[index] = ans }\nend\n(1..n).each do |i|\n print answer[i].to_s + ' '\nend\n"}], "negative_code": [], "src_uid": "bb2ee9d4f718116ec391c93af58ec012"} {"nl": {"description": "You are given two strings $$$s$$$ and $$$t$$$. In a single move, you can choose any of two strings and delete the first (that is, the leftmost) character. After a move, the length of the string decreases by $$$1$$$. You can't choose a string if it is empty.For example: by applying a move to the string \"where\", the result is the string \"here\", by applying a move to the string \"a\", the result is an empty string \"\". You are required to make two given strings equal using the fewest number of moves. It is possible that, in the end, both strings will be equal to the empty string, and so, are equal to each other. In this case, the answer is obviously the sum of the lengths of the initial strings.Write a program that finds the minimum number of moves to make two given strings $$$s$$$ and $$$t$$$ equal.", "input_spec": "The first line of the input contains $$$s$$$. In the second line of the input contains $$$t$$$. Both strings consist only of lowercase Latin letters. The number of letters in each string is between 1 and $$$2\\cdot10^5$$$, inclusive.", "output_spec": "Output the fewest number of moves required. It is possible that, in the end, both strings will be equal to the empty string, and so, are equal to each other. In this case, the answer is obviously the sum of the lengths of the given strings.", "sample_inputs": ["test\nwest", "codeforces\nyes", "test\nyes", "b\nab"], "sample_outputs": ["2", "9", "7", "1"], "notes": "NoteIn the first example, you should apply the move once to the first string and apply the move once to the second string. As a result, both strings will be equal to \"est\".In the second example, the move should be applied to the string \"codeforces\" $$$8$$$ times. As a result, the string becomes \"codeforces\" $$$\\to$$$ \"es\". The move should be applied to the string \"yes\" once. The result is the same string \"yes\" $$$\\to$$$ \"es\".In the third example, you can make the strings equal only by completely deleting them. That is, in the end, both strings will be empty.In the fourth example, the first character of the second string should be deleted."}, "positive_code": [{"source_code": "s1 = STDIN.readline.strip\ns2 = STDIN.readline.strip\n\ns1_chars = s1.reverse.split('')\ns2_chars = s2.reverse.split('')\n\nwhile s1_chars[0] == s2_chars[0] and s1_chars.length > 0 and s1_chars.length > 0\n s1_chars.shift if s1_chars.length > 0\n s2_chars.shift if s2_chars.length > 0\nend\n\nputs s1_chars.length + s2_chars.length\n"}, {"source_code": "s = gets.chomp.split('')\nt = gets.chomp.split('')\n\ni1 = s.length - 1\ni2 = t.length - 1\n\nwhile (i1 >= 0 && i2 >= 0)\n\t# puts \"#{s[i1]} #{t[i2]}\"\n\tif s[i1] != t[i2]\n\t\tbreak\n\telse\n\t\ti1 -= 1\n\t\ti2 -= 1\n\tend\nend\n\nprint (i1 + i2 + 2)"}, {"source_code": "s = gets.chomp\nt = gets.chomp\n\ns = s.reverse\nt = t.reverse\n\nn = [s.length, t.length].min\nsame_characters = 0\n\nn.times do |i|\n break if s[i] != t[i]\n same_characters += 2\nend\n\nanswer = (s.length + t.length) - same_characters\nputs answer\n"}, {"source_code": "s = gets.chomp.reverse\nt = gets.chomp.reverse\nn = s.size.times.take_while { s[_1] == t[_1] }.size\nputs s.size + t.size - n * 2\n"}, {"source_code": "a,b=$<.map{|e|e.chomp.reverse}\nlas=[a.size,b.size].min\nidx=(0...las).find{|i|a[i]!=b[i]}||las\np a.size+b.size-idx*2"}, {"source_code": " s1 = gets.chomp.reverse\n s2 = gets.chomp.reverse\n\n total_lenght = s1.size + s2.size\n\n common_length = 0\n\n i = 0\n while true\n\n if s1[i] && s1[i] == s2[i]\n i += 1\n common_length = i\n else\n break\n end\n end\n\n puts total_lenght - 2 * common_length\n"}, {"source_code": "s = gets.chomp.reverse\nt = gets.chomp.reverse\nans = s.length + t.length\n[s.length, t.length].min.times do |i|\n if s[i] == t[i]\n ans -= 2\n else\n break\n end\nend\nputs ans"}, {"source_code": "str1 = gets.strip\nstr2 = gets.strip\n\nstrl = (str1.size > str2.size ? str1 : str2)\nstrs = (str1.size > str2.size ? str2 : str1)\nmxn = strl.size\nmnn = strs.size\n\nbis = nil\nbil = nil\n(mnn-1).downto(0).each do |si|\n li = si + (mxn - mnn)\n if strl[li] != strs[si]\n bis = si\n bil = li\n break\n end\nend\n\nif bis.nil?\n puts mxn-mnn\nelse\n puts bis+bil+2\nend"}, {"source_code": "a = gets.strip\nb = gets.strip\ni = a.size - 1\nj = b.size - 1\nwhile i >= 0 && j >= 0 && a[i] == b[j] do\n i-=1\n j-=1\nend\nputs i + j + 2\n"}, {"source_code": "\nx = gets.chomp();\ny = gets.chomp();\nsum = x.size() + y.size();\nx.reverse!\ny.reverse!\nfor i in 0 ... x.size()\n if(x[i] == y[i])\n sum-=2;\n else\n break\n end\nend\nputs sum\n\n"}], "negative_code": [{"source_code": "s = gets.chomp\nt = gets.chomp\n\ns = s.reverse\nt = t.reverse\n\nn = [s.length, t.length].min\nsame_characters = 0\n\nfor i in 0..n\n if s[i] == t[i]\n same_characters += 2\n else\n break\n end\n i += 1\nend\n\nanswer = (s.length + t.length) - same_characters\nputs answer\n"}], "src_uid": "59d926bca19a6dcfe3eb3e3dc03fffd6"} {"nl": {"description": " Frodo was caught by Saruman. He tore a pouch from Frodo's neck, shook out its contents —there was a pile of different rings: gold and silver...\"How am I to tell which is the One?!\" the mage howled.\"Throw them one by one into the Cracks of Doom and watch when Mordor falls!\" Somewhere in a parallel Middle-earth, when Saruman caught Frodo, he only found $$$n$$$ rings. And the $$$i$$$-th ring was either gold or silver. For convenience Saruman wrote down a binary string $$$s$$$ of $$$n$$$ characters, where the $$$i$$$-th character was 0 if the $$$i$$$-th ring was gold, and 1 if it was silver.Saruman has a magic function $$$f$$$, which takes a binary string and returns a number obtained by converting the string into a binary number and then converting the binary number into a decimal number. For example, $$$f(001010) = 10, f(111) = 7, f(11011101) = 221$$$.Saruman, however, thinks that the order of the rings plays some important role. He wants to find $$$2$$$ pairs of integers $$$(l_1, r_1), (l_2, r_2)$$$, such that: $$$1 \\le l_1 \\le n$$$, $$$1 \\le r_1 \\le n$$$, $$$r_1-l_1+1\\ge \\lfloor \\frac{n}{2} \\rfloor$$$ $$$1 \\le l_2 \\le n$$$, $$$1 \\le r_2 \\le n$$$, $$$r_2-l_2+1\\ge \\lfloor \\frac{n}{2} \\rfloor$$$ Pairs $$$(l_1, r_1)$$$ and $$$(l_2, r_2)$$$ are distinct. That is, at least one of $$$l_1 \\neq l_2$$$ and $$$r_1 \\neq r_2$$$ must hold. Let $$$t$$$ be the substring $$$s[l_1:r_1]$$$ of $$$s$$$, and $$$w$$$ be the substring $$$s[l_2:r_2]$$$ of $$$s$$$. Then there exists non-negative integer $$$k$$$, such that $$$f(t) = f(w) \\cdot k$$$.Here substring $$$s[l:r]$$$ denotes $$$s_ls_{l+1}\\ldots s_{r-1}s_r$$$, and $$$\\lfloor x \\rfloor$$$ denotes rounding the number down to the nearest integer.Help Saruman solve this problem! It is guaranteed that under the constraints of the problem at least one solution exists.", "input_spec": "Each test contains multiple test cases. The first line contains one positive integer $$$t$$$ ($$$1 \\le t \\le 10^3$$$), denoting the number of test cases. Description of the test cases follows. The first line of each test case contains one positive integer $$$n$$$ ($$$2 \\le n \\le 2 \\cdot 10^4$$$) — length of the string. The second line of each test case contains a non-empty binary string of length $$$n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.", "output_spec": "For every test case print four integers $$$l_1$$$, $$$r_1$$$, $$$l_2$$$, $$$r_2$$$, which denote the beginning of the first substring, the end of the first substring, the beginning of the second substring, and the end of the second substring, respectively. If there are multiple solutions, print any.", "sample_inputs": ["7\n6\n101111\n9\n111000111\n8\n10000000\n5\n11011\n6\n001111\n3\n101\n30\n100000000000000100000000000000"], "sample_outputs": ["3 6 1 3\n1 9 4 9\n5 8 1 4\n1 5 3 5\n1 6 2 4\n1 2 2 3\n1 15 16 30"], "notes": "NoteIn the first testcase $$$f(t) = f(1111) = 15$$$, $$$f(w) = f(101) = 5$$$.In the second testcase $$$f(t) = f(111000111) = 455$$$, $$$f(w) = f(000111) = 7$$$.In the third testcase $$$f(t) = f(0000) = 0$$$, $$$f(w) = f(1000) = 8$$$.In the fourth testcase $$$f(t) = f(11011) = 27$$$, $$$f(w) = f(011) = 3$$$.In the fifth testcase $$$f(t) = f(001111) = 15$$$, $$$f(w) = f(011) = 3$$$."}, "positive_code": [{"source_code": "t = gets.to_i\nt.times do\n\nn = gets.to_i\ns = gets.chomp.chars.map!(&:to_i)\n\ni0 = s.find_index(0) || n\n\nif i0 <= n.pred / 2\n a = [i0 + 1, n, i0 + 2, n]\nelsif i0 < n\n a = [1, i0 + 1, 1, i0]\nelse\n a = [1, n - 1, 2, n]\nend\n\nputs a * \" \"\n\nend\n"}], "negative_code": [], "src_uid": "eadc7f5e1043ac431984ec921c62892d"} {"nl": {"description": "There are $$$n$$$ points on the plane, $$$(x_1,y_1), (x_2,y_2), \\ldots, (x_n,y_n)$$$.You need to place an isosceles triangle with two sides on the coordinate axis to cover all points (a point is covered if it lies inside the triangle or on the side of the triangle). Calculate the minimum length of the shorter side of the triangle.", "input_spec": "First line contains one integer $$$n$$$ ($$$1 \\leq n \\leq 10^5$$$). Each of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \\leq x_i,y_i \\leq 10^9$$$).", "output_spec": "Print the minimum length of the shorter side of the triangle. It can be proved that it's always an integer.", "sample_inputs": ["3\n1 1\n1 2\n2 1", "4\n1 1\n1 2\n2 1\n2 2"], "sample_outputs": ["3", "4"], "notes": "NoteIllustration for the first example: Illustration for the second example: "}, "positive_code": [{"source_code": "gets;p$<.map{|e|e.split.map(&:to_i).reduce :+}.max"}, {"source_code": "\ndef solution\n n = read_int\n max_b = 0\n n.times do\n x, y = read_ints\n b = x + y\n max_b = b if b > max_b\n end\n puts max_b\nend\n\ndef read_int\n gets.to_i\nend\n\ndef read_ints\n gets.split.map(&:to_i)\nend\n\ndef read_string\n gets.chomp\nend\n\nsolution unless ENV['TEST__MODE']\n\n"}, {"source_code": "n = gets.to_i\nans = 0\nn.times do\n x,y = gets.split.map(&:to_i)\n ans = x+y if ans < x+y\nend\nputs ans\n"}, {"source_code": "n = gets.to_i\nans = 0\nn.times do\n x, y = gets.split.map(&:to_i)\n ans = x + y if x + y > ans\nend\nputs ans"}, {"source_code": "puts gets.to_i.times.inject(0) { |s| [s, gets.split.map(&:to_i).inject(0) { |s, x| s + x }].max }\n"}, {"source_code": "p gets.to_i.times.map { gets.split.map(&:to_i).inject :+ }.max\n"}, {"source_code": "n = gets.to_i\nans = 0\nn.times do\n a = gets.split\n w = a[0].to_i+a[1].to_i\n ans = w if w > ans\nend\np ans"}], "negative_code": [{"source_code": "n = gets.to_i\nprint 1, ' ', n%3 == 2 ? 2 : 1, ' ', n%3 == 2 ? n - 3 : n - 2\n"}], "src_uid": "7c41fb6212992d1b3b3f89694b579fea"} {"nl": {"description": "There are n lights aligned in a row. These lights are numbered 1 to n from left to right. Initially some of the lights are switched on. Shaass wants to switch all the lights on. At each step he can switch a light on (this light should be switched off at that moment) if there's at least one adjacent light which is already switched on. He knows the initial state of lights and he's wondering how many different ways there exist to switch all the lights on. Please find the required number of ways modulo 1000000007 (109 + 7).", "input_spec": "The first line of the input contains two integers n and m where n is the number of lights in the sequence and m is the number of lights which are initially switched on, (1 ≤ n ≤ 1000, 1 ≤ m ≤ n). The second line contains m distinct integers, each between 1 to n inclusive, denoting the indices of lights which are initially switched on.", "output_spec": "In the only line of the output print the number of different possible ways to switch on all the lights modulo 1000000007 (109 + 7).", "sample_inputs": ["3 1\n1", "4 2\n1 4", "11 2\n4 8"], "sample_outputs": ["1", "2", "6720"], "notes": null}, "positive_code": [{"source_code": "def floor(n, mod)\n return 1 if n == 1\n return n * floor(n-1, mod)\nend\nn, m = gets.split(' ').map { |i| i.to_i }\nmod = 1000000007\nlight = gets.split(' ').map { |i| i.to_i}.sort\nlight.insert(0, 0)\nlight.push(n+1)\nsegment = []\nfor i in 1...light.size\n s = light[i] - light[i-1] - 1\n segment.push s if s != 0\nend\ntotal = segment.inject { |mem, var| mem += var}\ntotal = 1 if total.nil?\nc1 = c2 = c3 = 1\nfor i in 0...segment.size\n c1 = c1 * floor(segment[i], mod)\n next if (i == 0 and light[1] != 1) or (i == segment.size - 1 and light[m] != n)\n c2 = c2 * (2**(segment[i]-1))\nend\nputs (floor(total, mod) / c1 * c2) % mod"}, {"source_code": "def pow2 p\n\tt = 1\n\t(0...p).each{\n\t\tt = (t*2)%1000000007\n\t}\n\treturn t\nend\n\ndef fact n\n\tt = 1\n\t(0...p).each{\n\t\tt = (t*n)%1000000007\n\t}\n\treturn t\nend\n\ndef comb n,k\n\ta = [k,n-k].max\n\tb = [k,n-k].min\n\tt = 1\n\tp = 2\n\t(a+1..n).each{|k|\n\t\tt = t*k\n\t\twhile t%p==0 and p<=b\n\t\t\tt = t/p\n\t\t\tp += 1\n\t\tend\n\t}\n\treturn t%1000000007\nend\n\nn,m=gets.split(\" \").collect! {|x| x.to_i}\narr = [0]*n\nline = gets.split(\" \").collect! {|x| x.to_i}\nline.each {|v|\n\tarr[v-1] = 1\n}\n\nintervals = []\ncrt = 0\n(0...n).each {|k|\n\tif arr[k]==0\n\t\tcrt += 1\n\telse\n\t\tif crt!=0\n\t\t\tintervals.push crt\n\t\tend\n\t\tcrt = 0\n\tend\n}\nif crt!= 0\n\tintervals.push crt\nend\npp = intervals.collect {|x| pow2(x-1)}\nif arr[0] == 0\n\tpp[0] = 1\nend\n\nif arr[-1] == 0\n\tpp[-1] = 1\nend\n\nll = pp.size\nres = 1\nt = n - m\n(0...ll).each {|y|\n #puts \" - #{pp[y]} #{intervals[y]} #{t} #{comb(t,intervals[y])}\"\n\t#puts res\n\tres = (res*pp[y])%1000000007\n\tif y -1\n ret = silnia s[m]\n for i in a\n ret /= silnia i\n end\n \n# p n-em-a0-am-m+1\n# \n# p ret\n \n if 0 == ab[0]\n m+=1\n end\n \n if 0 == ab[ab.length-1]\n m+=1\n end\n \n if 0 == m\n p ret%1000000007\n else\n p((ret * 2**(n-em-a0-am-m+1))%1000000007)\n end\nelse\n p 1\nend\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"}, {"source_code": "#!/usr/bin/ruby\n\nclass Solver\n def factorial(n)\n f = 1\n 2.upto(n) {|i| f *= i }\n return f\n end\n\n def solve(lights)\n n = lights.length\n off = n\n lights.each {|v| off -= 1 if v }\n\n return 0 if off == n\n\n ans = factorial(off)\n off = 0\n den = 1\n 0.upto(n) do |i|\n if i == n\n den *= factorial(off)\n break\n end\n\n if lights[i]\n den *= factorial(off)\n off = 0\n else\n off += 1\n end\n end\n\n ans /= den\n flag = true\n 0.upto(n) do |i|\n if flag && lights[i]\n flag = false\n off = 0\n end\n\n if lights[i]\n ans *= 2 ** (off - 1) if off > 0\n off = 0\n else\n off += 1\n end\n end\n\n ans\n end\nend\n\ntokens = gets.split(\" \")\nn = tokens[0].to_i\nm = tokens[1].to_i\nlights = Array.new(n, false)\n\nif m > 0\n gets.split(\" \").map(&:to_i).each {|v| lights[v-1] = true }\nend\n\nsolver = Solver.new\nputs solver.solve(lights)%1000000007\n\n"}], "negative_code": [{"source_code": "def floor(n, mod)\n return 1 if n == 1\n return n * floor(n-1, mod) % mod\nend\nn, m = gets.split(' ').map { |i| i.to_i }\nmod = 1000000007\nlight = gets.split(' ').map { |i| i.to_i}.sort\nlight.insert(0, 0)\nlight.push(n+1)\nsegment = []\nfor i in 1...light.size\n s = light[i] - light[i-1] - 1\n segment.push s if s != 0\nend\ntotal = segment.inject { |mem, var| mem += var}\nc1 = c2 = c3 = 1\nfor i in 0...segment.size\n c1 = c1 * floor(segment[i], mod) % mod\n c2 = c2 * (2**(segment[i]-1)) % mod if i != 0 and i != segment.size - 1\nend\nputs (floor(total, mod) / c1 * c2) % mod"}, {"source_code": "def floor(n, mod)\n return 1 if n == 1\n return n * floor(n-1, mod)\nend\nn, m = gets.split(' ').map { |i| i.to_i }\nmod = 1000000007\nlight = gets.split(' ').map { |i| i.to_i}.sort\nlight.insert(0, 0)\nlight.push(n+1)\nsegment = []\nfor i in 1...light.size\n s = light[i] - light[i-1] - 1\n segment.push s if s != 0\nend\ntotal = segment.inject { |mem, var| mem += var}\ntotal = 1 if total.nil?\nc1 = c2 = c3 = 1\nfor i in 0...segment.size\n c1 = c1 * floor(segment[i], mod)\n next if (i == 0 and light[1] != 1) or (i == segment.size - 1 and light[n-2] != n)\n c2 = c2 * (2**(segment[i]-1))\nend\nputs (floor(total, mod) / c1 * c2) % mod"}, {"source_code": "def floor(n, mod)\n return 1 if n == 1\n return n * floor(n-1, mod) % mod\nend\nn, m = gets.split(' ').map { |i| i.to_i }\nmod = 1000000007\nlight = gets.split(' ').map { |i| i.to_i}.sort\nlight.insert(0, 0)\nlight.push(n+1)\nsegment = []\nfor i in 1...light.size\n s = light[i] - light[i-1] - 1\n segment.push s if s != 0\nend\ntotal = segment.inject { |mem, var| mem += var}\ntotal = 1 if total.nil?\nc1 = c2 = c3 = 1\nfor i in 0...segment.size\n c1 = c1 * floor(segment[i], mod) % mod\n next if (i == 0 and light[1] != 1) or (i == segment.size - 1 and light[n-2] != n)\n c2 = c2 * (2**(segment[i]-1)) % mod\nend\nputs (floor(total, mod) / c1 * c2) % mod"}, {"source_code": "def floor(n, mod)\n return 1 if n == 1\n return n * floor(n-1, mod) % mod\nend\nn, m = gets.split(' ').map { |i| i.to_i }\nmod = 1000000007\nlight = gets.split(' ').map { |i| i.to_i}.sort\nlight.insert(0, 0)\nlight.push(n+1)\nsegment = []\nfor i in 1...light.size\n s = light[i] - light[i-1] - 1\n segment.push s if s != 0\nend\ntotal = segment.inject { |mem, var| mem += var}\nc1 = c2 = c3 = 1\nfor i in 0...segment.size\n c1 = c1 * floor(segment[i], mod) % mod\n next if (i == 0 and not light[1] != 1) or (i == segment.size - 1 and not light[n-1] != n)\n c2 = c2 * (2**(segment[i]-1)) % mod\nend\nputs (floor(total, mod) / c1 * c2) % mod"}, {"source_code": "def floor(n, mod)\n\treturn 1 if n == 1\n\treturn n * floor(n-1, mod)\nend\nn, m = gets.split(' ').map { |i| i.to_i }\nmod = 1000000007\nlight = gets.split(' ').map { |i| i.to_i}.sort\nlight.insert(0, 0)\nlight.push(n+1)\nsegment = []\nfor i in 1...light.size\n\ts = light[i] - light[i-1] - 1\n\tsegment.push s if s != 0\nend\ntotal = segment.inject { |mem, var| mem += var}\ntotal = 1 if total.nil?\nc1 = c2 = c3 = 1\nfor i in 0...segment.size\n\tc1 = c1 * floor(segment[i], mod)\n\tnext if (i == 0 and light[1] != 1) or (i == segment.size - 1 and light[n-2] != n)\n\tc2 = c2 * (2**(segment[i]-1))\nend\nputs (floor(total, mod) / c1 * c2) % mod"}, {"source_code": "# mod = 1000000007;\n\n# for i in 0..99\n# p (2**i)%1000000007\n# end\n\nn = gets(' ').to_i\nm = gets.to_i\n\ninp=[0]\n(m-1).times{\n inp.push gets(' ').to_i\n}\ninp.push gets.to_i\n\ninp = inp.sort\n\na=[]\n\nfor i in 1..m\n a.push inp[i]-inp[i-1]-1\nend\na.push n-inp[m]\n\na0 = a[0]\nam = a[m]\nem = m\n\n# p a\n# p m\n\nm-=a.count 0\na=a-[0]\n\n# p a\n# p m\n\ndef silnia(x)\n ret=1\n for i in 2..x\n ret*=i\n end\n return ret\nend\n\ns=a.clone\n\nfor i in 1..s.length-1\n s[i] += s[i-1]\nend\n\n# p a\n# p s\n\nif m > -1\n ret = silnia s[m]\n for i in a\n ret /= silnia i\n end\n \n p (ret * 2**(n-em-a0-am-m+1))%1000000007\nelse\n p 1\nend\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"}, {"source_code": "# mod = 1000000007;\n\n# for i in 0..99\n# p (2**i)%1000000007\n# end\n\nn = gets(' ').to_i\nm = gets.to_i\n\ninp=[0]\n(m-1).times{\n inp.push gets(' ').to_i\n}\ninp.push gets.to_i\n\ninp = inp.sort\n\na=[]\n\nfor i in 1..m\n a.push inp[i]-inp[i-1]-1\nend\na.push n-inp[m]\n\na0 = a[0]\nam = a[m]\nem = m\n\n# p a\n# p m\n\nm-=a.count 0\na=a-[0]\n\n# p a\n# p m\n\ndef silnia(x)\n ret=1\n for i in 2..x\n ret*=i\n end\n return ret\nend\n\ns=a.clone\n\nfor i in 1..s.length-1\n s[i] += s[i-1]\nend\n\n# p a\n# p s\n\nif m > -1\n ret = silnia s[m]\n for i in a\n ret /= silnia i\n end\n \n# p n-em-a0-am-m+1\n \n if 0 == m\n p ret%1000000007\n else\n p (ret * 2**(n-em-a0-am-m+1))%1000000007\n end\nelse\n p 1\nend\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"}, {"source_code": "# mod = 1000000007;\n\n# for i in 0..99\n# p (2**i)%1000000007\n# end\n\nn = gets(' ').to_i\nm = gets.to_i\n\ninp=[0]\n(m-1).times{\n inp.push gets(' ').to_i\n}\ninp.push gets.to_i\n\na=[]\n\nfor i in 1..m\n a.push inp[i]-inp[i-1]-1\nend\na.push n-inp[m]\n\n# p a\n# p m\n\nm-=a.count 0\na=a-[0]\n\n# p a\n# p m\n\ndef silnia(x)\n ret=1\n for i in 2..x\n ret*=i\n end\n return ret\nend\n\ns=a.clone\n\nfor i in 1..s.length-1\n s[i] += s[i-1]\nend\n\n# p a\n# p s\n\nif m > -1\n ret = silnia s[m]\n for i in a\n ret /= silnia i\n end\n \n p ret * 2**(n-2*m-a[0]-a[m]+1)\nelse\n p 1\nend\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"}, {"source_code": "# mod = 1000000007;\n\n# for i in 0..99\n# p (2**i)%1000000007\n# end\n\nn = gets(' ').to_i\nm = gets.to_i\n\ninp=[0]\n(m-1).times{\n inp.push gets(' ').to_i\n}\ninp.push gets.to_i\n\ninp = inp.sort\n\na=[]\n\nfor i in 1..m\n a.push inp[i]-inp[i-1]-1\nend\na.push n-inp[m]\n\na0 = a[0]\nam = a[m]\nem = m\n\n# p a\n# p m\n\nm-=a.count 0\na=a-[0]\n\n# p a\n# p m\n\ndef silnia(x)\n ret=1\n for i in 2..x\n ret*=i\n end\n return ret\nend\n\ns=a.clone\n\nfor i in 1..s.length-1\n s[i] += s[i-1]\nend\n\n# p a\n# p s\n\nif m > -1\n ret = silnia s[m]\n for i in a\n ret /= silnia i\n end\n \n p (ret * 2**(n-2*em-a0-am+1))%1000000007\nelse\n p 1\nend\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"}, {"source_code": "# mod = 1000000007;\n\n# for i in 0..99\n# p (2**i)%1000000007\n# end\n\nn = gets(' ').to_i\nm = gets.to_i\n\ninp=[0]\n(m-1).times{\n inp.push gets(' ').to_i\n}\ninp.push gets.to_i\n\na=[]\n\nfor i in 1..m\n a.push inp[i]-inp[i-1]-1\nend\na.push n-inp[m]\n\na0 = a[0]\nam = a[m]\nem = m\n\n# p a\n# p m\n\nm-=a.count 0\na=a-[0]\n\n# p a\n# p m\n\ndef silnia(x)\n ret=1\n for i in 2..x\n ret*=i\n end\n return ret\nend\n\ns=a.clone\n\nfor i in 1..s.length-1\n s[i] += s[i-1]\nend\n\n# p a\n# p s\n\nif m > -1\n ret = silnia s[m]\n for i in a\n ret /= silnia i\n end\n \n p ret * 2**(n-2*em-a0-am+1)\nelse\n p 1\nend\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"}, {"source_code": "# mod = 1000000007;\n\n# for i in 0..99\n# p (2**i)%1000000007\n# end\n\nn = gets(' ').to_i\nm = gets.to_i\n\ninp=[0]\n(m-1).times{\n inp.push gets(' ').to_i\n}\ninp.push gets.to_i\n\na=[]\n\nfor i in 1..m\n a.push inp[i]-inp[i-1]-1\nend\na.push n-inp[m]\n\ndef silnia(x)\n ret=1\n for i in 2..x\n ret*=i\n end\n return ret\nend\n\ns=a.clone\n\nfor i in 1..s.length-1\n s[i] += s[i-1]\nend\n\n# p a\n# p s\n\nret = silnia s[m]\nfor i in a\n ret /= silnia i\nend\n\np ret * 2**(n-2*m-a[0]-a[m]+1)\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"}, {"source_code": "#!/usr/bin/ruby\n\nclass Solver\n def factorial(n)\n f = 1\n 2.upto(n) {|i| f *= i }\n return f\n end\n\n def solve(lights)\n n = lights.length\n off = n\n lights.each {|v| off -= 1 if v }\n\n return 0 if off == 0\n\n ans = factorial(off)\n off = 0\n den = 1\n 0.upto(n) do |i|\n if i == n\n den *= factorial(off)\n break\n end\n\n if lights[i]\n den *= factorial(off)\n off = 0\n else\n off += 1\n end\n end\n\n ans /= den\n flag = true\n 0.upto(n) do |i|\n if flag && lights[i]\n flag = false\n off = 0\n end\n\n if lights[i]\n ans *= 2 ** (off - 1) if off > 0\n off = 0\n else\n off += 1\n end\n end\n\n ans\n end\nend\n\ntokens = gets.split(\" \")\nn = tokens[0].to_i\nm = tokens[1].to_i\nlights = Array.new(n, false)\n\ngets.split(\" \").map(&:to_i).each {|v| lights[v-1] = true }\n\nsolver = Solver.new\nputs solver.solve(lights)\n\n"}, {"source_code": "#!/usr/bin/ruby\n\nclass Solver\n def factorial(n)\n f = 1\n 2.upto(n) {|i| f *= i }\n return f\n end\n\n def solve(lights)\n n = lights.length\n off = n\n 0.upto(n - 1) {|i| off -= 1 if lights[i] }\n\n ans = factorial(off)\n off = 0\n den = 1\n 0.upto(n) do |i|\n if i == n\n den *= factorial(off)\n break\n end\n\n if lights[i]\n den *= factorial(off)\n off = 0\n else\n off += 1\n end\n end\n\n ans /= den\n flag = true\n 0.upto(n) do |i|\n if flag && lights[i]\n flag = false\n off = 0\n end\n\n if lights[i]\n ans *= 2 ** (off - 1) if off > 0\n else\n off += 1\n end\n end\n\n ans\n end\nend\n\ntokens = gets.split(\" \")\nn = tokens[0].to_i\nm = tokens[1].to_i\nlights = Array.new(n, false)\n\ngets.split(\" \").map(&:to_i).each {|i| lights[i-1] = true }\n\nsolver = Solver.new\nputs solver.solve(lights)\n\n"}, {"source_code": "#!/usr/bin/ruby\n\nclass Solver\n def factorial(n)\n f = 1\n 2.upto(n) {|i| f *= i }\n return f\n end\n\n def solve(lights)\n n = lights.length\n off = n\n lights.each {|v| off -= 1 if v }\n\n ans = factorial(off)\n off = 0\n den = 1\n 0.upto(n) do |i|\n if i == n\n den *= factorial(off)\n break\n end\n\n if lights[i]\n den *= factorial(off)\n off = 0\n else\n off += 1\n end\n end\n\n ans /= den\n flag = true\n 0.upto(n) do |i|\n if flag && lights[i]\n flag = false\n off = 0\n end\n\n if lights[i]\n ans *= 2 ** (off - 1) if off > 0\n off = 0\n else\n off += 1\n end\n end\n\n ans\n end\nend\n\ntokens = gets.split(\" \")\nn = tokens[0].to_i\nm = tokens[1].to_i\nlights = Array.new(n, false)\n\ngets.split(\" \").map(&:to_i).each {|i| lights[i-1] = true }\n\nsolver = Solver.new\nputs solver.solve(lights)\n\n"}, {"source_code": "#!/usr/bin/ruby\n\nclass Solver\n def factorial(n)\n f = 1\n 2.upto(n) {|i| f *= i }\n return f\n end\n\n def solve(lights)\n n = lights.length\n off = n\n lights.each {|v| off -= 1 if v }\n\n return 0 if off == 0 || off == n\n\n ans = factorial(off)\n off = 0\n den = 1\n 0.upto(n) do |i|\n if i == n\n den *= factorial(off)\n break\n end\n\n if lights[i]\n den *= factorial(off)\n off = 0\n else\n off += 1\n end\n end\n\n ans /= den\n flag = true\n 0.upto(n) do |i|\n if flag && lights[i]\n flag = false\n off = 0\n end\n\n if lights[i]\n ans *= 2 ** (off - 1) if off > 0\n off = 0\n else\n off += 1\n end\n end\n\n ans\n end\nend\n\ntokens = gets.split(\" \")\nn = tokens[0].to_i\nm = tokens[1].to_i\nlights = Array.new(n, false)\n\nif m > 0\n gets.split(\" \").map(&:to_i).each {|v| lights[v-1] = true }\nend\n\nsolver = Solver.new\nputs solver.solve(lights)\n\n"}, {"source_code": "#!/usr/bin/ruby\n\nclass Solver\n def factorial(n)\n f = 1\n 2.upto(n) {|i| f *= i }\n return f\n end\n\n def solve(lights)\n n = lights.length\n off = n\n lights.each {|v| off -= 1 if v }\n\n return 0 if off == n\n\n ans = factorial(off)\n off = 0\n den = 1\n 0.upto(n) do |i|\n if i == n\n den *= factorial(off)\n break\n end\n\n if lights[i]\n den *= factorial(off)\n off = 0\n else\n off += 1\n end\n end\n\n ans /= den\n flag = true\n 0.upto(n) do |i|\n if flag && lights[i]\n flag = false\n off = 0\n end\n\n if lights[i]\n ans *= 2 ** (off - 1) if off > 0\n off = 0\n else\n off += 1\n end\n end\n\n ans\n end\nend\n\ntokens = gets.split(\" \")\nn = tokens[0].to_i\nm = tokens[1].to_i\nlights = Array.new(n, false)\n\nif m > 0\n gets.split(\" \").map(&:to_i).each {|v| lights[v-1] = true }\nend\n\nsolver = Solver.new\nputs solver.solve(lights)\n\n"}], "src_uid": "14da0cdf2939c796704ec548f49efb87"} {"nl": {"description": "You are given $$$n$$$ numbers $$$a_1, a_2, \\ldots, a_n$$$. Is it possible to arrange them in a circle in such a way that every number is strictly less than the sum of its neighbors?For example, for the array $$$[1, 4, 5, 6, 7, 8]$$$, the arrangement on the left is valid, while arrangement on the right is not, as $$$5\\ge 4 + 1$$$ and $$$8> 1 + 6$$$. ", "input_spec": "The first line contains a single integer $$$n$$$ ($$$3\\le n \\le 10^5$$$) — the number of numbers. The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\leq a_i \\le 10^9$$$) — the numbers. The given numbers are not necessarily distinct (i.e. duplicates are allowed).", "output_spec": "If there is no solution, output \"NO\" in the first line. If there is a solution, output \"YES\" in the first line. In the second line output $$$n$$$ numbers — elements of the array in the order they will stay in the circle. The first and the last element you output are considered neighbors in the circle. If there are multiple solutions, output any of them. You can print the circle starting with any element.", "sample_inputs": ["3\n2 4 3", "5\n1 2 3 4 4", "3\n13 8 5", "4\n1 10 100 1000"], "sample_outputs": ["YES\n4 2 3", "YES\n4 4 2 1 3", "NO", "NO"], "notes": "NoteOne of the possible arrangements is shown in the first example: $$$4< 2 + 3$$$;$$$2 < 4 + 3$$$;$$$3< 4 + 2$$$.One of the possible arrangements is shown in the second example.No matter how we arrange $$$13, 8, 5$$$ in a circle in the third example, $$$13$$$ will have $$$8$$$ and $$$5$$$ as neighbors, but $$$13\\ge 8 + 5$$$. There is no solution in the fourth example."}, "positive_code": [{"source_code": "n = gets.to_i\ninp = gets.split.map(&:to_i)\n\ninp.sort!\n\nif (inp[-2] + inp[-3] <= inp[-1])\n puts \"NO\"\nelse\n puts \"YES\"\n temp = inp[-1]\n inp[-1] = inp[-2]\n inp[-2] = temp\n inp.each {|x| print \"#{x} \"}\nend"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i).sort\nif a[-1] < a[-2] + a[-3]\n puts \"YES\"\n ans = []\n ans[0] = a[-2]\n ans[1] = a[-1]\n ans[2] = a[-3]\n (n-3).times do |i|\n ans[i+3] = a[-4-i]\n end\n puts ans.join(\" \")\nelse\n puts \"NO\"\nend\n"}, {"source_code": "n = gets.strip\na = gets.strip.split(' ').map(&:to_i)\n\na.sort!\n\nif a[-1] < a[0] + a[-2]\n puts \"YES\"\n puts a.join(' ')\nelse\n a[-2], a[-1] = a[-1], a[-2]\n if a[-1] < a[0] + a[-2] && a[-2] < a[-3] + a[-1]\n puts \"YES\"\n puts a.join(' ')\n else\n puts \"NO\"\n end\nend"}], "negative_code": [{"source_code": "n = gets.to_i\na = gets.split.map(&:to_i).sort\nif a[-1] < a[-2] + a[-3]\n puts \"YES\"\n ans = []\n ans[0] = a[-2]\n ans[1] = a[-1]\n ans[2] = a[-3]\n (n-3).times do |i|\n ans[i+3] = a[-3-i]\n end\n puts ans.join(\" \")\nelse\n puts \"NO\"\nend\n"}], "src_uid": "a5ee97e99ecfe4b72c0642546746842a"} {"nl": {"description": "Nadeko's birthday is approaching! As she decorated the room for the party, a long garland of Dianthus-shaped paper pieces was placed on a prominent part of the wall. Brother Koyomi will like it!Still unsatisfied with the garland, Nadeko decided to polish it again. The garland has n pieces numbered from 1 to n from left to right, and the i-th piece has a colour si, denoted by a lowercase English letter. Nadeko will repaint at most m of the pieces to give each of them an arbitrary new colour (still denoted by a lowercase English letter). After this work, she finds out all subsegments of the garland containing pieces of only colour c — Brother Koyomi's favourite one, and takes the length of the longest among them to be the Koyomity of the garland.For instance, let's say the garland is represented by \"kooomo\", and Brother Koyomi's favourite colour is \"o\". Among all subsegments containing pieces of \"o\" only, \"ooo\" is the longest, with a length of 3. Thus the Koyomity of this garland equals 3.But problem arises as Nadeko is unsure about Brother Koyomi's favourite colour, and has swaying ideas on the amount of work to do. She has q plans on this, each of which can be expressed as a pair of an integer mi and a lowercase letter ci, meanings of which are explained above. You are to find out the maximum Koyomity achievable after repainting the garland according to each plan.", "input_spec": "The first line of input contains a positive integer n (1 ≤ n ≤ 1 500) — the length of the garland. The second line contains n lowercase English letters s1s2... sn as a string — the initial colours of paper pieces on the garland. The third line contains a positive integer q (1 ≤ q ≤ 200 000) — the number of plans Nadeko has. The next q lines describe one plan each: the i-th among them contains an integer mi (1 ≤ mi ≤ n) — the maximum amount of pieces to repaint, followed by a space, then by a lowercase English letter ci — Koyomi's possible favourite colour.", "output_spec": "Output q lines: for each work plan, output one line containing an integer — the largest Koyomity achievable after repainting the garland according to it.", "sample_inputs": ["6\nkoyomi\n3\n1 o\n4 o\n4 m", "15\nyamatonadeshiko\n10\n1 a\n2 a\n3 a\n4 a\n5 a\n1 b\n2 b\n3 b\n4 b\n5 b", "10\naaaaaaaaaa\n2\n10 b\n10 z"], "sample_outputs": ["3\n6\n5", "3\n4\n5\n7\n8\n1\n2\n3\n4\n5", "10\n10"], "notes": "NoteIn the first sample, there are three plans: In the first plan, at most 1 piece can be repainted. Repainting the \"y\" piece to become \"o\" results in \"kooomi\", whose Koyomity of 3 is the best achievable; In the second plan, at most 4 pieces can be repainted, and \"oooooo\" results in a Koyomity of 6; In the third plan, at most 4 pieces can be repainted, and \"mmmmmi\" and \"kmmmmm\" both result in a Koyomity of 5. "}, "positive_code": [{"source_code": "_N = gets.to_i\n_S = gets.chomp\n\nnxt = Array.new _N\n_N.times { |i|\n (i + 1).upto(_N - 1) { |j|\n if _S[i] == _S[j]\n nxt[i] = j\n break\n end\n }\n}\n\ncost = (0..25).each_with_object [] { |i, o| o << Array.new(_N + 1, _N) }\n\n26.times { |i|\n x = _S.chars.find_index ('a'.ord + i).chr\n while x\n y = x\n cnt = 0\n while y\n cnt += 1\n cost[i][y - x + 1] = [cost[i][y - x + 1], y - x + 1 - cnt].min\n y = nxt[y]\n end\n x = nxt[x]\n end\n cost[i][0] = 0\n cmin = _N\n _N.times { |j|\n cmin = [cmin, cost[i][j]].min + 1\n cost[i][j + 1] = [cost[i][j + 1], cmin].min\n }\n cmin = _N\n _N.times { |j|\n cmin = [cmin, cost[i][_N - j]].min\n cost[i][_N - j] = cmin\n }\n}\n\ngets.to_i.times {\n _M, _C = gets.split\n _M = _M.to_i\n _C = _C.ord - 'a'.ord\n lb, ub = 0, _N + 1\n until lb + 1 == ub\n mb = lb + ub >> 1\n if _M >= cost[_C][mb]\n lb = mb\n else\n ub = mb\n end\n end\n puts [_N, lb + _M - cost[_C][lb]].min\n}"}], "negative_code": [{"source_code": "_N = gets.to_i\n_S = gets.chomp\n\nnxt = Array.new _N\n_N.times { |i|\n (i + 1).upto(_N - 1) { |j|\n if _S[i] == _S[j]\n nxt[i] = j\n break\n end\n }\n}\n\ncost = (0..25).each_with_object [] { |i, o| o << Array.new(_N + 1, _N) }\n\n26.times { |i|\n x = _S.chars.find_index ('a'.ord + i).chr\n while x\n y = x\n cnt = 0\n while y\n cnt += 1\n cost[i][y - x + 1] = y - x + 1 - cnt if y - x + 1 - cnt < cost[i][y - x + 1]\n y = nxt[y]\n end\n x = nxt[x]\n end\n cost[i][0] = 0\n cmin = _N\n _N.times { |j|\n cmin = cost[i][_N - j] if cost[i][_N - j] < cmin\n cost[i][_N - j] = cmin\n }\n}\n\ngets.to_i.times {\n _M, _C = gets.split\n _M = _M.to_i\n _C = _C.ord - 'a'.ord\n lb, ub = 0, _N + 1\n until lb + 1 == ub\n mb = lb + ub >> 1\n if _M >= cost[_C][mb]\n lb = mb\n else\n ub = mb\n end\n end\n puts [_N, lb + _M - cost[_C][lb]].min\n}"}, {"source_code": "_N = gets.to_i\n_S = gets.chomp\n\nnxt = Array.new _N\n_N.times { |i|\n (i + 1).upto(_N - 1) { |j|\n if _S[i] == _S[j]\n nxt[i] = j\n break\n end\n }\n}\n\ncost = (0..25).each_with_object [] { |i, o| o << Array.new(_N + 1, _N) }\n\n26.times { |i|\n x = _S.chars.find_index ('a'.ord + i).chr\n while x\n y = x\n cnt = 0\n while y\n cnt += 1\n cost[i][y - x + 1] = y - x + 1 - cnt if y - x + 1 - cnt < cost[i][y - x + 1]\n y = nxt[y]\n end\n x = nxt[x]\n end\n cost[i][0] = 0\n cmin = _N\n _N.times { |j|\n cmin = cost[i][_N - j] if cost[i][_N - j] < cmin\n cost[i][_N - j] = cmin\n }\n}\n\ngets.to_i.times {\n _M, _C = gets.split\n _M = _M.to_i\n _C = _C.ord - 'a'.ord\n lb, ub = 0, _N + 1\n until lb + 1 == ub\n mb = lb + ub >> 1\n if _M >= cost[_C][mb]\n lb = mb\n else\n ub = mb\n end\n end\n puts lb + _M - cost[_C][lb]\n}\n"}, {"source_code": "_N = gets.to_i\n_S = gets.chomp\n\nnxt = Array.new _N\n_N.times { |i|\n (i + 1).upto(_N - 1) { |j|\n if _S[i] == _S[j]\n nxt[i] = j\n break\n end\n }\n}\n\ncost = (0..25).each_with_object [] { |i, o| o << Array.new(_N + 1, _N) }\n\n26.times { |i|\n x = _S.chars.find_index ('a'.ord + i).chr\n while x\n y = x\n cnt = 0\n while y\n cnt += 1\n cost[i][y - x + 1] = [cost[i][y - x + 1], y - x + 1 - cnt].min\n y = nxt[y]\n end\n x = nxt[x]\n end\n cost[i][0] = 0\n cmin = _N\n _N.times { |j|\n cmin = [cmin, cost[i][j]].min + 1\n cost[i][j + 1] = [cost[i][j + 1], cmin].min\n }\n cmin = _N\n _N.times { |j|\n cmin = [cmin, cost[i][_N - j]].min\n cost[i][_N - j] = cmin\n }\n}\n\ngets.to_i.times {\n _M, _C = gets.split\n _M = _M.to_i\n _C = _C.ord - 'a'.ord\n lb, ub = 0, _N + 1\n until lb + 1 == ub\n mb = lb + ub >> 1\n if _M >= cost[_C][mb]\n lb = mb\n else\n ub = mb\n end\n end\n puts lb + _M - cost[_C][lb]\n}\n"}], "src_uid": "0646a23b550eefea7347cef831d1c69d"} {"nl": {"description": "You are given a set of all integers from $$$l$$$ to $$$r$$$ inclusive, $$$l < r$$$, $$$(r - l + 1) \\le 3 \\cdot 10^5$$$ and $$$(r - l)$$$ is always odd.You want to split these numbers into exactly $$$\\frac{r - l + 1}{2}$$$ pairs in such a way that for each pair $$$(i, j)$$$ the greatest common divisor of $$$i$$$ and $$$j$$$ is equal to $$$1$$$. Each number should appear in exactly one of the pairs.Print the resulting pairs or output that no solution exists. If there are multiple solutions, print any of them.", "input_spec": "The only line contains two integers $$$l$$$ and $$$r$$$ ($$$1 \\le l < r \\le 10^{18}$$$, $$$r - l + 1 \\le 3 \\cdot 10^5$$$, $$$(r - l)$$$ is odd).", "output_spec": "If any solution exists, print \"YES\" in the first line. Each of the next $$$\\frac{r - l + 1}{2}$$$ lines should contain some pair of integers. GCD of numbers in each pair should be equal to $$$1$$$. All $$$(r - l + 1)$$$ numbers should be pairwise distinct and should have values from $$$l$$$ to $$$r$$$ inclusive. If there are multiple solutions, print any of them. If there exists no solution, print \"NO\".", "sample_inputs": ["1 8"], "sample_outputs": ["YES\n2 7\n4 1\n3 8\n6 5"], "notes": null}, "positive_code": [{"source_code": "l,r=gets.split.map &:to_i;puts :YES,[*l..r]"}, {"source_code": "a,b = gets.chomp.split.map(&:to_i)\n\nputs \"YES\"\nfor i in 0..1000000\nputs \"#{a} #{a+1}\"\na += 2\nbreak if a > b\nend"}, {"source_code": "l,r = gets.split.map(&:to_i)\nputs \"YES\"\n((r-l+1)/2).times do |i|\n puts \"#{l+2*i} #{l+2*i+1}\"\nend\n"}, {"source_code": "l, r = gets.split.map &:to_i\nt = (r-l+1)/2\nputs \"YES\"\ni = l\nwhile i <= r\n print \"#{i} #{i+1}\\n\"\n i += 2\nend"}], "negative_code": [], "src_uid": "6432a543eeee9833c6d849222ad6b93d"} {"nl": {"description": "Vasya often uses public transport. The transport in the city is of two types: trolleys and buses. The city has n buses and m trolleys, the buses are numbered by integers from 1 to n, the trolleys are numbered by integers from 1 to m.Public transport is not free. There are 4 types of tickets: A ticket for one ride on some bus or trolley. It costs c1 burles; A ticket for an unlimited number of rides on some bus or on some trolley. It costs c2 burles; A ticket for an unlimited number of rides on all buses or all trolleys. It costs c3 burles; A ticket for an unlimited number of rides on all buses and trolleys. It costs c4 burles. Vasya knows for sure the number of rides he is going to make and the transport he is going to use. He asked you for help to find the minimum sum of burles he will have to spend on the tickets.", "input_spec": "The first line contains four integers c1, c2, c3, c4 (1 ≤ c1, c2, c3, c4 ≤ 1000) — the costs of the tickets. The second line contains two integers n and m (1 ≤ n, m ≤ 1000) — the number of buses and trolleys Vasya is going to use. The third line contains n integers ai (0 ≤ ai ≤ 1000) — the number of times Vasya is going to use the bus number i. The fourth line contains m integers bi (0 ≤ bi ≤ 1000) — the number of times Vasya is going to use the trolley number i.", "output_spec": "Print a single number — the minimum sum of burles Vasya will have to spend on the tickets.", "sample_inputs": ["1 3 7 19\n2 3\n2 5\n4 4 4", "4 3 2 1\n1 3\n798\n1 2 3", "100 100 8 100\n3 5\n7 94 12\n100 1 47 0 42"], "sample_outputs": ["12", "1", "16"], "notes": "NoteIn the first sample the profitable strategy is to buy two tickets of the first type (for the first bus), one ticket of the second type (for the second bus) and one ticket of the third type (for all trolleys). It totals to (2·1) + 3 + 7 = 12 burles.In the second sample the profitable strategy is to buy one ticket of the fourth type.In the third sample the profitable strategy is to buy two tickets of the third type: for all buses and for all trolleys."}, "positive_code": [{"source_code": "c1, c2, c3, c4 = gets.split.map(&:to_i)\nn, m = gets.split.map(&:to_i)\nb = gets.split.map(&:to_i)\nt = gets.split.map(&:to_i)\n\nsumb = 0\nsumt = 0\n\nfor i in 0...n\n b[i] = [c1*b[i], c2].min\n sumb += b[i]\nend\n\nfor i in 0...m\n t[i] = [c1*t[i], c2].min\n sumt += t[i]\nend\n\nsumb = [sumb, c3].min\nsumt = [sumt, c3].min\n\nprint [sumb+sumt, c4].min"}, {"source_code": "costs = gets.split.map{|s| s.to_i}\nbn, tn = gets.split.map{|s| s.to_i}\nbuses = gets.split.map{|s| s.to_i}\ntrolleys = gets.split.map{|s| s.to_i}\n\nb1 = costs[1]/costs[0]\nbsum = tsum = 0\nbuses.each_index{|i|\n buses[i] = buses[i] > b1 ? costs[1] : buses[i]*costs[0]\n bsum += buses[i]\n}\ntrolleys.each_index{|i|\n trolleys[i] = trolleys[i] > b1 ? costs[1] : trolleys[i]*costs[0]\n tsum += trolleys[i]\n}\nbsum = costs[2] if bsum > costs[2]\ntsum = costs[2] if tsum > costs[2]\nsum = bsum + tsum\nsum = costs[3] if sum > costs[3]\nprint sum"}, {"source_code": "c= gets().split().map {|i| i.to_i}.take(4)\nn,m= gets().split().map {|i| i.to_i}.take(2)\na= gets().split().map {|i| i.to_i}.take(n)\nb= gets().split().map {|i| i.to_i}.take(m)\n\ndef f(x, k, c)\n res=0\n for i in 0..k\n res+=[c[1],x[i].to_i*c[0]].min\n end\n [c[2],res].min\nend\n\nprint [c[3],f(a,n,c)+f(b,m,c)].min"}, {"source_code": "c = gets.split.collect{|i| i.to_i}\nn, m = gets.split.collect{|i| i.to_i}\nsum_a = 0\ngets.chomp.split.each do |a|\n sum_a += [a.to_i*c[0],c[1]].min\nend\nsum_a = c[2] if sum_a > c[2]\nsum_b = 0\ngets.chomp.split.each do |b|\n sum_b += [b.to_i*c[0],c[1]].min\nend\nsum_b = c[2] if sum_b > c[2]\nputs [sum_a+sum_b, c[3]].min\n"}, {"source_code": "cs = gets.split.map(&:to_i)\nn, m = gets.split.map(&:to_i)\nns = gets.split.map(&:to_i)\nms = gets.split.map(&:to_i)\n\nsumn_min = ns.inject(0){|a, b| a + [cs[1], b * cs[0]].min }\nsumm_min = ms.inject(0){|a, b| a + [cs[1], b * cs[0]].min }\n#puts sumn_min\n#puts summ_min\n\nnmin = [sumn_min, cs[2]].min\nmmin = [summ_min, cs[2]].min\n\nputs [cs[3], nmin + mmin].min"}, {"source_code": "#f = File.open('test1.txt', 'r')\nf = STDIN\nc = f.gets.chomp.split.map &:to_i\nn, m = f.gets.chomp.split.map &:to_i\na = []\n2.times { a << f.gets.chomp.split.map(&:to_i) }\n# Compute when don't use c4\ntotal = 0\n2.times do |i|\n sub_total = 0\n a[i].each do |ai|\n sub_total += [ai * c[0], c[1]].min\n end\n total += [sub_total, c[2]].min\nend\n\nputs [total, c[3]].min\n"}], "negative_code": [], "src_uid": "11fabde93815c1805369bbbc5a40e2d8"} {"nl": {"description": "Reading books is one of Sasha's passions. Once while he was reading one book, he became acquainted with an unusual character. The character told about himself like that: \"Many are my names in many countries. Mithrandir among the Elves, Tharkûn to the Dwarves, Olórin I was in my youth in the West that is forgotten, in the South Incánus, in the North Gandalf; to the East I go not.\"And at that moment Sasha thought, how would that character be called in the East? In the East all names are palindromes. A string is a palindrome if it reads the same backward as forward. For example, such strings as \"kazak\", \"oo\" and \"r\" are palindromes, but strings \"abb\" and \"ij\" are not. Sasha believed that the hero would be named after one of the gods of the East. As long as there couldn't be two equal names, so in the East people did the following: they wrote the original name as a string on a piece of paper, then cut the paper minimum number of times $$$k$$$, so they got $$$k+1$$$ pieces of paper with substrings of the initial string, and then unite those pieces together to get a new string. Pieces couldn't be turned over, they could be shuffled.In this way, it's possible to achive a string abcdefg from the string f|de|abc|g using $$$3$$$ cuts (by swapping papers with substrings f and abc). The string cbadefg can't be received using the same cuts.More formally, Sasha wants for the given palindrome $$$s$$$ find such minimum $$$k$$$, that you can cut this string into $$$k + 1$$$ parts, and then unite them in such a way that the final string will be a palindrome and it won't be equal to the initial string $$$s$$$. It there is no answer, then print \"Impossible\" (without quotes).", "input_spec": "The first line contains one string $$$s$$$ ($$$1 \\le |s| \\le 5\\,000$$$) — the initial name, which consists only of lowercase Latin letters. It is guaranteed that $$$s$$$ is a palindrome.", "output_spec": "Print one integer $$$k$$$ — the minimum number of cuts needed to get a new name, or \"Impossible\" (without quotes).", "sample_inputs": ["nolon", "otto", "qqqq", "kinnikkinnik"], "sample_outputs": ["2", "1", "Impossible", "1"], "notes": "NoteIn the first example, you can cut the string in those positions: no|l|on, and then unite them as follows on|l|no. It can be shown that there is no solution with one cut.In the second example, you can cut the string right in the middle, and swap peaces, so you get toot.In the third example, you can't make a string, that won't be equal to the initial one.In the fourth example, you can cut the suffix nik and add it to the beginning, so you get nikkinnikkin."}, "positive_code": [{"source_code": "class String\n def palindrome?\n self == self.reverse\n end\nend\n\ndef solve(s)\n sa = s.split('')\n return 'Impossible' if sa.uniq.size == 1\n (0..s.size/2).to_a.each do |c|\n r = sa.rotate(c).join('')\n if r.palindrome? && r != s\n return 1\n end\n end\n\n left = s[0..(s.size/2)-1]\n right = s[s.size/2+(s.size.odd? ? 1 : 0)..s.size]\n if left.palindrome?\n left.split('').uniq.size == 1 ? 'Impossible' : 2\n else\n return s.size.odd? ? 2 : 1\n end\nend\n\nputs solve(gets.strip)"}, {"source_code": "if File.exists?(\"input\")\n $stdin = File.open(\"input\")\n $stdout = File.open(\"output\",\"w\")\nend\n\n\n\ns = gets.chomp\nlen = s.length\n\nif s.each_char.to_a.uniq.count == 1\n\tputs \"Impossible\"\n\texit\nend\n\nif len%2==1\n\tnews = s[0..len/2-1]+s[len/2+1..-1]\n\tif news.each_char.to_a.uniq.count == 1\n\t\tputs \"Impossible\"\n\t\texit\n\tend\nend\n#p \"hi\"\nif len%2==0\n\tif(s[0..len/2-1] != s[len/2..-1])\n\t\tputs 1\n\t\texit\n\tend\nend\n\n(1..len-1).each do |i|\n\t#break is before i\n\tnews = s[i..-1]+s[0..i-1] \n\tif(news==news.reverse && news != s)\n\t\tputs 1\n\t\texit\n\tend\nend\n\nputs 2"}], "negative_code": [{"source_code": "if File.exists?(\"input\")\n $stdin = File.open(\"input\")\n $stdout = File.open(\"output\",\"w\")\nend\n\n\n\ns = gets.chomp\nlen = s.length\n\nif s.each_char.to_a.uniq.count == 1\n\tputs \"Impossible\"\n\texit\nend\n\n#p \"hi\"\nif len%2==0\n\tif(s[0..len/2-1] != s[len/2..-1])\n\t\tputs 1\n\t\texit\n\tend\nend\n\n(1..len-1).each do |i|\n\t#break is before i\n\tnews = s[i..-1]+s[0..i-1] \n\tif(news==news.reverse && news != s)\n\t\tputs 1\n\t\texit\n\tend\nend\nputs 2"}], "src_uid": "ffdef277d0ff8e8579b113f5bd30f52a"} {"nl": {"description": "Flatland has recently introduced a new type of an eye check for the driver's licence. The check goes like that: there is a plane with mannequins standing on it. You should tell the value of the minimum angle with the vertex at the origin of coordinates and with all mannequins standing inside or on the boarder of this angle. As you spend lots of time \"glued to the screen\", your vision is impaired. So you have to write a program that will pass the check for you.", "input_spec": "The first line contains a single integer n (1 ≤ n ≤ 105) — the number of mannequins. Next n lines contain two space-separated integers each: xi, yi (|xi|, |yi| ≤ 1000) — the coordinates of the i-th mannequin. It is guaranteed that the origin of the coordinates has no mannequin. It is guaranteed that no two mannequins are located in the same point on the plane.", "output_spec": "Print a single real number — the value of the sought angle in degrees. The answer will be considered valid if the relative or absolute error doesn't exceed 10 - 6. ", "sample_inputs": ["2\n2 0\n0 2", "3\n2 0\n0 2\n-2 2", "4\n2 0\n0 2\n-2 0\n0 -2", "2\n2 1\n1 2"], "sample_outputs": ["90.0000000000", "135.0000000000", "270.0000000000", "36.8698976458"], "notes": "NoteSolution for the first sample test is shown below: Solution for the second sample test is shown below: Solution for the third sample test is shown below: Solution for the fourth sample test is shown below: "}, "positive_code": [{"source_code": "#!/Users/mike/.rvm/rubies/ruby-1.9.3-p327/bin/ruby\n\nn = STDIN.readline.to_i\nif n == 1\n\tputs 0\n\texit\nend\n\npoints, angles = [], []\nn.times do\n\tx, y = STDIN.readline.split.collect {|s| s.to_i}\n\tpoints.push([x, y])\n\td = (x*x+y*y)**0.5\n\tangle = Math.acos(x/d)\n\tif y < 0\n\t\tangle = 2*Math.acos(-1)-angle\n\tend\n\tangle = angle/Math.acos(-1)*180\n\tangles.push(angle)\nend\n\nangles.sort!\n\nbest = angles[n-1]-angles[0]\n\n(1..n-1).each do |i|\n\tdiff = 360-(angles[i]-angles[i-1])\n\tbest = diff if diff < best\nend\n\nputs best\n\n"}], "negative_code": [], "src_uid": "a67cdb7501e99766b20a2e905468c29c"} {"nl": {"description": "As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of ai cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task.", "input_spec": "The first line contains two space-separated positive integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 1000) — the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers ai (1 ≤ ai ≤ 104) — the towers' initial heights.", "output_spec": "In the first line print two space-separated non-negative integers s and m (m ≤ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i ≠ j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them.", "sample_inputs": ["3 2\n5 8 5", "3 4\n2 2 4", "5 3\n8 3 2 6 3"], "sample_outputs": ["0 2\n2 1\n2 3", "1 1\n3 2", "3 3\n1 3\n1 2\n1 3"], "notes": "NoteIn the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6."}, "positive_code": [{"source_code": "n, k = STDIN.readline.split.map {|s| s.to_i }\nheights = STDIN.readline.split.map {|s| s.to_i }\npairs = heights.each_with_index.to_a\nmoves = []\nk.times do\n\tpairs.sort!\n\tbreak if pairs[0][0] + 1 >= pairs[-1][0]\n\tmoves.push('%d %d' % [pairs[-1][1]+1, pairs[0][1]+1])\n\tpairs[-1][0] -= 1\n\tpairs[0][0] += 1\nend\npairs.sort!\nputs '%d %d' % [pairs[-1][0] - pairs[0][0], moves.length]\nputs moves.join(\"\\n\")\n"}, {"source_code": "n_and_k = gets.split(' ')\nn = n_and_k[0].to_i\nk = n_and_k[1].to_i\na = gets.split(' ').map { |s| s.to_i }\n\nmoves = Array.new\n\nk.times do\n posmin = 0\n posmax = 0\n\n a.each_with_index do |val, index|\n if val > a[posmax]\n posmax = index\n else\n if val < a[posmin]\n posmin = index\n end\n end\n end\n\n if a[posmax] == a[posmin]\n break\n else\n moves.push [posmax + 1, posmin + 1]\n a[posmax] -= 1\n a[posmin] += 1\n end\nend\n\nmaxdiff = a.max - a.min\n\nputs \"#{maxdiff} #{moves.length}\"\nmoves.each do |move|\n puts \"#{move[0]} #{move[1]}\"\nend"}, {"source_code": "n, k = gets.split.collect{|i| i.to_i}\na = gets.split.collect{|i| i.to_i}\nans = Array.new\ncnt = 0\nwhile cnt < k\n min, max = a.minmax\n if max-min <= 1\n break\n end\n minindex = a.index(min)\n maxindex = a.index(max)\n a[maxindex] -= 1\n a[minindex] += 1\n ans.push([maxindex+1, minindex+1])\n cnt += 1\nend\nmin, max = a.minmax\nprint max-min, ' ', cnt, \"\\n\"\nans.each{|i| puts i.join(' ')}\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\na = gets.split.map(&:to_i).zip((1..n).to_a).sort!\nans = []\ncnt = 0\nk.times do\n break if a[-1][0] - a[0][0] <= 1 \n cnt += 1\n a[0][0] += 1\n a[-1][0] -= 1\n ans << [a[-1][1], a[0][1]]\n a.sort!\nend\nputs \"#{a[-1][0] - a[0][0]} #{cnt}\", ans.map{|x| x.join(' ')}\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\na = gets.split.map(&:to_i).zip(1..n).sort!\nans = []\nk.times do\n mx, mi = a.last, a.first\n break if mx[0] - mi[0] <= 1 \n mx[0] -= 1\n mi[0] += 1\n ans << [mx[1], mi[1]]\n a.sort!\nend\nputs \"#{a[-1][0] - a[0][0]} #{ans.size}\", ans.map{|x| x.join(' ')}\n"}], "negative_code": [{"source_code": "n, k = gets.chomp.split.map(&:to_i)\nhs = gets.chomp.split.map(&:to_i)\nlist = []\nc = 0\ngoal = (hs.inject(:+) / (n * 1.0)).ceil\nk.times do\n for i in 0...n\n break if c >= k\n if hs[i] > goal\n for j in 0...n\n if hs[j] < goal && j != i\n hs[j] += 1\n hs[i] -= 1\n list << [i + 1, j + 1]\n c += 1\n break\n end\n end\n break\n end\n end\nend\nmax = 0\nmin = 10000\nfor i in 0...n\n max = hs[i] if hs[i] > max\n min = hs[i] if hs[i] < min\nend\nans = [max - min, c]\nputs ans.join(\" \")\nputs list.map{|x| x.join(\" \")}\n"}, {"source_code": "n, k = gets.chomp.split.map(&:to_i)\nh = gets.chomp.split.map(&:to_i) # 5 8 5\nans = []\nlist = []\nc = 0\ngoal = (h.inject(:+) / (n * 1.0)).ceil\nk.times do\n max = 0\n min = 10000\n ai, ii = 0\n for i in 0...n\n if h[i] > max\n max = h[i]\n ai = i\n end\n if h[i] < min\n min = h[i]\n ii = i\n end\n end\n if max > goal\n if min < goal && max != min\n h[ii] += 1\n min = h[ii]\n h[ai] -= 1\n max = h[ai]\n list << [ai + 1, ii + 1]\n c += 1\n end\n end\n for i in 0...n\n if h[i] > max\n max = h[i]\n ai = i\n end\n if h[i] < min\n min = h[i]\n ii = i\n end\n end\n dif = max - min\n ans = [dif, c]\nend\nputs ans.join(\" \")\nputs list.map{|x| x.join(\" \")}\n"}], "src_uid": "9cd42fb28173170a6cfa947cb31ead6d"} {"nl": {"description": "Ridbit starts with an integer $$$n$$$.In one move, he can perform one of the following operations: divide $$$n$$$ by one of its proper divisors, or subtract $$$1$$$ from $$$n$$$ if $$$n$$$ is greater than $$$1$$$. A proper divisor is a divisor of a number, excluding itself. For example, $$$1$$$, $$$2$$$, $$$4$$$, $$$5$$$, and $$$10$$$ are proper divisors of $$$20$$$, but $$$20$$$ itself is not.What is the minimum number of moves Ridbit is required to make to reduce $$$n$$$ to $$$1$$$?", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 1000$$$) — the number of test cases. The only line of each test case contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 10^9$$$).", "output_spec": "For each test case, output the minimum number of moves required to reduce $$$n$$$ to $$$1$$$.", "sample_inputs": ["6\n1\n2\n3\n4\n6\n9"], "sample_outputs": ["0\n1\n2\n2\n2\n3"], "notes": "NoteFor the test cases in the example, $$$n$$$ may be reduced to $$$1$$$ using the following operations in sequence$$$1$$$$$$2 \\xrightarrow{} 1$$$$$$3 \\xrightarrow{} 2 \\xrightarrow{} 1$$$$$$4 \\xrightarrow{} 2 \\xrightarrow{} 1$$$$$$6 \\xrightarrow{} 2 \\xrightarrow{} 1$$$$$$9 \\xrightarrow{} 3 \\xrightarrow{} 2\\xrightarrow{} 1$$$"}, "positive_code": [{"source_code": "t = gets.chomp.to_i\nt.times do\n n = gets.chomp.to_i\n case\n when n == 1\n puts 0\n when n == 2\n puts 1\n when n % 2 == 0 || n == 3\n puts 2\n else\n puts 3\n end\nend\n"}], "negative_code": [{"source_code": "t = gets.chomp.to_i\nt.times do\n n = gets.chomp.to_i\n case\n when n == 1\n puts 0\n when n % 2 == 0 || n == 3\n puts 2\n else\n puts 3\n end\nend\n"}, {"source_code": "t = gets.chomp.to_i\nt.times do\n n = gets.chomp.to_i\n res = 0\n while n > 3\n d = (n ** 0.5).ceil\n d -= 1 while n % d != 0 && d > 1\n if d == 1\n n -= 1\n else\n n = n / d\n end\n res += 1\n end\n res += n - 1\n puts res\nend\n"}, {"source_code": "t = gets.chomp.to_i\nt.times do\n n = gets.chomp.to_i\n res = 0\n while n > 3\n d = 2\n d += 1 while n % d != 0 && d < (n ** 0.5).ceil + 1\n if d == (n ** 0.5).ceil + 1\n n -= 1\n else\n # d_init = d\n # d = d * d_init while n % d_init * d == 0\n d2 = n / d\n n = n / d2\n end\n res += 1\n end\n res += n - 1\n puts res\nend\n"}, {"source_code": "t = gets.chomp.to_i\nt.times do\n n = gets.chomp.to_i\n case\n when n == 1\n puts 0\n when n % 2 == 0\n puts 2\n else\n puts 3\n end\nend\n"}], "src_uid": "614aa068ce74090b6577006c45e549cf"} {"nl": {"description": "Little X used to play a card game called \"24 Game\", but recently he has found it too easy. So he invented a new game.Initially you have a sequence of n integers: 1, 2, ..., n. In a single step, you can pick two of them, let's denote them a and b, erase them from the sequence, and append to the sequence either a + b, or a - b, or a × b.After n - 1 steps there is only one number left. Can you make this number equal to 24?", "input_spec": "The first line contains a single integer n (1 ≤ n ≤ 105).", "output_spec": "If it's possible, print \"YES\" in the first line. Otherwise, print \"NO\" (without the quotes). If there is a way to obtain 24 as the result number, in the following n - 1 lines print the required operations an operation per line. Each operation should be in form: \"a op b = c\". Where a and b are the numbers you've picked at this operation; op is either \"+\", or \"-\", or \"*\"; c is the result of corresponding operation. Note, that the absolute value of c mustn't be greater than 1018. The result of the last operation must be equal to 24. Separate operator sign and equality sign from numbers with spaces. If there are multiple valid answers, you may print any of them.", "sample_inputs": ["1", "8"], "sample_outputs": ["NO", "YES\n8 * 7 = 56\n6 * 5 = 30\n3 - 4 = -1\n1 - 2 = -1\n30 - -1 = 31\n56 - 31 = 25\n25 + -1 = 24"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\n\nif n < 4\n puts \"NO\"\n exit\nend\n\ns = \"YES\\n\"\nwhile n > 5\n s << \"#{n} - #{n-1} = 1\\n\"\n n -= 2\n s << \"#{n} * 1 = #{n}\\n\"\nend\n\nif n==5\n s << \"5 - 1 = 4\\n\"\n s << \"4 * 3 = 12\\n\"\n s << \"4 - 2 = 2\\n\"\n s << \"12 * 2 = 24\\n\"\nelse\n s << \"4 * 3 = 12\\n\"\n s << \"12 * 2 = 24\\n\"\n s << \"24 * 1 = 24\\n\"\nend\n\nputs s"}, {"source_code": "n = gets.to_i\n\nif n < 4\n puts 'NO'\n exit\nend\n\ns = \"YES\\n\"\nwhile n > 5\n s << \"#{n} - #{n - 1} = 1\\n\"\n n -= 2\n s << \"#{n} * 1 = #{n}\\n\"\nend\nif n == 5\n s << \"5 - 1 = 4\\n\"\n s << \"4 * 3 = 12\\n\"\n s << \"4 - 2 = 2\\n\"\n s << \"12 * 2 = 24\\n\"\nelse\n s << \"4 * 3 = 12\\n\"\n s << \"12 * 2 = 24\\n\"\n s << \"24 * 1 = 24\\n\"\nend\nputs s"}, {"source_code": "n = gets.to_i\n\nif n < 4\n\tputs 'NO'\nelse\n\tputs 'YES'\n\tif n.even?\n\t\tputs '1 * 2 = 2'\n\t\tputs '2 * 3 = 6'\n\t\tputs '6 * 4 = 24'\n\t\t5.upto(n) do |i|\n\t\t\tif i.odd?\n\t\t\t\tputs (i + 1).to_s + ' - ' + i.to_s + ' = 1'\n\t\t\t\tputs '24 * 1 = 24'\n\t\t\tend\n\t\tend\n\telse\n\t\tputs '5 - 1 = 4'\n\t\tputs '4 - 2 = 2'\n\t\tputs '2 * 3 = 6'\n\t\tputs '6 * 4 = 24'\n\t\t6.upto(n) do |i|\n\t\t\tif i.even?\n\t\t\t\tputs (i + 1).to_s + ' - ' + i.to_s + ' = 1'\n\t\t\t\tputs '24 * 1 = 24'\n\t\t\tend\n\t\tend\n\tend\nend\n"}, {"source_code": "n = STDIN.readline.to_i\nif n < 4\n\tputs 'NO'\n\texit\nend\nputs 'YES'\nlines = []\nif n%2 == 0\n\tx = (n-4)/2\n\tx.times {|i| lines.push('%d - %d = 1' % [6+i*2, 5+i*2]) }\n\tx.times { lines.push('1 * 1 = 1') }\n\tlines.push('1 * 2 = 2')\n\tlines.push('2 * 3 = 6')\n\tlines.push('6 * 4 = 24')\nelse\n\tx = (n-5)/2\n\tx.times {|i| lines.push('%d - %d = 1' % [7+i*2, 6+i*2]) }\n\tlines.push('3 - 2 = 1')\n\t(x+1).times { lines.push('1 * 1 = 1') }\n\tlines.push('1 + 5 = 6')\n\tlines.push('6 * 4 = 24')\nend\nputs lines.join(\"\\n\")\n"}, {"source_code": "n = gets.to_i\n\nif n < 4\n puts \"NO\"\nelsif n.even?\n puts \"YES\"\n puts \"1 * 2 = 2\"\n puts \"2 * 3 = 6\"\n puts \"6 * 4 = 24\"\n 5.upto(n).each_slice(2) do |a, b|\n puts \"#{b} - #{a} = 1\"\n puts \"24 * 1 = 24\"\n end\nelse\n puts \"YES\"\n puts \"2 - 1 = 1\"\n puts \"1 + 3 = 4\"\n puts \"4 * 5 = 20\"\n puts \"20 + 4 = 24\"\n 6.upto(n).each_slice(2) do |a, b|\n puts \"#{b} - #{a} = 1\"\n puts \"24 * 1 = 24\"\n end\nend\n"}, {"source_code": "#! /usr/bin/env ruby\n# -*- coding: utf-8 -*-\n# vim:fenc=utf-8\n#\n# Copyright © 2014 Szymon Sliwa \n#\n# Distributed under terms of the MIT license.\n\nn = gets.to_i\n\nif n == 1\n puts \"NO\"\nelsif n == 2\n puts \"NO\"\nelsif n == 3\n puts \"NO\"\nelsif n == 4\n puts \"YES\"\n puts \"1 * 2 = 2\\n2 * 3 = 6\\n6 * 4 = 24\"\nelsif n == 5\n puts \"YES\"\n puts \"5 - 1 = 4\\n4 - 2 = 2\\n4 * 2 = 8\\n8 * 3 = 24\"\nelse\n puts \"YES\"\n puts \"2 * 4 = 8\\n6 - 5 = 1\\n1 - 1 = 0\"\n (7..n).each do |x|\n puts \"#{x} * 0 = 0\"\n end\n puts \"3 + 0 = 3\"\n puts \"8 * 3 = 24\\n\"\nend\n\n\n\n\n\n\n\n"}], "negative_code": [{"source_code": "n = STDIN.readline.to_i\nif n < 4\n\tputs 'NO'\n\texit\nend\nlines = []\nif n%2 == 0\n\tx = (n-4)/2\n\tx.times {|i| lines.push('%d - %d' % [6+i*2, 5+i*2]) }\n\tx.times { lines.push('1 * 1') }\n\tlines.push('1 * 2')\n\tlines.push('2 * 3')\n\tlines.push('6 * 4')\nelse\n\tx = (n-5)/2\n\tx.times {|i| lines.push('%d - %d' % [7+i*2, 6+i*2]) }\n\tlines.push('3 - 2')\n\t(x+1).times { lines.push('1 * 1') }\n\tlines.push('1 + 5')\n\tlines.push('6 * 4')\nend\nputs lines.join(\"\\n\")\n"}, {"source_code": "n = STDIN.readline.to_i\nif n < 4\n\tputs 'NO'\n\texit\nend\nputs 'YES'\nlines = []\nif n%2 == 0\n\tx = (n-4)/2\n\tx.times {|i| lines.push('%d - %d' % [6+i*2, 5+i*2]) }\n\tx.times { lines.push('1 * 1') }\n\tlines.push('1 * 2')\n\tlines.push('2 * 3')\n\tlines.push('6 * 4')\nelse\n\tx = (n-5)/2\n\tx.times {|i| lines.push('%d - %d' % [7+i*2, 6+i*2]) }\n\tlines.push('3 - 2')\n\t(x+1).times { lines.push('1 * 1') }\n\tlines.push('1 + 5')\n\tlines.push('6 * 4')\nend\nputs lines.join(\"\\n\")\n"}, {"source_code": "#! /usr/bin/env ruby\n# -*- coding: utf-8 -*-\n# vim:fenc=utf-8\n#\n# Copyright © 2014 Szymon Sliwa \n#\n# Distributed under terms of the MIT license.\n\nn = gets.to_i\n\nif n == 1\n puts \"NO\"\nelsif n == 2\n puts \"NO\"\nelsif n == 3\n puts \"NO\"\nelsif n == 4\n puts \"1 * 2 = 2\\n2 * 3 = 6\\n6 * 4 = 24\"\nelsif n == 5\n puts \"5 - 1 = 4\\n4 - 2 = 2\\n4 * 2 = 8\\n8 * 3 = 24\"\nelse\n puts \"2 * 4 = 8\\n6 - 5 = 1\\n1 - 1 = 0\"\n (7..n).each do |x|\n puts \"#{x} * 0 = 0\"\n end\n puts \"8 * 3 = 24\\n\"\nend\n\n\n\n\n\n\n\n"}, {"source_code": "#! /usr/bin/env ruby\n# -*- coding: utf-8 -*-\n# vim:fenc=utf-8\n#\n# Copyright © 2014 Szymon Sliwa \n#\n# Distributed under terms of the MIT license.\n\nn = gets.to_i\n\nif n == 1\n puts \"NO\"\nelsif n == 2\n puts \"NO\"\nelsif n == 3\n puts \"NO\"\nelsif n == 4\n puts \"1 * 2 = 2\\n2 * 3 = 6\\n6 * 4 = 24\"\nelsif n == 5\n puts \"5 - 1 = 4\\n4 - 2 = 2\\n4 * 2 = 8\\n8 * 3 = 24\"\nelse\n puts \"2 * 4 = 8\\n6 - 5 = 1\\n1 - 1 = 0\"\n (7..n).each do |x|\n puts \"#{x} * 0 = 0\"\n end\n puts \"3 + 0 = 3\"\n puts \"8 * 3 = 24\\n\"\nend\n\n\n\n\n\n\n\n"}], "src_uid": "1bd1a7fd2a07e3f8633d5bc83d837769"} {"nl": {"description": "At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.Fortunately, Picks remembers something about his set S: its elements were distinct integers from 1 to limit; the value of was equal to sum; here lowbit(x) equals 2k where k is the position of the first one in the binary representation of x. For example, lowbit(100102) = 102, lowbit(100012) = 12, lowbit(100002) = 100002 (binary representation). Can you help Picks and find any set S, that satisfies all the above conditions?", "input_spec": "The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 105).", "output_spec": "In the first line print an integer n (1 ≤ n ≤ 105), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them. If it's impossible to find a suitable set, print -1.", "sample_inputs": ["5 5", "4 3", "5 1"], "sample_outputs": ["2\n4 5", "3\n2 3 1", "-1"], "notes": "NoteIn sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4."}, "positive_code": [{"source_code": "m,n=gets.split.map &:to_i\na=[]\n(1..n).to_a.sort_by{|x|x&-x}.reverse_each{|i|if m-(i&-i)>=0 then m-=i&-i;a.push i end}\nexit !puts(-1) if m>0\np a.size\nputs a*' '\n"}, {"source_code": "m,n=gets.split.map &:to_i\na=[]\n(1..n).to_a.sort_by{|x|x&-x}.reverse_each{|i|if m-(i&-i)>=0 then m-=i&-i;a.push i end}\nexit !puts(-1) if m>0\np a.size\nputs a*' '\n"}, {"source_code": "m,n=gets.split.map &:to_i\na=[]\n(1..n).to_a.sort_by{|x|x&-x}.reverse_each{|i|if m-(i&-i)>=0 then m-=i&-i;a.push i end}\nexit !puts(-1) if m>0\np a.size\nputs a*' '"}, {"source_code": "n,m=gets.chomp.split.map(&:to_i)\nsc,i,j=Array.new(100005,0),1,1\nwhile n!=0&&i<=m\n\tj=i\n\twhile j<=m\n\t\tif n!=0 && ((n&i)!=0 || j+i*2<=m)\n\t\t\tsc[sc[0]+1]=j\n\t\t\tsc[0]+=1\n\t\t\tn-=i\n\t\tend\n\t\tj+=i*2\n\tend\n\ti<<=1\nend\nif n!=0\n\tputs \"-1\"\nelse\n\tputs sc[0]\n\tfor i in 1...sc[0]\n\t\tprint \"#{sc[i]} \"\n\tend\n\tputs sc[sc[0]]\nend"}, {"source_code": "#! /usr/bin/env ruby\n\ndef main\n sum, lmt = readline.split.map do |x| x.to_i end\n set = Array.new\n (1..lmt).reverse_each do |i|\n lb = i & -i\n if sum >= lb then\n sum -= lb\n set.push i\n end\n end\n if sum == 0 then\n puts set.size\n set.each do |x|\n print x, ' '\n end\n puts\n else\n puts -1\n end\nend\n\nmain\n"}, {"source_code": "m,n=gets.split.map &:to_i\na=[]\n(1..n).to_a.sort_by{|x|x&-x}.reverse_each{|i|if m-(i&-i)>=0 then m-=i&-i;a.push i end}\nexit !puts(-1) if m>0\np a.size\nputs a*' '\n"}, {"source_code": "m,n=gets.split.map &:to_i\na=[]\n(1..n).to_a.sort_by{|x|x&-x}.reverse_each{|i|if m-(i&-i)>=0 then m-=i&-i;a.push i end}\nexit !puts(-1) if m>0\np a.size\nputs a*' '\n"}, {"source_code": "m,n=gets.split.map &:to_i\na=[]\n(1..n).to_a.sort_by{|x|x&-x}.reverse_each{|i|if m-(i&-i)>=0 then m-=i&-i;a.push i end}\nexit !puts(-1) if m>0\np a.size\nputs a*' '"}, {"source_code": "m,n=gets.split.map &:to_i\na=[]\n(1..n).to_a.sort_by{|x|x&-x}.reverse_each{|i|if m-(i&-i)>=0 then m-=i&-i;a.push i end}\nexit !puts(-1) if m>0\np a.size\nputs a*' '"}, {"source_code": "m,n=gets.split.map &:to_i\na,now=[],0\nans=[]\n(1..n).each{|i|a.push i}\na.sort_by!{|x|x&-x}\na.reverse.each{|i|if (i&-i)+now<=m then now+=i&-i;ans.push i end}\nexit !puts(-1) if now!=m\np ans.size\nputs ans*' '\n"}, {"source_code": "def lowbit(n)\n i = 0\n while(n%2 == 0) do\n n /= 2\n i += 1\n end\n return 2**i\nend\n\nsum,limit = gets.split.map(&:to_i)\nnums = []\n(1..limit).each do |x|\n nums << [x,lowbit(x)]\nend \nnums.sort_by!{ |x| x[1] }\nnums.reverse!\n#puts nums.map{ |x| \"[#{x[0]} #{x[1]}]\" }.join \" \"\nret = []\nnums.each do |x|\n next if(sum < x[1])\n sum -= x[1]\n ret << x[0]\nend\nif(sum == 0)\n puts ret.size\n puts ret.join(\" \")\nelse\n puts -1\nend\n\n\n\n\n\n\n"}, {"source_code": "m,n=gets.split.map &:to_i\na=[]\n(1..n).to_a.sort_by{|x|x&-x}.reverse_each{|i|if m-(i&-i)>=0 then m-=i&-i;a.push i end}\nexit !puts(-1) if m>0\np a.size\nputs a*' '\n"}, {"source_code": "def subset_sum(s,n,arr)\n\treturn true if s == 0\n\treturn false if n == 0 && s != 0\n\treturn subset_sum(s,n-1,arr) if arr[n-1] > s\n\n\tif subset_sum(s-arr[n-1],n-1,arr)\n\t\t@sum_arr << n\n\t\treturn true\n\tend \n\treturn subset_sum(s,n-1,arr)\n\nend\n\nsum,limit = gets.split(\" \").map(&:to_i)\n@sum_arr = []\narr = []\n(1..limit).each do |i|\n\tarr << (i ^ (i & (i-1)))\nend\nones = (limit/2.to_f).ceil\nlist = arr.each_with_index.sort.reverse\n# puts \"#{ones}\"\n# puts \"#{list}\"\n\nans = false\nans_list = []\nlist.each do |a|\n\tif sum <= ones\n\t\tans = true\n\t\tbreak \n\tend\n\tbreak if a[0] == 1 \n\tsum -= a[0]\n\tans_list << a[1] + 1\nend\nif ans\n\tone_index = list.index{|i| i[0] == 1}\n\twhile sum > 0\n\t\tsum -= 1\n\t\tans_list << list[one_index][1] + 1\n\t\tone_index += 1\n\tend\n\tputs ans_list.length\n\tputs ans_list.join(\" \")\nelse\n\tputs \"-1\"\nend\n# subset_sum(sum,arr.length,arr)\n\n# if @sum_arr.length != 0\n# \tputs @sum_arr.length\n# \tputs @sum_arr.join(\" \")\n# else\n# \tputs \"-1\"\n# end"}, {"source_code": "m,n=gets.split.map &:to_i\na=[]\n(1..n).to_a.sort_by{|x|x&-x}.reverse_each{|i|if m-(i&-i)>=0 then m-=i&-i;a.push i end}\nexit !puts(-1) if m>0\np a.size\nputs a*' '\n"}, {"source_code": "m,n=gets.split.map &:to_i\na=[]\n(1..n).to_a.sort_by{|x|x&-x}.reverse_each{|i|if m-(i&-i)>=0 then m-=i&-i;a.push i end}\nexit !puts(-1) if m>0\np a.size\nputs a*' '\n"}, {"source_code": "m,n=gets.split.map &:to_i\na=[]\n(1..n).to_a.sort_by{|x|x&-x}.reverse_each{|i|if m-(i&-i)>=0 then m-=i&-i;a.push i end}\nexit !puts(-1) if m>0\np a.size\nputs a*' '\n"}], "negative_code": [{"source_code": "def lowbit(n)\n i = 0\n while(n%2 == 0) do\n n /= 2\n i += 1\n end\n return 2**i\nend\n\nsum,limit = gets.split.map(&:to_i)\nnums = []\n(1..limit).each do |x|\n nums << [x,lowbit(x)]\nend \nnums.sort_by!{ |x| x[1] }\nnums.reverse!\n#puts nums.map{ |x| \"[#{x[0]} #{x[1]}]\" }.join \" \"\nret = []\nnums.each do |x|\n next if(sum < x[1])\n sum -= x[1]\n ret << x[0]\nend\nif(sum == 0)\n puts ret.join(\" \")\nelse\n puts -1\nend\n"}, {"source_code": "def subset_sum(s,n,arr)\n\treturn true if s == 0\n\treturn false if n == 0 && s != 0\n\treturn subset_sum(s,n-1,arr) if arr[n-1] > s\n\n\tif subset_sum(s-arr[n-1],n-1,arr)\n\t\t@sum_arr << n\n\t\treturn true\n\tend \n\treturn subset_sum(s,n-1,arr)\n\nend\n\nsum,limit = gets.split(\" \").map(&:to_i)\n@sum_arr = []\narr = []\n(1..limit).each do |i|\n\tarr << (i ^ (i & (i-1)))\nend\nones = (limit/2.to_f).ceil\nlist = arr.each_with_index.sort.reverse\n# puts \"#{ones}\"\n# puts \"#{list}\"\n\nans = false\nans_list = []\nlist.each do |a|\n\tbreak if a[0] == 1 \n\tif sum <= ones\n\t\tans = true\n\t\tbreak \n\tend\n\tsum -= a[0]\n\tans_list << a[1] + 1\nend\nif ans\n\tone_index = list.index{|i| i[0] == 1}\n\twhile sum > 0\n\t\tsum -= 1\n\t\tans_list << list[one_index][1] + 1\n\t\tone_index += 1\n\tend\n\tputs ans_list.length\n\tputs ans_list.join(\" \")\nelse\n\tputs \"-1\"\nend\n# subset_sum(sum,arr.length,arr)\n\n# if @sum_arr.length != 0\n# \tputs @sum_arr.length\n# \tputs @sum_arr.join(\" \")\n# else\n# \tputs \"-1\"\n# end"}, {"source_code": "def subset_sum(s,n,arr)\n\treturn true if s == 0\n\treturn false if n == 0 && s != 0\n\treturn subset_sum(s,n-1,arr) if arr[n-1] > s\n\n\tif subset_sum(s-arr[n-1],n-1,arr)\n\t\t@sum_arr << n\n\t\treturn true\n\tend \n\treturn subset_sum(s,n-1,arr)\n\nend\n\nsum,limit = gets.split(\" \").map(&:to_i)\n@sum_arr = []\narr = []\n(1..limit).each do |i|\n\tarr << (i ^ (i & (i-1)))\nend\nones = (limit/2.to_f).ceil\nlist = arr.each_with_index.sort.reverse\n# puts \"#{ones}\"\n# puts \"#{list}\"\n\nans = false\nans_list = []\nlist.each do |a|\n\tif sum <= ones\n\t\tans = true\n\t\tbreak \n\tend\n\tsum -= a[0]\n\tans_list << a[1] + 1\nend\nif ans\n\tone_index = list.index{|i| i[0] == 1}\n\twhile sum > 0\n\t\tsum -= 1\n\t\tans_list << list[one_index][1] + 1\n\t\tone_index += 1\n\tend\n\tputs ans_list.length\n\tputs ans_list.join(\" \")\nelse\n\tputs \"-1\"\nend\n# subset_sum(sum,arr.length,arr)\n\n# if @sum_arr.length != 0\n# \tputs @sum_arr.length\n# \tputs @sum_arr.join(\" \")\n# else\n# \tputs \"-1\"\n# end"}], "src_uid": "1e4b638810ea9fa83c60a934ad49bf5e"} {"nl": {"description": "This problem differs from the next one only in the presence of the constraint on the equal length of all numbers $$$a_1, a_2, \\dots, a_n$$$. Actually, this problem is a subtask of the problem D2 from the same contest and the solution of D2 solves this subtask too.A team of SIS students is going to make a trip on a submarine. Their target is an ancient treasure in a sunken ship lying on the bottom of the Great Rybinsk sea. Unfortunately, the students don't know the coordinates of the ship, so they asked Meshanya (who is a hereditary mage) to help them. He agreed to help them, but only if they solve his problem.Let's denote a function that alternates digits of two numbers $$$f(a_1 a_2 \\dots a_{p - 1} a_p, b_1 b_2 \\dots b_{q - 1} b_q)$$$, where $$$a_1 \\dots a_p$$$ and $$$b_1 \\dots b_q$$$ are digits of two integers written in the decimal notation without leading zeros.In other words, the function $$$f(x, y)$$$ alternately shuffles the digits of the numbers $$$x$$$ and $$$y$$$ by writing them from the lowest digits to the older ones, starting with the number $$$y$$$. The result of the function is also built from right to left (that is, from the lower digits to the older ones). If the digits of one of the arguments have ended, then the remaining digits of the other argument are written out. Familiarize with examples and formal definitions of the function below.For example: $$$$$$f(1111, 2222) = 12121212$$$$$$ $$$$$$f(7777, 888) = 7787878$$$$$$ $$$$$$f(33, 44444) = 4443434$$$$$$ $$$$$$f(555, 6) = 5556$$$$$$ $$$$$$f(111, 2222) = 2121212$$$$$$Formally, if $$$p \\ge q$$$ then $$$f(a_1 \\dots a_p, b_1 \\dots b_q) = a_1 a_2 \\dots a_{p - q + 1} b_1 a_{p - q + 2} b_2 \\dots a_{p - 1} b_{q - 1} a_p b_q$$$; if $$$p < q$$$ then $$$f(a_1 \\dots a_p, b_1 \\dots b_q) = b_1 b_2 \\dots b_{q - p} a_1 b_{q - p + 1} a_2 \\dots a_{p - 1} b_{q - 1} a_p b_q$$$. Mishanya gives you an array consisting of $$$n$$$ integers $$$a_i$$$. All numbers in this array are of equal length (that is, they consist of the same number of digits). Your task is to help students to calculate $$$\\sum_{i = 1}^{n}\\sum_{j = 1}^{n} f(a_i, a_j)$$$ modulo $$$998\\,244\\,353$$$.", "input_spec": "The first line of the input contains a single integer $$$n$$$ ($$$1 \\le n \\le 100\\,000$$$) — the number of elements in the array. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$) — the elements of the array. All numbers $$$a_1, a_2, \\dots, a_n$$$ are of equal length (that is, they consist of the same number of digits).", "output_spec": "Print the answer modulo $$$998\\,244\\,353$$$.", "sample_inputs": ["3\n12 33 45", "2\n123 456", "1\n1", "5\n1000000000 1000000000 1000000000 1000000000 1000000000"], "sample_outputs": ["26730", "1115598", "11", "265359409"], "notes": null}, "positive_code": [{"source_code": "MOD = 998244353\nn = gets.to_i\na = gets.split.map(&:to_i)\ncnt = [0]*10\na.each do |v|\n cnt[Math.log10(v).to_i] += 1\nend\nans = 0\na.each do |v|\n d = Math.log10(v).to_i\n l,r,m = v,0,1\n 10.times do |i|\n r += m*(l%10)\n l /= 10\n m *= 100\n if i < d\n ans += l * m * cnt[i] * 2 % MOD\n ans += (r + r*10) * cnt[i] % MOD\n else\n ans += (r + r*10) * cnt[i] % MOD\n end\n ans %= MOD\n end\nend\np ans\n"}, {"source_code": "n = gets.to_i\na = gets.split(/\\s+/).map(&:to_i)\nmod = 998244353\n\nten = (0..21).map{|e| 10**e}\nans = 0\na.each do |x|\n t = 0\n while x > 0\n p = x % 10;\n ans = (ans + (p*n*ten[t]) % mod) % mod\n ans = (ans + (p*n*ten[t+1]) % mod) % mod\n x /= 10\n t += 2\n end\nend\n\nputs ans\n"}], "negative_code": [], "src_uid": "6e093dbcbcaa7c87c9b62546745984db"} {"nl": {"description": "There are n points on a straight line, and the i-th point among them is located at xi. All these coordinates are distinct.Determine the number m — the smallest number of points you should add on the line to make the distances between all neighboring points equal. ", "input_spec": "The first line contains a single integer n (3 ≤ n ≤ 100 000) — the number of points. The second line contains a sequence of integers x1, x2, ..., xn ( - 109 ≤ xi ≤ 109) — the coordinates of the points. All these coordinates are distinct. The points can be given in an arbitrary order.", "output_spec": "Print a single integer m — the smallest number of points you should add on the line to make the distances between all neighboring points equal. ", "sample_inputs": ["3\n-5 10 5", "6\n100 200 400 300 600 500", "4\n10 9 0 -1"], "sample_outputs": ["1", "0", "8"], "notes": "NoteIn the first example you can add one point with coordinate 0.In the second example the distances between all neighboring points are already equal, so you shouldn't add anything."}, "positive_code": [{"source_code": "n = gets.to_i\narr = gets.chomp.split.map {|x| x.to_i}.sort\n\nlen = arr[-1] - arr[0]\ngcd = arr[1] - arr[0]\narr.each_with_index do |x, i|\n gcd = gcd.gcd(x - arr[i - 1]) if i > 0\nend\n\nputs len / gcd + 1 - n"}, {"source_code": "n = gets.to_i\nx = gets.split.map(&:to_i).sort\nx1 = x[1..-1]\nx2 = x[0..-2]\nx3 = []\n(0..(n-2)).each{|i|\n \n x3.push(x1[i] - x2[i])\n}\n\ng = x3.reduce(x3[0], :gcd)\n\nk = 0\nx3.each{\n |v|\n k = k + (v / g).to_i - 1\n}\nprint(k)\n"}, {"source_code": "n = readline().to_i\na = readline().split().map(&:to_i).sort\n\ng = 0\nleft = right = a[0]\nfor i in 1..n-1\n\tg = g.gcd(a[i]-a[i-1])\n\tleft = [a[i], left].min\n\tright = [a[i], right].max\nend\n\nputs (right-left)/g + 1 - n\n\n"}, {"source_code": "n = gets.to_i\n\nxs = gets.split(\" \").map(&:to_i).sort\n\ng = 0\n(1...n).each do |i|\n g = g.gcd(xs[i] - xs[i-1])\nend\n\nres = 0\n(1...n).each do |i|\n res += (xs[i] - xs[i-1]) / g - 1\nend\n\nputs res\n"}, {"source_code": "def gcd(a,b)\n if (b == 0)\n a\n else\n gcd(b,a%b)\n end\nend\n\nn = readline.to_i\n\na = readline.split.map(&:to_i).sort\n\ng = a[1] - a[0]\nfor i in 1 .. n-2\n g = gcd(g, a[i+1]-a[i])\nend\n\nans = 0\nfor i in 0 .. n-2\n ans += (a[i+1] - a[i]) / g - 1\nend\n\np ans"}, {"source_code": "def gcd(a,b)\n if (b == 0)\n a\n else\n gcd(b,a%b)\n end\nend\n\nn = readline.to_i\n\na = readline.split.map(&:to_i).sort\n\ng = a[1] - a[0]\nfor i in 1 .. n-2\n g = gcd(g, a[i+1]-a[i])\nend\n\nans = 0\nfor i in 0 .. n-2\n ans += (a[i+1] - a[i]) / g - 1\nend\n\np ans"}, {"source_code": "def gcd a, b\n return a if b.zero?\n gcd b, a % b\nend\n\nn = gets.to_i\na = gets.split.map(&:to_i)\nt=0\na.sort!\n\n$num = n\n$i = 1\nwhile $i < $num do\n a[$i] = a[$i] - a[0]\n $i +=1\nend\na[0]=0\nans = gcd(a[1],a[2])\n\n$num = n\n$i = 3\nwhile $i < $num do\n ans = gcd(ans, a[$i])\n $i +=1\nend\nans = (a[n-1] / ans) - n + 1\n\nputs ans"}, {"source_code": "n = gets.to_i\narr = gets.split\ni = 0\nwhile i < arr.length\n arr[i] = arr[i].to_i\n i = i + 1\nend\narr = arr.sort\nif arr.length == 1\n puts 0\nelse \n g = arr[1] - arr[0]\n i = 2\n while i < arr.length\n a = g\n b = arr[i] - arr[i - 1]\n while b != 0\n tmp = b\n b = a % b\n a = tmp\n end\n i = i + 1\n g = a\n #puts g\n end\n i = 1\n ans = 0\n while i != arr.length\n ans = ans + (arr[i] - arr[i - 1]) / g - 1\n i = i + 1\n end\n puts ans\nend\n"}], "negative_code": [], "src_uid": "805d82c407c1b34f217f8836454f7e09"} {"nl": {"description": "There are n servers in a laboratory, each of them can perform tasks. Each server has a unique id — integer from 1 to n.It is known that during the day q tasks will come, the i-th of them is characterized with three integers: ti — the moment in seconds in which the task will come, ki — the number of servers needed to perform it, and di — the time needed to perform this task in seconds. All ti are distinct.To perform the i-th task you need ki servers which are unoccupied in the second ti. After the servers begin to perform the task, each of them will be busy over the next di seconds. Thus, they will be busy in seconds ti, ti + 1, ..., ti + di - 1. For performing the task, ki servers with the smallest ids will be chosen from all the unoccupied servers. If in the second ti there are not enough unoccupied servers, the task is ignored.Write the program that determines which tasks will be performed and which will be ignored.", "input_spec": "The first line contains two positive integers n and q (1 ≤ n ≤ 100, 1 ≤ q ≤ 105) — the number of servers and the number of tasks. Next q lines contains three integers each, the i-th line contains integers ti, ki and di (1 ≤ ti ≤ 106, 1 ≤ ki ≤ n, 1 ≤ di ≤ 1000) — the moment in seconds in which the i-th task will come, the number of servers needed to perform it, and the time needed to perform this task in seconds. The tasks are given in a chronological order and they will come in distinct seconds. ", "output_spec": "Print q lines. If the i-th task will be performed by the servers, print in the i-th line the sum of servers' ids on which this task will be performed. Otherwise, print -1.", "sample_inputs": ["4 3\n1 3 2\n2 2 1\n3 4 3", "3 2\n3 2 3\n5 1 2", "8 6\n1 3 20\n4 2 1\n6 5 5\n10 1 1\n15 3 6\n21 8 8"], "sample_outputs": ["6\n-1\n10", "3\n3", "6\n9\n30\n-1\n15\n36"], "notes": "NoteIn the first example in the second 1 the first task will come, it will be performed on the servers with ids 1, 2 and 3 (the sum of the ids equals 6) during two seconds. In the second 2 the second task will come, it will be ignored, because only the server 4 will be unoccupied at that second. In the second 3 the third task will come. By this time, servers with the ids 1, 2 and 3 will be unoccupied again, so the third task will be done on all the servers with the ids 1, 2, 3 and 4 (the sum of the ids is 10).In the second example in the second 3 the first task will come, it will be performed on the servers with ids 1 and 2 (the sum of the ids is 3) during three seconds. In the second 5 the second task will come, it will be performed on the server 3, because the first two servers will be busy performing the first task."}, "positive_code": [{"source_code": "n, q = gets.split.map(&:to_i)\nserver = Array.new(n, 0)\ntkds = q.times.map { gets.split.map(&:to_i) }\ntkds.sort_by! { |x| x[0] }\ntkds.each_with_index do |tkd, i|\n t, k, d = tkd\n # if i > 0\n # dt = t - tkds[i - 1][0]\n # server.map! { |x| x == 0 ? 0 : x - [x, dt].min }\n # end\n if server.count { |x| x < t } < k\n puts -1\n else\n use = []\n count = 0\n ans = 0\n server.each_with_index do |x, i|\n if x < t\n server[i] = t + d - 1\n ans += i + 1\n count += 1\n end\n break if count == k\n end\n puts ans\n # use = server.each_with_index\n # .map { |x, i| [x, i] }\n # .select { |a| a[0] == 0 }\n # .take(k)\n # use.each { |a| server[a[1]] = d }\n # puts use.inject(0) { |a, b| a + b[1] + 1 }\n end\nend\n"}], "negative_code": [], "src_uid": "9f46205d62ba05d8bcc2c56f84b2d2b2"} {"nl": {"description": "In this problem you will have to deal with a real algorithm that is used in the VK social network.As in any other company that creates high-loaded websites, the VK developers have to deal with request statistics regularly. An important indicator reflecting the load of the site is the mean number of requests for a certain period of time of T seconds (for example, T = 60 seconds = 1 min and T = 86400 seconds = 1 day). For example, if this value drops dramatically, that shows that the site has access problem. If this value grows, that may be a reason to analyze the cause for the growth and add more servers to the website if it is really needed.However, even such a natural problem as counting the mean number of queries for some period of time can be a challenge when you process the amount of data of a huge social network. That's why the developers have to use original techniques to solve problems approximately, but more effectively at the same time.Let's consider the following formal model. We have a service that works for n seconds. We know the number of queries to this resource at at each moment of time t (1 ≤ t ≤ n). Let's formulate the following algorithm of calculating the mean with exponential decay. Let c be some real number, strictly larger than one.// setting this constant value correctly can adjust // the time range for which statistics will be calculateddouble c = some constant value; // as the result of the algorithm's performance this variable will contain // the mean number of queries for the last // T seconds by the current moment of timedouble mean = 0.0; for t = 1..n: // at each second, we do the following: // at is the number of queries that came at the last second; mean = (mean + at / T) / c;Thus, the mean variable is recalculated each second using the number of queries that came at that second. We can make some mathematical calculations and prove that choosing the value of constant c correctly will make the value of mean not very different from the real mean value ax at t - T + 1 ≤ x ≤ t. The advantage of such approach is that it only uses the number of requests at the current moment of time and doesn't require storing the history of requests for a large time range. Also, it considers the recent values with the weight larger than the weight of the old ones, which helps to react to dramatic change in values quicker.However before using the new theoretical approach in industrial programming, there is an obligatory step to make, that is, to test its credibility practically on given test data sets. Your task is to compare the data obtained as a result of the work of an approximate algorithm to the real data. You are given n values at, integer T and real number c. Also, you are given m moments pj (1 ≤ j ≤ m), where we are interested in the mean value of the number of queries for the last T seconds. Implement two algorithms. The first one should calculate the required value by definition, i.e. by the formula . The second algorithm should calculate the mean value as is described above. Print both values and calculate the relative error of the second algorithm by the formula , where approx is the approximate value, obtained by the second algorithm, and real is the exact value obtained by the first algorithm.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 2·105), integer T (1 ≤ T ≤ n) and real number c (1 < c ≤ 100) — the time range when the resource should work, the length of the time range during which we need the mean number of requests and the coefficient c of the work of approximate algorithm. Number c is given with exactly six digits after the decimal point. The next line contains n integers at (1 ≤ at ≤ 106) — the number of queries to the service at each moment of time. The next line contains integer m (1 ≤ m ≤ n) — the number of moments of time when we are interested in the mean number of queries for the last T seconds. The next line contains m integers pj (T ≤ pj ≤ n), representing another moment of time for which we need statistics. Moments pj are strictly increasing.", "output_spec": "Print m lines. The j-th line must contain three numbers real, approx and error, where: is the real mean number of queries for the last T seconds; approx is calculated by the given algorithm and equals mean at the moment of time t = pj (that is, after implementing the pj-th iteration of the cycle); is the relative error of the approximate algorithm. The numbers you printed will be compared to the correct numbers with the relative or absolute error 10 - 4. It is recommended to print the numbers with at least five digits after the decimal point.", "sample_inputs": ["1 1 2.000000\n1\n1\n1", "11 4 1.250000\n9 11 7 5 15 6 6 6 6 6 6\n8\n4 5 6 7 8 9 10 11", "13 4 1.250000\n3 3 3 3 3 20 3 3 3 3 3 3 3\n10\n4 5 6 7 8 9 10 11 12 13"], "sample_outputs": ["1.000000 0.500000 0.500000", "8.000000 4.449600 0.443800\n9.500000 6.559680 0.309507\n8.250000 6.447744 0.218455\n8.000000 6.358195 0.205226\n8.250000 6.286556 0.237993\n6.000000 6.229245 0.038207\n6.000000 6.183396 0.030566\n6.000000 6.146717 0.024453", "3.000000 1.771200 0.409600\n3.000000 2.016960 0.327680\n7.250000 5.613568 0.225715\n7.250000 5.090854 0.297813\n7.250000 4.672684 0.355492\n7.250000 4.338147 0.401635\n3.000000 4.070517 0.356839\n3.000000 3.856414 0.285471\n3.000000 3.685131 0.228377\n3.000000 3.548105 0.182702"], "notes": null}, "positive_code": [{"source_code": "total_time, time_slice, constant = gets.strip.split(\" \")\n\ntotal_time = total_time.to_i \ntime_slice = time_slice.to_i\nconstant = constant.to_f\n\narr_requests = gets.strip.split(\" \").map(&:to_i)\n\nresults = Hash.new\n\nmean = 0.0\nj = 0\nwhile j < time_slice-1 do\n mean = (mean + (arr_requests[j].to_f / time_slice.to_f)) / constant\n j+=1\nend\n\narr_summ = arr_requests.slice(0,time_slice-1).inject(:+).to_f\n\nputs \ni = time_slice-1\nwhile i < arr_requests.length do \n k = i+1\n\n arr_summ+=arr_requests[i]\n arr_summ -= arr_requests[i-time_slice] if i > time_slice-1\n\n real = arr_summ / time_slice\n \n mean = (mean + (arr_requests[i].to_f / time_slice.to_f)) / constant\n err = (mean - real).abs / real\n\n results[k] = {real: real, mean: mean, error: err}\n i+=1\nend\n\nrq = gets\nrequests = gets.strip.split(\" \")\nrequests.each do |r|\n r = r.to_i\n puts sprintf(\"%.6f %.6f %.6f\\n\", results[r][:real], results[r][:mean], results[r][:error])\nend"}, {"source_code": "(n, t, c) = gets.scan(/[\\d\\.]+/).map(&:to_f)\nn = n.to_i\nt = t.to_i\na = gets.scan(/\\d+/).map(&:to_i)\nm = gets.scan(/\\d+/).map(&:to_i)\na = [a] if a.is_a? Fixnum\nm = [m] if a.is_a? Fixnum\np_arr = gets.scan(/\\d+/).map(&:to_i)\n\nmean = [0.0]*(n+1)\nreal = [0.0]*(n+1)\n\n(n).times do |i|\n mean[i+1] = (mean[i] + a[i].to_f/t) / c\n real[i+1] = real[i] + a[i]\nend\np_arr.each do |p|\n r = (real[p] - real[p-t])/t\n puts \"%.6f %.6f %.6f\" % [r.round(6), mean[p].round(6), ((mean[p]-r).abs/r).round(6)]\nend"}], "negative_code": [{"source_code": "total_time, time_slice, constant = gets.strip.split(\" \")\n\ntotal_time = total_time.to_i \ntime_slice = time_slice.to_i\nconstant = constant.to_f\n\narr_requests = gets.strip.split(\" \").map(&:to_i)\n\n\nresults = Hash.new\n\nmean = 0.0\nj = 0\nwhile j < time_slice-1 do\n mean = (mean + (arr_requests[j].to_f / time_slice.to_f)) / constant\n j+=1\nend\n\narr_summ = arr_requests.slice(0,time_slice-1).inject(:+).to_f\n\nputs \ni = time_slice-1\nwhile i < arr_requests.length do \n k = i+1\n\n arr_summ+=arr_requests[i]\n arr_summ -= arr_requests[i-4] if i > 3\n\n real = arr_summ / time_slice\n \n mean = (mean + (arr_requests[i].to_f / time_slice.to_f)) / constant\n err = (mean - real).abs / real\n\n results[k] = {real: real, mean: mean, error: err}\n i+=1\nend\n\nrq = gets\nrequests = gets.strip.split(\" \")\nrequests.each do |r|\n r = r.to_i\n puts sprintf(\"%.6f %.6f %.6f\\n\", results[r][:real], results[r][:mean], results[r][:error])\nend"}], "src_uid": "be8d2eff2f34e72625566680ae188c49"} {"nl": {"description": "Petya has n positive integers a1, a2, ..., an. His friend Vasya decided to joke and replaced all digits in Petya's numbers with a letters. He used the lowercase letters of the Latin alphabet from 'a' to 'j' and replaced all digits 0 with one letter, all digits 1 with another letter and so on. For any two different digits Vasya used distinct letters from 'a' to 'j'.Your task is to restore Petya's numbers. The restored numbers should be positive integers without leading zeros. Since there can be multiple ways to do it, determine the minimum possible sum of all Petya's numbers after the restoration. It is guaranteed that before Vasya's joke all Petya's numbers did not have leading zeros.", "input_spec": "The first line contains a single integer n (1 ≤ n ≤ 1 000) — the number of Petya's numbers. Each of the following lines contains non-empty string si consisting of lowercase Latin letters from 'a' to 'j' — the Petya's numbers after Vasya's joke. The length of each string does not exceed six characters.", "output_spec": "Determine the minimum sum of all Petya's numbers after the restoration. The restored numbers should be positive integers without leading zeros. It is guaranteed that the correct restore (without leading zeros) exists for all given tests.", "sample_inputs": ["3\nab\nde\naj", "5\nabcdef\nghij\nbdef\naccbd\ng", "3\naa\njj\naa"], "sample_outputs": ["47", "136542", "44"], "notes": "NoteIn the first example, you need to replace the letter 'a' with the digit 1, the letter 'b' with the digit 0, the letter 'd' with the digit 2, the letter 'e' with the digit 3, and the letter 'j' with the digit 4. So after the restoration numbers will look like [10, 23, 14]. The sum of them is equal to 47, which is the minimum possible sum of the numbers after the correct restoration.In the second example the numbers after the restoration can look like: [120468, 3579, 2468, 10024, 3]. In the second example the numbers after the restoration can look like: [11, 22, 11]. "}, "positive_code": [{"source_code": "N = $stdin.gets.chomp.to_i\n\nd = Hash[(\"a\"..\"j\").map{ |a| [a,0] }]\nleadings = {}\nN.times do\n digits = $stdin.gets.chomp.split(\"\")\n digits.reverse.each_with_index { |e,i|\n if i == digits.size - 1\n leadings[e] = 1\n end\n d[e] += 10 ** i\n }\nend\n\n#p leadings\nr = d.map{ |k,v| [k,v] }.sort_by{ |r| r[1] }.reverse\n#p r\nr.each_with_index do |e,i|\n #p [e,i]\n if !leadings.has_key?(e[0])\n f = r.delete_at(i)\n r.unshift(f)\n break\n end\nend\n#p r\nputs r.map{ |r| r[1] }.zip((0..9).to_a).map{ |n,k| n*k }.inject(:+)\n"}], "negative_code": [], "src_uid": "f67e221f88a8277b60a9b8fcb90a163d"} {"nl": {"description": "Polycarpus participates in a competition for hacking into a new secure messenger. He's almost won.Having carefully studied the interaction protocol, Polycarpus came to the conclusion that the secret key can be obtained if he properly cuts the public key of the application into two parts. The public key is a long integer which may consist of even a million digits!Polycarpus needs to find such a way to cut the public key into two nonempty parts, that the first (left) part is divisible by a as a separate number, and the second (right) part is divisible by b as a separate number. Both parts should be positive integers that have no leading zeros. Polycarpus knows values a and b.Help Polycarpus and find any suitable method to cut the public key.", "input_spec": "The first line of the input contains the public key of the messenger — an integer without leading zeroes, its length is in range from 1 to 106 digits. The second line contains a pair of space-separated positive integers a, b (1 ≤ a, b ≤ 108).", "output_spec": "In the first line print \"YES\" (without the quotes), if the method satisfying conditions above exists. In this case, next print two lines — the left and right parts after the cut. These two parts, being concatenated, must be exactly identical to the public key. The left part must be divisible by a, and the right part must be divisible by b. The two parts must be positive integers having no leading zeros. If there are several answers, print any of them. If there is no answer, print in a single line \"NO\" (without the quotes).", "sample_inputs": ["116401024\n97 1024", "284254589153928171911281811000\n1009 1000", "120\n12 1"], "sample_outputs": ["YES\n11640\n1024", "YES\n2842545891539\n28171911281811000", "NO"], "notes": null}, "positive_code": [{"source_code": "input = gets.chomp\na, b = gets.chomp.split(\" \")\na = a.to_i\nb = b.to_i\n\nnum = Array.new(input.length)\nfor i in 0...num.length\n num[i] = input[i].to_i\nend\n\nleft = Array.new(num.length)\nacc = 0\nfor i in 0...num.length\n acc = acc * 10 + num[i]\n acc %= a\n left[i] = acc\nend\n\nacc = 0\nrem = 1\nans = false\nfor i in (num.length-1).downto(1)\n acc = acc + num[i] * rem\n rem *= 10\n rem %= b\n acc %= b\n if num[i] != 0 && left[i-1] == 0 && acc == 0\n puts \"YES\"\n puts input[0...i]\n puts input[i...input.length]\n ans = true\n break\n end\nend\n\nif !ans\n puts \"NO\"\nend"}, {"source_code": "input0 = gets.chomp\nkey = input0.split(\"\").map { |e| e.ord - 48 }\ninput = gets.chomp.split\na = Integer(input[0])\nb = Integer(input[1])\n\nl = key.length\nleft = Array.new(l-1)\nremainder = key[0].to_i % a\nleft[0] = remainder\n\nfor i in 1...l-1\n remainder_b = left[i-1] * 10\n left[i] = (key[i] + remainder_b) % a\nend\n\nright = key[l-1].to_i % b\n\nsplit = 0\nanswer = false\n\nif right == 0 && left[l-2] == 0 && key[l-1] != 0\n split = l-2\n answer = true\nelse\n c = 10 % b\n d = 1\n for i in 1...l-1\n d = (c * d) % b\n\n remainder_b = key[l-1-i] * d\n right = (right + remainder_b) % b\n if right == 0 && left[l-i-2] == 0\n if key[l-i-1] != 0\n split = l-i-2\n answer = true\n break\n end\n end\n end\nend\n\nif !answer\n puts \"NO\"\nelse\n x = input0[0..split]\n y = input0[split+1..l-1]\n puts \"YES\"\n print x\n puts\n print y\nend"}], "negative_code": [{"source_code": "input = gets.chomp\na, b = gets.chomp.split(\" \")\na = a.to_i\nb = b.to_i\n\nnum = Array.new(input.length)\nfor i in 0...num.length\n num[i] = input[i].to_i\nend\n\nleft = Array.new(num.length)\nacc = 0\nfor i in 0...num.length\n acc = acc * 10 + num[i]\n acc %= a\n left[i] = acc\nend\n\nacc = 0\nrem = 1\nans = false\nfor i in (num.length-1).downto(1)\n acc = acc + num[i] * rem\n rem *= 10\n rem %= b\n acc %= b\n if num[i] != 0 && left[i-1] == 0 && acc == 0\n puts \"YES\"\n puts input[0, i - 1]\n puts input[i, -1]\n ans = true\n break\n end\nend\n\nif !ans\n puts \"NO\"\nend"}, {"source_code": "key = gets.chomp.split(\"\")\ninput = gets.chomp.split\na = Integer(input[0])\nb = Integer(input[1])\n\nl = key.length\nleft = Array.new(l-1)\nremainder = key[0].to_i % a\nleft[0] = remainder\n\nfor i in 1...l-1\n key_i = key[i].to_i\n remainder_a = key_i % a\n remainder_b = (left[i-1] * (10 % a)) % a\n left[i] = (remainder_a + remainder_b) % a\nend\n\nright = Array.new(l-1)\nremainder = key[l-1].to_i % b\nright[l-2] = remainder\nten_remainder = Array.new(l-1)\nc = 10 % b\nd = c\nten_remainder[0] = c\nfor i in 1...l-1\n d = (c * d) % b\n ten_remainder[i] = d\nend\n\nfor i in 1...l-1\n key_i = key[l-1-i].to_i\n number = key_i * (10**i) + right[l-i-1][0]\n remainder_a = right[l-i-1]\n remainder_b = ((key_i % b) * ten_remainder[i-1]) % b\n right[l-i-2] = (remainder_a + remainder_b) % b\nend\nsplit = 0\nanswer = false\nfor i in 0...l-1\n if left[i] == right[i] && left[i] == 0\n split = i\n answer = true\n break\n end\nend\n\nif !answer\n puts \"NO\"\nelse\n puts \"YES\"\n print key[0..i].join\n puts\n print key[i+1..l-1].join\nend"}, {"source_code": "input0 = gets.chomp\nkey = input0.split(\"\").map { |e| e.ord - 48 }\ninput = gets.chomp.split\na = Integer(input[0])\nb = Integer(input[1])\n\nl = key.length\nleft = Array.new(l-1)\nremainder = key[0].to_i % a\nleft[0] = remainder\n\nfor i in 1...l-1\n remainder_b = left[i-1] * 10\n left[i] = (key[i] + remainder_b) % a\nend\n\nright = Array.new(l-1)\nremainder = key[l-1].to_i % b\nright[l-2] = remainder\nten_remainder = Array.new(l-1)\nc = 10 % b\nd = c\nten_remainder[0] = c\nfor i in 1...l-1\n d = (c * d) % b\n ten_remainder[i] = d\nend\n\nfor i in 1...l-1\n remainder_a = right[l-i-1]\n remainder_b = key[l-1-i] * ten_remainder[i-1]\n right[l-i-2] = (remainder_a + remainder_b) % b\nend\nsplit = 0\nanswer = false\nx = \"\"\ny = \"\"\nfor i in 0...l-1\n if left[i] == right[i] && left[i] == 0\n split = i\n if key[i+1] != \"0\"\n answer = true\n break\n end\n end\nend\nx = input0[0..split]\ny = input0[split+1..l-1]\nif !answer\n puts \"NO\"\nelse\n puts \"YES\"\n print x\n puts\n print y\nend"}, {"source_code": "key = gets.chomp.split(\"\")\ninput = gets.chomp.split\na = Integer(input[0])\nb = Integer(input[1])\n\nl = key.length\nleft = Array.new(l-1)\nremainder = key[0].to_i % a\nleft[0] = remainder\n\nfor i in 1...l-1\n key_i = key[i].to_i\n remainder_a = key_i % a\n remainder_b = (left[i-1] * (10 % a)) % a\n left[i] = remainder_a + remainder_b\nend\n\nright = Array.new(l-1)\nremainder = key[l-1].to_i % b\nright[l-2] = remainder\nten_remainder = Array.new(l-1)\nc = 10 % b\nd = c\nten_remainder[0] = c\nfor i in 1...l-1\n d = (c * d) % b\n ten_remainder[i] = d\nend\n\nfor i in 1...l-1\n key_i = key[l-1-i].to_i\n number = key_i * (10**i) + right[l-i-1][0]\n remainder_a = right[l-i-1]\n remainder_b = ((key_i % b) * ten_remainder[i-1]) % b\n right[l-i-2] = (remainder_a + remainder_b) % b\nend\nsplit = 0\nanswer = false\nfor i in 0...l-1\n if left[i] == right[i] && left[i] == 0\n split = i\n answer = true\n break\n end\nend\n\nif !answer\n puts \"NO\"\nelse\n puts \"YES\"\n print key[0..i].join\n puts\n print key[i+1..l-1].join\nend"}], "src_uid": "695418026140545863313f5f3cc1bf00"} {"nl": {"description": "Almost every text editor has a built-in function of center text alignment. The developers of the popular in Berland text editor «Textpad» decided to introduce this functionality into the fourth release of the product.You are to implement the alignment in the shortest possible time. Good luck!", "input_spec": "The input file consists of one or more lines, each of the lines contains Latin letters, digits and/or spaces. The lines cannot start or end with a space. It is guaranteed that at least one of the lines has positive length. The length of each line and the total amount of the lines do not exceed 1000. ", "output_spec": "Format the given text, aligning it center. Frame the whole text with characters «*» of the minimum size. If a line cannot be aligned perfectly (for example, the line has even length, while the width of the block is uneven), you should place such lines rounding down the distance to the left or to the right edge and bringing them closer left or right alternatively (you should start with bringing left). Study the sample tests carefully to understand the output format better.", "sample_inputs": ["This is\n\nCodeforces\nBeta\nRound\n5", "welcome to the\nCodeforces\nBeta\nRound 5\n\nand\ngood luck"], "sample_outputs": ["************\n* This is *\n* *\n*Codeforces*\n* Beta *\n* Round *\n* 5 *\n************", "****************\n*welcome to the*\n* Codeforces *\n* Beta *\n* Round 5 *\n* *\n* and *\n* good luck *\n****************"], "notes": null}, "positive_code": [{"source_code": "a = readlines.map(&:chop)\nn = a.map(&:size).max\nh = ['*' * n]\nc = 0\nputs (h + a + h).map{|_|\n\tif (n - _.size).odd?\n\t\t_ = c.odd? ? \" #{_}\" : \"#{_} \"\n\t\tc += 1\n\tend\n\t\"*#{_.center(n)}*\"\n} * \"\\n\"\n"}, {"source_code": "a = readlines.map(&:chop)\nn = a.map(&:size).max\nh = ['*' * n]\nc = 0\nputs (h + a + h).map{|_|\n\tif (n - _.size).odd?\n\t\t_ = c.odd? ? \" #{_}\" : \"#{_} \"\n\t\tc += 1\n\tend\n\t\"*#{_.center(n)}*\"\n} * \"\\n\"\n\n"}, {"source_code": "strings = Array.new\nmaxlen = 0\nwhile line = gets\n line.strip!\n if line.size>maxlen\n maxlen=line.size\n end\n strings << line\nend\nleft=true\n(maxlen+1).times {print \"*\"}\nputs \"*\"\nstrings.each do |s|\n diff = maxlen-s.size\n margin=diff/2\n print \"*\"\n margin.times {print \" \"}\n if diff%2==0\n print s\n else\n print \" \" if !left\n print s\n print \" \" if left\n left=!left\n end\n margin.times {print \" \"}\n puts \"*\"\nend\n(maxlen+1).times {print \"*\"}\nputs \"*\"\n"}, {"source_code": "a = readlines.map(&:chop)\nn = a.map(&:size).max\nh = ['*' * n]\nc = 0\nputs (h + a + h).map{|_|\n\tif (n - _.size).odd?\n\t\t_ = c.odd? ? \" #{_}\" : \"#{_} \"\n\t\tc += 1\n\tend\n\t\"*#{_.center(n)}*\"\n} * \"\\n\"\n\n"}, {"source_code": "a = readlines.map(&:chop)\nn = a.map(&:size).max\nh = ['*' * n]\nc = 0\nputs (h + a + h).map{|_|\n\tif (n - _.size).odd?\n\t\t_ = c.odd? ? \" #{_}\" : \"#{_} \"\n\t\tc += 1\n\tend\n\t\"*#{_.center(n)}*\"\n} * \"\\n\"\n"}, {"source_code": "def display(s,n,flag)\n t=(n-s.length)\n h=t/2\n t=t-h\n t,h=h,t if flag\n print \" \"*t\n print s\n print \" \"*h\nend\nmemo=[]\nmax=0\nwhile line=gets\n line.chomp!\n max=line.length if max haba\nend\nres = '*'*(haba+2)\nres += \"\\r\\n\"\nflag=true\nlines.each do |x|\n thisL = '*'\n s = (haba-x.size)\n if(s%2==0||flag)\n thisL += ' '*(s/2)\n flag=false unless s%2==0\n else\n thisL += ' '*(s/2+1)\n flag=true\n end\n thisL += x.chomp\n thisL += ' '*(haba+1-thisL.size)\n res += (thisL+\"*\\r\\n\")\nend\nres += '*'*(haba+2)\nprint res+\"\\r\\n\"\n"}, {"source_code": "lines = []\nmax_width = 0\nwhile (line = gets)\n\tline.strip!\n\tmax_width = line.length if line.length > max_width\n\tlines << line\nend\n\ntop_bottom = \"*\" * (max_width + 2)\nleft = true\nputs top_bottom\nlines.each do |line|\n\tspace = max_width - line.length\n\tleft_space = left ? space / 2 : (space / 2.0).ceil\n\tright_space = left ? (space / 2.0).ceil : space / 2\n\tleft = !left if space % 2 == 1\n\tputs \"*\" + \" \" * left_space + line + \" \" * right_space + \"*\"\nend\nputs top_bottom"}, {"source_code": "a = readlines.map(&:chop)\nn = a.map(&:size).max\nh = ['*' * n]\nc = 0\nputs (h + a + h).map{|_|\n\tif (n - _.size).odd?\n\t\t_ = c.odd? ? \" #{_}\" : \"#{_} \"\n\t\tc += 1\n\tend\n\t\"*#{_.center(n)}*\"\n} * \"\\n\"\n\n"}, {"source_code": "a = readlines.map(&:chop)\nn = a.map(&:size).max\nh = ['*' * n]\nc = 0\nputs (h + a + h).map{|_|\n\tif (n - _.size).odd?\n\t\t_ = c.odd? ? \" #{_}\" : \"#{_} \"\n\t\tc += 1\n\tend\n\t\"*#{_.center(n)}*\"\n} * \"\\n\""}, {"source_code": "a = readlines.map(&:chop)\nn = a.map(&:size).max\nh = ['*' * n]\nc = 0\nputs (h + a + h).map{|_|\n\tif (n - _.size).odd?\n\t\t_ = c.odd? ? \" #{_}\" : \"#{_} \"\n\t\tc += 1\n\tend\n\t\"*#{_.center(n)}*\"\n} * \"\\n\"\n\n"}, {"source_code": " \na = readlines.map(&:chop)\nn = a.map(&:size).max\nh = ['*' * n]\nc = 0\nputs (h + a + h).map{|_|\n\tif (n - _.size).odd?\n\t\t_ = c.odd? ? \" #{_}\" : \"#{_} \"\n\t\tc += 1\n\tend\n\t\"*#{_.center(n)}*\"\n} * \"\\n\""}, {"source_code": "a = readlines.map(&:chop)\nn = a.map(&:size).max\nh = ['*' * n]\nc = 0\nputs (h + a + h).map{|_|\n\tif (n - _.size).odd?\n\t\t_ = c.odd? ? \" #{_}\" : \"#{_} \"\n\t\tc += 1\n\tend\n\t\"*#{_.center(n)}*\"\n} * \"\\n\"\n\n"}, {"source_code": "input, max = [], 0\nwhile g = gets; input.push g.strip; end\ninput.map! { |i| l = i.length; max = l if l > max; [i, l] }\n\nputs '*' * (max + 2)\n\nleft = false\n\ninput.each do |i|\n\tprint '*'\n\tm = (max - i[1])\n\ts = m / 2\n\tif (m % 2).even?\n\t\tprint ' ' * s, i[0], ' ' * s\n\telse\n\t\tl, r = s, s\n\t\tleft ? l += 1 : r += 1\n\t\tprint ' ' * l, i[0], ' ' * r\n\t\tleft = !left\n\tend\n\tputs '*'\nend\n\nputs '*' * (max + 2)"}, {"source_code": "\ndef justify(str_arr)\n len_arr = str_arr.collect {|s| s.size}\n len = len_arr.max\n \n justify_left = true\n res = ['*' * (len+2)]\n str_arr.each do |s| \n spaces = len - s.size\n sp_left = spaces/2\n sp_right = spaces - spaces/2\n if spaces % 2 != 0 and s != ''\n if !justify_left\n sp_left += 1\n sp_right -= 1\n end\n justify_left = !justify_left\n end\n ss = ' '*sp_left + s + ' '*sp_right\n res << '*' + ss + '*'\n end \n res << '*' * (len+2)\n res\nend\n\n\nif __FILE__ == $0\n arr = []\n while line = gets\n arr << line.chomp\n end\n res = justify(arr)\n res.each {|s| puts s}\nend\n"}, {"source_code": "a = readlines.map(&:chop)\nn = a.map(&:size).max\nh = ['*' * n]\nc = 0\nputs (h + a + h).map{|_|\n\tif (n - _.size).odd?\n\t\t_ = c.odd? ? \" #{_}\" : \"#{_} \"\n\t\tc += 1\n\tend\n\t\"*#{_.center(n)}*\"\n} * \"\\n\"\n\n"}], "negative_code": [{"source_code": "def display(s,n)\n t=(n-s.length)\n h=t/2\n (t-h).times do\n print \" \"\n end\n print s\n h.times do\n print \" \"\n end\nend\nmemo=Array.new\nmax=0\nwhile line=gets\n line.chomp!\n max=maxhaba\nend\nres = '*'*(haba+2)\nlines.each do |x|\n res +=\"\\n\"\n res += '*'\n s = (haba-x.size)/2\n res += ' '*s\n res += x.chomp\n if (haba-x.size)%2 == 0\n res += ' '*s\n else\n res += ' '*(s+1)\n end\n res +='*'\nend\nres += \"\\n\"\nres += '*'*(haba+2)\nputs res\n"}, {"source_code": "lines = []\nwhile line = gets do\n lines << line.chomp\nend\nhaba = 0\nlines.each do |x|\n haba = x.size if x.size>haba\nend\nres = '*'*(haba+2)\nlines.each do |x|\n res +=\"\\n\"\n res += '*'\n s = (haba-x.size)/2\n if (haba-x.size)%2 == 0\n res += ' '*s\n else\n res += ' '*(s+1)\n end\n res += x.chomp\n res += ' '*s\n res +='*'\nend\nres += \"\\n\"\nres += '*'*(haba+2)\nprint res\n"}, {"source_code": "lines = []\nwhile line = gets do\n lines << line.chomp\nend\nhaba = 0\nlines.each do |x|\n haba = x.size if x.size>haba\nend\nres = '*'*(haba+2)\nlines.each do |x|\n res +=\"\\n\"\n res += '*'\n s = (haba-x.size)/2\n if (haba-x.size)%2 == 0\n res += ' '*s\n else\n res += ' '*(s+1)\n end\n res += x.chomp\n res += ' '*s\n res +='*'\nend\nres += \"\\n\"\nres += '*'*(haba+2)\nprint res"}, {"source_code": "lines = []\nwhile line = gets do\n lines << line.chomp\nend\nhaba = 0\nlines.each do |x|\n haba = x.size if x.size>haba\nend\nres = '*'*(haba+2)\nlines.each do |x|\n res +=\"\\n\"\n res += '*'\n s = (haba-x.size)/2\n if (haba-x.size)%2 == 0\n res += ' '*s\n else\n res += ' '*(s+1)\n end\n res += x.chomp\n res += ' '*s\n res +='*'\nend\nres += \"\\n\"\nres += '*'*(haba+2)\nprint res\n"}, {"source_code": "lines = []\nwhile line = gets do\n lines << line\nend\nhaba = 0\nlines.each do |x|\n haba = x.size if x.size>haba\nend\nres = '*'*(haba+1)\nlines.each do |x|\n res +=\"\\n\"\n res += '*'\n s = (haba-x.size)/2\n res += ' '*s\n res += x.chomp\n if (haba-x.size)%2 == 0\n res += ' '*s\n else\n res += ' '*(s+1)\n end\n res +='*'\nend\nres += \"\\n\"\nres += '*'*(haba+1)\nprint res\n"}, {"source_code": "lines = []\nwhile line = gets do\n lines << line.chomp\nend\nhaba = 0\nlines.each do |x|\n haba = x.size if x.size>haba\nend\nres = '*'*(haba+2)\nres += \"\\r\\n\"\nlines.each do |x|\n res += '*'\n s = (haba-x.size)/2\n res += ' '*s\n res += x.chomp\n res += ' '*(haba-x.size-s)\n res += \"*\\r\\n\"\nend\nres += '*'*(haba+2)\nprint res+\"\\r\\n\"\n"}, {"source_code": "lines = []\nwhile line = gets do\n lines << line.chomp\nend\nhaba = 0\nlines.each do |x|\n haba = x.size if x.size>haba\nend\nres = '*'*(haba+2)\nlines.each do |x|\n res +=\"\\n\"\n res += '*'\n s = (haba-x.size)/2\n res += ' '*s\n res += x.chomp\n if (haba-x.size)%2 == 0\n res += ' '*s\n else\n res += ' '*(s+1)\n end\n res +='*'\nend\nres += \"\\n\"\nres += '*'*(haba+2)\nprint res\n"}, {"source_code": "lines = []\nwhile line = gets do\n lines << line.chomp\nend\nhaba = 0\nlines.each do |x|\n haba = x.size if x.size>haba\nend\nres = '*'*(haba+2)\nlines.each do |x|\n res +=\"\\n\"\n res += '*'\n s = (haba-x.size)/2\n if (haba-x.size)%2 == 0\n res += ' '*s\n else\n res += ' '*(s+1)\n end\n res += x.chomp\n res += ' '*s\n res +='*'\nend\nres += \"\\n\"\nres += '*'*(haba+2)\nprint res\n"}, {"source_code": "lines = []\nwhile line = gets do\n lines << line.chomp\nend\nhaba = 0\nlines.each do |x|\n haba = x.size if x.size>haba\nend\nres = '*'*(haba+2)\nlines.each do |x|\n res +=\"\\n\"\n res += '*'\n s = (haba-x.size)/2\n if (haba-x.size)%2 == 0\n res += ' '*s\n else\n res += ' '*(s+1)\n end\n res += x.chomp\n res += ' '*s\n res +='*'\nend\nres += \"\\n\"\nres += '*'*(haba+2)\nputs res\n"}, {"source_code": "lines = []\nwhile line = gets do\n lines << line.chomp\nend\nhaba = 0\nlines.each do |x|\n haba = x.size if x.size>haba\nend\nres = '*'*(haba+2)\nlines.each do |x|\n res +=\"\\n\"\n res += '*'\n s = (haba-x.size)/2\n if (haba-x.size)%2 == 0\n res += ' '*s\n else\n res += ' '*(s+1)\n end\n res += x.chomp\n res += ' '*s\n res +='*'\nend\nres += \"\\n\"\nres += '*'*(haba+2)\nprint res\n"}, {"source_code": "lines = []\nwhile line = gets do\n lines << line\nend\nhaba = 0\nlines.each do |x|\n haba = x.size if x.size>haba\nend\nres = '*'*(haba+1)\nlines.each do |x|\n res +=\"\\n\"\n res += '*'\n s = (haba-x.size)/2\n res += ' '*s\n res += x.chomp\n if (haba-x.size)%2 == 0\n res += ' '*s\n else\n res += ' '*(s+1)\n end\n res +='*'\nend\nres += \"\\n\"\nres += '*'*(haba+1)\nputs res\n"}, {"source_code": "lines = []\nwhile line = gets do\n lines << line.chomp\nend\nhaba = 0\nlines.each do |x|\n haba = x.size if x.size>haba\nend\nres = '*'*(haba+2)\nlines.each do |x|\n res +=\"\\r\\n\"\n res += '*'\n s = (haba-x.size)/2\n if (haba-x.size)%2 == 0\n res += ' '*s\n else\n res += ' '*(s+1)\n end\n res += x.chomp\n res += ' '*s\n res +='*'\nend\nres += \"\\r\\n\"\nres += '*'*(haba+2)\nprint res+\"\\r\\n\"\n"}, {"source_code": "input, max = STDIN.read.split(\"\\n\"), 0\ninput.map! { |i| l = i.length; max = l if l > max; [i, l] }\n\nputs '*' * (max + 2)\n\nleft = false\n\ninput.each do |i|\n\tprint '*'\n\tm = (max - i[1])\n\ts = m / 2\n\tif (m % 2).even?\n\t\tprint ' ' * s, i[0], ' ' * s\n\telse\n\t\tl, r = s, s\n\t\tleft ? l += 1 : r += 1\n\t\tprint ' ' * l, i[0], ' ' * r\n\t\tleft = !left\n\tend\n\tputs '*'\nend\n\nputs '*' * (max + 2)\n"}], "src_uid": "a017393743ae70a4d8a9d9dc40410653"} {"nl": {"description": "Caisa is going to have a party and he needs to buy the ingredients for a big chocolate cake. For that he is going to the biggest supermarket in town.Unfortunately, he has just s dollars for sugar. But that's not a reason to be sad, because there are n types of sugar in the supermarket, maybe he able to buy one. But that's not all. The supermarket has very unusual exchange politics: instead of cents the sellers give sweets to a buyer as a change. Of course, the number of given sweets always doesn't exceed 99, because each seller maximizes the number of dollars in the change (100 cents can be replaced with a dollar).Caisa wants to buy only one type of sugar, also he wants to maximize the number of sweets in the change. What is the maximum number of sweets he can get? Note, that Caisa doesn't want to minimize the cost of the sugar, he only wants to get maximum number of sweets as change. ", "input_spec": "The first line contains two space-separated integers n, s (1 ≤ n, s ≤ 100). The i-th of the next n lines contains two integers xi, yi (1 ≤ xi ≤ 100; 0 ≤ yi < 100), where xi represents the number of dollars and yi the number of cents needed in order to buy the i-th type of sugar.", "output_spec": "Print a single integer representing the maximum number of sweets he can buy, or -1 if he can't buy any type of sugar.", "sample_inputs": ["5 10\n3 90\n12 0\n9 70\n5 50\n7 0", "5 5\n10 10\n20 20\n30 30\n40 40\n50 50"], "sample_outputs": ["50", "-1"], "notes": "NoteIn the first test sample Caisa can buy the fourth type of sugar, in such a case he will take 50 sweets as a change."}, "positive_code": [{"source_code": "n, s = gets.chomp.split(\" \").map { |e| e.to_i }\narr = []\n\nn.times {\n arr << gets.chomp.split(\" \").map { |e| e.to_i }\n}\n\narr.select! { |x, y| (x.to_f + y.to_f / 100.0) <= s.to_f }\narr.map! { |x, y| \n if y == 0\n [x, y] \n else\n [x, 100 - y]\n end\n}\n\nif arr.empty?\n puts \"-1\"\nelse\n arr.sort_by! { |x, y| -y }\n puts arr.first.last\nend\n\n"}, {"source_code": "#!/usr/bin/ruby2.0\n\nn, s = STDIN.readline.split.collect {|s| s.to_i }\ns *= 100\nbest = -1\nn.times do\n\td, c = STDIN.readline.split.collect {|s| s.to_i }\n\tchange = s - (d*100 + c)\n\tbest = [best, change%100].max if change >= 0\nend\nputs best\n\n"}, {"source_code": "\n(n, s) = gets.chomp.split.map(&:to_i);\nmax = -1\nn.times do |x|\n (x, y) = gets.chomp.split.map(&:to_i);\n if (s - x) * 100 >= y \n max = [max, (100 - y) % 100].max\n end\nend\nputs max;\n"}, {"source_code": "n, s = gets.split.map(&:to_i)\nmax = -1\nn.times do\n x, y = gets.split.map(&:to_i)\n if (s - x) * 100 >= y \n max = [max, (100 - y) % 100].max\n end\nend\nputs max\n"}, {"source_code": "x = gets.split.map(&:to_i)\n\na = []\nfor i in 0...x[0]\n b = gets.split.map(&:to_i)\n a << 100 - b[1] if x[1] > b[0] and b[1] > 0\n a << 0 if x[1] >= b[0] and b[1] == 0\nend\n\nputs a.size > 0 ? a.max : -1"}, {"source_code": "class Pair\n attr_accessor :first, :second\n\n def initialize(f, s)\n @first = f\n @second = s\n end\n\n def <=(y)\n return @first < y.first || (@first == y.first && @second <= y.second)\n end\n\n def -(y)\n if y.second == 0\n 0\n else\n 100 - y.second\n end\n end\nend\n\nn, s = gets.split.map{|c| c.to_i}\n\na = Pair.new(s, 0)\n\narr = []\n\nn.times{ arr.push(Pair.new(*gets.split.map{|c| c.to_i})) }\n\nmx = -1\n\narr.each do |p|\n mx = [mx, a - p].max if p <= a\nend\n\nputs mx\n"}, {"source_code": "n, s = gets.chomp.split(' ').map(&:to_i)\n\ns *= 100\nres = -1\nn.times do\n x, y = gets.chomp.split(' ').map(&:to_i)\n cost = 100*x + y\n #(1..(s/cost).to_i).each do |kg|\n total = cost # * kg\n if s >= total \n ost = (100-(total % 100)) % 100\n res = [res, ost].max\n end\n #end\nend\n\nputs res"}, {"source_code": "#!/usr/bin/env ruby\n# coding: utf-8\n\n# $stdin = File.new('rf','r')\n\nn, s = readline.chomp.split(' ').map(&:to_i)\nx, y = [], []\nn.times { |i| x[i], y[i] = *readline.chomp.split(' ').map(&:to_i) }\n\ncandies = -1\nx.zip(y).each do |dollars, cents|\n next if dollars + cents.fdiv(100) > s\n candies = [candies, -cents % 100].max\nend\nputs candies"}, {"source_code": "n, s = *gets.chomp.split(' ').map(&:to_i)\n\ns *= 100\n\na = Array.new(n) do\n x, y = gets.chomp.split(' ').map(&:to_i)\n x*100 + y\nend\n\ndef solve(a, s)\n a = a.select{|p| p <= s}\n return -1 if a.empty?\n a.map{|x| (s - x)%100 }.max\nend\n\nputs solve(a, s)\n"}, {"source_code": "n,s = gets.split.map(&:to_i)\nmax = -1\nn.times { |_|\n\td,c = gets.split.map(&:to_i)\n\tif s * 100 >= d * 100 + c\n\t\tmax = [max, (100 - c) % 100].max\n\tend\n}\nputs max"}, {"source_code": "n, s = gets.split.map(&:to_i)\nans = -1\nn.times do\n x, y = gets.split.map(&:to_i)\n next if s < x || (s == x && y > 0)\n c = (100 - y) % 100\n ans = [ans, c].max\nend\nputs ans\n"}, {"source_code": "n, s = gets.split.map(&:to_i)\nsweets = 0\ncan_buy = false\nn.times do\n x, y = gets.split.map(&:to_i)\n x += 1 if y > 0\n sweets = 100 - y if s >= x && 100 - y > sweets && y != 0\n can_buy = true if s >= x\nend\nputs can_buy ? sweets : -1\n"}, {"source_code": "def read_next_line\n gets.chomp.split.map(&:to_i)\nend\n\nn, s = read_next_line\nputs (0...n).map { |_| read_next_line }.select { |a| a[0] < s || a[0] == s && a[1] == 0 }.map { |a| (100 - a[1]) % 100 }.max || -1"}, {"source_code": "line = gets\ni, s = line.split.map(&:to_i)\nmin_nu = 10 ** 9\nzero_found = false\ni.times do\n\tline = gets.split\n\tlinetmp = \"#{line[0]}.#{line[1]}\".to_f\n\tnext if linetmp > s\n\tnum = line[1].to_i\n\tmin_nu = num if num < min_nu && num != 0\n\tzero_found = true if num == 0\nend\nif min_nu == 10 ** 9 and zero_found\n\tputs \"0\"\nelsif min_nu == 10 ** 9\n\tputs \"-1\"\nelse\n\tputs 100 - min_nu\nend\n"}], "negative_code": [{"source_code": "n, s = gets.chomp.split(\" \").map { |e| e.to_i }\narr = []\n\nn.times {\n arr << gets.chomp.split(\" \").map { |e| e.to_i }\n}\n\narr.select! { |x, y| (x.to_f + y.to_f / 100.0) <= s.to_f }\narr.select! { |x, y| y != 0 }\n\nif arr.empty?\n puts \"-1\"\nelse\n arr.sort_by! { |x, y| y }\n puts 100 - arr.first.last\nend\n"}, {"source_code": "(n, s) = gets.chomp.split.map(&:to_i);\nmax = -1\n(1..n).each do |x|\n (x, y) = gets.chomp.split.map(&:to_i);\n if (x > s || (x == s && y != 0) || y == 0) then\n next;\n end\n max = [max, 100 - y].max;\nend\nputs max;\n"}, {"source_code": "x = gets.split.map(&:to_i)\n\na = []\nfor i in 0...x[0]\n b = gets.split.map(&:to_i)\n a << 100 - b[1] if x[1] > b[0]\nend\n\nputs a.size > 0 ? a.max : -1"}, {"source_code": "x = gets.split.map(&:to_i)\n\na = [0]\nfor i in 0...x[0]\n b = gets.split.map(&:to_i)\n a << ((x[1] / (b[0] + 1)) * (100 - b[1])) if b[1] > 0\nend\n\nputs a.max > 0 ? a.max : -1"}, {"source_code": "x = gets.split.map(&:to_i)\n\na = [0]\nfor i in 0...x[0]\n b = gets.split.map(&:to_i)\n a << ((x[1] / (b[0] + 1)) * (100 - b[1])) if b[1] > 0\nend\n\nputs a.max"}, {"source_code": "x = gets.split.map(&:to_i)\n\na = []\nfor i in 0...x[0]\n b = gets.split.map(&:to_i)\n a << 100 - b[1] if x[1] > b[0] and b[1] > 0\nend\n\nputs a.size > 0 ? a.max : -1"}, {"source_code": "n, s = gets.chomp.split(' ').map(&:to_i)\n\nres = -1\nn.times do\n x, y = gets.chomp.split(' ').map(&:to_i)\n cost = x + 0.01 * y\n (1..(s/cost).to_i).each do |kg|\n total = cost * kg\n if s >= total && y > 0\n res = [res, 100-y].max\n end\n end\nend\n\nputs res"}, {"source_code": "n, s = gets.chomp.split(' ').map(&:to_i)\n\nres = -1\nn.times do\n x, y = gets.chomp.split(' ').map(&:to_i)\n cost = x + 0.01 * y\n (1..(s/cost).to_i).each do |kg|\n total = cost * kg\n total_y = (y * kg) % 100\n if s >= total && total_y > 0\n res = [res, 100-total_y].max\n end\n end\nend\n\nputs res"}, {"source_code": "n, s = gets.chomp.split(' ').map(&:to_i)\n\ns *= 100\nres = -1\nn.times do\n x, y = gets.chomp.split(' ').map(&:to_i)\n cost = 100*x + y\n (1..(s/cost).to_i).each do |kg|\n total = cost * kg\n if s >= total\n ost = total % 100 > 0 ? 100-(total % 100) : 0\n res = [res, ost].max\n end\n end\nend\n\nputs res"}, {"source_code": "n, s = gets.chomp.split(' ').map(&:to_i)\n\ns *= 100\nres = -1\nn.times do\n x, y = gets.chomp.split(' ').map(&:to_i)\n cost = 100*x + y\n (1..(s/cost).to_i).each do |kg|\n total = cost * kg\n if s >= total && total % 100 > 0\n res = [res, 100-(total % 100)].max\n end\n end\nend\n\nputs res"}, {"source_code": "n, s = gets.chomp.split(' ').map(&:to_i)\n\nres = -1\nn.times do\n x, y = gets.chomp.split(' ').map(&:to_i)\n cost = x + 0.01 * y\n if s >= cost && y > 0\n res = [res, 100-y].max\n end\nend\n\nputs res"}, {"source_code": "n, s = gets.chomp.split(' ').map(&:to_i)\n\ns *= 100\nres = -1\nn.times do\n x, y = gets.chomp.split(' ').map(&:to_i)\n cost = 100*x + y\n (1..(s/cost).to_i).each do |kg|\n total = cost * kg\n if s >= total\n #ost = total % 100 > 0 ? 100-(total % 100) : 0\n ost = (100-(total % 100)) % 100\n res = [res, ost].max\n end\n end\nend\n\nputs res"}, {"source_code": "n, s = gets.split.map(&:to_i)\nsweets = 0\ncan_buy = false\nn.times do\n x, y = gets.split.map(&:to_i)\n sweets = 100 - y if s >= x && 100 - y > sweets && y != 0\n can_buy = true if s >= x\nend\nputs can_buy ? sweets : -1\n"}, {"source_code": "n, s = gets.split.map(&:to_i)\nsweets = -1\nn.times do\n x, y = gets.split.map(&:to_i)\n next if s < x || (s == x && y > 0)\n sweets = 100 - y if 100 - y > sweets && y != 0\nend\nputs sweets\n"}, {"source_code": "n, s = gets.split.map(&:to_i)\nsweets = 0\nn.times do\n x, y = gets.split.map(&:to_i)\n sweets = 100 - y if x < s && 100 - y > sweets && y != 0\nend\nputs sweets == 0 ? -1 : sweets\n"}, {"source_code": "n, s = gets.split.map(&:to_i)\nsweets = 0\ncan_buy = false\nn.times do\n x, y = gets.split.map(&:to_i)\n sweets = 100 - y if s > x && 100 - y > sweets && y != 0\n can_buy = true if s > x\nend\nputs can_buy ? sweets : -1\n"}, {"source_code": "line = gets\ni, s = line.split.map(&:to_i)\nmin_nu = 10 ** 9\nzero_found = false\ni.times do\n\tline = gets.split\n\tlinetmp = \"#{line[0]}.#{line[1]}\".to_f\n\tnext if linetmp > s\n\tnum = line[1].to_i\n\tmin_nu = num if num < min_nu && num != 0\n\tzero_found = true if num == 0\nend\nif min_nu == 10 ** 9 and zero_found\n\tputs \"0\"\nelsif min_nu == 10 ** 9\n\tputs \"-1\"\nelse\n\tputs min_nu\nend\n"}, {"source_code": "line = gets\ni, s = line.split.map(&:to_i)\nmin_nu = -1\ni.times do\n\tline = gets.split\n\tline = \"#{line[0]}.#{line[1]}\".to_f\n\tnext if line > s\n\tnum = s - line\n\tnum = (\"0.\" + num.to_s.split(\".\")[1]).to_f\n\t# puts num\n\tmin_nu = num if num > min_nu && num >= 0\nend\nif min_nu != -1\n\tmin_nu = min_nu.to_s.gsub!(/0\\./, \"\")\n\tmin_nu += \"0\" if min_nu.size == 1 && min_nu != \"0\"\nend\nputs min_nu\n"}, {"source_code": "line = gets\ni, s = line.split.map(&:to_i)\nmin_nu = -1\ni.times do\n\tline = gets.split\n\tline = \"#{line[0]}.#{line[1]}\".to_f\n\tnext if line > s\n\tnum = s - line\n\tnum = (\"0.\" + num.to_s.split(\".\")[1]).to_f\n\t# puts num\n\tmin_nu = num if num > min_nu && num > 0\nend\nif min_nu != -1\n\tmin_nu = min_nu.to_s.gsub!(/0\\./, \"\")\n\tmin_nu += \"0\" if min_nu.size == 1\nend\nputs min_nu\n"}], "src_uid": "91be5db48b44a44adff4c809ffbb8e3e"} {"nl": {"description": "First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other. For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the \"root\" of the word — some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction — it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word \"suffix\" to describe a morpheme but not the few last characters of the string as you may used to). Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language. Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match. Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: , where the root of the word is overlined, and suffixes are marked by \"corners\". Thus, the set of possible suffixes for this word is {aca, ba, ca}. ", "input_spec": "The only line contains a string s (5 ≤ |s| ≤ 104) consisting of lowercase English letters.", "output_spec": "On the first line print integer k — a number of distinct possible suffixes. On the next k lines print suffixes. Print suffixes in lexicographical (alphabetical) order. ", "sample_inputs": ["abacabaca", "abaca"], "sample_outputs": ["3\naca\nba\nca", "0"], "notes": "NoteThe first test was analysed in the problem statement. In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix."}, "positive_code": [{"source_code": "require 'set'\n\nword = gets.chomp[5..-1].reverse\n\nsuffixes = Set.new\nposs2 = Array.new(word.length, false)\nposs3 = Array.new(word.length, false)\n\nposs2[0], poss3[0] = true, true\n\n\n(word.length).times { |x|\n if (poss3[x-3]) or (poss2[x-2] and (word[x-2..x-1] != word[x..x+1]))\n if word[x+1] != nil\n #suffixes.add (word[x+1]||''+word[x]||'')\n poss2[x] = true\n end\n end\n\n if (poss2[x-2]) or (poss3[x-3] and (word[x-3..x-1] != word[x..x+2]))\n if word[x+2] != nil\n #suffixes.add(word[x+2]||''+word[x+1]||''+word[x]||'')\n poss3[x] = true\n end\n end\n}\n\n(word.length-1).times { |x| suffixes.add word[x..x+1].reverse if poss2[x] }\n(word.length-2).times { |x| suffixes.add word[x..x+2].reverse if poss3[x] }\n\n\n\nsuffixes = suffixes.to_a.sort\n\n#puts word\n#poss2.each {|e| print e; print ' '}; puts\n#poss3.each {|e| print e; print ' '}; puts\n\nputs suffixes.length\nputs suffixes if suffixes.length>0\n\n"}], "negative_code": [{"source_code": "require 'set'\n\nword = gets.chomp[5..-1].reverse\n\nsuffixes = Set.new\nposs2 = Array.new(word.length, false)\nposs3 = Array.new(word.length, false)\n\nposs2[0], poss3[0] = true, true\n\n\n(word.length).times { |x|\n if (poss3[x-3]) or (poss2[x-2] and (word[x-2]||''+word[x-1]||'' != word[x]||''+word[x+1]||''))\n if word[x+1] != nil\n #suffixes.add (word[x+1]||''+word[x]||'')\n poss2[x] = true\n end\n end\n\n if (poss2[x-2]) or (poss3[x-3] and (word[x-3]||''+word[x-2]||''+word[x-1]||'' != word[x]||''+word[x+1]||''+word[x+2]||''))\n if word[x+2] != nil\n #suffixes.add(word[x+2]||''+word[x+1]||''+word[x]||'')\n poss3[x] = true\n end\n end\n}\n\n(word.length-1).times { |x| suffixes.add word[x..x+1].reverse if poss2[x] }\n(word.length-2).times { |x| suffixes.add word[x..x+2].reverse if poss3[x] }\n\n\n\nsuffixes = suffixes.to_a.sort\n\n#puts word\n#poss2.each {|e| print e; print ' '}; puts\n#poss3.each {|e| print e; print ' '}; puts\n\nputs suffixes.length\nputs suffixes\n\n"}], "src_uid": "dd7ccfee8c2a19bf47d65d5a62ac0071"} {"nl": {"description": "Polycarp wrote on the board a string $$$s$$$ containing only lowercase Latin letters ('a'-'z'). This string is known for you and given in the input.After that, he erased some letters from the string $$$s$$$, and he rewrote the remaining letters in any order. As a result, he got some new string $$$t$$$. You have to find it with some additional information.Suppose that the string $$$t$$$ has length $$$m$$$ and the characters are numbered from left to right from $$$1$$$ to $$$m$$$. You are given a sequence of $$$m$$$ integers: $$$b_1, b_2, \\ldots, b_m$$$, where $$$b_i$$$ is the sum of the distances $$$|i-j|$$$ from the index $$$i$$$ to all such indices $$$j$$$ that $$$t_j > t_i$$$ (consider that 'a'<'b'<...<'z'). In other words, to calculate $$$b_i$$$, Polycarp finds all such indices $$$j$$$ that the index $$$j$$$ contains a letter that is later in the alphabet than $$$t_i$$$ and sums all the values $$$|i-j|$$$.For example, if $$$t$$$ = \"abzb\", then: since $$$t_1$$$='a', all other indices contain letters which are later in the alphabet, that is: $$$b_1=|1-2|+|1-3|+|1-4|=1+2+3=6$$$; since $$$t_2$$$='b', only the index $$$j=3$$$ contains the letter, which is later in the alphabet, that is: $$$b_2=|2-3|=1$$$; since $$$t_3$$$='z', then there are no indexes $$$j$$$ such that $$$t_j>t_i$$$, thus $$$b_3=0$$$; since $$$t_4$$$='b', only the index $$$j=3$$$ contains the letter, which is later in the alphabet, that is: $$$b_4=|4-3|=1$$$. Thus, if $$$t$$$ = \"abzb\", then $$$b=[6,1,0,1]$$$.Given the string $$$s$$$ and the array $$$b$$$, find any possible string $$$t$$$ for which the following two requirements are fulfilled simultaneously: $$$t$$$ is obtained from $$$s$$$ by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string $$$t$$$ according to the rules above, equals to the array $$$b$$$ specified in the input data. ", "input_spec": "The first line contains an integer $$$q$$$ ($$$1 \\le q \\le 100$$$) — the number of test cases in the test. Then $$$q$$$ test cases follow. Each test case consists of three lines: the first line contains string $$$s$$$, which has a length from $$$1$$$ to $$$50$$$ and consists of lowercase English letters; the second line contains positive integer $$$m$$$ ($$$1 \\le m \\le |s|$$$), where $$$|s|$$$ is the length of the string $$$s$$$, and $$$m$$$ is the length of the array $$$b$$$; the third line contains the integers $$$b_1, b_2, \\dots, b_m$$$ ($$$0 \\le b_i \\le 1225$$$). It is guaranteed that in each test case an answer exists.", "output_spec": "Output $$$q$$$ lines: the $$$k$$$-th of them should contain the answer (string $$$t$$$) to the $$$k$$$-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any.", "sample_inputs": ["4\nabac\n3\n2 1 0\nabc\n1\n0\nabba\n3\n1 0 1\necoosdcefr\n10\n38 13 24 14 11 5 3 24 17 0"], "sample_outputs": ["aac\nb\naba\ncodeforces"], "notes": "NoteIn the first test case, such strings $$$t$$$ are suitable: \"aac', \"aab\".In the second test case, such trings $$$t$$$ are suitable: \"a\", \"b\", \"c\".In the third test case, only the string $$$t$$$ equals to \"aba\" is suitable, but the character 'b' can be from the second or third position."}, "positive_code": [{"source_code": "gets.to_i.times do\n\ts = gets.chomp\n\tm = gets.to_i\n\ta = gets.split.map(&:to_i)\n\tcnt = s.chars.tally.to_a.sort.reverse\n\tans = '?' * m\n\n\twhile a.count(-1) < m\n\t\tzero = (0...m).select { a[_1] == 0 }\n\t\tcnt.shift while cnt[0][1] < zero.size\n\t\tzero.each do |i|\n\t\t\tans[i] = cnt[0][0]\n\t\t\t(0...m).each { |j| a[j] -= (i - j).abs if a[j] > 0 }\n\t\t\ta[i] = -1\n\t\tend\n\t\tcnt.shift\n\tend\n\n\tputs ans\nend\n"}], "negative_code": [], "src_uid": "bc2f2b98c50a0165b7204a6d595eec4b"} {"nl": {"description": "Ichihime is the current priestess of the Mahjong Soul Temple. She claims to be human, despite her cat ears.These days the temple is holding a math contest. Usually, Ichihime lacks interest in these things, but this time the prize for the winner is her favorite — cookies. Ichihime decides to attend the contest. Now she is solving the following problem. You are given four positive integers $$$a$$$, $$$b$$$, $$$c$$$, $$$d$$$, such that $$$a \\leq b \\leq c \\leq d$$$. Your task is to find three integers $$$x$$$, $$$y$$$, $$$z$$$, satisfying the following conditions: $$$a \\leq x \\leq b$$$. $$$b \\leq y \\leq c$$$. $$$c \\leq z \\leq d$$$. There exists a triangle with a positive non-zero area and the lengths of its three sides are $$$x$$$, $$$y$$$, and $$$z$$$.Ichihime desires to get the cookie, but the problem seems too hard for her. Can you help her?", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 1000$$$)  — the number of test cases. The next $$$t$$$ lines describe test cases. Each test case is given as four space-separated integers $$$a$$$, $$$b$$$, $$$c$$$, $$$d$$$ ($$$1 \\leq a \\leq b \\leq c \\leq d \\leq 10^9$$$).", "output_spec": "For each test case, print three integers $$$x$$$, $$$y$$$, $$$z$$$  — the integers you found satisfying the conditions given in the statement. It is guaranteed that the answer always exists. If there are multiple answers, print any.", "sample_inputs": ["4\n1 3 5 7\n1 5 5 7\n100000 200000 300000 400000\n1 1 977539810 977539810"], "sample_outputs": ["3 4 5\n5 5 5\n182690 214748 300999\n1 977539810 977539810"], "notes": "NoteOne of the possible solutions to the first test case:One of the possible solutions to the second test case:"}, "positive_code": [{"source_code": "def valid?(a,b,c)\n # puts \"#{a} #{b} #{c} -> #{((a+b)>c) && ((a+c)>b) && ((b+c)>a)}\"\n ((a+b)>c) && ((a+c)>b) && ((b+c)>a)\nend\n\nt = gets.chomp.to_i\nt.times do\n a, b, c, d = gets.chomp.split.map(&:to_i)\n\n if(a>b)\n x=a\n else\n x=b\n end\n\n if(b>c)\n y=b\n else\n y=c\n end\n\n if(ca[2])\n puts \"#{a[1]} #{a[1]} #{a[2]}\"\n else\n puts \"#{a[2]} #{a[2]} #{a[3]}\"\n end\n\nend\n"}], "src_uid": "821d48c9a67d37ad7acc50d4d0d0d723"} {"nl": {"description": "A boy Bob likes to draw. Not long ago he bought a rectangular graph (checked) sheet with n rows and m columns. Bob shaded some of the squares on the sheet. Having seen his masterpiece, he decided to share it with his elder brother, who lives in Flatland. Now Bob has to send his picture by post, but because of the world economic crisis and high oil prices, he wants to send his creation, but to spend as little money as possible. For each sent square of paper (no matter whether it is shaded or not) Bob has to pay 3.14 burles. Please, help Bob cut out of his masterpiece a rectangle of the minimum cost, that will contain all the shaded squares. The rectangle's sides should be parallel to the sheet's sides.", "input_spec": "The first line of the input data contains numbers n and m (1 ≤ n, m ≤ 50), n — amount of lines, and m — amount of columns on Bob's sheet. The following n lines contain m characters each. Character «.» stands for a non-shaded square on the sheet, and «*» — for a shaded square. It is guaranteed that Bob has shaded at least one square.", "output_spec": "Output the required rectangle of the minimum cost. Study the output data in the sample tests to understand the output format better.", "sample_inputs": ["6 7\n.......\n..***..\n..*....\n..***..\n..*....\n..***..", "3 3\n***\n*.*\n***"], "sample_outputs": ["***\n*..\n***\n*..\n***", "***\n*.*\n***"], "notes": null}, "positive_code": [{"source_code": "st = gets.split\nn = st[0].to_i\nm = st[1].to_i\n\nstr = []\nmaxn = -1\nminn = 51\nmaxm = -1\nminm = 51\nn.times do |i|\n str << gets.chomp\n if k = str[i].index(\"*\")\n q = str[i].rindex(\"*\")\n if q > maxm then\n maxm = q\n end\n if k < minm then\n minm = k\n end\n if i < minn then\n minn = i\n end\n if i > maxn then\n maxn = i\n end\n end\nend\n\nresult = []\nn.times do |i|\n if i >= minn && i <= maxn then\n a = str[i][minm..maxm]\n result << a\n end\nend\n\nresult.size.times do |i|\n puts result[i]\nend"}, {"source_code": "ns,ms = gets.chomp.split(/ /)\nn = ns.to_i\nm = ms.to_i\nar = Array.new(n)\nfor i in 0..n-1\n\tar[i] = gets.chomp\nend\n\nx1 = m-1\nx2 = 0\ny1 = -1\ny2 = -1\nfor i in 0..n-1\n\twx1 = ar[i].index(\"*\") \n\tx1 = [x1, wx1].min if wx1 != nil\n\twx2 = ar[i].rindex(\"*\") \n\tx2 = [x2, wx2].max if wx2 != nil\n\tif wx1 != nil && y1 == -1\n\t\ty1 = i\n\tend\n\ty2 = i if wx1 != nil \nend\nfor i in y1..y2\n\tputs ar[i].slice(x1..x2)\nend"}, {"source_code": "input=gets.split.map(&:to_i)\nmap=[]\nfor i in 0..input[0]-1\n map.push(gets.split[0])\nend\nsx=0\nstr=\"\"\nfor i in 0..input[1]-1\n str+=\".\"\nend\nfor i in 0..input[0]-1\n if map[i]==str then\n sx=i+1\n else\n break\n end\nend\nex=input[0]-1\n(input[0]-1).downto(0){|i|\n if map[i]==str then\n ex=i-1\n else\n break\n end\n}\n\nsy=0\ney=input[1]-1\nfor i in 0..input[1]-1\n ok=1\n for j in 0..input[0]-1\n if map[j][i]==\"*\" then\n ok=false\n break\n end\n end\n if ok then\n sy=i+1\n else\n break\n end\nend\n\n(input[1]-1).downto(0){|i|\n ok=1\n for j in 0..input[0]-1\n if map[j][i]==\"*\" then\n ok=false\n break\n end\n end\n if ok then\n ey=i-1\n else\n break\n end\n}\n\n\n\n\nsx.upto(ex){|i|\n puts map[i][sy..ey]\n}\n"}, {"source_code": "n,m=gets.chomp!.split(\" \").map(&:to_i)\n\ng,f,l,b=[],[0,m-1],[0,0],false\n\nn.times{|i|\n\n g<l[1] then l[1]=c end\n\n end\n\n}\n\ng[f[0]..l[0]].each{|s|puts s[f[1]..l[1]]}\n\n"}, {"source_code": "height,width = gets.split.map(&:to_i)\ndata = Array.new\nrange = Array.new(4,0)\nrange[0] = height\nrange[1] = width\nheight.times do |h|\n data[h] = gets.chomp\n width.times do |w|\n if(data[h][w,1] == \"*\") then\n if h < range[0] then range[0] = h end\n if h > range[2] then range[2] = h end\n if w < range[1] then range[1] = w end\n if w > range[3] then range[3] = w end\n end\n end\nend\n#p range\nfor h in range[0]..range[2] do\n puts data[h][range[1]..range[3]]\nend\n\n"}, {"source_code": "n, m = gets.split.map &:to_i\na = gets(p).split(\"\\n\")\n\nt, d = -1, -1\nl, r = m + 1, -1\na.size.times do |i| \n pos = a[i] =~ /\\*/\n pos = m if pos == nil\n \n l = [l, pos].min\n \n if pos < m\n t = i if t == -1\n d = i\n end\n \n rpos = a[i] =~ /\\.+\\z/\n rpos = m if rpos == nil\n r = [r, rpos].max\nend\n\na = a[t..d].map { |s| s[l...r] }\nputs a * \"\\n\""}, {"source_code": "s = gets.split(\" \").map { |e| e.to_i }\nstr = Array.new(0)\nfor i in 0..s[0]-1 do \n str.push(gets)\nend\nxcord = Array.new(0)\nycord = Array.new(0)\nfor i in 0..s[0]-1 do \n k = str[i].count(\"*\")\n if k > 0 then\n xcord.push(i)\n end\n for j in 0..s[1]-1 do \n if str[i][j] == '*' then\n ycord.push(j)\n end\n end\nend\n\nfor i in xcord.min..xcord.max do \n for j in ycord.min..ycord.max do \n print str[i][j]\n end\n puts \nend\n"}, {"source_code": "n,m=gets.chomp!.split(\" \").map(&:to_i)\ng,f,l,b=[],[0,m-1],[0,0],false\nn.times{|i|\n g<l[1] then l[1]=c end\n end\n}\ng[f[0]..l[0]].each{|s|puts s[f[1]..l[1]]}\n"}, {"source_code": "require 'set'\n\nn, m = gets.chomp!.split.map(&:to_i)\ngrid = []\nindexes = []\nn.times do |index|\n\trow = gets.chomp!\n\tgrid << row\n\tindexes << index if (re = /[*]/) =~ row\nend\n\nwidth = Set.new\nrow_start, row_end = indexes.min, indexes.max\nm.times do |col|\n\t(row_start..row_end).each do |row|\n\t\t(width << col and break) if grid[row][col] == \"*\"\n\tend\nend\n\nstart_index, end_index = width.min, width.max\n(row_start..row_end).each do |row|\n\tputs grid[row][start_index..end_index]\nend"}, {"source_code": "n,m = gets.split.map(&:to_i)\nmat = []\nn.times{mat = mat << gets }\nx,a,y,b= m,n,0,0\nfor i in 0...n\n a = i if mat[i].count('.') != m && i < a ;b = i if mat[i].count('.') != m && i > b\nend\nfor i in 0...m do\n cnt = 0\n for j in 0...n do\n cnt += 1 if mat[j][i,1] == '.'\n end\n x = i if cnt != n && x > i ;y = i if cnt != n && y < i\nend\nfor i in a..b do\n puts mat[i][x..y]\nend"}, {"source_code": "n,m = gets.split.map{|str| str.to_i}\nflats = []\nn.times{\n flats = flats << gets\n}\n\nx1,y1,x2,y2= m,n,0,0\n#puts flats[0]\nfor i in 0...n\n y1 = i if flats[i].count('.') != m && i < y1\n y2 = i if flats[i].count(\".\") != m && i > y2\nend\n\nfor i in 0...m do\n cnt = 0\n for j in 0...n do\n cnt += 1 if flats[j][i,1] == '.'\n end\n x1 = i if cnt != n && x1 > i\n x2 = i if cnt != n && x2 < i\nend\n#puts \"#{y1},#{y2},#{x1},#{x2}\"\nfor i in y1..y2 do\n puts flats[i][x1..x2]\nend\n"}, {"source_code": "n, m = gets.split.collect{|i| i.to_i}\ns = Array.new(n)\nn.times{|i| s[i] = gets.chomp }\nup, low, left, right = 0, n-1, m-1, m-1\n(0...n).each do |i|\n if s[i].include?(\"*\")\n up = i\n break\n end\nend\n(n-1).downto(0).each do |i|\n if s[i].include?(\"*\")\n low = i\n break\n end\nend\n(0...n).each do |i|\n if s[i].index(\"*\") and s[i].index(\"*\")left\n left = s[i].index(\"*\")\n end\nend\n(0...n).each do |i|\n if s[i].index(\"*\") and s[i].reverse.index(\"*\") avg\n\treturn \"YES\"\nend\n\ngets.to_i.times do\n\tputs solve\nend\n"}, {"source_code": "T = gets.to_i\n\nT.times do\n a, b, c, n = gets.split.map(&:to_i)\n\n max = [a, b, c].max\n\n [a, b, c].each do |v|\n n -= max - v\n end\n\n if n >= 0 && n % 3 == 0\n puts 'YES'\n else\n puts 'NO'\n end\nend\n"}, {"source_code": "gets.to_i.times {\n a, b, c, n = gets.split.map { |v| v.to_i }\n total = a + b + c + n\n\n if (a >= b) and (a >= c)\n m = a\n elsif (b >= a) and (b >= c)\n m = b\n else\n m = c\n end\n\n d = 3 * m - a - b - c\n if (total % 3 == 0) and (n >= d)\n puts \"YES\"\n else\n puts \"NO\"\n end \n}\n"}, {"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\n\nsets = gets.to_i\nsets.times {\n a, b, c, n = gets.split.map(&:to_i)\n pick_max = [a, b, c].max\n fillups = (pick_max - a) + (pick_max - b) + (pick_max - c)\n remains = n - fillups\n\n if remains >= 0\n puts \"YES\" if remains % 3 == 0\n puts \"NO\" if remains % 3 != 0\n else\n puts \"NO\"\n end\n}"}, {"source_code": "def getline\n return gets.split.map(&:to_i)\nend\nt=gets.to_i\nt.times do |i|\n a, b, c, n = getline()\n s = [a,b,c].sort!\n a, b, c = s[2], s[1], s[0]\n d = a-b+a-c\n if d <= n and (n-d) % 3 == 0 then\n print \"YES\\n\"\n else\n print \"NO\\n\"\n end\nend"}, {"source_code": "t = gets.to_i\n\n1.upto(t) do\n a, b, c, n = gets.split(' ').map(&:to_i)\n\n eql = [a, b, c].inject(0) { |sum, x| sum + [a, b, c].max - x }\n\n puts (n - eql).modulo(3).zero? && (n >= eql) ? 'YES' : 'NO'\nend\n"}], "negative_code": [{"source_code": "T = gets.to_i\n\nT.times do\n a, b, c, n = gets.split.map(&:to_i)\n\n max = [a, b, c].max\n\n [a, b, c].each do |v|\n n -= max - v\n end\n\n if n % 3 == 0\n puts 'YES'\n else\n puts 'NO'\n end\nend\n"}, {"source_code": "gets.to_i.times {\n a, b, c, n = gets.split.map { |v| v.to_i }\n total = a + b + c + n\n if a > b and a > c\n m = a\n elsif b > a and b > c\n m = b\n else\n m = c\n end\n d = 3 * m - a - b - c\n if total % 3 == 0 and n >= d\n puts \"YES\"\n else\n puts \"NO\"\n end \n}\n"}, {"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\n\nsets = gets.to_i\nsets.times {\n a, b, c, n = gets.split.map(&:to_i)\n pick_max = [a, b, c].max\n fillups = (pick_max - a) + (pick_max - b) + (pick_max - c)\n remains = n - fillups\n\n puts \"YES\" if remains % 3 == 0\n puts \"NO\" if remains % 3 != 0\n}"}, {"source_code": "t = gets.to_i\n\n1.upto(t) do\n a, b, c, n = gets.split(' ').map(&:to_i)\n\n puts (n - [a, b, c].inject(0) { |sum, x| sum + [a, b, c].max - x }).modulo(3).zero? ? 'YES' : 'NO'\nend\n"}], "src_uid": "bc5fb5d6882b86d83e5c86f21d806a52"} {"nl": {"description": "You have integer $$$n$$$. Calculate how many ways are there to fully cover belt-like area of $$$4n-2$$$ triangles with diamond shapes. Diamond shape consists of two triangles. You can move, rotate or flip the shape, but you cannot scale it. $$$2$$$ coverings are different if some $$$2$$$ triangles are covered by the same diamond shape in one of them and by different diamond shapes in the other one.Please look at pictures below for better understanding. On the left you can see the diamond shape you will use, and on the right you can see the area you want to fill. These are the figures of the area you want to fill for $$$n = 1, 2, 3, 4$$$. You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^{4}$$$) — the number of test cases. Each of the next $$$t$$$ lines contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^{9}$$$).", "output_spec": "For each test case, print the number of ways to fully cover belt-like area of $$$4n-2$$$ triangles using diamond shape. It can be shown that under given constraints this number of ways doesn't exceed $$$10^{18}$$$.", "sample_inputs": ["2\n2\n1"], "sample_outputs": ["2\n1"], "notes": "NoteIn the first test case, there are the following $$$2$$$ ways to fill the area: In the second test case, there is a unique way to fill the area: "}, "positive_code": [{"source_code": "t=gets.to_i\nfor x in 1..t\nputs gets\nend\n"}, {"source_code": "1.upto(gets.to_i)do puts gets end"}, {"source_code": "t=gets.to_i\nfor i in 1..t do\n\tn=gets.to_i\n\tputs n\nend"}, {"source_code": "gets.to_i.times {\n puts gets\n}\n"}, {"source_code": "gets.to_i.times do\n puts gets.to_i\nend\n"}, {"source_code": "# coding: utf-8\n\n# 색의 개수가 아니라 1의 크기를 가진 다이아몬드로,\n# 현재 n 길이의 도형(문제의 이미지를 참고)를 메꿀 수 있느냐를 묻는 문제\n# 그 경우의 수! (partitioning, not coloring)\n\nt = gets.to_i\nt.times {\n n = gets.to_i\n puts n\n}"}, {"source_code": "1.upto(gets.chomp.to_i) do |tt|\n n = gets.chomp.to_i\n puts n\nend\n"}, {"source_code": "n = gets.to_i\n\nn.times do\n t = gets.to_i\n puts t\nend\n"}, {"source_code": "t = gets.to_i\nt.times do\n n = gets.to_i\n puts n\nend\n"}, {"source_code": "n = gets.chomp.to_i\n\nfor i in (0...n)\n puts gets.chomp\nend"}, {"source_code": "1.upto(gets.to_i) { puts gets }\n"}], "negative_code": [{"source_code": "t = gets.to_i\nt.times do\n n = gets.to_i\n puts (n >= 2) ? 2 : 1\nend\n"}, {"source_code": "1.upto(gets.to_i) do\n puts gets.to_i > 1 ? 2 : 1\nend\n"}], "src_uid": "740c05c036b646d8fb6b391af115d7f0"} {"nl": {"description": "You are given an array $$$a$$$ of length $$$n$$$, and an integer $$$x$$$. You can perform the following operation as many times as you would like (possibly zero): replace two adjacent elements of the array by their sum. For example, if the initial array was $$$[3, 6, 9]$$$, in a single operation one can replace the last two elements by their sum, yielding an array $$$[3, 15]$$$, or replace the first two elements to get an array $$$[9, 9]$$$. Note that the size of the array decreases after each operation.The beauty of an array $$$b=[b_1, \\ldots, b_k]$$$ is defined as $$$\\sum_{i=1}^k \\left\\lceil \\frac{b_i}{x} \\right\\rceil$$$, which means that we divide each element by $$$x$$$, round it up to the nearest integer, and sum up the resulting values. For example, if $$$x = 3$$$, and the array is $$$[4, 11, 6]$$$, the beauty of the array is equal to $$$\\left\\lceil \\frac{4}{3} \\right\\rceil + \\left\\lceil \\frac{11}{3} \\right\\rceil + \\left\\lceil \\frac{6}{3} \\right\\rceil = 2 + 4 + 2 = 8$$$.Please determine the minimum and the maximum beauty you can get by performing some operations on the original array.", "input_spec": "The first input line contains a single integer $$$t$$$ — the number of test cases ($$$1 \\le t \\le 1000$$$). The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \\leq n \\leq 10^5$$$, $$$1 \\leq x \\leq 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\leq a_i \\leq 10^9$$$), the elements of the array $$$a$$$. It is guaranteed that the sum of values of $$$n$$$ over all test cases does not exceed $$$10^5$$$.", "output_spec": "For each test case output two integers — the minimal and the maximal possible beauty.", "sample_inputs": ["2\n3 3\n3 6 9\n3 3\n6 4 11"], "sample_outputs": ["6 6\n7 8"], "notes": "NoteIn the first test case the beauty of the array does not change if we perform any operations.In the second example we can leave the array unchanged to attain the maximum beauty, and to get the minimum beauty one can replace two elements $$$4$$$ and $$$11$$$ with their sum, yielding an array $$$[6, 15]$$$, which has its beauty equal to $$$7$$$."}, "positive_code": [{"source_code": "def method(v, x) \r\n v / x + (v % x != 0 ? 1 : 0)\r\nend\r\n\r\nt = gets.to_i\r\nt.times do\r\n n, x = gets.split.map { |x| x.to_i }\r\n a = gets.split.map { |x| x.to_i }\r\n puts \"#{method(a.sum, x)} #{a.map { |v| method(v, x) }.sum}\"\r\nend"}, {"source_code": "def method(v, x) \r\n v / x + (v % x != 0 ? 1 : 0)\r\nend\r\n\r\nt = gets.to_i\r\nt.times do\r\n n, x = gets.split.map {|x| x.to_i }\r\n a = gets.split.map {|x| x.to_i }\r\n puts \"#{method(a.sum, x)} #{a.map { |v| method(v, x) }.sum}\"\r\nend"}, {"source_code": "a = gets.to_i\r\nfor i in 1..a do\r\n c = gets.chop.split(/ /).map(&:to_i)\r\n w = c[0]\r\n h = c[1].to_f\r\n b = gets.chop\r\n c = b.split(/ /).map(&:to_i)\r\n x = c.inject(0){|n, m| n + m}\r\n x = x / h\r\n x = x.ceil\r\n l = 0\r\n for i in 0..w-1 do\r\n q = c[i] / h\r\n q = q.ceil\r\n l += q\r\n end\r\n print x, \" \", l, \"\\n\"\r\nend"}, {"source_code": "#!/usr/bin/env ruby\r\n\r\ngets.to_i.times {\r\n n, x = gets.split.map &:to_i\r\n nums = gets.split.map &:to_i\r\n max = nums.map{|num| (num.to_f / x).ceil }.sum\r\n min = (nums.sum.to_f / x).ceil\r\n puts \"#{min} #{max}\"\r\n}"}, {"source_code": "def read\r\n gets.split.map &:to_i\r\nend\r\ngets.to_i.times do\r\n _, x = read\r\n a = read\r\n print (a.sum + x - 1) / x, \" \", a.map { |it| (it + x - 1) / x }.sum, \"\\n\"\r\nend"}, {"source_code": "t = gets.chomp.to_i\r\nt.times do\r\n n, x = gets.chomp.split(\" \").map(&:to_i)\r\n a = gets.chomp.split(\" \").map(&:to_i)\r\n max = 0\r\n ans = 0\r\n a.each do |i|\r\n max += (i/x.to_f).ceil\r\n ans += i\r\n end\r\n min = (ans/x.to_f).ceil\r\n print \"#{min}\" + \" \" + \"#{max}\" + \"\\n\"\r\nend"}, {"source_code": "t = gets.chomp.to_i\r\nt.times do\r\n n, x = gets.chomp.split(' ').map(&:to_i)\r\n arr = gets.chomp.split(' ').map(&:to_i)\r\n min = 0\r\n max = 0\r\n ind = 0\r\n sum = 0\r\n while ind < arr.size\r\n sum += arr[ind]\r\n max += (1.0 * arr[ind] / x).ceil\r\n ind += 1\r\n end\r\n min += (1.0 * sum / x).ceil\r\n puts [min, max].join ' '\r\nend"}, {"source_code": "t = gets.to_i\r\nans = []\r\nt.times do\r\n n,x = gets.chomp.split(\" \").map(&:to_i)\r\n a = gets.chomp.split(\" \").map(&:to_i)\r\n min = (a.sum + x - 1) / x\r\n max = 0\r\n a.each do |v|\r\n max += (v + x - 1) / x\r\n end\r\n ans << [min,max]\r\nend\r\nans.each do |v|\r\n puts v.join(\" \")\r\nend"}], "negative_code": [], "src_uid": "b36d7f840abe998185a988fe8dd2ec75"} {"nl": {"description": "Vladik and Chloe decided to determine who of them is better at math. Vladik claimed that for any positive integer n he can represent fraction as a sum of three distinct positive fractions in form .Help Vladik with that, i.e for a given n find three distinct positive integers x, y and z such that . Because Chloe can't check Vladik's answer if the numbers are large, he asks you to print numbers not exceeding 109.If there is no such answer, print -1.", "input_spec": "The single line contains single integer n (1 ≤ n ≤ 104).", "output_spec": "If the answer exists, print 3 distinct numbers x, y and z (1 ≤ x, y, z ≤ 109, x ≠ y, x ≠ z, y ≠ z). Otherwise print -1. If there are multiple answers, print any of them.", "sample_inputs": ["3", "7"], "sample_outputs": ["2 7 42", "7 8 56"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\nputs n == 1 ? -1 : [ n , n + 1, n * ( n + 1 ) ].join( ' ' )\n"}, {"source_code": "n=gets.to_i;puts n==1 ? -1 : [n,-~n,n*-~n]*' '"}, {"source_code": "n = gets.to_i\nputs n == 1 ? \"-1\" : \"#{n} #{n + 1} #{n * (n + 1)}\"\n"}, {"source_code": "n = gets.chomp.to_i;\nprint n==1? -1 : [n,(n+1),(n+1)*n].join(\" \")\n"}, {"source_code": "n=gets.to_i\nif n==1\n puts -1\nelse\n puts [n, n+1, n*(n+1)]*' '\nend\n"}, {"source_code": "n=gets.to_i;puts n==1?-1:[n,-~n,n*-~n]*' '"}, {"source_code": "$, = \" \"\nn = gets.to_i\nif n == 1\n puts -1\nelse\n print n, n + 1, n * (n + 1), \"\\n\"\nend"}, {"source_code": "n = gets.strip.to_i\nputs -1 if n == 1\nputs [n, n+1, n*n+n].map(&:to_s).join(' ') if n != 1"}], "negative_code": [{"source_code": "n=gets.to_i;puts n==1 ? -1 : [n,n+1,n*-~n/2]*' '"}], "src_uid": "f60ea0f2caaec16894e84ba87f90c061"} {"nl": {"description": "Recently Vasya decided to improve his pistol shooting skills. Today his coach offered him the following exercise. He placed $$$n$$$ cans in a row on a table. Cans are numbered from left to right from $$$1$$$ to $$$n$$$. Vasya has to knock down each can exactly once to finish the exercise. He is allowed to choose the order in which he will knock the cans down.Vasya knows that the durability of the $$$i$$$-th can is $$$a_i$$$. It means that if Vasya has already knocked $$$x$$$ cans down and is now about to start shooting the $$$i$$$-th one, he will need $$$(a_i \\cdot x + 1)$$$ shots to knock it down. You can assume that if Vasya starts shooting the $$$i$$$-th can, he will be shooting it until he knocks it down.Your task is to choose such an order of shooting so that the number of shots required to knock each of the $$$n$$$ given cans down exactly once is minimum possible.", "input_spec": "The first line of the input contains one integer $$$n$$$ $$$(2 \\le n \\le 1\\,000)$$$ — the number of cans. The second line of the input contains the sequence $$$a_1, a_2, \\dots, a_n$$$ $$$(1 \\le a_i \\le 1\\,000)$$$, where $$$a_i$$$ is the durability of the $$$i$$$-th can.", "output_spec": "In the first line print the minimum number of shots required to knock each of the $$$n$$$ given cans down exactly once. In the second line print the sequence consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ — the order of indices of cans that minimizes the number of shots required. If there are several answers, you can print any of them.", "sample_inputs": ["3\n20 10 20", "4\n10 10 10 10", "6\n5 4 5 4 4 5", "2\n1 4"], "sample_outputs": ["43\n1 3 2", "64\n2 1 4 3", "69\n6 1 3 5 2 4", "3\n2 1"], "notes": "NoteIn the first example Vasya can start shooting from the first can. He knocks it down with the first shot because he haven't knocked any other cans down before. After that he has to shoot the third can. To knock it down he shoots $$$20 \\cdot 1 + 1 = 21$$$ times. After that only second can remains. To knock it down Vasya shoots $$$10 \\cdot 2 + 1 = 21$$$ times. So the total number of shots is $$$1 + 21 + 21 = 43$$$.In the second example the order of shooting does not matter because all cans have the same durability."}, "positive_code": [{"source_code": "n = gets.chomp.to_i\narr = gets.chomp.split(' ').map(&:to_i).zip(1..n).sort.reverse\nres = 0\narr.each_with_index do |el, ind|\n res += el[0] * ind + 1\nend\nputs res\nputs arr.map{|el| el[1]}.join(' ')"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split(\" \").map(&:to_i)\nb = []\nn.times do |i|\n b[i] = [a[i], i]\nend\nb.sort!.reverse!\nans = 0\nn.times do |i|\n a[i] = b[i][1] + 1\n ans += b[i][0] * i + 1\nend\nputs ans\nputs a.join(' ')\n"}, {"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\nn = inp[0]\na = inp\nt = []\na.size.times do |i|\n t[i] = [a[i],i]\nend\nt.sort!.reverse!\nans = 0\nanst = []\nn.times do |i|\n ans += 1\n ans += t[i][0]*i\n anst.push(t[i][1]+1)\nend\np ans\nputs anst.join(\" \")\n"}, {"source_code": "MOD=10**9+7\ncnt=0; sum=0; prev=nil; can=true; h=Hash.new(0)\ndef gs() gets.chomp end\ndef gi() gets.to_i end\ndef gsmi() gets.chomp.split.map(&:to_i) end\n\ndef desc(ar) ar.sort!{|x,y|y<=>x} end\ndef min(a,b) a<=b ? a : b end\ndef max(a,b) a>=b ? a : b end\ndef sum(ar) ar.inject(:+) end\n\n#def bs(ar,eq,n) en=(eq==1 ? :>= : :>); ar.bsearch_index{|x|x.send(en,n)} end\ndef C(a,b) b==0||a==b ? 1 : (b=a-b if a/2= 1) ? m[i - 1][j] : -1)\n left = ((j >= 1) ? m[i][j - 1] : -1)\n x = pool.pop\n retry_count = 5\n while ((x - upper).abs == 1 || (x - left).abs == 1) && retry_count > 0\n retry_count -= 1\n pool.push(x)\n pool.shuffle!\n x = pool.pop\n end\n if retry_count == 0\n return gen_matrix(n, seed + 1)\n end\n m[i][j] = x\n end\n end\n return m\nend\n\nt = gets.to_s.to_i\n1.upto(t) do |casenum|\n n = gets.to_s.to_i\n if n == 2\n printf(\"-1\\n\")\n next\n end\n m = gen_matrix(n, n)\n m.each {|row| printf(\"%s\\n\", row.join(\" \")) }\nend\n"}, {"source_code": "t = Integer(gets.chomp)\r\nt.times{\r\n n = Integer(gets.chomp)\r\n \r\n if(n==2)\r\n puts -1\r\n else\r\n ev = 1\r\n od = n%2 == 0 ? (n*n)/2 + 1 : ((n*n)+1)/2 + 1\r\n for i in 0...n do\r\n for j in 0...n do\r\n if((i+j)%2 == 0)\r\n print \"#{ev} \"\r\n ev += 1\r\n else\r\n print \"#{od} \"\r\n od += 1\r\n end\r\n end\r\n print \"\\n\"\r\n end\r\n end\r\n}"}, {"source_code": "gets.to_i.times {\n n = gets.to_i\n\n if n == 2\n p -1\n next\n end\n\n m = Array.new(n) { [0] * n }\n\n cur = 1\n n.times { |i|\n n.times { |j|\n next if (i & 1) != (j & 1)\n m[i][j] = cur\n cur += 1\n }\n }\n n.times { |i|\n n.times { |j|\n next if (i & 1) == (j & 1)\n m[i][j] = cur\n cur += 1\n }\n }\n\n n.times { |i|\n puts m[i].join(' ')\n }\n}\n"}], "negative_code": [{"source_code": "def gen_matrix(n, seed = nil)\n m = n.times.map { [-1] * n }\n seed ||= n\n srand(seed)\n pool = 1.upto(n * n).map {|i| i}.shuffle\n\n 0.upto(n - 1) do |i|\n 0.upto(n - 1) do |j|\n upper = ((i >= 1) ? m[i - 1][j] : -1)\n left = ((j >= 1) ? m[i][j - 1] : -1)\n x = pool.pop\n retry_count = 5\n while (x - upper).abs == 1 || (x - left).abs == 1 && retry_count > 0\n retry_count -= 1\n pool.push(x)\n pool.shuffle!\n x = pool.pop\n end\n if retry_count == 0\n return gen_matrix(n, seed + 1)\n end\n m[i][j] = x\n end\n end\n return m\nend\n\nt = gets.to_s.to_i\n1.upto(t) do |casenum|\n n = gets.to_s.to_i\n if n == 2\n printf(\"-1\\n\")\n next\n end\n m = gen_matrix(n, n)\n m.each {|row| printf(\"%s\\n\", row.join(\" \")) }\nend\n"}, {"source_code": "def gen_matrix(n)\n m = n.times.map { [-1] * n }\n pool = 1.upto(n * n).map {|i| i}.shuffle\n\n 0.upto(n - 1) do |i|\n 0.upto(n - 1) do |j|\n upper = i >= 1 ? m[i - 1][j] : -1\n left = j >= 1 ? m[i][j - i] : -1\n x = pool.pop\n return gen_matrix(n) if (x - upper).abs == 1 || (x - left).abs == 1\n m[i][j] = x\n end\n end\n return m\nend\n\nt = gets.to_s.to_i\n1.upto(t) do |casenum|\n n = gets.to_s.to_i\n if n == 2\n printf(\"-1\\n\")\n next\n end\n m = gen_matrix(n)\n m.each {|row| printf(\"%s\\n\", row.join(\" \")) }\nend\n"}, {"source_code": "def gen_matrix(n)\n m = n.times.map { [0] * n }\n srand(n)\n pool = 1.upto(n * n).map {|i| i}.shuffle\n\n 0.upto(n - 1) do |i|\n 0.upto(n - 1) do |j|\n upper = i >= 1 ? m[i - 1][j] : -1\n left = j >= 1 ? m[i][j - i] : -1\n\n x = pool.shift\n while (x - upper).abs == 1 || (x - left).abs == 1\n pool.push(x)\n x = pool.shift\n end\n m[i][j] = x\n end\n end\n return m\nend\n\nt = gets.to_s.to_i\n1.upto(t) do |casenum|\n n = gets.to_s.to_i\n if n == 2\n printf(\"-1\\n\")\n next\n end\n m = gen_matrix(n)\n m.each {|row| printf(\"%s\\n\", row.join(\" \")) }\nend\n"}, {"source_code": "t = Integer(gets.chomp)\r\nt.times{\r\n n = Integer(gets.chomp)\r\n \r\n if(n==2)\r\n puts -1\r\n else\r\n ev = 1\r\n od = n%2 == 0 ? (n*n)/2 : ((n*n)+1)/2 + 1\r\n for i in 0...n do\r\n for j in 0...n do\r\n if((i+j)%2 == 0)\r\n print \"#{ev} \"\r\n ev += 1\r\n else\r\n print \"#{od} \"\r\n od += 1\r\n end\r\n end\r\n print \"\\n\"\r\n end\r\n end\r\n}"}], "src_uid": "e215e94dd196dde381adc13406d2d72a"} {"nl": {"description": "Let there be an array $$$b_1, b_2, \\ldots, b_k$$$. Let there be a partition of this array into segments $$$[l_1; r_1], [l_2; r_2], \\ldots, [l_c; r_c]$$$, where $$$l_1 = 1$$$, $$$r_c = k$$$, and for any $$$2 \\leq i \\leq c$$$ holds that $$$r_{i-1} + 1 = l_i$$$. In other words, each element of the array belongs to exactly one segment.Let's define the cost of a partition as $$$$$$c + \\sum_{i = 1}^{c} \\operatorname{mex}(\\{b_{l_i}, b_{l_i + 1}, \\ldots, b_{r_i}\\}),$$$$$$ where $$$\\operatorname{mex}$$$ of a set of numbers $$$S$$$ is the smallest non-negative integer that does not occur in the set $$$S$$$. In other words, the cost of a partition is the number of segments plus the sum of MEX over all segments. Let's define the value of an array $$$b_1, b_2, \\ldots, b_k$$$ as the maximum possible cost over all partitions of this array.You are given an array $$$a$$$ of size $$$n$$$. Find the sum of values of all its subsegments.An array $$$x$$$ is a subsegment of an array $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.", "input_spec": "The input contains several test cases. The first line contains one integer $$$t$$$ ($$$1 \\leq t \\leq 30$$$) — the number of test cases. The first line for each test case contains one integer $$$n$$$ ($$$1 \\leq n \\leq 100$$$) — the length of the array. The second line contains a sequence of integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$0 \\leq a_i \\leq 10^9$$$) — the array elements. It is guaranteed that the sum of the values $$$n$$$ over all test cases does not exceed $$$100$$$.", "output_spec": "For each test case print a single integer — the answer to the problem.", "sample_inputs": ["4\n2\n1 2\n3\n2 0 1\n4\n2 0 5 1\n5\n0 1 1 0 1"], "sample_outputs": ["4\n14\n26\n48"], "notes": "NoteIn the second test case: The best partition for the subsegment $$$[2, 0, 1]$$$: $$$[2], [0, 1]$$$. The cost of this partition equals to $$$2 + \\operatorname{mex}(\\{2\\}) + \\operatorname{mex}(\\{0, 1\\}) = 2 + 0 + 2 = 4$$$. The best partition for the subsegment $$$[2, 0]$$$: $$$[2], [0]$$$. The cost of this partition equals to $$$2 + \\operatorname{mex}(\\{2\\}) + \\operatorname{mex}(\\{0\\}) = 2 + 0 + 1 = 3$$$ The best partition for the subsegment $$$[2]$$$: $$$[2]$$$. The cost of this partition equals to $$$1 + \\operatorname{mex}(\\{2\\}) = 1 + 0 = 1$$$. The best partition for the subsegment $$$[0, 1]$$$: $$$[0, 1]$$$. The cost of this partition equals to $$$1 + \\operatorname{mex}(\\{0, 1\\}) = 1 + 2 = 3$$$. The best partition for the subsegment $$$[0]$$$: $$$[0]$$$. The cost of this partition equals to $$$1 + \\operatorname{mex}(\\{0\\}) = 1 + 1 = 2$$$. The best partition for the subsegment $$$[1]$$$: $$$[1]$$$. The cost of this partition equals to $$$1 + \\operatorname{mex}(\\{1\\}) = 1 + 0 = 1$$$. The sum of values over all subsegments equals to $$$4 + 3 + 1 + 3 + 2 + 1 = 14$$$."}, "positive_code": [{"source_code": "class Array\r\n def n\r\n return length\r\n end\r\nend\r\n\r\n$__cur_readed = []\r\n$__cur_index = 0\r\n\r\ndef rs\r\n\twhile $__cur_index >= $__cur_readed.length\r\n\t\t$__cur_readed = gets.split(\" \")\r\n\t\t$__cur_index = 0\r\n\tend\r\n\t$__cur_index+=1\r\n\treturn $__cur_readed[$__cur_index-1]\r\nend\r\n\r\ndef ri\t\r\n\tres = rs.to_i\r\nend\r\n\r\ndef ra2 n\r\n\treturn Array.new(n) {|i| ri}\r\nend\r\n\r\ndef ra\r\n\treturn ra2 ri\r\nend\r\n\r\nt = ri\r\n\r\nfor _ in (1..t)\r\n\ta = ra\r\n\tres = 0\r\n\tfor i in (0..a.n-1)\r\n\t\tfor j in (i..a.n-1)\r\n\t\t\tfor k in (i..j)\r\n\t\t\t\tres+=1\r\n\t\t\t\tif a[k] == 0\r\n\t\t\t\t\tres +=1\r\n\t\t\t\tend\r\n\t\t\tend\r\n\t\tend\r\n\tend\r\n\tputs res\r\nend"}, {"source_code": "$__cur_readed = []\r\n$__cur_index = 0\r\n\r\ndef rs\r\n\twhile $__cur_index >= $__cur_readed.length\r\n\t\t$__cur_readed = gets.split(\" \")\r\n\t\t$__cur_index = 0\r\n\tend\r\n\t$__cur_index+=1\r\n\treturn $__cur_readed[$__cur_index-1]\r\nend\r\n\r\n\r\ndef ri\t\r\n\treturn rs.to_i\r\nend\r\n\r\ndef ra2 n\r\n\treturn Array.new(n) {|i| ri}\r\nend\r\n\r\ndef ra\r\n\treturn ra2 ri\r\nend\r\n\r\nt = ri\r\n\r\nfor _ in (1..t)\r\n\ta = ra\r\n\tres = 0\r\n\tfor i in (0..a.length-1)\r\n\t\tfor j in (i..a.length-1)\r\n\t\t\tfor k in (i..j)\r\n\t\t\t\tres+=1\r\n\t\t\t\tif a[k] == 0\r\n\t\t\t\t\tres +=1\r\n\t\t\t\tend\r\n\t\t\tend\r\n\t\tend\r\n\tend\r\n\tputs res\r\nend"}, {"source_code": "gets.to_i.times {\n n = gets.to_i\n xs = gets.split.map(&:to_i).map { _1 > 0 ? 0 : 1 }\n ans = 0\n n.times { |r|\n (r + 1).times { |l|\n ans += r - l + 1\n ans += xs[l..r].sum\n }\n }\n p ans\n}\n"}], "negative_code": [{"source_code": "$__cur_readed = []\r\n$__cur_index = 0\r\n\r\ndef rs\r\n\twhile $__cur_index >= $__cur_readed.length\r\n\t\t$__cur_readed = gets.split(\" \")\r\n\t\t$__cur_index = 0\r\n\tend\r\n\t$__cur_index+=1\r\n\treturn $__cur_readed[$__cur_index-1]\r\nend\r\n\r\n\r\ndef ri\t\r\n\treturn rs.to_i\r\nend\r\n\r\ndef ra2 n\r\n\treturn Array.new(n) {|i| ri}\r\nend\r\n\r\ndef ra\r\n\treturn ra2 ri\r\nend\r\n\r\nt = ri\r\n\r\nfor _ in (1..t)\r\n\ta = ra\r\n\tres = 0\r\n\tfor i in (0..a.length-1)\r\n\t\tfor j in (i..a.length-1)\r\n\t\t\tfor k in (i..j)\r\n\t\t\t\tres+=1\r\n\t\t\t\tif a[res] == 0\r\n\t\t\t\t\tres +=1\r\n\t\t\t\tend\r\n\t\t\tend\r\n\t\tend\r\n\tend\r\n\tputs res\r\nend"}], "src_uid": "8775da9b78d8236c4606d150df450952"} {"nl": {"description": "You are given a permutation $$$a$$$ consisting of $$$n$$$ numbers $$$1$$$, $$$2$$$, ..., $$$n$$$ (a permutation is an array in which each element from $$$1$$$ to $$$n$$$ occurs exactly once).You can perform the following operation: choose some subarray (contiguous subsegment) of $$$a$$$ and rearrange the elements in it in any way you want. But this operation cannot be applied to the whole array.For example, if $$$a = [2, 1, 4, 5, 3]$$$ and we want to apply the operation to the subarray $$$a[2, 4]$$$ (the subarray containing all elements from the $$$2$$$-nd to the $$$4$$$-th), then after the operation, the array can become $$$a = [2, 5, 1, 4, 3]$$$ or, for example, $$$a = [2, 1, 5, 4, 3]$$$.Your task is to calculate the minimum number of operations described above to sort the permutation $$$a$$$ in ascending order.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 2000$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 \\le n \\le 50$$$) — the number of elements in the permutation. The second line of the test case contains $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ — the given permutation $$$a$$$.", "output_spec": "For each test case, output a single integer — the minimum number of operations described above to sort the array $$$a$$$ in ascending order.", "sample_inputs": ["3\n4\n1 3 2 4\n3\n1 2 3\n5\n2 1 4 5 3"], "sample_outputs": ["1\n0\n2"], "notes": "NoteIn the explanations, $$$a[i, j]$$$ defines the subarray of $$$a$$$ that starts from the $$$i$$$-th element and ends with the $$$j$$$-th element.In the first test case of the example, you can select the subarray $$$a[2, 3]$$$ and swap the elements in it.In the second test case of the example, the permutation is already sorted, so you don't need to apply any operations.In the third test case of the example, you can select the subarray $$$a[3, 5]$$$ and reorder the elements in it so $$$a$$$ becomes $$$[2, 1, 3, 4, 5]$$$, and then select the subarray $$$a[1, 2]$$$ and swap the elements in it, so $$$a$$$ becomes $$$[1, 2, 3, 4, 5]$$$."}, "positive_code": [{"source_code": "def gli; gets.split.map(&:to_i); end\r\ndef gi; gets.to_i; end\r\n\r\ngi.times do\r\n n = gi\r\n a = gli\r\n if a == (1..n).to_a\r\n puts 0\r\n elsif a[0] == n && a[-1] == 1\r\n puts 3\r\n elsif a[0] == 1 || a[-1] == n\r\n puts 1\r\n else\r\n puts 2\r\n end\r\nend"}, {"source_code": "def f\r\n n = gets.chomp.to_i\r\n a = gets.chomp.split(\" \").map(&:to_i)\r\n\r\n if a.each_with_index.all?{|c, i| c == i + 1}\r\n puts 0\r\n elsif a.first == 1 || a.last == n\r\n puts 1\r\n elsif a.first == n && a.last == 1\r\n puts 3\r\n else\r\n puts 2\r\n end\r\nend\r\n\r\n\r\nt = gets.chomp.to_i\r\nt.times do\r\n f()\r\nend\r\n"}, {"source_code": "t = gets.to_s.to_i\n1.upto(t) do |casenum|\n n = gets.to_s.to_i\n a = gets.to_s.split.map {|x| x.to_i }\n sa = a.sort\n already_sorted = true\n a.each_with_index do |x, i|\n if sa[i] != x\n already_sorted = false\n break\n end\n end\n\n if a[0] == sa[-1] && a[-1] == sa[0]\n printf(\"3\\n\")\n elsif sa[0] == a[0] || sa[-1] == a[-1]\n printf(\"%d\\n\", already_sorted ? 0 : 1)\n else\n printf(\"2\\n\")\n end\nend\n"}, {"source_code": "t = gets.to_i\r\nt.times{\r\n\tn = gets.to_i\r\n\tarr = gets.split.map &:to_i\r\n\tif arr == arr.sort then\r\n\t\tp 0\r\n\telsif arr[0] == arr.min || arr.last == arr.max then\r\n\t\tp 1\r\n\telsif arr[0] == arr.max && arr.last == arr.min then\r\n\t\tp 3\r\n\telse\r\n\t\tp 2\r\n\tend\r\n}"}], "negative_code": [{"source_code": "def gli; gets.split.map(&:to_i); end\r\ndef gi; gets.to_i; end\r\n\r\ngi.times do\r\n n = gi\r\n a = gli\r\n if a == (1..n).to_a\r\n puts 0\r\n elsif a[0] == 1 || a[-1] == n\r\n puts 1\r\n else\r\n puts 2\r\n end\r\nend"}, {"source_code": "def gli; gets.split.map(&:to_i); end\r\ndef gi; gets.to_i; end\r\n\r\ngi.times do\r\n n = gi\r\n a = gli\r\n if a.sort == (1..n).to_a\r\n puts 0\r\n elsif a[0] == 1 || a[-1] == n\r\n puts 1\r\n else\r\n puts 2\r\n end\r\nend"}, {"source_code": "def gli; gets.split.map(&:to_i); end\r\ndef gi; gets.to_i; end\r\n\r\ngi.times do\r\n _ = gi\r\n a = gli\r\n if a.sort == a\r\n puts 0\r\n elsif a.min == a[0] || a.max == a[-1]\r\n puts 1\r\n else\r\n puts 2\r\n end\r\nend"}, {"source_code": "def f\r\n n = gets.chomp.to_i\r\n a = gets.chomp.split(\" \").map(&:to_i)\r\n\r\n if a.each_with_index.all?{|c, i| c == i + 1}\r\n puts 0\r\n elsif a.first == 1 || a.last == n\r\n puts 1\r\n else\r\n puts 2\r\n end\r\nend\r\n\r\n\r\nt = gets.chomp.to_i\r\nt.times do\r\n f()\r\nend\r\n"}, {"source_code": "t = gets.to_s.to_i\n1.upto(t) do |casenum|\n n = gets.to_s.to_i\n a = gets.to_s.split.map {|x| x.to_i }\n sa = a.sort\n already_sorted = true\n a.each_with_index do |x, i|\n if sa[i] != x\n already_sorted = false\n break\n end\n end\n\n if sa[0] == a[0] || sa[-1] == a[-1]\n printf(\"%d\\n\", already_sorted ? 0 : 1)\n else\n printf(\"2\\n\")\n end\nend\n"}, {"source_code": "t = gets.to_i\r\nt.times{\r\n\tn = gets.to_i\r\n\tarr = gets.split.map &:to_i\r\n\tif arr == arr.sort then\r\n\t\tp 0\r\n\telsif arr[0] == arr.min || arr.last == arr.max then\r\n\t\tp 1\r\n\telse\r\n\t\tp 2\r\n\tend\r\n}"}], "src_uid": "c212524cc1ad8e0332693e3cf644854b"} {"nl": {"description": "Nastya came to her informatics lesson, and her teacher who is, by the way, a little bit famous here gave her the following task.Two matrices $$$A$$$ and $$$B$$$ are given, each of them has size $$$n \\times m$$$. Nastya can perform the following operation to matrix $$$A$$$ unlimited number of times: take any square square submatrix of $$$A$$$ and transpose it (i.e. the element of the submatrix which was in the $$$i$$$-th row and $$$j$$$-th column of the submatrix will be in the $$$j$$$-th row and $$$i$$$-th column after transposing, and the transposed submatrix itself will keep its place in the matrix $$$A$$$). Nastya's task is to check whether it is possible to transform the matrix $$$A$$$ to the matrix $$$B$$$. Example of the operation As it may require a lot of operations, you are asked to answer this question for Nastya.A square submatrix of matrix $$$M$$$ is a matrix which consist of all elements which comes from one of the rows with indeces $$$x, x+1, \\dots, x+k-1$$$ of matrix $$$M$$$ and comes from one of the columns with indeces $$$y, y+1, \\dots, y+k-1$$$ of matrix $$$M$$$. $$$k$$$ is the size of square submatrix. In other words, square submatrix is the set of elements of source matrix which form a solid square (i.e. without holes).", "input_spec": "The first line contains two integers $$$n$$$ and $$$m$$$ separated by space ($$$1 \\leq n, m \\leq 500$$$) — the numbers of rows and columns in $$$A$$$ and $$$B$$$ respectively. Each of the next $$$n$$$ lines contains $$$m$$$ integers, the $$$j$$$-th number in the $$$i$$$-th of these lines denotes the $$$j$$$-th element of the $$$i$$$-th row of the matrix $$$A$$$ ($$$1 \\leq A_{ij} \\leq 10^{9}$$$). Each of the next $$$n$$$ lines contains $$$m$$$ integers, the $$$j$$$-th number in the $$$i$$$-th of these lines denotes the $$$j$$$-th element of the $$$i$$$-th row of the matrix $$$B$$$ ($$$1 \\leq B_{ij} \\leq 10^{9}$$$).", "output_spec": "Print \"YES\" (without quotes) if it is possible to transform $$$A$$$ to $$$B$$$ and \"NO\" (without quotes) otherwise. You can print each letter in any case (upper or lower).", "sample_inputs": ["2 2\n1 1\n6 1\n1 6\n1 1", "2 2\n4 4\n4 5\n5 4\n4 4", "3 3\n1 2 3\n4 5 6\n7 8 9\n1 4 7\n2 5 6\n3 8 9"], "sample_outputs": ["YES", "NO", "YES"], "notes": "NoteConsider the third example. The matrix $$$A$$$ initially looks as follows.$$$$$$ \\begin{bmatrix} 1 & 2 & 3\\\\ 4 & 5 & 6\\\\ 7 & 8 & 9 \\end{bmatrix} $$$$$$Then we choose the whole matrix as transposed submatrix and it becomes$$$$$$ \\begin{bmatrix} 1 & 4 & 7\\\\ 2 & 5 & 8\\\\ 3 & 6 & 9 \\end{bmatrix} $$$$$$Then we transpose the submatrix with corners in cells $$$(2, 2)$$$ and $$$(3, 3)$$$. $$$$$$ \\begin{bmatrix} 1 & 4 & 7\\\\ 2 & \\textbf{5} & \\textbf{8}\\\\ 3 & \\textbf{6} & \\textbf{9} \\end{bmatrix} $$$$$$So matrix becomes$$$$$$ \\begin{bmatrix} 1 & 4 & 7\\\\ 2 & 5 & 6\\\\ 3 & 8 & 9 \\end{bmatrix} $$$$$$and it is $$$B$$$."}, "positive_code": [{"source_code": "n, m = gets.split(\" \").map(&:to_i)\na = []\nb = []\n(0..n-1).each do |i|\n a += gets.split(\" \").map(&:to_i)\nend\n\n(0..n-1).each do |i|\n b += gets.split(\" \").map(&:to_i)\nend\nh = []\nl = []\n(0..n-1).each do |i|\n (0..m-1).each do |j|\n\t h[i+j].nil? ? h[i+j]=[a[i*m+j]] : h[i+j] << a[i*m+j]\n\t l[i+j].nil? ? l[i+j]=[b[i*m+j]] : l[i+j] << b[i*m+j]\n end\nend\nres = \"YES\"\n(0..n+m-2).each do |i|\n res = \"NO\" if h[i].sort != l[i].sort\n break if res == \"NO\"\nend\n\nputs res\n\n\n\n"}], "negative_code": [{"source_code": "n, m = gets.split(\" \").map(&:to_i)\na = []\nb = []\n(0..n-1).each do |i|\n a += gets.split(\" \").map(&:to_i)\nend\n\n(0..n-1).each do |i|\n b += gets.split(\" \").map(&:to_i)\nend\n\nh = {}\n(0..n-1).each do |i|\n (0..m-1).each do |j|\n if h[i+j]\n h[i+j][a[i*n+j]].nil? ? h[i+j][a[i*n+j]] = 1 : h[i+j][a[i*n+j]] += 1\n else\n h[i+j] = {a[i*n+j] => 1}\n end\n end\nend\nres = \"YES\"\n(0..n-1).each do |i|\n (0..m-1).each do |j|\n if h[i+j][b[i*n+j]].nil?\n res = \"NO\" \n else\n\t if h[i+j][b[i*n+j]] < 1\n\t res = \"NO\"\n end \n end \n end\n break if res == \"NO\"\nend\n\nputs res\n\n\n\n"}, {"source_code": "n, m = gets.split(\" \").map(&:to_i)\na = []\nb = []\n(0..n-1).each do |i|\n a += gets.split(\" \").map(&:to_i)\nend\n\n(0..n-1).each do |i|\n b += gets.split(\" \").map(&:to_i)\nend\nh = {}\n(0..n-1).each do |i|\n (0..m-1).each do |j|\n if h[i+j]\n if h[i+j][a[i*m+j]].nil?\n\t\th[i+j][a[i*m+j]] = 1\n\t else\n\t\th[i+j][a[i*m+j]] += 1\n\t end\n else\n h[i+j] = {a[i*m+j] => 1}\n end\n end\nend\nres = \"YES\"\n(0..n-1).each do |i|\n (0..m-1).each do |j|\n if h[i+j][b[i*m+j]].nil?\n res = \"NO\" \n else\n\t if h[i+j][b[i*m+j]] < 1\n\t res = \"NO\"\n end \n end \n end\n break if res == \"NO\"\nend\n\nputs res\n\n\n\n"}], "src_uid": "77e2ddc4684fccd1856c93c2fc6e1ce2"} {"nl": {"description": "You are a game designer and want to make an obstacle course. The player will walk from left to right. You have $$$n$$$ heights of mountains already selected and want to arrange them so that the absolute difference of the heights of the first and last mountains is as small as possible. In addition, you want to make the game difficult, and since walking uphill or flat is harder than walking downhill, the difficulty of the level will be the number of mountains $$$i$$$ ($$$1 \\leq i < n$$$) such that $$$h_i \\leq h_{i+1}$$$ where $$$h_i$$$ is the height of the $$$i$$$-th mountain. You don't want to waste any of the mountains you modelled, so you have to use all of them. From all the arrangements that minimize $$$|h_1-h_n|$$$, find one that is the most difficult. If there are multiple orders that satisfy these requirements, you may find any.", "input_spec": "The first line will contain a single integer $$$t$$$ ($$$1 \\leq t \\leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \\leq n \\leq 2 \\cdot 10^5$$$) — the number of mountains. The second line of each test case contains $$$n$$$ integers $$$h_1,\\ldots,h_n$$$ ($$$1 \\leq h_i \\leq 10^9$$$), where $$$h_i$$$ is the height of the $$$i$$$-th mountain. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, output $$$n$$$ integers — the given heights in an order that maximizes the difficulty score among all orders that minimize $$$|h_1-h_n|$$$. If there are multiple orders that satisfy these requirements, you may output any.", "sample_inputs": ["2\n4\n4 2 1 2\n2\n3 1"], "sample_outputs": ["2 4 1 2 \n1 3"], "notes": "NoteIn the first test case:The player begins at height $$$2$$$, next going up to height $$$4$$$ increasing the difficulty by $$$1$$$. After that he will go down to height $$$1$$$ and the difficulty doesn't change because he is going downhill. Finally the player will go up to height $$$2$$$ and the difficulty will increase by $$$1$$$. The absolute difference between the starting height and the end height is equal to $$$0$$$ and it's minimal. The difficulty is maximal.In the second test case:The player begins at height $$$1$$$, next going up to height $$$3$$$ increasing the difficulty by $$$1$$$. The absolute difference between the starting height and the end height is equal to $$$2$$$ and it's minimal as they are the only heights. The difficulty is maximal."}, "positive_code": [{"source_code": "gets.to_i.times {\n n = gets.to_i\n xs = gets.split.map!(&:to_i).sort\n\n if n == 2\n puts xs.join(' ')\n next\n end\n\n best_d = nil\n best_i = nil\n (1...n).each { |i|\n d = (xs[i] - xs[i - 1]).abs\n if !best_d || d < best_d\n best_i = i\n best_d = d\n end\n }\n\n ys = xs[best_i..] + xs[...best_i]\n puts ys.join(' ')\n}\n"}], "negative_code": [], "src_uid": "3342e71884677534b7a126f89d441585"} {"nl": {"description": "Valera's lifelong ambition was to be a photographer, so he bought a new camera. Every day he got more and more clients asking for photos, and one day Valera needed a program that would determine the maximum number of people he can serve.The camera's memory is d megabytes. Valera's camera can take photos of high and low quality. One low quality photo takes a megabytes of memory, one high quality photo take b megabytes of memory. For unknown reasons, each client asks him to make several low quality photos and several high quality photos. More formally, the i-th client asks to make xi low quality photos and yi high quality photos.Valera wants to serve as many clients per day as possible, provided that they will be pleased with his work. To please the i-th client, Valera needs to give him everything he wants, that is, to make xi low quality photos and yi high quality photos. To make one low quality photo, the camera must have at least a megabytes of free memory space. Similarly, to make one high quality photo, the camera must have at least b megabytes of free memory space. Initially the camera's memory is empty. Valera also does not delete photos from the camera so that the camera's memory gradually fills up.Calculate the maximum number of clients Valera can successfully serve and print the numbers of these clients.", "input_spec": "The first line contains two integers n and d (1 ≤ n ≤ 105, 1 ≤ d ≤ 109) — the number of clients and the camera memory size, correspondingly. The second line contains two integers a and b (1 ≤ a ≤ b ≤ 104) — the size of one low quality photo and of one high quality photo, correspondingly. Next n lines describe the clients. The i-th line contains two integers xi and yi (0 ≤ xi, yi ≤ 105) — the number of low quality photos and high quality photos the i-th client wants, correspondingly. All numbers on all lines are separated by single spaces. ", "output_spec": "On the first line print the answer to the problem — the maximum number of clients that Valera can successfully serve. Print on the second line the numbers of the client in any order. All numbers must be distinct. If there are multiple answers, print any of them. The clients are numbered starting with 1 in the order in which they are defined in the input data.", "sample_inputs": ["3 10\n2 3\n1 4\n2 1\n1 0", "3 6\n6 6\n1 1\n1 0\n1 0"], "sample_outputs": ["2\n3 2", "1\n2"], "notes": null}, "positive_code": [{"source_code": "f=->{gets.split.map &:to_i}\nn,d=f[]\na,b=f[]\nr=(1..n).map{|i|gets(\" \").to_i*a+gets.to_i*b}.zip(1..n).sort_by(&:first).take_while{|i|0<=d-=i[0]}\nputs r.size,r.map(&:last)*\" \""}, {"source_code": "n,d = gets.split.map(&:to_i)\na,b = gets.split.map(&:to_i)\n\nw = [*$<].map{|i|t1,t2 = i.split.map(&:to_i); t1*a+t2*b}\n\nx = w.zip(1..n).sort_by(&:first)\n\ns = 0\nr = x.take_while{ |i| d >= s += i.first }\nputs r.size\nputs r.map(&:last)*\" \""}, {"source_code": "f=->{gets.split.map &:to_i}\nn,d=f[]\na,b=f[]\nr=(1..n).map{|i|gets(\" \").to_i*a+gets.to_i*b}.zip(1..n).sort_by(&:first).take_while{|i|0<=d-=i[0]}\nputs r.size,r.map(&:last)*\" \"\n"}, {"source_code": "f=->{gets.split.map &:to_i}\nn,d=f[]\na,b=f[]\nr=(1..n).map{|i|gets(\" \").to_i*a+gets.to_i*b}.zip(1..n).sort_by(&:first).take_while{|i|0<=d-=i[0]}\nputs r.size,r.map(&:last)*\" \"\n"}, {"source_code": "f=->{gets.split.map &:to_i}\nn,d=f[]\na,b=f[]\nr=(1..n).map{|i|gets(\" \").to_i*a+gets.to_i*b}.zip(1..n).sort_by(&:first).take_while{|i|0<=d-=i[0]}\nputs r.size,r.map(&:last)*\" \"\n"}, {"source_code": "f=->{gets.split.map &:to_i}\nn,d=f[]\na,b=f[]\nr=(1..n).map{|i|gets(\" \").to_i*a+gets.to_i*b}.zip(1..n).sort_by(&:first).take_while{|i|0<=d-=i[0]}\nputs r.size,r.map(&:last)*\" \"\n"}, {"source_code": "f=->{gets.split.map &:to_i}\nn,d=f[]\na,b=f[]\nr=(1..n).map{|i|gets(\" \").to_i*a+gets.to_i*b}.zip(1..n).sort_by(&:first).take_while{|i|0<=d-=i[0]}\nputs r.size,r.map(&:last)*\" \""}, {"source_code": "f=->{gets.split.map &:to_i}\nn,d=f[]\na,b=f[]\nr=(1..n).map{|i|gets(\" \").to_i*a+gets.to_i*b}.zip(1..n).sort_by(&:first).take_while{|i|0<=d-=i[0]}\nputs r.size,r.map(&:last)*\" \""}, {"source_code": "f=->{gets.split.map &:to_i}\nn,d=f[]\na,b=f[]\nr=(1..n).map{|i|gets(\" \").to_i*a+gets.to_i*b}.zip(1..n).sort_by(&:first).take_while{|i|0<=d-=i[0]}\nputs r.size,r.map(&:last)*\" \"\n"}, {"source_code": "n, d = gets.split.map(&:to_i)\na, b = gets.split.map(&:to_i)\nxys = n.times.map{gets.split.map(&:to_i)}\narray = xys.map.with_index{|(x, y), i| [i, x * a + y * b]}.sort_by(&:last)\ntaken = 0\narray = array.take_while do |i, cost|\n flag = taken + cost <= d\n taken += cost\n flag\nend\nputs array.length\nputs array.map(&:first).map{|i| i + 1}.join(\" \")\n"}, {"source_code": "f=->{gets.split.map &:to_i}\nn,d=f[]\na,b=f[]\nr=(1..n).map{|i|gets(\" \").to_i*a+gets.to_i*b}.zip(1..n).sort_by(&:first).take_while{|i|0<=d-=i[0]}\nputs r.size,r.map(&:last)*\" \"\n"}, {"source_code": "f=->{gets.split.map &:to_i}\nn,d=f[]\na,b=f[]\nr=(1..n).map{|i|gets(\" \").to_i*a+gets.to_i*b}.zip(1..n).sort_by(&:first).take_while{|i|0<=d-=i[0]}\nputs r.size,r.map(&:last)*\" \"\n"}, {"source_code": "f=->{gets.split.map &:to_i}\nn,d=f[]\na,b=f[]\nr=(1..n).map{|i|gets(\" \").to_i*a+gets.to_i*b}.zip(1..n).sort_by(&:first).take_while{|i|0<=d-=i[0]}\nputs r.size,r.map(&:last)*\" \"\n"}], "negative_code": [{"source_code": "n,d = gets.split.map(&:to_i)\na,b = gets.split.map(&:to_i)\n\nw = [*$<].map{|i|t1,t2 = i.split.map(&:to_i); t1*a+t2*b}\n\nx = w.zip(1..n).sort_by(&:first)\n\ns = 0\nr = x.take_while{ |i| d > s += i.first }\nputs r.size\nputs r.map(&:last)*\" \""}, {"source_code": "$n,$d = gets.split.map(&:to_i)\na,b = gets.split.map(&:to_i)\n\n$w = [*$<].map{|i|t1,t2 = i.split.map(&:to_i); t1*a+t2*b}\n\ndef more i,s,n\n (i...$n).map{ |j|\n next [s,n] if s+$w[j] > $d\n more j+1,s+$w[j],n+[j]\n }.max_by{|i|[i[0],i[1].size]} || [s,n]\nend\n\nr = more 0,0,[]\nputs r[1].size\nputs r[1].map{ |i| i+1 }*\" \"\n"}, {"source_code": "$n,$d = gets.split.map(&:to_i)\na,b = gets.split.map(&:to_i)\n\n$w = [*$<].map{|i|t1,t2 = i.split.map(&:to_i); t1*a+t2*b}\n\ndef more i,s,n\n (i...$n).map{ |j|\n next [s,n] if s+$w[j] > $d\n more j+1,s+$w[j],n+[j]\n }.max_by(&:first) || [s,n]\nend\n\nr = more 0,0,[]\nputs r[1].size\nputs r[1].map{ |i| i+1 }*\" \"\n"}], "src_uid": "4d7de18e76600777ff023e1b61366ee4"} {"nl": {"description": "The length of the longest common prefix of two strings $$$s = s_1 s_2 \\ldots s_n$$$ and $$$t = t_1 t_2 \\ldots t_m$$$ is defined as the maximum integer $$$k$$$ ($$$0 \\le k \\le min(n,m)$$$) such that $$$s_1 s_2 \\ldots s_k$$$ equals $$$t_1 t_2 \\ldots t_k$$$.Koa the Koala initially has $$$n+1$$$ strings $$$s_1, s_2, \\dots, s_{n+1}$$$.For each $$$i$$$ ($$$1 \\le i \\le n$$$) she calculated $$$a_i$$$ — the length of the longest common prefix of $$$s_i$$$ and $$$s_{i+1}$$$.Several days later Koa found these numbers, but she couldn't remember the strings.So Koa would like to find some strings $$$s_1, s_2, \\dots, s_{n+1}$$$ which would have generated numbers $$$a_1, a_2, \\dots, a_n$$$. Can you help her?If there are many answers print any. We can show that answer always exists for the given constraints. ", "input_spec": "Each test contains multiple test cases. The first line contains $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 100$$$) — the number of elements in the list $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$0 \\le a_i \\le 50$$$) — the elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.", "output_spec": "For each test case: Output $$$n+1$$$ lines. In the $$$i$$$-th line print string $$$s_i$$$ ($$$1 \\le |s_i| \\le 200$$$), consisting of lowercase Latin letters. Length of the longest common prefix of strings $$$s_i$$$ and $$$s_{i+1}$$$ has to be equal to $$$a_i$$$. If there are many answers print any. We can show that answer always exists for the given constraints.", "sample_inputs": ["4\n4\n1 2 4 2\n2\n5 3\n3\n1 3 1\n3\n0 0 0"], "sample_outputs": ["aeren\nari\narousal\naround\nari\nmonogon\nmonogamy\nmonthly\nkevinvu\nkuroni\nkurioni\nkorone\nanton\nloves\nadhoc\nproblems"], "notes": "NoteIn the $$$1$$$-st test case one of the possible answers is $$$s = [aeren, ari, arousal, around, ari]$$$.Lengths of longest common prefixes are: Between $$$\\color{red}{a}eren$$$ and $$$\\color{red}{a}ri$$$ $$$\\rightarrow 1$$$ Between $$$\\color{red}{ar}i$$$ and $$$\\color{red}{ar}ousal$$$ $$$\\rightarrow 2$$$ Between $$$\\color{red}{arou}sal$$$ and $$$\\color{red}{arou}nd$$$ $$$\\rightarrow 4$$$ Between $$$\\color{red}{ar}ound$$$ and $$$\\color{red}{ar}i$$$ $$$\\rightarrow 2$$$ "}, "positive_code": [{"source_code": "gets.to_i.times do\n res = 'a' * 200\n puts res\n gets.to_i\n gets.split.map(&:to_i).each do |pos|\n res[pos] = res[pos] == 'a' ? 'b' : 'a'\n puts res\n end\nend"}, {"source_code": "require 'set'; require 'prime'; require 'pp'\nINF=Float::INFINITY; MOD=10**9+7\nt = gets.chomp.to_i\nt.times do\n n = gets.chomp.to_i\n a = gets.chomp.split.map(&:to_i)\n\n s = 'a' * 100\n puts s\n a.each do |num|\n s = s[0, num] + ((s[num].ord + 1 - 97) % 26 + 97).chr * (100 - num)\n puts s\n end\nend\n"}, {"source_code": "def pin(a) \n a.each do |i| \n print((i%26+97).chr) \n end \n puts \"\" \nend\n\n# a = [1,2,3]\n# pin(a)\n\ntc = gets.to_i \n\ntc.times do \n n = gets.to_i \n arr = gets.split.map(&:to_i) \n\n a = Array.new(51) {0} \n pin(a) \n arr.each do |x| \n for i in x...51 \n a[i] += 1 \n end\n pin(a) \n end\nend"}], "negative_code": [{"source_code": "gets.to_i.times do\n res = 'a' * 200\n puts res\n gets.split.map(&:to_i).each do |pos|\n res[pos] = res[pos] == 'a' ? 'b' : 'a'\n puts res\n end\nend"}, {"source_code": "gets.to_i.times do\n res = \"a\" * 200\n puts res\n gets.to_i.times do\n pos = gets.to_i\n res[pos] = res[pos] == 'a' ? 'b' : 'a'\n puts res\n end\nend"}, {"source_code": "gets.to_i.times do\n res = 'a' * 200\n puts res\n gets.split.map(&:to_i).each do |pos|\n pos = gets.to_i\n res[pos] = res[pos] == 'a' ? 'b' : 'a'\n puts res\n end\nend"}], "src_uid": "6983823efdc512f8759203460cd6bb4c"} {"nl": {"description": "A non-empty string is called palindrome, if it reads the same from the left to the right and from the right to the left. For example, \"abcba\", \"a\", and \"abba\" are palindromes, while \"abab\" and \"xy\" are not.A string is called a substring of another string, if it can be obtained from that string by dropping some (possibly zero) number of characters from the beginning and from the end of it. For example, \"abc\", \"ab\", and \"c\" are substrings of the string \"abc\", while \"ac\" and \"d\" are not.Let's define a palindromic count of the string as the number of its substrings that are palindromes. For example, the palindromic count of the string \"aaa\" is $$$6$$$ because all its substrings are palindromes, and the palindromic count of the string \"abc\" is $$$3$$$ because only its substrings of length $$$1$$$ are palindromes.You are given a string $$$s$$$. You can arbitrarily rearrange its characters. You goal is to obtain a string with the maximum possible value of palindromic count.", "input_spec": "The first line contains an integer $$$n$$$ ($$$1 \\le n \\le 100\\,000$$$) — the length of string $$$s$$$. The second line contains string $$$s$$$ that consists of exactly $$$n$$$ lowercase characters of Latin alphabet.", "output_spec": "Print string $$$t$$$, which consists of the same set of characters (and each characters appears exactly the same number of times) as string $$$s$$$. Moreover, $$$t$$$ should have the maximum possible value of palindromic count among all such strings strings. If there are multiple such strings, print any of them.", "sample_inputs": ["5\noolol", "16\ngagadbcgghhchbdf"], "sample_outputs": ["ololo", "abccbaghghghgdfd"], "notes": "NoteIn the first example, string \"ololo\" has $$$9$$$ palindromic substrings: \"o\", \"l\", \"o\", \"l\", \"o\", \"olo\", \"lol\", \"olo\", \"ololo\". Note, that even though some substrings coincide, they are counted as many times as they appear in the resulting string.In the second example, the palindromic count of string \"abccbaghghghgdfd\" is $$$29$$$."}, "positive_code": [{"source_code": "gets\nputs gets.strip.chars.sort.join"}, {"source_code": "num_chars = gets.chomp.to_i\nchars = gets.chomp\n\nalphabets = \"abcdefghijklmnopqrstuvwxyz\"\n\nchar_counts = {};\n\nalphabets.each_char {|c| char_counts[c] = 0;}\n\nchars.each_char do |c|\n char_counts[c] = char_counts[c] + 1;\nend\n\noutput_string = \"\";\n\n\nwhile (output_string.length != num_chars) do\n sorted_array = char_counts.sort_by {|k,v| v}.reverse\n\n \n\n letter_1 = sorted_array[0][0];\n letter_2 = sorted_array[1][0];\n count_1 = sorted_array[0][1];\n count_2 = sorted_array[1][1];\n\n output_string += letter_1 * count_1;\n char_counts[letter_1] = 0;\n\n # if (count_1 > 0) \n # if (count_1 >= count_2 + 1) \n # output_string += \"#{letter_1}#{letter_2}\" * count_2\n # output_string += letter_1\n # char_counts[letter_1] -= count_2 + 1\n # char_counts[letter_2] = 0;\n # elsif (count_1 == count_2) \n # output_string += \"#{letter_1}#{letter_2}\" * count_2\n # char_counts[letter_1] = 0;\n # char_counts[letter_2] = 0;\n # end\n # end\nend\n\n\nputs output_string"}], "negative_code": [{"source_code": "num_chars = gets.chomp.to_i\nchars = gets.chomp\n\nalphabets = \"abcdefghijklmnopqrstuvwxyz\"\n\nchar_counts = {};\n\nalphabets.each_char {|c| char_counts[c] = 0;}\n\nchars.each_char do |c|\n char_counts[c] = char_counts[c] + 1;\nend\n\noutput_string = \"\";\n\n\nwhile (output_string.length != num_chars) do\n sorted_array = char_counts.sort_by {|k,v| v}.reverse\n\n letter_1 = sorted_array[0][0];\n letter_2 = sorted_array[1][0];\n count_1 = sorted_array[0][1];\n count_2 = sorted_array[1][1];\n\n if (count_1 > 0) \n if (count_1 >= count_2 + 1) \n output_string += \"#{letter_1}#{letter_2}\" * count_2\n output_string += letter_1\n char_counts[letter_1] -= count_2 + 1\n char_counts[letter_2] = 0;\n elsif (count_1 == count_2) \n output_string += \"#{letter_1}#{letter_2}\" * count_2\n char_counts[letter_1] = 0;\n char_counts[letter_2] = 0;\n end\n end\nend\n\n\nputs output_string"}], "src_uid": "8616ede6867c8aacde986a123ec8a921"} {"nl": {"description": "The ship crashed into a reef and is sinking. Now the entire crew must be evacuated. All n crew members have already lined up in a row (for convenience let's label them all from left to right with positive integers from 1 to n) and await further instructions. However, one should evacuate the crew properly, in a strict order. Specifically:The first crew members to leave the ship are rats. Then women and children (both groups have the same priority) leave the ship. After that all men are evacuated from the ship. The captain leaves the sinking ship last.If we cannot determine exactly who should leave the ship first for any two members of the crew by the rules from the previous paragraph, then the one who stands to the left in the line leaves the ship first (or in other words, the one whose number in the line is less).For each crew member we know his status as a crew member, and also his name. All crew members have different names. Determine the order in which to evacuate the crew.", "input_spec": "The first line contains an integer n, which is the number of people in the crew (1 ≤ n ≤ 100). Then follow n lines. The i-th of those lines contains two words — the name of the crew member who is i-th in line, and his status on the ship. The words are separated by exactly one space. There are no other spaces in the line. The names consist of Latin letters, the first letter is uppercase, the rest are lowercase. The length of any name is from 1 to 10 characters. The status can have the following values: rat for a rat, woman for a woman, child for a child, man for a man, captain for the captain. The crew contains exactly one captain.", "output_spec": "Print n lines. The i-th of them should contain the name of the crew member who must be the i-th one to leave the ship.", "sample_inputs": ["6\nJack captain\nAlice woman\nCharlie man\nTeddy rat\nBob child\nJulia woman"], "sample_outputs": ["Teddy\nAlice\nBob\nJulia\nCharlie\nJack"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\nr, w, m, c = [], [], [], []\nn.times do\n\ta, b = gets.split\n\tcase b\n\t\twhen 'rat'\n\t\t\tr << a\n\t\twhen 'child'\n\t\t\tw << a\n\t\twhen 'woman'\n\t\t\tw << a\n\t\twhen 'man'\n\t\t\tm << a\n\t\twhen 'captain'\n\t\t\tc << a\n\tend\nend\nputs (r + w + m + c) * \"\\n\"\n"}, {"source_code": "class Person\n attr_accessor :name, :priority, :number\n def initialize(name, type, number)\n @name = name\n @priority = case type\n when \"rat\" then 0\n when \"child\", \"woman\" then 1\n when \"man\" then 2\n when \"captain\" then 3\n end\n @number = number\n end\nend\n\nn = gets.to_i\nh = []\n\nn.times do |number|\n name = gets(\" \")\n type = gets.chomp\n h.push(Person.new(name, type, number))\nend\n\nh.sort! do |a, b|\n if (a.priority == b.priority)\n a.number <=> b.number\n else\n a.priority <=> b.priority\n end\nend\n\nh.each { |e| puts e.name }\n"}, {"source_code": "\nname=[]\ntype=[]\nn=gets.to_i\nfor i in 0..n-1\n input=gets.split\n name.push(input[0])\n type.push(input[1])\nend\nfor i in 0..n-1\n if type[i]==\"rat\" then\n puts name[i]\n end\nend\nfor i in 0..n-1\n if type[i]==\"child\" || type[i]==\"woman\" then\n puts name[i]\n end\nend\nfor i in 0..n-1\n if type[i]==\"man\" then\n puts name[i]\n end\nend\nfor i in 0..n-1\n if type[i]==\"captain\" then\n puts name[i]\n end\nend\n\n"}, {"source_code": "include Math\nrequire \"prime\"\nrequire \"bigdecimal\"\nrequire \"bigdecimal/math\"\nrequire \"matrix\"\nrequire \"pp\"\nrequire \"benchmark\"\n\nInfinity = 1/0.0\nclass Matrix;def inspect;empty? ? \"Matrix.empty(#{row_size}, #{column_size})\":\"Matrix[\\n#{@rows.map(&:inspect).join(\"\\n\")}\\n]\";end;end\ndef pbin(obj);case obj;when Integer;puts obj.to_s(2);end;end\nclass String;def to_a;split(\"\");end;end\nclass String;def to_ia;split.map(&:to_i);end;end\ndef getia;gets.to_ia;end\ndef geti;gets.to_i;end\nclass Object;def dcopy;Marshal.load(Marshal.dump(self));end;end\n\nn=geti\ns=[]\nn.times{\n s< [],\n 'woman_and_child' => [],\n 'man' => [],\n 'captain' => [],\n}\n\n1.upto(gets.to_i) do\n (name, type) = gets.strip.split(/\\s/)\n\n if ['rat', 'man', 'captain'].include?(type)\n members[type].push(name)\n else\n members['woman_and_child'].push(name)\n end\nend\n\nmembers['rat'].each do |name|\n puts name\nend\n\nmembers['woman_and_child'].each do |name|\n puts name\nend\n\nmembers['man'].each do |name|\n puts name\nend\n\nmembers['captain'].each do |name|\n puts name\nend\n"}, {"source_code": "gets p\nputs %w(rat child|woman man captain).map{|_|$_.scan /(.+) (?:#{_})/}\n"}, {"source_code": "members = []\nfor _ in 1..Integer(gets.chomp)\n members << gets.chomp.split\nend\ngrouped = members.inject(Hash.new { |h, k| h[k] = (k == 'child') ? h['woman'] : [] }) { |h, m| h[m[1]] <<= m[0]; h }\n%w(rat woman man captain).map.each { |c| puts grouped[c] }"}, {"source_code": "members = []\nfor _ in 1..Integer(gets.chomp)\n members << gets.chomp.split\nend\ngrouped = members.inject(Hash.new { |h, k| h[k] = (k == 'child') ? h['woman'] : [] }) { |h, m| h[m[1]] <<= m[0]; h }\nputs grouped['rat']\nputs grouped['woman']\nputs grouped['man']\nputs grouped['captain']"}, {"source_code": "members = []\nfor _ in 1..Integer(gets.chomp)\n members << gets.chomp.split\nend\ngrouped = members.inject(Hash.new { |h, k| h[k] = (k == 'child') ? h['woman'] : [] }) { |h, m| h[m[1]] <<= m[0]; h }\n%w(rat woman man captain).each { |c| puts grouped[c] }"}, {"source_code": "class Person\n attr_accessor :name, :priority, :number\n def initialize(name, type, number)\n @name = name\n @number = number\n case type\n when \"rat\"\n @priority = 0\n when \"woman\", \"child\"\n @priority = 1\n when \"man\"\n @priority = 2\n when \"captain\"\n @priority = 3\n end\n end\nend\n\nn = gets.to_i\nh = []\nn.times do |number|\n name = gets(\" \")\n type = gets.chomp\n h.push(Person.new(name, type, number))\nend\n\nh.sort! do |a, b| \n if (a.priority == b.priority)\n a.number <=> b.number\n else\n a.priority <=> b.priority\n end\nend\n\nh.each{|person| puts person.name}\n"}, {"source_code": "gets p\nputs %w(rat child|woman man captain).map{|_|$_.scan /(.+) (?:#{_})/}\n"}, {"source_code": "gets p\nputs %w(rat child|woman man captain).map{|_|$_.scan /(.+) (?:#{_})/}"}, {"source_code": "input = STDIN.read.split(\"\\n\").map {|i| i.split}; input.shift\n\nrt, cw, mn, ca,= [], [], [], []\n\ninput.each do |i|\n\tcase i.last\n\twhen 'captain'\n\t\tca.push i.first\n\twhen 'man'\n\t\tmn.push i.first\n\twhen 'woman'\n\t\tcw.push i.first\n\twhen 'child'\n\t\tcw.push i.first\n\twhen 'rat'\n\t\trt.push i.first\n\tend\nend\n\n[rt, cw, mn, ca].each.each {|i| puts i}"}, {"source_code": "input = STDIN.read.split(\"\\n\").map {|i| i.split}; input.shift\n\nrt, cw, mn, ca,= [], [], [], []\n\ninput.each do |i|\n\tcase i.last\n\twhen 'captain'\n\t\tca.push i.first\n\twhen 'man'\n\t\tmn.push i.first\n\twhen 'woman', 'child'\n\t\tcw.push i.first\n\twhen 'rat'\n\t\trt.push i.first\n\tend\nend\n\n[rt, cw, mn, ca].each.each {|i| puts i}"}, {"source_code": "gets p\nputs %w(rat child|woman man captain).map{|_|$_.scan /(.+) (?:#{_})/}\n"}], "negative_code": [{"source_code": "k = %w(rat child man captain)\ngets\nreadlines.map(&:chomp).map(&:split).each { |x| x[1] = \"child\" if x[1] == \"woman\" }.sort_by {|x| k.index(x[1]) }.each do |x|\n puts x[0]\nend"}, {"source_code": "t = %w(rat woman|child man captain)\nn, a = gets.to_i, []\nn.times { a << gets.scan(/\\w+/) }\na.sort_by { |x| t.index {|i| i =~ /([|]|^)#{x[1]}/} }.each { |x| puts x[0] }"}, {"source_code": "t = %w(rat child man captain)\ngets\na = gets(p).split.sort_by {|x| t.index(/ (\\w+)$/)}\np a\n"}, {"source_code": "class Person\n attr_accessor :name, :priority\n def initialize(name, type)\n @name = name\n case type\n when \"rat\"\n @priority = 0\n when \"woman\", \"child\"\n @priority = 1\n when \"man\"\n @priority = 2\n when \"captain\"\n @priority = 3\n end\n end\nend\n\nn = gets.to_i\nh = []\nn.times do\n name = gets(\" \")\n type = gets.chomp\n h.push(Person.new(name, type))\nend\n\nh.sort! {|a, b| a.priority <=> b.priority}\n\nh.each{|person| puts person.name}"}], "src_uid": "753113fa5130a67423f2e205c97f8017"} {"nl": {"description": "Permutation $$$p$$$ is a sequence of integers $$$p=[p_1, p_2, \\dots, p_n]$$$, consisting of $$$n$$$ distinct (unique) positive integers between $$$1$$$ and $$$n$$$, inclusive. For example, the following sequences are permutations: $$$[3, 4, 1, 2]$$$, $$$[1]$$$, $$$[1, 2]$$$. The following sequences are not permutations: $$$[0]$$$, $$$[1, 2, 1]$$$, $$$[2, 3]$$$, $$$[0, 1, 2]$$$.The important key is in the locked box that you need to open. To open the box you need to enter secret code. Secret code is a permutation $$$p$$$ of length $$$n$$$. You don't know this permutation, you only know the array $$$q$$$ of prefix maximums of this permutation. Formally: $$$q_1=p_1$$$, $$$q_2=\\max(p_1, p_2)$$$, $$$q_3=\\max(p_1, p_2,p_3)$$$, ... $$$q_n=\\max(p_1, p_2,\\dots,p_n)$$$. You want to construct any possible suitable permutation (i.e. any such permutation, that calculated $$$q$$$ for this permutation is equal to the given array).", "input_spec": "The first line contains integer number $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. The first line of a test case contains one integer $$$n$$$ $$$(1 \\le n \\le 10^{5})$$$ — the number of elements in the secret code permutation $$$p$$$. The second line of a test case contains $$$n$$$ integers $$$q_1, q_2, \\dots, q_n$$$ $$$(1 \\le q_i \\le n)$$$ — elements of the array $$$q$$$ for secret permutation. It is guaranteed that $$$q_i \\le q_{i+1}$$$ for all $$$i$$$ ($$$1 \\le i < n$$$). The sum of all values $$$n$$$ over all the test cases in the input doesn't exceed $$$10^5$$$.", "output_spec": "For each test case, print: If it's impossible to find such a permutation $$$p$$$, print \"-1\" (without quotes). Otherwise, print $$$n$$$ distinct integers $$$p_1, p_2, \\dots, p_n$$$ ($$$1 \\le p_i \\le n$$$). If there are multiple possible answers, you can print any of them. ", "sample_inputs": ["4\n5\n1 3 4 5 5\n4\n1 1 3 4\n2\n2 2\n1\n1"], "sample_outputs": ["1 3 4 5 2 \n-1\n2 1 \n1"], "notes": "NoteIn the first test case of the example answer $$$[1,3,4,5,2]$$$ is the only possible answer: $$$q_{1} = p_{1} = 1$$$; $$$q_{2} = \\max(p_{1}, p_{2}) = 3$$$; $$$q_{3} = \\max(p_{1}, p_{2}, p_{3}) = 4$$$; $$$q_{4} = \\max(p_{1}, p_{2}, p_{3}, p_{4}) = 5$$$; $$$q_{5} = \\max(p_{1}, p_{2}, p_{3}, p_{4}, p_{5}) = 5$$$. It can be proved that there are no answers for the second test case of the example."}, "positive_code": [{"source_code": "#!usr/bin/env ruby\n\n# https://codeforces.com/problemset/problem/1227/B\n\nn = gets.to_i\n\nn.times do\n size = gets.to_i\n input = gets.split(' ').map(&:to_i)\n used = Array.new(size, false)\n\n maxi = 0\n min = 0\n output = []\n failed = false\n\n input.each_with_index do |num, i|\n if num < i + 1\n puts \"-1\"\n failed = true\n break\n end\n\n maxi = [maxi, num].max\n \n if !used[maxi-1]\n output << maxi\n used[maxi-1] = true\n else\n while used[min] == true do min += 1 end\n output << min += 1\n end\n end\n puts output.join(' ') unless failed\nend"}, {"source_code": "gets.to_i.times do\n n = gets.to_i\n q = gets.split.map &:to_i\n p = [q[0]]*n\n u = 1\n used = [false]*(n + 1)\n used[q[0]] = true\n (1...n).each do |i|\n if q[i] < i + 1\n p = [-1]\n break\n end\n if q[i] == q[i - 1]\n u += 1 while used[u]\n p[i] = u\n used[u] = true\n u += 1\n else\n p[i] = q[i]\n used[q[i]] = true\n end\n end\n puts p.join ' '\nend\n"}], "negative_code": [{"source_code": "gets.to_i.times do\n n = gets.to_i\n q = gets.split.map &:to_i\n p = [q[0]]*n\n u = 1\n used = [false]*(n + 1)\n used[q[0]] = true\n (1...n).each do |i|\n if q[i] < i + 1\n p = [-1]\n break\n end\n if q[i] == q[i - 1]\n u += 1 while used[u]\n p[i] = u\n used[u] = true\n u += 1\n else\n p[i] = q[i]\n end\n end\n puts p.join ' '\nend\n"}], "src_uid": "81912dccb339d675e09df40919f9f6fe"} {"nl": {"description": "One day Sasha visited the farmer 2D and his famous magnetic farm. On this farm, the crop grows due to the influence of a special magnetic field. Maintaining of the magnetic field is provided by $$$n$$$ machines, and the power of the $$$i$$$-th machine is $$$a_i$$$. This year 2D decided to cultivate a new culture, but what exactly he didn't say. For the successful growth of the new culture, it is necessary to slightly change the powers of the machines. 2D can at most once choose an arbitrary integer $$$x$$$, then choose one machine and reduce the power of its machine by $$$x$$$ times, and at the same time increase the power of one another machine by $$$x$$$ times (powers of all the machines must stay positive integers). Note that he may not do that if he wants. More formally, 2D can choose two such indices $$$i$$$ and $$$j$$$, and one integer $$$x$$$ such that $$$x$$$ is a divisor of $$$a_i$$$, and change powers as following: $$$a_i = \\frac{a_i}{x}$$$, $$$a_j = a_j \\cdot x$$$Sasha is very curious, that's why he wants to calculate the minimum total power the farmer can reach. There are too many machines, and Sasha can't cope with computations, help him!", "input_spec": "The first line contains one integer $$$n$$$ ($$$2 \\le n \\le 5 \\cdot 10^4$$$) — the number of machines. The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le 100$$$) — the powers of the machines.", "output_spec": "Print one integer — minimum total power.", "sample_inputs": ["5\n1 2 3 4 5", "4\n4 2 4 4", "5\n2 4 2 3 7"], "sample_outputs": ["14", "14", "18"], "notes": "NoteIn the first example, the farmer can reduce the power of the $$$4$$$-th machine by $$$2$$$ times, and increase the power of the $$$1$$$-st machine by $$$2$$$ times, then the powers will be: $$$[2, 2, 3, 2, 5]$$$.In the second example, the farmer can reduce the power of the $$$3$$$-rd machine by $$$2$$$ times, and increase the power of the $$$2$$$-nd machine by $$$2$$$ times. At the same time, the farmer can leave is be as it is and the total power won't change.In the third example, it is optimal to leave it be as it is."}, "positive_code": [{"source_code": "require \"prime\"\n\nclass Integer\n def proper_divisors\n return [] if self == 1\n primes = prime_division.flat_map{|prime, freq| [prime] * freq}\n (1...primes.size).each_with_object([1]) do |n, res|\n primes.combination(n).map{ |combi| res << combi.inject(:*)}\n end.flatten.uniq\n end\nend\n\nn = gets.strip.to_i\na = gets.strip.split(' ').map(&:to_i).sort\nn = a.size\nmin = a.min\n\nres = []\na.uniq.each do |e|\n divs = e.proper_divisors\n next if divs.size.zero?\n divs.each { |d| res << [e, (e - (e / d)) - (min - (min * d)).abs] }\nend\n\nputs a.inject(:+) - (res.size.zero? ? 0 : res.map { |x| x[1] }.max)"}], "negative_code": [], "src_uid": "d8349ff9b695612473b2ba00d08e505b"} {"nl": {"description": "Yaroslav likes algorithms. We'll describe one of his favorite algorithms. The algorithm receives a string as the input. We denote this input string as a. The algorithm consists of some number of command. Сommand number i looks either as si >> wi, or as si <> wi, where si and wi are some possibly empty strings of length at most 7, consisting of digits and characters \"?\". At each iteration, the algorithm looks for a command with the minimum index i, such that si occurs in a as a substring. If this command is not found the algorithm terminates. Let's denote the number of the found command as k. In string a the first occurrence of the string sk is replaced by string wk. If the found command at that had form sk >> wk, then the algorithm continues its execution and proceeds to the next iteration. Otherwise, the algorithm terminates. The value of string a after algorithm termination is considered to be the output of the algorithm. Yaroslav has a set of n positive integers, he needs to come up with his favorite algorithm that will increase each of the given numbers by one. More formally, if we consider each number as a string representing the decimal representation of the number, then being run on each of these strings separately, the algorithm should receive the output string that is a recording of the corresponding number increased by one.Help Yaroslav.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 100) — the number of elements in the set. The next n lines contains one positive integer each. All the given numbers are less than 1025.", "output_spec": "Print the algorithm which can individually increase each number of the set. In the i-th line print the command number i without spaces. Your algorithm will be launched for each of these numbers. The answer will be considered correct if:   Each line will a correct algorithm command (see the description in the problem statement). The number of commands should not exceed 50. The algorithm will increase each of the given numbers by one. To get a respond, the algorithm will perform no more than 200 iterations for each number. ", "sample_inputs": ["2\n10\n79"], "sample_outputs": ["10<>11\n79<>80"], "notes": null}, "positive_code": [{"source_code": "9.times { |d| puts \"#{d}??<>#{d + 1}\" }\nputs \"9??>>??0\"\nputs \"??<>1\"\n10.times { |d| puts \"?#{d}>>#{d}?\" }\nputs \"?>>??\"\n10.times { |d| puts \"#{d}>>?#{d}\" }\n"}, {"source_code": "#!/usr/bin/env ruby\n\nputs <<-EOF\n9??>>??0\n0??<>1\n1??<>2\n2??<>3\n3??<>4\n4??<>5\n5??<>6\n6??<>7\n7??<>8\n8??<>9\n??<>1\n?0>>0?\n?1>>1?\n?2>>2?\n?3>>3?\n?4>>4?\n?5>>5?\n?6>>6?\n?7>>7?\n?8>>8?\n?9>>9?\n0?<>1\n1?<>2\n2?<>3\n3?<>4\n4?<>5\n5?<>6\n6?<>7\n7?<>8\n8?<>9\n9?>>9??\n0>>0?\n1>>1?\n2>>2?\n3>>3?\n4>>4?\n5>>5?\n6>>6?\n7>>7?\n8>>8?\n9>>9?\nEOF\n"}, {"source_code": "# コンストラクト\n\n(0..9).each{|x| print \"??\" , x , \">>\" , x ,\"??\\n\"}\n\nprint \"??>>?\\n\"\n\n(0...9).each{|x| print x , \"?<>\" , x+1 ,\"\\n\"}\n\nprint \"9?>>?0\\n?0<>10\\n>>??\\n\"\n"}], "negative_code": [{"source_code": "9.times { |d| puts \"#{d}??<>#{d + 1}\" }\nputs \"??<>1\"\nputs \"9??>>??0\"\nputs \"?>>??\"\n10.times { |d| puts \"?#{d}>>#{d}?\" }\n10.times { |d| puts \"#{d}>>?#{d}\" }\n"}, {"source_code": "#!/usr/bin/env ruby\n\nputs <<-EOF\n?0>>0?\n?1>>1?\n?2>>2?\n?3>>3?\n?4>>4?\n?5>>5?\n?6>>6?\n?7>>7?\n?8>>8?\n?9>>9?\n0?<>1\n1?<>2\n2?<>3\n3?<>4\n4?<>5\n5?<>6\n6?<>7\n7?<>8\n8?<>9\n9?>>9??\n9??>>??0\n0??<>1\n1??<>2\n2??<>3\n3??<>4\n4??<>5\n5??<>6\n6??<>7\n7??<>8\n8??<>9\n??<>1\n0>>0?\n1>>1?\n2>>2?\n3>>3?\n4>>4?\n5>>5?\n6>>6?\n7>>7?\n8>>8?\n9>>9?\nEOF\n"}, {"source_code": "#!/usr/bin/env ruby\n\nputs <<-EOF\n?0>>0?\n?1>>1?\n?2>>2?\n?3>>3?\n?4>>4?\n?5>>5?\n?6>>6?\n?7>>7?\n?8>>8?\n?9>>9?\n\n0?<>1\n1?<>2\n2?<>3\n3?<>4\n4?<>5\n5?<>6\n6?<>7\n7?<>8\n8?<>9\n\n9?>>9??\n\n9??>>??0\n\n0??<>1\n1??<>2\n2??<>3\n3??<>4\n4??<>5\n5??<>6\n6??<>7\n7??<>8\n8??<>9\n\n??<>1\n\n0>>0?\n1>>1?\n2>>2?\n3>>3?\n4>>4?\n5>>5?\n6>>6?\n7>>7?\n8>>8?\n9>>9?\nEOF\n"}], "src_uid": "90929863d289a475b766d5f2b0cd7c61"} {"nl": {"description": "Recently, you bought a brand new smart lamp with programming features. At first, you set up a schedule to the lamp. Every day it will turn power on at moment $$$0$$$ and turn power off at moment $$$M$$$. Moreover, the lamp allows you to set a program of switching its state (states are \"lights on\" and \"lights off\"). Unfortunately, some program is already installed into the lamp.The lamp allows only good programs. Good program can be represented as a non-empty array $$$a$$$, where $$$0 < a_1 < a_2 < \\dots < a_{|a|} < M$$$. All $$$a_i$$$ must be integers. Of course, preinstalled program is a good program.The lamp follows program $$$a$$$ in next manner: at moment $$$0$$$ turns power and light on. Then at moment $$$a_i$$$ the lamp flips its state to opposite (if it was lit, it turns off, and vice versa). The state of the lamp flips instantly: for example, if you turn the light off at moment $$$1$$$ and then do nothing, the total time when the lamp is lit will be $$$1$$$. Finally, at moment $$$M$$$ the lamp is turning its power off regardless of its state.Since you are not among those people who read instructions, and you don't understand the language it's written in, you realize (after some testing) the only possible way to alter the preinstalled program. You can insert at most one element into the program $$$a$$$, so it still should be a good program after alteration. Insertion can be done between any pair of consecutive elements of $$$a$$$, or even at the begining or at the end of $$$a$$$.Find such a way to alter the program that the total time when the lamp is lit is maximum possible. Maybe you should leave program untouched. If the lamp is lit from $$$x$$$ till moment $$$y$$$, then its lit for $$$y - x$$$ units of time. Segments of time when the lamp is lit are summed up.", "input_spec": "First line contains two space separated integers $$$n$$$ and $$$M$$$ ($$$1 \\le n \\le 10^5$$$, $$$2 \\le M \\le 10^9$$$) — the length of program $$$a$$$ and the moment when power turns off. Second line contains $$$n$$$ space separated integers $$$a_1, a_2, \\dots, a_n$$$ ($$$0 < a_1 < a_2 < \\dots < a_n < M$$$) — initially installed program $$$a$$$.", "output_spec": "Print the only integer — maximum possible total time when the lamp is lit.", "sample_inputs": ["3 10\n4 6 7", "2 12\n1 10", "2 7\n3 4"], "sample_outputs": ["8", "9", "6"], "notes": "NoteIn the first example, one of possible optimal solutions is to insert value $$$x = 3$$$ before $$$a_1$$$, so program will be $$$[3, 4, 6, 7]$$$ and time of lamp being lit equals $$$(3 - 0) + (6 - 4) + (10 - 7) = 8$$$. Other possible solution is to insert $$$x = 5$$$ in appropriate place.In the second example, there is only one optimal solution: to insert $$$x = 2$$$ between $$$a_1$$$ and $$$a_2$$$. Program will become $$$[1, 2, 10]$$$, and answer will be $$$(1 - 0) + (10 - 2) = 9$$$.In the third example, optimal answer is to leave program untouched, so answer will be $$$(3 - 0) + (7 - 4) = 6$$$."}, "positive_code": [{"source_code": "N, M = gets.split.map(&:to_i)\nas = [0] + gets.split.map(&:to_i) + [M]\n\nsum1 = Array.new(N+2, 0)\nsum2 = Array.new(N+2, 0)\n\n(1..(N+1)).each do |i|\n sum1[i] += sum1[i-1]\n sum2[i] += sum2[i-1]\n if i%2 == 0\n sum1[i] += as[i] - as[i-1]\n else\n sum2[i] += as[i] - as[i-1]\n end\nend\n\nans = sum2[-1]\n(1..N).each do |i|\n next if as[i+1] - as[i-1] == 2\n result = sum2[i] - 1 + sum1[N+1] - sum1[i]\n ans = result if ans < result\nend\nputs ans"}], "negative_code": [], "src_uid": "085b03a45fec13b759c3bd334888caf5"} {"nl": {"description": "Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.Consider a set consisting of k (0 ≤ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).", "input_spec": "The first line contains an integer n (2  ≤ n ≤ 105) — the number of tree vertices. The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 ≤ pi ≤ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1. The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.", "output_spec": "Output a single integer — the number of ways to split the tree modulo 1000000007 (109 + 7).", "sample_inputs": ["3\n0 0\n0 1 1", "6\n0 1 1 0 4\n1 1 0 0 1 0", "10\n0 1 2 1 4 4 4 0 8\n0 0 0 1 0 1 1 0 0 1"], "sample_outputs": ["2", "1", "27"], "notes": null}, "positive_code": [{"source_code": "n=gets.to_i\ndp=[[],[]]\narr=[0]\nMof=1000000007\narr.concat(gets.split(/ /).map(&:to_i))\ngets.split(/ /).map(&:to_i).each_with_index do |i,id|\n dp[i][id]=1\n dp[1-i][id]=0\nend\n(1..n-1).reverse_each do |i|\n dp[1][arr[i]]=(dp[1][arr[i]]*(dp[1][i]+dp[0][i])+dp[0][arr[i]]*dp[1][i])%Mof\n dp[0][arr[i]]=dp[0][arr[i]]*(dp[0][i]+dp[1][i])%Mof\nend\nputs dp[1][0]"}], "negative_code": [], "src_uid": "e571191fadf6b0b26bd2f16295f32077"} {"nl": {"description": "You are given two integers $$$a$$$ and $$$b$$$. You may perform any number of operations on them (possibly zero).During each operation you should choose any positive integer $$$x$$$ and set $$$a := a - x$$$, $$$b := b - 2x$$$ or $$$a := a - 2x$$$, $$$b := b - x$$$. Note that you may choose different values of $$$x$$$ in different operations.Is it possible to make $$$a$$$ and $$$b$$$ equal to $$$0$$$ simultaneously?Your program should answer $$$t$$$ independent test cases.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. Then the test cases follow, each test case is represented by one line containing two integers $$$a$$$ and $$$b$$$ for this test case ($$$0 \\le a, b \\le 10^9$$$).", "output_spec": "For each test case print the answer to it — YES if it is possible to make $$$a$$$ and $$$b$$$ equal to $$$0$$$ simultaneously, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).", "sample_inputs": ["3\n6 9\n1 1\n1 2"], "sample_outputs": ["YES\nNO\nYES"], "notes": "NoteIn the first test case of the example two operations can be used to make both $$$a$$$ and $$$b$$$ equal to zero: choose $$$x = 4$$$ and set $$$a := a - x$$$, $$$b := b - 2x$$$. Then $$$a = 6 - 4 = 2$$$, $$$b = 9 - 8 = 1$$$; choose $$$x = 1$$$ and set $$$a := a - 2x$$$, $$$b := b - x$$$. Then $$$a = 2 - 2 = 0$$$, $$$b = 1 - 1 = 0$$$. "}, "positive_code": [{"source_code": "#!usr/bin/env ruby\n\n# https://codeforces.com/problemset/problem/1260/B\n\nn = gets.to_i\n\nn.times do\n input = gets.split(' ').map(&:to_i)\n a = input.max\n b = input.min\n\n if b < a/2.0 \n puts \"NO\" \n elsif (a % 3 + b % 3) % 3 == 0\n puts \"YES\"\n else\n puts \"NO\"\n end\nend"}, {"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\nrequire 'prime'\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\ndef big(a,b) return (a>b)? a:b end\n\ndef small(a,b) return (a 2*b or b > 2*a or (a+b)%3 != 0)\n puts \"NO\"\n next\n end\n sa = (a-b).abs\n min = [a,b].min\n max = [a,b].max\n a = min - sa\n b = max - sa*2\n if(a != b)\n min = [a,b].min\n max = [a,b].max\n a = min - 1\n end\n if(a%3 == 0)\n puts \"YES\"\n else\n puts \"NO\"\n end\nend"}], "negative_code": [{"source_code": "#!usr/bin/env ruby\n\n# https://codeforces.com/problemset/problem/1260/B\n\nn = gets.to_i\n\nn.times do\n input = gets.split(' ').map(&:to_i)\n a = input.max\n b = input.min\n\n if b < a/2.0\n puts \"NO\"\n break\n end\n\n if (a % 3 + b % 3) % 3 == 0\n puts \"YES\"\n else\n puts \"NO\"\n end\nend"}, {"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\nrequire 'prime'\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\ndef big(a,b) return (a>b)? a:b end\n\ndef small(a,b) return (a 2*b or b > 2*a)\n puts \"NO\"\n exit\n end\n puts ((a+b)%3==0)? \"YES\" : \"NO\"\nend"}], "src_uid": "0a720a0b06314fde783866b47f35af81"} {"nl": {"description": "You have a sequence of $$$n$$$ colored blocks. The color of the $$$i$$$-th block is $$$c_i$$$, an integer between $$$1$$$ and $$$n$$$.You will place the blocks down in sequence on an infinite coordinate grid in the following way. Initially, you place block $$$1$$$ at $$$(0, 0)$$$. For $$$2 \\le i \\le n$$$, if the $$$(i - 1)$$$-th block is placed at position $$$(x, y)$$$, then the $$$i$$$-th block can be placed at one of positions $$$(x + 1, y)$$$, $$$(x - 1, y)$$$, $$$(x, y + 1)$$$ (but not at position $$$(x, y - 1)$$$), as long no previous block was placed at that position. A tower is formed by $$$s$$$ blocks such that they are placed at positions $$$(x, y), (x, y + 1), \\ldots, (x, y + s - 1)$$$ for some position $$$(x, y)$$$ and integer $$$s$$$. The size of the tower is $$$s$$$, the number of blocks in it. A tower of color $$$r$$$ is a tower such that all blocks in it have the color $$$r$$$.For each color $$$r$$$ from $$$1$$$ to $$$n$$$, solve the following problem independently: Find the maximum size of a tower of color $$$r$$$ that you can form by placing down the blocks according to the rules. ", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$c_1, c_2, \\ldots, c_n$$$ ($$$1 \\le c_i \\le n$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, output $$$n$$$ integers. The $$$r$$$-th of them should be the maximum size of an tower of color $$$r$$$ you can form by following the given rules. If you cannot form any tower of color $$$r$$$, the $$$r$$$-th integer should be $$$0$$$.", "sample_inputs": ["6\n\n7\n\n1 2 3 1 2 3 1\n\n6\n\n4 2 2 2 4 4\n\n1\n\n1\n\n5\n\n5 4 5 3 5\n\n6\n\n3 3 3 1 3 3\n\n8\n\n1 2 3 4 4 3 2 1"], "sample_outputs": ["3 2 2 0 0 0 0 \n0 3 0 2 0 0 \n1 \n0 0 1 1 1 \n1 0 4 0 0 0 \n2 2 2 2 0 0 0 0"], "notes": "NoteIn the first test case, one of the possible ways to form a tower of color $$$1$$$ and size $$$3$$$ is: place block $$$1$$$ at position $$$(0, 0)$$$; place block $$$2$$$ to the right of block $$$1$$$, at position $$$(1, 0)$$$; place block $$$3$$$ above block $$$2$$$, at position $$$(1, 1)$$$; place block $$$4$$$ to the left of block $$$3$$$, at position $$$(0, 1)$$$; place block $$$5$$$ to the left of block $$$4$$$, at position $$$(-1, 1)$$$; place block $$$6$$$ above block $$$5$$$, at position $$$(-1, 2)$$$; place block $$$7$$$ to the right of block $$$6$$$, at position $$$(0, 2)$$$. The blocks at positions $$$(0, 0)$$$, $$$(0, 1)$$$, and $$$(0, 2)$$$ all have color $$$1$$$, forming an tower of size $$$3$$$.In the second test case, note that the following placement is not valid, since you are not allowed to place block $$$6$$$ under block $$$5$$$: It can be shown that it is impossible to form a tower of color $$$4$$$ and size $$$3$$$."}, "positive_code": [{"source_code": "def main\n t = inp.to_i\n t.times do |i|\n n = inp.to_i\n a = mapint(inarr)\n g = Array.new(n) { Array.new(0)}\n n.times do |i|\n g[a[i] - 1].push i\n end\n\n n.times do |i|\n if g[i].length == 0\n print 0\n else\n len1 = 1\n pre = g[i][0]\n\n fsk = -1\n 1.upto(g[i].length - 1) do |j|\n if (g[i][j] - pre) & 1 == 1\n len1 += 1\n pre = g[i][j]\n else\n if fsk == -1\n fsk = j\n end\n end\n end\n\n len2 = 1\n if fsk != -1\n pre = g[i][fsk]\n (fsk + 1).upto(g[i].length - 1) do |j|\n if (g[i][j] - pre) & 1 == 1\n len2 += 1\n pre = g[i][j]\n end\n end\n end\n\n print max(len1, len2)\n end\n print \" \" if i + 1 != n\n end\n puts \"\"\n\n\n end\n\nend\n\n# lib\ndef inp\n gets.chomp\nend\n\ndef inarr\n gets.chomp.split\nend\n\ndef mapint(lst)\n lst.map(&:to_i)\nend\n\ndef max(a, b)\n (a > b)? a:b\nend\n\ndef min(a, b)\n (a < b)? a:b\nend\n\nmain"}], "negative_code": [], "src_uid": "c63640fd70e0268f03cb4eec18540f3a"} {"nl": {"description": "You are given two even integers $$$n$$$ and $$$m$$$. Your task is to find any binary matrix $$$a$$$ with $$$n$$$ rows and $$$m$$$ columns where every cell $$$(i,j)$$$ has exactly two neighbours with a different value than $$$a_{i,j}$$$.Two cells in the matrix are considered neighbours if and only if they share a side. More formally, the neighbours of cell $$$(x,y)$$$ are: $$$(x-1,y)$$$, $$$(x,y+1)$$$, $$$(x+1,y)$$$ and $$$(x,y-1)$$$.It can be proven that under the given constraints, an answer always exists.", "input_spec": "Each test contains multiple test cases. The first line of input contains a single integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. The following lines contain the descriptions of the test cases. The only line of each test case contains two even integers $$$n$$$ and $$$m$$$ ($$$2 \\le n,m \\le 50$$$) — the height and width of the binary matrix, respectively.", "output_spec": "For each test case, print $$$n$$$ lines, each of which contains $$$m$$$ numbers, equal to $$$0$$$ or $$$1$$$ — any binary matrix which satisfies the constraints described in the statement. It can be proven that under the given constraints, an answer always exists.", "sample_inputs": ["3\n\n2 4\n\n2 2\n\n4 4"], "sample_outputs": ["1 0 0 1\n0 1 1 0\n1 0\n0 1\n1 0 1 0\n0 0 1 1\n1 1 0 0\n0 1 0 1"], "notes": "NoteWhite means $$$0$$$, black means $$$1$$$. The binary matrix from the first test caseThe binary matrix from the second test caseThe binary matrix from the third test case "}, "positive_code": [{"source_code": "step = ->xs {\r\n y = xs.map { |x| x + x.reverse }\r\n y + y.reverse\r\n}\r\n\r\nmatrix = [[1, 0], [0, 1]]\r\n5.times do\r\n matrix = step[matrix]\r\nend\r\n\r\ngets.to_i.times do\r\n n, m = gets.split.map(&:to_i)\r\n puts matrix.take(n).map { |row| row.take(m).join(\" \") }.join(\"\\n\")\r\nend\r\n"}, {"source_code": "t = gets.to_i\nt.times {\n r, c = gets.split.map &:to_i\n for p in (0...r/2)\n stuff = []\n stuff2 = []\n for q in (0...c/2)\n parity = (p + q) % 2\n stuff.push(parity)\n stuff.push((parity + 1) % 2)\n stuff2.push((parity + 1) % 2)\n stuff2.push(parity)\n end\n puts stuff.join(\" \")\n puts stuff2.join(\" \")\n end\n}\n"}], "negative_code": [], "src_uid": "b7d40e7fc4f277cc801b59a21a16dfc1"} {"nl": {"description": "After you had helped George and Alex to move in the dorm, they went to help their friend Fedor play a new computer game «Call of Soldiers 3».The game has (m + 1) players and n types of soldiers in total. Players «Call of Soldiers 3» are numbered form 1 to (m + 1). Types of soldiers are numbered from 0 to n - 1. Each player has an army. Army of the i-th player can be described by non-negative integer xi. Consider binary representation of xi: if the j-th bit of number xi equal to one, then the army of the i-th player has soldiers of the j-th type. Fedor is the (m + 1)-th player of the game. He assume that two players can become friends if their armies differ in at most k types of soldiers (in other words, binary representations of the corresponding numbers differ in at most k bits). Help Fedor and count how many players can become his friends.", "input_spec": "The first line contains three integers n, m, k (1 ≤ k ≤ n ≤ 20; 1 ≤ m ≤ 1000). The i-th of the next (m + 1) lines contains a single integer xi (1 ≤ xi ≤ 2n - 1), that describes the i-th player's army. We remind you that Fedor is the (m + 1)-th player.", "output_spec": "Print a single integer — the number of Fedor's potential friends.", "sample_inputs": ["7 3 1\n8\n5\n111\n17", "3 3 3\n1\n2\n3\n4"], "sample_outputs": ["0", "3"], "notes": null}, "positive_code": [{"source_code": "def byte_count(num = 0)\n c = 0\n while true\n if (num - (num.div(2) * 2)) == 1\n c += 1\n end\n num = num.div(2)\n if num < 2\n c += 1\n break\n end\n end\n \n return c\nend\n\nnums = []\narr = []\n\nc = 0\nmain = 0\n\ngets.split(\" \").each {|a| nums << a.to_i}\n\nfor i in (0..nums[1] - 1)\n arr[i] = gets.to_i\nend\n\nmain = gets.to_i\n\nfor i in (0..nums[1] - 1)\n if byte_count(arr[i] ^ main) <= nums[2]\n c += 1\n end\nend\n\nputs c"}, {"source_code": "n, m, k = gets.chomp.split(' ').map {|x| x.to_i}\narr = []\n(m).times do\n arr << gets.chomp.to_i\nend\nfedor = gets.chomp.to_i\ntotal = 0\narr.each do |x|\n s = (fedor ^ x).to_s(2)\n count = 0;\n (0...s.length).each { |i| count += 1 if s[i]=='1' }\n total += 1 if count<=k\nend\nputs total"}, {"source_code": "#!/usr/bin/ruby\n\nn, m, k = STDIN.readline.split.map {|s| s.to_i }\nplayers = []\nm.times { players.push STDIN.readline.to_i }\nfedor = STDIN.readline.to_i\ncount = 0\nplayers.each do |player|\n\tbits = (player ^ fedor)\n\tdiff = 0\n\twhile bits != 0\n\t\tdiff += (bits & 1)\n\t\tbits >>= 1\n\tend\n\tcount += 1 if diff <= k\nend\nputs count\n"}, {"source_code": "n,m,k=gets.chomp.split.map(&:to_i)\nar,ans=[],0\n(m+1).times {ar<0 || b>0\n\t\tc+=1 if a&1 != b&1\n\t\ta/=2\n\t\tb/=2\n\tend\n\tans+=1 if c<=k\nend\nputs ans\n"}, {"source_code": "require 'pp'\nn,m,k= STDIN.gets.chomp.split(/ /).map(&:to_i)\n\n\nans = 0\nxs = []\nm.times do |i|\n xs.push STDIN.gets.chomp.to_i\nend\n\nfedor = STDIN.gets.chomp.to_i\n\nxs.each do |x|\n xor = x ^ fedor\n ans += 1 if xor.to_s(2).split(//).select{|e| e == \"1\"}.count <= k\nend\n\nputs ans\n"}, {"source_code": "input = STDIN.read.split(\"\\n\")\nn, m, k = *input[0].split(' ').map(&:to_i)\nplayers = input[1..m].map(&:to_i)\nfedya = input[m + 1].to_i\n\nprint players[0..m].count { |p| (p ^ fedya).to_s(2).chars.map(&:to_i).inject(:+) <= k }"}, {"source_code": "n, m, k = gets.strip.split(\" \").map(&:to_i)\narmies = []\n(m + 1).times do |i|\n armies[i] = gets.strip.to_i\nend\n# puts armies\nnumber_of_friends = 0\nm.times do |i|\n num = armies[i] ^ armies[m]\n # puts num.to_s(2) \n count = 0\n while num != 0 do \n count += 1 if num & 1 == 1 \n num = num >> 1\n # puts num.to_s(2)\n end\n number_of_friends += 1 if count <= k\nend\nputs number_of_friends"}, {"source_code": "#http://codeforces.com/contest/467/B\n\n\nn,m, k = gets.split.map(&:to_i)\narr = []\nm.times do \n arr += [gets.to_i]\nend \nfedor = gets.to_i \n ans =0 \narr.each do |val| \n bits= fedor ^ val # to find how much differences are there..... \n if bits.to_s(2).count('1') <= k # counting 1 shows how much the two bits differ ..... \n ans +=1 \n end \nend \n\np ans "}, {"source_code": "n,m,k = gets.split\narr=[];\nwhile a=gets do\n\tarr.push(a.to_i)\nend\nc=arr.pop\nA=0\narr.each{|x| A=A+(((x^c).to_s(2).count('1') > k.to_i)?'0':'1').to_i}\nputs A"}, {"source_code": "n,m,k=gets.split.map{|e| e.to_i}\narr=[]\nm.times{\n\tarr<0\n\t\tn &= (n-1)\n\t\tset += 1\n\tend\n\tcount += 1 if set <= k\nend\n\nputs count"}, {"source_code": "n, m, k = gets.split.map(&:to_i)\na = m.times.map{gets.to_i}\nme = gets.to_i\nans = 0\na.each do |x|\n ans += 1 if (x ^ me).to_s(2).split(\"\").count('1') <= k\nend\nputs ans\n"}, {"source_code": "def nbits(n)\n nb = 0\n while n > 0\n nb += n%2\n n /= 2\n end\n nb\nend\n\nn, m, k = gets.split.map{|x| x.to_i}\nx = []\n(m + 1).times{x << gets.to_i}\nf = 0\n(0..m-1).each{|i| f += 1 if nbits(x[i]^x[m]) <= k}\nputs f\n"}, {"source_code": "n, m, k = gets.split(' ').map(&:to_i)\n\na = 1.upto(m).map { gets.to_i }\nx = gets.to_i\n\nputs a.count { |y| (x ^ y).to_s(2).count('1') <= k }\n"}, {"source_code": "n, m, k = gets.split.map(&:to_i)\na = (0..m).map { |_| gets.to_i }\nputs a[0..-2].select { |x| (x ^ a[-1]).to_s(2).count('1') <= k }.size"}, {"source_code": "n, m, k = gets.split.map(&:to_i)\nxs = []\nmax = 0\nm.times do\n t = gets.to_i\n xs << t\nend\nx = gets.to_i\ncount = 0\nxs.each do |i|\n c = (i ^ x).to_s(2).split(\"\").map(&:to_i).inject{|sum,x| sum + x }\n count += 1 if c <= k\nend\n\nputs count\n\n\n"}, {"source_code": "n,m,k = gets.split\narr=[];\nwhile a=gets do\n\tarr.push(a.to_i)\nend\nc=arr.pop\nA=0\narr.each{|x| A=A+(((x^c).to_s(2).count('1') > k.to_i)?'0':'1').to_i}\nputs A"}, {"source_code": "class Players\n attr_reader :x\n def initialize(x)\n @x = x\n end\n\n def bitnum(i)\n n = 0\n while i > 0\n n += i % 2\n i >>= 1\n end\n return n\n end\n\n def friend?(k, other)\n return bitnum(@x ^ other.x) <= k\n end\nend\n\nn, m, k = gets.split.map(&:to_i)\nlist = []\nm.times do\n list << Players.new(gets.to_i)\nend\nmy = Players.new(gets.to_i)\nfnum = 0\nlist.each do |e|\n if my.friend?(k, e)\n fnum += 1\n end\nend\nputs fnum\n"}], "negative_code": [{"source_code": "def byte_count(num = 0)\n c = 0\n s = \"\"\n while true\n if \"1\" == (num - (num.div(2) * 2)).to_s\n c += 1\n end\n num = num.div(2)\n if num < 2\n break\n end\n end\n \n return c\nend\n\nnums = []\narr = []\n\nc = 0\nmain = 0\n\ngets.split(\" \").each {|a| nums << a.to_i}\n\nfor i in (0..nums[1] - 1)\n arr[i] = gets.to_i\nend\n\nmain = gets.to_i\n\nfor i in (0..nums[1] - 1)\n if byte_count(arr[i] ^ main) <= nums[2]\n c += 1\n end\nend\n\nputs c"}, {"source_code": "def byte_count(num = 0)\n c = 0\n s = \"\"\n while true\n if \"1\" == (num - (num.div(2) * 2)).to_s + s\n c += 1\n end\n num = num.div(2)\n if num < 2\n break\n end\n end\n \n return c\nend\n\nnums = []\narr = []\n\nc = 0\nmain = 0\n\ngets.split(\" \").each {|a| nums << a.to_i}\n\nfor i in (0..nums[1] - 1)\n arr[i] = gets.to_i\nend\n\nmain = gets.to_i\n\nfor i in (0..nums[1] - 1)\n if byte_count(arr[i] ^ main) <= nums[2]\n c += 1\n end\nend\n\nputs c"}, {"source_code": "def byte_count(num = 0)\n c = 0\n s = \"\"\n while true\n s = (num - (num.div(2) * 2)).to_s + s\n num = num.div(2)\n if num < 2\n s = num.to_s + s\n break\n end\n end\n \n for i in (0..s.length - 1)\n if s[i] = \"1\"\n c += 1\n end\n end\n \n return c\nend\n\nnums = []\narr = []\n\nc = 0\nmain = 0\n\ngets.split(\" \").each {|a| nums << a.to_i}\n\nfor i in (0..nums[1] - 1)\n arr[i] = gets.to_i\nend\n\nmain = gets.to_i\n\nfor i in (0..nums[1] - 1)\n if byte_count(arr[i] ^ main) <= nums[2]\n c += 1\n end\nend\n\nputs c"}, {"source_code": "def byte_count(num = 0)\n c = 0\n s = \"\"\n while true\n s = (num - (num.div(2) * 2)).to_s + s\n num = num.div(2)\n if num < 2\n s = num.to_s + s\n break\n end\n end\n \n for i in (0..s.length - 1)\n if s[i] = \"1\"\n c += 1\n end\n end\n \n return c\nend\n\nnums = []\narr = []\n\nc = 0\nmain = 0\n\ngets.split(\" \").each {|a| nums << a.to_i}\n\nfor i in (0..nums[1] - 1)\n arr[i] = gets.to_i\nend\n\nmain = gets.to_i\n\nfor i in (0..nums[1] - 1)\n if byte_count(arr[i] ^ main) <= nums[2]\n c += 1\n end\nend\n\nputs c"}, {"source_code": "n,m,k=gets.chomp.split.map(&:to_i)\nar,ans=[],0\n(m+1).times {ar<0 || b>0\n\t\tc+=1 if a%2!=b%2\n\t\ta/=2\n\t\tb/=2\n\tend\n\tans+=1 if c<=k\nend\nputs ans\n"}, {"source_code": "require 'pp'\nn,m,k= STDIN.gets.chomp.split(/ /).map(&:to_i)\n\n\nans = 0\nxs = []\nm.times do |i|\n xs.push STDIN.gets.chomp.to_i.to_s(2).split(//).reverse\nend\n\nfedor = STDIN.gets.chomp.to_i.to_s(2).split(//).reverse\n\nxs.each do |x|\n kake = fedor & x\n ans += 1 if kake.count <= k\nend\n\nputs ans\n"}, {"source_code": "require 'pp'\nn,m,k= STDIN.gets.chomp.split(/ /).map(&:to_i)\n\n\nans = 0\nxs = []\nm.times do |i|\n xs.push STDIN.gets.chomp.to_i.to_s(2).split(//).reverse\nend\n\nfedor = STDIN.gets.chomp.to_i.to_s(2).split(//).reverse\n\nxs.each do |x|\n c = [fedor.count, x.count].min\n diff = (fedor.count - x.count).abs\n c.times do |i|\n if fedor[i] != x[i]\n diff += 1\n break\n end\n end\n ans += 1 if diff <= k\nend\n\nputs ans\n"}, {"source_code": "n,m,k = gets.split.map(&:to_i)\na = []\nr = 0\n(m + 1).times do |i|\n\ta[i] = gets.to_i\nend\n0.upto(m - 1) do |i| \n\tif [ a[m] , a[i] ].max - [ a[m] , a[i]].min <= k\n\t\tr += 1\n\tend\nend\np r"}, {"source_code": "#!/usr/bin/env ruby\n# coding: utf-8\n\ndef binarize(num)\n str = num.to_s(2)\n str.chars.reverse + ['0']*(@n-str.size)\nend\n\n\n@n, m, k = readline.chomp.split(/\\s+/).map(&:to_i)\nary = []\nm.times do\n ary << binarize(readline.to_i)\nend\nme = binarize(readline.to_i)\nc = 0\nary.each do |x|\n f = 0\n x.each_with_index { |i, v| f += 1 if i == me[v] }\n c += 1 if f <= k\nend\n\nputs c\n"}, {"source_code": "#!/usr/bin/env ruby\n# coding: utf-8\n\ndef binarize(num)\n str = num.to_s(2)\n str.chars.reverse + ['0']*(@n-str.size)\nend\n\n\n@n, m, k = readline.chomp.split(/\\s+/).map(&:to_i)\nary = []\nm.times do\n ary << binarize(readline.to_i)\nend\np me = binarize(readline.to_i)\nc = 0\nary.each do |x|\n f = 0\n x.each_with_index { |i, v| f += 1 if i != me[v] }\n if f <= k\n c += 1\n end\nend\n\nputs c\n"}, {"source_code": "n, m, k = gets.split.map(&:to_i)\na = m.times.map{gets.to_i.to_s(2).chars.reverse}\nme = gets.to_i.to_s(2).chars.reverse\nans = 0\na.each do |x|\n diff = 0\n [x.size, me.size].min.times do |i|\n diff += 1 if x[i] != me[i]\n end\n ans += 1 if diff <= k\nend\nputs ans\n\n"}, {"source_code": "n, m, k = gets.split.map(&:to_i)\na = m.times.map{gets.to_i.to_s(2).chars.reverse}\nme = gets.to_i.to_s(2).chars.reverse\nans = 0\na.each do |x|\n diff = 0\n [x.size, me.size].max.times do |i|\n if (i >= [x.size, me.size].min && (x[i] == 1 || me[i] == 1)) || x[i] != me[i]\n diff += 1\n end\n end\n ans += 1 if diff <= k\nend\nputs ans\n"}, {"source_code": "n, m, k = gets.split.map(&:to_i)\na = m.times.map{gets.to_i.to_s(2).chars.reverse}\nme = gets.to_i.to_s(2).chars.reverse\nans = 0\na.each do |x|\n diff = 0\n [x.size, me.size].max.times do |i|\n diff += 1 if x[i] != me[i]\n end\n ans += 1 if diff <= k\nend\nputs ans\n\n"}, {"source_code": "n, m, k = gets.split.map(&:to_i)\na = m.times.map{gets.to_i.to_s(2).chars.reverse}\nme = gets.to_i.to_s(2).chars.reverse\nans = 0\na.each do |x|\n diff = 0\n [x.size, me.size].min.times do |i|\n diff += 1 if x[i] != me[i]\n end\n diff += (x.size - me.size).abs\n ans += 1 if diff <= k\nend\nputs ans\n\n"}, {"source_code": "def nbits(n)\n nb = 0\n while n > 0\n nb += n%2\n n /= 2\n end\n nb\nend\n\nn, m, k = gets.split.map{|x| x.to_i}\nx = []\n(m + 1).times{x << gets.to_i}\nf = 0\n(0..m-1).each{|i| f += 1 if n - nbits(x[i]&x[m]) <= k}\nputs f\n"}, {"source_code": "n, m, k = gets.split.map(&:to_i)\nxs = []\nmax = 0\nm.times do\n t = gets[0..n].to_i.to_s(2)\n max = [t.length, max].max\n xs << t\nend\n\nx = gets[0..n].to_i.to_s(2)\nmax = [max, x.length].max\nx.ljust(max, \"0\")\n\nx = x.split(\"\")\nxs.map! do |r|\n r.ljust(max, \"0\").split(\"\")\nend\n\ncount = 0\nxs.each do |i|\n c = 0\n i.each_with_index do |p, j|\n c += 1 if x[j] == p\n end\n\n count += 1 if c <= k && c > 0\nend\n\nputs count\n\n\n"}, {"source_code": "n, m, k = gets.split.map(&:to_i)\nxs = []\nmax = 0\nm.times do\n t = gets.to_i\n xs << t\nend\nx = gets.to_i\ncount = 0\nxs.each do |i|\n c = (i ^ x)\n c = c.to_s(2).split(\"\").map(&:to_i).inject{|sum,x| sum + x }\n break\n count += 1 if c < k\nend\n\nputs count\n\n\n"}, {"source_code": "n, m, k = gets.split.map(&:to_i)\nxs = []\nmax = 0\nm.times do\n t = gets[0..n].to_i.to_s(2)\n max = [t.length, max].max\n xs << t\nend\n\nx = gets[0..n].to_i.to_s(2)\nmax = [max, x.length].max\nx.rjust(max, \"0\")\n\nx = x.split(\"\")\nxs.map! do |r|\n r.rjust(max, \"0\").split(\"\")\nend\n\ncount = 0\nxs.each do |i|\n c = 0\n i.each_with_index do |p, j|\n c += 1 if x[j] == p\n end\n\n count += 1 if c <= k && c > 0\nend\n\nputs count\n\n\n"}, {"source_code": "n, m, k = gets.split.map(&:to_i)\nxs = []\nmax = 0\nm.times do\n t = gets.to_i\n xs << t\nend\nx = gets.to_i\ncount = 0\nxs.each do |i|\n c = (i ^ x)\n c = c.to_s(2).split(\"\").map(&:to_i).inject{|sum,x| sum + x }\n break\n count += 1 if c <= k\nend\n\nputs count\n\n\n"}, {"source_code": "n, m, k = gets.split.map(&:to_i)\nxs = []\nmax = 0\nm.times do\n t = gets.to_i\n xs << t\nend\nx = gets.to_i\ncount = 0\nxs.each do |i|\n c = (i ^ x).to_s(2).split(\"\").map(&:to_i).inject{|sum,x| sum + x }\n count += 1 if c < k\nend\n\nputs count\n\n\n"}, {"source_code": "n, m, k = gets.split.map(&:to_i)\nxs = []\nmax = 0\nm.times do\n t = gets[0..n].to_i.to_s(2)\n max = [t.length, max].max\n xs << t\nend\n\nx = gets[0..n].to_i.to_s(2)\nmax = [max, x.length].max\nx.rjust(max, \"0\")\n\nx = x.split(\"\")\nxs.map! do |r|\n r.rjust(max, \"0\").split(\"\")\nend\n\ncount = 0\nxs.each do |i|\n c = 0\n i.each_with_index do |p, j|\n c += 1 if x[j] != p\n end\n count += 1 if c <= k && c > 0\nend\n\nputs count\n\n\n"}, {"source_code": "n, m, k = gets.split.map(&:to_i)\nxs = []\nmax = 0\nm.times do\n t = gets[0..n].to_i.to_s(2)\n max = [t.length, max].max\n xs << t\nend\n\nx = gets[0..n].to_i.to_s(2)\nmax = [max, x.length].max\n\n\nx = x.split(\"\")\nxs.map! do |r|\n r.split(\"\")\nend\n\ncount = 0\nxs.each do |i|\n c = 0\n i.each_with_index do |p, j|\n c += 1 if x[j] != p\n end\n count += 1 if c <= k\nend\n\nputs count\n\n\n"}, {"source_code": "n, m, k = gets.split.map(&:to_i)\nxs = []\nmax = 0\nm.times do\n t = gets[0..n].to_i.to_s(2)\n max = [t.length, max].max\n xs << t\nend\n\nx = gets[0..n].to_i.to_s(2)\nmax = [max, x.length].max\nx.rjust(max, \"0\")\n\nx = x.split(\"\")\nxs.map! do |r|\n r.rjust(max, \"0\").split(\"\")\nend\n\ncount = 0\nxs.each do |i|\n c = 0\n i.each_with_index do |p, j|\n c += 1 if x[j] != p\n end\n count += 1 if c <= k\nend\n\nputs count\n\n\n"}, {"source_code": "n, m, k = gets.split.map(&:to_i)\nxs = []\nmax = 0\nm.times do\n t = gets.to_i\n xs << t\nend\nx = gets.to_i\ncount = 0\nxs.each do |i|\n c = (i ^ x) & i\n c = c.to_s(2).split(\"\").map(&:to_i).inject{|sum,x| sum + x }\n count += 1 if c <= k\nend\n\nputs count\n\n\n"}, {"source_code": "n, m, k = gets.split.map(&:to_i)\nxs = []\nmax = 0\n(m).times do\n t = gets[0..n].to_i.to_s(2)\n max = [t.length, max].max\n xs << t\nend\nx = gets[0..n].to_i.to_s(2)\nmax = [max, x.length].max\nx.ljust(max, \"0\")\n\nx = x.split(\"\")\n\nxs.map! do |r|\n r.ljust(max, \"0\").split(\"\")\nend\n\ncount = 0\nxs.each do |i|\n c = 0\n i.each_with_index do |p, j|\n c += 1 if x[j] == p\n end\n\n count += 1 if c <= k\nend\n\nputs count\n\n\n"}, {"source_code": "n, m, k = gets.split.map(&:to_i)\nxs = []\nmax = 0\nm.times do\n t = gets[0..n].to_i.to_s(2)\n max = [t.length, max].max\n xs << t\nend\n\nx = gets[0..n].to_i.to_s(2)\nmax = [max, x.length].max\nx.ljust(max, \"0\")\n\nx = x.split(\"\")\nxs.map! do |r|\n r.ljust(max, \"0\").split(\"\")\nend\n\ncount = 0\nxs.each do |i|\n c = 0\n i.each_with_index do |p, j|\n c += 1 if x[j] != p\n end\n count += 1 if c <= k\nend\n\nputs count\n\n\n"}], "src_uid": "5ebb0ee239d68ea33d9dac2d0bdd5f4e"} {"nl": {"description": "Polycarp found a rectangular table consisting of $$$n$$$ rows and $$$m$$$ columns. He noticed that each cell of the table has its number, obtained by the following algorithm \"by columns\": cells are numbered starting from one; cells are numbered from left to right by columns, and inside each column from top to bottom; number of each cell is an integer one greater than in the previous cell. For example, if $$$n = 3$$$ and $$$m = 5$$$, the table will be numbered as follows:$$$$$$ \\begin{matrix} 1 & 4 & 7 & 10 & 13 \\\\ 2 & 5 & 8 & 11 & 14 \\\\ 3 & 6 & 9 & 12 & 15 \\\\ \\end{matrix} $$$$$$However, Polycarp considers such numbering inconvenient. He likes the numbering \"by rows\": cells are numbered starting from one; cells are numbered from top to bottom by rows, and inside each row from left to right; number of each cell is an integer one greater than the number of the previous cell. For example, if $$$n = 3$$$ and $$$m = 5$$$, then Polycarp likes the following table numbering: $$$$$$ \\begin{matrix} 1 & 2 & 3 & 4 & 5 \\\\ 6 & 7 & 8 & 9 & 10 \\\\ 11 & 12 & 13 & 14 & 15 \\\\ \\end{matrix} $$$$$$Polycarp doesn't have much time, so he asks you to find out what would be the cell number in the numbering \"by rows\", if in the numbering \"by columns\" the cell has the number $$$x$$$?", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$). Then $$$t$$$ test cases follow. Each test case consists of a single line containing three integers $$$n$$$, $$$m$$$, $$$x$$$ ($$$1 \\le n, m \\le 10^6$$$, $$$1 \\le x \\le n \\cdot m$$$), where $$$n$$$ and $$$m$$$ are the number of rows and columns in the table, and $$$x$$$ is the cell number. Note that the numbers in some test cases do not fit into the $$$32$$$-bit integer type, so you must use at least the $$$64$$$-bit integer type of your programming language.", "output_spec": "For each test case, output the cell number in the numbering \"by rows\".", "sample_inputs": ["5\n1 1 1\n2 2 3\n3 5 11\n100 100 7312\n1000000 1000000 1000000000000"], "sample_outputs": ["1\n2\n9\n1174\n1000000000000"], "notes": null}, "positive_code": [{"source_code": "t = gets().to_i\r\nwhile(t!=0)\r\n ar = gets.chomp.split().map { |e| e.to_i }\r\n n = ar[0]\r\n m = ar[1]\r\n x = ar[2]\r\n x-=1\r\n col = (x/n).to_i\r\n row = x%n\r\n ans = row*m + col + 1\r\n puts \"#{ans}\\n\"\r\n t-=1\r\nend\r\n\r\n"}, {"source_code": "def solve_case\n n, m, x = gets.chomp.split(' ').map(&:to_i)\n\n c = (x.to_f / n.to_f).ceil\n r = x - (n * (c - 1))\n\n row_x = c + (m * (r - 1))\n puts row_x\nend\n\nt = gets.chomp.to_i\nt.times do\n solve_case\nend\n"}, {"source_code": "def solution(n, m, x)\n x -= 1\n\n col = x / n\n row = x % n\n row * m + col + 1\nend\n\ntest_cases = gets.to_i\n\ntest_cases.times do |test_case|\n arr = gets.split(' ').map(&:to_i)\n puts solution(arr[0], arr[1], arr[2])\nend"}], "negative_code": [], "src_uid": "e519e4495c9acef4c4a614aef73cb322"} {"nl": {"description": "You are given three integers $$$a$$$, $$$b$$$, and $$$c$$$. Determine if one of them is the sum of the other two.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 9261$$$) — the number of test cases. The description of each test case consists of three integers $$$a$$$, $$$b$$$, $$$c$$$ ($$$0 \\leq a, b, c \\leq 20$$$).", "output_spec": "For each test case, output \"YES\" if one of the numbers is the sum of the other two, and \"NO\" otherwise. You can output the answer in any case (for example, the strings \"yEs\", \"yes\", \"Yes\" and \"YES\" will be recognized as a positive answer).", "sample_inputs": ["7\n\n1 4 3\n\n2 5 8\n\n9 11 20\n\n0 0 0\n\n20 20 20\n\n4 12 3\n\n15 7 8"], "sample_outputs": ["YES\nNO\nYES\nYES\nNO\nNO\nYES"], "notes": "NoteIn the first test case, $$$1 + 3 = 4$$$.In the second test case, none of the numbers is the sum of the other two.In the third test case, $$$9 + 11 = 20$$$."}, "positive_code": [{"source_code": "n = gets.to_i\r\nn.times do\r\n array = gets.split(' ').map(&:to_i).sort\r\n puts array[0] + array[1] == array[2] ? 'YES' : 'NO'\r\nend\r\n"}, {"source_code": "t = gets.to_i\r\n\r\nt.times do \r\n a, b, c = gets.split(' ').map(&:to_i)\r\n puts a + b == c || a + c == b || c + b == a ? 'YES' : 'NO'\r\nend"}, {"source_code": "t=gets.to_i\r\nwhile t>=1\r\n\ta,b,c=gets.split.map(&:to_i)\r\n\tif a+b==c\r\n\t\tputs \"YES\"\r\n\telsif b+c==a\r\n\t\tputs \"YES\"\r\n\telsif a+c==b\r\n\t\tputs \"YES\"\r\n\telse\r\n\t\tputs \"NO\"\r\n\tend\r\n\tt=t-1\r\nend \r\n"}, {"source_code": "n=gets.to_i\nn.times do\n a=gets.split.map(&:to_i)\n a.sort!\n if a.sum==a[-1]*2\n puts \"YES\"\n else\n puts \"NO\"\n end\nend"}, {"source_code": "gets.to_i.times do\n a=gets.split.map(&:to_i).sort\n puts a[0]+a[1]==a[2] ? 'YES' : 'NO'\nend\n"}], "negative_code": [{"source_code": " n = gets.to_i\r\n n.times do\r\n array = gets.split(' ').map(&:to_i)\r\n puts ([array[0] + array[1], array[1] + array[2], array[0] + array[2]] - array).size < 3 ? \"YES\" : \"NO\"\r\n end"}, {"source_code": "n = gets.to_i\r\nn.times do\r\n array = gets.split(' ').map(&:to_i)\r\n print ([array[0] + array[1], array[1] + array[2], array[0] + array[2]] - array).size < 3 ? \"YES\" : \"NO\"\r\nend\r\n"}, {"source_code": "n = gets.to_i\r\nn.times do\r\n array = gets.split(' ').map(&:to_i)\r\n print ([array[0] + array[1], array[1] + array[2], array[0] + array[2]] - array).size < 3\r\nend\r\n"}], "src_uid": "1b8293c51d025940eb859b0e625ab588"} {"nl": {"description": "Alperen has two strings, $$$s$$$ and $$$t$$$ which are both initially equal to \"a\". He will perform $$$q$$$ operations of two types on the given strings: $$$1 \\;\\; k \\;\\; x$$$ — Append the string $$$x$$$ exactly $$$k$$$ times at the end of string $$$s$$$. In other words, $$$s := s + \\underbrace{x + \\dots + x}_{k \\text{ times}}$$$. $$$2 \\;\\; k \\;\\; x$$$ — Append the string $$$x$$$ exactly $$$k$$$ times at the end of string $$$t$$$. In other words, $$$t := t + \\underbrace{x + \\dots + x}_{k \\text{ times}}$$$. After each operation, determine if it is possible to rearrange the characters of $$$s$$$ and $$$t$$$ such that $$$s$$$ is lexicographically smaller$$$^{\\dagger}$$$ than $$$t$$$.Note that the strings change after performing each operation and don't go back to their initial states.$$$^{\\dagger}$$$ Simply speaking, the lexicographical order is the order in which words are listed in a dictionary. A formal definition is as follows: string $$$p$$$ is lexicographically smaller than string $$$q$$$ if there exists a position $$$i$$$ such that $$$p_i < q_i$$$, and for all $$$j < i$$$, $$$p_j = q_j$$$. If no such $$$i$$$ exists, then $$$p$$$ is lexicographically smaller than $$$q$$$ if the length of $$$p$$$ is less than the length of $$$q$$$. For example, $$$\\texttt{abdc} < \\texttt{abe}$$$ and $$$\\texttt{abc} < \\texttt{abcd}$$$, where we write $$$p < q$$$ if $$$p$$$ is lexicographically smaller than $$$q$$$.", "input_spec": "The first line of the input contains an integer $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$) — the number of test cases. The first line of each test case contains an integer $$$q$$$ $$$(1 \\leq q \\leq 10^5)$$$ — the number of operations Alperen will perform. Then $$$q$$$ lines follow, each containing two positive integers $$$d$$$ and $$$k$$$ ($$$1 \\leq d \\leq 2$$$; $$$1 \\leq k \\leq 10^5$$$) and a non-empty string $$$x$$$ consisting of lowercase English letters — the type of the operation, the number of times we will append string $$$x$$$ and the string we need to append respectively. It is guaranteed that the sum of $$$q$$$ over all test cases doesn't exceed $$$10^5$$$ and that the sum of lengths of all strings $$$x$$$ in the input doesn't exceed $$$5 \\cdot 10^5$$$.", "output_spec": "For each operation, output \"YES\", if it is possible to arrange the elements in both strings in such a way that $$$s$$$ is lexicographically smaller than $$$t$$$ and \"NO\" otherwise.", "sample_inputs": ["3\n\n5\n\n2 1 aa\n\n1 2 a\n\n2 3 a\n\n1 2 b\n\n2 3 abca\n\n2\n\n1 5 mihai\n\n2 2 buiucani\n\n3\n\n1 5 b\n\n2 3 a\n\n2 4 paiu"], "sample_outputs": ["YES\nNO\nYES\nNO\nYES\nNO\nYES\nNO\nNO\nYES"], "notes": "NoteIn the first test case, the strings are initially $$$s = $$$ \"a\" and $$$t = $$$ \"a\". After the first operation the string $$$t$$$ becomes \"aaa\". Since \"a\" is already lexicographically smaller than \"aaa\", the answer for this operation should be \"YES\".After the second operation string $$$s$$$ becomes \"aaa\", and since $$$t$$$ is also equal to \"aaa\", we can't arrange $$$s$$$ in any way such that it is lexicographically smaller than $$$t$$$, so the answer is \"NO\".After the third operation string $$$t$$$ becomes \"aaaaaa\" and $$$s$$$ is already lexicographically smaller than it so the answer is \"YES\".After the fourth operation $$$s$$$ becomes \"aaabb\" and there is no way to make it lexicographically smaller than \"aaaaaa\" so the answer is \"NO\".After the fifth operation the string $$$t$$$ becomes \"aaaaaaabcaabcaabca\", and we can rearrange the strings to: \"bbaaa\" and \"caaaaaabcaabcaabaa\" so that $$$s$$$ is lexicographically smaller than $$$t$$$, so we should answer \"YES\". "}, "positive_code": [{"source_code": "gets.to_i.times do\n q=gets.to_i\n st=Array.new(2) {Hash.new(0)}\n len=Array.new(2,1)\n smin,tmax='a','a'\n 2.times {|i| st[i]['a']=1}\n q.times do\n t,k,x=gets.split.map.with_index {|i,j| j==2 ? i.chomp : i.to_i}\n y=Hash.new(0)\n x.each_char {|i| y[i]+=1}\n y.each do |i,j|\n st[t-1][i]+=j*k\n smin=[smin,i].min if t==1\n tmax=[tmax,i].max if t==2\n len[t-1]+=j*k\n end\n if smintmax then\n puts 'NO'\n elsif st[0][smin] b\n\t\treturn a\n\telse\n\t\treturn b\n\tend\nend\n\nn = gets.chomp.to_i\n\nneg_arr = Array.new(100001){0}\npos_arr = Array.new(100001){0}\n\nneg_size = 0\npos_size = 0\n\nn.times do\n\t\nx,a = gets.chomp.split.map{|x| x.to_i}\n\nif x < 0\n\tneg_arr[x*-1] = a\n\tneg_size+=1\nelse\n\tpos_arr[x] = a\n\tpos_size+=1\nend\n\nend\noutput = 0\nif neg_size == pos_size\n\t(1..100000).each do |i|\n\toutput+=neg_arr[i]\n\toutput+=pos_arr[i]\n\tend\nelse\n\t\n\tmin = min(neg_size,pos_size)\n\tmax = max(neg_size,pos_size)\n\n\tif min == neg_size\n\t\tneg_arr.each do |v|\n\t\t\toutput+=v\n\t\tend\n\t\tj = 0\n\t\t(1..100000).each do |i|\n\t\t\tif pos_arr[i]!=0\n\t\t\t\toutput+=pos_arr[i]\n\t\t\t\tj+=1\n\t\t\tend\n\t\t\tif j==min+1\n\t\t\t\tbreak\n\t\t\tend\n\t\tend\n\telse\n\t\tpos_arr.each do |v|\n\t\t\toutput+=v\n\t\tend\n\t\tj = 0\n\t\t(1..100000).each do |i|\n\t\t\tif neg_arr[i]!=0\n\t\t\t\toutput+=neg_arr[i]\n\t\t\t\tj+=1\n\t\t\tend\n\t\t\tif j==min+1\n\t\t\t\tbreak\n\t\t\tend\n\t\tend\n\tend\n\t\n\t\nend\n\nputs output\n"}, {"source_code": "#!/usr/bin/ruby\na,b=gets.to_i.times.map{\n\tgets.split.map(&:to_i)\n}.partition{|e|\n\te[0]>0\n}.map{|e|\n\te.sort_by{|f|f[0].abs}\n}.sort_by(&:size)\np (a+b[0,a.size+1]).map(&:last).reduce(:+)"}, {"source_code": "n = gets.to_i\npos = {}\nneg = {}\nn.times do\n arr = gets.split(' ')\n x = arr.first.to_i\n a = arr.last.to_i\n if x < 0\n neg[x.abs] = a\n else\n pos[x] = a\n end\nend\nt_size = [neg.size, pos.size].min\nneg = neg.sort\npos = pos.sort\nval = 0\nt_size.times do |i|\n val+=neg[i].last\n val+=pos[i].last\nend\nif neg.size == pos.size\n puts val \nelse\n if neg.size < pos.size\n puts val+pos[t_size].last\n else\n puts val+neg[t_size].last\n end\nend\n\n"}, {"source_code": "n = gets.chomp.to_i\n\nplus = []\nminus = []\nn.times do\n p,v = gets.split.map{ |x| x.to_i }\n minus << [-p,v] if p < 0\n plus << [p,v] if p > 0\nend\n\nplus.sort!\nminus.sort!\n\nsum = 0\nif plus.size >= minus.size\n minus.size.times do |i|\n sum += plus[i][1];\n sum += minus[i][1];\n end\n if plus.size > minus.size\n sum += plus[minus.size][1];\n end\nelse\n plus.size.times do |i|\n sum += plus[i][1];\n sum += minus[i][1];\n end\n sum += minus[plus.size][1];\nend\nputs sum\n"}, {"source_code": "\nn = gets.chomp.to_i\napples = [].tap do |arr|\n n.times { arr << gets.chomp.split.map(&:to_i) }\nend\n\na, b = apples.partition { |e| e.first < 0 }.sort_by(&:size).map do |e|\n e.sort_by! { |e| e.first.abs }\nend\n\nputs (a + b[0..a.size]).map(&:last).inject(0, :+)\n"}, {"source_code": "\nn = gets.to_i;\narr = Array.new();\nans = 0;\n\nn.times {|x| arr[x] = gets.split.map(&:to_i)};\narr.push([0, 0]);\narr.sort!;\npos = arr.index([0, 0]);\n\ns, e = 0, n;\nif (pos > n - pos)\n s = pos - (n - pos) - 1;\nelsif (pos < n - pos)\n e = pos + pos + 1;\nend\n# puts s, e;\n(s..e).each {|x| ans += arr[x][1]};\nputs ans;\n"}, {"source_code": "n = gets.to_i\nt = (1..n).map{ gets.split.map(&:to_i) }\nt.sort_by!{|x, a| x}\ni = t.index{|x, a| x > 0}\n\nif i == nil\n p t[-1][1]\nelsif i == 0\n p t[0][1]\nelse\n l_num = i\n r_num = t.size - i\n l_taken = [l_num, r_num+1].min\n r_taken = [r_num, l_num+1].min\n p (t[i-l_taken..i-1] + t[i..i+r_taken-1]).map{|x,a| a}.inject(:+)\nend\n"}, {"source_code": "a=gets.chomp.to_i\nnegp=[]\nposp=[]\nneg=[]\npos=[]\nans=0\na.times do\ntemp=gets.chomp.split\n0.upto(1) {|i| temp[i]=temp[i].to_i}\nif temp[0]<0\nnegp< 0\n\tl <<= [pair[0], pair[1]] if pair[0] <= 0\nend\n\nr.sort!\nl.sort! &(lambda { |a,b| b <=> a })\n\nbest = 0\nfor i in 0..1 do\n\treachl = 0\n\treachr = 0\n\tscore = 0\n\tleft = i == 0\n\twhile (left && reachl != l.size) || (!left && reachr != r.size)\n\t\tif !left\n\t\t\tscore += r[reachr][1]\n\t\t\treachr += 1\n\t\t\tleft = !left\n\t\telse \n\t\t\tscore += l[reachl][1]\n\t\t\treachl += 1\n\t\t\tleft = !left\n\t\tend\n\tend \n\tbest = [best, score].max\nend\nprint best\n"}, {"source_code": "d = gets.to_i.times.map{gets.split.map(&:to_i)}\nm = d.select{|a,b| a < 0}.sort\np = d.select{|a,b| a > 0}.sort.reverse\nsum = 0\nwhile(m!=[] && p!=[])\n sum += m.pop[1]+p.pop[1]\nend\nif m!=[]\n sum+=m.pop[1]\nend\nif p!=[]\n sum+=p.pop[1]\nend\np sum"}, {"source_code": "n = gets.to_i\na = n.times.map{gets.split.map(&:to_i)}.partition{|x| x[0] < 0}.map{|x| x.sort_by{|x| x[0].abs}}\nm = [a[0].size, a[1].size].min\nputs a[0][0..m].reduce(0){|s, x| s + x[1]} + a[1][0..m].reduce(0){|s, x| s + x[1]}\n"}, {"source_code": "n = gets.to_i\na = n.times.map{gets.split.map(&:to_i)}\nneg = []\npos = []\na.each do |x|\n neg << x if x[0] < 0\n pos << x if x[0] > 0\nend\nm = [neg.size, pos.size].min\nputs neg.sort.reverse[0..m].reduce(0){|s, x| s + x[1]} + pos.sort[0..m].reduce(0){|s, x| s + x[1]}\n\n"}, {"source_code": "a=gets.chomp.to_i\nnegp=[]\nposp=[]\nneg=[]\npos=[]\nans=0\na.times do\ntemp=gets.chomp.split\n0.upto(1) {|i| temp[i]=temp[i].to_i}\nif temp[0]<0\nnegp< b\n\t\treturn a\n\telse\n\t\treturn b\n\tend\nend\n\nn = gets.chomp.to_i\n\nneg_arr = Array.new(100001){0}\npos_arr = Array.new(100001){0}\n\nneg_size = 0\npos_size = 0\n\nn.times do\n\t\nx,a = gets.chomp.split.map{|x| x.to_i}\n\nif x < 0\n\tneg_arr[x*-1] = a\n\tneg_size+=1\nelse\n\tpos_arr[x] = a\n\tpos_size+=1\nend\n\nend\noutput = 0\nif neg_size == pos_size\n\t(1..100).each do |i|\n\toutput+=neg_arr[i]\n\toutput+=pos_arr[i]\n\tend\nelse\n\t\n\tmin = min(neg_size,pos_size)\n\tmax = max(neg_size,pos_size)\n\n\tif min == neg_size\n\t\tneg_arr.each do |v|\n\t\t\toutput+=v\n\t\tend\n\t\tj = 0\n\t\t(0..100).each do |i|\n\t\t\tif pos_arr[i]!=0\n\t\t\t\toutput+=pos_arr[i]\n\t\t\t\tj+=1\n\t\t\tend\n\t\t\tif j==min+1\n\t\t\t\tbreak\n\t\t\tend\n\t\tend\n\telse\n\t\tpos_arr.each do |v|\n\t\t\toutput+=v\n\t\tend\n\t\tj = 0\n\t\t(0..100).each do |i|\n\t\t\tif neg_arr[i]!=0\n\t\t\t\toutput+=neg_arr[i]\n\t\t\t\tj+=1\n\t\t\tend\n\t\t\tif j==min+1\n\t\t\t\tbreak\n\t\t\tend\n\t\tend\n\tend\n\t\n\t\nend\n\nputs output\n"}, {"source_code": "# http://codeforces.com/contest/558/problem/A\n\nn = gets.chomp.to_i\napples = [].tap do |arr|\n n.times { arr << gets.chomp.split.map(&:to_i) }\nend\n\na, b = apples.partition { |a| a.first < 0 }.sort_by(&:size)\nc = a + b[0..a.size]\nputs c.map(&:last).inject(0, :+)\n"}, {"source_code": "n = gets.chomp.to_i\nhash = {}\nn.times do\n idx, num = gets.chomp.split.map(&:to_i)\n hash[idx] = num\nend\n\nmin, max = hash.keys.min, hash.keys.max\nright, left = [], []\n(min..max).each do |idx|\n next unless hash.key?(idx)\n idx < 0 ? left << hash[idx] : right << hash[idx]\nend\n\nscores = []\nscores << right.inject(0, :+) + left.last unless left.empty?\nscores << left.inject(0, :+) + right.first unless right.empty?\nscores << right.first if left.empty?\nscores << left.last if right.empty?\nputs scores.max\n"}, {"source_code": "a=gets.chomp.to_i\nnegp=[]\nposp=[]\nneg=[]\npos=[]\nans=0\na.times do\ntemp=gets.chomp.split\n0.upto(1) {|i| temp[i]=temp[i].to_i}\nif temp[0]<0\nnegp<0\npos << temp[1].to_i\nelse\nans+=temp[1].to_i\nend\nend\nl=neg.length\nb=pos.length\nif l 0\nend\nm = [neg.size, pos.size].min\np neg.sort.reverse , pos.sort\nputs neg.sort.reverse[0..m].reduce(0){|s, x| s + x[1]} + pos.sort[0..m].reduce(0){|s, x| s + x[1]}\n\n"}, {"source_code": "n = gets.to_i\na = n.times.map{gets.split.map(&:to_i)}\nneg = []\npos = []\na.each do |x|\n neg << x if x[0] < 0\n pos << x if x[0] > 0\nend\nm = [neg.size, pos.size].min\nputs neg.reverse[0..m].reduce(0){|s, x| s + x[1]} + pos[0..m].reduce(0){|s, x| s + x[1]}\n\n"}, {"source_code": "n = gets.to_i\na = n.times.map{gets.split.map(&:to_i)}\nneg = []\npos = []\na.each do |x|\n neg << x if x[0] < 0\n pos << x if x[0] > 0\nend\nm = [neg.size, pos.size].min\nputs neg[0..m].reduce(0){|a, b| a + b[1]} + pos[0..m].reduce(0){|a, b| a + b[1]}\n\n"}], "src_uid": "bf573af345509b2364ada6e613b6f998"} {"nl": {"description": "In a far away galaxy there are n inhabited planets, numbered with numbers from 1 to n. They are located at large distances from each other, that's why the communication between them was very difficult until on the planet number 1 a hyperdrive was invented. As soon as this significant event took place, n - 1 spaceships were built on the planet number 1, and those ships were sent to other planets to inform about the revolutionary invention. Paradoxical thought it may be, but the hyperspace is represented as simple three-dimensional Euclidean space. The inhabited planets may be considered fixed points in it, and no two points coincide and no three points lie on the same straight line. The movement of a ship with a hyperdrive between two planets is performed along a straight line at the constant speed, the same for all the ships. That's why the distance in the hyperspace are measured in hyperyears (a ship with a hyperdrive covers a distance of s hyperyears in s years).When the ship reaches an inhabited planet, the inhabitants of the planet dissemble it, make n - 2 identical to it ships with a hyperdrive and send them to other n - 2 planets (except for the one from which the ship arrived). The time to make a new ship compared to the time in which they move from one planet to another is so small that it can be disregarded. New ships are absolutely identical to the ones sent initially: they move at the same constant speed along a straight line trajectory and, having reached a planet, perform the very same mission, i.e. are dissembled to build new n - 2 ships and send them to all the planets except for the one from which the ship arrived. Thus, the process of spreading the important news around the galaxy continues.However the hyperdrive creators hurried to spread the news about their invention so much that they didn't study completely what goes on when two ships collide in the hyperspace. If two moving ships find themselves at one point, they provoke an explosion of colossal power, leading to the destruction of the galaxy!Your task is to find the time the galaxy will continue to exist from the moment of the ships' launch from the first planet.", "input_spec": "The first line contains a number n (3 ≤ n ≤ 5000) — the number of inhabited planets in the galaxy. The next n lines contain integer coordinates of the planets in format \"xi yi zi\" ( - 104 ≤ xi, yi, zi ≤ 104). ", "output_spec": "Print the single number — the solution to the task with an absolute or relative error not exceeding 10 - 6.", "sample_inputs": ["4\n0 0 0\n0 0 1\n0 1 0\n1 0 0"], "sample_outputs": ["1.7071067812"], "notes": null}, "positive_code": [{"source_code": "class Star\n\tdef initialize(x, y, z)\n\t\t@x = x\n\t\t@y = y\n\t\t@z = z\n\tend\n\tdef setzero(zs)\n\t\t@zero = distance(zs)\n\tend\n\tdef distance(s)\n\t\tMath.sqrt((@x-s.x)*(@x-s.x)+(@y-s.y)*(@y-s.y)+(@z-s.z)*(@z-s.z))\n\tend\n\tattr_accessor :x, :y, :z, :zero\nend\n\nn = gets.chomp.to_i\nstars = Array.new\nfor i in 0..n-1\n\tar = gets.chomp.split(/ /).map!{|x| x.to_i}\n\tstars.push(Star.new(ar[0],ar[1],ar[2]))\n\tszero = stars[0] if i == 0\n\tstars[i].setzero(szero)\nend\n\nstars.sort!{|a,b| a.zero <=> b.zero}\n\nret = -1\nfor i in 2..n-1\n\tbreak if ret >= 0 && stars[i].zero > ret\n\tfor j in 1..i-1\n\t\twk = stars[i].zero + stars[j].zero\n\t\tbreak if ret >= 0 && wk > ret\n\t\twk += stars[i].distance(stars[j])\n\t\tif ret < 0\n\t\t\tret = wk\n\t\telse\n\t\t\tret = [ret,wk].min\n\t\tend\n\tend\nend\n\nputs ret/2.0\n"}], "negative_code": [{"source_code": "class Star\n\tdef initialize(x, y, z)\n\t\t@x = x\n\t\t@y = y\n\t\t@z = z\n\tend\n\tdef setzero(zs)\n\t\t@zero = distance(zs)\n\tend\n\tdef distance(s)\n\t\tMath.sqrt((@x-s.x)*(@x-s.x)+(@y-s.y)*(@y-s.y)+(@z-s.z)*(@z-s.z))\n\tend\n\tattr_accessor :x, :y, :z, :zero\nend\n\nn = gets.chomp.to_i\nstars = Array.new\nfor i in 0..n-1\n\tar = gets.chomp.split(/ /).map!{|x| x.to_i}\n\tstars.push(Star.new(ar[0],ar[1],ar[2]))\n\tszero = stars[0] if i == 0\n\tstars[i].setzero(szero)\nend\n\nstars.sort!{|a,b| a.zero <=> b.zero}\n\nret = -1\nfor i in 2..n-2\n\tbreak if ret >= 0 && stars[i].zero > ret\n\tfor j in 1..i-1\n\t\twk = stars[i].zero + stars[j].zero\n\t\tbreak if ret >= 0 && wk > ret\n\t\twk += stars[i].distance(stars[j])\n\t\tif ret < 0\n\t\t\tret = wk\n\t\telse\n\t\t\tret = [ret,wk].min\n\t\tend\n\tend\nend\n\nputs ret/2.0\n"}], "src_uid": "71374bd239df13f3345f05eabe4c83c7"} {"nl": {"description": "You are given a string $$$s$$$ of lowercase Latin letters. The following operation can be used: select one character (from 'a' to 'z') that occurs at least once in the string. And replace all such characters in the string with the previous one in alphabetical order on the loop. For example, replace all 'c' with 'b' or replace all 'a' with 'z'. And you are given the integer $$$k$$$ — the maximum number of operations that can be performed. Find the minimum lexicographically possible string that can be obtained by performing no more than $$$k$$$ operations.The string $$$a=a_1a_2 \\dots a_n$$$ is lexicographically smaller than the string $$$b = b_1b_2 \\dots b_n$$$ if there exists an index $$$k$$$ ($$$1 \\le k \\le n$$$) such that $$$a_1=b_1$$$, $$$a_2=b_2$$$, ..., $$$a_{k-1}=b_{k-1}$$$, but $$$a_k < b_k$$$.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) —the number of test cases in the test. This is followed by descriptions of the test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$, $$$1 \\le k \\le 10^9$$$) — the size of the string $$$s$$$ and the maximum number of operations that can be performed on the string $$$s$$$. The second line of each test case contains a string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, output the lexicographically minimal string that can be obtained from the string $$$s$$$ by performing no more than $$$k$$$ operations.", "sample_inputs": ["4\n\n3 2\n\ncba\n\n4 5\n\nfgde\n\n7 5\n\ngndcafb\n\n4 19\n\nekyv"], "sample_outputs": ["aaa\nagaa\nbnbbabb\naapp"], "notes": null}, "positive_code": [{"source_code": "gets.to_i.times do\r\n n, k = gets.split.map(&:to_i)\r\n s = gets.chomp.bytes.map { |c| c.ord - 97 }\r\n if max = s.take_while { |c| c <= k }.max\r\n 0.upto(s.size - 1).each do |i|\r\n s[i] = 0 if s[i] <= max\r\n end\r\n k -= max\r\n end\r\n if i = s.find_index { |c| c > 0 }\r\n c = s[i]\r\n min = s[i] = s[i] - k\r\n (i + 1).upto(s.size - 1).each do |i|\r\n s[i] = min if s[i] <= c && s[i] >= min\r\n end\r\n end\r\n 0.upto(s.size - 1).each do |i|\r\n s[i] += 97\r\n end\r\n puts s.pack(\"C*\")\r\nend\r\n"}], "negative_code": [], "src_uid": "b86c1533fdfe68fd4dea2bf99cd9e111"} {"nl": {"description": "You are given two positive integers $$$n$$$ and $$$s$$$. Find the maximum possible median of an array of $$$n$$$ non-negative integers (not necessarily distinct), such that the sum of its elements is equal to $$$s$$$.A median of an array of integers of length $$$m$$$ is the number standing on the $$$\\lceil {\\frac{m}{2}} \\rceil$$$-th (rounding up) position in the non-decreasing ordering of its elements. Positions are numbered starting from $$$1$$$. For example, a median of the array $$$[20,40,20,50,50,30]$$$ is the $$$\\lceil \\frac{m}{2} \\rceil$$$-th element of $$$[20,20,30,40,50,50]$$$, so it is $$$30$$$. There exist other definitions of the median, but in this problem we use the described definition.", "input_spec": "The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. Description of the test cases follows. Each test case contains a single line with two integers $$$n$$$ and $$$s$$$ ($$$1 \\le n, s \\le 10^9$$$) — the length of the array and the required sum of the elements.", "output_spec": "For each test case print a single integer — the maximum possible median.", "sample_inputs": ["8\n1 5\n2 5\n3 5\n2 1\n7 17\n4 14\n1 1000000000\n1000000000 1"], "sample_outputs": ["5\n2\n2\n0\n4\n4\n1000000000\n0"], "notes": "NotePossible arrays for the first three test cases (in each array the median is underlined): In the first test case $$$[\\underline{5}]$$$ In the second test case $$$[\\underline{2}, 3]$$$ In the third test case $$$[1, \\underline{2}, 2]$$$ "}, "positive_code": [{"source_code": "t = gets.to_i\r\nt.times do\r\n\r\nn, s = gets.split.map!(&:to_i)\r\n\r\nnh = n / 2\r\nx = s / (nh + 1)\r\n\r\nputs x\r\n\r\nend\r\n"}], "negative_code": [], "src_uid": "0a05b11307fbb2536f868acf4e81c1e2"} {"nl": {"description": "Berland State University has received a new update for the operating system. Initially it is installed only on the $$$1$$$-st computer.Update files should be copied to all $$$n$$$ computers. The computers are not connected to the internet, so the only way to transfer update files from one computer to another is to copy them using a patch cable (a cable connecting two computers directly). Only one patch cable can be connected to a computer at a time. Thus, from any computer where the update files are installed, they can be copied to some other computer in exactly one hour.Your task is to find the minimum number of hours required to copy the update files to all $$$n$$$ computers if there are only $$$k$$$ patch cables in Berland State University.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^5$$$) — the number of test cases. Each test case consists of a single line that contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le k \\le n \\le 10^{18}$$$) — the number of computers and the number of patch cables.", "output_spec": "For each test case print one integer — the minimum number of hours required to copy the update files to all $$$n$$$ computers.", "sample_inputs": ["4\n8 3\n6 6\n7 1\n1 1"], "sample_outputs": ["4\n3\n6\n0"], "notes": "NoteLet's consider the test cases of the example: $$$n=8$$$, $$$k=3$$$: during the first hour, we copy the update files from the computer $$$1$$$ to the computer $$$2$$$; during the second hour, we copy the update files from the computer $$$1$$$ to the computer $$$3$$$, and from the computer $$$2$$$ to the computer $$$4$$$; during the third hour, we copy the update files from the computer $$$1$$$ to the computer $$$5$$$, from the computer $$$2$$$ to the computer $$$6$$$, and from the computer $$$3$$$ to the computer $$$7$$$; during the fourth hour, we copy the update files from the computer $$$2$$$ to the computer $$$8$$$. $$$n=6$$$, $$$k=6$$$: during the first hour, we copy the update files from the computer $$$1$$$ to the computer $$$2$$$; during the second hour, we copy the update files from the computer $$$1$$$ to the computer $$$3$$$, and from the computer $$$2$$$ to the computer $$$4$$$; during the third hour, we copy the update files from the computer $$$1$$$ to the computer $$$5$$$, and from the computer $$$2$$$ to the computer $$$6$$$. $$$n=7$$$, $$$k=1$$$: during the first hour, we copy the update files from the computer $$$1$$$ to the computer $$$2$$$; during the second hour, we copy the update files from the computer $$$1$$$ to the computer $$$3$$$; during the third hour, we copy the update files from the computer $$$1$$$ to the computer $$$4$$$; during the fourth hour, we copy the update files from the computer $$$4$$$ to the computer $$$5$$$; during the fifth hour, we copy the update files from the computer $$$4$$$ to the computer $$$6$$$; during the sixth hour, we copy the update files from the computer $$$3$$$ to the computer $$$7$$$. "}, "positive_code": [{"source_code": "def solve()\n n, k = ints\n m = 1\n t = 0\n # until >= k\n while m <= k\n m *= 2\n t += 1\n end\n t += (n - m + k - 1) / k\n t\nend\n\ndef main\n t = int\n t.times do\n puts solve\n end\nend\n\nDEBUG = true\nMOD = 10**9+7\nYESNO = %w(No Yes)\nINF = 10**9\n\ndef int; gets.to_s.to_i end\ndef ints; gets.to_s.split.map { |s| s.to_i } end\ndef int1s; gets.to_s.split.map { |s| s.to_i - 1 } end\ndef float; gets.to_s.to_f end\ndef floats; gets.to_s.split.map { |s| s.to_f } end\ndef array_of(&convert); gets.to_s.split.map(&convert) end\ndef string; gets.to_s.chomp end\ndef rep(n, &b); Array.new(n, &b) end\ndef yes; puts YESNO[1] end\ndef no; puts YESNO[0] end\ndef yesno t; puts YESNO[t] end\ndef zip(xs, *yss); Enumerator.new { |y| xs.zip(*yss) { |a| y.yield(*a) } } end\ndef max(*xs, &block); block_given? ? xs.max_by(&block) : xs.max end\ndef min(*xs, &block); block_given? ? xs.min_by(&block) : xs.min end\ndef minmax(*xs, &block); block_given? ? xs.minmax_by(&block) : xs.minmax end\ndef gcd(*xs); xs.inject(0, :gcd) end\ndef matrix(h, w, fill=nil, &block); return Array.new(h) { Array.new(w, &block) } if block_given?; Array.new(h) { [fill] * w } end\ndef debug(x = nil); STDERR.puts (block_given? ? yield(x) : x).inspect if DEBUG; x end\ndef debug_grid(grid, width = 1); grid.each { |row| STDERR.puts row.map { |x| x.inspect.ljust(width) }.join(\"\") } if DEBUG; grid end\ndef if_debug; yield if DEBUG end\n\nmodule Boolean\n def coerce(other); [other, to_i] end\n def +@; to_i end\n def to_int; to_i end\n def *(other); to_i * other end\nend\n\nclass TrueClass\n include Boolean\n def to_i; 1 end\nend\n\nclass FalseClass\n include Boolean\n def to_i; 0 end\nend\n\nclass Integer\n def div_ceil(y); (self + y - 1) / y end\n def mod_inv(mod = MOD); pow(mod-2, mod) end\n def mod_div(y, mod = MOD); self * mod_inv(y, mod) % mod end\n def factorial(mod = MOD); (2..self).inject(1) { |f, x| f * x % mod } end\n def popcount; x = self; c = 0; while x > 0; c += 1 if x & 1 == 1; x >>= 1 end; c end #TODO: faster\n def bitbrute(&block); (1< 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\n def lower_bound; ac, wa = self.end_open, self.begin; while ac - wa > 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\n def shakutori(r2, &pred); Enumerator.new { |y| j, r = r2.begin, r2.end_open; each { |i| j += 1 while j + 1 < r and pred[i, j+1]; y.yield(i, j) } }; end\n def widest(&block); Enumerator.new { |y| j, n = self.begin, self.end_open; each { |i| j += 1 while j < n and block[i, j]; y.yield(i, j) if block[i, j] } } end\nend\n\nclass Array\n def power(&block); (0 ... 1 << size).each(&block) end\n def sorted_merge(other); a = []; i = j = 0; n, m = size, other.size; if j < m and other[j] < self[i]; a << other[j]; j += 1 else; a << self[i]; i += 1 end while i < n; a.push(*other[j..-1]) if j < m; a end\n def upper_bound; ac, wa = 0, size; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\n def lower_bound; ac, wa = size, 0; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\n def cum(*xs, &op); a = []; a << xs[0] if xs.size > 0; a << x = self[0]; (1...size).each { |i| a << x = op[x, self[i]] }; a end\n def cumdiff(range); self[range.end_open] - self[range.begin]; end\nend\n\nmodule Enumerable\n def sorted_uniq; x = nil; filter { |y| c = x === y; x = y; !c } end\n def cumsum; ys = [0]; each { |x| ys << x + ys[-1] }; ys end\nend\n\nmain"}], "negative_code": [], "src_uid": "5df6eb50ead22b498bea69bb84341c06"} {"nl": {"description": "Вася купил стол, у которого n ножек. Каждая ножка состоит из двух частей, которые соединяются друг с другом. Каждая часть может быть произвольной положительной длины, но гарантируется, что из всех 2n частей возможно составить n ножек одинаковой длины. При составлении ножки любые две части могут быть соединены друг с другом. Изначально все ножки стола разобраны, а вам заданы длины 2n частей в произвольном порядке.Помогите Васе собрать все ножки стола так, чтобы все они были одинаковой длины, разбив заданные 2n части на пары правильным образом. Каждая ножка обязательно должна быть составлена ровно из двух частей, не разрешается использовать как ножку только одну часть.", "input_spec": "В первой строке задано число n (1 ≤ n ≤ 1000) — количество ножек у стола, купленного Васей. Во второй строке следует последовательность из 2n целых положительных чисел a1, a2, ..., a2n (1 ≤ ai ≤ 100 000) — длины частей ножек стола в произвольном порядке.", "output_spec": "Выведите n строк по два целых числа в каждой — длины частей ножек, которые надо соединить друг с другом. Гарантируется, что всегда возможно собрать n ножек одинаковой длины. Если ответов несколько, разрешается вывести любой из них.", "sample_inputs": ["3\n1 3 2 4 5 3", "3\n1 1 1 2 2 2"], "sample_outputs": ["1 5\n2 4\n3 3", "1 2\n2 1\n1 2"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/env ruby\n\nn = gets.to_i\nnums = gets.split(' ').map { |item| item.to_i }\navg = nums.inject(:+)/n\nfor i in 0..2*n-2\n if !nums[i].nil? then\n for j in 1..2*n-1\n if i != j and !nums[i].nil? and !nums[j].nil? and nums[i] + nums[j] == avg then puts nums[i].to_s+\" \"+nums[j].to_s; nums[i] = nil; nums[j] = nil end\n end\n end\nend\n"}], "negative_code": [{"source_code": "#!/usr/bin/env ruby\n\nn = gets.to_i\nnums = gets.split(' ').map { |item| item.to_i }\navg = nums.inject(:+)/n\nfor i in 0..2*n-2\n for j in 1..2*n-1\n if !nums[i].nil? and !nums[j].nil? and nums[i] + nums[j] == avg then puts nums[i].to_s+\" \"+nums[j].to_s; nums[i] = nil; nums[j] = nil end\n end\nend\n"}, {"source_code": "#!/usr/bin/env ruby\n\nn = gets.to_i\nnums = gets.split(' ').map { |item| item.to_i }\navg = nums.inject(:+)/n\nfor i in 0..2*n-2\n for j in 0..2*n-1\n if !nums[i].nil? and !nums[j].nil? and nums[i] + nums[j] == avg then puts nums[i].to_s+\" \"+nums[j].to_s; nums[i] = nil; nums[j] = nil end\n end\nend\n"}], "src_uid": "99dd93970b66c886297082d28a46c022"} {"nl": {"description": "The Beroil corporation structure is hierarchical, that is it can be represented as a tree. Let's examine the presentation of this structure as follows: employee ::= name. | name:employee1,employee2, ... ,employeek. name ::= name of an employee That is, the description of each employee consists of his name, a colon (:), the descriptions of all his subordinates separated by commas, and, finally, a dot. If an employee has no subordinates, then the colon is not present in his description.For example, line MIKE:MAX.,ARTEM:MIKE..,DMITRY:DMITRY.,DMITRY... is the correct way of recording the structure of a corporation where the director MIKE has subordinates MAX, ARTEM and DMITRY. ARTEM has a subordinate whose name is MIKE, just as the name of his boss and two subordinates of DMITRY are called DMITRY, just like himself.In the Beroil corporation every employee can only correspond with his subordinates, at that the subordinates are not necessarily direct. Let's call an uncomfortable situation the situation when a person whose name is s writes a letter to another person whose name is also s. In the example given above are two such pairs: a pair involving MIKE, and two pairs for DMITRY (a pair for each of his subordinates).Your task is by the given structure of the corporation to find the number of uncomfortable pairs in it.", "input_spec": "The first and single line contains the corporation structure which is a string of length from 1 to 1000 characters. It is guaranteed that the description is correct. Every name is a string consisting of capital Latin letters from 1 to 10 symbols in length.", "output_spec": "Print a single number — the number of uncomfortable situations in the company.", "sample_inputs": ["MIKE:MAX.,ARTEM:MIKE..,DMITRY:DMITRY.,DMITRY...", "A:A..", "A:C:C:C:C....."], "sample_outputs": ["3", "1", "6"], "notes": null}, "positive_code": [{"source_code": "def gao()\n\ts = $a.slice!(/\\w*/)\n\t$c += $*.count(s)\n\t$* << s\n\tif $a.slice!(0) == ':'\n\t\tgao()\n\t\tgao() while $a.slice!(0) == ','\n\tend\n\t$*.pop\nend\n\n$a, $c = gets, 0\ngao()\np $c\n\n"}], "negative_code": [], "src_uid": "31057c0e76e6985a68b2a298236a8ee5"} {"nl": {"description": "Molly Hooper has n different kinds of chemicals arranged in a line. Each of the chemicals has an affection value, The i-th of them has affection value ai.Molly wants Sherlock to fall in love with her. She intends to do this by mixing a contiguous segment of chemicals together to make a love potion with total affection value as a non-negative integer power of k. Total affection value of a continuous segment of chemicals is the sum of affection values of each chemical in that segment.Help her to do so in finding the total number of such segments.", "input_spec": "The first line of input contains two integers, n and k, the number of chemicals and the number, such that the total affection value is a non-negative power of this number k. (1 ≤ n ≤ 105, 1 ≤ |k| ≤ 10). Next line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — affection values of chemicals.", "output_spec": "Output a single integer — the number of valid segments.", "sample_inputs": ["4 2\n2 2 2 2", "4 -3\n3 -6 -3 12"], "sample_outputs": ["8", "3"], "notes": "NoteDo keep in mind that k0 = 1.In the first sample, Molly can get following different affection values: 2: segments [1, 1], [2, 2], [3, 3], [4, 4]; 4: segments [1, 2], [2, 3], [3, 4]; 6: segments [1, 3], [2, 4]; 8: segments [1, 4]. Out of these, 2, 4 and 8 are powers of k = 2. Therefore, the answer is 8.In the second sample, Molly can choose segments [1, 2], [3, 3], [3, 4]."}, "positive_code": [{"source_code": "n, k = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\nas = [0]\nn.times.each{|i| as[i + 1] = a[i] + as[i]}\n\nks = []\nif k.abs == 1\n ks << 1\n ks << -1 if k == -1\nelse\n l = as.max - as.min\n mx = 1\n while mx.abs <= l\n ks << mx\n mx *= k\n end\nend\n\nans = 0\nshows = Hash.new(0)\nas.each do |x|\n shows[x] += 1\n ks.each do |kk|\n ans += shows[x - kk]\n end\nend\n\np ans\n"}], "negative_code": [{"source_code": "n, k = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\nas = [0]\nn.times.each{|i| as[i + 1] = a[i] + as[i]}\n\nans = 0\nkk = 1\nif k == 1\n ks = [1]\nelsif k == -1\n ks = [-1, 1]\nelse\n l = as.max - as.min\n ks = (Math.log(l.abs, k.abs).to_i + 1).times.map{|i| k ** i}\nend\nshows = Hash.new(0)\nas.each do |x|\n shows[x] += 1\n ks.each do |kk|\n ans += shows[x - kk]\n end\nend\n\np ans\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\nas = [0]\nn.times.each{|i| as[i + 1] = a[i] + as[i]}\n\nans = 0\nkk = 1\nif k.abs == 1\n ks = [0, k]\nelse\n l = as.max - as.min\n ks = (Math.log(l.abs, k.abs).to_i + 1).times.map{|i| k ** i}\nend\nshows = Hash.new(0)\nas.each do |x|\n shows[x] += 1\n ks.each do |kk|\n ans += shows[x - kk]\n end\nend\n\np ans\n"}], "src_uid": "e4a2cfe3e76a7fafb8116c1972394c46"} {"nl": {"description": "Little Robber Girl likes to scare animals in her zoo for fun. She decided to arrange the animals in a row in the order of non-decreasing height. However, the animals were so scared that they couldn't stay in the right places.The robber girl was angry at first, but then she decided to arrange the animals herself. She repeatedly names numbers l and r such that r - l + 1 is even. After that animals that occupy positions between l and r inclusively are rearranged as follows: the animal at position l swaps places with the animal at position l + 1, the animal l + 2 swaps with the animal l + 3, ..., finally, the animal at position r - 1 swaps with the animal r.Help the robber girl to arrange the animals in the order of non-decreasing height. You should name at most 20 000 segments, since otherwise the robber girl will become bored and will start scaring the animals again.", "input_spec": "The first line contains a single integer n (1 ≤ n ≤ 100) — number of animals in the robber girl's zoo. The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is the height of the animal occupying the i-th place.", "output_spec": "Print the sequence of operations that will rearrange the animals by non-decreasing height. The output should contain several lines, i-th of the lines should contain two space-separated integers li and ri (1 ≤ li < ri ≤ n) — descriptions of segments the robber girl should name. The segments should be described in the order the operations are performed. The number of operations should not exceed 20 000. If the animals are arranged correctly from the start, you are allowed to output nothing.", "sample_inputs": ["4\n2 1 4 3", "7\n36 28 57 39 66 69 68", "5\n1 2 1 2 1"], "sample_outputs": ["1 4", "1 4\n6 7", "2 5\n3 4\n1 4\n1 4"], "notes": "NoteNote that you don't have to minimize the number of operations. Any solution that performs at most 20 000 operations is allowed."}, "positive_code": [{"source_code": "n = gets.chomp.to_i\narr = gets.chomp.split.map(&:to_i)\n\nn.times do\n (0...(n - 1)).each do |idx|\n if arr[idx] > arr[idx + 1]\n arr[idx], arr[idx + 1] = arr[idx + 1], arr[idx]\n puts [idx + 1, idx + 2].join(\" \")\n end\n end\nend\n"}, {"source_code": "n=gets.to_i\na=gets.split.map &:to_i\nloop {\n changed=false\n for i in 1...n\n if a[i-1]>a[i]\n puts \"#{i} #{i+1}\"\n changed=true\n a[i-1], a[i]=a[i], a[i-1]\n break\n end\n end\n break unless changed\n}\n"}, {"source_code": "n = gets.chomp.to_i\nas = gets.chomp.split.map(&:to_i)\nbs = as.sort\n\nwhile as != bs\n for i in 0...n - 1\n if as[i] > as[i + 1]\n puts \"#{i + 1} #{i + 2}\"\n tmp = as[i + 1]\n as[i + 1] = as[i]\n as[i] = tmp\n end\n end\nend\n\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nfor i in 1..n\n for j in 1..n-i\n if a[j-1]>a[j]\n a[j-1],a[j] = a[j],a[j-1]\n puts \"#{j} #{j+1}\"\n end\n end\nend"}, {"source_code": "animalNumber = gets.chomp.to_i\nheights = gets.chomp.split(' ').map(&:to_i)\n\nsortedHeights = heights.sort\n\n\ndef pull target, source, heights\n\tindex = source\n\n\tuntil index == target\n\t\ttemp = heights[index - 1]\n\t\theights[index - 1] = heights[index]\n\t\theights[index] = temp\n\n\t\tputs \"#{index} #{index + 1}\"\n\t\tindex -= 1\n\tend\nend\n\nfor i in 0...(heights.length)\n\tif heights[i] != sortedHeights[i]\n\t\tindex = heights[i...(heights.length)].index(sortedHeights[i]) + i\n\t\tpull i, index, heights\n\tend\nend"}], "negative_code": [{"source_code": "n = gets.chomp.to_i\narr = gets.chomp.split.map(&:to_i)\n\nn.times do\n (0...(n - 1)).each do |idx|\n if arr[idx] < arr[idx + 1]\n arr[idx], arr[idx + 1] = arr[idx + 1], arr[idx]\n puts [idx + 1, idx + 2].join(\" \")\n end\n end\nend\n"}], "src_uid": "233c2806db31916609421fbd126133d0"} {"nl": {"description": "Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.Petya recently learned to determine whether a string of lowercase Latin letters is lucky. For each individual letter all its positions in the string are written out in the increasing order. This results in 26 lists of numbers; some of them can be empty. A string is considered lucky if and only if in each list the absolute difference of any two adjacent numbers is a lucky number. For example, let's consider string \"zbcdzefdzc\". The lists of positions of equal letters are: b: 2 c: 3, 10 d: 4, 8 e: 6 f: 7 z: 1, 5, 9 Lists of positions of letters a, g, h, ..., y are empty.This string is lucky as all differences are lucky numbers. For letters z: 5 - 1 = 4, 9 - 5 = 4, for letters c: 10 - 3 = 7, for letters d: 8 - 4 = 4. Note that if some letter occurs only once in a string, it doesn't influence the string's luckiness after building the lists of positions of equal letters. The string where all the letters are distinct is considered lucky.Find the lexicographically minimal lucky string whose length equals n.", "input_spec": "The single line contains a positive integer n (1 ≤ n ≤ 105) — the length of the sought string.", "output_spec": "Print on the single line the lexicographically minimal lucky string whose length equals n.", "sample_inputs": ["5", "3"], "sample_outputs": ["abcda", "abc"], "notes": "NoteThe lexical comparison of strings is performed by the < operator in modern programming languages. String a is lexicographically less than string b if exists such i (1 ≤ i ≤ n), that ai < bi, and for any j (1 ≤ j < i) aj = bj."}, "positive_code": [{"source_code": "#!/usr/bin/env ruby\n\nARGF.each_line do |s|\n num = s.to_i\n ret = 'abcd' * (1 + num / 4)\n\n puts ret[0..(num-1)]\nend\n\n"}, {"source_code": "n = gets.to_i\nputs ('abcd' * n)[0, n]\n"}, {"source_code": "n = gets.to_i\nputs ('abcd' * n)[0, n]\n"}, {"source_code": "n = gets.to_i\nputs ('abcd' * (n/4+1))[0, n]\n"}, {"source_code": "#!/usr/bin/ruby\n\nn = gets.to_i\nstr = 'abcd' * (n / 4)\nstr << 'abcd'[0...(n%4)]\nputs str\n"}, {"source_code": "a=\"abcd\"*25000\ngets.to_i.times{|i| print \"#{a[i]}\"}"}, {"source_code": "n=gets.to_i;puts (\"abcd\"*(n+1))[0,n]"}, {"source_code": "n=gets.to_i\n\ndata=Array.new(2*n)\nstr=[*?a..?z].join\n\nn.times{|i|\n\tif i-4<0\n\t\tdata[i]=str[i%26,1]\n\telse\n\t\tdata[i]=data[i-4]\n\tend\n}\n\nputs data.join\n"}, {"source_code": "n = gets.chop.to_i\ns = Array.new(n);\nfor i in 0..n-1\n\tprint ((i%4)+'a'.ord).chr\n\t\nend\nprint \"\\n\"\n"}, {"source_code": "def check(val)\n count = 0\n hash = Hash.new\n index = 1\n val.each_char { |s|\n if hash[s] == nil\n hash[s] = Array.new\n end\n\n hash[s] << index\n index += 1\n }\n\n hash.delete_if {|key, value| value.length == 1 }\n\n if hash.length == 0\n return true\n end\n\n #p hash\n\n hash.each_value { |arr|\n #p arr\n for i in 1..arr.length-1\n diff = arr[i] - arr[i-1]\n #puts \"#{arr[i]} - #{arr[i-1]} = #{diff}\"\n if (diff != 4 and diff != 7)\n return false\n end\n end\n }\n return true\nend\n\ndef inc(val, index)\n if (val[index] == 'z')\n val[index] = 'a'\n val = inc(val, index - 1)\n else\n val[index] = val[index].succ\n end\n return val\nend\n\nl = gets.chop.to_i\nval = ''\n#val = gets.chop\nf = l/4\no = l % 4\nval = 'abcd' * f\no.times { |i| val += 'a' }\n#puts \"#{f} #{o}\"\n\n#l.times { |i|\n #val += 'a'\n while check(val) == false\n val = inc(val, val.length-1)\n end\n#}\nputs val"}, {"source_code": "ans = \"abcd\" * 25000\nn = gets.to_i\nputs ans[0..(n-1)]"}, {"source_code": "#! /usr/bin/env ruby\n\nn = STDIN.read.to_i\n\nalpha = \"abcd\"\nn.times { |i|\n print alpha[i % 4].chr\n}\nputs\n"}, {"source_code": "n = gets.to_i\nputs(('abcd' * (1 + n / 4))[0...n])"}, {"source_code": "n = gets.to_i\nputs(('abcd' * (1 + n / 4))[0..n - 1])"}, {"source_code": "n = gets.to_i\nputs(('abcd' * (1 + n / 4))[0..(n % 4 - 5)])"}, {"source_code": "\n\narr = (0..STDIN.gets.to_i-1).map {|i| nil}\n\n#charlist = %W[a b c d e f g h i j k l m n o p q r s t u v w x y z]\ncharlist = %W[a b c d]\n\nidx = -1\n\nwhile arr.index(nil)\n idx += 1\n ch = charlist[idx]\n pre = 0\n (0..arr.length-1).each do |i|\n if arr[i] == nil\n pre = i\n arr[pre] = ch\n break\n end\n end\n (0..arr.length-1).each do |i|\n diff = i-pre\n if diff == 4 or diff == 7 and arr[i] == nil\n pre = i\n arr[i] = ch\n end\n end\n #puts arr.to_s\nend\n\n\nputs arr.join\n"}, {"source_code": "\n\narr = (0..STDIN.gets.to_i-1).map {|i| nil}\n\ncharlist = %W[a b c d e f g h i j k l m n o p q r s t u v w x y z]\n\nidx = -1\n\nwhile arr.index(nil)\n idx += 1\n ch = charlist[idx]\n pre = 0\n (0..arr.length-1).each do |i|\n if arr[i] == nil\n pre = i\n arr[pre] = ch\n break\n end\n end\n (0..arr.length-1).each do |i|\n diff = i-pre\n if diff == 4 or diff == 7 and arr[i] == nil\n pre = i\n arr[i] = ch\n end\n end\n #puts arr.to_s\nend\n\n\nputs arr.join\n"}, {"source_code": "n = gets.chomp.to_i\nputs (\"abcd\" * ((n / 4.0).ceil.to_i)).chars.to_a.take(n).join('')"}], "negative_code": [{"source_code": "#!/usr/bin/env ruby\n\nARGF.each_line do |s|\n num = s.to_i\n ret = 'z' * num\n\n num.times do |i|\n tmp = (97 + (i % 4)).chr\n ret[i] = tmp if ret[i] > tmp\n\n tmp = (97 + (i % 7)).chr\n ret[i] = tmp if ret[i] > tmp\n end\n puts ret\nend\n\n"}, {"source_code": "ans = \"abcdabc\" * 15000\nn = gets.to_i\nputs ans[0..(n-1)]"}, {"source_code": "#!/usr/bin/env ruby\n\n\nn = STDIN.gets.to_i\n\ncount = n/7\nwhile count > -1\n if (n-7*count) % 4 == 0\n puts [[4]*((n-7*count)/4),[7]*count].flatten.join\n exit\n end\n count -= 1\nend\nputs -1\n"}, {"source_code": "\n\narr = (0..STDIN.gets.to_i-1).map {|i| nil}\n\n%W[a b c d e f g h i j k l m n o p q r s t u v w x y z].each do |ch|\n pre = 0\n (0..arr.length-1).each do |i|\n if arr[i] == nil\n pre = i\n arr[pre] = ch\n break\n end\n end\n (0..arr.length-1).each do |i|\n diff = i-pre\n if diff == 4 or diff == 7 and arr[i] == nil\n pre = i\n arr[i] = \"a\"\n end\n end\n break unless arr.index(nil)\nend\n\n\nputs arr.join\n"}, {"source_code": "\n\narr = (0..STDIN.gets.to_i-1).map {|i| nil}\n\narr[0] = \"a\"\npre = 0\n(0..arr.length-1).each do |i|\n diff = i-pre\n arr[i] = \"a\" if diff == 4 or diff == 7\nend\n\nif (arr.length > 1)\n c = \"b\"\n pre = 1\n arr[pre] = c\n (0..arr.length-1).each do |i|\n diff = i-pre\n arr[i] = c if (diff == 4 or diff == 7) and arr[i] == nil\n end\nend\n\nif (arr.length > 2)\n c = \"c\"\n pre = 2\n arr[pre] = c\n (0..arr.length-1).each do |i|\n diff = i-pre\n arr[i] = c if (diff == 4 or diff == 7) and arr[i] == nil\n end\nend\n\nif (arr.length > 3)\n c = \"d\"\n pre = 3\n arr[pre] = c\n (0..arr.length-1).each do |i|\n diff = i-pre\n arr[i] = c if (diff == 4 or diff == 7) and arr[i] == nil\n end\nend\n\nputs arr.join\n"}], "src_uid": "94278e9c55f0fc82b48145ebecbc515f"} {"nl": {"description": "As you very well know, the whole Universe traditionally uses three-dimensional Cartesian system of coordinates. In this system each point corresponds to three real coordinates (x, y, z). In this coordinate system, the distance between the center of the Universe and the point is calculated by the following formula: . Mushroom scientists that work for the Great Mushroom King think that the Universe isn't exactly right and the distance from the center of the Universe to a point equals xa·yb·zc.To test the metric of mushroom scientists, the usual scientists offered them a task: find such x, y, z (0 ≤ x, y, z; x + y + z ≤ S), that the distance between the center of the Universe and the point (x, y, z) is maximum possible in the metric of mushroom scientists. The mushroom scientists aren't good at maths, so they commissioned you to do the task.Note that in this problem, it is considered that 00 = 1.", "input_spec": "The first line contains a single integer S (1 ≤ S ≤ 103) — the maximum sum of coordinates of the sought point. The second line contains three space-separated integers a, b, c (0 ≤ a, b, c ≤ 103) — the numbers that describe the metric of mushroom scientists.", "output_spec": "Print three real numbers — the coordinates of the point that reaches maximum value in the metrics of mushroom scientists. If there are multiple answers, print any of them that meets the limitations. A natural logarithm of distance from the center of the Universe to the given point in the metric of mushroom scientists shouldn't differ from the natural logarithm of the maximum distance by more than 10 - 6. We think that ln(0) =  - ∞.", "sample_inputs": ["3\n1 1 1", "3\n2 0 0"], "sample_outputs": ["1.0 1.0 1.0", "3.0 0.0 0.0"], "notes": null}, "positive_code": [{"source_code": "S = gets.to_f\na, b, c = gets.chomp.split.map{|x| x.to_i}\na=1 if a+b+c == 0\nprint S*a/(a+b+c)\nprint ' '\nprint S*b/(a+b+c)\nprint ' '\nprint S*c/(a+b+c)\nputs\n"}], "negative_code": [{"source_code": "S = gets.to_f\na, b, c = gets.chomp.split.map{|x| x.to_i}\nprint S*a/(a+b+c)\nprint ' '\nprint S*b/(a+b+c)\nprint ' '\nprint S*c/(a+b+c)\nputs\n"}], "src_uid": "0a9cabb857949e818453ffe411f08f95"} {"nl": {"description": "Vasily the bear has got a sequence of positive integers a1, a2, ..., an. Vasily the Bear wants to write out several numbers on a piece of paper so that the beauty of the numbers he wrote out was maximum. The beauty of the written out numbers b1, b2, ..., bk is such maximum non-negative integer v, that number b1 and b2 and ... and bk is divisible by number 2v without a remainder. If such number v doesn't exist (that is, for any non-negative integer v, number b1 and b2 and ... and bk is divisible by 2v without a remainder), the beauty of the written out numbers equals -1. Tell the bear which numbers he should write out so that the beauty of the written out numbers is maximum. If there are multiple ways to write out the numbers, you need to choose the one where the bear writes out as many numbers as possible.Here expression x and y means applying the bitwise AND operation to numbers x and y. In programming languages C++ and Java this operation is represented by \"&\", in Pascal — by \"and\".", "input_spec": "The first line contains integer n (1 ≤ n ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ a1 < a2 < ... < an ≤ 109).", "output_spec": "In the first line print a single integer k (k > 0), showing how many numbers to write out. In the second line print k integers b1, b2, ..., bk — the numbers to write out. You are allowed to print numbers b1, b2, ..., bk in any order, but all of them must be distinct. If there are multiple ways to write out the numbers, choose the one with the maximum number of numbers to write out. If there still are multiple ways, you are allowed to print any of them.", "sample_inputs": ["5\n1 2 3 4 5", "3\n1 2 4"], "sample_outputs": ["2\n4 5", "1\n4"], "notes": null}, "positive_code": [{"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\nMath.log2(a.last).to_i.downto(0) do |i|\n d=1<0\n c&=x\n end\n end\n if c==0\n b=a.select{|x|x&d>0}\n puts b.size, b.join(' ')\n exit\n end\nend\n"}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\nb=[]\n30.downto(0) do |i|\n d=1<0\n c&=x\n b<0\n c&=x\n end\n end\n if c==0\n b=a.select{|x|x&d>0}\n puts b.size, b.join(' ')\n exit\n end\nend\n"}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\nb=Array.new(31){|i|1<0\n c[i]<0\n puts c[i].size, c[i].join(' ')\n exit\n end\nend\n\n\n"}], "negative_code": [{"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\nm=1<=m}\nputs a.size,a.join(' ')\n"}], "src_uid": "54c23dda2aa1d58ecf22c03015dca55c"} {"nl": {"description": "Peter likes to travel by train. He likes it so much that on the train he falls asleep. Once in summer Peter was going by train from city A to city B, and as usual, was sleeping. Then he woke up, started to look through the window and noticed that every railway station has a flag of a particular colour.The boy started to memorize the order of the flags' colours that he had seen. But soon he fell asleep again. Unfortunately, he didn't sleep long, he woke up and went on memorizing the colours. Then he fell asleep again, and that time he slept till the end of the journey.At the station he told his parents about what he was doing, and wrote two sequences of the colours that he had seen before and after his sleep, respectively.Peter's parents know that their son likes to fantasize. They give you the list of the flags' colours at the stations that the train passes sequentially on the way from A to B, and ask you to find out if Peter could see those sequences on the way from A to B, or from B to A. Remember, please, that Peter had two periods of wakefulness.Peter's parents put lowercase Latin letters for colours. The same letter stands for the same colour, different letters — for different colours.", "input_spec": "The input data contains three lines. The first line contains a non-empty string, whose length does not exceed 105, the string consists of lowercase Latin letters — the flags' colours at the stations on the way from A to B. On the way from B to A the train passes the same stations, but in reverse order. The second line contains the sequence, written by Peter during the first period of wakefulness. The third line contains the sequence, written during the second period of wakefulness. Both sequences are non-empty, consist of lowercase Latin letters, and the length of each does not exceed 100 letters. Each of the sequences is written in chronological order. ", "output_spec": "Output one of the four words without inverted commas: «forward» — if Peter could see such sequences only on the way from A to B; «backward» — if Peter could see such sequences on the way from B to A; «both» — if Peter could see such sequences both on the way from A to B, and on the way from B to A; «fantasy» — if Peter could not see such sequences. ", "sample_inputs": ["atob\na\nb", "aaacaaa\naca\naa"], "sample_outputs": ["forward", "both"], "notes": "NoteIt is assumed that the train moves all the time, so one flag cannot be seen twice. There are no flags at stations A and B."}, "positive_code": [{"source_code": "a = gets.chomp\nb = a.reverse\n\nf = gets.chomp\ns = gets.chomp\n\nif (k = a.index(f)) && a.index(s,k+f.size) then\n n = \"forward\"\nelse\n n = 0\nend\n\nif (k = b.index(f)) && b.index(s,k+f.size) then\n if n == 0 then\n n = \"backward\"\n else\n n = \"both\"\n end\nelse\n if n == 0\n n = \"fantasy\"\n end\nend\n\nputs n"}, {"source_code": "s, a, b = readlines.map(&:chop)\nr = /#{a}.*#{b}/\nputs %w(fantasy forward backward both)[(s =~ r ? 1 : 0) | (s.reverse =~ r ? 2 : 0)]\n"}, {"source_code": "s, a, b = readlines.map(&:chop)\nr, i = /#{a}.*#{b}/, 0\ni |= 1 if s =~ r\ni |= 2 if s.reverse =~ r\nputs %w(fantasy forward backward both)[i]\n"}, {"source_code": "str1 = gets.chomp!\nstr2 = gets.chomp!\nstr3 = gets.chomp!\nforward = backward = false\nif str1.match(/#{str2}\\w*#{str3}/)\n\tforward = true\nend\nstr11 = str1.reverse\nif str11.match(/#{str2}\\w*#{str3}/)\n\tbackward = true\nend\nif forward && backward\n\tputs 'both'\nelsif forward\n\tputs 'forward'\nelsif backward\n\tputs 'backward'\nelse\n\tputs 'fantasy'\nend\n\n"}, {"source_code": "a=gets.chomp;b=gets.chomp;c=gets.chomp;\n puts %w[fantasy forward backward both][(a.match(b+'.*'+c)?1:0)|(a.match((b+'*.'+c).reverse)?2:0)]\n "}, {"source_code": "def solve(data)\n q=data.index($a)\n if q==nil then\n return false\n end\n r=data.rindex($b)\n if r==nil then\n return false\n end\n return q+$a.length<=r\nend\n\ninput=gets.split[0]\n$a=gets.split[0]\n$b=gets.split[0]\nif solve(input) then\n if solve(input.reverse) then\n puts \"both\"\n else\n puts \"forward\"\n end\nelse\n if solve(input.reverse) then\n puts \"backward\"\n else\n puts \"fantasy\"\n end\nend\n"}, {"source_code": "#!/usr/bin/ruby -Ku\n\ninclude Math\nrequire 'prime'\nrequire 'bigdecimal'\nrequire 'bigdecimal/math'\nrequire 'matrix'\nrequire 'pp'\nrequire 'benchmark'\n\nInf = Float::INFINITY\ndef pbin(obj);case obj;when Integer;puts obj.to_s(2);end;end\nclass String;def to_a;split(\"\");end;\n def to_ia(d = \" \");split(d).map(&:to_i);end;end\ndef getia(*arg);gets.to_ia(*arg);end\ndef geti;gets.to_i;end\nclass Object;def dcopy;Marshal.load(Marshal.dump(self));end;end\n\ns = gets.chomp\na = gets.chomp\nb = gets.chomp\nf = (s =~ Regexp.new(a + \".*\" + b))\ns = s.reverse\nb = (s =~ Regexp.new(a + \".*\" + b))\nif f && b\n puts \"both\"\nelsif f\n puts \"forward\"\nelsif b\n puts \"backward\"\nelse\n puts \"fantasy\"\nend\n"}, {"source_code": "a=gets;b=gets.chomp;c=gets.chomp;puts %w{fantasy forward backward both}[(a.match(b+'.*'+c)?1:0)|(a.match((b+'*.'+c).reverse)?2:0)]\n"}, {"source_code": "a=gets;b=gets.chomp;c=gets.chomp;puts %w{fantasy forward backward both}[(a.match(b+'.*'+c)?1:0)|(a.match((b+'*.'+c).reverse)?2:0)]\n"}, {"source_code": "a=gets;b=gets.chomp;c=gets.chomp;puts %w{fantasy forward backward both}[(a.match(b+'.*'+c)?1:0)|(a.match((b+'*.'+c).reverse)?2:0)]"}, {"source_code": "a=gets;b=gets.chomp;c=gets.chomp;puts %w{fantasy forward backward both}[(a.match(b+'.*'+c)?1:0)|(a.match((b+'*.'+c).reverse)?2:0)]"}, {"source_code": "a=gets;b=gets.chomp;c=gets.chomp;puts %w{fantasy forward backward both}[(a.match(b+'.*'+c)?1:0)|(a.match((b+'*.'+c).reverse)?2:0)]"}, {"source_code": "map = gets.strip\nx = gets.strip\ny = gets.strip\n\nresult = \"fantasy\"\ni = map.index(x)\nif i\n result = \"forward\" if map.index(y, i+x.length)\nend\nmap.reverse!\n\ni = map.index(x)\nif i\n if map.index(y, i+x.length)\n if result == \"forward\"\n result = \"both\"\n else\n result = \"backward\"\n end\n end\nend\nputs result"}, {"source_code": "a=gets;b=gets.chomp;c=gets.chomp;puts %w{fantasy forward backward both}[(a.match(b+'.*'+c)?1:0)|(a.match((b+'*.'+c).reverse)?2:0)]\n"}, {"source_code": "a=gets.chomp\nal=a.length\nb=gets.chomp\nbl=b.length\nc=gets.chomp\ncl=c.length\ntr=0\nif bl+cl<=al\nintr=0\ni=0\naka=b\nakal=bl\nloop do\nif a[i..i+akal-1]==aka\ni=i+akal-1\naka=c\nakal=cl\nintr+=1\nend\ni+=1\nbreak if intr==2\nbreak if i+akal>=al+1\nend \nif intr==2\ntr=1\nend\na.reverse!\nintr=0\ni=0\naka=b\nakal=bl\nloop do\nif a[i..i+akal-1]==aka\ni=i+akal-1\naka=c\nakal=cl\nintr+=1\nend\ni+=1\nbreak if intr==2\nbreak if i+akal>=al+1\nend \nif intr==2 && tr==1\ntr+=2\nelsif intr==2 \ntr=2\nend\nend\nif tr==3\nputs \"both\"\nelsif tr==2\nputs \"backward\"\nelsif tr==1\nputs \"forward\"\nelse\nputs \"fantasy\"\nend"}, {"source_code": "a=gets.chomp\nal=a.length\nb=gets.chomp\nbl=b.length\nc=gets.chomp\ncl=c.length\ntr=0\nif bl+cl<=al\nintr=0\ni=0\naka=b\nakal=bl\nloop do\nif a[i..i+akal-1]==aka\ni=i+akal-1\naka=c\nakal=cl\nintr+=1\nend\ni+=1\nbreak if intr==2\nbreak if i+akal>=al+1\nend \nif intr==2\ntr=1\nend\na.reverse!\nintr=0\ni=0\naka=b\nakal=bl\nloop do\nif a[i..i+akal-1]==aka\ni=i+akal-1\naka=c\nakal=cl\nintr+=1\nend\ni+=1\nbreak if intr==2\nbreak if i+akal>=al+1\nend \nif intr==2 && tr==1\ntr+=2\nelsif intr==2 \ntr=2\nend\nend\nif tr==3\nputs \"both\"\nelsif tr==2\nputs \"backward\"\nelsif tr==1\nputs \"forward\"\nelse\nputs \"fantasy\"\nend"}, {"source_code": "a=gets;b=gets.chomp;c=gets.chomp;puts %w{fantasy forward backward both}[(a.match(b+'.*'+c)?1:0)|(a.match((b+'*.'+c).reverse)?2:0)]\n"}, {"source_code": "a=gets.chomp\nal=a.length\nb=gets.chomp\nbl=b.length\nc=gets.chomp\ncl=c.length\ntr=0\nif bl+cl<=al\nintr=0\ni=0\naka=b\nakal=bl\nloop do\nif a[i..i+akal-1]==aka\ni=i+akal-1\naka=c\nakal=cl\nintr+=1\nend\ni+=1\nbreak if intr==2\nbreak if i+akal>=al+1\nend \nif intr==2\ntr=1\nend\na.reverse!\nintr=0\ni=0\naka=b\nakal=bl\nloop do\nif a[i..i+akal-1]==aka\ni=i+akal-1\naka=c\nakal=cl\nintr+=1\nend\ni+=1\nbreak if intr==2\nbreak if i+akal>=al+1\nend \nif intr==2 && tr==1\ntr+=2\nelsif intr==2 \ntr=2\nend\nend\nif tr==3\nputs \"both\"\nelsif tr==2\nputs \"backward\"\nelsif tr==1\nputs \"forward\"\nelse\nputs \"fantasy\"\nend"}, {"source_code": "a=gets;b=gets.chomp;c=gets.chomp;puts %w{fantasy forward backward both}[(a.match(b+'.*'+c)?1:0)|(a.match((b+'*.'+c).reverse)?2:0)]"}, {"source_code": "def test(a, a1, a2)\n i = a.index(a1)\n if !i.nil? then\n b = a[i+a1.size..a.size]\n return true if !b[a2].nil?\n end\n return false\nend\na = gets.strip\na1 = gets.strip\na2= gets.strip\nt1 = test(a, a1, a2)\na.reverse!\nt2 = test(a, a1, a2)\nif t1 then\n if t2 then\n puts \"both\"\n else\n puts \"forward\"\n end\nelse\n if t2 then\n puts \"backward\"\n else\n puts \"fantasy\"\n end\nend\n\n"}, {"source_code": "flags = gets.chomp\nline2 = gets.chomp\nline3 = gets.chomp\nflag2 = (flags =~ /#{line2}/)\nflag3 = (flags[(flag2+line2.size)..-1] =~ /#{line3}/) if flag2\nforward = true if flag2 && flag3\n\nrflags = flags.reverse\nflag2 = (rflags =~ /#{line2}/)\nflag3 = (rflags[(flag2+line2.size)..-1] =~ /#{line3}/) if flag2\nbackward = true if flag2 && flag3\n\nif forward && backward\n puts \"both\"\nelsif forward\n puts \"forward\"\nelsif backward\n puts \"backward\"\nelse \n puts \"fantasy\"\nend\n\n"}, {"source_code": "line, seq1, seq2, forward, backward = gets().chomp!, gets().chomp!, gets().chomp!, false, false\nif /#{seq1}.*#{seq2}/.match(line) then forward = true end\nif /#{seq2.reverse!}.*#{seq1.reverse!}/.match(line) then backward = true end\nif forward && backward then puts( \"both\" ) elsif forward then puts( \"forward\" ) elsif backward then puts( \"backward\" ) else puts( \"fantasy\" ) end\n\n"}, {"source_code": "sequence = gets.chomp\nfirst = gets.chomp\nsecond = gets.chomp\n\nresult = \"fantasy\"\nindex = sequence.index(first)\nif index\n\tresult = \"forward\" if sequence.index(second, index + first.length) \nend\n\nsequence.reverse!\n\nindex = sequence.index(first)\nif index\n\tif sequence.index(second, index + first.length)\n\t\tresult = (result == \"forward\") ? \"both\" : \"backward\"\n\tend\nend\n\nputs result"}, {"source_code": "a=gets;b=gets.chomp;c=gets.chomp;puts %w{fantasy forward backward both}[(a.match(b+'.*'+c)?1:0)|(a.match((b+'*.'+c).reverse)?2:0)]\n"}, {"source_code": "#!/usr/bin/env ruby\n\ntxt = gets.chomp\nstr1 = gets.chomp\nstr2 = gets.chomp\n\nforward = backward = false\n\nif txt =~ /#{str1}.*#{str2}/\n forward = true\nend\n\ntxt.reverse!\n\nif txt =~ /#{str1}.*#{str2}/\n backward = true\nend\n\nif forward == true and backward == true\n puts \"both\"\nelsif forward == true and backward == false\n puts \"forward\"\nelsif forward == false and backward == true\n puts \"backward\"\nelse\n puts \"fantasy\"\nend\n"}, {"source_code": "map = gets.strip\nx = gets.strip\ny = gets.strip\n\nresult = \"fantasy\"\ni = map.index(x)\nif i\n result = \"forward\" if map.index(y, i+x.length)\nend\nmap.reverse!\n\ni = map.index(x)\nif i\n if map.index(y, i+x.length)\n if result == \"forward\"\n result = \"both\"\n else\n result = \"backward\"\n end\n end\nend\nputs result"}, {"source_code": "flags = gets.chomp!\n\ns1 = gets.chomp!\ns2 = gets.chomp!\n\nforward = false\nbackward = false\nfantasy = false\n\nif (flags.index(s1) != nil && flags.index(s2, flags.index(s1)+s1.length) != nil)\n forward = true\nelse\n forward = false\nend\n\nif (flags.reverse.index(s1) != nil && flags.reverse.index(s2, flags.reverse.index(s1)+s1.length))\n backward = true\nelse\n backward = false\nend\n\n\nif backward && forward\n puts \"both\"\nelse\n if forward\n puts \"forward\"\n else\n if backward\n puts \"backward\"\n else\n puts \"fantasy\"\n end\n end\nend\n"}, {"source_code": "a=gets;b=gets.chomp;c=gets.chomp;puts %w{fantasy forward backward both}[(a.match(b+'.*'+c)?1:0)|(a.match((b+'*.'+c).reverse)?2:0)]"}, {"source_code": "a=gets;b=gets.chomp;c=gets.chomp;puts %w{fantasy forward backward both}[(a.match(b+'.*'+c)?1:0)|(a.match((b+'*.'+c).reverse)?2:0)]\n"}, {"source_code": "a=gets;b=gets.chomp;c=gets.chomp;puts %w{fantasy forward backward both}[(a.match(b+'.*'+c)?1:0)|(a.match((b+'*.'+c).reverse)?2:0)]\n"}], "negative_code": [{"source_code": "a = gets.chomp\nb = a.reverse\n\nf = gets.chomp\ns = gets.chomp\n\nif (k = a.index(f)) && a.index(s,k+1) then\n n = \"forward\"\nelse\n n = 0\nend\n\nif (k = b.index(f)) && b.index(s,k+1) then\n if n == 0 then\n n = \"backward\"\n else\n n = \"both\"\n end\nelse\n if n == 0\n n = \"fantasy\"\n end\nend\n\nputs n"}, {"source_code": "s, a, b = readlines.map(&:chop)\nr, i = /#{gets}.*#{gets}/, 0\ni |= 1 if s =~ r\ni |= 2 if s.reverse =~ r\nputs %w(fantasy forward backward both)[i]\n"}, {"source_code": "a=gets.chomp;b=gets.chomp;c=gets.chomp;\n puts %w[fantasy foreward backward both][(a.match(b+'.*'+c)?1:0)|(a.match((b+'*.'+c).reverse)?2:0)]\n "}, {"source_code": "a=gets.chomp\nal=a.length\nb=gets.chomp\nbl=b.length\nc=gets.chomp\ncl=c.length\ntr=0\nif bl+cl<=cl\nintr=0\ni=0\naka=b\nakal=bl\nloop do\nif a[i..i+akal-1]==b\naka=c\nakal=cl\nintr+=1\ni=i+aka-1\nend\ni+=1\nbreak if intr==2\nbreak if i+akal==al+1\nend \nif intr==2\ntr=1\nend\na.reverse!\nintr=0\nloop do\nif a[i..i+akal-1]==bl\naka=cl\nakal=cl\nintr+=1\ni=i+aka-1\nend\ni+=1\nbreak if intr==2\nbreak if i+akal==al+1\nend \nif intr==2 && tr==1\ntr+=2\nelse \ntr=2\nend\nend\nif tr==3\nputs \"both\"\nelsif tr==2\nputs \"backward\"\nelsif tr==1\nputs \"forward\"\nelse\nputs \"fantasy\"\nend"}, {"source_code": "a=gets.chomp\nal=a.length\nb=gets.chomp\nbl=b.length\nc=gets.chomp\ncl=c.length\ntr=0\nif bl+cl<=al\nintr=0\ni=0\naka=b\nakal=bl\nloop do\nif a[i..i+akal-1]==aka\naka=c\nakal=cl\nintr+=1\ni=i+akal-1\nend\ni+=1\nbreak if intr==2\nbreak if i+akal==al+1\nend \nif intr==2\ntr=1\nend\na.reverse!\nintr=0\ni=0\naka=b\nakal=bl\nloop do\nif a[i..i+akal-1]==aka\naka=cl\nakal=cl\nintr+=1\ni=i+akal-1\nend\ni+=1\nbreak if intr==2\nbreak if i+akal==al+1\nend \nif intr==2 && tr==1\ntr+=2\nelsif intr==2 \ntr=2\nend\nend\nif tr==3\nputs \"both\"\nelsif tr==2\nputs \"backward\"\nelsif tr==1\nputs \"forward\"\nelse\nputs \"fantasy\"\nend"}, {"source_code": "def test(a, a1, a2)\n i = a.index(a1)\n if !i.nil? then\n b = a[i+a1.size..a.size]\n return true if !b[a2].nil?\n end\n return false\nend\na = gets.strip\na1 = gets.strip\na2= gets.strip\nt1 = test(a,a1,a2)\na.reverse!\na1.reverse!\na2.reverse!\nt2 = test(a, a2, a1)\nif t1 then\n if t2 then\n puts \"both\"\n else\n puts \"forward\"\n end\nelse\n if t2 then\n puts \"backward\"\n else\n puts \"fantasy\"\n end\nend\n\n"}, {"source_code": "def test(a, a1, a2)\n i = a.index(a1)\n if !i.nil? then\n b = a[i+a1.size..a.size]\n return true if !b[a2].nil?\n end\n return false\nend\na = gets.strip\na1 = gets.strip\na2= gets.strip\nt1 = test(a,a1,a2)\na.reverse!\na1.reverse!\na.reverse!\nt2 = test(a, a2, a1)\nif t1 then\n if t2 then\n puts \"both\"\n else\n puts \"forward\"\n end\nelse\n if t2 then\n puts \"backward\"\n else\n puts \"fantasy\"\n end\nend\n\n"}, {"source_code": "def test(a, a1, a2)\n i = a.index(a1)\n if !i.nil? then\n b = a[i+a1.size..a.size]\n p b\n return true if !b[a2].nil?\n end\n return false\nend\na = gets.strip\na1 = gets.strip\na2= gets.strip\nt1 = test(a,a1,a2)\na.reverse!\na1.reverse!\na2.reverse!\nt2 = test(a, a2, a1)\nif t1 then\n if t2 then\n puts \"both\"\n else\n puts \"forward\"\n end\nelse\n if t2 then\n puts \"backward\"\n else\n puts \"fantasy\"\n end\nend\n\n"}, {"source_code": "#!/usr/bin/env ruby\n\ntxt = gets.chomp\nstr1 = gets.chomp\nstr2 = gets.chomp\n\nforward = backward = false\n\nif txt =~ /#{str1}.*#{str2}/\n forward = true\nend\n\ntxt.reverse!\nstr1.reverse!\nstr2.reverse!\n\nif txt =~ /#{str2}.*#{str1}/\n backward = true\nend\n\nif forward == true and backward == true\n puts \"both\"\nelsif forward == true and backward == false\n puts \"forward\"\nelsif forward == false and backward == true\n puts \"backward\"\nelse\n puts \"fantasy\"\nend\n"}, {"source_code": "#!/usr/bin/env ruby\n\ntxt = gets.chomp\nstr1 = gets.chomp\nstr2 = gets.chomp\n\nforward = backward = false\n\nif txt =~ /#{str1}.*#{str2}/\n forward = true\nend\n\ntxt.reverse!\nstr1.reverse!\nstr2.reverse!\n\nif txt =~ /#{str2}.*#{str2}/\n backward = true\nend\n\nif forward == true and backward == true\n puts \"both\"\nelsif forward == true and backward == false\n puts \"forward\"\nelsif forward == false and backward == true\n puts \"backward\"\nelse\n puts \"fantasy\"\nend\n"}, {"source_code": "#!/usr/bin/env ruby\n\ntxt = gets.chomp\nstr1 = gets.chomp\nstr2 = gets.chomp\n\nforward = backward = false\n\nif txt =~ /#{str1}.*#{str2}/\n forward = true\nend\n\n#txt.reverse!\n#str1.reverse!\n#str2.reverse!\n\nif txt =~ /#{str2}.*#{str1}/\n backward = true\nend\n\nif forward == true and backward == true\n puts \"both\"\nelsif forward == true and backward == false\n puts \"forward\"\nelsif forward == false and backward == true\n puts \"backward\"\nelse\n puts \"fantasy\"\nend\n"}, {"source_code": "#$stdin = File.open('in.txt')\nmap = gets.strip\nx = gets.strip\ny = gets.strip\n\nlast_x = nil\nresult = \"fantasy\"\ni = 0\nj = 0\n\n\n\nwhile i < map.length and j < x.length\n j += 1 if map[i] == x[j]\n i += 1\nend\n\nif j == x.length\n j = 0\n while i < map.length and j < y.length\n j += 1 if map[i] == y[j]\n i += 1\n end\n if j == y.length\n result = \"forward\"\n end\nend\n\ni = map.length-1\nj = x.length-1\n\nwhile i >= 0 and j >= 0\n j -= 1 if map[i] == x[j]\n i -= 1\nend\n\nif j == -1\n j = y.length-1\n while i >= 0 and j >= 0\n j -= 1 if map[i] == y[j]\n i -= 1\n end\n if j == -1\n if result == \"forward\"\n result = \"both\"\n else\n result = \"backward\"\n end\n end\nend\n\nputs result"}, {"source_code": "#$stdin = File.open('in.txt')\nmap = gets.strip\nx = gets.strip\ny = gets.strip\n\nresult = \"fantasy\"\ni = map.index(x)\nif i\n result = \"forward\" if map.index(y, i+1)\nend\nx.reverse!\ny.reverse!\n\ni = map.rindex(x)\nif i\n if map.rindex(y, y.length-(i+1))\n if result == \"forward\"\n result = \"both\"\n else\n result = \"backward\"\n end\n end\nend\n# i = 0\n# j = 0\n# find = false\n# while i < map.length and !find\n# if map[i] == x[0]\n# j = 0\n# while i < map.length and j < x.length and map[i] == x[j]\n# i += 1\n# j += 1\n# end\n# find = j == x.length\n# else\n# i += 1 \n# end\n# end\n# \n# if find\n# find = false\n# while i < map.length and !find\n# if map[i] == y[0]\n# j = 0\n# while i < map.length and j < y.length and map[i] == y[j]\n# i += 1\n# j += 1\n# end\n# find = j == y.length\n# else\n# i += 1 \n# end\n# end\n# if find\n# result = \"forward\"\n# end\n# end\n# \n# i = map.length-1\n# find = false\n# \n# while i >= 0 and !find\n# if map[i] == x[0]\n# j = 0\n# while i >=0 and j < x.length and map[i] == x[j]\n# i -= 1\n# j += 1\n# end\n# find = j == x.length\n# else\n# i -= 1 \n# end\n# end\n# \n# if find\n# find = false\n# while i >= 0 and !find\n# if map[i] == y[0]\n# j = 0\n# while i >=0 and j < y.length and map[i] == y[j]\n# i -= 1\n# j += 1\n# end\n# find = j == y.length\n# else\n# i -= 1 \n# end\n# end\n# if find\n# if result == \"forward\"\n# result = \"both\"\n# else\n# result = \"backward\"\n# end\n# end\n# end\n\nputs result"}, {"source_code": "#$stdin = File.open('in.txt')\nmap = gets.strip\nx = gets.strip\ny = gets.strip\n\nresult = \"fantasy\"\ni = 0\nj = 0\nfind = false\nwhile i < map.length and !find\n if map[i] == x[0]\n j = 0\n while i < map.length and j < x.length and map[i] == x[j]\n i += 1\n j += 1\n end\n find = j == x.length\n else\n i += 1 \n end\nend\n\nif find\n find = false\n while i < map.length and !find\n if map[i] == y[0]\n j = 0\n while i < map.length and j < y.length and map[i] == y[j]\n i += 1\n j += 1\n end\n find = j == y.length\n else\n i += 1 \n end\n end\n if find\n result = \"forward\"\n end\nend\n\ni = map.length-1\nfind = false\n\nwhile i >= 0 and !find\n if map[i] == x[0]\n j = 0\n while i >=0 and j < x.length and map[i] == x[j]\n i -= 1\n j += 1\n end\n find = j == x.length\n else\n i -= 1 \n end\nend\n\nif find\n find = false\n while i >= 0 and !find\n if map[i] == y[0]\n j = 0\n while i >=0 and j < y.length and map[i] == y[j]\n i -= 1\n j += 1\n end\n find = j == y.length\n else\n i -= 1 \n end\n end\n if find\n if result == \"forward\"\n result = \"both\"\n else\n result = \"backward\"\n end\n end\nend\n\nputs result"}, {"source_code": "#$stdin = File.open('in.txt')\nmap = gets.strip\nx = gets.strip\ny = gets.strip\n\nresult = \"fantasy\"\ni = 0\nj = 0\n\nwhile i < map.length and j < x.length\n j += 1 if map[i] == x[j]\n i += 1\nend\n\nif j == x.length\n j = 0\n while i < map.length and j < y.length\n j += 1 if map[i] == y[j]\n i += 1\n end\n if j == y.length\n result = \"forward\"\n end\nend\n\ni = map.length-1\nj = 0\n\nwhile i >= 0 and j < x.length\n j += 1 if map[i] == x[j]\n i -= 1\nend\n\nif j == x.length\n j = 0\n while i >= 0 and j < y.length\n j += 1 if map[i] == y[j]\n i -= 1\n end\n if j == y.length\n if result == \"forward\"\n result = \"both\"\n else\n result = \"backward\"\n end\n end\nend\n\nputs result"}, {"source_code": "flags = gets.chomp!\n\ns1 = gets.chomp!\ns2 = gets.chomp!\n\nforward = false\nbackward = false\nfantasy = false\n\nfi1 = flags.index(s1)\nbi1 = flags.reverse.index(s1)\n\nif (fi1 != nil)\n fi2 = flags.index(s2, fi1+s1.length)\n if (fi2 != nil && fi1 <= fi2)\n forward = true\n else\n forward = false\n end\nelse\n fantasy = true\nend\n\nif (bi1 != nil)\n bi2 = flags.reverse.index(s2, bi1+s1.length)\n if (bi2 != nil && bi1+s1.length <= bi2)\n backward = true\n else\n backward = false\n end\nelse\n fantasy = true\nend\n\nif fantasy\n puts \"fantasy\"\nelse\n if backward && forward\n puts \"both\"\n else\n if forward\n puts \"forward\"\n else\n if backward\n puts \"backward\"\n else\n puts \"fantasy\"\n end\n end\n end\nend"}], "src_uid": "c3244e952830643938d51ce14f043d7d"} {"nl": {"description": "Last year the world's largest square was built in Berland. It is known that the square can be represented as an infinite plane with an introduced Cartesian system of coordinates. On that square two sets of concentric circles were painted. Let's call the set of concentric circles with radii 1, 2, ..., K and the center in the point (z, 0) a (K, z)-set. Thus, on the square were painted a (N, x)-set and a (M, y)-set. You have to find out how many parts those sets divided the square into.", "input_spec": "The first line contains integers N, x, M, y. (1 ≤ N, M ≤ 100000,  - 100000 ≤ x, y ≤ 100000, x ≠ y).", "output_spec": "Print the sought number of parts.", "sample_inputs": ["1 0 1 1", "1 0 1 2", "3 3 4 7"], "sample_outputs": ["4", "3", "17"], "notes": "NotePicture for the third sample: "}, "positive_code": [{"source_code": "n,x,m,y = gets.chomp.split(/ /).map!{|x| x.to_i}\nif x > y\n\tx,y = y,x\n\tn,m = m,n\nend\n\nret = n+1\nfor i in 1..m\n ly=y-i-x\n\try=y+i-x\n if (ly>=n || ly<=-n)\n\t\tret+=1\n\telsif (ly>=0 && ry>=0)\n\t ry=[n,ry-1].min\n ret+=2*(ry-ly)\n else\n ry=[n,ry-1].min\n ret +=2*(ry+ly)+1\n end\nend\n\nputs ret"}], "negative_code": [], "src_uid": "ebaf9444531bb6ba6c3322dfa8edb69c"} {"nl": {"description": "Polycarp found under the Christmas tree an array $$$a$$$ of $$$n$$$ elements and instructions for playing with it: At first, choose index $$$i$$$ ($$$1 \\leq i \\leq n$$$) — starting position in the array. Put the chip at the index $$$i$$$ (on the value $$$a_i$$$). While $$$i \\leq n$$$, add $$$a_i$$$ to your score and move the chip $$$a_i$$$ positions to the right (i.e. replace $$$i$$$ with $$$i + a_i$$$). If $$$i > n$$$, then Polycarp ends the game. For example, if $$$n = 5$$$ and $$$a = [7, 3, 1, 2, 3]$$$, then the following game options are possible: Polycarp chooses $$$i = 1$$$. Game process: $$$i = 1 \\overset{+7}{\\longrightarrow} 8$$$. The score of the game is: $$$a_1 = 7$$$. Polycarp chooses $$$i = 2$$$. Game process: $$$i = 2 \\overset{+3}{\\longrightarrow} 5 \\overset{+3}{\\longrightarrow} 8$$$. The score of the game is: $$$a_2 + a_5 = 6$$$. Polycarp chooses $$$i = 3$$$. Game process: $$$i = 3 \\overset{+1}{\\longrightarrow} 4 \\overset{+2}{\\longrightarrow} 6$$$. The score of the game is: $$$a_3 + a_4 = 3$$$. Polycarp chooses $$$i = 4$$$. Game process: $$$i = 4 \\overset{+2}{\\longrightarrow} 6$$$. The score of the game is: $$$a_4 = 2$$$. Polycarp chooses $$$i = 5$$$. Game process: $$$i = 5 \\overset{+3}{\\longrightarrow} 8$$$. The score of the game is: $$$a_5 = 3$$$. Help Polycarp to find out the maximum score he can get if he chooses the starting index in an optimal way.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$1 \\leq n \\leq 2 \\cdot 10^5$$$) — the length of the array $$$a$$$. The next line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\leq a_i \\leq 10^9$$$) — elements of the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, output on a separate line one number — the maximum score that Polycarp can get by playing the game on the corresponding array according to the instruction from the statement. Note that Polycarp chooses any starting position from $$$1$$$ to $$$n$$$ in such a way as to maximize his result.", "sample_inputs": ["4\n5\n7 3 1 2 3\n3\n2 1 4\n6\n2 1000 2 3 995 1\n5\n1 1 1 1 1"], "sample_outputs": ["7\n6\n1000\n5"], "notes": "NoteThe first test case is explained in the statement.In the second test case, the maximum score can be achieved by choosing $$$i = 1$$$.In the third test case, the maximum score can be achieved by choosing $$$i = 2$$$.In the fourth test case, the maximum score can be achieved by choosing $$$i = 1$$$."}, "positive_code": [{"source_code": "gets.to_i.times {\n n = gets.to_i\n xs = gets.split.map!(&:to_i)\n ys = Array.new(n) { 0 }\n ans = 0\n (0...n).reverse_each { |i|\n ys[i] = xs[i]\n ys[i] += ys[i + xs[i]] if i + xs[i] < n\n ans = ys[i] if ys[i] > ans\n }\n p ans\n}\n"}, {"source_code": "def solve\r\n n = gets.to_i\r\n a = gets.split.map(&:to_i)\r\n\r\n dp = Array.new(n + 1, 0)\r\n (n - 1).downto(0) do |i|\r\n dp[i] = a[i]\r\n dp[i] += dp[i + a[i]] if i + a[i] < n\r\n end\r\n # STDERR.puts dp.inspect\r\n puts dp.max\r\nend\r\n\r\nt = gets.to_i\r\nt.times do\r\n solve\r\nend"}, {"source_code": "t = gets.chomp.to_i\r\nt.times do\r\n n = gets.chomp.to_i\r\n a = gets.chomp.split(\" \").map(&:to_i)\r\n arr = Array.new(n,0)\r\n max = 0\r\n n.downto(1) do |i|\r\n ans = 0\r\n ii = i.dup\r\n ans = ii + a[ii-1]\r\n if ans > n\r\n arr[ii-1] += (ans - ii)\r\n max = [arr[i-1],max].max\r\n else\r\n arr[ii-1] += (ans + arr[ans-1] - ii)\r\n max = [arr[i-1],max].max\r\n end\r\n #end\r\n end\r\n puts max\r\nend"}, {"source_code": "#!/usr/bin/env ruby\r\n\r\ngets.to_i.times {\r\n n = gets.to_i\r\n nums = gets.split.map &:to_i\r\n max_num = 0\r\n dp = Array.new(n)\r\n (n - 1).downto(0).each do |index|\r\n nk = index + nums[index]\r\n if nk >= dp.size\r\n dp[index] = nums[index]\r\n else\r\n nk = index + nums[index]\r\n #puts \"(#{index}, #{nk}) => (#{nums[index]}, #{nums[nk]})\"\r\n dp[index] = nums[index] + dp[nk]\r\n end\r\n end\r\n #puts nums.to_s\r\n #puts dp.to_s\r\n puts dp.max\r\n}"}, {"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n a = gets.split.map &:to_i\r\n dp = a\r\n (0...n - 1).reverse_each do |i|\r\n dp[i] += dp[i + a[i]] if i + a[i] < n\r\n end\r\n puts dp.max\r\nend"}, {"source_code": "require 'prime'\r\nt = gets.chomp.to_i\r\nt.times do\r\n n = gets.chomp.to_i\r\n arr = gets.chomp.split(' ').map(&:to_i)\r\n brr = [0] * (arr.size + 1)\r\n arr.each_with_index do |el, ind|\r\n new_ind = [ind + el, arr.size].min\r\n brr[new_ind] = [brr[new_ind], brr[ind] + el].max \r\n end\r\n puts brr.last\r\nend"}, {"source_code": "gets.to_i.times do\r\n n=gets.to_i\r\n a=gets.split.map(&:to_i)\r\n (n-1).downto(0) do |i|\r\n if i+a[i] 1\n\n# if n.index(\"8888888888888888888888888888888888888888888888888888888888888888\")\n# \tputs \"#{final}\"\t\n# else\n\tputs ans\n# end\n\n\n# n = gets.chomp\n# arr = Array.new(n.length-1,0)\n\n# i = 1\n# while i < n.length\n# \tif n[i].to_i + n[i-1].to_i == 9\n# \t\tarr[i] = 1\n# \t\ti += 1 \n# \tend\n# \ti += 1\n# end\n\n\n# puts \"#{arr}\"\n# final = []\n# arr.chunk{|x| x}.each do |r|\n# \t# puts \"#{r}\"\n# \tl = r[1].length\n# \tif r[0] == 1\n# \t\tputs \"#{r}\"\n# \t\tif l.even?\n# \t\t\tc = (l/2) + 1\n# \t\telsif l == 1\n# \t\t\tc = 1\n# \t\telse\n# \t\t\tc = l/2\n# \t\tend\n# \t\tfinal << c\n# \tend\n# end\n# ans = 1\n# # puts \"#{final}\"\n# ans *= final.inject(1,:*) if final.length > 1\n# puts ans"}, {"source_code": "def run(a)\n cnt = 0\n acm = 1\n a[0...-1].each_with_index do |x,i|\n if x + a[i+1] == 9\n cnt = cnt + 1\n elsif cnt > 0\n acm = acm*((cnt/2)+1) if cnt % 2 == 0\n cnt = 0\n end\n end\n acm = acm*((cnt/2)+1) if cnt > 0 and cnt % 2 == 0\n acm\nend\n\nSTDIN.read.split(\"\\n\").each_with_index do |line,i|\n puts run(line.split(//).map{|x| x.to_i}).to_s\nend"}], "negative_code": [{"source_code": "n = gets.chomp\narr = Array.new(n.length-1,0)\n\nn.chars.each_cons(2).each_with_index do |a,i|\n\t# puts \"#{a[0]} #{a[1]}\"\n\tif a[0].to_i + a[1].to_i == 9\n\t\tarr[i] = 1\n\tend\nend\n# puts \"#{arr}\"\nfinal = []\narr.chunk{|x| x}.each do |r|\n\t# puts \"#{r}\"\n\tl = r[1].length\n\tif r[0] == 1\n\t\t# puts \"#{l}\"\n\t\tif l.even?\n\t\t\tc = (l/2) + 1\n\t\telsif l == 1\n\t\t\tc = 1\n\t\telse\n\t\t\tc = l/2\n\t\tend\n\t\tfinal << c\n\tend\nend\nans = 1\n# puts \"#{final}\"\nans *= final.inject(1,:*) if final.length > 1\nputs ans"}, {"source_code": "n = gets.chomp\narr = Array.new(n.length,0)\n\nn.chars.each_cons(2).each_with_index do |a,i|\n\t# puts \"#{a[0]} #{a[1]}\"\n\tif a[0].to_i + a[1].to_i == 9\n\t\tarr[i] = 1\n\tend\nend\n# puts \"#{arr}\"\ncount = 1\narr.chunk{|x| x}.each do |r|\n\t# puts \"#{r}\"\n\tl = r[1].length\n\tif r[0] == 1 && l > 1\n\t\t# puts \"#{l}\"\n\t\tif l.even?\n\t\t\tcount *= (l/2) + 1\n\t\telse\n\t\t\tcount *= l/2\n\t\tend\n\tend\nend\nputs count"}, {"source_code": "n = gets.chomp\narr = Array.new(n.length-1,0)\n\nn.chars.each_cons(2).each_with_index do |a,i|\n\t# puts \"#{a[0]} #{a[1]}\"\n\tif a[0].to_i + a[1].to_i == 9\n\t\tarr[i] = 1\n\tend\nend\n# puts \"#{arr}\"\nfinal = []\narr.chunk{|x| x}.each do |r|\n\t# puts \"#{r}\"\n\tl = r[1].length + 1\n\tif r[0] == 1\n\t\t# puts \"#{r}\"\n\t\tif l.odd?\n\t\t\tc = l/2 + 1\t\t\t\n\t\telse\n\t\t\tc = 1\n\t\tend\n\t\tfinal << c\n\tend\nend\nans = 1\n# puts \"#{final}\"\nans *= final.inject(1,:*) if final.length > 1\n\nif n.index(\"8888888888888888888888888888888888888888888888888888888888888888\")\n\tputs \"#{final}\"\t\nelse\n\tputs ans\nend\n\n\n# n = gets.chomp\n# arr = Array.new(n.length-1,0)\n\n# i = 1\n# while i < n.length\n# \tif n[i].to_i + n[i-1].to_i == 9\n# \t\tarr[i] = 1\n# \t\ti += 1 \n# \tend\n# \ti += 1\n# end\n\n\n# puts \"#{arr}\"\n# final = []\n# arr.chunk{|x| x}.each do |r|\n# \t# puts \"#{r}\"\n# \tl = r[1].length\n# \tif r[0] == 1\n# \t\tputs \"#{r}\"\n# \t\tif l.even?\n# \t\t\tc = (l/2) + 1\n# \t\telsif l == 1\n# \t\t\tc = 1\n# \t\telse\n# \t\t\tc = l/2\n# \t\tend\n# \t\tfinal << c\n# \tend\n# end\n# ans = 1\n# # puts \"#{final}\"\n# ans *= final.inject(1,:*) if final.length > 1\n# puts ans"}, {"source_code": "n = gets.chomp\narr = Array.new(n.length-1,0)\n\nn.chars.each_cons(2).each_with_index do |a,i|\n\t# puts \"#{a[0]} #{a[1]}\"\n\tif a[0].to_i + a[1].to_i == 9\n\t\tarr[i] = 1\n\tend\nend\n# puts \"#{arr}\"\nfinal = []\narr.chunk{|x| x}.each do |r|\n\t# puts \"#{r}\"\n\tl = r[1].length + 1\n\tif r[0] == 1\n\t\t# puts \"#{r}\"\n\t\tif l.odd?\n\t\t\tc = l/2 + 1\t\t\t\n\t\telse\n\t\t\tc = 1\n\t\tend\n\t\tfinal << c\n\tend\nend\nans = 1\n# puts \"#{final}\"\nans *= final.inject(1,:*) if final.length > 1\nputs ans\n\n\n# n = gets.chomp\n# arr = Array.new(n.length-1,0)\n\n# i = 1\n# while i < n.length\n# \tif n[i].to_i + n[i-1].to_i == 9\n# \t\tarr[i] = 1\n# \t\ti += 1 \n# \tend\n# \ti += 1\n# end\n\n\n# puts \"#{arr}\"\n# final = []\n# arr.chunk{|x| x}.each do |r|\n# \t# puts \"#{r}\"\n# \tl = r[1].length\n# \tif r[0] == 1\n# \t\tputs \"#{r}\"\n# \t\tif l.even?\n# \t\t\tc = (l/2) + 1\n# \t\telsif l == 1\n# \t\t\tc = 1\n# \t\telse\n# \t\t\tc = l/2\n# \t\tend\n# \t\tfinal << c\n# \tend\n# end\n# ans = 1\n# # puts \"#{final}\"\n# ans *= final.inject(1,:*) if final.length > 1\n# puts ans"}, {"source_code": "def run(a)\n cnt = 0\n acm = 1\n a[0...-1].each_with_index do |x,i|\n if x + a[i+1] == 9\n cnt = cnt + 1\n elsif cnt > 0\n acm = acm*cnt if cnt % 2 == 0\n cnt = 0\n end\n end\n acm = acm*cnt if cnt > 0 and cnt % 2 == 0\n acm\nend\n\nSTDIN.read.split(\"\\n\").each_with_index do |line,i|\n puts run(line.split(//).map{|x| x.to_i}).to_s\nend"}, {"source_code": "def run(a)\n a = a.map{|x| x.to_i}\n cnt = 0\n acm = 1\n a[0...-1].each_with_index do |x,i|\n if x + a[i+1] == 9\n cnt = cnt + 1\n elsif cnt > 0\n acm = acm*cnt if cnt % 2 == 1\n cnt = 0\n end\n end\n acm = acm*cnt if cnt % 2 == 1\n acm\nend\n\nSTDIN.read.split(\"\\n\").each_with_index do |line,i|\n #puts run(line.split(//).map{|x| x.to_i}).to_s\nend\n\nputs run(\"18\".split(//)).to_s\nputs run(\"181\".split(//)).to_s\nputs run(\"1818\".split(//)).to_s\nputs run(\"18181\".split(//)).to_s\nputs run(\"181818\".split(//)).to_s\nputs run(\"1818181\".split(//)).to_s\nputs run(\"18181818\".split(//)).to_s\nputs run(\"181818181\".split(//)).to_s\n\n\n"}, {"source_code": "def run(a)\n cnt = 0\n acm = 1\n a[0...-1].each_with_index do |x,i|\n if x + a[i+1] == 9\n cnt = cnt + 1\n elsif cnt > 0\n acm = acm*cnt\n cnt = 0\n end\n end\n acm = acm*cnt if cnt > 0\n acm\nend\n\nSTDIN.read.split(\"\\n\").each_with_index do |line,i|\n puts run(line.split(//).map{|x| x.to_i}).to_s\nend"}], "src_uid": "bb1e110a7f53e6f7d43ddced7407f3d1"} {"nl": {"description": "Everyone knows that agents in Valorant decide, who will play as attackers, and who will play as defenders. To do that Raze and Breach decided to play $$$t$$$ matches of a digit game...In each of $$$t$$$ matches of the digit game, a positive integer is generated. It consists of $$$n$$$ digits. The digits of this integer are numerated from $$$1$$$ to $$$n$$$ from the highest-order digit to the lowest-order digit. After this integer is announced, the match starts.Agents play in turns. Raze starts. In one turn an agent can choose any unmarked digit and mark it. Raze can choose digits on odd positions, but can not choose digits on even positions. Breach can choose digits on even positions, but can not choose digits on odd positions. The match ends, when there is only one unmarked digit left. If the single last digit is odd, then Raze wins, else Breach wins.It can be proved, that before the end of the match (for every initial integer with $$$n$$$ digits) each agent has an ability to make a turn, i.e. there is at least one unmarked digit, that stands on a position of required parity.For each of $$$t$$$ matches find out, which agent wins, if both of them want to win and play optimally.", "input_spec": "First line of input contains an integer $$$t$$$ $$$(1 \\le t \\le 100)$$$  — the number of matches. The first line of each match description contains an integer $$$n$$$ $$$(1 \\le n \\le 10^3)$$$  — the number of digits of the generated number. The second line of each match description contains an $$$n$$$-digit positive integer without leading zeros.", "output_spec": "For each match print $$$1$$$, if Raze wins, and $$$2$$$, if Breach wins.", "sample_inputs": ["4\n1\n2\n1\n3\n3\n102\n4\n2069"], "sample_outputs": ["2\n1\n1\n2"], "notes": "NoteIn the first match no one can make a turn, the only digit left is $$$2$$$, it's even, so Breach wins.In the second match the only digit left is $$$3$$$, it's odd, so Raze wins.In the third match Raze can mark the last digit, after that Breach can only mark $$$0$$$. $$$1$$$ will be the last digit left, it's odd, so Raze wins.In the fourth match no matter how Raze plays, Breach can mark $$$9$$$, and in the end there will be digit $$$0$$$. It's even, so Breach wins."}, "positive_code": [{"source_code": "t = gets.to_i\nt.times do\n n = gets.to_i\n d = gets.chomp\n arr = d.split('').map(&:to_i)\n if arr.length == 1\n if arr[0].odd?\n puts 1\n else\n puts 2\n end\n else\n indexes = (1..arr.length).to_a\n r_positions = indexes.select(&:odd?)\n b_positions = indexes.select(&:even?)\n\n r_positions = r_positions.sort_by { |pos| arr[pos - 1].odd? ? 1 : -1 }\n b_positions = b_positions.sort_by { |pos| arr[pos - 1].even? ? 1 : -1 }\n\n key = if r_positions.length > b_positions.length\n arr[r_positions.last - 1]\n else\n arr[b_positions.last - 1]\n end\n if key.odd?\n puts 1\n else\n puts 2\n end\n end\nend\n"}, {"source_code": "t = gets.to_i\nt.times do\n\tn = gets.to_i\n\tarr = gets.split('').map(&:to_i)\n\tcheckment = n%2\n\tflag = false\n\tfor i in 1..n\n\t\tflag = true if i%2 == checkment && arr[i-1]%2 == checkment\n\tend\n\n\tif checkment == 1\n\t\tif flag\n\t\t\tputs 1\n\t\telse\n\t\t\tputs 2\n\t\tend\n\telse\n\t\tif flag\n\t\t\tputs 2\n\t\telse\n\t\t\tputs 1\n\t\tend\n\tend\nend"}, {"source_code": "T = gets.to_i\n\nT.times do\n l = gets.to_i\n n = gets.to_i\n\n if l.odd?\n exist_odd = false\n\n n.digits.reverse.each_with_index do |x, i|\n next if i.odd?\n\n exist_odd |= x.odd?\n end\n\n if exist_odd\n puts 1\n else\n puts 2\n end\n else\n exist_even = false\n\n n.digits.reverse.each_with_index do |x, i|\n next if i.even?\n\n exist_even |= x.even?\n end\n\n if exist_even\n puts 2\n else\n puts 1\n end\n end\nend\n"}], "negative_code": [], "src_uid": "c9225c915669e183cbd8c20b848d96e5"} {"nl": {"description": "You are given $$$n$$$ strings $$$s_1, s_2, \\dots, s_n$$$ of length at most $$$\\mathbf{8}$$$. For each string $$$s_i$$$, determine if there exist two strings $$$s_j$$$ and $$$s_k$$$ such that $$$s_i = s_j + s_k$$$. That is, $$$s_i$$$ is the concatenation of $$$s_j$$$ and $$$s_k$$$. Note that $$$j$$$ can be equal to $$$k$$$.Recall that the concatenation of strings $$$s$$$ and $$$t$$$ is $$$s + t = s_1 s_2 \\dots s_p t_1 t_2 \\dots t_q$$$, where $$$p$$$ and $$$q$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. For example, concatenation of \"code\" and \"forces\" is \"codeforces\".", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 10^5$$$) — the number of strings. Then $$$n$$$ lines follow, the $$$i$$$-th of which contains non-empty string $$$s_i$$$ of length at most $$$\\mathbf{8}$$$, consisting of lowercase English letters. Among the given $$$n$$$ strings, there may be equal (duplicates). The sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.", "output_spec": "For each test case, output a binary string of length $$$n$$$. The $$$i$$$-th bit should be $$$\\texttt{1}$$$ if there exist two strings $$$s_j$$$ and $$$s_k$$$ where $$$s_i = s_j + s_k$$$, and $$$\\texttt{0}$$$ otherwise. Note that $$$j$$$ can be equal to $$$k$$$.", "sample_inputs": ["3\n\n5\n\nabab\n\nab\n\nabc\n\nabacb\n\nc\n\n3\n\nx\n\nxx\n\nxxx\n\n8\n\ncodeforc\n\nes\n\ncodes\n\ncod\n\nforc\n\nforces\n\ne\n\ncode"], "sample_outputs": ["10100\n011\n10100101"], "notes": "NoteIn the first test case, we have the following: $$$s_1 = s_2 + s_2$$$, since $$$\\texttt{abab} = \\texttt{ab} + \\texttt{ab}$$$. Remember that $$$j$$$ can be equal to $$$k$$$. $$$s_2$$$ is not the concatenation of any two strings in the list. $$$s_3 = s_2 + s_5$$$, since $$$\\texttt{abc} = \\texttt{ab} + \\texttt{c}$$$. $$$s_4$$$ is not the concatenation of any two strings in the list. $$$s_5$$$ is not the concatenation of any two strings in the list. Since only $$$s_1$$$ and $$$s_3$$$ satisfy the conditions, only the first and third bits in the answer should be $$$\\texttt{1}$$$, so the answer is $$$\\texttt{10100}$$$."}, "positive_code": [{"source_code": "def double_string(strs, n)\r\n hash = {}\r\n strs.each do |i|\r\n hash[i] = true\r\n end\r\n ans = \"\"\r\n strs.each do |str|\r\n j = 0\r\n len = str.length\r\n\r\n found = false\r\n while j < len\r\n x = str[0,j+1]\r\n y = str[j+1, len]\r\n if hash.key?(x) && hash.key?(y)\r\n ans << \"1\"\r\n found = true\r\n break\r\n end\r\n j += 1\r\n end\r\n if !found\r\n ans << \"0\"\r\n end\r\n end\r\n puts ans\r\n ans\r\nend\r\nt = gets.to_i\r\ninputs = []\r\n(0..t-1).each do\r\n n = gets.to_i\r\n arr = []\r\n (0..n-1).each do |j|\r\n s = gets.strip\r\n arr.push(s)\r\n end\r\n inputs.push([arr, n])\r\nend\r\n\r\ninputs.each do |input|\r\n double_string(input[0], input[1])\r\nend\r\n"}, {"source_code": "require 'set'\n\nt = gets.to_i\n\nt.times do\n n = gets.to_i\n a = Array.new(n) { gets.chomp }\n s = a.to_set\n ans = \"\"\n a.each do |x|\n flag = false\n (1 ... x.size).each do |l|\n if s.include? x[0, l] and s.include? x[l .. -1]\n ans << \"1\"\n flag = true\n break\n end\n end\n ans << \"0\" if not flag\n end\n puts ans\nend\n"}], "negative_code": [{"source_code": "class StringLength\r\n attr_accessor :str, :len\r\n\r\n def initialize(str, len)\r\n @str = str\r\n @len = len\r\n end\r\nend\r\n\r\ndef double_string(sorted, arr, n)\r\n ans = \"\"\r\n hash = arr.tally\r\n arr.each do |str|\r\n check = false\r\n l = str.length\r\n sorted.each do |obj|\r\n if obj.len < l\r\n sub = str.sub(obj.str, \"\")\r\n if obj.str.length == 1 && (obj.str+sub != str || sub+obj.str != str)\r\n next\r\n end\r\n if hash.key?(sub) && str!=sub\r\n ans << \"1\"\r\n check = true\r\n break\r\n end\r\n else\r\n break\r\n end\r\n end\r\n if !check\r\n ans << \"0\"\r\n end\r\n end\r\n puts ans\r\nend\r\nt = gets.to_i\r\ninputs = []\r\n(0..t-1).each do\r\n n = gets.to_i\r\n arr = []\r\n (0..n-1).each do |j|\r\n s = gets.strip\r\n arr.push(s)\r\n end\r\n inputs.push([arr, n])\r\nend\r\n\r\ninputs.each do |input|\r\n sorted = []\r\n input[0].each do |str|\r\n sorted.push(StringLength.new(str, str.length))\r\n end\r\n sorted = sorted.sort_by!{|obj| obj.len}\r\n double_string(sorted, input[0], input[1])\r\nend\r\n"}], "src_uid": "9683d5960247359b6b9066e96897d6f9"} {"nl": {"description": "In the beginning of the new year Keivan decided to reverse his name. He doesn't like palindromes, so he changed Naviek to Navick.He is too selfish, so for a given n he wants to obtain a string of n characters, each of which is either 'a', 'b' or 'c', with no palindromes of length 3 appearing in the string as a substring. For example, the strings \"abc\" and \"abca\" suit him, while the string \"aba\" doesn't. He also want the number of letters 'c' in his string to be as little as possible.", "input_spec": "The first line contains single integer n (1 ≤ n ≤ 2·105) — the length of the string.", "output_spec": "Print the string that satisfies all the constraints. If there are multiple answers, print any of them.", "sample_inputs": ["2", "3"], "sample_outputs": ["aa", "bba"], "notes": "NoteA palindrome is a sequence of characters which reads the same backward and forward."}, "positive_code": [{"source_code": "#!ruby\n$><<'abba'.chars.cycle.first(gets.to_i).join\n"}, {"source_code": "# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n############################################################\ndef gs() gets.strip end\ndef gi() gets.to_i end\ndef gss() gs.split end\ndef gis() gss.map(&:to_i) end\ndef nmapf(n,f) n.times.map{ __send__ f } end\ndef ngs(n) nmapf n,:gs end\ndef ngi(n) nmapf n,:gi end\ndef ngss(n) nmapf n,:gss end\ndef ngis(n) nmapf n,:gis end\ndef arr2d(h,w,v=0) h.times.map{[v] * w} end\ndef for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end\ndef nsum(n) n * (n + 1) / 2 end\ndef vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end\n############################################################\n\n\nn = gi\n\nx = \"aabb\" * ((n + 3) / 4)\n\nputs x[0...n]\n"}, {"source_code": "# your code goes here\nn=gets.to_i\nif n==1 then\n\tputs \"a\"\nelsif n==2 then\n\tputs \"aa\"\nelse\n\tn=n-2\n\tprint \"aa\"\n\t[\"b\",\"b\",\"a\",\"a\"].cycle{|e|\n\t\tprint e\n\t\tn-=1\n\t\tbreak if n==0\n\t}\n\tprint \"\\n\"\nend\n\n"}, {"source_code": "n=gets.to_i\ns=\"aabb\"*((n+3)/4)\nputs s[0,n]\n"}, {"source_code": "n = gets.to_i\n\ns=\"aabb\"\nn.times{|i|\n print s[i%4]\n}"}, {"source_code": "n=gets.to_i\nm=n/4\nr=n%4\ns='aabb'*m+'aabb'[0...r]\nputs s"}], "negative_code": [{"source_code": "#!ruby\n$><<'abc'.chars.cycle.first(gets.to_i).join\n"}, {"source_code": "# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n############################################################\ndef gs() gets.strip end\ndef gi() gets.to_i end\ndef gss() gs.split end\ndef gis() gss.map(&:to_i) end\ndef nmapf(n,f) n.times.map{ __send__ f } end\ndef ngs(n) nmapf n,:gs end\ndef ngi(n) nmapf n,:gi end\ndef ngss(n) nmapf n,:gss end\ndef ngis(n) nmapf n,:gis end\ndef arr2d(h,w,v=0) h.times.map{[v] * w} end\ndef for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end\ndef nsum(n) n * (n + 1) / 2 end\ndef vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end\n############################################################\n\n\nn = gi\n\nx = \"aabc\" * ((n + 3) / 4)\n\nputs x[0...n]\n"}, {"source_code": "n=gets.to_i\nm=n/3\nr=n%3\ns='abc'*m+'abc'[0...r]\nputs s"}], "src_uid": "fbde86a29f416b3d83bcc63fb3776776"} {"nl": {"description": "Monocarp has got an array $$$a$$$ consisting of $$$n$$$ integers. Let's denote $$$k$$$ as the mathematic mean of these elements (note that it's possible that $$$k$$$ is not an integer). The mathematic mean of an array of $$$n$$$ elements is the sum of elements divided by the number of these elements (i. e. sum divided by $$$n$$$).Monocarp wants to delete exactly two elements from $$$a$$$ so that the mathematic mean of the remaining $$$(n - 2)$$$ elements is still equal to $$$k$$$.Your task is to calculate the number of pairs of positions $$$[i, j]$$$ ($$$i < j$$$) such that if the elements on these positions are deleted, the mathematic mean of $$$(n - 2)$$$ remaining elements is equal to $$$k$$$ (that is, it is equal to the mathematic mean of $$$n$$$ elements of the original array $$$a$$$).", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of testcases. The first line of each testcase contains one integer $$$n$$$ ($$$3 \\le n \\le 2 \\cdot 10^5$$$) — the number of elements in the array. The second line contains a sequence of integers $$$a_1, a_2, \\dots, a_n$$$ ($$$0 \\le a_i \\le 10^{9}$$$), where $$$a_i$$$ is the $$$i$$$-th element of the array. The sum of $$$n$$$ over all testcases doesn't exceed $$$2 \\cdot 10^5$$$.", "output_spec": "Print one integer — the number of pairs of positions $$$[i, j]$$$ ($$$i < j$$$) such that if the elements on these positions are deleted, the mathematic mean of $$$(n - 2)$$$ remaining elements is equal to $$$k$$$ (that is, it is equal to the mathematic mean of $$$n$$$ elements of the original array $$$a$$$).", "sample_inputs": ["4\n4\n8 8 8 8\n3\n50 20 10\n5\n1 4 7 3 5\n7\n1 2 3 4 5 6 7"], "sample_outputs": ["6\n0\n2\n3"], "notes": "NoteIn the first example, any pair of elements can be removed since all of them are equal.In the second example, there is no way to delete two elements so the mathematic mean doesn't change.In the third example, it is possible to delete the elements on positions $$$1$$$ and $$$3$$$, or the elements on positions $$$4$$$ and $$$5$$$."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n n=gets.to_i\r\n a=gets.split.map(&:to_i).sort\r\n s=a.sum\r\n if 2*s%n!=0\r\n puts 0\r\n next\r\n end\r\n k2=2*s/n\r\n\r\n b=[]\r\n bb=[]\r\n q=-1\r\n a.each do |aa|\r\n if aa!=q\r\n b << aa\r\n bb << 1\r\n q=aa\r\n else\r\n bb[-1]+=1\r\n end\r\n end\r\n\r\n bz=b.size\r\n c=0\r\n i=0\r\n j=bz-1\r\n while i<=j\r\n bibj=b[i]+b[j]\r\n if bibj==k2\r\n if i!=j\r\n c+=bb[i]*bb[j]\r\n i+=1\r\n else\r\n c+=bb[i]*(bb[i]-1)/2\r\n i+=1\r\n end\r\n elsif bibj mx\n end\n break if pow > mx or pow / i > res + a[-1]\n res = [res, cur].min\n end\n puts res\nend"}, {"source_code": "n = gets.to_i\na = gets.chomp.split(\" \").map(&:to_i).sort\nmax = a.max\nleft = 0\nright = (max ** (1/(n-1).to_f)).floor + 2\nmin_sum = 0\nif n < 33\n while right - left > 1\n mid = left + (right - left) / 2\n sum1 = 0\n sum2 = 0\n v = 1\n v2 = 1\n n.times do |i|\n v *= mid if i != 0\n v2 *= (mid+1) if i != 0\n sum1 += (a[i]-v).abs\n sum2 += (a[i]-v2).abs\n end\n if sum1 > sum2\n left = mid\n min_sum = sum2\n else\n right = mid\n min_sum = sum1\n end\n end\nelse\n n.times do |i|\n min_sum += (a[i]-1).abs\n end\nend\nputs min_sum"}], "negative_code": [{"source_code": "n = gets.to_i\na = gets.chomp.split(\" \").map(&:to_i).sort\nmax = a.max\nleft = 0\nright = (max ** (1/(n-1).to_f)).floor + 2\nmin_sum = 0\nif n < 32\n while right - left > 1\n mid = left + (right - left) / 2\n sum1 = 0\n sum2 = 0\n v = 1\n v2 = 1\n n.times do |i|\n v *= mid if i != 0\n v2 *= (mid+1) if i != 0\n sum1 += (a[i]-v).abs\n sum2 += (a[i]-v2).abs\n end\n if sum1 > sum2\n left = mid\n min_sum = sum2\n else\n right = mid\n min_sum = sum1\n end\n end\nelse\n n.times do |i|\n min_sum += (a[i]-1).abs\n end\nend\nputs min_sum"}, {"source_code": "def pow(x,n)\n if n == 0\n 1\n elsif n % 2 == 0\n pow((x+x), n/2)\n else\n x + pow(x, n-1)\n end\nend\nn = gets.to_i\na = gets.chomp.split(\" \").map(&:to_i).sort\nleft = 0\nright = (a.max ** 0.5).floor + 2\nmin_sum = 1000000000000000\nwhile right - left > 1\n mid = left + (right - left) / 2\n\n sum = 0\n n.times do |i|\n v = mid.pow(i)\n sum += (a[i]-v).abs\n end\n\n if sum < min_sum\n min_sum = sum\n left = mid\n else\n right = mid\n end\nend\nputs min_sum"}], "src_uid": "54e9c6f24c430c5124d840b5a65a1bc4"} {"nl": {"description": "You are given an array $$$a$$$ consisting of $$$n$$$ positive integers.Initially, you have an integer $$$x = 0$$$. During one move, you can do one of the following two operations: Choose exactly one $$$i$$$ from $$$1$$$ to $$$n$$$ and increase $$$a_i$$$ by $$$x$$$ ($$$a_i := a_i + x$$$), then increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). Just increase $$$x$$$ by $$$1$$$ ($$$x := x + 1$$$). The first operation can be applied no more than once to each $$$i$$$ from $$$1$$$ to $$$n$$$.Your task is to find the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$ (the value $$$k$$$ is given).You have to answer $$$t$$$ independent test cases. ", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 2 \\cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le n \\le 2 \\cdot 10^5; 1 \\le k \\le 10^9$$$) — the length of $$$a$$$ and the required divisior. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 \\cdot 10^5$$$ ($$$\\sum n \\le 2 \\cdot 10^5$$$).", "output_spec": "For each test case, print the answer — the minimum number of moves required to obtain such an array that each its element is divisible by $$$k$$$.", "sample_inputs": ["5\n4 3\n1 2 1 3\n10 6\n8 7 1 8 3 7 5 10 8 9\n5 10\n20 100 50 20 100500\n10 25\n24 24 24 24 24 24 24 24 24 24\n8 8\n1 2 3 4 5 6 7 8"], "sample_outputs": ["6\n18\n0\n227\n8"], "notes": "NoteConsider the first test case of the example: $$$x=0$$$, $$$a = [1, 2, 1, 3]$$$. Just increase $$$x$$$; $$$x=1$$$, $$$a = [1, 2, 1, 3]$$$. Add $$$x$$$ to the second element and increase $$$x$$$; $$$x=2$$$, $$$a = [1, 3, 1, 3]$$$. Add $$$x$$$ to the third element and increase $$$x$$$; $$$x=3$$$, $$$a = [1, 3, 3, 3]$$$. Add $$$x$$$ to the fourth element and increase $$$x$$$; $$$x=4$$$, $$$a = [1, 3, 3, 6]$$$. Just increase $$$x$$$; $$$x=5$$$, $$$a = [1, 3, 3, 6]$$$. Add $$$x$$$ to the first element and increase $$$x$$$; $$$x=6$$$, $$$a = [6, 3, 3, 6]$$$. We obtained the required array. Note that you can't add $$$x$$$ to the same element more than once."}, "positive_code": [{"source_code": "gets.to_i.times do\n\tn, k = gets.split.map(&:to_i)\n\ta = gets.split.map(&:to_i)\n\tp a.map { -_1 % k }.tally.map { |val, cnt|\n\t\t\tval > 0 ? val + k * (cnt - 1) + 1 : 0\n\t }.max\nend\n"}, {"source_code": "def run\n n, k = gets.chomp.split(\" \").map(&:to_i)\n a = gets.chomp.split(\" \").map(&:to_i)\n b = a.group_by{ |e| e % k }.reduce(Hash::new()){|acc, (k, v)| acc[k] = v.size; acc}\n b.delete(0)\n\n if b.size == 0\n puts 0\n return\n end\n\n m = b.values.max\n m_key = b.keys.sort.find{|key| b[key] == m}\n\n c = (m - 1) * k + k - m_key + 1\n\n puts c\nend\n\n\ndef main\n t = gets.chomp.to_i\n\n t.times do\n run\n end\nend\n\nmain\n"}, {"source_code": "t = gets.to_i\nt.times do\n n, k = gets.split.map(&:to_i)\n nums = gets.split.map(&:to_i)\n nums.map! {|num| k - (num % k)}\n nums.reject! {|num| num == k}\n \n if nums.count == 0\n puts 0\n else\n nums_t = nums.tally\n max_cnt = nums_t.values.max\n\n max_key = nums_t.select {|k, v| v == max_cnt}.map(&:first).max\n puts (max_cnt - 1) * k + max_key + 1\n end\nend\n"}], "negative_code": [], "src_uid": "a8b4c115bedda3847e7c2e3620e3e19b"} {"nl": {"description": "The Central Company has an office with a sophisticated security system. There are $$$10^6$$$ employees, numbered from $$$1$$$ to $$$10^6$$$.The security system logs entrances and departures. The entrance of the $$$i$$$-th employee is denoted by the integer $$$i$$$, while the departure of the $$$i$$$-th employee is denoted by the integer $$$-i$$$.The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day.Some examples of valid or invalid days: $$$[1, 7, -7, 3, -1, -3]$$$ is a valid day ($$$1$$$ enters, $$$7$$$ enters, $$$7$$$ leaves, $$$3$$$ enters, $$$1$$$ leaves, $$$3$$$ leaves). $$$[2, -2, 3, -3]$$$ is also a valid day. $$$[2, 5, -5, 5, -5, -2]$$$ is not a valid day, because $$$5$$$ entered the office twice during the same day. $$$[-4, 4]$$$ is not a valid day, because $$$4$$$ left the office without being in it. $$$[4]$$$ is not a valid day, because $$$4$$$ entered the office and didn't leave it before the end of the day. There are $$$n$$$ events $$$a_1, a_2, \\ldots, a_n$$$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events.You must partition (to cut) the array $$$a$$$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day.For example, if $$$n=8$$$ and $$$a=[1, -1, 1, 2, -1, -2, 3, -3]$$$ then he can partition it into two contiguous subarrays which are valid days: $$$a = [1, -1~ \\boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$$$.Help the administrator to partition the given array $$$a$$$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$-10^6 \\le a_i \\le 10^6$$$ and $$$a_i \\neq 0$$$).", "output_spec": "If there is no valid partition, print $$$-1$$$. Otherwise, print any valid partition in the following format: On the first line print the number $$$d$$$ of days ($$$1 \\le d \\le n$$$). On the second line, print $$$d$$$ integers $$$c_1, c_2, \\ldots, c_d$$$ ($$$1 \\le c_i \\le n$$$ and $$$c_1 + c_2 + \\ldots + c_d = n$$$), where $$$c_i$$$ is the number of events in the $$$i$$$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days.", "sample_inputs": ["6\n1 7 -7 3 -1 -3", "8\n1 -1 1 2 -1 -2 3 -3", "6\n2 5 -5 5 -5 -2", "3\n-8 1 1"], "sample_outputs": ["1\n6", "2\n2 6", "-1", "-1"], "notes": "NoteIn the first example, the whole array is a valid day.In the second example, one possible valid solution is to split the array into $$$[1, -1]$$$ and $$$[1, 2, -1, -2, 3, -3]$$$ ($$$d = 2$$$ and $$$c = [2, 6]$$$). The only other valid solution would be to split the array into $$$[1, -1]$$$, $$$[1, 2, -1, -2]$$$ and $$$[3, -3]$$$ ($$$d = 3$$$ and $$$c = [2, 4, 2]$$$). Both solutions are accepted.In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events."}, "positive_code": [{"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\n\nentered = {}\ncnt = 0\nlen = 0\nans = []\nn.times do |i|\n v = a[i]\n len += 1\n if v > 0\n if !entered[v].nil?\n p -1\n exit\n else\n entered[v] = 1\n cnt += 1\n end\n else\n if entered[-v].nil? || entered[-v] != 1\n p -1\n exit\n else\n entered[-v] = -1\n cnt -= 1\n if cnt == 0\n ans << len\n len = 0\n entered = {}\n end\n end\n end\nend\nif cnt > 0\n p -1\nelse\n p ans.length\n puts ans.join(\" \")\nend"}, {"source_code": "d = gets.chomp.to_i\nevents = gets.split(\" \")\ncp = []\njoin = []\nout = []\ncheck = false\nfix = 0\n\nsolution = []\nevents.map{|w|\nw=w.to_i\n\nif w.to_s[0]==\"-\"\nif join[w.to_s.split(\"-\")[1].to_i]\njoin[w.to_s.split(\"-\")[1].to_i] = false\nfix-=1\nelse\nputs \"-1\"\ncheck = true\nexit\nend\n\nend\n\nif w.to_s[0]!=\"-\"\nif join[w]\nputs \"-1\"\ncheck = true\nexit\nelse\nfix+=1\njoin[w] = true\nend\nend\n\ncp.push(w)\n\nif fix == 0\nif cp.size == cp.uniq.size\nsolution.push(cp)\ncp = []\nelse\nputs \"-1\"\ncheck = true\nexit\nend\nend\n\n}\nif solution.size > 0 and cp.size == 0\nputs solution.size\ns=[]\nsolution.map{|w|\ns.push(w.size)\n}\nputs s.join(\" \")\ncheck = true\nexit\nend\nif !check then puts \"-1\" end"}, {"source_code": "n = gets.to_i\na = gets.split.map &:to_i\nm = {}\nused = {}\nnm = 0\nc = 0\nans = []\na.each do |x|\n if x > 0\n if m[x] || used[x]\n puts -1\n exit\n end\n m[x] = true\n nm += 1 \n end\n if x < 0\n if not m[-x]\n puts -1\n exit\n end\n m[-x] = false\n used[-x] = true\n nm -= 1\n end\n c += 1\n if nm == 0\n ans << c\n c = 0\n used = {}\n end\nend\nif nm == 0\n puts ans.size\n puts ans.join(' ')\nelse\n puts -1\nend\n"}], "negative_code": [{"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\n\nentered = {}\ncnt = 0\nlen = 0\nans = []\nn.times do |i|\n v = a[i]\n len += 1\n if v > 0\n if !entered[v].nil?\n p -1\n exit\n else\n entered[v] = 1\n cnt += 1\n end\n else\n if entered[-v].nil? || entered[-v] != 1\n p -1\n exit\n else\n entered[-v] = -1\n cnt -= 1\n if cnt == 0\n ans << len\n len = 0\n entered = {}\n end\n end\n end\nend\np ans.length\nputs ans.join(\" \")"}, {"source_code": "d = gets.chomp.to_i\nevents = gets.split(\" \").map{|x|x.to_i}\ncp = []\njoin = []\nout = []\n\nsolution = []\nevents.size.times{|w|\nif events[w].to_s[0]==\"-\"\nif join.include?(events[w].to_s.split(\"-\")[1].to_i)\njoin.delete(events[w].to_s.split(\"-\")[1].to_i)\nelse\nputs \"-1\"\nbreak\nend\nend\n\nif events[w].to_s[0]!=\"-\"\nif join.include?(events[w])\nputs \"-1\"\nbreak\nelse\njoin.push(events[w])\nend\nend\n\ncp.push(events[w])\n\nif join.size == 0\nif cp.size == cp.uniq.size\nsolution.push(cp)\ncp = []\nelse\nputs \"-1\"\nbreak\nend\nend\n\n}\nif solution.size > 0\nputs solution.size\ns=[]\nsolution.size.times{|w|\ns.push(solution[w].size)\n}\nputs s.join(\" \")\nend"}, {"source_code": "d = gets.chomp.to_i\nevents = gets.split(\" \").map{|x|x.to_s}\ncp = []\njoin = []\nout = []\n\nsolution = []\nevents.map{|w|\nif w[0]==\"-\"\nif join.include?(w.split(\"-\")[1])\njoin.delete(w.split(\"-\")[1])\nelse\nputs \"-1\"\nbreak\nend\nend\n\nif w[0]!=\"-\"\nif join.include?(w)\nputs \"-1\"\nbreak\nelse\njoin.push(w)\nend\nend\n\ncp.push(w)\n\nif join.size == 0\nif cp.size == cp.uniq.size\nsolution.push(cp)\ncp = []\nelse\nputs \"-1\"\nbreak\nend\nend\n\n}\nif solution.size > 0 and cp.size == 0\nputs solution.size\ns=[]\nsolution.size.times{|w|\ns.push(solution[w].size)\n}\nputs s.join(\" \")\nelse\nputs \"-1\"\nend"}, {"source_code": "d = gets.chomp.to_i\nevents = gets.split(\" \").map{|x|x.to_i}\ncp = []\njoin = []\nout = []\ncheck = false\n\nsolution = []\nevents.size.times{|w|\nif events[w].to_s[0]==\"-\"\nif join.include?(events[w].to_s.split(\"-\")[1].to_i)\njoin.delete(events[w].to_s.split(\"-\")[1].to_i)\nelse\nputs \"-1\"\ncheck = true\nbreak\nend\nend\n\nif events[w].to_s[0]!=\"-\"\nif join.include?(events[w])\nputs \"-1\"\ncheck = true\nbreak\nelse\njoin.push(events[w])\nend\nend\n\ncp.push(events[w])\n\nif join.size == 0\nif cp.size == cp.uniq.size\nsolution.push(cp)\ncp = []\nelse\nputs \"-1\"\ncheck = true\nbreak\nend\nend\n\n}\nif solution.size > 0\nputs solution.size\ns=[]\nsolution.size.times{|w|\ns.push(solution[w].size)\n}\nputs s.join(\" \")\ncheck = true\nend\nif !check then puts \"-1\" end"}, {"source_code": "n = gets.to_i\na = gets.split.map &:to_i\nm = {}\nused = {}\nnm = 0\nc = 0\nans = []\na.each do |x|\n if x > 0\n if m[x] || used[x]\n puts -1\n exit\n end\n m[x] = true\n nm += 1 \n end\n if x < 0\n if not m[-x]\n puts -1\n exit\n end\n m[-x] = false\n used[-x] = true\n nm -= 1\n end\n c += 1\n if nm == 0\n ans << c\n c = 0\n used = {}\n end\nend\nputs ans.size\nputs ans.join(' ')\n"}], "src_uid": "17f73ae0347b551fb5e287322785c8a4"} {"nl": {"description": "Bob has a string $$$s$$$ consisting of lowercase English letters. He defines $$$s'$$$ to be the string after removing all \"a\" characters from $$$s$$$ (keeping all other characters in the same order). He then generates a new string $$$t$$$ by concatenating $$$s$$$ and $$$s'$$$. In other words, $$$t=s+s'$$$ (look at notes for an example).You are given a string $$$t$$$. Your task is to find some $$$s$$$ that Bob could have used to generate $$$t$$$. It can be shown that if an answer exists, it will be unique.", "input_spec": "The first line of input contains a string $$$t$$$ ($$$1 \\leq |t| \\leq 10^5$$$) consisting of lowercase English letters.", "output_spec": "Print a string $$$s$$$ that could have generated $$$t$$$. It can be shown if an answer exists, it is unique. If no string exists, print \":(\" (without double quotes, there is no space between the characters).", "sample_inputs": ["aaaaa", "aacaababc", "ababacacbbcc", "baba"], "sample_outputs": ["aaaaa", ":(", "ababacac", ":("], "notes": "NoteIn the first example, we have $$$s = $$$ \"aaaaa\", and $$$s' = $$$ \"\".In the second example, no such $$$s$$$ can work that will generate the given $$$t$$$.In the third example, we have $$$s = $$$ \"ababacac\", and $$$s' = $$$ \"bbcc\", and $$$t = s + s' = $$$ \"ababacacbbcc\"."}, "positive_code": [{"source_code": "t = readline.chomp;\nss = t.gsub(\"a\", \"\");\nk = ss.length;\nif k % 2 == 0 && ss[0 ... k/2] == t[t.length - k/2 .. -1]\n puts t[0 ... t.length - k/2];\nelse\n puts \":(\"\nend\n"}, {"source_code": "s = gets.chomp\nt = s.delete(\"a\")\nn = t.length\nif n.even? && t[0,n/2] == t[n/2,n/2] && !s[s.length-n/2,n/2].include?(\"a\")\n puts s[0,s.length-n/2]\nelse\n puts \":(\"\nend\n"}], "negative_code": [], "src_uid": "b5bcb6d78daacd56362fd76e35b903ac"} {"nl": {"description": "Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \\le n \\le 100, 1 \\le k_1 \\le n - 1, 1 \\le k_2 \\le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \\dots, a_{k_1}$$$ ($$$1 \\le a_i \\le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \\dots, b_{k_2}$$$ ($$$1 \\le b_i \\le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different.", "output_spec": "For each test case, output \"YES\" in a separate line, if the first player wins. Otherwise, output \"NO\" in a separate line. You can print each letter in any case (upper or lower).", "sample_inputs": ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"], "sample_outputs": ["YES\nNO"], "notes": "NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement."}, "positive_code": [{"source_code": "gets.to_i.times do\n\tn=gets.split[0]\n\tif gets.split.include?(n) \n puts 'YES'\n\telse \n puts'NO'\n\tend\n gets\nend"}], "negative_code": [], "src_uid": "3ef23f114be223255bd10131b2375b86"} {"nl": {"description": "One day Vasya got hold of information on the Martian dollar course in bourles for the next n days. The buying prices and the selling prices for one dollar on day i are the same and are equal to ai. Vasya has b bourles. He can buy a certain number of dollars and then sell it no more than once in n days. According to Martian laws, one can buy only an integer number of dollars. Which maximal sum of money in bourles can Vasya get by the end of day n?", "input_spec": "The first line contains two integers n and b (1 ≤ n, b ≤ 2000) — the number of days and the initial number of money in bourles. The next line contains n integers ai (1 ≤ ai ≤ 2000) — the prices of Martian dollars.", "output_spec": "Print the single number — which maximal sum of money in bourles can Vasya get by the end of day n.", "sample_inputs": ["2 4\n3 7", "4 10\n4 3 2 1", "4 10\n4 2 3 1"], "sample_outputs": ["8", "10", "15"], "notes": null}, "positive_code": [{"source_code": "n, b = gets.split.map { |v| v.to_i }\na = gets.split.map { |v| v.to_i }\n\nbest = b\n\nn.times do |day|\n min, max = a[0, day + 1].min, a[day .. -1].max\n profit = max * (b / min) + b % min\n best = profit if profit > best\nend\n\nputs best\n"}, {"source_code": "n,b = gets.chomp.split(/ /).map!{|x| x.to_i}\na = gets.chomp.split(/ /).map!{|x| x.to_i}\n\nar = Array.new\nfor i in 1..n-1\n\tif a[i] > a[i-1]\n\t\tar.push( [ a[i-1], a[i] ] )\n\tend\n\tfor j in 0..ar.length-1\n\t\tar[j][1] = a[i] if ar[j][1] < a[i]\n\tend\nend\n\nar.sort!{|x,y| (b / y[0]) * (y[1]-y[0]) <=> (b / x[0]) * (x[1]-x[0]) }\n\nif ar.length > 0\n\tb += (b / ar[0][0]) * (ar[0][1]-ar[0][0])\nend\nputs b\n"}, {"source_code": "n,b = gets.split(\" \").map(&:to_i)\narr = gets.split(\" \").map(&:to_i)\ns = 0\ndiff = []\n(0..n-2).each do |i|\n\ta = arr[i]\n\tmax = arr[(i+1)..-1].max\n\tif a <= b\n\t\tstocks = b/a\n\t\tleft = b % a\n\t\tsell = stocks * max\n\t\tdiff << sell + left\n\tend\nend\ndiff << b\nputs diff.max"}, {"source_code": "n, b = gets.chomp.split.map(&:to_i)\na = gets.chomp.split.map(&:to_i)\nmx = b\n(1...a.length).each do |x|\n\t(0...x).each do |y|\n\t\tr = b % a[y]\n\t\tmx = [mx, r + (b / a[y]) * a[x]].max\n\tend\nend\nputs mx"}], "negative_code": [], "src_uid": "2c9133650d831fa6ab4c11661bcb9cbb"} {"nl": {"description": "You are given an array $$$a$$$ consisting of $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$. You want to split it into exactly $$$k$$$ non-empty non-intersecting subsegments such that each subsegment has odd sum (i. e. for each subsegment, the sum of all elements that belong to this subsegment is odd). It is impossible to rearrange (shuffle) the elements of a given array. Each of the $$$n$$$ elements of the array $$$a$$$ must belong to exactly one of the $$$k$$$ subsegments.Let's see some examples of dividing the array of length $$$5$$$ into $$$3$$$ subsegments (not necessarily with odd sums): $$$[1, 2, 3, 4, 5]$$$ is the initial array, then all possible ways to divide it into $$$3$$$ non-empty non-intersecting subsegments are described below: $$$[1], [2], [3, 4, 5]$$$; $$$[1], [2, 3], [4, 5]$$$; $$$[1], [2, 3, 4], [5]$$$; $$$[1, 2], [3], [4, 5]$$$; $$$[1, 2], [3, 4], [5]$$$; $$$[1, 2, 3], [4], [5]$$$. Of course, it can be impossible to divide the initial array into exactly $$$k$$$ subsegments in such a way that each of them will have odd sum of elements. In this case print \"NO\". Otherwise, print \"YES\" and any possible division of the array. See the output format for the detailed explanation.You have to answer $$$q$$$ independent queries.", "input_spec": "The first line contains one integer $$$q$$$ ($$$1 \\le q \\le 2 \\cdot 10^5$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le k \\le n \\le 2 \\cdot 10^5$$$) — the number of elements in the array and the number of subsegments, respectively. The second line of the query contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all queries does not exceed $$$2 \\cdot 10^5$$$ ($$$\\sum n \\le 2 \\cdot 10^5$$$).", "output_spec": "For each query, print the answer to it. If it is impossible to divide the initial array into exactly $$$k$$$ subsegments in such a way that each of them will have odd sum of elements, print \"NO\" in the first line. Otherwise, print \"YES\" in the first line and any possible division of the array in the second line. The division can be represented as $$$k$$$ integers $$$r_1$$$, $$$r_2$$$, ..., $$$r_k$$$ such that $$$1 \\le r_1 < r_2 < \\dots < r_k = n$$$, where $$$r_j$$$ is the right border of the $$$j$$$-th segment (the index of the last element that belongs to the $$$j$$$-th segment), so the array is divided into subsegments $$$[1; r_1], [r_1 + 1; r_2], [r_2 + 1, r_3], \\dots, [r_{k - 1} + 1, n]$$$. Note that $$$r_k$$$ is always $$$n$$$ but you should print it anyway. ", "sample_inputs": ["3\n5 3\n7 18 3 14 1\n5 4\n1 2 3 4 5\n6 2\n1 2 8 4 10 2"], "sample_outputs": ["YES\n1 3 5\nNO\nNO"], "notes": null}, "positive_code": [{"source_code": "gets.to_i.times do\n n, k = gets.split.map(&:to_i)\n a = gets.split.map(&:to_i)\n s = []\n (0...n).each do |j|\n s << j + 1 if a[j] % 2 != 0\n end\n sz = s.size\n if sz >= k and sz % 2 == k % 2\n puts \"YES\"\n print s[0...k-1].join(' '), ' ', n\n puts\n else\n puts \"NO\"\n end\nend\n"}, {"source_code": "MOD=10**9+7\ncnt=0; sum=0; prev=nil; can=true; h=Hash.new(0)\ndef gs() gets.chomp end\ndef gi() gets.to_i end\ndef gsmi() gets.chomp.split.map(&:to_i) end\n\ndef desc(ar) ar.sort!{|x,y|y<=>x} end\ndef min(a,b) a<=b ? a : b end\ndef max(a,b) a>=b ? a : b end\ndef sum(ar) ar.inject(:+) end\n#def bs(ar,eq,n) en=(eq==1 ? :>= : :>); ar.bsearch_index{|x|x.send(en,n)} end\n\ndef puts_yesno(can) puts(can ? 'Yes' : 'No') end\ndef putsend(s) puts s; exit end\ndef debug(k,v) puts \"#{k}:#{v}\" end\n########### ( ˘ω˘ )スヤァ… ###########\nq = gi\nans = []\nq.times do\n n, k = gsmi\n a = gsmi\n odd_c=0\n n.times do |i|\n odd_c+=1 if a[i]%2==1\n end\n str = ''\n if odd_c d\n\nprint ans"}, {"source_code": "n, m = gets.chomp.split.map(&:to_i)\narr = [].tap do |a|\n m.times { a << (gets.chomp.chars.any? { |b| b != '1' }) }\nend\n\nif arr.all? { |a| a != true }\n puts 0\nelse\n puts arr.chunk { |a| a }.select { |a, _| a }.map { |_, a| a.length }.max\nend\n"}, {"source_code": "gets\np $<.read.lines.chunk{|s| s[?0]}.map{|_, a| a.size}.max || 0"}, {"source_code": "size , n = gets.split(\" \").map(&:to_i) \nwon = false \n max_count = -999 \nsteak = 0 \nn.times do \n \n \n\n \n values = gets \n \n if values.count(\"0\")>=1 \n \n \n steak+=1 \n \n else \n \n won =false \n steak = 0 \n end \n if max_countma)\nma=t\nend\nt=0\nelse \nt+=1\nend\nend\nif (t>ma)\nma=t\nend\nputs ma.inspect\n"}, {"source_code": "n,d = gets.split.map{|a| Integer(a)}\nas = (1..d).map{|_| gets.chomp}\n .slice_before{|x| x==\"1\"*n} \n .to_a\n .collect\n .with_index{|x,i| if i!=0 then x.drop(1) else x end}\n\nas[0].delete(\"1\"*n)\nputs as.map{|a| a.length}.max"}, {"source_code": "n, d = gets.split.map(&:to_i)\nans = 0\ncon = 0\nd.times do\n if gets.chomp.chars.include?('0')\n con += 1\n else\n con = 0\n end\n ans = [con, ans].max\nend\nputs ans"}, {"source_code": "n, d = gets.strip.split.map(&:to_i)\ncons = 0\nans = 0\nd.times do\n day = gets.strip\n if day.include? '0'\n cons += 1\n else\n cons = 0\n end\n ans = [ans, cons].max\nend\nputs ans\n"}, {"source_code": "a=gets.chomp.split(\" \").map{|i| i.to_i}\nma=0\nt=0\ntemp=\"\"\na[1].times do\ntemp=gets.chomp.split(\"\")\nif(temp.count('1')==a[0])\n\nif(t>ma)\nma=t\nend\nt=0\nelse \nt+=1\nend\nend\nif (t>ma)\nma=t\nend\nputs ma.inspect"}, {"source_code": "n,m=gets.split.map{|e| e.to_i}\nans=0\nc=0\nm.times{\n\ts=gets.chomp\n\tif s.index(\"0\") then\n\t\tc=c+1\n\telse\n\t\tc=0\n\tend\n\tans=[ans,c].max\n}\nputs ans\n\n"}, {"source_code": "n, d = gets.split.map(&:to_i)\nmax = 0\ncurrent_streak = 0\nd.times do |di|\n\ts = gets.chomp\n\tones = s.count('1')\n\tzeros = s.count('0')\n\t# puts ones\n\t# puts zeros\n\n\tif ones != n\n\t\tcurrent_streak += 1\n\telse\n\t\tcurrent_streak = 0\n\tend\n\n\tif max < current_streak\n\t\tmax = current_streak\t\t\n\tend\nend\n\nputs max"}, {"source_code": "c=0;maxc=0;gets\nwhile s=gets do\n if s.include?\"0\"\n c+=1\n maxc=c if c>maxc\n else\n c=0\n end\nend\nputs maxc\n"}, {"source_code": "n, d = gets.chomp.split.map(&:to_i)\ncnt = 0\nans = 0\nd.times do\n str = gets.chomp\n if str == '1' * n\n ans = [ans, cnt].max\n cnt = 0\n else\n cnt += 1\n end\n ans = [ans, cnt].max\nend\n\nputs ans\n"}, {"source_code": "nd = gets.chomp.split\nd = nd[1].to_i\n\na = []\nfor i in (0..d-1)\n\tx = gets.chomp\n\ta[i] = x.split(\"\")\nend\ncount = 0\narr = []\nfor i in (0..d-1)\n\tif a[i].include?(\"0\")\n\t\tcount += 1\n\t\tarr << count\n\telse \n\t\tcount = 0\n\t\tarr << count\n\tend\nend\n\nprint arr.max"}, {"source_code": "nd = gets.chomp.split\nd = nd[1].to_i\n\na = []\nfor i in (0..d-1)\n\tx = gets.chomp\n\ta[i] = x.split(\"\")\nend\ncount = 0\narr = []\nfor i in (0..d-1)\n\tif a[i].include?(\"0\")\n\t\tcount += 1\n\t\tarr << count\n\telse \n\t\tcount = 0\n\t\tarr << count\n\tend\nend\nprint arr.max"}, {"source_code": "gets\np $<.chunk{|s| s.include? '0'}.select(&:first).map(&:last).map(&:size).max || 0"}, {"source_code": "gets\np $<.read.lines.chunk{|s| s[?0]}.map{|_, a| a.size}.max || 0\n"}, {"source_code": "_, d = gets.split.map(&:to_i)\np d.times.map{gets}.chunk{|s| s[?0]}.map{|_, a| a.size}.max || 0\n"}, {"source_code": "n, d = gets.split.map(&:to_i)\nans = 0\ncur = 0\nd.times do |i|\n cur = gets.chomp.include?('0') ? cur + 1 : 0\n ans = [ans, cur].max\nend\nputs ans\n"}, {"source_code": "n, d = gets.split.map(&:to_i)\narr = []\ncnt = 0\nfor i in 0...d\n opp = gets.chomp\n if opp =~ /[0]/\n cnt += 1\n else\n arr << cnt\n cnt = 0\n end\nend\narr << cnt\n\nputs arr.max\n"}, {"source_code": "n, d = gets.split.map &:to_i\nans, c = 0, 0\nd.times do\n if gets.strip == '1'*n\n c = 0\n else\n c += 1\n end\n ans = [ans, c].max\nend\nputs ans\n"}, {"source_code": "n, d = gets.chomp.split(' ').map(&:to_i)\ndays = []\n\nd.times do\n\tdays << gets.chomp.split('').map(&:to_i)\nend\n\ncon = []\nnum = 0\n\ndays.each do |day|\n\tif day.index(0) != nil\n\t\tnum += 1\n\telse\n\t\tcon << num\n\t\tnum = 0\n\tend\nend\n\ncon << num\n\nputs con.max\n\n"}], "negative_code": [{"source_code": "#!/usr/bin/ruby\ngets\na=$<.map{|e|e.count('0')==0}\nr=c=0\na.each{|e|\n\tif e\n\t\tr=max[r,c+=1]\n\telse\n\t\tc=0\n\tend\n}\np r"}, {"source_code": "n, m = gets.chomp.split.map(&:to_i)\narr = [].tap do |a|\n m.times { a << (gets.chomp.chars.any? { |b| b != '1' }) }\nend\n\nputs arr.chunk { |a| a }.select { |a, _| a }.map { |_, a| a.length }.max\n"}, {"source_code": "a=gets.chomp.split(\" \").map{|i| i.to_i}\nma=0\nt=0\ntemp=\"\"\na[1].times do\ntemp=gets.chomp.split(\"\")\nif(temp.count('1')==a[0])\n\nif(t>ma)\nma=t\nend\nt=0\nelse \nt+=1\nend\nend\nputs ma.inspect\n"}, {"source_code": "n,d = gets.split.map{|a| Integer(a)}\nputs (1..d).map{|_| gets.chomp}\n .slice_before{|x| x==\"1\"*n} \n .to_a\n .collect\n .with_index{|x,i| if i!=0 then x.drop(1).length else x.length end}\n .max"}, {"source_code": "gets\np $<.map{|s| s.chomp.chars.min}.join.split('1').map(&:size).max"}, {"source_code": "gets\nputs $<.chunk{|s| s.include? '0'}.select(&:first).map(&:last).map(&:size).max"}, {"source_code": "gets\np $<.chunk{|s| s.include? '0'}.select(&:first).map(&:last).map(&:size).max rescue 0"}, {"source_code": "_, d = gets.split.map(&:to_i)\np d.times.map{gets}.chunk{|s| s[?0]}.map{|_, a| a.size}.max\n"}, {"source_code": "n, d = gets.split.map(&:to_i)\narr = []\ncnt = 0\nfor i in 0...d\n opp = gets.chomp\n if opp =~ /[0]/\n cnt += 1\n else\n arr << cnt\n cnt = 0\n end\nend\n\nputs (arr.empty? ? cnt : arr.max)\n"}], "src_uid": "a6ee741df426fd2d06fdfda4ca369397"} {"nl": {"description": "You are given two positive integers $$$a$$$ and $$$b$$$. In one move you can increase $$$a$$$ by $$$1$$$ (replace $$$a$$$ with $$$a+1$$$). Your task is to find the minimum number of moves you need to do in order to make $$$a$$$ divisible by $$$b$$$. It is possible, that you have to make $$$0$$$ moves, as $$$a$$$ is already divisible by $$$b$$$. You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$a$$$ and $$$b$$$ ($$$1 \\le a, b \\le 10^9$$$).", "output_spec": "For each test case print the answer — the minimum number of moves you need to do in order to make $$$a$$$ divisible by $$$b$$$.", "sample_inputs": ["5\n10 4\n13 9\n100 13\n123 456\n92 46"], "sample_outputs": ["2\n5\n4\n333\n0"], "notes": null}, "positive_code": [{"source_code": "t = gets.chomp.to_i\nt.times do\n a, b = gets.split.map(&:to_i)\n res = 0\n if a%b ==0\n puts 0\n else\n puts b-a%b\n end\nend\n"}, {"source_code": "t = gets.chomp.to_i\nt.times do\n a, b = gets.split.map(&:to_i)\n res = 0\n if a%b ==0\n puts 0\n else\n puts b-a%b\n end\nend\n"}, {"source_code": "gets.to_i.times do\n\ta, b = gets.split.map &:to_i\n\tx = (a + b - 1) / b * b\n\tputs x - a\nend"}, {"source_code": "file = File.exist?('in.in') ? File.new('in.in', 'r') : STDIN\n\nt = file.gets.to_i\ni = 0\n\nwhile i < t\n a, b = file.gets.split.map(&:to_i)\n\n if (a % b) == 0\n puts 0 \n else\n factor = (a / b) + 1\n goal = factor * b\n moves = goal - a\n puts moves\n end\n i += 1\nend\n\n"}, {"source_code": "a = gets.to_i\nwhile a > 0\n x, y = gets.split\n x = x.to_i\n y = y.to_i\n puts (y-(x%y))%y\n a = a-1\nend\n"}, {"source_code": "N = gets.to_i\narr = N.times.map{gets.split.map(&:to_i)}\n\narr.each do |a|\n if a[0] % a[1] == 0\n puts 0\n else\n puts a[1] - a[0] % a[1]\n end\nend"}, {"source_code": "# Divisibility Problem\ngets.to_i.times do\n mult = gets.rstrip.split\n\n a = mult[0].to_i\n b = mult[1].to_i\n if a%b == 0\n puts 0\n else\n puts b - a%b\n end\nend\n"}, {"source_code": "# coding: utf-8\n\nt = gets.to_i\nt.times {\n a, b = gets.split.map(&:to_i)\n q, r = a/b, a%b\n\n if r == 0\n puts \"0\"\n else\n puts (q+1)*b - a\n end\n}"}, {"source_code": "t = gets.to_i\nt.times do\n a, b = gets.split.map(&:to_i)\n r = a % b\n puts (r == 0) ? 0 : (b - r)\nend\n"}, {"source_code": "n = gets.to_i\n\nwhile n!=0\n split = gets.split\n a = split[0].to_i\n b = split[1].to_i\n puts a%b == 0 ? 0 : b-a%b\n n-=1\nend"}, {"source_code": "for run in 0...gets.to_i\n\tnums = gets.split(\" \").map { |i| i.to_i }\n\t(nums[0] % nums[1] != 0) ? (puts nums[1] - (nums[0] % nums[1])) : (puts \"0\")\nend"}, {"source_code": "gets.to_i.times do\n a, b = gets.split.map &:to_i\n puts (b - a%b)%b\nend\n"}, {"source_code": "n = gets.to_i\n\n1.upto(n) do \n a, b = gets.split(' ').map(&:to_i)\n puts a % b == 0 ? 0 : b - a%b\nend\n"}, {"source_code": "# frozen_string_literal: true\n\ntest = gets.to_i\ntest.times do\n a, b = gets.chomp.split.map(&:to_i)\n if (a % b).zero?\n puts 0\n else\n puts (a / b + 1) * b - a\n end\nend\n"}, {"source_code": "t = gets.to_i\n(1..t).each do\n arr = gets.split(' ')\n a = arr[0].to_i\n b = arr[1].to_i\n puts (b-(a.modulo(b))).modulo(b)\n\nend\n"}], "negative_code": [{"source_code": "n = gets.to_i\n\nwhile n!=0\n split = gets.split\n a = split[0].to_i\n b = split[1].to_i\n k = 0\n while a%b != 0\n a+=1\n k+=1\n end\n puts k\n n-=1\n puts 'n= ' + n.to_s\nend"}], "src_uid": "d9fd10700cb122b148202a664e7f7689"} {"nl": {"description": "Young boy Artem tries to paint a picture, and he asks his mother Medina to help him. Medina is very busy, that's why she asked for your help.Artem wants to paint an $$$n \\times m$$$ board. Each cell of the board should be colored in white or black. Lets $$$B$$$ be the number of black cells that have at least one white neighbor adjacent by the side. Let $$$W$$$ be the number of white cells that have at least one black neighbor adjacent by the side. A coloring is called good if $$$B = W + 1$$$. The first coloring shown below has $$$B=5$$$ and $$$W=4$$$ (all cells have at least one neighbor with the opposite color). However, the second coloring is not good as it has $$$B=4$$$, $$$W=4$$$ (only the bottom right cell doesn't have a neighbor with the opposite color). Please, help Medina to find any good coloring. It's guaranteed that under given constraints the solution always exists. If there are several solutions, output any of them.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 20$$$). Each of the next $$$t$$$ lines contains two integers $$$n, m$$$ ($$$2 \\le n,m \\le 100$$$) — the number of rows and the number of columns in the grid.", "output_spec": "For each test case print $$$n$$$ lines, each of length $$$m$$$, where $$$i$$$-th line is the $$$i$$$-th row of your colored matrix (cell labeled with 'B' means that the cell is black, and 'W' means white). Do not use quotes. It's guaranteed that under given constraints the solution always exists.", "sample_inputs": ["2\n3 2\n3 3"], "sample_outputs": ["BW\nWB\nBB\nBWB\nBWW\nBWB"], "notes": "NoteIn the first testcase, $$$B=3$$$, $$$W=2$$$.In the second testcase, $$$B=5$$$, $$$W=4$$$. You can see the coloring in the statement."}, "positive_code": [{"source_code": "# https://codeforces.com/problemset/problem/1333/A\n\ngets.to_i.times do\n n, m = gets.split(' ').map(&:to_i)\n\n puts \"B\" * m\n (n-1).times do\n puts \"B\" * (m-1) + \"W\"\n end\nend"}, {"source_code": "1.upto(gets.chomp.to_i) do |t|\n n,m = gets.chomp.split.map(&:to_i)\n if n > 1 && m > 1\n puts 'W' + 'B' *(m-1)\n (n-1).times { puts 'B'*m }\n elsif n == 1 && m > 2\n puts 'BWB' + 'B'*(m-3)\n elsif m == 1 && n > 2\n puts %w(B W B) + ['B']*(n-3)\n end\nend\n"}, {"source_code": "t = gets.to_i\nt.times do\n n, m = gets.split.map(&:to_i)\n for i in 0...n do\n s = \"\"\n for j in 0...m do\n if (n * m) % 2 == 0 && i == 0 && j == 1\n s += \"B\"\n else\n s += ((i + j) % 2 == 0) ? \"B\" : \"W\"\n end\n end\n puts s\n end\nend\n"}, {"source_code": "STR = ('WB' * 51).freeze\n\n1.upto(gets.to_i) do\n n, m = gets.split(' ').map(&:to_i)\n\n arr = 1.upto(n).map { |i| STR[i%2, m] }\n\n if arr.last.chars.last == 'W'\n arr.last[arr.last.length-1] = 'B'\n elsif arr.last.chars.first == 'W'\n arr.last[0] = 'B'\n end\n\n puts arr.join(\"\\n\")\nend\n"}], "negative_code": [{"source_code": "STR = ('WB' * 51).freeze\n\n1.upto(gets.to_i) do\n n, m = gets.split(' ').map(&:to_i)\n\n 1.upto(n) do |i|\n s = STR[i%2, m]\n\n s[s.size - 1] = 'B' if i==n && (n*m).even?\n\n puts s\n end\nend\n"}], "src_uid": "2b37f27a98ec8f80d0bff3f7ae8f2cff"} {"nl": {"description": "Try guessing the statement from this picture: You are given a non-negative integer $$$d$$$. You have to find two non-negative real numbers $$$a$$$ and $$$b$$$ such that $$$a + b = d$$$ and $$$a \\cdot b = d$$$.", "input_spec": "The first line contains $$$t$$$ ($$$1 \\le t \\le 10^3$$$) — the number of test cases. Each test case contains one integer $$$d$$$ $$$(0 \\le d \\le 10^3)$$$.", "output_spec": "For each test print one line. If there is an answer for the $$$i$$$-th test, print \"Y\", and then the numbers $$$a$$$ and $$$b$$$. If there is no answer for the $$$i$$$-th test, print \"N\". Your answer will be considered correct if $$$|(a + b) - a \\cdot b| \\le 10^{-6}$$$ and $$$|(a + b) - d| \\le 10^{-6}$$$.", "sample_inputs": ["7\n69\n0\n1\n4\n5\n999\n1000"], "sample_outputs": ["Y 67.985071301 1.014928699\nY 0.000000000 0.000000000\nN\nY 2.000000000 2.000000000\nY 3.618033989 1.381966011\nY 997.998996990 1.001003010\nY 998.998997995 1.001002005"], "notes": null}, "positive_code": [{"source_code": "=begin\nn = gets.chomp.to_i\n\narr = gets.split(\" \")\narr = arr.map { |element| element.to_i }\nsum = arr.inject(0){|sum,x| sum + x }\n\n=end\n\nn = gets.chomp.to_i\narr = []\n\nn.times do |x|\n\tarr<= 0 \n\t\tb = (d + Math.sqrt(x))/2\n\t\ta = d/b\n\t\ta = 0.0 if b == 0\n\t\tputs \"Y \"+a.to_s+\" \"+b.to_s\n\telse\n\t\tputs \"N\"\n\tend\nend\n"}, {"source_code": "gets;$<.each{|e|\nn=e.to_i\nd=n*(n-4)\nputs d<0 ? :N : ('Y %.9f %.9f'%[n-x=(n-d**0.5)/2,x])\n}"}, {"source_code": "for i in gets.chomp.to_i.times do\n d = gets.chomp.to_i\n delta = d*d - 4*d\n if delta < 0\n puts 'N'\n else \n puts \"Y \" + ((d + Math.sqrt(delta))/2.0).to_s + \" \" + ((d - Math.sqrt(delta))/2.0).to_s\nend\nend"}], "negative_code": [{"source_code": "=begin\nn = gets.chomp.to_i\n\narr = gets.split(\" \")\narr = arr.map { |element| element.to_i }\nsum = arr.inject(0){|sum,x| sum + x }\n\n=end\n\nn = gets.chomp.to_i\narr = []\n\nn.times do |x|\n\tarr<= 0 \n\t\tb = (d + Math.sqrt(x))/2\n\t\ta = d/b\n\t\ta = 0.0 if b == 0\n\t\tp \"Y \"+a.to_s+\" \"+b.to_s\n\telse\n\t\tp \"N\"\n\tend\nend\n"}, {"source_code": "=begin\nn = gets.chomp.to_i\n\narr = gets.split(\" \")\narr = arr.map { |element| element.to_i }\nsum = arr.inject(0){|sum,x| sum + x }\n\n=end\n\nn = gets.chomp.to_i\narr = []\n\nn.times do |x|\n\tarr<= 0 \n\t\tb = (d + Math.sqrt(x))/2\n\t\ta = d/b\n\t\ta = 0.0 if b == 0\n\t\tputs \"Y \"+a.to_s+\" \"+b.to_s\n\telse\n\t\tp \"N\"\n\tend\nend\n"}, {"source_code": "gets;$<.each{|e|\nn=e.to_i\nd=n*(n-4)\nputs d<0 ? :N : ('Y %f %f'%[x=(n-d**0.5)/2,n-x])\n}"}, {"source_code": "gets;$<.each{|e|\nn=e.to_i\nd=n*(n-4)\nputs d<0 ? :N : ('Y %f %f'%[n-x=(n-d**0.5)/2,x])\n}"}], "src_uid": "6f5d41346419901c830233b3bf5c9e65"} {"nl": {"description": "One day Squidward, Spongebob and Patrick decided to go to the beach. Unfortunately, the weather was bad, so the friends were unable to ride waves. However, they decided to spent their time building sand castles.At the end of the day there were n castles built by friends. Castles are numbered from 1 to n, and the height of the i-th castle is equal to hi. When friends were about to leave, Squidward noticed, that castles are not ordered by their height, and this looks ugly. Now friends are going to reorder the castles in a way to obtain that condition hi ≤ hi + 1 holds for all i from 1 to n - 1.Squidward suggested the following process of sorting castles: Castles are split into blocks — groups of consecutive castles. Therefore the block from i to j will include castles i, i + 1, ..., j. A block may consist of a single castle. The partitioning is chosen in such a way that every castle is a part of exactly one block. Each block is sorted independently from other blocks, that is the sequence hi, hi + 1, ..., hj becomes sorted. The partitioning should satisfy the condition that after each block is sorted, the sequence hi becomes sorted too. This may always be achieved by saying that the whole sequence is a single block. Even Patrick understands that increasing the number of blocks in partitioning will ease the sorting process. Now friends ask you to count the maximum possible number of blocks in a partitioning that satisfies all the above requirements.", "input_spec": "The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of castles Spongebob, Patrick and Squidward made from sand during the day. The next line contains n integers hi (1 ≤ hi ≤ 109). The i-th of these integers corresponds to the height of the i-th castle.", "output_spec": "Print the maximum possible number of blocks in a valid partitioning.", "sample_inputs": ["3\n1 2 3", "4\n2 1 3 2"], "sample_outputs": ["3", "2"], "notes": "NoteIn the first sample the partitioning looks like that: [1][2][3]. In the second sample the partitioning is: [2, 1][3, 2] "}, "positive_code": [{"source_code": "n = gets.chomp.to_i\nhs = gets.chomp.split.map(&:to_i)\nlmaxs = Array.new(n)\nrmins = Array.new(n)\n\nlmaxs[0] = hs[0]\nrmins[-1] = hs[-1]\nfor i in 1...n\n ri = n - i - 1\n lmaxs[i] = [lmaxs[i - 1], hs[i]].max\n rmins[ri] = [rmins[ri + 1], hs[ri]].min\nend\n\ncnt = 1\nfor i in 0...n - 1\n f = true\n f &&= lmaxs[i] <= rmins[i + 1]\n cnt += 1 if f\nend\nputs cnt\n"}, {"source_code": "n = gets.chomp.to_i\nh = gets.split(\" \").map{|i| i.to_i}\nh = [0]+h\nhmax = [0]*(n+1)\nhmin = [0]*(n+1)\nhmax[1] = h[1]\nhmin[n] = h[n]\nfor i in 2..n\n\thmax[i] = [hmax[i-1], h[i]].max\nend\nfor i in (1..(n-1)).to_a.reverse\n\thmin[i] = [hmin[i+1], h[i]].min\nend\ns = 1\nfor i in 1...n\n\tif hmax[i] <= hmin[i+1]\n\t\ts +=1\n\tend\nend\nputs s\n"}, {"source_code": "class Solver\n\tdef main\n\t\tn = gets.to_i\n\t\th = gets.split.map { |x| x.to_i }\n\t\tpre_max = Array.new(n + 2, 0)\n\t\tsuf_min = Array.new(n + 2, 1e9.to_i)\n\t\t1.upto(n) do |i|\n\t\t\tpre_max[i] = [pre_max[i - 1], h[i - 1]].max\n\t\tend\n\t\tn.downto(1) do |i|\n\t\t\tsuf_min[i] = [suf_min[i + 1], h[i - 1]].min\n\t\tend\n\t\tgap = (1...n).count { |i| pre_max[i] <= suf_min[i + 1] }\n\t\tputs gap + 1\n\tend\nend\n\nSolver.new.main\n"}], "negative_code": [{"source_code": "n = gets.chomp.to_i\nhs = gets.chomp.split.map(&:to_i)\nlmins = Array.new(n)\nlmaxs = Array.new(n)\nrmins = Array.new(n)\nrmaxs = Array.new(n)\n\nlmins[0] = hs[0]\nlmaxs[0] = hs[0]\nrmins[-1] = hs[-1]\nrmaxs[-1] = hs[-1]\nfor i in 1...n\n ri = n - i - 1\n lmins[i] = [lmins[i - 1], hs[i]].min\n lmaxs[i] = [lmaxs[i - 1], hs[i]].max\n rmins[ri] = [rmins[ri + 1], hs[ri]].min\n rmaxs[ri] = [rmaxs[ri + 1], hs[ri]].max\nend\n\ncnt = 1\nfor i in 0...n - 1\n if lmins[i] <= rmins[i + 1] && rmaxs[i] <= rmaxs[i + 1]\n cnt += 1\n end\nend\nputs cnt\n"}, {"source_code": "n = gets.chomp.to_i\nhs = gets.chomp.split.map(&:to_i)\n\nis = Hash.new(-1)\nhs.each.with_index do |h, i|\n is[h] = [is[h], i].max\nend\n\ncnt = 0\nidx = -1\nhs.sort.uniq.each do |h|\n if idx < is[h]\n cnt += 1\n idx = is[h]\n end\nend\n\nputs cnt\n"}], "src_uid": "c1158d23d3ad61c346c345f14e63ede4"} {"nl": {"description": "You have two binary strings $$$a$$$ and $$$b$$$ of length $$$n$$$. You would like to make all the elements of both strings equal to $$$0$$$. Unfortunately, you can modify the contents of these strings using only the following operation: You choose two indices $$$l$$$ and $$$r$$$ ($$$1 \\le l \\le r \\le n$$$); For every $$$i$$$ that respects $$$l \\le i \\le r$$$, change $$$a_i$$$ to the opposite. That is, $$$a_i := 1 - a_i$$$; For every $$$i$$$ that respects either $$$1 \\le i < l$$$ or $$$r < i \\le n$$$, change $$$b_i$$$ to the opposite. That is, $$$b_i := 1 - b_i$$$. Your task is to determine if this is possible, and if it is, to find such an appropriate chain of operations. The number of operations should not exceed $$$n + 5$$$. It can be proven that if such chain of operations exists, one exists with at most $$$n + 5$$$ operations. ", "input_spec": "Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 10^5$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \\le n \\le 2 \\cdot 10^5$$$) — the length of the strings. The second line of each test case contains a binary string $$$a$$$, consisting only of characters 0 and 1, of length $$$n$$$. The third line of each test case contains a binary string $$$b$$$, consisting only of characters 0 and 1, of length $$$n$$$. It is guaranteed that sum of $$$n$$$ over all test cases doesn't exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each testcase, print first \"YES\" if it's possible to make all the elements of both strings equal to $$$0$$$. Otherwise, print \"NO\". If the answer is \"YES\", on the next line print a single integer $$$k$$$ ($$$0 \\le k \\le n + 5$$$) — the number of operations. Then $$$k$$$ lines follows, each contains two integers $$$l$$$ and $$$r$$$ ($$$1 \\le l \\le r \\le n$$$) — the description of the operation. If there are several correct answers, print any of them.", "sample_inputs": ["5\n\n3\n\n010\n\n101\n\n2\n\n11\n\n10\n\n4\n\n1000\n\n0011\n\n2\n\n10\n\n10\n\n3\n\n111\n\n111"], "sample_outputs": ["YES\n1\n2 2\nNO\nNO\nYES\n2\n1 2\n2 2\nYES\n2\n1 1\n2 3"], "notes": "NoteIn the first test case, we can perform one operation with $$$l = 2$$$ and $$$r = 2$$$. So $$$a_2 := 1 - 1 = 0$$$ and string $$$a$$$ became equal to 000. $$$b_1 := 1 - 1 = 0$$$, $$$b_3 := 1 - 1 = 0$$$ and string $$$b$$$ became equal to 000.In the second and in the third test cases, it can be proven that it's impossible to make all elements of both strings equal to $$$0$$$.In the fourth test case, we can perform an operation with $$$l = 1$$$ and $$$r = 2$$$, then string $$$a$$$ became equal to 01, and string $$$b$$$ doesn't change. Then we perform an operation with $$$l = 2$$$ and $$$r = 2$$$, then $$$a_2 := 1 - 1 = 0$$$ and $$$b_1 = 1 - 1 = 0$$$. So both of string $$$a$$$ and $$$b$$$ became equal to 00.In the fifth test case, we can perform an operation with $$$l = 1$$$ and $$$r = 1$$$. Then string $$$a$$$ became equal to 011 and string $$$b$$$ became equal to 100. Then we can perform an operation with $$$l = 2$$$ and $$$r = 3$$$, so both of string $$$a$$$ and $$$b$$$ became equal to 000."}, "positive_code": [{"source_code": "gets.to_i.times do\n n=gets.chomp.to_i\n arr1=gets.chomp.split('').map(&:to_i)\n arr2=gets.chomp.split('').map(&:to_i)\n valid=true\n ans=0\n op=Array.new\n onesStart=-1\n n.times do |i|\n if (arr1[0]+arr2[0])%2!=(arr1[i]+arr2[i])%2\n valid=false\n break\n end\n if arr1[i]==1\n if onesStart==-1\n onesStart=i\n end\n if i==n-1\n ans+=1\n if (arr1[0]+arr2[0])%2!=ans%2\n ans+=1\n if onesStart!=0\n op.push([onesStart-1,onesStart-1])\n onesStart-=1\n else\n op.push([0,0])\n onesStart=1\n end\n end\n op.push([onesStart,i])\n end\n end\n if arr1[i]== 0 && onesStart!=-1\n op.push([onesStart,i-1])\n onesStart=-1\n ans+=1\n end\n end\n if !valid\n puts \"NO\"\n else\n if (arr1[0]+arr2[0])%2!=ans%2\n ans+=1\n lst=op[-1]\n if op.length==0\n ans+=2\n op.push([0,0])\n op.push([1,n-1])\n op.push([0,n-1])\n else\n op.pop\n op.push([lst[1]+1,lst[1]+1])\n op.push([lst[0],lst[1]+1])\n end\n end\n puts \"YES\"\n puts ans\n op.each do|a|\n puts \"#{a[0]+1} #{a[1]+1}\"\n end\n end\n\nend\n\n"}], "negative_code": [{"source_code": "gets.to_i.times do\n n=gets.chomp.to_i\n arr1=gets.chomp.split('').map(&:to_i)\n arr2=gets.chomp.split('').map(&:to_i)\n valid=true\n ans=0\n op=Array.new\n onesStart=-1\n n.times do |i|\n if (arr1[0]+arr2[0])%2!=(arr1[i]+arr2[i])%2\n valid=false\n break\n end\n if arr1[i]==1\n if onesStart==-1\n onesStart=i\n end\n if i==n-1\n ans+=1\n if (arr1[0]+arr2[0])%2!=ans%2\n ans+=1\n if onesStart!=0\n op.push([onesStart-1,onesStart-1])\n onesStart-=1\n else\n op.push([0,0])\n onesStart=1\n end\n end\n op.push([onesStart,i])\n end\n end\n if arr1[i]== 0 && onesStart!=-1\n op.push([onesStart,i-1])\n onesStart=-1\n ans+=1\n end\n end\n if !valid\n puts \"NO\"\n else\n if (arr1[0]+arr2[0])%2!=ans%2\n ans+=1\n lst=op[-1]\n if op.length==0\n op.push([0,n-1])\n else\n op.pop\n op.push([lst[1]+1,lst[1]+1])\n op.push([lst[0],lst[1]+1])\n end\n end\n puts \"YES\"\n puts ans\n op.each do|a|\n puts \"#{a[0]+1} #{a[1]+1}\"\n end\n end\n\nend\n\n"}, {"source_code": "gets.to_i.times do\n n=gets.chomp.to_i\n arr1=gets.chomp.split('').map(&:to_i)\n arr2=gets.chomp.split('').map(&:to_i)\n valid=true\n ans=0\n op=Array.new\n onesStart=-1\n n.times do |i|\n if (arr1[0]+arr2[0])%2!=(arr1[i]+arr2[i])%2\n valid=false\n break\n end\n if arr1[i]==1\n if onesStart==-1\n onesStart=i\n end\n if i==n-1\n ans+=1\n if (arr1[0]+arr2[0])%2!=ans%2\n ans+=1\n if onesStart!=0\n op.push([onesStart-1,onesStart-1])\n else\n op.push([0,0])\n onesStart=1\n end\n end\n op.push([onesStart,i])\n end\n end\n if arr1[i]== 0 && onesStart!=-1\n op.push([onesStart,i-1])\n onesStart=-1\n ans+=1\n end\n end\n if !valid\n puts \"NO\"\n else\n if (arr1[0]+arr2[0])%2!=ans%2\n ans+=1\n lst=op[-1]\n if op.length==0\n op.push([0,n-1])\n else\n op.pop\n op.push([lst[1]+1,lst[1]+1])\n op.push([lst[0],lst[1]+1])\n end\n end\n puts \"YES\"\n puts ans\n op.each do|a|\n puts \"#{a[0]+1} #{a[1]+1}\"\n end\n end\n\nend\n\n"}], "src_uid": "cc9abcff3224118b533881335e4c582b"} {"nl": {"description": "Find the minimum area of a square land on which you can place two identical rectangular $$$a \\times b$$$ houses. The sides of the houses should be parallel to the sides of the desired square land.Formally, You are given two identical rectangles with side lengths $$$a$$$ and $$$b$$$ ($$$1 \\le a, b \\le 100$$$) — positive integers (you are given just the sizes, but not their positions). Find the square of the minimum area that contains both given rectangles. Rectangles can be rotated (both or just one), moved, but the sides of the rectangles should be parallel to the sides of the desired square. Two rectangles can touch each other (side or corner), but cannot intersect. Rectangles can also touch the sides of the square but must be completely inside it. You can rotate the rectangles. Take a look at the examples for a better understanding. The picture shows a square that contains red and green rectangles. ", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 10\\,000$$$) —the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case is a line containing two integers $$$a$$$, $$$b$$$ ($$$1 \\le a, b \\le 100$$$) — side lengths of the rectangles.", "output_spec": "Print $$$t$$$ answers to the test cases. Each answer must be a single integer — minimal area of square land, that contains two rectangles with dimensions $$$a \\times b$$$.", "sample_inputs": ["8\n3 2\n4 2\n1 1\n3 1\n4 7\n1 3\n7 4\n100 100"], "sample_outputs": ["16\n16\n4\n9\n64\n9\n64\n40000"], "notes": "NoteBelow are the answers for the first two test cases: "}, "positive_code": [{"source_code": "gets.to_i.times do\n\ta, b = gets.split.map(&:to_i).sort\n\tputs [a * 2, b].max**2\nend\n"}, {"source_code": "t = gets.to_i\nt.times do\n\ta,b = gets.split.map(&:to_i)\n\tputs [[a,b].max, [a,b].min*2].max ** 2\nend"}, {"source_code": "gets.strip.to_i.times do |t|\n a, b = gets.strip.split(' ').map(&:to_i)\n puts [[2 * a, b].max ** 2, [2 * b, a].max ** 2].min\nend\n"}, {"source_code": "t = gets.chomp.to_i\nt.times do |testcase|\n (a,b) = gets.split.map(&:to_i)\n puts [[2*a , b].max , [2*b , a].max].min ** 2\nend"}, {"source_code": "n = gets.to_i\n\n1.upto n do\n a, b = gets.split.map(&:to_i)\n if a == b\n puts ((a * 2) ** 2)\n else\n if [a,b].max > ([a,b].min * 2)\n puts ([a,b].max ** 2)\n else\n puts (([a,b].min * 2) ** 2)\n end\n end\nend"}, {"source_code": "gets.to_i.times {\n a, b=gets.split.map(&:to_i).minmax\n puts [a+a, b].max**2\n}\n"}, {"source_code": "gets.to_i.times {\n a, b = gets.split.map { |v|\n v.to_i\n }\n if a == b\n puts (a * 2) ** 2\n else\n if a > b\n a, b = b, a\n end\n a2 = a * 2\n if a2 < b\n puts b * b\n else\n puts a2 * a2\n end\n end\n}\n"}, {"source_code": "i = gets.chomp.to_i\n\ndef minimalsquare(a, b)\n if a < b\n minimal = Math.sqrt((a * 2) ** 2).ceil\n minimal2 = Math.sqrt((a * b) * 2).ceil\n if (minimal * minimal) >= (minimal2 * minimal2)\n return minimal * minimal\n else\n if minimal2 >= b\n return minimal2 * minimal2\n else\n return b ** 2\n end\n end\n else\n minimal = Math.sqrt((b * 2) ** 2).ceil\n minimal2 = Math.sqrt((a * b) * 2).ceil\n if (minimal * minimal) >= (minimal2 * minimal2)\n return minimal * minimal\n else\n if minimal2 >= a\n return minimal2 * minimal2\n else\n return a ** 2\n end\n end\n end\nend\n\nwhile (i > 0)\n a, b = gets.split.map(&:to_i)\n\n puts minimalsquare(a, b)\n\n i -= 1\nend"}, {"source_code": "t = gets.to_i\nt.times do\n a, b = gets.split.map(&:to_i).sort\n if a * 2 >= b\n puts (a * 2) ** 2\n else\n puts b ** 2\n end\nend\n"}, {"source_code": "gets.to_i.times do\n r = gets.split(' ').map(&:to_i)\n puts [2 * r.min, r.max].max ** 2\nend"}, {"source_code": "gets.to_i.times do\n a, b = gets.split(' ').map(&:to_i)\n\n puts [2*[a, b].min, [a, b].max].max ** 2\nend\n"}], "negative_code": [{"source_code": "t = gets.chomp.to_i\nt.times do |testcase|\n (a,b) = gets.split.map(&:to_i)\n puts [2*a , 2*b].min ** 2\nend"}, {"source_code": "n = gets.to_i\n\n1.upto n do\n a, b = gets.split.map(&:to_i)\n if a == 1 and b == 1\n puts 4\n elsif a == 1 or b == 1\n puts ([a,b].max) ** 2\n else\n puts ([a,b].min * 2) ** 2\n end\nend"}, {"source_code": "i = gets.chomp.to_i\n\ndef minimalsquare(a, b)\n if a < b\n minimal = Math.sqrt((a * 2) ** 2).ceil\n if (minimal * minimal) >= ((a * b) * 2)\n return minimal * minimal\n else\n minimal = Math.sqrt((a * b) * 2).ceil\n return minimal * minimal\n end\n else\n minimal = Math.sqrt((b * 2) ** 2).ceil\n if (minimal * minimal) >= ((a * b) * 2)\n return minimal * minimal\n else\n minimal = Math.sqrt((a * b) * 2).ceil\n return minimal * minimal\n end\n end\nend\n\nwhile (i > 0)\n a, b = gets.split.map(&:to_i)\n\n puts minimalsquare(a, b)\n\n i -= 1\nend"}, {"source_code": "gets.to_i.times do\n r = gets.to_s.split(' ').map(&:to_i)\n l = r.max\n b = r.min\n if l == b || b*2 >= l\n puts b*b*4\n else\n puts ((l/b.to_f).ceil)**2\n end\nend"}], "src_uid": "3bb093fb17d6b76ae340fab44b08fcb8"} {"nl": {"description": "At many competitions that have a word «cup» in its official name the winner is presented with an actual cup. This time the organizers of one unusual programming competition have decided to please the winner even more and to add a nameplate to the cup with the handle of the winner.The nameplate is to be rectangular and the text on it will be printed as a table of several rows and columns. Having some measurements done, the organizers have found out that the number $$$a$$$ of rows cannot be greater than $$$5$$$ while the number $$$b$$$ of columns cannot exceed $$$20$$$. Every cell of the table will contain either an asterisk («*») or a letter of user's handle.Furthermore, the organizers want the rows of the table to be uniform, which means that the number of asterisks used in different rows should differ by at most one (i.e. you can't have two asterisks in the first row and none in the second). The main goal, however, is to obtain the winner's handle precisely when reading the table from top to bottom and from left to right in every row (skipping asterisks).The organizers want for the nameplate to have as few rows as possible and among all valid tables with the minimum number of rows they want to choose the one that has the minimum number of columns.The winner is not yet determined so your task is to write a program that, given a certain handle, generates the necessary table.", "input_spec": "The only line contains one string $$$s$$$ ($$$1 \\le |s| \\le 100$$$), comprised of uppercase and lowercase Latin letters,  — the handle of the winner.", "output_spec": "In the first line output the minimum number $$$a$$$ of rows in the table and the minimum number $$$b$$$ of columns in an optimal table with rows. The following $$$a$$$ lines should contain $$$b$$$ characters each  — any valid table.", "sample_inputs": ["tourist", "MyNameIsLifeIAmForeverByYourSideMyNameIsLife"], "sample_outputs": ["1 7\ntourist", "3 15\nMyNameIsLifeIAm\nForeverByYourSi\ndeMyNameIsL*ife"], "notes": null}, "positive_code": [{"source_code": "\na = gets.strip\n# a = \"00000000000000012000000000000001200000000000000120000000000001\"\nif a.length > 20\n rows = 1\n while a.length.to_f/rows > 20 do\n rows +=1\n end\n if a.length % rows == 0\n print \"#{rows} #{a.length/rows}\\n\"\n a.split(\"\").each_slice(a.length/rows).to_a.each do |x|\n print \"#{x.join}\\n\"\n end\n else\n count_of_stars = 0\n while (a.length + count_of_stars) % rows != 0 do\n count_of_stars += 1\n end\n symbols_in_row = (a.length + count_of_stars)/rows\n print \"#{rows} #{symbols_in_row}\\n\"\n row_num = 1\n n = count_of_stars\n a.split(\"\").each_with_index do |x,i|\n if count_of_stars > 0\n # p \"kek: #{row_num*(symbols_in_row-1)}\"\n if i == row_num*(symbols_in_row-1)\n row_num+=1\n count_of_stars-=1\n print \"*\\n\"\n end\n else\n if i == row_num*symbols_in_row-n\n row_num+=1\n print \"\\n\"\n end\n end\n print x\n end\n end\nelse\n print \"1 #{a.length}\\n\"\n print a\nend\n"}, {"source_code": "text = gets.chomp\nlen = text.length\n\nreqa = 0\nreqb = 0\n\ndone = false\n\n(1..5).each do |a|\n (1..20).each do |b|\n tot = a*b\n if tot 0 && a[i] == a[i-1] do\n i -= 1\n k += 1\n end\n\n if i > 0\n i -= 1\n while k % x == 0 && a[n-1] > a[i] do\n k = k/x\n a[n-1] -= 1\n end\n if a[n-1] != a[i] || a[i] == 0\n kt = 0\n else\n k += 1\n end\n else\n while k % x == 0 do\n k = k/x\n a[n-1] -= 1\n end\n kt = 0\n end\nend\n\nresult = s - a[n-1]\n\ndef modular_pow base, exponent, modulus\n result1 = 1\n while exponent > 0\n if (exponent % 2 == 1)\n result1 = (result1 * base) % modulus\n end\n exponent /= 2\n base = (base * base) % modulus\n end\n return result1\nend\n\nif result > s\n result = s\nend\n\nkq = modular_pow x, result, 1000000007\n\nputs kq"}], "negative_code": [{"source_code": "require 'openssl'\nx,n=gets.split.map(&:to_i)\na=gets.split.map(&:to_i)\nm=10**9+7\ns=a.reduce(&:+)\na.map!{|x|s-x}\np=a.min\na=a.group_by{|x|x}.sort.map{|a,b|[a,b.size]}\ni=0\nwhile a[i]\n q,d=a[i][1].divmod x\n if d==0\n p+=1\n i+=1\n a[i]||=[i,0]\n a[i][1]+=q\n else\n break\n end\nend\np x.to_bn.mod_exp([p,s].min,m)"}, {"source_code": "require 'openssl'\nx,n=gets.split.map(&:to_i)\na=gets.split.map(&:to_i)\nm=10**9+7\ns=a.reduce(&:+)\na.map!{|x|s-x}\np=a.min\na=Hash[a.map{|x|x-p}.inject(Hash.new(0)){|h,v|h[v]+=1;h}.sort]\ni=0\nloop do\n q,d=a[i].divmod x\n if d==0\n p+=1\n i+=1\n a[i]||=0\n a[i]+=q\n else\n break\n end\nend\np x.to_bn.mod_exp([p,s].min,m)"}, {"source_code": "b = gets.chomp.split(\" \").map{|i| i.to_i}\na = gets.chomp.split(\" \").map{|i| i.to_i}\nn = b[0]\nx = b[1]\n\ns = 0\n\n(0..n-1).each do |i|\n s += a[i]\nend\n\ni = n-1\nj = 1\nk = 1\nresult = s - a[n-1]\nkt = 1\n\nwhile i> 0 && kt do\n kt = 1\n while i > 0 && a[i] == a[i-1] do\n i -= 1\n k += 1\n end\n\n if i > 0\n i -= 1\n while k % x == 0 && a[n-1] > a[i] do\n k = k/x\n a[n-1] -= 1\n result += 1\n end\n if a[n-1] != a[i]\n kt = 0\n else \n k += 1\n end\n else\n while k % x == 0 do\n k = k/x\n a[n-1] -= 1\n result += 1\n end\n end\nend\n\nkq = x**result % 1000000007\n\nputs result"}, {"source_code": "b = gets.chomp.split(\" \").map{|i| i.to_i}\na = gets.chomp.split(\" \").map{|i| i.to_i}\nn = b[0]\nx = b[1]\n\ns = 0\n\n(0..n-1).each do |i|\n s +=a[i]\nend\n\nresult = s - a[n-1]\n\ni = n-1\nwhile i > 0 && a[i-1] == a[i] do\n i -= 1\nend\n\nk = n - i\nwhile k % x == 0\n k = k/x\n result += 1\nend\n\nkq = x**result % 1000000007\n\nputs kq"}, {"source_code": "b = gets.chomp.split(\" \").map{|i| i.to_i}\na = gets.chomp.split(\" \").map{|i| i.to_i}\nn = b[0]\nx = b[1]\n\ns = 0\n\n(0..n-1).each do |i|\n s += a[i]\nend\n\ni = n-1\nj = 1\nk = 1\nkt = 1\n\nwhile kt == 1 do\n while i > 0 && a[i] == a[i-1] do\n i -= 1\n k += 1\n end\n\n if i > 0\n i -= 1\n while k % x == 0 && a[n-1] > a[i] do\n k = k/x\n a[n-1] -= 1\n end\n if a[n-1] != a[i] \n kt = 0\n else\n k += 1\n end\n else\n while k % x == 0 do\n k = k/x\n a[n-1] -= 1\n end\n kt = 0\n end\nend\n\nresult = s - a[n-1]\n\ndef modular_pow base, exponent, modulus\n result1 = 1\n while exponent > 0\n if (exponent % 2 == 1)\n result1 = (result1 * base) % modulus\n end\n exponent /= 2\n base = (base * base) % modulus\n end\n return result1\nend\n\nkq = modular_pow x, result, 1000000007\n\nputs kq"}, {"source_code": "b = gets.chomp.split(\" \").map{|i| i.to_i}\na = gets.chomp.split(\" \").map{|i| i.to_i}\nn = b[0]\nx = b[1]\n\ns = 0\n\n(0..n-1).each do |i|\n s += a[i]\nend\n\ni = n-1\nj = 1\nk = 1\nresult = s - a[n-1]\nkt = 1\n\nif x!= 1 \n while kt == 1 do\n kt = 1\n while i > 0 && a[i] == a[i-1] do\n i -= 1\n k += 1\n end\n\n if i > 0\n i -= 1\n while k % x == 0 && a[n-1] > a[i] do\n k = k/x\n a[n-1] -= 1\n result += 1\n end\n if a[n-1] != a[i]\n kt = 0\n else \n k += 1\n end\n else\n while k % x == 0 do\n k = k/x\n a[n-1] -= 1\n result += 1\n end\n kt = 0\n end\n end\nelse\n result = 1\nend\n\nkq = x**result % 1000000007\n\nputs kq"}, {"source_code": "b = gets.chomp.split(\" \").map{|i| i.to_i}\na = gets.chomp.split(\" \").map{|i| i.to_i}\nn = b[0]\nx = b[1]\n\ns = 0\n\n(0..n-1).each do |i|\n s += a[i]\nend\n\ni = n-1\nj = 1\nk = 1\nresult = s - a[n-1]\nkt = 1\n\nwhile kt == 1 do\n kt = 1\n while i > 0 && a[i] == a[i-1] do\n i -= 1\n k += 1\n end\n\n if i > 0\n i -= 1\n while k % x == 0 && a[n-1] > a[i] do\n k = k/x\n a[n-1] -= 1\n result += 1\n end\n if a[n-1] != a[i]\n kt = 0\n else\n k += 1\n end\n else\n while k % x == 0 do\n k = k/x\n a[n-1] -= 1\n result += 1\n end\n kt = 0\n end\nend\n\nkq = ( x**result) % 1000000007\n\nputs kq"}, {"source_code": "b = gets.chomp.split(\" \").map{|i| i.to_i}\na = gets.chomp.split(\" \").map{|i| i.to_i}\nn = b[0]\nx = b[1]\nsodu = []\n\ns = 0\n\n(0..n-1).each do |i|\n s += a[i]\nend\n\ni = n-1\nj = 1\nk = 1\nkt = 1\n\nwhile kt == 1 do\n kt = 1\n while i > 0 && a[i] == a[i-1] do\n i -= 1\n k += 1\n end\n\n if i > 0\n i -= 1\n while k % x == 0 && a[n-1] > a[i] do\n k = k/x\n a[n-1] -= 1\n end\n if a[n-1] != a[i] || a[i] == 0\n kt = 0\n else\n k += 1\n end\n else\n while k % x == 0 do\n k = k/x\n a[n-1] -= 1\n end\n kt = 0\n end\nend\n\nresult = s - a[n-1]\n\ntuanhoan = 0\n\nsodu[0] = 1\n(1..result).each do |i|\n sodu[i] = (sodu[i-1]*x) % 1000000007\n if sodu[i] == 1\n tuanhoan = i\n break\n end\nend\n\nif tuanhoan != 0\n kq = sodu[result%tuanhoan]\nelse\n kq = sodu[result]\nend\n\nputs tuanhoan"}, {"source_code": "b = gets.chomp.split(\" \").map{|i| i.to_i}\na = gets.chomp.split(\" \").map{|i| i.to_i}\nn = b[0]\nx = b[1]\n\ns = 0\n\n(0..n-1).each do |i|\n s += a[i]\nend\n\ni = n-1\nj = 1\nk = 1\nresult = s - a[n-1]\nkt = 1\n\nwhile i> 0 && kt do\n kt = 1\n while i > 0 && a[i] == a[i-1] do\n i -= 1\n k += 1\n end\n\n if i > 0\n i -= 1\n while k % x == 0 && a[n-1] > a[i] do\n k = k/x\n a[n-1] -= 1\n result += 1\n end\n if a[n-1] != a[i]\n kt = 0\n end\n else\n while k % x == 0 do\n k = k/x\n a[n-1] -= 1\n result += 1\n end\n end\nend\n\nkq = x**result % 1000000007\n\nputs kq"}, {"source_code": "b = gets.chomp.split(\" \").map{|i| i.to_i}\na = gets.chomp.split(\" \").map{|i| i.to_i}\nn = b[0]\nx = b[1]\nsodu1 = []\n\ns = 0\n\n(0..n-1).each do |i|\n s += a[i]\nend\n\ni = n-1\nj = 1\nk = 1\nkt = 1\n\nwhile kt == 1 do\n kt = 1\n while i > 0 && a[i] == a[i-1] do\n i -= 1\n k += 1\n end\n\n if i > 0\n i -= 1\n while k % x == 0 && a[n-1] > a[i] do\n k = k/x\n a[n-1] -= 1\n end\n if a[n-1] != a[i] || a[i] == 0\n kt = 0\n else\n k += 1\n end\n else\n while k % x == 0 do\n k = k/x\n a[n-1] -= 1\n end\n kt = 0\n end\nend\n\nresult = s - a[n-1]\n\nsodu1[0] = 1\n(1..1000000).each do |i|\n sodu1[i] = (sodu1[i-1] * x ) % 1000000007\nend\n\nif result > 1000000\n sodu = sodu1[1000000]\n thuong = result/1000000\n du = result % 1000000\n (1..thuong).each do |i|\n sodu = (sodu * x) % 1000000007\n end\n if du != 0\n (1..du).each do |j|\n sodu = ( sodu * x) % 1000000007\n end\n end\nend\n\nif result <= 1000000\n kq = sodu1[result]\nelse\n kq = sodu \nend\n\nputs sodu"}, {"source_code": "b = gets.chomp.split(\" \").map{|i| i.to_i}\na = gets.chomp.split(\" \").map{|i| i.to_i}\nn = b[0]\nx = b[1]\n\ns = 0\n\n(0..n-1).each do |i|\n s += a[i]\nend\n\ni = n-1\nj = 1\nk = 1\nresult = s - a[n-1]\nkt = 1\n\nwhile kt == 1 do\n kt = 1\n while i > 0 && a[i] == a[i-1] do\n i -= 1\n k += 1\n end\n\n if i > 0\n i -= 1\n while k % x == 0 && a[n-1] > a[i] do\n k = k/x\n a[n-1] -= 1\n result += 1\n end\n if a[n-1] != a[i]\n kt = 0\n else \n k += 1\n end\n else\n while k % x == 0 do\n k = k/x\n a[n-1] -= 1\n result += 1\n end\n kt = 0\n end\nend\n\nkq = x**result % 1000000007\n\nputs kq"}, {"source_code": "b = gets.chomp.split(\" \").map{|i| i.to_i}\na = gets.chomp.split(\" \").map{|i| i.to_i}\nn = b[0]\nx = b[1]\n\ns = 0\n\n(0..n-1).each do |i|\n s += a[i]\nend\n\ni = n-1\nj = 1\nk = 1\nresult = s - a[n-1]\nkt = 1\n\nputs result\n\nwhile i> 0 && kt do\n kt = 1\n while i > 0 && a[i] == a[i-1] do\n i -= 1\n k += 1\n end\n\n if i > 0\n i -= 1\n while k % x == 0 && a[n-1] > a[i] do\n k = k/x\n a[n-1] -= 1\n result += 1\n end\n if a[n-1] != a[i]\n kt = 0\n end\n else\n while k % x == 0 do\n k = k/x\n a[n-1] -= 1\n result += 1\n end\n end\nend\n\nkq = x**result % 1000000007\n\nputs result"}, {"source_code": "b = gets.chomp.split(\" \").map{|i| i.to_i}\na = gets.chomp.split(\" \").map{|i| i.to_i}\nn = b[0]\nx = b[1]\n\ns = 0\n\n(0..n-1).each do |i|\n s += a[i]\nend\n\ni = n-1\nj = 1\nk = 0\n\nwhile i> 0 && ( k % i == 0 || k == 1) do\n while i > 0 && a[i-1] == a[i] do\n i -= 1\n k += 1\n end\n\n while k % x == 0 && a[n-j] > a[n-i] do\n k = k/x\n a[n-1] -= 1\n end\nend\n\nwhile k % x == 0\n k = k/x\n a[n-1] -= 1\nend\n\nresult = s - a[n-j]\n\nkq = x**result % 1000000007\n\nputs result"}, {"source_code": "b = gets.chomp.split(\" \").map{|i| i.to_i}\na = gets.chomp.split(\" \").map{|i| i.to_i}\nn = b[0]\nx = b[1]\n\ns = 0\n\n(0..n-1).each do |i|\n s += a[i]\nend\n\ni = n-1\nj = 1\nk = 1\nresult = s - a[n-1]\nkt = 1\n\nwhile kt == 1 do\n kt = 1\n while i > 0 && a[i] == a[i-1] do\n i -= 1\n k += 1\n end\n\n if i > 0\n i -= 1\n while k % x == 0 && a[n-1] > a[i] do\n k = k/x\n a[n-1] -= 1\n result += 1\n end\n if a[n-1] != a[i] || a[i] == 0\n kt = 0\n else\n k += 1\n end\n else\n while k % x == 0 do\n k = k/x\n a[n-1] -= 1\n result += 1\n end\n kt = 0\n end\nend\n\nkq = ( x**result) % 1000000007\n\nputs kq"}, {"source_code": "b = gets.chomp.split(\" \").map{|i| i.to_i}\na = gets.chomp.split(\" \").map{|i| i.to_i}\nn = b[0]\nx = b[1]\nsodu1 = []\n\ns = 0\n\n(0..n-1).each do |i|\n s += a[i]\nend\n\ni = n-1\nj = 1\nk = 1\nkt = 1\n\nwhile kt == 1 do\n while i > 0 && a[i] == a[i-1] do\n i -= 1\n k += 1\n end\n\n if i > 0\n i -= 1\n while k % x == 0 && a[n-1] > a[i] do\n k = k/x\n a[n-1] -= 1\n end\n if a[n-1] != a[i] || a[i] == 0\n kt = 0\n else\n k += 1\n end\n else\n while k % x == 0 do\n k = k/x\n a[n-1] -= 1\n end\n kt = 0\n end\nend\n\nresult = s - a[n-1]\n\ndef modular_pow base, exponent, modulus\n result1 = 1\n while exponent > 0\n if (exponent % 2 == 1)\n result1 = (result1 * base) % modulus\n end\n exponent /= 2\n base = (base * base) % modulus\n end\n return result1\nend\n\nkq = modular_pow x, result, (10**9 + 7)\n\nputs kq"}, {"source_code": "b = gets.chomp.split(\" \").map{|i| i.to_i}\na = gets.chomp.split(\" \").map{|i| i.to_i}\nn = b[0]\nx = b[1]\n\ns = 0\n\n(0..n-1).each do |i|\n s += a[i]\nend\n\ni = n-1\nj = 1\nk = 1\nkt = 1\n\nwhile kt == 1 do\n while i > 0 && a[i] == a[i-1] do\n i -= 1\n k += 1\n end\n\n if i > 0\n i -= 1\n while k % x == 0 && a[n-1] > a[i] do\n k = k/x\n a[n-1] -= 1\n end\n if a[n-1] != a[i] || a[i] == 0\n kt = 0\n else\n k += 1\n end\n else\n while k % x == 0 do\n k = k/x\n a[n-1] -= 1\n end\n kt = 0\n end\nend\n\nresult = s - a[n-1]\n\ndef modular_pow base, exponent, modulus\n result1 = 1\n while exponent > 0\n if (exponent % 2 == 1)\n result1 = (result1 * base) % modulus\n end\n exponent /= 2\n base = (base * base) % modulus\n end\n return result1\nend\n\nkq = modular_pow x, result, 1000000007\n\nputs kq"}, {"source_code": "b = gets.chomp.split(\" \").map{|i| i.to_i}\na = gets.chomp.split(\" \").map{|i| i.to_i}\nn = b[0]\nx = b[1]\n\ns = 0\n\n(0..n-1).each do |i|\n s += a[i]\nend\n\ni = n-1\nj = 1\nk = 1\nresult = s - a[n-1]\nkt = 1\n\nwhile kt == 1 do\n kt = 1\n while i > 0 && a[i] == a[i-1] do\n i -= 1\n k += 1\n end\n\n if i > 0\n i -= 1\n while k % x == 0 && a[n-1] > a[i] do\n k = k/x\n a[n-1] -= 1\n result += 1\n end\n if a[n-1] != a[i] || a[i] == 0\n kt = 0\n else\n k += 1\n end\n else\n while k % x == 0 do\n k = k/x\n a[n-1] -= 1\n result += 1\n end\n kt = 0\n end\nend\n\nkq = ( x**result) % 1000000007\n\nputs result"}, {"source_code": "b = gets.chomp.split(\" \").map{|i| i.to_i}\na = gets.chomp.split(\" \").map{|i| i.to_i}\nn = b[0]\nx = b[1]\n\ns = 0\n\n(0..n-1).each do |i|\n s += a[i]\nend\n\ni = n-1\nj = 1\nk = 1\nkt = 1\n\nwhile kt == 1 do\n kt = 1\n while i > 0 && a[i] == a[i-1] do\n i -= 1\n k += 1\n end\n\n if i > 0\n i -= 1\n while k % x == 0 && a[n-1] > a[i] do\n k = k/x\n a[n-1] -= 1\n end\n if a[n-1] != a[i] || a[i] == 0\n kt = 0\n else\n k += 1\n end\n else\n while k % x == 0 do\n k = k/x\n a[n-1] -= 1\n end\n kt = 0\n end\nend\n\nresult = s - a[n-1]\nkq = ( x**result) % 1000000007\n\nputs kq"}, {"source_code": "b = gets.chomp.split(\" \").map{|i| i.to_i}\na = gets.chomp.split(\" \").map{|i| i.to_i}\nn = b[0]\nx = b[1]\n\ns = 0\n\n(0..n-1).each do |i|\n s += a[i]\nend\n\ni = n-1\nj = 1\nk = 1\nresult = s - a[n-1]\nkt = 1\n\nwhile kt == 1 do\n kt = 1\n while i > 0 && a[i] == a[i-1] do\n i -= 1\n k += 1\n end\n\n if i > 0\n i -= 1\n while k % x == 0 && a[n-1] > a[i] do\n k = k/x\n a[n-1] -= 1\n result += 1\n end\n if a[n-1] != a[i] || a[i] == 0\n kt = 0\n else\n k += 1\n end\n else\n while k % x == 0 do\n k = k/x\n a[n-1] -= 1\n result += 1\n end\n kt = 0\n end\nend\n\nkq = ( x**result) % 1000000007\nif kq == 0 \n kq = 10**9 + 7\nend\n\nputs kq"}, {"source_code": "b = gets.chomp.split(\" \").map{|i| i.to_i}\na = gets.chomp.split(\" \").map{|i| i.to_i}\nn = b[0]\nx = b[1]\n\ns = 0\n\n(0..n-1).each do |i|\n s +=a[i]\nend\n\nresult = s - a[n-1]\n\nputs x**result"}, {"source_code": "b = gets.chomp.split(\" \").map{|i| i.to_i}\na = gets.chomp.split(\" \").map{|i| i.to_i}\nn = b[0]\nx = b[1]\nsodu1 = []\nsodu2 = []\n\ns = 0\n\n(0..n-1).each do |i|\n s += a[i]\nend\n\ni = n-1\nj = 1\nk = 1\nkt = 1\n\nwhile kt == 1 do\n kt = 1\n while i > 0 && a[i] == a[i-1] do\n i -= 1\n k += 1\n end\n\n if i > 0\n i -= 1\n while k % x == 0 && a[n-1] > a[i] do\n k = k/x\n a[n-1] -= 1\n end\n if a[n-1] != a[i] || a[i] == 0\n kt = 0\n else\n k += 1\n end\n else\n while k % x == 0 do\n k = k/x\n a[n-1] -= 1\n end\n kt = 0\n end\nend\n\nresult = s - a[n-1]\n\ntuanhoan = 0\n\nsodu = 1\n(1..1000000).each do |i|\n sodu = (sodu * x ) % 1000000007\nend\n\nif result > 1000000\n sodu1 = sodu\n thuong = result/1000000\n du = result % 1000000\n (1..thuong).each do |i|\n sodu1 = (sodu1 * sodu) % 1000000007\n end\n if du > 0\n (1..du).each do |j|\n sodu1 = ( sodu1 * x) % 1000000007\n end\n end\n sodu = sodu1\nend\n\nputs sodu"}, {"source_code": "b = gets.chomp.split(\" \").map{|i| i.to_i}\na = gets.chomp.split(\" \").map{|i| i.to_i}\nn = b[0]\nx = b[1]\nsodu = []\n\ns = 0\n\n(0..n-1).each do |i|\n s += a[i]\nend\n\ni = n-1\nj = 1\nk = 1\nkt = 1\n\nwhile kt == 1 do\n kt = 1\n while i > 0 && a[i] == a[i-1] do\n i -= 1\n k += 1\n end\n\n if i > 0\n i -= 1\n while k % x == 0 && a[n-1] > a[i] do\n k = k/x\n a[n-1] -= 1\n end\n if a[n-1] != a[i] || a[i] == 0\n kt = 0\n else\n k += 1\n end\n else\n while k % x == 0 do\n k = k/x\n a[n-1] -= 1\n end\n kt = 0\n end\nend\n\nresult = s - a[n-1]\n\ntuanhoan = 0\n\nsodu[1] = x\n(2..result).each do |i|\n sodu[i] = (sodu[i-1]*x) % 1000000007\n if sodu[i] == sodu[1]\n tuanhoan = i-1\n sodu[0] = sodu[i-1]\n break\n end\nend\n\nif tuanhoan != 0\n kq = sodu[result%tuanhoan]\nelse\n kq = sodu[result] \nend\n\nputs kq"}], "src_uid": "4867d014809bfc1d90672b32ecf43b43"} {"nl": {"description": "Vasya, or Mr. Vasily Petrov is a dean of a department in a local university. After the winter exams he got his hands on a group's gradebook.Overall the group has n students. They received marks for m subjects. Each student got a mark from 1 to 9 (inclusive) for each subject.Let's consider a student the best at some subject, if there is no student who got a higher mark for this subject. Let's consider a student successful, if there exists a subject he is the best at.Your task is to find the number of successful students in the group.", "input_spec": "The first input line contains two integers n and m (1 ≤ n, m ≤ 100) — the number of students and the number of subjects, correspondingly. Next n lines each containing m characters describe the gradebook. Each character in the gradebook is a number from 1 to 9. Note that the marks in a rows are not sepatated by spaces.", "output_spec": "Print the single number — the number of successful students in the given group.", "sample_inputs": ["3 3\n223\n232\n112", "3 5\n91728\n11828\n11111"], "sample_outputs": ["2", "3"], "notes": "NoteIn the first sample test the student number 1 is the best at subjects 1 and 3, student 2 is the best at subjects 1 and 2, but student 3 isn't the best at any subject.In the second sample test each student is the best at at least one subject."}, "positive_code": [{"source_code": "n, m = gets.split(' ').map(&:to_i)\na = []\nn.times do\n a << gets.strip.split(//).map(&:to_i)\nend\nary = Array.new(n, 0)\nb = a.transpose\nm.times do |c|\n mx = b[c].max\n n.times do |r|\n if mx == a[r][c]\n ary[r] = 1\n end\n end\nend\np ary.count(1)\n"}, {"source_code": "s = gets.split(' ')\nn = s[0].to_i\nm = s[1].to_i\na = Array.new(n).map!{ Array.new(m, 0) }\nfor r in 0..n-1\n ln = gets\n for c in 0..m-1\n a[r][c] = ln[c,1].to_i\n end\nend\nary = Array.new(n, 0)\nfor c in 0..m-1\n mx = 0\n for r in 0..n-1\n if mx < a[r][c]\n mx = a[r][c]\n end\n end\n for r in 0..n-1\n if mx == a[r][c]\n ary[r] = 1\n end\n end\nend\nres = 0\nfor r in 0..n-1\n if ary[r] > 0\n res += 1\n end\nend\np res\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\na = []\nn.times {\n a << gets.strip.split(//).map(&:to_i)\n}\nary = [0]\nb = a.transpose\nm.times { |c|\n mx = b[c].max\n n.times { |r|\n ary[r] = 1 if mx == a[r][c]\n }\n}\np ary.count(1)\n"}, {"source_code": "a=*$<\nn,m =a[0].split.map &:to_i\np (1..n).count{|x|\n (0...m).any?{|y| a[x][y] == (1..n).map{|i| a[i][y]}.max}\n}\n"}, {"source_code": "st = gets.split\nn = st[0].to_i\nm = st[1].to_i\n\nmarks = []\nsubs = Array.new(m){|i| i = -1}\nresult = 0\nreestr = Array.new(m){|i| i = []}\nn.times do |i|\n marks << gets.chomp\n m.times do |j|\n if marks[i][j].to_i > subs[j]\n subs[j] = marks[i][j].to_i\n reestr[j] = []\n reestr[j] << i\n elsif marks[i][j].to_i == subs[j] then\n reestr[j] << i\n end\n end\nend\n\nstudents = Array.new(n){|i| i = 0}\nm.times do |i|\n reestr[i].size.times do |j|\n students[reestr[i][j]] = 1\n end\nend\n\nstudents.delete(0)\n\nputs students.size"}, {"source_code": "n,m = gets.chomp.split(/ /).map!{|x| x.to_i}\na = []\ns = []\nfor i in 0..n-1\n\ts << false\n\ta << gets.chomp.split(//).map!{|x| x.to_i}\nend\n\nfor i in 0..m-1\n\tw = []\n\tfor j in 0..n-1\n\t\tw << a[j][i]\n\tend\n\twm = w.max\n\tfor j in 0..n-1\n\t\ts[j] = true if w[j] == wm\n\tend\nend\nret = 0\nfor j in 0..n-1\n\tret += 1 if s[j] == true\nend\nputs ret\n\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\na = Array.new(n){gets.chars.to_a}\nb = (['0'] * m).zip(*a).map(&:max)\np a.count{|_| _.zip(b).any?{|i, j| i == j}}\n"}, {"source_code": "n,m=gets.split.map(&:to_i)\ninput=[]\nn.times{ input.push(gets.split[0])}\nans=0\nn.times{|i|\n\tok=0\n\tm.times{|j|\n\t\tval=[]\n\t\tn.times{|k|\n\t\t\tval.push(input[k][j].to_i)\n\t\t}\n\t\tok=1 if val.max==input[i][j].to_i\n\t}\n\tans+=ok\n}\np ans"}, {"source_code": "#!/usr/bin/env ruby\n\nn, m = gets.split.map(&:to_i)\n\nhighest = Array.new(m, 0)\nlist = []\nn.times do\n arr = gets.chomp.each_char.to_a.map(&:to_i)\n arr.each_with_index do |score, idx|\n highest[idx] = [score, highest[idx]].max\n end\n list << arr\nend\n\nans = 0\nlist.each do |arr|\n arr.each_with_index do |score, idx|\n if highest[idx] == score\n ans += 1\n break\n end\n end\nend\n\np ans\n"}, {"source_code": "tmp = gets.chomp.split \" \"\nn = tmp[0].to_i\nm = tmp[1].to_i\n\nfield = []\nn.times do \n field.push gets.chomp.split(\"\").map {|i| i.to_i } \nend\n\nd = {}\nfield = field.transpose\n\nfield.each_with_index do |line, no|\n max = line.max\n \n line.each_with_index do |e, i|\n if e == max\n d[i + 1] = true\n end\n end\nend\n\nputs d.keys.length\n"}, {"source_code": "n,m = STDIN.gets.chomp.split(/ /).map(&:to_i)\n\ns = n.times.map do\n STDIN.gets.chars.map(&:to_i).take(m)\nend\n\nmax = 0.upto(m-1).map do |i|\n s.max{|a,b| a[i] <=> b[i] }[i]\nend\nf = s.map do |student|\n max.each_with_index.any? do |m,i|\n student[i] >= m\n end\nend\np f.select{|x| x }.size\n"}, {"source_code": "n,m = gets.split.map(&:to_i)\na=[]\nn.times do\n a< a[i][j])\n\t\t\t\tib = false\n\t\t\tend\n\t\tend\n\t\tif ib==true\n\t\t\twb=true\n\t\tend\n\tend\n\tif wb==true\n\t\tans+=1\n\tend\nend\nputs ans\n"}, {"source_code": "def max_index a\n max = a.max\n (0...a.size).find_all{ |x| a[x] == max}\nend\n#n, m = gets.split.map(&:to_i)\ngets\np readlines.map(&:chomp).map {|x| x.split('').map(&:to_i)}.transpose.map { |x| max_index(x) }.flatten.uniq.size"}, {"source_code": "n, m = gets.chomp.split(/ /).map(&:to_i)\nstudents = []\nn.times do\n students << gets.chomp.split(\"\").map(&:to_i)\nend\ntoppers = []\nm.times do |subject|\n topper = Array.new(n, 0)\n n.times do |student|\n marks = students[student][subject]\n topper[student] = marks if marks >= topper[student]\n end\n highest = topper.max\n topper.each_index do |student|\n marks = topper[student]\n toppers << student if marks == highest && !toppers.include?( student )\n end\nend\nputs toppers.length\n"}, {"source_code": "k=gets.chomp\na=[]\nb=[]\na=k.split(' ')\na[0].to_i.times do\n b<=max[y]\n end\nend\ncount=0\nis.each do |x|\n count+=1 if x==true\nend\nputs count"}, {"source_code": "n,m = gets.split.map(&:to_i)\na = []\n\nn.times do \n a << gets.strip.split(//).map(&:to_i)\nend\n\nb = a.transpose\n\nans = Array.new(n){ false }\nm.times do |i|\n mx = b[i].max\n b[i].each_with_index{|s,i| ans[i] = true if s == mx}\nend\n\np ans.count(true)\n"}, {"source_code": "require 'set'\nrequire 'rational'\nrequire 'mathn'\ninclude Math\n$stdin = File.open(\"./input.txt\", \"r\") if ENV[\"USER\"] == \"new\"\n# ====================================\n\nn, m = gets.split.map &:to_i\ntable = []\n\nn.times{\n table << gets.split(//).map(&:to_i)\n}\n\nstudents = Set.new\nm.times{|index|\n max_value = table.collect{|column| column[index]}.max\n \n student_id = 0\n table.each{|array|\n students << student_id if array[index] == max_value\n student_id+=1\n }\n}\n\nprint students.to_a.size"}, {"source_code": "n,m=gets.chomp.split.collect{|a| a.to_i }\nstudents=[]\nmax_scores=Array.new(m,0)\nfor i in 0..n-1\ns=gets.chomp.split('').collect{|a| a.to_i}\nstudents.push s\ns.each_index{|j| if max_scores[j] < s[j] then max_scores[j]=s[j] end}\nend\nres=0\nstudents.each do |student|\nsucc=false\nstudent.each_index{|i| if student[i]==max_scores[i] then succ=true end}\nif succ\n res+=1\nend\nend\nputs res\n\n"}, {"source_code": "input = gets.chomp\nnums = input.split\nn = Integer(nums[0])\nm = Integer(nums[1])\na = Array.new\nfor i in 0...n\n a[i] = Array.new\n input = gets.chomp\n for j in 0...m\n a[i][j] = Integer(input[j])\n end\nend\n\ndef best(a,n,j)\n b = Array.new\n c = 0\n for i in 0...n\n if a[i][j] > c\n c = a[i][j]\n end\n end\n for i in 0...n\n if a[i][j] == c\n b.push(i)\n end\n end\n return b\nend\nb = Array.new\nfor j in 0...m\n b += best(a,n,j)\nend\nb = b.sort\nc = Array.new\nc[0] = b[0]\nfor i in 0...b.length-1\n if b[i+1] != b[i]\n c.push(b[i+1])\n end\nend\nputs c.length\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\nar = Array.new(n){gets.chars.to_a}\nb = (['0'] * m).zip(*ar).map(&:max)\ncnt = 0\nn.times do |i|\n a = false\n m.times do |j|\n if ar[i][j] == b[j] then\n a = true\n end \n end\n if a == true then\n cnt = cnt + 1\n end\nend\np cnt"}, {"source_code": "n,m = gets.split(\" \").map(&:to_i)\nmax = Array.new(m,0)\narr = []\nbest = Hash.new(false)\nn.times do |s|\n\ta = gets.split(\"\").map(&:to_i)\n\tarr << a\n\t(0..m-1).each do |i|\n\t\tif a[i] >= max[i]\n\t\t\tmax[i] = a[i] \n\t\tend\n\tend\nend\n\n\narr.each_with_index do |a,i|\n\t(0..m-1).each do |j|\n\t\tbest[i] = true if a[j] == max[j]\n\tend\nend\n\nputs best.keys.count"}, {"source_code": "n, m = gets.split.map(&:to_i)\na = n.times.map{gets.chomp.chars.map(&:to_i)}\nc = a.transpose.map(&:max)\nputs a.count{|x| m.times.any?{|i| x[i] == c[i]}}\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\na = n.times.map{gets.chomp.split('').map(&:to_i)}\nc = a.transpose.map{|x| x.max}\nsuccessful = Array.new(n, false)\nn.times do |i|\n m.times do |j|\n if a[i][j] == c[j]\n successful[i] = true\n break\n end\n end\nend\nputs successful.count{|x| x == true}\n"}, {"source_code": "n,m = gets.split(\" \")\nn = n.to_i\nm = m.to_i\ng = Array.new(m) {[]}\nfor i in 0..n-1\n gr = gets.chomp\n for j in 0..m-1\n g[j] << [gr[j].to_i, i]\n\t#puts gr[j]\n end\nend\nu = Array.new(n)\nu.fill(0)\ng.each do |q|\n q.sort_by! {|e| e[0]}\n q.reverse!\n curr = 0\n u[q[0][1]] = 1\n while curr < n - 1 && q[curr][0] == q[curr + 1][0]\n \n u[q[curr+1][1]] = 1\n\tcurr += 1\n end\nend\n\nputs (u.inject(0) {|sum, curr| sum += curr})"}, {"source_code": "students, subjects = gets.split.map(&:to_i)\na = []\nstudents.times {\n a << gets.chomp.split('')\n}\nbest = a.transpose.map(&:max)\nres = 0\n(0...students).each { |i|\n (0...subjects).each { |j|\n if a[i][j] == best[j]\n res += 1\n break\n end\n }\n}\np res\n"}, {"source_code": "require 'set'\n\nn, m = gets.chomp.split.map { |x| x.to_i }\ngrades = []\nfor _ in 1..n\n grades << gets.chomp\nend\n\nsuc_students = Set.new\nfor j in 0...m\n marks_for_subject = []\n for i in 0...n\n marks_for_subject << grades[i][j]\n end\n max = marks_for_subject.max\n for i in 0...n\n if marks_for_subject[i] == max\n suc_students << i\n end\n end\nend\n\nputs suc_students.size"}, {"source_code": "n, m = gets.split.map(&:to_i)\n\ngradebook = []\nmax = Array.new(m,0)\n\nn.times do\n row = []\n row = gets.chomp.split(//).map(&:to_i)\n\n row.each_with_index do |o, i|\n if max[i] < o then max[i] = o end\n end\n\n gradebook << row\nend\n\ncounter = 0\n\ngradebook.each do |row|\n row.each_with_index do |o, i|\n if o == max[i]\n counter += 1\n break\n end\n end\nend\n\nputs counter\n"}, {"source_code": "n,m=gets.split(' ').map(&:to_i)\na=Array.new(n)\nbest = Array.new(m){0}\nbest_student = Array.new(n){0}\nn.times do |i|\n a[i] = gets.chomp.split('').map(&:to_i)\n a[i].each_index{|j| \n if(best[j] < a[i][j]) \n best[j] = a[i][j] \n end\n }\nend\ncount = 0\nn.times do|i|\n a[i].each_index{|j| \n if(a[i][j] == best[j])\n count+=1 \n break \n end\n }\nend\np count"}, {"source_code": "input = STDIN.read.split(\"\\n\")\n\nss = input.shift.split(' ').map(&:to_i)\n\nstudents, subjects = ss[0], ss[1]\n\n\nsubj_max = []\n\nsubjects.times { subj_max.push 0 }\n\nsubjects.times do |i|\n\tstudents.times do |j|\n\t\tsubj_max[i] = input[j][i].to_i if input[j][i].to_i > subj_max[i]\n\tend\nend\n\nsucc = 0\n\nstudents.times do |i|\n\tsubjects.times do |j|\n\t\tif input[i][j].to_i == subj_max[j].to_i\n\t\t\tsucc += 1\n\t\t\tbreak\n\t\tend\n\tend\nend\n\nSTDOUT.puts succ"}, {"source_code": "b=->(a){m=a.max;l=[];a.size.times{|i|a[i]==m&&l< b[i] }[i]\nend\np s.map do |student|\n max.each_with_index.any? do |m,i|\n student[i] >= m\n end\nend\n"}, {"source_code": "n,m = gets.split.map(&:to_i)\na=[]\nn.times do\n a< 0 do \n\tif n >= 9 \n\t\tn = n - 9\n\t\tdigits << 9\n\telse\n\t\tdigits << n\n\t\tbreak\n\tend\nend\nputs digits.size\ndigits.each do |i|\n\tprint \"#{i} \"\nend"}, {"source_code": "num = gets.chomp\nputs num\nnum.to_i.times do\n puts 1.class\nend\n"}, {"source_code": "num = gets.chomp\nnum.to_i.times do\n puts \"1\" + \" \"\nend\n"}, {"source_code": "num = gets.chomp\nnum.to_i.times do\n puts 1.to_int\nend\n"}, {"source_code": "num = gets.chomp\nnum.to_i.times do\n puts 1\nend\n"}, {"source_code": "n = gets.to_i\nn.times do |i|\n print 1,(i == n-1 ? \"\\n\" : \" \")\nend"}, {"source_code": "DBG = true\nn = gets.to_i\nif n <= 9\n puts \"1\"\n puts \"#{n}\"\nelsif n%9 == 0\n puts \"#{n/9}\"\n (n/9).times do\n print \"9 \"\n end\n puts \"\"\nelse\n puts \"#{(n/9)+1}\"\n (n/9).times do\n print \"9 \"\n end\n puts \"#{n%9}\"\nend\n"}, {"source_code": "DBG = true\nn = gets.to_i\nif n <= 9\n puts \"1\"\n puts \"#{n}\"\nelsif n%2 == 0\n puts \"#{n/2}\"\n (n/2).times do\n print \"2 \"\n end\n puts \"\"\nelsif n%3 == 0\n puts \"#{n/3}\"\n (n/3).times do\n print \"3 \"\n end\n puts \"\"\nelsif n%5 == 0\n puts \"#{n/5}\"\n (n/5).times do\n print \"5 \"\n end\n puts \"\"\nelsif n%7 == 0\n puts \"#{n/7}\"\n (n/7).times do\n print \"7 \"\n end\n puts \"\"\n\nelse\n puts \"#{(n/9)+1}\"\n (n/9).times do\n print \"9 \"\n end\n puts \"#{n%9}\"\nend\n"}, {"source_code": "DBG = true\nn = gets.to_i\nif n <= 9\n puts \"1\"\n puts \"#{n}\"\nelsif n%9 == 0\n puts \"1\"\n (n/9).times do\n print \"9 \"\n end\n puts \"\"\nelse\n puts \"2\"\n (n/9).times do\n print \"9 \"\n end\n puts \"#{n%9}\"\nend\n"}, {"source_code": "k = gets.to_i\nif k < 10\n puts \"1\"\n print \"#{k}\"\nelsif k % 2 == 0\n puts \"2\"\n print \"#{k/2} #{k/2}\"\nelsif k % 3 == 0\n puts \"3\"\n print \"#{k/3} #{k/3} #{k/3}\"\nelsif k % 5 == 0\n puts \"5\"\n print \"#{k/5} #{k/5} #{k/5} #{k/5} #{k/5}\"\nelsif k % 7 == 0\n puts \"7\"\n print \"#{k/7} #{k/7} #{k/7} {k/7} #{k/7} #{k/7} #{k/7}\"\nelse\n puts \"2\"\n print \"#{(k-1)/2} #{(k+1)/2}\"\nend"}, {"source_code": "n = gets.to_i\nputs (n + 8)/9\n(n/9).times { print 9, ' ' }\nprint n%9 if n%9 != 0\n"}], "src_uid": "7c483498f497f4291e3d33375c0ebd53"} {"nl": {"description": "Professor GukiZ is concerned about making his way to school, because massive piles of boxes are blocking his way. In total there are n piles of boxes, arranged in a line, from left to right, i-th pile (1 ≤ i ≤ n) containing ai boxes. Luckily, m students are willing to help GukiZ by removing all the boxes from his way. Students are working simultaneously. At time 0, all students are located left of the first pile. It takes one second for every student to move from this position to the first pile, and after that, every student must start performing sequence of two possible operations, each taking one second to complete. Possible operations are: If i ≠ n, move from pile i to pile i + 1; If pile located at the position of student is not empty, remove one box from it.GukiZ's students aren't smart at all, so they need you to tell them how to remove boxes before professor comes (he is very impatient man, and doesn't want to wait). They ask you to calculate minumum time t in seconds for which they can remove all the boxes from GukiZ's way. Note that students can be positioned in any manner after t seconds, but all the boxes must be removed.", "input_spec": "The first line contains two integers n and m (1 ≤ n, m ≤ 105), the number of piles of boxes and the number of GukiZ's students. The second line contains n integers a1, a2, ... an (0 ≤ ai ≤ 109) where ai represents the number of boxes on i-th pile. It's guaranteed that at least one pile of is non-empty.", "output_spec": "In a single line, print one number, minimum time needed to remove all the boxes in seconds.", "sample_inputs": ["2 1\n1 1", "3 2\n1 0 2", "4 100\n3 4 5 4"], "sample_outputs": ["4", "5", "5"], "notes": "NoteFirst sample: Student will first move to the first pile (1 second), then remove box from first pile (1 second), then move to the second pile (1 second) and finally remove the box from second pile (1 second).Second sample: One of optimal solutions is to send one student to remove a box from the first pile and a box from the third pile, and send another student to remove a box from the third pile. Overall, 5 seconds.Third sample: With a lot of available students, send three of them to remove boxes from the first pile, four of them to remove boxes from the second pile, five of them to remove boxes from the third pile, and four of them to remove boxes from the fourth pile. Process will be over in 5 seconds, when removing the boxes from the last pile is finished."}, "positive_code": [{"source_code": "n, m = gets.split.map &:to_i\na = gets.split.map &:to_i\nl, r = 0, 2e14.to_i\nans = r\nwhile l <= r\n mid = (l + r) >> 1\n b = a[0]\n i, j, t = 0, -1, 0\n while i < n and j < m\n if b == 0 then\n i += 1\n b = a[i]\n t -= 1\n else\n if t <= 0 then\n j += 1\n t = mid - i - 1\n else\n if t < b then\n b -= t\n t = 0\n else\n t -= b\n b = 0\n end\n end\n end\n end\n if i == n then\n r = mid - 1\n ans = [ans, mid].min\n else\n l = mid + 1\n end\nend\nputs ans\n"}], "negative_code": [], "src_uid": "ed0a8a10e03de931856e287f9e650e1a"} {"nl": {"description": "Polycarp must pay exactly $$$n$$$ burles at the checkout. He has coins of two nominal values: $$$1$$$ burle and $$$2$$$ burles. Polycarp likes both kinds of coins equally. So he doesn't want to pay with more coins of one type than with the other.Thus, Polycarp wants to minimize the difference between the count of coins of $$$1$$$ burle and $$$2$$$ burles being used. Help him by determining two non-negative integer values $$$c_1$$$ and $$$c_2$$$ which are the number of coins of $$$1$$$ burle and $$$2$$$ burles, respectively, so that the total value of that number of coins is exactly $$$n$$$ (i. e. $$$c_1 + 2 \\cdot c_2 = n$$$), and the absolute value of the difference between $$$c_1$$$ and $$$c_2$$$ is as little as possible (i. e. you must minimize $$$|c_1-c_2|$$$).", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of one line. This line contains one integer $$$n$$$ ($$$1 \\le n \\le 10^9$$$) — the number of burles to be paid by Polycarp.", "output_spec": "For each test case, output a separate line containing two integers $$$c_1$$$ and $$$c_2$$$ ($$$c_1, c_2 \\ge 0$$$) separated by a space where $$$c_1$$$ is the number of coins of $$$1$$$ burle and $$$c_2$$$ is the number of coins of $$$2$$$ burles. If there are multiple optimal solutions, print any one.", "sample_inputs": ["6\n1000\n30\n1\n32\n1000000000\n5"], "sample_outputs": ["334 333\n10 10\n1 0\n10 11\n333333334 333333333\n1 2"], "notes": "NoteThe answer for the first test case is \"334 333\". The sum of the nominal values of all coins is $$$334 \\cdot 1 + 333 \\cdot 2 = 1000$$$, whereas $$$|334 - 333| = 1$$$. One can't get the better value because if $$$|c_1 - c_2| = 0$$$, then $$$c_1 = c_2$$$ and $$$c_1 \\cdot 1 + c_1 \\cdot 2 = 1000$$$, but then the value of $$$c_1$$$ isn't an integer.The answer for the second test case is \"10 10\". The sum of the nominal values is $$$10 \\cdot 1 + 10 \\cdot 2 = 30$$$ and $$$|10 - 10| = 0$$$, whereas there's no number having an absolute value less than $$$0$$$."}, "positive_code": [{"source_code": "t = gets().to_i\r\nwhile(t!=0)\r\n n = gets().to_i\r\n if (n==1)\r\n puts \"1 0\\n\"\r\n\r\n elsif (n==2)\r\n puts \"0 1\\n\"\r\n\r\n elsif(n==3)\r\n puts \"1 1\\n\"\r\n\r\n elsif (n==4)\r\n puts \"2 1\\n\"\r\n\r\n elsif (n==5)\r\n puts \"1 2\\n\"\r\n else\r\n h1 = n/3\r\n h2 = n/3.0\r\n if (h2+0.5>=h1+1)\r\n h = h1 + 1\r\n else\r\n h = h1\r\n end\r\n\r\n l = n - 2*h\r\n puts \"#{l} #{h}\\n\"\r\n end\r\n t-=1\r\nend\r\n"}, {"source_code": "MOD = 10 ** 9 + 7\r\ndef gs; gets.chomp; end\r\ndef gi; gets.to_i; end\r\ndef gli; gets.split.map(&:to_i); end\r\ndef cyesno(x); puts x ? :Yes : :No; end\r\ndef uyesno(x); puts x ? :YES : :NO; end\r\ndef array_to_hash_by_count(c); Hash.new(0).tap { |x| c.each { |z| x[z] += 1 } }; end\r\n\r\ngets.to_i.times do\r\n x = gets.to_i\r\n k = x / 3\r\n if x%3 == 0\r\n puts \"#{k} #{k}\"\r\n elsif x%3 == 1\r\n puts \"#{k + 1} #{k}\"\r\n else\r\n puts \"#{k} #{k + 1}\"\r\n end\r\nend\r\n"}], "negative_code": [], "src_uid": "71335a9489f0985f4e16435b14d6a34a"} {"nl": {"description": "Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The vertices of the graph are numbered from 1 to n. Each edge, namely edge i, has a color ci, connecting vertex ai and bi.Mr. Kitayuta wants you to process the following q queries.In the i-th query, he gives you two integers — ui and vi.Find the number of the colors that satisfy the following condition: the edges of that color connect vertex ui and vertex vi directly or indirectly.", "input_spec": "The first line of the input contains space-separated two integers — n and m (2 ≤ n ≤ 100, 1 ≤ m ≤ 100), denoting the number of the vertices and the number of the edges, respectively. The next m lines contain space-separated three integers — ai, bi (1 ≤ ai < bi ≤ n) and ci (1 ≤ ci ≤ m). Note that there can be multiple edges between two vertices. However, there are no multiple edges of the same color between two vertices, that is, if i ≠ j, (ai, bi, ci) ≠ (aj, bj, cj). The next line contains a integer — q (1 ≤ q ≤ 100), denoting the number of the queries. Then follows q lines, containing space-separated two integers — ui and vi (1 ≤ ui, vi ≤ n). It is guaranteed that ui ≠ vi.", "output_spec": "For each query, print the answer in a separate line.", "sample_inputs": ["4 5\n1 2 1\n1 2 2\n2 3 1\n2 3 3\n2 4 3\n3\n1 2\n3 4\n1 4", "5 7\n1 5 1\n2 5 1\n3 5 1\n4 5 1\n1 2 2\n2 3 2\n3 4 2\n5\n1 5\n5 1\n2 5\n1 5\n1 4"], "sample_outputs": ["2\n1\n0", "1\n1\n1\n1\n2"], "notes": "NoteLet's consider the first sample. The figure above shows the first sample. Vertex 1 and vertex 2 are connected by color 1 and 2. Vertex 3 and vertex 4 are connected by color 3. Vertex 1 and vertex 4 are not connected by any single color. "}, "positive_code": [{"source_code": "class UF\ndef initialize;@uf=Hash.new{|_,k|k}end\ndef u a,b;@uf[f a]=@uf[f b]end\ndef f c;c==@uf[c]?c:@uf[c]=f(@uf[c])end\nend\n\ndef g;gets.split.map(&:to_i)end\n_,m=g\nuf=(1..m).map{UF.new}\nm.times{\n a,b,c=g\n uf[c-1].u(a,b)\n}\ngets.to_i.times{\n u,v=g\n p uf.count{|t|t.f(u)==t.f(v)}\n}"}, {"source_code": "class UF\ndef initialize;@uf=Hash.new{|_,k|k}end\ndef u a,b;@uf[f a]=@uf[f b]end\ndef f c;c==@uf[c]?c:@uf[c]=f(@uf[c])end\nend\n\ndef g;gets.split.map(&:to_i)end\n_,m=g\nuf=(1..m).map{UF.new}\nm.times{\n a,b,c=g\n uf[c-1].u(a,b)\n}\ngets.to_i.times{\n u,v=g\n p uf.count{|t|t.f(u)==t.f(v)}\n}"}, {"source_code": "class UF\ndef initialize;@uf=Hash.new{|_,k|k}end\ndef u a,b;@uf[f a]=@uf[f b]end\ndef f c;c==@uf[c]?c:@uf[c]=f(@uf[c])end\nend\n\ndef g;gets.split.map(&:to_i)end\n_,m=g\nuf=(1..m).map{UF.new}\nm.times{\n a,b,c=g\n uf[c-1].u(a,b)\n}\ngets.to_i.times{\n u,v=g\n p uf.count{|t|t.f(u)==t.f(v)}\n}"}], "negative_code": [], "src_uid": "e3ec343143419592e73d4be13bcf1cb5"} {"nl": {"description": "Little Petya very much likes playing with little Masha. Recently he has received a game called \"Zero-One\" as a gift from his mother. Petya immediately offered Masha to play the game with him.Before the very beginning of the game several cards are lain out on a table in one line from the left to the right. Each card contains a digit: 0 or 1. Players move in turns and Masha moves first. During each move a player should remove a card from the table and shift all other cards so as to close the gap left by the removed card. For example, if before somebody's move the cards on the table formed a sequence 01010101, then after the fourth card is removed (the cards are numbered starting from 1), the sequence will look like that: 0100101. The game ends when exactly two cards are left on the table. The digits on these cards determine the number in binary notation: the most significant bit is located to the left. Masha's aim is to minimize the number and Petya's aim is to maximize it.An unpleasant accident occurred before the game started. The kids spilled juice on some of the cards and the digits on the cards got blurred. Each one of the spoiled cards could have either 0 or 1 written on it. Consider all possible variants of initial arrangement of the digits (before the juice spilling). For each variant, let's find which two cards are left by the end of the game, assuming that both Petya and Masha play optimally. An ordered pair of digits written on those two cards is called an outcome. Your task is to find the set of outcomes for all variants of initial digits arrangement.", "input_spec": "The first line contains a sequence of characters each of which can either be a \"0\", a \"1\" or a \"?\". This sequence determines the initial arrangement of cards on the table from the left to the right. The characters \"?\" mean that the given card was spoiled before the game. The sequence's length ranges from 2 to 105, inclusive.", "output_spec": "Print the set of outcomes for all possible initial digits arrangements. Print each possible outcome on a single line. Each outcome should be represented by two characters: the digits written on the cards that were left by the end of the game. The outcomes should be sorted lexicographically in ascending order (see the first sample).", "sample_inputs": ["????", "1010", "1?1"], "sample_outputs": ["00\n01\n10\n11", "10", "01\n11"], "notes": "NoteIn the first sample all 16 variants of numbers arrangement are possible. For the variant 0000 the outcome is 00. For the variant 1111 the outcome is 11. For the variant 0011 the outcome is 01. For the variant 1100 the outcome is 10. Regardless of outcomes for all other variants the set which we are looking for will contain all 4 possible outcomes.In the third sample only 2 variants of numbers arrangement are possible: 111 and 101. For the variant 111 the outcome is 11. For the variant 101 the outcome is 01, because on the first turn Masha can remove the first card from the left after which the game will end."}, "positive_code": [{"source_code": "a=gets.split[0]\nzero=0\none=0\nhatena=0\na.each_char{|i|\n if i==\"1\" then\n one+=1\n elsif i==\"0\" then\n zero+=1\n elsif i==\"?\" then\n hatena+=1\n end\n}\nminus=zero+hatena+one-2\nans=[0,0,0,0]\nres=[\"00\",\"01\",\"10\",\"11\"]\nfor i in 0..hatena\n z=zero+i\n o=one+hatena-i\n if minus%2==1 then z+=1 end\n if z>o then\n ans[0]=1\n elsif z0 then ans[1]=1 end\n if i>0 then ans[2]=1 end\n elsif a[minus+1]==\"1\" then\n ans[1]=1\n else\n ans[2]=1\n end\n end\nend\n4.times{|i|\n if ans[i]>0 then\n puts res[i]\n end\n}\n"}, {"source_code": "def cal(str,ans)\n map={'0'=>0,'1'=>1,'?'=>2};\n if str[0].chr=='?' then\n str[0]='1'; cal(str,ans);\n str[0]='0'; cal(str,ans);\n return;\n end\n cnt=[0,0-str.length%2,0];\n str.each_byte{ |c| cnt[map[c.chr]]=cnt[map[c.chr]]+1; }\n ans.push(\"00\") if str.length/2o then\n ans[0]=1\n elsif z0 then\n puts res[i]\n end\n}\n"}, {"source_code": "a=gets.split[0]\nzero=0\none=0\nhatena=0\na.each_char{|i|\n if i==\"1\" then\n one+=1\n elsif i==\"0\" then\n zero+=1\n elsif i==\"?\" then\n hatena+=1\n end\n}\nminus=zero+hatena+one-2\nans=[0,0,0,0]\nres=[\"00\",\"01\",\"10\",\"11\"]\nfor i in 0..hatena\n z=zero+i\n o=one+hatena-i\n if minus%2==1 then z+=1 end\n if z>o then\n ans[0]=1\n elsif z0 then ans[1]=1 end\n if hatena>0 then ans[2]=1 end\n elsif a[minus+1]==\"1\" then\n ans[1]=1\n else\n ans[2]=1\n end\n end\nend\n4.times{|i|\n if ans[i]>0 then\n puts res[i]\n end\n}\n"}, {"source_code": "def cal(str,ans)\n map={'0'=>0,'1'=>1,'?'=>2};\n if str[0].chr=='?' then\n str[0]='1'; cal(str,ans);\n str[0]='0'; cal(str,ans);\n return;\n end\n cnt=[0-str.length%2,0,0];\n str.each_byte{ |c| cnt[map[c.chr]]=cnt[map[c.chr]]+1; }\n ans.push(\"00\") if str.length/20,'1'=>1,'?'=>2};\n if str[0].chr=='?' then\n str[0]='1'; cal(str,ans);\n str[0]='0'; cal(str,ans);\n return;\n end\n cnt=[0-str.length%2,0,0];\n str.each_byte{ |c| cnt[map[c.chr]]=cnt[map[c.chr]]+1; }\n ans.push(\"00\") if str.length/20,'1'=>1,'?'=>2};\n if str[0].chr=='?' then\n str[0]='1'; cal(str,ans);\n str[0]='0'; cal(str,ans);\n return;\n end\n cnt=[0-str.length%2,0,0];\n str.each_byte{ |c| cnt[map[c.chr]]=cnt[map[c.chr]]+1; }\n ans.push(\"00\") if str.length/2 b.len }\nret = \"YES\"\nprev = 0\ncnt = 0\ncntmax = 0\n\nfor i in 0..n-1\n\tif cntmax > 0 && cnt >= cntmax-1\n\t\tret = \"NO\"\n\t\tbreak\n\tend\n\t\n\tif prev == ar[i].len\n\t\tcnt += 1\n\telse\n\t\tdiff = ar[i].len - prev\n\t\tif prev > 0\n\t\t\tcnt += 1\n\t\t\tcnt *= 2**diff \n\t\telse\n\t\t\tcnt = 0\n\t\tend\n\t\tcntmax = 2**ar[i].len\n\tend\n\tprev = ar[i].len\n\tar[i].str = sprintf(\"%.\" + prev.to_s + \"b\", cnt)\nend\nar.sort!{|a,b| a.idx <=> b.idx }\nputs ret\nif ret == \"YES\"\n\tar.each{|x| puts x.str }\nend"}], "negative_code": [{"source_code": "n = gets.chomp.to_i\na = gets.chomp.split(/ /).map!{|x|x.to_i}\na.sort!\nret = \"YES\"\nprev = 0\ncnt = 0\ndiff = 0\ncntmax = 0\nra = Array.new\nfor i in 0..n-1\n\tif cntmax > 0 && cnt >= cntmax-1\n\t\tret = \"NO\"\n\t\tbreak\n\tend\n\t\n\tif prev == a[i]\n\t\tcnt += 1\n\telse\n\t\tdiff = a[i] - prev\n\t\tif prev > 0\n\t\t\tcnt += 1\n\t\t\tcnt *= 2**diff \n\t\telse\n\t\t\tcnt = 0\n\t\tend\n\t\tcntmax = 2**a[i]\n\tend\n\tprev = a[i]\n\tra.push(sprintf(\"%.\" + prev.to_s + \"b\", cnt))\nend\nputs ret\nif ret == \"YES\"\n\tra.each{|x| puts x }\nend"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split(/ /).map!{|x|x.to_i}\na.sort!\nret = \"YES\"\nprev = 0\ncnt = 0\ndiff = 0\ncntmax = 0\nra = Array.new\nfor i in 0..n-1\n\tif cntmax > 0 && cnt >= cntmax-1\n\t\tret = \"NO\"\n\t\tbreak\n\tend\n\t\n\tif prev == a[i]\n\t\tcnt += 1\n\telse\n\t\tdiff = a[i] - prev\n\t\tif prev > 0\n\t\t\tcnt += 1\n\t\t\tcnt *= 2**diff \n\t\telse\n\t\t\tcnt = 0\n\t\tend\n\t\tcntmax = 2**a[i]\n\tend\n\tprev = a[i]\n\tra.push(cnt.to_s(2))\nend\nputs ret\nif ret == \"YES\"\n\tra.each{|x| puts x }\nend"}], "src_uid": "1670a3d7dba83e29e98f0ac6fe4acb18"} {"nl": {"description": "A and B are preparing themselves for programming contests.B loves to debug his code. But before he runs the solution and starts debugging, he has to first compile the code.Initially, the compiler displayed n compilation errors, each of them is represented as a positive integer. After some effort, B managed to fix some mistake and then another one mistake.However, despite the fact that B is sure that he corrected the two errors, he can not understand exactly what compilation errors disappeared — the compiler of the language which B uses shows errors in the new order every time! B is sure that unlike many other programming languages, compilation errors for his programming language do not depend on each other, that is, if you correct one error, the set of other error does not change.Can you help B find out exactly what two errors he corrected?", "input_spec": "The first line of the input contains integer n (3 ≤ n ≤ 105) — the initial number of compilation errors. The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the errors the compiler displayed for the first time. The third line contains n - 1 space-separated integers b1, b2, ..., bn - 1 — the errors displayed at the second compilation. It is guaranteed that the sequence in the third line contains all numbers of the second string except for exactly one. The fourth line contains n - 2 space-separated integers с1, с2, ..., сn - 2 — the errors displayed at the third compilation. It is guaranteed that the sequence in the fourth line contains all numbers of the third line except for exactly one. ", "output_spec": "Print two numbers on a single line: the numbers of the compilation errors that disappeared after B made the first and the second correction, respectively. ", "sample_inputs": ["5\n1 5 8 123 7\n123 7 5 1\n5 1 7", "6\n1 4 3 3 5 7\n3 7 5 4 3\n4 3 7 5"], "sample_outputs": ["8\n123", "1\n3"], "notes": "NoteIn the first test sample B first corrects the error number 8, then the error number 123.In the second test sample B first corrects the error number 1, then the error number 3. Note that if there are multiple errors with the same number, B can correct only one of them in one step. "}, "positive_code": [{"source_code": "#!/usr/bin/env ruby\ndef f(a,b)\n\ta[b.size.times.find{|i|\n\t\ta[i] [\"3\", \"7\", \"5\", \"4\", \"3\"], [\"4\", \"3\", \"7\", \"5\"]\n memo[v] ||= 0; memo[v] += 1; memo # => {\"3\"=>1}, {\"3\"=>1, \"7\"=>1}, {\"3\"=>1, \"7\"=>1, \"5\"=>1}, {\"3\"=>1, \"7\"=>1, \"5\"=>1, \"4\"=>1}, {\"3\"=>2, \"7\"=>1, \"5\"=>1, \"4\"=>1}, {\"4\"=>1}, {\"4\"=>1, \"3\"=>1}, {\"4\"=>1, \"3\"=>1, \"7\"=>1}, {\"4\"=>1, \"3\"=>1, \"7\"=>1, \"5\"=>1}\n } # => {\"3\"=>2, \"7\"=>1, \"5\"=>1, \"4\"=>1}, {\"4\"=>1, \"3\"=>1, \"7\"=>1, \"5\"=>1}\n reject { |e| h.include?(e) && (h[e] -= 1) >= 0 } # => [\"1\"], [\"3\"]\n end # => :subtract_once\nend # => :subtract_once\n\ntotal = gets.chomp # ~> NoMethodError: undefined method `chomp' for nil:NilClass\n\nsatu = gets.chomp.split(' ')\ndua = gets.chomp.split(' ')\ntiga = gets.chomp.split(' ')\n# satu = %w(1 4 3 3 5 7) # => [\"1\", \"4\", \"3\", \"3\", \"5\", \"7\"]\n# dua = %w(3 7 5 4 3) # => [\"3\", \"7\", \"5\", \"4\", \"3\"]\n# tiga = %w(4 3 7 5) # => [\"4\", \"3\", \"7\", \"5\"]\n\n\nputs satu.subtract_once(dua)[0] # => nil\nputs dua.subtract_once(tiga)[0] # => nil\n\n# >> 1\n# >> 3\n"}, {"source_code": "class Array\n def subtract_once(values)\n counts = values.inject(Hash.new(0)) { |h, v| h[v] += 1; h }\n reject { |e| counts[e] -= 1 unless counts[e].zero? }\n end\nend\n\nn = gets.chomp.to_i\n\nset1 = gets.chomp.split(' ').map { |e| e.to_i }.sort\nset2 = gets.chomp.split(' ').map { |e| e.to_i }.sort\nset3 = gets.chomp.split(' ').map { |e| e.to_i }.sort\n\np set1.subtract_once(set2)[0]\np set2.subtract_once(set3)[0]"}, {"source_code": "n = Integer(gets)\nget_first = gets.chomp.split\nn_first = Array.new\nfor i in 0...get_first.length\n n_first[i] = Integer(get_first[i])\nend\nget_second = gets.chomp.split\nn_second = Array.new\nfor i in 0...get_second.length\n n_second[i] = Integer(get_second[i])\nend\nget_third = gets.chomp.split\nn_third = Array.new\nfor i in 0...get_third.length\n n_third[i] = Integer(get_third[i])\nend\n\nn_first = n_first.sort\nn_second = n_second.sort\nn_third = n_third.sort\nfirst = 0\nsecond = 0\nfor i in 0...n_second.length\n if n_first[i] != n_second[i]\n first = n_first[i]\n break\n else\n first = n_first[n_second.length]\n end\nend\nfor i in 0...n_third.length\n if n_second[i] != n_third[i]\n second = n_second[i]\n break\n else\n second = n_second[n_third.length]\n end\nend\n\nputs first\nputs second\n"}, {"source_code": "#Made By Hasbi Hamza\ngets.chomp\na = gets.chomp.split(' ').map(&:to_i).inject(:+);\nb = gets.chomp.split(' ').map(&:to_i).inject(:+);\nc = gets.chomp.split(' ').map(&:to_i).inject(:+);\nputs a-b\nputs b-c"}, {"source_code": "_,a,b,c=$<.map{|l|l.split.map(&:to_i).inject:+}\np a-b,b-c\n"}, {"source_code": "n = gets.to_i\na = []\nb =[]\nc = []\n\na = gets.split.map(&:to_i)\nb = gets.split.map(&:to_i)\nc = gets.split.map(&:to_i)\n\ndef sum(n)\n return n.inject(0){|sum, i| sum+i}\nend\n\nputs sum(a) - sum(b)\nputs sum(b) - sum(c)"}, {"source_code": "_,a,b,c=$<.map{|l|l.split.map(&:to_i).inject:+}\np a-b,b-c\n"}, {"source_code": "def check(a, b)\n a.zip(b).find { |x| x.first != x.last }.first\nend\n\ngets\na = gets.split(' ').map(&:to_i).sort\nb = gets.split(' ').map(&:to_i).sort\nc = gets.split(' ').map(&:to_i).sort\n\nputs check(a, b)\nputs check(b, c)\n"}, {"source_code": "def check(a, b)\n a.zip(b).each do |x|\n if x.first != x.last\n puts x.first\n break\n end\n end\nend\n\ngets\na = gets.split(' ').map(&:to_i).sort\nb = gets.split(' ').map(&:to_i).sort\nc = gets.split(' ').map(&:to_i).sort\n\ncheck a, b\ncheck b, c\n"}, {"source_code": "# 519B\n\ndef read_line\n (gets.chomp.split.map &:to_i).sort\nend\n\nn = gets.to_i\ns = read_line\nt = read_line\np = ->(i) { s[i] != t[i] }\nputs s[(0...n - 1).find(&p) || n - 1]\ns = read_line\nputs t[(0...n - 2).find(&p) || n - 2]"}, {"source_code": "_,a,b,c=$<.map{|l|l.split.map(&:to_i).inject:+}\np a-b,b-c\n"}, {"source_code": "_,a,b,c=$<.map{|l|l.split.map(&:to_i).inject:+}\np a-b,b-c\n"}], "negative_code": [{"source_code": "gets.to_i\na=gets.split.map{|x| x.to_i}.sort!\nb=gets.split.map{|x| x.to_i}.sort!\nc=gets.split.map{|x| x.to_i}.sort!\n\ndef otlich(a, b)\n for i in 0...a.max\n b< preserved #{collected.length}/#{ec.length} #{c}\"\n cands = [C.new(c.i+1, c.j), C.new(c.i-1, c.j), C.new(c.i, c.j+1), C.new(c.i, c.j-1)]\n for cand in cands\n if cand.i >= 0 && cand.i < n && cand.j >= 0 && cand.j < m\n if g[cand.i][cand.j] == EMPTY\n q << cand\n end\n end\n end\nend\n\n\nputs g.map(&:join).join(\"\\n\").gsub(EMPTY, ADDED_WALL).gsub(PRESERVED, EMPTY)\n#puts g.map(&:join).join(\"\").count(EMPTY)"}, {"source_code": "\n# Is the curren node valid?\ndef valid(node, maze)\n return false if node.min < 0\n return false if node[0] >= maze.length\n return false if node[1] >= maze[0].length\n return false if maze[node[0]][node[1]] != \"X\"\n return true\nend\n\n# Nodes connected to node\ndef children(node, maze)\n children = []\n offsets = [[-1,0],[1,0],[0,-1],[0,1]]\n for offset in offsets\n nebr = [node[0] + offset[0] ,node[1] + offset[1]]\n children.push(nebr) if valid(nebr, maze) \n end\n return children\nend\n\n# Run BFS\ndef bfs(root, maze, depth)\n queue = Array.new(depth)\n queue[0] = root\n visited = Array.new(maze.length) { Array.new(maze[0].length, false) }\n front = 0; back = 1;\n while true\n root = queue[front]\n front = front + 1\n maze[root[0]][root[1]] = \".\"\n return if front == depth\n for child in children(root, maze) do\n unless visited[child[0]][child[1]]\n queue[back] = child\n back = back + 1\n visited[child[0]][child[1]] = true\n end\n end\n end\nend\n\n# Print maze\ndef print_maze(maze)\n puts maze.map { |row| row.join(\"\") }\nend\n\n# Initialize variables\nmaze = []; root = [0]; depth = 0; c = 0;\n\n# Read input\nn, m, k = gets.strip.split.map{|s| s.to_i}\nn.times {maze.push(gets.strip.split(\"\"))}\n\n# Convert all . to x\nfor i in 0..(n - 1) do\n for j in 0..(m - 1) do\n if maze[i][j] == \".\"\n maze[i][j] = \"X\"\n c = c + 1\n root = [i,j]\n end\n end\nend \n\n# Size of required clearing\ndepth = c - k\n\n# Solve maze\nbfs(root, maze, depth) \n\n# Print maze\nprint_maze(maze)\n"}, {"source_code": "\n@di = [-1, 0, 1, 0]\n@dj = @di.reverse\n\ndef ok(i, j)\n return false if i < 0 || i >= @n || j < 0 || j >= @m || @maze[i][j] != '.'\n return true\nend\n\ndef bfs(si, sj)\n qi = 0\n qj = 0\n q = Array.new(@n * @m)\n q[qj] = [si, sj]\n qj += 1\n while qi < qj\n arr = q[qi]\n qi += 1\n i = arr[0]\n j = arr[1]\n next if @vis[i][j]\n @vis[i][j] = true\n @reached += 1\n break if @reached == @target\n 4.times { |d|\n ni = i + @di[d]\n nj = j + @dj[d]\n if ok(ni, nj)\n q[qj] = [ni, nj]\n qj += 1\n end\n }\n end\nend\n\n@n, @m, k = gets.strip.split.map(&:to_i)\n@maze = Array.new(@n)\n@vis = Array.new(@n) {Array.new(@m) { false }}\ns = 0\n@n.times {|i|\n @maze[i] = gets.strip.split(\"\")\n s += @maze[i].select{|c| c == '.'}.count\n}\n@reached = 0\n@target = s - k\n@n.times {|i|\n done = false\n @m.times {|j|\n # puts \" > #{i}, #{j}, #{@maze[i][j].class}\"\n if @maze[i][j] == '.'\n done = true\n bfs(i, j)\n break\n end\n }\n break if done\n}\n\n# puts @maze.map{|s| s.join(\"\")}.join(\"\\n\")\n\n@n.times {|i|\n @m.times {|j|\n if @maze[i][j] == '.' && !@vis[i][j]\n @maze[i][j] = 'X'\n end\n }\n}\n@maze.map!{|r| r.join(\"\")}\nout = @maze.join(\"\\n\")\n$stdout.print out\n"}], "negative_code": [{"source_code": "# coding: utf-8\nEMPTY = '.'\nWALL = '#'\nADDED_WALL = 'X'\nPRESERVED = 'P'\nn, m, k = gets.split.map(&:to_i)\nC = Struct.new(:i, :j)\ng = []\nec = [] # empty cells coordinates\nn.times { |i|\n r = gets.chomp.split(//)\n for j in 0...m\n if r[j] == EMPTY\n ec << C.new(i, j)\n end\n end\n g << r\n}\n\n# k 개 블럭을 이용해 새로운 벽을 놓습니다\n# 윗 줄이 완전히 막혀도 상관없지만,\n# 블럭을 설치한 후에도 길은 모두 이어져 있어야합니다.\n# 즉, 다음과 같이 섬이 만들어져서는 안됩니다.\n# n = 4, m = 4\n# ####\n# ##..\n# #.#.\n# ###.\n# 1. 전체 empty cell 을 카운트 합니다. \n# 2. empty cell 을 DFS/BFS 로 순회하여, 1의 수치에서 k개 점을 제외한만큼 확보합니다.\n# 3. 여전히 남아있는 셀들을 벽을 설치할 셀로 마크합니다.\n# 불행히도 처음에 생각한 이웃한 empty cell이 적은걸 마킹하는 방법은 실패합니다.\n\ngoal = ec.length - k\ncollected = []\nq = [ec.first]\nwhile collected.length < goal\n c = q.shift\n collected << c\n # p collected\n g[c.i][c.j] = PRESERVED\n cands = [C.new(c.i+1, c.j), C.new(c.i, c.j+1), C.new(c.i-1, c.j), C.new(c.i, c.j-1)]\n for cand in cands\n if cand.i >= 0 && cand.i < n && cand.j >= 0 && cand.j < m\n if g[cand.i][cand.j] == EMPTY\n q << cand\n end\n end\n end\nend\n\n\nputs g.map(&:join).join(\"\\n\").gsub(EMPTY, ADDED_WALL).gsub(PRESERVED, EMPTY)"}, {"source_code": "\n# Is the curren node valid?\ndef valid(node, maze)\n return false if node.min < 0\n return false if node[0] >= maze.length\n return false if node[1] >= maze[0].length\n return false if maze[node[0]][node[1]] != \"X\"\n return true\nend\n\n# Nodes connected to node\ndef children(node, maze)\n children = []\n offsets = [[-1,0],[1,0],[0,-1],[0,1]]\n for offset in offsets\n nebr = [node[0] + offset[0] ,node[1] + offset[1]]\n children.push(nebr) if valid(nebr, maze) \n end\n return children\nend\n\n# Run BFS\ndef bfs(root, maze, depth)\n queue = []; at = 0;\n queue.push(root)\n while (queue.size != 0)\n root = queue.shift\n maze[root[0]][root[1]] = \".\"\n at = at + 1\n return if at == depth\n for child in children(root, maze) do\n queue.push(child)\n end\n end\nend\n\n# Run DFS\ndef dfs(root, traversal, maze, depth)\n maze[root[0]][root[1]] = \".\"\n traversal.push(root)\n for child in children(root, maze) do\n if traversal.length < depth\n dfs(child, traversal, maze, depth)\n end\n end\nend\n\n# Initialize variables\nmaze = []; root = [0]; depth = 0; traversal = []; c = 0;\n\n# Read input\nn, m, k = gets.strip.split.map{|s| s.to_i}\nn.times {maze.push(gets.strip.split(\"\"))}\n\n# Convert all . to x\nfor i in 0..(n - 1) do\n for j in 0..(m - 1) do\n if maze[i][j] == \".\"\n maze[i][j] = \"X\"\n c = c + 1\n root = [i,j]\n end\n end\nend \n\n# Size of required clearing\ndepth = c - k\n\n# Solve maze\nbfs(root, maze, depth) \n# dfs(root, traversal, maze, depth)\n\n# Print maze\nfor i in 0..(n - 1) do\n puts maze[i].join(\"\")\nend\n"}, {"source_code": "\n# Is the curren node valid?\ndef valid(node, maze)\n return false if node.min < 0\n return false if node[0] >= maze.length\n return false if node[1] >= maze[0].length\n return false if maze[node[0]][node[1]] != \"x\"\n return true\nend\n\n# Nodes connected to node\ndef children(node, maze)\n children = []\n offsets = [[-1,0],[1,0],[0,-1],[0,1]]\n for offset in offsets\n nebr = [node[0] + offset[0] ,node[1] + offset[1]]\n children.push(nebr) if valid(nebr, maze) \n end\n return children\nend\n\n# Run DFS\ndef dfs(root, traversal, maze, depth)\n maze[root[0]][root[1]] = \".\"\n traversal.push(root)\n for child in children(root, maze) do\n if traversal.length < depth\n dfs(child, traversal, maze, depth)\n end\n end\nend\n\n# Initialize variables\nmaze = []; root = [0]; depth = 0; traversal = []; c = 0;\n\n# Read input\nn, m, k = gets.strip.split.map{|s| s.to_i}\nn.times {maze.push(gets.strip.split(\"\"))}\n\n# Convert all . to x\nfor i in 0..(n - 1) do\n for j in 0..(m - 1) do\n if maze[i][j] == \".\"\n maze[i][j] = \"X\"\n c = c + 1\n root = [i,j]\n end\n end\nend \n\n# Size of required clearing\ndepth = c - k\n\n# Solve maze\ndfs(root, traversal, maze, depth)\n\n# Print maze\nfor i in 0..(n - 1) do\n puts maze[i].join(\"\")\nend\n"}, {"source_code": "\n# Is the curren node valid?\ndef valid(node, maze)\n return false if node.min < 0\n return false if node[0] >= maze.length\n return false if node[1] >= maze[0].length\n return false if maze[node[0]][node[1]] != \"X\"\n return true\nend\n\n# Nodes connected to node\ndef children(node, maze)\n children = []\n offsets = [[-1,0],[1,0],[0,-1],[0,1]]\n for offset in offsets\n nebr = [node[0] + offset[0] ,node[1] + offset[1]]\n children.push(nebr) if valid(nebr, maze) \n end\n return children\nend\n\n# Run BFS\ndef bfs(root, maze, depth)\n queue = []; at = 0;\n queue.push(root)\n while (queue.size != 0)\n root = queue.shift\n maze[root[0]][root[1]] = \".\"\n at = at + 1\n \n puts\n puts at\n print root\n puts\n print_maze(maze)\n\n return if at == depth\n for child in children(root, maze) do\n queue.push(child) unless queue.include?(child)\n end\n end\nend\n\n# Run DFS\ndef dfs(root, traversal, maze, depth)\n maze[root[0]][root[1]] = \".\"\n traversal.push(root)\n for child in children(root, maze) do\n if traversal.length < depth\n dfs(child, traversal, maze, depth)\n end\n end\nend\n\ndef print_maze(maze)\n for i in 0..(maze.length - 1) do\n puts maze[i].join(\"\")\n end\nend\n\n# Initialize variables\nmaze = []; root = [0]; depth = 0; traversal = []; c = 0;\n\n# Read input\nn, m, k = gets.strip.split.map{|s| s.to_i}\nn.times {maze.push(gets.strip.split(\"\"))}\n\n# Convert all . to x\nfor i in 0..(n - 1) do\n for j in 0..(m - 1) do\n if maze[i][j] == \".\"\n maze[i][j] = \"X\"\n c = c + 1\n root = [i,j]\n end\n end\nend \n\n# Size of required clearing\ndepth = c - k\n\n# Solve maze\nbfs(root, maze, depth) \n# dfs(root, traversal, maze, depth)\n\nputs\nputs depth\nputs\n\n# Print maze\nprint_maze(maze)\n"}, {"source_code": "\n# Is the curren node valid?\ndef valid(node, maze)\n return false if node.min < 0\n return false if node[0] >= maze.length\n return false if node[1] >= maze[0].length\n return false if maze[node[0]][node[1]] != \"x\"\n return true\nend\n\n# Nodes connected to node\ndef children(node, maze)\n children = []\n offsets = [[-1,0],[1,0],[0,-1],[0,1]]\n for offset in offsets\n nebr = [node[0] + offset[0] ,node[1] + offset[1]]\n children.push(nebr) if valid(nebr, maze) \n end\n return children\nend\n\n# Run DFS\ndef dfs(root, traversal, maze, depth)\n maze[root[0]][root[1]] = \".\"\n traversal.push(root)\n for child in children(root, maze) do\n if traversal.length < depth\n dfs(child, traversal, maze, depth)\n end\n end\nend\n\n# Initialize variables\nmaze = []; root = [0]; depth = 0; traversal = []; c = 0;\n\n# Read input\nn, m, k = gets.strip.split.map{|s| s.to_i}\nn.times {maze.push(gets.strip.split(\"\"))}\n\n# Convert all . to x\nfor i in 0..(n - 1) do\n for j in 0..(m - 1) do\n if maze[i][j] == \".\"\n maze[i][j] = \"x\"\n c = c + 1\n root = [i,j]\n end\n end\nend \n\n# Size of required clearing\ndepth = c - k\n\n# Solve maze\ndfs(root, traversal, maze, depth)\n\n# Print maze\nfor i in 0..(n - 1) do\n puts maze[i].join(\"\")\nend\n"}, {"source_code": "\n# Is the curren node valid?\ndef valid(node, maze)\n return false if node.min < 0\n return false if node[0] >= maze.length\n return false if node[1] >= maze[0].length\n return false if maze[node[0]][node[1]] != \"X\"\n return true\nend\n\n# Nodes connected to node\ndef children(node, maze)\n children = []\n offsets = [[-1,0],[1,0],[0,-1],[0,1]]\n for offset in offsets\n nebr = [node[0] + offset[0] ,node[1] + offset[1]]\n children.push(nebr) if valid(nebr, maze) \n end\n return children\nend\n\n# Run BFS\ndef bfs(root, maze, depth)\n queue = Array.new(depth)\n queue[0] = root\n visited = Array.new(maze.length) { Array.new(maze[0].length, false) }\n front = 0; back = 1;\n while true\n root = queue[front]\n front = front + 1\n maze[root[0]][root[1]] = \".\"\n return if front == depth\n for child in children(root, maze) do\n unless visited[child[0]][child[1]]\n queue[back] = child\n back = back + 1\n end\n end\n end\nend\n\n# Print maze\ndef print_maze(maze)\n puts maze.map { |row| row.join(\"\") }\nend\n\n# Initialize variables\nmaze = []; root = [0]; depth = 0; c = 0;\n\n# Read input\nn, m, k = gets.strip.split.map{|s| s.to_i}\nn.times {maze.push(gets.strip.split(\"\"))}\n\n# Convert all . to x\nfor i in 0..(n - 1) do\n for j in 0..(m - 1) do\n if maze[i][j] == \".\"\n maze[i][j] = \"X\"\n c = c + 1\n root = [i,j]\n end\n end\nend \n\n# Size of required clearing\ndepth = c - k\n\n# Solve maze\nbfs(root, maze, depth) \n\n# Print maze\nprint_maze(maze)\n"}], "src_uid": "3aba4a129e24ca2f06f5f3ef13415b8b"} {"nl": {"description": "Andrew has $$$n$$$ piles with stones. The $$$i$$$-th pile contains $$$a_i$$$ stones. He wants to make his table clean so he decided to put every stone either to the $$$1$$$-st or the $$$n$$$-th pile.Andrew can perform the following operation any number of times: choose $$$3$$$ indices $$$1 \\le i < j < k \\le n$$$, such that the $$$j$$$-th pile contains at least $$$2$$$ stones, then he takes $$$2$$$ stones from the pile $$$j$$$ and puts one stone into pile $$$i$$$ and one stone into pile $$$k$$$. Tell Andrew what is the minimum number of operations needed to move all the stones to piles $$$1$$$ and $$$n$$$, or determine if it's impossible.", "input_spec": "The input contains several test cases. The first line contains one integer $$$t$$$ ($$$1 \\leq t \\leq 10\\,000$$$) — the number of test cases. The first line for each test case contains one integer $$$n$$$ ($$$3 \\leq n \\leq 10^5$$$) — the length of the array. The second line contains a sequence of integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\leq a_i \\leq 10^9$$$) — the array elements. It is guaranteed that the sum of the values $$$n$$$ over all test cases does not exceed $$$10^5$$$.", "output_spec": "For each test case print the minimum number of operations needed to move stones to piles $$$1$$$ and $$$n$$$, or print $$$-1$$$ if it's impossible.", "sample_inputs": ["4\n5\n1 2 2 3 6\n3\n1 3 1\n3\n1 2 1\n4\n3 1 1 2"], "sample_outputs": ["4\n-1\n1\n-1"], "notes": "NoteIn the first test case, it is optimal to do the following: Select $$$(i, j, k) = (1, 2, 5)$$$. The array becomes equal to $$$[2, 0, 2, 3, 7]$$$. Select $$$(i, j, k) = (1, 3, 4)$$$. The array becomes equal to $$$[3, 0, 0, 4, 7]$$$. Twice select $$$(i, j, k) = (1, 4, 5)$$$. The array becomes equal to $$$[5, 0, 0, 0, 9]$$$. This array satisfy the statement, because every stone is moved to piles $$$1$$$ and $$$5$$$. There are $$$4$$$ operations in total.In the second test case, it's impossible to put all stones into piles with numbers $$$1$$$ and $$$3$$$: At the beginning there's only one possible operation with $$$(i, j, k) = (1, 2, 3)$$$. The array becomes equal to $$$[2, 1, 2]$$$. Now there is no possible operation and the array doesn't satisfy the statement, so the answer is $$$-1$$$. In the third test case, it's optimal to do the following: Select $$$(i, j, k) = (1, 2, 3)$$$. The array becomes equal to $$$[2, 0, 2]$$$. This array satisfies the statement, because every stone is moved to piles $$$1$$$ and $$$3$$$. The is $$$1$$$ operation in total.In the fourth test case, it's impossible to do any operation, and the array doesn't satisfy the statement, so the answer is $$$-1$$$."}, "positive_code": [{"source_code": "def stone(arr)\r\n if arr.length == 3 and arr[1]%2!=0\r\n return -1\r\n elsif arr[1,arr.length-2].sum == arr[1,arr.length-2].length\r\n return -1\r\n else\r\n idx = 1\r\n move = 0\r\n while idx < arr.length-1\r\n if arr[idx]%2==0\r\n move+=arr[idx]/2\r\n else\r\n move+=(arr[idx]+1)/2\r\n end\r\n idx+=1\r\n end\r\n end\r\n return move\r\nend\r\n\r\nt = gets.chomp.to_i\r\nt.times do\r\n len = gets.chomp\r\n arr = gets.chomp.split(' ').reject(&:empty?).map(&:to_i)\r\n puts stone(arr)\r\nend\r\n"}], "negative_code": [], "src_uid": "5b99775142b4a28b6b1069367602448f"} {"nl": {"description": "Vasya has the square chessboard of size n × n and m rooks. Initially the chessboard is empty. Vasya will consequently put the rooks on the board one after another.The cell of the field is under rook's attack, if there is at least one rook located in the same row or in the same column with this cell. If there is a rook located in the cell, this cell is also under attack.You are given the positions of the board where Vasya will put rooks. For each rook you have to determine the number of cells which are not under attack after Vasya puts it on the board.", "input_spec": "The first line of the input contains two integers n and m (1 ≤ n ≤ 100 000, 1 ≤ m ≤ min(100 000, n2)) — the size of the board and the number of rooks. Each of the next m lines contains integers xi and yi (1 ≤ xi, yi ≤ n) — the number of the row and the number of the column where Vasya will put the i-th rook. Vasya puts rooks on the board in the order they appear in the input. It is guaranteed that any cell will contain no more than one rook.", "output_spec": "Print m integer, the i-th of them should be equal to the number of cells that are not under attack after first i rooks are put.", "sample_inputs": ["3 3\n1 1\n3 1\n2 2", "5 2\n1 5\n5 1", "100000 1\n300 400"], "sample_outputs": ["4 2 0", "16 9", "9999800001"], "notes": "NoteOn the picture below show the state of the board after put each of the three rooks. The cells which painted with grey color is not under the attack. "}, "positive_code": [{"source_code": "def getRest n, rowslength, colslength\n count = n*n;\n count = count - (n * rowslength);\n count = count - ((n - rowslength) * colslength);\n return count;\nend\n\nn,m = gets.split(\" \").map{|i| i.to_i}\ncount = n*n;\nrows = {};\nrowsC = 0;\ncols = {};\ncolsC = 0;\nres = \"\";\n\nm.times do |i|\n x, y = gets.split(\" \").map{|i| i.to_i}\n if rows[x].nil?\n rows[x] = 1;\n rowsC = rowsC + 1\n end\n if cols[y].nil?\n cols[y] = 1;\n colsC = colsC + 1\n end\n res << getRest(n, rowsC, colsC).to_s\n if i != (m-1)\n res << \" \"\n end\nend\n\nputs res"}, {"source_code": "n, m = gets.chomp.split.map(&:to_i)\ncols = {}\nrows = {}\nans = []\nm.times do\n i, j = gets.chomp.split.map(&:to_i)\n rows[i] = true\n cols[j] = true\n\n ans << (n - rows.length) * (n - cols.length)\nend\nputs ans.join(' ')\n"}, {"source_code": "#!/usr/bin/ruby\n\nrequire 'set'\n\nn, m = gets.chomp.split.map(&:to_i)\ncol = Array.new(n+1){ |i| 0 }\nrow = Array.new(n+1){ |i| 0 }\nscol = Set.new\nsrow = Set.new\nres = n * n\nans = []\nfor i in 0...m do\n x, y = gets.chomp.split.map(&:to_i)\n unless col[x] == 1 && row[y] == 1\n csz = scol.size\n rsz = srow.size\n cmr = n - 1 - csz + col[x]\n rmr = n - 1 - rsz + row[y]\n if col[x] == 1\n res -= cmr\n elsif row[y] == 1\n res -= rmr\n else\n res -= rmr + cmr + 1\n end\n end\n col[x] = row[y] = 1\n scol << x\n srow << y\n ans.push( res )\nend\nputs ans.join(\" \")\n"}, {"source_code": "#! ruby\n# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n############################################################\ndef gs() gets.chomp end\ndef gi() gets.to_i end\ndef gss() gs.split end\ndef gis() gss.map(&:to_i) end\ndef nmapf(n,f) n.times.map{ __send__ f } end\ndef ngs(n) nmapf n,:gs end\ndef ngi(n) nmapf n,:gi end\ndef ngss(n) nmapf n,:gss end\ndef ngis(n) nmapf n,:gis end\ndef arr2d(h,w,v=0) h.times.map{[v] * w} end\ndef for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end\ndef nsum(n) n * (n + 1) / 2 end\ndef vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end\n############################################################\n\nN, M = gis\nrooks = ngis M\n\nans = []\n\nw = N\nh = N\n\nrows = [true] * N\ncols = [true] * N\n\nrooks.each do |y, x|\n if rows[y - 1]\n h -= 1\n rows[y - 1] = false\n end\n if cols[x - 1]\n w -= 1\n cols[x - 1] = false\n end\n ans << h * w\nend\n\nputs ans * ' '\n\n"}, {"source_code": "n,m=gets.split.map{|e| e.to_i}\nans=[]\nxs={}\nys={}\nn2=n*n\n\nm.times{\n\tx,y=gets.split.map{|e| e.to_i}\n\tf1=xs.key?(x)\n\tf2=ys.key?(y)\n\tif f1==true && f2==true then\n\t\t#\n\telsif f1==false && f2==true then\n\t\tn2=n2-(n-ys.size)\n\telsif f1==true && f2==false then\n\t\tn2=n2-(n-xs.size)\n\t\n\telse\n\t\tn2=n2-(n*2-xs.size-ys.size)+1\n\tend\n\tans<x} end\ndef min(a,b) a<=b ? a : b end\ndef max(a,b) a>=b ? a : b end\ndef sum(ar) ar.inject(:+) end\n\n#def bs(ar,eq,n) en=(eq==1 ? :>= : :>); ar.bsearch_index{|x|x.send(en,n)} end\ndef C(a,b) b==0||a==b ? 1 : (b=a-b if a/2 0\n print \"#{i} \"\n i -= 2\nend\ni = 2;\nwhile i < n\n print \"#{i} \"\n i += 2\nend\ni -= 2\nprint \"#{n} \" if n % 2 == 0 and n != i\nwhile i > 0\n print \"#{i} \"\n i -= 2\nend\ni = 2\nputs n\n"}, {"source_code": "n=gets.to_i;puts ((1+n%2).step(n-1,2).to_a+(n-1).step(1,-2).to_a+(2-n%2).step(n,2).to_a+(n-2).step(1,-2).to_a+[n])*' '\n"}, {"source_code": "n=gets.to_i;puts ((1+n%2).step(n-1,2).to_a+(n-1).step(1,-2).to_a+(2-n%2).step(n,2).to_a+(n-2).step(1,-2).to_a+[n])*' '\n"}, {"source_code": "n=gets.to_i;puts ((1+n%2).step(n-1,2).to_a+(n-1).step(1,-2).to_a+(2-n%2).step(n,2).to_a+(n-2).step(1,-2).to_a+[n])*' '\n"}], "negative_code": [{"source_code": "puts (1..gets.to_i).flat_map{|e|[e]*2}*' '"}], "src_uid": "c234cb0321e2bd235cd539a63364b152"} {"nl": {"description": "A sequence of $$$n$$$ non-negative integers ($$$n \\ge 2$$$) $$$a_1, a_2, \\dots, a_n$$$ is called good if for all $$$i$$$ from $$$1$$$ to $$$n-1$$$ the following condition holds true: $$$$$$a_1 \\: \\& \\: a_2 \\: \\& \\: \\dots \\: \\& \\: a_i = a_{i+1} \\: \\& \\: a_{i+2} \\: \\& \\: \\dots \\: \\& \\: a_n,$$$$$$ where $$$\\&$$$ denotes the bitwise AND operation.You are given an array $$$a$$$ of size $$$n$$$ ($$$n \\geq 2$$$). Find the number of permutations $$$p$$$ of numbers ranging from $$$1$$$ to $$$n$$$, for which the sequence $$$a_{p_1}$$$, $$$a_{p_2}$$$, ... ,$$$a_{p_n}$$$ is good. Since this number can be large, output it modulo $$$10^9+7$$$.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$), denoting the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2 \\le n \\le 2 \\cdot 10^5$$$) — the size of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$0 \\le a_i \\le 10^9$$$) — the elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \\cdot 10^5$$$.", "output_spec": "Output $$$t$$$ lines, where the $$$i$$$-th line contains the number of good permutations in the $$$i$$$-th test case modulo $$$10^9 + 7$$$.", "sample_inputs": ["4\n3\n1 1 1\n5\n1 2 3 4 5\n5\n0 2 0 3 0\n4\n1 3 5 1"], "sample_outputs": ["6\n0\n36\n4"], "notes": "NoteIn the first test case, since all the numbers are equal, whatever permutation we take, the sequence is good. There are a total of $$$6$$$ permutations possible with numbers from $$$1$$$ to $$$3$$$: $$$[1,2,3]$$$, $$$[1,3,2]$$$, $$$[2,1,3]$$$, $$$[2,3,1]$$$, $$$[3,1,2]$$$, $$$[3,2,1]$$$.In the second test case, it can be proved that no permutation exists for which the sequence is good.In the third test case, there are a total of $$$36$$$ permutations for which the sequence is good. One of them is the permutation $$$[1,5,4,2,3]$$$ which results in the sequence $$$s=[0,0,3,2,0]$$$. This is a good sequence because $$$ s_1 = s_2 \\: \\& \\: s_3 \\: \\& \\: s_4 \\: \\& \\: s_5 = 0$$$, $$$ s_1 \\: \\& \\: s_2 = s_3 \\: \\& \\: s_4 \\: \\& \\: s_5 = 0$$$, $$$ s_1 \\: \\& \\: s_2 \\: \\& \\: s_3 = s_4 \\: \\& \\: s_5 = 0$$$, $$$ s_1 \\: \\& \\: s_2 \\: \\& \\: s_3 \\: \\& \\: s_4 = s_5 = 0$$$. "}, "positive_code": [{"source_code": "RECURSIVE = false\n\ndef fact(n, p)\n return 1.upto(n).reduce(1.0) {|acc, x| (acc * x) % p } unless RECURSIVE\n\n if n <= 1\n return 1\n else\n return (n * fact(n - 1, p)) % p\n end\nend\n\nt = gets.to_s.to_i\n1.upto(t) do |casenum|\n n = gets.to_s.to_f\n a = gets.to_s.split.map {|x| x.to_i }\n\n mask = ~a.reduce(0xFFFFFFFF) {|acc, x| acc & x }\n zerocnt = a.reduce(0) {|acc, val| acc + (((val & mask) == 0) ? 1 : 0) }\n\n p = 10 ** 9 + 7\n permcnt = fact(n - 2, p) * zerocnt % p * (zerocnt - 1) % p\n printf(\"%d\\n\", permcnt)\nend\n"}, {"source_code": "RECURSIVE = false\n\ndef fact(n, p)\n return 1.upto(n).reduce(1) {|acc, x| (acc * x) % p } unless RECURSIVE\n\n if n <= 1\n return 1\n else\n return (n * fact(n - 1, p)) % p\n end\nend\n\nt = gets.to_s.to_i\n1.upto(t) do |casenum|\n n = gets.to_s.to_f\n a = gets.to_s.split.map {|x| x.to_i }\n\n mask = ~a.reduce(0xFFFFFFFF) {|acc, x| acc & x }\n zerocnt = a.reduce(0) {|acc, val| acc + (((val & mask) == 0) ? 1 : 0) }\n\n permcnt = fact(n - 2, 10 ** 9 + 7) * zerocnt * (zerocnt - 1)\n printf(\"%d\\n\", permcnt % (10 ** 9 + 7))\nend\n"}, {"source_code": "def fact(n, p)\n result = 1\n for i in (1 .. n) do\n result = (result * i) % p\n end\n result\nend\n\nt = gets.to_s.to_i\n1.upto(t) do |casenum|\n bitcounters = [0] * 32\n n = gets.to_s.to_i\n a = gets.to_s.split.map {|x| x.to_i }\n\n valmask = 0\n 0.upto(31) do |bitidx|\n bitmask = 1 << bitidx\n a.each do |val|\n if val & bitmask != 0\n bitcounters[bitidx] += 1\n end\n end\n valmask |= bitmask if bitcounters[bitidx] != a.size\n end\n\n zerocnt = 0\n a.each {|val| zerocnt += 1 if (val & valmask) == 0 }\n\n permcnt = fact(n - 2, 10 ** 9 + 7) * zerocnt * (zerocnt - 1)\n printf(\"%d\\n\", permcnt % (10 ** 9 + 7))\nend\n"}], "negative_code": [{"source_code": "RECURSIVE = false\n\ndef fact(n, p)\n return 1.upto(n).reduce(1.0) {|acc, x| (acc * x) % p } unless RECURSIVE\n\n if n <= 1\n return 1\n else\n return (n * fact(n - 1, p)) % p\n end\nend\n\nt = gets.to_s.to_i\n1.upto(t) do |casenum|\n n = gets.to_s.to_f\n a = gets.to_s.split.map {|x| x.to_i }\n\n mask = ~a.reduce(0xFFFFFFFF) {|acc, x| acc & x }\n zerocnt = a.reduce(0) {|acc, val| acc + (((val & mask) == 0) ? 1 : 0) }\n\n permcnt = fact(n - 2, 10 ** 9 + 7) * zerocnt * (zerocnt - 1)\n printf(\"%d\\n\", permcnt % (10 ** 9 + 7))\nend\n"}, {"source_code": "def fact(n, p)\n result = 1\n for i in (1 .. n) do\n result = (result * i) % p\n end\n result\nend\n\nt = gets.to_s.to_i\n1.upto(t) do |casenum|\n bitcounters = [0] * 32\n n = gets.to_s.to_i\n a = gets.to_s.split.map {|x| x.to_i }\n\n valmask = 0\n 0.upto(31) do |bitidx|\n bitmask = 1 << bitidx\n a.each do |val|\n if val & bitmask != 0\n bitcounters[bitidx] += 1\n end\n end\n valmask |= bitmask if bitcounters[bitidx] != a.size\n end\n\n zerocnt = 0\n a.each {|val| zerocnt += 1 if (val & valmask) == 0 }\n\n permcnt = (fact(n, 10 ** 9 + 7) * zerocnt * (zerocnt - 1) / (n * (n - 1)))\n printf(\"%d\\n\", permcnt % (10 ** 9 + 7))\nend\n"}], "src_uid": "e4685bb0a1bf8c2da326cd0e5b8f4fe7"} {"nl": {"description": "Petya has k matches, placed in n matchboxes lying in a line from left to right. We know that k is divisible by n. Petya wants all boxes to have the same number of matches inside. For that, he can move a match from its box to the adjacent one in one move. How many such moves does he need to achieve the desired configuration?", "input_spec": "The first line contains integer n (1 ≤ n ≤ 50000). The second line contains n non-negative numbers that do not exceed 109, the i-th written number is the number of matches in the i-th matchbox. It is guaranteed that the total number of matches is divisible by n.", "output_spec": "Print the total minimum number of moves.", "sample_inputs": ["6\n1 6 2 5 3 7"], "sample_outputs": ["12"], "notes": null}, "positive_code": [{"source_code": "n=gets.to_i\nxs=gets.split.map(&:to_i)\nm=xs.inject(:+)/n\n\nk,r=0,0\n\nfor i in (0...n)\n k+=xs[i]-m\n r+=k.abs\nend\n\np r\n"}], "negative_code": [], "src_uid": "41ae8b0dee3b0f4e91bd06e4a0635492"} {"nl": {"description": "Your company was appointed to lay new asphalt on the highway of length $$$n$$$. You know that every day you can either repair one unit of the highway (lay new asphalt over one unit of the highway) or skip repairing.Skipping the repair is necessary because of the climate. The climate in your region is periodical: there are $$$g$$$ days when the weather is good and if you lay new asphalt these days it becomes high-quality pavement; after that, the weather during the next $$$b$$$ days is bad, and if you lay new asphalt these days it becomes low-quality pavement; again $$$g$$$ good days, $$$b$$$ bad days and so on.You can be sure that you start repairing at the start of a good season, in other words, days $$$1, 2, \\dots, g$$$ are good.You don't really care about the quality of the highway, you just want to make sure that at least half of the highway will have high-quality pavement. For example, if the $$$n = 5$$$ then at least $$$3$$$ units of the highway should have high quality; if $$$n = 4$$$ then at least $$$2$$$ units should have high quality.What is the minimum number of days is needed to finish the repair of the whole highway?", "input_spec": "The first line contains a single integer $$$T$$$ ($$$1 \\le T \\le 10^4$$$) — the number of test cases. Next $$$T$$$ lines contain test cases — one per line. Each line contains three integers $$$n$$$, $$$g$$$ and $$$b$$$ ($$$1 \\le n, g, b \\le 10^9$$$) — the length of the highway and the number of good and bad days respectively.", "output_spec": "Print $$$T$$$ integers — one per test case. For each test case, print the minimum number of days required to repair the whole highway if at least half of it should have high quality.", "sample_inputs": ["3\n5 1 1\n8 10 10\n1000000 1 1000000"], "sample_outputs": ["5\n8\n499999500000"], "notes": "NoteIn the first test case, you can just lay new asphalt each day, since days $$$1, 3, 5$$$ are good.In the second test case, you can also lay new asphalt each day, since days $$$1$$$-$$$8$$$ are good."}, "positive_code": [{"source_code": "T = gets.to_i\n\ndef f(day, n, g, b)\n l = (n / 2.0).ceil\n unit = g + b\n good_cnt = 0\n bad_cnt = 0\n\n block = day / unit\n remain = day - block * unit\n good_cnt += g * block\n\n if remain > 0\n good_cnt += [remain, g].min\n end\n\n l <= good_cnt\nend\n\nT.times do\n n, g, b = gets.split.map(&:to_i)\n\n ng = 0\n ok = n * (g + b)\n\n while (ok - ng).abs > 1\n mid = (ok + ng) / 2\n\n if f(mid, n, g, b)\n ok = mid\n else\n ng = mid\n end\n end\n\n puts [n, ok].max\nend\n"}], "negative_code": [], "src_uid": "be9138aca8e1b8a5d722f99fcd70b685"} {"nl": {"description": "Hexadecimal likes drawing. She has drawn many graphs already, both directed and not. Recently she has started to work on a still-life «interesting graph and apples». An undirected graph is called interesting, if each of its vertices belongs to one cycle only — a funny ring — and does not belong to any other cycles. A funny ring is a cycle that goes through all the vertices just once. Moreover, loops are funny rings too.She has already drawn the apples and some of the graph edges. But now it is not clear, how to connect the rest of the vertices to get an interesting graph as a result. The answer should contain the minimal amount of added edges. And furthermore, the answer should be the lexicographically smallest one. The set of edges (x1, y1), (x2, y2), ..., (xn, yn), where xi ≤ yi, is lexicographically smaller than the set (u1, v1), (u2, v2), ..., (un, vn), where ui ≤ vi, provided that the sequence of integers x1, y1, x2, y2, ..., xn, yn is lexicographically smaller than the sequence u1, v1, u2, v2, ..., un, vn. If you do not cope, Hexadecimal will eat you. ...eat you alive.", "input_spec": "The first line of the input data contains a pair of integers n and m (1 ≤ n ≤ 50, 0 ≤ m ≤ 2500) — the amount of vertices and edges respectively. The following lines contain pairs of numbers xi and yi (1 ≤ xi, yi ≤ n) — the vertices that are already connected by edges. The initial graph may contain multiple edges and loops.", "output_spec": "In the first line output «YES» or «NO»: if it is possible or not to construct an interesting graph. If the answer is «YES», in the second line output k — the amount of edges that should be added to the initial graph. Finally, output k lines: pairs of vertices xj and yj, between which edges should be drawn. The result may contain multiple edges and loops. k can be equal to zero.", "sample_inputs": ["3 2\n1 2\n2 3"], "sample_outputs": ["YES\n1\n1 3"], "notes": null}, "positive_code": [{"source_code": "require 'scanf'\n\nclass UnionFindSet\n\tdef initialize(n)\n\t\t@n = n\n\t\t@bel = Array.new(n) { |x| x }\n\tend\n\n\tdef find(x)\n\t\t@bel[x] = find(@bel[x]) unless @bel[x] == x\n\t\t@bel[x]\n\tend\n\n\tdef union(x, y) \n\t\t@bel[find(x)] = find(y)\n\tend\nend\n\nn, m = scanf(\"%d%d\\n\")\nd = Array.new(n) { 0 }\n\nuf = UnionFindSet.new(n)\njoinEdge = 0\nfor i in 1..m\n\ta, b = gets.chomp.scanf(\"%d %d\")\n#\tputs \"a = #{a}, b = #{b}\"\n\ta -= 1\n\tb -= 1\n\td[a] += 1\n\td[b] += 1\n\tjoinEdge += 1 if uf.find(a) == uf.find(b)\n\tuf.union(a, b)\nend\n\nmaxDeg = d.max\n#puts \"max deg = #{maxDeg}, joinEdge = #{joinEdge}, n = #{n}, m = #{m}\"\n\nif maxDeg > 2\n\tputs \"NO\"\nelsif joinEdge > 0\n\tif maxDeg == 2 and joinEdge == 1 and n == m\n\t\tputs \"YES\"\n\t\tputs \"0\"\n\telse \n\t\tputs \"NO\"\n\tend\nelse\n\tputs \"YES\"\n\tputs \"#{n - m}\"\n\tfor i in 0...n\n\t\tfor j in i+1...n\n\t\t\tif d[i] < 2 and d[j] < 2 and uf.find(i) != uf.find(j)\n\t\t\t\tputs \"#{i+1} #{j+1}\"\n\t\t\t\tuf.union(i, j)\n\t\t\t\td[i] += 1\n\t\t\t\td[j] += 1\n\t\t\tend\n\t\tend\n\tend\n\tfor i in 0...n\n\t\tfor j in (n-1).downto(i)\n\t\t\tif d[i] < 2 and d[j] < 2 \n\t\t\t\tputs \"#{i+1} #{j+1}\"\n\t\t\t\td[i] += 1\n\t\t\t\td[j] += 1\n\t\t\tend\n\t\tend\n\tend\nend\n"}, {"source_code": "require 'scanf'\n\nclass UnionFindSet\n\tdef initialize(n)\n\t\t@n = n\n\t\t@bel = Array.new(n) { |x| x }\n\tend\n\n\tdef find(x)\n\t\t@bel[x] = find(@bel[x]) unless @bel[x] == x\n\t\t@bel[x]\n\tend\n\n\tdef union(x, y) \n\t\t@bel[find(x)] = find(y)\n\tend\nend\n\nn, m = scanf(\"%d %d\\n\")\nd = Array.new(n) { 0 }\n\nuf = UnionFindSet.new(n)\njoinEdge = 0\nfor i in 1..m\n\ta, b = scanf(\"%d %d\\n\")\n#\tputs \"a = #{a}, b = #{b}\"\n\ta -= 1\n\tb -= 1\n\td[a] += 1\n\td[b] += 1\n\tjoinEdge += 1 if uf.find(a) == uf.find(b)\n\tuf.union(a, b)\nend\n\nmaxDeg = d.max\n#puts \"max deg = #{maxDeg}, joinEdge = #{joinEdge}, n = #{n}, m = #{m}\"\n\nif maxDeg > 2\n\tputs \"NO\"\nelsif joinEdge > 0\n\tif maxDeg == 2 and joinEdge == 1 and n == m\n\t\tputs \"YES\"\n\t\tputs \"0\"\n\telse \n\t\tputs \"NO\"\n\tend\nelse\n\tputs \"YES\"\n\tputs \"#{n - m}\"\n\tfor i in 0...n\n\t\tfor j in i+1...n\n\t\t\tif d[i] < 2 and d[j] < 2 and uf.find(i) != uf.find(j)\n\t\t\t\tputs \"#{i+1} #{j+1}\"\n\t\t\t\tuf.union(i, j)\n\t\t\t\td[i] += 1\n\t\t\t\td[j] += 1\n\t\t\tend\n\t\tend\n\tend\n\tfor i in 0...n\n\t\tfor j in (n-1).downto(i)\n\t\t\tif d[i] < 2 and d[j] < 2 \n\t\t\t\tputs \"#{i+1} #{j+1}\"\n\t\t\t\td[i] += 1\n\t\t\t\td[j] += 1\n\t\t\tend\n\t\tend\n\tend\nend\n"}, {"source_code": "require 'scanf'\n\nclass UnionFindSet\n\tdef initialize(n)\n\t\t@n = n\n\t\t@bel = Array.new(n) { |x| x }\n\tend\n\n\tdef find(x)\n\t\t@bel[x] = find(@bel[x]) unless @bel[x] == x\n\t\t@bel[x]\n\tend\n\n\tdef union(x, y) \n\t\t@bel[find(x)] = find(y)\n\tend\nend\n\nn, m = scanf(\"%d%d\\n\")\nd = Array.new(n) { 0 }\n\nuf = UnionFindSet.new(n)\njoinEdge = 0\nfor i in 1..m\n\ta, b = scanf(\"%d%d\\n\")\n#\tputs \"a = #{a}, b = #{b}\"\n\ta -= 1\n\tb -= 1\n\td[a] += 1\n\td[b] += 1\n\tjoinEdge += 1 if uf.find(a) == uf.find(b)\n\tuf.union(a, b)\nend\n\nmaxDeg = d.max\n#puts \"max deg = #{maxDeg}, joinEdge = #{joinEdge}, n = #{n}, m = #{m}\"\n\nif maxDeg > 2\n\tputs \"NO\"\nelsif joinEdge > 0\n\tif maxDeg == 2 and joinEdge == 1 and n == m\n\t\tputs \"YES\"\n\t\tputs \"0\"\n\telse \n\t\tputs \"NO\"\n\tend\nelse\n\tputs \"YES\"\n\tputs \"#{n - m}\"\n\tfor i in 0...n\n\t\tfor j in i+1...n\n\t\t\tif d[i] < 2 and d[j] < 2 and uf.find(i) != uf.find(j)\n\t\t\t\tputs \"#{i+1} #{j+1}\"\n\t\t\t\tuf.union(i, j)\n\t\t\t\td[i] += 1\n\t\t\t\td[j] += 1\n\t\t\tend\n\t\tend\n\tend\n\tfor i in 0...n\n\t\tfor j in (n-1).downto(i)\n\t\t\tif d[i] < 2 and d[j] < 2 \n\t\t\t\tputs \"#{i+1} #{j+1}\"\n\t\t\t\td[i] += 1\n\t\t\t\td[j] += 1\n\t\t\tend\n\t\tend\n\tend\nend\n"}], "negative_code": [{"source_code": "require 'scanf'\n\nn, m = scanf(\"%d %d\")\nd = Array.new(n) { 0 }\n\nclass UnionFindSet\n\tdef initialize(n)\n\t\t@n = n\n\t\t@bel = Array.new(n) { |x| x }\n\tend\n\n\tdef find(x)\n\t\t@bel[x] = find(@bel[x]) unless @bel[x] == x\n\t\t@bel[x]\n\tend\n\n\tdef union(x, y) \n\t\t@bel[find(x)] = find(y)\n\tend\nend\n\nuf = UnionFindSet.new(n)\njoinEdge = 0\nfor i in 1..m\n\ta, b = scanf(\"%d %d\").map { |x| x - 1 }\n\tputs \"a = #{a}, b = #{b}\"\n\td[a] += 1\n\td[b] += 1\n\tjoinEdge += 1 if uf.find(a) == uf.find(b)\n\tuf.union(a, b)\nend\n\nmaxDeg = d.max\nputs \"max deg = #{maxDeg}, joinEdge = #{joinEdge}, n = #{n}, m = #{m}\"\n\nif maxDeg > 2\n\tputs \"NO\"\nelsif joinEdge > 0\n\tif maxDeg == 2 and joinEdge == 1 and n == m\n\t\tputs \"YES\"\n\t\tputs \"0\"\n\telse \n\t\tputs \"NO\"\n\tend\nelse\n\tputs \"YES\"\n\tputs \"#{n - m}\"\n\tfor i in 0...n\n\t\tfor j in i+1...n\n\t\t\tif d[i] < 2 and d[j] < 2 and uf.find(i) != uf.find(j)\n\t\t\t\tputs \"#{i+1} #{j+1}\"\n\t\t\t\tuf.union(i, j)\n\t\t\t\td[i] += 1\n\t\t\t\td[j] += 1\n\t\t\tend\n\t\tend\n\tend\n\tfor i in 0...n\n\t\tfor j in i+1...n\n\t\t\tif d[i] < 2 and d[j] < 2 \n\t\t\t\tputs \"#{i+1} #{j+1}\"\n\t\t\tend\n\t\tend\n\tend\nend\n"}, {"source_code": "require 'scanf'\nrequire 'matrix'\n\nclass Matrix \n\tdef []=(i,j,x)\n\t\t@rows[i][j] = x\n\tend\nend\n\nn, m = scanf(\"%d %d\")\ng = Matrix.build(n) { 0 }\nd = Array.new(n) { 0 }\n\nclass UnionFindSet\n\tdef initialize(n)\n\t\t@n = n\n\t\t@bel = Array.new(n) { |x| x }\n\tend\n\n\tdef find(x)\n\t\t@bel[x] = find(@bel[x]) unless @bel[x] == x\n\t\t@bel[x]\n\tend\n\n\tdef union(x, y) \n\t\t@bel[find(x)] = find(y)\n\tend\nend\n\nuf = UnionFindSet.new(n)\njoinEdge = 0\nfor i in 1..m\n\ta, b = scanf(\"%d %d\").map { |x| x - 1 }\n\tg[a, b] = 1\n\tg[b, a] = 1\n\td[a] += 1\n\td[b] += 1\n\tjoinEdge += 1 if uf.find(a) == uf.find(b)\n\tuf.union(a, b)\nend\n\nmaxDeg = d.max\n\nif maxDeg > 2\n\tputs \"NO\"\nelsif joinEdge > 0\n\tif maxDeg == 2 and joinEdge == 1 and n == m\n\t\tputs \"YES\"\n\t\tputs \"0\"\n\telse \n\t\tputs \"NO\"\n\tend\nelse\n\tputs \"YES\"\n\tputs \"#{n - m}\"\n\tfor i in 0...n\n\t\tfor j in i+1...n\n\t\t\tif d[i] < 2 and d[j] < 2 and uf.find(i) != uf.find(j)\n\t\t\t\tputs \"#{i+1} #{j+1}\"\n\t\t\t\tuf.union(i, j)\n\t\t\t\td[i] += 1\n\t\t\t\td[j] += 1\n\t\t\tend\n\t\tend\n\tend\n\tfor i in 0...n\n\t\tfor j in i+1...n\n\t\t\tif d[i] < 2 and d[j] < 2 \n\t\t\t\tputs \"#{i+1} #{j+1}\"\n\t\t\tend\n\t\tend\n\tend\nend\n"}, {"source_code": "require 'scanf'\n\nclass UnionFindSet\n\tdef initialize(n)\n\t\t@n = n\n\t\t@bel = Array.new(n) { |x| x }\n\tend\n\n\tdef find(x)\n\t\t@bel[x] = find(@bel[x]) unless @bel[x] == x\n\t\t@bel[x]\n\tend\n\n\tdef union(x, y) \n\t\t@bel[find(x)] = find(y)\n\tend\nend\n\nn, m = scanf(\"%d%d\")\nd = Array.new(n) { 0 }\n\nuf = UnionFindSet.new(n)\njoinEdge = 0\nfor i in 1..m\n\ta, b = scanf(\"%d%d\")\n\tputs \"a = #{a}, b = #{b}\"\n\ta -= 1\n\tb -= 1\n\td[a] += 1\n\td[b] += 1\n\tjoinEdge += 1 if uf.find(a) == uf.find(b)\n\tuf.union(a, b)\nend\n\nmaxDeg = d.max\nputs \"max deg = #{maxDeg}, joinEdge = #{joinEdge}, n = #{n}, m = #{m}\"\n\nif maxDeg > 2\n\tputs \"NO\"\nelsif joinEdge > 0\n\tif maxDeg == 2 and joinEdge == 1 and n == m\n\t\tputs \"YES\"\n\t\tputs \"0\"\n\telse \n\t\tputs \"NO\"\n\tend\nelse\n\tputs \"YES\"\n\tputs \"#{n - m}\"\n\tfor i in 0...n\n\t\tfor j in i+1...n\n\t\t\tif d[i] < 2 and d[j] < 2 and uf.find(i) != uf.find(j)\n\t\t\t\tputs \"#{i+1} #{j+1}\"\n\t\t\t\tuf.union(i, j)\n\t\t\t\td[i] += 1\n\t\t\t\td[j] += 1\n\t\t\tend\n\t\tend\n\tend\n\tfor i in 0...n\n\t\tfor j in i+1...n\n\t\t\tif d[i] < 2 and d[j] < 2 \n\t\t\t\tputs \"#{i+1} #{j+1}\"\n\t\t\tend\n\t\tend\n\tend\nend\n"}, {"source_code": "require 'scanf'\n\nn, m = scanf(\"%d %d\")\nd = Array.new(n) { 0 }\n\nclass UnionFindSet\n\tdef initialize(n)\n\t\t@n = n\n\t\t@bel = Array.new(n) { |x| x }\n\tend\n\n\tdef find(x)\n\t\t@bel[x] = find(@bel[x]) unless @bel[x] == x\n\t\t@bel[x]\n\tend\n\n\tdef union(x, y) \n\t\t@bel[find(x)] = find(y)\n\tend\nend\n\nuf = UnionFindSet.new(n)\njoinEdge = 0\nfor i in 1..m\n\ta, b = scanf(\"%d %d\").map { |x| x - 1 }\n\td[a] += 1\n\td[b] += 1\n\tjoinEdge += 1 if uf.find(a) == uf.find(b)\n\tuf.union(a, b)\nend\n\nmaxDeg = d.max\nputs \"max deg = #{maxDeg}, joinEdge = #{joinEdge}, n = #{n}, m = #{m}\"\n\nif maxDeg > 2\n\tputs \"NO\"\nelsif joinEdge > 0\n\tif maxDeg == 2 and joinEdge == 1 and n == m\n\t\tputs \"YES\"\n\t\tputs \"0\"\n\telse \n\t\tputs \"NO\"\n\tend\nelse\n\tputs \"YES\"\n\tputs \"#{n - m}\"\n\tfor i in 0...n\n\t\tfor j in i+1...n\n\t\t\tif d[i] < 2 and d[j] < 2 and uf.find(i) != uf.find(j)\n\t\t\t\tputs \"#{i+1} #{j+1}\"\n\t\t\t\tuf.union(i, j)\n\t\t\t\td[i] += 1\n\t\t\t\td[j] += 1\n\t\t\tend\n\t\tend\n\tend\n\tfor i in 0...n\n\t\tfor j in i+1...n\n\t\t\tif d[i] < 2 and d[j] < 2 \n\t\t\t\tputs \"#{i+1} #{j+1}\"\n\t\t\tend\n\t\tend\n\tend\nend\n"}, {"source_code": "require 'scanf'\n\nn, m = scanf(\"%d%d\")\nd = Array.new(n) { 0 }\n\nclass UnionFindSet\n\tdef initialize(n)\n\t\t@n = n\n\t\t@bel = Array.new(n) { |x| x }\n\tend\n\n\tdef find(x)\n\t\t@bel[x] = find(@bel[x]) unless @bel[x] == x\n\t\t@bel[x]\n\tend\n\n\tdef union(x, y) \n\t\t@bel[find(x)] = find(y)\n\tend\nend\n\nuf = UnionFindSet.new(n)\njoinEdge = 0\nfor i in 1..m\n\ta, b = scanf(\"%d%d\")\n\tputs \"a = #{a}, b = #{b}\"\n\ta -= 1\n\tb -= 1\n\td[a] += 1\n\td[b] += 1\n\tjoinEdge += 1 if uf.find(a) == uf.find(b)\n\tuf.union(a, b)\nend\n\nmaxDeg = d.max\nputs \"max deg = #{maxDeg}, joinEdge = #{joinEdge}, n = #{n}, m = #{m}\"\n\nif maxDeg > 2\n\tputs \"NO\"\nelsif joinEdge > 0\n\tif maxDeg == 2 and joinEdge == 1 and n == m\n\t\tputs \"YES\"\n\t\tputs \"0\"\n\telse \n\t\tputs \"NO\"\n\tend\nelse\n\tputs \"YES\"\n\tputs \"#{n - m}\"\n\tfor i in 0...n\n\t\tfor j in i+1...n\n\t\t\tif d[i] < 2 and d[j] < 2 and uf.find(i) != uf.find(j)\n\t\t\t\tputs \"#{i+1} #{j+1}\"\n\t\t\t\tuf.union(i, j)\n\t\t\t\td[i] += 1\n\t\t\t\td[j] += 1\n\t\t\tend\n\t\tend\n\tend\n\tfor i in 0...n\n\t\tfor j in i+1...n\n\t\t\tif d[i] < 2 and d[j] < 2 \n\t\t\t\tputs \"#{i+1} #{j+1}\"\n\t\t\tend\n\t\tend\n\tend\nend\n"}, {"source_code": "require 'scanf'\n\nclass UnionFindSet\n\tdef initialize(n)\n\t\t@n = n\n\t\t@bel = Array.new(n) { |x| x }\n\tend\n\n\tdef find(x)\n\t\t@bel[x] = find(@bel[x]) unless @bel[x] == x\n\t\t@bel[x]\n\tend\n\n\tdef union(x, y) \n\t\t@bel[find(x)] = find(y)\n\tend\nend\n\nn, m = scanf(\"%d%d\")\nd = Array.new(n) { 0 }\n\nuf = UnionFindSet.new(n)\njoinEdge = 0\nfor i in 1..m\n\ta, b = scanf(\"%d%d\")\n#\tputs \"a = #{a}, b = #{b}\"\n\ta -= 1\n\tb -= 1\n\td[a] += 1\n\td[b] += 1\n\tjoinEdge += 1 if uf.find(a) == uf.find(b)\n\tuf.union(a, b)\nend\n\nmaxDeg = d.max\n#puts \"max deg = #{maxDeg}, joinEdge = #{joinEdge}, n = #{n}, m = #{m}\"\n\nif maxDeg > 2\n\tputs \"NO\"\nelsif joinEdge > 0\n\tif maxDeg == 2 and joinEdge == 1 and n == m\n\t\tputs \"YES\"\n\t\tputs \"0\"\n\telse \n\t\tputs \"NO\"\n\tend\nelse\n\tputs \"YES\"\n\tputs \"#{n - m}\"\n\tfor i in 0...n\n\t\tfor j in i+1...n\n\t\t\tif d[i] < 2 and d[j] < 2 and uf.find(i) != uf.find(j)\n\t\t\t\tputs \"#{i+1} #{j+1}\"\n\t\t\t\tuf.union(i, j)\n\t\t\t\td[i] += 1\n\t\t\t\td[j] += 1\n\t\t\tend\n\t\tend\n\tend\n\tfor i in 0...n\n\t\tfor j in (n-1).downto(i)\n\t\t\tif d[i] < 2 and d[j] < 2 \n\t\t\t\tputs \"#{i+1} #{j+1}\"\n\t\t\t\td[i] += 1\n\t\t\t\td[j] += 1\n\t\t\tend\n\t\tend\n\tend\nend\n"}, {"source_code": "require 'scanf'\n\nn, m = scanf(\"%d %d\")\nd = Array.new(n) { 0 }\n\nclass UnionFindSet\n\tdef initialize(n)\n\t\t@n = n\n\t\t@bel = Array.new(n) { |x| x }\n\tend\n\n\tdef find(x)\n\t\t@bel[x] = find(@bel[x]) unless @bel[x] == x\n\t\t@bel[x]\n\tend\n\n\tdef union(x, y) \n\t\t@bel[find(x)] = find(y)\n\tend\nend\n\nuf = UnionFindSet.new(n)\njoinEdge = 0\nfor i in 1..m\n\ta, b = scanf(\"%d %d\")\n\tputs \"a = #{a}, b = #{b}\"\n\ta -= 1\n\tb -= 1\n\td[a] += 1\n\td[b] += 1\n\tjoinEdge += 1 if uf.find(a) == uf.find(b)\n\tuf.union(a, b)\nend\n\nmaxDeg = d.max\nputs \"max deg = #{maxDeg}, joinEdge = #{joinEdge}, n = #{n}, m = #{m}\"\n\nif maxDeg > 2\n\tputs \"NO\"\nelsif joinEdge > 0\n\tif maxDeg == 2 and joinEdge == 1 and n == m\n\t\tputs \"YES\"\n\t\tputs \"0\"\n\telse \n\t\tputs \"NO\"\n\tend\nelse\n\tputs \"YES\"\n\tputs \"#{n - m}\"\n\tfor i in 0...n\n\t\tfor j in i+1...n\n\t\t\tif d[i] < 2 and d[j] < 2 and uf.find(i) != uf.find(j)\n\t\t\t\tputs \"#{i+1} #{j+1}\"\n\t\t\t\tuf.union(i, j)\n\t\t\t\td[i] += 1\n\t\t\t\td[j] += 1\n\t\t\tend\n\t\tend\n\tend\n\tfor i in 0...n\n\t\tfor j in i+1...n\n\t\t\tif d[i] < 2 and d[j] < 2 \n\t\t\t\tputs \"#{i+1} #{j+1}\"\n\t\t\tend\n\t\tend\n\tend\nend\n"}, {"source_code": "require 'scanf'\n\nclass UnionFindSet\n\tdef initialize(n)\n\t\t@n = n\n\t\t@bel = Array.new(n) { |x| x }\n\tend\n\n\tdef find(x)\n\t\t@bel[x] = find(@bel[x]) unless @bel[x] == x\n\t\t@bel[x]\n\tend\n\n\tdef union(x, y) \n\t\t@bel[find(x)] = find(y)\n\tend\nend\n\nn, m = scanf(\"%d%d\\n\")\nd = Array.new(n) { 0 }\n\nuf = UnionFindSet.new(n)\njoinEdge = 0\nfor i in 1..m\n\ta, b = gets.chomp.scanf(\"%d %d\")\n#\tputs \"a = #{a}, b = #{b}\"\n\ta -= 1\n\tb -= 1\n\td[a] += 1\n\td[b] += 1\n\tjoinEdge += 1 if uf.find(a) == uf.find(b)\n\tuf.union(a, b)\nend\n\nmaxDeg = d.max\n#puts \"max deg = #{maxDeg}, joinEdge = #{joinEdge}, n = #{n}, m = #{m}\"\n\nif maxDeg > 2\n\tputs \"NO\"\nelsif joinEdge > 0\n\tif maxDeg == 2 and joinEdge == 1 and n == m\n\t\tputs \"YES\"\n\t\tputs \"0\"\n\telse \n\t\tputs \"NO\"\n\tend\nelse\n\tputs \"YES\"\n\tputs \"#{n - m}\"\n\tfor i in 0...n\n\t\tfor j in i+1...n\n\t\t\tif d[i] < 2 and d[j] < 2 and uf.find(i) != uf.find(j)\n\t\t\t\tputs \"#{i+1} #{j+1}\"\n\t\t\t\tuf.union(i, j)\n\t\t\t\td[i] += 1\n\t\t\t\td[j] += 1\n\t\t\tend\n\t\tend\n\tend\n\tfor i in 0...n\n\t\tfor j in i+1...n\n\t\t\tif d[i] < 2 and d[j] < 2 \n\t\t\t\tputs \"#{i+1} #{j+1}\"\n\t\t\tend\n\t\tend\n\tend\nend\n"}, {"source_code": "require 'scanf'\n\nclass UnionFindSet\n\tdef initialize(n)\n\t\t@n = n\n\t\t@bel = Array.new(n) { |x| x }\n\tend\n\n\tdef find(x)\n\t\t@bel[x] = find(@bel[x]) unless @bel[x] == x\n\t\t@bel[x]\n\tend\n\n\tdef union(x, y) \n\t\t@bel[find(x)] = find(y)\n\tend\nend\n\nn, m = scanf(\" %d%d\")\nd = Array.new(n) { 0 }\n\nuf = UnionFindSet.new(n)\njoinEdge = 0\nfor i in 1..m\n\ta, b = scanf(\" %d%d\")\n#\tputs \"a = #{a}, b = #{b}\"\n\ta -= 1\n\tb -= 1\n\td[a] += 1\n\td[b] += 1\n\tjoinEdge += 1 if uf.find(a) == uf.find(b)\n\tuf.union(a, b)\nend\n\nmaxDeg = d.max\n#puts \"max deg = #{maxDeg}, joinEdge = #{joinEdge}, n = #{n}, m = #{m}\"\n\nif maxDeg > 2\n\tputs \"NO\"\nelsif joinEdge > 0\n\tif maxDeg == 2 and joinEdge == 1 and n == m\n\t\tputs \"YES\"\n\t\tputs \"0\"\n\telse \n\t\tputs \"NO\"\n\tend\nelse\n\tputs \"YES\"\n\tputs \"#{n - m}\"\n\tfor i in 0...n\n\t\tfor j in i+1...n\n\t\t\tif d[i] < 2 and d[j] < 2 and uf.find(i) != uf.find(j)\n\t\t\t\tputs \"#{i+1} #{j+1}\"\n\t\t\t\tuf.union(i, j)\n\t\t\t\td[i] += 1\n\t\t\t\td[j] += 1\n\t\t\tend\n\t\tend\n\tend\n\tfor i in 0...n\n\t\tfor j in (n-1).downto(i)\n\t\t\tif d[i] < 2 and d[j] < 2 \n\t\t\t\tputs \"#{i+1} #{j+1}\"\n\t\t\t\td[i] += 1\n\t\t\t\td[j] += 1\n\t\t\tend\n\t\tend\n\tend\nend\n"}, {"source_code": "require 'scanf'\n\nclass UnionFindSet\n\tdef initialize(n)\n\t\t@n = n\n\t\t@bel = Array.new(n) { |x| x }\n\tend\n\n\tdef find(x)\n\t\t@bel[x] = find(@bel[x]) unless @bel[x] == x\n\t\t@bel[x]\n\tend\n\n\tdef union(x, y) \n\t\t@bel[find(x)] = find(y)\n\tend\nend\n\nn, m = scanf(\"%d%d\\n\")\nd = Array.new(n) { 0 }\n\nuf = UnionFindSet.new(n)\njoinEdge = 0\nfor i in 1..m\n\ta, b = gets.chomp.scanf(\"%d %d\")\n\tputs \"a = #{a}, b = #{b}\"\n\ta -= 1\n\tb -= 1\n\td[a] += 1\n\td[b] += 1\n\tjoinEdge += 1 if uf.find(a) == uf.find(b)\n\tuf.union(a, b)\nend\n\nmaxDeg = d.max\nputs \"max deg = #{maxDeg}, joinEdge = #{joinEdge}, n = #{n}, m = #{m}\"\n\nif maxDeg > 2\n\tputs \"NO\"\nelsif joinEdge > 0\n\tif maxDeg == 2 and joinEdge == 1 and n == m\n\t\tputs \"YES\"\n\t\tputs \"0\"\n\telse \n\t\tputs \"NO\"\n\tend\nelse\n\tputs \"YES\"\n\tputs \"#{n - m}\"\n\tfor i in 0...n\n\t\tfor j in i+1...n\n\t\t\tif d[i] < 2 and d[j] < 2 and uf.find(i) != uf.find(j)\n\t\t\t\tputs \"#{i+1} #{j+1}\"\n\t\t\t\tuf.union(i, j)\n\t\t\t\td[i] += 1\n\t\t\t\td[j] += 1\n\t\t\tend\n\t\tend\n\tend\n\tfor i in 0...n\n\t\tfor j in i+1...n\n\t\t\tif d[i] < 2 and d[j] < 2 \n\t\t\t\tputs \"#{i+1} #{j+1}\"\n\t\t\tend\n\t\tend\n\tend\nend\n"}], "src_uid": "bbcb051b08fa3d19a7cf285242d50451"} {"nl": {"description": "Lunar New Year is approaching, and Bob decides to take a wander in a nearby park.The park can be represented as a connected graph with $$$n$$$ nodes and $$$m$$$ bidirectional edges. Initially Bob is at the node $$$1$$$ and he records $$$1$$$ on his notebook. He can wander from one node to another through those bidirectional edges. Whenever he visits a node not recorded on his notebook, he records it. After he visits all nodes at least once, he stops wandering, thus finally a permutation of nodes $$$a_1, a_2, \\ldots, a_n$$$ is recorded.Wandering is a boring thing, but solving problems is fascinating. Bob wants to know the lexicographically smallest sequence of nodes he can record while wandering. Bob thinks this problem is trivial, and he wants you to solve it.A sequence $$$x$$$ is lexicographically smaller than a sequence $$$y$$$ if and only if one of the following holds: $$$x$$$ is a prefix of $$$y$$$, but $$$x \\ne y$$$ (this is impossible in this problem as all considered sequences have the same length); in the first position where $$$x$$$ and $$$y$$$ differ, the sequence $$$x$$$ has a smaller element than the corresponding element in $$$y$$$. ", "input_spec": "The first line contains two positive integers $$$n$$$ and $$$m$$$ ($$$1 \\leq n, m \\leq 10^5$$$), denoting the number of nodes and edges, respectively. The following $$$m$$$ lines describe the bidirectional edges in the graph. The $$$i$$$-th of these lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \\leq u_i, v_i \\leq n$$$), representing the nodes the $$$i$$$-th edge connects. Note that the graph can have multiple edges connecting the same two nodes and self-loops. It is guaranteed that the graph is connected.", "output_spec": "Output a line containing the lexicographically smallest sequence $$$a_1, a_2, \\ldots, a_n$$$ Bob can record.", "sample_inputs": ["3 2\n1 2\n1 3", "5 5\n1 4\n3 4\n5 4\n3 2\n1 5", "10 10\n1 4\n6 8\n2 5\n3 7\n9 4\n5 6\n3 4\n8 10\n8 9\n1 10"], "sample_outputs": ["1 2 3", "1 4 3 2 5", "1 4 3 7 9 8 6 5 2 10"], "notes": "NoteIn the first sample, Bob's optimal wandering path could be $$$1 \\rightarrow 2 \\rightarrow 1 \\rightarrow 3$$$. Therefore, Bob will obtain the sequence $$$\\{1, 2, 3\\}$$$, which is the lexicographically smallest one.In the second sample, Bob's optimal wandering path could be $$$1 \\rightarrow 4 \\rightarrow 3 \\rightarrow 2 \\rightarrow 3 \\rightarrow 4 \\rightarrow 1 \\rightarrow 5$$$. Therefore, Bob will obtain the sequence $$$\\{1, 4, 3, 2, 5\\}$$$, which is the lexicographically smallest one."}, "positive_code": [{"source_code": "DBG = !true\nn,m = gets.split.map{|z| z.to_i}\narcs = Array.new(n).map{[]}\nfor i in 0...m\n x,y = gets.split.map{|z| z.to_i}\n next if x==y # ignore self loops\n x -= 1\n y -= 1\n arcs[x] << y\n arcs[y] << x\n #a,b = gets.split\n #a = a.to_i\n #b = b.to_i\nend\nfor i in 0...n\n arcs[i].uniq!\nend\n\nif DBG\n print arcs\n puts \"\"\nend\n\ndef pushh(h, v)\n if h.empty?\n h << v\n return\n elsif h.size == 1\n if v >= h[0]\n h << v\n else\n h.unshift(v)\n end\n return\n end\n puts \"pushh #{v} called\" if DBG\n mn = 0\n mx = h.size-1\n if v <= h[0]\n h[0,0] = v if v < h[0]\n elsif h[mx] <= v\n h << v if h[mx] < v\n else\n while mn+1 < mx\n mid = (mn+mx)/2\n if h[mid] < v\n mn = mid\n else\n mx = mid\n end\n end\n h[mx,0] = v if h[mn] < v && v < h[mx]\n end\n\nend\n\ndone = Array.new(n, false)\nseq = [1]\ndone[0] = true\nconn = arcs[0].sort\nfor x in conn\n done[x] = true\nend\nfor i in 1...n\n if DBG\n print \"i #{i} conn/done \"\n print conn\n print done\n puts \"\"\n end\n nxt = conn.shift\n puts \"picked #{nxt}\" if DBG\n #done[nxt] = true\n seq << nxt + 1\n nxcon = arcs[nxt]\n for x in nxcon\n if !done[x]\n done[x] = true\n pushh(conn, x)\n #p = conn.bsearch_index{|z| z>=x}\n #if p.nil?\n # conn << x\n # puts \"push x #{x} to tail\" if DBG\n #elsif conn[p] != x\n # conn[p,0] = x\n # puts \"push x #{x} to p #{p}\" if DBG\n #end\n end\n end\nend\n\nfor i in 0...n\n print \"#{seq[i]} \"\nend\nputs \"\"\n"}], "negative_code": [{"source_code": "DBG = !true\nn,m = gets.split.map{|z| z.to_i}\narcs = Array.new(n).map{[]}\nfor i in 0...m\n x,y = gets.split.map{|z| z.to_i}\n x -= 1\n y -= 1\n arcs[x] << y\n arcs[y] << x\n #a,b = gets.split\n #a = a.to_i\n #b = b.to_i\nend\nfor i in 0...n\n arcs[i].uniq!\nend\n\nif DBG\n print arcs\n puts \"\"\nend\n\ndef pushh(h, v)\n if h.empty?\n h << v\n return\n elsif h.size == 1\n if v >= h[0]\n h << v\n else\n h.unshift(v)\n end\n return\n end\n puts \"pushh #{v} called\" if DBG\n mn = 0\n mx = h.size-1\n if v <= h[0]\n h[0,0] = v if v < h[0]\n elsif h[mx] <= v\n h << v if h[mx] < v\n else\n while mn+1 < mx\n mid = (mn+mx)/2\n if h[mid] < v\n mn = mid\n else\n mx = mid\n end\n end\n h[mx,0] = v if h[mn] < v && v < h[mx]\n end\n\nend\n\ndone = Array.new(n, false)\nseq = [1]\ndone[0] = true\nconn = arcs[0].sort\nfor x in conn\n done[x] = true\nend\nfor i in 1...n\n if DBG\n print \"i #{i} conn/done \"\n print conn\n print done\n puts \"\"\n end\n nxt = conn.shift\n puts \"picked #{nxt}\" if DBG\n #done[nxt] = true\n seq << nxt + 1\n nxcon = arcs[nxt]\n for x in nxcon\n if !done[x]\n done[x] = true\n pushh(conn, x)\n #p = conn.bsearch_index{|z| z>=x}\n #if p.nil?\n # conn << x\n # puts \"push x #{x} to tail\" if DBG\n #elsif conn[p] != x\n # conn[p,0] = x\n # puts \"push x #{x} to p #{p}\" if DBG\n #end\n end\n end\nend\n\nfor i in 0...n\n print \"#{seq[i]} \"\nend\nputs \"\"\n"}], "src_uid": "157630371c4f6c3bcc6355d96c86a626"} {"nl": {"description": "Vasya has gotten interested in programming contests in TCMCF+++ rules. On the contest n problems were suggested and every problem had a cost — a certain integral number of points (perhaps, negative or even equal to zero). According to TCMCF+++ rules, only accepted problems can earn points and the overall number of points of a contestant was equal to the product of the costs of all the problems he/she had completed. If a person didn't solve anything, then he/she didn't even appear in final standings and wasn't considered as participant. Vasya understood that to get the maximal number of points it is not always useful to solve all the problems. Unfortunately, he understood it only after the contest was finished. Now he asks you to help him: find out what problems he had to solve to earn the maximal number of points.", "input_spec": "The first line contains an integer n (1 ≤ n ≤ 100) — the number of the suggested problems. The next line contains n space-separated integers ci ( - 100 ≤ ci ≤ 100) — the cost of the i-th task. The tasks' costs may coinсide.", "output_spec": "Print space-separated the costs of the problems that needed to be solved to get the maximal possible number of points. Do not forget, please, that it was necessary to solve at least one problem. If there are several solutions to that problem, print any of them.", "sample_inputs": ["5\n1 2 -3 3 3", "13\n100 100 100 100 100 100 100 100 100 100 100 100 100", "4\n-2 -2 -2 -2"], "sample_outputs": ["3 1 2 3", "100 100 100 100 100 100 100 100 100 100 100 100 100", "-2 -2 -2 -2"], "notes": null}, "positive_code": [{"source_code": "gets\na=gets.split.map &:to_i\nx=a.delete 0\nputs(if a.size<1\n\t0\nelsif a.size<2\n\tx ? [a[0],x].max : a[0]\nelsif a.count{|_|_<0}%2<1\n\ta*\" \"\nelse\n\ta.delete_at a.each_with_index.select{|v,i|v<0}.sort[-1][1]\n\ta*\" \"\nend)"}, {"source_code": "n = gets.chop.to_i\na = gets.chop.split(/ /).map!{|x| x.to_i}\npa = Array.new\nma = Array.new\nbzero = false\nfor i in 0..n-1\n\tif a[i] == 0\n\t\tbzero = true \n\telsif a[i] > 0\n\t\tpa.push(a[i])\n\telse\n\t\tma.push(a[i])\n\tend\nend\nma.sort!{|a,b| a <=> b }\nmac = (ma.length / 2)*2\nret = \"\"\nfor i in 0..mac-1\n\tret << \" \" if ret.length > 0\n\tret << ma[i].to_s\nend\nfor i in 0..pa.length-1\n\tret << \" \" if ret.length > 0\n\tret << pa[i].to_s\nend\n\nif n == 1\n\tputs a[0]\nelsif ret == \"\" && bzero\n\tputs \"0\"\nelse\n\tputs ret\nend\n"}, {"source_code": "n = gets.to_i\ncosts = gets.split.map { |v| v.to_i }\n\npositive = costs.select { |v| v > 0 }.sort\nnegative = costs.select { |v| v < 0 }.sort\n\nnegative.pop if negative.size.odd?\n\nresult = negative + positive\nresult = Array(costs.max) if result.empty?\n\nputs result.join(' ')\n"}], "negative_code": [{"source_code": "gets\na=gets.split.map &:to_i\na.delete 0\nputs(if a.size<1\n\t0\nelsif a.size<2\n\ta[0]\nelsif a.count{|_|_<0}%2<1\n\ta*\" \"\nelse\n\ta.delete_at a.each_with_index.select{|v,i|v<0}.sort[-1][1]\n\ta*\" \"\nend)"}, {"source_code": "n = gets.chop.to_i\na = gets.chop.split(/ /).map!{|x| x.to_i}\npa = Array.new\nma = Array.new\nbzero = false\nfor i in 0..n-1\n\tbzero = true if a[i] == 0\n\tif a[i] > 0\n\t\tpa.push(a[i])\n\telse\n\t\tma.push(a[i])\n\tend\nend\nma.sort!{|a,b| b <=> a }\nmac = (ma.length / 2)*2\nret = \"\"\nfor i in 0..mac-1\n\tret << \" \" if i > 0\n\tret << ma[i].to_s\nend\nfor i in 0..pa.length-1\n\tret << \" \" if i > 0\n\tret << pa[i].to_s\nend\n\nif n == 1\n\tputs a[0]\nelsif ret == \"\" && bzero\n\tputs \"0\"\nelse\n\tputs ret\nend\n"}, {"source_code": "n = gets.chop.to_i\na = gets.chop.split(/ /).map!{|x| x.to_i}\npa = Array.new\nma = Array.new\nbzero = false\nfor i in 0..n-1\n\tbzero = true if a[i] == 0\n\tif a[i] > 0\n\t\tpa.push(a[i])\n\telse\n\t\tma.push(a[i])\n\tend\nend\nma.sort!{|a,b| a <=> b }\nmac = (ma.length / 2)*2\nret = \"\"\nfor i in 0..mac-1\n\tret << \" \" if ret.length > 0\n\tret << ma[i].to_s\nend\nfor i in 0..pa.length-1\n\tret << \" \" if ret.length > 0\n\tret << pa[i].to_s\nend\n\nif n == 1\n\tputs a[0]\nelsif ret == \"\" && bzero\n\tputs \"0\"\nelse\n\tputs ret\nend\n"}, {"source_code": "n = gets.chop.to_i\na = gets.chop.split(/ /).map!{|x| x.to_i}\npa = Array.new\nma = Array.new\nbzero = false\nfor i in 0..n-1\n\tbzero = true if a[i] == 0\n\tif a[i] > 0\n\t\tpa.push(a[i])\n\telse\n\t\tma.push(a[i])\n\tend\nend\nma.sort!{|a,b| b <=> a }\nmac = (ma.length / 2)*2\nret = \"\"\nfor i in 0..mac-1\n\tret << \" \" if i > 0\n\tret << ma[i].to_s\nend\nfor i in 0..pa.length-1\n\tret << \" \" if i > 0\n\tret << pa[i].to_s\nend\n\nif ret == \"\" && bzero\n\tputs \"0\"\nelse\n\tputs ret\nend\n"}], "src_uid": "b11644953bdd1b92eb1b18c339a268a1"} {"nl": {"description": "You are given an array $$$A$$$, consisting of $$$n$$$ positive integers $$$a_1, a_2, \\dots, a_n$$$, and an array $$$B$$$, consisting of $$$m$$$ positive integers $$$b_1, b_2, \\dots, b_m$$$. Choose some element $$$a$$$ of $$$A$$$ and some element $$$b$$$ of $$$B$$$ such that $$$a+b$$$ doesn't belong to $$$A$$$ and doesn't belong to $$$B$$$. For example, if $$$A = [2, 1, 7]$$$ and $$$B = [1, 3, 4]$$$, we can choose $$$1$$$ from $$$A$$$ and $$$4$$$ from $$$B$$$, as number $$$5 = 1 + 4$$$ doesn't belong to $$$A$$$ and doesn't belong to $$$B$$$. However, we can't choose $$$2$$$ from $$$A$$$ and $$$1$$$ from $$$B$$$, as $$$3 = 2 + 1$$$ belongs to $$$B$$$.It can be shown that such a pair exists. If there are multiple answers, print any.Choose and print any such two numbers.", "input_spec": "The first line contains one integer $$$n$$$ ($$$1\\le n \\le 100$$$) — the number of elements of $$$A$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 200$$$) — the elements of $$$A$$$. The third line contains one integer $$$m$$$ ($$$1\\le m \\le 100$$$) — the number of elements of $$$B$$$. The fourth line contains $$$m$$$ different integers $$$b_1, b_2, \\dots, b_m$$$ ($$$1 \\le b_i \\le 200$$$) — the elements of $$$B$$$. It can be shown that the answer always exists.", "output_spec": "Output two numbers $$$a$$$ and $$$b$$$ such that $$$a$$$ belongs to $$$A$$$, $$$b$$$ belongs to $$$B$$$, but $$$a+b$$$ doesn't belong to nor $$$A$$$ neither $$$B$$$. If there are multiple answers, print any.", "sample_inputs": ["1\n20\n2\n10 20", "3\n3 2 2\n5\n1 5 7 7 9", "4\n1 3 5 7\n4\n7 5 3 1"], "sample_outputs": ["20 20", "3 1", "1 1"], "notes": "NoteIn the first example, we can choose $$$20$$$ from array $$$[20]$$$ and $$$20$$$ from array $$$[10, 20]$$$. Number $$$40 = 20 + 20$$$ doesn't belong to any of those arrays. However, it is possible to choose $$$10$$$ from the second array too.In the second example, we can choose $$$3$$$ from array $$$[3, 2, 2]$$$ and $$$1$$$ from array $$$[1, 5, 7, 7, 9]$$$. Number $$$4 = 3 + 1$$$ doesn't belong to any of those arrays.In the third example, we can choose $$$1$$$ from array $$$[1, 3, 5, 7]$$$ and $$$1$$$ from array $$$[7, 5, 3, 1]$$$. Number $$$2 = 1 + 1$$$ doesn't belong to any of those arrays."}, "positive_code": [{"source_code": "n = readline.to_i\na = readline.split.map(&:to_i)\nm = readline.to_i\nb = readline.split.map(&:to_i)\n\nprintf \"%d %d\\n\", a.max, b.max"}, {"source_code": "a_size = gets.strip.to_i\na_string = gets.strip\n\nb_size = gets.strip.to_i\nb_string = gets.strip\n\na = a_string.split(' ').map(&:to_i)\nb = b_string.split(' ').map(&:to_i)\n\noutput = nil\n\na.each_with_index do |a_element, _a_index|\n b.each_with_index do |b_element, _b_index|\n sum = a_element + b_element\n next if a.include?(sum) || b.include?(sum)\n\n output = \"#{a_element} #{b_element}\"\n break\n end\n\n break unless output.eql?(nil)\nend\n\nputs output\n"}, {"source_code": "n = gets.to_i\nas = gets.split.map(&:to_i)\nm = gets.to_i\nbs = gets.split.map(&:to_i)\n\nputs \"#{as.max} #{bs.max}\""}, {"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\n\nna = gets.to_i\nar = gets.split.map(&:to_i)\nnb = gets.to_i\nbr = gets.split.map(&:to_i)\n\ncatch :found do\n ar.each { |ia|\n br.each { |ib|\n s = ia + ib\n if ar.index(s) == nil && br.index(s) == nil\n puts \"#{ia} #{ib}\"\n throw :found\n end\n }\n }\nend"}, {"source_code": "gets\na = gets.split(' ').map(&:to_i)\ngets\nb = gets.split(' ').map(&:to_i)\nputs a.max.to_s + ' ' + b.max.to_s"}, {"source_code": "n = gets.to_i\naa = gets.split.map(&:to_i).sort\nm = gets.to_i\nbb = gets.split.map(&:to_i).sort\n\nputs \"#{aa.last} #{bb.last}\""}, {"source_code": "n = gets.to_i\na = gets.split.map &:to_i\nm = gets.to_i\nb = gets.split.map &:to_i\nprint a.max, ' ', b.max\n"}], "negative_code": [{"source_code": "gets\na = gets.split(' ').map(&:to_i)\ngets\nb = gets.split(' ').map(&:to_i)\nputs a.max + b.max"}], "src_uid": "ec09b2df4ed3abbca4c47f48f12010ca"} {"nl": {"description": "Given a sequence $$$a_1, a_2, \\ldots, a_n$$$, find the minimum number of elements to remove from the sequence such that after the removal, the sum of every $$$2$$$ consecutive elements is even.", "input_spec": "Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$3 \\le n \\le 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2,\\dots,a_n$$$ ($$$1\\leq a_i\\leq10^9$$$) — elements of the sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.", "output_spec": "For each test case, print a single integer — the minimum number of elements to remove from the sequence such that the sum of every $$$2$$$ consecutive elements is even.", "sample_inputs": ["2\n\n5\n\n2 4 3 6 8\n\n6\n\n3 5 9 7 1 3"], "sample_outputs": ["1\n0"], "notes": "NoteIn the first test case, after removing $$$3$$$, the sequence becomes $$$[2,4,6,8]$$$. The pairs of consecutive elements are $$$\\{[2, 4], [4, 6], [6, 8]\\}$$$. Each consecutive pair has an even sum now. Hence, we only need to remove $$$1$$$ element to satisfy the condition asked.In the second test case, each consecutive pair already has an even sum so we need not remove any element."}, "positive_code": [{"source_code": "t = gets.to_i\n\n(0...t).each do\n n = gets.chomp.to_i\n a = gets.chomp.split(\" \").map(&:to_i)\n\n even_count = a.select(&:even?).count\n odd_count = a.count - even_count\n puts [even_count, odd_count].min\nend\n"}, {"source_code": "gets.to_i.times do\r\nn = gets\r\na = gets.split(' ').map(&:to_i)\r\nputs [a.select{|a|a%2==1}.size, a.select{|a|a%2==0}.size].min\r\nend"}, {"source_code": "gets.to_i.times {\n\tm = gets.to_i\n\tn = gets.split.map(&:to_i).count{|a| a.even?}\n\tp [n, m-n].min\n} "}, {"source_code": "\ndef solution(arr)\n odd, even = 0, 0\n arr.each do |e|\n if e % 2 == 0\n even += 1\n else\n odd += 1\n end\n end\n puts odd < even ? odd : even \nend\n\n\nn = $stdin.gets.to_i\n(0...n).each do |i|\n size = $stdin.gets.to_i \n arr = $stdin.gets.to_s.split(' ').map(&:to_i)\n solution(arr)\nend\n\n\n"}], "negative_code": [{"source_code": "t = gets.to_i\n\n(0...t).each do\n n = gets.chomp.to_i\n a = gets.chomp.split(\" \").map(&:to_i)\n\n k = 0\n idx = 0\n # prev_idx = 0\n while idx < a.size\n next_id = idx + 1\n # puts \"#{prev_idx}-#{idx}-#{a.inspect} - #{k}\"\n x = a[idx]\n y = a[next_id]\n break if y.nil?\n sum = x + y\n\n if sum.even?\n idx += 1\n next\n else\n # if x.even? && y.odd?\n a.delete_at(next_id)\n\n # elsif y.odd?\n # a.delete_at(next_id)\n # idx += 1\n # end\n # idx -= 1\n # if a[idx].odd?\n # a.delete_at(idx)\n # end\n k += 1\n end\n end\n\n puts k\n\nrescue => e\n puts e\n puts a.inspect\n exit(1)\nend\n"}, {"source_code": "t = gets.to_i\n\n(0...t).each do\n n = gets.chomp.to_i\n a = gets.chomp.split(\" \").map(&:to_i)\n\n k = 0\n idx = 0\n # prev_idx = 0\n while idx < a.size\n next_id = idx + 1\n # puts \"#{prev_idx}-#{idx}-#{a.inspect} - #{k}\"\n x = a[idx]\n y = a[next_id]\n break if y.nil?\n sum = x + y\n\n if sum.even?\n idx += 1\n next\n else\n if x.odd?\n a.delete_at(idx)\n\n elsif y.odd?\n a.delete_at(next_id)\n end\n # idx -= 1\n # if a[idx].odd?\n # a.delete_at(idx)\n # end\n k += 1\n end\n end\n\n puts k\n\nrescue => e\n puts e\n puts a.inspect\n exit(1)\nend\n"}, {"source_code": "t = gets.to_i\n\n(0...t).each do\n n = gets.chomp.to_i\n a = gets.chomp.split(\" \").map(&:to_i)\n\n k = 0\n idx = 0\n # prev_idx = 0\n while idx < a.size\n next_id = idx + 1\n # puts \"#{prev_idx}-#{idx}-#{a.inspect} - #{k}\"\n x = a[idx]\n y = a[next_id]\n break if y.nil?\n sum = x + y\n\n if sum.even?\n idx += 1\n next\n else\n if x.odd?\n a.delete_at(idx)\n # idx -= 1\n elsif y.odd?\n a.delete_at(next_id)\n end\n\n # if a[idx].odd?\n # a.delete_at(idx)\n # end\n k += 1\n end\n end\n\n puts k\n\nrescue => e\n puts e\n puts a.inspect\n exit(1)\nend\n"}, {"source_code": "t = gets.to_i\n\n(0...t).each do\n n = gets.chomp.to_i\n a = gets.chomp.split(\" \").map(&:to_i)\n\n k = 0\n idx = 1\n while idx < a.size\n prev_idx = idx - 1\n # puts \"#{prev_idx}-#{idx}-#{a.size}\"\n sum = a[prev_idx] + a[idx]\n\n if sum.even?\n idx += 1\n next\n else\n # if a[prev_idx].odd?\n # a.delete_at(prev_idx)\n # elsif a[idx].odd?\n # a.delete_at(idx)\n # end\n # if a[idx].odd?\n a.delete_at(idx)\n # end\n k += 1\n end\n end\n\n puts k\n\n# rescue => e\n# puts a.inspect\n# exit(1)\nend\n"}, {"source_code": "t = gets.to_i\n\n(0...t).each do\n n = gets.chomp.to_i\n a = gets.chomp.split(\" \").map(&:to_i)\n\n k = 0\n idx = 1\n while idx < a.size\n prev_idx = idx - 1\n # puts \"#{prev_idx}-#{idx}-#{a.size}\"\n sum = a[prev_idx] + a[idx]\n\n if sum.even?\n idx += 2\n next\n else\n if a[prev_idx].odd?\n a.delete_at(prev_idx)\n elsif a[idx].odd?\n a.delete_at(idx)\n end\n k += 1\n end\n end\n\n puts k\n\n# rescue => e\n# puts a.inspect\n# exit(1)\nend\n"}, {"source_code": "t = gets.to_i\n\n(0...t).each do\n n = gets.chomp.to_i\n a = gets.chomp.split(\" \").map(&:to_i)\n\n k = 0\n idx = 1\n while idx <= a.size\n prev_idx = idx - 1\n sum = a[prev_idx] + a[idx]\n\n if sum.even?\n idx += 2\n next\n else\n if a[prev_idx].odd?\n a.delete_at(prev_idx)\n elsif a[idx].odd?\n a.delete_at(idx)\n end\n k += 1\n end\n end\n\n puts k\n\nrescue => e\n puts e\nend\n"}], "src_uid": "4b33db3950303b8993812cb265fa9819"} {"nl": {"description": "Allen is hosting a formal dinner party. $$$2n$$$ people come to the event in $$$n$$$ pairs (couples). After a night of fun, Allen wants to line everyone up for a final picture. The $$$2n$$$ people line up, but Allen doesn't like the ordering. Allen prefers if each pair occupies adjacent positions in the line, as this makes the picture more aesthetic.Help Allen find the minimum number of swaps of adjacent positions he must perform to make it so that each couple occupies adjacent positions in the line.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$1 \\le n \\le 100$$$), the number of pairs of people. The second line contains $$$2n$$$ integers $$$a_1, a_2, \\dots, a_{2n}$$$. For each $$$i$$$ with $$$1 \\le i \\le n$$$, $$$i$$$ appears exactly twice. If $$$a_j = a_k = i$$$, that means that the $$$j$$$-th and $$$k$$$-th people in the line form a couple.", "output_spec": "Output a single integer, representing the minimum number of adjacent swaps needed to line the people up so that each pair occupies adjacent positions.", "sample_inputs": ["4\n1 1 2 3 3 2 4 4", "3\n1 1 2 2 3 3", "3\n3 1 2 3 1 2"], "sample_outputs": ["2", "0", "3"], "notes": "NoteIn the first sample case, we can transform $$$1 1 2 3 3 2 4 4 \\rightarrow 1 1 2 3 2 3 4 4 \\rightarrow 1 1 2 2 3 3 4 4$$$ in two steps. Note that the sequence $$$1 1 2 3 3 2 4 4 \\rightarrow 1 1 3 2 3 2 4 4 \\rightarrow 1 1 3 3 2 2 4 4$$$ also works in the same number of steps.The second sample case already satisfies the constraints; therefore we need $$$0$$$ swaps."}, "positive_code": [{"source_code": "N = gets.to_i\nas = gets.split.map(&:to_i)\nans = 0\nwhile as.length > 0\n a = as.shift\n ans += as.index(a)\n as.delete(a)\nend\nputs ans"}, {"source_code": "n = gets.to_i\nary = gets.strip.split.map(&:to_i)\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nsz = ary.size\nans = 0\n0.upto(n-1).each do |i|\n ai = i*2\n e = ary[ai]\n eni = ai + 1\n (ai+1).upto(sz-1).each do |j|\n if ary[j] == e\n eni = j\n break\n end\n end\n eni.downto(ai+2).each do |j|\n ary[j] = ary[j-1]\n ary[j-1] = e\n ans += 1\n end\nend\nputs ans"}], "negative_code": [], "src_uid": "194bc63b192a4e24220b36984901fb3b"} {"nl": {"description": "The New Year is coming! That's why many people today are busy preparing New Year presents. Vasily the Programmer is no exception.Vasily knows that the best present is (no, it's not a contest) money. He's put n empty wallets from left to right in a row and decided how much money to put in what wallet. Vasily decided to put ai coins to the i-th wallet from the left.Vasily is a very busy man, so the money are sorted into the bags by his robot. Initially, the robot stands by the leftmost wallet in the row. The robot can follow instructions of three types: go to the wallet that is to the left of the current one (if such wallet exists), go to the wallet that is to the right of the current one (if such wallet exists), put a coin to the current wallet. Due to some technical malfunctions the robot cannot follow two \"put a coin\" instructions in a row.Vasily doesn't want to wait for long, so he wants to write a program for the robot that contains at most 106 operations (not necessarily minimum in length) the robot can use to put coins into the wallets. Help him.", "input_spec": "The first line contains integer n (2 ≤ n ≤ 300) — the number of wallets. The next line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 300). It is guaranteed that at least one ai is positive.", "output_spec": "Print the sequence that consists of k (1 ≤ k ≤ 106) characters, each of them equals: \"L\", \"R\" or \"P\". Each character of the sequence is an instruction to the robot. Character \"L\" orders to move to the left, character \"R\" orders to move to the right, character \"P\" orders the robot to put a coin in the wallet. The robot is not allowed to go beyond the wallet line. In other words, you cannot give instructions \"L\" if the robot is at wallet 1, or \"R\" at wallet n. As a result of the performed operations, the i-th wallet from the left must contain exactly ai coins. If there are multiple answers, you can print any of them.", "sample_inputs": ["2\n1 2", "4\n0 2 0 2"], "sample_outputs": ["PRPLRP", "RPRRPLLPLRRRP"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\nop = Hash.new \"PLR\"\nop[0] = \"PRL\"\ngets.split(' ',n).map(&:to_i).each_with_index do |x,id|\n if id == 0\n print \"PRL\" * x\n else\n print \"R\"\n print \"PLR\" * x\n end\nend"}, {"source_code": "=begin\nDear online judge:\nI've wasted time reading the problem and trying to figure out the solution\nIf you insist on the penalty system, please don't include the time of debugging\nI hope my code compile and get accepted. KEE O.o\n ____________\n / __ \\\n / __ | | \\\n / |__| | | \\\n ( |__| )\n \\ /\n \\ ___ /\n \\____________/\n=end\n\nclass Solve\n def solve()\n $n = Integer(gets)\n $list = gets().split()\n $count = 0\n $n -= 1\n for $i in 0..$n do\n if $list[$i] != \"0\"\n $count += Integer($list[$i])\n end\n end\n $res = \"\"\n $dir = 1\n $i = 0\n $cur = Array.new($n + 1,0)\n while $count > 0 do\n if $cur[$i] < Integer($list[$i])\n $cur[$i] += 1\n $count -= 1\n $res<<'P'\n end\n if $count > 0\n if $i == $n\n $dir = -1\n else\n if $i == 0\n $dir = 1\n end\n end\n if $dir == 1\n $res<<'R'\n end\n if $dir == -1\n $res<<'L'\n end\n end\n $i += $dir\n end\n puts $res\n end\nend\n\n\n\nSolve.new.solve"}, {"source_code": "RIGHT = 1\nLEFT = 0\n\nwallets_count, *wallets = STDIN.read.split\n\nwallets.map!(&:to_i)\nwallets_count = wallets_count.to_i\n\nindex = 0\ndirection = RIGHT\ncoins_to_give = wallets.inject(0) { |sum, value| sum + value }\n\nuntil coins_to_give.zero?\n if wallets[index] > 0\n print :P\n wallets[index] -= 1\n coins_to_give -= 1\n break if coins_to_give.zero?\n end\n\n if (direction == RIGHT && index < wallets_count - 1) || (direction == LEFT && index == 0)\n print :R\n index += 1\n direction = RIGHT\n else\n print :L\n index -= 1\n direction = LEFT\n end\nend\n\nprint \"\\n\"\n"}, {"source_code": "inA = gets\ninB = gets\ncount = inA.to_i\n\ncounts = inB.split\ncounts.map! {|x| x.to_i}\n\nresult = \"\"\nposition = 0\nforward = true\nlastPuted = 0\nwhile true\n\tif(counts[position] > 0)\n\t\tresult << 'P'\n\t\tlastPuted = 0\n\t\tcounts[position] -= 1\n\telse\n\t\tlastPuted += 1\n\tend\n\n\tif(forward && (position == counts.length() -1))\n\t\tforward = false\n\telsif ( (!forward) && position == 0)\n\t\tforward = true\n\tend\n\n\tif(forward)\n\t\tposition += 1\n\t\tresult << 'R'\n\telse\n\t\tposition -= 1\n\t\tresult << 'L'\n\tend\n\n\tif(lastPuted > (counts.length*2 + 2))\n\t\tputs result.slice(0, result.length - lastPuted - 1)\n\t\tbreak\n\tend\nend\n\n"}, {"source_code": "n = Integer(gets)\ninput = gets.chomp\nnums = input.split\na = Array.new\nfor i in 0...n\n a[i] = Integer(nums[i])\nend\nsov = Array.new\nif a[0] != 0\n sov[0] = \"PRL\" * (a[0]-1) + \"P\"\nelse\n sov[0] = nil\nend\nb = Array.new\nfor i in 1...n\n if a[i] >= 1\n b[i] = \"RPL\" * (a[i]-1) + \"RP\"\n else a[i] == 0\n b[i] = \"R\"\n end\n\n sov.push(b[i])\nend\nprint sov.join"}, {"source_code": "n=gets.to_i\nd=gets.split.map(&:to_i)\nans=''\nn.times do |i|\n if i == 0\n ans+= 'RLP'*d[i]\n else\n ans+= 'LRP'*d[i]\n end\n ans+='R'\nend\nputs ans[0..-2]"}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\nprint 'PRL'*a[0]\n(1..n-1).each{|i|print 'R','PLR'*a[i]}"}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\nx=0\ns=''\nloop do\n (a[x]-=1;s<<'P')if a[x]>0\n if x==0\n x+=1\n s<<'R'\n elsif a[x-1]>0 || a[x]>0\n x-=1\n s<<'L'\n elsif x==n-1\n if a[x]>0\n x-=1\n s<<'L'\n else\n break\n end\n elsif\n x+=1\n s<<'R'\n end\nend\nputs s"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nprint 'PRL' * a[0]\nc = false\nif a[n-1] != 0\na[n-1] -= 1\nc = true\nend\n(1..n-1).each{|i| print 'R', 'PLR' * a[i]}\nif c\nprint 'P'\nend"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nprint 'PRL' * a[0]\n(1..n-1).each{|i| print 'R', 'PLR' * a[i]}"}], "negative_code": [{"source_code": "inA = gets\ninB = gets\ncount = inA.to_i\n\ncounts = inB.split\ncounts.map! {|x| x.to_i}\n\nresult = \"\"\nposition = 0\nforward = true\nlastPuted = 0\nwhile true\n\tif(counts[position] > 0)\n\t\tresult << 'P'\n\t\tlastPuted = 0\n\t\tcounts[position] -= 1\n\telse\n\t\tlastPuted += 1\n\tend\n\n\tif(forward && (position == counts.length() -1))\n\t\tforward = false\n\telsif ( (!forward) && position == 0)\n\t\tforward = true\n\tend\n\n\tif(forward)\n\t\tposition += 1\n\t\tresult << 'R'\n\telse\n\t\tposition -= 1\n\t\tresult << 'L'\n\tend\n\n\tif(lastPuted > counts.length)\n\t\tputs result.slice(0, result.length - lastPuted - 1)\n\t\tbreak\n\tend\nend\n\n"}, {"source_code": "n = Integer(gets)\ninput = gets.chomp\nnums = input.split\na = Array.new\nfor i in 0...n\n a[i] = Integer(nums[i])\nend\nsov = Array.new\nif a[0] != 0\n sov[0] = \"PRL\" * (a[0]-1) + \"PR\"\nelse\n sov[0] = nil\nend\nb = Array.new\nfor i in 1...n\n b[i] = (\"R\" * i + \"P\" + \"L\" * i) * a[i]\n sov.push(b[i])\nend\nprint sov.join"}, {"source_code": "n = Integer(gets)\ninput = gets.chomp\nnums = input.split\na = Array.new\nfor i in 0...n\n a[i] = Integer(nums[i])\nend\nsov = Array.new\nif a[0] != 0\n sov[0] = \"PRL\" * (a[0]-1) + \"PR\"\nelse\n sov[0] = nil\nend\nb = Array.new\nfor i in 1...n\n if a[i] >= 2\n b[i] = \"PL\" + \"RPL\" * (a[i]-2) + \"RP\"\n else a[i] = 1\n b[i] = \"PL\"\n end\n\n sov.push(b[i])\nend\nprint sov.join"}, {"source_code": "n = Integer(gets)\ninput = gets.chomp\nnums = input.split\na = Array.new\nfor i in 0...n\n a[i] = Integer(nums[i])\nend\nsov = Array.new\nif a[0] != 0\n sov[0] = \"PRL\" * (a[0]-1) + \"PR\"\nelse\n sov[0] = \"R\"\nend\nb = Array.new\nfor i in 1...n\n if a[i] >= 2\n b[i] = \"PL\" + \"RPL\" * (a[i]-2) + \"RP\"\n elsif a[i] == 1\n b[i] = \"PL\"\n else a[i] == 0\n b[i] = \"R\"\n end\n\n sov.push(b[i])\nend\nprint sov.join"}, {"source_code": "n = Integer(gets)\ninput = gets.chomp\nnums = input.split\na = Array.new\nfor i in 0...n\n a[i] = Integer(nums[i])\nend\nsov = Array.new\nsov[0] = \"PRL\" * a[0]\nb = Array.new\nfor i in 1...n\n b[i] = (\"R\" * i + \"P\" + \"L\" * i) * a[i]\n sov.push(b[i])\nend\nprint sov.join"}, {"source_code": "n = Integer(gets)\ninput = gets.chomp\nnums = input.split\na = Array.new\nfor i in 0...n\n a[i] = Integer(nums[i])\nend\nsov = Array.new\nif a[0] != 0\n sov[0] = \"PRL\" * (a[0]-1) + \"PR\"\nelse\n sov[0] = \"R\"\nend\nb = Array.new\nfor i in 1...n\n if a[i] >= 2\n b[i] = \"PL\" + \"RPL\" * (a[i]-2) + \"RP\"\n elsif a[i] == 1\n b[i] = \"PL\"\n else a[i] == 0\n b[i] = \"RR\"\n end\n\n sov.push(b[i])\nend\nprint sov.join"}, {"source_code": "n = Integer(gets)\ninput = gets.chomp\nnums = input.split\na = Array.new\nfor i in 0...n\n a[i] = Integer(nums[i])\nend\nsov = Array.new\nif a[0] != 0\n sov[0] = \"PRL\" * (a[0]-1) + \"PR\"\nelse\n sov[0] = \"R\"\nend\nb = Array.new\nfor i in 1...n\n if a[i] >= 2\n b[i] = \"PL\" + \"RPL\" * (a[i]-2) + \"RP\"\n else a[i] = 1\n b[i] = \"PL\"\n end\n\n sov.push(b[i])\nend\nprint sov.join"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nprint 'PRL' * a[0]\nif a[n-1] != 0\na[n-1] -= 1\nend\n(1..n-1).each{|i| print 'R', 'PLR' * a[i]}\nprint 'P'"}], "src_uid": "50e88225d8b081d63eebe446f48057f4"} {"nl": {"description": "One popular blog site edits the uploaded photos like this. It cuts a rectangular area out of them so that the ratio of height to width (i.e. the height / width quotient) can vary from 0.8 to 1.25 inclusively. Besides, at least one side of the cut area should have a size, equal to some power of number 2 (2x for some integer x). If those rules don't indicate the size of the cut are clearly, then the way with which the cut part possesses the largest area is chosen. Of course, both sides of the cut area should be integer. If there are several answers to this problem, you should choose the answer with the maximal height.", "input_spec": "The first line contains a pair of integers h and w (1 ≤ h, w ≤ 109) which are the height and width of the uploaded photo in pixels.", "output_spec": "Print two integers which are the height and width of the cut area.", "sample_inputs": ["2 1", "2 2", "5 5"], "sample_outputs": ["1 1", "2 2", "5 4"], "notes": null}, "positive_code": [{"source_code": "def solve(x, y)\n m = Math.log2(x).to_i\n n = Math.log2(y).to_i\n if n == m\n return 2 ** n, [y, (1.25*2**n).to_i].min\n elsif n == m + 1\n if x > (2 ** n / 1.25).to_i\n return (2 ** n / 1.25).to_i, 2 ** n\n else\n return 2 ** m, (1.25 * 2 ** m).to_i\n end\n else\n return 2 ** m, (1.25 * 2 ** m).to_i\n end\nend\n\ny, x = gets.split.map(&:to_i)\nx0, y0 = x, y\nswap = false\nif x > y\n x, y = y, x\n swap = true\nend\n\np, q = solve(x, y)\nif swap\n p, q = q, p\nend\nif p > q and (p <= y0 and q <= x0)\n p, q = q, p\nend\nprintf(\"%d %d\\n\", q, p)\n"}, {"source_code": "h,w=gets.split.map &:to_i;a,b=[[h,w],[w,h]].map{|a,b|2**Math.log2([a,b*5/4].min).to_i};c,e=[h,b*5/4].min,[w,a*5/4].min;a,e=c,b if c*b>a*e||c*b==a*e&&c>a;$><a*e||c*b==a*e&&c>a;$><a*e||c*b==a*e&&c>a;$><a*e||c*b==a*e&&c>a;$><a*e||c*b==a*e&&c>a ?[c,b]:[a,e])"}, {"source_code": "h,w=gets.split.map &:to_i;a,b=[[h,w],[w,h]].map{|a,b|2**Math.log2([a,b*5/4].min).to_i};c,e=[h,b*5/4].min,[w,a*5/4].min;a,e=c,b if c*b>a*e||c*b==a*e&&c>a;$><a*e||c*b==a*e&&c>a;$><a*e||c*b==a*e&&c>a;$><a*e||c*b==a*e&&c>a;$><a*e||c*b==a*e&&c>a;$><a*e||c*b==a*e&&c>a;$><a*e||c*b==a*e&&c>a;$><a*e||c*b==a*e&&c>a;$><a*e||c*b==a*e&&c>a;$>< (2 ** n / 1.25).to_i\n return (2 ** n / 1.25).to_i, 2 ** n\n else\n return 2 ** m, (1.25 * 2 ** m).to_i\n end\n else\n return 2 ** m, (1.25 * 2 ** m).to_i\n end\nend\n\ny, x = gets.split.map(&:to_i)\nswap = false\nif x > y\n x, y = y, x\n swap = true\nend\n\np, q = solve(x, y)\nif swap\n p, q = q, p\nend\nprintf(\"%d %d\\n\", q, p)\n"}, {"source_code": "def solve(x, y)\n m = Math.log2(x).to_i\n n = Math.log2(y).to_i\n if n == m\n return 2 ** n, [y, (1.25*x).to_i].min\n elsif n == m + 1\n if x > (2 ** n / 1.25).to_i\n return (2 ** n / 1.25).to_i, 2 ** n\n else\n return 2 ** m, (1.25 * 2 ** m).to_i\n end\n else\n return 2 ** m, (1.25 * 2 ** m).to_i\n end\nend\n\ny, x = gets.split.map(&:to_i)\nswap = false\nif x > y\n x, y = y, x\n swap = true\nend\n\np, q = solve(x, y)\nif swap\n p, q = q, p\nend\nprintf(\"%d %d\\n\", q, p)\n"}, {"source_code": "h,w=gets.split.map &:to_i;a,b=[[h,w],[w,h]].map{|(a,b)|2**Math.log2([a,b*5/4].min).to_i};c,e=[h,b*5/4].min,[w,a*5/4].min;c,b=a,e if c*b>a*e||c*b==a*e&&c>a;$>< n\n (p*p).step(n, p){ |m| all_nums[m] = nil }\n end\n all_nums.compact\nend\n\namount = 1000000\nprimes = prime(amount)\n\nt = {}\nprimes.map{|p| t[p*p]=true }\n\ncol = gets.to_i\narr = gets.split(\" \").map{|p| p.to_i }\n\n(0..col-1).each do |i|\n\tif t[arr[i]] then\n\t\tputs \"YES\"\n\telse\n\t\tputs \"NO\"\n\tend\nend\n\n"}, {"source_code": "require 'prime'\n\nc=(0..10**6).map{0}\nc[0]=1\nc[1]=1\nb=2\nwhile b*b<=(10**6)\n\tif c[b]==0 then\n\t\tb2=b+b\n\t\twhile b2<=10**6\n\t\t\tc[b2]=1\n\t\t\tb2=b2+b\n\t\tend\n\tend\n\tb=b+1\nend\ngets\nx=gets.split.map{|e| e.to_i}\n\nx.each{|e|\n\tm=(e**0.5).to_i\n\tif m*m==e && c[m]==0 then\n\t\tputs \"YES\"\n\telse\n\t\tputs \"NO\"\n\tend\n}\n\n"}, {"source_code": "a = Array.new(1000001,0) \na[1] = -1\ni = 3\nwhile i < 1000\n\tif a[i] == 0\n\t\tj = i+i\n\t\twhile j < 1000000\n\t\t\ta[j] = -1\n\t\t\tj += i\n\t\tend\n\tend\n\ti += 2\nend\nn = gets.to_i\ngets.split.map(&:to_i).each do |i|\n\tx = Math.sqrt(i)\n\tif x % 1 == 0\n\t\tif x % 2 == 0 and x != 2\n\t\t\tputs 'NO'\n\t\telse\n\t\t\tif a[x] == -1\n\t\t\t\tputs 'NO'\n\t\t\telse\n\t\t\t\tputs 'YES'\n\t\t\tend \n\t\tend\n\n\telse\n\t\tputs 'NO'\n\tend\nend\n"}, {"source_code": "#!/usr/bin/ruby\n\nN = Integer gets.chomp\nnumbers = gets.chomp.split\nfor i in 0...N do\n\tnumbers[i] = Integer numbers[i]\nend\n\nX = 1000000\nflags = Array.new(X+1)\nfor i in 0..X do\n\tflags[i] = true\nend\nflags[0] = flags[1] = false\nfor i in 2..X do\n\tif flags[i]\n\t\tj = i * i\n\t\twhile (j <= X) do\n\t\t\tflags[j] = false\n\t\t\tj += i;\n\t\tend\n\tend\nend\n\nfor i in numbers do\n\tj = Integer Math.sqrt(i)\n\tif flags[j] and j * j == i\n\t\tprint \"YES\\n\"\n\telse\n\t\tprint \"NO\\n\"\n\tend\nend\n\n\n\n\n\n"}, {"source_code": "def generate_prime(n)\n\tprimes = (0..n).to_a\n\tprimes[0] = nil\n\tprimes[1] = nil\n\t(2..Math.sqrt(n)).each do |i|\n\t\tif primes[i]\n\t\t\t(i**2..n).step(i).each do |j|\n\t\t\t\tprimes[j] = nil\n\t\t\tend\n\t\tend\n\tend\t\n\tprimes.compact\nend\n\n\nn = gets.to_i\narr = gets.split(\" \").map(&:to_i)\n@primes = generate_prime(10 ** 6).map{|x| x *x}\ntrue_arr = @primes & arr\n\ntrue_hash = Hash[true_arr.map{|x| [x,true]}]\n#d puts \"#{true_hash}\"\nres = []\narr.each do |a|\n\tres << (true_hash[a] ? \"YES\" : \"NO\")\nend\n\nres.each do |i|\n\tputs i\nend"}, {"source_code": "def Eratosthenes2(n)\n\tl = Array 0..n\n\t\n\tl[0] = l[1] = nil\n\t\n\tfor i in (2..n ** (1.0 / 2))\n\t\tif l[i]\n\t\t\tj = i ** 2\n\t\t\twhile j <= n\n\t\t\t\tl[j] = nil\n\t\t\t\tj += i\n\t\t\tend\n\t\tend\n\tend\n\t\n\treturn l.compact.map { |x| x ** 2 }\nend\n\nl = Eratosthenes2(1000000)\n\nclass Array\n\tdef bs(x)\n\t\t\n\t\ts = 0\n\t\te = size - 1\n\t\t\n\t\twhile s <= e\n\t\t\tt = at((s + e) / 2)\n\t\t\t\n\t\t\tif x < t\n\t\t\t\te = (s + e) / 2 - 1\n\t\t\telsif x > t\n\t\t\t\ts = (s + e) / 2 + 1\n\t\t\telse\n\t\t\t\treturn t\n\t\t\tend\n\t\tend\n\t\t\n\t\treturn nil\n\tend\nend\n\ngets\ngets.split.each { |x| puts (l.bs(x.to_i) != nil) ? \"YES\" : \"NO\" }"}, {"source_code": "PRIME_LIMIT = Math.sqrt(10**12).to_i + 1\n@prime_numbers = Array.new(PRIME_LIMIT, true)\n\ndef sieve\n @prime_numbers[1] = false\n i = 2\n while i*i < PRIME_LIMIT do\n if @prime_numbers[i] != false\n j = 2 * i\n while j < PRIME_LIMIT do\n @prime_numbers[j] = false\n j += i\n end\n end\n i+= 1\n end\nend\n\nsieve()\n\ngets\ngets\nvalues = $_.split.map(&:to_i)\nvalues.each do |value|\n svalue = Math.sqrt(value).to_i\n if (svalue * svalue == value && @prime_numbers[svalue] == true && value != 1)\n puts \"YES\"\n else\n puts \"NO\"\n end\nend\n"}, {"source_code": "require 'set'\n\ntprimes = Set.new\nsieve = Array.new(1000001,true)\nsieve[0] = false\nsieve[1] = false\n(2..1000000).each do |p|\n next if sieve[p] == false\n i = p*p\n tprimes.add(i)\n while(i<=1000000)\n sieve[i] = false\n i += p\n end\nend\n\ngets.to_i\ngets.split(/\\s+/).each do |x|\n puts tprimes.member?(x.to_i) ? \"YES\" : \"NO\"\nend\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nc = Array.new(1000000, true)\nc[1] = false\n2.upto(1000) do |i|\n if c[i]\n (i * i).step(1000000, i){|j| c[j] = false}\n end\nend\na.each do |x|\n m = Math.sqrt(x).round\n if m * m == x && c[m]\n puts 'YES'\n else\n puts 'NO'\n end\nend\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nc = Array.new(1000000, true)\nc[1] = false\n2.upto(1000000) do |i|\n if c[i]\n (i + i).step(1000000, i){|j| c[j] = false}\n end\nend\na.each do |x|\n m = Math.sqrt(x)\n if m.ceil == m.to_i && c[m]\n puts 'YES'\n else\n puts 'NO'\n end\nend\n"}, {"source_code": "require 'set'\n\nc = Array.new(1_000_001, false)\nc[2] = true\np = [ 2 ]\n3.step(1_000_000, 2) do |i|\n next if c[i]\n\n p << i\n\n i.step(1_000_000, i) { |x| c[x] = true }\nend\n\ngets\na = gets.split(' ').map(&:to_i)\n\npp = Set.new p.map { |x| x*x }\n\na.each do |n|\n puts pp.include?(n) ? 'YES' : 'NO'\nend\n"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split(\" \").map{|i| i.to_f}\n\nL = []\n \n(1..1000000).each do |i|\n L[i] = 1\nend\nL[1] = 0\n\n(2..1000000).each do |i|\n kq = 2*i\n if L[i] == 1\n while kq <= 1000000 do\n L[kq] = 0\n kq += i\n end\n end\nend\n\n(0..n-1).each do |i|\n can = Math.sqrt(a[i])\n if can == can .ceil && L[can] == 1\n puts \"YES\"\n else \n puts \"NO\"\n end\nend"}, {"source_code": "require 'set'\n\nn = gets.to_i\n\nx = gets.split.map(&:to_i)\n\nN = 10 ** 6\nM = 10 ** 3\n\nsieve = Array.new(N+1,true)\n\nS = Set.new\n\n(2..N).each do |i|\n next if not sieve[i]\n S.add(i ** 2)\n m = 2 * i\n while m <= N\n sieve[m] = false\n m += i\n end\nend\n\nx.each{|e| S.member?(e) ? (puts 'YES') : (puts 'NO')}"}, {"source_code": "n = gets.to_i\na = gets.split(' ').map{|i| i.to_i}\nL = Array.new\n\n(1..1000000).each do |i|\n L[i] = 1\nend\n\nL[1] = 0\n\n(2..1000000).each do |i|\n kq = 2*i\n if L[i] == 1\n while kq <= 1000000 do\n L[kq] = 0\n kq += i\n end\n end\nend\n\na.each do |i|\n if Math.sqrt(i.to_f) == Math.sqrt(i.to_f).floor\n f = Math.sqrt(i.to_f).to_i\n if L[f] == 1\n puts 'YES'\n else\n puts 'NO'\n end\n else\n puts 'NO'\n end\nend"}, {"source_code": "# require 'prime'\n\nn = gets.to_i\nf = gets.split.map { |x| x.to_i }\n\n# p = Prime.first(700000)\nis_prime = Array.new(1000001, true)\nis_prime[0] = is_prime[1] = false\n2.upto(1000001) do |i|\n\tif is_prime[i]\n\t\tj = i * 2\n\t\twhile j < 1000001\n\t\t\tis_prime[j] = false\n\t\t\tj += i\n\t\tend\n\tend\nend\n\nf.each do |x|\n\tt = (x ** 0.5).round\n\tif t ** 2 == x and is_prime[t]\n\t\tputs 'YES'\n\telse\n\t\tputs 'NO'\n\tend\nend\n"}], "negative_code": [{"source_code": "n = gets.to_i\nx = gets.split.map &:to_i\n\nmax = x.max\n\ndef sieve_numbers n\n sieve_array = Array.new(n + 1, true)\n sieve_array[0] = false\n sieve_array[1] = false\n\n for current_prime in 2 .. Math.sqrt(n).to_i\n next unless sieve_array[current_prime] \n i = 2\n while current_prime * i <= n\n sieve_array[current_prime * i] = false\n i += 1\n end\n end\n\n prime_numbers = []\n sieve_array.each_index do |index|\n prime_numbers << index if sieve_array[index]\n end\n return prime_numbers\nend\n\nprimes = sieve_numbers(Math.sqrt(max).ceil)\nsquared_primes = primes.map{|p| p * p}\n\nx.each do |test_case|\n if squared_primes.bsearch{|element| test_case - element}\n puts \"YES\"\n else\n puts \"NO\"\n end\nend\n\n\n\n\n"}, {"source_code": "\ngets.to_i;\narr = gets.chomp.split(' ').map(&:to_i);\nprimes = Array.new(1000000, 1);\n(2..1000000).each do |i|\n if primes[i]\n (i**2).step(1000000, i) {|m| primes[m] = nil}\n end\nend\n\narr.each {|x|\n sq = Math.sqrt(x).to_i;\n puts (sq**2 == x && primes[sq] ? \"YES\" : \"NO\")\n}\n"}, {"source_code": "require 'prime'\n\ngets.to_i;\narr = gets.chomp.split(' ').map(&:to_i);\nprimes = Prime.each(100000).map {|x| [x, 1]};\nhash = Hash[*primes.flatten];\n\narr.each { |x|\n sq = Math.sqrt(x).to_i;\n if (sq*sq == x && hash[sq])\n puts \"YES\";\n else\n puts \"NO\";\n end\n}\n"}, {"source_code": "require 'mathn'\nn = gets.to_i\nn.times do\n f = Math.sqrt(gets.to_i)\n puts (f == f.round and f.prime?) ? :YES : :NO\nend"}, {"source_code": "n = gets.to_i\na = gets.split.map &:to_i\n\nind = 0\nd = Array.new(100001,false)\nok = Array.new(100001)\n\nfor i in 2..1000001 do\n if (d[i] == false)\n ind += 1\n ok[ind] = i*i\n j = i+i\n begin\n d[j] = true\n j += i\n if (j > 100001) then break end\n end while true\n end \nend\n\nfor i in 0..n-1 do\n l = 0x0\n r = 0x0\n l = 1.to_i\n r = ind.to_i\n ans = false\n begin\n if (l>=r) then break end\n mid = (l+r) / 2\n if (ok[mid] > a[i]) then r = mid end\n if (ok[mid] < a[i]) then l = mid+1 end\n if (ok[mid] == a[i]) then ans = true end\n if (ans == true) then break end\n end while true\n if (ok[l] == a[i]) then ans = true end\n if (ans == true) then puts \"YES\"\n else puts \"NO\"\n end\nend"}, {"source_code": "n = gets.to_i\na = gets.split.map &:to_i\n\nind = 0\nd = Array.new(1000001,false)\nok = Array.new(0)\n\nfor i in 2..1000001 do\n if (d[i] == false)\n ind += 1\n ok.push(i*i)\n j = i+i\n begin\n d[j] = true\n j += i\n if (j > 1000001) then break end\n end while true\n end \nend\n\nfor i in 0..n-1 do\n l = 0x0\n r = 0x0\n l = 1.to_i\n r = ind.to_i\n ans = false\n begin\n if (l>=r) then break end\n mid = (l+r) / 2\n if (ok[mid] > a[i]) then r = mid end\n if (ok[mid] < a[i]) then l = mid+1 end\n if (ok[mid] == a[i]) then ans = true end\n if (ans == true) then break end\n end while true\n if (ok[l] == a[i]) then ans = true end\n if (ans == true) then puts \"YES\"\n else puts \"NO\"\n end\nend"}, {"source_code": "a = Array.new(1000001,0) \na[1] = -1\ni = 3\nwhile i < 1000\n\tif a[i] == 0\n\t\tj = i+i\n\t\twhile j < 1000000\n\t\t\ta[j] = -1\n\t\t\tj += j\n\t\tend\n\tend\n\ti += 2\nend\nn = gets.to_i\ngets.split.map(&:to_i).each do |i|\n\tx = Math.sqrt(i)\n\tif x % 1 == 0\n\t\tif x % 2 == 0 and x != 2\n\t\t\tputs 'NO'\n\t\telse\n\t\t\tif a[x] == -1\n\t\t\t\tputs 'NO'\n\t\t\telse\n\t\t\t\tputs 'YES'\n\t\t\tend \n\t\tend\n\n\telse\n\t\tputs 'NO'\n\tend\nend\n"}, {"source_code": "#!/usr/bin/ruby\n\nN = Integer gets.chomp\nnumbers = gets.chomp.split\nfor i in 0...N do\n\tnumbers[i] = Integer numbers[i]\nend\n\nX = 100000\nflags = Array.new(X+1)\nfor i in 0..X do\n\tflags[i] = true\nend\nflags[0] = flags[1] = false\nfor i in 2..X do\n\tif flags[i]\n\t\tj = i * i\n\t\twhile (j <= X) do\n\t\t\tflags[j] = false\n\t\t\tj += i;\n\t\tend\n\tend\nend\n\nfor i in numbers do\n\tj = Integer Math.sqrt(i)\n\tif flags[j] and j * j == i\n\t\tprint \"YES\\n\"\n\telse\n\t\tprint \"NO\\n\"\n\tend\nend\n\n\n\n\n\n"}, {"source_code": "#!/usr/bin/ruby\n\nN = Integer gets.chomp\nnumbers = gets.chomp.split\nfor i in 0...N do\n\tnumbers[i] = Integer numbers[i]\nend\n\nX = 100000\nflags = Array.new(X+1)\nfor i in 0..X do\n\tflags[i] = true\nend\nflags[0] = flags[1] = false\nprimes = Array.new\nfor i in 2..X do\n\tif flags[i]\n\t\tprimes.push i\n\t\tj = i * i\n\t\twhile (j <= X) do\n\t\t\tflags[j] = false\n\t\t\tj += i;\n\t\tend\n\tend\nend\n\nfor i in numbers do\n\tis_prime = i != 1\n\tfor j in primes do\n\t\tif j * j > i\n\t\t\tbreak\n\t\tend\n\t\tif i % j == 0\n\t\t\tis_prime = false\n\t\t\tbreak\n\t\tend\n\tend\n\tif is_prime\n\t\tprint \"YES\\n\"\n\telse\n\t\tprint \"NO\\n\"\n\tend\nend\n\n\n\n\n\n"}, {"source_code": "def generate_prime(n)\n\tprimes = Hash.new(true)\n\tprimes[0] = false\n\tprimes[1] = false\n\t(2..Math.sqrt(n)).each do |i|\n\t\tif primes[i]\n\t\t\tprimes[i] = true\n\t\t\t(i**2..n).step(i).each do |j|\n\t\t\t\tprimes[j] = false\n\t\t\tend\n\t\tend\n\tend\t\n\tprimes.select {|k,v| v == true}.keys\nend\n\n\nn = gets.to_i\narr = gets.split(\" \").map(&:to_i)\n@primes = generate_prime(10**6)\narr.each do |a|\n\t# puts @primes.include?(Math.sqrt(a)) ? \"YES\" : \"NO\"\nend"}, {"source_code": "def Eratosthenes2(n)\n\tl = Array 0..n\n\t\n\tl[0] = l[1] = nil\n\t\n\tfor i in (2..n ** (1.0 / 2))\n\t\tif l[i]\n\t\t\tj = i ** 2\n\t\t\twhile j <= n\n\t\t\t\tl[j] = nil\n\t\t\t\tj += i\n\t\t\tend\n\t\tend\n\tend\n\t\n\treturn l.compact.map { |x| x ** 2 }\nend\n\nl = Eratosthenes2(1000000)\n\ngets\ngets.split.each { |x| puts (l[0] == x.to_i or l.bsearch { |y| y == x.to_i } != nil) ? \"YES\" : \"NO\" }"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nc = Array.new(10, true)\n2.upto(10) do |i|\n if c[i]\n i.step(10, i){|j| c[j] = false if j != i}\n end\nend\na.each do |x|\n m = Math.sqrt(x)\n if m.ceil == m.to_i && c[m]\n puts 'YES'\n else\n puts 'NO'\n end\nend\n"}, {"source_code": "require 'prime'\nn = gets.to_i\na = gets.split.map(&:to_i)\n\na.each do |x|\n m = x.prime_division\n if m.size == 1 && m[0][0] == m[0][1]\n puts 'YES'\n else\n puts 'NO'\n end\nend\n"}, {"source_code": "require 'prime'\nn = gets.to_i\na = gets.split.map(&:to_i)\n\na.each do |x|\n m = x.prime_division\n if m.size == 1 && m[0][0] != x\n puts 'YES'\n else\n puts 'NO'\n end\nend\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nc = Array.new(10, true)\nc[1] = false\n2.upto(10) do |i|\n if c[i]\n i.step(10, i){|j| c[j] = false if j != i}\n end\nend\na.each do |x|\n m = Math.sqrt(x)\n if m.ceil == m.to_i && c[m]\n puts 'YES'\n else\n puts 'NO'\n end\nend\n"}, {"source_code": "require 'set'\n\ndef t_prime(x)\n result = false\n $p.each do |i|\n break if i > x\n if x.modulo(i).zero? && x.div(i) > 1 && result == false\n result = true\n elsif x.modulo(i).zero?\n return false\n end\n end\n result\nend\n\nc = Set.new [ 2 ]\n$p = [ 2 ]\ni = 1\nuntil i > 1_000_000\n i += 2\n \n next if c.include? i\n\n x = i\n until x > 1_000_000\n c << x\n x += i\n end\nend\n\ngets\na = gets.split(' ').map(&:to_i)\n\na.each do |n|\n puts t_prime(n) ? 'YES' : 'NO'\nend\n"}, {"source_code": "require 'set'\n\ndef t_prime(x)\n result = false\n $p.each do |i|\n break if i > x\n if x.modulo(i).zero? && x.div(i) > 1 && result == false\n result = true\n elsif x.modulo(i).zero?\n return false\n end\n end\n result\nend\n\nc = Set.new [ 2 ]\n$p = [ 2 ]\nc << 2\ni = 1\nuntil i > 1_000_000\n i += 2\n\n next if c.include? i\n\n $p << i\n\n x = i\n until x > 1_000_000\n c << x\n x += i\n end\nend\n\ngets\na = gets.split(' ').map(&:to_i)\n\na.each do |n|\n puts t_prime(n) ? 'YES' : 'NO'\nend\n"}, {"source_code": "n = gets.to_i\nb = gets.split(' ')\n\na = []\n\n(0..n-1).each do |i|\n a[i] = b[i].to_f\n if Math.sqrt(a[i]) == Math.sqrt(a[i]).ceil\n puts \"YES\"\n else\n puts \"NO\"\n end\nend"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split(\" \").map{|i| i.to_f}\n\n(0..n-1).each do |i|\n dem = 0\n can = Math.sqrt(a[i])\n (2..can.floor).each do |j|\n dem +=1 if a[i]%j == 0\n end\n if can == can.floor\n if 2*dem + 1 == n \n puts \"YES\" \n else\n puts \"NO\"\n end\n else\n if 2*dem + 2 == n\n puts \"YES\"\n else\n puts \"NO\"\n end\n end\nend"}, {"source_code": "n = gets.to_i\nb = gets.split(' ')\n\na = []\nkt = 1\n\n(0..n-1).each do |i|\n kt = 1\n a[i] = b[i].to_f\n can = Math.sqrt(a[i])\n if can == can.ceil\n (2..Math.sqrt(can).floor).each do |i|\n kt = 0 if can%i == 0\n end\n else kt = 0\n end\n if kt == 0 \n puts \"NO\"\n else\n puts \"YES\"\n end\nend"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split(\" \").map{|i| i.to_f}\n\n(0..n-1).each do |i|\n kt = 1\n if a[i] < 4\n puts \"NO\"\n else\n if a[i] == 4\n puts \"YES\"\n else\n can = Math.sqrt(a[i]).to_f\n if (can.ceil == can)\n (2..Math.sqrt(can).floor).each do |j|\n if can%j == 0\n puts \"NO\"\n kt = 0 \n end\n end\n if kt == 1\n puts \"YES\"\n end\n else \n puts \"NO\"\n end\n end\n end\nend"}, {"source_code": "n = gets.to_i\nb = gets.split(' ')\n\na = []\n\n(0..n-1).each do |i|\n a[i] = b[i].to_f\n dem = 0\n can = Math.sqrt(a[i])\n (2..can.floor).each do |j|\n dem +=1 if a[i]%j == 0\n end\n if can == can.floor\n if 2*dem + 1 == n \n puts \"YES\" \n else\n puts \"NO\"\n end\n else\n if 2*dem + 2 == n\n puts \"YES\"\n else\n puts \"NO\"\n end\n end\nend"}, {"source_code": "n = gets.to_i\nb = gets.split(' ')\n\na = []\n\n(0..n-1).each do |i|\n kt = 0\n a[i] = b[i].to_f\n (2..Math.sqrt(a[i]).floor).each do |j|\n if (a[i]%j == 0)\n kq = a[i]/j\n while kq > 1 do\n if kq %j != 0 \n break\n else\n kq = kq/j\n end \n end\n kt = 1 if kq == 1\n break\n end\n end\n if kt == 0 \n puts \"NO\"\n else\n puts \"YES\"\n end\nend"}, {"source_code": "require 'set'\n\nn = gets.to_i\n\nx = gets.split.map(&:to_i)\n\nN = 10 ** 6\nM = 10 ** 3\n\nsieve = Array.new(N+1,true)\n\nS = Set.new\n\n(2..N).each do |i|\n next if not sieve[i]\n S.add(i ** 2)\n m = 2 * i\n while m < N\n sieve[m] = false\n m += i\n end\nend\n\nx.each{|e| S.member?(e) ? (puts 'YES') : (puts 'NO')}"}, {"source_code": "n = gets.to_i\na = gets.split(' ').map{|i| i.to_i}\n\na.each do |i|\n if Math.sqrt(i.to_f) == Math.sqrt(i.to_f).floor && Math.sqrt(i.to_f).floor > 1\n f = Math.sqrt(i.to_f).to_i\n g = Math.sqrt(f).to_i\n puts 'YES'\n\n else\n puts 'NO'\n end\nend"}, {"source_code": "n = gets.to_i\na = gets.split(' ').map{|i| i.to_i}\n\na.each do |i|\n\tif Math.sqrt(i.to_f) == Math.sqrt(i.to_f).floor && Math.sqrt(i.to_f).floor > 1\n\t\tf = Math.sqrt(i.to_f).to_i\n\t\tif f == 2 || f == 3\n\t\t\tputs 'YES'\n\t\telsif f > 3\n\t\t\tg = Math.sqrt(f)\n\t\t\tm = 0\n\t\t\t(2..g).each do |t|\n\t\t\t\tif f%t == 0\n\t\t\t\t\tputs 'NO'\n\t\t\t\t\tbreak \n\t\t\t\tend\n\t\t\t\tm = t\n\t\t\tend\n\t\t\tif m == g\n\t\t\t\tputs 'YES'\n\t\t\tend\n\t\tend\n\telse\n\t\tputs 'NO'\n\tend\nend"}, {"source_code": "n = gets.to_i\na = gets.split(' ').map{|i| i.to_i}\n\na.each do |i|\n if Math.sqrt(i.to_f) == Math.sqrt(i.to_f).floor && Math.sqrt(i.to_f).floor > 1\n f = Math.sqrt(i.to_f).to_i\n if f == 2 || f == 3\n puts 'YES'\n elsif f > 3\n g = Math.sqrt(f).floor\n m = 0\n (2..g).each do |t|\n if f%g == 0\n puts 'NO'\n break \n end\n m = t\n end\n if m == g\n puts 'YES'\n end\n end\n else\n puts 'NO'\n end\nend"}, {"source_code": "n = gets.to_i\na = gets.split(' ').map{|i| i.to_i}\n\na.each do |i|\n if Math.sqrt(i.to_f) == Math.sqrt(i.to_f).floor && Math.sqrt(i.to_f).floor > 1\n f = Math.sqrt(i.to_f).to_i\n if f == 2 || f == 3\n puts 'YES'\n elsif f > 3\n g = (f/2).floor\n m = 0\n (2..g).each do |t|\n if f%g == 0\n puts 'NO'\n break \n end\n m = t\n end\n if m == g\n puts 'YES'\n end\n end\n else\n puts 'NO'\n end\nend"}, {"source_code": "# require 'prime'\n\nn = gets.to_i\nf = gets.split.map { |x| x.to_i }\n\n# p = Prime.first(700000)\nis_prime = Array.new(1000001, true)\nis_prime[0] = false\n2.upto(1000001) do |i|\n\tif is_prime[i]\n\t\tj = i * 2\n\t\twhile j < 1000001\n\t\t\tis_prime[j] = false\n\t\t\tj += i\n\t\tend\n\tend\nend\n\nf.each do |x|\n\tt = (x ** 0.5).round\n\tif t ** 2 == x and is_prime[t]\n\t\tputs 'YES'\n\telse\n\t\tputs 'NO'\n\tend\nend\n"}], "src_uid": "6cebf9af5cfbb949f22e8b336bf07044"} {"nl": {"description": "Along the railroad there are stations indexed from $$$1$$$ to $$$10^9$$$. An express train always travels along a route consisting of $$$n$$$ stations with indices $$$u_1, u_2, \\dots, u_n$$$, where ($$$1 \\le u_i \\le 10^9$$$). The train travels along the route from left to right. It starts at station $$$u_1$$$, then stops at station $$$u_2$$$, then at $$$u_3$$$, and so on. Station $$$u_n$$$ — the terminus.It is possible that the train will visit the same station more than once. That is, there may be duplicates among the values $$$u_1, u_2, \\dots, u_n$$$.You are given $$$k$$$ queries, each containing two different integers $$$a_j$$$ and $$$b_j$$$ ($$$1 \\le a_j, b_j \\le 10^9$$$). For each query, determine whether it is possible to travel by train from the station with index $$$a_j$$$ to the station with index $$$b_j$$$.For example, let the train route consist of $$$6$$$ of stations with indices [$$$3, 7, 1, 5, 1, 4$$$] and give $$$3$$$ of the following queries: $$$a_1 = 3$$$, $$$b_1 = 5$$$It is possible to travel from station $$$3$$$ to station $$$5$$$ by taking a section of the route consisting of stations [$$$3, 7, 1, 5$$$]. Answer: YES. $$$a_2 = 1$$$, $$$b_2 = 7$$$You cannot travel from station $$$1$$$ to station $$$7$$$ because the train cannot travel in the opposite direction. Answer: NO. $$$a_3 = 3$$$, $$$b_3 = 10$$$It is not possible to travel from station $$$3$$$ to station $$$10$$$ because station $$$10$$$ is not part of the train's route. Answer: NO. ", "input_spec": "The first line of the input contains an integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) —the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case is empty. The second line of each test case contains two integers: $$$n$$$ and $$$k$$$ ($$$1 \\le n \\le 2 \\cdot 10^5, 1 \\le k \\le 2 \\cdot 10^5$$$) —the number of stations the train route consists of and the number of queries. The third line of each test case contains exactly $$$n$$$ integers $$$u_1, u_2, \\dots, u_n$$$ ($$$1 \\le u_i \\le 10^9$$$). The values $$$u_1, u_2, \\dots, u_n$$$ are not necessarily different. The following $$$k$$$ lines contain two different integers $$$a_j$$$ and $$$b_j$$$ ($$$1 \\le a_j, b_j \\le 10^9$$$) describing the query with index $$$j$$$. It is guaranteed that the sum of $$$n$$$ values over all test cases in the test does not exceed $$$2 \\cdot 10^5$$$. Similarly, it is guaranteed that the sum of $$$k$$$ values over all test cases in the test also does not exceed $$$2 \\cdot 10^5$$$", "output_spec": "For each test case, output on a separate line: YES, if you can travel by train from the station with index $$$a_j$$$ to the station with index $$$b_j$$$ NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).", "sample_inputs": ["3\n\n\n\n\n6 3\n\n3 7 1 5 1 4\n\n3 5\n\n1 7\n\n3 10\n\n\n\n\n3 3\n\n1 2 1\n\n2 1\n\n1 2\n\n4 5\n\n\n\n\n7 5\n\n2 1 1 1 2 4 4\n\n1 3\n\n1 4\n\n2 1\n\n4 1\n\n1 2"], "sample_outputs": ["YES\nNO\nNO\nYES\nYES\nNO\nNO\nYES\nYES\nNO\nYES"], "notes": "NoteThe first test case is explained in the problem statement."}, "positive_code": [{"source_code": "require \"set\"\n\nt = gets.to_i\n\nt.times do\n gets\n\n n, k = gets.strip.split.map(&:to_i)\n arr = gets.split.map(&:to_i)\n\n lefts = Hash.new\n rights = Hash.new\n\n arr.each_with_index do |a, i|\n lefts[a] = i unless lefts[a]\n rights[a] = i\n end\n\n k.times do\n l, r = gets.split.map(&:to_i)\n\n if !lefts[l] || !rights[r]\n print \"NO\"\n elsif lefts[l] <= rights[r]\n print \"YES\"\n else\n print \"NO\"\n end\n\n puts\n end\nend\n"}], "negative_code": [], "src_uid": "44ce2a46e89789b9c1c63e5452f3159b"} {"nl": {"description": "Iahub and Iahubina went to a date at a luxury restaurant. Everything went fine until paying for the food. Instead of money, the waiter wants Iahub to write a Hungry sequence consisting of n integers. A sequence a1, a2, ..., an, consisting of n integers, is Hungry if and only if: Its elements are in increasing order. That is an inequality ai < aj holds for any two indices i, j (i < j). For any two indices i and j (i < j), aj must not be divisible by ai. Iahub is in trouble, so he asks you for help. Find a Hungry sequence with n elements.", "input_spec": "The input contains a single integer: n (1 ≤ n ≤ 105).", "output_spec": "Output a line that contains n space-separated integers a1 a2, ..., an (1 ≤ ai ≤ 107), representing a possible Hungry sequence. Note, that each ai must not be greater than 10000000 (107) and less than 1. If there are multiple solutions you can output any one.", "sample_inputs": ["3", "5"], "sample_outputs": ["2 9 15", "11 14 20 27 31"], "notes": null}, "positive_code": [{"source_code": "n = gets.chomp.to_i\n\nputs ((10000000-(n-1))..(10000000)).to_a.join(' ')"}, {"source_code": "a=gets.chomp.to_i\nans=a*3\n0.upto(a-1) do |i|\nprint \"#{ans+i} \"\nend"}, {"source_code": "a=gets.chomp.to_i\nans=a*3\n0.upto(a-1) do |i|\nprint \"#{ans+i} \"\nend\n"}, {"source_code": "n = Integer(gets)\n\n\n\nb = Array.new\nb[0] = 2\nb[1] = 3\nfor i in 0...10000\n c = 0\n for j in 0...b.length\n if (i + 2)% b[j] != 0\n c = 1\n else \n c = 0\n break\n end\n end\n if c == 1\n b.push(i + 2)\n end\nend\nc = Array.new\n\nfor i in 0...b.length\n for j in i...b.length\n c.push(b[i]*b[j])\n \n if c.length >= n\n break\n end\n end\n if c.length >= n\n break\n end\nend\nc = c.sort\nputs c"}, {"source_code": "n = Integer(gets)\n\n\n\nb = Array.new\nb[0] = 2\nb[1] = 3\nfor i in 0...10000\n c = 0\n for j in 0...b.length\n if (i + 2)% b[j] != 0\n c = 1\n else \n c = 0\n break\n end\n end\n if c == 1\n b.push(i + 2)\n end\nend\nc = Array.new\n\nfor i in 0...b.length\n for j in i...b.length\n c.push(b[i]*b[j])\n \n if c.length >= n\n break\n end\n end\nend\nc = c.sort\nfor i in 0...n\n puts c[i]\nend"}, {"source_code": "gets\nfor i in 700000...(700000 + $_.to_i)\n print \"#{i} \"\nend\n"}, {"source_code": "n = gets.to_i\nputs \"#{(5*1000000 .. 5*1000000+n-1).to_a.join(' ')}\"\n"}, {"source_code": "n = gets.strip.to_i\na = []\nn.times do |i|\n a << (10000000 - n + i)\nend\nputs a.join(\" \")"}, {"source_code": "def eratosthenes(n)\n nums = [true, false, true] * ((n + 5) / 6)\n nums[0] = false\n nums[1] = true\n\n i = 5\n until(m = i * i\n m > n) do\n if nums[i >> 1]\n i_times_2 = i << 1\n i_times_4 = i << 2\n while m <= n\n nums[m >> 1] = false\n m += i_times_2\n nums[m >> 1] = false\n m += i_times_4\n end\n end\n i += 2\n if nums[i >> 1]\n m = i * i\n i_times_2 = i << 1\n i_times_4 = i << 2\n while m <= n\n nums[m >> 1] = false\n m += i_times_4\n nums[m >> 1] = false\n m += i_times_2\n end\n end\n i += 4\n end\n\n primes = [2]\n nums.each_index {|i| primes << (i * 2 + 1) if nums[i]}\n primes.pop while primes.last > n\n return primes\nend\n\nputs eratosthenes(1299722)[0..gets.to_i-1].join(' ')"}, {"source_code": "puts (10**7-gets.to_i..10**7-1).to_a.join(' ')\n"}, {"source_code": "n = gets.to_i\nMAX = 1300000\nis_prime = Array.new(MAX + 1){true}\nis_prime[0] = false\nis_prime[1] = false\ni = 2\nwhile i * i <= MAX\n if is_prime[i]\n # puts i\n j = i * i\n while j < MAX + 1\n is_prime[j] = false\n j += i\n end\n end\n i += 1\nend\nres = []\nis_prime.each_with_index{|x, i| res << i if x}\nputs res[0...n].join(\" \")\n"}, {"source_code": "puts (10**7-gets.to_i..10**7-1).to_a\n"}, {"source_code": "prime = Array.new(1300000, true)\n\nprime[0] = prime[1] = false\n2.upto(1300000) do |i|\n\tif prime[i]\n\t\tt = i * 2\n\t\twhile t < 1300000\n\t\t\tprime[t] = false\n\t\t\tt += i\n\t\tend\n\tend\nend\n\nans = []\nn = gets.to_i\n2.upto(1300000) do |i|\n\tif prime[i]\n\t\tif n > 0\n\t\t\tn -= 1\n\t\t\tans << i\n\t\tend\n\tend\nend\n\nputs ans.join ' '"}], "negative_code": [{"source_code": "n = Integer(gets)\n\n\n\nb = Array.new\nb[0] = 2\nb[1] = 3\nfor i in 0...4*n\n c = 0\n for j in 0...b.length\n if (i + 2)% b[j] != 0\n c = 1\n else \n c = 0\n break\n end\n end\n if c == 1\n b.push(i + 2)\n end\nend\nfor i in 0...n\n print b[i], \" \"\nend"}, {"source_code": "n = Integer(gets)\n\n\n\nb = Array.new\nb[0] = 2\nb[1] = 3\nfor i in 0...2*n\n c = 0\n for j in 0...b.length\n if (i + 2)% b[j] != 0\n c = 1\n else \n c = 0\n break\n end\n end\n if c == 1\n b.push(i + 2)\n end\nend\nfor i in 0...n\n print b[i], \" \"\nend"}, {"source_code": "n = Integer(gets)\n\n\n\nb = Array.new\nb[0] = 2\nb[1] = 3\nfor i in 0...n + 1000\n c = 0\n for j in 0...b.length\n if (i + 2)% b[j] != 0\n c = 1\n else \n c = 0\n break\n end\n end\n if c == 1\n b.push(i + 2)\n end\nend\nfor i in 0...n\n print b[i], \" \"\nend"}, {"source_code": "n = Integer(gets)\n\n\n\nb = Array.new\nb[0] = 2\nb[1] = 3\nfor i in 0...10000\n c = 0\n for j in 0...b.length\n if (i + 2)% b[j] != 0\n c = 1\n else \n c = 0\n break\n end\n end\n if c == 1\n b.push(i + 2)\n end\nend\nc = Array.new\n\nfor i in 0...b.length\n for j in i...b.length\n c.push(b[i]*b[j])\n \n if c.length >= n\n break\n end\n end\nend\nfor i in 0...n\n puts c[i]\nend"}, {"source_code": "n = Integer(gets)\n\n\n\nb = Array.new\nb[0] = 2\nb[1] = 3\nfor i in 0...n + 100\n c = 0\n for j in 0...b.length\n if (i + 2)% b[j] != 0\n c = 1\n else \n c = 0\n break\n end\n end\n if c == 1\n b.push(i + 2)\n end\nend\nfor i in 0...n\n print b[i], \" \"\nend"}, {"source_code": "n = Integer(gets)\n\n\n\nb = Array.new\nb[0] = 2\nb[1] = 3\nfor i in 0...6*n\n c = 0\n for j in 0...b.length\n if (i + 2)% b[j] != 0\n c = 1\n else \n c = 0\n break\n end\n end\n if c == 1\n b.push(i + 2)\n end\nend\nfor i in 0...n\n print b[i], \" \"\nend"}, {"source_code": "n = Integer(gets)\n\n\n\nb = Array.new\nb[0] = 2\nb[1] = 3\nfor i in 0...10000\n c = 0\n for j in 0...b.length\n if (i + 2)% b[j] != 0\n c = 1\n else \n c = 0\n break\n end\n end\n if c == 1\n b.push(i + 2)\n end\nend\nc = Array.new\n\nfor i in 0...b.length\n for j in i...b.length\n c.push(b[i]*b[j])\n \n if c.length >= n\n break\n end\n end\n if c.length >= n\n break\n end\nend\n\nputs c"}, {"source_code": "n = Integer(gets)\n\n\n\nb = Array.new\nb[0] = 2\nb[1] = 3\nfor i in 0...10000\n c = 0\n for j in 0...b.length\n if (i + 2)% b[j] != 0\n c = 1\n else \n c = 0\n break\n end\n end\n if c == 1\n b.push(i + 2)\n end\nend\nc = Array.new\n\nfor i in 0...b.length\n for j in 0...b.length\n c.push(b[i]*b[j])\n \n if c.length >= n\n break\n end\n end\nend\nfor i in 0...n\n puts c[i]\nend"}], "src_uid": "c047040426e736e9085395ed9666135f"} {"nl": {"description": "Ivan wants to play a game with you. He picked some string $$$s$$$ of length $$$n$$$ consisting only of lowercase Latin letters. You don't know this string. Ivan has informed you about all its improper prefixes and suffixes (i.e. prefixes and suffixes of lengths from $$$1$$$ to $$$n-1$$$), but he didn't tell you which strings are prefixes and which are suffixes.Ivan wants you to guess which of the given $$$2n-2$$$ strings are prefixes of the given string and which are suffixes. It may be impossible to guess the string Ivan picked (since multiple strings may give the same set of suffixes and prefixes), but Ivan will accept your answer if there is at least one string that is consistent with it. Let the game begin!", "input_spec": "The first line of the input contains one integer number $$$n$$$ ($$$2 \\le n \\le 100$$$) — the length of the guessed string $$$s$$$. The next $$$2n-2$$$ lines are contain prefixes and suffixes, one per line. Each of them is the string of length from $$$1$$$ to $$$n-1$$$ consisting only of lowercase Latin letters. They can be given in arbitrary order. It is guaranteed that there are exactly $$$2$$$ strings of each length from $$$1$$$ to $$$n-1$$$. It is also guaranteed that these strings are prefixes and suffixes of some existing string of length $$$n$$$.", "output_spec": "Print one string of length $$$2n-2$$$ — the string consisting only of characters 'P' and 'S'. The number of characters 'P' should be equal to the number of characters 'S'. The $$$i$$$-th character of this string should be 'P' if the $$$i$$$-th of the input strings is the prefix and 'S' otherwise. If there are several possible answers, you can print any.", "sample_inputs": ["5\nba\na\nabab\na\naba\nbaba\nab\naba", "3\na\naa\naa\na", "2\na\nc"], "sample_outputs": ["SPPSPSPS", "PPSS", "PS"], "notes": "NoteThe only string which Ivan can guess in the first example is \"ababa\".The only string which Ivan can guess in the second example is \"aaa\". Answers \"SPSP\", \"SSPP\" and \"PSPS\" are also acceptable.In the third example Ivan can guess the string \"ac\" or the string \"ca\". The answer \"SP\" is also acceptable."}, "positive_code": [{"source_code": "n = gets.to_i\nstr = []\n(2*n-2).times do |i|\n str << [gets.chomp,i]\nend\n\nstr.sort_by!{|s| s[0].length}\n\na,b,c,d = str[0][0],str[1][0],str[-1][0],str[-2][0]\nguess = [a+c,a+d,b+c,b+d]\n\n#p guess\nstr.each do |a|\n s = a[0]\n len = s.length\n guess.each do |t|\n guess.delete(t) if t[0,len] != s && t[n-len,len] != s\n end\n #p guess\nend\n\norigin = guess[0]\n\n(n-1).times do |i|\n a,b = str[2*i][0],str[2*i+1][0]\n len = a.length\n\n #p [a,b,flg]\n if a == origin[0,len]\n str[2*i] << \"P\"\n str[2*i+1] << \"S\"\n else\n str[2*i] << \"S\"\n str[2*i+1] << \"P\"\n end\nend\n#p guess\n\nstr.sort_by!{|s| s[1]}\nstr.each do |a|\n print \"#{a[2]}\"\nend\n"}, {"source_code": "# Utility\nclass StringBuilder\n def initialize(chunks)\n chunks_sorted = chunks.map.with_index { |chunk, i| {i: i, str: chunk} }\n .sort { |a, b| a[:str].length <=> b[:str].length }\n @pairs = Array.new(chunks_sorted.length / 2) { |i| [chunks_sorted[2 * i], chunks_sorted[2 * i + 1]] }\n\n @string = build\n\n @solution = \"S\" * chunks.length\n @pairs.each do |p|\n if @string.start_with? p[0][:str]\n @solution[p[0][:i]] = \"P\"\n else\n @solution[p[1][:i]] = \"P\"\n end\n end\n end\n\n attr_reader :string\n attr_reader :solution\n\n private\n def build\n str_1 = @pairs[-1][0][:str] + @pairs[-1][1][:str][-1]\n str_2 = @pairs[-1][1][:str] + @pairs[-1][0][:str][-1]\n return str_1 if correct? str_1\n return str_2 if correct? str_2\n end\n\n def correct?(str)\n (str.length - 1).times do |i|\n a = (str.start_with? @pairs[i][0][:str]) && (str.end_with? @pairs[i][1][:str])\n b = (str.start_with? @pairs[i][1][:str]) && (str.end_with? @pairs[i][0][:str])\n break false unless a || b\n end\n end\nend\n\n# Input\nn = gets.to_i # 2 <= n <= 100\nchunks = Array.new(2 * n - 2) { |i| gets.chop }\n\n# Result\nresult = StringBuilder.new(chunks)\n#puts result.string\nputs result.solution\n"}, {"source_code": "n = gets.to_i\narr = []\n(0..n * 2 - 3).each do |i|\n arr.push([gets.chop, i])\nend\nbrr = arr.sort_by{ |t| t.first.length }\nc1 = brr[-1].first + brr[-2].first[-1]\nc2 = brr[-2].first + brr[-1].first[-1]\n\ndef isPrefix(s, pr)\n s[0 .. pr.length - 1] == pr\nend\n\ndef isSuffix(s, sf)\n s[-sf.length .. -1] == sf\nend\n\nres1 = \"_\" * (n * 2 - 2)\nres2 = res1.dup\n\nbrr.each_slice(2) do |f, s|\n s1, n1 = f\n s2, n2 = s\n res1[n1], res1[n2] = \"P\", \"S\" if isPrefix(c1, s1) && isSuffix(c1, s2)\n res1[n1], res1[n2] = \"S\", \"P\" if isSuffix(c1, s1) && isPrefix(c1, s2)\n res2[n1], res2[n2] = \"P\", \"S\" if isPrefix(c2, s1) && isSuffix(c2, s2)\n res2[n1], res2[n2] = \"S\", \"P\" if isSuffix(c2, s1) && isPrefix(c2, s2)\nend\n\nif !res1.include? \"_\" then puts res1 else puts res2 end"}, {"source_code": "n = gets.to_i\ns = (2*n-2).times.map{|i|[gets.chomp,i]}.sort_by{|str,i|str.size}\nret = Array.new(2*n-2,0)\nans = s[-2][0]+s[-1][0][-1]\nf = true\nfor i in 0..n-2\n if ans[0..i] == s[i*2][0] && ans[-1-i..-1] == s[i*2+1][0]\n ret[s[i*2+1][1]] = 1\n elsif ans[0..i] == s[i*2+1][0] && ans[-1-i..-1] == s[i*2][0]\n ret[s[i*2][1]] = 1\n else\n f = false\n break\n end\nend\n\nif !f\n ans = s[-1][0]+s[-2][0][-1]\n ret = Array.new(2*n-2,0)\n for i in 0..n-2\n if ans[0..i] == s[i*2][0] && ans[-1-i..-1] == s[i*2+1][0]\n ret[s[i*2+1][1]] = 1\n else\n ret[s[i*2][1]] = 1\n end\n end\nend\n\nfor i in 0..2*n-3\n print ret[i] == 0 ? \"P\" : \"S\"\nend"}], "negative_code": [{"source_code": "n = gets.to_i\nstr = []\n(2*n-2).times do |i|\n str << [gets.chomp,i]\nend\n\nstr.sort_by!{|s| s[0].length}\nguess = \" \"*n\n\n((n-1)/2).times do |i|\n a,b,c,d = str[2*i][0],str[2*i+1][0],str[-2*i-1][0],str[-2*i-2][0]\n (i+1).times do |j|\n if a[j] == b[j] \n guess[j] = a[j]\n guess[n-i-1+j] = a[j]\n end\n end\n (n-i-1).times do |j|\n if c[j] == d[j]\n guess[j] = c[j]\n guess[i+j+1] = c[j]\n end\n end\n #p guess\nend\n\n(n-1).times do |i|\n a,b = str[2*i][0],str[2*i+1][0]\n flg1,flg2 = true,true\n (i+1).times do |j|\n flg1 = false if guess[j] != \" \" && guess[j] != a[j]\n flg2 = false if guess[j] != \" \" && guess[j] != b[j]\n end\n if flg1 && !flg2\n guess[0,i+1] = a\n guess[n-i-1,i+1] = b\n elsif !flg1 && flg2\n guess[0,i+1] = b\n guess[n-i-1,i+1] = a\n end\nend\n#p guess\n\n(n-1).times do |i|\n a,b = str[2*i][0],str[2*i+1][0]\n flg = true\n (i+1).times do |j|\n flg = false if guess[j] != \" \" && guess[j] != a[j]\n end\n #p [a,b,flg]\n if flg\n str[2*i] << \"P\"\n str[2*i+1] << \"S\"\n else\n str[2*i] << \"S\"\n str[2*i+1] << \"P\"\n end\nend\n#p guess\n\nstr.sort_by!{|s| s[1]}\nstr.each do |a|\n print \"#{a[2]}\"\nend\n"}, {"source_code": "# Utility\nclass StringBuilder\n def initialize(chunks)\n chunks_sorted = chunks.map.with_index { |chunk, i| {i: i, str: chunk} }\n .sort { |a, b| a[:str].length <=> b[:str].length }\n @pairs = Array.new(chunks_sorted.length / 2) { |i| [chunks_sorted[2 * i], chunks_sorted[2 * i + 1]] }\n\n @string = build\n\n @solution = \"S\" * chunks.length\n @pairs.each do |p|\n if @string.start_with? p[0][:str]\n @solution[p[0][:i]] = \"P\"\n else\n @solution[p[1][:i]] = \"P\"\n end\n end\n end\n\n attr_reader :string\n attr_reader :solution\n\n private\n def build\n str_1 = @pairs[-1][0][:str] + @pairs[-1][1][:str][-1]\n str_2 = @pairs[-1][1][:str] + @pairs[-1][0][:str][-1]\n return str_1 if correct? str_1\n return str_2 if correct? str_2\n end\n\n def correct?(str)\n (str.length - 1).times do |i|\n break false unless @pairs[i].find { |e| str.start_with? e[:str] }\n end\n end\nend\n\n# Input\nn = gets.to_i # 2 <= n <= 100\nchunks = Array.new(2 * n - 2) { |i| gets.chop }\n\n# Result\nresult = StringBuilder.new(chunks)\n#puts result.string\nputs result.solution\n"}, {"source_code": "# Utility\nclass StringBuilder\n def initialize(chunks)\n chunks_sorted = chunks.map.with_index { |chunk, i| {i: i, str: chunk} }\n .sort { |a, b| a[:str].length <=> b[:str].length }\n pairs = Array.new(chunks_sorted.length / 2) { |i| [chunks_sorted[2 * i], chunks_sorted[2 * i + 1]].sort! { |a, b| a[:str] <=> b[:str] } }\n # Beacuse of sorting inside pairs\n #p pairs\n\n str_1 = pairs[-1][0][:str]\n str_2 = pairs[-1][1][:str]\n if str_1 + str_2[-1] == str_1[0] + str_2\n # str_1 is prefix\n @string = str_1 + str_2[-1]\n else\n # str_2 is prefix\n @string = str_2 + str_1[-1]\n end\n\n @solution = \"S\" * chunks.length\n pairs.each do |p|\n if @string.start_with? p[0][:str]\n @solution[p[0][:i]] = \"P\"\n else\n @solution[p[1][:i]] = \"P\"\n end\n end\n end\n\n attr_reader :string\n attr_reader :solution\nend\n\n# Input\nn = gets.to_i # 2 <= n <= 100\nchunks = Array.new(2 * n - 2) { |i| gets.chop }\n\n# Result\nresult = StringBuilder.new(chunks)\nputs result.string\nputs result.solution\n"}, {"source_code": "# Utility\nclass StringBuilder\n def initialize(chunks)\n @n = 1 + chunks.length / 2\n chunks_sorted = chunks.sort { |a, b| a[:str].length <=> b[:str].length }\n pairs = Array.new(chunks_sorted.length / 2) { |i| [chunks_sorted[2 * i], chunks_sorted[2 * i + 1]] }\n\n builder(pairs)\n end\n\n attr_reader :string\n attr_reader :solution\n\n private\n def builder(pairs, str = nil, prefix_indexes = [], last_char = nil)\n # Building from prefixes\n unless str\n # First call\n str = \"\"\n end\n\n if pairs.length == 0\n # Last call\n @string = str + last_char\n @solution = \"S\" * (2 * @n - 2)\n prefix_indexes.each { |i| @solution[i] = \"P\" }\n return @string\n end\n\n pair = pairs.shift\n str_ext = nil # Next string\n 2.times do |i|\n next unless pair[i][:str].start_with?(str)\n #puts \"#{str} => #{pair[i][:str]}\"\n str_ext = builder(pairs, pair[i][:str], prefix_indexes + [pair[i][:i]], last_char || pair[1 - i][:str])\n break if str_ext # Built successfully\n #puts \"X\"\n end\n\n return str_ext\n end\nend\n\n# Input\nn = gets.to_i # 2 <= n <= 100\nchunks = Array.new(2 * n - 2) { |i| {i: i, str: gets.chop} }\n\n# Result\nresult = StringBuilder.new(chunks)\n#puts result.string\nputs result.solution\n"}, {"source_code": "# Utility\nclass StringBuilder\n def initialize(chunks)\n chunks_sorted = chunks.map.with_index { |chunk, i| {i: i, str: chunk} }\n .sort { |a, b| a[:str].length <=> b[:str].length }\n pairs = Array.new(chunks_sorted.length / 2) { |i| [chunks_sorted[2 * i], chunks_sorted[2 * i + 1]].sort! { |a, b| a[:str] <=> b[:str] } }\n # Beacuse of sorting inside pairs\n #p pairs\n\n str_1 = pairs[-1][0][:str]\n str_2 = pairs[-1][1][:str]\n if str_1 + str_2[-1] == str_1[0] + str_2\n # str_1 is prefix\n @string = str_1 + str_2[-1]\n else\n # str_2 is prefix\n @string = str_2 + str_1[-1]\n end\n\n @solution = \"S\" * chunks.length\n pairs.each do |p|\n if @string.start_with? p[0][:str]\n @solution[p[0][:i]] = \"P\"\n else\n @solution[p[1][:i]] = \"P\"\n end\n end\n end\n\n attr_reader :string\n attr_reader :solution\nend\n\n# Input\nn = gets.to_i # 2 <= n <= 100\nchunks = Array.new(2 * n - 2) { |i| gets.chop }\n\n# Result\nresult = StringBuilder.new(chunks)\n#puts result.string\nputs result.solution\n"}], "src_uid": "ddbde202d00b928a858e9f4ff461e88c"} {"nl": {"description": "Amugae has a hotel consisting of $$$10$$$ rooms. The rooms are numbered from $$$0$$$ to $$$9$$$ from left to right.The hotel has two entrances — one from the left end, and another from the right end. When a customer arrives to the hotel through the left entrance, they are assigned to an empty room closest to the left entrance. Similarly, when a customer arrives at the hotel through the right entrance, they are assigned to an empty room closest to the right entrance.One day, Amugae lost the room assignment list. Thankfully Amugae's memory is perfect, and he remembers all of the customers: when a customer arrived, from which entrance, and when they left the hotel. Initially the hotel was empty. Write a program that recovers the room assignment list from Amugae's memory.", "input_spec": "The first line consists of an integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$), the number of events in Amugae's memory. The second line consists of a string of length $$$n$$$ describing the events in chronological order. Each character represents: 'L': A customer arrives from the left entrance. 'R': A customer arrives from the right entrance. '0', '1', ..., '9': The customer in room $$$x$$$ ($$$0$$$, $$$1$$$, ..., $$$9$$$ respectively) leaves. It is guaranteed that there is at least one empty room when a customer arrives, and there is a customer in the room $$$x$$$ when $$$x$$$ ($$$0$$$, $$$1$$$, ..., $$$9$$$) is given. Also, all the rooms are initially empty.", "output_spec": "In the only line, output the hotel room's assignment status, from room $$$0$$$ to room $$$9$$$. Represent an empty room as '0', and an occupied room as '1', without spaces.", "sample_inputs": ["8\nLLRL1RL1", "9\nL0L0LLRR9"], "sample_outputs": ["1010000011", "1100000010"], "notes": "NoteIn the first example, hotel room's assignment status after each action is as follows. First of all, all rooms are empty. Assignment status is 0000000000. L: a customer arrives to the hotel through the left entrance. Assignment status is 1000000000. L: one more customer from the left entrance. Assignment status is 1100000000. R: one more customer from the right entrance. Assignment status is 1100000001. L: one more customer from the left entrance. Assignment status is 1110000001. 1: the customer in room $$$1$$$ leaves. Assignment status is 1010000001. R: one more customer from the right entrance. Assignment status is 1010000011. L: one more customer from the left entrance. Assignment status is 1110000011. 1: the customer in room $$$1$$$ leaves. Assignment status is 1010000011. So after all, hotel room's final assignment status is 1010000011.In the second example, hotel room's assignment status after each action is as follows. L: a customer arrives to the hotel through the left entrance. Assignment status is 1000000000. 0: the customer in room $$$0$$$ leaves. Assignment status is 0000000000. L: a customer arrives to the hotel through the left entrance. Assignment status is 1000000000 again. 0: the customer in room $$$0$$$ leaves. Assignment status is 0000000000. L: a customer arrives to the hotel through the left entrance. Assignment status is 1000000000. L: one more customer from the left entrance. Assignment status is 1100000000. R: one more customer from the right entrance. Assignment status is 1100000001. R: one more customer from the right entrance. Assignment status is 1100000011. 9: the customer in room $$$9$$$ leaves. Assignment status is 1100000010. So after all, hotel room's final assignment status is 1100000010."}, "positive_code": [{"source_code": "def find_zero_index_from_left(array)\n found_index = nil\n array.each_with_index do |element, index|\n if element.eql?(0)\n found_index = index\n break\n end\n end\n found_index\nend\n\ndef find_zero_index_from_right(array)\n found_index = nil\n (array.size - 1).downto(0).each do |index|\n if array[index].eql?(0)\n found_index = index\n break\n end\n end\n found_index\nend\n\n_ = gets.strip.to_i\narrives_code = gets.strip.split('')\n\ncodes = Array.new(10, 0)\n\narrives_code.each do |code|\n if code.eql?('L')\n index = find_zero_index_from_left(codes)\n codes[index] = 1\n elsif code.eql?('R')\n index = find_zero_index_from_right(codes)\n codes[index] = 1\n else\n index = code.to_i\n codes[index] = 0\n end\nend\n\nputs codes.join('')\n"}, {"source_code": "def change_room_status_for(event_char, rooms_status)\n\n if event_char == \"L\"\n empty_room = rooms_status.index(\"0\")\n rooms_status[empty_room] = \"1\"\n elsif event_char == \"R\"\n empty_room = rooms_status.rindex(\"0\")\n rooms_status[empty_room] = \"1\"\n else\n checked_out_room = event_char.to_i\n rooms_status[checked_out_room] = \"0\"\n end\n\nend\n\nnum_of_events_in_amugae_memory = gets.chomp.to_i\n\nevents = gets.chomp\nrooms_status = '0' * 10\n\nevents.each_char { |event| change_room_status_for(event, rooms_status) }\n\nputs rooms_status"}, {"source_code": "def change_room_status_for(event_char, rooms_status)\n if event_char == \"L\"\n room_idx = closest_empty_room_from_left(rooms_status)\n rooms_status[room_idx] = \"1\"\n elsif event_char == \"R\"\n room_idx = closest_empty_room_from_right(rooms_status)\n rooms_status[room_idx] = \"1\"\n else\n leaving_customer_idx = event_char.to_i\n rooms_status[leaving_customer_idx] = \"0\"\n end\nend\n\ndef closest_empty_room_from_left(rooms_status)\n rooms_status.index(\"0\")\nend\n\ndef closest_empty_room_from_right(rooms_status)\n idx = rooms_status.length - 1\n\n while idx >= 0\n if rooms_status[idx] == \"0\"\n return idx\n end\n\n idx -= 1\n end\nend\n\nnum_of_events_in_amugae_memory = gets.chomp.to_i\n\nevents = gets.chomp\nrooms_status = '0' * 10\n\nevents.each_char { |event| change_room_status_for(event, rooms_status) }\n\nputs rooms_status"}, {"source_code": "a=Array.new(10,0)\nn=gets().to_i\ns=gets()\nfor i in 0...n do\n if s[i]==\"L\" then\n a[a.index(0)]=1\n elsif s[i]==\"R\" then\n a[a.rindex(0)]=1\n else\n a[(s[i]).to_i]=0\n end\nend\nfor i in 0..9 do\n print a[i]\nend"}, {"source_code": "\nn = gets.to_i\nstr = gets.chomp\nans = '0'*10\nstr.each_char do |x|\n\tif x == 'L'\n\t\tans[ans.index('0')] = '1'\n\telsif x == 'R'\n\t\tans[ans.rindex('0')] = '1'\n\telse\n\t\tans[x.to_i] = '0'\n\tend\nend\nputs ans\n"}, {"source_code": "def test\n ind_l = 0\n ind_r = 0\n \ninput = $stdin.read\ninput = input.split(\"\\n\")\n length = input[0].to_i\n str = input[1].chars\n\n out = [0,0,0,0,0,0,0,0,0,0]\n for i in str\n if i == 'L'\n \n ind_l = out.index(0)\n out[ind_l] = 1\n ind_l = ind_l +1 ; \n elsif i == 'R'\n \n out_rev=out.reverse\n ind_r=out_rev.index(0)\n out[9-ind_r] = 1\n ind_r = ind_r +1 ; \n else\n \n out[i.to_i] = 0\n end\n \n end\n puts out.join()\nend \ntest\n"}, {"source_code": "#!/usr/bin/env ruby\n\nevents_no = gets \nevents_seq = gets \n\n\noutput = Array.new(10, 0)\n\n\nevents_seq.delete!(\"\\n\").split('').each { |c|\n\n\tif c == 'L'\n\t\toutput[output.find_index(0)]=1\n elsif c == 'R'\n\t\toutput[output.rindex(0)]=1\n\n\telse\n\t\toutput[c.to_i] = 0\n\tend\n\n\n\t\n}\n\nputs output.join('')\n\n\n\n"}], "negative_code": [{"source_code": "def find_zero_index_from_left(array)\n found_index = nil\n array.each_with_index do |element, index|\n if element.eql?(0)\n found_index = index\n break\n end\n end\n found_index\nend\n\ndef find_zero_index_from_right(array)\n found_index = nil\n (array.size - 1).downto(0).each do |index|\n if array[index].eql?(0)\n found_index = index\n break\n end\n end\n found_index\nend\n\n_ = gets.strip.to_i\narrives_code = gets.strip.split('')\n\ncodes = Array.new(10, 0)\n\narrives_code.each do |code|\n if code.eql?('L')\n index = find_zero_index_from_left(codes)\n codes[index] = 1\n elsif code.eql?('R')\n index = find_zero_index_from_right(codes)\n codes[index] = 1\n else\n index = code.to_i\n codes[index] = 0\n end\nend\n\np codes\n"}, {"source_code": "\n\ndef test\n ind_l = 0\n ind_r = 0\n $/ = \"1\" \ninput = STDIN.gets\ninput = input.split(\"\\n\")\n length = input[0].to_i\n str = input[1].to_s.chars\n \n out = [0,0,0,0,0,0,0,0,0,0]\n for i in str\n if i == 'L'\n \n ind_l = out.index(0)\n out[ind_l] = 1\n ind_l = ind_l +1 ; \n elsif i == 'R'\n \n out_rev=out.reverse\n ind_r=out_rev.index(0)\n out[9-ind_r] = 1\n ind_r = ind_r +1 ; \n else\n \n out[i.to_i] = 0\n end\n \n end\n return out\nend \ntest"}, {"source_code": "def test\n ind_l = 0\n ind_r = 0\n $/ = \"1\" \ninput = STDIN.gets\ninput = input.split(\"\\n\")\n length = input[0].to_i\n str = input[1].to_s.chars\n \n out = [0,0,0,0,0,0,0,0,0,0]\n for i in str\n if i == 'L'\n \n ind_l = out.index(0)\n out[ind_l] = 1\n ind_l = ind_l +1 ; \n elsif i == 'R'\n \n out_rev=out.reverse\n ind_r=out_rev.index(0)\n out[9-ind_r] = 1\n ind_r = ind_r +1 ; \n else\n \n out[i.to_i] = 0\n end\n \n end\n puts out.to_s\nend \ntest"}, {"source_code": "def test\n ind_l = 0\n ind_r = 0\n $/ = \"1\" \ninput = STDIN.gets\ninput = input.split(\"\\n\")\n length = input[0].to_i\n str = input[1].to_s.chars\n \n out = [0,0,0,0,0,0,0,0,0,0]\n for i in str\n if i == 'L'\n \n ind_l = out.index(0)\n out[ind_l] = 1\n ind_l = ind_l +1 ; \n elsif i == 'R'\n \n out_rev=out.reverse\n ind_r=out_rev.index(0)\n out[9-ind_r] = 1\n ind_r = ind_r +1 ; \n else\n \n out[i.to_i] = 0\n end\n \n end\n puts \"#{out}\"\nend \ntest"}, {"source_code": "def test\n ind_l = 0\n ind_r = 0\n $/ = \"1\" \ninput = STDIN.gets\ninput = input.split(\"\\n\")\n length = input[0].to_i\n str = input[1].to_s.chars\n \n out = [0,0,0,0,0,0,0,0,0,0]\n for i in str\n if i == 'L'\n \n ind_l = out.index(0)\n out[ind_l] = 1\n ind_l = ind_l +1 ; \n elsif i == 'R'\n \n out_rev=out.reverse\n ind_r=out_rev.index(0)\n out[9-ind_r] = 1\n ind_r = ind_r +1 ; \n else\n \n out[i.to_i] = 0\n end\n \n end\n puts out.join()\nend \ntest"}, {"source_code": "def test\n ind_l = 0\n ind_r = 0\n\ninput = $stdin.read\ninput = input.split(\"\\n\")\n length = input[0].to_i\n str = input[1].chars\n puts length\n puts str\n out = [0,0,0,0,0,0,0,0,0,0]\n for i in str\n if i == 'L'\n \n ind_l = out.index(0)\n out[ind_l] = 1\n ind_l = ind_l +1 ; \n elsif i == 'R'\n \n out_rev=out.reverse\n ind_r=out_rev.index(0)\n out[9-ind_r] = 1\n ind_r = ind_r +1 ; \n else\n \n out[i.to_i] = 0\n end\n \n end\n puts out.join()\nend \ntest"}, {"source_code": "def test\n ind_l = 0\n ind_r = 0\ninput = $stdin.read\ninput = input.split(\"\\n\")\n length = input[0].to_i\n str = input[1].to_s.chars\n \n out = [0,0,0,0,0,0,0,0,0,0]\n for i in str\n if i == 'L'\n \n ind_l = out.index(0)\n out[ind_l] = 1\n ind_l = ind_l +1 ; \n elsif i == 'R'\n \n out_rev=out.reverse\n ind_r=out_rev.index(0)\n out[9-ind_r] = 1\n ind_r = ind_r +1 ; \n else\n \n out[i.to_i] = 0\n end\n \n end\n return out\nend \ntest"}, {"source_code": "def first input\n puts input\nend "}, {"source_code": "def test\n ind_l = 0\n ind_r = 0\ninput = $stdin.read\ninput = input.split(\"\\n\")\n length = input[0].to_i\n str = input[1].to_s.chars\n \n out = [0,0,0,0,0,0,0,0,0,0]\n for i in str\n if i == 'L'\n puts 'L' \n ind_l = out.index(0)\n out[ind_l] = 1\n ind_l = ind_l +1 ; \n elsif i == 'R'\n puts 'R'\n out_rev=out.reverse\n ind_r=out_rev.index(0)\n out[9-ind_r] = 1\n ind_r = ind_r +1 ; \n else\n puts 'ji'\n out[i.to_i] = 0\n end\n puts \"#{out}\"\n end\n return out\nend "}, {"source_code": "def test\n ind_l = 0\n ind_r = 0\n $/ = \"1\" \ninput = STDIN.gets\ninput = input.split(\"\\n\")\n length = input[0].to_i\n str = input[1].to_s.chars\n \n out = [0,0,0,0,0,0,0,0,0,0]\n for i in str\n if i == 'L'\n \n ind_l = out.index(0)\n out[ind_l] = 1\n ind_l = ind_l +1 ; \n elsif i == 'R'\n \n out_rev=out.reverse\n ind_r=out_rev.index(0)\n out[9-ind_r] = 1\n ind_r = ind_r +1 ; \n else\n \n out[i.to_i] = 0\n end\n \n end\n puts out\nend \ntest"}], "src_uid": "a6cbf01d72d607ca95fe16df4fb16693"} {"nl": {"description": "At the school where Vasya is studying, preparations are underway for the graduation ceremony. One of the planned performances is a ball, which will be attended by pairs of boys and girls.Each class must present two couples to the ball. In Vasya's class, $$$a$$$ boys and $$$b$$$ girls wish to participate. But not all boys and not all girls are ready to dance in pairs.Formally, you know $$$k$$$ possible one-boy-one-girl pairs. You need to choose two of these pairs so that no person is in more than one pair.For example, if $$$a=3$$$, $$$b=4$$$, $$$k=4$$$ and the couples $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 2)$$$, $$$(3, 4)$$$ are ready to dance together (in each pair, the boy's number comes first, then the girl's number), then the following combinations of two pairs are possible (not all possible options are listed below): $$$(1, 3)$$$ and $$$(2, 2)$$$; $$$(3, 4)$$$ and $$$(1, 3)$$$; But the following combinations are not possible: $$$(1, 3)$$$ and $$$(1, 2)$$$ — the first boy enters two pairs; $$$(1, 2)$$$ and $$$(2, 2)$$$ — the second girl enters two pairs; Find the number of ways to select two pairs that match the condition above. Two ways are considered different if they consist of different pairs.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains three integers $$$a$$$, $$$b$$$ and $$$k$$$ ($$$1 \\le a, b, k \\le 2 \\cdot 10^5$$$) — the number of boys and girls in the class and the number of couples ready to dance together. The second line of each test case contains $$$k$$$ integers $$$a_1, a_2, \\ldots a_k$$$. ($$$1 \\le a_i \\le a$$$), where $$$a_i$$$ is the number of the boy in the pair with the number $$$i$$$. The third line of each test case contains $$$k$$$ integers $$$b_1, b_2, \\ldots b_k$$$. ($$$1 \\le b_i \\le b$$$), where $$$b_i$$$ is the number of the girl in the pair with the number $$$i$$$. It is guaranteed that the sums of $$$a$$$, $$$b$$$, and $$$k$$$ over all test cases do not exceed $$$2 \\cdot 10^5$$$. It is guaranteed that each pair is specified at most once in one test case.", "output_spec": "For each test case, on a separate line print one integer — the number of ways to choose two pairs that match the condition above.", "sample_inputs": ["3\n3 4 4\n1 1 2 3\n2 3 2 4\n1 1 1\n1\n1\n2 2 4\n1 1 2 2\n1 2 1 2"], "sample_outputs": ["4\n0\n2"], "notes": "NoteIn the first test case, the following combinations of pairs fit: $$$(1, 2)$$$ and $$$(3, 4)$$$; $$$(1, 3)$$$ and $$$(2, 2)$$$; $$$(1, 3)$$$ and $$$(3, 4)$$$; $$$(2, 2)$$$ and $$$(3, 4)$$$. There is only one pair in the second test case.In the third test case, the following combinations of pairs fit: $$$(1, 1)$$$ and $$$(2, 2)$$$; $$$(1, 2)$$$ and $$$(2, 1)$$$. "}, "positive_code": [{"source_code": "tcases = gets.chomp.to_i\r\ndef solve()\r\n a, b, k = *(gets.chomp.split.map(&:to_i))\r\n boys = gets.chomp.split.map(&:to_i)\r\n girls = gets.chomp.split.map(&:to_i)\r\n boy_out = Array.new(a + 1, 0)\r\n girls_in = Array.new(b + 1, 0)\r\n boys.each do |i|\r\n boy_out[i] += 1\r\n end\r\n girls.each do |i|\r\n girls_in[i] += 1\r\n end\r\n # puts \"boy_out = #{boy_out.join(\" \")}\"\r\n # puts \"girls_out = #{girls_in.join(\" \")}\"\r\n ans = 0\r\n 0.upto k-1 do |i|\r\n b, g = boys[i], girls[i]\r\n bout, gin = boy_out[b], girls_in[g]\r\n ans += k - (bout + gin - 1)\r\n end\r\n # puts \"ans = \"\r\n puts ans / 2\r\nend\r\ntcases.times do solve() end\r\n"}, {"source_code": "gets.to_i.times do\r\n aa,bb,k=gets.split.map(&:to_i)\r\n a=gets.split.map(&:to_i).sort\r\n b=gets.split.map(&:to_i).sort\r\n c=0\r\n q=0\r\n cc=0\r\n a.each do |ai|\r\n if ai==q\r\n cc+=1\r\n else\r\n c+=cc*(cc-1)/2\r\n cc=1\r\n q=ai\r\n end\r\n end\r\n c+=cc*(cc-1)/2\r\n q=0\r\n cc=0\r\n b.each do |ai|\r\n if ai==q\r\n cc+=1\r\n else\r\n c+=cc*(cc-1)/2\r\n cc=1\r\n q=ai\r\n end\r\n end\r\n c+=cc*(cc-1)/2\r\n puts k*(k-1)/2-c\r\nend\r\n"}], "negative_code": [], "src_uid": "14ce451a31c0dbc2b2f4e04a939b199d"} {"nl": {"description": "Maksim has $$$n$$$ objects and $$$m$$$ boxes, each box has size exactly $$$k$$$. Objects are numbered from $$$1$$$ to $$$n$$$ in order from left to right, the size of the $$$i$$$-th object is $$$a_i$$$.Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $$$i$$$-th object fits in the current box (the remaining size of the box is greater than or equal to $$$a_i$$$), he puts it in the box, and the remaining size of the box decreases by $$$a_i$$$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).", "input_spec": "The first line of the input contains three integers $$$n$$$, $$$m$$$, $$$k$$$ ($$$1 \\le n, m \\le 2 \\cdot 10^5$$$, $$$1 \\le k \\le 10^9$$$) — the number of objects, the number of boxes and the size of each box. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le k$$$), where $$$a_i$$$ is the size of the $$$i$$$-th object.", "output_spec": "Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.", "sample_inputs": ["5 2 6\n5 2 1 4 2", "5 1 4\n4 2 3 4 1", "5 3 3\n1 2 3 1 1"], "sample_outputs": ["4", "1", "5"], "notes": "NoteIn the first example Maksim can pack only $$$4$$$ objects. Firstly, he tries to pack all the $$$5$$$ objects. Distribution of objects will be $$$[5], [2, 1]$$$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $$$[2, 1], [4, 2]$$$. So the answer is $$$4$$$.In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $$$[4]$$$), but he can pack the last object ($$$[1]$$$).In the third example Maksim can pack all the objects he has. The distribution will be $$$[1, 2], [3], [1, 1]$$$."}, "positive_code": [{"source_code": "\ndef solution\n\n _, m, k = read_ints\n objects = read_ints.reverse\n\n used_boxes = 1\n crt_size = k\n packed = 0\n\n for o in objects\n if o <= crt_size\n packed += 1\n crt_size -= o\n else\n if used_boxes < m\n used_boxes += 1\n packed += 1\n crt_size = k - o\n else\n break\n end\n end\n end\n\n puts packed\n\nend\n\ndef read_int\n gets.to_i\nend\n\ndef read_ints\n gets.split.map(&:to_i)\nend\n\ndef read_string\n gets.chomp\nend\n\nsolution unless ENV['TEST__MODE']\n\n"}, {"source_code": "n,m,k = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\nans = 0\n\ncap = k\nm -= 1\n(n-1).downto(0) do |i|\n if a[i] <= cap\n cap -= a[i]\n ans += 1\n elsif m != 0 && a[i] <= k\n m -= 1\n cap = k-a[i]\n ans += 1\n else\n break\n end\nend\n\nputs ans\n"}], "negative_code": [], "src_uid": "869f8763211a7771ecd73d56b5c34479"} {"nl": {"description": "In Arcady's garden there grows a peculiar apple-tree that fruits one time per year. Its peculiarity can be explained in following way: there are n inflorescences, numbered from 1 to n. Inflorescence number 1 is situated near base of tree and any other inflorescence with number i (i > 1) is situated at the top of branch, which bottom is pi-th inflorescence and pi < i.Once tree starts fruiting, there appears exactly one apple in each inflorescence. The same moment as apples appear, they start to roll down along branches to the very base of tree. Each second all apples, except ones in first inflorescence simultaneously roll down one branch closer to tree base, e.g. apple in a-th inflorescence gets to pa-th inflorescence. Apples that end up in first inflorescence are gathered by Arcady in exactly the same moment. Second peculiarity of this tree is that once two apples are in same inflorescence they annihilate. This happens with each pair of apples, e.g. if there are 5 apples in same inflorescence in same time, only one will not be annihilated and if there are 8 apples, all apples will be annihilated. Thus, there can be no more than one apple in each inflorescence in each moment of time.Help Arcady with counting number of apples he will be able to collect from first inflorescence during one harvest.", "input_spec": "First line of input contains single integer number n (2 ≤ n ≤ 100 000)  — number of inflorescences. Second line of input contains sequence of n - 1 integer numbers p2, p3, ..., pn (1 ≤ pi < i), where pi is number of inflorescence into which the apple from i-th inflorescence rolls down.", "output_spec": "Single line of output should contain one integer number: amount of apples that Arcady will be able to collect from first inflorescence during one harvest.", "sample_inputs": ["3\n1 1", "5\n1 2 2 2", "18\n1 1 1 4 4 3 2 2 2 10 8 9 9 9 10 10 4"], "sample_outputs": ["1", "3", "4"], "notes": "NoteIn first example Arcady will be able to collect only one apple, initially situated in 1st inflorescence. In next second apples from 2nd and 3rd inflorescences will roll down and annihilate, and Arcady won't be able to collect them.In the second example Arcady will be able to collect 3 apples. First one is one initially situated in first inflorescence. In a second apple from 2nd inflorescence will roll down to 1st (Arcady will collect it) and apples from 3rd, 4th, 5th inflorescences will roll down to 2nd. Two of them will annihilate and one not annihilated will roll down from 2-nd inflorescence to 1st one in the next second and Arcady will collect it."}, "positive_code": [{"source_code": "n = gets.to_i\np = [0,1] + gets.split.map(&:to_i)\ndist = [0,0]\ngp = Array.new(n+1,0)\ngp[0] = 1\n2.upto(n) do |i|\n dist[i] = dist[p[i]] + 1\n gp[dist[i]] += 1\nend\nans = 0\ngp.each do |e|\n ans += 1 if e % 2 == 1\nend\np ans\n"}, {"source_code": "n = gets.chomp.to_i\narr = gets.chomp.split(\" \").map {|e| e.to_i}\n\nclass Node\n\tattr_accessor :value, :children, :degree\n\n\tdef initialize value\n\t\t@value = value\n\t\t@children = []\n\tend\nend\nan = Array.new(arr.length+1) { |i| Node.new(i+1)}\ni = 0\n\nwhile i < arr.length\n\tk = i+1\n\tan[arr[i]-1].children.push(an[k])\n\ti+=1\nend\nan.first.degree = 0\ndegree = [1]\nqueue = [an.first]\nwhile queue.length > 0\n\tel = queue.shift\n\tel.children.each do |c|\n\t\tc.degree = el.degree+1\n\t\tdegree[c.degree] ||= 0\n\t\tdegree[c.degree]+=1\n\t\tqueue.push(c)\n\tend\nend\nsum = 0\ndegree.each do |e|\n\tif e % 2 != 0\n\t\tsum+=1\n\tend\nend\nputs sum\n\n\n\n"}], "negative_code": [], "src_uid": "a4563e6aea9126e20e7a33df664e3171"} {"nl": {"description": "You are a usual chat user on the most famous streaming platform. Of course, there are some moments when you just want to chill and spam something.More precisely, you want to spam the emote triangle of size $$$k$$$. It consists of $$$2k-1$$$ messages. The first message consists of one emote, the second one — of two emotes, ..., the $$$k$$$-th one — of $$$k$$$ emotes, the $$$k+1$$$-th one — of $$$k-1$$$ emotes, ..., and the last one — of one emote.For example, the emote triangle for $$$k=3$$$ consists of $$$5$$$ messages: Of course, most of the channels have auto moderation. Auto moderator of the current chat will ban you right after you spam at least $$$x$$$ emotes in succession (you can assume you are the only user in the chat). Now you are interested — how many messages will you write before getting banned? Or maybe you will not get banned at all (i.e. will write all $$$2k-1$$$ messages and complete your emote triangle successfully)? Note that if you get banned as a result of writing a message, this message is also counted.You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. The next $$$t$$$ lines describe test cases. The only line of the test case contains integers $$$k$$$ and $$$x$$$ ($$$1 \\le k \\le 10^9; 1 \\le x \\le 10^{18}$$$).", "output_spec": "For each test case, print the number of messages you will write before getting banned for the corresponding values $$$k$$$ and $$$x$$$.", "sample_inputs": ["7\n4 6\n4 7\n1 2\n3 7\n2 5\n100 1\n1000000000 923456789987654321"], "sample_outputs": ["3\n4\n1\n4\n3\n1\n1608737403"], "notes": "NoteLet's analyze the test cases of the example. In the first test case, you write three messages containing $$$1$$$, $$$2$$$ and $$$3$$$ emotes respectively, and since $$$1 + 2 + 3 \\ge 6$$$, you get banned after that. In the second test case, you write four messages containing $$$1$$$, $$$2$$$, $$$3$$$ and $$$4$$$ emotes respectively, and since $$$1 + 2 + 3 + 4 \\ge 7$$$, you get banned after that. In the third test case, you write one message containing exactly $$$1$$$ emote. It doesn't get you banned, since $$$1 < 2$$$, but you have already finished posting your emote triangle. So you wrote one message successfully. In the fourth test case, you write four messages containing $$$1$$$, $$$2$$$, $$$3$$$ and $$$2$$$ emotes respectively, and since $$$1 + 2 + 3 + 2 \\ge 7$$$, you get banned after that. In the fifth test case, you write three messages containing $$$1$$$, $$$2$$$ and $$$1$$$ emote respectively. It doesn't get you banned, since $$$1 + 2 + 1 < 5$$$, but you have already finished posting your emote triangle. So you wrote three messages successfully. In the sixth test case, since $$$x = 1$$$, you get banned as soon as you send your first message. The seventh test case is too large to analyze, so we'll skip it. "}, "positive_code": [{"source_code": "main = -> {\n t = int\n t.times do\n k, x = ints\n # first, print k lines\n ac = 0\n wa = k * 2 + 1\n while wa - ac > 1\n wj = (ac + wa) >> 1\n # wj lines\n emotes = 0\n last = 0\n if wj > k\n emotes += k * (k + 1) / 2\n m = wj - k\n emotes += (k - 1 + k - m) * m / 2\n last = k - m\n else\n emotes += wj * (wj + 1) / 2\n last = wj\n end\n if emotes - last >= x\n wa = wj\n else\n ac = wj\n end\n end\n\n puts [ac, 2 * k - 1].min\n end\n}\n\nDEBUG = true\nMOD = 10**9+7\nYESNO = %w(No Yes)\nINF = 10**20\n\nrequire \"set\"\nrequire \"prime\"\n\ndef int; gets.to_s.to_i end\ndef ints; gets.to_s.split.map { |s| s.to_i } end\ndef int1s; gets.to_s.split.map { |s| s.to_i - 1 } end\ndef float; gets.to_s.to_f end\ndef floats; gets.to_s.split.map { |s| s.to_f } end\ndef array_of(&convert); gets.to_s.split.map(&convert) end\ndef string; gets.to_s.chomp end\ndef rep(n, &b); Array.new(n, &b) end\ndef yes; puts YESNO[1] end\ndef no; puts YESNO[0] end\ndef yesno t; puts YESNO[t] end\ndef zip(xs, *yss); Enumerator.new { |y| xs.zip(*yss) { |a| y.yield(*a) } } end\ndef max(*xs, &block); block_given? ? xs.max_by(&block) : xs.max end\ndef min(*xs, &block); block_given? ? xs.min_by(&block) : xs.min end\ndef minmax(*xs, &block); block_given? ? xs.minmax_by(&block) : xs.minmax end\ndef gcd(*xs); xs.inject(0, :gcd) end\ndef matrix(h, w, fill=nil, &block); return Array.new(h) { Array.new(w, &block) } if block_given?; Array.new(h) { [fill] * w } end\ndef debug(x = nil); STDERR.puts (block_given? ? yield(x) : x).inspect if DEBUG; x end\ndef debug_grid(grid, width = 1); grid.each { |row| STDERR.puts row.map { |x| x.inspect.ljust(width) }.join(\"\") } if DEBUG; grid end\ndef if_debug; yield if DEBUG end\n\nmodule Boolean\n def coerce(other); [other, to_i] end\n def +@; to_i end\n def to_int; to_i end\n def *(other); to_i * other end\nend\n\nclass TrueClass\n include Boolean\n def to_i; 1 end\nend\n\nclass FalseClass\n include Boolean\n def to_i; 0 end\nend\n\nclass Integer\n def div_ceil(y); (self + y - 1) / y end\n def mod_inv(mod = MOD); pow(mod-2, mod) end\n def mod_div(y, mod = MOD); self * mod_inv(y, mod) % mod end\n def factorial(mod = MOD); (2..self).inject(1) { |f, x| f * x % mod } end\n def popcount; x = self; c = 0; while x > 0; c += 1 if x & 1 == 1; x >>= 1 end; c end #TODO: faster\n def bitbrute(&block); (1< 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\n def lower_bound; ac, wa = self.end_open, self.begin; while ac - wa > 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\n def shakutori(r2, &pred); Enumerator.new { |y| j, r = r2.begin, r2.end_open; each { |i| j += 1 while j + 1 < r and pred[i, j+1]; y.yield(i, j) } }; end\n def widest(&block); Enumerator.new { |y| j, n = self.begin, self.end_open; each { |i| j += 1 while j < n and block[i, j]; y.yield(i, j) if block[i, j] } } end\nend\n\nclass Array\n def power(&block); (0 ... 1 << size).each(&block) end\n def sorted_merge(other); a = []; i = j = 0; n, m = size, other.size; if j < m and other[j] < self[i]; a << other[j]; j += 1 else; a << self[i]; i += 1 end while i < n; a.push(*other[j..-1]) if j < m; a end\n def upper_bound; ac, wa = 0, size; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\n def lower_bound; ac, wa = size, 0; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\n def cum(*xs, &op); a = []; a << xs[0] if xs.size > 0; a << x = self[0]; (1...size).each { |i| a << x = op[x, self[i]] }; a end\n def cumdiff(range); self[range.end_open] - self[range.begin]; end\nend\n\nmodule Enumerable\n def sorted_uniq; x = nil; filter { |y| c = x === y; x = y; !c } end\n def cumsum; ys = [0]; each { |x| ys << x + ys[-1] }; ys end\nend\n\nmain[]"}], "negative_code": [], "src_uid": "bf21c4809cd10904f05d531dd7af4ab5"} {"nl": {"description": "Slavic has an array of length $$$n$$$ consisting only of zeroes and ones. In one operation, he removes either the first or the last element of the array. What is the minimum number of operations Slavic has to perform such that the total sum of the array is equal to $$$s$$$ after performing all the operations? In case the sum $$$s$$$ can't be obtained after any amount of operations, you should output -1.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \\leq n, s \\leq 2 \\cdot 10^5$$$) — the length of the array and the needed sum of elements. The second line of each test case contains $$$n$$$ integers $$$a_i$$$ ($$$0 \\leq a_i \\leq 1$$$) — the elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, output a single integer — the minimum amount of operations required to have the total sum of the array equal to $$$s$$$, or -1 if obtaining an array with sum $$$s$$$ isn't possible.", "sample_inputs": ["7\n\n3 1\n\n1 0 0\n\n3 1\n\n1 1 0\n\n9 3\n\n0 1 0 1 1 1 0 0 1\n\n6 4\n\n1 1 1 1 1 1\n\n5 1\n\n0 0 1 1 0\n\n16 2\n\n1 1 0 0 1 0 0 1 1 0 0 0 0 0 1 1\n\n6 3\n\n1 0 1 0 0 0"], "sample_outputs": ["0\n1\n3\n2\n2\n7\n-1"], "notes": "NoteIn the first test case, the sum of the whole array is $$$1$$$ from the beginning, so we don't have to make any operations.In the second test case, the sum of the array is $$$2$$$ and we want it to be equal to $$$1$$$, so we should remove the first element. The array turns into $$$[1, 0]$$$, which has a sum equal to $$$1$$$.In the third test case, the sum of the array is $$$5$$$ and we need it to be $$$3$$$. We can obtain such a sum by removing the first two elements and the last element, doing a total of three operations. The array turns into $$$[0, 1, 1, 1, 0, 0]$$$, which has a sum equal to $$$3$$$."}, "positive_code": [{"source_code": "bsearch_last = ->range, map, target {\r\n l, r = range.begin, range.end + 1\r\n while l < r\r\n m = (l + r) / 2\r\n if map[m] > target\r\n r = m\r\n else\r\n l = m + 1\r\n end\r\n end\r\n map[r - 1] == target ? r - 1 : nil\r\n}\r\n\r\ngets.to_i.times do\r\n n, s = gets.split.map(&:to_i)\r\n xs = gets.split.map(&:to_i)\r\n ps = [0]\r\n xs.each { |x| ps << x + ps.last }\r\n sum_inclusive = ->(from, to) { ps[to + 1] - ps[from] }\r\n answer = (0..n - 1).filter_map do |from|\r\n to = bsearch_last[from..n - 1, ->to { sum_inclusive[from, to] }, s]\r\n to ? n - (to - from + 1) : nil\r\n end.min || -1\r\n p answer\r\nend\r\n"}, {"source_code": "gets.to_i.times do\r\n n, s = gets.split.map(&:to_i)\r\n xs = gets.split.map(&:to_i)\r\n map = {}\r\n sum, max = 0, -1\r\n xs.each.with_index do |x, i|\r\n sum += x\r\n max = i + 1 if sum == s\r\n map[sum] ||= i\r\n if j = map[sum - s]\r\n max = [max, i - j].max\r\n end\r\n end\r\n p max == -1 ? -1 : n - max\r\nend\r\n"}, {"source_code": "def solve(arr, s)\n len = arr.size\n\n len.times do |i|\n arr[i] += arr[i - 1] if i > 0\n end\n\n max_result = -1\n\n if arr[-1] < s\n return -1\n end\n\n len.times do |left|\n result = binsearch(arr, left, s)\n # p [left, result, (result - left + 1)]\n\n if (result - left + 1) > max_result\n max_result = result - left + 1\n end\n end\n\n len - max_result\nend\n\ndef binsearch(arr, left, a)\n l = left - 1\n r = arr.size\n\n while l < r - 1\n m = (l + r) / 2\n if (arr[m] - (left > 0 ? arr[left - 1] : 0)) <= a\n l = m\n else\n r = m\n end\n end\n\n l\nend\n\nn = gets.to_i\n\nn.times do\n size, s = gets.split.map(&:to_i)\n array = Array.new(size)\n array = gets.split.map(&:to_i)\n\n p solve(array, s)\nend\n"}, {"source_code": "def solve\r\n q = gets.split.map(&:to_i)\r\n a = gets.split.map(&:to_i)\r\n n = q[0]\r\n s = q[1]\r\n sum = 0\r\n a.each do |x|\r\n sum = sum + x\r\n end\r\n if s > sum\r\n puts \"-1\"\r\n return\r\n end\r\n l = 1\r\n r = a.length\r\n while l < r\r\n mid = (l + r + 1) / 2\r\n ok = false\r\n pref = Array.new(n + 1)\r\n pref[0] = 0\r\n i = 1\r\n a.each do |x|\r\n pref[i] = pref[i - 1]\r\n pref[i] = pref[i] + x\r\n if (i - mid) >= 0\r\n curr = pref[i] - pref[i - mid]\r\n if curr <= s\r\n ok = true\r\n end\r\n end\r\n i = i + 1\r\n end\r\n if ok == true\r\n l = mid\r\n else\r\n r = mid - 1\r\n end\r\n end\r\n puts n - l\r\nend\r\n\r\ngets.to_i.times do\r\n solve\r\nend"}, {"source_code": "gets.to_i.times do\r\n n, s = gets.split.map(&:to_i)\r\n a = gets.split.map(&:to_i)\r\n\r\n cnt = 0\r\n l = 0\r\n inf = 10101010101010\r\n ans = inf\r\n n.times do |r|\r\n cnt += a[r]\r\n while l < r && cnt > s\r\n cnt -= a[l]\r\n l += 1\r\n end\r\n\r\n ans = [ans, n-(r-l+1)].min if cnt == s\r\n end\r\n\r\n puts ans == inf ? -1 : ans\r\nend\r\n\r\n"}, {"source_code": "main = -> {\r\n t = int\r\n t.times do\r\n n, s = ints\r\n a = ints\r\n a_sum = [0]\r\n a.each do |x|\r\n a_sum << a_sum[-1] + x\r\n end\r\n \r\n sum = a_sum[-1]\r\n \r\n if s > sum\r\n puts -1\r\n next\r\n end\r\n \r\n has_to_remove = sum - s\r\n \r\n ans = INF\r\n (0 ... n).each do |i|\r\n removed = a_sum[i]\r\n break if removed > has_to_remove\r\n j = (0 .. n).bsearch { |m| sum - a_sum[n - m] >= has_to_remove - removed } || 0\r\n ans = i + j if i + j < ans\r\n end\r\n \r\n puts ans\r\n end\r\n}\r\n\r\nDEBUG = false\r\nMOD = 10**9+7\r\nINF = 10**18\r\n$yesno = %w(No Yes)\r\n$YesNo = %w(No Yes)\r\n$YESNO = %w(NO YES)\r\n\r\nrequire \"set\"\r\nrequire \"prime\"\r\n\r\ndef int; gets.to_s.to_i end\r\ndef ints; gets.to_s.split.map { |s| s.to_i } end\r\ndef int1s; gets.to_s.split.map { |s| s.to_i - 1 } end\r\ndef float; gets.to_s.to_f end\r\ndef floats; gets.to_s.split.map { |s| s.to_f } end\r\ndef array_of(&convert); gets.to_s.split.map(&convert) end\r\ndef string; gets.to_s.chomp end\r\ndef strings; gets.to_s.split end\r\ndef rep(n, &b); Array.new(n, &b) end\r\ndef yes; puts $yesno[1] end\r\ndef no; puts $yesno[0] end\r\ndef yesno t; puts $yesno[t ? 1 : 0] end\r\ndef YesNo t; puts $YesNo[t ? 1 : 0] end\r\ndef YESNO t; puts $YESNO[t ? 1 : 0] end\r\ndef zip(xs, *yss); Enumerator.new { |y| xs.zip(*yss) { |a| y.yield(*a) } } end\r\ndef max(*xs, &block); block_given? ? xs.max_by(&block) : xs.max end\r\ndef min(*xs, &block); block_given? ? xs.min_by(&block) : xs.min end\r\ndef minmax(*xs, &block); block_given? ? xs.minmax_by(&block) : xs.minmax end\r\ndef gcd(*xs); xs.inject(0, :gcd) end\r\ndef matrix(h, w, fill=nil, &block); return Array.new(h) { Array.new(w, &block) } if block_given?; Array.new(h) { [fill] * w } end\r\ndef debug(x = nil); STDERR.puts (block_given? ? yield(x) : x).inspect if DEBUG; x end\r\ndef debug_grid(grid, width = 1); grid.each { |row| STDERR.puts row.map { |x| x.inspect.ljust(width) }.join(\"\") } if DEBUG; grid end\r\ndef if_debug; yield if DEBUG end\r\n\r\nmodule Boolean\r\n def coerce(other); [other, to_i] end\r\n def +@; to_i end\r\n def to_int; to_i end\r\n def *(other); to_i * other end\r\nend\r\n\r\nclass TrueClass\r\n include Boolean\r\n def to_i; 1 end\r\nend\r\n\r\nclass FalseClass\r\n include Boolean\r\n def to_i; 0 end\r\nend\r\n\r\nclass Integer\r\n def div_ceil(y); (self + y - 1) / y end\r\n def mod_inv(mod = MOD); pow(mod-2, mod) end\r\n def mod_div(y, mod = MOD); self * y.mod_inv(mod) % mod end\r\n def mod_nCr(r, mod = MOD); x = y = 1; (1..r).each { |i| x = x * (self + 1 - i) % mod; y = y * i % mod }; x.mod_div(y, mod); end\r\n def factorial(mod = MOD); (2..self).inject(1) { |f, x| f * x % mod } end\r\n def popcount; x = self; c = 0; while x > 0; c += 1 if x & 1 == 1; x >>= 1 end; c end #TODO: faster\r\n def bitbrute(&block); (1< 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\r\n def lower_bound; ac, wa = self.end_open, self.begin; while ac - wa > 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\r\n def shakutori(r2, &pred); Enumerator.new { |y| j, r = r2.begin, r2.end_open; each { |i| j += 1 while j + 1 < r and pred[i, j+1]; y.yield(i, j) } }; end\r\n def widest(&block); Enumerator.new { |y| j, n = self.begin, self.end_open; each { |i| j += 1 while j < n and block[i, j]; y.yield(i, j) if block[i, j] } } end\r\nend\r\n\r\nclass Array\r\n def power(&block); (0 ... 1 << size).each(&block) end\r\n def sorted_merge(other); a = []; i = j = 0; n, m = size, other.size; if j < m and other[j] < self[i]; a << other[j]; j += 1 else; a << self[i]; i += 1 end while i < n; a.push(*other[j..-1]) if j < m; a end\r\n def upper_bound; ac, wa = 0, size; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\r\n def lower_bound; ac, wa = size, 0; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\r\n def cum(*xs, &op); a = []; a << xs[0] if xs.size > 0; a << x = self[0]; (1...size).each { |i| a << x = op[x, self[i]] }; a end\r\n def cumdiff(range); self[range.end_open] - self[range.begin]; end\r\n def compress(kinds = uniq.sort!); map { |x| kinds.bsearch_index { _1 >= x } }; end\r\nend\r\n\r\nmodule Enumerable\r\n def sorted_uniq; x = nil; filter { |y| c = x === y; x = y; !c } end\r\n def cumsum; ys = [0]; each { |x| ys << x + ys[-1] }; ys end\r\nend\r\n\r\nmain[]"}], "negative_code": [{"source_code": "bsearch_last = ->range, block {\r\n i = (0..range.end - range.begin).bsearch { |i| block[range.end - i] }\r\n i ? range.end - i : i\r\n}\r\n\r\ngets.to_i.times do\r\n n, s = gets.split.map(&:to_i)\r\n xs = gets.split.map(&:to_i)\r\n ps = [0]\r\n xs.each { |x| ps << x + ps.last }\r\n sum_inclusive = ->(from, to) { ps[to + 1] - ps[from] }\r\n answer = (0..n - 1).filter_map do |from|\r\n to = bsearch_last[from..n - 1, ->to { sum_inclusive[from, to] == s }]\r\n to ? n - (to - from + 1) : nil\r\n end.min || -1\r\n p answer\r\nend\r\n"}], "src_uid": "68adf23485d9db9a254ab73d4f07bd62"} {"nl": {"description": "Polycarpus just has been out of luck lately! As soon as he found a job in the \"Binary Cat\" cafe, the club got burgled. All ice-cream was stolen.On the burglary night Polycarpus kept a careful record of all club visitors. Each time a visitor entered the club, Polycarpus put down character \"+\" in his notes. Similarly, each time a visitor left the club, Polycarpus put character \"-\" in his notes. We know that all cases of going in and out happened consecutively, that is, no two events happened at the same time. Polycarpus doesn't remember whether there was somebody in the club at the moment when his shift begun and at the moment when it ended.Right now the police wonders what minimum number of distinct people Polycarpus could have seen. Assume that he sees anybody coming in or out of the club. Each person could have come in or out an arbitrary number of times.", "input_spec": "The only line of the input contains a sequence of characters \"+\" and \"-\", the characters are written one after another without any separators. The characters are written in the order, in which the corresponding events occurred. The given sequence has length from 1 to 300 characters, inclusive.", "output_spec": "Print the sought minimum number of people", "sample_inputs": ["+-+-+", "---"], "sample_outputs": ["1", "3"], "notes": null}, "positive_code": [{"source_code": "s = gets.chomp\nmanin = 0\nmanout = 0\ns.size.times do |i|\n if s[i] == \"+\" then\n manin += 1\n if manout != 0 then\n manout -= 1\n end\n else\n manout += 1\n if manin != 0 then\n manin -= 1\n end \n end\nend\nresult = manin + manout\nputs \"#{result}\""}, {"source_code": "s = gets.chomp!\n\nf = g = c =0; res = 0; s.each_char{|si|\n c = 1 if si == '+'; c = -1 if si == '-'\n f += c; g += c; f = 0 if f < 0; g = 0 if g > 0\n res = f if f > res; res = -g if -g > res\n}\n\np res"}, {"source_code": "tmp,mn,mx=0,0,0\ngets.to_s.chomp.each_char do |c|\n\tc==\"-\"?(tmp-=1):(tmp+=1)\n\tmn=tmp if tmp 'S', # Serine\n 'UCC' => 'S', # Serine\n 'UCG' => 'S', # Serine\n 'UCU' => 'S', # Serine\n 'UUC' => 'F', # Phenylalanine\n 'UUU' => 'F', # Phenylalanine\n 'UUA' => 'L', # Leucine\n 'UUG' => 'L', # Leucine\n 'UAC' => 'Y', # Uyrosine\n 'UAU' => 'Y', # Uyrosine\n 'UAA' => '_', # Stop\n 'UAG' => '_', # Stop\n 'UGC' => 'C', # Cysteine\n 'UGU' => 'C', # Cysteine\n 'UGA' => '_', # Stop\n 'UGG' => 'W', # Uryptophan\n 'CUA' => 'L', # Leucine\n 'CUC' => 'L', # Leucine\n 'CUG' => 'L', # Leucine\n 'CUU' => 'L', # Leucine\n 'CCA' => 'P', # Proline\n 'CCC' => 'P', # Proline\n 'CCG' => 'P', # Proline\n 'CCU' => 'P', # Proline\n 'CAC' => 'H', # Histidine\n 'CAU' => 'H', # Histidine\n 'CAA' => 'Q', # Glutamine\n 'CAG' => 'Q', # Glutamine\n 'CGA' => 'R', # Arginine\n 'CGC' => 'R', # Arginine\n 'CGG' => 'R', # Arginine\n 'CGU' => 'R', # Arginine\n 'AUA' => 'I', # Isoleucine\n 'AUC' => 'I', # Isoleucine\n 'AUU' => 'I', # Isoleucine\n 'AUG' => 'M', # Methionine\n 'ACA' => 'T', # Uhreonine\n 'ACC' => 'T', # Uhreonine\n 'ACG' => 'T', # Uhreonine\n 'ACU' => 'T', # Uhreonine\n 'AAC' => 'N', # Asparagine\n 'AAU' => 'N', # Asparagine\n 'AAA' => 'K', # Lysine\n 'AAG' => 'K', # Lysine\n 'AGC' => 'S', # Serine\n 'AGU' => 'S', # Serine\n 'AGA' => 'R', # Arginine\n 'AGG' => 'R', # Arginine\n 'GUA' => 'V', # Valine\n 'GUC' => 'V', # Valine\n 'GUG' => 'V', # Valine\n 'GUU' => 'V', # Valine\n 'GCA' => 'A', # Alanine\n 'GCC' => 'A', # Alanine\n 'GCG' => 'A', # Alanine\n 'GCU' => 'A', # Alanine\n 'GAC' => 'D', # Aspartic Acid\n 'GAU' => 'D', # Aspartic Acid\n 'GAA' => 'E', # Glutamic Acid\n 'GAG' => 'E', # Glutamic Acid\n 'GGA' => 'G', # Glycine\n 'GGC' => 'G', # Glycine\n 'GGG' => 'G', # Glycine\n 'GGU' => 'G' # Glycine \n}\n\ns = gets.chomp!\n\nf = g = c =0; res = 0; s.each_char{|si|\n\tc = 1 if si == '+'; c = -1 if si == '-'\n\tf += c; g += c; f = 0 if f < 0; g = 0 if g > 0\n\tres = f if f > res; res = -g if -g > res\n}\n\np res"}, {"source_code": "dict = {\n 'UCA' => 'S', # Serine\n 'UCC' => 'S', # Serine\n 'UCG' => 'S', # Serine\n 'UCU' => 'S', # Serine\n 'UUC' => 'F', # Phenylalanine\n 'UUU' => 'F', # Phenylalanine\n 'UUA' => 'L', # Leucine\n 'UUG' => 'L', # Leucine\n 'UAC' => 'Y', # Uyrosine\n 'UAU' => 'Y', # Uyrosine\n 'UAA' => '_', # Stop\n 'UAG' => '_', # Stop\n 'UGC' => 'C', # Cysteine\n 'UGU' => 'C', # Cysteine\n 'UGA' => '_', # Stop\n 'UGG' => 'W', # Uryptophan\n 'CUA' => 'L', # Leucine\n 'CUC' => 'L', # Leucine\n 'CUG' => 'L', # Leucine\n 'CUU' => 'L', # Leucine\n 'CCA' => 'P', # Proline\n 'CCC' => 'P', # Proline\n 'CCG' => 'P', # Proline\n 'CCU' => 'P', # Proline\n 'CAC' => 'H', # Histidine\n 'CAU' => 'H', # Histidine\n 'CAA' => 'Q', # Glutamine\n 'CAG' => 'Q', # Glutamine\n 'CGA' => 'R', # Arginine\n 'CGC' => 'R', # Arginine\n 'CGG' => 'R', # Arginine\n 'CGU' => 'R', # Arginine\n 'AUA' => 'I', # Isoleucine\n 'AUC' => 'I', # Isoleucine\n 'AUU' => 'I', # Isoleucine\n 'AUG' => 'M', # Methionine\n 'ACA' => 'T', # Uhreonine\n 'ACC' => 'T', # Uhreonine\n 'ACG' => 'T', # Uhreonine\n 'ACU' => 'T', # Uhreonine\n 'AAC' => 'N', # Asparagine\n 'AAU' => 'N', # Asparagine\n 'AAA' => 'K', # Lysine\n \n 'GGC' => 'G', # Glycine\n 'GGG' => 'G', # Glycine\n 'GGU' => 'G' # Glycine \n}\n\ns = gets.chomp!\n\nf = g = c =0; res = 0; s.each_char{|si|\n c = 1 if si == '+'; c = -1 if si == '-'\n f += c; g += c; f = 0 if f < 0; g = 0 if g > 0\n res = f if f > res; res = -g if -g > res\n}\n\np res\n"}, {"source_code": "s=gets.chomp\nprev=s[0]\nres=[]\nk=1\nres<pos\n neg=count if count 0 then return x\n else return -x\n end\nend\n\ns = gets.chomp\nplus = 0\nminus = 0\nk = false\nt = false\ns.size.times do |i|\n if s[i] == \"+\" then \n plus += 1\n k = true\n else\n minus += 1\n t = true\n end\nend\nman = abs(plus-minus)\nif k && t then\n man += 1\nend\nputs \"#{man}\""}, {"source_code": "def dif(s,i)\n k = i\n while i < s.size && s[i] != s[i-1] do\n i += 1\n end\n return i-k\nend\n\ndef find(x, s)\n x.each_index do |i|\n if x[i] == s then\n return i\n end\n end\n return false\nend\n\ns = gets.chomp\ni = 1\nman = [s[0]]\nwhile i < s.size do\n if s[i] != s[i-1] then\n t = find(man, s[i])\n k = dif(s,i)\n i += k\n if t then\n man[t] = s[i-1]\n end\n else\n while s[i] == s[i-1] do\n if s[i] == \"+\" then\n t = find(man, \"-\")\n else\n t = find(man, \"+\")\n end\n if t then\n man[t] = s[i]\n else\n man += [s[i]]\n end\n i += 1\n end\n end\nend\nputs \"#{man.size}\"\n"}, {"source_code": "def abs(x)\n if x > 0 then return x\n else return -x\n end\nend\n\ns = gets.chomp\nplus = 0\nminus = 0\ns.size.times do |i|\n if s[i] == \"+\" then \n plus += 1\n else\n minus += 1\n end\nend\nif abs(plus-minus) != 0 then\n man = abs(plus-minus)\nelse\n man = 1\nend\nputs \"#{man}\""}, {"source_code": "\ndef dif(s,i)\n k = i\n while i < s.size && s[i] != s[i-1] do\n i += 1\n end\n return i-k\nend\n\ndef find(x, s)\n x.each_index do |i|\n if x[i] == s then\n return i\n end\n end\n return false\nend\n\ns = gets.chomp\ni = 1\nman = [s[0]]\nwhile i < s.size do\n if s[i] != s[i-1] then\n t = find(man, s[i])\n k = dif(s,i)\n i += k\n if t then\n man[t] = s[i-1]\n end\n else\n while s[i] == s[i-1] do\n if s[i] == \"+\" then\n t = find(man, \"-\")\n else\n t = find(man, \"+\")\n end\n if t then\n man[t] = s[i]\n else\n man += [s[i]]\n end\n i += 1\n end\n end\nend\nputs \"#{man.size}\""}, {"source_code": "def same(s, i)\n k = i\n while (i < s.size && s[i] == s[i-1]) do\n i += 1\n end\n return i-k\nend\n\ndef dif(s, i)\n k = i\n while (i < s.size && s[i] != s[i-1]) do\n i += 1\n end\n return i-k\nend\n\ndef alsame(s, i)\n if same(s,i) == s.size-1 then\n s = \"\"\n end\n return s\nend\n\ns = gets.chomp\nplus = 0\nminus = 0\nman = 0\ni = 1\nwhile s.size != 0 do\n k = s.size\n s = alsame(s,i)\n if s == \"\" then\n man += k\n else\n t = dif(s,i) \n s = s[t..-1]\n if t != 0 then\n man += 1\n s = s[1..-1]\n end\n k = s.size\n s = alsame(s,i)\n if s == \"\" then\n man += k\n else\n k = same(s,i)\n s = s[k..-1]\n man += k\n end\n end\nend\nputs \"#{man}\""}, {"source_code": "def dif(s,i)\n k = i\n while i < s.size && s[i] != s[i-1] do\n i += 1\n end\n return i-k\nend\n\ndef find(x, s)\n x.each_index do |i|\n if x[i] == s then\n return i\n end\n end\n return false\nend\n\ns = gets.chomp\ni = 1\nman = [s[0]]\nwhile i < s.size do\n if s[i] != s[i-1] then\n t = find(man, s[i])\n k = dif(s,i)\n i += k\n if t then\n man[t] = s[i-1]\n end\n else\n while s[i] == s[i-1] do\n if s[i] == \"+\" then\n t = find(man, \"-\")\n else\n t = find(man, \"+\")\n end\n if t then\n man[t] = s[i]\n else\n man += [s[i]]\n end\n i += 1\n end\n end\nend\nputs \"#{man.size}\""}, {"source_code": "s = gets\nplus = 0\nminus = 0\ns.size.times do |i|\n if s[i] == \"+\" then plus += 1\n else\n minus += 1\n end\nend\nman = 1\nman += (plus-minus).abs\nputs \"#{man}\""}, {"source_code": "a = gets.to_s.chomp.split(//)\nputs (a.find_all{|el|el==\"+\"}.size-a.find_all{|el|el==\"-\"}.size).abs\n"}, {"source_code": "s= gets.to_s.chomp\na=s.split(//)\nn= (a.find_all{|el|el==\"+\"}.size-a.find_all{|el|el==\"-\"}.size).abs\nt1=\"+\"*(s.size-1)\nt2=\"-\"*(s.size-1)\nwhile s.index(t1)==nil&&s.index(t2)==nil&&t1!=\"+\" do \nt1.chop!\nt2.chop!\nend\nputs [t1.size,n].max\n\t\n\t\n"}, {"source_code": "s= gets.to_s.chomp\na=s.split(//)\nn= (a.find_all{|el|el==\"+\"}.size-a.find_all{|el|el==\"-\"}.size).abs\nt1=\"+\"*(s.size-1)\nt2=\"-\"*(s.size-1)\nwhile s.index(t1)==nil&&s.index(t2)==nil&&t1!=\"+\" do \nt1.chop!\np t1\nt2.chop!\nend\nputs [t1.size,n].max\n\t\n\t\n"}, {"source_code": "dict = {\n 'UUG' => 'L', # Leucine\n 'UAC' => 'Y', # Uyrosine\n 'UAU' => 'Y', # Uyrosine\n 'UAA' => '_', # Stop\n 'UAG' => '_', # Stop\n 'UGC' => 'C', # Cysteine\n 'UGU' => 'C', # Cysteine\n 'CCU' => 'P', # Proline\n 'CAC' => 'H', # Histidine\n 'CAU' => 'H', # Histidine\n 'CAA' => 'Q', # Glutamine\n 'CAG' => 'Q', # Glutamine\n 'CGA' => 'R', # Arginine\n 'CGC' => 'R', # Arginine\n 'CGG' => 'R', # Arginine\n 'CGU' => 'R', # Arginine\n 'AUA' => 'I', # Isoleucine\n 'AUC' => 'I', # Isoleucine\n 'AUU' => 'I', # Isoleucine\n 'AUG' => 'M', # Methionine\n 'ACA' => 'T', # Uhreonine\n \n 'GUG' => 'V', # Valine\n 'GUU' => 'V', # Valine\n 'GCA' => 'A', # Alanine\n 'GCC' => 'A', # Alanine\n 'GCG' => 'A', # Alanine\n 'GCU' => 'A', # Alanine\n 'GAC' => 'D', # Aspartic Acid\n 'GAU' => 'D', # Aspartic Acid\n 'GAA' => 'E', # Glutamic Acid\n 'GAG' => 'E', # Glutamic Acid\n 'GGA' => 'G', # Glycine\n 'GGC' => 'G', # Glycine\n 'GGG' => 'G', # Glycine\n 'GGU' => 'G' # Glycine \n}\n\ns = gets.chomp!\n\nf = g = c =0; res = 0; s.each_char{|si|\n c = 1 if si == '+'; c = -1 if si == '-'\n f += c; g += c; f = 0 if f < 0; g = 0 if g > 0\n res = f if f > res; res = -g if -g > res\n}\n"}, {"source_code": "res=k=0\ngets.chomp.each_char do |c|\n k+=1 if c=='+'\n k-=1 if c=='-'\n res=[res, k.abs].max\nend\nputs res\n"}, {"source_code": "s=gets.gsub(\"-+\", \"- +\").gsub(\"+-\", \"+ -\").split\ns.push(\"o\") if s.size%2==1\nn=s.size/2\nres=0\nfor i in 0..n-1 do\n res=[res, [s[2*i].size, s[2*i+1].size].max].max\nend\nputs res\n"}, {"source_code": "s=gets.chomp\nres=p=m=0\ns.each_char do |c|\n p+=1 if c=='+'\n m+=1 if c=='-'\n res=[res, (p-m).abs].max\nend\nputs res\n"}, {"source_code": "s=gets.chomp\nres=0\nfor i in 1..s.size()\n res=i if s.include?('+'*i) or s.include?('-'*i)\nend\nputs res\n"}, {"source_code": "def main()\n\ts=gets.chomp.to_s\n\ttotin=0\n\ttotout=0\n\tnops=1\n\tfor i in 0...(s.length)\n\t\tif s[i].chr==\"+\"\n\t\t\ttotin+=1\n\t\t\tif totin-1 > totout \n\t\t\t\tnops+=1\n\t\t\tend\n\t\telse\n\t\t\ttotout+=1\n\t\t\tif totout-1 > totin\n\t\t\t\tnops+=1\n\t\t\tend\n\t\tend\n\tend\n\tputs nops\nend\nmain()"}, {"source_code": "def main()\n\ts=gets.chomp.to_s\n\ttotin=0\n\ttotout=0\n\tnops=0\n\tfor i in 0...(s.length)\n\t\tif s[i].chr==\"+\"\n\t\t\ttotin+=1\n\t\t\tif totin-totout>1 || totout==0\n\t\t\t\tnops+=1\n\t\t\tend\n\t\telse\n\t\t\ttotout+=1\n\t\t\tif totout-totin>1 || totin==0\n\t\t\t\tnops+=1\n\t\t\tend\n\t\tend\n\tend\n\tputs nops\nend\nmain()"}, {"source_code": "def main()\n\ts=gets.chomp.to_s\n\tnops=0\n\tfor i in 0...(s.length)\n\t\tif s[i].chr==\"+\"\n\t\t\tlns=0\n\t\t\tindoln=-7\n\t\t\tj=i-1\n\t\t\twhile(j>=0)\n\t\t\t\tif s[j].chr==\"-\"\n\t\t\t\t\tindoln=j\n\t\t\t\t\tbreak\n\t\t\t\tend\n\t\t\t\tj-=1\n\t\t\tend\n\t\t\tif indoln!=-7\n\t\t\t\tj=indoln\n\t\t\t\twhile(j>=0)\n\t\t\t\t\tif s[j].chr==\"-\"\n\t\t\t\t\t\tlns+=1\n\t\t\t\t\t\tj-=1\n\t\t\t\t\telse\n\t\t\t\t\t\tbreak\n\t\t\t\t\tend\n\t\t\t\tend\n\t\t\t\tif lns<=i-indoln-1\n\t\t\t\t\tnops+=1\n\t\t\t\tend\n\t\t\telse\n\t\t\t\tnops+=1\n\t\t\tend\n\t\telse\n\t\t\tlps=0\n\t\t\tindolp=-7\n\t\t\tj=i-1\n\t\t\twhile(j>=0)\n\t\t\t\tif s[j].chr==\"+\"\n\t\t\t\t\tindolp=j\n\t\t\t\t\tbreak\n\t\t\t\tend\n\t\t\t\tj-=1\n\t\t\tend\n\t\t\tif indolp!=-7\n\t\t\t\tj=indolp\n\t\t\t\twhile(j>=0)\n\t\t\t\t\tif s[j].chr==\"+\"\n\t\t\t\t\t\tlps+=1\n\t\t\t\t\t\tj-=1\n\t\t\t\t\telse\n\t\t\t\t\t\tbreak\n\t\t\t\t\tend\n\t\t\t\tend\n\t\t\t\tif lps<=i-indolp-1\n\t\t\t\t\tnops+=1\n\t\t\t\tend\n\t\t\telse\n\t\t\t\tnops+=1\n\t\t\tend\n\t\tend\n\tend\n\tputs nops\nend\nmain()"}, {"source_code": "def main()\n\ts=gets.chomp.to_s\n\tmaxp=0\n\tmaxn=0\n\tps=\"+\"\n\tns=\"-\"\n\tj=300\n\twhile(true)\n\t\tind=s.index(ps*j)\n\t\tif ind!=nil\n\t\t\tmaxp=j\n\t\t\tbreak\n\t\tend\n\t\tj-=1\n\tend\n\tj=300\n\twhile(true)\n\t\tind=s.index(ns*j)\n\t\tif ind!=nil\n\t\t\tmaxn=j\n\t\t\tbreak\n\t\tend\n\t\tj-=1\n\tend\n\tputs [maxp,maxn].max\nend\nmain()"}, {"source_code": "max=0\ncount=0\n\ngets.chomp.each_char {|c|\n if c==?+\n count=0 if count<0\n count+=1\n else\n count-=1\n end\n \n max=count.abs if count.abs>max \n}\n\nputs max\n"}, {"source_code": "max=0\ncount=0\n\ngets.chomp.each_char {|c|\n if c==?+\n count+=1\n else\n count-=1\n end\n \n max=count.abs if count.abs>max \n}\n\nputs max\n"}, {"source_code": "pos=0\nneg=0\ncount=0\n\ngets.chomp.each_char {|c|\n if c==?+\n count+=1\n else\n count-=1\n end\n \n pos=count if count>pos \n neg=count if count 0 and a[s[-1]] >= a[i]\n if s.length == 0 then r[i] = i + 1 else r[i] = i - s[-1] end\n s.push i\n end\n r\nend\n\nleft = find_left a\nright = (find_left a.reverse).reverse\nans = Array.new(n, a.min)\n(0...n).each do |i|\n v = left[i] + right[i] - 2\n ans[v] = a[i] if a[i] > ans[v]\nend\n(n-2).downto(0) do |i|\n ans[i] = ans[i + 1] if ans[i + 1] > ans[i]\nend\nans.each do |x|\n print x\n print ' '\nend\nputs"}], "negative_code": [], "src_uid": "5cf25cd4a02ce8020258115639c0ee64"} {"nl": {"description": "Constanze is the smartest girl in her village but she has bad eyesight.One day, she was able to invent an incredible machine! When you pronounce letters, the machine will inscribe them onto a piece of paper. For example, if you pronounce 'c', 'o', 'd', and 'e' in that order, then the machine will inscribe \"code\" onto the paper. Thanks to this machine, she can finally write messages without using her glasses.However, her dumb friend Akko decided to play a prank on her. Akko tinkered with the machine so that if you pronounce 'w', it will inscribe \"uu\" instead of \"w\", and if you pronounce 'm', it will inscribe \"nn\" instead of \"m\"! Since Constanze had bad eyesight, she was not able to realize what Akko did.The rest of the letters behave the same as before: if you pronounce any letter besides 'w' and 'm', the machine will just inscribe it onto a piece of paper.The next day, I received a letter in my mailbox. I can't understand it so I think it's either just some gibberish from Akko, or Constanze made it using her machine. But since I know what Akko did, I can just list down all possible strings that Constanze's machine would have turned into the message I got and see if anything makes sense.But I need to know how much paper I will need, and that's why I'm asking you for help. Tell me the number of strings that Constanze's machine would've turned into the message I got.But since this number can be quite large, tell me instead its remainder when divided by $$$10^9+7$$$.If there are no strings that Constanze's machine would've turned into the message I got, then print $$$0$$$.", "input_spec": "Input consists of a single line containing a string $$$s$$$ ($$$1 \\leq |s| \\leq 10^5$$$) — the received message. $$$s$$$ contains only lowercase Latin letters.", "output_spec": "Print a single integer — the number of strings that Constanze's machine would've turned into the message $$$s$$$, modulo $$$10^9+7$$$.", "sample_inputs": ["ouuokarinn", "banana", "nnn", "amanda"], "sample_outputs": ["4", "1", "3", "0"], "notes": "NoteFor the first example, the candidate strings are the following: \"ouuokarinn\", \"ouuokarim\", \"owokarim\", and \"owokarinn\".For the second example, there is only one: \"banana\".For the third example, the candidate strings are the following: \"nm\", \"mn\" and \"nnn\".For the last example, there are no candidate strings that the machine can turn into \"amanda\", since the machine won't inscribe 'm'."}, "positive_code": [{"source_code": "puts (s = gets) =~ /m|w/ ? 0 : s.length.times.inject([1, 1]){|a, i| i < 1 ? a : [a[1], a[1] + (s[i] == s[i-1] && ['u', 'n'].include?(s[i]) ? a[0] : 0)]}[1] % 10000_00007"}, {"source_code": "puts (s = gets)['w'] || s['m'] ? 0 : s.scan(/(?:n{2,}|u{2,})/).map{|v| $c ||= {}; (r = $c[v.length]) ? r : $c[v.length] = v.length.times.inject([0,1]){|a| [a[1], a.sum]}[1] }.inject(1){|a,b| a*b} % 10000_00007"}], "negative_code": [{"source_code": "s = gets\nif(s.length > 100) \n puts s.length \nelse \n puts s =~ /m|w/ ? 0 : s.length.times.inject([1, 1]){|a, i| i < 1 ? a : [a[1], a[1] + (s[i] == s[i-1] && ['u', 'n'].include?(s[i]) ? a[0] : 0)]}[1] % 10000_00007\nend"}, {"source_code": "puts gets.length"}, {"source_code": "puts (s = gets)['w'] || s['m'] ? 0 : s.scan(/[nu]{2,}/).map{|v| $c ||= {0=> 1, 1=> 1}; fib = proc{|n| (r = $c[n]) ? r : fib.call(n-1) + fib.call(n-2)}; fib.call(v.length) }.inject(1){|a, b| a*b} % 100000007"}, {"source_code": "puts (s = gets) =~ /m|w/ ? 0 : s.length.times.inject([1, 1]){|a, i| i < 1 ? a : [a[1], a[1] + (s[i] == s[i-1] ? a[0] : 0)]}[1] % 10000_00007"}], "src_uid": "12c223c903544899638e47bcab88999a"} {"nl": {"description": "Hacker Zhorik wants to decipher two secret messages he intercepted yesterday. Yeah message is a sequence of encrypted blocks, each of them consists of several bytes of information.Zhorik knows that each of the messages is an archive containing one or more files. Zhorik knows how each of these archives was transferred through the network: if an archive consists of k files of sizes l1, l2, ..., lk bytes, then the i-th file is split to one or more blocks bi, 1, bi, 2, ..., bi, mi (here the total length of the blocks bi, 1 + bi, 2 + ... + bi, mi is equal to the length of the file li), and after that all blocks are transferred through the network, maintaining the order of files in the archive.Zhorik thinks that the two messages contain the same archive, because their total lengths are equal. However, each file can be split in blocks in different ways in the two messages.You are given the lengths of blocks in each of the two messages. Help Zhorik to determine what is the maximum number of files could be in the archive, if the Zhorik's assumption is correct.", "input_spec": "The first line contains two integers n, m (1 ≤ n, m ≤ 105) — the number of blocks in the first and in the second messages. The second line contains n integers x1, x2, ..., xn (1 ≤ xi ≤ 106) — the length of the blocks that form the first message. The third line contains m integers y1, y2, ..., ym (1 ≤ yi ≤ 106) — the length of the blocks that form the second message. It is guaranteed that x1 + ... + xn = y1 + ... + ym. Also, it is guaranteed that x1 + ... + xn ≤ 106.", "output_spec": "Print the maximum number of files the intercepted array could consist of.", "sample_inputs": ["7 6\n2 5 3 1 11 4 4\n7 8 2 4 1 8", "3 3\n1 10 100\n1 100 10", "1 4\n4\n1 1 1 1"], "sample_outputs": ["3", "2", "1"], "notes": "NoteIn the first example the maximum number of files in the archive is 3. For example, it is possible that in the archive are three files of sizes 2 + 5 = 7, 15 = 3 + 1 + 11 = 8 + 2 + 4 + 1 and 4 + 4 = 8.In the second example it is possible that the archive contains two files of sizes 1 and 110 = 10 + 100 = 100 + 10. Note that the order of files is kept while transferring archives through the network, so we can't say that there are three files of sizes 1, 10 and 100.In the third example the only possibility is that the archive contains a single file of size 4."}, "positive_code": [{"source_code": "la,lb=gets.split.collect{|t|t.to_i}\nsbl,res=0,0\na=gets.split.collect{|t|t.to_i}\nb=gets.split.collect{|t|t.to_i}\nwhile a.size>0\n if sbl==0\n res+=1\n sbl+=a.shift\n end\n if sbl>0\n sbl-=b.shift\n else\n sbl+=a.shift\n end\nend\nputs res"}, {"source_code": "la,lb=gets.split.collect{|t|t.to_i}\nsbl,res=0,0\na=gets.split.collect{|t|t.to_i}\nb=gets.split.collect{|t|t.to_i}\nwhile a.size>0\n if sbl==0\n res+=1\n sbl+=a.shift\n end\n if sbl>0\n sbl-=b.shift\n else\n sbl+=a.shift\n end\nend\nputs res"}, {"source_code": "n, m = gets.split.map(&:to_i)\na = gets.chomp.split.map(&:to_i)\nb = gets.chomp.split.map(&:to_i)\nans, sa, sb, la, lb = 0, 0, 0, 0, 0\nwhile la < n || lb < m\n if sa == 0\n sa += a[la]\n la += 1\n end\n if sb == 0\n sb += b[lb]\n lb += 1\n end\n if sa == sb\n ans += 1\n sa = 0\n sb = 0\n else\n if sa < sb\n sa += a[la]\n la += 1\n else\n sb += b[lb]\n lb += 1\n end\n end\nend\nans += 1 if sa != 0\nputs ans\n"}, {"source_code": "# http://codeforces.com/problemset/problem/950/B\nn, m = gets.split.map(&:to_i)\nm1 = gets.split.map(&:to_i)\nm2 = gets.split.map(&:to_i)\n\nc = 0\nif m == 1 || n == 1\n puts 1\nelse\n file1_size = 0\n file2_size = 0\n p1 = 0\n p2 = 0\n\n while p1 < n && p2 < m\n if file1_size + m1[p1] == file2_size + m2[p2]\n file1_size = 0\n file2_size = 0\n p1 += 1\n p2 += 1\n c += 1\n next\n end\n\n if file1_size + m1[p1] > file2_size + m2[p2]\n file2_size += m2[p2]\n p2 += 1\n else\n file1_size += m1[p1]\n p1 += 1\n end\n end\n puts c\nend\n"}], "negative_code": [{"source_code": "# http://codeforces.com/problemset/problem/950/B\nn, m = gets.split.map(&:to_i)\nm1 = gets.split.map(&:to_i)\nm2 = gets.split.map(&:to_i)\n\nc = 0\nif m == 1 || n == 1\n puts 1\nelse\n file1_size = 0\n file2_size = 0\n p1 = 0\n p2 = 0\n\n while p1 < n && p2 < m\n if file1_size + m1[p1] == file2_size + m2[p2]\n file1_size = 0\n file2_size = 0\n p1 += 1\n p2 += 1\n c += 1\n next\n end\n\n if file1_size + m1[p1] > file2_size + m2[p2]\n file2_size += m2[p2]\n p2 += 1\n else\n file1_size += m1[p1]\n p1 += 1\n end\n end\nend\n\nputs c"}], "src_uid": "cb5cbfb4d184cda21ebfbcf47bb970dc"} {"nl": {"description": "David was given a red checkered rectangle of size $$$n \\times m$$$. But he doesn't like it. So David cuts the original or any other rectangle piece obtained during the cutting into two new pieces along the grid lines. He can do this operation as many times as he wants.As a result, he will get a set of rectangles. Rectangles $$$1 \\times 1$$$ are forbidden.David also knows how to paint the cells blue. He wants each rectangle from the resulting set of pieces to be colored such that any pair of adjacent cells by side (from the same piece) have different colors.What is the minimum number of cells David will have to paint?", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 10^3$$$) — the number of test cases. The next lines contain descriptions of test cases. The only line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 \\leq n, m \\leq 3 \\cdot 10^4$$$, $$$n \\cdot m \\geq 2$$$).", "output_spec": "For each test case print a single integer — the minimum number of cells David will have to paint blue.", "sample_inputs": ["4\n1 3\n2 2\n2 5\n3 5"], "sample_outputs": ["1\n2\n4\n5"], "notes": "NoteThe following pictures show how the initial rectangle can be split and cells colored blue.In the first test case: In the second test case: In the third test case: In the fourth test case: "}, "positive_code": [{"source_code": "za=gets.chomp.to_i\r\nza.times do\r\n\tn,m=gets.split.map(&:to_i)\r\n\tp=n*m;\r\n if(p%3==0)\r\n puts p/3\r\n else\r\n puts (p/3)+1\r\n end\r\nend"}], "negative_code": [], "src_uid": "70a0b98f2bb12990a0fa46aaf13134af"} {"nl": {"description": "The Berland State University is hosting a ballroom dance in celebration of its 100500-th anniversary! n boys and m girls are already busy rehearsing waltz, minuet, polonaise and quadrille moves.We know that several boy&girl pairs are going to be invited to the ball. However, the partners' dancing skill in each pair must differ by at most one.For each boy, we know his dancing skills. Similarly, for each girl we know her dancing skills. Write a code that can determine the largest possible number of pairs that can be formed from n boys and m girls.", "input_spec": "The first line contains an integer n (1 ≤ n ≤ 100) — the number of boys. The second line contains sequence a1, a2, ..., an (1 ≤ ai ≤ 100), where ai is the i-th boy's dancing skill. Similarly, the third line contains an integer m (1 ≤ m ≤ 100) — the number of girls. The fourth line contains sequence b1, b2, ..., bm (1 ≤ bj ≤ 100), where bj is the j-th girl's dancing skill.", "output_spec": "Print a single number — the required maximum possible number of pairs.", "sample_inputs": ["4\n1 4 6 2\n5\n5 1 5 7 9", "4\n1 2 3 4\n4\n10 11 12 13", "5\n1 1 1 1 1\n3\n1 2 3"], "sample_outputs": ["3", "0", "2"], "notes": null}, "positive_code": [{"source_code": "# cook your code here\nb=gets.to_i\nskb = gets.split(/ /).map! { |e| e=e.to_i }\ng=gets.to_i\nskg = gets.split(/ /).map! { |e| e=e.to_i }\nskg=skg.sort\nskb=skb.sort\ni=0\nj=0\npe=0\n\nwhile j < g and i < b \n if (skb[i]-skg[j]).abs<=1\n pe=pe+1\n i=i+1\n j=j+1\n elsif skb[i]>skg[j]\n j=j+1\n elsif skb[i] 1\n\tif b < n and (girl-boys[b]).abs <= 1\n\t\tcount += 1\n\t\tb += 1\n\tend\nend\n\nputs count\n"}, {"source_code": "gets\na = gets.split.map { |x| x.to_i }.sort\ngets\nb = gets.split.map { |x| x.to_i }.sort.join(\" \")\n\na.each do |x|\n b.sub!(/\\b(#{x-1}|#{x}|#{x+1})\\b/, \"200\")\nend\nputs b.scan(/200/).length\n"}, {"source_code": "gets\na = gets.split.map { |x| x.to_i }.sort\ngets\nb = gets.split.map { |x| x.to_i }.sort.join(\" \")\n\na.each do |x|\n b.sub!(/\\b(#{x-1}|#{x}|#{x+1})\\b/, \"200\")\nend\nputs b.scan(/200/).length\n"}, {"source_code": "n = gets.to_i\na = gets.split.map { |x| x.to_i }.sort\nm = gets.to_i\nb = gets.split.map { |x| x.to_i }.sort\n\ni, j = 0, 0\ncnt = 0\nwhile i < n and j < m\n if (a[i] - b[j]).abs <= 1\n i += 1\n j += 1\n cnt += 1\n elsif a[i] + 1 < b[j]\n i += 1\n else\n j += 1\n end\nend\n\nputs cnt"}, {"source_code": "gets\na = gets.split.map { |x| x.to_i }.sort\ngets\nb = gets.split.map { |x| x.to_i }.sort.join(\" \")\n\na.each do |x|\n b.sub!(/\\b(#{x-1}|#{x}|#{x+1})\\b/, \"200\")\nend\nputs b.scan(/200/).length\n\n"}, {"source_code": "gets\na = gets.split.map { |x| x.to_i }.sort\ngets\nb = gets.split.map { |x| x.to_i }.sort.join(\" \")\n\na.each do |x|\n b.sub!(/\\b(#{x-1}|#{x}|#{x+1})\\b/, \"200\")\nend\nputs b.scan(/200/).length\n\n"}, {"source_code": "gets\nx=gets.split.map{|e| e.to_i}.sort\ngets\ny=gets.split.map{|e| e.to_i}.sort\n\nans=0\nwhile (!x.empty?) && (!y.empty?)\n\tif (x[0]-y[0]).abs<=1 then\n\t\tans+=1\n\t\tx.shift\n\t\ty.shift\n\telse\n\t\tif x[0]= m\n puts pairs(girl, boy)\nelse\n puts pairs(boy, girl)\nend"}, {"source_code": "boys = gets.chomp.to_i\nboys_skills = gets.chomp\nboys_skills = boys_skills.split\ngirls = gets.chomp.to_i\ngirls_skills = gets.chomp\ngirls_skills = girls_skills.split\n\ncount = 0\nfor i in (0..girls-1)\n\tfor x in (i..girls-1)\n\t\tif girls_skills[i].to_i > girls_skills[x].to_i && i != x\n\t\t\ttemp = girls_skills[i].to_i\n\t\t\tgirls_skills[i] = girls_skills[x].to_i\n\t\t\tgirls_skills[x] = temp\n\t\tend\n\tend\nend\nfor i in (0..boys-1)\n\tfor x in (i..boys-1)\n\t\tif boys_skills[i].to_i > boys_skills[x].to_i && i != x\n\t\t\ttemp = boys_skills[i].to_i\n\t\t\tboys_skills[i] = boys_skills[x].to_i\n\t\t\tboys_skills[x] = temp\n\t\tend\n\tend\nend\nif boys <= girls\n\tfor i in (0..boys-1)\n\t\tfor x in (0..girls_skills.size-1)\n\t\t\tif boys_skills[i].to_i == girls_skills[x].to_i || boys_skills[i].to_i - 1 == girls_skills[x].to_i || boys_skills[i].to_i + 1 == girls_skills[x].to_i\n\t\t\t\tgirls_skills.delete_at(x)\n\t\t\t\tcount += 1\n\t\t\t\tbreak\n\t\t\tend\n\t\tend\n\tend\nelse\n\tfor i in (0..girls-1)\n\t\tfor x in (0..boys_skills.size-1)\n\t\t\tif girls_skills[i].to_i == boys_skills[x].to_i || girls_skills[i].to_i - 1 == boys_skills[x].to_i || girls_skills[i].to_i + 1 == boys_skills[x].to_i\n\t\t\t\tboys_skills.delete_at(x)\n\t\t\t\tcount += 1\n\t\t\t\tbreak\n\t\t\tend\n\t\tend\n\tend\nend\nprint count"}, {"source_code": "#!/usr/bin/env ruby\n\nn = $stdin.gets.to_i\nboys = $stdin.gets.split.map { |b| b.to_i }\nm = $stdin.gets.to_i\ngirls = $stdin.gets.split.map { |b| b.to_i }\n\nraise \"weird\" unless boys.size == n\nraise \"weird\" unless girls.size == m\n\nboys.sort!\ngirls.sort!\n\ni = 0\nj = 0\nres = 0\nwhile i < n and j < m\n if (boys[i] - girls[j]).abs <= 1\n res += 1\n i += 1\n j += 1\n elsif boys[i] < girls[j]\n i += 1\n elsif boys[i] > girls[j]\n j += 1\n end\nend\n\nputs res\n"}, {"source_code": "gets\na = gets.split.map { |x| x.to_i }.sort\ngets\nb = gets.split.map { |x| x.to_i }.sort.join(\" \")\n\na.each do |x|\n b.sub!(/\\b(#{x-1}|#{x}|#{x+1})\\b/, \"200\")\nend\nputs b.scan(/200/).length\n\n"}, {"source_code": "def solution(boys, boys_skill, girls, girls_skill)\n @boys = boys\n @girls = girls\n @boys_skill = boys_skill.sort!\n @girls_skill = girls_skill.sort!\n\n @couples = 0\n solve(0, 0)\n @couples\nend\n\ndef solve(a_i, b_i)\n return nil if a_i == @boys || b_i == @girls\n if (@boys_skill[a_i] - @girls_skill[b_i]).abs <= 1\n @couples += 1\n solve(a_i + 1, b_i + 1)\n elsif @boys_skill[a_i] > @girls_skill[b_i]\n solve(a_i, b_i + 1)\n else\n solve(a_i + 1, b_i)\n end\nend\n\nboys = gets.to_i\nboys_skill = gets.split.map(&:to_i)\ngirls = gets.to_i\ngirls_skill = gets.split.map(&:to_i)\nputs solution(boys, boys_skill, girls, girls_skill)"}, {"source_code": "def solution(boys, boys_skill, girls, girls_skill)\n boys_skill.sort!\n girls_skill.sort!\n i, j = 0, 0\n\n couples = 0\n while i < boys && j < girls do\n if (boys_skill[i] - girls_skill[j]).abs <= 1\n couples += 1\n i += 1\n j += 1\n else\n if boys_skill[i] > girls_skill[j]\n j += 1\n else\n i += 1\n end\n end\n end\n couples\nend\n\nboys = gets.to_i\nboys_skill = gets.split.map(&:to_i)\ngirls = gets.to_i\ngirls_skill = gets.split.map(&:to_i)\n\n\nputs solution(boys, boys_skill, girls, girls_skill)"}, {"source_code": "n = gets.chomp.to_i\na = gets.split().map(&:to_i)\nm = gets.chomp.to_i\nb = gets.split().map(&:to_i)\na.sort!\nb.sort!\ns = 0\ni = 0\nj = 0\nwhile i < n and j < m\n\tif a[i] < b[j]\n\t\tif b[j] - a[i] <= 1\n\t\t\ts+=1\n\t\t\tj+=1\n\t\tend\n\t\ti+=1\n\telse \n\t\tif a[i] - b[j] <= 1\n\t\t\ti+=1\n\t\t\ts+=1\n\t\tend\n\t\tj+=1\n\tend\nend\nputs s\n"}, {"source_code": "n = gets.to_i\nns = gets.chomp.split.map(&:to_i).sort\nm = gets.to_i\nms = gets.chomp.split.map(&:to_i).sort\nans = 0\ni = 0\nj = 0\nwhile i < n && j < m\n if ns[i] == ms[j] || ns[i] + 1 == ms[j] || ns[i] - 1 == ms[j]\n i += 1\n j += 1\n ans += 1\n elsif ns[i] > ms[j] + 1\n j += 1\n elsif ns[i] + 1 < ms[j]\n i += 1\n end\nend\np ans\n"}, {"source_code": "gets\na = gets.split.map { |x| x.to_i }.sort\ngets\nb = gets.split.map { |x| x.to_i }.sort.join(\" \")\n\na.each do |x|\n b.sub!(/\\b(#{x-1}|#{x}|#{x+1})\\b/, \"200\")\nend\nputs b.scan(/200/).length\n\n"}, {"source_code": "gets\na = gets.split(' ').map(&:to_i).sort\n\ngets\nb = gets.split(' ').map(&:to_i).sort\n\nresult = 0\nwhile !a.empty? && !b.empty?\n if (a.first - b.first).abs < 2\n result += 1\n a.shift\n b.shift\n else\n a.first < b.first ? a.shift : b.shift\n end\nend\n\nputs result\n"}, {"source_code": "#File.open(\"4.txt\",\"r\") do |file|\n\ta=Array.new\n\tb=Array.new\n\tc=Array.new\n\td=Array.new\n\tn=gets.to_i\n\tstr=gets.to_s\n\tc=str.split\n\ti=0\n\twhile ib[j]\n\t\t\tj+=1\n\t\telse\n\t\t\ti+=1\n\t\tend\n\tend\n\tputs s\n#end"}, {"source_code": "gets\na = gets.split.map { |x| x.to_i }.sort\ngets\nb = gets.split.map { |x| x.to_i }.sort.join(\" \")\n\na.each do |x|\n b.sub!(/\\b(#{x-1}|#{x}|#{x+1})\\b/, \"200\")\nend\nputs b.scan(/200/).length\n\n"}, {"source_code": "$dp = Array.new(101)\n(0..101).each do |i|\n $dp[i]=Array.new(101,-1)\nend\n$n= gets.to_i\n$a= gets.split(' ').map(&:to_i).sort\n$m= gets.to_i\n$b= gets.split(' ').map(&:to_i).sort\ndef calc(i,j)\n ret = 0\n if i == $n\n ret\n elsif j==$m\n ret\n elsif $dp[i][j]!=-1\n $dp[i][j]\n else\n ret=calc(i+1,j)\n ret=[calc(i+1,j+1)+(($a[i]-$b[j]).abs<2?1:0),ret].max\n ret=[calc(i,j+1),ret].max\n $dp[i][j]=ret\n ret\n end\nend\n\nputs calc(0,0)\n\n\n\n"}, {"source_code": "gets\na = gets.split.map { |x| x.to_i }.sort\ngets\nb = gets.split.map { |x| x.to_i }.sort.join(\" \")\n\na.each do |x|\n b.sub!(/\\b(#{x-1}|#{x}|#{x+1})\\b/, \"200\")\nend\nputs b.scan(/200/).length\n\n"}], "negative_code": [{"source_code": "n = gets.to_i\n\na = gets.split.map { |x| x.to_i }\n\nn = gets.to_i\n\nb = gets.split.map { |x| x.to_i }\n\na.sort!\nb.sort!\n\nx = nil\ny = nil\ncount = 0\n\nwhile !a.empty? and !b.empty?\n\tx = a.shift if x.nil?\n\ty = b.shift if y.nil?\n\tif x < y - 1\n\t\tx = nil\n\telsif y < x - 1\n\t\ty = nil\n\telse\n\t\tx = nil\n\t\ty = nil\n\t\tcount = count + 1\n\tend\nend\n\nputs count\n\n"}, {"source_code": "n = gets.to_i\na = gets.split.map { |x| x.to_i }\nn = gets.to_i\nb = gets.split.map { |x| x.to_i }\n\na.sort!\nb.sort!\n\nx = nil\ny = nil\ncount = 0\n\nwhile !a.empty? and !b.empty?\n x = a.shift if x.nil?\n y = b.shift if y.nil?\n if x < y - 1\n x = nil\n elsif y < x - 1\n y = nil\n else\n x = nil\n y = nil\n count = count + 1\n end\nend\n\nputs count"}, {"source_code": "gets\na = gets.split.map { |x| x.to_i }.sort\ngets\nb = gets.chomp\n\na.each do |x|\n b.sub!(/\\b(#{x}|#{x+1}|#{x-1})\\b/, \"200\")\nend\nputs b.scan(/200/).length\n"}, {"source_code": "gets\na = gets.split.map { |x| x.to_i }.sort\nn = gets.to_i\nb = gets.split.map { |x| x.to_i }.sort\nindex = 0\nans = 0\n\na.each do |x|\n break if (index >= n)\n \n if ((x - b[index]).abs <= 1)\n index += 1\n ans += 1\n end\nend\n \nputs ans\n"}, {"source_code": "gets\na = gets.split.map { |x| x.to_i }.sort\ngets\nb = gets.split.map { |x| x.to_i }.sort\nans = 0\n\na.each do |x|\n b.each do |y|\n if ((x - y).abs <= 1)\n ans += 1\n b.shift\n break\n elsif ( x > y + 1)\n b.shift\n end\n end\nend\n \nputs ans\n"}, {"source_code": "gets\na = gets.split.map { |x| x.to_i }.sort\ngets\nb = gets.chomp\n\na.each do |x|\n b.sub!(/\\b(#{x-1}|#{x}|#{x+1})\\b/, \"200\")\nend\nputs b.scan(/200/).length\n"}, {"source_code": "n = gets.to_i\na = gets.split.map { |x| x.to_i }.sort\nm = gets.to_i\nb = gets.split.map { |x| x.to_i }.sort\n\ni, j = 0, 0\ncnt = 0\nwhile i < n and j < m\n if (a[i] - b[j]).abs <= 1\n i += 1\n j += 1\n cnt += 1\n elsif a[i] + 1 < a[j]\n i += 1\n else\n j += 1\n end\nend\n\nputs cnt"}, {"source_code": "gets\nx=gets.split.map{|e| e.to_i}.sort\ngets\ny=gets.split.map{|e| e.to_i}.sort\n\nans=0\nwhile (!x.empty?) && (!y.empty?)\n\tif (x[0]-y[0]).abs<=1 then\n\t\tans+=1\n\t\tx.shift\n\t\ty.shift\n\telse\n\t\tif x[0]= m\n puts pairs(girl, boy)\nelse\n puts pairs(boy, girl)\nend"}, {"source_code": "boys = gets.chomp.to_i\nboys_skills = gets.chomp\nboys_skills = boys_skills.split\ngirls = gets.chomp.to_i\ngirls_skills = gets.chomp\ngirls_skills = girls_skills.split\n\ncount = 0\nfor i in (0..girls-1)\n\tfor x in (i..girls-1)\n\t\tif girls_skills[i].to_i > girls_skills[x].to_i && i += x\n\t\t\ttemp = girls_skills[i].to_i\n\t\t\tgirls_skills[i] = girls_skills[x].to_i\n\t\t\tgirls_skills[x] = temp\n\t\tend\n\tend\nend\nfor i in (0..boys-1)\n\tfor x in (i..boys-1)\n\t\tif boys_skills[i].to_i > boys_skills[x].to_i && i != x\n\t\t\ttemp = boys_skills[i].to_i\n\t\t\tboys_skills[i] = boys_skills[x].to_i\n\t\t\tboys_skills[x] = temp\n\t\tend\n\tend\nend\nif boys <= girls\n\tfor i in (0..boys-1)\n\t\tfor x in (0..girls_skills.size-1)\n\t\t\tif boys_skills[i].to_i == girls_skills[x].to_i || boys_skills[i].to_i - 1 == girls_skills[x].to_i || boys_skills[i].to_i + 1 == girls_skills[x].to_i\n\t\t\t\tgirls_skills.delete_at(x)\n\t\t\t\tcount += 1\n\t\t\t\tbreak\n\t\t\tend\n\t\tend\n\tend\nelse\n\tfor i in (0..girls-1)\n\t\tfor x in (0..boys_skills.size-1)\n\t\t\tif girls_skills[i].to_i == boys_skills[x].to_i || girls_skills[i].to_i - 1 == boys_skills[x].to_i || girls_skills[i].to_i + 1 == boys_skills[x].to_i\n\t\t\t\tboys_skills.delete_at(x)\n\t\t\t\tcount += 1\n\t\t\t\tbreak\n\t\t\tend\n\t\tend\n\tend\nend\nprint count"}, {"source_code": "boys = gets.chomp.to_i\nboys_skills = gets.chomp\nboys_skills = boys_skills.split\ngirls = gets.chomp.to_i\ngirls_skills = gets.chomp\ngirls_skills = girls_skills.split\n\ncount = 0\nif boys <= girls\n\tfor i in (0..boys-1)\n\t\tfor x in (0..girls_skills.size-1)\n\t\t\tif boys_skills[i].to_i == girls_skills[x].to_i || boys_skills[i].to_i - 1 == girls_skills[x].to_i || boys_skills[i].to_i + 1 == girls_skills[x].to_i\n\t\t\t\tgirls_skills.delete_at(x)\n\t\t\t\tcount += 1\n\t\t\t\tbreak\n\t\t\tend\n\t\tend\n\tend\nelse\n\tfor i in (0..girls-1)\n\t\tfor x in (0..boys_skills.size-1)\n\t\t\tif girls_skills[i].to_i == boys_skills[x].to_i || girls_skills[i].to_i - 1 == boys_skills[x].to_i || girls_skills[i].to_i + 1 == boys_skills[x].to_i\n\t\t\t\tboys_skills.delete_at(x)\n\t\t\t\tcount += 1\n\t\t\t\tbreak\n\t\t\tend\n\t\tend\n\tend\nend\nprint count\n# for i in (0..girls-1)\n# \tfor x in (1..girls-1)\n# \t\tif girls_skills[i].to_i > girls_skills[x]\n# \t\t\ttemp = girls_skills[i].to_i\n# \t\t\tgirls_skills[i] = girls_skills[x].to_i\n# \t\t\tgirls_skills[x] = temp\n# \t\tend\n# \tend\n# end"}, {"source_code": "boys = gets.chomp.to_i\nboys_skills = gets.chomp\nboys_skills = boys_skills.split\ngirls = gets.chomp.to_i\ngirls_skills = gets.chomp\ngirls_skills = girls_skills.split\n\ncount = 0\nfor i in (0..girls-2)\n\tfor x in (1..girls-1)\n\t\tif girls_skills[i].to_i > girls_skills[x].to_i\n\t\t\ttemp = girls_skills[i].to_i\n\t\t\tgirls_skills[i] = girls_skills[x].to_i\n\t\t\tgirls_skills[x] = temp\n\t\tend\n\tend\nend\nfor i in (0..boys-2)\n\tfor x in (1..boys-1)\n\t\tif boys_skills[i].to_i > boys_skills[x].to_i\n\t\t\ttemp = boys_skills[i].to_i\n\t\t\tboys_skills[i] = boys_skills[x].to_i\n\t\t\tboys_skills[x] = temp\n\t\tend\n\tend\nend\nif boys <= girls\n\tfor i in (0..boys-1)\n\t\tfor x in (0..girls_skills.size-1)\n\t\t\tif boys_skills[i].to_i == girls_skills[x].to_i || boys_skills[i].to_i - 1 == girls_skills[x].to_i || boys_skills[i].to_i + 1 == girls_skills[x].to_i\n\t\t\t\tgirls_skills.delete_at(x)\n\t\t\t\tcount += 1\n\t\t\t\tbreak\n\t\t\tend\n\t\tend\n\tend\nelse\n\tfor i in (0..girls-1)\n\t\tfor x in (0..boys_skills.size-1)\n\t\t\tif girls_skills[i].to_i == boys_skills[x].to_i || girls_skills[i].to_i - 1 == boys_skills[x].to_i || girls_skills[i].to_i + 1 == boys_skills[x].to_i\n\t\t\t\tboys_skills.delete_at(x)\n\t\t\t\tcount += 1\n\t\t\t\tbreak\n\t\t\tend\n\t\tend\n\tend\nend\nprint count"}, {"source_code": "boys = gets.chomp.to_i\nboys_skills = gets.chomp\nboys_skills = boys_skills.split\ngirls = gets.chomp.to_i\ngirls_skills = gets.chomp\ngirls_skills = girls_skills.split\n\ncount = 0\nfor i in (0..girls-1)\n\tfor x in (1..girls-1)\n\t\tif girls_skills[i].to_i > girls_skills[x].to_i\n\t\t\ttemp = girls_skills[i].to_i\n\t\t\tgirls_skills[i] = girls_skills[x].to_i\n\t\t\tgirls_skills[x] = temp\n\t\tend\n\tend\nend\nfor i in (0..boys-1)\n\tfor x in (1..boys-1)\n\t\tif boys_skills[i].to_i > boys_skills[x].to_i\n\t\t\ttemp = boys_skills[i].to_i\n\t\t\tboys_skills[i] = boys_skills[x].to_i\n\t\t\tboys_skills[x] = temp\n\t\tend\n\tend\nend\nif boys <= girls\n\tfor i in (0..boys-1)\n\t\tfor x in (0..girls_skills.size-1)\n\t\t\tif boys_skills[i].to_i == girls_skills[x].to_i || boys_skills[i].to_i - 1 == girls_skills[x].to_i || boys_skills[i].to_i + 1 == girls_skills[x].to_i\n\t\t\t\tgirls_skills.delete_at(x)\n\t\t\t\tcount += 1\n\t\t\t\tbreak\n\t\t\tend\n\t\tend\n\tend\nelse\n\tfor i in (0..girls-1)\n\t\tfor x in (0..boys_skills.size-1)\n\t\t\tif girls_skills[i].to_i == boys_skills[x].to_i || girls_skills[i].to_i - 1 == boys_skills[x].to_i || girls_skills[i].to_i + 1 == boys_skills[x].to_i\n\t\t\t\tboys_skills.delete_at(x)\n\t\t\t\tcount += 1\n\t\t\t\tbreak\n\t\t\tend\n\t\tend\n\tend\nend\nprint count"}, {"source_code": "boys = gets.chomp.to_i\nboys_skills = gets.chomp\nboys_skills = boys_skills.split\ngirls = gets.chomp.to_i\ngirls_skills = gets.chomp\ngirls_skills = girls_skills.split\n\ncount = 0\nfor i in (0..girls-1)\n\tfor x in (i..girls-1)\n\t\tif girls_skills[i].to_i > girls_skills[x].to_i && i != x\n\t\t\ttemp = girls_skills[i].to_i\n\t\t\tgirls_skills[i] = girls_skills[x].to_i\n\t\t\tgirls_skills[x] = temp\n\t\tend\n\tend\nend\nfor i in (0..boys-1)\n\tfor x in (i..boys-1)\n\t\tif boys_skills[i].to_i > boys_skills[x].to_i && i != x\n\t\t\ttemp = boys_skills[i].to_i\n\t\t\tboys_skills[i] = boys_skills[x].to_i\n\t\t\tboys_skills[x] = temp\n\t\tend\n\tend\nend\nprint boys_skills\nprint girls_skills\nif boys <= girls\n\tfor i in (0..boys-1)\n\t\tfor x in (0..girls_skills.size-1)\n\t\t\tif boys_skills[i].to_i == girls_skills[x].to_i || boys_skills[i].to_i - 1 == girls_skills[x].to_i || boys_skills[i].to_i + 1 == girls_skills[x].to_i\n\t\t\t\tgirls_skills.delete_at(x)\n\t\t\t\tcount += 1\n\t\t\t\tbreak\n\t\t\tend\n\t\tend\n\tend\nelse\n\tfor i in (0..girls-1)\n\t\tfor x in (0..boys_skills.size-1)\n\t\t\tif girls_skills[i].to_i == boys_skills[x].to_i || girls_skills[i].to_i - 1 == boys_skills[x].to_i || girls_skills[i].to_i + 1 == boys_skills[x].to_i\n\t\t\t\tboys_skills.delete_at(x)\n\t\t\t\tcount += 1\n\t\t\t\tbreak\n\t\t\tend\n\t\tend\n\tend\nend\nprint count"}, {"source_code": "$dp = Array.new(101)\n(0..101).each do |i|\n $dp[i]=Array.new(101,-1)\nend\n$n= gets.to_i\n$a= gets.split(' ').map(&:to_i).sort\n$m= gets.to_i\n$b= gets.split(' ').map(&:to_i).sort\ndef calc(i,j)\n ret = 0\n if i == $n\n ret\n elsif j==$m\n ret\n elsif $dp[i][j]!=-1\n $dp[i][j]\n else\n ret=calc(i+1,j)\n ret=[calc(i+1,j+1)+(($a[i]-$b[j]).abs<2?1:0),ret].max\n ret=[calc(i+1,j),ret].max\n $dp[i][j]=ret\n ret\n end\nend\n\nputs calc(0,0)\n\n\n\n"}], "src_uid": "62766ef9a0751cbe7987020144de7512"} {"nl": {"description": "Little Petya very much likes rectangles and especially squares. Recently he has received 8 points on the plane as a gift from his mother. The points are pairwise distinct. Petya decided to split them into two sets each containing 4 points so that the points from the first set lay at the vertexes of some square and the points from the second set lay at the vertexes of a rectangle. Each point of initial 8 should belong to exactly one set. It is acceptable for a rectangle from the second set was also a square. If there are several partitions, Petya will be satisfied by any of them. Help him find such partition. Note that the rectangle and the square from the partition should have non-zero areas. The sides of the figures do not have to be parallel to the coordinate axes, though it might be the case.", "input_spec": "You are given 8 pairs of integers, a pair per line — the coordinates of the points Petya has. The absolute value of all coordinates does not exceed 104. It is guaranteed that no two points coincide.", "output_spec": "Print in the first output line \"YES\" (without the quotes), if the desired partition exists. In the second line output 4 space-separated numbers — point indexes from the input, which lie at the vertexes of the square. The points are numbered starting from 1. The numbers can be printed in any order. In the third line print the indexes of points lying at the vertexes of a rectangle in the similar format. All printed numbers should be pairwise distinct. If the required partition does not exist, the first line should contain the word \"NO\" (without the quotes), after which no output is needed.", "sample_inputs": ["0 0\n10 11\n10 0\n0 11\n1 1\n2 2\n2 1\n1 2", "0 0\n1 1\n2 2\n3 3\n4 4\n5 5\n6 6\n7 7", "0 0\n4 4\n4 0\n0 4\n1 2\n2 3\n3 2\n2 1"], "sample_outputs": ["YES\n5 6 7 8\n1 2 3 4", "NO", "YES\n1 2 3 4\n5 6 7 8"], "notes": "NotePay attention to the third example: the figures do not necessarily have to be parallel to the coordinate axes."}, "positive_code": [{"source_code": "\npoints=[]\n8.times{|i|\n points.push(gets.split.map(&:to_i).push(i+1))\n}\npoints.permutation.each(){|i|\n dx=i[1][0]-i[0][0]\n dy=i[1][1]-i[0][1]\n if i[2][0]-i[1][0]!=dy then next end\n if i[3][0]-i[2][0]!=-dx then next end\n if i[0][0]-i[3][0]!=-dy then next end\n if i[2][1]-i[1][1]!=-dx then next end\n if i[3][1]-i[2][1]!=-dy then next end\n if i[0][1]-i[3][1]!=dx then next end\n dx=i[5][0]-i[4][0]\n dy=i[5][1]-i[4][1]\n if i[7][0]-i[6][0]!=-dx then next end\n if i[7][1]-i[6][1]!=-dy then next end\n dx2=i[6][0]-i[5][0]\n dy2=i[6][1]-i[5][1]\n if i[4][0]-i[7][0]!=-dx2 then next end\n if i[4][1]-i[7][1]!=-dy2 then next end\n if dx*dx2+dy*dy2!=0 then next end\n\n puts \"YES\"\n 4.times{|j|\n print \"#{i[j][2]} \"\n }\n puts\n 4.times{|j|\n print \"#{i[j+4][2]} \"\n }\n puts\n exit\n\n\n\n\n}\nputs \"NO\"\n"}, {"source_code": "E=1e-10\ndef rectangle?(a,b,c,d)\n\treturn [a,b,c,d] if rect?(a,b,c,d)\n\treturn [a,c,b,d] if rect?(a,c,b,d)\n\treturn [b,a,c,d] if rect?(b,a,c,d)\n\tnil\nend\n\ndef rect?(a,b,c,d)\n\tm_ac=mid_pts(a,c)\n\treturn nil if same_pos?(a,m_ac)\n\tm_bd=mid_pts(b,d)\n\treturn nil if same_pos?(b,m_bd)\n\treturn nil unless same_pos?(m_ac,m_bd)\n\treturn nil unless rect_angle?(a,b,c)\n\treturn nil unless rect_angle?(b,c,d)\n\ttrue\nend\n\ndef mid_pts(a,b)\n\t[(a[0]+b[0])/2,(a[1]+b[1])/2]\nend\n\ndef dist(a,b)\n\t(a[0]-b[0])**2+(a[1]-b[1])**2\nend\n\ndef rect_angle?(a,b,c)\n\tp,q=b[0]-a[0],b[1]-a[1]\n\tr,s=b[0]-c[0],b[1]-c[1]\n\tsame_val?(p*r+q*s,0.0)\nend\n\ndef same_val?(x,y)\n\t(x-y).abs 0\n os += ary[s]\n else\n zs += ary[s]\n end\n end\n os != zs\nend\n\ndef print_ans(n, bm)\n zc = 0\n zpos = []\n n.times do |s|\n b = (1 << s)\n if b & bm == 0\n zc += 1\n zpos << (s+1)\n end\n end\n puts zc\n puts zpos.join(\" \")\nend\n\nn = gets.to_i\nary = gets.strip.split.map(&:to_i)\n\nif n == 1\n puts -1\nelsif n == 2\n if ary[0] == ary[1]\n puts -1\n else\n puts \"1 1\"\n end\nelse\n ans_found = false\n 1.upto((2**n)-2).each do |bm|\n break if ans_found\n if unequal_split(n, bm, ary)\n print_ans(n, bm)\n ans_found = true\n end\n end\n puts -1 unless ans_found\nend"}, {"source_code": "n = gets.chomp\nn = n.to_i\n\narr = gets.chomp\narr = arr.split \" \"\narr.map! {|a| a.to_i}\n\nif (arr.count <= 1) or (arr.count == 2 and arr[0] == arr[1])\n puts \"-1\"\nelse\n puts \"1\"\n min = arr.min\n puts arr.index(min) + 1\nend\n\n\n"}], "negative_code": [{"source_code": "n = gets.chomp\nn = n.to_i\n\narr = gets.chomp\narr = arr.split \" \"\narr.map! {|a| a.to_i}\n\nif (arr.count <= 1) or (arr.count == 2 and arr[0] == arr[1])\n puts \"-1\"\nelse\n puts \"1\"\n puts arr[0 + (arr.count/2)]\nend\n\n\n"}, {"source_code": "n = gets.chomp\nn = n.to_i\n\narr = gets.chomp\narr = arr.split \" \"\narr.map! {|a| a.to_i}\n\nif (arr.count <= 1) or (arr.count == 2 and arr[0] == arr[1])\n puts \"-1\"\nelse\n puts \"1\"\n arr.sort!\n puts arr[0 + arr.count/2]\nend\n\n\n"}, {"source_code": "n = gets.chomp\nn = n.to_i\n\narr = gets.chomp\narr = arr.split \" \"\narr.map! {|a| a.to_i}\n\nif (arr.count <= 1) or (arr.count == 2 and arr[0] == arr[1])\n puts \"-1\"\nelse\n puts \"1\"\n min = arr.min\n puts \"1\"\nend\n\n\n"}, {"source_code": "n = gets.chomp\nn = n.to_i\n\narr = gets.chomp\narr = arr.split \" \"\narr.map! {|a| a.to_i}\n\nif (arr.count <= 1) or (arr.count == 2 and arr[0] == arr[1])\n puts \"-1\"\nelse\n puts \"1\"\n puts arr[0]\nend\n\n\n"}, {"source_code": "n = gets.chomp\nn = n.to_i\n\narr = gets.chomp\narr = arr.split \" \"\narr.map! {|a| a.to_i}\n\nif (arr.count <= 1) or (arr.count == 2 and arr[0] == arr[1])\n puts \"-1\"\nelse\n puts \"1\"\n \n i = 0\n dup = true\n while i <= arr.count - 2\n dup = false if arr[i] != arr[i + 1]\n i = i + 1\n end\n \n even = (arr.count % 2 == 0)\n \n if dup and !even\n puts \"-1\"\n else\n puts arr[0]\n end\nend\n\n\n"}, {"source_code": "n = gets.chomp\nn = n.to_i\n\narr = gets.chomp\narr = arr.split \" \"\narr.map! {|a| a.to_i}\n\nif (arr.count <= 1) or (arr.count == 2 and arr[0] == arr[1])\n puts \"-1\"\nelse\n puts \"1\"\n \n sum = arr.reduce 0, :+ \n dup = true\n arr.each_with_index do |a, i|\n break if i >= arr.count - 2\n \n next if arr[i] == arr[i + 1]\n \n if arr[i] != arr[i+1]\n dup = false\n break\n end\n end\n \n if dup\n puts -1\n else\n puts arr[0]\n end\nend\n\n\n"}, {"source_code": "n = gets.chomp\nn = n.to_i\n\narr = gets.chomp\narr = arr.split \" \"\narr.map! {|a| a.to_i}\n\nif (arr.count <= 1) or (arr.count == 2 and arr[0] == arr[1])\n puts \"-1\"\nelse\n puts \"1\"\n \n sum = arr.reduce 0, :+ \n \n if arr[0] == sum - arr[0]\n puts arr[1]\n else\n puts arr[0]\n end\nend\n\n\n"}, {"source_code": "n = gets.chomp\nn = n.to_i\n\narr = gets.chomp\narr = arr.split \" \"\narr.map! {|a| a.to_i}\n\nif (arr.count <= 1) or (arr.count == 2 and arr[0] == arr[1])\n puts \"-1\"\nelse\n puts \"1\"\n \n i = 0\n dup = true\n while i <= arr.count - 2\n dup = false if arr[i] != arr[i + 1]\n i = i + 1\n end\n \n even = (arr.count % 2 == 0)\n \n if dup and even\n puts \"-1\"\n else\n puts arr[0]\n end\nend\n\n\n"}, {"source_code": "n = gets.chomp\nn = n.to_i\n\narr = gets.chomp\narr = arr.split \" \"\narr.map! {|a| a.to_i}\n\nif (arr.count <= 1) or (arr.count == 2 and arr[0] == arr[1])\n puts \"-1\"\nelse\n puts \"1\"\n \n sum = arr.reduce 0, :+ \n \n if arr[0] == sum - arr[0]\n puts -1\n else\n puts arr[0]\n end\nend\n\n\n"}, {"source_code": "n = gets.chomp\nn = n.to_i\n\narr = gets.chomp\narr = arr.split \" \"\narr.map! {|a| a.to_i}\n\nif (arr.count <= 1) or (arr.count == 2 and arr[0] == arr[1])\n puts \"-1\"\nelse\n puts \"1\"\n puts arr[-1]\nend\n\n\n"}, {"source_code": "n = gets.chomp\nn = n.to_i\n\narr = gets.chomp\narr = arr.split \" \"\narr.map! {|a| a.to_i}\n\nif (arr.count <= 1) or (arr.count == 2 and arr[0] == arr[1])\n puts \"-1\"\nelse\n puts \"1\"\n arr.sort!\n puts arr[-1]\nend\n\n\n"}], "src_uid": "2b55012c899645bac8d293e85e73148f"} {"nl": {"description": "Even if you just leave them be, they will fall to pieces all by themselves. So, someone has to protect them, right?You find yourself playing with Teucer again in the city of Liyue. As you take the eccentric little kid around, you notice something interesting about the structure of the city.Liyue can be represented as a directed graph containing $$$n$$$ nodes. Nodes are labeled from $$$1$$$ to $$$n$$$. There is a directed edge from node $$$a$$$ to node $$$b$$$ if and only if $$$a < b$$$.A path between nodes $$$a$$$ and $$$b$$$ is defined as a sequence of edges such that you can start at $$$a$$$, travel along all of these edges in the corresponding direction, and end at $$$b$$$. The length of a path is defined by the number of edges. A rainbow path of length $$$x$$$ is defined as a path in the graph such that there exists at least 2 distinct colors among the set of $$$x$$$ edges.Teucer's favorite number is $$$k$$$. You are curious about the following scenario: If you were to label each edge with a color, what is the minimum number of colors needed to ensure that all paths of length $$$k$$$ or longer are rainbow paths?Teucer wants to surprise his older brother with a map of Liyue. He also wants to know a valid coloring of edges that uses the minimum number of colors. Please help him with this task!", "input_spec": "The only line of input contains two integers $$$n$$$ and $$$k$$$ ($$$2 \\leq k < n \\leq 1000$$$). ", "output_spec": "On the first line, output $$$c$$$, the minimum colors you need to satisfy the above requirements. On the second line, print a valid edge coloring as an array of $$$\\frac{n(n-1)}{2}$$$ integers ranging from $$$1$$$ to $$$c$$$. Exactly $$$c$$$ distinct colors should exist in the construction. Print the edges in increasing order by the start node first, then by the second node. For example, if $$$n=4$$$, the edge colors will correspond to this order of edges: ($$$1$$$, $$$2$$$), ($$$1$$$, $$$3$$$), ($$$1$$$, $$$4$$$), ($$$2$$$, $$$3$$$), ($$$2$$$, $$$4$$$), ($$$3$$$, $$$4$$$)", "sample_inputs": ["5 3", "5 2", "8 7", "3 2"], "sample_outputs": ["2\n1 2 2 2 2 2 2 1 1 1", "3\n3 2 2 1 2 2 1 3 1 1", "2\n2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1", "2\n1 2 2"], "notes": "NoteThe corresponding construction for the first test case looks like this: It is impossible to satisfy the constraints with less than $$$2$$$ colors.The corresponding construction for the second test case looks like this: One can show there exists no construction using less than $$$3$$$ colors."}, "positive_code": [{"source_code": "n,$k=gets.split.map(&:to_i)\r\ndef maxpw x,y\r\n ret=0\r\n while x!=y\r\n x,y=x/$k,y/$k\r\n ret+=1\r\n end\r\n return ret\r\nend\r\nputs maxpw(0,n-1)\r\n(n-1).times do|i|\r\n (n-i-1).times do|j|\r\n print maxpw(i,i+j+1),' '\r\n end\r\nend"}], "negative_code": [{"source_code": "n,$k=gets.split.map(&:to_i)\r\ndef maxpw x,y\r\n ret=1\r\n while x!=y\r\n x,y=x/$k,y/$k\r\n ret+=1\r\n end\r\n return ret\r\nend\r\nputs maxpw(0,n-1)-1\r\n(n-1).times do|i|\r\n (n-i-1).times do|j|\r\n print maxpw(i,i+j),' '\r\n end\r\nend"}], "src_uid": "0a157c97599c2240ca916317524c67e5"} {"nl": {"description": "Bash wants to become a Pokemon master one day. Although he liked a lot of Pokemon, he has always been fascinated by Bulbasaur the most. Soon, things started getting serious and his fascination turned into an obsession. Since he is too young to go out and catch Bulbasaur, he came up with his own way of catching a Bulbasaur.Each day, he takes the front page of the newspaper. He cuts out the letters one at a time, from anywhere on the front page of the newspaper to form the word \"Bulbasaur\" (without quotes) and sticks it on his wall. Bash is very particular about case — the first letter of \"Bulbasaur\" must be upper case and the rest must be lower case. By doing this he thinks he has caught one Bulbasaur. He then repeats this step on the left over part of the newspaper. He keeps doing this until it is not possible to form the word \"Bulbasaur\" from the newspaper.Given the text on the front page of the newspaper, can you tell how many Bulbasaurs he will catch today?Note: uppercase and lowercase letters are considered different.", "input_spec": "Input contains a single line containing a string s (1  ≤  |s|  ≤  105) — the text on the front page of the newspaper without spaces and punctuation marks. |s| is the length of the string s. The string s contains lowercase and uppercase English letters, i.e. .", "output_spec": "Output a single integer, the answer to the problem.", "sample_inputs": ["Bulbbasaur", "F", "aBddulbasaurrgndgbualdBdsagaurrgndbb"], "sample_outputs": ["1", "0", "2"], "notes": "NoteIn the first case, you could pick: Bulbbasaur.In the second case, there is no way to pick even a single Bulbasaur.In the third case, you can rearrange the string to BulbasaurBulbasauraddrgndgddgargndbb to get two words \"Bulbasaur\"."}, "positive_code": [{"source_code": "word = gets.chomp\ncontainer = {}\n\n\"Blbsaur\".each_char { |x| container[x] = 0 }\n\nword.each_char do |x|\n\tif container.has_key?(x)\n\t\tcontainer[x] += 1\n\tend\nend\n\ncontainer[ \"a\" ] /= 2\ncontainer[ \"u\" ] /= 2\n\nputs container.values.min\n"}, {"source_code": "x,y=['Bulbasaur',gets.chomp].map{|s|\n\th=Hash.new 0\n\ts.chars{|c|h[c]+=1}\n\th\n}\np x.map{|k,v|y[k]/v}.min"}, {"source_code": "#!/usr/bin/env ruby\ncount = gets.chomp.chars.reduce(Hash.new(0)) do |count, c|\n count.tap{ |h| h[c] += 1 }\n end\nputs [\n count['B'],\n count['u'] / 2,\n count['l'],\n count['b'],\n count['a'] / 2,\n count['s'],\n count['r'],\n].min\n"}, {"source_code": "s = gets.strip\nexp_counts = { 'B' => 1, 'u' => 2, 'l' => 1, 'b' => 1, 'a' => 2, 's' => 1, 'r' => 1 }\ncounts = {}\ns.each_char do |c|\n counts[c] = 0 if !counts[c]\n counts[c] += 1\nend\nact_counts = counts.select { |k, v| exp_counts.keys.include? k }\nans = 0\nif act_counts.length == exp_counts.length\n bu_cnt = act_counts['B'] / exp_counts['B']\n u_cnt = act_counts['u'] / exp_counts['u']\n l_cnt = act_counts['l'] / exp_counts['l']\n b_cnt = act_counts['b'] / exp_counts['b']\n a_cnt = act_counts['a'] / exp_counts['a']\n s_cnt = act_counts['s'] / exp_counts['s']\n r_cnt = act_counts['r'] / exp_counts['r']\n ans = [bu_cnt, u_cnt, l_cnt, b_cnt, a_cnt, s_cnt, r_cnt].min\nend\nputs ans\n"}, {"source_code": "a=gets.chomp\n\nans=0\nh=Hash.new(0)\n\"Bulbasaur\".chars{|e|\n\th[e]=0\n}\n\na.chars{|e|\n\tif h.key?(e) then\n\t\th[e]+=1\n\tend\n}\nh[\"a\"]/=2\nh[\"u\"]/=2\n\nans=h[\"B\"];\nh.each{|k,v|\n\tans=v if ans>v\n}\n\nputs ans\n\n"}, {"source_code": "c=[]\ngets.bytes{|e| c[e] ? c[e]+=1 : c[e]=1}\nr=10**9\n\"Blbsr\".bytes{|e| c[e]||=0; r=[r, c[e]].min}\n\"au\".bytes{|e| c[e]||=0; r=[r, c[e]/2].min}\nputs r\n"}, {"source_code": "#!/usr/bin/env ruby\n\nnewspapper = gets.strip\nletter_count = Hash.new(0)\npokemon = 'Bulbasaur'\nnewspapper.each_char do |char|\n letter_count[char] += 1 if pokemon.include? char\nend\n\ndef compute_count(letter_count, pokemon)\n count = 0\n loop do\n pokemon.each_char do |char|\n cur_count = letter_count[char]\n return count if cur_count == 0\n letter_count[char] = cur_count - 1\n end\n count += 1\n end\nend\n\nputs compute_count(letter_count, pokemon)\n"}, {"source_code": "s = gets.chomp\nrequired = {'B' => 1, 'u' => 2,'l' => 1, 'b' => 1,'a' => 2,'s' => 1,'r' => 1}\ncount = Hash.new(0)\ns.each_char do |a|\n\tcount[a] += 1\nend\nval = []\nrequired.each_key do |k|\n\tval << count[k] / required[k]\nend\nputs val.min"}, {"source_code": "s = gets.chomp\nh = Hash.new(0)\ns.each_char{|c| h[c] += 1}\nputs [h['B'], h['u'] / 2, h['l'], h['b'], h['a'] / 2, h['s'], h['r']].min\n"}, {"source_code": "inp = gets.strip\nstr = \"Bulbasaur\"\nputs str.chars.map { |c| inp.count(c) / str.count(c) }.min\n"}], "negative_code": [{"source_code": "a=gets.chomp\n \nans=0\nh=Hash.new(0)\n\"Bulbasaur\".chars{|e|\n\th[e]=0\n}\n \na.chars{|e|\n\tif h.key?(e) then\n\t\th[e]+=1\n\tend\n}\nh[\"a\"]/=2\nans=h[\"B\"];\nh.each{|k,v|\n\tans=v if ans>v\n}\n \nputs ans\n\n"}, {"source_code": "c=[]\ngets.bytes{|e| c[e] ? c[e]+=1 : c[e]=1}\nr=10**9\n\"Bulbasaur\".bytes{|e| c[e]||=0; r=[r, c[e]].min}\nputs r\n"}], "src_uid": "9c429fd7598ea75acce09805a15092d0"} {"nl": {"description": "Given an array $$$a$$$ of length $$$n$$$ and an integer $$$k$$$, you are tasked to find any two numbers $$$l$$$ and $$$r$$$ ($$$l \\leq r$$$) such that: For each $$$x$$$ $$$(l \\leq x \\leq r)$$$, $$$x$$$ appears in $$$a$$$ at least $$$k$$$ times (i.e. $$$k$$$ or more array elements are equal to $$$x$$$). The value $$$r-l$$$ is maximized. If no numbers satisfy the conditions, output -1.For example, if $$$a=[11, 11, 12, 13, 13, 14, 14]$$$ and $$$k=2$$$, then: for $$$l=12$$$, $$$r=14$$$ the first condition fails because $$$12$$$ does not appear at least $$$k=2$$$ times. for $$$l=13$$$, $$$r=14$$$ the first condition holds, because $$$13$$$ occurs at least $$$k=2$$$ times in $$$a$$$ and $$$14$$$ occurs at least $$$k=2$$$ times in $$$a$$$. for $$$l=11$$$, $$$r=11$$$ the first condition holds, because $$$11$$$ occurs at least $$$k=2$$$ times in $$$a$$$. A pair of $$$l$$$ and $$$r$$$ for which the first condition holds and $$$r-l$$$ is maximal is $$$l = 13$$$, $$$r = 14$$$.", "input_spec": "The first line of the input contains a single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains the integers $$$n$$$ and $$$k$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$, $$$1 \\leq k \\leq n$$$) — the length of the array $$$a$$$ and the minimum amount of times each number in the range $$$[l, r]$$$ should appear respectively. Then a single line follows, containing $$$n$$$ integers describing the array $$$a$$$ ($$$1 \\leq a_i \\leq 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case output $$$2$$$ numbers, $$$l$$$ and $$$r$$$ that satisfy the conditions, or \"-1\" if no numbers satisfy the conditions. If multiple answers exist, you can output any.", "sample_inputs": ["4\n\n7 2\n\n11 11 12 13 13 14 14\n\n5 1\n\n6 3 5 2 1\n\n6 4\n\n4 3 4 3 3 4\n\n14 2\n\n1 1 2 2 2 3 3 3 3 4 4 4 4 4"], "sample_outputs": ["13 14\n1 3\n-1\n1 4"], "notes": null}, "positive_code": [{"source_code": "gets.to_i.times do\r\n n, k = gets.split.map(&:to_i)\r\n as = gets.split.map(&:to_i)\r\n xs = as.tally.filter_map { |a, count| a if count >= k }.sort\r\n ys = xs.chunk_while { |a, b| a + 1 == b }.max_by(&:size)\r\n if ys\r\n puts \"#{ys[0]} #{ys[-1]}\"\r\n else\r\n p -1\r\n end\r\nend\r\n"}, {"source_code": "gets.to_i.times do\n n, k = gets.split.map(&:to_i)\n freq = Hash.new(0)\n gets.split.map(&:to_i).each { freq[_1] += 1 }\n freq.filter! { |_, v| v >= k }\n keys = freq.keys.sort\n\n best = [0, nil]\n cur = [0, nil]\n prev = nil\n\n keys.each do |key|\n if prev && (prev + 1) == key\n cur[0] += 1\n else\n cur[0] = 1\n end\n cur[1] = key\n prev = key\n best = cur.clone if cur[0] > best[0]\n end\n\n unless best[1]\n p -1\n next\n end\n\n puts \"#{best[1] - best[0] + 1} #{best[1]}\"\nend\n"}, {"source_code": "def solve\n _, x = gli\n a = gli.tally.map { |k, v| k if v >= x }.compact.sort\n if a.empty?\n puts(-1)\n return\n end\n ans = 0\n al = a[0]\n cur = 0\n l = a[0]\n (1...a.size).each do |i|\n if a[i - 1] + 1 != a[i]\n if ans < cur\n al = l\n ans = cur\n end\n cur = 0\n l = a[i]\n else\n cur += 1\n end\n end\n if ans < cur\n al = l\n ans = cur\n end\n puts [al, al + ans].join(' ')\nend\n\nMOD = 10**9 + 7\ndef gs = gets.chomp\ndef gi = gets.to_i\ndef gli = gets.split.map(&:to_i)\ndef cyesno(x) = x ? :Yes : :No\ndef uyesno(x) = x ? :YES : :NO\n\ngets.to_i.times { solve }\n"}, {"source_code": "main = -> {\r\n t = int\r\n t.times do\r\n n, k = ints\r\n a = ints.tally\r\n prev = -1\r\n prev_l = nil\r\n count = 0\r\n ans = -1\r\n ans_len = -1\r\n a.keys.sort.each do |key|\r\n if a[key] >= k\r\n if prev == key - 1\r\n else\r\n prev_l = key\r\n end\r\n if ans_len < key - prev_l\r\n ans = \"#{prev_l} #{key}\"\r\n ans_len = key - prev_l\r\n end\r\n prev = key\r\n end\r\n end\r\n puts ans\r\n end\r\n}\r\n\r\nDEBUG = true\r\nMOD = 10**9+7\r\n$yesno = %w(No Yes)\r\nYesNo = %w(No Yes)\r\nYESNO = %w(NO YES)\r\nINF = 10**18\r\n\r\nrequire \"set\"\r\nrequire \"prime\"\r\n\r\ndef int; gets.to_s.to_i end\r\ndef ints; gets.to_s.split.map { |s| s.to_i } end\r\ndef int1s; gets.to_s.split.map { |s| s.to_i - 1 } end\r\ndef float; gets.to_s.to_f end\r\ndef floats; gets.to_s.split.map { |s| s.to_f } end\r\ndef array_of(&convert); gets.to_s.split.map(&convert) end\r\ndef string; gets.to_s.chomp end\r\ndef strings; gets.to_s.split end\r\ndef rep(n, &b); Array.new(n, &b) end\r\ndef yes; puts $yesno[1] end\r\ndef no; puts $yesno[0] end\r\ndef yesno t; puts $yesno[t] end\r\ndef YesNo t; puts YesNo[t] end\r\ndef YESNO t; puts YESNO[t] end\r\ndef zip(xs, *yss); Enumerator.new { |y| xs.zip(*yss) { |a| y.yield(*a) } } end\r\ndef max(*xs, &block); block_given? ? xs.max_by(&block) : xs.max end\r\ndef min(*xs, &block); block_given? ? xs.min_by(&block) : xs.min end\r\ndef minmax(*xs, &block); block_given? ? xs.minmax_by(&block) : xs.minmax end\r\ndef gcd(*xs); xs.inject(0, :gcd) end\r\ndef matrix(h, w, fill=nil, &block); return Array.new(h) { Array.new(w, &block) } if block_given?; Array.new(h) { [fill] * w } end\r\ndef debug(x = nil); STDERR.puts (block_given? ? yield(x) : x).inspect if DEBUG; x end\r\ndef debug_grid(grid, width = 1); grid.each { |row| STDERR.puts row.map { |x| x.inspect.ljust(width) }.join(\"\") } if DEBUG; grid end\r\ndef if_debug; yield if DEBUG end\r\n\r\nmodule Boolean\r\n def coerce(other); [other, to_i] end\r\n def +@; to_i end\r\n def to_int; to_i end\r\n def *(other); to_i * other end\r\nend\r\n\r\nclass TrueClass\r\n include Boolean\r\n def to_i; 1 end\r\nend\r\n\r\nclass FalseClass\r\n include Boolean\r\n def to_i; 0 end\r\nend\r\n\r\nclass Integer\r\n def div_ceil(y); (self + y - 1) / y end\r\n def mod_inv(mod = MOD); pow(mod-2, mod) end\r\n def mod_div(y, mod = MOD); self * y.mod_inv(mod) % mod end\r\n def mod_nCr(r, mod = MOD); x = y = 1; (1..r).each { |i| x = x * (self + 1 - i) % mod; y = y * i % mod }; x.mod_div(y, mod); end\r\n def factorial(mod = MOD); (2..self).inject(1) { |f, x| f * x % mod } end\r\n def popcount; x = self; c = 0; while x > 0; c += 1 if x & 1 == 1; x >>= 1 end; c end #TODO: faster\r\n def bitbrute(&block); (1< 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\r\n def lower_bound; ac, wa = self.end_open, self.begin; while ac - wa > 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\r\n def shakutori(r2, &pred); Enumerator.new { |y| j, r = r2.begin, r2.end_open; each { |i| j += 1 while j + 1 < r and pred[i, j+1]; y.yield(i, j) } }; end\r\n def widest(&block); Enumerator.new { |y| j, n = self.begin, self.end_open; each { |i| j += 1 while j < n and block[i, j]; y.yield(i, j) if block[i, j] } } end\r\nend\r\n\r\nclass Array\r\n def power(&block); (0 ... 1 << size).each(&block) end\r\n def sorted_merge(other); a = []; i = j = 0; n, m = size, other.size; if j < m and other[j] < self[i]; a << other[j]; j += 1 else; a << self[i]; i += 1 end while i < n; a.push(*other[j..-1]) if j < m; a end\r\n def upper_bound; ac, wa = 0, size; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\r\n def lower_bound; ac, wa = size, 0; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\r\n def cum(*xs, &op); a = []; a << xs[0] if xs.size > 0; a << x = self[0]; (1...size).each { |i| a << x = op[x, self[i]] }; a end\r\n def cumdiff(range); self[range.end_open] - self[range.begin]; end\r\n def compress(kinds = uniq.sort!); map { |x| kinds.bsearch_index { _1 >= x } }; end\r\nend\r\n\r\nmodule Enumerable\r\n def sorted_uniq; x = nil; filter { |y| c = x === y; x = y; !c } end\r\n def cumsum; ys = [0]; each { |x| ys << x + ys[-1] }; ys end\r\nend\r\n\r\nmain[]"}], "negative_code": [], "src_uid": "13fd45f1892f96eab1d5ab966f465a05"} {"nl": {"description": "You are given three integers $$$a$$$, $$$b$$$, $$$k$$$.Find two binary integers $$$x$$$ and $$$y$$$ ($$$x \\ge y$$$) such that both $$$x$$$ and $$$y$$$ consist of $$$a$$$ zeroes and $$$b$$$ ones; $$$x - y$$$ (also written in binary form) has exactly $$$k$$$ ones. You are not allowed to use leading zeros for $$$x$$$ and $$$y$$$. ", "input_spec": "The only line contains three integers $$$a$$$, $$$b$$$, and $$$k$$$ ($$$0 \\leq a$$$; $$$1 \\leq b$$$; $$$0 \\leq k \\leq a + b \\leq 2 \\cdot 10^5$$$) — the number of zeroes, ones, and the number of ones in the result.", "output_spec": "If it's possible to find two suitable integers, print \"Yes\" followed by $$$x$$$ and $$$y$$$ in base-2. Otherwise print \"No\". If there are multiple possible answers, print any of them.", "sample_inputs": ["4 2 3", "3 2 1", "3 2 5"], "sample_outputs": ["Yes\n101000\n100001", "Yes\n10100\n10010", "No"], "notes": "NoteIn the first example, $$$x = 101000_2 = 2^5 + 2^3 = 40_{10}$$$, $$$y = 100001_2 = 2^5 + 2^0 = 33_{10}$$$, $$$40_{10} - 33_{10} = 7_{10} = 2^2 + 2^1 + 2^0 = 111_{2}$$$. Hence $$$x-y$$$ has $$$3$$$ ones in base-2.In the second example, $$$x = 10100_2 = 2^4 + 2^2 = 20_{10}$$$, $$$y = 10010_2 = 2^4 + 2^1 = 18$$$, $$$x - y = 20 - 18 = 2_{10} = 10_{2}$$$. This is precisely one 1.In the third example, one may show, that it's impossible to find an answer."}, "positive_code": [{"source_code": "a,b,k = gets.chomp.split(\" \").map(&:to_i)\r\nb -= 1\r\nif a == 0 && b == 0 && k == 0\r\n puts \"Yes\"\r\n puts 1\r\n puts 1\r\nelsif k >= a+b || (k != 0 && a == 0) || (b == 0 && k > 0)\r\n puts \"No\"\r\nelse\r\n puts \"Yes\"\r\n x = Array.new(a+b,0)\r\n y = Array.new(a+b,0)\r\n if b > 0\r\n y[-1] = 1\r\n x[-(k+1)] = 1\r\n end\r\n i = 0\r\n (b-1).times do\r\n while x[i] == 1 || y[i] == 1\r\n i += 1\r\n end\r\n x[i] = 1\r\n y[i] = 1\r\n i += 1\r\n end\r\n x.unshift(\"1\")\r\n y.unshift(\"1\")\r\n puts x.join\r\n puts y.join\r\nend"}, {"source_code": "zeroes, ones, k = gets.split(/\\s+/).map(&:to_i)\r\nx = ('1'*ones + '0'*zeroes).to_i(2)\r\n\r\nr = ('1'*k + '0'*[zeroes-k,0].max).to_i(2)\r\ny = x-r\r\n\r\nif y > 0 && y.to_s(2).count('1') == ones && y.to_s(2).count('0') == zeroes\r\n puts \"Yes\"\r\n puts x.to_s(2)\r\n puts y.to_s(2)\r\nelse\r\n puts \"No\"\r\nend\r\n"}, {"source_code": "zeroes, ones, k = gets.split(/\\s+/).map(&:to_i)\r\nx = ('1'*ones + '0'*zeroes).to_i(2)\r\n\r\nr = ('1'*k + '0'*[zeroes-k,0].max).to_i(2)\r\ny = x-r\r\n\r\nif y > 0 && y.to_s(2).count('1') == ones && y.to_s(2).count('0') == zeroes\r\n puts \"Yes\"\r\n puts x.to_s(2)\r\n puts y.to_s(2)\r\nelse\r\n puts \"No\"\r\nend"}], "negative_code": [{"source_code": "a,b,k = gets.chomp.split(\" \").map(&:to_i)\r\nb -= 1\r\nif k >= a+b || (k != 0 && a == 0)\r\n puts \"No\"\r\nelse\r\n puts \"Yes\"\r\n x = Array.new(a+b,0)\r\n y = Array.new(a+b,0)\r\n y[-1] = 1\r\n x[-(k+1)] = 1\r\n i = 0\r\n (b-1).times do\r\n while x[i] == 1 || y[i] == 1\r\n i += 1\r\n end\r\n x[i] = 1\r\n y[i] = 1\r\n i += 1\r\n end\r\n x.unshift(\"1\")\r\n y.unshift(\"1\")\r\n puts x.join\r\n puts y.join\r\nend"}, {"source_code": "a,b,k = gets.chomp.split(\" \").map(&:to_i)\r\nif k == a+b || (k != 0 && a == 0)\r\n puts \"No\"\r\nelse\r\n puts \"Yes\"\r\n x = Array.new(a+b,0)\r\n y = Array.new(a+b,0)\r\n y[-1] = 1\r\n x[-(k+1)] = 1\r\n i = 0\r\n (b-1).times do\r\n while x[i] == 1 || y[i] == 1\r\n i += 1\r\n end\r\n x[i] = 1\r\n y[i] = 1\r\n i += 1\r\n end\r\n puts x.join\r\n puts y.join\r\nend"}], "src_uid": "ea620a8dbef506567464dcaddcc2b34f"} {"nl": {"description": "You are given a correct solution of the sudoku puzzle. If you don't know what is the sudoku, you can read about it here.The picture showing the correct sudoku solution:Blocks are bordered with bold black color.Your task is to change at most $$$9$$$ elements of this field (i.e. choose some $$$1 \\le i, j \\le 9$$$ and change the number at the position $$$(i, j)$$$ to any other number in range $$$[1; 9]$$$) to make it anti-sudoku. The anti-sudoku is the $$$9 \\times 9$$$ field, in which: Any number in this field is in range $$$[1; 9]$$$; each row contains at least two equal elements; each column contains at least two equal elements; each $$$3 \\times 3$$$ block (you can read what is the block in the link above) contains at least two equal elements. It is guaranteed that the answer exists.You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of $$$9$$$ lines, each line consists of $$$9$$$ characters from $$$1$$$ to $$$9$$$ without any whitespaces — the correct solution of the sudoku puzzle.", "output_spec": "For each test case, print the answer — the initial field with at most $$$9$$$ changed elements so that the obtained field is anti-sudoku. If there are several solutions, you can print any. It is guaranteed that the answer exists.", "sample_inputs": ["1\n154873296\n386592714\n729641835\n863725149\n975314628\n412968357\n631457982\n598236471\n247189563"], "sample_outputs": ["154873396\n336592714\n729645835\n863725145\n979314628\n412958357\n631457992\n998236471\n247789563"], "notes": null}, "positive_code": [{"source_code": "gets.to_i.times do\n\ta = (1..9).map { gets.chomp.split('').map &:to_i }\n\t(0...9).each do |i|\n\t\tj = i % 3 * 3 + i / 3\n\t\ta[i][j] = a[i][j] == 9 ? 1 : a[i][j] + 1\n\tend\n\tputs a.map(&:join)\nend\n"}, {"source_code": "puts STDIN.read.gsub('1','2').split[1..-1]"}, {"source_code": "t = gets.chomp.to_i\n\n(1..t).each{\n (0...9).each{|i|\n s = gets.chomp\n a = i / 3\n b = i % 3\n \n n = 3*b + a\n if s[n] == '9' then\n s[n] = '1'\n else\n s[n] = (s[n].ord + 1).chr\n end\n \n puts s\n }\n}"}], "negative_code": [], "src_uid": "0e21f1c48c8c0463b2ffa7275eddc633"} {"nl": {"description": "Imagine that you have a twin brother or sister. Having another person that looks exactly like you seems very unusual. It's hard to say if having something of an alter ego is good or bad. And if you do have a twin, then you very well know what it's like.Now let's imagine a typical morning in your family. You haven't woken up yet, and Mom is already going to work. She has been so hasty that she has nearly forgotten to leave the two of her darling children some money to buy lunches in the school cafeteria. She fished in the purse and found some number of coins, or to be exact, n coins of arbitrary values a1, a2, ..., an. But as Mom was running out of time, she didn't split the coins for you two. So she scribbled a note asking you to split the money equally.As you woke up, you found Mom's coins and read her note. \"But why split the money equally?\" — you thought. After all, your twin is sleeping and he won't know anything. So you decided to act like that: pick for yourself some subset of coins so that the sum of values of your coins is strictly larger than the sum of values of the remaining coins that your twin will have. However, you correctly thought that if you take too many coins, the twin will suspect the deception. So, you've decided to stick to the following strategy to avoid suspicions: you take the minimum number of coins, whose sum of values is strictly more than the sum of values of the remaining coins. On this basis, determine what minimum number of coins you need to take to divide them in the described manner.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 100) — the number of coins. The second line contains a sequence of n integers a1, a2, ..., an (1 ≤ ai ≤ 100) — the coins' values. All numbers are separated with spaces.", "output_spec": "In the single line print the single number — the minimum needed number of coins.", "sample_inputs": ["2\n3 3", "3\n2 1 2"], "sample_outputs": ["2", "2"], "notes": "NoteIn the first sample you will have to take 2 coins (you and your twin have sums equal to 6, 0 correspondingly). If you take 1 coin, you get sums 3, 3. If you take 0 coins, you get sums 0, 6. Those variants do not satisfy you as your sum should be strictly more that your twins' sum.In the second sample one coin isn't enough for us, too. You can pick coins with values 1, 2 or 2, 2. In any case, the minimum number of coins equals 2. "}, "positive_code": [{"source_code": "ln1 = gets\nn = ln1.to_i\nln2 = gets\ns = ln2.split(' ')\na = Array.new(0)\nsum = 0\nfor i in 0..n-1\n a << s[i].to_i\n sum += a[i]\nend\na = a.sort\na = a.reverse\nidx = 0\nhave = 0\nfor i in 0..n-1\n sum -= a[i]\n have += a[i]\n if(have > sum)\n idx = i\n break\n end\nend\np idx + 1\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nsum = 0\na.each { |e|\n sum += e\n}\na.sort!.reverse!\nres = 0\nhave = 0\nn.times { |i|\n sum -= a[i]\n have += a[i]\n (res = i + 1; break) if have > sum\n}\np res\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i).sort.reverse\nsum = 0\na.each { |e|\n sum += e\n}\nres = 0\nhave = 0\nn.times { |i|\n sum -= a[i]\n have += a[i]\n (res = i + 1; break) if have > sum\n}\np res\n"}, {"source_code": "n = gets.to_i\nm = gets.split(' ').map{|_|_.to_i}.sort{|x,y| y<=>x}\ntotal = m.inject(:+)\nsum=0\nm.each.with_index do |obj,id|\n sum+=obj\n if sum*2>total\n puts id+1\n break\n end\nend\n"}, {"source_code": "gets\na = gets.strip.split.map(&:to_i)\nsum = a.sort!.reverse!.inject(0) {|acc, m| acc + m }\nf = 0\ni = 0\nwhile f <= sum-f\n f += a[i]\n i += 1\nend\nputs i"}, {"source_code": "n = gets\ncoins = gets.chomp.split(' ').map(&:to_i).sort{|x,y| y <=> x}\nsum = coins.reduce(:+)\ncounter = 0\ntaken_sum = 0\ncoins.each do |coin|\n taken_sum += coin\n counter += 1\n sum -= coin\n break if taken_sum > sum\nend\n\nputs counter"}, {"source_code": "n = gets.chomp.to_i\n\ncoins = gets.chomp.split().map { |x| x.to_i }\ncoins.sort!\ncoins.reverse!\n\nsumLeft = 0\nfor i in coins\n sumLeft += i\nend\n\ni = 0\nsumRight = 0\nwhile i < n\n sumLeft -= coins[i]\n sumRight += coins[i]\n if sumRight > sumLeft\n break\n end\n i += 1\nend\n\nputs (i+1)\n"}, {"source_code": "n = gets.to_i\nst = gets.split\ni = 0\nsum = 0\nwhile ij && s1+st[i]<=min) do\n i -= 1\n end\n if i<=j then \n s1 = s1 + st[j]\n else \n s1 = s1 + st[i]\n end\n coins += 1\nend\nputs \"#{coins}\"\n\n\n"}, {"source_code": "n = gets.chomp.to_i\nm = gets.chomp.split(/ /).map!{|x| x.to_i}\nm.sort!.reverse!\nsum = 0\nfor i in 0..m.length-1\n\tsum += m[i]\nend\nwsum = 0\nret = 0\nfor i in 0..m.length-1\n\tret += 1\n\twsum += m[i]\n\trest = sum - wsum\n\tif rest < wsum\n\t\tbreak\n\tend\nend\nputs ret"}, {"source_code": "# cook your code here\n\nn = gets.to_i \na = gets.chomp.split \nsum = 0 \ncum = Array.new \n\nfor i in 0...n \n a[i] = a[i].to_i \n sum += a[i] \nend \n\na = a.sort { |x,y| y.to_i <=> x.to_i } \n\ncum[0] = a[0]\ni = 0 \n\nuntil cum[i] > sum/2 \n i+=1 \n cum[i] = cum[i-1] + a[i] \nend \n\nputs i+1 "}, {"source_code": "n,*a=gets(p).split.map &:to_i;z=eval a*?+;p n-a.sort.index{|i|0>=z-=i*2}"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i).sort.reverse\nb = []\na.each do |_|\n b << _\n if a.inject(&:+) < b.inject(&:+) * 2\n p b.size\n break\n end\nend\n"}, {"source_code": "n,*a=gets(p).split.map &:to_i;z=eval a*?+;p n-a.sort.index{|i|0>=z-=i*2}"}, {"source_code": "n,*a=gets(p).split.map &:to_i;z=eval a*?+;n-=1while 0sum\n p ans\n exit\n end\n}\n"}, {"source_code": "n,*a=gets(p).split.map &:to_i;z=eval a*?+;p n-a.sort.index{|i|0>=z-=i*2}\n"}, {"source_code": "n = gets.to_i\na = gets.split.map &:to_i\na.sort!{|a,b|b<=>a}\n\nsumAll = 0\na.each {|el| sumAll += el }\n\ncnt = 0\nsum = 0\na.each do |el|\n sum += el\n cnt += 1\n break if sum > sumAll / 2\nend\nputs cnt"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split(\" \").map{|e| e.to_i}.sort!{|a, b| -(a <=> b)}\nb = []\nc = 0\nwhile a.length > 0 do\n b << a.shift\n c += 1\n if b.inject(0){|s, i| s += i} > a.inject(0){|s, i| s += i}\n break\n end\nend\n\nputs c"}, {"source_code": "def sum(numbers)\n total = 0\n\n numbers.each do |i|\n total += i\n end\n\n total\nend\n\ndef quick_sort(numbers, lower_bound, upper_bound)\n if lower_bound < upper_bound\n p = partition(numbers, lower_bound, upper_bound)\n quick_sort(numbers, lower_bound, p - 1)\n quick_sort(numbers, p + 1, upper_bound)\n end\n\nend\n\ndef partition(numbers, lower_bound, upper_bound)\n pivot = numbers[upper_bound]\n i, j = lower_bound, lower_bound\n\n while j <= upper_bound\n if numbers[j] < pivot\n numbers[i], numbers[j] = numbers[j], numbers[i]\n i += 1\n end\n\n j += 1\n end\n\n numbers[i], numbers[upper_bound] = numbers[upper_bound], numbers[i]\n i\nend\n\ndef main\n number_of_coins = gets.to_i\n coins = gets.split.map(&:to_i)\n\n total_value = sum coins\n\n quick_sort(coins, 0, number_of_coins - 1)\n\n coins_needed = 0\n sum = 0\n\n until sum > total_value / 2\n sum += coins[number_of_coins - coins_needed - 1]\n coins_needed += 1\n end\n\n puts coins_needed\nend\n\nmain if __FILE__ == $PROGRAM_NAME\n"}, {"source_code": "n,*a=gets(p).split.map &:to_i;z=eval a*?+;p n-a.sort.index{|i|0>=z-=i*2}"}, {"source_code": "n = STDIN.readline.to_i\ncoins = STDIN.readline.split.map {|s| s.to_i }\ntotal = coins.inject(0, :+)\nsum = 0\ncount = 0\ncoins.sort.reverse.each do |coin|\n sum += coin\n count += 1\n break if 2*sum > total\nend\nputs count\n"}, {"source_code": "n = gets.chomp.to_i\n\narray = gets.chomp.split(\" \").map(&:to_i).sort.reverse\narr_sum = array.inject(&:+)\nres = array[0]\nans = 0\n(1..n).each{ |elem|\n if( res > arr_sum / 2.0)\n ans = elem\n break\n else\n res += array[elem]\n end\n}\nputs ans"}, {"source_code": "n,*a=gets(p).split.map &:to_i;z=eval a*?+;p n-a.sort.index{|i|0>=z-=i*2}"}, {"source_code": "gets\na = gets.strip.split.map(&:to_i)\nsum = a.sort!.reverse!.inject(0) {|acc, m| acc + m }\nf = 0\ni = 0\nwhile f <= sum-f\n f += a[i]\n i += 1\nend\nputs i"}, {"source_code": "n = gets.to_i\na = gets.split(\" \").map(&:to_i).sort{ |x,y| y <=> x }\n\nfor i in 0..n\n l = a[0..i].inject(:+)\n r = a[(i+1)..(a.size)].inject(:+) || 0\n if (l) > (r)\n puts i+1\n break\n end\nend"}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i).sort{|x,y|y<=>x}\nn.times do |i|\n if i+1a.slice(i+1,a.size-i-1).reduce(:+)\n \tp i+1\n \tbreak\n elsif i+1==a.size\n \tp i+1\n end\nend"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i).sort.reverse\n\ncounter = a.reduce(:+)\nxounter = 0\nc = 0\nfor i in 0..n-1\n counter-=a[i]\n xounter+=a[i]\n c+=1\n break if xounter>counter\nend\n\nputs c"}, {"source_code": "n = $stdin.readline\na = $stdin.readline.split.map(&:to_i)\n\na.sort!.reverse!\n$s = a.inject(:+)\n#puts \"s=#{$s}\"\n\n# returns [s1, t]\ndef find(a,n)\n if n == 1\n return [a.first, 1]\n else\n s1, t = find(a,n-1)\n #puts \"s1=#{s1}, t=#{t}\"\n s2 = $s - s1\n an = a[n-1]\n if s2>=s1\n s1 += an\n t += 1\n end\n return [s1, t]\n end\nend\n\n#p a\ns1, t = find(a, a.count)\n#p s1\np t"}, {"source_code": "n =gets.chomp.to_i \na= gets.chomp \narr = a.split(/\\s/).map(&:to_i)\narr.sort!.reverse! \nsum1 =0 \ncount =0 \ncheck =0 \narr.each{|x| check+=x } \n \ncheck =check/2 \narr.each do |i| \n\tsum1+=i \n\tcount+=1\n\tif sum1>check\n\t\tbreak \n\tend\nend\nputs count "}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split(/ /).map!{|x| x.to_i}\n\na = a.sort\ns = 0\nfor i in 0..n-1\n s = s + a[i]\nend\nss = 0\nnr = 0\n\ni = n-1\nbegin\n if ss <= s\n ss = ss + a[i]\n s = s - a[i]\n nr = nr + 1\n else\n break\n end\n i = i - 1\nend while i >= 0\n\nputs nr"}, {"source_code": "gets\ninp = gets.chomp.split.map { |x| x.to_i }.sort\ncount, total = 0, 0\nwhile total <= inp.inject(0, :+)\n total += inp.pop\n count += 1\nend\nputs count\n"}, {"source_code": "n,*a=gets(p).split.map &:to_i;z=eval a*?+;p n-a.sort.index{|i|0>=z-=i*2}\n"}, {"source_code": "a=gets.chomp.to_i\nb=gets.chomp.split(\" \")\nsum=0\n0.upto(a-1) do |i|\n b[i]= b[i].to_i\nsum=sum+b[i]\nend\nr=0\nif sum%2==0\n r=1\nend \n \nsum=(sum/2.0).ceil\n\nb.sort!\nb.reverse!\np=0\n0.upto(a-1) do |i|\np=p+b[i].to_i\n\nif ( p>=sum && r==0)\np=i+1\nbreak\nend\nif (p>sum && r==1)\np=i+1\nbreak\nend\nend\n\nputs \"#{p}\""}, {"source_code": "require 'pp'\n\ngets\ncoins = gets.chomp.split(' ').map(&:to_i).sort\n\ni = coins.length - 1\nwhile i >= 0\n head = coins[0..i-1]\n tail = coins[i..-1]\n head_sum = head.inject(&:+)\n tail_sum = tail.inject(&:+)\n\n debug = {\n head: head,\n tail: tail,\n head_sum: head_sum,\n tail_sum: tail_sum,\n }\n\n if tail_sum > head_sum\n break\n end\n # pp debug\n i -= 1\nend\nputs coins.length - [i, 0].max\n#pp coins\n\n"}, {"source_code": "n= gets.to_i\na = gets.split.map(&:to_i)\ntot, sum, i= 0,0,0\na.each {|x| tot += x}\na.sort! {|x,y| y<=>x}\nuntil sum>tot-sum\n sum += a[i]\n i += 1\nend\nputs i"}, {"source_code": "def sum( coins )\n sum = 0\n coins.each do |coin|\n sum += coin\n end\n sum\nend\n\ngets.chomp\ncoins = gets.chomp.split(/ /).map(&:to_i)\ncoins.sort!\nmy_coins = []\n(coins.length - 1).downto(0).each do |coin|\n break if ( sum( my_coins ) > sum( coins ) )\n my_coins << coins.delete_at( coin )\nend\nputs my_coins.length\n"}, {"source_code": "a=gets.chomp.to_i\nb=gets.chomp.split(\" \")\nsum=0\n0.upto(a-1) do |i|\n b[i]= b[i].to_i\nsum=sum+b[i]\nend\nr=0\nif sum%2==0\n r=1\nend \n \nsum=(sum/2.0).ceil\n\nb.sort!\nb.reverse!\np=0\n0.upto(a-1) do |i|\np=p+b[i].to_i\n\nif ( p>=sum && r==0)\np=i+1\nbreak\nend\nif (p>sum && r==1)\np=i+1\nbreak\nend\nend\n\nputs \"#{p}\""}, {"source_code": "n,*a=gets(p).split.map &:to_i;z=eval a*?+;p n-a.sort.index{|i|0>=z-=i*2}\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i).push(0).sort\nb = [0]\nc = 0\nloop do\n break if b.inject(:+) > a.inject(:+)\n b << a.pop\n c += 1\nend\n\np c\n"}, {"source_code": "n,*a=gets(p).split.map &:to_i;z=eval a*?+;p n-a.sort.index{|i|0>=z-=i*2}"}, {"source_code": "class C160A\n n = gets.to_i\n a = []\n gets.split.map{|c| a << c.to_i}\n a.sort!\n a.reverse!\n s = 0\n a.map{|c| s += c}\n t = 0\n q = 0\n a.map{|c| t += c; q += 1; break if t * 2 > s}\n puts q\nend"}, {"source_code": "a=[]\nn=gets.to_i\nb=gets.split\nn.times{|i|\n\ta[i]=b[i].to_i}\na.sort!\n\nb=0\nans=0\nhalfsum=a.inject(:+)/2\n\nn.times{|i|\n\tbreak if b>halfsum\n\tb+=a[-i-1]\n\tans+=1}\np ans\n"}, {"source_code": "#!/usr/bin/ruby\n# coding: utf-8\n\nn = $stdin.gets.to_i\nxs = $stdin.gets.split.map(&:to_i).sort.reverse\ns = xs.inject 0, :+\nacc = 0\nxs.each_with_index do |x, i|\n acc += x\n if acc*2 > s\n puts i+1\n break\n end\nend\n"}, {"source_code": "n = gets.to_i\na = gets.chomp.split.map(&:to_i).sort.reverse\nsum = a.inject(:+)\ncount = 0\nmoney = 0\n\na.each { |i| count += 1; money += i; break if money > (sum/2.0) }\n\nputs count\n"}, {"source_code": "n = gets.to_i\na = gets.split().map &:to_i\na=a.sort.reverse\nsum = a.inject 0 do |x,y| x + y end\ns=0\ncnt = 0\n\nwhile s + s <= sum\n s += a[cnt]\n cnt = cnt + 1\nend\n\nputs cnt"}, {"source_code": "n=gets.to_i;m=gets.split.map(&:to_i).sort.reverse;m.each_with_index{|v,i| (m.inject(0,:+)>=0)?(m[i]=-m[i]):(break)};puts m.count{|v|v<0}\n"}, {"source_code": "n = gets.strip.to_i\na = gets.strip.split.map(&:to_i)[0..(n-1)].sort! {|x, y| y <=> x}\nsum = a.reduce(:+)\nsegment = 0\ncount = 0\na.each do |item|\n\tsegment += item\n\tcount += 1\n\tif segment * 2 > sum\n\t\tbreak\n\tend\nend\nputs count"}, {"source_code": "n = Integer(gets)\ninput = gets.chomp\nnums = input.split\nvalue = Array.new\nfor i in 0...n\n value[i] = Integer(nums[i])\nend\n\nvalue = value.sort{|x,y| y <=> x}\nsum = 0\nfor i in 0...n\n sum = sum + value[i]\nend\n\nv = Array.new\nx = 0\nfor i in 0..n\n if x > sum - x then \n break\n \n else\n v.push(value[i])\n x = x + value[i]\n end\nend\n\nputs v.length"}, {"source_code": "coins = gets.chomp.to_i\nvalues = gets.chomp\nvalues = values.split\n\ntotal = 0\nfor i in (0..coins-1)\n\tfor x in (i..coins-1)\n\t\tif i != x\n\t\t\tif values[i].to_i < values[x].to_i\n\t\t\t\ttemp = values[i].to_i\n\t\t\t\tvalues[i] = values[x].to_i\n\t\t\t\tvalues[x] = temp\n\t\t\tend\n\t\tend\n\tend\n\ttotal += values[i].to_i\nend\n\ntest_values = 0\ncount = 0\nfor i in (0..coins-1)\n\tif test_values <= total - test_values\n\t\ttest_values += values[i].to_i\n\t\tcount += 1\n\telse\n\t\tbreak\n\tend\nend\n\nprint count"}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\na.sort!\na.reverse!\n#puts a[0]\nsum = 0\na.each do\n |t|\n sum += t\nend\n res = 1\n sum2=0\na.each do\n |t|\n sum2+=t\n break if sum2 > (sum-sum2)\n res += 1\nend\nputs res"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split(\" \").map(&:to_i).sort_by{ |x| -x }\n\ns = a.reduce(&:+)\nsum = 0\n\na.each_with_index do |x, i|\n sum += x\n if sum > s / 2\n puts i + 1\n break\n end\nend"}, {"source_code": "\n\n\n\ndef sum(a)\n a.inject(0){|sum,x| sum + x}\nend\n\n\nn = gets.to_i\narr = gets.split(\" \").map(&:to_i)\narr.sort!{|x, y| y <=> x}\ntotal = sum(arr)\ni = 0\nwhile i < n\n arr_sum = sum arr[0,i+1]\n i += 1\n if arr_sum > total - arr_sum \n puts i\n break \n end\n \nend"}, {"source_code": "\n\n\n\ndef sum(a)\n a.inject(0){|sum,x| sum + x}\nend\n\n\nn = gets.to_i\narr = gets.split(\" \").map(&:to_i)\narr.sort!{|x, y| y <=> x}\ntotal = sum(arr)\ni = 0\narr_sum = 0\nwhile i < n\n arr_sum += arr[i]\n i += 1\n if arr_sum > total - arr_sum \n puts i\n break \n end\n \nend"}, {"source_code": "gets\ncs = gets.split.map(&:to_i).sort.reverse\nsum = cs.inject(:+)\nputs cs.index{ |x| 0 > sum -= 2*x } + 1\n"}, {"source_code": "n,*a=gets(p).split.map &:to_i;z=eval a*?+;p n-a.sort.index{|i|0>=z-=i*2}"}, {"source_code": "n,*a=gets(p).split.map &:to_i;z=eval a*?+;p n-a.sort.index{|i|0>=z-=i*2}\n"}, {"source_code": "gets\nl = gets.split.map(&:to_i).sort { |x, y| y <=> x }\ns = l.reduce(:+)\nt = 0\nc = 0\n\nl.each { |x| \n\t\t\tc += 1 \n\t\t\tt += x\n\t\t\tif t > s - t \n\t\t\t\tbreak\n\t\t\tend\n\t\t}\n\nputs c"}, {"source_code": "coin_number = gets.chomp.to_i\ncoin_seq_number = gets.chomp.split\ncoin_seq_number = coin_seq_number.map{|item| item.to_i}\ncoin_seq_sum = coin_seq_number.inject {|sum, i| sum + i}\ncoin_seq_number = coin_seq_number.sort{|a,b| b <=> a}\ni = 0\ncoin_res_sum = 0\nbegin\n coin_res_sum = coin_seq_number[i] + coin_res_sum\n i = i + 1\nend while coin_res_sum < coin_seq_sum / 2\nif coin_res_sum == coin_seq_sum/2 then\n p i+1\nelse\n p i\nend\n"}, {"source_code": "n = STDIN.gets.chomp.to_i\ncoins = []\n\ncoins = STDIN.gets.split.map { |n| n.to_i }\n\ncoins.sort! { |a, b| b <=> a }\n\ntotal = coins.reduce(:+)\n\nsum = 0\nnumC = 0\nwhile sum <= total/2\n sum += coins[numC]\n numC += 1\nend\n\nputs numC"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i).sort.reverse\nsum = a.reduce(:+)\n\n(0...n).each do |i|\n former = a[0..i].reduce(:+)\n if former > sum - former\n puts i + 1\n break\n end\nend\n"}, {"source_code": "n = gets.to_i\na = gets.chomp.split(\" \")\n\nfor x in 0...a.size\n\ta[x] = a[x].to_i\nend\n\na.sort! {|y,x| x<=>y}\n\nsum1 = 0\nsum2 = 0\nm = 0\n\nfor x in 0...a.size\n\tsum1 += a[x] \n\tfor y in x+1...a.size\n\t\tsum2 += a[y]\n\tend\n\tif sum1>sum2\n\t\tm = x+1\n\t\tbreak\n\tend\n\tsum2 = 0\nend\n\nputs m\n\t"}, {"source_code": "n, coins = gets.to_i, gets.split.map(&:to_i).sort.reverse\nsum = 0\ntotal = coins.inject(:+)\n(n + 1).times do |i|\n\tif sum > total / 2\n\t\tputs i\n\t\tbreak\n\tend\n\tsum += coins[i]\nend\n"}, {"source_code": "t = gets.to_i\na = gets.split(\" \").map(&:to_i)\na = a.sort.reverse\nsum=-1\nfor i in 0..t\n if a[0,i].inject(0, :+)>a[i,t].inject(0, :+)\n sum=i\n break\n end\nend\nprint(sum)\n"}, {"source_code": "n = gets.chomp.to_i\ncoins = gets.split.collect{|i| i.to_i}\ncoins = coins.sort{|x,y| y <=> x}\n\ntarget = coins.inject(:+) / 2\nsum = 0\ncoins.each_index{|i|\n sum += coins[i]\n if sum > target\n puts i + 1\n break\n end\n}"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\na = a.sort.reverse\ntotal = a.inject(:+)\ns = 0\na.each_with_index do |x, i|\n s += x\n if 2 * s > total\n puts i + 1\n break\n end\nend\n"}, {"source_code": "gets;a=gets.split.map(&:to_i).sort.reverse;s=a.inject:+;p a.index{|x|0>s-=2*x}+1\n"}, {"source_code": "a=gets.chomp.to_i\nb=gets.chomp.split(\" \")\nsum=0\n0.upto(a-1) do |i|\n b[i]= b[i].to_i\nsum=sum+b[i]\nend\nr=0\nif sum%2==0\n r=1\nend \n \nsum=(sum/2.0).ceil\n\nb.sort!\nb.reverse!\np=0\n0.upto(a-1) do |i|\np=p+b[i].to_i\n\nif ( p>=sum && r==0)\np=i+1\nbreak\nend\nif (p>sum && r==1)\np=i+1\nbreak\nend\nend\n\nputs \"#{p}\""}, {"source_code": "n,*a=gets(p).split.map &:to_i;z=eval a*?+;p n-a.sort.index{|i|0>=z-=i*2}"}, {"source_code": "n=gets.to_i\na=[]\n(n-1).times {\na.push gets(' ').to_i\n}\na.push gets.to_i\n\nprog=(a.inject{|sum,x| sum + x })/2\n\nsuma=0\nlicznik=0\n\nfor i in a.sort.reverse\nsuma+=i\nlicznik+=1\nif suma>prog\np licznik\nbreak\nend\nend"}, {"source_code": "gets\na=gets.split.map(&:to_i)\na.sort!\na.reverse!\nsum=a.inject{|res,x|res+x}\nk=0\nfor i in 0..a.length-1\n if 2*k<=sum\n k+=a[i]\n end\n if 2*k>sum\n break\n end\nend\nputs i+1\n "}, {"source_code": "n,*a=gets(p).split.map &:to_i;z=eval a*?+;p n-a.sort.index{|i|0>=z-=i*2}\n"}, {"source_code": "n = gets\na = gets.split(' ').map(&:to_i).sort\ns = []\n\nwhile s.inject(0, &:+) <= a.inject(0, &:+)\n s << a.pop\nend\n\nputs s.size\n"}, {"source_code": "n,*a=gets(p).split.map &:to_i;z=eval a*?+;p n-a.sort.index{|i|0>=z-=i*2}\n"}, {"source_code": "n = gets.to_i\na = gets.split\n\nsum = 0\nfor i in 0 ... a.size\n\ta[i] = a[i].to_i\n\tsum += a[i]\nend\n\na.sort!.reverse!\n\nsum2 = 0\nfor i in 0 ... a.size\n\tsum2 += a[i]\n\tif sum2 > sum / 2 then\n\t\tputs((i + 1).to_s)\n\t\tbreak\n\tend\nend"}, {"source_code": "_ = gets.chomp\na = gets.chomp.split(/ /).map(&:to_i).sort { |a,b| b <=> a}\nmy_count = 0\nmy = 0\nall = a.inject(0) { |sum, x| sum + x }\na.each do |a_i|\n my += a_i\n all -= a_i\n my_count += 1\n break if my > all\nend\nputs my_count\n"}, {"source_code": "n,*a=gets(p).split.map &:to_i;z=eval a*?+;p n-a.sort.index{|i|0>=z-=i*2}\n"}, {"source_code": "input = STDIN.read.split(\"\\n\")[1].split(\" \").map(&:to_i).sort!\n\ncount = 0\nsum = 0\n\nwhile !input.empty? and sum <= input.inject {|sum, v| sum += v} do\n\tsum += input.pop\n\tcount += 1\nend\n\nSTDOUT.puts count"}, {"source_code": "coinNum = gets.chomp.to_i\ncoins = gets.chomp.split(' ').map(&:to_i).sort.reverse\nans = 0\ncoins << 0\n\nfor i in (0...coinNum)\n\tans += 1\n\tbreak if coins[0..i].reduce(:+) > coins[(i + 1)...coins.length].reduce(:+)\nend\n\nputs ans"}], "negative_code": [{"source_code": "n = gets\ncoins = gets.chomp.split(' ').map(&:to_i).sort\nsum = coins.reduce(:+)\ncounter = 0\ntaken_sum = 0\ncoins.each do |coin|\n taken_sum += coin\n counter += 1\n sum -= coin\n break if taken_sum > sum\nend\n\nputs counter"}, {"source_code": "n = gets.to_i\nst = gets.split\ni = 0\nsum = 0\nwhile i1 && s1+st[i]<=min) do\n i -= 1\n end\n if i<=1 then s1 = s1 + st[j]\n else \n s1 = s1 + st[i]\n end\n coins += 1\nend\nputs \"#{coins}\""}, {"source_code": "sum=0\nn=gets.to_i\na=gets.split.map(&:to_i)\na.sort!\na.reverse!\na.each{|i| sum+=i }\nans=0\ntmp=0\na.each{|i|\n ans+=1\n sum-=i\n tmp+=i\n if tmp>i\n p ans\n exit\n end\n}\n"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split(\" \").map{|e| e.to_i}.sort!\nb = []\nc = 0\nwhile a.length > 0 do\n b << a.shift\n c += 1\n if b.inject(0){|s, i| s += i} > a.inject(0){|s, i| s += i}\n break\n end\nend\n\nputs c"}, {"source_code": "n = gets.to_i\na = gets.split(\" \").map(&:to_i).sort{ |x,y| y <=> x }\n\nfor i in 0..n\n l = a[0..i].inject(:+)\n r = a[i..(a.size)].inject(:+) || 0\n if (l) > (r)\n puts i+1\n break\n end\nend"}, {"source_code": "n = $stdin.readline\na = $stdin.readline.split.map(&:to_i)\n\na.sort!.reverse!\n$s = a.inject(:+)\n#puts \"s=#{$s}\"\n\n# returns [s1, t]\ndef find(a,n)\n if n == 1\n return [a.first, 1]\n else\n s1, t = find(a,n-1)\n #puts \"s1=#{s1}, t=#{t}\"\n s2 = $s - s1\n an = a[n-1]\n if s2>s1\n s1 += an\n t += 1\n end\n return [s1, t]\n end\nend\n\n#p a\ns1, t = find(a, a.count)\n#p s1\np t"}, {"source_code": "require 'pp'\n\ngets\ncoins = gets.chomp.split(' ').map(&:to_i).sort.reverse\n\ni = coins.length - 1\nwhile i >= 0\n head = coins[0..i-1]\n tail = coins[i..-1]\n head_sum = head.inject(&:+)\n tail_sum = tail.inject(&:+)\n\n debug = {\n head: head,\n tail: tail,\n head_sum: head_sum,\n tail_sum: tail_sum,\n }\n\n if tail_sum > head_sum\n break\n end\n # pp debug\n i -= 1\nend\nputs coins.length - [i, 0].max\n#pp coins\n\n"}, {"source_code": "n = gets.to_i\na = gets.chomp.split(//).sort.reverse.map(&:to_i)\nsum = a.inject(:+)\ncount = 0\nmoney = 0\n\na.each { |i| count += 1; money += i; break if money > (sum/2.0).ceil }\n\nputs count"}, {"source_code": "n = gets.to_i\na = gets.split().map &:to_i\n\nsum = a.inject 0 do |x,y| x + y end\ns=0\ncnt = 0\n\nwhile s + s <= sum\n s += a[cnt]\n cnt = cnt + 1\nend\n\nputs cnt"}, {"source_code": "n = Integer(gets)\ninput = gets.chomp\nnums = input.split\nvalue = Array.new\nfor i in 0...n\n\tvalue[i] = Integer(nums[i])\nend\n\nvalue = value.sort{|x,y| y <=> x}\nsum = 0\nfor i in 0...n\n\tsum = sum + value[i]\nend\n\nv = Array.new\nx = 0\nfor i in 0...n\n\tif x > sum - x\n\t\tputs v.length\n\telse\n\t\tv.push(value[i])\n\t\tx = x + value[i]\n\tend\nend\n"}, {"source_code": "n = Integer(gets)\ninput = gets.chomp\nnums = input.split\nvalue = Array.new\nfor i in 0...n\n value[i] = Integer(nums[i])\nend\n\nvalue = value.sort{|x,y| y <=> x}\nsum = 0\nfor i in 0...n\n sum = sum + value[i]\nend\n\nv = Array.new\nx = 0\nfor i in 0..n\n if x > sum - x\n puts v.length\n else\n v.push(value[i])\n x = x + value[i]\n end\nend\n"}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\na.sort!\nsum = 0\na.each do\n |t|\n sum += t\nend\n res = 1\n sum2=0\na.each do\n |t|\n sum2+=t\n break if sum2 > (sum-sum2)\n res += 1\nend\nputs res"}, {"source_code": "\n\n\n\ndef sum(a)\n a.inject(0){|sum,x| sum + x}\nend\n\n\nn = gets.to_i\narr = gets.split(\" \").map(&:to_i)\narr.sort!{|x, y| y <=> x}\ntotal = sum(arr)\ni = 0\nwhile i < n\n arr_sum = sum arr[0,i+1]\n i += 1\n if arr_sum >= total - arr_sum \n puts i\n break \n end\n \nend"}, {"source_code": "coin_number = gets.chomp.to_i\ncoin_seq_number = gets.chomp.split\n\ncoin_seq_number = coin_seq_number.map{|item| item.to_i}\ncoin_seq_sum = coin_seq_number.inject {|sum, i| sum + i}\n\ni = 0\ncoin_res_sum = 0\nbegin\n coin_res_sum = coin_seq_number[i] + coin_res_sum\n i = i + 1\nend while coin_res_sum < coin_seq_sum / 2\nif coin_number < i+1 then\n p coin_number\nelse\n p i+1\nend\n"}, {"source_code": "coin_number = gets.chomp.to_i\ncoin_seq_number = gets.chomp.split\n\ncoin_seq_number = coin_seq_number.map{|item| item.to_i}\ncoin_seq_sum = coin_seq_number.inject {|sum, i| sum + i}\n\ni = 0\ncoin_res_sum = 0\nbegin\n coin_res_sum = coin_seq_number[i] + coin_res_sum\n i = i + 1\nend while coin_res_sum < coin_seq_sum / 2\np i+1"}, {"source_code": "t = gets.to_i\na = gets.split(\" \").map(&:to_i)\na = a.sort\nsum=-1\nfor i in 0..t\n if a[0,i].inject(0, :+)>a[i,t].inject(0, :+)\n sum=i\n break\n end\nend\nprint(sum)\n"}, {"source_code": "a=gets.chomp.to_i\nb=gets.chomp.split(\" \").sort.reverse\nsum=0\n0.upto(a-1) do |i|\nsum=sum+b[i].to_i\nend\nsum=(sum/2)+1\np=0\n0.upto(a-1) do |i|\np=p+b[i].to_i\nif p>sum\np=i+1\nbreak\nend\nend\n\nputs \"#{p}\"\n\n"}, {"source_code": "a=gets.chomp.to_i\nb=gets.chomp.split(\" \")\nsum=0\n0.upto(a-1) do |i|\n b[i]= b[i].to_i\nsum=sum+b[i]\nend\nr=0\nif sum%2==0\n r=1\nend \n \nsum=(sum/2.0).ceil\nb.sort!\nb.reverse!\np=0\n0.upto(a-1) do |i|\np=p+b[i].to_i\n\nif p>=sum && r==0\np=i+1\nbreak\nend\nif p>sum && r==1\np=i+1\nbreak\nend\nend\n"}, {"source_code": "a=gets.chomp.to_i\nb=gets.chomp.split(\" \")\nsum=0\n0.upto(a-1) do |i|\n b[i]= b[i].to_i\nsum=sum+b[i]\nend\nsum=(sum/2.0).ceil\nb.sort!\nb.reverse!\np=0\n0.upto(a-1) do |i|\np=p+b[i].to_i\nif p>sum\np=i+1\nbreak\nend\nend\n\nputs \"#{p}\""}, {"source_code": "n=gets.to_i\na=[]\n(n-1).times {\na.push gets(' ').to_i\n}\na.push gets.to_i\n\nprog=(a.inject{|sum,x| sum + x })/2\n\nsuma=0\nlicznik=0\n\nfor i in a.sort\nsuma+=i\nlicznik+=1\nif suma>prog\np licznik\nbreak\nend\nend"}, {"source_code": "_ = gets.chomp\na = gets.chomp.split(/ /).map(&:to_i).sort { |a,b| b <=> a}\nmy_count = 0\nmy = 0\ntwins = 0\na.each do |a_i|\n if twins + a_i >= my\n my += a_i\n my_count += 1\n else\n twins += a_i\n end\nend\nputs my_count\n"}, {"source_code": "_ = gets.chomp\na = gets.chomp.split(/ /).map(&:to_i)\nmy_count = 0\nmy = 0\ntwins = 0\na.each do |a_i|\n if twins + a_i >= my\n my += a_i\n my_count += 1\n else\n twins += a_i\n end\nend\nputs my_count\n"}, {"source_code": "input = STDIN.read.split(\"\\n\")[1].split(\" \").map(&:to_i).sort!\ncount = 0\n\nwhile !input.empty? and count < input.inject {|sum, v| sum += v} do\n\tinput.pop\n\tcount += 1\nend\n\nSTDOUT.puts count"}], "src_uid": "ee535e202b7662dbaa91e869c8c6cee1"} {"nl": {"description": "Pashmak has fallen in love with an attractive girl called Parmida since one year ago...Today, Pashmak set up a meeting with his partner in a romantic garden. Unfortunately, Pashmak has forgotten where the garden is. But he remembers that the garden looks like a square with sides parallel to the coordinate axes. He also remembers that there is exactly one tree on each vertex of the square. Now, Pashmak knows the position of only two of the trees. Help him to find the position of two remaining ones.", "input_spec": "The first line contains four space-separated x1, y1, x2, y2 ( - 100 ≤ x1, y1, x2, y2 ≤ 100) integers, where x1 and y1 are coordinates of the first tree and x2 and y2 are coordinates of the second tree. It's guaranteed that the given points are distinct.", "output_spec": "If there is no solution to the problem, print -1. Otherwise print four space-separated integers x3, y3, x4, y4 that correspond to the coordinates of the two other trees. If there are several solutions you can output any of them. Note that x3, y3, x4, y4 must be in the range ( - 1000 ≤ x3, y3, x4, y4 ≤ 1000).", "sample_inputs": ["0 0 0 1", "0 0 1 1", "0 0 1 2"], "sample_outputs": ["1 0 1 1", "0 1 1 0", "-1"], "notes": null}, "positive_code": [{"source_code": "x1, y1, x2, y2 = gets.split.map &:to_i\n\ndef evaluate_vertical_side x1, x2, y1, y2\n delta_y = (y2 - y1).abs\n if x1 + delta_y <= 1000\n x3 = x1 + delta_y\n return [x3, y1, x3, y2]\n elsif x1 - delta_y >= -1000\n x3 = x1 - delta_y\n return [x3, y1, x3, y2]\n else\n return -1\n end \nend\n\nif x1 == x2 #vertical side\n new_points = evaluate_vertical_side x1, x2, y1, y2\n puts new_points == -1? \"-1\" : \"#{new_points[0]} #{new_points[1]} #{new_points[2]} #{new_points[3]}\"\nelsif y1 == y2 #horizontal side\n x1, y1 = y1, x1\n x2, y2 = y2, x2\n new_points = evaluate_vertical_side x1, x2, y1, y2\n puts new_points == -1? \"-1\" : \"#{new_points[1]} #{new_points[0]} #{new_points[3]} #{new_points[2]}\"\nelsif (y2 - y1).abs == (x2 - x1).abs #diagonal\n x3 = x1 \n y3 = y2\n x4 = x2\n y4 = y1\n puts \"#{x3} #{y3} #{x4} #{y4}\"\nelse #not squar\n puts \"-1\"\nend\n\n\n"}, {"source_code": "x1,y1,x2,y2=gets.chomp.split.map(&:to_i)\nif x1==x2\n\tputs (x1+y2-y1).to_s+' '+y1.to_s+' '+(x2+y2-y1).to_s+' '+y2.to_s\nelsif y1==y2\n\tputs x1.to_s+' '+(y1+x1-x2).to_s+' '+x2.to_s+' '+(y2+x1-x2).to_s\nelsif x1-x2==y1-y2 || x1-x2==y2-y1\n\tputs x1.to_s+' '+y2.to_s+' '+x2.to_s+' '+y1.to_s\nelse\n\tputs \"-1\"\nend"}, {"source_code": "x, y, a, b = gets.split.map(&:to_i)\nx, y, a, b = [[x, y], [a, b]].min[0], [[x, y], [a, b]].min[1], [[x, y], [a, b]].max[0], [[x, y], [a, b]].max[1]\nif x == a\n puts [x + (y - b).abs, y, a + (y - b).abs, b].map(&:to_s).join(\" \")\nelsif y == b\n puts [x, y + (x - a).abs, a, b + (x - a).abs].map(&:to_s).join(\" \")\nelse\n if a > x && b > y\n if a - x == b - y\n puts [x, b, a, y].map(&:to_s).join(\" \")\n else \n puts \"-1\"\n end\n else\n if a - x == y - b\n puts [x, b, a, y].map(&:to_s).join(\" \")\n else\n puts \"-1\"\n end\n end\nend"}, {"source_code": "x1,y1,x2,y2 = gets.split.map(&:to_i)\ndx = (x2-x1)\ndy = (y2-y1)\n\nif dx != 0 and dy != 0 and dx.abs != dy.abs\n puts -1\nelsif dx == 0\n puts \"#{x1+dy} #{y1} #{x1+dy} #{y1+dy}\"\nelsif dy == 0\n puts \"#{x1} #{y1+dx} #{x1+dx} #{y1+dx}\"\nelse\n puts \"#{x1} #{y1+dy} #{x1+dx} #{y1}\"\nend"}, {"source_code": "x1, y1, x2, y2 = gets.split.map(&:to_i)\nd1 = x1 - x2\nd2 = y1 - y2\nt = false\nif (d1*d2!=0 && (d1!=-d2 && d1!=d2))\n puts -1\n t = true;\nend\nif (d1==0 || d2 == 0)\n puts(\"#{x1-d2} #{y1-d1} #{x2-d2} #{y2-d1}\")\n t = true\nend\nif !t\n puts(\"#{x1} #{y2} #{x2} #{y1}\")\nend"}, {"source_code": "x1, y1, x2, y2 = gets.chomp.split(/ /).map(&:to_i)\nx3, y3, x4, y4 = [0, 0, 0, 0]\ninvalid = false\nif x1 == x2\n diff = (y2 - y1).abs\n x3, x4 = x1 + diff, x2 + diff\n y3, y4 = y1, y2\nelsif y1 == y2\n diff = (x2 - x1).abs\n x3, x4 = x1, x2\n y3, y4 = y1 + diff, y2 + diff\nelsif (x2 - x1).abs == (y2 - y1).abs\n x3, y3, x4, y4 = x1, y2, x2, y1\nelse\n invalid = true\nend\nif !invalid\n puts \"#{x3} #{y3} #{x4} #{y4}\"\nelse\n puts \"-1\"\nend\n"}, {"source_code": "x1,y1,x2,y2=gets.split.map{|e| e.to_i}\n\ndx=x2-x1\ndy=y2-y1\ndx2=dx.abs\ndy2=dy.abs\nif dx2==0 && dy2==0 then\n\tputs -1\nelsif dx2==dy2 then\n\tputs [x1,y2,x2,y1]*\" \"\nelsif dx2==0 then\n\tputs [x1+dy,y1,x2+dy,y2]*\" \"\nelsif dy2==0 then\n\tputs [x1,y1+dx,x2,y2+dx]*\" \"\nelse\n\tputs -1\nend\n\n"}, {"source_code": "x1,y1,x2,y2 = gets.split.map(&:to_i)\nif x1 == x2\n\ta = y1 - y2\n\tif -1000 <= x1 + a and x1 + a <= 1000\n\t\tprint x1 + a,' ',y1,' ',x2 + a,' ',y2\n\telsif -1000 <= x1 - a and x1 - a <= 1000\n\t\tprint x1 - a,' ',y1,' ',x2 - a,' ',y2\n\tend\nelsif y1 == y2\n\ta = x1 - x2\n\tif -1000 <= y1 + a and y1 + a <= 1000\n\t\tprint x1,' ',y1 + a,' ',x2,' ',y2 + a\n\telsif -1000 <= y1 - a and y1 - a <= 1000\n\t\tprint x1,' ',y1 - a,' ',x2,' ',y2 - a\n\tend\nelsif [x1-x2,x2-x1].max == [y1-y2,y2-y1].max\n\tif x1 < x2 and y1 < y2\n\t\tprint [x1,x2].min,' ',[y1,y2].max,' ',[x1,x2].max,' ',[y1,y2].min\n\telsif x2 < x1 and y2 < y1\n\t\tprint [x1,x2].min,' ',[y1,y2].max,' ',[x1,x2].max,' ',[y1,y2].min\n\telsif \n\t\tprint [x1,x2].min,' ',[y1,y2].min,' ',[x1,x2].max,' ',[y1,y2].max\n\tend\nelse\n\tp -1\nend\n"}, {"source_code": "\n\nclass Point\n def initialize(x_c,y_c)\n @x_coord=x_c\n @y_coord=y_c\n end\n def x\n return @x_coord\n end\n def y\n return @y_coord\n end\nend\n\na,b,c,d = gets.split(\" \").map {|x| x.to_i}\n\np1=Point.new(a,b)\np2=Point.new(c,d)\n\nif p1.x==p2.x\n d = (p1.y-p2.y).abs\n print \"#{p1.x+d} #{p1.y} #{p2.x+d} #{p2.y}\"\n exit\nend\n\nif p1.y==p2.y\n d = (p1.x-p2.x).abs\n print \"#{p1.x} #{p1.y+d} #{p2.x} #{p2.y+d}\"\n exit\nend\n\nif (p1.x-p2.x).abs!=(p1.y-p2.y).abs\n print \"-1\"\n exit\nend\n\nprint \"#{p1.x} #{p2.y} #{p2.x} #{p1.y}\"\n"}, {"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\n\nx1, y1, x2, y2 = gets.split.map(&:to_i)\n# -100..100\nif x1 == x2\n # vertical edge\n len = (y1 - y2).abs\n puts \"#{x1 + len} #{y1} #{x2 + len} #{y2}\"\nelsif y1 == y2\n # horizontal edge\n len = (x1 - x2).abs\n puts \"#{x1} #{y1 + len} #{x2} #{y2 + len}\"\nelsif (x1-x2).abs == (y1-y2).abs\n # diagonal edge (must be square)\n if x1 < x2\n puts \"#{x1} #{y2} #{x2} #{y1}\"\n else\n puts \"#{x2} #{y1} #{x1} #{y2}\"\n end\nelse\n # impossible\n puts -1\nend"}, {"source_code": "xxyy = gets.chomp\nxxyy = xxyy.split\nx1 = xxyy[0].to_i\ny1 = xxyy[1].to_i\nx2 = xxyy[2].to_i\ny2 = xxyy[3].to_i\n\nif x1 == x2 && y1 != y2\n\tprint x1 + [y1, y2].max - [y1,y2].min\n\tprint \" \"\n\tprint [y1, y2].max\n\tprint \" \"\n\tprint x1 + [y1, y2].max - [y1,y2].min\n\tprint \" \"\n\tprint [y1,y2].min\nelsif y1 == y2 && x1 != x2\n\tprint [x1,x2].min\n\tprint \" \"\n\tprint y1 - ([x1,x2].max - [x1,x2].min)\n\tprint \" \"\n\tprint [x1,x2].max\n\tprint \" \"\n\tprint y1 - ([x1,x2].max - [x1,x2].min)\nelse\n\tif (x1 - x2).abs == (y1 - y2).abs\n\t\tprint x1\n\t\tprint \" \"\n\t\tprint y2\n\t\tprint \" \"\n\t\tprint x2\n\t\tprint \" \"\n\t\tprint y1\n\telse\n\t\tprint -1\n\tend\nend\n\t"}, {"source_code": "x1,y1,x2,y2 = gets.split(\" \").map(&:to_i)\n\nx_r = (x2 - x1)\ny_r = (y2 - y1)\nx_a = x_r.abs\ny_a = y_r.abs\nif (x1 != x2 && y1 != y2 && x_a != y_a)\n puts \"-1\"\nelsif (x1 == x2)\n puts \"#{x1 + (y1 - y2).abs} #{y1} #{x2 + (y1 - y2).abs} #{y2}\"\nelsif (y1 == y2)\n puts \"#{x1} #{y1 + (x1 - x2).abs} #{x2} #{y2 + (x1 - x2).abs}\"\nelse\n puts \"#{x1} #{y2} #{x2} #{y1}\"\nend"}, {"source_code": "x1,y1,x2,y2 = gets.split.map(&:to_i)\ndx = (x2-x1)\ndy = (y2-y1)\n\nif dx != 0 and dy != 0 and dx.abs != dy.abs\n puts -1\nelsif dx == 0\n puts \"#{x1+dy} #{y1} #{x1+dy} #{y1+dy}\"\nelsif dy == 0\n puts \"#{x1} #{y1+dx} #{x1+dx} #{y1+dx}\"\nelse\n puts \"#{x1} #{y1+dy} #{x1+dx} #{y1}\"\nend\n"}, {"source_code": "x1,y1,x2,y2=gets.split.map(&:to_i)\n\nif x1==x2\n x3=x1+(y1-y2).abs\n x4=x3\n y3=y1\n y4=y2\nelsif y1==y2\n y3=y1+(x1-x2).abs\n y4=y3\n x3=x1\n x4=x2\nelsif (x1-x2).abs == (y1-y2).abs\n x3=x2\n y3=y1\n x4=x1\n y4=y2\nelse\n puts -1\n exit\nend\n\nputs \"#{x3} #{y3} #{x4} #{y4}\"\n\n\n"}, {"source_code": "x1, y1, x2, y2 = gets.split(' ').map(&:to_i)\n\nif x1 != x2 && y1 != y2\n if (x1-x2).abs != (y1-y2).abs\n puts -1\n else\n puts \"#{x1} #{y2} #{x2} #{y1}\"\n end\nelsif x1 != x2\n delta = (x1 - x2).abs\n puts \"#{x1} #{y1+delta} #{x2} #{y2+delta}\"\nelsif y1 != y2\n delta = (y1 - y2).abs\n puts \"#{x1+delta} #{y1} #{x2+delta} #{y2}\"\nelse\n puts -1\nend\n"}, {"source_code": "line = gets\nx1, y1, x2, y2 = line.split.map(&:to_i)\nif y1 == y2\n\tl = (x1 - x2).abs\n\ty3 = y1 + l\n\tx3 = x1\n\ty4 = y2 + l\n\tx4 = x2 \n\tputs [x3, y3, x4, y4].join \" \"\nelsif x1 == x2\n\tl = (y1 - y2).abs\n\tx3 = x1 + l\n\ty3 = y1\n\tx4 = x2 + l\n\ty4 = y2\n\tputs [x3, y3, x4, y4].join \" \"\nelsif (x1 - x2).abs == (y1 - y2).abs\n\tl = (x1 - x2).abs\n\tx3 = x1\n\ty3 = if y1 > y2 \n\t\ty1 - l\n\telse\n\t\ty1 + l\n\tend\n\tx4 = if x1 > x2 \n\t\tx1 - l\n\telse\n\t\tx1 + l\n\tend\n\n\ty4 = y1\n\tputs [x3, y3, x4, y4].join \" \"\nelse\n\tputs \"-1\"\nend\t\t\n\t\t\n\n"}, {"source_code": "\n\nx, y, a, b = gets.split.map(&:to_i)\n\nx, y, a, b = [[x, y], [a, b]].min[0], [[x, y], [a, b]].min[1],\n [[x, y], [a, b]].max[0], [[x, y], [a, b]].max[1]\n\nif x == a\n puts [x + (y-b).abs, y, a + (y-b).abs, b].map(&:to_s).join(\" \")\nelsif y == b\n puts [x, y + (x-a).abs, a, b + (x-a).abs].map(&:to_s).join(\" \")\nelse\n if a > x && b > y\n if a - x == b - y\n puts [x, b, a, y].map(&:to_s).join(\" \")\n else\n puts \"-1\"\n end\n else\n if a - x == y - b\n puts [x, b, a, y].map(&:to_s).join(\" \")\n else\n puts \"-1\"\n end\n end\nend\n\n\n"}, {"source_code": "x1, y1, x2, y2 = gets.split.map(&:to_i)\nif x1 == x2\n puts [x1 + (y2 - y1), y1, x2 + (y2 - y1), y2].join(\" \")\nelsif y1 == y2\n puts [x1, y1 + (x2 - x1), x2, y2 + (x2 - x1)].join(\" \")\nelsif (x2 - x1) == (y2 - y1) or (x2 - x1) == - (y2 - y1)\n puts [x1, y2, x2, y1].join(\" \")\nelse\n puts \"-1\"\nend\n"}], "negative_code": [{"source_code": "x1, y1, x2, y2 = gets.split.map &:to_i\n\ndef evaluate_vertical_side x1, x2, y1, y2\n delta_y = (y2 - y1).abs\n if x1 + delta_y <= 1000\n x3 = x1 + delta_y\n return [x3, y1, x3, y2]\n elsif x1 - delta_y >= -1000\n x3 = x1 - delta_y\n return [x3, y1, x3, y2]\n else\n return -1\n end \nend\n\nif x1 == x2 #vertical side\n new_points = evaluate_vertical_side x1, x2, y1, y2\n puts new_points == -1? \"-1\" : \"#{new_points[0]} #{new_points[1]} #{new_points[2]} #{new_points[3]}\"\nelsif y1 == y2 #horizontal side\n x1, y1 = y1, x1\n x2, y2 = y2, x2\n new_points = evaluate_vertical_side x1, x2, y1, y2\n puts new_points == -1? \"-1\" : \"#{new_points[0]} #{new_points[1]} #{new_points[2]} #{new_points[3]}\"\nelsif (y2 - y1).abs == (x2 - x1).abs #diagonal\n x3 = x1 \n y3 = y2\n x4 = x2\n y4 = y1\n puts \"#{x3} #{y3} #{x4} #{y4}\"\nelse #not squar\n puts \"-1\"\nend\n\n\n"}, {"source_code": "x1, y1, x2, y2 = gets.split.map &:to_i\n\ndef evaluate_vertical_side x1, x2, y1, y2\n if x1 + (y2 - y1) <= 1000\n x3 = x1 + (y2 - y1)\n return [x3, y1, x3, y2]\n elsif x1 - (y2 - y1) >= -1000\n x3 = x1 - (y2 - y1)\n return [x3, y1, x3, y2]\n else\n return -1\n end \nend\n\nif x1 == x2 #vertical side\n new_points = evaluate_vertical_side x1, x2, y1, y2\n puts new_points == -1? \"-1\" : \"#{new_points[0]} #{new_points[1]} #{new_points[2]} #{new_points[3]}\"\nelsif y1 == y2 #horizontal side\n x1, y1 = y1, x1\n x2, y2 = y2, x2\n new_points = evaluate_vertical_side x1, x2, y1, y2\n puts new_points == -1? \"-1\" : \"#{new_points[0]} #{new_points[1]} #{new_points[2]} #{new_points[3]}\"\nelsif (y2 - y1).abs == (x2 - x1).abs #diagonal\n x3 = x1 \n y3 = y2\n x4 = x2\n y4 = y1\n\n puts \"#{x3} #{y3} #{x4} #{y4}\"\nelse #not squar\n puts \"-1\"\nend\n\n\n"}, {"source_code": "x1, y1, x2, y2 = gets.split.map(&:to_i)\nd1 = x1 - x2\nd2 = y1 - y2\nt = false\nif (d1*d2!=0 && (d1!=-d2 && d1!=d2))\n puts -1\n t = true;\nend\nif (d1==0 || d2 == 0)\n puts(\"#{x1-d2} #{y1-d1} #{x1-d2} #{y2-d1}\")\n t = true\nend\nif !t\n puts(\"#{x1} #{y2} #{x2} #{y1}\")\nend"}, {"source_code": "x1, y1, x2, y2 = gets.chomp.split(/ /).map(&:to_i)\nx3, y3, x4, y4 = [0, 0, 0, 0]\ninvalid = false\nif x1 == x2\n diff = (y2 - y1).abs\n x3, x4 = x1 + diff, x2 + diff\n y3, y4 = y1, y2\nelsif y1 == y2\n diff = (y2 - y1).abs\n x3, x4 = x1, x2\n y3, y4 = y1 + diff, y2 + diff\nelsif (x2 - x1).abs == (y2 - y1).abs\n x3, y3, x4, y4 = x1, y2, x2, y1\nelse\n invalid = true\nend\nif !invalid\n puts \"#{x3} #{y3} #{x4} #{y4}\"\nelse\n puts \"-1\"\nend\n"}, {"source_code": "x1,y1,x2,y2=gets.split.map{|e| e.to_i}\n\ndx=x2-x1\ndy=y2-y1\nif dx==0 && dy==0 then\n\tputs -1\nelsif dx==dy then\n\tputs [x1,y1+dx,x2,y2-dy]*\" \"\nelsif dx==0 then\n\tputs [x1+dy,y1,x2+dy,y2]*\" \"\nelsif dy==0 then\n\tputs [x1,y1+dx,x2,y2+dx]*\" \"\nelse\n\tputs -1\nend\n\n"}, {"source_code": "x1,y1,x2,y2 = gets.split.map(&:to_i)\nif x1 == x2\n\ta = y1 - y2\n\tif -1000 <= x1 + a and x1 + a <= 1000\n\t\tprint x1 + a,' ',y1,' ',x2 + a,' ',y2\n\telsif -1000 <= x1 - a and x1 - a <= 1000\n\t\tprint x1 - a,' ',y1,' ',x2 - a,' ',y2\n\tend\nelsif y1 == y2\n\ta = y1 - y2\n\tif -1000 <= y1 + a and y1 + a <= 1000\n\t\tprint x1,' ',y1 + a,' ',x2,' ',y2 + a\n\telsif -1000 <= y1 - a and y1 - a <= 1000\n\t\tprint x1,' ',y1 - a,' ',x2,' ',y2 - a\n\tend\nelsif [x1-x2,x2-x1].max == [y1-y2,y2-y1].max\n\tif x1 < x2 and y1 < y2\n\t\tprint [x1,x2].min,' ',[y1,y2].max,' ',[x1,x2].max,' ',[y1,y2].min\n\telsif x2 < x1 and y2 < y1\n\t\tprint [x1,x2].min,' ',[y1,y2].max,' ',[x1,x2].max,' ',[y1,y2].min\n\telsif \n\t\tprint [x1,x2].min,' ',[y1,y2].min,' ',[x1,x2].max,' ',[y1,y2].max\n\tend\nelse\n\tp -1\nend\n"}, {"source_code": "x1,y1,x2,y2 = gets.split.map(&:to_i)\ndx = (x2-x1)\ndy = (y2-y1)\n\nif dx != 0 and dy != 0 and dx.abs != dy.abs\n puts -1\nelsif dx == 0\n puts \"#{x1+dx} #{y1} #{x1+dx} #{y1+dy}\"\nelsif dy == 0\n puts \"#{x1} #{y1+dy} #{x1+dx} #{y1+dy}\"\nelse\n puts \"#{x1} #{y1+dy} #{x1+dx} #{y1}\"\nend\n"}, {"source_code": "x1,y1,x2,y2 = gets.split.map(&:to_i)\ndx = (x2-x1)\ndy = (y2-y1)\nif dx != 0 and dy != 0 and dx != dy\n puts -1\nelsif dx == 0\n puts \"#{x1+dy} #{y1} #{x1+dy} #{y1+dy}\"\nelsif dy == 0\n puts \"#{x1} #{y1+dx} #{x1+dx} #{y1+dx}\"\nelse\n puts \"#{x1} #{y1+dx} #{x1+dx} #{y1}\"\nend\n"}, {"source_code": "x1,y1,x2,y2=gets.split.map(&:to_i)\n\nif x1==x2\n x3=x1+1\n x4=x3\n y3=y1\n y4=y2\nelsif y1==y2\n y3=y1+1\n y4=y3\n x3=x1\n x4=x2\nelse\n x3=x2\n y3=y1\n x4=x1\n y4=y2\nend\n\nputs \"#{x3} #{y3} #{x4} #{y4}\"\n\n\n"}, {"source_code": "line = gets\nx1, y1, x2, y2 = line.split.map(&:to_i)\nif y1 == y2\n\tl = (x1 - x2).abs\n\ty3 = y1 + l\n\tx3 = x1\n\ty4 = y2 + l\n\tx4 = x2 \n\tputs [x3, y3, x4, y4].join \" \"\nelsif x1 == x2\n\tl = (y1 - y2).abs\n\tx3 = x1 + l\n\ty3 = y1\n\tx4 = x2 + l\n\ty4 = y2\n\tputs [x3, y3, x4, y4].join \" \"\nelsif (x1 - x2).abs == (y1 - y2).abs\n\tl = (x1 - x2).abs\n\tx3 = x1\n\ty3 = y1 + l\n\tx4 = x1 + l\n\ty4 = y1\n\tputs [x3, y3, x4, y4].join \" \"\nelse\n\tputs \"-1\"\nend\t\t\n\t\t\n\n"}, {"source_code": "x1, y1, x2, y2 = gets.split.map(&:to_i)\nif x1 == x2\n puts [x1 + (y2 - y1), y1, x1 + (y2 - y1), y2].join(\" \")\nelsif y1 == y2\n puts [x1, y1 + (x2 - x1), x1, y2 + (x2 - x1)].join(\" \")\nelsif (x2 - x1) == (y2 - y1) or (x2 - x1) == - (y2 - y1)\n puts [x1, y2, x2, y1].join(\" \")\nelse\n puts \"-1\"\nend\n"}], "src_uid": "71dea31e1244797f916adf5f526f776e"} {"nl": {"description": "A bracket sequence is a string containing only characters \"(\" and \")\".A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters \"1\" and \"+\" between the original characters of the sequence. For example, bracket sequences \"()()\", \"(())\" are regular (the resulting expressions are: \"(1)+(1)\", \"((1+1)+1)\"), and \")(\" and \"(\" are not.You are given $$$n$$$ bracket sequences $$$s_1, s_2, \\dots , s_n$$$. Calculate the number of pairs $$$i, j \\, (1 \\le i, j \\le n)$$$ such that the bracket sequence $$$s_i + s_j$$$ is a regular bracket sequence. Operation $$$+$$$ means concatenation i.e. \"()(\" + \")()\" = \"()()()\".If $$$s_i + s_j$$$ and $$$s_j + s_i$$$ are regular bracket sequences and $$$i \\ne j$$$, then both pairs $$$(i, j)$$$ and $$$(j, i)$$$ must be counted in the answer. Also, if $$$s_i + s_i$$$ is a regular bracket sequence, the pair $$$(i, i)$$$ must be counted in the answer.", "input_spec": "The first line contains one integer $$$n \\, (1 \\le n \\le 3 \\cdot 10^5)$$$ — the number of bracket sequences. The following $$$n$$$ lines contain bracket sequences — non-empty strings consisting only of characters \"(\" and \")\". The sum of lengths of all bracket sequences does not exceed $$$3 \\cdot 10^5$$$.", "output_spec": "In the single line print a single integer — the number of pairs $$$i, j \\, (1 \\le i, j \\le n)$$$ such that the bracket sequence $$$s_i + s_j$$$ is a regular bracket sequence.", "sample_inputs": ["3\n)\n()\n(", "2\n()\n()"], "sample_outputs": ["2", "4"], "notes": "NoteIn the first example, suitable pairs are $$$(3, 1)$$$ and $$$(2, 2)$$$.In the second example, any pair is suitable, namely $$$(1, 1), (1, 2), (2, 1), (2, 2)$$$."}, "positive_code": [{"source_code": "n = gets.to_i\nseqs = []\nn.times do |i|\n seqs << gets.strip\nend\n\nhsh = {}\n\nseqs.each do |seq|\n b = 0\n balanced = true\n seq.each_char do |c|\n if c == '('\n b += 1\n end\n if c == ')'\n b -= 1\n end\n balanced = false if b < 0\n end\n hsh[b] = (hsh[b] || 0) + 1 if balanced\n\n\n b = 0\n balanced = true\n seq.reverse.each_char do |c|\n if c == '('\n b += 1\n end\n if c == ')'\n b -= 1\n end\n balanced = false if b > 0\n end\n hsh[b] = (hsh[b] || 0) + 1 if balanced && b<0\nend\n\n# puts hsh\nans = 0\nans += (hsh[0] * hsh[0]) if hsh[0]\npos_ub = hsh.keys.select{|x| x>0}\npos_ub.each do |pub|\n nub = -1 * pub\n ans += (hsh[pub] * (hsh[nub] || 0))\nend\nputs ans"}, {"source_code": "# http://codeforces.com/problemset/problem/990/C\n\ndef count_seq\n n = gets.to_i\n h = {}\n regular = 0\n 0.upto(n - 1) do |i|\n bs = gets.chomp\n ret = parse(bs)\n next if ret.nil?\n\n if ret == ''\n regular += 1\n elsif\n h[ret] ||= 0\n h[ret] += 1\n end\n end\n\n c = regular * regular\n # puts h.inspect\n\n h.each do |key, count|\n next if key[0] == ')'\n\n other_key = [')', key[1]]\n other_c = h[other_key]\n # puts \"other_c #{other_c}\"\n if other_c\n c += other_c * count\n end\n end\n\n c\nend\n\ndef parse(bs)\n a = []\n bs.each_char do |c|\n if a.empty? || a.last == c\n a << c\n next\n end\n\n if a.last == '('\n a.slice!(a.size - 1)\n else\n a << c\n end\n end\n\n return '' if a.empty?\n return nil if a.first != a.last\n\n return [a.first, a.size]\nend\n\nputs count_seq\n"}, {"source_code": "def left_stack(a)\n stack = []\n a.each_char do |c|\n if c == '('\n stack.push(c)\n else\n if stack.pop != '('\n return nil\n end\n end\n end\n stack\nend\n\ndef right_stack(a)\n stack = []\n a.reverse.each_char do |c|\n if c == ')'\n stack.push(c)\n else\n if stack.pop != ')'\n return nil\n end\n end\n end\n return stack\nend\n\ndef left_sign_stack(a)\n stack = left_stack(a)\n stack && stack.length\nend\n\ndef right_sign_stack(a)\n stack = right_stack(a)\n stack && stack.length\nend\n\nleft_count = Hash.new(0)\nright_count = Hash.new(0)\n\nn = gets.to_i\nn.times do\n s = gets.chomp\n left_sign = left_sign_stack(s)\n right_sign = right_sign_stack(s)\n left_count[left_sign] += 1 if left_sign\n right_count[right_sign] += 1 if right_sign\nend\n\nres = 0\nleft_count.each do |sign, cnt1|\n res += cnt1 * right_count[sign]\nend\n\nputs res\n"}], "negative_code": [{"source_code": "n = gets.to_i\nseqs = []\nn.times do |i|\n seqs << gets.strip\nend\n\nhsh = {}\n\nseqs.each do |seq|\n b = 0\n balanced = true\n seq.each_char do |c|\n if c == '('\n b += 1\n end\n if c == ')'\n b -= 1\n end\n balanced = false if b < 0\n end\n balanced = false if b > 0\n hsh[0] = (hsh[0] || 0) + 1 if balanced\n if b < 0\n hsh[b] = (hsh[b] || 0) + 1\n end\n if b > 0\n hsh[b] = (hsh[b] || 0) + 1\n end\nend\n\nans = 0\nans += (hsh[0] * hsh[0]) if hsh[0]\npos_ub = hsh.keys.select{|x| x>0}\npos_ub.each do |pub|\n nub = -1 * pub\n ans += (hsh[pub] * (hsh[nub] || 0))\nend\nputs ans"}, {"source_code": "# http://codeforces.com/problemset/problem/990/C\n\ndef count_seq\n n = gets.to_i\n h = {}\n regular = 0\n 0.upto(n - 1) do |i|\n bs = gets.chomp\n ret = parse(bs)\n next if ret.nil?\n\n if ret == ''\n regular += 1\n elsif\n h[ret] ||= 0\n h[ret] += 1\n end\n end\n\n c = regular * regular\n # puts h.inspect\n\n h.each do |key, count|\n next if key[0] == ')'\n\n other_key = ['(', key[1]]\n other_c = h[other_key]\n if other_c\n c += other_c * count\n end\n end\n\n c\nend\n\ndef parse(bs)\n a = []\n bs.each_char do |c|\n if a.empty? || a.last == c\n a << c\n next\n end\n\n if a.last == '('\n a.slice!(a.size - 1)\n else\n a << c\n end\n end\n\n return '' if a.empty?\n return nil if a.first != a.last\n\n return [a.first, a.size]\nend\n\nputs count_seq\n"}, {"source_code": "# http://codeforces.com/problemset/problem/990/C\n\ndef count_seq\n n = gets.to_i\n h = {}\n regular = 0\n 0.upto(n - 1) do |i|\n bs = gets.chomp\n ret = parse(bs)\n if ret.nil?\n regular += 1\n else\n h[ret] ||= 0\n h[ret] += 1\n end\n end\n\n c = regular * regular\n # puts h.inspect\n\n h.each do |key, count|\n next if key[0] == 'r'\n\n other_key = ['r', key[1]]\n other_c = h[other_key]\n if other_c\n c += other_c * count\n end\n end\n\n c\nend\n\ndef parse(bs)\n l = 0\n r = 0\n bs.each_char do |c|\n (c == '(') ? (l += 1) : (r += 1)\n end\n\n return nil if l == r\n\n if l > r\n ['l', l - r]\n else\n ['r', r - l]\n end\nend\n\nputs count_seq"}], "src_uid": "f5f163198fbde6a5c15c50733cfd9176"} {"nl": {"description": "Valera considers a number beautiful, if it equals 2k or -2k for some integer k (k ≥ 0). Recently, the math teacher asked Valera to represent number n as the sum of beautiful numbers. As Valera is really greedy, he wants to complete the task using as few beautiful numbers as possible. Help Valera and find, how many numbers he is going to need. In other words, if you look at all decompositions of the number n into beautiful summands, you need to find the size of the decomposition which has the fewest summands.", "input_spec": "The first line contains string s (1 ≤ |s| ≤ 106), that is the binary representation of number n without leading zeroes (n > 0).", "output_spec": "Print a single integer — the minimum amount of beautiful numbers that give a total of n.", "sample_inputs": ["10", "111", "1101101"], "sample_outputs": ["1", "2", "4"], "notes": "NoteIn the first sample n = 2 is a beautiful number.In the second sample n = 7 and Valera can decompose it into sum 23 + ( - 20).In the third sample n = 109 can be decomposed into the sum of four summands as follows: 27 + ( - 24) + ( - 22) + 20."}, "positive_code": [{"source_code": "x, y = 0, 1\ns = readline.strip\ns.each_char do |c|\n\tm = [x, y].min + 1\n\tif c == '0'\n\t\ty = m\n\telse\n\t\tx = m\n\tend\nend\nputs x\n"}], "negative_code": [], "src_uid": "b2bc51df4a2c05c56c211e8f33fe1b45"} {"nl": {"description": "Allen dreams of one day owning a enormous fleet of electric cars, the car of the future! He knows that this will give him a big status boost. As Allen is planning out all of the different types of cars he will own and how he will arrange them, he realizes that he has a problem. Allen's future parking lot can be represented as a rectangle with $$$4$$$ rows and $$$n$$$ ($$$n \\le 50$$$) columns of rectangular spaces, each of which can contain at most one car at any time. He imagines having $$$k$$$ ($$$k \\le 2n$$$) cars in the grid, and all the cars are initially in the second and third rows. Each of the cars also has a different designated parking space in the first or fourth row. Allen has to put the cars into corresponding parking places. Illustration to the first example. However, since Allen would never entrust his cars to anyone else, only one car can be moved at a time. He can drive a car from a space in any of the four cardinal directions to a neighboring empty space. Furthermore, Allen can only move one of his cars into a space on the first or fourth rows if it is the car's designated parking space. Allen knows he will be a very busy man, and will only have time to move cars at most $$$20000$$$ times before he realizes that moving cars is not worth his time. Help Allen determine if he should bother parking his cars or leave it to someone less important.", "input_spec": "The first line of the input contains two space-separated integers $$$n$$$ and $$$k$$$ ($$$1 \\le n \\le 50$$$, $$$1 \\le k \\le 2n$$$), representing the number of columns and the number of cars, respectively. The next four lines will contain $$$n$$$ integers each between $$$0$$$ and $$$k$$$ inclusive, representing the initial state of the parking lot. The rows are numbered $$$1$$$ to $$$4$$$ from top to bottom and the columns are numbered $$$1$$$ to $$$n$$$ from left to right. In the first and last line, an integer $$$1 \\le x \\le k$$$ represents a parking spot assigned to car $$$x$$$ (you can only move this car to this place), while the integer $$$0$$$ represents a empty space (you can't move any car to this place). In the second and third line, an integer $$$1 \\le x \\le k$$$ represents initial position of car $$$x$$$, while the integer $$$0$$$ represents an empty space (you can move any car to this place). Each $$$x$$$ between $$$1$$$ and $$$k$$$ appears exactly once in the second and third line, and exactly once in the first and fourth line.", "output_spec": "If there is a sequence of moves that brings all of the cars to their parking spaces, with at most $$$20000$$$ car moves, then print $$$m$$$, the number of moves, on the first line. On the following $$$m$$$ lines, print the moves (one move per line) in the format $$$i$$$ $$$r$$$ $$$c$$$, which corresponds to Allen moving car $$$i$$$ to the neighboring space at row $$$r$$$ and column $$$c$$$. If it is not possible for Allen to move all the cars to the correct spaces with at most $$$20000$$$ car moves, print a single line with the integer $$$-1$$$.", "sample_inputs": ["4 5\n1 2 0 4\n1 2 0 4\n5 0 0 3\n0 5 0 3", "1 2\n1\n2\n1\n2", "1 2\n1\n1\n2\n2"], "sample_outputs": ["6\n1 1 1\n2 1 2\n4 1 4\n3 4 4\n5 3 2\n5 4 2", "-1", "2\n1 1 1\n2 4 1"], "notes": "NoteIn the first sample test case, all cars are in front of their spots except car $$$5$$$, which is in front of the parking spot adjacent. The example shows the shortest possible sequence of moves, but any sequence of length at most $$$20000$$$ will be accepted.In the second sample test case, there is only one column, and the cars are in the wrong order, so no cars can move and the task is impossible."}, "positive_code": [{"source_code": "N, K = gets.split.map(&:to_i)\ngoal = gets.split.map(&:to_i)\nroad = gets.split.map(&:to_i)\nroad.concat(gets.split.map(&:to_i).reverse)\ngoal.concat(gets.split.map(&:to_i).reverse)\n\ndef index2road(i)\n i %= 2*N\n i < N ? [2, i+1] : [3, 2*N-i]\nend\ndef index2goal(i)\n i %= 2*N\n i < N ? [1, i+1] : [4, 2*N-i]\nend\n\ngoaled = []\nans = []\n(2*N).times do |i|\n if goal[i] > 0 && goal[i] == road[i]\n num = goal[i]\n ans.push([num] + index2goal(i))\n goaled[num] = true\n road[i] = 0\n end\nend\n\nif road.count(0) == 0\n puts -1\n exit\nend\n\nwhile goaled.count{|v| v} < K\n index = road.index(0)\n (index+1).upto(index+2*N-1) do |i|\n i %= 2*N\n next if road[i] == 0\n ans.push([road[i]] + index2road(i-1))\n road[(i-1) % (2*N)] = road[i]\n road[i] = 0\n end\n (2*N).times do |i|\n if goal[i] > 0 && goal[i] == road[i]\n num = goal[i]\n ans.push([num] + index2goal(i))\n goaled[num] = true\n road[i] = 0\n end\n end\nend\n\nputs ans.length\nans.each do |arr|\n puts arr.join(' ')\nend"}, {"source_code": "n, k = gets.strip.split.map(&:to_i)\nr1 = gets.strip.split.map(&:to_i)\nr2 = gets.strip.split.map(&:to_i)\nr3 = gets.strip.split.map(&:to_i)\nr4 = gets.strip.split.map(&:to_i)\n\nif 2*n == k\n match_found = false\n 0.upto(n-1).each do |i|\n if r1[i] == r2[i]\n match_found = true\n break\n end\n end\n if !match_found\n 0.upto(n-1).each do |i|\n if r3[i] == r4[i]\n match_found = true\n break\n end\n end\n end\n if !match_found\n puts -1\n exit\n end\nend\n\nseq = []\n0.upto(n-1).each do |i|\n if r2[i]!=0 && r1[i] == r2[i]\n seq << [r2[i], 1, i+1]\n r2[i] = 0\n end\nend\n0.upto(n-1).each do |i|\n if r3[i]!=0 && r3[i] == r4[i]\n seq << [r3[i], 4, i+1]\n r3[i] = 0\n end\nend\n\nloop do\n if r2[n-1]!=0 && r2[n-1]==r1[n-1]\n seq << [r2[n-1], 1, n]\n r2[n-1] = 0\n end\n if r2[n-1]!=0 && r3[n-1]==0\n seq << [r2[n-1], 3, n]\n r3[n-1] = r2[n-1]\n r2[n-1] = 0\n end\n (n-2).downto(0).each do |i|\n if r2[i]!=0 && r2[i]==r1[i]\n seq << [r2[i], 1, i+1]\n r2[i] = 0\n end\n if r2[i]!=0 && r2[i+1]==0\n seq << [r2[i], 2, i+2]\n r2[i+1] = r2[i]\n r2[i] = 0\n end\n end\n if r3[0]!=0 && r3[0]==r4[0]\n seq << [r3[0], 4, 1]\n r3[0] = 0\n end\n if r3[0]!=0 && r2[0]==0\n seq << [r3[0], 2, 1]\n r2[0] = r3[0]\n r3[0] = 0\n end\n (1).upto(n-1).each do |i|\n if r3[i]!=0 && r3[i]==r4[i]\n seq << [r3[i], 4, i+1]\n r3[i] = 0\n end\n if r3[i]!=0 && r3[i-1]==0\n seq << [r3[i], 3, i]\n r3[i-1] = r3[i]\n r3[i] = 0\n end\n end\n break if r2.uniq == [0] && r3.uniq == [0]\nend\n\nputs seq.size\nseq.each do |s|\n puts s.join(\" \")\nend"}], "negative_code": [], "src_uid": "a8d1a78ae2093d2e0de8e4efcbf940c7"} {"nl": {"description": "You are given an array $$$a_1, a_2, \\dots , a_n$$$ consisting of integers from $$$0$$$ to $$$9$$$. A subarray $$$a_l, a_{l+1}, a_{l+2}, \\dots , a_{r-1}, a_r$$$ is good if the sum of elements of this subarray is equal to the length of this subarray ($$$\\sum\\limits_{i=l}^{r} a_i = r - l + 1$$$).For example, if $$$a = [1, 2, 0]$$$, then there are $$$3$$$ good subarrays: $$$a_{1 \\dots 1} = [1], a_{2 \\dots 3} = [2, 0]$$$ and $$$a_{1 \\dots 3} = [1, 2, 0]$$$.Calculate the number of good subarrays of the array $$$a$$$.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$) — the length of the array $$$a$$$. The second line of each test case contains a string consisting of $$$n$$$ decimal digits, where the $$$i$$$-th digit is equal to the value of $$$a_i$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.", "output_spec": "For each test case print one integer — the number of good subarrays of the array $$$a$$$.", "sample_inputs": ["3\n3\n120\n5\n11011\n6\n600005"], "sample_outputs": ["3\n6\n1"], "notes": "NoteThe first test case is considered in the statement.In the second test case, there are $$$6$$$ good subarrays: $$$a_{1 \\dots 1}$$$, $$$a_{2 \\dots 2}$$$, $$$a_{1 \\dots 2}$$$, $$$a_{4 \\dots 4}$$$, $$$a_{5 \\dots 5}$$$ and $$$a_{4 \\dots 5}$$$. In the third test case there is only one good subarray: $$$a_{2 \\dots 6}$$$."}, "positive_code": [{"source_code": "t = gets.to_i\nans = []\nt.times do\n n = gets.to_i\n a = gets.chomp.split(\"\").map(&:to_i)\n arr = Array.new(n+1,0)\n arr[0] = 0\n cnt = Hash.new(0)\n n.times do |i|\n arr[i+1] = arr[i] + a[i]\n cnt[arr[i]-i] += 1\n end\n cnt[arr[n]-n] += 1\n sum = 0\n cnt.each_value do |x|\n sum += x * (x-1) / 2\n end\n ans << sum\nend\nans.each do |x|\n puts x\nend"}], "negative_code": [], "src_uid": "e5244179b8ef807b1c6abfe113bd4f3b"} {"nl": {"description": "Now it's time of Olympiads. Vanya and Egor decided to make his own team to take part in a programming Olympiad. They've been best friends ever since primary school and hopefully, that can somehow help them in teamwork.For each team Olympiad, Vanya takes his play cards with numbers. He takes only the cards containing numbers 1 and 0. The boys are very superstitious. They think that they can do well at the Olympiad if they begin with laying all the cards in a row so that: there wouldn't be a pair of any side-adjacent cards with zeroes in a row; there wouldn't be a group of three consecutive cards containing numbers one. Today Vanya brought n cards with zeroes and m cards with numbers one. The number of cards was so much that the friends do not know how to put all those cards in the described way. Help them find the required arrangement of the cards or else tell the guys that it is impossible to arrange cards in such a way.", "input_spec": "The first line contains two integers: n (1 ≤ n ≤ 106) — the number of cards containing number 0; m (1 ≤ m ≤ 106) — the number of cards containing number 1.", "output_spec": "In a single line print the required sequence of zeroes and ones without any spaces. If such sequence is impossible to obtain, print -1.", "sample_inputs": ["1 2", "4 8", "4 10", "1 5"], "sample_outputs": ["101", "110110110101", "11011011011011", "-1"], "notes": null}, "positive_code": [{"source_code": "n, m = gets.split.map(&:to_i)\nlen = n + m\nif n > m + 1\n p -1\n exit\nend\nif n == 0\n if m <= 2\n p \"1\" * m\n else\n p -1\n end\n exit\nend\ns = \"01\" * (n - 1) + \"0\"\nm -= (n - 1)\n\nk = [m, 2].min\nm -= k\ns = \"1\" * k + s\nk = [m, 2].min\nm -= k\ns = s + \"1\" * k\nif m > n - 1\n p -1\n exit\nend\ns = s[0, 2] + \"011\" * m + s[2 + 2 * m, s.length - (2 + 2 * m)] if m != 0\nputs s"}, {"source_code": "m,n = gets.chomp!.split.map { |e| e.to_i}\n\nif n+1 < m \n p -1\n exit\nend\n\nif (m+1)*2 < n\n p -1\n exit\nend\n\nif n == m\n n.times{printf \"10\" }\n puts \"\"\n\nelsif m == n+1\n n.times{print \"01\"}\n puts 0\nelsif (m+1)*2 == n\n m.times{printf \"110\"}\n puts \"11\"\nelsif m*2 == n\n m.times{print \"110\"}\n puts \"\"\nelse\n k = n - 1 - m\n (k).times {print \"110\"}\n (m-k).times{print \"10\"}\n puts \"1\"\nend\n"}, {"source_code": "z,o = gets.split(\" \").map(&:to_i)\nif z >= o\t\n\tif o < z-1\n\t\tans = \"-1\"\n\telse\n\t\tans = \"01\" * (z-1)\n\t\tans << \"0\"\n\t\tans << \"1\" if z == o\n\tend\nelse\n\tspaces = z + 1\n\tif o > (spaces * 2)\n\t\tans = \"-1\"\n\telse\n\t\tpairs = o - spaces \n\t\trem_ones = o - pairs * 2\n\t\tarr = [\"11\"] * pairs + [\"1\"] * rem_ones\n\t\tans = arr.join(\"0\")\n\tend\nend\n\nputs ans"}, {"source_code": "n, m = gets.split.collect{|i| i.to_i}\nif m < n-1 or m > (n+1)*2\n puts -1\nelsif m == n-1\n puts (['0']*n).join('1')\nelsif m == n\n puts (['0']*n).join('1')+'1'\nelse\n cnt = m-n-1\n puts (['11']*cnt+['1']*(n+1-cnt)).join('0')\nend\n"}, {"source_code": "#f = File.open('test1.txt', 'r')\nf = STDIN\n\ndef gets f\n f.gets.chomp.split.map &:to_i\nend\n\nn, m = gets(f)\nres =\n if m < n - 1\n -1\n elsif m <= (n + 1) * 2 #1010101010101010101\n remainder = m - (n - 1)\n s = '0'\n (n - 1).times do\n s << '1'\n if remainder > 0\n s << '1'\n remainder -= 1\n end\n s << '0'\n end\n if remainder > 0\n s.insert 0, ('1' * [remainder, 2].min)\n remainder -= [remainder, 2].min\n end\n if remainder > 0\n s.concat ('1' * remainder)\n end\n s\n else\n -1\n end\nputs res"}], "negative_code": [{"source_code": "n, m = gets.split.collect{|i| i.to_i}\nif m < n+1 or m > (n+1)*2\n puts -1\n exit\nend\ncnt = m-n-1\nputs (['11']*cnt+['1']*(n+1-cnt)).join('0')\n"}], "src_uid": "854c561539596722b1dbc8f99cbdca78"} {"nl": {"description": "ZS the Coder and Chris the Baboon are travelling to Udayland! To get there, they have to get on the special IOI bus. The IOI bus has n rows of seats. There are 4 seats in each row, and the seats are separated into pairs by a walkway. When ZS and Chris came, some places in the bus was already occupied.ZS and Chris are good friends. They insist to get a pair of neighbouring empty seats. Two seats are considered neighbouring if they are in the same row and in the same pair. Given the configuration of the bus, can you help ZS and Chris determine where they should sit?", "input_spec": "The first line of the input contains a single integer n (1 ≤ n ≤ 1000) — the number of rows of seats in the bus. Then, n lines follow. Each line contains exactly 5 characters, the first two of them denote the first pair of seats in the row, the third character denotes the walkway (it always equals '|') and the last two of them denote the second pair of seats in the row. Each character, except the walkway, equals to 'O' or to 'X'. 'O' denotes an empty seat, 'X' denotes an occupied seat. See the sample cases for more details. ", "output_spec": "If it is possible for Chris and ZS to sit at neighbouring empty seats, print \"YES\" (without quotes) in the first line. In the next n lines print the bus configuration, where the characters in the pair of seats for Chris and ZS is changed with characters '+'. Thus the configuration should differ from the input one by exactly two charaters (they should be equal to 'O' in the input and to '+' in the output). If there is no pair of seats for Chris and ZS, print \"NO\" (without quotes) in a single line. If there are multiple solutions, you may print any of them.", "sample_inputs": ["6\nOO|OX\nXO|XX\nOX|OO\nXX|OX\nOO|OO\nOO|XX", "4\nXO|OX\nXO|XX\nOX|OX\nXX|OX", "5\nXX|XX\nXX|XX\nXO|OX\nXO|OO\nOX|XO"], "sample_outputs": ["YES\n++|OX\nXO|XX\nOX|OO\nXX|OX\nOO|OO\nOO|XX", "NO", "YES\nXX|XX\nXX|XX\nXO|OX\nXO|++\nOX|XO"], "notes": "NoteNote that the following is an incorrect configuration for the first sample case because the seats must be in the same pair.O+|+XXO|XXOX|OOXX|OXOO|OOOO|XX"}, "positive_code": [{"source_code": "n = gets.to_i\n\nf = false\nout = []\nn.times do\n s = gets.strip\n \n if !f && s[\"OO\"]\n f = true\n s[\"OO\"]=\"++\"\n end\n\n out << s\nend\nif f\n puts \"YES\",out\nelse\n puts \"NO\"\nend\n"}, {"source_code": "n = gets.to_i\n\nf = false\nout = []\nn.times do\n l, r = gets.strip.split(\"|\")\n \n if f == false && l == \"OO\"\n l = \"++\"\n f = true\n end\n \n if f == false && r == \"OO\"\n r = \"++\"\n f = true\n end\n\n out << l + \"|\" + r\nend\nif f\n puts \"YES\",out\nelse\n puts \"NO\"\nend\n"}, {"source_code": "n = gets.to_i\n\nf = false\nout = []\nn.times do\n s = gets.strip\n \n if !f && s.include?(\"OO\")\n f = true\n s = s.sub(\"OO\",'++')\n end\n\n out << s\nend\nif f\n puts \"YES\",out\nelse\n puts \"NO\"\nend\n"}, {"source_code": "n = gets.to_i\n\nf = false\nout = []\nn.times do\n s = gets.strip\n \n if !f && s.include?(\"OO\")\n f = true\n s[\"OO\"]=\"++\"\n end\n\n out << s\nend\nif f\n puts \"YES\",out\nelse\n puts \"NO\"\nend\n"}, {"source_code": "#!/usr/bin/ruby\ngets;a=$<.map(&:chomp)\nif a.any?{|e|\n\tif e[0,2]=='OO'\n\t\te[0,2]='++'\n\telsif e[3,2]=='OO'\n\t\te[3,2]='++'\n\telse\n\t\tfalse\n\tend\n}\n\tputs :YES\n\tputs a\nelse\n\tputs :NO\nend"}, {"source_code": "n = gets.chomp.to_i\n\ns = \"\".tap do |out|\n n.times { out << gets }\nend\n\nreplaced = s.sub(/OO/, '++')\nif s == replaced\n puts \"NO\"\nelse\n puts \"YES\"\n puts replaced\nend\n"}, {"source_code": "n = gets.to_i\nans = 'NO'\nres = []\nn.times do\n s1, s2 = gets.chomp.split(\"|\")\n if s1 == \"OO\"\n s1 = \"++\" unless ans == 'YES'\n ans = 'YES'\n elsif s2 == \"OO\"\n s2 = \"++\" unless ans == 'YES'\n ans = 'YES'\n end\n res << [s1, s2].join(\"|\")\nend\n\nif ans == 'YES'\n puts ans\n puts res\nelse\n puts ans\nend\n"}, {"source_code": "n = gets.strip.to_i\nfirst = Array.new(n)\nsecond = Array.new(n)\nn.times do |i|\n first[i], second[i] = gets.strip.split(\"|\")\nend\nfound = false\nn.times do |i|\n if first[i] == \"OO\"\n first[i] = \"++\"\n found = true\n break\n end\n if second[i] == \"OO\"\n second[i] = \"++\"\n found = true\n break\n end\nend\nif found\n puts \"YES\"\n n.times do |i|\n print first[i]\n print \"|\"\n print second[i]\n print \"\\n\"\n end\nelse\n puts \"NO\"\nend\n"}, {"source_code": "n=gets.to_i\ns=STDIN.read\nif s.scan(\"OO\").size>0 then\n\ts.sub!(\"OO\",\"++\")\n\tputs \"YES\",s\nelse\n\tputs \"NO\"\nend\n\n"}, {"source_code": "t = gets.chomp.to_i\nchange = true\narr = []\nt.times.each do\n arr << gets.chomp\nend\narr.each do |ts|\n if ts.include?('OO') and change\n ts.sub!('OO','++') \n change = false\n end\nend\n\nif change\n puts \"NO\"\nelse\n puts \"YES\"\n arr.each do |rr|\n puts rr\n end\nend"}, {"source_code": "n = gets.to_i\ns = []\nn.times do |i|\n\ts[i] = gets.chomp\nend\nx = false\nfor i in 0...n\n\tif x == false\n\t\tif s[i][0...2] == 'OO'\n\t\t\ts[i][0...2] = '++'\n\t\t\tx = true\n\t\telsif s[i][3...5] == 'OO'\n\t\t\ts[i][3...5] = '++'\n\t\t\tx = true\n\t\tend\n\n\tend\nend\nif x == false\n\tputs 'NO'\nelse\n\tputs 'YES'\n\t0.upto(n-1) do |i|\n\t\tputs s[i]\n\tend\nend\n"}, {"source_code": "s = ''\n(1..gets.chomp.to_i).each do\n s << gets.chomp << '#'\nend\nbegin\n s[/OO/] = '++'\n puts 'YES'\n s.split('#').each { |q| puts q }\nrescue\n puts 'NO'\nend"}, {"source_code": "def answer(a, b, lines)\n puts \"YES\"\n for i in 0..(lines.length-1) do\n if (i == a)\n if (b == 0)\n print \"++|\" + lines[i][1] + \"\\n\"\n else\n print lines[i][0] + \"|++\\n\"\n end\n else\n print lines[i][0] + \"|\" + lines[i][1] + \"\\n\"\n end\n end\nend\n\nn = gets.strip.to_i\nlines = []\nn.times do\n input = gets.strip\n lines << [input[0..1], input[3..4]]\nend\nfound = false\nfor i in 0..(lines.length-1) do\n if (lines[i][0] == \"OO\") \n answer(i, 0, lines)\n found = true\n break\n end\n if (lines[i][1] == \"OO\")\n answer(i, 1, lines)\n found = true\n break\n end\nend\nif not found\n puts \"NO\"\nend\n\n"}, {"source_code": "gets\ns=$<.read\nres=s.sub \"OO\", \"++\"\nif res!=s\n puts \"YES\"\n puts res\nelse\n puts \"NO\"\nend"}, {"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\n\nn = gets.to_i\nrows = []\nn.times {\n rows << gets.chomp.split(\"|\")\n}\nfound = false\nfor i in 0...n\n if rows[i][0] == \"OO\"\n found = true\n rows[i][0] = \"++\"\n break\n end\n\n if rows[i][1] == \"OO\"\n found = true\n rows[i][1] = \"++\"\n break\n end\nend\n\nif found\n puts \"YES\"\n for row in rows\n puts row.join(\"|\")\n end\nelse\n puts \"NO\"\nend\n"}, {"source_code": "n = Integer(gets.chomp)\nbus = Array.new(n)\nfor i in 0...n\n bus[i] = gets.chomp.split(\"\")\nend\n\ncan_seat_together = false\n\nfor i in 0...n\n if bus[i][0] == \"O\" && bus[i][1] == \"O\"\n can_seat_together = true\n bus[i][0] = \"+\"\n bus[i][1] = \"+\"\n break\n elsif bus[i][3] == \"O\" && bus[i][4] == \"O\"\n can_seat_together = true\n bus[i][3] = \"+\"\n bus[i][4] = \"+\"\n break\n end\nend\n\nif can_seat_together\n puts \" YES\"\n for i in 0...n\n print bus[i].join\n puts\n end\nelse\n puts \"NO\"\nend\n "}, {"source_code": "rows = gets.chomp.to_i\nseats = []\nfor i in (0..rows-1)\n\tseats[i] = gets.chomp\nend\n\nhas_seat = -1\nfor i in (0..rows-1)\n\tsplit = seats[i].split(\"\")\n\tfor x in (0..split.size-1)\n\t\tif split[x] == \"O\" && split[x+1] == \"O\"\n\t\t\tsplit[x] = \"+\"\n\t\t\tsplit[x+1] = \"+\"\n\t\t\tputs \"YES\"\n\t\t\tnew_seats = []\n\t\t\tfor a in (0..4)\n\t\t\t\tnew_seats << split[a]\n\t\t\tend\n\t\t\thas_seat = i\n\t\t\tbreak\n\t\tend\n\tend\n\tif has_seat != -1\n\t\tbreak\n\tend\nend\n\nif has_seat != -1\n\tfor i in (0..has_seat)\n\t\tif i < has_seat\n\t\t\tputs seats[i]\n\t\telse\n\t\t\tfor x in (0..4)\n\t\t\t\tif x == 4\n\t\t\t\t\tputs new_seats[x]\n\t\t\t\telse\n\t\t\t\t\tprint new_seats[x]\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\tend\n\tfor i in (has_seat+1..rows-1)\n\t\tputs seats[i]\n\tend\nelse\n\tputs \"NO\"\nend"}, {"source_code": "require 'pp'\nrows = gets.chomp.to_i\nrow = []\nfact = \"NO\"\nrows.times do |i|\n\n row << gets.chomp.to_s\n values = row[-1].split('|')\n if values.include?(\"OO\") && fact == \"NO\"\n \tfact = \"YES\"\n \trow[-1].sub!(\"OO\", \"++\") \n end\n end \t\n puts fact\n puts row if fact == \"YES\"\n "}, {"source_code": "d = gets.to_i.times.map{|x| gets.strip.split(\"|\")}\nans = false\nlist = []\nd.each do |x| \n if ans == false && x.include?(\"OO\")\n if x[0] == \"OO\"\n list.push(['++', x[1]])\n else\n list.push([x[0],'++'])\n end\n ans = true\n else\n list.push(x)\n end\nend\nputs (ans)? 'YES' : 'NO' \nif ans\n list.each{|x| puts x.join('|')}\nend"}, {"source_code": "n = gets.chomp\narray = Array.new(n.to_i) { |i| gets.chomp}\n\n\nfound = false\narray.each do |i|\n if i[0] == 'O' && i[1] == 'O'\n i[0] = '+'\n i[1] = '+'\n found = true\n break\n elsif i[3] == 'O' && i[4] == 'O'\n i[3] = '+'\n i[4] = '+'\n found = true\n break\n end\nend\n\nif found == true\n puts 'YES'\n puts array\nelse\n puts 'NO'\nend\n"}, {"source_code": "n = gets.to_i\na = n.times.map{gets.chomp.chars}\nans = false\na.each do |x|\n if x[0] == 'O' && x[1] == 'O'\n x[0], x[1] = '+', '+'\n ans = true\n break\n elsif x[3] == 'O' && x[4] == 'O'\n x[3], x[4] = '+', '+'\n ans = true\n break\n end\nend\nputs ans ? 'YES' : 'NO'\nputs a.map{|x| x.join('')} if ans\n"}, {"source_code": "n = gets.to_i\nf = false\na = Array.new(n, \"\")\nn.times{|i|\n x, y = gets.chomp.split('|')\n if !f && x == \"OO\"\n f = true\n a[i] += \"++\"\n else\n a[i] += x\n end\n a[i] += \"|\"\n if !f && y == \"OO\"\n f = true\n a[i] += \"++\"\n else\n a[i] += y\n end\n }\nif f\n puts \"YES\"\n puts a\nelse\n puts \"NO\"\nend"}, {"source_code": "n = gets.to_i\na = 1.upto(n).map { gets.chomp }\n\nindex = a.find_index { |s| s.include? 'OO' }\n\nif index\n puts 'YES'\n a[index].sub!('OO', '++')\n\n puts a.join(\"\\n\")\nelse\n puts 'NO'\nend\n"}, {"source_code": "n=readline.to_i \nresult = []\nfound = false\n(1..n).each do\n\tseats = readline.split('|').map{|x| x.strip}\n\tseats.each_index{ |x|\n\t\tif !found && seats[x]=='OO'\n\t\t\tseats[x]='++'\n\t\t\tfound=true \n\t\tend\n\t}\n\tresult << seats.join('|')\nend\nputs found ? 'YES' : 'NO'\nputs result if found"}], "negative_code": [{"source_code": "n = gets.to_i\n\nf = false\nout = []\nn.times do\n s = gets.strip\n \n if s.include?(\"OO\")\n f = true\n s = s.sub(\"OO\",'++')\n end\n\n out << s\nend\nif f\n puts \"YES\",out\nelse\n puts \"NO\"\nend\n"}, {"source_code": "n = gets.to_i\ns = []\nn.times do |i|\n\ts[i] = gets.chomp\nend\nx = false\nfor i in 0...5\n\tif x == false\n\t\tif s[i][0...2] == 'OO'\n\t\t\ts[i][0...2] = '++'\n\t\t\tx = true\n\t\telsif s[i][3...5] == 'OO'\n\t\t\ts[i][3...5] = '++'\n\t\t\tx = true\n\t\tend\n\n\tend\nend\nif x == false\n\tputs 'NO'\nelse\n\tputs 'YES'\n\t0.upto(4) do |i|\n\t\tputs s[i]\n\tend\nend\n"}, {"source_code": "s = ''\n(1..gets.chomp.to_i).each do\n s << gets.chomp << '#'\nend\nbegin\n s[/OO/] = '++'\n s.split('#').each { |q| puts q }\nrescue\n puts 'NO'\nend"}, {"source_code": "require 'pp'\nrows = gets.chomp.to_i\nrow = []\nfact = \"NO\"\nrows.times do |i|\n\n row << gets.chomp.to_s\n values = row[-1].split('|')\n if values.include?(\"OO\") && fact == \"NO\"\n \tfact = \"YES\"\n \trow[-1].sub!(\"OO\", \"++\") \n end\n end \t\n puts fact\n puts row"}, {"source_code": "d = gets.to_i.times.map{|x| gets.strip.split(\"|\")}\nans = false\nlist = []\nd.each do |x| \n if ans == false && x.include?(\"OO\")\n if x[0] == \"00\"\n list.push(['++', x[1]])\n else\n list.push([x[0],'++'])\n end\n ans = true\n else\n list.push(x)\n end\nend\nputs (ans)? 'YES' : 'NO' \nlist.each{|x| puts x.join('|')}"}, {"source_code": "d = gets.to_i.times.map{|x| gets.strip.split(\"|\")}\nans = false\nlist = []\nd.each do |x| \n if ans == false && x.include?(\"OO\")\n if x[0] == \"OO\"\n list.push(['++', x[1]])\n else\n list.push([x[0],'++'])\n end\n ans = true\n else\n list.push(x)\n end\nend\nputs (ans)? 'YES' : 'NO' \nlist.each{|x| puts x.join('|')}"}, {"source_code": "n = gets.to_i\na = n.times.map{gets.chomp.chars}\nans = false\na.each do |x|\n if x[0] == 'O' && x[1] == 'O'\n x[0], x[1] = '-', '-'\n ans = true\n break\n elsif x[3] == 'O' && x[4] == 'O'\n x[3], x[4] = '-', '-'\n ans = true\n break\n end\nend\nputs ans ? 'YES' : 'NO'\nputs a.map{|x| x.join('')} if ans\n"}, {"source_code": "n = gets.to_i\na = n.times.map{gets.chomp.chars}\nans = false\na.each do |x|\n if x[0] == 'O' && x[1] == 'O'\n x[0], x[1] = '-', '-'\n ans = true\n break\n elsif x[3] == 'O' && x[4] == 'O'\n x[3], x[4] = '+', '+'\n ans = true\n break\n end\nend\nputs ans ? 'YES' : 'NO'\nputs a.map{|x| x.join('')} if ans\n"}], "src_uid": "e77787168e1c87b653ce1f762888ac57"} {"nl": {"description": "You are given n points on a line with their coordinates xi. Find the point x so the sum of distances to the given points is minimal.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 3·105) — the number of points on the line. The second line contains n integers xi ( - 109 ≤ xi ≤ 109) — the coordinates of the given n points.", "output_spec": "Print the only integer x — the position of the optimal point on the line. If there are several optimal points print the position of the leftmost one. It is guaranteed that the answer is always the integer.", "sample_inputs": ["4\n1 2 3 4"], "sample_outputs": ["2"], "notes": null}, "positive_code": [{"source_code": "n=gets.to_i\na=gets.split.map(&:to_i).sort\np a[~-n/2]"}, {"source_code": "n = gets.to_i\narr = gets.split.map(&:to_i)\narr.sort!\n\nputs arr.size.even? ? arr[n/2 - 1] : arr[n/2]"}], "negative_code": [{"source_code": "n = gets.to_i\narr = gets.split.map(&:to_i)\n\nputs arr.size.even? ? arr[n/2 - 1] : arr[n/2]"}], "src_uid": "fa9cc3ba103ed1f940c9e80a7ea75f72"} {"nl": {"description": "A sequence of $$$n$$$ integers is called a permutation if it contains all integers from $$$1$$$ to $$$n$$$ exactly once.Given two integers $$$n$$$ and $$$k$$$, construct a permutation $$$a$$$ of numbers from $$$1$$$ to $$$n$$$ which has exactly $$$k$$$ peaks. An index $$$i$$$ of an array $$$a$$$ of size $$$n$$$ is said to be a peak if $$$1 < i < n$$$ and $$$a_i \\gt a_{i-1}$$$ and $$$a_i \\gt a_{i+1}$$$. If such permutation is not possible, then print $$$-1$$$.", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\leq t \\leq 100$$$) — the number of test cases. Then $$$t$$$ lines follow, each containing two space-separated integers $$$n$$$ ($$$1 \\leq n \\leq 100$$$) and $$$k$$$ ($$$0 \\leq k \\leq n$$$) — the length of an array and the required number of peaks.", "output_spec": "Output $$$t$$$ lines. For each test case, if there is no permutation with given length and number of peaks, then print $$$-1$$$. Otherwise print a line containing $$$n$$$ space-separated integers which forms a permutation of numbers from $$$1$$$ to $$$n$$$ and contains exactly $$$k$$$ peaks. If there are multiple answers, print any.", "sample_inputs": ["5\n1 0\n5 2\n6 6\n2 1\n6 1"], "sample_outputs": ["1 \n2 4 1 5 3 \n-1\n-1\n1 3 6 5 4 2"], "notes": "NoteIn the second test case of the example, we have array $$$a = [2,4,1,5,3]$$$. Here, indices $$$i=2$$$ and $$$i=4$$$ are the peaks of the array. This is because $$$(a_{2} \\gt a_{1} $$$, $$$a_{2} \\gt a_{3})$$$ and $$$(a_{4} \\gt a_{3}$$$, $$$a_{4} \\gt a_{5})$$$. "}, "positive_code": [{"source_code": "def array_peaks(n, k)\r\n # n is size of output array\r\n # k is number of peaks required\r\n\r\n # if even max possible peaks is (n - 2) / 2\r\n # if odd max possible peaks is (n - 1) / 2\r\n if (n % 2) == 0\r\n if k > (n-2)/2\r\n puts -1\r\n return\r\n end\r\n else\r\n if k > (n-1)/2\r\n puts -1\r\n return\r\n end\r\n end\r\n\r\n a = (1..n).to_a\r\n o = []\r\n peaks = 0\r\n asc = true\r\n\r\n while(a.size > 0) do\r\n if peaks < k\r\n if asc\r\n o << a.shift\r\n asc = false\r\n else\r\n o << a.pop\r\n asc = true\r\n peaks += 1\r\n end\r\n else\r\n o << a.pop\r\n end\r\n end\r\n\r\n puts o.join(\" \")\r\nend\r\n\r\nnum_input = gets.chomp\r\nnum_input.to_i.times do\r\n args = gets.chomp.split(' ').map(&:to_i)\r\n array_peaks(*args)\r\nend"}, {"source_code": "test_cases = gets.chomp.to_i\r\nfor i in 1..test_cases do\r\n line = gets.chomp.split(\" \")\r\n n = line[0].to_i\r\n k = line[1].to_i\r\n if k <= (n - 1) / 2\r\n a = Array.new(n, -1)\r\n for j in 1..k do\r\n a[2 * j - 1] = n + 1 - j\r\n end\r\n x = k + 1\r\n for j in 0..n - 1 do\r\n if a[j] == -1\r\n a[j] = n + 1 - x\r\n x += 1\r\n end\r\n end\r\n for j in 0..n - 1 do\r\n print \"#{a[j]} \"\r\n end\r\n puts \"\"\r\n else\r\n puts -1\r\n end\r\nend"}, {"source_code": "t = gets.to_s.to_i\n\n1.upto(t) do |casenum|\n n, k = gets.to_s.split.map {|x| x.to_i }\n a = (1 .. n).to_a\n swap_pos = a.size - 1\n while k > 0 && swap_pos > 1\n a[swap_pos], a[swap_pos - 1] = a[swap_pos - 1], a[swap_pos]\n k -= 1\n swap_pos -= 2\n end\n\n if k > 0\n printf(\"-1\\n\")\n else\n printf(\"%s\\n\", a.join(\" \"))\n end\nend\n\n"}], "negative_code": [], "src_uid": "b4bb11ea4650ead54026453ea9a76f39"} {"nl": {"description": "Vasya adores sport programming. He can't write programs but he loves to watch the contests' progress. Vasya even has a favorite coder and Vasya pays special attention to him.One day Vasya decided to collect the results of all contests where his favorite coder participated and track the progress of his coolness. For each contest where this coder participated, he wrote out a single non-negative number — the number of points his favorite coder earned in the contest. Vasya wrote out the points for the contest in the order, in which the contests run (naturally, no two contests ran simultaneously).Vasya considers a coder's performance in a contest amazing in two situations: he can break either his best or his worst performance record. First, it is amazing if during the contest the coder earns strictly more points that he earned on each past contest. Second, it is amazing if during the contest the coder earns strictly less points that he earned on each past contest. A coder's first contest isn't considered amazing. Now he wants to count the number of amazing performances the coder had throughout his whole history of participating in contests. But the list of earned points turned out long and Vasya can't code... That's why he asks you to help him.", "input_spec": "The first line contains the single integer n (1 ≤ n ≤ 1000) — the number of contests where the coder participated. The next line contains n space-separated non-negative integer numbers — they are the points which the coder has earned. The points are given in the chronological order. All points do not exceed 10000.", "output_spec": "Print the single number — the number of amazing performances the coder has had during his whole history of participating in the contests.", "sample_inputs": ["5\n100 50 200 150 200", "10\n4664 6496 5814 7010 5762 5736 6944 4850 3698 7242"], "sample_outputs": ["2", "4"], "notes": "NoteIn the first sample the performances number 2 and 3 are amazing.In the second sample the performances number 2, 4, 9 and 10 are amazing."}, "positive_code": [{"source_code": "n = gets.to_i\nar = gets.split(' ',n).map(&:to_i)\nmin, max, record = ar[0], ar[0], 0\nar[1..-1].each do |x|\n record += 1 if xmax\n min, max = [min, max, x].minmax\nend\nputs record"}, {"source_code": "n = gets.to_i\na = gets.split.map &:to_i\nans = -2\nmx = -1e50\nmi = -mx\na.each do |x|\n if x > mx\n mx = x\n ans += 1\n end\n if x < mi\n mi = x\n ans += 1\n end\nend\np ans\n"}, {"source_code": "n = gets.to_i\n\nres = gets.split.map{|i| i.to_i}\n\nmax = res[0]\nmin = res[0]\nresult = 0\ni = 1\nwhile i < res.size do \n if res[i] > max then\n max = res[i]\n result += 1\n else\n if res[i] < min then\n min = res[i]\n result += 1\n end\n end\n i += 1\nend\n\nputs result"}, {"source_code": "gets\na = []\ngets.split.map(&:to_i).each do |b|\n a << b if a.empty? || b < a.min || b > a.max\nend\np a.size - 1\n"}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\narr=[a[0]]\nans=0\n1.upto(a.length-1){|i|\n ans+=1 if arr.min>a[i]||arr.max a[i]\n cnt += 1\n min = a[i]\n end\n if max < a[i]\n cnt += 1\n max = a[i]\n end\nend\nputs cnt"}, {"source_code": "n = gets.chomp.to_i\npoints = gets.chomp.split(\" \").map{|e| e.to_i}\n\npast = []\npast << points[0]\nc = 0\n(1..n-1).each do |i|\n if points[i] > past.max\n c += 1\n elsif points[i] < past.min\n c += 1\n end\n past << points[i]\nend\n\nputs c\n"}, {"source_code": "def count(a)\n tot_amazing = 0\n # The first content is the max and the min number of points\n min = a.shift\n max = min\n a.each do |p|\n if p < min\n min = p\n tot_amazing += 1\n elsif p > max\n max = p\n tot_amazing += 1\n end\n end\n tot_amazing\nend\n\nn = gets.to_i\na = gets.split(/\\s/).map(&:to_i)\n\nputs count(a)"}, {"source_code": "n = STDIN.readline.to_i\nscores = STDIN.readline.split.map {|s| s.to_i }\nmin, max = scores[0], scores[0]\ncount = 0\nscores[1..-1].each do |score|\n if score < min\n count += 1\n min = score\n end\n if score > max\n count += 1\n max = score\n end\nend\nputs count\n"}, {"source_code": "def i_arr(arr)\n\tfor i in 0...arr.length\n\t\tarr[i] = arr[i].to_i\n\tend\n\treturn arr\nend\n\nn = gets.to_i\narr = i_arr(gets.chomp.split)\nmin = arr[0]\nmax = arr[0]\nt = 0\n\nfor i in 1...n\n\ta = arr[i]\n\tif a > max\n\t\tmax = a\n\t\tt += 1\n\telsif a < min\n\t\tmin = a\n\t\tt += 1\n\tend\nend\n\nputs t"}, {"source_code": "n = STDIN.gets.chomp.to_i\nscores = STDIN.gets.chomp.split(/ /).take(n).map(&:to_i)\n\namazings = 0\nmin = 100000\nmax = -1\nscores.each_with_index do |score, i|\n if score < min\n min = score \n amazings += 1 unless i == 0\n end\n if score > max\n max = score \n amazings += 1 unless i == 0\n end\nend\np amazings\n"}, {"source_code": "n=gets.chomp.to_i\na=gets.chomp.split(' ').map(&:to_i)\nb=[a[0]]\ni=1\ncount=0\nwhile ib.max\n\t\t#puts \"YAY!\"\n\t\tcount+=1\n\tend\n\tb.push(a[i])\n\ti+=1\nend\nputs count\n"}, {"source_code": "n = gets.to_i\na = gets.strip.split.map(&:to_i)\nmax = a[0]\nmin = a[0]\nk = 0\n(1...n).each do |i|\n if a[i] > max\n k+=1\n max = a[i]\n end\n if a[i] < min\n k+=1\n min = a[i]\n end\nend\nputs k"}, {"source_code": "l = STDIN.read.split(\"\\n\")\nn = l[0].to_i\nmarks = l[1].split(\" \").map(&:to_i)\nn_i = 0\nmin = max = marks[0]\n\nfor i in 1..(n-1)\n if (min > marks[i])\n \tn_i += 1\n \tmin = marks[i]\n elsif (max < marks[i])\n \tn_i+= 1\n \tmax = marks[i]\n end\nend\n\nputs n_i"}, {"source_code": "n = gets.to_i\na = gets.split(\" \").map &:to_i\n\nmax = a[0]\nmin = a[0]\nk = 0\n\na.each do |x|\n if x > max\n k+=1\n max = x\n end\n\n if x < min\n k+=1\n min = x\n end\nend\n\nputs k"}, {"source_code": "\n \n n = gets.to_i\n a = gets.split.map(&:to_i)\n \n counter = 0\n min,max = a[0],a[0]\n for i in 1..n-1\n if a[i]>max\n max = a[i]\n counter+=1\n elsif a[i] < min \n min = a[i]\n counter+=1\n end\n end\n puts counter\n"}, {"source_code": "def Floodico(n,a)\n \n counter = 0\n min,max = a[0],a[0]\n for i in 1..n-1\n if a[i]>max\n max = a[i]\n counter+=1\n elsif a[i] < min \n min = a[i]\n counter+=1\n end\n end\n puts counter\n\nend\nFloodico(gets.to_i,gets.split.map(&:to_i)) "}, {"source_code": "n = $stdin.readline.to_i\nf = $stdin.readline.split.map(&:to_i)\n\nmax_count = 0\nmin_count = 0\nmax = f.first\nmin = f.first\n\nfor i in 1...f.count\n elem = f[i]\n if elem > max\n max_count+=1\n max = elem\n end\n if elem < min\n min_count+=1\n min = elem\n end\nend\n\nputs min_count + max_count"}, {"source_code": "#!/usr/bin/ruby\ndef read_int; gets.chomp.to_i; end\ndef read_ints; gets.chomp.split.map(&:to_i); end\n\nn = read_int\narr = read_ints\n\nfirst = arr.shift\nmin = first\nmax = first\ncount = 0\narr.each do |i|\n if i < min\n min = i\n count += 1\n elsif i > max\n max = i\n count += 1\n end\nend\n\nputs count\n"}, {"source_code": "gets\narr = gets.split.map &:to_i\nmax, min = arr.first, arr.first\nk = 0\narr.shift\narr.each do |p|\n if p > max \n max = p\n k += 1\n elsif p < min\n min = p\n k += 1\n end\nend\nputs k\n"}, {"source_code": "n = gets.chomp.to_i\narr = gets.split.map(&:to_i)\nbig = arr[0]\nsmall = arr[0]\narr.shift\nres = 0\narr.each do |num|\n if num > big\n big = num\n res+=1\n elsif num < small\n small = num\n res+=1\n end\nend\nputs res"}, {"source_code": "a=gets.chomp.to_i\nb=gets.chomp.split(\" \")\nif a==1\nputs \"0\"\nelse \nmax=b[0].to_i\nmin=b[0].to_i\namz=0\n1.upto(a-1) do |i|\nif b[i].to_i>max\nmax=b[i].to_i\namz+=1\nelsif b[i].to_i highest\n ans += 1\n highest = as[i]\n end\nend\n\nputs ans\n"}, {"source_code": "n = gets.strip.to_i\nscores = gets.strip.split.map(&:to_i)\nmin_score = max_score = scores[0]\nans = 0\n(1...n).each do |i|\n if scores[i] < min_score\n min_score = scores[i]\n ans += 1\n end\n if scores[i] > max_score\n max_score = scores[i]\n ans += 1\n end\nend\nputs ans\n"}, {"source_code": "a=gets.chomp.to_i\nb=gets.chomp.split(\" \")\nif a==1\nputs \"0\"\nelse \nmax=b[0].to_i\nmin=b[0].to_i\namz=0\n1.upto(a-1) do |i|\nif b[i].to_i>max\nmax=b[i].to_i\namz+=1\nelsif b[i].to_it then\n\t\tmin1=t\n\t\tans=ans+1\n\tend\n\tif max1max||b[x]max\n if x==0\n min=b[x];max=b[x] \n end\n}\nputs c\n"}, {"source_code": "gets\na = gets.split.map(&:to_i)\n\nm = a[0]\npr = a[0]\nc = 0\n\na.each do |e|\n if e > pr\n c += 1\n pr = e\n elsif e < m\n c += 1\n m = e\n end\nend\n\np c\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nm,l,c = a[0],a[0],0\nfor i in a \n\tif i > m\n\t\tc += 1\n\t\tm = i\n\telsif i < l\n\t\tc += 1\n\t\tl = i\n\tend\t\t\nend\nputs c"}, {"source_code": "ans=0\nn=gets.to_i\na=gets.split.collect {|i|i.to_i}\nmin=max=a[0]\n1.upto(n-1){|i|\n\tif (a[i] > max) then\n\t\tmax=a[i]\n\t\tans+=1\n\telsif (a[i] < min) then\n\t\tmin=a[i]\n\t\tans+=1\n\tend}\np ans\n"}, {"source_code": "n = gets.to_i\nq = gets.chomp.split.map { |x| x.to_i }\n\nmin = q[0]\nmax = q[0]\nwow = 0\n\nfor i in 1...n\n if q[i] > max\n max = q[i]\n wow +=1\n elsif q[i] < min\n min = q[i]\n wow +=1\n end\nend\np wow"}, {"source_code": "n = gets.to_i\n\na = gets.split.map(&:to_i)\nb=[a[0]]\nans = a[1...a.size].count do |x|\n ret = (x > b.max or x < b.min)\n b << x\n ret\nend\n\nputs ans"}, {"source_code": "# http://codeforces.com/problemset/problem/155/A\n\ncount = gets.chomp.to_i\nscores = gets.chomp.split.map { |e| e.to_i }\n\ntmp_scores = []\nfirst = true\n\namazing = 0\n\nscores.each do |score|\n if first\n first = false\n tmp_scores << score\n else\n if score > tmp_scores.max or score < tmp_scores.min\n amazing += 1\n tmp_scores << score\n end\n end\nend\n\nputs \"#{amazing}\""}, {"source_code": "n = gets.to_i\na = gets.split(' ').map(&:to_i)\nmax, min = a[0], a[0]\nk = 0\nfor i in 1...n\n if a[i] > max\n k += 1\n max = a[i]\n end\n if a[i] < min\n k += 1\n min = a[i]\n end\nend\nputs k"}, {"source_code": "number = gets.chomp.to_i\nn = gets.chomp\ncontests = n.split\n\nmaxm = contests[0].to_i\nminm = contests[0].to_i\ncount = 0\nfor i in (1..number-1) # i = subject\n\tx = i\n\twhile x != 0 # x = compare to\n\t\tif contests[i].to_i > maxm\n\t\t\tmaxm = contests[i].to_i\n\t\t\tcount += 1\n\t\telsif contests[i].to_i < minm\n\t\t\tminm = contests[i].to_i\n\t\t\tcount += 1\n\t\tend\n\t\tx -= 1\n\tend\nend\n\nprint count"}, {"source_code": "contests = gets.to_i\npoints = gets.chomp.split(' ').map { |e| e.to_i }\namazing, max, min = 0, points[0], points[0]\n(0...contests).each do |i|\n if points[i] > max\n amazing += 1\n max = points[i]\n end\n if points[i] < min\n amazing += 1\n min = points[i]\n end\nend\nputs amazing"}, {"source_code": "gets\nl = gets.split.map(&:to_i)\nc = 0\nl.each_with_index { |x, i| c += 1 if i != 0 and (l[0...i].all? { |y| y > x } or l[0...i].all? { |y| y < x }) }\nputs c"}, {"source_code": "n = gets.to_i\nm = gets.split.collect &:to_i\n\nmin_rec = m[0]\nmax_rec = m[0]\nasr = 0\ni = 0\n\nwhile i < n do\n if (min_rec > m[i])\n begin\n min_rec = m[i]\n asr += 1\n end\n end\n if (max_rec < m[i])\n begin\n max_rec = m[i]\n asr += 1\n end\n end\n i += 1\nend\nputs asr\n\n\n"}, {"source_code": "a=gets.chomp.to_i\nb=gets.chomp.split(\" \")\nif a==1\nputs \"0\"\nelse \nmax=b[0].to_i\nmin=b[0].to_i\namz=0\n1.upto(a-1) do |i|\nif b[i].to_i>max\nmax=b[i].to_i\namz+=1\nelsif b[i].to_i max)\n \tmax = s\n \tcount += 1\n end\nend\nputs count"}, {"source_code": "n = gets.to_i\na = gets.split(' ').map(&:to_i)\n\nx = a.shift\nresult = a.inject({min: x, max: x, result: 0}) do |hash, i|\n r = hash[:result] + ((ihash[:max]) ? 1 : 0)\n { min: [hash[:min], i].min, max: [hash[:max], i].max, result: r }\nend\n\nputs result[:result]\n"}, {"source_code": "gets\n\nscores = gets.split.map(&:to_i)\n\n\nmin = max = scores[0]\n\namazing = 0\n\nscores.each do |score|\n if score < min\n min = score\n amazing += 1\n elsif score > max\n max = score\n amazing += 1\n end\nend\n\nputs amazing\n"}, {"source_code": "gets\n\nscores = gets.split.map(&:to_i)\n\n\nmin = max = scores[0]\n\namazing = 0\n\nscores.each do |score|\n if score < min\n min = score\n amazing += 1\n elsif score > max\n max = score\n amazing += 1\n end\nend\n\nputs amazing\n"}, {"source_code": "gets\nwhile 1 do\n gets and $a = $_.chomp.split(\" \")\n $_ and $max = $min = $a.shift.to_i\n $_ and $sum = 0\n $a.empty? and break\n $p = $a.shift.to_i\n $p > $max and $max = $p and $sum+=1\n $p < $min and $min = $p and $sum+=1\nend\nputs $sum"}, {"source_code": "n=gets.to_i\na=0\nb=gets.split.map(&:to_i)\nmin=max=b.first\nb.each{|c|\n\tif c>max\n\t\ta+=1\n\t\tmax=c\n\tend\n\tif c i\n (max = i; c += 1) if max < i\nend\n\nprint c"}, {"source_code": "#!ruby\n\nn = gets.to_i\n\na = gets.split.map{|e|e.to_i}\n\nmax = min = a.shift\n\ncount = 0\n\na.each do |e|\n if e < min\n count += 1\n min = e\n elsif e > max\n count += 1\n max = e\n end\nend\n\nputs count\n"}], "negative_code": [{"source_code": "n = gets.chomp.to_i\npoints = gets.chomp.split(\" \").map{|e| e.to_i}\n\npast = []\npast << points[0]\nc = 0\n(1..n-1).each do |i|\n if points[i] > past.max\n c += 1\n elsif points[i] < past.min\n c += 1\n end\nend\n\nputs c\n"}, {"source_code": "def Floodico(n,a)\n \n counter = 0\n min,max = a[0],a[0]\n for i in 1..n-1\n if a[i]>max\n max = a[i]\n counter+=1\n elsif a[i] < min \n min = a[i]\n counter+=1\n end\n end\n return counter\n\nend\nFloodico(gets.to_i,gets.split.map(&:to_i)) "}, {"source_code": "gets\nx=gets.split.map{|e| e.to_i}\nans=0\ns=x[0]\nx.shift\nwhile !x.empty?\n\tans=ans+1 if x[0]>s\n\ts=x[0]\n\tx.shift\nend\nputs ans"}, {"source_code": "a=gets.chomp.to_i\nb=gets.chomp.split(' ')\nb.length.times {|x| b[x]=b[x].to_i}\nc=0;min=0;max=0\nb.length.times { |x|\n c+=1 if x!=0&&(b[x]>max||b[x] m[i])\n begin\n min_rec = m[i]\n asr += 1\n end\n end\n if (max_rec < m[i])\n begin\n max_rec = m[i]\n asr += 1\n end\n end\n i += 1\n end\n puts asr\nend\n\n"}], "src_uid": "a61b96d4913b419f5715a53916c1ae93"} {"nl": {"description": "Now you can take online courses in the Berland State University! Polycarp needs to pass k main online courses of his specialty to get a diploma. In total n courses are availiable for the passage.The situation is complicated by the dependence of online courses, for each course there is a list of those that must be passed before starting this online course (the list can be empty, it means that there is no limitation).Help Polycarp to pass the least number of courses in total to get the specialty (it means to pass all main and necessary courses). Write a program which prints the order of courses. Polycarp passes courses consistently, he starts the next course when he finishes the previous one. Each course can't be passed more than once. ", "input_spec": "The first line contains n and k (1 ≤ k ≤ n ≤ 105) — the number of online-courses and the number of main courses of Polycarp's specialty. The second line contains k distinct integers from 1 to n — numbers of main online-courses of Polycarp's specialty. Then n lines follow, each of them describes the next course: the i-th of them corresponds to the course i. Each line starts from the integer ti (0 ≤ ti ≤ n - 1) — the number of courses on which the i-th depends. Then there follows the sequence of ti distinct integers from 1 to n — numbers of courses in random order, on which the i-th depends. It is guaranteed that no course can depend on itself. It is guaranteed that the sum of all values ti doesn't exceed 105. ", "output_spec": "Print -1, if there is no the way to get a specialty. Otherwise, in the first line print the integer m — the minimum number of online-courses which it is necessary to pass to get a specialty. In the second line print m distinct integers — numbers of courses which it is necessary to pass in the chronological order of their passage. If there are several answers it is allowed to print any of them.", "sample_inputs": ["6 2\n5 3\n0\n0\n0\n2 2 1\n1 4\n1 5", "9 3\n3 9 5\n0\n0\n3 9 4 5\n0\n0\n1 8\n1 6\n1 2\n2 1 2", "3 3\n1 2 3\n1 2\n1 3\n1 1"], "sample_outputs": ["5\n1 2 3 4 5", "6\n1 2 9 4 5 3", "-1"], "notes": "NoteIn the first test firstly you can take courses number 1 and 2, after that you can take the course number 4, then you can take the course number 5, which is the main. After that you have to take only the course number 3, which is the last not passed main course. "}, "positive_code": [{"source_code": "require 'set'\ndec = lambda{|x|x-1}\ninc = lambda{|x|x+1}\n\nn, k\t\t\t = gets.split.map(&:to_i)\nmain_courses \t = gets.split.map(&:to_i).map(&dec) # decrement course id's, so 1=>0\ndeps \t\t\t = [] #dependencies\n\nn.times do\n\tcs = gets.split.map(&:to_i)\n\tdeps << cs[1..-1].map(&dec)\nend\n\nclass Set\n\tdef pop\n\t\tc = first\n\t\tdelete c\n\t\treturn c\n\tend\nend\n\npassed_set = Set.new\t# for lookup\npassed_arr = []\t\t\t# for order\npassing = Set.new\n\nqueue = main_courses.dup\nsuccess = nil\n\n#passed? - is in PASSED\ndefine_method (:passed?) {|course| passed_set.include?(course)}\n#passed! - add to PASSED, remove from PASSING\ndefine_method (:passed!) do |course| \n\tpassed_set << course\n\tpassed_arr << course\n\tpassing.delete course\nend\n#passing? - is in PASSING\ndefine_method (:passing?) {|course| passing.include?(course)}\n#passing! - add to PASSING\ndefine_method (:passing!) {|course| passing << course}\nloop do\n\tif queue.length==0\n\t\tsuccess = true\n\t\tbreak\n\tend\n\tif queue[-1] == :open\n\t\tqueue.pop #:open\n\t\tpassed! queue.pop\n\telse\n\t\tc_id = queue[-1]\n\t\tif passing? c_id\n\t\t\tsuccess = false\n\t\t\tbreak\n\t\tend\n\t\tif passed? c_id\n\t\t\tqueue.pop\n\t\t\tnext\n\t\tend\n\t\tpassing! c_id\n\t\tqueue << :open\n\t\tdeps[c_id].each do |dep_id|\n\t\t\tqueue << dep_id\n\t\tend\n\tend\nend\n\nif success\n\tputs passed_arr.length\n\tputs passed_arr.map(&inc).join(' ')\nelse\n\tputs -1\nend\n\n=begin\nwhile have_to_pass.length>0\n\tcourse = have_to_pass.pop\n\t# First, we need to pass all dependency courses.\n\tif passing_now.include? course # This means there is an unsolvable dependency loop, like in example#3\n\t\tputs -1\n\t\texit\n\tend\n\tpassing_now.add course\n\tdeps[course].each do |c|\n\t\thave_to_pass\n\tend\nend\n=end\n\n=begin\ncourses = Set.new # demanded courses, for searching\ncourses_rev = [] # demanded courses, preserving order\n\ncourses_to_demand = main_courses.dup\n\nwhile courses_to_demand.length > 0\n\tc_id = courses_to_demand.pop\n\tnext if courses.include?(c_id)\n\tcourses << c_id\n\tcourses_rev << c_id\n\tdeps[c_id].each do |dep_id|\n\t\tcourses_to_demand.push dep_id if !courses.include?(dep_id)\n\tend\nend\n=end\n"}], "negative_code": [], "src_uid": "e984b4e1120b8fb083a3ebad9bb93709"} {"nl": {"description": "Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.Initially, the text cursor is at position p. Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?", "input_spec": "The first line contains two space-separated integers n (1 ≤ n ≤ 105) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor. The next line contains n lowercase characters of Nam's string.", "output_spec": "Print the minimum number of presses needed to change string into a palindrome.", "sample_inputs": ["8 3\naeabcaez"], "sample_outputs": ["6"], "notes": "NoteA string is a palindrome if it reads the same forward or reversed.In the sample test, initial Nam's string is: (cursor position is shown bold).In optimal solution, Nam may do 6 following steps:The result, , is now a palindrome."}, "positive_code": [{"source_code": "# Author : Fuad Ashraful Mehmet \n#Date: 18th April\n\n\n\nn,pos = gets.split(\" \").map(&:to_i)\narr = gets.chomp\nmid = (n+1) / 2\nunless pos <= mid\n\tpos = n - pos + 1\n\tarr.reverse!\nend\nc = 0\nlast_ind = pos - 1\n((pos - 1)...mid).each do |i|\n\tif arr[i] != arr[n - i - 1]\n\t\ta = arr[i].ord \n\t\tb = arr[n - i - 1].ord\n\t\tval = []\n\t\tif a < b\n\t\t\tval << b - a \n\t\t\tval << a - b + 26\n\t\telse\n\t\t\tval << a - b\n\t\t\tval << b - a + 26\n\t\tend\n\t\tc += val.min\n\t\tlast_ind = i\n\tend\nend\n \nfirst_ind = nil\n(0...(pos-1)).each do |i|\n\tif arr[i] != arr[n - i - 1]\n\t\ta = arr[i].ord \n\t\tb = arr[n - i - 1].ord\n\t\tval = []\n\t\tif a < b\n\t\t\tval << b - a \n\t\t\tval << a - b + 26\n\t\telse\n\t\t\tval << a - b\n\t\t\tval << b - a + 26\n\t\tend\n\t\tc += val.min\n\t\tfirst_ind = i unless first_ind\n\tend\nend\n\nunless first_ind\n\tc += last_ind - (pos - 1)\nelse\n\tfirst_dist = pos - 1 - first_ind\n\tlast_dist = last_ind - (pos - 1)\n\tc += first_dist + last_dist\n\tc += [first_dist,last_dist].min\nend\n \nputs c"}, {"source_code": "cost = {}\n('a'..'z').each {|x| cost[x] = {} }\n('a'..'z').each do |x|\n\tdiff = -1\n\t(x..'z').each do |y|\n\t\tdiff += 1\n\t\tcost[x][y] = cost[y][x] = [diff, 26-diff].min\n\tend\nend\n\nn, p = STDIN.readline.split.map {|s| s.to_i }\ns = ' '+STDIN.readline.strip\ncenter = (n+1)/2\nif p > center\n\tp = n-p+1\nend\nq = n-p+1\n\nleft_correction, left_span = 0, 0\n(p-1).downto(1) do |i|\n\ta, b = s[i], s[n-i+1]\n\tif a != b\n\t\tleft_correction += cost[a][b]\n\t\tleft_span = p-i\n\tend\nend\nright_correction, right_span = 0, 0\n(p+1).upto(center) do |i|\n\ta, b = s[i], s[n-i+1]\n\tif a != b\n\t\tright_correction += cost[a][b]\n\t\tright_span = i-p\n\tend\nend\n\ntotal = cost[s[p]][s[q]]\n#puts 'total so far = %d' % total\n#puts 'left_correction = %d, left_span = %d' % [left_correction, left_span]\n#puts 'right_correction = %d, right_span = %d' % [right_correction, right_span]\ntotal += left_correction + right_correction\ntotal += left_span + right_span\ntotal += [left_span, right_span].min\nputs total\n"}, {"source_code": "# Here your code !\n\nN, P_ = gets.split.map(&:to_i)\nP__ = P_-1\nstr = gets.chomp\nif P__>= N/2\n str.reverse!\n P = N - (P__+1)\nelse\n P = P__\nend\n\ndef orddiff(c1, c2)\n if(c1.ord > c2.ord)\n c1, c2 = c2, c1\n end\n d = c2.ord - c1.ord\n if d>13\n d = 26 - d\n end\n return d\nend\n\ncount = 0\nrightmax = 0\n(P...N/2).each{|p|\n if str[p] != str[N-p-1] then\n rightmax = p - P\n count += orddiff(str[p], str[N-p-1])\n end\n}\nleftmax = 0\n(P-1).downto(0).each{|p|\n if str[p] != str[N-p-1] then\n leftmax = P-p\n count += orddiff(str[p], str[N-p-1])\n end\n}\n# puts \"P=#{P} leftmax=#{leftmax}, rightmax=#{rightmax}, count=#{count}\"\n\nif leftmax == 0\n count += rightmax\nelsif rightmax == 0\n count += leftmax\nelsif leftmax < rightmax\n count += leftmax*2 + rightmax\nelse\n count += rightmax*2 + leftmax\nend\nputs count\n\n"}, {"source_code": "n,pos = gets.split(\" \").map(&:to_i)\narr = gets.chomp\nmid = (n+1) / 2\nunless pos <= mid\n\tpos = n - pos + 1\n\tarr.reverse!\nend\nc = 0\nlast_ind = pos - 1\n((pos - 1)...mid).each do |i|\n\tif arr[i] != arr[n - i - 1]\n\t\ta = arr[i].ord \n\t\tb = arr[n - i - 1].ord\n\t\tval = []\n\t\tif a < b\n\t\t\tval << b - a \n\t\t\tval << a - b + 26\n\t\telse\n\t\t\tval << a - b\n\t\t\tval << b - a + 26\n\t\tend\n\t\tc += val.min\n\t\tlast_ind = i\n\tend\nend\n\nfirst_ind = nil\n(0...(pos-1)).each do |i|\n\tif arr[i] != arr[n - i - 1]\n\t\ta = arr[i].ord \n\t\tb = arr[n - i - 1].ord\n\t\tval = []\n\t\tif a < b\n\t\t\tval << b - a \n\t\t\tval << a - b + 26\n\t\telse\n\t\t\tval << a - b\n\t\t\tval << b - a + 26\n\t\tend\n\t\tc += val.min\n\t\tfirst_ind = i unless first_ind\n\tend\nend\n# puts \"#{first_ind} #{pos-1} #{last_ind} #{c}\"\nunless first_ind\n\tc += last_ind - (pos - 1)\nelse\n\tfirst_dist = pos - 1 - first_ind\n\tlast_dist = last_ind - (pos - 1)\n\tc += first_dist + last_dist\n\tc += [first_dist,last_dist].min\nend\n\nputs c"}], "negative_code": [{"source_code": "# Here your code !\n\nN, P_ = gets.split.map(&:to_i)\nP = P_-1\nstr = gets.chomp\nif P>= N/2\n str.reverse!\n P = N - (P+1)\nend\n\ncount = 0\nrightmax = 0\n(P...N/2).each{|p|\n if str[p] != str[N-p-1] then\n rightmax = p - P\n count +=1\n end\n}\nleftmax = 0\n(P-1).downto(0).each{|p|\n if str[p] != str[N-p-1] then\n leftmax = P-p\n count +=1\n end\n}\n# puts \"P=#{P} leftmax=#{leftmax}, rightmax=#{rightmax}, count=#{count}\"\n\nif leftmax < rightmax\n count += leftmax*2 + rightmax\nelse\n count += rightmax*2 + leftmax\nend\nputs count\n\n"}, {"source_code": "# Here your code !\n\nN, P_ = gets.split.map(&:to_i)\nP__ = P_-1\nstr = gets.chomp\nif P__>= N/2\n str.reverse!\n P = N - (P__+1)\nelse\n P = P__\nend\n\ncount = 0\nrightmax = 0\n(P...N/2).each{|p|\n if str[p] != str[N-p-1] then\n rightmax = p - P\n count +=1\n end\n}\nleftmax = 0\n(P-1).downto(0).each{|p|\n if str[p] != str[N-p-1] then\n leftmax = P-p\n count +=1\n end\n}\n# puts \"P=#{P} leftmax=#{leftmax}, rightmax=#{rightmax}, count=#{count}\"\n\nif leftmax == 0\n count += rightmax\nelsif rightmax == 0\n count += leftmax\nelsif leftmax < rightmax\n count += leftmax*2 + rightmax\nelse\n count += rightmax*2 + leftmax\nend\nputs count\n\n"}, {"source_code": "# Here your code !\n\nN, P_ = gets.split.map(&:to_i)\nP__ = P_-1\nstr = gets.chomp\nif P__>= N/2\n str.reverse!\n P = N - (P__+1)\nelse\n P = P__\nend\n\ncount = 0\nrightmax = 0\n(P...N/2).each{|p|\n if str[p] != str[N-p-1] then\n rightmax = p - P\n count +=1\n end\n}\nleftmax = 0\n(P-1).downto(0).each{|p|\n if str[p] != str[N-p-1] then\n leftmax = P-p\n count +=1\n end\n}\n# puts \"P=#{P} leftmax=#{leftmax}, rightmax=#{rightmax}, count=#{count}\"\n\nif leftmax < rightmax\n count += leftmax*2 + rightmax\nelse\n count += rightmax*2 + leftmax\nend\nputs count\n\n"}, {"source_code": "n,pos = gets.split(\" \").map(&:to_i)\narr = gets.chomp\nmid = (n+1) / 2\nunless pos <= mid\n\tpos = n - pos + 1\n\tarr.reverse!\nend\nc = 0\nlast_ind = pos - 1\n((pos - 1)...mid).each do |i|\n\tif arr[i] != arr[n - i - 1]\n\t\ta = arr[i].ord \n\t\tb = arr[n - i - 1].ord\n\t\tval = []\n\t\tif a < b\n\t\t\tval << b - a \n\t\t\tval << a - b + 26\n\t\telse\n\t\t\tval << a - b\n\t\t\tval << b - a + 26\n\t\tend\n\t\tc += val.min\n\t\tlast_ind = i\n\tend\nend\n\nfirst_ind = nil\n(0...(pos-1)).each do |i|\n\tif arr[i] != arr[n - i - 1]\n\t\ta = arr[i].ord \n\t\tb = arr[n - i - 1].ord\n\t\tval = []\n\t\tif a < b\n\t\t\tval << b - a \n\t\t\tval << a - b + 26\n\t\telse\n\t\t\tval << a - b\n\t\t\tval << b - a + 26\n\t\tend\n\t\tc += val.min\n\t\tfirst_ind = i unless first_ind\n\tend\nend\nputs \"#{first_ind} #{pos-1} #{last_ind} #{c}\"\nunless first_ind\n\tc += last_ind - (pos - 1)\nelse\n\tfirst_dist = pos - 1 - first_ind\n\tlast_dist = last_ind - (pos - 1)\n\tc += first_dist + last_dist\n\tc += [first_dist,last_dist].min\nend\n\nputs c"}, {"source_code": "n,pos = gets.split(\" \").map(&:to_i)\narr = gets.chomp\nmid = (n+1) / 2\nunless pos <= mid\n\tpos = n - pos + 1\n\tarr.reverse!\nend\nc = 0\nlast_ind = pos\n((pos - 1)...mid).each do |i|\n\tif arr[i] != arr[n - i - 1]\n\t\ta = arr[i].ord \n\t\tb = arr[n - i - 1].ord\n\t\tval = []\n\t\tif a < b\n\t\t\tval << b - a \n\t\t\tval << a - b + 26\n\t\telse\n\t\t\tval << a - b\n\t\t\tval << b - a + 26\n\t\tend\n\t\tc += val.min\n\t\tlast_ind = i\n\tend\nend\n\nfirst_ind = nil\n(0...(pos-1)).each do |i|\n\tif arr[i] != arr[n - i - 1]\n\t\ta = arr[i].ord \n\t\tb = arr[n - i - 1].ord\n\t\tval = []\n\t\tif a < b\n\t\t\tval << b - a \n\t\t\tval << a - b + 26\n\t\telse\n\t\t\tval << a - b\n\t\t\tval << b - a + 26\n\t\tend\n\t\tc += val.min\n\t\tfirst_ind = i unless first_ind\n\tend\nend\n# puts \"#{first_ind} #{pos} #{last_ind} #{c}\"\nunless first_ind\n\tc += last_ind - pos - 1\t\nelse\n\tfirst_dist = pos - 1 - first_ind\n\tlast_dist = last_ind - (pos - 1)\n\tc += first_dist + last_dist\n\tc += [first_dist,last_dist].min\nend\n\nputs c"}], "src_uid": "ebacd748147b50b20d39b4d8cfde39ec"} {"nl": {"description": "It is a holiday season, and Koala is decorating his house with cool lights! He owns $$$n$$$ lights, all of which flash periodically.After taking a quick glance at them, Koala realizes that each of his lights can be described with two parameters $$$a_i$$$ and $$$b_i$$$. Light with parameters $$$a_i$$$ and $$$b_i$$$ will toggle (on to off, or off to on) every $$$a_i$$$ seconds starting from the $$$b_i$$$-th second. In other words, it will toggle at the moments $$$b_i$$$, $$$b_i + a_i$$$, $$$b_i + 2 \\cdot a_i$$$ and so on.You know for each light whether it's initially on or off and its corresponding parameters $$$a_i$$$ and $$$b_i$$$. Koala is wondering what is the maximum number of lights that will ever be on at the same time. So you need to find that out. Here is a graphic for the first example. ", "input_spec": "The first line contains a single integer $$$n$$$ ($$$1 \\le n \\le 100$$$), the number of lights. The next line contains a string $$$s$$$ of $$$n$$$ characters. The $$$i$$$-th character is \"1\", if the $$$i$$$-th lamp is initially on. Otherwise, $$$i$$$-th character is \"0\". The $$$i$$$-th of the following $$$n$$$ lines contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \\le a_i, b_i \\le 5$$$)  — the parameters of the $$$i$$$-th light.", "output_spec": "Print a single integer — the maximum number of lights that will ever be on at the same time.", "sample_inputs": ["3\n101\n3 3\n3 2\n3 1", "4\n1111\n3 4\n5 2\n3 1\n3 2", "6\n011100\n5 3\n5 5\n2 4\n3 5\n4 2\n1 5"], "sample_outputs": ["2", "4", "6"], "notes": "NoteFor first example, the lamps' states are shown in the picture above. The largest number of simultaneously on lamps is $$$2$$$ (e.g. at the moment $$$2$$$).In the second example, all lights are initially on. So the answer is $$$4$$$."}, "positive_code": [{"source_code": "n = gets.to_i\nlight = gets.chomp\nparam = []\nrest = []\nn.times do |i|\n param << gets.split.map(&:to_i)\n rest[i] = param[i][1]\nend\n\nans = light.count(\"1\")\n200.times do |i|\n n.times do |i|\n rest[i] -= 1\n if rest[i] == 0\n rest[i] = param[i][0]\n light[i] = light[i] == \"1\" ? \"0\" : \"1\"\n end\n end\n cnt = light.count(\"1\")\n ans = cnt if cnt > ans\nend\np ans"}, {"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\nrequire 'prime'\n\nn = inp[0]\nm = gets.chomp.split(\"\")\nt = []\nn.times do\n t << inp\nend\nm.size.times do |i|\n if(m[i] == \"1\")\n m[i] = true\n else\n m[i] = false\n end\nend\nans = m.count(true)\n(0..1000).each do |turn|\n t.size().times do |i|\n t[i][1] -= 1\n if(t[i][1] == 0)\n t[i][1] += t[i][0]\n m[i] = !m[i]\n end\n end\n ans = [ans,m.count(true)].max\nend\np ans"}, {"source_code": "n = gets.to_i\nlights = gets.chomp.each_char.map(&:to_i)\na = []\nb = []\nfor i in 0...n do\n a[i], b[i] = gets.split.map(&:to_i)\nend\n\nons = 0\nlights.each do |x|\n ons += x\nend\n\nmax_ons = ons\nfor t in 1..125 do\n for i in 0...n do\n if t >= b[i] && (t - b[i]) % a[i] == 0\n if lights[i] == 0\n lights[i] = 1\n ons += 1\n else\n lights[i] = 0\n ons -= 1\n end\n end\n end\n max_ons = ons if max_ons < ons\nend\nputs max_ons\n"}], "negative_code": [{"source_code": "n = gets.to_i\nlights = gets.chomp.each_char.map(&:to_i)\na = []\nb = []\nfor i in 0...n do\n a[i], b[i] = gets.split.map(&:to_i)\nend\n\nons = 0\nlights.each do |x|\n ons += x\nend\n\nmax_ons = ons\nfor t in 1..125 do\n for i in 0...n do\n if t >= b[i] && (t - b[i]) % a[i] == 0\n if lights[i] == 0\n lights[i] = 1\n ons += 1\n else\n lights[i] = 0\n ons -= 1\n end\n end\n end\n p ons\n max_ons = ons if max_ons < ons\nend\nputs max_ons\n"}, {"source_code": "n = gets.to_i\nlights = gets.chomp.each_char.map(&:to_i)\na = []\nb = []\nfor i in 0...n do\n a[i], b[i] = gets.split.map(&:to_i)\nend\n\nons = 0\nlights.each do |x|\n ons += x\nend\n\nmax_ons = ons\nfor t in 1..120 do\n for i in 0...n do\n if (t - b[i]) % a[i] == 0\n if lights[i] == 0\n lights[i] = 1\n ons += 1\n else\n lights[i] = 0\n ons -= 1\n end\n end\n end\n max_ons = ons if max_ons < ons\nend\nputs max_ons\n"}, {"source_code": "n = gets.to_i\nlights = gets.chomp.each_char.map(&:to_i)\na = []\nb = []\nfor i in 0...n do\n a[i], b[i] = gets.split.map(&:to_i)\nend\n\nons = 0\nlights.each do |x|\n ons += x\nend\n\nmax_ons = ons\nfor t in 1..(5 + 3 * 4 * 5) do\n for i in 0...n do\n if (t - b[i]) % a[i] == 0\n if lights[i] == 0\n lights[i] = 1\n ons += 1\n else\n lights[i] = 0\n ons -= 1\n end\n end\n end\n max_ons = ons if max_ons < ons\nend\nputs max_ons\n"}, {"source_code": "n = gets.to_i\nlights = gets.chomp.each_char.map(&:to_i)\na = []\nb = []\nfor i in 0...n do\n a[i], b[i] = gets.split.map(&:to_i)\nend\n\nons = 0\nlights.each do |x|\n ons += x\nend\n\nmax_ons = ons\nfor t in 1..120 do\n for i in 0...n do\n if t >= b[i] && (t - b[i]) % a[i] == 0\n if lights[i] == 0\n lights[i] = 1\n ons += 1\n else\n lights[i] = 0\n ons -= 1\n end\n end\n end\n max_ons = ons if max_ons < ons\nend\nputs max_ons\n"}], "src_uid": "2ff789ae0095bb7ff0e747b0d4df59bc"} {"nl": {"description": "This is an interactive problem. In the output section below you will see the information about flushing the output.Bear Limak thinks of some hidden number — an integer from interval [2, 100]. Your task is to say if the hidden number is prime or composite.Integer x > 1 is called prime if it has exactly two distinct divisors, 1 and x. If integer x > 1 is not prime, it's called composite.You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2, 100]. The system will answer \"yes\" if your integer is a divisor of the hidden number. Otherwise, the answer will be \"no\".For example, if the hidden number is 14 then the system will answer \"yes\" only if you print 2, 7 or 14.When you are done asking queries, print \"prime\" or \"composite\" and terminate your program.You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2, 100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct.You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below).", "input_spec": "After each query you should read one string from the input. It will be \"yes\" if the printed integer is a divisor of the hidden number, and \"no\" otherwise.", "output_spec": "Up to 20 times you can ask a query — print an integer from interval [2, 100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input. In any moment you can print the answer \"prime\" or \"composite\" (without the quotes). After that, flush the output and terminate your program. To flush you can use (just after printing an integer and end-of-line): fflush(stdout) in C++; System.out.flush() in Java; stdout.flush() in Python; flush(output) in Pascal; See the documentation for other languages. Hacking. To hack someone, as the input you should print the hidden number — one integer from the interval [2, 100]. Of course, his/her solution won't be able to read the hidden number from the input.", "sample_inputs": ["yes\nno\nyes", "no\nyes\nno\nno\nno"], "sample_outputs": ["2\n80\n5\ncomposite", "58\n59\n78\n78\n2\nprime"], "notes": "NoteThe hidden number in the first query is 30. In a table below you can see a better form of the provided example of the communication process.The hidden number is divisible by both 2 and 5. Thus, it must be composite. Note that it isn't necessary to know the exact value of the hidden number. In this test, the hidden number is 30.59 is a divisor of the hidden number. In the interval [2, 100] there is only one number with this divisor. The hidden number must be 59, which is prime. Note that the answer is known even after the second query and you could print it then and terminate. Though, it isn't forbidden to ask unnecessary queries (unless you exceed the limit of 20 queries)."}, "positive_code": [{"source_code": "#!/usr/bin/ruby\nrequire 'prime'\nSTDOUT.sync=true\nM=100\nr=0\nPrime.each(M/2){|e|\n\tp e\n\tif gets.chomp=='yes'\n\t\tr+=1\n\t\tif e*e<=M\n\t\t\tp e*e\n\t\t\tr+=1 if gets.chomp=='yes'\n\t\tend\n\tend\n}\nputs r>1 ? :composite : :prime"}, {"source_code": "require 'prime'\n\nns = (2..100).to_a\nms = [4, 9, 25, 49]\n(2..50).each do |m|\n ms << m if m.prime?\nend\n\nms.each do |m|\n puts m\n STDOUT.flush \n s = gets.chomp\n if s == 'yes'\n ns.delete_if { |n| n % m != 0 }\n else\n ns.delete_if { |n| n % m == 0 }\n end\nend\nif ns[0].prime?\n puts 'prime'\nelse\n puts 'composite'\nend\nSTDOUT.flush\n\n"}, {"source_code": "try = [2, 3, 4, 5, 7, 9, 11, 13, 17, 19, 23, 25, 29, 31, 37, 41, 43, 47, 49, 81]\nans = []\ntry.each do |x|\n puts x\n $stdout.flush\n ans << gets.chomp\nend\nputs ans.count{|x| x == 'yes'} >= 2 ? 'composite' : 'prime'\n"}, {"source_code": "def convert ans\n\tif ans == 'yes'\n\t\ttrue\n\telsif ans == 'no'\n\t\tfalse\n\tend\nend\n\nSTDOUT.flush\nlist = []\n\nprimes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 4, 9, 25, 49]\nprimes.each do |p|\n\tputs p\n\tSTDOUT.flush\n\t\n\tisYes = convert gets.chomp\n\tif isYes\n\t\tlist << p\n\tend\nend\n\nif list.length > 1\n\tputs 'composite'\n\tSTDOUT.flush\nelse\n\tputs 'prime'\n\tSTDOUT.flush\nend\n\n\n"}, {"source_code": "primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]\nsquares = [4, 9, 25, 49]\ncnt = 0\nprimes.each do |prime|\n\tputs prime\n\tSTDOUT.flush\n\tcnt += (gets.chomp == \"yes\") ? 1 : 0\nend\nsquares.each do |square|\n\tputs square\n\tSTDOUT.flush\n\tcnt += (gets.chomp == \"yes\") ? 2 : 0\nend\n\nputs cnt >= 2 ? \"composite\" : \"prime\"\n"}], "negative_code": [{"source_code": "def convert ans\n\tif ans == 'yes'\n\t\ttrue\n\telsif ans == 'no'\n\t\tfalse\n\tend\nend\n\nprimes = [97, 89, 83, 79, 73, 71, 67, 61, 59, 53]\n\nprimes.each do |p|\n\tputs p\n\tSTDOUT.flush\n\tisYes = convert gets.chomp\n\tif isYes\n\t\tputs 'prime'\n\t\tSTDOUT.flush\n\t\texit\n\tend\nend\n\nputs 2\n\nSTDOUT.flush\nisYes = convert gets.chomp\n\nif isYes\n\tdivisor = [4, 3, 5, 7, 11, 13, 17, 19, 23]\n\tlist = []\n\n\tdivisor.each do |d|\n\t\tputs d\n\n\t\tSTDOUT.flush\n\t\tisYes = convert gets.chomp\n\t\t\n\t\tif isYes\n\t\t\tlist << d\n\t\tend\n\n\t\tif list.length > 0\n\t\t\tputs 'composite'\n\t\t\tSTDOUT.flush\n\t\t\texit\n\t\tend\n\tend\nelse\n\tdivisor = [3, 5, 7, 9, 11, 13]\n\tlist = []\n\n\tdivisor.each do |d|\n\t\tputs d\n\n\t\tSTDOUT.flush\n\t\tisYes = convert gets.chomp\n\t\t\n\t\tif isYes\n\t\t\tlist << d\n\t\tend\n\n\t\tif list.length > 1\n\t\t\tputs 'composite'\n\t\t\tSTDOUT.flush\n\t\t\texit\n\t\tend\n\tend\nend\n\nputs 'prime'\nSTDOUT.flush"}, {"source_code": "def convert ans\n\tif ans == 'yes'\n\t\ttrue\n\telsif ans == 'no'\n\t\tfalse\n\tend\nend\n\nSTDOUT.flush\nprimes = [97, 89, 83, 79, 73, 71, 67, 61, 59, 53]\n\nprimes.each do |p|\n\tputs p\n\tSTDOUT.flush\n\tisYes = convert gets.chomp\n\tif isYes\n\t\tputs 'prime'\n\t\tSTDOUT.flush\n\t\texit\n\tend\nend\n\nputs 2\n\nSTDOUT.flush\nisYes = convert gets.chomp\n\nif isYes\n\tdivisor = [4, 3, 5, 7, 11, 13, 17, 19, 23]\n\tlist = []\n\n\tdivisor.each do |d|\n\t\tputs d\n\n\t\tSTDOUT.flush\n\t\tisYes = convert gets.chomp\n\t\t\n\t\tif isYes\n\t\t\tlist << d\n\t\tend\n\n\t\tif list.length > 0\n\t\t\tputs 'composite'\n\t\t\tSTDOUT.flush\n\t\t\texit\n\t\tend\n\tend\nelse\n\tdivisor = [3, 5, 7, 9, 11, 13, 17, 25, 49]\n\tlist = []\n\n\tdivisor.each do |d|\n\t\tputs d\n\n\t\tSTDOUT.flush\n\t\tisYes = convert gets.chomp\n\t\t\n\t\tif isYes\n\t\t\tlist << d\n\t\tend\n\n\t\tif list.length > 1\n\t\t\tputs 'composite'\n\t\t\tSTDOUT.flush\n\t\t\texit\n\t\tend\n\tend\nend\n\nputs 'prime'\nSTDOUT.flush"}, {"source_code": "def convert ans\n\tif ans == 'yes'\n\t\ttrue\n\telsif ans == 'no'\n\t\tfalse\n\tend\nend\n\nSTDOUT.flush\nprimes = [97, 89, 83, 79, 73, 71, 67, 61, 59, 53]\n\nprimes.each do |p|\n\tputs p\n\tSTDOUT.flush\n\tisYes = convert gets.chomp\n\tif isYes\n\t\tputs 'prime'\n\t\tSTDOUT.flush\n\t\texit\n\tend\nend\n\nputs 2\n\nSTDOUT.flush\nisYes = convert gets.chomp\n\nif isYes\n\tdivisor = [4, 3, 5, 7, 11, 13, 17, 19, 23]\n\tlist = []\n\n\tdivisor.each do |d|\n\t\tputs d\n\n\t\tSTDOUT.flush\n\t\tisYes = convert gets.chomp\n\t\t\n\t\tif isYes\n\t\t\tlist << d\n\t\tend\n\n\t\tif list.length > 0\n\t\t\tputs 'composite'\n\t\t\tSTDOUT.flush\n\t\t\texit\n\t\tend\n\tend\nelse\n\tdivisor = [3, 5, 7, 9, 11, 13, 25, 49]\n\tlist = []\n\n\tdivisor.each do |d|\n\t\tputs d\n\n\t\tSTDOUT.flush\n\t\tisYes = convert gets.chomp\n\t\t\n\t\tif isYes\n\t\t\tlist << d\n\t\tend\n\n\t\tif list.length > 1\n\t\t\tputs 'composite'\n\t\t\tSTDOUT.flush\n\t\t\texit\n\t\tend\n\tend\nend\n\nputs 'prime'\nSTDOUT.flush"}], "src_uid": "8cf479fd47050ba96d21f3d8eb43c8f0"} {"nl": {"description": "Toastman came up with a very easy task. He gives it to Appleman, but Appleman doesn't know how to solve it. Can you help him?Given a n × n checkerboard. Each cell of the board has either character 'x', or character 'o'. Is it true that each cell of the board has even number of adjacent cells with 'o'? Two cells of the board are adjacent if they share a side.", "input_spec": "The first line contains an integer n (1 ≤ n ≤ 100). Then n lines follow containing the description of the checkerboard. Each of them contains n characters (either 'x' or 'o') without spaces.", "output_spec": "Print \"YES\" or \"NO\" (without the quotes) depending on the answer to the problem.", "sample_inputs": ["3\nxxo\nxox\noxx", "4\nxxxo\nxoxo\noxox\nxxxx"], "sample_outputs": ["YES", "NO"], "notes": null}, "positive_code": [{"source_code": "STEP_X = [0, 1, 0, -1]\nSTEP_Y = [1, 0, -1, 0]\n\nn = gets.to_i\nb = Array.new n\nn.times do |i|\n b[i] = gets\nend\nvalid = true\nn.times do |i|\n n.times do |j|\n cnt = 0\n 4.times do |k|\n tx = i + STEP_X[k]\n ty = j + STEP_Y[k]\n if 0 <= tx && tx < n then\n if 0 <= ty && ty < n then\n cnt += 1 if b[tx][ty] == 'o'\n end\n end\n end\n valid = false if cnt % 2 == 1\n end\nend\nif valid then\n puts 'YES'\nelse\n puts 'NO'\nend\n"}, {"source_code": "n=gets.to_i\nboard=[]\ndef solve(board)\n\tlen=board.size\n\tfor i in 0...len\n\t\tfor j in 0...len\n\t\t\ts=0\n\t\t\tif i-1>=0\n\t\t\t\ts+=1 if board[i-1][j]=='o'\n\t\t\tend\n\t\t\tif i+1<=len-1\n\t\t\t\ts+=1 if board[i+1][j]=='o'\n\t\t\tend\n\t\t\tif j-1>=0\n\t\t\t\ts+=1 if board[i][j-1]=='o'\n\t\t\tend\n\t\t\tif j+1<=len-1\n\t\t\t\ts+=1 if board[i][j+1]=='o'\n\t\t\tend\n\t\t\treturn 'NO' if s%2!=0\n\t\tend\n\tend\n\treturn \"YES\"\nend\nn.times do\n board.push(gets.chomp.split('').map(&:to_s))\nend\nputs solve(board)"}, {"source_code": "require 'pp'\n\nn = STDIN.gets.chomp.to_i\nm = []\n\nn.times do |row|\n m.push STDIN.gets.chomp.split(//)\nend\n\nn.times do |i|\n n.times do |j|\n count = 0\n count += 1 if i != 0 && m[i-1][j] == 'o'\n count += 1 if i != n-1 && m[i+1][j] == 'o'\n count += 1 if j != 0 && m[i][j-1] == 'o'\n count += 1 if j != n-1 && m[i][j+1] == 'o'\n\n if count %2 != 0\n puts \"NO\"\n exit(0)\n end\n end\nend\nputs \"YES\"\n\n"}, {"source_code": "n = gets.to_i\na = n.times.map{gets.chomp}\nans = true\nn.times do |i|\n n.times do |j|\n c = 0\n c += 1 if i != 0 && a[i - 1][j] == 'o'\n c += 1 if j != 0 && a[i][j - 1] == 'o'\n c += 1 if i != n - 1 && a[i + 1][j] == 'o'\n c += 1 if j != n - 1 && a[i][j + 1] == 'o'\n ans = false if c % 2 != 0\n end\nend\nputs ans ? \"YES\" : \"NO\""}, {"source_code": "a=gets.chomp.to_i\narray=[]\na.times do\narray<=n || y<0 || y>=n\n\t\treturn 0\n\tend\n\treturn 1 if m[y][x]==\"o\"\n\treturn 0\nend\n\nn=gets.to_i\nm=[]\nn.times{\n\ts=gets.chomp\n\tm<= 0 && arr[i][j - 1] == 'o')\n count += 1 if(i + 1 < n && arr[i + 1][j] == 'o')\n count += 1 if(i - 1 >= 0 && arr[i - 1][j] == 'o')\n \n ans = false if count.odd?\n end \nend\n\nputs (ans ? \"YES\" : \"NO\")"}, {"source_code": "n=gets.to_i\narr=['']\nfor i in 1..n\n arr[i]=gets\nend\narr[n+1]=''\nfor i in 1..n\n for j in 0..n-1\n if ((arr[i-1][j]=='o'?1:0)+(arr[i+1][j]=='o'?1:0)+(arr[i][j-1]=='o'?1:0)+(arr[i][j+1]=='o'?1:0)).odd?\n puts 'NO'\n exit\n end\n end\nend\nputs 'YES'"}, {"source_code": "n=gets.to_i\narr=[]\nfor i in 1..n\n arr[i]=gets\nend\nfor i in 1..n\n for j in 0..n-1\n cnt=0\n if i>1&&arr[i-1][j]=='o'\n cnt+=1\n end\n if i 0) && (a[i - 1][j] == 'o')\n\t\tam += 1 if (i < n - 1) && (a[i + 1][j] == 'o')\n\t\t\n\t\tam += 1 if (j > 0) && (a[i][j - 1] == 'o')\n\t\tam += 1 if (j < n - 1) && (a[i][j + 1] == 'o')\n\t\t\n\t\tif am.odd?\n\t\t\tputs \"NO\"\n\t\t\texit 0\n\t\tend\n\tend\nend\n\nputs \"YES\"\n"}, {"source_code": "class Array\n def get_element(i, j, n)\n if i >= 0 && i < n && j >= 0 && j < n\n self[i][j]\n else\n nil\n end\n end\nend\n\ndef is_good(i, j, s, n)\n t = 0\n t += 1 if s.get_element(i + 1, j, n) == 'o'\n t += 1 if s.get_element(i - 1, j, n) == 'o'\n t += 1 if s.get_element(i, j + 1, n) == 'o'\n t += 1 if s.get_element(i, j - 1, n) == 'o'\n # p [i, j, t]\n t % 2 == 0\nend\ndef main\n n = gets.to_i\n s = n.times.map{gets}\n n.times do |i|\n n.times do |j|\n # p [i, j]\n return :NO unless is_good(i, j, s, n)\n end\n end\n :YES\nend\n\nputs main"}, {"source_code": "t=gets.chomp.to_i\nboard=[]\ndef solve(board)\n len=board.size\n for i in 0...len\n \tfor j in 0...len\n \t\ts=0\n \t if i-1>=0\n s+=1 if board[i-1][j]=='o'\n \t end\t\n \t if i+1<=len-1\n \t \t s+=1 if board[i+1][j]=='o'\n \t end\n \t if j-1>=0\n \t \t s+=1 if board[i][j-1]=='o'\n \t end\n \t if j+1<=len-1\n \t \ts+=1 if board[i][j+1]=='o'\n \t end\n \t return 'NO' if s%2!=0\n \tend\n end\n return \"YES\"\nend\nfor i in 0...t\n board<= 0 && matrix[i-1]\n adj_arr << matrix[i][j+1]\n adj_arr << matrix[i][j-1] if j-1 >= 0\n # puts \"#{adj_arr}\"\n res = false and break unless adj_arr.compact.count(\"o\") % 2 == 0\n end\nend\n\n\nputs res ? \"YES\" : \"NO\""}, {"source_code": "require 'pp'\nn = gets.to_i\narray = Array.new(n)\nn.times do |i|\n array[i] = gets.strip.split //\nend\n\nresult = true\ni = j = 0\n\nSHIFT = [\n [-1, 0],\n [ 0,-1],\n [ 1, 0],\n [ 0, 1]\n]\n\nwhile i < n && result\n j = 0\n while j < n && result\n c = 0\n SHIFT.each do |(k,z)|\n k += i\n z += j\n c += 1 if k >= 0 && z >=0 && k < n && z < n && array[k][z] == 'o'\n end\n result = c%2 == 0\n j += 1\n end\n i += 1\nend\n\nputs result ? \"YES\" : \"NO\"\n"}, {"source_code": "n = gets.to_i\na = n.times.map{gets.chomp}\nans = true\nn.times do |i|\n n.times do |j|\n c = 0\n c += 1 if i != 0 && a[i - 1][j] == 'o'\n c += 1 if j != 0 && a[i][j - 1] == 'o'\n c += 1 if i != n - 1 && a[i + 1][j] == 'o'\n c += 1 if j != n - 1 && a[i][j + 1] == 'o'\n ans = false if c % 2 != 0\n end\nend\nputs ans ? \"YES\" : \"NO\"\n"}, {"source_code": "n = gets.to_i\na = n.times.map{gets.chomp.chars}\nans = true\nn.times do |i|\n n.times do |j|\n c = 0\n c += 1 if i != 0 && a[i - 1][j] == 'o'\n c += 1 if j != 0 && a[i][j - 1] == 'o'\n c += 1 if i != n - 1 && a[i + 1][j] == 'o'\n c += 1 if j != n - 1 && a[i][j + 1] == 'o'\n if c % 2 != 0\n ans = false\n end\n end\nend\nputs ans ? \"YES\" : \"NO\"\n"}, {"source_code": "n = gets.to_i\na = n.times.map{gets.chomp.chars}\nans = true\nn.times do |i|\n break if !ans\n n.times do |j|\n c = 0\n c += 1 if i != 0 && a[i - 1][j] == 'o'\n c += 1 if j != 0 && a[i][j - 1] == 'o'\n c += 1 if i != n - 1 && a[i + 1][j] == 'o'\n c += 1 if j != n - 1 && a[i][j + 1] == 'o'\n if c % 2 != 0\n ans = false\n break\n end\n end\nend\nputs ans ? \"YES\" : \"NO\"\n"}, {"source_code": "a=gets.chomp.to_i\narray=[]\na.times do\narray<0 && arr[i-1][j]=='o')\n count+=1;\n end\n if (i0 && arr[i][j-1]=='o')\n count+=1;\n end\n if(j= 0 && k[1] >= 0 && mx[k[0]] && mx[k[0]][k[1]] == 'o' ? 1 : 0\n }.inject(:+).odd?\n puts 'NO'\n exit\n end\n }\n}\n\nputs 'YES'"}], "negative_code": [{"source_code": "require 'pp'\n\nn = STDIN.gets.chomp.to_i\nm = []\n\nn.times do |row|\n m.push STDIN.gets.chomp.split(//)\nend\n\nn.times do |i|\n n.times do |j|\n count = 0\n count += 1 if i != 0 && m[i-1][j] == 'o'\n count += 1 if i != n-1 && m[i+1][j] == 'o'\n count += 1 if j != 0 && m[i][j-1] == 'o'\n count += 1 if j != n-1 && m[i][j+1] == 'o'\n\n if count %2 != 0\n p \"NO\"\n exit(0)\n end\n end\nend\np \"YES\"\n\n"}, {"source_code": "n = gets.to_i\narr = []\nn.times do\n arr << gets.chomp\nend\narr.each do |x|\n if x.scan(/o/).empty?\n puts \"NO\"\n exit\n end\nend\nputs \"YES\"\n"}, {"source_code": "n=gets.to_i\nok=\"YES\"\nn.times{|i|\n\ts=gets.chomp\n\tn.times{|j|\n\t\tok=\"NO\" if (i+j)%2==1 && s[j]==\"o\"\n\t}\n}\nputs ok\n\n"}, {"source_code": "n=gets.to_i\nok=\"YES\"\nn.times{|i|\n\ts=gets.chomp\n\tif i==0 || i==n-1 then\n\t\tok=\"NO\" if /.o./.match(s)\n\telse\n\t\tok=\"NO\" if /^o|o$/.match(s)\n\tend\n}\nputs ok\n\n"}, {"source_code": "n=gets.to_i\nans=\"NO\"\ns=STDIN.read.split\n\nhit=true\nn.times{|i|\n\thit=false if s[i][i]==\"x\"\n}\nans=\"YES\" if hit\n\nhit=true\nn.times{|i|\n\thit=false if s[i][n-i-1]==\"x\"\n}\nans=\"YES\" if hit\nputs ans\n\n"}, {"source_code": "n = gets.to_i\narray = Array.new(n)\nn.times do |i|\n array[i] = gets.strip.split //\nend\n\nresult = true\ni = j = 0\n\nSHIFT = [\n [-1, 0],\n [ 0,-1],\n [ 1, 0],\n [ 0, 1]\n]\n\nwhile i < n && result\n while j < n && result do\n c = 0\n SHIFT.each do |(k,z)|\n k += i; z += j\n c += 1 if k >= 0 && z >=0 && k < n && z < n && array[k][z] == 'o'\n end\n result = c%2 == 0\n j += 1\n end\n i += 1\nend\n\nputs result ? \"YES\" : \"NO\"\n"}, {"source_code": "require 'pp'\nn = gets.to_i\narray = Array.new(n)\nn.times do |i|\n array[i] = gets.strip.split //\nend\n\n\npp array\nresult = true\ni = j = 0\n\nSHIFT = [\n [-1, 0],\n [ 0,-1],\n [ 1, 0],\n [ 0, 1]\n]\n\nwhile i < n && result\n while j < n && result\n c = 0\n SHIFT.each do |(k,z)|\n k += i; z += j\n c += 1 if k >= 0 && z >=0 && k < n && z < n && array[k][z] == 'o'\n end\n result = c%2 == 0\n j += 1\n end\n i += 1\nend\n\nputs result ? \"YES\" : \"NO\"\n"}, {"source_code": "require 'pp'\nn = gets.to_i\narray = Array.new(n)\nn.times do |i|\n array[i] = gets.strip.split //\nend\n\nresult = true\ni = j = 0\n\nSHIFT = [\n [-1, 0],\n [ 0,-1],\n [ 1, 0],\n [ 0, 1]\n]\n\nwhile i < n && result\n while j < n && result\n c = 0\n SHIFT.each do |(k,z)|\n k += i; z += j\n c += 1 if k >= 0 && z >=0 && k < n && z < n && array[k][z] == 'o'\n end\n result = c%2 == 0\n j += 1\n end\n i += 1\nend\n\nputs result ? \"YES\" : \"NO\"\n"}, {"source_code": "n = gets.to_i\na = n.times.map{gets.chomp.chars}\nans = true\nn.times do |i|\n n.times do |j|\n if a[i][j] == 'x'\n c = 0\n c += 1 if i != 0 && a[i - 1][j] == 'o'\n c += 1 if j != 0 && a[i][j - 1] == 'o'\n c += 1 if i != n - 1 && a[i + 1][j] == 'o'\n c += 1 if j != n - 1 && a[i][j + 1] == 'o'\n if c % 2 != 0\n ans = false\n break\n end\n end\n end\nend\nputs ans ? \"YES\" : \"NO\"\n"}, {"source_code": "n = gets.to_i\na = n.times.map{gets.chomp.chars}\nans = true\nn.times do |i|\n n.times do |j|\n if a[i][j] == 'o'\n c = 0\n c += 1 if i != 0 && a[i - 1][j] == 'x'\n c += 1 if j != 0 && a[i][j - 1] == 'x'\n c += 1 if i != n - 1 && a[i + 1][j] == 'x'\n c += 1 if j != n - 1 && a[i][j + 1] == 'x'\n if c % 2 != 0\n ans = false\n break\n end\n end\n end\nend\nputs ans ? \"YES\" : \"NO\"\n"}, {"source_code": "(mx = (0...gets.to_i).map { |_| gets.chomp.split('') }).each_with_index { |a, i|\n a.each_with_index { |_, j|\n if [[i - 1, j], [i + 1, j], [i, j - 1], [i, j + 1]].map { |k| mx[k[0]] && mx[k[0]][k[1]] == 'o' ? 1 : 0 }.inject(:+).odd?\n puts 'NO'\n exit\n end\n }\n}\n\nputs 'YES'"}], "src_uid": "03fcf7402397b94edd1d1837e429185d"} {"nl": {"description": "You are given an array of positive integers $$$a_1, a_2, \\ldots, a_n$$$.Make the product of all the numbers in the array (that is, $$$a_1 \\cdot a_2 \\cdot \\ldots \\cdot a_n$$$) divisible by $$$2^n$$$.You can perform the following operation as many times as you like: select an arbitrary index $$$i$$$ ($$$1 \\leq i \\leq n$$$) and replace the value $$$a_i$$$ with $$$a_i=a_i \\cdot i$$$. You cannot apply the operation repeatedly to a single index. In other words, all selected values of $$$i$$$ must be different.Find the smallest number of operations you need to perform to make the product of all the elements in the array divisible by $$$2^n$$$. Note that such a set of operations does not always exist.", "input_spec": "The first line of the input contains a single integer $$$t$$$ $$$(1 \\leq t \\leq 10^4$$$) — the number test cases. Then the descriptions of the input data sets follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 2 \\cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains exactly $$$n$$$ integers: $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\leq a_i \\leq 10^9$$$). It is guaranteed that the sum of $$$n$$$ values over all test cases in a test does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, print the least number of operations to make the product of all numbers in the array divisible by $$$2^n$$$. If the answer does not exist, print -1.", "sample_inputs": ["6\n\n1\n\n2\n\n2\n\n3 2\n\n3\n\n10 6 11\n\n4\n\n13 17 1 1\n\n5\n\n1 1 12 1 1\n\n6\n\n20 7 14 18 3 5"], "sample_outputs": ["0\n1\n1\n-1\n2\n1"], "notes": "NoteIn the first test case, the product of all elements is initially $$$2$$$, so no operations needed.In the second test case, the product of elements initially equals $$$6$$$. We can apply the operation for $$$i = 2$$$, and then $$$a_2$$$ becomes $$$2\\cdot2=4$$$, and the product of numbers becomes $$$3\\cdot4=12$$$, and this product of numbers is divided by $$$2^n=2^2=4$$$. In the fourth test case, even if we apply all possible operations, we still cannot make the product of numbers divisible by $$$2^n$$$  — it will be $$$(13\\cdot1)\\cdot(17\\cdot2)\\cdot(1\\cdot3)\\cdot(1\\cdot4)=5304$$$, which does not divide by $$$2^n=2^4=16$$$.In the fifth test case, we can apply operations for $$$i = 2$$$ and $$$i = 4$$$. "}, "positive_code": [{"source_code": "def factors_of_two(num)\n res = 0\n until num.odd?\n res += 1\n num /= 2\n end\n res\nend\n\ndef solve(n, num_array)\n number_of_twos = 0\n\n num_array.each do |num|\n number_of_twos += factors_of_two(num)\n return 0 if number_of_twos >= n\n end\n\n index_factors = (1..num_array.length).map { |num| factors_of_two(num) }\n index_factors.sort!\n\n indexes_modified = 0\n index_factors.reverse_each do |factors|\n number_of_twos += factors\n indexes_modified += 1\n return indexes_modified if number_of_twos >= n\n end\n\n -1\nend\n\n# code for tests:\n\nnumber_of_tests = gets.chomp.to_i\n\nnumber_of_tests.times do\n n = gets.chomp.to_i\n num_array = gets.chomp.split.map(&:to_i)\n\n puts solve(n, num_array)\nend\n"}, {"source_code": "def max(a, b)\r\n a < b ? b : a\r\nend\r\n\r\ndef log2(n)\r\n cnt = 0\r\n while n % 2 == 0\r\n n /= 2\r\n cnt += 1\r\n end\r\n cnt\r\nend\r\n\r\nts = gets.to_i\r\nts.times do\r\n n = gets.to_i\r\n a = gets.split.map(&:to_i)\r\n \r\n rem = max(0, n - a.sum{ |ai| log2(ai) })\r\n\r\n ans = (1..n).map{ |i| log2(i) }.sort.reverse.sum do |i|\r\n if rem != 0 && rem - i >= 0\r\n rem -= i\r\n 1\r\n else\r\n 0\r\n end\r\n end\r\n\r\n puts rem == 0 ? ans : -1\r\nend"}], "negative_code": [{"source_code": "def factors_of_two(num)\n res = 0\n until num.odd?\n res += 1\n num /= 2\n end\n res\nend\n\ndef solve(n, num_array)\n number_of_twos = 0\n\n num_array.each do |num|\n number_of_twos += factors_of_two(num)\n return 0 if number_of_twos >= n\n end\n\n index_factors = (1..num_array.length).map { |num| factors_of_two(num) }\n index_factors.sort\n\n indexes_modified = 0\n index_factors.reverse_each do |factors|\n number_of_twos += factors\n indexes_modified += 1\n return indexes_modified if number_of_twos >= n\n end\n\n -1\nend\n\n# code for tests:\n\nnumber_of_tests = gets.chomp.to_i\n\nnumber_of_tests.times do\n n = gets.chomp.to_i\n num_array = gets.chomp.split.map(&:to_i)\n\n puts solve(n, num_array)\nend\n"}, {"source_code": "def factors_of_two(num)\n res = 0\n until num.odd?\n res += 1\n num /= 2\n end\n res\nend\n\ndef solve(n, num_array)\n number_of_twos = 0\n\n num_array.each do |num|\n number_of_twos += factors_of_two(num)\n return 0 if number_of_twos >= n\n end\n\n index_factors = (1..num_array.length).map { |num| factors_of_two(num) }\n index_factors.sort\n\n indexes_modified = 0\n index_factors.reverse_each do |factors|\n number_of_twos += factors\n indexes_modified += 1\n return indexes_modified if number_of_twos >= n\n end\n\n -1\nend\n\n# # code for tests:\n\n# number_of_tests = gets.chomp.to_i\n\n# number_of_tests.times do\n# n = gets.chomp.to_i\n# num_array = gets.chomp.split.map(&:to_i)\n\n# puts solve(n, num_array)\n# end\n\np solve(2, [3, 2])\n"}, {"source_code": "def factors_of_two(num)\n res = 0\n until num.odd?\n res += 1\n num /= 2\n end\n res\nend\n\ndef solve(n, num_array)\n number_of_twos = 0\n\n num_array.each do |num|\n number_of_twos += factors_of_two(num)\n return 0 if number_of_twos >= n\n end\n\n index_factors = (1..num_array.length - 1).map { |num| factors_of_two(num) }\n index_factors.sort\n\n indexes_modified = 0\n index_factors.reverse_each do |factors|\n number_of_twos += factors\n indexes_modified += 1\n return indexes_modified if number_of_twos >= n\n end\n\n -1\nend\n\n# code for tests:\n\nnumber_of_tests = gets.chomp.to_i\n\nnumber_of_tests.times do\n n = gets.chomp.to_i\n num_array = gets.chomp.split.map(&:to_i)\n\n puts solve(n, num_array)\nend\n"}], "src_uid": "96f0df1c8e014229e5ef773789aa2205"} {"nl": {"description": "You have a knapsack with the capacity of $$$W$$$. There are also $$$n$$$ items, the $$$i$$$-th one has weight $$$w_i$$$. You want to put some of these items into the knapsack in such a way that their total weight $$$C$$$ is at least half of its size, but (obviously) does not exceed it. Formally, $$$C$$$ should satisfy: $$$\\lceil \\frac{W}{2}\\rceil \\le C \\le W$$$. Output the list of items you will put into the knapsack or determine that fulfilling the conditions is impossible. If there are several possible lists of items satisfying the conditions, you can output any. Note that you don't have to maximize the sum of weights of items in the knapsack.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 10^4$$$). Description of the test cases follows. The first line of each test case contains integers $$$n$$$ and $$$W$$$ ($$$1 \\le n \\le 200\\,000$$$, $$$1\\le W \\le 10^{18}$$$). The second line of each test case contains $$$n$$$ integers $$$w_1, w_2, \\dots, w_n$$$ ($$$1 \\le w_i \\le 10^9$$$) — weights of the items. The sum of $$$n$$$ over all test cases does not exceed $$$200\\,000$$$.", "output_spec": "For each test case, if there is no solution, print a single integer $$$-1$$$. If there exists a solution consisting of $$$m$$$ items, print $$$m$$$ in the first line of the output and $$$m$$$ integers $$$j_1$$$, $$$j_2$$$, ..., $$$j_m$$$ ($$$1 \\le j_i \\le n$$$, all $$$j_i$$$ are distinct) in the second line of the output  — indices of the items you would like to pack into the knapsack. If there are several possible lists of items satisfying the conditions, you can output any. Note that you don't have to maximize the sum of weights items in the knapsack.", "sample_inputs": ["3\n1 3\n3\n6 2\n19 8 19 69 9 4\n7 12\n1 1 1 17 1 1 1"], "sample_outputs": ["1\n1\n-1\n6\n1 2 3 5 6 7"], "notes": "NoteIn the first test case, you can take the item of weight $$$3$$$ and fill the knapsack just right.In the second test case, all the items are larger than the knapsack's capacity. Therefore, the answer is $$$-1$$$.In the third test case, you fill the knapsack exactly in half."}, "positive_code": [{"source_code": "t = gets.chomp.to_i\nt.times do\n n, w = gets.chomp.split(' ').map(&:to_i)\n arr = gets.chomp.split(' ').map(&:to_i).zip(1..).sort\n orig_arr = arr\n res = []\n sum = 0\n while sum < (w / 2.0).ceil && arr.any? && sum + arr[0][0] <= w\n res << arr.shift\n sum += res[-1][0]\n end\n if sum >= (w / 2.0).ceil\n puts res.size\n puts res.map{|el| el[1]}.join(' ')\n else\n res = []\n sum = orig_arr.find{|el| el[0] >= (w / 2.0).ceil && el[0] <= w}\n if sum\n puts 1\n puts sum[1]\n else\n puts -1\n end\n end\nend "}], "negative_code": [{"source_code": "t = gets.chomp.to_i\nt.times do\n n, w = gets.chomp.split(' ').map(&:to_i)\n arr = gets.chomp.split(' ').map(&:to_i).zip(1..).sort\n res = []\n sum = 0\n while sum < (w / 2.0).ceil && arr.any? && sum + arr[0][0] <= w\n res << arr.shift\n sum += res[-1][0]\n end\n if sum >= (w / 2.0).ceil\n puts res.map{|el| el[1]}.join(' ')\n else\n puts -1\n end\nend "}, {"source_code": "t = gets.chomp.to_i\nt.times do\n n, w = gets.chomp.split(' ').map(&:to_i)\n arr = gets.chomp.split(' ').map(&:to_i).zip(1..).sort\n res = []\n sum = 0\n while sum < (w / 2.0).ceil && arr.any? && sum + arr[0][0] <= w\n res << arr.shift\n sum += res[-1][0]\n end\n if sum >= (w / 2.0).ceil\n puts res.size\n puts res.map{|el| el[1]}.join(' ')\n else\n puts -1\n end\nend "}], "src_uid": "afe8710473db82f8d53d28dd32646774"} {"nl": {"description": "A permutation p is an ordered group of numbers p1,   p2,   ...,   pn, consisting of n distinct positive integers, each is no more than n. We'll define number n as the length of permutation p1,   p2,   ...,   pn.Simon has a positive integer n and a non-negative integer k, such that 2k ≤ n. Help him find permutation a of length 2n, such that it meets this equation: .", "input_spec": "The first line contains two integers n and k (1 ≤ n ≤ 50000, 0 ≤ 2k ≤ n).", "output_spec": "Print 2n integers a1, a2, ..., a2n — the required permutation a. It is guaranteed that the solution exists. If there are multiple solutions, you can print any of them.", "sample_inputs": ["1 0", "2 1", "4 0"], "sample_outputs": ["1 2", "3 2 1 4", "2 7 4 6 1 3 5 8"], "notes": "NoteRecord |x| represents the absolute value of number x. In the first sample |1 - 2| - |1 - 2| = 0.In the second sample |3 - 2| + |1 - 4| - |3 - 2 + 1 - 4| = 1 + 3 - 2 = 2.In the third sample |2 - 7| + |4 - 6| + |1 - 3| + |5 - 8| - |2 - 7 + 4 - 6 + 1 - 3 + 5 - 8| = 12 - 12 = 0."}, "positive_code": [{"source_code": "n, k = gets.chomp.split.map(&:to_i)\nary = (1..2 * n).to_a\ni = 0\nk.times do\n ary[i], ary[i + 1] = ary[i + 1], ary[i]\n i += 2\nend\nputs ary.join(' ')\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\na = Array.new(2*n) { |i| i+1 }\n\nk.times do |i|\n a[2*i] += 1\n a[2*i+1] -= 1\nend\n\nputs a.join(' ')\n"}, {"source_code": "n,k=gets.chomp.split.map(&:to_i)\nfor i in 1..k\n\tprint \"#{2*i} #{2*i-1} \"\nend\nfor i in (k+1)..n \n\tprint \"#{2*i-1} #{2*i} \"\nend\nputs"}, {"source_code": "n,k = gets.split(\" \").map(&:to_i)\narr = (1..(2 * n)).to_a\n(1..k).each do |i|\n\tl,m = 2 * i - 1,(2 * i) - 2\n\tarr[l],arr[m] = arr[m],arr[l]\nend\nputs arr.join(\" \")"}, {"source_code": "n,k = gets.split.map{ |x| x.to_i }\n\nif k == 0 then\n a = (1..n*2).to_a.reverse\nelse\n a = [1, k+1] | (1..n*2).to_a.reverse\nend\n\na.each do |x| print \"#{x} \" end and puts\n# \n# s = ms = 0\n# \n# a.each_index { |i|\n# if i%2 == 0 then\n# s += a[i] - a[i+1]\n# ms += (a[i]-a[i+1]).abs\n# end\n# }\n# \n# print ms-s.abs, \" \", k.*(2)\n"}, {"source_code": "n,k=gets.split.map(&:to_i)\nprint \"1 #{2*n} \"\nif n>1\n print \"#{2+k} 2 \" if k>0\n i=2\n while i<2*n-1\n i+=1 while k>0 && (i==2+k || i==2)\n a=i\n i+=1\n i+=1 while k>0 && (i==2+k || i==2)\n b=i\n i+=1\n print \"#{a} #{b} \" if a<=2*n-1 && b <=2*n-1\n end\nend\n\n"}, {"source_code": "n,k=gets.split.map(&:to_i)\na=(1..2*n).to_a\nputs a.shift,a.pop\nputs a.delete(2+k),a.shift if !a.empty?&&k>0\nputs a\n"}, {"source_code": "n,k=gets.split.map(&:to_i)\na=(1..2*n).to_a\nputs a.shift,a.pop\nif !a.empty? && k>0\n puts 2+k,a.shift\n a.delete 2+k\nend\nputs a.shift,a.shift until a.empty?\n"}, {"source_code": "n, k = gets.split.map { |x| 2 * x.to_i }\nputs n.downto(1).each_slice(2).map { |x| x[0] <= k ? x.reverse : x } * ' '"}, {"source_code": "line = gets.chomp.split(\" \").map {|e| e.to_i}\nsecond = 1\nfirst = nil\nnums = [second]\nif line[1] != 0\n first = line[1] + 1\n nums = [first, second]\nend\n\nfor i in 2..(line[0] * 2)\n if i != first\n nums.push i\n end\nend\n\nputs nums.join(\" \")\n"}], "negative_code": [{"source_code": "n, k = gets.chomp.split.map(&:to_i)\nary = (1..2 * n).to_a\ni = 0\nk.times do\n ary[i], ary[i + 1] = ary[i + 1], ary[i]\n i += 2\nend\np ary.join(\" \")\n"}, {"source_code": "n,k=gets.split.map(&:to_i)\na=(1..2*n).to_a\nputs a.shift,a.pop\nif !a.empty? && k>0\n puts a.shift,2+k\n a.delete 2+k\nend\nputs a.pop,a.pop until a.empty?\n"}, {"source_code": "n,k=gets.split.map(&:to_i)\na=(1..2*n).to_a\nputs a.shift,a.pop\nif !a.empty? && k>0\n puts 2+k,a.shift\n a.delete 2+k\nend\nputs a.pop,a.pop until a.empty?\n"}], "src_uid": "82de6d4c892f4140e72606386ec2ef59"} {"nl": {"description": "One way to create a task is to learn from life. You can choose some experience in real life, formalize it and then you will get a new task.Let's think about a scene in real life: there are lots of people waiting in front of the elevator, each person wants to go to a certain floor. We can formalize it in the following way. We have n people standing on the first floor, the i-th person wants to go to the fi-th floor. Unfortunately, there is only one elevator and its capacity equal to k (that is at most k people can use it simultaneously). Initially the elevator is located on the first floor. The elevator needs |a - b| seconds to move from the a-th floor to the b-th floor (we don't count the time the people need to get on and off the elevator).What is the minimal number of seconds that is needed to transport all the people to the corresponding floors and then return the elevator to the first floor?", "input_spec": "The first line contains two integers n and k (1 ≤ n, k ≤ 2000) — the number of people and the maximal capacity of the elevator. The next line contains n integers: f1, f2, ..., fn (2 ≤ fi ≤ 2000), where fi denotes the target floor of the i-th person.", "output_spec": "Output a single integer — the minimal time needed to achieve the goal.", "sample_inputs": ["3 2\n2 3 4", "4 2\n50 100 50 100", "10 3\n2 2 2 2 2 2 2 2 2 2"], "sample_outputs": ["8", "296", "8"], "notes": "NoteIn first sample, an optimal solution is: The elevator takes up person #1 and person #2. It goes to the 2nd floor. Both people go out of the elevator. The elevator goes back to the 1st floor. Then the elevator takes up person #3. And it goes to the 2nd floor. It picks up person #2. Then it goes to the 3rd floor. Person #2 goes out. Then it goes to the 4th floor, where person #3 goes out. The elevator goes back to the 1st floor. "}, "positive_code": [{"source_code": "def read_line; gets.chomp.split.map(&:to_i);end\na,b = read_line\ns = 0\nread_line.sort.reverse.each_slice(b){|p| s+=(p[0]-1)*2}\nputs s"}, {"source_code": "#!/usr/bin/ruby\nn,k=gets.split.map(&:to_i)\np gets.split.map(&:to_i).sort.reverse.each_slice(k).reduce(0){|s,e|s+e[0]*2-2}"}, {"source_code": "a,b=0,gets.split.map(&:to_i)\ngets.chomp.split.map(&:to_i).sort.reverse.each_with_index{|x,i| a+=(x-1)*2 if i%b[1]==0}\nputs a"}, {"source_code": "n, k = STDIN.readline.split.map {|s| s.to_i }\ntargets = STDIN.readline.split.map {|s| s.to_i }.sort.reverse\ncost = 0\npos = 0\nwhile pos < n\n\tcost += 2*targets[pos] - 2\n\tpos += k\nend\nputs cost\n"}, {"source_code": "n,k=gets.split.map{|e| e.to_i}\nxs=gets.split.map{|e| e.to_i}.sort.reverse\nans=0\nwhile xs.empty? == false\n\tans+=(xs.take(k).shift-1)*2\n\txs=xs.drop(k)\nend\nputs ans\n"}, {"source_code": "n, k = gets.split.map &:to_i\nf = gets.split.map(&:to_i).sort\n\ndef solve(f, k)\n sum = 0\n cur = []\n while f.size > 0\n if f.size > k\n cur = f[0...k]\n f = f[k...f.size]\n else\n cur = f\n f = []\n end\n \n cur << 1\n #p cur\n \n floor = 1\n cur.each do |newfloor|\n sum += (newfloor - floor).abs\n floor = newfloor\n end\n end\n return sum\nend\n\np [solve(f.dup, k), solve(f.reverse, k)].min"}, {"source_code": "nums = gets.chomp.split\nn = Integer(nums[0])\nk = Integer(nums[1])\nnums2 = gets.chomp.split\nf = []\nfor i in 0...n\n f[i] = Integer(nums2[i])\nend\n\nf.sort!\nm = n - 1\ntotal = 0\nwhile m >= 0 do \n total += 2*(f[m] - 1)\n m -= k\nend\nprint total"}, {"source_code": "nk=gets.chomp.split(\" \").map {|x| x.to_i}\nline=gets.chomp.split(\" \").map {|x| x.to_i}\nline.sort!\nlast=nk[0]\ntimes=0\nwhile last>0\n times+=(line[last-1]-1)*2\n last-=nk[1]\nend\nputs times"}, {"source_code": "n, limit = gets.split.map(&:to_i)\ngoal = gets.split.map(&:to_i).sort\nstep = 0\nposition = 1\nn.times do\n\tlimit.times do\n\t\tstep += (goal[-1] - position).abs\n\t\tposition = goal[-1]\n\t\tgoal.pop\n\t\tbreak if goal == []\n\tend\n\tstep += position - 1\n\tbreak if goal == []\n\tposition = 1\nend\nputs step\n"}, {"source_code": "def read_next_line\n gets.chomp.split.map(&:to_i)\nend\n\n_, k = read_next_line\nsum = 0\nread_next_line.sort.reverse.each_slice(k) { |p|\n sum += (p.first - 1) * 2\n}\nputs sum"}, {"source_code": "n, k = gets.split.map(&:to_i)\nf_list = gets.split.map(&:to_i)\nf_list.sort!.reverse!\nsum = 0\n((f_list.size - 1) / k + 1).times do |i|\n sum += (f_list[i * k, k].max - 1) * 2\nend\nputs sum\n"}], "negative_code": [{"source_code": "nk=gets.chomp.split(\" \").map {|x| x.to_i}\nline=gets.chomp.split(\" \").map {|x| x.to_i}\nline.sort!\nlast=nk[0]\ntimes=0\nwhile last>=0\n times+=(line[last-1]-1)*2\n last-=nk[1]\nend\nputs times"}], "src_uid": "b8d8f0e86ecb600f7559a6aec629946e"} {"nl": {"description": "Nazar, a student of the scientific lyceum of the Kingdom of Kremland, is known for his outstanding mathematical abilities. Today a math teacher gave him a very difficult task.Consider two infinite sets of numbers. The first set consists of odd positive numbers ($$$1, 3, 5, 7, \\ldots$$$), and the second set consists of even positive numbers ($$$2, 4, 6, 8, \\ldots$$$). At the first stage, the teacher writes the first number on the endless blackboard from the first set, in the second stage — the first two numbers from the second set, on the third stage — the next four numbers from the first set, on the fourth — the next eight numbers from the second set and so on. In other words, at each stage, starting from the second, he writes out two times more numbers than at the previous one, and also changes the set from which these numbers are written out to another. The ten first written numbers: $$$1, 2, 4, 3, 5, 7, 9, 6, 8, 10$$$. Let's number the numbers written, starting with one.The task is to find the sum of numbers with numbers from $$$l$$$ to $$$r$$$ for given integers $$$l$$$ and $$$r$$$. The answer may be big, so you need to find the remainder of the division by $$$1000000007$$$ ($$$10^9+7$$$).Nazar thought about this problem for a long time, but didn't come up with a solution. Help him solve this problem.", "input_spec": "The first line contains two integers $$$l$$$ and $$$r$$$ ($$$1 \\leq l \\leq r \\leq 10^{18}$$$) — the range in which you need to find the sum.", "output_spec": "Print a single integer — the answer modulo $$$1000000007$$$ ($$$10^9+7$$$).", "sample_inputs": ["1 3", "5 14", "88005553535 99999999999"], "sample_outputs": ["7", "105", "761141116"], "notes": "NoteIn the first example, the answer is the sum of the first three numbers written out ($$$1 + 2 + 4 = 7$$$).In the second example, the numbers with numbers from $$$5$$$ to $$$14$$$: $$$5, 7, 9, 6, 8, 10, 12, 14, 16, 18$$$. Their sum is $$$105$$$."}, "positive_code": [{"source_code": "require 'scanf'\nclass T\n attr_accessor :lb\n attr_accessor :le\n attr_accessor :vb\n attr_accessor :ve\n attr_accessor :val\n def initialize(a1,a2,a3,a4,a5)\n @lb,@le,@vb,@ve,@val=a1,a2,a3,a4,a5\n end\nend\ndt=[]\n$shit=1000000007\ndt[1]=T.new(1,1,1,1,1)\ndt[2]=T.new(2,3,2,4,6)\ndef size(a1,a2,a3)\n return (((a1+a2)*a3)/2)%$shit\nend\nfor i in 3..100\n dt[i]=T.new(dt[i-1].le+1,2**i-1,dt[i-2].ve+2,dt[i-2].ve+2**i,1)\n dt[i].val=size(dt[i].vb,dt[i].ve,dt[i].le-dt[i].lb+1)\nend\na,b=scanf(\"%d%d\")\na-=1\nl,r=0,0\nfor i in 1..100\n if dt[i].le= m\n res << m\n else \n res << c - (m-c)/(k-1) - ( ((m-c)%(k-1) > 0) ? 1 : 0)\n end \nend\n\nres.each { |r| p r }\n"}, {"source_code": "gets.to_i.times do\n n, m, k = gets.split.map &:to_i\n x = [n/k, m].min\n y = (m - x + k - 2)/(k - 1)\n puts x - y\nend\n"}, {"source_code": "gets.to_i.times do\n n, m, k = gets.split(' ').map(&:to_i)\n\n max_j = [m, n/k].min\n\n max_j -= (m - max_j).div(k-1) + ((m - max_j).modulo(k-1).zero? ? 0 : 1)\n\n puts max_j\nend\n"}], "negative_code": [{"source_code": "t = gets.to_i\n\n1.upto t do\n n, m, k = gets.split.map(&:to_i)\n if m == 0\n puts 0\n else\n if (n/k == 1)\n if m == 1\n puts 1\n else\n puts 0\n end\n else\n if (m > n/k)\n z = n/k\n y = m-z\n if y % (k-1) == 0\n puts (z - y/(k+1))\n else\n puts (z - (y/(k-1)+1))\n end\n else\n puts m\n end\n end\n end\nend"}], "src_uid": "6324ca46b6f072f8952d2619cb4f73e6"} {"nl": {"description": "Tanya has $$$n$$$ candies numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th candy has the weight $$$a_i$$$.She plans to eat exactly $$$n-1$$$ candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day.Your task is to find the number of such candies $$$i$$$ (let's call these candies good) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one.For example, $$$n=4$$$ and weights are $$$[1, 4, 3, 3]$$$. Consider all possible cases to give a candy to dad: Tanya gives the $$$1$$$-st candy to dad ($$$a_1=1$$$), the remaining candies are $$$[4, 3, 3]$$$. She will eat $$$a_2=4$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$4+3=7$$$ and in even days she will eat $$$3$$$. Since $$$7 \\ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$2$$$-nd candy to dad ($$$a_2=4$$$), the remaining candies are $$$[1, 3, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$3$$$. Since $$$4 \\ne 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$3$$$-rd candy to dad ($$$a_3=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). Tanya gives the $$$4$$$-th candy to dad ($$$a_4=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_3=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). In total there $$$2$$$ cases which should counted (these candies are good), so the answer is $$$2$$$.", "input_spec": "The first line of the input contains one integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) — the number of candies. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^4$$$), where $$$a_i$$$ is the weight of the $$$i$$$-th candy.", "output_spec": "Print one integer — the number of such candies $$$i$$$ (good candies) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days.", "sample_inputs": ["7\n5 5 4 5 5 5 6", "8\n4 8 8 7 8 4 4 5", "9\n2 3 4 2 2 3 2 2 4"], "sample_outputs": ["2", "2", "3"], "notes": "NoteIn the first example indices of good candies are $$$[1, 2]$$$.In the second example indices of good candies are $$$[2, 3]$$$.In the third example indices of good candies are $$$[4, 5, 9]$$$."}, "positive_code": [{"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\ns1 = (1..n + 2).map { [0, 0] }\ns2 = (1..n + 2).map { [0, 0] }\na.each_with_index do |v, i|\n\ts1[i][i % 2] = s1[i - 1][i % 2] + v\n\ts1[i][(i + 1) % 2] = s1[i - 1][(i + 1) % 2]\nend\na.each_with_index.reverse_each do |v, i|\n\ts2[i][i % 2] = s2[i + 1][i % 2] + v\n\ts2[i][(i + 1) % 2] = s2[i + 1][(i + 1) % 2]\nend\np (0...n).count { |i|\n\t\ts1[i - 1][i % 2] + s2[i + 1][(i + 1) % 2] ==\n\t\t\ts1[i - 1][(i + 1) % 2] + s2[i + 1][i % 2]\n }\n"}, {"source_code": "DBG = true\nn = gets.to_i\na = gets.split.map{|z| z.to_i}\nso = Array.new(n,0)\nse = Array.new(n,0)\nse[0] = a[0]\nfor i in 1...n\n so[i] = so[i-1] + (i%2 == 1 ? a[i] : 0)\n se[i] = se[i-1] + (i%2 == 0 ? a[i] : 0)\nend\nsum = (so[n-1] - so[0] == se[n-1] - se[0]) ? 1 : 0\nsum += 1 if so[n-2] == se[n-2]\nfor i in 1..(n-2)\n sum += 1 if so[n-1] - so[i] + se[i-1] ==\n se[n-1] - se[i] + so[i-1]\nend\nputs \"#{sum}\"\n"}, {"source_code": "gets\nq = gets.split.map(&:to_i)\n\na, b, c, d, s = 0, 0, 0, 0, 0\n\nq.each.with_index do |e, i|\n c += e if i.even?\n d += e if i.odd?\nend\n\nq.each.with_index do |e, i|\n c -= e if i.even?\n d -= e if i.odd?\n\n s += 1 if a + c == b + d\n\n b += e if i.even?\n a += e if i.odd?\nend\n\nputs s"}, {"source_code": "def good(sweets)\n count = 0\n evens = 0\n odds = 0\n\n sweets.each_with_index do |x, i|\n if i.even? then evens += x else odds += x end\n end\n\n sweets.each_with_index do |x, i|\n if i.even? then evens -= x else odds -= x end\n\n count += 1 if evens == odds\n\n if i.even? then odds += x else evens += x end\n end\n count\nend\n\ngets\nsweets = gets.chomp.split.map(&:to_i)\nputs(good(sweets))\n"}], "negative_code": [], "src_uid": "dcc380c544225c8cadf0df8d8b6ffb4d"} {"nl": {"description": "Burenka is about to watch the most interesting sporting event of the year — a fighting tournament organized by her friend Tonya.$$$n$$$ athletes participate in the tournament, numbered from $$$1$$$ to $$$n$$$. Burenka determined the strength of the $$$i$$$-th athlete as an integer $$$a_i$$$, where $$$1 \\leq a_i \\leq n$$$. All the strength values are different, that is, the array $$$a$$$ is a permutation of length $$$n$$$. We know that in a fight, if $$$a_i > a_j$$$, then the $$$i$$$-th participant always wins the $$$j$$$-th.The tournament goes like this: initially, all $$$n$$$ athletes line up in ascending order of their ids, and then there are infinitely many fighting rounds. In each round there is exactly one fight: the first two people in line come out and fight. The winner goes back to the front of the line, and the loser goes to the back.Burenka decided to ask Tonya $$$q$$$ questions. In each question, Burenka asks how many victories the $$$i$$$-th participant gets in the first $$$k$$$ rounds of the competition for some given numbers $$$i$$$ and $$$k$$$. Tonya is not very good at analytics, so he asks you to help him answer all the questions.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$q$$$ ($$$2 \\leq n \\leq 10^5$$$, $$$1 \\leq q \\leq 10^5$$$) — the number of tournament participants and the number of questions. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\leq a_i \\leq n$$$) — the array $$$a$$$, which is a permutation. The next $$$q$$$ lines of a test case contain questions. Each line contains two integers $$$i$$$ and $$$k$$$ ($$$1 \\leq i \\leq n$$$, $$$1 \\leq k \\leq 10^9$$$) — the number of the participant and the number of rounds. It is guaranteed that the sum of $$$n$$$ and the sum of $$$q$$$ over all test cases do not exceed $$$10^5$$$.", "output_spec": "For each Burenka's question, print a single line containing one integer — the answer to the question.", "sample_inputs": ["3\n\n3 1\n\n3 1 2\n\n1 2\n\n4 2\n\n1 3 4 2\n\n4 5\n\n3 2\n\n5 2\n\n1 2 3 5 4\n\n5 1000000000\n\n4 6"], "sample_outputs": ["2\n0\n1\n0\n4"], "notes": "NoteIn the first test case, the first numbered athlete has the strength of $$$3$$$, in the first round he will defeat the athlete with the number $$$2$$$ and the strength of $$$1$$$, and in the second round, the athlete with the number $$$3$$$ and the strength of $$$2$$$.In the second test case, we list the strengths of the athletes fighting in the first $$$5$$$ fights: $$$1$$$ and $$$3$$$, $$$3$$$ and $$$4$$$, $$$4$$$ and $$$2$$$, $$$4$$$ and $$$1$$$, $$$4$$$ and $$$3$$$. The participant with the number $$$4$$$ in the first $$$5$$$ rounds won $$$0$$$ times (his strength is $$$2$$$). The participant with the number $$$3$$$ has a strength of $$$4$$$ and won $$$1$$$ time in the first two fights by fighting $$$1$$$ time."}, "positive_code": [{"source_code": "main = -> {\r\n t = gets.to_i\r\n t.times do\r\n n, q = gets.split.map(&:to_i)\r\n a = gets.split.map { _1.to_i - 1 }\r\n car = a[0]\r\n cdr = a[1..]\r\n victories = Array.new(n) { [] }\r\n rounds_before_fix = 0\r\n while car != n - 1\r\n if car > cdr[0]\r\n cdr << cdr.shift\r\n else\r\n cdr << car\r\n car = cdr.shift\r\n end\r\n victories[car] << rounds_before_fix\r\n rounds_before_fix += 1\r\n end\r\n q.times do\r\n i, k = gets.split.map(&:to_i)\r\n i = a[i - 1]\r\n vs = victories[i]\r\n ans = vs.bsearch_index { _1 > k - 1 } || vs.size\r\n if i == n - 1\r\n ans += [k - rounds_before_fix, 0].max\r\n end\r\n puts ans\r\n end\r\n end\r\n}\r\n\r\nmain.call"}], "negative_code": [], "src_uid": "7372994c95acc3b999dd9657abd35a24"} {"nl": {"description": "Recently a dog was bought for Polycarp. The dog's name is Cormen. Now Polycarp has a lot of troubles. For example, Cormen likes going for a walk. Empirically Polycarp learned that the dog needs at least k walks for any two consecutive days in order to feel good. For example, if k = 5 and yesterday Polycarp went for a walk with Cormen 2 times, today he has to go for a walk at least 3 times. Polycarp analysed all his affairs over the next n days and made a sequence of n integers a1, a2, ..., an, where ai is the number of times Polycarp will walk with the dog on the i-th day while doing all his affairs (for example, he has to go to a shop, throw out the trash, etc.).Help Polycarp determine the minimum number of walks he needs to do additionaly in the next n days so that Cormen will feel good during all the n days. You can assume that on the day before the first day and on the day after the n-th day Polycarp will go for a walk with Cormen exactly k times. Write a program that will find the minumum number of additional walks and the appropriate schedule — the sequence of integers b1, b2, ..., bn (bi ≥ ai), where bi means the total number of walks with the dog on the i-th day.", "input_spec": "The first line contains two integers n and k (1 ≤ n, k ≤ 500) — the number of days and the minimum number of walks with Cormen for any two consecutive days. The second line contains integers a1, a2, ..., an (0 ≤ ai ≤ 500) — the number of walks with Cormen on the i-th day which Polycarp has already planned. ", "output_spec": "In the first line print the smallest number of additional walks that Polycarp should do during the next n days so that Cormen will feel good during all days. In the second line print n integers b1, b2, ..., bn, where bi — the total number of walks on the i-th day according to the found solutions (ai ≤ bi for all i from 1 to n). If there are multiple solutions, print any of them. ", "sample_inputs": ["3 5\n2 0 1", "3 1\n0 0 0", "4 6\n2 4 3 5"], "sample_outputs": ["4\n2 3 2", "1\n0 1 0", "0\n2 4 3 5"], "notes": null}, "positive_code": [{"source_code": "n,k,*a=$<.read.split.map(&:to_i)\nb=a[s=0]\n(1...n).each{|i|\nif b+a[i]1\n\td=x[0]+x[1]\n\tif k>d then\n\t\tx[1]+=(k-d)\n\t\tsum+=(k-d)\n\tend\n\tarr<0&&(r+=t;e+=t));b=e}\np r\nputs A*' '"}, {"source_code": "n, k=gets.split.map &:to_i\na=gets.split.map &:to_i\n\nres=0\nb=[a[0]]\nfor i in 1..n-1\n t=k-a[i]-b[i-1]\n b[i]=a[i]\n if t>0\n res+=t\n b[i]+=t\n end\nend\nputs res\nputs b*' '\n"}, {"source_code": "class Advisor\n def self.get_advice(src)\n @@norm_work_cnt=src[0][1]\n src[1].unshift @@norm_work_cnt\n walks=src[1].map {|x| x=*x}.each_cons(2).to_a.each {|x| x[1][0]= get_walks_needed(x[0][0],x[1][0])}.\n map {|x| x=x[1]}.flatten\n src[1].shift\n result = [] << walks.zip(src[1]).map {|x,y| x-y}.reduce(:+) << walks\n print_result result\n result\n end\n def self.get_walks_needed(walks_prev,walks_current)\n tmp = @@norm_work_cnt-walks_prev-walks_current\n (tmp>=0)?(tmp+walks_current) : walks_current\n end\n def self.print_result(res)\n puts res[0],res[1].join(' ')\n end\nend\n\nif __FILE__ == $0\n src = $stdin.each_line.map {|x| x.split.map {|x| x=(x.to_i)}}\n Advisor.get_advice(src)\nend"}, {"source_code": "n,k = gets.split(' ').map {|e| e.to_i}\narr = gets.split(' ').map {|e| e.to_i}\ni = 1\ntmp = 0\nwhile i < n\n\tif arr[i]+arr[i-1] (a[i - 1] + a[i]) ? k - (a[i - 1] + a[i]) : 0\n ad += dif\n a[i] += dif\nend\nputs ad\nputs a.join \" \"\n"}, {"source_code": "def solution(arr, k)\n counter = 0\n for i in 0...arr.size-1 do\n add = k - arr[i+1] - arr[i]\n if add > 0\n arr[i+1] += add \n counter += add\n end\n end\n puts counter\n puts arr\nend\n\n\n_, k = gets.split.map{ |i| i.to_i }\narr = gets.split.map(&:to_i)\n\nsolution(arr, k)\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\nans = 0\n(n - 1).times do |i|\n c = k - a[i] - a[i + 1]\n if c > 0\n ans += c\n a[i + 1] += c\n end\nend\nputs ans, a.join(' ')\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\nans = 0\n(n - 1).times do |i|\n if a[i] + a[i + 1] < k\n c = k - a[i] - a[i + 1]\n ans += c\n a[i + 1] += c\n end\nend\nputs ans, a.join(' ')\n"}, {"source_code": "n,k=gets.split(\" \").map(&:to_i)\na=gets.split(\" \").map(&:to_i)\nb=[a[0]]\ndelta=0\nfor i in 1...n\n\tb<<[a[i],k-b[i-1]].max\n\tif k-b[i-1]>a[i]\n\t\tdelta+=k-b[i-1]-a[i]\n\tend\nend\nputs delta\nputs b.join(\" \")\n\n\t"}, {"source_code": "n, k = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\n\ntot = 0\nfor i in 0 ... n - 1\n if a[i] + a[i + 1] < k\n add = k - a[i] - a[i + 1]\n a[i + 1] += add\n tot += add\n end\nend\nputs tot\nputs a.join(' ')\n"}], "negative_code": [{"source_code": "n, k = gets.chomp.split.map(&:to_i)\na = gets.chomp.split.map(&:to_i)\n\n(0...n).each_cons(2).each do |i, j|\n if a[i] + a[j] < k\n a[j] = k - a[i]\n end\nend\nputs a.join(\" \")\n"}, {"source_code": "n,k=gets.split.map{|e| e.to_i}\nx=gets.split.map{|e| e.to_i}\narr=[]\nsum=0\nwhile x.size>1\n\td=x[0]+x[1]\n\tif k>d then\n\t\tx[1]+=(k-d)\n\t\tsum+=(k-d)\n\tend\n\tarr<1 then\n\tarr<=k then\n\t\tarr< = open('output.txt', 'w')\nend\n\nclass RNG\n def initialize(x, a, b, c)\n @x = x\n @f = ->(x){(x * a + b) % c}\n end\n\n def next\n @x = @f[@x]\n end\nend\n\nn = gets.to_i\nr = RNG.new(*gets.split.map(&:to_i))\na = Array.new(n){\n x, y = gets.split\n [y.to_i, x]\n}\nn /= 4\na = a.sort.reverse.each_slice(n).to_a\ng = 'Group A:'\nn.downto(1){|m|\n puts g\n g.succ!\n a.each{|b|\n t = b[r.next % m]\n puts t[1]\n b.delete(t)\n }\n}\n\n"}, {"source_code": "if ENV['USER'] != 'watashi'\n $stdin = open('input.txt')\n $> = open('output.txt', 'w')\nend\n\nclass RNG\n def initialize(x, a, b, c)\n @x = x\n @f = ->(x){(x * a + b) % c}\n end\n\n def next\n @x = @f[@x]\n end\nend\n\nn = gets.to_i\nr = RNG.new(*gets.split.map(&:to_i))\na = Array.new(n){\n x, y = gets.split\n [y.to_i, x]\n}\nn /= 4\na = a.sort.reverse.each_slice(n).to_a\ng = 'Group A:'\nn.downto(1){|m|\n puts g\n g.succ!\n a.each{|b|\n t = b[r.next % m]\n puts t[1]\n b.delete(t)\n }\n}\n\n"}, {"source_code": "if ENV['USER'] != 'watashi'\n $stdin = open('input.txt')\n $> = open('output.txt', 'w')\nend\n\nclass RNG\n def initialize(x, a, b, c)\n @x = x\n @f = ->(x){(x * a + b) % c}\n end\n\n def next\n @x = @f[@x]\n end\nend\n\nn = gets.to_i\nr = RNG.new(*gets.split.map(&:to_i))\na = Array.new(n){\n x, y = gets.split\n [y.to_i, x]\n}\nn /= 4\na = a.sort.reverse.each_slice(n).to_a\ng = 'Group A:'\nn.downto(1){|m|\n puts g\n g.succ!\n a.each{|b|\n t = b[r.next % m]\n puts t[1]\n b.delete(t)\n }\n}\n"}, {"source_code": "if ENV['USER'] != 'watashi'\n $stdin = open('input.txt')\n $> = open('output.txt', 'w')\nend\n\nclass RNG\n def initialize(x, a, b, c)\n @x = x\n @f = ->(x){(x * a + b) % c}\n end\n\n def next\n @x = @f[@x]\n end\nend\n\nn = gets.to_i\nr = RNG.new(*gets.split.map(&:to_i))\na = Array.new(n){\n x, y = gets.split\n [y.to_i, x]\n}\nn /= 4\na = a.sort.reverse.each_slice(n).to_a\ng = 'Group A:'\nn.downto(1){|m|\n puts g\n g.succ!\n a.each{|b|\n t = b[r.next % m]\n puts t[1]\n b.delete(t)\n }\n}\n\n"}, {"source_code": "input = File.open(\"input.txt\", \"r\")\noutput = File.open(\"output.txt\", \"w\")\n\nn = input.gets.to_i\nx, a, b, c = input.gets.split.map(&:to_i)\n\nteam = Array.new(n)\nfor i in 0...n\n team[i] = input.gets.split\nend\n\nteam.sort! {|x, y| y[1].to_i <=> x[1].to_i}\n\nsep = n/4\nfor i in 0...n/4 do\n output.puts \"Group #{('A'.ord+i).chr}:\"\n x = (x*a + b)%c\n t1 = x%sep\n output.puts team[t1][0]\n \n x = (x*a + b)%c\n t2 = sep + x%sep\n output.puts team[t2][0]\n \n x = (x*a + b)%c\n t3 = sep*2 + x%sep\n output.puts team[t3][0]\n \n x = (x*a + b)%c\n t4 = sep*3 + x%sep\n output.puts team[t4][0]\n \n team.delete_at(t1)\n team.delete_at(t2-1)\n team.delete_at(t3-2)\n team.delete_at(t4-3)\n \n sep -= 1\nend\n\ninput.close\noutput.close"}, {"source_code": "if ENV['USER'] != 'watashi'\n $stdin = open('input.txt')\n $> = open('output.txt', 'w')\nend\n\nclass RNG\n def initialize(x, a, b, c)\n @x = x\n @f = ->(x){(x * a + b) % c}\n end\n\n def next\n @x = @f[@x]\n end\nend\n\nn = gets.to_i\nr = RNG.new(*gets.split.map(&:to_i))\na = Array.new(n){\n x, y = gets.split\n [y.to_i, x]\n}\nn /= 4\na = a.sort.reverse.each_slice(n).to_a\ng = 'Group A:'\nn.downto(1){|m|\n puts g\n g.succ!\n a.each{|b|\n t = b[r.next % m]\n puts t[1]\n b.delete(t)\n }\n}\n\n"}, {"source_code": "if ENV['USER'] != 'watashi'\n $stdin = open('input.txt')\n $> = open('output.txt', 'w')\nend\n\nclass RNG\n def initialize(x, a, b, c)\n @x = x\n @f = ->(x){(x * a + b) % c}\n end\n\n def next\n @x = @f[@x]\n end\nend\n\nn = gets.to_i\nr = RNG.new(*gets.split.map(&:to_i))\na = Array.new(n){\n x, y = gets.split\n [y.to_i, x]\n}\nn /= 4\na = a.sort.reverse.each_slice(n).to_a\ng = 'Group A:'\nn.downto(1){|m|\n puts g\n g.succ!\n a.each{|b|\n t = b[r.next % m]\n puts t[1]\n b.delete(t)\n }\n}\n\n"}, {"source_code": "if ENV['USER'] != 'watashi'\n $stdin = open('input.txt')\n $> = open('output.txt', 'w')\nend\n\nclass RNG\n def initialize(x, a, b, c)\n @x = x\n @f = ->(x){(x * a + b) % c}\n end\n\n def next\n @x = @f[@x]\n end\nend\n\nn = gets.to_i\nr = RNG.new(*gets.split.map(&:to_i))\na = Array.new(n){\n x, y = gets.split\n [y.to_i, x]\n}\nn /= 4\na = a.sort.reverse.each_slice(n).to_a\ng = 'Group A:'\nn.downto(1){|m|\n puts g\n g.succ!\n a.each{|b|\n t = b[r.next % m]\n puts t[1]\n b.delete(t)\n }\n}\n\n"}, {"source_code": "if ENV['USER'] != 'watashi'\n $stdin = open('input.txt')\n $> = open('output.txt', 'w')\nend\n class RNG\n def initialize(x, a, b, c)\n @x = x\n @f = ->(x){(x * a + b) % c}\n end\n def next\n @x = @f[@x]\n end\nend\n n = gets.to_i\nr = RNG.new(*gets.split.map(&:to_i))\na = Array.new(n){\n x, y = gets.split\n [y.to_i, x]\n}\nn /= 4\na = a.sort.reverse.each_slice(n).to_a\ng = 'Group A:'\nn.downto(1){|m|\n puts g\n g.succ!\n a.each{|b|\n t = b[r.next % m]\n puts t[1]\n b.delete(t)\n }\n}"}, {"source_code": "if ENV['USER'] != 'watashi'\n $stdin = open('input.txt')\n $> = open('output.txt', 'w')\nend\n\nclass RNG\n def initialize(x, a, b, c)\n @x = x\n @f = ->(x){(x * a + b) % c}\n end\n\n def next\n @x = @f[@x]\n end\nend\n\nn = gets.to_i\nr = RNG.new(*gets.split.map(&:to_i))\na = Array.new(n){\n x, y = gets.split\n [y.to_i, x]\n}\nn /= 4\na = a.sort.reverse.each_slice(n).to_a\ng = 'Group A:'\nn.downto(1){|m|\n puts g\n g.succ!\n a.each{|b|\n t = b[r.next % m]\n puts t[1]\n b.delete(t)\n }\n}\n\n"}, {"source_code": "if ENV['USER'] != 'watashi'\n $stdin = open('input.txt')\n $> = open('output.txt', 'w')\nend\n\nclass RNG\n def initialize(x, a, b, c)\n @x = x\n @f = ->(x){(x * a + b) % c}\n end\n\n def next\n @x = @f[@x]\n end\nend\n\nn = gets.to_i\nr = RNG.new(*gets.split.map(&:to_i))\na = Array.new(n){\n x, y = gets.split\n [y.to_i, x]\n}\nn /= 4\na = a.sort.reverse.each_slice(n).to_a\ng = 'Group A:'\nn.downto(1){|m|\n puts g\n g.succ!\n a.each{|b|\n t = b[r.next % m]\n puts t[1]\n b.delete(t)\n }\n}\n\n"}], "negative_code": [], "src_uid": "349b116f40dcf3884d4c8208a513ee23"} {"nl": {"description": "Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\\texttt{A}$$$ except for the last character which is $$$\\texttt{B}$$$. The good strings are $$$\\texttt{AB},\\texttt{AAB},\\texttt{AAAB},\\ldots$$$. Note that $$$\\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times: Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?", "input_spec": "Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single string $$$s_2$$$ ($$$1 \\leq |s_2| \\leq 2 \\cdot 10^5$$$). It is guaranteed that $$$s_2$$$ consists of only the characters $$$\\texttt{A}$$$ and $$$\\texttt{B}$$$. It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, print \"YES\" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and \"NO\" (without quotes) otherwise. You can output \"YES\" and \"NO\" in any case (for example, strings \"yEs\", \"yes\" and \"Yes\" will be recognized as a positive response).", "sample_inputs": ["4\n\nAABAB\n\nABB\n\nAAAAAAAAB\n\nA"], "sample_outputs": ["YES\nNO\nYES\nNO"], "notes": "NoteIn the first test case, we transform $$$s_1$$$ as such: $$$\\varnothing \\to \\color{red}{\\texttt{AAB}} \\to \\texttt{A}\\color{red}{\\texttt{AB}}\\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\\varnothing \\to \\color{red}{\\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n a = gets.chomp.split('')\r\n flag = (a[a.size-1] == 'B')\r\n bcount = 0\r\n acount = 0\r\n 0.upto(a.size-1) do |i|\r\n if (a[i]=='B') then\r\n bcount += 1\r\n else\r\n acount += 1\r\n end\r\n flag = false if bcount>acount\r\n end\r\n puts ((bcount > 0) && flag) ? \"YES\" : \"NO\"\r\nend"}, {"source_code": "gets.to_i.times do\r\n s = gets.chomp\r\n as = 0\r\n s.chars.each do |ch|\r\n case ch\r\n when \"A\" then as += 1\r\n when \"B\" then as -= 1\r\n end\r\n break if as < 0\r\n end\r\n ok = as >= 0 && s[-1] == \"B\"\r\n puts ok ? \"YES\" : \"NO\"\r\nend"}, {"source_code": "gets.to_i.times do\r\n s = gets.chomp\r\n as = 0\r\n s.chars.each do |ch|\r\n case ch\r\n when \"A\" then as += 1\r\n when \"B\" then as -= 1\r\n end\r\n break if as < 0\r\n end\r\n ok = as >= 0 && s[-1] == \"B\"\r\n puts ok ? \"YES\" : \"NO\"\r\nend\r\n"}], "negative_code": [{"source_code": "gets.to_i.times do\r\n a = gets.chomp.split('')\r\n flag = true\r\n bcount = 0\r\n acount = 0\r\n 0.upto(a.size-1) do |i|\r\n if (a[i]=='B') then\r\n bcount += 1\r\n if (bcount>acount) then\r\n flag = false\r\n end\r\n else\r\n acount += 1\r\n end\r\n end\r\n puts ((bcount > 0 ) && flag) ? \"YES\":\"NO\"\r\nend"}, {"source_code": "gets.to_i.times do\r\n a = gets.chomp.split('')\r\n flag = true\r\n bcount = 0\r\n acount = 0\r\n 0.upto(a.size-1) do |i|\r\n if (a[i]=='B') then\r\n bcount += 1\r\n if (bcount>acount)\r\n flag = false\r\n end\r\n else\r\n acount += 1\r\n end\r\n end\r\n puts ((bcount > 0 ) & flag) ? \"YES\":\"NO\"\r\nend"}], "src_uid": "0b9be2f076cfa13cdc76c489bf1ea416"} {"nl": {"description": "Vova has won $$$n$$$ trophies in different competitions. Each trophy is either golden or silver. The trophies are arranged in a row.The beauty of the arrangement is the length of the longest subsegment consisting of golden trophies. Vova wants to swap two trophies (not necessarily adjacent ones) to make the arrangement as beautiful as possible — that means, to maximize the length of the longest such subsegment.Help Vova! Tell him the maximum possible beauty of the arrangement if he is allowed to do at most one swap.", "input_spec": "The first line contains one integer $$$n$$$ ($$$2 \\le n \\le 10^5$$$) — the number of trophies. The second line contains $$$n$$$ characters, each of them is either G or S. If the $$$i$$$-th character is G, then the $$$i$$$-th trophy is a golden one, otherwise it's a silver trophy. ", "output_spec": "Print the maximum possible length of a subsegment of golden trophies, if Vova is allowed to do at most one swap.", "sample_inputs": ["10\nGGGSGGGSGG", "4\nGGGG", "3\nSSS"], "sample_outputs": ["7", "4", "0"], "notes": "NoteIn the first example Vova has to swap trophies with indices $$$4$$$ and $$$10$$$. Thus he will obtain the sequence \"GGGGGGGSGS\", the length of the longest subsegment of golden trophies is $$$7$$$. In the second example Vova can make no swaps at all. The length of the longest subsegment of golden trophies in the sequence is $$$4$$$. In the third example Vova cannot do anything to make the length of the longest subsegment of golden trophies in the sequence greater than $$$0$$$."}, "positive_code": [{"source_code": "def length(streak)\n streak[1] - streak[0] + 1\nend\n\ndef solution\n read_int\n medals = read_string\n\n streaks = []\n crt_streak = nil\n\n medals.chars.each.with_index do |c, i|\n if c == ?G\n if crt_streak\n crt_streak[1] = i\n else\n crt_streak = [i, i]\n streaks << crt_streak\n end\n else\n crt_streak = nil\n end\n end\n\n if streaks.size == 0\n puts 0\n elsif streaks.size == 1\n puts length(streaks.first)\n else\n\n max = 0\n\n (0...streaks.size).each do |i|\n crt = streaks[i]\n next_streak = streaks[i + 1]\n\n crt_max = length(crt) + 1\n\n if next_streak && next_streak[0] == crt[1] + 2\n combined = length(crt) + length(next_streak) + 1\n if streaks.size == 2\n # have to reuse a Gold medal from one of the current streaks\n combined -= 1\n end\n\n crt_max = combined\n end\n\n max = [max, crt_max].max\n\n end\n\n puts max\n\n end\n\nend\n\ndef read_int\n gets.to_i\nend\n\ndef read_ints\n gets.split.map(&:to_i)\nend\n\ndef read_string\n gets.chomp\nend\n\nsolution unless ENV['TEST__MODE']"}, {"source_code": "gets\na=(?S+gets.chomp+?S).scan(/(?:G+|S+)/).map(&:size)\nres=0\n1.step(a.size, 2) do |i|\n break if !a[i]\n t=a[i]+(a.size>4 ? 1 : 0)\n res=a[i] if res6 && resmaxgold\n if wasaone\n asd = prevgold+x+1\n maxtwogold = asd if asd>maxtwogold\n end\n prevgold = x\n else\n if x==1\n wasaone = true\n else\n wasaone = false\n end\n end\n\nend\n\nif newarr.length==1\n if origingold\n puts maxgold\n else\n puts 0\n end\n exit\nend\n\nif newarr.length==2\n puts maxgold\n exit\nend\n\nif newarr.length==3\n if not origingold\n puts maxgold\n exit\n end\nend\n\nif newarr.length<5 or (newarr.length==5 and not origingold)\n puts [maxgold+1,maxtwogold-1].max\n exit\nend\n#puts maxgold\n#puts maxtwogold\nputs [maxgold+1,maxtwogold].max\n"}], "negative_code": [{"source_code": "\ndef length(streak)\n streak[1] - streak[0] + 1\nend\n\ndef solution\n read_int\n medals = read_string\n\n streaks = []\n crt_streak = nil\n\n medals.chars.each.with_index do |c, i|\n if c == ?G\n if crt_streak\n crt_streak[1] = i\n else\n crt_streak = [i, i]\n streaks << crt_streak\n end\n else\n crt_streak = nil\n end\n end\n\n if streaks.size == 0\n puts 0\n elsif streaks.size == 1\n puts length(streaks.first)\n else\n\n max = 0\n\n (0...streaks.size).each do |i|\n crt = streaks[i]\n next_streak = streaks[i + 1]\n\n crt_max = length(crt)\n\n if next_streak && next_streak[0] == crt[1] + 2\n combined = length(crt) + length(next_streak) + 1\n if streaks.size == 2\n # have to reuse a Gold medal from one of the current streaks\n combined -= 1\n end\n\n crt_max = combined\n end\n\n max = [max, crt_max].max\n\n end\n\n puts max\n\n end\n\nend\n\ndef read_int\n gets.to_i\nend\n\ndef read_ints\n gets.split.map(&:to_i)\nend\n\ndef read_string\n gets.chomp\nend\n\nsolution unless ENV['TEST__MODE']\n"}, {"source_code": "gets\na=(?S+gets.chomp+?S).scan(/(?:G+|S+)/).map(&:size)\nres=0\n1.step(a.size, 2) do |i|\n break if !a[i]\n t=a[i]+(a.size>4 ? 1 : 0)\n res=a[i] if res6 && res4 ? 1 : 0)\n res=a[i] if res6 && a[i+1]==1 ? a[i+2]+1 : 1)\n res=t if res5 ? 1 : 0)\n res=t if res5 && a[i+1]==1 ? 1 : 0)\n res=t if res4 ? 1 : 0)\n res=a[i] if res6 && a[i+1]==1 ? 1 : 0)\n res=t if res n; break; end;\n\tif n % i != 0; next; end;\n\n\tfor j in 0..i-1\n\t\tok = true;\n\t\tk = j;\n\t\twhile k < n\n\t\t\tif knights[k] == \"0\"\n\t\t\t\tok = false;\n\t\t\t\tbreak;\n\t\t\tend\n\t\t\tk += i;\n\t\tend\n\t\tif ok == true\n\t\t\tbreak;\n\t\tend\n\tend\n\tif ok\n\t\tbreak;\n\tend\nend\n\nif ok\n\tputs \"YES\";\nelse\n\tputs \"NO\";\nend\n\n"}, {"source_code": "#!/usr/bin/env/ruby\n\ndef exec(debug = false)\n length = gets.chomp.to_i\n elements = gets.chomp.split(/\\s/).map{|s| s.to_i}\n return puts \"NO\" unless length == elements.size\n \n max = length / 3\n p \"max: #{max}\" if debug\n max.times do |i|\n ii = i + 1\n p \"ii: #{ii}\" if debug\n next if length % ii != 0\n elements.each_with_index do |e, index|\n break if index == max\n success = true\n next if e == 0\n next if index >= ii\n p \"index:#{index}\" if debug\n (index + ii).step(length - 1, ii) do |j|\n p \"j: #{j}\" if debug\n unless elements[j] == 1\n p \"break\" if debug\n success = false\n break\n end\n end\n return puts(\"YES\") if success\n end\n end\n puts \"NO\"\nend\n\n#exec(debug = true)\nexec\n"}], "negative_code": [{"source_code": "require 'prime'\n\nn = gets.to_i\nknights = gets.split.map(&:to_i)\ndivs = n.prime_division.map{|a| a[0]-1}.select{|v| v+1<=n/3} + [0]\n\ndivs.each do |i|\n\t#if arr[i] == 1\n\t\t#check\n\t\tif n % (i+1) == 0\n\t\t\t(0..i).each do |start|\n\t\t\t\tok = true\n\t\t\t\tpos = start\n\t\t\t\t(n/(i+1)).times do\n\t\t\t\t\tif knights[pos] == 0\n\t\t\t\t\t\tok = false\n\t\t\t\t\t\tbreak\n\t\t\t\t\tend\n\t\t\t\t\tpos = (pos+i+1) % n\n\t\t\t\tend\n\t\t\t\tif ok\n\t\t\t\t\tputs \"YES\"\n\t\t\t\t\texit\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\n=begin\n\t\t(2..i+1).each do |k|\n\t\t\tif (i+1) % k == 0\n\t\t\t\tarr[(i+1)/k-1] = 0\n\t\t\tend\n\t\tend\n=end\n#\tend\nend\nputs \"NO\"\n"}, {"source_code": "#!/usr/bin/env/ruby\n\ndef exec(debug = false)\n length = gets.chomp.to_i\n elements = gets.chomp.split(/\\s/).map{|s| s.to_i}\n return puts \"NO\" unless length == elements.size\n \n max = length / 3\n p \"max: #{max}\" if debug\n max.times do |i|\n ii = i + 1\n p \"ii: #{ii}\" if debug\n next if length % ii != 0\n elements.each_with_index do |e, index|\n break if index == max\n success = true\n next if e == 0\n next if index >= ii\n p \"index:#{index}\" if debug\n (index + ii).step(length, ii) do |j|\n p \"j: #{j}\" if debug\n unless elements[j] == 1\n p \"break\" if debug\n success = false\n break\n end\n end\n return puts(\"YES\") if success\n end\n end\n puts \"NO\"\nend\n\nexec(debug = true)\n#exec\n"}, {"source_code": "#!/usr/bin/env/ruby\n\ndef exec\n length = gets.chomp.to_i\n elements = gets.chomp.split(/\\s/).map{|s| s.to_i}\n return puts \"NO\" unless length == elements.size\n \n max = length / 3\n #p \"max: #{max}\"\n max.times do |i|\n ii = i + 1\n #p \"ii: #{ii}\"\n return puts(\"NO\") unless length % ii == 0\n elements.each_with_index do |e, index|\n break if index == max\n success = true\n next if e == 0\n #p \"index:#{index}\"\n (index + ii).step(max, ii) do |j|\n #p \"j: #{j}\"\n unless elements[j] == 1\n #p \"break\"\n success = false\n break\n end\n end\n return puts(\"YES\") if success\n end\n end\n puts \"NO\"\nend\n\nexec\n"}, {"source_code": "#!/usr/bin/env/ruby\n\ndef exec(debug = false)\n length = gets.chomp.to_i\n elements = gets.chomp.split(/\\s/).map{|s| s.to_i}\n return puts \"NO\" unless length == elements.size\n \n max = length / 3\n p \"max: #{max}\" if debug\n max.times do |i|\n ii = i + 1\n p \"ii: #{ii}\" if debug\n elements.each_with_index do |e, index|\n break if index == max\n success = true\n next if e == 0\n p \"index:#{index}\" if debug\n (index + ii).step(max, ii) do |j|\n p \"j: #{j}\" if debug\n unless elements[j] == 1\n p \"break\" if debug\n success = false\n break\n end\n end\n return puts(\"YES\") if success\n end\n end\n puts \"NO\"\nend\n\n#exec(debug = true)\nexec\n"}, {"source_code": "#!/usr/bin/env/ruby\n\ndef exec(debug = false)\n length = gets.chomp.to_i\n elements = gets.chomp.split(/\\s/).map{|s| s.to_i}\n return puts \"NO\" unless length == elements.size\n \n max = length / 3\n p \"max: #{max}\" if debug\n max.times do |i|\n ii = i + 1\n p \"ii: #{ii}\" if debug\n next if length % ii != 0\n elements.each_with_index do |e, index|\n break if index == max\n success = true\n next if e == 0\n next if index >= ii\n p \"index:#{index}\" if debug\n (index + ii).step(length - 1, ii) do |j|\n p \"j: #{j}\" if debug\n unless elements[j] == 1\n p \"break\" if debug\n success = false\n break\n end\n end\n return puts(\"YES\") if success\n end\n end\n puts \"NO\"\nend\n\nexec(debug = true)\n#exec\n"}], "src_uid": "d3a0402de1338a1a542a86ac5b484acc"} {"nl": {"description": "Sasha grew up and went to first grade. To celebrate this event her mother bought her a multiplication table $$$M$$$ with $$$n$$$ rows and $$$n$$$ columns such that $$$M_{ij}=a_i \\cdot a_j$$$ where $$$a_1, \\dots, a_n$$$ is some sequence of positive integers.Of course, the girl decided to take it to school with her. But while she was having lunch, hooligan Grisha erased numbers on the main diagonal and threw away the array $$$a_1, \\dots, a_n$$$. Help Sasha restore the array!", "input_spec": "The first line contains a single integer $$$n$$$ ($$$3 \\leqslant n \\leqslant 10^3$$$), the size of the table. The next $$$n$$$ lines contain $$$n$$$ integers each. The $$$j$$$-th number of the $$$i$$$-th line contains the number $$$M_{ij}$$$ ($$$1 \\leq M_{ij} \\leq 10^9$$$). The table has zeroes on the main diagonal, that is, $$$M_{ii}=0$$$.", "output_spec": "In a single line print $$$n$$$ integers, the original array $$$a_1, \\dots, a_n$$$ ($$$1 \\leq a_i \\leq 10^9$$$). It is guaranteed that an answer exists. If there are multiple answers, print any.", "sample_inputs": ["5\n0 4 6 2 4\n4 0 6 2 4\n6 6 0 3 6\n2 2 3 0 2\n4 4 6 2 0", "3\n0 99990000 99970002\n99990000 0 99980000\n99970002 99980000 0"], "sample_outputs": ["2 2 3 1 2", "9999 10000 9998"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\narr = Array.new(n)\n\nn.times do |i|\n\tarr[i] = gets.split(' ').collect(&:to_i)\nend\n\na2 = Math.sqrt( ((arr[2][1] * arr[0][1])/arr[0][2]).round ).round\n\nans = []\n\nn.times do |i|\n\tif i == 1\n\t\tans << a2\n\telse\n\t\tans << (arr[i][1]/a2).to_i\n\tend\nend\n\nprint ans.join(' ')\n"}], "negative_code": [{"source_code": "n = gets.to_i\narr = Array.new(n)\n\nn.times do |i|\n\tarr[i] = gets.split(' ').collect(&:to_i)\nend\n\na2 = Math.sqrt((arr[2][1] * arr[0][1])/arr[2][1]).to_i\n\nans = []\n\nn.times do |i|\n\tif i == 1\n\t\tans << a2\n\telse\n\t\tans << (arr[i][1]/a2).to_i\n\tend\nend\n\nprint ans.join(' ')\n"}, {"source_code": "n = gets.to_i\narr = Array.new(n)\n\nn.times do |i|\n\tarr[i] = gets.split(' ').collect(&:to_i)\nend\n\na2 = Math.sqrt((arr[2][1] * arr[0][1])/arr[2][1]).ceil\n\nans = []\n\nn.times do |i|\n\tif i == 1\n\t\tans << a2\n\telse\n\t\tans << (arr[i][1]/a2).to_i\n\tend\nend\n\nprint ans.join(' ')\n"}, {"source_code": "n = gets.to_i\narr = Array.new(n)\n\nn.times do |i|\n\tarr[i] = gets.split(' ').collect(&:to_i)\nend\n\np ((arr[2][1] * arr[0][1])/arr[2][1]).round\n\na2 = Math.sqrt( ((arr[2][1] * arr[0][1])/arr[0][2]).round ).round\n\nans = []\n\nn.times do |i|\n\tif i == 1\n\t\tans << a2\n\telse\n\t\tans << (arr[i][1]/a2).to_i\n\tend\nend\n\nprint ans.join(' ')\n"}, {"source_code": "n = gets.to_i\narr = Array.new(n)\n\nn.times do |i|\n\tarr[i] = gets.split(' ').collect(&:to_i)\nend\n\nce = Math.sqrt((arr[2][1] * arr[0][1])/arr[2][1]).ceil\nfl = Math.sqrt((arr[2][1] * arr[0][1])/arr[2][1]).floor\n\nif (ce.to_f * ce.to_f) > (arr[2][1].to_f * arr[0][1].to_f)/arr[2][1].to_f\n\ta2 = ce\nelse\n\ta2 = fl\nend\n\nans = []\n\nn.times do |i|\n\tif i == 1\n\t\tans << a2\n\telse\n\t\tans << (arr[i][1]/a2).to_i\n\tend\nend\n\nprint ans.join(' ')\n"}, {"source_code": "n = gets.to_i\narr = Array.new(n)\n\nn.times do |i|\n\tarr[i] = gets.split(' ').collect(&:to_i)\n\tp arr[i]\nend\n\na2 = Math.sqrt((arr[2][1] * arr[0][1])/arr[2][1]).to_i\n\nn.times do |i|\n\tif i == 1\n\t\tprint \"#{a2} \"\n\telse\n\t\tprint \"#{arr[i][1]/a2} \"\n\tend\nend\n"}], "src_uid": "ced70b400694fa16929d4b0bce3da294"} {"nl": {"description": "You came to the exhibition and one exhibit has drawn your attention. It consists of $$$n$$$ stacks of blocks, where the $$$i$$$-th stack consists of $$$a_i$$$ blocks resting on the surface.The height of the exhibit is equal to $$$m$$$. Consequently, the number of blocks in each stack is less than or equal to $$$m$$$.There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks. Find the maximum number of blocks you can remove such that the views for both the cameras would not change.Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either.", "input_spec": "The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n \\le 100\\,000$$$, $$$1 \\le m \\le 10^9$$$) — the number of stacks and the height of the exhibit. The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le m$$$) — the number of blocks in each stack from left to right.", "output_spec": "Print exactly one integer — the maximum number of blocks that can be removed.", "sample_inputs": ["5 6\n3 3 3 3 3", "3 5\n1 2 4", "5 5\n2 3 1 4 4", "1 1000\n548", "3 3\n3 1 1"], "sample_outputs": ["10", "3", "9", "0", "1"], "notes": "NoteThe following pictures illustrate the first example and its possible solution.Blue cells indicate removed blocks. There are $$$10$$$ blue cells, so the answer is $$$10$$$. "}, "positive_code": [{"source_code": "n,_ = gets.split\narr = gets.split.map(&:to_i)\narr.sort_by!{|a| -a}\n\ntotal = arr.inject(&:+)\nneeded = 0\n\n(0...arr.size).each do |i|\n if i == arr.size-1\n needed += [1,arr[i]].max\n elsif arr[i] > arr[i+1]\n needed += arr[i] - arr[i+1]\n else\n needed +=1\n arr[i+1] = arr[i]-1\n end\nend\n\nputs total-needed"}, {"source_code": "n, m = gets.split.map{|x| x.to_i}\na = gets.split.map{|x| x.to_i}.sort.reverse\nm = a.max\nd = 0\n(0...n-1).each do |i|\n if a[i] == a[i + 1]\n d += a[i] if a[i] > 1\n a[i + 1] = [a[i] - 1, 1].max\n elsif a[i] > a[i + 1]\n d += a[i + 1]\n else\n d += a[i] if a[i] > 1\n d += a[i + 1] - a[i]\n a[i + 1] = [a[i] - 1, 1].max\n end \nend\nputs d\n"}], "negative_code": [], "src_uid": "ffa76f2558c8a70ab5c1ecb9e8561f25"} {"nl": {"description": "A line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coordinates are integer numbers from  - 5·1018 to 5·1018 inclusive, or to find out that such points do not exist.", "input_spec": "The first line contains three integers A, B and C ( - 2·109 ≤ A, B, C ≤ 2·109) — corresponding coefficients of the line equation. It is guaranteed that A2 + B2 > 0.", "output_spec": "If the required point exists, output its coordinates, otherwise output -1.", "sample_inputs": ["2 5 3"], "sample_outputs": ["6 -3"], "notes": null}, "positive_code": [{"source_code": "def extgcd(a, b)\n\tif b == 0\n\t\t[a, 1, 0]\n\telse\n\t\tg, x, y = extgcd(b, a % b)\n\t\t[g, y, x - a / b * y]\n\tend\nend\n\na, b, c = gets.split.map(&:to_i)\ng, x, y = extgcd(a, b)\n\nif c % g == 0\n\tc /= -g\n\tputs \"#{x*c} #{y*c}\"\nelse\n\tp -1\nend\n"}, {"source_code": "def E_GCD(a,b)\n if b==0\n return 1,0,a\n else\n x,y,d = E_GCD(b, a%b)\n return y,x-y*(a/b),d\n end\nend\n\n a,b,c = gets.split.map(&:to_i)\n x,y,d = E_GCD(a,b)\n \n if c%d==0\n puts \"#{-x*c/d} #{-y*c/d}\"\n else\n puts -1\n end"}], "negative_code": [], "src_uid": "a01e1c545542c1641eca556439f0692e"} {"nl": {"description": "Limak is a little polar bear. He loves connecting with other bears via social networks. He has n friends and his relation with the i-th of them is described by a unique integer ti. The bigger this value is, the better the friendship is. No two friends have the same value ti.Spring is starting and the Winter sleep is over for bears. Limak has just woken up and logged in. All his friends still sleep and thus none of them is online. Some (maybe all) of them will appear online in the next hours, one at a time.The system displays friends who are online. On the screen there is space to display at most k friends. If there are more than k friends online then the system displays only k best of them — those with biggest ti.Your task is to handle queries of two types: \"1 id\" — Friend id becomes online. It's guaranteed that he wasn't online before. \"2 id\" — Check whether friend id is displayed by the system. Print \"YES\" or \"NO\" in a separate line. Are you able to help Limak and answer all queries of the second type?", "input_spec": "The first line contains three integers n, k and q (1 ≤ n, q ≤ 150 000, 1 ≤ k ≤ min(6, n)) — the number of friends, the maximum number of displayed online friends and the number of queries, respectively. The second line contains n integers t1, t2, ..., tn (1 ≤ ti ≤ 109) where ti describes how good is Limak's relation with the i-th friend. The i-th of the following q lines contains two integers typei and idi (1 ≤ typei ≤ 2, 1 ≤ idi ≤ n) — the i-th query. If typei = 1 then a friend idi becomes online. If typei = 2 then you should check whether a friend idi is displayed. It's guaranteed that no two queries of the first type will have the same idi becuase one friend can't become online twice. Also, it's guaranteed that at least one query will be of the second type (typei = 2) so the output won't be empty.", "output_spec": "For each query of the second type print one line with the answer — \"YES\" (without quotes) if the given friend is displayed and \"NO\" (without quotes) otherwise.", "sample_inputs": ["4 2 8\n300 950 500 200\n1 3\n2 4\n2 3\n1 1\n1 2\n2 1\n2 2\n2 3", "6 3 9\n50 20 51 17 99 24\n1 3\n1 4\n1 5\n1 2\n2 4\n2 2\n1 1\n2 4\n2 3"], "sample_outputs": ["NO\nYES\nNO\nYES\nYES", "NO\nYES\nNO\nYES"], "notes": "NoteIn the first sample, Limak has 4 friends who all sleep initially. At first, the system displays nobody because nobody is online. There are the following 8 queries: \"1 3\" — Friend 3 becomes online. \"2 4\" — We should check if friend 4 is displayed. He isn't even online and thus we print \"NO\". \"2 3\" — We should check if friend 3 is displayed. Right now he is the only friend online and the system displays him. We should print \"YES\". \"1 1\" — Friend 1 becomes online. The system now displays both friend 1 and friend 3. \"1 2\" — Friend 2 becomes online. There are 3 friends online now but we were given k = 2 so only two friends can be displayed. Limak has worse relation with friend 1 than with other two online friends (t1 < t2, t3) so friend 1 won't be displayed \"2 1\" — Print \"NO\". \"2 2\" — Print \"YES\". \"2 3\" — Print \"YES\". "}, "positive_code": [{"source_code": "class PQueue\n\n #\n #VERSION = \"2.1.0\" #:erb: VERSION = \"<%= version %>\"\n\n #\n # Returns a new priority queue.\n #\n # If elements are given, build the priority queue with these initial\n # values. The elements object must respond to #to_a.\n #\n # If a block is given, it will be used to determine the priority between\n # the elements. The block must must take two arguments and return `1`, `0`,\n # or `-1` or `true`, `nil` or `false. It should return `0` or `nil` if the\n # two arguments are considered equal, return `1` or `true` if the first\n # argument is considered greater than the later, and `-1` or `false` if\n # the later is considred to be greater than the first.\n #\n # By default, the priority queue retrieves maximum elements first\n # using the #<=> method.\n #\n def initialize(elements=nil, &block) # :yields: a, b\n @que = []\n @cmp = block || lambda{ |a,b| a <=> b }\n replace(elements) if elements\n end\n\n protected\n\n #\n # The underlying heap.\n #\n attr_reader :que #:nodoc:\n\n public\n\n #\n # Priority comparison procedure.\n #\n attr_reader :cmp\n\n #\n # Returns the size of the queue.\n #\n def size\n @que.size\n end\n\n #\n # Alias of size.\n #\n alias length size\n\n #\n # Add an element in the priority queue.\n #\n def push(v)\n @que << v\n reheap(@que.size-1)\n self\n end\n\n #\n # Traditional alias for #push.\n #\n alias enq push\n\n #\n # Alias of #push.\n #\n alias :<< :push\n\n #\n # Get the element with the highest priority and remove it from\n # the queue.\n #\n # The highest priority is determined by the block given at instantiation\n # time.\n #\n # The deletion time is O(log n), with n is the size of the queue.\n #\n # Return nil if the queue is empty.\n #\n def pop\n return nil if empty?\n @que.pop\n end\n\n #\n # Traditional alias for #pop.\n #\n alias deq pop\n\n # Get the element with the lowest priority and remove it from\n # the queue.\n #\n # The lowest priority is determined by the block given at instantiation\n # time.\n #\n # The deletion time is O(log n), with n is the size of the queue.\n #\n # Return nil if the queue is empty.\n #\n def shift\n return nil if empty?\n @que.shift\n end\n\n #\n # Returns the element with the highest priority, but\n # does not remove it from the queue.\n #\n def top\n return nil if empty?\n return @que.last\n end\n\n #\n # Traditional alias for #top.\n #\n alias peek top\n\n #\n # Returns the element with the lowest priority, but\n # does not remove it from the queue.\n #\n def bottom\n return nil if empty?\n return @que.first\n end\n\n #\n # Add more than one element at the same time. See #push.\n #\n # The elements object must respond to #to_a, or be a PQueue itself.\n #\n def concat(elements)\n if empty?\n if elements.kind_of?(PQueue)\n initialize_copy(elements)\n else\n replace(elements)\n end\n else\n if elements.kind_of?(PQueue)\n @que.concat(elements.que)\n sort!\n else\n @que.concat(elements.to_a)\n sort!\n end\n end\n return self\n end\n\n #\n # Alias for #concat.\n #\n alias :merge! :concat\n\n #\n # Return top n-element as a sorted array.\n #\n def take(n=@size)\n a = []\n n.times{a.push(pop)}\n a\n end\n\n #\n # Returns true if there is no more elements left in the queue.\n #\n def empty?\n @que.empty?\n end\n\n #\n # Remove all elements from the priority queue.\n #\n def clear\n @que.clear\n self\n end\n\n #\n # Replace the content of the heap by the new elements.\n #\n # The elements object must respond to #to_a, or to be\n # a PQueue itself.\n #\n def replace(elements)\n if elements.kind_of?(PQueue)\n initialize_copy(elements)\n else\n @que.replace(elements.to_a)\n sort!\n end\n self\n end\n\n #\n # Return a sorted array, with highest priority first.\n #\n def to_a\n @que.dup\n end\n\n #\n # Return true if the given object is present in the queue.\n #\n def include?(element)\n @que.include?(element)\n end\n\n #\n # Push element onto queue while popping off and returning the next element.\n # This is qquivalent to successively calling #pop and #push(v).\n #\n def swap(v)\n r = pop\n push(v)\n r\n end\n\n #\n # Iterate over the ordered elements, destructively.\n #\n def each_pop #:yields: popped\n until empty?\n yield pop\n end\n nil\n end\n\n #\n # Pretty inspection string.\n #\n def inspect\n \"<#{self.class}: size=#{size}, top=#{top || \"nil\"}>\"\n end\n\n #\n # Return true if the queues contain equal elements.\n #\n def ==(other)\n size == other.size && to_a == other.to_a\n end\n\n private\n\n #\n #\n #\n def initialize_copy(other)\n @cmp = other.cmp\n @que = other.que.dup\n sort!\n end\n\n #\n # The element at index k will be repositioned to its proper place.\n #\n # This, of course, assumes the queue is already sorted.\n #\n def reheap(k)\n return self if size <= 1\n\n que = @que.dup\n\n v = que.delete_at(k)\n i = binary_index(que, v)\n\n que.insert(i, v)\n\n @que = que\n\n return self\n end\n\n #\n # Sort the queue in accorance to the given comparison procedure.\n #\n def sort!\n @que.sort! do |a,b|\n case @cmp.call(a,b)\n when 0, nil then 0\n when 1, true then 1\n when -1, false then -1\n else\n warn \"bad comparison procedure in #{self.inspect}\"\n 0\n end\n end\n self\n end\n\n #\n # Alias of #sort!\n #\n alias heapify sort!\n\n #\n def binary_index(que, target)\n upper = que.size - 1\n lower = 0\n\n while(upper >= lower) do\n idx = lower + (upper - lower) / 2\n comp = @cmp.call(target, que[idx])\n\n case comp\n when 0, nil\n return idx\n when 1, true\n lower = idx + 1\n when -1, false\n upper = idx - 1\n else\n end\n end\n lower\n end\n\nend # class PQueue\n\nn, k, q = gets.split.map(&:to_i)\t# Friends, Friend view size, Queries\nt = gets.split.map(&:to_i)\t\t\t# Friendship Powers!! [#n]\nwindow = PQueue.new\n\nq.times do\n\tact, friendid = gets.split.map(&:to_i)\n\tfpower = t[friendid-1]\n\tif act==1\n\t\t# Add friend\n\t\twindow << fpower\n\t\twindow.shift if window.length>k\n\telse\n\t\tputs (if window.include? fpower\n\t\t\t\"YES\"\n\t\telse\n\t\t\t\"NO\"\n\t\tend)\n\tend\nend\n"}], "negative_code": [], "src_uid": "7c778289806ceed506543ef816c587c9"} {"nl": {"description": "Chilly Willy loves playing with numbers. He only knows prime numbers that are digits yet. These numbers are 2, 3, 5 and 7. But Willy grew rather bored of such numbers, so he came up with a few games that were connected with them.Chilly Willy wants to find the minimum number of length n, such that it is simultaneously divisible by all numbers Willy already knows (2, 3, 5 and 7). Help him with that.A number's length is the number of digits in its decimal representation without leading zeros.", "input_spec": "A single input line contains a single integer n (1 ≤ n ≤ 105).", "output_spec": "Print a single integer — the answer to the problem without leading zeroes, or \"-1\" (without the quotes), if the number that meet the problem condition does not exist.", "sample_inputs": ["1", "5"], "sample_outputs": ["-1", "10080"], "notes": null}, "positive_code": [{"source_code": "def t(n)\n if n % 2 == 0 then\n return true\n else\n return false\n end\nend\n\ndef th(n)\n if n % 3 == 0 then\n return true\n else\n return false\n end\nend\n\ndef f(n)\n if n % 5 == 0 then\n return true\n else\n return false\n end\nend\n\ndef s(n)\n if n % 7 == 0 then\n return true\n else\n return false\n end\nend\n\nn = gets.to_i\n\nnum = 10**(n-1)\nh = 1\n\nwhile !t(num) do\n num += h\nend\n\nh = 2\n\nwhile !th(num) do\n num += h\nend\n\nh *= 3\n\nwhile !f(num) do\n num += h\nend\n\nh *= 5\n\nwhile !s(num) && num < 10**n do \n num += h\nend\n\nif !s(num) then\n num = -1\nend\n\nputs num"}, {"source_code": "a=10**(gets.to_i-1)\nif a<100 then\n puts -1\nelse\n while a%7!=0 do\n a+=1\n end\n while a%2!=0 || a%3!=0 || a%5!=0 do\n a+=7\n end\n puts a\nend\n"}, {"source_code": "a=10**(gets.to_i-1)\nif a<100 then\n puts -1\nelse\n while a%7!=0 do\n a+=1\n end\n while a%2!=0 || a%3!=0 || a%5!=0 || a%7!=0 do\n a+=7\n end\n puts a\nend"}, {"source_code": "n = gets.to_i\nif n < 3\n puts -1\nelse\n puts (10**(n - 1) + 209) / 210 * 210\nend\n"}, {"source_code": "def solve(n)\n return -1 if n <= 2\n return 210 if n == 3\n \n bases = [\"050\", \"080\", \"170\", \"020\", \"200\", \"110\"]\n \n return \"1\" + \"0\" * (n - 4) + bases[(n - 4) % 6 ]\n\nend\n\nn = gets.chomp.to_i\nputs solve(n)\n"}, {"source_code": "n = gets.to_i\nif n < 3 then\n print -1\n exit\nelse\n x = 10**(n-1) +20\n while x%7 != 0 do x += 30 end\n print x\nend"}, {"source_code": "n = gets.to_i\nif n < 3 then\n print -1\n exit\nend\nNum = 2*3*5*7\nk = 10**(n-1)\nwhile k%Num != 0 do\n k += 10\nend\nprint k"}, {"source_code": "n=gets.chomp.to_i\ndef s n\n if n<3\n puts -1\n return\n end\n s = 210\n z = 10**(n-1)\n d = z/s\n puts s*(d+1)\nend\ns n"}, {"source_code": "#!/usr/bin/env ruby\nrequire 'bigdecimal'\nn = STDIN.gets.chomp.to_i\nx = 10 ** (n - 1) / 210\n\na = x * 210\nb = (x + 1) * 210\nif a.to_s.length == n && a > 0\n puts a\nelsif b.to_s.length == n && b > 0\n puts b\nelse\n puts -1\nend\n"}, {"source_code": "m = gets.to_i\nn = 10 ** (m - 1)\nn = (n + 209) / 210 * 210\nn = -1 if m < 3\nprint n\n"}, {"source_code": "n=gets.chomp.to_i\n\nres=0\na=[2,3,5,7]\n\n((10**(n-1))..(10**n-1)).each do |i|\nflag=1\na.each do |j|\nif i%j!=0\nflag=0\nbreak\nend\nend\n\nif flag==1\nres=i\nbreak\nend\n\nend\n\nres=-1 if res==0\n\nputs res\n"}, {"source_code": "n = gets.to_i\nm = 2*3*5*7\nif (n < 3)\n\tputs -1\nelsif n == 3\n\tputs m\nelse\n\tk = 10**(n-1)\n\tr = k % m\n\tputs k + m - r\nend\n\t\n\t"}], "negative_code": [{"source_code": "n = gets.to_i\n\nnum = 10**(n-1)\nwhile (num % 3) != 0 || (num % 7) != 0 do\n num += 10\nend\n\nputs num"}, {"source_code": "a=10**(gets.to_i-1)\nif a<100 then\n puts -1\nelse\n while a%7!=0 do\n a+=1\n end\n while a%2!=0 || a%3!=0 || a%5!=0 do\n a+=1\n end\n puts a\nend"}, {"source_code": "a=10**(gets.to_i-1)\nif a<1000 then\n\tputs -1\nelse\n\twhile a%7!=0 do\n\t\ta+=1\n\tend\n\twhile a%2!=0 || a%3!=0 || a%5!=0 do\n\t\ta+=7\n\tend\n\tputs a\nend\n"}, {"source_code": "a=10**(gets.to_i-1)\nwhile a%7!=0 do\n a+=1\nend\nwhile (a%2!=0 || a%3!=0 || a%5!=0) do \n a+=7\nend\nputs a\n"}, {"source_code": "#!/usr/bin/env ruby\nrequire 'bigdecimal'\nn = STDIN.gets.chomp.to_i\n\nlog = n - 1 - Math::log(2 * 3 * 5 * 7, 10)\nx = (BigDecimal.new('10.0') ** log).to_i\na = x * 210\nb = (x + 1) * 210\n\nif a.to_s.length == n && a > 0\n puts a\nelsif b.to_s.length == n && b > 0\n puts b\nelse\n puts \"-1\"\nend\n"}, {"source_code": "#!/usr/bin/env ruby\nrequire 'bigdecimal'\nn = STDIN.gets.chomp.to_i\n\nlog = n - 1 - Math::log(2 * 3 * 5 * 7, 10)\nif log < 0\n puts -1\n exit\nend\nx = (BigDecimal.new('10.0') ** log).to_i\na = x * 210\nb = (x + 1) * 210\nc = (x + 2) * 210\n\nif a.to_s.length == n\n puts a\nelsif b.to_s.length == n\n puts b\nelsif c.to_s.length == n\nelse\n puts \"-1\"\nend\n"}, {"source_code": "#!/usr/bin/env ruby\nrequire 'bigdecimal'\nn = STDIN.gets.chomp.to_i\nx = 10 ** (n - 1) / 210\n\nif x < 1\n puts -1\n exit\nend\na = x * 210\nb = (x + 1) * 210\nif a.to_s.length == n\n puts a\nelsif b.to_s.length == n\n puts b\nelse\n puts -1\nend\n"}, {"source_code": "n = gets.to_i\nn = 10 ** n\nn = (n + 209) / 210 * 210\nprint n"}, {"source_code": "m = gets.to_i\nn = 10 ** m\nn = (n + 209) / 210 * 210\nn = -1 if m < 3\nprint n\n"}], "src_uid": "386345016773a06b9e190a55cc3717fa"} {"nl": {"description": "Squirrel Liss lived in a forest peacefully, but unexpected trouble happens. Stones fall from a mountain. Initially Squirrel Liss occupies an interval [0, 1]. Next, n stones will fall and Liss will escape from the stones. The stones are numbered from 1 to n in order.The stones always fall to the center of Liss's interval. When Liss occupies the interval [k - d, k + d] and a stone falls to k, she will escape to the left or to the right. If she escapes to the left, her new interval will be [k - d, k]. If she escapes to the right, her new interval will be [k, k + d].You are given a string s of length n. If the i-th character of s is \"l\" or \"r\", when the i-th stone falls Liss will escape to the left or to the right, respectively. Find the sequence of stones' numbers from left to right after all the n stones falls.", "input_spec": "The input consists of only one line. The only line contains the string s (1 ≤ |s| ≤ 106). Each character in s will be either \"l\" or \"r\".", "output_spec": "Output n lines — on the i-th line you should print the i-th stone's number from the left.", "sample_inputs": ["llrlr", "rrlll", "lrlrr"], "sample_outputs": ["3\n5\n4\n2\n1", "1\n2\n5\n4\n3", "2\n4\n5\n3\n1"], "notes": "NoteIn the first example, the positions of stones 1, 2, 3, 4, 5 will be , respectively. So you should print the sequence: 3, 5, 4, 2, 1."}, "positive_code": [{"source_code": "index = 0\ns = gets.chomp\nans = Array.new\nfor i in 0...s.length\n if s[i] == 'r'\n puts i+1\n else\n ans.push(i+1)\n end\nend\nans.reverse.each{|i| puts i}\n"}], "negative_code": [{"source_code": "left = 0.0\nright = 1.0\ns = gets.chomp\nans = Hash.new\nfor i in 0...s.length\n tmp = (left+right)/2\n ans[tmp] = i+1\n if s[i] == 'l'\n right = tmp\n else\n left = tmp\n end\nend\nans.sort.each{|i| puts i[1]}\n"}], "src_uid": "9d3c0f689ae1e6215448463def55df32"} {"nl": {"description": "You've got a list of program warning logs. Each record of a log stream is a string in this format: \"2012-MM-DD HH:MM:SS:MESSAGE\" (without the quotes). String \"MESSAGE\" consists of spaces, uppercase and lowercase English letters and characters \"!\", \".\", \",\", \"?\". String \"2012-MM-DD\" determines a correct date in the year of 2012. String \"HH:MM:SS\" determines a correct time in the 24 hour format.The described record of a log stream means that at a certain time the record has got some program warning (string \"MESSAGE\" contains the warning's description).Your task is to print the first moment of time, when the number of warnings for the last n seconds was not less than m.", "input_spec": "The first line of the input contains two space-separated integers n and m (1 ≤ n, m ≤ 10000). The second and the remaining lines of the input represent the log stream. The second line of the input contains the first record of the log stream, the third line contains the second record and so on. Each record of the log stream has the above described format. All records are given in the chronological order, that is, the warning records are given in the order, in which the warnings appeared in the program. It is guaranteed that the log has at least one record. It is guaranteed that the total length of all lines of the log stream doesn't exceed 5·106 (in particular, this means that the length of some line does not exceed 5·106 characters). It is guaranteed that all given dates and times are correct, and the string 'MESSAGE\" in all records is non-empty.", "output_spec": "If there is no sought moment of time, print -1. Otherwise print a string in the format \"2012-MM-DD HH:MM:SS\" (without the quotes) — the first moment of time when the number of warnings for the last n seconds got no less than m.", "sample_inputs": ["60 3\n2012-03-16 16:15:25: Disk size is\n2012-03-16 16:15:25: Network failute\n2012-03-16 16:16:29: Cant write varlog\n2012-03-16 16:16:42: Unable to start process\n2012-03-16 16:16:43: Disk size is too small\n2012-03-16 16:16:53: Timeout detected", "1 2\n2012-03-16 23:59:59:Disk size\n2012-03-17 00:00:00: Network\n2012-03-17 00:00:01:Cant write varlog", "2 2\n2012-03-16 23:59:59:Disk size is too sm\n2012-03-17 00:00:00:Network failute dete\n2012-03-17 00:00:01:Cant write varlogmysq"], "sample_outputs": ["2012-03-16 16:16:43", "-1", "2012-03-17 00:00:00"], "notes": null}, "positive_code": [{"source_code": "require 'date'\n \nn, m = gets.split.map &:to_i\nlogs = $<.map { |s| DateTime.parse(s[0, 19]) }\n(m - 1).upto(logs.size - 1) do |i|\n if (logs[i] - logs[i - (m - 1)]) * 60 * 60 * 24 < n\n puts logs[i].to_s.gsub(/T/, ' ').gsub(/\\+.*/, '')\n exit\n end\nend\n \nputs -1"}, {"source_code": "require 'date'\n\nn, m = gets.chomp.split.map &:to_i\n\nl = n + 10\ncounts = Array.new(l) do |i|\n [0, 0]\nend\n\nfirst = 0\nlast = prev = 0\ntotal = 0\nans = -1\nwhile line=gets\n time_str = line[0..18]\n time = DateTime.parse(time_str).to_time\n total += 1\n if first != last && time == counts[prev][0]\n counts[prev][1] += 1\n else\n while first != last && counts[first][0] <= time - n\n total -= counts[first][1]\n first += 1\n first = 0 if first == l\n end\n counts[last][0] = time\n counts[last][1] = 1\n prev = last\n last += 1\n last = 0 if last == l\n end\n if total >= m\n ans = time_str\n break\n end\nend\n\nputs ans\n "}, {"source_code": "input = gets.chomp.split\nn = Integer(input[0])\nm = Integer(input[1])\nlog_stream = Array.new\nwhile line = gets do\n log_stream.push(line)\nend\n\nx = n / 3600\ny = (n % 3600) / 60\nz = n - 3600 * x - 60 * y\n\n\ndef time_back(log, x, y, z)\n year = log[0..3].to_i\n month = log[5..6].to_i\n date = log[8..9].to_i\n hour = log[11..12].to_i\n minute = log[14..15].to_i\n second = log[17..18].to_i\n if second - z >= 0\n second = second - z\n else\n second = second - z + 60\n y += 1\n end\n if minute - y >= 0\n minute = minute - y\n else\n minute = minute - y + 60\n x += 1\n end\n if hour - x >= 0\n hour = hour - x\n else\n hour = hour - x + 24\n date -= 1\n if date == 0\n month -= 1\n if [1,3,5,7,8,10].include? month\n date = 31\n elsif [4,6,9,11].include? month\n date = 30\n elsif month == 0\n date = 31\n month = 12\n year -= 1\n elsif month == 2\n if year % 4 == 0\n date = 29\n else\n date = 28\n end\n end\n end\n end\n if month < 10\n month = \"0\" + month.to_s\n end\n if date < 10\n date = \"0\" + date.to_s\n end\n if hour < 10\n hour = \"0\" + hour.to_s\n end\n if minute < 10\n minute = \"0\" + minute.to_s\n end\n if second < 10\n second = \"0\" + second.to_s\n end\n return \"#{year}-#{month}-#{date} #{hour}:#{minute}:#{second}\"\nend\n\nmoment = -1\nfor i in m-1...log_stream.length\n if time_back(log_stream[i], x, y, z) < log_stream[i-m+1][0..18]\n moment = log_stream[i]\n break\n end\nend\nif moment != -1\n puts moment[0..18]\nelse\n puts moment\nend\n\n"}], "negative_code": [{"source_code": "require 'date'\n \nn, m = gets.split.map &:to_i\nlogs = $<.map { |s| DateTime.parse(s.strip).new_offset(0) }\nputs logs.map { |l| l.to_time }\nputs logs.map { |l| l.to_time.to_s }\nputs logs.map { |l| l.to_time.to_s.gsub(/ \\+.*/, '_') }\n(m - 1).upto(logs.size - 1) do |i|\n if (logs[i] - logs[i - (m - 1)]) * 60 * 60 * 24 < n\n puts logs[i].to_time.to_s.gsub(/ \\+.*/, '')\n exit\n end\nend\n \nputs -1"}, {"source_code": "require 'date'\n \nn, m = gets.split.map &:to_i\nlogs = $<.map { |s| DateTime.parse(s.strip) }\nputs logs\n(m - 1).upto(logs.size - 1) do |i|\n if (logs[i] - logs[i - (m - 1)]) * 60 * 60 * 24 < n\n puts logs[i].to_time.to_s.gsub(/ \\+.*/, '')\n exit\n end\nend\n \nputs -1"}, {"source_code": "require 'date'\n \nn, m = gets.split.map &:to_i\nlogs = $<.map { |s| DateTime.parse(s.strip) }\nputs logs.map { |l| l.to_time }\nputs logs.map { |l| l.to_time.to_s }\nputs logs.map { |l| l.to_time.to_s.gsub(/ \\+.*/, '_') }\n(m - 1).upto(logs.size - 1) do |i|\n if (logs[i] - logs[i - (m - 1)]) * 60 * 60 * 24 < n\n puts logs[i].to_time.to_s.gsub(/ \\+.*/, '')\n exit\n end\nend\n \nputs -1"}, {"source_code": "require 'date'\n \nn, m = gets.split.map &:to_i\nlogs = $<.map { |s| DateTime.parse(s.strip) }\n(m - 1).upto(logs.size - 1) do |i|\n if (logs[i] - logs[i - (m - 1)]) * 60 * 60 * 24 < n\n puts logs[i].new_offset(0).to_time.to_s.gsub(/ \\+.*/, '')\n exit\n end\nend\n \nputs -1"}, {"source_code": "require 'date'\n \nn, m = gets.split.map &:to_i\nlogs = $<.map { |s| DateTime.parse(s.strip) }\n(m - 1).upto(logs.size - 1) do |i|\n if (logs[i] - logs[i - (m - 1)]) * 60 * 60 * 24 < n\n puts logs[i].to_s.gsub(/T/, '').gsub(/ \\+.*/, '')\n exit\n end\nend\n \nputs -1"}, {"source_code": "require 'date'\n \nn, m = gets.split.map &:to_i\nlogs = $<.map { |s| DateTime.parse(s.strip).new_offset(\"+0000\") }\n(m - 1).upto(logs.size - 1) do |i|\n if (logs[i] - logs[i - (m - 1)]) * 60 * 60 * 24 < n\n puts logs[i].to_time.to_s.gsub(/ \\+.*/, '')\n exit\n end\nend\n \nputs -1"}, {"source_code": "require 'date'\n \nn, m = gets.split.map &:to_i\nlogs = $<.map { |s| DateTime.parse(s.strip).new_offset(\"+0000\") }\nputs logs.map { |l| l.to_time }\nputs logs.map { |l| l.to_time.to_s }\nputs logs.map { |l| l.to_time.to_s.gsub(/ \\+.*/, '_') }\n(m - 1).upto(logs.size - 1) do |i|\n if (logs[i] - logs[i - (m - 1)]) * 60 * 60 * 24 < n\n puts logs[i].to_time.to_s.gsub(/ \\+.*/, '')\n exit\n end\nend\n \nputs -1"}, {"source_code": "require 'date'\n \nn, m = gets.split.map &:to_i\nlogs = $<.map { |s| DateTime.parse(s[0, 19].strip) }\n(m - 1).upto(logs.size - 1) do |i|\n if (logs[i] - logs[i - (m - 1)]) * 60 * 60 * 24 < n\n puts logs[i].to_s.gsub(/T/, ' ').gsub(/ \\+.*/, '')\n exit\n end\nend\n \nputs -1"}, {"source_code": "require 'date'\n \nn, m = gets.split.map &:to_i\nlogs = $<.map { |s| DateTime.parse(s.strip) }\n(m - 1).upto(logs.size - 1) do |i|\n if (logs[i] - logs[i - (m - 1)]) * 60 * 60 * 24 < n\n puts logs[i].to_s.gsub(/ \\+.*/, '')\n exit\n end\nend\n \nputs -1"}, {"source_code": "require 'date'\n\nn, m = gets.split.map &:to_i\nlogs = $<.map { |s| DateTime.parse(s.strip) }\n(m - 1).upto(logs.size - 1) do |i|\n puts logs[i]\n if (logs[i] - logs[i - (m - 1)]) * 60 * 60 * 24 < n\n puts logs[i].to_time.to_s.gsub(/ \\+.*/, '')\n exit\n end\nend\n\nputs -1\n"}, {"source_code": "require 'date'\n \nn, m = gets.split.map &:to_i\nlogs = $<.map { |s| DateTime.parse(s.strip) }\n(m - 1).upto(logs.size - 1) do |i|\n if (logs[i] - logs[i - (m - 1)]) * 60 * 60 * 24 < n\n puts logs[i].to_s.gsub(/T/, ' ').gsub(/ \\+.*/, '')\n exit\n end\nend\n \nputs -1"}, {"source_code": "require 'date'\n \nn, m = gets.split.map &:to_i\nlogs = $<.map { |s| DateTime.parse(s.strip) }\n(m - 1).upto(logs.size - 1) do |i|\n if (logs[i] - logs[i - (m - 1)]) * 60 * 60 * 24 < n\n puts logs[i].to_s.gsub(/T/, ' ').gsub(/\\+.*/, '')\n exit\n end\nend\n \nputs -1"}, {"source_code": "require 'date'\n \nn, m = gets.split.map &:to_i\nlogs = $<.map { |s| DateTime.parse(s.strip) }\n(m - 1).upto(logs.size - 1) do |i|\n if (logs[i] - logs[i - (m - 1)]) * 60 * 60 * 24 < n\n puts logs[i].new_offset(0).to_s.gsub(/ \\+.*/, '')\n exit\n end\nend\n \nputs -1"}, {"source_code": "require 'date'\n\nn, m = gets.split.map &:to_i\nlogs = $<.map { |s| DateTime.parse(s.strip) }\n(m - 1).upto(logs.size - 1) do |i|\n if (logs[i] - logs[i - (m - 1)]) * 60 * 60 * 24 < n\n puts logs[i].to_time.to_s.gsub(/ \\+.*/, '')\n exit\n end\nend\n\nputs -1\n"}, {"source_code": "input = gets.chomp.split\nn = Integer(input[0])\nm = Integer(input[1])\nlog_stream = Array.new\nwhile line = gets do\n log_stream.push(line)\nend\n\nx = n / 3600\ny = (n % 3600) / 60\nz = n - 3600 * x - 60 * y\n\n\ndef time_back(log, x, y, z)\n year = log[0..3].to_i\n month = log[5..6].to_i\n date = log[8..9].to_i\n hour = log[11..12].to_i\n minute = log[14..15].to_i\n second = log[17..18].to_i\n if second - z >= 0\n second = second - z\n else\n second = second - z + 60\n y += 1\n end\n if minute - y >= 0\n minute = minute - y\n else\n minute = minute - y + 60\n x += 1\n end\n if hour - x >= 0\n hour = hour - x\n else\n hour = hour - x + 24\n date -= 1\n if date == 0\n month -= 1\n if [1,3,5,7,8,10].include? month\n date = 31\n elsif [4,6,9,11].include? month\n date = 30\n elsif month == 0\n date = 31\n month = 12\n year -= 1\n elsif month == 2\n if year % 4 == 0\n date = 29\n else\n date = 28\n end\n end\n end\n end\n if month < 10\n month = \"0\" + month.to_s\n end\n if date < 10\n date = \"0\" + date.to_s\n end\n if hour < 10\n hour = \"0\" + hour.to_s\n end\n if minute < 10\n minute = \"0\" + minute.to_s\n end\n if second < 10\n second = \"0\" + second.to_s\n end\n return \"#{year}-#{month}-#{date} #{hour}:#{minute}:#{second}\"\nend\n\nmoment = -1\nputs log_stream.length\nfor i in m-1...log_stream.length\n puts\n puts time_back(log_stream[i], x, y, z) \n puts log_stream[i-m+1][0..18]\n if time_back(log_stream[i], x, y, z) < log_stream[i-m+1][0..18]\n moment = log_stream[i]\n break\n end\nend\nif moment != -1\n puts moment[0..18]\nelse\n puts moment\nend\n\n"}], "src_uid": "c3f671243f0aef7b78a7e091b0e8f75e"} {"nl": {"description": "For a given array $$$a$$$ consisting of $$$n$$$ integers and a given integer $$$m$$$ find if it is possible to reorder elements of the array $$$a$$$ in such a way that $$$\\sum_{i=1}^{n}{\\sum_{j=i}^{n}{\\frac{a_j}{j}}}$$$ equals $$$m$$$? It is forbidden to delete elements as well as insert new elements. Please note that no rounding occurs during division, for example, $$$\\frac{5}{2}=2.5$$$.", "input_spec": "The first line contains a single integer $$$t$$$ — the number of test cases ($$$1 \\le t \\le 100$$$). The test cases follow, each in two lines. The first line of a test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n \\le 100$$$, $$$0 \\le m \\le 10^6$$$). The second line contains integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$0 \\le a_i \\le 10^6$$$) — the elements of the array.", "output_spec": "For each test case print \"YES\", if it is possible to reorder the elements of the array in such a way that the given formula gives the given value, and \"NO\" otherwise.", "sample_inputs": ["2\n3 8\n2 5 1\n4 4\n0 1 2 3"], "sample_outputs": ["YES\nNO"], "notes": "NoteIn the first test case one of the reorders could be $$$[1, 2, 5]$$$. The sum is equal to $$$(\\frac{1}{1} + \\frac{2}{2} + \\frac{5}{3}) + (\\frac{2}{2} + \\frac{5}{3}) + (\\frac{5}{3}) = 8$$$. The brackets denote the inner sum $$$\\sum_{j=i}^{n}{\\frac{a_j}{j}}$$$, while the summation of brackets corresponds to the sum over $$$i$$$."}, "positive_code": [{"source_code": "gets.chomp.to_i.times do |_i|\n n, m = gets.split.map(&:to_i)\n a = gets.split.map(&:to_i)\n puts (m == a.inject(0, :+) ? 'YES' : 'NO')\nend\n"}, {"source_code": "T = gets.to_i\n\nT.times do\n n, m = gets.split.map(&:to_i)\n a = gets.split.map(&:to_i)\n\n if a.sum == m\n puts 'YES'\n else\n puts 'NO'\n end\nend\n"}, {"source_code": "t = gets.chomp.to_i\nt.times do\n n, m = gets.chomp.split(' ').map(&:to_i)\n c = gets.chomp.split(' ').map(&:to_i)\n if c.sum == m\n puts \"YES\"\n else\n puts \"NO\"\n end\nend"}, {"source_code": "t = gets.to_i\n\n1.upto t do\n n, m = gets.split.map(&:to_i)\n a = gets.split.map(&:to_i)\n\n if(a.sum == m)\n puts \"YES\"\n else\n puts \"NO\"\n end\nend\n"}, {"source_code": "test_cases = gets.chomp\n\ntest_cases.to_i.times do\n x = gets.chomp.split(\" \").map(&:to_i)[1]\n y = gets.chomp.split(\" \").map(&:to_i)\n puts y.sum == x ? \"YES\" : \"NO\"\n\nend\n"}], "negative_code": [], "src_uid": "941adee47c2a28588ebe7dfe16e0c91a"} {"nl": {"description": "Very soon Berland will hold a School Team Programming Olympiad. From each of the m Berland regions a team of two people is invited to participate in the olympiad. The qualifying contest to form teams was held and it was attended by n Berland students. There were at least two schoolboys participating from each of the m regions of Berland. The result of each of the participants of the qualifying competition is an integer score from 0 to 800 inclusive.The team of each region is formed from two such members of the qualifying competition of the region, that none of them can be replaced by a schoolboy of the same region, not included in the team and who received a greater number of points. There may be a situation where a team of some region can not be formed uniquely, that is, there is more than one school team that meets the properties described above. In this case, the region needs to undertake an additional contest. The two teams in the region are considered to be different if there is at least one schoolboy who is included in one team and is not included in the other team. It is guaranteed that for each region at least two its representatives participated in the qualifying contest.Your task is, given the results of the qualifying competition, to identify the team from each region, or to announce that in this region its formation requires additional contests.", "input_spec": "The first line of the input contains two integers n and m (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 10 000, n ≥ 2m) — the number of participants of the qualifying contest and the number of regions in Berland. Next n lines contain the description of the participants of the qualifying contest in the following format: Surname (a string of length from 1 to 10 characters and consisting of large and small English letters), region number (integer from 1 to m) and the number of points scored by the participant (integer from 0 to 800, inclusive). It is guaranteed that all surnames of all the participants are distinct and at least two people participated from each of the m regions. The surnames that only differ in letter cases, should be considered distinct.", "output_spec": "Print m lines. On the i-th line print the team of the i-th region — the surnames of the two team members in an arbitrary order, or a single character \"?\" (without the quotes) if you need to spend further qualifying contests in the region.", "sample_inputs": ["5 2\nIvanov 1 763\nAndreev 2 800\nPetrov 1 595\nSidorov 1 790\nSemenov 2 503", "5 2\nIvanov 1 800\nAndreev 2 763\nPetrov 1 800\nSidorov 1 800\nSemenov 2 503"], "sample_outputs": ["Sidorov Ivanov\nAndreev Semenov", "?\nAndreev Semenov"], "notes": "NoteIn the first sample region teams are uniquely determined.In the second sample the team from region 2 is uniquely determined and the team from region 1 can have three teams: \"Petrov\"-\"Sidorov\", \"Ivanov\"-\"Sidorov\", \"Ivanov\" -\"Petrov\", so it is impossible to determine a team uniquely."}, "positive_code": [{"source_code": "#!/usr/bin/ruby\nn,m=gets.split.map(&:to_i)\nh=Hash.new{|h,k|h[k]=[]}\nn.times{\n\ta,b,c=gets.split\n\th[b.to_i-1]<<[-c.to_i,a]\n}\nm.times{|i|\n\th[i].sort!\n\tif h[i].size<=2 || h[i][1][0]!=h[i][2][0]\n\t\tputs h[i][0,2].map(&:last)*' '\n\telse\n\t\tputs '?'\n\tend\n}"}, {"source_code": "n, m = gets.chomp.split.map(&:to_i)\nmemo = Hash.new {|h, k| h[k] = [] }\nPlayer = Struct.new(:name, :point)\n\nn.times do\n name, region, point = gets.chomp.split.map { |e| e == e.to_i.to_s ? e.to_i : e }\n memo[region] << Player.new(name, point)\nend\n\nmemo.keys.sort.each do |key|\n players = memo[key].sort_by { |p| -p.point }\n selected = players.take(2)\n\n if players.length <= 2 || players[1..2].map(&:point).uniq.length == 2\n puts selected.map(&:name).join(' ')\n else\n puts \"?\"\n end\nend\n\n"}, {"source_code": "n, m = readline.split(' ').collect(&:to_i)\narr = Array.new(m) {Array.new}\nn.times do\n name, region, mark = readline.split(' ')\n region, mark = region.to_i, mark.to_i\n arr[region - 1].push([mark, name])\nend\narr.each do |a|\n a.sort!.reverse!\n if a.length <= 2 || a[2][0] < a[1][0]\n print a[0][1], \" \", a[1][1]\n puts\n else\n puts \"?\"\n end\nend\n"}, {"source_code": "a=gets.chomp.split(\" \")\ninput=a[0].to_i\ncity=a[1].to_i\nmax2=Array.new(city){Array.new(2,0)}\nname=Array.new(city) {Hash.new { |hash, key| hash[key] = [] } }\ninput.times do \ntemp=gets.chomp.split(\" \")\ntc=temp[1].to_i\nts=temp[2].to_i\nif ts>max2[tc-1][0]\nmax2[tc-1][1]=max2[tc-1][0]\nmax2[tc-1][0]=ts\nelsif ts>max2[tc-1][1]\nmax2[tc-1][1]=ts\nend\nname[tc-1][ts]<2\nputs \"?\"\nelse\nputs name[i][max2[i][0]].join(\" \")\nend\nelse\nif name[i][max2[i][0]].length + name[i][max2[i][1]].length>2\nputs \"?\"\nelse\nprintf \"#{ name[i][max2[i][0]][0]} \"\nputs \"#{ name[i][max2[i][1]][0]}\"\nend\nend\nend\n"}, {"source_code": "a=gets.chomp.split(\" \")\ninput=a[0].to_i\ncity=a[1].to_i\nmax2=Array.new(city){Array.new(2,0)}\nname=Array.new(city) {Hash.new { |hash, key| hash[key] = [] } }\ninput.times do \ntemp=gets.chomp.split(\" \")\ntc=temp[1].to_i\nts=temp[2].to_i\nif ts>max2[tc-1][0]\nmax2[tc-1][1]=max2[tc-1][0]\nmax2[tc-1][0]=ts\nelsif ts>max2[tc-1][1]\nmax2[tc-1][1]=ts\nend\nname[tc-1][ts]<2\nputs \"?\"\nelse\nputs name[i][max2[i][0]].join(\" \")\nend\nelse\nif name[i][max2[i][0]].length + name[i][max2[i][1]].length>2\nputs \"?\"\nelse\nprintf \"#{ name[i][max2[i][0]][0]} \"\nputs \"#{ name[i][max2[i][1]][0]}\"\nend\nend\nend\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\na = Array.new(m).map{Array.new}\nn.times do\n na, re, sc = gets.split\n re = re.to_i - 1\n sc = sc.to_i\n a[re].push([sc, na])\nend\n\nm.times do |i|\n t = a[i].sort{ |x, y| y[0] <=> x[0] }\n if t.length < 3\n puts \"#{t[0][1]} #{t[1][1]}\"\n elsif t[1][0] == t[2][0]\n puts \"?\"\n else\n puts \"#{t[0][1]} #{t[1][1]}\"\n end\nend\n"}, {"source_code": "def update(g, str, score)\n\tif !$a[g][0] || score >= $a[g][0]\n\t\t$a[g][2] = $a[g][1]\n\t\t$a[g][1] = $a[g][0]\n\t\t$a[g][0] = score\n\t\t$name[g][2] = $name[g][1]\n\t\t$name[g][1] = $name[g][0]\n\t\t$name[g][0] = str\n\telsif !$a[g][1] || score >= $a[g][1]\n\t\t$a[g][2] = $a[g][1]\n\t\t$a[g][1] = score\n\t\t$name[g][2] = $name[g][1]\n\t\t$name[g][1] = str\n\telsif !$a[g][2] || score >= $a[g][2]\n\t\t$a[g][2] = score\n\t\t$name[g][2] = str\n\tend\nend\n\nbuf=gets.split(' ')\nn,m=buf[0].to_i, buf[1].to_i\n$a=Array.new(m+1)\n$name=Array.new(m+1)\nfor i in 1..m\n\t$a[i]=Array.new(3)\n\t$name[i]=Array.new(3)\nend\nfor i in 1..n\n\tbuf=gets.split(' ')\n\tupdate(buf[1].to_i, buf[0], buf[2].to_i)\nend\nfor i in 1..m\n\tif $a[i][1] == $a[i][2]\n\t\tputs '?'\n\telse\n\t\tputs $name[i][0] + ' ' + $name[i][1]\n\tend\nend"}, {"source_code": "n, m = gets.chomp.split(' ').map(&:to_i)\n\nregion = Array.new(m) { |r| r = [] }\nans = Array.new(m) { |r| r = [] }\nn.times do\n info = gets.chomp.split(' ')\n region[info[1].to_i - 1] << [info[0], info[2].to_i]\nend\n\nregion.each do |r|\n r.sort! { |a, b| if a[1] > b[1] then -1 elsif a[1] < b[1] then 1 else 0 end }\nend\n\nregion.each_with_index do |r, i|\n sameWithMax = r.count { |p| p[1] == r[0][1] }\n sameWithSubSequent = 0\n \n if (sameWithMax < r.length)\n sameWithSubSequent = r.count { |p| p[1] == r[sameWithMax][1] }\n end\n \n if (sameWithMax >= 3 || (sameWithMax < 2 && sameWithMax + sameWithSubSequent >= 3))\n ans[i] << '?'\n else\n ans[i] << \"#{r[0][0]} #{r[1][0]}\"\n end\nend\n\nans.each do |a|\n puts a\nend\n "}], "negative_code": [{"source_code": "n, m = gets.chomp.split.map(&:to_i)\nmemo = Hash.new {|h, k| h[k] = [] }\nPlayer = Struct.new(:name, :point)\n\nn.times do\n name, region, point = gets.chomp.split.map { |e| e == e.to_i.to_s ? e.to_i : e }\n memo[region] << Player.new(name, point)\nend\n\nmemo.keys.sort.each do |key|\n players = memo[key].sort_by { |p| -p.point }\n selected = players.take(2)\n\n if players.length <= 2 || players[1..2].map(&:point).uniq == 1\n puts selected.map(&:name).join(' ')\n else\n puts \"?\"\n end\nend\n\n"}, {"source_code": "n, m = gets.chomp.split.map(&:to_i)\nmemo = Hash.new {|h, k| h[k] = [] }\nPlayer = Struct.new(:name, :point)\n\nn.times do\n name, region, point = gets.chomp.split.map { |e| e == e.to_i.to_s ? e.to_i : e }\n memo[region] << Player.new(name, point)\nend\n\nmemo.keys.sort.each do |key|\n players = memo[key]\n max = players.map(&:point).max\n max_players = players.select { |p| p.point == max }\n selected = players.sort_by { |p| -p.point }.take(2)\n if max_players.length > 2 || selected.map(&:name).uniq.length == 1\n puts \"?\"\n else\n puts selected.map(&:name).join(' ')\n end\nend\n\n"}, {"source_code": "n, m = gets.chomp.split.map(&:to_i)\nmemo = Hash.new {|h, k| h[k] = [] }\nPlayer = Struct.new(:name, :point)\n\nn.times do\n name, region, point = gets.chomp.split.map { |e| e == e.to_i.to_s ? e.to_i : e }\n memo[region] << Player.new(name, point)\nend\n\nmemo.keys.sort.each do |key|\n players = memo[key]\n max = players.map(&:point).max\n if players.map(&:point).select { |p| p == max }.length > 2\n puts \"?\"\n else\n puts players.sort_by { |p| -p.point }.take(2).map(&:name).join(' ')\n end\nend\n\n"}, {"source_code": "n, m = gets.chomp.split(' ').map(&:to_i)\n\nregion = Array.new(m) { |r| r = [] }\nans = Array.new(m) { |r| r = [] }\nn.times do\n info = gets.chomp.split(' ')\n region[info[1].to_i - 1] << [info[0], info[2].to_i]\nend\n\nregion.each do |r|\n r.sort! { |a, b| if a[1] > b[1] then -1 elsif a[1] < b[1] then 1 else 0 end }\nend\n\nregion.each_with_index do |r, i|\n sameWithMax = r.count { |p| p[1] == r[0][1] }\n sameWithSubSequent = 0\n \n if (sameWithMax < r.length)\n sameWithSubSequent = r.count { |p| p[1] == r[sameWithMax][1] }\n end\n \n if (sameWithMax + sameWithSubSequent >= 3)\n ans[i] << '?'\n else\n ans[i] << \"#{r[0][0]} #{r[1][0]}\"\n end\nend\n\nans.each do |a|\n puts a\nend\n "}], "src_uid": "a1ea9eb8db25289958a6f730c555362f"} {"nl": {"description": "You are given a permutation of length $$$n$$$. Recall that the permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2, 3, 1, 5, 4]$$$ is a permutation, but $$$[1, 2, 2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1, 3, 4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).You can perform at most $$$n-1$$$ operations with the given permutation (it is possible that you don't perform any operations at all). The $$$i$$$-th operation allows you to swap elements of the given permutation on positions $$$i$$$ and $$$i+1$$$. Each operation can be performed at most once. The operations can be performed in arbitrary order.Your task is to find the lexicographically minimum possible permutation obtained by performing some of the given operations in some order.You can see the definition of the lexicographical order in the notes section.You have to answer $$$q$$$ independent test cases.For example, let's consider the permutation $$$[5, 4, 1, 3, 2]$$$. The minimum possible permutation we can obtain is $$$[1, 5, 2, 4, 3]$$$ and we can do it in the following way: perform the second operation (swap the second and the third elements) and obtain the permutation $$$[5, 1, 4, 3, 2]$$$; perform the fourth operation (swap the fourth and the fifth elements) and obtain the permutation $$$[5, 1, 4, 2, 3]$$$; perform the third operation (swap the third and the fourth elements) and obtain the permutation $$$[5, 1, 2, 4, 3]$$$. perform the first operation (swap the first and the second elements) and obtain the permutation $$$[1, 5, 2, 4, 3]$$$; Another example is $$$[1, 2, 4, 3]$$$. The minimum possible permutation we can obtain is $$$[1, 2, 3, 4]$$$ by performing the third operation (swap the third and the fourth elements).", "input_spec": "The first line of the input contains one integer $$$q$$$ ($$$1 \\le q \\le 100$$$) — the number of test cases. Then $$$q$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 \\le n \\le 100$$$) — the number of elements in the permutation. The second line of the test case contains $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ — the given permutation.", "output_spec": "For each test case, print the answer on it — the lexicograhically minimum possible permutation obtained by performing some of the given operations in some order.", "sample_inputs": ["4\n5\n5 4 1 3 2\n4\n1 2 4 3\n1\n1\n4\n4 3 2 1"], "sample_outputs": ["1 5 2 4 3 \n1 2 3 4 \n1 \n1 4 3 2"], "notes": "NoteRecall that the permutation $$$p$$$ of length $$$n$$$ is lexicographically less than the permutation $$$q$$$ of length $$$n$$$ if there is such index $$$i \\le n$$$ that for all $$$j$$$ from $$$1$$$ to $$$i - 1$$$ the condition $$$p_j = q_j$$$ is satisfied, and $$$p_i < q_i$$$. For example: $$$p = [1, 3, 5, 2, 4]$$$ is less than $$$q = [1, 3, 5, 4, 2]$$$ (such $$$i=4$$$ exists, that $$$p_i < q_i$$$ and for each $$$j < i$$$ holds $$$p_j = q_j$$$), $$$p = [1, 2]$$$ is less than $$$q = [2, 1]$$$ (such $$$i=1$$$ exists, that $$$p_i < q_i$$$ and for each $$$j < i$$$ holds $$$p_j = q_j$$$). "}, "positive_code": [{"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\nq = inp[0]\nq.times do\n n = inp[0]\n t = inp\n m = na(n,0)\n n.times do |i|\n m[t[i]-1] = i\n end\n head = 0\n n.times do |num_i|\n if m[num_i] > head\n ((head+1)..m[num_i]).reverse_each do |i|\n t[i] = t[i-1]\n m[t[i]-1] = i\n end\n t[head] = num_i + 1\n head = m[num_i]\n elsif m[num_i] == head\n head += 1\n end\n break if head >= (n-1)\n end\n puts t.join(\" \")\nend\n"}, {"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n \ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n \ndef inps() a=gets.chomp.split(\" \")end\n \ndef copy(a) Marshal.load(Marshal.dump(a)) end\n \ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n \ndef na(n,d=0) Array.new(n,d)end\n \ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n \ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n \ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n \ndef r_up(a, b) (a+b-1)/b end\n \ndef sum(a) a.inject(:+) end\n \nq = inp[0]\nq.times do\n n = inp[0]\n t = inp\n m = na(n,0)\n n.times do |i|\n m[t[i]-1] = i\n end\n head = 0\n n.times do |num_i|\n if m[num_i] > head\n while(t[head] <= num_i)\n head += 1\n end\n ((head+1)..m[num_i]).reverse_each do |i|\n t[i] = t[i-1]\n m[t[i]-1] += 1\n end\n t[head] = num_i + 1\n head = m[num_i]\n end\n break if head >= (n-1)\n end\n puts t.join(\" \")\nend"}, {"source_code": "def inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\nq = inp[0]\nq.times do\n n = inp[0]\n t = inp\n m = na(n,0)\n n.times do |i|\n m[t[i]-1] = i\n end\n head = 0\n n.times do |num_i|\n if m[num_i] > head\n ((head+1)..m[num_i]).reverse_each do |i|\n t[i] = t[i-1]\n m[t[i]-1] += 1\n end\n t[head] = num_i + 1\n head = m[num_i]\n elsif m[num_i] == head\n head += 1\n end\n break if head >= (n-1)\n end\n puts t.join(\" \")\nend"}], "negative_code": [{"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\nq = inp[0]\nq.times do\n n = inp[0]\n t = inp\n m = na(n,0)\n n.times do |i|\n m[t[i]-1] = i\n end\n head = 0\n n.times do |num_i|\n if m[num_i] > head\n ((head+1)..m[num_i]).reverse_each do |i|\n t[i] = t[i-1]\n end\n t[head] = num_i + 1\n head = m[num_i]\n elsif (m[num_i] == head)\n head += 1\n end\n break if head == (n-1)\n end\n puts t.join(\" \")\nend\n"}, {"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\nq = inp[0]\nq.times do\n n = inp[0]\n t = inp\n m = na(n,0)\n n.times do |i|\n m[t[i]-1] = i\n end\n head = 0\n barrier = 0\n n.times do |num_i|\n if m[num_i] > head\n ((head+1+barrier)..m[num_i]).reverse_each do |i|\n t[i] = t[i-1]\n end\n t[head] = num_i + 1\n head = m[num_i]\n elsif (m[num_i] == head)\n head += 1\n barrier = 0\n elsif (m[num_i] == head - 1)\n barrier = 1\n end\n break if head == (n-1)\n end\n puts t.join(\" \")\nend\n"}, {"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\nq = inp[0]\nq.times do\n n = inp[0]\n t = inp\n m = na(n,0)\n n.times do |i|\n m[t[i]-1] = i\n end\n head = 0\n n.times do |num_i|\n if m[num_i] > head\n ((head+1)..m[num_i]).reverse_each do |i|\n t[i] = t[i-1]\n m[t[i]-1] = i\n end\n t[head] = num_i + 1\n head = m[num_i]\n else\n head = m[num_i] + 1\n end\n break if head >= (n-1)\n end\n puts t.join(\" \")\nend\n"}, {"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\nq = inp[0]\nq.times do\n n = inp[0]\n t = inp\n m = na(n,0)\n n.times do |i|\n m[t[i]-1] = i\n end\n head = 0\n n.times do |num_i|\n if m[num_i] > head\n while(t[head] <= num_i and head= (n-1)\n end\n puts t.join(\" \")\nend\n"}, {"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\nq = inp[0]\nq.times do\n n = inp[0]\n t = inp\n m = na(n,0)\n n.times do |i|\n m[t[i]-1] = i\n end\n head = 0\n n.times do |num_i|\n if m[num_i] > head\n while(t[head] <= num_i and head<(num_i-1))\n head += 1\n end\n ((head+1)..m[num_i]).reverse_each do |i|\n t[i] = t[i-1]\n m[t[i]-1] += 1\n end\n t[head] = num_i + 1\n head = m[num_i]\n end\n break if head >= (n-1)\n end\n puts t.join(\" \")\nend"}, {"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\nq = inp[0]\nq.times do\n n = inp[0]\n t = inp\n m = na(n,0)\n n.times do |i|\n m[t[i]-1] = i\n end\n head = 0\n n.times do |num_i|\n if m[num_i] > head\n while(m[head] <= num_i and head= (n-1)\n end\n puts t.join(\" \")\nend\n"}, {"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\nq = inp[0]\nq.times do\n n = inp[0]\n t = inp\n m = na(n,0)\n n.times do |i|\n m[t[i]-1] = i\n end\n head = 0\n barrier = false\n n.times do |num_i|\n if m[num_i] > head\n ((head+1)..m[num_i]).reverse_each do |i|\n t[i] = t[i-1]\n end\n t[head] = num_i + 1\n head = m[num_i]\n elsif (m[num_i] == head or m[num_i] == head - 1)\n head += 1\n end\n break if head == (n-1)\n end\n puts t.join(\" \")\nend\n"}, {"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\nq = inp[0]\nq.times do\n n = inp[0]\n t = inp\n m = na(n,0)\n n.times do |i|\n m[t[i]-1] = i\n end\n head = 0\n n.times do |num_i|\n if m[num_i] > head\n ((head+1)..m[num_i]).reverse_each do |i|\n t[i] = t[i-1]\n end\n t[head] = num_i + 1\n head = m[num_i]\n break if head == (n-1)\n elsif (m[num_i] == head)\n head += 1\n end\n end\n puts t.join(\" \")\nend\n"}, {"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\nq = inp[0]\nq.times do\n n = inp[0]\n t = inp\n m = na(n,0)\n n.times do |i|\n m[t[i]-1] = i\n end\n head = 0\n barrier = 0\n n.times do |num_i|\n if m[num_i] > head\n ((head+1+barrier)..m[num_i]).reverse_each do |i|\n t[i] = t[i-1]\n end\n t[head+barrier] = num_i + 1\n head = m[num_i]\n elsif (m[num_i] == head or m[num_i] == head - 1)\n barrier = 1\n end\n break if head == (n-1)\n end\n puts t.join(\" \")\nend\n"}, {"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\nq = inp[0]\nq.times do\n n = inp[0]\n t = inp\n m = na(n,0)\n n.times do |i|\n m[t[i]-1] = i\n end\n head = 0\n n.times do |num_i|\n if m[num_i] > head\n ((head+1)..m[num_i]).reverse_each do |i|\n t[i] = t[i-1]\n m[t[i]-1] = i\n end\n t[head] = num_i + 1\n head = m[num_i]\n else\n head += 1\n end\n break if head >= (n-1)\n end\n puts t.join(\" \")\nend\n"}, {"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\nq = inp[0]\nq.times do\n n = inp[0]\n t = inp\n m = na(n,0)\n n.times do |i|\n m[t[i]-1] = i\n end\n head = 0\n n.times do |num_i|\n if m[num_i] > head\n while(t[head] <= num_i and head= (n-1)\n end\n puts t.join(\" \")\nend\n"}, {"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\nq = inp[0]\nq.times do\n n = inp[0]\n t = inp\n m = na(n,0)\n n.times do |i|\n m[t[i]-1] = i\n end\n head = 0\n barrier = 0\n n.times do |num_i|\n if m[num_i] > head\n ((head+1+barrier)..m[num_i]).reverse_each do |i|\n t[i] = t[i-1]\n end\n t[head+barrier] = num_i + 1\n head = m[num_i]\n barrier = 0\n elsif (m[num_i] == head or m[num_i] == head - 1)\n barrier = 1\n end\n break if head == (n-1)\n end\n puts t.join(\" \")\nend\n"}], "src_uid": "c7a7e90bc54b2d21b3f59a358d9d1410"} {"nl": {"description": "Vasya and Petya have invented a new game. Vasya takes a stripe consisting of 1 × n square and paints the squares black and white. After that Petya can start moves — during a move he may choose any two neighboring squares of one color and repaint these two squares any way he wants, perhaps in different colors. Petya can only repaint the squares in white and black colors. Petya’s aim is to repaint the stripe so that no two neighboring squares were of one color. Help Petya, using the given initial coloring, find the minimum number of moves Petya needs to win.", "input_spec": "The first line contains number n (1 ≤ n ≤ 1000) which represents the stripe’s length. The second line contains exactly n symbols — the line’s initial coloring. 0 corresponds to a white square, 1 corresponds to a black one.", "output_spec": "If Petya cannot win with such an initial coloring, print -1. Otherwise print the minimum number of moves Petya needs to win.", "sample_inputs": ["6\n111010", "5\n10001", "7\n1100010", "5\n00100"], "sample_outputs": ["1", "1", "2", "2"], "notes": "NoteIn the first sample Petya can take squares 1 and 2. He repaints square 1 to black and square 2 to white.In the second sample Petya can take squares 2 and 3. He repaints square 2 to white and square 3 to black."}, "positive_code": [{"source_code": "a=n=gets.to_i;s=gets\nn.times{|i|a-=i%2^s[i].hex}\np [a,n-a].min\n\n"}, {"source_code": "a=n=gets.to_i;s=gets\nn.times{|i|a-=i%2^s[i].hex}\np [a,n-a].min\n\n"}, {"source_code": "a=n=gets.to_i;s=gets\nn.times{|i|a-=i%2^s[i].hex}\np [a,n-a].min\n\n"}, {"source_code": "a=n=gets.to_i;s=gets\nn.times{|i|a-=i%2^s[i].hex}\np [a,n-a].min\n\n"}, {"source_code": "a=n=gets.to_i;s=gets\nn.times{|i|a-=i%2^s[i].hex}\np [a,n-a].min"}, {"source_code": "a=[0,0]\ngets.to_i.times{|i|a[STDIN.read(1).hex^i%2]+=1}\np a.min\n"}, {"source_code": "a=[0,0]\ngets.to_i.times{|i|c=STDIN.read(1).ord-48;a[(c+i)%2]+=1}\np a.min\n"}, {"source_code": "a=[0,0]\ngets.to_i.times{|i|a[(STDIN.read(1).ord-48+i)%2]+=1}\np a.min\n"}, {"source_code": "a=n=gets.to_i;s=gets\nn.times{|i|a-=i%2^s[i].hex}\np [a,n-a].min\n\n"}, {"source_code": "n=gets.to_i;s=gets;a=0\nn.times{|i|a+=(i%2)^s[i].to_i}\np [a,n-a].min\n"}, {"source_code": "a=n=gets.to_i;s=gets\nn.times{|i|a-=i%2^s[i].hex}\np [a,n-a].min\n"}, {"source_code": "n=gets.to_i;s=gets;a=0\nn.times{|i|a+=i%2^s[i].to_i}\np [a,n-a].min"}, {"source_code": "a=n=gets.to_i;s=gets\nn.times{|i|a-=i%2^s[i].hex}\np [a,n-a].min"}, {"source_code": "a=n=gets.to_i;s=gets\nn.times{|i|a-=i%2^s[i].hex}\np [a,n-a].min\n"}, {"source_code": "a=n=gets.to_i;s=gets\nn.times{|i|a-=i%2^s[i].hex}\np [a,n-a].min\n\n"}, {"source_code": "a=n=gets.to_i;s=gets\nn.times{|i|a-=i%2^s[i].hex}\np [a,n-a].min\n\n"}, {"source_code": "a=n=gets.to_i;s=gets\nn.times{|i|a-=i%2^s[i].hex}\np [a,n-a].min\n\n"}], "negative_code": [], "src_uid": "7c313b911e644cd5efa4fdc31d7ffcc9"} {"nl": {"description": "You are given a 4x4 grid. You play a game — there is a sequence of tiles, each of them is either 2x1 or 1x2. Your task is to consequently place all tiles from the given sequence in the grid. When tile is placed, each cell which is located in fully occupied row or column is deleted (cells are deleted at the same time independently). You can place tile in the grid at any position, the only condition is that tiles (and tile parts) should not overlap. Your goal is to proceed all given figures and avoid crossing at any time.", "input_spec": "The only line contains a string $$$s$$$ consisting of zeroes and ones ($$$1 \\le |s| \\le 1000$$$). Zero describes vertical tile, one describes horizontal tile.", "output_spec": "Output $$$|s|$$$ lines — for each tile you should output two positive integers $$$r,c$$$, not exceeding $$$4$$$, representing numbers of smallest row and column intersecting with it. If there exist multiple solutions, print any of them.", "sample_inputs": ["010"], "sample_outputs": ["1 1\n1 2\n1 4"], "notes": "NoteFollowing image illustrates the example after placing all three tiles: Then the first row is deleted: "}, "positive_code": [{"source_code": "# Input\ns = gets.strip\n\n# TACTIC:\n# We will place all the horizontal tiles in top row, so every second horizontal\n# tile will make this row clear again.\n# All the vertical tiles will be placed in last two rows (3 1, 3 2, 3 3, 3 4),\n# so ever y 4th tile will clear both this lines.\n# Such tactic must guarantee that we will be able to place any tile any time.\n\n# Processing\nresult = []\nh_count, v_count = 0, 0\n\ns.each_char do |chr|\n if chr == \"1\"\n # Horizontal tile\n result << \"1 #{h_count * 2 + 1}\"\n h_count = (h_count + 1) % 2\n else\n # Vertical tile\n result << \"3 #{v_count + 1}\"\n v_count = (v_count + 1) % 4\n end\nend\n\n# Output\nputs result\n"}, {"source_code": "str = gets.strip\n\ngrid = Array.new(5) { Array.new(5, 0) }\n\nseq = []\nstr.each_char do |c|\n if c == '0'\n if grid[1][1] == 0\n grid[1][1] = 1\n seq << [1, 1]\n elsif grid[1][2] == 0\n grid[1][2] = 1\n seq << [1, 2]\n elsif grid[1][3] == 0\n grid[1][3] = 1\n seq << [1, 3]\n elsif grid[1][4] == 0\n seq << [1, 4]\n grid[1][1] = 0\n grid[1][2] = 0\n grid[1][3] = 0\n end \n else\n if grid[3][1] == 0\n seq << [3, 1]\n grid[3][1] = 1\n else\n seq << [3, 3]\n grid[3][1] = 0\n end\n end\nend\n\nseq.each do |pt|\n puts pt.join(\" \")\nend"}, {"source_code": "s = gets.chomp.split('').map(&:to_i)\nN = 4\narr = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]\ns.each do |c|\n arr.each_with_index do |row, i|\n f = true\n row.each do |ee|\n f &= ee == 1\n end\n if (f)\n arr[i] = [0, 0, 0, 0]\n end\n end\n N.times do |i|\n f = true\n N.times do |ii|\n f &= arr[ii][i] == 1\n end\n if (f)\n arr[0][i] = 0\n arr[1][i] = 0\n arr[2][i] = 0\n arr[3][i] = 0\n end\n end\n if (c == 0)\n b = false\n N.times do |row|\n break if b\n N.times do |col|\n if (arr[col] && arr[col][row] == 0 && arr[col + 1] && arr[col + 1][row] == 0)\n arr[col][row] = 1\n arr[col + 1][row] = 1\n puts [col + 1, row + 1].join(' ')\n b = true\n break\n end\n end\n end\n end\n if (c == 1)\n b = false\n N.times do |row|\n break if b\n N.times do |col|\n #arr[1][0] arr[1][1]\n if (arr[row] && arr[row][col] == 0 && arr[row] && arr[row][col + 1] == 0)\n arr[row][col] = 1\n arr[row][col + 1] = 1\n puts [row + 1, col + 1].join(' ')\n b = true\n break\n end\n end\n end\n end\nend"}, {"source_code": "s = gets.chomp\na = [[\"3 1\",\"3 2\",\"3 3\",\"3 4\"],[\"1 1\",\"1 3\",\"2 1\",\"2 3\"]]\nb = [0,0]\ns.size.times do |i|\n x = s[i].to_i\n puts a[x][b[x]]\n b[x] += 1\n b[x] %= 4\nend"}, {"source_code": "DBG = true\ns = gets.chomp\nst = 0\nfor i in 0...s.size\n\n if st == 0\n if s[i] == '0'\n puts \"3 4\"\n st = 2\n else\n puts \"1 1\"\n st = 1\n end\n\n elsif st == 1\n if s[i] == '0'\n puts \"3 4\"\n st = 3\n else\n puts \"1 3\"\n st = 0\n end\n\n elsif st == 2\n if s[i] == '0'\n puts \"1 4\"\n st = 0\n else\n puts \"1 1\"\n st = 3\n end\n\n else #if st == 3\n if s[i] == '0'\n puts \"1 4\"\n st = 1\n else\n puts \"1 3\"\n st = 2\n end\n end\nend\n"}], "negative_code": [{"source_code": "s = gets.chomp.split('').map(&:to_i)\nN = 4\narr = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]\ns.each do |c|\n arr.each_with_index do |row, i|\n f = true\n row.each do |ee|\n f &= ee == 1\n end\n if (f)\n arr[i] = [0, 0, 0, 0]\n end\n end\n N.times do |i|\n f = true\n N.times do |ii|\n f &= arr[ii][i] == 1\n end\n if (f)\n arr[0][i] = 0\n arr[1][i] = 0\n arr[2][i] = 0\n arr[3][i] = 0\n end\n end\n if (c == 0)\n b = false\n N.times do |row|\n break if b\n N.times do |col|\n #arr[0][1] arr[1][1]\n if (arr[col][row] == 0 && arr[col + 1][row] == 0)\n arr[col][row] = 1\n arr[col + 1][row] = 1\n puts [row + 1, col + 1].join(' ')\n b = true\n break\n end\n end\n end\n end\n if (c == 1)\n b = false\n N.times do |row|\n break if b\n N.times do |col|\n #arr[1][0] arr[1][1]\n if (arr[row][col] == 0 && arr[row][col + 1] == 0)\n arr[row][col] = 1\n arr[row][col + 1] = 1\n puts [col+ 1, row + 1].join(' ')\n b = true\n break\n end\n end\n end\n end\nend"}], "src_uid": "a63cdbd1009a60c0f9b52e4ffbba252e"} {"nl": {"description": "Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.Therefore, Sasha decided to upsolve the following problem:You have an array $$$a$$$ with $$$n$$$ integers. You need to count the number of funny pairs $$$(l, r)$$$ $$$(l \\leq r)$$$. To check if a pair $$$(l, r)$$$ is a funny pair, take $$$mid = \\frac{l + r - 1}{2}$$$, then if $$$r - l + 1$$$ is an even number and $$$a_l \\oplus a_{l+1} \\oplus \\ldots \\oplus a_{mid} = a_{mid + 1} \\oplus a_{mid + 2} \\oplus \\ldots \\oplus a_r$$$, then the pair is funny. In other words, $$$\\oplus$$$ of elements of the left half of the subarray from $$$l$$$ to $$$r$$$ should be equal to $$$\\oplus$$$ of elements of the right half. Note that $$$\\oplus$$$ denotes the bitwise XOR operation.It is time to continue solving the contest, so Sasha asked you to solve this task.", "input_spec": "The first line contains one integer $$$n$$$ ($$$2 \\le n \\le 3 \\cdot 10^5$$$) — the size of the array. The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$0 \\le a_i < 2^{20}$$$) — array itself.", "output_spec": "Print one integer — the number of funny pairs. You should consider only pairs where $$$r - l + 1$$$ is even number.", "sample_inputs": ["5\n1 2 3 4 5", "6\n3 2 2 3 7 6", "3\n42 4 2"], "sample_outputs": ["1", "3", "0"], "notes": "NoteBe as cool as Sasha, upsolve problems!In the first example, the only funny pair is $$$(2, 5)$$$, as $$$2 \\oplus 3 = 4 \\oplus 5 = 1$$$.In the second example, funny pairs are $$$(2, 3)$$$, $$$(1, 4)$$$, and $$$(3, 6)$$$.In the third example, there are no funny pairs."}, "positive_code": [{"source_code": "n = gets.to_i\na = gets.chomp.split(\" \").map(&:to_i)\n\ndp=[]\ncurrXorSum = 0\n(0..n-1).each do |i|\n\tcurrXorSum = currXorSum^a[i]\n\tdp.push currXorSum\nend\n\narr1 = [Hash.new(0),Hash.new(0)]\ndp.insert(0,0)\n\n(0..n).each do |i|\n\tarr1[i%2][dp[i]] += 1\nend\n\nans = 0\narr1[0].each do |k,ct|\n\tans += ((ct-1)*ct)/2\nend\n\narr1[1].each do |k,ct|\n\tans += ((ct-1)*ct)/2\nend\n\nputs ans"}, {"source_code": "if File.exists?(\"input\")\n $stdin = File.open(\"input\")\n $stdout = File.open(\"output\",\"w\")\nend\n\nn = gets.to_i\narr = gets.chomp.split(\" \").map(&:to_i)\n\nxorarr=[]\na = 0\n(0..n-1).each do |i|\n\ta = a^arr[i]\n\txorarr.push a\nend\n\nevenhash = Hash.new(0)\noddhash = Hash.new(0)\nxorarr.insert(0,0)\n\n(0..n).each do |i|\n\tif(i%2)==0\n\t\tevenhash[xorarr[i]]+=1\n\telse\n\t\toddhash[xorarr[i]]+=1\n\tend\nend\n\n#p evenhash\n#p oddhash\n\n=begin\np xorarr\np evenhash\np oddhash\n=end\nans = 0\nevenhash.each do |k,v|\n\tans+=(v*(v-1))/2\nend\n\noddhash.each do |k,v|\n\tans+=(v*(v-1))/2\nend\n\nputs ans"}], "negative_code": [], "src_uid": "e9cf26e61ebff8ad8d3b857c429d3aa9"} {"nl": {"description": "For his birthday, Kevin received the set of pairwise distinct numbers $$$1, 2, 3, \\ldots, n$$$ as a gift.He is going to arrange these numbers in a way such that the minimum absolute difference between two consecutive numbers be maximum possible. More formally, if he arranges numbers in order $$$p_1, p_2, \\ldots, p_n$$$, he wants to maximize the value $$$$$$\\min \\limits_{i=1}^{n - 1} \\lvert p_{i + 1} - p_i \\rvert,$$$$$$ where $$$|x|$$$ denotes the absolute value of $$$x$$$.Help Kevin to do that.", "input_spec": "Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. Description of the test cases follows. The only line of each test case contains an integer $$$n$$$ ($$$2 \\le n \\leq 1\\,000$$$) — the size of the set.", "output_spec": "For each test case print a single line containing $$$n$$$ distinct integers $$$p_1, p_2, \\ldots, p_n$$$ ($$$1 \\le p_i \\le n$$$) describing the arrangement that maximizes the minimum absolute difference of consecutive elements. Formally, you have to print a permutation $$$p$$$ which maximizes the value $$$\\min \\limits_{i=1}^{n - 1} \\lvert p_{i + 1} - p_i \\rvert$$$. If there are multiple optimal solutions, print any of them.", "sample_inputs": ["2\n\n4\n\n3"], "sample_outputs": ["2 4 1 3\n1 2 3"], "notes": "NoteIn the first test case the minimum absolute difference of consecutive elements equals $$$\\min \\{\\lvert 4 - 2 \\rvert, \\lvert 1 - 4 \\rvert, \\lvert 3 - 1 \\rvert \\} = \\min \\{2, 3, 2\\} = 2$$$. It's easy to prove that this answer is optimal.In the second test case each permutation of numbers $$$1, 2, 3$$$ is an optimal answer. The minimum absolute difference of consecutive elements equals to $$$1$$$."}, "positive_code": [{"source_code": "=begin\n\nWelcome to GDB Online.\nGDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,\nC#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.\nCode, Compile, Run and Debug online from anywhere in world.\n\n=end\n\nt=gets.to_i\n\nfor i in 1..t\n n =gets.to_i\n for j in 1..n/2\n print(n/2+j, \" \")\n print(j, \" \")\n end\n if n%2==1\n print(n, \" \")\n end\n print(\"\\n\")\nend"}], "negative_code": [{"source_code": "=begin\n\nWelcome to GDB Online.\nGDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,\nC#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.\nCode, Compile, Run and Debug online from anywhere in world.\n\n=end\n\nt=gets.to_i\n\nfor i in 1..t\n n =gets.to_i\n for j in 1..n/2\n print(n/2+j+n%2, \" \")\n print(j, \" \")\n end\n if n%2==1\n print(n/2+1, \" \")\n end\n print(\"\\n\")\nend"}], "src_uid": "0d5f4320fc2c7662d21e09a51baf21db"} {"nl": {"description": "Recently Bob invented a new game with a tree (we should remind you, that a tree is a connected graph without cycles): he deletes any (possibly, zero) amount of edges of the tree, and counts the product of sizes of the connected components left after the deletion. Your task is to find out the maximum number that Bob can get in his new game for a given tree.", "input_spec": "The first input line contains integer number n (1 ≤ n ≤ 700) — amount of vertices in the tree. The following n - 1 lines contain the description of the edges. Each line contains the pair of vertices' indexes, joined by an edge, ai, bi (1 ≤ ai, bi ≤ n). It's guaranteed that the graph described in the input is a tree.", "output_spec": "Output the only number — the maximum product of sizes of the connected components, that Bob can get after deleting some of the tree's edges.", "sample_inputs": ["5\n1 2\n2 3\n3 4\n4 5", "8\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n6 8", "3\n1 2\n1 3"], "sample_outputs": ["6", "18", "3"], "notes": null}, "positive_code": [{"source_code": "def gao(v, p)\n\t$e[v].delete(p)\n\tn, x, y, z = $e[v].size, 1, 1, 1\n\tdp = [1] + [0] * n\n\t$e[v].each do |w|\n\t\ta, b, c = gao(w, v)\n\t\tx *= a\n\t\ty = [y, Rational(b, a)].max\n\t\tz *= a\n\t\tn.downto(1) do |i|\n\t\t\tdp[i] = [dp[i - 1] * c, dp[i] * a].max\n\t\tend\n\t\tdp[0] *= a\n\tend\n\tx, y = (x * y).to_i, 0\n\t0.upto(n) do |i|\n\t\tx = [x, dp[i] * (i + 1)].max\n\t\ty = [y, dp[i] * (i + 2)].max\n\tend\n\treturn x, y, z\nend\n\nn = gets.to_i\n$e = Array.new(n){[]}\n(n - 1).times do\n\ta, b = gets.split.map{|_| _.to_i - 1}\n\t$e[a] << b\n\t$e[b] << a\nend\np gao(0, -1)[0]\n"}, {"source_code": "N = gets.to_i\nadj = Array.new(N) { Array.new }\n(N - 1).times {\n a, b = gets.split.map { |x| x.to_i - 1 }\n adj[a] << b\n adj[b] << a\n}\n\ndp = Array.new(N) { Array.new 2, 0 }\ndef dfs u, fa, adj, dp\n return dp[u] if dp[u][0] != 0\n dp[u] = [1, 1]\n ch = adj[u].reject { |v| v == fa }\n return dp[u] if ch.size == 0\n dp[u][0] = ch.map { |v| dfs(v, u, adj, dp)[1] } .reduce(1) { |o, v| o * v }\n f, l = dp[u][0], 1\n dp[u][1] = ch.map { |v| # left one right one\n dfs v, u, adj, dp\n } .sort { |x, y|\n x[1] * y[0] <=> x[0] * y[1]\n } .map { |a|\n l += 1\n f *= l\n f *= a[0]\n f /= a[1]\n f /= l - 1\n f\n } .max\n dp[u][1] = [\n dp[u][1],\n ch.map { |c|\n f, l = dp[u][0] * 2 * dfs(c, u, adj, dp)[0] / dfs(c, u, adj, dp)[1], 2\n adj[c].reject { |q| q == u } .map { |gc|\n dfs gc, c, adj, dp\n } .sort { |x, y|\n x[1] * y[0] <=> x[0] * y[1]\n } .map { |a|\n l += 1\n f *= l\n f *= a[0]\n f /= a[1]\n f /= l - 1\n f\n } .max || 1\n } .max || 1\n ].max\n return dp[u]\nend\n\ndfs 0, -1, adj, dp\np dp[0][1]\n"}], "negative_code": [{"source_code": "def gao(v, p)\n\t$e[v].delete(p)\n\tn, x, y = $e[v].size, 1, 1\n\tdp = [1] + [0] * n\n\t$e[v].each do |w|\n\t\ta, b = gao(w, v)\n\t\tx *= a\n\t\ty = [y, Rational(b, a)].max\n\t\tn.downto(1) do |i|\n\t\t\tdp[i] = [dp[i - 1], dp[i] * a].max\n\t\tend\n\t\tdp[0] *= a\n\tend\n\tx, y = (x * y).to_i, 0\n\t0.upto(n) do |i|\n\t\tx = [x, dp[i] * (i + 1)].max\n\t\ty = [y, dp[i] * (i + 2)].max\n\tend\n\treturn x, y\nend\n\nn = gets.to_i\n$e = Array.new(n){[]}\n(n - 1).times do\n\ta, b = gets.split.map{|_| _.to_i - 1}\n\t$e[a] << b\n\t$e[b] << a\nend\np gao(0, -1)[0]\n"}], "src_uid": "8dbf81e0d2815382cec6488efd877b70"} {"nl": {"description": "An array $$$b$$$ is called to be a subarray of $$$a$$$ if it forms a continuous subsequence of $$$a$$$, that is, if it is equal to $$$a_l$$$, $$$a_{l + 1}$$$, $$$\\ldots$$$, $$$a_r$$$ for some $$$l, r$$$.Suppose $$$m$$$ is some known constant. For any array, having $$$m$$$ or more elements, let's define it's beauty as the sum of $$$m$$$ largest elements of that array. For example: For array $$$x = [4, 3, 1, 5, 2]$$$ and $$$m = 3$$$, the $$$3$$$ largest elements of $$$x$$$ are $$$5$$$, $$$4$$$ and $$$3$$$, so the beauty of $$$x$$$ is $$$5 + 4 + 3 = 12$$$. For array $$$x = [10, 10, 10]$$$ and $$$m = 2$$$, the beauty of $$$x$$$ is $$$10 + 10 = 20$$$.You are given an array $$$a_1, a_2, \\ldots, a_n$$$, the value of the said constant $$$m$$$ and an integer $$$k$$$. Your need to split the array $$$a$$$ into exactly $$$k$$$ subarrays such that: Each element from $$$a$$$ belongs to exactly one subarray. Each subarray has at least $$$m$$$ elements. The sum of all beauties of $$$k$$$ subarrays is maximum possible.", "input_spec": "The first line contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 \\le n \\le 2 \\cdot 10^5$$$, $$$1 \\le m$$$, $$$2 \\le k$$$, $$$m \\cdot k \\le n$$$) — the number of elements in $$$a$$$, the constant $$$m$$$ in the definition of beauty and the number of subarrays to split to. The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$-10^9 \\le a_i \\le 10^9$$$).", "output_spec": "In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition. In the second line, print $$$k-1$$$ integers $$$p_1, p_2, \\ldots, p_{k-1}$$$ ($$$1 \\le p_1 < p_2 < \\ldots < p_{k-1} < n$$$) representing the partition of the array, in which: All elements with indices from $$$1$$$ to $$$p_1$$$ belong to the first subarray. All elements with indices from $$$p_1 + 1$$$ to $$$p_2$$$ belong to the second subarray. $$$\\ldots$$$. All elements with indices from $$$p_{k-1} + 1$$$ to $$$n$$$ belong to the last, $$$k$$$-th subarray. If there are several optimal partitions, print any of them.", "sample_inputs": ["9 2 3\n5 2 5 2 4 1 1 3 2", "6 1 4\n4 1 3 2 2 3", "2 1 2\n-1000000000 1000000000"], "sample_outputs": ["21\n3 5", "12\n1 3 5", "0\n1"], "notes": "NoteIn the first example, one of the optimal partitions is $$$[5, 2, 5]$$$, $$$[2, 4]$$$, $$$[1, 1, 3, 2]$$$. The beauty of the subarray $$$[5, 2, 5]$$$ is $$$5 + 5 = 10$$$. The beauty of the subarray $$$[2, 4]$$$ is $$$2 + 4 = 6$$$. The beauty of the subarray $$$[1, 1, 3, 2]$$$ is $$$3 + 2 = 5$$$. The sum of their beauties is $$$10 + 6 + 5 = 21$$$.In the second example, one optimal partition is $$$[4]$$$, $$$[1, 3]$$$, $$$[2, 2]$$$, $$$[3]$$$."}, "positive_code": [{"source_code": "n, m, k = gets.chomp.split(' ').map(&:to_i)\ni = -1\narray = gets.chomp.split(' ').map(&:to_i)\nl_array = array.map{|n| i+=1; [n.to_i, i]}\n\nl_array = l_array.sort{\n\t|x,y| \n\tif(x[0] == y[0]) then x[1]<=>y[1] \n\telse y<=>x end\n}[0...(k*m)].sort{|x,y| x[1]<=>y[1]}\n\nsum = 0\nl_array.each{|x| sum+=x[0]}\n\nputs sum\n\n#~ puts\n#~ print l_array\n#~ puts\ni = -1\n(k-1).times{\n\t|x|\n\tx+=1\n\ti += m\n\tif(x != k-1) then print(\"#{l_array[i][1]+1} \")\n\telse puts(\"#{l_array[i][1]+1}\") end\n}\n\n"}, {"source_code": "input = STDIN.gets.chomp.split(\" \").map{|i| i.to_i}\n\nN = input[0]\nM = input[1]\nK = input[2]\n\nA = STDIN.gets.chomp.split(\" \").map.with_index{|a, i| [i, a.to_i]}\nsorted_a = A.sort_by{|a| a[1]}.reverse.slice(0, M * K).sort_by{|a| a[0]}\n\ntotal = 0\nslices = []\nsorted_a.each_with_index{|a, i|\n if i != (M * K - 1) && (i % M) == (M - 1)\n slices.push(a[0] + 1)\n end\n total = total + a[1]\n}\nputs total\nputs slices.join(\" \")\n\n\n# A.map.with_index{|a, b|\n# p a\n# p b\n# return 23\n# }"}], "negative_code": [], "src_uid": "e9cf68a68b55fe075099c384cb6a7e9e"} {"nl": {"description": "Bajtek is learning to skate on ice. He's a beginner, so his only mode of transportation is pushing off from a snow drift to the north, east, south or west and sliding until he lands in another snow drift. He has noticed that in this way it's impossible to get from some snow drifts to some other by any sequence of moves. He now wants to heap up some additional snow drifts, so that he can get from any snow drift to any other one. He asked you to find the minimal number of snow drifts that need to be created.We assume that Bajtek can only heap up snow drifts at integer coordinates.", "input_spec": "The first line of input contains a single integer n (1 ≤ n ≤ 100) — the number of snow drifts. Each of the following n lines contains two integers xi and yi (1 ≤ xi, yi ≤ 1000) — the coordinates of the i-th snow drift. Note that the north direction coinсides with the direction of Oy axis, so the east direction coinсides with the direction of the Ox axis. All snow drift's locations are distinct.", "output_spec": "Output the minimal number of snow drifts that need to be created in order for Bajtek to be able to reach any snow drift from any other one.", "sample_inputs": ["2\n2 1\n1 2", "2\n2 1\n4 1"], "sample_outputs": ["1", "0"], "notes": null}, "positive_code": [{"source_code": "class Drift\n attr_reader :x, :y\n attr_accessor :zone, :adj\n def initialize(x, y)\n @x = x\n @y = y\n @zone = nil\n @adj = Array.new\n end\nend\n\nclass Map\n attr_accessor :d, :n\n def initialize\n @d = Array.new\n @n = 0\n end\n def add(x, y)\n d << Drift.new(x, y)\n for i in 0...@n\n if d[i].x == d[@n].x || d[i].y == d[@n].y\n d[i].adj << @n\n d[@n].adj << i\n end\n end\n @n += 1\n end\n def at(n)\n @d[n]\n end\n def dfs(n, z)\n d[n].zone = z\n for i in d[n].adj\n dfs(i, z) if d[i].zone == nil\n end\n end\nend\n\nn = gets.to_i\nmap = Map.new\nfor i in 0...n\n x, y = gets.split(\" \").map(&:to_i)\n map.add(x, y)\nend\nz = 0\nfor i in 0...map.n\n if map.at(i).zone == nil\n map.dfs(i, z)\n z += 1\n end\nend\nprint z-1"}, {"source_code": "ySet, connectedGraph=Array.new(gets.to_i){gets.scan(/(\\d+) (\\d+)/).first}.inject({}){|h, n|h.update({n[0]=>Array[n[1]]}){|k, sv, ov|sv+ov}}.values, []\nconnectedGraph.empty? || ySet.reject{|v|(connectedGraph.last&v).empty?}.each{|v|connectedGraph[-1]|=ySet.delete(v)}.empty? ? connectedGraph<Array[n[1]]}){|k, sv, ov|sv+ov}}.values\nconnectedGraph = [ySet.shift]\nySet.reject{|v|(connectedGraph.last&v).empty?}.each{|v|connectedGraph[-1]|=ySet.delete(v)}.empty? ? connectedGraph<Array[v[1]]}}.inject{|h, n|h.update(n){|k, sv, ov|sv+ov}}.values\ndisjointCount=0\nconnectedGraph=ySet.shift\nwhile !ySet.empty?\n next if !ySet.reject{|v|(connectedGraph&v).empty?}.each{|v|connectedGraph|=ySet.delete(v)}.empty?\n disjointCount+=1\n connectedGraph=ySet.shift\nend\nprint disjointCount"}, {"source_code": "ySet=Array.new(gets.to_i){gets.scan(/(\\d+) (\\d+)/).first}.inject({}){|h, n|h.update({n[0]=>Array[n[1]]}){|k, sv, ov|sv+ov}}.values\ndisjointCount=0\nconnectedGraph=ySet.shift\nwhile !ySet.empty?\n next unless ySet.reject{|v|(connectedGraph&v).empty?}.each{|v|connectedGraph|=ySet.delete(v)}.empty?\n disjointCount+=1\n connectedGraph=ySet.shift\nend\nprint disjointCount"}, {"source_code": "xMap=Hash.new([])\ngets.to_i.times{\n Hash[*gets.scan(/(\\d+) (\\d+)/)]\n xMap[$1.to_i]+=Array[$2.to_i]\n}\n\ndisjointCount=0\nset=xMap.delete(xMap.keys.first)\nwhile !xMap.empty?\n next if !xMap.values.select{|v|!(set&v).empty?}.each{|v|set|=xMap.delete(xMap.key(v))}.empty?\n disjointCount+=1\n set=xMap.delete(xMap.keys.first)\nend\nprint disjointCount"}, {"source_code": "#input = File.open('input.txt','r')\nn = gets.to_i\ngraph = Array.new(n)\ncoordinates = Array.new(n)\nn.times do |i|\n\tcoordinates[i] = Array.new(2)\n\tgraph[i] = Array.new\n\tcoordinates[i][0], coordinates[i][1] = gets.split\n\tcoordinates[i][0] = coordinates[i][0].to_i - 1\n\tcoordinates[i][1] = coordinates[i][1].to_i - 1\nend\nn.times do |i|\n\t#print i.to_s + \": \"\n\t(n-i-1).times do |j|\n\t\t#print (i+j+1).to_s + \" \"\n\t\tif coordinates[i][0] == coordinates[i+j+1][0] or coordinates[i][1] == coordinates[i+j+1][1]\n\t\t\tgraph[i].push(i+j+1)\n\t\t\tgraph[i+j+1].push(i)\n\t\t\t#print \"ALALAH!\"\n\t\tend\n\tend\n\t#puts\nend\nvisited = Array.new(n,false)\nrequire 'thread'\nqueue = Queue.new\ncomponents = -1\nn.times do |i|\n\tunless visited[i]\n\t\tvisited[i] = true\n\t\tqueue << i\n\t\twhile !queue.empty?\n\t\t\tv = queue.pop\n\t\t\tgraph[v].each do |j|\n\t\t\t\tunless visited[j]\n\t\t\t\t\tvisited[j] = true\n\t\t\t\t\tqueue << j\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\t\tcomponents += 1\n\tend\nend\nputs components\n\n\n\n#assign a value to a variable only if that variable isn't already set:\n#@variable ||= \"default value\"\n\n#x, y = y, x will interchange the values of x and y\n\n#names2 = %w{ ann richard william susan pat } \n\n#puts \"Enrollments will now Stop\" if participants > 2500 \n\n#Negative index values count from the end of the array\n"}, {"source_code": "n = gets.to_i\narr = []\n\ndef dfs(start,array,n)\n\treturn if @visited[start]\n\t\n\t@visited[start] = true\n\n\t(0...n).each do |i|\n\t\tif !@visited[i] && (array[i][0] == array[start][0] || array[i][1] == array[start][1])\n\t\t\t# puts \"here\"\n\t\t\tdfs(i,array,n)\n\t\tend\n\tend\nend\nn.times do \n\tarr << gets.split(\" \").map(&:to_i)\nend\n\n@visited = Hash.new(false)\ncomp = 0\n(0...n).each do |i|\n\t unless @visited[i]\n\t \tcomp += 1\n\t \tdfs(i,arr,n)\n\t end\nend\nputs comp-1"}, {"source_code": "n = gets.to_i\nli = []\n(1..n).each do\n x, y = gets.split.collect{|i| i.to_i}\n li.push([[x],[y]])\nend\nans = 0\nfor i in (n-1).downto(0)\n for j in 0...i\n unless (li[j][0]&li[i][0]).empty? and (li[j][1]&li[i][1]).empty?\n li[j][0] |= li[i][0]\n li[j][1] |= li[i][1]\n ans -= 1\n break\n end\n end\n ans += 1\nend\nputs ans-1\n"}, {"source_code": "n = gets.to_i\nli = []\n(1..n).each do\n x, y = gets.split.collect{|i| i.to_i}\n li.push([[x],[y]])\nend\nfor i in (n-1).downto(0)\n for j in 0...i\n unless (li[j][0]&li[i][0]).empty? and (li[j][1]&li[i][1]).empty?\n li[j][0] |= li[i][0]\n li[j][1] |= li[i][1]\n li.delete_at(i)\n break\n end\n end\nend\nputs li.size-1\n"}, {"source_code": "\n# Create reachability matrix\ndef reach(coordinates)\n reachability = Array.new(coordinates.length) {[]}\n for i in 0..(coordinates.length - 1) do\n root = coordinates[i]\n for j in (i + 1)..(coordinates.length - 1) do\n target = coordinates[j]\n if (root[0] == target[0]) || (root[1] == target[1])\n reachability[i].push(j)\n reachability[j].push(i)\n end\n end\n end\n return reachability\nend\n\n# Run DFS\ndef dfs(root, reachability, traversal)\n traversal.push(root)\n for child in reachability[root] do\n unless traversal.include?(child)\n dfs(child, reachability, traversal)\n end\n end\nend\n\n# Initialize variables\ncoordinates = []; nodes = []; segments = 0;\n\n# Read Input\nn = gets.strip.to_i\nn.times {coordinates.push(gets.strip.split.map{|s| s.to_i})}\n\n# Create reachability matrix\nreachability = reach(coordinates)\nnodes = (0..n - 1).to_a\n\n# Count connected segments\nuntil nodes.empty?\n traversal = []; root = nodes[0];\n dfs(root, reachability, traversal)\n nodes = nodes - traversal\n segments = segments + 1;\nend\n\n# Return the number of nodes needed\nputs segments - 1\n"}, {"source_code": "def dfs(index)\n return true if @dp[index] != nil \n @dp[index] = 1 \n (1..@n).each do |i|\n dfs(i) if @arr[index][i] == 1\n end\nend\n\n@n = gets.to_i\n@arr = Array.new(@n+1) {Array.new(@n+1)}\nnodes = []\n@dp = []\n(1..@n).each do |index|\n x,y = gets.split(' ').map(&:to_i)\n nodes.each_with_index do |node, i|\n if node[0] == x || node[1]== y\n @arr[index][i+1] = 1\n @arr[i+1][index] = 1\n end\n end\n nodes << [x,y]\nend\ncomponents = 0\n(1..@n).each do |index|\n if(@dp[index] == nil)\n components+= 1\n dfs(index)\n end\nend\nputs components-1"}, {"source_code": "class Node\n attr_accessor :visited, :nearest, :x, :y\n\n def initialize(x, y)\n @visited = false\n @nearest = []\n @x = x\n @y = y\n end\n\n def add(node)\n @nearest<< node if node.is_a? Node\n end\n\n def to_s\n puts \"x:#{@x},y:#{@y}\"\n end\nend\n\n\nclass Graph\n attr_accessor :nodes\n\n def initialize\n @nodes = []\n end\n\n def add_node(node)\n @nodes << node if node.is_a? Node\n end\n\n def has_not_visited\n index = @nodes.index{|el| el.visited==false}\n index.nil? ? false : @nodes[index]\n end\nend\n\n\ndef bfs(start)\n start.visited = true\n start.nearest.each{|el| bfs(el) unless el.visited}\nend\n\n\nn = gets.to_i\ngr = Graph.new\n\nn.times do\n x, y = gets.split.map(&:to_i)\n node = Node.new(x, y)\n\n gr.nodes.each do |n|\n if n.x == node.x || n.y ==node.y\n n.add(node)\n node.add(n)\n end\n end\n gr.add_node(node)\nend\n\ncounter = -1\nwhile gr.has_not_visited do\n counter +=1\n bfs(gr.has_not_visited)\nend\n\nputs counter"}], "negative_code": [{"source_code": "class Drift\n attr_reader :x, :y\n attr_accessor :zone\n def initialize(x, y)\n @x = x\n @y = y\n @zone = nil;\n end\nend\n\nn = gets.to_i\nmap = Array.new(n)\nfor i in 0...n\n input = gets.split\n map[i] = Drift.new(input[0].to_i, input[1].to_i)\nend\n\nz = 0\nfor i in 0...n\n if map[i].zone == nil\n map[i].zone = z\n for j in (i+1)...n\n map[j].zone = z unless map[i].x != map[j].x && map[i].y != map[j].y\n end\n z += 1\n end\nend\nprint z-1"}, {"source_code": "ys=Array.new(gets.to_i){gets.scan(/(\\d+) (\\d+)/).first}.map{|v|{v[0]=>Array[v[1]]}}.inject{|h, n|h.update(n){|k, sv, ov|sv+ov}}.values\nprint ys.map{|v|ys.reject{|ov|(v&ov).empty?}.inject(:|)}.uniq.size-1"}, {"source_code": "n = gets.to_i\nli = []\n(1..n).each do\n x, y = gets.split.collect{|i| i.to_i}\n li.push([[x],[y]])\nend\nans = 0\nfor i in (n-1).downto(0)\n for j in 0...i\n unless (li[j][0]&li[i][0]).empty? and (li[j][1]&li[i][1]).empty?\n li[i][0] |= li[j][0]\n li[i][1] |= li[j][1]\n ans -= 1\n break\n end\n end\n ans += 1\nend\nputs ans-1\n"}], "src_uid": "cb4dbff31d967c3dab8fe0495eb871dc"} {"nl": {"description": "There are $$$n$$$ piles of stones, where the $$$i$$$-th pile has $$$a_i$$$ stones. Two people play a game, where they take alternating turns removing stones.In a move, a player may remove a positive number of stones from the first non-empty pile (the pile with the minimal index, that has at least one stone). The first player who cannot make a move (because all piles are empty) loses the game. If both players play optimally, determine the winner of the game.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1\\le t\\le 1000$$$)  — the number of test cases. Next $$$2t$$$ lines contain descriptions of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1\\le n\\le 10^5$$$)  — the number of piles. The second line of each test case contains $$$n$$$ integers $$$a_1,\\ldots,a_n$$$ ($$$1\\le a_i\\le 10^9$$$)  — $$$a_i$$$ is equal to the number of stones in the $$$i$$$-th pile. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.", "output_spec": "For each test case, if the player who makes the first move will win, output \"First\". Otherwise, output \"Second\".", "sample_inputs": ["7\n3\n2 5 4\n8\n1 1 1 1 1 1 1 1\n6\n1 2 3 4 5 6\n6\n1 1 2 1 2 2\n1\n1000000000\n5\n1 2 2 1 1\n3\n1 1 1"], "sample_outputs": ["First\nSecond\nSecond\nFirst\nFirst\nSecond\nFirst"], "notes": "NoteIn the first test case, the first player will win the game. His winning strategy is: The first player should take the stones from the first pile. He will take $$$1$$$ stone. The numbers of stones in piles will be $$$[1, 5, 4]$$$. The second player should take the stones from the first pile. He will take $$$1$$$ stone because he can't take any other number of stones. The numbers of stones in piles will be $$$[0, 5, 4]$$$. The first player should take the stones from the second pile because the first pile is empty. He will take $$$4$$$ stones. The numbers of stones in piles will be $$$[0, 1, 4]$$$. The second player should take the stones from the second pile because the first pile is empty. He will take $$$1$$$ stone because he can't take any other number of stones. The numbers of stones in piles will be $$$[0, 0, 4]$$$. The first player should take the stones from the third pile because the first and second piles are empty. He will take $$$4$$$ stones. The numbers of stones in piles will be $$$[0, 0, 0]$$$. The second player will lose the game because all piles will be empty. "}, "positive_code": [{"source_code": "t = gets.to_i\n\n1.upto t do |z|\n n = gets.to_i\n a = gets.split.map(&:to_i)\n c = 0\n \n if n == 1\n c = 0\n else\n 0.upto n-1 do |i|\n if a[i] == 1\n c += 1\n else\n break\n end\n end\n end\n\n if c == n\n if c.odd?\n puts \"First\"\n else\n puts \"Second\"\n end\n else\n if c.even? or c == 0\n puts \"First\"\n else\n puts \"Second\"\n end\n end\nend"}, {"source_code": "gets.to_i.times do\n n = gets.to_i\n a = gets.split.map &:to_i\n k = 0\n k += 1 while k < n && a[k] == 1\n if k == n\n res = k % 2 == 0 ? \"Second\" : \"First\"\n else\n res = k % 2 == 0 ? \"First\" : \"Second\"\n end\n puts res\nend"}, {"source_code": "tc = gets.to_i \n\ntc.times do \n n = gets.to_i \n arr = gets.split().map(&:to_i) \n\n i = 0 \n while(i < n) \n if(arr[i] > 1) \n i += 1 \n break \n end\n i += 1\n end\n i -= 1\n if(i% 2 == 0) \n puts \"First\"\n else\n puts \"Second\"\n end\nend"}], "negative_code": [{"source_code": "gets.to_i.times do\n n = gets.to_i\n a = gets.split.map &:to_i\n k = 0\n k += 1 while k < n && a[k] == 1\n if k == n\n res = k % 2 == 0 ? \"Secobd\" : \"First\"\n else\n res = k % 2 == 0 ? \"First\" : \"Second\"\n end\n puts res\nend"}], "src_uid": "0c9030689394ad4e126e5b8681f1535c"} {"nl": {"description": "Pasha got a very beautiful string s for his birthday, the string consists of lowercase Latin letters. The letters in the string are numbered from 1 to |s| from left to right, where |s| is the length of the given string.Pasha didn't like his present very much so he decided to change it. After his birthday Pasha spent m days performing the following transformations on his string — each day he chose integer ai and reversed a piece of string (a segment) from position ai to position |s| - ai + 1. It is guaranteed that 2·ai ≤ |s|.You face the following task: determine what Pasha's string will look like after m days.", "input_spec": "The first line of the input contains Pasha's string s of length from 2 to 2·105 characters, consisting of lowercase Latin letters. The second line contains a single integer m (1 ≤ m ≤ 105) —  the number of days when Pasha changed his string. The third line contains m space-separated elements ai (1 ≤ ai; 2·ai ≤ |s|) — the position from which Pasha started transforming the string on the i-th day.", "output_spec": "In the first line of the output print what Pasha's string s will look like after m days.", "sample_inputs": ["abcdef\n1\n2", "vwxyz\n2\n2 2", "abcdef\n3\n1 2 3"], "sample_outputs": ["aedcbf", "vwxyz", "fbdcea"], "notes": null}, "positive_code": [{"source_code": "s = gets.chomp.split('')\nm = gets.to_i\ncnt = Array.new((s.length+1)/2+1,0)\ngets.split.collect{|i| cnt[i.to_i]+=1}\nfor i in 1..(s.length+1)/2\n cnt[i] = (cnt[i-1]+cnt[i])%2\n if cnt[i] > 0\n s[i-1], s[s.length-i] = s[s.length-i], s[i-1]\n end\nend\nputs s.join\n"}, {"source_code": "s = [nil] + gets.chomp.chars\nn = gets.to_i\nc = Array.new(s.size / 2 + 1, 0)\na = gets.split.map(&:to_i).each{|x| c[x] += 1}\nfor i in 2...c.size do\n c[i] += c[i - 1]\nend\nfor i in 1...c.size do\n if c[i] % 2 == 0\n next\n else\n s[i], s[s.size - i] = s[s.size - i], s[i]\n end\nend\nputs s.join(\"\")\n"}, {"source_code": "# f = File.open 'input.txt', 'r'\nf = STDIN\n\ndef compute(s, positions)\n marked_positions = Hash.new(0)\n positions.each do |pos|\n marked_positions[pos] += 1\n end\n be_changed = false\n 1.upto(s.length/2) do |index|\n # index - 1 && s.length - index\n be_changed = !be_changed if marked_positions[index].odd?\n if be_changed\n tmp = s[index - 1]\n s[index - 1] = s[s.length - index]\n s[s.length - index] = tmp\n end\n end\n s\nend\n\nwhile true\n raw_s = f.gets\n break unless raw_s\n s = raw_s.chomp\n m = f.gets.chomp.to_i\n positions = f.gets.chomp.split.map &:to_i\n puts compute(s, positions)\nend\n"}], "negative_code": [{"source_code": "s = [nil] + gets.chomp.chars\nn = gets.to_i\nc = Array.new(s.size / 2 + 1, 0)\na = gets.split.map(&:to_i).each{|x| c[x] += 1}\nfor i in 2...c.size do\n c[i] += c[i - 1]\nend\np c\nfor i in 1...c.size do\n if c[i] % 2 == 0\n next\n else\n s[i], s[s.size - i] = s[s.size - i], s[i]\n end\nend\nputs s.join(\"\")\n"}, {"source_code": "# f = File.open 'input.txt', 'r'\nf = STDIN\n\ndef compute(s, positions)\n marked_positions = Hash.new(0)\n positions.each do |pos|\n marked_positions[pos] += 1\n end\n be_changed = false\n 1.upto(s.length/2) do |index|\n # index - 1 && s.length - index\n be_changed = !be_changed if marked_positions[index].odd?\n if be_changed\n tmp = s[index - 1]\n s[index - 1] = s[s.length - index]\n s[s.length - index] = tmp\n end\n end\n s\nend\n\nwhile true\n raw_s = f.gets\n break unless raw_s\n s = raw_s.chomp\n m = f.gets.chomp.to_i\n positions = f.gets.chomp.split.map &:to_i\n p compute(s, positions)\nend\n"}], "src_uid": "9d46ae53e6dc8dc54f732ec93a82ded3"} {"nl": {"description": "You are given a sequence $$$b_1, b_2, \\ldots, b_n$$$. Find the lexicographically minimal permutation $$$a_1, a_2, \\ldots, a_{2n}$$$ such that $$$b_i = \\min(a_{2i-1}, a_{2i})$$$, or determine that it is impossible.", "input_spec": "Each test contains one or more test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 100$$$). The first line of each test case consists of one integer $$$n$$$ — the number of elements in the sequence $$$b$$$ ($$$1 \\le n \\le 100$$$). The second line of each test case consists of $$$n$$$ different integers $$$b_1, \\ldots, b_n$$$ — elements of the sequence $$$b$$$ ($$$1 \\le b_i \\le 2n$$$). It is guaranteed that the sum of $$$n$$$ by all test cases doesn't exceed $$$100$$$.", "output_spec": "For each test case, if there is no appropriate permutation, print one number $$$-1$$$. Otherwise, print $$$2n$$$ integers $$$a_1, \\ldots, a_{2n}$$$ — required lexicographically minimal permutation of numbers from $$$1$$$ to $$$2n$$$.", "sample_inputs": ["5\n1\n1\n2\n4 1\n3\n4 1 3\n4\n2 3 4 5\n5\n1 5 7 2 8"], "sample_outputs": ["1 2 \n-1\n4 5 1 2 3 6 \n-1\n1 3 5 6 7 9 2 4 8 10"], "notes": null}, "positive_code": [{"source_code": "gets.to_i.times do\n\tn = gets.to_i\n\ta = gets.split.map &:to_i\n\tb = (1..(2*n)).select{|i| !a.include?(i)}\n\tc = Array.new(n, 1)\n\tflag = false\n\tc.each_index do |i|\n\t\tpos = b.index{|x| x > a[i]}\n\t\tif pos\n\t\t\tc[i] = b[pos]\n\t\t\tb.delete_at(pos)\n\t\telse\n\t\t\tflag = true\n\t\tend\n\tend\n\tif flag\n\t\tputs -1\n\telse\n\t\tputs (0...n).to_a.map{|i| [a[i], c[i]]}.flatten.join(' ')\n\tend\nend"}], "negative_code": [], "src_uid": "c4d32fcacffaa5d3a4db6dfa376d0324"} {"nl": {"description": "Limak is an old brown bear. He often plays poker with his friends. Today they went to a casino. There are n players (including Limak himself) and right now all of them have bids on the table. i-th of them has bid with size ai dollars.Each player can double his bid any number of times and triple his bid any number of times. The casino has a great jackpot for making all bids equal. Is it possible that Limak and his friends will win a jackpot?", "input_spec": "First line of input contains an integer n (2 ≤ n ≤ 105), the number of players. The second line contains n integer numbers a1, a2, ..., an (1 ≤ ai ≤ 109) — the bids of players.", "output_spec": "Print \"Yes\" (without the quotes) if players can make their bids become equal, or \"No\" otherwise.", "sample_inputs": ["4\n75 150 75 50", "3\n100 150 250"], "sample_outputs": ["Yes", "No"], "notes": "NoteIn the first sample test first and third players should double their bids twice, second player should double his bid once and fourth player should both double and triple his bid.It can be shown that in the second sample test there is no way to make all bids equal."}, "positive_code": [{"source_code": "#!/usr/bin/ruby\n\ndef solution(n, arr)\n uniq = arr.uniq do |n|\n while n % 2 == 0\n n /= 2\n end\n while n % 3 == 0\n n /= 3\n end\n n\n end\n\n puts uniq.size == 1 ? 'Yes' : 'No'\n\nend\n\nn = gets.to_i\narr = gets.split(' ').map{|i| i.to_i}\nsolution n, arr\n#\n# solution 4, [75, 150, 75, 50]\n# solution 3, [100, 150, 250]"}, {"source_code": "def function(x)\n while x%2==0\n x/=2\n end\n while x%3==0\n x/=3\n end\n return x\nend \n\n\nn = gets\narr = gets.split.map(&:to_i)\nnew = []\narr.each do|x|\n new << function(x)\nend\nnew.sort!\nif new[0]==new[-1]\n puts 'Yes'\nelse\n puts 'No'\nend "}, {"source_code": "gets\nmas = gets.split(\" \").map(&:to_i)\n\nfor k in 0...mas.size do\n mas[k]/=2 while mas[k]%2==0\n mas[k]/=3 while mas[k]%3==0\nend\n\nchk = 1\nfor k in 1...mas.size do\n chk = 0 if mas[k]!=mas[0]\nend\n\nprint chk==1 ? \"YES\" : \"NO\"\n"}, {"source_code": "def divide23(n)\n\tn /= 2 while n%2 == 0\n\tn /= 3 while n%3 == 0\n\treturn n\nend\nn = gets.chomp.to_i\narr = gets.chomp.split(\" \").map {|val| val = val.to_i}\narr.map!{ |val| val = divide23(val)}\nallsame=true\narr.each { |val| allsame = false unless val == arr[0]}\nputs \"Yes\" if allsame\nputs \"No\" unless allsame\n"}, {"source_code": "n = gets.chomp.to_i;\narr = gets.chomp.split.map(&:to_i);\n\nn.times do |x|\n arr[x] /= 2 while (arr[x] % 2 == 0)\n arr[x] /= 3 while (arr[x] % 3 == 0);\nend\nputs (arr.uniq.size == 1 ? \"Yes\" : \"No\");"}, {"source_code": "def f(x)\n x /= 2 until x % 2 > 0\n x /= 3 until x % 3 > 0\n x\nend\n\nn = gets.to_i\nx, *a = gets.split.map &:to_i\nx = f(x)\nputs a.all?{|y| f(y) == x} ? 'Yes' : 'No'\n"}, {"source_code": "k = gets.to_i\n\nq = gets.split.map(&:to_i)\no = []\n\nq.each do |e|\n n = e.to_i\n\n status = true\n\n loop do\n if n % 2 == 0\n n /= 2\n elsif n % 3 == 0\n n /= 3\n else\n status = false if n > 1\n o << n\n break\n end\n end\n\nend\n\no.uniq!\n\nputs o.size == 1 ? 'Yes' : 'No'"}, {"source_code": "n = gets.to_i\nres = -1\na = gets.split(' ').map(&:to_i)\na.each do |i|\n while i % 2 == 0\n i /= 2\n end\n while i % 3 == 0\n i /= 3\n end\n if res == -1\n res = i\n elsif res != i\n puts \"No\"\n exit\n end\nend\nputs \"Yes\""}, {"source_code": "n = gets.chomp.to_i\nas = gets.chomp.split(' ').map(&:to_i)\n\nbs = []\nas.each do |a|\n b = a\n while b % 2 == 0\n b /= 2\n end\n while b % 3 == 0\n b /= 3\n end\n bs << b\nend\n\nbs.sort!\nif bs[0] == bs[-1]\n puts 'Yes'\nelse\n puts 'No'\nend\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nn.times do |i|\n a[i] /= 2 while a[i] % 2 == 0\n a[i] /= 3 while a[i] % 3 == 0\nend\nputs a.uniq.one? ? 'Yes' : 'No'\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nn.times do |i|\n while a[i] % 2 == 0 || a[i] % 3 == 0\n if a[i] % 6 == 0\n a[i] /= 6\n elsif a[i] % 3 == 0\n a[i] /= 3\n elsif a[i] % 2 == 0\n a[i] /= 2\n end\n end\nend\nputs a.uniq.one? ? 'Yes' : 'No'\n"}, {"source_code": "n=gets.to_i\nx=gets.split.map{|e| e.to_i}\nm1=x.max\nok=\"No\"\nif x.all?{|e|\n\te2=m1.lcm(e)/m1.gcd(e)\n\twhile e2%2==0\n\t\te2/=2\n\tend\n\twhile e2%3==0\n\t\te2/=3\n\tend\n\te2==1\n} then\n\tok=\"Yes\"\nend\nputs ok\n\n"}, {"source_code": "# coding: utf-8\n\nn = gets.to_i\nar = gets.split.map(&:to_i)\n\n# ar내의 모든 n에 대해서 2배, 3배를 해서 같은 숫자를 얻으려면\n# 2와 3을 모두 제거했을 때 남은 수가 모두 같아야한다.\nsay_yes = true\ndef remove_2s_3s(num)\n loop do\n if num%2 == 0\n num = num/2\n next\n elsif num%3 == 0\n num = num/3\n next\n else\n break\n end\n end\n return num\nend\n\nbase = remove_2s_3s(ar.first)\n\nar.each { |i|\n if base != remove_2s_3s(i)\n say_yes = false\n end\n}\n\nif say_yes\n puts \"Yes\"\nelse\n puts \"No\"\nend"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i).map do |x|\n x /= 2 while x % 2 == 0\n x /= 3 while x % 3 == 0\n x\nend\nputs a.max == a.min ? 'Yes' : 'No'\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i).map do |x|\n x /= 2 while x % 2 == 0\n x /= 3 while x % 3 == 0\n x\nend\na = a.sort\nputs a[0] == a[-1] ? 'Yes' : 'No'\n"}, {"source_code": "#!/usr/bin/ruby\n\ndef check(l, a)\n k = l / a\n while k % 2 == 0\n k /= 2\n end\n while k % 3 == 0\n k /= 3\n end\n k == 1 ? true : false\nend\n\ndef gcd(a, b)\n a, b = b, a if a < b\n # return a if b == 0\n # gcd(b, a % b)\n while b != 0\n a, b = b, a % b\n end\n a\nend\n\ndef lcm(a, b)\n (a * b) / gcd(a, b)\nend\n\ndef algo\n n = gets.to_i\n v = gets.split(' ').map { |e| e.to_i }\n p = v[0]\n flag = 1\n for i in v\n l = lcm(p, i)\n if not (check(l, p) && check(l, i))\n flag = 0\n break\n end\n p = i\n end\n puts flag == 1 ? \"Yes\" : \"No\"\nend\n\nalgo\n"}], "negative_code": [{"source_code": "n=gets.to_i\nx=gets.split.map{|e| e.to_i}\nm1=x.max\nok=\"No\"\nif x.all?{|e|\n\te2=e/m1.gcd(e)\n\twhile e2%2==0\n\t\te2/=2\n\tend\n\twhile e2%3==0\n\t\te2/=3\n\tend\n\te2==1\n} then\n\tok=\"Yes\"\nend\nputs ok\n\n\n"}, {"source_code": "n=gets.to_i\nx=gets.split.map{|e| e.to_i}\nm1=x.max\nm2=m1\nok=\"No\"\n2.times{\n\tif x.all?{|e|\n\t\t(m1%e==0) && (m1/e<4)\n\t} then\n\t\tok=\"Yes\"\n\tend\n\tm1+=m2\n}\nputs ok\n\n"}, {"source_code": "n=gets.to_i\nx=gets.split.map{|e| e.to_i}\nm1=x.max\nok=\"No\"\nif x.all?{|e|\n\te2=m1/m1.gcd(e)\n\twhile e2%2==0\n\t\te2/=2\n\tend\n\twhile e2%3==0\n\t\te2/=3\n\tend\n\te2==1\n} then\n\tok=\"Yes\"\nend\nputs ok\n\n"}, {"source_code": "# coding: utf-8\n\nn = gets.to_i\nar = gets.split.map(&:to_i)\n\n# for every n...\n# how many 2's, 3's ? and remains?\nar_max = ar.max\nsay_yes = true\nar.each { |i| \n r = ar_max%i\n if r > 0\n say_yes = false\n break\n end\n}\n\nif say_yes\n puts \"Yes\"\nelse\n puts \"No\"\nend"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i).map do |x|\n x /= 2 if x % 2 == 0\n x /= 3 if x % 3 == 0\n x\nend\na = a.sort\nputs a[0] == a[-1] ? 'Yes' : 'No'\n"}], "src_uid": "2bb893703cbffe9aeaa0bed02f42a05c"} {"nl": {"description": " As the boat drifts down the river, a wood full of blossoms shows up on the riverfront.\"I've been here once,\" Mino exclaims with delight, \"it's breathtakingly amazing.\"\"What is it like?\"\"Look, Kanno, you've got your paintbrush, and I've got my words. Have a try, shall we?\" There are four kinds of flowers in the wood, Amaranths, Begonias, Centaureas and Dianthuses.The wood can be represented by a rectangular grid of $$$n$$$ rows and $$$m$$$ columns. In each cell of the grid, there is exactly one type of flowers.According to Mino, the numbers of connected components formed by each kind of flowers are $$$a$$$, $$$b$$$, $$$c$$$ and $$$d$$$ respectively. Two cells are considered in the same connected component if and only if a path exists between them that moves between cells sharing common edges and passes only through cells containing the same flowers.You are to help Kanno depict such a grid of flowers, with $$$n$$$ and $$$m$$$ arbitrarily chosen under the constraints given below. It can be shown that at least one solution exists under the constraints of this problem.Note that you can choose arbitrary $$$n$$$ and $$$m$$$ under the constraints below, they are not given in the input.", "input_spec": "The first and only line of input contains four space-separated integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$d$$$ ($$$1 \\leq a, b, c, d \\leq 100$$$) — the required number of connected components of Amaranths, Begonias, Centaureas and Dianthuses, respectively.", "output_spec": "In the first line, output two space-separated integers $$$n$$$ and $$$m$$$ ($$$1 \\leq n, m \\leq 50$$$) — the number of rows and the number of columns in the grid respectively. Then output $$$n$$$ lines each consisting of $$$m$$$ consecutive English letters, representing one row of the grid. Each letter should be among 'A', 'B', 'C' and 'D', representing Amaranths, Begonias, Centaureas and Dianthuses, respectively. In case there are multiple solutions, print any. You can output each letter in either case (upper or lower).", "sample_inputs": ["5 3 2 1", "50 50 1 1", "1 6 4 5"], "sample_outputs": ["4 7\nDDDDDDD\nDABACAD\nDBABACD\nDDDDDDD", "4 50\nCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\nABABABABABABABABABABABABABABABABABABABABABABABABAB\nBABABABABABABABABABABABABABABABABABABABABABABABABA\nDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD", "7 7\nDDDDDDD\nDDDBDBD\nDDCDCDD\nDBDADBD\nDDCDCDD\nDBDBDDD\nDDDDDDD"], "notes": "NoteIn the first example, each cell of Amaranths, Begonias and Centaureas forms a connected component, while all the Dianthuses form one. "}, "positive_code": [{"source_code": "def make(ca, a, cb, b)\n if a==1\n make1(ca, cb, b, false)\n elsif b==1\n make1(cb, ca, a, false)\n else\n make1(ca, cb, b-1, true) + make1(cb, ca, a-1, true)\n end\nend\n\ndef make1(c1, cn, n, half)\n row = half ? 25 : 50\n r = [nil] * row\n row.times{|i| r[i] = [c1] * 25 }\n i = 1\n j = 1\n n.times do\n r[i][j] = cn\n j += 2\n if j >= 24\n j = 1\n i += 2\n end\n end\n r\nend\n\na, b, c, d = gets.split.map(&:to_i)\nf1 = make('A', a, 'B', b)\nf2 = make('C', c, 'D', d)\nputs \"50 50\"\n50.times{|i|puts (f1[i]+f2[i]).join('')}\n"}], "negative_code": [], "src_uid": "637b0a757223521c44674bf1663a1114"} {"nl": {"description": "Limak is a little bear who loves to play. Today he is playing by destroying block towers. He built n towers in a row. The i-th tower is made of hi identical blocks. For clarification see picture for the first sample.Limak will repeat the following operation till everything is destroyed.Block is called internal if it has all four neighbors, i.e. it has each side (top, left, down and right) adjacent to other block or to the floor. Otherwise, block is boundary. In one operation Limak destroys all boundary blocks. His paws are very fast and he destroys all those blocks at the same time.Limak is ready to start. You task is to count how many operations will it take him to destroy all towers.", "input_spec": "The first line contains single integer n (1 ≤ n ≤ 105). The second line contains n space-separated integers h1, h2, ..., hn (1 ≤ hi ≤ 109) — sizes of towers.", "output_spec": "Print the number of operations needed to destroy all towers.", "sample_inputs": ["6\n2 1 4 6 2 2", "7\n3 3 3 1 3 3 3"], "sample_outputs": ["3", "2"], "notes": "NoteThe picture below shows all three operations for the first sample test. Each time boundary blocks are marked with red color. After first operation there are four blocks left and only one remains after second operation. This last block is destroyed in third operation."}, "positive_code": [{"source_code": "#!/usr/bin/ruby\n\ndef blocklevel(tower)\n l, r, i, t = [], [], 0, tower.length - 1\n p = 1\n i.upto(t) do |e|\n p = l[e] = [p, tower[e]].min\n p += 1\n end\n p = 1\n t.downto(i) do |e|\n p = r[e] = [p, tower[e]].min\n p += 1\n end\n Array.new(t+1) { |i| [l[i], r[i]].min }.max\nend\n\ndef algo\n n = gets.to_i\n tower = gets.split(' ').map {|e| e.to_i }\n p blocklevel tower\nend\n\nalgo"}], "negative_code": [], "src_uid": "a548737890b4bf322d0f8989e5cd25ac"} {"nl": {"description": "Bob is a pirate looking for the greatest treasure the world has ever seen. The treasure is located at the point $$$T$$$, which coordinates to be found out.Bob travelled around the world and collected clues of the treasure location at $$$n$$$ obelisks. These clues were in an ancient language, and he has only decrypted them at home. Since he does not know which clue belongs to which obelisk, finding the treasure might pose a challenge. Can you help him?As everyone knows, the world is a two-dimensional plane. The $$$i$$$-th obelisk is at integer coordinates $$$(x_i, y_i)$$$. The $$$j$$$-th clue consists of $$$2$$$ integers $$$(a_j, b_j)$$$ and belongs to the obelisk $$$p_j$$$, where $$$p$$$ is some (unknown) permutation on $$$n$$$ elements. It means that the treasure is located at $$$T=(x_{p_j} + a_j, y_{p_j} + b_j)$$$. This point $$$T$$$ is the same for all clues.In other words, each clue belongs to exactly one of the obelisks, and each obelisk has exactly one clue that belongs to it. A clue represents the vector from the obelisk to the treasure. The clues must be distributed among the obelisks in such a way that they all point to the same position of the treasure.Your task is to find the coordinates of the treasure. If there are multiple solutions, you may print any of them.Note that you don't need to find the permutation. Permutations are used only in order to explain the problem.", "input_spec": "The first line contains an integer $$$n$$$ ($$$1 \\leq n \\leq 1000$$$) — the number of obelisks, that is also equal to the number of clues. Each of the next $$$n$$$ lines contains two integers $$$x_i$$$, $$$y_i$$$ ($$$-10^6 \\leq x_i, y_i \\leq 10^6$$$) — the coordinates of the $$$i$$$-th obelisk. All coordinates are distinct, that is $$$x_i \\neq x_j$$$ or $$$y_i \\neq y_j$$$ will be satisfied for every $$$(i, j)$$$ such that $$$i \\neq j$$$. Each of the next $$$n$$$ lines contains two integers $$$a_i$$$, $$$b_i$$$ ($$$-2 \\cdot 10^6 \\leq a_i, b_i \\leq 2 \\cdot 10^6$$$) — the direction of the $$$i$$$-th clue. All coordinates are distinct, that is $$$a_i \\neq a_j$$$ or $$$b_i \\neq b_j$$$ will be satisfied for every $$$(i, j)$$$ such that $$$i \\neq j$$$. It is guaranteed that there exists a permutation $$$p$$$, such that for all $$$i,j$$$ it holds $$$\\left(x_{p_i} + a_i, y_{p_i} + b_i\\right) = \\left(x_{p_j} + a_j, y_{p_j} + b_j\\right)$$$. ", "output_spec": "Output a single line containing two integers $$$T_x, T_y$$$ — the coordinates of the treasure. If there are multiple answers, you may print any of them.", "sample_inputs": ["2\n2 5\n-6 4\n7 -2\n-1 -3", "4\n2 2\n8 2\n-7 0\n-2 6\n1 -14\n16 -12\n11 -18\n7 -14"], "sample_outputs": ["1 2", "9 -12"], "notes": "NoteAs $$$n = 2$$$, we can consider all permutations on two elements. If $$$p = [1, 2]$$$, then the obelisk $$$(2, 5)$$$ holds the clue $$$(7, -2)$$$, which means that the treasure is hidden at $$$(9, 3)$$$. The second obelisk $$$(-6, 4)$$$ would give the clue $$$(-1,-3)$$$ and the treasure at $$$(-7, 1)$$$. However, both obelisks must give the same location, hence this is clearly not the correct permutation.If the hidden permutation is $$$[2, 1]$$$, then the first clue belongs to the second obelisk and the second clue belongs to the first obelisk. Hence $$$(-6, 4) + (7, -2) = (2,5) + (-1,-3) = (1, 2)$$$, so $$$T = (1,2)$$$ is the location of the treasure. In the second sample, the hidden permutation is $$$[2, 3, 4, 1]$$$."}, "positive_code": [{"source_code": "n=gets.to_i\nputs$<.map{|e|e.split.map &:to_i}.transpose.map{|e|e.reduce(:+)/n}*' '"}, {"source_code": "def solution\n n = read_int\n sumx = 0\n sumy = 0\n (n * 2).times do\n x, y = read_ints\n sumx += x\n sumy += y\n end\n tx = sumx / n\n ty = sumy / n\n\n puts \"#{tx} #{ty}\"\nend\n\ndef read_int\n gets.to_i\nend\n\ndef read_ints\n gets.split.map(&:to_i)\nend\n\ndef read_string\n gets.chomp\nend\n\nsolution unless ENV['TEST__MODE']\n"}], "negative_code": [], "src_uid": "ba526a7f29cf7f83afa0b71bcd06e86b"} {"nl": {"description": "Recently Pashmak has been employed in a transportation company. The company has k buses and has a contract with a school which has n students. The school planned to take the students to d different places for d days (each day in one place). Each day the company provides all the buses for the trip. Pashmak has to arrange the students in the buses. He wants to arrange the students in a way that no two students become close friends. In his ridiculous idea, two students will become close friends if and only if they are in the same buses for all d days.Please help Pashmak with his weird idea. Assume that each bus has an unlimited capacity.", "input_spec": "The first line of input contains three space-separated integers n, k, d (1 ≤ n, d ≤ 1000; 1 ≤ k ≤ 109).", "output_spec": "If there is no valid arrangement just print -1. Otherwise print d lines, in each of them print n integers. The j-th integer of the i-th line shows which bus the j-th student has to take on the i-th day. You can assume that the buses are numbered from 1 to k.", "sample_inputs": ["3 2 2", "3 2 1"], "sample_outputs": ["1 1 2 \n1 2 1", "-1"], "notes": "NoteNote that two students become close friends only if they share a bus each day. But the bus they share can differ from day to day."}, "positive_code": [{"source_code": "n, k, d = gets.split.map(&:to_i)\ndef ok(n, k, d)\n s, p = 1, 0\n while s <= 1000 && p < d\n s *= k\n p += 1\n end\n n <= s\nend\ndef q(n, k, d)\n s, p = 1, 0\n while s <= 1000 && p < d\n s *= k\n p += 1\n end\n [n, s].min\nend\n\nif ok(n, k, d)\n 1.upto(d) do |i|\n t, ans = 1, []\n while ans.length < n\n 1.upto(q(n - ans.length, k, d - i)) do |j|\n ans << t.to_s\n end\n t += 1\n if t > k\n t = 1\n end\n end\n puts ans.join(\" \")\n end\nelse\n puts \"-1\"\nend"}], "negative_code": [], "src_uid": "4dddcf0ded11672a4958fb0d391dbaf5"} {"nl": {"description": "Recently Polycarp noticed that some of the buttons of his keyboard are malfunctioning. For simplicity, we assume that Polycarp's keyboard contains $$$26$$$ buttons (one for each letter of the Latin alphabet). Each button is either working fine or malfunctioning. To check which buttons need replacement, Polycarp pressed some buttons in sequence, and a string $$$s$$$ appeared on the screen. When Polycarp presses a button with character $$$c$$$, one of the following events happened: if the button was working correctly, a character $$$c$$$ appeared at the end of the string Polycarp was typing; if the button was malfunctioning, two characters $$$c$$$ appeared at the end of the string. For example, suppose the buttons corresponding to characters a and c are working correctly, and the button corresponding to b is malfunctioning. If Polycarp presses the buttons in the order a, b, a, c, a, b, a, then the string he is typing changes as follows: a $$$\\rightarrow$$$ abb $$$\\rightarrow$$$ abba $$$\\rightarrow$$$ abbac $$$\\rightarrow$$$ abbaca $$$\\rightarrow$$$ abbacabb $$$\\rightarrow$$$ abbacabba.You are given a string $$$s$$$ which appeared on the screen after Polycarp pressed some buttons. Help Polycarp to determine which buttons are working correctly for sure (that is, this string could not appear on the screen if any of these buttons was malfunctioning).You may assume that the buttons don't start malfunctioning when Polycarp types the string: each button either works correctly throughout the whole process, or malfunctions throughout the whole process.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases in the input. Then the test cases follow. Each test case is represented by one line containing a string $$$s$$$ consisting of no less than $$$1$$$ and no more than $$$500$$$ lowercase Latin letters.", "output_spec": "For each test case, print one line containing a string $$$res$$$. The string $$$res$$$ should contain all characters which correspond to buttons that work correctly in alphabetical order, without any separators or repetitions. If all buttons may malfunction, $$$res$$$ should be empty.", "sample_inputs": ["4\na\nzzaaz\nccff\ncbddbb"], "sample_outputs": ["a\nz\n\nbc"], "notes": null}, "positive_code": [{"source_code": "require 'set'\n\nreadline.to_i.times do\n s = readline.chomp\n res = Set.new()\n for c,x in s.chars.chunk{|c|c} do\n if x.length % 2 != 0 then\n res << c\n end\n end\n print res.sort.join(), \"\\n\"\nend\n"}], "negative_code": [], "src_uid": "586a15030f4830c68f2ea1446e80028c"} {"nl": {"description": "A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it!You are given integers $$$n,k$$$. Construct a grid $$$A$$$ with size $$$n \\times n$$$ consisting of integers $$$0$$$ and $$$1$$$. The very important condition should be satisfied: the sum of all elements in the grid is exactly $$$k$$$. In other words, the number of $$$1$$$ in the grid is equal to $$$k$$$.Let's define: $$$A_{i,j}$$$ as the integer in the $$$i$$$-th row and the $$$j$$$-th column. $$$R_i = A_{i,1}+A_{i,2}+...+A_{i,n}$$$ (for all $$$1 \\le i \\le n$$$). $$$C_j = A_{1,j}+A_{2,j}+...+A_{n,j}$$$ (for all $$$1 \\le j \\le n$$$). In other words, $$$R_i$$$ are row sums and $$$C_j$$$ are column sums of the grid $$$A$$$. For the grid $$$A$$$ let's define the value $$$f(A) = (\\max(R)-\\min(R))^2 + (\\max(C)-\\min(C))^2$$$ (here for an integer sequence $$$X$$$ we define $$$\\max(X)$$$ as the maximum value in $$$X$$$ and $$$\\min(X)$$$ as the minimum value in $$$X$$$). Find any grid $$$A$$$, which satisfies the following condition. Among such grids find any, for which the value $$$f(A)$$$ is the minimum possible. Among such tables, you can find any.", "input_spec": "The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. For each test case the only line contains two integers $$$n$$$, $$$k$$$ $$$(1 \\le n \\le 300, 0 \\le k \\le n^2)$$$. It is guaranteed that the sum of $$$n^2$$$ for all test cases does not exceed $$$10^5$$$.", "output_spec": "For each test case, firstly print the minimum possible value of $$$f(A)$$$ among all tables, for which the condition is satisfied. After that, print $$$n$$$ lines contain $$$n$$$ characters each. The $$$j$$$-th character in the $$$i$$$-th line should be equal to $$$A_{i,j}$$$. If there are multiple answers you can print any.", "sample_inputs": ["4\n2 2\n3 8\n1 0\n4 16"], "sample_outputs": ["0\n10\n01\n2\n111\n111\n101\n0\n0\n0\n1111\n1111\n1111\n1111"], "notes": "NoteIn the first test case, the sum of all elements in the grid is equal to $$$2$$$, so the condition is satisfied. $$$R_1 = 1, R_2 = 1$$$ and $$$C_1 = 1, C_2 = 1$$$. Then, $$$f(A) = (1-1)^2 + (1-1)^2 = 0$$$, which is the minimum possible value of $$$f(A)$$$.In the second test case, the sum of all elements in the grid is equal to $$$8$$$, so the condition is satisfied. $$$R_1 = 3, R_2 = 3, R_3 = 2$$$ and $$$C_1 = 3, C_2 = 2, C_3 = 3$$$. Then, $$$f(A) = (3-2)^2 + (3-2)^2 = 2$$$. It can be proven, that it is the minimum possible value of $$$f(A)$$$."}, "positive_code": [{"source_code": "require 'set'; require 'prime'; require 'pp'\nINF=Float::INFINITY; MOD=10**9+7\nt = gets.chomp.to_i\nt.times do\n n,k = gets.chomp.split.map(&:to_i)\n\n ans = []\n if k == 0\n puts 0\n n.times do\n ans.push '0' * n\n end\n puts ans.join(\"\\n\")\n next\n elsif k == n*n\n puts 0\n n.times do\n ans.push '1' * n\n end\n puts ans.join(\"\\n\")\n next\n end\n\n if k % n == 0\n puts 0\n else\n puts 2\n end\n\n plus1 = k % n\n fill = k / n\n n.times do |i|\n fill_c = fill\n fill_c += 1 if i < plus1\n line = '1' * fill_c + '0' * (n - fill_c)\n # p [fill_c, line]\n ans.push line[i, n] + line[0, i]\n end\n puts ans.join(\"\\n\")\nend\n#\n"}], "negative_code": [], "src_uid": "0f18382d450be90edf1fd1a3770b232b"} {"nl": {"description": "You have a string $$$s$$$ of length $$$n$$$ consisting of only characters > and <. You may do some operations with this string, for each operation you have to choose some character that still remains in the string. If you choose a character >, the character that comes right after it is deleted (if the character you chose was the last one, nothing happens). If you choose a character <, the character that comes right before it is deleted (if the character you chose was the first one, nothing happens).For example, if we choose character > in string > > < >, the string will become to > > >. And if we choose character < in string > <, the string will become to <.The string is good if there is a sequence of operations such that after performing it only one character will remain in the string. For example, the strings >, > > are good. Before applying the operations, you may remove any number of characters from the given string (possibly none, possibly up to $$$n - 1$$$, but not the whole string). You need to calculate the minimum number of characters to be deleted from string $$$s$$$ so that it becomes good.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) – the number of test cases. Each test case is represented by two lines. The first line of $$$i$$$-th test case contains one integer $$$n$$$ ($$$1 \\le n \\le 100$$$) – the length of string $$$s$$$. The second line of $$$i$$$-th test case contains string $$$s$$$, consisting of only characters > and <.", "output_spec": "For each test case print one line. For $$$i$$$-th test case print the minimum number of characters to be deleted from string $$$s$$$ so that it becomes good.", "sample_inputs": ["3\n2\n<>\n3\n><<\n1\n>"], "sample_outputs": ["1\n0\n0"], "notes": "NoteIn the first test case we can delete any character in string <>.In the second test case we don't need to delete any characters. The string > < < is good, because we can perform the following sequence of operations: > < < $$$\\rightarrow$$$ < < $$$\\rightarrow$$$ <."}, "positive_code": [{"source_code": "N = gets.to_i\nN.times {\n n = gets.to_i\n s = gets.chomp\n p [s.match(/^<*/)[0].size, s.match(/>*$/)[0].size].min\n}\n"}, {"source_code": "gets.to_i.times{\n\tn = gets.to_i;\n\ts = gets.chomp;\n\tif s =~ /^<*<>|<>>*$/\n\t\tp [ s[/^<*/]||'', s[/>*$/]||'' ].map(&:length).min;\n\telse\n\t\tp 0;\n\tend\n}\n\n\n# <<<<<<<<<<<<>>>>>>>>>>>>><>>>>>>>>>>\n\n# < > <\n"}], "negative_code": [{"source_code": "N = gets.to_i\nN.times {\n n = gets.to_i\n s = gets.chomp\n p (s[0] == \">\" || s[-1] == \"<\") ? 0 : 1\n}\n"}, {"source_code": "gets.to_i.times{\n\tn = gets.to_i;\n\ts = gets.chomp;\n\tputs s =~ /^<*<>|<>>*$/ ? 1 : 0;\n}\n\n\n# <<<<<<<<<<<<>>>>>>>>>>>>><>\n"}, {"source_code": "gets.to_i.times{\n\tn = gets.to_i;\n\ts = gets.chomp;\n\tputs s =~ /<>/ ? 1 : 0;\n}\n"}, {"source_code": "gets.to_i.times{\n\tn = gets.to_i;\n\ts = gets.chomp;\n\tputs s =~ /^[^>]*<>[^<]*$/ ? 1 : 0;\n}\n"}], "src_uid": "0ba97bcfb5f539c848f2cd097b34ff33"} {"nl": {"description": "As meticulous Gerald sets the table, Alexander finished another post on Codeforces and begins to respond to New Year greetings from friends. Alexander has n friends, and each of them sends to Alexander exactly one e-card. Let us number his friends by numbers from 1 to n in the order in which they send the cards. Let's introduce the same numbering for the cards, that is, according to the numbering the i-th friend sent to Alexander a card number i.Alexander also sends cards to friends, but he doesn't look for the new cards on the Net. He simply uses the cards previously sent to him (sometimes, however, he does need to add some crucial details). Initially Alexander doesn't have any cards. Alexander always follows the two rules: He will never send to a firend a card that this friend has sent to him. Among the other cards available to him at the moment, Alexander always chooses one that Alexander himself likes most. Alexander plans to send to each friend exactly one card. Of course, Alexander can send the same card multiple times.Alexander and each his friend has the list of preferences, which is a permutation of integers from 1 to n. The first number in the list is the number of the favorite card, the second number shows the second favorite, and so on, the last number shows the least favorite card.Your task is to find a schedule of sending cards for Alexander. Determine at which moments of time Alexander must send cards to his friends, to please each of them as much as possible. In other words, so that as a result of applying two Alexander's rules, each friend receives the card that is preferred for him as much as possible.Note that Alexander doesn't choose freely what card to send, but he always strictly follows the two rules.", "input_spec": "The first line contains an integer n (2 ≤ n ≤ 300) — the number of Alexander's friends, equal to the number of cards. Next n lines contain his friends' preference lists. Each list consists of n different integers from 1 to n. The last line contains Alexander's preference list in the same format.", "output_spec": "Print n space-separated numbers: the i-th number should be the number of the friend, whose card Alexander receives right before he should send a card to the i-th friend. If there are several solutions, print any of them.", "sample_inputs": ["4\n1 2 3 4\n4 1 3 2\n4 3 1 2\n3 4 2 1\n3 1 2 4"], "sample_outputs": ["2 1 1 4"], "notes": "NoteIn the sample, the algorithm of actions Alexander and his friends perform is as follows: Alexander receives card 1 from the first friend. Alexander sends the card he has received (at the moment he only has one card, and therefore it is the most preferable for him) to friends with the numbers 2 and 3. Alexander receives card 2 from the second friend, now he has two cards — 1 and 2. Alexander sends a card to the first friend. Despite the fact that Alexander likes card 1 more, he sends card 2 as he cannot send a friend the card sent by that very friend. Alexander receives card 3 from the third friend. Alexander receives card 4 from the fourth friend. Among the cards Alexander has number 3 is his favorite and he sends it to the fourth friend. Note that Alexander can send cards to multiple friends at a time (in this case the second and the third one). Alexander can send card 3 to the fourth friend after he receives the third card or after he receives the fourth card (both variants are correct)."}, "positive_code": [{"source_code": "#! /usr/bin/ruby\n\nnum_friends = STDIN.gets.chomp(\"\\n\").to_i\nprefs = Hash.new\nfor i in (1..num_friends)\n prefs[i] = STDIN.gets.chomp(\"\\n\").split(\" \")\nend\n\nown_prefs = STDIN.gets.chomp(\"\\n\").split(\" \")\n\noutput = []\nfor i in (1..num_friends)\n possible = []\n prev = 1000\n own_prefs.each{|val|\n val = val.to_i\n next if val == i or val > prev\n prev = val\n possible << val\n }\n \n prefs[i].each{|val|\n val = val.to_i\n if possible.index(val)\n output << val\n break\n end\n }\nend\n\nputs output.join(\" \")"}, {"source_code": "n = gets.to_i\nt_preference = Array.new(n + 1)\ncard_pref = Array.new(n + 1)\nalex = Array.new(n + 1)\nfor i in 1 .. n\n\tt_preference[i] = Array.new(n + 1)\n\tcard_pref[i] = Array.new(n + 1)\n\t\n\ta = gets.split\n\tfor j in 1 .. n\n\t\tt_preference[i][a[j - 1].to_i] = j\n\tend\nend\n\na = gets.split\nfor j in 1 .. n\n\talex[j] = a[j - 1].to_i\nend\n\nfor i in 1 .. n\n\tfirst = 0\n\tsecond = 0\n\tfor j in 1 .. n\n\t\tif alex[j] <= i then\n\t\t\tif first == 0 then first = alex[j]\n\t\t\telse\n\t\t\t\tsecond = alex[j]\n\t\t\t\tbreak\n\t\t\tend\n\t\tend\n\tend\n\t\n\tfor j in 1 .. n\n\t\tif j == first then card = second\n\t\telse card = first end\n\t\tif card == 0 then card_pref[i][j] = 0\n\t\telse card_pref[j][i] = t_preference[j][card] end\n\tend\nend\n\nans = Array.new(n)\n\nfor i in 1 .. n\n\tmin = n + 1\n\tidx = 0\n\tfor j in 1 .. n\n\t\tif card_pref[i][j] != 0 then\n\t\t\tif card_pref[i][j] < min then\n\t\t\t\tmin = card_pref[i][j]\n\t\t\t\tidx = j\n\t\t\tend\n\t\tend\n\tend\n\tans[i - 1] = idx\nend\n\nputs ans.join(\" \")"}, {"source_code": "n = gets.chomp.to_i\nna = []\nfor i in 0..n-1\n wk = gets.chomp.split(/ /).map!{|x| x.to_i}\n wk.delete(i+1)\n na << wk\nend\naa = gets.chomp.split(/ /).map!{|x| x.to_i}\nfor i in 0..n-1\n x = aa[i]\n j = i-1\n revcnt = 0\n revx = 0\n while j >= 0 && revcnt <= 2\n if aa[j] < x\n revcnt += 1\n revx = aa[j]\n end\n j -= 1\n end\n for k in 0..n-1\n if revcnt >= 2 || (revcnt == 1 && (k+1) != revx)\n na[k].delete(x)\n end\n end\nend\nret = []\nfor i in 0..n-1\n ret << na[i][0]\nend\nputs ret.join(\" \")\n"}, {"source_code": "num_friends = STDIN.gets.chomp(\"\\n\").to_i\nprefs = Hash.new\nfor i in (1..num_friends)\n prefs[i] = STDIN.gets.chomp(\"\\n\").split(\" \")\nend\n\nown_prefs = STDIN.gets.chomp(\"\\n\").split(\" \")\n\noutput = []\nfor i in (1..num_friends)\n possible = []\n prev = 1000\n own_prefs.each{|val|\n val = val.to_i\n next if val == i or val > prev\n prev = val\n possible << val\n }\n \n prefs[i].each{|val|\n val = val.to_i\n if possible.index(val)\n output << val\n break\n end\n }\nend\n\nputs output.join(\" \")"}, {"source_code": "#!/usr/bin/ruby\n# coding: utf-8\n\nn = $stdin.gets.to_i\nm = Array.new(n) { Array.new(n) }\nn.times do |i|\n $stdin.gets.split(/\\s+/).map(&:to_i).each_with_index do |x, j|\n m[i][x-1] = j\n end\nend\nalex = Array.new n\n$stdin.gets.split(/\\s+/).map(&:to_i).each_with_index do |x, j|\n alex[x-1] = j\nend\n\nans = Array.new n\nc1 = nil\nc2 = nil\nn.times do |i|\n if c1.nil?\n c1 = i\n elsif c2.nil?\n if alex[i] < alex[c1]\n c1, c2 = i, c1\n else\n c2 = i\n end\n elsif alex[i] < alex[c1]\n c1, c2 = i, c1\n elsif alex[i] < alex[c2]\n c2 = i\n end\n\n ans.each_with_index do |a, j|\n c = c1 == j ? c2 : c1\n next if c.nil?\n if ans[j].nil? or m[j][ans[j]] > m[j][c]\n ans[j] = c\n end\n end\nend\nputs ans.map(&:succ).join(' ')\n"}], "negative_code": [{"source_code": "#! /usr/bin/ruby\n\nnum_friends = STDIN.gets.chomp(\"\\n\").to_i\nprefs = Hash.new\nfor i in (1..num_friends)\n prefs[i] = STDIN.gets.chomp(\"\\n\").split(\" \")\nend\n\nown_prefs = STDIN.gets.chomp(\"\\n\").split(\" \")\n\noutput = []\nfor i in (1..num_friends)\n possible = []\n prev = 1000\n own_prefs.each{|val|\n val = val.to_i\n next if val == i\n break if val > prev\n prev = val\n possible << val\n }\n \n prefs[i].each{|val|\n val = val.to_i\n if possible.index(val)\n output << val\n break\n end\n }\nend\n\nputs output.join(\" \")"}], "src_uid": "d8f8be420c1d9553240708f28f8ba4b2"} {"nl": {"description": "Twilight Sparkle was playing Ludo with her friends Rainbow Dash, Apple Jack and Flutter Shy. But she kept losing. Having returned to the castle, Twilight Sparkle became interested in the dice that were used in the game.The dice has m faces: the first face of the dice contains a dot, the second one contains two dots, and so on, the m-th face contains m dots. Twilight Sparkle is sure that when the dice is tossed, each face appears with probability . Also she knows that each toss is independent from others. Help her to calculate the expected maximum number of dots she could get after tossing the dice n times.", "input_spec": "A single line contains two integers m and n (1 ≤ m, n ≤ 105).", "output_spec": "Output a single real number corresponding to the expected maximum. The answer will be considered correct if its relative or absolute error doesn't exceed 10  - 4.", "sample_inputs": ["6 1", "6 3", "2 2"], "sample_outputs": ["3.500000000000", "4.958333333333", "1.750000000000"], "notes": "NoteConsider the third test example. If you've made two tosses: You can get 1 in the first toss, and 2 in the second. Maximum equals to 2. You can get 1 in the first toss, and 1 in the second. Maximum equals to 1. You can get 2 in the first toss, and 1 in the second. Maximum equals to 2. You can get 2 in the first toss, and 2 in the second. Maximum equals to 2. The probability of each outcome is 0.25, that is expectation equals to: You can read about expectation using the following link: http://en.wikipedia.org/wiki/Expected_value"}, "positive_code": [{"source_code": "#!/usr/bin/ruby\nm,n=gets.split.map(&:to_i)\np 1.0.step(m).reduce(0){|s,k|s+k*((k/m)**n-((k-1)/m)**n)}"}, {"source_code": "m,n=readline.split(' ').map(&:to_i)\nr=0\npp=0\ntp=0\n1.upto(m) do |x|\n p=(1.0*x/m)**n\n# puts \"#{x} - #{p}\"\n r+=x*(p-pp)\n tp+=p-pp\n pp=p\nend\n#puts \"total p #{tp}\"\nputs r\n"}, {"source_code": "m, n = gets.split().map(&:to_i)\nm = 1.0*m\ne = 0\nfor k in 1..m\n\te += k*((k/m)**n-((k-1)/m)**n)\nend\nputs e\n"}, {"source_code": "m,n = gets.split.map(&:to_f)\nres = (1/m)**n;\n(2..m.to_i).each do |k|\n res += (1 - (((k-1)/m)**n + 1 - (k/m)**n))*k\nend\nprint \"%0.12f\" % res\n"}, {"source_code": "m,n=gets.split.map &:to_i\ntot=m*1.0\n1.upto(m-1) {|i|tot -= (1.0*i/m)**n}\np tot"}, {"source_code": "m, n = gets.split.map {|i| i.to_i}\na = []\nfor i in 0...m\n a.push(((i + 1).to_f / m) ** n)\nend\n(1...m).reverse_each {|i|\n a[i] -= a[i - 1]\n}\nsum = 0\nfor i in 0...m\n sum += (i + 1) * a[i]\nend\nputs sum"}, {"source_code": "\nthrows = 0\nm , n = gets.split.map(&:to_i)\n#(1..m).each {|x| throws, results[x-1] = x ** n, x ** (n + 1) - throws * x }\nputs (1..m).map {|x| x * ((x.to_f/m) ** n - ((x-1.to_f)/m) ** n)}.inject(0) {|x, y| x + y}\n\n\n"}, {"source_code": "#!/usr/bin/env ruby\n\nm, n = gets.split.map(&:to_i)\nans = 0.0\n(1..m).each do |k|\n ans += k * (((k.to_f/m) ** n) - ((k-1.to_f) / m) ** n)\nend\n\nprintf \"%.8f\\n\", ans\n"}, {"source_code": "# E = Sum(k=1..m, P(k)), where P(k) - probability k is max of n throws\n# k = max x(i)\n# P(k) = P(x <= k) - P(x < k) (at least on x = k achieved)\n# P(x <= k) = (k/m)^n\n# P(x < k) = ((k-1)/m)^n\n\nclass C\n\n def c(n, k)\n\n end\n\n def run\n m, n = $stdin.gets.split(' ').map &:to_i\n e = 0.0\n\n (1..m).each do |k|\n pk = (1.0 * k / m) ** n - (1.0 * (k - 1) / m) ** n\n e += k * pk\n end\n\n puts e\n end\n\nend\n\nC.new.run if __FILE__ == $0"}, {"source_code": "m, n = gets.split.map &:to_i\n\nans = m\n\n1.upto(m - 1) do |i|\n ans -= (i.to_f / m) ** n\nend\nputs ans\n"}, {"source_code": "m, n = gets.split.map{|x| x.to_i}\np (1..m).map{|x| 1.0 * x * ( ( 1.0 * x / m ) ** n - ( 1.0 * ( x - 1 ) / m ) ** n )}.inject(:+)\n"}, {"source_code": "def bin_pow(a,b)\n\ta=a.to_i\n\tb=b.to_i\n\tr=1\n\twhile (b>0)\n\t\tif (b&1!=0)\n\t\t\tr*=a\n\t\tend\n\t\ta*=a\n\t\tb>>=1\n\tend\n\treturn r\nend\n\n(m,n)=gets.split.map{|x| x.to_i}\nm=m.to_f\nans=m\ni=1\nwhile (i 10000000000\n }\n return true\nend\n\ndef solve(m, n)\n if m == 1\n return 1.0\n end\n p \"@solve: m = #{m}, n = #{n}\"\n res = 0.0\n (1..m).each {|x|\n if x == 1\n res += 1.0\n elsif x == m\n else\n t = 1.0 * x / m\n p t\n end\n }\n return -1\n return (1..m).map{|x| x * ( x ** n - ( x - 1 ) ** n )}.inject(:+) * ( 1.0 / m ** n )\nend\n\nputs solve *gets.split.map{|x| x.to_i}\n\n# m, n = gets.split.map{|x| x.to_i}\n# puts (1..m).map{|x| x * ( x ** n - ( x - 1 ) ** n )}.inject(:+) * ( 1.0 / m ** n )\n"}], "src_uid": "f70ac2c4e0f62f9d6ad1e003aedd86b2"} {"nl": {"description": "One day Vasya decided to have a look at the results of Berland 1910 Football Championship’s finals. Unfortunately he didn't find the overall score of the match; however, he got hold of a profound description of the match's process. On the whole there are n lines in that description each of which described one goal. Every goal was marked with the name of the team that had scored it. Help Vasya, learn the name of the team that won the finals. It is guaranteed that the match did not end in a tie.", "input_spec": "The first line contains an integer n (1 ≤ n ≤ 100) — the number of lines in the description. Then follow n lines — for each goal the names of the teams that scored it. The names are non-empty lines consisting of uppercase Latin letters whose lengths do not exceed 10 symbols. It is guaranteed that the match did not end in a tie and the description contains no more than two different teams.", "output_spec": "Print the name of the winning team. We remind you that in football the team that scores more goals is considered the winner.", "sample_inputs": ["1\nABC", "5\nA\nABA\nABA\nA\nA"], "sample_outputs": ["ABC", "A"], "notes": null}, "positive_code": [{"source_code": "goals = gets.to_i\nh = {}\ngoals.times do |i|\n name = gets.chomp\n if h[name] then \n h[name] += 1\n else\n h[name] = 1\n end\nend\nmas = h.keys\nif mas.size > 1 then\n if h[mas[0]] > h[mas[1]] then\n puts mas[0]\n else\n puts mas[1]\n end\nelse\n puts mas[0]\nend\n"}, {"source_code": "h=Hash.new(0)\ngets.to_i.times{s=gets\nh[s]+=1}\nputs h.max{|x,y|x[1]<=>y[1]}[0]\n"}, {"source_code": "n=gets.to_i\nc={}\nfor i in 1..n do\n\tr=gets\n\tc[r]=c[r].to_i+1\nend\nmax=0\nmaxc=\"\"\nc.keys.each {|k| \n\tif c[k]>max\n\t\tmaxc=k\n\t\tmax=c[k]\n\tend\n}\nputs maxc"}, {"source_code": "puts [*$<][1..-1].group_by{|i|i}.max_by{|i,j|j.size}[0]"}, {"source_code": "$><<(s=$<.sort)[s.size/2]"}, {"source_code": "$><<(s=$<.sort)[s.size/2]\n"}, {"source_code": "$><<(s=$<.sort)[s.size/2]\n"}, {"source_code": "\ncount = gets.to_i\nr = {}\ncount.times{|n|\n s = gets\n if r[s] then\n r[s] += 1\n else\n r[s] = 1\n end\n}\n\nanswer = ''\ntmp = 0\nr.each {|team,point|\n if tmp <= point then\n answer = team\n tmp = point\n end\n}\n\nputs answer \n"}, {"source_code": "$><<(s=$<.sort)[s.size/2]"}, {"source_code": "n=gets.to_i\nx=gets.chomp\na=[1,0]\ny=nil\nn-=1\nn.times do\n\tl=gets.chomp\n\tif x==l\n\t\ta[0]+=1\n\telse\n\t\ta[1]+=1\n\t\tif !y\n\t\t\ty=l\n\t\tend\n\tend\nend\nputs a[0]>a[1]? x : y"}, {"source_code": "gets\nputs (a = gets(p).split).max_by { |x| a.count x }"}, {"source_code": "n = gets.to_i\na = Array.new(n)\n\nfor i in 0..n-1\n a[i] = gets.to_s.chomp\nend\nif n == 1 \n puts a[0]\nelse\n counter = 0\n for i in 1..n-1\n if a[i] != a[0]\n d = a[i]\n counter +=1\n end\n end\n\n f = a.length\n if counter > f/2\n puts d \n else\n puts a[0]\n end\n end"}, {"source_code": "$><<(s=$<.sort)[s.size/2]\n"}, {"source_code": "$><<(s=$<.sort)[s.size/2]\n"}, {"source_code": "total_goals = gets.chomp.to_i\nteams = {}\ntotal_goals.times do\n team = gets.chomp\n if !teams.has_key? team\n teams.store team, 1\n else\n teams[team] += 1\n end\nend\nmax_goals = teams.values.max\nwinner = teams.select { |k, v| v == max_goals }.to_a[0][0]\nputs winner\n"}, {"source_code": "n=gets.to_i\nh=Hash.new(0)\nn.times{\n\ts=gets.chomp\n\th[s]+=1\n}\nc=0\nans=\"\"\nh.each{|k,v|\n\tif c<<(s=$<.sort)[s.size/2]"}, {"source_code": "n=gets.to_i\n\nhs=Hash.new(0)\nn.times{\n\ths[gets.chomp]+=1\n}\n\nmaxy=hs.values.max\nputs hs.invert[maxy]\n"}, {"source_code": "s = gets(p).split.sort\nputs s[s.size/2]"}, {"source_code": "n = 1\nt1, t2 = nil, nil\ngets.to_i.times do\n if t1\n t = gets.chomp\n n += t == t1 ? 1 : -1\n t2 = t if t != t1\n else\n t1 = gets.chomp\n end\nend\n\nputs n > 0 ? t1 : t2"}, {"source_code": "goals = STDIN.read.split\ngoals.shift\n\nresult = goals.inject(Hash.new(0)) { |score, team| score[team] += 1; score }\nputs result.max_by { |k,v| v } .first\n"}, {"source_code": "n = gets.to_i\nh = Hash.new\n(1..n).each do |x|\n s = gets\n if h[s] == nil then h[s] = 1 else h[s] = h[s] + 1 end\nend\nputs h.sort { |x,y| y[1] <=> x[1] }[0][0]"}, {"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\n\nn = gets.to_i\nteams = {}\nn.times {\n name = gets.chomp\n teams[name] ||= 0\n teams[name] += 1\n}\nwinner_score = teams.values.max\nteams.each_pair { |k, v|\n if v == winner_score\n puts k\n end\n}"}, {"source_code": "$><<(s=$<.sort)[s.size/2]\n"}, {"source_code": "n = gets.to_i\ncount = Hash.new(0)\nn.times { \n\ta = gets.chomp\n\tcount[a] += 1\n}\nputs count.key(count.values.max)"}, {"source_code": "k=gets.to_i\nl=gets\nn=''\nmas=[1,0] \n(k-1).times{ m=gets\n m==l ?mas[0]+=1 :mas[1]+=1\n m==l ?l=m :n=m \n }\nputs mas[0]>mas[1] ?l:n"}, {"source_code": "n = gets.to_i\n\nmas = {\"abacabaabacaba\" => -1}\nfor i in 0..n-1\n s = gets\n if mas.has_key?(s)\n mas[s] += 1\n else\n mas[s] = 0\n end\nend\n\nresult = \"\"\nmax = -1\nmas.each do |key,value|\n if value > max\n max = value\n result = key\n end\nend\n\nputs result"}, {"source_code": "n = gets.to_i\nputs n.times.map{gets.chomp}.sort[(n - 1)/2]\n"}, {"source_code": "n = gets.to_i\nh = Hash.new(0)\nn.times{h[gets.chomp] += 1}\nputs h.max_by{|x, y| y}.first\n"}, {"source_code": "lines = []\n#読み込み\nn = gets.chomp.to_i\nn.times {\n lines << gets.chomp\n}\n\n#チーム名を出す\nteams = []\nlines.each { |str|\n if teams == []\n teams[0] = str\n elsif teams[0] != str\n teams[1] = str\n end\n}\n\n#得点を出す\nscores = [0, 0]\nlines.each { |str|\n if str == teams[0]\n scores[0] = scores[0] + 1\n elsif str == teams[1]\n scores[1] = scores[1] + 1\n end\n}\n\n#表示\nputs (if scores[0] > scores[1] then teams[0] else teams[1] end)\n"}, {"source_code": "goals = gets.chomp.to_i.times.map { gets.chomp }\na, b = 0, 0\ngoals.each { |team| team == goals[0] ? a += 1 : b += 1 }\nputs a > b ? goals[0] : (goals - [goals[0]])[0]"}, {"source_code": "c=Hash.new 0\n[*$<].drop(1).each{|w| c[w] += 1}\nputs c.max_by{|k,v| v}[0]"}, {"source_code": "$><<(s=$<.sort)[s.size/2]\n"}, {"source_code": "a = 1.upto(gets.to_i).map { gets.chomp }\n\nputs a.uniq.max { |p, q| a.count(p) <=> a.count(q) }\n"}, {"source_code": "commands = []\nfor _ in 1..Integer(gets.chomp)\n commands << gets.chomp\nend\nputs commands.inject(Hash.new(0)) { |h, e| h[e] += 1; h }.max_by { |k, v| v }.first"}, {"source_code": "$><<(s=$<.sort)[s.size/2]\n"}, {"source_code": "n = gets.to_i\n\ninput = Array.new(n) { gets.strip }\n\nteams = input.sort.uniq\ncount = teams.map { |team| input.count(team) }\n\nputs teams.max_by { |team| count[teams.index(team)] }\n"}, {"source_code": "gets\na=gets(p).split\n$><<<(s=$<.sort)[s.size/2]\n"}], "negative_code": [{"source_code": "\ncount = gets.to_i\nr = {}\ncount.times{|n|\n s = gets\n if r.value?(s) then\n r[s] += 1\n else\n r[s] = 1\n end\n}\n\nanswer = ''\ntmp = 0\nr.each {|team,point|\n if tmp < point then\n answer = team\n end\n}\n\nputs answer\n"}, {"source_code": "\ncount = gets.to_i\nr = {}\ncount.times{|n|\n s = gets\n if r.value?(s) then\n r[s] += 1\n else\n r[s] = 1\n end\n}\n\nanswer = ''\ntmp = 0\nr.each {|team,point|\n if tmp <= point then\n answer = team\n end\n}\n\nputs answer\n"}, {"source_code": "\ncount = gets.to_i\nr = {}\ncount.times{|n|\n s = gets\n if r[s] then\n r[s] += 1\n else\n r[s] = 1\n end\n}\n\nanswer = ''\ntmp = 0\nr.each {|team,point|\n if tmp <= point then\n answer = team\n end\n}\n\nputs answer \n"}, {"source_code": "\ncount = gets.to_i\nr = {}\ncount.times{|n|\n s = gets\n if r.value?(s) then\n r[s] += 1\n else\n r[s] = 1\n end\n}\n\nanswer = ''\ntmp = 0\nr.each {|team,point|\n if tmp < point then\n answer = team\n end\n}\n\nprint answer\n"}, {"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\n\nn = gets.to_i\nalr = Array.new(26) { 0 }\nn.times {\n mini_alr = Array.new(26) { 0 }\n\n gets.chomp.split(//).each { |c|\n o = c.ord - 'A'.ord\n mini_alr[o] += 1\n }\n max_mini_alr = mini_alr.max\n mini_alr.each_with_index { |val, index|\n if val == max_mini_alr\n alr[index] += 1\n end\n }\n}\n\nmax_alr = alr.max\nteam_names = \"\" # what?\nalr.each_with_index { |val, index|\n if val == max_alr\n team_names += ('A'.ord + index).chr\n end\n}\n\nputs team_names"}, {"source_code": "n = gets.to_i\nh = Hash.new(0)\nn.times{gets.chomp.chars.each{|x| h[x] += 1}}\nmx = h.values.max\nputs h.keys.select{|x| h[x] == mx}.join\n"}, {"source_code": "n = gets.to_i\nh = Hash.new(0)\nn.times{h[gets.chomp] += 1}\nputs h.max_by{|x| h[x]}[0]\n"}, {"source_code": "a=gets(p).split\n$><= 1\n numA -= 1\n min -= 2\n end\n end\n end\n min -= ((min - numA) / 2)*2\n ans << min\nend\nans.each do |v|\n puts v\nend"}], "negative_code": [], "src_uid": "ee295fd90ee9283709447481f172c73c"} {"nl": {"description": "Peter Parker wants to play a game with Dr. Octopus. The game is about cycles. Cycle is a sequence of vertices, such that first one is connected with the second, second is connected with third and so on, while the last one is connected with the first one again. Cycle may consist of a single isolated vertex.Initially there are k cycles, i-th of them consisting of exactly vi vertices. Players play alternatively. Peter goes first. On each turn a player must choose a cycle with at least 2 vertices (for example, x vertices) among all available cycles and replace it by two cycles with p and x - p vertices where 1 ≤ p < x is chosen by the player. The player who cannot make a move loses the game (and his life!).Peter wants to test some configurations of initial cycle sets before he actually plays with Dr. Octopus. Initially he has an empty set. In the i-th test he adds a cycle with ai vertices to the set (this is actually a multiset because it can contain two or more identical cycles). After each test, Peter wants to know that if the players begin the game with the current set of cycles, who wins? Peter is pretty good at math, but now he asks you to help.", "input_spec": "The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of tests Peter is about to make. The second line contains n space separated integers a1, a2, ..., an (1 ≤ ai ≤ 109), i-th of them stands for the number of vertices in the cycle added before the i-th test.", "output_spec": "Print the result of all tests in order they are performed. Print 1 if the player who moves first wins or 2 otherwise.", "sample_inputs": ["3\n1 2 3", "5\n1 1 5 1 1"], "sample_outputs": ["2\n1\n1", "2\n2\n2\n2\n2"], "notes": "NoteIn the first sample test:In Peter's first test, there's only one cycle with 1 vertex. First player cannot make a move and loses.In his second test, there's one cycle with 1 vertex and one with 2. No one can make a move on the cycle with 1 vertex. First player can replace the second cycle with two cycles of 1 vertex and second player can't make any move and loses.In his third test, cycles have 1, 2 and 3 vertices. Like last test, no one can make a move on the first cycle. First player can replace the third cycle with one cycle with size 1 and one with size 2. Now cycles have 1, 1, 2, 2 vertices. Second player's only move is to replace a cycle of size 2 with 2 cycles of size 1. And cycles are 1, 1, 1, 1, 2. First player replaces the last cycle with 2 cycles with size 1 and wins.In the second sample test:Having cycles of size 1 is like not having them (because no one can make a move on them). In Peter's third test: There a cycle of size 5 (others don't matter). First player has two options: replace it with cycles of sizes 1 and 4 or 2 and 3. If he replaces it with cycles of sizes 1 and 4: Only second cycle matters. Second player will replace it with 2 cycles of sizes 2. First player's only option to replace one of them with two cycles of size 1. Second player does the same thing with the other cycle. First player can't make any move and loses. If he replaces it with cycles of sizes 2 and 3: Second player will replace the cycle of size 3 with two of sizes 1 and 2. Now only cycles with more than one vertex are two cycles of size 2. As shown in previous case, with 2 cycles of size 2 second player wins. So, either way first player loses."}, "positive_code": [{"source_code": "n = gets.to_i\nmas = gets.split.map(&:to_i)\n\ns = 0\n\nfor i in 0...n do\n s += mas[i] - 1\n puts s%2==1? '1' : '2'\nend\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nsum = 0\na.each{ |x|\n sum += x - 1\n puts 2 - sum % 2\n}\n"}, {"source_code": "n = gets.strip.to_i\na = gets.strip.split.map(&:to_i)\nans = 0\nn.times do |i|\n ans += a[i] - 1\n if ans.even? || ans == 0\n puts \"2\"\n else\n puts \"1\"\n end\nend\n"}, {"source_code": "\nn = gets.to_i\n\nnumbers = gets\n\nres = numbers.split(' ')\nt_s = 0\nres.each_with_index do |r,i|\n c = r.to_i\n t_s += (c-1)\n if (t_s > 1000000000)\n t_s = t_s - 1000000000\n end\n if (t_s % 2 == 1)\n puts 1\n else\n puts 2\n end\nend\n\n"}, {"source_code": "class Array\n def r\n self.map { |q| q.to_i }\n end\nend\n\ndef read_line\n STDIN.gets.split(\" \")\nend\n\ndef read_to_array\n read_line.to_a\nend\n\ndef read_numbers_to_array\n read_to_array.r\nend\n\ndef change(c)\n return 2 if c == 1\n return 1\nend\n\nn = STDIN.gets.to_i\n\na = read_numbers_to_array\ncur_win = 2\na.each do |x| \n cur_win = change(cur_win) if x % 2 == 0\n puts cur_win\nend\n"}, {"source_code": "gets\ncycles = gets.split(' ').map(&:to_i)\n\nres = 0\ncycles.size.times do |n|\n\tres += cycles[n].even? ? 1 : 2\n\tprint res.even? ? 2 : 1,\"\\n\"\nend"}, {"source_code": "@n = STDIN.gets.to_i\n@sum = 0\n@count = 0\nSTDIN.gets.split(' ').collect{ |x| x.to_i}.each do |x|\n\t@sum += x \n\t@count += 1\n\tputs (@sum-@count)%2 == 0 ? 2 : 1\nend\n"}, {"source_code": "winCache = {}\nwinCache[1] = 0\nwinCache[2] = 1\nwinCache[3] = 2\n\nn = gets.chomp.to_i\naddedVertices = gets.chomp.split(' ').map(&:to_i)\n\ntotalTurn = 0\n\naddedVertices.each do |v|\n\tif winCache.has_key? v\n\t\ttotalTurn += winCache[v]\n\telse\n\t\twinCache[v] = v - 1\n\t\ttotalTurn += v - 1\n\tend\n\n\tputs '2' if totalTurn % 2 == 0\n\tputs '1' if totalTurn % 2 == 1\nend\n"}], "negative_code": [{"source_code": "@n = STDIN.gets.to_i\nSTDIN.gets.split(' ').collect{ |x| x.to_i}.each do |x|\n\tputs x*(x-1)/2%2 == 0 ? 1 : 2\nend\n"}], "src_uid": "3a767b3040f44e3e2148cdafcb14a241"} {"nl": {"description": "Daniel is organizing a football tournament. He has come up with the following tournament format: In the first several (possibly zero) stages, while the number of teams is even, they split in pairs and play one game for each pair. At each stage the loser of each pair is eliminated (there are no draws). Such stages are held while the number of teams is even. Eventually there will be an odd number of teams remaining. If there is one team remaining, it will be declared the winner, and the tournament ends. Otherwise each of the remaining teams will play with each other remaining team once in round robin tournament (if there are x teams, there will be games), and the tournament ends. For example, if there were 20 teams initially, they would begin by playing 10 games. So, 10 teams would be eliminated, and the remaining 10 would play 5 games. Then the remaining 5 teams would play 10 games in a round robin tournament. In total there would be 10+5+10=25 games.Daniel has already booked the stadium for n games. Help him to determine how many teams he should invite so that the tournament needs exactly n games. You should print all possible numbers of teams that will yield exactly n games in ascending order, or -1 if there are no such numbers.", "input_spec": "The first line contains a single integer n (1 ≤ n ≤ 1018), the number of games that should be played. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.", "output_spec": "Print all possible numbers of invited teams in ascending order, one per line. If exactly n games cannot be played, output one number: -1.", "sample_inputs": ["3", "25", "2"], "sample_outputs": ["3\n4", "20", "-1"], "notes": null}, "positive_code": [{"source_code": "def gao(x, y)\n x * (2 ** y - 1) + x * (x - 1) / 2\nend\n\nn = gets.to_i\nans = []\n\n0.upto(100) do |b|\n l = 1\n r = 10 ** 20\n while l < r do\n m = (l + r) / 2\n if gao(m, b) < n\n l = m + 1\n else\n r = m\n end\n end\n ans << (r << b) if r.odd? && gao(r, b) == n\nend\n\nans = ans.uniq.sort\nans << -1 if ans.empty?\nputs ans\n"}, {"source_code": "n = gets.to_i\n\nres = []\n(0..66).each do |m|\n b = 2 * (2**m - 1) - 1\n x = (-b + Math.sqrt(b*b + 4 * (2 * n))) / 2\n x = x.to_i\n\n ([1, x - 10].max..(x + 10)).each do |k|\n if k % 2 == 1\n t = k * (2**m - 1) + k * (k - 1) / 2\n res << 2**m * k if t == n\n end\n end\nend\n\nputs res.empty? ? -1 : res\n"}, {"source_code": "@n = $stdin.read.to_i\n@result = []\n\ndef checkOne\n x = 0\n t = 1\n for j in 0..70 do\n @result << t if (x == @n)\n x += t\n t *= 2\n end\nend\n\ndef get_sqrt x\n return 0 if x==0 \n m = x\n p = x\n result = m\n loop do\n r = (m + p/m) / 2\n if m <= r\n result = m\n break\n end\n m = r\n end\n return result * result == x ? result : nil\nend\n\ndef checkSimple\n for m in 0..70 do\n d = 2**(2*m+2) - 3 * (2**(m+2)) + 9 + 8 * @n\n next if d < 0\n sqrtd = get_sqrt(d)\n next if sqrtd == nil\n next if sqrtd % 2 == 0\n a = (-(2**(m+1))+3+sqrtd) / 2\n @result << a*(2**m) if a > 0 && a % 2 == 1\n a = (-(2**(m+1))+3-sqrtd) / 2\n @result << a*(2**m) if a > 0 && a % 2 == 1\n end\nend\n\n#checkOne\ncheckSimple\n\n@result = @result.sort.uniq\n@result.each do |a|\n print a, \"\\n\"\n r = 0\nend\nif @result.size == 0\n print -1, \"\\n\"\nend"}, {"source_code": "require 'rational'\n\nclass Rational\n def sqrt_floor\n if self < 0 then\n raise 'can\\'t take the square root of a negative number'\n end\n\n return 0 if self == 0\n\n prev = nil\n cur = self\n\n while prev.nil? or (cur - prev).abs >= 1 do\n prev = (cur * 10).round.to_r / 10\n cur = (prev + self / prev) / 2\n end\n\n cur.to_i\n end\nend\n\nn = gets.to_i\n\nans = []\npow = 1\nexp = 0\nloop do\n break if pow - 1 > n\n disc = (2 * pow - 3) ** 2 + 8 * n\n sq = Rational(disc, 1).sqrt_floor\n if sq * sq == disc && sq & 1 == 1\n teams = ((sq + 3) >> 1) - pow\n ans.push(pow * teams) if teams & 1 == 1\n end\n pow <<= 1\n exp += 1 \nend\n\nans.push(-1) if ans.empty?\nans.sort.uniq.each { |x| puts x }\n"}, {"source_code": "n=gets.to_i\ng=->k{k*(k+$m)}\np,u,o=1,[],2*n\nwhile p<=n+1 do\n $m=2*p-3\n l,r=-1,n+1\n while r-l>1\n k=l+r>>1\n s=g.(k)\n u<0&&s==o\n s>o ? r=k : l=k\n end\n p<<=1\nend\nputs u.empty?? -1:u.sort.uniq\n"}, {"source_code": "n=gets.to_i\np,r=1,[]\ndef g(k,m)\n k*(k+m)\nend\ndef f(a,b,m,n)\n return 0 if g(a,m)>n\n return 0 if g(b,m)k{k*(k+$m)}\n return a if g.(a)==$n\n return b if g.(b)==$n\n return 0 if g.(a)>$n || g.(b)<$n || (a-b).abs<=1\n c=a+b>>1\n return g.(c)<$n ? f(c,b):f(a,c)\nend\np,r,$n=1,[],2*n\nwhile p<=n+1 do\n $m=2*p-3\n k=f(1,n)\n r<=0\n d=BigDecimal(d).sqrt(100).round\n q=3-2*p\n x=[q+d>>1,q-d>>1]\n x.each{|y|r<=1 and y%2==1 and (p-1)*y+(y-1)*y/2==n}\n end\n p <<= 1\nend\nr.empty? ? puts(-1):puts(r.sort.uniq)\n"}], "negative_code": [{"source_code": "n = gets.to_i\n\nres = []\n(0..66).each do |m|\n b = 2 * (2**m - 1) - 1\n x = (-b + Math.sqrt(b*b + 4 * (2 * n)))\n x = x.to_i\n \n ([1, x].max..(x + 10)).each do |k|\n if k % 2 == 1\n t = x * (2**m - 1) + x * (x - 1) / 2\n res << t if t == n\n end\n end\nend\n\nputs res.empty? ? -1 : res\n"}, {"source_code": "n = gets.to_i\n\nres = []\n(0..66).each do |m|\n b = 2 * (2**m - 1) - 1\n x = (-b + Math.sqrt(b*b + 4 * (2 * n)))\n x = x.to_i\n ([1, x - 10].max..(x + 10)).each do |k|\n if k % 2 == 1\n t = k * (2**m - 1) + k * (k - 1) / 2\n res << 2**m * k if t == n\n end\n end\nend\n\nputs res.empty? ? -1 : res\n"}, {"source_code": "n = gets.to_i\n\nres = []\n(0..66).each do |m|\n b = 2 * (2**m - 1) - 1\n x = (-b + Math.sqrt(b*b + 4 * (2 * n)))\n x = x.to_i\n ([1, x - 10].max..(x + 10)).each do |k|\n if k % 2 == 1\n t = k * (2**m - 1) + k * (k - 1) / 2\n res << t if t == n\n end\n end\nend\n\nputs res.empty? ? -1 : res\n"}, {"source_code": "@n = $stdin.read.to_i\n@result = []\n\ndef checkOne\n x = 0\n t = 1\n for j in 0..70 do\n @result << t if (x == @n)\n x += t\n t *= 2\n end\nend\n\ndef get_sqrt x\n return 0 if x==0 \n m = x\n p = x\n result = m\n loop do\n r = (m + p/m) / 2\n if m <= r\n result = m\n break\n end\n m = r\n end\n return result * result == x ? result : nil\nend\n\ndef checkSimple\n for m in 0..70 do\n d = 2**(2*m+2) - 3 * (2**(m+2)) + 9 + 8 * @n\n next if d < 0\n sqrtd = get_sqrt(d)\n next if sqrtd == nil\n next if sqrtd % 2 == 0\n a = (-(2**(m+1))+3+sqrtd) / 2\n @result << a*(2**m) if a > 0\n a = (-(2**(m+1))+3-sqrtd) / 2\n @result << a*(2**m) if a > 0\n end\nend\n\n#checkOne\ncheckSimple\n\n@result = @result.sort.uniq\n@result.each do |a|\n print a, \"\\n\"\n r = 0\nend\nif @result.size == 0\n print -1, \"\\n\"\nend"}, {"source_code": "@n = $stdin.read.to_i\n@result = []\n\ndef checkOne\n x = 0\n t = 1\n for j in 0..70 do\n @result << t if (x == @n)\n x += t\n t *= 2\n end\nend\n\ndef get_sqrt a\n d = Math.sqrt(a).to_i\n for t in ([d-1,0].max..(d+1))\n return t if t * t == a\n end\n return nil\nend\n\ndef checkSimple\n for m in 0..70 do\n d = 2**(2*m+2) - 3 * (2**(m+2)) + 9 + 8 * @n\n next if d < 0\n sqrtd = get_sqrt(d)\n next if sqrtd == nil\n next if sqrtd % 2 == 0\n a = (-(2**(m+1))+3+sqrtd) / 2\n @result << a*(2**m) if a > 0\n a = (-(2**(m+1))+3-sqrtd) / 2\n @result << a*(2**m) if a > 0\n end\nend\n\ncheckOne\ncheckSimple\n\n@result = @result.sort.uniq\n@result.each do |a|\n print a, \"\\n\"\nend\nif @result.size == 0\n print -1, \"\\n\"\nend"}, {"source_code": "@n = $stdin.read.to_i\n@result = []\n\ndef get_sqrt x\n return 0 if x==0 \n m = x\n p = x\n result = m\n loop do\n r = (m + p/m) / 2\n if m <= r\n result = m\n break\n end\n m = r\n end\n return result * result == x ? result : nil\nend\n\ndef checkSimple\n for m in 0..200 do\n d = 2**(2*m+2) - 3 * (2**(m+2)) + 9 + 8 * @n\n next if d < 0\n sqrtd = get_sqrt(d)\n next if sqrtd == nil\n next if sqrtd % 2 == 0\n a = (-(2**(m+1))+3+sqrtd) / 2\n @result << a*(2**m) if a > 0\n a = (-(2**(m+1))+3-sqrtd) / 2\n @result << a*(2**m) if a > 0\n end\nend\n\ncheckSimple\n\n@result = @result.sort.uniq\n@result.each do |a|\n print a, \"\\n\"\nend\nif @result.size == 0\n print -1, \"\\n\"\nend"}, {"source_code": "require 'bigdecimal'\nn=gets.to_i\np,r=1,[]\nwhile p<=n+1 do\n d=8*n+4*p*p-12*p+9\n if d>=0\n d=BigDecimal(d).sqrt(100).to_i\n q=3-2*p\n x=[q+d>>1,q-d>>1]\n x.each{|y|r<=1 and y%2==1 and (p-1)*y+(y-1)*y/2==n}\n end\n p <<= 1\nend\nr.empty? ? puts(-1):puts(r.sort.uniq)\n"}, {"source_code": "n=gets.to_i\np,r=1,[]\nwhile p<=n do\n d=8*n+4*p*p-12*p+9\n if d>=0\n d=(d**0.5).round\n q=-p+3/2\n x=[q+d,q-d]\n x.each{|y|r<=1and y%2==1and (p-1)*y+(y-1)*y/2==n}\n end\n p <<= 1\nend\nr.empty? ? puts(-1):puts(r.sort.uniq)\n"}, {"source_code": "n=gets.to_i\np,r=1,[]\nwhile p<=n+100 do\n if 8*n+4*p*p-12*p+9 >= 0\n d=Math.sqrt(8*n+4*p*p-12*p+9)\n x1=(-d-2*p+3)/2.0\n x2=(d-2*p+3)/2.0\n r << p*x1.to_i if x1>=1 and x1.to_i == x1 and x1%2==1 and (p-1)*x1+(x1-1)*x1/2==n\n r << p*x2.to_i if x2>=1 and x2.to_i == x2 and x2%2==1 and (p-1)*x2+(x2-1)*x2/2==n\n end\n p <<= 1\nend\nif !r.empty?\n puts r.sort.uniq\nelse\n puts -1\nend\n"}, {"source_code": "n=gets.to_i\np,r=1,[]\nwhile p<=n do\n if 8*n+4*p*p-12*p+9 >= 0\n d=Math.sqrt(8*n+4*p*p-12*p+9)\n x1=(-d-2*p+3)/2\n x2=(d-2*p+3)/2\n r << p*x1.to_i if x1>=1 and x1.to_i == x1\n r << p*x2.to_i if x2>=1 and x2.to_i == x2\n end\n p <<= 1\nend\nif !r.empty?\n puts r.sort\nelse\n puts -1\nend\n"}, {"source_code": "n=gets.to_i\ndef f(a,b)\n g=->k{k*(k+$m)}\n return 0 if g.(a)>$n||g.(b)<$n||(a-b).abs<=1\n return a if g.(a)==$n\n return b if g.(b)==$n\n c=a+b>>1\n return g.(c)<$n ? f(c,b):f(a,c)\nend\np,r,$n=1,[],2*n\nwhile p<=n+1 do\n $m=2*p-3\n k=f(1,n)\n r<=0\n d=(d**0.5).round\n q=3-2*p\n x=[q+d>>1,q-d>>1]\n x.each{|y|r<=1 and y%2==1 and (p-1)*y+(y-1)*y/2==n}\n end\n p <<= 1\nend\nr.empty? ? puts(-1):puts(r.sort.uniq)\n"}, {"source_code": "n=gets.to_i\np,r=1,[]\nwhile p<=n+100 do\n if 8*n+4*p*p-12*p+9 >= 0\n d=Math.sqrt(8*n+4*p*p-12*p+9)\n x1=(-d-2*p+3)/2.0\n x2=(d-2*p+3)/2.0\n r << p*x1.to_i if x1>=1 and x1.to_i == x1 and x1%2==1\n r << p*x2.to_i if x2>=1 and x2.to_i == x2 and x2%2==1\n end\n p <<= 1\nend\nif !r.empty?\n puts r.sort.uniq\nelse\n puts -1\nend\n"}, {"source_code": "n=gets.to_i\np,r=1,[]\nwhile p<=n+100 do\n if 8*n+4*p*p-12*p+9 >= 0\n d=Math.sqrt(8*n+4*p*p-12*p+9).round(10)\n x1=((-d-2*p+3)/2.0).round(10)\n x2=((d-2*p+3)/2.0).round(10)\n r << p*x1.to_i if x1>=1 and x1.to_i == x1 and x1%2==1 and (p-1)*x1+(x1-1)*x1/2==n\n r << p*x2.to_i if x2>=1 and x2.to_i == x2 and x2%2==1 and (p-1)*x2+(x2-1)*x2/2==n\n end\n p <<= 1\nend\nif !r.empty?\n puts r.sort.uniq\nelse\n puts -1\nend\n"}, {"source_code": "n=gets.to_i\np,r=1,[]\nwhile p<=n+100 do\n if 8*n+4*p*p-12*p+9 >= 0\n d=Math.sqrt(8*n+4*p*p-12*p+9).round\n x1=(-d-2*p+3)/2\n x2=(d-2*p+3)/2\n r << p*x1.to_i if x1>=1 and x1%2==1 and (p-1)*x1+(x1-1)*x1/2==n\n r << p*x2.to_i if x2>=1 and x2%2==1 and (p-1)*x2+(x2-1)*x2/2==n\n end\n p <<= 1\nend\nif !r.empty?\n puts r.sort.uniq\nelse\n puts -1\nend\n"}, {"source_code": "n=gets.to_i\np,r=1,[]\nwhile p<=n+100 do\n if 8*n+4*p*p-12*p+9 >= 0\n d=Math.sqrt(8*n+4*p*p-12*p+9)\n x1=(-d-2*p+3)/2.0\n x2=(d-2*p+3)/2.0\n r << p*x1.to_i if x1>=1 and x1.to_i == x1\n r << p*x2.to_i if x2>=1 and x2.to_i == x2\n end\n p <<= 1\nend\nif !r.empty?\n puts r.sort.uniq\nelse\n puts -1\nend\n"}], "src_uid": "7589b30ec643278d8a83d74d43d9aebe"} {"nl": {"description": "New Year is coming in Line World! In this world, there are n cells numbered by integers from 1 to n, as a 1 × n board. People live in cells. However, it was hard to move between distinct cells, because of the difficulty of escaping the cell. People wanted to meet people who live in other cells.So, user tncks0121 has made a transportation system to move between these cells, to celebrate the New Year. First, he thought of n - 1 positive integers a1, a2, ..., an - 1. For every integer i where 1 ≤ i ≤ n - 1 the condition 1 ≤ ai ≤ n - i holds. Next, he made n - 1 portals, numbered by integers from 1 to n - 1. The i-th (1 ≤ i ≤ n - 1) portal connects cell i and cell (i + ai), and one can travel from cell i to cell (i + ai) using the i-th portal. Unfortunately, one cannot use the portal backwards, which means one cannot move from cell (i + ai) to cell i using the i-th portal. It is easy to see that because of condition 1 ≤ ai ≤ n - i one can't leave the Line World using portals.Currently, I am standing at cell 1, and I want to go to cell t. However, I don't know whether it is possible to go there. Please determine whether I can go to cell t by only using the construted transportation system.", "input_spec": "The first line contains two space-separated integers n (3 ≤ n ≤ 3 × 104) and t (2 ≤ t ≤ n) — the number of cells, and the index of the cell which I want to go to. The second line contains n - 1 space-separated integers a1, a2, ..., an - 1 (1 ≤ ai ≤ n - i). It is guaranteed, that using the given transportation system, one cannot leave the Line World.", "output_spec": "If I can go to cell t using the transportation system, print \"YES\". Otherwise, print \"NO\".", "sample_inputs": ["8 4\n1 2 1 2 1 2 1", "8 5\n1 2 1 2 1 1 1"], "sample_outputs": ["YES", "NO"], "notes": "NoteIn the first sample, the visited cells are: 1, 2, 4; so we can successfully visit the cell 4.In the second sample, the possible cells to visit are: 1, 2, 4, 6, 7, 8; so we can't visit the cell 5, which we want to visit."}, "positive_code": [{"source_code": "args = gets.split(\" \").map{ |i| i.to_i}\nn = args[0]\nt = args[1]\npotrals = gets.split(\" \").map{ |i| i.to_i}.unshift(0)\nsucceed = false\ni = 1\nwhile i != n && !succeed\n i += potrals[i]\n if (t == i) \n succeed = true\n end\nend\nputs succeed == true ? \"YES\" : \"NO\""}, {"source_code": "#!/usr/bin/env ruby\nn,t=gets.split.map(&:to_i)\na=gets.split.map(&:to_i)\ncur=0\nwhile cur+1= t\n break\n end\nend\n\nif i == t\n print \"YES\"\nelse\n print \"NO\"\nend"}, {"source_code": "cells = gets.chomp\ncells = cells.split\nnumber = cells[0].to_i\ntarget = cells[1].to_i\nportals = gets.chomp\nportals = portals.split\n\ni = 0\nwhile i <= portals.size\n\tif i < target - 1\n\t\ti += portals[i].to_i\n\telse\n\t\tif i == target - 1\n\t\t\tprint \"YES\"\n\t\telse\n\t\t\tprint \"NO\"\n\t\tend\n\t\tbreak\n\tend\nend"}, {"source_code": "def solution(n, t, a)\n i = 1\n while (i <= t) do\n i += a[i-1]\n return puts \"YES\" if i == t\n end\n puts \"NO\"\nend\n\nn, t = gets.split(\" \").map { |i| i.to_i }\na = gets.split(\" \").map { |i| i.to_i }\n\nsolution(n, t, a)\n"}, {"source_code": "# http://codeforces.com/problemset/problem/500/A\n\nn, t = gets.chomp.split(\" \").map(&:to_i)\n\nas = gets.chomp.split(\" \").map(&:to_i)\n\ni = 0\n\nwhile i < n - 1 && i != t - 1\n i += as[i]\nend\n\nif i == t - 1\n puts 'YES'\nelse\n puts 'NO'\nend\n"}, {"source_code": "a = gets.split(\" \").map{|i| i.to_i}\ntab = gets.split(\" \").map{|i| i.to_i}\nn = a[0]\nt = a[1]\np = 0\nfor i in 0...n-1\n\tif i == p\n\t\tp += tab[i]\n\t\tif p >= t-1\n\t\t\tbreak\n\t\tend\n\tend\nend\nif p == t-1\n\tputs \"YES\"\nelse\n\tputs \"NO\"\nend\n"}, {"source_code": "n, t = gets.chomp.split.map(&:to_i)\n\na = gets.chomp.split.map(&:to_i)\n\ncan_solve = false\ni = 1\nloop do\n i += a[i - 1]\n\n if i == t\n can_solve = true\n break\n end\n\n break if i >= n\nend\n\nif can_solve\n puts 'YES'\nelse\n puts 'NO'\nend\n"}, {"source_code": "n, t = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\ni = 0\nans = \"NO\"\nwhile (i < n - 1)\n i += a[i]\n ans = \"YES\" if i == t - 1\n break if i >= t\nend\nputs ans\n"}, {"source_code": "n, t = gets.split.map(&:to_i)\na = [nil] + gets.split.map(&:to_i)\nans = false\ni = 1\nwhile i < t\n i += a[i]\n ans = true if i == t\nend\nputs ans ? \"YES\" : \"NO\"\n"}, {"source_code": "def g;gets.split.map(&:to_i)end\nn,t=g\na=g\nnow=0\nwhile now= t\n break\n end\nend\n\nif i == t\n print \"YES\"\nelse\n print \"NO\"\nend"}, {"source_code": "num = gets.chomp.split\nn = Integer(num[0])\nt = Integer(num[1])\n\nnums = gets.chomp.split\na = Array.new\nfor i in 1..n-1\n a[i] = Integer(nums[i - 1])\nend\ni = 1\nfor i in 1...n - 1\n i = i + a[i]\n if i >= t\n break\n end\nend\n\nif i == t\n print \"YES\"\nelse\n print \"NO\"\nend"}, {"source_code": "cells = gets.chomp\ncells = cells.split\nnumber = cells[0].to_i\ntarget = cells[1].to_i\nportals = gets.chomp\nportals = portals.split\n\ni = 0\nwhile i < portals.size\n\tif i < target - 1\n\t\ti += portals[i].to_i\n\telse\n\t\tif i == target - 1\n\t\t\tprint \"YES\"\n\t\telse\n\t\t\tprint \"NO\"\n\t\tend\n\t\tbreak\n\tend\nend"}, {"source_code": "cells = gets.chomp\ncells = cells.split\nnumber = cells[0].to_i\ntarget = cells[1].to_i\nportals = gets.chomp\nportals = portals.split\n\ni = 0\nwhile i < portals.size\n\tif i < target - 1\n\t\ti += portals[i].to_i\n\t\tputs i\n\telse\n\t\tif i == target - 1\n\t\t\tprint \"YES\"\n\t\telse\n\t\t\tprint \"NO\"\n\t\tend\n\t\tbreak\n\tend\nend"}, {"source_code": "cells = gets.chomp\ncells = cells.split\nnumber = cells[0].to_i\ntarget = cells[1].to_i\nportals = gets.chomp\nportals = portals.split\n\ni = 0\nwhile i < portals.size-1\n\tif i < target - 1\n\t\ti += portals[i].to_i\n\telse\n\t\tif i == target - 1\n\t\t\tprint \"YES\"\n\t\telse\n\t\t\tprint \"NO\"\n\t\tend\n\t\tbreak\n\tend\nend"}, {"source_code": "n, t = gets.split(' ').map(&:to_i)\na = gets.split(' ').map(&:to_i)\n\npos = 0\nresult = false\n\nwhile pos < n-1\n result ||= pos == t-1\n\n pos += a[pos]\nend\n\nputs result ? 'YES' : 'NO'\n"}], "src_uid": "9ee3d548f93390db0fc2f72500d9eeb0"} {"nl": {"description": "The king of Berland organizes a ball! $$$n$$$ pair are invited to the ball, they are numbered from $$$1$$$ to $$$n$$$. Each pair consists of one man and one woman. Each dancer (either man or woman) has a monochrome costume. The color of each costume is represented by an integer from $$$1$$$ to $$$k$$$, inclusive.Let $$$b_i$$$ be the color of the man's costume and $$$g_i$$$ be the color of the woman's costume in the $$$i$$$-th pair. You have to choose a color for each dancer's costume (i.e. values $$$b_1, b_2, \\dots, b_n$$$ and $$$g_1, g_2, \\dots g_n$$$) in such a way that: for every $$$i$$$: $$$b_i$$$ and $$$g_i$$$ are integers between $$$1$$$ and $$$k$$$, inclusive; there are no two completely identical pairs, i.e. no two indices $$$i, j$$$ ($$$i \\ne j$$$) such that $$$b_i = b_j$$$ and $$$g_i = g_j$$$ at the same time; there is no pair such that the color of the man's costume is the same as the color of the woman's costume in this pair, i.e. $$$b_i \\ne g_i$$$ for every $$$i$$$; for each two consecutive (adjacent) pairs both man's costume colors and woman's costume colors differ, i.e. for every $$$i$$$ from $$$1$$$ to $$$n-1$$$ the conditions $$$b_i \\ne b_{i + 1}$$$ and $$$g_i \\ne g_{i + 1}$$$ hold. Let's take a look at the examples of bad and good color choosing (for $$$n=4$$$ and $$$k=3$$$, man is the first in a pair and woman is the second):Bad color choosing: $$$(1, 2)$$$, $$$(2, 3)$$$, $$$(3, 2)$$$, $$$(1, 2)$$$ — contradiction with the second rule (there are equal pairs); $$$(2, 3)$$$, $$$(1, 1)$$$, $$$(3, 2)$$$, $$$(1, 3)$$$ — contradiction with the third rule (there is a pair with costumes of the same color); $$$(1, 2)$$$, $$$(2, 3)$$$, $$$(1, 3)$$$, $$$(2, 1)$$$ — contradiction with the fourth rule (there are two consecutive pairs such that colors of costumes of men/women are the same). Good color choosing: $$$(1, 2)$$$, $$$(2, 1)$$$, $$$(1, 3)$$$, $$$(3, 1)$$$; $$$(1, 2)$$$, $$$(3, 1)$$$, $$$(2, 3)$$$, $$$(3, 2)$$$; $$$(3, 1)$$$, $$$(1, 2)$$$, $$$(2, 3)$$$, $$$(3, 2)$$$. You have to find any suitable color choosing or say that no suitable choosing exists.", "input_spec": "The only line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$2 \\le n, k \\le 2 \\cdot 10^5$$$) — the number of pairs and the number of colors.", "output_spec": "If it is impossible to find any suitable colors choosing, print \"NO\". Otherwise print \"YES\" and then the colors of the costumes of pairs in the next $$$n$$$ lines. The $$$i$$$-th line should contain two integers $$$b_i$$$ and $$$g_i$$$ — colors of costumes of man and woman in the $$$i$$$-th pair, respectively. You can print each letter in any case (upper or lower). For example, \"YeS\", \"no\" and \"yES\" are all acceptable.", "sample_inputs": ["4 3", "10 4", "13 4"], "sample_outputs": ["YES\n3 1\n1 3\n3 2\n2 3", "YES\n2 1\n1 3\n4 2\n3 4\n4 3\n3 2\n2 4\n4 1\n1 4\n3 1", "NO"], "notes": null}, "positive_code": [{"source_code": "n, k = gets.split.map(&:to_i)\n\nif n <= k * (k - 1)\n\tputs 'YES'\n\ta, b = -1, -1\n\tans = []\n\tremain = n\n\t(1..k).each do |i|\n\t\t(i + 1..k).each do |j|\n\t\t\tif a != i && b != j\n\t\t\t\tans << [i, j] << [j, i]\n\t\t\t\ta, b = j, i\n\t\t\telse\n\t\t\t\tans << [j, i] << [i, j]\n\t\t\t\ta, b = i, j\n\t\t\tend\n\t\t\tremain -= 2\n\t\t\tbreak if remain <= 0\n\t\tend\n\t\tbreak if remain <= 0\n\tend\n\tans[...n].each { puts _1.join(' ') }\nelse\n\tputs 'NO'\nend\n"}, {"source_code": "n,k = gets.split.map(&:to_i)\nans = Array.new(n).map{[0,0]}\nif k*(k-1) < n\n puts \"NO\"\n exit\nend\nputs \"YES\"\nfor i in 0..n-1\n ans[i][0] = i%k+1\n ans[i][1] = (i/k+i%k+1)%k+1\n puts \"#{i%k+1} #{(i/k+i%k+1)%k+1}\"\nend"}], "negative_code": [], "src_uid": "c9237925ab3de588a3f047c226f7fe83"} {"nl": {"description": "Given an integer $$$n$$$, find any array $$$a$$$ of $$$n$$$ distinct nonnegative integers less than $$$2^{31}$$$ such that the bitwise XOR of the elements on odd indices equals the bitwise XOR of the elements on even indices.", "input_spec": "The first line of the input contains an integer $$$t$$$ ($$$1 \\leq t \\leq 629$$$) — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(3 \\leq n \\leq 2\\cdot10^5)$$$ — the length of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\\cdot 10^5$$$.", "output_spec": "For each test case, output one line containing $$$n$$$ distinct integers that satisfy the conditions. If there are multiple answers, you can output any of them.", "sample_inputs": ["7\n8\n3\n4\n5\n6\n7\n9"], "sample_outputs": ["4 2 1 5 0 6 7 3\n2 1 3\n2 1 3 0\n2 0 4 5 3\n4 1 2 12 3 8\n1 2 3 4 5 6 7\n8 2 3 7 4 0 5 6 9"], "notes": "NoteIn the first test case the XOR on odd indices is $$$4 \\oplus 1 \\oplus 0 \\oplus 7 = 2$$$ and the XOR on even indices is $$$2 \\oplus 5 \\oplus 6 \\oplus 3= 2$$$."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n xs = [*1..n - 3, 1 << 28, 1 << 29]\r\n puts xs * \" \" + \" #{xs.reduce(:^)}\"\r\nend\r\n"}, {"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n xs = [*1..n - 3, 1 << 28, 1 << 29]\r\n xs << xs.reduce(:^)\r\n puts xs * \" \"\r\nend\r\n"}, {"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n x = 1 << 28\r\n y = 1 << 29\r\n z = ((1..n - 3).reduce(:^) || 0) ^ x ^ y\r\n puts [*1..n - 3, x, y, z].join(\" \")\r\nend\r\n"}, {"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n i = -1\r\n large, small = if n % 2 == 0\r\n 2.times.map { (n / 2 - 1).times.map { i += 1 } }\r\n else\r\n [(n / 2).times.map { i += 1 },\r\n (n / 2 - 1).times.map { i += 1 }]\r\n end\r\n xlarge = large.reduce(:^) || 0\r\n xsmall = small.reduce(:^) || 0\r\n if xsmall == xlarge\r\n large[-1] = i += 1\r\n end\r\n xlarge = large.reduce(:^) || 0\r\n l, s = loop do\r\n l = i += 1\r\n s = xlarge ^ l ^ xsmall\r\n break [l, s] if s > i\r\n end\r\n large << l\r\n small << s\r\n puts 0.upto(n - 1).map { |i| i % 2 == 0 ? large[i / 2] : small[i / 2] }.join(\" \")\r\nend\r\n"}], "negative_code": [{"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n array = (0..n - 2).to_a\r\n a = array.filter.with_index { |x, i| i.even? }.reduce(:^)\r\n b = array.filter.with_index { |x, i| i.odd? }.reduce(:^)\r\n c = (n - 2).upto(2 ** 32).find do |c|\r\n a ^ c ^ b > n\r\n end\r\n d = a ^ c ^ b\r\n array << c << d\r\n puts array.join(\" \")\r\nend\r\n"}], "src_uid": "52bd5dc6b92b2af5aebd387553ef4076"} {"nl": {"description": "You are given an array $$$a$$$ consisting of $$$n$$$ integers. Initially all elements of $$$a$$$ are either $$$0$$$ or $$$1$$$. You need to process $$$q$$$ queries of two kinds: 1 x : Assign to $$$a_x$$$ the value $$$1 - a_x$$$. 2 k : Print the $$$k$$$-th largest value of the array. As a reminder, $$$k$$$-th largest value of the array $$$b$$$ is defined as following: Sort the array in the non-increasing order, return $$$k$$$-th element from it. For example, the second largest element in array $$$[0, 1, 0, 1]$$$ is $$$1$$$, as after sorting in non-increasing order it becomes $$$[1, 1, 0, 0]$$$, and the second element in this array is equal to $$$1$$$.", "input_spec": "The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 \\le n, q \\le 10^5$$$) — the length of the given array and the number of queries. The second line contains $$$n$$$ integers $$$a_1, a_2, a_3, \\dots, a_n$$$ ($$$0 \\le a_i \\le 1$$$) — elements of the initial array. Each of the following $$$q$$$ lines contains two integers. The first integer is $$$t$$$ ($$$1 \\le t \\le 2$$$) — the type of query. If $$$t = 1$$$ the second integer is $$$x$$$ ($$$1 \\le x \\le n$$$) — the position of the modified number. You have to assign to $$$a_x$$$ the value $$$1 - a_x$$$. If $$$t = 2$$$ the second integer is $$$k$$$ ($$$1 \\le k \\le n$$$) — you need to print the $$$k$$$-th largest value of the array. It's guaranteed that there will be at least one query of the second type (satisfying $$$t = 2$$$).", "output_spec": "For each query of the second type, print a single integer — the answer to the query.", "sample_inputs": ["5 5\n1 1 0 1 0\n2 3\n1 2\n2 3\n2 1\n2 5"], "sample_outputs": ["1\n0\n1\n0"], "notes": "NoteInitially $$$a = [1, 1, 0, 1, 0]$$$.The first operation is printing the third largest value, which is $$$1$$$.The second operation is assigning $$$a_2$$$ the value $$$0$$$, $$$a$$$ becomes $$$[1, 0, 0, 1, 0]$$$.The third operation is printing the third largest value, it is $$$0$$$.The fourth operation is printing the first largest value, it is $$$1$$$.The last operation is printing the fifth largest value, it is $$$0$$$."}, "positive_code": [{"source_code": "def read\r\n gets.split.map &:to_i\r\nend\r\n_, q = read\r\na = read\r\ncur = a.sum\r\nq.times do\r\n t, x = read\r\n if t == 1\r\n x -= 1\r\n cur += a[x] == 1 ? -1 : 1\r\n a[x] = 1 - a[x]\r\n else\r\n puts cur >= x ? 1 : 0\r\n end\r\nend"}, {"source_code": "# frozen_string_literal: true\r\n\r\nn, q = gets.chomp.split.map(&:to_i)\r\nlist = gets.chomp.split.map(&:to_i)\r\n\r\nnumber_of_ones = list.sum\r\nq.times do\r\n query = gets.chomp.split.map(&:to_i)\r\n\r\n if query[0] == 1\r\n x = query[1]\r\n list[x - 1] = 1 - list[x - 1]\r\n if list[x - 1].zero?\r\n number_of_ones -= 1\r\n else\r\n number_of_ones += 1\r\n end\r\n else\r\n k = query[1]\r\n puts number_of_ones > k - 1 ? 1 : 0\r\n end\r\nend\r\n"}, {"source_code": "inp = gets.chomp.split\nn, q = inp[0].to_i, inp[1].to_i\na = []\nk = 0\n\ni = 0\n\nstr = gets.chomp.split\ni = 0\nstr.each do |v|\n k += 1 if v.to_i == 1\n a[i] = v.to_i\n i+=1\nend\n\n\nq.times do\n inp = gets.chomp.split\n qr, qt = inp[0].to_i, inp[1].to_i \n if qr == 1\n if a[qt-1] == 0\n k+=1 \n else\n k-=1\n end\n\n a[qt-1] = 1 - a[qt-1]\n else \n if qt <= k \n puts(1) \n else\n puts(0) \n end\n end\nend"}], "negative_code": [{"source_code": "def read\r\n gets.split.map &:to_i\r\nend\r\n_, q = read\r\na = read\r\ncur = a.sum\r\nq.times do\r\n t, x = read\r\n if t == 1\r\n x -= 1\r\n cur += a[x] == 1 ? -1 : 1\r\n else\r\n puts cur >= x ? 1 : 0\r\n end\r\nend"}, {"source_code": "# frozen_string_literal: true\r\n\r\nn, q = gets.chomp.split.map(&:to_i)\r\nlist = gets.chomp.split.map(&:to_i)\r\n\r\nnumber_of_ones = list.count(1)\r\nq.times do\r\n query = gets.chomp.split.map(&:to_i)\r\n\r\n if query[0] == 1\r\n x = query[1]\r\n if x > number_of_ones\r\n number_of_ones += 1\r\n else\r\n number_of_ones -= 1\r\n end\r\n else\r\n k = query[1]\r\n puts k > number_of_ones ? '0' : '1'\r\n end\r\nend\r\n"}, {"source_code": "# frozen_string_literal: true\r\n\r\nn, q = gets.chomp.split.map(&:to_i)\r\nlist = gets.chomp.split.map(&:to_i)\r\n\r\nnumber_of_ones = list.count(1)\r\nq.times do\r\n query = gets.chomp\r\n\r\n if query[0] == '1'\r\n x = query[2].to_i\r\n if x > number_of_ones\r\n number_of_ones += 1\r\n else\r\n number_of_ones -= 1\r\n end\r\n else\r\n k = query[2].to_i\r\n puts k > number_of_ones ? '0' : '1'\r\n end\r\nend\r\n"}, {"source_code": "inp = gets.chomp.split\nn, q = inp[0].to_i, inp[1].to_i\na = []\nk = 0\n\ni = 0\n\nstr = gets.chomp.split\ni = 0\nstr.each do |v|\n k += 1 if v.to_i == 1\n a[i] = v.to_i\n i+=1\nend\n\n\nq.times do\n inp = gets.chomp.split\n qr, qt = inp[0].to_i, inp[1].to_i \n if qr == 1\n if a[qt-1] == 0\n k+=1 \n else\n k-=1\n end\n\n a[qt-1] = 1 - a[qt-1]\n else \n if k <= qt \n puts(1) \n else\n puts(0) \n end\n end\nend"}], "src_uid": "ccf7aba6ca9bbf39a5ec8a20ec018825"} {"nl": {"description": "One day Vitaly was going home late at night and wondering: how many people aren't sleeping at that moment? To estimate, Vitaly decided to look which windows are lit in the house he was passing by at that moment.Vitaly sees a building of n floors and 2·m windows on each floor. On each floor there are m flats numbered from 1 to m, and two consecutive windows correspond to each flat. If we number the windows from 1 to 2·m from left to right, then the j-th flat of the i-th floor has windows 2·j - 1 and 2·j in the corresponding row of windows (as usual, floors are enumerated from the bottom). Vitaly thinks that people in the flat aren't sleeping at that moment if at least one of the windows corresponding to this flat has lights on.Given the information about the windows of the given house, your task is to calculate the number of flats where, according to Vitaly, people aren't sleeping.", "input_spec": "The first line of the input contains two integers n and m (1 ≤ n, m ≤ 100) — the number of floors in the house and the number of flats on each floor respectively. Next n lines describe the floors from top to bottom and contain 2·m characters each. If the i-th window of the given floor has lights on, then the i-th character of this line is '1', otherwise it is '0'.", "output_spec": "Print a single integer — the number of flats that have lights on in at least one window, that is, the flats where, according to Vitaly, people aren't sleeping.", "sample_inputs": ["2 2\n0 0 0 1\n1 0 1 1", "1 3\n1 1 0 1 0 0"], "sample_outputs": ["3", "2"], "notes": "NoteIn the first test case the house has two floors, two flats on each floor. That is, in total there are 4 flats. The light isn't on only on the second floor in the left flat. That is, in both rooms of the flat the light is off.In the second test case the house has one floor and the first floor has three flats. The light is on in the leftmost flat (in both windows) and in the middle flat (in one window). In the right flat the light is off."}, "positive_code": [{"source_code": "p$<.read.split.map(&:to_i)[2..-1].each_slice(2).count{|x,y|x>0||y>0}"}, {"source_code": "n,m = gets.chomp.split(\" \").map(&:to_i)\ncounter = 0\nn.times do |f|\n\tfloor = gets.chomp.split(\" \")\n\t0.step((m*2-1), 2) do |ind|\n\t\tcounter+=1 if floor[ind] == \"1\" || floor[ind+1] == \"1\"\n\tend\nend\nputs counter"}, {"source_code": "n, m = gets.chomp.split.map(&:to_i)\narr = [].tap do |out|\n n.times do\n gets.chomp.split.map(&:to_i).each_slice(2).each { |a| out << a }\n end\nend\nputs arr.select { |a| a.include? 1 }.length\n"}, {"source_code": "#! ruby\n# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n\nn,m = gets.chomp.split.map(&:to_i)\n\nc = 0\n\nn.times do\n f = gets.chomp.split\n m.times do |j|\n c += 1 if f[2 * j] == \"1\" || f[2 * j + 1] == \"1\"\n end\nend\n\nputs c"}, {"source_code": "n, m = gets.split.map(&:to_i)\nans = 0\nn.times do\n w = gets.split.map(&:to_i)\n (0..m-1).each do |i|\n ans += 1 if w[(i*2)..(i*2+1)].include?(1)\n end\nend\nputs ans"}, {"source_code": "data=gets.chomp.split\n\nn=data[0].to_i\nm=data[1].to_i\n\nhouse=[]\n\na=0\nwhile a0 then\n\t\t\tans+=1\n\t\tend\n\t}\n\t\n}\nputs ans\n\n\n"}, {"source_code": "n, m = gets.chomp.split(\"\\s\").map(&:to_i)\nfloors = n.times.map { gets.chomp.split(\"\\s\").map(&:to_i) }\n\nflats = []\n\nfloors.each do |floor|\n floor.each_slice(2).each do |flat|\n flats << flat\n end\nend\n\nputs flats.reduce(0) { |acc, flat| flat.include?(1) ? acc + 1 : acc }\n"}, {"source_code": "s = 0\nn, m = gets.chomp.split.map(&:to_i)\nn.times do\n as = gets.chomp.split.map(&:to_i)\n for i in 0...m\n s += 1 if 0 < as[2 * i] + as [2 * i + 1]\n end\nend\nputs s\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\na = n.times.map{gets.split.map(&:to_i)}\nans = Array.new(n){Array.new(m, 0)}\nn.times do |i|\n (2 * m).times do |j|\n ans[i][j / 2] = 1 if a[i][j] == 1\n end\nend\np ans.flatten.inject(:+)\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\na = n.times.map{gets.split.map(&:to_i)}\nputs a.flatten.each_slice(2).count{|x| x.include?(1)}\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\na = n.times.map{gets.split.map(&:to_i)}\nans = 0\nn.times do |i|\n (2 * m).times do |j|\n if (a[i][j] == 1 && j.even?) || (a[i][j] == 1 && j.odd? && a[i][j - 1] != 1)\n ans += 1 \n end\n end\nend\np ans\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\na = n.times.map{gets.split.map(&:to_i)}\nputs a.flatten.each_slice(2).map{|a, b| a + b > 0 ? 1 : 0 }.inject(:+)\n"}, {"source_code": "n, m = gets.chomp.split(\" \").map(&:to_i)\nans = 0\nn.times do |i|\n rooms = gets.chomp.split(\" \").map(&:to_i)\n rooms.each_slice(2) do |a, b|\n if (a == 1) or (b == 1) then\n ans += 1\n end\n end\nend\nputs ans"}, {"source_code": "n, m = gets.split.map(&:to_i)\nnum = 0\n(1..n).each do |t|\n arr = gets.split.map(&:to_i)\n (0..arr.length).each do |index|\n if index%2 == 1\n num = num + ((arr[index-1]+arr[index]) > 0 ? 1 : 0)\n end\n end\nend\np num"}, {"source_code": "n, m = gets.split.map(&:to_i)\nans = 0\nn.times do\n a = gets.split.map(&:to_i)\n (0..m-1).each{|i| ans += 1 if a[2*i] == 1 || a[2*i + 1] == 1}\nend\np ans\n"}], "negative_code": [{"source_code": "n, m = gets.chomp.split.map(&:to_i)\narr = [].tap do |out|\n n.times do\n gets.chomp.split.map(&:to_i).each_slice(m).each { |a| out << a }\n end\nend\nputs arr.select { |a| a.include? 1 }.length\n"}, {"source_code": "#! ruby\n# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n\nn,m = gets.chomp.split.map(&:to_i)\n\nc = 0\n\nn.times do\n f = gets.chomp.split\n 0.step(m.div 2) do |j|\n if f[2 * j] == \"1\" || f[2 * j - 1] == \"1\"\n c += 1\n end\n end\nend\n\nputs c"}, {"source_code": "#! ruby\n# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n\nn,m = gets.chomp.split.map(&:to_i)\n\nc = 0\n\nn.times do\n f = gets.chomp.split\n f.size.pred.times do |i|\n c += 1 if f[i] == f[i + 1]\n end\nend\n\nputs c"}, {"source_code": "n,m=gets.split.map{|e| e.to_i}\nans=0\nn.times{\n\tx=gets.split.map{|e| e.to_i}\n\t0.step(m,2){|i|\n\t\tif x[i]+x[i+1]>0 then\n\t\t\tans+=1\n\t\tend\n\t}\n\t\n}\nputs ans\n\n\n"}], "src_uid": "5b9aed235094de7de36247a3b2a34e0f"} {"nl": {"description": "After the piece of a devilish mirror hit the Kay's eye, he is no longer interested in the beauty of the roses. Now he likes to watch snowflakes.Once upon a time, he found a huge snowflake that has a form of the tree (connected acyclic graph) consisting of n nodes. The root of tree has index 1. Kay is very interested in the structure of this tree.After doing some research he formed q queries he is interested in. The i-th query asks to find a centroid of the subtree of the node vi. Your goal is to answer all queries.Subtree of a node is a part of tree consisting of this node and all it's descendants (direct or not). In other words, subtree of node v is formed by nodes u, such that node v is present on the path from u to root.Centroid of a tree (or a subtree) is a node, such that if we erase it from the tree, the maximum size of the connected component will be at least two times smaller than the size of the initial tree (or a subtree).", "input_spec": "The first line of the input contains two integers n and q (2 ≤ n ≤ 300 000, 1 ≤ q ≤ 300 000) — the size of the initial tree and the number of queries respectively. The second line contains n - 1 integer p2, p3, ..., pn (1 ≤ pi ≤ n) — the indices of the parents of the nodes from 2 to n. Node 1 is a root of the tree. It's guaranteed that pi define a correct tree. Each of the following q lines contain a single integer vi (1 ≤ vi ≤ n) — the index of the node, that define the subtree, for which we want to find a centroid.", "output_spec": "For each query print the index of a centroid of the corresponding subtree. If there are many suitable nodes, print any of them. It's guaranteed, that each subtree has at least one centroid.", "sample_inputs": ["7 4\n1 1 3 3 5 3\n1\n2\n3\n5"], "sample_outputs": ["3\n2\n3\n6"], "notes": "Note The first query asks for a centroid of the whole tree — this is node 3. If we delete node 3 the tree will split in four components, two of size 1 and two of size 2.The subtree of the second node consists of this node only, so the answer is 2.Node 3 is centroid of its own subtree.The centroids of the subtree of the node 5 are nodes 5 and 6 — both answers are considered correct."}, "positive_code": [{"source_code": "Node = Struct.new(:i, :parent, :child, :size, :cent)\n\nn, q = gets.split.map(&:to_i)\n\nvs = n.times.map{|i| Node.new(i, nil, [])}\ngets.split.map{|v| v.to_i - 1}.each_with_index do |p, i|\n\ti += 1\n\tvs[p].child << vs[i]\n\tvs[i].parent = vs[p]\nend\n\ns = [[vs[0], 0]]\nwhile s.any?\n\tv, i = s.pop\n\tif i < v.child.size\n\t\ts.push [v, i + 1]\n\t\ts.push [v.child[i], 0]\n\telse\n\t\tv.size = 1 + v.child.map(&:size).inject(0, :+)\n\t\tw = v.child.find{|w| w.size > v.size / 2}\n\t\tif not w\n\t\t\tv.cent = v\n\t\telse\n\t\t\tw = w.cent\n\t\t\twhile v.size - w.size > v.size / 2\n\t\t\t\tw = w.parent\n\t\t\tend\n\t\t\tv.cent = w\n\t\tend\n\tend\nend\n\nq.times do\n\tv = vs[gets.to_i - 1]\n\tputs v.cent.i + 1\nend"}], "negative_code": [{"source_code": "Node = Struct.new(:i, :child, :size)\n\nn, q = gets.split.map(&:to_i)\n\nt = Time.now\n\nvs = n.times.map{|i| Node.new(i, [])}\ngets.split.map{|v| v.to_i - 1}.each_with_index do |p, i|\n i += 1\n vs[p].child << vs[i]\nend\n\nputs Time.now - t if n == 257777\nt = Time.now\n\ns = [[vs[0], 0]]\nc = 0\nwhile s.any?\n c += 1\n if c % 10000 == 0; puts c end\n v, i = s.pop\n if i < v.child.size\n s.push [v, i + 1]\n s.push [v.child[i], 0]\n else\n v.size = 1 + v.child.map(&:size).inject(0, :+)\n end\nend\n\nif n = 257777\n puts Time.now - t\n exit\nend\n\nq.times do\n v = vs[gets.to_i - 1]\n n = v.size\n loop do\n w = v.child.find{|v| v.size > n / 2}\n break if not w\n v = w\n end\n puts v.i + 1\nend"}, {"source_code": "Node = Struct.new(:i, :child, :size)\n\nn, q = gets.split.map(&:to_i)\n\nt = Time.now\n\nvs = n.times.map{|i| Node.new(i, [])}\ngets.split.map{|v| v.to_i - 1}.each_with_index do |p, i|\n i += 1\n vs[p].child << vs[i]\nend\n\nputs Time.now - t\nt = Time.now\n\ns = [[vs[0], 0]]\nc = 0\nwhile s.any?\n c += 1\n if c % 10000 == 0; puts c end\n v, i = s.pop\n if i < v.child.size\n s.push [v, i + 1]\n s.push [v.child[i], 0]\n else\n v.size = 1 + v.child.map(&:size).inject(0, :+)\n end\nend\n\nputs Time.now - t\nexit\n\nq.times do\n v = vs[gets.to_i - 1]\n n = v.size\n loop do\n w = v.child.find{|v| v.size > n / 2}\n break if not w\n v = w\n end\n puts v.i + 1\nend"}, {"source_code": "Node = Struct.new(:i, :child, :size)\n\nn, q = gets.split.map(&:to_i)\n\nt = Time.now\n\nvs = n.times.map{|i| Node.new(i, [])}\ngets.split.map{|v| v.to_i - 1}.each_with_index do |p, i|\n i += 1\n vs[p].child << vs[i]\nend\n\nif n == 257777; puts Time.now - t end\nt = Time.now\n\ns = [[vs[0], 0]]\n# c = 0\nwhile s.any?\n # c += 1\n # if c % 10000 == 0; puts c end\n v, i = s.pop\n if i < v.child.size\n s.push [v, i + 1]\n s.push [v.child[i], 0]\n else\n v.size = 1 + v.child.map(&:size).inject(0, :+)\n end\nend\n\nif n == 257777\n puts Time.now - t\n exit\nend\n\nq.times do\n v = vs[gets.to_i - 1]\n n = v.size\n loop do\n w = v.child.find{|v| v.size > n / 2}\n break if not w\n v = w\n end\n puts v.i + 1\nend"}, {"source_code": "Node = Struct.new(:i, :child, :size)\n\nn, q = gets.split.map(&:to_i)\n\nvs = n.times.map{|i| Node.new(i, [], 1)}\ngets.split.map{|v| v.to_i - 1}.each_with_index do |p, i|\n i += 1\n vs[p].child << vs[i]\n vs[p].size += vs[i].size\nend\n\nq.times do\n v = vs[gets.to_i - 1]\n loop do\n w = v.child.find{|v| v.size > n / 2}\n break if not w\n v = w\n end\n puts v.i + 1\nend"}, {"source_code": "Node = Struct.new(:i, :child, :size)\n\nn, q = gets.split.map(&:to_i)\n\nvs = n.times.map{|i| Node.new(i, [])}\ngets.split.map{|v| v.to_i - 1}.each_with_index do |p, i|\n i += 1\n vs[p].child << vs[i]\nend\n\nif n == 257777\n puts 1488\n exit\nend\n\ndef calc_size(v)\n v.size = 1 + v.child.map(&method(:calc_size)).inject(0, :+)\nend\ncalc_size vs[0]\n\n\n\nq.times do\n v = vs[gets.to_i - 1]\n loop do\n w = v.child.find{|v| v.size > n / 2}\n break if not w\n v = w\n end\n puts v.i + 1\nend"}, {"source_code": "Node = Struct.new(:i, :child, :size)\n\nn, q = gets.split.map(&:to_i)\n\nt = Time.now\n\nvs = n.times.map{|i| Node.new(i, [])}\ngets.split.map{|v| v.to_i - 1}.each_with_index do |p, i|\n i += 1\n vs[p].child << vs[i]\nend\n\nif n == 257777; puts Time.now - t end\nt = Time.now\n\ns = [[vs[0], 0]]\nwhile s.any?\n v, i = s.pop\n if i < v.child.size\n s.push [v, i + 1]\n s.push [v.child[i], 0]\n else\n v.size = 1 + v.child.map(&:size).inject(0, :+)\n end\nend\n\nif n = 257777\n puts Time.now - t\n exit\nend\n\nq.times do\n v = vs[gets.to_i - 1]\n n = v.size\n loop do\n w = v.child.find{|v| v.size > n / 2}\n break if not w\n v = w\n end\n puts v.i + 1\nend"}, {"source_code": "Node = Struct.new(:i, :child, :size)\n\nn, q = gets.split.map(&:to_i)\n\nif n == 257777\n puts 1488\n exit\nend\n\nvs = [Node.new(0, [])]\ngets.split.map{|v| v.to_i-1}.each_with_index do |p, i|\n i += 1\n vs << Node.new(i, [])\n vs[p].child << vs[i]\nend\n\n# def calc_size(v)\n# v.size = 1 + v.child.map(&method(:calc_size)).inject(0, :+)\n# end\n# calc_size vs[0]\nvs.reverse_each do |v|\n v.size = 1 + v.child.map(&:size).inject(0, :+)\nend\n\nif n == 257777\n puts 1488\n exit\nend\n\nq.times do\n v = vs[gets.to_i - 1]\n loop do\n w = v.child.find{|v| v.size > n / 2}\n break if not w\n v = w\n end\n puts v.i + 1\nend"}], "src_uid": "7f15864d46f09ea6f117929565ccb867"} {"nl": {"description": "Once upon a time there was only one router in the well-known company Bmail. Years went by and over time new routers were purchased. Every time they bought a new router, they connected it to one of the routers bought before it. You are given the values $$$p_i$$$ — the index of the router to which the $$$i$$$-th router was connected after being purchased ($$$p_i < i$$$).There are $$$n$$$ routers in Boogle in total now. Print the sequence of routers on the path from the first to the $$$n$$$-th router.", "input_spec": "The first line contains integer number $$$n$$$ ($$$2 \\le n \\le 200000$$$) — the number of the routers. The following line contains $$$n-1$$$ integers $$$p_2, p_3, \\dots, p_n$$$ ($$$1 \\le p_i < i$$$), where $$$p_i$$$ is equal to index of the router to which the $$$i$$$-th was connected after purchase.", "output_spec": "Print the path from the $$$1$$$-st to the $$$n$$$-th router. It starts with $$$1$$$ and ends with $$$n$$$. All the elements in the path should be distinct.", "sample_inputs": ["8\n1 1 2 2 3 2 5", "6\n1 2 3 4 5", "7\n1 1 2 3 4 3"], "sample_outputs": ["1 2 5 8", "1 2 3 4 5 6", "1 3 7"], "notes": null}, "positive_code": [{"source_code": "a=$<.read.split.map &:to_i\nx=a.size-1\nr=[x+1]\nwhile x>0;x=a[x]-1;r.unshift x+1 end\nputs r*' '"}], "negative_code": [], "src_uid": "e25efe1020ae64d55b3257ba457f809d"} {"nl": {"description": "The official capital and the cultural capital of Berland are connected by a single road running through n regions. Each region has a unique climate, so the i-th (1 ≤ i ≤ n) region has a stable temperature of ti degrees in summer.This summer a group of m schoolchildren wants to get from the official capital to the cultural capital to visit museums and sights. The trip organizers transport the children between the cities in buses, but sometimes it is very hot. Specifically, if the bus is driving through the i-th region and has k schoolchildren, then the temperature inside the bus is ti + k degrees.Of course, nobody likes it when the bus is hot. So, when the bus drives through the i-th region, if it has more than Ti degrees inside, each of the schoolchild in the bus demands compensation for the uncomfortable conditions. The compensation is as large as xi rubles and it is charged in each region where the temperature in the bus exceeds the limit.To save money, the organizers of the trip may arbitrarily add or remove extra buses in the beginning of the trip, and between regions (of course, they need at least one bus to pass any region). The organizers can also arbitrarily sort the children into buses, however, each of buses in the i-th region will cost the organizers costi rubles. Please note that sorting children into buses takes no money.Your task is to find the minimum number of rubles, which the organizers will have to spend to transport all schoolchildren.", "input_spec": "The first input line contains two integers n and m (1 ≤ n ≤ 105; 1 ≤ m ≤ 106) — the number of regions on the way and the number of schoolchildren in the group, correspondingly. Next n lines contain four integers each: the i-th line contains ti, Ti, xi and costi (1 ≤ ti, Ti, xi, costi ≤ 106). The numbers in the lines are separated by single spaces.", "output_spec": "Print the only integer — the minimum number of roubles the organizers will have to spend to transport all schoolchildren. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.", "sample_inputs": ["2 10\n30 35 1 100\n20 35 10 10", "3 100\n10 30 1000 1\n5 10 1000 3\n10 40 1000 100000"], "sample_outputs": ["120", "200065"], "notes": "NoteIn the first sample the organizers will use only one bus to travel through the first region. However, the temperature in the bus will equal 30 + 10 = 40 degrees and each of 10 schoolchildren will ask for compensation. Only one bus will transport the group through the second region too, but the temperature inside won't exceed the limit. Overall, the organizers will spend 100 + 10 + 10 = 120 rubles."}, "positive_code": [{"source_code": "n, m = gets.split.map(&:to_i)\ndata = []\ntotal = 0\nn.times { data << gets.split.map(&:to_i) }\n\ndata.each do |line|\n t, tmax, comp, bus = *line\n\n if t+m <= tmax\n total += bus\n next\n end\n if tmax-t < 1\n total += m*comp+bus\n else\n total += [((m+tmax-t-1)/(tmax-t))*bus, m*comp+bus].min\n end\n\nend\n\nputs total"}, {"source_code": "n, m = gets.split.map(&:to_i)\ndata = []\ntotal = 0\nn.times { data << gets.split.map(&:to_i) }\n\ndata.each do |line|\n t, tmax, comp, bus = *line\n\n if t+m <= tmax\n total += bus\n next\n end\n if tmax-t < 1\n total += m*comp+bus\n else\n total += [(m/(tmax-t) + (m%(tmax-t)==0 ? 0 : 1))*bus, m*comp+bus].min\n end\n\nend\n\nputs total"}, {"source_code": "n, m = gets.split.map(&:to_i)\ndata = []\ntotal = 0\nn.times { data << gets.split.map(&:to_i) }\n\ndata.each do |line|\n t, tmax, comp, bus = *line\n\n if t+m <= tmax\n total += bus\n next\n end\n if tmax-t < 1\n total += m*comp+bus\n else\n total += [(m/(tmax-t) + (m%(tmax-t)==0 ? 0 : 1))*bus, m*comp+bus].min\n end\n \nend\n\nputs total"}], "negative_code": [{"source_code": "n, m = gets.split.map(&:to_i)\ndata = []\ntotal = 0\nn.times { data << gets.split.map(&:to_i) }\n\ndata.each do |line|\n t, tmax, comp, bus = *line\n\n if t+m <= tmax\n total += bus\n next\n end\n if tmax-t < 1\n total += m*comp+bus\n else\n total += [((m+tmax-t+1)/(tmax-t))*bus, m*comp+bus].min\n end\n\nend\n\nputs total"}, {"source_code": "n, m = gets.split.map(&:to_i)\ndata = []\ntotal = 0\nn.times { data << gets.split.map(&:to_i) }\n\ndata.each do |line|\n\n if line[0] + m < line[1]\n total += line[3]\n next\n end\n\n if line[2] * m > line[3]\n del = line[1]-line[0]\n if del >0\n total += line[3] * (m/del)\n total += line[3] if (m%del>0)\n else\n total += line[3] * m\n end\n else\n total += line[2] * m + line[3]\n end\n\n\nend\n\nputs total"}, {"source_code": "n, m = gets.split.map(&:to_i)\ndata = []\ntotal = 0\nn.times { data << gets.split.map(&:to_i) }\n\ndata.each do |line|\n\n if line[0] + m <= line[1]\n total += line[3]\n next\n end\n\n if line[2] * (line[1]-line[0]) >= line[3]\n total += line[3] * (m/(line[1]-line[0]))\n total += line[3] if (m%(line[1]-line[0])>0)\n else\n total += line[2] * m + line[3]\n end\n\n\n\nend\n\nputs total"}, {"source_code": "\n n, m = gets.split.map(&:to_i)\ndata = []\ntotal = 0\nn.times { data << gets.split.map(&:to_i) }\n\ndata.each do |line|\n\n if line[0] + m < line[1]\n total += line[3]\n next\n end\n\n if line[2] * (line[1]-line[0]) >= line[3]\n total += line[3] * (m/(line[1]-line[0]))\n total += line[3] if (m%(line[1]-line[0])>0)\n else\n total += line[2] * m + line[3]\n end\n\n\n\nend\n\nputs total"}, {"source_code": "\n n, m = gets.split.map(&:to_i)\ndata = []\ntotal = 0\nn.times { data << gets.split.map(&:to_i) }\n\ndata.each do |line|\n\n if line[0] + m < line[1]\n total += line[3]\n next\n end\n\n if line[2] * (line[1]-line[0]) > line[3]\n total += line[3] * (m/(line[1]-line[0]))\n total += line[3] if (m%(line[1]-line[0])>0)\n else\n total += line[2] * m + line[3]\n end\n\n\n\nend\n\nputs total"}, {"source_code": "n, m = gets.split.map(&:to_i)\ndata = []\ntotal = 0\nn.times { data << gets.split.map(&:to_i) }\n\ndata.each do |line|\n t, tmax, comp, bus = *line\n\n if t+m <= tmax\n total += bus\n next\n end\n if tmax-t == 0\n total +=m*comp+bus\n else\n total += [(m/(tmax-t) + (m%(tmax-t)==0 ? 0 : 1))*bus, m*comp+bus].min\n end\nend\n\nputs total"}], "src_uid": "96a3f9d559a40050095de2f5f70b147f"} {"nl": {"description": "There are $$$n + 1$$$ cities, numbered from $$$0$$$ to $$$n$$$. $$$n$$$ roads connect these cities, the $$$i$$$-th road connects cities $$$i - 1$$$ and $$$i$$$ ($$$i \\in [1, n]$$$).Each road has a direction. The directions are given by a string of $$$n$$$ characters such that each character is either L or R. If the $$$i$$$-th character is L, it means that the $$$i$$$-th road initially goes from the city $$$i$$$ to the city $$$i - 1$$$; otherwise it goes from the city $$$i - 1$$$ to the city $$$i$$$.A traveler would like to visit as many cities of this country as possible. Initially, they will choose some city to start their journey from. Each day, the traveler must go from the city where they currently are to a neighboring city using one of the roads, and they can go along a road only if it is directed in the same direction they are going; i. e., if a road is directed from city $$$i$$$ to the city $$$i + 1$$$, it is possible to travel from $$$i$$$ to $$$i + 1$$$, but not from $$$i + 1$$$ to $$$i$$$. After the traveler moves to a neighboring city, all roads change their directions to the opposite ones. If the traveler cannot go from their current city to a neighboring city, their journey ends; it is also possible to end the journey whenever the traveler wants to.The goal of the traveler is to visit as many different cities as possible (they can visit a city multiple times, but only the first visit is counted). For each city $$$i$$$, calculate the maximum number of different cities the traveler can visit during exactly one journey if they start in the city $$$i$$$. ", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line contains one integer $$$n$$$ ($$$1 \\le n \\le 3 \\cdot 10^5$$$). The second line contains the string $$$s$$$ consisting of exactly $$$n$$$ characters, each character is either L or R. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 \\cdot 10^5$$$.", "output_spec": "For each test case, print $$$n + 1$$$ integers. The $$$i$$$-th integer should be equal to the maximum number of different cities the traveler can visit during one journey if this journey starts in the $$$i$$$-th city.", "sample_inputs": ["2\n6\nLRRRLL\n3\nLRL"], "sample_outputs": ["1 3 2 3 1 3 2\n1 4 1 4"], "notes": null}, "positive_code": [{"source_code": "gets.to_i.times do\r\n n=gets.to_i\r\n s=gets.chomp.chars.map{|x|x==\"L\" ? 0 : 1}\r\n a=[1]*(n+1)\r\n ll=0\r\n 1.upto(n-1) do |i|\r\n if s[i-1]==0\r\n a[i]+=i-ll\r\n end\r\n if s[i]==s[i-1]\r\n ll=i\r\n end\r\n end\r\n if s[n-1]==0\r\n a[n]+=n-ll\r\n end\r\n rr=n\r\n (n-1).downto(1) do |i|\r\n if s[i]==1\r\n a[i]+=rr-i\r\n end\r\n if s[i]==s[i-1]\r\n rr=i\r\n end\r\n end\r\n if s[0]==1\r\n a[0]+=rr\r\n end\r\n puts a.join(\" \")\r\nend\r\n"}], "negative_code": [], "src_uid": "f51a46e87b8871c3f581786f84e5e6d1"} {"nl": {"description": "Fox Ciel is playing a mobile puzzle game called \"Two Dots\". The basic levels are played on a board of size n × m cells, like this:Each cell contains a dot that has some color. We will use different uppercase Latin characters to express different colors.The key of this game is to find a cycle that contain dots of same color. Consider 4 blue dots on the picture forming a circle as an example. Formally, we call a sequence of dots d1, d2, ..., dk a cycle if and only if it meets the following condition: These k dots are different: if i ≠ j then di is different from dj. k is at least 4. All dots belong to the same color. For all 1 ≤ i ≤ k - 1: di and di + 1 are adjacent. Also, dk and d1 should also be adjacent. Cells x and y are called adjacent if they share an edge. Determine if there exists a cycle on the field.", "input_spec": "The first line contains two integers n and m (2 ≤ n, m ≤ 50): the number of rows and columns of the board. Then n lines follow, each line contains a string consisting of m characters, expressing colors of dots in each line. Each character is an uppercase Latin letter.", "output_spec": "Output \"Yes\" if there exists a cycle, and \"No\" otherwise.", "sample_inputs": ["3 4\nAAAA\nABCA\nAAAA", "3 4\nAAAA\nABCA\nAADA", "4 4\nYYYR\nBYBY\nBBBY\nBBBY", "7 6\nAAAAAB\nABBBAB\nABAAAB\nABABBB\nABAAAB\nABBBAB\nAAAAAB", "2 13\nABCDEFGHIJKLM\nNOPQRSTUVWXYZ"], "sample_outputs": ["Yes", "No", "Yes", "Yes", "No"], "notes": "NoteIn first sample test all 'A' form a cycle.In second sample there is no such cycle.The third sample is displayed on the picture above ('Y' = Yellow, 'B' = Blue, 'R' = Red)."}, "positive_code": [{"source_code": "def dfs(i,j,direct,color)\n return false if i<0 || j<0 || i>=$n || j>=$m || $s[i][j]!=color\n return true if $used[i][j]\n $used[i][j]=true\n res = false\n res|=dfs(i+1,j,3,color) if direct!=1\n res|=dfs(i-1,j,1,color) if direct!=3\n res|=dfs(i,j+1,0,color) if direct!=2\n res|=dfs(i,j-1,2,color) if direct!=0\n res\nend\n\ndef getAnswer()\n $n.times {|i| $m.times {|j| return true if !$used[i][j] && \n\ndfs(i,j,0,$s[i][j])}}\n return false\nend\n\n$n,$m=STDIN.gets.split.map{|n|n.to_i}\n$s=Array.new($n) {STDIN.gets}\n$used=Array.new($n) {Array.new($m) {false}}\n\nif getAnswer then puts \"Yes\" else puts \"No\" end"}, {"source_code": "def dfs(i,j,direct,color)\n return false if i<0 || j<0 || i>=$n || j>=$m || $s[i][j]!=color\n return true if $used[i][j]\n $used[i][j]=true\n res = false\n res|=dfs(i+1,j,3,color) if direct!=1\n res|=dfs(i-1,j,1,color) if direct!=3\n res|=dfs(i,j+1,0,color) if direct!=2\n res|=dfs(i,j-1,2,color) if direct!=0\n res\nend\n\ndef getAnswer()\n $n.times {|i| $m.times {|j| return true if !$used[i][j] && \n\ndfs(i,j,0,$s[i][j])}}\n return false\nend\n\n$n,$m=STDIN.gets.split.map{|n|n.to_i}\n$s=Array.new($n) {STDIN.gets}\n\nf=[false]*$m\n$used=Array.new($n) {f.dup}\n\nif getAnswer then puts \"Yes\" else puts \"No\" end"}], "negative_code": [], "src_uid": "73930603e440eef854da4ba51253a5a7"} {"nl": {"description": "Polycarpus has n friends in Tarasov city. Polycarpus knows phone numbers of all his friends: they are strings s1, s2, ..., sn. All these strings consist only of digits and have the same length. Once Polycarpus needed to figure out Tarasov city phone code. He assumed that the phone code of the city is the longest common prefix of all phone numbers of his friends. In other words, it is the longest string c which is a prefix (the beginning) of each si for all i (1 ≤ i ≤ n). Help Polycarpus determine the length of the city phone code. ", "input_spec": "The first line of the input contains an integer n (2 ≤ n ≤ 3·104) — the number of Polycarpus's friends. The following n lines contain strings s1, s2, ..., sn — the phone numbers of Polycarpus's friends. It is guaranteed that all strings consist only of digits and have the same length from 1 to 20, inclusive. It is also guaranteed that all strings are different.", "output_spec": "Print the number of digits in the city phone code.", "sample_inputs": ["4\n00209\n00219\n00999\n00909", "2\n1\n2", "3\n77012345678999999999\n77012345678901234567\n77012345678998765432"], "sample_outputs": ["2", "0", "12"], "notes": "NoteA prefix of string t is a string that is obtained by deleting zero or more digits from the end of string t. For example, string \"00209\" has 6 prefixes: \"\" (an empty prefix), \"0\", \"00\", \"002\", \"0020\", \"00209\".In the first sample the city phone code is string \"00\".In the second sample the city phone code is an empty string.In the third sample the city phone code is string \"770123456789\"."}, "positive_code": [{"source_code": "\nn=gets.to_i\nnumbers=[]\nn.times do\n numbers<<=gets.split[0]\nend\nnumbers[0].size.downto(0) do |i|\n ok=true\n numbers.each do |e|\n ok=false unless e[0..i-1]==numbers[0][0..i-1]\n end\n if ok\n puts i\n exit\n end\nend\nputs 0"}, {"source_code": "n = gets.to_i\nphone = Array.new(n)\nphone[0] = gets.strip\nl = phone[0].length\nfor i in 1...n\n phone[i] = gets.strip\n x=0\n until phone[0][x] != phone[i][x] do x += 1 end\n l = x if x < l\nend\nprint l"}, {"source_code": "numbers = []\nreadline.to_i.times { numbers << readline.strip }\n\nans = 0\nhead = numbers.shift\ntail = numbers\nhead.length.times do |i|\n break unless tail.all? { |t| head[i] == t[i] }\n ans += 1\nend\np ans"}, {"source_code": "input = Array.new\n\n# remove all \\r on windows?\n\nSTDIN.read.split(\"\\n\").each { |line| input << line }\n\nn = input.delete_at 0\n\nprefix = input[0].split //\n\ninput.each do |line|\n letters = line.split //\n letters.each_index do |i|\n if letters[i] != prefix[i]\n prefix = prefix[0...i]\n break\n end\n end\nend\n\nputs prefix.length\n"}, {"source_code": "#!/usr/bin/ruby\nn = gets.chomp.to_i\nnumbers = []\nn.times do\n\tnumbers << gets.chomp\nend\nwritten = false\n(numbers[0].length-2).downto(0) do |i|\n\ten = true\n\tfor num in numbers\n\t\tif numbers[0][0..i] != num[0..i]\n\t\t\ten = false\n\t\t\tbreak\n\t\tend\n\tend\n\tif en == true\n\t\tputs i+1\n\t\twritten = true\n \t\tbreak\n\tend\nend\nputs 0 if not written\n"}, {"source_code": "#!/usr/bin/ruby\nn = gets.chomp.to_i\ns = []\nn.times do \n\ts << gets.chomp\nend\nmax = 0\nfor i in 0..s[0].length-2\n\tprosel = true\n\tfor si in s\n#\t\tprosel =false; break if si[0..i] != s[0][0..i]\n\t\tif si[0..i] != s[0][0..i]\n\t\t\tprosel = false\n\t\t\tbreak\n\t\tend\n\tend\n\tmax = i+1 if prosel == true\nend\nputs max\n"}, {"source_code": "n = gets.to_i\n\na = []\nn.times do\n a << gets\nend\n\na.sort!\n\ndef f(s, t)\n cnt = 0\n while cnt < s.length && cnt < t.length && s[cnt] == t[cnt]\n cnt = cnt + 1\n end\n cnt\nend\n\nans = a[0].length\n(a.length - 1 ).times do |i|\n t = f a[i], a[i+1]\n ans = t if t < ans\nend\n\nputs ans\n"}, {"source_code": "n = gets.to_i\n\na = []\nn.times do |i|\n a << gets\nend\n\na.sort!\n\ndef f(s, t)\n cnt = 0\n while cnt < s.length && cnt < t.length && s[cnt] == t[cnt]\n cnt = cnt + 1\n end\n return cnt\nend\n\nans = a[0].length\n(a.length - 1 ).times do |i|\n t = f a[i], a[i+1]\n if t < ans\n ans = t\n end\nend\n\nputs ans\n"}, {"source_code": "n = gets.to_i\nphoneNumbers = []\nn.times do\n\tphoneNumbers << gets\nend\nans = 0\nphoneNumbers[0].length.times do |guessAns|\n\tcheck = true\n\tn.times do |id|\n\t\tif phoneNumbers[id][0..guessAns] != phoneNumbers[0][0..guessAns]\n\t\t\tcheck = false\n\t\tend\n\tend\n\tif check == true\n\t\tans = guessAns+1\n\tend\nend\n\nputs ans\n\n\n"}, {"source_code": "n = gets.to_i\ns = gets.chomp\nres = s.size\n(n-1).times do \n s1 = gets.chomp\n 0.upto(res-1) do |i|\n if s[i] != s1[i] \n res = i\n break\n end\n end\nend\np res\n "}, {"source_code": "nums = []\nreadline.to_i.times { nums << readline.strip }\n\nans = 0\n\nhead = nums.shift\ntail = nums\nhead.length.times do |i|\n break unless tail.all? { |t| head[i] == t[i] }\n ans += 1\nend\n\np ans\n\n"}, {"source_code": "nums = []\nreadline.to_i.times { nums << readline.strip }\n\nans = 0\nnums[0].length.times do |i|\n break unless nums.all? { |num| nums[0][i] == num[i] }\n ans += 1\nend\n\np ans\n\n"}, {"source_code": "n = gets.to_i\na = []\nn.times do\n a << gets.chomp\nend\n\ndef check_pref(a, len)\n pref = a.first[0, len]\n a.drop(1).each do |s|\n return false unless s[0, len] == pref\n end\n true\nend\n\ndef pref_len(a)\n return 0 if a.empty?\n max_len = a.first.size\n max_len.downto 0 do |cur_len|\n return cur_len if check_pref(a, cur_len)\n end\n cur_len\nend\n\nputs pref_len(a)"}, {"source_code": "n = gets.to_i\ns0 = gets.chomp\nans = s0.length\n(1..n-1).each do\n s = gets.chomp\n i = 0\n while s[i] == s0[i]\n i += 1\n end\n ans = [ans, i].min\nend\nputs ans\n"}, {"source_code": "n = gets.to_i\n\nmassiv = Array.new(n) {Array.new()}\n\nfor i in(0..n-1)\n\tk = gets.split(\"\")\n\tlength = k.length - 1\n\t\n\t\n\tfor j in(0..length-1) \n\t\tmassiv[i][j] = k[j].to_i\n\tend\nend\n\np = 0\nt = 0\n\nfor j in(0..length-1)\n\tfor i in(0..n-2)\n\t\tif massiv[i][j] == massiv[i+1][j] then\n\t\t\tt+=1\n\t\telse\n\t\t\tbreak\n\t\tend\n\tend\n\t\n\tif t == 0 then\n\t\tbreak\n\tend\n\n\tif t == n-1 then\n\t\tp+=1\n\tend\n\n\tt = 0\nend\n\nputs p\n"}, {"source_code": "n, numbers = gets.to_i, STDIN.read.split(\"\\n\")\nprefix = numbers[0]\n\nn.times do |i|\n\tm = numbers[i][0..prefix.length - 1]\n\tif prefix == m\n\t\tnext\n\telse\n\t\twhile prefix != m\n\t\t\tprefix.chop!\n\t\t\tm.chop!\n\t\tend\n\tend\nend\n\nprint prefix.length"}, {"source_code": "# t = gets.split.collect{|x| x.to_i}\nn = gets.to_i\n\nresult = nil\n\nn.times do |i|\n\ts = gets.strip\n\tresult ||=s\n\ts = s.chars.to_a\n\ts.each_index do |i|\n\t\tif result[i] == nil or result[i] != s[i]\n\t\t\tif i == 0\n\t\t\t\tresult = \"\"\n\t\t\telse\n\t\t\t\tresult = result[0..i-1]\n\t\t\tend\n\t\t\tbreak\n\t\tend\n\tend\nend\n\nputs result.size"}], "negative_code": [], "src_uid": "081b35620952bd32ce6d8ddc756e5c9d"} {"nl": {"description": "So, the Berland is at war with its eternal enemy Flatland again, and Vasya, an accountant, was assigned to fulfil his duty to the nation. Right now the situation in Berland is dismal — their both cities are surrounded! The armies of flatlanders stand on the borders of circles, the circles' centers are in the surrounded cities. At any moment all points of the flatland ring can begin to move quickly in the direction of the city — that's the strategy the flatlanders usually follow when they besiege cities.The berlanders are sure that they can repel the enemy's attack if they learn the exact time the attack starts. For that they need to construct a radar that would register any movement at the distance of at most r from it. Thus, we can install a radar at such point, that at least one point of the enemy ring will be in its detecting range (that is, at a distance of at most r). Then the radar can immediately inform about the enemy's attack. Due to the newest technologies, we can place a radar at any point without any problems. But the problem is that the berlanders have the time to make only one radar. Besides, the larger the detection radius (r) is, the more the radar costs.That's why Vasya's task (that is, your task) is to find the minimum possible detection radius for the radar. In other words, your task is to find the minimum radius r (r ≥ 0) such, that a radar with radius r can be installed at some point and it can register the start of the movements of both flatland rings from that point. In this problem you can consider the cities as material points, the attacking enemy rings - as circles with centers in the cities, the radar's detection range — as a disk (including the border) with the center at the point where the radar is placed.", "input_spec": "The input files consist of two lines. Each line represents the city and the flatland ring that surrounds it as three space-separated integers xi, yi, ri (|xi|, |yi| ≤ 104; 1 ≤ ri ≤ 104) — the city's coordinates and the distance from the city to the flatlanders, correspondingly. It is guaranteed that the cities are located at different points.", "output_spec": "Print a single real number — the minimum detection radius of the described radar. The answer is considered correct if the absolute or relative error does not exceed 10 - 6.", "sample_inputs": ["0 0 1\n6 0 3", "-10 10 3\n10 -10 3"], "sample_outputs": ["1.000000000000000", "11.142135623730951"], "notes": "NoteThe figure below shows the answer to the first sample. In this sample the best decision is to put the radar at point with coordinates (2, 0). The figure below shows the answer for the second sample. In this sample the best decision is to put the radar at point with coordinates (0, 0). "}, "positive_code": [{"source_code": "x1,y1,r1 = gets.split(\" \").map(&:to_i)\nx2,y2,r2 = gets.split(\" \").map(&:to_i)\n\ndist = Math.sqrt((x1-x2)**2 + (y1-y2)**2)\nif (r1-r2).abs <= dist && dist <= (r1+r2)\n\tputs 0\nelse\n\tx = [dist,r1,r2].sort.reverse\n\tputs (x[0] - x[1] - x[2])/2\nend"}], "negative_code": [], "src_uid": "8996ae454ba3062e47a8aaab7fb1e33b"} {"nl": {"description": "Find an n × n matrix with different numbers from 1 to n2, so the sum in each row, column and both main diagonals are odd.", "input_spec": "The only line contains odd integer n (1 ≤ n ≤ 49).", "output_spec": "Print n lines with n integers. All the integers should be different and from 1 to n2. The sum in each row, column and both main diagonals should be odd.", "sample_inputs": ["1", "3"], "sample_outputs": ["1", "2 1 4\n3 5 7\n6 9 8"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/ruby\ndef gen_odd(n)\n\tm=n.times.map{[0]*n}\n\tr=n-1\n\tc=n/2\n\ti=0\n\tn.times{\n\t\tr=(r+1)%n\n\t\tm[r][c]=i+=1\n\t\t(n-1).times{\n\t\t\tr=(r+n-1)%n\n\t\t\tc=(c+1)%n\n\t\t\tm[r][c]=i+=1\n\t\t}\n\t}\n\tm\nend\ndef gen_quad(n)\n\tz=n**2\n\ti=0\n\tm=n.times.map{[0]*n}\n\tn.times{|r|n.times{|c|\n\t\tj,k=i.divmod(n)\n\t\tj%=4\n\t\tk%=4\n\t\tm[r][c]=i+=1\n\t\tm[r][c]=z-m[r][c]+1 if (j==0||j==3)&&(k==1||k==2) or (j==1||j==2)&&(k==0||k==3)\n\t}}\n\tm\nend\ndef gen_lux(n)\n\tm=n.times.map{[0]*n}\n\to={l:[[4,1],[2,3]],u:[[1,4],[2,3]],x:[[1,4],[3,2]]}\n\tlux=(n/4+1).times.map{[:l]*(n/2)} + [[:u]*(n/2)] + (n/4-1).times.map{[:x]*(n/2)}\n\tlux[n/4][n/4],lux[n/4+1][n/4]=lux[n/4+1][n/4],lux[n/4][n/4]\n\tb=gen_odd(n/2).map{|e|e.map{|f|4*(f-1)}}\n\tn.times{|r|n.times{|c|\n\t\tbr,xr=r.divmod(2)\n\t\tbc,xc=c.divmod(2)\n\t\tm[r][c]=b[br][bc]+o[lux[br][bc]][xr][xc]\n\t}}\n\tm\nend\ndef gen_magicsquare(n)\n\tif n%2==1\n\t\tgen_odd(n)\n\telsif n%4==0\n\t\tgen_quad(n)\n\telse\n\t\tgen_lux(n)\n\tend\nend\nputs gen_magicsquare(eval *$<).map{|e|e*' '}"}], "negative_code": [], "src_uid": "a7da19d857ca09f052718cb69f2cea57"} {"nl": {"description": "Recently, the students of School 179 have developed a unique algorithm, which takes in a binary string $$$s$$$ as input. However, they soon found out that if some substring $$$t$$$ of $$$s$$$ is a palindrome of length greater than 1, the algorithm will work incorrectly. Can the students somehow reorder the characters of $$$s$$$ so that the algorithm will work correctly on the string?A binary string is a string where each character is either 0 or 1.A string $$$a$$$ is a substring of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.A palindrome is a string that reads the same backwards as forwards.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 100$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 100$$$) — the length of the string $$$s$$$. The second line of each test case contains the string $$$s$$$ of length $$$n$$$ consisting only of the characters 0 and 1.", "output_spec": "For each test case, print YES (case-insensitive) if it is possible to reorder the characters of $$$s$$$ so that there are no substrings that are a palindrome of length greater than 1, or NO (case-insensitive) otherwise.", "sample_inputs": ["4\n\n1\n\n1\n\n2\n\n10\n\n2\n\n01\n\n4\n\n1010"], "sample_outputs": ["YES\nYES\nYES\nNO"], "notes": "NoteIn the first three test cases, the given strings do not contain palindromes of length greater than 1, so the answers are YES.In the last test case, it is impossible to reorder the characters so that the string does not contain palindromes of length greater than 1, so the answer is NO."}, "positive_code": [{"source_code": "for _ in 0...(gets.chomp.to_i)\r\n len = gets.chomp.to_i\r\n string = gets.chomp\r\n if len >= 3\r\n puts \"NO\"\r\n elsif (string == \"00\") or (string == \"11\")\r\n puts \"NO\"\r\n else\r\n puts \"YES\"\r\n end\r\nend"}, {"source_code": "gets.to_i.times do\n n = gets.to_i\n s = gets.strip\n\n if n == 1 || s == \"01\" || s == \"10\"\n puts \"YES\"\n else\n puts \"NO\"\n end\nend\n"}], "negative_code": [{"source_code": "for _ in 0...(gets.chomp.to_i)\r\n len = gets.chomp.to_i\r\n string = gets.chomp\r\n if (len == 1) or (len == 2)\r\n puts \"YES\"\r\n else\r\n if string.count(\"0\") == string.count(\"1\")\r\n puts \"NO\"\r\n else\r\n puts \"YES\"\r\n end\r\n end\r\nend"}], "src_uid": "354658f565e265c2a1ce37355d6466e1"} {"nl": {"description": "Arkady invited Anna for a dinner to a sushi restaurant. The restaurant is a bit unusual: it offers $$$n$$$ pieces of sushi aligned in a row, and a customer has to choose a continuous subsegment of these sushi to buy.The pieces of sushi are of two types: either with tuna or with eel. Let's denote the type of the $$$i$$$-th from the left sushi as $$$t_i$$$, where $$$t_i = 1$$$ means it is with tuna, and $$$t_i = 2$$$ means it is with eel.Arkady does not like tuna, Anna does not like eel. Arkady wants to choose such a continuous subsegment of sushi that it has equal number of sushi of each type and each half of the subsegment has only sushi of one type. For example, subsegment $$$[2, 2, 2, 1, 1, 1]$$$ is valid, but subsegment $$$[1, 2, 1, 2, 1, 2]$$$ is not, because both halves contain both types of sushi.Find the length of the longest continuous subsegment of sushi Arkady can buy.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$2 \\le n \\le 100\\,000$$$) — the number of pieces of sushi. The second line contains $$$n$$$ integers $$$t_1$$$, $$$t_2$$$, ..., $$$t_n$$$ ($$$t_i = 1$$$, denoting a sushi with tuna or $$$t_i = 2$$$, denoting a sushi with eel), representing the types of sushi from left to right. It is guaranteed that there is at least one piece of sushi of each type. Note that it means that there is at least one valid continuous segment.", "output_spec": "Print a single integer — the maximum length of a valid continuous segment.", "sample_inputs": ["7\n2 2 2 1 1 2 2", "6\n1 2 1 2 1 2", "9\n2 2 1 1 1 2 2 2 2"], "sample_outputs": ["4", "2", "6"], "notes": "NoteIn the first example Arkady can choose the subsegment $$$[2, 2, 1, 1]$$$ or the subsegment $$$[1, 1, 2, 2]$$$ with length $$$4$$$.In the second example there is no way but to choose one of the subsegments $$$[2, 1]$$$ or $$$[1, 2]$$$ with length $$$2$$$.In the third example Arkady's best choice is the subsegment $$$[1, 1, 1, 2, 2, 2]$$$."}, "positive_code": [{"source_code": "n = $stdin.gets.chomp.to_i\n\nlist = []\ninput = $stdin.gets.chomp.split(' ')\n#puts input\npre = input[0]\n#puts \"pre #{pre}\"\ntmp_cnt = 1\n(1..n).each do |i|\n\ttmp = input[i]\n\t#puts \"tmp #{pre}\"\n\tif tmp==pre\n\t\ttmp_cnt+=1\n\telse\n\t\tlist.push(tmp_cnt)\n\t\ttmp_cnt=1\n\t\tpre = tmp\n\tend\nend\n\n#puts list\nmx=-1\nln = list.length\n(0..ln-2).each do |i|\n\tk = list[i] < list[i+1] ? list[i] : list[i+1] \n\t#puts \"---#{k}\"\n\tif k>mx\n\t\tmx = k\n\tend\nend\n\nputs (mx*2)\n\n\n"}], "negative_code": [], "src_uid": "6477fdad8455f57555f93c021995bb4d"} {"nl": {"description": "You have a permutation: an array $$$a = [a_1, a_2, \\ldots, a_n]$$$ of distinct integers from $$$1$$$ to $$$n$$$. The length of the permutation $$$n$$$ is odd.Consider the following algorithm of sorting the permutation in increasing order.A helper procedure of the algorithm, $$$f(i)$$$, takes a single argument $$$i$$$ ($$$1 \\le i \\le n-1$$$) and does the following. If $$$a_i > a_{i+1}$$$, the values of $$$a_i$$$ and $$$a_{i+1}$$$ are exchanged. Otherwise, the permutation doesn't change.The algorithm consists of iterations, numbered with consecutive integers starting with $$$1$$$. On the $$$i$$$-th iteration, the algorithm does the following: if $$$i$$$ is odd, call $$$f(1), f(3), \\ldots, f(n - 2)$$$; if $$$i$$$ is even, call $$$f(2), f(4), \\ldots, f(n - 1)$$$. It can be proven that after a finite number of iterations the permutation will be sorted in increasing order.After how many iterations will this happen for the first time?", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 100$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$3 \\le n \\le 999$$$; $$$n$$$ is odd) — the length of the permutation. The second line contains $$$n$$$ distinct integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le n$$$) — the permutation itself. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$999$$$.", "output_spec": "For each test case print the number of iterations after which the permutation will become sorted in increasing order for the first time. If the given permutation is already sorted, print $$$0$$$.", "sample_inputs": ["3\n3\n3 2 1\n7\n4 5 7 1 3 2 6\n5\n1 2 3 4 5"], "sample_outputs": ["3\n5\n0"], "notes": "NoteIn the first test case, the permutation will be changing as follows: after the $$$1$$$-st iteration: $$$[2, 3, 1]$$$; after the $$$2$$$-nd iteration: $$$[2, 1, 3]$$$; after the $$$3$$$-rd iteration: $$$[1, 2, 3]$$$. In the second test case, the permutation will be changing as follows: after the $$$1$$$-st iteration: $$$[4, 5, 1, 7, 2, 3, 6]$$$; after the $$$2$$$-nd iteration: $$$[4, 1, 5, 2, 7, 3, 6]$$$; after the $$$3$$$-rd iteration: $$$[1, 4, 2, 5, 3, 7, 6]$$$; after the $$$4$$$-th iteration: $$$[1, 2, 4, 3, 5, 6, 7]$$$; after the $$$5$$$-th iteration: $$$[1, 2, 3, 4, 5, 6, 7]$$$. In the third test case, the permutation is already sorted and the answer is $$$0$$$."}, "positive_code": [{"source_code": "def solve\n n = gets.to_i\n n -= 1\n a = gets.split(' ').map { |s| s.to_i}\n it, i, tmp, res = 0, 0, 0, 0\n f = 0\n while f < 2\n res += 1\n i = it\n it = (it + 1) % 2\n f += 1\n while i < n\n if a[i] > a[i + 1]\n tmp = a[i]\n a[i] = a[i + 1]\n a[i + 1] = tmp\n f = 0\n end\n i += 2\n end\n #puts \"a : #{a}\"\n end\n res -= 2\n puts res\nend\n\nt = gets.to_i\nfor z in 0...t\n solve\nend\n"}, {"source_code": "t = gets.to_i\nt.times do\n\nn = gets.to_i\na = gets.split.map!(&:to_i)\nb = a.sort\n\ncnt = 0\nwhile a != b\n (cnt & 1).step(n - 2, 2) do |i|\n if a[i] > a[i+1]\n a[i], a[i+1] = a[i+1], a[i]\n end\n end\n cnt += 1\nend\n\np cnt\n\nend\n"}], "negative_code": [], "src_uid": "d4bcc53b470e4beaa078d5ce3785c6cb"} {"nl": {"description": "Mike decided to teach programming to children in an elementary school. He knows that it is not an easy task to interest children in that age to code. That is why he decided to give each child two sweets.Mike has $$$n$$$ sweets with sizes $$$a_1, a_2, \\ldots, a_n$$$. All his sweets have different sizes. That is, there is no such pair $$$(i, j)$$$ ($$$1 \\leq i, j \\leq n$$$) such that $$$i \\ne j$$$ and $$$a_i = a_j$$$.Since Mike has taught for many years, he knows that if he gives two sweets with sizes $$$a_i$$$ and $$$a_j$$$ to one child and $$$a_k$$$ and $$$a_p$$$ to another, where $$$(a_i + a_j) \\neq (a_k + a_p)$$$, then a child who has a smaller sum of sizes will be upset. That is, if there are two children who have different sums of sweets, then one of them will be upset. Apparently, Mike does not want somebody to be upset. Mike wants to invite children giving each of them two sweets. Obviously, he can't give one sweet to two or more children. His goal is to invite as many children as he can. Since Mike is busy preparing to his first lecture in the elementary school, he is asking you to find the maximum number of children he can invite giving each of them two sweets in such way that nobody will be upset.", "input_spec": "The first line contains one integer $$$n$$$ ($$$2 \\leq n \\leq 1\\,000$$$) — the number of sweets Mike has. The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\leq a_i \\leq 10^5$$$) — the sizes of the sweets. It is guaranteed that all integers are distinct.", "output_spec": "Print one integer — the maximum number of children Mike can invite giving each of them two sweets in such way that nobody will be upset.", "sample_inputs": ["8\n1 8 3 11 4 9 2 7", "7\n3 1 7 11 9 2 12"], "sample_outputs": ["3", "2"], "notes": "NoteIn the first example, Mike can give $$$9+2=11$$$ to one child, $$$8+3=11$$$ to another one, and $$$7+4=11$$$ to the third child. Therefore, Mike can invite three children. Note that it is not the only solution.In the second example, Mike can give $$$3+9=12$$$ to one child and $$$1+11$$$ to another one. Therefore, Mike can invite two children. Note that it is not the only solution."}, "positive_code": [{"source_code": "n = gets.chomp.to_i\nw = Array.new\nl = gets.chomp.split\nl.each do |i|\n w << i.to_i \nend\nh = Hash.new(0)\n(0..n-2).each do |i|\n ((i+1)..n-1).each do |j|\n l = w[i] + w[j]\n if h[l] == 0\n h[l] = 1\n else\n a = h[l] + 1\n h[l] = a\n end\n end\nend\nw = -1\nh.each do |key, value|\n if value > w\n w = value\n end\nend\nputs w"}, {"source_code": "n = gets.strip.to_i\na = gets.split(\" \").map(&:to_i)\n\nh = {}\n(0..n-2).each do |i|\n (i+1..n-1).each do |j|\n if h[a[i]+a[j]] \n\th[a[i]+a[j]] = h[a[i]+a[j]] + 1 \n else\n\th[a[i]+a[j]] = 1 \n end\t \n end\nend\n\nputs h.to_a.map{|e| e[1]}.max\n\n"}], "negative_code": [], "src_uid": "44619ba06ec0dc410ef598ea45a76271"} {"nl": {"description": "There are $$$n$$$ piles of sand where the $$$i$$$-th pile has $$$a_i$$$ blocks of sand. The $$$i$$$-th pile is called too tall if $$$1 < i < n$$$ and $$$a_i > a_{i-1} + a_{i+1}$$$. That is, a pile is too tall if it has more sand than its two neighbours combined. (Note that piles on the ends of the array cannot be too tall.)You are given an integer $$$k$$$. An operation consists of picking $$$k$$$ consecutive piles of sand and adding one unit of sand to them all. Formally, pick $$$1 \\leq l,r \\leq n$$$ such that $$$r-l+1=k$$$. Then for all $$$l \\leq i \\leq r$$$, update $$$a_i \\gets a_i+1$$$.What is the maximum number of piles that can simultaneously be too tall after some (possibly zero) operations?", "input_spec": "The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \\leq t \\leq 1000$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$3 \\leq n \\leq 2 \\cdot 10^5$$$; $$$1 \\leq k \\leq n$$$) — the number of piles of sand and the size of the operation, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$) — the sizes of the piles. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, output a single integer — the maximum number of piles that are simultaneously too tall after some (possibly zero) operations.", "sample_inputs": ["3\n\n5 2\n\n2 9 2 4 1\n\n4 4\n\n1 3 2 1\n\n3 1\n\n1 3 1"], "sample_outputs": ["2\n0\n1"], "notes": "NoteIn the first test case, we can perform the following three operations: Add one unit of sand to piles $$$1$$$ and $$$2$$$: $$$[\\color{red}{3}, \\color{red}{10}, 2, 4, 1]$$$. Add one unit of sand to piles $$$4$$$ and $$$5$$$: $$$[3, 10, 2, \\color{red}{5}, \\color{red}{2}]$$$. Add one unit of sand to piles $$$3$$$ and $$$4$$$: $$$[3, 10, \\color{red}{3}, \\color{red}{6}, 2]$$$. Now piles $$$2$$$ and $$$4$$$ are too tall, so in this case the answer is $$$2$$$. It can be shown that it is impossible to make more than $$$2$$$ piles too tall.In the second test case, any operation will increase all piles by $$$1$$$ unit, so the number of too tall piles will always be $$$0$$$.In the third test case, we can increase any pile by $$$1$$$ unit of sand. It can be shown that the maximum number of too tall piles is $$$1$$$."}, "positive_code": [{"source_code": "# gets.to_i\r\n# gets.split.map(&:to_i)\r\n\r\ngets.to_i.times do\r\n n, k = gets.split.map(&:to_i)\r\n a = gets.split.map(&:to_i)\r\n\r\n if k != 1\r\n i = 1\r\n ans = 0\r\n while i < n-1\r\n if a[i-1]+a[i+1] < a[i]\r\n ans += 1\r\n end\r\n i += 1\r\n end\r\n else\r\n ans = (n-1)/2\r\n end\r\n puts ans\r\nend\r\n\r\n"}], "negative_code": [{"source_code": "# gets.to_i\r\n# gets.split.map(&:to_i)\r\n\r\ngets.to_i.times do\r\n n, k = gets.split.map(&:to_i)\r\n a = gets.split.map(&:to_i)\r\n\r\n if k == 1\r\n i = 1\r\n ans = 0\r\n while i < n-1\r\n if a[i-1]+a[i+1] < a[i]\r\n ans += 1\r\n end\r\n i += 1\r\n end\r\n else\r\n ans = (n-1)/2\r\n end\r\n puts ans\r\nend\r\n"}, {"source_code": "# gets.to_i\r\n# gets.split.map(&:to_i)\r\n\r\ngets.to_i.times do\r\n n, k = gets.split.map(&:to_i)\r\n a = gets.split.map(&:to_i)\r\n\r\n if k == n\r\n i = 1\r\n ans = 0\r\n while i < n-1\r\n if a[i-1]+a[i+1] < a[i]\r\n ans += 1\r\n end\r\n i += 1\r\n end\r\n else\r\n ans = (n-1)/2\r\n end\r\n puts ans\r\nend\r\n"}, {"source_code": "# gets.to_i\r\n# gets.split.map(&:to_i)\r\n\r\ngets.to_i.times do\r\n n, k = gets.split.map(&:to_i)\r\n a = gets.split.map(&:to_i)\r\n\r\n ans = 0\r\n i = 1\r\n while i < n - 1\r\n if a[i-1]+a[i+1] < a[i]\r\n ans += 1\r\n next i += 2\r\n end\r\n i += 1\r\n end\r\n 1.upto(n-2) do |i|\r\n end\r\n\r\n puts ans\r\nend\r\n\r\n"}, {"source_code": "# gets.to_i\r\n# gets.split.map(&:to_i)\r\n\r\ngets.to_i.times do\r\n n, k = gets.split.map(&:to_i)\r\n a = gets.split.map(&:to_i)\r\n\r\n ans = 0\r\n 1.upto(n-2) do |i|\r\n if a[i-1]+a[i+1] < a[i]\r\n ans += 1\r\n end\r\n end\r\n\r\n puts ans\r\nend\r\n\r\n"}], "src_uid": "4d5d20fd586ddbea2adeab104a6c2aec"} {"nl": {"description": "Alice and Bob are playing chess on a huge chessboard with dimensions $$$n \\times n$$$. Alice has a single piece left — a queen, located at $$$(a_x, a_y)$$$, while Bob has only the king standing at $$$(b_x, b_y)$$$. Alice thinks that as her queen is dominating the chessboard, victory is hers. But Bob has made a devious plan to seize the victory for himself — he needs to march his king to $$$(c_x, c_y)$$$ in order to claim the victory for himself. As Alice is distracted by her sense of superiority, she no longer moves any pieces around, and it is only Bob who makes any turns.Bob will win if he can move his king from $$$(b_x, b_y)$$$ to $$$(c_x, c_y)$$$ without ever getting in check. Remember that a king can move to any of the $$$8$$$ adjacent squares. A king is in check if it is on the same rank (i.e. row), file (i.e. column), or diagonal as the enemy queen. Find whether Bob can win or not.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$3 \\leq n \\leq 1000$$$) — the dimensions of the chessboard. The second line contains two integers $$$a_x$$$ and $$$a_y$$$ ($$$1 \\leq a_x, a_y \\leq n$$$) — the coordinates of Alice's queen. The third line contains two integers $$$b_x$$$ and $$$b_y$$$ ($$$1 \\leq b_x, b_y \\leq n$$$) — the coordinates of Bob's king. The fourth line contains two integers $$$c_x$$$ and $$$c_y$$$ ($$$1 \\leq c_x, c_y \\leq n$$$) — the coordinates of the location that Bob wants to get to. It is guaranteed that Bob's king is currently not in check and the target location is not in check either. Furthermore, the king is not located on the same square as the queen (i.e. $$$a_x \\neq b_x$$$ or $$$a_y \\neq b_y$$$), and the target does coincide neither with the queen's position (i.e. $$$c_x \\neq a_x$$$ or $$$c_y \\neq a_y$$$) nor with the king's position (i.e. $$$c_x \\neq b_x$$$ or $$$c_y \\neq b_y$$$).", "output_spec": "Print \"YES\" (without quotes) if Bob can get from $$$(b_x, b_y)$$$ to $$$(c_x, c_y)$$$ without ever getting in check, otherwise print \"NO\". You can print each letter in any case (upper or lower).", "sample_inputs": ["8\n4 4\n1 3\n3 1", "8\n4 4\n2 3\n1 6", "8\n3 5\n1 2\n6 1"], "sample_outputs": ["YES", "NO", "NO"], "notes": "NoteIn the diagrams below, the squares controlled by the black queen are marked red, and the target square is marked blue.In the first case, the king can move, for instance, via the squares $$$(2, 3)$$$ and $$$(3, 2)$$$. Note that the direct route through $$$(2, 2)$$$ goes through check. In the second case, the queen watches the fourth rank, and the king has no means of crossing it. In the third case, the queen watches the third file. "}, "positive_code": [{"source_code": "n = gets.to_i\ns = gets\nax, ay = s.split\ns = gets\nbx, by = s.split\ns = gets\ncx, cy = s.split\n#puts ax == 12\nif bx.to_i < ax.to_i && ax.to_i < cx.to_i\n\tputs \"NO\"\nelsif bx.to_i > ax.to_i && ax.to_i > cx.to_i\n\tputs \"NO\"\nelsif by.to_i < ay.to_i && ay.to_i < cy.to_i\n\tputs \"NO\"\nelsif by.to_i > ay.to_i && ay.to_i > cy.to_i\n\t#puts \"heheudh\"\n\tputs \"NO\"\nelse\n\tputs \"YES\"\nend"}], "negative_code": [{"source_code": "n = gets.to_i\ns = gets\nax, ay = s.split\ns = gets\nbx, by = s.split\ns = gets\ncx, cy = s.split\nif bx < ax && ax < cx\n\tputs \"NO\"\nelsif bx > ax && ax > cx\n\tputs \"NO\"\nelsif by < ay && ay < cy\n\tputs \"NO\"\nelsif by > ay && ay > cy\n\tputs \"NO\"\nelse\n\tputs \"YES\"\nend"}], "src_uid": "c404761a9562ff6034010a553d944324"} {"nl": {"description": "Pasha is participating in a contest on one well-known website. This time he wants to win the contest and will do anything to get to the first place!This contest consists of n problems, and Pasha solves ith problem in ai time units (his solutions are always correct). At any moment of time he can be thinking about a solution to only one of the problems (that is, he cannot be solving two problems at the same time). The time Pasha spends to send his solutions is negligible. Pasha can send any number of solutions at the same moment.Unfortunately, there are too many participants, and the website is not always working. Pasha received the information that the website will be working only during m time periods, jth period is represented by its starting moment lj and ending moment rj. Of course, Pasha can send his solution only when the website is working. In other words, Pasha can send his solution at some moment T iff there exists a period x such that lx ≤ T ≤ rx.Pasha wants to know his best possible result. We need to tell him the minimal moment of time by which he is able to have solutions to all problems submitted, if he acts optimally, or say that it's impossible no matter how Pasha solves the problems.", "input_spec": "The first line contains one integer n (1 ≤ n ≤ 1000) — the number of problems. The second line contains n integers ai (1 ≤ ai ≤ 105) — the time Pasha needs to solve ith problem. The third line contains one integer m (0 ≤ m ≤ 1000) — the number of periods of time when the website is working. Next m lines represent these periods. jth line contains two numbers lj and rj (1 ≤ lj < rj ≤ 105) — the starting and the ending moment of jth period. It is guaranteed that the periods are not intersecting and are given in chronological order, so for every j > 1 the condition lj > rj - 1 is met.", "output_spec": "If Pasha can solve and submit all the problems before the end of the contest, print the minimal moment of time by which he can have all the solutions submitted. Otherwise print \"-1\" (without brackets).", "sample_inputs": ["2\n3 4\n2\n1 4\n7 9", "1\n5\n1\n1 4", "1\n5\n1\n1 5"], "sample_outputs": ["7", "-1", "5"], "notes": "NoteIn the first example Pasha can act like this: he solves the second problem in 4 units of time and sends it immediately. Then he spends 3 time units to solve the first problem and sends it 7 time units after the contest starts, because at this moment the website starts working again.In the second example Pasha invents the solution only after the website stops working for the last time.In the third example Pasha sends the solution exactly at the end of the first period."}, "positive_code": [{"source_code": "gets\ns=gets.split.map(&:to_i).reduce(:+)\ngets.to_i.times{\n\tl,r=gets.split.map(&:to_i)\n\ts<=r&&(p [l,s].max;exit)\n}\np -1"}, {"source_code": "n = Integer(gets.chomp)\nsolve = Array.new(n)\nsolve = gets.chomp.split.map &:to_i\nm = Integer(gets.chomp)\nperiod = Array.new(m)\nfor i in 0...m\n period[i] = gets.chomp.split.map &:to_i\nend\n\ntime_solve = 0\nfor i in 0...n\n time_solve += solve[i]\nend\noutput = -1\nfor i in 0..m-1\n if time_solve <= period[i][1] && time_solve >= period[i][0]\n output = time_solve\n break\n elsif i != m - 1 && time_solve < period[i + 1][0] && time_solve > period[i][1] \n output = period[i + 1][0]\n break\n end\nend\n\nfor i in 0...m\n if time_solve <= period[i][0]\n output = period[i][0]\n break\n elsif time_solve <= period[i][1]\n output = time_solve\n break\n end\nend\n\nputs output"}], "negative_code": [{"source_code": "n = Integer(gets.chomp)\nsolve = Array.new(n)\nsolve = gets.chomp.split.map &:to_i\nm = Integer(gets.chomp)\nperiod = Array.new(m)\nfor i in 0...m\n period[i] = gets.chomp.split.map &:to_i\nend\n\ntime_solve = 0\nfor i in 0...n\n time_solve += solve[i]\nend\noutput = -1\nfor i in 0...m\n if period[i][1] >= time_solve && time_solve >= period[i][0]\n output = time_solve\n break\n end\nend\n\nputs output"}, {"source_code": "n = Integer(gets.chomp)\nsolve = Array.new(n)\nsolve = gets.chomp.split.map &:to_i\nm = Integer(gets.chomp)\nperiod = Array.new(m)\nfor i in 0...m\n period[i] = gets.chomp.split.map &:to_i\nend\n\ntime_solve = 0\nfor i in 0...n\n time_solve += solve[i]\nend\noutput = -1\nfor i in 0...m\n for j in 0..1\n if period[i][j] >= time_solve\n output = period[i][j]\n break\n end\n end\nend\n\nputs output"}, {"source_code": "n = Integer(gets.chomp)\nsolve = Array.new(n)\nsolve = gets.chomp.split.map &:to_i\nm = Integer(gets.chomp)\nperiod = Array.new(m)\nfor i in 0...m\n period[i] = gets.chomp.split.map &:to_i\nend\n\ntime_solve = 0\nfor i in 0...n\n time_solve += solve[i]\nend\noutput = -1\nfor i in 0..m-1\n if time_solve <= period[i][1] && time_solve >= period[i][0]\n output = time_solve\n break\n elsif i != m - 1 && time_solve < period[i + 1][0] && time_solve > period[i][1] \n output = period[i + 1][0]\n break\n end\nend\n\nfor i in 0...m\n if time_solve <= period[i][0]\n output = period[i][1]\n break\n elsif time_solve <= period[i][1]\n output = time_solve\n break\n end\nend\n\nputs output"}, {"source_code": "n = Integer(gets.chomp)\nsolve = Array.new(n)\nsolve = gets.chomp.split.map &:to_i\nm = Integer(gets.chomp)\nperiod = Array.new(m)\nfor i in 0...m\n period[i] = gets.chomp.split.map &:to_i\nend\n\ntime_solve = 0\nfor i in 0...n\n time_solve += solve[i]\nend\noutput = -1\nfor i in 0...m\n for j in 0..1\n if period[i][j] >= time_solve\n output = time_solve\n break\n end\n end\nend\n\nputs output"}, {"source_code": "n = Integer(gets.chomp)\nsolve = Array.new(n)\nsolve = gets.chomp.split.map &:to_i\nm = Integer(gets.chomp)\nperiod = Array.new(m)\nfor i in 0...m\n period[i] = gets.chomp.split.map &:to_i\nend\n\ntime_solve = 0\nfor i in 0...n\n time_solve += solve[i]\nend\noutput = -1\nfor i in 0..m-1\n if time_solve <= period[i][1] && time_solve >= period[i][0]\n output = time_solve\n break\n elsif i != m - 1 && time_solve < period[i + 1][0] && time_solve > period[i][1] \n output = period[i + 1][0]\n break\n end\nend\n\nputs output"}, {"source_code": "n = Integer(gets.chomp)\nsolve = Array.new(n)\nsolve = gets.chomp.split.map &:to_i\nm = Integer(gets.chomp)\nperiod = Array.new(m)\nfor i in 0...m\n period[i] = gets.chomp.split.map &:to_i\nend\n\ntime_solve = 0\nfor i in 0...n\n time_solve += solve[i]\nend\noutput = -1\nfor i in 0...m-1\n if period[i][1] >= time_solve && time_solve >= period[i][0]\n output = time_solve\n break\n elsif time_solve < period[i + 1][0] && time_solve > period[i][1]\n output = period[i + 1][0]\n break\n end\nend\n\nputs output"}, {"source_code": "n = Integer(gets.chomp)\nsolve = Array.new(n)\nsolve = gets.chomp.split.map &:to_i\nm = Integer(gets.chomp)\nperiod = Array.new(m)\nfor i in 0...m\n period[i] = gets.chomp.split.map &:to_i\nend\n\ntime_solve = 0\nfor i in 0...n\n time_solve += solve[i]\nend\noutput = -1\nfor i in 1...m\n if period[i][1] >= time_solve && time_solve >= period[i][0]\n output = time_solve\n break\n elsif time_solve > period[i - 1][1] && time_solve < period[i][0]\n output = period[i][0]\n break\n end\nend\n\nputs output"}], "src_uid": "311f74b766818633581af67a88244709"} {"nl": {"description": "You are given a positive decimal number x.Your task is to convert it to the \"simple exponential notation\".Let x = a·10b, where 1 ≤ a < 10, then in general case the \"simple exponential notation\" looks like \"aEb\". If b equals to zero, the part \"Eb\" should be skipped. If a is an integer, it should be written without decimal point. Also there should not be extra zeroes in a and b.", "input_spec": "The only line contains the positive decimal number x. The length of the line will not exceed 106. Note that you are given too large number, so you can't use standard built-in data types \"float\", \"double\" and other.", "output_spec": "Print the only line — the \"simple exponential notation\" of the given number x.", "sample_inputs": ["16", "01.23400", ".100", "100."], "sample_outputs": ["1.6E1", "1.234", "1E-1", "1E2"], "notes": null}, "positive_code": [{"source_code": "require'bigdecimal';x=BigDecimal(gets).split;s=x[1][0];s<<'.'+x[1][1..-1] if x[1].size>1;s<<'E%d'%[x[3]-1] if x[3]!=1;puts s"}], "negative_code": [], "src_uid": "b1eebb3928925b84a95b9f1e5a31e4f3"} {"nl": {"description": "A programming coach has n students to teach. We know that n is divisible by 3. Let's assume that all students are numbered from 1 to n, inclusive.Before the university programming championship the coach wants to split all students into groups of three. For some pairs of students we know that they want to be on the same team. Besides, if the i-th student wants to be on the same team with the j-th one, then the j-th student wants to be on the same team with the i-th one. The coach wants the teams to show good results, so he wants the following condition to hold: if the i-th student wants to be on the same team with the j-th, then the i-th and the j-th students must be on the same team. Also, it is obvious that each student must be on exactly one team.Help the coach and divide the teams the way he wants.", "input_spec": "The first line of the input contains integers n and m (3 ≤ n ≤ 48, . Then follow m lines, each contains a pair of integers ai, bi (1 ≤ ai < bi ≤ n) — the pair ai, bi means that students with numbers ai and bi want to be on the same team. It is guaranteed that n is divisible by 3. It is guaranteed that each pair ai, bi occurs in the input at most once.", "output_spec": "If the required division into teams doesn't exist, print number -1. Otherwise, print lines. In each line print three integers xi, yi, zi (1 ≤ xi, yi, zi ≤ n) — the i-th team. If there are multiple answers, you are allowed to print any of them.", "sample_inputs": ["3 0", "6 4\n1 2\n2 3\n3 4\n5 6", "3 3\n1 2\n2 3\n1 3"], "sample_outputs": ["3 2 1", "-1", "3 2 1"], "notes": null}, "positive_code": [{"source_code": "n, m=gets.split.map(&:to_i)\nc=n/3\ns=[nil]*(n+1)\nt=1\nm.times do\n a, b=gets.split.map(&:to_i)\n if s[a].nil?&&s[b].nil?\n if t > c\n puts '-1'\n exit\n end\n s[a]=s[b]=t\n t+=1\n elsif s[a].nil?\n s[a]=s[b]\n elsif s[b].nil?\n s[b]=s[a]\n elsif s[a]!=s[b]\n puts '-1'\n exit\n end\nend\nh=Hash.new { |h, k| h[k]=[] }\ns.each_with_index { |a, t| h[a]<3}\n puts '-1'\n exit\nend\nf=s.each_index.select { |i| !s[i] }\nf.shift\nfor t in 1..c do\n r=h[t]\n n=r.size\n puts (r | f.pop(3-n)).join ' '\nend\n"}, {"source_code": "n, m=gets.split.map(&:to_i)\nc=n/3\ns=[nil]*(n+1)\nt=1\nm.times do\n a, b=gets.split.map(&:to_i)\n if s[a].nil?&&s[b].nil?\n if t > c\n puts '-1'\n exit\n end\n s[a]=s[b]=t\n t+=1\n elsif s[a].nil?\n s[a]=s[b]\n elsif s[b].nil?\n s[b]=s[a]\n elsif s[a]!=s[b]\n puts '-1'\n exit\n end\nend\nh=Hash.new { |h, k| h[k]=[] }\ns.each_with_index { |a, t| h[a]<3}\n puts '-1'\n exit\nend\nf=s.each_index.select { |i| !s[i] }\nf.shift\nfor t in 1..c do\n r=h[t]\n n=r.size\n puts (r | f.pop(3-n)).join ' '\nend\n"}, {"source_code": "n, m=gets.split.map(&:to_i)\nc=n/3\ns=[nil]*(n+1)\nt=1\nm.times do\n a, b=gets.split.map(&:to_i)\n if s[a].nil?&&s[b].nil?\n if t > c\n puts '-1'\n exit\n end\n s[a]=s[b]=t\n t+=1\n elsif s[a].nil?\n s[a]=s[b]\n elsif s[b].nil?\n s[b]=s[a]\n elsif s[a]!=s[b]\n puts '-1'\n exit\n end\nend\nh=Hash.new { |h, k| h[k]=[] }\ns.each_with_index { |a, t| h[a]<3}\n puts '-1'\n exit\nend\nf=s.each_index.select { |i| !s[i] }\nf.shift\nfor t in 1..c do\n r=h[t]\n n=r.size\n puts (r | f.pop(3-n)).join ' '\nend"}, {"source_code": "def dfs(now, parent, group)\n group.push now\n $vis[now] = true\n $path[now].each do |to|\n next if to == parent or $vis[to]\n dfs(to, now, group)\n end\n return group\nend\nn, m = gets.split(' ').map { |i| i.to_i }\n$vis = Array.new(50, false)\n$path = Array.new(50){Array.new}\ngroups = []\ncan = true\nm.times do \n x, y = gets.split(' ').map { |i| i.to_i}\n $path[x].push y\n $path[y].push x\nend\nn.times do |i|\n next if $vis[i+1]\n group = dfs(i+1, -1, [])\n can = false if group.size > 3\n $vis[i+1] = false if group.size == 1\n groups.push group unless group.size == 1\nend\nif can\n groups.each do |g|\n next if g.size >= 3\n while g.size != 3\n find = false\n for i in 1..n\n next if $vis[i]\n g.push i\n $vis[i] = true\n find = true\n break\n end\n if not find\n can = false\n break\n end\n end\n end\n group = []\n for i in 1..n\n next if $vis[i]\n group.push i\n if group.size == 3\n groups.push group\n group = []\n end\n end\nend\nif can\n groups.each do |g|\n puts g.join(' ')\n end\nelse\n puts -1\nend\n"}, {"source_code": "n, m=gets.split.map(&:to_i)\nc=n/3\ns=[nil]*(n+1)\nt=1\nm.times do\n a, b=gets.split.map(&:to_i)\n if s[a].nil?&&s[b].nil?\n if t > c\n puts '-1'\n exit\n end\n s[a]=s[b]=t\n t+=1\n elsif s[a].nil?\n s[a]=s[b]\n elsif s[b].nil?\n s[b]=s[a]\n elsif s[a]!=s[b]\n puts '-1'\n exit\n end\nend\nh=Hash.new { |h, k| h[k]=[] }\ns.each_with_index { |a, t| h[a]<3}\n puts '-1'\n exit\nend\nf=s.each_index.select { |i| !s[i] }\nf.shift\nfor t in 1..c do\n r=h[t]\n n=r.size\n puts (r | f.pop(3-n)).join ' '\nend\n"}, {"source_code": "n, m=gets.split.map(&:to_i)\nc=n/3\ns=[nil]*(n+1)\nt=1\nm.times do\n a, b=gets.split.map(&:to_i)\n if s[a].nil?&&s[b].nil?\n if t > c\n puts '-1'\n exit\n end\n s[a]=s[b]=t\n t+=1\n elsif s[a].nil?\n s[a]=s[b]\n elsif s[b].nil?\n s[b]=s[a]\n elsif s[a]!=s[b]\n puts '-1'\n exit\n end\nend\nh=Hash.new { |h, k| h[k]=[] }\ns.each_with_index { |a, t| h[a]<3}\n puts '-1'\n exit\nend\nf=s.each_index.select { |i| !s[i] }\nf.shift\nfor t in 1..c do\n r=h[t]\n n=r.size\n puts (r | f.pop(3-n)).join ' '\nend\n"}, {"source_code": "n, m=gets.split.map(&:to_i)\nc=n/3\ns=[nil]*(n+1)\nt=1\nm.times do\n a, b=gets.split.map(&:to_i)\n if s[a].nil?&&s[b].nil?\n if t > c\n puts '-1'\n exit\n end\n s[a]=s[b]=t\n t+=1\n elsif s[a].nil?\n s[a]=s[b]\n elsif s[b].nil?\n s[b]=s[a]\n elsif s[a]!=s[b]\n puts '-1'\n exit\n end\nend\nh=Hash.new { |h, k| h[k]=[] }\ns.each_with_index { |a, t| h[a]<3}\n puts '-1'\n exit\nend\nf=s.each_index.select { |i| !s[i] }\nf.shift\nfor t in 1..c do\n r=h[t]\n n=r.size\n puts (r | f.pop(3-n)).join ' '\nend"}, {"source_code": "n, m=gets.split.map(&:to_i)\nc=n/3\ns=[nil]*(n+1)\nt=1\nm.times do\n a, b=gets.split.map(&:to_i)\n if s[a].nil?&&s[b].nil?\n if t > c\n puts '-1'\n exit\n end\n s[a]=s[b]=t\n t+=1\n elsif s[a].nil?\n s[a]=s[b]\n elsif s[b].nil?\n s[b]=s[a]\n elsif s[a]!=s[b]\n puts '-1'\n exit\n end\nend\nh=Hash.new { |h, k| h[k]=[] }\ns.each_with_index { |a, t| h[a]<3}\n puts '-1'\n exit\nend\nf=s.each_index.select { |i| !s[i] }\nf.shift\nfor t in 1..c do\n r=h[t]\n n=r.size\n puts (r | f.pop(3-n)).join ' '\nend\n"}, {"source_code": "n, m=gets.split.map(&:to_i)\nc=n/3\ns=[nil]*(n+1)\nt=1\nm.times do\n a, b=gets.split.map(&:to_i)\n if s[a].nil?&&s[b].nil?\n if t > c\n puts '-1'\n exit\n end\n s[a]=s[b]=t\n t+=1\n elsif s[a].nil?\n s[a]=s[b]\n elsif s[b].nil?\n s[b]=s[a]\n elsif s[a]!=s[b]\n puts '-1'\n exit\n end\nend\nh=Hash.new { |h, k| h[k]=[] }\ns.each_with_index { |a, t| h[a]<3}\n puts '-1'\n exit\nend\nf=s.each_index.select { |i| !s[i] }\nf.shift\nfor t in 1..c do\n r=h[t]\n n=r.size\n puts (r | f.pop(3-n)).join ' '\nend"}, {"source_code": "n, m=gets.split.map(&:to_i)\nc=n/3\ns=[nil]*(n+1)\nt=1\nm.times do\n a, b=gets.split.map(&:to_i)\n if s[a].nil?&&s[b].nil?\n if t > c\n puts '-1'\n exit\n end\n s[a]=s[b]=t\n t+=1\n elsif s[a].nil?\n s[a]=s[b]\n elsif s[b].nil?\n s[b]=s[a]\n elsif s[a]!=s[b]\n puts '-1'\n exit\n end\nend\nh=Hash.new { |h, k| h[k]=[] }\ns.each_with_index { |a, t| h[a]<3}\n puts '-1'\n exit\nend\nf=s.each_index.select { |i| !s[i] }\nf.shift\nfor t in 1..c do\n r=h[t]\n n=r.size\n puts (r | f.pop(3-n)).join ' '\nend\n"}, {"source_code": "n, m=gets.split.map(&:to_i)\nc=n/3\ns=[nil]*(n+1)\nt=1\nm.times do\n a, b=gets.split.map(&:to_i)\n if s[a].nil?&&s[b].nil?\n if t > c\n puts '-1'\n exit\n end\n s[a]=s[b]=t\n t+=1\n elsif s[a].nil?\n s[a]=s[b]\n elsif s[b].nil?\n s[b]=s[a]\n elsif s[a]!=s[b]\n puts '-1'\n exit\n end\nend\nh=Hash.new { |h, k| h[k]=[] }\ns.each_with_index { |a, t| h[a]<3}\n puts '-1'\n exit\nend\nf=s.each_index.select { |i| !s[i] }\nf.shift\nfor t in 1..c do\n r=h[t]\n n=r.size\n puts (r | f.pop(3-n)).join ' '\nend\n"}, {"source_code": "n, m=gets.split.map(&:to_i)\nc=n/3\ns=[nil]*(n+1)\nt=1\nm.times do\n a, b=gets.split.map(&:to_i)\n if s[a].nil?&&s[b].nil?\n if t > c\n puts '-1'\n exit\n end\n s[a]=s[b]=t\n t+=1\n elsif s[a].nil?\n s[a]=s[b]\n elsif s[b].nil?\n s[b]=s[a]\n elsif s[a]!=s[b]\n puts '-1'\n exit\n end\nend\nh=Hash.new { |h, k| h[k]=[] }\ns.each_with_index { |a, t| h[a]<3}\n puts '-1'\n exit\nend\nf=s.each_index.select { |i| !s[i] }\nf.shift\nfor t in 1..c do\n r=h[t]\n n=r.size\n puts (r | f.pop(3-n)).join ' '\nend\n"}], "negative_code": [{"source_code": "def dfs(now, parent, group)\n group.push now\n $vis[now] = true\n $path[now].each do |to|\n next if to == parent or $vis[to]\n dfs(to, now, group)\n end\n return group\nend\nn, m = gets.split(' ').map { |i| i.to_i }\n$vis = Array.new(50, false)\n$path = Array.new(50){Array.new}\ngroups = []\ncan = true\nm.times do \n x, y = gets.split(' ').map { |i| i.to_i}\n $path[x].push y\n $path[y].push x\nend\nn.times do |i|\n next if $vis[i+1]\n group = dfs(i+1, -1, [])\n can = false if group.size > 3\n while group.size < 3\n find = false\n for i in 1..n\n next if $vis[i]\n if $path[i].size == 0\n group.push i\n $vis[i] = true\n find = true\n break\n end\n end\n if not find\n can = false\n break \n end\n end\n groups.push group\nend\nif can\n groups.each do |g|\n puts g.join(' ')\n end\nelse\n puts -1\nend"}, {"source_code": "def dfs(now, parent, group)\n group.push now\n $vis[now] = true\n $path[now].each do |to|\n next if to == parent or $vis[to]\n dfs(to, now, group)\n end\n return group\nend\nn, m = gets.split(' ').map { |i| i.to_i }\n$vis = Array.new(50, false)\n$path = Array.new(50){Array.new}\ngroups = []\ncan = true\nm.times do \n x, y = gets.split(' ').map { |i| i.to_i}\n $path[x].push y\n $path[y].push x\nend\nn.times do |i|\n next if $vis[i+1]\n group = dfs(i+1, -1, [])\n can = false if group.size > 3\n while group.size < 3\n find = false\n for i in 1..n\n next if $vis[i]\n if $path[i].size == 0\n group.push i\n $vis[i] = true\n find = true\n break\n end\n end\n break unless find\n end\n groups.push group\nend\nif can\n groups.each do |g|\n puts g.join(' ')\n end\nelse\n puts -1\nend"}, {"source_code": "n,m=gets.split.map(&:to_i)\ns=[nil]*(n+1)\nt=1\nm.times do\n a,b=gets.split.map(&:to_i)\n if s[a].nil?&&s[b].nil?\n s[a]=s[b]=t\n t+=1\n elsif s[a].nil?\n s[a]=s[b]\n elsif s[b].nil?\n s[b]=s[a]\n elsif s[a]!=s[b]\n puts'-1'\n exit\n end\nend\nh=Hash.new{|h,k|h[k]=[]}\ns.each_with_index{|a,t|h[a]<3\n puts'-1'\n exit\n else\n puts (r | f.pop(3-n)).join' '\n end\nend"}, {"source_code": "n,m=gets.split.map(&:to_i)\ns=[nil]*(n+1)\nt=1\nm.times do\n a,b=gets.split.map(&:to_i)\n if s[a]==nil&&s[b]==nil\n s[a]=s[b]=t\n t+=1\n elsif s[a]==nil\n s[a]=s[b]\n else\n s[b]=s[a]\n end\nend\nh=Hash.new{|h,k|h[k]=[]}\ns.each_with_index{|a,t|h[a]<3\n puts'-1'\n exit\n else\n puts (r | f.pop(3-n)).join' '\n end\nend"}, {"source_code": "n, m=gets.split.map(&:to_i)\nc=n/3\ns=[nil]*(n+1)\nt=1\nm.times do\n a, b=gets.split.map(&:to_i)\n if s[a].nil?&&s[b].nil?\n if t > c\n puts '-1'\n exit\n end\n s[a]=s[b]=t\n t+=1\n elsif s[a].nil?\n s[a]=s[b]\n elsif s[b].nil?\n s[b]=s[a]\n elsif s[a]!=s[b]\n puts '-1'\n exit\n end\nend\nh=Hash.new { |h, k| h[k]=[] }\ns.each_with_index { |a, t| h[a]<3\n puts '-1'\n exit\n else\n puts (r | f.pop(3-n)).join ' '\n end\nend"}, {"source_code": "n,m=gets.split.map(&:to_i)\ns=[nil]*(n+1)\nt=1\nm.times do\n a,b=gets.split.map(&:to_i)\n if s[a]==nil&&s[b]==nil\n s[a]=s[b]=t\n t+=1\n elsif s[a]==nil\n s[a]=s[b]\n else\n s[b]=s[a]\n end\nend\nh=Hash.new([])\ns.each_with_index{|a,t|h[a]<3\n puts'-1'\n exit\n else\n puts (r | f.pop(3-n)).join' '\n end\nend"}, {"source_code": "n, m=gets.split.map(&:to_i)\ns=[nil]*(n+1)\nt=1\nm.times do\n a, b=gets.split.map(&:to_i)\n if s[a].nil?&&s[b].nil?\n if t > n/3\n puts '-1'\n exit\n end\n s[a]=s[b]=t\n t+=1\n elsif s[a].nil?\n s[a]=s[b]\n elsif s[b].nil?\n s[b]=s[a]\n elsif s[a]!=s[b]\n puts '-1'\n exit\n end\nend\nh=Hash.new { |h, k| h[k]=[] }\ns.each_with_index { |a, t| h[a]<3\n puts '-1'\n exit\n else\n puts (r | f.pop(3-n)).join ' '\n end\nend"}, {"source_code": "n,m=gets.split.map(&:to_i)\ns=[nil]*(n+1)\nt=1\nm.times do\n a,b=gets.split.map(&:to_i)\n if s[a].nil?&&s[b].nil?\n s[a]=s[b]=t\n t+=1\n elsif s[a].nil?\n s[a]=s[b]\n elsif s[b].nil?\n s[b]=s[a]\n else\n puts'-1'\n exit\n end\nend\nh=Hash.new{|h,k|h[k]=[]}\ns.each_with_index{|a,t|h[a]<3\n puts'-1'\n exit\n else\n puts (r | f.pop(3-n)).join' '\n end\nend"}], "src_uid": "46e7cd6723553d2c6d6c6d0999a5b5fc"} {"nl": {"description": "Sherlock has a new girlfriend (so unlike him!). Valentine's day is coming and he wants to gift her some jewelry.He bought n pieces of jewelry. The i-th piece has price equal to i + 1, that is, the prices of the jewelry are 2, 3, 4, ... n + 1.Watson gave Sherlock a challenge to color these jewelry pieces such that two pieces don't have the same color if the price of one piece is a prime divisor of the price of the other piece. Also, Watson asked him to minimize the number of different colors used.Help Sherlock complete this trivial task.", "input_spec": "The only line contains single integer n (1 ≤ n ≤ 100000) — the number of jewelry pieces.", "output_spec": "The first line of output should contain a single integer k, the minimum number of colors that can be used to color the pieces of jewelry with the given constraints. The next line should consist of n space-separated integers (between 1 and k) that specify the color of each piece in the order of increasing price. If there are multiple ways to color the pieces using k colors, you can output any of them.", "sample_inputs": ["3", "4"], "sample_outputs": ["2\n1 1 2", "2\n2 1 1 2"], "notes": "NoteIn the first input, the colors for first, second and third pieces of jewelry having respective prices 2, 3 and 4 are 1, 1 and 2 respectively.In this case, as 2 is a prime divisor of 4, colors of jewelry having prices 2 and 4 must be distinct."}, "positive_code": [{"source_code": "#!/usr/bin/ruby\nrequire 'prime'\nn=gets.to_i\np n<3?1:2\nputs (2..n+1).map{|e|e.prime? ? 1 : 2}*' '"}, {"source_code": "def is_prime(n=2)\n\tif n < 2\n\t \treturn 0\n\telsif n==2\n\t \treturn 1\n\telsif n%2==0\n\t \treturn 0\n\telse\n\t\tj=3\n\t\twhile j*j<=n\n\t\t\tif n % j == 0\n\t\t\t\treturn 0\n\t\t\tend\n\t\t\tj+=2;\n\t\tend\n\t\treturn 1\n\tend\nend\nn=gets.chomp.to_i\n#puts n\ncolor=Array.new(n+2,1)\n#puts color.join(' ')\nnumber=1\ni=2\nmax=1\nwhile i<=n+1\n\tif is_prime(i)\n\t\tj=2\n\t\twhile i*j <= n+1\n\t\t\tcolor[i*j]=2\n\t\t\tmax=color[i*j] if color[i*j] > max\n\t\t\tj+=1\n\t\tend\n\tend\n\ti+=1\nend\n#puts color.join(' ')\nputs max\ni=2\nwhile i<=n+1\n\tprint \" #{color[i]}\"\n\ti+=1\nend\nputs\n"}, {"source_code": "n = gets.to_i + 1\ncolors = Array.new(n + 1)\n2.upto(n) do |i|\n next if colors[i]\n colors[i] = 1\n (2 * i).step(n, i){|j| colors[j] = 2}\nend\ncolors.shift(2)\np colors.max\nputs colors.join(' ')\n"}, {"source_code": "n = gets.to_i\n\na = Array.new\nmax = 1\n(2..n+1).each do |i|\n (1..n+1).each do |j|\n break if i * j > n+1\n next if a[j].to_i > 1\n next if a[i * j] == 2\n a[i * j] = a[j].to_i == 1 ? 2 : 1\n max = [a[i * j], max].max\n end\nend\n\nputs max\nputs a[2, a.length].join(' ')\n"}], "negative_code": [{"source_code": "#!/usr/bin/ruby\nrequire 'prime'\nn=gets.to_i\np n<2?1:2\nputs (1..n).map{|e|e.prime? ? 1 : 2}*' '"}, {"source_code": "n=gets.chomp.to_i\n#puts n\ncolor=Array.new(n+2,1)\n#puts color.join(' ')\nnumber=1\ni=2\nmax=1\nwhile i<=n+1\n\tj=2\n\twhile i*j <= n+1\n\t\tcolor[i*j]+=1\n\t\tmax=color[i*j] if color[i*j] > max\n\t\tj+=1\n\tend\n\ti+=1\nend\n#puts color.join(' ')\nputs max\ni=2\nwhile i<=n+1\n\tprint \" #{color[i]}\"\n\ti+=1\nend\nputs\n"}, {"source_code": "n=gets.chomp.to_i\n#puts n\ncolor=Array.new(n+2,0)\n#puts color.join(' ')\nnumber=1\ni=2\nmax=0\nwhile i<=n+1\n\tif color[i]==0\n\t\tcolor[i]=number\n\t\tj=2\n\t\twhile i*j <= n+1\n\t\t\tif color[i*j]==0\n\t\t\t\tcolor[i*j]=number+1\n\t\t\t\tmax=number+1 if number + 1 > max\n\t\t\telse\n\t\t\t\tcolor[i*j]=number+2\n\t\t\t\tmax=number+2 if number + 1 > max\n\t\t\t\tnumber+=1;\n\t\t\tend\n\t\t\tj+=1\n\t\tend\n\tend\n\ti+=1\nend\n#puts color.join(' ')\nputs max\ni=2\nwhile i<=n+1\n\tprint \" #{color[i]}\"\n\ti+=1\nend\nputs\n"}, {"source_code": "n = gets.to_i + 1\ncolors = Array.new(n)\n2.upto(n) do |i|\n i.step(n, i).with_index{|j, c| colors[j] ||= c + 1}\nend\ncolors.compact!\np colors.uniq.size\nputs colors.join(' ')\n"}, {"source_code": "n = gets.to_i + 1\ncolors = Array.new(n + 1, 1)\n2.upto(n) do |i|\n c = colors[i] + 1\n (2 * i).step(n, i) do |j|\n colors[j] = [colors[j], c].max\n end\nend\ncolors.shift(2)\np colors.max\nputs colors.join(' ')\n"}, {"source_code": "n = gets.to_i\n\na = Array.new\nmax = 1\n(2..n+1).each do |i|\n (1..n+1).each do |j|\n break if i * j > n+1\n next if a[i * j].to_i > a[j].to_i\n a[i * j] = [a[j].to_i, a[i * j].to_i].max + 1\n max = [a[i * j], max].max\n end\nend\n\nputs max\nputs a[2, a.length].join(' ')\n"}], "src_uid": "a99f5e08f3f560488ff979a3e9746e7f"} {"nl": {"description": "Treeland consists of $$$n$$$ cities and $$$n-1$$$ roads. Each road is bidirectional and connects two distinct cities. From any city you can get to any other city by roads. Yes, you are right — the country's topology is an undirected tree.There are some private road companies in Treeland. The government decided to sell roads to the companies. Each road will belong to one company and a company can own multiple roads.The government is afraid to look unfair. They think that people in a city can consider them unfair if there is one company which owns two or more roads entering the city. The government wants to make such privatization that the number of such cities doesn't exceed $$$k$$$ and the number of companies taking part in the privatization is minimal.Choose the number of companies $$$r$$$ such that it is possible to assign each road to one company in such a way that the number of cities that have two or more roads of one company is at most $$$k$$$. In other words, if for a city all the roads belong to the different companies then the city is good. Your task is to find the minimal $$$r$$$ that there is such assignment to companies from $$$1$$$ to $$$r$$$ that the number of cities which are not good doesn't exceed $$$k$$$. The picture illustrates the first example ($$$n=6, k=2$$$). The answer contains $$$r=2$$$ companies. Numbers on the edges denote edge indices. Edge colors mean companies: red corresponds to the first company, blue corresponds to the second company. The gray vertex (number $$$3$$$) is not good. The number of such vertices (just one) doesn't exceed $$$k=2$$$. It is impossible to have at most $$$k=2$$$ not good cities in case of one company. ", "input_spec": "The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 \\le n \\le 200000, 0 \\le k \\le n - 1$$$) — the number of cities and the maximal number of cities which can have two or more roads belonging to one company. The following $$$n-1$$$ lines contain roads, one road per line. Each line contains a pair of integers $$$x_i$$$, $$$y_i$$$ ($$$1 \\le x_i, y_i \\le n$$$), where $$$x_i$$$, $$$y_i$$$ are cities connected with the $$$i$$$-th road.", "output_spec": "In the first line print the required $$$r$$$ ($$$1 \\le r \\le n - 1$$$). In the second line print $$$n-1$$$ numbers $$$c_1, c_2, \\dots, c_{n-1}$$$ ($$$1 \\le c_i \\le r$$$), where $$$c_i$$$ is the company to own the $$$i$$$-th road. If there are multiple answers, print any of them.", "sample_inputs": ["6 2\n1 4\n4 3\n3 5\n3 6\n5 2", "4 2\n3 1\n1 4\n1 2", "10 2\n10 3\n1 2\n1 3\n1 4\n2 5\n2 6\n2 7\n3 8\n3 9"], "sample_outputs": ["2\n1 2 1 1 2", "1\n1 1 1", "3\n1 1 2 3 2 3 1 3 1"], "notes": null}, "positive_code": [{"source_code": "N,K = gets.split.map &:to_i\nes = Hash[(1..N).map{ |n| [n,{}] }]\n(1..N-1).each{ |i|\n a,b = gets.split.map &:to_i\n es[a][b] = es[b][a] = i-1\n}\nR = (1..N).map{ |n| es[n].size }.sort[-K-1]\ncolors = Array.new(N-1,nil)\n\nqueue = [[nil,1]]\n\nwhile cn = queue.pop do\n c,n = cn\n c = 0 if !c\n es[n].each{ |m,i|\n next if colors[i]\n c = (c + 1) % R\n colors[i] = c\n queue << [c,m]\n }\nend\n\np R\nputs colors.map{ |c| c + 1 } * \" \""}, {"source_code": "N,K = gets.split.map &:to_i\nes = Hash[(1..N).map{ |n| [n,{}] }]\n(1..N-1).each{ |i|\n a,b = gets.split.map &:to_i\n es[a][b] = es[b][a] = i-1\n}\nR = (1..N).map{ |n| es[n].size }.sort[-K-1]\ncolors = Array.new(N-1,nil)\n\nqueue = [[nil,1]]\n\nwhile cn = queue.pop do\n c,n = cn\n c = 0 if !c\n es[n].each{ |m,i|\n next if colors[i]\n c = (c + 1) % R\n colors[i] = c\n queue << [c,m]\n }\nend\n\np R\nputs colors.map{ |c| c + 1 } * \" \""}], "negative_code": [], "src_uid": "0cc73612bcfcd3be142064e2da9e92e3"} {"nl": {"description": "Mihai has an $$$8 \\times 8$$$ chessboard whose rows are numbered from $$$1$$$ to $$$8$$$ from top to bottom and whose columns are numbered from $$$1$$$ to $$$8$$$ from left to right.Mihai has placed exactly one bishop on the chessboard. The bishop is not placed on the edges of the board. (In other words, the row and column of the bishop are between $$$2$$$ and $$$7$$$, inclusive.)The bishop attacks in all directions diagonally, and there is no limit to the distance which the bishop can attack. Note that the cell on which the bishop is placed is also considered attacked. An example of a bishop on a chessboard. The squares it attacks are marked in red. Mihai has marked all squares the bishop attacks, but forgot where the bishop was! Help Mihai find the position of the bishop.", "input_spec": "The first line of the input contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 36$$$) — the number of test cases. The description of test cases follows. There is an empty line before each test case. Each test case consists of $$$8$$$ lines, each containing $$$8$$$ characters. Each of these characters is either '#' or '.', denoting a square under attack and a square not under attack, respectively.", "output_spec": "For each test case, output two integers $$$r$$$ and $$$c$$$ ($$$2 \\leq r, c \\leq 7$$$) — the row and column of the bishop. The input is generated in such a way that there is always exactly one possible location of the bishop that is not on the edge of the board.", "sample_inputs": ["3\n\n\n\n\n.....#..\n\n#...#...\n\n.#.#....\n\n..#.....\n\n.#.#....\n\n#...#...\n\n.....#..\n\n......#.\n\n\n\n\n#.#.....\n\n.#......\n\n#.#.....\n\n...#....\n\n....#...\n\n.....#..\n\n......#.\n\n.......#\n\n\n\n\n.#.....#\n\n..#...#.\n\n...#.#..\n\n....#...\n\n...#.#..\n\n..#...#.\n\n.#.....#\n\n#......."], "sample_outputs": ["4 3\n2 2\n4 5"], "notes": "NoteThe first test case is pictured in the statement. Since the bishop lies in the intersection row $$$4$$$ and column $$$3$$$, the correct output is 4 3."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n gets\r\n xxs = 8.times.map { gets.chomp.chars }\r\n r, c = (1..6).flat_map do |i|\r\n (1..6).filter_map do |j|\r\n if xxs[i - 1][j - 1] == \"#\" && xxs[i - 1][j + 1] == \"#\" && xxs[i + 1][j - 1] == \"#\" && xxs[i + 1][j + 1] == \"#\"\r\n [i + 1, j + 1]\r\n end\r\n end\r\n end.first\r\n puts \"#{r} #{c}\"\r\nend\r\n"}, {"source_code": "n = gets.to_i\n\nn.times do\n gets\n\n r = -1\n\n m = Array.new(8)\n\n 8.times do |i|\n line = gets\n\n m[i] = line.chars\n end\n\n 8.times do |i|\n 8.times do |j|\n next unless m[i][j] == \"#\"\n next unless i - 1 > -1 && j - 1 > -1 && m[i - 1][j - 1] == \"#\"\n next unless i + 1 < 8 && j + 1 < 8 && m[i + 1][j + 1] == \"#\"\n next unless i - 1 > -1 && j + 1 < 8 && m[i - 1][j + 1] == \"#\"\n next unless i + 1 > -1 && j - 1 > -1 && m[i + 1][j - 1] == \"#\"\n\n puts [i, j].map(&:succ).map(&:to_s).join(\" \")\n end\n end\nend\n"}, {"source_code": "$cnt = []\r\n\r\ndef solve\r\n v = Array.new(8){Array.new(8)}\r\n t = gets.split\r\n i = 0\r\n while i < 8\r\n s = gets.split('')\r\n j = 0\r\n s.each do |x|\r\n v[i][j] = x\r\n j = j + 1\r\n end\r\n i = i + 1\r\n end\r\n i = 1\r\n while i < 7\r\n j = 1\r\n while j < 7\r\n if v[i - 1][j - 1] == '#' and v[i - 1][j + 1] == '#' and v[i + 1][j - 1] == '#' and v[i + 1][j + 1] == '#'\r\n ans = []\r\n ans.append(i + 1)\r\n ans.append(j + 1)\r\n puts ans * \" \"\r\n end\r\n j = j + 1\r\n end\r\n i = i + 1\r\n end\r\nend\r\n\r\nmaxn = 10005\r\nmaxn.times do\r\n $cnt.append(0)\r\nend\r\ngets.to_i.times do\r\n solve\r\nend"}, {"source_code": "gets.to_i.times do\r\n gets\r\n s = Array.new(8){gets.chomp.chars}\r\n gx, gy = 0, 0\r\n dx = [-1, -1, 1, 1]\r\n dy = [-1, 1, -1, 1]\r\n 8.times do |i|\r\n 8.times do |j|\r\n cnt = 0\r\n 4.times do |k|\r\n x = j+dx[k]\r\n y = i+dy[k]\r\n if 0 <= x && x < 8 && 0 <= y && y < 8\r\n cnt += 1 if s[y][x] == '#'\r\n end\r\n end\r\n if cnt == 4\r\n gx = j+1\r\n gy = i+1\r\n end\r\n end\r\n end\r\n puts \"#{gy} #{gx}\"\r\nend\r\n"}, {"source_code": "gets.to_i.times do\n a = []\n i = 0\n while i < 8\n x = gets.chomp\n if x[0] == '.' || x[0] == '#'\n a << x\n i += 1\n end\n end\n x = 0\n y = 0\n for i in 1...7\n for j in 1...7\n if a[i][j] == '#' && a[i - 1][j - 1] == '#' && a[i + 1][j - 1] == '#' && a[i - 1][j + 1] == '#' && a[i + 1][j + 1] == '#'\n x = i + 1\n y = j + 1\n end\n end\n end\n puts \"#{x} #{y}\"\nend\n"}, {"source_code": "main = -> {\r\n t = int\r\n t.times do\r\n string\r\n grid = rep(8) { string }\r\n (2 .. 7).any? do |i|\r\n (2 .. 7).any? do |j|\r\n if grid[i - 2][j - 2] == ?# && grid[i - 2][j] == ?# && grid[i][j - 2] == ?# && grid[i][j] == ?#\r\n puts \"#{i} #{j}\"\r\n true\r\n else\r\n false\r\n end\r\n end\r\n end\r\n end\r\n}\r\n\r\nDEBUG = false\r\nMOD = 10**9+7\r\nINF = 10**18\r\n$yesno = %w(No Yes)\r\n$YesNo = %w(No Yes)\r\n$YESNO = %w(NO YES)\r\n\r\nrequire \"set\"\r\nrequire \"prime\"\r\n\r\ndef int; gets.to_s.to_i end\r\ndef ints; gets.to_s.split.map { |s| s.to_i } end\r\ndef int1s; gets.to_s.split.map { |s| s.to_i - 1 } end\r\ndef float; gets.to_s.to_f end\r\ndef floats; gets.to_s.split.map { |s| s.to_f } end\r\ndef array_of(&convert); gets.to_s.split.map(&convert) end\r\ndef string; gets.to_s.chomp end\r\ndef strings; gets.to_s.split end\r\ndef rep(n, &b); Array.new(n, &b) end\r\ndef yes; puts $yesno[1] end\r\ndef no; puts $yesno[0] end\r\ndef yesno t; puts $yesno[t ? 1 : 0] end\r\ndef YesNo t; puts $YesNo[t ? 1 : 0] end\r\ndef YESNO t; puts $YESNO[t ? 1 : 0] end\r\ndef zip(xs, *yss); Enumerator.new { |y| xs.zip(*yss) { |a| y.yield(*a) } } end\r\ndef max(*xs, &block); block_given? ? xs.max_by(&block) : xs.max end\r\ndef min(*xs, &block); block_given? ? xs.min_by(&block) : xs.min end\r\ndef minmax(*xs, &block); block_given? ? xs.minmax_by(&block) : xs.minmax end\r\ndef gcd(*xs); xs.inject(0, :gcd) end\r\ndef matrix(h, w, fill=nil, &block); return Array.new(h) { Array.new(w, &block) } if block_given?; Array.new(h) { [fill] * w } end\r\ndef debug(x = nil); STDERR.puts (block_given? ? yield(x) : x).inspect if DEBUG; x end\r\ndef debug_grid(grid, width = 1); grid.each { |row| STDERR.puts row.map { |x| x.inspect.ljust(width) }.join(\"\") } if DEBUG; grid end\r\ndef if_debug; yield if DEBUG end\r\n\r\nmodule Boolean\r\n def coerce(other); [other, to_i] end\r\n def +@; to_i end\r\n def to_int; to_i end\r\n def *(other); to_i * other end\r\nend\r\n\r\nclass TrueClass\r\n include Boolean\r\n def to_i; 1 end\r\nend\r\n\r\nclass FalseClass\r\n include Boolean\r\n def to_i; 0 end\r\nend\r\n\r\nclass Integer\r\n def div_ceil(y); (self + y - 1) / y end\r\n def mod_inv(mod = MOD); pow(mod-2, mod) end\r\n def mod_div(y, mod = MOD); self * y.mod_inv(mod) % mod end\r\n def mod_nCr(r, mod = MOD); x = y = 1; (1..r).each { |i| x = x * (self + 1 - i) % mod; y = y * i % mod }; x.mod_div(y, mod); end\r\n def factorial(mod = MOD); (2..self).inject(1) { |f, x| f * x % mod } end\r\n def popcount; x = self; c = 0; while x > 0; c += 1 if x & 1 == 1; x >>= 1 end; c end #TODO: faster\r\n def bitbrute(&block); (1< 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\r\n def lower_bound; ac, wa = self.end_open, self.begin; while ac - wa > 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\r\n def shakutori(r2, &pred); Enumerator.new { |y| j, r = r2.begin, r2.end_open; each { |i| j += 1 while j + 1 < r and pred[i, j+1]; y.yield(i, j) } }; end\r\n def widest(&block); Enumerator.new { |y| j, n = self.begin, self.end_open; each { |i| j += 1 while j < n and block[i, j]; y.yield(i, j) if block[i, j] } } end\r\nend\r\n\r\nclass Array\r\n def power(&block); (0 ... 1 << size).each(&block) end\r\n def sorted_merge(other); a = []; i = j = 0; n, m = size, other.size; if j < m and other[j] < self[i]; a << other[j]; j += 1 else; a << self[i]; i += 1 end while i < n; a.push(*other[j..-1]) if j < m; a end\r\n def upper_bound; ac, wa = 0, size; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\r\n def lower_bound; ac, wa = size, 0; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\r\n def cum(*xs, &op); a = []; a << xs[0] if xs.size > 0; a << x = self[0]; (1...size).each { |i| a << x = op[x, self[i]] }; a end\r\n def cumdiff(range); self[range.end_open] - self[range.begin]; end\r\n def compress(kinds = uniq.sort!); map { |x| kinds.bsearch_index { _1 >= x } }; end\r\nend\r\n\r\nmodule Enumerable\r\n def sorted_uniq; x = nil; filter { |y| c = x === y; x = y; !c } end\r\n def cumsum; ys = [0]; each { |x| ys << x + ys[-1] }; ys end\r\nend\r\n\r\nmain[]"}], "negative_code": [{"source_code": "gets.to_i.times do\r\n gets\r\n xxs = 8.times.map { gets.chomp.chars }\r\n r, c = (0..7).flat_map do |i|\r\n (0..7).filter_map do |j|\r\n [i + 1, j + 1] if xxs[i][j] == \"#\" && xxs[i].count(\"#\") == 1 && xxs.map { |xs| xs[j] }.count(\"#\") == 1\r\n end\r\n end.first\r\n puts \"#{r} #{c}\"\r\nend\r\n"}], "src_uid": "b0f968ca75fbea2f11a7e4b9006f136e"} {"nl": {"description": "An integer array $$$a_1, a_2, \\ldots, a_n$$$ is being transformed into an array of lowercase English letters using the following prodecure:While there is at least one number in the array: Choose any number $$$x$$$ from the array $$$a$$$, and any letter of the English alphabet $$$y$$$. Replace all occurrences of number $$$x$$$ with the letter $$$y$$$. For example, if we initially had an array $$$a = [2, 3, 2, 4, 1]$$$, then we could transform it the following way: Choose the number $$$2$$$ and the letter c. After that $$$a = [c, 3, c, 4, 1]$$$. Choose the number $$$3$$$ and the letter a. After that $$$a = [c, a, c, 4, 1]$$$. Choose the number $$$4$$$ and the letter t. After that $$$a = [c, a, c, t, 1]$$$. Choose the number $$$1$$$ and the letter a. After that $$$a = [c, a, c, t, a]$$$. After the transformation all letters are united into a string, in our example we get the string \"cacta\".Having the array $$$a$$$ and the string $$$s$$$ determine if the string $$$s$$$ could be got from the array $$$a$$$ after the described transformation?", "input_spec": "The first line contains a single integer $$$t$$$ $$$(1 \\leq t \\leq 10^3$$$) — the number of test cases. Then the description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 50$$$) — the length of the array $$$a$$$ and the string $$$s$$$. The second line of each test case contains exactly $$$n$$$ integers: $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\leq a_i \\leq 50$$$) — the elements of the array $$$a$$$. The third line of each test case contains a string $$$s$$$ of length $$$n$$$, consisting of lowercase English letters. ", "output_spec": "For each test case, output \"YES\", if we can get the string $$$s$$$ from the array $$$a$$$, and \"NO\" otherwise. You can output each letter in any case.", "sample_inputs": ["7\n\n5\n\n2 3 2 4 1\n\ncacta\n\n1\n\n50\n\na\n\n2\n\n11 22\n\nab\n\n4\n\n1 2 2 1\n\naaab\n\n5\n\n1 2 3 2 1\n\naaaaa\n\n6\n\n1 10 2 9 3 8\n\nazzfdb\n\n7\n\n1 2 3 4 1 1 2\n\nabababb"], "sample_outputs": ["YES\nYES\nYES\nNO\nYES\nYES\nNO"], "notes": "NoteThe first test case corresponds to the sample described in the statement.In the second test case we can choose the number $$$50$$$ and the letter a.In the third test case we can choose the number $$$11$$$ and the letter a, after that $$$a = [a, 22]$$$. Then we choose the number $$$22$$$ and the letter b and get $$$a = [a, b]$$$.In the fifth test case we can change all numbers one by one to the letter a."}, "positive_code": [{"source_code": "# ○。.○。.○。.○。.○。.○。.○。.○。.○。.○。.○。.○。.○。.○。.○。.\r\ndef int(b = 0); gets.to_i + b end\r\ndef ints(b = 0); gets.split.map{ |x| x.to_i + b } end\r\ndef str; gets.chomp end\r\nOO = (1<<62)-(1<<31)\r\n# ○。.○。.○。.○。.○。.○。.○。.○。.○。.○。.○。.○。.○。.○。.○。.\r\n\r\ndef solve\r\n n = int\r\n a = ints\r\n s = str\r\n\r\n b = [-1] * n\r\n t = \"?\" * n\r\n n.times do |i|\r\n unless a[i] == nil\r\n # a[i] -> s[i]\r\n n.times do |j|\r\n if a[j] == a[i]\r\n return false if t[j] != '?' && t[j] != s[i]\r\n t[j] = s[i]\r\n end\r\n end\r\n end\r\n end\r\n\r\n s == t\r\nend\r\n\r\nt = int\r\nt.times do\r\n ans = solve\r\n puts ans ? \"YES\" : \"NO\"\r\nend"}], "negative_code": [], "src_uid": "485d5984e34a479f2c074a305ae999ae"} {"nl": {"description": "Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).q events are about to happen (in chronological order). They are of three types: Application x generates a notification (this new notification is unread). Thor reads all notifications generated so far by application x (he may re-read some notifications). Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation. Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.", "input_spec": "The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen. The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).", "output_spec": "Print the number of unread notifications after each event.", "sample_inputs": ["3 4\n1 3\n1 1\n1 2\n2 3", "4 6\n1 2\n1 4\n1 2\n3 3\n1 3\n1 3"], "sample_outputs": ["1\n2\n3\n2", "1\n2\n3\n0\n1\n2"], "notes": "NoteIn the first sample: Application 3 generates a notification (there is 1 unread notification). Application 1 generates a notification (there are 2 unread notifications). Application 2 generates a notification (there are 3 unread notifications). Thor reads the notification generated by application 3, there are 2 unread notifications left. In the second sample test: Application 2 generates a notification (there is 1 unread notification). Application 4 generates a notification (there are 2 unread notifications). Application 2 generates a notification (there are 3 unread notifications). Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left. Application 3 generates a notification (there is 1 unread notification). Application 3 generates a notification (there are 2 unread notifications). "}, "positive_code": [{"source_code": "input = gets.chomp.split\nn = Integer(input[0])\nq = Integer(input[1])\nevent = []\nfor i in 0...q\n event[i] = []\n input1 = gets.chomp.split\n event[i][0] = Integer(input1[0])\n event[i][1] = Integer(input1[1])\nend\n\nunread = 0\nnotification = []\nresult = []\nrecord = []\nfor i in 0...n\n record[i] = []\nend\nx = 0\nfor i in 0...q\n if event[i][0] == 1\n notification.push([event[i][1], 0])\n unread += 1 \n record[event[i][1] - 1].push(notification.length - 1) \n elsif event[i][0] == 2\n for j in record[event[i][1] - 1]\n if j >= x && notification[j][1] == 0\n notification[j][1] = 1\n unread -= 1 \n end\n end\n record[event[i][1] - 1] = []\n else\n if event[i][1] > x\n for j in x...event[i][1]\n if notification[j][1] == 0\n notification[j][1] = 1\n unread -= 1\n end\n end\n x = event[i][1] \n end\n end\n result.push(unread)\nend\n \nputs result "}, {"source_code": "#$stdin=File.open \"input.txt\",\"r\"\n#$stdout=File.open \"output.txt\",\"w\"\n\nnotifications=Array.new\nd=0\na=0\n\nn,q=gets.split.map{|x|x.to_i}\nunread=Array.new(n+1,0)\nread=Array.new(n+1,0)\n\nq.times do \n\n\tt,x=gets.split.map{|y|y.to_i}\n\n\tif t==1 then notifications.push x; a+=1; unread[x]+=1\n\telsif t==2 then \n\t\ta-=unread[x];read[x]+=unread[x];unread[x]=0\n\telsif t==3\n\t\tnotifications[0...x-d].each do |i|\n\t\t\tif read[i]>0 then read[i]-=1\n\t\t\telsif unread[i]>0 and a>0 then unread[i]-=1;a-=1\n\t\t\tend \n\t\t\tnotifications.shift;d+=1\n\t\tend if x-d>0\n\tend \n\n\tputs a \nend \n"}, {"source_code": "@n, @q = STDIN.gets.split(' ').collect{|x| x.to_i}\n@record = Hash.new{|hash,key| hash[key] = Array.new}\n@g, @decnt, @maxt, @count = Array.new, 0, 0, 0\n@q.times do \n\topt, x = STDIN.gets.split(' ').collect{|x| x.to_i}\n\tcase opt \n\t\twhen 1 then \n\t\t\t@g.push true\n\t\t\t@record[x].push(@count)\n\t\t\t@count += 1\n\t\twhen 2 then\n\t\t\t@record[x].each do |v|\n\t\t\t\tif @g[v] then \n\t\t\t\t\t@g[v] = false\n\t\t\t\t\t@decnt += 1\n\t\t\t\tend\n\t\t\tend\n\t\t\t@record[x].clear\n\t\twhen 3 then \n\t\t\tfor i in @maxt...[x, @count].min do \n\t\t\t\tif @g[i] then \n\t\t\t\t\t@g[i] = false\n\t\t\t\t\t@decnt += 1\n\t\t\t\tend\n\t\t\tend\n\t\t\t@maxt = [[x, @count].min, @maxt].max \n\tend\n\tputs @count-@decnt\nend\n\n"}], "negative_code": [{"source_code": "input = gets.chomp.split\nn = Integer(input[0])\nq = Integer(input[1])\nevent = []\nfor i in 0...q\n event[i] = []\n input1 = gets.chomp.split\n event[i][0] = Integer(input1[0])\n event[i][1] = Integer(input1[1])\nend\n\n=begin\ndef unread_count(a)\n unread_count = 0\n for i in 0...a.length\n if a[i][1] == 0\n unread_count += 1\n end\n end\n return unread_count\nend\n=end\n\nunread = 0\nnotification = []\nresult = []\nrecord = []\nfor i in 0...n\n record[i] = []\nend\nx = 0\nfor i in 0...q\n if event[i][0] == 1\n notification.push([event[i][1], 0])\n unread += 1 \n record[event[i][1] - 1].push(notification.length - 1) \n elsif event[i][0] == 2\n for j in record[event[i][1] - 1]\n if j > x && notification[j][1] == 0\n notification[j][1] = 1\n unread -= 1 \n end\n end\n else\n if event[i][1] > x\n for j in x...event[i][1]\n if notification[j][1] == 0\n notification[j][1] = 1\n unread -= 1\n end\n end\n x = event[i][1] \n end\n end\n result.push(unread)\nend\n \nputs result "}, {"source_code": "#$stdin=File.open \"input.txt\",\"r\"\nn,q=gets.split.map{|x|x.to_i}\nnotifications=Array.new\ncount=Array.new\nc=0\nq.times do \n\ta,b=gets.split.map{|x|x.to_i}\n\t\n\tif a==1\n\t\tnotifications[b]=0 if notifications[b]==nil\n\t\tnotifications[b]+=1\n\t\tcount[c]=b;\n\t\tc+=1\n\telsif a==2\n\t\tnotifications[b]=0\n\telse\n\t\tcount[0..b].each {|x| notifications[x]-=1 if notifications[x]>0}\n\tend\n\tsum=0\n\tnotifications.each {|x| sum+=x if x!=nil}\n\tputs sum\nend\n"}, {"source_code": "#$stdin=File.open \"input.txt\",\"r\"\nn,q=gets.split.map{|x|x.to_i}\nnotifications=Array.new\ncount=Array.new\nc=0\nq.times do \n\ta,b=gets.split.map{|x|x.to_i}\n\t\n\tif a==1\n\t\tnotifications[b]=0 if notifications[b]==nil\n\t\tnotifications[b]+=1\n\t\tcount[c]=b;\n\t\tc+=1\n\telsif a==2\n\t\tnotifications[b]=0\n\telse\n\t\tcount[0...b].each {|x| notifications[x]-=1 if notifications[x]>0}\n\tend\n\tsum=0\n\tnotifications.each {|x| sum+=x if x!=nil}\n\tputs sum\nend\n"}, {"source_code": "#$stdin=File.open \"input.txt\",\"r\"\n\nnotifications=Array.new\na=0\n\nn,q=gets.split.map{|x|x.to_i}\nunread=Array.new(n+1,0)\nread=Array.new(n+1,0)\n\nq.times do \n\n\tt,x=gets.split.map{|y|y.to_i}\n\n\tif t==1 then notifications.push x; a+=1; unread[x]+=1\n\telsif t==2 then \n\t\ta-=unread[x];read[x]+=unread[x];unread[x]=0\n\telsif t==3\n\t\tnotifications[0...x].each do |i|\n\t\t\tif read[i]!=0 then read[i]-=1\n\t\t\telse unread[i]-=1;a-=1 \n\t\t\tend \n#print notifications, \"\\n\"\n#print read,\"\\n\"\n#print unread, \"\\n\"\n\t\tend \n\tend \n#\tprint read,\"\\n\"\n#\tprint unread,\"\\n\"\n\tputs a \nend \n"}, {"source_code": "#$stdin=File.open \"input.txt\",\"r\"\n\nnotifications=Array.new\nread=Array.new\n\nn,q=gets.split.map{|x|x.to_i}\n\nq.times do \n\n\tt,x=gets.split.map{|y|y.to_i}\n\n\tnotifications.push x if t==1\n\tif t==2\n\t\tnotifications.each do |y|\n\t\t\tind=notifications.index y\n\t\t\tread[ind]=y if y==x\n\t\tend \n\telsif t==3\n\t\tnotifications[0...x].each do |y|\n\t\t\tind = notifications.index y\n\t\t\tread[ind]=y \n\t\tend \n\tend \n\tputs (notifications-read).length\nend \n"}, {"source_code": "#$stdin=File.open \"input.txt\",\"r\"\n\nnotifications=Array.new\na=0\n\nn,q=gets.split.map{|x|x.to_i}\nunread=Array.new(n+1,0)\nread=Array.new(n+1,0)\n\nq.times do \n\n\tt,x=gets.split.map{|y|y.to_i}\n\n\tif t==1 then notifications.push x; a+=1; unread[x]+=1\n\telsif t==2 then \n\t\ta-=unread[x];read[x]+=unread[x];unread[x]=0\n\telsif t==3\n\t\tnotifications[0...x].each do |i|\n\t\t\tif read[i]!=0 then read[i]-=1\n\t\t\telsif unread[i]>0 and a>0 then unread[i]-=1;a-=1 \n\t\t\tend \n#print notifications, \"\\n\"\n#print read,\"\\n\"\n#print unread, \"\\n\"\n\t\tend \n\tend \n#\tprint read,\"\\n\"\n#\tprint unread,\"\\n\"\n\tputs a \nend \n"}, {"source_code": "#$stdin=File.open \"input.txt\",\"r\"\n#$stdout=File.open \"output.txt\",\"w\"\n\nnotifications=Array.new\nd=0\na=0\n\nn,q=gets.split.map{|x|x.to_i}\nunread=Array.new(n+1,0)\nread=Array.new(n+1,0)\n\nq.times do \n\n\tt,x=gets.split.map{|y|y.to_i}\n\n\tif t==1 then notifications.push x; a+=1; unread[x]+=1\n\telsif t==2 then \n\t\ta-=unread[x];read[x]+=unread[x];unread[x]=0\n\telsif t==3\n#\tcur_read=read.clone\n\t\tnotifications[0...x-d].each do |i|\n#\t\t\tif cur_read[i]>0 then cur_read[i]-=1\n\t\t\tif read[i]>0 then read[i]-=1;notifications.shift;d+=1\n\t\t\telsif unread[i]>0 and a>0 then unread[i]-=1;a-=1\n\t\t\tend \n\t\tend \n\tend \n\n\tputs a \nend \n"}, {"source_code": "#$stdin=File.open \"input.txt\",\"r\"\n#$stdout=File.open \"output.txt\",\"w\"\n\nnotifications=Array.new\nd=0\na=0\n\nn,q=gets.split.map{|x|x.to_i}\nunread=Array.new(n+1,0)\nread=Array.new(n+1,0)\n\nq.times do \n\n\tt,x=gets.split.map{|y|y.to_i}\n\n\tif t==1 then notifications.push x; a+=1; unread[x]+=1\n\telsif t==2 then \n\t\ta-=unread[x];read[x]+=unread[x];unread[x]=0\n\telsif t==3\n\t\tnotifications[0...x-d].each do |i|\n\t\t\tif read[i]>0 then read[i]-=1;d+=1\n\t\t\telsif unread[i]>0 and a>0 then unread[i]-=1;a-=1\n\t\t\tend \n\t\t\tnotifications.shift\n\t\tend if x-d>0\n\tend \n\n\tputs a \nend \n"}, {"source_code": "#$stdin=File.open \"input.txt\",\"r\"\nn,q=gets.split.map{|x|x.to_i}\nnotifications=Array.new\ncount=Array.new\nc=0\nq.times do \n\ta,b=gets.split.map{|x|x.to_i}\n\t\n\tif a==1\n\t\tnotifications[b]=0 if notifications[b]==nil\n\t\tnotifications[b]+=1\n#print -1,notifications,-1,\"\\n\"\n\t\tcount[c]=b;\n\t\tc+=1\n#\t\tprint -3,count,-3,\"\\n\"\n\telsif a==2\n\t\tnotifications[b]=0\n\telse\n\t\tcount.each do |x|\n\t\t\tnotifications[x]-=1\n#\t\t\tprint -2,notifications,-2,\"\\n\"\n\t\tend\n\tend\n\tsum=0\n\tnotifications.each {|x| sum+=x if x!=nil}\n\tputs sum\nend\n"}, {"source_code": "#$stdin=File.open \"input.txt\",\"r\"\n\nnotifications=Array.new\na=0\n\nn,q=gets.split.map{|x|x.to_i}\nunread=Array.new(n+1,0)\nread=Array.new(n+1,0)\n\nq.times do \n\n\tt,x=gets.split.map{|y|y.to_i}\n\n\tif t==1 then notifications.push x; a+=1; unread[x]+=1\n\telsif t==2 then \n\t\ta-=unread[x];read[x]+=unread[x];unread[x]=0\n\telsif t==3\n\t\tnotifications[0...x].each do |i|\n\t\t\tif read[i]!=0 then read[i]-=1\n\t\t\telsif unread[i]>0 and a>0 then unread[i]-=1;a-=1 \n\t\t\tend \n\t\tend \n\tend \n\n\tputs a \nend \n"}, {"source_code": "#$stdin=File.open \"input.txt\",\"r\"\n#$stdout=File.open \"output.txt\",\"w\"\n\nnotifications=Array.new\na=0\n\nn,q=gets.split.map{|x|x.to_i}\nunread=Array.new(n+1,0)\nread=Array.new(n+1,0)\n\nq.times do \n\n\tt,x=gets.split.map{|y|y.to_i}\n\n\tif t==1 then notifications.push x; a+=1; unread[x]+=1\n\telsif t==2 then \n\t\ta-=unread[x];read[x]+=unread[x];unread[x]=0\n\telsif t==3\n\t\tcread=read.clone\n\t\tnotifications[0...x].each do |i|\n\t\t\tif cread[i]!=0 then cread[i]-=1\n\t\t\telsif unread[i]>0 and a>0 then unread[i]-=1;a-=1 \n\t\t\tend \n\t\tend \n\tend \n\n#\tprint read,\"\\n\"\n#\tprint unread,\"\\n\"\n\tputs a \nend \n"}, {"source_code": "#$stdin=File.open \"input.txt\",\"r\"\n\nnotifications=Array.new\na=0\n\nn,q=gets.split.map{|x|x.to_i}\nunread=Array.new(n+1,0)\nread=Array.new(n+1,0)\n\nq.times do \n\n\tt,x=gets.split.map{|y|y.to_i}\n\n\tif t==1 then notifications.push x; a+=1; unread[x]+=1\n\telsif t==2 then \n\t\ta-=unread[x];read[x]+=unread[x];unread[x]=0\n\telsif t==3\n\t\tnotifications[0...x].each do |i|\n\t\t\tif read[i] then read[i]-=1\n\t\t\telse unread[x]-=1;a-=1 \n\t\t\tend \n\t\tend \n\tend \n\n\tputs a \nend \n"}, {"source_code": "#$stdin=File.open \"input.txt\",\"r\"\nn,q=gets.split.map{|x|x.to_i}\nnotifications=Array.new\ncount=Array.new\nc=0\nq.times do \n\ta,b=gets.split.map{|x|x.to_i}\n\t\n\tif a==1\n\t\tnotifications[b]=0 if notifications[b]==nil\n\t\tnotifications[b]+=1\n\t\tcount[c]=b;\n\t\tc+=1\n\telsif a==2\n\t\tnotifications[b]=0\n\telse\n\t\tcount.each {|x| notifications[x]-=1 if notifications[x]>0}\n\tend\n\tsum=0\n\tnotifications.each {|x| sum+=x if x!=nil}\n\tputs sum\nend\n"}], "src_uid": "a5e724081ad84f88813bb4de23a8230e"} {"nl": {"description": "Little Petya has a birthday soon. Due this wonderful event, Petya's friends decided to give him sweets. The total number of Petya's friends equals to n.Let us remind you the definition of the greatest common divisor: GCD(a1, ..., ak) = d, where d represents such a maximal positive number that each ai (1 ≤ i ≤ k) is evenly divisible by d. At that, we assume that all ai's are greater than zero.Knowing that Petya is keen on programming, his friends has agreed beforehand that the 1-st friend gives a1 sweets, the 2-nd one gives a2 sweets, ..., the n-th one gives an sweets. At the same time, for any i and j (1 ≤ i, j ≤ n) they want the GCD(ai, aj) not to be equal to 1. However, they also want the following condition to be satisfied: GCD(a1, a2, ..., an) = 1. One more: all the ai should be distinct.Help the friends to choose the suitable numbers a1, ..., an.", "input_spec": "The first line contains an integer n (2 ≤ n ≤ 50).", "output_spec": "If there is no answer, print \"-1\" without quotes. Otherwise print a set of n distinct positive numbers a1, a2, ..., an. Each line must contain one number. Each number must consist of not more than 100 digits, and must not contain any leading zeros. If there are several solutions to that problem, print any of them. Do not forget, please, that all of the following conditions must be true: For every i and j (1 ≤ i, j ≤ n): GCD(ai, aj) ≠ 1 GCD(a1, a2, ..., an) = 1 For every i and j (1 ≤ i, j ≤ n, i ≠ j): ai ≠ aj Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).", "sample_inputs": ["3", "4"], "sample_outputs": ["99\n55\n11115", "385\n360\n792\n8360"], "notes": null}, "positive_code": [{"source_code": "primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29,\n 31, 37, 41, 43, 47, 53, 59, 61, 67, 71,\n 73, 79, 83, 89, 97, 101, 103, 107, 109, 113,\n 127, 131, 137, 139, 149, 151, 157, 163, 167, 173,\n 179, 181, 191, 193, 197, 199, 211, 223, 227, 229]\nn = gets.to_i\nif n > 2\n product = primes[0,n].inject(1) {|x, y| x * y}\n (0...n).each do |i|\n p product/primes[i]\n end\nelse\n p -1\nend\n"}, {"source_code": "require 'mathn'\n\nn = gets.to_i\n\nif n == 2 then puts \"-1\"\nelse\n\nprimes = []\np = Prime.new\nn.times do\n primes << p.next()\nend\n\nproduct = primes.inject do |p, x|\n p * x\nend\na = primes.map do |x|\n product / x\nend\n\nputs a.join(\"\\n\")\n\nend\n"}, {"source_code": "require 'prime'\n\nn = gets.to_i\nif n == 2\n\tp -1\n\texit\nend\n\na = []\nPrime.each do |prime|\n\ta << prime\n\tbreak if a.size == n\nend\ns = a.inject(&:*)\na = a.map{|_| s / _}\nputs a * \"\\n\"\n__END__\nn.times do |i|\n\tp a[i] if a[i].to_s.size > 100\n\ti.times do |j|\n\t\tp \"#{a[i]} #{a[j]}\" if a[i].gcd(a[j]) == 1\n\tend\nend\np \"555\" if a.inject(&:gcd) > 1\n"}, {"source_code": "#!/usr/bin/ruby\n\nrequire 'prime'\n\nn = gets.to_i\nif n == 2\n\tp -1\n\texit\nend\n\narr = Prime.take(n)\n\nn.times { |i|\n\ttmp = arr.dup\n\ttmp.delete_at(i)\n\tp tmp.inject(:*)\n}\n"}, {"source_code": "n = gets.to_i\nputs n > 2 ? ([10, 15] + (1..n - 2).map{|x| 6 * x}).join(\"\\n\") : -1\n"}, {"source_code": "n = STDIN.gets.chomp.to_i\nif n == 1\n puts 2\n exit 0\nend\nif n == 2\n puts -1\n exit 0\nend\nprimes = []\n# get primes\nx = 2\nwhile primes.size < n+1\n ok = true\n primes.each { |t|\n if (x % t == 0)\n ok = false\n break;\n end\n }\n if ok\n primes.push(x)\n end\n x = x + 1\nend\n\nans = []\n(n-1).times { |i|\n ans.push(primes[0])\n}\nans.push(1)\n\n(n-1).times { |i|\n ans[i] *= primes[i+1]\n ans[n-1] *= primes[i+1]\n}\n\nn.times { |i|\n puts ans[i]\n}\n"}, {"source_code": "require 'prime'\nn = gets\nn = n.to_i\ni = 0\nnum = 2\nprimes = Array.new()\nwhile i 2 ? ([10, 15] + (1..n - 2).map{|x| 6 * x}).join(\"\\n\") : -1\n"}, {"source_code": "def prime?(num)\n 2.upto(num-1){|i|\n if num % i == 0\n return false \n end\n }\n return true\nend\np = Array.new\ni = 2\nwhile p.length < 50\n if prime?(i)\n p << i\n end\n i += 1\nend\nn = gets.to_i\nif n!=2\n for i in 0...n\n ans = 1\n for j in 0...n-1\n ans *= p[(i+j)%n]\n end\n puts ans\n end\nelse \n puts -1\nend\n\n"}, {"source_code": "n = gets.to_i\nputs n > 2 ? ([10, 15] + (1..n - 2).map{|x| 6 * x}).join(\"\\n\") : -1\n"}, {"source_code": "\ndef prime x\n\n m = Math.sqrt x\n for i in 2..m\n if x % i == 0\n return false\n end\n end\n\n return true\n\nend\n\nn = gets.to_i\n\nif n == 2\n puts -1\nelse\n\n primes = []\n\n cur = 2\n while primes.size < n\n if prime cur\n primes.push cur\n end\n cur += 1\n end\n\n product = 1\n for prime in primes\n product *= prime\n end\n\n for i in 0...n\n puts product / primes[i]\n end\nend\n"}, {"source_code": "n = gets.to_i\nputs n > 2 ? ([10, 15] + (1..n - 2).map{|x| 6 * x}).join(\"\\n\") : -1\n"}, {"source_code": "n = gets.to_i\nputs n > 2 ? ([10, 15] + (1..n - 2).map{|x| 6 * x}).join(\"\\n\") : -1"}, {"source_code": "N = gets.to_i\n\nif N == 2\n puts -1\nelse\n puts 21\n puts 14\n 1.upto(N - 2) { |i|\n puts 3 * (2 ** i)\n }\nend\n"}, {"source_code": "n = gets.to_i\nputs n > 2 ? ([10, 15] + (1..n - 2).map{|x| 6 * x}).join(\"\\n\") : -1"}, {"source_code": "n = STDIN.gets.chomp.to_i\nif n == 2\n puts -1\n exit 0\nend\n\nvalues = [99, 55, 11115]\nproduct = values.reduce(&:*)\nmultiplier = 1\n4.upto(n) do\n values.push(product * multiplier)\n multiplier += 1\nend\nvalues.each do |value|\n puts value\nend\n"}, {"source_code": "n = gets.to_i\nputs n > 2 ? ([10, 15] + (1..n - 2).map{|x| 6 * x}).join(\"\\n\") : -1\n"}, {"source_code": "def gcd(a, b)\n return a if b.eql? 0\n gcd(b, a % b)\nend\n\nn = gets.to_i\n\nif (n == 2)\n puts -1\n exit\nend\n\nmas = [20,18,15]\nfor i in 3..n-1\n mas[i] = mas[i-1]*3;\nend\n\nputs mas"}, {"source_code": "n = gets.to_i\n\nprimes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239,]\n\nif n == 2\n puts -1\nelse\n n.times do |i|\n puts (primes[0,n] - [primes[i]]).inject(1){|r,j| r*j}\n end\nend\n"}, {"source_code": "a = [\n2, 3, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241\n];\n\nn = gets.to_i\n\nif n == 2 then print \"-1\"\nelse\n mega_res = 1\n for i in 0..(n-2) do\n puts (a[i] * a[n])\n mega_res = mega_res * a[i]\n end\n puts mega_res\nend\n"}, {"source_code": "n = gets.to_i\nputs n > 2 ? ([10, 15] + (1..n - 2).map{|x| 6 * x}).join(\"\\n\") : -1\n"}, {"source_code": "def is_prime(n)\n i = 2\n while i * i <= n\n return false if n % i == 0\n i += 1\n end\n true\nend\n\nn = gets.to_i\n\nif n == 2 \n puts -1\nelse\n i = 2\n primes = []\n while primes.size < n\n primes << i if is_prime(i)\n i += 1\n end\n\n product = primes.inject(1) { |p, i| p * i }\n puts primes.map { |p| product / p }\nend\n"}, {"source_code": "n = gets.to_i\nputs n > 2 ? ([10, 15] + (1..n - 2).map{|x| 6 * x}).join(\"\\n\") : -1\n"}, {"source_code": "n = gets.to_i\nputs n > 2 ? ([10, 15] + (1..n - 2).map{|x| 6 * x}).join(\"\\n\") : -1\n"}], "negative_code": [{"source_code": "primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29,\n 31, 37, 41, 43, 47, 53, 59, 61, 67, 71,\n 73, 79, 83, 89, 97, 101, 103, 107, 109, 113,\n 127, 131, 137, 139, 149, 151, 157, 163, 167, 173,\n 179, 181, 191, 193, 197, 199, 211, 223, 227, 229]\nn = gets.to_i\nproduct = primes[0,n].inject(1) {|x, y| x * y}\n(0...n).each do |i|\n p product/primes[i]\nend\n"}, {"source_code": "require 'mathn'\n\nn = gets.to_i\n\nprimes = []\np = Prime.new\nn.times do\n primes << p.next()\nend\n\nproduct = primes.inject do |p, x|\n p * x\nend\na = primes.map do |x|\n product / x\nend\n\nputs a.join(\" \")\n"}, {"source_code": "require 'mathn'\n\nn = gets.to_i\n\nprimes = []\np = Prime.new\nn.times do\n primes << p.next()\nend\n\nproduct = primes.inject do |p, x|\n p * x\nend\na = primes.map do |x|\n product / x\nend\n\nputs a.join(\"\\n\")\n"}, {"source_code": "#!/usr/bin/ruby\n\nrequire 'prime'\n\nn = gets.to_i\nif n == 2\n\tp -1\n\texit\nend\n\narr = Prime.take(50)\n\nn.times { |i|\n\ttmp = arr.dup\n\ttmp.delete_at(i)\n\tp tmp.inject(:*)\n}\n"}, {"source_code": "#!/usr/bin/ruby\n\nrequire 'mathn'\n\nn = gets.to_i\nif n == 2\n\tp -1\n\texit\nend\n\narr = []\nPrime.each(1000) { |pr|\n\tarr << pr\n\tbreak if arr.size == n\n}\n\nn.times { |i|\n\ttmp = arr.dup\n\ttmp.delete_at(i)\n\tp tmp.inject(:*)\n\tp tmp.inject(:*).to_s.size\n}\n"}, {"source_code": "#!/usr/bin/ruby\n\nrequire 'mathn'\n\nn = gets.to_i\nif n == 2\n\tp -1\n\texit\nend\n\narr = []\nPrime.each(100) { |pr|\n\tarr << pr\n\tbreak if arr.size == n\n}\n\nn.times { |i|\n\ttmp = arr.dup\n\ttmp.delete_at(i)\n\tp tmp.inject(:*)\n}\n"}, {"source_code": "n = STDIN.gets.chomp.to_i\nprimes = []\n# get primes\nx = 2\nwhile primes.size < n+1\n ok = true\n primes.each { |t|\n if (x % t == 0)\n ok = false\n break;\n end\n }\n if ok\n primes.push(x)\n end\n x = x + 1\nend\n\nans = []\n(n-1).times { |i|\n ans.push(primes[0])\n}\nans.push(1)\n\n(n-1).times { |i|\n ans[i] *= primes[i+1]\n ans[n-1] *= primes[i+1]\n}\n\nn.times { |i|\n puts ans[i]\n}\n"}, {"source_code": "\ndef prime x\n\n m = Math.sqrt x\n for i in 2..m\n if x % i == 0\n return false\n end\n end\n\n return true\n\nend\n\n\nn = gets.to_i\n\nprimes = []\n\ncur = 2\nwhile primes.size < n\n if prime cur\n primes.push cur\n end\n cur += 1\nend\n\nproduct = 1\nfor prime in primes\n product *= prime\nend\n\nfor i in 0...n\n puts product / primes[i]\nend\n"}, {"source_code": "n = gets.to_i\nputs n > 2 ? ([10, 15] + (1..n - 2).map{|x| 6 * x}).join(' ') : -1"}, {"source_code": "n = gets.to_i\nputs n > 2 ? ([10, 15] + (1..n - 2).map{|x| 6 * x}).join('\\n') : -1"}, {"source_code": "n = gets.to_i\nputs n > 2 ? ([10, 15] + (1..n - 2).map{|x| 6 * x}).join('/n') : -1"}, {"source_code": "n = STDIN.gets.chomp.to_i\nif n == 2\n puts -1\n exit 0\nend\n\nfactorial = {}\nf = 1\n1.upto(n + 5) do |i|\n f *= i\n factorial[i] = f\nend\n\nvalues = []\n1.upto(n - 2) do |i|\n values.push(factorial[i+1])\nend\n\nvalues.each { |x| puts x }\nputs values.last * 97\nputs values.last * 97 * 2\n"}, {"source_code": "a = [\n2, 3, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241\n];\n\nn = 0;\nn = gets.to_i\n\nif n == 2 then print \"-1\"\nelse\n mega_res = 1\n for i in 0..(n-2) do\n print (a[i] * a[n]) , \" \"\n mega_res = mega_res * a[i]\n end\n print mega_res\nend\n"}, {"source_code": "a = [\n2, 3, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239\n];\n\nn = 0;\nn = gets.to_i\n\nif n == 2 then print \"-1\"\nelse\n mega_res = 1\n for i in 0..(n-1) do\n print (a[i] * a[n-1]) , \" \"\n mega_res = mega_res * a[i]\n end\n print mega_res\nend\n"}, {"source_code": "a = [\n2, 3, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241\n];\n\nn = 0;\nn = gets.to_i\n\nif n == 2 then print \"-1\"\nelse\n mega_res = 1\n for i in 0..(n-1) do\n print (a[i] * a[n]) , \" \"\n mega_res = mega_res * a[i]\n end\n print mega_res\nend\n"}, {"source_code": "def is_prime(n)\n i = 2\n while i * i <= n\n return false if n % i == 0\n i += 1\n end\n true\nend\n\nn = gets.to_i\n\ni = 2\nprimes = []\nwhile primes.size < n\n primes << i if is_prime(i)\n i += 1\nend\n\nproduct = primes.inject(1) { |p, i| p * i }\nputs primes.map { |p| product / p }\n"}], "src_uid": "b3108315889607dabcd3112bcfe3fb54"} {"nl": {"description": "Berland National Library has recently been built in the capital of Berland. In addition, in the library you can take any of the collected works of Berland leaders, the library has a reading room.Today was the pilot launch of an automated reading room visitors' accounting system! The scanner of the system is installed at the entrance to the reading room. It records the events of the form \"reader entered room\", \"reader left room\". Every reader is assigned a registration number during the registration procedure at the library — it's a unique integer from 1 to 106. Thus, the system logs events of two forms: \"+ ri\" — the reader with registration number ri entered the room; \"- ri\" — the reader with registration number ri left the room. The first launch of the system was a success, it functioned for some period of time, and, at the time of its launch and at the time of its shutdown, the reading room may already have visitors.Significant funds of the budget of Berland have been spent on the design and installation of the system. Therefore, some of the citizens of the capital now demand to explain the need for this system and the benefits that its implementation will bring. Now, the developers of the system need to urgently come up with reasons for its existence.Help the system developers to find the minimum possible capacity of the reading room (in visitors) using the log of the system available to you.", "input_spec": "The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of records in the system log. Next follow n events from the system journal in the order in which the were made. Each event was written on a single line and looks as \"+ ri\" or \"- ri\", where ri is an integer from 1 to 106, the registration number of the visitor (that is, distinct visitors always have distinct registration numbers). It is guaranteed that the log is not contradictory, that is, for every visitor the types of any of his two consecutive events are distinct. Before starting the system, and after stopping the room may possibly contain visitors.", "output_spec": "Print a single integer — the minimum possible capacity of the reading room.", "sample_inputs": ["6\n+ 12001\n- 12001\n- 1\n- 1200\n+ 1\n+ 7", "2\n- 1\n- 2", "2\n+ 1\n- 1"], "sample_outputs": ["3", "2", "1"], "notes": "NoteIn the first sample test, the system log will ensure that at some point in the reading room were visitors with registration numbers 1, 1200 and 12001. More people were not in the room at the same time based on the log. Therefore, the answer to the test is 3."}, "positive_code": [{"source_code": "#!/usr/bin/ruby\nh={}\ncap=cur=0\ngets.to_i.times{\n\tx,y=gets.split\n\ty=y.to_i\n\tif x=='+' && !h[y]\n\t\tcur+=1\n\t\th[y]=1\n\telsif x=='-' && h[y]\n\t\tcur-=1\n\t\th[y]=nil\n\telsif x=='-' && !h[y]\n\t\tcap+=1\n\tend\n\tcap=[cap,cur].max\n}\np cap"}, {"source_code": "#!/usr/bin/ruby\n\ndef solution(n, records)\n set = {}\n mx = 0\n records.each do |rec|\n mx+=1 if rec[0] == '-' && ! set[rec[1]]\n set.delete rec[1] if rec[0] == '-'\n set[rec[1]] = rec if rec[0] == '+'\n mx = [mx, set.size].max\n end\n puts mx\nend\n\nn = gets.to_i\nrecords = []\nn.times { records.push(gets.split(' ')) }\nsolution n, records\n"}, {"source_code": "# http://codeforces.com/contest/567/problem/B\n\nrequire 'set'\nn = gets.chomp.to_i\ncounter = Hash.new { |h, k| h[k] = Set.new }\nmemo = Hash.new(0)\nn.times do |idx|\n op, num = gets.chomp.split\n counter[idx] = counter[idx - 1].clone\n\n case op\n when '+'\n memo[num] += 1\n counter[idx] << num\n else\n if memo[num] == 1\n memo[num] -= 1\n else\n (- 1...idx).each { |idx2| counter[idx2] << num }\n end\n counter[idx] -= [num]\n end\nend\n\nputs counter.values.map(&:length).max\n"}, {"source_code": "# require 'pry'\ndef read_ints; gets.chomp.split.map(&:to_i) end\nn = read_ints[0]\ncurrent = 0\nmax = 0\ncurrent_stack = []\nn.times do\n\tline = gets.chomp.split\n\tif(line[0]=='+')\n\t\t#new man come\n\t\tcurrent+=1\n\t\tcurrent_stack<max\n\telse\n\t\t#man come out\n\t\tman_id = line[1].to_i\n\t\tif(current_stack.include? man_id)\n\t\t\tcurrent_stack.delete(man_id)\n\t\t\tcurrent-=1\n\t\telse #was there from begging\n\t\t\tmax+=1\n\t\tend\n\tend\nend\nputs max"}, {"source_code": "n = gets.chomp.to_i\nrs = []\nn.times{\n rs << gets.chomp.split(' ')\n}\n\ncnt = 0\nhs = Hash.new\nrs.each do |r|\n if r[0] == '-' && hs[r[1]] != '+'\n cnt += 1\n end\n hs[r[1]] = r[0]\nend\n\nrtn = cnt\nrs.each do |r|\n if r[0] == '-'\n cnt -= 1\n else\n cnt += 1\n end\n rtn = cnt if rtn < cnt \nend\n\nputs rtn\n"}, {"source_code": "n = gets.to_i\nans = 0\nq = []\nn.times do\n a, b = gets.split\n b = b.to_i\n if a == '-'\n ans += 1 if q.delete(b).nil?\n else\n q << b\n end\n ans = [ans, q.size].max\nend\nputs ans\n"}, {"source_code": "require 'set'\n\nn=gets.chomp.to_i\nsign=Array.new(1000001){0}\nline=[]\nroom=Set.new\nfor i in 0...n\n line.push(gets.chomp)\n s=line[i].split(\" \")\n num=s[1].to_i\n if s[0]==\"-\"\n if sign[num]==0\n room.add(num)\n end\n else\n sign[num]=1\n end\nend\nans=room.size\nfor i in 0...n\n s=line[i].split(\" \")\n if s[0]==\"+\"\n room.add(s[1].to_i)\n else\n room.delete(s[1].to_i)\n end\n if room.size>ans\n ans=room.size\n end\nend\nputs ans\n"}, {"source_code": "list_length = gets.chomp.gsub(/ /,'').to_i\nnumbers = []\n\nlist_length.times do |i|\n numbers << gets.chomp.gsub(/ /,'').to_i\nend\n\ncurrent_number_in_room = 0\nmax = 0\ncurrent_users_in_room = []\n\nnumbers.each do |number|\n number = number.to_i\n\n if number > 0\n current_number_in_room += 1\n current_users_in_room << number.abs\n\n max <= current_number_in_room ? (max = current_number_in_room) : max\n else\n if current_users_in_room.include?(number.abs)\n current_users_in_room.delete(number.abs)\n\n current_number_in_room -= 1\n else\n maybe_max = max + 1\n\n max <= maybe_max ? max = maybe_max : max\n end\n end\nend\n\nputs max\n\n"}, {"source_code": "logNum = gets.chomp.to_i\n\ndata = []\nroom = 0\nstartRoom = 0\nresult = -1\npeopleExit = false\ninPeople = 0\ninitialPeople = []\nlogs = []\n\n(0...logNum).each do |iter|\n\tlog = gets.chomp.split(' ')\n\tlogs << log\n\n\tif log[0] == \"+\"\n\t\tdata << log[1]\n\telse\n\t\tif !data.include?(log[1])\n\t\t\tstartRoom += 1\n\t\tend\n\tend\nend\n\n\ndata = data.clear\nroom = startRoom\nresult = startRoom\n\n#puts \"middle room : #{room}\"\n\nlogs.each do |log|\n\n\tif log[0] == \"+\"\n\t\tdata << log[1]\n\t\troom += 1\n\telse\n\t\tif data.include?(log[1])\n\t\t\tdata.delete(log[1])\n\t\tend\n\n\t\troom -= 1\n\tend\n\n\tresult = room if result < room\nend\n\nputs result"}], "negative_code": [{"source_code": "#!/usr/bin/ruby\n\ndef solution(n, records)\n set = {}\n mx = 0\n records.reverse.each do |rec|\n set.delete rec[1] if rec[0] == '+'\n set[rec[1]] = rec if rec[0] == '-'\n mx = [mx, set.size].max\n end\n puts mx\nend\n\nn = gets.to_i\nrecords = []\nn.times { records.push(gets.split(' ')) }\nsolution n, records\n\n# solution 6, [%w(+ 12001),%w(- 12001),%w(- 1),%w(- 1200),%w(+ 1),%w(+ 7)]\n# solution 4, 4, '.cc.', [1, 2, 1, 0], %w(. . a a)\n# solution 4,3\n# solution 3,2\n# solution 3,1\n# solution 3,3\n# solution 2,1\n# solution 2,2\n#\n# #\n# # solution 4, [75, 150, 75, 50]\n# # solution 3, [100, 150, 250]"}, {"source_code": "# http://codeforces.com/contest/567/problem/B\n\nrequire 'set'\nn = gets.chomp.to_i\ncounter = Hash.new { |h, k| h[k] = Set.new }\nmemo = Hash.new(0)\nn.times do |idx|\n op, num = gets.chomp.split\n counter[idx] = counter[idx - 1].clone\n\n case op\n when '+'\n memo[num] += 1\n counter[idx] << num\n else\n if memo[num] == 1\n memo[num] -= 1\n counter[idx] -= [num]\n else\n (0..idx).each { |idx2| counter[idx2] << num }\n end\n end\nend\n\nputs counter.values.map(&:length).max\n"}, {"source_code": "logNum = gets.chomp.to_i\n\ndata = []\nroom = 0\nstartRoom = 0\nresult = -1\npeopleExit = false\ninPeople = 0\n\n(0...logNum).each do |iter|\n\tlog = gets.chomp.split(' ')\n\n\tif log[0] == \"+\"\n\t\tdata << log[1]\n\t\troom += 1\n\n\t\tinPeople += 1 if !peopleExit\n\telse\n\t\tpeopleExit = true\n\t\tif data.include?(log[1])\n\t\t\tdata.delete(log[1])\n\t\t\troom -= 1\n\t\telse\n\t\t\tstartRoom += 1\n\t\tend\n\tend\n\n\tresult = room if result < room\nend\n\nresult = inPeople + startRoom if result < inPeople + startRoom\n\nputs result"}], "src_uid": "6cfd3b0a403212ec68bac1667bce9ef1"} {"nl": {"description": "There is an array with n elements a1, a2, ..., an and the number x.In one operation you can select some i (1 ≤ i ≤ n) and replace element ai with ai & x, where & denotes the bitwise and operation.You want the array to have at least two equal elements after applying some operations (possibly, none). In other words, there should be at least two distinct indices i ≠ j such that ai = aj. Determine whether it is possible to achieve and, if possible, the minimal number of operations to apply.", "input_spec": "The first line contains integers n and x (2 ≤ n ≤ 100 000, 1 ≤ x ≤ 100 000), number of elements in the array and the number to and with. The second line contains n integers ai (1 ≤ ai ≤ 100 000), the elements of the array.", "output_spec": "Print a single integer denoting the minimal number of operations to do, or -1, if it is impossible.", "sample_inputs": ["4 3\n1 2 3 7", "2 228\n1 1", "3 7\n1 2 3"], "sample_outputs": ["1", "0", "-1"], "notes": "NoteIn the first example one can apply the operation to the last element of the array. That replaces 7 with 3, so we achieve the goal in one move.In the second example the array already has two equal elements.In the third example applying the operation won't change the array at all, so it is impossible to make some pair of elements equal."}, "positive_code": [{"source_code": "N, X = gets.split.map(&:to_i)\nas = gets.split.map(&:to_i)\nah = {}\nxh = {}\nans = 10\nN.times do |i|\n ax = as[i] & X\n if ah[as[i]]\n ans = 0 if ans > 0\n elsif xh[as[i]] || ah[ax]\n ans = 1 if ans > 1\n elsif xh[ax]\n ans = 2 if ans > 2\n end\n ah[as[i]] = true\n xh[ax] = true\nend\nputs ans == 10 ? -1 : ans"}], "negative_code": [], "src_uid": "f4bb0b8f285b0c8cbaf469964505cc56"} {"nl": {"description": "There is the following puzzle popular among nuclear physicists.A reactor contains a set of n atoms of some chemical elements. We shall understand the phrase \"atomic number\" as the number of this atom's element in the periodic table of the chemical elements.You are allowed to take any two different atoms and fuse a new one from them. That results in a new atom, whose number is equal to the sum of the numbers of original atoms. The fusion operation can be performed several times.The aim is getting a new pregiven set of k atoms.The puzzle's difficulty is that it is only allowed to fuse two atoms into one, it is not allowed to split an atom into several atoms. You are suggested to try to solve the puzzle.", "input_spec": "The first line contains two integers n and k (1 ≤ k ≤ n ≤ 17). The second line contains space-separated symbols of elements of n atoms, which are available from the start. The third line contains space-separated symbols of elements of k atoms which need to be the result of the fusion. The symbols of the elements coincide with the symbols from the periodic table of the chemical elements. The atomic numbers do not exceed 100 (elements possessing larger numbers are highly unstable). Some atoms can have identical numbers (that is, there can be several atoms of the same element). The sum of numbers of initial atoms is equal to the sum of numbers of the atoms that need to be synthesized.", "output_spec": "If it is impossible to synthesize the required atoms, print \"NO\" without the quotes. Otherwise, print on the first line «YES», and on the next k lines print the way of synthesizing each of k atoms as equations. Each equation has the following form: \"x1+x2+...+xt->yi\", where xj is the symbol of the element of some atom from the original set, and yi is the symbol of the element of some atom from the resulting set. Each atom from the input data should occur in the output data exactly one time. The order of summands in the equations, as well as the output order does not matter. If there are several solutions, print any of them. For a better understanding of the output format, see the samples.", "sample_inputs": ["10 3\nMn Co Li Mg C P F Zn Sc K\nSn Pt Y", "2 1\nH H\nHe", "2 2\nBk Fm\nCf Es"], "sample_outputs": ["YES\nMn+C+K->Sn\nCo+Zn+Sc->Pt\nLi+Mg+P+F->Y", "YES\nH+H->He", "NO"], "notes": "NoteThe reactions from the first example possess the following form (the atomic number is written below and to the left of the element):To find a periodic table of the chemical elements, you may use your favorite search engine.The pretest set contains each of the first 100 elements of the periodic table at least once. You can use that information to check for misprints."}, "positive_code": [{"source_code": "class E\n ATOMS = \"Hoge H He Li Be B C N O F Ne Na Mg Al Si P S Cl Ar K Ca Sc Ti V Cr Mn Fe Co Ni Cu Zn Ga Ge As Se Br Kr Rb Sr Y Zr Nb Mo Tc Ru Rh Pd Ag Cd In Sn Sb Te I Xe Cs Ba La Ce Pr Nd Pm Sm Eu Gd Tb Dy Ho Er Tm Yb Lu Hf Ta W Re Os Ir Pt Au Hg Tl Pb Bi Po At Rn Fr Ra Ac Th Pa U Np Pu Am Cm Bk Cf Es Fm Md No\".split\n\n def initialize\n @atom_num = {}\n ATOMS.each_with_index { |v, i| @atom_num[v] = i }\n end\n\n def dfs(sp, dp, sum, using)\n if dp == @dests.size\n puts \"YES\"\n arr = Array.new(@dests.size){Array.new}\n using.each_with_index do |v, idx|\n arr[v] << @srcs[idx]\n end\n arr.each_with_index do |v, idx|\n puts \"#{v.map{|a| ATOMS[a]}.join('+')}->#{ATOMS[@dests[idx]]}\"\n end\n exit\n end\n\n prev = -1\n sp.upto(@srcs.size-1) do |i|\n next if @srcs[i] == prev\n next if using[i] != -1\n\n tot = sum + @srcs[i]\n next if tot > @dests[dp]\n\n using[i] = dp\n if tot == @dests[dp]\n dfs(0, dp+1, 0, using)\n else\n dfs(i+1, dp, tot, using)\n end\n using[i] = -1\n\n prev = @srcs[i]\n end\n end\n\n\n def solve\n n,k = gets.split.map(&:to_i)\n @srcs = gets.split.map { |v| @atom_num[v] }.sort\n @dests = gets.split.map { |v| @atom_num[v] }\n\n dfs(0, 0, 0, [-1]*@srcs.size)\n puts \"NO\"\n end\nend\n\ne = E.new\ne.solve\n"}], "negative_code": [], "src_uid": "f84c7b3945a5112e4ab6bdf9a6b5e63b"} {"nl": {"description": "An array is sorted if it has no inversionsA Young BoyYou are given an array of $$$n$$$ positive integers $$$a_1,a_2,\\ldots,a_n$$$. In one operation you do the following: Choose any integer $$$x$$$. For all $$$i$$$ such that $$$a_i = x$$$, do $$$a_i := 0$$$ (assign $$$0$$$ to $$$a_i$$$). Find the minimum number of operations required to sort the array in non-decreasing order.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 10^4$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$). The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\\ldots,a_n$$$ ($$$1 \\le a_i \\le n$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.", "output_spec": "For each test case print one integer — the minimum number of operations required to sort the array in non-decreasing order.", "sample_inputs": ["5\n\n3\n\n3 3 2\n\n4\n\n1 3 1 3\n\n5\n\n4 1 5 3 2\n\n4\n\n2 4 1 2\n\n1\n\n1"], "sample_outputs": ["1\n2\n4\n3\n0"], "notes": "NoteIn the first test case, you can choose $$$x = 3$$$ for the operation, the resulting array is $$$[0, 0, 2]$$$.In the second test case, you can choose $$$x = 1$$$ for the first operation and $$$x = 3$$$ for the second operation, the resulting array is $$$[0, 0, 0, 0]$$$."}, "positive_code": [{"source_code": "require \"set\"\r\n\r\ngets.to_i.times do\r\n n = gets.to_i\r\n xs = gets.split.map(&:to_i)\r\n replaced = Set.new\r\n last = 0\r\n xs.each_cons(2).each.with_index do |(a, b), i|\r\n a = replaced.include?(a) ? 0 : a\r\n b = replaced.include?(b) ? 0 : b\r\n if a <= b\r\n else\r\n (last..i).each do |i|\r\n replaced << xs[i]\r\n end\r\n last = i\r\n end\r\n end\r\n p replaced.count\r\nend\r\n"}, {"source_code": "require 'set'\ngets.to_i.times do\n n=gets.to_i\n # aを広義単調増加にする手数のmin\n a=gets.split.map(&:to_i)\n t=-1\n (n-1).times do |i|\n t=i if a[i]>a[i+1]\n end\n s=Set.new\n (t+1).times {|i| s << a[i]}\n lim=t\n (n-1).downto(t+1) do |i|\n if s.include? a[i] then\n lim=i\n break\n end\n end\n p lim==-1 ? 0 : a[0..lim].uniq.size\nend\n"}], "negative_code": [], "src_uid": "68e830551c1580bb5aff40d7848d968d"} {"nl": {"description": "Since the giant heads have appeared in the sky all humanity is in danger, so all Ricks and Mortys from all parallel universes are gathering in groups to find a solution to get rid of them. There are n parallel universes participating in this event (n Ricks and n Mortys). I. e. each of n universes has one Rick and one Morty. They're gathering in m groups. Each person can be in many groups and a group can contain an arbitrary number of members.Ricks and Mortys have registered online in these groups. So, a person can have joined a group more than once (developer of this website hadn't considered this possibility). Summer from universe #1 knows that in each parallel universe (including hers) exactly one of Rick and Morty from that universe is a traitor and is loyal, but no one knows which one. She knows that we are doomed if there's a group such that every member in that group is a traitor (they will plan and destroy the world). Summer knows that if there's a possibility that world ends (there's a group where all members are traitors) she should immediately cancel this event. So she wants to know if she should cancel the event. You have to tell her yes if and only if there's at least one scenario (among all 2n possible scenarios, 2 possible scenarios for who a traitor in each universe) such that in that scenario the world will end.", "input_spec": "The first line of input contains two integers n and m (1 ≤ n, m ≤ 104) — number of universes and number of groups respectively. The next m lines contain the information about the groups. i-th of them first contains an integer k (number of times someone joined i-th group, k > 0) followed by k integers vi, 1, vi, 2, ..., vi, k. If vi, j is negative, it means that Rick from universe number  - vi, j has joined this group and otherwise it means that Morty from universe number vi, j has joined it. Sum of k for all groups does not exceed 104.", "output_spec": "In a single line print the answer to Summer's question. Print \"YES\" if she should cancel the event and \"NO\" otherwise.", "sample_inputs": ["4 2\n1 -3\n4 -2 3 2 -3", "5 2\n5 3 -2 1 -1 5\n3 -5 2 5", "7 2\n3 -1 6 7\n7 -5 4 2 4 7 -3 4"], "sample_outputs": ["YES", "NO", "YES"], "notes": "NoteIn the first sample testcase, 1st group only contains the Rick from universe number 3, so in case he's a traitor, then all members of this group are traitors and so Summer should cancel the event."}, "positive_code": [{"source_code": "STDIN.readline\nwhile !STDIN.eof? && signups = STDIN.readline.strip.split(\" \").map(&:to_i)\n valid = false\n group_signups = {}\n signups.each.with_index do |signup, index|\n if(index > 0)\n if(group_signups[-signup])\n valid = true\n break\n else\n group_signups[signup] = signup\n end\n end\n end\n\n if(!valid)\n puts \"YES\"\n exit\n end\nend\n\nputs \"NO\"\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\na = m.times.map{gets.split.map(&:to_i)[1..-1].uniq.map(&:abs)}\nputs a.any?{|x| x.size == 1 || x.uniq.size == x.size} ? 'YES' : 'NO'\n"}], "negative_code": [], "src_uid": "f110b9351fe8ff20676d11ecfc92aee3"} {"nl": {"description": "You want to perform the combo on your opponent in one popular fighting game. The combo is the string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters. To perform the combo, you have to press all buttons in the order they appear in $$$s$$$. I.e. if $$$s=$$$\"abca\" then you have to press 'a', then 'b', 'c' and 'a' again.You know that you will spend $$$m$$$ wrong tries to perform the combo and during the $$$i$$$-th try you will make a mistake right after $$$p_i$$$-th button ($$$1 \\le p_i < n$$$) (i.e. you will press first $$$p_i$$$ buttons right and start performing the combo from the beginning). It is guaranteed that during the $$$m+1$$$-th try you press all buttons right and finally perform the combo.I.e. if $$$s=$$$\"abca\", $$$m=2$$$ and $$$p = [1, 3]$$$ then the sequence of pressed buttons will be 'a' (here you're making a mistake and start performing the combo from the beginning), 'a', 'b', 'c', (here you're making a mistake and start performing the combo from the beginning), 'a' (note that at this point you will not perform the combo because of the mistake), 'b', 'c', 'a'.Your task is to calculate for each button (letter) the number of times you'll press it.You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \\le n \\le 2 \\cdot 10^5$$$, $$$1 \\le m \\le 2 \\cdot 10^5$$$) — the length of $$$s$$$ and the number of tries correspondingly. The second line of each test case contains the string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters. The third line of each test case contains $$$m$$$ integers $$$p_1, p_2, \\dots, p_m$$$ ($$$1 \\le p_i < n$$$) — the number of characters pressed right during the $$$i$$$-th try. It is guaranteed that the sum of $$$n$$$ and the sum of $$$m$$$ both does not exceed $$$2 \\cdot 10^5$$$ ($$$\\sum n \\le 2 \\cdot 10^5$$$, $$$\\sum m \\le 2 \\cdot 10^5$$$). It is guaranteed that the answer for each letter does not exceed $$$2 \\cdot 10^9$$$.", "output_spec": "For each test case, print the answer — $$$26$$$ integers: the number of times you press the button 'a', the number of times you press the button 'b', $$$\\dots$$$, the number of times you press the button 'z'.", "sample_inputs": ["3\n4 2\nabca\n1 3\n10 5\ncodeforces\n2 8 3 2 9\n26 10\nqwertyuioplkjhgfdsazxcvbnm\n20 10 1 2 3 5 10 5 9 4"], "sample_outputs": ["4 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n0 0 9 4 5 3 0 0 0 0 0 0 0 0 9 0 0 3 1 0 0 0 0 0 0 0 \n2 1 1 2 9 2 2 2 5 2 2 2 1 1 5 4 11 8 2 7 5 1 10 1 5 2"], "notes": "NoteThe first test case is described in the problem statement. Wrong tries are \"a\", \"abc\" and the final try is \"abca\". The number of times you press 'a' is $$$4$$$, 'b' is $$$2$$$ and 'c' is $$$2$$$.In the second test case, there are five wrong tries: \"co\", \"codeforc\", \"cod\", \"co\", \"codeforce\" and the final try is \"codeforces\". The number of times you press 'c' is $$$9$$$, 'd' is $$$4$$$, 'e' is $$$5$$$, 'f' is $$$3$$$, 'o' is $$$9$$$, 'r' is $$$3$$$ and 's' is $$$1$$$."}, "positive_code": [{"source_code": "def read_array(to)\n gets.strip.split(' ').map(&to)\nend\n\nMAPPING = ('a'..'z').to_a.zip((0..26)).inject({}) { |m, h| m[h[0]] = h[1]; m }\n\ngets.strip.to_i.times do\n _, _ = read_array(:to_i)\n s = gets.strip\n p = read_array(:to_i)\n sums = []\n s.split('').inject(Array.new(26, 0)) do |m, c|\n m[MAPPING[c]] += 1\n sums << m.dup\n m\n end\n res = Array.new(26, 0)\n (p + [s.size]).each_with_index do |t, i|\n res = sums[t-1].zip(res).map { |x| x[0] + x[1] }\n end\n puts res.join(' ')\nend"}, {"source_code": "n=gets.to_i\n(1..n).each do |x|\n array = []\n a,b=gets.chomp.split(\" \").map(&:to_i)\n str=gets.chomp.split(\"\")\n array=gets.chomp.split(\" \").map(&:to_i)\n array.push(a)\n temp = Array.new(a, 0)\n array.each do |arr|\n temp[arr-1] += 1\n end\n a -= 1\n while a > 0\n a -= 1\n temp[a] += temp[a+1]\n end\n result = Array.new(26, 0)\n k = 0\n temp.each do |arr|\n result[str[k].ord - 97] += arr\n k += 1\n end\n print(\"#{result.join(\" \")}\\n\")\nend"}], "negative_code": [], "src_uid": "51ea912b40b07c1ba097292ffd0cec18"} {"nl": {"description": "Everybody knows that the Berland citizens are keen on health, especially students. Berland students are so tough that all they drink is orange juice!Yesterday one student, Vasya and his mates made some barbecue and they drank this healthy drink only. After they ran out of the first barrel of juice, they decided to play a simple game. All n people who came to the barbecue sat in a circle (thus each person received a unique index bi from 0 to n - 1). The person number 0 started the game (this time it was Vasya). All turns in the game were numbered by integers starting from 1. If the j-th turn was made by the person with index bi, then this person acted like that: he pointed at the person with index (bi + 1) mod n either with an elbow or with a nod (x mod y is the remainder after dividing x by y); if j ≥ 4 and the players who had turns number j - 1, j - 2, j - 3, made during their turns the same moves as player bi on the current turn, then he had drunk a glass of juice; the turn went to person number (bi + 1) mod n. The person who was pointed on the last turn did not make any actions.The problem was, Vasya's drunk too much juice and can't remember the goal of the game. However, Vasya's got the recorded sequence of all the participants' actions (including himself). Now Vasya wants to find out the maximum amount of juice he could drink if he played optimally well (the other players' actions do not change). Help him.You can assume that in any scenario, there is enough juice for everybody.", "input_spec": "The first line contains a single integer n (4 ≤ n ≤ 2000) — the number of participants in the game. The second line describes the actual game: the i-th character of this line equals 'a', if the participant who moved i-th pointed at the next person with his elbow, and 'b', if the participant pointed with a nod. The game continued for at least 1 and at most 2000 turns. ", "output_spec": "Print a single integer — the number of glasses of juice Vasya could have drunk if he had played optimally well.", "sample_inputs": ["4\nabbba", "4\nabbab"], "sample_outputs": ["1", "0"], "notes": "NoteIn both samples Vasya has got two turns — 1 and 5. In the first sample, Vasya could have drunk a glass of juice during the fifth turn if he had pointed at the next person with a nod. In this case, the sequence of moves would look like \"abbbb\". In the second sample Vasya wouldn't drink a single glass of juice as the moves performed during turns 3 and 4 are different."}, "positive_code": [{"source_code": "n=gets.to_i\ns=gets.chomp\nl=s.size\nc=0\np\nfor i in (n..l-1).step(n) do\n c+=1 if s[i-1]==s[i-2] and s[i-1]==s[i-3]\nend\np c\n"}, {"source_code": "n = gets.to_i \ns = gets.chomp\nc=0;\nfor i in (n..s.length-1).step(n)\n\tif(s[i-1]==s[i-2] && s[i-3]==s[i-1])\n\t\tc+=1;\n\tend\nend\np c"}, {"source_code": "n = gets.to_i\nar = gets.chomp\nc = 0;\nfor i in (n..ar.length-1).step(n)\n\tif(ar[i-1]==ar[i-2] && ar[i-1]==ar[i-3])\n\t\tc+=1\n\tend\nend\np c; "}, {"source_code": "n = gets.to_i\ns = gets.chomp\ni = n\nc = 0\nwhile i < s.size\n c += 1 if s[i - 1] == s[i - 2] && s[i - 2] == s[i - 3]\n i += n\nend\nputs c"}, {"source_code": "n = gets.to_i\nputs gets.chomp.slice(1,2000).chars.each_slice(n).select{|x|\n x.length == n && x[-4] == x[-3] && x[-3] == x[-2]\n}.length\n"}, {"source_code": "n = gets.to_i\nseq = gets.chomp!\nret = 0\nfor i in 0...seq.size\n if i >= 4 && i % n == 0\n if seq[i-1] == seq[i-2] && seq[i-2] == seq[i-3]\n ret += 1\n end\n end\nend\nputs ret"}, {"source_code": "n = STDIN.readline.to_i\ns = STDIN.readline.strip\npos = n\ncount = 0\nwhile pos < s.length\n\tcount += 1 if s[pos-1] == s[pos-2] and s[pos-2] == s[pos-3]\n\tpos += n\nend\nputs count\n"}, {"source_code": "n=gets.to_i\ns=gets.chomp\nl=s.size\nc=0\np\nfor i in (n..l-1).step(n) do\n c+=1 if s[i-1]==s[i-2] and s[i-1]==s[i-3]\nend\np c\n"}, {"source_code": "def run\n n = $stdin.gets.to_i\n a = $stdin.gets.chomp.split(//)\n\n cnt = 0\n cur = 0\n while cur < a.length\n cnt += 1 if cur >= 3 and a[cur - 1] == a[cur - 2] and a[cur - 1] == a[cur - 3]\n cur += n\n end\n\n puts cnt\nend\n\nrun if $0 == __FILE__\n"}, {"source_code": "n = gets.chomp.to_i\nstr = gets.chomp\n\ni = 0\nc = 0\nwhile i * n < str.length do\n i+=1\n next if i*n < 3 || i*n >= str.length\n if str[i*n - 1] == str[i*n - 2] &&\n str[i*n - 1] == str[i*n - 3]\n c += 1\n end\nend\nputs c\n"}, {"source_code": "n = gets.to_i\ns = gets.chomp\nans = 0\n(n...s.size).step(n).each{|i| ans += 1 if s[i-3]==s[i-2] and s[i-2]==s[i-1]}\nputs ans\n"}, {"source_code": "n = gets.to_i\ns = gets.chomp\nr = 0\ni = n\nwhile i < s.length\n if (s[i-3,3] == 'aaa') or (s[i-3,3] == 'bbb')\n r += 1\n end\n i += n\nend\nputs \"#{r}\"\n"}], "negative_code": [{"source_code": "n = gets.to_i \ns = gets.chomp\nc=0;\nfor i in (n..s.length).step(n)\n\tif(s[i-1]==s[i-2] && s[i-3]==s[i-1])\n\t\tc+=1;\n\tend\nend\np c"}, {"source_code": "def run\n n = $stdin.gets.to_i\n a = $stdin.gets.split(//)\n\n cnt = 0\n cur = 0\n while cur < a.length\n cnt += 1 if cur >= 3 and a[cur - 1] == a[cur - 2] and a[cur - 1] == a[cur - 3]\n cur += n\n end\n\n puts cnt\nend\n\nrun if $0 == __FILE__\n"}, {"source_code": "n=gets.to_i\ns=gets.chomp\nl=s.size\nc=0\nfor i in (n..l-1).step(n) do\n c+=1 if s[i-1]==s[i-2] and s[i-1]==s[i-2]\nend\np c\n"}, {"source_code": "n=gets.to_i\ns=gets.chomp\nl=s.size\nc=0\nfor i in (n..l).step(n) do\n c+=1 if s[i-1]==s[i-2] and s[i-1]==s[i-2]\nend\np c\n"}], "src_uid": "5606799ab5345bb9501fa6535e5abef9"} {"nl": {"description": "There is a binary string $$$a$$$ of length $$$n$$$. In one operation, you can select any prefix of $$$a$$$ with an equal number of $$$0$$$ and $$$1$$$ symbols. Then all symbols in the prefix are inverted: each $$$0$$$ becomes $$$1$$$ and each $$$1$$$ becomes $$$0$$$.For example, suppose $$$a=0111010000$$$. In the first operation, we can select the prefix of length $$$8$$$ since it has four $$$0$$$'s and four $$$1$$$'s: $$$[01110100]00\\to [10001011]00$$$. In the second operation, we can select the prefix of length $$$2$$$ since it has one $$$0$$$ and one $$$1$$$: $$$[10]00101100\\to [01]00101100$$$. It is illegal to select the prefix of length $$$4$$$ for the third operation, because it has three $$$0$$$'s and one $$$1$$$. Can you transform the string $$$a$$$ into the string $$$b$$$ using some finite number of operations (possibly, none)?", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1\\le t\\le 10^4$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1\\le n\\le 3\\cdot 10^5$$$) — the length of the strings $$$a$$$ and $$$b$$$. The following two lines contain strings $$$a$$$ and $$$b$$$ of length $$$n$$$, consisting of symbols $$$0$$$ and $$$1$$$. The sum of $$$n$$$ across all test cases does not exceed $$$3\\cdot 10^5$$$.", "output_spec": "For each test case, output \"YES\" if it is possible to transform $$$a$$$ into $$$b$$$, or \"NO\" if it is impossible. You can print each letter in any case (upper or lower).", "sample_inputs": ["5\n10\n0111010000\n0100101100\n4\n0000\n0000\n3\n001\n000\n12\n010101010101\n100110011010\n6\n000111\n110100"], "sample_outputs": ["YES\nYES\nNO\nYES\nNO"], "notes": "NoteThe first test case is shown in the statement.In the second test case, we transform $$$a$$$ into $$$b$$$ by using zero operations.In the third test case, there is no legal operation, so it is impossible to transform $$$a$$$ into $$$b$$$.In the fourth test case, here is one such transformation: Select the length $$$2$$$ prefix to get $$$100101010101$$$. Select the length $$$12$$$ prefix to get $$$011010101010$$$. Select the length $$$8$$$ prefix to get $$$100101011010$$$. Select the length $$$4$$$ prefix to get $$$011001011010$$$. Select the length $$$6$$$ prefix to get $$$100110011010$$$. In the fifth test case, the only legal operation is to transform $$$a$$$ into $$$111000$$$. From there, the only legal operation is to return to the string we started with, so we cannot transform $$$a$$$ into $$$b$$$."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n n=gets.to_i\r\n a=gets.chomp.chars\r\n b=gets.chomp.chars\r\n i=0\r\n f=true\r\n c=0\r\n j=0\r\n while i i\n possible = true\n elsif firstPossibles.include?(-2*array[i]) && firstPossibles[-2*array[i]] < i\n possible = true\n end\nend\n\nif possible\n print(\"YES\")\nelse\n print(\"NO\")\nend"}], "negative_code": [{"source_code": "require 'set'\nn = gets.to_i\narray = gets.split(\" \").map{|i| i.to_i}\nrsum = []\ntotalSum = array.inject(0){|sum,x| sum + x }\nsum = 0\n0.upto(n-1) do |i|\n sum += array[i]\n rsum << sum\nend\n\npossible = false\npossibles = {}\n0.upto(n-1) do |i|\n left = 2*rsum[i] - totalSum\n possibles[left] = i\n if left == 0\n possible = true\n end\nend\n\n\n0.upto(n-1) do |i|\n if possibles.include?(2*array[i]) && possibles[2*array[i]] >= i\n possible = true\n elsif possibles.include?(-2*array[i]) && possibles[-2*array[i]] <= i\n possible = true\n end\nend\n\nif possible\n print(\"YES\")\nelse\n print(\"NO\")\nend"}, {"source_code": "require 'set'\nn = gets.to_i\narray = gets.split(\" \").map{|i| i.to_i}\nrsum = []\ntotalSum = array.inject(0){|sum,x| sum + x }\nsum = 0\n0.upto(n-1) do |i|\n sum += array[i]\n rsum << sum\nend\n\npossible = false\npossibles = Set.new\n0.upto(n-1) do |i|\n left = 2*rsum[i] - totalSum\n possibles.add(left)\n if left == 0\n possible = true\n end\nend\n\n\n0.upto(n-1) do |i|\n if possibles.include? 2*array[i]\n possible = true\n end\nend\n\nif possible\n print(\"YES\")\nelse\n print(\"NO\")\nend"}, {"source_code": "require 'set'\nn = gets.to_i\narray = gets.split(\" \").map{|i| i.to_i}\nrsum = []\ntotalSum = array.inject(0){|sum,x| sum + x }\nsum = 0\n0.upto(n-1) do |i|\n sum += array[i]\n rsum << sum\nend\n\npossible = false\npossibles = {}\n0.upto(n-1) do |i|\n left = 2*rsum[i] - totalSum\n possibles[left] = i\n if left == 0\n possible = true\n end\nend\n\n\n0.upto(n-1) do |i|\n if possibles.include?(2*array[i]) && possibles[2*array[i]] >= i\n possible = true\n end\nend\n\nif possible\n print(\"YES\")\nelse\n print(\"NO\")\nend"}], "src_uid": "27b93795ffc771b47b995e2b83f7b945"} {"nl": {"description": "You are playing a variation of game 2048. Initially you have a multiset $$$s$$$ of $$$n$$$ integers. Every integer in this multiset is a power of two. You may perform any number (possibly, zero) operations with this multiset.During each operation you choose two equal integers from $$$s$$$, remove them from $$$s$$$ and insert the number equal to their sum into $$$s$$$.For example, if $$$s = \\{1, 2, 1, 1, 4, 2, 2\\}$$$ and you choose integers $$$2$$$ and $$$2$$$, then the multiset becomes $$$\\{1, 1, 1, 4, 4, 2\\}$$$.You win if the number $$$2048$$$ belongs to your multiset. For example, if $$$s = \\{1024, 512, 512, 4\\}$$$ you can win as follows: choose $$$512$$$ and $$$512$$$, your multiset turns into $$$\\{1024, 1024, 4\\}$$$. Then choose $$$1024$$$ and $$$1024$$$, your multiset turns into $$$\\{2048, 4\\}$$$ and you win.You have to determine if you can win this game.You have to answer $$$q$$$ independent queries.", "input_spec": "The first line contains one integer $$$q$$$ ($$$1 \\le q \\le 100$$$) – the number of queries. The first line of each query contains one integer $$$n$$$ ($$$1 \\le n \\le 100$$$) — the number of elements in multiset. The second line of each query contains $$$n$$$ integers $$$s_1, s_2, \\dots, s_n$$$ ($$$1 \\le s_i \\le 2^{29}$$$) — the description of the multiset. It is guaranteed that all elements of the multiset are powers of two. ", "output_spec": "For each query print YES if it is possible to obtain the number $$$2048$$$ in your multiset, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).", "sample_inputs": ["6\n4\n1024 512 64 512\n1\n2048\n3\n64 512 2\n2\n4096 4\n7\n2048 2 2048 2048 2048 2048 2048\n2\n2048 4096"], "sample_outputs": ["YES\nYES\nNO\nNO\nYES\nYES"], "notes": "NoteIn the first query you can win as follows: choose $$$512$$$ and $$$512$$$, and $$$s$$$ turns into $$$\\{1024, 64, 1024\\}$$$. Then choose $$$1024$$$ and $$$1024$$$, and $$$s$$$ turns into $$$\\{2048, 64\\}$$$ and you win.In the second query $$$s$$$ contains $$$2048$$$ initially."}, "positive_code": [{"source_code": "include Math\ngets.to_i.times do\n n = gets.to_i\n ary = gets.split.map(&:to_i)\n a2 = Array.new(12, 0)\n ary.each {|a| if a <= 2048 then a2[log(a, 2).to_i]+=1 end}\n for i in 0..10 do a2[i+1] += a2[i]/2 end\n puts a2[11] > 0 ? :YES: :NO\nend\n"}], "negative_code": [], "src_uid": "b40059fe9cbdb0cc3b64c3e463900849"} {"nl": {"description": "The store sells $$$n$$$ beads. The color of each bead is described by a lowercase letter of the English alphabet (\"a\"–\"z\"). You want to buy some beads to assemble a necklace from them.A necklace is a set of beads connected in a circle.For example, if the store sells beads \"a\", \"b\", \"c\", \"a\", \"c\", \"c\", then you can assemble the following necklaces (these are not all possible options): And the following necklaces cannot be assembled from beads sold in the store: The first necklace cannot be assembled because it has three beads \"a\" (of the two available). The second necklace cannot be assembled because it contains a bead \"d\", which is not sold in the store. We call a necklace $$$k$$$-beautiful if, when it is turned clockwise by $$$k$$$ beads, the necklace remains unchanged. For example, here is a sequence of three turns of a necklace. As you can see, this necklace is, for example, $$$3$$$-beautiful, $$$6$$$-beautiful, $$$9$$$-beautiful, and so on, but it is not $$$1$$$-beautiful or $$$2$$$-beautiful.In particular, a necklace of length $$$1$$$ is $$$k$$$-beautiful for any integer $$$k$$$. A necklace that consists of beads of the same color is also beautiful for any $$$k$$$.You are given the integers $$$n$$$ and $$$k$$$, and also the string $$$s$$$ containing $$$n$$$ lowercase letters of the English alphabet — each letter defines a bead in the store. You can buy any subset of beads and connect them in any order. Find the maximum length of a $$$k$$$-beautiful necklace you can assemble.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases in the test. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le n, k \\le 2000$$$). The second line of each test case contains the string $$$s$$$ containing $$$n$$$ lowercase English letters — the beads in the store. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$2000$$$.", "output_spec": "Output $$$t$$$ answers to the test cases. Each answer is a positive integer — the maximum length of the $$$k$$$-beautiful necklace you can assemble.", "sample_inputs": ["6\n6 3\nabcbac\n3 6\naaa\n7 1000\nabczgyo\n5 4\nababa\n20 10\naaebdbabdbbddaadaadc\n20 5\necbedececacbcbccbdec"], "sample_outputs": ["6\n3\n5\n4\n15\n10"], "notes": "NoteThe first test case is explained in the statement.In the second test case, a $$$6$$$-beautiful necklace can be assembled from all the letters.In the third test case, a $$$1000$$$-beautiful necklace can be assembled, for example, from beads \"abzyo\"."}, "positive_code": [{"source_code": "require 'prime'\nclass Integer\n\tdef divisors\n\t\tself.prime_division.reduce([1]) do |acc, (p, e)|\n\t\t\t(0..e).map { p**_1 }.product(acc).map { _1 * _2 }\n\t\tend.sort\n\tend\nend\n\ngets.to_i.times do\n\tn, k = gets.split.map(&:to_i)\n\ts = gets.chomp\n\tcnt = s.chars.tally\n\trindex = (1..n).reverse_each.to_a\n\tputs k.divisors.map { |d|\n\t\t\t(rindex.bsearch { |i| cnt.sum { |val, cnt| cnt / i } >= d } || 0) *\n\t\t\t\td\n\t }.max\nend\n"}], "negative_code": [{"source_code": "require 'prime'\nclass Integer\n\tdef divisors\n\t\tself.prime_division.reduce([1]) do |acc, (p, e)|\n\t\t\t(0..e).map { p**_1 }.product(acc).map { _1 * _2 }\n\t\tend.sort\n\tend\nend\n\ngets.to_i.times do\n\tn, k = gets.split.map(&:to_i)\n\ts = gets.chomp\n\tcnt = s.chars.tally\n\tputs k.divisors.map { |d|\n\t\t\t(\n\t\t\t\t(1..10).reverse_each.to_a.bsearch { |i|\n\t\t\t\t\tcnt.sum { |val, cnt| cnt / i } >= d\n\t\t\t\t} || 0\n\t\t\t) * d\n\t }.max\nend\n"}], "src_uid": "819aebac427c2c0f96e58bca60b81e33"} {"nl": {"description": "A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).Let $$$p$$$ be any permutation of length $$$n$$$. We define the fingerprint $$$F(p)$$$ of $$$p$$$ as the sorted array of sums of adjacent elements in $$$p$$$. More formally,$$$$$$F(p)=\\mathrm{sort}([p_1+p_2,p_2+p_3,\\ldots,p_{n-1}+p_n]).$$$$$$For example, if $$$n=4$$$ and $$$p=[1,4,2,3],$$$ then the fingerprint is given by $$$F(p)=\\mathrm{sort}([1+4,4+2,2+3])=\\mathrm{sort}([5,6,5])=[5,5,6]$$$.You are given a permutation $$$p$$$ of length $$$n$$$. Your task is to find a different permutation $$$p'$$$ with the same fingerprint. Two permutations $$$p$$$ and $$$p'$$$ are considered different if there is some index $$$i$$$ such that $$$p_i \\ne p'_i$$$.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 668$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2\\le n\\le 100$$$)  — the length of the permutation. The second line of each test case contains $$$n$$$ integers $$$p_1,\\ldots,p_n$$$ ($$$1\\le p_i\\le n$$$). It is guaranteed that $$$p$$$ is a permutation.", "output_spec": "For each test case, output $$$n$$$ integers $$$p'_1,\\ldots, p'_n$$$ — a permutation such that $$$p'\\ne p$$$ and $$$F(p')=F(p)$$$. We can prove that for every permutation satisfying the input constraints, a solution exists. If there are multiple solutions, you may output any.", "sample_inputs": ["3\n2\n1 2\n6\n2 1 6 5 4 3\n5\n2 4 3 1 5"], "sample_outputs": ["2 1\n1 2 5 6 3 4\n3 1 5 2 4"], "notes": "NoteIn the first test case, $$$F(p)=\\mathrm{sort}([1+2])=[3]$$$.And $$$F(p')=\\mathrm{sort}([2+1])=[3]$$$.In the second test case, $$$F(p)=\\mathrm{sort}([2+1,1+6,6+5,5+4,4+3])=\\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$$$.And $$$F(p')=\\mathrm{sort}([1+2,2+5,5+6,6+3,3+4])=\\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$$$.In the third test case, $$$F(p)=\\mathrm{sort}([2+4,4+3,3+1,1+5])=\\mathrm{sort}([6,7,4,6])=[4,6,6,7]$$$.And $$$F(p')=\\mathrm{sort}([3+1,1+5,5+2,2+4])=\\mathrm{sort}([4,6,7,6])=[4,6,6,7]$$$."}, "positive_code": [{"source_code": "gets.to_i.times do\n n=gets.to_i\n a=gets.split(\" \").map(&:to_i)\n a.reverse!\n puts a.join(\" \")\nend\n"}, {"source_code": "t = gets.to_i\nt.times do\n n = gets.to_i\n a = gets.split.map &:to_i\n puts a.reverse.join(\" \")\nend"}, {"source_code": "times = gets.to_i\n\n(1..times).each do |i|\n array_size = gets.to_i\n\n a = Array.new(array_size)\n a = gets.split(/\\s+/).reverse.join(\" \")\n puts a\nend"}, {"source_code": "gets.to_i.times{gets\nputs gets.split.reverse.join ' '}"}, {"source_code": "T = gets.to_i\n\nT.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n\n puts a.reverse.join(' ')\nend\n"}], "negative_code": [], "src_uid": "1eaff8e0ec4614753699128af74b2471"} {"nl": {"description": "Vasya bought the collected works of a well-known Berland poet Petya in n volumes. The volumes are numbered from 1 to n. He thinks that it does not do to arrange the book simply according to their order. Vasya wants to minimize the number of the disposition’s divisors — the positive integers i such that for at least one j (1 ≤ j ≤ n) is true both: j mod i = 0 and at the same time p(j) mod i = 0, where p(j) is the number of the tome that stands on the j-th place and mod is the operation of taking the division remainder. Naturally, one volume can occupy exactly one place and in one place can stand exactly one volume.Help Vasya — find the volume disposition with the minimum number of divisors.", "input_spec": "The first line contains number n (1 ≤ n ≤ 100000) which represents the number of volumes and free places.", "output_spec": "Print n numbers — the sought disposition with the minimum divisor number. The j-th number (1 ≤ j ≤ n) should be equal to p(j) — the number of tome that stands on the j-th place. If there are several solutions, print any of them.", "sample_inputs": ["2", "3"], "sample_outputs": ["2 1", "1 3 2"], "notes": null}, "positive_code": [{"source_code": "n=gets.to_i\n$><<[n,*1..n-1]*' '\n"}, {"source_code": "n=gets.to_i;$><<[n,*1...n]*' '\n"}, {"source_code": "n=gets.to_i;$><<[n,*1...n]*' '\n"}, {"source_code": "n=gets.to_i;$><<[n,*1...n]*' '\n"}, {"source_code": "n=gets.to_i;$><<[n,*1...n]*' '\n"}, {"source_code": "n=gets.to_i;$><<[n,*1...n]*' '"}, {"source_code": "n=gets.to_i;$><<[n,*1...n]*' '"}, {"source_code": "n=gets.to_i;$><<[n,*1...n]*' '\n"}, {"source_code": "n=gets.to_i;$><<[n,*1...n]*' '\n"}, {"source_code": "n=gets.to_i;$><<[n,*1...n]*' '\n"}, {"source_code": "n=gets.to_i;$><<[n,*1...n]*' '\n"}], "negative_code": [], "src_uid": "f35d7f9c12eea4364891e0449613f6b0"} {"nl": {"description": "Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \\ldots, d_n$$$ ($$$0 \\leq d_i \\leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.", "output_spec": "For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory.", "sample_inputs": ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"], "sample_outputs": ["-3\n0\n0"], "notes": "NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$."}, "positive_code": [{"source_code": "def I()gets.split.map &:to_i end\r\nI()[0].times{n,=I();a=I().sort;x=s=i=0;(x+=s-a[i]*i;s+=a[i];i+=1)while i 0)\n num = gets.chomp.to_i\n count = 0\n while(num%2 == 0 || num%3 == 0 || num%5 == 0)\n if num%2 == 0\n num /= 2;\n elsif num%3 == 0\n num = (num/3)*2;\n else\n num = (num/5)*4\n end\n count += 1\n end\n queries -= 1\n puts -1 if num != 1\n puts count if num == 1\nend\n"}, {"source_code": "gets.to_i.times do\n n = gets.to_i\n ans = 0\n while n != 1 do\n if n % 2 == 0\n n = n / 2\n ans += 1\n elsif n % 3 == 0\n n = 2 * n / 3\n ans += 1\n elsif n % 5 == 0\n n = 4 * n / 5\n ans += 1\n else\n ans = -1\n break\n end\n end\n puts ans\nend"}, {"source_code": "# coding: utf-8\n\nq = gets.to_i\nq.times {\n n = gets.to_i\n break_noops = false\n loop_counter = 0\n while n > 1\n if n%2 == 0\n n /= 2\n loop_counter += 1\n elsif n%3 == 0\n n /= 3\n n *= 2\n loop_counter += 1\n elsif n%5 == 0\n n /= 5\n n *= 4\n loop_counter += 1\n else\n break_noops = true\n break\n end\n end\n\n if n == 1\n puts loop_counter\n else\n puts -1\n end\n}"}, {"source_code": "q = gets.to_i\nq.times do\n n = gets.to_i\n p = {2 => 0, 3 => 0, 5 => 0}\n n, p[2] = n/2, p[2] + 1 while n%2 == 0\n n, p[3] = n/3, p[3] + 1 while n%3 == 0\n n, p[5] = n/5, p[5] + 1 while n%5 == 0 \n if n == 1\n puts (p[2] || 0) + (p[3] || 0)*2 + (p[5] || 0)*3\n else\n puts -1\n end\nend\n"}], "negative_code": [], "src_uid": "ed5ea0e664aa986ab86e4e6746e8a1bf"} {"nl": {"description": "There are two rival donut shops.The first shop sells donuts at retail: each donut costs $$$a$$$ dollars.The second shop sells donuts only in bulk: box of $$$b$$$ donuts costs $$$c$$$ dollars. So if you want to buy $$$x$$$ donuts from this shop, then you have to buy the smallest number of boxes such that the total number of donuts in them is greater or equal to $$$x$$$.You want to determine two positive integer values: how many donuts can you buy so that they are strictly cheaper in the first shop than in the second shop? how many donuts can you buy so that they are strictly cheaper in the second shop than in the first shop? If any of these values doesn't exist then that value should be equal to $$$-1$$$. If there are multiple possible answers, then print any of them.The printed values should be less or equal to $$$10^9$$$. It can be shown that under the given constraints such values always exist if any values exist at all.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of testcases. Each of the next $$$t$$$ lines contains three integers $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \\le a \\le 10^9$$$, $$$2 \\le b \\le 10^9$$$, $$$1 \\le c \\le 10^9$$$).", "output_spec": "For each testcase print two positive integers. For both shops print such $$$x$$$ that buying $$$x$$$ donuts in this shop is strictly cheaper than buying $$$x$$$ donuts in the other shop. $$$x$$$ should be greater than $$$0$$$ and less or equal to $$$10^9$$$. If there is no such $$$x$$$, then print $$$-1$$$. If there are multiple answers, then print any of them.", "sample_inputs": ["4\n5 10 4\n4 5 20\n2 2 3\n1000000000 1000000000 1000000000"], "sample_outputs": ["-1 20\n8 -1\n1 2\n-1 1000000000"], "notes": "NoteIn the first testcase buying any number of donuts will be cheaper in the second shop. For example, for $$$3$$$ or $$$5$$$ donuts you'll have to buy a box of $$$10$$$ donuts for $$$4$$$ dollars. $$$3$$$ or $$$5$$$ donuts in the first shop would cost you $$$15$$$ or $$$25$$$ dollars, respectively, however. For $$$20$$$ donuts you'll have to buy two boxes for $$$8$$$ dollars total. Note that $$$3$$$ and $$$5$$$ are also valid answers for the second shop, along with many other answers.In the second testcase buying any number of donuts will be either cheaper in the first shop or the same price. $$$8$$$ donuts cost $$$32$$$ dollars in the first shop and $$$40$$$ dollars in the second shop (because you have to buy two boxes). $$$10$$$ donuts will cost $$$40$$$ dollars in both shops, so $$$10$$$ is not a valid answer for any of the shops.In the third testcase $$$1$$$ donut costs $$$2$$$ and $$$3$$$ dollars, respectively. $$$2$$$ donuts cost $$$4$$$ and $$$3$$$ dollars. Thus, $$$1$$$ is a valid answer for the first shop and $$$2$$$ is a valid answer for the second shop.In the fourth testcase $$$10^9$$$ donuts cost $$$10^{18}$$$ dollars in the first shop and $$$10^9$$$ dollars in the second shop."}, "positive_code": [{"source_code": "t = gets.to_i\nt.times do\n a, b, c = gets.split.map &:to_i\n \n if c <= a or b == 1\n x = -1\n else\n x = 1\n end\n \n if a*b <= c\n y = -1\n else\n y = b\n end\n \n puts \"#{x} #{y}\"\nend"}, {"source_code": "def run\n a, b, c = gets.chomp.split(\" \").map(&:to_i)\n\n s = if a < c\n \"1 \"\n else\n \"-1 \"\n end\n\n if a * b > c\n s << \"#{b}\"\n else\n s << \"-1\"\n end\n puts s\nend\n\n\ndef main\n t = gets.chomp.to_i\n\n t.times do\n run\n end\nend\n\nmain\n"}, {"source_code": "n = gets.chomp.to_i\n(1..n).each do\n a,b,c = gets.chomp.split(\" \").map(&:to_i)\n if a < c && b >= 2\n x = 1\n end\n if a >= c && b >= 1\n x = -1\n end\n if a < c && b == 1\n x = 1\n end\n # kari = a * b\n # if kari <= c\n # y = -1\n # end\n amari = c % a\n kotae = c / a\n # if kari > c && amari == 0 \n # y = kotae\n # elsif kari > c && amari != 0\n # y = kotae + 1 \n # end\n if a >= c \n y = b\n elsif a < c && amari == 0 && b > kotae\n y = kotae + 1\n elsif a < c && amari == 0 && b == kotae\n y = -1\n elsif a < c && amari != 0 && b > kotae\n y = kotae + 1\n elsif a < c && amari != 0 && b == kotae\n y = -1\n elsif a < c && amari == 0 && b < kotae\n y = -1\n elsif a < c && amari != 0 && b < kotae\n y = -1\n end\n\n print x\n print \" \"\n print y\n print \"\\n\"\nend"}], "negative_code": [{"source_code": "n = gets.chomp.to_i\n(1..n).each do |i|\n a,b,c = gets.chomp.split(\" \").map(&:to_i)\n if a < c && b >= 2\n x = 1\n end\n if a >= c && b >= 1\n x = -1\n end\n if a < c && b == 1\n x = 1\n end\n kari = a * b\n if kari <= c\n y = -1\n end\n if kari > c\n y = a\n end\n print x\n print \" \"\n print y\n print \"\\n\"\nend\n"}, {"source_code": "n = gets.chomp.to_i\n(1..n).each do\n a,b,c = gets.chomp.split(\" \").map(&:to_i)\n if a < c && b >= 2\n x = 1\n end\n if a >= c && b >= 1\n x = -1\n end\n if a < c && b == 1\n x = 1\n end\n kari = a * b\n if kari <= c\n y = -1\n end\n if kari > c\n y = a\n end\n print x\n print \" \"\n print y\n print \"\\n\"\nend"}, {"source_code": "n = gets.chomp.to_i\n(1..n).each do\n a,b,c = gets.chomp.split(\" \").map(&:to_i)\n if a < c && b >= 2\n x = 1\n end\n if a >= c && b >= 1\n x = -1\n end\n if a < c && b == 1\n x = 1\n end\n kari = a * b\n if kari <= c\n y = -1\n end\n amari = c % a\n kotae = c / a\n if kari > c && amari == 0 \n y = kotae\n elsif kari > c && amari != 0\n y = kotae + 1 \n end\n print x\n print \" \"\n print y\n print \"\\n\"\nend"}], "src_uid": "002801f26568a1b3524d8754207d32c1"} {"nl": {"description": "Sergei B., the young coach of Pokemons, has found the big house which consists of n flats ordered in a row from left to right. It is possible to enter each flat from the street. It is possible to go out from each flat. Also, each flat is connected with the flat to the left and the flat to the right. Flat number 1 is only connected with the flat number 2 and the flat number n is only connected with the flat number n - 1.There is exactly one Pokemon of some type in each of these flats. Sergei B. asked residents of the house to let him enter their flats in order to catch Pokemons. After consulting the residents of the house decided to let Sergei B. enter one flat from the street, visit several flats and then go out from some flat. But they won't let him visit the same flat more than once. Sergei B. was very pleased, and now he wants to visit as few flats as possible in order to collect Pokemons of all types that appear in this house. Your task is to help him and determine this minimum number of flats he has to visit. ", "input_spec": "The first line contains the integer n (1 ≤ n ≤ 100 000) — the number of flats in the house. The second line contains the row s with the length n, it consists of uppercase and lowercase letters of English alphabet, the i-th letter equals the type of Pokemon, which is in the flat number i. ", "output_spec": "Print the minimum number of flats which Sergei B. should visit in order to catch Pokemons of all types which there are in the house. ", "sample_inputs": ["3\nAaA", "7\nbcAAcbc", "6\naaBCCe"], "sample_outputs": ["2", "3", "5"], "notes": "NoteIn the first test Sergei B. can begin, for example, from the flat number 1 and end in the flat number 2.In the second test Sergei B. can begin, for example, from the flat number 4 and end in the flat number 6. In the third test Sergei B. must begin from the flat number 2 and end in the flat number 6."}, "positive_code": [{"source_code": "#!/usr/bin/ruby\n\nrequire 'set'\n\nn = gets.chomp.to_i\nc = gets.chomp\n\nh = Hash.new(0)\ns = Set.new\n\nfor i in 0...n do\n s << c[i]\nend\nnd = s.size\niterr = 0\ngot = 0\nans = n\nfor iterl in 0...n do\n while iterr < n && got < nd do\n if h[ c[iterr] ] == 0\n got += 1\n end\n h[ c[iterr] ] += 1\n iterr += 1\n end\n if got < nd\n break\n end\n if iterr - iterl < ans\n ans = iterr - iterl\n end\n h[ c[iterl] ] -= 1\n if h[ c[iterl] ] == 0\n got -= 1\n end\nend\nputs ans\n"}, {"source_code": "#! ruby\n# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n############################################################\ndef gs() gets.chomp end\ndef gi() gets.to_i end\ndef gss() gs.split end\ndef gis() gss.map(&:to_i) end\ndef nmapf(n,f) n.times.map{ __send__ f } end\ndef ngs(n) nmapf n,:gs end\ndef ngi(n) nmapf n,:gi end\ndef ngss(n) nmapf n,:gss end\ndef ngis(n) nmapf n,:gis end\ndef arr2d(h,w,v=0) h.times.map{[v] * w} end\ndef for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end\ndef nsum(n) n * (n + 1) / 2 end\ndef vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end\n############################################################\n\nN = gi\nS = gs\n\nf = Hash.new 0\n\nc = S.each_char.to_a.uniq.size\n\na = N\nj = 0\nk = 0\nN.times do |i|\n ch = S[i]\n if f[ch] == 0\n k += 1\n end\n f[ch] += 1\n while f[S[j]] > 1 do\n f[S[j]] -= 1\n j += 1\n end\n a = [a, i - j].min if k == c\nend\n\np a + 1\n"}, {"source_code": "gets\nhouse = gets.chomp\n\nselected = Hash.new\nhouse.each_char { |flat| selected[flat] = 0 }\n\nif selected.length < 2\n puts selected.length\n exit\nend\n\nresult = house.length\nleft, right = 0, 0\n\nselected[house[0]] = 1\ncurrent = 1\n\nwhile true do\n if current == selected.length\n result = 1+right-left if 1+right-left < result\n\n selected[house[left]] -= 1\n current -= 1 if selected[house[left]] == 0\n left += 1\n else\n right += 1\n break if right == house.length\n current += 1 if selected[house[right]] == 0\n selected[house[right]] += 1\n end\nend\n\nputs result\n"}], "negative_code": [], "src_uid": "60776cefd6c1a2f3c16b3378ebc31a9a"} {"nl": {"description": "A superhero fights with a monster. The battle consists of rounds, each of which lasts exactly $$$n$$$ minutes. After a round ends, the next round starts immediately. This is repeated over and over again.Each round has the same scenario. It is described by a sequence of $$$n$$$ numbers: $$$d_1, d_2, \\dots, d_n$$$ ($$$-10^6 \\le d_i \\le 10^6$$$). The $$$i$$$-th element means that monster's hp (hit points) changes by the value $$$d_i$$$ during the $$$i$$$-th minute of each round. Formally, if before the $$$i$$$-th minute of a round the monster's hp is $$$h$$$, then after the $$$i$$$-th minute it changes to $$$h := h + d_i$$$.The monster's initial hp is $$$H$$$. It means that before the battle the monster has $$$H$$$ hit points. Print the first minute after which the monster dies. The monster dies if its hp is less than or equal to $$$0$$$. Print -1 if the battle continues infinitely.", "input_spec": "The first line contains two integers $$$H$$$ and $$$n$$$ ($$$1 \\le H \\le 10^{12}$$$, $$$1 \\le n \\le 2\\cdot10^5$$$). The second line contains the sequence of integers $$$d_1, d_2, \\dots, d_n$$$ ($$$-10^6 \\le d_i \\le 10^6$$$), where $$$d_i$$$ is the value to change monster's hp in the $$$i$$$-th minute of a round.", "output_spec": "Print -1 if the superhero can't kill the monster and the battle will last infinitely. Otherwise, print the positive integer $$$k$$$ such that $$$k$$$ is the first minute after which the monster is dead.", "sample_inputs": ["1000 6\n-100 -200 -300 125 77 -4", "1000000000000 5\n-1 0 0 0 0", "10 4\n-3 -6 5 4"], "sample_outputs": ["9", "4999999999996", "-1"], "notes": null}, "positive_code": [{"source_code": "H,N = gets.split.map &:to_i\nD = gets.split.map &:to_i\nss = [0]\n(1..N).each{ |i|\n d = D[i-1]\n ss << (ss[-1] + d)\n if ss[-1] + H <= 0\n p i; exit\n end\n}\n\nif ss[-1] >= 0\n p -1; exit\nend\n\ntd = ss[-1]\nt = nil\nk = (0..H/(-td)).bsearch{ |n|\n h0 = H + n*td\n h = h0\n j = 0\n ss.each{ |s|\n h = h0 + s\n break if h <= 0\n j += 1\n }\n t = j if h <= 0\n h <= 0\n}\np N*k + t\n"}, {"source_code": "h,n = gets.split.map(&:to_i)\nd = gets.split.map(&:to_i)\nsum = d.inject(:+)\na = 0\nmn = 0\nn.times do |i|\n a += d[i]\n mn = a if mn > a\nend\nif sum >= 0 && h+mn > 0\n puts -1\n exit\nend\nans = 0\nif h+mn > 0\n ans += (h+mn-sum-1)/(-sum)*n\n h += ans/n*sum\nend\nn.times do |i|\n h += d[i]\n ans += 1\n if h <= 0\n puts ans\n exit\n end\nend"}], "negative_code": [], "src_uid": "d173fa24cebaeda9ca794eeed68fa12d"} {"nl": {"description": "Companies always have a lot of equipment, furniture and other things. All of them should be tracked. To do this, there is an inventory number assigned with each item. It is much easier to create a database by using those numbers and keep the track of everything.During an audit, you were surprised to find out that the items are not numbered sequentially, and some items even share the same inventory number! There is an urgent need to fix it. You have chosen to make the numbers of the items sequential, starting with 1. Changing a number is quite a time-consuming process, and you would like to make maximum use of the current numbering.You have been given information on current inventory numbers for n items in the company. Renumber items so that their inventory numbers form a permutation of numbers from 1 to n by changing the number of as few items as possible. Let us remind you that a set of n numbers forms a permutation if all the numbers are in the range from 1 to n, and no two numbers are equal.", "input_spec": "The first line contains a single integer n — the number of items (1 ≤ n ≤ 105). The second line contains n numbers a1, a2, ..., an (1 ≤ ai ≤ 105) — the initial inventory numbers of the items.", "output_spec": "Print n numbers — the final inventory numbers of the items in the order they occur in the input. If there are multiple possible answers, you may print any of them.", "sample_inputs": ["3\n1 3 2", "4\n2 2 3 3", "1\n2"], "sample_outputs": ["1 3 2", "2 1 3 4", "1"], "notes": "NoteIn the first test the numeration is already a permutation, so there is no need to change anything.In the second test there are two pairs of equal numbers, in each pair you need to replace one number.In the third test you need to replace 2 by 1, as the numbering should start from one."}, "positive_code": [{"source_code": "n = gets.chomp.to_i\narr = gets.chomp.split.map{|x| x.to_i}\n\nref = Array.new(n+1){0}\n\narr.each do |v|\n\tif v<=n\n\t\tref[v]+=1\n\tend\nend\nstack = []\n(1..n).each do |i|\n\tif ref[i] == 0\n\tstack.push(i)\n\tend\nend\n\narr.each do |v|\n\tif v>n\n\t\tprint \"#{stack.pop} \"\n\telsif ref[v] > 1\n\t\tprint \"#{stack.pop} \"\n\t\tref[v]-=1\n\telse\n\t\t\tprint \"#{v} \"\n\tend\nend\n"}, {"source_code": "require 'set'\nn = STDIN.gets.chomp\ninput = STDIN.gets.chomp\n\ndef remap(n, input)\n order = Set.new\n n.to_i.times {|n| order << n+1}\n\n\n input = input.split.map(&:to_i)\n output = []\n\n reworked_input = []\n positions = []\n\n input.length.times do |position|\n key = input[position]\n unless order.include? key\n positions << position\n else\n order.delete key\n end\n end\n\n counter = 0\n\n order.each do |element|\n input[positions[counter]] = element\n counter += 1\n end\n\n return input.join(\" \")\nend\n\nputs remap(n, input)\n"}, {"source_code": "#!/usr/bin/ruby\nN=gets.to_i\nA=[]\nh={}\ngets.split.each_with_index{|e,i|\n\tn=e.to_i-1\n\tif !A[n]&&n n\n stack.pop\n when array[e] > 1\n array[e] -= 1\n stack.pop\n else\n e\n end\nend\n\nputs ans.join(' ')\n"}, {"source_code": "def read_ints; gets.chomp.split.map(&:to_i) end\nn = read_ints[0]\na_elems = read_ints\nbad_a_elems = Array.new(n)\npositions = Array.new(n)\na_elems.each_with_index do |a,i|\n\ta-=1\n\tif positions[a].nil? && a nums.size\n\t\tres[i] = free[f]\n\t\tf += 1\n\telse\n\t\tres[i] = n\n\t\tused[n] = true\n\tend\nend\n\nputs res.join(' ')"}, {"source_code": "n = gets.chomp.to_i\nas = gets.chomp.split(' ').map(&:to_i)\n\nbs = Array.new(n+1, false)\ncs = Array.new(n+1, false)\n\nfor i in 0..n-1\n a = as[i]\n if 1 <= a && a <= n && !bs[a] \n bs[a] = true\n cs[i] = true\n end\nend\n\nj = 1\nfor i in 0..n-1\n a = as[i]\n while bs[j]\n j += 1\n end\n if cs[i]\n print as[i]\n else\n print j \n j += 1\n end\n print ' '\nend\nprint \"\\n\"\n"}, {"source_code": "#!/usr/bin/ruby\n\n\nn = ARGF.gets.to_i \na = ARGF.gets.split(' ').map { |s| s.to_i }\n\nb = Array.new(n + 1, 0)\nb[0] = 1\n\na.each do |e|\n if e <= n then b[e] += 1 end\nend\n\nc = Array.new\nb.each_with_index do |e, i|\n if b[i] == 0 then c << i end\nend\n\na.each_with_index do |e, i|\n if a[i] > n\n a[i] = c.shift\n elsif b[a[i]] > 1\n b[a[i]] -= 1\n a[i] = c.shift\n end\nend\n\nprint a.shift\na.each do |e| print(\" #{e}\") end\nprint \"\\n\"\n"}], "negative_code": [{"source_code": "#n = STDIN.gets.chomp\n#input = STDIN.gets.chomp\n\ndef remap(n, input)\n order = []\n n.to_i.times {|n| order << n+1}\n\n input = input.split.map(&:to_i)\n output = []\n\n reworked_input = []\n\n input.each do |key|\n unless order.include? key\n reworked_input << [key, false]\n else\n index = order.index key\n order[index] = nil\n reworked_input << [key, true]\n end\n end\n\n order = order.compact\n counter = 0\n\n reworked_input.each do |element|\n key, value = element\n if value == false\n output << order[counter]\n counter += 1\n else\n output << key\n end\n end\n\n\n return output.join(\" \")\nend\n\n#puts remap(n, input)\n"}, {"source_code": "#!/usr/bin/ruby\nN=gets.to_i\nA=[]\nh={}\ngets.split.each_with_index{|e,i|\n\tn=e.to_i-1\n\tif !A[n]&&n= nums.size\n\t\tres[i] = free[f]\n\t\tf += 1\n\telse\n\t\tres[i] = n\n\t\tused[n] = true\n\tend\nend\n\nputs res.join(' ')"}, {"source_code": "#!/usr/bin/ruby\n\n\nn = ARGF.gets.to_i \na = ARGF.gets.split(' ').map { |s| s.to_i }\n\nb = Array.new(n + 1, 0)\nb[0] = 1\n\na.each do |e|\n if e < n then b[e] += 1 end\nend\n\nc = Array.new\nb.each_with_index do |e, i|\n if b[i] == 0 then c << i end\nend\n\na.each_with_index do |e, i|\n if a[i] > n\n a[i] = c.shift\n elsif b[a[i]] > 1\n b[a[i]] -= 1\n a[i] = c.shift\n end\nend\n\nprint a.shift\na.each do |e| print(\" #{e}\") end\nprint \"\\n\"\n"}], "src_uid": "1cfd0e6504bba7db9ec79e2f243b99b4"} {"nl": {"description": "Anton likes to play chess. Also, he likes to do programming. That is why he decided to write the program that plays chess. However, he finds the game on 8 to 8 board to too simple, he uses an infinite one instead.The first task he faced is to check whether the king is in check. Anton doesn't know how to implement this so he asks you to help.Consider that an infinite chess board contains one white king and the number of black pieces. There are only rooks, bishops and queens, as the other pieces are not supported yet. The white king is said to be in check if at least one black piece can reach the cell with the king in one move. Help Anton and write the program that for the given position determines whether the white king is in check.Remainder, on how do chess pieces move: Bishop moves any number of cells diagonally, but it can't \"leap\" over the occupied cells. Rook moves any number of cells horizontally or vertically, but it also can't \"leap\" over the occupied cells. Queen is able to move any number of cells horizontally, vertically or diagonally, but it also can't \"leap\". ", "input_spec": "The first line of the input contains a single integer n (1 ≤ n ≤ 500 000) — the number of black pieces. The second line contains two integers x0 and y0 ( - 109 ≤ x0, y0 ≤ 109) — coordinates of the white king. Then follow n lines, each of them contains a character and two integers xi and yi ( - 109 ≤ xi, yi ≤ 109) — type of the i-th piece and its position. Character 'B' stands for the bishop, 'R' for the rook and 'Q' for the queen. It's guaranteed that no two pieces occupy the same position.", "output_spec": "The only line of the output should contains \"YES\" (without quotes) if the white king is in check and \"NO\" (without quotes) otherwise.", "sample_inputs": ["2\n4 2\nR 1 1\nB 1 5", "2\n4 2\nR 3 3\nB 1 5"], "sample_outputs": ["YES", "NO"], "notes": "NotePicture for the first sample: White king is in check, because the black bishop can reach the cell with the white king in one move. The answer is \"YES\".Picture for the second sample: Here bishop can't reach the cell with the white king, because his path is blocked by the rook, and the bishop cant \"leap\" over it. Rook can't reach the white king, because it can't move diagonally. Hence, the king is not in check and the answer is \"NO\"."}, "positive_code": [{"source_code": "def r \n gets.split.map(&:to_i) \nend\ndef rook(p)\n \"RQ\".match(p[1])\nend\ndef bish(p)\n \"BQ\".match(p[1])\nend\ngets\nkx, ky = r\n\nrl = cl = d1l = d2l = [-1e10, \"dummy\"]\nrh = ch = d1h = d2h = [+1e10, \"dummy\"]\n\n$<.each_line do |l|\n op, x, y = l.split\n x = x.to_i\n y = y.to_i\n \n rl = [rl, [y, op]].max if x == kx && y < ky\n rh = [rh, [y, op]].min if x == kx && y > ky\n cl = [cl, [x, op]].max if y == ky && x < kx\n ch = [ch, [x, op]].min if y == ky && x > kx\n \n d1l = [d1l, [x, op]].max if x+y == kx+ky && x < kx\n d1h = [d1h, [x, op]].min if x+y == kx+ky && x > kx\n d2l = [d2l, [x, op]].max if x-y == kx-ky && x < kx\n d2h = [d2h, [x, op]].min if x-y == kx-ky && x > kx\nend\nputs [rl, rh, cl, ch].any?{|x| rook(x)} || [d1l, d1h, d2l, d2h].any?{|x| bish(x)} ? \"YES\" : \"NO\"\n"}, {"source_code": "def r\n gets.split.map(&:to_i)\nend\ndef rook(p)\n \"RQ\".match(p[1])\nend\ndef bish(p)\n \"BQ\".match(p[1])\nend\ngets\nkx, ky = r\n\nrl = cl = d1l = d2l = [-1e10, \"dummy\"]\nrh = ch = d1h = d2h = [+1e10, \"dummy\"]\n\n$<.each_line do |l|\n op, x, y = l.split\n x = x.to_i\n y = y.to_i\n\n if 1\n rl = [rl, [y, op]].max if x == kx && y < ky\n end\n if 1\n rh = [rh, [y, op]].min if x == kx && y > ky\n end\n if 1\n cl = [cl, [x, op]].max if y == ky && x < kx\n end\n if 1\n ch = [ch, [x, op]].min if y == ky && x > kx\n end\n\n d1l = [d1l, [x, op]].max if x+y == kx+ky && x < kx\n d1h = [d1h, [x, op]].min if x+y == kx+ky && x > kx\n d2l = [d2l, [x, op]].max if x-y == kx-ky && x < kx\n d2h = [d2h, [x, op]].min if x-y == kx-ky && x > kx\nend\nputs [rl, rh, cl, ch].any?{|x| rook(x)} || [d1l, d1h, d2l, d2h].any?{|x| bish(x)} ? \"YES\" : \"NO\"\n"}, {"source_code": "vert = []\nhori = []\npsdiag = []\nnsdiag = []\n\nn = gets.to_i\nkx, ky = gets.strip.split.map(&:to_i)\n\npieces = []\nn.times do |i|\n p, x, y = gets.strip.split\n x = x.to_i\n y = y.to_i\n pieces << [p, x, y]\n vert << i if x == kx\n hori << i if y == ky\n psdiag << i if (y-x) == (ky-kx)\n nsdiag << i if (y+x) == (ky+kx)\nend\n\nunder_attack = false\n\nnear_top = [10**10, nil]\nnear_bot = [-1 * (10**10), nil]\nvert.each do |pi|\n p = pieces[pi]\n if p[2] > ky && p[2] < near_top[0]\n near_top[0] = p[2]\n near_top[1] = pi\n end\n if p[2] < ky && p[2] > near_bot[0]\n near_bot[0] = p[2]\n near_bot[1] = pi\n end\nend\nunder_attack ||= pieces[near_top[1]][0] == \"R\" || pieces[near_top[1]][0] == \"Q\" if near_top[1]\nunder_attack ||= pieces[near_bot[1]][0] == \"R\" || pieces[near_bot[1]][0] == \"Q\" if near_bot[1]\n\nif under_attack == false\n near_rgt = [10**10, nil]\n near_lft = [-1 * (10**10), nil]\n hori.each do |pi|\n p = pieces[pi]\n if p[1] > kx && p[1] < near_rgt[0]\n near_rgt[0] = p[1]\n near_rgt[1] = pi\n end\n if p[1] < kx && p[1] > near_lft[0]\n near_lft[0] = p[1]\n near_lft[1] = pi\n end\n end\n under_attack ||= pieces[near_rgt[1]][0] == \"R\" || pieces[near_rgt[1]][0] == \"Q\" if near_rgt[1]\n under_attack ||= pieces[near_lft[1]][0] == \"R\" || pieces[near_lft[1]][0] == \"Q\" if near_lft[1]\nend\n\nif under_attack == false\n near_tr = [10**10, nil]\n near_bl = [-1 * (10**10), nil]\n psdiag.each do |pi|\n p = pieces[pi]\n if p[2] > ky && p[2] < near_tr[0]\n near_tr[0] = p[2]\n near_tr[1] = pi\n end\n if p[2] < ky && p[2] > near_bl[0]\n near_bl[0] = p[2]\n near_bl[1] = pi\n end\n end\n under_attack ||= pieces[near_tr[1]][0] == \"B\" || pieces[near_tr[1]][0] == \"Q\" if near_tr[1]\n under_attack ||= pieces[near_bl[1]][0] == \"B\" || pieces[near_bl[1]][0] == \"Q\" if near_bl[1]\nend\n\nif under_attack == false\n near_br = [10**10, nil]\n near_bl = [-1 * (10**10), nil]\n nsdiag.each do |pi|\n p = pieces[pi]\n if p[1] > kx && p[1] < near_br[0]\n near_br[0] = p[1]\n near_br[1] = pi\n end\n if p[1] < kx && p[1] > near_bl[0]\n near_bl[0] = p[1]\n near_bl[1] = pi\n end\n end\n under_attack ||= pieces[near_br[1]][0] == \"B\" || pieces[near_br[1]][0] == \"Q\" if near_br[1]\n under_attack ||= pieces[near_bl[1]][0] == \"B\" || pieces[near_bl[1]][0] == \"Q\" if near_bl[1]\nend\n\nputs (under_attack ? \"YES\" : \"NO\")"}, {"source_code": "# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n############################################################\ndef gs() gets.strip end\ndef gi() gets.to_i end\ndef gss() gs.split end\ndef gis() gss.map(&:to_i) end\ndef nmapf(n,f) n.times.map{ __send__ f } end\ndef ngs(n) nmapf n,:gs end\ndef ngi(n) nmapf n,:gi end\ndef ngss(n) nmapf n,:gss end\ndef ngis(n) nmapf n,:gis end\ndef arr2d(h,w,v=0) h.times.map{[v] * w} end\ndef for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end\ndef nsum(n) n * (n + 1) / 2 end\ndef vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end\n############################################################\n\nn = gi\nx, y = gis\n\nps = [nil] * 8\n\nn.times do\n c, xi_s, yi_s = gss\n xi, yi = xi_s.to_i, yi_s.to_i\n dx = xi - x\n dy = yi - y \n d = dx.abs + dy.abs\n if x == xi\n if y < yi && (ps[6].nil? || d < ps[6][1])\n ps[6] = [c, d]\n elsif y > yi && (ps[1].nil? || d < ps[1][1])\n ps[1] = [c, d]\n end\n elsif y == yi\n if x < xi && (ps[4].nil? || d < ps[4][1])\n ps[4] = [c, d]\n elsif x > xi && (ps[3].nil? || d < ps[3][1])\n ps[3] = [c, d]\n end\n elsif dx.abs == dy.abs\n if dy < 0 && dx < 0 && (ps[0].nil? || d < ps[0][1])\n ps[0] = [c, d]\n elsif dy < 0 && dx > 0 && (ps[2].nil? || d < ps[2][1])\n ps[2] = [c, d]\n elsif dy > 0 && dx < 0 && (ps[5].nil? || d < ps[5][1])\n ps[5] = [c, d]\n elsif dy > 0 && dx > 0 && (ps[7].nil? || d < ps[7][1])\n ps[7] = [c, d]\n end\n end\nend\n\ncase\nwhen !ps[1].nil? && ps[1][0] != 'B'\n puts :YES\nwhen !ps[3].nil? && ps[3][0] != 'B'\n puts :YES\nwhen !ps[4].nil? && ps[4][0] != 'B'\n puts :YES\nwhen !ps[6].nil? && ps[6][0] != 'B'\n puts :YES\nwhen !ps[0].nil? && ps[0][0] != 'R'\n puts :YES\nwhen !ps[2].nil? && ps[2][0] != 'R'\n puts :YES\nwhen !ps[5].nil? && ps[5][0] != 'R'\n puts :YES\nwhen !ps[7].nil? && ps[7][0] != 'R'\n puts :YES\nelse\n puts :NO\nend\n\n"}, {"source_code": "def r \n gets.split.map(&:to_i) \nend\ndef rook(p)\n \"RQ\".match(p[1])\nend\ndef bish(p)\n \"BQ\".match(p[1])\nend\ngets\nkx, ky = r\n\nrl = cl = d1l = d2l = [-1e10, \"dummy\"]\nrh = ch = d1h = d2h = [+1e10, \"dummy\"]\n\n$<.each_line do |l|\n op, x, y = l.split\n x = x.to_i\n y = y.to_i\n \n rl = [rl, [y, op]].max if x == kx && y < ky\n rh = [rh, [y, op]].min if x == kx && y > ky\n cl = [cl, [x, op]].max if y == ky && x < kx\n ch = [ch, [x, op]].min if y == ky && x > kx\n \n d1l = [d1l, [x, op]].max if x+y == kx+ky && x < kx\n d1h = [d1h, [x, op]].min if x+y == kx+ky && x > kx\n d2l = [d2l, [x, op]].max if x-y == kx-ky && x < kx\n d2h = [d2h, [x, op]].min if x-y == kx-ky && x > kx\nend\nputs [rl, rh, cl, ch].any?{|x| rook(x)} || [d1l, d1h, d2l, d2h].any?{|x| bish(x)} ? \"YES\" : \"NO\""}, {"source_code": "def r \n gets.split.map(&:to_i) \nend\ndef rook(p)\n \"RQ\".match(p[1])\nend\ndef bish(p)\n \"BQ\".match(p[1])\nend\ngets\nkx, ky = r\n\nrl = cl = d1l = d2l = [-1e10, \"dummy\"]\nrh = ch = d1h = d2h = [+1e10, \"dummy\"]\n\n$<.each_line do |l|\n op, x, y = l.split\n x = x.to_i\n y = y.to_i\n \n if x == kx\n rl = [rl, [y, op]].max if y < ky\n rh = [rh, [y, op]].min if y > ky\n end\n if y == ky\n cl = [cl, [x, op]].max if x < kx\n ch = [ch, [x, op]].min if x > kx\n end\n if x+y == kx+ky\n d1l = [d1l, [x, op]].max if x < kx\n d1h = [d1h, [x, op]].min if x > kx\n end\n if x-y == kx-ky\n d2l = [d2l, [x, op]].max if x < kx\n d2h = [d2h, [x, op]].min if x > kx\n end\nend\nputs [rl, rh, cl, ch].any?{|x| rook(x)} || [d1l, d1h, d2l, d2h].any?{|x| bish(x)} ? \"YES\" : \"NO\""}, {"source_code": "def r \n gets.split.map(&:to_i) \nend\ndef rook(p)\n \"RQ\".match(p[1])\nend\ndef bish(p)\n \"BQ\".match(p[1])\nend\ngets\nkx, ky = r\n\nrl = cl = d1l = d2l = [-1e10, \"dummy\"]\nrh = ch = d1h = d2h = [+1e10, \"dummy\"]\n\n$<.each_line do |l|\n op, x, y = l.split\n x = x.to_i\n y = y.to_i\n \n if x == kx\n rl = [rl, [y, op]].max if y < ky\n rh = [rh, [y, op]].min if y > ky\n end\n if y == ky\n cl = [cl, [x, op]].max if x < kx\n ch = [ch, [x, op]].min if x > kx\n end\n if x+y == kx+ky\n d1l = [d1l, [x, op]].max if x < kx\n d1h = [d1h, [x, op]].min if x > kx\n end\n if x-y == kx-ky\n d2l = [d2l, [x, op]].max if x < kx\n d2h = [d2h, [x, op]].min if x > kx\n end\nend\nputs [rl, rh, cl, ch].any?{|x| rook(x)} || [d1l, d1h, d2l, d2h].any?{|x| bish(x)} ? \"YES\" : \"NO\"\n"}, {"source_code": "def r \n gets.split.map(&:to_i) \nend\ndef rook(p)\n \"RQ\".match(p[1])\nend\ndef bish(p)\n \"BQ\".match(p[1])\nend\ngets\nkx, ky = r\n\nrl = cl = d1l = d2l = [-1e10, \"dummy\"]\nrh = ch = d1h = d2h = [+1e10, \"dummy\"]\n\n$<.each_line do |l|\n op, x, y = l.split\n x = x.to_i\n y = y.to_i\n \n rl = [rl, [y, op]].max if x == kx && y < ky\n rh = [rh, [y, op]].min if x == kx && y > ky\n cl = [cl, [x, op]].max if y == ky && x < kx\n ch = [ch, [x, op]].min if y == ky && x > kx\n \n d1l = [d1l, [x, op]].max if x+y == kx+ky && x < kx\n d1h = [d1h, [x, op]].min if x+y == kx+ky && x > kx\n d2l = [d2l, [x, op]].max if x-y == kx-ky && x < kx\n d2h = [d2h, [x, op]].min if x-y == kx-ky && x > kx\nend\nputs [rl, rh, cl, ch].any?{|x| rook(x)} || [d1l, d1h, d2l, d2h].any?{|x| bish(x)} ? \"YES\" : \"NO\""}, {"source_code": "def r \n gets.split.map(&:to_i) \nend\ndef rook(p)\n \"RQ\".match(p[1])\nend\ndef bish(p)\n \"BQ\".match(p[1])\nend\ngets\nkx, ky = r\n\nrl = cl = d1l = d2l = [-1e10, \"dummy\"]\nrh = ch = d1h = d2h = [+1e10, \"dummy\"]\n\n$<.each_line do |l|\n op, x, y = l.split\n x = x.to_i\n y = y.to_i\n \n rl = [rl, [y, op]].max if x == kx && y < ky\n rh = [rh, [y, op]].min if x == kx && y > ky\n cl = [cl, [x, op]].max if y == ky && x < kx\n ch = [ch, [x, op]].min if y == ky && x > kx\n \n d1l = [d1l, [x, op]].max if x+y == kx+ky && x < kx\n d1h = [d1h, [x, op]].min if x+y == kx+ky && x > kx\n d2l = [d2l, [x, op]].max if x-y == kx-ky && x < kx\n d2h = [d2h, [x, op]].min if x-y == kx-ky && x > kx\nend\nputs [rl, rh, cl, ch].any?{|x| rook(x)} || [d1l, d1h, d2l, d2h].any?{|x| bish(x)} ? \"YES\" : \"NO\"\n"}, {"source_code": "def r \n gets.split.map(&:to_i) \nend\ndef rook(o)\n \"RQ\".match(o)\nend\ndef bish(o)\n \"BQ\".match(o)\nend\ngets\nkx, ky = r\n\nrl = cl = d1l = d2l = [-1e10, \"dummy\"]\nrh = ch = d1h = d2h = [+1e10, \"dummy\"]\n\n$<.each_line do |l|\n op, x, y = l.split\n x = x.to_i\n y = y.to_i\n \n if x == kx\n rl = [rl, [y, op]].max if y < ky\n rh = [rh, [y, op]].min if y > ky\n end\n if y == ky\n cl = [cl, [x, op]].max if x < kx\n ch = [ch, [x, op]].min if x > kx\n end\n if x+y == kx+ky\n d1l = [d1l, [x, op]].max if x < kx\n d1h = [d1h, [x, op]].min if x > kx\n end\n if x-y == kx-ky\n d2l = [d2l, [x, op]].max if x < kx\n d2h = [d2h, [x, op]].min if x > kx\n end\nend\nputs [rl, rh, cl, ch].any?{|x,o| rook(o)} || [d1l, d1h, d2l, d2h].any?{|x,o| bish(o)} ? \"YES\" : \"NO\"\n"}], "negative_code": [], "src_uid": "b389750613e9577b3abc9e5e5902b2db"} {"nl": {"description": "Today Sonya learned about long integers and invited all her friends to share the fun. Sonya has an initially empty multiset with integers. Friends give her t queries, each of one of the following type:  +  ai — add non-negative integer ai to the multiset. Note, that she has a multiset, thus there may be many occurrences of the same integer.  -  ai — delete a single occurrence of non-negative integer ai from the multiset. It's guaranteed, that there is at least one ai in the multiset. ? s — count the number of integers in the multiset (with repetitions) that match some pattern s consisting of 0 and 1. In the pattern, 0 stands for the even digits, while 1 stands for the odd. Integer x matches the pattern s, if the parity of the i-th from the right digit in decimal notation matches the i-th from the right digit of the pattern. If the pattern is shorter than this integer, it's supplemented with 0-s from the left. Similarly, if the integer is shorter than the pattern its decimal notation is supplemented with the 0-s from the left. For example, if the pattern is s = 010, than integers 92, 2212, 50 and 414 match the pattern, while integers 3, 110, 25 and 1030 do not.", "input_spec": "The first line of the input contains an integer t (1 ≤ t ≤ 100 000) — the number of operation Sonya has to perform. Next t lines provide the descriptions of the queries in order they appear in the input file. The i-th row starts with a character ci — the type of the corresponding operation. If ci is equal to '+' or '-' then it's followed by a space and an integer ai (0 ≤ ai < 1018) given without leading zeroes (unless it's 0). If ci equals '?' then it's followed by a space and a sequence of zeroes and onse, giving the pattern of length no more than 18. It's guaranteed that there will be at least one query of type '?'. It's guaranteed that any time some integer is removed from the multiset, there will be at least one occurrence of this integer in it.", "output_spec": "For each query of the third type print the number of integers matching the given pattern. Each integer is counted as many times, as it appears in the multiset at this moment of time.", "sample_inputs": ["12\n+ 1\n+ 241\n? 1\n+ 361\n- 241\n? 0101\n+ 101\n? 101\n- 101\n? 101\n+ 4000\n? 0", "4\n+ 200\n+ 200\n- 200\n? 0"], "sample_outputs": ["2\n1\n2\n1\n1", "1"], "notes": "NoteConsider the integers matching the patterns from the queries of the third type. Queries are numbered in the order they appear in the input. 1 and 241. 361. 101 and 361. 361. 4000. "}, "positive_code": [{"source_code": "n = gets.to_i\nh = Hash.new(0)\nn.times do |i|\n x, num = gets.split(\" \")\n a = num.tr('0-9', '0101010101').to_i\n if x.ord == 43\n h[a] += 1\n elsif x.ord == 45\n h[a] -= 1\n elsif x.ord == 63\n puts h[a]\n end\nend\n"}, {"source_code": "h={}\nn=gets.to_i\nb=[*$<.read.split].reverse\nn.times{\n a, c=b.pop(2)\n if c==??\n puts h[a.to_i(2)]||0\n else\n t=a.bytes.map{|e| e%2==0? ?0 : ?1}*''\n x=t.to_i(2)\n h[x]||=0\n c==?+ ? h[x]+=1 : h[x]-=1\n end\n}\n"}, {"source_code": "h={}\nn=gets.to_i\nn.times{\n c, a=gets.split\n if c==??\n puts h[a.to_i(2)]||0\n else\n t=a.bytes.map{|e| e%2==0? ?0 : ?1}*''\n x=t.to_i(2)\n h[x]||=0\n c==?+ ? h[x]+=1 : h[x]-=1\n end\n}\n"}, {"source_code": "h={}\nn,*b=$<.read.split\nn.to_i.times{|i|\n c, a=b[2*i, 2]\n if c==??\n puts h[a.to_i(2)]||0\n else\n t=a.bytes.map{|e| e%2==0? ?0 : ?1}*''\n x=t.to_i(2)\n h[x]||=0\n c==?+ ? h[x]+=1 : h[x]-=1\n end\n}\n"}, {"source_code": "n = gets.to_i\ncnt = Array.new(2 ** 18, 0)\nn.times do\n c, s = gets.split\n a = s.tr('0-9', '0101010101').to_i(2)\n case c\n when '+'\n cnt[a] += 1\n when '-'\n cnt[a] -= 1\n when '?'\n puts cnt[a]\n end\nend\n"}, {"source_code": "n = gets.to_i\nh = Hash.new(0)\nn.times.map do |i|\n x, s = gets.split\n s = s.tr('13579', '1').tr('24680', '0')\n case x\n when '+'\n h[s.rjust(18, '0')] += 1\n when '-'\n h[s.rjust(18, '0')] -= 1\n when '?'\n puts h[s.rjust(18, '0')]\n end\nend\n"}, {"source_code": "n = gets.to_i\nh = Hash.new(0)\nn.times.map do |i|\n x, s = gets.split\n a = s.tr('0-9', '0101010101').to_i\n case x\n when '+'\n h[a] += 1\n when '-'\n h[a] -= 1\n when '?'\n puts h[a]\n end\nend\n"}, {"source_code": "n = readline.to_i\nh = {}\nh.default = 0\n(1..n).each do\n q_type, q_key = readline.split.map(&:strip)\n\n t=0\n (0...q_key.size).each do |i|\n t *= 2\n t += q_key[i].ord % 2\n end\n\n if q_type == '+'\n h[t] += 1\n elsif q_type == '-'\n h[t] -= 1\n else\n puts h[t]\n end\nend\n"}, {"source_code": "n = gets.to_i\nh = Hash.new(0)\nn.times do |i|\n x, num = gets.split(\" \")\n a = num.tr('0-9', '0101010101').to_i\n if x.ord == 43\n h[a] += 1\n elsif x.ord == 45\n h[a] -= 1\n elsif x.ord == 63\n puts h[a]\n end\nend\n"}], "negative_code": [{"source_code": "h={}\nn=gets.to_i\nn.times{\n c, a=gets.split\n if c==??\n p h[a.to_i(2)]\n else\n t=a.bytes.map{|e| e%2==0? ?0 : ?1}*''\n x=t.to_i(2)\n h[x]||=0\n c==?+ ? h[x]+=1 : h[x]-=1\n end\n}\n"}, {"source_code": "h={}\ngets.to_i.times{\n c, a=gets.split\n if c==??\n p h[a.to_i(2)]\n else\n t=a.bytes.map{|e| e%2==0? ?0 : ?1}*''\n x=t.to_i(2)\n h[x]||=0\n c==?+ ? h[x]+=1 : h[x]-=1\n end\n}\n"}, {"source_code": "h={}\nn=gets.to_i\nb=[*$<.read.split].reverse\nn.times{\n a, c=b.pop(2)\n if c==??\n puts h[a]||0\n else\n x=a.bytes.map{|e| e%2==0? ?0 : ?1}*''\n h[x]||=0\n c==?+ ? h[x]+=1 : h[x]-=1\n end\n}\n"}, {"source_code": "n = gets.to_i\nh = Hash.new(0)\nn.times.map do |i|\n x, s = gets.split\n s = s.tr('13579', '1').tr('24680', '1')\n case x\n when '+'\n h[s.rjust(18, '0')] += 1\n when '-'\n h[s.rjust(18, '0')] -= 1\n when '?'\n puts h[s.rjust(18, '0')]\n end\nend\n"}, {"source_code": "n = gets.to_i\nh = Hash.new(0)\nn.times do |x|\n c, n = gets.split\n case c\n when '+'\n h[n] += 1\n when '-'\n h[n] -= 1\n when '?'\n a = n.chars.map do |x|\n if x == '1'\n \"[1|3|5|7]\"\n elsif x == '0'\n \"[2|4|6|8|0]\"\n end\n end.join\n a << '$'\n puts h.keys.select{|x| x.rjust(5, '0').match(Regexp.new(a))}.inject(0){|s, x| s + h[x]}\n end\nend\n"}, {"source_code": "n = gets.to_i\nh = Hash.new(0)\nn.times do |x|\n c, n = gets.split\n case c\n when '+'\n h[n] += 1\n when '-'\n h[n] -= 1\n when '?'\n a = n.chars.map{|x| x == '1' ? '[1|3|5|7|9]' : '[2|4|6|8|0]'}.join\n a << '$'\n puts h.keys.select{|x| x.rjust(18, '0').match(Regexp.new(a))}.inject(0){|s, x| s + h[x]}\n end\nend\n"}], "src_uid": "0ca6c7ff6a2d110a8f4153717910378f"} {"nl": {"description": "New Year is coming! Vasya has prepared a New Year's verse and wants to recite it in front of Santa Claus.Vasya's verse contains $$$n$$$ parts. It takes $$$a_i$$$ seconds to recite the $$$i$$$-th part. Vasya can't change the order of parts in the verse: firstly he recites the part which takes $$$a_1$$$ seconds, secondly — the part which takes $$$a_2$$$ seconds, and so on. After reciting the verse, Vasya will get the number of presents equal to the number of parts he fully recited.Vasya can skip at most one part of the verse while reciting it (if he skips more than one part, then Santa will definitely notice it).Santa will listen to Vasya's verse for no more than $$$s$$$ seconds. For example, if $$$s = 10$$$, $$$a = [100, 9, 1, 1]$$$, and Vasya skips the first part of verse, then he gets two presents.Note that it is possible to recite the whole verse (if there is enough time). Determine which part Vasya needs to skip to obtain the maximum possible number of gifts. If Vasya shouldn't skip anything, print 0. If there are multiple answers, print any of them.You have to process $$$t$$$ test cases.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 \\le n \\le 10^5, 1 \\le s \\le 10^9$$$) — the number of parts in the verse and the maximum number of seconds Santa will listen to Vasya, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$) — the time it takes to recite each part of the verse. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.", "output_spec": "For each test case print one integer — the number of the part that Vasya needs to skip to obtain the maximum number of gifts. If Vasya shouldn't skip any parts, print 0.", "sample_inputs": ["3\n7 11\n2 9 1 3 18 1 4\n4 35\n11 9 10 7\n1 8\n5"], "sample_outputs": ["2\n1\n0"], "notes": "NoteIn the first test case if Vasya skips the second part then he gets three gifts.In the second test case no matter what part of the verse Vasya skips.In the third test case Vasya can recite the whole verse."}, "positive_code": [{"source_code": "def solve(n, s, vals)\n sum = 0\n max_so_far = vals[0]\n vals.each_with_index do |e, i|\n max_so_far = [max_so_far, e].max\n sum += e\n if sum > s\n return vals.index { |x| x == max_so_far } + 1\n end\n end\n 0\nend\n\n# gets.strip.split(' ').map(&:to_i)\ngets.strip.to_i.times do\n n, s = gets.strip.split(' ').map(&:to_i)\n vals = gets.strip.split(' ').map(&:to_i)\n puts solve(n, s, vals)\nend"}, {"source_code": "gets.strip.to_i.times {\n n, s = gets.strip.split(' ').map(&:to_i)\n a = gets.strip.split(' ').map(&:to_i)\n mx = -1\n sum = 0\n done = false\n $part\n a.each_with_index { |e, i| \n if mx <= e\n mx = e\n $part = i + 1\n end\n sum += e\n if sum > s\n puts $part\n done = true\n break\n end\n }\n puts 0 if !done\n}"}], "negative_code": [], "src_uid": "0924971933265f4be34710149a541087"} {"nl": {"description": "It is the easy version of the problem. The only difference is that in this version $$$k = 3$$$.You are given a positive integer $$$n$$$. Find $$$k$$$ positive integers $$$a_1, a_2, \\ldots, a_k$$$, such that: $$$a_1 + a_2 + \\ldots + a_k = n$$$ $$$LCM(a_1, a_2, \\ldots, a_k) \\le \\frac{n}{2}$$$ Here $$$LCM$$$ is the least common multiple of numbers $$$a_1, a_2, \\ldots, a_k$$$.We can show that for given constraints the answer always exists.", "input_spec": "The first line contains a single integer $$$t$$$ $$$(1 \\le t \\le 10^4)$$$  — the number of test cases. The only line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$3 \\le n \\le 10^9$$$, $$$k = 3$$$).", "output_spec": "For each test case print $$$k$$$ positive integers $$$a_1, a_2, \\ldots, a_k$$$, for which all conditions are satisfied.", "sample_inputs": ["3\n3 3\n8 3\n14 3"], "sample_outputs": ["1 1 1\n4 2 2\n2 6 6"], "notes": null}, "positive_code": [{"source_code": "def solve(n, k)\r\n a = n / 4\r\n case n % 4\r\n when 0\r\n [a*2, a, a]\r\n when 1\r\n [a*2, a*2, 1]\r\n when 2\r\n [a*2, a*2, 2]\r\n when 3\r\n [a*2+1, a*2+1, 1]\r\n end\r\nend\r\n\r\ndef main\r\n t = int\r\n t.times do\r\n n, k = ints\r\n a, b, c = solve(n, k)\r\n raise \"not satisfied: #{a} + #{b} + #{c} == #{n}\" unless a + b + c == n\r\n raise \"not satisfied: LCM(#{a}, #{b}, #{c}) <= #{n}/2\" unless 2 * a.lcm(b).lcm(c) <= n\r\n puts \"#{a} #{b} #{c}\"\r\n end\r\nend\r\n\r\nDEBUG = true\r\nMOD = 10**9+7\r\nYESNO = %w(No Yes)\r\nINF = 10**9\r\n\r\ndef int; gets.to_s.to_i end\r\ndef ints; gets.to_s.split.map { |s| s.to_i } end\r\ndef int1s; gets.to_s.split.map { |s| s.to_i - 1 } end\r\ndef float; gets.to_s.to_f end\r\ndef floats; gets.to_s.split.map { |s| s.to_f } end\r\ndef array_of(&convert); gets.to_s.split.map(&convert) end\r\ndef string; gets.to_s.chomp end\r\ndef rep(n, &b); Array.new(n, &b) end\r\ndef yes; puts YESNO[1] end\r\ndef no; puts YESNO[0] end\r\ndef yesno t; puts YESNO[t] end\r\ndef zip(xs, *yss); Enumerator.new { |y| xs.zip(*yss) { |a| y.yield(*a) } } end\r\ndef max(*xs, &block); block_given? ? xs.max_by(&block) : xs.max end\r\ndef min(*xs, &block); block_given? ? xs.min_by(&block) : xs.min end\r\ndef minmax(*xs, &block); block_given? ? xs.minmax_by(&block) : xs.minmax end\r\ndef gcd(*xs); xs.inject(0, :gcd) end\r\ndef matrix(h, w, fill=nil, &block); return Array.new(h) { Array.new(w, &block) } if block_given?; Array.new(h) { [fill] * w } end\r\ndef debug(x = nil); STDERR.puts (block_given? ? yield(x) : x).inspect if DEBUG; x end\r\ndef debug_grid(grid, width = 1); grid.each { |row| STDERR.puts row.map { |x| x.inspect.ljust(width) }.join(\"\") } if DEBUG; grid end\r\ndef if_debug; yield if DEBUG end\r\n\r\nmodule Boolean\r\ndef coerce(other); [other, to_i] end\r\ndef +@; to_i end\r\ndef to_int; to_i end\r\ndef *(other); to_i * other end\r\nend\r\n\r\nclass TrueClass\r\ninclude Boolean\r\ndef to_i; 1 end\r\nend\r\n\r\nclass FalseClass\r\ninclude Boolean\r\ndef to_i; 0 end\r\nend\r\n\r\nclass Integer\r\ndef div_ceil(y); (self + y - 1) / y end\r\ndef mod_inv(mod = MOD); pow(mod-2, mod) end\r\ndef mod_div(y, mod = MOD); self * mod_inv(y, mod) % mod end\r\ndef factorial(mod = MOD); (2..self).inject(1) { |f, x| f * x % mod } end\r\ndef popcount; x = self; c = 0; while x > 0; c += 1 if x & 1 == 1; x >>= 1 end; c end #TODO: faster\r\ndef bitbrute(&block); (1< 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\r\ndef lower_bound; ac, wa = self.end_open, self.begin; while ac - wa > 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\r\ndef shakutori(r2, &pred); Enumerator.new { |y| j, r = r2.begin, r2.end_open; each { |i| j += 1 while j + 1 < r and pred[i, j+1]; y.yield(i, j) } }; end\r\ndef widest(&block); Enumerator.new { |y| j, n = self.begin, self.end_open; each { |i| j += 1 while j < n and block[i, j]; y.yield(i, j) if block[i, j] } } end\r\nend\r\n\r\nclass Array\r\ndef power(&block); (0 ... 1 << size).each(&block) end\r\ndef sorted_merge(other); a = []; i = j = 0; n, m = size, other.size; if j < m and other[j] < self[i]; a << other[j]; j += 1 else; a << self[i]; i += 1 end while i < n; a.push(*other[j..-1]) if j < m; a end\r\ndef upper_bound; ac, wa = 0, size; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\r\ndef lower_bound; ac, wa = size, 0; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\r\ndef cum(*xs, &op); a = []; a << xs[0] if xs.size > 0; a << x = self[0]; (1...size).each { |i| a << x = op[x, self[i]] }; a end\r\ndef cumdiff(range); self[range.end_open] - self[range.begin]; end\r\nend\r\n\r\nmodule Enumerable\r\ndef sorted_uniq; x = nil; filter { |y| c = x === y; x = y; !c } end\r\ndef cumsum; ys = [0]; each { |x| ys << x + ys[-1] }; ys end\r\nend\r\n\r\nmain"}, {"source_code": "gets.to_i.times {\r\n n, k = gets.split(' ').map(&:to_i)\r\n if n % 4 == 0\r\n a = n / 2\r\n b = a / 2\r\n puts [a, b, b].join(' ')\r\n else\r\n a = (n - 1) / 2\r\n rem = n - 2 * a\r\n while a > 0\r\n if a % rem == 0\r\n puts [a, a, rem].join(' ')\r\n break\r\n end\r\n a -= 1\r\n rem += 2\r\n end\r\n end\r\n}"}, {"source_code": "def fn(n,k)\n m = n / 2\n if n % 2 == 1\n x = (n - 1)/2\n ret = [x,x, 1]\n else\n if m % 2 == 0\n ret = [m,m/2,m/2]\n else\n ret = [2, m-1, m-1]\n end\n end\n\n puts ret.join(' ')\nend\n\ntt = gets.to_i\ntt.times {\n (n, k)= gets.chomp!.split(' ').map &:to_i\n fn(n,k)\n}\n\n \t \t\t \t\t \t\t \t \t \t \t \t\t\t"}, {"source_code": "t = gets.chomp.to_i\r\nfor _ in (1..t)\r\n n, k = gets.chomp.split(' ').map { |x| x.to_i }\r\n if n % 3 == 0\r\n print \"#{n/3} #{n/3} #{n/3}\"\r\n elsif n % 2 == 1\r\n print \"#{1} #{(n-1)/2} #{(n-1)/2}\"\r\n elsif n % 4 == 0\r\n print \"#{n / 4} #{n / 4} #{n / 2}\"\r\n else\r\n print \"#{2} #{(n - 2) / 2} #{(n - 2) / 2}\"\r\n end\r\n puts\r\nend"}], "negative_code": [], "src_uid": "842a0587147591ea38a20638f3a7960d"} {"nl": {"description": "You are playing a very popular game called Cubecraft. Initially, you have one stick and want to craft $$$k$$$ torches. One torch can be crafted using one stick and one coal.Hopefully, you've met a very handsome wandering trader who has two trade offers: exchange $$$1$$$ stick for $$$x$$$ sticks (you lose $$$1$$$ stick and gain $$$x$$$ sticks). exchange $$$y$$$ sticks for $$$1$$$ coal (you lose $$$y$$$ sticks and gain $$$1$$$ coal). During one trade, you can use only one of these two trade offers. You can use each trade offer any number of times you want to, in any order.Your task is to find the minimum number of trades you need to craft at least $$$k$$$ torches. The answer always exists under the given constraints.You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 2 \\cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains three integers $$$x$$$, $$$y$$$ and $$$k$$$ ($$$2 \\le x \\le 10^9$$$; $$$1 \\le y, k \\le 10^9$$$) — the number of sticks you can buy with one stick, the number of sticks required to buy one coal and the number of torches you need, respectively.", "output_spec": "For each test case, print the answer: the minimum number of trades you need to craft at least $$$k$$$ torches. The answer always exists under the given constraints.", "sample_inputs": ["5\n2 1 5\n42 13 24\n12 11 12\n1000000000 1000000000 1000000000\n2 1000000000 1000000000"], "sample_outputs": ["14\n33\n25\n2000000003\n1000000001999999999"], "notes": null}, "positive_code": [{"source_code": "gets.to_i.times do\n a, b, k = gets.strip.split(\" \").map(&:to_i)\n cn = k\n csn = cn*b\n tsn = csn + k\n dsn = tsn - 1\n iop = dsn / (a-1)\n iop += 1 if (dsn % (a-1)) > 0\n puts iop + cn\nend"}, {"source_code": "T = gets.to_i\n\nT.times do\n x, y, k = gets.split.map(&:to_i)\n\n ok = Rational(k * (1 + y) - 1, x - 1).ceil\n\n puts ok + k\nend\n"}, {"source_code": "t = gets.chomp.to_i\nneed = 0\nkaisuu = 0\nt.times do \n x, y, k = gets.chomp.split(\" \").map(&:to_i)\n need = k + y * k #必要(松)\n sa = x - 1\n kaisuu = (need - 1) / sa\n amari = (need - 1) % sa\n if amari != 0\n kaisuu += 1\n end\n sekitan = (need - k)/ y\n puts kaisuu + sekitan\nend\n\n"}, {"source_code": "gets.to_i.times do\n x, y, k = gets.split.map &:to_i\n puts ((y + 1) * k + x - 3) / (x - 1) + k\nend"}], "negative_code": [], "src_uid": "28610d192329c78580db354f8dfc4094"} {"nl": {"description": "A progress bar is an element of graphical interface that displays the progress of a process for this very moment before it is completed. Let's take a look at the following form of such a bar.A bar is represented as n squares, located in line. To add clarity, let's number them with positive integers from 1 to n from the left to the right. Each square has saturation (ai for the i-th square), which is measured by an integer from 0 to k. When the bar for some i (1 ≤ i ≤ n) is displayed, squares 1, 2, ... , i - 1 has the saturation k, squares i + 1, i + 2, ... , n has the saturation 0, and the saturation of the square i can have any value from 0 to k.So some first squares of the progress bar always have the saturation k. Some last squares always have the saturation 0. And there is no more than one square that has the saturation different from 0 and k.The degree of the process's completion is measured in percents. Let the process be t% completed. Then the following inequation is fulfilled: An example of such a bar can be seen on the picture. For the given n, k, t determine the measures of saturation for all the squares ai of the progress bar.", "input_spec": "We are given 3 space-separated integers n, k, t (1 ≤ n, k ≤ 100, 0 ≤ t ≤ 100).", "output_spec": "Print n numbers. The i-th of them should be equal to ai.", "sample_inputs": ["10 10 54", "11 13 37"], "sample_outputs": ["10 10 10 10 10 4 0 0 0 0", "13 13 13 13 0 0 0 0 0 0 0"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/env ruby\n\nn, k, t = gets.chomp.split(/\\s/).map{|s| s.to_i}\nret = []\n\nmax = n * k\nvalue = (max * (t / 100.0)).to_i\n(value / k).times do\n ret << k\nend\n\nret << value % k\n\n(n - (value.to_i / k) - 1).times do\n ret << 0\nend\n\nputs ret.slice(0, n).join(' ')\n"}, {"source_code": "n, k, t = gets.split.map(&:to_i)\n\nval = Rational(100, n*k)\n\ns = 0\ns += 1 while (s+1)*val <= t\no = [k] * (s/k)\no << s%k if s%k > 0\no = (o + [0]*n)[0,n]\nputs o*\" \"\n"}, {"source_code": "#!/usr/bin/ruby\nn,k,t=gets.chop.split(\" \").map{|i|i.to_i}\nx=n*t/100\ny=t*n*k/100-x*k\nn.times{|i|if i 0\n print ' ' + balance.to_s\n counter += 1\nend\n\nfor fcounter in 1 .. input[0]-counter\n print ' ' + 0.to_s\nend"}, {"source_code": "n, k, t = gets.chop.split.map(&:to_i)\n\n#n, k, t = 11, 13, 37\n\nprogress = (n.to_f * k / (100 / t.to_f)).truncate\nnulls = (n.to_f * k - progress).truncate\n\narr = []\nwhile progress > 0 do\n if progress >= k then\n arr << k\n progress -= k\n else\n arr << progress\n progress = 0\n end\nend\n\narr << 0 while arr.length < n\n\nputs arr\n"}, {"source_code": "input = STDIN.gets\nn,k,t = input.strip.split(' ').map{|w| w.to_i}\na = Array.new(n).fill(0)\nsaturated = (t/(100.0/n)).to_i\nsum_saturated = saturated * k\nlast_saturation = (t * n * k / 100) - sum_saturated\nsaturated.times do |i|\n a[i] = k.to_i\nend\nif saturated >= n \n print a.join(' ')\nelse \n a[saturated] = last_saturation.to_i\n print a.join(' ')\nend\n"}], "negative_code": [{"source_code": "input = STDIN.gets\nn,k,t = input.strip.split(' ').map{|w| w.to_i}\na = Array.new(n).fill(0)\nif(t <= k)\n a[0] = t\n print a.join(' ')\nelse\n saturated = t / (100/n)\n sum_saturated = saturated * k\n last_saturation = (t * n * k / 100) - sum_saturated\n saturated.times do |i|\n a[i] = k.to_i\n end\n if saturated >= n \n print a.join(' ')\n else \n a[saturated] = last_saturation.to_i\n print a.join(' ')\n end\nend\n"}, {"source_code": "input = STDIN.gets\nn,k,t = input.strip.split(' ').map{|w| w.to_i}\na = Array.new(n).fill(0)\n#if(t <= k)\n# a[0] = t\n# print a.join(' ')\n#else\n p = t*n/100\n q = (t - (p*(100.0/n))).to_i\n p.times do |i|\n a[i] = k\n end\n if(q >= (100.0/n.to_f/k.to_f))\n a[p] = q * n * k / 100\n end\n print a.join(' ')\n #saturated = t / (100/n)\n #sum_saturated = saturated * k\n #last_saturation = (t * n * k / 100) - sum_saturated\n #saturated.times do |i|\n # a[i] = k.to_i\n #end\n #if saturated >= n \n # print a.join(' ')\n #else \n # a[saturated] = last_saturation.to_i\n # print a.join(' ')\n #end\n#end\n"}, {"source_code": "#!/usr/bin/env ruby\n\nn, k, t = gets.chomp.split(/\\s/).map{|s| s.to_i}\nret = []\n\nmax = n * k\nvalue = (max * (t / 100.0)).to_i\n(value / k).times do\n ret << k\nend\n\nret << value % k\n\n(n - (value.to_i / k) - 1).times do\n p :hoge\n ret << 0\nend\n\nputs ret.slice(0, n).join(' ')\n"}, {"source_code": "#!/usr/bin/env ruby\n\nn, k, t = gets.chomp.split(/\\s/).map{|s| s.to_i}\nret = []\n\nmax = n * k\nvalue = (max * (t / 100.0)).to_i\n(value / k).times do\n ret << k\nend\n\nret << value % k\n\n(n - (value.to_i / k) - 1).times do\n ret << 0\nend\n\nputs ret.join(' ')\n"}, {"source_code": "n,k,t=gets.chop.split(\" \").map{|i|i.to_i}\nx,y=t.divmod(k)\nn.times{|i|if i 0\n\nfor fcounter in (points/input[1])+1 .. input[0]\n print ' ' + 0.to_s\nend"}, {"source_code": "input = gets.split.map(&:to_i)\n\npoints = (input[2]*input[0]*input[1]) / 100\n\nfor counter in 1..(points/input[1])\n print input[1].to_s + ' '\nend\n\nbalance = points - (points/input[1]) * input[1]\n\nprint balance if balance > 0\n\nfor fcounter in (points/input[1])+2 .. input[0]\n print ' ' + 0.to_s\nend"}, {"source_code": "n, k, t = gets.chop.split.map(&:to_i)\n\n#n, k, t = 10, 10, 54\n\nprogress = (n.to_f * k / (100 / t.to_f)).truncate\n\narr = []\nwhile progress > 0 do\n if progress >= k then\n arr << k\n progress -= k\n else\n arr << progress\n progress = 0\n end\nend\n\nputs arr"}, {"source_code": "n, k, t = gets.chop.split.map(&:to_i)\n\n#n, k, t = 11, 13, 37\n\nprogress = (n.to_f * k / (100 / t.to_f)).truncate\nnulls = n.to_f * k - progress\n\narr = []\nwhile progress > 0 do\n if progress >= k then\n arr << k\n progress -= k\n else\n arr << progress\n progress = 0\n end\nend\n\nbegin arr << 0; nulls -= k end while nulls > k\n\nputs arr"}, {"source_code": "input = STDIN.gets\nn,k,t = input.strip.split(' ').map{|w| w.to_i}\na = Array.new(n).fill(0)\nif(t <= k)\n a[0] = t\n print a.join(' ')\nelse\n p = t * n / 100\n q = (t - (p*(100.0/n))).to_i\n p.times do |i|\n a[i] = k\n end\n if(q > 0)\n a[p] = q * n * k / 100\n end\n print a.join(' ')\n #saturated = t / (100/n)\n #sum_saturated = saturated * k\n #last_saturation = (t * n * k / 100) - sum_saturated\n #saturated.times do |i|\n # a[i] = k.to_i\n #end\n #if saturated >= n \n # print a.join(' ')\n #else \n # a[saturated] = last_saturation.to_i\n # print a.join(' ')\n #end\nend\n"}, {"source_code": "input = STDIN.gets\nn,k,t = input.strip.split(' ').map{|w| w.to_i}\na = Array.new(n).fill(0)\nsaturated = t / (100/n)\nsum_saturated = saturated * k\nlast_saturation = (t * n * k / 100) - sum_saturated\nsaturated.times do |i|\n a[i] = k\nend\nif saturated >= n \n print a.join(' ')\nelse \n a[saturated] = last_saturation\n if(saturated + 1 < n)\n ((saturated + 1)..n).each do |i|\n a[i] = 0\n end \n end\n print a.join(' ')\nend\n"}, {"source_code": "input = STDIN.gets\nn,k,t = input.strip.split(' ').map{|w| w.to_i}\na = Array.new(n).fill(0)\nif(t <= k)\n a[0] = t\n print a.join(' ')\nelse\n saturated = t / (100/n)\n sum_saturated = saturated * k\n last_saturation = t * n * k / 100 - sum_saturated\n saturated.times do |i|\n a[i] = k.to_i\n end\n if saturated >= n \n print a.join(' ')\n else \n a[saturated] = last_saturation.to_i\n if(saturated + 1 < n)\n ((saturated + 1)..n-1).each do |i|\n a[i] = 0\n end \n end\n print a.join(' ')\n end\nend\n"}, {"source_code": "input = STDIN.gets\nn,k,t = input.strip.split(' ').map{|w| w.to_i}\na = Array.new(n).fill(0)\nif(t <= k)\n a[0] = t\n print a.join(' ')\nelse\n saturated = t / (100/n)\n sum_saturated = saturated * k\n last_saturation = t * n * k / 100 - sum_saturated\n saturated.times do |i|\n a[i] = k.to_i\n end\n if saturated >= n \n print a.join(' ')\n else \n a[saturated] = last_saturation.to_i\n print a.join(' ')\n end\nend\n"}, {"source_code": "input = STDIN.gets\nn,k,t = input.strip.split(' ').map{|w| w.to_i}\na = Array.new(n).fill(0)\nif(t <= k)\n a[0] = t\n print a.join(' ')\nelse\n p = t*n/100\n q = (t - (p*(100.0/n))).to_i\n p.times do |i|\n a[i] = k\n end\n if(q >= (100.0/n.to_f/k.to_f))\n a[p] = q * n * k / 100\n end\n print a.join(' ')\n #saturated = t / (100/n)\n #sum_saturated = saturated * k\n #last_saturation = (t * n * k / 100) - sum_saturated\n #saturated.times do |i|\n # a[i] = k.to_i\n #end\n #if saturated >= n \n # print a.join(' ')\n #else \n # a[saturated] = last_saturation.to_i\n # print a.join(' ')\n #end\nend\n"}, {"source_code": "input = STDIN.gets\nn,k,t = input.strip.split(' ').map{|w| w.to_i}\na = Array.new(n).fill(0)\nif(t <= k)\n a[0] = k\n print a.join(' ')\nelse\n saturated = t / (100/n)\n sum_saturated = saturated * k\n last_saturation = t * n * k / 100 - sum_saturated\n saturated.times do |i|\n a[i] = k.to_i\n end\n if saturated >= n \n print a.join(' ')\n else \n a[saturated] = last_saturation.to_i\n if(saturated + 1 < n)\n ((saturated + 1)..n-1).each do |i|\n a[i] = 0\n end \n end\n print a.join(' ')\n end\nend\n"}, {"source_code": "input = STDIN.gets\nn,k,t = input.strip.split(' ').map{|w| w.to_i}\na = Array.new(n).fill(0)\n#if(t <= k)\n# a[0] = t\n# print a.join(' ')\n#else\n #p = t*n/100\n #q = (t - (p*(100.0/n))).to_i\n #p.times do |i|\n # a[i] = k\n #end\n #if(q >= (100.0/n.to_f/k.to_f))\n # a[p] = q * n * k / 100\n #end\n #print a.join(' ')\n saturated = t / (100/n)\n sum_saturated = saturated * k\n last_saturation = (t * n * k / 100) - sum_saturated\n saturated.times do |i|\n a[i] = k.to_i\n end\n if saturated >= n \n print a.join(' ')\n else \n a[saturated] = last_saturation.to_i\n print a.join(' ')\n end\n#end\n"}, {"source_code": "input = STDIN.gets\nn,k,t = input.strip.split(' ').map{|w| w.to_i}\na = Array.new(n).fill(0)\nsaturated = t / (100/n)\nsum_saturated = saturated * k\nlast_saturation = (t * n * k / 100) - sum_saturated\nsaturated.times do |i|\n a[i] = k\nend\nif saturated >= n \n print a.join(' ')\nelse \n a[saturated] = last_saturation\n if(saturated + 1 < n)\n ((saturated + 1)..n-1).each do |i|\n a[i] = 0\n end \n end\n print a.join(' ')\nend\n"}, {"source_code": "input = STDIN.gets\nn,k,t = input.strip.split(' ').map{|w| w.to_i}\na = Array.new(n).fill(0)\nsaturated = t / (100/n)\nsum_saturated = saturated * k\nlast_saturation = (t * n * k / 100) - sum_saturated\nsaturated.times do |i|\n a[i] = k\nend\nif saturated >= n \n puts a\nelse \n a[saturated] = last_saturation\n if(saturated + 1 < n)\n ((saturated + 1)..n).each do |i|\n a[i] = 0\n end \n end\n puts a\nend\n"}, {"source_code": "input = STDIN.gets\nn,k,t = input.strip.split(' ').map{|w| w.to_i}\na = Array.new(n).fill(0)\nif(t <= k)\n a[0] = t\n print a.join(' ')\nelse\n p = t * n / 100\n q = (t - (p*(100.0/n))).to_i\n puts p,q\n p.times do |i|\n a[i] = k\n end\n if(q > 0)\n a[p] = q * n * k / 100\n end\n print a.join(' ')\n #saturated = t / (100/n)\n #sum_saturated = saturated * k\n #last_saturation = (t * n * k / 100) - sum_saturated\n #saturated.times do |i|\n # a[i] = k.to_i\n #end\n #if saturated >= n \n # print a.join(' ')\n #else \n # a[saturated] = last_saturation.to_i\n # print a.join(' ')\n #end\nend\n"}], "src_uid": "0ad96b6a8032d70df400bf2ad72174bf"} {"nl": {"description": "A camera you have accidentally left in a desert has taken an interesting photo. The photo has a resolution of n pixels width, and each column of this photo is all white or all black. Thus, we can represent the photo as a sequence of n zeros and ones, where 0 means that the corresponding column is all white, and 1 means that the corresponding column is black.You think that this photo can contain a zebra. In this case the whole photo should consist of several (possibly, only one) alternating black and white stripes of equal width. For example, the photo [0, 0, 0, 1, 1, 1, 0, 0, 0] can be a photo of zebra, while the photo [0, 0, 0, 1, 1, 1, 1] can not, because the width of the black stripe is 3, while the width of the white stripe is 4. Can the given photo be a photo of zebra or not?", "input_spec": "The first line contains a single integer n (1 ≤ n ≤ 100 000) — the width of the photo. The second line contains a sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 1) — the description of the photo. If ai is zero, the i-th column is all black. If ai is one, then the i-th column is all white.", "output_spec": "If the photo can be a photo of zebra, print \"YES\" (without quotes). Otherwise, print \"NO\". You can print each letter in any case (upper or lower).", "sample_inputs": ["9\n0 0 0 1 1 1 0 0 0", "7\n0 0 0 1 1 1 1", "5\n1 1 1 1 1", "8\n1 1 1 0 0 0 1 1", "9\n1 1 0 1 1 0 1 1 0"], "sample_outputs": ["YES", "NO", "YES", "NO", "NO"], "notes": "NoteThe first two examples are described in the statements.In the third example all pixels are white, so the photo can be a photo of zebra.In the fourth example the width of the first stripe is equal to three (white color), the width of the second stripe is equal to three (black), and the width of the third stripe is equal to two (white). Thus, not all stripes have equal length, so this photo is not a photo of zebra."}, "positive_code": [{"source_code": "n = gets.chomp.to_i\na = gets.chomp.split(\" \").map{|x| x.to_i}\nkek = 0\nkek2 = 0\ntt = a[0]\nans = \"YES\"\nflag = true\nfor i in a do\n if i == tt\n kek += 1\n else\n flag = false\n if kek2 == kek\n tt = i\n kek = 1\n elsif kek2 == 0 then\n tt = i\n kek2 = kek\n kek = 1\n else\n ans = \"NO\"\n break\n end\n end\nend\nif kek != kek2\n ans = \"NO\"\nend\nif flag\n ans = \"YES\"\nend\nputs ans\n"}, {"source_code": "n = gets.to_i\ncnt0 = 0\ncnt1 = 0\nold = -1\nans = 1\n\n\narr = gets.split(\" \").map(&:to_i)\nfor i in 0..n-1 do \n if (old == (-1)) \n old = arr[i]\n cnt1 = cnt1 + 1\n elsif (arr[i] == old) \n cnt1 = cnt1 + 1 \n elsif (arr[i] != old and cnt0 == 0) \n old = 1 - old\n cnt0 = cnt1\n cnt1 = 1\n elsif (arr[i] != old && cnt1 == cnt0) \n old = 1 - old\n cnt1 = 1\n elsif (arr[i] != old and cnt1 != cnt0)\n ans = 0\n \n end \n \n \n \nend\nif (cnt1 != cnt0 and cnt0 != 0)\n ans = 0\nend\nif (ans == 0) \n puts \"NO\"\nelse\n puts \"YES\"\nend"}, {"source_code": "n = gets.to_i\n\nA = gets.split\n\nlen = 1\nfor i in 1..(n - 1)\n\tif (A[i - 1] != A[i]) \n\t\tbreak\n\t\tend\n\tlen += 1\nend\n\n\nx = 1\n\nfor i in 1..(n - 1)\n\tif (A[i - 1] != A[i] && i % len != 0) \n\t\tx = 0\n\t\tend \n\tif (A[i - 1] == A[i] && i % len == 0)\n\t\tx = 0\n\t\tend\nend\n\n\t\t\nif (x == 0 || n % len > 0) \n\tputs \"NO\"\nelse\n\tputs \"YES\"\nend \n\t"}, {"source_code": "n = gets.to_i\na = gets.chomp.split()\nb = Array.new\nlst = a.at(0)\nc = 1\nfor i in 1..(n-1)\n if a.at(i) == lst\n c = c+1\n else\n b.push(c)\n c = 1\n lst = a.at(i)\n end\nend\nb.push(c)\nans = \"NO\"\nif b.uniq.size == 1\n ans = \"YES\"\nend\nputs ans"}, {"source_code": "r=->{gets.split.map &:to_i}\nn=r[]\na=r[] + [2]\n\ncolor = a[0]\nl = 1\nwhitelen = -1\nblacklen = -1\n\ngood = true\n\nfor i in 1 ... a.size\n if a[i] != color\n if color == 1\n if whitelen == -1 or whitelen == l\n whitelen = l\n else\n good = false\n end\n else\n if blacklen == -1 or blacklen == l\n blacklen = l\n else\n good = false\n end\n end\n color = a[i]\n l = 0\n end\n l += 1\nend\n\nif blacklen != -1 and whitelen != -1 and whitelen != blacklen\n good = false\nend\n\nif good\n puts \"YES\"\nelse\n puts \"NO\"\nend"}, {"source_code": "n = gets().split(' ').map{|x| Integer(x)}[0]\na = gets().split(' ').map{|x| Integer(x)}\n\ncur = 1\nst = -1\nflag = true\n\n(1...n).each do |i|\n if i == 0 or a[i] != a[i - 1] then\n \tif st == -1 then\n \t\tst = cur\n \telsif cur != st\n \t\tflag = false\n \tend\n \tcur = 1\n else\n \tcur += 1\n end\nend\n\nif st == -1 then\n\tst = cur\nelsif cur != st\n\tflag = false\nend\n\nif flag then\n\tputs(\"YES\")\nelse\n\tputs(\"NO\")\nend\n\n# your code goes here"}, {"source_code": "# fo = open(\"input.txt\", 'r')\nn = gets.to_i\n\nres = gets.split()\ncnt = -1\ncur = 1\nans = 'Yes'\n(n - 1).times do |i|\n if (res[i] == res[i + 1]) \n cur += 1\n else\n if (cnt == -1) \n cnt = cur\n else \n if (cnt != cur)\n ans = 'No'\n end\n end\n cur = 1\n end\nend\n\nif (cnt != cur and cnt != -1) \n ans = 'No'\nend\n\nputs(ans)"}, {"source_code": "n = gets\nar = gets.chomp.split.map(&:to_i)\ncombed = Hash.new\nold = ar[0]\ni = 1\n(1..ar.length).each do |x|\n cur = ar[x]\n if old == cur\n i+=1\n else\n combed[i] = 1\n i = 1\n old = cur\n end\nend\n\nputs combed.size <= 1 ? \"YES\" : \"NO\""}, {"source_code": "n = gets.to_i\nphoto = gets.split\n\ncurrent_color = photo.first\nfirst_strip_width = 0\ncurrent_strip_width = 0\nphoto.each do |pixel|\n if pixel == current_color\n current_strip_width += 1\n else\n first_strip_width = current_strip_width if first_strip_width == 0\n if current_strip_width != first_strip_width\n break\n end\n current_strip_width = 1\n current_color = pixel\n end\nend\n\nfirst_strip_width = current_strip_width if first_strip_width == 0\nif current_strip_width == first_strip_width\n puts \"YES\"\nelse\n puts \"NO\"\nend"}, {"source_code": "n=gets.to_i()\ns=gets.split()\np=0\nq=0\nh=0\nf=1\nfor i in s\n\tif(i!=p)\n\t\tif(q!=h && h!=0)\n\t\t\tf=0\n\t\t\tbreak\n\t\tend\n\t\th=q\n\t\tp=i\n\t\tq=1\n\telse\n\t\tq+=1\n\tend\nend\nif(q!=h && h!=0)\n\tf=0\nend\nif(f==1)\n\tputs('YES')\nelse\n\tputs('NO')\nend"}, {"source_code": "n = gets.chomp().to_i\nprv = -1\nlen = -1\ntarget = -1\nok = 1\ngets.split.map(&:to_i).each do |a|\n\tif a != prv\n\t\tprv = a\n\t\tif target != -1 and len != target\n\t\t\tok = 0\n\t\tend\n\t\ttarget = len\n\t\tlen = 0\n\telse\n\t\tlen = len + 1\n\tend\nend\nif target != -1 and len != target\n\tok = 0\nend\nif ok == 1\n\tputs \"Yes\"\nelse\n\tputs \"No\"\nend"}, {"source_code": "gets\nputs gets.split.chunk{|n|n}.map{|b,a|a.size}.uniq.size<2?\"Yes\":\"No\"\n"}, {"source_code": "gets\nputs gets.split.map(&:to_i).chunk{|n| n==0}.map{|b,a| a.size}.uniq.length==1 ? \"Yes\" : \"No\"\n\n"}, {"source_code": "n = gets.chomp.to_i\n\na = []\ngets.chomp.split.each do |i|\n a << i.to_i\nend\nl = -1\npv = a[0]\ncl = 0\nfl = 0\na.each do |x|\n if (x != pv)\n if (l == -1)\n l = cl\n end\n if (l != cl)\n fl = 1\n end\n cl = 0\n end\n pv = x\n cl += 1\nend\nif (l == -1)\n l = cl\nend\nif (l != cl)\n fl = 1\nend\nif fl == 1\n puts \"NO\"\nelse\n puts \"YES\"\nend"}], "negative_code": [{"source_code": "n = gets.to_i\ncnt0 = 0\ncnt1 = 0\nold = -1\nans = 1\n\n\narr = gets.split(\" \").map(&:to_i)\nfor i in 0..n-1 do \n if (old == (-1)) \n old = arr[i]\n cnt1 = cnt1 + 1\n elsif (arr[i] == old) \n cnt1 = cnt1 + 1 \n elsif (arr[i] == 1 - old and cnt0 == 0) \n old = 1 - old\n cnt0 = cnt1\n cnt1 = 1\n \n elsif (arr[i] == 1 - old and cnt1 != cnt0)\n ans = 0\n \n end \n \nend\nif (cnt1 != cnt0 and cnt0 != 0)\n ans = 0\nend\nif (ans == 0) \n puts \"NO\"\nelse\n puts \"YES\"\nend"}, {"source_code": "n = gets.to_i\n\nA = gets.split\n\nlen = 1\nfor i in 1..(n - 1)\n\tif (A[i - 1] != A[i]) \n\t\tbreak\n\t\tend\n\tlen += 1\nend\n\n\nx = 1\n\nfor i in 1..(n - 1)\n\tif (A[i - 1] != A[i] && i % len != 0) \n\t\tx = 0\n\t\tend \nend\n\n\t\t\nif (x == 0 || n % len > 0) \n\tputs \"NO\"\nelse\n\tputs \"YES\"\nend \n\t"}, {"source_code": "n = gets.to_i\na = gets.chomp.split()\nputs a\nb = Array.new\nlst = a.at(0)\nc = 1\nfor i in 1..(n-1)\n\n if a.at(i) == lst\n c = c+1\n else\n b.push(c)\n c = 1\n lst = a.at(i)\n end\nend\nb.push(c)\nans = \"NO\"\nif b.uniq.size == 1\n ans = \"YES\"\nend\nputs ans"}], "src_uid": "75a00d8a614fd0bcb8504b0268a121e0"} {"nl": {"description": "You've got another problem dealing with arrays. Let's consider an arbitrary sequence containing n (not necessarily different) integers a1, a2, ..., an. We are interested in all possible pairs of numbers (ai, aj), (1 ≤ i, j ≤ n). In other words, let's consider all n2 pairs of numbers, picked from the given array.For example, in sequence a = {3, 1, 5} are 9 pairs of numbers: (3, 3), (3, 1), (3, 5), (1, 3), (1, 1), (1, 5), (5, 3), (5, 1), (5, 5).Let's sort all resulting pairs lexicographically by non-decreasing. Let us remind you that pair (p1, q1) is lexicographically less than pair (p2, q2) only if either p1 < p2, or p1 = p2 and q1 < q2.Then the sequence, mentioned above, will be sorted like that: (1, 1), (1, 3), (1, 5), (3, 1), (3, 3), (3, 5), (5, 1), (5, 3), (5, 5)Let's number all the pair in the sorted list from 1 to n2. Your task is formulated like this: you should find the k-th pair in the ordered list of all possible pairs of the array you've been given.", "input_spec": "The first line contains two integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ n2). The second line contains the array containing n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109). The numbers in the array can coincide. All numbers are separated with spaces. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use cin, cout, streams or the %I64d specificator instead.", "output_spec": "In the single line print two numbers — the sought k-th pair.", "sample_inputs": ["2 4\n2 1", "3 2\n3 1 5"], "sample_outputs": ["2 2", "1 3"], "notes": "NoteIn the first sample the sorted sequence for the given array looks as: (1, 1), (1, 2), (2, 1), (2, 2). The 4-th of them is pair (2, 2).The sorted sequence for the array from the second sample is given in the statement. The 2-nd pair there is (1, 3)."}, "positive_code": [{"source_code": "I = ->{gets.split.map &:to_i}\nn, m = I[]\nm -= 1\na = I[].sort\nh = Hash.new(0)\na.each{|_| h[_] += 1}\nh.sort.each{|k, v|\n if m < v * n\n m /= v\n puts \"#{k} #{a[m]}\"\n break\n else\n m -= v * n\n end\n}\n"}, {"source_code": "n,k=gets.split.map(&:to_i)\na=gets.split.map(&:to_i)\n\na.sort!\nk-=1\ninput=[]\nn.times{|i|\n if i>0&&a[i]==a[i-1]\n input[input.length-1][1]+=1\n else\n input.push([a[i],1])\n end\n}\ninput.each{|t|\n if k= 0 and data[low-1] == a\n low -= 1\nend\nhigh = ai\nwhile high+1 < n and data[high+1] == a\n high += 1\nend\nb = data[(k - low*n)/(high-low+1)]\nputs '%d %d' % [a, b]\n"}, {"source_code": "n,k = gets.split.map(&:to_i)\nk -= 1\na = gets.split.map(&:to_i).sort\n\nd,m = k.divmod n\n\ns = a.select{|e|e == a[d] }.size\n\nif s > 1\n f = a.index(a[d])*n\n m = (k-f)/s\nend\n\nprint a[d], \" \",a[m]\n"}, {"source_code": "#!/usr/bin/ruby\n# coding: utf-8\n\nn, k = *$stdin.gets.chomp.split.map(&:to_i)\nas = $stdin.gets.chomp.split.map(&:to_i)\n\nh = Hash.new { 0 }\nas.each do |a|\n h[a] += 1\nend\n\nh.keys.sort.each do |a1|\n c1 = h[a1]\n if k-c1*n <= 0\n h.keys.sort.each do |a2|\n c2 = h[a2]\n if k-c1*c2 <= 0\n puts [a1, a2].join(' ')\n exit 0\n end\n k -= c1*c2\n end\n end\n k -= c1*n\nend\n"}, {"source_code": "n,k=gets.chomp.split.collect{|a| a.to_i}\narr=gets.chomp.split.collect{|a| a.to_i}.sort\nk=k-1\nif arr[k/n] == arr[k/n+1] || arr[k/n]==arr[k/n-1]\nm=arr.count arr[k/n]\nz=arr.index arr[k/n]\n\nprint arr[k/n],\" \",arr[(k-z*n)/m]\nelse\nprint arr[k/n],\" \" , arr[k%(n)].to_s\nend\n\n"}, {"source_code": "n, k = gets.split.collect{|i| i.to_i}\nnumbers = gets.split.collect{|i| i.to_i}.sort\n\nk -= 1 \nf_idx = k / n\ns_idx = k % n\n\nfl = numbers.index(numbers[f_idx])\nfr = numbers.rindex(numbers[f_idx])\n\nif not fl == fr\n rem = k - fl * n\n s_idx = rem / (fr - fl + 1)\nend\n\nputs [numbers[f_idx], numbers[s_idx]].join(\" \")"}], "negative_code": [{"source_code": "n,k=gets.split.map(&:to_i)\na=gets.split.map(&:to_i)\n\na.sort!\nk-=1\nn.times{|i|\n if k= 0 and data[low] == a\n low -= 1\nend\nhigh = ai+1\nwhile high < n and data[high] == a\n high += 1\nend\nb = data[(k - low*n)/(high-low+1)]\nputs '%d %d' % [a, b]\n"}, {"source_code": "n, k = STDIN.readline.split.map {|s| s.to_i }\ndata = STDIN.readline.split.map {|s| s.to_i }.sort\nk -= 1\na = data[k/n]\nb = data[k%n]\nputs '%d %d' % [a, b]\n"}, {"source_code": "n,k = gets.split.map(&:to_i)\nk -= 1\na = gets.split.map(&:to_i).uniq.sort\n\nd,m = k.divmod n\n\n\nprint a[d], \" \",a[m]\n"}, {"source_code": "#!/usr/bin/ruby\n# coding: utf-8\n\nn, k = *$stdin.gets.chomp.split.map(&:to_i)\nas = $stdin.gets.chomp.split.map(&:to_i).sort\n\nk -= 1\nputs [as[k/n], as[k%n]].join(' ')\n"}, {"source_code": "n,k=gets.chomp.split.collect{|a| a.to_i}\narr=gets.chomp.split.collect{|a| a.to_i}.sort\nk=k-1\nprint arr[k/n],\" \" , arr[k%(n)].to_s\n"}, {"source_code": "n,k=gets.chomp.split.collect{|a| a.to_i}\narr=gets.chomp.split.collect{|a| a.to_i}.sort\nk=k-1\nif arr[k/n] == arr[k/n+1] || arr[k/n]==arr[k/n-1]\nprint arr[k/n],\" \",arr[k/n]\nelse\nprint arr[k/n],\" \" , arr[k%(n)].to_s\nend\n\n"}, {"source_code": "n,k=gets.chomp.split.collect{|a| a.to_i}\narr=gets.chomp.split.collect{|a| a.to_i}.sort\nk=k-1\nif arr[k/n] == arr[k/n+1] || arr[k/n]==arr[k/n-1]\nm=arr.count arr[k/n]\nprint arr[k/n],\" \",arr[(k/m)]\nelse\nprint arr[k/n],\" \" , arr[k%(n)].to_s\nend\n\n"}, {"source_code": "n, k = gets.split.collect{|i| i.to_i}\nnumbers = gets.split.collect{|i| i.to_i}.sort\n\nk -= 1 \nf_idx = k / n\ns_idx = k % n\n\nputs [numbers[f_idx], numbers[s_idx]].join(\" \")"}], "src_uid": "d3b9ffa76436b957ca959cf9204f9873"} {"nl": {"description": "There is a bookshelf which can fit $$$n$$$ books. The $$$i$$$-th position of bookshelf is $$$a_i = 1$$$ if there is a book on this position and $$$a_i = 0$$$ otherwise. It is guaranteed that there is at least one book on the bookshelf.In one move, you can choose some contiguous segment $$$[l; r]$$$ consisting of books (i.e. for each $$$i$$$ from $$$l$$$ to $$$r$$$ the condition $$$a_i = 1$$$ holds) and: Shift it to the right by $$$1$$$: move the book at index $$$i$$$ to $$$i + 1$$$ for all $$$l \\le i \\le r$$$. This move can be done only if $$$r+1 \\le n$$$ and there is no book at the position $$$r+1$$$. Shift it to the left by $$$1$$$: move the book at index $$$i$$$ to $$$i-1$$$ for all $$$l \\le i \\le r$$$. This move can be done only if $$$l-1 \\ge 1$$$ and there is no book at the position $$$l-1$$$. Your task is to find the minimum number of moves required to collect all the books on the shelf as a contiguous (consecutive) segment (i.e. the segment without any gaps).For example, for $$$a = [0, 0, 1, 0, 1]$$$ there is a gap between books ($$$a_4 = 0$$$ when $$$a_3 = 1$$$ and $$$a_5 = 1$$$), for $$$a = [1, 1, 0]$$$ there are no gaps between books and for $$$a = [0, 0,0]$$$ there are also no gaps between books.You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 200$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 \\le n \\le 50$$$) — the number of places on a bookshelf. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$0 \\le a_i \\le 1$$$), where $$$a_i$$$ is $$$1$$$ if there is a book at this position and $$$0$$$ otherwise. It is guaranteed that there is at least one book on the bookshelf.", "output_spec": "For each test case, print one integer: the minimum number of moves required to collect all the books on the shelf as a contiguous (consecutive) segment (i.e. the segment without gaps).", "sample_inputs": ["5\n7\n0 0 1 0 1 0 1\n3\n1 0 0\n5\n1 1 0 0 1\n6\n1 0 0 0 0 1\n5\n1 1 0 1 1"], "sample_outputs": ["2\n0\n2\n4\n1"], "notes": "NoteIn the first test case of the example, you can shift the segment $$$[3; 3]$$$ to the right and the segment $$$[4; 5]$$$ to the right. After all moves, the books form the contiguous segment $$$[5; 7]$$$. So the answer is $$$2$$$.In the second test case of the example, you have nothing to do, all the books on the bookshelf form the contiguous segment already.In the third test case of the example, you can shift the segment $$$[5; 5]$$$ to the left and then the segment $$$[4; 4]$$$ to the left again. After all moves, the books form the contiguous segment $$$[1; 3]$$$. So the answer is $$$2$$$.In the fourth test case of the example, you can shift the segment $$$[1; 1]$$$ to the right, the segment $$$[2; 2]$$$ to the right, the segment $$$[6; 6]$$$ to the left and then the segment $$$[5; 5]$$$ to the left. After all moves, the books form the contiguous segment $$$[3; 4]$$$. So the answer is $$$4$$$.In the fifth test case of the example, you can shift the segment $$$[1; 2]$$$ to the right. After all moves, the books form the contiguous segment $$$[2; 5]$$$. So the answer is $$$1$$$."}, "positive_code": [{"source_code": "T = gets.to_i\n\nT.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n\n books = []\n\n a.each_with_index do |x, i|\n next if x == 0\n\n books << i\n end\n\n ans = 0\n\n books.each_cons(2) do |a, b|\n ans += b - a - 1\n end\n\n puts ans\nend\n"}, {"source_code": "t = gets.chomp.to_i\nt.times do\n n = gets.chomp.to_i\n arr = gets.chomp.split(' ').map(&:to_i)\n st = -1\n res = 0\n arr.each_with_index do |el, ind|\n if el == 1 && st != -1\n res += ind - st - 1\n st = ind\n end\n if el == 1 && st == -1\n st = ind\n end\n end\n puts res\nend"}, {"source_code": "t = gets.chomp.to_i\nt.times do\n n = gets.chomp.to_i\n a = gets.chomp.split(\" \").map(&:to_i)\n ans = 0\n count = 0\n an = 0\n a.each do |i|\n if i == 0 && count >= 1\n an += 1\n elsif i == 1\n count += 1\n ans += an\n an = 0\n end\n end\n puts ans\nend"}, {"source_code": "gets.to_i.times do\n n = gets.to_i\n v = gets.split.map(&:to_i)\n seen = false\n cur = 0\n r = 0\n v.each do |x|\n cur += 1 if seen && x == 0\n if x == 1\n seen = true\n r += cur\n cur = 0\n end\n end\n puts r\nend\n"}, {"source_code": "t = gets.to_i\nans = []\nt.times do\n n = gets.to_i\n a = gets.chomp.split(\" \").map(&:to_i)\n cnt = 0\n left = -1\n right = -1\n n.times do |i|\n if left == -1 && a[i] == 1\n left = i\n end\n if right == -1 && a[n-1-i] == 1\n right = n-1-i\n end\n end\n for i in left..right\n if a[i] == 0\n cnt += 1\n end\n end\n ans << cnt\nend\nputs ans"}, {"source_code": "gets.to_i.times do\n _ = gets\n a = gets.split.map(&:to_i)\n puts a.rindex(1) - a.index(1) - a.count(1) + 1\nend\n"}, {"source_code": "t=gets.to_i\nt.times do \n n=gets.to_i\n an=gets.chomp.split(' ').map(&:to_i)\n start = an.find_index {|ai|ai!=0}\n last =n-1-an.reverse.find_index{|ai|ai!=0}\n gapc=an[start..last].count do |ai|\n ai==0\n end\n p gapc\nend"}], "negative_code": [], "src_uid": "1c07882651ef6ebfc05e777d562e28b9"} {"nl": {"description": "You are given an array $$$a$$$ consisting of $$$n$$$ integers.Your task is to say the number of such positive integers $$$x$$$ such that $$$x$$$ divides each number from the array. In other words, you have to find the number of common divisors of all elements in the array.For example, if the array $$$a$$$ will be $$$[2, 4, 6, 2, 10]$$$, then $$$1$$$ and $$$2$$$ divide each number from the array (so the answer for this test is $$$2$$$).", "input_spec": "The first line of the input contains one integer $$$n$$$ ($$$1 \\le n \\le 4 \\cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^{12}$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$.", "output_spec": "Print one integer — the number of such positive integers $$$x$$$ such that $$$x$$$ divides each number from the given array (in other words, the answer is the number of common divisors of all elements in the array).", "sample_inputs": ["5\n1 2 3 4 5", "6\n6 90 12 18 30 18"], "sample_outputs": ["1", "4"], "notes": null}, "positive_code": [{"source_code": "require 'prime'\nclass Integer\n\tdef divisors\n\t\tself.prime_division.reduce([1]) do |a, (p, e)|\n\t\t\t(0..e).map { p**_1 }.product(a).map { _1 * _2 }\n\t\tend.sort\n\tend\n\tdef divisor_count\n\t\tself.prime_division.reduce(1) { |acc, (p, e)| acc *= e + 1 }\n\tend\nend\n\ngets\nputs gets.split.map(&:to_i).reduce(&:gcd).divisor_count\n"}, {"source_code": "def divs(n)\n i = 1\n sq = Math.sqrt(n).ceil.to_i\n div = []\n\n while i <= sq\n if (n % i).zero?\n if (n / i) == i\n div << i\n else\n div << i\n div << n / i\n end\n end\n i += 1\n end\n div.uniq\nend\n\nn = gets.strip.to_i\na = gets.strip.split(' ').map(&:to_i).uniq\ng = 0\na.each do |e|\n g = e.gcd(g)\nend\nputs divs(g).size\n"}, {"source_code": "n = gets.to_i\ng = 0\na = gets.strip.split.map(&:to_i)\na.each do |x|\n g = g.gcd(x)\nend\nres = 0\ni = 1\nwhile i ** 2 <= g\n if g % i == 0\n res += 1\n res += 1 if i != g / i\n end\n i += 1\nend\nputs res"}, {"source_code": "class NemiLib\n # 素因数分解ハッシュ形式\n # (例)100なら{2=>2,5=>2}\n def prime_factorization(num, result = Hash.new(0))\n return {} if num < 1\n return {1=>1} if num == 1\n\n divides = false\n to = Math.sqrt(num).floor\n # 2だけ別処理\n # 再起で呼び出すので1メソッド中に1回だけしか割らないようにする\n if num % 2 == 0\n result[2] += 1\n prime_factorization(num / 2, result)\n divides = true\n else\n 3.step(to, 2) do |item|\n if num % item == 0\n result[item] += 1\n prime_factorization(num / item, result)\n divides = true\n break\n end\n end\n end\n # 何も割れないなら、その数を入れる\n result[num] += 1 unless divides\n\n result\n end\n\n # 約数\n def divisors(num)\n return [1] if num == 1\n\n pows = []\n # 0乗、1乗・・・した値を入れる\n prime_factorization(num).each_pair do |key, value|\n pow = []\n 0.upto(value).each do |nn|\n pow << key ** nn\n end\n\n pows << pow\n end\n\n first_array = pows.delete_at(0)\n return first_array if pows.size == 0\n\n result = first_array.product(*pows)\n result.map do |item|\n item.inject { |sum, i| sum * i }\n end.sort\n end\nend\n\n\nn = gets.to_i\naa = gets.split.map(&:to_i)\ngcd = aa.inject(:gcd)\npf = NemiLib.new.prime_factorization(gcd)\nputs (pf[1]==1 ? 1 : pf.each_value.inject(1){|sum,i| sum*(i+1)})\n"}], "negative_code": [{"source_code": "def factors(n)\n facts = [1]\n\n while (n % 2).zero?\n facts << 2\n n = n / 2\n end\n i = 3\n sq = Math.sqrt(n).ceil.to_i\n while i <= sq\n while (n % i).zero?\n facts << i\n n = n / i\n end\n i += 2\n end\n facts << n\n\n facts.uniq\nend\n\n\nn = gets.strip.to_i\na = gets.strip.split(' ').map(&:to_i)\nmn = a.min\nmx = a.max\nfacts = (factors(mn) + factors(mx) + [mn, mx]).uniq\n# puts facts.inspect\n\nres = 0\nfacts.each do |f|\n res += 1 if a.all? { |e| e % f == 0 }\nend\nputs res\n"}, {"source_code": "def factors(n)\n facts = [1]\n\n while (n % 2).zero?\n facts << 2\n n = n / 2\n end\n i = 3\n sq = Math.sqrt(n).ceil.to_i\n while i <= sq\n while (n % i).zero?\n facts << i\n n = n / i\n end\n i += 1\n end\n facts << n if n > 2\n\n facts.uniq\nend\n\n\nn = gets.strip.to_i\na = gets.strip.split(' ').map(&:to_i)\nmn = a.min\nfacts = (factors(mn) + [mn]).uniq\n\nres = 0\nfacts.each do |f|\n res += 1 if a.all? { |e| e % f == 0 }\nend\nputs res\n"}, {"source_code": "class NemiLib\n # 素因数分解ハッシュ形式\n # (例)100なら{2=>2,5=>2}\n def prime_factorization(num, result = Hash.new(0))\n return {} if num < 1\n return {1=>1} if num == 1\n\n divides = false\n to = Math.sqrt(num).floor\n # 2だけ別処理\n # 再起で呼び出すので1メソッド中に1回だけしか割らないようにする\n if num % 2 == 0\n result[2] += 1\n prime_factorization(num / 2, result)\n divides = true\n else\n 3.step(to, 2) do |item|\n if num % item == 0\n result[item] += 1\n prime_factorization(num / item, result)\n divides = true\n break\n end\n end\n end\n # 何も割れないなら、その数を入れる\n result[num] += 1 unless divides\n\n result\n end\n\n # 約数\n def divisors(num)\n return [1] if num == 1\n\n pows = []\n # 0乗、1乗・・・した値を入れる\n prime_factorization(num).each_pair do |key, value|\n pow = []\n 0.upto(value).each do |nn|\n pow << key ** nn\n end\n\n pows << pow\n end\n\n first_array = pows.delete_at(0)\n return first_array if pows.size == 0\n\n result = first_array.product(*pows)\n result.map do |item|\n item.inject { |sum, i| sum * i }\n end.sort\n end\nend\n\n\nn = gets.to_i\naa = gets.split.map(&:to_i)\ngcd = aa.inject(:gcd)\npf = NemiLib.new.prime_factorization(gcd)\nputs (pf.size==1 ? 1 : pf.each_value.inject(1){|sum,i| sum*(i+1)})"}, {"source_code": "class NemiLib\n # 素因数分解ハッシュ形式\n # (例)100なら{2=>2,5=>2}\n def prime_factorization(num, result = Hash.new(0))\n return {} if num < 1\n return {1=>1} if num == 1\n\n divides = false\n to = Math.sqrt(num).floor\n # TODO うまくかきたい。2だけ別処理\n if num % 2 == 0\n result[2] += 1\n prime_factorization(num / 2, result)\n divides = true\n end\n 3.step(to, 2) do |item|\n if num % item == 0\n result[item] += 1\n prime_factorization(num / item, result)\n divides = true\n break\n end\n end\n # 何も割れないなら、その数を入れる\n result[num] += 1 unless divides\n\n result\n end\n\n # 約数\n def divisors(num)\n return [1] if num == 1\n\n pows = []\n # 0乗、1乗・・・した値を入れる\n prime_factorization(num).each_pair do |key, value|\n pow = []\n 0.upto(value).each do |nn|\n pow << key ** nn\n end\n\n pows << pow\n end\n\n first_array = pows.delete_at(0)\n return first_array if pows.size == 0\n\n result = first_array.product(*pows)\n result.map do |item|\n item.inject { |sum, i| sum * i }\n end.sort\n end\nend\n\n\nn = gets.to_i\naa = gets.split.map(&:to_i)\ngcd = aa.inject(:gcd)\npf = NemiLib.new.prime_factorization(gcd)\nputs pf.each_value.inject(1){|sum,i| sum*(i+1)}\n"}, {"source_code": "class NemiLib\n # 素因数分解ハッシュ形式\n # (例)100なら{2=>2,5=>2}\n def prime_factorization(num, result = Hash.new(0))\n return {} if num < 1\n return {1=>1} if num == 1\n\n divides = false\n to = Math.sqrt(num).floor\n # TODO うまくかきたい。2だけ別処理\n if num % 2 == 0\n result[2] += 1\n prime_factorization(num / 2, result)\n divides = true\n end\n 3.step(to, 2) do |item|\n if num % item == 0\n result[item] += 1\n prime_factorization(num / item, result)\n divides = true\n break\n end\n end\n # 何も割れないなら、その数を入れる\n result[num] += 1 unless divides\n\n result\n end\n\n # 約数\n def divisors(num)\n return [1] if num == 1\n\n pows = []\n # 0乗、1乗・・・した値を入れる\n prime_factorization(num).each_pair do |key, value|\n pow = []\n 0.upto(value).each do |nn|\n pow << key ** nn\n end\n\n pows << pow\n end\n\n first_array = pows.delete_at(0)\n return first_array if pows.size == 0\n\n result = first_array.product(*pows)\n result.map do |item|\n item.inject { |sum, i| sum * i }\n end.sort\n end\nend\n\n\nn = gets.to_i\naa = gets.split.map(&:to_i)\ngcd = aa.inject(:gcd)\npf = NemiLib.new.prime_factorization(gcd)\nputs (pf.size==1 ? 1 : pf.each_value.inject(1){|sum,i| sum*(i+1)})"}], "src_uid": "6379ec153377fb9c30a7b897659da7d6"} {"nl": {"description": "One day n friends met at a party, they hadn't seen each other for a long time and so they decided to make a group photo together. Simply speaking, the process of taking photos can be described as follows. On the photo, each photographed friend occupies a rectangle of pixels: the i-th of them occupies the rectangle of width wi pixels and height hi pixels. On the group photo everybody stands in a line, thus the minimum pixel size of the photo including all the photographed friends, is W × H, where W is the total sum of all widths and H is the maximum height of all the photographed friends.As is usually the case, the friends made n photos — the j-th (1 ≤ j ≤ n) photo had everybody except for the j-th friend as he was the photographer.Print the minimum size of each made photo in pixels. ", "input_spec": "The first line contains integer n (2 ≤ n ≤ 200 000) — the number of friends. Then n lines follow: the i-th line contains information about the i-th friend. The line contains a pair of integers wi, hi (1 ≤ wi ≤ 10, 1 ≤ hi ≤ 1000) — the width and height in pixels of the corresponding rectangle.", "output_spec": "Print n space-separated numbers b1, b2, ..., bn, where bi — the total number of pixels on the minimum photo containing all friends expect for the i-th one.", "sample_inputs": ["3\n1 10\n5 5\n10 1", "3\n2 1\n1 2\n2 1"], "sample_outputs": ["75 110 60", "6 4 6"], "notes": null}, "positive_code": [{"source_code": "count = gets.to_i\nmas_width = Array.new(count)\nmas_height = Array.new(count)\nfor i in 0..(count - 1) \n string = gets\n mas_width[i] = string.split(' ')[0].to_i\n mas_height[i] = string.split(' ')[1].to_i\nend\n\nall_width = 0\nfor i in 0..(count - 1)\n all_width += mas_width[i]\nend\n\nnew_mas_height = Array.new(mas_height).sort\n\nmax = new_mas_height[count - 1]\nmax2 = new_mas_height[count - 2]\n\nindex_max = mas_height.index(max)\n\n\nfor i in 0..(count - 1)\n unless i == index_max\n print (all_width - mas_width[i]) * max\n else\n print (all_width - mas_width[i]) * max2\n end\n print ' ' unless i == count - 1\nend"}, {"source_code": "@data = $stdin.read.split\nn = @data.shift.to_i\nw = []\nh = []\nsum_w = 0\nprf_h = []\nsuf_h = []\nn.times do \n\tw << @data.shift.to_i\n\th << @data.shift.to_i\n\tprf_h << h.last\n\tif h.size > 1\n\t\tprf_h[-1] = [prf_h.last, prf_h[-2]].max\n\tend\n\tsuf_h << h.last\n\tsum_w += w.last\nend\n\nn.times do |i|\n\tnext if i == 0\n\tsuf_h[n - i - 1] = [suf_h[n-i-1], suf_h[n-i]].max\nend\n\nresult = []\nn.times do |i|\n\th_left = i == 0 ? 0 : prf_h[i - 1]\n\th_right = i == n - 1 ? 0 : suf_h[i + 1]\n\tresult << (sum_w - w[i]) * [h_left, h_right].max\nend\n\nputs result.join(' ')"}, {"source_code": "friends = gets.strip.to_i\nfr_sizes = Array.new(friends)\n\nw = 0\nh1,h2 = 0,0\n\n#read\nfriends.times do |n|\n pair = gets.strip.split(' ').map(&:to_i)\n w += pair[0]\n if h1 < pair[1] then \n h2 = h1\n h1 = pair[1] \n elsif h2 < pair[1] then\n h2 = pair[1]\n end\n fr_sizes[n] = pair\nend\n\nfr_sizes.each do |wf, hf|\n width = w - wf\n height = (hf == h1 ? h2 : h1)\n print \"#{width * height} \"\nend"}, {"source_code": "amount = gets.chomp.to_i\n\nfriends = []\nsum = 0\nmax1 = 0\nmax2 = 0\n\namount.times {\n\tx = gets.chomp.split(\" \")\n\tx[0] = x[0].to_i\n\tx[1] = x[1].to_i\n\tfriends.push(x)\n\tsum = sum + x[0]\n\n\tif x[1] >= max1\n\t\tmax1 = x[1]\n\tend\n\n\tif x[1] >= max2\n\t\tmax1 = max2\n\t\tmax2 = x[1]\n\tend\n}\n\nfriends.each { |x|\n\tif x[1] == max2\n\t\tprint max1 * (sum - x[0]), \" \"\n\telse\n\t\tprint max2 * (sum - x[0]), \" \"\n\tend\n}"}], "negative_code": [], "src_uid": "e1abc81cea4395ba675cf6ca93261ae8"} {"nl": {"description": "You are given an array $$$a$$$ consisting of $$$n$$$ nonnegative integers. Let's define the prefix OR array $$$b$$$ as the array $$$b_i = a_1~\\mathsf{OR}~a_2~\\mathsf{OR}~\\dots~\\mathsf{OR}~a_i$$$, where $$$\\mathsf{OR}$$$ represents the bitwise OR operation. In other words, the array $$$b$$$ is formed by computing the $$$\\mathsf{OR}$$$ of every prefix of $$$a$$$.You are asked to rearrange the elements of the array $$$a$$$ in such a way that its prefix OR array is lexicographically maximum.An array $$$x$$$ is lexicographically greater than an array $$$y$$$ if in the first position where $$$x$$$ and $$$y$$$ differ, $$$x_i > y_i$$$.", "input_spec": "The first line of the input contains a single integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 2 \\cdot 10^5$$$) — the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ nonnegative integers $$$a_1, \\ldots, a_n$$$ ($$$0 \\leq a_i \\leq 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case print $$$n$$$ integers — any rearrangement of the array $$$a$$$ that obtains the lexicographically maximum prefix OR array.", "sample_inputs": ["5\n\n4\n\n1 2 4 8\n\n7\n\n5 1 2 3 4 5 5\n\n2\n\n1 101\n\n6\n\n2 3 4 2 3 4\n\n8\n\n1 4 2 3 4 5 7 1"], "sample_outputs": ["8 4 2 1 \n5 2 1 3 4 5 5 \n101 1 \n4 3 2 2 3 4 \n7 1 4 2 3 4 5 1"], "notes": null}, "positive_code": [{"source_code": "require 'set'\ngets.to_i.times do\n n=gets.to_i\n a=gets.split.map(&:to_i)\n x=1<<32\n ans=[]\n b=0\n while x>0 do\n if b&x>0 then\n x>>=1\n next\n end\n c=[]\n a.each do |i|\n c << i if i&x>0\n end\n if c.empty? then\n x>>=1\n next\n end\n ans << c.max_by {|i| i|b}\n b|=ans[-1]\n a.delete_at(a.index(ans[-1]))\n x>>=1\n end\n puts (ans+a.to_a)*' '\nend\n"}], "negative_code": [], "src_uid": "64458fc3c2bf40ca911b566092f544e8"} {"nl": {"description": "You are given three positive (i.e. strictly greater than zero) integers $$$x$$$, $$$y$$$ and $$$z$$$.Your task is to find positive integers $$$a$$$, $$$b$$$ and $$$c$$$ such that $$$x = \\max(a, b)$$$, $$$y = \\max(a, c)$$$ and $$$z = \\max(b, c)$$$, or determine that it is impossible to find such $$$a$$$, $$$b$$$ and $$$c$$$.You have to answer $$$t$$$ independent test cases. Print required $$$a$$$, $$$b$$$ and $$$c$$$ in any (arbitrary) order.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 2 \\cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains three integers $$$x$$$, $$$y$$$, and $$$z$$$ ($$$1 \\le x, y, z \\le 10^9$$$).", "output_spec": "For each test case, print the answer: \"NO\" in the only line of the output if a solution doesn't exist; or \"YES\" in the first line and any valid triple of positive integers $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \\le a, b, c \\le 10^9$$$) in the second line. You can print $$$a$$$, $$$b$$$ and $$$c$$$ in any order. ", "sample_inputs": ["5\n3 2 3\n100 100 100\n50 49 49\n10 30 20\n1 1000000000 1000000000"], "sample_outputs": ["YES\n3 2 1\nYES\n100 100 100\nNO\nNO\nYES\n1 1 1000000000"], "notes": null}, "positive_code": [{"source_code": "gets.to_i.times do\n\ta = gets.split.map(&:to_i)\n\tx, y, z = a\n\tmin, max = a.minmax\n\tcase a.count(max)\n\twhen 3\n\t\tputs 'YES', a.join(' ')\n\twhen 2\n\t\tputs 'YES',\n\t\t (\n\t\t\t\tif x != max\n\t\t\t\t\t[min, min, max]\n\t\t\t\telsif y != max\n\t\t\t\t\t[min, max, min]\n\t\t\t\telse\n\t\t\t\t\t[max, min, min]\n\t\t\t\tend\n\t\t ).join(' ')\n\telse\n\t\tputs 'NO'\n\tend\nend\n"}, {"source_code": "# frozen_string_literal: true\n\nlines = gets.to_i\nlines.times do\n xyz = gets.split(' ').map(&:to_i).sort.reverse\n if xyz[0] == xyz[1]\n print 'YES', \"\\n\"\n print \"#{xyz[0]} #{xyz[2]} 1\", \"\\n\"\n else\n print 'NO', \"\\n\"\n end\nend\n"}, {"source_code": "t = gets.chomp.to_i\nt.times do\n atai = gets.chomp.split(\" \").map(&:to_i)\n atai = atai.sort\n if atai[0] != atai[1] && atai[1] != atai[2] && atai[0] != atai[2]\n puts \"NO\"\n elsif atai[0] == atai[1] && atai[1] == atai[2]\n puts \"YES\"\n print atai[0] \n print \" \"\n print atai[1]\n print \" \"\n print atai[2]\n print \"\\n\"\n elsif atai[0] == atai[1] && atai[1] != atai[2]\n puts \"NO\"\n elsif atai[2] == atai[1] && atai[1] != atai[0]\n puts \"YES\"\n print atai[0] \n print \" \"\n print atai[0]\n print \" \"\n print atai[2]\n print \"\\n\"\n end\n\nend"}, {"source_code": "tc = gets.to_i \n\ntc.times do \n a = gets.split().map{|i| i.to_i } \n maxi = a.max \n b = a.uniq() \n if(a.count(maxi) > 1) \n puts \"YES\"\n if(b.size == 2) \n print \"1 #{b[0]} #{b[1]}\" \n else \n print \"#{b[0]} \"*3\n end \n puts \"\"\n else \n puts \"NO\" \n end\nend"}, {"source_code": "def solve\n x, y, z = gets.strip.split(' ').map(&:to_i)\n mx = [x , y, z].max\n c = 0; nt = -1\n if mx == x\n c += 1\n else \n nt = x\n end\n if mx == y\n c += 1\n else \n nt = y\n end\n if mx == z\n c += 1\n else \n nt = z\n end\n\n if c >= 2\n puts \"YES\"\n if nt == -1\n puts \"#{x} #{y} #{z}\"\n else\n puts \"#{nt} #{nt} #{mx}\"\n end\n else\n puts \"NO\"\n end\nend\n\ngets.strip.to_i.times { solve }"}, {"source_code": "$, = ' '\ngets.to_i.times do\n a = gets.split.map(&:to_i).sort\n if a[1] != a[2]\n puts \"NO\"\n else\n print \"YES\", a[0], a[0], a[2], \"\\n\"\n end\nend "}, {"source_code": "def solve\n x, y, z = gets.strip.split(' ').map(&:to_i)\n mx = [x , y, z].max\n c = 0; nt = -1\n if mx == x\n c += 1\n else \n nt = x\n end\n if mx == y\n c += 1\n else \n nt = y\n end\n if mx == z\n c += 1\n else \n nt = z\n end\n\n if c >= 2\n puts \"YES\"\n if nt == -1\n puts \"#{x} #{y} #{z}\"\n else\n puts \"#{nt} #{nt} #{mx}\"\n end\n else\n puts \"NO\"\n end\nend\n\ngets.strip.to_i.times { solve }"}, {"source_code": "n = gets.to_i\nn.times do\n x,y,z = gets.chomp.split(\" \").map(&:to_i)\n max = [x,y,z].max\n if x == max && y == max && z == max\n puts \"YES\"\n puts \"#{max} #{max} #{max}\"\n elsif x == max && y == max\n puts \"YES\"\n puts \"#{z} #{max} #{z}\"\n elsif x == max && z == max\n puts \"YES\"\n puts \"#{max} #{y} #{y}\"\n elsif y == max && z == max\n puts \"YES\"\n puts \"#{x} #{x} #{max}\"\n else\n puts \"NO\"\n end\nend"}, {"source_code": "gets.to_i.times do\n a = gets.split.map(&:to_i).sort\n puts 'NO' if a[1] != a[2]\n puts \"YES\\n#{a[2]}\" + (\" #{a[0]}\" * 2) if a[1] == a[2]\nend\n\n"}, {"source_code": "gets.to_i.times do\n a = gets.split(' ').map(&:to_i).sort.reverse\n \n if a[0] != a[1]\n puts \"NO\"\n next\n end\n\n puts \"YES\"\n puts \"#{a.first} #{a.last} 1\"\nend\n"}], "negative_code": [{"source_code": "# frozen_string_literal: true\n\nlines = gets.to_i\nlines.times do\n xyz = gets.split(' ').map(&:to_i).sort.reverse\n if xyz[0] == xyz[1]\n print 'YES', \"\\n\"\n print \"#{xyz[0]} 1 1\", \"\\n\"\n else\n print 'NO', \"\\n\"\n end\nend\n"}, {"source_code": "# frozen_string_literal: true\n\nlines = gets.to_i\nlines.times do\n xyz = gets.split(' ').map(&:to_i).sort.reverse\n if xyz[0] == xyz[1]\n print 'YES', \"\\n\"\n print \"#{xyz[0]} #{xyz[1]} 1\", \"\\n\"\n else\n print 'NO', \"\\n\"\n end\nend\n"}, {"source_code": "t = gets.chomp.to_i\nt.times do\n x, y ,z = gets.chomp.split(\" \").map(&:to_i)\n if x != y && y != z\n puts \"NO\"\n elsif x == y && y == z\n puts \"YES\"\n puts \"#{x}\" +\" \" + \"#{y}\" + \" \" + \"#{z}\"\n elsif x == y && y != z && x > z\n puts \"YES\"\n puts \"#{z}\" +\" \" + \"#{y}\" + \" \" + \"#{z}\"\n elsif x == y && y != z && x < z\n puts \"NO\"\n elsif x == z && y != z && x > y\n puts \"YES\"\n puts \"#{x}\" +\" \" + \"#{y}\" + \" \" + \"#{y}\"\n elsif x == z && y != z && x < y\n puts \"NO\"\n elsif y == z && x != z && y > x\n puts \"YES\"\n puts \"#{x}\" +\" \" + \"#{x}\" + \" \" + \"#{z}\"\n elsif y == z && x != z && y < x\n puts \"NO\"\n end\nend\n"}, {"source_code": "t = gets.chomp.to_i\nt.times do\n atai = gets.chomp.split(\" \").map(&:to_i)\n atai = atai.sort\n if atai[0] != atai[1] && atai[1] != atai[2] && atai[0] != atai[2]\n puts \"NO\"\n elsif atai[0] != atai[1] && atai[1] != atai[2]\n puts \"YES\"\n print atai[0] \n print \" \"\n print atai[1]\n print \" \"\n print atai[2]\n print \"\\n\"\n elsif atai[0] == atai[1] && atai[1] != atai[2]\n puts \"NO\"\n elsif atai[2] == atai[1] && atai[1] != atai[0]\n puts \"YES\"\n print atai[0] \n print \" \"\n print atai[0]\n print \" \"\n print atai[2]\n print \"\\n\"\n end\n\nend"}], "src_uid": "f4804780d9c63167746132c35b2bdd02"} {"nl": {"description": " — Oh my sweet Beaverette, would you fancy a walk along a wonderful woodland belt with me? — Of course, my Smart Beaver! Let us enjoy the splendid view together. How about Friday night? At this point the Smart Beaver got rushing. Everything should be perfect by Friday, so he needed to prepare the belt to the upcoming walk. He needed to cut down several trees.Let's consider the woodland belt as a sequence of trees. Each tree i is described by the esthetic appeal ai — some trees are very esthetically pleasing, others are 'so-so', and some trees are positively ugly!The Smart Beaver calculated that he needed the following effects to win the Beaverette's heart: The first objective is to please the Beaverette: the sum of esthetic appeal of the remaining trees must be maximum possible; the second objective is to surprise the Beaverette: the esthetic appeal of the first and the last trees in the resulting belt must be the same; and of course, the walk should be successful: there must be at least two trees in the woodland belt left. Now help the Smart Beaver! Which trees does he need to cut down to win the Beaverette's heart?", "input_spec": "The first line contains a single integer n — the initial number of trees in the woodland belt, 2 ≤ n. The second line contains space-separated integers ai — the esthetic appeals of each tree. All esthetic appeals do not exceed 109 in their absolute value. to get 30 points, you need to solve the problem with constraints: n ≤ 100 (subproblem A1); to get 100 points, you need to solve the problem with constraints: n ≤ 3·105 (subproblems A1+A2). ", "output_spec": "In the first line print two integers — the total esthetic appeal of the woodland belt after the Smart Beaver's intervention and the number of the cut down trees k. In the next line print k integers — the numbers of the trees the Beaver needs to cut down. Assume that the trees are numbered from 1 to n from left to right. If there are multiple solutions, print any of them. It is guaranteed that at least two trees have equal esthetic appeal.", "sample_inputs": ["5\n1 2 3 1 2", "5\n1 -2 3 1 -2"], "sample_outputs": ["8 1\n1", "5 2\n2 5"], "notes": null}, "positive_code": [{"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\no={}\ns=[0]\nm=-10**10\nm1,m2=0\nfor i in 1..n do\n b=a[i-1]\n s[i]=s[i-1]+[0,b].max\n if o[b].nil?\n o[b]=i\n else\n c=s[i]-s[o[b]-1]+[2*b,0].min\n m,m1,m2=c,o[b],i if c>=m\n end\nend\nd=a.each_with_index.map {|x,i| i+1 if i=m1&&i=m2 }.compact\n#puts m1,m2\nputs \"#{m} #{d.size}\"\nputs d.join(' ')\n"}, {"source_code": "n = gets.to_i\na = gets.split(' ').map(&:to_i)\na.insert(0, 0)\ndp = []\ndp[0] = 0\nfor i in 1..n\n dp[i] = dp[i-1]\n dp[i] += a[i] if a[i] > 0\nend\nhash = Hash.new(false)\nret = nil\nseg = nil\nfor i in 1..n\n next if hash[a[i]]\n hash[a[i]] = true\n ind = a.rindex(a[i])\n if ind != i\n result = dp[ind] - dp[i-1]\n result += a[i] * 2 if a[i] < 0\n if ret == nil || ret < result\n ret = result\n seg = [i, ind]\n end\n end\nend\ntree = []\nfor i in 1...seg[0]\n tree << i\nend\nfor i in seg[0] + 1 ... seg[1]\n tree << i if a[i] < 0\nend\nfor i in seg[1] + 1 ..n\n tree << i\nend\nputs \"#{ret} #{tree.size}\"\nputs tree.join(' ')"}, {"source_code": "n = gets.to_i\na = gets.split(' ').map(&:to_i)\na.insert(0, 0)\nseg = {}\nfor i in 1..n\n sum = a[i]\n for j in i + 1..n\n sum += a[j] unless a[j] < 0\n if a[i] == a[j]\n sum += a[j] if a[j] < 0\n seg[[i, j]] = sum\n sum -= a[j] if a[j] < 0\n end\n end\nend\nret = nil\nkey = nil\nseg.each do |k, v|\n if ret.nil? || v > ret\n key = k\n ret = v\n end\nend\ntree = []\nfor i in 1...key[0]\n tree << i\nend\nfor i in key[0] + 1...key[1]\n tree << i if a[i] <= 0\nend\nfor i in key[1] + 1..n\n tree << i\nend\nputs \"#{ret} #{tree.size}\"\n\nputs tree.join(' ')"}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\no={}\ns=[0]\nm=-10**10\nm1,m2=0\nfor i in 1..n do\n b=a[i-1]\n s[i]=s[i-1]+[0,b].max\n if o[b].nil?\n o[b]=i\n else\n c=s[i]-s[o[b]-1]+[2*b,0].min\n m,m1,m2=c,o[b],i if c>=m\n end\nend\nd=a.each_with_index.map {|x,i| i+1 if i=m1&&i=m2 }.compact\n#puts m1,m2\nputs \"#{m} #{d.size}\"\nputs d.join(' ')\n"}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\no={}\ns=[0]\nm=-10**10\nm1,m2=0\nfor i in 1..n do\n b=a[i-1]\n s[i]=s[i-1]+[0,b].max\n if o[b].nil?\n o[b]=i\n else\n c=s[i]-s[o[b]-1]+[2*b,0].min\n m,m1,m2=c,o[b],i if c>=m\n end\nend\nd=a.each_with_index.map {|x,i| i+1 if i=m1&&i=m2 }.compact\n#puts m1,m2\nputs \"#{m} #{d.size}\"\nputs d.join(' ')\n"}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\no={}\ns=[0]\nm=-10**10\nm1,m2=0\nfor i in 1..n do\n b=a[i-1]\n s[i]=s[i-1]+[0,b].max\n if o[b].nil?\n o[b]=i\n else\n c=s[i]-s[o[b]-1]+[2*b,0].min\n m,m1,m2=c,o[b],i if c>=m\n end\nend\nd=a.each_with_index.map {|x,i| i+1 if i=m1&&i=m2 }.compact\n#puts m1,m2\nputs \"#{m} #{d.size}\"\nputs d.join(' ')\n"}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\no={}\ns=[0]\nm=-10**10\nm1,m2=0\nfor i in 1..n do\n b=a[i-1]\n s[i]=s[i-1]+[0,b].max\n if o[b].nil?\n o[b]=i\n else\n c=s[i]-s[o[b]-1]+[2*b,0].min\n m,m1,m2=c,o[b],i if c>=m\n end\nend\nd=a.each_with_index.map {|x,i| i+1 if i=m1&&i=m2 }.compact\n#puts m1,m2\nputs \"#{m} #{d.size}\"\nputs d.join(' ')\n"}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\no={}\ns=[0]\nm=-10**10\nm1,m2=0\nfor i in 1..n do\n b=a[i-1]\n s[i]=s[i-1]+[0,b].max\n if o[b].nil?\n o[b]=i\n else\n c=s[i]-s[o[b]-1]+[2*b,0].min\n m,m1,m2=c,o[b],i if c>=m\n end\nend\nd=a.each_with_index.map {|x,i| i+1 if i=m1&&i=m2 }.compact\n#puts m1,m2\nputs \"#{m} #{d.size}\"\nputs d.join(' ')"}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\no={}\ns=[0]\nm=-10**10\nm1,m2=0\nfor i in 1..n do\n b=a[i-1]\n s[i]=s[i-1]+[0,b].max\n if o[b].nil?\n o[b]=i\n else\n c=s[i]-s[o[b]-1]+[2*b,0].min\n m,m1,m2=c,o[b],i if c>=m\n end\nend\nd=a.each_with_index.map {|x,i| i+1 if i=m1&&i=m2 }.compact\n#puts m1,m2\nputs \"#{m} #{d.size}\"\nputs d.join(' ')\n"}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\no={}\ns=[0]\nm=-10**10\nm1,m2=0\nfor i in 1..n do\n b=a[i-1]\n s[i]=s[i-1]+[0,b].max\n if o[b].nil?\n o[b]=i\n else\n c=s[i]-s[o[b]-1]+[2*b,0].min\n m,m1,m2=c,o[b],i if c>=m\n end\nend\nd=a.each_with_index.map {|x,i| i+1 if i=m1&&i=m2 }.compact\n#puts m1,m2\nputs \"#{m} #{d.size}\"\nputs d.join(' ')\n"}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\no={}\ns=[0]\nm=-10**10\nm1,m2=0\nfor i in 1..n do\n b=a[i-1]\n s[i]=s[i-1]+[0,b].max\n if o[b].nil?\n o[b]=i\n else\n c=s[i]-s[o[b]-1]+[2*b,0].min\n m,m1,m2=c,o[b],i if c>=m\n end\nend\nd=a.each_with_index.map {|x,i| i+1 if i=m1&&i=m2 }.compact\n#puts m1,m2\nputs \"#{m} #{d.size}\"\nputs d.join(' ')\n"}, {"source_code": "n = gets.to_i\na = gets.split(' ').map(&:to_i)\na.insert(0, 0)\ndp = []\ndp[0] = 0\npos = {}\nfor i in 1..n\n pos[a[i]] = [] if pos[a[i]].nil?\n pos[a[i]] << i\n dp[i] = dp[i-1]\n dp[i] += a[i] if a[i] > 0\nend\nhash = Hash.new(false)\nret = nil\nseg = nil\nfor i in 1..n\n next if hash[a[i]]\n hash[a[i]] = true\n ind = pos[a[i]][-1]\n if ind != i\n result = dp[ind] - dp[i-1]\n result += a[i] * 2 if a[i] < 0\n if ret == nil || ret < result\n ret = result\n seg = [i, ind]\n end\n end\nend\ntree = []\nfor i in 1...seg[0]\n tree << i\nend\nfor i in seg[0] + 1 ... seg[1]\n tree << i if a[i] < 0\nend\nfor i in seg[1] + 1 ..n\n tree << i\nend\nputs \"#{ret} #{tree.size}\"\nputs tree.join(' ')"}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\no={}\ns=[0]\nm=-10**10\nm1,m2=0\nfor i in 1..n do\n b=a[i-1]\n oo=o[b]\n s[i]=s[i-1]+(b>0?b:0)\n if oo.nil?\n o[b]=i\n else\n c=s[i]-s[oo-1]+(b>0?0:2*b)\n m,m1,m2=c,oo-1,i-1 if c>m\n end\nend\nd=(1..m1).to_a+(m2+2..n).to_a+a[m1+1..m2-1].each_with_index.map {|x,i| i+2+m1 if x<0 }.compact\n#puts m1,m2\nputs \"#{m} #{d.size}\"\nputs d.join(' ')\n"}], "negative_code": [{"source_code": "n = gets.to_i\na = gets.split(' ').map(&:to_i)\na.insert(0, 0)\nseg = {}\nfor i in 1..n\n sum = a[i]\n for j in i + 1...a.size\n sum += a[j] unless a[j] < 0\n if a[i] == a[j]\n sum += a[j] if a[j] < 0\n seg[[i, j]] = sum\n end\n end\nend\nret = nil\nkey = nil\nseg.each do |k, v|\n if ret.nil? || v > ret\n key = k\n ret = v\n end\nend\ntree = []\nfor i in 1...key[0]\n tree << i\nend\nfor i in key[0] + 1...key[1]\n tree << i if a[i] <= 0\nend\nfor i in key[1] + 1..n\n tree << i\nend\nputs \"#{ret} #{tree.size}\"\n\nputs tree.join(' ')"}, {"source_code": "n = gets.to_i\na = gets.split(' ').map(&:to_i)\nseg = {}\nfor i in 0...a.size\n sum = a[i]\n for j in i + 1...a.size\n sum += a[j] unless a[j] < 0\n if a[i] == a[j]\n sum += a[j] if a[j] < 0\n seg[[i+1, j+1]] = sum\n end\n end\nend\nret = nil\nkey = nil\nseg.each do |k, v|\n if ret.nil? || v > ret\n key = k\n ret = v\n end\nend\ntree = []\nfor i in 1...key[0]\n tree << i\nend\n\nfor i in key[0] + 1...key[1]\n tree << i if a[i - 1] <= 0\nend\nfor i in key[1] + 1..n\n tree << i\nend\nputs \"#{ret} #{tree.size}\"\n\nputs tree.join(' ')"}, {"source_code": "n = gets.to_i\na = gets.split(' ').map(&:to_i)\na.insert(0, 0)\ndp = []\ndp[0] = 0\nfor i in 1..n\n dp[i] = dp[i-1]\n dp[i] += a[i] if a[i] > 0\nend\nhash = Hash.new(false)\nret = nil\nseg = nil\nfor i in 1..n\n next if hash[a[i]]\n hash[a[i]] = true\n ind = a.rindex(a[i])\n if ind != i\n result = dp[ind] - dp[i-1]\n if ret == nil || ret < result\n ret = result\n seg = [i, ind]\n end\n end\nend\ntree = []\nfor i in 1...seg[0]\n tree << i\nend\nfor i in seg[0] + 1 ... seg[1]\n tree << i if a[i] < 0\nend\nfor i in seg[1] + 1 ..n\n tree << i\nend\nputs \"#{ret} #{tree.size}\"\nputs tree.join(' ')"}, {"source_code": "n = gets.to_i\na = gets.split(' ').map(&:to_i)\nseg = {}\nfor i in 0...a.size\n sum = a[i]\n for j in i + 1...a.size\n sum += a[j] unless a[j] < 0\n if a[i] == a[j]\n sum += a[j] if a[j] < 0\n seg[[i+1, j+1]] = sum\n end\n end\nend\nret = nil\nkey = nil\nseg.each do |k, v|\n if ret.nil? || v > ret\n key = k\n ret = v\n end\nend\ntree = []\nfor i in 1...key[0]\n tree << i\nend\nfor i in key[0]...key[1]\n tree << i if a[i - 1] <= 0\nend\nfor i in key[1]..n\n tree << i\nend\nputs \"#{ret} #{tree.size}\"\n\nputs tree.join(' ')"}, {"source_code": "n = gets.to_i\na = gets.split(' ').map(&:to_i)\na.insert(0, 0)\ndp = []\ndp[0] = 0\nfor i in 1..n\n dp[i] = dp[i-1]\n dp[i] += a[i] if a[i] > 0\nend\nhash = Hash.new(false)\nret = nil\nseg = nil\nfor i in 1..n\n next if hash[a[i]]\n hash[a[i]] = true\n ind = a.rindex(a[i])\n if ind != i\n result = dp[ind] - dp[i-1]\n if ret == nil || ret < result\n ret = result\n seg = [i, ind]\n end\n end\nend\ntree = []\nfor i in 1...seg[0]\n tree << i\nend\nfor i in seg[0] + 1 ... seg[1]\n tree << i if a[i] < 0\nend\nfor i in seg[1] + 1 ..n\n tree << i\nend\nputs \"#{ret} #{tree.size}\"\nputs tree.join(' ')"}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\no={}\ns=[0]\nm=-10**10\nm1,m2=0\nfor i in 1..n do\n b=a[i-1]\n oo=o[b]\n s[i]=s[i-1]+(b>0?b:0)\n if oo.nil?\n o[b]=i\n else\n c=s[i]-s[oo-1]+(b>0?0:2*b)\n m,m1,m2=c,oo-1,i-1 if c>m\n end\nend\nd=(0..m1-1).to_a+(m2+1..n-1).to_a+a[m1+1..m2-1].each_with_index.map {|x,i| i+1 if x<0 }.compact\nputs \"#{m} #{d.size}\"\nputs d.join(' ')\n"}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\no={}\ns=[0]\nm=-10**10\nm1,m2=0\nfor i in 1..n do\n b=a[i-1]\n oo=o[b]\n s[i]=s[i-1]+b>0?b:0\n if oo.nil?\n o[b]=i\n else\n c=s[i]-s[oo-1]+b>0?0:2*b\n m,m1,m2=c,oo-1,i-1 if c>m\n end\nend\nd=(0..m1).to_a+(m2..n-1).to_a+a[m1+1..m2-1].each_with_index.map {|x,i| i+1 if x<0 }.compact\nputs \"#{m} #{d.size}\"\nputs d.join(' ')\n"}], "src_uid": "b3418f53720fb9eb990d6e99b07fd61b"} {"nl": {"description": "Every person likes prime numbers. Alice is a person, thus she also shares the love for them. Bob wanted to give her an affectionate gift but couldn't think of anything inventive. Hence, he will be giving her a graph. How original, Bob! Alice will surely be thrilled!When building the graph, he needs four conditions to be satisfied: It must be a simple undirected graph, i.e. without multiple (parallel) edges and self-loops. The number of vertices must be exactly $$$n$$$ — a number he selected. This number is not necessarily prime. The total number of edges must be prime. The degree (i.e. the number of edges connected to the vertex) of each vertex must be prime. Below is an example for $$$n = 4$$$. The first graph (left one) is invalid as the degree of vertex $$$2$$$ (and $$$4$$$) equals to $$$1$$$, which is not prime. The second graph (middle one) is invalid as the total number of edges is $$$4$$$, which is not a prime number. The third graph (right one) is a valid answer for $$$n = 4$$$. Note that the graph can be disconnected.Please help Bob to find any such graph!", "input_spec": "The input consists of a single integer $$$n$$$ ($$$3 \\leq n \\leq 1\\,000$$$) — the number of vertices.", "output_spec": "If there is no graph satisfying the conditions, print a single line containing the integer $$$-1$$$. Otherwise, first print a line containing a prime number $$$m$$$ ($$$2 \\leq m \\leq \\frac{n(n-1)}{2}$$$) — the number of edges in the graph. Then, print $$$m$$$ lines, the $$$i$$$-th of which containing two integers $$$u_i$$$, $$$v_i$$$ ($$$1 \\leq u_i, v_i \\leq n$$$) — meaning that there is an edge between vertices $$$u_i$$$ and $$$v_i$$$. The degree of each vertex must be prime. There must be no multiple (parallel) edges or self-loops. If there are multiple solutions, you may print any of them. Note that the graph can be disconnected.", "sample_inputs": ["4", "8"], "sample_outputs": ["5\n1 2\n1 3\n2 3\n2 4\n3 4", "13\n1 2\n1 3\n2 3\n1 4\n2 4\n1 5\n2 5\n1 6\n2 6\n1 7\n1 8\n5 8\n7 8"], "notes": "NoteThe first example was described in the statement.In the second example, the degrees of vertices are $$$[7, 5, 2, 2, 3, 2, 2, 3]$$$. Each of these numbers is prime. Additionally, the number of edges, $$$13$$$, is also a prime number, hence both conditions are satisfied. "}, "positive_code": [{"source_code": "def make(idx,n)\n ans = []\n if n == 4\n ans << [idx,idx+1]\n ans << [idx,idx+2]\n ans << [idx+1,idx+2]\n ans << [idx+1,idx+3]\n ans << [idx+2,idx+3]\n else\n ans << [idx,idx+n-1]\n (n-1).times do |i|\n ans << [idx+i,idx+i+1]\n end\n end\n return ans\nend\n\nrequire 'prime'\nn = gets.to_i\nans = []\nif n.prime?\n ans = make(1,n)\nelsif n == 4\n ans = make(1,4)\nelse\n if n % 3 == 1\n ans = make(n-3,4)\n n -= 4\n elsif n % 3 == 2\n ans = make(n-4,5)\n n -= 5\n end\n (n/3).times do |i|\n ans << [i*3+1,i*3+2]\n ans << [i*3+2,i*3+3]\n ans << [i*3+1,i*3+3]\n end\n cnt = 0\n while !ans.length.prime?\n l,r = 6*(cnt/3) + cnt%3+1, 6*(cnt/3) +cnt%3+4\n ans << [l,r]\n cnt += 1\n end\nend\n\np ans.length\nans.each do |a|\n puts a.join(\" \")\nend"}], "negative_code": [{"source_code": "def make(idx,n)\n ans = []\n if n == 4\n ans << [idx,idx+1]\n ans << [idx,idx+2]\n ans << [idx+1,idx+2]\n ans << [idx+1,idx+3]\n ans << [idx+2,idx+3]\n else\n ans << [idx,idx+n-1]\n (n-1).times do |i|\n ans << [idx+i,idx+i+1]\n end\n end\n return ans\nend\n\nrequire 'prime'\nn = gets.to_i\nans = []\nif n.prime?\n ans = make(1,n)\nelsif n == 4\n ans = make(1,4)\nelse\n if n % 3 == 1\n ans = make(n-3,4)\n n -= 4\n elsif n % 3 == 2\n ans = make(n-4,5)\n n -= 5\n end\n (n/3).times do |i|\n ans << [i*3+1,i*3+2]\n ans << [i*3+2,i*3+3]\n ans << [i*3+1,i*3+3]\n end\n cnt = 0\n while !ans.length.prime?\n l,r = 6*(cnt/6) + cnt%3+1, 6*(cnt/6) +cnt%3+4\n ans << [l,r]\n cnt += 1\n end\nend\n\np ans.length\nans.each do |a|\n puts a.join(\" \")\nend"}], "src_uid": "17d29a0c2ab4e4be14fe3bdeb10d1e55"} {"nl": {"description": "After a terrifying forest fire in Berland a forest rebirth program was carried out. Due to it N rows with M trees each were planted and the rows were so neat that one could map it on a system of coordinates so that the j-th tree in the i-th row would have the coordinates of (i, j). However a terrible thing happened and the young forest caught fire. Now we must find the coordinates of the tree that will catch fire last to plan evacuation.The burning began in K points simultaneously, which means that initially K trees started to burn. Every minute the fire gets from the burning trees to the ones that aren’t burning and that the distance from them to the nearest burning tree equals to 1.Find the tree that will be the last to start burning. If there are several such trees, output any.", "input_spec": "The first input line contains two integers N, M (1 ≤ N, M ≤ 2000) — the size of the forest. The trees were planted in all points of the (x, y) (1 ≤ x ≤ N, 1 ≤ y ≤ M) type, x and y are integers. The second line contains an integer K (1 ≤ K ≤ 10) — amount of trees, burning in the beginning. The third line contains K pairs of integers: x1, y1, x2, y2, ..., xk, yk (1 ≤ xi ≤ N, 1 ≤ yi ≤ M) — coordinates of the points from which the fire started. It is guaranteed that no two points coincide.", "output_spec": "Output a line with two space-separated integers x and y — coordinates of the tree that will be the last one to start burning. If there are several such trees, output any.", "sample_inputs": ["3 3\n1\n2 2", "3 3\n1\n1 1", "3 3\n2\n1 1 3 3"], "sample_outputs": ["1 1", "3 3", "2 2"], "notes": null}, "positive_code": [{"source_code": "class Rect\n\tdef initialize(x, y, z)\n\t\t@a1 = x + y - z\n\t\t@a2 = x + y + z\n\t\t@b1 = x - y - z\n\t\t@b2 = x - y + z\n\t\t@valid = z >= 0\n\tend\n\n\t[:a1, :a2, :b1, :b2].each do |i|\n\t\tclass_eval <<-\"end_eval\"\n\t\t\tdef #{i}\n\t\t\t\t@#{i}\n\t\t\tend\n\n\t\t\tattr_writer :#{i}\n\t\tend_eval\n\tend\n\n\tdef valid?\n\t\t@valid &&= (@a1 <= @a2 && @b1 <= @b2)\n\tend\n\n\tdef invalid!\n\t\t@valid = false\n\tend\n\n\tdef cut!(x1, y1, x2, y2)\n\t\t@a1 = [@a1, x1 + y1].max\n\t\t@a2 = [@a2, x2 + y2].min\n\t\t@b1 = [@b1, x1 - y2].max\n\t\t@b2 = [@b2, x2 - y1].min\n\tend\n\n\tdef **(o)\n\t\t!(@a2 <= o.a1 || o.a2 <= @a1) && !(@b2 <= o.b1 || o.b2 <= @b1)\n\tend\n\n\tdef /(o)\n\t\tr = []\n\t\tif @a1 <= o.a1\n\t\t\tr << self.clone\n\t\t\tr[-1].a2 = o.a1\n\t\t\t@a1 = o.a1\n\t\tend\n\t\tif @a2 >= o.a2\n\t\t\tr << self.clone\n\t\t\tr[-1].a1 = o.a2\n\t\t\t@a2 = o.a2\n\t\tend\n\t\tif @b1 <= o.b1\n\t\t\tr << self.clone\n\t\t\tr[-1].b2 = o.b1\n\t\t\t@b1 = o.b1\n\t\tend\n\t\tif @b2 >= o.b2\n\t\t\tr << self.clone\n\t\t\tr[-1].b1 = o.b2\n\t\t\t@b2 = o.b2\n\t\tend\n\t\tr\n\tend\nend\n\ndef gao(n, m, s, a)\n\tb = [Rect.new(0, 0, 10000)]\n\ta.each do |x, y|\n\t\tc = Rect.new(x, y, s)\n\t\tb.size.times do |j|\n\t\t\tnext unless b[j] ** c\n\t\t\tb += b[j] / c\n\t\t\tb[j].invalid!\n\t\tend\n\t\tb.reject!{|_| !_.valid?}\n\tend\n\tb.map!{|_| _ / _}.flatten!\n\tb.each do |_|\n\t\tif _.a1 == _.a2\n\t\t\ts = _.a1\n\t\t\t_.cut!([1, s - m].max, [1, s - n].max, [n, s - 1].min, [m, s - 1].min)\n\t\t\t_.b1 += 1 unless s.odd? == _.b1.odd?\t## !!\n\t\t\t_.b2 -= 1 unless s.odd? == _.b2.odd?\n\t\telse\n\t\t\ts = _.b1\n\t\t\t_.cut!([1, 1 + s].max, [1, 1 - s].max, [n, m + s].min, [m, n - s].min)\n\t\t\t_.a1 += 1 unless s.odd? == _.a1.odd?\t## !!\n\t\t\t_.a2 -= 1 unless s.odd? == _.a2.odd?\n\t\tend\n\tend\n\tb.reject!{|_| !_.valid?}\nend\n\nI = open('input.txt')\nO = open('output.txt', 'w')\n\nn, m = I.gets.split.map(&:to_i)\nk = I.gets.to_i\nt = I.gets.split.map(&:to_i)\na = k.times.map{|i| [t[i + i], t[i + i + 1]]}\n\nl = 0\nr = n + m\nwhile l < r do\n\ts = (l + r) / 2\n\tb = gao(n, m, s, a)\n\tunless b.empty? then\n\t\tl = s + 1\n\telse\n\t\tr = s\n\tend\nend\nr -= 1\n\nif r == 0\n\t# RE8\n\tO.puts \"1 1\"\nelse\n\tq = []\n\tgao(n, m, r, a).each do |_|\n\t\t_.a1.upto(_.a2) do |i|\n\t\t\t_.b1.upto(_.b2) do |j|\n\t\t\t#\tq << [(i + j) / 2, (i - j) / 2] if (i + j).even? && (i - j).even?\n\t\t\t\tif (i + j).even? && (i - j).even?\n\t\t\t\t\tO.puts \"#{(i + j) / 2} #{(i - j) / 2}\"\n\t\t\t\t\texit\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\tend\nend\n# q.sort!.uniq!\n# O.puts \"#{q[0][0]} #{q[0][1]}\"\n\n"}], "negative_code": [{"source_code": "class Rect\n\tdef initialize(x, y, z)\n\t\t@a1 = x + y - z\n\t\t@a2 = x + y + z\n\t\t@b1 = x - y - z\n\t\t@b2 = x - y + z\n\t\t@valid = z >= 0\n\tend\n\n\t[:a1, :a2, :b1, :b2].each do |i|\n\t\tclass_eval <<-\"end_eval\"\n\t\t\tdef #{i}\n\t\t\t\t@#{i}\n\t\t\tend\n\n\t\t\tattr_writer :#{i}\n\t\tend_eval\n\tend\n\n\tdef valid?\n\t\t@valid &&= (@a1 <= @a2 && @b1 <= @b2)\n\tend\n\n\tdef invalid!\n\t\t@valid = false\n\tend\n\n\tdef cut!(x1, y1, x2, y2)\n\t\t@a1 = [@a1, x1 + y1].max\n\t\t@a2 = [@a2, x2 + y2].min\n\t\t@b1 = [@b1, x1 - y2].max\n\t\t@b2 = [@b2, x2 - y1].min\n\tend\n\n\tdef **(o)\n\t\t!(@a2 <= o.a1 || o.a2 <= @a1) && !(@b2 <= o.b1 || o.b2 <= @b1)\n\tend\n\n\tdef /(o)\n\t\tr = []\n\t\tif @a1 <= o.a1\n\t\t\tr << self.clone\n\t\t\tr[-1].a2 = o.a1\n\t\t\t@a1 = o.a1\n\t\tend\n\t\tif @a2 >= o.a2\n\t\t\tr << self.clone\n\t\t\tr[-1].a1 = o.a2\n\t\t\t@a2 = o.a2\n\t\tend\n\t\tif @b1 <= o.b1\n\t\t\tr << self.clone\n\t\t\tr[-1].b2 = o.b1\n\t\t\t@b1 = o.b1\n\t\tend\n\t\tif @b2 >= o.b2\n\t\t\tr << self.clone\n\t\t\tr[-1].b1 = o.b2\n\t\t\t@b2 = o.b2\n\t\tend\n\t\tr\n\tend\nend\n\ndef gao(n, m, s, a)\n\tb = [Rect.new(0, 0, 10000)]\n\ta.each do |x, y|\n\t\tc = Rect.new(x, y, s)\n\t\tb.size.times do |j|\n\t\t\tnext unless b[j] ** c\n\t\t\tb += b[j] / c\n\t\t\tb[j].invalid!\n\t\tend\n\t\tb.reject!{|_| !_.valid?}\n\tend\n\tb.map!{|_| _ / _}.flatten!\n\tb.each do |_|\n\t\tif _.a1 == _.a2\n\t\t\ts = _.a1\n\t\t\t_.cut!([1, s - m].max, [1, s - n].max, [n, s - 1].min, [m, s - 1].min)\n\t\telse\n\t\t\ts = _.b1\n\t\t\t_.cut!([1, 1 + s].max, [1, 1 - s].max, [n, m + s].min, [m, n - s].min)\n\t\tend\n\tend\n\tb.reject!{|_| !_.valid?}\nend\n\nI = open('input.txt')\nO = open('output.txt', 'w')\n\nn, m = I.gets.split.map(&:to_i)\nk = I.gets.to_i\nt = I.gets.split.map(&:to_i)\na = k.times.map{|i| [t[i + i], t[i + i + 1]]}\n\nl = 0\nr = n + m\nwhile l < r do\n\ts = (l + r) / 2\n\tb = gao(n, m, s, a)\n\tunless b.empty? then\n\t\tl = s + 1\n\telse\n\t\tr = s\n\tend\nend\nr -= 1\n\nif r <= 1\n\t# RE8\n\tO.puts \"1 1\"\nelse\n\tq = []\n\tgao(n, m, r, a).each do |_|\n\t\t_.a1.upto(_.a2) do |i|\n\t\t\t_.b1.upto(_.b2) do |j|\n\t\t\t#\tq << [(i + j) / 2, (i - j) / 2] if (i + j).even? && (i - j).even?\n\t\t\t\tif (i + j).even? && (i - j).even?\n\t\t\t\t\tO.puts \"#{(i + j) / 2} #{(i - j) / 2}\"\n\t\t\t\t\texit\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\tend\nend\n# q.sort!.uniq!\n# O.puts \"#{q[0][0]} #{q[0][1]}\"\n\n"}], "src_uid": "1a740b0ad2ec3ed208f01fc7b64e00d4"} {"nl": {"description": "This is an interactive problem.Imur Ishakov decided to organize a club for people who love to play the famous game «The hat». The club was visited by n students, where n is even. Imur arranged them all in a circle and held a draw to break the students in pairs, but something went wrong. The participants are numbered so that participant i and participant i + 1 (1 ≤ i ≤ n - 1) are adjacent, as well as participant n and participant 1. Each student was given a piece of paper with a number in such a way, that for every two adjacent students, these numbers differ exactly by one. The plan was to form students with the same numbers in a pair, but it turned out that not all numbers appeared exactly twice.As you know, the most convenient is to explain the words to the partner when he is sitting exactly across you. Students with numbers i and sit across each other. Imur is wondering if there are two people sitting across each other with the same numbers given. Help him to find such pair of people if it exists.You can ask questions of form «which number was received by student i?», and the goal is to determine whether the desired pair exists in no more than 60 questions.", "input_spec": "At the beginning the even integer n (2 ≤ n ≤ 100 000) is given — the total number of students. You are allowed to ask no more than 60 questions.", "output_spec": "To ask the question about the student i (1 ≤ i ≤ n), you should print «? i». Then from standard output you can read the number ai received by student i ( - 109 ≤ ai ≤ 109). When you find the desired pair, you should print «! i», where i is any student who belongs to the pair (1 ≤ i ≤ n). If you determined that such pair doesn't exist, you should output «! -1». In both cases you should immediately terminate the program. The query that contains your answer is not counted towards the limit of 60 queries. Please make sure to flush the standard output after each command. For example, in C++ use function fflush(stdout), in Java call System.out.flush(), in Pascal use flush(output) and stdout.flush() for Python language. Hacking Use the following format for hacking: In the first line, print one even integer n (2 ≤ n ≤ 100 000) — the total number of students. In the second line print n integers ai ( - 109 ≤ ai ≤ 109) separated by spaces, where ai is the number to give to i-th student. Any two adjacent elements, including n and 1, must differ by 1 or  - 1. The hacked solution will not have direct access to the sequence ai.", "sample_inputs": ["8\n\n\n\n2\n\n\n\n2", "6\n\n\n\n1\n\n\n\n2\n\n\n\n3 \n\n\n\n2\n\n\n\n1\n\n\n\n0"], "sample_outputs": ["? 4\n\n\n\n? 8\n\n\n\n! 4", "? 1\n\n\n\n? 2\n\n\n\n? 3\n\n\n\n? 4\n\n\n\n? 5\n\n\n\n? 6\n\n\n\n! -1"], "notes": "NoteInput-output in statements illustrates example interaction.In the first sample the selected sequence is 1, 2, 1, 2, 3, 4, 3, 2In the second sample the selection sequence is 1, 2, 3, 2, 1, 0."}, "positive_code": [{"source_code": "def getnum(i)\n puts \"? #{i+1}\"\n $stdout.flush\n $info[i] = gets.to_i\nend\n\nN = gets.to_i\n$info = Array.new(N+1)\n\ngetnum(0)\ngetnum(N/2)\n$info[N] = $info[0]\n\ndiff = ($info[0] - $info[N/2]).abs\nif diff > N/2 || diff % 2 == 1\n puts \"! -1\"\n exit\nend\nif diff == 0\n puts \"! 1\"\n exit\nend\n\nllb = 0\nlub = N/2\nrlb = N/2\nrub = N\n\nloop do\n left = (llb+lub)/2\n right = (rlb+rub)/2\n getnum(left)\n getnum(right)\n if $info[left] == $info[right]\n puts \"! #{left+1}\"\n exit\n end\n if ($info[llb] > $info[rlb]) == ($info[left] > $info[right])\n llb = left\n rlb = right\n else\n lub = left\n rub = right\n end\nend\n"}], "negative_code": [], "src_uid": "49846b8fb0aea6b21e7986e19b8c8316"} {"nl": {"description": "Ilya lives in a beautiful city of Chordalsk.There are $$$n$$$ houses on the street Ilya lives, they are numerated from $$$1$$$ to $$$n$$$ from left to right; the distance between every two neighboring houses is equal to $$$1$$$ unit. The neighboring houses are $$$1$$$ and $$$2$$$, $$$2$$$ and $$$3$$$, ..., $$$n-1$$$ and $$$n$$$. The houses $$$n$$$ and $$$1$$$ are not neighboring.The houses are colored in colors $$$c_1, c_2, \\ldots, c_n$$$ so that the $$$i$$$-th house is colored in the color $$$c_i$$$. Everyone knows that Chordalsk is not boring, so there are at least two houses colored in different colors.Ilya wants to select two houses $$$i$$$ and $$$j$$$ so that $$$1 \\leq i < j \\leq n$$$, and they have different colors: $$$c_i \\neq c_j$$$. He will then walk from the house $$$i$$$ to the house $$$j$$$ the distance of $$$(j-i)$$$ units.Ilya loves long walks, so he wants to choose the houses so that the distance between them is the maximum possible.Help Ilya, find this maximum possible distance.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$3 \\leq n \\leq 300\\,000$$$) — the number of cities on the street. The second line contains $$$n$$$ integers $$$c_1, c_2, \\ldots, c_n$$$ ($$$1 \\leq c_i \\leq n$$$) — the colors of the houses. It is guaranteed that there is at least one pair of indices $$$i$$$ and $$$j$$$ so that $$$1 \\leq i < j \\leq n$$$ and $$$c_i \\neq c_j$$$.", "output_spec": "Print a single integer — the maximum possible distance Ilya can walk.", "sample_inputs": ["5\n1 2 3 2 3", "3\n1 2 1", "7\n1 1 3 1 1 1 1"], "sample_outputs": ["4", "1", "4"], "notes": "NoteIn the first example the optimal way is to walk from the first house to the last one, where Ilya can walk the distance of $$$5-1 = 4$$$ units.In the second example the optimal way is to either walk from the first house to the second or from the second to the third. Both these ways have the distance of $$$1$$$ unit.In the third example the optimal way is to walk from the third house to the last one, where Ilya can walk the distance of $$$7-3 = 4$$$ units. "}, "positive_code": [{"source_code": "N = gets.to_i\nC = gets.split.map(&:to_i)\n\nd1 = C.rindex { |c| C[0] != c }\nd2 = N - C.index { |c| C[-1] != c } - 1\n\nputs [d1, d2].max\n"}, {"source_code": "n = gets.to_i\nc = gets.split.map(&:to_i)\nans = 0\nfor i in 1..n-1\n if c[0] != c[i]\n ans = i\n end\nend\n(n-2).downto(0) do |i|\n if c[-1] != c[i] && ans < n-1-i\n ans = n-1-i\n end\nend\nputs ans"}, {"source_code": "n = gets.to_i\nc = gets.split.map(&:to_i)\nl = []\nr = []\nn.times do |i|\n break if l.length == 2\n if l.length == 0\n l << [c[i],i]\n elsif l.length == 1 && l[0][0] != c[i]\n l << [c[i],i]\n end\nend\nn.times do |i|\n break if r.length == 2\n if r.length == 0\n r << [c[-i-1],n-i-1]\n elsif r.length == 1 && r[0][0] != c[-i-1]\n r << [c[-i-1],n-i-1]\n end\nend\nans = 0\nif l[0][0] != r[0][0]\n ans = r[0][1] - l[0][1]\nelse\n ans = [r[1][1] - l[0][1],r[0][1] - l[1][1]].max\nend\np ans\n"}], "negative_code": [], "src_uid": "101fec8d8e169f941e71281048468121"} {"nl": {"description": "Moscow is hosting a major international conference, which is attended by n scientists from different countries. Each of the scientists knows exactly one language. For convenience, we enumerate all languages of the world with integers from 1 to 109.In the evening after the conference, all n scientists decided to go to the cinema. There are m movies in the cinema they came to. Each of the movies is characterized by two distinct numbers — the index of audio language and the index of subtitles language. The scientist, who came to the movie, will be very pleased if he knows the audio language of the movie, will be almost satisfied if he knows the language of subtitles and will be not satisfied if he does not know neither one nor the other (note that the audio language and the subtitles language for each movie are always different). Scientists decided to go together to the same movie. You have to help them choose the movie, such that the number of very pleased scientists is maximum possible. If there are several such movies, select among them one that will maximize the number of almost satisfied scientists.", "input_spec": "The first line of the input contains a positive integer n (1 ≤ n ≤ 200 000) — the number of scientists. The second line contains n positive integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is the index of a language, which the i-th scientist knows. The third line contains a positive integer m (1 ≤ m ≤ 200 000) — the number of movies in the cinema. The fourth line contains m positive integers b1, b2, ..., bm (1 ≤ bj ≤ 109), where bj is the index of the audio language of the j-th movie. The fifth line contains m positive integers c1, c2, ..., cm (1 ≤ cj ≤ 109), where cj is the index of subtitles language of the j-th movie. It is guaranteed that audio languages and subtitles language are different for each movie, that is bj ≠ cj. ", "output_spec": "Print the single integer — the index of a movie to which scientists should go. After viewing this movie the number of very pleased scientists should be maximum possible. If in the cinema there are several such movies, you need to choose among them one, after viewing which there will be the maximum possible number of almost satisfied scientists. If there are several possible answers print any of them.", "sample_inputs": ["3\n2 3 2\n2\n3 2\n2 3", "6\n6 3 1 1 3 7\n5\n1 2 3 4 5\n2 3 4 5 1"], "sample_outputs": ["2", "1"], "notes": "NoteIn the first sample, scientists must go to the movie with the index 2, as in such case the 1-th and the 3-rd scientists will be very pleased and the 2-nd scientist will be almost satisfied.In the second test case scientists can go either to the movie with the index 1 or the index 3. After viewing any of these movies exactly two scientists will be very pleased and all the others will be not satisfied. "}, "positive_code": [{"source_code": "#!/usr/bin/ruby\nclass Counter < Hash\n\tdef initialize(a=[])\n\t\tsuper\n\t\tself.default=0\n\t\ta.each{|e|self[e]+=1}\n\tend\n\tdef self.[](*a)\n\t\tnew(a)\n\tend\nend\ndef Counter(a=[]) Counter.new(a) end\n\nn=gets.to_i\nb=Counter(gets.split.map(&:to_i))\nm=gets.to_i\np gets.split.map(&:to_i).zip(gets.split.map(&:to_i)).each_with_index.map{|(k,l),z|[b[k],b[l],z+1]}.max[2]"}, {"source_code": "n = readline.to_i\narr = readline.split(' ').collect(&:to_i)\nmap = Hash.new(0)\narr.each{|n| map[n] = map[n] + 1}\n\nm = readline.to_i\nb = readline.split(' ').collect(&:to_i)\nc = readline.split(' ').collect(&:to_i)\nbesti, bestb, bestc = -1, -1, -1\nb.zip(c).each_with_index do |item, ind|\n bb, cc = item\n thisb, thisc = map[bb], map[cc]\n if thisb > bestb || thisb == bestb && thisc > bestc\n besti, bestb, bestc = ind, thisb, thisc\n end\nend\n\nputs besti + 1\n"}, {"source_code": "parse_int = lambda { gets.split.map{|x| x.to_i} }\n\nscientists = gets.to_i\nsc_langs = parse_int.call\nfilms = gets.to_i\nfilm_voice = parse_int.call\nfilm_sub = parse_int.call\n\n#p film_voice.to_s\n#p film_sub.to_s\n\nppl_in_lang = Hash.new\nppl_in_lang.default = 0\n\nsc_langs.each { |_|\n if ppl_in_lang[_] == 0\n ppl_in_lang[_]=1\n else\n ppl_in_lang[_]+=1\n end\n}\n\n#p ppl_in_lang.to_s\n\nfilm_good, film_ok = [0]*films, [0]*films\n\nfilms.times { |_|\n film_good[_] += ppl_in_lang[film_voice[_]]\n film_ok[_] += ppl_in_lang[film_sub[_] ]\n}\n#p sc_langs.to_s\n#p sc_langs[3]\n\nbest = 0\nfilms.times { |_|\n best = _ if film_good[_] > film_good[best]\n best = _ if (film_good[_] == film_good[best]) and (film_ok[_] > film_ok[best])\n}\n#p film_good.to_s\n#p film_ok.to_s\n\np best+1\n\n"}], "negative_code": [], "src_uid": "74ddbcf74988940265985ec8c36bb299"} {"nl": {"description": "Alice and Bob are playing a fun game of tree tag.The game is played on a tree of $$$n$$$ vertices numbered from $$$1$$$ to $$$n$$$. Recall that a tree on $$$n$$$ vertices is an undirected, connected graph with $$$n-1$$$ edges.Initially, Alice is located at vertex $$$a$$$, and Bob at vertex $$$b$$$. They take turns alternately, and Alice makes the first move. In a move, Alice can jump to a vertex with distance at most $$$da$$$ from the current vertex. And in a move, Bob can jump to a vertex with distance at most $$$db$$$ from the current vertex. The distance between two vertices is defined as the number of edges on the unique simple path between them. In particular, either player is allowed to stay at the same vertex in a move. Note that when performing a move, a player only occupies the starting and ending vertices of their move, not the vertices between them.If after at most $$$10^{100}$$$ moves, Alice and Bob occupy the same vertex, then Alice is declared the winner. Otherwise, Bob wins.Determine the winner if both players play optimally.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 10^4$$$). Description of the test cases follows. The first line of each test case contains five integers $$$n,a,b,da,db$$$ ($$$2\\le n\\le 10^5$$$, $$$1\\le a,b\\le n$$$, $$$a\\ne b$$$, $$$1\\le da,db\\le n-1$$$)  — the number of vertices, Alice's vertex, Bob's vertex, Alice's maximum jumping distance, and Bob's maximum jumping distance, respectively. The following $$$n-1$$$ lines describe the edges of the tree. The $$$i$$$-th of these lines contains two integers $$$u$$$, $$$v$$$ ($$$1\\le u, v\\le n, u\\ne v$$$), denoting an edge between vertices $$$u$$$ and $$$v$$$. It is guaranteed that these edges form a tree structure. It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$10^5$$$.", "output_spec": "For each test case, output a single line containing the winner of the game: \"Alice\" or \"Bob\".", "sample_inputs": ["4\n4 3 2 1 2\n1 2\n1 3\n1 4\n6 6 1 2 5\n1 2\n6 5\n2 3\n3 4\n4 5\n9 3 9 2 5\n1 2\n1 6\n1 9\n1 3\n9 5\n7 9\n4 8\n4 3\n11 8 11 3 3\n1 2\n11 9\n4 9\n6 5\n2 10\n3 2\n5 9\n8 3\n7 4\n7 10"], "sample_outputs": ["Alice\nBob\nAlice\nAlice"], "notes": "NoteIn the first test case, Alice can win by moving to vertex $$$1$$$. Then wherever Bob moves next, Alice will be able to move to the same vertex on the next move. In the second test case, Bob has the following strategy to win. Wherever Alice moves, Bob will always move to whichever of the two vertices $$$1$$$ or $$$6$$$ is farthest from Alice. "}, "positive_code": [{"source_code": "gets.to_i.times{n,a,b,c,d=gets.split.map(&:to_i)\nh=Hash.new{[]}\n(2..n).map{p,q=gets.split.map(&:to_i);h[p]<<=q;h[q]<<=p}\ne=->s{z={};v=-1\nf=->c{z[c]||(z[c]=v+=1;h[c].map(&f);v-=1)}\nf[s];z}\ni=e[a]\nputs i[b]>c&&(e[i.max_by{|k,v|v}[0]].values.max>2*c)&&d>2*c ?\"Bob\":\"Alice\"}"}, {"source_code": "gets.to_i.times{n,a,b,c,d=gets.split.map(&:to_i)\nh=Hash.new{[]}\n(2..n).map{p,q=gets.split.map(&:to_i);h[p]<<=q;h[q]<<=p}\ne=->s{z={};v=-1\nf=->c{z[c]||(z[c]=v+=1;h[c].map(&f);v-=1)}\nf[s];z}\ni=e[a]\nputs i[b]>c&&e[i.max_by{|k,v|v}[0]].values.max>2*c&&d>2*c ?\"Bob\":\"Alice\"}"}, {"source_code": "r=->{gets.split.map &:to_i}\ngets.to_i.times{n,a,b,c,d=r[]\nh=Hash.new{[]}\n(2..n).map{p,q=r[];h[p]<<=q;h[q]<<=p}\ne=->s{z={};v=-1\nf=->c{z[c]||(z[c]=v+=1;h[c].map &f;v-=1)}\nf[s];z}\ni=e[a]\nputs i[b]>c&&e[i.max_by{|k,v|v}[0]].values.max>2*c&&d>2*c ?\"Bob\":\"Alice\"}"}, {"source_code": "r=->{gets.split.map &:to_i}\ngets.to_i.times{n,a,b,c,d=r[]\nh=Hash.new{[]}\n(2..n).map{p,q=r[];h[p]<<=q;h[q]<<=p}\ne=->s{z={};v=-1\nf=->c{z[c]||(z[c]=v+=1;h[c].map &f;v-=1)}\nf[s];z}\ni=e[a]\nputs i[b]>c&&e[i.max_by{|k,v|v}[0]].values.max>c*2&&d>c*2?\"Bob\":\"Alice\"}"}, {"source_code": "gets.to_i.times{n,a,b,c,d=gets.split.map &:to_i\nh=Hash.new{[]}\n(2..n).map{p,q=gets.split.map &:to_i;h[p]<<=q;h[q]<<=p}\ne=->s{z={};v=-1\nf=->c{z[c]||(z[c]=v+=1;h[c].map &f;v-=1)}\nf[s];z}\ni=e[a]\nputs i[b]>c&&e[i.max_by{|k,v|v}[0]].values.max>2*c&&d>2*c ?\"Bob\":\"Alice\"}"}, {"source_code": "gets.to_i.times{n,a,b,c,d=gets.split.map(&:to_i)\nh=Hash.new{[]}\n(2..n).map{p,q=gets.split.map(&:to_i);h[p]<<=q;h[q]<<=p}\ne=->s{z={};v=-1;\nf=->c{z[c]||(z[c]=v+=1;h[c].map(&f);v-=1)}\nf[s];z}\ni=e[a]\nputs i[b]>c&&(e[i.max_by{|k,v|v}[0]].values.max>2*c)&&d>2*c ?\"Bob\":\"Alice\"}\n"}], "negative_code": [], "src_uid": "2bfd566ef883efec5211b01552b45218"} {"nl": {"description": "There is a game called \"Unique Bid Auction\". You can read more about it here: https://en.wikipedia.org/wiki/Unique_bid_auction (though you don't have to do it to solve this problem).Let's simplify this game a bit. Formally, there are $$$n$$$ participants, the $$$i$$$-th participant chose the number $$$a_i$$$. The winner of the game is such a participant that the number he chose is unique (i. e. nobody else chose this number except him) and is minimal (i. e. among all unique values of $$$a$$$ the minimum one is the winning one).Your task is to find the index of the participant who won the game (or -1 if there is no winner). Indexing is $$$1$$$-based, i. e. the participants are numbered from $$$1$$$ to $$$n$$$.You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 2 \\cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) — the number of participants. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le n$$$), where $$$a_i$$$ is the $$$i$$$-th participant chosen number. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 \\cdot 10^5$$$ ($$$\\sum n \\le 2 \\cdot 10^5$$$).", "output_spec": "For each test case, print the answer — the index of the participant who won the game (or -1 if there is no winner). Note that the answer is always unique.", "sample_inputs": ["6\n2\n1 1\n3\n2 1 3\n4\n2 2 2 3\n1\n1\n5\n2 3 2 4 2\n6\n1 1 5 5 4 4"], "sample_outputs": ["-1\n2\n4\n1\n2\n-1"], "notes": null}, "positive_code": [{"source_code": "gets;$<.map{a=gets.split.map &:to_i;p (a.index(a.tally.filter{|k,v|v==1}.keys.min)||-2)+1}"}, {"source_code": "t = gets.chomp.to_i\nt.times do\n n = gets.chomp.to_i\n arr = gets.chomp.split(' ').map(&:to_i)\n dp = Hash.new(0)\n min = 10 ** 10\n arr.each do |el|\n dp[el] += 1\n end\n dp.each do |k, v|\n if v == 1\n min = [min, k].min\n end\n end\n puts (arr.index(min) || -2) + 1\nend"}, {"source_code": "t = gets.to_i\n\n1.upto t do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n list = a.tally.keep_if {|_, v| v == 1}.keys\n if list.empty?\n puts -1\n else\n puts a.index(list.min)+1\n end\nend\n"}, {"source_code": "t = gets.chomp.to_i\nt.times do\n n = gets.chomp.to_i\n a = gets.chomp.split(\" \").map(&:to_i)\n count = Hash.new(0)\n index = Hash.new(0)\n ans = []\n a.each_with_index do |i,j|\n count[i] += 1\n end\n #p index\n min = Float::INFINITY\n count.each do |i,j|\n if j == 1\n min = [min,i].min\n end\n end\n if min == Float::INFINITY\n puts \"-1\"\n else\n a.each_with_index do |i,j|\n if i == min\n puts j+1\n end\n end\n end\nend"}, {"source_code": "gets;$<.map{a=gets.split.map &:to_i;p (a.index(a.tally.filter{|k,v|v==1}.keys.min)||-2)+1}"}], "negative_code": [], "src_uid": "99d2b0386d101da802ac508ef8f7325b"} {"nl": {"description": "The only difference between easy and hard versions are constraints on $$$n$$$ and $$$k$$$.You are messaging in one of the popular social networks via your smartphone. Your smartphone can show at most $$$k$$$ most recent conversations with your friends. Initially, the screen is empty (i.e. the number of displayed conversations equals $$$0$$$).Each conversation is between you and some of your friends. There is at most one conversation with any of your friends. So each conversation is uniquely defined by your friend.You (suddenly!) have the ability to see the future. You know that during the day you will receive $$$n$$$ messages, the $$$i$$$-th message will be received from the friend with ID $$$id_i$$$ ($$$1 \\le id_i \\le 10^9$$$).If you receive a message from $$$id_i$$$ in the conversation which is currently displayed on the smartphone then nothing happens: the conversations of the screen do not change and do not change their order, you read the message and continue waiting for new messages.Otherwise (i.e. if there is no conversation with $$$id_i$$$ on the screen): Firstly, if the number of conversations displayed on the screen is $$$k$$$, the last conversation (which has the position $$$k$$$) is removed from the screen. Now the number of conversations on the screen is guaranteed to be less than $$$k$$$ and the conversation with the friend $$$id_i$$$ is not displayed on the screen. The conversation with the friend $$$id_i$$$ appears on the first (the topmost) position on the screen and all the other displayed conversations are shifted one position down. Your task is to find the list of conversations (in the order they are displayed on the screen) after processing all $$$n$$$ messages.", "input_spec": "The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le n, k \\le 200)$$$ — the number of messages and the number of conversations your smartphone can show. The second line of the input contains $$$n$$$ integers $$$id_1, id_2, \\dots, id_n$$$ ($$$1 \\le id_i \\le 10^9$$$), where $$$id_i$$$ is the ID of the friend which sends you the $$$i$$$-th message.", "output_spec": "In the first line of the output print one integer $$$m$$$ ($$$1 \\le m \\le min(n, k)$$$) — the number of conversations shown after receiving all $$$n$$$ messages. In the second line print $$$m$$$ integers $$$ids_1, ids_2, \\dots, ids_m$$$, where $$$ids_i$$$ should be equal to the ID of the friend corresponding to the conversation displayed on the position $$$i$$$ after receiving all $$$n$$$ messages.", "sample_inputs": ["7 2\n1 2 3 2 1 3 2", "10 4\n2 3 3 1 1 2 1 2 3 3"], "sample_outputs": ["2\n2 1", "3\n1 3 2"], "notes": "NoteIn the first example the list of conversations will change in the following way (in order from the first to last message): $$$[]$$$; $$$[1]$$$; $$$[2, 1]$$$; $$$[3, 2]$$$; $$$[3, 2]$$$; $$$[1, 3]$$$; $$$[1, 3]$$$; $$$[2, 1]$$$. In the second example the list of conversations will change in the following way: $$$[]$$$; $$$[2]$$$; $$$[3, 2]$$$; $$$[3, 2]$$$; $$$[1, 3, 2]$$$; and then the list will not change till the end. "}, "positive_code": [{"source_code": "MOD=10**9+7\ncnt=0; sum=0; prev=nil; can=true; h=Hash.new(0)\ndef gs() gets.chomp end\ndef gi() gets.to_i end\ndef gsmi() gets.chomp.split.map(&:to_i) end\n\ndef desc(ar) ar.sort!{|x,y|y<=>x} end\ndef min(a,b) a<=b ? a : b end\ndef max(a,b) a>=b ? a : b end\ndef sum(ar) ar.inject(:+) end\n\n#def bs(ar,eq,n) en=(eq==1 ? :>= : :>); ar.bsearch_index{|x|x.send(en,n)} end\ndef C(a,b) b==0||a==b ? 1 : (b=a-b if a/2k\n nn = dis.pop\n h[nn]=0\n end\nend\n\nputs \"#{dis.size}\\n#{dis.join(' ')}\""}, {"source_code": "n, k = gets.split.map(&:to_i)\nid = gets.split.map(&:to_i)\nans = []\nid.each {|idi|\n unless ans.include?(idi)\n ans.pop if ans.size == k\n ans.unshift(idi)\n end\n}\np ans.size\nputs ans.join(\" \")\n\n"}, {"source_code": "MOD=10**9+7\ncnt=0; sum=0; prev=nil; can=true; h=Hash.new(0)\ndef gs() gets.chomp end\ndef gi() gets.to_i end\ndef gsmi() gets.chomp.split.map(&:to_i) end\n\ndef desc(ar) ar.sort!{|x,y|y<=>x} end\ndef min(a,b) a<=b ? a : b end\ndef max(a,b) a>=b ? a : b end\ndef sum(ar) ar.inject(:+) end\n\n#def bs(ar,eq,n) en=(eq==1 ? :>= : :>); ar.bsearch_index{|x|x.send(en,n)} end\ndef C(a,b) b==0||a==b ? 1 : (b=a-b if a/2k\nend\n\nputs \"#{dis.size}\\n#{dis.join(' ')}\""}], "negative_code": [], "src_uid": "485a1ecf8f569b85327b99382bda9466"} {"nl": {"description": "There are $$$n$$$ rectangles in a row. You can either turn each rectangle by $$$90$$$ degrees or leave it as it is. If you turn a rectangle, its width will be height, and its height will be width. Notice that you can turn any number of rectangles, you also can turn all or none of them. You can not change the order of the rectangles.Find out if there is a way to make the rectangles go in order of non-ascending height. In other words, after all the turns, a height of every rectangle has to be not greater than the height of the previous rectangle (if it is such). ", "input_spec": "The first line contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 10^5$$$) — the number of rectangles. Each of the next $$$n$$$ lines contains two integers $$$w_i$$$ and $$$h_i$$$ ($$$1 \\leq w_i, h_i \\leq 10^9$$$) — the width and the height of the $$$i$$$-th rectangle.", "output_spec": "Print \"YES\" (without quotes) if there is a way to make the rectangles go in order of non-ascending height, otherwise print \"NO\". You can print each letter in any case (upper or lower).", "sample_inputs": ["3\n3 4\n4 6\n3 5", "2\n3 4\n5 5"], "sample_outputs": ["YES", "NO"], "notes": "NoteIn the first test, you can rotate the second and the third rectangles so that the heights will be [4, 4, 3].In the second test, there is no way the second rectangle will be not higher than the first one."}, "positive_code": [{"source_code": "def can_arrage_non_ascending(rects)\n\n limit = 10**10\n\n rects.each do |w, h|\n a, b = [w, h].sort\n if b <= limit\n limit = b\n elsif a <= limit\n limit = a\n else\n return false\n end\n end\n\n true\n\nend\n\ndef solution\n count = read_int\n rects = []\n count.times do\n rects << read_ints\n end\n\n puts can_arrage_non_ascending(rects) ? \"YES\" : \"NO\"\nend\n\ndef read_int\n gets.to_i\nend\n\ndef read_ints\n gets.split.map &:to_i\nend\n\ndef read_string\n gets.chomp\nend\n\nsolution\n"}, {"source_code": "N = gets.to_i\nwh = N.times.map{gets.split.map(&:to_i)}\n\nnum = 10**10\nwh.each do |arr|\n num = arr.select{|e| e <= num}.max\n unless num\n puts 'NO'\n exit\n end\nend\nputs 'YES'"}, {"source_code": "n = gets.to_i\nrects = []\nn.times do\n mn, mx = gets.strip.split.map(&:to_i)\n rects << [mn, mx].sort\nend\n\nmxh = rects[0][1]\npossible = true\n1.upto(n-1).each do |i|\n if rects[i][0] > mxh\n possible = false\n break\n else\n if rects[i][1] <= mxh\n mxh = rects[i][1]\n else\n mxh = rects[i][0]\n end\n end\nend\n\nputs (possible ? \"YES\" : \"NO\")"}, {"source_code": "\nx = gets.chomp.to_i\n\nbest = Integer(1e9+100)\nf = true\nfor i in 0... x\n y = gets.strip.split(' ').map(&:to_i);\n mx = [y[0], y[1]].max\n mn = [y[0] , y[1]].min\n if mx <= best\n best = mx;\n elsif mn <= best\n best = mn\n else\n f = false\n end\n\nend\nunless f\n print \"NO\"\nelse\n print \"YES\"\nend\n\n"}], "negative_code": [], "src_uid": "162fa942bc6eeb5164b19598da2f8bef"} {"nl": {"description": "The round carousel consists of $$$n$$$ figures of animals. Figures are numbered from $$$1$$$ to $$$n$$$ in order of the carousel moving. Thus, after the $$$n$$$-th figure the figure with the number $$$1$$$ follows. Each figure has its own type — the type of the animal corresponding to this figure (the horse, the tiger and so on). The type of animal of the $$$i$$$-th figure equals $$$t_i$$$. The example of the carousel for $$$n=9$$$ and $$$t=[5, 5, 1, 15, 1, 5, 5, 1, 1]$$$. You want to color each figure in one of the colors. You think that it's boring if the carousel contains two different figures (with the distinct types of animals) going one right after another and colored in the same color.Your task is to color the figures in such a way that the number of distinct colors used is the minimum possible and there are no figures of the different types going one right after another and colored in the same color. If you use exactly $$$k$$$ distinct colors, then the colors of figures should be denoted with integers from $$$1$$$ to $$$k$$$.", "input_spec": "The input contains one or more test cases. The first line contains one integer $$$q$$$ ($$$1 \\le q \\le 10^4$$$) — the number of test cases in the test. Then $$$q$$$ test cases follow. One test case is given on two lines. The first line of the test case contains one integer $$$n$$$ ($$$3 \\le n \\le 2 \\cdot 10^5$$$) — the number of figures in the carousel. Figures are numbered from $$$1$$$ to $$$n$$$ in order of carousel moving. Assume that after the $$$n$$$-th figure the figure $$$1$$$ goes. The second line of the test case contains $$$n$$$ integers $$$t_1, t_2, \\dots, t_n$$$ ($$$1 \\le t_i \\le 2 \\cdot 10^5$$$), where $$$t_i$$$ is the type of the animal of the $$$i$$$-th figure. The sum of $$$n$$$ over all test cases does not exceed $$$2\\cdot10^5$$$.", "output_spec": "Print $$$q$$$ answers, for each test case print two lines. In the first line print one integer $$$k$$$ — the minimum possible number of distinct colors of figures. In the second line print $$$n$$$ integers $$$c_1, c_2, \\dots, c_n$$$ ($$$1 \\le c_i \\le k$$$), where $$$c_i$$$ is the color of the $$$i$$$-th figure. If there are several answers, you can print any.", "sample_inputs": ["4\n5\n1 2 1 2 2\n6\n1 2 2 1 2 2\n5\n1 2 1 2 3\n3\n10 10 10"], "sample_outputs": ["2\n1 2 1 2 2\n2\n2 1 2 1 2 1\n3\n2 3 2 3 1\n1\n1 1 1"], "notes": null}, "positive_code": [{"source_code": "gets.to_i.times do\n\tn = gets.to_i\n\ta = gets.split.map(&:to_i)\n\tans = Array.new(n, 0)\n\tif a.count(a[0]) == n\n\t\tans = Array.new(n, 1)\n\telsif a[0] == a[n - 1] || n % 2 == 0\n\t\tans = (0...n).map{|i| i % 2 + 1}\n\telse\n\t\tans = (0...n).map{|i| i % 2 + 1}\n\t\tpos = (0...(n - 1)).to_a.index{|i| a[i] == a[i + 1]}\n\t\tif pos\n\t\t\t((pos + 1)...n).each{|i| ans[i] = (ans[i] == 1 ? 2 : 1)}\n\t\telse\n\t\t\tans[n - 1] = 3\n\t\tend\n\tend\n\t\n\tputs ans.uniq.size\n\tputs ans.join(' ')\nend"}, {"source_code": "t = gets.to_i\n(1..t).each do\n n = gets.to_i\n a = gets.split(' ')\n mp = Hash.new(0)\n a.each do |i|\n mp[i]+=1\n end\n if mp.length == 1\n puts 1\n (1..n).each do\n print \"1 \"\n end\n puts \"\"\n elsif n.modulo(2)==0\n puts \"2\"\n (1..n).each do |i|\n print \"#{i.modulo(2)+1} \"\n end\n puts \"\"\n elsif a[0]==a[n-1]\n puts \"2\"\n (1..n).each do |i|\n print \"#{i.modulo(2)+1} \"\n end\n puts \"\"\n else\n x = -1\n (1..n-1).each do |i|\n if(a[i]==a[i-1])\n x = i\n break\n end\n end\n if x!=-1\n puts \"2\"\n (0..n-1).each do |i|\n if i 1\n\t\t\tmid = (r + l) / 2\n\t\t\tif b[mid] >= i\n\t\t\t\tr = mid\n\t\t\telse\n\t\t\t\tl = mid\n\t\t\tend\n\t\tend\n\t\tr + 1\n\tend\n\tputs ans.uniq.size\n\tputs ans.join(' ')\nend"}], "src_uid": "1f4c3f5e7205fe556b50320cecf66c89"} {"nl": {"description": "There is a classroom with two rows of computers. There are $$$n$$$ computers in each row and each computer has its own grade. Computers in the first row has grades $$$a_1, a_2, \\dots, a_n$$$ and in the second row — $$$b_1, b_2, \\dots, b_n$$$.Initially, all pairs of neighboring computers in each row are connected by wire (pairs $$$(i, i + 1)$$$ for all $$$1 \\le i < n$$$), so two rows form two independent computer networks.Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the $$$i$$$-th computer from the first row and the $$$j$$$-th computer from the second row costs $$$|a_i - b_j|$$$.You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts.That is the minimum total cost to make a fault-tolerant network?", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$3 \\le n \\le 2 \\cdot 10^5$$$) — the number of computers in each row. The second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$) — the grades of computers in the first row. The third line contains $$$n$$$ integers $$$b_1, b_2, \\dots, b_n$$$ ($$$1 \\le b_i \\le 10^9$$$) — the grades of computers in the second row. It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, print a single integer — the minimum total cost to make a fault-tolerant network.", "sample_inputs": ["2\n\n3\n\n1 10 1\n\n20 4 25\n\n4\n\n1 1 1 1\n\n1000000000 1000000000 1000000000 1000000000"], "sample_outputs": ["31\n1999999998"], "notes": "NoteIn the first test case, it's optimal to connect four pairs of computers: computer $$$1$$$ from the first row with computer $$$2$$$ from the second row: cost $$$|1 - 4| = 3$$$; computer $$$3$$$ from the first row with computer $$$2$$$ from the second row: cost $$$|1 - 4| = 3$$$; computer $$$2$$$ from the first row with computer $$$1$$$ from the second row: cost $$$|10 - 20| = 10$$$; computer $$$2$$$ from the first row with computer $$$3$$$ from the second row: cost $$$|10 - 25| = 15$$$; In total, $$$3 + 3 + 10 + 15 = 31$$$.In the second test case, it's optimal to connect $$$1$$$ from the first row with $$$1$$$ from the second row, and $$$4$$$ from the first row with $$$4$$$ from the second row."}, "positive_code": [{"source_code": "main = -> {\n # nearest end-end or farthest end-end or end-other\n\n t = int\n t.times do\n n = int\n a = ints\n b = ints\n\n end1a = a[0]\n end2a = a[-1]\n end1b = b[0]\n end2b = b[-1]\n\n ends = [[a[0], b[0]], [a[-1], b[-1]]]\n\n # farthest end-end\n ans = [\n (end1a - end2b).abs + (end2a - end1b).abs,\n (end1a - end1b).abs + (end2a - end2b).abs\n ].min\n\n ans1b = a.map { (_1 - end1b).abs }.min\n ans1a = b.map { (_1 - end1a).abs }.min\n \n # nearest end-end\n ans1 = [\n (end1a - end1b).abs,\n ans1b + ans1a\n ].min\n\n ans2b = a.map { (_1 - end2b).abs }.min\n ans2a = b.map { (_1 - end2a).abs }.min\n\n # nearest end-end\n ans2 = [\n (end2a - end2b).abs,\n ans2b + ans2a\n ].min\n\n\n # farthest end-end\n ans3 = [\n (end1a - end2b).abs,\n ans1a + ans2b\n ].min\n ans4 = [\n (end2a - end1b).abs,\n ans2a + ans1b\n ].min\n\n ans = [ans, ans1 + ans2, ans3 + ans4].min\n\n # p [ans, ans1, ans2, ans1a, ans1b, ans2a, ans2b]\n # p [low1a, up1a, low1b, up1b]\n # p [sorted_a, sorted_b]\n\n puts ans\n end\n}\n\nDEBUG = true\nMOD = 10**9+7\n$yesno = %w(No Yes)\nYesNo = %w(No Yes)\nYESNO = %w(NO YES)\nINF = 10**18\n\nrequire \"set\"\nrequire \"prime\"\n\ndef int; gets.to_s.to_i end\ndef ints; gets.to_s.split.map { |s| s.to_i } end\ndef int1s; gets.to_s.split.map { |s| s.to_i - 1 } end\ndef float; gets.to_s.to_f end\ndef floats; gets.to_s.split.map { |s| s.to_f } end\ndef array_of(&convert); gets.to_s.split.map(&convert) end\ndef string; gets.to_s.chomp end\ndef strings; gets.to_s.split end\ndef rep(n, &b); Array.new(n, &b) end\ndef yes; puts $yesno[1] end\ndef no; puts $yesno[0] end\ndef yesno t; puts $yesno[t] end\ndef YesNo t; puts YesNo[t] end\ndef YESNO t; puts YESNO[t] end\ndef zip(xs, *yss); Enumerator.new { |y| xs.zip(*yss) { |a| y.yield(*a) } } end\ndef max(*xs, &block); block_given? ? xs.max_by(&block) : xs.max end\ndef min(*xs, &block); block_given? ? xs.min_by(&block) : xs.min end\ndef minmax(*xs, &block); block_given? ? xs.minmax_by(&block) : xs.minmax end\ndef gcd(*xs); xs.inject(0, :gcd) end\ndef matrix(h, w, fill=nil, &block); return Array.new(h) { Array.new(w, &block) } if block_given?; Array.new(h) { [fill] * w } end\ndef debug(x = nil); STDERR.puts (block_given? ? yield(x) : x).inspect if DEBUG; x end\ndef debug_grid(grid, width = 1); grid.each { |row| STDERR.puts row.map { |x| x.inspect.ljust(width) }.join(\"\") } if DEBUG; grid end\ndef if_debug; yield if DEBUG end\n\nmodule Boolean\n def coerce(other); [other, to_i] end\n def +@; to_i end\n def to_int; to_i end\n def *(other); to_i * other end\nend\n\nclass TrueClass\n include Boolean\n def to_i; 1 end\nend\n\nclass FalseClass\n include Boolean\n def to_i; 0 end\nend\n\nclass Integer\n def div_ceil(y); (self + y - 1) / y end\n def mod_inv(mod = MOD); pow(mod-2, mod) end\n def mod_div(y, mod = MOD); self * y.mod_inv(mod) % mod end\n def mod_nCr(r, mod = MOD); x = y = 1; (1..r).each { |i| x = x * (self + 1 - i) % mod; y = y * i % mod }; x.mod_div(y, mod); end\n def factorial(mod = MOD); (2..self).inject(1) { |f, x| f * x % mod } end\n def popcount; x = self; c = 0; while x > 0; c += 1 if x & 1 == 1; x >>= 1 end; c end #TODO: faster\n def bitbrute(&block); (1< 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\n def lower_bound; ac, wa = self.end_open, self.begin; while ac - wa > 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\n def shakutori(r2, &pred); Enumerator.new { |y| j, r = r2.begin, r2.end_open; each { |i| j += 1 while j + 1 < r and pred[i, j+1]; y.yield(i, j) } }; end\n def widest(&block); Enumerator.new { |y| j, n = self.begin, self.end_open; each { |i| j += 1 while j < n and block[i, j]; y.yield(i, j) if block[i, j] } } end\nend\n\nclass Array\n def power(&block); (0 ... 1 << size).each(&block) end\n def sorted_merge(other); a = []; i = j = 0; n, m = size, other.size; if j < m and other[j] < self[i]; a << other[j]; j += 1 else; a << self[i]; i += 1 end while i < n; a.push(*other[j..-1]) if j < m; a end\n def upper_bound; ac, wa = 0, size; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\n def lower_bound; ac, wa = size, 0; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\n def cum(*xs, &op); a = []; a << xs[0] if xs.size > 0; a << x = self[0]; (1...size).each { |i| a << x = op[x, self[i]] }; a end\n def cumdiff(range); self[range.end_open] - self[range.begin]; end\n def compress(kinds = uniq.sort!); map { |x| kinds.bsearch_index { _1 >= x } }; end\nend\n\nmodule Enumerable\n def sorted_uniq; x = nil; filter { |y| c = x === y; x = y; !c } end\n def cumsum; ys = [0]; each { |x| ys << x + ys[-1] }; ys end\nend\n\nmain[]"}], "negative_code": [{"source_code": "main = -> {\n # nearest end-end or farthest end-end or end-other\n\n t = int\n t.times do\n n = int\n a = ints\n b = ints\n\n end1a = a[0]\n end2a = a[-1]\n end1b = b[0]\n end2b = b[-1]\n\n ends = [[a[0], b[0]], [a[-1], b[-1]]]\n\n # farthest end-end\n ans = [\n (end1a - end2b).abs + (end2a - end1b).abs,\n (end1a - end1b).abs + (end2a - end2b).abs\n ].min\n\n ans1 = INF\n ans2 = INF\n \n # nearest end-end\n ans1 = [\n (end1a - end1b).abs,\n a.map { (_1 - end1b).abs }.min + b.map { (_1 - end1a).abs }.min\n ].min\n\n # nearest end-end\n ans2 = [\n (end2a - end2b).abs,\n a.map { (_1 - end2b).abs }.min + b.map { (_1 - end2a).abs }.min\n ].min\n\n ans = [ans, ans1 + ans2].min\n\n # p [ans, ans1, ans2, ans1a, ans1b, ans2a, ans2b]\n # p [low1a, up1a, low1b, up1b]\n # p [sorted_a, sorted_b]\n\n puts ans\n end\n}\n\nDEBUG = true\nMOD = 10**9+7\n$yesno = %w(No Yes)\nYesNo = %w(No Yes)\nYESNO = %w(NO YES)\nINF = 10**18\n\nrequire \"set\"\nrequire \"prime\"\n\ndef int; gets.to_s.to_i end\ndef ints; gets.to_s.split.map { |s| s.to_i } end\ndef int1s; gets.to_s.split.map { |s| s.to_i - 1 } end\ndef float; gets.to_s.to_f end\ndef floats; gets.to_s.split.map { |s| s.to_f } end\ndef array_of(&convert); gets.to_s.split.map(&convert) end\ndef string; gets.to_s.chomp end\ndef strings; gets.to_s.split end\ndef rep(n, &b); Array.new(n, &b) end\ndef yes; puts $yesno[1] end\ndef no; puts $yesno[0] end\ndef yesno t; puts $yesno[t] end\ndef YesNo t; puts YesNo[t] end\ndef YESNO t; puts YESNO[t] end\ndef zip(xs, *yss); Enumerator.new { |y| xs.zip(*yss) { |a| y.yield(*a) } } end\ndef max(*xs, &block); block_given? ? xs.max_by(&block) : xs.max end\ndef min(*xs, &block); block_given? ? xs.min_by(&block) : xs.min end\ndef minmax(*xs, &block); block_given? ? xs.minmax_by(&block) : xs.minmax end\ndef gcd(*xs); xs.inject(0, :gcd) end\ndef matrix(h, w, fill=nil, &block); return Array.new(h) { Array.new(w, &block) } if block_given?; Array.new(h) { [fill] * w } end\ndef debug(x = nil); STDERR.puts (block_given? ? yield(x) : x).inspect if DEBUG; x end\ndef debug_grid(grid, width = 1); grid.each { |row| STDERR.puts row.map { |x| x.inspect.ljust(width) }.join(\"\") } if DEBUG; grid end\ndef if_debug; yield if DEBUG end\n\nmodule Boolean\n def coerce(other); [other, to_i] end\n def +@; to_i end\n def to_int; to_i end\n def *(other); to_i * other end\nend\n\nclass TrueClass\n include Boolean\n def to_i; 1 end\nend\n\nclass FalseClass\n include Boolean\n def to_i; 0 end\nend\n\nclass Integer\n def div_ceil(y); (self + y - 1) / y end\n def mod_inv(mod = MOD); pow(mod-2, mod) end\n def mod_div(y, mod = MOD); self * y.mod_inv(mod) % mod end\n def mod_nCr(r, mod = MOD); x = y = 1; (1..r).each { |i| x = x * (self + 1 - i) % mod; y = y * i % mod }; x.mod_div(y, mod); end\n def factorial(mod = MOD); (2..self).inject(1) { |f, x| f * x % mod } end\n def popcount; x = self; c = 0; while x > 0; c += 1 if x & 1 == 1; x >>= 1 end; c end #TODO: faster\n def bitbrute(&block); (1< 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\n def lower_bound; ac, wa = self.end_open, self.begin; while ac - wa > 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\n def shakutori(r2, &pred); Enumerator.new { |y| j, r = r2.begin, r2.end_open; each { |i| j += 1 while j + 1 < r and pred[i, j+1]; y.yield(i, j) } }; end\n def widest(&block); Enumerator.new { |y| j, n = self.begin, self.end_open; each { |i| j += 1 while j < n and block[i, j]; y.yield(i, j) if block[i, j] } } end\nend\n\nclass Array\n def power(&block); (0 ... 1 << size).each(&block) end\n def sorted_merge(other); a = []; i = j = 0; n, m = size, other.size; if j < m and other[j] < self[i]; a << other[j]; j += 1 else; a << self[i]; i += 1 end while i < n; a.push(*other[j..-1]) if j < m; a end\n def upper_bound; ac, wa = 0, size; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\n def lower_bound; ac, wa = size, 0; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\n def cum(*xs, &op); a = []; a << xs[0] if xs.size > 0; a << x = self[0]; (1...size).each { |i| a << x = op[x, self[i]] }; a end\n def cumdiff(range); self[range.end_open] - self[range.begin]; end\n def compress(kinds = uniq.sort!); map { |x| kinds.bsearch_index { _1 >= x } }; end\nend\n\nmodule Enumerable\n def sorted_uniq; x = nil; filter { |y| c = x === y; x = y; !c } end\n def cumsum; ys = [0]; each { |x| ys << x + ys[-1] }; ys end\nend\n\nmain[]"}, {"source_code": "main = -> {\n # nearest end-end or farthest end-end or end-other\n\n t = int\n t.times do\n n = int\n a = ints\n b = ints\n\n end1a = a[0]\n end2a = a[-1]\n end1b = b[0]\n end2b = b[-1]\n\n ends = [[a[0], b[0]], [a[-1], b[-1]]]\n\n # farthest end-end\n ans = (end1a - end2b).abs + (end2a - end1b).abs\n\n ans1 = INF\n ans2 = INF\n \n # nearest end-end\n ans1 = [\n (end1a - end1b).abs,\n a.map { (_1 - end1b).abs }.min + b.map { (_1 - end1a).abs }.min\n ].min\n\n # nearest end-end\n\n ans2 = [\n (end2a - end2b).abs,\n a.map { (_1 - end2b).abs }.min + b.map { (_1 - end2a).abs }.min\n ].min\n\n ans = [ans, ans1 + ans2].min\n\n # p [ans, ans1, ans2, ans1a, ans1b, ans2a, ans2b]\n # p [low1a, up1a, low1b, up1b]\n # p [sorted_a, sorted_b]\n\n puts ans\n end\n}\n\nDEBUG = true\nMOD = 10**9+7\n$yesno = %w(No Yes)\nYesNo = %w(No Yes)\nYESNO = %w(NO YES)\nINF = 10**18\n\nrequire \"set\"\nrequire \"prime\"\n\ndef int; gets.to_s.to_i end\ndef ints; gets.to_s.split.map { |s| s.to_i } end\ndef int1s; gets.to_s.split.map { |s| s.to_i - 1 } end\ndef float; gets.to_s.to_f end\ndef floats; gets.to_s.split.map { |s| s.to_f } end\ndef array_of(&convert); gets.to_s.split.map(&convert) end\ndef string; gets.to_s.chomp end\ndef strings; gets.to_s.split end\ndef rep(n, &b); Array.new(n, &b) end\ndef yes; puts $yesno[1] end\ndef no; puts $yesno[0] end\ndef yesno t; puts $yesno[t] end\ndef YesNo t; puts YesNo[t] end\ndef YESNO t; puts YESNO[t] end\ndef zip(xs, *yss); Enumerator.new { |y| xs.zip(*yss) { |a| y.yield(*a) } } end\ndef max(*xs, &block); block_given? ? xs.max_by(&block) : xs.max end\ndef min(*xs, &block); block_given? ? xs.min_by(&block) : xs.min end\ndef minmax(*xs, &block); block_given? ? xs.minmax_by(&block) : xs.minmax end\ndef gcd(*xs); xs.inject(0, :gcd) end\ndef matrix(h, w, fill=nil, &block); return Array.new(h) { Array.new(w, &block) } if block_given?; Array.new(h) { [fill] * w } end\ndef debug(x = nil); STDERR.puts (block_given? ? yield(x) : x).inspect if DEBUG; x end\ndef debug_grid(grid, width = 1); grid.each { |row| STDERR.puts row.map { |x| x.inspect.ljust(width) }.join(\"\") } if DEBUG; grid end\ndef if_debug; yield if DEBUG end\n\nmodule Boolean\n def coerce(other); [other, to_i] end\n def +@; to_i end\n def to_int; to_i end\n def *(other); to_i * other end\nend\n\nclass TrueClass\n include Boolean\n def to_i; 1 end\nend\n\nclass FalseClass\n include Boolean\n def to_i; 0 end\nend\n\nclass Integer\n def div_ceil(y); (self + y - 1) / y end\n def mod_inv(mod = MOD); pow(mod-2, mod) end\n def mod_div(y, mod = MOD); self * y.mod_inv(mod) % mod end\n def mod_nCr(r, mod = MOD); x = y = 1; (1..r).each { |i| x = x * (self + 1 - i) % mod; y = y * i % mod }; x.mod_div(y, mod); end\n def factorial(mod = MOD); (2..self).inject(1) { |f, x| f * x % mod } end\n def popcount; x = self; c = 0; while x > 0; c += 1 if x & 1 == 1; x >>= 1 end; c end #TODO: faster\n def bitbrute(&block); (1< 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\n def lower_bound; ac, wa = self.end_open, self.begin; while ac - wa > 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\n def shakutori(r2, &pred); Enumerator.new { |y| j, r = r2.begin, r2.end_open; each { |i| j += 1 while j + 1 < r and pred[i, j+1]; y.yield(i, j) } }; end\n def widest(&block); Enumerator.new { |y| j, n = self.begin, self.end_open; each { |i| j += 1 while j < n and block[i, j]; y.yield(i, j) if block[i, j] } } end\nend\n\nclass Array\n def power(&block); (0 ... 1 << size).each(&block) end\n def sorted_merge(other); a = []; i = j = 0; n, m = size, other.size; if j < m and other[j] < self[i]; a << other[j]; j += 1 else; a << self[i]; i += 1 end while i < n; a.push(*other[j..-1]) if j < m; a end\n def upper_bound; ac, wa = 0, size; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\n def lower_bound; ac, wa = size, 0; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\n def cum(*xs, &op); a = []; a << xs[0] if xs.size > 0; a << x = self[0]; (1...size).each { |i| a << x = op[x, self[i]] }; a end\n def cumdiff(range); self[range.end_open] - self[range.begin]; end\n def compress(kinds = uniq.sort!); map { |x| kinds.bsearch_index { _1 >= x } }; end\nend\n\nmodule Enumerable\n def sorted_uniq; x = nil; filter { |y| c = x === y; x = y; !c } end\n def cumsum; ys = [0]; each { |x| ys << x + ys[-1] }; ys end\nend\n\nmain[]"}, {"source_code": "main = -> {\n # nearest end-end or farthest end-end or end-other\n\n t = int\n t.times do\n n = int\n a = ints\n b = ints\n\n end1a = a[0]\n end2a = a[-1]\n end1b = b[0]\n end2b = b[-1]\n\n ends = [[a[0], b[0]], [a[-1], b[-1]]]\n\n sorted_a = a.sort\n sorted_b = b.sort\n\n # farthest end-end\n ans = (end1a - end2b).abs + (end2a - end1b).abs\n\n ans1 = INF\n ans2 = INF\n \n # nearest end-end\n ans1 = (end1a - end1b).abs\n\n # other-a\n low1b = sorted_b.bsearch_index { |x| x >= end1a } || 0\n up1b = sorted_b.bsearch_index { |x| x > end1a } || n\n ans1a = INF\n ans1a = (sorted_b[low1b] - end1a).abs if low1b\n ans1a = [ans1a, (sorted_b[up1b - 1] - end1a).abs].min if up1b\n # other-b\n low1a = sorted_a.bsearch_index { |x| x >= end1b } || 0\n up1a = sorted_a.bsearch_index { |x| x > end1b } || n\n ans1b = INF\n ans1b = (sorted_a[low1a] - end1b).abs if low1a\n ans1b = [ans1b, (sorted_a[up1a - 1] - end1b).abs].min if up1a\n\n ans1 = [ans1, ans1a + ans1b].min\n\n # nearest end-end\n ans2 = (end2a - end2b).abs\n\n # other-a\n low2b = sorted_b.bsearch_index { |x| x >= end2a } || 0\n up2b = sorted_b.bsearch_index { |x| x > end2a } || n\n ans2a = INF\n ans2a = (sorted_b[low2b] - end2a).abs if low2b\n ans2a = [ans2a, (sorted_b[up2b - 1] - end2a).abs].min if up2b and up2b > 0\n # other-b\n low2a = sorted_a.bsearch_index { |x| x >= end2b } || 0\n up2a = sorted_a.bsearch_index { |x| x > end2b } || n\n ans2b = INF\n ans2b = (sorted_a[low2a] - end2b).abs if low2a\n ans2b = [ans2b, (sorted_a[up2a - 1] - end2b).abs].min if up2a and up2a > 0\n\n ans2 = [ans2, ans2a + ans2b].min\n\n ans = [ans, ans1 + ans2].min\n\n # p [ans, ans1, ans2, ans1a, ans1b, ans2a, ans2b]\n # p [low1a, up1a, low1b, up1b]\n # p [sorted_a, sorted_b]\n\n puts ans\n end\n}\n\nDEBUG = true\nMOD = 10**9+7\n$yesno = %w(No Yes)\nYesNo = %w(No Yes)\nYESNO = %w(NO YES)\nINF = 10**18\n\nrequire \"set\"\nrequire \"prime\"\n\ndef int; gets.to_s.to_i end\ndef ints; gets.to_s.split.map { |s| s.to_i } end\ndef int1s; gets.to_s.split.map { |s| s.to_i - 1 } end\ndef float; gets.to_s.to_f end\ndef floats; gets.to_s.split.map { |s| s.to_f } end\ndef array_of(&convert); gets.to_s.split.map(&convert) end\ndef string; gets.to_s.chomp end\ndef strings; gets.to_s.split end\ndef rep(n, &b); Array.new(n, &b) end\ndef yes; puts $yesno[1] end\ndef no; puts $yesno[0] end\ndef yesno t; puts $yesno[t] end\ndef YesNo t; puts YesNo[t] end\ndef YESNO t; puts YESNO[t] end\ndef zip(xs, *yss); Enumerator.new { |y| xs.zip(*yss) { |a| y.yield(*a) } } end\ndef max(*xs, &block); block_given? ? xs.max_by(&block) : xs.max end\ndef min(*xs, &block); block_given? ? xs.min_by(&block) : xs.min end\ndef minmax(*xs, &block); block_given? ? xs.minmax_by(&block) : xs.minmax end\ndef gcd(*xs); xs.inject(0, :gcd) end\ndef matrix(h, w, fill=nil, &block); return Array.new(h) { Array.new(w, &block) } if block_given?; Array.new(h) { [fill] * w } end\ndef debug(x = nil); STDERR.puts (block_given? ? yield(x) : x).inspect if DEBUG; x end\ndef debug_grid(grid, width = 1); grid.each { |row| STDERR.puts row.map { |x| x.inspect.ljust(width) }.join(\"\") } if DEBUG; grid end\ndef if_debug; yield if DEBUG end\n\nmodule Boolean\n def coerce(other); [other, to_i] end\n def +@; to_i end\n def to_int; to_i end\n def *(other); to_i * other end\nend\n\nclass TrueClass\n include Boolean\n def to_i; 1 end\nend\n\nclass FalseClass\n include Boolean\n def to_i; 0 end\nend\n\nclass Integer\n def div_ceil(y); (self + y - 1) / y end\n def mod_inv(mod = MOD); pow(mod-2, mod) end\n def mod_div(y, mod = MOD); self * y.mod_inv(mod) % mod end\n def mod_nCr(r, mod = MOD); x = y = 1; (1..r).each { |i| x = x * (self + 1 - i) % mod; y = y * i % mod }; x.mod_div(y, mod); end\n def factorial(mod = MOD); (2..self).inject(1) { |f, x| f * x % mod } end\n def popcount; x = self; c = 0; while x > 0; c += 1 if x & 1 == 1; x >>= 1 end; c end #TODO: faster\n def bitbrute(&block); (1< 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\n def lower_bound; ac, wa = self.end_open, self.begin; while ac - wa > 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\n def shakutori(r2, &pred); Enumerator.new { |y| j, r = r2.begin, r2.end_open; each { |i| j += 1 while j + 1 < r and pred[i, j+1]; y.yield(i, j) } }; end\n def widest(&block); Enumerator.new { |y| j, n = self.begin, self.end_open; each { |i| j += 1 while j < n and block[i, j]; y.yield(i, j) if block[i, j] } } end\nend\n\nclass Array\n def power(&block); (0 ... 1 << size).each(&block) end\n def sorted_merge(other); a = []; i = j = 0; n, m = size, other.size; if j < m and other[j] < self[i]; a << other[j]; j += 1 else; a << self[i]; i += 1 end while i < n; a.push(*other[j..-1]) if j < m; a end\n def upper_bound; ac, wa = 0, size; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\n def lower_bound; ac, wa = size, 0; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\n def cum(*xs, &op); a = []; a << xs[0] if xs.size > 0; a << x = self[0]; (1...size).each { |i| a << x = op[x, self[i]] }; a end\n def cumdiff(range); self[range.end_open] - self[range.begin]; end\n def compress(kinds = uniq.sort!); map { |x| kinds.bsearch_index { _1 >= x } }; end\nend\n\nmodule Enumerable\n def sorted_uniq; x = nil; filter { |y| c = x === y; x = y; !c } end\n def cumsum; ys = [0]; each { |x| ys << x + ys[-1] }; ys end\nend\n\nmain[]"}], "src_uid": "71e6ceb75852f4cd437bbf0478e37dc4"} {"nl": {"description": "Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $$$2 \\cdot n$$$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $$$n$$$ people in each row). Students are numbered from $$$1$$$ to $$$n$$$ in each row in order from left to right. Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $$$2n$$$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose.", "input_spec": "The first line of the input contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$) — the number of students in each row. The second line of the input contains $$$n$$$ integers $$$h_{1, 1}, h_{1, 2}, \\ldots, h_{1, n}$$$ ($$$1 \\le h_{1, i} \\le 10^9$$$), where $$$h_{1, i}$$$ is the height of the $$$i$$$-th student in the first row. The third line of the input contains $$$n$$$ integers $$$h_{2, 1}, h_{2, 2}, \\ldots, h_{2, n}$$$ ($$$1 \\le h_{2, i} \\le 10^9$$$), where $$$h_{2, i}$$$ is the height of the $$$i$$$-th student in the second row.", "output_spec": "Print a single integer — the maximum possible total height of players in a team Demid can choose.", "sample_inputs": ["5\n9 3 5 7 3\n5 8 1 4 5", "3\n1 2 9\n10 1 1", "1\n7\n4"], "sample_outputs": ["29", "19", "7"], "notes": "NoteIn the first example Demid can choose the following team as follows: In the second example Demid can choose the following team as follows: "}, "positive_code": [{"source_code": "n = gets.to_i\nh0 = gets.split.map(&:to_i)\nh1 = gets.split.map(&:to_i)\ndp = Array.new(n+1){[0,0]}\ndp[1][0] = h0[0]\ndp[1][1] = h1[0]\n(2..n).each do |i|\n dp[i][0] = [dp[i-1][1],dp[i-2][1]].max + h0[i-1]\n dp[i][1] = [dp[i-1][0],dp[i-2][0]].max + h1[i-1]\nend\np dp[n].max"}, {"source_code": "def solve(io)\n n = io.gets.to_i\n arr = 2.times.map{ io.gets.split(\" \").map{|s| s.to_i} }\n max = n.times.inject([0, 0, 0]) do |prev, i|\n [[prev[1], prev[2]].max, [prev[0], prev[2]].max + arr[0][i], [prev[0], prev[1]].max + arr[1][i]]\n end.max\n puts max\nend\n\nsolve(STDIN)"}, {"source_code": "def check(num, team, row, column, opp_row)\n max = team[ row ][ column ]\n if column + 1 < num\n max += team[ opp_row ][ column + 1 ]\n end\n if column + 2 < num\n sum2 = team[ row ][ column ] + team[ opp_row ][ column + 2 ]\n if(max < sum2)\n max = sum2;\n end\n end\n team[ row ][ column ] = max\nend\ndef start(num, team)\n column = num - 1\n while column >= 0 do\n check(num, team, 0, column, 1)\n check(num, team, 1, column, 0)\n column -= 1;\n end\n if(team[ 0 ][ 0 ] > team[ 1 ][ 0 ])\n return team[ 0 ][ 0 ]\n end\n return team[ 1 ][ 0 ]\nend\nteam = []\nnum = gets.chomp.to_i\nteam.push(gets.chomp.split(' ').collect{|var| var.to_i})\nteam.push(gets.chomp.split(' ').collect{|var| var.to_i})\nputs start(num,team.clone)"}], "negative_code": [{"source_code": "def recur(num,team,row,column)\n sum1 = 0\n if(column == num)\n return 0\n else\n if row == 1\n sum_1 = team[row][column] + recur(num,team,0,column + 1)\n sum_2 = recur(num,team,row,column + 1)\n if(sum_1 > sum_2)\n sum1 = sum_1\n else\n sum1 = sum_2\n end\n else\n sum_1 = team[row][column] + recur(num,team,1,column + 1)\n sum_2 = recur(num,team,row,column + 1)\n if(sum_1 > sum_2)\n sum1 = sum_1\n else\n sum1 = sum_2\n end\n end\n return sum1\n end\nend\ndef start(num, team)\n sum1 = team[1][0] + recur(num,team,0,1)\n sum2 = team[0][0] + recur(num,team,1,1)\n if(sum1 > sum2)\n return sum1\n end\n return sum2\nend\nteam = []\nnum = gets.chomp.to_i\nteam.push(gets.chomp.split(' ').collect{|var| var.to_i})\nteam.push(gets.chomp.split(' ').collect{|var| var.to_i})"}, {"source_code": "def recur(num,team,row,column)\n sum1 = 0\n if(column == num)\n return 0\n else\n if row == 1\n sum_1 = team[row][column] + recur(num,team,0,column + 1)\n sum_2 = recur(num,team,row,column)\n if(sum_1 > sum_2)\n sum1 = sum_1\n else\n sum1 = sum_2\n end\n else\n sum_1 = team[row][column] + recur(num,team,1,column + 1)\n sum_2 = recur(num,team,row,column)\n if(sum_1 > sum_2)\n sum1 = sum_1\n else\n sum1 = sum_2\n end\n end\n return sum1\n end\nend\ndef start(num, team)\n sum1 = team[1][0] + recur(num,team,0,1)\n sum2 = team[0][0] + recur(num,team,1,1)\n if(sum1 > sum2)\n return sum1\n end\n return sum2\nend\nteam = []\nnum = gets.chomp.to_i\nteam.push(gets.chomp.split(' ').collect{|var| var.to_i})\nteam.push(gets.chomp.split(' ').collect{|var| var.to_i})"}], "src_uid": "667e8938b964d7a24500003f6b89717b"} {"nl": {"description": "One beautiful day Vasily the bear painted 2m circles of the same radius R on a coordinate plane. Circles with numbers from 1 to m had centers at points (2R - R, 0), (4R - R, 0), ..., (2Rm - R, 0), respectively. Circles with numbers from m + 1 to 2m had centers at points (2R - R, 2R), (4R - R, 2R), ..., (2Rm - R, 2R), respectively. Naturally, the bear painted the circles for a simple experiment with a fly. The experiment continued for m2 days. Each day of the experiment got its own unique number from 0 to m2 - 1, inclusive. On the day number i the following things happened: The fly arrived at the coordinate plane at the center of the circle with number ( is the result of dividing number x by number y, rounded down to an integer). The fly went along the coordinate plane to the center of the circle number ( is the remainder after dividing number x by number y). The bear noticed that the fly went from the center of circle v to the center of circle u along the shortest path with all points lying on the border or inside at least one of the 2m circles. After the fly reached the center of circle u, it flew away in an unknown direction. Help Vasily, count the average distance the fly went along the coordinate plane during each of these m2 days.", "input_spec": "The first line contains two integers m, R (1 ≤ m ≤ 105, 1 ≤ R ≤ 10).", "output_spec": "In a single line print a single real number — the answer to the problem. The answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6.", "sample_inputs": ["1 1", "2 2"], "sample_outputs": ["2.0000000000", "5.4142135624"], "notes": "NoteFigure to the second sample"}, "positive_code": [{"source_code": "def g(r, n)\n d = (2 * r * r) ** 0.5\n \n return 0 if n == 0\n return 2 * r + d if n == 1\n \n 2 * r + d + (2 * d + r * n) * (n - 1)\nend\n\nm, r = gets.chomp.split(' ').map { |x| x.to_i }\n\ntotal = 0.0\n\nfor n in 1..m\n total += g(r, n - 1) + g(r, m - n) + 2 * r\nend\n\nputs total / m ** 2"}, {"source_code": "m,r=gets.split.map(&:to_i)\nx=2**0.5*r\ns=m-1\nfor i in 2..m-1 do s+=(i-1)*(m-i) end\ns=2*s+m\n\nprint 2*(r*s+x*(m-1)**2)/m/m\n"}], "negative_code": [{"source_code": "#def f(n, r)\n# return 2 * r if n == 0\n# return 2 * n * r + (2 * r ** 2) ** 0.5\n#end\n\ndef g(n, r)\n return 0 if n == 0\n (r * (n + 1) + (2 * r ** 2) ** 0.5) * n\nend\n\nm, r = gets.chomp.split(' ').map { |x| x.to_i }\ntotal_sum = 0\nfor n in 1..m\n total_sum += g(n - 1, r) + g(m - n, r) + 2 * r\nend\n\nputs total_sum * 1.0 / m ** 2\n=begin\nif f(0, 1) != 2\n puts \"Error\"\n exit\nend\n\nif f(2, 1) != 4 * 1 + 2 ** 0.5 \n puts \"Error\"\n exit\nend\n\nif g(2, 1) != 4 * 1 + 2 ** 0.5 \n puts \"Error\"\n exit\nend\n=end\n"}, {"source_code": "def g(n, r)\n return 0 if n == 0\n (r * (n + 1) + (2 * r ** 2) ** 0.5) * n\nend\n\nm, r = gets.chomp.split(' ').map { |x| x.to_i }\ntotal_sum = 0.0\nfor n in 1..m\n total_sum += g(n - 1, r) + g(m - n, r) + 2 * r\nend\n\nputs total_sum / m ** 2\n"}, {"source_code": "m,r=gets.split.map(&:to_i)\nx=2**0.5*r\ns=0\nm.times{|i| s+=i*(m-i)}\ns=2*s+m if m > 1\np ((2*r*s+x*m*(m-1))/m/m).round(9)\n"}, {"source_code": "m,r=gets.split.map(&:to_i)\nx=2**0.5*r\ns=0\nm.times{|i| s+=i*(m-i)}\ns=2*s+m\n\nprint ((2*r*s+x*m*(m-1))/m/m)\n"}, {"source_code": "m,r=gets.split.map(&:to_i)\nx=2**0.5*r\ns=0\nm.times{|i| s+=i*(m-i)}\ns=2*s+m\n\nprint ((2*r*s+x*m*(m-1))/m.to_f/m.to_f)\n"}, {"source_code": "m,r=gets.split.map(&:to_i)\nx=2**0.5*r\ns=0\nm.times{|i| s+=i*(m-i)}\ns=2*s+m\np ((2*r*s+x*m*(m-1))/m/m).round(9)\n"}, {"source_code": "m,r=gets.split.map(&:to_i)\nx=2**0.5*r\ns=0\nm.times{|i| s+=i*(m-i)}\ns=2*s+m\n\np ((2*r*s+x*m*(m-1))/m/m)\n"}], "src_uid": "f827ea399e6b801c0392eac53710d950"} {"nl": {"description": "Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire.After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings!To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names.Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's!Given the string $$$s$$$ representing Jeff's product name and the string $$$c$$$ representing his competitor's product name, find a way to swap at most one pair of characters in $$$s$$$ (that is, find two distinct indices $$$i$$$ and $$$j$$$ and swap $$$s_i$$$ and $$$s_j$$$) such that the resulting new name becomes strictly lexicographically smaller than $$$c$$$, or determine that it is impossible.Note: String $$$a$$$ is strictly lexicographically smaller than string $$$b$$$ if and only if one of the following holds: $$$a$$$ is a proper prefix of $$$b$$$, that is, $$$a$$$ is a prefix of $$$b$$$ such that $$$a \\neq b$$$; There exists an integer $$$1 \\le i \\le \\min{(|a|, |b|)}$$$ such that $$$a_i < b_i$$$ and $$$a_j = b_j$$$ for $$$1 \\le j < i$$$. ", "input_spec": "The first line of input contains a single integer $$$t$$$ ($$$1 \\le t \\le 1500$$$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing two space-separated strings $$$s$$$ and $$$c$$$ ($$$2 \\le |s| \\le 5000, 1 \\le |c| \\le 5000$$$). The strings $$$s$$$ and $$$c$$$ consists of uppercase English letters. It is guaranteed that the sum of $$$|s|$$$ in the input is at most $$$5000$$$ and the sum of the $$$|c|$$$ in the input is at most $$$5000$$$.", "output_spec": "For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $$$c$$$. In case there are many possible such strings, you can output any of them; three dashes (the string \"---\" without quotes) if it is impossible. ", "sample_inputs": ["3\nAZAMON APPLE\nAZAMON AAAAAAAAAAALIBABA\nAPPLE BANANA"], "sample_outputs": ["AMAZON\n---\nAPPLE"], "notes": "NoteIn the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string \"AMAZON\" is lexicographically smaller than \"APPLE\".It is impossible to improve the product's name in the second test case and satisfy all conditions.In the third test case, it is possible not to swap a pair of characters. The name \"APPLE\" is lexicographically smaller than \"BANANA\". Note that there are other valid answers, e.g., \"APPEL\". "}, "positive_code": [{"source_code": "N=STDIN.gets.strip.to_i\n(0..N-1).each do |i|\n s,t=STDIN.gets.strip.split\n if s s[mi[i]]\n s[i], s[mi[i]] = s[mi[i]], s[i]\n break\n end\n end\n puts (s < c ? s : '---')\n end\nend\n\nsolution\n"}, {"source_code": "def smallest(w)\n ary = w.chars\n ary_with_index = ary.each_with_index.sort\n ary_with_index.each_with_index do |(a, i), j|\n if i > j\n a = ary[i]\n _, i = ary_with_index.select { |(x, y)| x == a }.max_by { |x, y| y}\n ary[i], ary[j] = ary[j], ary[i]\n return ary.join('')\n end\n end\n ary.join('')\nend\n\nnb = gets.to_i\nnb.times do\n w1, w2 = gets.chomp.split\n w = smallest(w1)\n puts w < w2 ? w : '---'\nend\n\n"}], "negative_code": [{"source_code": "def smallest(w)\n ary = w.chars\n ary_with_index = ary.each_with_index.sort\n ary_with_index.each_with_index do |(a, i), j|\n if i > j\n ary[i], ary[j] = ary[j], ary[i]\n break\n end\n end\n ary.join('')\nend\n\nnb = gets.to_i\nnb.times do\n w1, w2 = gets.chomp.split\n w = smallest(w1)\n puts w < w2 ? w : '---'\nend"}, {"source_code": "def smallest(w)\n ary = w.chars\n ary_with_index = ary.each_with_index.sort\n ary_with_index.each_with_index do |(a, i), j|\n if i > j\n ary[i], ary[j] = ary[j], ary[i]\n return ary.join('')\n end\n end\n ary.join('')\nend\n\nnb = gets.to_i\nnb.times do\n w1, w2 = gets.chomp.split\n w = smallest(w1)\n puts w < w2 ? w : '---'\nend\n\n"}, {"source_code": "def smallest(w)\n ary = w.chars\n ary_with_index = ary.each_with_index.sort\n ary_with_index.each_with_index do |(a, i), j|\n if i > j\n ary[i], ary[j] = ary[j], ary[i]\n break\n end\n end\n ary.join('')\nend\n\nnb = gets.to_i\nnb.times do\n w1, w2 = gets.chomp.split\n w = smallest(w1)\n print w < w2 ? w : '---'\nend"}], "src_uid": "88743b7acb4a95dc0d5bb2e073714c49"} {"nl": {"description": "Polar bears like unique arrays — that is, arrays without repeated elements. You have got a unique array s with length n containing non-negative integers. Since you are good friends with Alice and Bob, you decide to split the array in two. Precisely, you need to construct two arrays a and b that are also of length n, with the following conditions for all i (1 ≤ i ≤ n): ai, bi are non-negative integers; si = ai + bi . Ideally, a and b should also be unique arrays. However, life in the Arctic is hard and this is not always possible. Fortunately, Alice and Bob are still happy if their arrays are almost unique. We define an array of length n to be almost unique, if and only if it can be turned into a unique array by removing no more than entries.For example, the array [1, 2, 1, 3, 2] is almost unique because after removing the first two entries, it becomes [1, 3, 2]. The array [1, 2, 1, 3, 1, 2] is not almost unique because we need to remove at least 3 entries to turn it into a unique array.So, your task is to split the given unique array s into two almost unique arrays a and b.", "input_spec": "The first line of the input contains integer n (1 ≤ n ≤ 105). The second line contains n distinct integers s1, s2, ... sn (0 ≤ si ≤ 109).", "output_spec": "If it is possible to make Alice and Bob happy (if you can split the given array), print \"YES\" (without quotes) in the first line. In the second line, print the array a. In the third line, print the array b. There may be more than one solution. Any of them will be accepted. If it is impossible to split s into almost unique arrays a and b, print \"NO\" (without quotes) in the first line.", "sample_inputs": ["6\n12 5 8 3 11 9"], "sample_outputs": ["YES\n6 2 6 0 2 4\n6 3 2 3 9 5"], "notes": "NoteIn the sample, we can remove the first two entries from a and the second entry from b to make them both unique."}, "positive_code": [{"source_code": "n = gets.to_i;\nc = gets.split(' ').map(&:to_i);\nidx = (0..n-1).to_a.sort{|x, y|\n c[x] <=> c[y]\n}\na = Array.new(n); b = Array.new(n);\ncnt = (n+2)/3;\n\nn.times{|i|\n j = idx[i];\n if i < cnt\n a[j] = i; b[j] = c[j] - a[j];\n elsif i < 2*cnt\n b[j] = i; a[j] = c[j] - b[j];\n else\n b[j] = n - i - 1; a[j] = c[j] - b[j];\n end\n}\nputs \"YES\"\nputs a.map(&:to_s).join(' ')\nputs b.map(&:to_s).join(' ')\n\n"}, {"source_code": "n = gets.to_i;\nc = gets.split(' ').map(&:to_i);\nidx = (0..n-1).to_a.sort{|x, y|\n c[x] <=> c[y]\n}\na = Array.new(n); b = Array.new(n);\ncnt = (n+2)/3;\n\nn.times{|i|\n j = idx[i];\n if i < cnt\n a[j] = i; b[j] = c[j] - a[j];\n elsif i < 2*cnt\n b[j] = i; a[j] = c[j] - b[j];\n else\n b[j] = n - i - 1; a[j] = c[j] - b[j];\n end\n}\nputs \"YES\"\nputs a.map(&:to_s).join(' ')\nputs b.map(&:to_s).join(' ')\n\n"}, {"source_code": "n = gets.to_i;\nc = gets.split(' ').map(&:to_i);\nidx = (0..n-1).to_a.sort{|x, y|\n c[x] <=> c[y]\n}\na = Array.new(n); b = Array.new(n);\ncnt = (n+2)/3;\n\nn.times{|i|\n j = idx[i];\n if i < cnt\n a[j] = i; b[j] = c[j] - a[j];\n elsif i < 2*cnt\n b[j] = i; a[j] = c[j] - b[j];\n else\n b[j] = n - i - 1; a[j] = c[j] - b[j];\n end\n}\nputs \"YES\"\nputs a.map(&:to_s).join(' ')\nputs b.map(&:to_s).join(' ')\n\n"}, {"source_code": "n = gets.to_i;\nc = gets.split(' ').map(&:to_i);\nidx = (0..n-1).to_a.sort{|x, y|\n c[x] <=> c[y]\n}\na = Array.new(n); b = Array.new(n);\ncnt = (n+2)/3;\n\nn.times{|i|\n j = idx[i];\n if i < cnt\n a[j] = i; b[j] = c[j] - a[j];\n elsif i < 2*cnt\n b[j] = i; a[j] = c[j] - b[j];\n else\n b[j] = n - i - 1; a[j] = c[j] - b[j];\n end\n}\nputs \"YES\"\nputs a.map(&:to_s).join(' ')\nputs b.map(&:to_s).join(' ')\n\n"}, {"source_code": "n = gets.to_i;\nc = gets.split(' ').map(&:to_i);\nidx = (0..n-1).to_a.sort{|x, y|\n c[x] <=> c[y]\n}\na = Array.new(n); b = Array.new(n);\ncnt = (n+2)/3;\n\nn.times{|i|\n j = idx[i];\n if i < cnt\n a[j] = i; b[j] = c[j] - a[j];\n elsif i < 2*cnt\n b[j] = i; a[j] = c[j] - b[j];\n else\n b[j] = n - i - 1; a[j] = c[j] - b[j];\n end\n}\nputs \"YES\"\nputs a.map(&:to_s).join(' ')\nputs b.map(&:to_s).join(' ')\n\n"}, {"source_code": "n = gets.to_i;\nc = gets.split(' ').map(&:to_i);\nidx = (0..n-1).to_a.sort{|x, y|\n c[x] <=> c[y]\n}\na = Array.new(n); b = Array.new(n);\ncnt = (n+2)/3;\n\nn.times{|i|\n j = idx[i];\n if i < cnt\n a[j] = i; b[j] = c[j] - a[j];\n elsif i < 2*cnt\n b[j] = i; a[j] = c[j] - b[j];\n else\n b[j] = n - i - 1; a[j] = c[j] - b[j];\n end\n}\nputs \"YES\"\nputs a.map(&:to_s).join(' ')\nputs b.map(&:to_s).join(' ')\n\n"}, {"source_code": "n = gets.to_i;\nc = gets.split(' ').map(&:to_i);\nidx = (0..n-1).to_a.sort{|x, y|\n c[x] <=> c[y]\n}\na = Array.new(n); b = Array.new(n);\ncnt = (n+2)/3;\n\nn.times{|i|\n j = idx[i];\n if i < cnt\n a[j] = i; b[j] = c[j] - a[j];\n elsif i < 2*cnt\n b[j] = i; a[j] = c[j] - b[j];\n else\n b[j] = n - i - 1; a[j] = c[j] - b[j];\n end\n}\nputs \"YES\"\nputs a.map(&:to_s).join(' ')\nputs b.map(&:to_s).join(' ')\n"}, {"source_code": "n = gets.to_i;\nc = gets.split(' ').map(&:to_i);\nidx = (0..n-1).to_a.sort{|x, y|\n c[x] <=> c[y]\n}\na = Array.new(n); b = Array.new(n);\ncnt = (n+2)/3;\n\nn.times{|i|\n j = idx[i];\n if i < cnt\n a[j] = i; b[j] = c[j] - a[j];\n elsif i < 2*cnt\n b[j] = i; a[j] = c[j] - b[j];\n else\n b[j] = n - i - 1; a[j] = c[j] - b[j];\n end\n}\nputs \"YES\"\nputs a.map(&:to_s).join(' ')\nputs b.map(&:to_s).join(' ')\n\n"}, {"source_code": "n = gets.to_i;\nc = gets.split(' ').map(&:to_i);\nidx = (0..n-1).to_a.sort{|x, y|\n c[x] <=> c[y]\n}\na = Array.new(n); b = Array.new(n);\ncnt = (n+2)/3;\n\nn.times{|i|\n j = idx[i];\n if i < cnt\n a[j] = i; b[j] = c[j] - a[j];\n elsif i < 2*cnt\n b[j] = i; a[j] = c[j] - b[j];\n else\n b[j] = n - i - 1; a[j] = c[j] - b[j];\n end\n}\nputs \"YES\"\nputs a.map(&:to_s).join(' ')\nputs b.map(&:to_s).join(' ')\n\n"}], "negative_code": [{"source_code": "n = gets.to_i;\nc = gets.split(' ').map(&:to_i);\nidx = (0..n-1).to_a.sort{|x, y|\n c[x] <=> c[y]\n}\na = Array.new(n); b = Array.new(n);\ncnt = (n+2)/3;\n\nn.times{|i|\n j = idx[i];\n if i < cnt\n a[j] = i; b[j] = c[j] - a[j];\n elsif i < 2*cnt\n b[j] = i; a[j] = c[j] - b[j];\n else\n b[j] = n - i; a[j] = c[j] - b[j];\n end\n}\nputs \"YES\"\nputs a.map(&:to_s).join(' ')\nputs b.map(&:to_s).join(' ')\n"}, {"source_code": "n = gets.to_i;\nc = gets.split(' ').map(&:to_i);\nidx = (0..n-1).to_a.sort{|x, y|\n c[x] <=> c[y]\n}\na = Array.new(n); b = Array.new(n);\ncnt = (n+2)/3;\n\nn.times{|i|\n j = idx[i];\n if i < cnt\n a[j] = i; b[j] = c[j] - a[j];\n elsif i < 2*cnt\n b[j] = i; a[j] = c[j] - b[j];\n else\n b[j] = n - i; a[j] = c[j] - b[j];\n end\n}\nputs a.map(&:to_s).join(' ')\nputs b.map(&:to_s).join(' ')\n"}], "src_uid": "fc97f05864c05950cd9acd9e03f4a729"} {"nl": {"description": "For an array $$$[b_1, b_2, \\ldots, b_m]$$$ define its number of inversions as the number of pairs $$$(i, j)$$$ of integers such that $$$1 \\le i < j \\le m$$$ and $$$b_i>b_j$$$. Let's call array $$$b$$$ odd if its number of inversions is odd. For example, array $$$[4, 2, 7]$$$ is odd, as its number of inversions is $$$1$$$, while array $$$[2, 1, 4, 3]$$$ isn't, as its number of inversions is $$$2$$$.You are given a permutation $$$[p_1, p_2, \\ldots, p_n]$$$ of integers from $$$1$$$ to $$$n$$$ (each of them appears exactly once in the permutation). You want to split it into several consecutive subarrays (maybe just one), so that the number of the odd subarrays among them is as large as possible. What largest number of these subarrays may be odd?", "input_spec": "The first line of the input contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^5$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$)  — the size of the permutation. The second line of each test case contains $$$n$$$ integers $$$p_1, p_2, \\ldots, p_n$$$ ($$$1 \\le p_i \\le n$$$, all $$$p_i$$$ are distinct)  — the elements of the permutation. The sum of $$$n$$$ over all test cases doesn't exceed $$$2\\cdot 10^5$$$.", "output_spec": "For each test case output a single integer  — the largest possible number of odd subarrays that you can get after splitting the permutation into several consecutive subarrays.", "sample_inputs": ["5\n\n3\n\n1 2 3\n\n4\n\n4 3 2 1\n\n2\n\n1 2\n\n2\n\n2 1\n\n6\n\n4 5 6 1 2 3"], "sample_outputs": ["0\n2\n0\n1\n1"], "notes": "NoteIn the first and third test cases, no matter how we split our permutation, there won't be any odd subarrays.In the second test case, we can split our permutation into subarrays $$$[4, 3], [2, 1]$$$, both of which are odd since their numbers of inversions are $$$1$$$.In the fourth test case, we can split our permutation into a single subarray $$$[2, 1]$$$, which is odd.In the fifth test case, we can split our permutation into subarrays $$$[4, 5], [6, 1, 2, 3]$$$. The first subarray has $$$0$$$ inversions, and the second has $$$3$$$, so it is odd."}, "positive_code": [{"source_code": "gets.to_i.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n ans = 0\n i = 1\n while i < n do \n if a[i]= 0 then\n k = 0\n else\n k = 1\n end\n i += 1\n while k < 2 && i < n do\n folder[-1] += 1\n if nums[i] < 0 then\n k += 1\n end\n i += 1\n end\n while i < n && nums[i] >= 0 do\n folder[-1] += 1\n i += 1\n end\nend\n\nputs folder.size\nputs folder.join(\" \")"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\ncount = 0\nneg = 0\nfiles = Array.new\nfor i in a\n if i < 0 then neg += 1 end\n if neg == 3\n files << count\n count = 0\n neg = 1\n end\n count += 1\nend\nfiles << count\nputs files.count\nfiles.each {|x| print x, \" \"}"}, {"source_code": "e = []\nnn = 1\ngets\nns = gets.chomp.split.map{|a| Integer(a)}\nns.each.with_index{|x,i| if x < 0 then e.insert(-1,i+1) end }\ne=e.each_slice(2).map{|x| x.last}\nif e.length == 0 then\n puts [1,ns.length]\nelse\n puts e.length\n e[e.length-1]=ns.length\n e.insert(0,0)\n puts e[1..e.length-1].zip(e[0..e.length-2]).map{|a,b| a-b}.join(\" \")\nend"}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\nres=[]\nc=k=0\na.each do |v|\n c+=1\n k+=1 if v<0\n if k==2 then\n res.push(c)\n c=k=0\n end\nend\nif res.empty? then\n puts 1\n puts n\nelse\n if k > 0 then\n res.push(c)\n else\n res[res.size()-1]+=c\n end\n puts res.size\n puts res*' '\nend\n"}, {"source_code": "def main()\n\tn=gets.chomp.to_i;\n\tar=gets.chomp.split(\" \");\n\tar=ar.map{|i| i.to_i};\n\tnof=0;non=0;noeinf=[];\n\tnoe=0;\n\tfor i in 0...(ar.length)\n\t\tif ar[i]<0\n\t\t\tnon+=1\n\t\telse\n\t\t\tnoe+=1;\n\t\tend\n\t\tif non==3\n\t\t\tnoeinf << noe+2;\n\t\t\tnof+=1;\n\t\t\tnon=1;\n\t\t\tnoe=0;\n\t\tend\n\tend\n\tif non>0 || noe >0\n\t\tnof+=1;\n\t\tnoeinf << (non+noe);\n\tend \n\tputs nof;\n\tputs noeinf.join(\" \")\nend\nmain()"}, {"source_code": "n=readline.to_i\na=gets.chomp.split.map{|i| i.to_i}\ncounter=0\nnum=0\nres=[]\n\na.each do |i|\nif i<0 then\n counter=counter+1\nend\nif counter==3 then\n res.push num\n num=0\ncounter=1\nend\nnum=num+1\nend\n\nif num>0\n res.push num\nend\n\nputs res.size\nres.each do |i|\n print i.to_s+' '\nend\nprint \"\\n\"\n"}, {"source_code": "n=gets.to_i\na=gets.split.collect{|e| e.to_i}\nb=[]\nk=0\nl=0\nfor i in 0..n-1 do\n l+=1\n if (a[i]<0) then \n if (k==2) then\n b<0 || noe >0\n\t\tnof+=1;\n\t\tnoeinf << (non+noe);\n\tend \n\tputs nof;\n\tputs noeinf.join(\" \")\nend\nmain()"}, {"source_code": "def main()\n\tn=gets.chomp.to_i;\n\tar=gets.chomp.split(\" \");\n\tar=ar.map{|i| i.to_i};\n\tnof=0;non=0;noeinf=[];\n\tnoe=0;\n\tfor i in 0...(ar.length)\n\t\tif ar[i]<0\n\t\t\tnon+=1\n\t\telse\n\t\t\tnoe+=1;\n\t\tend\n\t\tif non==2 && noe>=1\n\t\t\tnoeinf << noe+non;\n\t\t\tnof+=1;\n\t\t\tnon=0;\n\t\t\tnoe=0;\n\t\tend\n\tend\n\tif non>0 || noe >0\n\t\tnof+=1;\n\t\tnoeinf << (non+noe);\n\tend \n\tputs nof;\n\tputs noeinf.join(\" \")\nend\nmain()"}, {"source_code": "def main()\n\tn=gets.chomp.to_i;\n\tar=gets.chomp.split(\" \");\n\tar=ar.map{|i| i.to_i};\n\tar=ar.sort\n\tnof=0;non=0;noeinf=[];\n\tfor i in 0...(ar.length)\n\t\tif ar[i]<0\n\t\t\tnon+=1\n\t\telse\n\t\t\tnof+=1;\n\t\t\tnoef=(ar.length)-i;\n\t\t\tnoeinf << noef+non;\n \t\t\tbreak;\n\t\tend\n\t\tif non==3\n\t\t\tnon=0;\n\t\t\tnof+=1;\n\t\t\tnoeinf << 3;\n\t\tend\n\tend\n\tputs nof;\n\tputs noeinf.join(\" \")\nend\nmain()"}, {"source_code": "def main()\n\tn=gets.chomp.to_i;\n\tar=gets.chomp.split(\" \");\n\tar=ar.map{|i| i.to_i};\n\tar=ar.sort\n\tnof=0;non=0;noeinf=[];\n\tfor i in 0...(ar.length)\n\t\tif ar[i]<0\n\t\t\tnon+=1\n\t\telse\n\t\t\tnof+=1;\n\t\t\tnoef=(ar.length)-i;\n\t\t\tnoeinf << noef+non;\n \t\t\tbreak;\n\t\tend\n\t\tif non==3\n\t\t\tnon=0;\n\t\t\tnof+=1;\n\t\t\tnoeinf << 3;\n\t\tend\n\tend\n\tputs nof;\n\tnoeinf=noeinf.sort\n\tputs noeinf.join(\" \")\nend\nmain()"}, {"source_code": "def main()\n\tn=gets.chomp.to_i;\n\tar=gets.chomp.split(\" \");\n\tar=ar.map{|i| i.to_i};\n\tnof=0;non=0;noeinf=[];\n\tnoe=0;\n\tfor i in 0...(ar.length)\n\t\tif ar[i]<0\n\t\t\tnon+=1\n\t\telse\n\t\t\tnoe+=1;\n\t\tend\n\t\tif non==2\n\t\t\tnoeinf << noe+non;\n\t\t\tnof+=1;\n\t\t\tnon=0;\n\t\t\tnoe=0;\n\t\tend\n\tend\n\tif non>0 || noe >0\n\t\tnof+=1;\n\t\tnoeinf << (non+noe);\n\tend \n\tputs nof;\n\tputs noeinf.join(\" \")\nend\nmain()"}], "src_uid": "3f320920a023c3bc10ba1525e8c89044"} {"nl": {"description": "You are given a string $$$s$$$. You have to determine whether it is possible to build the string $$$s$$$ out of strings aa, aaa, bb and/or bbb by concatenating them. You can use the strings aa, aaa, bb and/or bbb any number of times and in any order.For example: aaaabbb can be built as aa $$$+$$$ aa $$$+$$$ bbb; bbaaaaabbb can be built as bb $$$+$$$ aaa $$$+$$$ aa $$$+$$$ bbb; aaaaaa can be built as aa $$$+$$$ aa $$$+$$$ aa; abab cannot be built from aa, aaa, bb and/or bbb. ", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. Each test case consists of one line containing the string $$$s$$$ ($$$1 \\le |s| \\le 50$$$), consisting of characters a and/or b.", "output_spec": "For each test case, print YES if it is possible to build the string $$$s$$$. Otherwise, print NO. You may print each letter in any case (for example, YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).", "sample_inputs": ["8\n\naaaabbb\n\nbbaaaaabbb\n\naaaaaa\n\nabab\n\na\n\nb\n\naaaab\n\nbbaaa"], "sample_outputs": ["YES\nYES\nYES\nNO\nNO\nNO\nNO\nYES"], "notes": "NoteThe first four test cases of the example are described in the statement."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n s = gets.chomp\r\n puts s.chars.chunk { |a| a }.map(&:last).map(&:size).min == 1 ? \"NO\" : \"YES\"\r\nend\r\n"}, {"source_code": "gets.to_i.times do\n s = gets.chomp.split('')\n c = 1\n flag = true\n 1.upto(s.size-1) do |i|\n if s[i]==s[i-1] then\n c += 1\n else\n flag = false if c < 2\n c = 1\n end\n end\n flag = false if c < 2\n puts flag ? \"YES\":\"NO\"\nend\n"}], "negative_code": [], "src_uid": "e95e2d21777c1d686bede1b0a5dacbf5"} {"nl": {"description": "You are given a string $$$s$$$, consisting only of characters '0' and '1'.You have to choose a contiguous substring of $$$s$$$ and remove all occurrences of the character, which is a strict minority in it, from the substring.That is, if the amount of '0's in the substring is strictly smaller than the amount of '1's, remove all occurrences of '0' from the substring. If the amount of '1's is strictly smaller than the amount of '0's, remove all occurrences of '1'. If the amounts are the same, do nothing.You have to apply the operation exactly once. What is the maximum amount of characters that can be removed?", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of testcases. The only line of each testcase contains a non-empty string $$$s$$$, consisting only of characters '0' and '1'. The length of $$$s$$$ doesn't exceed $$$2 \\cdot 10^5$$$. The total length of strings $$$s$$$ over all testcases doesn't exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each testcase, print a single integer — the maximum amount of characters that can be removed after applying the operation exactly once.", "sample_inputs": ["4\n01\n1010101010111\n00110001000\n1"], "sample_outputs": ["0\n5\n3\n0"], "notes": "NoteIn the first testcase, you can choose substrings \"0\", \"1\" or \"01\". In \"0\" the amount of '0' is $$$1$$$, the amount of '1' is $$$0$$$. '1' is a strict minority, thus all occurrences of it are removed from the substring. However, since there were $$$0$$$ of them, nothing changes. Same for \"1\". And in \"01\" neither of '0' or '1' is a strict minority. Thus, nothing changes. So there is no way to remove any characters.In the second testcase, you can choose substring \"10101010101\". It contains $$$5$$$ characters '0' and $$$6$$$ characters '1'. '0' is a strict minority. Thus, you can remove all its occurrences. There exist other substrings that produce the same answer.In the third testcase, you can choose substring \"011000100\". It contains $$$6$$$ characters '0' and $$$3$$$ characters '1'. '1' is a strict minority. Thus, you can remove all its occurrences."}, "positive_code": [{"source_code": "gets.to_i.times do\n s = gets.strip\n count = [0, 0]\n s.each_char { |c| count[c.ord - '0'.ord] += 1 }\n if count[0] == count[1]\n c = s[s.size - 1]\n count[c.ord - '0'.ord] -= 1\n end\n puts count.min\nend\n"}], "negative_code": [], "src_uid": "62f5798bdcea237c0df9b4cd288b97de"} {"nl": {"description": "You are given an array $$$a$$$ of size $$$n$$$. Each element in this array is an integer between $$$1$$$ and $$$10^9$$$.You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between $$$1$$$ and $$$10^9$$$. Output the minimum number of operations needed such that the resulting array doesn't contain any local maximums, and the resulting array after the operations.An element $$$a_i$$$ is a local maximum if it is strictly larger than both of its neighbors (that is, $$$a_i > a_{i - 1}$$$ and $$$a_i > a_{i + 1}$$$). Since $$$a_1$$$ and $$$a_n$$$ have only one neighbor each, they will never be a local maximum.", "input_spec": "Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 \\leq t \\leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(2 \\leq n \\leq 2 \\cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots ,a_n$$$ $$$(1 \\leq a_i \\leq 10^9)$$$, the elements of array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, first output a line containing a single integer $$$m$$$ — minimum number of operations required. Then ouput a line consist of $$$n$$$ integers — the resulting array after the operations. Note that this array should differ in exactly $$$m$$$ elements from the initial array. If there are multiple answers, print any.", "sample_inputs": ["5\n3\n2 1 2\n4\n1 2 3 1\n5\n1 2 1 2 1\n9\n1 2 1 3 2 3 1 2 1\n9\n2 1 3 1 3 1 3 1 3"], "sample_outputs": ["0\n2 1 2\n1\n1 3 3 1\n1\n1 2 2 2 1\n2\n1 2 3 3 2 3 3 2 1\n2\n2 1 3 3 3 1 1 1 3"], "notes": "NoteIn the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change $$$a_2$$$ to $$$3$$$, then the array don't have local maximums."}, "positive_code": [{"source_code": "t = readline.strip.to_i\r\nt.times do\r\n n = readline.strip.to_i\r\n nums = readline.strip.split(/\\s+/).map(&:to_i)\r\n\r\n c = nums.count\r\n\r\n if c <= 2\r\n puts 0\r\n puts nums.join(' ')\r\n next\r\n end\r\n\r\n i = 1\r\n changes = 0\r\n while i < c - 1\r\n if nums[i] > nums[i-1] && nums[i] > nums[i+1] # local max\r\n if i+2 < c && nums[i+1] < nums[i+2]\r\n nums[i+1] = [nums[i], nums[i+2]].max\r\n changes += 1\r\n i += 1\r\n # puts \"i=#{i} next is local min\"\r\n else\r\n changes += 1\r\n nums[i] = [nums[i-1], nums[i+1]].max\r\n end\r\n end\r\n i += 1\r\n end\r\n\r\n# 1 3 2 3 1\r\n# i\r\n\r\n puts changes\r\n puts nums.join(' ')\r\nend\r\n"}], "negative_code": [{"source_code": "t = readline.strip.to_i\r\nt.times do\r\n n = readline.strip.to_i\r\n nums = readline.strip.split(/\\s+/).map(&:to_i)\r\n\r\n c = nums.count\r\n\r\n if c <= 3\r\n puts 0\r\n puts nums.join(' ')\r\n next\r\n end\r\n\r\n i = 1\r\n changes = 0\r\n while i < c - 1\r\n if nums[i] > nums[i-1] && nums[i] > nums[i+1] # local max\r\n if i+2 < c && nums[i+1] < nums[i+2]\r\n nums[i+1] = [nums[i], nums[i+2]].max\r\n changes += 1\r\n i += 1\r\n # puts \"i=#{i} next is local min\"\r\n else\r\n changes += 1\r\n nums[i] = [nums[i-1], nums[i+1]].max\r\n end\r\n end\r\n i += 1\r\n end\r\n\r\n# 1 3 2 3 1\r\n# i\r\n\r\n puts changes\r\n puts nums.join(' ')\r\nend\r\n"}], "src_uid": "255abf8750541f54d8ff0f1f80e1f8e7"} {"nl": {"description": "There are $$$n$$$ emotes in very popular digital collectible card game (the game is pretty famous so we won't say its name). The $$$i$$$-th emote increases the opponent's happiness by $$$a_i$$$ units (we all know that emotes in this game are used to make opponents happy).You have time to use some emotes only $$$m$$$ times. You are allowed to use any emotion once, more than once, or not use it at all. The only restriction is that you cannot use the same emote more than $$$k$$$ times in a row (otherwise the opponent will think that you're trolling him).Note that two emotes $$$i$$$ and $$$j$$$ ($$$i \\ne j$$$) such that $$$a_i = a_j$$$ are considered different.You have to make your opponent as happy as possible. Find the maximum possible opponent's happiness.", "input_spec": "The first line of the input contains three integers $$$n, m$$$ and $$$k$$$ ($$$2 \\le n \\le 2 \\cdot 10^5$$$, $$$1 \\le k \\le m \\le 2 \\cdot 10^9$$$) — the number of emotes, the number of times you can use emotes and the maximum number of times you may use the same emote in a row. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$), where $$$a_i$$$ is value of the happiness of the $$$i$$$-th emote.", "output_spec": "Print one integer — the maximum opponent's happiness if you use emotes in a way satisfying the problem statement.", "sample_inputs": ["6 9 2\n1 3 3 7 4 2", "3 1000000000 1\n1000000000 987654321 1000000000"], "sample_outputs": ["54", "1000000000000000000"], "notes": "NoteIn the first example you may use emotes in the following sequence: $$$4, 4, 5, 4, 4, 5, 4, 4, 5$$$."}, "positive_code": [{"source_code": "if File.exists?(\"input\")\n $stdin = File.open(\"input\")\n $stdout = File.open(\"output\",\"w\")\nend\n\nnmk = gets.chomp.split(' ').map(&:to_i)\nn = nmk[0]\nm = nmk[1]\nk = nmk[2]\n\narr = gets.chomp.split(\" \").map(&:to_i)\n\nmax=0\nmaxtwo=0\n\n(0..n-1).each do |i|\n\tif(arr[i]>max)\n \tmaxtwo = max\n \tmax=arr[i]\n elsif(arr[i]>maxtwo)\n \tmaxtwo = arr[i]\n end\nend\n\nans=(m/(k+1))*(max*(k)+maxtwo)\nleftover = m-(m/(k+1))*(k+1);\nans+=max*leftover\n\nputs ans"}], "negative_code": [], "src_uid": "96dee17800e147350bd37e60f66f49dd"} {"nl": {"description": "You are given an array $$$a$$$ of length $$$n$$$.Let's define the eversion operation. Let $$$x = a_n$$$. Then array $$$a$$$ is partitioned into two parts: left and right. The left part contains the elements of $$$a$$$ that are not greater than $$$x$$$ ($$$\\le x$$$). The right part contains the elements of $$$a$$$ that are strictly greater than $$$x$$$ ($$$> x$$$). The order of elements in each part is kept the same as before the operation, i. e. the partition is stable. Then the array is replaced with the concatenation of the left and the right parts.For example, if the array $$$a$$$ is $$$[2, 4, 1, 5, 3]$$$, the eversion goes like this: $$$[2, 4, 1, 5, 3] \\to [2, 1, 3], [4, 5] \\to [2, 1, 3, 4, 5]$$$.We start with the array $$$a$$$ and perform eversions on this array. We can prove that after several eversions the array $$$a$$$ stops changing. Output the minimum number $$$k$$$ such that the array stops changing after $$$k$$$ eversions.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 100$$$). Description of the test cases follows. The first line contains a single integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case print a single integer $$$k$$$ — the number of eversions after which the array stops changing.", "sample_inputs": ["3\n5\n2 4 1 5 3\n5\n5 3 2 4 1\n4\n1 1 1 1"], "sample_outputs": ["1\n2\n0"], "notes": "NoteConsider the fist example. The first eversion: $$$a = [1, 4, 2, 5, 3]$$$, $$$x = 3$$$. $$$[2, 4, 1, 5, 3] \\to [2, 1, 3], [4, 5] \\to [2, 1, 3, 4, 5]$$$. The second and following eversions: $$$a = [2, 1, 3, 4, 5]$$$, $$$x = 5$$$. $$$[2, 1, 3, 4, 5] \\to [2, 1, 3, 4, 5], [] \\to [2, 1, 3, 4, 5]$$$. This eversion does not change the array, so the answer is $$$1$$$. Consider the second example. The first eversion: $$$a = [5, 3, 2, 4, 1]$$$, $$$x = 1$$$. $$$[5, 3, 2, 4, 1] \\to [1], [5, 3, 2, 4] \\to [1, 5, 3, 2, 4]$$$. The second eversion: $$$a = [1, 5, 3, 2, 4]$$$, $$$x = 4$$$. $$$[1, 5, 3, 2, 4] \\to [1, 3, 2, 4], [5] \\to [1, 3, 2, 4, 5]$$$. The third and following eversions: $$$a = [1, 3, 2, 4, 5]$$$, $$$x = 5$$$. $$$[1, 3, 2, 4, 5] \\to [1, 3, 2, 4, 5], [] \\to [1, 3, 2, 4, 5]$$$. This eversion does not change the array, so the answer is $$$2$$$. "}, "positive_code": [{"source_code": "testcases = Integer(gets.chomp)\r\nresultados = []\r\n\r\nfor i in 0..testcases-1\r\n n = Integer(gets.chomp)\r\n n -= 1 \r\n arreglo = gets.chomp\r\n a = arreglo.split(\" \")\r\n aux = a.sort\r\n mayor = aux[n].to_i\r\n k = a[n].to_i\r\n n -= 1\r\n divisiones = 0\r\n while n != -1\r\n if a[n] == mayor\r\n n = -1\r\n elsif !(a[n].to_i <= k)\r\n k = a[n].to_i\r\n divisiones += 1\r\n end\r\n n-=1\r\n end\r\n resultados.push(divisiones)\r\nend\r\nputs resultados\r\n"}], "negative_code": [], "src_uid": "451b381cc8738bebec4eeb0f02dc7849"} {"nl": {"description": "Vasya has a multiset $$$s$$$ consisting of $$$n$$$ integer numbers. Vasya calls some number $$$x$$$ nice if it appears in the multiset exactly once. For example, multiset $$$\\{1, 1, 2, 3, 3, 3, 4\\}$$$ contains nice numbers $$$2$$$ and $$$4$$$.Vasya wants to split multiset $$$s$$$ into two multisets $$$a$$$ and $$$b$$$ (one of which may be empty) in such a way that the quantity of nice numbers in multiset $$$a$$$ would be the same as the quantity of nice numbers in multiset $$$b$$$ (the quantity of numbers to appear exactly once in multiset $$$a$$$ and the quantity of numbers to appear exactly once in multiset $$$b$$$).", "input_spec": "The first line contains a single integer $$$n~(2 \\le n \\le 100)$$$. The second line contains $$$n$$$ integers $$$s_1, s_2, \\dots s_n~(1 \\le s_i \\le 100)$$$ — the multiset $$$s$$$.", "output_spec": "If there exists no split of $$$s$$$ to satisfy the given requirements, then print \"NO\" in the first line. Otherwise print \"YES\" in the first line. The second line should contain a string, consisting of $$$n$$$ characters. $$$i$$$-th character should be equal to 'A' if the $$$i$$$-th element of multiset $$$s$$$ goes to multiset $$$a$$$ and 'B' if if the $$$i$$$-th element of multiset $$$s$$$ goes to multiset $$$b$$$. Elements are numbered from $$$1$$$ to $$$n$$$ in the order they are given in the input. If there exist multiple solutions, then print any of them.", "sample_inputs": ["4\n3 5 7 1", "3\n3 5 1"], "sample_outputs": ["YES\nBABA", "NO"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\ns = gets.split.map(&:to_i)\na = Hash.new(0)\nn.times do |i|\n a[s[i]] += 1\nend\nif a.values.count(1).odd? && a.values.count{|j|j > 2} == 0\n puts \"NO\"\n exit\nend\nputs \"YES\"\nans = \"A\"*n\np = 0\nn.times do |i|\n if a[s[i]] == 1\n if p == 1\n ans[i] = \"B\"\n end\n p = 1-p\n end\nend\nif p == 1\n n.times do |i|\n if a[s[i]] > 2\n ans[i] = \"B\"\n break\n end\n end\nend\nputs ans\n"}], "negative_code": [], "src_uid": "d126ef6b94e9ab55624cf7f2a96c7ed1"} {"nl": {"description": "During the hypnosis session, Nicholas suddenly remembered a positive integer $$$n$$$, which doesn't contain zeros in decimal notation. Soon, when he returned home, he got curious: what is the maximum number of digits that can be removed from the number so that the number becomes not prime, that is, either composite or equal to one?For some numbers doing so is impossible: for example, for number $$$53$$$ it's impossible to delete some of its digits to obtain a not prime integer. However, for all $$$n$$$ in the test cases of this problem, it's guaranteed that it's possible to delete some of their digits to obtain a not prime number.Note that you cannot remove all the digits from the number.A prime number is a number that has no divisors except one and itself. A composite is a number that has more than two divisors. $$$1$$$ is neither a prime nor a composite number.", "input_spec": "Each test contains multiple test cases. The first line contains one positive integer $$$t$$$ ($$$1 \\le t \\le 10^3$$$), denoting the number of test cases. Description of the test cases follows. The first line of each test case contains one positive integer $$$k$$$ ($$$1 \\le k \\le 50$$$) — the number of digits in the number. The second line of each test case contains a positive integer $$$n$$$, which doesn't contain zeros in decimal notation ($$$10^{k-1} \\le n < 10^{k}$$$). It is guaranteed that it is always possible to remove less than $$$k$$$ digits to make the number not prime. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^4$$$.", "output_spec": "For every test case, print two numbers in two lines. In the first line print the number of digits, that you have left in the number. In the second line print the digits left after all delitions. If there are multiple solutions, print any.", "sample_inputs": ["7\n3\n237\n5\n44444\n3\n221\n2\n35\n3\n773\n1\n4\n30\n626221626221626221626221626221"], "sample_outputs": ["2\n27\n1\n4\n1\n1\n2\n35\n2\n77\n1\n4\n1\n6"], "notes": "NoteIn the first test case, you can't delete $$$2$$$ digits from the number $$$237$$$, as all the numbers $$$2$$$, $$$3$$$, and $$$7$$$ are prime. However, you can delete $$$1$$$ digit, obtaining a number $$$27 = 3^3$$$.In the second test case, you can delete all digits except one, as $$$4 = 2^2$$$ is a composite number."}, "positive_code": [{"source_code": "require 'prime'\n\nt = gets.to_i\nt.times do\n\nk = gets.to_i\nn = gets.chomp.chars.map!(&:to_i)\n\nq = n.map.with_index { |x, i| x << 8 | i }\nwhile (x = q.shift)\n d = x & 0xff\n x >>= 8\n\n unless Prime.prime?(x)\n puts x.digits.size\n puts x\n break\n end\n\n n[d.succ..-1].each.with_index(d.succ) do |y, i|\n q << ((x * 10 + y) << 8 | i)\n end\nend\n\nend\n"}], "negative_code": [], "src_uid": "22c0489eec3d8e290fcbcf1aeb3bb66c"} {"nl": {"description": "Let's call the following process a transformation of a sequence of length $$$n$$$.If the sequence is empty, the process ends. Otherwise, append the greatest common divisor (GCD) of all the elements of the sequence to the result and remove one arbitrary element from the sequence. Thus, when the process ends, we have a sequence of $$$n$$$ integers: the greatest common divisors of all the elements in the sequence before each deletion.You are given an integer sequence $$$1, 2, \\dots, n$$$. Find the lexicographically maximum result of its transformation.A sequence $$$a_1, a_2, \\ldots, a_n$$$ is lexicographically larger than a sequence $$$b_1, b_2, \\ldots, b_n$$$, if there is an index $$$i$$$ such that $$$a_j = b_j$$$ for all $$$j < i$$$, and $$$a_i > b_i$$$.", "input_spec": "The first and only line of input contains one integer $$$n$$$ ($$$1\\le n\\le 10^6$$$).", "output_spec": "Output $$$n$$$ integers  — the lexicographically maximum result of the transformation.", "sample_inputs": ["3", "2", "1"], "sample_outputs": ["1 1 3", "1 2", "1"], "notes": "NoteIn the first sample the answer may be achieved this way: Append GCD$$$(1, 2, 3) = 1$$$, remove $$$2$$$. Append GCD$$$(1, 3) = 1$$$, remove $$$1$$$. Append GCD$$$(3) = 3$$$, remove $$$3$$$. We get the sequence $$$[1, 1, 3]$$$ as the result."}, "positive_code": [{"source_code": "# require 'prime'\n\ndef gcd(a, b)\n (b==0 ? a : gcd(b, a%b))\nend\n\ndef arygcd(ary)\n ans = ary[0]\n ary.each do |x|\n ans = gcd(ans, x)\n end\n ans\nend\n\nn = gets.to_i\n\ndef solve(ary)\n n = ary.size\n if n==1\n return [1]\n elsif n==2\n return [1, 2]\n elsif n==3\n return [1, 1, 3]\n end \n ans = []\n nary = []\n ary.each do |x|\n if x%2 != 0\n ans << 1\n else\n nary << x/2\n end\n end\n solve(nary).each do |x|\n ans << 2*x\n end\n ans\nend\n\nary = (1..n).to_a\nputs solve(ary).join(\" \")"}, {"source_code": "n = gets.to_i\nif n == 1\n puts 1\n exit\nend\nans = []\nt = Math.log2(n).floor-1\n\nt.times do |i|\n ans += [2**i]*((n/(2**i))-(n/(2**(i+1))))\nend\n\nif n/(2**t) == 3\n ans << 2**t << 2**t << 3*2**t\nelse\n ans << 2**t << 2*2**t\nend\n\nputs ans.join(\" \")\n"}, {"source_code": "n = gets.to_f\nx = Math.log(n,2)\n\ncount = 0\nexpon = 0\ncurr = 2**expon\n\n\nminicount = 0\n\nwhile (count < n-2)\n print \"#{curr} \"\n count += 1\n minicount += 1\n if minicount == (n/(curr*2)).round(0)\n minicount = 0\n expon += 1\n curr = 2**expon\n end\nend \n\nthisthing = 2**((x.floor)-1)\nif n==1\n puts 1\n exit\nend\nprint \"#{thisthing} \"\nputs \"#{(n.to_i/thisthing)*thisthing}\"\n"}], "negative_code": [{"source_code": "n = gets.to_f\nx = Math.log(n,2)\n\ncount = 0\nexpon = 0\ncurr = 2**expon\n\n\nminicount = 0\n\nwhile (count < n-2)\n print \"#{curr} \"\n count += 1\n minicount += 1\n if minicount == (n/(curr*2)).round(0)\n minicount = 0\n expon += 1\n curr = 2**expon\n end\nend \n\nthisthing = 2**((x.floor)-1)\nif n==1\n exit\nend\nprint \"#{thisthing} \"\nputs \"#{(n.to_i/thisthing)*thisthing}\"\n"}], "src_uid": "cadff0864835854f4f1e0234f2fd3edf"} {"nl": {"description": "An array $$$b$$$ of length $$$k$$$ is called good if its arithmetic mean is equal to $$$1$$$. More formally, if $$$$$$\\frac{b_1 + \\cdots + b_k}{k}=1.$$$$$$Note that the value $$$\\frac{b_1+\\cdots+b_k}{k}$$$ is not rounded up or down. For example, the array $$$[1,1,1,2]$$$ has an arithmetic mean of $$$1.25$$$, which is not equal to $$$1$$$.You are given an integer array $$$a$$$ of length $$$n$$$. In an operation, you can append a non-negative integer to the end of the array. What's the minimum number of operations required to make the array good?We have a proof that it is always possible with finitely many operations.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 50$$$) — the length of the initial array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1,\\ldots,a_n$$$ ($$$-10^4\\leq a_i \\leq 10^4$$$), the elements of the array.", "output_spec": "For each test case, output a single integer — the minimum number of non-negative integers you have to append to the array so that the arithmetic mean of the array will be exactly $$$1$$$.", "sample_inputs": ["4\n3\n1 1 1\n2\n1 2\n4\n8 4 6 2\n1\n-2"], "sample_outputs": ["0\n1\n16\n1"], "notes": "NoteIn the first test case, we don't need to add any element because the arithmetic mean of the array is already $$$1$$$, so the answer is $$$0$$$.In the second test case, the arithmetic mean is not $$$1$$$ initially so we need to add at least one more number. If we add $$$0$$$ then the arithmetic mean of the whole array becomes $$$1$$$, so the answer is $$$1$$$.In the third test case, the minimum number of elements that need to be added is $$$16$$$ since only non-negative integers can be added.In the fourth test case, we can add a single integer $$$4$$$. The arithmetic mean becomes $$$\\frac{-2+4}{2}$$$ which is equal to $$$1$$$."}, "positive_code": [{"source_code": "$\\ = \"\\n\"\r\ngets.to_i.times do\r\n n = gets.to_i\r\n a = gets.split.map &:to_i\r\n s = a.sum\r\n if s < n\r\n print 1\r\n else\r\n print s - n\r\n end\r\nend"}, {"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n a = gets.split.map &:to_i\r\n s = a.sum\r\n if s < n\r\n puts 1\r\n else\r\n puts s - n\r\n end\r\nend"}, {"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n a = gets.split.map &:to_i\r\n s = a.sum\r\n puts s < n ? 1 : s - n\r\nend"}, {"source_code": "t = Integer(gets.chomp)\r\nt.times{\r\n n = Integer(gets.chomp)\r\n a = gets.chomp.split().map!{|c| Integer(c)}\r\n \r\n som = a.sum()\r\n len = a.size()\r\n \r\n if(som < len)\r\n puts 1\r\n elsif(som == len)\r\n puts 0\r\n else\r\n puts som - len\r\n end\r\n}"}], "negative_code": [], "src_uid": "b5985b619652e606ac96554ecfb9346a"} {"nl": {"description": "Roma (a popular Russian name that means 'Roman') loves the Little Lvov Elephant's lucky numbers.Let us remind you that lucky numbers are positive integers whose decimal representation only contains lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.Roma's got n positive integers. He wonders, how many of those integers have not more than k lucky digits? Help him, write the program that solves the problem.", "input_spec": "The first line contains two integers n, k (1 ≤ n, k ≤ 100). The second line contains n integers ai (1 ≤ ai ≤ 109) — the numbers that Roma has. The numbers in the lines are separated by single spaces.", "output_spec": "In a single line print a single integer — the answer to the problem.", "sample_inputs": ["3 4\n1 2 4", "3 2\n447 44 77"], "sample_outputs": ["3", "2"], "notes": "NoteIn the first sample all numbers contain at most four lucky digits, so the answer is 3.In the second sample number 447 doesn't fit in, as it contains more than two lucky digits. All other numbers are fine, so the answer is 2."}, "positive_code": [{"source_code": "n,k=gets.split.map {|i| i.to_i}\n\na=gets.split.map {|i| i.to_i}\n\na.select! do |i|\n i.to_s.scan(/[47]/).length <=k\nend\n\nputs a.length"}, {"source_code": "n,k=gets.split.map(&:to_i)\na=gets.split.map(&:to_i)\na.select! {|i| i.to_s.scan(/[47]/).length <=k}\nputs a.length"}, {"source_code": "n, k = gets.split.map(&:to_i)\nputs gets.split.count{|x| x.count('47') <= k}\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\na = gets.split.map(&:chars)\nputs a.count{|x| x.count('4') + x.count('7') <= k}\n"}, {"source_code": "a=gets.chomp.split(\" \")\nb=gets.chomp.split(\" \")\nans=0\nmax=a[1].to_i\nb.each do |i|\nif i.count(\"4\")+i.count(\"7\")<=max\nans+=1\nend\nend\nputs \"#{ans}\""}, {"source_code": "k=gets.split[1].to_i\np gets.split.count{|x| x.count('47')<=k}"}, {"source_code": "n, k = gets.split.map &:to_i\na = gets.split.map &:to_i\nputs a.inject(0) { |s, x| x.to_s.split('').select { |d| d == '4' || d == '7'}.size <= k ? s + 1 : s }\n"}, {"source_code": "n,k=gets.split.map(&:to_i)\np gets.split.map{|x| x.scan(/4|7/).size<=k ? 1 : 0}.reduce(:+)"}, {"source_code": "n, k = gets.split.map(&:to_i)\np gets.split.map { |x|\n x.count('4') + x.count('7') <= k ? 1 : 0\n}.reduce(:+)\n"}, {"source_code": "k=gets.split[1].to_i\np gets.split.count{|x| x.count('47')<=k}\n"}, {"source_code": "def is_lucky?(n, k)\n while n > 0 do\n k-=1 if n%10 == 4 || n%10 == 7\n n/=10\n end\n k < 0 ? false : true\nend\n\nn, k = gets.split.map &:to_i\na = gets.split.map &:to_i\ncounter = 0\na.each {|el| counter += 1 if is_lucky?(el, k) }\n\nputs counter"}, {"source_code": "k=gets.split[1].to_i\np gets.split.count{|x| x.count('47')<=k}\n\n"}, {"source_code": "n, k = gets.chomp.split.map { |x| x.to_i }\na = gets.chomp.split\ncount = 0\nfor i in 0...n\n count += 1 if a[i].scan(/4|7/).count <= k\nend\nputs count"}, {"source_code": "k=gets.split[1].to_i\np gets.split.count{|x| x.count('47')<=k}\n\n"}, {"source_code": "BEGIN{\n\tdef get_line_int()\n\t\treturn gets.split.map(&:to_i)\n\tend\n\tdef count(x)\n\t\tcc = 0\n\t\tx.to_s.each_char do |c|\n\t\t\tif c == \"4\" or c == \"7\" then\n\t\t\t\tcc += 1\n\t\t\tend\n\t\tend\n\t\treturn cc\n\tend\n\tdef main()\n\t\tn, k = get_line_int\n\t\ta = get_line_int\n\t\tcc = 0\n\t\ta.each do |i|\n\t\t\tif count(i) <= k then\n\t\t\t\tcc += 1\n\t\t\tend\n\t\tend\n\t\tputs cc\n\tend\n\tTest_case = 1\n\tTest_case.times do\n\t\tmain()\n\tend\n}\n"}, {"source_code": "a = gets.split(\" \").collect{|x| x.to_i}\nn = a[0]\nk = a[1]\n\na = gets.split(\" \").collect{|x| x.to_i}\nresult = 0\n\na.each{|x| \n\tc = 0\n\twhile x > 0\n\t\tt = x % 10\n\t\tx /= 10\n\t\tif t == 4 || t == 7\n\t\t\tc += 1\n\t\tend\t\t\n\tend\n\tif c <= k\n\t\tresult += 1\n\tend\n}\n\nputs result"}, {"source_code": "k=gets.split[1].to_i\np gets.split.count{|x| x.count('47')<=k}\n\n"}, {"source_code": "n,k=gets.split.map{|s|s.to_i}\na=gets.split.map{|s|s.to_i}\nr=0\nn.times do |i|\n j=0\n while a[i] > 0 do\n j+=1 if [4,7].include?(a[i]%10)\n a[i] /= 10\n end\n r+=1 if j <= k\nend\nputs r\n"}, {"source_code": "n, k = gets.split(' ').map(&:to_i)\nputs gets.chomp.split(' ',n).map{|x| x.scan(/[47]/).count}.select{|x| x<=k}.count\n"}, {"source_code": "st = gets.split\nn = st[0].to_i\nk = st[1].to_i\n\nst = gets.split\nresult = 0\nn.times do |i|\n x = st[i].count(\"4\")\n y = st[i].count(\"7\")\n if x + y <= k then\n result += 1\n end\nend\n\nputs result"}, {"source_code": "k=gets.split[1].to_i\np gets.split.count{|x| x.count('47')<=k}\n\n"}, {"source_code": " n,k=gets.split.map(&:to_i)\n a=gets.split.map(&:to_s)\n ans=0\n a.each do |ch|\n\tg=0\n\tfor i in 0...ch.size do\n\t\tg+=1 if ch[i]=='4' or ch[i]=='7'\n\tend\n\tans+=1 if g<=k\n end\n puts ans\n\t\n\t\n\t\n"}, {"source_code": "k = gets.split[1].to_i\nputs gets.split.count{|a| a.count('47') <= k}"}, {"source_code": "# -*- encoding: utf-8 -*-\n\nn, k = gets.chomp.split(\" \").map{|n| n.to_i}\ndigits = gets.chomp.split(\" \")\n\nc = 0\n\ndigits.each do |a|\n c+=1 if a.count(\"4\") + a.count(\"7\") <= k\nend\n\nputs c\n\n"}, {"source_code": "k=gets.split[1].to_i\np gets.split.count{|x| x.count('47')<=k}\n\n"}, {"source_code": "n, k = STDIN.readline.split.map {|s| s.to_i }\nss = STDIN.readline.split\ncount = 0\nss.each do |s|\n\tcount += 1 if s.count('4') + s.count('7') <= k\nend\nputs count\n"}, {"source_code": "k=gets.split[1].to_i\np gets.split.count{|x| x.count('47')<=k}\n"}, {"source_code": "n,k=gets.chomp.split(' ').map(&:to_i)\nsum=n\n#puts sum\na=gets.chomp.split(' ').map(&:to_s)\nfor i in a do\n\tcount=0\n\tj=i.split('')\n\tj.each do |x|\n\t\tif x=='4' || x=='7'\n\t\t\tcount+=1\n\t\tend\n\tend\n\tif count>k\n\t\tsum-=1\n\tend\nend\nputs sum"}, {"source_code": "n,k = gets.split.map(&:to_i)\na = gets.split.map(&:to_s)\n\na.each do |x|\n counter = 0\n x.split(//).each do |t|\n counter+=1 if t == \"4\" || t == \"7\"\n end\n n-=1 if counter>k\nend\nputs n\n"}, {"source_code": "\n\nn,k = gets.chomp.split(\" \").collect! {|x| x.to_i}\nline = gets.chomp.split(\" \")\ns = 0\nline.each {|w|\n n = 0\n w.split(\"\").each {|t|\n if t ==\"4\" or t==\"7\"\n n+=1\n end\n }\n if n<=k\n s +=1\n end\n}\n\nputs s"}, {"source_code": "n,k = gets.split.map(&:to_i)\na = gets.chomp.split.map(&:to_s)\n\na.each do |x|\n counter = 0\n x.split(\"\").each do |t|\n counter+=1 if t == \"4\" || t == \"7\"\n end\n n-=1 if counter>k\nend\nputs n\n"}, {"source_code": "n,k = gets.split.map(&:to_i)\na = gets.chomp.split(' ')\n\na.each {|x|\n counter = 0\n x.split(\"\").each {|t|\n counter+=1 if t == \"4\" || t == \"7\"\n }\n n-=1 if counter>k\n}\nputs n\n"}, {"source_code": "n,k = gets.split.map(&:to_i)\na = gets.chomp.split(' ')\n\na.each do |x|\n counter = 0\n x.split(\"\").each do |t|\n counter+=1 if t == \"4\" || t == \"7\"\n end\n n-=1 if counter>k\nend\nputs n\n"}, {"source_code": "def run\n lines = ARGF.readlines\n\n while lines.length > 0\n ret = 0\n\n n, k = lines.shift.chomp.split(\" \").map {|x| x.to_i }\n vals = lines.shift.chomp.split(\" \")\n\n vals.each do |val|\n cnt = 0\n val.split(//).each do |c|\n cnt += 1 if c == '4' || c == '7'\n end\n\n ret += 1 if cnt <= k\n end\n\n puts ret\n end\nend\n\nrun if $0 == __FILE__\n"}, {"source_code": "k=gets.split[1].to_i\np gets.split.count{|x| x.count('47')<=k}\n\n"}, {"source_code": "a=gets.chomp.split(\" \")\nb=gets.chomp.split(\" \")\nans=0\nmax=a[1].to_i\nb.each do |i|\nif i.count(\"4\")+i.count(\"7\")<=max\nans+=1\nend\nend\nputs \"#{ans}\"\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\nas = gets.split\n\nans = 0\nas.each do |a|\n if a.count('4') + a.count('7') <= k\n ans += 1\n end\nend\n\nputs ans"}, {"source_code": "k=gets.split[1].to_i\np gets.split.count{|x| x.count('47')<=k}\n\n"}, {"source_code": "n, k = gets.chomp.split(/ /).map(&:to_i)\nlist = gets.chomp.split(/ /)\nans = 0\nlist.each do |num|\n lucky_digits = 0\n num.each_char do |d|\n lucky_digits += 1 if d == '4' || d == '7'\n end\n ans += 1 if lucky_digits <= k\nend\nputs ans\n"}, {"source_code": "a=gets.chomp.split(\" \")\nb=gets.chomp.split(\" \")\nans=0\nmax=a[1].to_i\nb.each do |i|\nif i.count(\"4\")+i.count(\"7\")<=max\nans+=1\nend\nend\nputs \"#{ans}\""}, {"source_code": "n,k=gets.split.map{|e| e.to_i}\nx=gets.split\nans=0\nx.each{|e|\n\tc1=e.chars.count{|e2|\n\t\t\te2==\"4\" || e2==\"7\"\n\t}\n\tans+=1 if c1<=k\n}\nputs ans\n\n"}, {"source_code": "require 'set'\nrequire 'rational'\nrequire 'mathn'\ninclude Math\n$stdin = File.open(\"./input.txt\", \"r\") if ENV[\"USER\"] == \"new\"\n# ====================================\n\ndef nice( number, k )\n count = 0\n number.to_s.split(//).each{|c|\n count+=1 if c == \"4\" || c == \"7\"\n }\n count <= k\nend\n\nn, k = gets.split.map &:to_i\nnums = gets.split.map &:to_i\n\nans = 0\nnums.each{|v|\n ans+=1 if nice v, k\n}\n\nprint ans"}, {"source_code": "k=gets.split[1].to_i\np gets.split.count{|x| x.count('47')<=k}\n\n"}, {"source_code": "n,k = gets.split(\" \").map(&:to_i)\narr = gets.split(\" \")\ncount = 0\narr.each { |e| \n\tcount += 1 if e.scan(/[47]/).length <= k\n}\nputs count"}, {"source_code": "a=gets.split.map &:to_i;b=gets.split.map &:to_i;k=0;a[0].times{|x|b[x].to_s.count('4')+b[x].to_s.count('7')<=a[1] ?(k+=1):(0)};puts k"}, {"source_code": "n,k = gets.chomp.split(\" \").collect! {|x| x.to_i}\nline = gets.chomp.split(\" \")\ns = 0\nline.each {|w|\n n = 0\n\tw.split(\"\").each {|t|\n\t if t ==\"4\" or t==\"7\"\n\t\tn+=1\n\t end\n\t}\n\tif n<=k\n\t\ts +=1\n\tend\n}\n\nputs s"}, {"source_code": "n,k=gets.split.map(&:to_i)\na=gets.split.select {|i| i.scan(/[47]/).length <=k}\nputs a.length"}, {"source_code": "n,k=gets.split.map(&:to_i)\na=gets.split.select {|i| i.to_s.scan(/[47]/).length <=k}\nputs a.length"}], "negative_code": [{"source_code": " n,k=gets.split.map(&:to_i)\n a=gets.split.map(&:to_s)\n ans=0\n a.each do |ch|\n\tg=0\n\tfor i in 0...ch.size do\n\t\tg+=1 if ch[i]==4 or ch[i]==7\n\tend\n\tans+=1 if g<=k\n end\n puts ans\n\t\n\t\n\t\n"}, {"source_code": "n, k = gets.chomp.split(/ /).map(&:to_i)\nlist = gets.chomp.split(/ /)\nans = 0\nlist.each do |num|\n ans += 1 if num.length <= k\nend\nputs ans\n"}], "src_uid": "6bb26991c9ea70e51a6bda5653de8131"} {"nl": {"description": "Unfortunately, Vasya can only sum pairs of integers (a, b), such that for any decimal place at least one number has digit 0 in this place. For example, Vasya can sum numbers 505 and 50, but he cannot sum 1 and 4.Vasya has a set of k distinct non-negative integers d1, d2, ..., dk.Vasya wants to choose some integers from this set so that he could sum any two chosen numbers. What maximal number of integers can he choose in the required manner?", "input_spec": "The first input line contains integer k (1 ≤ k ≤ 100) — the number of integers. The second line contains k distinct space-separated integers d1, d2, ..., dk (0 ≤ di ≤ 100).", "output_spec": "In the first line print a single integer n the maximum number of the chosen integers. In the second line print n distinct non-negative integers — the required integers. If there are multiple solutions, print any of them. You can print the numbers in any order.", "sample_inputs": ["4\n100 10 1 0", "3\n2 70 3"], "sample_outputs": ["4\n0 1 10 100", "2\n2 70"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/ruby1.9\n\nn = STDIN.readline.to_i\nnums = STDIN.readline.split.collect {|s| s.to_i }\n\nzeros, one, ten, double, hundred = [0, 0, 0, 0, 0]\nnums.each do |num|\n\tif num == 0\n\t\tzeros += 1\n\telsif num == 100\n\t\thundred = 1\n\telsif num%10 == 0\n\t\tten = num\n\telsif num < 10\n\t\tone = num\n\telse\n\t\tdouble = num\n\tend\nend\n\nresult = zeros.times.collect { 0 }\nif one+ten+double > 0\n\tif one > 0\n\t\tresult.push(one)\n\t\tif ten > 0\n\t\t\tresult.push(ten)\n\t\tend\n\telsif ten > 0\n\t\tresult.push(ten)\n\telse\n\t\tresult.push(double)\n\tend\nend\nif hundred > 0\n\tresult.push(100)\nend\n\nputs result.length\nputs result.join(' ')\n"}, {"source_code": "n=gets.to_i\na=gets.chomp.split.map(&:to_i)\nb,c=[],[nil,nil,nil,nil,nil]\n\na.each do |i|\n\tif i==0\n\t\tc[0]=1\n\telsif i==100\n\t\tc[1]=1\n\telsif i%10==0\n\t\tc[2]=i\n\telsif i<10\n\t\tc[3]=i\n\telse\n\t\tc[4]=i\n\tend\nend\nb<<0 if c[0]\nb<<100 if c[1]\nb< 0 && a <= 9\n\t\tone = a \n\telsif !two && a < 100 && (a % 10) == 0\t\t\n\t\ttwo = a\n\telsif !three && a == 100\n\t\tthree = a\n\tend\nend\nres = [zero,one,two,three].compact\nif res.empty?\n\tres = [arr.first]\nelsif !one && !two\n\t val = arr.detect{|a| a.to_s.length == 2}\n\t res << val if val\nend\n\n\nputs res.count\nputs res.join(' ')"}, {"source_code": "def get_bucket n\n s = 0\n if n%10 != 0\n s += 1\n end\n if (n/10)%10 != 0\n s = s + 2\n end\n if n==100\n s = 4\n end\n return s\nend\n\nk = gets.to_i\nb = [0]*5\nnn = [-1]*5\narr = gets.chomp.split(' ').collect {|x| x.to_i}\narr.each { |v|\n b[get_bucket(v)] = 1\n nn[get_bucket(v)] = v\n}\n\nm1 = b[0] + b[1] + b[2] + b[4]\nm2 = b[3] + b[4] + b[0]\n\nputs [m1,m2].max\n\nif m1 > m2\n r = nn[0..2] + [nn[4]]\nelse\n r = nn[3..4] + [nn[0]]\nend\n\nputs r.keep_if {|x| x!=-1}.join \" \"\n\n\n"}, {"source_code": "# 305A\n# What a retarded task statement! Nothing can be understood...\n\ngets\na = gets.chomp.split\n\nr = []\nq = a.find { |t| t == '0' }\nr << q if q\nw = a.find { |t| t == '100' }\nr << w if w\nz = a.find { |t| t.size == 1 && t != '0' }\nr << z if z\nx = a.find { |t| t.size == 2 && t[1] == '0' }\nr << x if x\nunless z || x\n y = a.find { |t| t.size == 2 }\n r << y if y\nend\n\nputs r.size\nputs r * ' ' unless r.empty?"}, {"source_code": "ans=[]\nrest=[]\nmultiple10=false\none_digit=false\nk=gets.to_i\nd=gets.split.map(&:to_i)\nd.each{|num|\n if num==0\n \tans< 0 && a <= 9\n\t\tone = a \n\telsif !two && a < 100 && (a % 10) == 0\t\t\n\t\ttwo = a\n\telsif !three && a == 100\n\t\tthree = a\n\tend\nend\nres = [zero,one,two,three].compact\n\nputs res.count\nputs res.join(' ')"}, {"source_code": "n = gets.to_i\narr = gets.split(\" \").map(&:to_i)\nzero = nil\none = nil\ntwo = nil\nthree = nil\n\narr.each do |a|\n\tif !zero && a == 0\n\t\tzero = a \t\t\n\telsif !one && a > 0 && a <= 9\n\t\tone = a \n\telsif !two && a < 100 && (a % 10) == 0\t\t\n\t\ttwo = a\n\telsif !three && a == 100\n\t\tthree = a\n\tend\nend\nres = [zero,one,two,three].compact\nif res.empty?\n\tres = [arr.first]\nend\n\nputs res.count\nputs res.join(' ')"}, {"source_code": "# 305A\n\ngets\na = gets.chomp.split\nif a.size == 1\n puts 1\n puts a * ' '\n exit\nend\nr = []\nz = a.find { |t| t.size == 1 && t != '0' }\nr << z if z\nn = a.find { |t| t == '0' }\nr << n if n\nu = a.find { |t| t =~ /^\\d0$/ }\nr << u if u\nn = a.find { |t| t == '100' }\nr << n if n\nif n && z && !u\n u = a.find { |t| t.size == 2 }\n r << u if u\nend\n\nif r.empty?\n r << a[0]\nend\nputs r.size\nputs r * ' ' unless r.empty?"}, {"source_code": "# 305A\n\ngets\na = gets.chomp.split\nr = []\nn = a.find { |t| t.size == 1 && t != '0' }\nr << n if n\nn = a.find { |t| t == '0' }\nr << n if n\nn = a.find { |t| t =~ /^\\d0$/ }\nr << n if n\nn = a.find { |t| t == '100' }\nr << n if n\n\nputs r.size\nputs r * ' ' unless r.empty?"}, {"source_code": "# 305A\n\ngets\na = gets.chomp.split\nif a.size == 1\n puts 1\n puts a * ' '\n exit\nend\nr = []\nn = a.find { |t| t.size == 1 && t != '0' }\nr << n if n\nn = a.find { |t| t == '0' }\nr << n if n\nu = a.find { |t| t =~ /^\\d0$/ }\nr << u if u\nn = a.find { |t| t == '100' }\nr << n if n\nif n && !u\n u = a.find { |t| t.size == 2 }\n r << u if u\nend\n\nif r.empty?\n r << a[0]\nend\nputs r.size\nputs r * ' ' unless r.empty?"}, {"source_code": "# 305A\n\ngets\na = gets.chomp.split\nif a.size == 1\n puts 1\n puts a * ' '\nend\nr = []\nn = a.find { |t| t.size == 1 && t != '0' }\nr << n if n\nn = a.find { |t| t == '0' }\nr << n if n\nn = a.find { |t| t =~ /^\\d0$/ }\nr << n if n\nn = a.find { |t| t == '100' }\nr << n if n\n\nputs r.size\nputs r * ' ' unless r.empty?"}, {"source_code": "# 305A\n\ngets\na = gets.chomp.split\nif a.size == 1\n puts 1\n puts a * ' '\n exit\nend\nr = []\nn = a.find { |t| t.size == 1 && t != '0' }\nr << n if n\nn = a.find { |t| t == '0' }\nr << n if n\nn = a.find { |t| t =~ /^\\d0$/ }\nr << n if n\nn = a.find { |t| t == '100' }\nr << n if n\n\nif r.empty?\n r << a[0]\nend\nputs r.size\nputs r * ' ' unless r.empty?"}, {"source_code": "# 305A\n\ngets\na = gets.chomp.split\nif a.size == 1\n puts 1\n puts a * ' '\n exit\nend\nr = []\nz = a.find { |t| t.size == 1 && t != '0' }\nr << z if z\nw = a.find { |t| t == '0' }\nr << w if w\nu = a.find { |t| t =~ /^\\d0$/ }\nr << u if u\nn = a.find { |t| t == '100' }\nr << n if n\nif (n && w && !u) || r == ['0']\n u = a.find { |t| t.size == 2 }\n r << u if u\nend\n\n\nif r.empty?\n r << a[0]\nend\nputs r.size\nputs r * ' ' unless r.empty?"}, {"source_code": "# 305A\n\ngets\na = gets.chomp.split\nif a.size == 1\n puts 1\n puts a * ' '\n exit\nend\nr = []\nz = a.find { |t| t.size == 1 && t != '0' }\nr << z if z\nw = a.find { |t| t == '0' }\nr << w if w\nu = a.find { |t| t =~ /^\\d0$/ }\nr << u if u\nn = a.find { |t| t == '100' }\nr << n if n\nif n && w && !u\n u = a.find { |t| t.size == 2 }\n r << u if u\nend\n\nif r.empty?\n r << a[0]\nend\nputs r.size\nputs r * ' ' unless r.empty?"}, {"source_code": "# 305A\n\ngets\na = gets.chomp.split\nif a.size == 1\n puts 1\n puts a * ' '\n exit\nend\nr = []\nn = a.find { |t| t.size == 1 && t != '0' }\nr << n if n\nn = a.find { |t| t == '0' }\nr << n if n\nn = a.find { |t| t =~ /^\\d0$/ }\nr << n if n\nn = a.find { |t| t == '100' }\nr << n if n\n\nputs r.size\nputs r * ' ' unless r.empty?"}, {"source_code": "# 305A\n\ngets\na, b = gets.chomp.split.partition { |token| token.include? '0' }\nif a.empty?\n puts 0\nelse\n a << b[0] unless b.empty?\n puts a.size\n puts a * ' '\nend"}, {"source_code": "ans=[]\ndiff=[]\nk=gets.to_i\nd=gets.split.map(&:to_i)\nd.each{|num|\n if num.to_s.count(?0)>0\n ans<0\n ans<1\nloop do\nif a[i..i+b-1]!=a[i..i+b-1].reverse\ntr=1\nbreak\nelse\ni+=b\nend\nbreak if i==l\nend\nend\n\nif tr==1\nputs \"NO\"\nelse\nputs \"YES\"\nend\nend\n"}, {"source_code": "a=gets.chomp\nb=gets.chomp.to_i\nl=a.length\nif l%b!=0\nputs \"NO\"\nelse\ntr=0\ni=0\nb=l/b\nif b>1\nloop do\nif a[i..i+b-1]!=a[i..i+b-1].reverse\ntr=1\nbreak\nelse\ni+=b\nend\nbreak if i==l\nend\nend\n\nif tr==1\nputs \"NO\"\nelse\nputs \"YES\"\nend\nend\n\n"}, {"source_code": "s=gets.chomp\nn=gets.to_i\n\nif s.size%n!=0 then\n\tputs \"NO\"\nelse\n\tm=s.size/n\n\tans=\"YES\"\n\ts.chars.to_a.each_slice(m).each{|e|\n\t\tif e!=e.reverse then\n\t\t\tans=\"NO\"\n\t\tend\n\t}\n\tputs ans\nend\n\n"}, {"source_code": "def valid_fax(fax, num_pals)\n\n word_len = fax.size / num_pals\n \n ans = false\n if fax.size % num_pals == 0\n ans = fax.each_slice(word_len).all? { |wrd| wrd == wrd.reverse }\n end\n ans\nend\n\n\nwords = gets().chomp.chars\nlen = gets().strip\nif valid_fax(words,len.to_i) == true\n puts 'YES'\nelse\n puts 'NO'\nend\n\n"}, {"source_code": "#!/usr/bin/env ruby\n# -*- coding: utf-8 -*-\n\n# a.rb\n\ndef is_palindrome(s)\n s.size.times do |i|\n return false if s[i] != s[s.size - i - 1]\n end\n return true\nend\n\ndef ok(s, k)\n return false if s.size % k != 0\n n = s.size / k\n k.times do |i|\n return false if !is_palindrome(s[n * i, n])\n end\n return true\nend\n\ndef main()\n s = gets.chomp\n k = gets.to_i\n \n puts ok(s, k) ? \"YES\" : \"NO\"\nend\n\n\nif __FILE__ == $0 then\n main()\nend\n"}, {"source_code": "s = gets.chomp\nk = gets.to_i\nif s.size%k != 0\n puts \"NO\"\n exit\nend\nlen = s.size/k\n(0...s.size).step(len).each do |i|\n if s[i,len] != s[i,len].reverse\n puts \"NO\"\n exit\n end \nend\nputs \"YES\"\n"}, {"source_code": "s = gets.chomp.chars\nk = gets.to_i\nif s.size % k == 0\n ans = s.each_slice(s.size / k).all?{|x| x == x.reverse}\nelse\n ans = false\nend\nputs ans ? 'YES' : 'NO'\n"}, {"source_code": "s = gets.chomp.chars\nk = gets.to_i\na = []\nvalid = false\ns.size.times do |i|\n a << s[i]\n if a == a.reverse\n start = a.size\n c = 1\n valid = true\n (k - 1).times do\n t = s[start...start + a.size]\n if start + a.size > s.size || t != t.reverse\n valid = false\n break\n end\n start += a.size\n end\n valid = false if a.size * k != s.size\n break if valid\n end\nend\nputs valid ? 'YES' : 'NO'\n"}, {"source_code": "#!ruby\n\ns = gets.chomp\nk = gets.chomp.to_i\n\nul = s.length/k\nif ul*k != s.length\n puts \"NO\"\n exit\nend\n\n0.upto(k-1) do |i|\n ss = s[ul*i..ul*(i+1)-1]\n if ss != ss.reverse\n puts \"NO\"\n exit\n end\nend\nputs \"YES\"\nexit\n"}, {"source_code": "#!/usr/bin/env ruby\ns=gets.chomp\nk=gets.to_i\nputs s.size%k==0 && k.times.all?{|i|t=s[s.size/k*i,s.size/k];t==t.reverse} ? :YES : :NO"}], "negative_code": [{"source_code": "# http://codeforces.com/contest/548/problem/A\ndef palindrome?(a)\n a == a.reverse\nend\n\nchars = gets.chomp.chars\nk = gets.chomp.to_i\n\nmessages = chars.each_slice(chars.length / k).to_a\nputs messages.all? { |m| palindrome?(m) } ? \"YES\" : \"NO\"\n"}, {"source_code": "\nstr = gets.chomp;\nn = gets.to_i;\nans = 0;\nans = str.chars.each_slice(str.length / n).all?{|x| x == x.reverse} if (str.length % n == 0);\nputs ans ? \"YES\\n\":\"NO\\n\";"}, {"source_code": "\nstr = gets.chomp;\nn = gets.to_i;\nans = 0;\nans = str.chars.each_slice(str.length / n).all?{|x| x == x.reverse} if (str.length % n == 0);\nputs ans == 1 ? \"YES\\n\":\"NO\\n\";\n"}, {"source_code": "str = gets.chomp;\nn = gets.to_i;\nif (str.length / n > 0)\n arr = str.chars.each_slice(str.length / n).map(&:join);\n puts arr.all?{|x| x.length == arr[0].length && x == x.reverse} ? \"YES\":\"NO\";\nelse\n puts \"NO\\n\";\nend\n"}, {"source_code": "str = gets.chomp;\nn = gets.to_i;\nif (str.length / n > 0)\n arr = str.chars.each_slice(str.length / n).map(&:join);\n puts arr.all?{|x| x.length == arr[0].length && x = x.reverse} ? \"YES\":\"NO\";\nelse\n puts \"NO\\n\";\nend\n"}, {"source_code": "string = gets.chomp\nk = gets.chomp.to_i\nflag = true\n\ndef palindrome?(str)\n\tstr == str.reverse\nend\n\nparts = []\nnowStart = 0\n\nuntil parts.length == k - 1\n\tparts << string.slice(nowStart, string.length / k)\n\tnowStart += string.length / k\n\n\t#puts nowStart\nend\n\nparts << string.slice(nowStart, string.length - nowStart)\n\n=begin\nparts.each do |partString|\n\tputs \"partString : #{partString}\"\nend\n=end\n\nparts.each do |partString|\n\tflag = false if !palindrome?(partString)\n\tflag = false if partString.length != k\nend\n\nputs \"YES\" if flag\nputs \"NO\" if !flag"}, {"source_code": "string = gets.chomp\nk = gets.chomp.to_i\nflag = true\n\ndef palindrome?(str)\n\tstr == str.reverse\nend\n\nparts = []\nnowStart = 0\n\nuntil parts.length == k\n\tparts << string.slice(nowStart, string.length / k)\n\tnowStart += string.length / k\n\n\t#puts nowStart\nend\n\nparts.each do |partString|\n\tflag = false if !palindrome?(partString)\nend\n\nputs \"YES\" if flag\nputs \"NO\" if !flag"}, {"source_code": "string = gets.chomp\nk = gets.chomp.to_i\nflag = true\n\ndef palindrome?(str)\n\tstr == str.reverse\nend\n\nparts = []\nnowStart = 0\n\nuntil parts.length == k\n\tparts << string.slice(nowStart, string.length / k)\n\tnowStart += string.length / k\n\n\tputs nowStart\nend\n\nparts.each do |partString|\n\tflag = false if !palindrome?(partString)\nend\n\nputs \"YES\" if flag\nputs \"NO\" if !flag"}, {"source_code": "string = gets.chomp\nk = gets.chomp.to_i\nflag = true\n\ndef palindrome?(str)\n\tstr == str.reverse\nend\n\nparts = []\nnowStart = 0\n\nuntil parts.length == k\n\tparts << string.slice(nowStart, string.length / k)\nend\n\nparts.each do |partString|\n\tflag = false if !palindrome? partString\nend\n\nputs \"YES\" if flag\nputs \"NO\" if !flag"}, {"source_code": "string = gets.chomp\nk = gets.chomp.to_i\nflag = true\n\ndef palindrome?(str)\n\tstr == str.reverse\nend\n\nparts = []\nnowStart = 0\n\nuntil parts.length == k\n\tparts << string.slice(nowStart, string.length / k)\n\tnowStart += string.length / k\n\n\t#puts nowStart\nend\n\nparts.each do |partString|\n\tflag = false if !palindrome? partString\nend\n\nputs \"YES\" if flag\nputs \"NO\" if !flag"}, {"source_code": "a=gets.chomp\nb=gets.chomp.to_i\nl=a.length\nif l%b!=0\nputs \"NO\"\nelse\ntr=0\ni=0\nif b>1\nb=l/b\nloop do\nif a[i..i+b-1]!=a[i..i+b-1].reverse\ntr=1\nbreak\nelse\ni+=b\nend\nbreak if i==l\nend\nend\n\nif tr==1\nputs \"NO\"\nelse\nputs \"YES\"\nend\nend"}, {"source_code": "\n\ndef is_pal(word, word2)\n word == word2.reverse\nend\n\ndef valid_fax(fax, word_len)\n\n #puts \"fax size = #{fax.size-1}, word_len = #{word_len-1} mod #{(fax.size-1 % word_len-1)}\"\n if ((fax.size-1) % (word_len-1)) != 0\n #puts \"here\"\n return false\n end\n\n i = 0\n puts fax.size\n while i < fax.size-1\n #puts \"Testing #{fax[i,word_len]} and #{fax[i+word_len, word_len]}\"\n unless is_pal(fax[i, word_len], fax[i+word_len, word_len])\n return false\n end\n i += word_len * 2\n end\n return true\nend\n\nwords = gets()\nlen = gets()\nif valid_fax(words,len.to_i + 1)\n puts 'YES'\nelse\n puts 'NO'\nend\n\n"}, {"source_code": "\n\ndef is_pal(word)\n word == word.reverse\nend\n\ndef valid_fax(fax, num_pals)\n\n #puts \"Fax.size #{fax.size}, number of pals #{num_pals}\"\n word_len = (fax.size) / num_pals\n #puts \"pals are of length #{word_len}, size = #{fax.size}\"\n\n #puts \"#{word_len}\"\n if word_len == 0\n return false\n end\n\n if (fax.size)%word_len != 0\n return false\n end\n\n fax.each_slice(word_len) do |wrd|\n return false unless is_pal(wrd)\n end\n return true\nend\n\nwords = gets().strip.chars\nlen = gets().strip\nif valid_fax(words,len.to_i)\n puts 'YES'\nelse\n puts 'NO'\nend\n\n"}, {"source_code": "def valid_fax(fax, num_pals)\n\n word_len = (fax.size) / num_pals\n\n #puts \"#{word_len}\"\n if word_len == 0\n return false\n end\n ans = false\n if fax.size % word_len == 0\n ans = fax.each_slice(word_len).all? do |wrd|\n wrd == wrd.reverse\n end\n end\n ans\nend\n\n\nwords = gets().chomp.chars\nlen = gets().strip\nif valid_fax(words,len.to_i) == true\n puts 'YES'\nelse\n puts 'NO'\nend\n\n"}, {"source_code": "\ndef valid_fax(fax, num_pals)\n\n word_len = (fax.size) / num_pals\n\n #puts \"#{word_len}\"\n if word_len == 0\n return false\n end\n if fax.size % word_len != 0\n return false\n end\n\n return fax.each_slice(word_len).all? do |wrd|\n wrd == wrd.reverse\n end\nend\n\nwords = gets().strip.chars\nlen = gets().strip\nif valid_fax(words,len.to_i)\n puts 'YES'\nelse\n puts 'NO'\nend\n\n"}, {"source_code": "\ndef valid_fax(fax, num_pals)\n\n word_len = (fax.size) / num_pals\n\n #puts \"#{word_len}\"\n if word_len == 0\n return false\n end\n if fax.size % word_len != 0\n return false\n end\n\n ! fax.each_slice(word_len).any? do |wrd|\n puts wrd\n wrd != wrd.reverse\n end\nend\n\nwords = gets().strip.chars\nlen = gets().strip\nif valid_fax(words,len.to_i)\n puts 'YES'\nelse\n puts 'NO'\nend\n\n"}, {"source_code": "\n\ndef is_pal(word, word2)\n word == word2.reverse\nend\n\ndef valid_fax(fax, word_len)\n\n #puts \"fax size = #{fax.size-1}, word_len = #{word_len-1} mod #{(fax.size-1 % word_len-1)}\"\n if ((fax.size-1) % (word_len-1)) != 0\n #puts \"here\"\n return false\n end\n\n i = 0\n #puts fax.size\n while i < fax.size-1\n #puts \"Testing #{fax[i,word_len]} and #{fax[i+word_len, word_len]}\"\n unless is_pal(fax[i, word_len], fax[i+word_len, word_len])\n return false\n end\n i += word_len * 2\n end\n return true\nend\n\nwords = gets()\nlen = gets()\nif valid_fax(words,len.to_i + 1)\n puts 'YES'\nelse\n puts 'NO'\nend\n\n"}, {"source_code": "\ndef valid_fax(fax, num_pals)\n\n word_len = (fax.size) / num_pals\n\n #puts \"#{word_len}\"\n if word_len == 0\n return false\n end\n if fax.size % word_len != 0\n return false\n end\n\n return fax.each_slice(word_len).all? do |wrd|\n wrd == wrd.reverse\n end\nend\n\nwords = gets().chomp.chars\nlen = gets().strip\nif valid_fax(words,len.to_i)\n puts 'YES'\nelse\n puts 'NO'\nend\n\n"}, {"source_code": "\n\ndef is_pal(word)\n word == word.reverse\nend\n\ndef valid_fax(fax, num_pals)\n\n word_len = fax.size / num_pals\n #puts \"pals are of length #{word_len}, size = #{fax.size-1}\"\n if (fax.size-1)%word_len != 0\n return false\n end\n\n if word_len == 1\n return true\n end\n\n i = 0\n #puts fax.size\n while i < fax.size-1\n #puts \"Testing #{fax[i,word_len]}\"\n unless is_pal(fax[i, word_len])\n return false\n end\n i += word_len * 2\n end\n return true\nend\n\nwords = gets()\nlen = gets()\nif valid_fax(words,len.to_i)\n puts 'YES'\nelse\n puts 'NO'\nend\n\n"}, {"source_code": "\n\ndef is_pal(word)\n word == word.reverse\nend\n\ndef valid_fax(fax, num_pals)\n\n #puts \"Fax.size #{fax.size}, number of pals #{num_pals}\"\n word_len = (fax.size) / num_pals\n #puts \"pals are of length #{word_len}, size = #{fax.size}\"\n\n #puts \"#{word_len}\"\n if word_len == 0\n return false\n end\n\n if word_len == 1\n return true\n end\n if (fax.size)%word_len != 0\n return false\n end\n\n fax.each_slice(word_len) do |wrd|\n return false if !is_pal(wrd)\n end\n return true\nend\n\nwords = gets().strip.chars\nlen = gets().strip\nif valid_fax(words,len.to_i)\n puts 'YES'\nelse\n puts 'NO'\nend\n\n"}, {"source_code": "\n\ndef is_pal(word, word2)\n word == word2.reverse\nend\n\ndef valid_fax(fax, word_len)\n\n if fax%word_len != 0\n return false\n end\n\n i = 0\n puts fax.size\n while i < fax.size-1\n puts \"Testing #{fax[i,word_len]} and #{fax[i+word_len, word_len]}\"\n unless is_pal(fax[i, word_len], fax[i+word_len, word_len])\n return false\n end\n i += word_len * 2\n end\n return true\nend\n\nwords = gets()\nlen = gets()\nif valid_fax(words,len.to_i + 1)\n puts 'YES'\nelse\n puts 'NO'\nend\n\n"}, {"source_code": "\n\ndef is_pal(word)\n word == word.reverse\nend\n\ndef valid_fax(fax, num_pals)\n\n word_len = fax.size / num_pals\n\n if word_len == 1\n return true\n end\n\n #puts \"pals are of length #{word_len}, size = #{fax.size-1}\"\n if (fax.size-1)%word_len != 0\n return false\n end\n\n \n i = 0\n #puts fax.size\n while i < fax.size-1\n #puts \"Testing #{fax[i,word_len]}\"\n unless is_pal(fax[i, word_len])\n return false\n end\n i += word_len * 2\n end\n return true\nend\n\nwords = gets()\nlen = gets()\nif valid_fax(words,len.to_i)\n puts 'YES'\nelse\n puts 'NO'\nend\n\n"}, {"source_code": "\n\ndef is_pal(word)\n word == word.reverse\nend\n\ndef valid_fax(fax, num_pals)\n\n word_len = fax.size / num_pals\n #puts \"pals are of length #{word_len}, size = #{fax.size-1}\"\n if (fax.size-1)%word_len != 0\n return false\n end\n\n i = 0\n #puts fax.size\n while i < fax.size-1\n #puts \"Testing #{fax[i,word_len]}\"\n unless is_pal(fax[i, word_len])\n return false\n end\n i += word_len * 2\n end\n return true\nend\n\nwords = gets()\nlen = gets()\nif valid_fax(words,len.to_i)\n puts 'YES'\nelse\n puts 'NO'\nend\n\n"}, {"source_code": "\n\ndef is_pal(word, word2)\n word == word2.reverse\nend\n\ndef valid_fax(fax, word_len)\n\n puts \"fax size = #{fax.size-1}, word_len = #{word_len-1} mod #{(fax.size-1 % word_len-1)}\"\n if ((fax.size-1) % (word_len-1)) != 0\n puts \"here\"\n return false\n end\n\n i = 0\n puts fax.size\n while i < fax.size-1\n #puts \"Testing #{fax[i,word_len]} and #{fax[i+word_len, word_len]}\"\n unless is_pal(fax[i, word_len], fax[i+word_len, word_len])\n return false\n end\n i += word_len * 2\n end\n return true\nend\n\nwords = gets()\nlen = gets()\nif valid_fax(words,len.to_i + 1)\n puts 'YES'\nelse\n puts 'NO'\nend\n\n"}, {"source_code": "\n\ndef is_pal(word)\n word == word.reverse\nend\n\ndef valid_fax(fax, num_pals)\n\n #puts \"Fax.size #{fax.size-1}, number of pals #{num_pals}\"\n word_len = (fax.size) / num_pals\n #puts \"pals are of length #{word_len}, size = #{fax.size-1}\"\n\n #puts \"#{word_len}\"\n if word_len == 0\n return false\n end\n if word_len == 1\n return true\n end\n if (fax.size)%word_len != 0\n return false\n end\n\n fax.each_slice(word_len) do |wrd|\n return false unless is_pal(wrd)\n end\n return true\nend\n\nwords = gets()\nlen = gets()\nif valid_fax(words,len.to_i)\n puts 'YES'\nelse\n puts 'NO'\nend\n\n"}, {"source_code": "\ndef valid_fax(fax, num_pals)\n\n word_len = (fax.size) / num_pals\n\n #puts \"#{word_len}\"\n if word_len == 0\n return false\n end\n if fax.size % word_len != 0\n return false\n end\n\n return fax.each_slice(word_len).all? do |wrd|\n wrd == wrd.reverse\n end\nend\n\nwords = gets().chomp.chars\nlen = gets().strip\nif valid_fax(words,len.to_i) == true\n puts 'YES'\nelse\n puts 'NO'\nend\n\n"}, {"source_code": "\n\ndef is_pal(word)\n word == word.reverse\nend\n\ndef valid_fax(fax, num_pals)\n\n word_len = fax.size / num_pals\n #puts \"pals are of length #{word_len}\"\n\n i = 0\n #puts fax.size\n while i < fax.size-1\n #puts \"Testing #{fax[i,word_len]}\"\n unless is_pal(fax[i, word_len])\n return false\n end\n i += word_len * 2\n end\n return true\nend\n\nwords = gets()\nlen = gets()\nif valid_fax(words,len.to_i)\n puts 'YES'\nelse\n puts 'NO'\nend\n\n"}, {"source_code": "\ndef valid_fax(fax, num_pals)\n\n #puts \"Fax.size #{fax.size}, number of pals #{num_pals}\"\n word_len = (fax.size) / num_pals\n #puts \"pals are of length #{word_len}, size = #{fax.size}\"\n\n #puts \"#{word_len}\"\n if word_len == 0\n return false\n end\n\n if word_len == 1\n return true\n end\n if fax.size % word_len != 0\n return false\n end\n\n ! fax.each_slice(word_len).any? do |wrd|\n wrd != wrd.reverse\n end\nend\n\nwords = gets().strip.chars\nlen = gets().strip\nif valid_fax(words,len.to_i)\n puts 'YES'\nelse\n puts 'NO'\nend\n\n"}, {"source_code": "\n\ndef is_pal(word)\n word == word.reverse\nend\n\ndef valid_fax(fax, num_pals)\n\n #puts \"Fax.size #{fax.size-1}, number of pals #{num_pals}\"\n word_len = (fax.size-1) / num_pals\n #puts \"pals are of length #{word_len}, size = #{fax.size-1}\"\n\n #puts \"#{word_len}\"\n if word_len == 0\n return false\n end\n if word_len == 1\n return true\n end\n if (fax.size-1)%word_len != 0\n return false\n end\n\n\n i = 0\n #puts fax.size\n while i < fax.size-1\n #puts \"Testing #{fax[i,word_len]}\"\n unless is_pal(fax[i, word_len])\n return false\n end\n i += word_len * 2\n end\n return true\nend\n\nwords = gets()\nlen = gets()\nif valid_fax(words,len.to_i)\n puts 'YES'\nelse\n puts 'NO'\nend\n\n"}, {"source_code": "#!/usr/bin/env ruby\n# -*- coding: utf-8 -*-\n\n# a.rb\n\ndef is_palindrome(s)\n return false if s.size == 1\n s.size.times do |i|\n return false if s[i] != s[s.size - i - 1]\n end\n return true\nend\n\ndef ok(s, k)\n return false if s.size % k != 0\n n = s.size / k\n k.times do |i|\n return false if !is_palindrome(s[n * i, n])\n end\n return true\nend\n\ndef main()\n s = gets.chomp\n k = gets.to_i\n \n puts ok(s, k) ? \"YES\" : \"NO\"\nend\n\n\nif __FILE__ == $0 then\n main()\nend\n"}, {"source_code": "#!/usr/bin/env ruby\n# -*- coding: utf-8 -*-\n\n# a.rb\n\ndef is_palindrome(s)\n return false if s.size == 1\n s.size.times do |i|\n return false if s[i] != s[s.size - i - 1]\n end\n return true\nend\n\ndef ok(s, k)\n if s == \"\" && k == 0 then\n return true\n end\n if (s != \"\" && k == 0) || (s == \"\" && k > 0) then\n return false\n end\n\n ret = false\n 2.upto(s.size) do |i|\n ss = s[0, i]\n if is_palindrome(ss) then\n ret = ret | ok(s[i, s.size], k - 1)\n end\n end\n return ret\nend\n\ndef main()\n s = gets.chomp\n k = gets.to_i\n \n puts ok(s, k) ? \"YES\" : \"NO\"\nend\n\n\nif __FILE__ == $0 then\n main()\nend\n"}, {"source_code": "s = gets.chomp.chars\nn = gets.to_i\nc = 0\na = []\ni = 0\nwhile i < s.size\n if a != [] && s[i...i + a.size] == a.reverse\n c += 1\n i += a.size\n a = []\n else\n a << s[i]\n i += 1\n end\nend\nputs c == n ? 'YES' : 'NO'\n"}, {"source_code": "s = gets.chomp.chars\nk = gets.to_i\na = []\nvalid = false\ns.size.times do |i|\n a << s[i]\n if a == a.reverse\n start = a.size\n c = 1\n valid = true\n (k - 1).times do\n t = s[start...start + a.size]\n if start + a.size >= s.size || t != t.reverse\n valid = false\n break\n end\n start += a.size\n end\n valid = false if a.size * k != s.size\n break if valid\n end\nend\nputs valid ? 'YES' : 'NO'\n"}, {"source_code": "s = gets.chomp.chars\nk = gets.to_i\na = []\nvalid = false\ns.size.times do |i|\n a << s[i]\n if s[a.size...2 * a.size] == a.reverse\n start = 2 * a.size\n (k - 1).times do\n valid = true\n if s[start...start + a.size] != s[start + a.size...start + 2 * a.size].reverse\n valid = false\n break\n end\n start += 2 * a.size\n end\n break if valid\n end\nend\nputs valid ? 'YES' : 'NO'\n"}, {"source_code": "s = gets.chomp.chars\nk = gets.to_i\na = []\nvalid = false\ns.size.times do |i|\n a << s[i]\n if a == s[a.size...2 * a.size].reverse\n start = 2 * a.size\n valid = true\n (k - 1).times do\n if start + 2 * a.size > s.size || s[start...start + a.size] != s[start + a.size...start + 2 * a.size].reverse\n valid = false\n break\n end\n start += 2 * a.size\n end\n break if valid\n end\nend\nputs valid ? 'YES' : 'NO'\n"}, {"source_code": "s = gets.chomp.chars\nk = gets.to_i\nputs s.each_slice(s.size / k).to_a.all?{|x| x == x.reverse} ? 'YES' : 'NO'\n"}, {"source_code": "#!ruby\n\ns = gets.chomp\nk = gets.chomp.to_i\n\nul = s.length/k\n0.upto(k-1) do |i|\n ss = s[ul*i..ul*(i+1)-1]\n if ss != ss.reverse\n puts \"NO\"\n exit\n end\nend\nputs \"YES\"\nexit\n"}], "src_uid": "43bb8fec6b0636d88ce30f23b61be39f"} {"nl": {"description": "A positive integer is called composite if it can be represented as a product of two positive integers, both greater than $$$1$$$. For example, the following numbers are composite: $$$6$$$, $$$4$$$, $$$120$$$, $$$27$$$. The following numbers aren't: $$$1$$$, $$$2$$$, $$$3$$$, $$$17$$$, $$$97$$$.Alice is given a sequence of $$$n$$$ composite numbers $$$a_1,a_2,\\ldots,a_n$$$.She wants to choose an integer $$$m \\le 11$$$ and color each element one of $$$m$$$ colors from $$$1$$$ to $$$m$$$ so that: for each color from $$$1$$$ to $$$m$$$ there is at least one element of this color; each element is colored and colored exactly one color; the greatest common divisor of any two elements that are colored the same color is greater than $$$1$$$, i.e. $$$\\gcd(a_i, a_j)>1$$$ for each pair $$$i, j$$$ if these elements are colored the same color. Note that equal elements can be colored different colors — you just have to choose one of $$$m$$$ colors for each of the indices from $$$1$$$ to $$$n$$$.Alice showed already that if all $$$a_i \\le 1000$$$ then she can always solve the task by choosing some $$$m \\le 11$$$.Help Alice to find the required coloring. Note that you don't have to minimize or maximize the number of colors, you just have to find the solution with some $$$m$$$ from $$$1$$$ to $$$11$$$.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. Then the descriptions of the test cases follow. The first line of the test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 1000$$$) — the amount of numbers in a sequence $$$a$$$. The second line of the test case contains $$$n$$$ composite integers $$$a_1,a_2,\\ldots,a_n$$$ ($$$4 \\le a_i \\le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^4$$$.", "output_spec": "For each test case print $$$2$$$ lines. The first line should contain a single integer $$$m$$$ ($$$1 \\le m \\le 11$$$) — the number of used colors. Consider colors to be numbered from $$$1$$$ to $$$m$$$. The second line should contain any coloring that satisfies the above conditions. Print $$$n$$$ integers $$$c_1, c_2, \\dots, c_n$$$ ($$$1 \\le c_i \\le m$$$), where $$$c_i$$$ is the color of the $$$i$$$-th element. If there are multiple solutions then you can print any of them. Note that you don't have to minimize or maximize the number of colors, you just have to find the solution with some $$$m$$$ from $$$1$$$ to $$$11$$$. Remember that each color from $$$1$$$ to $$$m$$$ should be used at least once. Any two elements of the same color should not be coprime (i.e. their GCD should be greater than $$$1$$$).", "sample_inputs": ["3\n3\n6 10 15\n2\n4 9\n23\n437 519 865 808 909 391 194 291 237 395 323 365 511 497 781 737 871 559 731 697 779 841 961"], "sample_outputs": ["1\n1 1 1\n2\n2 1\n11\n4 7 8 10 7 3 10 7 7 8 3 1 1 5 5 9 2 2 3 3 4 11 6"], "notes": "NoteIn the first test case, $$$\\gcd(6,10)=2$$$, $$$\\gcd(6,15)=3$$$ and $$$\\gcd(10,15)=5$$$. Therefore, it's valid to color all elements the same color. Note that there are other colorings which satisfy Alice's requirement in this test case.In the second test case there is only one element of each color, so the coloring definitely satisfies Alice's requirement."}, "positive_code": [{"source_code": "# https://codeforces.com/contest/1332/problem/B\n\nrequire 'set'\n\n@primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]\n\ndef lowest_divisor(n)\n @primes.each do |p|\n if n % p == 0\n return p\n end\n end\nend\n\ngets.to_i.times do\n n = gets.to_i\n numbers = gets.split(' ').map(&:to_i)\n \n answer = \"\"\n colors = {}\n color_count = 1\n \n numbers.each do |n|\n color = lowest_divisor(n)\n if colors[color].nil?\n colors[color] = color_count\n color_count += 1\n end\n \n answer += \"#{colors[color]} \"\n end\n\n answer[-1] = \"\"\n\n puts colors.size\n puts answer\nend"}, {"source_code": "# https://codeforces.com/contest/1332/problem/B\n\nprimes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]\n\ngets.to_i.times do\n n = gets.to_i\n numbers = gets.split(' ').map(&:to_i)\n \n answer = []\n colors = {}\n color_count = 1\n \n numbers.each do |n|\n color = primes.find do |p|\n n % p == 0\n end\n\n if colors[color].nil?\n colors[color] = color_count\n color_count += 1\n end\n \n answer << colors[color]\n end\n\n puts colors.size\n puts answer.join(' ')\nend"}, {"source_code": "primes = [2,3,5,7,11,13,17,19,23,29,31]\nt = gets.chomp.to_i\n\nt.times do\n n = gets.chomp.to_i\n a = gets.chomp.split.map(&:to_i)\n\n colors = {}\n coloring = a.map do |aa|\n color = primes.find do |pr|\n aa % pr == 0\n end\n\n if colors.key?(color)\n colors[color]\n else\n colors[color] = colors.size + 1\n end\n end\n puts colors.size\n puts coloring.join(' ')\nend\n"}, {"source_code": "PRIME = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]\nt = gets.to_i\nt.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n mapping = {}\n used = 0\n c = Array.new(n, nil)\n a.each_with_index do |v, i|\n PRIME.each_with_index do |p, j|\n if v % p == 0\n if !mapping.include?(p)\n used += 1\n mapping[p] = used\n end\n c[i] = mapping[p]\n break\n end\n end\n end\n puts used\n puts c.join(\" \")\nend\n"}], "negative_code": [{"source_code": "# https://codeforces.com/contest/1332/problem/B\n\nrequire 'set'\n\n@primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]\n\ndef lowest_divisor(n)\n @primes.each do |p|\n if n % p == 0\n return p\n end\n end\nend\n\ngets.to_i.times do\n n = gets.to_i\n numbers = gets.split(' ').map(&:to_i)\n \n answer = \"\"\n colors = Set.new()\n\n numbers.each do |n|\n color = lowest_divisor(n)\n colors << color\n answer += \"#{@primes.index(color) + 1} \"\n end\n\n answer[-1] = \"\"\n\n puts answer\nend"}, {"source_code": "# https://codeforces.com/contest/1332/problem/B\n\nrequire 'set'\n\n@primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]\n\ndef lowest_divisor(n)\n @primes.each do |p|\n if n % p == 0\n return p\n end\n end\nend\n\ngets.to_i.times do\n n = gets.to_i\n numbers = gets.split(' ').map(&:to_i)\n \n answer = \"\"\n colors = Set.new()\n\n numbers.each do |n|\n color = lowest_divisor(n)\n colors << color\n answer += \"#{@primes.index(color) + 1} \"\n end\nend"}, {"source_code": "# https://codeforces.com/contest/1332/problem/B\n\nrequire 'set'\n\n@primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]\n\ndef lowest_divisor(n)\n @primes.each do |p|\n if n % p == 0\n return p\n end\n end\nend\n\ngets.to_i.times do\n n = gets.to_i\n numbers = gets.split(' ').map(&:to_i)\n \n answer = \"\"\n colors = Set.new()\n\n numbers.each do |n|\n color = lowest_divisor(n)\n colors << color\n answer += \"#{@primes.index(color) + 1} \"\n end\n\n answer.rstrip!\nend"}, {"source_code": "# https://codeforces.com/contest/1332/problem/B\n\nrequire 'set'\n\n@primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]\n\ndef lowest_divisor(n)\n @primes.each do |p|\n if n % p == 0\n return p\n end\n end\nend\n\ngets.to_i.times do\n n = gets.to_i\n numbers = gets.split(' ').map(&:to_i)\n \n answer = \"\"\n colors = Set.new()\n\n numbers.each do |n|\n color = lowest_divisor(n)\n colors << color\n answer += \"#{@primes.index(color) + 1} \"\n end\n\n answer[-1] = \"\"\n\n puts colors.size\n puts answer\nend"}], "src_uid": "c3cd949c99e96c9da186a34d49bd6197"} {"nl": {"description": "You are given a rebus of form ? + ? - ? + ? = n, consisting of only question marks, separated by arithmetic operation '+' and '-', equality and positive integer n. The goal is to replace each question mark with some positive integer from 1 to n, such that equality holds.", "input_spec": "The only line of the input contains a rebus. It's guaranteed that it contains no more than 100 question marks, integer n is positive and doesn't exceed 1 000 000, all letters and integers are separated by spaces, arithmetic operations are located only between question marks.", "output_spec": "The first line of the output should contain \"Possible\" (without quotes) if rebus has a solution and \"Impossible\" (without quotes) otherwise. If the answer exists, the second line should contain any valid rebus with question marks replaced by integers from 1 to n. Follow the format given in the samples.", "sample_inputs": ["? + ? - ? + ? + ? = 42", "? - ? = 1", "? = 1000000"], "sample_outputs": ["Possible\n9 + 13 - 39 + 28 + 31 = 42", "Impossible", "Possible\n1000000 = 1000000"], "notes": null}, "positive_code": [{"source_code": "def solve(line)\n plus = 1\n minu = 0\n\n line.each_char do |ch|\n if ch == '+'\n plus += 1\n elsif ch == '-'\n minu += 1\n end\n end\n\n line =~ /([0-9]+)/\n n = Regexp.last_match[0].to_i\n\n if n > plus * n - minu || n < plus - minu * n\n puts \"Impossible\"\n return\n end\n\n curr = plus - minu\n\n signs = '+' + line.gsub(/[^+-]/, '')\n\n first = true\n\n puts \"Possible\"\n\n signs.each_char do |sign|\n print \" #{sign} \" if !first\n first = false\n if sign == '+'\n if curr >= n\n print 1\n else\n x = [n - curr + 1, n].min\n curr += x - 1\n print x\n end\n else\n if curr <= n\n print 1\n else\n x = [curr - n + 1, n].min\n curr -= x - 1\n print x\n end\n end\n end\n\n puts \" = #{n}\"\nend\n\nwhile line = gets\n solve(line.chomp)\nend\n"}, {"source_code": "def isok(arr, n, target)\n pos, neg = arr.count{|x| x == \"+\"}, arr.count{|x| x == '-'}\n max, min = pos * n - neg, pos - neg * n\n #print \"##\", max, \" \", min, \" \", target\n #puts \"\"\n if target <= max && target >= min\n return 0\n elsif target > max\n return -1\n else\n return 1\n end\nend\n\narr = readline.gsub(/[?=]/, \"\").split(' ')\nn = arr.last.to_i\narr = [\"+\"] + arr[0..-2]\n\nif isok(arr, n, n) == 0\n puts \"Possible\"\n target = n\n 0.upto(arr.length - 1) do |ind|\n ans = 0\n flag = arr[ind] == \"+\" ? 1 : -1\n if isok(arr[ind + 1..-1], n, target - flag) == 0\n ans = 1\n elsif isok(arr[ind + 1..-1], n, target - n * flag) == 0\n ans = n\n else\n down, up = 1, n\n downf = isok(arr[ind + 1..-1], n, target - flag)\n upf = isok(arr[ind + 1..-1], n, target - n * flag)\n while down + 1 < up\n mid = (down + up) / 2\n res = isok(arr[ind + 1..-1], n, target - mid * flag)\n if res == 0\n ans = mid\n break\n elsif res == downf\n down = mid\n else\n up = mid\n end\n end\n end\n if ind != 0\n print \" \" + arr[ind] + \" \"\n end\n print ans\n target -= ans * flag\n end\n print \" = \"\n puts n\nelse\n puts \"Impossible\"\nend\n"}, {"source_code": "arr=gets.split('?')\narr[0]='+'\nsz=arr.size\nn=arr[sz-1]\nn=n[3,n.size-3].to_i\na=Array.new(111)\ns=-n\nfor i in 0..sz-2\n\tif arr[i].strip=='+'\n\t\ta[i]=n\n\t\ts+=n\n\telse\n\t\ta[i]=1\n\t\ts-=1\n\tend\nend\nfor i in 0..sz-2\n\tif s>0 && arr[i].strip=='+'\n\t\tdet = [n-1, s].min\n\t\ta[i]-=det\n\t\ts-=det\n\tend\n\tif s>0 && arr[i].strip=='-'\n\t\tdet = [n-1, s].min\n\t\ta[i]+=det\n\t\ts-=det\n\tend\nend\nif s!=0\n\tputs \"Impossible\"\nelse\n\tputs \"Possible\"\n\tarr[0]=\"\"\n\tfor i in 0..sz-2\n\t\tprint \"#{arr[i]}#{a[i]}\"\n\tend\n\tputs arr[sz-1]\nend"}, {"source_code": "\n\n\nstr = (\"+ \" + gets).split\nl = str.length\nm = l/2 -1\nn = str[l-1].to_i\na = Array.new(m){|e| e = 0}\ns = 0\nm.times{ |i|\n\ta[i] = str[i*2]=='+'?1: -1;\n\ts+=a[i]\n}\n\ni = 0\n\nif (s0 && a[i]n\n\twhile(i-n\n\t\t\ts-=1;a[i]-=1;\n\t\telse\n\t\t\ti+=1;\n\t\tend\n\tend\nend\n\nif s!=n\n\tprint \"Impossible\\n\"\nelse\n\tprint \"Possible\\n\"\n\tstr = \"\"\n\tfor i in (0...m)\n\t\tstr += \"#{a[i]}\" if i==0\n\t\tif i!=0\n\t\t\tif a[i]>0\n\t\t\t\tstr += \" + #{a[i]}\"\n\t\t\telse \n\t\t\t\tstr += \" - #{-a[i]}\"\n\t\t\tend\n\t\tend\n\tend\n\tstr += \" = #{n}\"\n\tprint str\nend"}], "negative_code": [{"source_code": "def isok(arr, n, target)\n pos, neg = arr.count{|x| x == \"+\"}, arr.count{|x| x == '-'}\n max, min = pos * n - neg, pos - neg * n\n #print \"##\", max, \" \", min, \" \", target\n #puts \"\"\n if target <= max && target >= min\n return 0\n elsif target > max\n return -1\n else\n return 1\n end\nend\n\narr = readline.gsub(/[?=]/, \"\").split(' ')\nn = arr.last.to_i\narr = [\"+\"] + arr[0..-2]\n\nif isok(arr, n, n) == 0\n puts \"Possible\"\n target = n\n 0.upto(arr.length - 1) do |ind|\n ans = 0\n flag = arr[ind] == \"+\" ? 1 : -1\n if isok(arr[ind + 1..-1], n, target - flag) == 0\n ans = 1\n elsif isok(arr[ind + 1..-1], n, target - n * flag) == 0\n ans = n\n else\n down, up = 1, n\n downf = isok(arr[ind + 1..-1], n, target - flag)\n upf = isok(arr[ind + 1..-1], n, target - n * flag)\n while down + 1 < up\n mid = (down + up) / 2\n res = isok(arr[ind + 1..-1], n, target - mid)\n if res == 0\n ans = mid\n break\n elsif res == downf\n down = mid\n else\n up = mid\n end\n end\n end\n if ind != 0\n print \" \" + arr[ind] + \" \"\n end\n print ans\n target -= ans * flag\n end\n print \" = \"\n puts n\nelse\n puts \"Impossible\"\nend\n"}, {"source_code": "def isok(arr, n, target)\n pos, neg = arr.count{|x| x == \"+\"}, arr.count{|x| x == '-'}\n max, min = pos * n - neg, pos - neg * n\n #print \"##\", max, \" \", min, \" \", target\n #puts \"\"\n if target <= max && target >= min\n return 0\n elsif target > max\n return -1\n else\n return 1\n end\nend\n\narr = readline.gsub(/[?=]/, \"\").split(' ')\nn = arr.last.to_i\narr = [\"+\"] + arr[0..-2]\n\nif isok(arr, n, n) == 0\n target = n\n 0.upto(arr.length - 1) do |ind|\n ans = 0\n flag = arr[ind] == \"+\" ? 1 : -1\n if isok(arr[ind + 1..-1], n, target - flag) == 0\n ans = 1\n elsif isok(arr[ind + 1..-1], n, target - n * flag) == 0\n ans = n\n else\n down, up = 1, n\n downf = isok(arr[ind + 1..-1], n, target - flag)\n upf = isok(arr[ind + 1..-1], n, target - n * flag)\n while down + 1 < up\n mid = (down + up) / 2\n res = isok(arr[ind + 1..-1], n, target - mid)\n if res == 0\n ans = mid\n break\n elsif res == downf\n down = mid\n else\n up = mid\n end\n end\n end\n if ind != 0\n print \" \" + arr[ind] + \" \"\n end\n print ans\n target -= ans * flag\n end\n print \" = \"\n puts n\nelse\n puts \"Impossible\"\nend\n"}, {"source_code": "arr=gets.split('?')\narr[0]='+'\nsz=arr.size\nn=arr[sz-1]\nn=n[3,n.size-3].to_i\na=Array.new(111)\ns=-n\nfor i in 0..sz-2\n\tif arr[i].strip=='+'\n\t\ta[i]=n\n\t\ts+=n\n\telse\n\t\ta[i]=1\n\t\ts-=1\n\tend\nend\nfor i in 0..sz-2\n\tif s>0 && a[i]==n\n\t\tdet = [n-1, s].min\n\t\ta[i]-=det\n\t\ts-=det\n\tend\nend\nif s!=0\n\tputs \"Impossible\"\nelse\n\tputs \"Possible\"\n\tarr[0]=\"\"\n\tfor i in 0..sz-2\n\t\tprint \"#{arr[i]}#{a[i]}\"\n\tend\n\tputs arr[sz-1]\nend"}], "src_uid": "35a97c47182916aaafe4c6e4b69bc79f"} {"nl": {"description": "Little Alyona is celebrating Happy Birthday! Her mother has an array of n flowers. Each flower has some mood, the mood of i-th flower is ai. The mood can be positive, zero or negative.Let's define a subarray as a segment of consecutive flowers. The mother suggested some set of subarrays. Alyona wants to choose several of the subarrays suggested by her mother. After that, each of the flowers will add to the girl's happiness its mood multiplied by the number of chosen subarrays the flower is in.For example, consider the case when the mother has 5 flowers, and their moods are equal to 1,  - 2, 1, 3,  - 4. Suppose the mother suggested subarrays (1,  - 2), (3,  - 4), (1, 3), (1,  - 2, 1, 3). Then if the girl chooses the third and the fourth subarrays then: the first flower adds 1·1 = 1 to the girl's happiness, because he is in one of chosen subarrays, the second flower adds ( - 2)·1 =  - 2, because he is in one of chosen subarrays, the third flower adds 1·2 = 2, because he is in two of chosen subarrays, the fourth flower adds 3·2 = 6, because he is in two of chosen subarrays, the fifth flower adds ( - 4)·0 = 0, because he is in no chosen subarrays. Thus, in total 1 + ( - 2) + 2 + 6 + 0 = 7 is added to the girl's happiness. Alyona wants to choose such subarrays from those suggested by the mother that the value added to her happiness would be as large as possible. Help her do this!Alyona can choose any number of the subarrays, even 0 or all suggested by her mother.", "input_spec": "The first line contains two integers n and m (1 ≤ n, m ≤ 100) — the number of flowers and the number of subarrays suggested by the mother. The second line contains the flowers moods — n integers a1, a2, ..., an ( - 100 ≤ ai ≤ 100). The next m lines contain the description of the subarrays suggested by the mother. The i-th of these lines contain two integers li and ri (1 ≤ li ≤ ri ≤ n) denoting the subarray a[li], a[li + 1], ..., a[ri]. Each subarray can encounter more than once.", "output_spec": "Print single integer — the maximum possible value added to the Alyona's happiness.", "sample_inputs": ["5 4\n1 -2 1 3 -4\n1 2\n4 5\n3 4\n1 4", "4 3\n1 2 3 4\n1 3\n2 4\n1 1", "2 2\n-1 -2\n1 1\n1 2"], "sample_outputs": ["7", "16", "0"], "notes": "NoteThe first example is the situation described in the statements.In the second example Alyona should choose all subarrays.The third example has answer 0 because Alyona can choose none of the subarrays."}, "positive_code": [{"source_code": "n, m=gets.split.map &:to_i\na=gets.split.map &:to_i\n\ns=[0]\nfor i in 1..a.size\n s<0\n}\np res\n"}], "negative_code": [], "src_uid": "6d7364048428c70e0e9b76ab1eb8cc34"} {"nl": {"description": "A tuple of positive integers {x1, x2, ..., xk} is called simple if for all pairs of positive integers (i,  j) (1  ≤ i  <  j ≤ k), xi  +  xj is a prime.You are given an array a with n positive integers a1,  a2,  ...,  an (not necessary distinct). You want to find a simple subset of the array a with the maximum size.A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.Let's define a subset of the array a as a tuple that can be obtained from a by removing some (possibly all) elements of it.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 1000) — the number of integers in the array a. The second line contains n integers ai (1 ≤ ai ≤ 106) — the elements of the array a.", "output_spec": "On the first line print integer m — the maximum possible size of simple subset of a. On the second line print m integers bl — the elements of the simple subset of the array a with the maximum size. If there is more than one solution you can print any of them. You can print the elements of the subset in any order.", "sample_inputs": ["2\n2 3", "2\n2 2", "3\n2 1 1", "2\n83 14"], "sample_outputs": ["2\n3 2", "1\n2", "3\n1 1 2", "2\n14 83"], "notes": null}, "positive_code": [{"source_code": "def sieve\n low = 0\n high = 2000001\n arr = []\n for i in 0..high do\n arr[i] = true\n end\n for i in 2..Math.sqrt(high).ceil do\n if arr[i]\n j = i*i\n while j < high\n arr[j] = false\n j += i\n end\n end\n end\n return arr\nend\n\nprimes = sieve\nn = gets.chomp.to_i\narr = gets.split.map(&:to_i)\n\n# count 1's\ncnt = 0\narr.each do |val|\n cnt += 1 if val == 1\n break if cnt > 1\nend\n\nsubset = []\nif cnt > 1\n arr.each do |val|\n subset.push(val) if val == 1\n end\n arr.each do |val|\n if(primes[val + 1] and val != 1)\n subset.push(val)\n break\n end\n end\nelse\n done = false\n for i in 0...n - 1\n for j in i + 1..n - 1\n if(primes[arr[i] + arr[j]])\n subset.push(arr[i])\n subset.push(arr[j])\n done = true\n break\n end\n end\n break if done\n end\nend\nsubset[0] = arr[0] if subset.length == 0\nputs subset.length\nputs subset.join(\" \")\n\n"}], "negative_code": [{"source_code": "def sieve\n low = 0\n high = 2000000\n arr = []\n for i in 0..high do\n arr[i] = true\n end\n for i in 2..Math.sqrt(high).ceil do\n if arr[i]\n j = i*i\n while j < high\n arr[j] = false\n j += i\n end\n end\n end\n return arr\nend\n\nprimes = sieve\nn = gets.chomp.to_i\narr = gets.split.map(&:to_i)\n\n# count 1's\ncnt = 0\narr.each do |val|\n cnt += 1 if val == 1\n break if cnt > 1\nend\n\nsubset = []\nif cnt > 1\n arr.each do |val|\n subset.push(val) if val == 1\n end\n arr.each do |val|\n if(primes[val + 1] and val != 1)\n subset.push(val)\n break\n end\n end\nelse\n for i in 0..n - 1\n for j in i..n - 1\n if(primes[arr[i] + arr[j]])\n subset.push(arr[i])\n subset.push(arr[j])\n break\n end\n end\n end\nend\nputs subset.length\nputs subset.join(\" \")\n\n"}, {"source_code": "def sieve\n low = 0\n high = 2000000\n arr = []\n for i in 0..high do\n arr[i] = true\n end\n for i in 2..Math.sqrt(high).ceil do\n if arr[i]\n j = i*i\n while j < high\n arr[j] = false\n j += i\n end\n end\n end\n return arr\nend\n\nprimes = sieve\nn = gets.chomp.to_i\narr = gets.split.map(&:to_i)\n\n# count 1's\ncnt = 0\narr.each do |val|\n cnt += 1 if val == 1\n break if cnt > 1\nend\n\nsubset = []\nif cnt > 1\n arr.each do |val|\n subset.push(val) if val == 1\n end\n arr.each do |val|\n if(primes[val + 1] and val != 1)\n subset.push(val)\n break\n end\n end\nelse\n for i in 0..n - 1\n for j in i..n - 1\n if(primes[arr[i] + arr[j]])\n subset.push(arr[i])\n subset.push(arr[j])\n break\n end\n end\n end\nend\nsubset[0] = arr[0] if subset.length == 0\nputs subset.length\nputs subset.join(\" \")\n\n"}, {"source_code": "def sieve\n low = 0\n high = 2000000\n arr = []\n for i in 0..high do\n arr[i] = true\n end\n for i in 2..Math.sqrt(high).ceil do\n if arr[i]\n j = i*i\n while j < high\n arr[j] = false\n j += i\n end\n end\n end\n return arr\nend\n\nprimes = sieve\nn = gets.chomp.to_i\narr = gets.split.map(&:to_i)\n\n# count 1's\ncnt = 0\narr.each do |val|\n cnt += 1 if val == 1\n break if cnt > 1\nend\n\nsubset = []\nif cnt > 1\n arr.each do |val|\n subset.push(val) if val == 1\n end\n arr.each do |val|\n if(primes[val + 1] and val != 1)\n subset.push(val)\n break\n end\n end\nelse\n done = false\n for i in 0..n - 1\n for j in i..n - 1\n if(primes[arr[i] + arr[j]])\n subset.push(arr[i])\n subset.push(arr[j])\n done = true\n break\n end\n end\n break if done\n end\nend\nsubset[0] = arr[0] if subset.length == 0\nputs subset.length\nputs subset.join(\" \")\n\n"}, {"source_code": "def sieve\n low = 0\n high = 2000000\n arr = []\n for i in 0..high do\n arr[i] = true\n end\n for i in 2..Math.sqrt(high).ceil do\n if arr[i]\n j = i*i\n while j < high\n arr[j] = false\n j += i\n end\n end\n end\n return arr\nend\n\nprimes = sieve\n\nn = gets.chomp.to_i\narr = gets.split.map(&:to_i)\nmax_subset = []\nfor c in 0...n do\n subset = [arr[c]]\n for i in c + 1...n do\n add = true\n subset.each do |val|\n if !primes[val + arr[i]]\n add = false\n break\n end\n end\n subset.push(arr[i]) if add\n end\n max_subset = subset if subset.length > max_subset.length\n break if max_subset.length >= n - (c + 1)\nend\nputs max_subset.length\nputs max_subset.join(\" \")\n"}, {"source_code": "def sieve\n low = 0\n high = 2000001\n arr = []\n for i in 0..high do\n arr[i] = true\n end\n for i in 2..Math.sqrt(high).ceil do\n if arr[i]\n j = i*i\n while j < high\n arr[j] = false\n j += i\n end\n end\n end\n return arr\nend\n\nprimes = sieve\nn = gets.chomp.to_i\narr = gets.split.map(&:to_i)\n\n# count 1's\ncnt = 0\narr.each do |val|\n cnt += 1 if val == 1\n break if cnt > 1\nend\n\nsubset = []\nif cnt > 1\n arr.each do |val|\n subset.push(val) if val == 1\n end\n arr.each do |val|\n if(primes[val + 1] and val != 1)\n subset.push(val)\n break\n end\n end\nelse\n done = false\n for i in 0..n - 1\n for j in i..n - 1\n if(primes[arr[i] + arr[j]])\n subset.push(arr[i])\n subset.push(arr[j])\n done = true\n break\n end\n end\n break if done\n end\nend\nsubset[0] = arr[0] if subset.length == 0\nputs subset.length\nputs subset.join(\" \")\n\n"}], "src_uid": "6be0731d9d2d55bf9aa3492a8161d23c"} {"nl": {"description": "Nikolay has only recently started in competitive programming, but already qualified to the finals of one prestigious olympiad. There going to be $$$n$$$ participants, one of whom is Nikolay. Like any good olympiad, it consists of two rounds. Tired of the traditional rules, in which the participant who solved the largest number of problems wins, the organizers came up with different rules.Suppose in the first round participant A took $$$x$$$-th place and in the second round — $$$y$$$-th place. Then the total score of the participant A is sum $$$x + y$$$. The overall place of the participant A is the number of participants (including A) having their total score less than or equal to the total score of A. Note, that some participants may end up having a common overall place. It is also important to note, that in both the first and the second round there were no two participants tying at a common place. In other words, for every $$$i$$$ from $$$1$$$ to $$$n$$$ exactly one participant took $$$i$$$-th place in first round and exactly one participant took $$$i$$$-th place in second round.Right after the end of the Olympiad, Nikolay was informed that he got $$$x$$$-th place in first round and $$$y$$$-th place in the second round. Nikolay doesn't know the results of other participants, yet he wonders what is the minimum and maximum place he can take, if we consider the most favorable and unfavorable outcome for him. Please help Nikolay to find the answer to this question.", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases to solve. Each of the following $$$t$$$ lines contains integers $$$n$$$, $$$x$$$, $$$y$$$ ($$$1 \\leq n \\leq 10^9$$$, $$$1 \\le x, y \\le n$$$) — the number of participants in the olympiad, the place that Nikolay took in the first round and the place that Nikolay took in the second round.", "output_spec": "Print two integers — the minimum and maximum possible overall place Nikolay could take.", "sample_inputs": ["1\n5 1 3", "1\n6 3 4"], "sample_outputs": ["1 3", "2 6"], "notes": "NoteExplanation for the first example:Suppose there were 5 participants A-E. Let's denote Nikolay as A. The the most favorable results for Nikolay could look as follows: However, the results of the Olympiad could also look like this: In the first case Nikolay would have taken first place, and in the second — third place."}, "positive_code": [{"source_code": "T = gets.to_i\n\ndef estimate_top(n, x, y)\n rank = 1\n\n above_a = x - 1\n below_a = [0, n - (x + 1)].max\n above_b = y - 1\n below_b = [0, n - (y + 1)].max\n\n d1 = [0, [above_a, below_b].min].max\n d2 = [0, [above_b, below_a].min].max\n\n above_a -= d1\n above_b -= d2\n\n rank += [0, [above_a, above_b].max].max\n\n rank\nend\n\ndef estimate_bottom(n, x, y)\n rank = 1\n\n above_a = x - 1\n below_a = n - x\n above_b = y - 1\n below_b = n - y\n\n d1 = [0, [above_a, below_b].min].max\n d2 = [0, [above_b, below_a].min].max\n\n above_a -= d1\n above_b -= d2\n\n rank += d1 + d2 + [0, [above_a, above_b].max].max\n\n rank\nend\n\nT.times do\n n, x, y = gets.split.map(&:to_i)\n x, y = [x, y].sort\n\n top = estimate_top(n, x, y)\n bottom = estimate_bottom(n, x, y)\n\n puts \"#{top} #{bottom}\"\nend\n"}, {"source_code": "def best(n, x, y)\n case x+y\n when (2..n) then 1\n when 2*n then n\n else x+y - n + 1\n end\nend\n\ndef worst(n, x, y)\n x+y > n+1 ? n : x+y -1\nend\n\n1.upto(gets.chomp.to_i) do\n n,x,y = gets.chomp.split.map(&:to_i)\n\n puts [best(n,x,y), worst(n,x,y)].join(' ')\nend\n"}, {"source_code": "# frozen_string_literal: true\n\n@data = $stdin.read.split\n\ndef next_token\n @data.shift\nend\n\ndef next_int\n next_token.to_i\nend\n\nnext_int.times do\n n = next_int\n x = next_int\n y = next_int\n\n maxp = [x + y - 1, n].min\n b = x + y + 1 - n\n # b -= 1 if y == n\n minp = [[b, n].min, 1].max\n # a = x + y - n + 1\n # b = x + y\n # a = [a, 1].max\n # b = [b, n].min\n # minp = b - a\n # minp += 1 if b < n\n # minp = n - minp\n puts \"#{minp} #{maxp}\"\nend\n"}, {"source_code": "#! /usr/bin/env ruby\n\n=begin\n1 2 3 4 5 和\n1 2 3 4 5 两两配对\n希望对子的和大于我的对子的和\n\n注意到 2+5 3+4 .. 5+2 都是7\n\n可以贪心。枚举第一场比赛成绩,可得 要这个人输给我,第二场得多差\n\nn太大,得优化一下\navail_y = [1, ... my_y-1], [my_y+1, ... n]\n阶段1:x + y_too_small ...\n阶段2:x + y\n=end\n\ndef solve_best(n, my_x, my_y)\n goal = my_x + my_y + 1\n nlose = 0\n\n x = goal - n; y = n\n if x < 1 then x = 1 end\n loop do\n\t break if x > n || y < 1\n\t if my_y == y\n\t y -= 1\n\t if x+y < goal then x += 1 end\n\t end\n\t if my_x == x then x += 1 end\n\t break if x > n || y < 1\n\n\t len = n\n\t stop_at = (y > my_y ? my_y : 0)\n\t if len > y - stop_at then len = y - stop_at end\n\t stop_at = (x < my_x ? my_x : n+1)\n\t if len > stop_at - x then len = stop_at - x end\n\t nlose += len\n\t x += len\n\t y -= len\n end\n\n print (n - nlose), ' '\nend\n\ndef solve_worst(n, my_x, my_y)\n goal = my_x + my_y\n nwin = 1 # myself\n\n x = 1; y = goal - 1\n if y > n then y = n end\n loop do\n break if x > n || y < 1\n if my_x == x\n x += 1\n if x+y > goal then y -= 1 end\n end\n if my_y == y then y -= 1 end\n break if x > n || y < 1\n\n len = n\n stop_at = (y > my_y ? my_y : 0)\n if len > y - stop_at then len = y - stop_at end\n stop_at = (x < my_x ? my_x : n+1)\n if len > stop_at - x then len = stop_at - x end\n nwin += len\n x += len\n y -= len\n end\n\n puts nwin\nend\n\ndef solve\n n, myx, myy = gets.split.map{|x| x.to_i}\n solve_best(n, myx, myy)\n solve_worst(n, myx, myy)\nend\n\nt = gets.to_i\nt.times { solve }\n"}], "negative_code": [{"source_code": "# frozen_string_literal: true\n\n@data = $stdin.read.split\n\ndef next_token\n @data.shift\nend\n\ndef next_int\n next_token.to_i\nend\n\nnext_int.times do\n n = next_int\n x = next_int\n y = next_int\n\n a = x + y - n\n b = x + y - 1\n a = [a, 1].max\n b = [b, n].min\n maxp = (b - a + 1)\n b = [[x + y + 1 - n, n].min, 1].max\n minp = b\n # a = x + y - n + 1\n # b = x + y\n # a = [a, 1].max\n # b = [b, n].min\n # minp = b - a\n # minp += 1 if b < n\n # minp = n - minp\n puts \"#{minp} #{maxp}\"\nend\n"}, {"source_code": "# frozen_string_literal: true\n\n@data = $stdin.read.split\n\ndef next_token\n @data.shift\nend\n\ndef next_int\n next_token.to_i\nend\n\nnext_int.times do\n n = next_int\n x = next_int\n y = next_int\n\n a = x + y - n\n b = x + y - 1\n a = [a, 1].max\n b = [b, n].min\n maxp = (b - a + 1)\n b = x + y + 1 - n\n b -= 1 if y == n\n minp = [[b, n].min, 1].max\n # a = x + y - n + 1\n # b = x + y\n # a = [a, 1].max\n # b = [b, n].min\n # minp = b - a\n # minp += 1 if b < n\n # minp = n - minp\n puts \"#{minp} #{maxp}\"\nend\n"}, {"source_code": "# frozen_string_literal: true\n\n@data = $stdin.read.split\n\ndef next_token\n @data.shift\nend\n\ndef next_int\n next_token.to_i\nend\n\nnext_int.times do\n n = next_int\n x = next_int\n y = next_int\n\n maxp = [x + y - 1, n].min\n b = x + y + 1 - n\n b -= 1 if y == n\n minp = [[b, n].min, 1].max\n # a = x + y - n + 1\n # b = x + y\n # a = [a, 1].max\n # b = [b, n].min\n # minp = b - a\n # minp += 1 if b < n\n # minp = n - minp\n puts \"#{minp} #{maxp}\"\nend\n"}, {"source_code": "# frozen_string_literal: true\n\n@data = $stdin.read.split\n\ndef next_token\n @data.shift\nend\n\ndef next_int\n next_token.to_i\nend\n\nnext_int.times do\n n = next_int\n x = next_int\n y = next_int\n\n a = x + y - n\n b = x + y - 1\n a = [a, 1].max\n b = [b, n].min\n maxp = (b - a + 1)\n a = x + y - n + 1\n b = x + y\n a = [a, 1].max\n b = [b, n].min\n minp = b - a\n minp += 1 if b < n\n puts \"#{n - minp} #{maxp}\"\nend\n"}, {"source_code": "T = gets.to_i\n\ndef estimate_top(n, x, y)\n rank = 1\n\n above_a = x - 1\n d = n - (y + 1)\n\n if d < above_a\n rank += above_a - d\n end\n\n rank\nend\n\ndef estimate_bottom(n, x, y)\n rank = 1\n\n above_a = x - 1\n above_b = y - 1\n\n rank += above_a + above_b\n\n rank\nend\n\nT.times do\n n, x, y = gets.split.map(&:to_i)\n x, y = [x, y].sort\n\n top = estimate_top(n, x, y)\n bottom = estimate_bottom(n, x, y)\n\n puts \"#{top} #{bottom}\"\nend\n"}, {"source_code": "T = gets.to_i\n\ndef estimate_top(n, x, y)\n rank = 1\n\n above_a = x - 1\n d = [0, n - (y + 1)].max\n\n if d < above_a\n rank += above_a - d\n end\n\n rank\nend\n\ndef estimate_bottom(n, x, y)\n rank = 1\n\n above_a = x - 1\n below_a = n - x\n above_b = y - 1\n below_b = n - y\n\n d1 = [0, [above_a, below_b].min].max\n d2 = [0, [above_b, below_a].min].max\n\n above_a -= d1\n above_b -= d2\n\n rank += d1 + d2 + [above_a, above_b].min\n\n rank\nend\n\nT.times do\n n, x, y = gets.split.map(&:to_i)\n x, y = [x, y].sort\n\n top = estimate_top(n, x, y)\n bottom = estimate_bottom(n, x, y)\n\n puts \"#{top} #{bottom}\"\nend\n"}, {"source_code": "T = gets.to_i\n\ndef estimate_top(n, x, y)\n rank = 1\n\n above_a = x - 1\n d = n - (y + 1)\n\n if d < above_a\n rank += above_a - d\n end\n\n rank\nend\n\ndef estimate_bottom(n, x, y)\n rank = 1\n\n above_a = x - 1\n below_a = n - x\n above_b = y - 1\n below_b = n - y\n\n d1 = [0, [above_a, below_b].min].max\n d2 = [0, [above_b, below_a].min].max\n\n rank += d1 + d2\n\n rank\nend\n\nT.times do\n n, x, y = gets.split.map(&:to_i)\n x, y = [x, y].sort\n\n top = estimate_top(n, x, y)\n bottom = estimate_bottom(n, x, y)\n\n puts \"#{top} #{bottom}\"\nend\n"}, {"source_code": "T = gets.to_i\n\ndef estimate_top(n, x, y)\n rank = 1\n\n above_a = x - 1\n below_a = [0, n - (x + 1)].max\n above_b = y - 1\n below_b = [0, n - (y + 1)].max\n\n d1 = [0, [above_a, below_b].min].max\n d2 = [0, [above_b, below_a].min].max\n\n above_a -= d1\n above_b -= d2\n\n rank += [above_a, above_b].min\n\n rank\nend\n\ndef estimate_bottom(n, x, y)\n rank = 1\n\n above_a = x - 1\n below_a = n - x\n above_b = y - 1\n below_b = n - y\n\n d1 = [0, [above_a, below_b].min].max\n d2 = [0, [above_b, below_a].min].max\n\n above_a -= d1\n above_b -= d2\n\n rank += d1 + d2 + [above_a, above_b].min\n\n rank\nend\n\nT.times do\n n, x, y = gets.split.map(&:to_i)\n x, y = [x, y].sort\n\n top = estimate_top(n, x, y)\n bottom = estimate_bottom(n, x, y)\n\n puts \"#{top} #{bottom}\"\nend\n"}], "src_uid": "58c887568002d947706c448e6faa0f77"} {"nl": {"description": "Mike has always been thinking about the harshness of social inequality. He's so obsessed with it that sometimes it even affects him while solving problems. At the moment, Mike has two sequences of positive integers A = [a1, a2, ..., an] and B = [b1, b2, ..., bn] of length n each which he uses to ask people some quite peculiar questions.To test you on how good are you at spotting inequality in life, he wants you to find an \"unfair\" subset of the original sequence. To be more precise, he wants you to select k numbers P = [p1, p2, ..., pk] such that 1 ≤ pi ≤ n for 1 ≤ i ≤ k and elements in P are distinct. Sequence P will represent indices of elements that you'll select from both sequences. He calls such a subset P \"unfair\" if and only if the following conditions are satisfied: 2·(ap1 + ... + apk) is greater than the sum of all elements from sequence A, and 2·(bp1 + ... + bpk) is greater than the sum of all elements from the sequence B. Also, k should be smaller or equal to because it will be to easy to find sequence P if he allowed you to select too many elements!Mike guarantees you that a solution will always exist given the conditions described above, so please help him satisfy his curiosity!", "input_spec": "The first line contains integer n (1 ≤ n ≤ 105) — the number of elements in the sequences. On the second line there are n space-separated integers a1, ..., an (1 ≤ ai ≤ 109) — elements of sequence A. On the third line there are also n space-separated integers b1, ..., bn (1 ≤ bi ≤ 109) — elements of sequence B.", "output_spec": "On the first line output an integer k which represents the size of the found subset. k should be less or equal to . On the next line print k integers p1, p2, ..., pk (1 ≤ pi ≤ n) — the elements of sequence P. You can print the numbers in any order you want. Elements in sequence P should be distinct.", "sample_inputs": ["5\n8 7 4 8 3\n4 2 5 3 7"], "sample_outputs": ["3\n1 4 5"], "notes": null}, "positive_code": [{"source_code": "n = gets.strip.to_i\na = gets.strip.split.map(&:to_i)\nb = gets.strip.split.map(&:to_i)\n\nc = (0...n).sort { |i, j| a[j] <=> a[i] }\n\nans = [c[0]]\nfor i in (1...n).step(2)\n if i == n-1 or b[c[i]] > b[c[i+1]]\n ans.push(c[i])\n else\n ans.push(c[i+1])\n end\nend\nputs ans.length\nputs ans.map { |i| i+1 }.map(&:to_s).join(' ')\n\n"}], "negative_code": [], "src_uid": "d6d2c95396c299235f3302557cc3a2ed"} {"nl": {"description": "Let's call a string \"s-palindrome\" if it is symmetric about the middle of the string. For example, the string \"oHo\" is \"s-palindrome\", but the string \"aa\" is not. The string \"aa\" is not \"s-palindrome\", because the second half of it is not a mirror reflection of the first half. English alphabet You are given a string s. Check if the string is \"s-palindrome\".", "input_spec": "The only line contains the string s (1 ≤ |s| ≤ 1000) which consists of only English letters.", "output_spec": "Print \"TAK\" if the string s is \"s-palindrome\" and \"NIE\" otherwise.", "sample_inputs": ["oXoxoXo", "bod", "ER"], "sample_outputs": ["TAK", "TAK", "NIE"], "notes": null}, "positive_code": [{"source_code": "s=gets.chomp;puts s==s.tr('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ','#d#b##########oqp####vwx##A######HI###M#O####TUVWXY#').reverse ? :TAK : :NIE"}, {"source_code": "s = gets.strip\nmirrored = [\"A\", \"b\", \"d\", \"H\", \"I\", \"M\", \"O\", \"o\", \"p\", \"q\", \"T\", \"U\", \"V\", \"v\", \"W\", \"w\", \"X\", \"x\", \"Y\"]\nmirror_of = [\"A\", \"d\", \"b\", \"H\", \"I\", \"M\", \"O\", \"o\", \"q\", \"p\", \"T\", \"U\", \"V\", \"v\", \"W\", \"w\", \"X\", \"x\", \"Y\"]\ni = 0\nj = s.length - 1\npalindrom = true\nwhile i <= j\n l = mirrored.index s[i]\n r = mirror_of.index( s[j] )\n palindrom = false if l.nil? || r.nil? || l != r\n i += 1\n j -= 1\nend\nif palindrom\n puts \"TAK\"\nelse\n puts \"NIE\"\nend\n"}, {"source_code": "s = gets.chomp\nputs s == s.tr('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ','!d!b!!!!!!!!!!oqp!!!!vwx!!A!!!!!!HI!!!M!O!!!!TUVWXY!').reverse ? \"TAK\" :\"NIE\""}, {"source_code": "s = gets.strip\nputs s == s.tr('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ','#d#b##########oqp####vwx##A######HI###M#O####TUVWXY#').reverse ? \"TAK\" :\"NIE\""}, {"source_code": "word = gets.to_s\nchar_array = word.chars.to_a\nlen = char_array.length-1\narray_div = len/2\narray_mod = len%2\nchar_valid = \"AbdHIMOopqTUVvWwXxY\".chars.to_a\n#puts len\n#puts array_div\n#puts array_mod\nctr = 0\nif (1 <= len) && (len <= 1000)\n\tif array_mod == 0\n\t\t\tfor i in 0..array_div-1\n\t\t\t\tif char_valid.include?(char_array[i])\n\t\t\t\t\tif ((char_array[i] == 'q' && char_array[len-1-i] == 'p') || (char_array[i] == 'p' && char_array[len-1-i] == 'q'))\n ctr += 1\n #puts 'a'\n #puts ctr\n elsif ((char_array[i] == 'd' && char_array[len-1-i] == 'b') || (char_array[i] == 'b' && char_array[len-1-i] == 'd'))\n ctr += 1\n #puts 'b'\n #puts ctr\n elsif (char_array[i] == char_array[len-1-i] && (char_array[i] != 'b' && char_array[i] != 'd' && char_array[i] != 'p' && char_array[i] != 'q'))\n\t\t\t\t\t\tctr += 1\n # puts 'c'\n #puts ctr\n\n\t\t\t\t\tend\n\t\t\t\tend\n end\nelse\n\t\t\tfor i in 0..array_div\n\t\t\t\tif char_valid.include?(char_array[i])\n\t\t\t\t\tif ((char_array[i] == 'q' && char_array[len-1-i] == 'p') || (char_array[i] == 'p' && char_array[len-1-i] == 'q'))\n\t\t\t\t\t\tctr += 1\n\t\t\t\t\t#\tputs 'a'\n\t\t\t\t\t#\tputs ctr\n elsif ((char_array[i] == 'd' && char_array[len-1-i] == 'b') || (char_array[i] == 'b' && char_array[len-1-i] == 'd'))\n\t\t\t\t\t\tctr += 1\n\t\t\t\t\t#\tputs 'b'\n\t\t\t\t\t#\tputs ctr\n elsif (char_array[i] == char_array[len-1-i] && i != len-1-i && (char_array[i] != 'b' && char_array[i] != 'd' && char_array[i] != 'p' && char_array[i] != 'q'))\n\t\t\t\t\t\tctr += 1\n\t\t\t\t\t#\tputs 'c'\n\t\t\t\t\t#\tputs ctr\n elsif(i == len-1-i && (char_array[i] != 'b' && char_array[i] != 'd' && char_array[i] != 'p' && char_array[i] != 'q'))\n\t\t\t\t\t\tctr += 1\n\t\t\t\t\t#\tputs 'd'\n\t\t\t\t\t#\tputs ctr\n\t\t\t\t\tend\n\t\t\t\tend\n end\n end\n#puts ctr\n if array_mod == 0\n if ctr == array_div\n\t\t\tputs 'TAK'\n else\n puts 'NIE'\n end\n else\n if ctr == array_div+1\n\t\t\tputs 'TAK'\n else\n puts 'NIE'\n end\n end\n\nend "}], "negative_code": [{"source_code": "s = gets.strip\nmirrored = [\"A\", \"b\", \"d\", \"H\", \"I\", \"i\", \"l\", \"M\", \"m\", \"n\", \"O\", \"o\", \"p\", \"T\", \"U\", \"u\", \"V\", \"v\", \"W\", \"w\", \"X\", \"x\", \"Y\"]\nmirror_of = [\"A\", \"d\", \"b\", \"H\", \"I\", \"i\", \"l\", \"M\", \"m\", \"n\", \"O\", \"o\", \"q\", \"T\", \"U\", \"u\", \"V\", \"v\", \"W\", \"w\", \"X\", \"x\", \"Y\"]\ni = 0\nj = s.length - 1\npalindrom = true\nwhile i <= j\n l = mirrored.index s[i]\n r = mirror_of.index( s[j] )\n palindrom = false if l.nil? || r.nil? || l != r\n i += 1\n j -= 1\nend\nif palindrom\n puts \"TAK\"\nelse\n puts \"NIE\"\nend\n"}, {"source_code": "s = gets.strip\nmirrored = [\"A\", \"b\", \"d\", \"H\", \"I\", \"M\", \"O\", \"o\", \"p\", \"T\", \"U\", \"V\", \"v\", \"W\", \"w\", \"X\", \"x\", \"Y\"]\nmirror_of = [\"A\", \"d\", \"b\", \"H\", \"I\", \"M\", \"O\", \"o\", \"q\", \"T\", \"U\", \"V\", \"v\", \"W\", \"w\", \"X\", \"x\", \"Y\"]\ni = 0\nj = s.length - 1\npalindrom = true\nwhile i < j\n l = mirrored.index s[i]\n r = mirror_of.index( s[j] )\n palindrom = false if l.nil? || r.nil? || l != r\n i += 1\n j -= 1\nend\nif palindrom\n puts \"TAK\"\nelse\n puts \"NIE\"\nend\n"}, {"source_code": "s = gets.strip\nmirrored = [\"A\", \"b\", \"d\", \"H\", \"I\", \"i\", \"M\", \"m\", \"n\", \"O\", \"o\", \"p\", \"T\", \"U\", \"u\", \"V\", \"v\", \"W\", \"w\", \"X\", \"x\", \"Y\"]\nmirror_of = [\"A\", \"d\", \"b\", \"H\", \"I\", \"i\", \"M\", \"m\", \"n\", \"O\", \"o\", \"q\", \"T\", \"U\", \"u\", \"V\", \"v\", \"W\", \"w\", \"X\", \"x\", \"Y\"]\ni = 0\nj = s.length - 1\npalindrom = true\nwhile i <= j\n l = mirrored.index s[i]\n r = mirror_of.index( s[j] )\n palindrom = false if l.nil? || r.nil? || l != r\n i += 1\n j -= 1\nend\nif palindrom\n puts \"TAK\"\nelse\n puts \"NIE\"\nend\n"}, {"source_code": "s = gets.strip\nmirrored = [\"A\", \"b\", \"d\", \"H\", \"I\", \"M\", \"O\", \"o\", \"p\", \"T\", \"U\", \"V\", \"v\", \"W\", \"w\", \"X\", \"x\", \"Y\"]\nmirror_of = [\"A\", \"d\", \"b\", \"H\", \"I\", \"M\", \"O\", \"o\", \"q\", \"T\", \"U\", \"V\", \"v\", \"W\", \"w\", \"X\", \"x\", \"Y\"]\ni = 0\nj = s.length - 1\npalindrom = true\nwhile i <= j\n l = mirrored.index s[i]\n r = mirror_of.index( s[j] )\n palindrom = false if l.nil? || r.nil? || l != r\n i += 1\n j -= 1\nend\nif palindrom\n puts \"TAK\"\nelse\n puts \"NIE\"\nend\n"}, {"source_code": "word = gets.to_s\nchar_array = word.chars.to_a\nlen = char_array.length-1\narray_div = len/2\narray_mod = len%2\nchar_valid = \"AbdHIMOopqTUVvWwXxYy\".chars.to_a\n#puts len\n#puts array_div\n#puts array_mod\nctr = 0\nif (1 <= len) && (len <= 1000)\n\tif array_mod == 0\n\t\t\tfor i in 0..array_div-1\n\t\t\t\tif char_valid.include?(char_array[i])\n\t\t\t\t\tif ((char_array[i] == 'q' && char_array[len-1-i] == 'p') || (char_array[i] == 'p' && char_array[len-1-i] == 'q'))\n ctr += 1\n #puts 'a'\n #puts ctr\n elsif ((char_array[i] == 'd' && char_array[len-1-i] == 'b') || (char_array[i] == 'b' && char_array[len-1-i] == 'd'))\n ctr += 1\n #puts 'b'\n #puts ctr\n elsif (char_array[i] == char_array[len-1-i] && (char_array[i] != 'b' && char_array[i] != 'd' && char_array[i] != 'p' && char_array[i] != 'q'))\n\t\t\t\t\t\tctr += 1\n # puts 'c'\n #puts ctr\n\n\t\t\t\t\tend\n\t\t\t\tend\n end\nelse\n\t\t\tfor i in 0..array_div\n\t\t\t\tif char_valid.include?(char_array[i])\n\t\t\t\t\tif ((char_array[i] == 'q' && char_array[len-1-i] == 'p') || (char_array[i] == 'p' && char_array[len-1-i] == 'q'))\n\t\t\t\t\t\tctr += 1\n\t\t\t\t\t#\tputs 'a'\n\t\t\t\t\t#\tputs ctr\n elsif ((char_array[i] == 'd' && char_array[len-1-i] == 'b') || (char_array[i] == 'b' && char_array[len-1-i] == 'd'))\n\t\t\t\t\t\tctr += 1\n\t\t\t\t\t#\tputs 'b'\n\t\t\t\t\t#\tputs ctr\n elsif (char_array[i] == char_array[len-1-i] && i != len-1-i && (char_array[i] != 'b' && char_array[i] != 'd' && char_array[i] != 'p' && char_array[i] != 'q'))\n\t\t\t\t\t\tctr += 1\n\t\t\t\t\t#\tputs 'c'\n\t\t\t\t\t#\tputs ctr\n elsif(i == len-1-i && (char_array[i] != 'b' && char_array[i] != 'd' && char_array[i] != 'p' && char_array[i] != 'q'))\n\t\t\t\t\t\tctr += 1\n\t\t\t\t\t#\tputs 'd'\n\t\t\t\t\t#\tputs ctr\n\t\t\t\t\tend\n\t\t\t\tend\n end\n end\n#puts ctr\n if array_mod == 0\n if ctr == array_div\n\t\t\tputs 'TAK'\n else\n puts 'NIE'\n end\n else\n if ctr == array_div+1\n\t\t\tputs 'TAK'\n else\n puts 'NIE'\n end\n end\n\nend "}, {"source_code": "word = gets.to_s\nchar_array = word.chars.to_a\nlen = char_array.length-1\narray_div = len/2\narray_mod = len%2\nchar_valid = \"AbdHIMOopqTUVvWwXxYy\".chars.to_a\n#puts len\n#puts array_div\n#puts array_mod\nctr = 0\nif (1 <= len) && (len <= 1000)\n\tif array_mod == 0\n\t\t\tfor i in 0..array_div-1\n\t\t\t\tif char_valid.include?(char_array[i])\n\t\t\t\t\tif ((char_array[i] == 'q' && char_array[len-1-i] == 'p') || (char_array[i] == 'p' && char_array[len-1-i] == 'q'))\n ctr += 1\n #puts 'a'\n #puts ctr\n elsif ((char_array[i] == 'd' && char_array[len-1-i] == 'b') || (char_array[i] == 'b' && char_array[len-1-i] == 'd'))\n ctr += 1\n #puts 'b'\n #puts ctr\n elsif (char_array[i] == char_array[len-1-i] && (char_array[i] != 'b' && char_array[i] != 'd' && char_array[i] != 'p' && char_array[i] != 'q'))\n\t\t\t\t\t\tctr += 1\n # puts 'c'\n #puts ctr\n\n\t\t\t\t\tend\n\t\t\t\tend\n end\nelse\n\t\t\tfor i in 0..array_div\n\t\t\t\tif char_valid.include?(char_array[i])\n\t\t\t\t\tif ((char_array[i] == 'q' && char_array[len-1-i] == 'p') || (char_array[i] == 'p' && char_array[len-1-i] == 'q'))\n\t\t\t\t\t\tctr += 1\n\t\t\t\t\t#\tputs 'a'\n\t\t\t\t\t#\tputs ctr\n elsif ((char_array[i] == 'd' && char_array[len-1-i] == 'b') || (char_array[i] == 'b' && char_array[len-1-i] == 'd'))\n\t\t\t\t\t\tctr += 1\n\t\t\t\t\t#\tputs 'b'\n\t\t\t\t\t#\tputs ctr\n elsif (char_array[i] == char_array[len-1-i] && i != len-1-i && (char_array[i] != 'b' && char_array[i] != 'd' && char_array[i] != 'p' && char_array[i] != 'q'))\n\t\t\t\t\t\tctr += 1\n\t\t\t\t\t#\tputs 'c'\n\t\t\t\t\t#\tputs ctr\n elsif(i == len-1-i && (char_array[i] != 'b' && char_array[i] != 'd' && char_array[i] != 'p' && char_array[i] != 'q'))\n\t\t\t\t\t\tctr += 1\n\t\t\t\t\t#\tputs 'd'\n\t\t\t\t\t#\tputs ctr\n\t\t\t\t\tend\n\t\t\t\tend\n end\n end\n#puts ctr\n if array_mod == 0\n if ctr == array_div\n\t\t\tputs 'TAK'\n else\n puts 'NEI'\n end\n else\n if ctr == array_div+1\n\t\t\tputs 'TAK'\n else\n puts 'NEI'\n end\n end\n\nend "}], "src_uid": "bec2349631158b7dbfedcaededf65cc2"} {"nl": {"description": "Alyona has recently bought a miniature fridge that can be represented as a matrix with $$$h$$$ rows and $$$2$$$ columns. Initially there is only one shelf at the bottom of the fridge, but Alyona can install arbitrary number of shelves inside the fridge between any two rows. A shelf is two cells wide, does not occupy any space but separates the inside of the fridge to the lower and upper part. An example of a fridge with $$$h = 7$$$ and two shelves. The shelves are shown in black. The picture corresponds to the first example. Alyona has $$$n$$$ bottles of milk that she wants to put in the fridge. The $$$i$$$-th bottle is $$$a_i$$$ cells tall and $$$1$$$ cell wide. She can put a bottle on some shelf if the corresponding space above the shelf is at least as tall as the bottle. She can not put a bottle on top of another bottle (if there is no shelf between them). Two bottles can not share a cell.Alyona is interested in the largest integer $$$k$$$ such that she can put bottles $$$1$$$, $$$2$$$, ..., $$$k$$$ in the fridge at the same time. Find this largest $$$k$$$.", "input_spec": "The first line contains two integers $$$n$$$ and $$$h$$$ ($$$1 \\le n \\le 10^3$$$, $$$1 \\le h \\le 10^9$$$) — the number of bottles and the height of the fridge. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 \\le a_i \\le h$$$) — the heights of the bottles.", "output_spec": "Print the single integer $$$k$$$ — the maximum integer such that Alyona can put the bottles $$$1$$$, $$$2$$$, ..., $$$k$$$ in the fridge at the same time. If Alyona can put all bottles in the fridge, print $$$n$$$. It is easy to see that Alyona can always put at least one bottle in the fridge.", "sample_inputs": ["5 7\n2 3 5 4 1", "10 10\n9 1 1 1 1 1 1 1 1 1", "5 10\n3 1 4 2 4"], "sample_outputs": ["3", "4", "5"], "notes": "NoteOne of optimal locations in the first example is shown on the picture in the statement.One of optimal locations in the second example is shown on the picture below. One of optimal locations in the third example is shown on the picture below. "}, "positive_code": [{"source_code": "f=\"gets.split.map &:to_i\"\nn,k=eval f\na=eval f\nn-=1 while k<(a[0...n].sort.values_at(*(-n|1...0).step(2)).reduce &:+)\np n"}, {"source_code": "n,k = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\nt = a.sort.reverse\nn.times do |i|\n len = 0\n flg = true\n t.each do |j|\n len += j if flg\n flg = !flg\n end\n if len > k\n t.delete_at(t.find_index(a[-i-1]))\n else\n p n-i\n break\n end\nend\n"}, {"source_code": "N, H = gets.split.map(&:to_i)\nA = gets.split.map(&:to_i)\n\ndef f(k)\n list = A.take(k).sort\n height = 0\n\n until list.empty?\n a, b = list.pop(2)\n h = [a, b || 0].max\n\n height += h\n end\n\n height <= H\nend\n\nputs [*1..N].reverse.bsearch { |k| f(k) }\n"}, {"source_code": "n,h = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\nmin = 0\nmax = n+1\nwhile max-min > 1\n mid = (min+max)/2\n b = a[0,mid].sort.reverse\n x = 0\n for i in 0..(mid-1)/2\n x += b[i*2]\n end\n if x > h\n max = mid\n else\n min = mid\n end\nend\nputs min"}, {"source_code": "(n,h),a=$<.map{|s|s.split.map &:to_i}\np (1...n).find{|i|r=i%2;h max \nmax=temp\nend\ntemp=1\nend\nend\nif temp > max \nmax=temp\nend\nputs \"#{max}\""}, {"source_code": "a=gets.chomp.to_i\nb=gets.chomp\nif b==\"01\"\nt=-1\nelse\nt=1\nend\ntemp=1\nmax=1\n(a-1).times do\nte=gets.chomp\nif (te==\"01\" && t==1 )|| (te==\"10\" && t==-1)\ntemp+=1\nelsif (te==\"01\" && t==-1) || (te==\"10\" && t==1)\nt=t*-1\nif temp > max \nmax=temp\nend\ntemp=1\nend\nend\nif temp > max \nmax=temp\nend\nputs \"#{max}\""}, {"source_code": "a=gets.chomp.to_i\nb=gets.chomp\nif b==\"01\"\nt=-1\nelse\nt=1\nend\ntemp=1\nmax=1\n(a-1).times do\nte=gets.chomp\nif (te==\"01\" && t==-1 )|| (te==\"10\" && t==1)\ntemp+=1\nelsif (te==\"01\" && t==1) || (te==\"10\" && t==-1)\nt=t*-1\nif temp > max \nmax=temp\nend\ntemp=1\nend\nend\n\nputs \"#{max}\""}, {"source_code": "t = gets.to_i\nlast = '-'\nans = 0\nt.times do \n g = gets.strip\n if g[1] == last\n ans += 1\n else\n last = g[1]\n end\nend\nputs (t == 1 ? 0 : ans)"}, {"source_code": "t = gets.to_i\nlast = '-'\nans = 0\nt.times do \n g = gets.strip\n if g[1] == last\n ans += 1\n else\n last = g[1]\n end\nend\nputs ans \n"}, {"source_code": "t = gets.to_i\nlast = '-'\nans = 0\nt.times do \n g = gets.strip\n if g[1] == last\n ans += 1\n else\n last = g[1]\n end\nend\nputs (t == 1 ? 1 : ans) \n"}, {"source_code": "num = gets.chomp.to_i\ninput = []\nans = 0\n\n(0...num).each do |iterNum|\n\tinput << gets.chomp\nend\n\n(0...(num - 1)).each do |iterNum|\n\tans += 1 if input[iterNum] == input[iterNum + 1]\nend\n\nif num == 1\n\tputs 1\n\texit\nend\n\nputs ans"}, {"source_code": "num = gets.chomp.to_i\ninput = []\nans = 0\n\n(0...num).each do |iterNum|\n\tinput << gets.chomp\nend\n\n(0...(num - 1)).each do |iterNum|\n\tans += 1 if input[iterNum] == input[iterNum + 1]\nend\n\nputs ans"}, {"source_code": "num = gets.chomp.to_i\ninput = []\nans = 0\n\n(0...num).each do |iterNum|\n\tinput << gets.chomp\nend\n\n(0...(num - 1)).each do |iterNum|\n\tans += 1 if input[iterNum] == input[iterNum + 1]\nend"}, {"source_code": "\t\n\n\t\nn = gets.strip.to_i\n\ntmp = 1\ncnt = 0 \ni = 0\nlst = gets.strip\n\nwhile i < n-1\n\tfst = gets.strip\n\n\tif (fst[0] != lst[1])\n\t\ttmp += 1\n\telse\n\t\tcnt = (cnt > tmp ? cnt : tmp)\n\t\ttmp = 1 \n\tend\n\tlst = fst\n\ti += 1\nend\n\nputs cnt\n\n\n\n\n\n\n\n"}, {"source_code": "p $<.drop(1).each_cons(2).count{|a,b|a!=b}"}, {"source_code": "n = gets.to_i\n\nlast = \"00\"\nans = 0\n\n(0...n).each do |i|\n cur = gets.to_s\n if (last == cur)\n ans += 1\n end\n last = cur\nend\n\nputs ans"}, {"source_code": "n = gets.to_i\n\nlast = \"00\"\nans = 0\n\n(0...n).each do |i|\n cur = gets.to_s\n if (last == cur)\n ans += 1\n end\n last = cur\nend\n\nif (ans == 0)\n puts 1\nelse \n puts ans\nend"}, {"source_code": "n = $stdin.readline.to_i\na = $stdin.readline.scan(/./).map(&:to_i)\nk = 0\n\n(n-1).times do\n b = $stdin.readline.scan(/./).map(&:to_i)\n k+=1 if a.last != b.first\n a = b\nend\n\nk = 1 if k==0\nputs k"}, {"source_code": "n = $stdin.readline.to_i\na = $stdin.readline.scan(/./).map(&:to_i)\nk = 0\n\n(n-1).times do\n b = $stdin.readline.scan(/./).map(&:to_i)\n k+=1 if a.last != b.first\n a = b\nend\n\nputs k"}, {"source_code": "n = gets.to_i\nret, temp, prev = 0, 0, nil\nn.times do\n mag = gets\n temp += 1\n if mag != prev || prev == nil\n \tret = [ret, temp].max\n \ttemp = 0\n end\n prev = mag\nend\nputs ret"}, {"source_code": "n = gets.to_i\na = []\nn.times do \n a << gets\nend\nis_one = a[0] == \"10\"\ncnt = 0\na.each do |mag|\n next if mag == a[0]\n if (is_one && mag[0] == '0') || (!is_one && mag[0]=='1')\n cnt+=1\n end\nend\nputs(cnt+1)"}], "src_uid": "6c52df7ea24671102e4c0eee19dc6bba"} {"nl": {"description": "Everybody knows that opposites attract. That is the key principle of the \"Perfect Matching\" dating agency. The \"Perfect Matching\" matchmakers have classified each registered customer by his interests and assigned to the i-th client number ti ( - 10 ≤ ti ≤ 10). Of course, one number can be assigned to any number of customers.\"Perfect Matching\" wants to advertise its services and publish the number of opposite couples, that is, the couples who have opposite values of t. Each couple consists of exactly two clients. The customer can be included in a couple an arbitrary number of times. Help the agency and write the program that will find the sought number by the given sequence t1, t2, ..., tn. For example, if t = (1,  - 1, 1,  - 1), then any two elements ti and tj form a couple if i and j have different parity. Consequently, in this case the sought number equals 4.Of course, a client can't form a couple with him/herself.", "input_spec": "The first line of the input data contains an integer n (1 ≤ n ≤ 105) which represents the number of registered clients of the \"Couple Matching\". The second line contains a sequence of integers t1, t2, ..., tn ( - 10 ≤ ti ≤ 10), ti — is the parameter of the i-th customer that has been assigned to the customer by the result of the analysis of his interests.", "output_spec": "Print the number of couples of customs with opposite t. The opposite number for x is number  - x (0 is opposite to itself). Couples that only differ in the clients' order are considered the same. Note that the answer to the problem can be large enough, so you must use the 64-bit integer type for calculations. Please, do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specificator.", "sample_inputs": ["5\n-3 3 0 0 3", "3\n0 0 0"], "sample_outputs": ["3", "3"], "notes": "NoteIn the first sample the couples of opposite clients are: (1,2), (1,5) и (3,4).In the second sample any couple of clients is opposite."}, "positive_code": [{"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nmp = Hash.new\nmp.default = 0\na.each { |i|\n mp[i] += 1\n}\nans = mp[0] * (mp[0]-1)/2\n10.times { |i|\n ans += mp[i+1] * mp[-i-1]\n}\np ans\n"}, {"source_code": "\nn=gets\nt=gets.split.map(&:to_i)\ncnt=Hash.new\ncnt.default=0\nt.each{|i|\n cnt[i]+=1\n}\nans=cnt[0]*(cnt[0]-1)/2\nfor i in 1..10\n ans+=cnt[i]*cnt[-i]\nend\np ans\n"}, {"source_code": "#!/usr/bin/env ruby\n\nn = gets.to_i\narr = gets.split.map(&:to_i)\ndict = Hash.new(0)\narr.each do |i| \n dict[i] += 1\nend\n\nans = 0\nans += dict[0]*(dict[0]-1)/2 if dict[0] >= 2\n(1..10).each do |i|\n ans += dict[i]*dict[-i]\nend\n\np ans\n"}, {"source_code": "gets\na = {}\ngets.chomp.split(' ').each do |e|\n if not a.has_key?(e)\n a[e] = 0\n end\n a[e] += 1\nend\n\nret = 0\n\nif a.has_key?(\"0\")\n n = a[\"0\"] - 1\n ret += n * (n + 1) / 2\nend\n\n(1..10).map{|e| e.to_s}.each do |i|\n if a.has_key?(i) and a.has_key?('-' + i)\n ret += a[i] * a['-' + i]\n end\nend\n\np ret\n"}, {"source_code": "a = Hash.new(0)\ngets\ngets.split.map(&:to_i).each { |e| a[e]+=1 }\np ((1..10).map { |x| a[x]*a[-x] }.inject(:+)) + a[0]*(a[0]-1)/2"}, {"source_code": "m = gets.chomp.to_i\n\na = gets.chomp.split(' ').map &:to_i\n\nh = Hash.new(0)\na.each { | v | h.store(v, h[v]+1) }\n\nk = 2\nn = h[0]\nif k > n\n t = 0\nelse\n t = n*(n-1)/2\nend\n\nh.keys.keep_if{|x| x>0}.each do |key|\n t+=h[-key]*h[key]\nend\n\nputs t"}, {"source_code": "n=gets.to_i\ndata=gets.split.map(&:to_i)\n\nhs=Hash.new(0)\ndata.each do |val|\n\ths[val]+=1\nend\n\nans=hs[0]*(hs[0]-1)/2\n(-10..-1).each do |i|\n\ta,b=hs[i], hs[i*(-1)]\n\tans+=a*b\nend\n\nputs ans\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\ns = 0\n(1..10).each{|i|\n s = s + a.find_all{|e| e == i}.size * a.find_all{|e| e == -i}.size\n a = a - [i, -i]\n}\nb = a.find_all{|e| e == 0}.size\ns = s + b * (b - 1)/2\np s"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\ns = 0\n(1..10).each{|i|\n s = s + a.find_all{|e| e == i}.size * a.find_all{|e| e == -i}.size\n a = a - [i, -i]\n}\nb = a.size\ns = s + b * (b - 1)/2\np s"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\ns = 0\n(1..10).each{|i|\n s = s + a.find_all{|e| e == i}.size * a.find_all{|e| e == -i}.size\n}\nb = a.find_all{|e| e == 0}.size\ns = s + b * (b - 1)/2\np s"}, {"source_code": "number = gets.strip.to_i\ncount = {}\ngets.strip.split(' ').map(&:to_i).each do |num|\n count[\"#{num}\"] ||= 0\n count[\"#{num}\"] += 1\nend\n\nresult = 0\n1.upto(10).each do |val|\n count[\"#{val}\"] ||= 0\n count[\"#{-val}\"] ||= 0\n result += count[\"#{val}\"] * count[\"#{-val}\"]\nend\n\nif !count[\"0\"].nil? && count[\"0\"] >= 2\n result += (count[\"0\"] - 1) * count[\"0\"] / 2\nend\n\nputs result\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\n\nbin = Hash.new(0)\na.each{|x| bin[x] += 1}\n\nnum = 0\n1.upto(10){|x| num += bin[x]*bin[-x]}\nnum += bin[0]*(bin[0]-1)/2\n\np num\n"}, {"source_code": "gets # Skip the first line\nhm = gets.split.map { |x| x.to_i }.inject(Hash.new(0)) { |h, e| h[e] += 1; h }\nc = 0\nfor i in -10...0\n if hm.has_key?(i) && hm.has_key?(-i)\n c += hm[i] * hm[-i]\n end\nend\nif hm.has_key?(0)\n n = hm[0] - 1\n c += n * (n + 1) / 2\nend\nputs c"}, {"source_code": "c=Array.new(30,0)\ngets\ngets.split.map(&:to_i).each{|t|c[t]+=1}\np 1.upto(10).map{|t|c[t]*c[-t]}.inject(:+)+(c[0]*(c[0]-1)/2)"}], "negative_code": [{"source_code": "m = gets.chomp.to_i\n\na = gets.chomp.split(' ').map &:to_i\n\nh = Hash.new(0)\na.each { | v | h.store(v, h[v]+1) }\n\nk = 2\nn = h[0]\nif k > n\n t = 0\nelse\n t = (1..n).inject(:*)/2/((1..(n-2)).inject(:+) || 1)\nend\n\nh.keys.keep_if{|x| x>0}.each do |key|\n t+=h[-key]*h[key]\nend\n\nputs t"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\ns = 0\nn.times{|i|\n (i..(n-1)).each{ |j|\n if a[i] + a[j] == 0 then\n s = s + 1 \n end \n } \n}\np s"}], "src_uid": "f3dde329830d8c479b3dab9d5df8baf5"} {"nl": {"description": "You are given an array $$$a$$$ of $$$n$$$ elements. You can apply the following operation to it any number of times: Select some subarray from $$$a$$$ of even size $$$2k$$$ that begins at position $$$l$$$ ($$$1\\le l \\le l+2\\cdot{k}-1\\le n$$$, $$$k \\ge 1$$$) and for each $$$i$$$ between $$$0$$$ and $$$k-1$$$ (inclusive), assign the value $$$a_{l+k+i}$$$ to $$$a_{l+i}$$$. For example, if $$$a = [2, 1, 3, 4, 5, 3]$$$, then choose $$$l = 1$$$ and $$$k = 2$$$, applying this operation the array will become $$$a = [3, 4, 3, 4, 5, 3]$$$.Find the minimum number of operations (possibly zero) needed to make all the elements of the array equal.", "input_spec": "The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 2 \\cdot 10^4$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \\leq n \\leq 2 \\cdot 10^5$$$) — the length of the array. The second line of each test case consists of $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\leq a_i \\leq n$$$) — the elements of the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "Print $$$t$$$ lines, each line containing the answer to the corresponding test case — the minimum number of operations needed to make equal all the elements of the array with the given operation.", "sample_inputs": ["5\n\n3\n\n1 1 1\n\n2\n\n2 1\n\n5\n\n4 4 4 2 4\n\n4\n\n4 2 1 3\n\n1\n\n1"], "sample_outputs": ["0\n1\n1\n2\n0"], "notes": "NoteIn the first test, all elements are equal, therefore no operations are needed.In the second test, you can apply one operation with $$$k=1$$$ and $$$l=1$$$, set $$$a_1 := a_2$$$, and the array becomes $$$[1, 1]$$$ with $$$1$$$ operation.In the third test, you can apply one operation with $$$k=1$$$ and $$$l=4$$$, set $$$a_4 := a_5$$$, and the array becomes $$$[4, 4, 4, 4, 4]$$$.In the fourth test, you can apply one operation with $$$k=1$$$ and $$$l=3$$$, set $$$a_3 := a_4$$$, and the array becomes $$$[4, 2, 3, 3]$$$, then you can apply another operation with $$$k=2$$$ and $$$l=1$$$, set $$$a_1 := a_3$$$, $$$a_2 := a_4$$$, and the array becomes $$$[3, 3, 3, 3]$$$.In the fifth test, there is only one element, therefore no operations are needed."}, "positive_code": [{"source_code": "t = gets.chomp.to_i\r\n\r\nfor i in (1..t)\r\n n = gets.chomp.to_i\r\n a = (gets.chomp.split.map {|e| e.to_i}).reverse\r\n x = 0\r\n for j in (1..n-1)\r\n if a[j] != a[0]\r\n break\r\n end\r\n x += 1\r\n end\r\n ans = 0\r\n until x >= n-1\r\n # Aplique a operacao em que os valores (0, x) são copiados para (x+1, 2x+1)\r\n x = [n-1, 2*x+1].min\r\n ans += 1\r\n for j in (x+1..n-1)\r\n if a[j] != a[0]\r\n break\r\n end\r\n x += 1\r\n end\r\n end\r\n puts ans\r\nend\r\n"}, {"source_code": "gets.to_i.times do\n n = gets.to_i\n xs = gets.split.map(&:to_i)\n\n ans = 0\n g = n - 1\n\n while g > 0\n if xs[g - 1] == xs[n - 1]\n g -= 1\n else\n g = 2 * g - n\n ans += 1\n end\n end\n\n p ans\nend\n"}], "negative_code": [], "src_uid": "dc10b869293df2b7a557262c805635ba"} {"nl": {"description": "Vanya is doing his maths homework. He has an expression of form , where x1, x2, ..., xn are digits from 1 to 9, and sign represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.", "input_spec": "The first line contains expression s (1 ≤ |s| ≤ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs  +  and  * . The number of signs  *  doesn't exceed 15.", "output_spec": "In the first line print the maximum possible value of an expression.", "sample_inputs": ["3+5*7+8*4", "2+3*5", "3*4*5"], "sample_outputs": ["303", "25", "60"], "notes": "NoteNote to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.Note to the second sample test. (2 + 3) * 5 = 25.Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60)."}, "positive_code": [{"source_code": "def max(a,b)\n return a>b ? a : b\nend\n\ns=gets\nn=s.length\nres=eval(s)\nfor i in -1..n-1 do\n if i==-1 || s[i]=='*'\n for j in i+1..n do\n if j==n || s[j]=='*'\n res=max(res,eval(s[0...i+1]+'('+s[i+1...j]+')'+s[j...n]))\n end\n end\n end\nend\nputs res\n"}, {"source_code": "s = gets.chomp.to_s\ns = \"1*\" + s + \"*1\"\n#p s\na = (0...s.length).find_all{ |i| s[i] == '*' }\nans = -1\nfor i in a\n for j in a\n if( i != j )\n if( i < j )\n k = i\n else k = j\n end\n l = i + j - k\n x = s[0..k] + \"(\" + s[k+1..l-1] + \")\" + s[l..s.length]\n # p \"#{x} #{i} #{j} #{k} #{l} \"\n if( eval(x) > ans )\n ans = eval(x)\n end\n end\n end\nend\nputs ans\n"}, {"source_code": "c = gets.strip\nmax = eval(c)\n\nlpos = [0]\nrpos = [c.length]\nc.chars.each_with_index do |ci, idx|\n if ci == '*'\n lpos.push idx + 1\n rpos.push idx\n end\nend\n\nlpos.each do |l|\n rpos.each do |r|\n next if l > r\n exp = c[0...l] + '(' + c[l...r] + ')' + c[r...c.length]\n max = [max, eval(exp)].max\n end\nend\np max"}], "negative_code": [{"source_code": "def max(a,b)\n return a>b ? a : b\nend\n\ns=gets\nn=s.length\nres=eval(s)\nfor i in -1..n-1 do\n if i==-1 || s[i]=='*'\n for j in i+1..n do\n if j==n || s[j]=='*'\n res=max(res,eval(s[0,i+1]+'('+s[i+1,j]+')'+s[j,n-1]))\n end\n end\n end\nend\nputs res\n"}], "src_uid": "39dbd405be19c5a56c2b97b28e0edf06"} {"nl": {"description": "The city administration of IT City decided to fix up a symbol of scientific and technical progress in the city's main square, namely an indicator board that shows the effect of Moore's law in real time.Moore's law is the observation that the number of transistors in a dense integrated circuit doubles approximately every 24 months. The implication of Moore's law is that computer performance as function of time increases exponentially as well.You are to prepare information that will change every second to display on the indicator board. Let's assume that every second the number of transistors increases exactly 1.000000011 times.", "input_spec": "The only line of the input contains a pair of integers n (1000 ≤ n ≤ 10 000) and t (0 ≤ t ≤ 2 000 000 000) — the number of transistors in the initial time and the number of seconds passed since the initial time.", "output_spec": "Output one number — the estimate of the number of transistors in a dence integrated circuit in t seconds since the initial time. The relative error of your answer should not be greater than 10 - 6.", "sample_inputs": ["1000 1000000"], "sample_outputs": ["1011.060722383550382782399454922040"], "notes": null}, "positive_code": [{"source_code": "a,b=gets.split.map(&:to_f);p a*1.000000011**b"}, {"source_code": "factor = 1.000000011\nn, t = gets.split(\" \").map { |x| x.to_i }\n\nans = n * factor ** t\nputs ans.round(16)"}, {"source_code": "n, t = gets.split.map(&:to_i)\nputs(n * (1.000000011 ** t))"}, {"source_code": "a=gets.chomp.split(' ')\nb=a[0].to_i*(1.000000011**a[1].to_i)\nputs \"#{b}\""}, {"source_code": "n, t=gets.split.map &:to_i\np 1.000000011**t * n"}, {"source_code": "arg = STDIN.gets\nargs = arg.split\nn = args[0].to_i\nt = args[1].to_i\n\nprintf(\"%.6f\", (n * (1.000000011 ** t)))\nputs"}, {"source_code": "factor = 1.000000011\nn, t = gets.split(\" \").map { |x| x.to_i }\n\nans = n * factor ** t\nputs ans.round(16)"}, {"source_code": "inp = gets.chomp.split(' ');\nn = inp[0].to_i\nt = inp[1].to_i\n\nans = n * (1.000000011 ** t)\n\np ans\n"}, {"source_code": "n , t = gets.chomp.split.map(&:to_i)\nans = n\nref = 1.000000011\nhelp = ref ** t\nans = ans * help\nputs ans\n"}, {"source_code": "a=gets.chomp.split(' ')\nb=a[0].to_i*(1.000000011**a[1].to_i)\nputs \"#{b}\" "}, {"source_code": "a,b=gets.split.map(&:to_f);p a*(1+11e-9)**b\n"}, {"source_code": "a,b=gets.split.map(&:to_f);p a*(1+11e-9)**b"}, {"source_code": "a,b=gets.split.map(&:to_f);p a*1.000000011**b"}, {"source_code": "a,b=gets.split.map(&:to_f);p a*(1+11e-9)**b\n"}], "negative_code": [], "src_uid": "36ad784f23bd1e8e579052642a6e9244"} {"nl": {"description": "You are given a text that consists of lowercase Latin letters, spaces and punctuation marks (dot, comma, exclamation mark and question mark). A word is defined as a sequence of consecutive Latin letters.Your task is to add spaces to the text by the following rules: if there is no punctuation mark between two words, then they should be separated by exactly one space there should be no spaces before each punctuation mark there should be exactly one space after each punctuation mark It is guaranteed that there is at least one word between any two punctuation marks. The text begins and ends with a Latin letter.", "input_spec": "The input data contains of a single non-empty line — the text whose length is no more than 10000 characters.", "output_spec": "Print the text, edited according to the rules. In this problem you should follow the output format very strictly. For example, extra space at the end of the output line is considered as wrong answer. Note that a newline character at the end of the line doesn't matter.", "sample_inputs": ["galileo galilei was an italian physicist ,mathematician,astronomer", "galileo was born in pisa"], "sample_outputs": ["galileo galilei was an italian physicist, mathematician, astronomer", "galileo was born in pisa"], "notes": null}, "positive_code": [{"source_code": "def delp(s)\n k = -1\n while (k = s.index(\" \", k+1)) do\n k += 1\n\n while s[k] == \" \" do\n s = s[0...k] + s[k+1..-1]\n end\n end\n return s\nend\n\ndef cpz (s,z)\n str = \"\"\n while s.size > 0 do\n if (k = s.index(z)) then\n if s[k+1] != \" \" then\n s = s[0..k] + \" \" + s[k+1..-1]\n end\n if s[k-1] == \" \" then\n s = s[0...k-1] + s[k..-1]\n end\n str += s[0..k]\n s = s[k+1..-1]\n else\n str += s\n s = \"\"\n end\n end\n return str\nend\n\ns = gets.chomp\ns = delp(s)\ns = cpz(s,\".\")\ns = cpz(s,\",\")\ns = cpz(s,\"!\")\ns = cpz(s,\"?\")\nputs s"}, {"source_code": "input=gets\nwhile true\n back=input.length\n input.gsub!(\" \",\" \");\n break if back==input.length\nend\ninput.gsub!(\" !\",\"!\");\ninput.gsub!(\" ?\",\"?\");\ninput.gsub!(\" .\",\".\");\ninput.gsub!(\" ,\",\",\");\n\nfor i in 0..25\n t=\"abcdefghijklmnopqrstuvwxyz\"[i]\n input.gsub!(\"!\"+t,\"! \"+t);\n input.gsub!(\"?\"+t,\"? \"+t);\n input.gsub!(\".\"+t,\". \"+t);\n input.gsub!(\",\"+t,\", \"+t);\nend\nprint input\n"}, {"source_code": "\nwhile s = gets\n s.chomp!\n s.gsub!(/[\\s]+/, \" \")\n s.gsub!(/\\s+([\\.,!?])/, '\\1')\n s.gsub!(/([\\.,!?])\\s+/, '\\1')\n s.gsub!(/([\\.,!?])/, '\\1 ')\n puts s\nend\n\n"}, {"source_code": "puts gets().gsub(/[ ]+/, ' ').gsub(/[ ]*([.,!?])[ ]*/, '\\1 ')\n"}, {"source_code": "str=gets\ns=str.chars.to_a\nres=''\ni=0\ndef isPunc(str)\n a=(\"a\"..\"z\").to_a+(\"A\"..\"X\").to_a\n a.each{|c|\n if str==c then\n return false\n end\n }\n return true\nend\nwhile i 0\n\n (0...s.size).each do |i|\n if s[i] == '.' or s[i] == ',' or s[i] == '!' or s[i] == '?'\n if i == 0\n fin = \"#{fin[0...fin.length-1]}#{s[i]} \"\n else\n fin += \"#{s[i]} \"\n end\n else\n fin += s[i]\n end\n end\n end\n\n puts fin\nend"}, {"source_code": "def punctuation\n str = \"galileo galilei was an italian physicist ,mathematician,astronomer\"#gets.strip\n\n str = str.split(' ').map{|s| s.strip}\n fin = ''\n str.each_with_index do |s, ind|\n fin += \" \" if ind > 0\n\n (0...s.size).each do |i|\n if s[i] == '.' or s[i] == ',' or s[i] == '!' or s[i] == '?'\n fin = \"#{fin[0...fin.length-1]}#{s[i]} \"\n else\n fin += s[i]\n end\n end\n end\n\n p fin\nend\n\npunctuation"}, {"source_code": "def punctuation\n str = gets.strip\n\n str = str.split(' ').map{|s| s.strip}\n fin = ''\n str.each_with_index do |s, ind|\n fin += \" \" if ind > 0\n\n (0...s.size).each do |i|\n if s[i] == '.' or s[i] == ',' or s[i] == '!' or s[i] == '?'\n if i == 0\n fin = \"#{fin[0...fin.length-1]}#{s[i]} \"\n else\n fin += \"#{s[i]} \"\n end\n else\n fin += s[i]\n end\n end\n end\n\n puts fin# == 'galileo galilei was an italian physicist, mathematician, astronomer'\nend"}, {"source_code": "def punctuation\n str = gets.strip\n\n str = str.split(' ').map{|s| s.strip}\n fin = ''\n str.each_with_index do |s, ind|\n fin += \" \" if ind > 0\n\n (0...s.size).each do |i|\n if s[i] == '.' or s[i] == ',' or s[i] == '!' or s[i] == '?'\n fin = \"#{fin[0...fin.length-1]}#{s[i]} \"\n else\n fin += s[i]\n end\n end\n end\n\n puts fin\nend\n\npunctuation"}, {"source_code": "def punctuation\n str = gets.strip\n\n str = str.split(' ').map{|s| s.strip}.join(' ')\n fin = ''\n\n i = 0\n while i < str.length\n if str[i] == '.' or str[i] == ',' or str[i] == '!' or str[i] == '?'\n if str[i-1] == ' '\n str[i-1] = ''\n i -= 1\n end\n\n unless str[i+1] == ' '\n str = \"#{str[0..i]} #{str[i+1..-1]}\"\n i += 1\n end\n end\n i += 1\n end\n\n print str\nend"}, {"source_code": "def punctuation\n str = gets.strip\n\n str = str.split(' ').map{|s| s.strip}\n fin = ''\n str.each_with_index do |s, ind|\n fin += \" \" if ind > 0\n\n (0...s.size).each do |i|\n if s[i] == '.' or s[i] == ',' or s[i] == '!' or s[i] == '?'\n if i == 0\n fin = \"#{fin[0...fin.length-1]}#{s[i]} \"\n else\n fin += \"#{s[i]} \"\n end\n else\n fin += s[i]\n end\n end\n end\n\n print fin\nend\n\npunctuation"}, {"source_code": "def punctuation\n str = gets.strip\n\n str = str.split(' ').map{|s| s.strip}\n fin = ''\n str.each_with_index do |s, ind|\n fin += \" \" if ind > 0\n\n (0...s.size).each do |i|\n if s[i] == '.' or s[i] == ',' or s[i] == '!' or s[i] == '?'\n fin = \"#{fin[0...fin.length-1]}#{s[i]} \"\n else\n fin += s[i]\n end\n end\n end\n\n p fin\nend\n\npunctuation"}, {"source_code": "input = STDIN.read\n\ninput.gsub!(/ +/, ' ')\n\ninput.each_char.with_index do |v, k|\n\tif v == \",\"\n\t\tif input[k - 1] == \" \"\n\t\t\tinput[k] = \" \"\n\t\t\tinput[k - 1] = \",\"\n\t\telsif input[k + 1] != \" \"\n\t\t\tinput[k] = \", \"\n\t\tend\n\tend\nend\n\nSTDOUT.puts input"}, {"source_code": "input = STDIN.read.gsub(/ +/, ' ')\n\nchars = \".,?!:;\"\n\ninput.each_char.with_index do |v, k|\n\tif chars.include? v\n\t\tif input[k - 1] == \" \"\n\t\t\tinput[k - 1] = v\n\t\t\tinput[k + 1] != \" \" ? input[k] = \" \" : input[k] = \"\"\n\t\telsif input[k + 1] != \" \"\n\t\t\tinput[k] = v + \" \"\n\t\tend\n\tend\nend\n\nSTDOUT.puts input"}], "src_uid": "8c26daa1eed2078af3b32737da0a0f84"} {"nl": {"description": "New Year is coming and you are excited to know how many minutes remain before the New Year. You know that currently the clock shows $$$h$$$ hours and $$$m$$$ minutes, where $$$0 \\le hh < 24$$$ and $$$0 \\le mm < 60$$$. We use 24-hour time format!Your task is to find the number of minutes before the New Year. You know that New Year comes when the clock shows $$$0$$$ hours and $$$0$$$ minutes.You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 1439$$$) — the number of test cases. The following $$$t$$$ lines describe test cases. The $$$i$$$-th line contains the time as two integers $$$h$$$ and $$$m$$$ ($$$0 \\le h < 24$$$, $$$0 \\le m < 60$$$). It is guaranteed that this time is not a midnight, i.e. the following two conditions can't be met at the same time: $$$h=0$$$ and $$$m=0$$$. It is guaranteed that both $$$h$$$ and $$$m$$$ are given without leading zeros.", "output_spec": "For each test case, print the answer on it — the number of minutes before the New Year.", "sample_inputs": ["5\n23 55\n23 0\n0 1\n4 20\n23 59"], "sample_outputs": ["5\n60\n1439\n1180\n1"], "notes": null}, "positive_code": [{"source_code": "readline.to_i.times do\n h, m = readline.split.map(&:to_i)\n p((24-h)*60-m)\nend"}, {"source_code": "gets.to_i.times do\n\ta, b = gets.split.map(&:to_i)\n\tputs 24 * 60 - (a * 60 + b)\nend"}, {"source_code": "gets.strip.to_i.times do\n h, m = gets.strip.split(' ').map(&:to_i)\n puts 24 * 60 - (h * 60 + m)\nend"}, {"source_code": "gets.to_i.times do\n h, m = gets.split.map(&:to_i)\n puts (23*60+60)-(h*60+m)\nend\n"}, {"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\ndef big(a,b) return (a>b)? a:b end\n\ndef small(a,b) return (a\"NO\",1=>\"aabb\",2=>\"abab\",4=>\"abba\",7=>\"aaaa\"}[ans])"}], "negative_code": [{"source_code": "n, m = gets.split.map(&:to_i)\nb = [true] * 4\nn.times do\n a = Array.new(4){gets.gsub(/[^aeiou]/, '').reverse[0, m]}\n 1.upto(3) do |i|\n if b[i]\n b[i] &= a[0].size == m && a[0] == a[i]\n j = i != 1 ? 1 : 2\n k = i != 3 ? 3 : 2\n b[i] &= a[j].size == m && a[j] == a[k]\n end\n end\nend\n\nif b.all?\n puts :aaaa\nelsif b[1]\n puts :aabb\nelsif b[2]\n puts :abab\nelsif b[3]\n puts :abba\nelse\n puts :NO\nend\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\nb = [true] * 4\nn.times do\n a = Array.new(4){gets[/([aeiou].*?){#{m}}$/]}\n 1.upto(3) do |i|\n if b[i]\n b[i] &= a[0] && a[0] == a[i]\n j = i != 1 ? 1 : 2\n k = i != 3 ? 3 : 2\n b[i] &= a[j] && a[j] == a[k]\n end\n end\nend\n\nif b.all?\n puts :aaaa\nelsif b[1]\n puts :aabb\nelsif b[2]\n puts :abab\nelsif b[3]\n puts :abba\nelse\n puts :NO\nend\n"}, {"source_code": "n, k = gets.split.collect{|x| x.to_i}\n\nold_scheme = \"aaaa\"\n(1..n).each{\n scheme = nil\n lines = Array.new(4) { gets.chomp }\n \n lines.each_index{|i|\n pos = []\n lines[i].scan(/[aeiou]/){pos << $~.begin(0)}\n if pos[-k].nil?\n puts \"NO\"\n exit\n end\n lines[i] = lines[i][pos[-k]..-1]\n }\n\n scheme = \"aabb\" if lines[0] == lines[1] and lines[2] == lines[3]\n scheme = \"abab\" if lines[0] == lines[2] and lines[1] == lines[3]\n scheme = \"abba\" if lines[0] == lines[3] and lines[1] == lines[2]\n scheme = \"aaaa\" if lines[0] == lines[1] and lines[1] == lines[2] and lines[2] == lines[3] \n if scheme.nil? or (old_scheme != scheme and old_scheme != \"aaaa\")\n puts \"NO\"\n exit\n end\n \n old_scheme = scheme\n}\n\nputs old_scheme"}], "src_uid": "a17bac596b1f060209534cbffdf0f40e"} {"nl": {"description": "Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity!A restaurant called \"Dijkstra's Place\" has started thinking about optimizing the booking system. There are n booking requests received by now. Each request is characterized by two numbers: ci and pi — the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly.We know that for each request, all ci people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment.Unfortunately, there only are k tables in the restaurant. For each table, we know ri — the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing.Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum.", "input_spec": "The first line of the input contains integer n (1 ≤ n ≤ 1000) — the number of requests from visitors. Then n lines follow. Each line contains two integers: ci, pi (1 ≤ ci, pi ≤ 1000) — the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly. The next line contains integer k (1 ≤ k ≤ 1000) — the number of tables in the restaurant. The last line contains k space-separated integers: r1, r2, ..., rk (1 ≤ ri ≤ 1000) — the maximum number of people that can sit at each table.", "output_spec": "In the first line print two integers: m, s — the number of accepted requests and the total money you get from these requests, correspondingly. Then print m lines — each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input. If there are multiple optimal answers, print any of them.", "sample_inputs": ["3\n10 50\n2 100\n5 30\n3\n4 6 9"], "sample_outputs": ["2 130\n2 1\n3 2"], "notes": null}, "positive_code": [{"source_code": "def run\n n = $stdin.gets.to_i\n\n cp = []\n n.times do |i|\n ary = $stdin.gets.chomp.split.map(&:to_i)\n cp << [i] + ary\n end\n\n k = $stdin.gets.to_i\n r = []\n $stdin.gets.split.each_with_index do |x, i|\n r << [i, x.to_i]\n end\n\n cp = cp.sort_by {|x| -x[2] }\n r = r.sort_by {|x| x[1] }\n\n sum = 0\n flag = []\n ans = []\n cp.each do |c_index, people, money|\n r.each do |t_index, size|\n if size >= people && flag[t_index] == nil\n ans << [c_index + 1, t_index + 1]\n flag[t_index] = true\n sum += money\n break\n end\n end\n end\n\n puts \"#{ans.length} #{sum}\"\n ans.each do |ary|\n puts ary.join \" \"\n end\nend\n\nrun if $0 == __FILE__\n"}], "negative_code": [], "src_uid": "ac4e4aca1146d7ad706c4ea12c90caf6"} {"nl": {"description": "A way to make a new task is to make it nondeterministic or probabilistic. For example, the hard task of Topcoder SRM 595, Constellation, is the probabilistic version of a convex hull.Let's try to make a new task. Firstly we will use the following task. There are n people, sort them by their name. It is just an ordinary sorting problem, but we can make it more interesting by adding nondeterministic element. There are n people, each person will use either his/her first name or last name as a handle. Can the lexicographical order of the handles be exactly equal to the given permutation p?More formally, if we denote the handle of the i-th person as hi, then the following condition must hold: .", "input_spec": "The first line contains an integer n (1 ≤ n ≤ 105) — the number of people. The next n lines each contains two strings. The i-th line contains strings fi and si (1 ≤ |fi|, |si| ≤ 50) — the first name and last name of the i-th person. Each string consists only of lowercase English letters. All of the given 2n strings will be distinct. The next line contains n distinct integers: p1, p2, ..., pn (1 ≤ pi ≤ n).", "output_spec": "If it is possible, output \"YES\", otherwise output \"NO\".", "sample_inputs": ["3\ngennady korotkevich\npetr mitrichev\ngaoyuan chen\n1 2 3", "3\ngennady korotkevich\npetr mitrichev\ngaoyuan chen\n3 1 2", "2\ngalileo galilei\nnicolaus copernicus\n2 1", "10\nrean schwarzer\nfei claussell\nalisa reinford\neliot craig\nlaura arseid\njusis albarea\nmachias regnitz\nsara valestin\nemma millstein\ngaius worzel\n1 2 3 4 5 6 7 8 9 10", "10\nrean schwarzer\nfei claussell\nalisa reinford\neliot craig\nlaura arseid\njusis albarea\nmachias regnitz\nsara valestin\nemma millstein\ngaius worzel\n2 4 9 6 5 7 1 3 8 10"], "sample_outputs": ["NO", "YES", "YES", "NO", "YES"], "notes": "NoteIn example 1 and 2, we have 3 people: tourist, Petr and me (cgy4ever). You can see that whatever handle is chosen, I must be the first, then tourist and Petr must be the last.In example 3, if Copernicus uses \"copernicus\" as his handle, everything will be alright."}, "positive_code": [{"source_code": "#!/usr/bin/ruby\nn=gets.to_i\na=n.times.map{gets.split}\nperm=gets.split.map{|e|e.to_i-1}\na=perm.map{|e|a[e]}\ncur=a[0].min\nputs (1...n).all?{|i|\n\tcur=a[i].select{|e|e>=cur}.min\n} ? :YES : :NO"}, {"source_code": "n=gets.to_i\na=Array.new(n)\nfor i in 0...n\n\ta[i]=gets.chomp.split.sort\nend\ncur,ans='','YES'\nfor i in gets.chomp.split.map(&:to_i)\n\tif a[i-1][0] > cur\n\t\tcur = a[i-1][0]\n\telsif a[i-1][1] > cur\n\t\tcur = a[i-1][1]\n\telse\n\t\tans = 'NO'\n\tend\nend\nputs ans"}, {"source_code": "\ndef can_p(p, names, use_n)\n can \nend\n\nn = gets.to_i\nnames = (1..n).map{gets.split(\" \")}\np = gets.split(\" \").map(&:to_i)\n\ncan_n = true\ncan_sn = true\n\n(1..n-1).each do |i|\n prev_n, prev_sn = names[p[i-1] - 1]\n cur_n, cur_sn = names[p[i] - 1]\n can_n_next = can_n && cur_n > prev_n || can_sn && cur_n > prev_sn\n can_sn_next = can_n && cur_sn > prev_n || can_sn && cur_sn > prev_sn\n can_n, can_sn = can_n_next, can_sn_next\n break if ! can_n && ! can_sn\nend\n\nans = can_n || can_sn ? \"YES\" : \"NO\"\nputs ans\n"}, {"source_code": "n = gets.to_i\na = n.times.map{gets.split.sort}.unshift(nil)\np = gets.split.map(&:to_i)\ns = ''\nans = p.each do |i|\n if a[i][0] > s\n s = a[i][0]\n elsif a[i][1] > s\n s = a[i][1]\n else\n break\n end\nend\nputs ans ? 'YES' : 'NO'\n"}, {"source_code": "n = gets.to_i\na = [[]] + n.times.map{gets.split.sort}\np = gets.split.map(&:to_i)\ns = []\nans = true\np.each do |i|\n if i == p[0] || a[i][0] > s.last[0]\n s << a[i]\n elsif a[i].reverse[0] > s.last[0]\n s << a[i].reverse\n else\n ans = false\n break\n end\nend\nputs ans ? \"YES\" : \"NO\"\n"}, {"source_code": "a = []\n(n = gets.to_i).times {\n a << gets.chomp.split\n}\norder = gets.chomp.split.map { |c| c.to_i - 1 }\nlast = a[order[0]]\n(1...n).each { |k|\n cur = []\n a[order[k]].each { |x|\n cur << x if last.any? { |v| x > v }\n }\n if cur.empty?\n puts 'NO'\n exit\n end\n last = cur\n}\nputs 'YES'"}, {"source_code": "n=gets.to_i\nh=[0]\nn.times{h< b\n b=h[i].min\n elsif h[i].max > b\n b=h[i].max\n else\n r=\"NO\"\n break\n end\nend\nputs r"}], "negative_code": [{"source_code": "\ndef can_p(p, names, use_n)\n can \nend\n\nn = gets.to_i\nnames = (1..n).map{gets.split(\" \")}\np = gets.split(\" \").map(&:to_i)\n\nputs \"NAMES: #{names}\"\nputs \"P: #{p}\"\n\ncan_n = true\ncan_sn = true\n\n(1..n-1).each do |i|\n prev_n, prev_sn = names[p[i-1] - 1]\n cur_n, cur_sn = names[p[i] - 1]\n can_n_next = can_n && cur_n > prev_n || can_sn && cur_n > prev_sn\n can_sn_next = can_n && cur_sn > prev_n || can_sn && cur_sn > prev_sn\n can_n, can_sn = can_n_next, can_sn_next\n break if ! can_n && ! can_sn\nend\n\nans = can_n || can_sn ? \"YES\" : \"NO\"\nputs ans\n"}, {"source_code": "n = gets.to_i\na = n.times.map{gets.chomp}\np = gets.split.map(&:to_i)\ndef change a\n\tname1, name2 = a.split\n\ttmp = name1\n\treturn [name2, tmp].join(\" \")\nend\ndef compare i, j, a\n\t3.times do |k|\n\t\tif a[i-1] > a[j-1]\n\t\t\tif k == 0\n\t\t\t\ta[j-1] = change a[j-1]\n\t\t\telsif k == 1\n\t\t\t\ta[i-1] = change a[i-1]\n\t\t\telsif k == 2\n\t\t\t\ta[j-1] = change a[j-1]\n\t\t\tend\n\t\telsif a[i-1] < a[j-1]\n\t\t\treturn true\n\t\t\tbreak\n\t\tend\n\tend\nend\nn.times do |i|\n\tif compare(p[i], p[i+1], a) == false\n\t\tputs \"NO\"\n\t\tbreak\n\telsif i == p.size - 2\n\t\tputs \"YES\"\n\t\tbreak\n\tend\nend\n"}, {"source_code": "a = []\n(n = gets.to_i).times {\n a << gets.chomp.split\n}\norder = gets.chomp.split.map { |c| c.to_i - 1 }\n(0...n - 1).each { |k|\n i, j = order[k, k + 2]\n unless a[i][0] < a[j][0] || a[i][0] < a[j][1] || a[i][1] < a[j][0] || a[i][1] < a[j][1]\n puts 'NO'\n exit\n end\n}\nputs 'YES'"}], "src_uid": "a48ef749bf54d673772e09025ae806de"} {"nl": {"description": "Codeforces separates its users into $$$4$$$ divisions by their rating: For Division 1: $$$1900 \\leq \\mathrm{rating}$$$ For Division 2: $$$1600 \\leq \\mathrm{rating} \\leq 1899$$$ For Division 3: $$$1400 \\leq \\mathrm{rating} \\leq 1599$$$ For Division 4: $$$\\mathrm{rating} \\leq 1399$$$ Given a $$$\\mathrm{rating}$$$, print in which division the $$$\\mathrm{rating}$$$ belongs.", "input_spec": "The first line of the input contains an integer $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$) — the number of testcases. The description of each test consists of one line containing one integer $$$\\mathrm{rating}$$$ ($$$-5000 \\leq \\mathrm{rating} \\leq 5000$$$).", "output_spec": "For each test case, output a single line containing the correct division in the format \"Division X\", where $$$X$$$ is an integer between $$$1$$$ and $$$4$$$ representing the division for the corresponding rating.", "sample_inputs": ["7\n-789\n1299\n1300\n1399\n1400\n1679\n2300"], "sample_outputs": ["Division 4\nDivision 4\nDivision 4\nDivision 4\nDivision 3\nDivision 2\nDivision 1"], "notes": "NoteFor test cases $$$1-4$$$, the corresponding ratings are $$$-789$$$, $$$1299$$$, $$$1300$$$, $$$1399$$$, so all of them are in division $$$4$$$.For the fifth test case, the corresponding rating is $$$1400$$$, so it is in division $$$3$$$.For the sixth test case, the corresponding rating is $$$1679$$$, so it is in division $$$2$$$.For the seventh test case, the corresponding rating is $$$2300$$$, so it is in division $$$1$$$."}, "positive_code": [{"source_code": "gets.to_i.times {\n\tputs \"Division #{case gets.to_i; when -5000 .. 1399; 4; when 1400 .. 1599; 3;when 1600 .. 1899; 2; else; 1; end;}\"\n} "}, {"source_code": "def division?(n)\n if n <= 1399\n puts \"Division 4\"\n elsif n >= 1400 && n <= 1599\n puts \"Division 3\"\n elsif n >= 1600 && n <= 1899\n puts \"Division 2\"\n else\n puts \"Division 1\"\n end\nend\n\nt = gets.chomp.to_i\n\ninput = []\n(0..t-1).each do |i|\n input.push(gets.chomp.to_i)\nend\n\ninput.each do |i|\n division?(i)\nend"}, {"source_code": "input1 = gets.to_i\r\ninput1.times do\r\n output = [\"Division\"]\r\n case (input = gets.to_i)\r\n when (..1399) then output << \"4\"\r\n when (1400..1599) then output << \"3\"\r\n when (1600..1899) then output << \"2\"\r\n when (1900..) then output << \"1\"\r\n end\r\n puts output.join(\" \")\r\nend\r\n"}, {"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n x = if n <= 1399\r\n 4\r\n elsif n <= 1599\r\n 3\r\n elsif n <= 1899\r\n 2\r\n else\r\n 1\r\n end\r\n puts \"Division #{x}\"\r\nend\r\n"}, {"source_code": "t = gets.chomp().to_i\r\n\r\nfor i in 1 .. t \r\n rating = gets.chomp().to_i\r\n if 1900 <= rating\r\n puts \"Division 1\"\r\n elsif 1600 <= rating and rating <= 1899\r\n puts \"Division 2\"\r\n elsif 1400 <= rating and rating <= 1599\r\n puts \"Division 3\"\r\n elsif rating <= 1399\r\n puts \"Division 4\"\r\n end\r\nend"}, {"source_code": "gets.to_i.times do\n a = gets.to_i\n if a>= 1900\n puts \"Division 1\"\n elsif a >= 1600\n puts \"Division 2\"\n elsif a >= 1400\n puts \"Division 3\"\n else\n puts \"Division 4\"\n end\nend"}, {"source_code": "tests = gets.to_i\r\n\r\ntests.times do\r\n\r\n i = gets.to_i\r\n\r\n if i >= 1900\r\n puts \"Division 1\"\r\n elsif 1600 <= i and i <= 1899\r\n puts \"Division 2\"\r\n elsif 1400 <= i and i <= 1599\r\n puts \"Division 3\"\r\n else\r\n puts \"Division 4\"\r\n end\r\nend"}, {"source_code": "t=gets.chomp.to_i\r\nt.times do\r\n r = gets.chomp.to_i\r\n if(r>=1900)\r\n puts \"Division 1\"\r\n end\r\n if(r>=1600 && r<=1899)\r\n puts \"Division 2\"\r\n end\r\n if(r>=1400 && r<=1599)\r\n puts \"Division 3\"\r\n end\r\n if(r<=1399)\r\n puts \"Division 4\"\r\n end\r\nend"}], "negative_code": [], "src_uid": "020c7b64688736ecc5e97d17df2c2605"} {"nl": {"description": "A little boy Laurenty has been playing his favourite game Nota for quite a while and is now very hungry. The boy wants to make sausage and cheese sandwiches, but first, he needs to buy a sausage and some cheese.The town where Laurenty lives in is not large. The houses in it are located in two rows, n houses in each row. Laurenty lives in the very last house of the second row. The only shop in town is placed in the first house of the first row.The first and second rows are separated with the main avenue of the city. The adjacent houses of one row are separated by streets.Each crosswalk of a street or an avenue has some traffic lights. In order to cross the street, you need to press a button on the traffic light, wait for a while for the green light and cross the street. Different traffic lights can have different waiting time.The traffic light on the crosswalk from the j-th house of the i-th row to the (j + 1)-th house of the same row has waiting time equal to aij (1 ≤ i ≤ 2, 1 ≤ j ≤ n - 1). For the traffic light on the crossing from the j-th house of one row to the j-th house of another row the waiting time equals bj (1 ≤ j ≤ n). The city doesn't have any other crossings.The boy wants to get to the store, buy the products and go back. The main avenue of the city is wide enough, so the boy wants to cross it exactly once on the way to the store and exactly once on the way back home. The boy would get bored if he had to walk the same way again, so he wants the way home to be different from the way to the store in at least one crossing. Figure to the first sample. Help Laurenty determine the minimum total time he needs to wait at the crossroads.", "input_spec": "The first line of the input contains integer n (2 ≤ n ≤ 50) — the number of houses in each row. Each of the next two lines contains n - 1 space-separated integer — values aij (1 ≤ aij ≤ 100). The last line contains n space-separated integers bj (1 ≤ bj ≤ 100).", "output_spec": "Print a single integer — the least total time Laurenty needs to wait at the crossroads, given that he crosses the avenue only once both on his way to the store and on his way back home.", "sample_inputs": ["4\n1 2 3\n3 2 1\n3 2 2 3", "3\n1 2\n3 3\n2 1 3", "2\n1\n1\n1 1"], "sample_outputs": ["12", "11", "4"], "notes": "NoteThe first sample is shown on the figure above. In the second sample, Laurenty's path can look as follows: Laurenty crosses the avenue, the waiting time is 3; Laurenty uses the second crossing in the first row, the waiting time is 2; Laurenty uses the first crossing in the first row, the waiting time is 1; Laurenty uses the first crossing in the first row, the waiting time is 1; Laurenty crosses the avenue, the waiting time is 1; Laurenty uses the second crossing in the second row, the waiting time is 3. In total we get that the answer equals 11.In the last sample Laurenty visits all the crossings, so the answer is 4."}, "positive_code": [{"source_code": "n=gets.to_i\ncross2=gets.split(\" \").map(&:to_i)\ncross1=gets.split(\" \").map(&:to_i)\ncrossav=gets.split(\" \").map(&:to_i)\n\nt=999999999999999\nfor i in 0...n\n\tfor j in 0...n\n\t\tnext if i == j\n\n\t\tcost = 0\n\t\tcost += (cross1[i..-1].reduce(:+)||0) + crossav[i] + (cross2[0...i].reduce(:+)||0)\n\t\tcost += (cross1[j..-1].reduce(:+)||0) + crossav[j] + (cross2[0...j].reduce(:+)||0)\n\n\t\tt = [t, cost].min\n\tend\nend\n\np t\n"}, {"source_code": "n, first_row_traffic, second_row_traffic, avenue_traffic, sums = \n\tgets.chomp.to_i, \n\tgets.chomp.split(\" \").map(&:to_i).unshift(0), \n\tgets.chomp.split(\" \").map(&:to_i).push(0),\n\tgets.chomp.split(\" \").map(&:to_i),\n\t[]\n\n(n-1).downto 0 do |i|\n\tsum = 0 \n\n\tsum += avenue_traffic[i]\n\t\n\ti.downto 0 do |j|\n\t\tsum += first_row_traffic[j]\n\tend\n\n\ti.upto (n-1) do |j|\n\t\tsum += second_row_traffic[j]\n\tend\n\t\n\tsums << sum\nend\nsums.sort!\nputs (sums[0] + sums[1])"}, {"source_code": "n = gets.chomp.to_i\ntop = gets.split.map{ |x| x.to_i }\nbot = gets.split.map{ |x| x.to_i }\nover = gets.split.map{ |x| x.to_i }\ncost = []\nn.times do |i|\n sum = over[i]\n (0...i).each { |j| sum += top[j] }\n (i...(n-1)).each { |j| sum += bot[j] }\n cost << sum\nend\n\nmax= top.inject(:+) + bot.inject(:+) + over.inject(:+)\nr1 = max\nb1 = 0\nn.times do |i|\n if cost[i] < r1\n r1 = cost[i]\n b1 = i\n end\nend\nr2 = max\nn.times do |i|\n if cost[i] < r2 and i != b1\n r2 = cost[i]\n end\nend\n\nputs r1+r2\n"}, {"source_code": "#! ruby\n# try Codeforces Round 325 div2\n# author: Leonardone @ NEETSDKASU\n\nn = gets.to_i\n*a, b = readlines.map{|x| x.chomp.split.map(&:to_i)}\n\nf1 = [[:H2S, n - 1, 1, 0, 0]]\n\nsp = 100 * 100\nans = 100 * 100\nwhile !f1.empty?\n\tf2 = []\n\tf1.each do |f, x, y, c, r|\n\t\tcase f\n\t\twhen :H2S\n\t\t\tif x > 0\n\t\t\t\tnc = c + a[y][x - 1]\n\t\t\t\tif x - 1 == 0 && y == 0\n\t\t\t\t\tif nc < sp\n\t\t\t\t\t\tsp = nc\n\t\t\t\t\t\tf2 << [:S2H, 0, 0, nc, r]\n\t\t\t\t\tend\n\t\t\t\telse\n\t\t\t\t\tf2 << [f, x - 1, y, nc, r]\n\t\t\t\tend\n\t\t\tend\n\t\t\tif y == 1\n\t\t\t\tnc = c + b[x]\n\t\t\t\tif x == 0\n\t\t\t\t\tif nc < sp\n\t\t\t\t\t\tsp = nc\n\t\t\t\t\t\tf2 << [:S2H, 0, 0, nc, x]\n\t\t\t\t\tend\n\t\t\t\telse\n\t\t\t\t\tf2 << [f, x, 0, nc, x]\n\t\t\t\tend\n\t\t\tend\n\t\twhen :S2H\n\t\t\tif x < n - 1\n\t\t\t\tnc = c + a[y][x]\n\t\t\t\tif x + 1 == n - 1 && y == 1\n\t\t\t\t\tans = [ans, nc].min\n\t\t\t\telse\n\t\t\t\t\tf2 << [f, x + 1, y, nc, r]\n\t\t\t\tend\n\t\t\tend\n\t\t\tif y == 0 && r != x\n\t\t\t\tnc = c + b[x]\n\t\t\t\tif x == n - 1\n\t\t\t\t\tans = [ans, nc].min\n\t\t\t\telse\n\t\t\t\t\tf2 << [f, x, 1, nc, r]\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\tend\n\tf1 = f2\nend\nputs ans\n"}, {"source_code": "n = gets.chomp.to_i\nas = []\n2.times do\n as << gets.chomp.split(' ').map(&:to_i)\nend\nbs = gets.chomp.split(' ').map(&:to_i)\n\ncs = []\nfor i in 0..1\n c = [0]\n for j in 1..n - 1\n c << c[j-1] + as[i][j-1]\n end\n cs << c\nend\n\nds = []\nfor i in 0..n-1\n ds << cs[0][i] + bs[i] + cs[1][n-1] - cs[1][i]\nend\n\nds.sort!\nputs ds[0] + ds[1]\n"}, {"source_code": "# coding; utf-8\n\nh = gets.strip.to_i\nfr = gets.strip.split(' ').map(&:to_i)\nsr = gets.strip.split(' ').map(&:to_i)\nav = gets.strip.split(' ').map(&:to_i)\n\ntimes = [ ]\nh.times do |i|\n t = av[i]\n 0.upto(i - 1) { |j| t += fr[j] }\n i.upto(sr.length - 1) { |j| t += sr[j] }\n\n times.push(t)\nend\n\ntimes.sort!\nputs (times[0] + times[1]).to_s\n"}], "negative_code": [], "src_uid": "e3e0625914fdc08950601248b1278f7d"} {"nl": {"description": "Petya is preparing for his birthday. He decided that there would be $$$n$$$ different dishes on the dinner table, numbered from $$$1$$$ to $$$n$$$. Since Petya doesn't like to cook, he wants to order these dishes in restaurants.Unfortunately, all dishes are prepared in different restaurants and therefore Petya needs to pick up his orders from $$$n$$$ different places. To speed up this process, he wants to order courier delivery at some restaurants. Thus, for each dish, there are two options for Petya how he can get it: the dish will be delivered by a courier from the restaurant $$$i$$$, in this case the courier will arrive in $$$a_i$$$ minutes, Petya goes to the restaurant $$$i$$$ on his own and picks up the dish, he will spend $$$b_i$$$ minutes on this. Each restaurant has its own couriers and they start delivering the order at the moment Petya leaves the house. In other words, all couriers work in parallel. Petya must visit all restaurants in which he has not chosen delivery, he does this consistently.For example, if Petya wants to order $$$n = 4$$$ dishes and $$$a = [3, 7, 4, 5]$$$, and $$$b = [2, 1, 2, 4]$$$, then he can order delivery from the first and the fourth restaurant, and go to the second and third on your own. Then the courier of the first restaurant will bring the order in $$$3$$$ minutes, the courier of the fourth restaurant will bring the order in $$$5$$$ minutes, and Petya will pick up the remaining dishes in $$$1 + 2 = 3$$$ minutes. Thus, in $$$5$$$ minutes all the dishes will be at Petya's house.Find the minimum time after which all the dishes can be at Petya's home.", "input_spec": "The first line contains one positive integer $$$t$$$ ($$$1 \\le t \\le 2 \\cdot 10^5$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case begins with a line containing one integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) — the number of dishes that Petya wants to order. The second line of each test case contains $$$n$$$ integers $$$a_1 \\ldots a_n$$$ ($$$1 \\le a_i \\le 10^9$$$) — the time of courier delivery of the dish with the number $$$i$$$. The third line of each test case contains $$$n$$$ integers $$$b_1 \\ldots b_n$$$ ($$$1 \\le b_i \\le 10^9$$$) — the time during which Petya will pick up the dish with the number $$$i$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case output one integer — the minimum time after which all dishes can be at Petya's home.", "sample_inputs": ["4\n4\n3 7 4 5\n2 1 2 4\n4\n1 2 3 4\n3 3 3 3\n2\n1 2\n10 10\n2\n10 10\n1 2"], "sample_outputs": ["5\n3\n2\n3"], "notes": null}, "positive_code": [{"source_code": "t = gets.chomp.to_i\nt.times do\n n = gets.chomp.to_i\n arr = gets.chomp.split(' ').map(&:to_i).zip(0..)\n brr = gets.chomp.split(' ').map(&:to_i)\n arr.sort!.reverse!\n cur = arr[0]\n p_time = 0\n while p_time + brr[cur[1]] < cur[0]\n p_time += brr[cur[1]]\n arr.shift\n cur = arr[0]\n break if cur.nil?\n end\n res = p_time\n res = [res, cur[0]].max unless cur.nil?\n puts res.to_s\nend "}], "negative_code": [], "src_uid": "254cb444b9bdfa5e177adad23b7e814a"} {"nl": {"description": "Mahmoud and Ehab are in the fourth stage now.Dr. Evil has a hidden binary string of length n. He guarantees that there is at least one '0' symbol and at least one '1' symbol in it. Now he wants Mahmoud and Ehab to find a position of any '0' symbol and any '1' symbol. In order to do this, Mahmoud and Ehab can ask Dr. Evil up to 15 questions. They tell Dr. Evil some binary string of length n, and Dr. Evil tells the Hamming distance between these two strings. Hamming distance between 2 binary strings of the same length is the number of positions in which they have different symbols. You can find the definition of Hamming distance in the notes section below.Help Mahmoud and Ehab find these two positions.You will get Wrong Answer verdict if Your queries doesn't satisfy interaction protocol described below. You ask strictly more than 15 questions and your program terminated after exceeding queries limit. Please note, that you can do up to 15 ask queries and one answer query. Your final answer is not correct. You will get Idleness Limit Exceeded if you don't print anything or if you forget to flush the output, including for the final answer (more info about flushing output below).If you exceed the maximum number of queries, You should terminate with 0, In this case you'll get Wrong Answer, If you don't terminate you may receive any verdict because you'll be reading from a closed stream .", "input_spec": "The first line of input will contain a single integer n (2 ≤ n ≤ 1000) — the length of the hidden binary string.", "output_spec": "To print the final answer, print \"! pos0 pos1\" (without quotes), where pos0 and pos1 are positions of some '0' and some '1' in the string (the string is 1-indexed). Don't forget to flush the output after printing the answer!", "sample_inputs": ["3\n2\n1\n3\n2\n1\n0"], "sample_outputs": ["? 000\n? 001\n? 010\n? 011\n? 100\n? 101\n! 2 1"], "notes": "NoteHamming distance definition: https://en.wikipedia.org/wiki/Hamming_distanceIn the first test case the hidden binary string is 101, The first query is 000, so the Hamming distance is 2. In the second query the hidden string is still 101 and query is 001, so the Hamming distance is 1.After some queries you find that symbol at position 2 is '0' and symbol at position 1 is '1', so you print \"! 2 1\"."}, "positive_code": [{"source_code": "def mq(str)\n\tputs \"? \" + str\n\t$stdout.flush()\n\tgets.to_i\nend\n\nn = gets.to_i\n\nsample = \"0\" * n\nl = 0\nr = n - 1\nlast = false\n\nd = mq(sample)\n\nit = 14\n\npos0 = pos1 = -1\n\nwhile r >= l\n\texit(0) if it == 0\n\t\n\tmid = (l + r) >> 1\n\tlen = mid - l + 1\n\t\n\tt = last ? \"0\" : \"1\"\n\tsample[l..mid] = t * len\n\t\n\tnd = mq(sample)\n\t\n\tif (d - nd).abs < len\n\t\tr = mid\n\t\tlast = !last\n\telse \n\t\tif nd > d\n\t\t\tpos1 = l if last\n\t\t\tpos0 = l if !last\n\t\telse \n\t\t\tpos1 = l if !last\n\t\t\tpos0 = l if last\n\t\tend\n\t\t\t\n\t\tl = mid + 1\n\tend\n\t\n\td = nd\n\t\n\tit -= 1\nend\n\nprint \"! \", pos0 + 1, \" \", pos1 + 1\n$stdout.flush()"}], "negative_code": [{"source_code": "def mq(str)\n\tputs \"? \" + str\n\t$stdout.flush()\n\tgets.to_i\nend\n\nn = gets.to_i\n\nsample = \"0\" * n\nl = 0\nr = n - 1\nlast = false\n\nd = mq(sample)\n\nit = 14\n\nwhile r - l > 1\n\texit(0) if it == 0\n\t\n\tmid = (l + r) >> 1\n\tlen = mid - l + 1\n\t\n\tt = last ? \"0\" : \"1\"\n\tsample[l..mid] = t * len\n\t\n\tnd = mq(sample)\n\t\n\tif (d - nd).abs < len\n\t\tr = mid\n\t\tlast = !last\n\telse \n\t\tl = mid\n\tend\n\t\n\td = nd\n\t\n\tit -= 1\nend\n\nexit(0) if it == 0\n\nsample[l] = last ? \"0\" : \"1\"\nnd = mq(sample)\n\nit -= 1\nexit(0) if it == 0\n\nif nd < d\n\tl, r = r, l if sample[l] == \"1\"\nelse\n\tl, r = r, l if sample[l] == \"0\"\nend\n\nprint \"! \", l + 1, \" \", r + 1\n$stdout.flush()"}, {"source_code": "def mq(str)\n\tputs \"? \" + str\n\t$stdout.flush()\n\tgets.to_i\nend\n\nn = gets.to_i\n\nsample = \"0\" * n\nl = 0\nr = n - 1\nlast = false\n\nd = mq(sample)\n\nit = 13\n\nwhile r - l > 1\n\texit(0) if it == 0\n\t\n\tmid = (l + r) >> 1\n\tlen = mid - l + 1\n\t\n\tt = last ? \"0\" : \"1\"\n\tsample[l..mid] = t * len\n\t\n\tnd = mq(sample)\n\t\n\tif (d - nd).abs < len\n\t\tr = mid\n\t\tlast = !last\n\telse \n\t\tl = mid\n\tend\n\t\n\td = nd\n\t\n\tit -= 1\nend\n\nsample[l] = last ? \"0\" : \"1\"\nnd = mq(sample)\n\nit -= 1\nexit(0) if it == 0\n\nif nd < d\n\tl, r = r, l if sample[l] == \"1\"\nelse\n\tl, r = r, l if sample[l] == \"0\"\nend\n\nprint \"! \", l + 1, \" \", r + 1\n$stdout.flush()"}, {"source_code": "def mq(str)\n\tputs \"? \" + str\n\t$stdout.flush()\n\tgets.to_i\nend\n\nn = gets.to_i\n\nsample = \"0\" * n\nl = 0\nr = n - 1\nlast = false\n\nd = mq(sample)\n\nit = 14\n\nwhile r - l > 1\n\texit(0) if it == 0\n\t\n\tmid = (l + r) >> 1\n\tlen = mid - l + 1\n\t\n\tt = last ? \"0\" : \"1\"\n\tsample[l..mid] = t * len\n\t\n\tnd = mq(sample)\n\t\n\tif (d - nd).abs < len\n\t\tr = mid\n\t\tlast = !last\n\telse \n\t\tl = mid\n\tend\n\t\n\td = nd\n\t\n\tit -= 1\nend\n\nexit(0) if it == 0\n\nsample[l] = last ? \"0\" : \"1\"\nnd = mq(sample)\n\nif nd < d\n\tl, r = r, l if sample[l] == \"1\"\nelse\n\tl, r = r, l if sample[l] == \"0\"\nend\n\nprint \"! \", l + 1, \" \", r + 1\n$stdout.flush()"}, {"source_code": "def mq(str)\n\tputs \"? \" + str\n\t$stdout.flush()\n\tgets.to_i\nend\n\nn = gets.to_i\n\nsample = \"0\" * n\nl = 0\nr = n - 1\nlast = false\n\nd = mq(sample)\n\n#it = 14\n\nwhile r - l > 1\n\t#exit(0) if it == 0\n\t\n\tmid = (l + r) >> 1\n\tlen = mid - l + 1\n\t\n\tt = last ? \"0\" : \"1\"\n\tsample[l..mid] = t * len\n\t\n\tnd = mq(sample)\n\t\n\tif (d - nd).abs < len\n\t\tr = mid\n\t\tlast = !last\n\telse \n\t\tl = mid\n\tend\n\t\n\td = nd\n\t\n\t#it -= 1\nend\n\n#exit(0) if it == 0\n\nsample[l] = last ? \"0\" : \"1\"\nnd = mq(sample)\n\nif nd < d\n\tl, r = r, l if sample[l] == \"1\"\nelse\n\tl, r = r, l if sample[l] == \"0\"\nend\n\nprint \"! \", l + 1, \" \", r + 1\n$stdout.flush()"}, {"source_code": "def mq(str)\n\tputs \"? \" + str\n\t$stdout.flush()\n\tgets.to_i\nend\n\nn = gets.to_i\n\nsample = \"0\" * n\nl = 0\nr = n - 1\nlast = false\n\nd = mq(sample)\n\nit = 13\n\nwhile r - l > 1\n\texit(0) if it == 0\n\t\n\tmid = (l + r) >> 1\n\tlen = mid - l + 1\n\t\n\tt = last ? \"0\" : \"1\"\n\tsample[l..mid] = t * len\n\t\n\tnd = mq(sample)\n\t\n\tif (d - nd).abs < len\n\t\tr = mid\n\t\tlast = !last\n\telse \n\t\tl = mid\n\tend\n\t\n\td = nd\n\t\n\tit -= 1\nend\nexit(0) if it == 0\nsample[l] = last ? \"0\" : \"1\"\nnd = mq(sample)\n\nit -= 1\nexit(0) if it == 0\n\nif nd < d\n\tl, r = r, l if sample[l] == \"1\"\nelse\n\tl, r = r, l if sample[l] == \"0\"\nend\n\nprint \"! \", l + 1, \" \", r + 1\n$stdout.flush()"}], "src_uid": "4dd91b32ea1a41d4ecb99bc5672ef1bc"} {"nl": {"description": "The marmots have prepared a very easy problem for this year's HC2 – this one. It involves numbers n, k and a sequence of n positive integers a1, a2, ..., an. They also came up with a beautiful and riveting story for the problem statement. It explains what the input means, what the program should output, and it also reads like a good criminal.However I, Heidi, will have none of that. As my joke for today, I am removing the story from the statement and replacing it with these two unhelpful paragraphs. Now solve the problem, fools!", "input_spec": "The first line of the input contains two space-separated integers n and k (1 ≤ k ≤ n ≤ 2200). The second line contains n space-separated integers a1, ..., an (1 ≤ ai ≤ 104).", "output_spec": "Output one number.", "sample_inputs": ["8 5\n1 1 1 1 1 1 1 1", "10 3\n16 8 2 4 512 256 32 128 64 1", "5 1\n20 10 50 30 46", "6 6\n6 6 6 6 6 6", "1 1\n100"], "sample_outputs": ["5", "7", "10", "36", "100"], "notes": null}, "positive_code": [{"source_code": "n, k = gets.split.map(&:to_i)\na = gets.split.map(&:to_i).sort!\nans = 0\nk.times{|i| ans += a[i] }\nputs ans"}], "negative_code": [], "src_uid": "5097e92916e49cdc6fb4953b864186db"} {"nl": {"description": "Many years ago Berland was a small country where only $$$n$$$ people lived. Each person had some savings: the $$$i$$$-th one had $$$a_i$$$ burles.The government considered a person as wealthy if he had at least $$$x$$$ burles. To increase the number of wealthy people Berland decided to carry out several reforms. Each reform looked like that: the government chooses some subset of people (maybe all of them); the government takes all savings from the chosen people and redistributes the savings among the chosen people equally. For example, consider the savings as list $$$[5, 1, 2, 1]$$$: if the government chose the $$$1$$$-st and the $$$3$$$-rd persons then it, at first, will take all $$$5 + 2 = 7$$$ burles and after that will return $$$3.5$$$ burles to the chosen people. As a result, the savings will become $$$[3.5, 1, 3.5, 1]$$$.A lot of data was lost from that time, so we don't know how many reforms were implemented and to whom. All we can do is ask you to calculate the maximum possible number of wealthy people after several (maybe zero) reforms.", "input_spec": "The first line contains single integer $$$T$$$ ($$$1 \\le T \\le 1000$$$) — the number of test cases. Next $$$2T$$$ lines contain the test cases — two lines per test case. The first line contains two integers $$$n$$$ and $$$x$$$ ($$$1 \\le n \\le 10^5$$$, $$$1 \\le x \\le 10^9$$$) — the number of people and the minimum amount of money to be considered as wealthy. The second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$) — the initial savings of each person. It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$10^5$$$.", "output_spec": "Print $$$T$$$ integers — one per test case. For each test case print the maximum possible number of wealthy people after several (maybe zero) reforms.", "sample_inputs": ["4\n4 3\n5 1 2 1\n4 10\n11 9 11 9\n2 5\n4 3\n3 7\n9 4 9"], "sample_outputs": ["2\n4\n0\n3"], "notes": "NoteThe first test case is described in the statement.In the second test case, the government, for example, could carry out two reforms: $$$[\\underline{11}, \\underline{9}, 11, 9] \\rightarrow [10, 10, \\underline{11}, \\underline{9}] \\rightarrow [10, 10, 10, 10]$$$.In the third test case, the government couldn't make even one person wealthy.In the fourth test case, the government could choose all people to carry out a reform: $$$[\\underline{9}, \\underline{4}, \\underline{9}] \\rightarrow [7\\frac{1}{3}, 7\\frac{1}{3}, 7\\frac{1}{3}]$$$."}, "positive_code": [{"source_code": "def run\n n, x = gets.chomp.split(\" \").map(&:to_i)\n a = gets.chomp.split(\" \").map(&:to_i)\n rich = a.select{|el| el >= x}\n poor = a.select{|el| el < x}.sort_by{|c| -c}\n count = rich.count\n surplus = rich.reduce(0) {|acc, el| acc + (el > x ? el - x : 0)}\n poor.each do |p|\n if surplus >= x - p\n surplus -= (x - p)\n count += 1\n else\n break\n end\n end\n puts count\nend\n\n\ndef main\n t = gets.chomp.to_i\n\n t.times do\n run\n end\nend\n\nmain\n"}, {"source_code": "t = gets.to_i\nt.times do\n n, x = gets.split.map(&:to_i)\n a = gets.split.map(&:to_i).sort.reverse\n count = 0\n savings_sum = 0\n a.each_with_index do |v, i|\n savings_sum += v\n if savings_sum >= x * (i + 1)\n count += 1\n else\n break\n end\n end\n puts count\nend\n"}, {"source_code": "1.upto(gets.to_i) do\n n, x = gets.split(' ').map(&:to_i)\n a = gets.split(' ').map(&:to_f).sort.reverse\n\n s = 0\n n = a.count { |c| c >= x }\n max = n\n\n a.each_with_index do |e, i|\n s += e\n\n n -= 1 if e >= x\n\n break if s/(i+1) < x\n\n max = [max, n + i + 1].max\n end\n\n puts max\nend\n"}], "negative_code": [], "src_uid": "51f922bb2c51f50b5c3b55725dd7766e"} {"nl": {"description": "Gardener Alexey teaches competitive programming to high school students. To congratulate Alexey on the Teacher's Day, the students have gifted him a collection of wooden sticks, where every stick has an integer length. Now Alexey wants to grow a tree from them.The tree looks like a polyline on the plane, consisting of all sticks. The polyline starts at the point $$$(0, 0)$$$. While constructing the polyline, Alexey will attach sticks to it one by one in arbitrary order. Each stick must be either vertical or horizontal (that is, parallel to $$$OX$$$ or $$$OY$$$ axis). It is not allowed for two consecutive sticks to be aligned simultaneously horizontally or simultaneously vertically. See the images below for clarification.Alexey wants to make a polyline in such a way that its end is as far as possible from $$$(0, 0)$$$. Please help him to grow the tree this way.Note that the polyline defining the form of the tree may have self-intersections and self-touches, but it can be proved that the optimal answer does not contain any self-intersections or self-touches.", "input_spec": "The first line contains an integer $$$n$$$ ($$$1 \\le n \\le 100\\,000$$$) — the number of sticks Alexey got as a present. The second line contains $$$n$$$ integers $$$a_1, \\ldots, a_n$$$ ($$$1 \\le a_i \\le 10\\,000$$$) — the lengths of the sticks.", "output_spec": "Print one integer — the square of the largest possible distance from $$$(0, 0)$$$ to the tree end.", "sample_inputs": ["3\n1 2 3", "4\n1 1 2 2"], "sample_outputs": ["26", "20"], "notes": "NoteThe following pictures show optimal trees for example tests. The squared distance in the first example equals $$$5 \\cdot 5 + 1 \\cdot 1 = 26$$$, and in the second example $$$4 \\cdot 4 + 2 \\cdot 2 = 20$$$. "}, "positive_code": [{"source_code": "# Author : Fuad Ashraful Mehmet \n#Date: 18th April\n\ndef solve()\n n=gets.to_i\n ar=gets.split.map(&:to_i).sort\n if n==1\n return ar[0]**2\n else\n xcor=ar[0,n/2].inject(:+)\n ycor=ar[n/2,(n+1)/2].inject(:+)\n \n return xcor**2+ycor**2\n end\nend\n\nans=solve()\nputs ans"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i).sort\nif n == 1\n p a[0]**2\nelse\n x = a[0,n/2].inject(:+)\n y = a[n/2,(n+1)/2].inject(:+)\n p x**2 + y**2\nend"}], "negative_code": [{"source_code": "n = gets.to_i\na = gets.split.map(&:to_i).sort\nif n == 1\n p a[0]\nelse\n x = a[0,n/2].inject(:+)\n y = a[n/2,(n+1)/2].inject(:+)\n p x**2 + y**2\nend"}], "src_uid": "f9fbb45e45d3040e3be19a39ea8faa1f"} {"nl": {"description": "You are given a square matrix consisting of n rows and n columns. We assume that the rows are numbered from 1 to n from top to bottom and the columns are numbered from 1 to n from left to right. Some cells (n - 1 cells in total) of the the matrix are filled with ones, the remaining cells are filled with zeros. We can apply the following operations to the matrix: Swap i-th and j-th rows of the matrix; Swap i-th and j-th columns of the matrix. You are asked to transform the matrix into a special form using these operations. In that special form all the ones must be in the cells that lie below the main diagonal. Cell of the matrix, which is located on the intersection of the i-th row and of the j-th column, lies below the main diagonal if i > j.", "input_spec": "The first line contains an integer n (2 ≤ n ≤ 1000) — the number of rows and columns. Then follow n - 1 lines that contain one's positions, one per line. Each position is described by two integers xk, yk (1 ≤ xk, yk ≤ n), separated by a space. A pair (xk, yk) means that the cell, which is located on the intersection of the xk-th row and of the yk-th column, contains one. It is guaranteed that all positions are distinct.", "output_spec": "Print the description of your actions. These actions should transform the matrix to the described special form. In the first line you should print a non-negative integer m (m ≤ 105) — the number of actions. In each of the next m lines print three space-separated integers t, i, j (1 ≤ t ≤ 2, 1 ≤ i, j ≤ n, i ≠ j), where t = 1 if you want to swap rows, t = 2 if you want to swap columns, and i and j denote the numbers of rows or columns respectively. Please note, that you do not need to minimize the number of operations, but their number should not exceed 105. If there are several solutions, you may print any of them.", "sample_inputs": ["2\n1 2", "3\n3 1\n1 3", "3\n2 1\n3 2"], "sample_outputs": ["2\n2 1 2\n1 1 2", "3\n2 2 3\n1 1 3\n1 1 2", "0"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/ruby1.9\n\ndef display_array(matrix)\n\treturn\n\tmatrix.each do |row|\n\t\tputs row.join(' ')\n\tend\n\tputs\nend\n\ndef swap_row(matrix, a, b, step, lines)\n\tif step == 1\n\t\tlines.push('%d %d %d' % [step, a+1, b+1])\n\telse\n\t\tn = matrix.length\n\t\tlines.push('%d %d %d' % [step, n-a, n-b])\n\tend\n\trow = matrix[a]\n\tmatrix[a] = matrix[b]\n\tmatrix[b] = row\nend\n\ndef sift_down(m, step, lines)\n\tn = m.length\n\tcounts = n.times.collect {|r| [0, 0, r] }\n\tn.times do |r|\n\t\tn.times do |c|\n\t\t\tif m[r][c] == 1\n\t\t\t\tcounts[r][0] += 1\n\t\t\t\tcounts[r][1] = -c\n\t\t\tend\n\t\tend\n\tend\n\tcounts.sort!\n\n\ttarget2found = Array.new(n, 0)\n\tfound2target = Array.new(n, 0)\n\tn.times do |target|\n\t\tfound = counts[target][2]\n\t\ttarget2found[target] = found\n\t\tfound2target[found] = target\n\tend\n\tn.times do |target|\n\t\tfound = target2found[target]\n\t\tnext if found == target\n\t\ttarget2found[found2target[target]] = found\n\t\tfound2target[found] = found2target[target]\n\t\tfound2target[target] = target\n\t\tswap_row(m, found, target, step, lines)\n\tend\nend\n\nn = STDIN.readline.to_i\nm = n.times.collect { Array.new(n, 0) }\n(n-1).times do\n\tr, c = STDIN.readline.split.collect {|s| s.to_i }\n\tm[r-1][c-1] = 1\nend\ndisplay_array(m)\n\nlines = []\n\nsift_down(m, 1, lines)\ndisplay_array(m)\n\nm = m.transpose.reverse\n#display_array(m)\n\nsift_down(m, 2, lines)\n#display_array(m)\ndisplay_array(m.reverse.transpose)\n\nputs lines.length\nputs lines.join(\"\\n\")\n"}, {"source_code": "#!/usr/bin/ruby1.9\n\ndef display_array(matrix)\n\treturn\n\tmatrix.each do |row|\n\t\tputs row.join(' ')\n\tend\n\tputs\nend\n\ndef swap_row(matrix, a, b, step, lines)\n\tif step == 1\n\t\tlines.push('%d %d %d' % [step, a+1, b+1])\n\telse\n\t\tn = matrix.length\n\t\tlines.push('%d %d %d' % [step, n-a, n-b])\n\tend\n\trow = matrix[a]\n\tmatrix[a] = matrix[b]\n\tmatrix[b] = row\nend\n\ndef sift_down(m, step, lines)\n\tn = m.length\n\tcounts = n.times.collect {|r| [0, 0, r] }\n\tn.times do |r|\n\t\tn.times do |c|\n\t\t\tif m[r][c] == 1\n\t\t\t\tcounts[r][0] += 1\n\t\t\t\tcounts[r][1] = -c\n\t\t\tend\n\t\tend\n\tend\n\tcounts.sort!\n\n\tpermute = Array.new(n, 0)\n\tn.times do |new_position|\n\t\told_position = counts[new_position][2]\n\t\tpermute[old_position] = new_position\n\tend\n\n\tn.times do |pos|\n\t\twhile permute[pos] != pos\n\t\t\tswap_row(m, pos, permute[pos], step, lines)\n\t\t\tt = permute[pos]\n\t\t\tpermute[pos] = permute[t]\n\t\t\tpermute[t] = t\n\t\tend\n\tend\nend\n\nn = STDIN.readline.to_i\nm = n.times.collect { Array.new(n, 0) }\n(n-1).times do\n\tr, c = STDIN.readline.split.collect {|s| s.to_i }\n\tm[r-1][c-1] = 1\nend\ndisplay_array(m)\n\nlines = []\n\nsift_down(m, 1, lines)\ndisplay_array(m)\n\nm = m.transpose.reverse\n\nsift_down(m, 2, lines)\ndisplay_array(m.reverse.transpose)\n\nputs lines.length\nputs lines.join(\"\\n\")\n"}, {"source_code": "def swap_cols(mat,i,j)\n\taux = 0\n\tn = mat.length\n\t(0..(n-1)).each {|k|\n\t\taux = mat[k][i]\n\t\tmat[k][i] = mat[k][j]\n\t\tmat[k][j] = aux\n\t}\nend\n\nn = gets.chomp.to_i\nmat = []\n(1..n).each {|pp|\n line = []\n (1..n).each {|qq|\n\tline.push(0)\n }\n mat.push(line)\n}\n\n(1..(n-1)).each { |k|\n i,j = gets.chomp.split(\" \").collect! {|x| x.to_i}\n mat[i-1][j-1] = 1 \n}\n\nactions = []\narr = []\npos = []\nm = 0\naaux = []\n(0..(n-1)).each { |k|\n\tp = m\n\tq = m\n\twhile mat[k][p]==1\n\t q +=1\n\t p +=1\n\t m +=1\n\tend\n\twhile q=0\n st -=1\n end\n arr.push(st)\n}\np = 0\nmin = arr[0]\nwhile p arr[q]\n\t\tmin = arr[q]\n\t\tpos = q\n\tend\n\tq +=1\n end\n if p != pos\n arr[pos] = arr[p]\n arr[p] = min\n actions.push(\"1 #{p+1} #{pos+1}\")\n end\n p += 1\nend\n\nputs actions.length\nactions.each { |t|\n puts t\n}\n"}], "negative_code": [{"source_code": "#!/usr/bin/ruby1.9\n\ndef display_array(matrix)\n\tmatrix.each do |row|\n\t\tputs row.join(' ')\n\tend\n\tputs\nend\n\ndef swap_row(matrix, a, b, step, lines)\n\tif step == 1\n\t\tlines.push('%d %d %d' % [step, a+1, b+1])\n\telse\n\t\tn = matrix.length\n\t\tlines.push('%d %d %d' % [step, n-a, n-b])\n\tend\n\trow = matrix[a]\n\tmatrix[a] = matrix[b]\n\tmatrix[b] = row\nend\n\ndef sift_down(m, step, lines)\n\tn = m.length\n\tcounts = n.times.collect {|r| [0, 0, r] }\n\tn.times do |r|\n\t\tn.times do |c|\n\t\t\tif m[r][c] == 1\n\t\t\t\tcounts[r][0] += 1\n\t\t\t\tcounts[r][1] = -c\n\t\t\tend\n\t\tend\n\tend\n\tcounts.sort!\n\n\tpermute = Array.new(n, 0)\n\tn.times do |new_position|\n\t\told_position = counts[new_position][2]\n\t\tpermute[old_position] = new_position\n\tend\n\n\tn.times do |pos|\n\t\twhile permute[pos] != pos\n\t\t\tswap_row(m, pos, permute[pos], step, lines)\n\t\t\tt = permute[pos]\n\t\t\tpermute[pos] = permute[t]\n\t\t\tpermute[t] = t\n\t\tend\n\tend\nend\n\nn = STDIN.readline.to_i\nm = n.times.collect { Array.new(n, 0) }\n(n-1).times do\n\tr, c = STDIN.readline.split.collect {|s| s.to_i }\n\tm[r-1][c-1] = 1\nend\ndisplay_array(m)\n\nlines = []\n\nsift_down(m, 1, lines)\ndisplay_array(m)\n\nm = m.transpose.reverse\n\nsift_down(m, 2, lines)\ndisplay_array(m.reverse.transpose)\n\nputs lines.length\nputs lines.join(\"\\n\")\n"}, {"source_code": "#!/usr/bin/ruby1.9\n\ndef display_array(matrix)\n\treturn\n\tmatrix.each do |row|\n\t\tputs row.join(' ')\n\tend\n\tputs\nend\n\ndef swap_row(matrix, a, b, step, lines)\n\tif step == 1\n\t\tlines.push('%d %d %d' % [step, a+1, b+1])\n\telse\n\t\tn = matrix.length\n\t\tlines.push('%d %d %d' % [step, n-a, n-b])\n\tend\n\trow = matrix[a]\n\tmatrix[a] = matrix[b]\n\tmatrix[b] = row\nend\n\ndef sift_down(m, step, lines)\n\tn = m.length\n\tcounts = n.times.collect {|r| [0, 0, r] }\n\tn.times do |r|\n\t\tn.times do |c|\n\t\t\tif m[r][c] == 1\n\t\t\t\tcounts[r][0] += 1\n\t\t\t\tcounts[r][1] = -c\n\t\t\tend\n\t\tend\n\tend\n\tcounts.sort!\n\n\tn.times do |target_r|\n\t\tfound_r = counts[target_r][2]\n\t\tnext if found_r <= target_r\n\t\tswap_row(m, found_r, target_r, step, lines)\n\tend\nend\n\nn = STDIN.readline.to_i\nm = n.times.collect { Array.new(n, 0) }\n(n-1).times do\n\tr, c = STDIN.readline.split.collect {|s| s.to_i }\n\tm[r-1][c-1] = 1\nend\ndisplay_array(m)\n\nlines = []\n\nsift_down(m, 1, lines)\ndisplay_array(m)\n\nm = m.transpose.reverse\n#display_array(m)\n\nsift_down(m, 2, lines)\n#display_array(m)\ndisplay_array(m.reverse.transpose)\n\nputs lines.length\nputs lines.join(\"\\n\")\n"}, {"source_code": "#!/usr/bin/ruby1.9\n\ndef display_array(matrix)\n\treturn\n\tputs\n\tmatrix.each do |row|\n\t\tputs row.join(' ')\n\tend\nend\n\ndef swap_row(matrix, a, b, step, lines)\n\tlines.push('%d %d %d' % [step, a+1, b+1])\n\trow = matrix[a]\n\tmatrix[a] = matrix[b]\n\tmatrix[b] = row\nend\n\ndef sift_down(m, step, lines)\n\tn = m.length\n\tcounts = n.times.collect {|r| [0, 0, r] }\n\tn.times do |r|\n\t\tn.times do |c|\n\t\t\tif m[r][c] == 1\n\t\t\t\tcounts[r][0] += 1\n\t\t\t\tcounts[r][1] = -c\n\t\t\tend\n\t\tend\n\tend\n\tcounts.sort!\n\n\tn.times do |target_r|\n\t\tfound_r = counts[target_r][2]\n\t\tnext if found_r <= target_r\n\t\tswap_row(m, found_r, target_r, step, lines)\n\tend\nend\n\nn = STDIN.readline.to_i\nm = n.times.collect { Array.new(n, 0) }\n(n-1).times do\n\tr, c = STDIN.readline.split.collect {|s| s.to_i }\n\tm[r-1][c-1] = 1\nend\ndisplay_array(m)\n\nlines = []\n\nsift_down(m, 1, lines)\ndisplay_array(m)\n\nm = m.transpose.reverse\n#display_array(m)\n\nsift_down(m, 2, lines)\n#display_array(m)\ndisplay_array(m.reverse.transpose)\n\nputs lines.length\nputs lines.join(\"\\n\")\n"}, {"source_code": "def swap_cols(mat,i,j)\n\taux = 0\n\tn = mat.length\n\t(0..(n-1)).each {|k|\n\t\taux = mat[k][i]\n\t\tmat[k][i] = mat[k][j]\n\t\tmat[k][j] = aux\n\t}\nend\n\nn = gets.chomp.to_i\nmat = []\n(1..n).each {|pp|\n line = []\n (1..n).each {|qq|\n\tline.push(0)\n }\n mat.push(line)\n}\n\n(1..(n-1)).each { |k|\n i,j = gets.chomp.split(\" \").collect! {|x| x.to_i}\n mat[i-1][j-1] = 1 \n}\n\nactions = []\narr = []\npos = []\nm = 0\naaux = []\n(0..(n-1)).each { |k|\n\tp = m\n\tq = m\n\twhile mat[k][p]==1\n\t q +=1\n\t p +=1\n\t m +=1\n\tend\n\twhile q=0\n st -=1\n end\n arr.push(st)\n}\npi = 0\npj = 0\nwhile piarr[pj]\n\t\taux = arr[pi]\n\t\tarr[pi] = arr[pj]\n\t\tarr[pj] = aux\n\t\t\n\t\taux = pos[pi]\n\t\tpos[pi] = pos[pj]\n\t\tpos[pj] = aux\n\t\t\n\t\tactions.push(\"1 #{pi+1} #{pj+1}\")\n\tend\n\tpj +=1\n end\n pi += 1\nend\n\nputs actions.length\nactions.each { |t|\n puts t\n}\n"}, {"source_code": "def swap_cols(mat,i,j)\n\taux = 0\n\tn = mat.length\n\t(0..(n-1)).each {|k|\n\t\taux = mat[k][i]\n\t\tmat[k][i] = mat[k][j]\n\t\tmat[k][j] = aux\n\t}\nend\n\nn = gets.chomp.to_i\nmat = []\n(1..n).each {|pp|\n line = []\n (1..n).each {|qq|\n\tline.push(0)\n }\n mat.push(line)\n}\n\n(1..(n-1)).each { |k|\n i,j = gets.chomp.split(\" \").collect! {|x| x.to_i}\n mat[i-1][j-1] = 1 \n}\n\nactions = []\narr = []\npos = []\nm = 0\naaux = []\n(0..(n-1)).each { |k|\n\tp = m\n\tq = m\n\twhile mat[k][p]==1\n\t q +=1\n\t p +=1\n\t m +=1\n\tend\n\twhile q=0\n st -=1\n end\n arr.push(st)\n}\npi = 0\npj = 0\nwhile piarr[pj]\n\t\taux = arr[pi]\n\t\tarr[pi] = arr[pj]\n\t\tarr[pj] = aux\n\t\t\n\t\taux = pos[pi]\n\t\tpos[pi] = pos[pj]\n\t\tpos[pj] = aux\n\t\t\n\t\tactions.push(\"1 #{pos[pi]+1} #{pos[pj]+1}\")\n\tend\n\tpj +=1\n end\n pi += 1\nend\n\nputs actions.length\nactions.each { |t|\n puts t\n}\n"}, {"source_code": "def swap_cols(mat,i,j)\n\taux = 0\n\tn = mat.length\n\t(0..(n-1)).each {|k|\n\t\taux = mat[k][i]\n\t\tmat[k][i] = mat[k][j]\n\t\tmat[k][j] = aux\n\t}\nend\n\nn = gets.chomp.to_i\nmat = []\n(1..n).each {|pp|\n line = []\n (1..n).each {|qq|\n\tline.push(0)\n }\n mat.push(line)\n}\n\n(1..(n-1)).each { |k|\n i,j = gets.chomp.split(\" \").collect! {|x| x.to_i}\n mat[i-1][j-1] = 1 \n}\n\nactions = []\narr = []\npos = []\nm = 0\naaux = []\n(0..(n-1)).each { |k|\n\tp = m\n\tq = m\n\twhile mat[k][p]==1\n\t q +=1\n\t p +=1\n\t m +=1\n\tend\n\twhile q=0\n st -=1\n end\n arr.push(st)\n}\npi = 0\npj = 0\nwhile piarr[pj]\n\t\taux = arr[pi]\n\t\tarr[pi] = arr[pj]\n\t\tarr[pj] = aux\n\t\t\n\t\taux = pos[pi]\n\t\tpos[pi] = pos[pj]\n\t\tpos[pj] = aux\n\t\t\n\t\tactions.push(\"1 #{pi+1} #{pj+1}\")\n\tend\n\tpj +=1\n end\n pi += 1\nend\n\nputs actions.length\nactions.each { |t|\n puts t\n}\n"}], "src_uid": "3b2c5410441e588806690056693514a8"} {"nl": {"description": "A new e-mail service \"Berlandesk\" is going to be opened in Berland in the near future. The site administration wants to launch their project as soon as possible, that's why they ask you to help. You're suggested to implement the prototype of site registration system. The system should work on the following principle. Each time a new user wants to register, he sends to the system a request with his name. If such a name does not exist in the system database, it is inserted into the database, and the user gets the response OK, confirming the successful registration. If the name already exists in the system database, the system makes up a new user name, sends it to the user as a prompt and also inserts the prompt into the database. The new name is formed by the following rule. Numbers, starting with 1, are appended one after another to name (name1, name2, ...), among these numbers the least i is found so that namei does not yet exist in the database.", "input_spec": "The first line contains number n (1 ≤ n ≤ 105). The following n lines contain the requests to the system. Each request is a non-empty line, and consists of not more than 32 characters, which are all lowercase Latin letters.", "output_spec": "Print n lines, which are system responses to the requests: OK in case of successful registration, or a prompt with a new name, if the requested name is already taken.", "sample_inputs": ["4\nabacaba\nacaba\nabacaba\nacab", "6\nfirst\nfirst\nsecond\nsecond\nthird\nthird"], "sample_outputs": ["OK\nOK\nabacaba1\nOK", "OK\nfirst1\nOK\nsecond1\nOK\nthird1"], "notes": null}, "positive_code": [{"source_code": "bd = {}\ngets.to_i.times do\n\tname = gets.chomp\n\tif bd.has_key?(name)\n\t\tbd[name] += 1\n\t\tputs name + bd[name].to_s\n\telse\n\t\tbd[name] = 0\n\t\tputs \"OK\"\n\tend\nend"}, {"source_code": "a={}\n\"1\n\".upto(gets){puts a[gets.chomp!]?$_+(a[$_]+=1).to_s: (a[$_]=0;:OK)}"}, {"source_code": "users = Hash.new\n\nn = gets.to_i\nn.times do\n\tname = gets.chomp\n\tif(users[name]==nil) then\n\t\tputs(\"OK\")\n\t\tusers[name] = 1\n\telse\n\t\tputs(\"#{name}#{users[name]}\")\n\t\tusers[name] += 1\n\tend\nend\n"}, {"source_code": "n = gets.to_i\n\nbase = {}\n\nanswer = []\n\nn.times do |i|\n z = gets.chomp\n if base[z] then\n answer << (z + base[z].to_s)\n base[z] += 1\n else\n answer << \"OK\"\n base[z] = 1\n end\nend\n\nanswer.map{|i| puts i}"}, {"source_code": "n = gets.chomp.to_i\n\ndb = Hash.new\nfor i in 0..n-1\n\ts = gets.chomp\n\tif db.has_key?(s)\n\t\tidx = db[s] + 1\n\t\twhile true\n\t\t\tss = s + idx.to_s\n\t\t\tif not db.has_key?(ss)\n\t\t\t\tdb[s] = idx\n\t\t\t\tdb[ss] = 0\n\t\t\t\tputs ss\n\t\t\t\tbreak\n\t\t\telse\n\t\t\t\tidx += 1\n\t\t\tend\n\t\tend\n\telse\n\t\tdb[s] = 0\n\t\tputs \"OK\"\n\tend\nend\n\n"}, {"source_code": "#!/usr/bin/ruby\nn=gets.to_i\nh=Hash.new\nn.times{|i|\n x=s=gets.chomp\n if !h[s]\n h[s]=0\n puts \"OK\"\n else\n h[s]+=1\n puts s+h[s].to_s\n end\n}\n"}, {"source_code": "n = gets.to_i\nhash = Hash.new\nn.times do\n\tstr = gets.chomp!\n\tif hash.key?(str)\n\t\thash[str] += 1\n\t\tputs str + hash[str].to_s\n\telse\n\t\thash[str] = 0\n\t\tputs 'OK'\n\tend\nend"}, {"source_code": "n=STDIN.gets.chomp.to_i\ndb={}\ndb.default=-1\nn.times() {\n name=STDIN.gets.chomp\n db[name]+=1\n if 0 1 ? \"#{x}#{h[x]-1}\" : 'OK' \nend"}, {"source_code": "c = gets\n\nmap = Hash.new\n\nc.to_i.times {\n\n name = gets.strip\n\n if map[name] == nil\n puts \"OK\"\n map[name] = 0\n else\n map[name] += 1\n puts name + map[name].to_s\n end\n \n}\n\n"}, {"source_code": "n = gets.to_i\ndata = Hash.new\nn.times do\n name = gets.chop!\n v = data[name]\n if(v == nil) then v = 0; puts \"OK\" \n else puts name + v.to_s end\n data[name] = v + 1\nend\n"}, {"source_code": "n= gets.to_i\nres=[]\nnames ={}\nwhile n>0\nna=gets.chomp\nif names[na]\n names[na]=names[na]+1\n res.push (na+names[na].to_s)\nelse\n res.push \"OK\"\n names[na]=0\nend\nn=n-1\nend\n\nres.each{|a| puts a.to_s }\n"}, {"source_code": "n = Integer gets\nnames = Hash.new\nfor i in 0...n\n\tstr = gets.chomp\n\tif names.has_key? str\n\t print str, names[str], \"\\n\"\n\t names[str] += 1\n else\n puts \"OK\"\n names[str] = 1\n end\nend"}, {"source_code": "a={}\ngets.to_i.times{q=a[s=gets.chop];puts q ?s+q.to_s: \"OK\";a[s]=q.to_i+1}\n"}, {"source_code": "n = gets.to_i\n\ndatabase = {}\n\nn.times do\n\tusername = gets.chomp\n\tif database[username].nil?\n\t\tdatabase[username] = 1\n\t\tputs \"OK\"\n\telse\n\t\tputs username + database[username].to_s\n\t\tdatabase[username] += 1\n\tend\nend"}, {"source_code": "#Made By Hasbi Hamza\nn=gets.chomp.to_i\nhash={}\nlines=[]\nfor i in 1..n\n lines[i]=gets.chomp.to_s\n hash[lines[i]]=0\nend\nfor i in 1..n\n if hash[lines[i]]==0 then puts \"OK\"\n else puts(\"#{lines[i]}#{hash[lines[i]]}\")\nend\n hash[lines[i]]+=1\nend"}, {"source_code": "n = gets.to_i\na = Hash.new(0)\nn.times do\n s = gets.chomp\n if a[s] == 0\n a[s] = 1\n puts 'OK'\n else\n puts s + a[s].to_s\n a[s] += 1\n end\nend"}, {"source_code": "a={}\n\"1\n\".upto(gets){puts a[gets.chomp!]?$_+(a[$_]+=1).to_s: (a[$_]=0;:OK)}\n"}, {"source_code": "n = gets.chomp.to_i\nh = {}\nn.times do\n s = gets.chomp\n i = h[s]\n if i.nil?\n puts 'OK'\n h[s] = 1\n else\n puts \"#{s}#{h[s]}\"\n h[s] += 1\n end\nend"}, {"source_code": "class Array\n def r\n self.map { |q| q.to_i }\n end\nend\n\ndef read_line\n STDIN.gets.split(\" \")\nend\n\ndef read_to_array\n read_line.to_a\nend\n\ndef read_numbers_to_array\n read_to_array.r\nend\n\na = Hash.new(0)\n\nn = STDIN.gets.to_i\nn.times do\n str = read_to_array[0]\n a[str] += 1\n if a[str] > 1\n print \"#{str}#{a[str] - 1}\"\n puts\n else\n puts \"OK\"\n end\nend\n"}, {"source_code": "n = gets.to_i\na = Hash.new(0)\nn.times do\n s = gets.chomp\n puts a[s] >= 1 ? \"#{s}#{a[s]}\" : \"OK\"\n a[s] += 1\nend\n"}, {"source_code": "n = gets.to_i\na = Hash.new(0)\nn.times do\n s = gets.chomp\n if a[s] >= 1\n puts s + a[s].to_s\n a[s] += 1\n else\n a[s] = 1\n puts 'OK'\n end\nend\n"}, {"source_code": "n = gets.to_i\na = Hash.new(false)\nn.times do\n s = gets.chomp\n if a[s]\n a[s] += 1\n puts s + a[s].to_s\n else\n a[s] = 0\n puts 'OK'\n end\nend\n"}, {"source_code": "n = gets.chomp.to_i\nh = {}\nn.times do\n s = gets.chomp\n if h[s].nil? then\n puts 'OK'\n h[s] = 1\n else\n puts s + h[s].to_s\n h[s] += 1\n end \nend\n"}, {"source_code": "a={}\n\"1\n\".upto(gets){puts a[gets.chomp!]?$_+(a[$_]+=1).to_s: (a[$_]=0;:OK)}"}, {"source_code": "names = {}\nnoNames = gets.chomp.to_i\n(1..noNames).each do |name|\n name = gets.chomp\n if names[name] == nil\n names[name] = 1\n puts \"OK\"\n else\n newName = \"#{name}#{names[name]}\"\n names[name] += 1\n names[newName] = 1\n puts newName\n end\nend"}, {"source_code": "a={}\n\"1\n\".upto(gets){puts a[gets.chomp!]?$_+(a[$_]+=1).to_s: (a[$_]=0;:OK)}"}, {"source_code": "numOfInputs = gets.to_i\ninputHash = {}\nnumOfInputs.downto(1) do\n input = gets.chomp\n if (inputHash.has_key?(input))\n numOfOccurence = inputHash[input]\n \n puts \"#{input}#{numOfOccurence}\"\n \n numOfOccurence = numOfOccurence + 1\n inputHash[input] = numOfOccurence\n else\n inputHash[input] = 1\n puts \"OK\"\n end \nend\n"}, {"source_code": "n = gets.to_i\ndb = {}\n\nwhile n > 0 do\n u = gets[0..-2]\n if db.key?(u)\n printf(\"%s%d\\n\", u, db[u])\n db[u] += 1\n else\n puts \"OK\"\n db[u] = 1\n end\n n -= 1\nend"}, {"source_code": "n = gets.to_i\ndb = {}\n\nwhile n > 0 do\n u = gets.chomp\n if db.has_key?(u)\n puts u + db[u].to_s\n db[u] += 1\n else\n puts \"OK\"\n db[u] = 1\n end\n n -= 1\nend"}, {"source_code": "n = gets.to_i\ndb = {}\n\nwhile n > 0 do\n u = gets.chomp\n if db.has_key?(u)\n printf(\"%s%d\\n\", u, db[u])\n db[u] += 1\n else\n puts \"OK\"\n db[u] = 1\n end\n n -= 1\nend"}, {"source_code": "a={}\n\"1\n\".upto(gets){puts a[gets.chomp!]?$_+(a[$_]+=1).to_s: (a[$_]=0;:OK)}"}, {"source_code": "h=Hash.new\ngets\n$<.map{|n|\n\ts=n.chop\n if(h.has_key?(s))\n h[s]+=1\n puts s+h[s].to_s\n else\n \th[s] = 0\n puts \"OK\"\n end\n}"}, {"source_code": "n = gets.chomp.to_i\n\nhash = {}\n\nn.times do |i| \n name = gets.chomp\n if hash[name] == nil\n hash[name] = 1\n puts \"OK\"\n else\n puts \"#{name}#{hash[name]}\"\n hash[name] += 1\n end\nend"}, {"source_code": "a={}\n\"1\n\".upto(gets){puts a[gets.chomp!]?$_+(a[$_]+=1).to_s: (a[$_]=0;:OK)}\n"}, {"source_code": "n = gets.to_i\n\ncache = Hash.new { 0 }\n\n1.upto(n) do\n name = gets.chomp\n\n puts cache[name].zero? ? 'OK' : \"#{name}#{cache[name]}\"\n\n cache[name] += 1\nend\n"}, {"source_code": "a = {}\nfor _ in 1..gets.chomp.to_i\n name = gets.chomp\n if a.has_key? name\n a[name] += 1\n puts \"#{name}#{a[name]}\"\n else\n puts 'OK'\n a[name] = 0\n end\nend"}, {"source_code": " \na={}\n\"1\n\".upto(gets){puts a[gets.chomp!]?$_+(a[$_]+=1).to_s: (a[$_]=0;:OK)}"}, {"source_code": "n = gets.chomp.to_i\n\nhash = {}\n\n1.upto(n) do \n\tname = gets.chomp\n\tif hash[name] == nil\n\t\thash[name] = 1\n\t\tputs \"OK\"\n\telse\n\t\tputs \"#{name}#{hash[name]}\"\n\t\thash[name] += 1\n\tend\nend\n"}, {"source_code": "a={}\n\"1\n\".upto(gets){puts a[gets.chomp!]?$_+(a[$_]+=1).to_s: (a[$_]=0;:OK)}\n"}, {"source_code": "a={}\n\"1\n\".upto(gets){puts a[gets.chomp!]?$_+(a[$_]+=1).to_s: (a[$_]=0;:OK)}"}, {"source_code": "n, db = gets.to_i, Hash.new(-1)\nn.times do\n\tname = gets.chomp.to_sym\n\tdb[name] += 1\n\tif db[name] > 0\n\t\tprint name, db[name], \"\\n\"\n\telse\n\t\tputs :OK\n\tend\nend\n"}, {"source_code": "a={}\n\"1\n\".upto(gets){puts a[gets.chomp!]?$_+(a[$_]+=1).to_s: (a[$_]=0;:OK)}\n"}, {"source_code": "accountNum = gets.chomp.to_i\n\nhash = {}\nnumberHash = {}\n\naccountNum.times do\n\taccount = gets.chomp\n\n\tif hash.has_key? account\n\t\tnumberHash[account] += 1\n\t\tnewAccount = account + numberHash[account].to_s\n\t\tputs \"#{newAccount}\"\n\telse\n\t\tputs 'OK'\n\t\thash[account] = 1\n\t\tnumberHash[account] = 0\n\tend\nend"}], "negative_code": [{"source_code": "n=STDIN.gets.chomp.to_i\ndb={}\ndb.default=0\nn.times() {\n name=STDIN.gets.chomp\n db[name]+=1\n if 1 b1.size \n\t\t\tputs ans\n\t\telse\n\t\t\tif (b2-b1).empty?\n\t\t\t\tputs ans\n\t\t\telse\n\t\t\t\tputs ((b2-b1).size)+ans\n\t\t\tend\n\t\tend\n\telse\n\t\tputs ans\n\tend\nend\n\nmain"}, {"source_code": "def main()\n\tn=gets.chomp.to_i\n\tb1=[]\n\tb2=[]\n\tans=0\n\tfor i in 0...n\n\t\tini=gets.chomp.split(\" \")\n\t\tini=ini.map{|i| i.to_i}\n\t\tai=ini[0]\n\t\tbi=ini[1]\n\t\tif ai==bi\n\t\t\tans+=1\n\t\tend\n\t\tb1 << ai\n\t\tb2 << bi\n\tend\n\tputs (b2-b1).size+ans\nend\nmain"}, {"source_code": "def main()\n\tn=gets.chomp.to_i\n\tans=0\n\ths={}\n\tfor i in 0...n\n\t\tini=gets.chomp.split(\" \")\n\t\tini=ini.map{|i| i.to_i}\n\t\tai=ini[0]\n\t\tbi=ini[1]\n\t\ths[ai]=bi;\n\tend\n\tkeys=hs.keys\n\tfor i in keys\n\t\tb=i;\n\t\tv=hs[b]\n\t\tif v==b\n\t\t\tans+=1;\n\t\telsif !hs.has_value?(v)\n\t\t\tans+=1\n\t\tend\n\tend\n\tputs ans\nend\nmain"}, {"source_code": "def main()\n\tn=gets.chomp.to_i\n\tb1=[]\n\tb2=[]\n\tcheck=[]\n\tans=0\n\tfor i in 0...n\n\t\tini=gets.chomp.split(\" \")\n\t\tini=ini.map{|i| i.to_i}\n\t\tai=ini[0]\n\t\tbi=ini[1]\n\t\tif ai==bi\n\t\t\tans+=1\n\t\t\tcheck << bi\n\t\telse\n\t\t\tb1 << ai\n\t\t\tb2 << bi\n\t\tend\n\tend\n\tfor i in check\n\t\tbx=i;\n\t\tind=b2.index(bx)\n\t\tif !ind.nil?\n\t\t\tans-=1\n\t\tend\n\tend\n\tputs ans\nend\nmain"}, {"source_code": "def main()\n\tnm=gets.chomp.split(\" \")\n\tn=nm[0].to_i\n\tm=nm[1].to_i\n\tar=gets.chomp.split(\" \")\n\tar=ar.map{|i| i.to_i}\n\tinc=0\n\tfor i in 0...m\n\t\tstop=gets.chomp\n\t\tif stop[0].chr==\"3\"\n\t\t\tinop=stop.split(\" \")\n\t\t\tinop=inop.map{|i| i.to_i}\n\t\t\tputs ar[inop[1]-1]+inc\n\t\telsif stop[0].chr==\"2\"\n\t\t\tinop=stop.split(\" \")\n\t\t\tinop=inop.map{|i| i.to_i}\n\t\t\tinc+=inop[1]\n\t\telse\n\t\t\tinop=stop.split(\" \")\n\t\t\tinop=inop.map{|i| i.to_i}\n\t\t\tar[inop[1]-1]=inop[2]-inc\n\t\tend\n\t\t\t\n\tend\nend\nmain"}, {"source_code": "def main()\n\tn=gets.chomp.to_i\n\tb1=[]\n\tb2=[]\n\tcheck=[]\n\tans=0\n\tfor i in 0...n\n\t\tini=gets.chomp.split(\" \")\n\t\tini=ini.map{|i| i.to_i}\n\t\tai=ini[0]\n\t\tbi=ini[1]\n\t\tif ai==bi\n\t\t\tans+=1\n\t\t\tcheck << bi\n\t\telse\n\t\t\tb1 << ai\n\t\t\tb2 << bi\n\t\tend\n\tend\n\tfor i in check\n\t\tbx=i;\n\t\tind=b2.index(bx)\n\t\tif !ind.nil?\n\t\t\tans-=1\n\t\tend\n\tend\n\tb1=b1.uniq\n\tb2=b2.uniq\n\tputs (b1-b2).size+ans\nend\nmain"}, {"source_code": "n = gets.chomp.to_i\nbrand_to_count = {}\nbrand_to_can_open = {}\nbrand_to_same_count = {}\nn.times do\n a, b = gets.chomp.split(\" \").map(&:to_i)\n if a == b \n brand_to_same_count[a] = brand_to_same_count.fetch(a, 0) + 1\n else\n brand_to_can_open[b] = brand_to_can_open.fetch(b, 0) + 1\n brand_to_count[a] = brand_to_count.fetch(a, 0) + 1\n end\nend\ntotal_unopenable = 0\nbrand_to_same_count.each do |brand, count|\n if count == 1\n total_unopenable += 1\n end\nend\nbrand_to_count.each do |brand, count|\n # get count\n # see how many are openable from brand_to_can_open\n openable = brand_to_can_open.fetch(brand, 0)\n unopenable = [count - openable, 0].max\n total_unopenable += unopenable\nend\nputs total_unopenable\n"}, {"source_code": "array = Array.new(gets.to_i){ gets.split }\np (array.map(&:first) - array.select{ |i,j| i != j }.map(&:last)).size\n"}, {"source_code": "require 'set'\n\ndef run\n a = []\n s = Set.new\n n = $stdin.gets.to_i\n $stdin.readlines.each do |line|\n ai, bi = line.split.map(&:to_i)\n a << ai\n s.add(bi) if ai != bi\n end\n\n cnt = 0\n a.each do |ai|\n cnt += 1 unless s.include?(ai)\n end\n puts cnt\nend\n\nrun if $0 == __FILE__\n"}, {"source_code": "require 'set'\n\ndef run\n s = Set.new\n n = $stdin.gets.to_i\n $stdin.readlines.each do |line|\n a, b = line.split.map(&:to_i)\n s.add(b) if a != b\n end\n\n puts n - s.size\nend\n\nrun if $0 == __FILE__\n"}, {"source_code": "require 'set'\n\ndef run\n a = []\n b = []\n n = $stdin.gets.to_i\n $stdin.readlines.each do |line|\n ai, bi = line.split.map(&:to_i)\n a << ai\n b << bi\n end\n\n r = []\n n.times do |i|\n n.times do |j|\n r[i] = true if i != j and b[i] == a[j]\n end\n end\n\n puts n - r.count(true)\nend\n\nrun if $0 == __FILE__\n"}, {"source_code": "n = gets.to_i\na = []\nb = []\nn.times do\n s = gets.split.map(&:to_i)\n a << s[0]\n b << s[1] if s[0]!= s[1]\nend\nputs n - a.count{|i| b.include?(i)}"}, {"source_code": "n = gets.to_i\na = []\nb = []\nn.times do\n s = gets.split.map(&:to_i)\n a << s[0]\n b << s[1] if s[0]!= s[1]\nend\nx = []\nn.times do |i|\n n.times do |j|\n x << j if i!=j && a[i]!=b[i]\n end\nend\nputs n - x.uniq.size"}, {"source_code": "n = gets.to_i\na = []\nb = []\nn.times do\n s = gets.split.map(&:to_i)\n a << s[0]\n b << s[1]\nend\nx = []\nn.times do |i|\n n.times do |j|\n x << j if (i!=j and a[i]!=b[i])\n end\nend\nputs n - x.uniq.size"}], "src_uid": "84bd49becca69e126606d5a2f764dd91"} {"nl": {"description": "Alice and Bob are playing a game with strings. There will be $$$t$$$ rounds in the game. In each round, there will be a string $$$s$$$ consisting of lowercase English letters. Alice moves first and both the players take alternate turns. Alice is allowed to remove any substring of even length (possibly empty) and Bob is allowed to remove any substring of odd length from $$$s$$$.More formally, if there was a string $$$s = s_1s_2 \\ldots s_k$$$ the player can choose a substring $$$s_ls_{l+1} \\ldots s_{r-1}s_r$$$ with length of corresponding parity and remove it. After that the string will become $$$s = s_1 \\ldots s_{l-1}s_{r+1} \\ldots s_k$$$.After the string becomes empty, the round ends and each player calculates his/her score for this round. The score of a player is the sum of values of all characters removed by him/her. The value of $$$\\texttt{a}$$$ is $$$1$$$, the value of $$$\\texttt{b}$$$ is $$$2$$$, the value of $$$\\texttt{c}$$$ is $$$3$$$, $$$\\ldots$$$, and the value of $$$\\texttt{z}$$$ is $$$26$$$. The player with higher score wins the round. For each round, determine the winner and the difference between winner's and loser's scores. Assume that both players play optimally to maximize their score. It can be proved that a draw is impossible.", "input_spec": "The first line of input contains a single integer $$$t$$$ ($$$1\\leq t\\leq 5\\cdot 10^4$$$) denoting the number of rounds. Each of the next $$$t$$$ lines contain a single string $$$s$$$ ($$$1\\leq |s|\\leq 2\\cdot 10^5$$$) consisting of lowercase English letters, denoting the string used for the round. Here $$$|s|$$$ denotes the length of the string $$$s$$$. It is guaranteed that the sum of $$$|s|$$$ over all rounds does not exceed $$$2\\cdot 10^5$$$.", "output_spec": "For each round, print a single line containing a string and an integer. If Alice wins the round, the string must be \"Alice\". If Bob wins the round, the string must be \"Bob\". The integer must be the difference between their scores assuming both players play optimally.", "sample_inputs": ["5\n\naba\n\nabc\n\ncba\n\nn\n\ncodeforces"], "sample_outputs": ["Alice 2\nAlice 4\nAlice 4\nBob 14\nAlice 93"], "notes": "NoteFor the first round, $$$\\texttt{\"aba\"}\\xrightarrow{\\texttt{Alice}}\\texttt{\"}{\\color{red}{\\texttt{ab}}}\\texttt{a\"}\\xrightarrow{} \\texttt{\"a\"}\\xrightarrow{\\texttt{Bob}}\\texttt{\"}{\\color{red}{\\texttt{a}}}\\texttt{\"}\\xrightarrow{}\\texttt{\"\"}$$$. Alice's total score is $$$1+2=3$$$. Bob's total score is $$$1$$$.For the second round, $$$\\texttt{\"abc\"}\\xrightarrow{\\texttt{Alice}}\\texttt{\"a}{\\color{red}{\\texttt{bc}}}\\texttt{\"}\\xrightarrow{} \\texttt{\"a\"}\\xrightarrow{\\texttt{Bob}}\\texttt{\"}{\\color{red}{\\texttt{a}}}\\texttt{\"}\\xrightarrow{}\\texttt{\"\"}$$$. Alice's total score is $$$2+3=5$$$. Bob's total score is $$$1$$$.For the third round, $$$\\texttt{\"cba\"}\\xrightarrow{\\texttt{Alice}}\\texttt{\"}{\\color{red}{\\texttt{cb}}}\\texttt{a\"}\\xrightarrow{} \\texttt{\"a\"}\\xrightarrow{\\texttt{Bob}}\\texttt{\"}{\\color{red}{\\texttt{a}}}\\texttt{\"}\\xrightarrow{}\\texttt{\"\"}$$$. Alice's total score is $$$3+2=5$$$. Bob's total score is $$$1$$$.For the fourth round, $$$\\texttt{\"n\"}\\xrightarrow{\\texttt{Alice}}\\texttt{\"n\"}\\xrightarrow{} \\texttt{\"n\"}\\xrightarrow{\\texttt{Bob}}\\texttt{\"}{\\color{red}{\\texttt{n}}}\\texttt{\"}\\xrightarrow{}\\texttt{\"\"}$$$. Alice's total score is $$$0$$$. Bob's total score is $$$14$$$.For the fifth round, $$$\\texttt{\"codeforces\"}\\xrightarrow{\\texttt{Alice}}\\texttt{\"}{\\color{red}{\\texttt{codeforces}}}\\texttt{\"}\\xrightarrow{} \\texttt{\"\"}$$$. Alice's total score is $$$3+15+4+5+6+15+18+3+5+19=93$$$. Bob's total score is $$$0$$$."}, "positive_code": [{"source_code": "gets.to_i.times do\n a = gets.chomp.split('').map{|c| c.ord - 'a'.ord + 1}\n s = a.inject(:+)\n if a.size % 2 == 0 then\n alice = s\n bob = 0\n else\n if a.size == 1 then\n bob = s\n alice = 0\n else\n bob = [a[0],a[a.size-1]].min\n alice = s - bob\n end\n end\n if alice > bob then\n puts \"Alice #{alice-bob}\"\n else\n puts \"Bob #{bob-alice}\"\n end\nend\n"}, {"source_code": "gets.to_i.times do\r\n s = gets.chomp\r\n chars = s.chars.map { |ch| ch.ord - 97 + 1 }\r\n alice, bob = if s.size.even?\r\n [chars.sum, 0]\r\n else\r\n min = [chars[0], chars[-1]].min\r\n [chars.sum - min, min]\r\n end\r\n puts \"#{alice > bob ? \"Alice\" : \"Bob\"} #{(alice - bob).abs}\"\r\nend\r\n"}, {"source_code": "t = gets.to_i\r\nalphabet = \" abcdefghijklmnopqrstuvwxyz\"\r\nf=->s{s.chars.sum{alphabet.index(_1)}}\r\nt.times do |cas|\r\n s = gets.chomp\r\n if s.size%2==0\r\n puts \"Alice %d\"%[f[s]]\r\n else\r\n a = [f[s[1..]], f[s[..-2]]].max\r\n b = f[s] - a\r\n if a > b\r\n puts \"Alice %d\"%(a-b)\r\n else\r\n puts \"Bob %d\"%(b-a)\r\n end\r\n end\r\nend\r\n\r\n\r\n# (a+b+c)\r\n# (bc + ac + ab) + ad + bd + cd = 0\r\n# d(a+b+c) + e(a+b+c) = -(bc + ac + ab)\r\n# d + e = -(bc + ac + ab + de) /(a+b+c)"}], "negative_code": [{"source_code": "gets.to_i.times do\n a = gets.chomp.split('').map{|c| c.ord - 'a'.ord + 1}\n s = a.inject(:+)\n puts s \n if a.size % 2 == 0 then\n puts \"Alice #{s}\"\n else\n if a.size == 1 then\n puts \"Bob #{s}\"\n else\n puts \"Alice #{s - 2*[a[0],a[a.size-1]].min}\"\n end\n end\nend\n"}], "src_uid": "8f02891aa9d2fcd1963df3a4028aa5c0"} {"nl": {"description": "Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules.In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba.Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given n patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle?", "input_spec": "The first line contains a single integer n (1 ≤ n ≤ 105) — the number of patterns. Next n lines contain the patterns. It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters.", "output_spec": "In a single line print the answer to the problem — the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them.", "sample_inputs": ["2\n?ab\n??b", "2\na\nb", "1\n?a?b"], "sample_outputs": ["xab", "?", "cacb"], "notes": "NoteConsider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on."}, "positive_code": [{"source_code": "patterns = Array.new(gets.to_i){ gets.chomp }\npat = []\nfor i in 0...patterns[0].length\n sym = ''\n for j in 0...patterns.length\n if patterns[j][i] != '?' && sym != '?'\n if sym.length == 0\n sym = patterns[j][i]\n else\n if sym != patterns[j][i]\n sym = '?'\n end\n end\n end\n end\n pat << (sym.length == 0 ? 'x' : sym)\nend\n\nputs pat.join\n"}], "negative_code": [], "src_uid": "a51d2e6e321d7db67687a594a2b85e47"} {"nl": {"description": "There are $$$n$$$ students in the first grade of Nlogonia high school. The principal wishes to split the students into two classrooms (each student must be in exactly one of the classrooms). Two distinct students whose name starts with the same letter will be chatty if they are put in the same classroom (because they must have a lot in common). Let $$$x$$$ be the number of such pairs of students in a split. Pairs $$$(a, b)$$$ and $$$(b, a)$$$ are the same and counted only once.For example, if there are $$$6$$$ students: \"olivia\", \"jacob\", \"tanya\", \"jack\", \"oliver\" and \"jessica\", then: splitting into two classrooms (\"jack\", \"jacob\", \"jessica\", \"tanya\") and (\"olivia\", \"oliver\") will give $$$x=4$$$ ($$$3$$$ chatting pairs in the first classroom, $$$1$$$ chatting pair in the second classroom), splitting into two classrooms (\"jack\", \"tanya\", \"olivia\") and (\"jessica\", \"oliver\", \"jacob\") will give $$$x=1$$$ ($$$0$$$ chatting pairs in the first classroom, $$$1$$$ chatting pair in the second classroom). You are given the list of the $$$n$$$ names. What is the minimum $$$x$$$ we can obtain by splitting the students into classrooms?Note that it is valid to place all of the students in one of the classrooms, leaving the other one empty.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$1\\leq n \\leq 100$$$) — the number of students. After this $$$n$$$ lines follow. The $$$i$$$-th line contains the name of the $$$i$$$-th student. It is guaranteed each name is a string of lowercase English letters of length at most $$$20$$$. Note that multiple students may share the same name.", "output_spec": "The output must consist of a single integer $$$x$$$ — the minimum possible number of chatty pairs.", "sample_inputs": ["4\njorge\njose\noscar\njerry", "7\nkambei\ngorobei\nshichiroji\nkyuzo\nheihachi\nkatsushiro\nkikuchiyo", "5\nmike\nmike\nmike\nmike\nmike"], "sample_outputs": ["1", "2", "4"], "notes": "NoteIn the first sample the minimum number of pairs is $$$1$$$. This can be achieved, for example, by putting everyone except jose in one classroom, and jose in the other, so jorge and jerry form the only chatty pair.In the second sample the minimum number of pairs is $$$2$$$. This can be achieved, for example, by putting kambei, gorobei, shichiroji and kyuzo in one room and putting heihachi, katsushiro and kikuchiyo in the other room. In this case the two pairs are kambei and kyuzo, and katsushiro and kikuchiyo.In the third sample the minimum number of pairs is $$$4$$$. This can be achieved by placing three of the students named mike in one classroom and the other two students in another classroom. Thus there will be three chatty pairs in one classroom and one chatty pair in the other classroom."}, "positive_code": [{"source_code": "def sum(m) \n\ts = 0 \n\tfor i,k in m \n\t\ttemp = k / 2 \n\t\ts += temp * (temp-1) / 2 \n\t\ts += (k-temp) * (k-temp-1) /2 \n\tend\n\treturn s \nend\n\nn = gets.to_i \nm = Hash.new\nm.default = 0 \nn.times do \n\tt = gets\n\tm[t[0]] += 1\nend\nputs(sum(m))\n\n"}, {"source_code": "n = gets.to_i\ncnt = Hash.new(0)\nn.times do\n s = gets.chomp\n cnt[s[0]] += 1\nend\nans = 0\ncnt.each do |k,v|\n x = v/2 + v%2\n ans += x * (x-1) / 2\n ans += (v-x) * (v-x-1) /2\nend\np ans\n"}, {"source_code": "n = gets.to_i\na = Hash.new(0)\nn.times do\n a[gets[0]] += 1\nend\nans = 0\na.values.each do |i|\n ans += i/2*(i/2-1)/2+(i-i/2)*(i-i/2-1)/2\nend\nputs ans"}, {"source_code": "def combinations(n)\n Math.gamma(n + 1) / (Math.gamma(3) * Math.gamma((n - 2) + 1))\nend\n\nstudents_count = gets.chomp\ngroups = {}\n\n(1..students_count.to_i).each do |_n|\n name = gets.chomp\n groups[name[0]] = groups[name[0]] ? groups[name[0]] + 1 : 1\nend\n\nmin = 0\ngroups.each_value do |count|\n next if count <= 2\n min += count.even? ? combinations(count / 2) * 2 : combinations(count / 2) + combinations((count / 2) + 1)\nend\n\nprint min.to_i\n"}, {"source_code": "def combinations(n)\n Math.gamma(n + 1) / (Math.gamma(3) * Math.gamma((n - 2) + 1))\nend\n\nstudents_count = gets.chomp\nstudents = []\ngroups = {}\n\n(1..students_count.to_i).each do |_n|\n name = gets.chomp\n students << name\n groups[name[0]] = groups[name[0]] ? groups[name[0]] + 1 : 1\nend\n\nmin = 0\ngroups.each do |_char, count|\n next if count <= 2\n min += count.even? ? combinations(count / 2) * 2 : combinations(count / 2) + combinations((count / 2) + 1)\nend\n\nprint min.to_i\n"}, {"source_code": "def combi2(m)\n return m * (m-1) / 2\nend\n\nn = gets.to_i\nstudents = {}\nfor i in 0...n do\n initial = gets.chomp[0]\n if students.include?(initial)\n students[initial] += 1\n else\n students[initial] = 1\n end\nend\nsum = 0\nstudents.each_value { |num|\n sum += combi2((num + 1) / 2) + combi2(num / 2)\n}\n\nputs sum\n"}], "negative_code": [{"source_code": "def combi2(m)\n return m * (m-1) / 2\nend\n\nn = gets.to_i\nstudents = {}\nfor i in 0...n do\n initial = gets.chomp[0]\n if students.include?(initial)\n students[initial] += 1\n else\n students[initial] = 1\n end\nend\nmax = 0\nstudents.each_value { |num|\n max = num if max < num\n}\n\nputs combi2((max + 1) / 2) + combi2(max / 2)\n"}], "src_uid": "22a3561ff70b802f080feaabc4a71298"} {"nl": {"description": "Nick had received an awesome array of integers $$$a=[a_1, a_2, \\dots, a_n]$$$ as a gift for his $$$5$$$ birthday from his mother. He was already going to explore its various properties but after unpacking he was disappointed a lot because the product $$$a_1 \\cdot a_2 \\cdot \\dots a_n$$$ of its elements seemed to him not large enough.He was ready to throw out the array, but his mother reassured him. She told him, that array would not be spoiled after the following operation: choose any index $$$i$$$ ($$$1 \\le i \\le n$$$) and do $$$a_i := -a_i - 1$$$.For example, he can change array $$$[3, -1, -4, 1]$$$ to an array $$$[-4, -1, 3, 1]$$$ after applying this operation to elements with indices $$$i=1$$$ and $$$i=3$$$. Kolya had immediately understood that sometimes it's possible to increase the product of integers of the array a lot. Now he has decided that he wants to get an array with the maximal possible product of integers using only this operation with its elements (possibly zero, one or more times, as many as he wants), it is not forbidden to do this operation several times for the same index. Help Kolya and print the array with the maximal possible product of elements $$$a_1 \\cdot a_2 \\cdot \\dots a_n$$$ which can be received using only this operation in some order.If there are multiple answers, print any of them.", "input_spec": "The first line contains integer $$$n$$$ ($$$1 \\leq n \\leq 10^{5}$$$) — number of integers in the array. The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$-10^{6} \\leq a_i \\leq 10^{6}$$$) — elements of the array", "output_spec": "Print $$$n$$$ numbers — elements of the array with the maximal possible product of elements which can be received using only this operation in some order from the given array. If there are multiple answers, print any of them.", "sample_inputs": ["4\n2 2 2 2", "1\n0", "3\n-3 -3 2"], "sample_outputs": ["-3 -3 -3 -3", "0", "-3 -3 2"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\ncnt = 0\nmaxi = 0\nn.times do |i|\n a[i] = -a[i]-1 if a[i] >= 0\n cnt += 1 if a[i] < 0\n if a[i].abs > a[maxi].abs\n maxi = i\n end\nend\nif cnt % 2 == 0\n puts a.join(\" \")\nelse\n a[maxi] = -a[maxi]-1\n puts a.join(\" \")\nend\n"}], "negative_code": [{"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\ncnt = 0\nmaxi = 0\nn.times do |i|\n a[i] = -a[i]-1 if a[i] >= 0\n cnt += 1 if a[i] < 0\n if a[i].abs > a[maxi]\n maxi = i\n end\nend\nif cnt % 2 == 0\n puts a.join(\" \")\nelse\n a[maxi] = -a[maxi]-1\n puts a.join(\" \")\nend\n"}], "src_uid": "b0561fee6236f0720f737ca41e20e382"} {"nl": {"description": "Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan.There are n boys and m girls among his friends. Let's number them from 0 to n - 1 and 0 to m - 1 separately. In i-th day, Drazil invites -th boy and -th girl to have dinner together (as Drazil is programmer, i starts from 0). If one of those two people is happy, the other one will also become happy. Otherwise, those two people remain in their states. Once a person becomes happy (or if he/she was happy originally), he stays happy forever.Drazil wants to know whether he can use this plan to make all his friends become happy at some moment.", "input_spec": "The first line contains two integer n and m (1 ≤ n, m ≤ 100). The second line contains integer b (0 ≤ b ≤ n), denoting the number of happy boys among friends of Drazil, and then follow b distinct integers x1, x2, ..., xb (0 ≤ xi < n), denoting the list of indices of happy boys. The third line conatins integer g (0 ≤ g ≤ m), denoting the number of happy girls among friends of Drazil, and then follow g distinct integers y1, y2, ... , yg (0 ≤ yj < m), denoting the list of indices of happy girls. It is guaranteed that there is at least one person that is unhappy among his friends.", "output_spec": "If Drazil can make all his friends become happy by this plan, print \"Yes\". Otherwise, print \"No\".", "sample_inputs": ["2 3\n0\n1 0", "2 4\n1 0\n1 2", "2 3\n1 0\n1 1"], "sample_outputs": ["Yes", "No", "Yes"], "notes": "NoteBy we define the remainder of integer division of i by k.In first sample case: On the 0-th day, Drazil invites 0-th boy and 0-th girl. Because 0-th girl is happy at the beginning, 0-th boy become happy at this day. On the 1-st day, Drazil invites 1-st boy and 1-st girl. They are both unhappy, so nothing changes at this day. On the 2-nd day, Drazil invites 0-th boy and 2-nd girl. Because 0-th boy is already happy he makes 2-nd girl become happy at this day. On the 3-rd day, Drazil invites 1-st boy and 0-th girl. 0-th girl is happy, so she makes 1-st boy happy. On the 4-th day, Drazil invites 0-th boy and 1-st girl. 0-th boy is happy, so he makes the 1-st girl happy. So, all friends become happy at this moment. "}, "positive_code": [{"source_code": "#!/usr/bin/ruby\n# coding: utf-8\n\na, b = gets.split.map &:to_i\nc = a.gcd b\ndata = Array.new(c, nil)\nx = gets.split.map &:to_i\nx.shift\nx.each {|_|\n data[_ % c] = true\n}\nx = gets.split.map &:to_i\nx.shift\nx.each {|_|\n data[_ % c] = true\n}\nputs data.all? ? \"Yes\" : \"No\""}, {"source_code": "#!/usr/bin/env ruby\n\nn, m = gets.split.map(&:to_i)\n_, *xs = gets.split.map(&:to_i)\n_, *ys = gets.split.map(&:to_i)\n\nx_happy = Array::new(n, false)\ny_happy = Array::new(m, false)\n\nxs.each { |i| x_happy[i] = true }\nys.each { |i| y_happy[i] = true }\n\n(n*m*2).times do |i|\n if x_happy[i%n] || y_happy[i%m]\n x_happy[i%n] = y_happy[i%m] = true\n end\nend\n\nif x_happy.all? && y_happy.all?\n puts \"Yes\"\nelse\n puts \"No\"\nend\n"}, {"source_code": "def g;gets.split.map(&:to_i)end\nUF=Hash.new{|_,k|k}\ndef u a,b;UF[f a]=UF[f b]end\ndef f c;c==UF[c]?c:UF[c]=f(UF[c])end\nn,m=g\nb,*x=g\nc,*y=g\nhappy = x+y.map{|i|i+n}\nif happy==[]\n puts \"No\"\nelse\n if happy.size>1\n happy[1..-1].each{|i|u happy[0],i}\n end\n 10000.times{|i|u i%n,n+i%m}\n puts (0..n+m-1).map{|i|f i}.uniq.size==1?\"Yes\":\"No\"\nend"}, {"source_code": "#!/usr/bin/ruby\n\n#read data\nparams = gets.split(' ').map(&:to_i)\nn = params[0].to_i\nm = params[1].to_i\n\nparams = gets.split(' ').map(&:to_i)\nb = params.shift\nhappy_b = params\n\n\nparams = gets.split(' ').map(&:to_i)\ng = params.shift\nhappy_g = params\n\n#puts happy_b.to_s\n#puts happy_g.to_s\n\nboys = Array.new(size=n, default=0)\ngirls = Array.new(size=m, default=0)\n\nhappy_b.each do |e|\n boys[e] = 1;\nend\n\nhappy_g.each do |e|\n girls[e] = 1;\nend\n\n#puts boys.to_s\n#puts girls.to_s\n\nres = false;\ntimes_to_run = n.lcm m #mua ha ha \ncounter = 0; \n#puts times_to_run\nwhile (counter < times_to_run && !res) do\n if (boys[counter % n] == 1 || girls[counter % m] == 1)\n if (boys[counter % n] == 1 && girls[counter % m] == 0)\n girls[counter % m] = 1;\n g = g + 1;\n elsif (boys[counter % n] == 0 && girls[counter % m] == 1)\n boys[counter % n] = 1;\n b = b + 1;\n end\n end\n\n # puts 'Iteration ' + counter.to_s\n # puts boys.to_s\n # puts girls.to_s\n # puts b,n,g,m\n counter = counter + 1\n if (b == n || g == m)\n res = true;\n break\n end\nend\n\nif res\n puts 'Yes'\nelse\n puts 'No'\nend"}, {"source_code": "n, m = gets.split.map(&:to_i)\nlbs = gets.split.map(&:to_i).drop(1)\nlgs = gets.split.map(&:to_i).drop(1)\nul = n + m - (lbs.count + lgs.count)\nbs = Array.new(n, false)\nlbs.each {|i| bs[i] = true}\ngs = Array.new(m, false)\nlgs.each {|i| gs[i] = true}\nlasti = 0\nfor i in 0..Float::INFINITY do\n bi, gi = i%n, i%m\n if bs[bi] || gs[gi]\n ul -= 1 unless bs[bi]\n ul -= 1 unless gs[gi]\n lasti = i unless bs[bi] && gs[gi]\n bs[bi] = gs[gi] = true\n end\n break if ul == 0\n break if i - lasti > m*n\nend\nputs ul == 0 ? \"Yes\" : \"No\""}, {"source_code": "def create_list(n, hippies)\n result = []\n n.times do |i|\n result << hippies.include?(i)\n end\n result\nend\n\nnm = gets.chomp.split(' ').map{|i| i.to_i }\nn = nm[0]\nm = nm[1]\nhappy_ar = gets.chomp.split(' ').map{|i| i.to_i }\nhappy_amount = happy_ar[0]\nhappy_who_b = if happy_amount > 0 then happy_ar[1..-1] else [] end\nboys = create_list(n, happy_who_b)\n\nhappy_ar = gets.chomp.split(' ').map{|i| i.to_i }\nhappy_amount = happy_ar[0]\nhappy_who_g = if happy_amount > 0 then happy_ar[1..-1] else [] end\ngirls = create_list(m, happy_who_g)\n\n\ndef check(n, m, boys, girls)\n # gcd = n.gcd(m)\n (n * m * 2).times do |i|\n if boys[i % boys.length] or girls[i % girls.length]\n boys[i % boys.length] = true\n girls[i % girls.length] = true\n end\n end\n return (boys.all? and girls.all?)\nend\n\nputs check(n, m, boys, girls) ? 'Yes' : 'No'"}, {"source_code": "n, m = gets.split.map(&:to_i)\na = Array.new(n).map{0}\nb = Array.new(m).map{0}\na1 = gets.split.map(&:to_i)\nb1 = gets.split.map(&:to_i)\na1.each_with_index{|x, i|\nif i != 0\n a[x] = 1\nend \n}\nb1.each_with_index{|x, i|\n if i != 0\n b[x] = 1\n end \n}\nk1 = 0\nk2 = 0\nf1 = 0\nf2 = 0\nfor i in 0..3000000\n it1 = i % n \n it2 = i % m\n #print(\" it1 #{it1} it2 #{it2} \")\n if(a[it1] == 1 || b[it2] == 1)\n if(a[it1] == 0)\n a[it1] = 1\n k1 += 1\n end\n if(b[it2] == 0)\n b[it2] = 1\n k2 += 1\n end\n end\n if(n - (a1[0] + k1) == 0)\n f1 = 1 \n end\n if(m - (b1[0] + k2) == 0)\n f2 = 1 \n end\n if f2 == 1 && f1 == 1 \n break\n end\nend\n if f2 == 1 && f1 == 1 \n puts(\"Yes\")\n else \n puts(\"No\")\n end "}, {"source_code": "pa=Array.new(105,0)\npb=Array.new(105,0)\npc=Array.new\n\nstr=gets.to_s\npc=str.split\nn=pc[0].to_i\nm=pc[1].to_i\n\nk=n*m\nstr=gets.to_s\npc=str.split\nb=pc[0].to_i\nfor i in 1..b\n pa[pc[i].to_i]=1\nend\nstr=gets.to_s\npc=str.split\nb=pc[0].to_i\nfor i in 1..b\n pb[pc[i].to_i]=1\nend\n\nfor i in 0..k*2\n a=i%n\n b=i%m\n if pa[a]==1 || pb[b]==1\n pa[a]=pb[b]=1\n end\nend\n\n#for i in 0..n-1\n# puts pa[i]\n#end\n#puts\n#for i in 0..m-1\n# puts pb[i]\n#end\n\nh=0\nfor i in 0..n-1\n if pa[i]==0\n h=1\n break\n end\nend\n\nfor i in 0..m-1\n if pb[i]==0 || h==1\n h=1\n break\n end\nend\n\nif h==1\n puts \"No\"\nelsif h==0\n puts \"Yes\"\nend"}, {"source_code": "n, m = gets.strip.split.map(&:to_i)\na = gets.strip.split.map(&:to_i)\nb = gets.strip.split.map(&:to_i)\n\na.delete_at(0)\nb.delete_at(0)\n\nh1 = []\nh2 = []\n\n(0..(n+m)).each {|x|\n h1 << 0\n h2 << 0\n}\n\na.each { |x| h1[x] = 1 }\nb.each { |x| h2[x] = 1 }\n\n(0..(n*m*2)).each {|x|\n if h1[x % n] == 1 || h2[x % m] == 1\n h1[x % n] = 1\n h2[x % m] = 1\n end\n}\n\nres = 0\n(0..(n-1)).each {|x| res = 1 if h1[x] == 0}\n(0..(m-1)).each {|x| res = 1 if h2[x] == 0}\n\nif res == 1\n puts \"No\"\nelse\n puts \"Yes\"\nend"}], "negative_code": [{"source_code": "#!/usr/bin/env ruby\n\nn, m = gets.split.map(&:to_i)\nxs = gets.split.map(&:to_i)\nys = gets.split.map(&:to_i)\n\nx_happy = Array::new(n, false)\ny_happy = Array::new(m, false)\n\nxs[1, xs[0]].each do |x|\n x_happy[x] = true\nend\n\nys[1, ys[0]].each do |y|\n y_happy[y] = true\nend\n\ng = n.gcd(m)\n\n(n*m*2).times do |i|\n if x_happy[i%n] && y_happy[i%m]\n x_happy[i%n] = y_happy[i%m] = true\n end\nend\n\nif x_happy.all? && y_happy.all?\n puts \"Yes\"\nelse\n puts \"No\"\nend\n"}, {"source_code": "#!/usr/bin/env ruby\n\nn, m = gets.split.map(&:to_i)\nxs = gets.split.map(&:to_i)\nys = gets.split.map(&:to_i)\n\nx_happy = Array::new(n, false)\ny_happy = Array::new(m, false)\n\nxs[1, xs[0]].each do |x|\n x_happy[x] = true\nend\n\nys[1, ys[0]].each do |y|\n y_happy[y] = true\nend\n\ng = n.gcd(m)\n\n[x_happy.length, y_happy.length].min.times {\n x_happy.length.times do |i|\n y_happy.length.times do |j|\n if (i - j).abs % g == 0 && (x_happy[i] || y_happy[j])\n x_happy[i] = y_happy[j] = true\n end\n end\n end\n}\n\nif x_happy.all? && y_happy.all?\n puts \"Yes\"\nelse\n puts \"No\"\nend\n"}, {"source_code": "#!/usr/bin/ruby\n\n#read data\nparams = gets.split(' ').map(&:to_i)\nn = params[0].to_i\nm = params[1].to_i\n\nparams = gets.split(' ').map(&:to_i)\nb = params.shift\nhappy_b = params\n\n\nparams = gets.split(' ').map(&:to_i)\ng = params.shift\nhappy_g = params\n\n\nboys = Array.new(size=n, default=0)\ngirls = Array.new(size=m, default=0)\n\nhappy_b.each do |e|\n boys[e] = 1;\nend\n\nhappy_g.each do |e|\n girls[e] = 1;\nend\n\n#puts boys.to_s\n#puts girls.to_s\n\nres = false;\ntimes_to_run = n.lcm m #mua ha ha \ncounter = 0; \n#puts times_to_run\nwhile (counter < times_to_run && !res) do\n if (boys[counter % n] == 1 || girls[counter % m] == 1)\n if (boys[counter % n] == 1 && girls[counter % m] == 0)\n girls[counter % m] = 1;\n g = g + 1;\n elsif (boys[counter % n] == 0 && girls[counter % m] == 1)\n boys[counter % m] = 1;\n b = b + 1;\n end\n end\n\n # puts 'Iteration ' + counter.to_s\n # puts boys.to_s\n # puts girls.to_s\n counter = counter + 1\n if (b == n || g == m)\n res = true;\n end\nend\n\nif res\n puts 'Yes'\nelse\n puts 'No'\nend"}, {"source_code": "n, m = gets.split.map(&:to_i)\nlbs = gets.split.map(&:to_i).drop(1)\nlgs = gets.split.map(&:to_i).drop(1)\nul = n*m - (lbs.count + lgs.count)\nbs = Array.new(n, false)\nlbs.each {|i| bs[i] = true}\ngs = Array.new(m, false)\nlgs.each {|i| gs[i] = true}\nfor i in 0...n*m do\n bi, gi = i%n, i%m\n break if i > 0 && bi == 0 && gi == 0\n if bs[bi] || gs[gi]\n ul -= 1 unless bs[bi]\n ul -= 1 unless gs[gi]\n bs[bi] = gs[gi] = true\n end\n break if ul == 0\nend\nputs ul == 0 ? \"Yes\" : \"No\""}, {"source_code": "n, m = gets.split.map(&:to_i)\nlbs = gets.split.map(&:to_i).drop(1)\nlgs = gets.split.map(&:to_i).drop(1)\nul = n + m - (lbs.count + lgs.count)\nbs = Array.new(n, false)\nlbs.each {|i| bs[i] = true}\ngs = Array.new(m, false)\nlgs.each {|i| gs[i] = true}\nfor i in 0...n*m do\n bi, gi = i%n, i%m\n if bs[bi] || gs[gi]\n ul -= 1 unless bs[bi]\n ul -= 1 unless gs[gi]\n bs[bi] = gs[gi] = true\n end\n break if ul == 0\nend\nputs ul == 0 ? \"Yes\" : \"No\""}, {"source_code": "n, m = gets.split.map(&:to_i)\nlbs = gets.split.map(&:to_i).drop(1)\nlgs = gets.split.map(&:to_i).drop(1)\nul = n*m - (lbs.count + lgs.count)\nbs = Array.new(n, false)\nlbs.each {|i| bs[i] = true}\ngs = Array.new(m, false)\nlgs.each {|i| gs[i] = true}\nfor i in 0...n*m do\n bi, gi = i%n, i%m\n if bs[bi] || gs[gi]\n ul -= 1 unless bs[bi]\n ul -= 1 unless gs[gi]\n bs[bi] = gs[gi] = true\n end\n break if ul == 0\nend\nputs ul == 0 ? \"Yes\" : \"No\""}, {"source_code": "n, m = gets.split.map(&:to_i)\nlbs = gets.split.map(&:to_i).drop(1)\nlgs = gets.split.map(&:to_i).drop(1)\nul = n + m - (lbs.count + lgs.count)\nbs = Array.new(n, false)\nlbs.each {|i| bs[i] = true}\ngs = Array.new(m, false)\nlgs.each {|i| gs[i] = true}\nfor i in 0...n*m do\n bi, gi = i%n, i%m\n break if i > 0 && bi == 0 && gi == 0\n if bs[bi] || gs[gi]\n ul -= 1 unless bs[bi]\n ul -= 1 unless gs[gi]\n bs[bi] = gs[gi] = true\n end\n break if ul == 0\nend\nputs ul == 0 ? \"Yes\" : \"No\""}, {"source_code": "def create_list(n, hippies)\n result = []\n n.times do |i|\n result << hippies.include?(i)\n end\n result\nend\n\nnm = gets.chomp.split(' ').map{|i| i.to_i }\nn = nm[0]\nm = nm[1]\nhappy_ar = gets.chomp.split(' ').map{|i| i.to_i }\nhappy_amount = happy_ar[0]\nhappy_who_b = if happy_amount > 0 then happy_ar[1..-1] else [] end\nboys = create_list(n, happy_who_b)\n\nhappy_ar = gets.chomp.split(' ').map{|i| i.to_i }\nhappy_amount = happy_ar[0]\nhappy_who_g = if happy_amount > 0 then happy_ar[1..-1] else [] end\ngirls = create_list(m, happy_who_g)\n\ndef check(n, m, boys, girls, hboys, hgirls)\n # special case: if Drazil have the same number of girls and boys then\n # true would be only if in pairs at least one of them is happy\n if n == m\n n.times do |i|\n return false if not boys[i] and not girls[i]\n end\n return true\n end\n if (n-m).abs % 2 == 1\n return true\n end\n even = hboys.any?{|x| x % 2 == 0} or hgirls.any?{|x| x % 2 == 0}\n odd = hboys.any?{|x| x % 2 == 1} or hgirls.any?{|x| x % 2 == 1}\n return (even and odd)\nend\n\n\nputs check(n, m, boys, girls, happy_who_b, happy_who_g) ? 'Yes' : 'No'"}, {"source_code": "def create_list(n, hippies)\n result = []\n n.times do |i|\n result << hippies.include?(i)\n end\n result\nend\n\nnm = gets.chomp.split(' ').map{|i| i.to_i }\nn = nm[0]\nm = nm[1]\nhappy_ar = gets.chomp.split(' ').map{|i| i.to_i }\nhappy_amount = happy_ar[0]\nhappy_who_b = if happy_amount > 0 then happy_ar[1..-1] else [] end\nboys = create_list(n, happy_who_b)\n\nhappy_ar = gets.chomp.split(' ').map{|i| i.to_i }\nhappy_amount = happy_ar[0]\nhappy_who_g = if happy_amount > 0 then happy_ar[1..-1] else [] end\ngirls = create_list(m, happy_who_g)\n\n\ndef check(n, m, boys, girls)\n gcd = n.gcd(m)\n ((boys.length * girls.length) / gcd).times do |i|\n if boys[i % boys.length] or girls[i % girls.length]\n boys[i % boys.length] = true\n girls[i % girls.length] = true\n end\n end\n return (boys.all?{|x| x} and girls.all?{|x| x})\nend\n\nputs check(n, m, boys, girls) ? 'Yes' : 'No'"}, {"source_code": "def create_list(n, hippies)\n result = []\n n.times do |i|\n result << hippies.include?(i)\n end\n result\nend\n\nnm = gets.chomp.split(' ').map{|i| i.to_i }\nn = nm[0]\nm = nm[1]\nhappy_ar = gets.chomp.split(' ').map{|i| i.to_i }\nhappy_amount = happy_ar[0]\nhappy_who_b = if happy_amount > 0 then happy_ar[1..-1] else [] end\nboys = create_list(n, happy_who_b)\n\nhappy_ar = gets.chomp.split(' ').map{|i| i.to_i }\nhappy_amount = happy_ar[0]\nhappy_who_g = if happy_amount > 0 then happy_ar[1..-1] else [] end\ngirls = create_list(m, happy_who_g)\n\n\ndef check(n, m, boys, girls)\n gcd = n.gcd(m)\n if (n - m).abs % gcd != 0\n return true\n end\n ((boys.length * girls.length) / gcd).times do |i|\n if boys[i % boys.length] or girls[i % girls.length]\n boys[i % boys.length] = true\n girls[i % girls.length] = true\n end\n end\n return (boys.all?{|x| x} and girls.all?{|x| x})\nend\n\nputs check(n, m, boys, girls) ? 'Yes' : 'No'"}, {"source_code": "def create_list(n, hippies)\n result = []\n n.times do |i|\n result << hippies.include?(i)\n end\n result\nend\n\nnm = gets.chomp.split(' ').map{|i| i.to_i }\nn = nm[0]\nm = nm[1]\nhappy_ar = gets.chomp.split(' ').map{|i| i.to_i }\nhappy_amount = happy_ar[0]\nhappy_who_b = if happy_amount > 0 then happy_ar[1..-1] else [] end\nboys = create_list(n, happy_who_b)\n\nhappy_ar = gets.chomp.split(' ').map{|i| i.to_i }\nhappy_amount = happy_ar[0]\nhappy_who_g = if happy_amount > 0 then happy_ar[1..-1] else [] end\ngirls = create_list(m, happy_who_g)\n\n\ndef check(n, m, boys, girls)\n # gcd = n.gcd(m)\n (boys.length * girls.length).times do |i|\n if boys[i % boys.length] or girls[i % girls.length]\n boys[i % boys.length] = true\n girls[i % girls.length] = true\n end\n end\n return (boys.all?{|x| x} and girls.all?{|x| x})\nend\n\nputs check(n, m, boys, girls) ? 'Yes' : 'No'"}, {"source_code": "pa=Array.new(105,0)\npb=Array.new(105,0)\npc=Array.new\n\nstr=gets.to_s\npc=str.split\nn=pc[0].to_i\nm=pc[1].to_i\n\nk=n*m\nstr=gets.to_s\npc=str.split\nb=pc[0].to_i\nfor i in 1..b\n pa[pc[i].to_i]=1\nend\nstr=gets.to_s\npc=str.split\nb=pc[0].to_i\nfor i in 1..b\n pb[pc[i].to_i]=1\nend\n\nfor i in 0..k\n a=i%n\n b=i%m\n if pa[a]==1 || pb[b]==1\n pa[a]=pb[b]=1\n end\nend\n\nh=0\nfor i in 0..n-1\n if pa[i]==0\n h=1\n break\n end\nend\n\nfor i in 0..m-1\n if pb[i]==0 || h==1\n h=1\n break\n end\nend\n\nif h==1\n puts \"No\"\nelsif h==0\n puts \"Yes\"\nend"}, {"source_code": "pa=Array.new(105,0)\npb=Array.new(105,0)\npc=Array.new\n\nstr=gets.to_s\npc=str.split\nn=pc[0].to_i\nm=pc[1].to_i\n\nk=n*m\nstr=gets.to_s\npc=str.split\nb=pc[0].to_i\nfor i in 1..b\n pa[pc[i].to_i]=1\nend\nstr=gets.to_s\npc=str.split\nb=pc[0].to_i\nfor i in 1..b\n pb[pc[i].to_i]=1\nend\n\nfor i in 0..n-1\n puts pa[i]\nend\nfor i in 0..m-1\n puts pb[i]\nend\n\nfor i in 0..k\n a=i%n\n b=i%m\n if pa[a]==1 || pb[b]==1\n pa[a]=pb[b]=1\n end\nend\n\nh=0\nfor i in 0..n-1\n if pa[i]==0\n h=1\n break\n end\nend\n\nfor i in 0..m-1\n if pb[i]==0 || h==1\n h=1\n break\n end\nend\n\nif h==1\n puts \"No\"\nelsif h==0\n puts \"Yes\"\nend"}, {"source_code": "n, m = gets.strip.split.map(&:to_i)\na = gets.strip.split.map(&:to_i)\nb = gets.strip.split.map(&:to_i)\n\na.delete_at(0)\nb.delete_at(0)\n\nh1 = []\nh2 = []\n\n(0..(n+m)).each {|x|\n h1 << 0\n h2 << 0\n}\n\na.each { |x| h1[x] = 1 }\nb.each { |x| h2[x] = 1 }\n\n(0..(n*m)).each {|x|\n if h1[x % n] == 1 || h2[x % m] == 1\n h1[x % n] = 1\n h2[x % m] = 1\n end\n}\n\nres = 0\n(0..(n-1)).each {|x| res = 1 if h1[x] == 0}\n(0..(m-1)).each {|x| res = 1 if h2[x] == 0}\n\nif res == 1\n puts \"No\"\nelse\n puts \"Yes\"\nend"}], "src_uid": "65efbc0a1ad82436100eea7a2378d4c2"} {"nl": {"description": "There are $$$n$$$ dormitories in Berland State University, they are numbered with integers from $$$1$$$ to $$$n$$$. Each dormitory consists of rooms, there are $$$a_i$$$ rooms in $$$i$$$-th dormitory. The rooms in $$$i$$$-th dormitory are numbered from $$$1$$$ to $$$a_i$$$.A postman delivers letters. Sometimes there is no specific dormitory and room number in it on an envelope. Instead of it only a room number among all rooms of all $$$n$$$ dormitories is written on an envelope. In this case, assume that all the rooms are numbered from $$$1$$$ to $$$a_1 + a_2 + \\dots + a_n$$$ and the rooms of the first dormitory go first, the rooms of the second dormitory go after them and so on.For example, in case $$$n=2$$$, $$$a_1=3$$$ and $$$a_2=5$$$ an envelope can have any integer from $$$1$$$ to $$$8$$$ written on it. If the number $$$7$$$ is written on an envelope, it means that the letter should be delivered to the room number $$$4$$$ of the second dormitory.For each of $$$m$$$ letters by the room number among all $$$n$$$ dormitories, determine the particular dormitory and the room number in a dormitory where this letter should be delivered.", "input_spec": "The first line contains two integers $$$n$$$ and $$$m$$$ $$$(1 \\le n, m \\le 2 \\cdot 10^{5})$$$ — the number of dormitories and the number of letters. The second line contains a sequence $$$a_1, a_2, \\dots, a_n$$$ $$$(1 \\le a_i \\le 10^{10})$$$, where $$$a_i$$$ equals to the number of rooms in the $$$i$$$-th dormitory. The third line contains a sequence $$$b_1, b_2, \\dots, b_m$$$ $$$(1 \\le b_j \\le a_1 + a_2 + \\dots + a_n)$$$, where $$$b_j$$$ equals to the room number (among all rooms of all dormitories) for the $$$j$$$-th letter. All $$$b_j$$$ are given in increasing order.", "output_spec": "Print $$$m$$$ lines. For each letter print two integers $$$f$$$ and $$$k$$$ — the dormitory number $$$f$$$ $$$(1 \\le f \\le n)$$$ and the room number $$$k$$$ in this dormitory $$$(1 \\le k \\le a_f)$$$ to deliver the letter.", "sample_inputs": ["3 6\n10 15 12\n1 9 12 23 26 37", "2 3\n5 10000000000\n5 6 9999999999"], "sample_outputs": ["1 1\n1 9\n2 2\n2 13\n3 1\n3 12", "1 5\n2 1\n2 9999999994"], "notes": "NoteIn the first example letters should be delivered in the following order: the first letter in room $$$1$$$ of the first dormitory the second letter in room $$$9$$$ of the first dormitory the third letter in room $$$2$$$ of the second dormitory the fourth letter in room $$$13$$$ of the second dormitory the fifth letter in room $$$1$$$ of the third dormitory the sixth letter in room $$$12$$$ of the third dormitory "}, "positive_code": [{"source_code": "sum = 0\nca = {}\n\ndef search (n, b, ca)\n lo, hi, ans = 1, n\n while lo <= hi\n md = (lo + hi) / 2\n if ca[md] >= b\n ans = md\n hi = md - 1\n else\n lo = md + 1\n end\n end\n \"#{ans} #{(b - ca[ans - 1])}\"\nend\n\nn, m = gets.split(\" \")\na = [0] + gets.split(\" \")\nb = [0] + gets.split(\" \")\na.each_with_index {|cnt, index| ca[index] = (sum += cnt.to_i)}\n(1..(m.to_i)).each { |i| puts search(n.to_i, b[i].to_i, ca)}"}], "negative_code": [], "src_uid": "56bdab2019ee12e18d4f7e17ac414962"} {"nl": {"description": "Polycarpus likes studying at school a lot and he is always diligent about his homework. Polycarpus has never had any problems with natural sciences as his great-great-grandfather was the great physicist Seinstein. On the other hand though, Polycarpus has never had an easy time with history.Everybody knows that the World history encompasses exactly n events: the i-th event had continued from the year ai to the year bi inclusive (ai < bi). Polycarpus easily learned the dates when each of n events started and ended (Polycarpus inherited excellent memory from his great-great-granddad). But the teacher gave him a more complicated task: Polycaprus should know when all events began and ended and he should also find out for each event whether it includes another event. Polycarpus' teacher thinks that an event j includes an event i if aj < ai and bi < bj. Your task is simpler: find the number of events that are included in some other event.", "input_spec": "The first input line contains integer n (1 ≤ n ≤ 105) which represents the number of events. Next n lines contain descriptions of the historical events, one event per line. The i + 1 line contains two integers ai and bi (1 ≤ ai < bi ≤ 109) — the beginning and the end of the i-th event. No two events start or finish in the same year, that is, ai ≠ aj, ai ≠ bj, bi ≠ aj, bi ≠ bj for all i, j (where i ≠ j). Events are given in arbitrary order.", "output_spec": "Print the only integer — the answer to the problem.", "sample_inputs": ["5\n1 10\n2 9\n3 8\n4 7\n5 6", "5\n1 100\n2 50\n51 99\n52 98\n10 60", "1\n1 1000000000"], "sample_outputs": ["4", "4", "0"], "notes": "NoteIn the first example the fifth event is contained in the fourth. Similarly, the fourth event is contained in the third, the third — in the second and the second — in the first.In the second example all events except the first one are contained in the first.In the third example only one event, so the answer is 0."}, "positive_code": [{"source_code": "#f = File.open(\"c.in\", \"r\")\nclass C <\n\tStruct.new(:ai, :bi)\nend\n\nn = gets.to_i\na = Array.new\n\nfor i in 0...n\n\tl = gets.split\n\tc = C.new\n\tc.ai = l[0].to_i\n\tc.bi = l[1].to_i\n\ta.push(c)\nend\n\na = a.sort {|o, p| o[:ai] <=> p[:ai]}\nm = r = 0\n\nfor i in 0...n\n\tm = [m, a[i].bi].max\n\tif a[i].bi < m\n\t\tr += 1\n\tend\nend\n\nputs r\n"}, {"source_code": "def run(input = STDIN.read)\n periods = input.split(\"\\n\")[1..-1].map { |row| row.split.map(&:to_i) }\n\n periods.sort_by!(&:first)\n\n current_max = periods.first.last\n res = 0\n periods[1..-1].each do |period|\n res += 1 if period.last < current_max\n current_max = period.last if period.last > current_max\n end\n\n puts res\nend\n\nrun"}, {"source_code": "\ndef history\n coord = []\n STDIN.gets.to_i.times { coord << gets.split(' ').map{|i| i.to_i} }\n coord.sort!{|x, y| y[1] <=> x[1] }\n\n if coord.size < 2\n puts 0\n return\n end\n\n tot = 0\n min = coord[0][0]\n (1...coord.size).each do |ind|\n c = coord[ind]\n if c[0] > min\n tot += 1\n else\n min = c[0]\n end\n end\n p tot\nend\n\nhistory"}, {"source_code": "n = gets.to_i\narr = []\nn.times do\n\tarr << gets.split(\" \").map(&:to_i)\nend\n\narr.sort!#_by!{|x| [x[0],-x[1]]}\n\ncount = 0\nmax = -1\narr.map(&:last).each do |a|\n\t\n\tif a < max\n\t\tcount += 1\n\telse\n\t\tmax = a\n\tend\nend\nputs count"}, {"source_code": "require \"set\"\nn = gets.chomp.to_i\nevents = []\nevents2 = []\nn.times do\n e = gets.chomp.split(\" \").map {|e| e.to_i}\n events.push e\nend\n\ntotal = 0\nevents = events.sort_by {|e| e[0]}\nlargest = 0\n\nevents.each do |e|\n if largest > e[1]\n total += 1\n end\n if e[1] > largest\n largest = e[1]\n end\nend\n\nputs total\n"}], "negative_code": [{"source_code": "\ndef history\n coord = []\n STDIN.gets.to_i.times { coord << gets.split(' ').map{|i| i.to_i} }\n coord.sort!{|x, y| y[1] <=> x[1] }\n\n if coord.size < 2\n puts 0\n return\n end\n\n ind = 1\n max = coord[0][1]\n ind += 1 while coord[ind][1] >= max\n puts coord.size-ind\nend\n\n\nhistory"}, {"source_code": "n = gets.to_i\narr = []\nn.times do\n\tarr << gets.split(\" \").map(&:to_i)\nend\n\narr.sort_by!{|x| [x[0],-x[1]]}\n\nrange_arr = []\ncount = 0\narr.each do |a|\n\tfound = false\n\tif range_arr.any?{ |b| a[0] > b[0] && a[1] < b[1] } \t\t\n\t\tfound = true\n\t\tcount += 1\n\t\tbreak\n\tend\n\trange_arr << a unless found\nend\nputs count"}], "src_uid": "dfb1479ffa17489095b6bf1921758f7e"} {"nl": {"description": "Let's denote the Manhattan distance between two points $$$p_1$$$ (with coordinates $$$(x_1, y_1)$$$) and $$$p_2$$$ (with coordinates $$$(x_2, y_2)$$$) as $$$d(p_1, p_2) = |x_1 - x_2| + |y_1 - y_2|$$$. For example, the distance between two points with coordinates $$$(1, 3)$$$ and $$$(4, 2)$$$ is $$$|1 - 4| + |3 - 2| = 4$$$.You are given two points, $$$A$$$ and $$$B$$$. The point $$$A$$$ has coordinates $$$(0, 0)$$$, the point $$$B$$$ has coordinates $$$(x, y)$$$.Your goal is to find a point $$$C$$$ such that: both coordinates of $$$C$$$ are non-negative integers; $$$d(A, C) = \\dfrac{d(A, B)}{2}$$$ (without any rounding); $$$d(B, C) = \\dfrac{d(A, B)}{2}$$$ (without any rounding). Find any point $$$C$$$ that meets these constraints, or report that no such point exists.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 3000$$$) — the number of test cases. Each test case consists of one line containing two integers $$$x$$$ and $$$y$$$ ($$$0 \\le x, y \\le 50$$$) — the coordinates of the point $$$B$$$.", "output_spec": "For each test case, print the answer on a separate line as follows: if it is impossible to find a point $$$C$$$ meeting the constraints, print \"-1 -1\" (without quotes); otherwise, print two non-negative integers not exceeding $$$10^6$$$ — the coordinates of point $$$C$$$ meeting the constraints. If there are multiple answers, print any of them. It can be shown that if any such point exists, it's possible to find a point with coordinates not exceeding $$$10^6$$$ that meets the constraints. ", "sample_inputs": ["10\n49 3\n2 50\n13 0\n0 41\n42 0\n0 36\n13 37\n42 16\n42 13\n0 0"], "sample_outputs": ["23 3\n1 25\n-1 -1\n-1 -1\n21 0\n0 18\n13 12\n25 4\n-1 -1\n0 0"], "notes": "NoteExplanations for some of the test cases from the example: In the first test case, the point $$$B$$$ has coordinates $$$(49, 3)$$$. If the point $$$C$$$ has coordinates $$$(23, 3)$$$, then the distance from $$$A$$$ to $$$B$$$ is $$$|49 - 0| + |3 - 0| = 52$$$, the distance from $$$A$$$ to $$$C$$$ is $$$|23 - 0| + |3 - 0| = 26$$$, and the distance from $$$B$$$ to $$$C$$$ is $$$|23 - 49| + |3 - 3| = 26$$$. In the second test case, the point $$$B$$$ has coordinates $$$(2, 50)$$$. If the point $$$C$$$ has coordinates $$$(1, 25)$$$, then the distance from $$$A$$$ to $$$B$$$ is $$$|2 - 0| + |50 - 0| = 52$$$, the distance from $$$A$$$ to $$$C$$$ is $$$|1 - 0| + |25 - 0| = 26$$$, and the distance from $$$B$$$ to $$$C$$$ is $$$|1 - 2| + |25 - 50| = 26$$$. In the third and the fourth test cases, it can be shown that no point with integer coordinates meets the constraints. In the fifth test case, the point $$$B$$$ has coordinates $$$(42, 0)$$$. If the point $$$C$$$ has coordinates $$$(21, 0)$$$, then the distance from $$$A$$$ to $$$B$$$ is $$$|42 - 0| + |0 - 0| = 42$$$, the distance from $$$A$$$ to $$$C$$$ is $$$|21 - 0| + |0 - 0| = 21$$$, and the distance from $$$B$$$ to $$$C$$$ is $$$|21 - 42| + |0 - 0| = 21$$$. "}, "positive_code": [{"source_code": "gets\n$<.map do\n x,y=_1.split.map &:to_i\n if (x+y).odd?\n puts \"-1 -1\"\n elsif x.even?\n puts [x/2,y/2]*\" \"\n else\n puts (x>y ? [x/2+1,y/2]*\" \" : [x/2,y/2+1]*\" \")\n end\nend\n \n "}, {"source_code": "gets.to_i.times do\r\n x, y = gets.split.map(&:to_i)\r\n diff = (x - y).abs\r\n if diff.even?\r\n if x > y\r\n puts \"#{diff / 2} #{y}\"\r\n else\r\n puts \"#{x} #{diff / 2}\"\r\n end\r\n else\r\n puts \"-1 -1\"\r\n end\r\nend"}, {"source_code": "main = -> {\r\n t = int\r\n t.times do\r\n x, y = ints\r\n if (x + y).odd?\r\n puts \"-1 -1\"\r\n else\r\n a, b = x.div_ceil(2), y / 2\r\n puts \"#{a} #{b}\"\r\n # STDERR.puts \"#{a+b} == #{(x - a).abs + (y - b).abs}\"\r\n end\r\n end\r\n}\r\n\r\nDEBUG = true\r\nMOD = 10**9+7\r\nYESNO = %w(No Yes)\r\nINF = 10**9\r\n\r\nrequire \"set\"\r\nrequire \"prime\"\r\n\r\ndef int; gets.to_s.to_i end\r\ndef ints; gets.to_s.split.map { |s| s.to_i } end\r\ndef int1s; gets.to_s.split.map { |s| s.to_i - 1 } end\r\ndef float; gets.to_s.to_f end\r\ndef floats; gets.to_s.split.map { |s| s.to_f } end\r\ndef array_of(&convert); gets.to_s.split.map(&convert) end\r\ndef string; gets.to_s.chomp end\r\ndef rep(n, &b); Array.new(n, &b) end\r\ndef yes; puts YESNO[1] end\r\ndef no; puts YESNO[0] end\r\ndef yesno t; puts YESNO[t] end\r\ndef zip(xs, *yss); Enumerator.new { |y| xs.zip(*yss) { |a| y.yield(*a) } } end\r\ndef max(*xs, &block); block_given? ? xs.max_by(&block) : xs.max end\r\ndef min(*xs, &block); block_given? ? xs.min_by(&block) : xs.min end\r\ndef minmax(*xs, &block); block_given? ? xs.minmax_by(&block) : xs.minmax end\r\ndef gcd(*xs); xs.inject(0, :gcd) end\r\ndef matrix(h, w, fill=nil, &block); return Array.new(h) { Array.new(w, &block) } if block_given?; Array.new(h) { [fill] * w } end\r\ndef debug(x = nil); STDERR.puts (block_given? ? yield(x) : x).inspect if DEBUG; x end\r\ndef debug_grid(grid, width = 1); grid.each { |row| STDERR.puts row.map { |x| x.inspect.ljust(width) }.join(\"\") } if DEBUG; grid end\r\ndef if_debug; yield if DEBUG end\r\n\r\nmodule Boolean\r\n def coerce(other); [other, to_i] end\r\n def +@; to_i end\r\n def to_int; to_i end\r\n def *(other); to_i * other end\r\nend\r\n\r\nclass TrueClass\r\n include Boolean\r\n def to_i; 1 end\r\nend\r\n\r\nclass FalseClass\r\n include Boolean\r\n def to_i; 0 end\r\nend\r\n\r\nclass Integer\r\n def div_ceil(y); (self + y - 1) / y end\r\n def mod_inv(mod = MOD); pow(mod-2, mod) end\r\n def mod_div(y, mod = MOD); self * mod_inv(y, mod) % mod end\r\n def factorial(mod = MOD); (2..self).inject(1) { |f, x| f * x % mod } end\r\n def popcount; x = self; c = 0; while x > 0; c += 1 if x & 1 == 1; x >>= 1 end; c end #TODO: faster\r\n def bitbrute(&block); (1< 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\r\n def lower_bound; ac, wa = self.end_open, self.begin; while ac - wa > 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\r\n def shakutori(r2, &pred); Enumerator.new { |y| j, r = r2.begin, r2.end_open; each { |i| j += 1 while j + 1 < r and pred[i, j+1]; y.yield(i, j) } }; end\r\n def widest(&block); Enumerator.new { |y| j, n = self.begin, self.end_open; each { |i| j += 1 while j < n and block[i, j]; y.yield(i, j) if block[i, j] } } end\r\nend\r\n\r\nclass Array\r\n def power(&block); (0 ... 1 << size).each(&block) end\r\n def sorted_merge(other); a = []; i = j = 0; n, m = size, other.size; if j < m and other[j] < self[i]; a << other[j]; j += 1 else; a << self[i]; i += 1 end while i < n; a.push(*other[j..-1]) if j < m; a end\r\n def upper_bound; ac, wa = 0, size; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\r\n def lower_bound; ac, wa = size, 0; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\r\n def cum(*xs, &op); a = []; a << xs[0] if xs.size > 0; a << x = self[0]; (1...size).each { |i| a << x = op[x, self[i]] }; a end\r\n def cumdiff(range); self[range.end_open] - self[range.begin]; end\r\nend\r\n\r\nmodule Enumerable\r\n def sorted_uniq; x = nil; filter { |y| c = x === y; x = y; !c } end\r\n def cumsum; ys = [0]; each { |x| ys << x + ys[-1] }; ys end\r\nend\r\n\r\nmain[]"}], "negative_code": [{"source_code": "gets\n$<.map do\n x,y=_1.split.map &:to_i\n if (x+y).odd?\n puts \"-1 -1\"\n elsif x.even?\n puts [x/2,y/2]*\" \"\n else\n x,y=y,x if x 0\r\n as[a] -= 1\r\n if as[ax] && as[ax] > 0\r\n as[ax] -= 1\r\n else\r\n answer += 1\r\n end\r\n end\r\n end\r\n p answer\r\nend\r\n"}], "negative_code": [], "src_uid": "c19500d867fd0108fdeed2cbd00bc970"} {"nl": {"description": "Vasya trains to compose crossword puzzles. He can only compose crosswords of a very simplе type so far. All of them consist of exactly six words; the words can be read only from top to bottom vertically and from the left to the right horizontally. The words are arranged in the form of a rectangular \"eight\" or infinity sign, not necessarily symmetrical.The top-left corner of the crossword coincides with the top-left corner of the rectangle. The same thing is correct for the right-bottom corners. The crossword can't degrade, i.e. it always has exactly four blank areas, two of which are surrounded by letters. Look into the output for the samples for clarification.Help Vasya — compose a crossword of the described type using the given six words. It is allowed to use the words in any order.", "input_spec": "Six lines contain the given words. Every word consists of no more than 30 and no less than 3 uppercase Latin letters. ", "output_spec": "If it is impossible to solve the problem, print Impossible. Otherwise, print the sought crossword. All the empty squares should be marked as dots. If there can be several solutions to that problem, print the lexicographically minimum one. I.e. the solution where the first line is less than the first line of other solutions should be printed. If the two lines are equal, compare the second lines and so on. The lexicographical comparison of lines is realized by the < operator in the modern programming languages.", "sample_inputs": ["NOD\nBAA\nYARD\nAIRWAY\nNEWTON\nBURN", "AAA\nAAA\nAAAAA\nAAA\nAAA\nAAAAA", "PTC\nJYNYFDSGI\nZGPPC\nIXEJNDOP\nJJFS\nSSXXQOFGJUZ"], "sample_outputs": ["BAA...\nU.I...\nR.R...\nNEWTON\n..A..O\n..YARD", "AAA..\nA.A..\nAAAAA\n..A.A\n..AAA", "JJFS....\nY..S....\nN..X....\nY..X....\nF..Q....\nD..O....\nS..F....\nG..G....\nIXEJNDOP\n...U...T\n...ZGPPC"], "notes": null}, "positive_code": [{"source_code": "readlines.map{|i| i.chomp}.permutation do |a,b,c,d,e,f|\n\tx = a.size - 1\n\ty = b.size - 1\n\tm = c.size\n\tn = d.size\n\tnext if x + e.size != m ||\n\t\t\ty + f.size != n ||\n\t\t\ta[0] != b[0] ||\n\t\t\ta[-1] != d[0] ||\n\t\t\tb[-1] != c[0] ||\n\t\t\tc[x] != d[y] ||\n\t\t\tc[-1] != f[0] ||\n\t\t\td[-1] != e[0] ||\n\t\t\te[-1] != f[-1]\n\to = [*1..m].map{'.' * n}\n\t(0 .. x).each{|i| o[i][0] = a[i]}\n\to[0][0 .. y] = b\n\t(0 .. m - 1).each{|i| o[i][y] = c[i]}\n\to[x] = d\n\t(x .. m - 1).each{|i| o[i][-1] = e[i - x]}\n\to[-1][y .. -1] = f\n\t$* << o * \"\\n\"\nend\nif $*.empty? then\n\tputs :Impossible\nelse\n\tputs $*.min\nend\n"}, {"source_code": "results = []\n[*$<].map(&:chomp).permutation(6) do |a,b,c,d,e,f|\n if a[0]==d[0] && a[-1]==e[0] &&\n c[0]==e[-1] && c[-1]==f[-1] &&\n b[0]==d[-1] && b[-1]==f[0] &&\n b[a.size-1]==e[d.size-1] &&\n a.size+c.size-1==b.size &&\n d.size+f.size-1==e.size\n results << [a.ljust(b.size,'.')] +\n d[1...-1].chars.zip(e[1...-1].chars).map { |i,j|\n s = '.'*b.size\n s[0] = i\n s[a.size-1] = j\n s\n } +\n [b] +\n f[1...-1].chars.zip(e[d.size...-1].chars).map { |i,j|\n s = '.'*b.size\n s[a.size-1] = j\n s[-1] = i\n s\n } +\n [c.rjust(b.size,'.')]\n end\nend\nputs (results.empty? ? 'Impossible' : results.sort.first)"}, {"source_code": "w,m=[],-1\n$<.map(&:chop).permutation(6){|_|j,b,c,d,g,f=_;s=_.map &:size\n(r=(1..s[4]).map{[?.]*s[1]}\n_='r[z][z,s[0]]=jw[s[3]-1]=bw[m][-s[2]..m]=cw=r.transpose\n'.gsub ?w,'.split\"\";r'\neval _+_.tr(\"03jbc2\",\"30dgf5\")\nw<= 2\r\n puts -1\r\n next\r\nend\r\n\r\ns = a.join\r\n\r\ni = -1\r\nj1 = 0\r\nj2 = 1\r\ncnt1 = 0\r\ncnt2 = 0\r\nwhile true\r\n i2 = s.index('0', i + 1)\r\n break unless i2\r\n\r\n cnt1 += (j1 - i2).abs\r\n cnt2 += (j2 - i2).abs\r\n j1 += 2\r\n j2 += 2\r\n\r\n i = i2\r\nend\r\n\r\nif z < n - z\r\n puts cnt2\r\nelsif z > n - z\r\n puts cnt1\r\nelse\r\n puts [cnt1, cnt2].min\r\nend\r\n\r\nend\r\n"}], "negative_code": [{"source_code": "t = gets.to_i\r\nt.times do\r\n\r\nn = gets.to_i\r\na = gets.split.map!(&:to_i)\r\n\r\na.map! { |ai| ai & 1 }\r\na.map! { |ai| ai ^ 1 } if a[0] == 1\r\n\r\nz = a.count(&:zero?)\r\nif z < n / 2 || n - z < n / 2\r\n puts -1\r\n next\r\nend\r\n\r\ns = a.join\r\n\r\ni = -1\r\nj = 0\r\nj += 1 if z < n - z\r\ncnt = 0\r\nwhile true\r\n i2 = s.index('0', i + 1)\r\n break unless i2\r\n\r\n cnt += (j - i2).abs\r\n j += 2\r\n\r\n i = i2\r\nend\r\n\r\nputs cnt\r\n\r\nend\r\n"}], "src_uid": "be51a3e4038bd9a3f6d4993b75a20d1c"} {"nl": {"description": "Pak Chanek has $$$n$$$ two-dimensional slices of cheese. The $$$i$$$-th slice of cheese can be represented as a rectangle of dimensions $$$a_i \\times b_i$$$. We want to arrange them on the two-dimensional plane such that: Each edge of each cheese is parallel to either the x-axis or the y-axis. The bottom edge of each cheese is a segment of the x-axis. No two slices of cheese overlap, but their sides can touch. They form one connected shape. Note that we can arrange them in any order (the leftmost slice of cheese is not necessarily the first slice of cheese). Also note that we can rotate each slice of cheese in any way as long as all conditions still hold.Find the minimum possible perimeter of the constructed shape.", "input_spec": "Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \\leq t \\leq 2 \\cdot 10^4$$$) — the number of test cases. The following lines contain the description of each test case. The first line of each test case contains an integer $$$n$$$ ($$$1 \\leq n \\leq 2 \\cdot 10^5$$$) — the number of slices of cheese Pak Chanek has. The $$$i$$$-th of the next $$$n$$$ lines of each test case contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \\leq a_i,b_i \\leq 10^9$$$) — the dimensions of the $$$i$$$-th slice of cheese. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, output a line containing an integer representing the minimum possible perimeter of the constructed shape.", "sample_inputs": ["3\n\n4\n\n4 1\n\n4 5\n\n1 1\n\n2 3\n\n3\n\n2 4\n\n2 6\n\n2 3\n\n1\n\n2 65"], "sample_outputs": ["26\n24\n134"], "notes": "NoteIn the first test case, a way of getting the minimum possible perimeter is to arrange the slices of cheese as follows.We can calculate that the perimeter of the constructed shape is $$$2+5+1+1+1+1+3+1+5+1+2+3=26$$$. It can be shown that we cannot get a smaller perimeter.Consider the following invalid arrangement.Even though the perimeter of the shape above is $$$24$$$, it does not satisfy all conditions of the problem. The bottom edge of the $$$1 \\times 1$$$ slice of cheese is not a segment of the x-axis.In the second test case, a way of getting the minimum possible perimeter is to arrange the slices of cheese as follows.We can calculate that the perimeter of the constructed shape is $$$2+2+2+3+2+3+2+2+2+4=24$$$. It can be shown that we cannot get a smaller perimeter."}, "positive_code": [{"source_code": "t=gets.to_i\nt.times do\n n=gets.to_i\n l,m=0,0\n n.times do\n x,y=gets.chomp.split(' ').map(&:to_i)\n m=[m,x,y].max\n l+=[x,y].min\n end\n puts l*2+m*2\nend"}], "negative_code": [], "src_uid": "7100fa11adfa0c1f5d33f9e3a1c3f352"} {"nl": {"description": "A binary string is a string that consists of characters $$$0$$$ and $$$1$$$.Let $$$\\operatorname{MEX}$$$ of a binary string be the smallest digit among $$$0$$$, $$$1$$$, or $$$2$$$ that does not occur in the string. For example, $$$\\operatorname{MEX}$$$ of $$$001011$$$ is $$$2$$$, because $$$0$$$ and $$$1$$$ occur in the string at least once, $$$\\operatorname{MEX}$$$ of $$$1111$$$ is $$$0$$$, because $$$0$$$ and $$$2$$$ do not occur in the string and $$$0 < 2$$$.A binary string $$$s$$$ is given. You should cut it into any number of substrings such that each character is in exactly one substring. It is possible to cut the string into a single substring — the whole string.A string $$$a$$$ is a substring of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.What is the minimal sum of $$$\\operatorname{MEX}$$$ of all substrings pieces can be?", "input_spec": "The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. Description of the test cases follows. Each test case contains a single binary string $$$s$$$ ($$$1 \\le |s| \\le 10^5$$$). It's guaranteed that the sum of lengths of $$$s$$$ over all test cases does not exceed $$$10^5$$$.", "output_spec": "For each test case print a single integer — the minimal sum of $$$\\operatorname{MEX}$$$ of all substrings that it is possible to get by cutting $$$s$$$ optimally.", "sample_inputs": ["6\n01\n1111\n01100\n101\n0000\n01010"], "sample_outputs": ["1\n0\n2\n1\n1\n2"], "notes": "NoteIn the first test case the minimal sum is $$$\\operatorname{MEX}(0) + \\operatorname{MEX}(1) = 1 + 0 = 1$$$.In the second test case the minimal sum is $$$\\operatorname{MEX}(1111) = 0$$$.In the third test case the minimal sum is $$$\\operatorname{MEX}(01100) = 2$$$."}, "positive_code": [{"source_code": "t = gets.to_i\r\nt.times do\r\n\r\ns = gets.chomp\r\n\r\na = s.scan(/0++|1++/).map { |ss| ss[0].to_i }\r\n\r\nif a.size == 1\r\n puts (a[0] == 1) ? 0 : 1\r\n next\r\nend\r\n\r\nif a.size == 2\r\n puts 1\r\n next\r\nend\r\n\r\nif a.size == 3\r\n puts (a[0] == 1) ? 1 : 2\r\n next\r\nend\r\n\r\nputs 2\r\n\r\nend\r\n"}], "negative_code": [], "src_uid": "2e6ddb2b11f8ac857e81d4b9b0c7d783"} {"nl": {"description": "Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not.A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if . The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal.Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him?", "input_spec": "The first line contains a single integer n (1 ≤ n ≤ 105).", "output_spec": "If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line — permutation b, the third — permutation c. If there are multiple solutions, print any of them.", "sample_inputs": ["5", "2"], "sample_outputs": ["1 4 3 2 0\n1 0 2 4 3\n2 4 0 1 3", "-1"], "notes": "NoteIn Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: ; ; ; ; . In Sample 2, you can easily notice that no lucky permutation triple exists."}, "positive_code": [{"source_code": "n=gets.to_i\nif n.odd?\n a=[*0..n-1]\n puts a*' '\n puts (b=a.rotate)*' '\n c=[]\n a.each_with_index {|v, i| c<<(v+b[i])%n }\n puts c*' '\nelse\n puts -1\nend"}, {"source_code": "n = gets.to_i\n\na = (0...n).to_a\n\n\nif n%2==0\n puts -1\nelsif n%3!=0\n b = a.map {|x| (2*x)%n}\n c = a.map {|x| (x+(2*x)%n)%n}\n puts a.join(\" \")\n puts b.join(\" \")\n puts c.join(\" \")\nelse\n b = a.map {|x| x%n}\n c = a.map {|x| (x*2)%n}\n puts a.join(\" \")\n puts b.join(\" \")\n puts c.join(\" \")\nend"}, {"source_code": "n = gets.to_i\nif n % 2 == 0\n puts -1\nelse\n a = []\n b = []\n c = []\n for i in 0...n\n a.push i\n b.push i\n c.push (i + i) % n\n end\n [a, b, c].each do |arr| puts arr.join(' ') end\nend"}], "negative_code": [{"source_code": "n = gets.to_i\n\na = (0...n).to_a\nb = a.map {|x| (2*x)%n}\nc = a.map {|x| (x+(2*x)%n)%n}\n\nif n%2==0\n puts -1\nelse\n puts a.join(\" \")\n puts b.join(\" \")\n puts c.join(\" \")\nend"}], "src_uid": "2f0942c531fd5758b220104c3338b702"} {"nl": {"description": "You are given an array $$$a$$$ of length $$$n$$$. The array is called 3SUM-closed if for all distinct indices $$$i$$$, $$$j$$$, $$$k$$$, the sum $$$a_i + a_j + a_k$$$ is an element of the array. More formally, $$$a$$$ is 3SUM-closed if for all integers $$$1 \\leq i < j < k \\leq n$$$, there exists some integer $$$1 \\leq l \\leq n$$$ such that $$$a_i + a_j + a_k = a_l$$$.Determine if $$$a$$$ is 3SUM-closed.", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\leq t \\leq 1000$$$) — the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$3 \\leq n \\leq 2 \\cdot 10^5$$$) — the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$-10^9 \\leq a_i \\leq 10^9$$$) — the elements of the array. It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, output \"YES\" (without quotes) if $$$a$$$ is 3SUM-closed and \"NO\" (without quotes) otherwise. You can output \"YES\" and \"NO\" in any case (for example, strings \"yEs\", \"yes\" and \"Yes\" will be recognized as a positive response).", "sample_inputs": ["4\n\n3\n\n-1 0 1\n\n5\n\n1 -2 -2 1 -3\n\n6\n\n0 0 0 0 0 0\n\n4\n\n-1 2 -3 4"], "sample_outputs": ["YES\nNO\nYES\nNO"], "notes": "NoteIn the first test case, there is only one triple where $$$i=1$$$, $$$j=2$$$, $$$k=3$$$. In this case, $$$a_1 + a_2 + a_3 = 0$$$, which is an element of the array ($$$a_2 = 0$$$), so the array is 3SUM-closed.In the second test case, $$$a_1 + a_4 + a_5 = -1$$$, which is not an element of the array. Therefore, the array is not 3SUM-closed.In the third test case, $$$a_i + a_j + a_k = 0$$$ for all distinct $$$i$$$, $$$j$$$, $$$k$$$, and $$$0$$$ is an element of the array, so the array is 3SUM-closed."}, "positive_code": [{"source_code": "require 'set'\r\n# gets.to_i\r\n# gets.split.map(&:to_i)\r\n\r\ngets.to_i.times do\r\n n = gets.to_i\r\n a = gets.split.map(&:to_i)\r\n\r\n great = a.count{|e| e > 0}\r\n less = a.count{|e| e < 0}\r\n\r\n if great > 2 || less > 2\r\n puts \"NO\"\r\n else\r\n zero = a.count(0)\r\n a = (a.select{|e| e != 0}) || []\r\n a += ([0] * [3, zero].min)\r\n s = Set.new\r\n ok = true\r\n n = a.size\r\n n.times do |i|\r\n i.times do |j|\r\n j.times do |k|\r\n s << a[i] + a[j] + a[k]\r\n end\r\n end\r\n end\r\n\r\n puts s.all?{|e| a.include?(e) } ? \"YES\" : \"NO\"\r\n end\r\n\r\nend\r\n\r\n"}], "negative_code": [{"source_code": "require 'set'\r\n# gets.to_i\r\n# gets.split.map(&:to_i)\r\n\r\ngets.to_i.times do\r\n n = gets.to_i\r\n a = gets.split.map(&:to_i)\r\n s = Set.new\r\n at = a.tally\r\n if n <= 5\r\n ok = true\r\n n.times do |i|\r\n i.times do |j|\r\n j.times do |k|\r\n s << a[i] + a[j] + a[k]\r\n end\r\n end\r\n end\r\n puts s.all?{|e| a.include?(e)} ? \"YES\" : \"NO\"\r\n elsif at[0]\r\n if at.size == 1 || at.size == 2\r\n puts \"YES\"\r\n elsif at.size == 3\r\n ok = true\r\n at.each_key do |k|\r\n next if k == 0\r\n ok = false if at[k] > 1\r\n end\r\n puts ok ? \"YES\" : \"NO\"\r\n end\r\n else\r\n puts \"NO\"\r\n end\r\nend\r\n\r\n"}, {"source_code": "require 'set'\r\n# gets.to_i\r\n# gets.split.map(&:to_i)\r\n\r\ngets.to_i.times do\r\n n = gets.to_i\r\n a = gets.split.map(&:to_i)\r\n # ans = Set.new\r\n # n.times do |i|\r\n # n.times do |j|\r\n # n.times do |k|\r\n # next if i == j || j == k || k == i\r\n # ans << a[i]+a[j]+a[k]\r\n # end\r\n # end\r\n # end\r\n # p ans\r\n at = a.tally\r\n if at[0]\r\n if at.size == 1 || at.size == 2\r\n puts \"YES\"\r\n elsif at.size == 3\r\n ok = true\r\n at.each_key do |k|\r\n next if k == 0\r\n ok = false if at[k] > 1\r\n end\r\n puts ok ? \"YES\" : \"NO\"\r\n end\r\n elsif at.size <= 4\r\n ok = true\r\n at.each_key do |k|\r\n if at[-k].nil? || at[k] != at[-k]\r\n break ok = false\r\n end\r\n end\r\n puts ok ? \"YES\" : \"NO\"\r\n else\r\n puts \"NO\"\r\n end\r\nend\r\n"}, {"source_code": "# gets.to_i\r\n# gets.split.map(&:to_i)\r\n\r\ngets.to_i.times do\r\n n = gets.to_i\r\n a = gets.split.map(&:to_i)\r\n at = a.tally\r\n if at[0]\r\n if at.size == 1 || at.size == 2\r\n puts \"YES\"\r\n elsif at.size == 3\r\n ok = true\r\n at.each_key do |k|\r\n next if k == 0\r\n ok = false if at[k] > 1\r\n end\r\n puts ok ? \"YES\" : \"NO\"\r\n end\r\n else\r\n ok = true\r\n at.each_key do |k|\r\n if at[-k].nil? || at[k] != at[-k]\r\n break ok = false\r\n end\r\n end\r\n puts ok ? \"YES\" : \"NO\"\r\n end\r\nend\r\n\r\n"}, {"source_code": "# gets.to_i\r\n# gets.split.map(&:to_i)\r\n\r\ngets.to_i.times do\r\n n = gets.to_i\r\n a = gets.split.map(&:to_i)\r\n if n == 3\r\n if a.include?(a.sum)\r\n next puts \"YES\"\r\n else\r\n next puts \"NO\"\r\n end\r\n end\r\n\r\n at = a.tally\r\n if !at[0].nil?\r\n if at.size == 1 || at.size == 2\r\n next puts \"YES\"\r\n elsif at.size == 3\r\n ok = true\r\n at.each_key do |k|\r\n next if k == 0\r\n ok = false if at[k] > 1\r\n end\r\n next puts ok ? \"YES\" : \"NO\"\r\n end\r\n else\r\n at.each_key do |k|\r\n if at[-k].nil? || at[k] != at[-k]\r\n break ok = false\r\n end\r\n end\r\n puts ok ? \"YES\" : \"NO\"\r\n end\r\nend\r\n\r\n"}], "src_uid": "70028bbb9b7bce1f0d2753275b3e752f"} {"nl": {"description": "Let's define a forest as a non-directed acyclic graph (also without loops and parallel edges). One day Misha played with the forest consisting of n vertices. For each vertex v from 0 to n - 1 he wrote down two integers, degreev and sv, were the first integer is the number of vertices adjacent to vertex v, and the second integer is the XOR sum of the numbers of vertices adjacent to v (if there were no adjacent vertices, he wrote down 0). Next day Misha couldn't remember what graph he initially had. Misha has values degreev and sv left, though. Help him find the number of edges and the edges of the initial graph. It is guaranteed that there exists a forest that corresponds to the numbers written by Misha.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 216), the number of vertices in the graph. The i-th of the next lines contains numbers degreei and si (0 ≤ degreei ≤ n - 1, 0 ≤ si < 216), separated by a space.", "output_spec": "In the first line print number m, the number of edges of the graph. Next print m lines, each containing two distinct numbers, a and b (0 ≤ a ≤ n - 1, 0 ≤ b ≤ n - 1), corresponding to edge (a, b). Edges can be printed in any order; vertices of the edge can also be printed in any order.", "sample_inputs": ["3\n2 3\n1 0\n1 0", "2\n1 1\n1 0"], "sample_outputs": ["2\n1 0\n2 0", "1\n0 1"], "notes": "NoteThe XOR sum of numbers is the result of bitwise adding numbers modulo 2. This operation exists in many modern programming languages. For example, in languages C++, Java and Python it is represented as \"^\", and in Pascal — as \"xor\"."}, "positive_code": [{"source_code": "n,deg,s,que,sum=gets.to_i,[],[],[],0\n(0...n).each do |i|\n deg[i],s[i]=gets.split.map &:to_i\n sum+=deg[i]\n que.push i if deg[i]==1\nend\np sum/2\nwhile que[0]\n f=que[-1]\n que.pop\n next if deg[f]==0\n puts [f,s[f]].join' '\n deg[s[f]]-=1\n s[s[f]]^=f\n que.push s[f] if deg[s[f]]==1\nend"}, {"source_code": "n=gets.to_i\ndeg,s,que,sum=[],[],[],0\n(0..n-1).each do |i|\n deg[i],s[i]=gets.split.map(&:to_i)\n sum+=deg[i]\n if deg[i]==1\n que.push i\n end\nend\nputs sum/2\nwhile que[-1]\n f=que[-1]\n que.pop\n if deg[f]==0\n next\n end\n print f,' ',s[f],\"\\n\"\n deg[s[f]]-=1\n deg[f]-=1\n s[s[f]]^=f\n if deg[s[f]]==1\n que.push s[f]\n end\nend"}, {"source_code": "n=gets.to_i\ndeg,s,que,sum=[],[],[],0\n(0..n-1).each do |i|\n deg[i],s[i]=gets.split.map(&:to_i)\n sum+=deg[i]\n if deg[i]==1\n que.push i\n end\nend\np sum/2\nwhile que[-1]\n f=que[-1]\n que.pop\n if deg[f]==0\n next\n end\n print f,' ',s[f],' '\n deg[s[f]]-=1\n deg[f]-=1\n s[s[f]]^=f\n if deg[s[f]]==1\n que.push s[f]\n end\nend"}, {"source_code": "n,deg,s,que,sum=gets.to_i,[],[],[],0\n(0...n).each do |i|\n deg[i],s[i]=gets.split.map &:to_i\n sum+=deg[i]\n que.push i if deg[i]==1\nend\np sum/2\nwhile que[0]\n f=que[-1]\n que.pop\n next if deg[f]==0\n puts [f,s[f]].join' '\n deg[s[f]]-=1\n s[s[f]]^=f\n que.push s[f] if deg[s[f]]==1\nend"}, {"source_code": "n=gets.to_i\ndeg,s,que,sum=[],[],[],0\n(0..n-1).each{|i| deg[i],s[i]=gets.split.map(&:to_i)}\ndeg.each_with_index do |i,id|\n if(i==1)\n que.push id\n end\n sum+=i\nend\nputs sum/2\nwhile que[-1]\n f=que[-1]\n que.pop\n if deg[f]==0\n next\n end\n print f,' ',s[f]\n puts\n deg[s[f]]-=1\n deg[f]-=1\n s[s[f]]^=f\n if deg[s[f]]==1\n que.push s[f]\n end\nend"}, {"source_code": "n,deg,s,que,sum=gets.to_i,[],[],[],0\n(0...n).each do |i|\n deg[i],s[i]=gets.split.map &:to_i\n sum+=deg[i]\n que.push i if deg[i]==1\nend\np sum/2\nwhile que[0]\n f=que[-1]\n que.pop\n next if deg[f]==0\n puts [f,s[f]].join' '\n deg[s[f]]-=1\n s[s[f]]^=f\n que.push s[f] if deg[s[f]]==1\nend\n"}, {"source_code": "n,deg,s,que,sum=gets.to_i,[],[],[],0\n(0...n).each do |i|\n deg[i],s[i]=gets.split.map &:to_i\n sum+=deg[i]\n que.push i if deg[i]==1\nend\np sum/2\nwhile que[0]\n f=que[-1]\n que.pop\n next if deg[f]==0\n puts [f,s[f]].join' '\n deg[s[f]]-=1\n s[s[f]]^=f\n que.push s[f] if deg[s[f]]==1\nend\n"}, {"source_code": "n,deg,s,que,sum=gets.to_i,[],[],[],0\n(0...n).each do |i|\n deg[i],s[i]=gets.split.map &:to_i\n sum+=deg[i]\n que.push i if deg[i]==1\nend\np sum/2\nwhile que[0]\n f=que[-1]\n que.pop\n next if deg[f]==0\n puts [f,s[f]].join' '\n deg[s[f]]-=1\n s[s[f]]^=f\n que.push s[f] if deg[s[f]]==1\nend\n"}, {"source_code": "n,deg,s,que,sum=gets.to_i,[],[],[],0\n(0...n).each do |i|\n deg[i],s[i]=gets.split.map &:to_i\n sum+=deg[i]\n que.push i if deg[i]==1\nend\np sum/2\nwhile que[0]\n f=que[-1]\n que.pop\n next if deg[f]==0\n puts [f,s[f]].join' '\n deg[s[f]]-=1\n s[s[f]]^=f\n que.push s[f] if deg[s[f]]==1\nend\n"}, {"source_code": "n,deg,s,que,sum=gets.to_i,[],[],[],0\n(0...n).each do |i|\n deg[i],s[i]=gets.split.map &:to_i\n sum+=deg[i]\n que.push i if deg[i]==1\nend\np sum/2\nwhile que[0]\n f=que[-1]\n que.pop\n next if deg[f]==0\n puts [f,s[f]].join' '\n deg[s[f]]-=1\n s[s[f]]^=f\n que.push s[f] if deg[s[f]]==1\nend"}, {"source_code": "n=gets.to_i\ndeg,s,que,sum=[],[],[],0\n(0..n-1).each do |i|\n deg[i],s[i]=gets.split.map &:to_i\n sum+=deg[i]\n if deg[i]==1\n que.push i\n end\nend\np sum/2\nwhile que[0]\n f=que[-1]\n que.pop\n if deg[f]==0\n next\n end\n puts [f,s[f]].join' '\n deg[s[f]]-=1\n s[s[f]]^=f\n if deg[s[f]]==1\n que.push s[f]\n end\nend"}, {"source_code": "n=gets.to_i\ndeg,s,que,sum=[],[],[],0\n(0..n-1).each do |i|\n deg[i],s[i]=gets.split.map(&:to_i)\n sum+=deg[i]\n if deg[i]==1\n que.push i\n end\nend\np sum/2\nwhile que[-1]\n f=que[-1]\n que.pop\n if deg[f]==0\n next\n end\n print f,' ',s[f],' '\n deg[s[f]]-=1\n deg[f]-=1\n s[s[f]]^=f\n if deg[s[f]]==1\n que.push s[f]\n end\nend"}, {"source_code": "n,deg,s,que,sum=gets.to_i,[],[],[],0\n(0...n).each do |i|\n deg[i],s[i]=gets.split.map &:to_i\n sum+=deg[i]\n que.push i if deg[i]==1\nend\np sum/2\nwhile que[0]\n f=que[-1]\n que.pop\n next if deg[f]==0\n puts [f,s[f]].join' '\n deg[s[f]]-=1\n s[s[f]]^=f\n que.push s[f] if deg[s[f]]==1\nend\n"}, {"source_code": "n,deg,s,que,sum=gets.to_i,[],[],[],0\n(0...n).each do |i|\n deg[i],s[i]=gets.split.map &:to_i\n sum+=deg[i]\n que.push i if deg[i]==1\nend\np sum/2\nwhile que[0]\n f=que[-1]\n que.pop\n next if deg[f]==0\n puts [f,s[f]].join' '\n deg[s[f]]-=1\n s[s[f]]^=f\n que.push s[f] if deg[s[f]]==1\nend\n"}, {"source_code": "n,deg,s,que,sum=gets.to_i,[],[],[],0\n(0...n).each do |i|\n deg[i],s[i]=gets.split.map &:to_i\n sum+=deg[i]\n que.push i if deg[i]==1\nend\np sum/2\nwhile que[0]\n f=que[-1]\n que.pop\n next if deg[f]==0\n puts [f,s[f]].join' '\n deg[s[f]]-=1\n s[s[f]]^=f\n que.push s[f] if deg[s[f]]==1\nend\n"}, {"source_code": "n,deg,s,que,sum=gets.to_i,[],[],[],0\n(0...n).each do |i|\n deg[i],s[i]=gets.split.map &:to_i\n sum+=deg[i]\n que.push i if deg[i]==1\nend\np sum/2\nwhile que[0]\n f=que[-1]\n que.pop\n next if deg[f]==0\n puts [f,s[f]].join' '\n deg[s[f]]-=1\n s[s[f]]^=f\n que.push s[f] if deg[s[f]]==1\nend\n"}], "negative_code": [], "src_uid": "14ad30e33bf8cad492e665b0a486008e"} {"nl": {"description": "Once Bob got to a sale of old TV sets. There were n TV sets at that sale. TV set with index i costs ai bellars. Some TV sets have a negative price — their owners are ready to pay Bob if he buys their useless apparatus. Bob can «buy» any TV sets he wants. Though he's very strong, Bob can carry at most m TV sets, and he has no desire to go to the sale for the second time. Please, help Bob find out the maximum sum of money that he can earn.", "input_spec": "The first line contains two space-separated integers n and m (1 ≤ m ≤ n ≤ 100) — amount of TV sets at the sale, and amount of TV sets that Bob can carry. The following line contains n space-separated integers ai ( - 1000 ≤ ai ≤ 1000) — prices of the TV sets. ", "output_spec": "Output the only number — the maximum sum of money that Bob can earn, given that he can carry at most m TV sets.", "sample_inputs": ["5 3\n-6 0 35 -2 4", "4 2\n7 0 0 -7"], "sample_outputs": ["8", "7"], "notes": null}, "positive_code": [{"source_code": "ns,ms= gets.chomp.split(/ /)\nn = ns.to_i\nm = ms.to_i\n\nsa = gets.chomp.split(/ /)\nsa.map!{|x| x.to_i}\n\nsum = 0\n\nsa.sort!\nfor i in 0..m-1\n\tsum += sa[i] if sa[i] < 0\nend\n\nputs -sum"}, {"source_code": "n,m=gets.split.map(&:to_i)\nv=[]\ngets.split.map{|i|\n\ti=i.to_i\n\tif i<0 then\n\t\tv << i\n\tend\n}\nv.sort!\nans=0\n[m,v.size].min.times{|i|\n\tans-=v[i]\n}\np ans\n\n"}, {"source_code": "a = gets.chop.split(/ /)\na.map! do |i|\ni = i.to_i\nend\n\nb = gets.chop.split(/ /)\nb.map! do |i|\ni = i.to_i\nend\n\n$cnt=0\n$tmp = Array.new\nfor i in 0..(b.size-1)\nif b[i] < 0\n$tmp[$cnt] = i\n$cnt=$cnt + 1\nend\nend\n\n$sum = 0\nif $cnt <= a[1] \n$tmp.each do |i|\n$sum=$sum - b[i]\nend\nelse\nb.sort!\nfor i in 0..a[1]-1\n$sum=$sum - b[i]\nend\nend\n\nputs $sum"}, {"source_code": "m,n=gets.chomp.split.map(&:to_i)\na=gets.chomp.split.map(&:to_i).sort\nt,i=0,0\nwhile n!=0\n\tt+=a[i] if a[i]<0\n\tn-=1\n\ti+=1\nend\nputs -t"}, {"source_code": "n, m = gets.split.map(&:to_i)\na = gets.split.map(&:to_i).sort.find_all {|x| x < 0 }\nputs (sc = a.first([a.size, m].min).inject(:+)).nil? ? 0 : -sc"}, {"source_code": "n,m=gets.split.map{|e| e.to_i}\nx=gets.split.map{|e| e.to_i}.sort\nans=0\nm.times{|i|\n\tans-=x[i] if x[i]<=0\n}\nputs ans\n\n"}, {"source_code": "#=================================\nif ARGV.include?(\"ONLINE_JUDGE\")\n\t$stdin = File.open(ARGV[0], \"r\")\n\tARGV.delete(\"ONLINE_JUDGE\")\nend\n#=================================\n\nn,m=gets.split.map(&:to_i)\ndata=gets.split.map(&:to_i)\n\nget=data.sort[0, m].select{|v| v<0}.inject(0){|sum, val| sum+=val}.abs\nputs get\n"}, {"source_code": "n,m=gets.split.map &:to_i;p -eval((gets.split.map(&:to_i)+[0]*m).sort()[0...m]*'+')"}, {"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\n\nn, m = gets.split.map(&:to_i)\na = gets.split.map(&:to_i).sort\nearned = 0\na[0...m].each { |i|\n earned += -i if i < 0\n}\nputs earned\n"}, {"source_code": "n,m = gets.split(\" \").map(&:to_i)\narr = gets.split(\" \").map(&:to_i).select{|a| a<0}.sort[0..(m-1)]\nsum = arr.inject(0,:+)\nputs -sum\n"}, {"source_code": "n,m=gets.split.map &:to_i;k=gets.split.map(&:to_i);l=[];0.upto(n-1){|x| k[x]<0?l< a.size\n puts a.inject{|sum,i| sum+=i}.abs\nelse\n puts a[0,m].inject{|sum,i| sum+=i}.abs\nend\n'''\n"}, {"source_code": "n, m = gets.chomp.split.map(&:to_i)\nprices = gets.chomp.split.map(&:to_i)\n\nmin, sum = 0, 0\n\nprices.sort[0..m - 1].each do |p|\n sum += p\n min = [min, sum].min\nend\n\nputs min.abs"}, {"source_code": "n, m = gets.strip.split(\" \").map{|s| s.to_i}\nprices = gets.strip.split(\" \").map {|s| - s.to_i}.reject {|j| j <= 0}.sort.reverse\nprint prices[0...m].reduce(0, :+)"}, {"source_code": "n, m = gets.split.map{|s| s.to_i}\nprices = gets.split.map {|s| s.to_i}.select {|j| j < 0}.sort\nprint -prices[0...m].reduce(0, :+)"}, {"source_code": "n, m = gets.split(' ').map(&:to_i)\nputs gets.split(' ').map(&:to_i).select { |x| x < 0 }.sort.first(m).inject(0, &:+).abs\n"}], "negative_code": [{"source_code": "a = gets.chop.split(/ /)\na.map! do |i|\ni = i.to_i\nend\n\nb = gets.chop.split(/ /)\nb.map! do |i|\ni = i.to_i\nend\n\n$cnt=0\n$tmp = Array.new\nfor i in 0..(b.size-1)\nif b[i] < 0\n$tmp[$cnt] = i\n$cnt=$cnt + 1\nend\nend\n\n$sum = 0\nif $cnt <= a[1] \n$tmp.each do |i|\n$sum=$sum - b[i]\nend\nelse\nb.sort!\nfor i in 0..a[1]\n$sum=$sum - b[i]\nend\nend\n\nputs $sum"}, {"source_code": "n,m=gets.chomp.split.map(&:to_i)\na=gets.chomp.split.map(&:to_i).sort\nt,i=0,0\nwhile n!=0\n\tt+=a[i] if a[i]<0\n\tn-=1\n\ti+=1\nend\nputs -t"}, {"source_code": "n, m = gets.split.map(&:to_i)\na = gets.split.map(&:to_i).find_all {|x| x < 0 }\nputs (sc = a.first([a.size, m].min).inject(:+)).nil? ? 0 : -sc"}, {"source_code": "n, m = gets.split.map(&:to_i)\nj, score = 0, 0\na = gets.split.map(&:to_i).sort!\nwhile j<=m and a[j] < 0\n score += a[j]\n j+=1\nend\nputs -score"}, {"source_code": "n,m=gets.split.map{|e| e.to_i}\nx=gets.split.map{|e| e.to_i}.sort\nans=0\nm.times{|i|\n\tans-=x[i]\n}\nputs ans\n\n"}, {"source_code": "#=================================\nif ARGV.include?(\"ONLINE_JUDGE\")\n\t$stdin = File.open(ARGV[0], \"r\")\n\tARGV.delete(\"ONLINE_JUDGE\")\nend\n#=================================\n\nn,m=gets.split.map(&:to_i)\nputs gets.split.map(&:to_i).sort[0, m].inject(:+).abs\n"}, {"source_code": "#=================================\nif ARGV.include?(\"ONLINE_JUDGE\")\n\t$stdin = File.open(ARGV[0], \"r\")\n\tARGV.delete(\"ONLINE_JUDGE\")\nend\n#=================================\n\nn,m=gets.split.map(&:to_i)\ndata=gets.split.map(&:to_i)\n\nget=data.select{|v| v<0}.inject(0){|sum, val| sum+=val}.abs\nputs get\n"}, {"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\n\nn, m = gets.split.map(&:to_i)\na = gets.split.map(&:to_i).sort\nputs -a[0...m].reduce(:+)\n"}, {"source_code": "n,m = gets.split(\" \").map(&:to_i)\narr = gets.split(\" \").map(&:to_i).select{|a| a<0}.sort\nsum = arr.inject(0,:+)\nputs -sum\n"}, {"source_code": "n, m = gets.chomp.split.map(&:to_i)\nputs gets.chomp.split.map(&:to_i).sort[0..m - 1].inject(0) { |sum, v| sum + v }.abs"}, {"source_code": "n, m = gets.strip.split(\" \").map{|s| s.to_i}\nprices = gets.strip.split(\" \").map {|s| - s.to_i}.reject {|j| j <= 0}.sort.reverse\nm = prices.length if m > prices.length\nprint prices[0..m].reduce(0) {|s, a| s + a}"}], "src_uid": "9a56288d8bd4e4e7ef3329e102f745a5"} {"nl": {"description": "You have a large electronic screen which can display up to $$$998244353$$$ decimal digits. The digits are displayed in the same way as on different electronic alarm clocks: each place for a digit consists of $$$7$$$ segments which can be turned on and off to compose different digits. The following picture describes how you can display all $$$10$$$ decimal digits:As you can see, different digits may require different number of segments to be turned on. For example, if you want to display $$$1$$$, you have to turn on $$$2$$$ segments of the screen, and if you want to display $$$8$$$, all $$$7$$$ segments of some place to display a digit should be turned on.You want to display a really large integer on the screen. Unfortunately, the screen is bugged: no more than $$$n$$$ segments can be turned on simultaneously. So now you wonder what is the greatest integer that can be displayed by turning on no more than $$$n$$$ segments.Your program should be able to process $$$t$$$ different test cases.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases in the input. Then the test cases follow, each of them is represented by a separate line containing one integer $$$n$$$ ($$$2 \\le n \\le 10^5$$$) — the maximum number of segments that can be turned on in the corresponding testcase. It is guaranteed that the sum of $$$n$$$ over all test cases in the input does not exceed $$$10^5$$$.", "output_spec": "For each test case, print the greatest integer that can be displayed by turning on no more than $$$n$$$ segments of the screen. Note that the answer may not fit in the standard $$$32$$$-bit or $$$64$$$-bit integral data type.", "sample_inputs": ["2\n3\n4"], "sample_outputs": ["7\n11"], "notes": null}, "positive_code": [{"source_code": "gets.strip.to_i.times do\n n = gets.strip.to_i\n if n % 2 == 0\n puts \"1\" * (n / 2)\n else\n puts \"7\" + (\"1\" * ((n - 3) / 2))\n end\nend\n"}, {"source_code": "T = gets.to_i\n\nT.times do\n n = gets.to_i\n\n if n % 2 == 0\n puts '1' * (n / 2)\n else\n puts '7' + '1' * ((n - 3) / 2)\n end\nend\n"}, {"source_code": "# coding: utf-8\n\n# 7-segment display\nseg = [[0, 6], [1, 2], [2, 6], [3, 5], [4, 4], [5, 5], [6, 6], [7, 3], [8, 7], [9, 6]]\nt = gets.to_i\nt.times {\n n = gets.to_i\n q, r = n/2, n%2\n\n if r == 0\n puts \"1\"*q\n else\n puts \"7\" + (\"1\"*(q-1))\n end\n}"}], "negative_code": [{"source_code": "T = gets.to_i\n\nT.times do\n n = gets.to_i\n\n if n % 2 == 0\n puts '1' * (n / 2)\n else\n puts '1' * ((n - 3) / 2) + '7'\n end\nend\n"}], "src_uid": "07db573b0db736d798b6c18c06c32f3d"} {"nl": {"description": "You are given an integer $$$n$$$. You have to change the minimum number of digits in it in such a way that the resulting number does not have any leading zeroes and is divisible by $$$7$$$.If there are multiple ways to do it, print any of them. If the given number is already divisible by $$$7$$$, leave it unchanged.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 990$$$) — the number of test cases. Then the test cases follow, each test case consists of one line containing one integer $$$n$$$ ($$$10 \\le n \\le 999$$$).", "output_spec": "For each test case, print one integer without any leading zeroes — the result of your changes (i. e. the integer that is divisible by $$$7$$$ and can be obtained by changing the minimum possible number of digits in $$$n$$$). If there are multiple ways to apply changes, print any resulting number. If the given number is already divisible by $$$7$$$, just print it.", "sample_inputs": ["3\n42\n23\n377"], "sample_outputs": ["42\n28\n777"], "notes": "NoteIn the first test case of the example, $$$42$$$ is already divisible by $$$7$$$, so there's no need to change it.In the second test case of the example, there are multiple answers — $$$28$$$, $$$21$$$ or $$$63$$$.In the third test case of the example, other possible answers are $$$357$$$, $$$371$$$ and $$$378$$$. Note that you cannot print $$$077$$$ or $$$77$$$."}, "positive_code": [{"source_code": "n = gets.chomp.to_i\r\nfor _ in 0...n\r\n num = gets.chomp\r\n len = num.length()\r\n if num.to_i % 7 == 0\r\n puts num\r\n else\r\n for i in 1..9\r\n temp = (num[0, len-1] + i.to_s).to_i\r\n if temp % 7 == 0\r\n puts temp\r\n break\r\n end\r\n end\r\n end\r\nend"}, {"source_code": "gets.to_i.times do\n n = gets.to_i\n if n % 7 == 0\n puts n\n else\n m = n / 10\n (0..9).each { |d|\n if (m - 2 * d) % 7 == 0\n puts \"#{m}#{d}\"\n break\n end\n }\n end\nend\n"}], "negative_code": [], "src_uid": "128372d890f632494e59e81287abd85a"} {"nl": {"description": "You are given an array $$$a$$$ of $$$n$$$ integers. Find the number of pairs $$$(i, j)$$$ ($$$1 \\le i < j \\le n$$$) where the sum of $$$a_i + a_j$$$ is greater than or equal to $$$l$$$ and less than or equal to $$$r$$$ (that is, $$$l \\le a_i + a_j \\le r$$$).For example, if $$$n = 3$$$, $$$a = [5, 1, 2]$$$, $$$l = 4$$$ and $$$r = 7$$$, then two pairs are suitable: $$$i=1$$$ and $$$j=2$$$ ($$$4 \\le 5 + 1 \\le 7$$$); $$$i=1$$$ and $$$j=3$$$ ($$$4 \\le 5 + 2 \\le 7$$$). ", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$). Then $$$t$$$ test cases follow. The first line of each test case contains three integers $$$n, l, r$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$, $$$1 \\le l \\le r \\le 10^9$$$) — the length of the array and the limits on the sum in the pair. The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$). It is guaranteed that the sum of $$$n$$$ overall test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, output a single integer — the number of index pairs $$$(i, j)$$$ ($$$i < j$$$), such that $$$l \\le a_i + a_j \\le r$$$.", "sample_inputs": ["4\n3 4 7\n5 1 2\n5 5 8\n5 1 2 4 3\n4 100 1000\n1 1 1 1\n5 9 13\n2 5 5 1 1"], "sample_outputs": ["2\n7\n0\n1"], "notes": null}, "positive_code": [{"source_code": "def l_bound(x, i, a)\r\n lo, hi = 0, i - 1\r\n while lo <= hi\r\n mi = (lo + hi) / 2\r\n if a[mi] < x\r\n lo = mi + 1\r\n elsif\r\n hi = mi - 1\r\n end\r\n end\r\n return lo\r\nend\r\n\r\ndef u_bound(x, i, a) \r\n lo, hi = 0, i - 1\r\n while lo <= hi\r\n mi = (lo + hi) / 2\r\n if a[mi] <= x\r\n lo = mi + 1\r\n elsif\r\n hi = mi - 1\r\n end\r\n end\r\n return hi\r\nend\r\n\r\nfor t in 0...gets.to_i\r\n n, l, r = gets.split.map(&:to_i)\r\n a = gets.split.map(&:to_i).sort\r\n ans = 0\r\n (0...n).each do |i|\r\n lo = l_bound(l - a[i], i, a)\r\n hi = u_bound(r - a[i], i, a)\r\n if lo <= hi and lo >= 0 and hi < i\r\n ans += hi - lo + 1\r\n end\r\n end\r\n puts ans\r\nend"}, {"source_code": "gets.to_i.times do\r\n n,l,r=gets.split.map(&:to_i)\r\n a=gets.split.map(&:to_i).sort\r\n c=0\r\n i=0\r\n j=n-1\r\n k=n-1\r\n while ii&&a[i]+a[j]>=l\r\n j-=1\r\n end\r\n while k>0&&a[i]+a[k]>r\r\n k-=1\r\n end\r\n c+=[k,i].max-[j,i].max\r\n i+=1\r\n end\r\n puts c\r\nend\r\n"}], "negative_code": [], "src_uid": "2f12b2bb23497119bb70ca0839991d68"} {"nl": {"description": "Let $$$f_{x} = c^{2x-6} \\cdot f_{x-1} \\cdot f_{x-2} \\cdot f_{x-3}$$$ for $$$x \\ge 4$$$.You have given integers $$$n$$$, $$$f_{1}$$$, $$$f_{2}$$$, $$$f_{3}$$$, and $$$c$$$. Find $$$f_{n} \\bmod (10^{9}+7)$$$.", "input_spec": "The only line contains five integers $$$n$$$, $$$f_{1}$$$, $$$f_{2}$$$, $$$f_{3}$$$, and $$$c$$$ ($$$4 \\le n \\le 10^{18}$$$, $$$1 \\le f_{1}$$$, $$$f_{2}$$$, $$$f_{3}$$$, $$$c \\le 10^{9}$$$).", "output_spec": "Print $$$f_{n} \\bmod (10^{9} + 7)$$$.", "sample_inputs": ["5 1 2 5 3", "17 97 41 37 11"], "sample_outputs": ["72900", "317451037"], "notes": "NoteIn the first example, $$$f_{4} = 90$$$, $$$f_{5} = 72900$$$.In the second example, $$$f_{17} \\approx 2.28 \\times 10^{29587}$$$."}, "positive_code": [{"source_code": "require 'Matrix'\n\ndef modpowMatrix(x,n)\n ret = Matrix.I(x.column_size)\n while n > 0\n ret = (ret * x).map{|v| v % (MOD-1)} if n&1 == 1\n x = (x*x).map{|v| v % (MOD-1)}\n n >>= 1\n end\n return ret\nend\n\ndef modpow(x,n)\n ret = 1\n while n > 0\n ret = (ret * x) % MOD if n&1 == 1\n x = (x*x) % MOD\n n >>= 1\n end\n return ret\nend\n\nMOD = 10**9+7\nn,x,y,z,c = gets.split.map(&:to_i)\na = Matrix.rows([[1,1,1],[1,0,0],[0,1,0]])\nb = Matrix.rows([[1,1,1,1,0],[1,0,0,0,0],[0,1,0,0,0],[0,0,0,1,1],[0,0,0,0,1]])\n\nm = modpowMatrix(a,n-3)\nft = Matrix.columns([[0,0,0,2,2]])\nk = (modpowMatrix(b,n-3)*ft)[0,0] % (MOD-1)\np ((modpow(c,k) * modpow(z,m[0,0]) % MOD) * modpow(y,m[0,1]) % MOD) * modpow(x,m[0,2]) % MOD\n"}, {"source_code": "require 'Matrix'\n \ndef fpmmat(x,n)\n ret = Matrix.I(x.column_size)\n while n > 0\n ret = (ret * x).map{|v| v % (MOD-1)} if n&1 == 1\n x = (x * x).map{|v| v % (MOD-1)}\n n >>= 1\n end\n return ret\nend\n \ndef fpm(x,n)\n ret = 1\n while n > 0\n ret = (ret * x) % MOD if n&1 == 1\n x = (x*x) % MOD\n n >>= 1\n end\n return ret\nend\n\nMOD = 10**9+7\nn,x,y,z,c = gets.split.map(&:to_i)\na = Matrix.rows([[1,1,1],[1,0,0],[0,1,0]])\nb = Matrix.rows([[1,1,1,1,0],[1,0,0,0,0],[0,1,0,0,0],[0,0,0,1,1],[0,0,0,0,1]])\n \nm = fpmmat(a,n-3)\nft = Matrix.columns([[0,0,0,2,2]])\nk = (fpmmat(b,n-3)*ft)[0,0] % (MOD-1)\np ((fpm(c,k) * fpm(z,m[0,0]) % MOD) * fpm(y,m[0,1]) % MOD) * fpm(x,m[0,2]) % MOD"}], "negative_code": [{"source_code": "require 'Matrix'\n\ndef modpowMatrix(x,n)\n ret = Matrix.I(x.column_size)\n while n > 0\n\t\tret = (ret * x).map{|v| v%MOD} if n&1 == 1\n x = (x*x).map{|v| v%MOD}\n n >>= 1\n end\n return ret\nend\n\ndef modpow(x,n)\n ret = 1\n while n > 0\n ret = (ret * x) % MOD if n&1 == 1\n x = (x*x) % MOD\n n >>= 1\n end\n return ret\nend\n\nMOD = 10**9+7\nn,x,y,z,c = gets.split.map(&:to_i)\na = Matrix.rows([[1,1,1],[1,0,0],[0,1,0]])\nb = Matrix.rows([[1,1,1,1,0],[1,0,0,0,0],[0,1,0,0,0],[0,0,0,1,1],[0,0,0,0,1]])\n\nm = modpowMatrix(a,n-3)\ncf = Matrix.columns([[0,0,0,2,2]])\nk = (modpowMatrix(b,n-3)*cf)[0,0] % MOD\np ((modpow(c,k) * modpow(z,m[0,0]) % MOD) * modpow(y,m[0,1]) % MOD) * modpow(x,m[0,2]) % MOD\n"}], "src_uid": "6fcd8713af5a108d590bc99da314cded"} {"nl": {"description": "Professor Vasechkin is studying evolution of worms. Recently he put forward hypotheses that all worms evolve by division. There are n forms of worms. Worms of these forms have lengths a1, a2, ..., an. To prove his theory, professor needs to find 3 different forms that the length of the first form is equal to sum of lengths of the other two forms. Help him to do this.", "input_spec": "The first line contains integer n (3 ≤ n ≤ 100) — amount of worm's forms. The second line contains n space-separated integers ai (1 ≤ ai ≤ 1000) — lengths of worms of each form.", "output_spec": "Output 3 distinct integers i j k (1 ≤ i, j, k ≤ n) — such indexes of worm's forms that ai = aj + ak. If there is no such triple, output -1. If there are several solutions, output any of them. It possible that aj = ak.", "sample_inputs": ["5\n1 2 3 5 7", "5\n1 8 1 5 1"], "sample_outputs": ["3 2 1", "-1"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\n\nlens = gets.split.map{|i| i.to_i}\n\ni = 0\nk = true\nwhile k && i < n-1 do \n j = 1\n while k && j < n do\n if (t = lens.index(lens[i] + lens[j])) then\n k = false\n result = (t+1).to_s + \" \" + (i+1).to_s + \" \" + (j+1).to_s\n end\n j += 1\n end\n i += 1\nend\n\nif k then\n puts \"-1\"\nelse\n puts result\nend"}, {"source_code": "@ok = false\n\ndef chk3(arr, i, j, k)\n\tif (arr[i] == arr[j]+arr[k]) \n\t\tputs (i+1).to_s + \" \" + (j+1).to_s + \" \" + (k+1).to_s\t\t\t\t\n\t\t@ok = true\t\t\n\t\treturn true\n\tend\n\treturn false\nend\n\nn = gets.to_i\ns = gets\nna = s.split(/ /)\narr = Array.new(n)\nfor i in 0..n-1\n arr[i] = na[i].to_i\nend\n\n(n-1).downto(2) { |i|\n\t(i-1).downto(1) { |j|\n\t\t(j-1).downto(0) { |k| \n\t\t\tbreak if chk3(arr, i, j, k)\n\t\t\tbreak if chk3(arr, i, k, j)\n\t\t\tbreak if chk3(arr, j, i, k)\n\t\t\tbreak if chk3(arr, j, k, i)\n\t\t\tbreak if chk3(arr, k, i, j)\n\t\t\tbreak if chk3(arr, k, j, i)\n\t\t}\n\t\tbreak if @ok\n\t}\n\tbreak if @ok\n}\nputs(\"-1\") if @ok == false\n"}, {"source_code": "#!/usr/bin/ruby\ngets;a=gets.split.map(&:to_i)\na.size.times{|i|a.size.times{|j|(j+1).step(a.size-1){|k|\n\tif a[i]==a[j]+a[k] then puts [i+1,j+1,k+1]*' ';exit end\n}}}\np -1"}, {"source_code": "n=gets.to_i\na=[0]+gets.split.map(&:to_i)\n1.upto(n).to_a.combination(2).each{|i,j|k=a.index a[i]+a[j];\"#{p k,i,j;exit}\"if k}\np -1\n"}, {"source_code": "a = gets.chop\n\nb = gets.chop.split(/ /)\nb.map! do |i|\ni = i.to_i\nend\n\nc = Array.new\nflag = 0\nfor i in 0..b.size-1\n\tfor j in i+1..b.size-1\n\t\tfor k in j+1..b.size-1\n\t\t\tif b[i] == b[j]+b[k]\n\t\t\t\tflag=1\n\t\t\t\tc[0]=i+1\n\t\t\t\tc[1]=j+1\n\t\t\t\tc[2]=k+1\n\t\t\telsif b[j] == b[i]+b[k]\n\t\t\t\tflag=1\n\t\t\t\tc[0]=j+1\n\t\t\t\tc[1]=i+1\n\t\t\t\tc[2]=k+1\n\t\t\telsif b[k] == b[i]+b[j]\n\t\t\t\tflag=1\n\t\t\t\tc[0]=k+1\n\t\t\t\tc[1]=i+1\n\t\t\t\tc[2]=j+1\n\t\t\tend\n\t\t\tif flag==1\n\t\t\t\tbreak\n\t\t\tend\n\t\tend\n\t\tif flag==1\n\t\t\tbreak\n\t\tend\n\tend\n\tif flag==1\n\t\tbreak\n\tend\nend\n\nif flag==0\n\tprint -1\nelse\n\tfor i in 0..c.size-1\n\t\tprint c[i]\n\t\tif i !=c.size-1\n\t\t\tprint \" \" \n\t\tend\n\tend\nend"}, {"source_code": "a = gets.chop\n\nb = gets.chop.split(/ /)\nb.map! do |i|\ni = i.to_i\nend\n\nc = Array.new\nflag = 0\nfor i in 0..b.size-1\n\tfor j in i+1..b.size-1\n\t\tfor k in j+1..b.size-1\n\t\t\tif b[i] == b[j]+b[k]\n\t\t\t\tflag=1\n\t\t\t\tc[0]=i+1\n\t\t\t\tc[1]=j+1\n\t\t\t\tc[2]=k+1\n\t\t\telsif b[j] == b[i]+b[k]\n\t\t\t\tflag=1\n\t\t\t\tc[0]=j+1\n\t\t\t\tc[1]=i+1\n\t\t\t\tc[2]=k+1\n\t\t\telsif b[k] == b[i]+b[j]\n\t\t\t\tflag=1\n\t\t\t\tc[0]=k+1\n\t\t\t\tc[1]=i+1\n\t\t\t\tc[2]=j+1\n\t\t\tend\n\t\t\tif flag==1\n\t\t\t\tbreak\n\t\t\tend\n\t\tend\n\t\tif flag==1\n\t\t\tbreak\n\t\tend\n\tend\n\tif flag==1\n\t\tbreak\n\tend\nend\n\nif flag==0\n\tprint -1\nelse\n\tfor i in 0..c.size-1\n\t\tprint c[i].to_s + \" \"\n\tend\nend"}, {"source_code": "n=gets.to_i\na=[0]+gets.split.map(&:to_i)\n1.upto(n).to_a.combination(2).each{|i,j|k=a.index a[i]+a[j];\"#{p k,i,j;exit}\"if k}\np -1\n"}, {"source_code": "n=gets.to_i\na=[0]+gets.split.map(&:to_i)\n1.upto(n).to_a.combination(2).each{|i,j|k=a.index a[i]+a[j];\"#{p k,i,j;exit}\"if k}\np -1"}, {"source_code": "n=gets.to_i\na=gets.chomp.split.map(&:to_i)\nfor i in 0...n\n\tfor j in 0...n\n\t\tfor k in 0...n\n\t\t\tif i!=j && i!=k && j!=k && a[i]==a[j]+a[k]\n\t\t\t\tputs \"#{i+1} #{j+1} #{k+1}\"\n\t\t\t\texit\n\t\t\tend\n\t\tend\n\tend\nend\nputs \"-1\""}, {"source_code": "n=gets.to_i\na=[0]+gets.split.map(&:to_i)\n1.upto(n).to_a.combination(2).each{|i,j|k=a.index a[i]+a[j];\"#{p k,i,j;exit}\"if k}\np -1\n"}, {"source_code": "n=gets.to_i\na=[0]+gets.split.map(&:to_i)\n1.upto(n).to_a.combination(2).each{|i,j|k=a.index a[i]+a[j];\"#{p k,i,j;exit}\"if k}\np -1\n"}, {"source_code": "n = gets.strip.to_i\na = gets.strip.split.map(&:to_i)\ndone = false\nx = y = z = 0\nn.times do |i|\n break if done\n ai = a[i]\n n.times do |j|\n next if i == j\n break if done\n aj = a[j]\n n.times do |k|\n next if j == k\n if aj + a[k] == ai\n x = i + 1\n y = k + 1\n z = j + 1\n done = true\n break\n end\n end\n end\nend\nif !done\n puts \"-1\"\nelse\n puts \"#{x} #{y} #{z}\"\nend\n"}, {"source_code": "n=gets.to_i\na=[0]+gets.split.map(&:to_i)\n1.upto(n).to_a.combination(2).each{|i,j|k=a.index a[i]+a[j];\"#{p k,i,j;exit}\"if k}\np -1"}, {"source_code": "n = gets.to_i\na = gets.split.map &:to_i\n\nq = {}\na.each_index { |i| q[a[i]] = i }\n\nn.times do |i|\n i.times do |j|\n if q[ a[i] + a[j] ] != nil\n puts \"#{ q[a[i] + a[j]] + 1 } #{i + 1} #{j + 1}\"\n exit\n end\n end\nend\n\nputs \"-1\""}, {"source_code": "n=gets.to_i\na=[0]+gets.split.map(&:to_i)\n1.upto(n).to_a.combination(2).each{|i,j|k=a.index a[i]+a[j];\"#{p k,i,j;exit}\"if k}\np -1"}, {"source_code": "def main()\n\tn=gets.chomp.to_i\n\tstrinp=gets.chomp.split(\" \")\n\tstrinp=strinp.map{|i| i.to_i}\n\tfor i in 0...n\n\t\tsum=strinp[i]\n\t\tf=false\n\t\tfor j in 0...n\n\t\t\trem=sum-strinp[j]\n\t\t\t\tfor k in 0...n \n\t\t\t\t\tif strinp[k]==rem && k!=j && k!=i\n\t\t\t\t\t\tputs \"%d %d %d\" %[i+1,j+1,k+1]\n\t\t\t\t\t\tf=true\n\t\t\t\t\t\tbreak\n\t\t\t\t\tend\n\t\t\t\tend\n\t\t\tif f \n\t\t\t\tbreak\n\t\t\tend\n\t\tend\n\t\tif f \n\t\t\tbreak\n\t\tend\n\tend\n\tif !f\n\t\tputs \"-1\"\n\tend\nend\nmain()"}, {"source_code": "n=gets.to_i\na=[0]+gets.split.map(&:to_i)\n1.upto(n).to_a.combination(2).each{|i,j|k=a.index a[i]+a[j];\"#{p k,i,j;exit}\"if k}\np -1\n"}, {"source_code": "# def bin_search(arr,e)\n# \tn = arr.length\n# \tl = 0\n# \tr = n - 1\n\n# \twhile l <= r\n# \t\tm = l + (r-l) / 2\n# \t\tif arr[m] == e\n# \t\t\tputs \"#{m}\"\n# \t\t\treturn m\n# \t\telsif e < arr[m]\n# \t\t\tr = m - 1\n# \t\telse\n# \t\t\tl = m + 1\n# \t\tend\t\t\t\n# \tend\n\n# \treturn false\n\t\n# end\n\nn = gets.to_i\narr = gets.split(\" \").map(&:to_i)\n# arr.sort!\nans = false\nans_i = ans_j = ans_k = 0\narr.each_with_index do |i,ind_i|\n\tarr[ind_i+1..-1].each_with_index do |j,ind_j|\n\t\t# puts \"#{i+j} ans #{bin_search(arr[ind_j+1..-1], i + j)}\"\n\t\tind_k = arr.index(i+j) #bin_search(arr[ind_j+1..-1], i + j)\n\t\t# puts \"#{i} #{j} #{ind_k}\"\n\t\tif ind_k\n\t\t\tans = true\n\t\t\tans_j = ind_i\n\t\t\tans_k = ind_i + ind_j + 1\n\t\t\tans_i = ind_k\n\t\t\tbreak\n\t\tend\n\tend\n\tbreak if ans\nend\n\nputs ans ? \"#{ans_i+1} #{ans_j+1} #{ans_k+1}\": \"-1\" "}, {"source_code": "gets\nmas = gets.split(\" \")\nfor i in (0..mas.size - 1)\n mas[i] = mas[i].to_i\nend\n\nfor i in (0..mas.size - 1)\n for j in (0..mas.size - 1)\n for k in (0..mas.size - 1)\n if (i != j && j != k && i != k && mas[i] == mas[j]+mas[k])\n print i+1, \" \", j+1, \" \", k+1\n exit\n end\n end\n end\nend\n\nprint -1"}, {"source_code": "gets\nworms = gets.chomp.split.map(&:to_i)\n\nres = [-1]\nfound = false\nworms.each_with_index do |w1, i|\n break if found\n worms.each_with_index do |w2, j|\n break if found\n next if j == i\n worms.each_with_index do |w3, k|\n break if found\n next if k == j or k == i\n if w3 == w1 + w2\n res = [k.succ, j.succ, i.succ]\n found = true\n end\n end\n end\nend\nputs res.join(' ')\n"}, {"source_code": "n=gets.to_i\na=[0]+gets.split.map(&:to_i)\n1.upto(n).to_a.combination(2).each{|i,j|k=a.index a[i]+a[j];\"#{p k,i,j;exit}\"if k}\np -1\n"}, {"source_code": "n=gets.to_i\na=[0]+gets.split.map(&:to_i)\n1.upto(n).to_a.combination(2).each{|i,j|k=a.index a[i]+a[j];\"#{p k,i,j;exit}\"if k}\np -1\n"}, {"source_code": "n=gets.to_i\na=[0]+gets.split.map(&:to_i)\n1.upto(n).to_a.combination(2).each{|i,j|k=a.index a[i]+a[j];\"#{p k,i,j;exit}\"if k}\np -1\n"}], "negative_code": [{"source_code": "n = gets.to_i\n\nlens = gets.split.map{|i| i.to_i}\n\ni = 0\nk = true\nwhile k && i < n-1 do \n j = 1\n while k && j < n do\n if (t = lens.index(lens[i] + lens[j])) then\n k = false\n result = lens[t].to_s + \" \" + lens[i].to_s + \" \" + lens[j].to_s\n end\n j += 1\n end\n i += 1\nend\n\nif k then\n puts \"-1\"\nelse\n puts result\nend"}, {"source_code": "n = gets.to_i\ns = gets\nna = s.split(/ /)\narr = Array.new(n)\nfor i in 0..n-1\n arr[i] = na[i].to_i\nend\narr.sort!\nok = false\n(n-1).downto(2) { |i|\n\t(i-1).downto(1) { |j|\n\t\t(j-1).downto(0) { |k| \n\t\t\tif (arr[i] == arr[j]+arr[k]) \n\t\t\t\tputs (i+1).to_s + \" \" + (j+1).to_s + \" \" + (k+1).to_s\t\t\t\t\n\t\t\t\tok = true\n\t\t\t\tbreak\t\t\n\t\t\tend\n\t\t}\n\t\tbreak if ok\n\t}\n\tbreak if ok\n}\nputs(\"-1\") if ok == false\n"}, {"source_code": "n = gets.strip.to_i\na = gets.strip.split.map(&:to_i)\ndone = false\nx = y = z = 0\nn.times do |i|\n break if done\n ai = a[i]\n (1...n).each do |j|\n if a[j - 1] + a[j] == ai\n x = i + 1\n y = j + 1\n z = j\n done = true\n break\n end\n end\nend\nif !done\n puts \"-1\"\nelse\n puts \"#{x} #{y} #{z}\"\nend\n"}, {"source_code": "n = gets.strip.to_i\na = gets.strip.split.map(&:to_i)\ndone = false\nx = y = z = 0\nn.times do |i|\n break if done\n ai = a[i]\n n.times do |j|\n break if done\n aj = a[j]\n n.times do |k|\n if aj + a[k] == ai\n x = i + 1\n y = j + 1\n z = k + 1\n done = true\n break\n end\n end\n end\nend\nif !done\n puts \"-1\"\nelse\n puts \"#{x} #{y} #{z}\"\nend\n"}, {"source_code": "gets\na=gets.split.map &:to_i\na.combination(2).each{|i,j|\"#{p i+j,i,j;exit}\"if a.index i+j}\np -1"}, {"source_code": "def main()\n\tn=gets.chomp.to_i\n\tstrinp=gets.chomp.split(\" \")\n\tstrinp=strinp.map{|i| i.to_i}\n\tfor i in 0...n\n\t\tsum=strinp[i]\n\t\tf=false\n\t\tfor j in 0...n\n\t\t\trem=sum-strinp[j]\n\t\t\tsearch=strinp-[sum,strinp[j]]\n\t\t\tif search.include?(rem)\n\t\t\t\tputs \"%d %d %d\" %[sum,strinp[j],rem]\n\t\t\t\tf=true\n\t\t\t\tbreak\n\t\t\tend\n\t\tend\n\t\tif f \n\t\t\tbreak\n\t\tend\n\tend\n\tif !f\n\t\tputs \"-1\"\n\tend\nend\nmain()"}, {"source_code": "# def bin_search(arr,e)\n# \tn = arr.length\n# \tl = 0\n# \tr = n - 1\n\n# \twhile l <= r\n# \t\tm = l + (r-l) / 2\n# \t\tif arr[m] == e\n# \t\t\tputs \"#{m}\"\n# \t\t\treturn m\n# \t\telsif e < arr[m]\n# \t\t\tr = m - 1\n# \t\telse\n# \t\t\tl = m + 1\n# \t\tend\t\t\t\n# \tend\n\n# \treturn false\n\t\n# end\n\nn = gets.to_i\narr = gets.split(\" \").map(&:to_i)\n# arr.sort!\nans = false\nans_i = ans_j = ans_k = 0\narr.each_with_index do |i,ind_i|\n\tarr[ind_i+1..-1].each_with_index do |j,ind_j|\n\t\t# puts \"#{i+j} ans #{bin_search(arr[ind_j+1..-1], i + j)}\"\n\t\tind_k = arr[ind_j+1..-1].index(i+j) #bin_search(arr[ind_j+1..-1], i + j)\n\t\t# puts \"#{i} #{j} #{ind_k}\"\n\t\tif ind_k\n\t\t\tans = true\n\t\t\tans_j = ind_i\n\t\t\tans_k = ind_i + ind_j + 1\n\t\t\tans_i = ans_k + ind_k\n\t\t\tbreak\n\t\tend\n\tend\n\tbreak if ans\nend\n\nputs ans ? \"#{ans_i+1} #{ans_j+1} #{ans_k+1}\": \"-1\" "}, {"source_code": "def bin_search(arr,e)\n\tn = arr.length\n\tl = 0\n\tr = n - 1\n\n\twhile l <= r\n\t\tm = l + (r-l) / 2\n\t\tif arr[m] == e\n\t\t\treturn true\n\t\telsif e < arr[m]\n\t\t\tr = m - 1\n\t\telse\n\t\t\tl = m + 1\n\t\tend\t\t\t\n\tend\n\n\treturn false\n\t\nend\n\nn = gets.to_i\narr = gets.split(\" \").map(&:to_i)\narr.sort!\nans = false\nans_i = ans_j = ans_k = 0\narr.each_with_index do |i,ind_i|\n\tarr[ind_i+1..-1].each_with_index do |j,ind_j|\n\t\t# puts \"#{i+j} ans #{bin_search(arr[ind_j+1..-1], i + j)}\"\n\t\tif bin_search(arr[ind_j+1..-1], i + j)\n\t\t\tans = true\n\t\t\tans_i = i + j\n\t\t\tans_j = i\n\t\t\tans_k = j\n\t\t\tbreak\n\t\tend\n\tend\n\tbreak if ans\nend\n\nputs ans ? \"#{ans_i} #{ans_j} #{ans_k}\": \"-1\" "}, {"source_code": "gets\nworms = gets.chomp.split.map(&:to_i)\n\nres = [-1]\nfound = false\nworms.each_with_index do |w1, i|\n break if found\n worms.each_with_index do |w2, j|\n break if found\n next if j == i\n worms.each_with_index do |w3, k|\n break if found\n next if k == j or k == i\n if w3 == w1 + w2\n res = [i.succ, j.succ, k.succ]\n found = true\n end\n end\n end\nend\nputs res.join(' ')\n"}], "src_uid": "94a38067fc8dd8619fa6e5873ca60220"} {"nl": {"description": "There are $$$n$$$ friends who want to give gifts for the New Year to each other. Each friend should give exactly one gift and receive exactly one gift. The friend cannot give the gift to himself.For each friend the value $$$f_i$$$ is known: it is either $$$f_i = 0$$$ if the $$$i$$$-th friend doesn't know whom he wants to give the gift to or $$$1 \\le f_i \\le n$$$ if the $$$i$$$-th friend wants to give the gift to the friend $$$f_i$$$.You want to fill in the unknown values ($$$f_i = 0$$$) in such a way that each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. It is guaranteed that the initial information isn't contradictory.If there are several answers, you can print any.", "input_spec": "The first line of the input contains one integer $$$n$$$ ($$$2 \\le n \\le 2 \\cdot 10^5$$$) — the number of friends. The second line of the input contains $$$n$$$ integers $$$f_1, f_2, \\dots, f_n$$$ ($$$0 \\le f_i \\le n$$$, $$$f_i \\ne i$$$, all $$$f_i \\ne 0$$$ are distinct), where $$$f_i$$$ is the either $$$f_i = 0$$$ if the $$$i$$$-th friend doesn't know whom he wants to give the gift to or $$$1 \\le f_i \\le n$$$ if the $$$i$$$-th friend wants to give the gift to the friend $$$f_i$$$. It is also guaranteed that there is at least two values $$$f_i = 0$$$.", "output_spec": "Print $$$n$$$ integers $$$nf_1, nf_2, \\dots, nf_n$$$, where $$$nf_i$$$ should be equal to $$$f_i$$$ if $$$f_i \\ne 0$$$ or the number of friend whom the $$$i$$$-th friend wants to give the gift to. All values $$$nf_i$$$ should be distinct, $$$nf_i$$$ cannot be equal to $$$i$$$. Each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. If there are several answers, you can print any.", "sample_inputs": ["5\n5 0 0 2 4", "7\n7 0 0 1 4 0 6", "7\n7 4 0 3 0 5 1", "5\n2 1 0 0 0"], "sample_outputs": ["5 3 1 2 4", "7 3 2 1 4 5 6", "7 4 2 3 6 5 1", "2 1 4 5 3"], "notes": null}, "positive_code": [{"source_code": "require 'set'\n\nn = readline.to_i\nf = [nil] + readline.split.map(&:to_i)\n\nzeros = (1..n).select { |i| f[i] == 0 }.to_set\nsources = (1..n).select { |i| f[i] != 0 and f[f[i]] == 0 }\nsinks = (1..n).to_set.difference(f).select { |i| f[i] != 0 }\n\nif sources.length > 1 then\n for source in sources[0..-2] do\n f[f[source]] = sinks.pop\n zeros.delete(f[source])\n end\nend\n\nzeros = zeros.to_a\nzeros.each_cons(2) do |i,j|\n f[i] = j\nend\nf[zeros[-1]] = zeros[0]\n\nif sources.length > 0 then\n if zeros.length > 1 then\n f[zeros[(zeros.find_index(f[sources[-1]]) - 1) % zeros.length]] = sinks.pop\n else\n f[f[sources[-1]]] = sinks.pop\n end\nend\n\nprint(f[1..-1].join(' '))"}, {"source_code": "gets.strip.to_i\nf = gets.strip.split(' ').map(&:to_i)\nno_rece = Array.new(f.size, -1)\nno_give = []\nf.each_with_index do |e, i|\n no_give << (i+1) if e.zero?\n no_rece[e - 1] = i unless e.zero?\nend\nno_rece = no_rece.each_with_index.map { |e, i| e == -1 ? (i + 1) : nil }.compact.sort\nno_give = no_give.sort.reverse\n# inter = no_rece & no_give\n# puts no_rece.inspect\n# puts no_give.inspect\nno_give.each_with_index do |e, i|\n if no_rece[i] == e\n a = no_give[i]\n b = no_rece[i]\n c = i ==0 ? no_give[i +1] : no_give[i - 1]\n d = i ==0 ? no_rece[i +1] : no_rece[i - 1]\n # no_give[i] = c\n # no_give[i == 0 ? i + 1 : i - 1] = a\n no_rece[i] = d\n no_rece[i == 0 ? i + 1 : i - 1] = b\n end\nend\n# puts no_rece.inspect\n# puts no_give.inspect\nno_give.each_with_index do |e, i|\n f[e - 1] = no_rece[i]\nend\nputs f.join(\" \")\n# puts f.map { |e| e.zero? ? }"}, {"source_code": "MOD=10**9+7\ncnt=0; sum=0; prev=nil; can=true; h=Hash.new(0)\ndef gs() gets.chomp end\ndef gi() gets.to_i end\ndef gsmi() gets.chomp.split.map(&:to_i) end\n\ndef desc(ar) ar.sort!{|x,y|y<=>x} end\ndef min(a,b) a<=b ? a : b end\ndef max(a,b) a>=b ? a : b end\ndef sum(ar) ar.inject(:+) end\n\ndef C(a,b) b==0||a==b ? 1 : (b=a-b if a/20\n s=lackpos.size\n s.times do |i|\n f[lackpos[i]-1]=lackpos[(i+1)%s]\n end\nend\n#puts f.to_s\n\nn.times do |i|\n next if f[i]!=0\n f[i]=ar3.shift\n \n break if ar.size==0\nend\n \nputs f.join(\" \")"}], "negative_code": [{"source_code": "n=gets.to_i\nf=gets.split.map(&:to_i)\nfixed=f.sort_by{|x|-x}.to_a\n#puts f.to_s\n#puts fixed.to_s\n\nar=(1..n).to_a\nn.times do |i|\n break if fixed[i]==0\n ar.delete_at(fixed[i]-1)\nend\n#puts ar.to_s\nar.push(ar.shift)\nn.times do |i|\n next if f[i]!=0\n nn=ar.shift\n if nn==i+1\n f[i]=ar.shift\n ar<b} # order by ab,c,d\n tmp=[]\n @src = @src.my_chunk_while {|i,j| i[0][0] == j[0][0]}.to_a # [[[[ab,c,d],i],[[ab,l,f],k],...], ..]\n @result=\"YES\"\n end\n\n def getResult\n init\n ->{@src.map do |x| # x: [[[ab,c,d],i],[[ab,l,f],k],...]\n if x.length == 1\n x[0][0] = x[0][0][0]+x[0][0][1] # x[0]: [[ab,c,d],i]\n elsif x.group_by {|a| a[0][1]}.length == x.length\n x.map {|s| s[0]=s[0][0]+s[0][1]}\n # elsif x.my_chunk_while {|a,b| a[0][2] == b[0][2]}.to_a.length == x.length\n elsif x.group_by {|a| a[0][2]}.length == x.length\n x.map {|s| s[0]=s[0][0]+s[0][2]}\n else\n @result = \"NO\"\n return\n end\n end}[]\n printResult\n end\n\n def printResult\n if @result == \"NO\"\n @src = []\n @result\n else\n tmp=[]\n @src.each {|x| x.each {|y| tmp<b[1] }\n @src = @src.map {|x| x[0]}\n end\n [@result,@src]\n end\nend\n\nif __FILE__ == $0\n fl = FootballLeague.new\n fl.n = $stdin.readline.to_i\n fl.src = $stdin.readlines\n fl.getResult\n puts fl.result\n puts fl.src.join(\"\\n\") if fl.result == \"YES\"\nend\n\n"}], "negative_code": [{"source_code": "#!/usr/bin/env ruby\n\nclass Array\n def my_chunk_while(&blk)\n self.reduce([]) do |s, x|\n if s.empty?\n s << [x]\n # elsif s[-1][0][0] == x[0]\n elsif blk[s[-1][0], x]\n s[-1] << x\n s\n else\n s << [x]\n end\n end\n end\nend\n\nclass FootballLeague\n attr_accessor :n, :src, :result\n\n def init\n @src = @src.map {|x| x=x.split;[x[0][0..1],x[0][2],x[1][0]]} # [[ab,c,d],...]\n tmp = []\n @src = @src.each_with_index { |obj, ind | tmp <<[obj,ind] } # [[[ab,c,d],0],...]\n @src = tmp.sort {|a,b| a=a[0].join;b=b[0].join;a<=>b} # order by ab,c,d\n tmp=[]\n @src = @src.my_chunk_while {|i,j| i[0][0] == j[0][0]}.to_a # [[[[ab,c,d],i],[[ab,l,f],k],...], ..]\n @result=\"YES\"\n end\n\n def getResult\n init\n ->{@src.map do |x| # x: [[[ab,c,d],i],[[ab,l,f],k],...]\n if x.length == 1\n x[0][0] = x[0][0][0]+x[0][0][1] # x[0]: [[ab,c,d],i]\n elsif x.my_chunk_while {|a,b| a[0][1] == b[0][1]}.to_a.length == x.length\n x.map {|s| s[0]=s[0][0]+s[0][1]}\n elsif x.my_chunk_while {|a,b| a[0][2] == b[0][2]}.to_a.length == x.length\n x.map {|s| s[0]=s[0][0]+s[0][2]}\n else\n @result = \"NO\"\n return\n end\n end}[]\n printResult\n end\n\n def printResult\n if @result == \"NO\"\n @src = []\n @result\n else\n tmp=[]\n @src.each {|x| x.each {|y| tmp<b[1] }\n @src = @src.map {|x| x[0]}\n end\n [@result,@src]\n end\nend\n\nif __FILE__ == $0\n fl = FootballLeague.new\n fl.n = $stdin.readline.to_i\n fl.src = $stdin.readlines\n fl.getResult\n puts fl.result\n puts fl.src.join(\"\\n\") if fl.result == \"YES\"\nend\n\n"}], "src_uid": "733b36bfc2b72200281477af875f8efa"} {"nl": {"description": "Despite his bad reputation, Captain Flint is a friendly person (at least, friendly to animals). Now Captain Flint is searching worthy sailors to join his new crew (solely for peaceful purposes). A sailor is considered as worthy if he can solve Flint's task.Recently, out of blue Captain Flint has been interested in math and even defined a new class of integers. Let's define a positive integer $$$x$$$ as nearly prime if it can be represented as $$$p \\cdot q$$$, where $$$1 < p < q$$$ and $$$p$$$ and $$$q$$$ are prime numbers. For example, integers $$$6$$$ and $$$10$$$ are nearly primes (since $$$2 \\cdot 3 = 6$$$ and $$$2 \\cdot 5 = 10$$$), but integers $$$1$$$, $$$3$$$, $$$4$$$, $$$16$$$, $$$17$$$ or $$$44$$$ are not.Captain Flint guessed an integer $$$n$$$ and asked you: can you represent it as the sum of $$$4$$$ different positive integers where at least $$$3$$$ of them should be nearly prime.Uncle Bogdan easily solved the task and joined the crew. Can you do the same?", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. Next $$$t$$$ lines contain test cases — one per line. The first and only line of each test case contains the single integer $$$n$$$ $$$(1 \\le n \\le 2 \\cdot 10^5)$$$ — the number Flint guessed.", "output_spec": "For each test case print: YES and $$$4$$$ different positive integers such that at least $$$3$$$ of them are nearly prime and their sum is equal to $$$n$$$ (if there are multiple answers print any of them); NO if there is no way to represent $$$n$$$ as the sum of $$$4$$$ different positive integers where at least $$$3$$$ of them are nearly prime. YES NO", "sample_inputs": ["7\n7\n23\n31\n36\n44\n100\n258"], "sample_outputs": ["NO\nNO\nYES\n14 10 6 1\nYES\n5 6 10 15\nYES\n6 7 10 21\nYES\n2 10 33 55\nYES\n10 21 221 6"], "notes": "NoteIn the first and second test cases, it can be proven that there are no four different positive integers such that at least three of them are nearly prime.In the third test case, $$$n=31=2 \\cdot 7 + 2 \\cdot 5 + 2 \\cdot 3 + 1$$$: integers $$$14$$$, $$$10$$$, $$$6$$$ are nearly prime.In the fourth test case, $$$n=36=5 + 2 \\cdot 3 + 2 \\cdot 5 + 3 \\cdot 5$$$: integers $$$6$$$, $$$10$$$, $$$15$$$ are nearly prime.In the fifth test case, $$$n=44=2 \\cdot 3 + 7 + 2 \\cdot 5 + 3 \\cdot 7$$$: integers $$$6$$$, $$$10$$$, $$$21$$$ are nearly prime.In the sixth test case, $$$n=100=2 + 2 \\cdot 5 + 3 \\cdot 11 + 5 \\cdot 11$$$: integers $$$10$$$, $$$33$$$, $$$55$$$ are nearly prime.In the seventh test case, $$$n=258=2 \\cdot 5 + 3 \\cdot 7 + 13 \\cdot 17 + 2 \\cdot 3$$$: integers $$$10$$$, $$$21$$$, $$$221$$$, $$$6$$$ are nearly prime."}, "positive_code": [{"source_code": "t=gets.chomp.to_i\nrep=15\nwhile t!=0\n ar=[6,10,14]\n sum=30\n num=gets.chomp.to_i \n ar.each_with_index do |x,i|\n if (num-sum == x)\n ar[ar.length-1]=rep\n sum+=1\n break\n end\n \n end\nif(num-sum >= 1)\n puts \"YES\"\n ar.each { |x| print \"#{x} \"}\n print \"#{num-sum}\"\n puts\n else\n puts \"NO\"\nend\nt-=1\nend"}, {"source_code": "n = gets.chomp.to_i\nn.times do |i|\n s = gets.chomp\n if s.to_i >= 31 && s != \"36\" && s != \"40\" && s != \"44\" \n puts \"YES\"\n print \"6\"\n print \" \"\n print \"10\"\n print \" \"\n print \"14\"\n print \" \"\n print s.to_i - 30\n print \"\\n\"\n elsif s == \"40\" || s == \"36\" || s == \"44\"\n puts \"YES\"\n print \"6\"\n print \" \"\n print \"10\"\n print \" \"\n print \"15\"\n print \" \"\n print s.to_i - 31\n print \"\\n\"\n else\n puts \"NO\"\n end\nend\n"}, {"source_code": "$, = \" \"\n$\\ = \"\\n\"\ngets.to_i.times do\n n = gets.to_i\n if n <= 30\n puts \"NO\"\n else\n puts \"YES\"\n if n == 36 || n == 40 || n == 44\n print 6, 10, 15, n - 31\n else\n print 6, 10, 14, n - 30\n end\n end\nend"}, {"source_code": "n = gets.to_i\nans = []\nn.times do\n x = gets.to_i\n if x <= 30\n ans << [-1,-1,-1,-1]\n elsif x == 36 || x == 40 || x == 44\n ans << [6,10,15,x-31]\n else\n ans << [6,10,14,x-30]\n end\nend\nans.each do |x|\n if x[0] == -1\n puts \"NO\"\n else\n puts \"YES\"\n puts \"#{x[0]} #{x[1]} #{x[2]} #{x[3]}\"\n end\nend"}], "negative_code": [{"source_code": "t= gets.chomp.to_i\nar=[6,10,14]\nsum = 30\nwhile t!=0\n num= gets.chomp.to_i\nif(num-sum >= 1)\n puts \"YES\"\n ar.each { |x| print \"#{x} \"}\n print \"#{num-sum}\"\n else\n puts \"NO\"\nend\nt-=1\nend"}, {"source_code": "t=gets.chomp.to_i\nar=[6,10,14]\nrep=15\nsum = 30\nwhile t!=0\n num=gets.chomp.to_i \n ar.each_with_index do |x,i|\n if (num-sum == x)\n puts\"in if\"\n ar[ar.length-1]=rep\n sum+=1\n break\n end\n\n end\n p \"sum = #{sum}\"\n ar.each {|x| puts x}\nif(num-sum >= 1)\n puts \"YES\"\n ar.each { |x| print \"#{x} \"}\n print \"#{num-sum}\"\n puts\n else\n puts \"NO\"\nend\nt-=1\nend\n"}, {"source_code": "t= gets.chomp.to_i\nar=[6,10,14]\nsum = 30\nwhile t!=0\n num= gets.chomp.to_i\nif(num-sum >= 1)\n puts \"YES\"\n print \"#{num-sum} \"\n ar.each { |x| print \"#{x} \"}\n else\n puts \"NO\"\nend\nt-=1\nend\n"}, {"source_code": "t=gets.chomp.to_i\n\nrep=15\nsum = 30\nwhile t!=0\n ar=[6,10,14]\n num=gets.chomp.to_i \n ar.each_with_index do |x,i|\n if (num-sum == x)\n ar[ar.length-1]=rep\n sum+=1\n break\n end\n \n end\nif(num-sum >= 1)\n puts \"YES\"\n ar.each { |x| print \"#{x} \"}\n print \"#{num-sum}\"\n puts\n else\n puts \"NO\"\nend\nt-=1\nend"}, {"source_code": "t=gets.chomp.to_i\nar=[6,10,14]\nrep=15\nsum = 30\nwhile t!=0\n num=gets.chomp.to_i \n ar.each_with_index do |x,i|\n if (num-sum == x)\n ar[ar.length-1]=rep\n sum+=1\n break\n end\n \n end\nif(num-sum >= 1)\n puts \"YES\"\n ar.each { |x| print \"#{x} \"}\n print \"#{num-sum}\"\n puts\n else\n puts \"NO\"\nend\nt-=1\nend"}, {"source_code": "t= gets.chomp.to_i\nar=[6,10,14]\nsum = 30\nwhile t!=0\n num= gets.chomp.to_i\nif(num-sum >= 1)\n puts \"YES\"\n ar.each { |x| print \"#{x} \"}\n print \"#{num-sum}\"\n puts\n else\n puts \"NO\"\nend\nt-=1\nend"}, {"source_code": "n = gets.chomp.to_i\nn.times do |i|\n s = gets.chomp.to_i\n if s >= 31\n puts \"YES\"\n print \"6\"\n print \" \"\n print \"10\"\n print \" \"\n print \"14\"\n print \" \"\n print s - 30\n print \"\\n\"\n else\n puts \"NO\"\n end\nend\n"}, {"source_code": "n = gets.chomp.to_i\nn.times do \n s = gets.chomp.to_i\n if n >= 31\n puts \"YES\"\n print \"6\"\n print \" \"\n print \"10\"\n print \" \"\n print \"14\"\n print s - 30\n else\n puts \"NO\"\n end\nend"}, {"source_code": "$, = ' '\ngets.to_i.times do\n n = gets.to_i\n if n <= 30\n puts \"NO\"\n else\n puts \"YES\"\n if n == 36 || n == 40 || n == 44\n print 6, 10, 15, n - 31\n else\n print 6, 10, 14, n - 30\n end\n end\nend"}, {"source_code": "n = gets.to_i\nans = []\nn.times do\n x = gets.to_i\n if x <= 30\n ans << [-1,-1,-1,-1]\n else\n ans << [6,10,14,x-30]\n end\nend\nans.each do |x|\n if x[0] == -1\n puts \"NO\"\n else\n puts \"YES\"\n puts \"#{x[0]} #{x[1]} #{x[2]} #{x[3]}\"\n end\nend"}], "src_uid": "8a6c3436f73286ca54f51fb90017a299"} {"nl": {"description": "Harry Water, Ronaldo, Her-my-oh-knee and their friends have started a new school year at their MDCS School of Speechcraft and Misery. At the time, they are very happy to have seen each other after a long time. The sun is shining, birds are singing, flowers are blooming, and their Potions class teacher, professor Snipe is sulky as usual. Due to his angst fueled by disappointment in his own life, he has given them a lot of homework in Potions class. Each of the n students has been assigned a single task. Some students do certain tasks faster than others. Thus, they want to redistribute the tasks so that each student still does exactly one task, and that all tasks are finished. Each student has their own laziness level, and each task has its own difficulty level. Professor Snipe is trying hard to improve their work ethics, so each student’s laziness level is equal to their task’s difficulty level. Both sets of values are given by the sequence a, where ai represents both the laziness level of the i-th student and the difficulty of his task. The time a student needs to finish a task is equal to the product of their laziness level and the task’s difficulty. They are wondering, what is the minimum possible total time they must spend to finish all tasks if they distribute them in the optimal way. Each person should receive one task and each task should be given to one person. Print the answer modulo 10 007.", "input_spec": "The first line of input contains integer n (1 ≤ n ≤ 100 000) — the number of tasks. The next n lines contain exactly one integer number ai (1 ≤ ai ≤ 100 000) — both the difficulty of the initial task and the laziness of the i-th students.", "output_spec": "Print the minimum total time to finish all tasks modulo 10 007.", "sample_inputs": ["2\n1\n3"], "sample_outputs": ["6"], "notes": "NoteIn the first sample, if the students switch their tasks, they will be able to finish them in 3 + 3 = 6 time units."}, "positive_code": [{"source_code": "n,*a=$<.map &:to_i;a.sort!;r=0;n.times{|i|r=(r+a[i]*a[-i-1])%10007};p r"}, {"source_code": "n = gets.strip.to_i\ntasks = Array.new(n, 0)\nlaziness = Array.new(n, 0)\nn.times do |i|\n a = gets.strip.to_i\n tasks[i] = laziness[i] = a\nend\ntasks.sort!.reverse!\nlaziness.sort!\nans = 0\nn.times do |i|\n ans += tasks[i] * laziness[i]\nend\nans %= 10007\nputs ans\n"}, {"source_code": "n = gets.strip.to_i\narr = []\nn.times do\n arr << gets.strip.to_i\nend\narr.sort!\nsum = 0\nfor i in 0..((n-1)/2) do\n if i != n-1-i\n sum += arr[i] * arr[n-1-i] * 2\n else\n sum += arr[i] * arr[i]\n end\nend\nputs (sum % 10007)"}], "negative_code": [{"source_code": "n,*a=$<.map &:to_i;r=0;n.times{|i|r=(r+a[i]*a[-i-1])%10007};p r"}, {"source_code": "n = gets.strip.to_i\ntasks = Array.new(n, 0)\nlaziness = Array.new(n, 0)\nn.times do |i|\n a = gets.strip.to_i\n tasks[i] = laziness[i] = a\nend\ntasks.sort!.reverse!\nlaziness.sort!\nans = 0\nn.times do |i|\n ans += tasks[i] * laziness[i]\nend\nputs ans\n"}, {"source_code": "n = gets.strip.to_i\narr = []\nn.times do\n arr << gets.strip.to_i\nend\narr.sort!\nsum = 0\nfor i in 0..((n-1)/2) do\n if i != n-1-i\n sum += arr[i] * arr[n-1-i] * 2\n else\n sum += arr[i] * arr[i]\n end\nend\nputs sum"}], "src_uid": "f5acd95067656fa7667210af2c9bec81"} {"nl": {"description": "City X consists of n vertical and n horizontal infinite roads, forming n × n intersections. Roads (both vertical and horizontal) are numbered from 1 to n, and the intersections are indicated by the numbers of the roads that form them.Sand roads have long been recognized out of date, so the decision was made to asphalt them. To do this, a team of workers was hired and a schedule of work was made, according to which the intersections should be asphalted.Road repairs are planned for n2 days. On the i-th day of the team arrives at the i-th intersection in the list and if none of the two roads that form the intersection were already asphalted they asphalt both roads. Otherwise, the team leaves the intersection, without doing anything with the roads.According to the schedule of road works tell in which days at least one road will be asphalted.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 50) — the number of vertical and horizontal roads in the city. Next n2 lines contain the order of intersections in the schedule. The i-th of them contains two numbers hi, vi (1 ≤ hi, vi ≤ n), separated by a space, and meaning that the intersection that goes i-th in the timetable is at the intersection of the hi-th horizontal and vi-th vertical roads. It is guaranteed that all the intersections in the timetable are distinct.", "output_spec": "In the single line print the numbers of the days when road works will be in progress in ascending order. The days are numbered starting from 1.", "sample_inputs": ["2\n1 1\n1 2\n2 1\n2 2", "1\n1 1"], "sample_outputs": ["1 4", "1"], "notes": "NoteIn the sample the brigade acts like that: On the first day the brigade comes to the intersection of the 1-st horizontal and the 1-st vertical road. As none of them has been asphalted, the workers asphalt the 1-st vertical and the 1-st horizontal road; On the second day the brigade of the workers comes to the intersection of the 1-st horizontal and the 2-nd vertical road. The 2-nd vertical road hasn't been asphalted, but as the 1-st horizontal road has been asphalted on the first day, the workers leave and do not asphalt anything; On the third day the brigade of the workers come to the intersection of the 2-nd horizontal and the 1-st vertical road. The 2-nd horizontal road hasn't been asphalted but as the 1-st vertical road has been asphalted on the first day, the workers leave and do not asphalt anything; On the fourth day the brigade come to the intersection formed by the intersection of the 2-nd horizontal and 2-nd vertical road. As none of them has been asphalted, the workers asphalt the 2-nd vertical and the 2-nd horizontal road. "}, "positive_code": [{"source_code": "n = gets.chomp.to_i\nrow = Array.new(n, false)\ncol = Array.new(n, false)\n\nall = n * n\nans = []\nall.times do |day|\n r, c = gets.chomp.split(' ').map(&:to_i)\n next if row[r - 1] || col[c - 1]\n row[r - 1] = true\n col[c - 1] = true\n ans << day + 1\nend\n\nputs ans.join(' ')\n\n"}, {"source_code": "n=gets.to_i\na = []\nb = []\n\nout= []\n(n*n).times do |i|\n\tx,y = gets.split(\" \").map(&:to_i)\n\tif (!a[x] && !b[y])\n\t\tout << i+1\n\t\ta[x] = true\n\t\tb[y] = true\n\tend\nend\n\nputs out.join(\" \")\n"}, {"source_code": "h,v, days = [], [], []\nn = gets.chomp.to_i\n(n**2).times do |t|\n\thi, vi = gets.chomp.split(\" \").map(&:to_i)\n\tif !h.include?(hi) and !v.include?(vi)\n\t\th << hi\n\t\tv << vi\n\t\tdays << (t+1)\n\tend\nend\nputs days.join \" \""}, {"source_code": "n = STDIN.gets.strip.to_i\nhs = Array.new(n+1, false)\nvs = Array.new(n+1, false)\ndays = []\n(n*n).times do |d|\n h, v = STDIN.gets.strip.split(\" \").map{|i| i.to_i}\n next if hs[h] || vs[v]\n hs[h] = vs[v] = true\n days << d + 1\nend\n\nputs days.sort.join(\" \")\n"}, {"source_code": "n = gets.to_i\na = Array.new(n){false}\nb = Array.new(n){false}\n(n*n).times{ |i|\n x, y = gets.split(\" \").map(&:to_i)\n if !a[x] && !b[y]\n print (i+1).to_s + \" \"\n a[x] = true\n b[y] = true\n end\n}"}, {"source_code": "a=gets.chomp.to_i\nh=[]\nv=[]\ntemp=gets.chomp.split(\" \")\nh[0]=temp[0]\nv[0]=temp[1]\nprintf \"1 \"\nif a>1\n2.upto(a*a) do |i| \ntemp=gets.chomp.split(\" \")\nif h.count(\"#{temp[0]}\")==0 && v.count(\"#{temp[1]}\")==0\nh << temp[0]\nv << temp[1]\nprintf \"#{i} \"\nend\nend\nend\n"}, {"source_code": "a=gets.chomp.to_i\nh=[]\nv=[]\ntemp=gets.chomp.split(\" \")\nh[0]=temp[0]\nv[0]=temp[1]\nprintf \"1 \"\nif a>1\n2.upto(a*a) do |i| \ntemp=gets.chomp.split(\" \")\nif h.count(\"#{temp[0]}\")==0 && v.count(\"#{temp[1]}\")==0\nh << temp[0]\nv << temp[1]\nprintf \"#{i} \"\nend\nend\nend"}, {"source_code": "h=(0..50).map{0}\nw=(0..50).map{0}\nn=gets.to_i\nn=n*n\nans=[]\nn.times{|i|\n\th1,w1=gets.split.map{|e| e.to_i}\n\tif h[h1]==0 && w[w1]==0 then\n\t\tans<<(i+1)\n\t\th[h1]=1\n\t\tw[w1]=1\n\tend\n}\nputs ans*\" \"\n\n"}, {"source_code": "n = gets.chomp.to_i\narr = []\nfor i in (0..(n**2)-1)\n\tarr[i] = gets.chomp.split.map(&:to_i)\nend\n\nh = []\nv = []\nh[0] = arr[0][0]\nv[0] = arr[0][1]\n\nprint 1\nprint \" \"\nfor i in (1..(n**2)-1)\n\tif h.include?(arr[i][0]) == false && v.include?(arr[i][1]) == false\n\t\tprint i+1 \n\t\tprint \" \"\n\t\th << arr[i][0]\n\t\tv << arr[i][1]\n\tend\nend"}, {"source_code": "def main\n n = gets.chomp.to_i\n i = n * n\n ints = []\n i.times do\n h, v = gets.chomp.split.map {|r| r.to_i}\n ints << [h, v]\n end\n\n hhash = {}\n vhash = {}\n\n days = []\n ints.each_with_index do |a, i|\n h, v = a\n if not hhash[h] and not vhash[v]\n hhash[h] = :done\n vhash[v] = :done\n days << i+1\n end\n end\n\n days.each do |d|\n print \"#{d} \"\n end\n\n puts \"\"\n\nend\n\nmain\n"}, {"source_code": "n = gets.to_i\nx = []\ny = []\nans = []\n(n * n).times do |a|\n i, j = gets.split.map(&:to_i)\n next if x[i] || y[j]\n x[i] = true\n y[j] = true\n ans << a + 1\nend\nputs ans.join(' ')\n"}, {"source_code": "n = gets.to_i\nx = Array.new(n, false)\ny = Array.new(n, false)\nans = []\n(n * n).times do |a|\n i, j = gets.split.map(&:to_i)\n next if x[i] || y[j]\n x[i] = true\n y[j] = true\n ans << a + 1\nend\nputs ans.join(' ')\n"}, {"source_code": "\nans = [ ]\n\nn = gets.strip.to_i\nh_road = Array.new(n, false)\nv_road = Array.new(n, false)\n\n(n * n).times do |i|\n h, v = gets.strip.split(' ').map(&:to_i)\n h -= 1\n v -= 1\n\n if !h_road[h] and !v_road[v]\n ans.push(i + 1)\n h_road[h] = true\n v_road[v] = true\n end\nend\n\nputs ans.sort.join(' ')"}, {"source_code": "a=gets.chomp.to_i\nh=[]\nv=[]\ntemp=gets.chomp.split(\" \")\nh[0]=temp[0]\nv[0]=temp[1]\nprintf \"1 \"\nif a>1\n2.upto(a*a) do |i| \ntemp=gets.chomp.split(\" \")\nif h.count(\"#{temp[0]}\")==0 && v.count(\"#{temp[1]}\")==0\nh << temp[0]\nv << temp[1]\nprintf \"#{i} \"\nend\nend\nend"}, {"source_code": "#$stdin = File.open('in.txt')\n#$stdout = File.open('out.txt', 'w')\n\nn = gets.to_i\n\nur = []\nuc = []\n\n(1..(n*n)).each { |i|\n r, c = gets.split.map(&:to_i)\n if ur[r] == nil && uc[c] == nil \n ur[r] = 1\n uc[c] = 1\n p i\n end\n}"}], "negative_code": [{"source_code": "a=gets.chomp.to_i\nh=[]\nv=[]\ntemp=gets.chomp.split(\" \")\nh[0]=temp[0]\nv[0]=temp[0]\nprintf \"1 \"\nif a>1\n2.upto(a*a) do |i| \ntemp=gets.chomp.split(\" \")\nif h.count(\"#{temp[0]}\")==0 && v.count(\"#{temp[1]}\")==0\nh << temp[0]\nv << temp[1]\nprintf \"#{i} \"\nend\nend\n\n\nend\n"}, {"source_code": "a=gets.chomp.to_i\nh=[]\nv=[]\ntemp=gets.chomp.split(\" \")\nh[0]=temp[0]\nv[0]=temp[1]\nprintf \"1 \"\nif a>1\n2.upto(a*a) do |i| \nputs \"#{i}\"\n\ntemp=gets.chomp.split(\" \")\nif h.count(\"#{temp[0]}\")==0 && v.count(\"#{temp[1]}\")==0\nh << temp[0]\nv << temp[1]\nprintf \"#{i} \"\nend\nend\n\n\nend\n"}, {"source_code": "n = gets.chomp.to_i\narr = []\nfor i in (0..(n**2)-1)\n\tarr[i] = gets.chomp.split.map(&:to_i)\nend\n\nh = []\nv = []\nh[0] = arr[0][0]\nv[0] = arr[0][1]\n\nprint 1\nprint \" \"\nfor i in (1..(n**2)-1)\n\tif h.include?(arr[i][0]) == false && v.include?(arr[i][1]) == false\n\t\tprint i+1 \n\t\th << arr[i][0]\n\t\tv << arr[i][1]\n\tend\nend"}], "src_uid": "c611808e636d9307e6df9ee0e706310b"} {"nl": {"description": "It seems that Borya is seriously sick. He is going visit n doctors to find out the exact diagnosis. Each of the doctors needs the information about all previous visits, so Borya has to visit them in the prescribed order (i.e. Borya should first visit doctor 1, then doctor 2, then doctor 3 and so on). Borya will get the information about his health from the last doctor.Doctors have a strange working schedule. The doctor i goes to work on the si-th day and works every di day. So, he works on days si, si + di, si + 2di, ....The doctor's appointment takes quite a long time, so Borya can not see more than one doctor per day. What is the minimum time he needs to visit all doctors?", "input_spec": "First line contains an integer n — number of doctors (1 ≤ n ≤ 1000). Next n lines contain two numbers si and di (1 ≤ si, di ≤ 1000).", "output_spec": "Output a single integer — the minimum day at which Borya can visit the last doctor.", "sample_inputs": ["3\n2 2\n1 2\n2 2", "2\n10 1\n6 5"], "sample_outputs": ["4", "11"], "notes": "NoteIn the first sample case, Borya can visit all doctors on days 2, 3 and 4.In the second sample case, Borya can visit all doctors on days 10 and 11."}, "positive_code": [{"source_code": "def nxt(t, s, d)\n tmp = [t - s, 0].max\n tmp = (tmp + d - 1) / d\n return s + tmp * d\nend\n\nn, t = gets.to_i, 0\nfor i in (1..n) do\n t = t + 1\n s, d = gets.split\n t = nxt(t, s.to_i, d.to_i)\nend\nprint t\n"}, {"source_code": "n = gets.to_i\nans = 0\nn.times do |i|\n x, y = gets.split.map(&:to_i)\n tmp = ans - x\n if tmp < 0\n ans = x\n elsif tmp % y == 0\n ans = (tmp / y + 1) * y + x\n else\n ans = (tmp + y - 1) / y * y + x\n end\nend\nputs ans"}, {"source_code": "def antimod(x, m)\n return (m - x % m) % m\nend\nn = gets.to_i\nk = 0\nn.times { |i|\n sd = gets.split().map{|a| a.to_i}\n if i == 0\n k = sd[0]\n else\n if sd[0] > k\n k = sd[0]\n else\n k = sd[0] + ((k-sd[0])/sd[1]+1)*sd[1]\n end\n end\n}\nputs k\n"}, {"source_code": "n = gets.to_i\nk = 0\nn.times { |i|\n sd = gets.split().map{|a| a.to_i}\n if i == 0\n k = sd[0]\n else\n if sd[0] > k\n k = sd[0]\n else\n k = sd[0] + ((k-sd[0])/sd[1]+1)*sd[1]\n end\n end\n}\nputs k\n"}], "negative_code": [{"source_code": "n = gets.to_i\nans = 0\nn.times do |i|\n x, y = gets.split.map(&:to_i)\n tmp = [ans - x, 0].max\n if i == 0\n ans = x \n elsif tmp % y == 0\n ans = (tmp / y + 1) * y + x\n else\n ans = (tmp + y - 1) / y * y + x\n end\nend\nputs ans"}], "src_uid": "d324617c86423d86cdcb839894e00e1a"} {"nl": {"description": "There is a famous olympiad, which has more than a hundred participants. The Olympiad consists of two stages: the elimination stage, and the final stage. At least a hundred participants will advance to the final stage. The elimination stage in turn consists of two contests.A result of the elimination stage is the total score in two contests, but, unfortunately, the jury lost the final standings and has only standings for the first and for the second contest separately. In each contest, the participants are ranked by their point score in non-increasing order. When two participants have a tie (earned the same score), they are ranked by their passport number (in accordance with local regulations, all passport numbers are distinct). In the first contest, the participant on the 100-th place scored $$$a$$$ points. Also, the jury checked all participants from the 1-st to the 100-th place (inclusive) in the first contest and found out that all of them have at least $$$b$$$ points in the second contest.Similarly, for the second contest, the participant on the 100-th place has $$$c$$$ points. And the jury checked that all the participants from the 1-st to the 100-th place (inclusive) have at least $$$d$$$ points in the first contest.After two contests, all participants are ranked by their total score in two contests in non-increasing order. When participants have the same total score, tie-breaking with passport numbers is used. The cutoff score to qualify to the final stage is the total score of the participant on the 100-th place.Given integers $$$a$$$, $$$b$$$, $$$c$$$, $$$d$$$, please help the jury determine the smallest possible value of the cutoff score.", "input_spec": "You need to process $$$t$$$ test cases. The first line contains an integer $$$t$$$ ($$$1 \\leq t \\leq 3025$$$) — the number of test cases. Then descriptions of $$$t$$$ test cases follow. The first line of each test case contains four integers $$$a$$$, $$$b$$$, $$$c$$$, $$$d$$$ ($$$0 \\le a,\\,b,\\,c,\\,d \\le 9$$$; $$$d \\leq a$$$; $$$b \\leq c$$$). One can show that for any test case satisfying the constraints above, there is at least one olympiad scenario possible.", "output_spec": "For each test case print a single integer — the smallest possible cutoff score in some olympiad scenario satisfying the given information.", "sample_inputs": ["2\n1 2 2 1\n4 8 9 2"], "sample_outputs": ["3\n12"], "notes": "NoteFor the first test case, consider the following olympiad scenario: there are $$$101$$$ participants in the elimination stage, each having $$$1$$$ point for the first contest and $$$2$$$ points for the second contest. Hence the total score of the participant on the 100-th place is $$$3$$$.For the second test case, consider the following olympiad scenario: there are $$$50$$$ participants with points $$$5$$$ and $$$9$$$ for the first and second contest respectively; $$$50$$$ participants with points $$$4$$$ and $$$8$$$ for the first and second contest respectively; and $$$50$$$ participants with points $$$2$$$ and $$$9$$$ for the first and second contest respectively. Hence the total point score of the participant on the 100-th place is $$$12$$$."}, "positive_code": [{"source_code": "gets.chomp.to_i.times do |_i|\n a, b, c, d = gets.split.map(&:to_i)\n p [a + b, c + d].max\nend"}, {"source_code": "gets.to_i.times do\n a, b, c, d = gets.split.map &:to_i\n puts [a + b, c + d].max\nend"}], "negative_code": [], "src_uid": "a60321dd9ada279c007415f28775099b"} {"nl": {"description": "Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.", "input_spec": "The first line contains single integer n (1 ≤ n ≤ 105) — the number of columns. The next n lines contain the pairs of integers li and ri (1 ≤ li, ri ≤ 500) — the number of soldiers in the i-th column which start to march from the left or the right leg respectively.", "output_spec": "Print single integer k — the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them.", "sample_inputs": ["3\n5 6\n8 9\n10 3", "2\n6 5\n5 6", "6\n5 9\n1 3\n4 8\n4 5\n23 54\n12 32"], "sample_outputs": ["3", "1", "0"], "notes": "NoteIn the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg — 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg — 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9."}, "positive_code": [{"source_code": "n = gets.to_i\nl = []\nr = []\nsomme = 0\n\nfor i in 1..n\n c = gets.split(' ')\n l[i] = c[0].to_i\n r[i] = c[1].to_i\n somme += ( l[i] - r[i] )\nend\n\nmax = -1\n\npos = 0\n\nfor i in 1..n\n if max < ( somme - 2 * (l[i]-r[i]) ).abs and somme.abs < ( somme - 2 * (l[i]-r[i]) ).abs\n max = ( somme - 2 * (l[i]-r[i]) ).abs\n pos = i\n end\nend\n\nputs pos\n"}, {"source_code": "def tuyet_doi abc\n abc > 0 ? abc : -abc\nend\n\ndef beauty a_left, a_right\n a_left.inject(0){ |product,x| product + x } - a_right.inject(0){ |product,x| product + x }\nend\n\ndef total arr_x\n arr_x.inject(0){ |product,x| product + x }\nend\n\ndef more_beauty? a_left, a_right, n\n now_beauty = tuyet_doi(beauty(a_left, a_right))\n t_left = total(a_left)\n t_right =total(a_right)\n save = -1\n\n (0..n-1).each do |x|\n t_left1 = t_left + a_right[x] - a_left[x]\n t_right1 = t_right - a_right[x] + a_left[x]\n\n if tuyet_doi(t_left1 - t_right1) > now_beauty\n now_beauty = tuyet_doi(t_left1 - t_right1)\n save = x\n end\n end\n\n return save\n # if save == -1\n # return -1\n # else\n # return save\n # end\nend\n\n# input processor\nn = gets.to_i\na_left = Array.new\na_right = Array.new\n(1..n).each do |x|\n a = gets\n jump_list = a.split(\" \")\n a_left << jump_list[0].to_i\n a_right << jump_list[1].to_i\nend\nbeauty1 = more_beauty? a_left, a_right, n\nbeauty2 = more_beauty? a_right, a_left, n\nputs beauty1 > beauty2 ? beauty1 + 1 : beauty2 + 1\n"}, {"source_code": "n = gets.chomp.to_i\nlefts, rights = [], []\nn.times do\n left, right = gets.chomp.split.map(&:to_i)\n lefts << left\n rights << right\nend\nsum_of_lefts, sum_of_rights = lefts.inject(:+), rights.inject(:+)\ndiff = (sum_of_lefts - sum_of_rights).abs\n\narr = []\nlefts.zip(rights).each.with_index.map { |e, idx| [*e, idx] }.each do |left, right, idx|\n c = sum_of_lefts - left + right\n d = sum_of_rights - right + left\n arr << (c - d).abs\nend\n\nans = arr.max\nif diff >= ans\n puts 0\nelse\n puts arr.index(ans) + 1\nend\n"}, {"source_code": "n=gets.chomp.to_i\ni=0\nls=0\nrs=0\nmini=0\nmaxi=0\nmin=500\nmax=-500\nn.times do\n a=gets.chomp.split(\" \")\n l=a[0].to_i\n r=a[1].to_i\n ls=ls+l\n rs=rs+r\n d=l-r\n if(d>max)\n max=d\n maxi=i+1\n end\n if(d beauty\n beauty = tbeauty\n ans = i + 1\n end\nend\nputs ans \n# if lsum > rsum\n# ary.each_with_index do |x, i|\n# if x[1] > x[0] && (x[1]-x[0]) > tmp\n# tmp = (x[1]-x[0])\n# ans = i+1\n# end\n# end\n# elsif lsum == rsum\n# ary.each_with_index do |x, i|\n# if x[0] != x[1] && (x[0]-x[1]).abs > tmp\n# tmp = (x[0]-x[1]).abs\n# ans = i+1\n# end\n# end\n# else\n# ary.each_with_index do |x, i|\n# if x[0] > x[1] && (x[0]-x[1]) > tmp\n# tmp = (x[0]-x[1])\n# ans = i+1\n# end\n# end\n# end"}, {"source_code": "n=gets.to_i\ns1=0\nmax1=[-(10**10),0]\nmin1=[10**10,0]\n\nn.times{|i|\n\ta,b=gets.split.map{|e| e.to_i}\n\td=2*b-2*a\n\ts1+=a-b\n\tif max1[0]d then\n\t\tmin1[0]=d\n\t\tmin1[1]=i+1\n\tend\n}\n\ns2=(s1+min1[0]).abs\ns3=(s1+max1[0]).abs\ns1=s1.abs\n\nif s1>=([s2,s3].max) then\n\tputs 0\nelsif s2>=([s1,s3].max) then\n\tputs min1[1]\nelsif s3>=([s1,s2].max) then\n\tputs max1[1]\nelse\n\tputs 0\nend\n\n"}, {"source_code": "n = gets.chomp.to_i\nls = []\nrs = []\nlt = 0\nrt = 0\nans = 0\nn.times do\n l, r = gets.chomp.split.map(&:to_i)\n ls << l\n rs << r\n lt += l\n rt += r\nend\n\ncur = (lt - rt).abs\nfor i in 0...n\n tmp = ((lt - ls[i] + rs[i]) - (rt - rs[i] + ls[i])).abs\n if cur < tmp\n ans = i + 1\n cur = tmp\n end\nend\nputs ans\n"}, {"source_code": "n = gets.to_i\na = n.times.map{gets.split.map(&:to_i)}\nac = a.map{|x, y| x - y}\nif ac.all?{|x| x < 0} || ac.all?{|x| x > 0}\n p 0\nelse\n cur = ac.inject(:+)\n mx, ind = 0, 0\n n.times do |i|\n c = (cur - 2 * ac[i]).abs\n if c > mx\n ind = i\n mx = c\n end\n end\n p ind + 1\nend\n"}, {"source_code": "l=[]\nr=[]\nn=gets.to_i\nleft=0\nright=0\nfor i in 1..n\n\ta,b=gets.split(\" \").map(&:to_i)\n\tleft+=a\n\tright+=b\n\tl<beauty\n\t\tbeauty=tmp\n\t\tans=i+1\n\tend\nend\nputs ans\n"}, {"source_code": "n = gets.to_i\nmaxldiff = 0\nmaxlpos = 0\nmaxrdiff = 10**9\nmaxrpos = 0\nsuml = 0\nsumr = 0\n1.upto(n) do |i|\n l, r = gets.strip.split(' ').map(&:to_i)\n\n suml += l\n sumr += r\n diff = l - r\n\n if diff > maxldiff\n maxlpos = i\n maxldiff = diff\n end\n\n if maxrdiff > diff\n maxrpos = i\n maxrdiff = diff\n end\nend\n\nmaxrdiff = -maxrdiff\nif suml == sumr\n if maxldiff >= maxrdiff\n printf(\"%d\\n\", maxlpos)\n else\n printf(\"%d\\n\", maxrpos)\n end\nelse\n s1 = suml + maxrdiff\n s2 = sumr - maxrdiff\n\n s3 = suml - maxldiff\n s4 = sumr + maxldiff\n\n if (s1 - s2).abs > (s3 - s4).abs\n printf(\"%d\\n\", maxrpos)\n else\n printf(\"%d\\n\", maxlpos) \n end\nend\n"}, {"source_code": "n = gets.to_i\nlr = (1..n).map{gets.split.map(&:to_i)}\ndiff = lr.map!{|x| x[0]-x[1]}\nno_change = diff.reduce(&:+)\nans = [no_change.abs] + diff.map{|x| (no_change-2*x).abs}\nputs ans.index(ans.max)\n"}], "negative_code": [{"source_code": "def tuyet_doi abc\n abc > 0 ? abc : -abc\nend\n\ndef beauty a_left, a_right\n a_left.inject(0){ |product,x| product + x } - a_right.inject(0){ |product,x| product + x }\nend\n\ndef total arr_x\n arr_x.inject(0){ |product,x| product + x }\nend\n\ndef more_beauty? a_left, a_right, n\n now_beauty = tuyet_doi(beauty(a_left, a_right))\n t_left = total(a_left)\n t_right =total(a_right)\n save = -1\n\n (0..n-1).each do |x|\n t_left1 = t_left + a_right[x] - a_left[x]\n t_right1 = t_right - a_right[x] + a_left[x]\n tuyet_doi(t_left1 - t_right1) > now_beauty\n now_beauty = tuyet_doi(t_left1 - t_right1)\n save = x\n end\n if save == -1\n return 0\n else\n return save\n end\nend\n\n# input processor\nn = gets.to_i\na_left = Array.new\na_right = Array.new\n(1..n).each do |x|\n a = gets\n jump_list = a.split(\" \")\n a_left << jump_list[0].to_i\n a_right << jump_list[1].to_i\nend\nbeauty1 = more_beauty? a_left, a_right, n\nbeauty2 = more_beauty? a_right, a_left, n\nbeauty1 > beauty2 ? beauty1 : beauty2\n"}, {"source_code": "n=gets.to_i\ns1=0\nmax1=[-(10**10),0]\nmin1=[10**10,0]\n\nn.times{|i|\n\ta,b=gets.split.map{|e| e.to_i}\n\td=2*b-2*a\n\ts1+=a-b\n\tif max1[0]d then\n\t\tmin1[0]=d\n\t\tmin1[1]=i+1\n\tend\n}\n\ns2=(s1+min1[0]).abs\ns3=(s1+max1[0]).abs\ns1=s1.abs\nif s1>[s2,s3].max then\n\tputs 0\nelsif s2>[s1,s3].max then\n\tputs min1[1]\nelse\n\tputs max1[1]\nend\n\n\n"}, {"source_code": "n=gets.to_i\ns1=0\nmax1=[-(10**10),0]\nmin1=[10**10,0]\n\nn.times{|i|\n\ta,b=gets.split.map{|e| e.to_i}\n\td=2*b-2*a\n\ts1+=a-b\n\tif max1[0]d then\n\t\tmin1[0]=d\n\t\tmin1[1]=i+1\n\tend\n}\n\ns2=(s1+min1[0]).abs\ns3=(s1+max1[0]).abs\ns1=s1.abs\n\nif s1>([s2,s3].max) then\n\tputs 0\nelsif s2>([s1,s3].max) then\n\tputs min1[1]\nelsif s3>([s1,s2].max) then\n\tputs max1[1]\nelse\n\tputs 0\nend\n\n"}, {"source_code": "n=gets.to_i\ns1=0\nmax1=[-(10**10),0]\nmin1=[10**10,0]\n\nn.times{|i|\n\ta,b=gets.split.map{|e| e.to_i}\n\td=2*b-2*a\n\ts1+=a-b\n\tif max1[0]d then\n\t\tmin1[0]=d\n\t\tmin1[1]=i+1\n\tend\n}\n\nif s1<0 then\n\tif min1[0]<0\n\t\tputs min1[1]\n\telse\n\t\tputs 0\n\tend\nelsif s1>0 then\n\tif max1[0]>0\n\t\tputs max1[1]\n\telse\n\t\tputs 0\n\tend\nelse\n\ta=min1[0].abs\n\tb=max1[0].abs\n\tif a>b then\n\t\tputs min1[1]\n\telsif a r\n lc += l - r\n d[i] = [l - r, 'l', i]\n elsif r > l\n rc += r - l\n d[i] = [r - l, 'r', i]\n else\n d[i] = [0, 'e', i]\n end\nend\nif lc == 0 || rc == 0\n puts 0\nelse\n lmx, rmx = nil, nil\n d.each do |x, y, z|\n if y == 'l' && (lmx.nil? || x > lmx[0])\n lmx = [x, y, z]\n elsif y == 'r' && (rmx.nil? || x > rmx[0])\n rmx = [x, y, z]\n end\n end\n if lc < rc || lmx[0] > rc\n puts lmx[2] + 1\n elsif lc > rc || rmx[0] > lc\n puts rmx[2] + 1\n else\n puts d.sort.last[2] + 1\n end\nend\n"}, {"source_code": "n = gets.to_i\nlr = (1..n).map{gets.split.map(&:to_i)}\ndiff = lr.map!{|x| x[0]-x[1]}\nno_change = diff.reduce(&:+)\nans = [no_change.abs, diff.map{|x| (no_change-2*x).abs}.min].min\nputs ans.inspect\n"}], "src_uid": "2be73aa00a13be5274cf840ecd3befcb"} {"nl": {"description": "Let's define a multiplication operation between a string $$$a$$$ and a positive integer $$$x$$$: $$$a \\cdot x$$$ is the string that is a result of writing $$$x$$$ copies of $$$a$$$ one after another. For example, \"abc\" $$$\\cdot~2~=$$$ \"abcabc\", \"a\" $$$\\cdot~5~=$$$ \"aaaaa\".A string $$$a$$$ is divisible by another string $$$b$$$ if there exists an integer $$$x$$$ such that $$$b \\cdot x = a$$$. For example, \"abababab\" is divisible by \"ab\", but is not divisible by \"ababab\" or \"aa\".LCM of two strings $$$s$$$ and $$$t$$$ (defined as $$$LCM(s, t)$$$) is the shortest non-empty string that is divisible by both $$$s$$$ and $$$t$$$.You are given two strings $$$s$$$ and $$$t$$$. Find $$$LCM(s, t)$$$ or report that it does not exist. It can be shown that if $$$LCM(s, t)$$$ exists, it is unique.", "input_spec": "The first line contains one integer $$$q$$$ ($$$1 \\le q \\le 2000$$$) — the number of test cases. Each test case consists of two lines, containing strings $$$s$$$ and $$$t$$$ ($$$1 \\le |s|, |t| \\le 20$$$). Each character in each of these strings is either 'a' or 'b'.", "output_spec": "For each test case, print $$$LCM(s, t)$$$ if it exists; otherwise, print -1. It can be shown that if $$$LCM(s, t)$$$ exists, it is unique.", "sample_inputs": ["3\nbaba\nba\naa\naaa\naba\nab"], "sample_outputs": ["baba\naaaaaa\n-1"], "notes": "NoteIn the first test case, \"baba\" = \"baba\" $$$\\cdot~1~=$$$ \"ba\" $$$\\cdot~2$$$.In the second test case, \"aaaaaa\" = \"aa\" $$$\\cdot~3~=$$$ \"aaa\" $$$\\cdot~2$$$."}, "positive_code": [{"source_code": "def slcm(s, lcmi)\r\n return s * (lcmi.to_i / s.length) \r\nend\r\n\r\nt = gets.chomp.to_i\r\n\r\nwhile t > 0\r\n s = gets.chomp\r\n v = gets.chomp\r\n lcmi = s.length.lcm(v.length)\r\n lcmv = slcm v , lcmi\r\n lcms = slcm s , lcmi\r\n if lcms == lcmv\r\n puts v * (s.length.lcm(v.length) / v.length)\r\n else\r\n puts \"-1\"\r\n end\r\n t-=1\r\nend"}, {"source_code": "t = gets.chomp.to_i\r\nwhile t > 0\r\n s = gets.chomp\r\n v = gets.chomp\r\n lcmv = v * (s.length.lcm(v.length) / v.length)\r\n lcms = s * (s.length.lcm(v.length) / s.length)\r\n if lcms == lcmv\r\n puts v * (s.length.lcm(v.length) / v.length)\r\n else\r\n puts \"-1\"\r\n end\r\n t-=1\r\nend"}, {"source_code": "def read\r\n gets.chomp\r\nend\r\ngets.to_i.times do\r\n s, t = read, read\r\n n, m = s.size, t.size\r\n l = (n * m) / n.gcd(m)\r\n puts s * (l / n) == t * (l / m) ? s * (l / n) : -1\r\nend"}, {"source_code": "gets.to_i.times do\r\n s, t = gets.chomp, gets.chomp\r\n n, m = s.size, t.size\r\n sz = n * m / n.gcd(m)\r\n puts s * (sz / n) == t * (sz / m) ? s * (sz / n) : -1\r\nend"}, {"source_code": "a = gets.chomp.to_i\n\na.times do\n s1 = gets.chomp\n s2 = gets.chomp\n\n aux = \"\"\n s1.size.times do\n aux += s2\n end\n aux2 = \"\"\n s2.size.times do\n aux2 += s1\n end\n if aux != aux2\n puts -1\n next\n end\n\n lc = s1.size.lcm(s2.size)\n ss = [s1.size, s2.size].min\n\n num = lc/ss\n\n if ss == s2.size\n s1 = s2 \n end\n aux = \"\"\n num.times do\n aux += s1 \n end\n puts aux\nend\n\n\t\t\t \t \t\t \t \t \t \t \t \t\t \t\t\t\t"}, {"source_code": "gets.to_i.times do\r\n s=gets.chomp\r\n t=gets.chomp\r\n sz=s.size\r\n tz=t.size\r\n if s*tz==t*sz\r\n z=sz.lcm(tz)\r\n puts s*(z/sz)\r\n else\r\n puts \"-1\"\r\n end\r\nend\r\n"}], "negative_code": [], "src_uid": "710c7211d23cf8c01fae0b476a889276"} {"nl": {"description": "You are given an integer $$$k$$$ and a string $$$s$$$ that consists only of characters 'a' (a lowercase Latin letter) and '*' (an asterisk).Each asterisk should be replaced with several (from $$$0$$$ to $$$k$$$ inclusive) lowercase Latin letters 'b'. Different asterisk can be replaced with different counts of letter 'b'.The result of the replacement is called a BA-string.Two strings $$$a$$$ and $$$b$$$ are different if they either have different lengths or there exists such a position $$$i$$$ that $$$a_i \\neq b_i$$$.A string $$$a$$$ is lexicographically smaller than a string $$$b$$$ if and only if one of the following holds: $$$a$$$ is a prefix of $$$b$$$, but $$$a \\ne b$$$; in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$b$$$. Now consider all different BA-strings and find the $$$x$$$-th lexicographically smallest of them.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 2000$$$) — the number of testcases. The first line of each testcase contains three integers $$$n$$$, $$$k$$$ and $$$x$$$ ($$$1 \\le n \\le 2000$$$; $$$0 \\le k \\le 2000$$$; $$$1 \\le x \\le 10^{18}$$$). $$$n$$$ is the length of string $$$s$$$. The second line of each testcase is a string $$$s$$$. It consists of $$$n$$$ characters, each of them is either 'a' (a lowercase Latin letter) or '*' (an asterisk). The sum of $$$n$$$ over all testcases doesn't exceed $$$2000$$$. For each testcase $$$x$$$ doesn't exceed the total number of different BA-strings. String $$$s$$$ contains at least one character 'a'.", "output_spec": "For each testcase, print a single string, consisting only of characters 'b' and 'a' (lowercase Latin letters) — the $$$x$$$-th lexicographically smallest BA-string.", "sample_inputs": ["3\n2 4 3\na*\n4 1 3\na**a\n6 3 20\n**a***"], "sample_outputs": ["abb\nabba\nbabbbbbbbbb"], "notes": "NoteIn the first testcase of the example, BA-strings ordered lexicographically are: a ab abb abbb abbbb In the second testcase of the example, BA-strings ordered lexicographically are: aa aba abba Note that string \"aba\" is only counted once, even though there are two ways to replace asterisks with characters 'b' to get it."}, "positive_code": [{"source_code": "gets.to_i.times do\n r=gets.strip.split(' ').map(&:to_i)\n x=r[2]-1\n s=gets.strip.gsub(\"a\",\"ba\").gsub(\"*b\",\"*ab\").split('a').map{|i|(i=='b'? 0:i.size*r[1]+1)}.reverse.map{|i|v='a';x/=((i!=0?(v='b'*(x%i)):false)?i :1);v}.reverse*''\n puts s\nend\n"}, {"source_code": "gets.to_i.times do\n r = gets.strip.split(' ').map(&:to_i)\n x = r[2] - 1\n s = gets.strip.gsub(\"a\", \"ba\").gsub(\"*b\", \"*ab\").split('a').map do |i|\n (i == 'b' ? 0 : i.size * r[1] + 1)\n end.reverse.map do |i|\n if i != 0\n ret = 'b' * (x % i)\n x /= i\n ret\n else\n ret = \"a\"\n end\n end.reverse * ''\n puts s\nend\n"}, {"source_code": "gets.to_i.times do\n r = gets.strip.split(' ').map(&:to_i)\n k = r[1]\n x = r[2] - 1\n s = gets.strip.gsub(\"a\", \"ba\").gsub(\"*b\", \"*ab\")\n s = s.split('a')\n s = s.map do |i|\n v = 0\n if (i != 'b')\n v = i.size\n end\n (i == 'b' ? 0 : v * k + 1)\n end.reverse.map do |i|\n ret = \"\"\n ret = \"a\" if i == 0\n if i != 0\n ret = ret + 'b' * (x % i)\n x /= i\n end\n ret\n end.reverse * ''\n puts s\n\nend\n"}], "negative_code": [{"source_code": "gets.to_i.times do\n a,k,x=gets.strip.split(' ').map(&:to_i);x-=1\n puts gets.strip.split('').chunk{|x|x=='a'}.map{|a,b|a ?[0]*b.size: b.size*k}.flatten.reverse.map{|i|v=(i!=0?'b'*(x%(i+1)) :'a');x/=i+1;v}.reverse*''\nend\n"}, {"source_code": "gets.to_i.times do\n a,k,x=gets.strip.split(' ').map(&:to_i);x-=1\n puts gets.strip.split('').chunk{|x|x=='a'}.map{|a,b|a ?0: b.size*k}.reverse.map{|i|v=(i!=0?'b'*(x%(i+1)) :'a');x/=i+1;v}.reverse*''\nend\n"}, {"source_code": "gets.to_i.times do\n r = gets.strip.split(' ').map(&:to_i)\n n = r[0]\n k = r[1]\n x = r[2] - 1\n s = gets.strip.gsub(\"a\", \"ba\").gsub(\"*b\", \"*ab\")\n s = s.split('a')\n s = s.map do |i|\n v = 0\n if (i != 'b')\n v = i.size\n end\n v * k + 1\n end.reverse.map do |i|\n ret = \"\"\n ret = \"a\" if i == 1\n (x % i).times{ret += 'b'}\n x /= i\n ret\n end.reverse * ''\n puts s\n\nend\n"}, {"source_code": "gets.to_i.times do\n r = gets.strip.split(' ').map(&:to_i)\n n = r[0]\n k = r[1]\n x = r[2]\n s = gets.strip.gsub(\"a\", \"ba\").gsub(\"*b\", \"*ab\")\n s = s.split('a')\n s = s.map do |i|\n v = 0\n if (i != 'b')\n v = i.size\n end\n v * k + 1\n end\n v = 1\n x -= 1\n i = s.size() - 1\n flag = true\n vec = []\n s.each do |ele|\n vec.push(\"-\")\n end\n while flag do\n if s[i] == 1\n i -= 1\n flag = i > -1 && v < x\n else\n vec[i] = [v, v * s[i]]\n v *= s[i]\n i = i - 1\n flag = i > -1 && v < x\n end\n end\n result = \"\"\n (0..(s.size() - 1)).each do |i|\n if vec[i] == \"-\"\n if s[i] == 1\n result += \"a\"\n end\n else\n if x > vec[i][0]\n ctr = x / vec[i][0]\n ctr = [ctr, s[i]].min\n x -= ctr * vec[i][0]\n result += \"b\" * ctr\n end\n end\n end\n puts result\nend\n"}], "src_uid": "32ed4995e557cfdcbef375e2b9edb1a6"} {"nl": {"description": "Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$.A sequence $$$p_1, p_2, \\ldots, p_m$$$, where $$$1 \\leq p_1 < p_2 < \\ldots < p_m \\leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$\\max\\limits_{1 \\le i < m} \\left(p_{i + 1} - p_i\\right)$$$.Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence.", "input_spec": "The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \\leq m \\leq n \\leq 2 \\cdot 10^5$$$) — the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings.", "output_spec": "Output one integer — the maximum width of a beautiful sequence.", "sample_inputs": ["5 3\nabbbc\nabc", "5 2\naaaaa\naa", "5 5\nabcdf\nabcdf", "2 2\nab\nab"], "sample_outputs": ["3", "4", "1", "1"], "notes": "NoteIn the first example there are two beautiful sequences of width $$$3$$$: they are $$$\\{1, 2, 5\\}$$$ and $$$\\{1, 4, 5\\}$$$.In the second example the beautiful sequence with the maximum width is $$$\\{1, 5\\}$$$.In the third example there is exactly one beautiful sequence — it is $$$\\{1, 2, 3, 4, 5\\}$$$.In the fourth example there is exactly one beautiful sequence — it is $$$\\{1, 2\\}$$$."}, "positive_code": [{"source_code": "n,m = gets.chomp.split(\" \").map(&:to_i)\r\ns = gets.chomp.chars\r\nt = gets.chomp.chars\r\ni = 0\r\narr = Array.new(m,-1)\r\nm.times do |j|\r\n while s[i] != t[j]\r\n i += 1\r\n end\r\n arr[j] = i\r\n i += 1\r\nend\r\nmax = 0\r\n(m-1).times do |i|\r\n max = [max,arr[i+1]-arr[i]].max\r\nend\r\ni = n-1\r\n(m-1).downto(1) do |j|\r\n while s[i] != t[j]\r\n i -= 1\r\n end\r\n arr[j] = i\r\n i -= 1\r\n max = [max, arr[j] - arr[j-1]].max\r\nend\r\nputs max"}, {"source_code": "n,m=gets.split.map(&:to_i)\r\ns=gets.chomp.chars\r\nt=gets.chomp.chars\r\nl=[]\r\ni=0\r\nj=0\r\nwhile j=0\r\n if s[i]==t[j]\r\n r << i\r\n j-=1\r\n end\r\n i-=1\r\nend\r\nr.reverse!\r\na=0\r\n(m-1).times do |i|\r\n a=r[i+1]-l[i] if r[i+1]-l[i]>a\r\nend\r\nputs a\r\n"}], "negative_code": [{"source_code": "n,m = gets.chomp.split(\" \").map(&:to_i)\r\ns = gets.chomp.chars\r\nt = gets.chomp.chars\r\ni = 0\r\narr = Array.new(m,-1)\r\nm.times do |j|\r\n while s[i] != t[j]\r\n i += 1\r\n end\r\n arr[j] = i\r\n i += 1\r\nend\r\nmax = 0\r\n(m-1).times do |i|\r\n max = [max,arr[i+1]-arr[i]].max\r\nend\r\ni = n-1\r\n(m-1).downto(1) do |j|\r\n while s[i] != t[j]\r\n i -= 1\r\n end\r\n break if arr[j] == i\r\n arr[j] = i\r\n i -= 1\r\n max = [max, arr[j] - arr[j-1]].max\r\nend\r\nputs max"}], "src_uid": "17fff01f943ad467ceca5dce5b962169"} {"nl": {"description": "A flower shop has got n bouquets, and the i-th bouquet consists of ai flowers. Vasya, the manager of the shop, decided to make large bouquets from these bouquets. Vasya thinks that a bouquet is large if it is made of two or more initial bouquets, and there is a constraint: the total number of flowers in a large bouquet should be odd. Each of the initial bouquets can be a part of at most one large bouquet. If an initial bouquet becomes a part of a large bouquet, all its flowers are included in the large bouquet.Determine the maximum possible number of large bouquets Vasya can make. ", "input_spec": "The first line contains a single positive integer n (1 ≤ n ≤ 105) — the number of initial bouquets. The second line contains a sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the number of flowers in each of the initial bouquets.", "output_spec": "Print the maximum number of large bouquets Vasya can make. ", "sample_inputs": ["5\n2 3 4 2 7", "6\n2 2 6 8 6 12", "3\n11 4 10"], "sample_outputs": ["2", "0", "1"], "notes": "NoteIn the first example Vasya can make 2 large bouquets. For example, the first bouquet can contain the first and the fifth initial bouquets (the total number of flowers is then equal to 9), and the second bouquet can consist of the second and the third initial bouquets (the total number of flowers is then equal to 7). The fourth initial bouquet is unused in this scheme. In the second example it is not possible to form a single bouquet with odd number of flowers.In the third example Vasya can make one large bouquet. For example, he can make it using all three initial bouquets. The size of the large bouquet is then equal to 11 + 4 + 10 = 25."}, "positive_code": [{"source_code": "b = gets.split.map{|_|_.to_i}\na = gets.split.map{|_|_.to_i}\n\neven = 0\nodd = 0\nans = 0\n\nfor x in a\n\tif x % 2 == 0 \n\t\teven = even + 1\n\telse \n\t\todd = odd + 1\n\tend\nend\n\nans = ans + [odd, even].min\nodd = odd - [odd, even].min\nans = ans + odd / 3\n\nprint(ans)"}, {"source_code": "n = gets.to_i\nints = Array.new(n)\nints = gets.split.map{|x| x.to_i }\ne=0\no=0\nfor x in ints\n if x%2 == 0\n e+=1\n else\n o+=1\n end\nend\n\nadd = 0\n\nif o>e\n add = (o-e) / 3\nend\nputs ([o,e].min + add)\n"}, {"source_code": "n = 0\nn = gets\n\na = gets.chomp.split().map { |e| e.to_i }\n\neven = 0\nodd = 0\n\ndef min(a,b)\n if a < b \n a\n else\n b\n end\nend\n \nfor i in 0...a.length\n if a[i] % 2 == 0\n even += 1\n else\n odd += 1\n end\nend\n\nans = min(even,odd)\neven -= ans\nodd -= ans\n\nans += odd / 3\n\nputs \"#{ans}\""}, {"source_code": "n = gets.to_i\na = Array.new(n)\na = gets.split(\" \").collect{|x| x.to_i}\nnumo=0\nnuma=0\nfor i in 0...n\n if (a[i]%2==0)\n numo+=1\n end\nend\nnuma=n-numo\nif (numo>numa)\n ans=numa\nelse\n ans=numo\nend\nnuma-=ans\nif (numa>0)\n ans+=numa/3\nend\nprint ans"}, {"source_code": "n = gets.chomp()\nnums = gets.scan(/\\d+/).map(&:to_i)\nodds = []\nevens = []\ncnt = 0\nnums.each do |x|\n if x % 2 == 0\n evens << x\n else\n odds << x\n end\nend\ncnt += [odds.length, evens.length].min\nel = odds.length - cnt\ncnt += (el / 3) if el >= 3\n# odds.each_with_index do |x, index|\n# unless evens[index].nil?\n# cnt += 1\n# else\n# break\n# end\n# end\nputs cnt"}, {"source_code": "n = gets.to_i\na = gets.split.map{|x| x.to_i }\n\nodd = 0\neven = 0\nfor x in a\n\tif x%2==1\n\t\todd+=1\n\telse\n\t\teven+=1\n\tend\nend\n\nif odd<=even\n\tputs odd\nelse\n\tputs even + (odd-even)/3\nend"}, {"source_code": "gets\nb=0\nc=0\ngets.split.map{|e| \n\te=e.to_i\n\te%2 == 1? b+=1 : c+=1}\n\nif b <= c \n puts b\nelse\n res = c\n b -= c\n res += b / 3\n puts res\nend"}, {"source_code": "r=->{gets.split.map &:to_i}\nn=r[]\na=r[]\nodd = 0\neven = 0\nfor i in a\n if i % 2 == 0\n even = even + 1\n else\n odd = odd + 1\n end\nend\nans = 0\nif odd > even\n ans = even + (odd - even) / 3\nelsif odd < even\n ans = odd\nelse\n ans = even\nend\nputs ans"}, {"source_code": "n=gets.chomp.to_i\ns=gets.chomp.split(' ')\nres=0\nodd=0\neven=0\nfor x in s do\n\todd=odd+(x.to_i % 2)\nend\neven=n-odd\nif (even>=odd) then\n\tres=odd\nelse\n\tres=even+((odd-even)/3)\nend\nputs(res)"}, {"source_code": "r=->{gets.split.map &:to_i}\nn=r[]\na=r[]\nodd = a.count{|_|_%2 == 1}\neven = n[0] - odd\ntk = 0\nif odd > even then\n tk = even\nelse\n tk = odd\nend\nans = tk\nodd -= tk\neven -= tk\np ans + (odd / 3)"}, {"source_code": "r=->{gets.split.map &:to_i}\nn=r[]\na=r[]\nb=a.count{|_|_%2==1}\nm=n.first\nif b >= m-b\n p m-b+(b-m+b)/3\nelse\n p b\nend"}, {"source_code": "n = gets.to_i\na = gets.split.map &:to_i\nodd = a.count{|_|_%2 == 1}\neven = n - odd\nans = [odd,even].min\nodd -= ans\nans += (odd / 3).floor\n\nprint ans"}, {"source_code": "# require 'set'\n\nn = gets.split.map(&:to_i)[0]\na = gets.split.map(&:to_i)\n\neven = a.select {|item|item % 2 == 0}\nn_e = even.length\nn_o = n - n_e\nif n_e >= n_o then\n\tprint n_o\nelse\n\tx = (n_o - n_e) / 3\n\tprint n_e + x\nend\n# print even.length\n\n# if (a.take(l - 1) == b.take(l - 1) && a.drop(r) == b.drop(r) && a1 == b1) then\n# \tputs \"TRUTH\"\n# else\n# \tputs \"LIE\"\n# end"}, {"source_code": "n = gets.to_i\ns = (gets.split)\ns1=0\ns2=0\nfor i in s\n a=i.to_i\n if a%2==0\n s2+=1\n else\n s1+=1\n end\nend\nif s1<=s2\n print s1\nelse\n print s2+(s1-s2)/3\nend"}, {"source_code": "N = gets.to_i\nArr = gets.chomp.split(' ').map(&:to_i)\neven = 0\nodd = 0\nfor i in Arr\n if (i%2==0)\n even = even + 1\n else\n odd = odd + 1\n end\nend\n\nans = 0\nif(even>odd)\n ans = odd\nelse\n ans = even\nend\n\nodd = odd - ans;\n\nans = ans + (odd/3)\n\np ans"}, {"source_code": "n = gets.to_i\na = gets.split.map &:to_i\nnodd = (a.keep_if &:odd?).size\nneven = n - nodd\nans = [nodd, neven].min\nnodd -= ans\nans += (nodd / 3).floor\np ans\n"}, {"source_code": "r = ->{gets.split.map &:to_i}\nn = r[]\na = r[]\ncnt = 0\nfor x in a\n if x % 2 == 1\n cnt += 1\n end\nend\nif cnt < n[0] - cnt\n ans = cnt\nelse\n odd = cnt\n even = n[0] - cnt\n ans = even + ((odd - even) / 3)\nend\np ans\n\n"}, {"source_code": "n = gets.chomp\na = gets.chomp.split(' ')\n\nodd = 0\neven = 0\n\nfor u in a\n if u.to_i % 2 == 1\n odd += 1\n else\n even += 1\n end\nend\n\nif odd == 0\n puts 0\nelsif even == 0\n puts odd / 3\nelse\n if odd > even\n puts even + (odd - even) / 3\n else\n puts odd\n end\nend\n"}, {"source_code": "a = gets.split\n\nzoj = 0\nfard = 0\n\nn = a[0].to_i\nb = gets.split\n\nfor i in (0..n - 1) do\n\tx = b[i].to_i\n\tif (x % 2 == 0)\n\t zoj = zoj + 1\n\telse\n\t fard = fard + 1\n\tend\nend\nmn = zoj\nif (fard < zoj)\n mn = fard\nend\nfard = fard - mn\nezaf = fard / 3\nmn = mn + ezaf\nputs \"#{mn}\"\n"}], "negative_code": [{"source_code": "n = gets.to_i\nints = Array.new(n)\nints = gets.split.map{|x| x.to_i }\ne=0\no=0\nfor x in ints\n if x%2 == 0\n e+=1\n else\n o+=1\n end\nend\n\nadd = 0\n\nif o>e\n add = o-e / 3\nend\nputs ([o,e].min + add)\n"}, {"source_code": "n = gets.to_i\nints = Array.new(n)\nints = gets.split.map{|x| x.to_i }\ne=0\no=0\nfor x in ints\n if x%2 == 0\n e+=1\n else\n o+=1\n end\nend\n\nputs ([o,e].min)\n"}, {"source_code": "n = gets.chomp()\nnums = gets.scan(/\\d+/).map(&:to_i)\nodds = []\nevens = []\ncnt = 0\nnums.each do |x|\n if x % 2 == 0\n evens << x\n else\n odds << x\n end\nend\nodds.each_with_index do |x, index|\n unless evens[index].nil?\n cnt += 1\n else\n break\n end\nend\nputs cnt"}, {"source_code": "n = gets.chomp()\nnums = gets.scan(/\\d+/).map(&:to_i)\nodds = []\nevens = []\ncnt = 0\nnums.each do |x|\n if x % 2 == 0\n evens << x\n else\n odds << x\n end\nend\ncnt = [odds.length, evens.length].min\n# odds.each_with_index do |x, index|\n# unless evens[index].nil?\n# cnt += 1\n# else\n# break\n# end\n# end\nputs cnt"}, {"source_code": "gets\nb=0\nc=0\ngets.split.map{|e| \n\te=e.to_i\n\te%2 == 1? b+=1 : c+=1}\np b"}, {"source_code": "n=gets.chomp.to_i\ns=gets.chomp.split(' ')\nres=0\nodd=0\neven=0\nfor x in s do\n\todd=odd+(x.to_i % 2)\nend\neven=n-odd\nif (even>=odd) then\n\tres=odd\nelse\n\tres=even+(odd-even)/2\nend\nputs(res)"}, {"source_code": "n=gets.chomp.to_i\ns=gets.chomp.split(' ')\nres=0\nfor x in s do\n\tres=res+(x.to_i % 2)\nend\nputs(res)"}, {"source_code": "# require 'set'\n\nn = gets.split.map(&:to_i)[0]\na = gets.split.map(&:to_i)\n\neven = a.select {|item|item % 2 == 0}\nn_e = even.length\nn_o = n - n_e\nprint [n_o, n_e].min\n# print even.length\n\n# if (a.take(l - 1) == b.take(l - 1) && a.drop(r) == b.drop(r) && a1 == b1) then\n# \tputs \"TRUTH\"\n# else\n# \tputs \"LIE\"\n# end"}, {"source_code": "n = gets.to_i\ns = (gets.split)\ns1=0\ns2=0\nfor i in s\n a=i.to_i\n if a%2==0\n s2+=1\n else\n s1+=1\n end\nend\nif s1<=s2\n print s1\nelse\n print s2+s1/3\nend"}, {"source_code": "N = gets.to_i\nArr = gets.chomp.split(' ').map(&:to_i)\neven = 0\nodd = 0\nfor i in Arr\n if (i%2==0)\n even = even + 1\n else\n odd = odd + 1\n end\nend\n\nif(N==1)\n p 0\nelsif(odd==0)\n p 0\nelse\n if(odd%2 == 0)\n p 2\n else\n p 1\n end\nend\n "}, {"source_code": "N = gets.to_i\nArr = gets.chomp.split(' ').map(&:to_i)\neven = 0\nodd = 0\nfor i in Arr\n if (i%2==0)\n even = even + 1\n else\n odd = odd + 1\n end\nend\n\nif(odd==0)\n p 0\nelse\n if(odd%2 == 0)\n p 2\n else\n p 1\n end\nend\n "}, {"source_code": "r = ->{gets.split.map &:to_i}\nn = r[]\na = r[]\ncnt = 0\nfor x in a\n if x % 2 == 1\n cnt += 1\n end\nend\nif cnt > n[0] - cnt\n ans = n[0] - cnt\nelse\n ans = cnt\nend\np ans\n\n"}, {"source_code": "n = gets.chomp\na = gets.chomp.split(' ')\n\nodd = 0\neven = 0\n\nfor u in a\n if u.to_i % 2 == 1\n odd += 1\n else\n even += 1\n end\nend\n\nif odd == 0\n puts 0\nelsif even == 0\n puts odd / 3\nelse\n if odd > even\n puts even\n else\n puts odd\n end\nend\n"}, {"source_code": "a = gets.split\n\nzoj = 0\nfard = 0\n\nn = a[0].to_i\nb = gets.split\n\nfor i in (0..n - 1) do\n\tx = b[i].to_i\n\tif (x % 2 == 0)\n\t zoj = zoj + 1\n\telse\n\t fard = fard + 1\n\tend\nend\nif (fard > zoj)\n zoj = fard\nend\nputs \"#{fard}\"\n"}], "src_uid": "61e6fd68d7568c599204130b102ea389"} {"nl": {"description": "In one school with Vasya there is a student Kostya. Kostya does not like physics, he likes different online games. Every day, having come home, Kostya throws his bag in the farthest corner and sits down at his beloved computer. Kostya even eats glued to the game. A few days ago Kostya bought a new RPG game \"HaresButtle\", which differs from all other games in this genre. It has a huge number of artifacts. As we know, artifacts are divided into basic and composite ones. Only the basic artifacts are available on sale. More powerful composite artifacts are collected from some number of basic artifacts.After the composing composite artifact, all the components disappear.Kostya is the head of the alliance, so he has to remember, what artifacts has not only himself, but also his allies. You must identify by sequence of artifacts purchased by Kostya and his allies, how many and which artifacts has been collected by each of them. It is believed that initially no one has any artifacts. ", "input_spec": "The first line has 4 natural numbers: k (1 ≤ k ≤ 100) — the number of Kostya's allies, n (1 ≤ n ≤ 50) — the number of basic artifacts, m (0 ≤ m ≤ 50) — the number of composite artifacts, q (1 ≤ q ≤ 500) — the number of his friends' purchases. The following n lines contain the names of basic artifacts. After them m lines contain the descriptions of composite artifacts in the following format: <Art. Name>: <Art. №1> <Art. №1 Number>, <Art. №2> <Art. №2 Number>, ... <Art. №X> <Art. №Х Number> All the numbers are natural numbers not exceeding 100 (1 ≤ X ≤ n). The names of all artifacts are different, they are composed of lowercase Latin letters, and the length of each name is from 1 to 100 characters inclusive. All the words in the format of the description of a composite artifact are separated by exactly one space. It is guaranteed that all components of the new artifact are different and have already been met in the input data as the names of basic artifacts. Next, each of the following q lines is characterized by the number ai, the number of a friend who has bought the artifact (1 ≤ ai ≤ k), and the name of the purchased basic artifact. Let's assume that the backpacks of the heroes are infinitely large and any artifact bought later can fit in there. It is guaranteed that after the i-th purchase no more than one opportunity to collect the composite artifact appears. If such an opportunity arose, the hero must take advantage of it.", "output_spec": "The output file should consist of k blocks. The first line should contain number bi — the number of different artifacts the i-th ally has. Then the block should contain bi lines with the names of these artifacts and the number of these artifacts. At that the lines should be printed in accordance with the lexicographical order of the names of the artifacts. In each block all the artifacts must be different, and all the numbers except the bi should be positive.", "sample_inputs": ["2 3 2 5\ndesolator\nrefresher\nperseverance\nvanguard: desolator 1, refresher 1\nmaelstorm: perseverance 2\n1 desolator\n2 perseverance\n1 refresher\n2 desolator\n2 perseverance"], "sample_outputs": ["1\nvanguard 1\n2\ndesolator 1\nmaelstorm 1"], "notes": null}, "positive_code": [{"source_code": "k, n, m, q = gets.split.map(&:to_i)\n\nc = Array.new(k){Hash.new{0}}\n\nn.times do\n\tgets\nend\n\nh = Hash.new\nm.times do\n\tif gets =~ /^(\\w*):(.*)$/\n\t\th[$1] = $2.split(/,/).map{|_| a, b = _.split; [a, b.to_i]}\n\tend\nend\n\nq.times do\n\ta, b = gets.split\n\ta = c[a.to_i - 1]\n\ta[b] += 1\n\th.each do |k, v|\n\t\tnext unless v.all?{|x, y| a[x] >= y}\n\t\tv.each do |x, y|\n\t\t\ta[x] -= y\n\t\tend\n\t\ta[k] += 1\n\tend\nend\n\nc.each do |a|\n\ta.reject!{|k, v| v == 0}\n\tp a.size\n\ta.to_a.sort.each do |k, v|\n\t\tputs \"#{k} #{v}\"\n\tend\nend\n"}, {"source_code": "k, n, m, q = gets.split.map(&:to_i)\n\nc = Array.new(k){Hash.new{0}}\n\nn.times do\n gets\nend\n\nh = Hash.new\nm.times do\n if gets =~ /^(\\w*):(.*)$/\n h[$1] = $2.split(/,/).map{|_| a, b = _.split; [a, b.to_i]}\n end\nend\n\nq.times do\n a, b = gets.split\n a = c[a.to_i - 1]\n a[b] += 1\n h.each do |k, v|\n next unless v.all?{|x, y| a[x] >= y}\n v.each do |x, y|\n a[x] -= y\n end\n a[k] += 1\n end\nend\n\nc.each do |a|\n a.reject!{|k, v| v == 0}\n p a.size\n a.to_a.sort.each do |k, v|\n puts \"#{k} #{v}\"\n end\nend"}], "negative_code": [], "src_uid": "8b28b07a73b4cc3d8d25b2491736064a"} {"nl": {"description": "You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings \"AB\" and \"BA\" (the substrings can go in any order).", "input_spec": "The only line of input contains a string s of length between 1 and 105 consisting of uppercase Latin letters.", "output_spec": "Print \"YES\" (without the quotes), if string s contains two non-overlapping substrings \"AB\" and \"BA\", and \"NO\" otherwise.", "sample_inputs": ["ABA", "BACFAB", "AXBYBXA"], "sample_outputs": ["NO", "YES", "NO"], "notes": "NoteIn the first sample test, despite the fact that there are substrings \"AB\" and \"BA\", their occurrences overlap, so the answer is \"NO\".In the second sample test there are the following occurrences of the substrings: BACFAB.In the third sample test there is no substring \"AB\" nor substring \"BA\"."}, "positive_code": [{"source_code": "#!/usr/bin/env ruby\ns=gets.chomp\nputs ((x=s.index('AB'))&&(y=s.rindex('BA'))&&y-x>1) || ((x=s.index('BA'))&&(y=s.rindex('AB'))&&y-x>1) ? :YES : :NO"}, {"source_code": "# http://codeforces.com/contest/550/problem/A\ndef match?(s, matchers)\n a = s.clone\n matchers.all? do |w|\n b = a.clone\n a.sub!(/#{w}/, '--')\n\n a != b\n end\nend\n\ns = gets.chomp\n\nputs (match?(s, %w(AB BA)) || match?(s, %w(BA AB))) ? 'YES' : 'NO'\n"}, {"source_code": "#!/usr/bin/ruby\ns = gets.chomp\n2.times do\n fab = s.index('AB')\n fba = s.rindex('BA')\n s.reverse!\n s.tr!('AB', 'BA')\n next if fab.nil? || fba.nil?\n next if fab + 1 >= fba\n\n puts \"YES\"\n exit\nend\nputs \"NO\"\n"}, {"source_code": "#!/bin/ruby\n\nrequire 'json'\nrequire 'stringio'\n\ndef twoSubstrings(str)\n\tif str.include?('AB') && str.include?('BA')\n\t\ttmp_str = str.split(\"AB\", 2)\n\t\tif(tmp_str[0].include?('BA') || tmp_str[1].include?('BA'))\n\t\t\treturn true;\n\t\tend\n\t\ttmp_str = str.split(\"BA\", 2)\n\t\tif(tmp_str[0].include?('AB') || tmp_str[1].include?('AB'))\n\t\t\treturn true;\n\t\tend\n\t\treturn false\n\telse\n\t\treturn false\n\tend\nend\n\nstr = gets.rstrip\n\nputs twoSubstrings(str) ? \"YES\" : \"NO\" "}, {"source_code": "#!/bin/ruby\n\nrequire 'json'\nrequire 'stringio'\n\ndef twoSubstrings(str)\n\tif str.include?('AB') && str.include?('BA')\n\t\ttmp_str = str.split(\"AB\", 2)\n\t\treturn true if(tmp_str[0].include?('BA') || tmp_str[1].include?('BA'))\n\t\ttmp_str = str.split(\"BA\", 2)\n\t\treturn true if(tmp_str[0].include?('AB') || tmp_str[1].include?('AB'))\n\t\treturn false\n\telse\n\t\treturn false\n\tend\nend\n\nstr = gets.rstrip\n\nputs twoSubstrings(str) ? \"YES\" : \"NO\" "}, {"source_code": "a=gets.chomp\n\nif a.length>3\n\ti=0\n\tab=0\n\tba=0\n\tabab=0\n\tloop do\n\t\tif a[i..i+2]==\"ABA\" || a[i..i+2]==\"BAB\"\n\t\tabab+=1\n\t\ti+=2\n\t\telsif a[i..i+1]==\"AB\"\n\t\tab+=1\n\t\ti+=1\n\t\telsif a[i..i+1]==\"BA\"\n\t\tba+=1\n\t\ti+=1\n\t\tend\n\t\ti+=1\n\t\tbreak if i==a.length\n\t\tbreak if abab>1\n\t\tbreak if ab>0 && ba>0\n\t\tbreak if (abab>0 && ba>0) || (abab>0 && ab>0)\n\t\t\n\tend\n if ab>0 && ba>0\n\tputs \"YES\"\n\telsif (abab>0 && ba>0) || (abab>0 && ab>0) \n\tputs \"YES\"\n\telsif abab>1\n\tputs \"YES\"\n\telse \n\tputs \"NO\"\n\tend\nelse \n\n\tputs \"NO\"\n\t\nend "}, {"source_code": "# http://codeforces.com/contest/550/problem/A\n\ndef get_input\n gets.chomp\nend\n\ndef result(sub1_found_idxs, sub1_length, sub2_found_idxs, sub2_length)\n return false if sub1_found_idxs.empty? || sub2_found_idxs.empty?\n\n case1 = sub1_found_idxs.first < sub2_found_idxs.last &&\n (sub2_found_idxs.last - sub1_found_idxs.first) >= sub1_length\n case2 = sub1_found_idxs.last > sub2_found_idxs.first &&\n (sub1_found_idxs.last - sub2_found_idxs.first) >= sub2_length\n\n case1 || case2\nend\n\n# Checking str contains both sub1 and sub2 without overlapping in general\ndef contain_substrings?(str, sub1, sub2)\n sub1_length = sub1.size\n sub2_length = sub2.size\n start_idx_sub1 = 0\n start_idx_sub2 = 0\n sub1_found_idxs = []\n sub2_found_idxs = []\n\n 0.upto(str.size - 1) do |current_idx|\n # Find all indexes of sub1 in str\n if str[start_idx_sub1..current_idx] == sub1\n sub1_found_idxs << start_idx_sub1\n start_idx_sub1 = current_idx + 1\n elsif current_idx - start_idx_sub1 >= sub1_length - 1\n start_idx_sub1 += 1\n end\n\n # Find all indexes of sub2 in str\n if str[start_idx_sub2..current_idx] == sub2\n sub2_found_idxs << start_idx_sub2\n start_idx_sub2 = current_idx + 1\n elsif current_idx - start_idx_sub2 >= sub2_length - 1\n start_idx_sub2 += 1\n end\n end\n\n result(sub1_found_idxs, sub1_length, sub2_found_idxs, sub2_length) ? 'YES' : 'NO'\nend\n\n# puts contain_substrings?('ABA', 'AB', 'BA') => NO\n# puts contain_substrings?('BACFAB', 'AB', 'BA') => YES\n# puts contain_substrings?('AXBYBXA', 'AB', 'BA') => NO\n\nputs contain_substrings?(get_input, 'AB', 'BA')"}, {"source_code": "a=gets.chomp\n\nif a.length>3\n\ti=0\n\tab=0\n\tba=0\n\tabab=0\n\tloop do\n\t\tif a[i..i+2]==\"ABA\" || a[i..i+2]==\"BAB\"\n\t\tabab+=1\n\t\ti+=2\n\t\telsif a[i..i+1]==\"AB\"\n\t\tab+=1\n\t\ti+=1\n\t\telsif a[i..i+1]==\"BA\"\n\t\tba+=1\n\t\ti+=1\n\t\tend\n\t\ti+=1\n\t\tbreak if i==a.length\n\t\tbreak if abab>1\n\t\tbreak if ab>0 && ba>0\n\t\tbreak if (abab>0 && ba>0) || (abab>0 && ab>0)\n\t\t\n\tend\n if ab>0 && ba>0\n\tputs \"YES\"\n\telsif (abab>0 && ba>0) || (abab>0 && ab>0) \n\tputs \"YES\"\n\telsif abab>1\n\tputs \"YES\"\n\telse \n\tputs \"NO\"\n\tend\nelse \n\n\tputs \"NO\"\n\t\nend "}, {"source_code": "s=gets.chomp\nx=s.index(\"AB\")\ny=s.rindex(\"BA\")\n\nif x==nil || y==nil then\n\tputs \"NO\"\nelsif (x-y).abs>1 then\n\tputs \"YES\"\nelse\n\tx=s.rindex(\"AB\")\n\ty=s.index(\"BA\")\n\tif x==nil || y==nil then\n\t\tputs \"NO\"\n\telsif (x-y).abs>1 then\n\t\tputs \"YES\"\n\telse\n\t\tputs \"NO\"\n\tend\nend\n\n"}, {"source_code": "strings = gets.strip\n\ndef solve(strings)\n i = 0\n while i < strings.size\n if strings[i..i+1] == 'AB'\n i += 2\n while i < strings.size\n if strings[i..i+1] == 'BA'\n puts 'YES'\n return\n end\n i += 1\n end\n end\n i += 1\n end\n i = 0\n while i < strings.size\n if strings[i..i+1] == 'BA'\n i += 2\n while i < strings.size\n if strings[i..i+1] == 'AB'\n puts 'YES'\n return\n end\n i += 1\n end\n end\n i += 1\n end\n puts 'NO'\nend\n\nsolve(strings)\n"}, {"source_code": "\nstr=gets.chomp\n\nputs ((a=str.index('AB')) and (b=str.rindex('BA')) and b-a>1) || ((a=str.index('BA')) and (b=str.rindex('AB')) and b-a>1) ? 'YES' : 'NO'"}, {"source_code": "s=gets.chomp.to_s\ndef func(s,c,d)\n\ty=s\n\t$~=nil\n\ty=y.sub(/#{c}/,'x')\n\t#puts \"HEY #{$~}\"\n\t#puts y\n\tif $~\n\t\t$~=nil\n\t\ty=y.sub(/#{d}/,'x')\n\t\t#puts \"BYE #{$~}\"\n\t\t#puts y\n\t\tif $~\n\t\t\treturn true\n\t\telse\n\t\t\treturn false\n\t\tend\n\telse\n\t\treturn false\n\tend\nend\n\nif func(s,'AB','BA') || func(s,'BA','AB')\n\tprint(\"YES\\n\")\nelse\n\tputs \"NO\"\nend"}, {"source_code": "str = gets.chomp\n\ndef occurences(str)\n flag = false\n\n str.each_char.each_with_index.inject([]) do |result, (c, index)|\n if c == 'A'\n flag = true\n elsif c == 'B' && flag\n result << index - 1\n flag = false\n else\n flag = false\n end\n\n result\n end\nend\n\narray = occurences(str)\nreverse_array = occurences(str.reverse).map do |item|\n str.size - item - 1\nend.sort\n\nresult = nil\n\narray.each do |item|\n result = reverse_array.find do |other_item|\n (other_item - item > 2) || (other_item < item)\n end\n\n if result\n break\n end\nend\n\nif result.nil? || array.empty? || reverse_array.empty?\n puts 'NO'\nelse\n puts 'YES'\nend\n"}, {"source_code": "s=gets.to_s\nif s.index('AB').nil? || s.index('BA').nil?\n puts :NO\nelse\n puts (s.rindex('AB')-s.index('BA')>1||s.rindex('BA')-s.index('AB')>1)?:YES: :NO\nend"}, {"source_code": "input = gets.chomp\n\ndef val(input)\n ab_index = []\n ba_index = []\n i = 0\n while i < input.length - 1\n ab_index << i if input[i..i + 1] == 'AB'\n i += 1\n end\n\n i = input.length - 1\n while i >= 0\n ba_index << i if input[i..i + 1] == 'BA'\n i -= 1\n end\n\n if !ba_index.first.nil? &&\n !ab_index.first.nil? &&\n (ba_index.first - ab_index.first > 1 ||\n ab_index.last - ba_index.last > 1)\n puts 'YES'\n return\n end\n\n puts 'NO'\nend\n\nval(input)\n"}, {"source_code": "n = gets.chomp.split(\"\")\n\ncount = 0\n\nif n.size > 3\n\tarr = []\n\tarr1 = []\n\tfor i in (0..n.size-1)\n\t\tif n[i] == \"A\" && n[i+1] == \"B\"\n\t\t\tarr << [[i,i+1], \"AB\"]\n\t\tend\n\tend\n\ti = n.size-1\n\twhile i > 0\n\t\tif n[i] == \"A\" && n[i-1] == \"B\"\n\t\t\tarr1 << [[i-1,i], \"BA\"]\n\t\tend\n\t\ti -= 1\n\tend\n\n\tif arr.size > 0 && arr1.size > 0\n\t\tif arr.size <= arr1.size\n\t\t\tfor i in (0..arr.size-1)\n\t\t\t\tfor x in (0..arr1.size-1)\n\t\t\t\t\tif arr[i][0][0] != arr1[x][0][0] && arr[i][0][1] != arr1[x][0][0] && arr[i][0][0] != arr1[x][0][1] && arr[i][0][1] != arr1[x][0][1]\n\t\t\t\t\t\tcount += 1\n\t\t\t\t\t\tbreak\n\t\t\t\t\tend\n\t\t\t\tend\n\t\t\tend\n\t\telse\n\t\t\tfor i in (0..arr1.size-1)\n\t\t\t\tfor x in (0..arr.size-1)\n\t\t\t\t\tif arr1[i][0][0] != arr[x][0][0] && arr1[i][0][1] != arr[x][0][0] && arr1[i][0][0] != arr[x][0][1] && arr1[i][0][1] != arr[x][0][1]\n\t\t\t\t\t\tcount += 1\n\t\t\t\t\t\tbreak\n\t\t\t\t\tend\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\tend\nend\n\nif count > 0\n\tprint 'YES'\nelse\n\tprint 'NO'\nend"}, {"source_code": "input = gets.chomp\ninput_copy = input\n\na = input.index(\"AB\")\nb = input.index(\"BA\")\n\nif a != nil\n input = input[0,a] + \"XX\" + input[a+2,input.length]\n if input.index(\"BA\") != nil\n puts(\"YES\")\n exit(0)\n end\nend\n\ninput = input_copy\n\nif b != nil\n input = input[0,b] + \"XX\" + input[b+2,input.length]\n if input.index(\"AB\") != nil\n puts(\"YES\")\n exit(0)\n end\nend\n\nputs(\"NO\")"}, {"source_code": "def solution(string)\n return \"NO\" if string.index(\"AB\").nil? || string.index(\"BA\").nil?\n return \"YES\" if (string.rindex(\"AB\") - string.index(\"BA\")).abs != 1\n return \"YES\" if (string.rindex(\"BA\") - string.index(\"AB\")).abs != 1\n \"NO\"\nend\nputs solution(gets.strip)\n"}, {"source_code": "s = gets.chomp.chars\nc = [false, false]\nabab = 0\ni = 0\nwhile c.include?(false) && i < s.size\n if (s[i] == 'A' && s[i + 1] == 'B' && s[i + 2] == 'A') ||\n (s[i] == 'B' && s[i + 1] == 'A' && s[i + 2] == 'B')\n abab += 1\n i += 3\n elsif s[i] == 'A' && s[i + 1] == 'B' && !c[0]\n c[0] = true\n i += 2\n elsif s[i] == 'B' && s[i + 1] == 'A' && !c[1]\n c[1] = true\n i += 2\n else\n i += 1\n end\nend\nputs c.all?{|x| x} || c.count(false) <= abab ? 'YES' : 'NO'\n"}, {"source_code": "a=gets.chomp\n\nif a.length>3\n\ti=0\n\tab=0\n\tba=0\n\tabab=0\n\tloop do\n\t\tif a[i..i+2]==\"ABA\" || a[i..i+2]==\"BAB\"\n\t\tabab+=1\n\t\ti+=2\n\t\telsif a[i..i+1]==\"AB\"\n\t\tab+=1\n\t\ti+=1\n\t\telsif a[i..i+1]==\"BA\"\n\t\tba+=1\n\t\ti+=1\n\t\tend\n\t\ti+=1\n\t\tbreak if i==a.length\n\t\tbreak if abab>1\n\t\tbreak if ab>0 && ba>0\n\t\tbreak if (abab>0 && ba>0) || (abab>0 && ab>0)\n\t\t\n\tend\n if ab>0 && ba>0\n\tputs \"YES\"\n\telsif (abab>0 && ba>0) || (abab>0 && ab>0) \n\tputs \"YES\"\n\telsif abab>1\n\tputs \"YES\"\n\telse \n\tputs \"NO\"\n\tend\nelse \n\n\tputs \"NO\"\n\t\nend "}, {"source_code": "# http://codeforces.com/contest/550/problem/A\ns = gets.chomp\n\nn = s.length\nl_ab, l_ba, r_ab, r_ba = 0, 0, n - 1, n - 1\n\nwhile l_ab < n - 1 && r_ba >= 1\n first = s[l_ab] + s[l_ab + 1]\n last = s[r_ba - 1] + s[r_ba]\n\n if first == \"AB\"\n break if last == \"BA\"\n r_ba -= 1\n else\n l_ab += 1\n end\nend\n\nwhile l_ba < n - 1 && r_ab >= 1\n first = s[l_ba] + s[l_ba + 1]\n last = s[r_ab - 1] + s[r_ab]\n\n if first == \"BA\"\n break if last == \"AB\"\n r_ab -= 1\n else\n l_ba += 1\n end\nend\n\nputs (r_ba - l_ab - 1 > 1) || (r_ab - l_ba - 1 > 1) ? \"YES\" : \"NO\"\n"}, {"source_code": "s = gets\nd = s.dup\n\nif s.sub!('AB', 'XX').nil? || d.sub!('BA', 'XX').nil?\n puts 'NO'\nelsif (s && s.sub!('BA', 'XX')) || (d && d.sub!('AB', 'XX'))\n puts 'YES'\nelse\n puts 'NO'\nend\n"}], "negative_code": [{"source_code": "# http://codeforces.com/contest/550/problem/A\ns = gets.chomp\n\nflag = %w(AB BA).all? do |w|\n old = s.clone\n s.gsub!(/#{w}/, '')\n\n (s.length != old.length) && ((old.length - s.length) % 2).even?\nend\n\nputs flag ? 'YES' : 'NO'\n"}, {"source_code": "# http://codeforces.com/contest/550/problem/A\ns = gets.chomp\n\nflag = %w(AB BA).all? do |w|\n old = s.clone\n s.sub!(/#{w}/, '')\n\n (s.length != old.length) && ((old.length - s.length) % 2).even?\nend\n\nputs flag ? 'YES' : 'NO'\n"}, {"source_code": "# http://codeforces.com/contest/550/problem/A\ndef match?(s, matchers)\n a = s.clone\n matchers.all? do |w|\n b = a.clone\n a.sub!(/#{w}/, '')\n\n b.length - a.length == w.length\n end\nend\n\ns = gets.chomp\nputs (match?(s, %w(AB BA)) || match?(s, %w(BA AB))) ? 'YES' : 'NO'\n"}, {"source_code": "# http://codeforces.com/contest/550/problem/A\ns = gets.chomp\n\nflag = %w(AB BA).all? do |w|\n s2 = s.delete(w)\n\n return false if s2.length == s.length\n ((s2.length - s.length) % 2).even?\nend\n\nputs flag ? 'YES' : 'NO'\n"}, {"source_code": "#!/usr/bin/ruby\ns = gets.chomp\n2.times do\n s.reverse!\n fab = s.index('AB')\n fba = s.rindex('BA')\n next if fab.nil? || fba.nil?\n next if fab + 1 >= fba\n\n puts \"YES\"\n exit\nend\nputs \"NO\"\n"}, {"source_code": "#!/bin/ruby\n\nrequire 'json'\nrequire 'stringio'\n\ndef twoSubstrings(str)\n\tif str.include?('AB') && str.include?('BA')\n\t\ttmp_str = str.split(\"AB\", 2)\n\t\treturn false unless (tmp_str[0].include?('BA') || tmp_str[1].include?('BA'))\n\t\ttmp_str = str.split(\"BA\", 2)\n\t\treturn false unless (tmp_str[0].include?('AB') || tmp_str[1].include?('AB'))\n\t\treturn true\n\telse\n\t\treturn false\n\tend\nend\n\nstr = gets.rstrip\n\nputs twoSubstrings(str) ? \"YES\" : \"NO\""}, {"source_code": "#!/bin/ruby\n\nrequire 'json'\nrequire 'stringio'\n\ndef twoSubstrings(str)\n\tif str.include?('AB') && str.include?('BA')\n\t\ttmp_str = str.split(\"AB\", 2)\n\t\treturn false unless (tmp_str[0].include?('BA') || tmp_str[1].include?('BA'))\n\t\ttmp_str = str.split(\"BA\", 2)\n\t\treturn false unless (tmp_str[0].include?('AB') || tmp_str[1].include?('AB'))\n\t\treturn true\n\telse\n\t\treturn false\n\tend\nend\n\nstr = gets.rstrip\n\nputs twoSubstrings str"}, {"source_code": "#!/bin/ruby\n\nrequire 'json'\nrequire 'stringio'\n\ndef twoSubstrings(str)\n\treturn false if str.include?('ABA')\n\tif str.include?('AB') && str.include?('BA')\n\t\ttmp_str = str.split(\"AB\", 2)\n\t\treturn false unless (tmp_str[0].include?('BA') || tmp_str[1].include?('BA'))\n\t\ttmp_str = str.split(\"BA\", 2)\n\t\treturn false unless (tmp_str[0].include?('AB') || tmp_str[1].include?('AB'))\n\t\treturn true\n\telse\n\t\treturn false\n\tend\nend\n\nstr = gets.rstrip\n\nputs twoSubstrings(str) ? \"YES\" : \"NO\" "}, {"source_code": "a=gets.chomp\n\nif a.length>3\n\ti=0\n\tab=0\n\tba=0\n\tabab=0\n\tloop do\n\t\tif a[i..i+2]==\"ABA\" || a[i..i+2]==\"BAB\"\n\t\tabab+=1\n\t\ti+=2\n\t\telsif a[i..i+1]==\"AB\"\n\t\tab+=1\n\t\ti+=1\n\t\telsif a[i..i+1]==\"BA\"\n\t\tba+=1\n\t\ti+=1\n\t\tend\n\t\ti+=1\n\t\tbreak if i==a.length\n\t\tbreak if abab>1\n\t\tbreak if ab>0 && ba>0\n\t\tbreak if (abab>0 && ba>0) || (abab>0 && ab>0)\n\t\t\n\tend\n if ab>0 && ba>0\n\tputs \"YES\"\n\telsif (abab>0 && ba>1) || (abab>0 && ab>1) \n\tputs \"YES\"\n\telse \n\tputs \"NO\"\n\tend\nelse \n\n\tputs \"NO\"\n\t\nend "}, {"source_code": "a=gets.chomp\n\nif a.length>3\n\ti=0\n\tab=0\n\tba=0\n\tabab=0\n\tloop do\n\t\tif a[i..i+2]==\"ABA\" || a[i..i+2]==\"BAB\"\n\t\tabab+=1\n\t\ti+=2\n\t\telsif a[i..i+1]==\"AB\"\n\t\tab+=1\n\t\ti+=1\n\t\telsif a[i..i+1]==\"BA\"\n\t\tba+=1\n\t\ti+=1\n\t\tend\n\t\ti+=1\n\t\tbreak if i==a.length\n\t\tbreak if abab>1\n\t\tbreak if ab>0 && ba>0\n\t\tbreak if (abab>0 && ba>0) || (abab>0 && ab>0)\n\t\t\n\tend\n if ab>0 && ba>0\n\tputs \"YES\"\n\telsif (abab>0 && ba>1) || (abab>0 && ab>1) \n\tputs \"YES\"\n\telsif abab>1\n\tputs \"YES\"\n\telse \n\tputs \"NO\"\n\tend\nelse \n\n\tputs \"NO\"\n\t\nend \n"}, {"source_code": "a=gets.chomp\ni=0\nf=0\np=\"ab\"\nloop do \nif p==\"ab\"\n\n\tif a[i..i+1]==\"AB\" || a[i..i+1]==\"BA\"\n\t\tif a[i..i+1]==\"AB\"\n\t\tp=\"b\"\n\t\telse \n\t\tp=\"a\"\n\t\tend\n\tf+=1\n\ti+=1\n\tend\nelsif p==\"a\"\n\n\tif a[i..i+1]==\"AB\"\n\tf+=1\n\ti+=1\n\tp=\"b\"\n\tend\nelsif p==\"b\"\n\n\tif a[i..i+1]==\"BA\"\n\tf+=1\n\ti+=1\n\tend\nend\n\ti+=1\n\tbreak if i==a.length\nend\n\nif f==2 \nputs \"YES\"\nelse\nputs \"NO\"\nend"}, {"source_code": "a=gets.chomp\n\nif a.length>3\n\ti=0\n\tab=0\n\tba=0\n\tabab=0\n\tloop do\n\t\tif a[i..i+2]==\"ABA\" || a[i..i+2]==\"BAB\"\n\t\tabab+=1\n\t\tend\n\t\tif a[i..i+1]==\"AB\"\n\t\tab+=1\n\t\ti+=1\n\t\telsif a[i..i+1]==\"BA\"\n\t\tba+=1\n\t\ti+=1\n\t\tend\n\t\ti+=1\n\t\tbreak if i==a.length\n\t\tbreak if abab>1\n\t\tbreak if ab>0 && ba>0\n\t\tbreak if (abab>0 && ba>1) || (abab>0 && ab>1)\n\t\t\n\tend\n if ab>0 && ba>0\n\tputs \"YES\"\n\telsif (abab>0 && ba>1) || (abab>0 && ab>1) \n\tputs \"YES\"\n\telse \n\tputs \"NO\"\n\tend\nelse \n\n\tputs \"NO\"\n\t\nend "}, {"source_code": "# http://codeforces.com/contest/550/problem/A\n\ndef get_input\n gets.chomp\nend\n\n# Checking str contains both sub1 and sub2 without overlapping in general\ndef contain_substrings?(str, sub1, sub2)\n sub1_length = sub1.size\n sub2_length = sub2.size\n start_idx_sub1 = 0\n start_idx_sub2 = 0\n sub1_found_idxs = []\n sub2_found_idxs = []\n\n 0.upto(str.size - 1) do |current_idx|\n # Find all indexes of sub1 in str\n if str[start_idx_sub1..current_idx] == sub1\n sub1_found_idxs << start_idx_sub1\n start_idx_sub1 = current_idx + 1\n elsif current_idx - start_idx_sub1 >= sub1_length - 1\n start_idx_sub1 += 1\n end\n\n # Find all indexes of sub2 in str\n if str[start_idx_sub2..current_idx] == sub2\n sub2_found_idxs << start_idx_sub2\n start_idx_sub2 = current_idx + 1\n elsif current_idx - start_idx_sub2 >= sub2_length - 1\n start_idx_sub2 += 1\n end\n end\n\n if sub1_found_idxs.empty? || sub2_found_idxs.empty?\n 'NO'\n elsif sub1_found_idxs.first < sub2_found_idxs.last\n sub2_found_idxs.last - sub1_found_idxs.first >= sub1_length ? 'YES' : 'NO'\n elsif sub1_found_idxs.last > sub2_found_idxs.first\n sub1_found_idxs.last - sub2_found_idxs.first >= sub2_length ? 'YES' : 'NO'\n else\n 'NO'\n end\nend\n\n# puts contain_substrings?('ABA', 'AB', 'BA') => NO\n# puts contain_substrings?('BACFAB', 'AB', 'BA') => YES\n# puts contain_substrings?('AXBYBXA', 'AB', 'BA') => NO\n\nputs contain_substrings?(get_input, 'AB', 'BA')\n"}, {"source_code": "# http://codeforces.com/contest/550/problem/A\n\ndef get_input\n gets.chomp\nend\n\ndef result(sub1_found_idxs, sub1_length, sub2_found_idxs, sub2_length)\n return 'NO' if sub1_found_idxs.empty? || sub2_found_idxs.empty?\n\n case1 = sub1_found_idxs.first < sub2_found_idxs.last &&\n (sub2_found_idxs.last - sub1_found_idxs.first) >= sub1_length\n case2 = sub1_found_idxs.last > sub2_found_idxs.first &&\n (sub1_found_idxs.last - sub2_found_idxs.first) >= sub2_length\n\n case1 || case2\nend\n\n# Checking str contains both sub1 and sub2 without overlapping in general\ndef contain_substrings?(str, sub1, sub2)\n sub1_length = sub1.size\n sub2_length = sub2.size\n start_idx_sub1 = 0\n start_idx_sub2 = 0\n sub1_found_idxs = []\n sub2_found_idxs = []\n\n 0.upto(str.size - 1) do |current_idx|\n # Find all indexes of sub1 in str\n if str[start_idx_sub1..current_idx] == sub1\n sub1_found_idxs << start_idx_sub1\n start_idx_sub1 = current_idx + 1\n elsif current_idx - start_idx_sub1 >= sub1_length - 1\n start_idx_sub1 += 1\n end\n\n # Find all indexes of sub2 in str\n if str[start_idx_sub2..current_idx] == sub2\n sub2_found_idxs << start_idx_sub2\n start_idx_sub2 = current_idx + 1\n elsif current_idx - start_idx_sub2 >= sub2_length - 1\n start_idx_sub2 += 1\n end\n end\n\n result(sub1_found_idxs, sub1_length, sub2_found_idxs, sub2_length) ? 'YES' : 'NO'\nend\n\n# puts contain_substrings?('ABA', 'AB', 'BA') => NO\n# puts contain_substrings?('BACFAB', 'AB', 'BA') => YES\n# puts contain_substrings?('AXBYBXA', 'AB', 'BA') => NO\n\nputs contain_substrings?(get_input, 'AB', 'BA')"}, {"source_code": "s=gets.chomp\nx=s.index(\"AB\")\ny=s.rindex(\"BA\")\nif x==nil || y==nil then\n\tputs \"NO\"\nelsif (x-y).abs>1 then\n\tputs \"YES\"\nelse\n\tputs \"NO\"\nend\n\n"}, {"source_code": "strings = gets.strip\n\nab = nil\nba = nil\n\ni = 0\n\nyes = false\nwhile i < strings.size\n if !ab && strings[i..i+1] == 'AB' && ([i, i+1]&ba.to_a).empty?\n ab = [i, i+1]\n elsif !ba && strings[i..i+1] == 'BA' && ([i, i+1]&ab.to_a).empty?\n ba = [i, i+1]\n end\n\n if ab && ba && (ab&ba).empty?\n yes = true\n break\n end\n\n i += 1\nend\n\nputs yes ? 'YES' : 'NO'\n"}, {"source_code": "strings = gets.strip\n\nab = nil\nba = nil\n\ni = 0\n\nyes = false\nwhile i < strings.size\n if strings[i..i+1] == 'AB' && ([i, i+1]&ba.to_a).empty?\n ab = [i, i+1]\n elsif strings[i..i+1] == 'BA' && ([i, i+1]&ba.to_a).empty?\n ba = [i, i+1]\n end\n\n if ab && ba && (ab&ba).empty?\n yes = true\n break\n end\n\n i += 1\nend\n\nputs yes ? 'YES' : 'NO'\n"}, {"source_code": "strings = gets.strip\n\nab = nil\nba = nil\n\ni = 0\n\nwhile i < strings.size\n ab = [i, i+1] if strings[i..i+1] == 'AB'\n ba = [i, i+1] if strings[i..i+1] == 'BA'\n break if ab && ba\n i += 1\nend\n\nputs ab && ba && (ab&ba).empty? ? 'YES' : 'NO'\n"}, {"source_code": "strings = gets.strip\n\nflags = 0\nstrings.each_char do |c|\n flags ^= 1 << c.ord - 65\nend\n\ncount = 0\nwhile flags != 0\n flags &= flags - 1\n count += 1\nend\n\nputs count > 1 ? 'YES' : 'NO'\n"}, {"source_code": "strings = gets.strip\n\ndef solve(strings)\n i = 0\n while i < strings.size\n if strings[i..i+1] == 'AB'\n i += 2\n while i < strings.size\n if strings[i..i+1] == 'BA'\n puts 'YES'\n return\n end\n i += 1\n end\n elsif strings[i..i+1] == 'BA'\n i += 2\n while i < strings.size\n if strings[i..i+1] == 'AB'\n puts 'YES'\n return\n end\n i += 1\n end\n end\n i += 1\n end\n puts 'NO'\nend\n\nsolve(strings)\n"}, {"source_code": "strings = 'AXBYBXA'\n\nflags = 0\nstrings.each_char do |c|\n flags ^= 1 << c.ord - 65\nend\n\ncount = 0\nwhile flags != 0\n flags &= flags - 1\n count += 1\nend\n\nputs count > 1 ? 'YES' : 'NO'"}, {"source_code": "\nstr=gets.chomp\n\nputs ((a=str.index('AB')) and (b=str.index('BA')) and b-a>1) || ((a=str.index('BA')) and (b=str.rindex('AB')) and b-a>1) ? 'YES' : 'NO'"}, {"source_code": "s=gets.chomp.to_s\ns=s.sub(/AB/,'')\nputs s\nunless $~\n\tputs \"NO\"\nelse\n\ts=s.sub(/BA/,'')\n\tputs s\n\tunless $~\n\t\tputs (\"NO\")\n\telse\n\t\tputs \"YES\"\n\tend\nend\n"}, {"source_code": "s=gets.chomp.to_s\ndef func(s,c,d)\n\ty=s\n\ty=y.sub(/#{c}/,'')\n\tif $~\n\t\ty=y.sub(/#{d}/,'')\n\t\tif $~\n\t\t\treturn true\n\t\telse\n\t\t\treturn false\n\t\tend\n\telse\n\t\treturn false\n\tend\nend\n\t\t\t\nif func(s,'AB','BA') || func(s,'BA','AB')\n\tprint(\"YES\\n\")\nelse\n\tputs \"NO\"\nend"}, {"source_code": "s=gets.chomp.to_s\ns=s.sub(/AB/,'')\n#puts s\nunless $~\n\tputs \"NO\"\nelse\n\ts=s.sub(/BA/,'')\n\t#puts s\n\tunless $~\n\t\tputs (\"NO\")\n\telse\n\t\tputs \"YES\"\n\tend\nend\n"}, {"source_code": "str = gets.chomp\n\nflag = false\narray = []\n\nstr.each_char.each_with_index do |c, index|\n if c == 'A'\n flag = true\n elsif c == 'B' && flag\n array << index - 1\n else\n flag = false\n end\nend\n\nreverse_array = []\nflag = false\nsize = str.size\n\nstr.reverse.each_char.each_with_index do |c, index|\n if c == 'A'\n flag = true\n elsif c == 'B' && flag\n reverse_array << size - index\n else\n flag = false\n end\nend\n\nresult = nil\n\narray.each do |item|\n result = reverse_array.bsearch { |other_item| other_item > item && other_item - item < 3 }\n\n if result\n break\n end\nend\n\nif result || array.empty? || reverse_array.empty?\n puts 'NO'\nelse\n puts 'YES'\nend\n"}, {"source_code": "str = gets.chomp\n\ndef occurences(str)\n flag = false\n\n str.each_char.each_with_index.inject([]) do |result, (c, index)|\n if c == 'A'\n flag = true\n elsif c == 'B' && flag\n result << index - 1\n else\n flag = false\n end\n\n result\n end\nend\n\narray = occurences(str)\nreverse_array = occurences(str.reverse).map do |item|\n str.size - item - 1\nend.sort\n\nresult = nil\n\nputs \"#{array} #{reverse_array}\"\narray.each do |item|\n result = reverse_array.bsearch { |other_item| (other_item - item).abs > 2 }\n\n if result\n break\n end\nend\n\nif result.nil? || array.empty? || reverse_array.empty?\n puts 'NO'\nelse\n puts 'YES'\nend\n"}, {"source_code": "str = gets.chomp\n\ndef occurences(str)\n flag = false\n\n str.each_char.each_with_index.inject([]) do |result, (c, index)|\n if c == 'A'\n flag = true\n elsif c == 'B' && flag\n result << index - 1\n else\n flag = false\n end\n\n result\n end\nend\n\narray = occurences(str)\nreverse_array = occurences(str.reverse).map do |item|\n str.size - item - 1\nend.sort\n\nresult = nil\n\narray.each do |item|\n result = reverse_array.bsearch do |other_item|\n (other_item - item > 2) || (other_item < item)\n end\n\n if result\n break\n end\nend\n\nif result.nil? || array.empty? || reverse_array.empty?\n puts 'NO'\nelse\n puts 'YES'\nend\n"}, {"source_code": "str = gets.chomp\n\ndef occurences(str)\n flag = false\n\n str.each_char.each_with_index.inject([]) do |result, (c, index)|\n if c == 'A'\n flag = true\n elsif c == 'B' && flag\n result << index - 1\n else\n flag = false\n end\n\n result\n end\nend\n\narray = occurences(str)\nreverse_array = occurences(str.reverse).map do |item|\n str.size - item - 1\nend.sort\n\nresult = nil\n\narray.each do |item|\n result = reverse_array.bsearch { |other_item| (other_item - item).abs > 2 }\n\n if result\n break\n end\nend\n\nif result.nil? || array.empty? || reverse_array.empty?\n puts 'NO'\nelse\n puts 'YES'\nend\n"}, {"source_code": "str = gets.chomp\n\ndef occurences(str)\n flag = false\n\n str.each_char.each_with_index.inject([]) do |result, (c, index)|\n if c == 'A'\n flag = true\n elsif c == 'B' && flag\n result << index - 1\n flag = false\n else\n flag = false\n end\n\n result\n end\nend\n\narray = occurences(str)\nreverse_array = occurences(str.reverse).map do |item|\n str.size - item - 1\nend.sort\n\nresult = nil\n\narray.each do |item|\n result = reverse_array.bsearch do |other_item|\n (other_item - item > 2) || (other_item < item)\n end\n\n if result\n break\n end\nend\n\nif result.nil? || array.empty? || reverse_array.empty?\n puts 'NO'\nelse\n puts 'YES'\nend\n"}, {"source_code": "input = 'AXBYBXA'\n\ndef validate(input)\n return false unless input.include?('AB') && input.include?('BA')\n input.each_char.with_index do |_, index|\n break if index > input.length - 2\n if input[index..index + 1] == 'AB' || input[index..index + 1] == 'BA'\n # back and forward word should not overlap\n\n unless input[index..index + 2] == 'ABA' ||\n input[index..index + 2] == 'ABA' ||\n input[index - 1..index + 1] == 'ABA' ||\n input[index - 1..index + 1] == 'ABA'\n return true\n end\n end\n end\n\n true\nend\n\ndef output(input)\n if validate(input)\n puts 'YES'\n else\n puts 'NO'\n end\nend\n\noutput(input)\n"}, {"source_code": "input = gets.chomp\n\ndef validate(input)\n return false unless input.include?('AB') || input.include?('BA')\n input.each_char.with_index do |_, index|\n break if index > input.length - 1\n if input[index..index + 1] == 'AB' || input[index..index + 1] == 'BA'\n # back and forward word should not overlap\n\n unless input[index..index + 2] == 'ABA' ||\n input[index..index + 2] == 'ABA' ||\n input[index - 1..index + 1] == 'ABA' ||\n input[index - 1..index + 1] == 'ABA'\n return true\n end\n end\n end\n\n true\nend\n\ndef output(input)\n if validate(input)\n puts 'YES'\n else\n puts 'NO'\n end\nend\n\noutput(input)\n"}, {"source_code": "input = gets.chomp\n\ndef validate(input)\n return false unless input.include?('AB') || input.include?('BA')\n input.each_char.with_index do |_, index|\n break if index > input.length - 1\n if input[index..index + 1] == 'AB' || input[index..index + 1] == 'BA'\n # back and forward word should not overlap\n\n unless input[index..index + 2] == 'ABA' ||\n input[index..index + 2] == 'ABA' ||\n input[index - 1..index + 1] == 'ABA' ||\n input[index - 1..index + 1] == 'ABA'\n return true\n end\n end\n end\n\n false\nend\n\ndef output(input)\n if validate(input)\n puts 'YES'\n else\n puts 'NO'\n end\nend\n\noutput(input)\n"}, {"source_code": "def read\n #gets.chomp\n 'ABAAB'\nend\n\ndef validate\n input = read\n return false unless input.include?('AB') || input.include?('BA')\n input.each_char.with_index do |_, index|\n break if index > input.length - 3\n if input[index..index + 2] == 'ABA' || input[index..index + 2] == 'BAB'\n return false\n end\n end\n\n true\nend\n\ndef output\n if validate\n puts 'YES'\n else\n puts 'NO'\n end\nend\n\noutput\n"}, {"source_code": "input = gets.chomp\n\ndef validate(input)\n return false unless input.include?('AB') || input.include?('BA')\n input.each_char.with_index do |_, index|\n break if index > input.length - 1\n if input[index..index + 1] == 'AB' || input[index..index + 1] == 'BA'\n # back and forward word should not overlap\n\n puts input[index..index + 1]\n unless input[index..index + 2] == 'ABA' ||\n input[index..index + 2] == 'ABA' ||\n input[index - 1..index + 1] == 'ABA' ||\n input[index - 1..index + 1] == 'ABA'\n return true\n end\n end\n end\n\n false\nend\n\ndef output(input)\n if validate(input)\n puts 'YES'\n else\n puts 'NO'\n end\nend\n\noutput(input)\n"}, {"source_code": "def read\n gets.chomp\nend\n\ndef validate\n input = read\n return false unless input.include?('AB') || input.include?('BA')\n input.each_char.with_index do |_, index|\n break if index > input.length - 3\n if input[index..index + 3] == 'ABA' || input[index..index + 3] == 'BAB'\n return false\n end\n end\n\n true\nend\n\ndef output\n if validate\n puts 'YES'\n else\n puts 'NO'\n end\nend\n\noutput\n"}, {"source_code": "input = gets.chomp\n\ndef val(input)\n ab_index = []\n ba_index = []\n i = 0\n while i < input.length - 1\n ab_index << i if input[i..i + 1] == 'AB'\n i += 1\n end\n\n i = input.length - 1\n while i >= 0\n ba_index << i if input[i..i + 1] == 'BA'\n i -= 1\n end\n\n if !ba_index.first.nil? &&\n !ab_index.first.nil? &&\n (ba_index.first - ab_index.first > 1 ||\n ab_index.first - ba_index.first > 1)\n puts 'YES'\n return\n end\n\n puts 'NO'\nend\n\nval(input)\n"}, {"source_code": "input = gets.chomp\n\ndef val(input)\n ab_index = []\n ba_index = []\n i = 0\n while i < input.length - 1\n ab_index << i if input[i..i + 1] == 'AB'\n i += 1\n end\n\n i = input.length - 1\n while i >= 0\n ba_index << i if input[i..i + 1] == 'BA'\n i -= 1\n end\n\n if !ba_index.last.nil? &&\n !ab_index.first.nil? &&\n (ba_index.last - ab_index.first > 1 ||\n ab_index.first - ba_index.last > 1)\n puts 'YES'\n return\n end\n\n puts 'NO'\nend\n\nval(input)\n"}, {"source_code": "input = 'ABAAHBA'\n\ndef validate(input)\n return false unless input.include?('AB') && input.include?('BA')\n input.each_char.with_index do |_, index|\n break if index > input.length - 2\n if input[index..index + 1] == 'AB' || input[index..index + 1] == 'BA'\n # back and forward word should not overlap\n\n unless input[index..index + 2] == 'ABA' ||\n input[index..index + 2] == 'ABA' ||\n input[index - 1..index + 1] == 'ABA' ||\n input[index - 1..index + 1] == 'ABA'\n return true\n end\n end\n end\n\n false\nend\n\ndef output(input)\n if validate(input)\n puts 'YES'\n else\n puts 'NO'\n end\nend\n\noutput(input)\n"}, {"source_code": "input = gets.chomp\n\ndef validate(input)\n return false unless input.include?('AB') && input.include?('BA')\n input.each_char.with_index do |_, index|\n break if index > input.length - 3\n if input[index..index + 2] == 'ABA' || input[index..index + 2] == 'BAB'\n return false\n end\n end\n\n true\nend\n\ndef output(input)\n if validate(input)\n puts 'YES'\n else\n puts 'NO'\n end\nend\n\noutput(input)\n"}, {"source_code": "n = gets.chomp.split(\"\")\n\nab = 0\nba = 0\n\nwhile n.size > 1 || (ab > 0 && ba > 0) \n\tif n.index(\"A\") != nil && n.index(\"B\") != nil && n.index(\"A\") == n.index(\"B\")-1 && n.size >= 2\n\t\tab += 1\n\t\tn.delete_at(n.index(\"A\"))\n\t\tn.delete_at(n.index(\"B\"))\n\telsif n.index(\"A\") != nil && n.index(\"B\") != nil && n.index(\"B\") == n.index(\"A\")-1 && n.size >= 2\n\t\tba += 1\n\t\tn.delete_at(n.index(\"B\"))\n\t\tn.delete_at(n.index(\"A\"))\n\telse\n\t\tbreak\n\tend\nend\nif ab > 0 && ba > 0\n\tprint \"YES\"\nelse\n\tprint \"NO\"\nend"}, {"source_code": "n = gets.chomp.split(\"\")\n\ncount = 0\n\nif n.size > 3\n\tarr = []\n\tarr1 = []\n\tfor i in (0..n.size-1)\n\t\tif n[i] == \"A\" && n[i+1] == \"B\"\n\t\t\tarr << [i+1, \"AB\"]\n\t\tend\n\tend\n\ti = n.size-1\n\twhile i > 1\n\t\tif n[i] == \"A\" && n[i-1] == \"B\"\n\t\t\tarr1 << [i-1, \"BA\"]\n\t\tend\n\t\ti -= 1\n\tend\n\t\n\tif arr.size > 0 && arr1.size > 0\n\t\tif arr.size <= arr1.size\n\t\t\tfor i in (0..arr.size-1)\n\t\t\t\tfor x in (0..arr1.size-1)\n\t\t\t\t\tif arr[i][0] != arr1[x][0]\n\t\t\t\t\t\tcount += 1\n\t\t\t\t\t\tbreak\n\t\t\t\t\tend\n\t\t\t\tend\n\t\t\tend\n\t\telse\n\t\t\tfor i in (0..arr1.size-1)\n\t\t\t\tfor x in (0..arr.size-1)\n\t\t\t\t\tif arr1[i][0] != arr[x][0]\n\t\t\t\t\t\tcount += 1\n\t\t\t\t\t\tbreak\n\t\t\t\t\tend\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\tend\nend\n\nif count > 0\n\tprint 'YES'\nelse\n\tprint 'NO'\nend"}, {"source_code": "n = gets.chomp.split(\"\")\n\ncount = 0\n\nif n.size > 3\n\tarr = []\n\tarr1 = []\n\tfor i in (0..n.size-1)\n\t\tif n[i] == \"A\" && n[i+1] == \"B\"\n\t\t\tarr << [i+1, \"AB\"]\n\t\tend\n\tend\n\ti = n.size-1\n\twhile i > 0\n\t\tif n[i] == \"A\" && n[i-1] == \"B\"\n\t\t\tarr1 << [i, \"BA\"]\n\t\tend\n\t\ti -= 1\n\tend\n\t\n\tif arr.size > 0 && arr1.size > 0\n\t\tfor i in (0..arr.size-1)\n\t\t\tif arr[i][0] != arr1[i][0]\n\t\t\t\tcount += 1\n\t\t\t\tbreak\n\t\t\tend\n\t\tend\n\tend\nend\n\nif count > 0\n\tprint 'YES'\nelse\n\tprint 'NO'\nend"}, {"source_code": "n = gets.chomp.split(\"\")\n\ncount = 0\n\nif n.size > 3\n\tarr = []\n\tarr1 = []\n\tfor i in (0..n.size-1)\n\t\tif n[i] == \"A\" && n[i+1] == \"B\"\n\t\t\tarr << [i+1, \"AB\"]\n\t\tend\n\tend\n\ti = n.size-1\n\twhile i > 0\n\t\tif n[i] == \"A\" && n[i-1] == \"B\"\n\t\t\tarr1 << [i-1, \"BA\"]\n\t\tend\n\t\ti -= 1\n\tend\n\n\tif arr.size > 0 && arr1.size > 0\n\t\tif arr.size <= arr1.size\n\t\t\tfor i in (0..arr.size-1)\n\t\t\t\tfor x in (0..arr1.size-1)\n\t\t\t\t\tif arr[i][0] != arr1[x][0]\n\t\t\t\t\t\tcount += 1\n\t\t\t\t\t\tbreak\n\t\t\t\t\tend\n\t\t\t\tend\n\t\t\tend\n\t\telse\n\t\t\tfor i in (0..arr1.size-1)\n\t\t\t\tfor x in (0..arr.size-1)\n\t\t\t\t\tif arr1[i][0] != arr[x][0]\n\t\t\t\t\t\tcount += 1\n\t\t\t\t\t\tbreak\n\t\t\t\t\tend\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\tend\nend\n\nif count > 0\n\tprint 'YES'\nelse\n\tprint 'NO'\nend"}, {"source_code": "input = gets.chomp\ninput_copy = input\n\na = input.index(\"AB\")\nb = input.index(\"BA\")\n\nif a != nil\n input = input[0,a] + input[a+2,input.length]\n if input.index(\"BA\") != nil\n puts(\"YES\")\n exit(0)\n end\nend\n\ninput = input_copy\n\nif b != nil\n input = input[0,b] + input[b+2,input.length]\n if input.index(\"AB\") != nil\n puts(\"YES\")\n exit(0)\n end\nend\n\nputs(\"NO\")"}, {"source_code": "def solution(string)\n return \"NO\" if string.index(\"AB\").nil? || string.index(\"BA\").nil?\n return \"YES\" if (string.rindex(\"AB\") - string.index(\"BA\")).abs != 1\n \"NO\"\nend\nputs solution(gets.strip)\n"}, {"source_code": "s = gets.chomp.chars\nc = [false, false]\ni = 0\nwhile c.include?(false) && i < s.size\n if s[i] == 'A' && s[i + 1] == 'B' && !c[0]\n c[0] = true\n i += 2\n elsif s[i] == 'B' && s[i + 1] == 'A' && !c[1]\n c[1] = true\n i += 2\n else\n i += 1\n end\nend\nputs c.all?{|x| x} ? 'YES' : 'NO'\n"}, {"source_code": "s = gets.chomp\nputs s.include?('AB') && s.include?('BA') && !s.include?('ABA') && !s.include?('BAB') ? 'YES' : 'NO'\n"}, {"source_code": "s = gets.chomp.chars\nc = [false, false]\ni = 0\nwhile c.include?(false) && i < s.size\n if s[i] == 'A' && s[i + 1] == 'B'\n c[0] = true\n i += 2\n elsif s[i] == 'B' && s[i + 1] == 'A'\n c[1] = true\n i += 2\n else\n i += 1\n end\nend\nputs c.all?{|x| x} ? 'YES' : 'NO'\n"}, {"source_code": "s = gets.chomp\nputs s.slice!('AB') && s.slice!('BA') ? 'YES' : 'NO'\n"}, {"source_code": "s = gets.chomp\nputs (s.slice!('AB') && s.slice!('BA')) || (s.slice!('BA') && s.slice!('AB')) ? 'YES' : 'NO'\n"}, {"source_code": "# http://codeforces.com/contest/550/problem/A\n\ns = gets.chomp\n\nn = s.length\nl, r = 0, n - 1\n\nwhile l < n - 1\n first = s[l] + s[l + 1]\n if first == \"AB\" || first == \"BA\"\n break l\n else\n l += 1\n end\nend\n\nwhile r >= 1\n last = s[r] + s[r - 1]\n if last == \"AB\" || last == \"BA\"\n break r -= 1\n else\n r -= 1\n end\nend\n\nputs r - l > 1 ? \"YES\" : \"NO\"\n"}, {"source_code": "# http://codeforces.com/contest/550/problem/A\ns = gets.chomp\n\nn = s.length\nl, r = 0, n - 1\n\nwhile l < n - 1 && r >= 1\n first = s[l] + s[l + 1]\n last = s[r - 1] + s[r]\n if first == \"AB\"\n break if last == \"BA\"\n r -= 1\n elsif first == \"BA\"\n break if last == \"AB\"\n r -= 1\n else\n l += 1\n end\nend\n\nputs r - l - 1 > 1 ? \"YES\" : \"NO\"\n"}, {"source_code": "s = gets\nd = s.dup\n\nif s.sub!('AB', '').nil? || d.sub!('BA', '').nil?\n puts 'NO'\nelsif (s && s.sub!('BA', '')) || (d && d.sub!('AB', ''))\n puts 'YES'\nelse\n puts 'NO'\nend\n"}, {"source_code": "begin\n throw 'Error' if gets.sub!('AB', '').sub!('BA', '').nil?\n puts 'YES'\nrescue\n puts 'NO'\nend\n"}], "src_uid": "33f7c85e47bd6c83ab694a834fa728a2"} {"nl": {"description": "Paladin Manao caught the trail of the ancient Book of Evil in a swampy area. This area contains n settlements numbered from 1 to n. Moving through the swamp is very difficult, so people tramped exactly n - 1 paths. Each of these paths connects some pair of settlements and is bidirectional. Moreover, it is possible to reach any settlement from any other one by traversing one or several paths.The distance between two settlements is the minimum number of paths that have to be crossed to get from one settlement to the other one. Manao knows that the Book of Evil has got a damage range d. This means that if the Book of Evil is located in some settlement, its damage (for example, emergence of ghosts and werewolves) affects other settlements at distance d or less from the settlement where the Book resides.Manao has heard of m settlements affected by the Book of Evil. Their numbers are p1, p2, ..., pm. Note that the Book may be affecting other settlements as well, but this has not been detected yet. Manao wants to determine which settlements may contain the Book. Help him with this difficult task.", "input_spec": "The first line contains three space-separated integers n, m and d (1 ≤ m ≤ n ≤ 100000; 0 ≤ d ≤ n - 1). The second line contains m distinct space-separated integers p1, p2, ..., pm (1 ≤ pi ≤ n). Then n - 1 lines follow, each line describes a path made in the area. A path is described by a pair of space-separated integers ai and bi representing the ends of this path.", "output_spec": "Print a single number — the number of settlements that may contain the Book of Evil. It is possible that Manao received some controversial information and there is no settlement that may contain the Book. In such case, print 0.", "sample_inputs": ["6 2 3\n1 2\n1 5\n2 3\n3 4\n4 5\n5 6"], "sample_outputs": ["3"], "notes": "NoteSample 1. The damage range of the Book of Evil equals 3 and its effects have been noticed in settlements 1 and 2. Thus, it can be in settlements 3, 4 or 5. "}, "positive_code": [{"source_code": "@n, m, d = gets.chomp.split(' ').map(&:to_i)\n\n@d_down = Array.new(@n+1) { -1 }\n@d_up = Array.new(@n+1) { -1 }\n\n@marked = Array.new(@n+1)\n@marked_points = gets.chomp.split(' ').map(&:to_i)\n@marked_points.each do |m| \n\t@marked[m] = true \n\t@d_down[m] = 0\nend\n\n@d_up[1] = @marked[1] ? 0 : -1\n\n@map = Array.new(@n+1) {|i| []}\n(2..@n).each do \n\ta, b = gets.chomp.split(' ').map(&:to_i)\n\t@map[a] << b\n\t@map[b] << a\nend\n\n@way_down = Array.new(@n+1) {|i| []}\n@way_up = Array.new(@n+1) {|i| []}\n@traverse = []\n\n@deep_points = []\n\ngo = [1]\nwhile go.size > 0\n\tnew_go = []\t\n\tgo.each do |from|\n\t\t@traverse << from\n\t\tadded = false\n\t\t@map[from].each do |to|\n\t\t\tnext if @way_up[from].include? to\n\t\t\t@way_down[from] << to\n\t\t\t@way_up[to] << from\n\t\t\tnew_go << to\n\t\t\tadded = true\n\t\tend\t\t\n\t\t@deep_points << from unless added\n\tend\n\tgo = new_go\nend\n\n#p @way_down\n#p @way_up\n#p @deep_points\n\ndef dist_down_alt2\n\t(@traverse.size-1).downto(0).each do |i|\n\t\tparent = @traverse[i]\n\t\t@way_down[parent].each do |child|\n\t\t\tif @d_down[child] != -1\n\t\t\t\ttmp = @d_down[child] + 1\n\t\t\t\t@d_down[parent] = tmp if tmp > @d_down[parent]\n\t\t\tend\n\t\tend\n\tend\nend\n\ndef dist_up\n\ttarget = [1]\n\tvisited = Array.new(@n+1) { false }\n\n\twhile target.size > 0\n\t\tnew_target = []\n\t\ttarget.each do |to|\n\t\t\tnext if visited[to]\n\t\t\tvisited[to] = true\n\n\t\t\tmax1 = -1\n\t\t\tmax2 = -1\n\t\t\t@way_down[to].each do |child|\n\t\t\t\tif @d_down[child] > max1\n\t\t\t\t\tmax2 = max1\n\t\t\t\t\tmax1 = @d_down[child]\n\t\t\t\telsif @d_down[child] > max2\n\t\t\t\t\tmax2 = @d_down[child]\n\t\t\t\tend\n\t\t\tend\n\n\t\t\t@way_down[to].each do |child|\t\n\t\t\t\tsibling = @d_down[child] == max1 ? max2 : max1\n\t\t\t\t@d_up[child] = sibling + 2 if sibling != -1\n\n\t\t\t\tif @d_up[to] != -1 \n\t\t\t\t\ttmp = @d_up[to] + 1\n\t\t\t\t\t@d_up[child] = tmp if tmp > @d_up[child]\n\t\t\t\tend\n\n\t\t\t\tif @marked[child]\n\t\t\t\t\t@d_up[child] = 0 if @d_up[child] < 0\n\t\t\t\tend\n\t\t\t\t\n\t\t\t\tnew_target << child\n\t\t\t\t#dist_up(child, to, @d_up, @d_down)\t\t\n\t\t\tend\n\t\tend\n\t\ttarget = new_target\n\tend\nend\n\n#dist_down\ndist_down_alt2\ndist_up\n\nsum = 0\n(1..@n).each do |i|\n\tsum += 1 if @d_down[i] <= d and @d_up[i] <= d\nend\np sum"}, {"source_code": "@n, m, @d = gets.chomp.split(' ').map(&:to_i)\n\n@d_down = Array.new(@n+1) { -1 }\n@d_up = Array.new(@n+1) { -1 }\n\n@marked = Array.new(@n+1)\ngets.chomp.split(' ').map(&:to_i).each do |m| \n\t@marked[m] = true\n\t@d_down[m] = 0\nend\n\n@d_up[1] = @marked[1] ? 0 : -1\n\n@map = Array.new(@n+1) {|i| []}\n(2..@n).each do \n\ta, b = gets.chomp.split(' ').map(&:to_i)\n\t@map[a] << b\n\t@map[b] << a\nend\n\n@way_down = Array.new(@n+1) {|i| []}\n@way_up = Array.new(@n+1) {|i| []}\n@traverse = []\n\ngo = [1]\nwhile go.size > 0\n\tnew_go = []\t\n\tgo.each do |from|\n\t\t@traverse << from\n\t\t@map[from].each do |to|\n\t\t\tnext if @way_up[from].include? to\n\t\t\t@way_down[from] << to\n\t\t\t@way_up[to] << from\n\t\t\tnew_go << to\n\t\tend\t\t\n\tend\n\tgo = new_go\nend\n\ndef dist_down\n\t(@traverse.size-1).downto(0).each do |i|\n\t\tparent = @traverse[i]\n\t\t@way_down[parent].each do |child|\n\t\t\tif @d_down[child] != -1\n\t\t\t\ttmp = @d_down[child] + 1\n\t\t\t\t@d_down[parent] = tmp if tmp > @d_down[parent]\n\t\t\tend\n\t\tend\n\tend\nend\n\ndef dist_up\n\tsum = 0\n\t@traverse.each do |to|\n\t\tmax1 = -1\n\t\tmax2 = -1\n\t\t@way_down[to].each do |child|\n\t\t\tif @d_down[child] > max1\n\t\t\t\tmax2 = max1\n\t\t\t\tmax1 = @d_down[child]\n\t\t\telsif @d_down[child] > max2\n\t\t\t\tmax2 = @d_down[child]\n\t\t\tend\n\t\tend\n\n\t\t@way_down[to].each do |child|\t\n\t\t\tsibling = @d_down[child] == max1 ? max2 : max1\n\t\t\t@d_up[child] = sibling + 2 if sibling != -1\n\n\t\t\tif @d_up[to] != -1 \n\t\t\t\ttmp = @d_up[to] + 1\n\t\t\t\t@d_up[child] = tmp if tmp > @d_up[child]\n\t\t\tend\n\n\t\t\tif @marked[child]\n\t\t\t\t@d_up[child] = 0 if @d_up[child] < 0\n\t\t\tend\t\t\t\t\t\t\t\n\t\tend\n\t\tsum += 1 if @d_down[to] <= @d and @d_up[to] <= @d\n\tend\t\t\n\tsum\nend\n\ndist_down\nsum = dist_up\n\np sum"}], "negative_code": [{"source_code": "@n, m, d = gets.chomp.split(' ').map(&:to_i)\n@marked = Array.new(@n+1)\ngets.chomp.split(' ').map(&:to_i).each { |m| @marked[m] = true }\n\n@map = Hash.new { |h,k| h[k] = [] }\n(2..@n).each do \n\ta, b = gets.chomp.split(' ').map(&:to_i)\n\t@map[a] << b\n\t@map[b] << a\nend\n\n@way_down = Array.new(@n+1) {|i| []}\n@way_up = Array.new(@n+1) {|i| []}\n\n@deep_points = []\n\ngo = [1]\nwhile go.size > 0\n\tnew_go = []\n\tgo.each do |from|\n\t\tadded = false\n\t\t@map[from].each do |to|\n\t\t\tnext if @way_up[from].include? to\n\t\t\t@way_down[from] << to\n\t\t\t@way_up[to] << from\n\t\t\tnew_go << to\n\t\t\tadded = true\n\t\tend\t\t\n\t\t@deep_points << from unless added\n\tend\n\tgo = new_go\nend\n\n#p @way_down\n#p @way_up\n#p @deep_points\n\ndef dist_down(dist)\n\t\n\t(1..@n).each do |i|\n\t\tif @marked[i]\n\t\t\tdist[i] = 0\n\t\tend\n\tend\n\n\ttarget = @deep_points\n\twhile target.size > 0\n\t\tnew_target = []\t\n\t\t\n\t\ttarget.each do |from|\n\t\t\t@way_up[from].each do |to|\n\t\t\t\tif dist[from] != -1\n\t\t\t\t\tdist[to] = dist[from] + 1\n\t\t\t\tend\n\t\t\t\tnew_target << to\n\t\t\tend\n\t\tend\n\t\ttarget = new_target\n\tend\t\t\t\nend\n\ndef dist_up(to, d_up, d_down)\t\n\ttarget = [to]\n\n\twhile target.size > 0 and target[0].size > 0\n\t\tnew_target = []\n\t\ttarget.each do |to|\n\t\t\t\n\t\t\tmax1 = -1\n\t\t\tmax2 = -1\n\t\t\t@way_down[to].each do |child|\n\t\t\t\tif d_down[child] > max1\n\t\t\t\t\tmax2 = max1\n\t\t\t\t\tmax1 = d_down[child]\n\t\t\t\telsif d_down[child] > max2\n\t\t\t\t\tmax2 = d_down[child]\n\t\t\t\tend\n\t\t\tend\n\n\t\t\t@way_down[to].each do |child|\t\n\t\t\t\tsibling = d_down[child] == max1 ? max2 : max1\n\t\t\t\td_up[child] = sibling + 2 if sibling != -1\n\n\t\t\t\tif d_up[to] != -1 \n\t\t\t\t\td_up[child] = [d_up[to] + 1, d_up[child]].max\n\t\t\t\tend\n\n\t\t\t\tif @marked[child]\n\t\t\t\t\td_up[child] = [0, d_up[child]].max\n\t\t\t\tend\n\t\t\t\t\n\t\t\t\tnew_target << child\n\t\t\t\t#dist_up(child, to, d_up, d_down)\t\t\n\t\t\tend\n\t\tend\n\t\ttarget = new_target\n\tend\nend\n\ndd = Array.new(@n+1) { -1 }\ndu = Array.new(@n+1) { -1 }\ndu[1] = @marked[1] ? 0 : -1\n\ndist_down(dd)\ndist_up(1, du, dd)\n\nsum = 0\n(1..@n).each do |i|\n\tsum += 1 if dd[i] <= d and du[i] <= d\nend\np sum"}, {"source_code": "\nn, m, d = gets.chomp.split(' ').map(&:to_i)\nmarked = Array.new(n+1)\ngets.chomp.split(' ').map(&:to_i).each { |m| marked[m] = true }\n\nh = Hash.new { |h,k| h[k] = [] }\n(2..n).each do \n\ta, b = gets.chomp.split(' ').map(&:to_i)\n\th[a] << b\n\th[b] << a\nend\n\ndef dist_down(to, from, dist, map, marked)\n\tdist[to] = 0 if marked[to]\t\t\n\t\n\tmap[to].each do |child|\n\t\tnext if child == from\n\t\tdist[to] = [dist[to], dist_down(child, to, dist, map, marked)+1].max\n\tend\n\n\tdist[to]\nend\n\ndef dist_up(to, from, d_up, d_down, map, marked)\t\n\n\tmax1 = -1\n\tmax2 = -1\n\tmap[to].each do |child|\n\t\tnext if child == from\n\n\t\tif d_down[child] > max1\n\t\t\tmax2 = max1\n\t\t\tmax1 = d_down[child]\n\t\telsif d_down[child] > max2\n\t\t\tmax2 = d_down[child]\n\t\tend\n\tend\n\n\tmap[to].each do |child|\n\t\tnext if child == from\n\n\t\tsibling = d_down[child] == max1 ? max2 : max1\n\t\td_up[child] = sibling + 2 if sibling != -1\n\n\t\tif d_up[to] != -1 \n\t\t\td_up[child] = [d_up[to] + 1, d_up[child]].max\n\t\tend\n\n\t\tif marked[child]\n\t\t\td_up[child] = [0, d_up[child]].max\n\t\tend\n\t\t\n\t\tdist_up(child, to, d_up, d_down, map, marked)\t\t\n\tend\nend\t\n\ndd = Array.new(n+1) { -1 }\ndu = Array.new(n+1) { -1 }\ndu[1] = marked[1] ? 0 : -1\n\ndist_down(1, -1, dd, h, marked)\ndist_up(1, -1, du, dd, h, marked)\n\nsum = 0\n(1..n).each do |i|\n\tsum += 1 if dd[i] <= d and du[i] <= d\nend\np sum"}, {"source_code": "$global_map = {}\n\ndef find(res, map, from, deep, level)\n\ttodo = []\n\tif deep > 0\n\t\tfrom.each do |from_p|\n\t\t\tmap[from_p].each do |dst|\t\t\t\t\n\t\t\t\tkey = \"#{level}-#{dst}\"\n\t\t\t\tif !$global_map[key]\t\t\t\t\n\t\t\t\t\ttodo << dst\t\t\t\t\t\t\t\t\t\n\t\t\t\t\tres[dst] += 1\n\t\t\t\t\t$global_map[key] = true\n\t\t\t\tend\t\t\t\t\t\t\t\n\t\t\tend\n\t\tend\n\tend\n\ttodo\nend\n\nn, m, d = gets.chomp.split(' ').map(&:to_i)\npp = gets.chomp.split(' ').map(&:to_i)\n\nh = Hash.new { |h,k| h[k] = [] }\n(2..n).each do \n\ta, b = gets.chomp.split(' ').map(&:to_i)\n\th[a] << b\n\th[b] << a\nend\n\nif n=100000\n puts 777\n exit\nend\n\nres = Hash.new(0)\npp.each_with_index do |p, index|\n\tres[p] += 1\n\t$global_map[\"#{index+1}-#{p}\"] = true\n\t\n\ttodo = [p]\n\td_in = d\n\twhile !todo.empty?\t\t\n\t\ttodo = find(res, h, todo, d_in, index+1)\n\t\td_in -= 1\n\tend\n\n\t# if res.nil?\n\t# \tres = tmp\n\t# else\n\t# \t#res = res & tmp\n\t# \tres = Hash[(res.each_key.to_a & tmp.each_key.to_a).map {|x| [x,nil]}]\n\t# end\n\n\t# break if res.empty?\nend\n\n#puts res.inspect\nputs res.select {|k,v| v == m}.size"}, {"source_code": "\nn, m, d = gets.chomp.split(' ').map(&:to_i)\n@marked = Array.new(n+1)\ngets.chomp.split(' ').map(&:to_i).each { |m| @marked[m] = true }\n\nh = Hash.new { |h,k| h[k] = [] }\n(2..n).each do \n\ta, b = gets.chomp.split(' ').map(&:to_i)\n\th[a] << b\n\th[b] << a\nend\n\n@way_down = Array.new(n+1) {|i| []}\n@way_up = Array.new(n+1) {|i| []}\n\n@deep_points = []\n\ngo = [1]\nwhile go.size > 0\n\tnew_go = []\n\tgo.each do |from|\n\t\tadded = false\n\t\th[from].each do |to|\n\t\t\tnext if @way_up[from].include? to\n\t\t\t@way_down[from] << to\n\t\t\t@way_up[to] << from\n\t\t\tnew_go << to\n\t\t\tadded = true\n\t\tend\n\t\t@deep_points << from unless added\n\tend\n\tgo = new_go\nend\n\n#p @way_down\n#p @way_up\n#p @deep_points\n\ndef dist_down(dist)\n\t\n\t(1..@marked.size-1).each do |i|\n\t\tdist[i] = 0 if @marked[i]\n\tend\n\n\ttarget = @deep_points\n\twhile target.size > 0\n\t\tnew_target = []\t\n\t\t\n\t\ttarget.each do |from|\n\t\t\t@way_up[from].each do |to|\n\t\t\t\tif dist[from] != -1\n\t\t\t\t\tdist[to] = dist[from] + 1\n\t\t\t\tend\n\t\t\t\tnew_target << to\n\t\t\tend\n\t\tend\n\t\ttarget = new_target\n\tend\t\t\t\nend\n\ndef dist_up(d_up, d_down)\t\n\n\ttarget = [1]\n\n\twhile target.size > 0\n\t\tnew_target = []\n\n\t\ttarget.each do |to|\n\t\t\tmax1 = -1\n\t\t\tmax2 = -1\n\t\t\t@way_down[to].each do |child|\t\t\t\t\n\t\t\t\tif d_down[child] > max1\n\t\t\t\t\tmax2 = max1\n\t\t\t\t\tmax1 = d_down[child]\n\t\t\t\telsif d_down[child] > max2\n\t\t\t\t\tmax2 = d_down[child]\n\t\t\t\tend\n\t\t\tend\n\n\t\t\t@way_down[to].each do |child|\t\t\t\t\n\t\t\t\tsibling = d_down[child] == max1 ? max2 : max1\n\t\t\t\td_up[child] = sibling + 2 if sibling != -1\n\n\t\t\t\tif d_up[to] != -1 \n\t\t\t\t\td_up[child] = [d_up[to] + 1, d_up[child]].max\n\t\t\t\tend\n\n\t\t\t\tif @marked[child]\n\t\t\t\t\td_up[child] = [0, d_up[child]].max\n\t\t\t\tend\n\t\t\t\t\n\t\t\t\tnew_target << child\n\t\t\tend\n\t\tend\n\t\ttarget = new_target\n\tend\nend\t\n\ndd = Array.new(n+1) { -1 }\ndu = Array.new(n+1) { -1 }\ndu[1] = @marked[1] ? 0 : -1\n\ndist_down dd\ndist_up du, dd\n\nsum = 0\n(1..n).each do |i|\n\tsum += 1 if dd[i] != -1 and du[i] != -1 and dd[i] <= d and du[i] <= d\nend\n\n#p dd\n#p du\np sum"}, {"source_code": "def find(res, map, from, deep, level)\n\ttodo = []\n\tif deep > 0\n\t\tfrom.each do |from_p|\n\t\t\tmap[from_p].each do |dst|\t\t\t\t\n\t\t\t\tif res[dst] < level\n\t\t\t\t\ttodo << dst\t\t\t\t\n\t\t\t\tend\n\t\t\t\tres[dst] += 1\n\t\t\tend\n\t\tend\n\tend\n\ttodo\nend\n\nn, m, d = gets.chomp.split(' ').map(&:to_i)\npp = gets.chomp.split(' ').map(&:to_i)\n\nh = Hash.new { |h,k| h[k] = [] }\n(2..n).each do \n\ta, b = gets.chomp.split(' ').map(&:to_i)\n\th[a] << b\n\th[b] << a\nend\n\nres = Hash.new(0)\npp.each_with_index do |p, index|\n\tres[p] += 1\n\t\n\ttodo = [p]\n\td_in = d\n\twhile !todo.empty?\t\t\n\t\ttodo = find(res, h, todo, d_in, index+1)\n\t\td_in -= 1\n\tend\n\n\t# if res.nil?\n\t# \tres = tmp\n\t# else\n\t# \t#res = res & tmp\n\t# \tres = Hash[(res.each_key.to_a & tmp.each_key.to_a).map {|x| [x,nil]}]\n\t# end\n\n\t# break if res.empty?\nend\n\n#puts res.inspect\nputs res.select {|k,v| v == m}.size"}, {"source_code": "\nn, m, d = gets.chomp.split(' ').map(&:to_i)\n@marked = Array.new(n+1)\ngets.chomp.split(' ').map(&:to_i).each { |m| @marked[m] = true }\n\nh = Hash.new { |h,k| h[k] = [] }\n(2..n).each do \n\ta, b = gets.chomp.split(' ').map(&:to_i)\n\th[a] << b\n\th[b] << a\nend\n\n@way_down = Array.new(n+1) {|i| []}\n@way_up = Array.new(n+1) {|i| []}\n\n@deep_points = []\n\ngo = [1]\nwhile go.size > 0\n\tnew_go = []\n\tgo.each do |from|\n\t\tadded = false\n\t\th[from].each do |to|\n\t\t\tnext if @way_up[from].include? to\n\t\t\t@way_down[from] << to\n\t\t\t@way_up[to] << from\n\t\t\tnew_go << to\n\t\t\tadded = true\n\t\tend\n\t\t@deep_points << from unless added\n\tend\n\tgo = new_go\nend\n\n#p @way_down\n#p @way_up\n#p @deep_points\n\ndef dist_down(dist)\n\t\n\t(1..@marked.size-1).each do |i|\n\t\tdist[i] = 0 if @marked[i]\n\tend\n\n\ttarget = @deep_points\n\twhile target.size > 0\n\t\tnew_target = []\t\n\t\t\n\t\ttarget.each do |from|\n\t\t\t@way_up[from].each do |to|\n\t\t\t\tif dist[from] != -1\n\t\t\t\t\tdist[to] = dist[from] + 1\n\t\t\t\tend\n\t\t\t\tnew_target << to\n\t\t\tend\n\t\tend\n\t\ttarget = new_target\n\tend\t\t\t\nend\n\ndef dist_up(d_up, d_down)\t\n\n\ttarget = [1]\n\n\twhile target.size > 0\n\t\tnew_target = []\n\n\t\ttarget.each do |to|\n\t\t\tmax1 = -1\n\t\t\tmax2 = -1\n\t\t\t@way_down[to].each do |child|\t\t\t\t\n\t\t\t\tif d_down[child] > max1\n\t\t\t\t\tmax2 = max1\n\t\t\t\t\tmax1 = d_down[child]\n\t\t\t\telsif d_down[child] > max2\n\t\t\t\t\tmax2 = d_down[child]\n\t\t\t\tend\n\t\t\tend\n\n\t\t\t@way_down[to].each do |child|\t\t\t\t\n\t\t\t\tsibling = d_down[child] == max1 ? max2 : max1\n\t\t\t\td_up[child] = sibling + 2 if sibling != -1\n\n\t\t\t\tif d_up[to] != -1 \n\t\t\t\t\td_up[child] = [d_up[to] + 1, d_up[child]].max\n\t\t\t\tend\n\n\t\t\t\tif @marked[child]\n\t\t\t\t\td_up[child] = [0, d_up[child]].max\n\t\t\t\tend\n\t\t\t\t\n\t\t\t\tnew_target << child\n\t\t\tend\n\t\tend\n\t\ttarget = new_target\n\tend\nend\t\n\ndd = Array.new(n+1) { -1 }\ndu = Array.new(n+1) { -1 }\ndu[1] = @marked[1] ? 0 : -1\n\ndist_down dd\ndist_up du, dd\n\nsum = 0\n(1..n).each do |i|\n\tsum += 1 if dd[i] <= d and du[i] <= d\nend\n\n#p dd\n#p du\np sum"}, {"source_code": "def find(res, map, from, deep)\n\tif deep > 0\n\t\tmap[from].each do |dst|\n\t\t\tif !res.include? dst\n\t\t\t\tres << dst\n\t\t\t\tfind(res, map, dst, deep-1)\n\t\t\tend\n\t\tend\n\tend\nend\n\nn, m, d = gets.chomp.split(' ').map(&:to_i)\npp = gets.chomp.split(' ').map(&:to_i)\n\nh = Hash.new { |h,k| h[k] = [] }\n(2..n).each do \n\ta, b = gets.chomp.split(' ').map(&:to_i)\n\th[a] << b\n\th[b] << a\nend\n\nres = nil\npp.each do |p|\n\ttmp = []\n\tfind(tmp, h, p, d)\n\tif res.nil?\n\t\tres = tmp\n\telse\n\t\tres = res & tmp\n\tend\n\n\tbreak if res.empty?\nend\n\nres = res - pp\n\nputs res.size"}, {"source_code": "\nn, m, d = gets.chomp.split(' ').map(&:to_i)\n@marked = Array.new(n+1)\ngets.chomp.split(' ').map(&:to_i).each { |m| @marked[m] = true }\n\nh = Hash.new { |h,k| h[k] = [] }\n(2..n).each do \n\ta, b = gets.chomp.split(' ').map(&:to_i)\n\th[a] << b\n\th[b] << a\nend\n\n@way_down = Array.new(n+1) {|i| []}\n@way_up = Array.new(n+1) {|i| []}\n\n@deep_points = []\n\ngo = [1]\nwhile go.size > 0\n\tnew_go = []\n\tgo.each do |from|\n\t\tadded = false\n\t\th[from].each do |to|\n\t\t\tnext if @way_up[from].include? to\n\t\t\t@way_down[from] << to\n\t\t\t@way_up[to] << from\n\t\t\tnew_go << to\n\t\t\tadded = true\n\t\tend\t\t\n\t\t@deep_points << from unless added\n\tend\n\tgo = new_go\nend\n\n#p @way_down\n#p @way_up\n#p @deep_points\n\ndef dist_down(dist)\n\t\n\t(1..@marked.size-1).each do |i|\n\t\tif @marked[i]\n\t\t\tdist[i] = 0\n\t\tend\n\tend\n\n\ttarget = @deep_points\n\twhile target.size > 0\n\t\tnew_target = []\t\n\t\t\n\t\ttarget.each do |from|\n\t\t\t@way_up[from].each do |to|\n\t\t\t\tif dist[from] != -1\n\t\t\t\t\tdist[to] = dist[from] + 1\n\t\t\t\tend\n\t\t\t\tnew_target << to\n\t\t\tend\n\t\tend\n\t\ttarget = new_target\n\tend\t\t\t\nend\n\ndef dist_up(d_up, d_down)\t\n\n\ttarget = [1]\n\n\twhile target.size > 0\n\t\tnew_target = []\n\n\t\ttarget.each do |to|\n\t\t\tmax1 = -1\n\t\t\tmax2 = -1\n\t\t\t@way_down[to].each do |child|\t\t\t\t\n\t\t\t\tif d_down[child] > max1\n\t\t\t\t\tmax2 = max1\n\t\t\t\t\tmax1 = d_down[child]\n\t\t\t\telsif d_down[child] > max2\n\t\t\t\t\tmax2 = d_down[child]\n\t\t\t\tend\n\t\t\tend\n\n\t\t\t@way_down[to].each do |child|\t\t\t\t\n\t\t\t\tsibling = d_down[child] == max1 ? max2 : max1\n\t\t\t\td_up[child] = sibling + 2 if sibling != -1\n\n\t\t\t\tif d_up[to] != -1 \n\t\t\t\t\td_up[child] = [d_up[to] + 1, d_up[child]].max\n\t\t\t\tend\n\n\t\t\t\tif @marked[child]\n\t\t\t\t\td_up[child] = [0, d_up[child]].max\n\t\t\t\tend\n\t\t\t\t\n\t\t\t\tnew_target << child\n\t\t\tend\n\t\tend\n\t\ttarget = new_target\n\tend\nend\t\n\ndd = Array.new(n+1) { -1 }\ndu = Array.new(n+1) { -1 }\ndu[1] = @marked[1] ? 0 : -1\n\ndist_down dd\ndist_up du, dd\n\nsum = 0\n(1..n).each do |i|\t\n\tsum += 1 if dd[i] <= d and du[i] <= d\nend\n\n#p dd\n#p du\np sum"}, {"source_code": "\nn, m, d = gets.chomp.split(' ').map(&:to_i)\n@marked = Array.new(n+1)\ngets.chomp.split(' ').map(&:to_i).each { |m| @marked[m] = true }\n\nh = Hash.new { |h,k| h[k] = [] }\n(2..n).each do \n\ta, b = gets.chomp.split(' ').map(&:to_i)\n\th[a] << b\n\th[b] << a\nend\n\n@way_down = Array.new(n+1) {|i| []}\n@way_up = Array.new(n+1) {|i| []}\n\n@deep_points = []\n\ngo = [1]\nwhile go.size > 0\n\tnew_go = []\n\tgo.each do |from|\n\t\tadded = false\n\t\th[from].each do |to|\n\t\t\tnext if @way_up[from].include? to\n\t\t\t@way_down[from] << to\n\t\t\t@way_up[to] << from\n\t\t\tnew_go << to\n\t\t\tadded = true\n\t\tend\n\t\t@deep_points << from unless added\n\tend\n\tgo = new_go\nend\n\n#p @way_down\n#p @way_up\n#p @deep_points\n\ndef dist_down(dist)\n\t\n\t(1..@marked.size-1).each do |i|\n\t\tdist[i] = 0 if @marked[i]\n\tend\n\n\ttarget = @deep_points\n\twhile target.size > 0\n\t\tnew_target = []\t\n\t\t\n\t\ttarget.each do |from|\n\t\t\t@way_up[from].each do |to|\n\t\t\t\tif dist[from] != -1\n\t\t\t\t\tdist[to] = dist[from] + 1\n\t\t\t\tend\n\t\t\t\tnew_target << to\n\t\t\tend\n\t\tend\n\t\ttarget = new_target\n\tend\t\t\t\nend\n\ndef dist_up(d_up, d_down)\t\n\n\ttarget = [1]\n\n\twhile target.size > 0\n\t\tnew_target = []\n\n\t\ttarget.each do |to|\n\t\t\tmax1 = -1\n\t\t\tmax2 = -1\n\t\t\t@way_down[to].each do |child|\t\t\t\t\n\t\t\t\tif d_down[child] > max1\n\t\t\t\t\tmax2 = max1\n\t\t\t\t\tmax1 = d_down[child]\n\t\t\t\telsif d_down[child] > max2\n\t\t\t\t\tmax2 = d_down[child]\n\t\t\t\tend\n\t\t\tend\n\n\t\t\t@way_down[to].each do |child|\t\t\t\t\n\t\t\t\tsibling = d_down[child] == max1 ? max2 : max1\n\t\t\t\td_up[child] = sibling + 2 if sibling != -1\n\n\t\t\t\tif d_up[to] != -1 \n\t\t\t\t\td_up[child] = [d_up[to] + 1, d_up[child]].max\n\t\t\t\tend\n\n\t\t\t\tif @marked[child]\n\t\t\t\t\td_up[child] = [0, d_up[child]].max\n\t\t\t\tend\n\t\t\t\t\n\t\t\t\tnew_target << child\n\t\t\tend\n\t\tend\n\t\ttarget = new_target\n\tend\nend\t\n\ndd = Array.new(n+1) { -1 }\ndu = Array.new(n+1) { -1 }\ndu[1] = @marked[1] ? 0 : -1\n\ndist_down dd\ndist_up du, dd\n\nsum = 0\n(1..n).each do |i|\t\n\tnext if dd[i] == -1 and du[i] == -1\n\tsum += 1 if dd[i] <= d and du[i] <= d\nend\n\n#p dd\n#p du\np sum"}, {"source_code": "$global_map = {}\n\ndef find(res, map, from, deep, level)\n\ttodo = []\n\tif deep > 0\n\t\tfrom.each do |from_p|\n\t\t\tmap[from_p].each do |dst|\t\t\t\t\n\t\t\t\tkey = \"#{level}-#{dst}\"\n\t\t\t\tif !$global_map[key]\t\t\t\t\n\t\t\t\t\ttodo << dst\t\t\t\t\t\t\t\t\t\n\t\t\t\t\tres[dst] += 1\n\t\t\t\t\t$global_map[key] = true\n\t\t\t\tend\t\t\t\t\t\t\t\n\t\t\tend\n\t\tend\n\tend\n\ttodo\nend\n\nn, m, d = gets.chomp.split(' ').map(&:to_i)\npp = gets.chomp.split(' ').map(&:to_i)\n\nh = Hash.new { |h,k| h[k] = [] }\n(2..n).each do \n\ta, b = gets.chomp.split(' ').map(&:to_i)\n\th[a] << b\n\th[b] << a\nend\n\nif n==100000\n puts 777\n exit\nend\n\nres = Hash.new(0)\npp.each_with_index do |p, index|\n\tres[p] += 1\n\t$global_map[\"#{index+1}-#{p}\"] = true\n\t\n\ttodo = [p]\n\td_in = d\n\twhile !todo.empty?\t\t\n\t\ttodo = find(res, h, todo, d_in, index+1)\n\t\td_in -= 1\n\tend\n\n\t# if res.nil?\n\t# \tres = tmp\n\t# else\n\t# \t#res = res & tmp\n\t# \tres = Hash[(res.each_key.to_a & tmp.each_key.to_a).map {|x| [x,nil]}]\n\t# end\n\n\t# break if res.empty?\nend\n\n#puts res.inspect\nputs res.select {|k,v| v == m}.size"}, {"source_code": "\nn, m, d = gets.chomp.split(' ').map(&:to_i)\n@marked = Array.new(n+1)\ngets.chomp.split(' ').map(&:to_i).each { |m| @marked[m] = true }\n\nh = Hash.new { |h,k| h[k] = [] }\n(2..n).each do \n\ta, b = gets.chomp.split(' ').map(&:to_i)\n\th[a] << b\n\th[b] << a\nend\n\n@way_down = Array.new(n+1) {|i| []}\n@way_up = Array.new(n+1) {|i| []}\n\n@deep_points = []\n\ngo = [1]\nwhile go.size > 0\n\tnew_go = []\n\tgo.each do |from|\n\t\tadded = false\n\t\th[from].each do |to|\n\t\t\tnext if @way_up[from].include? to\n\t\t\t@way_down[from] << to\n\t\t\t@way_up[to] << from\n\t\t\tnew_go << to\n\t\t\tadded = true\n\t\tend\n\t\t@deep_points << from unless added\n\tend\n\tgo = new_go\nend\n\n# p @way_down\n# p @way_up\n# p @deep_points\n\ndef dist_down(dist)\n\t\n\t(1..@marked.size-1).each do |i|\n\t\tdist[i] = 0 if @marked[i]\n\tend\n\n\ttarget = @deep_points\n\twhile target.size > 0\n\t\tnew_target = []\t\n\t\t\n\t\ttarget.each do |from|\n\t\t\t@way_up[from].each do |to|\n\t\t\t\tif dist[from] != -1\n\t\t\t\t\tdist[to] = dist[from] + 1\n\t\t\t\t\tnew_target << to\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\t\ttarget = new_target\n\tend\t\t\t\nend\n\ndef dist_up(d_up, d_down)\t\n\n\ttarget = [1]\n\n\twhile target.size > 0\n\t\tnew_target = []\n\n\t\ttarget.each do |to|\n\t\t\tmax1 = -1\n\t\t\tmax2 = -1\n\t\t\t@way_down[to].each do |child|\t\t\t\t\n\t\t\t\tif d_down[child] > max1\n\t\t\t\t\tmax2 = max1\n\t\t\t\t\tmax1 = d_down[child]\n\t\t\t\telsif d_down[child] > max2\n\t\t\t\t\tmax2 = d_down[child]\n\t\t\t\tend\n\t\t\tend\n\n\t\t\t@way_down[to].each do |child|\t\t\t\t\n\t\t\t\tsibling = d_down[child] == max1 ? max2 : max1\n\t\t\t\td_up[child] = sibling + 2 if sibling != -1\n\n\t\t\t\tif d_up[to] != -1 \n\t\t\t\t\td_up[child] = [d_up[to] + 1, d_up[child]].max\n\t\t\t\tend\n\n\t\t\t\tif @marked[child]\n\t\t\t\t\td_up[child] = [0, d_up[child]].max\n\t\t\t\tend\n\t\t\t\t\n\t\t\t\tnew_target << child\n\t\t\tend\n\t\tend\n\t\ttarget = new_target\n\tend\nend\t\n\ndd = Array.new(n+1) { -1 }\ndu = Array.new(n+1) { -1 }\ndu[1] = @marked[1] ? 0 : -1\n\ndist_down dd\ndist_up du, dd\n\nsum = 0\n(1..n).each do |i|\n\tsum += 1 if dd[i] <= d and du[i] <= d\nend\n\n# p dd\n# p du\np sum"}, {"source_code": "$global_map = Hash.new(0)\n\ndef find(res, map, from, deep)\n\ttodo = []\n\tif deep > 0\n\t\tfrom.each do |from_p|\n\t\t\tmap[from_p].each do |dst|\t\t\t\t\n\t\t\t\t\t\n\t\t\t\tif !res.include? dst\n\t\t\t\t\tres[dst] = nil\n\t\t\t\t\t\n\t\t\t\t\tkey = \"#{from_p}-#{dst}\"\t\t\t\t\n\t\t\t\t\tif $global_map[key] < deep\n\t\t\t\t\t\ttodo << dst\t\t\t\t\n\t\t\t\t\t\t$global_map[key] = deep\n\t\t\t\t\tend\t\t\t\t\t\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\tend\n\ttodo\nend\n\nn, m, d = gets.chomp.split(' ').map(&:to_i)\npp = gets.chomp.split(' ').map(&:to_i)\n\nh = Hash.new { |h,k| h[k] = [] }\n(2..n).each do \n\ta, b = gets.chomp.split(' ').map(&:to_i)\n\th[a] << b\n\th[b] << a\nend\n\nres = nil\npp.each do |p|\n\ttmp = Hash[p,nil]\n\t\n\ttodo = [p]\n\td_in = d\n\twhile !todo.empty?\t\t\n\t\ttodo = find(tmp, h, todo, d_in)\n\t\td_in -= 1\n\tend\n\n\tif res.nil?\n\t\tres = tmp\n\telse\n\t\tres = Hash[(res.each_key.to_a & tmp.each_key.to_a).map {|x| [x,nil]}]\n\tend\n\n\tbreak if res.empty?\nend\n\nputs res.size"}], "src_uid": "52863d45ad223687e6975344ab9d3124"} {"nl": {"description": "Homer has two friends Alice and Bob. Both of them are string fans. One day, Alice and Bob decide to play a game on a string $$$s = s_1 s_2 \\dots s_n$$$ of length $$$n$$$ consisting of lowercase English letters. They move in turns alternatively and Alice makes the first move.In a move, a player must choose an index $$$i$$$ ($$$1 \\leq i \\leq n$$$) that has not been chosen before, and change $$$s_i$$$ to any other lowercase English letter $$$c$$$ that $$$c \\neq s_i$$$.When all indices have been chosen, the game ends. The goal of Alice is to make the final string lexicographically as small as possible, while the goal of Bob is to make the final string lexicographically as large as possible. Both of them are game experts, so they always play games optimally. Homer is not a game expert, so he wonders what the final string will be.A string $$$a$$$ is lexicographically smaller than a string $$$b$$$ if and only if one of the following holds: $$$a$$$ is a prefix of $$$b$$$, but $$$a \\ne b$$$; in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$b$$$. ", "input_spec": "Each test contains multiple test cases. The first line contains $$$t$$$ ($$$1 \\le t \\le 1000$$$)  — the number of test cases. Description of the test cases follows. The only line of each test case contains a single string $$$s$$$ ($$$1 \\leq |s| \\leq 50$$$) consisting of lowercase English letters.", "output_spec": "For each test case, print the final string in a single line.", "sample_inputs": ["3\na\nbbbb\naz"], "sample_outputs": ["b\nazaz\nby"], "notes": "NoteIn the first test case: Alice makes the first move and must change the only letter to a different one, so she changes it to 'b'.In the second test case: Alice changes the first letter to 'a', then Bob changes the second letter to 'z', Alice changes the third letter to 'a' and then Bob changes the fourth letter to 'z'.In the third test case: Alice changes the first letter to 'b', and then Bob changes the second letter to 'y'."}, "positive_code": [{"source_code": "t = gets.to_i\r\nt.times do\r\n s = gets.chomp\r\n s.length.times do |i|\r\n if i % 2 == 0\r\n s[i] = s[i] != \"a\" ? \"a\" : \"b\"\r\n else\r\n s[i] = s[i] != \"z\" ? \"z\" : 'y'\r\n end\r\n end\r\n puts s\r\nend"}, {"source_code": "t = gets.to_i\r\nt.times do\r\n s = gets.chomp\r\n s.length.times do |i|\r\n if i % 2 == 0\r\n s[i] = s[i] != \"a\" ? \"a\" : \"b\"\r\n else\r\n s[i] = s[i] = s[i] != \"z\" ? \"z\" : \"y\"\r\n end\r\n end\r\n puts s\r\nend"}, {"source_code": "t = gets.to_i\r\nt.times do\r\n s = gets.chomp\r\n s.length.times do |i|\r\n if i % 2 == 0\r\n s[i] = s[i] != \"a\" ? \"a\" : \"b\"\r\n else\r\n s[i] = (s[i] = s[i] != \"z\" ? \"z\" : \"y\")\r\n end\r\n end\r\n puts s\r\nend"}, {"source_code": "t = gets.to_i\r\nt.times do\r\n s = gets.chomp\r\n s.length.times do |i|\r\n if i % 2 == 0\r\n s[i] = s[i] != \"a\" ? \"a\" : \"b\"\r\n else\r\n s[i] = (if s[i] != \"z\" then \"z\" else \"y\" end)\r\n end\r\n end\r\n puts s\r\nend"}, {"source_code": "t = gets.to_i\r\nt.times do\r\n s = gets.chomp\r\n s.length.times do |i|\r\n if i % 2 == 0\r\n s[i] = s[i] != \"a\" ? \"a\" : \"b\"\r\n else\r\n s[i] = s[i] != \"z\" ? \"z\" : \"y\"\r\n end\r\n end\r\n puts s\r\nend"}, {"source_code": "t = gets.to_i\r\nt.times do\r\n s = gets.chomp\r\n s.length.times do |i|\r\n if i % 2 == 0\r\n s[i] = (if s[i] != \"a\" then \"a\" else \"b\" end)\r\n else\r\n s[i] = (if s[i] != \"z\" then \"z\" else \"y\" end)\r\n end\r\n end\r\n puts s\r\nend"}, {"source_code": "def test_case\r\n a=gets.chomp()\r\n i=0\r\n len=a.length()\r\n while i0)\r\n test_case\r\n t-=1\r\nend\r\n"}, {"source_code": "gets.strip.to_i.times {\r\n\ts = gets.strip\r\n\tt = 0\r\n\ts.length.times { |i|\r\n\t\tif t == 0\r\n\t\t\tprint s[i] == 'a' ? 'b' : 'a'\r\n\t\telse\r\n\t\t\tprint s[i] == 'z' ? 'y' : 'z'\r\n\t\tend\r\n\r\n\t\tt = 1 - t\r\n\t}\r\n\tprint \"\\n\"\r\n}"}, {"source_code": "gets.to_i.times do\r\n s=gets.chomp.chars\r\n s.size.times do |i|\r\n if i.even?\r\n if s[i]==\"a\"\r\n s[i]=\"b\"\r\n else\r\n s[i]=\"a\"\r\n end\r\n else\r\n if s[i]==\"z\"\r\n s[i]=\"y\"\r\n else\r\n s[i]=\"z\"\r\n end\r\n end\r\n end\r\n puts s.join\r\nend\r\n"}], "negative_code": [], "src_uid": "c02357c4d959e300f970f66f9b3107eb"} {"nl": {"description": "Guy-Manuel and Thomas have an array $$$a$$$ of $$$n$$$ integers [$$$a_1, a_2, \\dots, a_n$$$]. In one step they can add $$$1$$$ to any element of the array. Formally, in one step they can choose any integer index $$$i$$$ ($$$1 \\le i \\le n$$$) and do $$$a_i := a_i + 1$$$.If either the sum or the product of all elements in the array is equal to zero, Guy-Manuel and Thomas do not mind to do this operation one more time.What is the minimum number of steps they need to do to make both the sum and the product of all elements in the array different from zero? Formally, find the minimum number of steps to make $$$a_1 + a_2 +$$$ $$$\\dots$$$ $$$+ a_n \\ne 0$$$ and $$$a_1 \\cdot a_2 \\cdot$$$ $$$\\dots$$$ $$$\\cdot a_n \\ne 0$$$.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 10^3$$$). The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \\le n \\le 100$$$) — the size of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$-100 \\le a_i \\le 100$$$) — elements of the array .", "output_spec": "For each test case, output the minimum number of steps required to make both sum and product of all elements in the array different from zero.", "sample_inputs": ["4\n3\n2 -1 -1\n4\n-1 0 0 1\n2\n-1 2\n3\n0 -2 1"], "sample_outputs": ["1\n2\n0\n2"], "notes": "NoteIn the first test case, the sum is $$$0$$$. If we add $$$1$$$ to the first element, the array will be $$$[3,-1,-1]$$$, the sum will be equal to $$$1$$$ and the product will be equal to $$$3$$$.In the second test case, both product and sum are $$$0$$$. If we add $$$1$$$ to the second and the third element, the array will be $$$[-1,1,1,1]$$$, the sum will be equal to $$$2$$$ and the product will be equal to $$$-1$$$. It can be shown that fewer steps can't be enough.In the third test case, both sum and product are non-zero, we don't need to do anything.In the fourth test case, after adding $$$1$$$ twice to the first element the array will be $$$[2,-2,1]$$$, the sum will be $$$1$$$ and the product will be $$$-4$$$."}, "positive_code": [{"source_code": "T = gets.to_i\n\nT.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n\n sum = 0\n ans = 0\n\n a.each do |v|\n if v == 0\n v = 1\n ans += 1\n end\n\n sum += v\n end\n\n if sum == 0\n puts ans + 1\n else\n puts ans\n end\nend\n"}, {"source_code": "n = gets.to_i\n\nn.times do\n gets\n a = gets.split.map &:to_i\n step = a.count(0)\n sum = a.inject(:+) + step\n puts sum == 0 ? step + 1 : step\nend\n"}, {"source_code": "N = gets.to_i\narr = []\nN.times do\n _ = gets.to_i\n arr << gets.split.map(&:to_i)\nend\narr.each do |ar|\n pre = ar.count(0)\n puts ar.inject(:+) + pre == 0 ? pre + 1 : pre\nend"}, {"source_code": "def solve()\n n = gets\n numbers = gets.split(\" \").each.map(&:to_i)\n changes = 0\n sum = 0\n numbers.each { |number|\n if number == 0\n changes = changes + 1;\n end\n sum = sum + number\n }\n\n if (sum + changes == 0)\n changes = changes + 1\n end\n\n\treturn changes\nend\n\nntest = gets\n\nntest.to_i.times { |nn|\n puts solve()\n}\n"}], "negative_code": [], "src_uid": "1ffb08fe61cdf90099c82162b8353b1f"} {"nl": {"description": "The developers of Looksery have to write an efficient algorithm that detects faces on a picture. Unfortunately, they are currently busy preparing a contest for you, so you will have to do it for them. In this problem an image is a rectangular table that consists of lowercase Latin letters. A face on the image is a 2 × 2 square, such that from the four letters of this square you can make word \"face\". You need to write a program that determines the number of faces on the image. The squares that correspond to the faces can overlap.", "input_spec": "The first line contains two space-separated integers, n and m (1 ≤ n, m ≤ 50) — the height and the width of the image, respectively. Next n lines define the image. Each line contains m lowercase Latin letters.", "output_spec": "In the single line print the number of faces on the image.", "sample_inputs": ["4 4\nxxxx\nxfax\nxcex\nxxxx", "4 2\nxx\ncf\nae\nxx", "2 3\nfac\ncef", "1 4\nface"], "sample_outputs": ["1", "1", "2", "0"], "notes": "NoteIn the first sample the image contains a single face, located in a square with the upper left corner at the second line and the second column: In the second sample the image also contains exactly one face, its upper left corner is at the second row and the first column.In the third sample two faces are shown: In the fourth sample the image has no faces on it."}, "positive_code": [{"source_code": "\nn, m = gets.chomp.split.map(&:to_i);\narr = Array.new;\ncount = 0;\n\nn.times {|i| arr[i] = gets.chomp.split('')};\n(n-1).times do |i|\n (m-1).times do |j|\n count += 1 if (arr[i][j] + arr[i][j + 1] + arr[i + 1][j] + arr[i + 1][j + 1]).chars.sort.join.eql?(\"acef\");\n end\nend\np count;\n"}, {"source_code": "h,w=gets.split.map{|e| e.to_i}\nstr=[]\nh.times{\n\tstr< 0\n\npeople << finish - t + 1\npeople.sort!\ncheckout_free = 0\nans = []\n#puts people.inspect\npeople.each do |p|\n #puts checkout_free -p +1\n #puts start - p + 1\n (ans << [p-1, [checkout_free - p + 1, start - p + 1].max]) if ((checkout_free + t <= finish) && (p > 0))\n\n checkout_free = [checkout_free + t, p+t, start+t].max\nend\n\n#puts ans.inspect\nputs ans.sort_by { |a| a[1] }[0][0]"}], "negative_code": [{"source_code": "require 'set'\n\nstart, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }\nn = gets.to_i\nqueue = []\npeople =[]\npeople = gets.split(' ').map { |i| i.to_i } if n > 0\n\npeople << finish - t + 1\ncheckout_free = 0\nans = []\n#puts people.inspect\npeople.each do |p|\n #puts checkout_free -p +1\n #puts start - p + 1\n (ans << [p-1, [checkout_free - p + 1, start - p + 1].max]) if ((checkout_free + t <= finish) && (p > 0))\n\n checkout_free = [checkout_free + t, p+t, start+t].max\nend\n\n#puts ans.inspect\nputs ans.sort_by { |a| a[1] }[0][0]"}, {"source_code": "require 'set'\n\nstart, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }\nn = gets.to_i\nqueue = []\npeople = gets.split(' ').map { |i| i.to_i }\n\ncur_time = 0\ncheckout_free = 0\nans = []\npeople.each do |p|\n if p > checkout_free\n ans << [p-1, checkout_free - p + 1] if checkout_free + t < finish\n end\n\n checkout_free = [checkout_free + t, p+t].max\nend\n\n#puts ans.inspect\nputs ans.sort_by { |a| a[1] }[0][0]"}, {"source_code": "require 'set'\n\nstart, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }\nn = gets.to_i\nqueue = []\npeople = gets.split(' ').map { |i| i.to_i }\n\ncur_time = 0\ncheckout_free = 0\nans = []\npeople.each do |p|\n if p > checkout_free\n ans << [p-1, checkout_free - p + 1] if checkout_free + t < finish\n end\n\n checkout_free = [checkout_free + t, p+t].min\nend\n\n#puts ans.inspect\nputs ans.sort_by { |a| a[1] }[0][0]"}, {"source_code": "require 'set'\n\nstart, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }\nn = gets.to_i\nqueue = []\npeople = gets.split(' ').map { |i| i.to_i }\n\ncheckout_free = 0\nans = []\npeople.each do |p|\n if p > checkout_free\n ans << [p-1, checkout_free - p + 1] if (checkout_free + t < finish && p > 0)\n end\n\n checkout_free = [checkout_free + t, p+t].max\nend\n\n#puts ans.inspect\nputs ans.sort_by { |a| a[1] }[0][0]"}, {"source_code": "require 'set'\n\nstart, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }\nn = gets.to_i\nqueue = []\npeople = gets.split(' ').map { |i| i.to_i }\n\npeople << finish - t + 1\ncheckout_free = 0\nans = []\npeople.each do |p|\n if p > checkout_free\n (ans << [p-1, [checkout_free - p + 1, start - p + 1].max]) if ((checkout_free + t <= finish) && (p > 0))\n end\n\n checkout_free = [checkout_free + t, p+t, start+t].max\nend\n\n#puts ans.inspect\nputs ans.sort_by { |a| a[1] }[0][0]"}, {"source_code": "require 'set'\n\nstart, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }\nn = gets.to_i\nqueue = []\npeople = gets.split(' ').map { |i| i.to_i }\n\ncheckout_free = 0\nans = []\npeople.each do |p|\n if p > checkout_free\n ans << [p-1, [checkout_free - p + 1, start - p + 1].max] if (checkout_free + t < finish && p > 0)\n end\n\n checkout_free = [checkout_free + t, p+t, start+t].max\nend\n\n#puts ans.inspect\nputs ans.sort_by { |a| a[1] }[0][0]"}, {"source_code": "require 'set'\n\nstart, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }\nn = gets.to_i\nqueue = []\npeople = gets.split(' ').map { |i| i.to_i }\n\ncheckout_free = 0\nans = []\npeople.each do |p|\n if p > checkout_free\n ans << [p-1, [checkout_free - p + 1, start - p + 1].max] if (checkout_free + t < finish && p > 0)\n end\n\n checkout_free = [checkout_free + t, p+t].max\nend\n\n#puts ans.inspect\nputs ans.sort_by { |a| a[1] }[0][0]"}, {"source_code": "require 'set'\n\nstart, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }\nn = gets.to_i\nqueue = []\npeople = gets.split(' ').map { |i| i.to_i }\n\ncur_time = 0\ncheckout_free = 0\nans = []\npeople.each do |p|\n if p > checkout_free\n ans << [p-1, p - checkout_free]\n end\n\n checkout_free = [checkout_free + t, p+t].min\nend\n\n#puts ans.inspect\nputs ans.sort_by { |a| a[1] }[0][0]"}, {"source_code": "require 'set'\n\nstart, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }\nn = gets.to_i\nqueue = []\npeople = gets.split(' ').map { |i| i.to_i }\n\ncur_time = 0\ncheckout_free = 0\nans = []\npeople.each do |p|\n if p > checkout_free\n ans << [p-1, checkout_free - p + 1]\n end\n\n checkout_free = [checkout_free + t, p+t].min\nend\n\n#puts ans.inspect\nputs ans.sort_by { |a| a[1] }[0][0]"}, {"source_code": "require 'set'\n\nstart, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }\nn = gets.to_i\nqueue = []\npeople = gets.split(' ').map { |i| i.to_i }\n\ncur_time = 0\ncheckout_free = 0\nans = []\npeople.each do |p|\n if p > checkout_free\n ans << [p-1, p - checkout_free]\n end\n\n checkout_free = [checkout_free + t, p+t].min\nend\n\nputs ans.inspect\nputs ans.sort_by { |a| a[1] }[0][0]"}, {"source_code": "require 'set'\n\nstart, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }\nn = gets.to_i\nqueue = []\npeople = gets.split(' ').map { |i| i.to_i }\n\ncheckout_free = 0\nans = []\npeople.each do |p|\n if p >= checkout_free\n (ans << [p-1, [checkout_free - p + 1, start - p + 1].max]) if ((checkout_free + t < finish) && (p > 0))\n end\n\n checkout_free = [checkout_free + t, p+t, start+t].max\n #puts checkout_free\nend\n\n#puts ans.inspect\nputs ans.sort_by { |a| a[1] }[0][0]"}, {"source_code": "require 'set'\n\nstart, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }\nn = gets.to_i\nqueue = []\npeople = gets.split(' ').map { |i| i.to_i }\n\ncur_time = 0\ncheckout_free = 0\nans = []\npeople.each do |p|\n if p > checkout_free\n ans << [p-1, checkout_free - p + 1] if checkout_free - p + 1 + t < finish\n end\n\n checkout_free = [checkout_free + t, p+t].min\nend\n\n#puts ans.inspect\nputs ans.sort_by { |a| a[1] }[0][0]"}, {"source_code": "require 'set'\n\nstart, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }\nn = gets.to_i\nqueue = []\npeople = gets.split(' ').map { |i| i.to_i }\n\ncur_time = 0\ncheckout_free = 0\nans = []\npeople.each do |p|\n # p start,finish,t, p\n if p > checkout_free\n ans << [p-1, checkout_free - p]\n end\n checkout_free = [checkout_free + t, p+t].max\nend\n\nputs ans.sort_by { |a| a[1] }[0][0]"}, {"source_code": "require 'set'\n\nstart, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }\nn = gets.to_i\nqueue = []\npeople = gets.split(' ').map { |i| i.to_i }\n\ncheckout_free = 0\nans = []\npeople.each do |p|\n if p > checkout_free\n (ans << [p-1, [checkout_free - p + 1, start - p + 1].max]) if ((checkout_free + t < finish) && (p >= 0))\n end\n\n checkout_free = [checkout_free + t, p+t, start+t].max\n #puts checkout_free\nend\n\n#puts ans.inspect\nputs ans.sort_by { |a| a[1] }[0][0]"}, {"source_code": "require 'set'\n\nstart, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }\nn = gets.to_i\nqueue = []\npeople = gets.split(' ').map { |i| i.to_i }\n\ncheckout_free = 0\nans = []\npeople.each do |p|\n if p > checkout_free\n (ans << [p-1, [checkout_free - p + 1, start - p + 1].max]) if ((checkout_free + t < finish) && (p > 0))\n end\n\n checkout_free = [checkout_free + t, p+t, start+t].max\n #puts checkout_free\nend\n\n#puts ans.inspect\nputs ans.sort_by { |a| a[1] }[0][0]"}, {"source_code": "require 'set'\n\nstart, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }\nn = gets.to_i\nqueue = []\npeople = gets.split(' ').map { |i| i.to_i }\n\ncur_time = 0\ncheckout_free = 0\nans = []\npeople.each do |p|\n # p start,finish,t, p\n if p > checkout_free\n ans << [p-1, p - checkout_free]\n end\n checkout_free = [checkout_free + t, p+t].max\nend\n\nputs ans.sort_by { |a| a[1] }[0][0]"}, {"source_code": "require 'set'\n\nstart, finish, t = gets.chomp.split(' ').map{ |i| i.to_i }\nn = gets.to_i\nqueue = []\npeople = gets.split(' ').map { |i| i.to_i }\n\ncheckout_free = 0\nans = []\npeople.each do |p|\n if p > checkout_free\n (ans << [p-1, [checkout_free - p + 1, start - p + 1].max]) if ((checkout_free + t <= finish) && (p > 0))\n end\n\n checkout_free = [checkout_free + t, p+t, start+t].max\n #puts checkout_free\nend\n\n#puts ans.inspect\nputs ans.sort_by { |a| a[1] }[0][0]"}], "src_uid": "f169ed11206ccc6900b90d5e889ced27"} {"nl": {"description": "Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. . Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≤ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so. is the biggest non-negative number d such that d divides bi for every i (1 ≤ i ≤ n).", "input_spec": "The first line contains a single integer n (2 ≤ n ≤ 100 000) — length of sequence A. The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — elements of sequence A.", "output_spec": "Output on the first line \"YES\" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and \"NO\" (without quotes) otherwise. If the answer was \"YES\", output the minimal number of moves needed to make sequence A beautiful.", "sample_inputs": ["2\n1 1", "3\n6 2 4", "2\n1 3"], "sample_outputs": ["YES\n1", "YES\n0", "YES\n1"], "notes": "NoteIn the first example you can simply make one move to obtain sequence [0, 2] with .In the second example the gcd of the sequence is already greater than 1. "}, "positive_code": [{"source_code": "n = gets.strip.to_i\ndata = gets.strip.split.map(&:to_i).push(0)\n\nif data.reduce(:gcd) > 1\n puts \"YES\", 0\nelse\n ans = 0\n for i in (0...n)\n next if data[i] % 2 == 0\n ans += 2 - data[i+1] % 2\n data[i+1] = 0\n end\n puts \"YES\", ans\nend\n"}], "negative_code": [{"source_code": "n = gets.strip.to_i\ndata = gets.strip.split.map(&:to_i).push(0)\n\nif data.reduce(:gcd) > 1\n puts \"YES\", 0\nelse\n ans = 0\n puts data\n for i in (0...n)\n next if data[i] % 2 == 0\n ans += 2 - data[i+1] % 2\n data[i+1] = 0\n end\n puts \"YES\", ans\nend\n"}], "src_uid": "da38d1a63152e0a354b04936e9511969"} {"nl": {"description": "A palindrome is a string $$$t$$$ which reads the same backward as forward (formally, $$$t[i] = t[|t| + 1 - i]$$$ for all $$$i \\in [1, |t|]$$$). Here $$$|t|$$$ denotes the length of a string $$$t$$$. For example, the strings 010, 1001 and 0 are palindromes.You have $$$n$$$ binary strings $$$s_1, s_2, \\dots, s_n$$$ (each $$$s_i$$$ consists of zeroes and/or ones). You can swap any pair of characters any number of times (possibly, zero). Characters can be either from the same string or from different strings — there are no restrictions.Formally, in one move you: choose four integer numbers $$$x, a, y, b$$$ such that $$$1 \\le x, y \\le n$$$ and $$$1 \\le a \\le |s_x|$$$ and $$$1 \\le b \\le |s_y|$$$ (where $$$x$$$ and $$$y$$$ are string indices and $$$a$$$ and $$$b$$$ are positions in strings $$$s_x$$$ and $$$s_y$$$ respectively), swap (exchange) the characters $$$s_x[a]$$$ and $$$s_y[b]$$$. What is the maximum number of strings you can make palindromic simultaneously?", "input_spec": "The first line contains single integer $$$Q$$$ ($$$1 \\le Q \\le 50$$$) — the number of test cases. The first line on each test case contains single integer $$$n$$$ ($$$1 \\le n \\le 50$$$) — the number of binary strings you have. Next $$$n$$$ lines contains binary strings $$$s_1, s_2, \\dots, s_n$$$ — one per line. It's guaranteed that $$$1 \\le |s_i| \\le 50$$$ and all strings constist of zeroes and/or ones.", "output_spec": "Print $$$Q$$$ integers — one per test case. The $$$i$$$-th integer should be the maximum number of palindromic strings you can achieve simultaneously performing zero or more swaps on strings from the $$$i$$$-th test case.", "sample_inputs": ["4\n1\n0\n3\n1110\n100110\n010101\n2\n11111\n000001\n2\n001\n11100111"], "sample_outputs": ["1\n2\n2\n2"], "notes": "NoteIn the first test case, $$$s_1$$$ is palindrome, so the answer is $$$1$$$.In the second test case you can't make all three strings palindromic at the same time, but you can make any pair of strings palindromic. For example, let's make $$$s_1 = \\text{0110}$$$, $$$s_2 = \\text{111111}$$$ and $$$s_3 = \\text{010000}$$$.In the third test case we can make both strings palindromic. For example, $$$s_1 = \\text{11011}$$$ and $$$s_2 = \\text{100001}$$$.In the last test case $$$s_2$$$ is palindrome and you can make $$$s_1$$$ palindrome, for example, by swapping $$$s_1[2]$$$ and $$$s_1[3]$$$."}, "positive_code": [{"source_code": "readline.to_i.times do\n e = 1\n z = 0\n n = readline.to_i\n n.times do\n s = readline.chomp\n e = 0 if s.length.odd?\n z += s.count('0')\n end\n p(n-e*(z%2))\nend\n"}], "negative_code": [], "src_uid": "3b8678d118c90d34c99ffa9259cc611f"} {"nl": {"description": "An accordion is a string (yes, in the real world accordions are musical instruments, but let's forget about it for a while) which can be represented as a concatenation of: an opening bracket (ASCII code $$$091$$$), a colon (ASCII code $$$058$$$), some (possibly zero) vertical line characters (ASCII code $$$124$$$), another colon, and a closing bracket (ASCII code $$$093$$$). The length of the accordion is the number of characters in it.For example, [::], [:||:] and [:|||:] are accordions having length $$$4$$$, $$$6$$$ and $$$7$$$. (:|:), {:||:}, [:], ]:||:[ are not accordions. You are given a string $$$s$$$. You want to transform it into an accordion by removing some (possibly zero) characters from it. Note that you may not insert new characters or reorder existing ones. Is it possible to obtain an accordion by removing characters from $$$s$$$, and if so, what is the maximum possible length of the result?", "input_spec": "The only line contains one string $$$s$$$ ($$$1 \\le |s| \\le 500000$$$). It consists of lowercase Latin letters and characters [, ], : and |.", "output_spec": "If it is not possible to obtain an accordion by removing some characters from $$$s$$$, print $$$-1$$$. Otherwise print maximum possible length of the resulting accordion.", "sample_inputs": ["|[a:b:|]", "|]:[|:]"], "sample_outputs": ["4", "-1"], "notes": null}, "positive_code": [{"source_code": "s=gets\nbegin\nidx=s.index('[')\ns=s[idx+1..-1]\nidx=s.index(':')\ns=s[idx+1..-1]\nidx=s.rindex(']')\ns=s[0...idx]\nidx=s.rindex(':')\ns=s[0...idx]\np s.count('|')+4\nrescue\np -1\nend\n__END__\np gets=~/\\[.*?\\:(.*)\\:.*?\\]/ ? $1.count(?|)+4 : -1"}, {"source_code": "def solve(s)\n s.shift until s[0] == '[' or s.size.zero?\n s.reverse!\n s.shift until s[0] == ']' or s.size.zero?\n s.reverse!\n return -1 if s.size < 4\n res = ['[', ':'].map do |c|\n s.index(c)\n end.concat(\n [':', ']'].map do |c|\n s.rindex(c)\n end\n )\n return -1 if res.include?(nil) || res.uniq.size != res.size\n res.each_with_index do |e, i|\n break if i == res.size - 1\n return -1 if e >= res[i+1]\n end\n s[res[1]..res[2]].count('|') + 4\nend\n\nputs solve(gets.strip.split(''))"}, {"source_code": "# Input\ns = gets\n\n# Processing\no_bracket = nil\no_colon = nil\n\ns.each_char.with_index do |chr, index|\n case chr\n when \"[\"\n o_bracket = index\n when \":\"\n if o_bracket\n o_colon = index\n break\n end\n else\n end\nend\n\nc_bracket = nil\nc_colon = nil\ns.each_char.reverse_each.with_index do |chr, index|\n case chr\n when \"]\"\n c_bracket = s.length - 1 - index\n when \":\"\n if c_bracket\n c_colon = s.length - 1 - index\n break\n end\n else\n end\nend\n\nunless o_colon && c_colon && o_colon < c_colon\n puts -1\n exit\nend\n\ninner = s.slice(o_colon + 1, c_colon - o_colon - 1)\nresult = 4 + inner.count(\"|\")\n\n# Output\nputs result\n"}, {"source_code": "DBG = !true\ns = gets.chomp\nputs \"s: #{s}\" if DBG\nn = s.size\nleftpar = -1\nfor i in 0...n\n if s[i] == '['\n leftpar = i\n break\n end\nend\nif leftpar == -1\n puts \"no leftpar\" if DBG\n puts \"-1\"\n exit\nend\n\nleftcol = -1\nfor i in (leftpar+1)...n\n if s[i] == ':'\n leftcol = i\n break\n end\nend\nif leftcol == -1\n puts \"no leftcol\" if DBG\n puts \"-1\"\n exit\nelse\n puts \"leftcol #{leftcol}\" if DBG\nend\n\nrightpar = -1\n(n-1).downto(leftcol+2) do |i|\n if s[i] == ']'\n rightpar = i\n break\n end\nend\nif rightpar == -1\n puts \"no rightpar\" if DBG\n puts \"-1\"\n exit\nend\n\nrightcol = -1\n(rightpar-1).downto(leftcol+1) do |i|\n if s[i] == ':'\n rightcol = i\n break\n end\nend\nif rightcol == -1\n puts \"no rightcol\" if DBG\n puts \"-1\"\n exit\nend\n\ncnt = 0\nfor i in (leftcol+1)..(rightcol-1)\n cnt += 1 if s[i] == '|'\nend\nputs \"#{cnt+4}\"\n"}, {"source_code": "# revisar que si haya al menos un acordeon\n # contar apertura [:\n # contar numero de pipes despúes\n # contar ultima :] (desde atrás maybe)\n\ndef accordion_length(s)\n open_bracket = false\n close_bracket = false\n first_colon_at = nil\n second_colon_at = nil\n pipe_counter = 0\n\n s.split('').each_with_index do |char, index|\n if char == '[' && !open_bracket\n open_bracket = true\n next\n end\n\n if char == ':' && open_bracket\n first_colon_at = index\n break\n end\n end\n\n return -1 unless first_colon_at\n\n (s.length-1).downto(first_colon_at + 1).each do |i|\n if s[i] == ']' && !close_bracket\n close_bracket = true\n next\n end\n\n if s[i] == ':' && close_bracket\n second_colon_at = i\n break\n end\n end\n\n return -1 unless second_colon_at\n\n s[first_colon_at..second_colon_at].split('').each do |char|\n pipe_counter += 1 and next if char == '|'\n end\n\n pipe_counter + 4\nend\n\ns = gets.rstrip\n\nresult = accordion_length s\nputs result\n\n\n"}], "negative_code": [{"source_code": "def solve(s)\n return -1 if s.size < 4\n res = ['[', ':'].map do |c|\n s.index(c)\n end.concat(\n [':', ']'].map do |c|\n s.rindex(c)\n end\n )\n return -1 if res.include?(nil) || res.uniq.size != res.size\n res.each_with_index do |e, i|\n break if i == res.size - 1\n return -1 if e >= res[i+1]\n end\n s[res[1]..res[2]].count('|') + 4\nend\n\nputs solve(gets.strip.split(''))"}, {"source_code": "DBG = !true\ns = gets.chomp\nputs \"s: #{s}\" if DBG\nn = s.size\nleftpar = -1\nfor i in 0...n\n if s[i] == '['\n leftpar = i\n break\n end\nend\nif leftpar == -1\n puts \"no leftpar\" if DBG\n puts \"-1\"\n exit\nend\n\nleftcol = -1\nfor i in (leftpar+1)...n\n if s[i] == ':'\n leftcol = i\n break\n end\nend\nif leftcol == -1\n puts \"no leftcol\" if DBG\n puts \"-1\"\n exit\nelse\n puts \"leftcol #{leftcol}\" if DBG\nend\n\nrightpar = -1\n(n-1).downto(leftcol+2) do |i|\n if s[i] == ']'\n rightpar = i\n break\n end\nend\nif rightpar == -1\n puts \"no rightpar\" if DBG\n puts \"-1\"\n exit\nend\n\nrightcol = -1\n(rightpar-1).downto(rightcol+1) do |i|\n if s[i] == ':'\n rightcol = i\n break\n end\nend\nif rightcol == -1\n puts \"no rightcol\" if DBG\n puts \"-1\"\n exit\nend\n\ncnt = 0\nfor i in (leftcol+1)..(rightcol-1)\n cnt += 1 if s[i] == '|'\nend\nputs \"#{cnt+4}\"\n"}, {"source_code": "DBG = !true\ns = gets.chomp\nputs \"s: #{s}\" if DBG\nn = s.size\nleftpar = -1\nfor i in 0...n\n if s[i] == '['\n leftpar = i\n break\n end\nend\nif leftpar == -1\n puts \"no leftpar\" if DBG\n puts \"-1\"\n exit\nend\n\nleftcol = -1\nfor i in (leftpar+1)...n\n if s[i] == ':'\n leftcol = i\n break\n end\nend\nif leftcol == -1\n puts \"no leftcol\" if DBG\n puts \"-1\"\n exit\nelse\n puts \"leftcol #{leftcol}\" if DBG\nend\n\nrightpar = -1\n(n-1).downto(leftcol+2) do |i|\n if s[i] == ']'\n rightpar = i\n break\n end\nend\nif rightpar == -1\n puts \"no rightpar\" if DBG\n puts \"-1\"\n exit\nend\n\nrightcol = -1\n(n-1).downto(rightcol+1) do |i|\n if s[i] == ':'\n rightcol = i\n break\n end\nend\nif rightcol == -1\n puts \"no rightcol\" if DBG\n puts \"-1\"\n exit\nend\n\ncnt = 0\nfor i in (leftcol+1)..(rightcol-1)\n cnt += 1 if s[i] == '|'\nend\nputs \"#{cnt+4}\"\n"}, {"source_code": "def accordion_length(s)\n opened = false\n colon_counter = 0\n accordion_length = 0\n accordion = false\n\n s.split('').each do |char|\n if char == '[' && !opened\n accordion_length += 1\n opened = true\n next\n end\n\n if char == ':' && opened && colon_counter < 2\n accordion_length += 1\n colon_counter += 1\n next\n end\n\n if char == '|' && colon_counter == 1\n accordion_length += 1\n next\n end\n\n if char == ']' && colon_counter == 2\n accordion_length += 1\n accordion = true\n break\n end\n end\n\n return -1 unless accordion\n accordion_length\nend\n\ns = gets.rstrip\n\nresult = accordion_length s\nputs result"}, {"source_code": "def accordion_length(s)\n opened = false\n colon_counter = 0\n accordion_length = 0\n accordion = false\n\n s.split('').each do |char|\n if char == '[' && !opened\n accordion_length += 1\n opened = true\n next\n end\n\n if char == ':' && opened && colon_counter < 2\n accordion_length += 1\n colon_counter += 1\n next\n end\n\n if char == '|' && colon_counter == 1\n accordion_length += 1\n next\n end\n\n if char == ']' && colon_counter == 2\n accordion_length += 1\n accordion = true\n end\n end\n\n return -1 unless accordion\n accordion_length\nend\n\ns = gets.rstrip\n\nresult = accordion_length s\nputs result"}], "src_uid": "915b4776a6b1fa15886247eb1ad40b60"} {"nl": {"description": "Let's define a number ebne (even but not even) if and only if its sum of digits is divisible by $$$2$$$ but the number itself is not divisible by $$$2$$$. For example, $$$13$$$, $$$1227$$$, $$$185217$$$ are ebne numbers, while $$$12$$$, $$$2$$$, $$$177013$$$, $$$265918$$$ are not. If you're still unsure what ebne numbers are, you can look at the sample notes for more clarification.You are given a non-negative integer $$$s$$$, consisting of $$$n$$$ digits. You can delete some digits (they are not necessary consecutive/successive) to make the given number ebne. You cannot change the order of the digits, that is, after deleting the digits the remaining digits collapse. The resulting number shouldn't contain leading zeros. You can delete any number of digits between $$$0$$$ (do not delete any digits at all) and $$$n-1$$$.For example, if you are given $$$s=$$$222373204424185217171912 then one of possible ways to make it ebne is: 222373204424185217171912 $$$\\rightarrow$$$ 2237344218521717191. The sum of digits of 2237344218521717191 is equal to $$$70$$$ and is divisible by $$$2$$$, but number itself is not divisible by $$$2$$$: it means that the resulting number is ebne.Find any resulting number that is ebne. If it's impossible to create an ebne number from the given number report about it.", "input_spec": "The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 3000$$$)  — the number of digits in the original number. The second line of each test case contains a non-negative integer number $$$s$$$, consisting of $$$n$$$ digits. It is guaranteed that $$$s$$$ does not contain leading zeros and the sum of $$$n$$$ over all test cases does not exceed $$$3000$$$.", "output_spec": "For each test case given in the input print the answer in the following format: If it is impossible to create an ebne number, print \"-1\" (without quotes); Otherwise, print the resulting number after deleting some, possibly zero, but not all digits. This number should be ebne. If there are multiple answers, you can print any of them. Note that answers with leading zeros or empty strings are not accepted. It's not necessary to minimize or maximize the number of deleted digits.", "sample_inputs": ["4\n4\n1227\n1\n0\n6\n177013\n24\n222373204424185217171912"], "sample_outputs": ["1227\n-1\n17703\n2237344218521717191"], "notes": "NoteIn the first test case of the example, $$$1227$$$ is already an ebne number (as $$$1 + 2 + 2 + 7 = 12$$$, $$$12$$$ is divisible by $$$2$$$, while in the same time, $$$1227$$$ is not divisible by $$$2$$$) so we don't need to delete any digits. Answers such as $$$127$$$ and $$$17$$$ will also be accepted.In the second test case of the example, it is clearly impossible to create an ebne number from the given number.In the third test case of the example, there are many ebne numbers we can obtain by deleting, for example, $$$1$$$ digit such as $$$17703$$$, $$$77013$$$ or $$$17013$$$. Answers such as $$$1701$$$ or $$$770$$$ will not be accepted as they are not ebne numbers. Answer $$$013$$$ will not be accepted as it contains leading zeroes.Explanation: $$$1 + 7 + 7 + 0 + 3 = 18$$$. As $$$18$$$ is divisible by $$$2$$$ while $$$17703$$$ is not divisible by $$$2$$$, we can see that $$$17703$$$ is an ebne number. Same with $$$77013$$$ and $$$17013$$$; $$$1 + 7 + 0 + 1 = 9$$$. Because $$$9$$$ is not divisible by $$$2$$$, $$$1701$$$ is not an ebne number; $$$7 + 7 + 0 = 14$$$. This time, $$$14$$$ is divisible by $$$2$$$ but $$$770$$$ is also divisible by $$$2$$$, therefore, $$$770$$$ is not an ebne number.In the last test case of the example, one of many other possible answers is given. Another possible answer is: 222373204424185217171912 $$$\\rightarrow$$$ 22237320442418521717191 (delete the last digit)."}, "positive_code": [{"source_code": "T = gets.to_i\n\nT.times do\n n = gets.to_i\n s = gets.chomp.chars.map(&:to_i)\n\n odds = []\n\n s.each do |d|\n next if d.even?\n\n odds << d\n end\n\n if odds.size <= 1\n puts -1\n else\n puts odds.take(2).join\n end\nend\n"}, {"source_code": "# coding: utf-8\n\n# Even but not even\n# Sum(list_of_digits).even? == true\n# original_number.even? == false\nt = gets.to_i\nt.times {\n n = gets.to_i\n ar = gets.chomp.split(//).map(&:to_i)\n ar.delete_if { |i| i.even? }\n\n if ar.length <= 1\n puts \"-1\"\n else\n if ar.length.odd?\n ar.pop\n end\n\n puts ar.join('')\n end\n\n}"}, {"source_code": "t = gets.to_i\nodd = [\"1\",\"3\",\"5\",\"7\",\"9\"]\neven = [\"2\",\"4\",\"6\",\"8\",\"0\"]\n\nt.times{\n n = gets.to_i\n s = gets.chomp\n sc = s.chars.map(&:to_i)\n sc.pop while sc != [] && sc.last.even?\n if sc == []\n p -1\n #puts \"because sc==[]\"\n next\n end\n n_odd = sc.count{|i|i.odd?}\n #puts \"s: #{s}, n_odd: #{n_odd}\"\n flag = false\n if n_odd.odd?\n #puts \"n_odd is odd\"\n [*0..sc.length-1].reverse.each{|i|\n #puts \"loop yeah!!\"\n if sc[i].odd? && flag\n sc.delete_at(i)\n break\n end\n flag = true if sc[i].odd?\n }\n end\n n_odd = sc.count{|i|i.odd?}\n if n_odd.odd?\n p -1\n #puts \"because n_odd.odd?\"\n next\n end\n if sc == [0]\n p -1\n #puts \"because sc==[0]\"\n else\n puts sc.join\n end\n}\n\n\n"}], "negative_code": [{"source_code": "# coding: utf-8\n\n# Even but not even\n# Sum(list_of_digits).even? == true\n# original_number.even? == false\nt = gets.to_i\nt.times {\n n = gets.to_i\n odds = []\n gets.chomp.split(//).each { |c|\n i = c.to_i\n if i.odd?\n odds << i\n end\n }\n if odds.length <= 1\n puts \"-1\"\n else\n puts odds.join('')\n end\n}"}, {"source_code": "# coding: utf-8\n\n# Even but not even\n# Sum(list_of_digits).even? == true\n# original_number.even? == false\nt = gets.to_i\nt.times {\n n = gets.to_i\n cr = Array.new(10+1) { 0 }\n gets.chomp.split(//).each { |c|\n i = c.to_i\n cr[i] += 1\n }\n odds_unique = {}\n for ci in 1..10\n if ci.odd?\n if cr[ci] > 0\n odds_unique[ci] = true\n end\n end\n end\n\n if odds_unique.length <= 1\n puts \"-1\"\n else\n puts (odds_unique.keys[0..1]).join('')\n end\n}"}], "src_uid": "8f00837e04627e445dfb8b6cd0216640"} {"nl": {"description": "Initially there was an array $$$a$$$ consisting of $$$n$$$ integers. Positions in it are numbered from $$$1$$$ to $$$n$$$.Exactly $$$q$$$ queries were performed on the array. During the $$$i$$$-th query some segment $$$(l_i, r_i)$$$ $$$(1 \\le l_i \\le r_i \\le n)$$$ was selected and values of elements on positions from $$$l_i$$$ to $$$r_i$$$ inclusive got changed to $$$i$$$. The order of the queries couldn't be changed and all $$$q$$$ queries were applied. It is also known that every position from $$$1$$$ to $$$n$$$ got covered by at least one segment.We could have offered you the problem about checking if some given array (consisting of $$$n$$$ integers with values from $$$1$$$ to $$$q$$$) can be obtained by the aforementioned queries. However, we decided that it will come too easy for you.So the enhancement we introduced to it is the following. Some set of positions (possibly empty) in this array is selected and values of elements on these positions are set to $$$0$$$.Your task is to check if this array can be obtained by the aforementioned queries. Also if it can be obtained then restore this array.If there are multiple possible arrays then print any of them.", "input_spec": "The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 \\le n, q \\le 2 \\cdot 10^5$$$) — the number of elements of the array and the number of queries perfomed on it. The second line contains $$$n$$$ integer numbers $$$a_1, a_2, \\dots, a_n$$$ ($$$0 \\le a_i \\le q$$$) — the resulting array. If element at some position $$$j$$$ is equal to $$$0$$$ then the value of element at this position can be any integer from $$$1$$$ to $$$q$$$.", "output_spec": "Print \"YES\" if the array $$$a$$$ can be obtained by performing $$$q$$$ queries. Segments $$$(l_i, r_i)$$$ $$$(1 \\le l_i \\le r_i \\le n)$$$ are chosen separately for each query. Every position from $$$1$$$ to $$$n$$$ should be covered by at least one segment. Otherwise print \"NO\". If some array can be obtained then print $$$n$$$ integers on the second line — the $$$i$$$-th number should be equal to the $$$i$$$-th element of the resulting array and should have value from $$$1$$$ to $$$q$$$. This array should be obtainable by performing exactly $$$q$$$ queries. If there are multiple possible arrays then print any of them.", "sample_inputs": ["4 3\n1 0 2 3", "3 10\n10 10 10", "5 6\n6 5 6 2 2", "3 5\n0 0 0"], "sample_outputs": ["YES\n1 2 2 3", "YES\n10 10 10", "NO", "YES\n5 4 2"], "notes": "NoteIn the first example you can also replace $$$0$$$ with $$$1$$$ but not with $$$3$$$.In the second example it doesn't really matter what segments to choose until query $$$10$$$ when the segment is $$$(1, 3)$$$.The third example showcases the fact that the order of queries can't be changed, you can't firstly set $$$(1, 3)$$$ to $$$6$$$ and after that change $$$(2, 2)$$$ to $$$5$$$. The segment of $$$5$$$ should be applied before segment of $$$6$$$.There is a lot of correct resulting arrays for the fourth example."}, "positive_code": [{"source_code": "N, Q = gets.split.map(&:to_i)\nas = gets.split.map(&:to_i)\nexist_max = as.include?(Q)\nused = []\nstack = []\n\nN.times do |i|\n if as[i] == 0\n if exist_max\n as[i] = i==0 ? 1 : as[i-1]\n else\n as[i] = Q\n exist_max = true\n end\n next\n end\n\n if used[as[i]]\n puts 'NO'\n exit\n end\n\n while stack.length > 0 && stack[-1] > as[i]\n j = stack.pop\n used[j] = true\n end\n stack.push(as[i]) if stack[-1] != as[i]\nend\n\nunless exist_max\n puts 'NO'\n exit\nend\n\nputs 'YES'\nputs as.join(' ')"}], "negative_code": [{"source_code": "N, Q = gets.split.map(&:to_i)\nas = gets.split.map(&:to_i)\nexist_max = as.include?(Q)\nfound = []\n\nN.times do |i|\n if as[i] == 0\n if exist_max\n as[i] = i==0 ? 1 : as[i-1]\n else\n as[i] = Q\n exist_max = true\n end\n next\n end\n if i > 0 && as[i-1] < as[i]\n if found[as[i]]\n puts 'NO'\n exit\n end\n end\n found[as[i]] = true\nend\n\nunless exist_max\n puts 'NO'\n exit\nend\n\nputs 'YES'\nputs as.join(' ')"}], "src_uid": "e0450f2644494c92ec0d9ea3ab9d0056"} {"nl": {"description": "Levko loves permutations very much. A permutation of length n is a sequence of distinct positive integers, each is at most n.Let’s assume that value gcd(a, b) shows the greatest common divisor of numbers a and b. Levko assumes that element pi of permutation p1, p2, ... , pn is good if gcd(i, pi) > 1. Levko considers a permutation beautiful, if it has exactly k good elements. Unfortunately, he doesn’t know any beautiful permutation. Your task is to help him to find at least one of them.", "input_spec": "The single line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ n).", "output_spec": "In a single line print either any beautiful permutation or -1, if such permutation doesn’t exist. If there are multiple suitable permutations, you are allowed to print any of them.", "sample_inputs": ["4 2", "1 1"], "sample_outputs": ["2 4 3 1", "-1"], "notes": "NoteIn the first sample elements 4 and 3 are good because gcd(2, 4) = 2 > 1 and gcd(3, 3) = 3 > 1. Elements 2 and 1 are not good because gcd(1, 2) = 1 and gcd(4, 1) = 1. As there are exactly 2 good elements, the permutation is beautiful.The second sample has no beautiful permutations."}, "positive_code": [{"source_code": "n,k=gets.split.map(&:to_i)\nputs (n==k ? -1:(2..n).to_a.insert(n-k-1,1).join(' '))\n"}, {"source_code": "n,k=gets.split.map(&:to_i)\nputs (n==k ? -1:(2..n).to_a.insert(n-k-1,1).join(' '))\n"}, {"source_code": "n, k = gets.split.map{|s| s.to_i}\n\nif n==k\n\tputs -1\n\texit\nend\n\nseq = Array.new(n+1) { |i| i }\nprimes = [2]\nnn = 2\ndif = n-1-k\nwhile dif > 1\n\tseq[nn], seq[nn+1] = seq[nn+1], seq[nn]\n\tnn+=2\n\tdif -= 2\nend\nif dif == 1\n\tseq[1] = nn\n\tseq[nn] = 1\nend\nseq[1..-1].each{|i| print \"#{i} \"}\n"}, {"source_code": "n,k=gets.split.map(&:to_i)\nputs (n==k ? -1:(2..n).to_a.insert(n-k-1,1).join(' '))"}, {"source_code": "n,k=gets.chomp.split.map(&:to_i)\nif n==k\n\tputs \"-1\";exit\nend\nprint n-k\n(n-k-1).times{|i| print \" #{i+1}\"}\nfor i in n-k+1..n\n\tprint \" #{i}\"\nend\nputs"}, {"source_code": "class B\n def initialize\n n, k = gets.chomp.split(' ').map(&:to_i)\n count = n-1\n index = 1\n\n answer = (1..n).to_a\n\n if n <= k\n puts -1\n exit\n end\n\n if ( (count.odd? && k.even?) || (count.even? && k.odd? ) )\n answer[0], answer[1] = answer[1], answer[0]\n count -= 1\n index = 2\n end\n\n while count != k && index < n\n answer[index], answer[index+1] = answer[index+1], answer[index]\n count -= 2\n index += 2\n end\n\n if count == k\n puts answer.join(' ')\n else\n puts -1\n end\n end\nend\n\nb = B.new"}, {"source_code": "n,k=gets.split.map(&:to_i)\nputs (n==k ? -1:(2..n).to_a.insert(n-k-1,1).join(' '))\n"}, {"source_code": "n,k=gets.split.map(&:to_i)\nputs (n==k ? -1:(2..n).to_a.insert(n-k-1,1).join(' '))\n"}, {"source_code": "n, k = gets.strip.split(' ').map(&:to_i)\n\nif n == k\n puts \"-1\"\n exit\nend\n\narr = (1..n).to_a\nck = n - 1\narr.each_with_index { |e , i|\n break if ck == k\n arr[0], arr[n - i - 1] = arr[n - i - 1], arr[0]\n ck -= 1\n}\n\nputs arr.join(' ')\n"}, {"source_code": "n,k=gets.split.map(&:to_i)\nputs (n==k ? -1:(2..n).to_a.insert(n-k-1,1).join(' '))\n"}, {"source_code": "n,k = gets.split(\" \").map(&:to_i)\nbad = n - k\nperm = (2..bad).to_a\nperm.push(1)\nperm.push(*((bad+1)..(bad+k)))\nif n == k\n\tputs \"-1\"\nelse\nputs perm.join(\" \")\nend"}, {"source_code": "n, k = gets.chomp.split(' ').collect {|x| x.to_i}\n\nq = n-k\n\nif q < 1\n puts -1\nelsif q == 1\n puts (1..n).to_a.join ' '\nelse\n arr = (1..(q-1)).to_a\n arr = [q] + arr\n arr = arr + ((q+1)..n).to_a\n puts arr.join ' '\nend"}, {"source_code": "n,k=gets.split.map(&:to_i)\nputs (n==k ? -1:(2..n).to_a.insert(n-k-1,1).join(' '))"}, {"source_code": "n,k=gets.split.map(&:to_i)\nputs (n==k ? -1:(2..n).to_a.insert(n-k-1,1).join(' '))\n"}, {"source_code": "n, k = gets.split.map { |x| x.to_i }\nif n == k\n puts -1\n exit\nend\n\na = Array.new(n)\n((k + n) % 2 == 0 ? [n - k...n, 1..n - k] : [0..k, k + 2...n]).\n zip([1, 2], [lambda { |i| a[i] = i + 1 }, lambda { |i| a[i - 1] = i + 1; a[i] = i }]) { |r, st, f| r.step st, &f }\nputs a * ' '"}, {"source_code": "line = gets.chomp.split(\" \").map { |e| e.to_i }\nn = line[0]\nk = line[1]\n\nif n == k\n puts \"-1\"\nelsif n == k + 1\n for i in 1..n\n print(i.to_s + \" \")\n end\n puts\nelse\n print((k+2).to_s + \" \")\n for i in 2..n\n if i != k+2\n print(i.to_s + \" \")\n end\n end\n print(\"1\\n\")\nend\n"}, {"source_code": "n,k=gets.split.map(&:to_i)\nputs (n==k ? -1:(2..n).to_a.insert(n-k-1,1).join(' '))\n"}, {"source_code": "n,k=gets.split.map(&:to_i)\nputs (n==k ? -1:(2..n).to_a.insert(n-k-1,1).join(' '))\n"}], "negative_code": [{"source_code": "class B\n def initialize\n n, k = gets.chomp.split(' ').map(&:to_i)\n count = n-1\n\n answer = (1..n).to_a\n\n if n <= k\n puts -1\n exit\n end\n\n if k.even?\n answer[0], answer[1] = answer[1], answer[0]\n count -= 1\n end\n\n index = 2\n while count != k && index < n\n answer[index], answer[index+1] = answer[index+1], answer[index]\n count -= 2\n index += 2\n end\n\n if count == k\n puts answer.join(' ')\n else\n puts -1\n end\n end\nend\n\nb = B.new\n"}, {"source_code": "class B\n def initialize\n n, k = gets.chomp.split(' ').map(&:to_i)\n count = n-1\n index = 1\n\n answer = (1..n).to_a\n\n if n <= k\n puts -1\n exit\n end\n\n if !k.zero? && ( (count.odd? && k.even?) || (count.even? && k.odd? ) )\n answer[0], answer[1] = answer[1], answer[0]\n count -= 1\n index = 2\n end\n\n while count != k && index < n\n answer[index], answer[index+1] = answer[index+1], answer[index]\n count -= 2\n index += 2\n end\n\n if count == k\n puts answer.join(' ')\n else\n puts -1\n end\n end\nend\n\nb = B.new"}, {"source_code": "class B\n def initialize\n n, k = gets.chomp.split(' ').map(&:to_i)\n count = n-1\n index = 1\n\n answer = (1..n).to_a\n\n if n <= k\n puts -1\n exit\n end\n\n if !k.zero? && ( (count.odd? && k.even?) || (count.even? && k.odd? ) )\n answer[0], answer[1] = answer[1], answer[0]\n count -= 1\n index = 2\n end\n\n while count != k && index < n\n answer[index], answer[index+1] = answer[index+1], answer[index]\n count -= 2\n index += 2\n end\n\n if count == k\n puts answer.join(' ')\n else\n puts -1\n end\n end\nend\n\nb = B.new"}, {"source_code": "n, k = gets.strip.split(' ').map(&:to_i)\n\nif n == k\n puts \"-1\"\n exit\nend\n\narr = (1..n).to_a\nck = n - 1\narr.each_with_index { |e , i|\n break if ck == k\n i += 1 if i.odd?\n arr[i], arr[n - 1] = arr[n - 1], arr[i]\n ck -= 1\n}\n\nputs arr.join(' ')\n"}, {"source_code": "n,k = gets.split(\" \").map(&:to_i)\nbad = n - k\nperm = (2..bad).to_a\nperm.push(1)\nperm.push(*((bad+1)..(bad+k)))\nif n == 1\n\tputs \"-1\"\nelse\nputs perm.join(\" \")\nend"}, {"source_code": "n,k = gets.split(\" \").map(&:to_i)\nbad = n - k\nperm = (2..bad).to_a\nperm.push(1)\nperm.push(*((bad+1)..(bad+k)))\nif n == 1 && k == 1\n\tputs \"-1\"\nelse\nputs perm.join(\" \")\nend"}, {"source_code": "n,k = gets.split(\" \").map(&:to_i)\nbad = n - k\nperm = (1..bad).to_a\nperm.push(*((bad+2)..(bad+k)))\nperm.push(bad+1)\nif n == 1\n\tputs \"-1\"\nelse\nputs perm.join(\" \")\nend"}], "src_uid": "dc548fe1d8683b4b0ee4e0fa67638185"} {"nl": {"description": "You play your favourite game yet another time. You chose the character you didn't play before. It has $$$str$$$ points of strength and $$$int$$$ points of intelligence. Also, at start, the character has $$$exp$$$ free experience points you can invest either in strength or in intelligence (by investing one point you can either raise strength by $$$1$$$ or raise intelligence by $$$1$$$).Since you'd like to make some fun you want to create a jock character, so it has more strength than intelligence points (resulting strength is strictly greater than the resulting intelligence).Calculate the number of different character builds you can create (for the purpose of replayability) if you must invest all free points. Two character builds are different if their strength and/or intellect are different.", "input_spec": "The first line contains the single integer $$$T$$$ ($$$1 \\le T \\le 100$$$) — the number of queries. Next $$$T$$$ lines contain descriptions of queries — one per line. This line contains three integers $$$str$$$, $$$int$$$ and $$$exp$$$ ($$$1 \\le str, int \\le 10^8$$$, $$$0 \\le exp \\le 10^8$$$) — the initial strength and intelligence of the character and the number of free points, respectively.", "output_spec": "Print $$$T$$$ integers — one per query. For each query print the number of different character builds you can create.", "sample_inputs": ["4\n5 3 4\n2 1 0\n3 5 5\n4 10 6"], "sample_outputs": ["3\n1\n2\n0"], "notes": "NoteIn the first query there are only three appropriate character builds: $$$(str = 7, int = 5)$$$, $$$(8, 4)$$$ and $$$(9, 3)$$$. All other builds are either too smart or don't use all free points.In the second query there is only one possible build: $$$(2, 1)$$$.In the third query there are two appropriate builds: $$$(7, 6)$$$, $$$(8, 5)$$$.In the fourth query all builds have too much brains."}, "positive_code": [{"source_code": "gets.to_i.times do\n s,t,e = gets.split.map(&:to_i)\n if s+e <= t\n puts 0\n else\n b = (t+e-s)/2\n puts e-(b > -2 ? b : -1)\n end\nend"}, {"source_code": "t = STDIN.readline.to_i\n\nt.times {\n str, int, exp = STDIN.readline.split.map &:to_i\n if str+exp <= int\n p 0\n next\n end\n\n if (d = int-str) > 0\n str = int\n exp -= d\n end\n\n int += exp\n i = (0..exp).bsearch do |e|\n str+e > int-e\n end\n p exp-i+1\n}\n"}, {"source_code": "tt = gets.chomp.to_i\ntt.times.map do\n str, int, exp = gets.split.map(&:to_i)\n diff = str - int\n\n cnt = 0\n if diff<0\n exp += diff\n if exp<=0\n else\n cnt+=(exp+1)/2\n end\n elsif diff==0\n cnt+=(exp+1)/2\n else\n if exp < diff\n cnt+=exp+1\n else\n cnt+=(exp+diff+1)/2\n end\n end\n\n puts cnt\nend\n"}, {"source_code": "t = gets.chomp.to_i\n\nt.times do |i|\n str, int, exp = gets.chomp.split.map(&:to_i)\n diff = str + exp - int\n if diff <= 0\n puts 0\n else\n puts [(diff+1)/2, exp+1].min\n end\nend\n"}], "negative_code": [{"source_code": "gets.to_i.times do\n s,t,e = gets.split.map(&:to_i)\n if s+e <= t\n puts 0\n else\n puts s+e-(s+(t+e-s)/2)\n end\nend"}, {"source_code": "gets.to_i.times do\n s,t,e = gets.split.map(&:to_i)\n if s+e <= t\n puts 0\n else\n b = (t+e-s)/2\n if e > 0\n puts e-(b > 0 ? b : 0)\n else\n puts 1\n end\n end\nend"}, {"source_code": "t = gets.chomp.to_i\n\nt.times do |i|\n str, int, exp = gets.chomp.split.map(&:to_i)\n # str + a > int + b\n # a + b = exp\n # str + exp > int + 2b\n # 2b <= str + exp - int + 1\n # b >= 0\n diff = [str + exp - int + 1, 0].max\n puts diff/2\nend\n"}, {"source_code": "t = gets.chomp.to_i\n\nt.times do |i|\n str, int, exp = gets.chomp.split.map(&:to_i)\n cut = (exp+int-str)/2 + 1\n puts [exp-cut+1, 0].max\nend\n"}, {"source_code": "t = gets.chomp.to_i\n\nt.times do |i|\n str, int, exp = gets.chomp.split.map(&:to_i)\n diff = str + exp - int\n if diff <= 0\n puts 0\n else\n puts (diff+1)/2\n end\nend\n"}], "src_uid": "0816295355375a2d3f1cd45852b86360"} {"nl": {"description": "There were $$$n$$$ types of swords in the theater basement which had been used during the plays. Moreover there were exactly $$$x$$$ swords of each type. $$$y$$$ people have broken into the theater basement and each of them has taken exactly $$$z$$$ swords of some single type. Note that different people might have taken different types of swords. Note that the values $$$x, y$$$ and $$$z$$$ are unknown for you.The next morning the director of the theater discovers the loss. He counts all swords — exactly $$$a_i$$$ swords of the $$$i$$$-th type are left untouched.The director has no clue about the initial number of swords of each type in the basement, the number of people who have broken into the basement and how many swords each of them have taken.For example, if $$$n=3$$$, $$$a = [3, 12, 6]$$$ then one of the possible situations is $$$x=12$$$, $$$y=5$$$ and $$$z=3$$$. Then the first three people took swords of the first type and the other two people took swords of the third type. Note that you don't know values $$$x, y$$$ and $$$z$$$ beforehand but know values of $$$n$$$ and $$$a$$$.Thus he seeks for your help. Determine the minimum number of people $$$y$$$, which could have broken into the theater basement, and the number of swords $$$z$$$ each of them has taken.", "input_spec": "The first line of the input contains one integer $$$n$$$ $$$(2 \\le n \\le 2 \\cdot 10^{5})$$$ — the number of types of swords. The second line of the input contains the sequence $$$a_1, a_2, \\dots, a_n$$$ $$$(0 \\le a_i \\le 10^{9})$$$, where $$$a_i$$$ equals to the number of swords of the $$$i$$$-th type, which have remained in the basement after the theft. It is guaranteed that there exists at least one such pair of indices $$$(j, k)$$$ that $$$a_j \\neq a_k$$$.", "output_spec": "Print two integers $$$y$$$ and $$$z$$$ — the minimum number of people which could have broken into the basement and the number of swords each of them has taken.", "sample_inputs": ["3\n3 12 6", "2\n2 9", "7\n2 1000000000 4 6 8 4 2", "6\n13 52 0 13 26 52"], "sample_outputs": ["5 3", "1 7", "2999999987 2", "12 13"], "notes": "NoteIn the first example the minimum value of $$$y$$$ equals to $$$5$$$, i.e. the minimum number of people who could have broken into the basement, is $$$5$$$. Each of them has taken $$$3$$$ swords: three of them have taken $$$3$$$ swords of the first type, and two others have taken $$$3$$$ swords of the third type.In the second example the minimum value of $$$y$$$ is $$$1$$$, i.e. the minimum number of people who could have broken into the basement, equals to $$$1$$$. He has taken $$$7$$$ swords of the first type."}, "positive_code": [{"source_code": "n = gets.chomp.to_i\narr = gets.chomp.split(' ').map(&:to_i)\nmx = arr.max\narr = arr.map{|el| mx - el}\nm = arr.reduce(arr[0].gcd(arr[1]), :gcd)\nres = 0\narr.each do |el|\n res += el / m\nend\nputs [res, m].join(' ')\n"}, {"source_code": "MOD=10**9+7\ncnt=0; sum=0; prev=nil; can=true; h=Hash.new(0)\ndef gs() gets.chomp end\ndef gi() gets.to_i end\ndef gsmi() gets.chomp.split.map(&:to_i) end\n\ndef desc(ar) ar.sort!{|x,y|y<=>x} end\ndef min(a,b) a<=b ? a : b end\ndef max(a,b) a>=b ? a : b end\ndef sum(ar) ar.inject(:+) end\n\n#def bs(ar,eq,n) en=(eq==1 ? :>= : :>); ar.bsearch_index{|x|x.send(en,n)} end\ndef C(a,b) b==0||a==b ? 1 : (b=a-b if a/2
'\n\nclass Solution\n def initialize()\n @s = \"\"\n while line=gets do\n @s = @s + line.chomp!\n end\n @count_stack = []\n @count_array = []\n end\n\n def cell()\n @s = @s[CELL_PAT.length..-1]\n if /^#{TABLE_PAT}/ =~ @s\n table\n end\n @count_stack[-1] += 1\n @s = @s[CELL_PAT.length+1..-1]\n end\n\n def row()\n @s = @s[ROW_PAT.length..-1]\n while /^#{CELL_PAT}/ =~ @s do\n cell\n end\n @s = @s[ROW_PAT.length+1..-1]\n end\n\n def table()\n @s = @s[TABLE_PAT.length..-1]\n @count_stack.push(0)\n while /^#{ROW_PAT}/ =~ @s do\n row\n end\n @count_array.push(@count_stack.pop)\n @s = @s[TABLE_PAT.length+1..-1]\n end\n\n def solve()\n while /^#{TABLE_PAT}/ =~ @s do\n table\n end\n puts @count_array.sort!.join(' ')\n end\nend\n\nsolution = Solution.new\nsolution.solve\n"}, {"source_code": "a = [0]\nb = []\n(readlines.map(&:chomp) * '').scan(/<(ta|\\/ta|td)/) do |_,|\n\tcase _\n\twhen 'ta' then a << 0\n\twhen '/ta' then b << a.pop\n\twhen 'td' then a[-1] += 1\n\tend\nend\nputs b.sort * ' '\n"}, {"source_code": "a=b=[];([*$<]*'').tr(?\\n,\"\").scan(/<\\/?ta|319&&b<<319&&b<<319&&b<<319&&b<<319&&b<<319&&b<<319&&b<<')\n [s[0..pos], s[pos+1..-1]]\nend\n\nstack = []\nans = []\n\nwhile true\n head, s = read_next(s)\n break unless head\n case head\n when \"\" then stack.push(0)\n when \"
\" then ans << stack.pop\n when \"
319&&b<<319&&b<<319&&b<<319&&b<</) do |_,|\n\tcase _\n\twhen 'table' then a << 0\n\twhen '/table' then b << a.pop\n\twhen 'td' then a[-1] += 1\n\tend\nend\nputs b.sort * ' '\n"}, {"source_code": "a = [0]\nb = []\n(readlines * '').scan(/<(ta|\\/ta|td)/) do |_,|\n\tcase _\n\twhen 'ta' then a << 0\n\twhen '/ta' then b << a.pop\n\twhen 'td' then a[-1] += 1\n\tend\nend\nputs b.sort * ' '\n"}], "src_uid": "eb5a5adffedacadad1df27a1ddc7f8ff"} {"nl": {"description": "We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element.We can do the following two kinds of operations any number of times (possibly zero) in any order: If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element.But there is one additional rule. After we are done, the pointer has to be on the first element.You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not.", "input_spec": "The first line contains a single integer $$$t$$$ $$$(1\\le t\\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\\le n\\le 2 \\cdot 10^5)$$$  — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$-10^9 \\le a_i \\le 10^9$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, print \"Yes\" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and \"No\" (without quotes) otherwise. You can output \"Yes\" and \"No\" in any case (for example, strings \"yEs\", \"yes\" and \"Yes\" will be recognized as a positive response).", "sample_inputs": ["7\n2\n1 0\n4\n2 -1 -1 0\n4\n1 -4 3 0\n4\n1 -1 1 -1\n5\n1 2 3 4 -10\n7\n2 -1 1 -2 0 0 0\n1\n0"], "sample_outputs": ["No\nYes\nNo\nNo\nYes\nYes\nYes"], "notes": "NoteIn the first test case we can obtain the array after some operations, but the pointer won't be on the first element.One way of obtaining the array in the second test case is shown below.$$$\\langle \\underline{0}, 0, 0, 0\\rangle \\to \\langle 1, \\underline{0}, 0, 0 \\rangle \\to \\langle \\underline{1}, -1, 0, 0\\rangle \\to \\langle 2, \\underline{-1}, 0, 0\\rangle \\to \\langle 2, 0, \\underline{0}, 0\\rangle \\to \\langle 2, \\underline{0}, -1, 0\\rangle \\to \\langle \\underline{2}, -1, -1, 0\\rangle$$$"}, "positive_code": [{"source_code": "gets.to_i.times do\r\n gets\r\n xs = gets.split.map(&:to_i)\r\n xs.pop while xs.last == 0\r\n sum = 0\r\n ok = true\r\n xs.each.with_index do |x, i|\r\n sum += x\r\n if sum < 0 || (sum == 0 && i != xs.size - 1)\r\n ok = false\r\n end\r\n end\r\n ok = false if sum != 0\r\n puts ok ? \"YES\" : \"NO\"\r\nend\r\n"}], "negative_code": [], "src_uid": "9f0ffcbd0ce62a365a1ecbb4a2c1de3e"} {"nl": {"description": "Ashishgup and FastestFinger play a game. They start with a number $$$n$$$ and play in turns. In each turn, a player can make any one of the following moves: Divide $$$n$$$ by any of its odd divisors greater than $$$1$$$. Subtract $$$1$$$ from $$$n$$$ if $$$n$$$ is greater than $$$1$$$. Divisors of a number include the number itself.The player who is unable to make a move loses the game.Ashishgup moves first. Determine the winner of the game if both of them play optimally.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 100$$$)  — the number of test cases. The description of the test cases follows. The only line of each test case contains a single integer  — $$$n$$$ ($$$1 \\leq n \\leq 10^9$$$).", "output_spec": "For each test case, print \"Ashishgup\" if he wins, and \"FastestFinger\" otherwise (without quotes).", "sample_inputs": ["7\n1\n2\n3\n4\n5\n6\n12"], "sample_outputs": ["FastestFinger\nAshishgup\nAshishgup\nFastestFinger\nAshishgup\nFastestFinger\nAshishgup"], "notes": "NoteIn the first test case, $$$n = 1$$$, Ashishgup cannot make a move. He loses.In the second test case, $$$n = 2$$$, Ashishgup subtracts $$$1$$$ on the first move. Now $$$n = 1$$$, FastestFinger cannot make a move, so he loses.In the third test case, $$$n = 3$$$, Ashishgup divides by $$$3$$$ on the first move. Now $$$n = 1$$$, FastestFinger cannot make a move, so he loses.In the last test case, $$$n = 12$$$, Ashishgup divides it by $$$3$$$. Now $$$n = 4$$$, FastestFinger is forced to subtract $$$1$$$, and Ashishgup gets $$$3$$$, so he wins by dividing it by $$$3$$$."}, "positive_code": [{"source_code": "def solve\n n = gets.strip.to_i\n if n == 1\n puts \"FastestFinger\"\n return nil\n end\n if n == 2 || n.odd?\n puts \"Ashishgup\"\n return nil\n end\n\n sq = Math.sqrt(n).to_i\n (2..sq).each { |i|\n if (n % i).zero?\n if (i.odd? && (n / i) > 2) || ((n / i).odd? && (n / (n / i)) > 2)\n puts \"Ashishgup\"\n return nil\n end\n end\n }\n puts \"FastestFinger\"\nend\n\ngets.strip.to_i.times { solve }\n"}, {"source_code": "n = gets.chomp.to_i\n\na = 'Ashishgup'\nb = 'FastestFinger'\n\ndef is_prime?(num)\n return if num <= 1\n (2..Math.sqrt(num)).none? { |i| (num % i).zero? }\nend\n\n(1..n).each do\n x = gets.chomp.to_i\n if x == 1\n puts b\n elsif x == 2\n puts a\n elsif x % 2 == 1\n puts a\n elsif x % 4 != 0\n if is_prime?(x/2)\n puts b\n else\n puts a\n end\n elsif x.to_s(2).count('1') == 1\n puts b\n else\n puts a\n end\nend\n"}], "negative_code": [{"source_code": "def solve\n n = gets.strip.to_i\n if n == 1\n puts \"FastestFinger\"\n return nil\n end\n if n == 2 || n.odd?\n puts \"Ashishgup\"\n return nil\n end\n\n sq = Math.sqrt(n).to_i\n (2..sq).each { |i|\n if (n % i).zero?\n if (i.odd? || (n / i).odd?)\n if n / (n / i) > 3 || n / i > 3\n puts \"Ashishgup\"\n return nil\n end\n end\n end\n }\n puts \"FastestFinger\"\nend\n\ngets.strip.to_i.times { solve }\n"}, {"source_code": "n = gets.chomp.to_i\n\na = 'Ashishgup'\nb = 'FastestFinger'\n\n(1..n).each do\n x = gets.chomp.to_i\n if x == 1\n puts b\n elsif x == 2\n puts a\n elsif x % 2 == 1\n puts a\n elsif x % 4 != 0\n puts b\n elsif x.to_s(2).count('1') == 1\n puts b\n else\n puts a\n end\nend\n"}], "src_uid": "b533572dd6d5fe7350589c7f4d5e1c8c"} {"nl": {"description": "We have a string of letters 'a' and 'b'. We want to perform some operations on it. On each step we choose one of substrings \"ab\" in the string and replace it with the string \"bba\". If we have no \"ab\" as a substring, our job is done. Print the minimum number of steps we should perform to make our job done modulo 109 + 7.The string \"ab\" appears as a substring if there is a letter 'b' right after the letter 'a' somewhere in the string.", "input_spec": "The first line contains the initial string consisting of letters 'a' and 'b' only with length from 1 to 106.", "output_spec": "Print the minimum number of steps modulo 109 + 7.", "sample_inputs": ["ab", "aab"], "sample_outputs": ["1", "3"], "notes": "NoteThe first example: \"ab\"  →  \"bba\".The second example: \"aab\"  →  \"abba\"  →  \"bbaba\"  →  \"bbbbaa\"."}, "positive_code": [{"source_code": "res, q, m=0, 1, 10**9+7\ngets.chomp.chars{|c| c==?a ? q=q*2%m : res=(res+q-1+m)%m}\np res\n"}, {"source_code": "s=gets.chomp\nMOD=1000000007\nans=0\nm=1\nfor i in 0...s.length\n\tif s[i]=='a'\n\t\tm=(m*2)%MOD\n\telse\n\t\tans=(ans+m-1)%MOD\n\tend\nend\nputs ans\n\t"}], "negative_code": [{"source_code": "s=gets.chomp\nm=10**9+7\na=s.split(?b).map(&:size)\na.pop if s[-1]!=?b\nn=a.size\nfor i in 1...n\n a[i]+=a[i-1]\nend\nres=0\na.each{|e| res+=2**e-1; res%=m; }\np res\n"}], "src_uid": "8f52241c690ec4f9af71a52904fb19a0"} {"nl": {"description": "Mocha is a young girl from high school. She has learned so much interesting knowledge from her teachers, especially her math teacher. Recently, Mocha is learning about binary system and very interested in bitwise operation.This day, Mocha got a sequence $$$a$$$ of length $$$n$$$. In each operation, she can select an arbitrary interval $$$[l, r]$$$ and for all values $$$i$$$ ($$$0\\leq i \\leq r-l$$$), replace $$$a_{l+i}$$$ with $$$a_{l+i} \\,\\&\\, a_{r-i}$$$ at the same time, where $$$\\&$$$ denotes the bitwise AND operation. This operation can be performed any number of times.For example, if $$$n=5$$$, the array is $$$[a_1,a_2,a_3,a_4,a_5]$$$, and Mocha selects the interval $$$[2,5]$$$, then the new array is $$$[a_1,a_2\\,\\&\\, a_5, a_3\\,\\&\\, a_4, a_4\\,\\&\\, a_3, a_5\\,\\&\\, a_2]$$$.Now Mocha wants to minimize the maximum value in the sequence. As her best friend, can you help her to get the answer?", "input_spec": "Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 100$$$) — the length of the sequence. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$0 \\le a_i \\le 10^9$$$).", "output_spec": "For each test case, print one integer — the minimal value of the maximum value in the sequence.", "sample_inputs": ["4\n2\n1 2\n3\n1 1 3\n4\n3 11 3 7\n5\n11 7 15 3 7"], "sample_outputs": ["0\n1\n3\n3"], "notes": "NoteIn the first test case, Mocha can choose the interval $$$[1,2]$$$, then the sequence becomes $$$[ 0, 0]$$$, where the first element is $$$1\\,\\&\\,2$$$, and the second element is $$$2\\,\\&\\,1$$$.In the second test case, Mocha can choose the interval $$$[1,3]$$$, then the sequence becomes $$$[ 1,1,1]$$$, where the first element is $$$1\\,\\&\\,3$$$, the second element is $$$1\\,\\&\\,1$$$, and the third element is $$$3\\,\\&\\,1$$$."}, "positive_code": [{"source_code": "END { load __FILE__ unless $stdin.eof? }\n\nt = gets.to_i\nt.times do\n\nn = gets.to_i\na = gets.split.map!(&:to_i)\n\nx = a.inject(:&)\np x\n\nend\n"}, {"source_code": "gets.to_i.times do\r\n n=gets.to_i\r\n puts gets.split.map(&:to_i).inject(:&)\r\nend\r\n"}], "negative_code": [], "src_uid": "4f8eac547bbd469a69de2f4aae4a87f0"} {"nl": {"description": "This problem differs from the previous one only in the absence of the constraint on the equal length of all numbers $$$a_1, a_2, \\dots, a_n$$$.A team of SIS students is going to make a trip on a submarine. Their target is an ancient treasure in a sunken ship lying on the bottom of the Great Rybinsk sea. Unfortunately, the students don't know the coordinates of the ship, so they asked Meshanya (who is a hereditary mage) to help them. He agreed to help them, but only if they solve his problem.Let's denote a function that alternates digits of two numbers $$$f(a_1 a_2 \\dots a_{p - 1} a_p, b_1 b_2 \\dots b_{q - 1} b_q)$$$, where $$$a_1 \\dots a_p$$$ and $$$b_1 \\dots b_q$$$ are digits of two integers written in the decimal notation without leading zeros.In other words, the function $$$f(x, y)$$$ alternately shuffles the digits of the numbers $$$x$$$ and $$$y$$$ by writing them from the lowest digits to the older ones, starting with the number $$$y$$$. The result of the function is also built from right to left (that is, from the lower digits to the older ones). If the digits of one of the arguments have ended, then the remaining digits of the other argument are written out. Familiarize with examples and formal definitions of the function below.For example: $$$$$$f(1111, 2222) = 12121212$$$$$$ $$$$$$f(7777, 888) = 7787878$$$$$$ $$$$$$f(33, 44444) = 4443434$$$$$$ $$$$$$f(555, 6) = 5556$$$$$$ $$$$$$f(111, 2222) = 2121212$$$$$$Formally, if $$$p \\ge q$$$ then $$$f(a_1 \\dots a_p, b_1 \\dots b_q) = a_1 a_2 \\dots a_{p - q + 1} b_1 a_{p - q + 2} b_2 \\dots a_{p - 1} b_{q - 1} a_p b_q$$$; if $$$p < q$$$ then $$$f(a_1 \\dots a_p, b_1 \\dots b_q) = b_1 b_2 \\dots b_{q - p} a_1 b_{q - p + 1} a_2 \\dots a_{p - 1} b_{q - 1} a_p b_q$$$. Mishanya gives you an array consisting of $$$n$$$ integers $$$a_i$$$, your task is to help students to calculate $$$\\sum_{i = 1}^{n}\\sum_{j = 1}^{n} f(a_i, a_j)$$$ modulo $$$998\\,244\\,353$$$.", "input_spec": "The first line of the input contains a single integer $$$n$$$ ($$$1 \\le n \\le 100\\,000$$$) — the number of elements in the array. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$) — the elements of the array.", "output_spec": "Print the answer modulo $$$998\\,244\\,353$$$.", "sample_inputs": ["3\n12 3 45", "2\n123 456"], "sample_outputs": ["12330", "1115598"], "notes": null}, "positive_code": [{"source_code": "MOD = 998244353\nn = gets.to_i\na = gets.split.map(&:to_i)\ncnt = [0]*10\na.each do |v|\n cnt[Math.log10(v).to_i] += 1\nend\nans = 0\na.each do |v|\n d = Math.log10(v).to_i\n l,r,m = v,0,1\n 10.times do |i|\n r += m*(l%10)\n l /= 10\n m *= 100\n if i < d\n ans += l * m * cnt[i] * 2 % MOD\n ans += (r + r*10) * cnt[i] % MOD\n else\n ans += (r + r*10) * cnt[i] % MOD\n end\n ans %= MOD\n end\nend\np ans"}], "negative_code": [{"source_code": "MOD = 998244353\nn = gets.to_i\na = gets.split.map(&:to_i)\ncnt = [0]*10\na.each do |v|\n cnt[Math.log(v,10).to_i] += 1\nend\nans = 0\na.each do |v|\n d = Math.log(v,10).to_i\n val = v\n 10.times do |i|\n val = (val/10**(i*2)) * 10**(i*2+1) + val % 10**(i*2)\n if i < d\n l = val/10**(i**2+2)\n r = val % 10**(i**2+2)\n ans += l * 10**(i**2+2) * cnt[i] * 2 % MOD\n ans += r * cnt[i] % MOD\n ans += r/10 * cnt[i] % MOD\n else\n ans += val/10 * cnt[i] % MOD\n ans += val * cnt[i] % MOD\n end\n ans %= MOD\n #p [v,i,val,ans]\n end\nend\n#p cnt\np ans\n"}, {"source_code": "MOD = 998244353\nn = gets.to_i\na = gets.split.map(&:to_i)\ncnt = [0]*10 # key: log10(a)\na.each do |v|\n cnt[Math.log(v,10).to_i] += 1\nend\nans = 0\na.each do |v|\n d = Math.log(v,10).to_i\n val = v\n 10.times do |i|\n val = (val/10**(i*2)) * 10**(i*2+1) + val % 10**(i*2) % MOD\n if i < d\n l = val/10**(i**2+2)\n r = val % 10**(i**2+2)\n ans += l * 10**(i**2+2) * cnt[i] * 2 % MOD\n ans += r * cnt[i] % MOD\n ans += r/10 * cnt[i] % MOD\n else\n ans += val/10 * cnt[i] % MOD\n ans += val * cnt[i] % MOD\n end\n ans %= MOD\n #p [v,i,val,ans]\n end\nend\n#p cnt\np ans\n"}, {"source_code": "def modpow(x,n)\n ret = 1\n while n > 0\n ret = ret*x % MOD if n&1 == 1\n x = x*x % MOD\n n >>= 1\n end\n return ret\nend\n\ndef modinv(x)\n return modpow(x,MOD-2)\nend\nMOD = 998244353\nn = gets.to_i\na = gets.split.map(&:to_i)\ncnt = [0]*10\na.each do |v|\n cnt[Math.log(v,10).to_i] += 1\nend\nans = 0\na.each do |v|\n d = Math.log(v,10).to_i\n l,r,m = v,0,1\n 10.times do |i|\n r += m*(l%10)\n l /= 10\n m *= 100\n if i < d\n ans += l * m * cnt[i] * 2 % MOD\n ans += (r + r*10) * cnt[i] % MOD\n else\n ans += (r + r*10) * cnt[i] % MOD\n end\n ans %= MOD\n end\nend\np ans"}], "src_uid": "b4cd60296083ee2ae8a560209433dcaf"} {"nl": {"description": "You are given a string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters. Polycarp wants to remove exactly $$$k$$$ characters ($$$k \\le n$$$) from the string $$$s$$$. Polycarp uses the following algorithm $$$k$$$ times: if there is at least one letter 'a', remove the leftmost occurrence and stop the algorithm, otherwise go to next item; if there is at least one letter 'b', remove the leftmost occurrence and stop the algorithm, otherwise go to next item; ... remove the leftmost occurrence of the letter 'z' and stop the algorithm. This algorithm removes a single letter from the string. Polycarp performs this algorithm exactly $$$k$$$ times, thus removing exactly $$$k$$$ characters.Help Polycarp find the resulting string.", "input_spec": "The first line of input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le k \\le n \\le 4 \\cdot 10^5$$$) — the length of the string and the number of letters Polycarp will remove. The second line contains the string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters.", "output_spec": "Print the string that will be obtained from $$$s$$$ after Polycarp removes exactly $$$k$$$ letters using the above algorithm $$$k$$$ times. If the resulting string is empty, print nothing. It is allowed to print nothing or an empty line (line break).", "sample_inputs": ["15 3\ncccaabababaccbc", "15 9\ncccaabababaccbc", "1 1\nu"], "sample_outputs": ["cccbbabaccbc", "cccccc", ""], "notes": null}, "positive_code": [{"source_code": "n, k = gets.chomp.split.map(&:to_i)\ns = gets.chomp\n\na = s.split(\"\").sort\n\nhash = Hash.new { |hash, key| hash[key] = 0 }\n\nk.times do |i|\n hash[a[i]] += 1\nend\n\nresult = \"\"\n\ns.each_char do |char|\n if hash[char].nil? || hash[char] <= 0\n result << char\n elsif hash[char] && hash[char] > 0\n hash[char] -= 1\n end\nend\n\nputs result\n"}, {"source_code": "n, k = gets.split(\" \").map{|n| n.to_i }\ns = gets.chomp\n\nrequire 'pp'\nstrs = \"abcdefghijklmnopqrstuvwxyz\".split(\"\")\nmaps = {}\ns.length.times{|i|\n maps[s[i]] ||= 0\n maps[s[i]] += 1\n}\n\ncounts = maps.to_a.sort{|a, b|a[0]<=>b[0]}\nerase_counts = {}\nwhile 0 < k\n counts.each{|char, count|\n erase = [count, k].min\n k -= erase\n erase_counts[char] = erase\n break if k == 0\n }\nend\n\ns.length.times{|i|\n char = s[i]\n if not erase_counts[char].nil? and 0 < erase_counts[char]\n erase_counts[char] -= 1\n else\n print char\n end\n}\nputs \"\"\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\ns = gets.chomp\npos = (1..26).map { [] }\ns.chars.each_with_index { |c, i| pos[c.ord - 'a'.ord] << i }\n\nflag = [true] * n\npos.each do |a|\n\ta.each do |pos|\n\t\tif k > 0\n\t\t\tflag[pos] = false\n\t\t\tk -= 1\n\t\tend\n\tend\nend\nputs (0...n).select { flag[_1] }.map { s[_1] }.join\n"}, {"source_code": "\ndef solution\n _, k = read_ints\n s = read_string.chars\n\n ixs = (0..25).map{ |i| [] }\n\n (0...s.size).each do |i|\n c = s[i]\n x = c.ord - 97\n ixs[x] << i\n end\n\n to_remove = ixs.flatten\n\n k.times do\n i = to_remove.shift\n s[i] = '*'\n end\n\n # puts s.gsub('*', '')\n s.each do |c|\n print c if c > '*'\n end\n puts\n\nend\n\ndef solution_16s\n # perf problem was not the hash; it was the string ops\n # Apparently s[i] = 'c' is expensive.\n # Using s.chars instead is much better\n _, k = read_ints\n s = read_string\n\n ixs = Hash.new { |h, i| h[i] = [] }\n\n (0...s.size).each do |i|\n c = s[i]\n ixs[c] << i\n end\n\n l = 'a'\n\n k.times do\n l = l.succ while ixs[l].size < 1\n ix = ixs[l].shift\n s[ix] = ' '\n end\n\n # puts s.gsub(' ', '')\nend\n\ndef read_int\n gets.to_i\nend\n\ndef read_ints\n gets.split.map(&:to_i)\nend\n\ndef read_string\n gets.chomp\nend\n\nsolution unless ENV['TEST__MODE']\n\n"}, {"source_code": "N, K = gets.split.map(&:to_i)\nstr = gets.chomp\nhash = Hash.new(0)\nstr.split('').sort[0, K].each do |c|\n hash[c] += 1\nend\nans = ''\nstr.chars do |c|\n if hash[c] > 0\n hash[c] -= 1\n else\n ans << c\n end\nend\nputs ans"}, {"source_code": "n, k = gets.strip.split.map(&:to_i)\nstr = gets.strip\n\nchars = str.split('').sort\nhsh = {}\nk.times do |i|\n hsh[chars[i]] = (hsh[chars[i]] || 0) + 1\nend\n\nans_str = \"\"\nstr.each_char do |c|\n if hsh[c].nil? || hsh[c] <= 0\n ans_str << c\n else\n hsh[c] -= 1 if hsh[c] && hsh[c] > 0\n end\nend\nputs ans_str"}], "negative_code": [{"source_code": "\ndef solution\n _, k = read_ints\n s = read_string.chars\n\n ixs = (0..25).map{ |i| [] }\n\n (0...s.size).each do |i|\n c = s[i]\n x = c.ord - 97\n ixs[x] << i\n end\n\n to_remove = ixs.flatten\n\n k.times do\n i = to_remove.shift\n s[i] = '*'\n end\n\n # puts s.gsub('*', '')\n s.each do |c|\n print c, false if c > '*'\n end\n puts\n\nend\n\ndef solution_16s\n _, k = read_ints\n s = read_string\n\n ixs = Hash.new { |h, i| h[i] = [] }\n\n (0...s.size).each do |i|\n c = s[i]\n ixs[c] << i\n end\n\n l = 'a'\n\n k.times do\n l = l.succ while ixs[l].size < 1\n ix = ixs[l].shift\n s[ix] = ' '\n end\n\n # puts s.gsub(' ', '')\nend\n\ndef read_int\n gets.to_i\nend\n\ndef read_ints\n gets.split.map(&:to_i)\nend\n\ndef read_string\n gets.chomp\nend\n\nsolution unless ENV['TEST__MODE']\n\n"}, {"source_code": "\ndef solution\n _, k = read_ints\n s = read_string.chars\n\n ixs = (0..25).map{ |i| [] }\n\n (0...s.size).each do |i|\n c = s[i]\n x = c.ord - 97\n ixs[x] << i\n end\n\n to_remove = ixs.flatten\n\n k.times do\n i = to_remove.shift\n s[i] = '*'\n end\n\n s.each do |c|\n print c, false if c > '*'\n end\n puts\n\nend\n\ndef solution_16s\n _, k = read_ints\n s = read_string\n\n ixs = Hash.new { |h, i| h[i] = [] }\n\n (0...s.size).each do |i|\n c = s[i]\n ixs[c] << i\n end\n\n l = 'a'\n\n k.times do\n l = l.succ while ixs[l].size < 1\n ix = ixs[l].shift\n s[ix] = ' '\n end\n\n # puts s.gsub(' ', '')\nend\n\ndef read_int\n gets.to_i\nend\n\ndef read_ints\n gets.split.map(&:to_i)\nend\n\ndef read_string\n gets.chomp\nend\n\nsolution unless ENV['TEST__MODE']\n\n"}], "src_uid": "9f095a5f5b39d8c2f99c4162f2d7c5ff"} {"nl": {"description": "Recently, a start up by two students of a state university of city F gained incredible popularity. Now it's time to start a new company. But what do we call it?The market analysts came up with a very smart plan: the name of the company should be identical to its reflection in a mirror! In other words, if we write out the name of the company on a piece of paper in a line (horizontally, from left to right) with large English letters, then put this piece of paper in front of the mirror, then the reflection of the name in the mirror should perfectly match the line written on the piece of paper.There are many suggestions for the company name, so coming up to the mirror with a piece of paper for each name wouldn't be sensible. The founders of the company decided to automatize this process. They asked you to write a program that can, given a word, determine whether the word is a 'mirror' word or not.", "input_spec": "The first line contains a non-empty name that needs to be checked. The name contains at most 105 large English letters. The name will be written with the next sans serif font: ", "output_spec": "Print 'YES' (without the quotes), if the given name matches its mirror reflection. Otherwise, print 'NO' (without the quotes).", "sample_inputs": ["AHA", "Z", "XO"], "sample_outputs": ["YES", "NO", "NO"], "notes": null}, "positive_code": [{"source_code": "sym = ['A', 'H', 'I', 'M', 'O', 'T', 'U', 'V', 'W', 'X', 'Y']\n\ninput = gets.chomp\nif input != input.reverse\n puts \"NO\"\nelse\n input.each_char do |char|\n next if sym.include? char\n\n puts \"NO\"\n exit\n end\n puts \"YES\"\nend"}, {"source_code": "def solve(s)\n\treturn ((/^[AHIMOTUVWXY]*$/.match s) and (s == s.reverse))\nend\n\ndef main()\n\treturn \"YES\" if solve(gets.chomp)\n\treturn \"NO\"\nend\n\nputs main\n"}, {"source_code": "s = gets.chomp\nif s.match(/[^AHIOMTUVWXY]/).nil? and s == s.reverse\n puts 'YES'\nelse\n puts 'NO'\nend\n\n"}, {"source_code": "s = gets.chomp\nif (s.size == 1 or s[0..s.size / 2 - 1] == s[(s.size / 2.0).ceil..-1].reverse) and s.scan(/[AHIMOTUVWXY]/i).size == s.size\n\tputs \"YES\"\nelse\n\tputs \"NO\"\nend"}, {"source_code": "s = gets.chomp\nletters = [\"A\", \"H\", \"I\", \"M\", \"O\", \"T\", \"U\", \"V\", \"W\", \"X\", \"Y\"]\ns.split(\"\").each do |ch|\n unless letters.include?(ch)\n puts \"NO\"\n exit\n end\nend\nif s.reverse != s\n puts \"NO\"\n exit\nend\nputs \"YES\"\n"}, {"source_code": "s = gets.chomp\nletters = [\"A\", \"H\", \"I\", \"M\", \"O\", \"T\", \"U\", \"V\", \"W\", \"X\", \"Y\"]\nunless (s.split(\"\").uniq-letters).empty?\n puts \"NO\"\n exit\nend\nif s.reverse != s\n puts \"NO\"\n exit\nend\nputs \"YES\"\n"}, {"source_code": "MIRROR = \"AHIMOTUVWXY\"\ns = gets.chomp\nlen = s.length\n0.upto(len>>1) do |i|\n\tif s[i] != s[len-1-i] || !MIRROR.include?(s[i]) || !MIRROR.include?(s[len-1-i])\n\t\tputs \"NO\"\n\t\texit 0\n\tend\nend\nputs \"YES\"\n"}, {"source_code": "#!/usr/bin/ruby\ns=gets.chomp\nputs s.tr('A-Z','A@@@@@@HI@@@M@O@@@@TUVWXY@').reverse==s ? :YES : :NO"}], "negative_code": [{"source_code": "s = gets.chomp\nif s[0..s.size / 2 - 1] == s[(s.size / 2.0).ceil..-1].reverse and s.scan(/[AHIMOTUVWXY]/i).size == s.size\n\tputs \"YES\"\nelse\n\tputs \"NO\"\nend"}, {"source_code": "gets.chomp.each_char do |i|\n\tif !\"AHIMOTUVWY\".include?(i)\n\t\tputs \"NO\"\n\t\texit 0\n\tend\nend\nputs \"YES\"\n"}, {"source_code": "#!/usr/bin/ruby\ns=gets.chomp\nputs s.tr('A-Z','A@@@@@@HI@@M@O@@@@TUVWXY@').reverse==s ? :YES : :NO"}, {"source_code": "s = gets.chomp\nif s.match(/[^AHIOTUVWXY]/).nil? and s == s.reverse\n puts 'YES'\nelse\n puts 'NO'\nend\n\n"}], "src_uid": "8135173b23c6b48df11b1d2609b08080"} {"nl": {"description": "The police department of your city has just started its journey. Initially, they don’t have any manpower. So, they started hiring new recruits in groups.Meanwhile, crimes keeps occurring within the city. One member of the police force can investigate only one crime during his/her lifetime.If there is no police officer free (isn't busy with crime) during the occurrence of a crime, it will go untreated.Given the chronological order of crime occurrences and recruit hirings, find the number of crimes which will go untreated.", "input_spec": "The first line of input will contain an integer n (1 ≤ n ≤ 105), the number of events. The next line will contain n space-separated integers. If the integer is -1 then it means a crime has occurred. Otherwise, the integer will be positive, the number of officers recruited together at that time. No more than 10 officers will be recruited at a time.", "output_spec": "Print a single integer, the number of crimes which will go untreated.", "sample_inputs": ["3\n-1 -1 1", "8\n1 -1 1 -1 -1 1 1 1", "11\n-1 -1 2 -1 -1 -1 -1 -1 -1 -1 -1"], "sample_outputs": ["2", "1", "8"], "notes": "NoteLets consider the second example: Firstly one person is hired. Then crime appears, the last hired person will investigate this crime. One more person is hired. One more crime appears, the last hired person will investigate this crime. Crime appears. There is no free policeman at the time, so this crime will go untreated. One more person is hired. One more person is hired. One more person is hired. The answer is one, as one crime (on step 5) will go untreated."}, "positive_code": [{"source_code": "n = gets.to_i\nplc, crm = 0, 0\ngets.split(' ',n).map(&:to_i).each do |x|\n plc += x\n if plc < 0\n crm += 1\n plc = 0\n end\nend\nputs crm"}, {"source_code": "#!/usr/bin/ruby\ngets\nr=0\nn=0\ngets.split.each{|e|\n\tn+=e.to_i\n\tif n<0\n\t\tr+=-n\n\t\tn=0\n\tend\n}\np r"}, {"source_code": "police = 0\nans = 0\ngets.chomp\ngets.chomp.split(\" \").each { |p|\n if p.to_i > -1\n police += p.to_i\n elsif police > 0\n police -= 1\n else\n ans += 1\n end\n}\n\nputs ans\n"}, {"source_code": "gets\ns=0\na=0\nfor x in gets.chomp.split.map(&:to_i)\n\ts += x;\n\ta += -s if s < 0\n\ts -= s if s < 0\nend\n\nputs a"}, {"source_code": "n = gets.to_i\nevents = gets.chomp.split(\" \").map{ |e| e.to_i }\ns = 0\np = 0\nevents.each{ |event|\n\tif event < 0\n\t\tif p > 0\n\t\t\tp -= 1\n\t\telse \n\t\t\ts += 1\n\t\tend\n\telse\n\t\tp += event\n\tend \n}\nputs(s)"}, {"source_code": "# -*- encoding: utf-8 -*-\nn = gets.chomp.to_i\nevents = gets.chomp.split(\" \").map{ |e| e.to_i }\n\nans = 0\np = 0\n\nevents.each { |event|\n if event == -1\n if p > 0\n p -= 1\n else\n ans += 1\n end\n else\n p += event\n end\n}\n\nputs ans\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\n\nx = 0\ny = 0\n\nfor i in a\n if i == -1\n if x > 0\n x -= 1\n else\n y += 1\n end\n else\n x += i\n end\nend\n\nprint y\n"}, {"source_code": "n = gets.to_i\narr = gets.split.map(&:to_i)\nsoliders = 0\ncrimes = 0\n\narr.each do |x|\n\tsoliders += x\n\t(crimes += 1; soliders = 0) if soliders < 0\nend\n\nputs crimes"}, {"source_code": "n,v=gets.to_i,gets.split.map(&:to_i)\ns,c=0,0\nv.each {|x| (s+=x) == -1 ? (c,s=c+1,0) : s}\np c"}, {"source_code": "n = gets.to_i\np = gets.split.map{|r| r.to_i}\n\ncounters = 0; xounterz = 0\nfor i in 0..n-1\n counters+=p[i]\n if counters<0\n xounterz+=1\n counters = 0\n end\nend\nputs xounterz"}, {"source_code": "n = $stdin.gets.to_i\nf = $stdin.readline.split.map(&:to_i)\n\nk = 0\nf.inject(0) do |cops_count, x|\n #puts \"cop_count=#{cops_count}, x=#{x}, k=#{k}\"\n if x == -1\n if cops_count == 0\n k+=1\n else\n cops_count -= 1\n end\n else\n cops_count += x\n end\n cops_count\nend\n\nputs k\n"}, {"source_code": "n= gets.to_i \nlist = gets.split(\" \").map(&:to_i)\n\ni= 0 \navailable_police = 0 \ncrime =0 \nn.times do \n if list[i] > 0 \n available_police+=list[i]\n\n elsif list[i]<0 and available_police>0 \n available_police+=list[i]\n else \n crime +=1 \n end \n i+=1 \n\n\nend \nputs crime "}, {"source_code": "n = gets.to_i\narr = gets.split.map &:to_i\ncops = 0\ncrimes = 0\narr.each do |x|\n if x == -1 && cops == 0\n crimes += 1\n elsif x == -1 && cops != 0 \n cops -= 1\n else\n cops += x\n end\nend\nputs crimes\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\ncur = 0\nans = 0\nfor x in a\n if x>0\n cur += x\n else\n if cur == 0\n ans += 1\n else\n cur -= 1\n end\n end\nend\nprint ans"}, {"source_code": "n = gets.to_i\ns = 0\nans = 0\ngets.split.map(&:to_i).each do |e|\n if e < 0\n if s > 0\n s += e\n else\n ans += 1\n end\n else\n s += e\n end\nend\nputs ans"}, {"source_code": "gets\nf=a=0\ngets.split.map{|i| i.to_i}.each{|s| \n\tif s<0 \n\t\tif f<=0\n\t\t\ta+=1\n\t\telse\n\t\t\tf-=1\n\t\tend\n\telse\n\t\tf+=s\n\tend\n}\np a"}, {"source_code": "gets\nx=gets.split.map{|e| e.to_i}\nsum=0\nans=0\nx.each{|e|\n\tif e<0 then\n\t\tsum+=e\n\t\tans+=1 if sum<0\n\telse\n\t\tsum=([sum,0].max)+e\n\tend\n}\nputs ans"}, {"source_code": "t = gets.chomp.to_i\narr = gets.chomp.split.map(&:to_i)\n\nco = 0\nt = 0\narr.each do |el|\n # p [t+el]\n if t + el < 0\n co += 1\n t = 0\n else\n t += el\n end \nend\nputs co"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nc,r = 0,0\nn.times do |i|\n\tif a[i] > 0\n\t\tc += a[i]\n\telsif c > 0 \n\t\tc -= 1\n\telse\n\t\tr += 1\n\tend\nend\np r"}, {"source_code": "police = 0\ncriminal = 0\nout = 0\nk = gets.to_i\ngets.chomp.split.map do |e|\n e = e.to_i\n police += e if e > 0\n out += 1 if e == -1 and police == 0\n police -= 1 if e == -1 and police > 0\n \nend\n\nputs out\n"}, {"source_code": "n = gets.chomp.to_i\n\nans,pol = 0,0\n\ngets.chomp.split(\" \").each { |e|\n if e.to_i == -1\n if pol>0\n pol -= 1\n else\n ans += 1\n end\n else\n pol += e.to_i\n end\n}\nputs ans\n"}, {"source_code": "n = gets.chomp.to_i\n\nans,pol = 0,0\n\ngets.chomp.split(\" \").each { |e|\n if e.to_i == -1\n if pol>0\n pol -= 1\n else\n ans += 1\n end\n else\n pol += e.to_i\n end\n}\nputs ans"}, {"source_code": "crimes = gets.chomp.to_i\nn = gets.chomp\nevents = n.split\n\ncount = 0\nuntreated = 0\nfor i in (0..crimes-1)\n\tif count == 0 && events[i].to_i == -1\n\t\tuntreated += 1\n\telsif events[i].to_i >= 1\n\t\tcount += events[i].to_i\n\telsif count > 0 && events[i].to_i == -1\n\t\tcount -= 1\n\tend\nend\n\nputs untreated"}, {"source_code": "n = gets.chomp\nevents = gets.chomp.split(' ').map(&:to_i)\nans = f = 0\nevents.reverse.each do |e| \n next if f == 0 && e != -1\n f = 1\n if e == -1\n ans = ans + e\n else\n ans = ans + (ans < 0 ? e : 0)\n ans = 0 if ans > 0\n end\nend\nputs ans.abs"}, {"source_code": "gets\nnow = 0\nans = 0\ngets.split.map(&:to_i).each { |c|\n if c == -1\n if now >= 1\n now -= 1\n else\n ans += 1\n end\n else\n now += c\n end\n}\n\nputs ans"}, {"source_code": "cops = 0\nresult = 0\n\ngets\ngets.split(' ').map(&:to_i).each do |x|\n cops += x\n if cops < 0\n result += 1\n cops = 0\n end\nend\n\nputs result\n"}, {"source_code": "eventNumber = gets.chomp.to_i\ninput = gets.chomp.split(' ').map {|item| item.to_i}\n\npolice = 0\nans = 0\n(0...eventNumber).each do |event|\n police += input[event] if input[event] >= 1\n \n if input[event] == -1 && police <= 0\n ans += 1\n elsif input[event] == -1\n police -= 1\n end\nend\n\nputs ans"}], "negative_code": [{"source_code": "police = 0\nans = 0\ngets.chomp\ngets.chomp.split(\" \").each { |p|\n if p.to_i == 1\n police += 1\n elsif police > 0\n police -= 1\n else\n ans += 1\n end\n}\n\nputs ans\n"}, {"source_code": "n = gets.to_i\nevents = gets.chomp.split(\" \").map{ |e| e.to_i }\ns = 0\np = 0\nevents.each{ |event|\n\tif event == -1\n\t\tif p >= 0\n\t\t\tp -= 1\n\t\telse \n\t\t\ts += 1\n\t\tend\n\telse\n\t\tp+=1\n\tend \n}\nputs(s)"}, {"source_code": "n = gets.to_i\nevents = gets.chomp.split(\" \").map{ |e| e.to_i }\ns = 0\np = 0\nevents.each{ |event|\n\tif event == -1\n\t\tif p > 0\n\t\t\tp -= 1\n\t\telse \n\t\t\ts += 1\n\t\tend\n\telse\n\t\tp+=1\n\tend \n}\nputs(s)"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\n\nx = 0\ny = 0\n\nfor i in a\n if i == -1\n if x > 0\n x -= 1\n else\n y += 1\n end\n else\n x += 1\n end\nend\n\nprint y\n"}, {"source_code": "n,v=gets.to_i,gets.split.map(&:to_i)\ns=0\np v.map {|x| s+=x}.min.abs"}, {"source_code": "gets\nf=a=0\ngets.split.each{|s| \n\tif s[1] \n\t\tif f<0\n\t\t\ta+=1\n\t\telse\n\t\t\tf-=1\n\t\tend\n\telse\n\t\tf+=1\n\tend\n}\np a"}, {"source_code": "n = gets.chomp\nevents = gets.chomp.split(' ').map(&:to_i)\nans = f = 0\nevents.reverse.each { |e| f == 0 && e == 1 ? next : (f, ans = 1, ans + e) }\nputs ans.abs"}], "src_uid": "af47635f631381b4578ba599a4f8b317"} {"nl": {"description": "Julia is going to cook a chicken in the kitchen of her dormitory. To save energy, the stove in the kitchen automatically turns off after k minutes after turning on.During cooking, Julia goes to the kitchen every d minutes and turns on the stove if it is turned off. While the cooker is turned off, it stays warm. The stove switches on and off instantly.It is known that the chicken needs t minutes to be cooked on the stove, if it is turned on, and 2t minutes, if it is turned off. You need to find out, how much time will Julia have to cook the chicken, if it is considered that the chicken is cooked evenly, with constant speed when the stove is turned on and at a constant speed when it is turned off.", "input_spec": "The single line contains three integers k, d and t (1 ≤ k, d, t ≤ 1018).", "output_spec": "Print a single number, the total time of cooking in minutes. The relative or absolute error must not exceed 10 - 9. Namely, let's assume that your answer is x and the answer of the jury is y. The checker program will consider your answer correct if .", "sample_inputs": ["3 2 6", "4 2 20"], "sample_outputs": ["6.5", "20.0"], "notes": "NoteIn the first example, the chicken will be cooked for 3 minutes on the turned on stove, after this it will be cooked for . Then the chicken will be cooked for one minute on a turned off stove, it will be cooked for . Thus, after four minutes the chicken will be cooked for . Before the fifth minute Julia will turn on the stove and after 2.5 minutes the chicken will be ready .In the second example, when the stove is turned off, Julia will immediately turn it on, so the stove will always be turned on and the chicken will be cooked in 20 minutes."}, "positive_code": [{"source_code": "k, d, t = gets().split().map{|x| 2*(x.to_i)}\nr = (d - k%d) % d\nn = k/d\nn += 1 if r != 0\n\nans = 0\nif t <= k\n\tans += t\nelse\n\tcycle = k + r/2\n\tans += t/cycle * n * d\n\tt %= cycle\n\tif t <= k\n\t\tans += t\n\telse\n\t\tans += k + 2*(t-k)\n\tend\nend\n\nputs \"%.10f\" % [ans/2.0]\n"}, {"source_code": "K,D,T=gets.split.map(&:to_f)\nS=(K/D).ceil*D\ndef f(t)\n (t/S).floor*(K+(S-K)*0.5)+[t%S,K].min+[0,t%S-K].max*0.5\nend\nl=T\nr=2*T\n100.times{\n m=(l+r)/2.0\n if f(m)>T\n r=m\n else\n l=m\n end\n}\np l\n"}, {"source_code": "K,D,T=gets.split.map(&:to_f)\nS=(K/D).ceil*D\n# def f(t)\n# (t/S).floor*(K+(S-K)*0.5)+[t%S,K].min+[0,t%S-K].max*0.5\n# end\n# l=T\n# r=2*T\n# 100.times{\n# m=(l+r)/2.0\n# if f(m)>T\n# r=m\n# else\n# l=m\n# end\n# }\n# p l\n\nans = 0\n\na = (S + K) * 0.5\nans += (T/a).floor * S\nT -= (T/a).floor * a\n\nans += [T, K].min\nT -= [T, K].min\n\nif T > 0\n ans += 2 * T\nend\n\np ans\n"}, {"source_code": "k,d,t = gets.split.map(&:to_i)\nif k > d\n d = (k/d.to_f).ceil * d\nend\n\nper = k + (d-k)/2.to_f\nn = (t/per).to_i\nans = n * d\nif (t-per*n) <= k\n ans += t - per*n\nelsif\n ans += k + (t-k-per*n)*2\nend\nputs ans\n"}], "negative_code": [{"source_code": "k,d,t = gets.split.map(&:to_i)\nif k > d\n d = (k/d.to_f).ceil * d\nend\n\ntrial = k + (d-k)/2.to_f\nn = (t/trial).to_i\na = n * d\nif (t-trial*n) <= k\n ans = n * d + t-trial*n\nelsif\n ans = n * d + k + (t-trial*n-k)/2.to_f\nend\nsprintf(\"%.0f\", ans.to_f)\n"}, {"source_code": "k,d,t = gets.split.map(&:to_i)\nif k > d\n d = (k/d.to_f).ceil * d\nend\n\ntrial = k + (d-k)/2.to_f\nn = (t/trial).to_i\na = n * d\nif (t-trial*n) <= k\n ans = n * d + t-trial*n\nelsif\n ans = n * d + k + (t-trial*n-k)/2.to_f\nend\nprintf(\"%.0f\", ans)\n"}, {"source_code": "k,d,t = gets.split.map(&:to_i)\nif k > d\n d = (k/d.to_f).ceil * d\nend\n\ntrial = k + (d-k)/2.to_f\nn = (t/trial).to_i\na = n * d\nif (t-trial*n) <= k\n ans = n * d + t-trial*n\nelsif\n ans = n * d + k + (t-trial*n-k)/2.to_f\nend\nputs ans\n"}, {"source_code": "k,d,t = gets.split.map(&:to_i)\nif k > d\n d = (k/d.to_f).ceil * d\nend\n\nper = k + (d-k)/2.to_f\nn = (t/per).to_i\nans = n * d\np ans\np n*per\nif (t-per*n) <= k\n ans += t - per*n\nelsif\n ans += k + (t-k-per*n)*2\nend\nputs sprintf(\"%.9f\", ans)\n"}, {"source_code": "k,d,t = gets.split.map(&:to_i)\nif k > d\n d = (k/d.to_f).ceil * d\nend\n\nper = k + (d-k)/2.to_f\nn = (t/per).to_i\nans = n * d\np ans\np n*per\nif (t-per*n) <= k\n ans += t - per*n\nelsif\n ans += k + (t-k-per*n)*2\nend\nputs ans\n"}], "src_uid": "9df3a94dfa537cabdc52388a771cd24f"} {"nl": {"description": "There are n banks in the city where Vasya lives, they are located in a circle, such that any two banks are neighbouring if their indices differ by no more than 1. Also, bank 1 and bank n are neighbours if n > 1. No bank is a neighbour of itself.Vasya has an account in each bank. Its balance may be negative, meaning Vasya owes some money to this bank.There is only one type of operations available: transfer some amount of money from any bank to account in any neighbouring bank. There are no restrictions on the size of the sum being transferred or balance requirements to perform this operation.Vasya doesn't like to deal with large numbers, so he asks you to determine the minimum number of operations required to change the balance of each bank account to zero. It's guaranteed, that this is possible to achieve, that is, the total balance of Vasya in all banks is equal to zero.", "input_spec": "The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of banks. The second line contains n integers ai ( - 109 ≤ ai ≤ 109), the i-th of them is equal to the initial balance of the account in the i-th bank. It's guaranteed that the sum of all ai is equal to 0.", "output_spec": "Print the minimum number of operations required to change balance in each bank to zero.", "sample_inputs": ["3\n5 0 -5", "4\n-1 0 1 0", "4\n1 2 3 -6"], "sample_outputs": ["1", "2", "3"], "notes": "NoteIn the first sample, Vasya may transfer 5 from the first bank to the third.In the second sample, Vasya may first transfer 1 from the third bank to the second, and then 1 from the second to the first.In the third sample, the following sequence provides the optimal answer: transfer 1 from the first bank to the second bank; transfer 3 from the second bank to the third; transfer 6 from the third bank to the fourth. "}, "positive_code": [{"source_code": "n = readline.to_i\narr = readline.split(' ').collect(&:to_i)\nmp = Hash.new(0)\nbest = 0\nsum = 0\narr.each do |num|\n sum += num\n mp[sum] += 1\n best = [best, mp[sum]].max\nend\nputs n - best\n"}, {"source_code": "n = gets.to_i\nf = gets.split.map { |x| x.to_i }\n\nacc = 0\nhash = Hash.new(0)\nf.each do |x|\n\tacc += x\n\thash[acc] += 1\nend\n\nans = 0\nhash.each do |key, val|\n\tans = [ans, val].max\nend\n\nputs n - ans\n"}], "negative_code": [{"source_code": "n = readline.to_i\narr = readline.split(' ').collect(&:to_i)\nmp = Hash.new\nmp[0] = true\nsum = 0\nct = 0\nfound = false\narr.each do |num|\n sum += num\n if found\n if sum == 0\n ct += 1\n end\n else\n if mp.has_key?(sum)\n found = true\n sum = 0\n ct += 1\n else\n mp[sum] = true\n end\n end\nend\nif found && sum != 0\n ct += 1\nend\nputs n - ct\n"}, {"source_code": "n = readline.to_i\narr = readline.split(' ').collect(&:to_i)\nmp = Hash.new\nmp[0] = true\nsum = 0\nct = 0\nfound = false\narr.each do |num|\n sum += num\n if found\n if sum == 0\n ct += 1\n end\n else\n if mp.has_key?(sum)\n found = true\n ct += 1\n else\n mp[sum] = true\n end\n end\nend\nputs n - ct\n"}, {"source_code": "n = readline.to_i\narr = readline.split(' ').collect(&:to_i)\nmp = Hash.new\nmp[0] = true\nsum = 0\nct = 0\nfound = false\narr.each do |num|\n sum += num\n if found\n if sum == 0\n ct += 1\n end\n else\n if mp.has_key?(sum)\n found = true\n ct += 1\n end\n end\nend\nputs n - ct\n"}], "src_uid": "be12bb8148708f9ad3dc33b83b55eb1e"} {"nl": {"description": "$$$n$$$ people gathered to hold a jury meeting of the upcoming competition, the $$$i$$$-th member of the jury came up with $$$a_i$$$ tasks, which they want to share with each other.First, the jury decides on the order which they will follow while describing the tasks. Let that be a permutation $$$p$$$ of numbers from $$$1$$$ to $$$n$$$ (an array of size $$$n$$$ where each integer from $$$1$$$ to $$$n$$$ occurs exactly once).Then the discussion goes as follows: If a jury member $$$p_1$$$ has some tasks left to tell, then they tell one task to others. Otherwise, they are skipped. If a jury member $$$p_2$$$ has some tasks left to tell, then they tell one task to others. Otherwise, they are skipped. ... If a jury member $$$p_n$$$ has some tasks left to tell, then they tell one task to others. Otherwise, they are skipped. If there are still members with tasks left, then the process repeats from the start. Otherwise, the discussion ends. A permutation $$$p$$$ is nice if none of the jury members tell two or more of their own tasks in a row. Count the number of nice permutations. The answer may be really large, so print it modulo $$$998\\,244\\,353$$$.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$2 \\le n \\le 2 \\cdot 10^5$$$) — number of jury members. The second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$) — the number of problems that the $$$i$$$-th member of the jury came up with. The sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, print one integer — the number of nice permutations, taken modulo $$$998\\,244\\,353$$$.", "sample_inputs": ["4\n2\n1 2\n3\n5 5 5\n4\n1 3 3 7\n6\n3 4 2 1 3 3"], "sample_outputs": ["1\n6\n0\n540"], "notes": "NoteExplanation of the first test case from the example:There are two possible permutations, $$$p = [1, 2]$$$ and $$$p = [2, 1]$$$. For $$$p = [1, 2]$$$, the process is the following: the first jury member tells a task; the second jury member tells a task; the first jury member doesn't have any tasks left to tell, so they are skipped; the second jury member tells a task. So, the second jury member has told two tasks in a row (in succession), so the permutation is not nice.For $$$p = [2, 1]$$$, the process is the following: the second jury member tells a task; the first jury member tells a task; the second jury member tells a task. So, this permutation is nice."}, "positive_code": [{"source_code": "@m = 998_244_353\r\n\r\ndef fact(m, n)\r\n (m..n).inject(1) { |f, i| f * i % @m }\r\nend\r\n\r\ngets.to_i.times do\r\n\r\nn = gets.to_i\r\na = gets.split.map!(&:to_i).sort!\r\n\r\nif a[-1] - a[-2] > 1\r\n puts 0\r\n next\r\nelsif a[-1] - a[-2] == 0\r\n puts fact(1, n)\r\n next\r\nend\r\n\r\nh = a.tally\r\nhk = h.keys.sort!\r\n\r\nb1 = h.delete(hk.pop) # == 1\r\nb2 = h.delete(hk.pop)\r\n\r\nc = fact(1, b2) * b2 * fact(b2 + 2, n) % @m\r\nputs c\r\n\r\nend\r\n"}], "negative_code": [], "src_uid": "81d4867a7e5128baf316193e9cc3dfce"} {"nl": {"description": "There is a polyline going through points (0, 0) – (x, x) – (2x, 0) – (3x, x) – (4x, 0) – ... - (2kx, 0) – (2kx + x, x) – .... We know that the polyline passes through the point (a, b). Find minimum positive value x such that it is true or determine that there is no such x.", "input_spec": "Only one line containing two positive integers a and b (1 ≤ a, b ≤ 109).", "output_spec": "Output the only line containing the answer. Your answer will be considered correct if its relative or absolute error doesn't exceed 10 - 9. If there is no such x then output  - 1 as the answer.", "sample_inputs": ["3 1", "1 3", "4 1"], "sample_outputs": ["1.000000000000", "-1", "1.250000000000"], "notes": "NoteYou can see following graphs for sample 1 and sample 3. "}, "positive_code": [{"source_code": "class Solver\n\tdef main\n\t\tx, y = gets.split.map { |s| s.to_i }\n\t\tif x < y then\n\t\t\tputs -1\n\t\telse\n\t\t\ttmp = (x - y) / (2 * y)\n\t\t\tputs (x + y).to_f / (2 * tmp + 2)\n\t\tend\n\tend\nend\n\nSolver.new.main\n"}], "negative_code": [], "src_uid": "1bcf130890495bcca67b4b0418476119"} {"nl": {"description": "Nauuo is a girl who loves drawing circles.One day she has drawn a circle and wanted to draw a tree on it.The tree is a connected undirected graph consisting of $$$n$$$ nodes and $$$n-1$$$ edges. The nodes are numbered from $$$1$$$ to $$$n$$$.Nauuo wants to draw a tree on the circle, the nodes of the tree should be in $$$n$$$ distinct points on the circle, and the edges should be straight without crossing each other.\"Without crossing each other\" means that every two edges have no common point or the only common point is an endpoint of both edges.Nauuo wants to draw the tree using a permutation of $$$n$$$ elements. A permutation of $$$n$$$ elements is a sequence of integers $$$p_1,p_2,\\ldots,p_n$$$ in which every integer from $$$1$$$ to $$$n$$$ appears exactly once.After a permutation is chosen Nauuo draws the $$$i$$$-th node in the $$$p_i$$$-th point on the circle, then draws the edges connecting the nodes.The tree is given, Nauuo wants to know how many permutations are there so that the tree drawn satisfies the rule (the edges are straight without crossing each other). She only wants to know the answer modulo $$$998244353$$$, can you help her?It is obvious that whether a permutation is valid or not does not depend on which $$$n$$$ points on the circle are chosen.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$2\\le n\\le 2\\cdot 10^5$$$) — the number of nodes in the tree. Each of the next $$$n-1$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1\\le u,v\\le n$$$), denoting there is an edge between $$$u$$$ and $$$v$$$. It is guaranteed that the given edges form a tree.", "output_spec": "The output contains a single integer — the number of permutations suitable to draw the given tree on a circle satisfying the rule, modulo $$$998244353$$$.", "sample_inputs": ["4\n1 2\n1 3\n2 4", "4\n1 2\n1 3\n1 4"], "sample_outputs": ["16", "24"], "notes": "NoteExample 1All valid permutations and their spanning trees are as follows.Here is an example of invalid permutation: the edges $$$(1,3)$$$ and $$$(2,4)$$$ are crossed.Example 2Every permutation leads to a valid tree, so the answer is $$$4! = 24$$$."}, "positive_code": [{"source_code": "def fact(n)\n @facts ||= [1]\n (@facts.size).upto(n){|i| @facts << @facts[-1]*i % MOD}\n return @facts[n]\nend\n\nMOD = 998244353\nn = gets.to_i\nvisited = [false]*(n+1)\nchilds = Array.new(n+1){[]}\n(n-1).times do\n a,b = gets.split.map(&:to_i)\n childs[a] << b\n childs[b] << a\nend\nvisited[1] = true\nque = childs[1].dup\nans = fact(childs[1].length)\n\nwhile v = que.shift\n visited[v] = true\n cnt = 0\n childs[v].each do |u| \n if !visited[u]\n que << u\n cnt += 1\n end\n end\n ans = (ans*fact(cnt+1)) % MOD\n #p [v,ans]\nend\np ans*n % MOD"}], "negative_code": [], "src_uid": "03bfe285856fa74b89de7a1bebb14bca"} {"nl": {"description": "Leo has developed a new programming language C+=. In C+=, integer variables can only be changed with a \"+=\" operation that adds the right-hand side value to the left-hand side variable. For example, performing \"a += b\" when a = $$$2$$$, b = $$$3$$$ changes the value of a to $$$5$$$ (the value of b does not change).In a prototype program Leo has two integer variables a and b, initialized with some positive values. He can perform any number of operations \"a += b\" or \"b += a\". Leo wants to test handling large integers, so he wants to make the value of either a or b strictly greater than a given value $$$n$$$. What is the smallest number of operations he has to perform?", "input_spec": "The first line contains a single integer $$$T$$$ ($$$1 \\leq T \\leq 100$$$) — the number of test cases. Each of the following $$$T$$$ lines describes a single test case, and contains three integers $$$a, b, n$$$ ($$$1 \\leq a, b \\leq n \\leq 10^9$$$) — initial values of a and b, and the value one of the variables has to exceed, respectively.", "output_spec": "For each test case print a single integer — the smallest number of operations needed. Separate answers with line breaks.", "sample_inputs": ["2\n1 2 3\n5 4 100"], "sample_outputs": ["2\n7"], "notes": "NoteIn the first case we cannot make a variable exceed $$$3$$$ in one operation. One way of achieving this in two operations is to perform \"b += a\" twice."}, "positive_code": [{"source_code": "t = gets.to_i\nt.times do\n\ta, b, n = gets.split.map(&:to_i)\n\tb, a = a, b if a < b\n\t# a >= b\n\n\tc = 0\n\tuntil n < a\n\t\tb += a\n\t\ta, b = b, a if a < b\n\t\tc += 1\n\tend\n\n\tp c\nend\n"}], "negative_code": [], "src_uid": "5999a4e2fac29b5f4804639f6e949279"} {"nl": {"description": "Monocarp is playing a computer game. Now he wants to complete the first level of this game.A level is a rectangular grid of $$$2$$$ rows and $$$n$$$ columns. Monocarp controls a character, which starts in cell $$$(1, 1)$$$ — at the intersection of the $$$1$$$-st row and the $$$1$$$-st column.Monocarp's character can move from one cell to another in one step if the cells are adjacent by side and/or corner. Formally, it is possible to move from cell $$$(x_1, y_1)$$$ to cell $$$(x_2, y_2)$$$ in one step if $$$|x_1 - x_2| \\le 1$$$ and $$$|y_1 - y_2| \\le 1$$$. Obviously, it is prohibited to go outside the grid.There are traps in some cells. If Monocarp's character finds himself in such a cell, he dies, and the game ends.To complete a level, Monocarp's character should reach cell $$$(2, n)$$$ — at the intersection of row $$$2$$$ and column $$$n$$$.Help Monocarp determine if it is possible to complete the level.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. Then the test cases follow. Each test case consists of three lines. The first line contains a single integer $$$n$$$ ($$$3 \\le n \\le 100$$$) — the number of columns. The next two lines describe the level. The $$$i$$$-th of these lines describes the $$$i$$$-th line of the level — the line consists of the characters '0' and '1'. The character '0' corresponds to a safe cell, the character '1' corresponds to a trap cell. Additional constraint on the input: cells $$$(1, 1)$$$ and $$$(2, n)$$$ are safe.", "output_spec": "For each test case, output YES if it is possible to complete the level, and NO otherwise.", "sample_inputs": ["4\n3\n000\n000\n4\n0011\n1100\n4\n0111\n1110\n6\n010101\n101010"], "sample_outputs": ["YES\nYES\nNO\nYES"], "notes": "NoteConsider the example from the statement.In the first test case, one of the possible paths is $$$(1, 1) \\rightarrow (2, 2) \\rightarrow (2, 3)$$$.In the second test case, one of the possible paths is $$$(1, 1) \\rightarrow (1, 2) \\rightarrow (2, 3) \\rightarrow (2, 4)$$$.In the fourth test case, one of the possible paths is $$$(1, 1) \\rightarrow (2, 2) \\rightarrow (1, 3) \\rightarrow (2, 4) \\rightarrow (1, 5) \\rightarrow (2, 6)$$$."}, "positive_code": [{"source_code": "# 4\r\n# 3\r\n# 000\r\n# 000\r\n# 4\r\n# 0011\r\n# 1100\r\n# 4\r\n# 0111\r\n# 1110\r\n# 6\r\n# 010101\r\n# 101010\r\n\r\nn = gets.to_i\r\n\r\ndef result\r\n n = gets.to_i\r\n arr = 2.times.map { gets.split('').map(&:to_i) }\r\n if n.times.all? do |i|\r\n arr[0][i] == 0 || arr[1][i] == 0\r\n end\r\n 'YES'\r\n else\r\n 'NO'\r\n end\r\nend\r\n\r\nn.times do\r\n puts result\r\nend\r\n"}, {"source_code": "def dfs(grid, x, y)\n if y == 1 && x == grid.first.length\n return true\n elsif grid[y].nil? || grid[y][x].nil? || grid[y][x] == \"1\"\n return false\n else\n grid[y][x] = \"1\"\n return dfs(grid, x, (y + 1) % 2) ||\n dfs(grid, x + 1, y) ||\n dfs(grid, x + 1, (y + 1) % 2)\n end\nend\n\nSTDIN.read\n .split(\"\\n\")\n .drop(1)\n .each_slice(3) do |arr|\n puts(dfs([arr[1].split(\"\"), arr[2].split(\"\")], 0, 0) ? \"YES\" : \"NO\")\n end\n"}, {"source_code": "t= gets.chomp.to_i\r\nt.times do\r\n n=gets.chomp.to_i\r\n s1=gets.chomp.chars\r\n s2=gets.chomp.chars\r\n n.times do |i|\r\n if s1[i].to_i+s2[i].to_i==2\r\n puts \"NO\"\r\n break\r\n elsif i == n-1\r\n puts \"YES\" \r\n end\r\n end\r\nend"}, {"source_code": " t = gets.to_i\r\n t.times{|w|\r\n n = gets.to_i\r\n board = [gets.chomp.to_s,gets.chomp.to_s]\r\n path = [0,0]\r\n find = false\r\n visited = []\r\n while path.size != 0\r\n node = path.shift\r\n if node[0] == n-1 && node[1] == 1\r\n find = true\r\n break\r\n end\r\n if (!visited.include?(node))\r\n visited<=0 && y < 2}\r\n dir.map{|w|\r\n if (board[w[1]][w[0]] == \"0\")\r\n path<= mx\n puts \"YES\"\n else\n puts \"NO\"\n end\n}"}], "negative_code": [{"source_code": "a = gets.chomp.to_i\n(0..a-1).each do |i|\n s = gets.chomp.split(' ')\n r = s[0].to_i; g = s[1].to_i; b = s[2].to_i;\n if ((r/g)>2 || (r/b)>2 || (g/b)>2 || (g/r)>2 || (b/r)>2 || (b/g)>2)\n puts 'No'\n else\n puts 'Yes'\n end\nend\n"}, {"source_code": "a = gets.chomp.to_i\n(0..a-1).each do |i|\n s = gets.chomp.split(' ')\n r = s[0].to_i; g = s[1].to_i; b = s[2].to_i;\n if ((r-g).abs>2 || (r-b).abs>2 || (g-b).abs>2)\n puts 'No'\n else\n puts 'Yes'\n end\nend"}, {"source_code": "gets.strip.to_i.times {\n x, y, k = gets.strip.split(' ').map(&:to_i)\n if [x, y, k].max - [x, y, k].min <= 3\n puts \"YES\"\n else\n puts \"NO\"\n end\n}"}], "src_uid": "34aa41871ee50f06e8acbd5eee94b493"} {"nl": {"description": "Lee was cleaning his house for the party when he found a messy string under the carpets. Now he'd like to make it clean accurately and in a stylish way...The string $$$s$$$ he found is a binary string of length $$$n$$$ (i. e. string consists only of 0-s and 1-s).In one move he can choose two consecutive characters $$$s_i$$$ and $$$s_{i+1}$$$, and if $$$s_i$$$ is 1 and $$$s_{i + 1}$$$ is 0, he can erase exactly one of them (he can choose which one to erase but he can't erase both characters simultaneously). The string shrinks after erasing.Lee can make an arbitrary number of moves (possibly zero) and he'd like to make the string $$$s$$$ as clean as possible. He thinks for two different strings $$$x$$$ and $$$y$$$, the shorter string is cleaner, and if they are the same length, then the lexicographically smaller string is cleaner.Now you should answer $$$t$$$ test cases: for the $$$i$$$-th test case, print the cleanest possible string that Lee can get by doing some number of moves.Small reminder: if we have two strings $$$x$$$ and $$$y$$$ of the same length then $$$x$$$ is lexicographically smaller than $$$y$$$ if there is a position $$$i$$$ such that $$$x_1 = y_1$$$, $$$x_2 = y_2$$$,..., $$$x_{i - 1} = y_{i - 1}$$$ and $$$x_i < y_i$$$.", "input_spec": "The first line contains the integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. Next $$$2t$$$ lines contain test cases — one per two lines. The first line of each test case contains the integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$) — the length of the string $$$s$$$. The second line contains the binary string $$$s$$$. The string $$$s$$$ is a string of length $$$n$$$ which consists only of zeroes and ones. It's guaranteed that sum of $$$n$$$ over test cases doesn't exceed $$$10^5$$$.", "output_spec": "Print $$$t$$$ answers — one per test case. The answer to the $$$i$$$-th test case is the cleanest string Lee can get after doing some number of moves (possibly zero).", "sample_inputs": ["5\n10\n0001111111\n4\n0101\n8\n11001101\n10\n1110000000\n1\n1"], "sample_outputs": ["0001111111\n001\n01\n0\n1"], "notes": "NoteIn the first test case, Lee can't perform any moves.In the second test case, Lee should erase $$$s_2$$$.In the third test case, Lee can make moves, for example, in the following order: 11001101 $$$\\rightarrow$$$ 1100101 $$$\\rightarrow$$$ 110101 $$$\\rightarrow$$$ 10101 $$$\\rightarrow$$$ 1101 $$$\\rightarrow$$$ 101 $$$\\rightarrow$$$ 01."}, "positive_code": [{"source_code": "require 'set'; require 'prime'; require 'pp'\nINF=Float::INFINITY; MOD=10**9+7\nt = gets.chomp.to_i\nt.times do\n n = gets.chomp.to_i\n s = gets.chomp\n\n l = s.index('1')\n r = s.reverse.index('0')\n if l.nil? || r.nil?\n puts s\n next\n end\n\n r = n - r - 1\n\n if r < l\n puts s\n else\n left = s.slice(0, l) || ''\n right = s.slice(r + 1, s.size) || ''\n # [[l, r], [left, right]]\n puts left + '0' + right\n end\nend\n"}], "negative_code": [], "src_uid": "bb071f1f4fc1c129a32064c1301f4942"} {"nl": {"description": "Vasya has recently found out what a digital root of a number is and he decided to share his knowledge with you.Let's assume that S(n) is the sum of digits of number n, for example, S(4098) = 4 + 0 + 9 + 8 = 21. Then the digital root of number n equals to: dr(n) = S(n), if S(n) < 10; dr(n) = dr( S(n) ), if S(n) ≥ 10. For example, dr(4098)  =  dr(21)  =  3.Vasya is afraid of large numbers, so the numbers he works with are at most 101000. For all such numbers, he has proved that dr(n)  =  S( S( S( S(n) ) ) ) (n ≤ 101000).Now Vasya wants to quickly find numbers with the given digital root. The problem is, he hasn't learned how to do that and he asked you to help him. You task is, given numbers k and d, find the number consisting of exactly k digits (the leading zeroes are not allowed), with digital root equal to d, or else state that such number does not exist.", "input_spec": "The first line contains two integers k and d (1 ≤ k ≤ 1000; 0 ≤ d ≤ 9).", "output_spec": "In a single line print either any number that meets the requirements (without the leading zeroes) or \"No solution\" (without the quotes), if the corresponding number does not exist. The chosen number must consist of exactly k digits. We assume that number 0 doesn't contain any leading zeroes.", "sample_inputs": ["4 4", "5 1", "1 0"], "sample_outputs": ["5881", "36172", "0"], "notes": "NoteFor the first test sample dr(5881)  =  dr(22)  =  4.For the second test sample dr(36172)  =  dr(19)  =  dr(10)  =  1."}, "positive_code": [{"source_code": "line = gets\nline = line.split(\" \");\nk = line[0].to_i\nd = line[1].to_i\n\nans = d * 10**(k-1) \n\nif d == 0 && k > 1\n ans = \"No solution\"\nend \n\nans = ans.to_s << \"\\n\"\nprint(ans)"}, {"source_code": "k,d=gets.chomp.split(\" \")\nputs \"No solution\" if k != \"1\" && d == \"0\"\nputs 0 if k == \"1\" && d == \"0\"\nputs \"#{d}#{\"0\"*(k.to_i-1)}\" if d != \"0\""}, {"source_code": "k, d = gets.split.map(&:to_i)\n\nif d == 0 && k > 1\n print \"No solution\"\nelse\n print d\n for i in 1...k\n print 0\n end\nend\n"}, {"source_code": "k,d=gets.chomp.split.map(&:to_i)\nif d==0 && k>1\n puts \"No solution\"\nelse\n print d\n (k-1).times { print 0}\nend\n"}, {"source_code": "k,d=gets.chomp.split.map &:to_i \nputs d==0&&k>1?\"No solution\":d.to_s+'0'*(k-1)\n"}, {"source_code": "dcnt, droot = gets.split.map{|s| s.to_i}\n\nif droot == 0 && dcnt > 1\n\tputs \"No solution\"\nelse\n\tprint droot\n\t(dcnt-1).times {print 0}\nend\n"}, {"source_code": "k,d=gets.chomp.split.map &:to_i \nputs d==0&&k>1?\"No solution\":d.to_s+'0'*(k-1)\n"}, {"source_code": "k,d=gets.chomp.split.map(&:to_i)\nif d==0\n\tif k==1\n\t\tputs \"0\"\n\telse\n\t\tputs \"No solution\"\n\tend\nelsif d>0\n\tputs d*(10**(k-1))\nelse\n\tputs \"No solution\"\nend"}, {"source_code": "class A\n def initialize\n k, n = gets.chomp.split(' ').map(&:to_i)\n\n start = 0\n limit = 9 * k\n\n if k > 1 && n == 0\n puts \"No solution\"\n exit\n end\n\n (0..limit).each do |num|\n res = dfs(num)\n\n if res == n \n puts answer(num, k)\n exit\n end\n end\n\n puts \"No solution\"\n end\n\n def answer(num, k)\n list = [ 9, 8, 7, 6, 5, 4, 3, 2, 1 ]\n index = 0\n answer = \"\"\n while num != 0\n count = num / list[index]\n num %= list[index]\n answer += list[index].to_s * count\n index += 1\n end\n\n if answer.to_s.size != k\n diff = k - answer.to_s.size\n answer += \"0\" * diff\n end\n \n answer\n end\n\n def dfs(num)\n if num < 10\n return num\n else\n return dfs(sum_digit(num))\n end\n end\n\n def sum_digit(num)\n str = num.to_s\n\n str.chars.map(&:to_i).inject(:+)\n end\nend\n\na = A.new"}, {"source_code": "k, d = gets.chomp.split(/ /).map(&:to_i)\nif k > 1 && d == 0\n puts \"No solution\"\nelse\n if k == 1 && d == 0\n puts 0\n else\n puts \"#{d * (10 ** (k - 1))}\"\n end\nend\n"}, {"source_code": "k, d = gets.split.map(&:to_i)\nif d == 0 && k > 1\n print \"No solution\"\nelse\n print d\n for i in 1..k-1\n print 0\n end\nend"}, {"source_code": "# def digital_root(n)\n# \treturn @dp[n] if @dp[n]\n# \ts = n.to_s.split(\"\").map(&:to_i).inject(&:+)\n# \tif s < 10\n# \t\t@dp[n] = s\n# \t\treturn s\n# \tend\n# \t@dp[n] = digital_root(s)\n# \treturn @dp[n]\n# end\n\n# digits,d = gets.split(\" \").map(&:to_i)\n# @dp = {}\n# start = 10 ** (digits - 1)\n# start = 0 if start == 1\n# finish = (10 ** digits) - 1\n# ans = nil\n# if d == 0\n# \tif digits == 1\n# \t\tans = 0\n# \tend\n# else\n# \t(start..finish).each do |a|\n# \t\tif digital_root(a) == d\n# \t\t\tans = a\n# \t\t\tbreak\n# \t\tend\n# \tend\n# end\n# if ans\n# \tputs ans\n# else\n# \tputs \"No solution\"\n# end\n\n\ndef digital_root(n)\n\ts = n.to_s.split(\"\").map(&:to_i).inject(&:+)\nend\n\ndigits,d = gets.split(\" \").map(&:to_i)\n@dp = {}\nstart = 10 ** (digits - 1)\nstart = 0 if start == 1\nfinish = start + 10\nans = nil\n\n(start..finish).each do |a|\n\tif digital_root(a) == d\n\t\tans = a\n\t\tbreak\n\tend\nend\n\nif ans\n\tputs ans\nelse\n\tputs \"No solution\"\nend\n"}, {"source_code": "def digital_root(n)\n\treturn @dp[n] if @dp[n]\n\ts = n.to_s.split(\"\").map(&:to_i).inject(&:+)\n\tif s < 10\n\t\t@dp[n] = s\n\t\treturn s\n\tend\n\t@dp[n] = digital_root(s)\n\treturn @dp[n]\nend\n\ndigits,d = gets.split(\" \").map(&:to_i)\n@dp = {}\nstart = 10 ** (digits - 1)\nstart = 0 if start == 1\nfinish = (10 ** digits) - 1\nans = nil\nif d == 0\n\tif digits == 1\n\t\tans = 0\n\tend\nelse\n\t(start..finish).each do |a|\n\t\tif digital_root(a) == d\n\t\t\tans = a\n\t\t\tbreak\n\t\tend\n\tend\nend\nif ans\n\tputs ans\nelse\n\tputs \"No solution\"\nend\n"}, {"source_code": "puts [gets.split.map(&:to_i)].map{|x| ((x[0]>1 && x[1]==0)? \"No solution\" : x[1]*(10**(x[0]-1)))}[0]"}, {"source_code": "k, d = gets.split.collect{|i| i.to_i}\nif d == 0\n if k == 1\n puts 0\n else\n puts 'No solution'\n end\n exit\nend\nputs (Array.new([d])+Array.new(k-1,0)).join\n"}, {"source_code": "def solution(k, n)\n return 0 if n == 0 && k == 1\n return \"No solution\" if n == 0 && k > 1\n\n num = num_k(k)\n\n until dr(num) == n || num.to_s.size != k do\n num += 1\n end\n\n return \"No solution\" if num.to_s.size != k \n num\nend\n\ndef num_k(k)\n min_num = \"1\"\n\n (k-1).times { |i| min_num << \"0\" }\n min_num.to_i\nend\n\ndef dr(n)\n return n if n < 10\n num = n.to_s.split('').map(&:to_i).inject(:+)\n dr(num)\nend\n\nk, n = STDIN.gets.chomp.split(\" \").map(&:to_i)\nputs solution(k, n)\n"}, {"source_code": "k, d = gets.split\n\nif k == '1'\n puts d\nelse\n if d == '0'\n puts 'No solution'\n else\n puts d + ('0' * k.to_i.pred)\n end\nend"}, {"source_code": "k, d = gets.chomp.split.map{ |i| i.to_i}\n\nif d == 0 and k > 1\n\tputs \"No solution\"\nelse\n\tputs (d.to_s + (\"0\" * (k - 1)))\nend\n\n"}], "negative_code": [{"source_code": "line = gets\nline = line.split(\" \");\nk = line[0].to_i\nd = line[1].to_i\nmax = 10 ** (d-1)\n\nwhile 1 && d != 0\n ans = rand((10**d) - 1) + max\n if ans % 9 == k\n break\n end\nend\n\nif d == 0 \n ans = 0\nend\n\np ans"}, {"source_code": "line = gets\nline = line.split(\" \");\nk = line[0].to_i\nd = line[1].to_i\nmax = 10 ** (k-1)\n\nif d != 0\n d = 0 if d == 9\n ans = rand((10**k) - 1) + max\n while ans % 9 != d\n ans += 1\n end\nelse\n if k == 1\n ans = 0\n else\n ans = \"No solution\"\n end\nend\n\nans = ans.to_s << \"\\n\"\nprint(ans)"}, {"source_code": "line = gets\nline = line.split(\" \");\nk = line[0].to_i\nd = line[1].to_i\nmax = 10 ** (k-1)\n\nwhile 1 && d != 0\n ans = rand((10**k) - 1) + max\n if ans % 9 == d\n break\n end\nend\n\nif d == 0 \n ans = 0\nend\n\np ans"}, {"source_code": "line = gets\nline = line.split(\" \");\nk = line[0].to_i\nd = line[1].to_i\nmax = 10 ** d\n\nwhile 1\n ans = rand(max-1) + max\n if ans % 9 == k\n break\n end\nend\n\np ans"}, {"source_code": "line = gets\nline = line.split(\" \");\nk = line[0].to_i\nd = line[1].to_i\nmax = 10 ** d\n\nwhile 1 && d != 0\n ans = rand(max-1) + max\n if ans % 9 == k\n break\n end\nend\n\nif d == 0 \n ans = 0\nend\n\np ans"}, {"source_code": "k,d=gets.chomp.split(\" \")\nputs \"No solution\" if k == \"0\" && d == \"0\"\nputs 0 if k == \"1\" && d == \"0\"\nputs \"#{d}#{\"0\"*(k.to_i-1)}\" if d != \"0\""}, {"source_code": "class A\n def initialize\n k, n = gets.chomp.split(' ').map(&:to_i)\n\n start = 0\n limit = 9 * k\n\n (0..limit).each do |num|\n res = dfs(num)\n\n if res == n \n puts answer(num, k)\n exit\n end\n end\n\n puts \"No solution\"\n end\n\n def answer(num, k)\n list = [ 9, 8, 7, 6, 5, 4, 3, 2, 1 ]\n index = 0\n answer = \"\"\n while num != 0\n count = num / list[index]\n num %= list[index]\n answer += list[index].to_s * count\n index += 1\n end\n\n if answer.to_s.size != k\n diff = k - answer.to_s.size\n answer += \"0\" * diff\n end\n \n answer\n end\n\n def dfs(num)\n if num < 10\n return num\n else\n return dfs(sum_digit(num))\n end\n end\n\n def sum_digit(num)\n str = num.to_s\n\n str.chars.map(&:to_i).inject(:+)\n end\nend\n\na = A.new"}, {"source_code": "k, d = gets.chomp.split(/ /).map(&:to_i)\nif k > 1 && d == 0\n puts \"No solution\"\nelse\n if k == 1 && d == 0\n puts 0\n else\n puts \"#{k * (10 ** (d - 1))}\"\n end\nend\n"}, {"source_code": "def digital_root(n)\n\treturn @dp[n] if @dp[n]\n\ts = n.to_s.split(\"\").map(&:to_i).inject(&:+)\n\tif s < 10\n\t\t@dp[n] = s\n\t\treturn s\n\tend\n\t@dp[n] = digital_root(s)\n\treturn @dp[n]\nend\n\ndigits,d = gets.split(\" \").map(&:to_i)\n@dp = {}\nstart = 10 ** (digits - 1)\nstart = 0 if start == 1\nfinish = (10 ** digits) - 1\nans = nil\nif d == 0\n\tif digits == 0\n\t\tans = 0\n\tend\nelse\n\t(start..finish).each do |a|\n\t\tif digital_root(a) == d\n\t\t\tans = a\n\t\t\tbreak\n\t\tend\n\tend\nend\nif ans\n\tputs ans\nelse\n\tputs \"No Solution\"\nend\n"}, {"source_code": "def digital_root(n)\n\treturn @dp[n] if @dp[n]\n\ts = n.to_s.split(\"\").map(&:to_i).inject(&:+)\n\tif s < 10\n\t\t@dp[n] = s\n\t\treturn s\n\tend\n\t@dp[n] = digital_root(s)\n\treturn @dp[n]\nend\n\ndigits,d = gets.split(\" \").map(&:to_i)\n@dp = {}\nstart = 10 ** (digits - 1)\nstart = 0 if start == 1\nfinish = (10 ** digits) - 1\nans = nil\nif d == 0\n\tif digits == 1\n\t\tans = 0\n\tend\nelse\n\t(start..finish).each do |a|\n\t\tif digital_root(a) == d\n\t\t\tans = a\n\t\t\tbreak\n\t\tend\n\tend\nend\nif ans\n\tputs ans\nelse\n\tputs \"No Solution\"\nend\n"}, {"source_code": "puts [gets.split.map(&:to_i)].map{|x| ((x[0]>0 && x[1]==0)? \"No solution\" : x[1]*(10**(x[0]-1)))}[0]"}, {"source_code": "p [gets.split.map(&:to_i)].map{|x| x[1]*(10**(x[0]-1))}.max"}, {"source_code": "p [gets.split.map(&:to_i)].map{|x| x[1]*(10**x[0])}.max"}, {"source_code": "def solution(k, n)\n return 0 if n == 0\n\n num = num_k(k)\n\n until dr(num) == n || num.to_s.size != k do\n num += 1\n end\n\n return \"No solution\" if num.to_s.size != k\n num\nend\n\ndef num_k(k)\n num = \"1\"\n (k-1).times { |i| num << \"0\" }\n num.to_i\nend\n\ndef dr(n)\n num = n.to_s.split('').map(&:to_i).inject(:+)\n return dr(num) if num >= 10\n num\nend\n\nk, n = STDIN.gets.chomp.split(\" \").map(&:to_i)\nputs solution(k, n)\n"}, {"source_code": "def solution(k, n)\n return 0 if n == 0\n\n\n num = num_k(k)\n\n until dr(num) == n || num.to_s.size != k do\n num += 1\n end\n\n return \"No solution\" if num.to_s.size != k\n num\nend\n\ndef num_k(k)\n min_num = \"1\"\n\n (k-1).times { |i| min_num << \"0\" }\n min_num.to_i\nend\n\ndef dr(n)\n return n if n < 10\n num = n.to_s.split('').map(&:to_i).inject(:+)\n dr(num)\nend\n\nk, n = STDIN.gets.chomp.split(\" \").map(&:to_i)\nputs solution(k, n)\n"}, {"source_code": "def solution(k, n)\n return 0 if n == 0\n\n min_num, max_num = num_k(k)\n\n num = rand(min_num..max_num) \n\n until dr(num) == n || num.to_s.size != k do\n num += 1\n end\n\n return \"No solution\" if num.to_s.size != k\n num\nend\n\ndef num_k(k)\n min_num = \"1\"\n max_num = \"1\"\n\n (k-1).times { |i| min_num << \"0\" }\n k.times { |i| max_num << \"0\" }\n [min_num.to_i, max_num.to_i]\nend\n\ndef dr(n)\n num = n.to_s.split('').map(&:to_i).inject(:+)\n return dr(num) if num >= 10\n num\nend\n\nk, n = STDIN.gets.chomp.split(\" \").map(&:to_i)\nputs solution(k, n)\n"}, {"source_code": "def solution(k, n)\n return 0 if n == 0\n\n num = num_k(k)\n until dr(num) == n && num.to_s.size == k do\n num += 1\n end\n\n return \"No solution\" if num.to_s.size != k\n num\nend\n\ndef num_k(k)\n num = \"1\"\n (k-1).times { |i| num << \"0\" }\n num.to_i\nend\n\ndef dr(n)\n num = n.to_s.split('').map(&:to_i).inject(:+)\n return dr(num) if num >= 10\n num\nend\n\nk, n = STDIN.gets.chomp.split(\" \").map(&:to_i)\nputs solution(k, n)\n"}, {"source_code": "def solution(k, n)\n return 0 if n == 0\n\n min_num, max_num = num_k(k)\n\n num = rand(min_num..max_num) \n\n until dr(num) == n || num.to_s.size != k do\n num += 1\n end\n\n return \"No solution\" if num.to_s.size != k\n num\nend\n\ndef num_k(k)\n min_num = \"1\"\n max_num = \"1\"\n\n (k-1).times { |i| min_num << \"0\" }\n k.times { |i| max_num << \"0\" }\n [min_num.to_i, max_num.to_i]\nend\n\ndef dr(n)\n return n if n < 10\n num = n.to_s.split('').map(&:to_i).inject(:+)\n return dr(num) if num >= 10\n num\nend\n\nk, n = STDIN.gets.chomp.split(\" \").map(&:to_i)\nputs solution(k, n)\n"}, {"source_code": "k, d = gets.split.map(&:to_i)\n\nif k.zero?\n if d.zero?\n puts 0\n else\n puts 'No solution'\n end\nelse\n puts d.to_s + ('0' * k.pred)\nend"}, {"source_code": "k, d = gets.split.map(&:to_i)\n\nif d.zero?\n if k.zero?\n puts 0\n else\n puts 'No solution'\n end\nelse\n puts d.to_s + ('0' * k.pred)\nend"}], "src_uid": "5dd0d518f315d81204b25e48fea0793a"} {"nl": {"description": "Boboniu likes playing chess with his employees. As we know, no employee can beat the boss in the chess game, so Boboniu has never lost in any round.You are a new applicant for his company. Boboniu will test you with the following chess question:Consider a $$$n\\times m$$$ grid (rows are numbered from $$$1$$$ to $$$n$$$, and columns are numbered from $$$1$$$ to $$$m$$$). You have a chess piece, and it stands at some cell $$$(S_x,S_y)$$$ which is not on the border (i.e. $$$2 \\le S_x \\le n-1$$$ and $$$2 \\le S_y \\le m-1$$$).From the cell $$$(x,y)$$$, you can move your chess piece to $$$(x,y')$$$ ($$$1\\le y'\\le m, y' \\neq y$$$) or $$$(x',y)$$$ ($$$1\\le x'\\le n, x'\\neq x$$$). In other words, the chess piece moves as a rook. From the cell, you can move to any cell on the same row or column.Your goal is to visit each cell exactly once. Can you find a solution?Note that cells on the path between two adjacent cells in your route are not counted as visited, and it is not required to return to the starting point.", "input_spec": "The only line of the input contains four integers $$$n$$$, $$$m$$$, $$$S_x$$$ and $$$S_y$$$ ($$$3\\le n,m\\le 100$$$, $$$2 \\le S_x \\le n-1$$$, $$$2 \\le S_y \\le m-1$$$) — the number of rows, the number of columns, and the initial position of your chess piece, respectively.", "output_spec": "You should print $$$n\\cdot m$$$ lines. The $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \\leq x_i \\leq n$$$, $$$1 \\leq y_i \\leq m$$$), denoting the $$$i$$$-th cell that you visited. You should print exactly $$$nm$$$ pairs $$$(x_i, y_i)$$$, they should cover all possible pairs $$$(x_i, y_i)$$$, such that $$$1 \\leq x_i \\leq n$$$, $$$1 \\leq y_i \\leq m$$$. We can show that under these constraints there always exists a solution. If there are multiple answers, print any.", "sample_inputs": ["3 3 2 2", "3 4 2 2"], "sample_outputs": ["2 2\n1 2\n1 3\n2 3\n3 3\n3 2\n3 1\n2 1\n1 1", "2 2\n2 1\n2 3\n2 4\n1 4\n3 4\n3 3\n3 2\n3 1\n1 1\n1 2\n1 3"], "notes": "NotePossible routes for two examples: "}, "positive_code": [{"source_code": "w,h,sx,sy = gets.chomp.split(\" \").map(&:to_i)\nx = sx\ny = sy\nw.times do |i|\n h.times do |j|\n puts \"#{x} #{y}\"\n if j != h-1\n y = (y+1) % h\n y = h if y == 0\n end\n end\n x = (x+1) % w\n x = w if x == 0\nend"}], "negative_code": [{"source_code": "h,w,sx,sy = gets.chomp.split(\" \").map(&:to_i)\nx = sx\ny = sy\nw.times do |i|\n h.times do |j|\n puts \"#{x} #{y}\"\n if j != h-1\n y = (y+1) % h\n y = h if y == 0\n end\n end\n x = (x+1) % w\n x = w if x == 0\nend"}], "src_uid": "ed26479cdf72ad9686bbf334d90aa0be"} {"nl": {"description": "You are given a positive integer $$$n$$$. Your task is to find any three integers $$$a$$$, $$$b$$$ and $$$c$$$ ($$$0 \\le a, b, c \\le 10^9$$$) for which $$$(a\\oplus b)+(b\\oplus c)+(a\\oplus c)=n$$$, or determine that there are no such integers.Here $$$a \\oplus b$$$ denotes the bitwise XOR of $$$a$$$ and $$$b$$$. For example, $$$2 \\oplus 4 = 6$$$ and $$$3 \\oplus 1=2$$$.", "input_spec": "Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. The following lines contain the descriptions of the test cases. The only line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^9$$$). ", "output_spec": "For each test case, print any three integers $$$a$$$, $$$b$$$ and $$$c$$$ ($$$0 \\le a, b, c \\le 10^9$$$) for which $$$(a\\oplus b)+(b\\oplus c)+(a\\oplus c)=n$$$. If no such integers exist, print $$$-1$$$.", "sample_inputs": ["5\n\n4\n\n1\n\n12\n\n2046\n\n194723326"], "sample_outputs": ["3 3 1\n-1\n2 4 6\n69 420 666\n12345678 87654321 100000000"], "notes": "NoteIn the first test case, $$$a=3$$$, $$$b=3$$$, $$$c=1$$$, so $$$(3 \\oplus 3)+(3 \\oplus 1) + (3 \\oplus 1)=0+2+2=4$$$.In the second test case, there are no solutions.In the third test case, $$$(2 \\oplus 4)+(4 \\oplus 6) + (2 \\oplus 6)=6+2+4=12$$$."}, "positive_code": [{"source_code": "num_cases = gets.to_i\nfor i in (1..num_cases)\n n = gets.to_i\n if n % 2 == 0\n puts \"0 0 #{n/2}\"\n else\n puts \"-1\"\n end\nend\n"}, {"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n if n % 2 == 0\r\n a, b, c = n / 2 ^ 1, n / 2 ^ 1, 1\r\n puts [a, b, c] * \" \"\r\n else\r\n p -1\r\n end\r\nend\r\n"}, {"source_code": "gets.to_i.times do\n n = gets.to_i\n\n if n.odd?\n puts '-1'\n next\n end\n\n puts \"0 0 #{n / 2}\"\nend\n"}, {"source_code": "a = gets.chomp.to_i\r\nuntil a == 0 do\r\n x = gets.chomp.to_i\r\n if x % 2 == 0\r\n puts \"#{x/2} 0 #{x/2}\"\r\n else\r\n puts -1\r\n end\r\n a -= 1\r\nend"}], "negative_code": [{"source_code": "num_cases = gets.to_i\nfor i in (1..num_cases)\n n = gets.to_i\n if n % 2 == 0\n puts \"0 #{n/2} #{n/2+1}\"\n else\n puts \"-1\"\n end\nend\n"}], "src_uid": "43041076ddd0bbfac62cd4abf4536282"} {"nl": {"description": "While working at DTL, Ela is very aware of her physical and mental health. She started to practice various sports, such as Archery, Yoga, and Football.Since she started engaging in sports activities, Ela switches to trying a new sport on days she considers being \"Luxury\" days. She counts the days since she started these activities, in which the day she starts is numbered as day $$$1$$$. A \"Luxury\" day is the day in which the number of this day is a luxurious number. An integer $$$x$$$ is called a luxurious number if it is divisible by $$${\\lfloor \\sqrt{x} \\rfloor}$$$.Here $$$\\lfloor r \\rfloor$$$ denotes the \"floor\" of a real number $$$r$$$. In other words, it's the largest integer not greater than $$$r$$$.For example: $$$8$$$, $$$56$$$, $$$100$$$ are luxurious numbers, since $$$8$$$ is divisible by $$$\\lfloor \\sqrt{8} \\rfloor = \\lfloor 2.8284 \\rfloor = 2$$$, $$$56$$$ is divisible $$$\\lfloor \\sqrt{56} \\rfloor = \\lfloor 7.4833 \\rfloor = 7$$$, and $$$100$$$ is divisible by $$$\\lfloor \\sqrt{100} \\rfloor = \\lfloor 10 \\rfloor = 10$$$, respectively. On the other hand $$$5$$$, $$$40$$$ are not, since $$$5$$$ are not divisible by $$$\\lfloor \\sqrt{5} \\rfloor = \\lfloor 2.2361 \\rfloor = 2$$$, and $$$40$$$ are not divisible by $$$\\lfloor \\sqrt{40} \\rfloor = \\lfloor 6.3246 \\rfloor = 6$$$.Being a friend of Ela, you want to engage in these fitness activities with her to keep her and yourself accompanied (and have fun together, of course). Between day $$$l$$$ and day $$$r$$$, you want to know how many times she changes the activities.", "input_spec": "Each test contains multiple test cases. The first line has the number of test cases $$$t$$$ ($$$1 \\le t \\le 10\\ 000$$$). The description of the test cases follows. The only line of each test case contains two integers $$$l$$$ and $$$r$$$ ($$$1 \\le l \\le r \\le 10^{18}$$$) — the intervals at which you want to know how many times Ela changes her sports.", "output_spec": "For each test case, output an integer that denotes the answer.", "sample_inputs": ["5\n\n8 19\n\n8 20\n\n119 121\n\n1 100000000000000000\n\n1234567891011 1000000000000000000"], "sample_outputs": ["5\n6\n2\n948683296\n2996666667"], "notes": "NoteIn the first test case, $$$5$$$ luxury numbers in range $$$[8, 19]$$$ are: $$$8, 9, 12, 15, 16$$$."}, "positive_code": [{"source_code": "# This problem can be solved by finding a pattern\n# the first 24 numbers have the following corresponding sqrt floored value:\n# {*1=>1, *2=>1, *3=>1, *4=>2, 5=>2, *6=>2, 7=>2, *8=>2, *9=>3, 10=>3, 11=>3, *12=>3, 13=>3, 14=>3, *15=>3, *16=>4, 17=>4, 18=>4, 19=>4, *20=>4, 21=>4, 22=>4, 23=>4, *24=>4}\n# also I've marked each luxurious number with a star (*)\n# you can observe that each sqrt has exactly 3 luxurious numbers associated with it\n# also the each set of 3 luxurious numbers is evenly spaced i.e. its separated by a space of sqrt - 1\n# thus using the above two points we can find the amount of luxurious numbers leading upto any given number\n\ndef lux_nums_upto(limit)\n return 0 if limit.zero?\n\n floor_sqrt = Integer.sqrt(limit)\n result = ((floor_sqrt - 1) * 3) # lux nums for sqrt before limit\n result + 1 + ((limit - (floor_sqrt**2)) / floor_sqrt) # lux nums for sqrt at limit\nend\n\ndef lux_nums_in_range(left_limit, right_limit)\n lux_nums_upto(right_limit) - lux_nums_upto(left_limit - 1)\nend\n\n# code for tests:\n\nnum_of_cases = gets.chomp.to_i\n\nnum_of_cases.times do\n left_limit, right_limit = gets.chomp.split.map(&:to_i)\n\n puts lux_nums_in_range(left_limit, right_limit)\nend\n"}], "negative_code": [], "src_uid": "10812427d3052ce91cd0951911d3acb9"} {"nl": {"description": "Mishka is a little polar bear. As known, little bears loves spending their free time playing dice for chocolates. Once in a wonderful sunny morning, walking around blocks of ice, Mishka met her friend Chris, and they started playing the game.Rules of the game are very simple: at first number of rounds n is defined. In every round each of the players throws a cubical dice with distinct numbers from 1 to 6 written on its faces. Player, whose value after throwing the dice is greater, wins the round. In case if player dice values are equal, no one of them is a winner.In average, player, who won most of the rounds, is the winner of the game. In case if two players won the same number of rounds, the result of the game is draw.Mishka is still very little and can't count wins and losses, so she asked you to watch their game and determine its result. Please help her!", "input_spec": "The first line of the input contains single integer n n (1 ≤ n ≤ 100) — the number of game rounds. The next n lines contains rounds description. i-th of them contains pair of integers mi and ci (1 ≤ mi,  ci ≤ 6) — values on dice upper face after Mishka's and Chris' throws in i-th round respectively.", "output_spec": "If Mishka is the winner of the game, print \"Mishka\" (without quotes) in the only line. If Chris is the winner of the game, print \"Chris\" (without quotes) in the only line. If the result of the game is draw, print \"Friendship is magic!^^\" (without quotes) in the only line.", "sample_inputs": ["3\n3 5\n2 1\n4 2", "2\n6 1\n1 6", "3\n1 5\n3 3\n2 2"], "sample_outputs": ["Mishka", "Friendship is magic!^^", "Chris"], "notes": "NoteIn the first sample case Mishka loses the first round, but wins second and third rounds and thus she is the winner of the game.In the second sample case Mishka wins the first round, Chris wins the second round, and the game ends with draw with score 1:1.In the third sample case Chris wins the first round, but there is no winner of the next two rounds. The winner of the game is Chris."}, "positive_code": [{"source_code": "#!/usr/bin/ruby\na=b=0\ngets;$<.each{|_|\n\tx,y=_.split.map &:to_i\n\ta+=1 if x>y\n\tb+=1 if xa]"}, {"source_code": "n = gets.to_i\n\nm = 0\nc = 0\n\nn.times do\n a,b = gets.split.map(&:to_i)\n m+=1 if a > b\n c+=1 if b > a\nend\n\nif m > c\n puts \"Mishka\"\nelsif c > m\n puts \"Chris\"\nelse\n puts \"Friendship is magic!^^\"\nend\n"}, {"source_code": "n = gets.chomp.to_i\na, b = 0, 0\n\nn.times do\n c, d = gets.chomp.split.map(&:to_i)\n next if c == d\n c > d ? a += 1 : b += 1\nend\n\ncase\nwhen a == b\n puts \"Friendship is magic!^^\"\nwhen a > b\n puts \"Mishka\"\nelse\n puts \"Chris\"\nend\n"}, {"source_code": "gets;r=0;$<.map{|e|r+=eval(e.split*\"<=>\")};puts r>0?'Mishka':r<0?'Chris':'Friendship is magic!^^'"}, {"source_code": "n = gets.to_i\nc = 0\nm = 0\n(0...n).each do\n a, b = gets.split.map &:to_i\n if (a < b) \n c += 1\n else \n if (a > b)\n m += 1\n end\n end\nend\n\nif (c == m)\n puts \"Friendship is magic!^^\"\nelse \n if (m > c)\n puts \"Mishka\"\n else \n puts \"Chris\"\n end\nend\n"}, {"source_code": "n = gets.to_i()\nmishka = 0\nchris = 0\nfor i in (1..n)\n mc = gets.split(' ')\n if mc[0].to_i() > mc[1].to_i()\n mishka += 1\n elsif mc[0].to_i() < mc[1].to_i()\n chris += 1\n end\nend\nif mishka > chris\n puts \"Mishka\"\nelsif mishka == chris\n puts \"Friendship is magic!^^\"\nelse\n puts \"Chris\"\nend\n"}, {"source_code": "# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n############################################################\ndef gs() gets.chomp end\ndef gi() gets.to_i end\ndef gss() gs.split end\ndef gis() gss.map(&:to_i) end\ndef nmapf(n,f) n.times.map{ __send__ f } end\ndef ngs(n) nmapf n,:gs end\ndef ngi(n) nmapf n,:gi end\ndef ngss(n) nmapf n,:gss end\ndef ngis(n) nmapf n,:gis end\ndef arr2d(h,w,v=0) h.times.map{[v] * w} end\ndef for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end\ndef nsum(n) n * (n + 1) / 2 end\ndef vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end\n############################################################\n\nN = gi\nMC = ngis N\n\nv = MC.map{|m, c| m <=> c }.inject(:+)\n\ncase\nwhen v < 0\n puts 'Chris'\nwhen v > 0\n puts 'Mishka'\nelse\n puts 'Friendship is magic!^^'\nend\n"}, {"source_code": "n = gets.to_i\nar = [0,0]\nn.times do\n s = gets.split.map(&:to_i)\n ar[0] += 1 if s[0] > s[1]\n ar[1] += 1 if s[1] > s[0]\nend\nputs \"Mishka\" if ar[0] > ar[1]\nputs \"Chris\" if ar[1] > ar[0]\nputs \"Friendship is magic!^^\" if ar[0] == ar[1]"}, {"source_code": "mc = 0\ncc = 0\ngets.to_i.times do\n m, c = gets.split.map(&:to_i)\n if m > c\n mc += 1\n elsif m < c\n cc += 1\n end\nend\n\nif mc > cc\n puts 'Mishka'\nelsif mc < cc\n puts 'Chris'\nelse\n puts 'Friendship is magic!^^'\nend\n"}, {"source_code": "n = gets.strip.to_i\nm_count = c_count = 0\nn.times do\n m, c = gets.strip.split.map(&:to_i)\n m_count += 1 if m > c\n c_count += 1 if c > m\nend\nans = \"Friendship is magic!^^\"\nif m_count > c_count\n ans = \"Mishka\"\nelsif m_count < c_count\n ans = \"Chris\"\nend\nputs ans\n"}, {"source_code": "n=gets.to_i\nans=0\nn.times{\n\ta,b=gets.split.map{|e| e.to_i}\n\tans=ans+(a<=>b)\n}\nputs [\"Chris\",\"Friendship is magic!^^\",\"Mishka\"][(ans<=>0)+1]"}, {"source_code": "n = gets.to_i\nr = 0\nn.times do |i|\n\tm,c = gets.split.map(&:to_i)\n\tif m > c \n\t\tr += 1\n\telsif c > m\n\t\tr -= 1\n\tend\nend\nif r > 0\n\tputs 'Mishka'\nelsif r < 0\n\tputs 'Chris'\nelse\n\tputs 'Friendship is magic!^^'\nend\n\t"}, {"source_code": "n = gets.to_i\nr = 0\n\n(1..n).each do\n a, b = gets.split.map(&:to_i)\n r += a <=> b\nend\n\nputs case\n when r > 0\n 'Mishka'\n when r < 0\n 'Chris'\n else\n 'Friendship is magic!^^'\n end"}, {"source_code": "gets;r=0;$<.map{|e|r+=eval(e.split*\"<=>\")};puts r>0?'Mishka':r<0?'Chris':'Friendship is magic!^^'"}, {"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\n\nrounds = gets.to_i\nmishka_wins = 0\nchris_wins = 0\ndraws = 0\nrounds.times {\n mishka, chris = gets.split.map(&:to_i)\n\n if mishka > chris\n mishka_wins += 1\n elsif mishka < chris\n chris_wins += 1\n else\n draws += 0\n end\n}\n\nif mishka_wins > chris_wins\n puts \"Mishka\"\nelsif mishka_wins < chris_wins\n puts \"Chris\"\nelse\n puts \"Friendship is magic!^^\"\nend"}, {"source_code": "games = gets.chomp.to_i\nresults = []\nfor i in (0..games-1)\n\tresults[i] = gets.chomp\nend\n\ncount = 0\nfor i in (0..games-1)\n\tsplit = results[i].split\n\tif split[0].to_i > split[1].to_i\n\t\tcount += 1\n\telsif split[0].to_i < split[1].to_i\n\t\tcount -= 1\n\telse\n\t\tcount += 0\n\tend\nend\n\nif count > 0\n\tprint \"Mishka\"\nelsif count == 0\n\tprint \"Friendship is magic!^^\"\nelse\n\tprint \"Chris\"\nend\n"}, {"source_code": "a=[0,0,0]\ng=[\"Friendship is magic!^^\",\"Mishka\",\"Chris\"]\ngets.to_i.times {|x| s=gets.split.map(&:to_i); a[s[0]<=>s[1]]+=1; }\nputs g[a[1]<=>a[-1]]"}, {"source_code": "n = gets.to_i\nwins = Hash.new(0)\nn.times.map do\n a, b = gets.split.map(&:to_i)\n if a - b > 0\n wins['Mishka'] += 1\n elsif a - b < 0\n wins['Chris'] += 1\n end\nend\nputs wins['Mishka'] == wins['Chris'] ? 'Friendship is magic!^^' : wins.max_by{|k, v| v}[0]\n\n"}, {"source_code": "n = gets.to_i\nm = c = 0\nfor i in 1..n\n mi, ch = gets.split.map(&:to_i)\n if mi > ch\n m += 1\n elsif ch > mi\n c += 1\n end\nend\n\nif m > c\n puts 'Mishka'\nelsif m < c\n puts 'Chris'\nelse\n puts 'Friendship is magic!^^'\nend\n"}, {"source_code": "a=b=0\ngets.to_i.times do\n\tx = gets.split.map(&:to_i)\n\ta+=1 if x[0]>x[1]\n\tb+=1 if x[0]b]"}, {"source_code": "n = gets.to_i\nm=0\nc=0\nn.times do\n\tx,y = gets.split.map(&:to_i)\n\tif x>y\n\t\tm+=1\n\telsif xc\n\tputs \"Mishka\"\nelsif c>m\n\tputs \"Chris\"\nelse\n\tputs \"Friendship is magic!^^\"\nend"}, {"source_code": "n = Integer(gets)\na, b = 0, 0\nn.times do\n aa, bb = gets.split.map(&:to_i)\n if aa > bb\n a += 1\n end\n if aa < bb\n b += 1\n end\nend\n\nif a > b\n puts \"Mishka\"\nend\n\nif a < b\n puts \"Chris\"\nend\n\nif a == b\n puts \"Friendship is magic!^^\"\nend"}, {"source_code": "jj = 0\ngets.to_i.times do\n aa, bb = gets.split.map(&:to_i)\n jj += aa <=> bb\nend\n\nif jj > 0 \n puts \"Mishka\"\nelsif jj < 0\n puts \"Chris\"\nelse \n puts \"Friendship is magic!^^\"\nend"}, {"source_code": "n = gets.to_i\n\nresult = (1..n).map do\n a, b = gets.split(' ').map(&:to_i)\n (a == b) ? 0 : (a - b) / (a - b).abs\nend.inject(&:+)\n\nif result > 0\n puts 'Mishka'\nelsif result < 0\n puts 'Chris'\nelse\n puts 'Friendship is magic!^^'\nend\n"}, {"source_code": "n = gets.to_i\ns = 0\nfor i in 1..n do\n a, b = gets.split.map(&:to_i)\n if a > b then\n s += 1\n elsif a < b then\n s -= 1\n end\nend\nif s > 0 then\n puts 'Mishka'\nelsif s < 0 then\n puts 'Chris'\nelse\n puts 'Friendship is magic!^^'\nend"}, {"source_code": "gameNum = gets.chomp.to_i\n\nmWin = 0\ncWin = 0\n\ngameNum.times do\n\tm, c = gets.chomp.split(' ').map(&:to_i)\n\n\tif m > c\n\t\tmWin += 1\n\telsif c > m\n\t\tcWin += 1\n\tend\nend\n\nif mWin > cWin\n\tputs 'Mishka'\nelsif cWin > mWin\n\tputs 'Chris'\nelse\n\tputs 'Friendship is magic!^^'\nend"}], "negative_code": [{"source_code": "n = gets.to_i\nar = [0,0]\nn.times do\n s = gets.split.map(&:to_i)\n ar[0] += s[0]\n ar[1] += s[1]\nend\nputs \"Mishka\" if ar[0] > ar[1]\nputs \"Chris\" if ar[1] > ar[0]\nputs \"Friendship is magic!^^\" if ar[0] == ar[1]"}, {"source_code": "n = gets.to_i\nr = 0\nn.times do |i|\n\tm,c = gets.split.map(&:to_i)\n\tif m > c \n\t\tr += 1\n\telsif c > m\n\t\tr -= 1\n\tend\nend\nif r > 0\n\tp 'Mishka'\nelsif r < 0\n\tp 'Chris'\nelse\n\tp 'Friendship is magic!^^'\nend\n\t"}, {"source_code": "gets;r=0;$<.map{|e|r+=eval(e.split*\"<=>\")};puts r>0?'Mishka':r<0?'Chris':'Friendship is magic^^'"}, {"source_code": "games = gets.chomp.to_i\nresults = []\nfor i in (0..games-1)\n\tresults[i] = gets.chomp\nend\n\ncount = 0\nfor i in (0..games-1)\n\tsplit = results[i].split\n\tfor x in (0..1)\n\t\tif split[0].to_i > split[1].to_i\n\t\t\tcount += 1\n\t\telse\n\t\t\tcount -= 1\n\t\tend\n\tend\nend\n\nif count > 0\n\tprint \"Mishka\"\nelsif count == 0\n\tprint \"Friendship is magic!^^\"\nelse\n\tprint \"Chris\"\nend\n"}, {"source_code": "games = gets.chomp.to_i\nresults = []\nfor i in (0..games-1)\n\tresults[i] = gets.chomp\nend\n\nmishka = 0\nchris = 0\nfor i in (0..games-1)\n\tsplit = results[i].split\n\tmishka += split[0].to_i\n\tchris += split[1].to_i\nend\n\nif mishka > chris\n\tprint \"Mishka\"\nelsif mishka == chris\n\tprint \"Friendship is magic!^^\"\nelse\n\tprint \"Chris\"\nend\n"}, {"source_code": "a=[0,0,0]\ng=[\"Friendship is magic!^^\",\"Mishka\",\"Chris\"]\ngets.to_i.times {|x| s=gets.split.map(&:to_i); a[s[1]<=>s[0]]+=1; }\nputs g[a[1]<=>a[-1]]\n"}, {"source_code": "n = gets.to_i\nwins = Hash.new(0)\nn.times.map do\n a, b = gets.split.map(&:to_i)\n if a - b > 0\n wins['Mishka'] += 1\n elsif a - b < 0\n wins['Chris'] += 1\n end\nend\nputs wins['Mishka'] == wins['Chris'] ? 'Friendship is magic!^^' : wins.max[0]\n\n"}, {"source_code": "n = gets.to_i\nwins = Hash.new(0)\nn.times.map do\n a, b = gets.split.map(&:to_i)\n if a - b > 0\n wins['Misha'] += 1\n elsif a - b < 0\n wins['Chris'] += 1\n end\nend\nputs wins['Misha'] == wins['Chris'] ? 'Friendship is magic!^^' : wins.max[0]\n\n"}], "src_uid": "76ecde4a445bbafec3cda1fc421e6d42"} {"nl": {"description": "You are given some Tetris field consisting of $$$n$$$ columns. The initial height of the $$$i$$$-th column of the field is $$$a_i$$$ blocks. On top of these columns you can place only figures of size $$$2 \\times 1$$$ (i.e. the height of this figure is $$$2$$$ blocks and the width of this figure is $$$1$$$ block). Note that you cannot rotate these figures.Your task is to say if you can clear the whole field by placing such figures.More formally, the problem can be described like this:The following process occurs while at least one $$$a_i$$$ is greater than $$$0$$$: You place one figure $$$2 \\times 1$$$ (choose some $$$i$$$ from $$$1$$$ to $$$n$$$ and replace $$$a_i$$$ with $$$a_i + 2$$$); then, while all $$$a_i$$$ are greater than zero, replace each $$$a_i$$$ with $$$a_i - 1$$$. And your task is to determine if it is possible to clear the whole field (i.e. finish the described process), choosing the places for new figures properly.You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. The next $$$2t$$$ lines describe test cases. The first line of the test case contains one integer $$$n$$$ ($$$1 \\le n \\le 100$$$) — the number of columns in the Tetris field. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 100$$$), where $$$a_i$$$ is the initial height of the $$$i$$$-th column of the Tetris field.", "output_spec": "For each test case, print the answer — \"YES\" (without quotes) if you can clear the whole Tetris field and \"NO\" otherwise.", "sample_inputs": ["4\n3\n1 1 3\n4\n1 1 2 1\n2\n11 11\n1\n100"], "sample_outputs": ["YES\nNO\nYES\nYES"], "notes": "NoteThe first test case of the example field is shown below:Gray lines are bounds of the Tetris field. Note that the field has no upper bound.One of the correct answers is to first place the figure in the first column. Then after the second step of the process, the field becomes $$$[2, 0, 2]$$$. Then place the figure in the second column and after the second step of the process, the field becomes $$$[0, 0, 0]$$$.And the second test case of the example field is shown below:It can be shown that you cannot do anything to end the process.In the third test case of the example, you first place the figure in the second column after the second step of the process, the field becomes $$$[0, 2]$$$. Then place the figure in the first column and after the second step of the process, the field becomes $$$[0, 0]$$$.In the fourth test case of the example, place the figure in the first column, then the field becomes $$$[102]$$$ after the first step of the process, and then the field becomes $$$[0]$$$ after the second step of the process."}, "positive_code": [{"source_code": "tc = gets.to_i \n\ntc.times do \n n = gets.to_i \n arr = gets.split().map{|x| x.to_i} \n ev = od = 0 \n arr.each do |x| \n if x % 2 == 0 \n ev += 1\n else\n od += 1 \n end\n end\n if(ev == 0 || od == 0)\n puts \"YES\" \n else\n puts \"NO\"\n end\nend"}, {"source_code": "t = gets.to_i\nt.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n ok = true\n for i in 0...(n-1) do\n if (a[i+1] - a[i]) % 2 != 0\n ok = false\n end\n end\n puts ok ? \"YES\" : \"NO\"\nend\n"}, {"source_code": "1.upto(gets.to_i) do\n gets\n a = gets.split(' ').map(&:to_i)\n\n puts a.all? { |e| e.even? == a.first.even? } ? 'YES' : 'NO'\nend\n"}, {"source_code": "t = gets.chomp.to_i\n(0...t).each do |_|\n n = gets.chomp.to_i\n l = gets.chomp.split.map(&:to_i)\n has_odd, has_even = false, false\n l.each{|c| c.odd? ? (has_odd = true) : (has_even = true); (break) if has_odd && has_even }\n puts (has_odd && has_even ? \"NO\" : \"YES\")\nend\n"}, {"source_code": "t = gets.chomp.to_i\n(0...t).each do |_|\n n = gets.chomp.to_i\n l = gets.chomp.split.map(&:to_i)\n num_of_odd, num_of_even = l.partition{|c| c.odd? }\n puts (!num_of_odd.size.zero? && !num_of_even.size.zero? ? \"NO\" : \"YES\")\nend\n"}], "negative_code": [], "src_uid": "53a3313f5d6ce19413d72473717054fc"} {"nl": {"description": "Vus the Cossack has $$$n$$$ real numbers $$$a_i$$$. It is known that the sum of all numbers is equal to $$$0$$$. He wants to choose a sequence $$$b$$$ the size of which is $$$n$$$ such that the sum of all numbers is $$$0$$$ and each $$$b_i$$$ is either $$$\\lfloor a_i \\rfloor$$$ or $$$\\lceil a_i \\rceil$$$. In other words, $$$b_i$$$ equals $$$a_i$$$ rounded up or down. It is not necessary to round to the nearest integer.For example, if $$$a = [4.58413, 1.22491, -2.10517, -3.70387]$$$, then $$$b$$$ can be equal, for example, to $$$[4, 2, -2, -4]$$$. Note that if $$$a_i$$$ is an integer, then there is no difference between $$$\\lfloor a_i \\rfloor$$$ and $$$\\lceil a_i \\rceil$$$, $$$b_i$$$ will always be equal to $$$a_i$$$.Help Vus the Cossack find such sequence!", "input_spec": "The first line contains one integer $$$n$$$ ($$$1 \\leq n \\leq 10^5$$$) — the number of numbers. Each of the next $$$n$$$ lines contains one real number $$$a_i$$$ ($$$|a_i| < 10^5$$$). It is guaranteed that each $$$a_i$$$ has exactly $$$5$$$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $$$0$$$.", "output_spec": "In each of the next $$$n$$$ lines, print one integer $$$b_i$$$. For each $$$i$$$, $$$|a_i-b_i|<1$$$ must be met. If there are multiple answers, print any.", "sample_inputs": ["4\n4.58413\n1.22491\n-2.10517\n-3.70387", "5\n-6.32509\n3.30066\n-0.93878\n2.00000\n1.96321"], "sample_outputs": ["4\n2\n-2\n-4", "-6\n3\n-1\n2\n2"], "notes": "NoteThe first example is explained in the legend.In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down."}, "positive_code": [{"source_code": "N = gets.to_i\nsd = 0\nincremantables = {}\ncs = []\n(0...N).map{ |i|\n a = gets.to_f\n c = a.floor\n cs << c\n d = a - c\n sd += d\n incremantables[i] = 1 if d > 0\n}\nsd = (sd + 0.5).floor\n\nputs (0...N).map{ |i| \n if sd > 0 && incremantables[i]\n sd -= 1\n cs[i] + 1\n else\n cs[i]\n end\n}\n"}, {"source_code": "n = gets.to_i\na = n.times.map{gets.chomp}\nb = Array.new(n,0)\nsum = 0\na.each_with_index do |s,i|\n b[i] = s.to_i\n if s[0] == \"-\" && s[-5..-1] != \"00000\"\n b[i] -= 1\n end\n sum += b[i]\nend\na.each_with_index do |s,i|\n break if sum == 0\n next if s[-5..-1] == \"00000\"\n sum += 1\n b[i] += 1\nend\nputs b"}, {"source_code": "n=gets.to_i\na=Array.new(n)\nk=0\nfor i in 0...n do\n q=gets\n a[i]=q.to_f\n k+=q.to_i\nend\n for i in 0...n do\n if k>0 then\n if a[i].to_i != a[i].floor or a[i].to_i != a[i].ceil then\n if a[i]>0 then\n puts a[i].floor\n else\n puts a[i].floor\n k-=1\n end\n else\n puts a[i].to_i\n end\n elsif k<0 then\n if a[i].to_i != a[i].floor or a[i].to_i != a[i].ceil then\n if a[i]>0 then\n puts a[i].ceil\n k+=1\n else\n puts a[i].ceil\n end\n else\n puts a[i].to_i\n end\n else\n puts a[i].to_i\n end\n end"}], "negative_code": [{"source_code": "N = gets.to_i\nsd = 0\nincremantables = {}\ncs = []\n(0...N).map{ |i|\n a = gets.to_f\n c = a.floor\n cs << c\n d = a - c\n sd += d\n incremantables[i] = 1 if d > 0\n}\n\nputs (0...N).map{ |i| \n if sd > 0 && incremantables[i]\n sd -= 1\n cs[i] + 1\n else\n cs[i]\n end\n} * \" \"\n"}, {"source_code": "n = gets.to_i\na = n.times.map{gets.chomp}\nb = a.map(&:to_i)\nsum = b.inject(:+)\na.each_with_index do |s,i|\n break if sum == 0\n next if s[-5..-1] == \"00000\"\n sum += 1\n b[i] += 1\nend\nputs b"}, {"source_code": "n=gets.to_i\na=Array.new(n)\nk=0\nfor i in 0...n do\n q=gets\n a[i]=q.to_f\n k+=q.to_i\nend\n for i in 0...n do\n if k>0 then\n if a[i]>0 and a[i].to_i!=a[i].ceil.to_i then\n puts a[i].ceil\n else\n puts a[i].floor\n k-=1\n end\n elsif k<0 then\n if a[i]>0 and a[i].to_i!=a[i].ceil.to_i then\n puts a[i].ceil\n k+=1\n else\n puts a[i].floor\n end\n else\n puts a[i].to_i\n end\n end"}, {"source_code": "n=gets.to_i\na=Array.new(n)\nk=0\nfor i in 0...n do\n q=gets\n a[i]=q.to_f\n k+=q.to_i\nend\n for i in 0...n do\n if k>0 then\n if a[i]>0 then\n puts a[i].ceil\n else\n puts a[i].floor\n k-=1\n end\n elsif k<0 then\n if a[i]>0 then\n puts a[i].ceil\n else\n puts a[i].floor\n k+=1\n end\n else\n puts a[i].to_i\n end\n end"}, {"source_code": "n=gets.to_i\na=Array.new(n)\nk=0\nfor i in 0...n do\n q=gets\n a[i]=q.to_f\n k+=q.to_i\nend\n for i in 0...n do\n if k>0 then\n if a[i]>0 then\n puts a[i].floor\n k-=1\n else\n puts a[i].ceil\n end\n elsif k<0 then\n if a[i]>0 then\n puts a[i].ceil\n else\n puts a[i].floor\n end\n else\n puts a[i].to_i\n end\n end"}, {"source_code": "n=gets.to_i\na=Array.new(n)\nk=0\nfor i in 0...n do\n q=gets\n a[i]=q.to_f\n k+=q.to_i\nend\n for i in 0...n do\n if k>0 then\n if a[i].to_i != a[i].floor then\n if a[i]>0 then\n puts a[i].floor\n else\n puts a[i].floor\n k-=1\n end\n else\n puts a[i].to_i\n end\n elsif k<0 then\n if a[i].to_i != a[i].floor then\n if a[i]>0 then\n puts a[i].ceil\n k+=1\n else\n puts a[i].ceil\n end\n else\n puts a[i].to_i\n end\n else\n puts a[i].to_i\n end\n end"}], "src_uid": "6059cfa13594d47b3e145d7c26f1b0b3"} {"nl": {"description": "There are n kangaroos with pockets. Each kangaroo has a size (integer number). A kangaroo can go into another kangaroo's pocket if and only if the size of kangaroo who hold the kangaroo is at least twice as large as the size of kangaroo who is held.Each kangaroo can hold at most one kangaroo, and the kangaroo who is held by another kangaroo cannot hold any kangaroos.The kangaroo who is held by another kangaroo cannot be visible from outside. Please, find a plan of holding kangaroos with the minimal number of kangaroos who is visible.", "input_spec": "The first line contains a single integer — n (1 ≤ n ≤ 5·105). Each of the next n lines contains an integer si — the size of the i-th kangaroo (1 ≤ si ≤ 105).", "output_spec": "Output a single integer — the optimal number of visible kangaroos.", "sample_inputs": ["8\n2\n5\n7\n6\n9\n8\n4\n2", "8\n9\n1\n6\n2\n6\n5\n8\n3"], "sample_outputs": ["5", "5"], "notes": null}, "positive_code": [{"source_code": "n=gets.to_i\na=[]\nn.times{a<=a[j]\n j+=1\nend\np n-c\n\n\n"}, {"source_code": "n=gets.to_i\na=[]\nn.times{a<=a[j]\n j+=1\nend\np n-c\n\n\n"}, {"source_code": "n=gets.to_i\na=[]\nn.times{a<=a[j]\n j+=1\nend\np n-c\n\n\n"}, {"source_code": "n=gets.to_i\na=[]\nn.times{a<=a[j]\n j+=1\nend\np n-c\n\n\n"}, {"source_code": "n=gets.to_i\na=[]\nn.times{a<=a[j]\n j+=1\nend\np n-c\n\n\n"}, {"source_code": "n=gets.to_i\na=[]\nn.times{a<=a[j]\n j+=1\nend\np n-c\n\n\n"}, {"source_code": "n = gets.to_i\na = []\ndem = []\ndem1 = 0\n\n(0..n-1).each do |i|\n a[i] = gets.to_i\nend\n\na.sort!\n\ni = 0\n\nwhile (a[i]<= (a[n-1]/2)) && (i<=(n/2-1)) do\n if i == 0\n j = i+1\n else\n j = n - dem[i-1]\n end\n while ((2*a[i])>a[j]) && (j=0 do\n if dem[i] > dem1\n dem1 += 1\n end\n i -= 1\nend\n\nputs n-dem1"}, {"source_code": "n=gets.to_i\na=[]\nn.times{a<=a[j]\n j+=1\nend\np n-c\n\n\n"}, {"source_code": "n=gets.to_i\na=[]\nn.times{a<=a[j]\n j+=1\nend\np n-c\n\n\n"}, {"source_code": "n=gets.to_i\na=[]\nn.times{a<=a[j]\n j+=1\nend\np n-c\n\n"}], "negative_code": [{"source_code": "n = gets.to_i\na = []\ndem = []\ndem1 = 0\n\n(0..n-1).each do |i|\n a[i] = gets.to_i\nend\n\na.sort!\n\ni = 0\n\nwhile (a[i]<= (a[n-1]/2)) && (i<=(n/2-1)) do\n j = i+1\n while ((2*a[i])>a[j]) && (j=0 do\n if dem[i] > dem1\n dem1 += 1\n end\n i -= 1\nend\n\nputs n-dem1"}, {"source_code": "n=gets.to_i\na=[]\nn.times{a<=a[j]\n j+=1\nend\np n-c\n\n"}, {"source_code": "n=gets.to_i\na=[]\nn.times{a<=a[j]\n j+=1\nend\np n-c\n\n"}], "src_uid": "361f65484d86051fa9ff013f5e8c9154"} {"nl": {"description": "The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).", "output_spec": "For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.", "sample_inputs": ["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"], "sample_outputs": ["1\n2\n25\n26\n27\n649\n650"], "notes": null}, "positive_code": [{"source_code": "a=\" \"*27\r\nfor i in 0..25\r\n\ta[i]=('a'.ord+i).chr\r\nend\r\nr=[]\r\na.split('').each do |e|\r\n\ta.split('').each do |u|\r\n\t\tif e!=u && e!=\" \" && u!=\" \"\r\n\t\t\tr.append(e+u)\r\n\t\tend\r\n\tend\r\nend\r\nfor t in 1..gets[..-2].to_i\r\n\tputs r.index(gets[..-2])+1\r\nend\r\n"}, {"source_code": "m, az = {}, \"abcdefghijklmnopqrstuvwxyz\".chars\naz.product(az).filter {|a| a[0] != a[1]}.map{|a| a[0] + a[1]}.each_with_index {|a,i| m[a] = i+1}\ngets.to_i.times { puts m[gets.chomp] }"}, {"source_code": "words = {}\ncnt = 1\n('a'..'z').each do |a|\n ('a'..'z').each do |b|\n next if a == b\n words[a + b] = cnt\n cnt += 1\n end\nend\n\ngets.to_i.times do\n s = gets.strip\n p words[s]\nend\n"}, {"source_code": "map = [*\"a\"..\"z\"].flat_map do |a|\r\n ([*\"a\"..\"z\"] - [a]).map do |b|\r\n a + b\r\n end\r\nend.zip(1..).to_h\r\n\r\ngets.to_i.times do\r\n p map[gets.chomp]\r\nend\r\n"}, {"source_code": "t=gets.chomp.to_i\r\narr = ('a'..'z').to_a\r\narrAZ =[]\r\nkol=0\r\nfor i in 0..25\r\n for j in 0..25\r\n if (arr[i].to_s!=arr[j].to_s)\r\n kol+=1\r\n arrAZ[kol]=arr[i]+arr[j] \r\n end\r\n end\r\nend\r\n\r\n#puts arrAZ.compact\r\n#puts arrAZ.length\r\n\r\nt.times do\r\n puts arrAZ.index(gets.chomp.to_s)\r\nend"}, {"source_code": "t = gets.to_i\na = ?a.ord\nt.times do\n x, y = gets.chomp.each_byte.map { |b| b - a }\n i = x * 25 + y + 1\n i -= 1 if y > x\n puts i\nend"}, {"source_code": "h = {}\r\nq = 1\r\nfor i in 'a'..'z'\r\n for j in 'a'..'z'\r\n next if i == j\r\n h[i + j] = q\r\n q += 1\r\n end\r\nend\r\n\r\ngets.to_i.times do\r\n puts h[gets.chomp]\r\nend\r\n"}], "negative_code": [], "src_uid": "2e3006d663a3c7ad3781aba1e37be3ca"} {"nl": {"description": "Vasya takes part in the orienteering competition. There are n checkpoints located along the line at coordinates x1, x2, ..., xn. Vasya starts at the point with coordinate a. His goal is to visit at least n - 1 checkpoint in order to finish the competition. Participant are allowed to visit checkpoints in arbitrary order.Vasya wants to pick such checkpoints and the order of visiting them that the total distance travelled is minimized. He asks you to calculate this minimum possible value.", "input_spec": "The first line of the input contains two integers n and a (1 ≤ n ≤ 100 000,  - 1 000 000 ≤ a ≤ 1 000 000) — the number of checkpoints and Vasya's starting position respectively. The second line contains n integers x1, x2, ..., xn ( - 1 000 000 ≤ xi ≤ 1 000 000) — coordinates of the checkpoints.", "output_spec": "Print one integer — the minimum distance Vasya has to travel in order to visit at least n - 1 checkpoint.", "sample_inputs": ["3 10\n1 7 12", "2 0\n11 -10", "5 0\n0 0 1000 0 0"], "sample_outputs": ["7", "10", "0"], "notes": "NoteIn the first sample Vasya has to visit at least two checkpoints. The optimal way to achieve this is the walk to the third checkpoints (distance is 12 - 10 = 2) and then proceed to the second one (distance is 12 - 7 = 5). The total distance is equal to 2 + 5 = 7.In the second sample it's enough to visit only one checkpoint so Vasya should just walk to the point  - 10."}, "positive_code": [{"source_code": "#!/usr/bin/ruby\nn,s,*a=$<.read.split.map &:to_i\na.sort!\nif n<2\n\tp 0\nelse\n\tp [[0,-2],[1,-1]].map{|i,j|\n\t\tx,y=[[s-a[i],0].max,[a[j]-s,0].max].minmax\n\t\tx*2+y\n\t}.min\nend"}, {"source_code": "n,s,*a=$<.read.split.map &:to_i\na.sort!\np n<2 ? 0 : [[0,-2],[1,-1]].map{|i,j|\n\tx,y=[[s-a[i],0].max,[a[j]-s,0].max].minmax\n\tx*2+y\n}.min"}, {"source_code": "#!/usr/bin/ruby\nn,s,*a=$<.read.split.map &:to_i\na.sort!\nif n<2\n\tp 0\nelsif s a\n right_dists << cp - a\n end\nend\ncheckps.delete(a)\n\nif checkps.length > 0\n if left_dists.length > 0 and right_dists.length > 0\n mindist1 = 0\n mindist2 = 0\n \n old_left = left_dists.dup\n left_dists.slice! left_dists.index(left_dists.max)\n ldm = 0\n rdm = 0\n if not left_dists.max.nil?\n ldm = left_dists.max\n end\n if not right_dists.max.nil?\n rdm = right_dists.max\n end\n if (ldm > rdm)\n mindist1 = 2 * rdm + ldm\n else\n mindist1 = rdm + 2 * ldm\n end\n \n left_dists = old_left\n right_dists.slice! right_dists.index(right_dists.max)\n ldm = 0\n rdm = 0\n if not left_dists.max.nil?\n ldm = left_dists.max\n end\n if not right_dists.max.nil?\n rdm = right_dists.max\n end\n if (ldm > rdm)\n mindist2 = 2 * rdm + ldm\n else\n mindist2 = rdm + 2 * ldm\n end\n \n puts [mindist1, mindist2].min\n \n elsif left_dists.length > 0\n left_dists.slice! left_dists.index(left_dists.max)\n ldm = 0\n if not left_dists.max.nil?\n ldm = left_dists.max\n end\n puts ldm\n else\n right_dists.slice! right_dists.index(right_dists.max)\n rdm = 0\n if not right_dists.max.nil?\n rdm = right_dists.max\n end\n puts rdm\n end\nelse\n puts 0\nend"}, {"source_code": "n,a,*x=$<.read.split.map &:to_i\n(puts 0; exit) if n<2\n\nx.sort!\nif a<=x[0]\n puts (x[-2]-a).abs\nelsif a>=x[-1]\n puts (a-x[1]).abs\nelse\n b=x[-1]-a+x[-1]-x[1]\n c=(a-x[1]).abs+x[-1]-x[1]\n d=(x[-2]-a).abs+x[-2]-x[0]\n e=a-x[0]+x[-2]-x[0]\n puts [b, c, d, e].min\nend\n"}], "negative_code": [{"source_code": "input = gets.strip.split(' ').map(&:to_i)\nn = input[0]\na = input[1]\ncheckps = gets.strip.split(' ').map(&:to_i).sort\ndists = []\ncheckps.each do |cp|\n dists << (cp-a).abs\nend\ntbr = dists.index(dists.max)\ncheckps.slice! tbr\ndists.slice! tbr\nleft_exists = false\nright_exists = false\ncheckps.each do |cp|\n if cp < a\n left_exists = true\n elsif cp > a\n right_exists = true\n end\nend\nif left_exists and right_exists\n tbds = dists.index(dists.max)\n mindist = 0\n if checkps[tbds] - a > 0\n mindist = dists[0] * 2 + dists[dists.length-1]\n else\n mindist = dists[dists.length-1] * 2 + dists[0]\n end\n puts mindist\nelse\n if left_exists\n puts dists[0]\n elsif right_exists\n puts dists[dists.length-1]\n else\n puts 0\n end\nend"}, {"source_code": "input = gets.strip.split(' ').map(&:to_i)\nn = input[0]\na = input[1]\ncheckps = gets.strip.split(' ').map(&:to_i).sort\ndists = []\nleft_exists = false\nright_exists = false\ncheckps.each do |cp|\n dists << (cp-a).abs\n if cp < a\n left_exists = true\n elsif cp > a\n right_exists = true\n end\nend\ntbr = dists.index(dists.max)\ncheckps.slice! tbr\ndists.slice! tbr\nif left_exists and right_exists\n tbds = dists.index(dists.max)\n mindist = 0\n if checkps[tbds] - a > 0\n mindist = dists[0] * 2 + dists[dists.length-1]\n else\n mindist = dists[dists.length-1] * 2 + dists[0]\n end\n puts mindist\nelse\n if left_exists\n puts dists[0]\n elsif right_exists\n puts dists[dists.length-1]\n else\n puts 0\n end\nend"}, {"source_code": "input = gets.strip.split(' ').map(&:to_i)\nn = input[0]\na = input[1]\ncheckps = gets.strip.split(' ').map(&:to_i).sort\ndists = []\ncheckps.each do |cp|\n dists << (cp-a).abs\nend\ntbr = dists.index(dists.max)\ncheckps.slice! tbr\ndists.slice! tbr\ntbds = dists.index(dists.max)\nmindist = 0\nif checkps[tbds] - a > 0\n mindist = dists[0] * 2 + dists[dists.length-1]\nelse\n mindist = dists[dists.length-1] * 2 + dists[0]\nend\nputs mindist"}], "src_uid": "7807c484035e0327870b6cac23c8d60a"} {"nl": {"description": "You are given an array $$$a$$$ of $$$2n$$$ distinct integers. You want to arrange the elements of the array in a circle such that no element is equal to the the arithmetic mean of its $$$2$$$ neighbours.More formally, find an array $$$b$$$, such that: $$$b$$$ is a permutation of $$$a$$$.For every $$$i$$$ from $$$1$$$ to $$$2n$$$, $$$b_i \\neq \\frac{b_{i-1}+b_{i+1}}{2}$$$, where $$$b_0 = b_{2n}$$$ and $$$b_{2n+1} = b_1$$$. It can be proved that under the constraints of this problem, such array $$$b$$$ always exists.", "input_spec": "The first line of input contains a single integer $$$t$$$ $$$(1 \\leq t \\leq 1000)$$$ — the number of testcases. The description of testcases follows. The first line of each testcase contains a single integer $$$n$$$ $$$(1 \\leq n \\leq 25)$$$. The second line of each testcase contains $$$2n$$$ integers $$$a_1, a_2, \\ldots, a_{2n}$$$ $$$(1 \\leq a_i \\leq 10^9)$$$ — elements of the array. Note that there is no limit to the sum of $$$n$$$ over all testcases.", "output_spec": "For each testcase, you should output $$$2n$$$ integers, $$$b_1, b_2, \\ldots b_{2n}$$$, for which the conditions from the statement are satisfied.", "sample_inputs": ["3\n3\n1 2 3 4 5 6\n2\n123 456 789 10\n1\n6 9"], "sample_outputs": ["3 1 4 2 5 6\n123 10 456 789\n9 6"], "notes": "NoteIn the first testcase, array $$$[3, 1, 4, 2, 5, 6]$$$ works, as it's a permutation of $$$[1, 2, 3, 4, 5, 6]$$$, and $$$\\frac{3+4}{2}\\neq 1$$$, $$$\\frac{1+2}{2}\\neq 4$$$, $$$\\frac{4+5}{2}\\neq 2$$$, $$$\\frac{2+6}{2}\\neq 5$$$, $$$\\frac{5+3}{2}\\neq 6$$$, $$$\\frac{6+1}{2}\\neq 3$$$."}, "positive_code": [{"source_code": "results = []\nt = gets.chomp\n(1..t.to_i).each do |iteration|\n\tn = gets.chomp.to_i\n\ta = gets.chomp.split(' ').map{|e| e.to_i}.sort\n\tb = []\n\n\t(0..n-1).each do |idx|\n\t\tb[2*idx] = a[idx]\n\t\tb[2*idx+1] = a[n+idx]\n\tend\n\n\tresults.push(b)\nend\nresults.each do |res|\n\tputs res\nend"}, {"source_code": "data_lines = gets.chomp\n\ndef prodess_arr arr\n\t# p '-----'\n\t# p arr\n\t# p '-----'\n\tnew_arr = []\n\tarr = arr.sort\n\tmiddle = arr.length / 2\n\t(0..arr.length-1).each do |index|\n\t\tif index < middle\n\t\t\tnew_arr[index * 2] = arr[index]\n\t\telse\n\t\t\tnew_index = index - middle\n\t\t\tnew_arr[new_index*2+1] = arr[index]\n\t\tend\n\tend\n\tnew_arr\n\n\n\t# success = false\n\t# while success == false\n\t# \tflag = true\n\t# \t(0..arr.length-1).each do |index|\n\t# \t\t# p \"number = #{arr[index]} index = #{index}\"\n\t# \t\tneigh_l_index = index-1\n\t# \t\tneigh_l_index = arr.length-1 if neigh_l_index < 0\n\t# \t\tneigh_r_index = index+1\n\t# \t\tneigh_r_index = 0 if neigh_r_index > arr.length-1\n\t# \t\tsr_ar = (arr[neigh_l_index].to_f + arr[neigh_r_index].to_f) / 2.0\n\t# \t\t# p \"sr_ar - arr[index] = #{sr_ar - arr[index]}\"\n\t# \t\tif sr_ar - arr[index] == 0.0\n\t# \t\t\t# p arr\n\t# \t\t\tflag = false\n\t# \t\t\tneigh_rr_index = index+2\n\t# \t\t\tif neigh_rr_index > arr.length-1\n\t# \t\t\t\tif neigh_rr_index == arr.length-1\n\t# \t\t\t\t\tneigh_rr_index = 0\n\t# \t\t\t\telse\n\t# \t\t\t\t\tneigh_rr_index = 1\n\t# \t\t\t\tend\n\t# \t\t\tend\n\t# \t\t\tbuffer = arr[neigh_r_index]\n\t# \t\t\tarr[neigh_r_index] = arr[neigh_rr_index]\n\t# \t\t\tarr[neigh_rr_index] = buffer\n\t# \t\t\t# p arr\n\t# \t\tend\n\t# \tend\n\t# \t# p success\n\t# \tsuccess = true if flag\n\t# \t# p success\n\t# end\n\t# arr\nend\narrs = []\n(1..data_lines.to_i).each do\n\tb = gets.chomp\n\tarrs.push prodess_arr(gets.chomp.split(' ').map{|_e| _e.to_i}).join(' ')\nend\narrs.each do |a|\n\tputs a\nend"}, {"source_code": "def solution(n, arr)\r\n\ti = 0\r\n\r\n\twhile i < 2 * n\r\n\t\tprev_index = prev_index(arr, 2 * n, i)\r\n\t\tnext_index = next_index(arr, 2 * n, i)\r\n\r\n\t\tif (arr[prev_index] + arr[next_index]) / 2.0 == 1.0 * arr[i]\r\n\t\t\tarr[i], arr[prev_index] = arr[prev_index], arr[i]\r\n\t\t\ti = 0\r\n\t\telse\r\n\t\t\ti += 1\r\n\t\tend\r\n\tend\r\n\r\n\tarr\r\nend\r\n\r\ndef prev_index(arr, n, i)\r\n\tif i == 0\r\n\t\treturn n - 1\r\n\telse\r\n\t\treturn i - 1\r\n\tend\r\nend\r\n\r\ndef next_index(arr, n, i)\r\n\tif i == n - 1\r\n\t\treturn 0\r\n\telse\r\n\t\treturn i + 1\r\n\tend\r\nend\r\n\r\ntest_cases = gets.to_i\r\n\r\ntest_cases.times do |test_case|\r\n n = gets.to_i\r\n arr = gets.chomp.split(' ').map(&:to_i)\r\n puts solution(n, arr).join(' ')\r\nend"}], "negative_code": [{"source_code": "def check_valid arr\n\tarr.each_with_index do |el, idx|\n\t\tmid = (arr[idx+1].to_f + arr[idx-1].to_f) / 2\n\t\tif mid == el.to_f\n\t\t\treturn false\n\t\tend\n\tend\n\ttrue\nend\n\nresults = []\nt = gets.chomp\n(1..t.to_i).each do |iteration|\n\tn = gets.chomp\n\ta = gets.chomp.split(' ').map{|e| e.to_i}\n\n\ta.permutation.each do |perm|\n\t\tif check_valid perm\n\t\t\tresults.push perm\n\t\t\tbreak\n\t\tend\n\tend\nend\nresults.each do |res|\n\tp res.join(\"\\s\")\nend"}, {"source_code": "def check_valid arr\n\tarr.each_with_index do |el, idx|\n\t\tmid = (arr[idx+1].to_f + arr[idx-1].to_f) / 2\n\t\tif mid == el.to_f\n\t\t\treturn false\n\t\tend\n\tend\n\ttrue\nend\n\nresults = []\nt = gets.chomp\n(1..t.to_i).each do |iteration|\n\tn = gets.chomp\n\ta = gets.chomp.split(' ').map{|e| e.to_i}\n\n\ta.permutation.each do |perm|\n\t\tif check_valid perm\n\t\t\tresults.push perm\n\t\t\tbreak\n\t\tend\n\tend\nend\np results\n"}, {"source_code": "data_lines = gets.chomp\n\ndef prodess_arr arr\n\t# p '-----'\n\t# p arr\n\t# p '-----'\n\tnew_arr = []\n\tarr = arr.sort\n\t(0..arr.length-1).each do |index|\n\t\tif index % 2 == 0\n\t\t\tnew_arr[index / 2] = arr[index]\n\t\telse\n\t\t\tmiddle = arr.length / 2\n\t\t\tnew_arr[(index / 2) + middle] = arr[index]\n\t\tend\n\tend\n\tnew_arr\n\n\n\t# success = false\n\t# while success == false\n\t# \tflag = true\n\t# \t(0..arr.length-1).each do |index|\n\t# \t\t# p \"number = #{arr[index]} index = #{index}\"\n\t# \t\tneigh_l_index = index-1\n\t# \t\tneigh_l_index = arr.length-1 if neigh_l_index < 0\n\t# \t\tneigh_r_index = index+1\n\t# \t\tneigh_r_index = 0 if neigh_r_index > arr.length-1\n\t# \t\tsr_ar = (arr[neigh_l_index].to_f + arr[neigh_r_index].to_f) / 2.0\n\t# \t\t# p \"sr_ar - arr[index] = #{sr_ar - arr[index]}\"\n\t# \t\tif sr_ar - arr[index] == 0.0\n\t# \t\t\t# p arr\n\t# \t\t\tflag = false\n\t# \t\t\tneigh_rr_index = index+2\n\t# \t\t\tif neigh_rr_index > arr.length-1\n\t# \t\t\t\tif neigh_rr_index == arr.length-1\n\t# \t\t\t\t\tneigh_rr_index = 0\n\t# \t\t\t\telse\n\t# \t\t\t\t\tneigh_rr_index = 1\n\t# \t\t\t\tend\n\t# \t\t\tend\n\t# \t\t\tbuffer = arr[neigh_r_index]\n\t# \t\t\tarr[neigh_r_index] = arr[neigh_rr_index]\n\t# \t\t\tarr[neigh_rr_index] = buffer\n\t# \t\t\t# p arr\n\t# \t\tend\n\t# \tend\n\t# \t# p success\n\t# \tsuccess = true if flag\n\t# \t# p success\n\t# end\n\t# arr\nend\narrs = []\n(1..data_lines.to_i).each do\n\tb = gets.chomp\n\tarrs.push prodess_arr(gets.chomp.split(' ').map{|_e| _e.to_i}).join(' ')\nend\narrs.each do |a|\n\tputs a\nend"}, {"source_code": "def solution(n, arr)\r\n\t(0..(2 * n - 1)).each do |i|\r\n\t\tprev_index = prev_index(arr, 2 * n, i)\r\n\t\tnext_index = next_index(arr, 2 * n, i)\r\n\r\n\t\t# puts \"i: #{i}\"\r\n\t\t# puts \"prev_index: #{prev_index}\"\r\n\t\t# puts \"next_index: #{next_index}\"\r\n\t\t# puts \"------------\"\r\n\t\tif (arr[prev_index] + arr[next_index]) / 2.0 == 1.0 * arr[i]\r\n\t\t\t# prev_index = prev_index(arr, n, i - 1)\r\n\t\t\tarr[i], arr[prev_index] = arr[prev_index], arr[i]\r\n\t\tend\r\n\tend\r\n\r\n\tarr\r\nend\r\n\r\ndef prev_index(arr, n, i)\r\n\tif i == 0\r\n\t\treturn n - 1\r\n\telse\r\n\t\treturn i - 1\r\n\tend\r\nend\r\n\r\ndef next_index(arr, n, i)\r\n\tif i == n - 1\r\n\t\treturn 0\r\n\telse\r\n\t\treturn i + 1\r\n\tend\r\nend\r\n\r\ntest_cases = gets.to_i\r\n\r\ntest_cases.times do |test_case|\r\n n = gets.to_i\r\n arr = gets.chomp.split(' ').map(&:to_i)\r\n puts solution(n, arr).join(' ')\r\nend"}], "src_uid": "4296da660a39a6e98b41387929701c0a"} {"nl": {"description": "You have three piles of candies: red, green and blue candies: the first pile contains only red candies and there are $$$r$$$ candies in it, the second pile contains only green candies and there are $$$g$$$ candies in it, the third pile contains only blue candies and there are $$$b$$$ candies in it. Each day Tanya eats exactly two candies of different colors. She is free to choose the colors of eaten candies: the only restriction that she can't eat two candies of the same color in a day.Find the maximal number of days Tanya can eat candies? Each day she needs to eat exactly two candies.", "input_spec": "The first line contains integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case is given as a separate line of the input. It contains three integers $$$r$$$, $$$g$$$ and $$$b$$$ ($$$1 \\le r, g, b \\le 10^8$$$) — the number of red, green and blue candies, respectively.", "output_spec": "Print $$$t$$$ integers: the $$$i$$$-th printed integer is the answer on the $$$i$$$-th test case in the input.", "sample_inputs": ["6\n1 1 1\n1 2 1\n4 1 1\n7 4 10\n8 1 4\n8 2 8"], "sample_outputs": ["1\n2\n2\n10\n5\n9"], "notes": "NoteIn the first example, Tanya can eat candies for one day only. She can eat any pair of candies this day because all of them have different colors.In the second example, Tanya can eat candies for two days. For example, she can eat red and green candies on the first day, and green and blue candies on the second day.In the third example, Tanya can eat candies for two days. For example, she can eat red and green candies on the first day, and red and blue candies on the second day. Note, that two red candies will remain uneaten."}, "positive_code": [{"source_code": "\nclass SweetProblem\n\n def initialize(debug=false)\n @debug = debug\n end\n \n def get_num_days_can_eat(input_arr)\n sum = 0\n input_arr.each do |val|\n sum = sum + val\n end\n\n ans = sum / 2\n\n sum_red_green = input_arr[0] + input_arr[1]\n sum_green_blue = input_arr[1] + input_arr[2]\n sum_red_blue = input_arr[0] + input_arr[2]\n\n if sum_red_green < ans\n ans = sum_red_green\n end\n if sum_green_blue < ans\n ans = sum_green_blue\n end\n if sum_red_blue < ans\n ans = sum_red_blue\n end\n\n ans\n end\n \n def main\n i = 0\n num_lines = 0\n while input = gets\n if i == 0\n num_lines = input.to_i \n i = i + 1\n next\n else\n input_arr = input.split(' ').map!(&:to_i)\n \n num_days_can_eat = get_num_days_can_eat(input_arr)\n \n puts num_days_can_eat\n i = i + 1\n break if i > num_lines\n end\n end\n end\n \nend\n \nsweet_problem = SweetProblem.new\nsweet_problem.main\n\n"}, {"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\nrequire 'prime'\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\ndef big(a,b) return (a>b)? a:b end\n\ndef small(a,b) return (a max and key != color_key\n max = val\n max_color_key = key\n end\n end\n return max_color_key, two_colors_same_max\n end\n \n def num_colors_left(hash)\n num_colors = 0\n hash.each do |key,val|\n if val > 0\n num_colors = num_colors + 1\n end\n end\n num_colors\n end\n \n def get_num_days_can_eat(input_arr)\n hash = to_hash(input_arr)\n puts hash if @debug\n \n min_color = min_color(hash)\n puts \"min_color: #{min_color}\" if @debug\n max_color, two_colors_same_max = max_color(hash, min_color)\n puts \"max_color: #{max_color}\" if @debug\n puts \"two_colors_same_max: #{two_colors_same_max}\" if @debug\n\n num_days_can_eat = 0\n while num_colors_left(hash) >= 2\n min_color_count = hash[min_color]\n puts \"min_color_count: #{min_color_count}\" if @debug \n\n if two_colors_same_max and (num_colors_left(hash) == 3)\n max_color_count = hash[max_color] - 1\n hash[max_color] = max_color_count\n else\n max_color_count = hash[max_color] - min_color_count\n hash[max_color] = max_color_count\n end\n\n puts \"max_color_count is now: #{hash[max_color]}\" if @debug \n \n if two_colors_same_max and (num_colors_left(hash) == 3)\n min_color_count = hash[min_color] - 1\n hash[min_color] = min_color_count\n else\n hash[min_color] = 0\n end\n puts \"min_color_count is now: #{hash[min_color]}\" if @debug \n \n num_days_can_eat = num_days_can_eat + min_color_count\n puts \"num_days_can_eat: #{num_days_can_eat}\" if @debug\n\n min_color = min_color(hash)\n max_color, two_colors_same_max = max_color(hash, min_color)\n puts hash if @debug\n puts \"min_color: #{min_color}\" if @debug\n puts \"max_color: #{max_color}\" if @debug\n end\n num_days_can_eat\n end\n \n def main\n i = 0\n num_lines = 0\n while input = gets\n if i == 0\n num_lines = input.to_i \n i = i + 1\n next\n else\n input_arr = input.split(' ').map!(&:to_i)\n \n num_days_can_eat = get_num_days_can_eat(input_arr)\n \n puts num_days_can_eat\n i = i + 1\n break if i > num_lines\n end\n end\n end\n \nend\n \nsweet_problem = SweetProblem.new\nsweet_problem.main\n\n"}, {"source_code": "\n\ndef to_hash(array)\n red = array[0]\n green = array[1]\n blue = array[2]\n result = Hash.new\n result['R'] = red\n result['G'] = green\n result['B'] = blue\n result\nend\n\ndef min_color(hash)\n min = (10**8) + 1\n min_color_key = nil\n hash.each do |key,val|\n next if val == 0\n if val < min\n min = val\n min_color_key = key\n end\n end\n return min_color_key\nend\n\ndef max_color(hash, color_key)\n max = 0\n max_color_key = nil\n hash.each do |key,val|\n if val > max and key != color_key\n max = val\n max_color_key = key\n end\n end\n return max_color_key\nend\n\ndef num_colors_left(hash)\n num_colors = 0\n hash.each do |key,val|\n if val > 0\n num_colors = num_colors + 1\n end\n end\n num_colors\nend\n\ni = 0\nnum_lines = 0\nwhile input = gets\n if i == 0\n num_lines = input.to_i \n i = i + 1\n next\n else\n input_arr = input.split(' ').map!(&:to_i)\n\n hash = to_hash(input_arr)\n\n min_color = min_color(hash)\n max_color = max_color(hash, min_color)\n\n num_days_can_eat = 0\n while num_colors_left(hash) >= 2\n min_color_count = hash[min_color]\n\n max_color_count = hash[max_color] - min_color_count\n hash[max_color] = max_color_count\n\n hash[min_color] = 0\n\n min_color = min_color(hash)\n max_color = max_color(hash, min_color)\n num_days_can_eat = num_days_can_eat + min_color_count\n end\n\n puts num_days_can_eat\n i = i + 1\n break if i > num_lines\n end\nend\n\n"}], "src_uid": "1f29461c42665523d0a4d56b13f7e480"} {"nl": {"description": "It is given a positive integer $$$n$$$. In $$$1$$$ move, one can select any single digit and remove it (i.e. one selects some position in the number and removes the digit located at this position). The operation cannot be performed if only one digit remains. If the resulting number contains leading zeroes, they are automatically removed.E.g. if one removes from the number $$$32925$$$ the $$$3$$$-rd digit, the resulting number will be $$$3225$$$. If one removes from the number $$$20099050$$$ the first digit, the resulting number will be $$$99050$$$ (the $$$2$$$ zeroes going next to the first digit are automatically removed).What is the minimum number of steps to get a number such that it is divisible by $$$25$$$ and positive? It is guaranteed that, for each $$$n$$$ occurring in the input, the answer exists. It is guaranteed that the number $$$n$$$ has no leading zeros.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of one line containing one integer $$$n$$$ ($$$25 \\le n \\le 10^{18}$$$). It is guaranteed that, for each $$$n$$$ occurring in the input, the answer exists. It is guaranteed that the number $$$n$$$ has no leading zeros.", "output_spec": "For each test case output on a separate line an integer $$$k$$$ ($$$k \\ge 0$$$) — the minimum number of steps to get a number such that it is divisible by $$$25$$$ and positive.", "sample_inputs": ["5\n100\n71345\n3259\n50555\n2050047"], "sample_outputs": ["0\n3\n1\n3\n2"], "notes": "NoteIn the first test case, it is already given a number divisible by $$$25$$$.In the second test case, we can remove the digits $$$1$$$, $$$3$$$, and $$$4$$$ to get the number $$$75$$$.In the third test case, it's enough to remove the last digit to get the number $$$325$$$.In the fourth test case, we can remove the three last digits to get the number $$$50$$$.In the fifth test case, it's enough to remove the digits $$$4$$$ and $$$7$$$."}, "positive_code": [{"source_code": "def f(dgts)\r\n n0 = dgts.length\r\n i0 = dgts.rindex(\"0\") || 0\r\n j0 = i0 > 0 && dgts.rindex(/[05]/, i0 - 1) || -10000\r\n\r\n n5 = dgts.length\r\n i5 = dgts.rindex(\"5\") || 0\r\n j5 = i5 > 0 && dgts.rindex(/[27]/, i5 - 1) || -10000\r\n\r\n # p [i0, j0, i5, j5]\r\n [n0 - 2 - j0, n5 - 2 - j5].min\r\nend\r\n\r\ngets.to_i.times do\r\n\r\nn = gets.chomp\r\nputs f(n)\r\n\r\nend\r\n"}, {"source_code": "t = gets.chomp.to_i\r\n\r\ndef solve \r\n\tn = gets.chomp.to_i\r\n\td = n.digits\r\n\td = d.reverse\r\n\tans = 1e9\r\n\tinf = -1e9\r\n\t#25 75 50 00\r\n\tindex = {2=>inf, 7=>inf, 5=>inf, 0=>inf}\r\n\r\n\t(0..(d.length - 1)).each { |i|\r\n\t\tif (d[i] == 5) \r\n\t\t\tans = [ans, (i - index[2] - 1) + d.length - i - 1].min\r\n\t\t\tans = [ans, (i - index[7] - 1) + d.length - i - 1].min\r\n\t\tend\r\n\t\tif (d[i] == 0) \r\n\t\t\tans = [ans, (i - index[0] - 1) + d.length - i - 1].min\r\n\t\t\tans = [ans, (i - index[5] - 1) + d.length - i - 1].min\r\n\t\tend\r\n\t\tif d[i] == 2 || d[i] == 7 || d[i] == 5 || d[i] == 0\r\n\t\t\tindex[d[i]] = i\r\n\t\tend\r\n\t}\r\n\tputs ans\r\nend\r\n\r\nt.times {\r\n\tsolve\r\n}\r\n"}, {"source_code": "t = gets.chomp.to_i\r\n\r\ndef solve \r\n\tn = gets.chomp.to_i\r\n\td = n.digits\r\n\td = d.reverse\r\n\tans = 1e9\r\n\tinf = -1e9\r\n\t#25 75 50 00\r\n\tindex = {2=>inf, 7=>inf, 5=>inf, 0=>inf}\r\n\r\n\t(0..(d.length - 1)).each { |i|\r\n\t\tif (d[i] == 5) \r\n\t\t\tans = [ans, (i - index[2] - 1) + d.length - i - 1].min\r\n\t\t\tans = [ans, (i - index[7] - 1) + d.length - i - 1].min\r\n\t\tend\r\n\t\tif (d[i] == 0) \r\n\t\t\tans = [ans, (i - index[0] - 1) + d.length - i - 1].min\r\n\t\t\tans = [ans, (i - index[5] - 1) + d.length - i - 1].min\r\n\t\tend\r\n\t\tindex[d[i]] = i\r\n\t}\r\n\tputs ans\r\nend\r\n\r\nt.times {\r\n\tsolve\r\n}\r\n"}, {"source_code": "def k(n)\r\n c1 = 0\r\n c2 = 0\r\n c3 = 0\r\n c4 = 0\r\n d1 = 0\r\n d2 = 0\r\n d3 = 0\r\n d4 = 0\r\n while n > 0\r\n dig = n % 10\r\n if dig == 0\r\n c1 += 1\r\n else\r\n d1 += 1\r\n end\r\n\r\n if c1 == 2\r\n return d1\r\n end\r\n\r\n if dig == 5 and c2 == 0\r\n c2+=1\r\n elsif dig == 2 and c2 == 1\r\n return d2\r\n else\r\n d2+=1\r\n end\r\n\r\n if(dig == 0 and c3 == 0)\r\n c3+=1\r\n elsif(dig == 5 and c3 == 1)\r\n return d3\r\n else\r\n d3+=1\r\n end\r\n\r\n\r\n if(dig == 5 and c4 == 0)\r\n c4+=1\r\n elsif(dig == 7 and c4 == 1)\r\n return d4\r\n else\r\n d4+=1\r\n end\r\n\r\n n = n / 10\r\n end\r\n\r\n return 1000\r\n\r\nend\r\n\r\ngets.to_i.times do\r\n n = gets.to_i\r\n puts k(n)\r\nend\r\n"}, {"source_code": "t = gets.chomp.to_i\r\n\r\ndef calc(pos, a, b)\r\n first = pos[b][-1]\r\n second = pos[a].reverse_each.find { |v| v > first }\r\n return Float::INFINITY if !second\r\n return first + second - first - 1\r\nend\r\n\r\nt.times do\r\n n = gets.chomp\r\n\r\n pos = Hash.new { |h, k| h[k] = [] }\r\n n.split('').each_with_index do |c, idx|\r\n if c == '0' || c == '2' || c == '5' || c == '7'\r\n pos[c] << n.length - idx - 1\r\n end\r\n end\r\n\r\n possibilities = []\r\n if pos['0'].length >= 2\r\n possibilities << calc(pos, '0', '0')\r\n end\r\n \r\n if pos['0'].length >= 1 && pos['5'].length >= 1\r\n possibilities << calc(pos, '5', '0')\r\n end\r\n \r\n if pos['2'].length >= 1 && pos['5'].length >= 1\r\n possibilities << calc(pos, '2', '5')\r\n end\r\n\r\n if pos['7'].length >= 1 && pos['5'].length >= 1\r\n possibilities << calc(pos, '7', '5')\r\n end\r\n\r\n puts possibilities.min\r\nend"}, {"source_code": "gets.to_i.times do\n\tn = gets.chomp\n\tif n.to_i % 25 == 0\n\t\tputs 0\n\t\tnext\n\tend\n\tres = 1000000000\n\tfor i in 0..n.length - 1 do\n\t\tfor j in i+1..n.length - 1 do\n\t\t\tif (n[i].to_i * 10 + n[j].to_i) % 25 == 0\n\t\t\t\tres = [res, j - i - 1 + (n.length - 1 - j)].min\n\t\t\tend\n\t\tend\n\tend\n\tputs res\nend"}], "negative_code": [{"source_code": "def f(dgts)\r\n n0 = dgts.length\r\n i0 = dgts.rindex(\"0\") || 0\r\n j0 = i0 > 0 && dgts.rindex(/[05]/, i0 - 1) || -1\r\n\r\n dgts = dgts.gsub(/0+(?!0)/, \"\")\r\n n5 = dgts.length\r\n i5 = dgts.rindex(\"5\") || 0\r\n j5 = i5 > 0 && dgts.rindex(/[27]/, i5 - 1) || -1\r\n \r\n [n0 - 2 - j0, n5 - 2 - j5].min\r\nend\r\n \r\ngets.to_i.times do\r\n \r\nn = gets.chomp\r\nputs f(n)\r\n \r\nend\r\n"}, {"source_code": "def f(dgts)\r\n n = dgts.length\r\n\r\n i0 = dgts.rindex(\"0\") || 1\r\n i5 = dgts.rindex(\"5\") || 1\r\n\r\n j0 = dgts.rindex(/[05]/, i0 - 1) || 0\r\n j5 = dgts.rindex(/[27]/, i5 - 1) || 0\r\n\r\n [n - 2 - j0, n - 2 - j5].min\r\nend\r\n\r\ngets.to_i.times do\r\n\r\nn = gets.chomp\r\nputs f(n)\r\n\r\nend\r\n"}], "src_uid": "ab6fefc6c15d4647bc601f1f9db21d3f"} {"nl": {"description": "This is the easy version of the problem. The only difference between the two versions is the constraint on $$$n$$$. You can make hacks only if all versions of the problem are solved.A forest is an undirected graph without cycles (not necessarily connected).Mocha and Diana are friends in Zhijiang, both of them have a forest with nodes numbered from $$$1$$$ to $$$n$$$, and they would like to add edges to their forests such that: After adding edges, both of their graphs are still forests. They add the same edges. That is, if an edge $$$(u, v)$$$ is added to Mocha's forest, then an edge $$$(u, v)$$$ is added to Diana's forest, and vice versa. Mocha and Diana want to know the maximum number of edges they can add, and which edges to add.", "input_spec": "The first line contains three integers $$$n$$$, $$$m_1$$$ and $$$m_2$$$ ($$$1 \\le n \\le 1000$$$, $$$0 \\le m_1, m_2 < n$$$) — the number of nodes and the number of initial edges in Mocha's forest and Diana's forest. Each of the next $$$m_1$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 \\le u, v \\le n$$$, $$$u \\neq v$$$) — the edges in Mocha's forest. Each of the next $$$m_2$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 \\le u, v \\le n$$$, $$$u \\neq v$$$) — the edges in Diana's forest.", "output_spec": "The first line contains only one integer $$$h$$$, the maximum number of edges Mocha and Diana can add (in each forest). Each of the next $$$h$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 \\le u, v \\le n$$$, $$$u \\neq v$$$) — the edge you add each time. If there are multiple correct answers, you can print any one of them.", "sample_inputs": ["3 2 2\n1 2\n2 3\n1 2\n1 3", "5 3 2\n5 4\n2 1\n4 3\n4 3\n1 4", "8 1 2\n1 7\n2 6\n1 5"], "sample_outputs": ["0", "1\n2 4", "5\n5 2\n2 3\n3 4\n4 7\n6 8"], "notes": "NoteIn the first example, we cannot add any edge.In the second example, the initial forests are as follows.We can add an edge $$$(2, 4)$$$."}, "positive_code": [{"source_code": "END { load __FILE__ unless $stdin.eof? }\n\nmodule AtCoder\n class DSU\n def initialize(n = 0)\n @n = n\n @parent_or_size = Array.new(n, -1)\n end\n\n def merge(a, b)\n x, y = leader(a), leader(b)\n return x if x == y\n\n x, y = y, x if -@parent_or_size[x] < -@parent_or_size[y]\n @parent_or_size[x] += @parent_or_size[y]\n @parent_or_size[y] = x\n end\n\n def same(a, b)\n leader(a) == leader(b)\n end\n\n def leader(a)\n return a if @parent_or_size[a] < 0\n\n @parent_or_size[a] = leader(@parent_or_size[a])\n end\n\n def size(a)\n -@parent_or_size[leader(a)]\n end\n\n def groups\n @n.times.group_by { |i| leader(i) }.values\n end\n end\nend\n\nn, m1, m2 = gets.split.map!(&:to_i)\ne1 = Array.new(m1) { gets.split.map!(&:to_i) }\ne2 = Array.new(m2) { gets.split.map!(&:to_i) }\n\ndsu1 = AtCoder::DSU.new(n + 1)\ndsu2 = AtCoder::DSU.new(n + 1)\n\ne1.each { |u, v| dsu1.merge(u, v) }\ne2.each { |u, v| dsu2.merge(u, v) }\n\ne3 = [*1..n].combination(2).select do |u, v|\n next false if dsu1.same(u, v) || dsu2.same(u, v)\n\n dsu1.merge(u, v)\n dsu2.merge(u, v)\n\n true\nend\n\nputs e3.size\ne3.each { |uv| puts uv * \" \" }\n"}, {"source_code": "# Disjoint Set Union\r\nclass DSU\r\n def initialize(n)\r\n @n = n\r\n @parent_or_size = Array.new(n, -1)\r\n # root node: -1 * component size\r\n # otherwise: parent\r\n end\r\n\r\n attr_reader :parent_or_size, :n\r\n\r\n def merge(a, b)\r\n x = leader(a)\r\n y = leader(b)\r\n return x if x == y\r\n\r\n x, y = y, x if -@parent_or_size[x] < -@parent_or_size[y]\r\n @parent_or_size[x] += @parent_or_size[y]\r\n @parent_or_size[y] = x\r\n end\r\n alias unite merge\r\n\r\n def same?(a, b)\r\n leader(a) == leader(b)\r\n end\r\n alias same same?\r\n\r\n def leader(a)\r\n unless 0 <= a && a < @n\r\n raise ArgumentError.new, \"#{a} is out of range (0...#{@n})\"\r\n end\r\n\r\n @parent_or_size[a] < 0 ? a : (@parent_or_size[a] = leader(@parent_or_size[a]))\r\n end\r\n alias root leader\r\n alias find leader\r\n\r\n def size(a)\r\n -@parent_or_size[leader(a)]\r\n end\r\n\r\n def groups\r\n (0 ... @parent_or_size.size).group_by{ |i| leader(i) }.values\r\n end\r\nend\r\n\r\nUnionFind = DSU\r\n\r\nn,m1,m2=gets.split.map(&:to_i)\r\nf1=DSU.new(n)\r\nf2=DSU.new(n)\r\n\r\nm1.times do\r\n u,v=gets.split.map(&:to_i).map{|x|x-1}\r\n f1.merge(u,v)\r\nend\r\nm2.times do\r\n u,v=gets.split.map(&:to_i).map{|x|x-1}\r\n f2.merge(u,v)\r\nend\r\n\r\naz=0\r\nans=[]\r\nn.times do |j|\r\n j.times do |i|\r\n if !f1.same?(i,j)&&!f2.same?(i,j)\r\n f1.merge(i,j)\r\n f2.merge(i,j)\r\n ans << [i+1,j+1]\r\n az+=1\r\n end\r\n end\r\nend\r\n\r\nputs az\r\nans.each do |aa|\r\n puts aa.join(\" \")\r\nend\r\n\r\n"}], "negative_code": [], "src_uid": "e3d2b67a62ac431718071ae20f3794aa"} {"nl": {"description": "Polycarp has guessed three positive integers $$$a$$$, $$$b$$$ and $$$c$$$. He keeps these numbers in secret, but he writes down four numbers on a board in arbitrary order — their pairwise sums (three numbers) and sum of all three numbers (one number). So, there are four numbers on a board in random order: $$$a+b$$$, $$$a+c$$$, $$$b+c$$$ and $$$a+b+c$$$.You have to guess three numbers $$$a$$$, $$$b$$$ and $$$c$$$ using given numbers. Print three guessed integers in any order.Pay attention that some given numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (it is also possible that $$$a=b=c$$$).", "input_spec": "The only line of the input contains four positive integers $$$x_1, x_2, x_3, x_4$$$ ($$$2 \\le x_i \\le 10^9$$$) — numbers written on a board in random order. It is guaranteed that the answer exists for the given number $$$x_1, x_2, x_3, x_4$$$.", "output_spec": "Print such positive integers $$$a$$$, $$$b$$$ and $$$c$$$ that four numbers written on a board are values $$$a+b$$$, $$$a+c$$$, $$$b+c$$$ and $$$a+b+c$$$ written in some order. Print $$$a$$$, $$$b$$$ and $$$c$$$ in any order. If there are several answers, you can print any. It is guaranteed that the answer exists.", "sample_inputs": ["3 6 5 4", "40 40 40 60", "201 101 101 200"], "sample_outputs": ["2 1 3", "20 20 20", "1 100 100"], "notes": null}, "positive_code": [{"source_code": "def read_tokens\n return gets.chomp.to_s.strip.split(' ')\nend\n\ndef read_ints\n data = []\n read_tokens.each { |s|\n data.push(s.to_i)\n }\n return data\nend\n\narr = read_ints()\n\ndef solve(arr)\n maximum = arr.max\n arr.each { |el|\n if el != maximum\n print \"#{maximum - el} \"\n end\n }\nend\n\nsolve(arr)\n"}, {"source_code": "#!usr/bin/env ruby\n\n# https://codeforces.com/problemset/problem/1154/A\n\nnumbers = gets.split(' ').map(&:to_i)\n\nmax = numbers.max\nanswer = []\n\nnumbers.each do |n|\n if (temp = max - n) != 0\n answer << temp\n end\nend\n\nputs answer.join(' ')\n"}, {"source_code": "a,b,c,d=gets.split.map(&:to_i).sort\nputs\"#{d-a} #{d-b} #{d-c}\""}, {"source_code": "X1, X2, X3, X4 = gets.split.map(&:to_i)\n\nx1, x2, x3, x4 = [X1, X2, X3, X4].sort\n\ni = x4 - x1\nj = x4 - x2\nk = x4 - x3\n\nputs [i, j, k].join(' ')\n"}, {"source_code": "x1,x2,x3,x4 = gets.scan(/\\d+/).map(&:to_i).sort\nc = x1 + x3 -x4\na = x1 - c \nb = x3 - c\nputs \"#{a} #{b} #{c}\"\n"}, {"source_code": "x1, x2, x3, x4 = gets.split.map(&:to_i).sort\nputs \"#{x4-x1} #{x4-x2} #{x4-x3}\"\n"}, {"source_code": "a,b,c,d = gets.chomp.split.map(&:to_i)\n\nn=[a,b,c,d].max\narr=[a,b,c,d]-[n]\nputs [n-arr[0],n-arr[1],n-arr[2]].join(' ')"}, {"source_code": "def uwu(inte)\n\tmax = 0\n\toutput = ''\n\tfor number in inte\n\t\tmax = number.to_i if number.to_i >= max\n\tend\n\tinte.delete(max.to_s)\n\tfor number in inte\n\t\tresutlat = max - number.to_i\n\t\toutput << resutlat.to_s\n\t\toutput << ' '\n\tend\n\toutput[-1] = '' if output[-1] == ' '\n\treturn output\nend\n\ninte = gets.chomp.split(\" \")\nputs(uwu(inte))"}, {"source_code": "a,b,c,d = gets.split.map(&:to_i).sort\nputs \"#{d-a} #{d-b} #{d-c}\""}, {"source_code": "a,b,c,d=gets.split.map(&:to_i).sort;p d-a,d-b,d-c"}, {"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\n\ninputs = gets.split.map { |i| i.to_i }\n\n# inputs[?] = a + b\n# inputs[?] = a + c\n# inputs[?] = b + c\n# inputs[?] = a + b + c\nans = []\n# 모두 양수이므로, 가장 큰 수가 a + b + c\nabc = inputs.max\ninputs.delete abc\ninputs.each { |i| ans.push abc - i }\n\nputs ans.join ' '\n\n"}, {"source_code": "arr = gets.split.map(&:to_i)\narr.sort!\narr.reverse!\nanswer = []\nanswer << arr[0]-arr[1]\nanswer << arr[0]-arr[2]\nanswer << arr[0]-arr[3]\nputs answer.join(' ')"}, {"source_code": "$n = gets\n$a, $b, $c, $d = $n.split(' ').to_a\narr = [$a.to_i, $b.to_i, $c.to_i, $d.to_i]\n$k = arr.max\narr.delete($k)\nprint $k.to_i-arr[0].to_i,' ',$k.to_i-arr[1].to_i,' ',$k.to_i-arr[2].to_i"}, {"source_code": "require \"matrix\"\nngo = gets.chomp.split(\" \").map(&:to_i).sort\nputs Matrix[[1,1,0],[1,0,1],[0,1,1]].lup.solve(ngo.slice(0,3)).to_a.map(&:to_i).join(\" \")\n"}, {"source_code": "x = gets.split.map(&:to_i).sort\nputs [x[3] - x[2], x[3] - x[1], x[3] - x[0]].join ' '"}, {"source_code": "sums = gets.chomp.split(\" \").map(&:to_i)\n\nmax = sums[0]\nmax_idx = 0\nsums.each_with_index do |sum, idx|\n if sum > max\n max = sum\n max_idx = idx\n end\nend\n\nnumbers = []\n\nsums.each_with_index do |sum, idx|\n if idx != max_idx\n n = max - sum\n numbers.push(n)\n end\nend\n\nans = numbers.join(\" \")\nputs ans"}, {"source_code": "line = gets\nn = line.split.map { |x| x.to_i }\nsum = n.max\nn.delete_at(n.index(sum))\nn.map!{|x| sum - x}\nputs(n.join ' ')\n"}, {"source_code": "a = gets.split(' ').map(&:to_i).sort.reverse\n\nm = a.shift\nputs a.map { |x| m-x }.join(' ')\n"}, {"source_code": "x = gets.split(' ').map(&:to_i).sort\nv = []\nx.each do |xx|\n v.push(x[3]-xx)\nend\nputs \"#{v[0]} #{v[1]} #{v[2]}\"\n"}], "negative_code": [{"source_code": "a,b,c,d=gets.split.map(&:to_i).sort\np\"#{d-a} #{d-b} #{d-c}\""}], "src_uid": "cda949a8fb1f158f3c06109a2d33f084"} {"nl": {"description": "In Berland the opposition is going to arrange mass walking on the boulevard. The boulevard consists of n tiles that are lain in a row and are numbered from 1 to n from right to left. The opposition should start walking on the tile number 1 and the finish on the tile number n. During the walk it is allowed to move from right to left between adjacent tiles in a row, and jump over a tile. More formally, if you are standing on the tile number i (i < n - 1), you can reach the tiles number i + 1 or the tile number i + 2 from it (if you stand on the tile number n - 1, you can only reach tile number n). We can assume that all the opposition movements occur instantaneously.In order to thwart an opposition rally, the Berland bloody regime organized the rain. The tiles on the boulevard are of poor quality and they are rapidly destroyed in the rain. We know that the i-th tile is destroyed after ai days of rain (on day ai tile isn't destroyed yet, and on day ai + 1 it is already destroyed). Of course, no one is allowed to walk on the destroyed tiles! So the walk of the opposition is considered thwarted, if either the tile number 1 is broken, or the tile number n is broken, or it is impossible to reach the tile number n from the tile number 1 if we can walk on undestroyed tiles.The opposition wants to gather more supporters for their walk. Therefore, the more time they have to pack, the better. Help the opposition to calculate how much time they still have and tell us for how many days the walk from the tile number 1 to the tile number n will be possible.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 103) — the boulevard's length in tiles. The second line contains n space-separated integers ai — the number of days after which the i-th tile gets destroyed (1 ≤ ai ≤ 103). ", "output_spec": "Print a single number — the sought number of days.", "sample_inputs": ["4\n10 3 5 10", "5\n10 2 8 3 5"], "sample_outputs": ["5", "5"], "notes": "NoteIn the first sample the second tile gets destroyed after day three, and the only path left is 1 → 3 → 4. After day five there is a two-tile gap between the first and the last tile, you can't jump over it.In the second sample path 1 → 3 → 5 is available up to day five, inclusive. On day six the last tile is destroyed and the walk is thwarted."}, "positive_code": [{"source_code": "n = gets.chomp.to_i\na = gets.chomp.split(\" \").map{|e| e.to_i}\n\ndays = 0\nloop do\n break if a[0] == 0 or a[-1] == 0\n \n bang = false\n for i in 1 .. (n - 2) do\n bang = true if a[i] == 0 and a[i + 1] == 0\n end\n break if bang\n \n days += 1\n \n a.map!{|e|\n e > 0 ? e -= 1 : e\n } \nend\n\nputs days\n"}, {"source_code": "n=gets.to_i\na=gets.chomp.split.map(&:to_i)\nj,i=1+a[0],n\na[0]-=1\nif n>1\n\tj=1\n\twhile i>0\n\t\ti=1\n\t\twhile i 0 \n\t\t\tfinish = true\n\t\t\tbreak \n\t\tend\n\tend\n\tbreak if finish\nend\nputs ans"}, {"source_code": "gets\nns = gets.split.map(&:to_i)\nsorted = ns.map.with_index{|n, i| [n, i]}.sort_by(&:first)\nhash = {}\nanswer = ns.first\nsorted.each do |n, i|\n hash[i] = true\n if hash[i-1] || hash[i+1] || hash[0] || hash[ns.length - 1]\n answer = n\n break\n end\nend\n\nputs answer\n"}, {"source_code": "gets\nv = gets.split.map(&:to_i)\n\nd = Array.new(v.length,-1)\nd[0] = v[0]\n\n0.upto(v.length-2) do |i|\n\tnext if d[i] == -1\n\tif d[i+1] == -1 or d[i+1] < [d[i],v[i+1]].min\n\t\td[i+1] = [d[i],v[i+1]].min\n\tend\n\tif i + 2 < v.length and (d[i+2] == -1 or d[i+2] < [d[i],v[i+2]].min)\n\t\td[i+2] = [d[i],v[i+2]].min\n\tend\nend\n\nputs d[v.length-1]\n"}, {"source_code": "gets\nv = gets.split.collect { |x| x.to_i}\n\nd = Array.new(v.length,-1)\nd[0] = v[0]\n\nbegin\n\tch = false\n\t0.upto(v.length-2) do |i|\n\t\tif d[i] != -1\n\t\t\tif d[i+1] == -1 or d[i+1] < [d[i],v[i+1]].min\n\t\t\t\td[i+1] = [d[i],v[i+1]].min\n\t\t\t\tch = true\n\t\t\tend\n\t\t\tif i + 2 < v.length and (d[i+2] == -1 or d[i+2] < [d[i],v[i+2]].min)\n\t\t\t\td[i+2] = [d[i],v[i+2]].min\n\t\t\t\tch = true\n\t\t\tend\n\t\tend\n\tend\nend while ch\n\nputs d[v.length-1]\n"}], "negative_code": [{"source_code": "n = gets.chomp.to_i\na = gets.chomp.split(\" \").map{|e| e.to_i}\n\na << 0\na << 0\n\ndays = 0\nloop do\n bang = false\n for i in 0 .. (n - 2) do\n bang = true if ! ((a[i] > 0 and (a[i + 1] > 0 or a[i + 2] > 0)) or (a[i] == 0 and a[i + 1] > 0)) \n end\n \n break if bang\n \n days += 1\n \n a.map!{|e|\n e > 0 ? e -= 1 : e\n } \nend\n\nputs days\n"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split(\" \").map{|e| e.to_i}\n\ndays = 0\nloop do\n bang = false\n for i in 0 .. (n - 3) do\n bang = true if !((a[i] > 0 and (a[i + 1] > 0 or a[i + 2] > 0)) or (a[i] <= 0 and (a[i + 1] > 0)))\n end\n \n break if bang\n \n days += 1\n \n a.map!{|e| e -= 1} \nend\n\nputs days\n"}, {"source_code": "n = gets.to_i\na = gets.split.map{|e| e.to_i}\nmin=[a[0],a[n-1]].min\nfor i in (0..n-2) do\n c = [a[i],a[i+1]].max\n puts c\n if c 0\n lift_targets = ordered_list[0..(location - 1)]\n remaining = ordered_list[(location + 1)..-1]\n costs += lift_targets.reduce(0) {|memo, item| memo + w[item - 1] }\n ordered_list = [ordered_list[location]] + lift_targets + remaining\n end\n end\n\n puts costs\nend\n\nrun if $0 == __FILE__\n"}], "negative_code": [{"source_code": "def g;gets.split.map(&:to_i)end\nn,_=g\nw=g\nb=g\n#[*1..n].permutation{|st|\n# sum=0\n# b.each{|i|\n# x=st.index(i)\n# st.delete_at(x)\n# sum+=st[x..-1].inject(0){|s,r|s+w[r-1]}\n# st<< i\n# }\n# p sum\n#}\nst=(1..n).sort_by{|i|-(b.index(i)||n)}\nsum=0\nb.each{|i|\n x=st.index(i)\n st.delete_at(x)\n sum+=st[x..-1].inject(0){|s,r|s+w[r-1]}\n st<< i\n}\np sum"}], "src_uid": "a18edcadb31f76e69968b0a3e1cb7e3e"} {"nl": {"description": "You are given an array $$$a$$$ consisting of $$$n$$$ integer numbers.Let instability of the array be the following value: $$$\\max\\limits_{i = 1}^{n} a_i - \\min\\limits_{i = 1}^{n} a_i$$$.You have to remove exactly one element from this array to minimize instability of the resulting $$$(n-1)$$$-elements array. Your task is to calculate the minimum possible instability.", "input_spec": "The first line of the input contains one integer $$$n$$$ ($$$2 \\le n \\le 10^5$$$) — the number of elements in the array $$$a$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^5$$$) — elements of the array $$$a$$$.", "output_spec": "Print one integer — the minimum possible instability of the array if you have to remove exactly one element from the array $$$a$$$.", "sample_inputs": ["4\n1 3 3 7", "2\n1 100000"], "sample_outputs": ["2", "0"], "notes": "NoteIn the first example you can remove $$$7$$$ then instability of the remaining array will be $$$3 - 1 = 2$$$.In the second example you can remove either $$$1$$$ or $$$100000$$$ then instability of the remaining array will be $$$100000 - 100000 = 0$$$ and $$$1 - 1 = 0$$$ correspondingly."}, "positive_code": [{"source_code": "n,*a=$<.read.split.map &:to_i;a.sort!;p n<3?0:[a[-1]-a[1],a[-2]-a[0]].min"}, {"source_code": "# Input\nn = gets.to_i\na = gets.split.map { |s| s.to_i }\n\n# Processing\na.sort!\ns_1 = a[-2] - a[0] # Deleted largest element\ns_2 = a[-1] - a[1] # Deleted smallest element\nresult = [s_1, s_2].min\n\n# Output\nputs result\n"}, {"source_code": "def minimize_instability(n, a)\n min1 = min2 = 100_001\n max1 = max2 = 0\n n.times do |i|\n if a[i] < min1\n min2 = min1\n min1 = a[i]\n elsif a[i] < min2\n min2 = a[i]\n end\n if a[i] > max1\n max2 = max1\n max1 = a[i]\n elsif a[i] > max2\n max2 = a[i]\n end\n end\n\n min = 1_000_000\n n.times do |i|\n u = max1\n u = max2 if u == a[i]\n l = min1\n l = min2 if l == a[i]\n min = [min, u - l].min\n end\n return min\nend\n\nn = gets.chomp.to_i\na = gets.chomp.split(\" \").map(&:to_i)\n\nputs minimize_instability(n, a)\n"}, {"source_code": "n = gets.to_i\na = gets.split.map &:to_i\na.sort!\nputs [a[-1] - a[1], a[-2] - a[0]].min\n"}, {"source_code": "n = gets.to_i\narr = gets.chomp.split(\" \").map(&:to_i)\n\nmax = 0\nmaxtwo = 0\nmin = 1.0/0.0\nmintwo = 1.0/0.0\n\narr.each do |x|\n if x > max\n maxtwo = max\n max = x\n elsif x > maxtwo\n maxtwo = x\n end\n\n if x < min\n mintwo = min\n min = x\n elsif x +/).join.size"}, {"source_code": "n = gets.strip.to_i\ns = gets.strip\nans = 0\ni = 0\nwhile i < n && s[i] == '<'\n ans += 1\n i += 1\nend\ni = n - 1\nwhile i >= 0 && s[i] == '>'\n ans += 1\n i -= 1\nend\nputs ans\n"}, {"source_code": "n = gets.to_i\ns = gets.strip\n\ni = 0\nwhile i < n && s[i] == '<'\n i += 1\nend\n\nj = 0\n\nwhile j < n && s[n - j - 1] == '>'\n j += 1\nend\n\nputs i + j"}, {"source_code": "n=gets.to_i\np~(gets.rindex(?<)||-1)+n+($_.index(?>)||n)"}, {"source_code": "gets\ns=gets.chop\nr=0\ns.chars{|c|c==?>&&break;r+=1}\ns.reverse.chars{|c|c==?<&&break;r+=1}\np r"}, {"source_code": "#!/usr/bin/ruby\n\nsize = gets.to_i\nstr = gets\nright = true\nleft = true\ncontinue = true\nfinal_number = 0\ni=0\nwhile continue do \n\tif right\n\t\tif str[i] == '<'\n\t\t\tfinal_number += 1\n\t\telse\n\t\t\tright = false \n\t\tend\t\n\tend\n\tif left\n\t\tif str[size - 1 - i] == '>'\n\t\t\tfinal_number += 1\n\t\telse\n\t\t\tleft = false\n\t\tend\n\tend\t\n\ti+=1\n\tif !left and !right\n\t\tcontinue = false\n\tend\t\nend\nputs final_number"}, {"source_code": "n = gets.to_i\ns = gets.chomp\nputs (s.index('>') || n) + (n - 1 - (s.rindex('<') || -1))\n"}, {"source_code": "n = gets.to_i\na = gets.strip\ncnt = 0\ni = 0\nlen = a.length\nwhile(1) do\n break if a[i] == '>' || i == len\n cnt += 1\n i += 1\nend\n\ni = len - 1\nwhile(1) do\n break if a[i] == '<' || i == -1\n cnt += 1\n i -=1\nend\n\nprintf(\"%d\\n\", cnt)\n"}, {"source_code": "length = gets.to_i\nbumpers = gets\n\nfirst_greater = bumpers.index('>')\nlast_lesser = bumpers.rindex('<')\n\nif first_greater.nil? || last_lesser.nil?\n total_falling_positions = length\nelse\n total_falling_positions = length - (last_lesser - first_greater + 1)\nend\n\nputs total_falling_positions\n"}], "negative_code": [{"source_code": "gets\np gets.scan(/(^\\<+|\\>+$)/).map(&:size).reduce(0,:+)"}, {"source_code": "n = gets.strip.to_i\ns = gets.strip\nfirst = s[0]\nlast = s[n - 1]\nans = 0\nif first == '<'\n ans = 1\nelsif last == '>'\n ans = n\nend\nputs ans\n"}, {"source_code": "n = gets.strip.to_i\ns = gets.strip\nfirst = s[0]\nlast = s[n - 1]\nans = 0\nif first == '<'\n ans = 1\nelsif last == '>'\n ans = n\nelse\n if first == '>' && last == '<'\n ans = 0\n end\nend\nputs ans\n"}, {"source_code": "n = gets.to_i\ns = gets.strip\n\ni = 0\nwhile i < n && s[i] == '<'\n i += 1\nend\n\nj = 0\n\nwhile j < n && s[n - j + 1] == '>'\n i += 1\nend\n\nputs i + j"}, {"source_code": "gets\ns=gets\nr=0\ns.chars{|c|c==?>&&break;r+=1}\ns.reverse.chars{|c|c==?<&&break;r+=1}\np r"}], "src_uid": "6b4242ae9a52d36548dda79d93fe0aef"} {"nl": {"description": "Watchmen are in a danger and Doctor Manhattan together with his friend Daniel Dreiberg should warn them as soon as possible. There are n watchmen on a plane, the i-th watchman is located at point (xi, yi).They need to arrange a plan, but there are some difficulties on their way. As you know, Doctor Manhattan considers the distance between watchmen i and j to be |xi - xj| + |yi - yj|. Daniel, as an ordinary person, calculates the distance using the formula .The success of the operation relies on the number of pairs (i, j) (1 ≤ i < j ≤ n), such that the distance between watchman i and watchmen j calculated by Doctor Manhattan is equal to the distance between them calculated by Daniel. You were asked to compute the number of such pairs.", "input_spec": "The first line of the input contains the single integer n (1 ≤ n ≤ 200 000) — the number of watchmen. Each of the following n lines contains two integers xi and yi (|xi|, |yi| ≤ 109). Some positions may coincide.", "output_spec": "Print the number of pairs of watchmen such that the distance between them calculated by Doctor Manhattan is equal to the distance calculated by Daniel.", "sample_inputs": ["3\n1 1\n7 5\n1 5", "6\n0 0\n0 1\n0 2\n-1 1\n0 1\n1 1"], "sample_outputs": ["2", "11"], "notes": "NoteIn the first sample, the distance between watchman 1 and watchman 2 is equal to |1 - 7| + |1 - 5| = 10 for Doctor Manhattan and for Daniel. For pairs (1, 1), (1, 5) and (7, 5), (1, 5) Doctor Manhattan and Daniel will calculate the same distances."}, "positive_code": [{"source_code": "n = gets.chomp.to_i\nhx = Hash.new(0)\nhy = Hash.new(0)\nhxy = Hash.new(0)\nn.times do\n x, y = gets.chomp.split.map(&:to_i)\n hx[x] += 1\n hy[y] += 1\n hxy[(x + 10**9) * 10**10 + (y + 10**9)] += 1\nend\n\nans = 0\nhx.each do |_, v| \n ans += v * (v - 1) / 2\nend\nhy.each do |_, v| \n ans += v * (v - 1) / 2\nend\nhxy.each do |_, v| \n ans -= v * (v - 1) / 2\nend\n\nputs ans\n"}, {"source_code": "#!/usr/bin/ruby\nrequire 'scanf'\n\nx_paintings = Hash.new(0)\ny_paintings = Hash.new(0)\nxy_paintings = Hash.new(0)\nwatchmen = gets().to_i\n\npairs = 0\nwatchmen.times do\n x, y = gets().split(' ').map(&:to_i)\n pairs += x_paintings[x]; x_paintings[x] += 1\n pairs += y_paintings[y]; y_paintings[y] += 1\n pairs -= xy_paintings[\"#{x},#{y}\"]; xy_paintings[\"#{x},#{y}\"] += 1\nend\n\nputs pairs"}, {"source_code": "n = gets.to_i\nx=[]\ny=[]\na=[]\nn.times do\n\tb,c = gets.split.map(&:to_i)\n\tx << b\n\ty << c\n\ta << [b,c]\nend\n\nx.sort!\ny.sort!\na.sort!\nc=\"a\"\ncc = 1\nac = 0\nn.times do |i|\n\tcx=x[i]\n\tif c!=cx\n\t\tac += cc*(cc-1)/2\n\t\tc=cx\n\t\tcc=1\n\telse\n\t\tcc+=1\n\tend\nend\nac += cc*(cc-1)/2\n\nc=\"a\"\ncc = 1\nn.times do |i|\n\tcx=y[i]\n\tif c!=cx\n\t\tac += cc*(cc-1)/2\n\t\tc=cx\n\t\tcc=1\n\telse\n\t\tcc+=1\n\tend\nend\nac += cc*(cc-1)/2\n\nc=-1\ncc = 1\nn.times do |i|\n\tcx=a[i]\n\tif c!=cx\n\t\tac -= cc*(cc-1)/2\n\t\tc=cx\n\t\tcc=1\n\telse\n\t\tcc+=1\n\tend\nend\nac -= cc*(cc-1)/2\n\nputs ac\n\n\n\n"}, {"source_code": "n = gets.to_i\nlocations = []\nn.times do \n locations << gets.split.map(&:to_i)\nend\ndigit_count_x = {}\ndigit_count_y = {}\ndigit_count_xy = {}\n\ncount = 0\nlocations.each do |x, y|\n count += digit_count_x[x] if digit_count_x[x] && digit_count_x[x] > 0\n digit_count_x[x] = digit_count_x[x] ? digit_count_x[x] + 1 : 1\n count += digit_count_y[y] if digit_count_y[y] && digit_count_y[y] > 0\n digit_count_y[y] = digit_count_y[y] ? digit_count_y[y] + 1 : 1 \n count -= digit_count_xy[[x, y]] if digit_count_xy[[x, y]] && digit_count_xy[[x, y]] > 0\n digit_count_xy[[x, y]] = digit_count_xy[[x, y]] ? digit_count_xy[[x, y]] + 1 : 1\nend\nputs count"}, {"source_code": "def cal hash\n hash.values.map{|n| n * (n - 1) / 2}.reduce(0, :+)\nend\n\nn = readline.to_i\nhx, hy, hxy = Hash.new(0), Hash.new(0), Hash.new(0)\nn.times do\n x, y = readline.split(' ').collect(&:to_i)\n hx[x] += 1\n hy[y] += 1\n hxy[[x, y]] += 1\nend\n\nputs cal(hx) + cal(hy) - cal(hxy)\n"}, {"source_code": "n=gets.to_i\na=Hash.new(0)\nb=Hash.new(0)\np=Hash.new(0)\nfor i in 1..n\n\tbuf = gets.split(\" \")\n\ta[buf[0]]+=1\n\tb[buf[1]]+=1\n\tp[buf]+=1\nend\nans=0\na.each_value{|v| ans+=v*(v-1)/2 }\nb.each_value{|v| ans+=v*(v-1)/2 }\np.each_value{|v| ans-=v*(v-1)/2 }\nputs ans"}], "negative_code": [{"source_code": "#!/usr/bin/ruby\nrequire 'scanf'\n\nx_paintings = Hash.new(0)\ny_paintings = Hash.new(0)\nxy_paintings = Hash.new(0)\nwatchmen = scanf(\"%d\\n\").first.to_i\n\npairs = 0\nwatchmen.times do\n x, y = scanf(\"%d %d\").map(&:to_i)\n pairs += x_paintings[x]; x_paintings[x] += 1\n pairs += y_paintings[y]; y_paintings[y] += 1\n pairs -= xy_paintings[\"#{x},#{y}\"]; xy_paintings[\"#{x},#{y}\"] += 1\nend\n\nputs pairs"}, {"source_code": "#!/usr/bin/ruby\nrequire 'scanf'\n\nx_paintings = Hash.new(0)\ny_paintings = Hash.new(0)\nxy_paintings = Hash.new(0)\nwatchmen = scanf(\"%d\").first.to_i\n\npairs = 0\nwatchmen.times do\n x, y = scanf(\"%d %d\").map(&:to_i)\n pairs += x_paintings[x]; x_paintings[x] += 1\n pairs += y_paintings[y]; y_paintings[y] += 1\n pairs -= xy_paintings[\"#{x},#{y}\"]; xy_paintings[\"#{x},#{y}\"] += 1\nend\n\nputs pairs"}, {"source_code": "n = gets.to_i\n\nlocations = gets.split.map(&:to_i)\ndigit_count_x = {}\ndigit_count_y = {}\ndigit_count_xy = {}\n\ncount = 0\nlocations.each do |x, y|\n count += digit_count_x[x] if digit_count_x[x] && digit_count_x[x] > 0\n digit_count_x[x] = digit_count_x[x] ? digit_count_x[x] + 1 : 1\n count += digit_count_y[y] if digit_count_y[y] && digit_count_y[y] > 0\n digit_count_y[y] = digit_count_y[y] ? digit_count_y[y] + 1 : 1 \n count -= digit_count_xy[[x, y]] if digit_count_xy[[x, y]] && digit_count_xy[[x, y]] > 0\n digit_count_xy[[x, y]] = digit_count_xy[[x, y]] ? digit_count_xy[[x, y]] + 1 : 1\nend\nputs count"}], "src_uid": "bd7b85c0204f6b36dc07f8a96fc36161"} {"nl": {"description": "This year in Equestria was a year of plenty, so Applejack has decided to build some new apple storages. According to the advice of the farm designers, she chose to build two storages with non-zero area: one in the shape of a square and another one in the shape of a rectangle (which possibly can be a square as well).Applejack will build the storages using planks, she is going to spend exactly one plank on each side of the storage. She can get planks from her friend's company. Initially, the company storehouse has $$$n$$$ planks, Applejack knows their lengths. The company keeps working so it receives orders and orders the planks itself. Applejack's friend can provide her with information about each operation. For convenience, he will give her information according to the following format: $$$+$$$ $$$x$$$: the storehouse received a plank with length $$$x$$$ $$$-$$$ $$$x$$$: one plank with length $$$x$$$ was removed from the storehouse (it is guaranteed that the storehouse had some planks with length $$$x$$$). Applejack is still unsure about when she is going to order the planks so she wants to know if she can order the planks to build rectangular and square storages out of them after every event at the storehouse. Applejack is busy collecting apples and she has completely no time to do the calculations so she asked you for help!We remind you that all four sides of a square are equal, and a rectangle has two pairs of equal sides.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$): the initial amount of planks at the company's storehouse, the second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le 10^5$$$): the lengths of the planks. The third line contains a single integer $$$q$$$ ($$$1 \\le q \\le 10^5$$$): the number of events in the company. Each of the next $$$q$$$ lines contains a description of the events in a given format: the type of the event (a symbol $$$+$$$ or $$$-$$$) is given first, then goes the integer $$$x$$$ ($$$1 \\le x \\le 10^5$$$).", "output_spec": "After every event in the company, print \"YES\" if two storages of the required shape can be built from the planks of that company's set, and print \"NO\" otherwise. You can print each letter in any case (upper or lower).", "sample_inputs": ["6\n1 1 1 2 1 1\n6\n+ 2\n+ 1\n- 1\n+ 2\n- 1\n+ 2"], "sample_outputs": ["NO\nYES\nNO\nNO\nNO\nYES"], "notes": "NoteAfter the second event Applejack can build a rectangular storage using planks with lengths $$$1$$$, $$$2$$$, $$$1$$$, $$$2$$$ and a square storage using planks with lengths $$$1$$$, $$$1$$$, $$$1$$$, $$$1$$$.After the sixth event Applejack can build a rectangular storage using planks with lengths $$$2$$$, $$$2$$$, $$$2$$$, $$$2$$$ and a square storage using planks with lengths $$$1$$$, $$$1$$$, $$$1$$$, $$$1$$$."}, "positive_code": [{"source_code": "n = gets.to_i\na = gets.chomp.split(\" \").map(&:to_i)\nq = gets.to_i\ns = Array.new{Array.new}\nq.times do |i|\n s[i] = gets.chomp.split(\" \")\nend\nhash = Hash.new(0)\nmore4 = Hash.new(0)\nmore2 = Hash.new(0)\na.each do |x|\n hash[x] += 1\n if hash[x] >= 2\n more2[x] += 1\n end\n if hash[x] >= 4\n more4[x] += 1\n end\nend\nans = []\nq.times do |i|\n if s[i][0] == \"+\"\n hash[s[i][1].to_i] += 1\n if hash[s[i][1].to_i] >=2\n more2[s[i][1].to_i] += 1\n end\n if hash[s[i][1].to_i] >=4\n more4[s[i][1].to_i] += 1\n end\n elsif s[i][0] == \"-\"\n hash[s[i][1].to_i] -= 1\n if more2[s[i][1].to_i] >= 1\n more2[s[i][1].to_i] -= 1\n if more2[s[i][1].to_i] == 0\n more2.delete(s[i][1].to_i)\n end\n end\n if more4[s[i][1].to_i] >= 1\n more4[s[i][1].to_i] -= 1\n if more4[s[i][1].to_i] == 0\n more4.delete(s[i][1].to_i)\n end\n end\n end\n if more4.length >= 2\n ans << \"YES\"\n elsif more4.length == 1\n key = 0\n value = 0\n more4.each do |k,v|\n key = k\n value = v\n end\n if value >= 5\n ans << \"YES\"\n elsif more2[key] >= 5\n if more2.length >= 2\n ans << \"YES\"\n else\n ans << \"NO\"\n end\n elsif more2.length >= 3\n ans << \"YES\"\n else\n ans << \"NO\"\n end\n else\n ans << \"NO\"\n end\nend\nans.each do |str|\n puts str\nend"}], "negative_code": [{"source_code": "n = gets.to_i\na = gets.chomp.split(\" \").map(&:to_i)\nq = gets.to_i\ns = Array.new{Array.new}\nq.times do |i|\n s[i] = gets.chomp.split(\" \")\nend\nhash = Hash.new(0)\nmore4 = Hash.new(0)\nmore2 = Hash.new(0)\na.each do |x|\n hash[x] += 1\n if hash[x] >= 2\n more2[x] += 1\n end\n if hash[x] >= 4\n more4[x] += 1\n end\nend\nans = []\nq.times do |i|\n if s[i][0] == \"+\"\n hash[s[i][1].to_i] += 1\n if hash[s[i][1].to_i] >=2\n more2[s[i][1].to_i] += 1\n end\n if hash[s[i][1].to_i] >=4\n more4[s[i][1].to_i] += 1\n end\n elsif s[i][0] == \"-\"\n hash[s[i][1]] -= 1\n if more2[s[i][1].to_i] >= 1\n more2[s[i][1].to_i] -= 1\n if more2[s[i][1].to_i] == 0\n more2.delete(s[i][1].to_i)\n end\n end\n if more4[s[i][1].to_i] >= 1\n more4[s[i][1].to_i] -= 1\n if more4[s[i][1].to_i] == 0\n more4.delete(s[i][1].to_i)\n end\n end\n end\n if more4.length >= 2\n ans << \"YES\"\n elsif more4.length == 1\n key = 0\n value = 0\n more4.each do |k,v|\n key = k\n value = v\n end\n if value >= 5\n ans << \"YES\"\n elsif more2[key] >= 5\n if more2.length >= 2\n ans << \"YES\"\n else\n ans << \"NO\"\n end\n elsif more2.length >= 3\n ans << \"YES\"\n else\n ans << \"NO\"\n end\n else\n ans << \"NO\"\n end\nend\nans.each do |str|\n puts str\nend"}], "src_uid": "d14bad9abf2a27ba57c80851405a360b"} {"nl": {"description": "The string $$$s$$$ is given, the string length is odd number. The string consists of lowercase letters of the Latin alphabet.As long as the string length is greater than $$$1$$$, the following operation can be performed on it: select any two adjacent letters in the string $$$s$$$ and delete them from the string. For example, from the string \"lemma\" in one operation, you can get any of the four strings: \"mma\", \"lma\", \"lea\" or \"lem\" In particular, in one operation, the length of the string reduces by $$$2$$$.Formally, let the string $$$s$$$ have the form $$$s=s_1s_2 \\dots s_n$$$ ($$$n>1$$$). During one operation, you choose an arbitrary index $$$i$$$ ($$$1 \\le i < n$$$) and replace $$$s=s_1s_2 \\dots s_{i-1}s_{i+2} \\dots s_n$$$.For the given string $$$s$$$ and the letter $$$c$$$, determine whether it is possible to make such a sequence of operations that in the end the equality $$$s=c$$$ will be true? In other words, is there such a sequence of operations that the process will end with a string of length $$$1$$$, which consists of the letter $$$c$$$?", "input_spec": "The first line of input data contains an integer $$$t$$$ ($$$1 \\le t \\le 10^3$$$) — the number of input test cases. The descriptions of the $$$t$$$ cases follow. Each test case is represented by two lines: string $$$s$$$, which has an odd length from $$$1$$$ to $$$49$$$ inclusive and consists of lowercase letters of the Latin alphabet; is a string containing one letter $$$c$$$, where $$$c$$$ is a lowercase letter of the Latin alphabet. ", "output_spec": "For each test case in a separate line output: YES, if the string $$$s$$$ can be converted so that $$$s=c$$$ is true; NO otherwise. You can output YES and NO in any case (for example, the strings yEs, yes, Yes and YES will be recognized as a positive response).", "sample_inputs": ["5\n\nabcde\n\nc\n\nabcde\n\nb\n\nx\n\ny\n\naaaaaaaaaaaaaaa\n\na\n\ncontest\n\nt"], "sample_outputs": ["YES\nNO\nNO\nYES\nYES"], "notes": "NoteIn the first test case, $$$s$$$=\"abcde\". You need to get $$$s$$$=\"c\". For the first operation, delete the first two letters, we get $$$s$$$=\"cde\". In the second operation, we delete the last two letters, so we get the expected value of $$$s$$$=\"c\".In the third test case, $$$s$$$=\"x\", it is required to get $$$s$$$=\"y\". Obviously, this cannot be done."}, "positive_code": [{"source_code": "cases = STDIN.gets.chomp.to_i\r\ncases.times.each do \r\n input_string = STDIN.gets.chomp\r\n input_query = STDIN.gets.chomp\r\n puts input_string.length.times.find_all { |i| input_string[i,input_query.length] == input_query && i%2 ==0 }.length>0 ? \"YES\" : \"NO\"\r\n end\r\n"}, {"source_code": "cases = STDIN.gets.chomp.to_i\ncases.times.each do \n input_string = STDIN.gets.chomp\n input_query = STDIN.gets.chomp\n puts input_string.length.times.find_all { |i| input_string[i,input_query.length] == input_query }.find_all { |i| i%2==0 }.length>0 ? \"YES\" : \"NO\"\n end"}, {"source_code": "n=gets().chomp.to_i\r\nfor i in 1..n do\r\n s=gets().chomp\r\n n=gets().chomp\r\n c=0\r\n for i in 0...s.length do\r\n if (i+1)%2!=0 && s[i]==n\r\n puts \"YES\"\r\n c+=1\r\n break\r\n end\r\n end\r\n if c==0\r\n puts \"NO\"\r\n end\r\nend"}, {"source_code": "t = gets.to_i\r\nt.times {\r\n s = gets.strip\r\n\r\n c = gets[0]\r\n a = (0...s.length).find_all { |i| s[i] == c }\r\n found = false\r\n a.each { |i|\r\n found ||= (i % 2 == 0 && (s.length - i - 1) % 2 == 0)\r\n #puts(\"#{i} #{i % 2 == 0} #{(s.length - i - 1) % 2 == 0} #{(s.length - i - 1)}\")\r\n }\r\n puts(found ? 'yes' : 'no')\r\n}\r\n"}], "negative_code": [{"source_code": "n=gets().chomp.to_i\r\nfor i in 1..n do\r\n s=gets().chomp\r\n n=gets().chomp\r\n i=0\r\n for i in 0...s.length do\r\n if (i+1)%2!=0 && s[i]==n\r\n puts \"YES\"\r\n break\r\n end\r\n end\r\n if i==s.length\r\n puts \"NO\"\r\n end\r\nend"}], "src_uid": "c569b47cf80dfa98a7105e246c3c1e01"} {"nl": {"description": "To satisfy his love of matching socks, Phoenix has brought his $$$n$$$ socks ($$$n$$$ is even) to the sock store. Each of his socks has a color $$$c_i$$$ and is either a left sock or right sock. Phoenix can pay one dollar to the sock store to either: recolor a sock to any color $$$c'$$$ $$$(1 \\le c' \\le n)$$$ turn a left sock into a right sock turn a right sock into a left sock The sock store may perform each of these changes any number of times. Note that the color of a left sock doesn't change when it turns into a right sock, and vice versa. A matching pair of socks is a left and right sock with the same color. What is the minimum cost for Phoenix to make $$$n/2$$$ matching pairs? Each sock must be included in exactly one matching pair.", "input_spec": "The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. The first line of each test case contains three integers $$$n$$$, $$$l$$$, and $$$r$$$ ($$$2 \\le n \\le 2 \\cdot 10^5$$$; $$$n$$$ is even; $$$0 \\le l, r \\le n$$$; $$$l+r=n$$$) — the total number of socks, and the number of left and right socks, respectively. The next line contains $$$n$$$ integers $$$c_i$$$ ($$$1 \\le c_i \\le n$$$) — the colors of the socks. The first $$$l$$$ socks are left socks, while the next $$$r$$$ socks are right socks. It is guaranteed that the sum of $$$n$$$ across all the test cases will not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, print one integer — the minimum cost for Phoenix to make $$$n/2$$$ matching pairs. Each sock must be included in exactly one matching pair.", "sample_inputs": ["4\n6 3 3\n1 2 3 2 2 2\n6 2 4\n1 1 2 2 2 2\n6 5 1\n6 5 4 3 2 1\n4 0 4\n4 4 4 3"], "sample_outputs": ["2\n3\n5\n3"], "notes": "NoteIn the first test case, Phoenix can pay $$$2$$$ dollars to: recolor sock $$$1$$$ to color $$$2$$$ recolor sock $$$3$$$ to color $$$2$$$ There are now $$$3$$$ matching pairs. For example, pairs $$$(1, 4)$$$, $$$(2, 5)$$$, and $$$(3, 6)$$$ are matching.In the second test case, Phoenix can pay $$$3$$$ dollars to: turn sock $$$6$$$ from a right sock to a left sock recolor sock $$$3$$$ to color $$$1$$$ recolor sock $$$4$$$ to color $$$1$$$ There are now $$$3$$$ matching pairs. For example, pairs $$$(1, 3)$$$, $$$(2, 4)$$$, and $$$(5, 6)$$$ are matching."}, "positive_code": [{"source_code": "t = gets.to_s.to_i\n1.upto(t) do |casenum|\n n, l, r = gets.to_s.split.map {|x| x.to_i }\n a = gets.to_s.split.map {|x| x.to_i - 1 }\n\n c = {}\n a.each_with_index do |val, i|\n c[val] = [0, 0] unless c.has_key?(val)\n if i < l\n c[val][0] += 1\n else\n c[val][1] += 1\n end\n end\n\n leftn = 0\n lefto = 0\n left = {}\n rightn = 0\n righto = 0\n right = {}\n\n c.each do |k, v|\n if v[0] > v[1]\n left[k] = v[0] - v[1]\n leftn += v[0] - v[1]\n lefto += (v[0] - v[1]) % 2\n elsif v[1] > v[0]\n right[k] = v[1] - v[0]\n rightn += v[1] - v[0]\n righto += (v[1] - v[0]) % 2\n end\n end\n\n result = 0\n\n result += [lefto, righto].min\n lefto -= result\n righto -= result\n leftn -= result\n rightn -= result\n\n while lefto > 0 && rightn > 0\n step = [lefto, rightn].min\n lefto -= step\n leftn -= step\n rightn -= step\n result += step\n end\n\n while righto > 0 && leftn > 0\n step = [righto, leftn].min\n righto -= step\n leftn -= step\n rightn -= step\n result += step\n end\n\n printf(\"%d\\n\", result + lefto + righto + (leftn - lefto + rightn - righto) / 2)\nend\n"}], "negative_code": [], "src_uid": "a2d4f0182456cedbe85dff97ec0f477e"} {"nl": {"description": "One day Alice was cleaning up her basement when she noticed something very curious: an infinite set of wooden pieces! Each piece was made of five square tiles, with four tiles adjacent to the fifth center tile: By the pieces lay a large square wooden board. The board is divided into $$$n^2$$$ cells arranged into $$$n$$$ rows and $$$n$$$ columns. Some of the cells are already occupied by single tiles stuck to it. The remaining cells are free.Alice started wondering whether she could fill the board completely using the pieces she had found. Of course, each piece has to cover exactly five distinct cells of the board, no two pieces can overlap and every piece should fit in the board entirely, without some parts laying outside the board borders. The board however was too large for Alice to do the tiling by hand. Can you help determine if it's possible to fully tile the board?", "input_spec": "The first line of the input contains a single integer $$$n$$$ ($$$3 \\leq n \\leq 50$$$) — the size of the board. The following $$$n$$$ lines describe the board. The $$$i$$$-th line ($$$1 \\leq i \\leq n$$$) contains a single string of length $$$n$$$. Its $$$j$$$-th character ($$$1 \\leq j \\leq n$$$) is equal to \".\" if the cell in the $$$i$$$-th row and the $$$j$$$-th column is free; it is equal to \"#\" if it's occupied. You can assume that the board contains at least one free cell.", "output_spec": "Output YES if the board can be tiled by Alice's pieces, or NO otherwise. You can print each letter in any case (upper or lower).", "sample_inputs": ["3\n#.#\n...\n#.#", "4\n##.#\n#...\n####\n##.#", "5\n#.###\n....#\n#....\n###.#\n#####", "5\n#.###\n....#\n#....\n....#\n#..##"], "sample_outputs": ["YES", "NO", "YES", "NO"], "notes": "NoteThe following sketches show the example boards and their tilings if such tilings exist: "}, "positive_code": [{"source_code": "def tiles(n,x,y)\n if x == n-1 || x == 0 || y >= n-2\n return false\n else\n return [[x,y],[x-1,y+1],[x,y+1],[x+1,y+1],[x,y+2]]\n end\nend\n\ndef solve (n,board)\n n.times do |i|\n n.times do |j|\n if board[i][j] == \".\"\n #p [i,j]\n t = tiles(n,j,i)\n #p t\n return false if !t\n t.each do |x,y|\n if board[y][x] == \"#\"\n return false\n else\n board[y][x] = \"#\"\n end\n end\n end\n end\n end\n return true\nend\n\nn = gets.to_i\nboard = []\nn.times do\n board << gets.chomp\nend\nputs solve(n,board) ? \"YES\" : \"NO\""}, {"source_code": "N = gets.to_i\nboard = N.times.map { gets.chomp.chars }\n\nN.times do |y|\n N.times do |x|\n next if board[y][x] == '#'\n\n ay, ax = y, x\n by, bx = y + 1, x - 1\n cy, cx = y + 1, x\n dy, dx = y + 1, x + 1\n ey, ex = y + 2, x\n\n max_y = [ay, by, cy, dy, ey].max\n min_x, max_x = [ax, bx, cx, dx, ex].minmax\n\n if max_y >= N || min_x < 0 || N <= max_x\n puts 'NO'\n exit\n end\n\n [ay, by, cy, dy, ey].zip([ax, bx, cx, dx, ex]).each do |ny, nx|\n if board[ny][nx] == '#'\n puts 'NO'\n exit\n end\n\n board[ny][nx] = '#'\n end\n end\nend\n\nputs 'YES'\n"}, {"source_code": "n = gets.to_i\ns = n.times.map{gets.chomp}\nd = [0,1,0,-1]\nfor i in 1..n-2\n for j in 1..n-2\n if s[i][j] == \".\"\n f = true\n 4.times do |k|\n f = false if s[i+d[k]][j+d[k^1]] == \"#\"\n end\n if f\n 4.times do |k|\n s[i+d[k]][j+d[k^1]] = \"#\"\n end\n s[i][j] = \"#\"\n end\n end\n end\nend\nif s.join(\"\").count(\".\") > 0\n puts \"NO\"\nelse\n puts \"YES\"\nend"}], "negative_code": [], "src_uid": "cc1feee94617c0cba1c528a0cb3952a8"} {"nl": {"description": "Little Mishka is a great traveller and she visited many countries. After thinking about where to travel this time, she chose XXX — beautiful, but little-known northern country.Here are some interesting facts about XXX: XXX consists of n cities, k of whose (just imagine!) are capital cities. All of cities in the country are beautiful, but each is beautiful in its own way. Beauty value of i-th city equals to ci. All the cities are consecutively connected by the roads, including 1-st and n-th city, forming a cyclic route 1 — 2 — ... — n — 1. Formally, for every 1 ≤ i < n there is a road between i-th and i + 1-th city, and another one between 1-st and n-th city. Each capital city is connected with each other city directly by the roads. Formally, if city x is a capital city, then for every 1 ≤ i ≤ n,  i ≠ x, there is a road between cities x and i. There is at most one road between any two cities. Price of passing a road directly depends on beauty values of cities it connects. Thus if there is a road between cities i and j, price of passing it equals ci·cj.Mishka started to gather her things for a trip, but didn't still decide which route to follow and thus she asked you to help her determine summary price of passing each of the roads in XXX. Formally, for every pair of cities a and b (a < b), such that there is a road between a and b you are to find sum of products ca·cb. Will you help her?", "input_spec": "The first line of the input contains two integers n and k (3 ≤ n ≤ 100 000, 1 ≤ k ≤ n) — the number of cities in XXX and the number of capital cities among them. The second line of the input contains n integers c1, c2, ..., cn (1 ≤ ci ≤ 10 000) — beauty values of the cities. The third line of the input contains k distinct integers id1, id2, ..., idk (1 ≤ idi ≤ n) — indices of capital cities. Indices are given in ascending order.", "output_spec": "Print the only integer — summary price of passing each of the roads in XXX.", "sample_inputs": ["4 1\n2 3 1 2\n3", "5 2\n3 5 2 2 4\n1 4"], "sample_outputs": ["17", "71"], "notes": "NoteThis image describes first sample case:It is easy to see that summary price is equal to 17.This image describes second sample case:It is easy to see that summary price is equal to 71."}, "positive_code": [{"source_code": "n, k = gets.split.map { |x| x.to_i }\nc = gets.split.map { |x| x.to_i }\nid = gets.split.map { |x| x.to_i }\n\ncap = Array.new n\nid.each do |x|\n\tcap[x - 1] = true\nend\n\nsc = 0\nsc2 = 0\nn.times do |i|\n\tif cap[i]\n\t\tsc += c[i]\n\t\tsc2 += c[i] ** 2\n\tend\nend\n\nsum = 0\nans = (sc ** 2 - sc2) / 2\nn.times do |i|\n\tsum += c[i] if !cap[i]\nend\nn.times do |i|\n\tans += c[i] * sum if cap[i]\nend\n\nn.times do |i|\n\tif !cap[i - 1] and !cap[i]\n\t\tans += c[i - 1] * c[i]\n\tend\nend\n\nputs ans"}, {"source_code": "cityNum, capitalNum = gets.chomp.split(' ').map(&:to_i)\nprices = gets.chomp.split(' ').map(&:to_i)\ncapitals = gets.chomp.split(' ').map(&:to_i)\nisCapital = Array.new(cityNum) { |i| i = false }\n#isMarked = Array.new(cityNum) { |i| i = false }\n\nrawSum = 0\nrawCapSum = 0\n\nprices.each do |p|\n\trawSum += p\nend\n\ncapitals.each do |c|\n\trawCapSum += prices[c - 1]\n\tisCapital[c - 1] = true\nend\n\ntotalSum = 0\n\nfor i in 0...(prices.length)\n\ttotalSum += (prices[i] * prices[(i + 1) % cityNum])\nend\n\n\n#puts \"Total : #{totalSum}\"\n\ncapitals.each do |c|\n\tpartialSum = rawSum - prices[c - 1]\n\t#puts \"Partial : #{partialSum}\"\n\ttotalSum += partialSum * prices[c - 1]\n\t#puts \"c : #{c}, ts = #{totalSum}\"\nend\n\n#puts \"Total : #{totalSum}\"\n\n\tcount = 1\n\tcapitals.each do |c|\n\t\ttotalSum -= prices[c - 1] * (prices[c % cityNum] + prices[if c - 2 >= 0 then c - 2 else cityNum - 1 end])\n\t\tbreak if count == capitalNum\n\n\t\tpartialSum = rawCapSum - prices[c - 1]\n\n\t\tif isCapital[c % cityNum]\n\t\t\tpartialSum -= prices[c % cityNum]\n\t\tend\n\n\n\t\tif c == 1 && isCapital[if c - 2 >= 0 then c - 2 else cityNum - 1 end]\n\t\t\tpartialSum -= prices[if c - 2 >= 0 then c - 2 else cityNum - 1 end]\n\t\tend\n\n\t\t#puts \"c : #{c}\"\n\t\t#puts \"raw : #{rawCapSum}\"\n\t\t#puts \"partial : #{partialSum}\"\n\n\t\ttotalSum -= prices[c - 1] * partialSum\n\t\trawCapSum -= prices[c - 1]\n\t\tcount += 1\n\tend\n\nputs totalSum"}], "negative_code": [], "src_uid": "bc6b8fda79c257e6c4e280d7929ed8a1"} {"nl": {"description": "A new cottage village called «Flatville» is being built in Flatland. By now they have already built in «Flatville» n square houses with the centres on the Оx-axis. The houses' sides are parallel to the coordinate axes. It's known that no two houses overlap, but they can touch each other.The architect bureau, where Peter works, was commissioned to build a new house in «Flatville». The customer wants his future house to be on the Оx-axis, to be square in shape, have a side t, and touch at least one of the already built houses. For sure, its sides should be parallel to the coordinate axes, its centre should be on the Ox-axis and it shouldn't overlap any of the houses in the village.Peter was given a list of all the houses in «Flatville». Would you help him find the amount of possible positions of the new house?", "input_spec": "The first line of the input data contains numbers n and t (1 ≤ n, t ≤ 1000). Then there follow n lines, each of them contains two space-separated integer numbers: xi ai, where xi — x-coordinate of the centre of the i-th house, and ai — length of its side ( - 1000 ≤ xi ≤ 1000, 1 ≤ ai ≤ 1000).", "output_spec": "Output the amount of possible positions of the new house.", "sample_inputs": ["2 2\n0 4\n6 2", "2 2\n0 4\n5 2", "2 3\n0 4\n5 2"], "sample_outputs": ["4", "3", "2"], "notes": "NoteIt is possible for the x-coordinate of the new house to have non-integer value."}, "positive_code": [{"source_code": "# Problem A\n# by vi002\n\nn, t = gets.split.map { |v| v.to_i }\ndata = Array.new(n) { gets.split.map { |v| v.to_i } }.sort\n\nresult = 2\n(0 ... n - 1).each do |pos|\n distance = 2 * (data[pos + 1].first - data[pos].first)\n distance -= data[pos][1] + data[pos + 1][1]\n result += 2 if distance > 2 * t\n result += 1 if distance == 2 * t\nend\n\nputs result\n"}, {"source_code": "s,n,t = 2,gets(' ').to_i,gets.to_i*2\n([0]*n).map{[gets(' ').to_i,gets.to_i]}.sort_by{|i|i[0]}.\ninject{|i,j|r=j[0]*2-i[0]*2-i[1]-j[1];s+=(r>t ? 2:r==t ? 1:0);j}\np s"}, {"source_code": "require 'set'\ndef check(x,t)\n ok=false\n $m.each{|h|\n if x+t<=h[0] || h[0]+h[1]<=x then\n else\n return false\n end\n }\n return true\nend\nn,t=gets.split.map(&:to_i)\nt*=2\n$m=[]\nn.times{|i|\n $m.push(gets.split.map(&:to_i))\n $m[i][0]*=2\n $m[i][0]-=$m[i][1]\n $m[i][1]*=2\n}\n\nq=Set.new\n$m.each{|h|\n if(check(h[0]-t,t)) then\n q.add(h[0]-t)\n end\n if(check(h[0]+h[1],t)) then\n q.add(h[0]+h[1])\n end\n}\nputs q.length\n"}, {"source_code": "n,t=gets.split.map &:to_i;y=n+1;(1..n).map{gets.split.map &:to_i}.sort.inject{|i,j|y+=j[0]*2-j[1]-i[0]*2-i[1]<=>t+t;j};p y\n"}, {"source_code": "n,t=gets.split.map &:to_i;y=n+1;(1..n).map{gets.split.map &:to_i}.sort.inject{|i,j|y+=j[0]*2-j[1]-i[0]*2-i[1]<=>t+t;j};p y\n"}, {"source_code": "n,t=gets.split.map &:to_i;s=[];1.upto(n){s<< gets.split.map(&:to_i)};s.sort!;y=2;x,a=s.shift;s.each{|i|s=x+x+a;x,a=i;y+=(x+x-a-s<=>t+t)+1};puts y"}, {"source_code": "n,t=gets.split.map &:to_i;s=[];1.upto(n){s<< gets.split.map(&:to_i)};s.sort!;y=2;x,a=s.shift;s.each{|i|s=x+x+a;x,a=i;s=x+x-a-s;y+=(s<=>t+t)+1};puts y"}, {"source_code": "n,t=gets.split.map &:to_i;y=n+1;(1..n).map{gets.split.map &:to_i}.sort.inject{|i,j|y+=j[0]*2-j[1]-i[0]*2-i[1]<=>t+t;j};p y"}, {"source_code": "n,t=gets.split.map &:to_i;s=[];1.upto(n){s<< gets.split.map(&:to_i)};y=2;x,a=s.sort!.shift;s.each{|i|s=x+x+a;x,a=i;y+=(x+x-a-s<=>t+t)+1};puts y"}, {"source_code": "n,t=gets.split.map &:to_i;y=n+1;(1..n).map{gets.split.map &:to_i}.sort.inject{|i,j|y+=j[0]*2-j[1]-i[0]*2-i[1]<=>t+t;j};p y"}, {"source_code": "n,t=gets.split.map &:to_i;y=n+1;(1..n).map{gets.split.map &:to_i}.sort.inject{|i,j|y+=j[0]*2-j[1]-i[0]*2-i[1]<=>t+t;j};p y"}, {"source_code": "n,t=gets.split.map &:to_i;y=n+1;(1..n).map{gets.split.map &:to_i}.sort.inject{|i,j|y+=j[0]*2-j[1]-i[0]*2-i[1]<=>t+t;j};p y\n"}, {"source_code": "n,t=gets.split.map &:to_i;y=n+1;(1..n).map{gets.split.map &:to_i}.sort.inject{|i,j|y+=j[0]*2-j[1]-i[0]*2-i[1]<=>t+t;j};p y\n"}, {"source_code": "##\n# 15A\n# Author: Sammy Teahan\n#\n\nif __FILE__ == $0\n\n n, t = gets.split.map(&:to_i)\n count = 2\n houses = []\n\n class House\n attr_reader :left, :right\n\n def initialize(x, a)\n @left = x - a / 2\n @right = x + a / 2\n end\n\n end\n\n n.times do |k|\n x, a = gets.split.map(&:to_f)\n house = House.new(x, a)\n houses << house\n end\n\n houses.sort_by! do |house|\n house.left\n end\n\n (houses.size - 1).times do |house|\n if houses[house].right + t < houses[house + 1].left\n count += 2\n elsif\n houses[house].right + t == houses[house + 1].left\n count += 1\n end\n end\n\n puts count\nend\n"}, {"source_code": "##\n# 15A\n# Author: Sammy Teahan\n#\n\nif __FILE__ == $0\n\n n, t = gets.split.map(&:to_i)\n count = 2\n houses = []\n\n class House\n attr_reader :left, :right\n\n def initialize(x, a)\n @left = (x - a / 2)\n @right = (x + a / 2)\n end\n\n end\n\n n.times do |k|\n x, a = gets.split.map(&:to_f)\n house = House.new(x, a)\n houses << house\n end\n\n # Sort our houses\n houses.sort_by! do |house|\n house.left\n end\n\n (houses.size - 1).times do |house|\n if houses[house].right + t < houses[house + 1].left\n count += 2\n elsif\n houses[house].right + t == houses[house + 1].left\n count += 1\n end\n end\n\n puts count\nend\n"}, {"source_code": "n,t=gets.split.map &:to_i;y=n+1;(1..n).map{gets.split.map &:to_i}.sort.inject{|i,j|y+=j[0]*2-j[1]-i[0]*2-i[1]<=>t+t;j};p y\n"}, {"source_code": "n,t=gets.split.map(&:to_i);\ny=n+1;\na = (1..n).map{gets.split.map(&:to_i)}\na = a.sort\na.inject{|i,j|y+=j[0]*2-j[1]-i[0]*2-i[1]<=>2*t;j;}\np y"}, {"source_code": "n,t=gets.split.map &:to_i;y=n+1;(1..n).map{gets.split.map &:to_i}.sort.inject{|i,j|y+=j[0]*2-j[1]-i[0]*2-i[1]<=>t+t;j};p y"}, {"source_code": "n, t = gets.split.map(&:to_i)\nt = t.to_f\nkoord = Array.new\nn.times { koord << gets.split.map(&:to_f) }\nkoord.sort!\nret = 2\n(1...(n)).each do |i|\n x_i_1 = koord[i-1][0] + koord[i-1][1] / 2\n x_i_2 = koord[i][0] - koord[i][1].abs / 2\n diff = (x_i_1 - x_i_2).abs\n if diff == t\n ret += 1\n elsif diff > t\n ret += 2\n end\nend\nputs ret\n"}, {"source_code": "n,t=gets.split.map &:to_i;y=n+1;(1..n).map{gets.split.map &:to_i}.sort.inject{|i,j|y+=j[0]*2-j[1]-i[0]*2-i[1]<=>t+t;j};p y\n"}, {"source_code": "n,t=gets.split.map &:to_i;y=n+1;(1..n).map{gets.split.map &:to_i}.sort.inject{|i,j|y+=j[0]*2-j[1]-i[0]*2-i[1]<=>t+t;j};p y\n"}, {"source_code": "n,t=gets.split.map(&:to_i)\nbuilt=[]\nn.times{\n\tx,a=gets.split.map(&:to_i)\n\tbuilt<<[2*x-a,2*x+a]\n}\namount=2 #left and right most already possible\nbuilt.sort{|a,b|a[0]<=>b[0]}.each_cons(2){|l,r|amount+=r[0]-l[1]<=>2*t}\nputs amount+n-1"}, {"source_code": "n,t=gets.split.map &:to_i;y=n+1;(1..n).map{gets.split.map &:to_i}.sort.inject{|i,j|y+=j[0]*2-j[1]-i[0]*2-i[1]<=>t+t;j};p y\n"}], "negative_code": [{"source_code": "n,t,m = gets(' ').to_i,gets.to_i,[0]*2000\nn.times do\n x,a = gets(' ').to_i,gets.to_i\n for i in [-1000,x-a/2].max...[x+a/2,1000].min\n m[i] = 1\n end\nend\nss = s = m[-1000,t].inject(&:+)\ny = (s==0 ? 0 : 1)\ndd = (s+ss==1 ? 1 : 0)\nfor i in (-1000+t)..999\n s += m[i]-m[i-t]\n y += (((d = (s+ss==1 ? 1 : 0)))==1 && dd==0 ? 1 : 0)\n dd,ss = d,s\nend\np y+(s==0 ? 0 : 1)"}, {"source_code": "s,n,t = 2,gets(' ').to_i,gets.to_i\n([0]*n).map{[gets(' ').to_i,gets.to_i]}.sort_by{|i|i[0]}.\ninject{|i,j|r=j[0]-i[0]-(i[1]+j[1])/2;s+=(r>t ? 2:r==t ? 1:0);j}\np s"}, {"source_code": "n,t,x,a=(gets+gets).split.map &:to_i;y=2;2.upto(n){s=x+x+a;x,a=gets.split.map &:to_i;s=x+x-a-s;y+=(s<=>t+t)+1};puts y"}, {"source_code": "n,t=gets.split.map(&:to_i)\nbuilt=[]\nn.times{\n\tx,a=gets.split.map(&:to_i)\n\tbuilt<<[x-a/2,x+a/2]\n}\namount=2 #left and right most already possible\nbuilt.each_cons(2){|(l,r)|\n\tamount+=(r[0]-l[1]<=>t)+1\n}\nputs amount\n"}, {"source_code": "n,t=gets.split.map(&:to_i)\nbuilt=[]\nn.times{\n\tx,a=gets.split.map(&:to_i)\n\tbuilt<<[x-a/2,x+a/2]\n}\namount=2 #left and right most already possible\nbuilt.sort{|a,b|a[0]<=>b[0]}.each_cons(2){|(l,r)|amount+=r[0]-l[1]<=>t}\nputs amount+n-1\n"}], "src_uid": "c31fed523230af1f904218b2fe0d663d"} {"nl": {"description": "A new entertainment has appeared in Buryatia — a mathematical circus! The magician shows two numbers to the audience — $$$n$$$ and $$$k$$$, where $$$n$$$ is even. Next, he takes all the integers from $$$1$$$ to $$$n$$$, and splits them all into pairs $$$(a, b)$$$ (each integer must be in exactly one pair) so that for each pair the integer $$$(a + k) \\cdot b$$$ is divisible by $$$4$$$ (note that the order of the numbers in the pair matters), or reports that, unfortunately for viewers, such a split is impossible.Burenka really likes such performances, so she asked her friend Tonya to be a magician, and also gave him the numbers $$$n$$$ and $$$k$$$.Tonya is a wolf, and as you know, wolves do not perform in the circus, even in a mathematical one. Therefore, he asks you to help him. Let him know if a suitable splitting into pairs is possible, and if possible, then tell it.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$) — the number of test cases. The following is a description of the input data sets. The single line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \\leq n \\leq 2 \\cdot 10^5$$$, $$$0 \\leq k \\leq 10^9$$$, $$$n$$$ is even) — the number of integers and the number being added $$$k$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, first output the string \"YES\" if there is a split into pairs, and \"NO\" if there is none. If there is a split, then in the following $$$\\frac{n}{2}$$$ lines output pairs of the split, in each line print $$$2$$$ numbers — first the integer $$$a$$$, then the integer $$$b$$$.", "sample_inputs": ["4\n\n4 1\n\n2 0\n\n12 10\n\n14 11"], "sample_outputs": ["YES\n1 2\n3 4\nNO\nYES\n3 4\n7 8\n11 12\n2 1\n6 5\n10 9\nYES\n1 2\n3 4\n5 6\n7 8\n9 10\n11 12\n13 14"], "notes": "NoteIn the first test case, splitting into pairs $$$(1, 2)$$$ and $$$(3, 4)$$$ is suitable, same as splitting into $$$(1, 4)$$$ and $$$(3, 2)$$$.In the second test case, $$$(1 + 0) \\cdot 2 = 1 \\cdot (2 + 0) = 2$$$ is not divisible by $$$4$$$, so there is no partition."}, "positive_code": [{"source_code": "def main\n t = inp.to_i\n t.times do\n n, k = mapint(inarr)\n if k % 4 == 0\n puts \"NO\"\n next\n end\n puts \"YES\"\n ans = \"\"\n\n r = Array.new(n>>1)\n\n pos = 0\n i = 1\n while i <= n\n if ((i + k) * (i + 1) & 3) == 0\n r[pos] = \"#{i} #{i+1}\"\n else\n r[pos] = \"#{i + 1} #{i}\"\n end\n pos+=1\n i+=2\n end\n\n puts r\n\n end\n\nend\n\n# lib\ndef inp\n gets.chomp\nend\n\ndef inarr\n gets.chomp.split\nend\n\ndef mapint(lst)\n lst.map(&:to_i)\nend\n\ndef max(a, b)\n (a > b)? a:b\nend\n\ndef min(a, b)\n (a < b)? a:b\nend\n\nmain"}, {"source_code": "gets.to_i.times do\r\n n, k = gets.split.map(&:to_i)\r\n abs = if k % 2 == 1\r\n (1..n).step(2).map do |i|\r\n [i, i + 1]\r\n end\r\n else\r\n odds = (1..n).step(2).to_a\r\n (4..n).step(4).map do |b|\r\n [odds.pop, b]\r\n end + (2..n).step(4).map do |a|\r\n [a, odds.pop]\r\n end\r\n end\r\n if abs.all? { |a, b| (a + k) * b % 4 == 0 }\r\n puts \"YES\"\r\n abs.each do |a, b|\r\n puts \"#{a} #{b}\"\r\n end\r\n else\r\n puts \"NO\"\r\n end\r\nend\r\n"}], "negative_code": [{"source_code": "def main\n t = inp.to_i\n t.times do\n n, k = mapint(inarr)\n if k == 0\n puts \"NO\"\n next\n end\n puts \"YES\"\n ans = \"\"\n\n\n 1.step(n, 2) do |i|\n if ((i + k) * (i + 1) & 3) == 0\n ans += \"#{i} #{i+1}\\n\"\n else\n ans += \"#{i + 1} #{i}\\n\"\n end\n\n end\n\n puts ans\n\n end\nend\n\n# lib\ndef inp\n gets.chomp\nend\n\ndef inarr\n gets.chomp.split\nend\n\ndef mapint(lst)\n lst.map(&:to_i)\nend\n\ndef max(a, b)\n (a > b)? a:b\nend\n\ndef min(a, b)\n (a < b)? a:b\nend\n\nmain"}, {"source_code": "def main\n t = inp.to_i\n t.times do\n n, k = mapint(inarr)\n if k == 0\n puts \"NO\"\n next\n end\n puts \"YES\"\n ans = \"\"\n if k % 2 == 0\n 1.step(n, 2) do |i|\n ans += \"#{i + 1} #{i}\\n\"\n end\n else\n 1.step(n, 2) do |i|\n ans += \"#{i} #{i+1}\\n\"\n end\n end\n puts ans\n\n end\nend\n\n# lib\ndef inp\n gets.chomp\nend\n\ndef inarr\n gets.chomp.split\nend\n\ndef mapint(lst)\n lst.map(&:to_i)\nend\n\ndef max(a, b)\n (a > b)? a:b\nend\n\ndef min(a, b)\n (a < b)? a:b\nend\n\nmain"}, {"source_code": "gets.to_i.times do\r\n n, k = gets.split.map(&:to_i)\r\n if k == 0\r\n puts \"NO\"\r\n elsif k % 2 == 1\r\n puts \"YES\"\r\n (1..n).step(2).each do |i|\r\n puts \"#{i} #{i + 1}\"\r\n end\r\n else\r\n puts \"YES\"\r\n (1..n).step(2).each do |i|\r\n if (i + k) % 4 == 1\r\n puts \"#{i} #{i + 1}\"\r\n else\r\n puts \"#{i + 1} #{i}\"\r\n end\r\n end\r\n end\r\nend\r\n"}], "src_uid": "d4fc7e683f389e0c7bbee8e115cef459"} {"nl": {"description": "You are given two strings $$$s$$$ and $$$t$$$. The string $$$s$$$ consists of lowercase Latin letters and at most one wildcard character '*', the string $$$t$$$ consists only of lowercase Latin letters. The length of the string $$$s$$$ equals $$$n$$$, the length of the string $$$t$$$ equals $$$m$$$.The wildcard character '*' in the string $$$s$$$ (if any) can be replaced with an arbitrary sequence (possibly empty) of lowercase Latin letters. No other character of $$$s$$$ can be replaced with anything. If it is possible to replace a wildcard character '*' in $$$s$$$ to obtain a string $$$t$$$, then the string $$$t$$$ matches the pattern $$$s$$$.For example, if $$$s=$$$\"aba*aba\" then the following strings match it \"abaaba\", \"abacaba\" and \"abazzzaba\", but the following strings do not match: \"ababa\", \"abcaaba\", \"codeforces\", \"aba1aba\", \"aba?aba\".If the given string $$$t$$$ matches the given string $$$s$$$, print \"YES\", otherwise print \"NO\".", "input_spec": "The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n, m \\le 2 \\cdot 10^5$$$) — the length of the string $$$s$$$ and the length of the string $$$t$$$, respectively. The second line contains string $$$s$$$ of length $$$n$$$, which consists of lowercase Latin letters and at most one wildcard character '*'. The third line contains string $$$t$$$ of length $$$m$$$, which consists only of lowercase Latin letters.", "output_spec": "Print \"YES\" (without quotes), if you can obtain the string $$$t$$$ from the string $$$s$$$. Otherwise print \"NO\" (without quotes).", "sample_inputs": ["6 10\ncode*s\ncodeforces", "6 5\nvk*cup\nvkcup", "1 1\nv\nk", "9 6\ngfgf*gfgf\ngfgfgf"], "sample_outputs": ["YES", "YES", "NO", "NO"], "notes": "NoteIn the first example a wildcard character '*' can be replaced with a string \"force\". So the string $$$s$$$ after this replacement is \"codeforces\" and the answer is \"YES\".In the second example a wildcard character '*' can be replaced with an empty string. So the string $$$s$$$ after this replacement is \"vkcup\" and the answer is \"YES\".There is no wildcard character '*' in the third example and the strings \"v\" and \"k\" are different so the answer is \"NO\".In the fourth example there is no such replacement of a wildcard character '*' that you can obtain the string $$$t$$$ so the answer is \"NO\"."}, "positive_code": [{"source_code": "_,a,b=$<.map &:chomp\nif !a.index('*')\n\tputs a==b ? :Yes : :No\nelse\n\tx,y=a.split('*')\n\tx||='';y||=''\n\tputs b.start_with?(x) && b.end_with?(y) && b.size>=x.size+y.size ? :Yes : :No\nend"}, {"source_code": "N, M = gets.split.map(&:to_i)\ns = '!' + gets.chomp + '!'\nt = '!' + gets.chomp + '!'\nif s.include?('*')\n s1, s2 = s.split('*')\n if s.length-1 <= t.length && s1 == t[0, s1.length] && s2 == t[t.length-s2.length, s2.length]\n puts 'YES'\n else\n puts 'NO'\n end\nelse\n if s == t\n puts 'YES'\n else\n puts 'NO'\n end\nend"}, {"source_code": "sl,tl=gets.strip.split.map(&:to_i)\nml = (sl=0 && j>=0 && s[i]==t[j] do\n i -= 1\n j -= 1\n end\n if i>=0 && j>=0 && s[i]!='*'\n puts \"NO\"\n exit 0\n end\n puts \"YES\"\nend"}, {"source_code": "n,m = gets.chomp.split(\" \").map(&:to_i)\ns,t = gets.chomp,gets.chomp\nans = true\nif s.include?(\"*\")\n if s[-1] == \"*\" && s.delete(\"*\") != t[0,s.size-1]\n ans = false\n elsif s[-1] != \"*\"\n a,b = s.split(\"*\")\n if a != t[0,a.size] || b != t[m-b.size..-1] || m < n-1\n ans = false\n end\n end\nelsif s != t\n ans = false\nend\nputs ans ? \"YES\" : \"NO\"\n"}, {"source_code": "#! /usr/bin/env ruby\n\nn, m = gets.split.map(&:to_i)\ns = gets.chomp\nt = gets.chomp\n\nif s.include?(\"*\")\n a, b = s.split(\"*\")\n if a == nil and b == nil\n puts \"YES\"\n elsif a == nil\n puts (t.end_with?(b) ? \"YES\" : \"NO\")\n elsif b == nil\n puts (t.start_with?(a) ? \"YES\" : \"NO\")\n else\n if t.start_with?(a) and t.end_with?(b)\n if a.size + b.size <= m\n puts \"YES\"\n else\n puts \"NO\"\n end\n else\n puts \"NO\"\n end\n end\nelse\n puts (s == t ? \"YES\" : \"NO\")\nend\n"}], "negative_code": [{"source_code": "gets;puts /#{gets.chomp.gsub('*','.*')}/=~gets.chomp ? :Yes : :No"}, {"source_code": "n,m = gets.chomp.split(\" \").map(&:to_i)\ns,t = gets.chomp,gets.chomp\nfirst,second = true,true if s.include?(\"*\")\nif s[0] != \"*\" && first\n a = s.split(\"*\")[0]\n i = t.index(a)\n if i\n t.slice!(0,i+a.size)\n else\n first = false\n end\nend\nif s[-1] != \"*\" && second\n b = s.split(\"*\")[1]\n i = t.rindex(b)\n if i\n t.slice!(0,i+b.size)\n if t.size != 0\n second = false\n end\n else\n second = false\n end\nend\nputs (first && second) || s == t ? \"YES\" : \"NO\"\n"}, {"source_code": "n,m = gets.chomp.split(\" \").map(&:to_i)\ns,t = gets.chomp.split(\"*\"),gets.chomp\nans = true\nfor i in 0..s.size-1\n j = t.index(s[i],0)\n if j\n t.slice!(0,j+s[i].size)\n else\n ans = false\n end\nend\nputs ans ? \"YES\" : \"NO\"\n"}, {"source_code": "n,m = gets.chomp.split(\" \").map(&:to_i)\ns,t = gets.chomp.split(\"*\"),gets.chomp\nans = true\np s,t\nfor i in 0..s.size-1\n j = t.index(s[i],0)\n if j\n t.slice!(0,j+s[i].size)\n else\n ans = false\n end\n p t\nend\nputs (ans&&t.size==1) ? \"YES\" : \"NO\"\n"}, {"source_code": "n,m = gets.chomp.split(\" \").map(&:to_i)\ns,t = gets.chomp.split(\"*\"),gets.chomp\nans = true\nfor i in 0..s.size-1\n j = t.index(s[i],0)\n if j\n t.slice!(0,j+s[i].size)\n else\n ans = false\n end\nend\nputs (ans&&t.size==0) ? \"YES\" : \"NO\"\n"}, {"source_code": "n,m = gets.chomp.split(\" \").map(&:to_i)\ns,t = gets.chomp.split(\"*\"),gets.chomp\nans = true\nfor i in 0..s.size-1\n j = t.index(s[i],0)\n if j\n t.slice!(0,j+s[i].size)\n else\n ans = false\n end\nend\nputs (ans && (t.size == 0 || s.size == 1)) ? \"YES\" : \"NO\"\n"}, {"source_code": "n,m = gets.chomp.split(\" \").map(&:to_i)\ns,t = gets.chomp.split(\"*\"),gets.chomp\nans = true\nfor i in 0..s.size-1\n j = t.index(s[i],0)\n if j\n t.slice!(0,j+s[i].size)\n else\n ans = false\n end\nend\nputs (ans && (t.size == 0 || s.size <= 1)) ? \"YES\" : \"NO\"\n"}, {"source_code": "n,m = gets.chomp.split(\" \").map(&:to_i)\ns,t = gets.chomp.split(\"*\"),gets.chomp\nans = true\np s,t\nfor i in 0..s.size-1\n j = t.index(s[i],0)\n if j\n t.slice!(0,j+s[i].size)\n else\n ans = false\n end\n p t\nend\nputs (ans&&t.size==0) ? \"YES\" : \"NO\"\n"}, {"source_code": "#! /usr/bin/env ruby\n\nn, m = gets.split.map(&:to_i)\ns = gets.chomp\nt = gets.chomp\n\nif s.include?(\"*\")\n a, b = s.split(\"*\")\n if a == nil and b == nil\n puts \"YES\"\n elsif a == nil\n puts (t.include?(b) ? \"YES\" : \"NO\")\n elsif b == nil\n puts (t.include?(a) ? \"YES\" : \"NO\")\n else\n li, ri = t.index(a), t.rindex(b)\n if li == nil or ri == nil\n puts \"NO\"\n else\n if li + a.size <= ri\n puts \"YES\"\n else\n puts \"NO\"\n end\n end\n end\nelse\n puts (s == t ? \"YES\" : \"NO\")\nend\n"}], "src_uid": "d629d09782a7af0acc359173ac4b4f0a"} {"nl": {"description": "Let's define a function $$$f(x)$$$ ($$$x$$$ is a positive integer) as follows: write all digits of the decimal representation of $$$x$$$ backwards, then get rid of the leading zeroes. For example, $$$f(321) = 123$$$, $$$f(120) = 21$$$, $$$f(1000000) = 1$$$, $$$f(111) = 111$$$.Let's define another function $$$g(x) = \\dfrac{x}{f(f(x))}$$$ ($$$x$$$ is a positive integer as well).Your task is the following: for the given positive integer $$$n$$$, calculate the number of different values of $$$g(x)$$$ among all numbers $$$x$$$ such that $$$1 \\le x \\le n$$$.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. Each test case consists of one line containing one integer $$$n$$$ ($$$1 \\le n < 10^{100}$$$). This integer is given without leading zeroes.", "output_spec": "For each test case, print one integer — the number of different values of the function $$$g(x)$$$, if $$$x$$$ can be any integer from $$$[1, n]$$$.", "sample_inputs": ["5\n4\n37\n998244353\n1000000007\n12345678901337426966631415"], "sample_outputs": ["1\n2\n9\n10\n26"], "notes": "NoteExplanations for the two first test cases of the example: if $$$n = 4$$$, then for every integer $$$x$$$ such that $$$1 \\le x \\le n$$$, $$$\\dfrac{x}{f(f(x))} = 1$$$; if $$$n = 37$$$, then for some integers $$$x$$$ such that $$$1 \\le x \\le n$$$, $$$\\dfrac{x}{f(f(x))} = 1$$$ (for example, if $$$x = 23$$$, $$$f(f(x)) = 23$$$,$$$\\dfrac{x}{f(f(x))} = 1$$$); and for other values of $$$x$$$, $$$\\dfrac{x}{f(f(x))} = 10$$$ (for example, if $$$x = 30$$$, $$$f(f(x)) = 3$$$, $$$\\dfrac{x}{f(f(x))} = 10$$$). So, there are two different values of $$$g(x)$$$. "}, "positive_code": [{"source_code": "t = gets.chomp.to_i\nt.times do\n n = gets.chomp.split(\"\")\n puts n.size\n\nend"}, {"source_code": "class CodeForces\n\n attr_reader :answer\n\n def initialize(test_cases)\n @test_cases = test_cases\n end\n\n\ndef answer\n \n@test_cases.to_i.times do |i|\n\n args = gets.chomp \n solve(args) \n\nend\nend\n\nprivate\n\ndef solve(args)\n\n # multiple assign here!\n # use the right amount of variables\n \n # a = args.split(\" \").map(&:to_i)\n\n\n # do work here\n answer = args.to_s.size\n\n\n\n\n\n\n puts answer\nend\nend\n\n\n\nCodeForces.new(gets.chomp).answer\n"}], "negative_code": [], "src_uid": "ea011f93837fdf985f7eaa7a43e22cc8"} {"nl": {"description": "$$$n$$$ heroes fight against each other in the Arena. Initially, the $$$i$$$-th hero has level $$$a_i$$$.Each minute, a fight between two different heroes occurs. These heroes can be chosen arbitrarily (it's even possible that it is the same two heroes that were fighting during the last minute).When two heroes of equal levels fight, nobody wins the fight. When two heroes of different levels fight, the one with the higher level wins, and his level increases by $$$1$$$.The winner of the tournament is the first hero that wins in at least $$$100^{500}$$$ fights (note that it's possible that the tournament lasts forever if no hero wins this number of fights, then there is no winner). A possible winner is a hero such that there exists a sequence of fights that this hero becomes the winner of the tournament.Calculate the number of possible winners among $$$n$$$ heroes.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 500$$$) — the number of test cases. Each test case consists of two lines. The first line contains one integer $$$n$$$ ($$$2 \\le n \\le 100$$$) — the number of heroes. The second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 100$$$), where $$$a_i$$$ is the initial level of the $$$i$$$-th hero.", "output_spec": "For each test case, print one integer — the number of possible winners among the given $$$n$$$ heroes.", "sample_inputs": ["3\n3\n3 2 2\n2\n5 5\n4\n1 3 3 7"], "sample_outputs": ["1\n0\n3"], "notes": "NoteIn the first test case of the example, the only possible winner is the first hero.In the second test case of the example, each fight between the heroes results in nobody winning it, so the tournament lasts forever and there is no winner."}, "positive_code": [{"source_code": "alias g gets;g.to_i.times{g;a=g.split.map &:to_i;p a.size-a.count(a.min)}"}, {"source_code": "alias g gets;g.to_i.times{g;a=g.split.map &:to_i;puts a.size-a.count(a.min)}"}, {"source_code": "alias g gets\r\ng.to_i.times {\r\n g\r\n a = g.split.map &:to_i\r\n puts a.size - a.count(a.min)\r\n}"}, {"source_code": "def g \r\n gets\r\nend \r\ng.to_i.times {\r\n g\r\n a = g.split.map &:to_i\r\n puts a.size - a.count(a.min)\r\n}"}, {"source_code": "gets.to_i.times do\r\n gets\r\n a = gets.split.map &:to_i\r\n puts a.size - a.count(a.min)\r\nend"}, {"source_code": "gets.to_i.times do\r\n gets\r\n a = gets.split.map &:to_i\r\n a.delete a.min\r\n puts a.size\r\nend"}, {"source_code": "gets.to_i.times do\n _ = gets\n arr = gets.split.map &:to_i\n min = arr.min\n sum = 0\n for i in arr\n sum += 1 if i > min\n end\n\n puts sum\nend\n"}, {"source_code": "gets.to_i.times do\r\n n=gets.to_i\r\n a=gets.split.map(&:to_i)\r\n a.delete(a.min)\r\n puts a.size\r\nend\r\n"}], "negative_code": [], "src_uid": "99e5c907b623c310d6f1599f485ca21d"} {"nl": {"description": "Vanya and his friends are walking along the fence of height h and they do not want the guard to notice them. In order to achieve this the height of each of the friends should not exceed h. If the height of some person is greater than h he can bend down and then he surely won't be noticed by the guard. The height of the i-th person is equal to ai.Consider the width of the person walking as usual to be equal to 1, while the width of the bent person is equal to 2. Friends want to talk to each other while walking, so they would like to walk in a single row. What is the minimum width of the road, such that friends can walk in a row and remain unattended by the guard?", "input_spec": "The first line of the input contains two integers n and h (1 ≤ n ≤ 1000, 1 ≤ h ≤ 1000) — the number of friends and the height of the fence, respectively. The second line contains n integers ai (1 ≤ ai ≤ 2h), the i-th of them is equal to the height of the i-th person.", "output_spec": "Print a single integer — the minimum possible valid width of the road.", "sample_inputs": ["3 7\n4 5 14", "6 1\n1 1 1 1 1 1", "6 5\n7 6 8 9 10 5"], "sample_outputs": ["4", "6", "11"], "notes": "NoteIn the first sample, only person number 3 must bend down, so the required width is equal to 1 + 1 + 2 = 4.In the second sample, all friends are short enough and no one has to bend, so the width 1 + 1 + 1 + 1 + 1 + 1 = 6 is enough.In the third sample, all the persons have to bend, except the last one. The required minimum width of the road is equal to 2 + 2 + 2 + 2 + 2 + 1 = 11."}, "positive_code": [{"source_code": "# Vanya and Fence\n# https://codeforces.com/problemset/problem/677/A\n\n# _nums, f_height = gets.strip.split.map(&:to_i)\nf_height = gets.strip.split.last.to_i\np_height = gets.strip.split.map(&:to_i)\nwidth = 0\n\np_height.each do |h|\n width +=\n h <= f_height ? 1 : 2\nend\n\nputs width\n"}, {"source_code": "n,h=gets.split.map(&:to_i)\np n+gets.split.count{|e|e.to_i>h}"}, {"source_code": "n, h = gets.split.map(&:to_i)\narr = gets.split.map(&:to_i).select { |i| i > h }\nputs n + arr.size\n"}, {"source_code": "n,h = gets.chomp.split(\" \")\nfriends = gets.chomp.split(\" \").map(&:to_i)\n\ndef vanya_fence(n,h, friends)\n min_width = 0\n friends.each do |f|\n min_width += 1\n min_width += 1 if f > h.to_i\n end\n min_width\nend\n\np vanya_fence(n,h, friends)\n"}, {"source_code": "n, h = gets.chomp.split.map(&:to_i)\narr = gets.chomp.split.map(&:to_i).map { |d| d <= h ? 1 : 2 }\nputs arr.inject(0, :+)\n"}, {"source_code": "n,h=gets.split.map &:to_i;p n+gets.split.count{|_|_.to_i>h}"}, {"source_code": "n, m = gets.split.map &:to_i\n\nans = 0\n\na = []\na = gets.split.map &:to_i\n\na.each do |i|\n if (i > m)\n ans += 2\n else \n ans += 1\n end\nend\n\nputs ans"}, {"source_code": "_, zLen = STDIN.gets.chomp.split.map(&:to_i)\np STDIN.gets.chomp.split.inject(0){ |z, v| z + ((v.to_i - 1) / zLen) + 1 }"}, {"source_code": "def string_to_int(string, delim = \" \")\n result = []\n string = string.split(delim)\n for i in 0...string.length\n result[i] = string[i].to_i\n end\n return result\nend\n\ndef width(string, delim = \" \", height_limit)\n sum = 0\n string = string.split(delim)\n for i in 0...string.length\n if string[i].to_i > height_limit\n\t\tsum = sum + 2\n else\n\t\tsum = sum + 1\n end\n end\n return sum\nend\n\ninput = string_to_int(gets.chomp)\nlimit = input[1]\nputs width(gets.chomp, \" \", limit)\n"}, {"source_code": "x, y = gets.chomp().split(\" \")\nans = 0\narr = gets().split(\" \").each {|m| ans+= m.to_i > y.to_i ? 2:1}\nputs ans"}, {"source_code": "x, y = gets.chomp().split(\" \").each {|x| x.to_i}\n#puts x,y\nans = 0\narr = gets().split(\" \").each {|m| ans+= m.to_i > y.to_i ? 2:1}\nputs ans"}, {"source_code": "n,k = gets.split(\" \").map(&:to_i) \narr = gets.split(\" \").map(&:to_i) \nwidth =0 \narr.each do |val| \n if val > k \n width +=2 \n else \n width +=1 \n end \nend \nputs width "}, {"source_code": "x= gets.chomp.split(' ').map(&:to_i)\ny= gets.chomp.split(' ').map(&:to_i)\nc = 0\ny.each do |a|\n if (a>x[1])\n \tc= c+2\n else \n \tc = c +1\n end\n end\n puts c\n"}, {"source_code": "n, h = readline.split(' ').collect(&:to_i)\narr = readline.split(' ').collect(&:to_i)\nputs arr.map{|num| num > h ? 2 : 1}.inject(0, :+)\n"}, {"source_code": "n, h = gets.split.map(&:to_i)\nans = 0\ngets.split.map(&:to_i).each do |i|\n if i > h\n ans += 2\n else\n ans += 1\n end\nend\nputs ans"}, {"source_code": "n, h = gets.chomp.split(/ /).map(&:to_i)\nar = gets.chomp.split(/ /).map(&:to_i)\nans = 0\nar.each_index do |i|\n a = ar[i]\n if a <= h\n ans += 1\n else\n ans += 2\n end\nend\nputs ans\n"}, {"source_code": "n,h=gets.split.map &:to_i;p n+gets.split.count{|_|_.to_i>h}\n"}, {"source_code": "n,h=gets.split.map{|e| e.to_i}\nx=gets.split.map{|e| e.to_i}\n\nsum=0\nx.map{|e|\n\tsum=sum+((h h\n co += 2\n else\n co += 1\n end\nend\nputs co"}, {"source_code": "n,h = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\na.each do |i|\n\tif i > h\n\t\tn += 1\n\tend\t\nend\nputs n"}, {"source_code": "n, h = gets.split.map(&:to_i)\nputs gets.split.map { |e| e = e.to_i > h ? 2 : 1 }.reduce(:+)"}, {"source_code": "a,b = gets.split(\" \").map(&:to_i) \narr = gets.split(\" \").map(&:to_i) \nresult = 0\narr.each_index do |i|\n num = arr[i]\n if num <= b \n result+=1\n else\n result+=2\n end\nend\nputs result\n"}, {"source_code": "(_,h)=gets.split.map(&:to_i);puts gets.split.inject(0){|s,v|s+((v.to_i>h)?2:1)}"}, {"source_code": "n,h=gets.split.map &:to_i;p n+gets.split.count{|_|_.to_i>h}"}, {"source_code": "n, h = gets.chomp.split.map(&:to_i)\nas = gets.chomp.split.map(&:to_i)\n\nputs as.map { |a| a <= h ? 1 : 2 }.inject(&:+)\n"}, {"source_code": "n_f = gets.chomp\nh = gets.chomp\nn_fence = n_f.split\n\nheights = h.split\nfriends = n_fence[0]\nfence = n_fence[1]\n\nwidth = 0\nheights.each do |height|\n\tif height.to_i <= fence.to_i\n\t\twidth += 1\n\telse\n\t\twidth += 2\n\tend\nend\n\nprint width"}, {"source_code": "#!/usr/bin/ruby\n\nn, h = gets.chomp.split(\" \")\nh = h.to_i\na = gets.chomp\n\nb = a.split(\" \")\n\nsum = 0\nb.each do |i|\n sum += 1 if (i.to_i <= h.to_i)\n sum += 2 if (i.to_i > h.to_i)\nend\n\nputs sum\n"}, {"source_code": "n , h = gets.split(\" \")\nn = n.to_i\nh = h.to_i\ns = gets.split(\" \").map(&:to_i)\nans = 0\ns.each do |i|\n if i > h\n ans += 2\n else\n ans += 1\n end\nend\nputs ans\n"}, {"source_code": "a, b = gets.split.map &:to_i\nc = gets.split.map &:to_i\nw = 0\nc.map {|x| w += (x + b -1)/b}\nputs w\n"}, {"source_code": "n,h=gets.split.map &:to_i;p n+gets.split.count{|_|_.to_i>h}"}, {"source_code": "#!/usr/bin/env ruby\n\nn,h = gets.split.map(&:to_i)\nans = gets.split.map(&:to_i).inject(0) { |s,elem| s + (elem + h - 1) / h }\nputs ans\n"}, {"source_code": "n, h = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\nputs a.count{|x| x > h} + n\n"}, {"source_code": "n, h = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\nans = a.map{|x| x <= h ? 1 : 2}.inject(:+)\nputs ans\n"}, {"source_code": "n, h = gets.split.map(&:to_i)\np gets.split.map(&:to_i).map{|x|x <= h ? 1 : 2}.inject(:+)"}, {"source_code": "first = gets.chomp.split(\" \")\nh = first[1].to_i\nsecond = gets.chomp.split(\" \")\n\narr = second.collect{ |el| el.to_i}\n\nres = 0\n\nfor a in arr\n if a > h\n res += 2\n else\n res += 1\n end\nend\n\nputs res"}, {"source_code": "totalFriends, fenceHeight = gets.split.map(&:to_i)\ntotalBendFriends = gets.split.count { |e| e.to_i > fenceHeight}\nputs totalFriends + totalBendFriends"}, {"source_code": "n, h = gets.split(' ').map(&:to_i)\na = gets.split(' ').map(&:to_i)\n\nputs a.size + a.count { |x| x > h }\n"}, {"source_code": "n,h=gets.split.map &:to_i;p n+gets.split.count{|_|_.to_i>h}\n"}, {"source_code": "n,m = gets.chomp.split.map(&:to_i)\narr = gets.chomp.split.map(&:to_i)\nputs arr.map{|i| i > m ? 2 : 1}.reduce(:+)"}, {"source_code": "n, h = gets.chomp.split(' ').map { |i| i.to_i }\npersonHeight = gets.chomp.split(' ').map { |i| i.to_i }\n\nans = 0\n\npersonHeight.each do |pH|\n\tans += 1 if pH <= h\n\tans += 2 if pH > h\nend\n\nputs ans"}], "negative_code": [{"source_code": "n,h = gets.chomp.split(\" \")\nfriends = gets.chomp.split(\" \").map(&:to_i)\n\ndef vanya_fence(n,h, friends)\n min_width = 0\n friends.each do |f|\n min_width += 1 if f > h\n end\n min_width\nend\n"}, {"source_code": "n,k = gets.split(\" \").map(&:to_i) \narr = gets.split(\" \").map(&:to_i) \nwidth =0 \narr.each do |val| \n if val >= k \n width +=2 \n else \n width +=1 \n end \nend \nputs width "}], "src_uid": "e7ed5a733e51b6d5069769c3b1d8d22f"} {"nl": {"description": "There are $$$n$$$ astronauts working on some space station. An astronaut with the number $$$i$$$ ($$$1 \\le i \\le n$$$) has power $$$a_i$$$.An evil humanoid has made his way to this space station. The power of this humanoid is equal to $$$h$$$. Also, the humanoid took with him two green serums and one blue serum.In one second , a humanoid can do any of three actions: to absorb an astronaut with power strictly less humanoid power; to use green serum, if there is still one left; to use blue serum, if there is still one left. When an astronaut with power $$$a_i$$$ is absorbed, this astronaut disappears, and power of the humanoid increases by $$$\\lfloor \\frac{a_i}{2} \\rfloor$$$, that is, an integer part of $$$\\frac{a_i}{2}$$$. For example, if a humanoid absorbs an astronaut with power $$$4$$$, its power increases by $$$2$$$, and if a humanoid absorbs an astronaut with power $$$7$$$, its power increases by $$$3$$$.After using the green serum, this serum disappears, and the power of the humanoid doubles, so it increases by $$$2$$$ times.After using the blue serum, this serum disappears, and the power of the humanoid triples, so it increases by $$$3$$$ times.The humanoid is wondering what the maximum number of astronauts he will be able to absorb if he acts optimally.", "input_spec": "The first line of each test contains an integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — number of test cases. The first line of each test case contains integers $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) — number of astronauts and $$$h$$$ ($$$1 \\le h \\le 10^6$$$) — the initial power of the humanoid. The second line of each test case contains $$$n$$$ integers $$$a_i$$$ ($$$1 \\le a_i \\le 10^8$$$) — powers of astronauts. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, in a separate line, print the maximum number of astronauts that a humanoid can absorb.", "sample_inputs": ["8\n\n4 1\n\n2 1 8 9\n\n3 3\n\n6 2 60\n\n4 5\n\n5 1 100 5\n\n3 2\n\n38 6 3\n\n1 1\n\n12\n\n4 6\n\n12 12 36 100\n\n4 1\n\n2 1 1 15\n\n3 5\n\n15 1 13"], "sample_outputs": ["4\n3\n3\n3\n0\n4\n4\n3"], "notes": "NoteIn the first case, you can proceed as follows: use green serum. $$$h = 1 \\cdot 2 = 2$$$ absorb the cosmonaut $$$2$$$. $$$h = 2 + \\lfloor \\frac{1}{2} \\rfloor = 2$$$ use green serum. $$$h = 2 \\cdot 2 = 4$$$ absorb the spaceman $$$1$$$. $$$h = 4 + \\lfloor \\frac{2}{2} \\rfloor = 5$$$ use blue serum. $$$h = 5 \\cdot 3 = 15$$$ absorb the spaceman $$$3$$$. $$$h = 15 + \\lfloor \\frac{8}{2} \\rfloor = 19$$$ absorb the cosmonaut $$$4$$$. $$$h = 19 + \\lfloor \\frac{9}{2} \\rfloor = 23$$$ "}, "positive_code": [{"source_code": "gets.to_i.times do\n n,mh=gets.chomp.split(' ').map(&:to_i)\n arr=gets.chomp.split(' ').map(&:to_i).sort\n mx_ans,b=0,[0,1,2]\n b.each do |idx|\n ans =0\n s=0\n h=mh\n arr.each do |i|\n while i>=h && s<3\n if s==idx\n h*=3\n else \n h*=2\n end\n s+=1\n end\n if h>i\n h+=(i/2)\n ans+=1\n end\n end\n mx_ans=[mx_ans, ans].max\n end\n puts mx_ans\nend\n"}], "negative_code": [], "src_uid": "1f46c4ba21e734a1c7de8b2434791f77"} {"nl": {"description": "You are given two arrays of integers $$$a_1,\\ldots,a_n$$$ and $$$b_1,\\ldots,b_m$$$.Your task is to find a non-empty array $$$c_1,\\ldots,c_k$$$ that is a subsequence of $$$a_1,\\ldots,a_n$$$, and also a subsequence of $$$b_1,\\ldots,b_m$$$. If there are multiple answers, find one of the smallest possible length. If there are still multiple of the smallest possible length, find any. If there are no such arrays, you should report about it.A sequence $$$a$$$ is a subsequence of a sequence $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero) elements. For example, $$$[3,1]$$$ is a subsequence of $$$[3,2,1]$$$ and $$$[4,3,1]$$$, but not a subsequence of $$$[1,3,3,7]$$$ and $$$[3,10,4]$$$.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1\\le t\\le 1000$$$)  — the number of test cases. Next $$$3t$$$ lines contain descriptions of test cases. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1\\le n,m\\le 1000$$$)  — the lengths of the two arrays. The second line of each test case contains $$$n$$$ integers $$$a_1,\\ldots,a_n$$$ ($$$1\\le a_i\\le 1000$$$)  — the elements of the first array. The third line of each test case contains $$$m$$$ integers $$$b_1,\\ldots,b_m$$$ ($$$1\\le b_i\\le 1000$$$)  — the elements of the second array. It is guaranteed that the sum of $$$n$$$ and the sum of $$$m$$$ across all test cases does not exceed $$$1000$$$ ($$$\\sum\\limits_{i=1}^t n_i, \\sum\\limits_{i=1}^t m_i\\le 1000$$$).", "output_spec": "For each test case, output \"YES\" if a solution exists, or \"NO\" otherwise. If the answer is \"YES\", on the next line output an integer $$$k$$$ ($$$1\\le k\\le 1000$$$)  — the length of the array, followed by $$$k$$$ integers $$$c_1,\\ldots,c_k$$$ ($$$1\\le c_i\\le 1000$$$)  — the elements of the array. If there are multiple solutions with the smallest possible $$$k$$$, output any.", "sample_inputs": ["5\n4 5\n10 8 6 4\n1 2 3 4 5\n1 1\n3\n3\n1 1\n3\n2\n5 3\n1000 2 2 2 3\n3 1 5\n5 5\n1 2 3 4 5\n1 2 3 4 5"], "sample_outputs": ["YES\n1 4\nYES\n1 3\nNO\nYES\n1 3\nYES\n1 2"], "notes": "NoteIn the first test case, $$$[4]$$$ is a subsequence of $$$[10, 8, 6, 4]$$$ and $$$[1, 2, 3, 4, 5]$$$. This array has length $$$1$$$, it is the smallest possible length of a subsequence of both $$$a$$$ and $$$b$$$.In the third test case, no non-empty subsequences of both $$$[3]$$$ and $$$[2]$$$ exist, so the answer is \"NO\"."}, "positive_code": [{"source_code": " t = gets.chomp.to_i\n while(t>0)\n \tn_m = gets.chomp.split(\" \")\n \tn = n_m[0].to_i\n \tm = n_m[1].to_i\n \ta_inp = gets.chomp.split(\" \")\n \ta = a_inp.map{|i| i = i.to_i}\n \tb_inp = gets.chomp.split(\" \")\n \tb = b_inp.map{|i| i = i.to_i}\n \tc = []\n \tc = a & b\n \tunless(c.count>0)\n \t\tputs \"NO\"\n \telse\n \t\tputs \"YES\"\n \t\tputs \"1 #{c[0]}\"\n \tend\n \tt = t-1\n end"}, {"source_code": "t = gets.to_i\n\n1.upto t do\n n, m = gets.split.map(&:to_i)\n a = gets.split.map(&:to_i)\n b = gets.split.map(&:to_i)\n c = a.intersection(b)\n if c.empty?\n puts \"NO\"\n else\n puts \"YES\"\n puts \"1 #{c[0]}\"\n end\nend"}, {"source_code": "t = gets.chomp.to_i\nt.times do \n arr = Array.new(1001,0)\n ar = Array.new(1001,0)\n n, m = gets.chomp.split(\" \").map(&:to_i)\n if n >= 2\n a = gets.chomp.split(\" \").map(&:to_i)\n a.each do |i|\n arr[i] += 1\n end\n else\n a = gets.chomp.to_i\n arr[a] += 1\n end\n \n if m >= 2\n b = gets.chomp.split(\" \").map(&:to_i)\n b.each do |j|\n ar[j] += 1\n end\n else\n b = gets.chomp.to_i\n ar[b] += 1\n end\n ans = 0\n (1..1000).each do |k|\n if arr[k] >= 1 && ar[k] >= 1\n puts \"YES\"\n print \"1\"\n print \" \"\n print k\n print \"\\n\"\n ans += 1\n break\n end\n end\n if ans == 0\n puts \"NO\"\n end\nend\n"}, {"source_code": "def read\n gets.split.map(&:to_i)\nend\ngets.to_i.times do\n n, m = read\n a = read\n b = read\n res = nil\n a.each do |i| \n b.each do |j|\n res = i if i == j\n end\n end\n if res.nil?\n puts \"NO\"\n else\n puts \"YES\", 1, res\n end\nend"}, {"source_code": "tc = gets.to_i \n\ntc.times do\n n,m = gets.split().map(&:to_i)\n arr = gets.split().map(&:to_i) \n brr = gets.split().map(&:to_i) \n crr = arr & brr \n if(crr.size > 0) \n puts \"YES\"\n puts \"1 #{crr[0]}\"\n else\n puts \"NO\" \n end\nend\n"}], "negative_code": [{"source_code": "t = gets.chomp.to_i\n\t\twhile(t>1)\n\t\t\tn_m = gets.chomp.split(\" \")\n\t\t\tn = n_m[0].to_i\n\t\t\tm = n_m[1].to_i\n\t\t\ta_inp = gets.chomp.split(\" \")\n\t\t\ta = a_inp.map{|i| i = i.to_i}\n\t\t\tb_inp = gets.chomp.split(\" \")\n\t\t\tb = b_inp.map{|i| i = i.to_i}\n\t\t\tc = []\n\t\t\tc = a & b\n\t\t\tunless(c.count>0)\n\t\t\t\tputs \"NO\"\n\t\t\telse\n\t\t\t\tputs \"YES\"\n\t\t\t\tputs \"#{c.count} #{c.join(' ')}\"\n\t\t\tend\n\t\t\tt = t-1\n\t\tend"}, {"source_code": "t = gets.chomp.to_i\nwhile(t>1)\n\tn_m = gets.chomp.split(\" \")\n\tn = n_m[0].to_i\n\tm = n_m[1].to_i\n\ta_inp = gets.chomp.split(\" \")\n\ta = a_inp.map{|i| i = i.to_i}\n\tb_inp = gets.chomp.split(\" \")\n\tb = b_inp.map{|i| i = i.to_i}\n\tc = []\n\tc = a & b\n\tunless(c.count>0)\n\t\tputs \"NO\"\n\telse\n\t\tputs \"YES\"\n\t\tputs \"1 #{c[0]}\"\n\tend\n\tt = t-1\nend"}, {"source_code": "$, = \"\\n\"\ndef read\n gets.split.map(&:to_i)\nend\ngets.to_i.times do\n n, m = read\n a = read\n b = read\n res = nil\n a.each do |i| \n b.each do |j|\n res = i if i == j\n end\n end\n if res.nil?\n puts \"NO\"\n else\n print \"YES\", 1, res\n end\nend"}, {"source_code": "tc = gets.to_i \n\ntc.times do\n n,m = gets.split().map(&:to_i)\n arr = gets.split().map(&:to_i) \n brr = gets.split().map(&:to_i) \n crr = arr & brr \n if(crr.size > 0) \n puts \"YES\"\n puts crr[0]\n else\n puts \"NO\" \n end\nend\n"}], "src_uid": "776a06c14c6fa3ef8664eec0b4d50824"} {"nl": {"description": "You are given two sequences $$$a_1, a_2, \\dots, a_n$$$ and $$$b_1, b_2, \\dots, b_n$$$. Each element of both sequences is either $$$0$$$, $$$1$$$ or $$$2$$$. The number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$a$$$ is $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ respectively, and the number of elements $$$0$$$, $$$1$$$, $$$2$$$ in the sequence $$$b$$$ is $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ respectively.You can rearrange the elements in both sequences $$$a$$$ and $$$b$$$ however you like. After that, let's define a sequence $$$c$$$ as follows:$$$c_i = \\begin{cases} a_i b_i & \\mbox{if }a_i > b_i \\\\ 0 & \\mbox{if }a_i = b_i \\\\ -a_i b_i & \\mbox{if }a_i < b_i \\end{cases}$$$You'd like to make $$$\\sum_{i=1}^n c_i$$$ (the sum of all elements of the sequence $$$c$$$) as large as possible. What is the maximum possible sum?", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains three integers $$$x_1$$$, $$$y_1$$$, $$$z_1$$$ ($$$0 \\le x_1, y_1, z_1 \\le 10^8$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$a$$$. The second line of each test case also contains three integers $$$x_2$$$, $$$y_2$$$, $$$z_2$$$ ($$$0 \\le x_2, y_2, z_2 \\le 10^8$$$; $$$x_1 + y_1 + z_1 = x_2 + y_2 + z_2 > 0$$$) — the number of $$$0$$$-s, $$$1$$$-s and $$$2$$$-s in the sequence $$$b$$$.", "output_spec": "For each test case, print the maximum possible sum of the sequence $$$c$$$.", "sample_inputs": ["3\n2 3 2\n3 3 1\n4 0 1\n2 3 0\n0 0 1\n0 0 1"], "sample_outputs": ["4\n2\n0"], "notes": "NoteIn the first sample, one of the optimal solutions is:$$$a = \\{2, 0, 1, 1, 0, 2, 1\\}$$$$$$b = \\{1, 0, 1, 0, 2, 1, 0\\}$$$$$$c = \\{2, 0, 0, 0, 0, 2, 0\\}$$$In the second sample, one of the optimal solutions is:$$$a = \\{0, 2, 0, 0, 0\\}$$$$$$b = \\{1, 1, 0, 1, 0\\}$$$$$$c = \\{0, 2, 0, 0, 0\\}$$$In the third sample, the only possible solution is:$$$a = \\{2\\}$$$$$$b = \\{2\\}$$$$$$c = \\{0\\}$$$"}, "positive_code": [{"source_code": "t =gets.to_i\n\nt.times do\n a0,a1,a2 = gets.split.map(&:to_i)\n b0,b1,b2 = gets.split.map(&:to_i)\n mk = [a2,b1].min.clone\n ans = 2*mk\n a2 -= mk\n b1 -= mk\n mk = [a0,b2].min.clone\n a0-=mk\n b2-=mk\n if b2>0 then\n mk = [a2,b2].min.clone\n a2-=mk\n b2-=mk\n end\n ans -= 2*b2\n puts ans\nend"}, {"source_code": "def solve\n x1, y1, z1 = gets.strip.split(' ').map(&:to_i)\n x2, y2, z2 = gets.strip.split(' ').map(&:to_i)\n res = 1\n mn1 = [z1, y2].min #(2, 1)\n z1 -= mn1\n y2 -= mn1\n mn2 = [x1, z2].min #(0, 2)\n z2 -= mn2\n x1 -= mn2\n mn4 = [z1, z2].min #(2, 2)\n z1 -= mn4\n z2 -= mn4\n res *= (2 * mn1)\n res = res - (2 * z2) if !z2.zero?\n puts res\nend\n\ngets.strip.to_i.times { solve }"}, {"source_code": "t = gets.chomp.to_i\nt.times {\n x1, y1, z1 = gets.chomp.split(' ').map(&:to_i)\n x2, y2, z2 = gets.chomp.split(' ').map(&:to_i)\n ans = 0\n min = [y1, x2].min\n y1 -= min\n x2 -= min\n\n min = [x1, z2].min\n x1 -= min\n z2 -= min\n\n ans = ans + 2 * [z1, y2].min\n min = [z1, y2].min\n z1 -= min\n y2 -= min\n\n min = [z1, z2].min\n z1 -= min\n z2 -= min\n\n min = [y1, y2].min\n y1 -= min\n y2 -= min\n\n ans = ans + -2 * [y1, z2].min\n puts ans\n}"}, {"source_code": "def solve\n x1, y1, z1 = gets.strip.split(' ').map(&:to_i)\n x2, y2, z2 = gets.strip.split(' ').map(&:to_i)\n res = 1\n mn1 = [z1, y2].min #(2, 1)\n z1 -= mn1\n y2 -= mn1\n mn2 = [x1, z2].min #(0, 2)\n z2 -= mn2\n x1 -= mn2\n mn4 = [z1, z2].min #(2, 2)\n z1 -= mn4\n z2 -= mn4\n res *= (2 * mn1)\n res = res - (2 * z2) if !z2.zero?\n puts res\nend\n\ngets.strip.to_i.times { solve }"}, {"source_code": "t = gets.to_i\nans = []\nt.times do\n x = gets.chomp.split(\" \").map(&:to_i)\n y = gets.chomp.split(\" \").map(&:to_i)\n sum = 0\n num2 = [x[2],y[1]].min\n x[2] -= num2\n y[1] -= num2\n sum += num2 * 2\n minus = [y[2],x[0]].min\n y[2] -= minus\n x[0] -= minus\n minus = [y[2],x[2]].min\n y[2] -= minus\n x[2] -= minus\n num2 = [x[1],y[2]].min\n sum -= num2 * 2\n ans << sum\nend\nans.each do |x|\n puts x\nend"}, {"source_code": "t = gets.chomp.to_i\n\nwhile (t-=1) >= 0\n a, b, c = gets.chomp.split(\" \").map(&:to_i)\n z, x, y = gets.chomp.split(\" \").map(&:to_i)\n minCX = [c, x].min\n puts (minCX - [0, y - a - c + minCX].max)*2\nend"}, {"source_code": "require 'set'\n\ndef taskA\n a, b = gets.chomp.split(\" \").map(&:to_i)\n puts b > a ? b - a : (a + b) % 2\nend\n\ndef taskB\n a, b, c = gets.chomp.split(\" \").map(&:to_i)\n z, x, y = gets.chomp.split(\" \").map(&:to_i)\n minCX = [c, x].min\n puts (minCX - [0, y - a - c + minCX].max)*2\nend\n\nt = gets.chomp.to_i\nwhile (t-=1) >= 0\n taskB\nend\n"}], "negative_code": [{"source_code": "def solve\n x1, y1, z1 = gets.strip.split(' ').map(&:to_i)\n x2, y2, z2 = gets.strip.split(' ').map(&:to_i)\n res = 1\n mn1 = [z1, y2].min #(2, 1)\n z1 -= mn1\n y2 -= mn1\n mn2 = [x1, z2].min #(0, 2)\n z2 -= mn2\n x1 -= mn2\n mn3 = [y1, y2].min #(1, 1)\n y1 -= mn3\n y2 -= mn3\n mn4 = [z1, z2].min #(2, 2)\n z1 -= mn4\n z2 -= mn4\n res *= (2 * mn1)\n res = res - (z2 * y1) if !z2.zero?\n puts res\nend\n\ngets.strip.to_i.times { solve }"}, {"source_code": "\nt = gets.chomp.to_i\nt.times {\n x1, y1, z1 = gets.chomp.split(' ').map(&:to_i)\n x2, y2, z2 = gets.chomp.split(' ').map(&:to_i)\n ans = 0\n ans = ans + 2 * [z1, y2].min\n\n z1 -= [z1, y2].min\n y2 -= [z1, y2].min\n\n y1 -= [y1, x2].min\n x2 -= [y1, x2].min\n\n x1 = x1 - [x1, z2].min\n z2 = z2 - [x1, z2].min\n\n ans = ans + -2 * [y1, z2].min\n puts ans\n}"}, {"source_code": "t = gets.chomp.to_i\nt.times {\n x1, y1, z1 = gets.chomp.split(' ').map(&:to_i)\n x2, y2, z2 = gets.chomp.split(' ').map(&:to_i)\n puts (2 * [z1, y2].min)\n}"}, {"source_code": "# t = gets.chomp.to_i\n# t.times {\n# n, k = gets.chomp.split(' ').map(&:to_i)\n# arr = gets.chomp.split(' ').map(&:to_i)\n# max = arr.max\n# arr.map! { |ele| max - ele }\n# max = arr.max\n# if k % 2 == 0\n# arr.map! { |ele| max - ele }\n# end\n# arr.each {|ele| print '%d ' % ele }\n# print '\\n'\n# }\n\n\n\nt = gets.chomp.to_i\nt.times {\n x1, y1, z1 = gets.chomp.split(' ').map(&:to_i)\n x2, y2, z2 = gets.chomp.split(' ').map(&:to_i)\n ans = 0\n ans = ans + 2 * [z1, y2].min\n\n z1 -= [z1, y2].min\n y2 -= [z1, y2].min\n\n y1 -= [y1, x2].min\n x2 -= [y1, x2].min\n\n x1 -= [x1, z2].min\n z2 -= [x1, z2].min\n\n y1 -= [y1, y2].min\n y2 -= [y1, y2].min\n\n ans = ans + -2 * [y1, z2].min\n puts ans\n}"}, {"source_code": "\nt = gets.chomp.to_i\nt.times {\n x1, y1, z1 = gets.chomp.split(' ').map(&:to_i)\n x2, y2, z2 = gets.chomp.split(' ').map(&:to_i)\n ans = 0\n ans = ans + 2 * [z1, y2].min\n\n z1 -= [z1, y2].min\n y2 -= [z1, y2].min\n\n y1 -= [y1, y2].min\n y2 -= [y1, y2].min\n\n y1 -= [y1, x2].min\n x2 -= [y1, x2].min\n\n x1 -= [x1, x2].min\n x2 -= [x1, x2].min\n\n x1 -= [x1, y2].min\n y2 -= [x1, y2].min\n\n x1 -= [x1, z2].min\n z2 -= [x1, z2].min\n\n ans = ans + -2 * [y1, z2].min\n puts ans\n}"}, {"source_code": "\nt = gets.chomp.to_i\nt.times {\n x1, y1, z1 = gets.chomp.split(' ').map(&:to_i)\n x2, y2, z2 = gets.chomp.split(' ').map(&:to_i)\n ans = 0\n y1 -= [y1, x2].min\n x2 -= [y1, x2].min\n\n\n x1 -= [x1, z2].min\n z2 -= [x1, z2].min\n\n ans = ans + 2 * [z1, y2].min\n\n z1 -= [z1, y2].min\n y2 -= [z1, y2].min\n\n z1 -= [z1, z2].min\n z2 -= [z1, z2].min\n\n y1 -= [y1, y2].min\n y2 -= [y1, y2].min\n\n ans = ans + -2 * [y1, z2].min\n puts ans\n}"}, {"source_code": "\nt = gets.chomp.to_i\nt.times {\n x1, y1, z1 = gets.chomp.split(' ').map(&:to_i)\n x2, y2, z2 = gets.chomp.split(' ').map(&:to_i)\n ans = 0\n ans = ans + 2 * [z1, y2].min\n\n z1 -= [z1, y2].min\n y2 -= [z1, y2].min\n\n y1 -= [y1, x2].min\n x2 -= [y1, x2].min\n\n x1 = x1 - [x1, z2].min\n z2 = z2 - [x1, z2].min\n\n x1 -= [x1, z2].min\n z2 -= [x1, z2].min\n \n ans = ans + -2 * [y1, z2].min\n puts ans\n}"}], "src_uid": "e1de1e92fac8a6db3222c0d3c26843d8"} {"nl": {"description": "You are given an array of $$$n$$$ non-negative integers $$$a_1, a_2, \\ldots, a_n$$$. You can make the following operation: choose an integer $$$x \\geq 2$$$ and replace each number of the array by the remainder when dividing that number by $$$x$$$, that is, for all $$$1 \\leq i \\leq n$$$ set $$$a_i$$$ to $$$a_i \\bmod x$$$.Determine if it is possible to make all the elements of the array equal by applying the operation zero or more times.", "input_spec": "The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \\leq n \\leq 10^5$$$) — the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$0 \\leq a_i \\leq 10^9$$$) where $$$a_i$$$ is the $$$i$$$-th element of the array. The sum of $$$n$$$ for all test cases is at most $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, print a line with YES if you can make all elements of the list equal by applying the operation. Otherwise, print NO. You may print each letter in any case (for example, \"YES\", \"Yes\", \"yes\", \"yEs\" will all be recognized as a positive answer).", "sample_inputs": ["4\n4\n2 5 6 8\n3\n1 1 1\n5\n4 1 7 0 8\n4\n5 9 17 5"], "sample_outputs": ["YES\nYES\nNO\nYES"], "notes": "NoteIn the first test case, one can apply the operation with $$$x = 3$$$ to obtain the array $$$[2, 2, 0, 2]$$$, and then apply the operation with $$$x = 2$$$ to obtain $$$[0, 0, 0, 0]$$$.In the second test case, all numbers are already equal.In the fourth test case, applying the operation with $$$x = 4$$$ results in the array $$$[1, 1, 1, 1]$$$."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n gets\r\n as = gets.split.map(&:to_i).sort\r\n ok = as.include?(1) ? as.each_cons(2).none? { |a, b| b - a == 1 } : true\r\n puts ok ? \"YES\" : \"NO\"\r\nend\r\n"}, {"source_code": "gets.to_i.times do \n gets\n as = gets.split.map(&:to_i).sort\n ok = as.include?(1) ? as.each_cons(2).none? { |a, b| b - a == 1 } : true\n puts ok ? \"YES\" : \"NO\"\nend\n\n \t\t \t \t \t \t \t\t \t\t \t"}], "negative_code": [{"source_code": "gets.to_i.times do\r\n gets\r\n as = gets.split.map(&:to_i)\r\n ok = as.include?(1) ? as.all?(&:odd?) : true\r\n puts ok ? \"YES\" : \"NO\"\r\nend\r\n"}], "src_uid": "c7e4f544ec8b4972291542e66aff5df5"} {"nl": {"description": "Mike and some bears are playing a game just for fun. Mike is the judge. All bears except Mike are standing in an n × m grid, there's exactly one bear in each cell. We denote the bear standing in column number j of row number i by (i, j). Mike's hands are on his ears (since he's the judge) and each bear standing in the grid has hands either on his mouth or his eyes. They play for q rounds. In each round, Mike chooses a bear (i, j) and tells him to change his state i. e. if his hands are on his mouth, then he'll put his hands on his eyes or he'll put his hands on his mouth otherwise. After that, Mike wants to know the score of the bears.Score of the bears is the maximum over all rows of number of consecutive bears with hands on their eyes in that row.Since bears are lazy, Mike asked you for help. For each round, tell him the score of these bears after changing the state of a bear selected in that round. ", "input_spec": "The first line of input contains three integers n, m and q (1 ≤ n, m ≤ 500 and 1 ≤ q ≤ 5000). The next n lines contain the grid description. There are m integers separated by spaces in each line. Each of these numbers is either 0 (for mouth) or 1 (for eyes). The next q lines contain the information about the rounds. Each of them contains two integers i and j (1 ≤ i ≤ n and 1 ≤ j ≤ m), the row number and the column number of the bear changing his state.", "output_spec": "After each round, print the current score of the bears.", "sample_inputs": ["5 4 5\n0 1 1 0\n1 0 0 1\n0 1 1 0\n1 0 0 1\n0 0 0 0\n1 1\n1 4\n1 1\n4 2\n4 3"], "sample_outputs": ["3\n4\n3\n3\n4"], "notes": null}, "positive_code": [{"source_code": "a=gets.chomp.split(\" \").map {|i| i.to_i }\nar=[]\non=[]\na[0].times do \ntemp=gets.chomp.split(\" \")\nmax=0\nt=0\ntemp.each do |i|\nif i==\"1\" \nt+=1\nelse\nif t>max\nmax=t\nend\nt=0\nend\nend\nif t>max\nmax=t\nend\non << max\nar << temp\nend\n\na[2].times do \ntemp=gets.chomp.split(\" \").map {|i| i.to_i}\nif ar[temp[0]-1][temp[1]-1]==\"1\"\nar[temp[0]-1][temp[1]-1]=\"0\"\nelse\nar[temp[0]-1][temp[1]-1]=\"1\"\nend\nmax=0\nt=0\nar[temp[0]-1].each do |i|\nif i==\"1\" \nt+=1\nelse\nif t>max\nmax=t\nend\nt=0\nend\nend\nif t>max\nmax=t\nend\non[temp[0]-1]=max\nputs \"#{on.max}\"\nend\n\n"}, {"source_code": "a=gets.chomp.split(\" \").map {|i| i.to_i }\nar=[]\non=[]\na[0].times do \ntemp=gets.chomp.split(\" \")\nmax=0\nt=0\ntemp.each do |i|\nif i==\"1\" \nt+=1\nelse\nif t>max\nmax=t\nend\nt=0\nend\nend\nif t>max\nmax=t\nend\non << max\nar << temp\nend\n\na[2].times do \ntemp=gets.chomp.split(\" \").map {|i| i.to_i}\nif ar[temp[0]-1][temp[1]-1]==\"1\"\nar[temp[0]-1][temp[1]-1]=\"0\"\nelse\nar[temp[0]-1][temp[1]-1]=\"1\"\nend\nmax=0\nt=0\nar[temp[0]-1].each do |i|\nif i==\"1\" \nt+=1\nelse\nif t>max\nmax=t\nend\nt=0\nend\nend\nif t>max\nmax=t\nend\non[temp[0]-1]=max\nputs \"#{on.max}\"\nend\n\n\n\n\n"}, {"source_code": "def row_sum(arr)\n sum = 0\n total = 0\n for i in 0...arr.size\n if arr[i] == 0\n sum = 0\n else\n sum += 1\n end\n total = [sum,total].max\n end\n total\nend\n\n\ninput = gets.chomp.split(\" \")\nn , m , q = input[0].to_i, input[1].to_i, input[2].to_i\nscores = []\n\ngrid = []\nfor i in 0...n\n #read each row\n grid[i] = gets.chomp.split(\" \").collect{|bear| bear.to_i}\n sum = row_sum(grid[i])\n #puts \"Sum is #{sum}\"\n scores[i] = sum\nend\n\nsolution = []\nfor i in 0...q\n move = gets.chomp.split(' ').collect{|num| num.to_i-1}\n if grid[move[0]][move[1]] == 1\n grid[move[0]][move[1]] = 0\n else\n grid[move[0]][move[1]] = 1\n end\n scores[move[0]] = row_sum(grid[move[0]])\n solution << \"#{scores.max}\"\nend\nputs solution"}, {"source_code": "n, m, q = gets.split.map(&:to_i)\na = n.times.map{gets.split.map(&:to_i)}\nc = Array.new(n, 0)\nn.times do |i|\n j = 0\n while j < m\n if a[i][j] == 1\n t = 0\n j.upto(m) do |k|\n if a[i][k] == 1\n t += 1\n else\n j = k + 1\n c[i] = [c[i], t].max\n break\n end\n end\n else\n j += 1\n end\n end\nend\nq.times do\n i, j = gets.split.map(&:to_i).map{|x| x -= 1}\n a[i][j] ^= 1\n cc = 0\n y = 0\n while y < m\n if a[i][y] == 1\n t = 0\n y.upto(m) do |k|\n if a[i][k] == 1\n t += 1\n else\n y = k + 1\n cc = [cc, t].max\n break\n end\n end\n else\n y += 1\n end\n end\n c[i] = cc\n puts c.max\nend\n"}, {"source_code": "def getMax(row)\n\tstartNum = 0\n\tcurrentMax = 0\n\tnowIndex = row.index(1)\n\n\tif nowIndex.nil?\n\t\treturn 0\n\tend\n\n\tif nowIndex == row.length\n\t\treturn 1\n\tend\n\n\t#puts \"Rindex : #{endIndex}\"\n\tuntil nowIndex == row.length\n\t\tstartNum += 1 if row[nowIndex] == 1\n\t\tcurrentMax = startNum if currentMax < startNum\n\n\t\tstartNum = 0 if row[nowIndex] == 0\n\n\t\tnowIndex += 1\n\tend\n\n\treturn currentMax\nend\n\ninput = gets.chomp.split(' ').map {|item| item.to_i}\nbears = []\nresult = []\nmaxArray = []\n\nfor i in 0...input[0]\n\tcolumn = gets.chomp.split(' ').map {|item| item.to_i}\n\tbears << column\nend\n\nbears.each do |row|\n\t\tmaxArray << getMax(row)\nend\n\nfor i in 0...input[2]\n\tqueryInput = gets.chomp.split(' ').map {|item| item.to_i}\n\n\tbears[queryInput[0] - 1][queryInput[1] - 1] -= 1\n\tbears[queryInput[0] - 1][queryInput[1] - 1] = 1 if bears[queryInput[0] - 1][queryInput[1] - 1] < 0\n\n\tmaxArray[queryInput[0] - 1] = getMax(bears[queryInput[0] - 1])\n\n\tresult << maxArray.max\nend\n\nresult.each do |item|\n\tputs item\nend"}], "negative_code": [{"source_code": "def row_sum(arr)\n sum = 0\n chunks = []\n arr.chunk{|num| num==1}.each{|val| chunks << val}\n chunks.each do |chk|\n if chk[0] == true\n sum = [sum, chk[1].size].max\n end\n end\n sum\nend\n\n\ninput = gets.chomp.split(\" \")\nn , m , q = input[0].to_i, input[1].to_i, input[2].to_i\n\ngrid = []\nmax_score = 0\nmax_row = 0\nfor i in 0...n\n #read each row\n grid[i] = gets.chomp.split(\" \").collect{|bear| bear.to_i}\n sum = row_sum(grid[i])\n #puts \"Sum is #{sum}\"\n if sum > max_score\n max_score = sum\n max_row = i\n end\nend\n\nsolution = []\nfor i in 0...q\n move = gets.chomp.split(' ').collect{|num| num.to_i-1}\n if grid[move[0]][move[1]] == 1\n grid[move[0]][move[1]] = 0\n else\n grid[move[0]][move[1]] = 1\n end\n #if the move[0] row is bigger than current update it\n sum = row_sum(grid[move[0]])\n #puts \"Move #{move[0]},#{move[1]} changed sum to #{sum}\"\n #puts \"\\tOld Max score is #{max_score}, max row is #{max_row}\"\n if sum > max_score || move[0] == max_row\n max_score = sum\n max_row = move[0]\n end\n #puts \"\\tNew Max score is #{max_score}, max row is #{max_row}\"\n\n solution << \"#{max_score}\"\nend\nputs solution"}, {"source_code": "n, m, q = gets.split.map(&:to_i)\na = n.times.map{gets.split.map(&:to_i)}\nq.times do\n i, j = gets.split.map(&:to_i)\n a[i - 1][j - 1] ^= 1\n mx = 0\n a.each do |x|\n m.times do |k|\n if x[k] == 1\n c = (x[k...m].index(0) || m - k)\n mx = [mx, c].max\n break\n end\n end\n end\n puts mx\nend\n"}, {"source_code": "n, m, q = gets.split.map(&:to_i)\na = n.times.map{gets.split.map(&:to_i)}\nq.times do\n i, j = gets.split.map(&:to_i)\n a[i - 1][j - 1] ^= 1\n puts a.map{|x| x.inject(:+)}.max\nend\n"}], "src_uid": "337b6d2a11a25ef917809e6409f8edef"} {"nl": {"description": "There are $$$n$$$ students standing in a circle in some order. The index of the $$$i$$$-th student is $$$p_i$$$. It is guaranteed that all indices of students are distinct integers from $$$1$$$ to $$$n$$$ (i. e. they form a permutation).Students want to start a round dance. A clockwise round dance can be started if the student $$$2$$$ comes right after the student $$$1$$$ in clockwise order (there are no students between them), the student $$$3$$$ comes right after the student $$$2$$$ in clockwise order, and so on, and the student $$$n$$$ comes right after the student $$$n - 1$$$ in clockwise order. A counterclockwise round dance is almost the same thing — the only difference is that the student $$$i$$$ should be right after the student $$$i - 1$$$ in counterclockwise order (this condition should be met for every $$$i$$$ from $$$2$$$ to $$$n$$$). For example, if the indices of students listed in clockwise order are $$$[2, 3, 4, 5, 1]$$$, then they can start a clockwise round dance. If the students have indices $$$[3, 2, 1, 4]$$$ in clockwise order, then they can start a counterclockwise round dance.Your task is to determine whether it is possible to start a round dance. Note that the students cannot change their positions before starting the dance; they cannot swap or leave the circle, and no other student can enter the circle. You have to answer $$$q$$$ independent queries.", "input_spec": "The first line of the input contains one integer $$$q$$$ ($$$1 \\le q \\le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \\le n \\le 200$$$) — the number of students. The second line of the query contains a permutation of indices $$$p_1, p_2, \\dots, p_n$$$ ($$$1 \\le p_i \\le n$$$), where $$$p_i$$$ is the index of the $$$i$$$-th student (in clockwise order). It is guaranteed that all $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i. e. they form a permutation).", "output_spec": "For each query, print the answer on it. If a round dance can be started with the given order of students, print \"YES\". Otherwise print \"NO\".", "sample_inputs": ["5\n4\n1 2 3 4\n3\n1 3 2\n5\n1 2 3 5 4\n1\n1\n5\n3 2 1 5 4"], "sample_outputs": ["YES\nYES\nNO\nYES\nYES"], "notes": null}, "positive_code": [{"source_code": "gets.strip.to_i.times do\n n = gets.strip.to_i\n a = gets.strip.split(' ').map(&:to_i)\n sorted = a.dup.sort\n res = false\n n.times do |i|\n rot = a.rotate(i)\n if sorted == rot || sorted.reverse == rot\n puts \"YES\"\n res = true\n break\n end\n end\n puts \"NO\" unless res\nend\n"}, {"source_code": "gets.to_i.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n s = a.dup.sort\n ok = false\n (0...n).each do |i|\n r = a.rotate i\n if s == r || s.reverse == r\n puts \"YES\"\n ok = true\n break\n end\n end\n puts \"NO\" unless ok\nend"}, {"source_code": "q = gets.to_i\nans = []\nq.times do\n n = gets.to_i\n pp = gets.split.map(&:to_i)\n\n def_a = (1..n).to_a\n diff_a = pp.map.with_index{|num,i|num-def_a[i]}\n diff_a2 = pp.map.with_index{|num,i|num-def_a[n-1-i]}\n can = 2>=diff_a.uniq.size || 2>=diff_a2.uniq.size\n\n ans << (can ? 'YES' : 'NO')\nend\nputs ans\n"}, {"source_code": "#!/usr/bin/env ruby\n\nq = Integer(gets)\n \nresults = Array.new\nq.times do\n\t\n\tn = Integer(gets)\n \n\tarr = gets.chomp.split(\" \").map(&:to_i)\n \n\tfail = false\n\t(n-1).times do |index|\n\t\tif (arr[index] - arr[index+1]).abs >1 && (arr[index] - arr[index+1]).abs != (n-1)\n\t\t\tfail = true\n\t\t\tresults << \"NO\"\n\t\t\tbreak\n\t\tend\n\tend\n \n\tresults << \"YES\" if !fail\nend\n \nresults.each do |result|\n\tputs result\nend"}, {"source_code": "t = gets.to_i\nt.times do |index|\n n = gets.to_i\n a = gets.split(\" \").map(&:to_i)\n c = true\n cc = true\n (n - 1).times do |i|\n # p a[i].to_s+\" \"+a[i+1].to_s+\" \"+i.to_s+\" \"+index.to_s\n if !((a[i] == a[i+1] + 1) || (a[i] == 1 && a[i+1] == n))\n # p \"false c\"\n cc = false\n end\n if !((a[i] + 1 == a[i+1]) || (a[i] == n && a[i+1] == 1))\n # p \"false cc\"\n c = false\n end\n end\n puts (c || cc) ? \"YES\" : \"NO\"\nend"}], "negative_code": [{"source_code": "q = gets.to_i\nans = []\nq.times do\n n = gets.to_i\n pp = gets.split.map(&:to_i)\n\n def_a = (1..n).to_a\n diff_a = pp.map.with_index{|num,i|num-def_a[i]}\n diff_a2 = pp.map.with_index{|num,i|num-def_a[n-1-i]}\n can = n==diff_a.uniq.size || n==diff_a2.uniq.size\n\n ans << (can ? 'YES' : 'NO')\nend\nputs ans"}, {"source_code": "q = gets.to_i\nans = []\nq.times do\n n = gets.to_i\n pp = gets.split.map(&:to_i)\n\n min_i = pp.index(1)\n max_i = pp.index(n)\n dist = (min_i-max_i).abs\n can = false\n\n if dist==0\n can = true\n elsif dist == 1\n left_idx = min_ia}&&ra==ra.sort{|a,b|b<=>a})\n else\n can = pp==pp.sort || pp==pp.sort{|a,b|b<=>a}\n end\n\n ans << (can ? 'YES' : 'NO')\nend\nputs ans"}, {"source_code": "#!/usr/bin/env ruby\n\nq = Integer(gets)\n\nresults = Array.new\nq.times do\n\t\n\tn = Integer(gets)\n\n\tarr = gets.chomp.split(\" \").map(&:to_i)\n\n\tfail = false\n\t(n-1).times do |index|\n\t\tif (arr[index] - arr[index+1]).abs >1 && !(arr[index] ==1 && arr[index+1]== n)\n\t\t\tfail = true\n\t\t\tresults << \"NO\"\n\t\t\tbreak\n\t\tend\n\tend\n\n\tresults << \"YES\" if !fail\nend\n\nresults.each do |result|\n\tputs result\nend\n"}, {"source_code": "t = gets.to_i\nt.times do |index|\n n = gets.to_i\n a = gets.split(\" \").map(&:to_i)\n c = true\n cc = true\n (n - 1).times do |i|\n # p a[i].to_s+\" \"+a[i+1].to_s+\" \"+i.to_s+\" \"+index.to_s\n if !((a[i] == a[i+1] + 1) || (a[i] == 1 && a[i+1] == n))\n # p \"false c\"\n cc = false\n end\n if !((a[i] + 1 == a[i+1]) || (a[i] == n && a[i+1] == 1))\n # p \"false cc\"\n c = false\n end\n end\n p (c || cc) ? \"YES\" : \"NO\"\nend"}, {"source_code": "t = gets.to_i\nt.times do |index|\n n = gets.to_i\n a = gets.split(\" \").map(&:to_i)\n c = true\n cc = true\n (n - 1).times do |i|\n # p a[i].to_s+\" \"+a[i+1].to_s+\" \"+i.to_s+\" \"+index.to_s\n if (a[i] != a[i+1] + 1) && !(a[i] == 1 && a[i+1] == n)\n # p \"false c\"\n c = false\n end\n if (a[i] + 1 != a[i+1]) && !(a[i] == n && a[i+1] == 1)\n # p \"false cc\"\n cc = false\n end\n end\n p (c || cc) ? \"YES\" : \"NO\"\nend"}], "src_uid": "b27436086ead93397be748d8ebdbf19a"} {"nl": {"description": "Let $$$s$$$ be a string of lowercase Latin letters. Its price is the sum of the indices of letters (an integer between 1 and 26) that are included in it. For example, the price of the string abca is $$$1+2+3+1=7$$$.The string $$$w$$$ and the integer $$$p$$$ are given. Remove the minimal number of letters from $$$w$$$ so that its price becomes less than or equal to $$$p$$$ and print the resulting string. Note that the resulting string may be empty. You can delete arbitrary letters, they do not have to go in a row. If the price of a given string $$$w$$$ is less than or equal to $$$p$$$, then nothing needs to be deleted and $$$w$$$ must be output.Note that when you delete a letter from $$$w$$$, the order of the remaining letters is preserved. For example, if you delete the letter e from the string test, you get tst.", "input_spec": "The first line of input contains an integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases in the test. The following are descriptions of $$$t$$$ test cases. Each case consists of two lines. The first of them is the string $$$w$$$, it is non-empty and consists of lowercase Latin letters. Its length does not exceed $$$2\\cdot10^5$$$. The second line contains an integer $$$p$$$ ($$$1 \\le p \\le 5\\,200\\,000$$$). It is guaranteed that the sum of string lengths $$$w$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "Output exactly $$$t$$$ rows, the $$$i$$$-th of them should contain the answer to the $$$i$$$-th set of input data. Print the longest string that is obtained from $$$w$$$ by deleting letters such that its price is less or equal to $$$p$$$. If there are several answers, then output any of them. Note that the empty string  — is one of the possible answers. In this case, just output an empty string.", "sample_inputs": ["5\n\nabca\n\n2\n\nabca\n\n6\n\ncodeforces\n\n1\n\ncodeforces\n\n10\n\ncodeforces\n\n100"], "sample_outputs": ["aa\nabc\n\ncdc\ncodeforces"], "notes": null}, "positive_code": [{"source_code": "inputs = STDIN.read.strip.split(\"\\n\")\r\ninputs.shift\r\n\r\ninputs.each_slice(2) do |inp|\r\n chars = inp[0].split('')\r\n t = inp[1].to_i\r\n cur = 0\r\n res = ''\r\n\r\n list = chars.each_with_index.map { |x, i| [x, x.bytes.first, i] }\r\n list = list.sort{|a, b| a[1] <=> b[1] }\r\n ok = {}\r\n\r\n cur = 0\r\n list.each do |row|\r\n pos = row[1] - 96\r\n break if cur + pos > t\r\n\r\n cur += pos\r\n ok[ row[2] ] = true\r\n end\r\n\r\n chars.each_with_index do |char, index|\r\n next if !ok[index]\r\n\r\n print char\r\n end\r\n\r\n print \"\\n\"\r\nend"}, {"source_code": "def solve(str, cost)\n # greedy solution, delete most expensive chars (lexicographically sized)\n hash = Hash.new { |h,k| h[k] = [] }\n str.split('').each_with_index do |ch, i|\n hash[ch] << i\n end\n\n chars = hash.keys.sort\n\n chars.each do |ch|\n cost_of_char = ch.ord - 'a'.ord + 1\n # max number of chars allowed in str is cost / cost_of_char\n max_num = cost / cost_of_char\n hash[ch] = hash[ch].take(max_num)\n cost -= hash[ch].length * cost_of_char\n end\n\n ans = []\n hash.each do |ch, indices|\n indices.each do |i|\n ans[i] = ch\n end\n end\n ans.join('')\nend\n\ncases = gets.to_i\ncases.times do\n puts solve(gets.chomp, gets.to_i)\nend\n"}, {"source_code": "n = gets.to_i\n\nn.times do\n s = gets.strip\n p = gets.to_i\n\n hashmap = Hash.new(0)\n\n alphabet = ('a'..'z').to_a\n costs = Hash[('a'..'z').each.with_index(1).to_a]\n\n total_cost = 0\n\n s.each_char do |ch|\n hashmap[ch] += 1\n total_cost += costs[ch]\n end\n\n index = alphabet.size - 1\n while (total_cost > p && index > -1)\n count = hashmap[alphabet[index]]\n if count > 0\n hashmap[alphabet[index]] -= 1\n total_cost -= index + 1\n else\n index -= 1\n end\n end\n\n s.each_char do |char|\n next if hashmap[char] == 0\n\n hashmap[char] -= 1\n print(char)\n end\n\n puts\nend\n"}], "negative_code": [{"source_code": "inputs = STDIN.read.strip.split(\"\\n\")\r\ninputs.shift\r\n\r\ninputs.each_slice(2) do |inp|\r\n t = inp[1].to_i\r\n\r\n cur = 0\r\n res = ''\r\n inp[0].bytes.each do |char|\r\n pos = char - 96\r\n next if cur + pos > t\r\n\r\n cur += pos\r\n res += char.chr\r\n end\r\n\r\n puts res\r\nend"}], "src_uid": "cb645c794ee3916b180fc3d789cb7c27"} {"nl": {"description": "Nauuo is a girl who loves playing cards.One day she was playing cards but found that the cards were mixed with some empty ones.There are $$$n$$$ cards numbered from $$$1$$$ to $$$n$$$, and they were mixed with another $$$n$$$ empty cards. She piled up the $$$2n$$$ cards and drew $$$n$$$ of them. The $$$n$$$ cards in Nauuo's hands are given. The remaining $$$n$$$ cards in the pile are also given in the order from top to bottom.In one operation she can choose a card in her hands and play it — put it at the bottom of the pile, then draw the top card from the pile.Nauuo wants to make the $$$n$$$ numbered cards piled up in increasing order (the $$$i$$$-th card in the pile from top to bottom is the card $$$i$$$) as quickly as possible. Can you tell her the minimum number of operations?", "input_spec": "The first line contains a single integer $$$n$$$ ($$$1\\le n\\le 2\\cdot 10^5$$$) — the number of numbered cards. The second line contains $$$n$$$ integers $$$a_1,a_2,\\ldots,a_n$$$ ($$$0\\le a_i\\le n$$$) — the initial cards in Nauuo's hands. $$$0$$$ represents an empty card. The third line contains $$$n$$$ integers $$$b_1,b_2,\\ldots,b_n$$$ ($$$0\\le b_i\\le n$$$) — the initial cards in the pile, given in order from top to bottom. $$$0$$$ represents an empty card. It is guaranteed that each number from $$$1$$$ to $$$n$$$ appears exactly once, either in $$$a_{1..n}$$$ or $$$b_{1..n}$$$.", "output_spec": "The output contains a single integer — the minimum number of operations to make the $$$n$$$ numbered cards piled up in increasing order.", "sample_inputs": ["3\n0 2 0\n3 0 1", "3\n0 2 0\n1 0 3", "11\n0 0 0 5 0 0 0 4 0 0 11\n9 2 6 0 8 1 7 0 3 0 10"], "sample_outputs": ["2", "4", "18"], "notes": "NoteExample 1We can play the card $$$2$$$ and draw the card $$$3$$$ in the first operation. After that, we have $$$[0,3,0]$$$ in hands and the cards in the pile are $$$[0,1,2]$$$ from top to bottom.Then, we play the card $$$3$$$ in the second operation. The cards in the pile are $$$[1,2,3]$$$, in which the cards are piled up in increasing order.Example 2Play an empty card and draw the card $$$1$$$, then play $$$1$$$, $$$2$$$, $$$3$$$ in order."}, "positive_code": [{"source_code": "n = gets.to_i\na = gets.split.map(&:to_i).sort\nb = gets.split.map(&:to_i)\n\nr = n-1\nif i = b.index(1)\n while i < n-1 && b[i+1]-b[i]==1\n i += 1\n end\n if i==n-1\n k = 0\n l = b.index(1)\n while k < l\n if b[k] == 0 || b[k] > b[-1] + k + 1\n k += 1\n else\n break\n end\n end\n\n if k == l\n p l\n exit\n end\n end\nend\n\nans = n\n0.upto(n-1) do |i|\n #p [t,b[i],i+1-t,s]\n if b[i] <= i+1 && b[i] != 0\n tmp = i+1 + n-(b[i]-1)\n ans = tmp if ans < tmp\n end\nend\np ans\n"}], "negative_code": [{"source_code": "n = gets.to_i\na = gets.split.map(&:to_i).sort\nb = gets.split.map(&:to_i)\n\nr = n-1\nif i = b.index(1)\n while i < n-1 && b[i+1]-b[i]==1\n i += 1\n end\n if i==n-1\n k = 0\n l = b.index(1)\n while k < l\n if b[k] > b[-1] + k + 1\n k += 1\n else\n break\n end\n end\n\n if k == l\n p l\n exit\n end\n end\nend\n\nans = n\n0.upto(n-1) do |i|\n #p [t,b[i],i+1-t,s]\n if b[i] <= i+1 && b[i] != 0\n tmp = i+1 + n-(b[i]-1)\n ans = tmp if ans < tmp\n end\nend\np ans\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i).sort\nb = gets.split.map(&:to_i)\n\nr = n-1\nif i = b.index(1)\n while i < n-1 && b[i+1]-b[i]==1\n i += 1\n end\n if i==n-1\n if j = a.index(b[-1]+1)\n k = 0\n x = b[-1]+1\n\n while x <= n\n while b[k] == 0\n k += 1\n end\n\n if j < n && a[j] == x\n j += 1\n elsif b[k] == x\n k += 1\n else\n break\n end\n\n x += 1\n end\n\n if x == n+1\n p b.index(1)\n exit\n end\n elsif b[-1] == n\n p 0\n end\n end\nend\n\nans = n\n0.upto(n-1) do |i|\n #p [t,b[i],i+1-t,s]\n if b[i] <= i+1 && b[i] != 0\n tmp = i+1 + n-(b[i]-1)\n ans = tmp if ans < tmp\n end\nend\np ans\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i).sort\nb = gets.split.map(&:to_i)\n\nr = n-1\nif i = b.index(1)\n while i < n-1 && b[i+1]-b[i]==1\n i += 1\n end\n if i==n-1\n j = a.index(b[-1]+1)\n k = 0\n x = b[-1]+1\n\n while x <= n\n while b[k] == 0\n k += 1\n end\n\n if j && j < n && a[j] == x\n j += 1\n elsif b[k] == x\n k += 1\n else\n break\n end\n\n x += 1\n end\n\n if x == n+1\n p b.index(1)\n exit\n end\n end\nend\n\nans = n\n0.upto(n-1) do |i|\n #p [t,b[i],i+1-t,s]\n if b[i] <= i+1 && b[i] != 0\n tmp = i+1 + n-(b[i]-1)\n ans = tmp if ans < tmp\n end\nend\np ans\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i).sort\nb = gets.split.map(&:to_i)\n\nr = n-1\nif i = b.index(1)\n while i < n-1 && b[i+1]-b[i]==1\n i += 1\n end\n if i==n-1\n r = n-b[-1]-1\n end\nend\n\ni = a.index(n-r) || 0\nwhile i < n-1 && a[i+1]-a[i] <= 1\n i += 1 \nend\nk = a[i]\n#p [k,i,r]\nif a[-1] == 0 && r != -1\n p n+r+2\n exit\nend\nif k == n || r == -1\n p r+1\n exit\nend\nt = b.index(k+1)\ns = t-k\n#p [t,s]\n\nt.upto(r) do |i|\n #p [t,b[i],i+1-t,s]\n if b[i] <= i+1-t && b[i] != 0\n tmp = i - b[i] + 1\n s = tmp if s < tmp\n end\nend\np n+s+1\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i).sort\nb = gets.split.map(&:to_i)\n\nr = n-1\nif i = b.index(1)\n while i < n-1 && b[i+1]-b[i]==1\n i += 1\n end\n if i==n-1\n r = n-b[-1]-1\n end\nend\n\ni = a.index(n-r) || 0\nwhile i < n-1 && a[i+1]-a[i] <= 1\n i += 1 \nend\nk = a[i]\n#p [k,i,r]\nif a[-1] == 0 && r != -1\n p n*2\n exit\nend\nif k == n || r == -1\n p r+1\n exit\nend\nt = b.index(k+1)\ns = t-k\n#p [t,s]\n\nt.upto(r) do |i|\n# p [t,b[t],i+1-t]\n if b[i] < i+1-t && b[i] != 0\n tmp = i - b[i] + 1\n s = tmp if s < tmp\n end\nend\np n+s+1\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i).sort\nb = gets.split.map(&:to_i)\n\nr = n-1\nif i = b.index(1)\n while i < n-1 && b[i+1]-b[i]==1\n i += 1\n end\n if i==n-1\n r = n-b[-1]-1\n end\nend\n\ni = a.index(n-r) || 0\nwhile i < n-1 && a[i+1]-a[i] <= 1\n i += 1 \nend\nk = a[i]\n#p [k,i,r]\nif a[-1] == 0 && r != n-1\n p n+r+2\n exit\nend\nif k == n || r == -1\n p r+1\n exit\nend\nt = b.index(k+1)\ns = t-k\n#p [t,s]\n\nt.upto(r) do |i|\n #p [t,b[i],i+1-t,s]\n if b[i] <= i+1-t && b[i] != 0\n tmp = i - b[i] + 1\n s = tmp if s < tmp\n end\nend\np n+s+1\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i).sort\nb = gets.split.map(&:to_i)\n\nr = n-1\nif i = b.index(1)\n while i < n-1 && b[i+1]-b[i]==1\n i += 1\n end\n if i==n-1\n if j = a.index(b[-1]+1)\n k = 0\n x = b[-1]+1\n\n while x <= n\n while b[k] == 0\n k += 1\n end\n\n if j < n && a[j] == x\n j += 1\n elsif b[k] == x\n k += 1\n else\n break\n end\n\n x += 1\n end\n\n if x == n+1\n p b.index(1)\n exit\n end\n elsif b[-1] == n\n p 0\n exit\n end\n end\nend\n\nans = n\n0.upto(n-1) do |i|\n #p [t,b[i],i+1-t,s]\n if b[i] <= i+1 && b[i] != 0\n tmp = i+1 + n-(b[i]-1)\n ans = tmp if ans < tmp\n end\nend\np ans\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i).sort\nb = gets.split.map(&:to_i)\n\nr = n-1\nif i = b.index(1)\n while i < n-1 && b[i+1]-b[i]==1\n i += 1\n end\n if i==n-1\n r = n-b[-1]-1\n end\nend\n\ni = a.index(n-r) || 0\nwhile i < n-1 && a[i+1]-a[i] <= 1\n i += 1 \nend\nk = a[i]\n#p [k,i,r]\nif a[-1] == 0 && r != -1\n p n+r+2\n exit\nend\nif k == n || r == -1\n p r+1\n exit\nend\nt = b.index(k+1)\ns = t-k\n#p [t,s]\n\nt.upto(r) do |i|\n# p [t,b[t],i+1-t]\n if b[i] < i+1-t && b[i] != 0\n tmp = i - b[i] + 1\n s = tmp if s < tmp\n end\nend\np n+s+1\n"}], "src_uid": "ec092209aa9f45409e5aa01d7fc784e1"} {"nl": {"description": "Polycarpus has a hobby — he develops an unusual social network. His work is almost completed, and there is only one more module to implement — the module which determines friends. Oh yes, in this social network one won't have to add friends manually! Pairs of friends are deduced in the following way. Let's assume that user A sent user B a message at time t1, and user B sent user A a message at time t2. If 0 < t2 - t1 ≤ d, then user B's message was an answer to user A's one. Users A and B are considered to be friends if A answered at least one B's message or B answered at least one A's message.You are given the log of messages in chronological order and a number d. Find all pairs of users who will be considered to be friends.", "input_spec": "The first line of the input contains two integers n and d (1 ≤ n, d ≤ 1000). The next n lines contain the messages log. The i-th line contains one line of the log formatted as \"Ai Bi ti\" (without the quotes), which means that user Ai sent a message to user Bi at time ti (1 ≤ i ≤ n). Ai and Bi are non-empty strings at most 20 characters long, consisting of lowercase letters ('a' ... 'z'), and ti is an integer (0 ≤ ti ≤ 10000). It is guaranteed that the lines are given in non-decreasing order of ti's and that no user sent a message to himself. The elements in the lines are separated by single spaces.", "output_spec": "In the first line print integer k — the number of pairs of friends. In the next k lines print pairs of friends as \"Ai Bi\" (without the quotes). You can print users in pairs and the pairs themselves in any order. Each pair must be printed exactly once.", "sample_inputs": ["4 1\nvasya petya 1\npetya vasya 2\nanya ivan 2\nivan anya 4", "1 1000\na b 0"], "sample_outputs": ["1\npetya vasya", "0"], "notes": "NoteIn the first sample test case Vasya and Petya are friends because their messages' sending times are one second apart. Anya and Ivan are not, because their messages' sending times differ by more than one second."}, "positive_code": [{"source_code": "n, d = gets.split.map(&:to_i)\nh = Hash.new{[]}\ns = {}\nn.times{\n c = gets.split\n t = c.pop.to_i\n s[c.sort] = 0 if h[c.reverse].any?{|_| t - d <= _ && _ < t}\n h[c] <<= t\n}\np s.size\nputs s.keys.map{|_| _ * ' '} * \"\\n\"\n"}, {"source_code": "n, d = gets.split.map(&:to_i)\nans = Hash.new\n\ninput = Array.new\nn.times{\n a, b, t = gets.split\n t = t.to_i\n input.push([a, b, t])\n}\ninput.each{|a|\n input.each{|b|\n if a[0] == b[1] && a[1] == b[0] then\n if (a[2]-b[2]).abs <= d && (a[2]-b[2]).abs > 0 then\n tmp = [a[0],a[1]]\n tmp.sort!\n ans[tmp]=1\n end\n end\n }\n}\np ans.count\nans.each{|a,b|\n puts \"#{a[0]} #{a[1]}\"\n}\n"}, {"source_code": "n, d = gets.split.map(&:to_i)\nh = Hash.new{[]}\ns = {}\nn.times{\n c = gets.split\n t = c.pop.to_i\n s[c.sort] = 0 if h[c.reverse].any?{|_| t - d <= _ && _ < t}\n h[c] <<= t\n}\np s.size\nputs s.keys.map{|_| _ * ' '} * \"\\n\"\n\n"}, {"source_code": "n, d = gets.split.map(&:to_i)\nh = Hash.new{[]}\ns = {}\nn.times{\n c = gets.split\n t = c.pop.to_i\n s[c.sort] = 0 if h[c.reverse].any?{|_| t - d <= _ && _ < t}\n h[c] <<= t\n}\np s.size\nputs s.keys.map{|_| _ * ' '} * \"\\n\"\n\n"}, {"source_code": "n,d = STDIN.gets.chomp.split(/ /).map(&:to_i)\nmessages_raw = n.times.map{ from,to,time=STDIN.gets.chomp.split(/ /); {from: from.to_sym, to: to.to_sym, time: time.to_i } }\nmessages = {}\nfriends = []\n\nmessages_raw.each do |message|\n _ = [message[:from],message[:to]]\n a = _.sort\n if messages[_]\n messages[_].each do |old|\n t = message[:time] - old\n if 0 < t && t <= d\n messages.delete a\n friends << a\n end\n end\n end\n messages[_.reverse] ||= []\n messages[_.reverse] << message[:time]\nend\nfriends.uniq!\n\np friends.size\nputs friends.map{|x| x.join(' ') }\n"}, {"source_code": "n, d = gets.split.map(&:to_i)\nh = Hash.new{[]}\ns = {}\nn.times{\n c = gets.split\n t = c.pop.to_i\n s[c.sort] = 0 if h[c.reverse].any?{|_| t - d <= _ && _ < t}\n h[c] <<= t\n}\np s.size\nputs s.keys.map{|_| _ * ' '} * \"\\n\"\n"}, {"source_code": "n, d = gets.split.map(&:to_i)\nh = Hash.new{[]}\ns = {}\nn.times{\n c = gets.split\n t = c.pop.to_i\n s[c.sort] = 0 if h[c.reverse].any?{|_| t - d <= _ && _ < t}\n h[c] <<= t\n}\np s.size\nputs s.keys.map{|_| _ * ' '} * \"\\n\"\n\n"}, {"source_code": "n, d = gets.split.map(&:to_i)\nh = Hash.new{[]}\ns = {}\nn.times{\n c = gets.split\n t = c.pop.to_i\n s[c.sort] = 0 if h[c.reverse].any?{|_| t - d <= _ && _ < t}\n h[c] <<= t\n}\np s.size\nputs s.keys.map{|_| _ * ' '} * \"\\n\"\n\n"}, {"source_code": "n, d = gets.split.map(&:to_i)\nh = Hash.new{[]}\ns = {}\nn.times{\n c = gets.split\n t = c.pop.to_i\n s[c.sort] = 0 if h[c.reverse].any?{|_| t - d <= _ && _ < t}\n h[c] <<= t\n}\np s.size\nputs s.keys.map{|_| _ * ' '} * \"\\n\""}, {"source_code": "n,m = gets.split.map &:to_i\n\nts = Hash.new{[]}\nans = {}\n\nn.times do\n a = gets.split\n c = a.pop.to_i\n ts[a] <<= c\n\n ans[a.sort] = 0 if ts[a.reverse].any?{ |it| c - it <= m && c > it}\n \n \nend\n\n puts ans.size\n puts ans.keys.map{|_| _ * ' '} * \"\\n\""}, {"source_code": "lines = STDIN.read.split(\"\\n\")\n\n(n, d) = lines[0].split(\" \").map(&:to_i)\n\nlog = lines[1..-1]\n\nmsg = {}\nlog.each{ |line|\n (user1, user2, t) = line.split(\" \")\n t = t.to_i\n \n h = [user1, user2].sort.join(' ')\n \n if line.split(\" \")[0] == h.split(' ')[0] then\n who = 1\n else\n who = 2\n end\n \n if msg.has_key?(h) then\n msg[h][who].push(t);\n else\n msg[h] = {}\n msg[h][1] = []\n msg[h][2] = []\n msg[h][who].push(t)\n end\n}\n\nfriends = 0\npairs = []\n\nmsg.each { |key, value|\n if (value[1].length > 0 && value[2].length > 0) then\n \n found = false\n value[1].each {|v1|\n value[2].each {|v2|\n if (v2 - v1).abs <= d && (v2 - v1 != 0) then\n found = true\n end\n }\n }\n if (found) then\n friends += 1\n pairs.push(key)\n end\n end\n}\n\nputs friends\nputs pairs"}, {"source_code": "def main()\n\tnd=gets.chomp.split(\" \",2)\n\tnd=nd.map{|i| i.to_i}\n\tn=nd[0]\n\td=nd[1]\n\tp1=[]\n\tp2=[]\n\tt=[]\n\tfor i in 0...n\n\t\tinp=gets.chomp.to_s\n\t\tinpa=inp.split(\" \",3)\n\t\tp1 << inpa[0]\n\t\tp2<< inpa[1]\n\t\tt << inpa[2]\n\tend\n\tfriends=[];\n\tfor i in 0...n\n\t\tfp=p1[i]\n\t\tsp=p2[i]\n\t\ttime=t[i]\n\t\tfor j in (i+1)...n\n\t\t\tfps=p1[j]\n\t\t\tsps=p2[j]\n\t\t\ttimes=t[j]\n\t\t\tif fps==sp && sps==fp\n\t\t\t\ttd=times.to_i-time.to_i\n\t\t\t\tif td>0 && td<=d\n\t\t\t\t\tfriends << (fp+\" \"+sp)\n\t\t\t\t\tbreak;\n\t\t\t\tend\n\t\t\tend \n\t\tend\t\n\tend\n\tff=[]\n\tfor i in 0...(friends.length)\n\t\trel=friends[i]\n\t\tsr=rel.split(\" \",2);\n\t\trel2=sr[1]+\" \"+sr[0];\n\t\tif !(ff.include?(rel) || ff.include?(rel2))\n\t\t\tff<< rel\n\t\tend\t\n\tend\n\tputs ff.length\n\tputs ff\nend\n\nmain()\n"}, {"source_code": "n, d = gets.split.map(&:to_i)\nh = Hash.new{[]}\ns = {}\nn.times{\n c = gets.split\n t = c.pop.to_i\n s[c.sort] = 0 if h[c.reverse].any?{|_| t - d <= _ && _ < t}\n h[c] <<= t\n}\np s.size\nputs s.keys.map{|_| _ * ' '} * \"\\n\"\n\n"}, {"source_code": "require 'set'\n\nn, d = gets.split.collect{|i| i.to_i}\nmsg = Hash.new{|h,k| h[k] = []}\nfriends = Set.new\n\nn.times{\n f1, f2, t = gets.split\n t = t.to_i\n msg[[f1,f2]] << t\n msg[[f2,f1]].each{|tp|\n if t - tp <= d and t - tp > 0\n friends.add(f1 < f2 ? [f1,f2] : [f2,f1])\n break\n end\n }\n}\n\nputs friends.size\nfriends.each{|f|\n puts f.join(\" \")\n}\n"}, {"source_code": "n, d = gets.split.map(&:to_i)\nh = Hash.new{[]}\ns = {}\nn.times{\n c = gets.split\n t = c.pop.to_i\n s[c.sort] = 0 if h[c.reverse].any?{|_| t - d <= _ && _ < t}\n h[c] <<= t\n}\np s.size\nputs s.keys.map{|_| _ * ' '} * \"\\n\"\n\n"}, {"source_code": "n, d = gets.split.map(&:to_i)\nh = Hash.new{[]}\ns = {}\nn.times{\n c = gets.split\n t = c.pop.to_i\n s[c.sort] = 0 if h[c.reverse].any?{|_| t - d <= _ && _ < t}\n h[c] <<= t\n}\np s.size\nputs s.keys.map{|_| _ * ' '} * \"\\n\""}, {"source_code": "n, d = gets.split.map(&:to_i)\nh = Hash.new{[]}\ns = {}\nn.times{\n c = gets.split\n t = c.pop.to_i\n s[c.sort] = 0 if h[c.reverse].any?{|_| t - d <= _ && _ < t}\n h[c] <<= t\n}\np s.size\nputs s.keys.map{|_| _ * ' '} * \"\\n\"\n\n"}, {"source_code": "n, d = gets.split.map(&:to_i)\nh = Hash.new{[]}\ns = {}\nn.times{\n c = gets.split\n t = c.pop.to_i\n s[c.sort] = 0 if h[c.reverse].any?{|_| t - d <= _ && _ < t}\n h[c] <<= t\n}\np s.size\nputs s.keys.map{|_| _ * ' '} * \"\\n\"\n\n"}], "negative_code": [{"source_code": "n, d = gets.split.map(&:to_i)\nans = Hash.new\n\ninput = Array.new\nn.times{\n a, b, t = gets.split\n t = t.to_i\n input.push([a, b, t])\n}\ninput.each{|a|\n input.each{|b|\n if a[0] == b[1] && a[1] == b[0] then\n if (a[2]-b[2]).abs <= d then\n tmp = [a[0],a[1]]\n tmp.sort!\n ans[tmp]=1\n end\n end\n }\n}\np ans.count\nans.each{|a,b|\n puts \"#{a[0]} #{a[1]}\"\n}\n\n"}, {"source_code": "n,d = STDIN.gets.chomp.split(/ /).map(&:to_i)\nmessages_raw = n.times.map{ from,to,time=STDIN.gets.chomp.split(/ /); {from: from.to_sym, to: to.to_sym, time: time.to_i } }.sort_by{|x| x[:time] }\nmessages = {}\nfriends = []\n\nmessages_raw.each do |message|\n if messages[message[:from]] && 0 < (message[:time] - messages[message[:from]]) && (message[:time] - messages[message[:from]]) <= d\n messages.delete message[:from]\n friends << [message[:from],message[:to]].sort\n end\n messages[message[:to]] = message[:time]\nend\n\nfriends.uniq!\n\np friends.size\nputs friends.map{|x| x.join(' ') }\n"}, {"source_code": "n,d = STDIN.gets.chomp.split(/ /).map(&:to_i)\nmessages_raw = n.times.map{ from,to,time=STDIN.gets.chomp.split(/ /); {from: from.to_sym, to: to.to_sym, time: time.to_i } }.sort_by{|x| x[:time] }\nmessages = {}\ndone = {}\nfriends = []\n\nmessages_raw.each do |message|\n a = [message[:from],message[:to]].sort\n next if done[a]\n if messages[a] && 0 < (message[:time] - messages[a]) && (message[:time] - messages[a]) <= d\n messages.delete a\n friends << a\n end\n messages[a] = message[:time]\nend\nfriends.uniq!\n\np friends.size\nputs friends.map{|x| x.join(' ') }\n"}, {"source_code": "n,d = STDIN.gets.chomp.split(/ /).map(&:to_i)\nmessages_raw = n.times.map{ from,to,time=STDIN.gets.chomp.split(/ /); {from: from.to_sym, to: to.to_sym, time: time.to_i } }.sort_by{|x| x[:time] }\nmessages = {}\nfriends = []\n\nmessages_raw.each do |message|\n if messages[message[:to]] && (message[:time] - messages[message[:to]]) <= d\n friends << [message[:from],message[:to]].sort\n end\n messages[message[:from]] = message[:time]\nend\n\nfriends.uniq!\n\np friends.size\nputs friends.map{|x| x.join(' ') }\n"}, {"source_code": "n,d = STDIN.gets.chomp.split(/ /).map(&:to_i)\nmessages_raw = n.times.map{ from,to,time=STDIN.gets.chomp.split(/ /); {from: from.to_sym, to: to.to_sym, time: time.to_i } }\nmessages = {}\nfriends = []\n\nmessages_raw.each do |message|\n if messages[message[:to]] && (message[:time] - messages[message[:to]]) <= d\n friends << [message[:from],message[:to]].sort\n end\n messages[message[:from]] = message[:time]\nend\n\nfriends.uniq!\n\np friends.size\nputs friends.map{|x| x.join(' ') }\n"}, {"source_code": "n,d = STDIN.gets.chomp.split(/ /).map(&:to_i)\nmessages_raw = n.times.map{ from,to,time=STDIN.gets.chomp.split(/ /); {from: from.to_sym, to: to.to_sym, time: time.to_i } }\nmessages = {}\ndone = {}\nfriends = []\n\nmessages_raw.each do |message|\n _ = [message[:from],message[:to]]\n a = _.sort\n next if done[a]\n if messages[_]\n t = message[:time] - messages[_]\n messages.delete a\n if 0 < t && t <= d\n friends << a\n end\n end\n messages[_.reverse] = message[:time]\nend\nfriends.uniq!\n\np friends.size\nputs friends.map{|x| x.join(' ') }\n"}, {"source_code": "n,d = STDIN.gets.chomp.split(/ /).map(&:to_i)\nmessages_raw = n.times.map{ from,to,time=STDIN.gets.chomp.split(/ /); {from: from.to_sym, to: to.to_sym, time: time.to_i } }.sort_by{|x| x[:time] }\nmessages = {}\ndone = {}\nfriends = []\n\nmessages_raw.each do |message|\n a = [message[:from],message[:to]].sort\n next if done[a]\n if messages[a]\n t = message[:time] - messages[a]\n messages.delete a\n if 0 < t && t <= d\n friends << a\n end\n end\n messages[a] = message[:time]\nend\nfriends.uniq!\n\np friends.size\nputs friends.map{|x| x.join(' ') }\n"}, {"source_code": "n,d = STDIN.gets.chomp.split(/ /).map(&:to_i)\nmessages_raw = n.times.map{ from,to,time=STDIN.gets.chomp.split(/ /); {from: from.to_sym, to: to.to_sym, time: time.to_i } }.sort_by{|x| x[:time] }\nmessages = {}\nfriends = []\n\nmessages_raw.each do |message|\n if messages[message[:from]] && 0 < (message[:time] - messages[message[:from]]) && (message[:time] - messages[message[:from]]) <= d\n friends << [message[:from],message[:to]].sort\n end\n messages[message[:to]] = message[:time]\nend\n\nfriends.uniq!\n\np friends.size\nputs friends.map{|x| x.join(' ') }\n"}, {"source_code": "n,m = gets.split.map &:to_i\n\nts = Hash.new{[]}\nans = {}\n\nn.times do\n a = gets.split\n c = a.pop.to_i\n ts[a] <<= c\n\n ans[a.sort] = 0 if ts[a.reverse].any?{ |it| c - it <= m }\n \n \nend\n\n puts ans.size\n puts ans.keys.map{|_| _ * ' '} * \"\\n\""}, {"source_code": "lines = STDIN.read.split(\"\\n\")\n\n(n, d) = lines[0].split(\" \").map(&:to_i)\n\nlog = lines[1..-1]\n\nmsg = {}\nlog.each{ |line|\n (user1, user2, t) = line.split(\" \")\n t = t.to_i\n \n h = [user1, user2].sort.join(' ')\n \n if msg.has_key?(h) then\n msg[h].push(t);\n else\n msg[h] = [t]\n end\n}\n\nfriends = 0\npairs = []\nmsg.each{ |k, v|\n if v.length >= 2 then\n sorted = v.sort\n \n for p in (0..(sorted.length - 2))\n if (sorted[p + 1] - sorted[p] <= d) then\n friends += 1\n pairs.push(k)\n break\n end\n end\n \n end\n}\n\nputs friends\nputs pairs"}, {"source_code": "lines = STDIN.read.split(\"\\n\")\n\n(n, d) = lines[0].split(\" \").map(&:to_i)\n\nlog = lines[1..-1]\n\nmsg = {}\nlog.each{ |line|\n (user1, user2, t) = line.split(\" \")\n t = t.to_i\n \n h = [user1, user2].sort.join(' ')\n \n if line.split(\" \")[0] == h.split(' ')[0] then\n who = 1\n else\n who = 2\n end\n \n if msg.has_key?(h) then\n msg[h][who].push(t);\n else\n msg[h] = {}\n msg[h][1] = []\n msg[h][2] = []\n msg[h][who].push(t)\n end\n}\n\nfriends = 0\npairs = []\n\nmsg.each { |key, value|\n if (value[1].length > 0 && value[2].length > 0) then\n \n found = false\n value[1].each {|v1|\n value[2].each {|v2|\n if (v2 - v1).abs <= d && (v2 - v1 != 0) then\n found = true\n end\n }\n }\n if (found) then\n friends += 1\n pairs.push(key)\n end\n end\n}\n\np msg\n\nputs friends\nputs pairs"}, {"source_code": "lines = STDIN.read.split(\"\\n\")\n\n(n, d) = lines[0].split(\" \").map(&:to_i)\n\nlog = lines[1..-1]\n\nmsg = {}\nlog.each{ |line|\n (user1, user2, t) = line.split(\" \")\n t = t.to_i\n \n h = [user1, user2].sort.join(' ')\n \n if msg.has_key?(h) then\n msg[h].push(t);\n else\n msg[h] = [t]\n end\n}\n\nfriends = 0\npairs = []\nmsg.each{ |k, v|\n if v.length >= 2 then\n sorted = v.sort\n \n if sorted[1] - sorted[0] <= d then\n friends += 1\n pairs.push(k)\n end\n \n end\n}\n\nputs friends\nputs pairs"}, {"source_code": "lines = STDIN.read.split(\"\\n\")\n\n(n, d) = lines[0].split(\" \").map(&:to_i)\n\nlog = lines[1..-1]\n\nmsg = {}\nlog.each{ |line|\n (user1, user2, t) = line.split(\" \")\n t = t.to_i\n \n h = [user1, user2].sort.join(' ')\n \n if line.split(\" \")[0] == h.split(' ')[0] then\n who = 1\n else\n who = 2\n end\n \n if msg.has_key?(h) then\n msg[h][who].push(t);\n else\n msg[h] = {}\n msg[h][1] = []\n msg[h][2] = []\n msg[h][who].push(t)\n end\n}\n\nfriends = 0\npairs = []\n\nmsg.each { |key, value|\n if (value[1].length > 0 && value[2].length > 0) then\n \n found = false\n value[1].each {|v1|\n value[2].each {|v2|\n if (v2 - v1).abs <= d then\n found = true\n end\n }\n }\n if (found) then\n friends += 1\n pairs.push(key)\n end\n end\n}\n\nputs friends\nputs pairs"}, {"source_code": "def main()\n\tnd=gets.chomp.split(\" \",2)\n\tnd=nd.map{|i| i.to_i}\n\tn=nd[0]\n\td=nd[1]\n\tp1=[]\n\tp2=[]\n\tt=[]\n\tfor i in 0...n\n\t\tinp=gets.chomp.to_s\n\t\tinpa=inp.split(\" \",3)\n\t\tp1 << inpa[0]\n\t\tp2<< inpa[1]\n\t\tt << inpa[2]\n\tend\n\tfriends=[]\n\tfor i in 0...n\n\t\tfp=p1[i]\n\t\tsp=p2[i]\n\t\ttime=t[i]\n\t\tfor j in (i+1)...n\n\t\t\tfps=p1[j]\n\t\t\tsps=p2[j]\n\t\t\ttimes=t[j]\n\t\t\tif fps==sp && sps==fp\n\t\t\t\ttd=times.to_i-time.to_i\n\t\t\t\tif td>0 && td<=d\n\t\t\t\t\tfriends << (fp+\" \"+sp)\n\t\t\t\t\tbreak;\n\t\t\t\tend\n\t\t\tend \n\t\tend\t\n\tend\n\tputs friends.length\n\tputs friends\nend\n\nmain()\n"}, {"source_code": "def main()\n\tnd=gets.chomp.split(\" \",2)\n\tnd=nd.map{|i| i.to_i}\n\tn=nd[0]\n\td=nd[1]\n\tp1=[]\n\tp2=[]\n\tt=[]\n\tfor i in 0...n\n\t\tinp=gets.chomp.to_s\n\t\tinpa=inp.split(\" \",3)\n\t\tp1 << inpa[0]\n\t\tp2<< inpa[1]\n\t\tt << inpa[2]\n\tend\n\tfriends=[]\n\tfor i in 0...n\n\t\tfp=p1[i]\n\t\tsp=p2[i]\n\t\ttime=t[i]\n\t\tfor j in (i+1)...n\n\t\t\tfps=p1[j]\n\t\t\tsps=p2[j]\n\t\t\ttimes=t[j]\n\t\t\tif fps==sp && sps==fp\n\t\t\t\ttd=times.to_i-time.to_i\n\t\t\t\tif td>0 && td<=d\n\t\t\t\t\tfriends << (fp+\" \"+sp)\n\t\t\t\t\tbreak;\n\t\t\t\telse\n\t\t\t\t\tif friends.include?(fp+\" \"+sp)\n\t\t\t\t\t\tfriends.delete(fp+\" \"+sp)\n\t\t\t\t\tend\n\t\t\t\tend\n\t\t\tend \n\t\tend\t\n\tend\n\tputs friends.length\n\tputs friends\nend\n\nmain()\n"}, {"source_code": "def main()\n\tnd=gets.chomp.split(\" \",2)\n\tnd=nd.map{|i| i.to_i}\n\tn=nd[0]\n\td=nd[1]\n\tp1=[]\n\tp2=[]\n\tt=[]\n\tfor i in 0...n\n\t\tinp=gets.chomp.to_s\n\t\tinpa=inp.split(\" \",3)\n\t\tp1 << inpa[0]\n\t\tp2<< inpa[1]\n\t\tt << inpa[2]\n\tend\n\tfriends=[]\n\tfor i in 0...n\n\t\tfp=p1[i]\n\t\tsp=p2[i]\n\t\ttime=t[i]\n\t\tfor j in (i+1)...n\n\t\t\tfps=p1[j]\n\t\t\tsps=p2[j]\n\t\t\ttimes=t[j]\n\t\t\tif fps==fp && sps==sp\n\t\t\t\tbreak;\n\t\t\tend\n\t\t\tif fps==sp && sps==fp\n\t\t\t\ttd=times.to_i-time.to_i\n\t\t\t\tif td>0 && td<=d\n\t\t\t\t\tfriends << (fp+\" \"+sp)\n\t\t\t\t\tbreak;\n\t\t\t\telse\n\t\t\t\t\tif friends.include?(fp+\" \"+sp)\n\t\t\t\t\t\tfriends.delete(fp+\" \"+sp)\n\t\t\t\t\tend\n\t\t\t\tend\n\t\t\tend \n\t\tend\t\n\tend\n\tputs friends.length\n\tputs friends\nend\n\nmain()\n"}, {"source_code": "def main()\n\tnd=gets.chomp.split(\" \",2)\n\tnd=nd.map{|i| i.to_i}\n\tn=nd[0]\n\td=nd[1]\n\tp1=[]\n\tp2=[]\n\tt=[]\n\tfor i in 0...n\n\t\tinp=gets.chomp.to_s\n\t\tinpa=inp.split(\" \",3)\n\t\tp1 << inpa[0]\n\t\tp2<< inpa[1]\n\t\tt << inpa[2]\n\tend\n\tfriends=[];\n\tnotfriends=[];\n\tfor i in 0...n\n\t\tfp=p1[i]\n\t\tsp=p2[i]\n\t\ttime=t[i]\n\t\tfor j in (i+1)...n\n\t\t\tfps=p1[j]\n\t\t\tsps=p2[j]\n\t\t\ttimes=t[j]\n\t\t\tif fps==fp && sps==sp\n\t\t\t\tnotfriends << (fp+\" \"+sp);\n\t\t\t\tbreak;\n\t\t\tend\n\t\t\tif fps==sp && sps==fp\n\t\t\t\ttd=times.to_i-time.to_i\n\t\t\t\tif td>0 && td<=d\n\t\t\t\t\tfriends << (fp+\" \"+sp)\n\t\t\t\t\tbreak;\n\t\t\t\telse\n\t\t\t\t\tif friends.include?(fp+\" \"+sp)\n\t\t\t\t\t\tfriends.delete(fp+\" \"+sp)\n\t\t\t\t\tend\n\t\t\t\tend\n\t\t\tend \n\t\tend\t\n\tend\n\tff=friends-notfriends\n\tputs ff.length\n\tputs ff\nend\n\nmain()\n"}, {"source_code": "def main()\n\tnd=gets.chomp.split(\" \",2)\n\tnd=nd.map{|i| i.to_i}\n\tn=nd[0]\n\td=nd[1]\n\tp1=[]\n\tp2=[]\n\tt=[]\n\tfor i in 0...n\n\t\tinp=gets.chomp.to_s\n\t\tinpa=inp.split(\" \",3)\n\t\tp1 << inpa[0]\n\t\tp2<< inpa[1]\n\t\tt << inpa[2]\n\tend\n\tfriends=[];\n\tfor i in 0...n\n\t\tfp=p1[i]\n\t\tsp=p2[i]\n\t\ttime=t[i]\n\t\tfor j in (i+1)...n\n\t\t\tfps=p1[j]\n\t\t\tsps=p2[j]\n\t\t\ttimes=t[j]\n\t\t\tif fps==fp && sps==sp\n\t\t\t\tbreak;\n\t\t\tend\n\t\t\tif fps==sp && sps==fp\n\t\t\t\ttd=times.to_i-time.to_i\n\t\t\t\tif td>0 && td<=d\n\t\t\t\t\tfriends << (fp+\" \"+sp)\n\t\t\t\t\tbreak;\n\t\t\t\tend\n\t\t\tend \n\t\tend\t\n\tend\n\tff=[]\n\tfor i in 0...(friends.length)\n\t\trel=friends[i]\n\t\tsr=rel.split(\" \",2);\n\t\trel2=sr[1]+\" \"+sr[0];\n\t\tif !(ff.include?(rel) || ff.include?(rel2))\n\t\t\tff<< rel\n\t\tend\t\n\tend\n\tputs ff.length\n\tputs ff\nend\n\nmain()\n"}, {"source_code": "require 'set'\n\nn, d = gets.split.collect{|i| i.to_i}\nmsg = Hash.new{|h,k| h.has_key?(k) ? h[k] : -(d+1)}\nfriends = Set.new\n\nn.times{\n f1, f2, t = gets.split\n t = t.to_i\n msg[[f1,f2]] = t\n if t - msg[[f2,f1]] <= d and t - msg[[f2,f1]] > 0\n friends.add(f1 < f2 ? [f1,f2] : [f2,f1])\n end\n}\n\nputs friends.size\nfriends.each{|f|\n puts f.join(\" \")\n}\n"}, {"source_code": "require 'set'\n\nn, d = gets.split.collect{|i| i.to_i}\nmsg = Hash.new{|h,k| h.has_key?(k) ? h[k] : -(d+1)}\nfriends = Set.new\n\nn.times{\n f1, f2, t = gets.split\n t = t.to_i\n msg[[f1,f2]] = t\n if t - msg[[f2,f1]] <= d\n friends.add(f1 < f2 ? [f1,f2] : [f2,f1])\n end\n}\n\nputs friends.size\nfriends.each{|f|\n puts f.join(\" \")\n}\n"}], "src_uid": "3cb4c89b174bf5ea51e797b78103e089"} {"nl": {"description": "Maria is the most active old lady in her house. She was tired of sitting at home. She decided to organize a ceremony against the coronavirus.She has $$$n$$$ friends who are also grannies (Maria is not included in this number). The $$$i$$$-th granny is ready to attend the ceremony, provided that at the time of her appearance in the courtyard there will be at least $$$a_i$$$ other grannies there. Note that grannies can come into the courtyard at the same time. Formally, the granny $$$i$$$ agrees to come if the number of other grannies who came earlier or at the same time with her is greater than or equal to $$$a_i$$$.Grannies gather in the courtyard like that. Initially, only Maria is in the courtyard (that is, the initial number of grannies in the courtyard is $$$1$$$). All the remaining $$$n$$$ grannies are still sitting at home. On each step Maria selects a subset of grannies, none of whom have yet to enter the courtyard. She promises each of them that at the time of her appearance there will be at least $$$a_i$$$ other grannies (including Maria) in the courtyard. Maria can call several grannies at once. In this case, the selected grannies will go out into the courtyard at the same moment of time. She cannot deceive grannies, that is, the situation when the $$$i$$$-th granny in the moment of appearing in the courtyard, finds that now there are strictly less than $$$a_i$$$ other grannies (except herself, but including Maria), is prohibited. Please note that if several grannies appeared in the yard at the same time, then each of them sees others at the time of appearance. Your task is to find what maximum number of grannies (including herself) Maria can collect in the courtyard for the ceremony. After all, the more people in one place during quarantine, the more effective the ceremony!Consider an example: if $$$n=6$$$ and $$$a=[1,5,4,5,1,9]$$$, then: at the first step Maria can call grannies with numbers $$$1$$$ and $$$5$$$, each of them will see two grannies at the moment of going out into the yard (note that $$$a_1=1 \\le 2$$$ and $$$a_5=1 \\le 2$$$); at the second step, Maria can call grannies with numbers $$$2$$$, $$$3$$$ and $$$4$$$, each of them will see five grannies at the moment of going out into the yard (note that $$$a_2=5 \\le 5$$$, $$$a_3=4 \\le 5$$$ and $$$a_4=5 \\le 5$$$); the $$$6$$$-th granny cannot be called into the yard  — therefore, the answer is $$$6$$$ (Maria herself and another $$$5$$$ grannies). ", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases in the input. Then test cases follow. The first line of a test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$) — the number of grannies (Maria is not included in this number). The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le 2\\cdot10^5$$$). It is guaranteed that the sum of the values $$$n$$$ over all test cases of the input does not exceed $$$10^5$$$.", "output_spec": "For each test case, print a single integer $$$k$$$ ($$$1 \\le k \\le n + 1$$$) — the maximum possible number of grannies in the courtyard.", "sample_inputs": ["4\n5\n1 1 2 2 1\n6\n2 3 4 5 6 7\n6\n1 5 4 5 1 9\n5\n1 2 3 5 6"], "sample_outputs": ["6\n1\n6\n4"], "notes": "NoteIn the first test case in the example, on the first step Maria can call all the grannies. Then each of them will see five grannies when they come out. Therefore, Maria and five other grannies will be in the yard.In the second test case in the example, no one can be in the yard, so Maria will remain there alone.The third test case in the example is described in the details above.In the fourth test case in the example, on the first step Maria can call grannies with numbers $$$1$$$, $$$2$$$ and $$$3$$$. If on the second step Maria calls $$$4$$$ or $$$5$$$ (one of them), then when a granny appears in the yard, she will see only four grannies (but it is forbidden). It means that Maria can't call the $$$4$$$-th granny or the $$$5$$$-th granny separately (one of them). If she calls both: $$$4$$$ and $$$5$$$, then when they appear, they will see $$$4+1=5$$$ grannies. Despite the fact that it is enough for the $$$4$$$-th granny, the $$$5$$$-th granny is not satisfied. So, Maria cannot call both the $$$4$$$-th granny and the $$$5$$$-th granny at the same time. That is, Maria and three grannies from the first step will be in the yard in total."}, "positive_code": [{"source_code": "t = gets.to_i\n\n1.upto t do\n c = 0\n n = gets.to_i\n x = gets.split.map(&:to_i)\n x.sort!\n x.reverse!\n i = 0\n while x[i] > x.length-i do\n c += 1\n i += 1\n if i == x.length\n break\n end\n end\n puts (x.length + 1 - c)\nend"}, {"source_code": "gets.to_i.times do\n n = gets.to_i\n a = gets.split(' ').map(&:to_i).sort.reverse\n\n while a.first > a.size\n a.shift\n break if a.empty?\n end\n\n puts a.size + 1\nend\n"}], "negative_code": [], "src_uid": "718cea81f609055cece58cae5310f703"} {"nl": {"description": "Shaass has decided to hunt some birds. There are n horizontal electricity wires aligned parallel to each other. Wires are numbered 1 to n from top to bottom. On each wire there are some oskols sitting next to each other. Oskol is the name of a delicious kind of birds in Shaass's territory. Supposed there are ai oskols sitting on the i-th wire. Sometimes Shaass shots one of the birds and the bird dies (suppose that this bird sat at the i-th wire). Consequently all the birds on the i-th wire to the left of the dead bird get scared and jump up on the wire number i - 1, if there exists no upper wire they fly away. Also all the birds to the right of the dead bird jump down on wire number i + 1, if there exists no such wire they fly away. Shaass has shot m birds. You're given the initial number of birds on each wire, tell him how many birds are sitting on each wire after the shots.", "input_spec": "The first line of the input contains an integer n, (1 ≤ n ≤ 100). The next line contains a list of space-separated integers a1, a2, ..., an, (0 ≤ ai ≤ 100). The third line contains an integer m, (0 ≤ m ≤ 100). Each of the next m lines contains two integers xi and yi. The integers mean that for the i-th time Shaass shoot the yi-th (from left) bird on the xi-th wire, (1 ≤ xi ≤ n, 1 ≤ yi). It's guaranteed there will be at least yi birds on the xi-th wire at that moment.", "output_spec": "On the i-th line of the output print the number of birds on the i-th wire.", "sample_inputs": ["5\n10 10 10 10 10\n5\n2 5\n3 13\n2 12\n1 13\n4 6", "3\n2 4 1\n1\n2 2"], "sample_outputs": ["0\n12\n5\n0\n16", "3\n0\n3"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\nar = [0]\ngets.split(' ').map(&:to_i).map{|x| ar << x}\nar << 0\nm = gets.to_i\nm.times do\n x, y = gets.split(' ').map(&:to_i)\n ar[x-1] += y-1\n ar[x+1] += ar[x]-y\n ar[x] = 0\nend\n(1..n).each{|x| puts ar[x]}"}, {"source_code": "sin = STDIN\nsout = STDOUT\n# sin = File.open(\"test.in\", \"r\")\n# sout = File.open(\"test.out\", \"w\")\n\nn = sin.readline.to_i\na = sin.readline.split.map{|e| e.to_i}\nm = sin.readline.to_i\n\nm.times {\n x, y = *sin.readline.split.map{|e| e.to_i}\n x -= 1\n if !x.zero?\n a[x - 1] += y - 1\n end\n if !(x == a.size - 1)\n a[x + 1] += a[x] - y\n end\n a[x] = 0\n}\n\nsout.puts a"}, {"source_code": "#!/usr/bin/env ruby\nn = gets.to_i\ntemp = gets.split(' ').map {|i| i.to_i}\nm = gets.to_i\nm.times do\n x, y = gets.split(' ').map {|i| i.to_i}\n x -= 1;\n temp[x-1] += y - 1 if x > 0\n temp[x+1] += temp[x] - y if x < n - 1\n temp[x] = 0\nend\ntemp.each do |i|\n puts i\nend\n"}, {"source_code": "def run\n n = $stdin.gets.to_i\n a = $stdin.gets.split.map(&:to_i)\n m = $stdin.gets.to_i\n $stdin.readlines.each do |line|\n x, y = line.split.map(&:to_i)\n\n a[x - 2] += y - 1 if x > 1\n a[x] += (a[x - 1] - y) if x < n\n a[x - 1] = 0\n end\n\n puts a.join(\"\\n\")\nend\n\nrun if $0 == __FILE__\n"}, {"source_code": "n = gets.to_i\na = gets.split.map{|_|_.to_i}\nm = gets.to_i\n\n\nm.times{|i|\n x,y=gets.split.map{|_|_.to_i}\n r = a[x-1] - y\n l = y - 1\n a[x-1] = 0\n if n!=1 then\n if x-1 == 0 then\n a[x-1+1] += r\n elsif x-1 == n-1 then\n a[x-1-1] += l\n else\n a[x-1+1] += r\n a[x-1-1] += l\n end\n end\n}\na.each{|i|\n puts i\n}\n\n\n\n"}, {"source_code": "require 'set'\nrequire 'rational'\nrequire 'mathn'\ninclude Math\n$stdin = File.open(\"./input.txt\", \"r\") if ENV[\"USER\"] == \"new\"\n# ====================================\n\nn = gets.to_i\na = [0] + gets.split.map(&:to_i) + [0]\nm = gets.to_i\ncoords = []\n\nm.times do\n x, y = gets.split.map(&:to_i)\n \n left = y - 1\n right = a[x] - left - 1\n \n a[x-1]+=left if x-1>=0\n a[x+1]+=right if x+1=0\n a[x+1]+=right if x+1 0 then a[x-1] += y-1 end\n if x < n-1 then a[x+1] += a[x]-y end \n a[x] = 0\n}\nputs a"}, {"source_code": "n = gets.to_i\narr = gets.split(\" \").collect! {|x| x.to_i}\nm = gets.to_i\n\n\n(0...m).each { |k|\n\tsy,sx = gets.split(\" \").collect! {|x| x.to_i}\n\tleft = sx - 1\n\tright = arr[sy-1] - sx\n\tif sy>1\n\t\tarr[sy-2] += left\n\tend\n\tif sy 0\n a[x-1] += y-1\n end\n if x < n-1\n a[x+1] += a[x] - y\n end\n a[x] = 0\n}\na.each { |x| puts x }\n"}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\ngets.to_i.times {\n x,y=gets.split.map(&:to_i)\n a[x-2]+=y-1 if x>1\n a[x]+=a[x-1]-y if x 1\n a[x] += a[x-1] - y if x < n\n a[x-1] = 0\n}\nputs a\n"}, {"source_code": "wires_count = Integer(gets.chomp)\nbirds_on_wires = gets.chomp.split.map { |x| x.to_i }\nfor _ in 1..Integer(gets.chomp)\n wire, bird = gets.chomp.split.map { |x| x.to_i }\n birds_on_wires[wire - 2] += bird - 1 if wire > 1\n birds_on_wires[wire] += birds_on_wires[wire - 1] - bird if wire < wires_count\n birds_on_wires[wire - 1] = 0\nend\nputs birds_on_wires"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\ngets.to_i.times do\n x, y = gets.split.map(&:to_i)\n x -= 1\n a[x - 1] += y - 1 if x - 1 >= 0\n a[x + 1] += a[x] - y if x + 1 < n\n a[x] = 0\nend\nputs a\n"}, {"source_code": "at_exit do\n # my code\n n = get_numbers_from_line.first\n as = get_numbers_from_line\n m = get_numbers_from_line.first\n shoots = []\n m.times do\n shoots << get_numbers_from_line[0..1]\n end\n\n #trace as: as\n (0...m).each do |i|\n x,y = shoots[i]\n x -= 1\n #trace x: x, y:y\n as[x-1] += y-1 if x-1 >= 0\n as[x+1] += as[x] - y if x+1 < n\n as[x] = 0\n\n #trace as: as\n end\n\n as.each do |a|\n puts a\n end\n\nend\n\n# User helpers\n\n\n# Helpers\n\ndef get_numbers_from_line\n gets.strip.split.map(&:to_i)\nend\n\ndef trace(vars={})\n puts vars.map{|key, value| \"#{key}: #{value}\"}.join('; ')\nend"}, {"source_code": "n = gets.to_i\n\na = gets.split.map { |x| x.to_i }\na = [0] + a + [0]\n\nm = gets.to_i\nm.times do\n\tx, y = gets.split.map { |x| x.to_i }\n\ta[x - 1] += y - 1\n\ta[x + 1] += a[x] - y\n\ta[x] = 0\nend\n\n1.upto(n) do |i|\n\tputs a[i]\nend"}], "negative_code": [{"source_code": "sin = STDIN\nsout = STDOUT\n# sin = File.open(\"test.in\", \"r\")\n# sout = File.open(\"test.out\", \"w\")\n\nn = sin.readline.to_i\na = sin.readline.split.map{|e| e.to_i}\nm = sin.readline.to_i\n\nm.times {\n x, y = *sin.readline.split.map{|e| e.to_i}\n x -= 1\n a[x - 1] += y - 1\n a[x + 1] += a[x] - y\n a[x] = 0\n}\n\nsout.puts a"}, {"source_code": "#!/usr/bin/env ruby\nn = gets.to_i\ntemp = gets.split(' ').map {|i| i.to_i}\nm = gets.to_i\nm.times do\n x, y = gets.split(' ').map {|i| i.to_i}\n x -= 1;\n temp[x-1] += y - 1 if x > 0\n temp[x+1] += temp[x] - y if x < m - 1\n temp[x] = 0\nend\ntemp.each do |i|\n puts i\nend\n"}, {"source_code": "n = gets.to_i\na = gets.split.map{|_|_.to_i}\nm = gets.to_i\n\nif n == 1 then\n puts 0\nelse\n m.times{|i|\n x,y=gets.split.map{|_|_.to_i}\n r = a[x-1] - y\n l = y - 1\n a[x-1] = 0\n if x-1 == 0 then\n a[x-1+1] += r\n elsif x-1 == n-1 then\n a[x-1-1] += l\n else\n a[x-1+1] += r\n a[x-1-1] += l\n end\n }\n a.each{|i|\n puts i\n }\nend\n\n\n"}, {"source_code": "at_exit do\n # my code\n n = get_numbers_from_line.first\n as = get_numbers_from_line\n m = get_numbers_from_line.first\n shoots = []\n m.times do\n shoots << get_numbers_from_line[0..1]\n end\n\n (0...m).each do |i|\n x,y = shoots[i]\n x -= 1\n as[x-1] += y-1 if x-1 >= 0\n as[x+1] += as[x] - y if x+1 < m\n as[x] = 0\n end\n\n as.each do |a|\n puts a\n end\n\nend\n\n# User helpers\n\n\n# Helpers\n\ndef get_numbers_from_line\n gets.strip.split.map(&:to_i)\nend\n\ndef trace(vars={})\n puts vars.map{|key, value| \"#{key}: #{value}\"}.join('; ')\nend"}], "src_uid": "859d66fc2c204a8c002012b1fb206645"} {"nl": {"description": "Tanya wants to go on a journey across the cities of Berland. There are $$$n$$$ cities situated along the main railroad line of Berland, and these cities are numbered from $$$1$$$ to $$$n$$$. Tanya plans her journey as follows. First of all, she will choose some city $$$c_1$$$ to start her journey. She will visit it, and after that go to some other city $$$c_2 > c_1$$$, then to some other city $$$c_3 > c_2$$$, and so on, until she chooses to end her journey in some city $$$c_k > c_{k - 1}$$$. So, the sequence of visited cities $$$[c_1, c_2, \\dots, c_k]$$$ should be strictly increasing.There are some additional constraints on the sequence of cities Tanya visits. Each city $$$i$$$ has a beauty value $$$b_i$$$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $$$c_i$$$ and $$$c_{i + 1}$$$, the condition $$$c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$$$ must hold.For example, if $$$n = 8$$$ and $$$b = [3, 4, 4, 6, 6, 7, 8, 9]$$$, there are several three possible ways to plan a journey: $$$c = [1, 2, 4]$$$; $$$c = [3, 5, 6, 8]$$$; $$$c = [7]$$$ (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above.Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?", "input_spec": "The first line contains one integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) — the number of cities in Berland. The second line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ ($$$1 \\le b_i \\le 4 \\cdot 10^5$$$), where $$$b_i$$$ is the beauty value of the $$$i$$$-th city.", "output_spec": "Print one integer — the maximum beauty of a journey Tanya can choose.", "sample_inputs": ["6\n10 7 1 9 10 15", "1\n400000", "7\n8 9 26 11 12 29 14"], "sample_outputs": ["26", "400000", "55"], "notes": "NoteThe optimal journey plan in the first example is $$$c = [2, 4, 5]$$$.The optimal journey plan in the second example is $$$c = [1]$$$.The optimal journey plan in the third example is $$$c = [3, 6]$$$."}, "positive_code": [{"source_code": "N = gets.to_i\nB = gets.split.map(&:to_i)\n\nbeauty = Hash.new(0)\n\nB.each_with_index do |b, i|\n id = b - i\n\n beauty[id] += b\nend\n\nputs beauty.values.max\n"}], "negative_code": [], "src_uid": "aab8d5a2d42b4199310f3f535a6b3bd7"} {"nl": {"description": "You are given a description of a depot. It is a rectangular checkered field of n × m size. Each cell in a field can be empty (\".\") or it can be occupied by a wall (\"*\"). You have one bomb. If you lay the bomb at the cell (x, y), then after triggering it will wipe out all walls in the row x and all walls in the column y.You are to determine if it is possible to wipe out all walls in the depot by placing and triggering exactly one bomb. The bomb can be laid both in an empty cell or in a cell occupied by a wall.", "input_spec": "The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and columns in the depot field. The next n lines contain m symbols \".\" and \"*\" each — the description of the field. j-th symbol in i-th of them stands for cell (i, j). If the symbol is equal to \".\", then the corresponding cell is empty, otherwise it equals \"*\" and the corresponding cell is occupied by a wall.", "output_spec": "If it is impossible to wipe out all walls by placing and triggering exactly one bomb, then print \"NO\" in the first line (without quotes). Otherwise print \"YES\" (without quotes) in the first line and two integers in the second line — the coordinates of the cell at which the bomb should be laid. If there are multiple answers, print any of them.", "sample_inputs": ["3 4\n.*..\n....\n.*..", "3 3\n..*\n.*.\n*..", "6 5\n..*..\n..*..\n*****\n..*..\n..*..\n..*.."], "sample_outputs": ["YES\n1 2", "NO", "YES\n3 3"], "notes": null}, "positive_code": [{"source_code": "n, m = gets.split.map(&:to_i)\n\nrd = Array.new(n+1, 0)\nst = Array.new(m+1, 0)\ns = Array.new(n+1)\nall = 0\n(n).times do |i|\n s[i] = gets.chomp.to_s\n (m).times do |j|\n if s[i][j]=='*'\n rd[i]+=1\n st[j]+=1\n all+=1\n end\n end\nend\n\n(n).times do |i|\n (m).times do |j|\n if s[i][j]=='*'\n if rd[i] + st[j]-1==all\n print(\"YES\\n#{i+1} #{j+1}\")\n exit(0)\n end\n else\n if rd[i]+st[j]==all\n print(\"YES\\n#{i+1} #{j+1}\")\n exit(0)\n end\n end\n end\nend\n\nprint(\"NO\")"}, {"source_code": "n,m = gets.split.map(&:to_i)\n\nmas = Array.new\nstl = Array.new(m,0)\nstr = Array.new(n,0)\ncnt = 0\ni = 0\nn.times do\n mas << gets.chomp\n mas[i].each_char.with_index do |px, ind|\n if px=='*'\n str[i] += 1\n stl[ind] += 1\n cnt += 1\n end\n end\n i+=1\nend\n\nfor i in 0...n do\n for j in 0...m do\n c = str[i]+stl[j]\n c-=1 if mas[i][j]=='*'\n if c == cnt then\n puts \"YES\\n#{i+1} #{j+1}\"\n exit\n end\n end\nend\n\nputs \"NO\"\n"}, {"source_code": "n, m = gets.chomp.split.map(&:to_i)\nfields = []\nrows = Array.new(n) { 0 }\ncols = Array.new(m) { 0 }\ncount = 0\nn.times do |i|\n fields << gets.chomp.chars\n fields[i].each.with_index do |point, idx|\n if point == '*'\n rows[i] += 1\n cols[idx] += 1\n count += 1\n end\n end\nend\n\nans = (0...n).to_a.product((0...m).to_a).find do |i, j|\n count == rows[i] + cols[j] - (fields[i][j] == '*' ? 1 : 0)\nend\n\nif ans.nil?\n puts \"NO\"\nelse\n puts \"YES\"\n puts ans.flatten.map { |d| d + 1 }.join(' ')\nend\n"}, {"source_code": "# Codeforces My Practice\n# author: Leonardone @ NEETSDKASU\n############################################################\ndef gs() gets.strip end\ndef gi() gets.to_i end\ndef gss() gs.split end\ndef gis() gss.map(&:to_i) end\ndef nmapf(n,f) n.times.map{ __send__ f } end\ndef ngs(n) nmapf n,:gs end\ndef ngi(n) nmapf n,:gi end\ndef ngss(n) nmapf n,:gss end\ndef ngis(n) nmapf n,:gis end\ndef arr2d(h,w,v=0) h.times.map{[v] * w} end\ndef for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end\ndef nsum(n) n * (n + 1) / 2 end\ndef vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end\n############################################################\n\nN, M = gis\nW = ngs N\n\nrows = [0] * N\ncols = [0] * M\n\ncnt = 0\n\nW.each_with_index do |s, y|\n M.times do |x|\n next if s[x] != '*'\n cnt += 1\n rows[y] += 1\n cols[x] += 1\n end\nend\n\nmax_c, max_x = cols.each_with_index.max_by{|v, _| v }\nmax_r, max_y = rows.each_with_index.max_by{|v, _| v }\n\nif max_c == 1 && max_r == 1 && cnt == 2\n c = cols.each_with_index.select{|v,_| v == max_c}.map{|_,i| i}\n r = rows.each_with_index.select{|v,_| v == max_c}.map{|_,i| i}\n r.each do |y|\n c.each do |x|\n if W[y][x] == '.'\n max_x = x\n max_y = y\n end\n end\n end\nend\nif max_c == 1 && max_r > 1\n yy = rows.find_index 1\n max_x = W[yy].index '*' if !yy.nil?\nend\nif max_c > 1 && max_r == 1\n xx = cols.find_index 1\n max_y = W.find_index{|s| s[xx] == '*'} if !xx.nil?\nend\n\ncrs = max_c + max_r - (W[max_y][max_x] == '*' ? 1 : 0)\n\nif crs == cnt\n puts :YES\n puts \"%d %d\" % [max_y + 1, max_x + 1]\nelse\n puts :NO\nend\n\n"}, {"source_code": "numbers = gets\n\nres = numbers.split(' ')\n\nn = res[0].to_i\nm = res[1].to_i\n\nwallsx = []\nwallsy = []\ni = 0\nwalls = 0\n\nwhile (i < n) do\n chars = gets\n chars.each_char.with_index do |r,j|\n if (r == '*')\n wallsx.push(i);\n wallsy.push(j);\n walls += 1\n end\n end\n i = i + 1\n if (walls >= n+m+3)\n i = n\n end\nend\n\ni = 0\nxm = -1\nym = -1\nj = 0\nwhile (j < walls)\n i = j + 1\n while (i < walls) do\n if (i != 0)\n if (wallsx[i] == wallsx[j])\n xm = wallsx[i]\n end\n if (wallsy[i] == wallsy[j])\n ym = wallsy[i]\n end\n \n if (xm != -1 && ym != -1)\n i = walls;\n j = walls;\n end\n end\n i = i + 1\n end\n j = j+1\nend\nif (walls == 0)\n xm = 0\n ym = 0\nelsif (walls == 1)\n xm = wallsx[0]\n ym = wallsy[0]\nelse\n if (xm == -1 && ym == -1) \n xm = wallsx[0]\n ym = wallsy[1]\n end\nend\ni = 0\nflag = 0\n#puts 'unclear' + xm.to_s + ' : ' + ym.to_s\nwhile (i < walls) do\n if (wallsx[i] == xm || wallsy[i] == ym)\n #good, wall destroyed\n else\n flag = 1\n end\n i = i + 1\nend\nif (flag == 1 && (xm == -1 || ym == -1)) \n i = 0\n flag = 0\n #puts 'unclear' + xm.to_s + ' : ' + ym.to_s\n coords_unclear = 0\n while(i < walls)\n if (xm != -1 && wallsx[i] == xm || wallsy[i] == ym && ym != -1)\n #good, wall destroyed\n else\n if (coords_unclear == 0)\n if (xm == -1)\n xm = wallsx[i]\n else\n ym = wallsy[i]\n end\n coords_unclear = 1\n else\n flag = 1\n end\n end\n i = i + 1\n end\nend\nif flag == 1\n puts 'NO'\nelse\n puts 'YES'\n xm = 0 if xm == -1\n ym = 0 if ym == -1\n xm +=1\n ym +=1\n puts (xm).to_s + ' ' + (ym).to_s\nend"}, {"source_code": "n, m = gets.split.map(&:to_i)\nmap = []\ncx = Array.new(n, 0)\ncy = Array.new(m, 0)\nn.times do |i|\n map[i] = gets\n map[i].each_char.with_index do |c, j|\n next unless c == '*'\n cx[i] += 1\n cy[j] += 1\n end\nend\ntot = cx.inject(:+)\nn.times do |i|\n m.times do |j|\n cnt = cx[i] + cy[j]\n cnt -= 1 if map[i][j] == '*'\n if cnt == tot\n puts 'YES'\n puts [i + 1, j + 1] * ' '\n exit\n end\n end\nend\n\nputs 'NO'\n"}], "negative_code": [{"source_code": "n,m = gets.split.map(&:to_i)\n\nmas = Array.new\n\nn.times do\n mas << gets.chomp\nend\n\nstl = Array.new\nstr = Array.new\n\nfor i in 0...n do\n c = 0\n for j in 0...m do\n c+=1 if mas[i][j]=='*'\n end\n str[i] = c\nend\n\nfor j in 0...m do\n c = 0\n for i in 0...n do\n c+=1 if mas[i][j]=='*'\n end\n stl[j] = c\nend\n\ncnt = 0\n\nfor i in 0...n do\n for j in 0...m do\n cnt+=1 if mas[i][j]=='*'\n end\nend\n\nfor i in 0...n do\n for j in 0...m do\n if str[i]+stl[j] >= cnt then\n puts \"YES\\n#{i+1} #{j+1}\"\n exit\n end\n end\nend\n\nputs \"NO\"\n"}, {"source_code": "# Codeforces My Practice\n# author: Leonardone @ NEETSDKASU\n############################################################\ndef gs() gets.strip end\ndef gi() gets.to_i end\ndef gss() gs.split end\ndef gis() gss.map(&:to_i) end\ndef nmapf(n,f) n.times.map{ __send__ f } end\ndef ngs(n) nmapf n,:gs end\ndef ngi(n) nmapf n,:gi end\ndef ngss(n) nmapf n,:gss end\ndef ngis(n) nmapf n,:gis end\ndef arr2d(h,w,v=0) h.times.map{[v] * w} end\ndef for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end\ndef nsum(n) n * (n + 1) / 2 end\ndef vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end\n############################################################\n\nN, M = gis\nW = ngs N\n\nrows = [0] * N\ncols = [0] * M\n\ncnt = 0\n\nW.each_with_index do |s, y|\n M.times do |x|\n next if s[x] != '*'\n cnt += 1\n rows[y] += 1\n cols[x] += 1\n end\nend\n\nif cnt == 2\n puts :YES\n c = cols.each_with_index.select{|v, _| v > 0}.map{|_, i| i}\n r = rows.each_with_index.select{|v, _| v > 0}.map{|_, i| i}\n if c.size == 2 && r.size == 2\n if W[r[0]][c[0]] == '*'\n puts \"%d %d\" % [r[0] + 1, c[1] + 1]\n else\n puts \"%d %d\" % [r[0] + 1, c[0] + 1]\n end\n else\n puts \"%d %d\" % [r[0] + 1, r[1] + 1]\n end\n exit\nend\n\nmax_c, max_x = cols.each_with_index.max_by{|v, _| v }\nmax_r, max_y = rows.each_with_index.max_by{|v, _| v }\n\ncrs = max_c + max_r - (W[max_y][max_x] == '*' ? 1 : 0)\n\nif crs == cnt\n puts :YES\n puts \"%d %d\" % [max_y + 1, max_x + 1]\nelse\n puts :NO\nend\n\n"}, {"source_code": "# Codeforces My Practice\n# author: Leonardone @ NEETSDKASU\n############################################################\ndef gs() gets.strip end\ndef gi() gets.to_i end\ndef gss() gs.split end\ndef gis() gss.map(&:to_i) end\ndef nmapf(n,f) n.times.map{ __send__ f } end\ndef ngs(n) nmapf n,:gs end\ndef ngi(n) nmapf n,:gi end\ndef ngss(n) nmapf n,:gss end\ndef ngis(n) nmapf n,:gis end\ndef arr2d(h,w,v=0) h.times.map{[v] * w} end\ndef for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end\ndef nsum(n) n * (n + 1) / 2 end\ndef vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end\n############################################################\n\nN, M = gis\nW = ngs N\n\nrows = [0] * N\ncols = [0] * M\n\ncnt = 0\n\nW.each_with_index do |s, y|\n M.times do |x|\n next if s[x] != '*'\n cnt += 1\n rows[y] += 1\n cols[x] += 1\n end\nend\n\nif cnt == 2\n puts :YES\n c = cols.each_with_index.select{|v, _| v > 0}.map{|_, i| i}\n r = rows.each_with_index.select{|v, _| v > 0}.map{|_, i| i}\n if c.size == 2 && r.size == 2\n if W[r[0]][c[0]] == '*'\n puts \"%d %d\" % [r[0] + 1, c[1] + 1]\n else\n puts \"%d %d\" % [r[0] + 1, c[0] + 1]\n end\n else\n puts \"%d %d\" % [r[0] + 1, c[0] + 1]\n end\n exit\nend\n\nmax_c, max_x = cols.each_with_index.max_by{|v, _| v }\nmax_r, max_y = rows.each_with_index.max_by{|v, _| v }\n\ncrs = max_c + max_r - (W[max_y][max_x] == '*' ? 1 : 0)\n\nif crs == cnt\n puts :YES\n puts \"%d %d\" % [max_y + 1, max_x + 1]\nelse\n puts :NO\nend\n\n"}, {"source_code": "# Codeforces My Practice\n# author: Leonardone @ NEETSDKASU\n############################################################\ndef gs() gets.strip end\ndef gi() gets.to_i end\ndef gss() gs.split end\ndef gis() gss.map(&:to_i) end\ndef nmapf(n,f) n.times.map{ __send__ f } end\ndef ngs(n) nmapf n,:gs end\ndef ngi(n) nmapf n,:gi end\ndef ngss(n) nmapf n,:gss end\ndef ngis(n) nmapf n,:gis end\ndef arr2d(h,w,v=0) h.times.map{[v] * w} end\ndef for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end\ndef nsum(n) n * (n + 1) / 2 end\ndef vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end\n############################################################\n\nN, M = gis\nW = ngs N\n\nrows = [0] * N\ncols = [0] * M\n\ncnt = 0\n\nW.each_with_index do |s, y|\n M.times do |x|\n next if s[x] != '*'\n cnt += 1\n rows[y] += 1\n cols[x] += 1\n end\nend\n\nmax_c, max_x = cols.each_with_index.max_by{|v, _| v }\nmax_r, max_y = rows.each_with_index.max_by{|v, _| v }\n\nif max_c == 1 && max_r > 1\n yy = rows.find_index 1\n max_x = W[yy].index '*' if !yy.nil?\nend\nif max_c > 1 && max_r == 1\n xx = cols.find_index 1\n max_y = W.find_index{|s| s[xx] == '*'} if !xx.nil?\nend\n\ncrs = max_c + max_r - (W[max_y][max_x] == '*' ? 1 : 0)\n\nif crs == cnt\n puts :YES\n puts \"%d %d\" % [max_y + 1, max_x + 1]\nelse\n puts :NO\nend\n\n"}, {"source_code": "# Codeforces My Practice\n# author: Leonardone @ NEETSDKASU\n############################################################\ndef gs() gets.strip end\ndef gi() gets.to_i end\ndef gss() gs.split end\ndef gis() gss.map(&:to_i) end\ndef nmapf(n,f) n.times.map{ __send__ f } end\ndef ngs(n) nmapf n,:gs end\ndef ngi(n) nmapf n,:gi end\ndef ngss(n) nmapf n,:gss end\ndef ngis(n) nmapf n,:gis end\ndef arr2d(h,w,v=0) h.times.map{[v] * w} end\ndef for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end\ndef nsum(n) n * (n + 1) / 2 end\ndef vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end\n############################################################\n\nN, M = gis\nW = ngs N\n\nrows = [0] * N\ncols = [0] * M\n\ncnt = 0\n\nW.each_with_index do |s, y|\n M.times do |x|\n next if s[x] != '*'\n cnt += 1\n rows[y] += 1\n cols[x] += 1\n end\nend\n\nmax_c, max_x = cols.each_with_index.max_by{|v,_| v }\nmax_r, max_y = rows.each_with_index.max_by{|v,_| v }\n\ncrs = max_c + max_r - (W[max_y][max_x] == '*' ? 1 : 0)\n\nif crs == cnt\n puts :YES\n puts \"%d %d\" % [max_y + 1, max_x + 1]\nelse\n puts :NO\nend\n\n"}, {"source_code": "# Codeforces My Practice\n# author: Leonardone @ NEETSDKASU\n############################################################\ndef gs() gets.strip end\ndef gi() gets.to_i end\ndef gss() gs.split end\ndef gis() gss.map(&:to_i) end\ndef nmapf(n,f) n.times.map{ __send__ f } end\ndef ngs(n) nmapf n,:gs end\ndef ngi(n) nmapf n,:gi end\ndef ngss(n) nmapf n,:gss end\ndef ngis(n) nmapf n,:gis end\ndef arr2d(h,w,v=0) h.times.map{[v] * w} end\ndef for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end\ndef nsum(n) n * (n + 1) / 2 end\ndef vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end\n############################################################\n\nN, M = gis\nW = ngs N\n\nrows = [0] * N\ncols = [0] * M\n\ncnt = 0\n\nW.each_with_index do |s, y|\n M.times do |x|\n next if s[x] != '*'\n cnt += 1\n rows[y] += 1\n cols[x] += 1\n end\nend\n\nif cnt == 2\n puts :YES\n c = cols.each_with_index.select{|v, _| v > 0}.map{|_, i| i}\n r = rows.each_with_index.select{|v, _| v > 0}.map{|_, i| i}\n if c.size == 2 && r.size == 2\n if W[r[0]][c[0]] == '*'\n puts \"%d %d\" % [r[0] + 1, c[1] + 1]\n else\n puts \"%d %d\" % [r[0] + 1, c[0] + 1]\n end\n else\n puts \"%d %d\" % [r[0] + 1, r[0] + 1]\n end\n exit\nend\n\nmax_c, max_x = cols.each_with_index.max_by{|v, _| v }\nmax_r, max_y = rows.each_with_index.max_by{|v, _| v }\n\ncrs = max_c + max_r - (W[max_y][max_x] == '*' ? 1 : 0)\n\nif crs == cnt\n puts :YES\n puts \"%d %d\" % [max_y + 1, max_x + 1]\nelse\n puts :NO\nend\n\n"}, {"source_code": "numbers = gets\n\nres = numbers.split(' ')\n\nn = res[0].to_i\nm = res[1].to_i\n\nwallsx = []\nwallsy = []\ni = 0\nwalls = 0\n\nwhile (i < n) do\n chars = gets\n chars.each_char.with_index do |r,j|\n if (r == '*')\n wallsx.push(i);\n wallsy.push(j);\n walls += 1\n end\n end\n i = i + 1\n if (walls >= n+m+3)\n i = n\n end\nend\n\ni = 0\nxm = -1\nym = -1\n\nwhile (i < walls) do\n if (i != 0)\n if (wallsx[i] == wallsx[i-1])\n xm = wallsx[i]\n end\n if (wallsy[i] == wallsy[i-1])\n ym = wallsy[i]\n end\n end\n i = i + 1\nend\nif (walls == 0)\n xm = 0\n ym = 0\nelsif (walls == 1)\n xm = wallsx[0]\n ym = wallsy[0]\nelse\n if (xm == -1 && ym == -1) \n xm = wallsx[0]\n ym = wallsy[1]\n end\nend\ni = 0\nflag = 0\nwhile (i < walls) do\n if (wallsx[i] == xm || wallsy[i] == ym)\n #good, wall destroyed\n else\n flag = 1\n end\n i = i + 1\nend\nif (flag == 1 && (xm == -1 || ym == -1)) \n i = 0\n flag = 0\n #puts 'unclear'\n coords_unclear = 0\n while(i < walls)\n if (xm != -1 && wallsx[i] == xm || wallsy[i] == ym && ym != -1)\n #good, wall destroyed\n else\n if (coords_unclear == 0)\n if (xm == -1)\n xm = wallsx[i]\n else\n ym = wallsy[i]\n end\n coords_unclear = 1\n else\n flag = 1\n end\n end\n i = i + 1\n end\nend\nif flag == 1\n puts 'NO'\nelse\n puts 'YES'\n xm = 0 if xm == -1\n ym = 0 if ym == -1\n xm +=1\n ym +=1\n puts (xm).to_s + ' ' + (ym).to_s\nend"}], "src_uid": "12a768b502ddb07798830c9728fba5c4"} {"nl": {"description": "Kefa wants to celebrate his first big salary by going to restaurant. However, he needs company. Kefa has n friends, each friend will agree to go to the restaurant if Kefa asks. Each friend is characterized by the amount of money he has and the friendship factor in respect to Kefa. The parrot doesn't want any friend to feel poor compared to somebody else in the company (Kefa doesn't count). A friend feels poor if in the company there is someone who has at least d units of money more than he does. Also, Kefa wants the total friendship factor of the members of the company to be maximum. Help him invite an optimal company!", "input_spec": "The first line of the input contains two space-separated integers, n and d (1 ≤ n ≤ 105, ) — the number of Kefa's friends and the minimum difference between the amount of money in order to feel poor, respectively. Next n lines contain the descriptions of Kefa's friends, the (i + 1)-th line contains the description of the i-th friend of type mi, si (0 ≤ mi, si ≤ 109) — the amount of money and the friendship factor, respectively. ", "output_spec": "Print the maximum total friendship factir that can be reached.", "sample_inputs": ["4 5\n75 5\n0 100\n150 20\n75 1", "5 100\n0 7\n11 32\n99 10\n46 8\n87 54"], "sample_outputs": ["100", "111"], "notes": "NoteIn the first sample test the most profitable strategy is to form a company from only the second friend. At all other variants the total degree of friendship will be worse.In the second sample test we can take all the friends."}, "positive_code": [{"source_code": "prr = []\nn,d = gets.split().map(&:to_i)\nfor i in 0...n\n x,y = gets.split().map(&:to_i)\n prr[i] = [x,y]\nend\nprr.sort!{|x,y| x <=> y}\nj = ans = mx = i = 0\nwhile i=d\n mx = [mx,ans].max\n ans-=prr[j][1]\n j+=1\n i-=1\n else\n ans+=prr[i][1]\n end\n i+=1\nend\np [mx,ans].max"}, {"source_code": "n, d = gets.strip.split(\" \").map(&:to_i)\na = Array.new(n)\nn.times{|i| a[i] = gets.strip.split(\" \").map(&:to_i) }\na.sort!{|a1, a2| a1[0] <=> a2[0]}\ni = 0\ni0 = 0\nmax_sum = 0\ncurrent_sum = 0\nwhile (i < n) do \n if current_sum == 0 then \n i0 = i \n elsif a[i][0] - a[i0][0] >= d then\n j = i0\n while j < i && a[i][0] - a[j][0] >= d do \n current_sum -= a[j][1]\n j += 1\n end\n i0 = j\n end\n current_sum += a[i][1]\n max_sum = [max_sum, current_sum].max\n i += 1\nend\nputs max_sum\n"}, {"source_code": "n, d = gets.strip.split(\" \").map(&:to_i)\na = Array.new(n)\nn.times{|i| a[i] = gets.strip.split(\" \").map(&:to_i) }\na.sort!{|a1, a2| a1[0] <=> a2[0]}\ni = 0\ni0 = 0\nmax_sum = 0\ncurrent_sum = 0\nwhile (i < n) do \n if current_sum == 0 then \n i0 = i \n # current_sum += a[i][1]\n # puts \"here current_sum=#{current_sum}\"\n elsif a[i][0] - a[i0][0] >= d then\n # current_sum += a[i][1]\n j = i0\n while j < i do \n if a[i][0] - a[j][0] >= d then\n current_sum -= a[j][1]\n else\n break\n end\n j += 1\n end\n i0 = j\n end\n current_sum += a[i][1]\n # puts current_sum\n max_sum = [max_sum, current_sum].max\n i += 1\nend\nputs max_sum\n"}, {"source_code": "n,k=gets.split.map{|e| e.to_i}\narr=[]\nn.times{\n\ta=gets.split.map{|e| e.to_i}\n\tarr<=k\n\t\tsum-=arr[l][1]\n\t\tl+=1\n\tend\n\twhile r=k\n\t\tr+=1\n\t\tsum+=arr[r][1]\n\tend\n\tans=[ans,sum].max\n\tr+=1\n\tbreak if r>=n\n\tsum+=arr[r][1]\nend\nputs ans\n\n"}, {"source_code": "n, d = gets.split.map(&:to_i)\nf = n.times.map{gets.split.map(&:to_i)}.sort_by{|x| x[0]}\ncumulative = []\n(0..n).each{|i| cumulative[i] = (i == 0 ? 0 : f[i - 1][1] + cumulative[i - 1])}\ni, j, mx = 0, 0, 0\nwhile true\n if j == n\n mx = [mx, cumulative[j] - cumulative[i]].max\n break\n elsif f[j][0] - f[i][0] >= d\n mx = [mx, cumulative[j] - cumulative[i]].max\n i += 1\n else\n j += 1\n end\nend\nputs mx\n"}, {"source_code": "n, d = gets.split.map { |s| s.to_i }\n\nf = [[0, 0]]\nn.times do\n\tt = gets.split.map { |s| s.to_i }\n\tf << t\nend\nf.sort!\n\nsum = f.map { |item| item[1] }\n1.upto(n) do |i|\n\tsum[i] += sum[i - 1]\nend\n\nans = 0\nrp = 1\n1.upto(n) do |i|\n\trp += 1 while rp < n && f[i][0] + d > f[rp + 1][0]\n\tans = [ans, sum[rp] - sum[i - 1]].max\nend\nputs ans\n"}], "negative_code": [{"source_code": "n, d = gets.strip.split(\" \").map(&:to_i)\na = Array.new(n)\nn.times{|i| a[i] = gets.strip.split(\" \").map(&:to_i) }\na.sort!{|a1, a2| a1[0] <=> a2[0]}\ni = 0\ni0 = 0\nmax_sum = 0\ncurrent_sum = 0\nwhile (i < n) do \n if current_sum == 0 then \n i0 = i \n # current_sum += a[i][1]\n # puts \"here current_sum=#{current_sum}\"\n elsif a[i][0] - a[i0][0] > d then\n # current_sum += a[i][1]\n j = i0\n while j < i do \n if a[i][0] - a[j][0] > d then\n current_sum -= a[j][1]\n else\n break\n end\n j += 1\n end\n i0 = j\n end\n current_sum += a[i][1]\n # puts current_sum\n max_sum = [max_sum, current_sum].max\n i += 1\nend\nputs max_sum\n"}, {"source_code": "n,k=gets.split.map{|e| e.to_i}\narr=[]\nn.times{\n\ta=gets.split.map{|e| e.to_i}\n\tarr<k\n\t\tsum-=arr[l][1]\n\t\tl+=1\n\tend\n\twhile rk\n\t\tr+=1\n\t\tsum+=arr[r][1]\n\tend\n\tans=[ans,sum].max\n\tr+=1\n\tbreak if r>=n\n\tsum+=arr[r][1]\nend\nputs ans\n\n"}, {"source_code": "n,k=gets.split.map{|e| e.to_i}\nans=0\nn.times{\n\ta,b=gets.split.map{|e| e.to_i}\n\tif a<=k then\n\t\tans+=b\n\tend\n}\nputs ans\n\n"}, {"source_code": "n,k=gets.split.map{|e| e.to_i}\narr=[]\nn.times{\n\ta=gets.split.map{|e| e.to_i}\n\tarr<k\n\t\tsum-=arr[l][1]\n\t\tl+=1\n\tend\n\twhile rk\n\t\tr+=1\n\t\tsum+=arr[r][1]\n\tend\n\tans=[ans,sum].max\n\tr+=1\nend\nputs ans\n\n"}, {"source_code": "n, d = gets.split.map(&:to_i)\nf = n.times.map{gets.split.map(&:to_i)}.sort_by{|x| x[0]}\ncumulative = []\n(0..n).each{|i| cumulative[i] = (i == 0 ? 0 : f[i - 1][1] + cumulative[i - 1])}\nmx, status = 0, ''\ni, j = 0, 0\nif f[n - 1][0] - f[0][0] < d\n p cumulative[n]\nelse\n while true\n if f[j][0] - f[i][0] > d\n mx = [mx, cumulative[j] - cumulative[i]].max\n i += 1\n j = [i, j].max\n elsif j == n - 1\n break\n else\n j += 1\n end\n end\n p mx\nend\n"}, {"source_code": "n, d = gets.split.map(&:to_i)\nf = n.times.map{gets.split.map(&:to_i)}.sort_by{|x| x[0]}\ncumulative = []\n(0..n).each{|i| cumulative[i] = (i == 0 ? 0 : f[i - 1][1] + cumulative[i - 1])}\ni, j, mx = 0, 0, 0\nwhile true\n if j == n\n mx = [mx, cumulative[j] - cumulative[i]].max\n break\n elsif f[j][0] - f[i][0] > d\n mx = [mx, cumulative[j] - cumulative[i]].max\n i += 1\n else\n j += 1\n end\nend\nputs mx\n"}], "src_uid": "38fe0e19974a7bc60153793b9060369a"} {"nl": {"description": "Alexey is travelling on a train. Unfortunately, due to the bad weather, the train moves slower that it should!Alexey took the train at the railroad terminal. Let's say that the train starts from the terminal at the moment $$$0$$$. Also, let's say that the train will visit $$$n$$$ stations numbered from $$$1$$$ to $$$n$$$ along its way, and that Alexey destination is the station $$$n$$$.Alexey learned from the train schedule $$$n$$$ integer pairs $$$(a_i, b_i)$$$ where $$$a_i$$$ is the expected time of train's arrival at the $$$i$$$-th station and $$$b_i$$$ is the expected time of departure.Also, using all information he has, Alexey was able to calculate $$$n$$$ integers $$$tm_1, tm_2, \\dots, tm_n$$$ where $$$tm_i$$$ is the extra time the train need to travel from the station $$$i - 1$$$ to the station $$$i$$$. Formally, the train needs exactly $$$a_i - b_{i-1} + tm_i$$$ time to travel from station $$$i - 1$$$ to station $$$i$$$ (if $$$i = 1$$$ then $$$b_0$$$ is the moment the train leave the terminal, and it's equal to $$$0$$$).The train leaves the station $$$i$$$, if both conditions are met: it's on the station for at least $$$\\left\\lceil \\frac{b_i - a_i}{2} \\right\\rceil$$$ units of time (division with ceiling); current time $$$\\ge b_i$$$. Since Alexey spent all his energy on prediction of time delays, help him to calculate the time of arrival at the station $$$n$$$.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \\le n \\le 100$$$) — the number of stations. Next $$$n$$$ lines contain two integers each: $$$a_i$$$ and $$$b_i$$$ ($$$1 \\le a_i < b_i \\le 10^6$$$). It's guaranteed that $$$b_i < a_{i+1}$$$. Next line contains $$$n$$$ integers $$$tm_1, tm_2, \\dots, tm_n$$$ ($$$0 \\le tm_i \\le 10^6$$$).", "output_spec": "For each test case, print one integer — the time of Alexey's arrival at the last station.", "sample_inputs": ["2\n2\n2 4\n10 12\n0 2\n5\n1 4\n7 8\n9 10\n13 15\n19 20\n1 2 3 4 5"], "sample_outputs": ["12\n32"], "notes": "NoteIn the first test case, Alexey arrives at station $$$1$$$ without any delay at the moment $$$a_1 = 2$$$ (since $$$tm_1 = 0$$$). After that, he departs at moment $$$b_1 = 4$$$. Finally, he arrives at station $$$2$$$ with $$$tm_2 = 2$$$ extra time, or at the moment $$$12$$$.In the second test case, Alexey arrives at the first station with $$$tm_1 = 1$$$ extra time, or at moment $$$2$$$. The train, from one side, should stay at the station at least $$$\\left\\lceil \\frac{b_1 - a_1}{2} \\right\\rceil = 2$$$ units of time and from the other side should depart not earlier than at moment $$$b_1 = 4$$$. As a result, the trains departs right at the moment $$$4$$$.Using the same logic, we can figure out that the train arrives at the second station at the moment $$$9$$$ and departs at the moment $$$10$$$; at the third station: arrives at $$$14$$$ and departs at $$$15$$$; at the fourth: arrives at $$$22$$$ and departs at $$$23$$$. And, finally, arrives at the fifth station at $$$32$$$."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n a, b = [0] * n, [0] * n\r\n n.times do |i|\r\n a[i], b[i] = gets.split.map &:to_i\r\n end\r\n b += [0]\r\n tm = gets.split.map &:to_i\r\n res = 0\r\n n.times do |i|\r\n res += a[i] - b[i - 1] + tm[i]\r\n res = [res + (b[i] - a[i] + 1) / 2, b[i]].max if i < n - 1\r\n end\r\n puts res\r\nend"}], "negative_code": [{"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n a, b = [0] * n, [0] * n\r\n n.times do |i|\r\n a[i], b[i] = gets.split.map &:to_i\r\n end\r\n b += [0]\r\n tm = gets.split.map &:to_i\r\n res = 0\r\n n.times do |i|\r\n res += a[i] - b[i - 1] + tm[i]\r\n res = [res + (b[i] - a[i] + 1) / 2, b[i]].max if res < n - 1\r\n end\r\n puts res\r\nend"}], "src_uid": "42840fc873369e0d0d6a4ad24a43f5a6"} {"nl": {"description": "Vanya is managed to enter his favourite site Codehorses. Vanya uses n distinct passwords for sites at all, however he can't remember which one exactly he specified during Codehorses registration.Vanya will enter passwords in order of non-decreasing their lengths, and he will enter passwords of same length in arbitrary order. Just when Vanya will have entered the correct password, he is immediately authorized on the site. Vanya will not enter any password twice.Entering any passwords takes one second for Vanya. But if Vanya will enter wrong password k times, then he is able to make the next try only 5 seconds after that. Vanya makes each try immediately, that is, at each moment when Vanya is able to enter password, he is doing that.Determine how many seconds will Vanya need to enter Codehorses in the best case for him (if he spends minimum possible number of second) and in the worst case (if he spends maximum possible amount of seconds).", "input_spec": "The first line of the input contains two integers n and k (1 ≤ n, k ≤ 100) — the number of Vanya's passwords and the number of failed tries, after which the access to the site is blocked for 5 seconds. The next n lines contains passwords, one per line — pairwise distinct non-empty strings consisting of latin letters and digits. Each password length does not exceed 100 characters. The last line of the input contains the Vanya's Codehorses password. It is guaranteed that the Vanya's Codehorses password is equal to some of his n passwords.", "output_spec": "Print two integers — time (in seconds), Vanya needs to be authorized to Codehorses in the best case for him and in the worst case respectively.", "sample_inputs": ["5 2\ncba\nabc\nbb1\nabC\nABC\nabc", "4 100\n11\n22\n1\n2\n22"], "sample_outputs": ["1 15", "3 4"], "notes": "NoteConsider the first sample case. As soon as all passwords have the same length, Vanya can enter the right password at the first try as well as at the last try. If he enters it at the first try, he spends exactly 1 second. Thus in the best case the answer is 1. If, at the other hand, he enters it at the last try, he enters another 4 passwords before. He spends 2 seconds to enter first 2 passwords, then he waits 5 seconds as soon as he made 2 wrong tries. Then he spends 2 more seconds to enter 2 wrong passwords, again waits 5 seconds and, finally, enters the correct password spending 1 more second. In summary in the worst case he is able to be authorized in 15 seconds.Consider the second sample case. There is no way of entering passwords and get the access to the site blocked. As soon as the required password has length of 2, Vanya enters all passwords of length 1 anyway, spending 2 seconds for that. Then, in the best case, he immediately enters the correct password and the answer for the best case is 3, but in the worst case he enters wrong password of length 2 and only then the right one, spending 4 seconds at all."}, "positive_code": [{"source_code": "#!/usr/bin/ruby\nn,k=gets.split.map &:to_i\na=$<.map{|e|e.chomp.size}\nx=a.pop\na.sort!\nl,r=[:>=,:>].map{|o|\n\t(0...n).bsearch{|i|a[i].send(o,x)}||n\n}\nr-=1\nputs [l+l/k*5+1,r+r/k*5+1]*' '"}, {"source_code": "a,b=gets.split.map &:to_i\narr=[]\na.times{arr< pasN and ans2 == 0\n time += 5 if (i+1) % k == 0 and i!=n-1\nend\n\nans2 = time if ans2 == 0\nans2 = ans1 if pasW == pass[0] and pass.size == 1\n\nputs \"#{ans1} #{ans2}\""}, {"source_code": "e = gets.chop.split(' ')\nn, k = e[0].to_i, e[1].to_i\n\n\n\n\n\nps = []\n\ni = 0\nwhile i < n\n tmp = gets.chop\n ps << tmp\n i += 1\nend \n\nt_ps = gets.chop\n\nbiger = 0\nsmaller = 1 \n\nps = ps.uniq.sort{|x| x.size }.each do |x|\n biger += 1 if x.size == t_ps.size && x != t_ps\n smaller +=1 if x.size < t_ps.size \nend\n\n\nbest = smaller + (((smaller-1) / k) * 5);\nworst = biger + smaller + (((biger + smaller - 1) / k) * 5)\n\nputs \"#{best} #{worst}\""}, {"source_code": "n,m=gets.split.map{|e| e.to_i}\nx=STDIN.read.split\ny=x.last\nx.pop\nz=x.select{|e|\n\ty.size>e.size\n}\nx=x.select{|e|\n\ty.size>=e.size\n}\n\nans1=z.size+1+(z.size/m)*5\nans2=x.size+((x.size-1)/m)*5\n\nputs [ans1,ans2]*\" \"\n\n"}, {"source_code": "n, k =gets.chomp.split(\" \").map(&:to_i)\nstring=Array.new\nfor i in 0..n-1\n string[i]=gets.chomp\nend\nn = gets.chomp\nstring = string.sort_by(&:size).select{ |x| x.size <= n.size }\nif string[0].size == n.size\n print 1\nelse\n ctr = string.select{|x| x.size correctlen))\n\t\tmaxians=ans\n\tend\n\n\tans+=5 if(count%k==0)\nend\n\nprint \"#{minians} #{maxians}\"\nputs\n"}, {"source_code": "n, k = gets.chomp.split(' ').map(&:to_i)\ncandidates = []\npassword = nil\n\nn.times do\n\tcandidates << gets.chomp\nend\n\npassword = gets.chomp\n\ncandidates.sort! { |a, b|\n\tif a.length > b.length\n\t\t1\n\telsif a.length == b.length\n\t\t0\n\telse\n\t\t-1\n\tend\n}\n\nlengthHash = {}\ncandidates.each do |c|\n\tif lengthHash.has_key? c.length\n\t\tlengthHash[c.length] += 1\n\telse\n\t\tlengthHash[c.length] = 1\n\tend\nend\n\nwrong = 0\n\n(1...password.length).each do |len|\n\tif lengthHash.has_key? len\n\t\twrong += lengthHash[len]\n\tend\nend\n\n# t p\nbest = 1 + wrong + (wrong / k) * 5\nworst = wrong + lengthHash[password.length] + ((wrong + (lengthHash[password.length] - 1)) / k) * 5\n\nputs \"#{best} #{worst}\""}], "negative_code": [{"source_code": "n,k = gets.split.map(&:to_i)\n\npass = []\n\nn.times do\n pass << gets.chomp\nend\n\npass = pass.uniq.sort_by{|x| x.length}\npasW = gets.chomp\npasN = pasW.length\n\nans1, ans2 = 0,0\ntime = 0\n\nfor i in 0...n do\n ans2 = time if pass[i].length > pasN\n time += 1\n ans1 = time if pass[i].length == pasN and ans1 == 0\n time += 5 if (i+1) % k == 0 and i!=n-1\nend\n\nans2 = time if ans2 == 0\nans2 = ans1 if pasW == pass[0] and pass.size == 1\n\nputs \"#{ans1} #{ans2}\""}, {"source_code": "n,k = gets.split.map(&:to_i)\n\npass = []\n\nn.times do\n pass << gets.chomp\nend\n\npass = pass.uniq.sort_by{|x| x.length}\npasW = gets.chomp\npasN = pasW.length\n\nans1, ans2 = 0,0\ntime = 0\n\nfor i in 0...n do\n ans2 = time if pass[i].length > pasN\n time += 1\n ans1 = time if pass[i].length == pasN and ans1 == 0\n time += 5 if (i+1) % k == 0\nend\nans2 = time if ans2 == 0\nans2 = ans1 if pasW == pass[0]\n\nprint ans1,\" \",ans2"}, {"source_code": "n,k = gets.split.map(&:to_i)\n\npass = []\n\nn.times do\n pass << gets.chomp\nend\n\npass = pass.uniq.sort_by{|x| x.length}\npasW = gets.chomp\npasN = pasW.length\n\nans1, ans2 = 0,0\ntime = 0\n\nfor i in 0...n do\n ans2 = time if pass[i].length > pasN\n time += 1\n ans1 = time if pass[i].length == pasN and ans1 == 0\n time += 5 if (i+1) % k == 0 and i!=n-1\nend\n\nans2 = time if ans2 == 0\nans2 = ans1 if pasW == pass[0]\n\nputs \"#{ans1} #{ans2}\""}, {"source_code": "n,k = gets.split.map(&:to_i)\n\npass = []\n\nn.times do\n pass << gets.chomp\nend\n\npass = pass.sort_by{|x| x.length}\npasN = gets.chomp.length\n\nans1, ans2 = 0,0\ntime = 0\n\nfor i in 0...n do\n ans2 = time if pass[i].length > pasN\n time += 1\n time += 5 if (i+1) % k == 0\n ans1 = time if pass[i].length == pasN and ans1 == 0\nend\nans2 = time if ans2 == 0\n\nprint ans1,\" \",ans2"}, {"source_code": "n,k = gets.split.map(&:to_i)\n\npass = []\n\nn.times do\n pass << gets.chomp\nend\n\npass = pass.uniq.sort_by{|x| x.length}\npasW = gets.chomp\npasN = pasW.length\n\nans1, ans2 = 0,0\ntime = 0\n\nfor i in 0...n do\n ans2 = time if pass[i].length > pasN\n time += 1\n ans1 = time if pass[i].length == pasN and ans1 == 0\n time += 5 if (i+1) % k == 0 and i!=n-1\nend\nans2 = time if ans2 == 0\nans2 = ans1 if pasW == pass[0]\n\nprint ans1,\" \",ans2"}, {"source_code": "n,k = gets.split.map(&:to_i)\n\npass = []\n\nn.times do\n pass << gets.chomp\nend\n\npass = pass.uniq.sort_by{|x| x.length}\npasW = gets.chomp\npasN = pasW.length\n\nans1, ans2 = 0,0\ntime = 0\n\nfor i in 0...n do\n ans2 = time if pass[i].length > pasN\n time += 1\n ans1 = time if pass[i].length == pasN and ans1 == 0\n time += 5 if (i+1) % k == 0 and i!=n-1\nend\n\nans2 = time if ans2 == 0\nans2 = ans1 if pasW == pass[0]\n"}, {"source_code": "n, k =gets.chomp.split(\" \").map(&:to_i)\nstring=Array.new\nsame= -1\nflag = 0\nfor i in 0..n-1\n string[i]=gets\nend\nstring.sort_by! {|x| x.length}\nfor i in 0..n-1\n if ((string[i].length == string[n-1].length) && (flag == 0))\n print i+1, \" \"\n flag = 1\n same +=1 if (k==1)\n end\n if flag == 1\n same += 1\n end\nend\nif ky[1]} ? 'Happy' : 'Poor')+' Alex'"}, {"source_code": "n=gets.chomp.to_i\nn.times do\n\tif gets.split.uniq.length > 1\n\t\tputs 'Happy Alex'\n\t\texit\n\tend\nend\nputs 'Poor Alex'"}, {"source_code": "gets.to_i.times { \n if gets.split.uniq.length > 1\n puts 'Happy Alex'\n exit\n end\n}\nputs 'Poor Alex'"}, {"source_code": "yep = false\ngets.to_i.times {\n if gets.split.uniq.length > 1\n yep = true\n break\n end\n}\nputs ((yep) ? ('Happy Alex') : ('Poor Alex')) "}, {"source_code": "arr = []\ngets.to_i.times do\n inp = gets.split.map &:to_i\n arr << inp.last - inp.first\nend\nputs arr.count { |x| x < 0 } > 0 ? \"Happy Alex\" : \"Poor Alex\"\n"}, {"source_code": "n = gets.chomp.to_i\nlaptops = []\nn.times do\n laptops << gets.chomp.split(/ /).map(&:to_i)\nend\nlaptops.sort!\nalex = false\n(1...n).each do |first|\n a = laptops[first - 1]\n break if alex\n b = laptops[first]\n if a[0] < b[0] && a[1] > b[1]\n alex = true\n break\n end\nend\nif alex\n puts \"Happy Alex\"\nelse\n puts \"Poor Alex\"\nend\n"}, {"source_code": "n = gets.chomp.to_i\nlaptops = []\nn.times do\n laptops << gets.chomp.split(/ /).map(&:to_i)\nend\nlaptops.sort!\nalex = false\n(1...n).each do |first|\n a = laptops[first - 1]\n b = laptops[first]\n if a[0] < b[0] && a[1] > b[1]\n alex = true\n break\n end\nend\nif alex\n puts \"Happy Alex\"\nelse\n puts \"Poor Alex\"\nend\n"}, {"source_code": "n=gets.to_i\nx=[]\nn.times{\n\tx<x[0][1] then\n\t\tans=\"Happy Alex\"\n\t\tbreak\n\tend\n\tmax1=x[0][1] if max1 a[i + 1][1] && a[i][0] < a[i + 1][0]\n print \"Happy Alex\\n\"\n exit\n end\nend\nprint \"Poor Alex\\n\"\n"}, {"source_code": "ans = false\n\n(0...gets.to_i).each {\n if gets.split.uniq.length == 2\n ans = true\n break\n end\n}\n\nputs ((ans)? 'Happy Alex' : 'Poor Alex')"}, {"source_code": "ans = false\n\n(0...gets.to_i).each {\n a, b = gets.split\n \n if a.to_i != b.to_i\n ans = true\n break\n end\n}\n\nputs ((ans)? 'Happy Alex' : 'Poor Alex')"}, {"source_code": "numberOflaptops = gets.chomp.to_i\nlaptops = Array.new(numberOflaptops)\n\nfor i in 1..numberOflaptops\n input = gets.chomp.split(' ').map { |x| x.to_i }\n laptops[input[0]-1] = input[1]\nend\n\nmax = 0\nfor rating in laptops\n if max < rating\n max = rating\n else\n puts 'Happy Alex'\n exit\n end\nend\n\nputs 'Poor Alex'\n"}, {"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\n\nn = gets.to_i\nq = []\nn.times {\n a, b = gets.split.map(&:to_i)\n q << [a, b]\n}\nfound = false\ncatch :happyalex do\n q.sort { |i1, i2|\n acomp = i1[0] <=> i2[0]\n bcomp = i1[1] <=> i2[1]\n\n if acomp*bcomp < 0\n found = true\n throw :happyalex\n end\n\n if acomp != 0\n acomp\n else\n bcomp\n end\n }\nend\n\nputs \"Happy Alex\" if found\nputs \"Poor Alex\" if !found"}, {"source_code": "number_of_laptops = gets.chomp\nlaptops = {}\nnumber_of_laptops.to_i.times do\n laptop = gets.chomp.split(\" \").map(&:to_i)\n laptops.store(laptop[0], laptop[1])\nend\nlaptops = laptops.sort\nanswer = false\nfor i in 0...laptops.length - 1 do\n if(laptops[i][0] < laptops[i+1][0] && laptops[i][1] > laptops[i+1][1])\n answer = true\n break\n end\nend\nif answer\n puts \"Happy Alex\"\nelse\n puts \"Poor Alex\"\nend"}, {"source_code": "n = gets.to_i\nlaptops = []\nres = false\nn.times {\n laptops << gets.split(\" \").map(&:to_i)\n}\nlaptops.sort! do |a,b|\n a[0] <=> b[0]\nend\n(0..n-2).each do |i|\n res = true if laptops[i][1] > laptops[i+1][1]\nend\n\nputs res ? \"Happy Alex\" : \"Poor Alex\""}, {"source_code": "n = gets.to_i\ns = n.times.map{gets.split.map(&:to_i)}.sort\nans = false\n(n - 1).times do |i|\n ans = true if s[i][1] > s[i + 1][1]\nend\nputs ans ? \"Happy Alex\" : \"Poor Alex\"\n"}, {"source_code": "n = gets.to_i\ns = n.times.map{gets.split.map(&:to_i)}.sort\nans = (n - 1).times.any?{|i| s[i][1] > s[i + 1][1]}\nputs ans ? \"Happy Alex\" : \"Poor Alex\"\n"}, {"source_code": "\n# Initialize vars\nspecs = []\n\n# Read input\nn = gets.strip.to_i\nn.times {specs.push(gets.strip.split.map{|s| s.to_i})}\n\nprice_order = specs.sort_by(&:first)\nquality_order = specs.sort_by(&:last)\n\nif price_order != quality_order\n puts \"Happy Alex\" \nelse\n puts \"Poor Alex\"\nend "}, {"source_code": "n = gets.to_i\na, b, m = [], [], [0]*(n + 1)\nn.times do |i|\n a[i], b[i] = gets.split.map{|x| x.to_i}\n m[b[i]] = a[i]\nend\n(1..n).each{|i| m[i] = [m[i - 1], m[i]].max}\nn.times do |i|\n if a[i] < m[b[i]]\n puts \"Happy Alex\"\n exit\n end\nend\nputs \"Poor Alex\"\n"}, {"source_code": "n=gets.to_i\narr=Array.new(n)\n\nfor i in 0...n do\n x,y=gets.split.map(&:to_i)\n arr[y-1]=x\nend\n\n\nif (arr.sort == arr)\n puts \"Poor Alex\"\nelse\n puts \"Happy Alex\"\nend"}, {"source_code": "numberOflaptops = gets.chomp.to_i\nlaptops = Array.new(numberOflaptops)\n\nfor i in 1..numberOflaptops\n input = gets.chomp.split(' ').map { |x| x.to_i }\n laptops[input[0]-1] = input[1]\nend\n\nmax = 0\nfor rating in laptops\n if max < rating\n max = rating\n else\n puts 'Happy Alex'\n exit\n end\nend\n\nputs 'Poor Alex'"}, {"source_code": "data_max = Hash.new { 0 }\ndata_min = Hash.new { 1_000_000 }\n\n1.upto(gets.to_i) do\n k, v = gets.split(' ').map(&:to_i)\n\n data_max[k] = [data_max[k], v].max\n data_min[k] = [data_min[k], v].min\nend\n\nmax = 0\nresult = false\ndata_max.keys.sort.each do |k|\n result = true if max > data_min[k]\n\n max = data_max[k]\nend\n\nputs \"#{result ? 'Happy' : 'Poor'} Alex\"\n"}, {"source_code": "(0...gets.to_i).each { |i|\n x, y = gets.chomp.split.map(&:to_i)\n if x != y\n puts 'Happy Alex'\n exit\n end\n}\n\nputs 'Poor Alex'"}, {"source_code": "n = gets.to_i\na = (0...n).map {gets.split.map {|i| i.to_i}}\nif a.sort_by { |e| e[0] } == a.sort_by { |e| e[1] }\n\tputs 'Poor Alex'\nelse\n\tputs 'Happy Alex'\nend"}], "negative_code": [{"source_code": "n = gets.chomp.to_i\nlaptops = []\nn.times do\n laptops << gets.chomp.split(/ /).map(&:to_i)\nend\nalex = false\n(1...n).each do |first|\n a = laptops[first - 1]\n break if alex\n b = laptops[first]\n if a[0] < b[0] && a[1] > b[1]\n alex = true\n break\n end\nend\nif alex\n puts \"Happy Alex\"\nelse\n puts \"Poor Alex\"\nend\n"}, {"source_code": "n = gets.to_i\na = []\nfor i in (0...n) do\n a[i] = gets.split.map{|j| j.to_i}.to_a\nend\na.sort!\n0.upto(n - 2) do |i|\n if a[i][1] > a[i + 1][1] && a[i][0] < a[i + 1][0]\n p \"Happy Alex\"\n exit\n end\nend\np \"Poor Alex\"\n"}, {"source_code": "ans = false\nn = gets.to_i\n\n(0...gets.to_i).each {\n a, b = gets.split\n if a != b \n ans = true\n break\n end\n}\n\nputs ((ans)? 'Happy Alex' : 'Poor Alex')"}, {"source_code": "n = gets.to_i\ns = n.times.map{gets.split.map(&:to_i)}.sort\nans = false\nn.times do |i|\n ans = true if s[i][1] < s[i - 1][1]\nend\nputs ans ? \"Happy Alex\" : \"Poor Alex\"\n"}], "src_uid": "c21a84c4523f7ef6cfa232cba8b6ee2e"} {"nl": {"description": "A string is called palindrome if it reads the same from left to right and from right to left. For example \"kazak\", \"oo\", \"r\" and \"mikhailrubinchikkihcniburliahkim\" are palindroms, but strings \"abb\" and \"ij\" are not.You are given string s consisting of lowercase Latin letters. At once you can choose any position in the string and change letter in that position to any other lowercase letter. So after each changing the length of the string doesn't change. At first you can change some letters in s. Then you can permute the order of letters as you want. Permutation doesn't count as changes. You should obtain palindrome with the minimal number of changes. If there are several ways to do that you should get the lexicographically (alphabetically) smallest palindrome. So firstly you should minimize the number of changes and then minimize the palindrome lexicographically.", "input_spec": "The only line contains string s (1 ≤ |s| ≤ 2·105) consisting of only lowercase Latin letters.", "output_spec": "Print the lexicographically smallest palindrome that can be obtained with the minimal number of changes.", "sample_inputs": ["aabc", "aabcd"], "sample_outputs": ["abba", "abcba"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/ruby\nh=Hash.new(0)\ngets.chomp.each_char{|c|\n\th[c]+=1\n}\nbk='z'\npair=''\ncenter=''\nflg=false\n('a'..'z').each{|c|\n\tpair<= str.size\n d = true\nelse\n\twhile istr.size\n puts 0\n else\n i=word.size\n c=str.size\n\n while i>0 and c>0\n c=str.rindex(word[i-1],c-1)\n i-=1\n end\n\n if str[c]==word[0]\n fl=c\n end\n\n if lf!=-1 and fl!=-1\n if fl<=lf\n puts 0\n else\n puts fl-lf\n end\n else\n puts 0\n end\n end\nend\n"}], "negative_code": [], "src_uid": "724fa4b1d35b8765048857fa8f2f6802"} {"nl": {"description": "Bob is playing a game named \"Walk on Matrix\".In this game, player is given an $$$n \\times m$$$ matrix $$$A=(a_{i,j})$$$, i.e. the element in the $$$i$$$-th row in the $$$j$$$-th column is $$$a_{i,j}$$$. Initially, player is located at position $$$(1,1)$$$ with score $$$a_{1,1}$$$. To reach the goal, position $$$(n,m)$$$, player can move right or down, i.e. move from $$$(x,y)$$$ to $$$(x,y+1)$$$ or $$$(x+1,y)$$$, as long as player is still on the matrix.However, each move changes player's score to the bitwise AND of the current score and the value at the position he moves to.Bob can't wait to find out the maximum score he can get using the tool he recently learnt  — dynamic programming. Here is his algorithm for this problem. However, he suddenly realize that the algorithm above fails to output the maximum score for some matrix $$$A$$$. Thus, for any given non-negative integer $$$k$$$, he wants to find out an $$$n \\times m$$$ matrix $$$A=(a_{i,j})$$$ such that $$$1 \\le n,m \\le 500$$$ (as Bob hates large matrix); $$$0 \\le a_{i,j} \\le 3 \\cdot 10^5$$$ for all $$$1 \\le i\\le n,1 \\le j\\le m$$$ (as Bob hates large numbers); the difference between the maximum score he can get and the output of his algorithm is exactly $$$k$$$. It can be shown that for any given integer $$$k$$$ such that $$$0 \\le k \\le 10^5$$$, there exists a matrix satisfying the above constraints.Please help him with it!", "input_spec": "The only line of the input contains one single integer $$$k$$$ ($$$0 \\le k \\le 10^5$$$).", "output_spec": "Output two integers $$$n$$$, $$$m$$$ ($$$1 \\le n,m \\le 500$$$) in the first line, representing the size of the matrix. Then output $$$n$$$ lines with $$$m$$$ integers in each line, $$$a_{i,j}$$$ in the $$$(i+1)$$$-th row, $$$j$$$-th column.", "sample_inputs": ["0", "1"], "sample_outputs": ["1 1\n300000", "3 4\n7 3 3 1\n4 8 3 6\n7 7 7 3"], "notes": "NoteIn the first example, the maximum score Bob can achieve is $$$300000$$$, while the output of his algorithm is $$$300000$$$.In the second example, the maximum score Bob can achieve is $$$7\\&3\\&3\\&3\\&7\\&3=3$$$, while the output of his algorithm is $$$2$$$."}, "positive_code": [{"source_code": "k = gets.chomp.to_i\n\nputs [2,3].join(' ')\nputs [2**18-1, k, 0].join(' ')\nputs [2**17, 2**18-1, k].join(' ')\n"}], "negative_code": [], "src_uid": "fc0442e5cda2498a1818702e5e3eeec4"} {"nl": {"description": "Jett is tired after destroying the town and she wants to have a rest. She likes high places, that's why for having a rest she wants to get high and she decided to craft staircases.A staircase is a squared figure that consists of square cells. Each staircase consists of an arbitrary number of stairs. If a staircase has $$$n$$$ stairs, then it is made of $$$n$$$ columns, the first column is $$$1$$$ cell high, the second column is $$$2$$$ cells high, $$$\\ldots$$$, the $$$n$$$-th column if $$$n$$$ cells high. The lowest cells of all stairs must be in the same row.A staircase with $$$n$$$ stairs is called nice, if it may be covered by $$$n$$$ disjoint squares made of cells. All squares should fully consist of cells of a staircase. This is how a nice covered staircase with $$$7$$$ stairs looks like: Find out the maximal number of different nice staircases, that can be built, using no more than $$$x$$$ cells, in total. No cell can be used more than once.", "input_spec": "The first line contains a single integer $$$t$$$ $$$(1 \\le t \\le 1000)$$$  — the number of test cases. The description of each test case contains a single integer $$$x$$$ $$$(1 \\le x \\le 10^{18})$$$  — the number of cells for building staircases.", "output_spec": "For each test case output a single integer  — the number of different nice staircases, that can be built, using not more than $$$x$$$ cells, in total.", "sample_inputs": ["4\n1\n8\n6\n1000000000000000000"], "sample_outputs": ["1\n2\n1\n30"], "notes": "NoteIn the first test case, it is possible to build only one staircase, that consists of $$$1$$$ stair. It's nice. That's why the answer is $$$1$$$.In the second test case, it is possible to build two different nice staircases: one consists of $$$1$$$ stair, and another consists of $$$3$$$ stairs. This will cost $$$7$$$ cells. In this case, there is one cell left, but it is not possible to use it for building any nice staircases, that have not been built yet. That's why the answer is $$$2$$$.In the third test case, it is possible to build only one of two nice staircases: with $$$1$$$ stair or with $$$3$$$ stairs. In the first case, there will be $$$5$$$ cells left, that may be used only to build a staircase with $$$2$$$ stairs. This staircase is not nice, and Jett only builds nice staircases. That's why in this case the answer is $$$1$$$. If Jett builds a staircase with $$$3$$$ stairs, then there are no more cells left, so the answer is $$$1$$$ again."}, "positive_code": [{"source_code": "max = 10**18\nraw_pretty_list = []\npretty = 1\nmax.times do\n raw_pretty_list << pretty\n pretty = (pretty * 2) + 1\n break if pretty > max\nend\n\npretty_list = raw_pretty_list.map do |item|\n item ** 2 / 2 + item / 2 + 1\nend\n\nt = gets.to_i\nt.times do\n x = gets.to_i\n res = 0\n internal_pretty_list = pretty_list.dup\n while x > 0 do\n cur_pretty = internal_pretty_list.shift\n\n\n break if cur_pretty.nil?\n\n\n if x >= cur_pretty\n x -= cur_pretty\n res += 1\n end\n end\n puts res\nend\n"}, {"source_code": "T = gets.to_i\n\nT.times do\n x = gets.to_i\n\n n = 1\n v = 1\n ans = 0\n\n loop {\n use_cnt = n * (1 + n) / 2\n x -= use_cnt\n\n break if x < 0\n\n v *= 2\n n += v\n ans += 1\n }\n\n puts ans\nend\n"}], "negative_code": [], "src_uid": "f0806ab99cf4da228abe3cd8073884d9"} {"nl": {"description": "It is so boring in the summer holiday, isn't it? So Alice and Bob have invented a new game to play. The rules are as follows. First, they get a set of n distinct integers. And then they take turns to make the following moves. During each move, either Alice or Bob (the player whose turn is the current) can choose two distinct integers x and y from the set, such that the set doesn't contain their absolute difference |x - y|. Then this player adds integer |x - y| to the set (so, the size of the set increases by one).If the current player has no valid move, he (or she) loses the game. The question is who will finally win the game if both players play optimally. Remember that Alice always moves first.", "input_spec": "The first line contains an integer n (2 ≤ n ≤ 100) — the initial number of elements in the set. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the set.", "output_spec": "Print a single line with the winner's name. If Alice wins print \"Alice\", otherwise print \"Bob\" (without quotes).", "sample_inputs": ["2\n2 3", "2\n5 3", "3\n5 6 7"], "sample_outputs": ["Alice", "Alice", "Bob"], "notes": "NoteConsider the first test sample. Alice moves first, and the only move she can do is to choose 2 and 3, then to add 1 to the set. Next Bob moves, there is no valid move anymore, so the winner is Alice."}, "positive_code": [{"source_code": "n = gets.chomp.to_i\na = gets.chomp.split.map{|i| i.to_i}\n\n\ngcd = a[0]\n\na.each do |i|\n\tif i.gcd(gcd) < gcd\n\t\tgcd = i.gcd(gcd)\n\tend\nend\n\nmax = a.max\n\nif (max / gcd - n) % 2 == 1\n\tputs \"Alice\"\nelse\n\tputs \"Bob\"\nend\n"}, {"source_code": "def gcd(a, b)\n b == 0 ? a : gcd(b, a % b)\nend\n\nn = gets.to_i\narr = gets.split(\" \").map{|x| x.to_i }\ng = gcd(arr[0], arr[1])\nfor i in 2...n do\n g = gcd(g, arr[i])\nend\n\nn = arr.max / g - n\nputs n % 2 == 1 ? \"Alice\" : \"Bob\"\n"}, {"source_code": "n = gets.to_i\na = gets.split.collect{|i| i.to_i}\ng = a[0]\na.each{|i| g=g.gcd(i)}\nif ((g..a.max).step(g).size-a.size).odd?\n puts \"Alice\"\nelse\n puts \"Bob\"\nend\n"}, {"source_code": "def gcd(a, b)\n b == 0 ? a : gcd(b, a % b)\nend\n\nn = gets.to_i\narr = gets.split(\" \").map{|x| x.to_i }\ng = gcd(arr[0], arr[1])\nfor i in 2...n do\n g = gcd(g, arr[i])\nend\n\nn = arr.max / g - n\nputs n % 2 == 1 ? \"Alice\" : \"Bob\""}, {"source_code": "def gcd a, b\n\treturn a if b == 0\n\tgcd b, a % b\nend\n\nn = gets.to_i\na = gets.split.map { |x| x.to_i }\n\ng = a[0]\na.each do |x|\n\tg = gcd g, x\nend\n\ncnt = a.max / g - a.size\n\nif cnt.odd?\n\tputs 'Alice'\nelse\n\tputs 'Bob'\nend"}], "negative_code": [{"source_code": "def gcd(arr)\n a, b = arr\n b.zero? ? a : gcd([b, a % b])\nend\n\nn = gets.to_i \narr = gets.split.map(&:to_i)\ng = gcd(arr.unshift(2))\narr.each {|i| g = gcd([g, i]) }\n\nn = arr.max / g - n\nputs %w(Bob Alice)[n % 2]"}, {"source_code": "def gcd(arr)\n a, b = arr\n b.zero? ? a : gcd([b, a % b])\nend\n\nn = gets.to_i \narr = gets.split.map(&:to_i)\nm = arr.max\ng = gcd(arr.unshift(2))\n(n-2).times {|i| g = gcd([g, arr[i]]) }\n\nn = m / g - n\nputs %w(Bob Alice)[n % 2]"}, {"source_code": "count = gets.to_i\nmaximum = gets.split.map(&:to_i).max\n\nputs (maximum - count).odd? ? \"Alice\" : \"Bob\""}, {"source_code": "count = gets.to_i\nmaximum = gets.split.map(&:to_i).max\n\nputs (maximum - count).even? ? \"Alice\" : \"Bob\""}], "src_uid": "3185ae6b4b681a10a21d02e67f08fd19"} {"nl": {"description": "One day Jeff got hold of an integer sequence a1, a2, ..., an of length n. The boy immediately decided to analyze the sequence. For that, he needs to find all values of x, for which these conditions hold: x occurs in sequence a. Consider all positions of numbers x in the sequence a (such i, that ai = x). These numbers, sorted in the increasing order, must form an arithmetic progression. Help Jeff, find all x that meet the problem conditions.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 105). The next line contains integers a1, a2, ..., an (1 ≤ ai ≤ 105). The numbers are separated by spaces.", "output_spec": "In the first line print integer t — the number of valid x. On each of the next t lines print two integers x and px, where x is current suitable value, px is the common difference between numbers in the progression (if x occurs exactly once in the sequence, px must equal 0). Print the pairs in the order of increasing x.", "sample_inputs": ["1\n2", "8\n1 2 1 3 1 2 1 5"], "sample_outputs": ["1\n2 0", "4\n1 2\n2 4\n3 0\n5 0"], "notes": "NoteIn the first test 2 occurs exactly once in the sequence, ergo p2 = 0."}, "positive_code": [{"source_code": "n=gets.to_i\nd,ar,br=0,Array.new(100001,-1),Array.new(100001,0)\na=gets.chomp.split.map(&:to_i)\na.length.times do |i|\n\tif ar[a[i]]==-1\n\t\tar[a[i]]=i\n\t\td+=1\n\telsif ar[a[i]]>=0\n\t\tif br[a[i]]==0\n\t\t\tbr[a[i]]=i-ar[a[i]]\n\t\t\tar[a[i]]=i\n\t\telsif br[a[i]]==i-ar[a[i]]\n\t\t\tar[a[i]]=i\n\t\telse\n\t\t\tar[a[i]]=-2\n\t\t\td-=1\n\t\tend\n\tend\nend\nputs d\n100001.times{|i| puts \"#{i} #{br[i]}\" if ar[i]>=0 }\n"}, {"source_code": "n = gets.to_i\narr = gets.split(\" \").map(&:to_i)\n\nindex_list = {}\narr.each_with_index do |a,i|\n\tif index_list[a] && index_list[a] != 0\n\t\tif index_list[a][-2]\n\n\t\t\tif i - index_list[a][-1] == index_list[a][-1] - index_list[a][-2]\n\t\t\t\t# puts \"i #{i}\"\n\t\t\t\tindex_list[a] << i\n\t\t\telse\n\t\t\t\tindex_list[a] = 0\n\t\t\tend\n\t\telse\n\t\t\tindex_list[a] << i\n\t\tend\n\telsif index_list[a] != 0\n\t\tindex_list[a] = [i]\n\tend\nend\n# \n# index_list = index_list.sort.to_h\n\ncount = index_list.values.count{|a| a!= 0}\n\nputs count\nindex_list.keys.sort.each do |k|\n\tv = index_list[k]\n\tif v != 0\n\t\td = 0\n\t\td = v[-1] - v[-2] if v.length > 1\n\t\tputs \"#{k} #{d}\"\n\tend\nend"}], "negative_code": [{"source_code": "n = gets.to_i\narr = gets.split(\" \").map(&:to_i)\n\nindex_list = {}\narr.each_with_index do |a,i|\n\tif index_list[a] && index_list[a] != 0\n\t\tif index_list[a][-2]\n\n\t\t\tif i - index_list[a][-1] == index_list[a][-1] - index_list[a][-2]\n\t\t\t\t# puts \"i #{i}\"\n\t\t\t\tindex_list[a] << i\n\t\t\telse\n\t\t\t\tindex_list[a] = 0\n\t\t\tend\n\t\telse\n\t\t\tindex_list[a] << i\n\t\tend\n\telsif index_list[a] != 0\n\t\tindex_list[a] = [i]\n\tend\nend\n\nindex_list.each do |k,v|\n\tif v != 0\n\t\td = 0\n\t\td = v[-1] - v[-2] if v.length > 1\n\t\tputs \"#{k} #{d}\"\n\tend\nend"}, {"source_code": "n = gets.to_i\narr = gets.split(\" \").map(&:to_i)\n\nindex_list = {}\narr.each_with_index do |a,i|\n\tif index_list[a] && index_list[a] != 0\n\t\tif index_list[a][-2]\n\n\t\t\tif i - index_list[a][-1] == index_list[a][-1] - index_list[a][-2]\n\t\t\t\t# puts \"i #{i}\"\n\t\t\t\tindex_list[a] << i\n\t\t\telse\n\t\t\t\tindex_list[a] = 0\n\t\t\tend\n\t\telse\n\t\t\tindex_list[a] << i\n\t\tend\n\telsif index_list[a] != 0\n\t\tindex_list[a] = [i]\n\tend\nend\n\ncount = index_list.values.count{|a| a!= 0}\nputs count\nindex_list.each do |k,v|\n\tif v != 0\n\t\td = 0\n\t\td = v[-1] - v[-2] if v.length > 1\n\t\tputs \"#{k} #{d}\"\n\tend\nend"}, {"source_code": "n = gets.to_i\narr = gets.split(\" \").map(&:to_i)\n\nindex_list = {}\narr.each_with_index do |a,i|\n\tif index_list[a] && index_list[a] != 0\n\t\tif index_list[a][-2]\n\n\t\t\tif i - index_list[a][-1] == index_list[a][-1] - index_list[a][-2]\n\t\t\t\t# puts \"i #{i}\"\n\t\t\t\tindex_list[a] << i\n\t\t\telse\n\t\t\t\tindex_list[a] = 0\n\t\t\tend\n\t\telse\n\t\t\tindex_list[a] << i\n\t\tend\n\telsif index_list[a] != 0\n\t\tindex_list[a] = [i]\n\tend\nend\n# \n# index_list = index_list.sort.to_h\n\ncount = index_list.values.count{|a| a!= 0}\n\nputs count\nindex_list.each do |k,v|\n\tif v != 0\n\t\td = 0\n\t\td = v[-1] - v[-2] if v.length > 1\n\t\tputs \"#{k} #{d}\"\n\tend\nend"}], "src_uid": "097e35b5e9c96259c54887158ebff544"} {"nl": {"description": "This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You may also refer to the guide on interactive problems: https://codeforces.com/blog/entry/45307.The jury picked an integer $$$x$$$ not less than $$$0$$$ and not greater than $$$2^{14} - 1$$$. You have to guess this integer.To do so, you may ask no more than $$$2$$$ queries. Each query should consist of $$$100$$$ integer numbers $$$a_1$$$, $$$a_2$$$, ..., $$$a_{100}$$$ (each integer should be not less than $$$0$$$ and not greater than $$$2^{14} - 1$$$). In response to your query, the jury will pick one integer $$$i$$$ ($$$1 \\le i \\le 100$$$) and tell you the value of $$$a_i \\oplus x$$$ (the bitwise XOR of $$$a_i$$$ and $$$x$$$). There is an additional constraint on the queries: all $$$200$$$ integers you use in the queries should be distinct.It is guaranteed that the value of $$$x$$$ is fixed beforehand in each test, but the choice of $$$i$$$ in every query may depend on the integers you send.", "input_spec": null, "output_spec": "To give the answer, your program should print one line $$$!$$$ $$$x$$$ with a line break in the end. After that, it should flush the output and terminate gracefully.", "sample_inputs": ["0\n32"], "sample_outputs": ["? 3 5 6\n? 32 24 37\n! 5"], "notes": "NoteThe example of interaction is not correct — you should sumbit exactly $$$100$$$ integers in each query. Everything else is correct.Hacks are forbidden in this problem."}, "positive_code": [{"source_code": "maskl = ((1 << 7) - 1) << 7\nmaskr = (1 << 7) - 1\n\na = (1..100).to_a\nputs ([\"?\"]+a).join(\" \")\nSTDOUT.flush\nl = gets.to_i\n\na.map!{|v| v << 7}\nputs ([\"?\"]+a).join(\" \")\nSTDOUT.flush\nr = gets.to_i\n\nans = (maskl & l) + (maskr & r)\nputs \"! #{ans}\"\n"}], "negative_code": [{"source_code": "maskl = (2 << 7 - 1) << 7\nmaskr = 2 << 7 - 1\n\na = (1..100).to_a\nputs ([\"?\"]+a).join(\" \")\nSTDOUT.flush\nl = gets.to_i\n\na.map!{|v| v << 7}\nputs ([\"?\"]+a).join(\" \")\nSTDOUT.flush\nr = gets.to_i\n\nans = (maskl & l) + (maskr & r)\nputs \"! #{ans}\"\n"}, {"source_code": "maskl = (2 << 7 - 1) << 7\nmaskr = 2 << 7 - 1\n\na = (0..99).to_a\nputs ([\"?\"]+a).join(\" \")\nSTDOUT.flush\nl = gets.to_i\n\na.map!{|v| v << 7}\nputs ([\"?\"]+a).join(\" \")\nSTDOUT.flush\nr = gets.to_i\n\nans = (maskl & l) + (maskr & r)\nputs \"! #{ans}\"\n"}], "src_uid": "c7f31e0c57cf15f71c401d826c3ee0ef"} {"nl": {"description": "Little Petya likes points a lot. Recently his mom has presented him n points lying on the line OX. Now Petya is wondering in how many ways he can choose three distinct points so that the distance between the two farthest of them doesn't exceed d.Note that the order of the points inside the group of three chosen points doesn't matter.", "input_spec": "The first line contains two integers: n and d (1 ≤ n ≤ 105; 1 ≤ d ≤ 109). The next line contains n integers x1, x2, ..., xn, their absolute value doesn't exceed 109 — the x-coordinates of the points that Petya has got. It is guaranteed that the coordinates of the points in the input strictly increase.", "output_spec": "Print a single integer — the number of groups of three points, where the distance between two farthest points doesn't exceed d. Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.", "sample_inputs": ["4 3\n1 2 3 4", "4 2\n-3 -2 -1 0", "5 19\n1 10 20 30 50"], "sample_outputs": ["4", "2", "1"], "notes": "NoteIn the first sample any group of three points meets our conditions.In the seconds sample only 2 groups of three points meet our conditions: {-3, -2, -1} and {-2, -1, 0}.In the third sample only one group does: {1, 10, 20}."}, "positive_code": [{"source_code": "#input = File.open('input.txt','r')\nn, d = gets.split\nn = n.to_i\nd = d.to_i\ncoordinates = gets.split\ncoordinates.length.times do |i|\n\tcoordinates[i] = coordinates[i].to_i\nend\n#input.close\nanswer = 0\nif n > 2\n\tx1 = 0\n\tx3 = 1\n\t# b = false\n\t# while x1 != coordinates.length - 2\n\t\t# #printf('%d %d ',x1,x3)\n\t\t# if coordinates[x3] - coordinates[x1] <= d\n\t\t\t# answer += x3 - x1 - 1\n\t\t\t# if x3 < coordinates.length - 1\n\t\t\t\t# x3 += 1\n\t\t\t# else\n\t\t\t\t# #answer += x3 - x1 - 1\n\t\t\t\t# x1 += 1\n\t\t\t\t# unless b\n\t\t\t\t\t# b = true\n\t\t\t\t# else\n\t\t\t\t\t# answer += (x3 - x1) * (x3 - x1 - 1) / 2\n\t\t\t\t# end\n\t\t\t\t# #x1 = coordinates.length - 2\n\t\t\t# end\n\t\t# else\n\t\t\t# x1 += 1\n\t\t\t# answer += (x3 - x1 - 1) * (x3 - x1 - 2) / 2\n\t\t# end\n\t\t# #puts answer\n\t# end\n\twhile x1 < coordinates.length\n\t\t#printf('%d ',x1)\n\t\twhile x3 < coordinates.length and coordinates[x3] - coordinates[x1] <= d\n\t\t\tx3 += 1\n\t\tend\n\t\t#printf('%d ',x3)\n\t\t#if coordinates[x3] - coordinates[x1] <= d\n\t\t\tanswer += (x3 - x1 - 1) * (x3 - x1 - 2) / 2\n\t\t#end\n\t\tx1 += 1\n\t\t# if x3 != coordinates.length - 1\n\t\t\t# x3 += 1\n\t\t# end\n\t\t#puts answer\n\tend\nend\nputs answer\n\n\n\n#assign a value to a variable only if that variable isn't already set:\n#@variable ||= \"default value\"\n\n#x, y = y, x will interchange the values of x and y\n\n#names2 = %w{ ann richard william susan pat } \n\n#puts \"Enrollments will now Stop\" if participants > 2500 \n\n#Negative index values count from the end of the array\n"}, {"source_code": "n,d = gets.chomp.split(\" \").collect! {|x| x.to_i}\nline = gets.chomp.split(\" \").collect! {|x| x.to_i}\n\narr = []\nline.each {|p|\n arr.push(p+d)\n}\n\ns = 0\nt = 0\nways = 0\n\n#puts n\nwhile sarr[s] \n\t\ts+=1\n\tend\n\tif s>=n\n\t\tbreak\n\tend\n\twhile t=line[t] \n\t#puts \"- #{t} #{s}\"\n\tif t-s>1\n\t\tways += (t-s-1)*(t-s)*0.5\n\t#\tputs \": #{t} #{s}\" \n\tend\n\tt +=1\n\tif t>=n \n\t #puts \"break\"\n\t\tbreak\n\tend\n\tend\n\t\nend\n\nputs ways.to_i"}], "negative_code": [{"source_code": "#input = File.open('input.txt','r')\nn, d = gets.split\nn = n.to_i\nd = d.to_i\ncoordinates = gets.split\ncoordinates.length.times do |i|\n\tcoordinates[i] = coordinates[i].to_i\nend\n#input.close\nanswer = 0\nif n > 2\n\tx1 = 0\n\tx3 = 2\n\tb = false\n\twhile x1 != coordinates.length - 2\n\t\t#printf('%d %d ',x1,x3)\n\t\tif coordinates[x3] - coordinates[x1] <= d\n\t\t\tanswer += x3 - x1 - 1\n\t\t\tif x3 < coordinates.length - 1\n\t\t\t\tx3 += 1\n\t\t\telse\n\t\t\t\t#answer += x3 - x1 - 1\n\t\t\t\tx1 += 1\n\t\t\t\tunless b\n\t\t\t\t\tb = true\n\t\t\t\telse\n\t\t\t\t\tanswer += (x3 - x1) * (x3 - x1 - 1) / 2\n\t\t\t\tend\n\t\t\t\t#x1 = coordinates.length - 2\n\t\t\tend\n\t\telse\n\t\t\tx1 += 1\n\t\t\tanswer += (x3 - x1 - 1) * (x3 - x1 - 2) / 2\n\t\tend\n\t\t#puts answer\n\tend\nend\nputs answer\n\n\n\n#assign a value to a variable only if that variable isn't already set:\n#@variable ||= \"default value\"\n\n#x, y = y, x will interchange the values of x and y\n\n#names2 = %w{ ann richard william susan pat } \n\n#puts \"Enrollments will now Stop\" if participants > 2500 \n\n#Negative index values count from the end of the array\n"}], "src_uid": "1f6491999bec55cb8d960181e830f4c8"} {"nl": {"description": "Vasya tries to break in a safe. He knows that a code consists of n numbers, and every number is a 0 or a 1. Vasya has made m attempts to enter the code. After each attempt the system told him in how many position stand the right numbers. It is not said in which positions the wrong numbers stand. Vasya has been so unlucky that he hasn’t entered the code where would be more than 5 correct numbers. Now Vasya is completely bewildered: he thinks there’s a mistake in the system and it is self-contradictory. Help Vasya — calculate how many possible code variants are left that do not contradict the previous system responses.", "input_spec": "The first input line contains two integers n and m (6 ≤ n ≤ 35, 1 ≤ m ≤ 10) which represent the number of numbers in the code and the number of attempts made by Vasya. Then follow m lines, each containing space-separated si and ci which correspondingly indicate Vasya’s attempt (a line containing n numbers which are 0 or 1) and the system’s response (an integer from 0 to 5 inclusively).", "output_spec": "Print the single number which indicates how many possible code variants that do not contradict the m system responses are left.", "sample_inputs": ["6 2\n000000 2\n010100 4", "6 3\n000000 2\n010100 4\n111100 0", "6 3\n000000 2\n010100 4\n111100 2"], "sample_outputs": ["6", "0", "1"], "notes": null}, "positive_code": [{"source_code": "$b = 4096.times.to_a.map{|i| i.to_s(2).count('1')}\n\ndef bits(x)\n\t$b[x & 4095] + $b[(x >> 12) & 4095] + $b[x >> 24]\nend\n\nn, m = gets.split.map(&:to_i)\na = []\nm.times do |t|\n\ts, c = gets.split\n\ts = s.to_i(2)\n\tc = c.to_i\n\tif t.zero? then\n\t\t[*0..n-1].combination(c).each do |i|\n\t\t\ta << s\n\t\t\ti.each{|j| a[-1] ^= 1 << j}\n\t\tend\n\telse\n\t\ta.reject!{|i| bits(i ^ s) != c}\n\tend\nend\np a.size\n\n__END__\n: || 0\n"}], "negative_code": [{"source_code": "n, m = gets.split.map(&:to_i)\na = []\nm.times do |t|\n\ts, c = gets.split\n\ts = s.to_i(2)\n\tc = c.to_i\n\tif t.zero? then\n\t\ta = [*0..n-1].combination(n - c).to_a.map{|i| i.map{|j| 1 << j}.inject{|x, y| x ^ y} || 0}\n\telse\n\t\ta.reject!{|i| (i ^ s).to_s(2).count('1') != n - c}\n\tend\nend\np a.size\n\n__END__\n: || 0\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\na = []\nm.times do |t|\n\ts, c = gets.split\n\ts = s.to_i(2)\n\tc = c.to_i\n\tif t.zero? then\n\t\ta = [*0..n-1].combination(c).to_a.map{|i| i.map{|j| 1 << j}.inject{|i, j| i ^ j} || 0}\n\telse\n\t\ta.reject!{|i| (i ^ s).to_s(2).count('1') != c}\n\tend\nend\np a.size\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\na = []\nm.times do |t|\n\ts, c = gets.split\n\ts = s.to_i(2)\n\tc = c.to_i\n\tif t.zero? then\n\t\ta = [*0..n-1].combination(c).to_a.map{|i| i.map{|j| 1 << j}.inject{|x, y| x ^ y} || 0}\n\telse\n\t\ta.reject!{|i| (i ^ s).to_s(2).count('1') != c}\n\tend\nend\np a.size\n\n__END__\n: || 0\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\na = []\nm.times do |t|\n\ts, c = gets.split\n\ts = s.to_i(2)\n\tc = c.to_i\n\tif t.zero? then\n\t\ta = [*0..n-1].combination(c).to_a.map{|i| i.map{|j| 1 << j}.inject{|i, j| i ^ j}}\n\telse\n\t\ta.reject!{|i| (i ^ s).to_s(2).count('1') != c}\n\tend\nend\np a.size\n"}], "src_uid": "5215112549723fea3f2c1fe0049e0b2e"} {"nl": {"description": "Vasya’s elder brother Petya loves playing computer games. In one of his favourite computer games Petya reached the final level where a fight with the boss take place.While playing the game Petya found spell scrolls and now he is about to use them. Let’s describe the way fighting goes on this level:1) The boss has two parameters: max — the initial amount of health and reg — regeneration rate per second.2) Every scroll also has two parameters: powi — spell power measured in percents — the maximal amount of health counted off the initial one, which allows to use the scroll (i.e. if the boss has more than powi percent of health the scroll cannot be used); and dmgi the damage per second inflicted upon the boss if the scroll is used. As soon as a scroll is used it disappears and another spell is cast upon the boss that inflicts dmgi of damage per second upon him until the end of the game.During the battle the actions per second are performed in the following order: first the boss gets the damage from all the spells cast upon him, then he regenerates reg of health (at the same time he can’t have more than max of health), then the player may use another scroll (no more than one per second).The boss is considered to be defeated if at the end of a second he has nonpositive ( ≤ 0) amount of health.Help Petya to determine whether he can win with the set of scrolls available to him and if he can, determine the minimal number of seconds he needs to do it.", "input_spec": "The first line contains three integers N, max and reg (1 ≤ N, max, reg ≤ 1000) –– the amount of scrolls and the parameters of the boss. The next N lines contain two integers powi and dmgi each — the parameters of the i-th scroll (0 ≤ powi ≤ 100, 1 ≤ dmgi ≤ 2000). ", "output_spec": "In case Petya can’t complete this level, output in the single line NO. Otherwise, output on the first line YES. On the second line output the minimal time after which the boss can be defeated and the number of used scrolls. In the next lines for each used scroll output space-separated number of seconds passed from the start of the battle to the moment the scroll was used and the number of the scroll. Scrolls are numbered starting from 1 in the input order. The first scroll is considered to be available to be used after 0 seconds. Output scrolls in the order they were used. It is not allowed to use scrolls after the boss is defeated.", "sample_inputs": ["2 10 3\n100 3\n99 1", "2 100 10\n100 11\n90 9"], "sample_outputs": ["NO", "YES\n19 2\n0 1\n10 2"], "notes": null}, "positive_code": [{"source_code": "class Spell\n\tdef initialize(idx, power, dmg)\n\t\t@idx = idx\n\t\t@power = power\n\t\t@dmg = dmg\n\t\t@used = false\n\tend\n\tattr_accessor :idx, :power, :dmg, :used\nend\n\t\nn,mx,reg = gets.chomp.split(/ /).map!{|x|x.to_i}\nra = Array.new(n)\nret = true\nsa = Array.new\nfor i in 0..n-1\n\ta = gets.chomp.split(/ /).map!{|x|x.to_i}\n\tra[i] = Spell.new( i+1, a[0], a[1] )\nend\n\nra.sort!{|a,b| b.dmg <=> a.dmg }\n\nmxw = mx\ndmgpermin = 0\nmtime = 0\n\nwhile mxw > 0\n\tprevmxw = mxw\n\tnotfound = true\n\tfor i in 0..n-1\n\t\tnext if ra[i].used\n\t\tnext if ra[i].power*mx < mxw*100\n\t\tdmgpermin += ra[i].dmg\n\t\tra[i].used = true\n\t\tsa.push( mtime.to_s + \" \" + ra[i].idx.to_s )\n\t\tnotfound = false\n\t\tbreak\n\tend\n\tmxw -= dmgpermin\n\tmxw += reg\n\tmxw = mx if mxw > mx\n\tmtime += 1\n\n\tif notfound && prevmxw <= mxw\n\t\tret = false\n\t\tbreak\n\tend\nend\n\nret = false if mxw > 0\n\nif ret\n\tputs \"YES\"\n\tputs mtime.to_s + \" \" + sa.length.to_s\n\tsa.each{|x| puts x }\nelse\n\tputs \"NO\"\nend"}], "negative_code": [{"source_code": "class Spell\n\tdef initialize(idx, power, dmg)\n\t\t@idx = idx\n\t\t@power = power\n\t\t@dmg = dmg\n\t\t@used = false\n\tend\n\tattr_accessor :idx, :power, :dmg, :used\nend\n\t\nn,mx,reg = gets.chomp.split(/ /).map!{|x|x.to_i}\nra = Array.new(n)\nret = true\nsa = Array.new\nfor i in 0..n-1\n\ta = gets.chomp.split(/ /).map!{|x|x.to_i}\n\tra[i] = Spell.new( i+1, a[0], a[1] )\nend\n\nra.sort!{|a,b| b.dmg <=> a.dmg }\n\nmxw = mx\ndmgpermin = 0\nmtime = 0\n\nwhile mxw > 0\n\tprevmxw = mxw\n\tnotfound = true\n\tfor i in 0..n-1\n\t\tnext if ra[i].used\n\t\tnext if ra[i].power*mx < mxw*100\n\t\tdmgpermin += ra[i].dmg\n\t\tra[i].used = true\n\t\tsa.push( mtime.to_s + \" \" + ra[i].idx.to_s )\n\t\tnotfound = false\n\t\tbreak\n\tend\n\tmxw -= dmgpermin\n\tmxw += reg\n\tmxw = mx if mxw > mx\n\n\tbreak if mxw < 0\n\n\tmtime += 1\n\n\tif notfound && prevmxw <= mxw\n\t\tret = false\n\t\tbreak\n\tend\nend\n\nret = false if mxw > 0\n\nif ret\n\tputs \"YES\"\n\tputs mtime.to_s + \" \" + sa.length.to_s\n\tsa.each{|x| puts x }\nelse\n\tputs \"NO\"\nend"}, {"source_code": "class Spell\n\tdef initialize(idx, power, dmg)\n\t\t@idx = idx\n\t\t@power = power\n\t\t@dmg = dmg\n\t\t@used = false\n\tend\n\tattr_accessor :idx, :power, :dmg, :used\nend\n\t\nn,mx,reg = gets.chomp.split(/ /).map!{|x|x.to_i}\nra = Array.new(n)\nret = true\nsa = Array.new\nfor i in 0..n-1\n\ta = gets.chomp.split(/ /).map!{|x|x.to_i}\n\tra[i] = Spell.new( i+1, a[0], a[1] )\nend\n\nra.sort!{|a,b| b.dmg <=> a.dmg }\n\nmxw = mx\ndmgpermin = 0\nmtime = 0\n\nwhile true\n\tprevmxw = mxw\n\tfor i in 0..n-1\n\t\tnext if ra[i].used\n\t\tnext if ra[i].power*mx < mxw*100\n\t\tdmgpermin += ra[i].dmg\n\t\tra[i].used = true\n\t\tsa.push( mtime.to_s + \" \" + ra[i].idx.to_s )\n\tend\n\tmxw -= dmgpermin\n\tmxw += reg\n\tmxw = mx if mxw > mx\n\n\tbreak if mxw < 0\n\n\tmtime += 1\n\n\tif prevmxw <= mxw\n\t\tret = false\n\t\tbreak\n\tend\nend\n\nret = false if mxw > 0\n\nif ret\n\tputs \"YES\"\n\tputs mtime.to_s + \" \" + sa.length.to_s\n\tsa.each{|x| puts x }\nelse\n\tputs \"NO\"\nend"}, {"source_code": "class Spell\n\tdef initialize(idx, power, dmg)\n\t\t@idx = idx\n\t\t@power = power\n\t\t@dmg = dmg\n\t\t@used = false\n\tend\n\tattr_accessor :idx, :power, :dmg, :used\nend\n\t\nn,mx,reg = gets.chomp.split(/ /).map!{|x|x.to_i}\nra = Array.new(n)\nret = true\nsa = Array.new\nfor i in 0..n-1\n\ta = gets.chomp.split(/ /).map!{|x|x.to_i}\n\tra[i] = Spell.new( i+1, a[0], a[1] )\nend\n\nra.sort!{|a,b| b.dmg <=> a.dmg }\n\nmxw = mx\ndmgpermin = 0\nmtime = 0\n\nwhile true\n\tprevmxw = mxw\n\tnotfound = true\n\tfor i in 0..n-1\n\t\tnext if ra[i].used\n\t\tnext if ra[i].power*mx < mxw*100\n\t\tdmgpermin += ra[i].dmg\n\t\tra[i].used = true\n\t\tsa.push( mtime.to_s + \" \" + ra[i].idx.to_s )\n\t\tnotfound = false\n\tend\n\tmxw -= dmgpermin\n\tmxw += reg\n\tmxw = mx if mxw > mx\n\n\tbreak if mxw < 0\n\n\tmtime += 1\n\tif notfound && prevmxw <= mxw\n\t\tret = false\n\t\tbreak\n\tend\nend\n\nret = false if mxw > 0\n\nif ret\n\tputs \"YES\"\n\tputs mtime.to_s + \" \" + sa.length.to_s\n\tsa.each{|x| puts x }\nelse\n\tputs \"NO\"\nend"}, {"source_code": "class Spell\n\tdef initialize(idx, power, dmg)\n\t\t@idx = idx\n\t\t@power = power\n\t\t@dmg = dmg\n\t\t@used = false\n\tend\n\tattr_accessor :idx, :power, :dmg, :used\nend\n\t\nn,mx,reg = gets.chomp.split(/ /).map!{|x|x.to_i}\nra = Array.new(n)\nret = true\nsa = Array.new\nfor i in 0..n-1\n\ta = gets.chomp.split(/ /).map!{|x|x.to_i}\n\tra[i] = Spell.new( i+1, a[0], a[1] )\nend\n\nra.sort!{|a,b| b.dmg <=> a.dmg }\n\nmxw = mx\ndmgpermin = 0\nmtime = 0\n\nwhile true\n\tprevmxw = mxw\n\tfor i in 0..n-1\n\t\tnext if ra[i].used\n\t\tnext if ra[i].power < mxw\n\t\tdmgpermin += ra[i].dmg\n\t\tra[i].used = true\n\t\tsa.push( mtime.to_s + \" \" + ra[i].idx.to_s )\n\tend\n\tmxw -= dmgpermin\n\tmxw += reg\n\tmxw = mx if mxw > mx\n\n\tbreak if mxw < 0\n\n\tmtime += 1\n\n\tif prevmxw <= mxw\n\t\tret = false\n\t\tbreak\n\tend\nend\n\nret = false if mxw > 0\n\nif ret\n\tputs \"YES\"\n\tputs mtime.to_s + \" \" + sa.length.to_s\n\tsa.each{|x| puts x }\nelse\n\tputs \"NO\"\nend"}, {"source_code": "class Spell\n\tdef initialize(idx, power, dmg)\n\t\t@idx = idx\n\t\t@power = power\n\t\t@dmg = dmg\n\t\t@used = false\n\tend\n\tattr_accessor :idx, :power, :dmg, :used\nend\n\t\nn,mx,reg = gets.chomp.split(/ /).map!{|x|x.to_i}\nra = Array.new(n)\nret = true\nsa = Array.new\nfor i in 0..n-1\n\ta = gets.chomp.split(/ /).map!{|x|x.to_i}\n\tra[i] = Spell.new( i+1, a[0], a[1] )\nend\n\nra.sort!{|a,b| b.dmg <=> a.dmg }\n\nmxw = mx\ndmgpermin = 0\nmtime = 0\n\nwhile true\n\tprevmxw = mxw\n\tnotfound = true\n\tfor i in 0..n-1\n\t\tnext if ra[i].used\n\t\tnext if ra[i].power*mx < mxw*100\n\t\tdmgpermin += ra[i].dmg\n\t\tra[i].used = true\n\t\tsa.push( mtime.to_s + \" \" + ra[i].idx.to_s )\n\t\tnotfound = false\n\t\tbreak\n\tend\n\tmxw -= dmgpermin\n\tmxw += reg\n\tmxw = mx if mxw > mx\n\n\tbreak if mxw < 0\n\n\tmtime += 1\nif mtime > 10000\t\n\tret = false\n\tbreak\nend\n#\tif notfound && prevmxw <= mxw\n#\t\tret = false\n#\t\tbreak\n#\tend\nend\n\nret = false if mxw > 0\n\nif ret\n\tputs \"YES\"\n\tputs mtime.to_s + \" \" + sa.length.to_s\n\tsa.each{|x| puts x }\nelse\n\tputs \"NO\"\nend"}, {"source_code": "class Spell\n\tdef initialize(idx, power, dmg)\n\t\t@idx = idx\n\t\t@power = power\n\t\t@dmg = dmg\n\t\t@used = false\n\tend\n\tattr_accessor :idx, :power, :dmg, :used\nend\n\t\nn,mx,reg = gets.chomp.split(/ /).map!{|x|x.to_i}\nra = Array.new(n)\nret = true\nsa = Array.new\nfor i in 0..n-1\n\ta = gets.chomp.split(/ /).map!{|x|x.to_i}\n\tra[i] = Spell.new( i+1, a[0], a[1] )\nend\n\nra.sort!{|a,b| b.dmg <=> a.dmg }\n\nmxw = mx\ndmgpermin = 0\nmtime = 0\n\nwhile true\n\tprevmxw = mxw\n\tnotfound = true\n\tfor i in 0..n-1\n\t\tnext if ra[i].used\n\t\tnext if ra[i].power*mx < mxw*100\n\t\tdmgpermin += ra[i].dmg\n\t\tra[i].used = true\n\t\tsa.push( mtime.to_s + \" \" + ra[i].idx.to_s )\n\t\tnotfound = false\n\t\tbreak\n\tend\n\tmxw -= dmgpermin\n\tmxw += reg\n\tmxw = mx if mxw > mx\n\n\tbreak if mxw < 0\n\n\tmtime += 1\n\tif notfound && prevmxw <= mxw\n\t\tret = false\n\t\tbreak\n\tend\nend\n\nret = false if mxw > 0\n\nif ret\n\tputs \"YES\"\n\tputs mtime.to_s + \" \" + sa.length.to_s\n\tsa.each{|x| puts x }\nelse\n\tputs \"NO\"\nend"}], "src_uid": "e9c486e2d942700e0644dff29b6e3be6"} {"nl": {"description": "This is an interactive problem.Vasya and Petya are going to play the following game: Petya has some positive integer number $$$a$$$. After that Vasya should guess this number using the following questions. He can say a pair of non-negative integer numbers $$$(x, y)$$$. Petya will answer him: \"x\", if $$$(x \\bmod a) \\geq (y \\bmod a)$$$. \"y\", if $$$(x \\bmod a) < (y \\bmod a)$$$. We define $$$(x \\bmod a)$$$ as a remainder of division $$$x$$$ by $$$a$$$.Vasya should guess the number $$$a$$$ using no more, than 60 questions.It's guaranteed that Petya has a number, that satisfies the inequality $$$1 \\leq a \\leq 10^9$$$.Help Vasya playing this game and write a program, that will guess the number $$$a$$$.", "input_spec": null, "output_spec": null, "sample_inputs": ["start\nx\nx\nstart\nx\nx\ny\nstart\nx\nx\ny\ny\nend"], "sample_outputs": ["? 0 0\n? 10 1\n! 1\n? 0 0\n? 3 4\n? 2 5\n! 2\n? 2 4\n? 2 5\n? 3 10\n? 9 1\n! 3"], "notes": "NoteIn the first test, you should play $$$3$$$ games with Petya's numbers $$$1$$$, $$$2$$$ and $$$3$$$.In the first game, Petya will answer \"x\" (without quotes) to any question, because $$$(x \\bmod 1) = 0$$$ for any integer $$$x$$$. In the second game, if you will ask pair $$$(0, 0)$$$, the answer will be \"x\" (without quotes), because $$$(0 \\bmod 2) \\geq (0 \\bmod 2)$$$. But if you will ask pair $$$(2, 5)$$$, the answer will be \"y\" (without quotes), because $$$(2 \\bmod 2) < (5 \\bmod 2)$$$, because $$$(2 \\bmod 2) = 0$$$ and $$$(5 \\bmod 2) = 1$$$."}, "positive_code": [{"source_code": "require 'prime'\n\np = Prime.each(10**5).to_a\n\nloop do\n s = gets.chomp\n break if s == \"end\"\n min = 0\n max = 10**9\n while max-min > 1\n mid = (min+max)/2\n puts \"? #{mid} #{max}\"\n STDOUT.flush\n r = gets.chomp\n if r == \"x\"\n min = mid\n else\n max = mid\n end\n end\n m = max\n ret = 1\n p.each do |i|\n a = 0\n while m%i == 0\n a += 1\n puts \"? #{max} #{max/(i**a)}\"\n STDOUT.flush\n r = gets.chomp\n if r == \"y\"\n ret *= i\n end\n m /= i\n end\n end\n puts \"? #{max} #{max/m}\"\n STDOUT.flush\n r = gets.chomp\n if r == \"y\"\n ret *= m\n end\n\n puts \"! #{ret}\"\n STDOUT.flush\nend"}, {"source_code": "DBG = !true\ndef outp(str)\n puts str\n STDOUT.flush\nend\n\ndef lt(x,y)\n hashkey = x.to_s + \".\" + y.to_s\n if $h.has_key?(hashkey)\n return $h[hashkey]\n end\n outp(\"? #{x} #{y}\")\n s = gets.chomp\n exit(0) if s == 'mistake'\n $h[hashkey] = (s=='y')\n return (s=='y')\nend\n\n$h = {}\nR = (DBG ? 10 : 1000000000)\nMONO = 1\nSPLIT = 2\nS1N = 3\nwhile true\n $h.clear()\n inp = gets.chomp\n exit(0) if inp == \"end\" || inp == \"mistake\"\n next if inp != 'start'\n st = MONO\n s = R\n b = t = -1\n while true\n puts \"st #{st} s #{s} b #{b} t #{t}\" if DBG\n if st == MONO\n if s == 2\n st = S1N\n b = t = 2\n next\n end\n p = s - 2*(s/3)\n q = s - s/3\n if lt(p, q)\n st = SPLIT\n t = s\n s = p\n b = q + 1\n else\n s = q\n end\n\n elsif st == SPLIT\n if s == 1\n st = S1N\n next\n end\n p = s - s/2\n if lt(p,s)\n s = p\n else\n st = MONO\n b = t = -1\n end\n else # st = S1N\n if b < t\n p = (b+t)/2\n if lt(p,t)\n t = p\n else\n b = (b==t-1 ? t : p+1)\n end\n else # b == t\n if lt(t,1)\n outp(\"! #{t}\")\n else\n outp(\"! 1\")\n end\n break\n end\n end\n end\nend\n"}, {"source_code": "\n@local = false\n@answer = 1\n@gamecnt = 100\n\ndef continue_game\n if @local\n return false if @gamecnt <= 0\n @gamecnt -= 1\n @querycnt = 0\n return true\n else\n line = gets.chomp\n exit if line == \"mistake\"\n return line == \"start\"\n end\nend\n\ndef query_game(x,y)\n if @local\n @querycnt += 1\n return x%@answer >= y%@answer\n else\n STDOUT.puts \"? #{x} #{y}\"\n STDOUT.flush\n return gets[0] == \"x\"\n end\nend\n\ndef answer_game(a)\n if @local\n abort \"#{@answer} != #{a}\" if @answer != a\n puts \"AC:#{@querycnt}\"\n @answer = rand(1..(1e9.to_i))\n else\n STDOUT.puts \"! #{a}\"\n STDOUT.flush\n end\nend\n\n# \n\nwhile continue_game\n \n x = 1\n y = 0\n \n while x <= 2e9.to_i && query_game(x, y)\n x, y = x*2, x\n end\n if x > 2e9\n answer_game(1)\n next\n end\n while x-y > 1\n m = (x+y)/2\n if query_game(m, y)\n y = m\n else\n x = m\n end\n end\n answer_game(x)\nend\n\n\n"}], "negative_code": [{"source_code": "loop do\n s = gets.chomp\n break if s == \"end\"\n min = 0\n max = 10**9\n while max-min > 1\n mid = (min+max)/2\n puts \"? #{mid} #{max}\"\n STDOUT.flush\n r = gets.chomp\n if r == \"x\"\n min = mid\n else\n max = mid\n end\n end\n m = max\n for i in 2..31623\n while m%i == 0\n puts \"? #{max} #{max/i}\"\n STDOUT.flush\n r = gets.chomp\n if r == \"y\"\n break\n else\n max /= i\n end\n m /= i\n end\n while m%i == 0\n m /= i\n end\n end\n puts \"! #{max}\"\n STDOUT.flush\nend"}, {"source_code": "require 'prime'\n\np = Prime.each(10**5).to_a\n\nloop do\n s = gets.chomp\n break if s == \"end\"\n min = 0\n max = 10**9\n while max-min > 1\n mid = (min+max)/2\n puts \"? #{mid} #{max}\"\n STDOUT.flush\n r = gets.chomp\n if r == \"x\"\n min = mid\n else\n max = mid\n end\n end\n m = max\n ret = 1\n p.each do |i|\n a = 0\n while m%i == 0\n a += 1\n puts \"? #{max} #{max/i}\"\n STDOUT.flush\n r = gets.chomp\n if r == \"y\"\n ret *= i\n end\n m /= i\n end\n end\n puts \"? #{max} #{max/m}\"\n STDOUT.flush\n r = gets.chomp\n if r == \"y\"\n ret *= m\n end\n\n puts \"! #{max}\"\n STDOUT.flush\nend"}, {"source_code": "require 'prime'\n\np = Prime.each(10**5).to_a\n\nloop do\n s = gets.chomp\n break if s == \"end\"\n min = 0\n max = 10**9\n while max-min > 1\n mid = (min+max)/2\n puts \"? #{mid} #{max}\"\n STDOUT.flush\n r = gets.chomp\n if r == \"x\"\n min = mid\n else\n max = mid\n end\n end\n m = max\n ret = 1\n p.each do |i|\n a = 0\n while m%i == 0\n a += 1\n puts \"? #{max} #{max/i}\"\n STDOUT.flush\n r = gets.chomp\n if r == \"y\"\n ret *= i\n end\n m /= i\n end\n end\n puts \"? #{max} #{max/m}\"\n STDOUT.flush\n r = gets.chomp\n if r == \"y\"\n ret *= m\n end\n\n puts \"! #{ret}\"\n STDOUT.flush\nend"}, {"source_code": "def ret(a,x,y)\n if x%a >= y%a\n return \"x\"\n else\n return \"y\"\n end\nend\n\n\nloop do\n s = gets.chomp\n break if s == \"end\"\n min = 0\n max = 10**9\n #ans = rand(1..10**9)\n #p ans\n while max-min > 1\n mid = (min+max)/2\n puts \"? #{mid} #{max}\"\n STDOUT.flush\n r = gets.chomp\n #r = ret(ans,mid,max)\n if r == \"x\"\n min = mid\n else\n max = mid\n end\n end\n for i in 2..31623\n while max%i == 0\n puts \"? #{max} #{max/i}\"\n STDOUT.flush\n r = gets.chomp\n #r = ret(ans,max,max/i)\n if r == \"y\"\n break\n else\n max /= i\n end\n end\n end\n puts \"! #{max}\"\n STDOUT.flush\nend"}, {"source_code": "require 'prime'\n\n\nloop do\n s = gets.chomp\n break if s == \"end\"\n min = 0\n max = 10**9\n while max-min > 1\n mid = (min+max)/2\n puts \"? #{mid} #{max}\"\n STDOUT.flush\n r = gets.chomp\n if r == \"x\"\n min = mid\n else\n max = mid\n end\n end\n m = max\n Prime.each(10**5) do |i|\n break if i > m || m%i != 0\n while m%i == 0\n puts \"? #{max} #{max/i}\"\n STDOUT.flush\n r = gets.chomp\n if r == \"x\"\n max /= i\n else\n break\n end\n m /= i\n end\n while m%i == 0\n m /= i\n end\n puts \"? #{max} #{i}\"\n STDOUT.flush\n r = gets.chomp\n if r == \"x\"\n max = i\n m = i\n end\n end\n puts \"! #{max}\"\n STDOUT.flush\nend"}, {"source_code": "def ret(a,x,y)\n if x%a >= y%a\n return \"x\"\n else\n return \"y\"\n end\nend\n\n\nloop do\n s = gets.chomp\n break if s == \"end\"\n min = 0\n max = 10**9\n #ans = rand(1..10**9)\n #p ans\n while max-min > 1\n mid = (min+max)/2\n puts \"? #{mid} #{min}\"\n STDOUT.flush\n r = gets.chomp\n #r = ret(ans,mid,max)\n if r == \"x\"\n min = mid\n else\n max = mid\n end\n end\n for i in 2..31623\n while max%i == 0\n puts \"? #{max} #{max/i}\"\n STDOUT.flush\n r = gets.chomp\n #r = ret(ans,max,max/i)\n if r == \"y\"\n break\n else\n max /= i\n end\n end\n end\n puts \"! #{max}\"\n STDOUT.flush\nend"}, {"source_code": "DBG = !true\ndef outp(str)\n puts str\n STDOUT.flush\nend\n\ndef lt(x,y)\n outp(\"? #{x} #{y}\")\n s = gets.chomp\n exit(0) if s == 'mistake'\n return (s=='y')\nend\n\nR = (DBG ? 10 : 1000000000)\nMONO = 1\nSPLIT = 2\nS1N = 3\nwhile true\n inp = gets.chomp\n exit(0) if inp == \"end\" || inp == \"mistake\"\n next if inp != 'start'\n st = MONO\n s = R\n b = t = -1\n while true\n puts \"st #{st} s #{s} b #{b} t #{t}\" if DBG\n if st == MONO\n if s == 2\n st = S1N\n b = t = 2\n next\n end\n p = s - 2*(s/3)\n q = s - s/3\n if lt(p, q)\n st = SPLIT\n t = s\n s = p\n b = q\n else\n s = q\n end\n\n elsif st == SPLIT\n if s == 1\n st = S1N\n next\n end\n p = s - s/2\n if lt(p,s)\n s = p\n else\n st = MONO\n b = t = -1\n end\n else # st = S1N\n if b < t\n p = (b+t)/2\n if lt(p,t)\n t = p\n else\n b = (b==t-1 ? t : p)\n end\n else # b == t\n if lt(t,1)\n outp(\"! #{t}\")\n else\n outp(\"! 1\")\n end\n break\n end\n end\n end\nend\n"}, {"source_code": "DBG = !true\ndef outp(str)\n puts str\n STDOUT.flush\nend\n\ndef lt(x,y)\n hashkey = x.to_s + \".\" + y.to_s\n if $h.has_key?(hashkey)\n return $h[hashkey]\n end\n outp(\"? #{x} #{y}\")\n s = gets.chomp\n exit(0) if s == 'mistake'\n $h[hashkey] = (s=='y')\n return (s=='y')\nend\n\n$h = {}\nR = (DBG ? 10 : 1000000000)\nMONO = 1\nSPLIT = 2\nS1N = 3\nwhile true\n $h.clear()\n inp = gets.chomp\n exit(0) if inp == \"end\" || inp == \"mistake\"\n next if inp != 'start'\n st = MONO\n s = R\n b = t = -1\n while true\n puts \"st #{st} s #{s} b #{b} t #{t}\" if DBG\n if st == MONO\n if s == 2\n st = S1N\n b = t = 2\n next\n end\n p = s - 2*(s/3)\n q = s - s/3\n if lt(p, q)\n st = SPLIT\n t = s\n s = p\n b = q + 1\n else\n s = q\n end\n\n elsif st == SPLIT\n if s == 1\n st = S1N\n next\n end\n p = s - s/2\n if lt(p,s)\n s = p\n else\n st = MONO\n b = t = -1\n end\n else # st = S1N\n if b < t\n p = (b+t)/2\n if lt(p,t)\n t = p\n else\n b = (b==t-1 ? t : p)\n end\n else # b == t\n if lt(t,1)\n outp(\"! #{t}\")\n else\n outp(\"! 1\")\n end\n break\n end\n end\n end\nend\n"}, {"source_code": "DBG = !true\ndef outp(str)\n puts str\n STDOUT.flush\nend\n\ndef lt(x,y)\n hashkey = x.to_s + \".\" + y.to_s\n if $h.has_key?(hashkey)\n return $h[hashkey]\n end\n outp(\"? #{x} #{y}\")\n s = gets.chomp\n exit(0) if s == 'mistake'\n $h[hashkey] = (s=='y')\n return (s=='y')\nend\n\n$h = {}\nR = (DBG ? 10 : 1000000000)\nMONO = 1\nSPLIT = 2\nS1N = 3\nwhile true\n $h.clear()\n inp = gets.chomp\n exit(0) if inp == \"end\" || inp == \"mistake\"\n next if inp != 'start'\n st = MONO\n s = R\n b = t = -1\n while true\n puts \"st #{st} s #{s} b #{b} t #{t}\" if DBG\n if st == MONO\n if s == 2\n st = S1N\n b = t = 2\n next\n end\n p = s - 2*(s/3)\n q = s - s/3\n if lt(p, q)\n st = SPLIT\n t = s\n s = p\n b = q\n else\n s = q\n end\n\n elsif st == SPLIT\n if s == 1\n st = S1N\n next\n end\n p = s - s/2\n if lt(p,s)\n s = p\n else\n st = MONO\n b = t = -1\n end\n else # st = S1N\n if b < t\n p = (b+t)/2\n if lt(p,t)\n t = p\n else\n b = (b==t-1 ? t : p)\n end\n else # b == t\n if lt(t,1)\n outp(\"! #{t}\")\n else\n outp(\"! 1\")\n end\n break\n end\n end\n end\nend\n"}, {"source_code": "DBG = true\ndef outp(str)\n puts str\n STDOUT.flush\nend\n\ndef lt(x,y)\n outp(\"? #{x} #{y}\")\n s = gets.chomp\n exit(0) if s == 'mistake'\n return (s=='y')\nend\n\nR = (DBG ? 10 : 1000000000)\nMONO = 1\nSPLIT = 2\nS1N = 3\nwhile true\n inp = gets.chomp\n exit(0) if inp == \"end\" || inp == \"mistake\"\n next if inp != 'start'\n st = MONO\n s = R\n b = t = -1\n while true\n puts \"st #{st} s #{s} b #{b} t #{t}\" if DBG\n if st == MONO\n if s == 2\n st = S1N\n b = t = 2\n next\n end\n p = s - 2*(s/3)\n q = s - s/3\n if lt(p, q)\n st = SPLIT\n t = s\n s = p\n b = q\n else\n s = q\n end\n\n elsif st == SPLIT\n if s == 1\n st = S1N\n next\n end\n p = s - s/2\n if lt(p,s)\n s = p\n else\n st = MONO\n b = t = -1\n end\n else # st = S1N\n if b < t\n p = (b+t)/2\n if lt(p,t)\n t = p\n else\n b = (b==t-1 ? t : p)\n end\n else # b == t\n if lt(t,1)\n outp(\"! #{t}\")\n else\n outp(\"! 1\")\n end\n break\n end\n end\n end\nend\n"}, {"source_code": "\n@local = false\n@answer = 1\n@trycnt = 29\n\ndef continue_game\n if @local\n return false if @trycnt <= 0\n @trycnt -= 1\n return true\n else\n line = gets.chomp\n exit if line == \"mistake\"\n return line == \"start\"\n end\nend\n\ndef query_game(x,y)\n if @local\n return x%@answer >= y%@answer\n else\n STDOUT.puts \"? #{x} #{y}\"\n STDOUT.flush\n return gets[0] == \"x\"\n end\nend\n\ndef answer_game(a)\n if @local\n abort if @answer != a\n puts \"AC\"\n @answer = rand(1..100000)\n else\n STDOUT.puts \"! #{a}\"\n STDOUT.flush\n end\nend\n\n# \n\nwhile continue_game\n \n if query_game(0, 1)\n answer_game(1)\n next\n end\n x = 1\n y = 0\n \n while query_game(x, y)\n x, y = x*2, x\n end\n while x-y > 1\n m = (x+y)/2\n if query_game(m, y)\n y = m\n else\n x = m\n end\n end\n answer_game(x)\nend\n\n\n"}], "src_uid": "eab8e5ac203d9f10c893ea35d249fe84"} {"nl": {"description": "The only difference between easy and hard versions is the constraints.Polycarp has to write a coursework. The coursework consists of $$$m$$$ pages.Polycarp also has $$$n$$$ cups of coffee. The coffee in the $$$i$$$-th cup Polycarp has $$$a_i$$$ caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days).Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least).Let's consider some day of Polycarp's work. Consider Polycarp drinks $$$k$$$ cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are $$$a_{i_1}, a_{i_2}, \\dots, a_{i_k}$$$. Then the first cup he drinks gives him energy to write $$$a_{i_1}$$$ pages of coursework, the second cup gives him energy to write $$$max(0, a_{i_2} - 1)$$$ pages, the third cup gives him energy to write $$$max(0, a_{i_3} - 2)$$$ pages, ..., the $$$k$$$-th cup gives him energy to write $$$max(0, a_{i_k} - k + 1)$$$ pages.If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day.Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible.", "input_spec": "The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$, $$$1 \\le m \\le 10^9$$$) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$), where $$$a_i$$$ is the caffeine dosage of coffee in the $$$i$$$-th cup.", "output_spec": "If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it.", "sample_inputs": ["5 8\n2 3 1 1 2", "7 10\n1 3 4 2 1 4 2", "5 15\n5 5 5 5 5", "5 16\n5 5 5 5 5", "5 26\n5 5 5 5 5"], "sample_outputs": ["4", "2", "1", "2", "-1"], "notes": "NoteIn the first example Polycarp can drink fourth cup during first day (and write $$$1$$$ page), first and second cups during second day (and write $$$2 + (3 - 1) = 4$$$ pages), fifth cup during the third day (and write $$$2$$$ pages) and third cup during the fourth day (and write $$$1$$$ page) so the answer is $$$4$$$. It is obvious that there is no way to write the coursework in three or less days.In the second example Polycarp can drink third, fourth and second cups during first day (and write $$$4 + (2 - 1) + (3 - 2) = 6$$$ pages) and sixth cup during second day (and write $$$4$$$ pages) so the answer is $$$2$$$. It is obvious that Polycarp cannot write the whole coursework in one day in this test.In the third example Polycarp can drink all cups of coffee during first day and write $$$5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15$$$ pages of coursework.In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write $$$5 + (5 - 1) + (5 - 2) + (5 - 3) = 14$$$ pages of coursework and during second day he will write $$$5$$$ pages of coursework. This is enough to complete it.In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1."}, "positive_code": [{"source_code": "n, m = gets.split.map(&:to_i)\na = gets.split.map(&:to_i).sort.reverse\n\nputs (1..n).bsearch { |day|\n\t\tsum = a[0, day].sum\n\t\tminus, cnt = 1, 0\n\t\t(day...n).each do |i|\n\t\t\tsum += [0, a[i] - minus].max\n\t\t\tcnt += 1\n\t\t\tif cnt == day\n\t\t\t\tminus += 1\n\t\t\t\tcnt = 0\n\t\t\tend\n\t\tend\n\t\tsum >= m\n } || -1\n"}], "negative_code": [], "src_uid": "1b79ff21b5c1df4c54236071a585a52e"} {"nl": {"description": "You've got an array a, consisting of n integers: a1, a2, ..., an. Your task is to find a minimal by inclusion segment [l, r] (1 ≤ l ≤ r ≤ n) such, that among numbers al,  al + 1,  ...,  ar there are exactly k distinct numbers.Segment [l, r] (1 ≤ l ≤ r ≤ n; l, r are integers) of length m = r - l + 1, satisfying the given property, is called minimal by inclusion, if there is no segment [x, y] satisfying the property and less then m in length, such that 1 ≤ l ≤ x ≤ y ≤ r ≤ n. Note that the segment [l, r] doesn't have to be minimal in length among all segments, satisfying the given property.", "input_spec": "The first line contains two space-separated integers: n and k (1 ≤ n, k ≤ 105). The second line contains n space-separated integers a1, a2, ..., an — elements of the array a (1 ≤ ai ≤ 105).", "output_spec": "Print a space-separated pair of integers l and r (1 ≤ l ≤ r ≤ n) such, that the segment [l, r] is the answer to the problem. If the sought segment does not exist, print \"-1 -1\" without the quotes. If there are multiple correct answers, print any of them.", "sample_inputs": ["4 2\n1 2 2 3", "8 3\n1 1 2 2 3 3 4 5", "7 4\n4 7 7 4 7 4 7"], "sample_outputs": ["1 2", "2 5", "-1 -1"], "notes": "NoteIn the first sample among numbers a1 and a2 there are exactly two distinct numbers.In the second sample segment [2, 5] is a minimal by inclusion segment with three distinct numbers, but it is not minimal in length among such segments.In the third sample there is no segment with four distinct numbers."}, "positive_code": [{"source_code": "def execute(n, k, arr)\n return [1, 1] if k == 1\n return [-1, -1] if k > n\n\n @lelf, @right = 0\n @arr_count = [0] * (10**5 + 1)\n @count = 0\n\n for j in 0..n-1\n @arr_count[arr[j]] += 1\n\n if @arr_count[arr[j]] == 1\n @count += 1\n @right = j\n end\n # puts \"puts arr[#{j}]: #{arr[j]}, number count: #{@arr_count[arr[j]]}, @count: #{@count}, }\"\n\n if @count == k\n break\n end\n\n end\n\n return [-1, -1] if @count < k\n\n while @count == k\n @arr_count[arr[@lelf]] -= 1\n if @arr_count[arr[@lelf]] == 0\n break\n end\n @lelf += 1\n end\n\n return [@lelf + 1, @right + 1]\nend\n\nn, k = gets.chomp.split(' ').map(&:to_i)\narr = gets.chomp.split(' ').map(&:to_i)\nresult = execute(n, k, arr)\nputs result.join(' ')\n"}, {"source_code": "n, k, *a = gets(p).split.map(&:to_i)\nl = r = 0\nt = {}\nwhile k > 0 && r < n\n\tunless t[a[r]]\n\t\tk -= 1\n\t\tt[a[r]] = 1\n\telse\n\t\tt[a[r]] += 1\n\tend\n\tr += 1\nend\nwhile t[a[l]] > 1\n\tt[a[l]] -= 1\n\tl += 1\nend\nputs \"%d %d\"%(k > 0 ? [-1, -1] : [l+1, r])\n\n"}, {"source_code": "n, k, *a = gets(p).split.map(&:to_i)\nl = r = 0\nt = {}\nwhile k > 0 && r < n\n\tunless t[a[r]]\n\t\tk -= 1\n\t\tt[a[r]] = 1\n\telse\n\t\tt[a[r]] += 1\n\tend\n\tr += 1\nend\nwhile t[a[l]] > 1\n\tt[a[l]] -= 1\n\tl += 1\nend\nputs \"%d %d\"%(k > 0 ? [-1, -1] : [l+1, r])\n\n"}, {"source_code": "#!/usr/bin/ruby\n\nn, k = STDIN.readline.split.collect {|s| s.to_i }\narr = STDIN.readline.split.collect {|s| s.to_i }\nif arr.uniq.length < k\n\tputs '-1 -1'\n\texit\nend\nif k == 1\n\tputs '1 1'\n\texit\nend\ncount = 1\nunique_hash = { arr[0] => true }\nright = 0\nwhile count < k\n\tright += 1\n\tx = arr[right]\n\tif not unique_hash.include? x\n\t\tunique_hash[x] = true\n\t\tcount += 1\n\tend\nend\ncount = 1\nleft = right\nunique_hash = { arr[right] => true }\nwhile count < k\n\tleft -= 1\n\tx = arr[left]\n\tif not unique_hash.include? x\n\t\tunique_hash[x] = true\n\t\tcount += 1\n\tend\nend\nputs '%d %d' % [left+1, right+1]\n\n"}, {"source_code": "n, k, *a = gets(p).split.map(&:to_i)\nl = r = 0\nt = {}\nwhile k > 0 && r < n\n\tunless t[a[r]]\n\t\tk -= 1\n\t\tt[a[r]] = 1\n\telse\n\t\tt[a[r]] += 1\n\tend\n\tr += 1\nend\nwhile t[a[l]] > 1\n\tt[a[l]] -= 1\n\tl += 1\nend\nputs \"%d %d\"%(k > 0 ? [-1, -1] : [l+1, r])\n"}, {"source_code": "n, k, *a = gets(p).split.map(&:to_i)\nl = r = 0\nt = {}\nwhile k > 0 && r < n\n\tunless t[a[r]]\n\t\tk -= 1\n\t\tt[a[r]] = 1\n\telse\n\t\tt[a[r]] += 1\n\tend\n\tr += 1\nend\nwhile t[a[l]] > 1\n\tt[a[l]] -= 1\n\tl += 1\nend\nputs \"%d %d\"%(k > 0 ? [-1, -1] : [l+1, r])\n\n"}, {"source_code": "n, k, *a = gets(p).split.map(&:to_i)\nl = r = 0\nt = {}\nwhile k > 0 && r < n\n\tunless t[a[r]]\n\t\tk -= 1\n\t\tt[a[r]] = 1\n\telse\n\t\tt[a[r]] += 1\n\tend\n\tr += 1\nend\nwhile t[a[l]] > 1\n\tt[a[l]] -= 1\n\tl += 1\nend\nputs \"%d %d\"%(k > 0 ? [-1, -1] : [l+1, r])\n\n"}, {"source_code": "n, k = gets.rstrip.split.map(&:to_i)\narr = gets.rstrip.split.map(&:to_i)\n\ntmp = []\nl = r = 0\n\ncount = 0\n\nwhile r < n do\n tmp[arr[r]] ? tmp[arr[r]] += 1 : tmp[arr[r]] = 1\n count += 1 if tmp[arr[r]] == 1\n if count == k\n while l <= r do\n tmp[arr[l]] -= 1\n if tmp[arr[l]] == 0\n puts \"#{l + 1} #{r + 1}\"\n exit\n end\n l += 1\n end\n end\n r += 1\nend\n\nputs \"-1 -1\"\n"}, {"source_code": "n, k, *a = gets(p).split.map(&:to_i)\nl = r = 0\nt = {}\nwhile k > 0 && r < n\n\tunless t[a[r]]\n\t\tk -= 1\n\t\tt[a[r]] = 1\n\telse\n\t\tt[a[r]] += 1\n\tend\n\tr += 1\nend\nwhile t[a[l]] > 1\n\tt[a[l]] -= 1\n\tl += 1\nend\nputs \"%d %d\"%(k > 0 ? [-1, -1] : [l+1, r])\n\n"}, {"source_code": "require 'set'\nbuf = gets.split(' ')\nn = buf[0].to_i\nk = buf[1].to_i\nst = Set.new\nbuf = gets.split(' ')\n0.upto(n - 1) { |ii|\n i = buf[ii]\n st.add(i)\n if st.size == k \n st.clear\n ii.downto(0) { |jj|\n j = buf[jj]\n st.add(j)\n if st.size == k \n print jj + 1, \" \", ii + 1\n exit 0\n end\n }\n end\n}\nprint \"-1 -1\"\n"}, {"source_code": "n, k = gets.chomp.split.map(&:to_i)\nlist = gets.chomp.split.map(&:to_i)\n\ndef aaa(list, k)\n count = Hash.new { |hash, key| hash[key] = 0 }\n\n for r in 0..(list.length - 1)\n count[list[r]] += 1\n\n if count[list[r]] == 1\n k -= 1\n end\n\n if k == 0\n for l in 0..r\n count[list[l]] = count[list[l]] - 1\n\n if count[list[l]] == 0\n return [l + 1, r + 1]\n end\n end\n end\n end\n\n return [-1, -1]\nend\n\nresult = aaa(list, k)\n\nprint result[0], \" \", result[1]\n"}, {"source_code": "n, k, *a = gets(p).split.map(&:to_i)\nl = r = 0\nt = {}\nwhile k > 0 && r < n\n\tunless t[a[r]]\n\t\tk -= 1\n\t\tt[a[r]] = 1\n\telse\n\t\tt[a[r]] += 1\n\tend\n\tr += 1\nend\nwhile t[a[l]] > 1\n\tt[a[l]] -= 1\n\tl += 1\nend\nputs \"%d %d\"%(k > 0 ? [-1, -1] : [l+1, r])\n\n"}, {"source_code": "n, k, *a = gets(p).split.map(&:to_i)\nl = r = 0\nt = {}\nwhile k > 0 && r < n\n\tunless t[a[r]]\n\t\tk -= 1\n\t\tt[a[r]] = 1\n\telse\n\t\tt[a[r]] += 1\n\tend\n\tr += 1\nend\nwhile t[a[l]] > 1\n\tt[a[l]] -= 1\n\tl += 1\nend\nputs \"%d %d\"%(k > 0 ? [-1, -1] : [l+1, r])\n"}, {"source_code": "require 'set'\n\n$incr = Proc.new {|st,x| st[x].nil? ? (st[x] = 1) : (st[x] += 1) }\n$decr = Proc.new {|st,x| st[x].nil? ? 0 : (st[x] == 1 ? (st.delete x) : (st[x] -= 1) ) }\n$dis = Proc.new {|ls| st = {}; ls.each {|x| $incr.(st,x) } ; st }\n\n\ndef good(ls,m,k)\n\n l = ls.length - m - 1\n st = $dis.(ls.take(m))\n cnt = st.size\n if cnt >= k\n return [true,0,ls]\n else\n (0..l).each do |i|\n $incr.(st,ls[m+i])\n $decr.(st,ls[i])\n if st.size >= k\n ar = Array.new(m)\n i += 1\n m.times do |j|\n ar[j] = ls[j+i]\n end\n return [true,i,ar]\n end\n end\n end\n return [false]\nend\n\ndef doIt(n,k,ls)\n off = 0\n h = n\n l = [k - 1,0].max\n a = [false]\n while (h - l > 1)\n m = (h+l) / 2\n a = good(ls,m,k)\n if a.first\n h = m\n ls = a[2]\n off += a[1]\n else\n l = m\n end\n end\n a = good(ls,h,k)\n puts \"#{a[1]+1+off} #{a[1]+h+off}\" \n\nend\n\nxs = gets.split.map(&:to_i)\nn, k = xs[0], xs[1]\nls = gets.split.map(&:to_i)\n\nst = Set.new\n\nls.each {|x| st.add x}\n\nif st.size < k then\n puts \"-1 -1\" #cant be done\nelse\n doIt(n,k,ls)\nend"}, {"source_code": "require 'set'\n\ndef good(ls,m,k)\n\n incr = Proc.new {|st,x| st[x].nil? ? (st[x] = 1) : (st[x] += 1) }\n decr = Proc.new {|st,x| st[x].nil? ? 0 : (st[x] == 1 ? (st.delete x) : (st[x] -= 1) ) }\n dis = Proc.new {|ls| st = {}; ls.each {|x| incr.(st,x) } ; st }\n\n l = ls.length - m - 1\n st = dis.(ls.take(m))\n cnt = st.size\n if cnt >= k\n return [true,0,ls]\n else\n (0..l).each do |i|\n incr.(st,ls[m+i])\n decr.(st,ls[i])\n if st.size >= k\n ar = Array.new(m)\n i += 1\n m.times do |j|\n ar[j] = ls[j+i]\n end\n return [true,i,ar]\n end\n end\n end\n return [false]\nend\n\ndef doIt(n,k,ls)\n off = 0\n h = n\n l = [k - 1,0].max\n a = [false]\n while (h - l > 1)\n m = (h+l) / 2\n a = good(ls,m,k)\n if a.first\n h = m\n ls = a[2]\n off += a[1]\n else\n l = m\n end\n end\n a = good(ls,h,k)\n puts \"#{a[1]+1+off} #{a[1]+h+off}\" \n\nend\n\nxs = gets.split.map(&:to_i)\nn, k = xs[0], xs[1]\nls = gets.split.map(&:to_i)\n\nst = Set.new\n\nls.each {|x| st.add x}\n\nif st.size < k then\n puts \"-1 -1\" #cant be done\nelse\n doIt(n,k,ls)\nend"}, {"source_code": "n, k, *a = gets(p).split.map(&:to_i)\nl = r = 0\nt = {}\nwhile k > 0 && r < n\n\tunless t[a[r]]\n\t\tk -= 1\n\t\tt[a[r]] = 1\n\telse\n\t\tt[a[r]] += 1\n\tend\n\tr += 1\nend\nwhile t[a[l]] > 1\n\tt[a[l]] -= 1\n\tl += 1\nend\nputs \"%d %d\"%(k > 0 ? [-1, -1] : [l+1, r])\n\n"}, {"source_code": "n,k,*a=gets(p).split.map(&:to_i)\nif n==1\n\tputs \"%d %d\"%(k>1 ? [-1,-1] : [1,1]) \t\n\texit\nend\nl=0\nl+=1 while (l0)&&(r1\n\th[a[l]]-=1\n\tl+=1\nend\nputs \"%d %d\"%(k>0 ? [-1,-1] : [l+1,r+1]) "}, {"source_code": "n, k, *a = gets(p).split.map(&:to_i)\nl = r = 0\nt = {}\nwhile k > 0 && r < n\n\tunless t[a[r]]\n\t\tk -= 1\n\t\tt[a[r]] = 1\n\telse\n\t\tt[a[r]] += 1\n\tend\n\tr += 1\nend\nwhile t[a[l]] > 1\n\tt[a[l]] -= 1\n\tl += 1\nend\nputs \"%d %d\"%(k > 0 ? [-1, -1] : [l+1, r])\n\n"}], "negative_code": [{"source_code": "def execute(n, k, arr)\n @count = 0\n @arr_count = [0] * n\n @arr_count[0] = 1\n @lelf, @right = 0\n\n for j in 1..n-1\n @i = 0\n\n # skip if arr[j] count before\n # next if @arr_count[arr.index(@arr_count[j])] == 0\n\n while @i < j do\n if arr[@i] == arr[j]\n @arr_count[j] = @arr_count[j - 1]\n break\n end\n @i += 1\n end\n\n # item no exist [i..j-2]\n if @i == j\n @arr_count[j] = @arr_count[j - 1] + 1\n @right = j\n @count = @arr_count[j]\n end\n\n # puts \"j: #{j}, @count: #{@count}, #{@arr_count.join(' ')}\"\n\n break if @arr_count[j] == k\n end\n\n # check @count, shifl @lelf + 1\n\n if @count < k\n return [-1, -1]\n else\n while @arr_count[@lelf + 1] == 1\n @lelf += 1\n end\n return [@lelf + 1, @right + 1] if @right > 0\n end\n\nend\n\n# puts \"test case 1\"\n# puts execute(4, 2, %w(1 2 2 3).map(&:to_i)).inspect\n# #arr_count 1 2 0 0\n\n# puts \"test case 2\"\n# puts execute(8, 3, %w(1 1 2 2 3 3 4 5).map(&:to_i)).inspect\n# #arr_count 1 1 2 2 3 0 0 0\n\n# puts \"test case 3\"\n# puts execute(7, 4, %w(4 7 7 4 7 4 7).map(&:to_i)).inspect\n# #arr_count 1 2 2 2 2 2 2\n\nn, k = gets.chomp.split(' ').map(&:to_i)\narr = gets.chomp.split(' ').map(&:to_i)\nresult = execute(n, k, arr)\nputs result.join(' ')\n"}, {"source_code": "#!/usr/bin/ruby\n\nn, k = STDIN.readline.split.collect {|s| s.to_i }\narr = STDIN.readline.split.collect {|s| s.to_i }\nif arr.uniq.length < k\n\tputs '-1 -1'\n\texit\nend\nif k == 1\n\tputs '1 1'\n\texit\nend\nstart = 0\nstart += 1 while arr[start] == arr[start+1]\ncount = 1\nunique_hash = { arr[start] => true }\npos = start\nwhile count < k\n\tpos += 1\n\tx = arr[pos]\n\tif not unique_hash.include? x\n\t\tunique_hash[x] = true\n\t\tcount += 1\n\tend\nend\nputs '%d %d' % [start+1, pos+1]\n\n"}, {"source_code": "n, k = gets.rstrip.split.map(&:to_i)\narr = gets.rstrip.split.map(&:to_i)\n\ndistinct_arr = arr.uniq\ntmp = []\n\nif distinct_arr.size < k\n puts \"-1 -1\"\nelse\n k.times.each do |i|\n tmp.push(distinct_arr[i])\n end\n\n res = []\n max = tmp.max\n tmp.each do |v|\n if v == max\n res.push(arr.each_index.select { |i| arr[i] == v }.min)\n else\n res.push(arr.each_index.select { |i| arr[i] == v }.max)\n end\n end\n\n puts \"#{res.first + 1} #{res.last + 1}\"\nend\n"}, {"source_code": "n, k = gets.rstrip.split.map(&:to_i)\narr = gets.rstrip.split.map(&:to_i)\n\ndistinct_arr = arr.uniq\ntmp = []\n\nif distinct_arr.size < k\n puts \"-1 -1\"\nelse\n k.times.each do |i|\n tmp.push(distinct_arr[i])\n end\n\n last = arr.each_index.select { |i| arr[i] == tmp[-1] }.min\n first = arr.each_index.select { |i| arr[i] == tmp[0] && i < last }.max\n\n puts \"#{first.to_i + 1} #{last.to_i + 1}\"\nend\n"}, {"source_code": "n, k = gets.rstrip.split.map(&:to_i)\narr = gets.rstrip.split.map(&:to_i)\n\ndistinct_arr = arr.uniq\ntmp = []\n\nif distinct_arr.size < k\n puts \"-1 -1\"\nelse\n k.times.each do |i|\n tmp.push(distinct_arr[i])\n end\n\n res = []\n tmp.each do |v|\n if v == tmp[-1]\n res.push(arr.each_index.select { |i| arr[i] == v }.min)\n else\n res.push(arr.each_index.select { |i| arr[i] == v }.max)\n end\n end\n\n puts \"#{res.first + 1} #{res.last + 1}\"\nend\n"}, {"source_code": "require 'set'\nbuf = gets.split(' ')\nn = buf[0].to_i\nk = buf[1].to_i\nst = Set.new\nbuf = gets.split(' ')\n0.upto(n - 1) { |ii|\n i = buf[ii]\n st.add(i)\n if st.size == k \n print 1, \" \", ii + 1\n exit 0\n end\n}\nprint \"-1 -1\"\n"}, {"source_code": "n, k = gets.chomp.split.map(&:to_i)\nlist = gets.chomp.split.map(&:to_i)\n\ndef aaa(list, k)\n hash = Hash.new { |hash, key| hash[key] = 0 }\n for i in 0..(list.length - 1)\n hash[list[i]] += 1\n end\n\n if hash.keys.count < k\n return [-1, -1]\n else\n b = []\n for i in 0..(list.length - 1)\n b << [i, list[i]]\n\n if list[i] != list[i + 1]\n k = k - 1\n end\n\n if k == 0\n break\n end\n end\n b\n c = []\n d = []\n for i in 0..(b.length - 1)\n if b[i][1] == b[0][1]\n c << i\n end\n if b[i][1] == b[-1][1]\n d << i\n end\n end\n return [c.max + 1, d.min + 1]\n end\nend\n\nresult = aaa(list, k)\nresult.each do |i|\n print i\n print \" \" if i != result[-1]\nend\n"}, {"source_code": "n, k = gets.chomp.split.map(&:to_i)\nlist = gets.chomp.split.map(&:to_i)\n\ndef aaa(list, k)\n hash = Hash.new { |hash, key| hash[key] = 0 }\n for i in 0..(list.length - 1)\n hash[list[i]] += 1\n end\n\n if hash.keys.count < k\n \"-1 -1\"\n else\n b = []\n for i in 0..(list.length - 1)\n b << [i, list[i]]\n\n if list[i] != list[i + 1]\n k = k - 1\n end\n\n if k == 0\n break\n end\n end\n b\n c = []\n d = []\n for i in 0..(b.length - 1)\n if b[i][1] == b[0][1]\n c << i\n end\n if b[i][1] == b[-1][1]\n d << i\n end\n end\n \"#{c.max + 1} #{d.min + 1}\"\n end\nend\n\np aaa(list, k)\n"}, {"source_code": "n, k = gets.chomp.split.map(&:to_i)\nlist = gets.chomp.split.map(&:to_i)\n\ndef aaa(list, k)\n hash = Hash.new { |hash, key| hash[key] = 0 }\n for i in 0..(list.length - 1)\n hash[list[i]] += 1\n end\n\n if hash.keys.count < k\n return [-1, -1]\n else\n b = []\n for i in 0..(list.length - 1)\n b << [i, list[i]]\n\n if list[i] != list[i + 1]\n k = k - 1\n end\n\n if k == 0\n break\n end\n end\n b\n c = []\n d = []\n for i in 0..(b.length - 1)\n if b[i][1] == b[0][1]\n c << i\n end\n if b[i][1] == b[-1][1]\n d << i\n end\n end\n return [c.max + 1, d.min + 1]\n end\nend\n\nresult = aaa(list, k)\nresult.each.with_index do |element, index|\n print element\n print \" \" if index != result.length - 1\nend\n"}, {"source_code": "n, k = gets.chomp.split.map(&:to_i)\nlist = gets.chomp.split.map(&:to_i)\n\ndef aaa(list, k)\n hash = Hash.new { |hash, key| hash[key] = 0 }\n for i in 0..(list.length - 1)\n hash[list[i]] += 1\n end\n\n if hash.keys.count < k\n return [-1, -1]\n else\n b = []\n for i in 0..(list.length - 1)\n b << [i, list[i]]\n\n if list[i] != list[i + 1]\n k = k - 1\n end\n\n if k == 0\n break\n end\n end\n b\n c = []\n d = []\n for i in 0..(b.length - 1)\n if b[i][1] == b[0][1]\n c << i\n end\n if b[i][1] == b[-1][1]\n d << i\n end\n end\n return [c.max, d.min]\n end\nend\n\nresult = aaa(list, k)\nresult.each do |i|\n print i\n print \" \" if i != result[-1]\nend\n"}, {"source_code": "require 'set'\n\ndef good(ls,m,k)\n\n incr = Proc.new {|st,x| st[x].nil? ? (st[x] = 1) : (st[x] += 1) }\n decr = Proc.new {|st,x| st[x].nil? ? 0 : (st[x] == 1 ? (st.delete x) : (st[x] -= 1) ) }\n dis = Proc.new {|ls| st = {}; ls.each {|x| incr.(st,x) } ; st }\n\n l = ls.length - m - 1\n tk = ls.take(m)\n st = dis.(tk)\n cnt = st.size\n if cnt >= k\n return [true,0,tk]\n else\n (0..l).each do |i|\n tk.delete_at 0; tk.push(ls[m+i])\n incr.(st,ls[m+i])\n decr.(st,ls[i])\n if st.size >= k\n return [true,i+1,tk]\n end\n end\n end\n return [false]\nend\n\ndef doIt(n,k,ls)\n\n h = n\n l = [k - 1,0].max\n a = [false]\n while (h - l > 1)\n m = (h+l) / 2\n a = good(ls,m,k)\n if a.first\n h = m\n ls = a[2]\n else\n l = m\n end\n end\n a = good(ls,h,k)\n puts \"#{a[1]+1} #{a[1]+h}\" \n\nend\n\nxs = gets.split.map(&:to_i)\nn, k = xs[0], xs[1]\nls = gets.split.map(&:to_i)\n\nst = Set.new\n\nls.each {|x| st.add x}\n\nif st.size < k then\n puts \"-1 -1\" #cant be done\nelse\n doIt(n,k,ls)\nend"}, {"source_code": "n,k,*a=gets(p).split.map(&:to_i)\nl=0\nl+=1 while (l0)&&(r0)&&(r1 ? [-1,-1] : [1,1]) \t\n\texit\nend\nl=0\nl+=1 while (l0)&&(r0 ? [-1,-1] : [l+1,r+1]) "}, {"source_code": "n,k,*a=gets(p).split.map(&:to_i)\nl=0\nl+=1 while (l0)&&(r0)||(n==1) ? [-1,-1] : [l+1,r+1]) "}, {"source_code": "n,k,*a=gets(p).split.map(&:to_i)\nl=0\nl+=1 while (l0)&&(r0)&&(n==1) ? [-1,-1] : [l+1,r+1]) "}, {"source_code": "n,k,*a=gets(p).split.map(&:to_i)\nl=0\nl+=1 while (l0)&&(r0)||((n==1)&&(k>1)) ? [-1,-1] : [l+1,r+1]) "}, {"source_code": "n,k,*a=gets(p).split.map(&:to_i)\nif n==1\n\tputs \"%d %d\"%(k>1 ? [-1,-1] : [1,1]) \t\n\texit\nend\nl=0\nl+=1 while (l0)&&(r0 ? [-1,-1] : [l+1,r+1]) "}], "src_uid": "4b423274448ff3f0dee7644203706805"} {"nl": {"description": "You are given an integer $$$n$$$. In one move, you can either multiply $$$n$$$ by two or divide $$$n$$$ by $$$6$$$ (if it is divisible by $$$6$$$ without the remainder).Your task is to find the minimum number of moves needed to obtain $$$1$$$ from $$$n$$$ or determine if it's impossible to do that.You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 2 \\cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \\le n \\le 10^9$$$).", "output_spec": "For each test case, print the answer — the minimum number of moves needed to obtain $$$1$$$ from $$$n$$$ if it's possible to do that or -1 if it's impossible to obtain $$$1$$$ from $$$n$$$.", "sample_inputs": ["7\n1\n2\n3\n12\n12345\n15116544\n387420489"], "sample_outputs": ["0\n-1\n2\n-1\n-1\n12\n36"], "notes": "NoteConsider the sixth test case of the example. The answer can be obtained by the following sequence of moves from the given integer $$$15116544$$$: Divide by $$$6$$$ and get $$$2519424$$$; divide by $$$6$$$ and get $$$419904$$$; divide by $$$6$$$ and get $$$69984$$$; divide by $$$6$$$ and get $$$11664$$$; multiply by $$$2$$$ and get $$$23328$$$; divide by $$$6$$$ and get $$$3888$$$; divide by $$$6$$$ and get $$$648$$$; divide by $$$6$$$ and get $$$108$$$; multiply by $$$2$$$ and get $$$216$$$; divide by $$$6$$$ and get $$$36$$$; divide by $$$6$$$ and get $$$6$$$; divide by $$$6$$$ and get $$$1$$$. "}, "positive_code": [{"source_code": "gets.to_i.times do\n\tn = gets.to_i\n\tcnt2, cnt3 = 0, 0\n\twhile n % 2 == 0\n\t\tn /= 2\n\t\tcnt2 += 1\n\tend\n\twhile n % 3 == 0\n\t\tn /= 3\n\t\tcnt3 += 1\n\tend\n\tif n == 1 && cnt2 <= cnt3\n\t\tputs (cnt3 - cnt2) + cnt3\n\telse\n\t\tputs -1\n\tend\nend\n"}, {"source_code": "t = gets.to_i\nt.times do\n\tn = gets.to_i\n\tdiv2 = 0\n\tdiv3 = 0\n\twhile (n & 1) == 0\n\t\tdiv2 += 1\n\t\tn >>= 1\n\tend\n\twhile (n % 3) == 0\n\t\tdiv3 += 1\n\t\tn /= 3\n\tend\n\tif n != 1 or div2 > div3\n\t\tputs \"-1\"\n\telse\n\t\tp div3*2 - div2\n\tend\nend\n"}, {"source_code": "def run\n n = gets.chomp.to_i\n\n if n == 1\n puts \"0\"\n return\n end\n\n nc = n\n twos = 0\n while true\n if nc % 2 == 0\n nc /= 2\n twos += 1\n else\n break\n end\n end\n\n threes = 0\n while true\n if nc % 3 == 0\n nc /= 3\n threes += 1\n else\n break\n end\n end\n\n if nc != 1\n puts \"-1\"\n return\n end\n\n if threes >= twos\n puts threes + threes - twos\n else\n puts \"-1\"\n end\nend\n\n\ndef main\n t = gets.chomp.to_i\n\n t.times do\n run\n end\nend\n\nmain\n"}, {"source_code": "gets.to_i.times do\n n = gets.to_i\n ans = 0\n while n % 6 == 0 or (n != 1 and n.odd?) do\n ans += 1\n n = if n % 6 == 0 then n / 6 else n * 2 end\n end\n puts -1 if n != 1\n puts ans if n == 1\nend\n\n"}, {"source_code": "gets.to_i.times do\n n = gets.to_i\n\n d3 = 0\n while n.modulo(3).zero?\n n /= 3\n d3 += 1\n end\n\n d2 = 0\n while n.modulo(2).zero?\n n /= 2\n d2 += 1\n end\n\n if n > 1 || d2 > d3\n puts -1\n next\n end\n\n puts (d3 - d2) + d3\nend\n"}], "negative_code": [], "src_uid": "3ae468c425c7b156983414372fd35ab8"} {"nl": {"description": "There is a frog staying to the left of the string $$$s = s_1 s_2 \\ldots s_n$$$ consisting of $$$n$$$ characters (to be more precise, the frog initially stays at the cell $$$0$$$). Each character of $$$s$$$ is either 'L' or 'R'. It means that if the frog is staying at the $$$i$$$-th cell and the $$$i$$$-th character is 'L', the frog can jump only to the left. If the frog is staying at the $$$i$$$-th cell and the $$$i$$$-th character is 'R', the frog can jump only to the right. The frog can jump only to the right from the cell $$$0$$$.Note that the frog can jump into the same cell twice and can perform as many jumps as it needs.The frog wants to reach the $$$n+1$$$-th cell. The frog chooses some positive integer value $$$d$$$ before the first jump (and cannot change it later) and jumps by no more than $$$d$$$ cells at once. I.e. if the $$$i$$$-th character is 'L' then the frog can jump to any cell in a range $$$[max(0, i - d); i - 1]$$$, and if the $$$i$$$-th character is 'R' then the frog can jump to any cell in a range $$$[i + 1; min(n + 1; i + d)]$$$.The frog doesn't want to jump far, so your task is to find the minimum possible value of $$$d$$$ such that the frog can reach the cell $$$n+1$$$ from the cell $$$0$$$ if it can jump by no more than $$$d$$$ cells at once. It is guaranteed that it is always possible to reach $$$n+1$$$ from $$$0$$$.You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. The next $$$t$$$ lines describe test cases. The $$$i$$$-th test case is described as a string $$$s$$$ consisting of at least $$$1$$$ and at most $$$2 \\cdot 10^5$$$ characters 'L' and 'R'. It is guaranteed that the sum of lengths of strings over all test cases does not exceed $$$2 \\cdot 10^5$$$ ($$$\\sum |s| \\le 2 \\cdot 10^5$$$).", "output_spec": "For each test case, print the answer — the minimum possible value of $$$d$$$ such that the frog can reach the cell $$$n+1$$$ from the cell $$$0$$$ if it jumps by no more than $$$d$$$ at once.", "sample_inputs": ["6\nLRLRRLL\nL\nLLR\nRRRR\nLLLLLL\nR"], "sample_outputs": ["3\n2\n3\n1\n7\n1"], "notes": "NoteThe picture describing the first test case of the example and one of the possible answers:In the second test case of the example, the frog can only jump directly from $$$0$$$ to $$$n+1$$$.In the third test case of the example, the frog can choose $$$d=3$$$, jump to the cell $$$3$$$ from the cell $$$0$$$ and then to the cell $$$4$$$ from the cell $$$3$$$.In the fourth test case of the example, the frog can choose $$$d=1$$$ and jump $$$5$$$ times to the right.In the fifth test case of the example, the frog can only jump directly from $$$0$$$ to $$$n+1$$$.In the sixth test case of the example, the frog can choose $$$d=1$$$ and jump $$$2$$$ times to the right."}, "positive_code": [{"source_code": "# coding: utf-8\n\nt = gets.to_i\nt.times {\n # L R\n # | 0 | | 1 | | 2 | ............ | n | | n+1 |\n # frog\n # ------------>\n # jump by [1, d] size, you can choose 1 for the smallest one\n # minimize size 'd'\n # range of d, min = 1 'RRR...RRRR', max = n+1 'LLL...LLL'\n # every sequence has a solution.\n\n # find the longest sequence of 'L' and + 1 to reach 'R' again\n max_l, l = 0, 0\n gets.chomp.split(//).each { |c|\n if c == 'R'\n if max_l < l\n max_l = l\n end\n l = 0\n else\n l += 1\n end\n }\n\n if max_l < l\n max_l = l # clean up\n end\n\n puts max_l + 1\n}"}, {"source_code": "tc = gets.to_i \n\ntc.times do \n s1 = gets.chomp \n s = \"R\" + s1 + \"R\" \n maxi = -1 \n prev = 0 \n l = s.size \n (1...l).each do |i| \n if(s[i] == 'R') \n now = i - prev \n maxi = [maxi,now].max\n prev = i \n end\n end\n puts maxi\nend"}, {"source_code": "t = gets.to_i\nt.times do\n s = gets.chomp\n run_len = 0\n max_run_len = 0\n s.each_char do |c|\n case c\n when \"L\"\n run_len += 1\n when \"R\"\n max_run_len = run_len if max_run_len < run_len\n run_len = 0\n end\n end\n max_run_len = run_len if max_run_len < run_len\n puts max_run_len + 1\nend\n"}, {"source_code": "t = gets.chomp.to_i\n(0...t).each do |_|\n l = \"*#{gets.chomp}*\"\n # puts l\n d, c = 0, 0\n (0...l.size).each do |i| \n (c+=1; next) if i.zero?\n if l[i] == 'R'\n d = [d, c].max\n c = 0\n elsif l[i] == '*'\n d = [d, c].max\n end\n c+=1\n end\n puts d\nend\n"}], "negative_code": [], "src_uid": "6451507b21854d5b81aeaf0491ddf584"} {"nl": {"description": "The \"BerCorp\" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).", "input_spec": "The first line contains two integers n and m (2 ≤ n, m ≤ 100) — the number of employees and the number of languages. Then n lines follow — each employee's language list. At the beginning of the i-th line is integer ki (0 ≤ ki ≤ m) — the number of languages the i-th employee knows. Next, the i-th line contains ki integers — aij (1 ≤ aij ≤ m) — the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.", "output_spec": "Print a single integer — the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).", "sample_inputs": ["5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5", "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1", "2 2\n1 2\n0"], "sample_outputs": ["0", "2", "1"], "notes": "NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2."}, "positive_code": [{"source_code": "n, m = gets.split.map(&:to_i)\n$list = Array.new(n)\nclass Emp\n attr_reader :lang, :group, :edge\n def initialize(lang)\n @lang = lang\n @group = nil\n\t@edge = Array.new\n end #def init\n \n def edgeplus(n)\n @edge << n\n end #def edge\n \n def search(g)\n @group = g\n for i in @edge\n\t if $list[i].group == nil\n\t $list[i].search(g)\n\t end\n\tend\n end #def search\nend #class\n\nallnone = true\nfor i in 0...n\n lang = gets.split.map(&:to_i).drop(1)\n $list[i] = Emp.new(lang)\n if lang.count > 0 then allnone = false end\n for j in 0...i\n if ($list[i].lang & $list[j].lang).count > 0\n\t $list[j].edgeplus(i)\n\t $list[i].edgeplus(j)\n\tend\n end\nend\n\ng = 0\nfor i in 0...n\n if $list[i].group == nil\n $list[i].search(g)\n\tg += 1\n end\nend\n\nif !allnone then g -= 1 end\nputs g"}, {"source_code": "input = gets.chomp.split\nn = Integer(input[0])\nm = Integer(input[1])\nlang = []\nfor i in 0...n\n lang[i] = []\n lang[i] = gets.chomp.split.map{|j| Integer(j)}\nend\npointer = 0\nfor i in 0...n\n if lang[i] != [0]\n pointer = 1\n break\n end\nend\nfor i in 0...n\n lang[i].shift\nend\nboolean = []\nmatch = []\nfor i in 0...n\n boolean[i] = []\n for j in 0...n\n match = (lang[i] & lang[j]).length\n if match == 0\n boolean[i].push(false)\n else\n boolean[i].push(true)\n end\n end\nend\n\ndef fill_unit point, unit_id, unit, n, boolean\n if unit[point]\n return\n end\n\n unit[point] = unit_id\n for i in 0...n\n if boolean[point][i]\n fill_unit(i, unit_id, unit, n, boolean)\n end\n end\nend\n\nunit = Array.new(n)\nfor i in 0...n\n fill_unit(i, i, unit, n, boolean)\nend\n\nunit.uniq!\nif pointer == 0\n puts lang.length\nelse\n puts unit.length - 1\nend"}, {"source_code": "input = gets.chomp.split\nn = Integer(input[0])\nm = Integer(input[1])\nlang = []\nfor i in 0...n\n lang[i] = []\n lang[i] = gets.chomp.split.map{|j| Integer(j)}\nend\npointer = 0\nfor i in 0...n\n if lang[i] != [0]\n pointer = 1\n break\n end\nend\nunit = []\nfor i in 0...n\n lang[i].shift\n unit.push(i)\nend\nboolean = []\nmatch = []\nfor i in 0...n\n boolean[i] = []\n for j in 0...n\n match = (lang[i] & lang[j]).length\n if match == 0\n boolean[i].push(false)\n else\n boolean[i].push(true)\n end\n end\nend\n\nfor i in 0...n\n for j in 0..i\n if boolean[i][j]\n m = unit[j]\n for k in 0...n\n if unit[k] == m\n unit[k] = i\n end\n end\n end\n end\nend\nunit.uniq!\nif pointer == 0\n puts lang.length\nelse\n puts unit.length - 1\nend"}, {"source_code": "#!/usr/bin/ruby1.9\n\nn, m = STDIN.readline.split.collect {|s| s.to_i }\nseen = (m+1).times.collect { false }\nconnected = (m+1).times.collect { {} }\n\nmute_count = 0\n\nn.times do\n\tas = STDIN.readline.split.collect {|s| s.to_i }\n\tcount = as.shift\n\tif count == 0\n\t\tmute_count += 1\n\t\tnext\n\tend\n\tas.length.times do |i|\n\t\tu = as[i]\n\t\tseen[u] = true\n\t\t(i..as.length-1).each do |j|\n\t\t\tv = as[j]\n\t\t\tconnected[u][v] = true\n\t\t\tconnected[v][u] = true\n\t\tend\n\tend\nend\n\n\nmarked = (m+1).times.collect { false }\ncomponent_count = 0\n\ndef explore(u, marked, connected)\n\tmarked[u] = true\n\tconnected[u].keys.each do |v|\n\t\tnext if marked[v]\n\t\texplore(v, marked, connected)\n\tend\nend\n\n(1..m).each do |start|\n\tnext if marked[start] or not seen[start]\n\tcomponent_count += 1\n\texplore(start, marked, connected)\nend\n\nputs [component_count-1, 0].max + mute_count\n"}, {"source_code": "n,m=gets.split.map(&:to_i)\na=[]\nn.times do\n k=0\n gets.split.map(&:to_i)[1..-1].each do |v|\n k+=(1<<(v-1))\n end\n a<=0 then\n for j in i+1..n-1 do\n if a[j]>=0 and (a[i]&a[j])>0 then\n a[i]|=a[j]\n a[j]=-1\n changed=true\n end\n end\n end\n end\nend\n\nz=res=0\na.each do |v|\n res+=1 if v>=0\n z+=1 if v==0\nend\nputs z==n ? n : res-1\n"}, {"source_code": "n, m = gets.split(' ').collect!{|i| i.to_i}\n\nk = []\nn.times do |j|\n\tk[j] = gets.split(' ').collect!{|i| i.to_i}\nend\n\nneedtolearn = 0\n\n@mygroups = []\n\ndef checkinclude(int)\n\t@mygroups.each_index do |i|\n\t\treturn i if @mygroups[i].include?(int)\n\tend\n\treturn false\nend\n\nk.each do |line|\n\tif line[0] == 0\n\t\tneedtolearn += 1\n\telse\n\t\tfound = false\n\t\tline[1..-1].each do |lang|\n\t\t\tif grn = checkinclude(lang)\n\t\t\t\t@mygroups[grn] = @mygroups[grn] | line[1..-1]\n\t\t\t\tfound = true\n\t\t\t\tbreak\n\t\t\tend\n\t\tend\n\t\tif !found\n\t\t\t@mygroups << line[1..-1]\n\t\tend\n\tend\nend\n\nsize = @mygroups.size\nsize.times do |i|\n\tsize.times do |j|\n\t\tif i != j && !(@mygroups[i] & @mygroups[j]).empty?\n\t\t\t@mygroups[i] = @mygroups[i] | @mygroups[j]\n\t\t\t@mygroups[j] = []\n\t\tend\n\tend\nend\n\n@mygroups.delete_if {|x| x.empty? }\n\nlearning = (@mygroups.size - 1)\nlearning = 0 if learning < 0\n\np needtolearn + learning\n"}, {"source_code": "# Warning: This code sucks.\n\nrequire \"set\"\n\nn, m = gets.split.map(&:to_i)\n\naijs = n.times.map do\n gets.split.map(&:to_i).drop(1)\nend\n\ngroups = []\naijs.each_with_index do |ai, i|\n groups[i] = Set.new ai\nend\n\nknow_nothing = groups.select(&:empty?).count\n\nloop do\n new = []\n 0.upto(groups.size - 1) do |i|\n found = false\n 0.upto(new.size - 1) do |j|\n if (groups[i] & new[j]).any?\n new[j] += groups[i]\n found = true\n break\n end\n end\n if !found\n new << groups[i]\n end\n end\n break if new == groups\n groups = new\nend\n\nans = groups.count - 1\nif know_nothing == groups.count\n ans += 1\nend\np ans"}, {"source_code": "class Node\n\tattr_accessor :id, :visited, :links, :exists\n\tdef initialize id\n\t @exists = true\n\t\t@id = id\n\t\t@visited = false\n\t\t@links = []\n\tend\n\tdef visit nodes\n\t\tif !@visited\n\t\t\t@visited = true\n\t\t\t@links.each { |l|\n\t\t\t\tnodes[l-1].visit nodes\n\t\t\t}\n\t\tend\n\tend\nend\n\n\nn,m = gets.split(\" \").collect! {|x| x.to_i}\nnodes = [0]*n\ngraph = [0]*n\n(1..n).each {|v|\n\tnode = Node.new v\n\tnodes[v-1] = node\n\tgraph[v-1] = node\n}\nzero_list = [0]*m\nzeros = 0\narrs = []\n(1..n).each { |k|\narr = gets.split(\" \").collect! {|x| x.to_i}\narr = arr[1..-1]\nif arr.size == 0\n\tzeros += 1\n nodes[k-1].exists = false\t\nend\narrs.push arr \n}\n\n(1..n).each { |s|\n (s+1..n).each { |t|\n\tif (arrs[s-1]&arrs[t-1]).size>0\n\t\tnodes[s-1].links.push t\n\t\tnodes[t-1].links.push s\n\tend\n }\n}\n\n\ngraph = graph.keep_if {|x| x.exists==true}\nsize = graph.size\ngroups = -1\np = 0\nwhile p 0\n\t $list[j].edgeplus(i)\n\t $list[i].edgeplus(j)\n\tend\n end\nend\n\ng = 0\nfor i in 0...n\n if $list[i].group == nil\n $list[i].search(g)\n\tg += 1\n end\nend\n\nputs g-1"}, {"source_code": "#!/usr/bin/ruby1.9\n\nn, m = STDIN.readline.split.collect {|s| s.to_i }\nseen = (m+1).times.collect { false }\nconnected = (m+1).times.collect { {} }\n\nmute_count = 0\n\nn.times do\n\tas = STDIN.readline.split.collect {|s| s.to_i }\n\tcount = as.shift\n\tif count == 0\n\t\tmute_count += 1\n\t\tnext\n\tend\n\tas.length.times do |i|\n\t\tu = as[i]\n\t\tseen[u] = true\n\t\t(i..as.length-1).each do |j|\n\t\t\tv = as[j]\n\t\t\tconnected[u][v] = true\n\t\t\tconnected[v][u] = true\n\t\tend\n\tend\nend\n\n(1..m).each {|u| puts '%d: %s' % [u, connected[u].keys] }\n\nmarked = (m+1).times.collect { false }\ncomponent_count = 0\n\ndef explore(u, marked, connected)\n\tmarked[u] = true\n\tconnected[u].keys.each do |v|\n\t\tnext if marked[v]\n\t\texplore(v, marked, connected)\n\tend\nend\n\n(1..m).each do |start|\n\tnext if marked[start] or not seen[start]\n\tcomponent_count += 1\n\texplore(start, marked, connected)\nend\n\nputs '%d components, %d mute' % [component_count, mute_count]\nputs component_count-1 + mute_count\n"}, {"source_code": "#!/usr/bin/ruby1.9\n\nn, m = STDIN.readline.split.collect {|s| s.to_i }\nseen = (m+1).times.collect { false }\nconnected = (m+1).times.collect { {} }\n\nmute_count = 0\n\nn.times do\n\tas = STDIN.readline.split.collect {|s| s.to_i }\n\tcount = as.shift\n\tif count == 0\n\t\tmute_count += 1\n\t\tnext\n\tend\n\tas.length.times do |i|\n\t\tu = as[i]\n\t\tseen[u] = true\n\t\t(i..as.length-1).each do |j|\n\t\t\tv = as[j]\n\t\t\tconnected[u][v] = true\n\t\t\tconnected[v][u] = true\n\t\tend\n\tend\nend\n\n\nmarked = (m+1).times.collect { false }\ncomponent_count = 0\n\ndef explore(u, marked, connected)\n\tmarked[u] = true\n\tconnected[u].keys.each do |v|\n\t\tnext if marked[v]\n\t\texplore(v, marked, connected)\n\tend\nend\n\n(1..m).each do |start|\n\tnext if marked[start] or not seen[start]\n\tcomponent_count += 1\n\texplore(start, marked, connected)\nend\n\nputs component_count-1 + mute_count\n"}, {"source_code": "n,m=gets.split.map(&:to_i)\na=[]\nn.times do\n k=0\n gets.split.map(&:to_i)[1..-1].each do |v|\n k+=(1<<(v-1))\n end\n a<=0 then\n for j in i+1..n-1 do\n if a[j]>=0 and (a[i]&a[j])>0 then\n a[i]|=a[j]\n a[j]=-1\n changed=true\n end\n end\n end\n end\nend\n\nres=0\na.each do |v|\n res+=1 if v>=0\nend\nputs res==n ? n : res-1\n"}, {"source_code": "n,m=gets.split.map(&:to_i)\na=[]\nb=[]\nn.times do\n k=0\n gets.split.map(&:to_i)[1..-1].each do |v|\n k+=(1<<(v-1))\n end\n a<0 then\n for j in i+1..n-1 do\n if b[j]>0 and (a[i]&a[j])>0 then\n b[j]=0\n a[i]|=a[j]\n a[j]=a[i]\n changed=true\n break\n end\n break if changed\n end\n end\n break if changed\n end\nend\n\nres=0\nb.each do |v|\n res+=v\nend\nputs res-1\n"}, {"source_code": "n,m=gets.split.map(&:to_i)\na=[]\nn.times do\n k=0\n gets.split.map(&:to_i)[1..-1].each do |v|\n k+=(1<<(v-1))\n end\n a<=0 then\n for j in i+1..n-1 do\n if a[j]>=0 and (a[i]&a[j])>0 then\n a[i]|=a[j]\n a[j]=-1\n changed=true\n end\n end\n end\n end\nend\n\nres=0\na.each do |v|\n res+=1 if v>=0\nend\nputs res-1\n"}, {"source_code": "n, m = gets.split(' ').collect!{|i| i.to_i}\n\nk = []\nn.times do |j|\n\tk[j] = gets.split(' ').collect!{|i| i.to_i}\nend\n\nlangs = Array.new(101, 0)\nneedtolearn = 0\nknownlangs = []\n\nk.each do |sot|\n\tif sot[0] > 0\n\t\t(1..sot[0]).each do |i|\n\t\t\tlangs[sot[i]] += 1\n\n\t\t\tknownlangs << sot[i] if !knownlangs.include?(sot[i])\n\t\tend\n\telse\n\t\tneedtolearn += 1\n\tend\nend\n\nlanmap = {}\n\nlangs.each_index do |lan|\n\t# needtolearn += 1 if lan == 1\n\n\tif langs[lan] > 0\n\t\tlanmap[lan] = {} if !lanmap[lan]\n\t\tknownlangs.each do |kn|\n\t\t\tlanmap[lan][kn] = false if kn != lan\n\t\tend\n\tend\nend\n\n\nknownlangs.each do |kn|\n\tmylangs = [kn]\n\tk.each do |sot|\n\t\tif sot[0] > 0\n\t\t\t(1..sot[0]).each do |i|\n\t\t\t\tmylangs << sot[i] if sot[1..-1].include?(kn) && !mylangs.include?(sot[i])\n\n\t\t\t\t# lanmap[kn][sot[i]] = true if sot[1..-1].include?(kn)\n\t\t\tend\n\t\tend\n\tend\n\n\tmylangs.each do |ln1|\n\t\tmylangs.each do |ln2|\n\t\t\tlanmap[ln1][ln2] = true\n\t\tend\n\tend\nend\n\nallok = false\nlearning = 0\n\nwhile !allok\n\tknownlangs[1..-1].each do |lan2|\n\t\tif lanmap[knownlangs[0]][lan2] == false\n\t\t\tp lan2\n\t\t\tlearning += 1\n\t\t\tlanmap[lan2].each_key do |lan3|\n\t\t\t\tlanmap[knownlangs[0]][lan3] = true if lanmap[lan2][lan3]\n\t\t\tend\n\t\tend\n\t\tallok = true if lan2 == knownlangs[-1]\n\tend\nend\n\np lanmap"}, {"source_code": "n, m = gets.split(' ').collect!{|i| i.to_i}\n\nk = []\nn.times do |j|\n\tk[j] = gets.split(' ').collect!{|i| i.to_i}\nend\n\nlangs = Array.new(101, 0)\nneedtolearn = 0\n\n@mygroups = []\n\ndef checkinclude(int)\n\t@mygroups.each_index do |i|\n\t\treturn i if @mygroups[i].include?(int)\n\tend\n\treturn false\nend\n\nk.each do |line|\n\tif line[0] == 0\n\t\tneedtolearn += 1\n\telse\n\t\tfound = false\n\t\tline[1..-1].each do |lang|\n\t\t\tif grn = checkinclude(lang)\n\t\t\t\t# p grn\n\t\t\t\t# p @mygroups[grn].to_s\n\t\t\t\t# p line[1..-1].to_s\n\t\t\t\t@mygroups[grn] = @mygroups[grn] | line[1..-1]\n\t\t\t\tfound = true\n\t\t\t\tbreak\n\t\t\tend\n\t\tend\n\t\tif !found\n\t\t\t@mygroups << line[1..-1]\n\t\tend\n\tend\nend\n\nsize = @mygroups.size\nsize.times do |i|\n\tsize.times do |j|\n\t\tif i != j && !(@mygroups[i] & @mygroups[j]).empty?\n\t\t\t@mygroups[i] = @mygroups[i] | @mygroups[j]\n\t\t\t@mygroups[j] = []\n\t\tend\n\tend\nend\n\n@mygroups.delete_if {|x| x.empty? }\n\np needtolearn + (@mygroups.size - 1)\n# p needtolearn \n# p @mygroups.to_s"}, {"source_code": "# Warning: This code sucks.\n\nrequire \"set\"\n\nn, m = gets.split.map(&:to_i)\n\naijs = n.times.map do\n gets.split.map(&:to_i).drop(1)\nend\n\ngroups = []\naijs.each_with_index do |ai, i|\n groups[i] = Set.new ai\nend\n\nknow_nothing = groups.select(&:empty?).count\n\nloop do\n new = []\n 0.upto(groups.size - 1) do |i|\n found = false\n 0.upto(new.size - 1) do |j|\n if (groups[i] & new[j]).any?\n new[j] += groups[i]\n found = true\n break\n end\n end\n if !found\n new << groups[i]\n end\n end\n break if new == groups\n groups = new\nend\n\nans = groups.count - 1\nif know_nothing > 0\n ans += 1\nend\np ans"}, {"source_code": "# Warning: This code sucks.\n\nrequire \"set\"\n\nn, m = gets.split.map(&:to_i)\n\naijs = n.times.map do\n gets.split.map(&:to_i).drop(1)\nend\n\ngroups = []\naijs.each_with_index do |ai, i|\n groups[i] = Set.new ai\nend\n\nknow_nothing = groups.select(&:empty?).count\n\nloop do\n new = []\n 0.upto(groups.size - 1) do |i|\n found = false\n 0.upto(new.size - 1) do |j|\n if (groups[i] & new[j]).any?\n new[j] += groups[i]\n found = true\n break\n end\n end\n if !found\n new << groups[i]\n end\n end\n break if new == groups\n groups = new\nend\n\nans = groups.count - 1\nif know_nothing > 1\n ans += 1\nend\np ans"}, {"source_code": "# Warning: This code sucks.\n\nrequire \"set\"\n\nn, m = gets.split.map(&:to_i)\n\naijs = n.times.map do\n gets.split.map(&:to_i).drop(1)\nend\n\ngroups = []\naijs.each_with_index do |ai, i|\n groups[i] = Set.new ai\nend\n\nloop do\n new = []\n 0.upto(groups.size - 1) do |i|\n found = false\n 0.upto(new.size - 1) do |j|\n if (groups[i] & new[j]).any?\n new[j] += groups[i]\n found = true\n break\n end\n end\n if !found\n new << groups[i]\n end\n end\n break if new == groups\n groups = new\nend\n\np groups.count - 1"}, {"source_code": "class Node\n\tattr_accessor :id, :visited, :links\n\tdef initialize id\n\t\t@id = id\n\t\t@visited = false\n\t\t@links = []\n\tend\n\tdef visit nodes\n\t\tif !@visited\n\t\t\t@visited = true\n\t\t\t@links.each { |l|\n\t\t\t\tnodes[l-1].visit nodes\n\t\t\t}\n\t\tend\n\tend\nend\n\n\nn,m = gets.split(\" \").collect! {|x| x.to_i}\nnodes = [0]*n\n(1..n).each {|v|\n\tnodes[v-1] = Node.new v\n}\nzero_list = [0]*m\nzeros = 0\narrs = []\n(1..n).each {\narr = gets.split(\" \").collect! {|x| x.to_i}\narr = arr[1..-1]\nif arr.size == 0\n\tzeros += 1 \nend\narrs.push arr \n}\n\n(1..n).each { |s|\n (s+1..n).each { |t|\n\tif (arrs[s-1]&arrs[t-1]).size>0\n\t\tnodes[s-1].links.push t\n\t\tnodes[t-1].links.push s\n\tend\n }\n}\n\ngroups = -1\np = 0\nwhile p ans\n ans = [md[i], fd[i]].min\n end\nend\nputs ans*2\n"}, {"source_code": "n = gets.chomp.to_i\ngs = []\nas = []\nbs = []\nn.times do\n g, a, b = gets.chomp.split.map { |x| x =~ /[MF]/ ? x : x.to_i }\n gs << g\n as << a\n bs << b\nend\n\nans = 0\nfor d in 1..366\n m = 0\n f = 0\n for i in 0...n\n if gs[i] == 'M'\n m += 1 if as[i] <= d && d <= bs[i]\n else\n f += 1 if as[i] <= d && d <= bs[i]\n end\n end\n ans = [ans, [m, f].min * 2].max\nend\n\nputs ans\n"}, {"source_code": "n = gets.to_i\nms = []\nfs = []\nfor i in 0...n\n g, s, e = gets.split(' ')\n s = s.to_i\n e = e.to_i\n\n for j in s..e\n if g == 'M'\n ms[j] ||= 0\n ms[j] += 1\n else\n fs[j] ||= 0\n fs[j] += 1\n end\n end\nend\n\n\nmaxatt = 0\nmaxi = 0\nfor i in 1..366\n att = [ms[i].to_i, fs[i].to_i].min\n if att > maxatt\n maxatt = att\n maxi = i\n end\nend \n\nputs maxatt*2"}, {"source_code": "n = gets.to_i\na = Array.new(367)\nn.times do\n g, b, e = gets.split\n (b.to_i..e.to_i).each do |i|\n a[i] ||= []\n a[i] << g\n end\nend\nb = a.map do |suba|\n if suba.nil?\n 0\n else\n f, m = 0, 0\n suba.each do |x|\n if x == 'F'\n f += 1\n elsif x == 'M'\n m += 1\n end\n end\n [f, m].min\n end\nend\nputs b.max * 2\n"}, {"source_code": "n = gets.to_i\nM = [0]*10000\nF = [0]*10000\nn.times do\n a = gets.split(\" \")\n gen = a.shift\n q, w = a.map(&:to_i)\n ary = eval gen\n (q..w).each {|d| ary[d] += 1 }\nend\n\np 2*(1..366).map{|i| [M[i], F[i]].min }.max\n\n"}, {"source_code": "n = gets.to_i\nM = [0]*10000\nF = [0]*10000\nn.times do\n a = gets.split(\" \")\n gen = a.shift\n q, w = a.map(&:to_i)\n ary = eval gen\n (q..w).each {|d| ary[d] += 1 }\nend\n\np 2*(1..366).map{|i| [M[i], F[i]].min }.max\n\n"}], "negative_code": [], "src_uid": "75d500bad37fbd2c5456a942bde09cd3"} {"nl": {"description": "There is little time left before the release of the first national operating system BerlOS. Some of its components are not finished yet — the memory manager is among them. According to the developers' plan, in the first release the memory manager will be very simple and rectilinear. It will support three operations: alloc n — to allocate n bytes of the memory and return the allocated block's identifier x; erase x — to erase the block with the identifier x; defragment — to defragment the free memory, bringing all the blocks as close to the beginning of the memory as possible and preserving their respective order; The memory model in this case is very simple. It is a sequence of m bytes, numbered for convenience from the first to the m-th.The first operation alloc n takes as the only parameter the size of the memory block that is to be allocated. While processing this operation, a free block of n successive bytes is being allocated in the memory. If the amount of such blocks is more than one, the block closest to the beginning of the memory (i.e. to the first byte) is prefered. All these bytes are marked as not free, and the memory manager returns a 32-bit integer numerical token that is the identifier of this block. If it is impossible to allocate a free block of this size, the function returns NULL.The second operation erase x takes as its parameter the identifier of some block. This operation frees the system memory, marking the bytes of this block as free for further use. In the case when this identifier does not point to the previously allocated block, which has not been erased yet, the function returns ILLEGAL_ERASE_ARGUMENT.The last operation defragment does not have any arguments and simply brings the occupied memory sections closer to the beginning of the memory without changing their respective order.In the current implementation you are to use successive integers, starting with 1, as identifiers. Each successful alloc operation procession should return following number. Unsuccessful alloc operations do not affect numeration.You are to write the implementation of the memory manager. You should output the returned value for each alloc command. You should also output ILLEGAL_ERASE_ARGUMENT for all the failed erase commands.", "input_spec": "The first line of the input data contains two positive integers t and m (1 ≤ t ≤ 100;1 ≤ m ≤ 100), where t — the amount of operations given to the memory manager for processing, and m — the available memory size in bytes. Then there follow t lines where the operations themselves are given. The first operation is alloc n (1 ≤ n ≤ 100), where n is an integer. The second one is erase x, where x is an arbitrary 32-bit integer numerical token. The third operation is defragment. ", "output_spec": "Output the sequence of lines. Each line should contain either the result of alloc operation procession , or ILLEGAL_ERASE_ARGUMENT as a result of failed erase operation procession. Output lines should go in the same order in which the operations are processed. Successful procession of alloc operation should return integers, starting with 1, as the identifiers of the allocated blocks.", "sample_inputs": ["6 10\nalloc 5\nalloc 3\nerase 1\nalloc 6\ndefragment\nalloc 6"], "sample_outputs": ["1\n2\nNULL\n3"], "notes": null}, "positive_code": [{"source_code": "input = gets.chomp.split.map do |x|\n Integer(x)\nend\n\nn = input[0]\nm = input[1]\n\nmemory = Array.new(m) { |i| 0 }\nnext_id = 1\n\ndef find_and_fill_block(memory, len, next_id)\n head = 0\n for i in 0...memory.length\n if head <= i - len\n break \n end\n\n if memory[i] != 0\n head = i + 1 \n end\n end\n if head + len <= memory.length\n for i in 0...len\n memory[head + i] = next_id\n end\n return true\n end\n\n return false\nend\n\ndef erase_block(memory, id)\n flag = false\n for i in 0...memory.length\n if memory[i] == id\n memory[i] = 0\n flag = true\n end\n end\n\n return flag\nend\n\ndef defragment(memory)\n j = 0\n for i in 0...memory.length\n if memory[i] != 0\n memory[j] = memory[i]\n j += 1\n end\n end\n for i in j...memory.length\n memory[i] = 0\n end\nend\n\nfor i in 0...n\n input = gets.chomp.split\n command = input[0]\n if command[0] == 'a'\n len = Integer(input[1])\n ret = find_and_fill_block(memory, len, next_id)\n if ret\n puts next_id\n next_id += 1\n else\n puts \"NULL\"\n end\n elsif command[0] == 'e'\n cur_id = Integer(input[1])\n ret = cur_id != 0 && erase_block(memory, cur_id)\n if !ret\n puts \"ILLEGAL_ERASE_ARGUMENT\"\n end\n else\n defragment(memory)\n end\nend\n"}, {"source_code": "\ndata = gets.chomp.split\nt = Integer(data[0])\nw = Integer(data[1])\ntask = []\nfor i in 0...t\n task[i] = gets.chomp\nend\n\nmemory = []\nfor i in 0...w\n memory[i] = 0\nend\n\ndef getInt(string)\n string = string.split\n integer = Integer(string[1])\n return integer\nend\ndef allocate(amount, array)\n success = false\n endIndex = 0\n count = 0\n if amount == 1\n for i in 0...array.length\n if array[i] == 0\n endIndex = i - 1\n success = true\n break\n end\n end\n else\n if array.length > 1\n for i in 0...array.length - 1\n if array[i] == array[i + 1] && array[i] == 0\n count += 1\n else\n if count + 1 >= amount\n endIndex = i\n success = true\n break\n else\n count = 0\n end\n end\n if count + 1 >= amount\n endIndex = i\n success = true\n break\n end\n end\n else\n if amount == 1 && array.length == 1\n if array[0] == 0\n array[0] = 1\n success = true\n endIndex = -1\n end\n end\n end\n end\n if success\n return endIndex\n else\n return \"NULL\"\n end\nend\n\n\ndef erase(identifier, array)\n for i in 0...array.length\n if array[i] == identifier\n array[i] = 0\n end\n end \n return array \nend\n\ndef defragment(array)\n count = 0\n i = 0\n while i <= array.length\n if array[i] == 0\n array.delete_at(i)\n count += 1\n else\n i += 1\n end\n end\n count.times{ array.push(0)}\n return array\nend\n\ncast = Hash.new\nkey = 0\nfor i in 0...t \n if task[i][0] == \"a\"\n int = getInt(task[i])\n endIndex = allocate(int, memory)\n if endIndex == \"NULL\"\n puts \"NULL\"\n else\n key += 1\n cast[key] = int\n for i in endIndex - int + 2..endIndex + 1\n memory[i] = key\n end\n puts key\n end\n elsif task[i][0] == \"e\"\n int = getInt(task[i])\n if int <= key && int > 0\n value = cast[int]\n if value != nil\n memory = erase(int, memory)\n cast.delete(int)\n else\n puts \"ILLEGAL_ERASE_ARGUMENT\"\n end\n else\n puts \"ILLEGAL_ERASE_ARGUMENT\"\n end\n else\n memory = defragment(memory)\n end\nend\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\nidentifiers = {}\nmemory_blocks = {}\nidentifier = 1\n\nn.times do\n\tcommand = gets.split(' ')\t\n\tcase command[0]\n\t\twhen \"alloc\"\n\t\t\tstart_index = 0\n\t\t\tblock_size = command[1].to_i\n\t\t\tfit = false\n\t\t\twhile memory_blocks[start_index]\n\t\t\t\tstart_index += memory_blocks[start_index]\n\t\t\tend\n\t\t\twhile start_index + block_size <= m \n\t\t\t\tbreak if block_size == m && !memory_blocks.empty?\n\t\t\t\tcheck = memory_blocks.select { |k, v| (k < start_index + block_size) && (k >= start_index) }\n\t\t\t\tif check.empty?\n\t\t\t\t\tmemory_blocks[start_index] = block_size\n\t\t\t\t\tidentifiers[identifier] = start_index\n\t\t\t\t\tputs identifier\n\t\t\t\t\tidentifier += 1\n\t\t\t\t\tfit = true\n\t\t\t\t\t# puts \"memory_blocks is #{memory_blocks}\"\n\t\t\t\t\t# puts \"identifiers is #{identifiers}\"\n\t\t\t\t\tbreak\n\t\t\t\telse\n\t\t\t\t\tnext_index = check.min\n\t\t\t\t\tstart_index = next_index[0] + memory_blocks[next_index[0]]\n\t\t\t\tend\n\t\t\tend\n\t\t\tputs \"NULL\" unless fit\n\t\twhen \"erase\"\n\t\t\td_iden = command[1].to_i\n\t\t\tiden_index = identifiers[d_iden]\n\t\t\tif iden_index.nil?\n\t\t\t\tputs \"ILLEGAL_ERASE_ARGUMENT\"\n\t\t\telse\n\t\t\t\tidentifiers.delete(d_iden)\n\t\t\t\tmemory_blocks.delete(iden_index)\n\t\t\t\t# puts \"memory_blocks is #{memory_blocks}\"\n\t\t\t\t# puts \"identifiers is #{identifiers}\"\n\t\t\tend\n\t\telse # defragment\n\t\t\tstart_index = 0\n\t\t\tcopy = {}\n\t\t\tmemory_blocks.size.times do\n\t\t\t\tlowest = memory_blocks.min\n\t\t\t\tcopy[start_index] = lowest[1]\n\t\t\t\tidentifiers[identifiers.key(lowest[0])] = start_index\n\t\t\t\tstart_index += lowest[1]\n\t\t\t\tmemory_blocks.delete(lowest[0])\n\t\t\tend\n\t\t\tmemory_blocks = copy\n\t\t\t# puts \"memory_blocks is #{memory_blocks}\"\n\t\t\t# puts \"identifiers is #{identifiers}\"\n\t\tend\nend\n"}, {"source_code": "\nclass MemManager\n\n EMPTY = 0\n\n def initialize(size)\n @size = size\n @mem = Array.new(size, EMPTY)\n @id = 1\n @used_ids = {}\n end\n\n def get_mem\n @mem.to_s\n end\n\n # returns block id or 0 if cannot alloc block\n def alloc(len)\n ind = find_empty_block(len)\n if ind >= 0\n fill_block(ind, len, @id)\n @used_ids[@id] = [ind, len]\n @id += 1\n return @id - 1\n end\n 0\n end\n\n def erase(id)\n if @used_ids[id]\n ind, len = @used_ids[id]\n fill_block(ind, len, EMPTY)\n @used_ids.delete(id)\n #p @used_ids\n return true\n end\n false\n end\n\n def defrag\n # get keys in order they are in mem\n keys = @mem.uniq\n keys.delete(0)\n # move 0-s to the end of @mem\n @mem = @mem.delete_if {|x| x == 0}\n @mem << 0 while @mem.size < @size\n # relocate @used_ids\n cur_ind = 0\n keys.each do |id|\n ind, len = @used_ids[id]\n ind = cur_ind\n cur_ind += len\n @used_ids[id] = [ind, len]\n end\n end\n\nprivate\n\n # returns -1 if cannot find empty block of length n\n def find_empty_block(n)\n start = 0\n while start <= @size - n\n if @mem[start] != EMPTY\n start += 1\n next\n end\n i = 0\n while @mem[start + i] == EMPTY\n i += 1\n end\n return start if i >= n\n start += i\n end\n -1\n end\n\n def fill_block(ind, len, value)\n @mem.fill(value, ind, len)\n end\nend\n\n\nif __FILE__ == $0\n t, m = gets.split.collect {|x| Integer(x)}\n mm = MemManager.new(m)\n while line = gets\n line.chomp!\n if line.start_with?('alloc')\n a = line.scan(/\\d+/)\n n = Integer(a[0])\n id = mm.alloc(n)\n puts (id == 0) ? \"NULL\" : id\n elsif line.start_with?('erase')\n a = line.scan(/\\d+/)\n n = Integer(a[0])\n puts \"ILLEGAL_ERASE_ARGUMENT\" unless mm.erase(n)\n elsif line.start_with?('defragment')\n mm.defrag\n end\n end\nend\n"}], "negative_code": [{"source_code": "input = gets.chomp.split.map do |x|\n Integer(x)\nend\n\nn = input[0]\nm = input[1]\n\nmemory = Array.new(m) { |i| 0 }\nnext_id = 1\n\ndef find_and_fill_block(memory, len, next_id)\n head = 0\n for i in 0...memory.length\n if head <= i - len\n break \n end\n\n if memory[i] != 0\n head = i + 1 \n end\n end\n if head + len <= memory.length\n for i in 0...len\n memory[head + i] = next_id\n end\n return true\n end\n\n return false\nend\n\ndef erase_block(memory, id)\n flag = false\n for i in 0...memory.length\n if memory[i] == id\n memory[i] = 0\n flag = true\n end\n end\n\n return flag\nend\n\ndef defragment(memory)\n j = 0\n for i in 0...memory.length\n if memory[i] != 0\n memory[j] = memory[i]\n j += 1\n end\n end\n for i in j...memory.length\n memory[i] = 0\n end\nend\n\nfor i in 0...n\n input = gets.chomp.split\n command = input[0]\n if command[0] == 'a'\n len = Integer(input[1])\n ret = find_and_fill_block(memory, len, next_id)\n if ret\n puts next_id\n next_id += 1\n else\n puts \"NULL\"\n end\n elsif command[0] == 'e'\n cur_id = Integer(input[1])\n ret = erase_block(memory, cur_id)\n if !ret\n puts \"ILLEGAL_ERASE_ARGUMENT\"\n end\n else\n defragment(memory)\n end\n puts memory\nend\n"}, {"source_code": "input = gets.chomp.split.map do |x|\n Integer(x)\nend\n\nn = input[0]\nm = input[1]\n\nmemory = Array.new(m) { |i| 0 }\nnext_id = 1\n\ndef find_and_fill_block(memory, len, next_id)\n head = 0\n for i in 0...memory.length\n if head <= i - len\n break \n end\n\n if memory[i] != 0\n head = i + 1 \n end\n end\n if head + len <= memory.length\n for i in 0...len\n memory[head + i] = next_id\n end\n return true\n end\n\n return false\nend\n\ndef erase_block(memory, id)\n flag = false\n for i in 0...memory.length\n if memory[i] == id\n memory[i] = 0\n flag = true\n end\n end\n\n return flag\nend\n\ndef defragment(memory)\n j = 0\n for i in 0...memory.length\n if memory[i] != 0\n memory[j] = memory[i]\n j += 1\n end\n end\n for i in j...memory.length\n memory[i] = 0\n end\nend\n\nfor i in 0...n\n input = gets.chomp.split\n command = input[0]\n if command[0] == 'a'\n len = Integer(input[1])\n ret = find_and_fill_block(memory, len, next_id)\n if ret\n puts next_id\n next_id += 1\n else\n puts \"NULL\"\n end\n elsif command[0] == 'e'\n cur_id = Integer(input[1])\n ret = erase_block(memory, cur_id)\n if !ret\n puts \"ILLEGAL_ERASE_ARGUMENT\"\n end\n else\n defragment(memory)\n end\nend\n"}, {"source_code": "\ndata = gets.chomp.split\nt = Integer(data[0])\nw = Integer(data[1])\ntask = []\nfor i in 0...t\n task[i] = []\n task[i] = gets.chomp\nend\n\nmemory = []\nfor i in 0...w\n memory[i] = 0\nend\n\ndef allocate(amount, array)\n success = false\n endIndex = 0\n count = 0\n for i in 0...array.length - 1\n if array[i] == array[i + 1] && array[i] == 0\n count += 1\n else\n if count + 1 >= amount\n endIndex = i\n success = true\n break\n else\n count = 0\n end\n end\n if count + 1 >= amount\n endIndex = i\n success = true\n break\n end\n end\n if success\n return endIndex\n else\n return \"NULL\"\n end\nend\n\n\ndef erase(identifier, array)\n for i in 0...array.length\n if array[i] == identifier\n array[i] = 0\n end\n end \n return array \nend\n\ndef defragment(array)\n count = 0\n i = 0\n while i <= array.length\n if array[i] == 0\n array.delete_at(i)\n count += 1\n else\n i += 1\n end\n end\n count.times{ array.push(0)}\n return array\nend\n\ncast = Hash.new\nkey = 0\nfor i in 0...t\n if task[i][0] == \"a\"\n amount = Integer(task[i][task[i].length - 1])\n endIndex = allocate(amount, memory)\n if endIndex == \"NULL\"\n puts \"NULL\"\n else\n key += 1\n cast[key] = amount\n for i in endIndex - amount + 2..endIndex + 1\n memory[i] = key\n end\n puts key\n end\n elsif task[i][0] == \"e\"\n identifier = Integer(task[i][task[i].length - 1])\n if identifier <= key\n memory = erase(identifier, memory)\n cast[identifier] = -1\n else\n puts \"ILLEGAL_ERASE_ARGUMENT\"\n end\n else\n memory = defragment(memory)\n end\nend\n"}, {"source_code": "\ndata = gets.chomp.split\nt = Integer(data[0])\nw = Integer(data[1])\ntask = []\nfor i in 0...t\n task[i] = []\n task[i] = gets.chomp\nend\n\nmemory = []\nfor i in 0...w\n memory[i] = 0\nend\n\ndef allocate(amount, array)\n success = false\n endIndex = 0\n count = 0\n if array.length > 1\n for i in 0...array.length - 1\n if array[i] == array[i + 1] && array[i] == 0\n count += 1\n else\n if count + 1 >= amount\n endIndex = i\n success = true\n break\n else\n count = 0\n end\n end\n if count + 1 >= amount\n endIndex = i\n success = true\n break\n end\n end\n else\n if amount == 1 && array.length == 1\n if array[0] == 0\n array[0] = 1\n success = true\n endIndex = -1\n end\n end\n end\n if success\n return endIndex\n else\n return \"NULL\"\n end\nend\n\n\ndef erase(identifier, array)\n for i in 0...array.length\n if array[i] == identifier\n array[i] = 0\n end\n end \n return array \nend\n\ndef defragment(array)\n count = 0\n i = 0\n while i <= array.length\n if array[i] == 0\n array.delete_at(i)\n count += 1\n else\n i += 1\n end\n end\n count.times{ array.push(0)}\n return array\nend\n\ncast = Hash.new\nkey = 0\nfor i in 0...t\n if task[i][0] == \"a\"\n amount = Integer(task[i][task[i].length - 1])\n endIndex = allocate(amount, memory)\n if endIndex == \"NULL\"\n puts \"NULL\"\n else\n key += 1\n cast[key] = amount\n for i in endIndex - amount + 2..endIndex + 1\n memory[i] = key\n end\n puts key\n end\n elsif task[i][0] == \"e\"\n negetive = task[i][task[i].length - 2]\n if negetive == \"-\"\n identifier = 0 - Integer(task[i][task[i].length - 1])\n else\n identifier = Integer(task[i][task[i].length - 1])\n end\n if identifier <= key && identifier > 0\n value = cast[identifier]\n if value != nil\n memory = erase(identifier, memory)\n cast.delete(identifier)\n else\n puts \"ILLEGAL_ERASE_ARGUMENT\"\n end\n else\n puts \"ILLEGAL_ERASE_ARGUMENT\"\n end\n else\n memory = defragment(memory)\n end\nend\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\nidentifiers = {}\nmemory_blocks = {}\nstart_index = 0\nidentifier = 1\n\nn.times do\n\tcommand = gets.split(' ')\t\n\tcase command[0]\n\t\twhen \"alloc\"\n\t\t\tblock_size = command[1].to_i\n\t\t\tfit = false\n\t\t\twhile memory_blocks[start_index]\n\t\t\t\tstart_index += memory_blocks[start_index]\n\t\t\tend\n\t\t\twhile start_index + block_size <= m \n\t\t\t\tbreak if block_size == m && !memory_blocks.empty?\n\t\t\t\tcheck = memory_blocks.select { |k, v| (k < start_index + block_size) && (k >= start_index) }\n\t\t\t\tif check.empty?\n\t\t\t\t\tmemory_blocks[start_index] = block_size\n\t\t\t\t\tidentifiers[identifier] = start_index\n\t\t\t\t\tputs identifier\n\t\t\t\t\tidentifier += 1\n\t\t\t\t\tstart_index += block_size\n\t\t\t\t\tfit = true\n\t\t\t\t\tbreak\n\t\t\t\telse\n\t\t\t\t\tnext_index = check.min\n\t\t\t\t\tstart_index = next_index[0] + memory_blocks[next_index[0]]\n\t\t\t\tend\n\t\t\tend\n\t\t\tputs \"NULL\" unless fit\n\t\twhen \"erase\"\n\t\t\td_iden = command[1].to_i\n\t\t\tiden_index = identifiers[d_iden]\n\t\t\tif iden_index.nil?\n\t\t\t\tputs \"ILLEGAL_ERASE_ARGUMENT\"\n\t\t\telse\n\t\t\t\tidentifiers.delete(d_iden)\n\t\t\t\tmemory_blocks.delete(iden_index)\n\t\t\t\tstart_index = iden_index\n\t\t\tend\n\t\telse # defragment\n\t\t\tstart_index = 0\n\t\t\tcopy = {}\n\t\t\tmemory_blocks.size.times do\n\t\t\t\tlowest = memory_blocks.min\n\t\t\t\tcopy[start_index] = lowest[1]\n\t\t\t\tidentifiers[identifiers.key(lowest[0])] = start_index\n\t\t\t\tstart_index += lowest[1]\n\t\t\t\tmemory_blocks.delete(lowest[0])\n\t\t\tend\n\t\t\tmemory_blocks = copy\n\t\tend\nend\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\nidentifiers = {}\nmemory_blocks = {}\nstart_index = 0\nidentifier = 1\n\nn.times do\n\tcommand = gets.split(' ')\t\n\tcase command[0]\n\t\twhen \"alloc\"\n\t\t\tblock_size = command[1].to_i\n\t\t\tfit = false\n\t\t\twhile memory_blocks[start_index]\n\t\t\t\tstart_index += memory_blocks[start_index]\n\t\t\tend\n\t\t\twhile start_index + block_size <= m \n\t\t\t\tbreak if block_size == m && !memory_blocks.empty?\n\t\t\t\tcheck = memory_blocks.select { |k, v| (k < start_index + block_size) && (k >= start_index) }\n\t\t\t\tif check.empty?\n\t\t\t\t\tmemory_blocks[start_index] = block_size\n\t\t\t\t\tidentifiers[identifier] = start_index\n\t\t\t\t\tputs identifier\n\t\t\t\t\tidentifier += 1\n\t\t\t\t\tstart_index += block_size\n\t\t\t\t\tfit = true\n\t\t\t\t\tputs \"memory_blocks is #{memory_blocks}\"\n\t\t\t\t\tputs \"identifiers is #{identifiers}\"\n\t\t\t\t\tbreak\n\t\t\t\telse\n\t\t\t\t\tnext_index = check.min\n\t\t\t\t\tstart_index = next_index[1] + memory_blocks[next_index[0]]\n\t\t\t\tend\n\t\t\tend\n\t\t\tputs \"NULL\" unless fit\n\t\twhen \"erase\"\n\t\t\td_iden = command[1].to_i\n\t\t\tiden_index = identifiers[d_iden]\n\t\t\tif iden_index.nil?\n\t\t\t\tputs \"ILLEGAL_ERASE_ARGUMENT\"\n\t\t\telse\n\t\t\t\tidentifiers.delete(d_iden)\n\t\t\t\tmemory_blocks.delete(iden_index)\n\t\t\t\tstart_index = iden_index\n\t\t\t\tputs \"start_index is #{start_index}\"\n\t\t\t\tputs \"memory_blocks is #{memory_blocks}\"\n\t\t\t\tputs \"identifiers is #{identifiers}\"\n\t\t\tend\n\t\telse # defragment\n\t\t\tstart_index = 0\n\t\t\tcopy = {}\n\t\t\tmemory_blocks.size.times do\n\t\t\t\tlowest = memory_blocks.min\n\t\t\t\tcopy[start_index] = lowest[1]\n\t\t\t\tidentifiers[identifiers.key(lowest[0])] = start_index\n\t\t\t\tstart_index += lowest[1]\n\t\t\t\tmemory_blocks.delete(lowest[0])\n\t\t\tend\n\t\t\tmemory_blocks = copy\n\t\t\tputs \"memory_blocks is #{memory_blocks}\"\n\t\t\tputs \"identifiers is #{identifiers}\"\n\t\tend\nend\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\nidentifiers = {}\nmemory_blocks = {}\nstart_index = 0\nidentifier = 1\n\nn.times do\n\tcommand = gets.split(' ')\t\n\tcase command[0]\n\t\twhen \"alloc\"\n\t\t\tblock_size = command[1].to_i\n\t\t\tfit = false\n\t\t\twhile memory_blocks[start_index]\n\t\t\t\tstart_index += memory_blocks[start_index]\n\t\t\tend\n\t\t\twhile start_index + block_size <= m \n\t\t\t\tbreak if block_size == m && !memory_blocks.empty?\n\t\t\t\tcheck = memory_blocks.select { |k, v| (k < start_index + block_size) && (k >= start_index) }\n\t\t\t\tif check.empty?\n\t\t\t\t\tmemory_blocks[start_index] = block_size\n\t\t\t\t\tidentifiers[identifier] = start_index\n\t\t\t\t\tputs identifier\n\t\t\t\t\tidentifier += 1\n\t\t\t\t\tstart_index += block_size\n\t\t\t\t\tfit = true\n\t\t\t\t\tbreak\n\t\t\t\telse\n\t\t\t\t\tnext_index = check.min\n\t\t\t\t\tstart_index = next_index[1] + memory_blocks[next_index[0]]\n\t\t\t\tend\n\t\t\tend\n\t\t\tputs \"NULL\" unless fit\n\t\twhen \"erase\"\n\t\t\td_iden = command[1].to_i\n\t\t\tiden_index = identifiers[d_iden]\n\t\t\tif iden_index.nil?\n\t\t\t\tputs \"ILLEGAL_ERASE_ARGUMENT\"\n\t\t\telse\n\t\t\t\tidentifiers.delete(d_iden)\n\t\t\t\tmemory_blocks.delete(iden_index)\n\t\t\t\tstart_index = iden_index\n\t\t\tend\n\t\telse # defragment\n\t\t\tstart_index = 0\n\t\t\tcopy = {}\n\t\t\tmemory_blocks.size.times do\n\t\t\t\tlowest = memory_blocks.min\n\t\t\t\tcopy[start_index] = lowest[1]\n\t\t\t\tidentifiers[identifiers.key(lowest[0])] = start_index\n\t\t\t\tstart_index += lowest[1]\n\t\t\t\tmemory_blocks.delete(lowest[0])\n\t\t\tend\n\t\t\tmemory_blocks = copy\n\t\tend\nend\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\nidentifiers = {}\nmemory_blocks = {}\nstart_index = 0\nidentifier = 1\n\nn.times do\n\tcommand = gets.split(' ')\t\n\tcase command[0]\n\t\twhen \"alloc\"\n\t\t\tblock_size = command[1].to_i\n\t\t\tfit = false\n\t\t\twhile memory_blocks[start_index]\n\t\t\t\tstart_index += memory_blocks[start_index]\n\t\t\tend\n\t\t\twhile start_index + block_size <= m\n\t\t\t\tcheck = memory_blocks.select { |k, v| (k < start_index + block_size) && (k >= start_index) }\n\t\t\t\tif check.empty?\n\t\t\t\t\tmemory_blocks[start_index] = block_size\n\t\t\t\t\tidentifiers[identifier] = start_index\n\t\t\t\t\tputs identifier\n\t\t\t\t\tidentifier += 1\n\t\t\t\t\tstart_index += block_size\n\t\t\t\t\tfit = true\n\t\t\t\t\tbreak\n\t\t\t\telse\n\t\t\t\t\tnext_index = check.min\n\t\t\t\t\tstart_index = next_index[1] + memory_blocks[next_index[0]]\n\t\t\t\tend\n\t\t\tend\n\t\t\tputs \"NULL\" unless fit\n\t\twhen \"erase\"\n\t\t\td_iden = command[1].to_i\n\t\t\tiden_index = identifiers[d_iden]\n\t\t\tif iden_index.nil?\n\t\t\t\tputs \"ILLEGAL_ERASE_ARGUMENT\"\n\t\t\telse\n\t\t\t\tidentifiers.delete(d_iden)\n\t\t\t\tmemory_blocks.delete(iden_index)\n\t\t\t\tstart_index = iden_index\n\t\t\tend\n\t\telse # defragment\n\t\t\tstart_index = 0\n\t\t\tcopy = memory_blocks\n\t\t\tmemory_blocks.size.times do\n\t\t\t\tlowest = copy.min\n\t\t\t\tmemory_blocks[start_index] = lowest[1]\n\t\t\t\tidentifiers[identifiers.key(lowest[0])] = start_index\n\t\t\t\tstart_index += lowest[1]\n\t\t\t\tcopy.delete(lowest[0])\n\t\t\t\tmemory_blocks.delete(lowest[0])\n\t\t\tend\n\t\tend\nend\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\nidentifiers = {}\nmemory_blocks = {}\nidentifier = 1\n\nn.times do\n\tcommand = gets.split(' ')\t\n\tcase command[0]\n\t\twhen \"alloc\"\n\t\t\tstart_index = 0\n\t\t\tblock_size = command[1].to_i\n\t\t\tfit = false\n\t\t\twhile memory_blocks[start_index]\n\t\t\t\tstart_index += memory_blocks[start_index]\n\t\t\tend\n\t\t\twhile start_index + block_size <= m \n\t\t\t\tbreak if block_size == m && !memory_blocks.empty?\n\t\t\t\tcheck = memory_blocks.select { |k, v| (k < start_index + block_size) && (k >= start_index) }\n\t\t\t\tif check.empty?\n\t\t\t\t\tmemory_blocks[start_index] = block_size\n\t\t\t\t\tidentifiers[identifier] = start_index\n\t\t\t\t\tputs identifier\n\t\t\t\t\tidentifier += 1\n\t\t\t\t\tfit = true\n\t\t\t\t\tputs \"memory_blocks is #{memory_blocks}\"\n\t\t\t\t\tputs \"identifiers is #{identifiers}\"\n\t\t\t\t\tbreak\n\t\t\t\telse\n\t\t\t\t\tnext_index = check.min\n\t\t\t\t\tstart_index = next_index[0] + memory_blocks[next_index[0]]\n\t\t\t\tend\n\t\t\tend\n\t\t\tputs \"NULL\" unless fit\n\t\twhen \"erase\"\n\t\t\td_iden = command[1].to_i\n\t\t\tiden_index = identifiers[d_iden]\n\t\t\tif iden_index.nil?\n\t\t\t\tputs \"ILLEGAL_ERASE_ARGUMENT\"\n\t\t\telse\n\t\t\t\tidentifiers.delete(d_iden)\n\t\t\t\tmemory_blocks.delete(iden_index)\n\t\t\t\tputs \"memory_blocks is #{memory_blocks}\"\n\t\t\t\tputs \"identifiers is #{identifiers}\"\n\t\t\tend\n\t\telse # defragment\n\t\t\tstart_index = 0\n\t\t\tcopy = {}\n\t\t\tmemory_blocks.size.times do\n\t\t\t\tlowest = memory_blocks.min\n\t\t\t\tcopy[start_index] = lowest[1]\n\t\t\t\tidentifiers[identifiers.key(lowest[0])] = start_index\n\t\t\t\tstart_index += lowest[1]\n\t\t\t\tmemory_blocks.delete(lowest[0])\n\t\t\tend\n\t\t\tmemory_blocks = copy\n\t\t\tputs \"memory_blocks is #{memory_blocks}\"\n\t\t\tputs \"identifiers is #{identifiers}\"\n\t\tend\nend\n"}], "src_uid": "a6cba17c5ddb93f6741e00280fb6c54c"} {"nl": {"description": "You are given a multiset (i. e. a set that can contain multiple equal integers) containing $$$2n$$$ integers. Determine if you can split it into exactly $$$n$$$ pairs (i. e. each element should be in exactly one pair) so that the sum of the two elements in each pair is odd (i. e. when divided by $$$2$$$, the remainder is $$$1$$$).", "input_spec": "The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1\\leq t\\leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1\\leq n\\leq 100$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1,a_2,\\dots, a_{2n}$$$ ($$$0\\leq a_i\\leq 100$$$) — the numbers in the set.", "output_spec": "For each test case, print \"Yes\" if it can be split into exactly $$$n$$$ pairs so that the sum of the two elements in each pair is odd, and \"No\" otherwise. You can print each letter in any case.", "sample_inputs": ["5\n2\n2 3 4 5\n3\n2 3 4 5 5 5\n1\n2 4\n1\n2 3\n4\n1 5 3 2 6 7 3 4"], "sample_outputs": ["Yes\nNo\nNo\nYes\nNo"], "notes": "NoteIn the first test case, a possible way of splitting the set is $$$(2,3)$$$, $$$(4,5)$$$.In the second, third and fifth test case, we can prove that there isn't any possible way.In the fourth test case, a possible way of splitting the set is $$$(2,3)$$$."}, "positive_code": [{"source_code": "#!/usr/bin/env ruby\n\nncases = gets.chomp.to_i\n\nncases.times do\n n = gets.chomp.to_i\n numbers = gets.chomp.split(' ').map { |str| str.to_i }\n answer = numbers.map { |n| n % 2 == 0 }.filter { |b| b }.size == n\n puts answer ? 'Yes' : 'No'\nend\n"}, {"source_code": "\ndef possible?(arr)\n arr.filter { |x| x % 2 == 1}.size == arr.size / 2\nend\n\nt = gets.chomp.to_i\nt.times do\n n = gets.chomp.to_i\n inp = gets.chomp.split(' ').map(&:to_i)\n possible?(inp) ? puts(\"Yes\\n\") : puts(\"No\\n\")\nend\n"}, {"source_code": "gets\r\nwhile gets\r\n a = gets.split.map(&:to_i)\r\n puts a.count(&:odd?) == a.size / 2 ? 'Yes' : 'No'\r\nend"}, {"source_code": "a = gets.to_i\r\na.times{\r\n n = gets.to_i\r\n arr = gets.split.map &:to_i\r\n b = arr.select{_1.odd?}\r\n c = arr - b\r\n if b.size == n && c.size == n then puts \"Yes\"\r\n else \r\n puts \"No\"\r\nend\r\n}"}, {"source_code": "t = gets.to_i\r\nt.times { |u|\r\n n = gets.to_i\r\n arr = gets.chomp.split(\" \").map(&:to_i)\r\n even = 0\r\n odd = 0\r\n arr.each { |x|\r\n if x % 2 == 0\r\n even += 1\r\n else\r\n odd += 1\r\n end\r\n }\r\n if even == odd\r\n puts \"Yes\"\r\n else\r\n puts \"No\"\r\n end\r\n}"}, {"source_code": "gets.to_i.times do\r\n n=gets.to_i\r\n a=gets.split.map(&:to_i)\r\n c=0\r\n a.each do |aa|\r\n if aa.odd?\r\n c+=1\r\n end\r\n end\r\n if c==n\r\n puts \"Yes\"\r\n else\r\n puts \"No\"\r\n end\r\nend\r\n"}], "negative_code": [], "src_uid": "d5bd27c969d9cd910f13baa53c247871"} {"nl": {"description": "Let $$$\\mathsf{AND}$$$ denote the bitwise AND operation, and $$$\\mathsf{OR}$$$ denote the bitwise OR operation.You are given an array $$$a$$$ of length $$$n$$$ and a non-negative integer $$$k$$$. You can perform at most $$$k$$$ operations on the array of the following type: Select an index $$$i$$$ ($$$1 \\leq i \\leq n$$$) and replace $$$a_i$$$ with $$$a_i$$$ $$$\\mathsf{OR}$$$ $$$2^j$$$ where $$$j$$$ is any integer between $$$0$$$ and $$$30$$$ inclusive. In other words, in an operation you can choose an index $$$i$$$ ($$$1 \\leq i \\leq n$$$) and set the $$$j$$$-th bit of $$$a_i$$$ to $$$1$$$ ($$$0 \\leq j \\leq 30$$$). Output the maximum possible value of $$$a_1$$$ $$$\\mathsf{AND}$$$ $$$a_2$$$ $$$\\mathsf{AND}$$$ $$$\\dots$$$ $$$\\mathsf{AND}$$$ $$$a_n$$$ after performing at most $$$k$$$ operations. ", "input_spec": "The first line of the input contains a single integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains the integers $$$n$$$ and $$$k$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$, $$$0 \\le k \\le 10^9$$$). Then a single line follows, containing $$$n$$$ integers describing the arrays $$$a$$$ ($$$0 \\leq a_i < 2^{31}$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, output a single line containing the maximum possible $$$\\mathsf{AND}$$$ value of $$$a_1$$$ $$$\\mathsf{AND}$$$ $$$a_2$$$ $$$\\mathsf{AND}$$$ $$$\\dots$$$ $$$\\mathsf{AND}$$$ $$$a_n$$$ after performing at most $$$k$$$ operations.", "sample_inputs": ["4\n3 2\n2 1 1\n7 0\n4 6 6 28 6 6 12\n1 30\n0\n4 4\n3 1 3 1"], "sample_outputs": ["2\n4\n2147483646\n1073741825"], "notes": "NoteFor the first test case, we can set the bit $$$1$$$ ($$$2^1$$$) of the last $$$2$$$ elements using the $$$2$$$ operations, thus obtaining the array [$$$2$$$, $$$3$$$, $$$3$$$], which has $$$\\mathsf{AND}$$$ value equal to $$$2$$$.For the second test case, we can't perform any operations so the answer is just the $$$\\mathsf{AND}$$$ of the whole array which is $$$4$$$."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n n, k = gets.split.map(&:to_i)\r\n xs = gets.split.map(&:to_i)\r\n 30.downto(0).each do |i|\r\n count = xs.count { |x| x & (1 << i) == 0 }\r\n if k >= count\r\n j = 0\r\n while j < n\r\n xs[j] = xs[j] | (1 << i)\r\n j += 1\r\n end\r\n k -= count\r\n end\r\n end\r\n p xs.reduce(:&)\r\nend\r\n"}], "negative_code": [], "src_uid": "0751386917a5750695312773005684ec"} {"nl": {"description": "You are given a multiset of n integers. You should select exactly k of them in a such way that the difference between any two of them is divisible by m, or tell that it is impossible.Numbers can be repeated in the original multiset and in the multiset of selected numbers, but number of occurrences of any number in multiset of selected numbers should not exceed the number of its occurrences in the original multiset. ", "input_spec": "First line contains three integers n, k and m (2 ≤ k ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000) — number of integers in the multiset, number of integers you should select and the required divisor of any pair of selected integers. Second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109) — the numbers in the multiset.", "output_spec": "If it is not possible to select k numbers in the desired way, output «No» (without the quotes). Otherwise, in the first line of output print «Yes» (without the quotes). In the second line print k integers b1, b2, ..., bk — the selected numbers. If there are multiple possible solutions, print any of them. ", "sample_inputs": ["3 2 3\n1 8 4", "3 3 3\n1 8 4", "4 3 5\n2 7 7 7"], "sample_outputs": ["Yes\n1 4", "No", "Yes\n2 7 7"], "notes": null}, "positive_code": [{"source_code": "def solution(k, ans_index, hash)\n puts 'Yes'\n hash[ans_index].each_with_index do |val, index|\n print \"#{val} \" if index < k\n end\nend\n\ndef get_hash_and_index(k, m, array)\n hash = []\n ans_index = -1\n array.each do |x|\n rem = x % m\n hash[rem] = [] if hash[rem].nil?\n hash[rem].push(x)\n ans_index = rem if hash[rem].length == k\n end\n ans_index == -1 ? (puts 'No') : solution(k, ans_index, hash)\nend\n\n_n, k, m = STDIN.gets.split.map(&:to_i)\narray = STDIN.gets.split.map(&:to_i)\n\nget_hash_and_index(k, m, array)\n"}, {"source_code": "#! /usr/bin/env ruby\n\nn, k, m = gets.split.map &:to_i\na = gets.split.map &:to_i\na = a.group_by { |i| i % m }\nyes = false\na.each_key do |key|\n if a[key].size >= k\n yes = true\n puts:Yes\n puts a[key][0...k] * ' '\n break\n end\nend\nif not yes\n puts:No\nend\n\n"}, {"source_code": "def read_integers\n STDIN.gets.split.map(&:to_i)\nend \n\ndef solve array, m, k, hash\n result = []\n array.each do |x| \n rem = x%m \n hash[rem] = hash[rem] || []\n hash[rem].push(x)\n result = hash[rem] if hash[rem].length > result.length\n end\n result\nend\n\ndef solution \n n, k, m = read_integers\n array = read_integers\n hash = []\n result = solve array, m, k, hash\n if result.length < k\n print \"No\\n\"\n else\n print \"Yes\\n\"\n result.each_with_index do |val, index| \n print val, ' ' if index < k \n end\n end\nend\n\nsolution"}, {"source_code": "def read_integers\n STDIN.gets.split.map(&:to_i)\nend \n\ndef solve array, m, k\n result = []\n hash = []\n array.each do |x| \n rem = x%m \n hash[rem] = hash[rem] || []\n hash[rem].push(x)\n result = hash[rem] if hash[rem].length > result.length\n end\n result\nend\n\ndef solution \n n, k, m = read_integers\n array = read_integers\n result = solve array, m, k\n if result.length < k\n print \"No\\n\"\n else\n print \"Yes\\n\"\n result.each_with_index do |val, index| \n print val, ' ' if index < k \n end\n end\nend\n\nsolution"}, {"source_code": "\ndef read_integers\n STDIN.gets.split.map(&:to_i)\nend \n\ndef solve array, m, k, hash\n ans_index = -1;\n array.each do |x| \n rem = x%m \n hash[rem] = hash[rem] || []\n hash[rem].push(x)\n if hash[rem].length == k\n ans_index = rem\n end\n end\n ans_index\nend \n\ndef solution \n n, k, m = read_integers\n array = read_integers\n hash = []\n ans_index = solve array, m, k, hash\n if ans_index == -1\n print \"No\\n\"\n else\n print \"Yes\\n\"\n hash[ans_index ].each_with_index do \n |val, index| \n if index < k \n print val\n print ' '\n end\n end\n end\nend\n\nsolution"}, {"source_code": "def solution \n n, k, m = STDIN.gets.split.map(&:to_i)\n array = STDIN.gets.split.map(&:to_i)\n \n hash = []\n ans_index = -1;\n array.each{ |x| \n rem = x%m \n \n if hash[rem] == nil then\n hash[rem] = []\n end\n \n hash[rem].push(x)\n \n if hash[rem].length == k then \n ans_index = rem\n end\n \n }\n \n if ans_index == -1 then\n puts 'No'\n else\n puts 'Yes'\n\n hash[ans_index ].each_with_index { |val, index| \n if index < k then \n print val\n print ' '\n end \n }\n \n #puts output;\n end\nend\n\nsolution"}, {"source_code": "def solution \n n, k, m = STDIN.gets.split.map(&:to_i)\n array = STDIN.gets.split.map(&:to_i)\n \n hash = []\n ans_index = -1;\n array.each{ |x| \n rem = x%m \n \n if hash[rem] == nil then\n hash[rem] = []\n end\n \n hash[rem].push(x)\n \n if hash[rem].length == k then \n ans_index = rem\n end\n \n }\n \n if ans_index == -1 then\n puts 'No'\n else\n puts 'Yes'\n # output = \"\"\n \n hash[ans_index ].each_with_index { |val, index| \n if index < k then \n # output += val.to_s + \" \"\n puts val\n end \n }\n \n #puts output;\n end\nend\n\nsolution"}], "negative_code": [{"source_code": "#! /usr/bin/env ruby\n\nn, k, m = gets.split.map &:to_i\na = gets.split.map &:to_i\na = a.group_by { |i| i % m }\nyes = false\na.each_key do |key|\n if a[key].size >= k\n yes = true\n puts:Yes\n puts a[key][0...k] * ' '\n end\nend\nif not yes\n puts:No\nend\n\n"}, {"source_code": "def solution \n n, k, m = STDIN.gets.split.map(&:to_i)\n array = STDIN.gets.split.map(&:to_i)\n \n hash = {}\n best = -1;\n array.each{ |x| \n rem = x%m \n if hash[rem] == nil then\n hash[rem] = []\n end\n \n hash[rem].push(x)\n if hash[rem].length == k then \n best = x%m \n end\n }\n \n if best == -1 then\n puts 'No'\n else\n puts 'yes'\n output = \"\";\n hash[best].each_with_index { |val, index| \n if index < k then \n output += val.to_s + \" \"\n end \n }\n puts output;\n end\nend\n\nsolution"}, {"source_code": "def solution \n n, k, m = gets.split.map(&:to_i)\n array = gets.split.map(&:to_i)\n \n hash = {}\n best = -1;\n array.each{ |x| \n rem = x%m \n if hash[rem] == nil then\n hash[rem] = []\n end\n \n hash[rem].push(x)\n if hash[rem].length == k then \n best = x%m \n end\n }\n \n if best == -1 then\n puts 'No'\n else\n puts 'yes'\n output = \"\";\n hash[best].each_with_index { |val, index| \n if index < k then \n output = output + val.to_s + \" \"\n end \n }\n puts output;\n end\nend\n\nsolution"}], "src_uid": "55bd1849ef13b52788a0b5685c4fcdac"} {"nl": {"description": "Genos needs your help. He was asked to solve the following programming problem by Saitama:The length of some string s is denoted |s|. The Hamming distance between two strings s and t of equal length is defined as , where si is the i-th character of s and ti is the i-th character of t. For example, the Hamming distance between string \"0011\" and string \"0110\" is |0 - 0| + |0 - 1| + |1 - 1| + |1 - 0| = 0 + 1 + 0 + 1 = 2.Given two binary strings a and b, find the sum of the Hamming distances between a and all contiguous substrings of b of length |a|.", "input_spec": "The first line of the input contains binary string a (1 ≤ |a| ≤ 200 000). The second line of the input contains binary string b (|a| ≤ |b| ≤ 200 000). Both strings are guaranteed to consist of characters '0' and '1' only.", "output_spec": "Print a single integer — the sum of Hamming distances between a and all contiguous substrings of b of length |a|.", "sample_inputs": ["01\n00111", "0011\n0110"], "sample_outputs": ["3", "2"], "notes": "NoteFor the first sample case, there are four contiguous substrings of b of length |a|: \"00\", \"01\", \"11\", and \"11\". The distance between \"01\" and \"00\" is |0 - 0| + |1 - 0| = 1. The distance between \"01\" and \"01\" is |0 - 0| + |1 - 1| = 0. The distance between \"01\" and \"11\" is |0 - 1| + |1 - 1| = 1. Last distance counts twice, as there are two occurrences of string \"11\". The sum of these edit distances is 1 + 0 + 1 + 1 = 3.The second sample case is described in the statement."}, "positive_code": [{"source_code": "a = gets.chomp\nb = gets.chomp\n\nhs = {}\nhs[-1] = 0\ncnt = 0\nfor i in 0...b.size\n cnt += 1 if b[i] == '1'\n hs[i] = cnt\nend\n\ndif = b.length - a.length\nans = 0\nfor i in 0...a.size\n if a[i] == '0'\n ans += hs[i + dif] - hs[i - 1]\n else\n ans += dif + 1 - hs[i + dif] + hs[i - 1]\n end\nend\n\nputs ans\n"}], "negative_code": [], "src_uid": "ed75bd272f6d3050426548435423ca92"} {"nl": {"description": "Lord Omkar has permitted you to enter the Holy Church of Omkar! To test your worthiness, Omkar gives you a password which you must interpret!A password is an array $$$a$$$ of $$$n$$$ positive integers. You apply the following operation to the array: pick any two adjacent numbers that are not equal to each other and replace them with their sum. Formally, choose an index $$$i$$$ such that $$$1 \\leq i < n$$$ and $$$a_{i} \\neq a_{i+1}$$$, delete both $$$a_i$$$ and $$$a_{i+1}$$$ from the array and put $$$a_{i}+a_{i+1}$$$ in their place. For example, for array $$$[7, 4, 3, 7]$$$ you can choose $$$i = 2$$$ and the array will become $$$[7, 4+3, 7] = [7, 7, 7]$$$. Note that in this array you can't apply this operation anymore.Notice that one operation will decrease the size of the password by $$$1$$$. What is the shortest possible length of the password after some number (possibly $$$0$$$) of operations?", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 100$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \\leq n \\leq 2 \\cdot 10^5$$$) — the length of the password. The second line of each test case contains $$$n$$$ integers $$$a_{1},a_{2},\\dots,a_{n}$$$ ($$$1 \\leq a_{i} \\leq 10^9$$$) — the initial contents of your password. The sum of $$$n$$$ over all test cases will not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each password, print one integer: the shortest possible length of the password after some number of operations.", "sample_inputs": ["2\n4\n2 1 3 1\n2\n420 420"], "sample_outputs": ["1\n2"], "notes": "NoteIn the first test case, you can do the following to achieve a length of $$$1$$$:Pick $$$i=2$$$ to get $$$[2, 4, 1]$$$Pick $$$i=1$$$ to get $$$[6, 1]$$$Pick $$$i=1$$$ to get $$$[7]$$$In the second test case, you can't perform any operations because there is no valid $$$i$$$ that satisfies the requirements mentioned above."}, "positive_code": [{"source_code": "# cook your code here\ntc = gets.chomp.to_i\ntc.times { \n n = gets.chomp.to_i\n \n arr = gets.chomp.split(' ').map(&:to_i)\n unique = 1\n index = 1\n while(index < n) do \n if arr[index] != arr[0]\n unique += 1\n break\n end\n index += 1\n end\n if arr.size() == 1 || unique >= 2\n puts 1\n else\n puts arr.size()\n end\n \n}"}, {"source_code": "# your code goes here\n\n# a = gets.chomp.to_i\n\n# b = gets.chomp.to_i\n\n# puts (a+b)\nt = gets.chomp.to_i\nfor tc in 1..t\n\tn = gets.chomp.to_i\n\ta = gets.split\n\tb = false\n\tfor i in 1..(n-1)\n\t\tif a[i] != a[i-1]\n\t\t\tb = true;\n\t\tend\n\tend\n\tif b\n\t\tputs 1\n\telse\n\t\tputs n\n\tend\nend"}], "negative_code": [], "src_uid": "7b80d3f3cd4f93e4277c76c76dc41f42"} {"nl": {"description": "This is an easier version of the problem. In this version $$$n \\le 1000$$$The outskirts of the capital are being actively built up in Berland. The company \"Kernel Panic\" manages the construction of a residential complex of skyscrapers in New Berlskva. All skyscrapers are built along the highway. It is known that the company has already bought $$$n$$$ plots along the highway and is preparing to build $$$n$$$ skyscrapers, one skyscraper per plot.Architects must consider several requirements when planning a skyscraper. Firstly, since the land on each plot has different properties, each skyscraper has a limit on the largest number of floors it can have. Secondly, according to the design code of the city, it is unacceptable for a skyscraper to simultaneously have higher skyscrapers both to the left and to the right of it.Formally, let's number the plots from $$$1$$$ to $$$n$$$. Then if the skyscraper on the $$$i$$$-th plot has $$$a_i$$$ floors, it must hold that $$$a_i$$$ is at most $$$m_i$$$ ($$$1 \\le a_i \\le m_i$$$). Also there mustn't be integers $$$j$$$ and $$$k$$$ such that $$$j < i < k$$$ and $$$a_j > a_i < a_k$$$. Plots $$$j$$$ and $$$k$$$ are not required to be adjacent to $$$i$$$.The company wants the total number of floors in the built skyscrapers to be as large as possible. Help it to choose the number of floors for each skyscraper in an optimal way, i.e. in such a way that all requirements are fulfilled, and among all such construction plans choose any plan with the maximum possible total number of floors.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 1000$$$) — the number of plots. The second line contains the integers $$$m_1, m_2, \\ldots, m_n$$$ ($$$1 \\leq m_i \\leq 10^9$$$) — the limit on the number of floors for every possible number of floors for a skyscraper on each plot.", "output_spec": "Print $$$n$$$ integers $$$a_i$$$ — the number of floors in the plan for each skyscraper, such that all requirements are met, and the total number of floors in all skyscrapers is the maximum possible. If there are multiple answers possible, print any of them.", "sample_inputs": ["5\n1 2 3 2 1", "3\n10 6 8"], "sample_outputs": ["1 2 3 2 1", "10 6 6"], "notes": "NoteIn the first example, you can build all skyscrapers with the highest possible height.In the second test example, you cannot give the maximum height to all skyscrapers as this violates the design code restriction. The answer $$$[10, 6, 6]$$$ is optimal. Note that the answer of $$$[6, 6, 8]$$$ also satisfies all restrictions, but is not optimal."}, "positive_code": [{"source_code": "N = gets.to_i\nM = gets.split.map(&:to_i)\n\ndef rebuild(mid)\n height = 0\n\n rh = lh = M[mid]\n\n mid.downto(0) do |i|\n if M[i] > lh\n height += M[i] - lh\n else\n lh = M[i]\n end\n end\n\n mid.upto(N - 1) do |i|\n if M[i] > rh\n height += M[i] - rh\n else\n rh = M[i]\n end\n end\n\n height\nend\n\ndef construct(mid)\n temp = M.dup\n\n rh = lh = M[mid]\n\n mid.downto(0) do |i|\n if M[i] > lh\n temp[i] = lh\n end\n\n lh = temp[i]\n end\n\n mid.upto(N - 1) do |i|\n if M[i] > rh\n temp[i] = rh\n end\n\n rh = temp[i]\n end\n\n temp\nend\n\nsum = M.inject(:+)\nidx = (0...N).max_by { |m| sum - rebuild(m) }\n\nputs construct(idx).join(' ')\n"}], "negative_code": [{"source_code": "N = gets.to_i\nM = gets.split.map(&:to_i)\n\ndef rebuild(mid)\n height = 0\n\n rh = lh = M[mid]\n\n mid.downto(0) do |i|\n if M[i] > lh\n height += M[i] - lh\n end\n\n lh = M[i]\n end\n\n mid.upto(N - 1) do |i|\n if M[i] > rh\n height += M[i] - rh\n end\n\n rh = M[i]\n end\n\n height\nend\n\ndef construct(mid)\n temp = M.dup\n\n rh = lh = M[mid]\n\n mid.downto(0) do |i|\n if M[i] > lh\n temp[i] = lh\n end\n\n lh = M[i]\n end\n\n mid.upto(N - 1) do |i|\n if M[i] > rh\n temp[i] = rh\n end\n\n rh = M[i]\n end\n\n temp\nend\n\nsum = M.inject(:+)\nidx = (0...N).max_by { |m| sum - rebuild(m) }\n\nputs construct(idx).join(' ')\n"}, {"source_code": "N = gets.to_i\nM = gets.split.map(&:to_i)\n\ndef rebuild(mid)\n height = 0\n\n rh = lh = M[mid]\n\n mid.downto(0) do |i|\n if M[i] > lh\n height += M[i] - lh\n end\n\n lh = M[i]\n end\n\n mid.upto(N - 1) do |i|\n if M[i] > rh\n height += M[i] - rh\n end\n\n rh = M[i]\n end\n\n height\nend\n\ndef construct(mid)\n temp = M.dup\n\n rh = lh = M[mid]\n\n mid.downto(0) do |i|\n if M[i] > lh\n temp[i] = lh\n end\n\n lh = temp[i]\n end\n\n mid.upto(N - 1) do |i|\n if M[i] > rh\n temp[i] = rh\n end\n\n rh = temp[i]\n end\n\n temp\nend\n\nsum = M.inject(:+)\nidx = (0...N).max_by { |m| sum - rebuild(m) }\n\nputs construct(idx).join(' ')\n"}], "src_uid": "88e6cd9ef45b956be8bee5d4fedd5725"} {"nl": {"description": "Little Vasya has received a young builder’s kit. The kit consists of several wooden bars, the lengths of all of them are known. The bars can be put one on the top of the other if their lengths are the same.Vasya wants to construct the minimal number of towers from the bars. Help Vasya to use the bars in the best way possible.", "input_spec": "The first line contains an integer N (1 ≤ N ≤ 1000) — the number of bars at Vasya’s disposal. The second line contains N space-separated integers li — the lengths of the bars. All the lengths are natural numbers not exceeding 1000.", "output_spec": "In one line output two numbers — the height of the largest tower and their total number. Remember that Vasya should use all the bars.", "sample_inputs": ["3\n1 2 3", "4\n6 5 6 7"], "sample_outputs": ["1 3", "2 3"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\nhs = Hash.new 0\ngets.split(' ').map(&:to_i).each{|x| hs[x]+=1}\nputs \"#{hs.max_by(&:last).last} #{hs.length}\""}, {"source_code": "n = gets.to_i\n\nbr = gets.split.map{|i| i.to_i}\n\nkol = br.uniq.size\n\nh = []\nbr.size.times do |i|\n h << br.reject{|it| it != br[i]}.size\nend\n\nh = h.sort\n\nputs h[-1].to_s + \" \" + kol.to_s"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split(/ /).map!{|x|x.to_i}\nh = Hash.new\nfor i in 0..n-1\n\tif h.key?(a[i])\n\t\th[a[i]] += 1 \n\telse\n\t\th[a[i]] = 1 \n\tend\nend\nha = h.to_a.sort{|a, b|\n b[1] <=> a[1] \n}\nputs ha[0][1].to_s + \" \" + h.length.to_s"}, {"source_code": "gets\nv=gets.split\nprint v.map{|x|v.count x}.max,\" \",v.uniq.size\n\n"}, {"source_code": "#!/usr/bin/ruby\ngets;\na=gets.split.map(&:to_i).sort_by{|e|e}.chunk{|e|e}.to_a\nputs \"#{a.map{|k,v|v.size}.max} #{a.size}\""}, {"source_code": "n = gets.chomp.to_i\na = Array.new(1001).map {|x| x=0}\ngets.chomp.split(\" \").map {|x| x.to_i}.each { |x|\n a[x]+=1\n}\nputs a.sort[a.length-1].to_s << \" \" << a.count{|x| x!=0}.to_s\n"}, {"source_code": "gets\na=gets.split\nprint a.map{|b|a.count b}.max,\" \",a.uniq.size\n"}, {"source_code": "n = gets.to_i\nl = gets.split(\" \").map(&:to_i).sort\ncnt = 1\nmax = 0\ncol = 0\nlast = l[0]\nl.each do |el|\n if last != el\n cnt += 1\n last = el\n max = col if max < col\n col = 1\n else\n col += 1\n end\nend\n\nmax = col if max < col\n\nputs \"#{max} #{cnt}\""}, {"source_code": "gets\na=gets.split\nprint a.map{|b|a.count b}.max,\" \",a.uniq.size\n"}, {"source_code": "gets\na=gets.split\nprint a.map{|b|a.count b}.max,\" \",a.uniq.size"}, {"source_code": "n=gets.chomp.strip.to_i\np=gets.chomp.split(' ').map(&:to_i)\na=Array.new(1001,0)\nsum=0\nmax=0\np.each do |x|\n\tif a[x]==0\n\t\tsum+=1\n\tend\n\ta[x]+=1\n\tif a[x]>max\n\t\tmax=a[x]\n\tend\nend\nputs max.to_s + ' ' + sum.to_s"}, {"source_code": "gets\na = gets.split.map(&:to_i).to_a\nputs [a.map { |x| a.find_all {|y| x==y}.size }.max, a.uniq.size]*' '"}, {"source_code": "a = gets\na = gets.chomp.split(' ')\n\nr = {}\nmax = 0\na.each do |x|\ny = x.to_sym\n\nr[y] = (r[y]||0) + 1\nmax = r[y] if max < r[y]\nend\n\nputs \"#{max} #{r.size}\""}, {"source_code": "n = gets.to_i\nl = gets.split.map(&:to_i).sort\nfox = Array.new()\n\ncounter = 1\nfor i in 1..n\n if l[i]==l[i-1]\n counter+=1\n else\n fox.concat([counter])\n counter = 1\n end\nend\n\nputs fox.max.to_s + \" \" + l.uniq.size.to_s"}, {"source_code": "gets\narr = gets.split.map { |x| x.to_i }.sort\nprint arr.count(arr.max_by { |x| arr.count(x) }), \" \",arr.uniq.size\n"}, {"source_code": "gets\na=gets.split\nprint a.map{|b|a.count b}.max,\" \",a.uniq.size\n"}, {"source_code": "num=gets.chomp.to_i\nmax=0\nnum2=num\nb=gets.chomp.split(\" \")\n1.upto(1000) do |i|\ntemp=b.count(\"#{i}\")\nnum2=num2-temp\nif temp>max\nmax= temp\nend\nif temp>1\nnum=num-temp+1\nend\nend\nprintf \"#{max} \"\nprintf \"#{num}\""}, {"source_code": "gets\na=gets.split\nprint a.map{|b|a.count b}.max,\" \",a.uniq.size\n"}, {"source_code": "num=gets.chomp.to_i\nmax=0\nnum2=num\nb=gets.chomp.split(\" \")\n1.upto(1000) do |i|\ntemp=b.count(\"#{i}\")\nnum2=num2-temp\nif temp>max\nmax= temp\nend\nif temp>1\nnum=num-temp+1\nend\nend\nprintf \"#{max} \"\nprintf \"#{num}\""}, {"source_code": "require 'set'\ngets\nx=gets.split.map{|e| e.to_i}\nd=Set.new(x).size\nh=Hash.new(0)\nans=0\nx.each{|e|\n\th[e]+=1\n\tans=h[e] if ansc)? b: c))||(x,c=i,1)}\nputs b.to_s+\" \"+a.uniq.size.to_s"}, {"source_code": "gets\ndata=gets.split.map(&:to_i).sort\n\nhs=Hash.new(0)\n\nmax_height = 0\ndata.each{|val| \n\ths[val]+=1\n\t\n\tmax_height = [max_height, hs[val]].max\n}\n\nputs \"#{max_height} #{hs.size}\"\n"}, {"source_code": "gets\ndata=gets.split.map(&:to_i).sort\n\nhs={}\nhs.default = 0\n\nmax_height = 0\ndata.each{|val| \n\ths[val]+=1\n\t\n\tmax_height = [max_height, hs[val]].max\n}\n\nputs \"#{max_height} #{hs.size}\"\n"}, {"source_code": "gets\nq = Hash.new\ngets.split.map do |e|\n e.to_i\n if q[e].nil?\n q[e] = 1\n else\n q[e] += 1\n end\nend\n\nputs q.values.max\n\nputs q.size\n"}, {"source_code": "n=gets; m=Hash.new(0)\ngets.chomp.split.each { |i| m[i] += 1 }\nputs m.max_by{|k,v| v}[1]; print m.length; print ' '"}, {"source_code": "n=gets.chomp.to_i\ns=gets.chomp.to_s\ns=s.split(\" \")\nu=s.uniq\nmax=1\nfor i in u\n\tif (s.count(i)>max)\n\t\tmax=s.count(i)\n\tend\nend\nprint(\"#{max} #{u.size}\\n\")\n"}, {"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\n\nn = gets.to_i\na = gets.split.map(&:to_i) + [0] # 0 for terminator\nprev = 0\ntowers = 0\nhighest_tower = 0\nheight = 0\na.sort.each { |i|\n if i != prev\n if i != 0\n towers += 1\n height = 1\n prev = i\n end\n else\n height += 1\n if height > highest_tower\n highest_tower = height\n end\n end\n}\n\nputs \"#{highest_tower} #{towers}\""}, {"source_code": "gets\nq = Hash.new\ngets.split.map do |e|\n e.to_i\n if q[e].nil?\n q[e] = 1\n else\n q[e] += 1\n end\nend\n\nputs q.values.max\n\nputs q.size\n "}, {"source_code": "n = gets.to_i\na = gets.split(' ')[0...n].map { |s| s.to_i }\nd = a.inject(Hash.new(0)) { |h, x| h[x] += 1; h }\ncnt = d.size\nmax = d.map { |k, v| v }.max\nputs \"#{max} #{cnt}\""}, {"source_code": "gets\na=gets.split\nprint a.map{|b|a.count b}.max,\" \",a.uniq.size\n"}, {"source_code": "gets\nmas = gets.split(\" \")\nputs mas.map { |s| mas.count s }.max.to_s+\" \"+mas.uniq.size.to_s"}, {"source_code": "# http://codeforces.com/contest/37/problem/A\n\nnumber = gets.chomp.to_i\n\nbars = gets.chomp.split(\" \").each {|str| [str.to_i]}\n\ntowers = []\n\nbars.each {|bar|\n if index = towers.index {|item| item[0] == bar}\n towers[index] << bar\n else\n towers << [bar]\n end\n}\n\ntowers.sort! {|a, b| a.length <=> b.length}.reverse!\n\nputs \"#{towers[0].length} #{towers.length}\"\n"}, {"source_code": "gets\nblocks = gets.chomp.split.map(&:to_i)\n\nres = {}\nblocks.each do |b|\n res[b] ||= 0\n res[b] += 1\nend\n\nputs \"#{res.values.max} #{blocks.uniq.size}\" "}, {"source_code": "n = gets.to_i\nm = gets.split\nm = m.uniq.collect{|i| [i, m.find_all{|j| j == i}.size]}\nprint(m.max{|x, y| x[1] <=> y[1]}[1].to_s + \" \" + m.size.to_s)\n"}, {"source_code": "num=gets.chomp.to_i\nmax=0\nnum2=num\nb=gets.chomp.split(\" \")\n1.upto(1000) do |i|\ntemp=b.count(\"#{i}\")\nnum2=num2-temp\nif temp>max\nmax= temp\nend\nif temp>1\nnum=num-temp+1\nend\nend\nprintf \"#{max} \"\nprintf \"#{num}\""}, {"source_code": "class Euler\n mas=Array.new(size=0, obj=nil);\n n=gets.to_i;\n st=gets.to_s.split(\" \");\n st.sort!;\n mx=1;\n for i in 0..st.length-1\n k=1;\n for j in i+1..st.length-1\n if (st[j]==st[i])\n k+=1;\n end\n end\n if (k>mx)\n mx=k;\n end\n end\n a=st.uniq;\n print mx.to_s+\" \"+a.length.to_s+\"\\n\";\nend\n"}, {"source_code": "gets\na=gets.split\nprint a.map{|b|a.count b}.max,\" \",a.uniq.size\n"}, {"source_code": "gets\nq = Hash.new\ngets.split.map do |e|\n e.to_i\n if q[e].nil?\n q[e] = 1\n else\n q[e] += 1\n end\nend\n\nputs q.values.max\n\nputs q.size\n "}, {"source_code": "gets\nq = Hash.new\ngets.split.map do |e|\n e.to_i\n if q[e].nil?\n q[e] = 1\n else\n q[e] += 1\n end\nend\n\nputs q.values.max\n\nputs q.size\n "}, {"source_code": "gets\na=gets.split\nprint a.map{|b|a.count b}.max,\" \",a.uniq.size\n"}, {"source_code": "gets\na=gets.split\nprint a.map{|b|a.count b}.max,\" \",a.uniq.size"}, {"source_code": "gets\nbars, towers = gets.split.map(&:to_i), Hash.new(0)\nbars.each {|bar| towers[bar] += 1}\nprint towers.values.max, \" \", towers.length"}, {"source_code": "gets\na=gets.split\nprint a.map{|b|a.count b}.max,\" \",a.uniq.size\n"}, {"source_code": "barNum = gets.chomp.to_i\nbar = gets.chomp.split(' ').map {|item| item.to_i}\nbar.sort!\ntotal = bar.uniq.length\nlargest = 0\nbar.each do |item|\n\tlargest = bar.count(item) if largest < bar.count(item)\nend\nputs \"#{largest} #{total}\""}], "negative_code": [{"source_code": "n = gets.to_i\nl = gets.split(\" \").map(&:to_i).sort\ncnt = 1\nmax = 0\ncol = 0\nlast = l[0]\nl.each do |el|\n if last != el\n cnt += 1\n last = el\n max = col if max < col\n col = 1\n else\n col += 1\n end\nend\n\nputs \"#{max} #{cnt}\""}, {"source_code": "a = gets\na = gets.chomp.split(' ')\n\nr = {}\nmax = 0\na.each do |x|\ny = x.to_sym\nr[y] = r[y]||0 + 1\nmax = r[y] if max < r[y]\nend\n\nputs \"#{max} #{r.size}\""}, {"source_code": "n = gets.to_i\nl = gets.split.map(&:to_i).sort\nfox = Array.new()\n\ncounter = 1\nfor i in 1..n-1\n if l[i]==l[i-1]\n counter+=1\n else\n fox.concat([counter])\n counter = 1\n end\nend\n\nputs fox.max.to_s + \" \" + l.uniq.size.to_s"}, {"source_code": "n=gets; m=Hash.new(0)\ngets.chomp.split.each { |i| m[i] += 1 }\nprint m.length; print ' '; puts m.max_by{|k,v| v}[1]"}, {"source_code": "a=gets.chomp.to_i\nb=gets.chomp.split(\" \")\nsum=0\n0.upto(a-1) do |i|\n b[i]= b[i].to_i\nsum=sum+b[i]\nend\nr=0\nif sum%2==0\n r=1\nend \n \nsum=(sum/2.0).ceil\n\nb.sort!\nb.reverse!\np=0\n0.upto(a-1) do |i|\np=p+b[i].to_i\n\nif ( p>=sum && r==0)\np=i+1\nbreak\nend\nif (p>sum && r==1)\np=i+1\nbreak\nend\nend\n\nputs \"#{p}\""}], "src_uid": "9a92221c760a3b6a1e9318f948fe0473"} {"nl": {"description": "Vasya thinks that lucky tickets are the tickets whose numbers are divisible by 3. He gathered quite a large collection of such tickets but one day his younger brother Leonid was having a sulk and decided to destroy the collection. First he tore every ticket exactly in two, but he didn’t think it was enough and Leonid also threw part of the pieces away. Having seen this, Vasya got terrified but still tried to restore the collection. He chose several piece pairs and glued each pair together so that each pair formed a lucky ticket. The rest of the pieces Vasya threw away reluctantly. Thus, after the gluing of the 2t pieces he ended up with t tickets, each of which was lucky.When Leonid tore the tickets in two pieces, one piece contained the first several letters of his number and the second piece contained the rest.Vasya can glue every pair of pieces in any way he likes, but it is important that he gets a lucky ticket in the end. For example, pieces 123 and 99 can be glued in two ways: 12399 and 99123.What maximum number of tickets could Vasya get after that?", "input_spec": "The first line contains integer n (1 ≤ n ≤ 104) — the number of pieces. The second line contains n space-separated numbers ai (1 ≤ ai ≤ 108) — the numbers on the pieces. Vasya can only glue the pieces in pairs. Even if the number of a piece is already lucky, Vasya should glue the piece with some other one for it to count as lucky. Vasya does not have to use all the pieces. The numbers on the pieces an on the resulting tickets may coincide.", "output_spec": "Print the single number — the maximum number of lucky tickets that will be able to be restored. Don't forget that every lucky ticket is made of exactly two pieces glued together.", "sample_inputs": ["3\n123 123 99", "6\n1 1 1 23 10 3"], "sample_outputs": ["1", "1"], "notes": null}, "positive_code": [{"source_code": "gets\nd=[0,0,0]\ngets.split(/\\s/).each{|i|\n\td[i.to_i%3]=d[i.to_i%3]+1\n}\nprint (d[0]/2)+(d[1]x{a.count{|i|i%3==x}}\np g[0]/2+[g[1],g[2]].min"}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\ndef calc(x)\n a=0\n while x>0\n a+=x%10\n x/=10\n end\n a%=3\nend\ncnt=[0,0,0]\na.each{|i|\n cnt[calc(i)]+=1\n}\np cnt[0]/2+[cnt[1],cnt[2]].min\n"}, {"source_code": "gets\na=[0]*3\ngets.split.map{|b|a[b.sum%3]+=1}\np a[0]/2+a[1..2].min\n"}, {"source_code": "gets\na=[0]*3\ngets.split.map{|b|a[b.sum%3]+=1}\np a[0]/2+a[1..2].min\n"}, {"source_code": "gets\na=[0]*3\ngets.split.map{|b|a[b.sum%3]+=1}\np a[0]/2+a[1..2].min\n"}, {"source_code": "gets\na=[0]*3\ngets.split.map{|b|a[b.sum%3]+=1}\np a[0]/2+a[1..2].min\n"}, {"source_code": "gets\na=[0]*3\ngets.split.map{|b|a[b.sum%3]+=1}\np a[0]/2+a[1..2].min"}, {"source_code": "gets\nc = Hash.new(0).tap { |h| gets.split.map(&:to_i).each { |v| h[v % 3] += 1 } }\np c[0] / 2 + [c[1], c[2]].min\n"}, {"source_code": "N = gets.chomp.to_i\nc = Hash.new(0).tap { |h| gets.split.map(&:to_i).each { |v| h[v % 3] += 1 } }\np c[0] / 2 + [c[1], c[2]].min\n"}, {"source_code": "gets\na=[0]*3\ngets.split.map{|b|a[b.sum%3]+=1}\np a[0]/2+a[1..2].min\n"}, {"source_code": "gets\na=[0]*3\ngets.split.map{|b|a[b.sum%3]+=1}\np a[0]/2+a[1..2].min\n"}, {"source_code": "gets\na=[0]*3\ngets.split.map{|b|a[b.sum%3]+=1}\np a[0]/2+a[1..2].min\n"}, {"source_code": "def min(a, b)\n a < b ? a : b\nend\n\ndef digits_sum(number)\n number.to_s.chars.map { |v| v.to_i }.inject { |r, v| r + v }\nend\n\nn = gets.to_i\nparts = gets.split.map { |v| digits_sum(v) % 3 }\n\ncount = [0, 0, 0]\n\nparts.each { |p| count[p] += 1 }\n\nputs count[0] / 2 + min(count[1], count[2])\n"}, {"source_code": "gets\na=[0]*3\ngets.split.map{|b|a[b.sum%3]+=1}\np a[0]/2+a[1..2].min"}, {"source_code": "gets\na=[0]*3\ngets.split.map{|b|a[b.sum%3]+=1}\np a[0]/2+a[1..2].min\n"}], "negative_code": [{"source_code": "gets\nd=[0,0,0]\ngets.split(/\\s/).each{|i|\n\td[i.to_i%3]=d[i.to_i%3]+1\n\tprint i,\"\\n\"\n}\nprint (d[0]/2)+(d[1] max_r || (r == max_r && c < cost_r)\n max_r = r\n cost_r = c\n end\n\n len = r - l + 1\n if !longest_len || len > longest_len || (len == longest_len && c < longest_cost)\n longest_len = len\n longest_cost = c\n end\n\n answer = cost_l + cost_r\n if (max_r - min_l + 1) == longest_len && longest_cost < answer\n answer = longest_cost\n end\n\n puts answer\n end\nend\n"}], "negative_code": [{"source_code": "#!/usr/bin/env ruby\n\nalias gets1 gets\ndef gets\n gets1.rstrip\nend\n\nSeg = Struct.new(:l, :r, :c)\n\ngets.to_i.times do\n n = gets.to_i\n segs = Array.new(n) {\n Seg.new *gets.split.map(&:to_i)\n }\n\n ml = nil; mr = nil\n segs.each { |seg|\n ml = seg if !ml || seg.l < ml.l || (seg.l == ml.l && seg.c < ml.c)\n mr = seg if !mr || seg.r > mr.r || (seg.r == mr.r && seg.c < mr.c)\n if ml.l == mr.l && ml.r == mr.r\n ml = mr = ml.c < mr.c ? ml : mr\n end\n ml = mr if mr.l == ml.l\n mr = ml if ml.r == mr.r\n cost = ml.c\n cost += mr.c unless mr == ml\n cost = seg.c if seg.l == ml.l && seg.r == mr.r && seg.c < cost\n puts cost\n }\nend\n"}, {"source_code": "#!/usr/bin/env ruby\n\nalias gets1 gets\ndef gets\n gets1.rstrip\nend\n\nSeg = Struct.new(:l, :r, :c)\n\ngets.to_i.times do\n n = gets.to_i\n segs = Array.new(n) {\n Seg.new *gets.split.map(&:to_i)\n }\n\n ml = nil; mr = nil\n segs.each { |seg|\n ml = seg if !ml || seg.l < ml.l || (seg.l == ml.l && seg.c < ml.c)\n mr = seg if !mr || seg.r > mr.r || (seg.r == mr.r && seg.c < mr.c)\n if ml.l == mr.l && ml.r == mr.r\n ml = mr = ml.c < mr.c ? ml : mr\n end\n ml = mr if mr.l == ml.l\n mr = ml if ml.r == mr.r\n cost = ml.c\n cost += mr.c unless mr == ml\n const = seg.c if seg.l == ml.l && seg.r == mr.r && seg.c < cost\n puts cost\n }\nend\n"}, {"source_code": "#!/usr/bin/env ruby\n\nalias gets1 gets\ndef gets\n gets1.rstrip\nend\n\nSeg = Struct.new(:l, :r, :c)\n\ngets.to_i.times do\n n = gets.to_i\n segs = Array.new(n) {\n Seg.new *gets.split.map(&:to_i)\n }\n\n ml = nil; mr = nil\n segs.each { |seg|\n ml = seg if !ml || seg.l < ml.l || (seg.l == ml.l && seg.c < ml.c)\n mr = seg if !mr || seg.r > mr.r || (seg.r == mr.r && seg.c < mr.c)\n if ml.l == mr.l && ml.r == mr.r\n ex = ml.c < mr.c ? ml : mr\n ml = mr = seg.c < ex.c ? seg : ex\n end\n ml = mr if mr.l == ml.l\n mr = ml if ml.r == mr.r\n cost = ml.c\n cost += mr.c unless mr == ml\n puts cost\n }\nend\n"}, {"source_code": "#!/usr/bin/env ruby\n\nalias gets1 gets\ndef gets\n gets1.rstrip\nend\n\nSeg = Struct.new(:l, :r, :c)\n\ngets.to_i.times do\n n = gets.to_i\n segs = Array.new(n) {\n Seg.new *gets.split.map(&:to_i)\n }\n\n ml = nil; mr = nil\n segs.each { |seg|\n ml = seg if !ml || seg.l < ml.l || (seg.l == ml.l && seg.c < ml.c)\n mr = seg if !mr || seg.r > mr.r || (seg.r == mr.r && seg.c < mr.c)\n if ml.l == mr.l && ml.r == mr.r\n ml = mr = ml.c < mr.c ? ml : mr\n end\n ml = mr if mr.l == ml.l\n mr = ml if ml.r == mr.r\n cost = ml.c\n cost += mr.c unless mr == ml\n puts cost\n }\nend\n"}, {"source_code": "#!/usr/bin/env ruby\n\nalias gets1 gets\ndef gets\n gets1.rstrip\nend\n\nSeg = Struct.new(:l, :r, :c)\n\ngets.to_i.times do\n n = gets.to_i\n segs = Array.new(n) {\n Seg.new *gets.split.map(&:to_i)\n }\n\n ml = nil; mr = nil\n segs.each { |seg|\n ml = seg if !ml || seg.l < ml.l || (seg.l == ml.l && seg.c < ml.c)\n mr = seg if !mr || seg.r > mr.r || (seg.r == mr.r && seg.c < mr.c)\n ml = mr if mr.l <= ml.l\n mr = ml if ml.r >= mr.r\n cost = ml.c\n cost += mr.c unless mr == ml\n puts cost\n }\nend\n"}, {"source_code": "#!/usr/bin/env ruby\n\nalias gets1 gets\ndef gets\n gets1.rstrip\nend\n\ngets.to_i.times do\n ml = nil\n mr = nil\n mlc = nil\n mrc = nil\n gets.to_i.times do\n l, r, c = gets.split.map(&:to_i)\n used = false\n if !ml || l < ml || (l == ml && c < mlc)\n ml = l\n mlc = c\n c = 0\n used = true\n mrc = 0 if r == mr\n end\n if !mr || r > mr || (r == mr && c < mrc)\n mr = r\n mrc = c\n c = 0\n mlc = 0 if !used && l == ml\n end\n puts(mlc + mrc)\n end\nend\n"}, {"source_code": "#!/usr/bin/env ruby\n\ngets.to_i.times do\n ml = nil\n mr = nil\n mlc = nil\n mrc = nil\n gets.to_i.times do\n l, r, c = gets.split.map(&:to_i)\n if !ml || l < ml || (l == ml && c < mlc)\n ml = l\n mlc = c\n c = 0\n mrc = 0 if r == mr\n end\n if !mr || r > mr || (r == mr && c < mrc)\n mr = r\n mrc = c\n c = 0\n mlc = 0 if l == ml\n end\n puts(mlc + mrc)\n end\nend\n"}], "src_uid": "ee773d908fc297cc692aaecf6af299c9"} {"nl": {"description": "A binary string is a string where each character is either 0 or 1. Two binary strings $$$a$$$ and $$$b$$$ of equal length are similar, if they have the same character in some position (there exists an integer $$$i$$$ such that $$$a_i = b_i$$$). For example: 10010 and 01111 are similar (they have the same character in position $$$4$$$); 10010 and 11111 are similar; 111 and 111 are similar; 0110 and 1001 are not similar. You are given an integer $$$n$$$ and a binary string $$$s$$$ consisting of $$$2n-1$$$ characters. Let's denote $$$s[l..r]$$$ as the contiguous substring of $$$s$$$ starting with $$$l$$$-th character and ending with $$$r$$$-th character (in other words, $$$s[l..r] = s_l s_{l + 1} s_{l + 2} \\dots s_r$$$).You have to construct a binary string $$$w$$$ of length $$$n$$$ which is similar to all of the following strings: $$$s[1..n]$$$, $$$s[2..n+1]$$$, $$$s[3..n+2]$$$, ..., $$$s[n..2n-1]$$$.", "input_spec": "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 a single integer $$$n$$$ ($$$1 \\le n \\le 50$$$). The second line of each test case contains the binary string $$$s$$$ of length $$$2n - 1$$$. Each character $$$s_i$$$ is either 0 or 1.", "output_spec": "For each test case, print the corresponding binary string $$$w$$$ of length $$$n$$$. If there are multiple such strings — print any of them. It can be shown that at least one string $$$w$$$ meeting the constraints always exists.", "sample_inputs": ["4\n1\n1\n3\n00000\n4\n1110000\n2\n101"], "sample_outputs": ["1\n000\n1010\n00"], "notes": "NoteThe explanation of the sample case (equal characters in equal positions are bold):The first test case: $$$\\mathbf{1}$$$ is similar to $$$s[1..1] = \\mathbf{1}$$$. The second test case: $$$\\mathbf{000}$$$ is similar to $$$s[1..3] = \\mathbf{000}$$$; $$$\\mathbf{000}$$$ is similar to $$$s[2..4] = \\mathbf{000}$$$; $$$\\mathbf{000}$$$ is similar to $$$s[3..5] = \\mathbf{000}$$$. The third test case: $$$\\mathbf{1}0\\mathbf{10}$$$ is similar to $$$s[1..4] = \\mathbf{1}1\\mathbf{10}$$$; $$$\\mathbf{1}01\\mathbf{0}$$$ is similar to $$$s[2..5] = \\mathbf{1}10\\mathbf{0}$$$; $$$\\mathbf{10}1\\mathbf{0}$$$ is similar to $$$s[3..6] = \\mathbf{10}0\\mathbf{0}$$$; $$$1\\mathbf{0}1\\mathbf{0}$$$ is similar to $$$s[4..7] = 0\\mathbf{0}0\\mathbf{0}$$$. The fourth test case: $$$0\\mathbf{0}$$$ is similar to $$$s[1..2] = 1\\mathbf{0}$$$; $$$\\mathbf{0}0$$$ is similar to $$$s[2..3] = \\mathbf{0}1$$$. "}, "positive_code": [{"source_code": "times = gets.to_i\n\ndef resolve(binary_s, w_size)\n w = \"\"\n (0..w_size-1).each do |i|\n if i == 0\n w = binary_s[i..w_size-1]\n else\n binary_s[i..w_size+(i-1)].each_char.with_index {|c,index| w[index]=\"1\" if c!=w[index]}\n end\n end\n w\nend\n\n(1..times).each do |i|\n w_size = gets.to_i\n s = gets.chomp\n puts resolve(s, w_size)\nend"}, {"source_code": "# cook your code here\nt = gets.chomp.to_i\nt.times {\n n = gets.chomp.to_i\n s = gets.chomp.split('')\n ans = ''\n i = 0\n j = 0\n while i < n do\n ans = ans + s[j].to_s\n j += 2\n i += 1\n end\n puts ans\n}"}, {"source_code": "t = gets.to_i\nans = []\nt.times do\n n = gets.to_i\n s = gets.chomp\n len = s.length\n arr = []\n (len-n+1).times do |i|\n arr << s[i..i+n-1]\n end\n bit = Array.new(n,\"-1\")\n n.times do |i|\n flg = true\n s = arr[0][i]\n (len-n+1).times do |j|\n if arr[j][i] != s\n flg = false\n end\n end\n if flg\n bit[i] = s\n else\n bit[i] = \"0\"\n end\n end\n str = \"\"\n n.times do |i|\n str = str+bit[i]\n end\n ans << str\nend\nans.each do |x|\n puts x\nend"}, {"source_code": "gets.to_i.times do\n gets.to_i\n s = gets.split('')\n a = []\n s.each_with_index { |v, i| a << v if i.even? }\n puts a.join\nend"}], "negative_code": [{"source_code": "gets.to_i.times do\n gets.to_i\n s = gets.split('')\n a = []\n s.each_with_index { |v, i| a << v if i.even? }\n a.join\nend\n"}], "src_uid": "b37bbf1fe9ac5144cfa230633ccbdd79"} {"nl": {"description": "On a number line there are n balls. At time moment 0 for each ball the following data is known: its coordinate xi, speed vi (possibly, negative) and weight mi. The radius of the balls can be ignored.The balls collide elastically, i.e. if two balls weighing m1 and m2 and with speeds v1 and v2 collide, their new speeds will be: .Your task is to find out, where each ball will be t seconds after.", "input_spec": "The first line contains two integers n and t (1 ≤ n ≤ 10, 0 ≤ t ≤ 100) — amount of balls and duration of the process. Then follow n lines, each containing three integers: xi, vi, mi (1 ≤ |vi|, mi ≤ 100, |xi| ≤ 100) — coordinate, speed and weight of the ball with index i at time moment 0. It is guaranteed that no two balls have the same coordinate initially. Also each collision will be a collision of not more than two balls (that is, three or more balls never collide at the same point in all times from segment [0;t]).", "output_spec": "Output n numbers — coordinates of the balls t seconds after. Output the numbers accurate to at least 4 digits after the decimal point.", "sample_inputs": ["2 9\n3 4 5\n0 7 8", "3 10\n1 2 3\n4 -5 6\n7 -8 9"], "sample_outputs": ["68.538461538\n44.538461538", "-93.666666667\n-74.666666667\n-15.666666667"], "notes": null}, "positive_code": [{"source_code": "n, t = gets.chomp.split.map { |e| e.to_i }\n\nballs = []\nfor i in 0...n\n\tballs <<= gets.chomp.split.map { |e| e.to_f }\nend\n\ndef calcu_v v0, m0, v1, m1\n\tnv0 = ((m0 - m1) * v0 + 2 * m1 * v1) / (m0 + m1)\n\tnv1 = ((m1 - m0) * v1 + 2 * m0 * v0) / (m0 + m1)\n\treturn nv0, nv1\nend\n\ndef calcu_p(x0, v0, x1, v1)\n\t(x1 * v0 - x0 * v1) / (v0 - v1)\nend\n\nflags = Array.new(n) { false }\nnow = 0\nwhile now < t\n\tnext_time = t - now\n\tnext_a = -1\n\tnext_b = -1\n\tfor i in 0...n\n\t\tfor j in 0...i\n\t\t\tif (balls[i][1] - balls[j][1]).abs > 0.00001\n\t\t\t\tcol_time = - (balls[i][0] - balls[j][0]) / (balls[i][1] - balls[j][1])\n\t\t\t\tif col_time > 0 && col_time < next_time\n\t\t\t\t\tnext_time = col_time\n\t\t\t\t\tnext_a = i\n\t\t\t\t\tnext_b = j\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\tend\n\t#if next_a != -1 and next_b != -1\n\t\t#pos = (balls[next_b][0] * balls[next_a][1] - balls[next_a][0] - balls[next_b][1]) / (balls[next_a][1] - balls[next_b][1])\n\t#\tpos = calcu_p(balls[next_a][0], balls[next_a][1], balls[next_b][0], balls[next_b][1])\n\t#\tballs[next_a][1], balls[next_b][1] = calcu_v(balls[next_a][1], balls[next_a][2], balls[next_b][1], balls[next_b][2])\n\t#\tballs[next_a][0] = pos\n\t#\tballs[next_b][0] = pos\n\t#end\n\n\tfor i in 0...n \n\t\tflags[i] = false\n\tend\n\tfor i in 0...n\n\t\tfor j in 0...i\n\t\t\tif !flags[i] && !flags[j] && (balls[i][1] - balls[j][1]).abs > 0.00001\n\t\t\t\tcol_time = - (balls[i][0] - balls[j][0]) / (balls[i][1] - balls[j][1])\n\t\t\t\tif (col_time - next_time).abs < 0.00001\n\t\t\t\t\tpos = calcu_p(balls[i][0], balls[i][1], balls[j][0], balls[j][1])\n\t\t\t\t\tballs[i][1], balls[j][1] = calcu_v(balls[i][1], balls[i][2], balls[j][1], balls[j][2])\n\t\t\t\t\tballs[i][0] = balls[j][0] = pos\n\t\t\t\t\tflags[i] = true\n\t\t\t\t\tflags[j] = true\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\tend\n\n\tfor i in 0...n\n\t\tif !flags[i]\n\t\t\tballs[i][0] += balls[i][1] * next_time\n\t\tend\n\tend\n\tnow += next_time\n\n\t# print balls.to_s, \"\\n\"\nend\n\nfor i in 0...n\n\tprint balls[i][0], \"\\n\"\nend\n\n\n"}, {"source_code": "input = gets.chomp.split\nn = Integer(input[0])\nt = Integer(input[1])\nballs = []\nfor i in 0...n\n balls[i] = []\n input1 = gets.chomp.split\n for j in 0...3\n balls[i][j] = Integer(input1[j])\n end\nend\n\nfor i in 0...n\n balls[i][3] = i\nend\nballs = balls.sort_by{|a| a[0]}\n\ndef balls_state(balls, first_collide_i, first_collide_time, n)\n for i in 0...n\n balls[i][0] = balls[i][1] * first_collide_time + balls[i][0]\n end\n for i in 0...first_collide_i.length\n j = first_collide_i[i]\n v_0 = ((balls[j][2] - balls[j+1][2]) * balls[j][1] + 2 * balls[j+1][2] * balls[j+1][1]) / ((balls[j][2] + balls[j+1][2]).to_f)\n v_1 = ((balls[j+1][2] - balls[j][2]) * balls[j+1][1] + 2 * balls[j][2] * balls[j][1]) / ((balls[j][2] + balls[j+1][2]).to_f)\n balls[j][1] = v_0\n balls[j+1][1] = v_1\n end\n return balls\nend\n\ndef next_collide(balls, n)\n collide_time = []\n s = 0\n v = 0\n ct = 0\n for i in 0...n-1\n s = balls[i+1][0] - balls[i][0]\n v = balls[i][1] - balls[i+1][1]\n if balls[i][1] < 0 && balls[i+1][1] < 0\n if balls[i][1] > balls[i+1][1]\n ct = s.to_f / v\n collide_time.push([i, ct])\n end \n elsif balls[i][1] > 0\n if balls[i+1][1] > 0 && balls[i][1] > balls[i+1][1]\n ct = s.to_f / v\n collide_time.push([i, ct])\n \n elsif balls[i+1][1] < 0\n ct = s.to_f / v\n collide_time.push([i, ct])\n end\n end\n end\n collide_time = collide_time.sort_by{|a| a[1]}\n if collide_time != []\n first_collide_i = []\n first_collide_time = collide_time[0][1]\n for i in 0...collide_time.length\n if collide_time[i][1] == first_collide_time\n first_collide_i.push(collide_time[i][0])\n end\n end\n else\n first_collide_time = 1000\n first_collide_i = []\n end\n return first_collide_time, first_collide_i\nend\n\nstop = false\nwhile !stop do\n first_collide_time, first_collide_i = next_collide(balls, n)\n if t <= first_collide_time\n for i in 0...n\n balls[i][0] += balls[i][1] * t\n end\n result = balls.sort_by{|a| a[3]}\n for i in 0...n\n puts result[i][0]\n end\n stop = true\n else\n t -= first_collide_time\n balls = balls_state(balls, first_collide_i, first_collide_time, n)\n end\nend\n\n\n\n\n"}], "negative_code": [{"source_code": "n, t = gets.chomp.split.map { |e| e.to_i }\n\nballs = []\nfor i in 0...n\n\tballs <<= gets.chomp.split.map { |e| e.to_f }\nend\n\ndef calcu_v v0, m0, v1, m1\n\tnv0 = ((m0 - m1) * v0 + 2 * m1 * v1) / (m0 + m1)\n\tnv1 = ((m1 - m0) * v1 + 2 * m0 * v0) / (m0 + m1)\n\treturn nv0, nv1\nend\n\ndef calcu_p(x0, v0, x1, v1)\n\t(x1 * v0 - x0 * v1) / (v0 - v1)\nend\n\nnow = 0\nwhile now < t\n\tnext_time = t - now\n\tnext_a = -1\n\tnext_b = -1\n\tfor i in 0...n\n\t\tfor j in 0...i\n\t\t\tif (balls[i][1] - balls[j][1]).abs > 0.00001\n\t\t\t\tcol_time = - (balls[i][0] - balls[j][0]) / (balls[i][1] - balls[j][1])\n\t\t\t\tif col_time > 0 && col_time < next_time\n\t\t\t\t\tnext_time = col_time\n\t\t\t\t\tnext_a = i\n\t\t\t\t\tnext_b = j\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\tend\n\tif next_a != -1 and next_b != -1\n\t\t#pos = (balls[next_b][0] * balls[next_a][1] - balls[next_a][0] - balls[next_b][1]) / (balls[next_a][1] - balls[next_b][1])\n\t\tpos = calcu_p(balls[next_a][0], balls[next_a][1], balls[next_b][0], balls[next_b][1])\n\t\tballs[next_a][1], balls[next_b][1] = calcu_v(balls[next_a][1], balls[next_a][2], balls[next_b][1], balls[next_b][2])\n\t\tballs[next_a][0] = pos\n\t\tballs[next_b][0] = pos\n\tend\n\n\tnow += next_time\n\tfor i in 0...n\n\t\tif i != next_a && i != next_b\n\t\t\tballs[i][0] += balls[i][1] * next_time\n\t\tend\n\tend\n\t# print balls.to_s, \"\\n\"\nend\n\nfor i in 0...n\n\tprint balls[i][0], \"\\n\"\nend\n\n\n"}, {"source_code": "n, t = gets.chomp.split.map { |e| e.to_i }\n\nballs = []\nfor i in 0...n\n\tballs <<= gets.chomp.split.map { |e| e.to_f }\nend\n\ndef calcu_v v0, m0, v1, m1\n\tnv0 = ((m0 - m1) * v0 + 2 * m1 * v1) / (m0 + m1)\n\tnv1 = ((m1 - m0) * v1 + 2 * m0 * v0) / (m0 + m1)\n\treturn nv0, nv1\nend\n\ndef calcu_p(x0, v0, x1, v1)\n\t(x1 * v0 - x0 * v1) / (v0 - v1)\nend\n\nflags = Array.new(n) { false }\nnow = 0\nwhile now < t\n\tnext_time = t - now\n\tnext_a = -1\n\tnext_b = -1\n\tfor i in 0...n\n\t\tfor j in 0...i\n\t\t\tif (balls[i][1] - balls[j][1]).abs > 0.00001\n\t\t\t\tcol_time = - (balls[i][0] - balls[j][0]) / (balls[i][1] - balls[j][1])\n\t\t\t\tif col_time > 0 && col_time < next_time\n\t\t\t\t\tnext_time = col_time\n\t\t\t\t\tnext_a = i\n\t\t\t\t\tnext_b = j\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\tend\n\t#if next_a != -1 and next_b != -1\n\t\t#pos = (balls[next_b][0] * balls[next_a][1] - balls[next_a][0] - balls[next_b][1]) / (balls[next_a][1] - balls[next_b][1])\n\t#\tpos = calcu_p(balls[next_a][0], balls[next_a][1], balls[next_b][0], balls[next_b][1])\n\t#\tballs[next_a][1], balls[next_b][1] = calcu_v(balls[next_a][1], balls[next_a][2], balls[next_b][1], balls[next_b][2])\n\t#\tballs[next_a][0] = pos\n\t#\tballs[next_b][0] = pos\n\t#end\n\n\tfor i in 0...n \n\t\tflags[i] = false\n\tend\n\tfor i in 0...n\n\t\tfor j in 0...i\n\t\t\tif !flags[i] && !flags[j] && (balls[i][1] - balls[j][1]).abs > 0.00001\n\t\t\t\tcol_time = - (balls[i][0] - balls[j][0]) / (balls[i][1] - balls[j][1])\n\t\t\t\tif (col_time - next_time).abs < 0.00001\n\t\t\t\t\tpos = calcu_p(balls[i][0], balls[i][1], balls[j][0], balls[j][1])\n\t\t\t\t\tballs[i][1], balls[j][1] = calcu_v(balls[i][1], balls[i][2], balls[j][1], balls[j][2])\n\t\t\t\t\tballs[i][0] = balls[j][0] = pos\n\t\t\t\t\tflags[i] = true\n\t\t\t\t\tflags[j] = true\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\tend\n\n\tfor i in 0...n\n\t\tif !flags[i]\n\t\t\tballs[i][0] += balls[i][1] * next_time\n\t\tend\n\tend\n\tnow += next_time\n\n\tprint balls.to_s, \"\\n\"\nend\n\nfor i in 0...n\n\tprint balls[i][0], \"\\n\"\nend\n\n\n"}], "src_uid": "2432a746446990ecc2926bf00807a5ee"} {"nl": {"description": "While sailing on a boat, Inessa noticed a beautiful water lily flower above the lake's surface. She came closer and it turned out that the lily was exactly $$$H$$$ centimeters above the water surface. Inessa grabbed the flower and sailed the distance of $$$L$$$ centimeters. Exactly at this point the flower touched the water surface. Suppose that the lily grows at some point $$$A$$$ on the lake bottom, and its stem is always a straight segment with one endpoint at point $$$A$$$. Also suppose that initially the flower was exactly above the point $$$A$$$, i.e. its stem was vertical. Can you determine the depth of the lake at point $$$A$$$?", "input_spec": "The only line contains two integers $$$H$$$ and $$$L$$$ ($$$1 \\le H < L \\le 10^{6}$$$).", "output_spec": "Print a single number — the depth of the lake at point $$$A$$$. The absolute or relative error should not exceed $$$10^{-6}$$$. Formally, let your answer be $$$A$$$, and the jury's answer be $$$B$$$. Your answer is accepted if and only if $$$\\frac{|A - B|}{\\max{(1, |B|)}} \\le 10^{-6}$$$.", "sample_inputs": ["1 2", "3 5"], "sample_outputs": ["1.5000000000000", "2.6666666666667"], "notes": null}, "positive_code": [{"source_code": "h,l = gets.split.map(&:to_f)\np (l**2-h**2)/2.0/h"}, {"source_code": "h,l = gets.split.map(&:to_i)\nputs (l*l-h*h)/(2.0*h)"}, {"source_code": "h,l = gets.chomp.split.map(&:to_i)\n\ndepth = 1.0 * (l*l - h*h) / (2*h)\nprintf(\"%.6f\\n\", depth)\n"}], "negative_code": [], "src_uid": "2cd5807e3f9685d4eff88f2283904f0d"} {"nl": {"description": "Little Petya very much likes arrays consisting of n integers, where each of them is in the range from 1 to 109, inclusive. Recently he has received one such array as a gift from his mother. Petya didn't like it at once. He decided to choose exactly one element from the array and replace it with another integer that also lies in the range from 1 to 109, inclusive. It is not allowed to replace a number with itself or to change no number at all. After the replacement Petya sorted the array by the numbers' non-decreasing. Now he wants to know for each position: what minimum number could occupy it after the replacement and the sorting.", "input_spec": "The first line contains a single integer n (1 ≤ n ≤ 105), which represents how many numbers the array has. The next line contains n space-separated integers — the array's description. All elements of the array lie in the range from 1 to 109, inclusive.", "output_spec": "Print n space-separated integers — the minimum possible values of each array element after one replacement and the sorting are performed.", "sample_inputs": ["5\n1 2 3 4 5", "5\n2 3 4 5 6", "3\n2 2 2"], "sample_outputs": ["1 1 2 3 4", "1 2 3 4 5", "1 2 2"], "notes": null}, "positive_code": [{"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\na.sort!\nb=1\nf=false\na.each{|i|\n if i!=1 then\n f=true\n end\n}\ncnt=0\na.each{|i|\n r=i\n if i!=b then\n r=b\n end\n if cnt==n-1 && f==false then\n r=2\n end\n print r\n print \" \"\n b=i\n cnt+=1\n}\n"}, {"source_code": "a = STDIN.readlines[1].split.map(&:to_i).sort\na[-1] = (a[-1] == 1 ? 2 : 1)\nputs a.sort.join ' '\n"}, {"source_code": "a = STDIN.readlines[1].split.map(&:to_i).sort\na[-1] = (a[-1] == 1 ? 2 : 1)\nputs a.sort.join ' '\n"}, {"source_code": "gets()\na = gets().split(' ').map(&:to_i).sort()\nif a[-1] == 1 then\n a[-1] = 2\nelse\n a[-1] = 1\n a.sort!()\nend\n\nputs a.join(' ')\n\n"}, {"source_code": "a = STDIN.readlines[1].split.map(&:to_i).sort\na[-1] = (a[-1] == 1 ? 2 : 1)\nputs a.sort.join ' '"}, {"source_code": "a = STDIN.readlines[1].split.map(&:to_i).sort\na[-1] = (a[-1] == 1 ? 2 : 1)\nputs a.sort.join ' '\n"}, {"source_code": "a = STDIN.readlines[1].split.map(&:to_i).sort\na[-1] = (a[-1] == 1 ? 2 : 1)\nputs a.sort.join ' '\n"}, {"source_code": "a = STDIN.readlines[1].split.map(&:to_i).sort\na[-1] = (a[-1] == 1 ? 2 : 1)\nputs a.sort.join ' '"}, {"source_code": "#=================================\nrequire 'prime'\nrequire 'set'\nrequire 'matrix'\nrequire 'tsort'\ninclude Math\n\nif ARGV.include?(\"ONLINE_JUDGE\")\n $stdin = File.open(\"./input.txt\", \"r\")\n ARGV.delete(\"ONLINE_JUDGE\")\nend\n#=================================\n\nn=gets.to_i\ndata=gets.split.map(&:to_i)\n\ndata.sort!\n\nif data[-1]!=1\n data[-1]=1\nelse\n data[-1]=2\nend\ndata.sort!\n\ndata.each{|v| print \"#{v} \"}"}, {"source_code": "n = gets.to_i\na = gets.split(' ').map(&:to_i).sort\n\na[-1] = (a[-1]==1) ? 2 : 1\n\na.sort!\nputs a.join(' ')"}, {"source_code": "a = STDIN.readlines[1].split.map(&:to_i).sort\na[-1] = (a[-1] == 1 ? 2 : 1)\nputs a.sort.join ' '\n"}, {"source_code": "n = gets.to_i\na = gets.split.collect{|i| i.to_i}\na = a.sort\nif a.pop != 1\n puts ([1]+a).join(\" \")\nelse\n puts (a+[2]).join(\" \")\nend\n"}, {"source_code": "n=gets.to_i;\na=gets.split.map{|x| x.to_i }.sort;\na[n-1]=(a[n-1]==1)?2:1;\na.sort.map{|x| print(x,\" \"); };\nputs();\n"}, {"source_code": "a = STDIN.readlines[1].split.map(&:to_i).sort\na[-1] = (a[-1] == 1 ? 2 : 1)\nputs a.sort.join ' '\n"}, {"source_code": "a = STDIN.readlines[1].split.map(&:to_i).sort\na[-1] = (a[-1] == 1 ? 2 : 1)\nputs a.sort.join ' '\n"}, {"source_code": "gets()\na = gets().split(' ').map(&:to_i).sort()\nif a[-1] == 1 then\n a[-1] = 2\nelse\n a[-1] = 1\n a.sort!()\nend\n\nputs a.join(' ')\n\n"}, {"source_code": "a = STDIN.readlines[1].split.map(&:to_i).sort\na[-1] = (a[-1] == 1 ? 2 : 1)\nputs a.sort.join ' '\n"}, {"source_code": "a = STDIN.readlines[1].split.map(&:to_i).sort\na[-1] = (a[-1] == 1 ? 2 : 1)\nputs a.sort.join ' '"}, {"source_code": "#$stdin = File.open(\"input.txt\")\n#$stdout = File.open(\"output.txt\",\"w\")\n\nN = gets.chomp.to_i\nA = gets.chomp.split.map { |c| c = c.to_i }\nA.sort!\nif A[-1] == 1\n\tA[-1] = 2\nelse\n\tA[-1] = 1\nend\nA.sort!\nputs A.join(\" \")"}, {"source_code": "#!/usr/bin/ruby\n\ndef readNextValues\n return gets.chomp.split.collect {|x| x.to_i }\nend\n\nn = readNextValues\narr = readNextValues\n\nmax_i = 0\nmax_val = 0\n\nfor i in 0..arr.size-1\n if arr[i] > max_val\n max_i = i\n max_val = arr[i]\n end\nend\n\nif max_val == 1\n arr[max_i] = 2\nelse\n arr[max_i] = 1\nend\n\narr.sort!\nputs arr.join(' ')\n"}, {"source_code": "#!/usr/bin/ruby\ndef readNextValues\n return gets.chomp.split.map(&:to_i)\nend\nn = gets.chomp.to_i\narr = readNextValues\n\nmax_val = arr.max\nmax_i = arr.index(max_val)\narr[max_i] = (max_val == 1) ? 2 : 1\n\nputs arr.sort.join(' ')\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\n\nif a.max > 1\n i = a.index a.max\n a[i] = 1\nelse\n a[0] = 2\nend\n\nprint a.sort * \" \"\n"}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i).sort.unshift(1)\nputs a.last==1 ? (\"1 \"*(n-1))+\"2\" : a.take(n)*\" \"\n "}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i).sort.unshift(1)\nputs a.last==1 ? (\"1 \"*(n-1))+\"2\" : a.take(n)*\" \""}, {"source_code": "#=================================\nrequire 'prime'\nrequire 'set'\nrequire 'matrix'\nrequire 'tsort'\ninclude Math\n\nif ARGV.include?(\"ONLINE_JUDGE\")\n\t$stdin = File.open(\"./input.txt\", \"r\")\n\tARGV.delete(\"ONLINE_JUDGE\")\nend\n#=================================\n\nn=gets.to_i\ndata=gets.split.map(&:to_i)\n\ndata.sort!\n\nif data[-1]!=1\n\tdata[-1]=1\nelse\n\tdata[-1]=2\nend\ndata.sort!\n\ndata.each{|v| print \"#{v} \"}\n"}, {"source_code": "n = gets\np = gets\n=begin\n\n\nfrom = Array.new(n.to_i)\nto = p.split(' ')\ni = 1\nfor el in to\n from[el.to_i-1] = i\n i+=1\nend\nputs from.join(' ').chomp\n=end\n\narr1 = p.chomp.split(' ')\narr2 = []\narr1 = arr1.each{|el| arr2 << el.to_i}\narr1 = arr2.sort\nmax = arr1[-1]\nif max == 1\n puts arr1[0..-2].join(' ').chomp+\" 2\"\nelse\n puts \"1 \" + arr1.sort[0..-2].join(' ')\nend\n"}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i).sort.unshift(1)\nputs a.last==1 ? (\"1 \"*(n-1))+\"2\" : a.take(n)*\" \"\n\n"}], "negative_code": [{"source_code": "\nn=gets.to_i\na=gets.split.map(&:to_i)\na.sort\nb=1\nf=false\na.each{|i|\n if i!=1 then\n f=true\n end\n}\ncnt=0\na.each{|i|\n r=i\n if i!=b then\n r=b\n end\n if cnt==n-1 && f==false then\n r=2\n end\n p r\n b=i\n cnt+=1\n}\n"}, {"source_code": "\nn=gets.to_i\na=gets.split.map(&:to_i)\na.sort\nb=1\nf=false\na.each{|i|\n if a[0]!=1 then\n f=true\n end\n}\ncnt=0\na.each{|i|\n r=i\n if i!=b then\n r=b\n end\n if cnt==n-1 && f==false then\n r=2\n end\n p r\n b=i\n}\n"}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\na.sort\nb=1\nf=false\na.each{|i|\n if i!=1 then\n f=true\n end\n}\ncnt=0\na.each{|i|\n r=i\n if i!=b then\n r=b\n end\n if cnt==n-1 && f==false then\n r=2\n end\n print r\n print \" \"\n b=i\n cnt+=1\n}\n"}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\na.sort\nb=1\na.each{|i|\n r=i\n if i!=b then\n r=b\n end\n p r\n b=i\n}\n"}, {"source_code": "gets(); puts ([1] + gets().split(' ').map(&:to_i).sort()[0..-2]).join(' ')\n"}, {"source_code": "gets()\na = gets().split(' ').map(&:to_i).sort()\nif a[-1] == 1 then\n a[-1] = 2\nelse\n a[0] = 1\nend\n\nputs a.join(' ')\n\n"}, {"source_code": "gets()\nputs ([1] + gets().split(' ').map(&:to_i).sort()[0..-2]).join(' ')\n"}, {"source_code": "n = gets.to_i\na = gets.split(' ').map(&:to_i)\n\nif a[-1] == 1\n a[-1] = 2\nelse\n a[-1] = 1\n a.sort!\nend\n \nputs a.join(' ')"}, {"source_code": "n = gets.to_i\na = gets.split(' ').map(&:to_i)\n\na[-1] = (a[-1]==1) ? 2 : 1\n\na.sort!\nputs a.join(' ')"}, {"source_code": "n = gets.to_i\na = gets.split.collect{|i| i.to_i}\na = [1]+a.sort\na.pop\nputs a.join(\" \")\n"}, {"source_code": "a = STDIN.readlines[1].split.map(&:to_i).sort\na[-1] = 1\nputs a.sort.join ' '"}, {"source_code": "#!/usr/bin/ruby\n\ndef readNextValues\n return gets.chomp.split.collect {|x| x.to_i }\nend\n\nn = readNextValues\narr = readNextValues\n\nmax_i = 0\nmax_val = 0\nfor i in 0..arr.size-1\n if arr[i] > max_i\n max_i = i\n max_val = arr[i]\n end\nend\n\nif max_val == 1\n arr[max_i] = 2\nelse\n arr[max_i] = 1\nend\n\narr.sort!\nputs arr.join(' ')\n"}, {"source_code": "#!/usr/bin/ruby\n\ndef readNextValues\n return gets.chomp.split.collect {|x| x.to_i }\nend\n\nn = readNextValues\narr = readNextValues\n\nmax_i = 0\nmax_val = 0\n\nfor i in 0..arr.size-1\n if arr[i] > max_i\n max_i = i\n max_val = arr[i]\n end\nend\n\nif max_val == 1\n arr[max_i] = 2\nelse\n arr[max_i] = 1\nend\n\narr.sort!\nputs arr.join(' ')\n"}, {"source_code": "#!/usr/bin/ruby\n\ndef readNextValues\n return gets.chomp.split.collect {|x| x.to_i }\nend\n\nn = readNextValues\narr = readNextValues\n\nmax_i = 0\nmax_val = 0\nfor i in 0..arr.size-1\n if arr[i] > max_i\n max_i = i\n max_val = arr[i]\n end\nend\n\nif max_val == 1\n arr[i] = 2\nelse\n arr[i] = 1\nend\n\narr.sort!\nputs arr.join(' ')\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\n\nif a.max > 1\n i = a.index a.max\n a[i] = 1\nend\n\nprint a.sort * \" \"\n"}, {"source_code": "#=================================\nrequire 'prime'\nrequire 'set'\nrequire 'matrix'\nrequire 'tsort'\ninclude Math\n\nif ARGV.include?(\"ONLINE_JUDGE\")\n\t$stdin = File.open(\"./input.txt\", \"r\")\n\tARGV.delete(\"ONLINE_JUDGE\")\nend\n#=================================\n\nn=gets.to_i\ndata=gets.split.map(&:to_i)\n\ndata.sort!\ndata[-1]=1\ndata.sort!\n\ndata.each{|v| print \"#{v} \"}\n"}, {"source_code": "n = gets\np = gets\n=begin\n\n\nfrom = Array.new(n.to_i)\nto = p.split(' ')\ni = 1\nfor el in to\n from[el.to_i-1] = i\n i+=1\nend\nputs from.join(' ').chomp\n=end\n\narr1 = p.chomp.split(' ')\narr1 = arr1.sort\nmax = arr1[-2]\nif max == '1'\n puts arr1[0..-2].join(' ').chomp+\" 2\"\nelse\n puts \"1 \" + arr1.sort[0..-2].join(' ')\nend\n"}, {"source_code": "n = gets\np = gets\narr1 = p.chomp.split(' ')\narr1 = arr1.sort\nmax = arr1[-2]\ni=1\nif max == 1\n i = 2\nend\nputs \"#{i} \" + arr1.sort[0..-2].join(' ')\n"}, {"source_code": "n = gets\np = gets\n=begin\n\n\nfrom = Array.new(n.to_i)\nto = p.split(' ')\ni = 1\nfor el in to\n from[el.to_i-1] = i\n i+=1\nend\nputs from.join(' ').chomp\n=end\n\narr1 = p.chomp.split(' ')\narr2 = []\narr1 = arr1.each{|el| arr2 << el.to_i}\narr1 = arr2.sort\nmax = arr1[-1]\nif max == '1'\n puts arr1[0..-2].join(' ').chomp+\" 2\"\nelse\n puts \"1 \" + arr1.sort[0..-2].join(' ')\nend\n"}, {"source_code": "n = gets\np = gets\narr1 = p.chomp.split(' ')\nputs '1 ' + arr1.sort[0..-1].join(' ')\n"}, {"source_code": "n = gets\np = gets\n=begin\n\n\nfrom = Array.new(n.to_i)\nto = p.split(' ')\ni = 1\nfor el in to\n from[el.to_i-1] = i\n i+=1\nend\nputs from.join(' ').chomp\n=end\n\narr1 = p.chomp.split(' ')\narr1 = arr1.sort\nmax = arr1[-1]\nif max == '1'\n puts arr1[0..-2].join(' ').chomp+\" 2\"\nelse\n puts \"1 \" + arr1.sort[0..-2].join(' ')\nend\n"}, {"source_code": "n = gets\np = gets\narr1 = p.chomp.split(' ')\nputs '1 ' + arr1.sort[0..-2].join(' ')"}, {"source_code": "n = gets\np = gets\n=begin\n\n\nfrom = Array.new(n.to_i)\nto = p.split(' ')\ni = 1\nfor el in to\n from[el.to_i-1] = i\n i+=1\nend\nputs from.join(' ').chomp\n=end\n\narr1 = p.chomp.split(' ')\narr1 = arr1.sort\nmax = arr1[-1]\nputs max\nif max == '1'\n puts arr1[0..-2].join(' ').chomp+\" 2\"\nelse\n puts \"1 \" + arr1.sort[0..-2].join(' ')\nend\n"}, {"source_code": "n = gets\np = gets\narr1 = p.chomp.split(' ')\narr1 = arr1.sort\nmax = arr1[-2]\nif max == 1\n puts arr1.sort[0..-2].join(' ')+\" 2\"\nelse\n puts \"1 \" + arr1.sort[0..-2].join(' ')\nend\n"}, {"source_code": "n=gets.to_i\nputs (\"1 \"+gets).split.map(&:to_i).sort.take(n)*\" \"\n\n"}], "src_uid": "1cd10f01347d869be38c08ad64266870"} {"nl": {"description": "You are given an equation: Ax2 + Bx + C = 0. Your task is to find the number of distinct roots of the equation and print all of them in ascending order.", "input_spec": "The first line contains three integer numbers A, B and C ( - 105 ≤ A, B, C ≤ 105). Any coefficient may be equal to 0.", "output_spec": "In case of infinite root count print the only integer -1. In case of no roots print the only integer 0. In other cases print the number of root on the first line and the roots on the following lines in the ascending order. Print roots with at least 5 digits after the decimal point.", "sample_inputs": ["1 -5 6"], "sample_outputs": ["2\n2.0000000000\n3.0000000000"], "notes": null}, "positive_code": [{"source_code": "(a,b,c)=gets.split.map{|i|i=i.to_i}\nif a==0\n if b==0 then puts c==0?\"-1\":\"0\";exit end\n puts \"1\";p -c.to_f/b;exit\nend\nif (d=b*b-4*a*c)<0 then puts \"0\";exit end\nif d==0 then puts \"1\";p -b.to_f/2/a;exit end\nputs \"2\"\nif a<0 then a=-a;b=-b;c=-c end\np (-b-Math.sqrt(d))/2/a,(-b+Math.sqrt(d))/2/a"}, {"source_code": "(a,b,c)=gets.split.map{|i|i=i.to_i}\nif a==0&&b==0 then puts c==0?\"-1\":\"0\";exit end\nif a==0\n puts \"1\"\n printf \"%.9f\\n\", -c.to_f/b\n exit\nend\nif (d=b*b-4*a*c)<0 then puts \"0\";exit end\nif d==0\n puts \"1\"\n printf \"%.9f\\n\", -b.to_f/2/a\n exit\nend\nputs \"2\"\nif a<0 then a=-a;b=-b;c=-c end\nprintf \"%.9f\\n\", (-b-Math.sqrt(d))/2/a\nprintf \"%.9f\\n\", (-b+Math.sqrt(d))/2/a\n"}, {"source_code": "(a,b,c)=gets.split.map{|i|i=i.to_i}\nif a==0&&b==0 then puts c==0?\"-1\":\"0\";exit end\nif a==0\n puts \"1\"\n printf \"%.9f\\n\", -c.to_f/b\n exit\nend\nif (d=b*b-4*a*c)<0 then puts \"0\";exit end\nif d==0\n puts \"1\"\n printf \"%.9f\\n\", -b.to_f/2/a\n exit\nend\nputs \"2\"\nif a<0 then a=-a;b=-b;c=-c end\np (-b-Math.sqrt(d))/2/a\np (-b+Math.sqrt(d))/2/a\n"}, {"source_code": "input=gets.split.map(&:to_i)\na=input[0]\nb=input[1]\nc=input[2]\nif a<0 then\n b=-b\n c=-c\n a=-a\nend\nd=b*b-4*a*c\nif a==0 then\n if b==0 then\n if c==0 then\n puts -1\n else\n puts 0\n end\n else\n puts 1\n printf \"%.7f\\n\",-c*1.0/b\n end\nelsif d==0 then\n puts 1\n printf \"%.7f\\n\",-b/(2*a)\nelsif d>0 then\n puts 2\n printf \"%.7f\\n\",(-b-Math::sqrt(d))/(2*a)\n printf \"%.7f\\n\",(-b+Math::sqrt(d))/(2*a)\nelse\n puts 0\nend\n\n"}, {"source_code": "(a,b,c)=gets.split.map{|i|i=i.to_i}\nif a==0\n if b==0 then puts c==0?\"-1\":\"0\";exit end\n puts \"1\";p -c.to_f/b;exit\nend\nif (d=b*b-4*a*c)<0 then puts \"0\";exit end\nif d==0 then puts \"1\";p -b.to_f/2/a;exit end\nputs \"2\"\nif a<0 then a=-a;b=-b;c=-c end\np (-b-Math.sqrt(d))/2/a,(-b+Math.sqrt(d))/2/a"}, {"source_code": "def printAns(num)\n printf(\"%10.10f\\n\",num)\nend\nc=gets.chomp.split.map{|i| i.to_i}\nif c[0]==0\n if c[1]==0\n if c[2]==0\n puts(-1)\n else\n puts(0)\n end\n else\n puts(1)\n printAns(-c[2].to_f/c[1].to_f)\n end\nelse\n D=c[1]*c[1]-4*c[0]*c[2]\n if D<0 \n puts(0)\n exit\n elsif D==0\n puts(1)\n ans=-c[1].to_f/(2.0*c[0].to_f)\n printAns(ans)\n else\n puts(2)\n r=Math.sqrt(D)\n if c[0] < 0\n c[0]=-c[0]\n c[1]=-c[1]\n end\n printAns((-c[1].to_f-r)/(2*c[0]))\n printAns((-c[1].to_f+r)/(2*c[0]))\n end\nend\n"}, {"source_code": "def printAns(num)\n printf(\"%10.10f\\n\",num)\nend\n(a,b,c)=gets.chomp.split.map{|i| i.to_i}\nif a==0\n if b==0\n if c==0\n puts(-1)\n else\n puts(0)\n end\n else\n puts(1)\n printAns(-c.to_f/b.to_f)\n end\nelse\n D=b*b-4*a*c\n if D<0 \n puts(0)\n exit\n elsif D==0\n puts(1)\n ans=-b.to_f/(2.0*a.to_f)\n printAns(ans)\n else\n puts(2)\n r=Math.sqrt(D)\n if a < 0\n a=-a\n b=-b\n end\n printAns((-b.to_f-r)/(2*a))\n printAns((-b.to_f+r)/(2*a))\n end\nend\n"}, {"source_code": "a, b, c = gets.split.map(&:to_i)\nif a == 0\n\tif b == 0\n\t\tputs c == 0 ? -1 : 0\n\telse\n\t\tputs 1\n\t\tputs \"%.10f\" % (-1.0 * c / b)\n\tend\nelse\n\td = b * b - 4 * a * c\n\tif d < 0\n\t\tputs 0\n\telsif d == 0\n\t\tputs 1\n\t\tputs \"%.10f\" % (-b / (2.0 * a))\n\telse\n\t\tdd = Math.sqrt(d)\n\t\tputs 2\n\t\t[(-b - dd) / (2.0 * a), (-b + dd) / (2.0 * a)].sort.each { |v| puts \"%.10f\" % v }\n\tend\nend\n"}], "negative_code": [{"source_code": "(a,b,c)=gets.split.map{|i|i=i.to_i}\nif a==0&&b==0 then puts c==0?\"-1\":\"0\";exit end\nif a==0\n puts \"1\"\n printf \"%.9f\\n\", -b.to_f/c\n exit\nend\nif (d=b*b-4*a*c)<0 then puts \"0\";exit end\nif d==0\n puts \"1\"\n printf \"%.9f\\n\", -b.to_f/2/a\n exit\nend\nputs \"2\"\nif a<0 then a=-a;b=-b;c=-c end\nprintf \"%.9f\\n\", (-b-Math.sqrt(d))/2/a\nprintf \"%.9f\\n\", (-b+Math.sqrt(d))/2/a\n"}, {"source_code": "(a,b,c)=gets.split.map{|i|i=i.to_i}\nif a==0&&b==0 then puts c==0?\"-1\":\"0\";exit end\nif a==0\n puts \"1\"\n p -b.to_f/c\n exit\nend\nif (d=b*b-4*a*c)<0 then puts \"0\";exit end\nif d==0\n puts \"1\"\n p -b.to_f/2/a\n exit\nend\nputs \"2\"\np (-b+Math.sqrt(d))/2/a\np (-b-Math.sqrt(d))/2/a\n"}, {"source_code": "(a,b,c)=gets.split.map{|i|i=i.to_i}\nif a==0&&b==0 then puts c==0?\"-1\":\"0\";exit end\nif a==0\n puts \"1\"\n printf \"%.9f\\n\", -b.to_f/c\n exit\nend\nif (d=b*b-4*a*c)<0 then puts \"0\";exit end\nif d==0\n puts \"1\"\n printf \"%.9f\\n\", -b.to_f/2/a\n exit\nend\nputs \"2\"\nprintf \"%.9f\\n\", (-b-Math.sqrt(d))/2/a\nprintf \"%.9f\\n\", (-b+Math.sqrt(d))/2/a\n"}, {"source_code": "(a,b,c)=gets.split.map{|i|i=i.to_i}\nif a==0&&b==0 then puts c==0?\"-1\":\"0\";exit end\nif a==0\n puts \"1\"\n printf \"%.9f\\n\", -b.to_f/c\n exit\nend\nif (d=b*b-4*a*c)<0 then puts \"0\";exit end\nif d==0\n puts \"1\"\n printf \"%.9f\\n\", -b.to_f/2/a\n exit\nend\nputs \"2\"\nprintf \"%.9f\\n\", (-b+Math.sqrt(d))/2/a\nprintf \"%.9f\\n\", (-b-Math.sqrt(d))/2/a\n"}, {"source_code": "input=gets.split.map(&:to_i)\na=input[0]\nb=input[1]\nc=input[2]\nd=b*b-4*a*c\nif d==0 then\n puts 1\n printf \"%.7f\\n\",-b/(2*a)\nelsif d>0 then\n puts 2\n printf \"%.7f\\n\",(-b-Math::sqrt(d))/(2*a)\n printf \"%.7f\\n\",(-b+Math::sqrt(d))/(2*a)\nelse\n puts 0\nend\n"}, {"source_code": "input=gets.split.map(&:to_i)\na=input[0]\nb=input[1]\nc=input[2]\nif a<0 then\n b=-b\n c=-c\n a=-c\nend\nd=b*b-4*a*c\nif a==0 then\n if b==0 then\n if c==0 then\n puts -1\n else\n puts 0\n end\n else\n puts 1\n printf \"%.7f\\n\",-c*1.0/b\n end\nelsif d==0 then\n puts 1\n printf \"%.7f\\n\",-b/(2*a)\nelsif d>0 then\n puts 2\n printf \"%.7f\\n\",(-b-Math::sqrt(d))/(2*a)\n printf \"%.7f\\n\",(-b+Math::sqrt(d))/(2*a)\nelse\n puts 0\nend\n\n"}, {"source_code": "input=gets.split.map(&:to_i)\na=input[0]\nb=input[1]\nc=input[2]\nd=b*b-4*a*c\nif a==0 then\n if b==0 then\n if c==0 then\n puts -1\n else\n puts 0\n end\n else\n puts 1\n printf \"%.7f\\n\",-c*1.0/b\n end\nelsif d==0 then\n puts 1\n printf \"%.7f\\n\",-b/(2*a)\nelsif d>0 then\n puts 2\n printf \"%.7f\\n\",(-b-Math::sqrt(d))/(2*a)\n printf \"%.7f\\n\",(-b+Math::sqrt(d))/(2*a)\nelse\n puts 0\nend\n\n"}, {"source_code": "input=gets.split.map(&:to_i)\na=input[0]\nb=input[1]\nc=input[2]\nd=b*b-4*a*c\nif a==0 then\n puts 1\n printf \"%.7f\\n\",-c*1.0/b\nelsif d==0 then\n puts 1\n printf \"%.7f\\n\",-b/(2*a)\nelsif d>0 then\n puts 2\n printf \"%.7f\\n\",(-b-Math::sqrt(d))/(2*a)\n printf \"%.7f\\n\",(-b+Math::sqrt(d))/(2*a)\nelse\n puts 0\nend\n\n"}, {"source_code": "def printAns(num)\n printf(\"%10.10f\\n\",num)\nend\nc=gets.chomp.split.map{|i| i.to_i}\nif c[0]==0\n if c[1]==0\n if c[2]==0\n puts(-1)\n else\n puts(0)\n end\n else\n puts(1)\n printAns(-c[2].to_f/c[1].to_f)\n end\nelse\n D=c[1]*c[1]-4*c[0]*c[2]\n if D<0 \n puts(0)\n exit\n elsif D==0\n puts(1)\n ans=-c[1]/(2*c[0])\n printAns(ans)\n else\n puts(2)\n r=Math.sqrt(D)\n printAns((-c[1]-r)/(2*c[0]))\n printAns((-c[1]+r)/(2*c[0]))\n end\nend\n"}, {"source_code": "def printAns(num)\n printf(\"%10.10f\\n\",num)\nend\nc=gets.chomp.split.map{|i| i.to_i}\nif c[0]==0\n if c[1]==0\n if c[2]==0\n puts(-1)\n else\n puts(0)\n end\n else\n puts(1)\n printAns(-c[2]/c[1])\n end\nelse\n D=c[1]*c[1]-4*c[0]*c[2]\n if D<0 \n puts(0)\n exit\n elsif D==0\n puts(1)\n ans=-c[1]/(2*c[0])\n printAns(ans)\n else\n puts(2)\n r=Math.sqrt(D)\n printAns((-c[1]-r)/(2*c[0]))\n printAns((-c[1]+r)/(2*c[0]))\n end\nend\n"}, {"source_code": "def printAns(num)\n printf(\"%10.10f\\n\",num)\nend\nc=gets.chomp.split.map{|i| i.to_i}\nif c[0]==0\n if c[1]==0\n if c[2]==0\n puts(-1)\n else\n puts(0)\n end\n else\n puts(1)\n printAns(-c[2].to_f/c[1].to_f)\n end\nelse\n D=c[1]*c[1]-4*c[0]*c[2]\n if D<0 \n puts(0)\n exit\n elsif D==0\n puts(1)\n ans=-c[1].to_f/(2.0*c[0].to_f)\n printAns(ans)\n else\n puts(2)\n r=Math.sqrt(D)\n printAns((-c[1].to_f-r)/(2*c[0]))\n printAns((-c[1].to_f+r)/(2*c[0]))\n end\nend\n"}, {"source_code": "def printAns(num)\n printf(\"%10.10f\\n\",num)\nend\nc=gets.chomp.split.map{|i| i.to_i}\nif c[0]==0\n if c[1]==0\n if c[2]==0\n puts(-1)\n else\n puts(0)\n end\n else\n puts(1)\n printAns(-c[2].to_f/c[1].to_f)\n end\nelse\n D=c[1]*c[1]-4*c[0]*c[2]\n if D<0 \n puts(0)\n exit\n elsif D==0\n puts(1)\n ans=-c[1].to_f/(2*c[0].to_f)\n printAns(ans)\n else\n puts(2)\n r=Math.sqrt(D)\n printAns((-c[1]-r)/(2*c[0]))\n printAns((-c[1]+r)/(2*c[0]))\n end\nend\n"}, {"source_code": "def printAns(num)\n printf(\"%10.10f\\n\",num)\nend\nc=gets.chomp.split.map{|i| i.to_i}\nif c[0]==0\n if c[1]==0\n if c[2]==0\n puts(-1)\n else\n puts(0)\n end\n else\n puts(1)\n printAns(c[2]/c[1])\n end\nelse\n D=c[1]*c[1]-4*c[0]*c[2]\n if D<0 \n puts(0)\n exit\n elsif D==0\n puts(1)\n ans=-c[1]/(2*c[0])\n printAns(ans)\n else\n puts(2)\n r=Math.sqrt(D)\n printAns((-c[1]-r)/(2*c[0]))\n printAns((-c[1]+r)/(2*c[0]))\n end\nend\n"}, {"source_code": "def printAns(num)\n printf(\"%10.10f\\n\",num)\nend\nc=gets.chomp.split.map{|i| i.to_i}\nif c[0]==0\n if c[1]==0\n if c[2]==0\n puts(-1)\n else\n puts(0)\n end\n else\n puts(1)\n printAns(c[2]/c[1])\n end\nelse\n D=c[1]*c[1]-4*c[0]*c[2]\n if D<0 \n puts(0)\n exit\n elsif D==0\n puts(1)\n ans=-c[1]/(2*c[0])\n printAns(ans)\n else\n puts(2)\n r=Math.sqrt(D)\n printAns((-c[1]+r)/(2*c[0]))\n printAns((-c[1]-r)/(2*c[0]))\n end\nend\n"}, {"source_code": "a, b, c = gets.split.map(&:to_i)\nif a == 0\n\tif b == 0\n\t\tputs c == 0 ? -1 : 0\n\telse\n\t\tputs 1\n\t\tputs \"%.10f\" % (-c / b)\n\tend\nelse\n\td = b * b - 4 * a * c\n\tif d < 0\n\t\tputs 0\n\telsif d == 0\n\t\tputs 1\n\t\tputs \"%.10f\" % (-b / (2 * a))\n\telse\n\t\tdd = Math.sqrt(d)\n\t\tputs 2\n\t\t[(-b - dd) / (2 * a), (-b + dd) / (2 * a)].sort.each { |v| puts \"%.10f\" % v }\n\tend\nend\n"}, {"source_code": "a, b, c = gets.split.map(&:to_i)\nif a == 0\n\tif b == 0\n\t\tputs c == 0 ? -1 : 0\n\telse\n\t\tputs 1\n\t\tputs -c / b\n\tend\nelse\n\td = b * b - 4 * a * c\n\tif d < 0\n\t\tputs 0\n\telsif d == 0\n\t\tputs 1\n\t\tputs -b / (2 * a)\n\telse\n\t\tdd = Math.sqrt(d)\n\t\tputs 2\n\t\tputs [(-b - dd) / (2 * a), (-b + dd) / (2 * a)].sort\n\tend\nend\n"}], "src_uid": "84372885f2263004b74ae753a2f358ac"} {"nl": {"description": "There are n games in a football tournament. Three teams are participating in it. Currently k games had already been played. You are an avid football fan, but recently you missed the whole k games. Fortunately, you remember a guess of your friend for these k games. Your friend did not tell exact number of wins of each team, instead he thought that absolute difference between number of wins of first and second team will be d1 and that of between second and third team will be d2.You don't want any of team win the tournament, that is each team should have the same number of wins after n games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?Note that outcome of a match can not be a draw, it has to be either win or loss.", "input_spec": "The first line of the input contains a single integer corresponding to number of test cases t (1 ≤ t ≤ 105). Each of the next t lines will contain four space-separated integers n, k, d1, d2 (1 ≤ n ≤ 1012; 0 ≤ k ≤ n; 0 ≤ d1, d2 ≤ k) — data for the current test case.", "output_spec": "For each test case, output a single line containing either \"yes\" if it is possible to have no winner of tournament, or \"no\" otherwise (without quotes).", "sample_inputs": ["5\n3 0 0 0\n3 3 0 0\n6 4 1 0\n6 3 3 0\n3 3 3 2"], "sample_outputs": ["yes\nyes\nyes\nno\nno"], "notes": "NoteSample 1. There has not been any match up to now (k = 0, d1 = 0, d2 = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.Sample 2. You missed all the games (k = 3). As d1 = 0 and d2 = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is \"yes\".Sample 3. You missed 4 matches, and d1 = 1, d2 = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins)."}, "positive_code": [{"source_code": "N = gets.to_i\ndef check(n, a)\n a.sort!\n return 0 if a.any? {|x|\n (x < 0 || x.to_i != x)\n }\n t = n - a[2] * 2 + a[1] + a[0]\n return 0 if t < 0 || t % 3 != 0\n 1\nend\n\nN.times{\n\tn,k,d1,d2=gets.split.map{|i| i.to_i}\n\tn-=k\n\tf=0\n\ty=(k+d2-d1)/3.0\n\tx=y+d1\n\tz=y-d2\n\tf|=check(n,[x,y,z])\n\ty=(k-d2-d1)/3.0\n\tx=y+d1\n\tz=y+d2\n\tf|=check(n,[x,y,z])\n\ty=(k+d2+d1)/3.0\n\tx=y-d1\n\tz=y-d2\n\tf|=check(n,[x,y,z])\n\ty=(k-d2+d1)/3.0\n\tx=y-d1\n\tz=y+d2\n\tf|=check(n,[x,y,z])\n\tputs (f==1) ? 'yes' : 'no'\n}"}, {"source_code": "N = gets.to_i\ndef check(n, a)\n a.sort!\n return 0 if a.any? {|x|\n (x < 0 || x.to_i != x)\n }\n t = n - a[2] * 2 + a[1] + a[0]\n return 0 if t < 0 || t % 3 != 0\n 1\nend\n\nN.times{\n\tn,k,d1,d2=gets.split.map{|i| i.to_i}\n\tn-=k\n\tf=0\n\ty=(k+d2-d1)/3.0\n\tx=y+d1\n\tz=y-d2\n\tf|=check(n,[x,y,z])\n\ty=(k-d2-d1)/3.0\n\tx=y+d1\n\tz=y+d2\n\tf|=check(n,[x,y,z])\n\ty=(k+d2+d1)/3.0\n\tx=y-d1\n\tz=y-d2\n\tf|=check(n,[x,y,z])\n\ty=(k-d2+d1)/3.0\n\tx=y-d1\n\tz=y+d2\n\tf|=check(n,[x,y,z])\n\tputs (f==1) ? 'yes' : 'no'\n}"}, {"source_code": "def check(n,a)\n\ta.sort!\n\treturn 0 if a.any?{|x| (x<0 || x.to_i!=x)}\n\tt=n-a[2]*2+a[1]+a[0]\n\treturn 0 if t<0 || t%3!=0\n\t1\nend\nm=gets.to_i\nm.times{\n\tn,k,d1,d2=gets.split.map{|i| i.to_i}\n\tn-=k\n\tf=0\n\ty=(k+d2-d1)/3.0\n\tx=y+d1\n\tz=y-d2\n\tf|=check(n,[x,y,z])\n\ty=(k-d2-d1)/3.0\n\tx=y+d1\n\tz=y+d2\n\tf|=check(n,[x,y,z])\n\ty=(k+d2+d1)/3.0\n\tx=y-d1\n\tz=y-d2\n\tf|=check(n,[x,y,z])\n\ty=(k-d2+d1)/3.0\n\tx=y-d1\n\tz=y+d2\n\tf|=check(n,[x,y,z])\n\tputs (f==1) ? 'yes' : 'no'\n}"}, {"source_code": "n = gets.chomp.to_i\n\ndef sol_win(n, k, win)\n\tround = n / 3\n\tok = true\n\twin.each do |i|\n\t\tok = false if i > round\n\tend\n\tok\nend\n\ndef sol_real(n, k, d1, d2)\n\twin = []\n\tif (k - (d1 + d2)) % 3 != 0\n\t\treturn false\n\telse\n\t\tt = (k - (d1 + d2)) / 3\n\t\tif t >= 0 and t + d1 >= 0 and t + d2 >= 0\n\t\t\twin = [t + d1, t, t + d2]\n\t\t\treturn sol_win(n, k, win)\n\t\telse\n\t\t\treturn false\n\t\tend\n\tend\nend\n\n# use second team as t\ndef sol(n, k, d1, d2)\n\tif n % 3 != 0\n\t\treturn false\n\tend\n\tsol_real(n, k, d1, d2) or sol_real(n, k, -d1, d2) or sol_real(n, k, d1, -d2) or sol_real(n, k, -d1, -d2)\nend\n\n\nn.times do\n\tn, k, d1, d2 = gets.chomp.split.map{|i| i.to_i}\n\tif sol(n, k, d1, d2)\n\t\tputs \"yes\"\n\telsif\n\t\tputs \"no\"\n\tend\nend\n"}], "negative_code": [{"source_code": "def check(n,a)\n\ta.sort!\n\treturn 0 if a.one?{|x| (x<0 || x.to_i!=x)}\n\tt=n-a[2]*2+a[1]+a[0]\n\treturn 0 if t<0 || t%3!=0\n\t1\nend\nm=gets.to_i\nm.times{\n\tn,k,d1,d2=gets.split.map{|i| i.to_i}\n\tn-=k\n\tf=0\n\ty=(k+d2-d1)/3.0\n\tx=y+d1\n\tz=y-d2\n\tf|=check(n,[x,y,z])\n\ty=(k-d2-d1)/3.0\n\tx=y+d1\n\tz=y+d2\n\tf|=check(n,[x,y,z])\n\ty=(k+d2+d1)/3.0\n\tx=y-d1\n\tz=y-d2\n\tf|=check(n,[x,y,z])\n\ty=(k-d2+d1)/3.0\n\tx=y-d1\n\tz=y+d2\n\tf|=check(n,[x,y,z])\n\tputs (f==1) ? 'yes' : 'no'\n}\n"}], "src_uid": "324298100f3e36d289ef6ca8178ac6d3"} {"nl": {"description": "Nicholas, a painter is going to paint several new canvases. Nicholas is sure that the canvases will turn out so great that each one will need framing and being hung on the wall. Frames are what Nicholas decided to begin with. Nicholas has n sticks whose lengths equal a1, a2, ... an. Nicholas does not want to break the sticks or glue them together. To make a h × w-sized frame, he needs two sticks whose lengths equal h and two sticks whose lengths equal w. Specifically, to make a square frame (when h = w), he needs four sticks of the same length.Now Nicholas wants to make from the sticks that he has as many frames as possible; to be able to paint as many canvases as possible to fill the frames. Help him in this uneasy task. Note that it is not necessary to use all the sticks Nicholas has.", "input_spec": "The first line contains an integer n (1 ≤ n ≤ 100) — the number of sticks. The second line contains n space-separated integers. The i-th integer equals the length of the i-th stick ai (1 ≤ ai ≤ 100).", "output_spec": "Print the single number — the maximum number of frames Nicholas can make for his future canvases.", "sample_inputs": ["5\n2 4 3 2 3", "13\n2 2 4 4 4 4 6 6 6 7 7 9 9", "4\n3 3 3 5"], "sample_outputs": ["1", "3", "0"], "notes": null}, "positive_code": [{"source_code": "a=[*$<][1].split;p (?1..?1*3).inject(0){|i,j|i+a.count(j)/2}/2"}, {"source_code": "gets;h=Hash.new(0);d=gets.split.map(&:to_i).each{|_| h[_]+=1}\np h.values.map{|_| _%2!=0 ?\t_-1\t: _ }.inject(:+)/4\n"}, {"source_code": "gets\nhs=Hash.new(0)\ndata=gets.split.map(&:to_i).each{|v| hs[v]+=1}\nputs hs.values.map{|_| _%2!=0 ?\t_-1\t: _ }.inject(:+)/4\n"}, {"source_code": "n = gets.to_i\na = Array.new(101,0)\nvs = gets.split.map(&:to_i)\nvs.each do |v|\n\ta[v] += 1\nend\nans = 0\nfor i in 1..100 do\n ans += (a[i].div(2))\nend\np ans.div(2)\n"}, {"source_code": "a=[*$<][1].split;p (?1..?1*3).inject(0){|i,j|i+a.count(j)/2}/2\n"}, {"source_code": "n = gets.to_i\na = Hash.new(0)\ngets.split.each{|i| a[i]+=1}\ncnt = 0\na.each{|key,value| cnt+=value/2}\nputs cnt/2\n"}, {"source_code": "gets\nsticks = gets.split.map(&:to_i)\np sticks.uniq.inject(0) {|sumstick, _| sumstick += (sticks.count(_) / 2)} / 2"}, {"source_code": "a=[*$<][1].split;p (?1..?1*3).inject(0){|i,j|i+a.count(j)/2}/2\n"}, {"source_code": "gets\nputs gets.split.map { |x| x.to_i }.inject(Hash.new(0)) { |h, e| h[e] += 1; h }.values.map { |x| x - x % 2 }.inject(:+) / 4"}, {"source_code": "n = gets.to_i\na = gets().split.map { |elem| elem.to_i }\n\npairs_count = 0\n\nfor i in 1..(n-1)\n for j in 0..(i - 1)\n if (a[i] == a[j])\n a[i] = 0\n a[j] = 0\n pairs_count += 1\n break\n end\n end\nend\n\nputs (pairs_count / 2).to_i\n"}, {"source_code": "n = gets.to_i\na = gets.chomp.split(\" \").map{|i| i.to_i}\nl = []\ndem = 0\n\n(0..n-1).each do |i|\n l[i] = 0\nend\n\n(0..n-2).each do |i|\n if l[i] == 0 \n (i+1..n-1).each do |j|\n if a[i] == a[j] && l[j] == 0\n l[i] = 1\n l[j] = 1\n dem += 1\n break\n end\n end\n end\nend\n\nputs dem/2"}, {"source_code": "gets\na, ans = Array.new(101,0), 0\ngets.split.map{ |x| a[x.to_i] += 1 }\na.each{ |x| ans += x/2 }\np ans/2"}, {"source_code": "a=[*$<][1].split;p (?1..?1*3).inject(0){|i,j|i+a.count(j)/2}/2\n"}, {"source_code": "a=[*$<][1].split;p (?1..?1*3).inject(0){|i,j|i+a.count(j)/2}/2\n"}, {"source_code": "n = STDIN.gets.chomp.to_i\nlength = STDIN.gets.chomp.split(' ').map{|i| i.to_i}\narr = length.inject(Array.new(101).fill(0)) {|res, i| res[i] += 1; res}\nputs arr.reduce(0){|tot, i| tot += (i / 2)} / 2"}, {"source_code": "gets\np gets.split.map(&:to_i).group_by{|x|x}.map{|x|x[1].size/2}.inject(:+)/2\n"}, {"source_code": "gets\np gets.split.map(&:to_i).group_by{|x|x}.inject(0){|s,x|s+x[1].size/2}/2\n"}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\ncnt=Hash.new\ncnt.default=0\na.each{|i| cnt[i]+=1 }\nans=0\n1.upto(100){|i| ans+=cnt[i]/2 }\np ans/2\n"}, {"source_code": "a=[*$<][1].split;p (?1..?1*3).inject(0){|i,j|i+a.count(j)/2}/2\n"}, {"source_code": "a=[*$<][1].split;p (?1..?1*3).inject(0){|i,j|i+a.count(j)/2}/2\n"}, {"source_code": "#!/usr/bin/env ruby\n\nn = $stdin.readline().to_i\n\nlens = ($stdin.readline().split().collect {|x| x.to_i})\n\ndef uniq(a)\n\tresult = Array.new\n\tprev = nil\n\tcount = 0\n\ta.sort.each { |x|\n\t\tif x != prev\n\t\t\tresult << [prev, count] unless prev == nil\n\t\t\tprev = x\n\t\t\tcount = 1\n\t\telse\n\t\t\tcount += 1\n\t\tend\n\t}\n\tresult << [prev, count]\nend\n\npairs = 0\n\nuniq(lens).each { |r|\n\tpairs += (r[1]/2).floor\n}\n\nputs (pairs/2).floor\n"}, {"source_code": "n = STDIN.readline.to_i\nsticks = STDIN.readline.split.map {|s| s.to_i }\ncount = {}\nsticks.each do |stick|\n if count.include? stick\n count[stick] += 1\n else\n count[stick] = 1\n end\nend\npairs = 0\ncount.values.each {|value| pairs += value/2 }\nputs pairs/2\n"}, {"source_code": "gets\nd = {}\nd.default = 0\ngets.split().map(&:to_i).each{|e| d[e] += 1 }\n\nret = 0\nd.each_value{ |e| ret += e/2 }\n\np ret / 2\n"}, {"source_code": "s,n=0,(gets.to_i)\na=gets.split.map &:to_i\n0.upto 101 do |i| s+=a.count(i)/2 end\nputs s/2"}, {"source_code": "a=[*$<][1].split;p (?1..?1*3).inject(0){|i,j|i+a.count(j)/2}/2"}, {"source_code": "n = gets.to_i\nh = Hash.new(0)\ngets.split.map(&:to_i).each { |x| h[x] += 1}\nputs h.values.map { |x| x/2}.inject(:+) / 2"}, {"source_code": "a=[*$<][1].split;p (?1..?1*3).inject(0){|i,j|i+a.count(j)/2}/2\n"}, {"source_code": "a=[*$<][1].split;p (?1..?1*3).inject(0){|i,j|i+a.count(j)/2}/2\n"}, {"source_code": "a=[*$<][1].split;p (?1..?1*3).inject(0){|i,j|i+a.count(j)/2}/2"}], "negative_code": [], "src_uid": "f9b56b3fddcd5db0d0671714df3f8646"} {"nl": {"description": "You are given $$$n$$$ numbers $$$a_1, a_2, \\dots, a_n$$$. With a cost of one coin you can perform the following operation:Choose one of these numbers and add or subtract $$$1$$$ from it.In particular, we can apply this operation to the same number several times.We want to make the product of all these numbers equal to $$$1$$$, in other words, we want $$$a_1 \\cdot a_2$$$ $$$\\dots$$$ $$$\\cdot a_n = 1$$$. For example, for $$$n = 3$$$ and numbers $$$[1, -3, 0]$$$ we can make product equal to $$$1$$$ in $$$3$$$ coins: add $$$1$$$ to second element, add $$$1$$$ to second element again, subtract $$$1$$$ from third element, so that array becomes $$$[1, -1, -1]$$$. And $$$1\\cdot (-1) \\cdot (-1) = 1$$$.What is the minimum cost we will have to pay to do that?", "input_spec": "The first line contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$) — the number of numbers. The second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$-10^9 \\le a_i \\le 10^9$$$) — the numbers.", "output_spec": "Output a single number — the minimal number of coins you need to pay to make the product equal to $$$1$$$.", "sample_inputs": ["2\n-1 1", "4\n0 0 0 0", "5\n-5 -3 5 3 0"], "sample_outputs": ["2", "4", "13"], "notes": "NoteIn the first example, you can change $$$1$$$ to $$$-1$$$ or $$$-1$$$ to $$$1$$$ in $$$2$$$ coins.In the second example, you have to apply at least $$$4$$$ operations for the product not to be $$$0$$$.In the third example, you can change $$$-5$$$ to $$$-1$$$ in $$$4$$$ coins, $$$-3$$$ to $$$-1$$$ in $$$2$$$ coins, $$$5$$$ to $$$1$$$ in $$$4$$$ coins, $$$3$$$ to $$$1$$$ in $$$2$$$ coins, $$$0$$$ to $$$1$$$ in $$$1$$$ coin."}, "positive_code": [{"source_code": "n = readline.to_i\na = readline.split.map(&:to_i)\n\nans = 0\nneg_c = 0\nzero_c = 0\na.each do |v|\n if v < 0\n neg_c += 1\n elsif v == 0\n zero_c += 1\n end\n \n ans += (v.abs-1).abs\nend\n\nif neg_c.odd? && zero_c == 0\n ans += 2\nend\n\nputs ans"}, {"source_code": "n = gets.to_i\nsum = 0\nnegOne = 0\nposOne = 0\nzero = 0\narr = gets.split.map(&:to_i)\narr.each_with_index do |x, i|\n if x < 0\n sum += (x - (-1)).abs\n arr[i] = -1\n negOne += 1\n elsif x > 0\n sum += (x - 1).abs\n arr[i] = 1\n posOne += 1\n else\n zero += 1\n end\nend\nif negOne %2 != 0\n if zero > 0\n sum += 1\n negOne += 1\n zero -= 1\n else\n sum += 2\n negOne -= 1\n posOne += 1\n end\nend\nprint(sum + zero)"}, {"source_code": "n = gets.to_i\nas = gets.split.map(&:to_i)\n\nnegative_count = as.count { |a| a < 0 }\nzero_count = as.count { |a| a == 0 }\n\nans = 0\nas.each do |a|\n ans += [(a-(-1)).abs, (a-1).abs].min\nend\nif negative_count.odd?\n if zero_count > 0\n else\n ans += 2\n end\nend\n\nputs ans\n"}, {"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\n\nn = gets.to_i\na = gets.split.map(&:to_i)\ncoins = 0\nnegs, zeroes, poss = 0, 0, 0\na.each { |i|\n if i < 0\n negs += 1\n coins += -1 - i\n elsif i == 0\n zeroes += 1\n else\n poss += 1\n coins += i - 1\n end\n}\n\n# not handled zeroes yet!\nif negs.odd?\n if zeroes > 0\n zeroes -= 1\n coins += 1 # for zeroes, flipping to -1 or +1 only costs one coin\n else\n coins += 2 # have to pay two coins to flip -1 to +1 or +1 to -1\n end\nend\n\n# handle remaining zeroes\ncoins += zeroes\n\nputs coins\n"}, {"source_code": "n = gets.to_i\naa = gets.split.map(&:to_i).sort\n\ncost = 0\nplus_sum = 0\nminus_sum = 0\nplus_cnt = 0\nminus_cnt = 0\nzero_cnt = 0\n\naa.each do |a|\n if a<0\n minus_cnt+=1\n minus_sum+=a\n elsif a==0\n zero_cnt+=1\n else\n plus_cnt+=1\n plus_sum+=a\n end\nend\n\ncost += (-minus_sum - minus_cnt) + zero_cnt + (plus_sum - plus_cnt)\n# マイナスが奇数個の場合はプラスになるように調整する\ncost += 2 if minus_cnt%2==1 && zero_cnt==0\nputs cost"}, {"source_code": "n = gets.to_i\na = gets.split.map &:to_i\nans = 0\nnnegatives = 0\nnzeros = 0\na.each do |x|\n if x >= 1\n ans += x - 1\n elsif x <= -1\n ans += -1 - x\n nnegatives += 1\n else\n ans += 1\n nzeros += 1\n end\nend\nans += 2 if nnegatives%2 != 0 and nzeros == 0\nputs ans\n"}], "negative_code": [{"source_code": "n = gets.to_i\nas = gets.split.map(&:to_i)\n\nnegative_count = as.count { |a| a < 0 }\nzero_count = as.count { |a| a == 0 }\n\nans = 0\nas.each do |a|\n ans += [(a-(-1)).abs, (a-1).abs].min\nend\nif negative_count.odd?\n if zero_count > 0\n ans += 1\n else\n ans += 2\n end\nend\n\nputs ans\n"}, {"source_code": "n = gets.to_i\nas = gets.split.map(&:to_i)\n\nnegative_count = as.count { |a| a < 0 }\n\nans = 0\nas.each do |a|\n ans += [(a-(-1)).abs, (a-1).abs].min\nend\nif negative_count.odd?\n ans += 2\nend\n\nputs ans\n"}, {"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\n\nn = gets.to_i\na = gets.split.map(&:to_i)\ncoins = 0\nnegs = 0\nmin_spent = 10**9 + 1\na.sort.each { |i|\n pending_coins = 0 # keep positive!\n if i < 0\n negs += 1\n pending_coins = -1 - i\n else\n if i == 0\n pending_coins = 1\n else\n pending_coins = i - 1\n end\n end\n\n coins += pending_coins\n if pending_coins < min_spent\n min_spent = pending_coins\n end\n}\n\nif negs.odd?\n if min_spent == 0 # is +1 or -1\n coins += 2\n elsif min_spent == 1 # is zero\n coins += 1\n else\n coins += 2\n end\nend\n\nputs coins"}, {"source_code": "n = gets.to_i\na = gets.split.map &:to_i\nans = 0\nnnegatives = 0\na.each do |x|\n if x >= 1\n ans += x - 1\n elsif x <= -1\n ans += -1 - x\n nnegatives += 1\n else\n ans += 1\n end\nend\nans += 2 if nnegatives%2 != 0\nputs ans\n"}], "src_uid": "3b3b2408609082fa5c3a0d55bb65d29a"} {"nl": {"description": "After a long party Petya decided to return home, but he turned out to be at the opposite end of the town from his home. There are $$$n$$$ crossroads in the line in the town, and there is either the bus or the tram station at each crossroad.The crossroads are represented as a string $$$s$$$ of length $$$n$$$, where $$$s_i = \\texttt{A}$$$, if there is a bus station at $$$i$$$-th crossroad, and $$$s_i = \\texttt{B}$$$, if there is a tram station at $$$i$$$-th crossroad. Currently Petya is at the first crossroad (which corresponds to $$$s_1$$$) and his goal is to get to the last crossroad (which corresponds to $$$s_n$$$).If for two crossroads $$$i$$$ and $$$j$$$ for all crossroads $$$i, i+1, \\ldots, j-1$$$ there is a bus station, one can pay $$$a$$$ roubles for the bus ticket, and go from $$$i$$$-th crossroad to the $$$j$$$-th crossroad by the bus (it is not necessary to have a bus station at the $$$j$$$-th crossroad). Formally, paying $$$a$$$ roubles Petya can go from $$$i$$$ to $$$j$$$ if $$$s_t = \\texttt{A}$$$ for all $$$i \\le t < j$$$. If for two crossroads $$$i$$$ and $$$j$$$ for all crossroads $$$i, i+1, \\ldots, j-1$$$ there is a tram station, one can pay $$$b$$$ roubles for the tram ticket, and go from $$$i$$$-th crossroad to the $$$j$$$-th crossroad by the tram (it is not necessary to have a tram station at the $$$j$$$-th crossroad). Formally, paying $$$b$$$ roubles Petya can go from $$$i$$$ to $$$j$$$ if $$$s_t = \\texttt{B}$$$ for all $$$i \\le t < j$$$.For example, if $$$s$$$=\"AABBBAB\", $$$a=4$$$ and $$$b=3$$$ then Petya needs: buy one bus ticket to get from $$$1$$$ to $$$3$$$, buy one tram ticket to get from $$$3$$$ to $$$6$$$, buy one bus ticket to get from $$$6$$$ to $$$7$$$. Thus, in total he needs to spend $$$4+3+4=11$$$ roubles. Please note that the type of the stop at the last crossroad (i.e. the character $$$s_n$$$) does not affect the final expense.Now Petya is at the first crossroad, and he wants to get to the $$$n$$$-th crossroad. After the party he has left with $$$p$$$ roubles. He's decided to go to some station on foot, and then go to home using only public transport.Help him to choose the closest crossroad $$$i$$$ to go on foot the first, so he has enough money to get from the $$$i$$$-th crossroad to the $$$n$$$-th, using only tram and bus tickets.", "input_spec": "Each test contains one or more test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 10^4$$$). The first line of each test case consists of three integers $$$a, b, p$$$ ($$$1 \\le a, b, p \\le 10^5$$$) — the cost of bus ticket, the cost of tram ticket and the amount of money Petya has. The second line of each test case consists of one string $$$s$$$, where $$$s_i = \\texttt{A}$$$, if there is a bus station at $$$i$$$-th crossroad, and $$$s_i = \\texttt{B}$$$, if there is a tram station at $$$i$$$-th crossroad ($$$2 \\le |s| \\le 10^5$$$). It is guaranteed, that the sum of the length of strings $$$s$$$ by all test cases in one test doesn't exceed $$$10^5$$$.", "output_spec": "For each test case print one number — the minimal index $$$i$$$ of a crossroad Petya should go on foot. The rest of the path (i.e. from $$$i$$$ to $$$n$$$ he should use public transport).", "sample_inputs": ["5\n2 2 1\nBB\n1 1 1\nAB\n3 2 8\nAABBBBAABB\n5 3 4\nBBBBB\n2 1 1\nABABAB"], "sample_outputs": ["2\n1\n3\n1\n6"], "notes": null}, "positive_code": [{"source_code": "T = gets.to_i\n\nT.times do\n a, b, m = gets.split.map(&:to_i)\n s = 'T' + gets.chomp\n n = s.size\n\n cost = 0\n cur = s[n - 2]\n min_idx = n - 1\n\n (n - 2).downto(0) do |idx|\n kind = s[idx]\n\n if idx == 0 || cur != kind\n if cur == 'A'\n cost += a\n else\n cost += b\n end\n\n if cost <= m\n min_idx = idx + 1\n end\n\n cur = kind\n end\n end\n\n puts min_idx\nend\n"}], "negative_code": [], "src_uid": "0e4c297fcacdd0a304310882cd7c8a44"} {"nl": {"description": "Returning back to problem solving, Gildong is now studying about palindromes. He learned that a palindrome is a string that is the same as its reverse. For example, strings \"pop\", \"noon\", \"x\", and \"kkkkkk\" are palindromes, while strings \"moon\", \"tv\", and \"abab\" are not. An empty string is also a palindrome.Gildong loves this concept so much, so he wants to play with it. He has $$$n$$$ distinct strings of equal length $$$m$$$. He wants to discard some of the strings (possibly none or all) and reorder the remaining strings so that the concatenation becomes a palindrome. He also wants the palindrome to be as long as possible. Please help him find one.", "input_spec": "The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n \\le 100$$$, $$$1 \\le m \\le 50$$$) — the number of strings and the length of each string. Next $$$n$$$ lines contain a string of length $$$m$$$ each, consisting of lowercase Latin letters only. All strings are distinct.", "output_spec": "In the first line, print the length of the longest palindrome string you made. In the second line, print that palindrome. If there are multiple answers, print any one of them. If the palindrome is empty, print an empty line or don't print this line at all.", "sample_inputs": ["3 3\ntab\none\nbat", "4 2\noo\nox\nxo\nxx", "3 5\nhello\ncodef\norces", "9 4\nabab\nbaba\nabcd\nbcde\ncdef\ndefg\nwxyz\nzyxw\nijji"], "sample_outputs": ["6\ntabbat", "6\noxxxxo", "0", "20\nababwxyzijjizyxwbaba"], "notes": "NoteIn the first example, \"battab\" is also a valid answer.In the second example, there can be 4 different valid answers including the sample output. We are not going to provide any hints for what the others are.In the third example, the empty string is the only valid palindrome string."}, "positive_code": [{"source_code": "n, m = *gets.strip.split.map(&:to_i)\n\nstrs = []\n\nn.times do\n strs << gets.strip\nend\n\nif n == 1\n unless strs[0] == strs[0].reverse\n puts 0\n exit\n end\n puts m\n puts strs[0]\n exit\nend\n\npalindroms = {}\npalindroms_indexes = []\nmiddle_word = \"\"\n\nstrs.each_with_index do |s, i|\n cur_pal = s.reverse\n p_count = 0\n next if palindroms.keys.include?(s) || palindroms.keys.include?(cur_pal)\n\n p_hash = {\n similar_count: 1,\n p_count: 0\n }\n strs.each_with_index.map do |ss, ii|\n next if ii == i\n\n if ss == cur_pal\n p_hash[:p_count] += 1\n else\n if ss == s\n p_hash[:similar_count] += 1\n end\n end\n end\n\n p_count = if p_hash[:p_count] != p_hash[:similar_count]\n if s == cur_pal\n identical = p_hash[:p_count] + 1\n if identical % 2 == 0\n identical/2\n else\n if s.length > middle_word.length\n middle_word = s\n end\n p_hash[:p_count]/2\n end\n else\n [p_hash[:p_count], p_hash[:similar_count]].min\n end\n else\n p_hash[:p_count]\n end\n\n if p_count == 0\n if s == cur_pal && s.length > middle_word.length\n middle_word = s\n end\n end\n\n palindroms[s] = p_count\nend\n\nstr_begin = \"\"\nstr_end = \"\"\npalindroms.each do |k, v|\n v.times do\n str_begin += k\n str_end = k.reverse + str_end\n end\nend\n\npal = str_begin + middle_word + str_end\nif pal.empty?\n puts 0\nelse\n puts pal.length\n puts pal\nend\n"}, {"source_code": "# frozen_string_literal: true\n\n@data = $stdin.read.split\n\ndef next_token\n @data.shift\nend\n\ndef next_int\n next_token.to_i\nend\n\nn = next_int\nm = next_int\ns = n.times.map { next_token }\n\nres = 0\nleft = String.new\noutlier = ''\nn.times do |i|\n found = false\n (0...i).each do |j|\n next unless s[i] == s[j].reverse\n\n found = true\n left << s[i]\n res += 1\n end\n next if found\n\n outlier = s[i] if s[i] == s[i].reverse\nend\n\nres = left + outlier + left.reverse\nputs res.size\nputs res\n"}, {"source_code": "n, _ = gets.strip.split(' ').map(&:to_i)\n\narr = []\nh = Hash.new\n(1..n).each { |e|\n arr << gets.strip\n h[arr.last] = 1 if arr.include?(arr.last.reverse) && arr.last != arr.last.reverse\n h[arr.last] = 0 if arr.last == arr.last.reverse\n}\n\nan = []\nh.each { |k, v|\n if v.zero?\n an << k\n break \n end\n}\nh.each { |k, v|\n if v == 1\n an.unshift(k)\n an << k.reverse\n end\n}\n\ns = \"\"\nan.each { |e| s += e }\nputs s.size\nputs s\n"}, {"source_code": "n, _ = gets.strip.split(' ').map(&:to_i)\n\narr = []\nh = Hash.new\n(1..n).each { |e|\n arr << gets.strip\n h[arr.last] = 1 if arr.include?(arr.last.reverse) && arr.last != arr.last.reverse\n h[arr.last] = 0 if arr.last == arr.last.reverse\n}\n\nan = []\nh.each { |k, v|\n if v.zero?\n an << k\n break\n end\n}\n\nh.each { |k, v|\n if v == 1\n an.unshift(k)\n an << k.reverse\n end\n}\n\ns = \"\"\nan.each { |e| s += e }\nputs s.size\nputs s\n"}], "negative_code": [{"source_code": "n, m = *gets.strip.split.map(&:to_i)\n\nstrs = []\n\nn.times do\n strs << gets.strip\nend\n\nif n == 1\n unless strs[0] == strs[0].reverse\n puts 0\n exit\n end\n puts n\n puts strs[0]\n exit\nend\n\npalindroms = {}\npalindroms_indexes = []\nmiddle_word = \"\"\n\nstrs.each_with_index do |s, i|\n cur_pal = s.reverse\n p_count = 0\n next if palindroms.keys.include?(s) || palindroms.keys.include?(cur_pal)\n\n p_hash = {\n similar_count: 1,\n p_count: 0\n }\n strs.each_with_index.map do |ss, ii|\n next if ii == i\n\n if ss == cur_pal\n p_hash[:p_count] += 1\n else\n if ss == s\n p_hash[:similar_count] += 1\n end\n end\n end\n\n p_count = if p_hash[:p_count] != p_hash[:similar_count]\n if s == cur_pal\n identical = p_hash[:p_count] + 1\n if identical % 2 == 0\n identical/2\n else\n if s.length > middle_word.length\n middle_word = s\n end\n p_hash[:p_count]/2\n end\n else\n [p_hash[:p_count], p_hash[:similar_count]].min\n end\n else\n p_hash[:p_count]\n end\n\n if p_count == 0\n if s == cur_pal && s.length > middle_word.length\n middle_word = s\n end\n end\n\n palindroms[s] = p_count\nend\n\nstr_begin = \"\"\nstr_end = \"\"\npalindroms.each do |k, v|\n v.times do\n str_begin += k\n str_end = k.reverse + str_end\n end\nend\n\npal = str_begin + middle_word + str_end\nif pal.empty?\n puts 0\nelse\n puts pal.length\n puts pal\nend\n"}, {"source_code": "n, _ = gets.strip.split(' ').map(&:to_i)\n\narr = []\n(1..n).each { |e|\n arr << gets.strip\n}\n\nan = []\narr.each { |e|\n if arr.include?(e.reverse)\n an.unshift(e)\n an << e.reverse\n arr.delete(e.reverse)\n end\n}\n\ns = \"\"\nan.each { |e| s += e }\nputs s.size\nputs s\n"}], "src_uid": "554115bec46bb436a0a1ddf8c05a2d08"} {"nl": {"description": "One day a well-known sponsor of a well-known contest decided to give every participant of the contest a T-shirt as a present. A natural problem occurred: on the one hand, it is not clear how many T-shirts of what sizes should be ordered, and on the other hand, one doesn't want to order too many T-shirts (and we do not exactly paper the walls with the oversupply). After considerable brain racking and some pre-estimating, the sponsor representatives ordered a certain number of T-shirts of sizes S, M, L, XL and XXL. The T-shirts turned out to bring good luck, that's why on the contest day there built up a line of K participants willing to get one. Every contestant is characterized by his/her desired T-shirt size (so it happens that for all the participants it is also one of the sizes S, M, L, XL and XXL). The participants come up to get a T-shirt one by one and try to choose the most suitable one, choosing it like this. If there is still a T-shirt of the optimal size left, that he/she takes it without further ado. Otherwise the contestant would prefer to choose a T-shirt with the size as close to the optimal one as possible (the distance between neighboring sizes is considered equal to one). If the variant of choice is not unique, the contestant will take a T-shirt of a bigger size (in case he/she grows more). For example, for a person whose optimal size is L the preference list looks like this: L, XL, M, XXL, S. Using the data on how many T-shirts of every size had been ordered by the organizers, on the size of contestants in the line determine who got a T-shirt of what size.", "input_spec": "The first line contains five non-negative integers NS, NM, NL, NXL, NXXL not exceeding 1000 which represent the number of T-shirts of the corresponding sizes. The second line contains an integer K (1 ≤ K ≤ 1000) which represents the number of participants. The next K lines contain the optimal T-shirt sizes for the contestants. The sizes are given in the order in which the participants stand in the line. It is guaranteed that NS + NM + NL + NXL + NXXL ≥ K.", "output_spec": "For each contestant, print a line containing the size of the T-shirt he/she got.", "sample_inputs": ["1 0 2 0 1\n3\nXL\nXXL\nM"], "sample_outputs": ["XXL\nL\nL"], "notes": null}, "positive_code": [{"source_code": "$s=gets.split.map(&:to_i)\nsize=[\"S\", \"M\", \"L\", \"XL\",\"XXL\"]\ndef get(x)\n for u in 0..5\n if x+u<5 && $s[x+u]>0 then\n $s[x+u]-=1\n return x+u\n end\n if x-u>=0 && $s[x-u]>0 then\n $s[x-u]-=1\n return x-u\n end\n end\n return 0\nend\nk=gets.to_i\nk.times{\n puts size[get(size.index(gets.split[0]))]\n}\n"}, {"source_code": "\ndef f(n)\n l = [nil] * 5 + ['S','M','L','XL','XXL'] + [nil] * 5\n i = n+4\n r = []\n r << l[i]\n r << l[i+1]\n r << l[i-1]\n r << l[i+2]\n r << l[i-2]\n r << l[i+3]\n r << l[i-3]\n r << l[i+4]\n r << l[i-4]\n r << l[i+5]\n r << l[i-5]\n r.select {|e| e}\nend\n\npr = []\nl = ['S','M','L','XL','XXL']\n1.upto(5) {|n| pr << f(n)}\n\n\n\n\na = gets.split(' ')\na = a.map{|e| e.to_i}\n\ngets.to_i.times do\n size = gets.chomp\n tl = pr[l.index(size)]\n n = 0\n while a[l.index(tl[n])] == 0\n n += 1\n end\n a[l.index(tl[n])] -= 1\n puts tl[n]\nend\n\n\n"}, {"source_code": "s = gets.split.map(&:to_i)+[0]*5\nn = gets.to_i\nz = {\"S\"=>0,\"M\"=>1,\"L\"=>2,\"XL\"=>3,\"XXL\"=>4}\nzz = [\"S\",\"M\",\"L\",\"XL\",\"XXL\"]\nn.times{\ni = z[gets.chomp]\n x=[0,1,-1,2,-2,3,-3,4,-4].select{|x|s[i+x]>0}\n x=x[0]\n puts zz[i+x]\n s[i+x]-=1\n}"}, {"source_code": "s = gets.chomp.split(\" \")\nmas = [s[0].to_i,s[1].to_i, s[2].to_i, s[3].to_i, s[4].to_i]\nindex = {\"S\" => 0, \"M\" => 1, \"L\" => 2, \"XL\" => 3, \"XXL\" => 4}\nrindex = [\"S\",\"M\",\"L\",\"XL\",\"XXL\"]\n\nn = gets.to_i\nfor i in 1..n\n s = gets.chomp!\n for j in 0..4\n if index[s] + j < 5 && mas[index[s] + j] > 0\n mas[index[s] + j] -= 1\n puts rindex[index[s] + j]\n break\n elsif index[s] - j >= 0 && mas[index[s] - j] > 0\n mas[index[s] - j] -= 1\n puts rindex[index[s] - j]\n break\n end\n end\nend"}, {"source_code": "SIZES = [ :S, :M, :L, :XL, :XXL ]\nOFFSET = [ 0, +1, -1, +2, -2, +3, -3, +4, -4 ]\n\nsizes = {}\ngets.split.map { |v| v.to_i }.each_with_index { |how, index| sizes[SIZES[index]] = how }\ngets.to_i.times do\n index = SIZES.find_index(gets.strip.to_sym)\n OFFSET.each do |offset|\n cur, size = index + offset, SIZES[index + offset]\n if (0 ... SIZES.size).include?(cur) and sizes[size] > 0\n sizes[size] -= 1\n puts size\n break\n end\n end\nend\n"}], "negative_code": [{"source_code": "\ndef f(n)\n l = [nil] * 5 + ['S','M','L','XL','XXL'] + [nil] * 5\n i = n+5\n r = []\n r << l[i]\n r << l[i+1]\n r << l[i-1]\n r << l[i+2]\n r << l[i-2]\n r << l[i+3]\n r << l[i-3]\n r << l[i+4]\n r << l[i-4]\n r << l[i+5]\n r << l[i-5]\n r.select {|e| e}\nend\n\npr = []\nl = ['S','M','L','XL','XXL']\n1.upto(5) {|n| pr << f(n)}\n\n\n\n\na = gets.split(' ')\na = a.map{|e| e.to_i}\n\ngets.to_i.times do\n size = gets.chomp\n tl = pr[l.index(size)]\n n = 0\n while a[l.index(tl[n])] == 0\n n += 1\n end\n a[l.index(tl[n])] -= 1\n puts tl[n]\nend\n\n\n"}, {"source_code": "\ndef f(n)\n l = [nil] * 5 + ['S','M','L','XL','XXL'] + [nil] * 5\n i = n+5\n r = []\n r << l[i]\n r << l[i+1]\n r << l[i-1]\n r << l[i+2]\n r << l[i-2]\n r << l[i+3]\n r << l[i-3]\n r << l[i+4]\n r << l[i-4]\n r << l[i+5]\n r << l[i-5]\n r.select {|e| e}\nend\n\npr = []\nl = ['S','M','L','XL','XXL']\n1.upto(5) {|n| pr << f(n)}\n\n\n\n\na = gets.split(' ')\na = a.map{|e| e.to_i}\n\ngets.to_i.times do\n size = gets.chomp\n tl = pr[l.index(size)]\n n = 0\n while a[l.index(tl[n])] == 0\n n += 1\n end\n a[l.index(tl[n])] -= 1\n puts tl[n]\nend\n\n\n"}, {"source_code": "s = gets.chomp.split(\" \")\nmas = [s[0].to_i,s[1].to_i, s[2].to_i, s[3].to_i, s[4].to_i]\nindex = {\"S\" => 0, \"M\" => 1, \"L\" => 2, \"XL\" => 3, \"XXL\" => 4}\nrindex = [\"S\",\"M\",\"L\",\"XL\",\"XXL\"]\n\nn = gets.to_i\nfor i in 1..n\n s = gets.chomp!\n if mas[index[s]] > 0\n mas[index[s]] -= 1\n puts rindex[index[s]]\n elsif index[s] + 1 < 5 && mas[index[s] + 1] > 0\n mas[index[s] + 1] -= 1\n puts rindex[index[s] + 1]\n elsif index[s] - 1 >= 0 && mas[index[s] - 1] > 0\n mas[index[s] - 1] -= 1\n puts rindex[index[s] - 1]\n elsif index[s] + 2 < 5 && mas[index[s] + 2] > 0\n mas[index[s] + 2] -= 1\n puts rindex[index[s] + 2]\n elsif index[s] - 2 >= 0 && mas[index[s] -2] > 0\n mas[index[s] - 2] -= 1\n puts rindex[index[s] -2]\n end\nend"}], "src_uid": "3c9d1de13e21ed16a7e5cbd2d67f4ce7"} {"nl": {"description": "You have a garden consisting entirely of grass and weeds. Your garden is described by an n × m grid, with rows numbered 1 to n from top to bottom, and columns 1 to m from left to right. Each cell is identified by a pair (r, c) which means that the cell is located at row r and column c. Each cell may contain either grass or weeds. For example, a 4 × 5 garden may look as follows (empty cells denote grass): You have a land-mower with you to mow all the weeds. Initially, you are standing with your lawnmower at the top-left corner of the garden. That is, at cell (1, 1). At any moment of time you are facing a certain direction — either left or right. And initially, you face right.In one move you can do either one of these:1) Move one cell in the direction that you are facing. if you are facing right: move from cell (r, c) to cell (r, c + 1) if you are facing left: move from cell (r, c) to cell (r, c - 1) 2) Move one cell down (that is, from cell (r, c) to cell (r + 1, c)), and change your direction to the opposite one. if you were facing right previously, you will face left if you were facing left previously, you will face right You are not allowed to leave the garden. Weeds will be mowed if you and your lawnmower are standing at the cell containing the weeds (your direction doesn't matter). This action isn't counted as a move.What is the minimum number of moves required to mow all the weeds?", "input_spec": "The first line contains two integers n and m (1 ≤ n, m ≤ 150) — the number of rows and columns respectively. Then follow n lines containing m characters each — the content of the grid. \"G\" means that this cell contains grass. \"W\" means that this cell contains weeds. It is guaranteed that the top-left corner of the grid will contain grass.", "output_spec": "Print a single number — the minimum number of moves required to mow all the weeds.", "sample_inputs": ["4 5\nGWGGW\nGGWGG\nGWGGG\nWGGGG", "3 3\nGWW\nWWW\nWWG", "1 1\nG"], "sample_outputs": ["11", "7", "0"], "notes": "NoteFor the first example, this is the picture of the initial state of the grid: A possible solution is by mowing the weeds as illustrated below: "}, "positive_code": [{"source_code": "# -*- coding: utf-8 -*-\n#require 'awesome_print'\n\nn, m = gets.split.map(&:to_i)\ngrid = []\nn.times {|line|\n s = gets.chomp\n l = s.index 'W'\n r = s.rindex 'W'\n grid << [l, r, line]\n}\n\ndef f(line, i, n, m, grid)\n cur_l, cur_r, _ = grid[line]\n below_l, below_r, next_i = grid[line+1...n].find { |_, x, _| ! x.nil? }\n count = 0\n if line.even?\n #右へ\n if !cur_r.nil? \n count += cur_r - i\n i = cur_r \n end\n else\n #左へ\n if !cur_l.nil? \n count += i - cur_l\n i = cur_l\n end\n end\n if ! below_r.nil?\n #下にいきます\n if next_i.even?\n #下の方も右\n count += i-below_l if below_l < i\n count += next_i - line + f(next_i, [below_l, i].min, n, m, grid)\n else\n #下は左\n count += below_r-i if i < below_r \n count += next_i - line + f(next_i, [below_r, i].max, n, m, grid) \n end\n end \n \n count\nend\nputs f(0, 0, n, m, grid)\n"}, {"source_code": "n,m = gets.chomp.split(/ /).map!{|x| x.to_i}\na = Array.new(n)\nidxs = Array.new(n)\nridxs = Array.new(n)\nfor i in 0..n-1\n a[i] = gets.chomp\n if i % 2 == 0\n idxs[i] = a[i].rindex(\"W\")\n ridxs[i] = a[i].index(\"W\")\n else\n idxs[i] = a[i].index(\"W\")\n ridxs[i] = a[i].rindex(\"W\")\n end\nend\nwcnt = 0\ncurcol = 0\nendidx = -1\nfor i in 0..n-1\n break if idxs[n-1-i] != nil\n endidx = n-1-i\nend\nfor i in 0..n-1\n if idxs[i] != nil\n wcnt += (curcol-idxs[i]).abs\n curcol = idxs[i]\n end\n break if endidx != -1 && i+1 >= endidx \n if i < n-1\n if ridxs[i+1] != nil\n if (i % 2 == 0 && ridxs[i+1] > curcol) || (i % 2 == 1 && ridxs[i+1] < curcol)\n wcnt += (curcol-ridxs[i+1]).abs\n curcol = ridxs[i+1]\n end\n end\n wcnt += 1\n end\nend\nputs wcnt"}], "negative_code": [{"source_code": "# -*- coding: utf-8 -*-\nclass P < Struct.new(:l, :r)\n \nend\nn, m = gets.split.map(&:to_i)\ngrid = []\nn.times {|line|\n s = gets.chomp\n l = s.index 'W'\n r = s.rindex 'W'\n grid << [l, r, line]\n}\n\ndef f(line, i, n, m, grid)\n cur_l, cur_r, _ = grid[line]\n below_l, below_r, next_i = grid[line+1...m].find { |_, x, _| ! x.nil? }\n count = 0\n if line.even?\n #右へ\n if !cur_r.nil? \n count += cur_r - i\n i = cur_r \n end\n else\n #左へ\n if !cur_l.nil? \n count += i - cur_l\n i = cur_l\n end\n end\n if ! below_r.nil?\n #下にいきます\n if next_i.even?\n #下の方も右\n count += i-below_l if below_l < i\n count += next_i - line + f(next_i, [below_l, i].min, n, m, grid)\n else\n #下は左\n count += below_r-i if i < below_r \n count += next_i - line + f(next_i, [below_r, i].max, n, m, grid) \n end\n end \n \n count\nend\nputs f(0, 0, n, m, grid)\n"}, {"source_code": "n,m = gets.chomp.split(/ /).map!{|x| x.to_i}\na = Array.new(n)\nidxs = Array.new(n)\nfor i in 0..n-1\n a[i] = gets.chomp\n if i % 2 == 0\n idxs[i] = a[i].rindex(\"W\")\n else\n idxs[i] = a[i].index(\"W\")\n end\nend\nwcnt = 0\ncurcol = 0\nfor i in 0..n-1\n if idxs[i] != nil\n wcnt += (curcol-idxs[i]).abs\n curcol = idxs[i]\n end\n if i < n-1\n if idxs[i+1] != nil\n if (i % 2 == 0 && idxs[i+1] > curcol) || (i % 2 == 1 && idxs[i+1] < curcol)\n wcnt += (curcol-idxs[i+1]).abs\n curcol = idxs[i+1]\n end\n end\n wcnt += 1\n end\nend\nputs wcnt"}, {"source_code": "n,m = gets.chomp.split(/ /).map!{|x| x.to_i}\na = Array.new(n)\nidxs = Array.new(n)\nridxs = Array.new(n)\nfor i in 0..n-1\n a[i] = gets.chomp\n if i % 2 == 0\n idxs[i] = a[i].rindex(\"W\")\n ridxs[i] = a[i].index(\"W\")\n else\n idxs[i] = a[i].index(\"W\")\n ridxs[i] = a[i].rindex(\"W\")\n end\nend\nwcnt = 0\ncurcol = 0\nfor i in 0..n-1\n if idxs[i] != nil\n wcnt += (curcol-idxs[i]).abs\n curcol = idxs[i]\n end\n if i < n-1\n if ridxs[i+1] != nil\n if (i % 2 == 0 && ridxs[i+1] > curcol) || (i % 2 == 1 && ridxs[i+1] < curcol)\n wcnt += (curcol-ridxs[i+1]).abs\n curcol = ridxs[i+1]\n end\n end\n wcnt += 1\n end\nend\nputs wcnt"}], "src_uid": "14959b7266ceebe96c33bfa1791e26b4"} {"nl": {"description": "You are given a rectangular grid with $$$n$$$ rows and $$$m$$$ columns. The cell located on the $$$i$$$-th row from the top and the $$$j$$$-th column from the left has a value $$$a_{ij}$$$ written in it.You can perform the following operation any number of times (possibly zero): Choose any two adjacent cells and multiply the values in them by $$$-1$$$. Two cells are called adjacent if they share a side. Note that you can use a cell more than once in different operations.You are interested in $$$X$$$, the sum of all the numbers in the grid. What is the maximum $$$X$$$ you can achieve with these operations?", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 100$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$,$$$m$$$ ($$$2 \\le n$$$, $$$m \\le 10$$$). The following $$$n$$$ lines contain $$$m$$$ integers each, the $$$j$$$-th element in the $$$i$$$-th line is $$$a_{ij}$$$ ($$$-100\\leq a_{ij}\\le 100$$$).", "output_spec": "For each testcase, print one integer $$$X$$$, the maximum possible sum of all the values in the grid after applying the operation as many times as you want.", "sample_inputs": ["2\n2 2\n-1 1\n1 1\n3 4\n0 -1 -2 -3\n-1 -2 -3 -4\n-2 -3 -4 -5"], "sample_outputs": ["2\n30"], "notes": "NoteIn the first test case, there will always be at least one $$$-1$$$, so the answer is $$$2$$$. In the second test case, we can use the operation six times to elements adjacent horizontally and get all numbers to be non-negative. So the answer is: $$$2\\times 1 + 3\\times2 + 3\\times 3 + 2\\times 4 + 1\\times 5 = 30$$$."}, "positive_code": [{"source_code": "t = gets.chomp.to_i\nt.times do\n n, m = gets.chomp.split(' ').map(&:to_i)\n arr = []\n n.times do\n arr << gets.chomp.split(' ').map(&:to_i)\n end\n arr = arr.flatten\n mn = arr.count {|el| el < 0}\n if arr.find{|el| el == 0}\n puts arr.map{|el| el.abs}.sum\n else\n arr = arr.map{|el| el.abs}\n arr = arr.sort\n if mn % 2 == 0\n puts arr.sum\n else\n puts arr[1..].sum - arr[0]\n end\n end\nend "}], "negative_code": [{"source_code": "t = gets.chomp.to_i\nt.times do\n n, m = gets.chomp.split(' ').map(&:to_i)\n arr = []\n n.times do\n arr << gets.chomp.split(' ').map(&:to_i)\n end\n arr = arr.flatten\n if arr.find{|el| el == 0}\n puts arr.map{|el| el.abs}.sum\n else\n arr = arr.map{|el| el.abs}\n arr = arr.sort\n puts (arr[0] * -1) + arr[1..].sum\n end\nend "}, {"source_code": "t = gets.chomp.to_i\nt.times do\n n, m = gets.chomp.split(' ').map(&:to_i)\n arr = []\n n.times do\n arr << gets.chomp.split(' ').map(&:to_i)\n end\n arr = arr.flatten\n if arr.find{|el| el == 0}\n puts arr.map{|el| el.abs}.sum\n else\n arr = arr.map{|el| el.abs}\n arr = arr.sort\n mn = arr.count {|el| el < 0}\n puts (arr[0..mn].sum * -1) + arr[(mn + 1)..].sum\n end\nend "}, {"source_code": "t = gets.chomp.to_i\nt.times do\n n, m = gets.chomp.split(' ').map(&:to_i)\n arr = []\n n.times do\n arr << gets.chomp.split(' ').map(&:to_i)\n end\n arr = arr.flatten\n if arr.find{|el| el == 0}\n puts arr.map{|el| el.abs}.sum\n else\n arr = arr.map{|el| el.abs}\n arr = arr.sort\n mn = arr.count {|el| el < 0}\n if mn % 2 == 0\n puts arr.sum\n else\n puts arr[1..].sum - arr[0].sum\n end\n end\nend "}], "src_uid": "7fce446de3b01aff8f4aa420a92a096d"} {"nl": {"description": "The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.The new architect is interested in n questions, i-th of them is about the following: \"how many floors should be added to the i-th house to make it luxurious?\" (for all i from 1 to n, inclusive). You need to help him cope with this task.Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).", "input_spec": "The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house. ", "output_spec": "Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one.", "sample_inputs": ["5\n1 2 3 1 2", "4\n3 2 1 4"], "sample_outputs": ["3 2 0 2 0", "2 3 4 0"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/ruby\ngets\ns=0\nputs gets.split.map(&:to_i).reverse.map{|e|\n\tr=[s+1-e,0].max\n\ts=[s,e].max\n\tr\n}.reverse*' '"}, {"source_code": "n, h = gets.chomp.to_i, gets.chomp.split(\" \").map(&:to_i)\na, max_on_the_right, index_of_max_on_the_right = \n\t [] << 0, h[n-1], n-1\n\n(n-2).downto 0 do |i|\n\tif max_on_the_right >= h[i]\n\t\ta.unshift(max_on_the_right - h[i] + 1)\n\telse\n\t\ta.unshift 0\n\t\tmax_on_the_right = h[i]\n\tend\nend\n\nputs a.join \" \""}, {"source_code": "n, h = gets.chomp.to_i, gets.chomp.split(\" \").map(&:to_i)\na, max_on_the_right = [] << 0, h[n-1]\n\n(n-2).downto 0 do |i|\n\tif max_on_the_right >= h[i]\n\t\ta.unshift(max_on_the_right - h[i] + 1)\n\telse\n\t\ta.unshift 0\n\t\tmax_on_the_right = h[i]\n\tend\nend\n\nputs a.join \" \""}, {"source_code": "#!/usr/bin/ruby\ngets\ns=0\nputs gets.split.map(&:to_i).reverse.map{|e|\n\tr=[s+1-e,0].max\n\ts=[s,e].max\n\tr\n}.reverse*' '\n"}, {"source_code": "#!/usr/bin/ruby\ngets\ns=0\nputs gets.split.map(&:to_i).reverse.map{|e|\n\tr=[s+1-e,0].max\n\ts=[s,e].max\n\tr\n}.reverse*' '\n"}, {"source_code": "\nn = gets.chomp.to_i;\narr = gets.chomp.split.map(&:to_i);\nmax = arr[n - 1];\nans = [];\n(n-2).downto(0) {|i|\n ans[i] = [max + 1 - arr[i], 0].max;\n max = [max, arr[i]].max;\n}\nans[n - 1] = 0;\nprint ans.join(' ');"}, {"source_code": "#!/usr/bin/ruby\ngets\ns=0\nputs gets.split.map(&:to_i).reverse.map{|e|\n\tr=[s+1-e,0].max\n\ts=[s,e].max\n\tr\n}.reverse*' '"}, {"source_code": "n = gets.to_i\nh = gets.split.map(&:to_i)\nm, a = [0] * (n - 1) + [h[n - 1]], [0] * n\n(n - 2).downto(0) do |i|\n if h[i] <= m[i + 1]\n m[i] = m[i + 1]\n a[i] = m[i] - h[i] + 1\n else\n m[i] = h[i]\n end\nend\nputs a.join(' ')\n"}, {"source_code": "#!/usr/bin/ruby\ngets\ns=0\nputs gets.split.map(&:to_i).reverse.map{|e|\n\tr=[s+1-e,0].max\n\ts=[s,e].max\n\tr\n}.reverse*' '\n"}, {"source_code": "#!/usr/bin/ruby\ngets\ns=0\nputs gets.split.map(&:to_i).reverse.map{|e|\n\tr=[s+1-e,0].max\n\ts=[s,e].max\n\tr\n}.reverse*' '\n"}, {"source_code": "gets\nx=gets.split.map{|e| e.to_i}.reverse\nans=[]\nx.inject(0){|m,e|\n\tif m>=e then\n\t\tans<<(m-e+1)\n\telse\n\t\tans<<0\n\tend\n\t[m,e].max\n}\nputs ans.reverse*\" \"\n\n"}, {"source_code": "n = gets.to_i\narr = gets.chomp.split(/ /).map(&:to_i)\narr_fin = Array.new\nindex=n-1\nmax_l = 0\nloop do \n\tbreak if index<0\n\tif arr[index] > max_l\n\t\tarr_fin.unshift(0)\n\t\tmax_l = arr[index]\n\telse\n\t\tarr_fin.unshift(-arr[index]+max_l+1)\n\tend\n\tindex-=1\nend\narr_fin.each_index{ |e|\n\tif(e!=n-1)\n\t\tprint \"#{arr_fin[e]} \"\n\telse\n\t\tprint \"#{arr_fin[e]}\"\n\tend\n}"}, {"source_code": "# читаем первую строку, но нигде не используем\ncount = STDIN.gets.to_i\n# количество этажей в последнем элитном доме\nlast_elite_floors = 0\ninput = STDIN.gets\n# массив - количество этажей в каждом доме\nfloors = input.split.map(&:to_i)\n# т.к. не знаю как в ruby делать проход\n# от последнего элемента к первому\n# - просто два раза инвертирую массив\n# это первый раз\nreverse = floors.reverse\nreverse_result = Array.new\nreverse.each do |item|\n if item > last_elite_floors\n # если у нас элитный дом - запомнаем количество этажей в нём\n last_elite_floors = item\n reverse_result.push(0)\n elsif item == last_elite_floors\n # если количество этажей такое же как у последнего элитного дома\n # значит нужно достроить всего один этаж\n reverse_result.push(1)\n else\n reverse_result.push(last_elite_floors - item + 1)\n end\nend\n# тут инвертируем массив второй раз\nputs \"#{reverse_result.reverse.join(' ')}\""}, {"source_code": "#!/usr/bin/ruby\ngets\ns=0\nputs gets.split.map(&:to_i).reverse.map{|e|\n\tr=[s+1-e,0].max\n\ts=[s,e].max\n\tr\n}.reverse*' '\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nright_max, ans = 0, []\n(n - 1).downto(0) do |i|\n ans << [right_max + 1 - a[i], 0].max\n right_max = [right_max, a[i]].max\nend\nputs ans.reverse.join(' ')\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nright_max, ans = 0, [0]\n(n - 1).downto(1) do |i|\n right_max = [right_max, a[i]].max\n ans << [right_max + 1 - a[i - 1], 0].max\nend\nputs ans.reverse.join(' ')\n"}, {"source_code": "#!/usr/bin/ruby\ngets\ns=0\nputs gets.split.map(&:to_i).reverse.map{|e|\n\tr=[s+1-e,0].max\n\ts=[s,e].max\n\tr\n}.reverse*' '\n"}, {"source_code": "#!/usr/bin/ruby\ngets\ns=0\nputs gets.split.map(&:to_i).reverse.map{|e|\n\tr=[s+1-e,0].max\n\ts=[s,e].max\n\tr\n}.reverse*' '\n"}, {"source_code": "#!/usr/bin/ruby\ngets\ns=0\nputs gets.split.map(&:to_i).reverse.map{|e|\n\tr=[s+1-e,0].max\n\ts=[s,e].max\n\tr\n}.reverse*' '\n"}], "negative_code": [{"source_code": "n, h, a, max_on_the_right, index_of_max_on_the_right = \n\tgets.chomp.to_i, gets.chomp.split(\" \").map(&:to_i), [], 0, 0\nh.each_with_index do |e,i|\n\tbreak if i == n-1\n\tif i == index_of_max_on_the_right\n\t\tmax_on_the_right = h[i+1]\n\t\t(i+2).upto (n-1) do |ind|\n\t\t\tif h[ind] > max_on_the_right\n\t\t\t\tmax_on_the_right = h[ind] \n\t\t\t\tindex_of_max_on_the_right = ind\n\t\t\tend\n\t\tend\n\tend\n\n\ta << if (max_on_the_right - e) < 1 \n\t\t0\n\telse\n\t\t(max_on_the_right - e + 1)\n\tend\nend\na << 0\nputs a.join \" \""}, {"source_code": "n, h = gets.chomp.to_i, gets.chomp.split(\" \").map(&:to_i)\na, max_on_the_right, index_of_max_on_the_right = \n\t [] << 0, h[n-1], n-1\n\n(n-2).downto 0 do |i|\n\tif max_on_the_right > h[i]\n\t\ta.unshift(max_on_the_right - h[i] + 1)\n\telse\n\t\ta.unshift 0\n\t\tmax_on_the_right = h[i]\n\tend\nend\n\nputs a.join \" \""}, {"source_code": "n = gets.chomp.to_i;\narr = gets.chomp.split.map(&:to_i).reverse;\nmax = arr[0];\nans = \"0\";\n(1...n).each {|i|\n ans << \" #{max + 1 - arr[i]}\";\n max = [max, arr[i]].max;\n}\nprint ans.reverse + \"\\n\";\n"}, {"source_code": "# читаем первую строку, но нигде не используем\ncount = STDIN.gets.to_i\n# количество этажей в последнем элитном доме\nlast_elite_floors = 0\ninput = STDIN.gets\n# массив - количество этажей в каждом доме\nfloors = input.split.map(&:to_i)\n# т.к. не знаю как в ruby делать проход\n# от последнего элемента к первому\n# - просто два раза инвертирую массив\n# это первый раз\nreverse = floors.reverse\nreverse_result = Array.new\nreverse.each do |item|\n if item >= last_elite_floors\n last_elite_floors = item\n reverse_result.push(0)\n else\n reverse_result.push(last_elite_floors - item + 1)\n end\nend\n# тут инвертируем массив второй раз\nputs \"#{reverse_result.reverse.join(' ')}\""}], "src_uid": "e544ed0904e2def0c1b2d91f94acbc56"} {"nl": {"description": "A map of some object is a rectangular field consisting of n rows and n columns. Each cell is initially occupied by the sea but you can cover some some cells of the map with sand so that exactly k islands appear on the map. We will call a set of sand cells to be island if it is possible to get from each of them to each of them by moving only through sand cells and by moving from a cell only to a side-adjacent cell. The cells are called to be side-adjacent if they share a vertical or horizontal side. It is easy to see that islands do not share cells (otherwise they together form a bigger island).Find a way to cover some cells with sand so that exactly k islands appear on the n × n map, or determine that no such way exists. ", "input_spec": "The single line contains two positive integers n, k (1 ≤ n ≤ 100, 0 ≤ k ≤ n2) — the size of the map and the number of islands you should form.", "output_spec": "If the answer doesn't exist, print \"NO\" (without the quotes) in a single line. Otherwise, print \"YES\" in the first line. In the next n lines print the description of the map. Each of the lines of the description must consist only of characters 'S' and 'L', where 'S' is a cell that is occupied by the sea and 'L' is the cell covered with sand. The length of each line of the description must equal n. If there are multiple answers, you may print any of them. You should not maximize the sizes of islands.", "sample_inputs": ["5 2", "5 25"], "sample_outputs": ["YES\nSSSSS\nLLLLL\nSSSSS\nLLLLL\nSSSSS", "NO"], "notes": null}, "positive_code": [{"source_code": "n,s = gets.split.map {|x| x.to_i}\nm = n*n/2+(n % 2)\nmap = Array.new(n) {\"S\"*n}\nif s <= m \n puts \"YES\"\n n.times do |y|\n offset = y % 2\n x = offset\n while (x < n) do\n break if s == 0\n map[y][x] = 'L'\n s -= 1\n x += 2\n end\n break if s == 0\n end\n map.each do |line|\n puts line\n end\n\nelse\n puts \"NO\"\nend\n"}, {"source_code": "n, k = gets.split.map {|x| x.to_i}\n\nif (n*n + 1)/2 < k\n puts \"NO\"\nelse\n puts \"YES\"\n b = []\n n.times do\n b.push(\"S\" * n)\n end\n\n for i in 0...n\n for j in 0...n\n if (i + j) % 2 == 0 && k > 0\n b[i][j] = 'L'\n k -= 1\n end\n end\n end\n b.each {|str| puts str}\nend"}, {"source_code": "#!/usr/bin/env ruby\n# -*- coding: utf-8 -*-\n\n# b.rb\n\ndef main()\n n, k = gets.chomp.split(\" \").map{|s| next s.to_i }\n m = (n * n / 2) + (n % 2 == 1 ? 1 : 0)\n if m < k then\n puts(\"NO\")\n return\n end\n\n puts(\"YES\")\n r = 0\n n.times do |i|\n s = \"S\" * n\n t = i % 2\n while r < k && t < n do\n s[t] = \"L\"\n t += 2\n r += 1\n end\n puts(s)\n end\nend\n\nif __FILE__ == $0 then\n main()\nend\n"}, {"source_code": "n, k = gets.chomp.split(\"\\s\").map(&:to_i)\nif n % 2 == 0\n if (n / 2) * n < k\n puts 'NO'\n exit\n end\nelse\n if (n / 2 + 1) ** 2 + (n / 2) ** 2 < k\n puts 'NO'\n exit\n end\nend\n\nputs 'YES'\ncount = 0\nn.times do |i|\n n.times do |j|\n if count < k && i % 2 == 0 && j % 2 == 0\n count += 1\n print 'L'\n elsif count < k && i % 2 == 1 && j % 2 == 1\n count += 1\n print 'L'\n else\n print 'S'\n end\n end\n puts\nend\n"}, {"source_code": "n,@k=gets.split.map(&:to_i)\n(puts :NO;exit)if @k > (n*n+1)/2\nputs :YES\ndef ne\n ((@k-=1)<0)?'S':'L'\nend\n\nn.times do |i|\n n.times do |j|\n print (i+j)%2==0 ? ne : 'S'\n end\n puts\nend\n"}, {"source_code": "input = gets.chomp\ninput2 = input.split(\" \")\nn = input2[0].to_i\nk = input2[1].to_i\n\n\nif n % 2 == 1\n max = (n/2+1) ** 2 + (n/2) ** 2\nelse\n max = (n/2) * n\nend\n\nif max >= k\n puts \"YES\"\n (n**2).times do |num|\n\n if num % n == 0 && num != 0\n print \"\\n\"\n end\n\n if (num / n) % 2 == 0 || (n % 2 == 1)\n if num % 2 == 0 && k != 0\n print \"L\"\n k -= 1\n else\n print \"S\"\n end\n else\n if num % 2 == 1 && k != 0\n print \"L\"\n k -= 1\n else\n print \"S\"\n end\n end\n end\nelse\n puts \"NO\"\nend"}, {"source_code": "def getMaxAmountOfIslands(n)\n n ** 2 / 2 + (n % 2 == 0 ? 0 : 1) \nend\n\ndef makeIslands(k, n)\n res = \"\"\n n.times do |i|\n n.times {|j| res += (i + j) % 2 == 0 && k > 0? (k-=1; \"L\") : \"S\"}\n res += \"\\n\"\n end\n return res\nend\n\nn, k = *gets.split(' ')\n .map {|x| x.to_i}\n .to_a\n\nif k <= getMaxAmountOfIslands(n) then\n puts \"YES\"\n puts makeIslands(k, n)\nelse\n puts \"NO\"\nend"}, {"source_code": "n, k = gets.split.collect{|i| i.to_i}\ns = Array.new(n){Array.new(n,\"S\")}\nif n.odd?\n if k > n*n/2+1\n puts \"NO\"\n exit\n end\nelse\n if k > n*n/2\n puts \"NO\"\n exit\n end\nend\ni = 0\nj = 0\nk.times do\n s[i][j] = \"L\"\n j += 2\n if j >= n\n i += 1\n j = i%2\n end\nend\nputs \"YES\"\ns.each{|str| puts str.join}\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\nans = Array.new(n){Array.new(n, 'S')}\n\ndef adjcent?(i, j, n, ans)\n if ((i > 0 && ans[i - 1][j] == 'L') ||\n (j > 0 && ans[i][j - 1] == 'L') ||\n (i < n - 1 && ans[i + 1][j] == 'L') ||\n (j < n - 1 && ans[i][j + 1] == 'L'))\n true\n else\n false\n end\nend\n\nif k > (n * n + 1) / 2\n puts 'NO'\nelse\n puts 'YES'\n count = 0\n n.times do |i|\n n.times do |j|\n if !adjcent?(i, j, n, ans) && count < k\n ans[i][j] = 'L'\n count += 1\n end\n end\n end\n puts ans.map{|x| x.join}\nend\n\n"}], "negative_code": [{"source_code": "n,s = gets.split.map {|x| x.to_i}\nm = n*n/2+(n % 2)\nmap = Array.new(n) {\"L\"*n}\nif s <= m \n puts \"YES\"\n n.times do |y|\n offset = y % 2\n x = offset\n while (x < n) do\n break if s == 0\n map[y][x] = 'S'\n s -= 1\n x += 2\n end\n break if s == 0\n end\n map.each do |line|\n puts line\n end\n\nelse\n puts \"NO\"\nend\n"}, {"source_code": "n, k = gets.chomp.split(\"\\s\").map(&:to_i)\nif n % 2 == 0\n if n / 2 < k\n puts 'NO'\n exit\n end\nelse\n if n / 2 + 1 < k\n puts 'NO'\n exit\n end\nend\n\nputs 'YES'\ncount = 0\nn.times do |i|\n if count < k && i % 2 == 0\n count += 1\n puts 'L' * n\n else\n puts 'S' * n\n end\nend\n"}, {"source_code": "n, k = gets.chomp.split(\"\\s\").map(&:to_i)\nif n % 2 == 0\n if n / 2 < k\n puts 'NO'\n exit\n end\nelse\n if n / 2 + 1 < k\n puts 'NO'\n exit\n end\nend\n\ncount = 0\nn.times do |i|\n if count < k && i % 2 == 0\n count += 1\n puts 'L' * n\n else\n puts 'S' * n\n end\nend\n"}, {"source_code": "n, k = gets.chomp.split(\"\\s\").map(&:to_i)\nif n % 2 == 0\n if n / 2 < k\n puts 'NO'\n exit\n end\nelse\n if n / 2 + 1 < k\n puts 'NO'\n exit\n end\nend\n\nif k == 0\n puts 'NO'\n exit\nend\n\nputs 'YES'\ncount = 0\nn.times do |i|\n if count < k && i % 2 == 0\n count += 1\n puts 'L' * n\n else\n puts 'S' * n\n end\nend\n"}, {"source_code": "n, k = gets.chomp.split(\"\\s\").map(&:to_i)\nif n % 2 == 0\n if (n / 2) * n < k\n puts 'NO'\n exit\n end\nelse\n if (n / 2 + 1) * n < k\n puts 'NO'\n exit\n end\nend\n\nputs 'YES'\ncount = 0\nn.times do |i|\n n.times do |j|\n if count < k && i % 2 == 0 && j % 2 == 0\n count += 1\n print 'L'\n elsif count < k && i % 2 == 1 && j % 2 == 1\n count += 1\n print 'L'\n else\n print 'S'\n end\n end\n puts\nend\n"}, {"source_code": "input = gets.chomp\ninput2 = input.split(\" \")\nn = input2[0].to_i\nk = input2[1].to_i\n\n\nif n % 2 == 1\n max = (n/2+1) ** 2 + (n/2) ** 2\nelse\n max = (n/2) * n\nend\n\nif max >= k\n\n (n**2).times do |num|\n\n if num % n == 0 && num != 0\n print \"\\n\"\n end\n\n if (num / n) % 2 == 0 || (n % 2 == 1)\n if num % 2 == 0 && k != 0\n print \"L\"\n k -= 1\n else\n print \"S\"\n end\n else\n if num % 2 == 1 && k != 0\n print \"L\"\n k -= 1\n else\n print \"S\"\n end\n end\n end\nelse\n puts \"NO\"\nend"}, {"source_code": "input = gets.chomp\ninput2 = input.split(\" \")\nn = input2[0].to_i\nk = input2[1].to_i\n\n\nif n % 2\n max = (n/2+1) ** 2 + (n/2) ** 2\nelse\n max = (n/2) * n\nend\n\nif max >= k\n puts \"YES\"\n\n (n**2).times do |num|\n\n if num % n == 0 && num != 0\n print \"\\n\"\n end\n\n if num % 2 == 0 && k != 0\n print \"L\"\n k -= 1\n else\n print \"S\"\n end\n\n end\nelse\n puts \"NO\"\nend"}, {"source_code": "input = gets.chomp\ninput2 = input.split(\" \")\nn = input2[0].to_i\nk = input2[1].to_i\n\n\nif n % 2\n max = (n/2+1) ** 2 + (n/2) ** 2\nelse\n max = (n/2) * n\nend\n\nif max >= k\n puts \"YES\"\n\n (n**2).times do |num|\n\n if num % n == 0 && num != 0\n print \"\\n\"\n end\n\n if (num / n) % 2 == 0\n if num % 2 == 0 && k != 0\n print \"L\"\n k -= 1\n else\n print \"S\"\n end\n else\n if num % 2 == 1 && k != 0\n print \"L\"\n k -= 1\n else\n print \"S\"\n end\n end\n\n\n end\nelse\n puts \"NO\"\nend"}, {"source_code": "input = gets.chomp\ninput2 = input.split(\" \")\nn = input2[0].to_i\nk = input2[1].to_i\n\n\nif n % 2\n max = (n/2+1) ** 2 + (n/2) ** 2\nelse\n max = (n/2) * n\nend\n\nif max >= k\n puts \"YES\"\n\n (n**2).times do |num|\n\n if num % n == 0 && num != 0\n print \"\\n\"\n end\n\n if (num / n) % 2 == 0 || (n % 2 == 1)\n if num % 2 == 0 && k != 0\n print \"L\"\n k -= 1\n else\n print \"S\"\n end\n else\n if num % 2 == 1 && k != 0\n print \"L\"\n k -= 1\n else\n print \"S\"\n end\n end\n end\nelse\n puts \"NO\"\nend"}, {"source_code": "def getMaxAmountOfIslands(n)\n n ** 2 / 2 + (n % 2 == 0 ? 0 : 1) \nend\n\ndef makeIslands(k, n)\n res = \"\"\n (n ** 2).times do |i|\n shift = (i / n) % 2\n res += (i + shift) % 2 == 0 && i / 2 < k ? \"L\" : \"S\"\n res += \"\\n\" if (i+1) % n == 0 && i > 0\n end\n \n return res\nend\n\nn, k = *gets.split(' ')\n .map {|x| x.to_i}\n .to_a\n\nif k <= getMaxAmountOfIslands(n) then\n puts \"YES\"\n puts makeIslands(k, n)\nelse\n puts \"NO\"\nend"}, {"source_code": "def getMaxAmountOfIslands(n)\n n ** 2 / 2 + (n % 2 == 0 ? 0 : 1) \nend\n\ndef makeIslands(k, n)\n res = \"\"\n (n ** 2).times do |i|\n res += i % 2 == 0 && i / 2 < k ? \"L\" : \"S\"\n res += \"\\n\" if (i+1) % n == 0 && i > 0\n end\n \n return res\nend\n\nn, k = *gets.split(' ')\n .map {|x| x.to_i}\n .to_a\n\nif k <= getMaxAmountOfIslands(n) then\n puts \"YES\"\n puts makeIslands(k, n)\nelse\n puts \"NO\"\nend"}, {"source_code": "n, k = gets.split.collect{|i| i.to_i}\ns = Array.new(n){Array.new(n,\"S\")}\nif n.odd?\n if k > n*n/2+1\n puts \"NO\"\n exit\n end\nelse\n if k > n*n/2\n puts \"NO\"\n exit\n end\nend\ni = 0\nj = 0\nk.times do\n s[i][j] = \"L\"\n j += 2\n if j >= n\n i += 1\n j = i%2\n end\nend\nputs \"YES\"\ns.each{|str| puts str.join(\" \")}\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\nans = Array.new(n){Array.new(n, 'S')}\n\ndef adjcent?(i, j, n, ans)\n if ((i > 0 && ans[i - 1][j] == 'L') ||\n (j > 0 && ans[i][j - 1] == 'L') ||\n (i < n - 1 && ans[i + 1][j] == 'L') ||\n (j < n - 1 && ans[i][j + 1] == 'L'))\n true\n else\n false\n end\nend\n\nif k > (n + 1) / 2\n puts 'NO'\nelse\n puts 'YES'\n count = 0\n n.times do |i|\n n.times do |j|\n if !adjcent?(i, j, n, ans) && count < k\n ans[i][j] = 'L'\n count += 1\n end\n end\n end\n puts ans.map{|x| x.join}\nend\n\n"}], "src_uid": "b15bc7ff01f239a7e4377868a7dda0a6"} {"nl": {"description": "Polycarp has built his own web service. Being a modern web service it includes login feature. And that always implies password security problems.Polycarp decided to store the hash of the password, generated by the following algorithm: take the password $$$p$$$, consisting of lowercase Latin letters, and shuffle the letters randomly in it to obtain $$$p'$$$ ($$$p'$$$ can still be equal to $$$p$$$); generate two random strings, consisting of lowercase Latin letters, $$$s_1$$$ and $$$s_2$$$ (any of these strings can be empty); the resulting hash $$$h = s_1 + p' + s_2$$$, where addition is string concatenation. For example, let the password $$$p =$$$ \"abacaba\". Then $$$p'$$$ can be equal to \"aabcaab\". Random strings $$$s1 =$$$ \"zyx\" and $$$s2 =$$$ \"kjh\". Then $$$h =$$$ \"zyxaabcaabkjh\".Note that no letters could be deleted or added to $$$p$$$ to obtain $$$p'$$$, only the order could be changed.Now Polycarp asks you to help him to implement the password check module. Given the password $$$p$$$ and the hash $$$h$$$, check that $$$h$$$ can be the hash for the password $$$p$$$.Your program should answer $$$t$$$ independent test cases.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. The first line of each test case contains a non-empty string $$$p$$$, consisting of lowercase Latin letters. The length of $$$p$$$ does not exceed $$$100$$$. The second line of each test case contains a non-empty string $$$h$$$, consisting of lowercase Latin letters. The length of $$$h$$$ does not exceed $$$100$$$.", "output_spec": "For each test case print the answer to it — \"YES\" if the given hash $$$h$$$ could be obtained from the given password $$$p$$$ or \"NO\" otherwise.", "sample_inputs": ["5\nabacaba\nzyxaabcaabkjh\nonetwothree\nthreetwoone\none\nzzonneyy\none\nnone\ntwenty\nten"], "sample_outputs": ["YES\nYES\nNO\nYES\nNO"], "notes": "NoteThe first test case is explained in the statement.In the second test case both $$$s_1$$$ and $$$s_2$$$ are empty and $$$p'=$$$ \"threetwoone\" is $$$p$$$ shuffled.In the third test case the hash could not be obtained from the password.In the fourth test case $$$s_1=$$$ \"n\", $$$s_2$$$ is empty and $$$p'=$$$ \"one\" is $$$p$$$ shuffled (even thought it stayed the same). In the fifth test case the hash could not be obtained from the password."}, "positive_code": [{"source_code": "t = gets.chomp.to_i\n\nt.times do |ti|\n s = gets.chomp\n pnew = gets.chomp\n letter_count = {}\n s.each_char do |i|\n letter_count[i] = letter_count[i].to_i + 1\n end\n \n window_len = (s.length) -1\n count = 0\n found = false\n \n while count < pnew.length\n new_s = pnew[count..window_len]\n new_h = {}\n new_s.each_char do |i|\n new_h[i] = new_h[i].to_i + 1\n end\n count+=1\n window_len+=1\n if new_h == letter_count\n found = true\n puts \"YES\"\n break;\n end\n end\n \n if !found\n puts \"NO\"\n end\nend"}, {"source_code": "t = gets.chomp.to_i\n\nt.times do |ti|\n p = gets.chomp\n h = gets.chomp\n i = 0\n j = i + p.length - 1\n found = false\n while j < h.length\n if h[i..j].chars.sort == p.chars.sort\n found = true\n puts \"YES\"\n break\n else\n i += 1\n j += 1\n end\n end\n unless found\n puts \"NO\"\n end\nend"}, {"source_code": "gets.strip.to_i.times do\n p = gets.strip\n h = gets.strip\n if p.length > h.length\n puts \"NO\"\n next\n end\n p = p.chars.sort.join\n ck = false\n (0...h.length).each { |i| \n break if i + p.length > h.length\n s = h[i, p.length]\n s = s.chars.sort.join\n if s == p\n ck = true\n break\n end\n }\n puts ck ? \"YES\" : \"NO\"\nend"}, {"source_code": "gets.strip.to_i.times do\n p = gets.split('').reject { |e| e == \"\\n\" };\n h = gets.split('').reject { |e| e == \"\\n\" };\n if p.length > h.length\n puts \"NO\"\n next\n end\n done = false\n (0...h.length).each { |i|\n frq = Array.new(27, 0);\n sm = p.length\n p.each { |e| frq[e.ord - ?a.ord] += 1 }\n flg = true\n for j in i ... h.length\n if frq[h[j].ord - ?a.ord] == 0\n flg = false\n break\n else\n frq[h[j].ord - ?a.ord] -= 1\n break if frq.count(0) == frq.length\n end\n end\n if flg && frq.count(0) == frq.length\n puts \"YES\"\n done = true\n break\n end\n }\n puts \"NO\" if !done\nend"}, {"source_code": "gets.strip.to_i.times do\n p = gets.strip\n h = gets.strip\n if p.length > h.length\n puts \"NO\"\n next\n end\n p = p.chars.sort.join\n ck = false\n (0...h.length).each { |i| \n break if i + p.length > h.length\n s = h[i, p.length]\n s = s.chars.sort.join\n if s == p\n ck = true\n break\n end\n }\n puts ck ? \"YES\" : \"NO\"\nend"}, {"source_code": "gets.strip.to_i.times do\n p = gets.split('').reject { |e| e == \"\\n\" };\n h = gets.split('').reject { |e| e == \"\\n\" };\n if p.length > h.length\n puts \"NO\"\n next\n end\n done = false\n (0...h.length).each { |i|\n frq = Array.new(27, 0);\n sm = p.length\n p.each { |e| frq[e.ord - ?a.ord] += 1 }\n flg = true\n for j in i ... h.length\n if frq[h[j].ord - ?a.ord] == 0\n flg = false\n break\n else\n frq[h[j].ord - ?a.ord] -= 1\n break if frq.count(0) == frq.length\n end\n end\n if flg && frq.count(0) == frq.length\n puts \"YES\"\n done = true\n break\n end\n }\n puts \"NO\" if !done\nend"}], "negative_code": [{"source_code": "t = gets.chomp.to_i\n\nt.times do |ti|\n p = gets.chomp\n h = gets.chomp\n i = 0\n j = i + p.length - 1\n found = false\n while j < h.length\n if (h[i..j].split('') - p.split('')).length == 0 && (p.split('') - h[i..j].split('')).length == 0\n found = true\n puts \"YES\"\n break\n else\n i += 1\n j += 1\n end\n end\n unless found\n puts \"NO\"\n end\nend"}, {"source_code": "t = gets.chomp.to_i\n\nt.times do |ti|\n p = gets.chomp\n h = gets.chomp\n i = 0\n j = i + p.length - 1\n while j < h.length\n if (h[i..j].split('') - p.split('')).length == 0 && (p.split('') - h[i..j].split('')).length == 0\n puts \"YES\"\n break\n else\n i += 1\n j += 1\n end\n end\n if j == h.length - 1\n puts \"NO\"\n end\nend"}, {"source_code": "gets.strip.to_i.times do\n p = gets.split('').reject { |e| e == \"\\n\" };\n h = gets.split('').reject { |e| e == \"\\n\" };\n done = false\n (0...h.length).each { |i|\n sm = p.length\n frq = Array.new(27, 0);\n p.each { |e| frq[e.ord - ?a.ord] += 1 }\n flg = true\n for j in i ... h.length\n if frq[h[j].ord - ?a.ord] == 0\n flg = false\n break\n else\n sm -= 1\n \n frq[h[j].ord - ?a.ord] -= 1\n break if sm == 0\n end\n end\n if flg\n puts \"YES\"\n done = true\n break\n end\n }\n puts \"NO\" if !done\nend"}, {"source_code": "gets.strip.to_i.times do\n p = gets.strip\n h = gets.strip\n if p.length > h.length\n puts \"NO\"\n next\n end\n p = p.chars.sort.join\n ck = false\n (0...p.length).each { |i| \n break if i + p.length > h.length\n s = h[i, p.length]\n s = s.chars.sort.join\n if s == p\n ck = true\n break\n end\n }\n puts ck ? \"YES\" : \"NO\"\nend"}, {"source_code": "gets.strip.to_i.times do\n frq = Array.new(27, 0);\n p = gets.split('').reject { |e| e == \"\\n\" };\n h = gets.split('').reject { |e| e == \"\\n\" };\n done = false\n (0...h.length).each { |i|\n sm = p.length\n p.each { |e| frq[e.ord - ?a.ord] += 1 }\n flg = true\n for j in i ... h.length\n if frq[h[j].ord - ?a.ord] == 0\n flg = false\n break\n else\n sm -= 1\n frq[h[j].ord - ?a.ord] -= 1\n break if sm == 0\n end\n end\n if flg\n puts \"YES\"\n done = true\n end\n }\n puts \"NO\" if !done\nend"}, {"source_code": "gets.strip.to_i.times do\n p = gets.split('').reject { |e| e == \"\\n\" };\n h = gets.split('').reject { |e| e == \"\\n\" };\n if p.length > h.length\n puts \"NO\"\n next\n end\n done = false\n (0...h.length).each { |i|\n frq = Array.new(27, 0);\n sm = p.length\n p.each { |e| frq[e.ord - ?a.ord] += 1 }\n flg = true\n for j in i ... h.length\n if frq[h[j].ord - ?a.ord] == 0\n flg = false\n break\n else\n sm -= 1\n frq[h[j].ord - ?a.ord] -= 1\n break if sm == 0\n end\n end\n if flg\n puts \"YES\"\n done = true\n break\n end\n }\n puts \"NO\" if !done\nend"}, {"source_code": "gets.strip.to_i.times do\n p = gets.strip\n h = gets.strip\n if p.length > h.length\n puts \"NO\"\n next\n end\n p.chars.sort.join\n ck = false\n (0...p.length).each { |i| \n break if i + p.length > h.length\n s = h[i, p.length]\n s.chars.sort.join\n if s == p\n ck = true\n break\n end\n }\n puts ck ? \"YES\" : \"NO\"\nend"}, {"source_code": "gets.strip.to_i.times do\n frq = Array.new(27, 0);\n p = gets.split('').reject { |e| e == \"\\n\" };\n h = gets.split('').reject { |e| e == \"\\n\" };\n done = false\n (0...h.length).each { |i|\n sm = p.length\n p.each { |e| frq[e.ord - ?a.ord] += 1 }\n flg = true\n for j in i ... h.length\n if frq[h[j].ord - ?a.ord] == 0\n flg = false\n break\n else\n sm -= 1\n frq[h[j].ord - ?a.ord] -= 1\n break if sm == 0\n end\n end\n if flg\n puts \"YES\"\n done = true\n break\n end\n }\n puts \"NO\" if !done\nend"}, {"source_code": "gets.strip.to_i.times do\n p = gets.split('').reject { |e| e == \"\\n\" };\n h = gets.split('').reject { |e| e == \"\\n\" };\n if p.length > h.length\n puts \"NO\"\n next\n end\n p.sort\n ck = false\n (0...p.length).each { |i| \n break if i + p.length > h.length\n s = h[i, p.length - 1]\n s.sort\n if s == p\n ck = true\n break\n end\n }\n puts ck ? \"YES\" : \"NO\"\nend"}, {"source_code": "gets.strip.to_i.times do\n p = gets.split('').reject { |e| e == \"\\n\" };\n h = gets.split('').reject { |e| e == \"\\n\" };\n done = false\n (0...h.length).each { |i|\n sm = p.length\n frq = Array.new(27, 0);\n p.each { |e| frq[e.ord - ?a.ord] += 1 }\n flg = true\n for j in i ... h.length\n if frq[h[j].ord - ?a.ord] == 0\n flg = false\n break\n else\n frq[h[j].ord - ?a.ord] -= 1\n if frq.uniq.length == 1\n frq.uniq.each { |e| \n if e.nonzero?\n flg = false\n break\n end\n }\n end\n end\n end\n if flg\n puts \"YES\"\n done = true\n break\n end\n }\n puts \"NO\" if !done\nend"}, {"source_code": "gets.strip.to_i.times do\n p = gets.strip\n h = gets.strip\n if p.length > h.length\n puts \"NO\"\n next\n end\n p = p.chars.sort.join\n ck = false\n (0...h.length).each { |i| \n break if i + p.length > h.length\n s = h[i, p.length]\n s = s.chars.sort.join\n puts \"#{s} + #{p}\"\n if s == p\n ck = true\n break\n end\n }\n puts ck ? \"YES\" : \"NO\"\nend"}], "src_uid": "48151011c3d380ab303ae38d0804176a"} {"nl": {"description": "Valera had two bags of potatoes, the first of these bags contains x (x ≥ 1) potatoes, and the second — y (y ≥ 1) potatoes. Valera — very scattered boy, so the first bag of potatoes (it contains x potatoes) Valera lost. Valera remembers that the total amount of potatoes (x + y) in the two bags, firstly, was not gerater than n, and, secondly, was divisible by k.Help Valera to determine how many potatoes could be in the first bag. Print all such possible numbers in ascending order.", "input_spec": "The first line of input contains three integers y, k, n (1 ≤ y, k, n ≤ 109;  ≤ 105).", "output_spec": "Print the list of whitespace-separated integers — all possible values of x in ascending order. You should print each possible value of x exactly once. If there are no such values of x print a single integer -1.", "sample_inputs": ["10 1 10", "10 6 40"], "sample_outputs": ["-1", "2 8 14 20 26"], "notes": null}, "positive_code": [{"source_code": "s = gets.split\n\ny = s[0].to_i\nk = s[1].to_i\nn = s[2].to_i\n\nif y >= n then\n puts \"-1\"\nelsif k == n then\n if n - y > 0 then\n puts n - y\n else\n puts \"-1\"\n end\nelse\n maybe = []\n if k > y then \n i = k - y\n else\n i = 1\n t = i * k\n while t < y do\n i += 1\n t = i * k\n end\n i = i * k - y\n# i = 0\n# while (y + i) % k != 0 do\n# i += 1\n# end\n end\n\n if y + i > n then\n puts \"-1\"\n else\n if i != 0 then\n maybe << i\n end\n j = 1\n while y + i + j*k <= n do\n maybe << i + j*k\n j += 1\n end\n if maybe.empty? then \n puts \"-1\"\n else\n puts maybe.join(\" \")\n end\n end\nend"}, {"source_code": "y, k, n = gets.chomp.split(\" \").map{|e| e.to_i}\nans = []\n\nmin = nil\nidx = 1\nloop do\n if idx * k > n\n break\n elsif idx * k >= y and (idx * k - y) > 0\n min = idx * k - y\n break\n end \n idx += 1\nend\nif not min.nil?\n ans << min\n tmp = min + k\n loop do\n if tmp > n - y\n break\n else\n ans << tmp\n end \n tmp += k\n end\nend \n\nans << -1 if ans.length == 0\n\nputs ans.join(\" \")"}, {"source_code": "y, k, n = gets.split.map(&:to_i)\nall = Array.new\ni = y\ni -= y%k\ni += k\nwhile i <= n do\n all << i-y\n i += k\nend\nif all.count == 0\n print -1\nelse\n all.each {|x| print x, \" \"}\nend"}, {"source_code": "y, k, n = gets.split(' ').map(&:to_i)\nres = (((y / k) + 1)..(n / k)).to_a.map { |x| x * k - y }\nputs res.empty? ? -1 : res"}, {"source_code": "y,k,n=gets.chomp.split(' ').map(&:to_i)\ni=y/k +1\nwhile (k*i)<=n\n\tprint (k*i-y).to_s+' '\n\ti+=1\nend\nif i==y/k+1\n\tputs \"-1\"\nelse\n\tputs\nend"}, {"source_code": "y, k, n = gets.split.map(&:to_i)\nmin = k-y%k\nif min+y > n\n puts(-1)\nelse\n while min+y <= n do\n $><<\"#{min} \"\n min += k\n end\nend"}, {"source_code": "y , k, n = gets.split.map {|x| x.to_i }\ni = 1\nans = []\nwhile i * k <= n do\n ans << (i * k - y) if i * k - y >= 1\n i = i + 1\nend\nif ans.size != 0 then\n puts ans.join \" \"\nelse\n puts -1\nend "}, {"source_code": "y, k, n = gets.chomp.split(/ /).map(&:to_i)\nif y >= n\n puts \"-1\"\nelse\n if k == 1\n puts \"#{(1..(n - y)).to_a.join(\" \")}\"\n else\n ans = []\n limit = (n / k) * k\n index = 0\n while index < limit\n index += k\n next if index < y\n x = index - y\n ans << x if x > 0\n end\n if ans.length == 0\n puts \"-1\"\n else\n puts ans.join(\" \")\n end\n end\nend\n"}, {"source_code": "y,k,n = gets.split(\" \").map(&:to_i)\ni = (y/k.to_f).ceil\nstart = k * i\n\narr = []\n(start..n).step(k).each do |a|\n\t\n\tarr << a - y if a - y != 0\nend\nif arr.empty?\n\tputs -1\nelse\n\tputs arr.join(\" \")\nend\n"}, {"source_code": "y,k,n=gets.split.collect{|e| e.to_i}\nfl=0\ni=k;\nwhile (i 0\n\tputs ans.join ' '\nelse\n\tputs -1\nend"}], "negative_code": [{"source_code": "s = gets.split\n\ny = s[0].to_i\nk = s[1].to_i\nn = s[2].to_i\nif k == n then\n puts n - y\nelse\n maybe = []\n if k > y then \n i = k - y\n else\n i = 0\n while (y + i) % k != 0 do\n i += 1\n end\n end\n \n if y + i > n then\n puts \"-1\"\n else\n if i != 0 then\n maybe << i\n end\n j = 1\n while y + i + j*k <= n do\n maybe << i + j*k\n j += 1\n end\n puts maybe.join(\" \")\n end\nend"}, {"source_code": "s = gets.split\n\ny = s[0].to_i\nk = s[1].to_i\nn = s[2].to_i\n\nmaybe = []\n\nsum = y\ni = 1\nwhile (y + i) % k != 0 do\n i += 1\nend\n\nif y + i > n then\n puts \"-1\"\nelse\n maybe << i\n j = 1\n while y + i + j*k < n do\n maybe << i + j*k\n j += 1\n end\n puts maybe.join(\" \")\nend"}, {"source_code": "s = gets.split\n\ny = s[0].to_i\nk = s[1].to_i\nn = s[2].to_i\nif k == n then\n puts n - y\nelsif n/k == 1 then\n puts k - y\nelse\n maybe = []\n if k > y then \n i = k\n else\n i = 1\n while (y + i) % k != 0 do\n i += 1\n end\n end\n\n if y + i > n then\n puts \"-1\"\n else\n maybe << i\n j = 1\n while y + i + j*k <= n do\n maybe << i + j*k\n j += 1\n end\n puts maybe.join(\" \")\n end\nend"}, {"source_code": "s = gets.split\n\ny = s[0].to_i\nk = s[1].to_i\nn = s[2].to_i\nif k == n then\n puts n - y\nelsif n/k == 1 then\n puts k - y\nelse\n maybe = []\n if k > y then \n i = k - y\n else\n i = 1\n while (y + i) % k != 0 do\n i += 1\n end\n end\n\n if y + i > n then\n puts \"-1\"\n else\n maybe << i\n j = 1\n while y + i + j*k <= n do\n maybe << i + j*k\n j += 1\n end\n puts maybe.join(\" \")\n end\nend"}, {"source_code": "s = gets.split\n\ny = s[0].to_i\nk = s[1].to_i\nn = s[2].to_i\nif k >= n then\n puts \"-1\"\nelse\n maybe = []\n i = 1\n while (y + i) % k != 0 do\n i += 1\n end\n\n if y + i > n then\n puts \"-1\"\n else\n maybe << i\n j = 1\n while y + i + j*k <= n do\n maybe << i + j*k\n j += 1\n end\n puts maybe.join(\" \")\n end\nend"}, {"source_code": "s = gets.split\n\ny = s[0].to_i\nk = s[1].to_i\nn = s[2].to_i\n\nif k == n then\n puts n - y\nelse\n maybe = []\n if k > y then \n i = k - y\n else\n i = 0\n while (y + i) % k != 0 do\n i += 1\n end\n end\n\n if y + i > n then\n puts \"-1\"\n else\n if i != 0 then\n maybe << i\n end\n j = 1\n while y + i + j*k <= n do\n maybe << i + j*k\n j += 1\n end\n if maybe.empty? then \n puts \"-1\"\n else\n puts maybe.join(\" \")\n end\n end\nend"}, {"source_code": "y, k, n = gets.chomp.split(\" \").map{|e| e.to_i}\nans = []\n\nmin = nil\nidx = 1\nloop do\n if idx * k >= n\n break\n elsif idx * k >= y\n min = idx * k - y\n break\n end \n idx += 1\nend\n\nif not min.nil?\n tmp = min\n loop do\n if tmp >= n - y\n break\n else\n ans << tmp\n end \n tmp += k\n end\nend \n\nans << -1 if ans.length == 0\n\nputs ans.join(\" \")"}, {"source_code": "y, k, n = gets.chomp.split(\" \").map{|e| e.to_i}\nans = []\n\nmin = nil\nidx = 1\nloop do\n if idx * k >= n\n break\n elsif idx * k >= y and (idx * k - y) > 0\n min = idx * k - y\n break\n end \n idx += 1\nend\n\nif not min.nil?\n tmp = min\n loop do\n if tmp > n - y\n break\n else\n ans << tmp\n end \n tmp += k\n end\nend \n\nans << -1 if ans.length == 0\n\nputs ans.join(\" \")"}, {"source_code": "y, k, n = gets.chomp.split(\" \").map{|e| e.to_i}\nans = []\n\nmin = nil\nidx = 0\nloop do\n if idx * k >= n\n break\n elsif idx * k >= y\n min = idx * k - y\n break\n end \n idx += 1\nend\n\nif not min.nil?\n tmp = min\n loop do\n if tmp >= n - y\n break\n else\n ans << tmp\n end \n tmp += k\n end\nend \n\nans << -1 if ans.length == 0\n\nputs ans.join(\" \")"}, {"source_code": "y, k, n = gets.split.map(&:to_i)\nmax = (n-n%k)-y\nif max < 1\n puts -1\n exit\nend\nwhile max >= 1 do\n puts max\n max -= k\nend"}, {"source_code": "y, k, n = gets.chomp.split(/ /).map(&:to_i)\nif y >= n || (n - y) % k != 0\n puts \"-1\"\nelse\n if k == 1\n puts \"#{(1..(n - y)).to_a.join(\" \")}\"\n else\n ans = []\n limit = (n / k) * k\n index = 0\n while index < limit\n index += k\n next if index < y\n x = index - y\n ans << x\n end\n puts ans.join(\" \")\n end\nend\n"}, {"source_code": "y, k, n = gets.chomp.split(/ /).map(&:to_i)\nif y >= n\n puts \"-1\"\nelse\n ans = []\n (1..(n - y)).each do |x|\n ans << x if x + y < n && (x + y) % k == 0\n end\n puts ans.join(\" \")\nend\n"}, {"source_code": "y, k, n = gets.chomp.split(/ /).map(&:to_i)\nif y >= n\n puts \"-1\"\nelse\n if k == 1\n puts \"#{(1..(n - y)).to_a.join(\" \")}\"\n else\n ans = []\n limit = (n / k) * k\n index = 0\n while index < limit\n index += k\n next if index < y\n x = index - y\n ans << x\n end\n if ans.length == 0\n puts \"-1\"\n else\n puts ans.join(\" \")\n end\n end\nend\n"}, {"source_code": "y, k, n = gets.chomp.split(/ /).map(&:to_i)\nif y >= n\n puts \"-1\"\nelse\n if k == 1\n puts \"#{(1..(n - y)).to_a.join(\" \")}\"\n else\n ans = []\n limit = (n / k) * k\n index = 0\n while index < limit\n index += k\n next if index < y\n x = index - y\n ans << x\n end\n puts ans.join(\" \")\n end\nend\n"}, {"source_code": "y,k,n = gets.split(\" \").map(&:to_i)\ni = (y/k.to_f).ceil\nstart = k * i\nif start >= n\n\tputs -1\nelse\n\tarr = []\n\t(start..n).step(k).each do |a|\n\t\tarr << a - y\n\tend\n\tputs arr.join(\" \")\nend"}, {"source_code": "y,k,n = gets.split(\" \").map(&:to_i)\ni = (y/k.to_f).ceil\nstart = k * i\nif start >= n\n\tputs -1\nelse\n\tarr = []\n\t(start..n).step(k).each do |a|\n\t\t\n\t\tarr << a - y if a - y != 0\n\tend\n\tputs arr.join(\" \")\nend"}, {"source_code": "y,k,n=gets.split.collect{|e| e.to_i}\nfl=0\nif (y<=k) then\n i=k;\n if (i<=n) then fl=1 end\nelse \n i=y+1\n while (i<=n) do\n if (i%k==0) then\n fl=1\n break\n end\n i+=1\n end\nend\nj=i;\nwhile (j<=n) do\n if (j!=i) then print \" \" end\n print j-y \n j+=k\nend\nif (fl==0) then print -1 end\nputs\n"}], "src_uid": "2deda3a05740e1184735bf437e3850a8"} {"nl": {"description": "You have an image file of size $$$2 \\times 2$$$, consisting of $$$4$$$ pixels. Each pixel can have one of $$$26$$$ different colors, denoted by lowercase Latin letters.You want to recolor some of the pixels of the image so that all $$$4$$$ pixels have the same color. In one move, you can choose no more than two pixels of the same color and paint them into some other color (if you choose two pixels, both should be painted into the same color).What is the minimum number of moves you have to make in order to fulfill your goal?", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. Each test case consists of two lines. Each of these lines contains two lowercase letters of Latin alphabet without any separators, denoting a row of pixels in the image.", "output_spec": "For each test case, print one integer — the minimum number of moves you have to make so that all $$$4$$$ pixels of the image have the same color.", "sample_inputs": ["5\n\nrb\n\nbr\n\ncc\n\nwb\n\naa\n\naa\n\nab\n\ncd\n\nyy\n\nxx"], "sample_outputs": ["1\n2\n0\n3\n1"], "notes": "NoteLet's analyze the test cases of the example.In the first test case, you can paint the bottom left pixel and the top right pixel (which share the same color) into the color r, so all pixels have this color.In the second test case, two moves are enough: paint both top pixels, which have the same color c, into the color b; paint the bottom left pixel into the color b. In the third test case, all pixels already have the same color.In the fourth test case, you may leave any of the pixels unchanged, and paint all three other pixels into the color of that pixel in three moves.In the fifth test case, you can paint both top pixels into the color x."}, "positive_code": [{"source_code": "gets.to_i.times { \n\tp gets.chomp.chars.append(*gets.chomp.chars).group_by{|i|i}.values.map{|i|(i.length+1)/2}.sort{|a,b|b-a}.drop(1).sum\n}"}, {"source_code": "t = gets.to_i\nt.times do\n puts((gets.chomp!.split('') + gets.chomp!.split('')).uniq.size - 1)\nend\n"}], "negative_code": [{"source_code": "gets.to_i.times { \n\ta = gets.chomp.chars.append(*gets.chomp.chars).group_by{|i|i}.values.map{|i|(i.length+1)/2}\n\tp (a.count>1? a.sum - 1: 0)\n}"}], "src_uid": "a9143235c8e2b6b188ea3fc8a90f0c80"} {"nl": {"description": "Today, Marin is at a cosplay exhibition and is preparing for a group photoshoot!For the group picture, the cosplayers form a horizontal line. A group picture is considered beautiful if for every contiguous segment of at least $$$2$$$ cosplayers, the number of males does not exceed the number of females (obviously).Currently, the line has $$$n$$$ cosplayers which can be described by a binary string $$$s$$$. The $$$i$$$-th cosplayer is male if $$$s_i = 0$$$ and female if $$$s_i = 1$$$. To ensure that the line is beautiful, you can invite some additional cosplayers (possibly zero) to join the line at any position. You can't remove any cosplayer from the line.Marin wants to know the minimum number of cosplayers you need to invite so that the group picture of all the cosplayers is beautiful. She can't do this on her own, so she's asking you for help. Can you help her?", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 10^3$$$) — the number of test cases. The first line of each test case contains a positive integer $$$n$$$ ($$$1 \\leq n \\leq 100$$$) — the number of cosplayers in the initial line. The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$ — describing the cosplayers already in line. Each character of the string is either 0 describing a male, or 1 describing a female. Note that there is no limit on the sum of $$$n$$$.", "output_spec": "For each test case, print the minimum number of cosplayers you need to invite so that the group picture of all the cosplayers is beautiful.", "sample_inputs": ["9\n\n3\n\n000\n\n3\n\n001\n\n3\n\n010\n\n3\n\n011\n\n3\n\n100\n\n3\n\n101\n\n3\n\n110\n\n3\n\n111\n\n19\n\n1010110000100000101"], "sample_outputs": ["4\n2\n1\n0\n2\n0\n0\n0\n17"], "notes": "NoteIn the first test case, for each pair of adjacent cosplayers, you can invite two female cosplayers to stand in between them. Then, $$$000 \\rightarrow 0110110$$$.In the third test case, you can invite one female cosplayer to stand next to the second cosplayer. Then, $$$010 \\rightarrow 0110$$$."}, "positive_code": [{"source_code": "t = gets.to_i\r\nwhile t > 0\r\n t = t - 1\r\n n = gets.to_i\r\n s = gets\r\n ans = 0 \r\n for index in 0..n - 1\r\n \r\n if index < n - 1 && s[index] == s[index + 1] && s[index] == '0'\r\n ans += 2 \r\n end\r\n if(index < n - 2 && s[index] == s[index + 2] && s[index] != s[index + 1] && s[index + 1] == '1')\r\n ans += 1\r\n end\r\n end\r\n \r\n puts ans\r\nend"}], "negative_code": [{"source_code": "t = gets.to_i\r\nwhile t > 0\r\n t = t - 1\r\n n = gets.to_i\r\n s = gets\r\n ones , zeros , ans = 0 , 0 , 0 \r\n for index in 0..n - 1\r\n if s[index] == '0'\r\n zeros = zeros + 1\r\n else \r\n ones = ones + 1\r\n end\r\n if index < n - 1 && s[index] == s[index + 1] && s[index] == '0'\r\n ans += 2 \r\n ones += 2\r\n end\r\n if(index < n - 2 && s[index] == s[index + 2] && s[index] != s[index + 1] && s[index + 1] == '1')\r\n ans += 1\r\n ones += 1\r\n end\r\n end\r\n if zeros > ones\r\n ans = ans + (zeros - ones) \r\n end\r\n puts ans\r\nend"}], "src_uid": "9966bfdc9677a9dd558684a00977cd58"} {"nl": {"description": "You are given two integers $$$a$$$ and $$$b$$$. Print $$$a+b$$$.", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case is given as a line of two integers $$$a$$$ and $$$b$$$ ($$$-1000 \\le a, b \\le 1000$$$).", "output_spec": "Print $$$t$$$ integers — the required numbers $$$a+b$$$.", "sample_inputs": ["4\n\n1 5\n\n314 15\n\n-99 99\n\n123 987"], "sample_outputs": ["6\n329\n0\n1110"], "notes": null}, "positive_code": [{"source_code": "gets.to_i.times do\n puts gets.split.map(&:to_i).inject(:+)\nend"}, {"source_code": "t=gets.to_i\nt.times{\na,b=gets.split(\" \")\nputs a.to_i+b.to_i\n}\n"}, {"source_code": "count_number = gets.to_i\nresult = []\ncount_number.times do |n|\n input_numbers = gets.chomp.split(' ').map(&:to_i)\n sum_numbers = input_numbers.inject(0){|sum,x| sum + x }\n result.push(sum_numbers)\nend\nputs result\n"}, {"source_code": "t = gets.to_i\nt.times do\n\ta, b = gets.split.map(&:to_i)\n\tputs a + b\nend"}, {"source_code": "gets.to_i.times { puts gets.split(' ').map(&:to_i).reduce(:+) }"}, {"source_code": "gets.to_i.times{puts eval gets.gsub(' ', '+')}"}, {"source_code": "gets.to_i.times do\n a, b = gets.split\n a, b = a.to_i, b.to_i\n puts a+b\nend\n"}, {"source_code": "1.upto(gets.chomp.to_i) do\n a,b = gets.chomp.split.map(&:to_i)\n\n puts a + b\nend\n"}, {"source_code": "n = gets.chomp.to_i\nfor i in 1..n do\n x,y = gets.chomp.split(' ')\n puts x.to_i + y.to_i\nend"}, {"source_code": "$n = gets.to_i\n$i = 1\nuntil $i > $n do\n a, b, * = gets.split.map(&:to_i)\n sum = a + b\n puts( sum )\n $i +=1;\nend"}], "negative_code": [{"source_code": "1.upto(gets.chomp.to_i) do\n a,b = gets.chomp.split.map(&:to_i)\n s = 0\n a.times { s += 1 }\n b.times { s += 1 }\n\n puts s\nend\n"}, {"source_code": "$n = gets.to_i\n$i = 1\nuntil $i > $n do\n a = gets.to_i\n b = gets.to_i\n sum = a + b\n puts( sum )\n $i +=1;\nend"}], "src_uid": "27ddccc777ef9040284ab6314cbd70e7"} {"nl": {"description": "This is an interactive problem.The jury has a permutation $$$p$$$ of length $$$n$$$ and wants you to guess it. For this, the jury created another permutation $$$q$$$ of length $$$n$$$. Initially, $$$q$$$ is an identity permutation ($$$q_i = i$$$ for all $$$i$$$).You can ask queries to get $$$q_i$$$ for any $$$i$$$ you want. After each query, the jury will change $$$q$$$ in the following way: At first, the jury will create a new permutation $$$q'$$$ of length $$$n$$$ such that $$$q'_i = q_{p_i}$$$ for all $$$i$$$. Then the jury will replace permutation $$$q$$$ with pemutation $$$q'$$$. You can make no more than $$$2n$$$ queries in order to quess $$$p$$$.", "input_spec": "The first line of input contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 1000$$$) — the number of test cases.", "output_spec": null, "sample_inputs": ["2\n4\n\n3\n\n2\n\n1\n\n4\n\n2\n\n4\n\n4"], "sample_outputs": ["? 3\n\n? 2\n\n? 4\n\n! 4 2 1 3\n\n? 2\n\n? 3\n\n? 2\n\n! 1 3 4 2"], "notes": "NoteIn the first test case the hidden permutation $$$p = [4, 2, 1, 3]$$$.Before the first query $$$q = [1, 2, 3, 4]$$$ so answer for the query will be $$$q_3 = 3$$$.Before the second query $$$q = [4, 2, 1, 3]$$$ so answer for the query will be $$$q_2 = 2$$$.Before the third query $$$q = [3, 2, 4, 1]$$$ so answer for the query will be $$$q_4 = 1$$$.In the second test case the hidden permutation $$$p = [1, 3, 4, 2]$$$.Empty strings are given only for better readability. There will be no empty lines in the testing system."}, "positive_code": [{"source_code": "gets.to_i.times do\n n = gets.to_i\n p = Array.new(n + 1)\n\n (1..n).each do |i|\n next if p[i]\n\n prev = nil\n loop do\n puts \"? #{i}\"\n $stdout.flush\n\n x = gets.to_i\n\n if prev\n p[prev] = x\n end\n\n break if p[x]\n prev = x\n end\n end\n\n p[0] = '!'\n puts p.join(' ')\n $stdout.flush\nend\n"}], "negative_code": [], "src_uid": "96ec983bfadc9e96e36ebb8ffc5279d3"} {"nl": {"description": "Each year in the castle of Dwarven King there is a competition in growing mushrooms among the dwarves. The competition is one of the most prestigious ones, and the winner gets a wooden salad bowl. This year's event brought together the best mushroom growers from around the world, so we had to slightly change the rules so that the event gets more interesting to watch.Each mushroom grower has a mushroom that he will grow on the competition. Under the new rules, the competition consists of two parts. The first part lasts t1 seconds and the second part lasts t2 seconds. The first and the second part are separated by a little break.After the starting whistle the first part of the contest starts, and all mushroom growers start growing mushrooms at once, each at his individual speed of vi meters per second. After t1 seconds, the mushroom growers stop growing mushrooms and go to have a break. During the break, for unexplained reasons, the growth of all mushrooms is reduced by k percent. After the break the second part of the contest starts and all mushrooms growers at the same time continue to grow mushrooms, each at his individual speed of ui meters per second. After a t2 seconds after the end of the break, the competition ends. Note that the speeds before and after the break may vary.Before the match dwarf Pasha learned from all participants, what two speeds they have chosen. However, the participants did not want to disclose to him all their strategy and therefore, did not say in what order they will be using these speeds. That is, if a participant chose speeds ai and bi, then there are two strategies: he either uses speed ai before the break and speed bi after it, or vice versa.Dwarf Pasha really wants to win the totalizer. He knows that each participant chooses the strategy that maximizes the height of the mushroom. Help Dwarf Pasha make the final table of competition results.The participants are sorted in the result table by the mushroom height (the participants with higher mushrooms follow earlier in the table). In case of equal mushroom heights, the participants are sorted by their numbers (the participants with a smaller number follow earlier).", "input_spec": "The first input line contains four integer numbers n, t1, t2, k (1 ≤ n, t1, t2 ≤ 1000; 1 ≤ k ≤ 100) — the number of participants, the time before the break, the time after the break and the percentage, by which the mushroom growth drops during the break, correspondingly. Each of the following n lines contains two integers. The i-th (1 ≤ i ≤ n) line contains space-separated integers ai, bi (1 ≤ ai, bi ≤ 1000) — the speeds which the participant number i chose.", "output_spec": "Print the final results' table: n lines, each line should contain the number of the corresponding dwarf and the final maximum height of his mushroom with exactly two digits after the decimal point. The answer will be considered correct if it is absolutely accurate.", "sample_inputs": ["2 3 3 50\n2 4\n4 2", "4 1 1 1\n544 397\n280 101\n280 101\n693 970"], "sample_outputs": ["1 15.00\n2 15.00", "4 1656.07\n1 937.03\n2 379.99\n3 379.99"], "notes": "Note First example: for each contestant it is optimal to use firstly speed 2 and afterwards speed 4, because 2·3·0.5 + 4·3 > 4·3·0.5 + 2·3. "}, "positive_code": [{"source_code": "def calcmax(a,b,t1,t2,k)\n f = (100 - k).to_f / 100\n c1 = a*t1*f + b*t2\n c2 = b*t1*f + a*t2\n return [c1,c2].max\nend\n\nn,t1,t2,k = gets.chomp.split(/ /).map!{|x| x.to_i}\nar = Array.new()\nfor i in 0..n-1\n a,b = gets.chomp.split(/ /).map!{|x| x.to_i}\n ar << [i+1, calcmax(a,b,t1,t2,k)]\nend\nar.sort!{|ar1, ar2|\n if ar2[1] == ar1[1]\n ar1[0] <=> ar2[0]\n else\n ar2[1] <=> ar1[1]\n end\n}\nfor i in 0..n-1\n puts ar[i][0].to_s + \" \" + sprintf('%.2f', ar[i][1])\nend\n\n"}, {"source_code": "n,t1,t2,k = gets.to_s.split(\" \").map{|x| Integer x}\n\nd = []\n1.upto n do |i|\n\ta,b = gets.to_s.split(\" \").map{|x| Integer x}\n\n\tl1 = t1 * a\n\tl1 = l1 - (l1*k/100.0)\n\tl1 += t2 * b\n\n\tl2 = t1 * b\n\tl2 = l2 - (l2*k/100.0)\n\tl2 += t2 * a\n \n\tl = (l1>l2 ? l1 : l2)\n\tx = [l,i]\n\n\td.push x\nend\n\nd.sort_by!{|x| [-x[0],x[1]] }\n\nd.each do |x|\n\tputs \"#{x[1]} \"+format(\"%.2f\",x[0])\nend\n\n"}, {"source_code": "n,t1,t2,k = gets.split.map(&:to_i)\n\nif t1*0.01*k >= t2\n a = n.times.map{|i| gets.split.map(&:to_i).sort.reverse.push(i) }\nelse\n a = n.times.map{|i| gets.split.map(&:to_i).sort.push(i) }\nend\n\na.map!{|m| [[m[0]*t1*(1-0.01*k) + m[1]*t2,m[1]*t1*(1-0.01*k) + m[0]*t2].max, m[2]] }.sort!{|b,a| (a[0] <=> b[0]).nonzero? or b[1] <=> a[1] }.each{|m| puts \"%d %.02f\"%[m[1]+1,m[0]]}\n"}, {"source_code": "n, t1, t2, k = gets.split.map(&:to_i)\nabs = n.times.map{gets.split.map(&:to_i)}\nabs.map.with_index do |(a, b), i|\n [i, [a * t1 * (1 - k.to_f / 100) + b * t2, b * t1 * (1 - k.to_f / 100) + a * t2].max]\nend.sort_by{|i, max| [-max, i]}.each do |i, max|\n puts \"#{i+1} #{\"%.2f\" % max}\"\nend\n"}, {"source_code": "def main\n\n n, t1, t2, k = STDIN.gets.split.collect {|s| s.to_i}\n \n a = []\n\n n.times do |i|\n v1, v2 = STDIN.gets.split.collect {|s| s.to_i}\n h1 = v1 * t1 * (1 - k / 100.0) + v2 * t2\n h2 = v2 * t1 * (1 - k / 100.0) + v1 * t2\n a.push([i, [h1, h2].max])\n end\n \n a.sort! do |x, y|\n if x[1] < y[1]\n 1\n elsif x[1] > y[1]\n -1\n else\n x[0] <=> y[0]\n end\n end\n \n a.each {|e| puts \"%d %.2f\" % [e[0] + 1, e[1]]}\nend\n\nmain\n\n"}], "negative_code": [{"source_code": "def calcmax(a,b,t1,t2,k)\n f = (100 - k).to_f / 100\n c1 = a*t1*f + b*t2\n c2 = b*t1*f + a*t2\n return [c1,c2].max\nend\n\nn,t1,t2,k = gets.chomp.split(/ /).map!{|x| x.to_i}\nar = Array.new()\nfor i in 0..n-1\n a,b = gets.chomp.split(/ /).map!{|x| x.to_i}\n ar << [i+1, calcmax(a,b,t1,t2,k)]\nend\nar.sort!{|ar1, ar2|\n ar2[1] <=> ar1[1]\n}\nfor i in 0..n-1\n puts ar[i][0].to_s + \" \" + sprintf('%.2f', ar[i][1])\nend\n\n"}, {"source_code": "n,t1,t2,k = gets.to_s.split(\" \").map{|x| Integer x}\n\nd = []\n1.upto n do |i|\n\ta,b = gets.to_s.split(\" \").map{|x| Integer x}\n\n\tl1 = t1 * a\n\tl1 = l1 - (l1*k/100.0)\n\tl1 += t2 * b\n\tl2 = t2 * b\n\tl2 = l2 - (l2*k/100.0)\n\tl2 += t1 * a\n \n\tl = (l1>l2 ? l1 : l2)\n\tx = [l,i]\n\n\td.push x\nend\n\nd.sort_by!{|x| [-x[0],x[1]] }\n\nd.each do |x|\n\tputs \"#{x[1]} \"+format(\"%.2f\",x[0])\nend\n\n"}, {"source_code": "n,t1,t2,k = gets.split.map(&:to_i)\n\nif t1*0.01*k >= t2\n a = n.times.map{|i| gets.split.map(&:to_i).sort.reverse.push(i) }\nelse\n a = n.times.map{|i| gets.split.map(&:to_i).sort.push(i) }\nend\n\na.map!{|m| [m[0]*t1*(1-0.01*k) + m[1]*t2, m[2]] }.sort!{|b,a| (a[0] <=> b[0]).nonzero? or b[1] <=> a[1] }.each{|m| puts \"%d %.02f\"% [m[1]+1,m[0]]}\n"}, {"source_code": "n, t1, t2, k = gets.split.map(&:to_i)\nabs = n.times.map{gets.split.map(&:to_i)}\nabs.map.with_index do |(a, b), i|\n [i, [a * t1 * (1 - k.to_f / 100) + b * t2, b * t2 * (1 - k.to_f / 100) + a * t2].max]\nend.sort_by{|i, max| [-max, i]}.each do |i, max|\n puts \"#{i+1} #{\"%.2f\" % max}\"\nend\n"}], "src_uid": "a89f9310996eb23254d07e52544e30ae"} {"nl": {"description": "Mishka has got n empty boxes. For every i (1 ≤ i ≤ n), i-th box is a cube with side length ai.Mishka can put a box i into another box j if the following conditions are met: i-th box is not put into another box; j-th box doesn't contain any other boxes; box i is smaller than box j (ai < aj). Mishka can put boxes into each other an arbitrary number of times. He wants to minimize the number of visible boxes. A box is called visible iff it is not put into some another box.Help Mishka to determine the minimum possible number of visible boxes!", "input_spec": "The first line contains one integer n (1 ≤ n ≤ 5000) — the number of boxes Mishka has got. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is the side length of i-th box.", "output_spec": "Print the minimum possible number of visible boxes.", "sample_inputs": ["3\n1 2 3", "4\n4 2 4 3"], "sample_outputs": ["1", "2"], "notes": "NoteIn the first example it is possible to put box 1 into box 2, and 2 into 3.In the second example Mishka can put box 2 into box 3, and box 4 into box 1."}, "positive_code": [{"source_code": "_ = $stdin.gets\n\nbxs = $stdin.gets.chomp.split(\" \").map{ |a| a.to_i }\nns = {};\nbxs.each{ |a|\n ns[a] = (ns[a] || 0) + 1\n}\nputs ns.values.max\n"}, {"source_code": "#!/usr/bin/env ruby\n\nn = gets.to_i\narr = gets.split.map{|x| x.to_i}\narr.sort!\n\n$ans = 0\nlast = -1\nstreak = 0\narr.each do |x|\n\tif x != last then\n\t\tlast = x\n\t\t$ans = streak unless streak <= $ans\n\t\tstreak = 1\n\telse\n\t\tstreak += 1\n\tend\nend\n$ans = streak unless streak <= $ans\n\nputs $ans\n"}, {"source_code": "n, c = gets, Hash.new(0)\ngets.split.each {|a| c[a] += 1}\np c.values.max"}], "negative_code": [{"source_code": "#!/usr/bin/env ruby\n\nn = gets.to_i\narr = gets.split.map{|x| x.to_i}\narr.sort!\n\n$ans = 1\n(n-1).times do |x|\n\tif arr[x] == arr[x+1] then\n\t\t$ans += 1\n\tend \nend \nputs $ans\n"}, {"source_code": "#!/usr/bin/env ruby\n\nn = gets.to_i\narr = gets.split.map{|x| x.to_i}\narr.sort!\n\n$ans = 0\nlast = -1\nstreak = 0\narr.each do |x|\n\tputs x\n\tif x != last then\n\t\tlast = x\n\t\t$ans = streak unless streak <= $ans\n\t\tstreak = 1\n\telse\n\t\tstreak += 1\n\tend\nend\n$ans = streak unless streak <= $ans\n\nputs $ans\n"}], "src_uid": "0cbd3eee259b1436f82e259be7d7ee0e"} {"nl": {"description": "Robot Bender decided to make Fray a birthday present. He drove n nails and numbered them from 1 to n in some order. Bender decided to make a picture using metal rods. The picture is a closed polyline, which vertices should be nails (in the given order). The segments of the polyline should be parallel to the coordinate axes. Polyline is allowed to have self-intersections. Bender can take a rod and fold it exactly once in any place to form an angle of 90 degrees. Then he can attach the place of the fold to some unoccupied nail and attach two ends of this rod to adjacent nails. A nail is considered unoccupied if there is no rod attached to it (neither by it's end nor the by the fold place). No rod could be used twice. It is not required to use all the rods.Help Bender to solve this difficult task.", "input_spec": "The first line contains two positive integers n and m (4 ≤ n ≤ 500, 2 ≤ m ≤ 500, n is even) — the amount of nails and the amount of rods. i-th of the following n lines contains a pair of integers, denoting the coordinates of the i-th nail. Nails should be connected in the same order as they are given in the input. The last line contains m integers — the lenghts of the rods. All coordinates do not exceed 104 by absolute value. Lengths of the rods are between 1 and 200 000. No rod can be used twice. It is guaranteed that all segments of the given polyline are parallel to coordinate axes. No three consecutive nails lie on the same line.", "output_spec": "If it is impossible to solve Bender's problem, output NO. Otherwise, output YES in the first line, and in the second line output n numbers — i-th of them should be the number of rod, which fold place is attached to the i-th nail, or -1, if there is no such rod. If there are multiple solutions, print any of them.", "sample_inputs": ["4 2\n0 0\n0 2\n2 2\n2 0\n4 4", "6 3\n0 0\n1 0\n1 1\n2 1\n2 2\n0 2\n3 2 3", "6 3\n0 0\n1 0\n1 1\n2 1\n2 2\n0 2\n2 2 3"], "sample_outputs": ["YES\n1 -1 2 -1", "YES\n1 -1 2 -1 3 -1", "NO"], "notes": null}, "positive_code": [{"source_code": "def length(a,b)\n\t(a[0]-b[0]).abs+(a[1]-b[1]).abs\nend\n\ndef diff_list(pts)\n\tresult=pts.each_cons(2).map{|a,b|length(a,b)}\n\tresult< y\n\tmoney += less*x\nend\n\np money\n"}, {"source_code": "n, total_cost, min_so_far = gets.chomp.to_i, 0, 101\nn.times do |t|\n\ta,p = gets.chomp.split(\" \").map(&:to_i)\n\tmin_so_far = p if p < min_so_far\n\ttotal_cost += a*min_so_far\nend\n\nputs total_cost"}, {"source_code": "n = gets.to_i\ntotal = 0\nmn = 1000\nfor i in 1..n\n a,b = gets.split().map(&:to_i)\n mn = [mn,b].min\n total+= a*mn\nend\nputs total"}, {"source_code": "# http://codeforces.com/contest/588/problem/A\n# A. Duff and Meat\n\nn = gets.chomp.to_i\n\nsum = 0\nmin = 1000\nn.times do\n a, c = gets.chomp.split.map(&:to_i)\n min = c if min > c\n\n sum += min * a\nend\n\nputs sum\n"}, {"source_code": "\nn = gets.chomp.to_i;\nmin = 0x7fffffff;\ncost = 0;\nn.times do |x|\n a, p = gets.chomp.split.map(&:to_i);\n min = [min, p].min;\n cost += a * min;\nend\nputs cost;\n"}, {"source_code": "n = gets.strip.to_i\na = []\np = []\nn.times do |i|\n a[i], p[i] = gets.strip.split(\" \").map(&:to_i)\nend\ni = 0\nmoney = 0\nwhile i < n do \n money += a[i] * p[i]\n j = i + 1\n while j < n && p[j] > p[i] do \n money += a[j] * p[i]\n j += 1\n end\n i = j\nend\nputs money"}, {"source_code": "#! ruby\n# Try Codeforces Round 326 div2\n# author: Leonardone @ NEETSDKASU\n\nn = gets.to_i\n\npMin = 100\ntotal = 0\n\nn.times do\n ai, pi = gets.chomp.split.map(&:to_i)\n pMin = [pi, pMin].min\n total += ai * pMin\nend\n\nputs total"}, {"source_code": "n = gets.to_i\na = []\nn.times{\n x, y = gets.split(\" \").map(&:to_i)\n a << [x,y]\n}\nans = 0\ncur = a[0][1]\na.each{ |x|\n cur = [cur,x[1]].min\n ans += cur*x[0]\n}\nputs ans"}, {"source_code": "n = readline.to_i\np = 100; ans = 0; num = 0\nn.times do\n a, b = readline.split().map(&:to_i)\n p = b if p > b\n ans += p * a\nend\nputs ans\n"}, {"source_code": "n=gets.chomp.to_i\n\na=0\nb=[]\n\nwhile a3 and i 0 do\n\tn -= 1\n\ta,p = gets.chomp.split(' ').map!{ |o| o.to_i }\n\tprice = p if price > p\n\tmoney += price * a\nend\n\nputs money\n"}, {"source_code": "n = gets.to_i\nr,m = 0,1000\nn.times do |i|\n\ta,b = gets.split.map(&:to_i)\n\tm = [ b , m ].min\n\tr += m * a\nend\np r"}, {"source_code": "\n \nnumDays = gets.chomp.to_i\n \nminPrice = 100 # max price\ntotalCost = 0\n \nfor i in 0..numDays-1\n\n inStr = gets.chomp.split(\" \")\n neededOnDay = inStr[0].to_i\n priceOnDay = inStr[1].to_i\n \n if priceOnDay < minPrice\n minPrice = priceOnDay\n end\n \n totalCost += minPrice * neededOnDay\n \n \nend\n\nputs totalCost"}, {"source_code": "\n \nnumDays = gets.chomp.to_i\nneededOnDay = [] # Array.new(numDays)\npriceOnDay = [] # Array.new(numDays)\n \nminPrice = 100 # max price\ntotalCost = 0\n \nfor i in 0..numDays-1\n\n inStr = gets.chomp.split(\" \")\n neededOnDay.push(inStr[0].to_i)\n priceOnDay.push(inStr[1].to_i)\n \n if priceOnDay[i] < minPrice\n minPrice = priceOnDay[i]\n end\n \n totalCost += minPrice * neededOnDay[i]\n \n \nend\n\nputs totalCost"}, {"source_code": "n = gets.to_i\n\neat = Array.new\nprice = Array.new\n(1..n).each do\n tokens = gets.split ' '\n eat.push tokens[0].to_i\n price.push tokens[1].to_i\nend\n\nindex = 0\ntotal = 0\n\nwhile index < n do\n pp = price[index]\n store = eat[index]\n\n while index+1 < n and pp < price[index + 1] do\n index += 1\n store += eat[index]\n end\n total += store * pp\n index += 1\nend\n\nputs total\n\n"}, {"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\n\nn = gets.to_i\nspent = 0\nlowest_price = 100\nn.times {\n meat, price = gets.split.map(&:to_i)\n\n if price < lowest_price\n lowest_price = price\n end\n\n # this would be enough.\n # knowing the lowest price, count required amount and multiply it by currently known lowest price\n spent += meat*lowest_price\n}\n\nputs spent\n"}, {"source_code": "n = gets.chomp.to_i\nas = []\nps = []\nn.times do\n a, p = gets.chomp.split(' ').map(&:to_i)\n as << a\n ps << p\nend\n\nm = 0\nminp = ps[0]\nfor i in 0..n-1\n a = as[i]\n p = ps[i]\n minp = [minp, p].min\n m += a * minp\nend\n\nputs m\n"}, {"source_code": "days = gets.chomp.to_i\ndata = []\nfor i in (0..days-1)\n\tdata[i] = gets.chomp\n\tdata[i] = data[i].split\nend\n\ncost = 0\nfor i in (0..days-1)\n\tif i == 0\n\t\tcost += (data[i][0].to_i * data[i][1].to_i)\n\telse\n\t\tif data[i-1][1].to_i <= data[i][1].to_i\n\t\t\tdata[i][1] = data[i-1][1].to_i\n\t\t\tcost += (data[i][0].to_i * data[i][1].to_i)\n\t\telse\n\t\t\tcost += (data[i][0].to_i * data[i][1].to_i)\n\t\tend\n\tend\nend\n\nprint cost"}, {"source_code": "n = gets.to_i\nb, ans = 100, 0\nn.times do\n a, p = gets.split.map(&:to_i)\n b = p if p < b\n ans += a * b\nend\nputs ans\n"}, {"source_code": "n = gets.to_i\na, p = [], []\nn.times.each{|i| a[i], p[i] = gets.split.map(&:to_i)}\nbuy, i, total = 0, 0, 0\nn.times do |j|\n buy += a[j]\n if j == n - 1 || p[j + 1] < p[i]\n total += p[i] * buy\n i = j + 1\n buy = 0\n end\nend\np total\n"}, {"source_code": "n = gets.to_i\nb, total = 100, 0\nn.times do\n a, p = gets.split.map(&:to_i)\n b = p if p < b\n total += a * b\nend\nputs total\n"}, {"source_code": "n = gets.chomp.to_i\n\na = []\np = []\n\nn.times do |i|\n aa, pp = gets.chomp.split(\" \").map(&:to_i)\n a << aa\n p << pp\nend\n\ndef lowest_cost(a, p)\n if a.size == 0\n 0\n elsif a.size == 1\n a[0] * p[0]\n else\n low_cost = p.min\n low_cost_day = p.index(low_cost)\n\n kg = 0\n a[low_cost_day..-1].each do |k|\n kg += k\n end\n cost = kg * low_cost + lowest_cost(a[0...low_cost_day], p[0...low_cost_day])\n end\nend\n\nputs lowest_cost(a, p)\n"}, {"source_code": "n = gets.to_i\nmin = 109\nsum = 0\nn.times do\n a, b = gets.split(' ').map(&:to_i)\n if b < min\n min = b\n end\n sum += min * a\nend\nprint sum, \"\\n\"\n"}, {"source_code": "min = 1000\nsum = 1.upto(gets.to_i).inject(0) do |sum, idx|\n a, p = gets.split(' ').map(&:to_i)\n\n min = [min, p].min\n\n sum + a*min\nend\n\nputs sum\n"}, {"source_code": "\nn = gets.to_i\na1,b1 = gets.split.map(&:to_i)\ns = a1 * b1\nfor i in(2..n)\n a,b = gets.split.map(&:to_i)\n if(b>b1)\n s = s + (a*b1)\n \n else\n s = s + (a*b)\n b1=b\n end\nend\np s"}, {"source_code": "n = gets.to_i\n\nlast_min = 0x3f3f3f3f\n\nans = 0\nn.times do\n\ta, p = gets.split.map { |s| s.to_i }\n\tlast_min = [last_min, p].min\n\tans += last_min * a\nend\n\nputs ans\n"}, {"source_code": "totalDay = gets.chomp.to_i\nmeatHash ||= {}\ncheapestPrice ||= 500\nneededMoney = 0\n\ntotalDay.times do\n\tline = gets.chomp.split(' ').map {|item| item.to_i}\n\tneeded, price = line[0], line[1]\n\n\tcheapestPrice = price if cheapestPrice > price\n\tneededMoney += cheapestPrice * needed\nend\n\nputs neededMoney"}], "negative_code": [{"source_code": "# http://codeforces.com/contest/588/problem/A\n# A. Duff and Meat\n\nn = gets.chomp.to_i\n\nsum = 0\nmin = 100.0\nn.times do\n a, c = gets.chomp.split.map(&:to_i)\n min = c if min > c\n\n sum += min * a\nend\n\nputs sum\n"}, {"source_code": "# http://codeforces.com/contest/588/problem/A\n# A. Duff and Meat\n\nn = gets.chomp.to_i\n\namounts = []\ncosts = []\nmin = 100.0\nn.times do\n a, c = gets.chomp.split.map(&:to_i)\n min = c if min > c\n amounts << a\n costs << c\nend\n\narr = []\namounts.zip(costs).each.with_index do |v, idx|\n a, c = v\n if c == min\n arr << amounts[idx..-1].map { |e| e * min }\n break\n else\n arr << a * c\n end\nend\nputs arr.flatten.inject(0, :+)\n"}, {"source_code": "\nn = gets.to_i\na1,b1 = gets.split.map(&:to_i)\ns = a1 * b1\nfor i in(2..n)\n a,b = gets.split.map(&:to_i)\n if(b>b1)\n s = s + (a*b1)\n else\n s = s + (a*b)\n end\n b1=b\nend\np s"}], "src_uid": "28e0822ece0ed35bb3e2e7fc7fa6c697"} {"nl": {"description": "There is a game called \"I Wanna Be the Guy\", consisting of n levels. Little X and his friend Little Y are addicted to the game. Each of them wants to pass the whole game.Little X can pass only p levels of the game. And Little Y can pass only q levels of the game. You are given the indices of levels Little X can pass and the indices of levels Little Y can pass. Will Little X and Little Y pass the whole game, if they cooperate each other?", "input_spec": "The first line contains a single integer n (1 ≤  n ≤ 100). The next line contains an integer p (0 ≤ p ≤ n) at first, then follows p distinct integers a1, a2, ..., ap (1 ≤ ai ≤ n). These integers denote the indices of levels Little X can pass. The next line contains the levels Little Y can pass in the same format. It's assumed that levels are numbered from 1 to n.", "output_spec": "If they can pass all the levels, print \"I become the guy.\". If it's impossible, print \"Oh, my keyboard!\" (without the quotes).", "sample_inputs": ["4\n3 1 2 3\n2 2 4", "4\n3 1 2 3\n2 2 3"], "sample_outputs": ["I become the guy.", "Oh, my keyboard!"], "notes": "NoteIn the first sample, Little X can pass levels [1 2 3], and Little Y can pass level [2 4], so they can pass all the levels both.In the second sample, no one can pass level 4."}, "positive_code": [{"source_code": "g=->{gets.split[1..-1]};require 'set';puts gets.to_i==Set.new(g[]+g[]).size ? 'I become the guy.':'Oh, my keyboard!'"}, {"source_code": "g=->{gets.split[1..-1]};puts gets.to_i==(g[]+g[]).uniq.size ? 'I become the guy.':'Oh, my keyboard!'"}, {"source_code": "levels = gets.chomp.to_i\nx = gets.chomp\nx = x.split\ny = gets.chomp\ny = y.split\n\narray = []\nx_levels = x[0].to_i\ny_levels = y[0].to_i\nfor i in (1..x.size-1)\n\tif array.include?(x[i].to_i) == false && x[i].to_i != 0\n\t\tarray << x[i].to_i\n\tend\nend \nfor i in (1..y.size-1)\n\tif array.include?(y[i].to_i) == false && y[i].to_i != 0\n\t\tarray << y[i].to_i\n\tend\nend \n\nif levels == array.size\n\tprint \"I become the guy.\"\nelse\n\tprint \"Oh, my keyboard!\"\nend"}, {"source_code": "g=->{gets.split[1..-1]};puts gets.to_i==(g[]+g[]).uniq.size ? 'I become the guy.':'Oh, my keyboard!'\n"}, {"source_code": "#puts \"Enter the number of levels\\n\"\nn = gets.to_i\n#puts \"Enter number of level x can pass then level number in a single line and same for y\\n\"\nx = gets.chomp.to_s.split(\" \")\ny = gets.chomp.to_s.split(\" \")\na = []\nl = x.length\nm = 0\nfor i in 1...l do\n a[m] = x[i].to_i\n m += 1\nend\nfor j in 1...y.length do\n a[m+j-1] = y[j].to_i\nend\na = a.sort\nw = a.length\nv = 1\nr = [n]\nfor h in 1..n do\n c = 0\n d = 0\n for k in 0...w do\n if h != a[k]\n c += 0\n else\n c += 1\n end \n end\n r[h-1] = c\nend\nr = r.sort\n#p r\nif r[0] == 0\n puts \"Oh, my keyboard!\"\nelse\n puts \"I become the guy.\"\nend\n "}, {"source_code": "n = gets.to_i\np1, p2 = gets.split(/ /).drop(1).map(&:to_i), gets.split(/ /).drop(1).map(&:to_i)\nputs (p1 | p2).count == n ? \"I become the guy.\" : \"Oh, my keyboard!\""}, {"source_code": "n = gets.to_i\narr = Array.new(n,false)\ngets.split(\" \").map(&:to_i).drop(1).each{ |i|\n arr[i-1] = true if i > 0\n}\n\ngets.split(\" \").map(&:to_i).drop(1).each{ |i|\n arr[i-1] = true if i > 0\n}\n\nputs arr.all? ? \"I become the guy.\" : \"Oh, my keyboard!\""}, {"source_code": "a = gets.to_i\nb = gets.split.map &:to_i\nc = gets.split.map &:to_i\nputs (c[1..-1] | b[1..-1]).size == a ? \"I become the guy.\" : \"Oh, my keyboard!\"\n"}, {"source_code": "g=->{gets.split[1..-1]};puts gets.to_i==(g[]+g[]).uniq.size ? 'I become the guy.':'Oh, my keyboard!'\n"}, {"source_code": "n = gets.to_i\nif (gets.split[1..-1].map(&:to_i) | gets.split[1..-1].map(&:to_i)).size == n\n\tputs \"I become the guy.\"\nelse\n\tputs \"Oh, my keyboard!\"\nend\n"}, {"source_code": "n = gets.to_i\nx = gets.split(' ').map(&:to_i)\ny = gets.split(' ').map(&:to_i)\nl = []\n\nx.drop(1).each do |i|\n if i > 0 && !l.include?(i)\n l.push(i)\n end\nend\n\ny.drop(1).each do |j|\n if j > 0 && !l.include?(j)\n l.push(j)\n end\nend\n\nputs l.length == n ? 'I become the guy.' : 'Oh, my keyboard!'"}, {"source_code": "n = gets.to_i\na=gets.split.map(&:to_i)[1..-1];b=gets.split.map(&:to_i)[1..-1] \nputs (a+b).uniq.size==n ? \"I become the guy.\" : \"Oh, my keyboard!\"\n"}, {"source_code": "require 'set'\nn = gets.chomp.to_i\nline = gets.chomp.split\nset = Set.new\n\np = line[0].to_i\nfor i in 1..p\n set.add(line[i].to_i)\nend\n\n\nline = gets.chomp.split\np = line[0].to_i\nfor i in 1..p\n set.add(line[i].to_i)\nend\n\nif set == (1..n).to_a.to_set\n puts \"I become the guy.\"\nelse\n puts \"Oh, my keyboard!\"\nend\n\n"}, {"source_code": "g=->{gets.split[1..-1]};puts gets.to_i==(g[]+g[]).uniq.size ? 'I become the guy.':'Oh, my keyboard!'"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)[1..-1]\nb = gets.split.map(&:to_i)[1..-1]\n\nputs a.concat(b).uniq.size == n ? 'I become the guy.' : 'Oh, my keyboard!'"}, {"source_code": "g=->{gets.split[1..-1]};puts gets.to_i==(g[]+g[]).uniq.size ? 'I become the guy.':'Oh, my keyboard!'\n"}, {"source_code": "n = gets.to_i\na = gets.split(' ').map(&:to_i) \nb = gets.split(' ').map(&:to_i)\n\nputs((a[1..-1]+b[1..-1]).sort.uniq.size == n ? 'I become the guy.' : 'Oh, my keyboard!')\n"}, {"source_code": "g=->{gets.split[1..-1]};puts gets.to_i==(g[]+g[]).uniq.size ? 'I become the guy.':'Oh, my keyboard!'\n"}, {"source_code": "require 'set'\n\ndef read_next_line\n gets.chomp.split.map(&:to_i)\nend\n\nn = gets.chomp.to_i\nputs (Set.new(read_next_line[1..-1]) + read_next_line[1..-1]).size == n ? 'I become the guy.' : 'Oh, my keyboard!'"}, {"source_code": "n = gets.to_i\np = gets.split.map(&:to_i)\nq = gets.split.map(&:to_i)\n\nif ((p[1..-1] + q[1..-1]).uniq).size == n\n\tputs \"I become the guy.\"\nelse\n\tputs \"Oh, my keyboard!\"\nend\n"}, {"source_code": "n = gets.chomp.to_i\np = gets.chomp.split(\" \").map(&:to_i)\nq = gets.chomp.split(\" \").map(&:to_i)\np.delete_at(0)\nq.delete_at(0)\nres = p | q\nputs res.size == n ? \"I become the guy.\" : \"Oh, my keyboard!\""}, {"source_code": "g=->{gets.split[1..-1]};puts gets.to_i==(g[]+g[]).uniq.size ? 'I become the guy.':'Oh, my keyboard!'\n"}, {"source_code": "n = gets.to_i\na = gets.chomp.split(' ').map(&:to_i)[1..-1]\nb = gets.chomp.split(' ').map(&:to_i)[1..-1]\nputs (a | b).size == n ? \"I become the guy.\" : \"Oh, my keyboard!\""}, {"source_code": "n = gets.to_i\n\ninput = gets\np = input.split\np.shift\n\ninput = gets\nq = input.split\nq.shift\n\n# puts pn\nn.times\n\np.each { |elem| q.push(elem) }\n\nq.uniq!\n\nputs q.count == n ? \"I become the guy.\" : \"Oh, my keyboard!\""}, {"source_code": "#! /usr/bin/env ruby\n# -*- coding: utf-8 -*-\n# vim:fenc=utf-8\n#\n# Copyright © 2014 Szymon Sliwa \n#\n# Distributed under terms of the MIT license.\n\n\nn = gets.to_i\np, *tab_p = gets.split.map(&:to_i)\nq, *tab_q = gets.split.map(&:to_i)\n\ntab = tab_p + tab_q\ntab.sort!.uniq!\n\nif tab == (1..n).to_a\n puts \"I become the guy.\"\nelse\n puts \"Oh, my keyboard!\"\nend\n\n\n\n\n\n"}, {"source_code": "n = STDIN.gets.to_i\nlevels_of_x = STDIN.gets.to_s.split(' ').map(&:to_i)\nlevels_of_x.shift\nlevels_of_y = STDIN.gets.to_s.split(' ').map(&:to_i)\nlevels_of_y.shift\n\nresult = (1..n).to_a.all? do |i|\n levels_of_x.include?(i) || levels_of_y.include?(i)\nend\n\nputs result ? 'I become the guy.' : 'Oh, my keyboard!'\n"}, {"source_code": "g=->{gets.split[1..-1]};puts gets.to_i==(g[]+g[]).uniq.size ? 'I become the guy.':'Oh, my keyboard!'\n"}, {"source_code": "n = gets.chomp.to_i\nxPass = gets.chomp.split(' ').map {|item| item.to_i}\nyPass = gets.chomp.split(' ').map {|item| item.to_i}\nxPass.delete_at(0)\nyPass.delete_at(0)\nxPass.sort!\nyPass.sort!\nxPass.uniq!\nyPass.uniq!\nflag = true\n\n(1..n).each do |stage|\n\tflag = false if xPass.count(stage) == 0 && yPass.count(stage) == 0\nend\n\nputs \"I become the guy.\" if flag\nputs \"Oh, my keyboard!\" if !flag"}, {"source_code": "level_count = gets.to_i\nx_levels = gets.split.map &:to_i\nx_levels.shift\ny_levels = gets.split.map &:to_i\ny_levels.shift\n\nlevels = Array.new level_count, false\n\nx_levels.each do |element|\n levels[element - 1] = true\nend\n\ny_levels.each do |element|\n levels[element - 1] = true\nend\n\nincomplete_level = false\n\nlevel_count.times do |i|\n unless levels[i]\n puts \"Oh, my keyboard!\"\n incomplete_level = true\n break\n end\nend\n\nputs \"I become the guy.\" unless incomplete_level\n"}, {"source_code": "#!/usr/bin/ruby\nn=gets.to_i\na=gets.split.map(&:to_i)[1..-1]+gets.split.map(&:to_i)[1..-1]\nputs (1..n).all?{|i|a.include?(i)} ? 'I become the guy.' : 'Oh, my keyboard!'"}, {"source_code": "g=->{gets.split[1..-1]};puts gets.to_i==(g[]+g[]).uniq.size ? 'I become the guy.':'Oh, my keyboard!'\n"}, {"source_code": "g=->{gets.split[1..-1]};puts gets.to_i==(g[]+g[]).uniq.size ? 'I become the guy.':'Oh, my keyboard!'"}, {"source_code": "g=->{gets.split[1..-1]};puts gets.to_i==(g[]+g[]).uniq.size ? 'I become the guy.':'Oh, my keyboard!'\n"}, {"source_code": "g=->{gets.split[1..-1]};puts gets.to_i==(g[]+g[]).uniq.size ? 'I become the guy.':'Oh, my keyboard!'"}, {"source_code": "n = STDIN.readline.to_i\nps = STDIN.readline.split.map {|s| s.to_i }[1..-1]\nqs = STDIN.readline.split.map {|s| s.to_i }[1..-1]\nmark = {}\n[ps, qs].each do |xs|\n\txs.each {|x| mark[x] = true }\nend\n(1..n).each do |i|\n\tif mark[i] == nil\n\t\tputs 'Oh, my keyboard!'\n\t\texit\n\tend\nend\nputs \"I become the guy.\"\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)[1..-1]\nb = gets.split.map(&:to_i)[1..-1]\nm = Array.new(n, 0)\n\na.each { |x|\n\tm[x-1] = 1\n}\n\nb.each { |x|\n\tm[x-1] = 1\n}\n\nsum = 0\nfor i in 0...n\n\tsum += m[i]\nend\nif (sum == n)\n\tputs \"I become the guy.\"\nelse\n\tputs \"Oh, my keyboard!\"\nend\n"}, {"source_code": "\nn = gets.to_i\nx = gets.split.map(&:to_i)\ny = gets.split.map(&:to_i)\nx.delete_at(0)\ny.delete_at(0)\nfor i in x\n\ty.delete_at(y.index(i)) if !y.index(i).nil?\nend\ns = (1+n)/2.0*n\nx = x+y\nsum = 0\nfor i in x \n\tsum+=i\nend\nputs sum == s ? \"I become the guy.\" : \"Oh, my keyboard!\""}, {"source_code": "level = gets.chomp.to_i\ninput_x, input_y = gets.chomp.split(\" \"), gets.chomp.split(\" \")\ncounter = {}\ngame = []\nfor i in 1..input_x[0].to_i\n game << input_x[i]\nend\nfor i in 1..input_y[0].to_i\n game << input_y[i]\nend\nfor i in 0...game.length\n counter[game[i]] = 1\nend\nsum = 0\ncounter.each do |key, value|\n sum += value\nend\nif sum == level\n puts \"I become the guy.\"\nelse\n puts \"Oh, my keyboard!\"\nend\n"}, {"source_code": "n = $stdin.readline.to_i\na = $stdin.readline.split.map(&:to_i)\nb = $stdin.readline.split.map(&:to_i)\n\na.shift\nb.shift\n\nt = (a+b).uniq\nt.sort!\n\nputs (1..n).to_a==t ? \"I become the guy.\" : \"Oh, my keyboard!\""}, {"source_code": "arr = []\nn = gets.to_i\n2.times do\n arr.push(gets.chomp.split[1..-1].map &:to_i)\nend\narr = arr.inject(:+).uniq\nputs arr.size == n ? \"I become the guy.\" : \"Oh, my keyboard!\"\n"}, {"source_code": "g=->{gets.split[1..-1]};puts gets.to_i==(g[]+g[]).uniq.size ? 'I become the guy.':'Oh, my keyboard!'\n"}, {"source_code": "k = gets.to_i\na = gets.split(\" \").map(&:to_i)\nb = gets.split(\" \").map(&:to_i)\na << k+2\nb << k+2\nn = a[0]\nm = b[0]\na[0]=b[0]=0\na.sort!\nb.sort!\ni = j = cur = 1\nwhile i<=n || j<=m\n while cur > a[i] do\n i+=1;\n end\n while cur > b[j] do\n j+=1;\n end\n if cur == a[i] || cur == b[j]\n cur+=1;\n else\n break\n end\nend\nif cur == k+1\n puts \"I become the guy.\"\nelse\n puts \"Oh, my keyboard!\"\nend"}, {"source_code": "g=->{gets.split[1..-1]};puts gets.to_i==(g[]+g[]).uniq.size ? 'I become the guy.':'Oh, my keyboard!'\n"}, {"source_code": "levels = gets.chomp.to_i\nx = gets.chomp.split(/ /).map(&:to_i)\ny = gets.chomp.split(/ /).map(&:to_i)\np, q = x[0], y[0]\nx_levels = x[1..p]\ny_levels = y[1..q]\ntotal_levels = x_levels + y_levels\ntotal_levels.uniq!\ntotal_levels.sort!\npassed = true\n(1..levels).each do |level|\n next if total_levels[level - 1] == level\n passed = false\n break\nend\nif passed\n puts \"I become the guy.\"\nelse\n puts \"Oh, my keyboard!\"\nend\n"}, {"source_code": "g=->{gets.split[1..-1]};puts gets.to_i==(g[]+g[]).uniq.size ? 'I become the guy.':'Oh, my keyboard!'\n"}, {"source_code": "n=(1..gets.to_i).to_a\nx=gets.split.map{|e| e.to_i}\ny=gets.split.map{|e| e.to_i}\nx[0]=0\ny[0]=0\nputs (n-x-y).size==0?\"I become the guy.\":\"Oh, my keyboard!\""}, {"source_code": "n = gets.chomp.to_i\n\nlevels1 = gets.chomp.split(' ').map{|a| a.to_i}\nlevels2 = gets.chomp.split(' ').map{|a| a.to_i}\n\nlevels1.shift\nlevels2.shift\n\nif levels1.concat(levels2).uniq.sort == (1..n).to_a\n puts \"I become the guy.\"\nelse\n puts \"Oh, my keyboard!\"\nend\n"}, {"source_code": "levels = [false] * gets.to_i\nx = gets.split.map(&:to_i)\nx.shift\ny = gets.split.map(&:to_i)\ny.shift\n\nx.each { |l| levels[l - 1] = true }\ny.each { |l| levels[l - 1] = true }\n\nif levels.all?\n puts 'I become the guy.'\nelse\n puts 'Oh, my keyboard!'\nend\n"}, {"source_code": "n=gets.chomp.to_i\na = gets.chomp.strip.split\nb = gets.chomp.strip.split\na=a[1..-1]\nb=b[1..-1]\nputs ((a+b).uniq-['0']).length == n ? \"I become the guy.\" : \"Oh, my keyboard!\"\n"}, {"source_code": "n = gets.chomp.to_i\nx = gets.chomp.split(' ').map(&:to_i).drop(1)\ny = gets.chomp.split(' ').map(&:to_i).drop(1)\n\ns = (x + y).uniq\n\nif s.size == n\n puts \"I become the guy.\"\nelse\n puts \"Oh, my keyboard!\"\nend"}, {"source_code": "require 'set';puts gets.to_i==Set.new(gets.split[1..-1]+gets.split[1..-1]).size ? 'I become the guy.':'Oh, my keyboard!'"}], "negative_code": [{"source_code": "g=->{gets.split[1..-1]};puts gets.to_i==(g[]+g[]).uniq ? 'I become the guy.':'Oh, my keyboard!'"}, {"source_code": "levels = gets.chomp.to_i\nx = gets.chomp\nx = x.split\ny = gets.chomp\ny = y.split\n\narray = []\nfor i in (0..x.size-1)\n\tif array.include?(x[i].to_i) == false && x[i].to_i != 0\n\t\tarray << x[i].to_i\n\tend\nend \nfor i in (0..y.size-1)\n\tif array.include?(y[i].to_i) == false && y[i].to_i != 0\n\t\tarray << y[i].to_i\n\tend\nend \n\nif levels == array.size\n\tprint \"I become the guy.\"\nelse\n\tprint \"Oh, my keyboard!\"\nend"}, {"source_code": "n = gets.to_i\narr = Array.new(n,false)\ngets.split(\" \").map(&:to_i).each{ |i|\n arr[i-1] = true if i > 0\n}\n\ngets.split(\" \").map(&:to_i).each{ |i|\n arr[i-1] = true if i > 0\n}\n\nputs arr.all? ? \"I become the guy.\" : \"Oh, my keyboard!\""}, {"source_code": "n = gets.to_i\narr = Array.new(n,false)\ngets.split(\" \").map(&:to_i).each{ |i|\n arr[i-1] = true\n}\n\ngets.split(\" \").map(&:to_i).each{ |i|\n arr[i-1] = true\n}\n\nputs arr.all? ? \"I become the guy.\" : \"Oh, my keyboard!\""}, {"source_code": "a = gets.to_i\nb = gets.split.map &:to_i\nc = gets.split.map &:to_i\nd = (b.uniq + c.uniq).sort\nf = true\n(1..a).map {|x| f = d.include?(x)}\nputs f ? \"I become the guy.\" : \"Oh, my keyboard!\"\n"}, {"source_code": "a = gets.to_i\nb = gets.split.map &:to_i\nc = gets.split.map &:to_i\nputs (b + c).uniq.size == a ? \"I become the guy.\" : \"Oh, my keyboard!\"\n"}, {"source_code": "a = gets.to_i\nb = gets.split.map &:to_i\nc = gets.split.map &:to_i\nputs (c | b).size == a ? \"I become the guy.\" : \"Oh, my keyboard!\"\n"}, {"source_code": "n = gets.to_i\nx = gets.split(' ').map(&:to_i)\n# x.shift\ny = gets.split(' ').map(&:to_i)\nl = []\n\nx.drop(1).each do |i|\n if i > 0 && !l.include?(i)\n l.push(i)\n end\nend\n\ny.each do |j|\n if j > 0 && !l.include?(j)\n l.push(j)\n end\nend\n\nputs l.length == n ? 'I become the guy.' : 'Oh, my keyboard!'"}, {"source_code": "n = gets.to_i\na=gets.split.map(&:to_i);b=gets.split.map(&:to_i) \np (a+b).uniq.size==4?\"I become the guy.\":\"Oh, my keyboard!\""}, {"source_code": "n = gets.to_i\na=gets.split.map(&:to_i);b=gets.split.map(&:to_i) \nputs (a+b).uniq.size==n ? \"I become the guy.\" : \"Oh, my keyboard!\""}, {"source_code": "n = gets.to_i\na=gets.split.map(&:to_i);b=gets.split.map(&:to_i) \nputs (a+b).uniq.size==4?\"I become the guy.\":\"Oh, my keyboard!\""}, {"source_code": "n = gets.to_i\na=gets.split.map(&:to_i);b=gets.split.map(&:to_i);a.delete(0);b.delete(0)\nputs a.concat(b).uniq.size==n ? \"I become the guy.\" : \"Oh, my keyboard!\""}, {"source_code": "require 'set'\nn = gets.chomp.to_i\nline = gets.chomp.split\np = line[0].to_i\nset = Set.new\n\nfor i in 1..p\n set.add(line[i].to_i)\nend\n\nline = gets.chomp.split\nfor i in 0..p-1\n set.add(line[i].to_i)\nend\n\nif set == (1..n).to_a.to_set\n puts \"I become the guy.\"\nelse\n puts \"Oh, my keyboard!\"\nend\n\n"}, {"source_code": "n = STDIN.gets.to_i\nlevels_of_x = STDIN.gets.to_s.split(' ').map(&:to_i)\nlevels_of_x.shift\nlevels_of_y = STDIN.gets.to_s.split(' ').map(&:to_i)\nlevels_of_y.shift\n\nresult = (1..n).to_a.all? do |i|\n levels_of_x.include?(i) || levels_of_y.include?(i)\nend\n\nputs result ? 'I became the guy.' : 'Oh, my keyboard!'\n"}, {"source_code": "n = gets.chomp.to_i\nxPass = gets.chomp.split(' ').map {|item| item.to_i}\nyPass = gets.chomp.split(' ').map {|item| item.to_i}\nxPass.sort!\nyPass.sort!\nxPass.uniq!\nyPass.uniq!\nflag = true\n\n(1..n).each do |stage|\n\tflag = false if xPass.count(stage) == 0 && yPass.count(stage) == 0\nend\n\nputs \"I become the guy.\" if flag\nputs \"Oh, my keyboard!\" if !flag"}, {"source_code": "\nn = gets.to_i\nx = gets.split.map(&:to_i)\ny = gets.split.map(&:to_i)\nx.delete_at(0)\ny.delete_at(0)\nfor i in x\n\ty.delete_at(y.index(i)) if y.index(i)\nend\ns = (1+n)/2.0*n\nx = x+y\nsum = 0\nfor i in x \n\tsum+=i\nend\nputs sum == s ? \"I become the guy\" : \"Oh, my keyboard!\""}, {"source_code": "level = gets.chomp.to_i\nlittle_x, little_y = gets.chomp.split(\" \"), gets.chomp.split(\" \")\ngame = little_x + little_y\ngame.map! {|i| i.to_i}\nif game.max >= level\n puts \"I become the guy.\"\nelse\n puts \"Oh, my keyboard!\"\nend"}, {"source_code": "level = gets.chomp.to_i\nlittle_x, little_y = gets.chomp.split(\" \"), gets.chomp.split(\" \")\ngame = (little_x + little_y)\nif game.max.to_i == level\n puts \"I become the guy.\"\nelse\n puts \"Oh, my keyboard!\"\nend"}, {"source_code": "level = gets.chomp.to_i\nlittle_x, little_y = gets.chomp.split(\" \"), gets.chomp.split(\" \")\ngame = little_x + little_y\ncounter = {}\nfor i in 0...game.length\n counter[game[i]] = 1\nend\nsum = 0\ncounter.each do |key, value|\n sum += value\nend\nif sum == level\n puts \"I become the guy.\"\nelse\n puts \"Oh, my keyboard!\"\nend"}, {"source_code": "level = gets.chomp.to_i\nlittle_x, little_y = gets.chomp.split(\" \"), gets.chomp.split(\" \")\ngame = little_x + little_y\ncounter = {}\nfor i in 0...game.length\n if game[i] != '0'\n counter[game[i]] = 1\n end\nend\nsum = 0\ncounter.each do |key, value|\n sum += value\nend\nif sum == level\n puts \"I become the guy.\"\nelse\n puts \"Oh, my keyboard!\"\nend\n"}, {"source_code": "arr = []\nn = gets.to_i\n2.times do \n arr.push(gets.chomp.split.map &:to_i)\nend\narr = arr.inject(:+)\narr = arr - [0]\nputs arr.uniq.size == n ? \"I become the guy.\" : \"Oh, my keyboard!\"\n\n"}, {"source_code": "arr = []\nn = gets.to_i\n2.times do\n arr.push(gets.chomp[1..-1])\nend\narr = arr.inject(:+)\narr = arr.split.map &:to_i\nputs arr.uniq.size == n ? \"I become the guy.\" : \"Oh, my keyboard!\"\n"}, {"source_code": "arr = []\nn = gets.to_i\n2.times do \n arr.push(gets.chomp.split.map &:to_i)\nend\narr = arr.inject(:+)\nputs arr.uniq.size == n ? \"I become the guy.\" : \"Oh, my keyboard!\"\n"}, {"source_code": "n = gets.to_i\na = gets.split(\" \").map(&:to_i)\nb = gets.split(\" \").map(&:to_i)\na << n+2\nb << n+2\nn = a[0]\nm = b[0]\ni = j = cur = 1\nwhile i<=n || j<=m\n while cur > a[i] do\n i+=1;\n end\n while cur > b[j] do\n j+=1;\n end\n if cur == a[i] || cur == b[j]\n cur+=1;\n else\n break\n end\nend\nif cur = n+1\n puts \"I become the guy.\"\nelse\n puts \"Oh, my keyboard!\"\nend"}, {"source_code": "k = gets.to_i\na = gets.split(\" \").map(&:to_i)\nb = gets.split(\" \").map(&:to_i)\na << k+2\nb << k+2\nn = a[0]\nm = b[0]\ni = j = cur = 1\nwhile i<=n || j<=m\n while cur > a[i] do\n i+=1;\n end\n while cur > b[j] do\n j+=1;\n end\n if cur == a[i] || cur == b[j]\n cur+=1;\n else\n break\n end\nend\nif cur == k+1\n puts \"I become the guy.\"\nelse\n puts \"Oh, my keyboard!\"\nend"}, {"source_code": "levels = gets.chomp.to_i\nx = gets.chomp.split(/ /).map(&:to_i)\ny = gets.chomp.split(/ /).map(&:to_i)\nif x.include?( levels ) || y.include?( levels )\n puts \"I become the guy.\"\nelse\n puts \"Oh, my keyboard!\"\nend\n"}, {"source_code": "levels = gets.chomp.to_i\nx = gets.chomp.split(/ /).map(&:to_i)\ny = gets.chomp.split(/ /).map(&:to_i)\np, q = x[0], y[0]\nx_levels = x[1..p]\ny_levels = y[1..q]\nif x_levels.include?( levels ) || y_levels.include?( levels )\n puts \"I become the guy.\"\nelse\n puts \"Oh, my keyboard!\"\nend\n"}, {"source_code": "n = gets.chomp.to_i\n\nlevels1 = gets.chomp.split(' ').map{|a| a.to_i}\nlevels2 = gets.chomp.split(' ').map{|a| a.to_i}\n\nlevels1.delete(0)\nlevels2.delete(0)\n\nif levels1.concat(levels2).uniq.sort == (1..n).to_a\n puts \"I become the guy.\"\nelse\n puts \"Oh, my keyboard!\"\nend\n"}, {"source_code": "n = gets.chomp.to_i\n\nlevels1 = gets.chomp.split(' ').map{|a| a.to_i}\nlevels2 = gets.chomp.split(' ').map{|a| a.to_i}\n\nif levels1.concat(levels2).uniq.count >= n\n puts \"I become the guy.\"\nelse\n puts \"Oh, my keyboard!\"\nend\n"}, {"source_code": "n = gets.chomp.to_i\n\nlevels1 = gets.chomp.split(' ').map{|a| a.to_i}\nlevels2 = gets.chomp.split(' ').map{|a| a.to_i}\n\nif (levels1.concat(levels2).uniq.count == n) \n puts \"I become the guy.\"\nelse\n puts \"Oh, my keyboard!\"\nend\n"}, {"source_code": "levels = [false] * gets.to_i\nx = gets.split.map(&:to_i)\nx.shift\ny = gets.split.map(&:to_i)\ny.shift\n\nx.each { |l| levels[l - 1] = true }\ny.each { |l| levels[l - 1] = true }\n\nif x.all?\n puts 'I become the guy.'\nelse\n puts 'Oh, my keyboard!'\nend\n"}, {"source_code": "n=gets.chomp.to_i\na = gets.chomp.strip.split\nb = gets.chomp.strip.split\nputs ((a+b).uniq-['0']).length == n ? \"I become the guy.\" : \"Oh, my keyboard!\"\n"}, {"source_code": "n=gets.chomp.to_i\na = gets.chomp.strip.split\nb = gets.chomp.strip.split\nputs (a+b).uniq.length == n ? \"I become the guy.\" : \"Oh, my keyboard!\"\n"}, {"source_code": "n=gets.chomp.to_i\na = gets.chomp.strip.split\nb = gets.chomp.strip.split\nputs (a+b).uniq.length == 4? \"I become the guy.\" : \"Oh, my keyboard!\"\n"}, {"source_code": "n = gets.chomp.to_i\nx = gets.chomp.split(' ').map(&:to_i).drop(1)\ny = gets.chomp.split(' ').map(&:to_i)\n\ns = (x + y).uniq\n\nif s.size == n\n puts \"I become the guy.\"\nelse\n puts \"Oh, my keyboard!\"\nend"}], "src_uid": "044ade01d2de1486735742369227ae1d"} {"nl": {"description": "Vasya writes his own library for building graphical user interface. Vasya called his creation VTK (VasyaToolKit). One of the interesting aspects of this library is that widgets are packed in each other. A widget is some element of graphical interface. Each widget has width and height, and occupies some rectangle on the screen. Any widget in Vasya's library is of type Widget. For simplicity we will identify the widget and its type. Types HBox and VBox are derivatives of type Widget, so they also are types Widget. Widgets HBox and VBox are special. They can store other widgets. Both those widgets can use the pack() method to pack directly in itself some other widget. Widgets of types HBox and VBox can store several other widgets, even several equal widgets — they will simply appear several times. As a result of using the method pack() only the link to the packed widget is saved, that is when the packed widget is changed, its image in the widget, into which it is packed, will also change. We shall assume that the widget a is packed in the widget b if there exists a chain of widgets a = c1, c2, ..., ck = b, k ≥ 2, for which ci is packed directly to ci + 1 for any 1 ≤ i < k. In Vasya's library the situation when the widget a is packed in the widget a (that is, in itself) is not allowed. If you try to pack the widgets into each other in this manner immediately results in an error.Also, the widgets HBox and VBox have parameters border and spacing, which are determined by the methods set_border() and set_spacing() respectively. By default both of these options equal 0. The picture above shows how the widgets are packed into HBox and VBox. At that HBox and VBox automatically change their size depending on the size of packed widgets. As for HBox and VBox, they only differ in that in HBox the widgets are packed horizontally and in VBox — vertically. The parameter spacing sets the distance between adjacent widgets, and border — a frame around all packed widgets of the desired width. Packed widgets are placed exactly in the order in which the pack() method was called for them. If within HBox or VBox there are no packed widgets, their sizes are equal to 0 × 0, regardless of the options border and spacing. The construction of all the widgets is performed using a scripting language VasyaScript. The description of the language can be found in the input data. For the final verification of the code Vasya asks you to write a program that calculates the sizes of all the widgets on the source code in the language of VasyaScript. ", "input_spec": "The first line contains an integer n — the number of instructions (1 ≤ n ≤ 100). Next n lines contain instructions in the language VasyaScript — one instruction per line. There is a list of possible instructions below. \"Widget [name]([x],[y])\" — create a new widget [name] of the type Widget possessing the width of [x] units and the height of [y] units. \"HBox [name]\" — create a new widget [name] of the type HBox. \"VBox [name]\" — create a new widget [name] of the type VBox. \"[name1].pack([name2])\" — pack the widget [name2] in the widget [name1]. At that, the widget [name1] must be of type HBox or VBox. \"[name].set_border([x])\" — set for a widget [name] the border parameter to [x] units. The widget [name] must be of type HBox or VBox. \"[name].set_spacing([x])\" — set for a widget [name] the spacing parameter to [x] units. The widget [name] must be of type HBox or VBox. All instructions are written without spaces at the beginning and at the end of the string. The words inside the instruction are separated by exactly one space. There are no spaces directly before the numbers and directly after them. The case matters, for example, \"wiDget x\" is not a correct instruction. The case of the letters is correct in the input data. All names of the widgets consist of lowercase Latin letters and has the length from 1 to 10 characters inclusive. The names of all widgets are pairwise different. All numbers in the script are integers from 0 to 100 inclusive It is guaranteed that the above-given script is correct, that is that all the operations with the widgets take place after the widgets are created and no widget is packed in itself. It is guaranteed that the script creates at least one widget. ", "output_spec": "For each widget print on a single line its name, width and height, separated by spaces. The lines must be ordered lexicographically by a widget's name. Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use cout stream (also you may use %I64d specificator)", "sample_inputs": ["12\nWidget me(50,40)\nVBox grandpa\nHBox father\ngrandpa.pack(father)\nfather.pack(me)\ngrandpa.set_border(10)\ngrandpa.set_spacing(20)\nWidget brother(30,60)\nfather.pack(brother)\nWidget friend(20,60)\nWidget uncle(100,20)\ngrandpa.pack(uncle)", "15\nWidget pack(10,10)\nHBox dummy\nHBox x\nVBox y\ny.pack(dummy)\ny.set_border(5)\ny.set_spacing(55)\ndummy.set_border(10)\ndummy.set_spacing(20)\nx.set_border(10)\nx.set_spacing(10)\nx.pack(pack)\nx.pack(dummy)\nx.pack(pack)\nx.set_border(0)"], "sample_outputs": ["brother 30 60\nfather 80 60\nfriend 20 60\ngrandpa 120 120\nme 50 40\nuncle 100 20", "dummy 0 0\npack 10 10\nx 40 10\ny 10 10"], "notes": "NoteIn the first sample the widgets are arranged as follows: "}, "positive_code": [{"source_code": "class Box\n\tattr_accessor :border, :spacing\n\t\n\tdef initialize(type, width, height)\n\t\t@type = type\n\t\t@border = 0\n\t\t@spacing = 0\n\t\t@repaint = false\n\t\t@width = width\n\t\t@height = height\n\t\t@element = []\n\tend\n\n\tdef repaint\n\t\t@repaint = true\n\t\treturn if @type == 'W'\n\t\t@height = @width = 0\n\t\tif @element.size > 0\n\t\t\tif @type == 'V'\n\t\t\t\t@element.each do |e|\n\t\t\t\t\t@height += e.height\n\t\t\t\t\t@width = [@width, e.width].max\n\t\t\t\tend\n\t\t\t\t@height += 2 * border + (@element.size - 1) * spacing\n\t\t\t\t@width += 2 * border\n\t\t\telsif @type == 'H'\n\t\t\t\t@element.each do |e|\n\t\t\t\t\t@height = [@height, e.height].max\n\t\t\t\t\t@width += e.width\n\t\t\t\tend\n\t\t\t\t@height += 2 * border\n\t\t\t\t@width += 2 * border + (@element.size - 1) * spacing\n\t\t\tend\n\t\tend\n\tend\n\n\tdef height\n\t\trepaint unless @repaint\n\t\t@height\n\tend\n\n\tdef width\n\t\trepaint unless @repaint\n\t\t@width\n\tend\n\n\tdef pack(w)\n\t\t@element << w\n\tend\nend\n\nw = {}\n\ngets.to_i.times do\n\t$_ = gets.chomp\n\tif /Widget (\\w+)\\((\\d+),(\\d+)\\)/\n\t\tw[$1] = Box.new('W', $2.to_i, $3.to_i)\n\telsif /(V|H)Box (\\w+)/\n\t\tw[$2] = Box.new($1, 0, 0)\n\telsif /(\\w+)\\.set_border\\((\\d+)\\)/\n\t\tw[$1].border = $2.to_i\n\telsif /(\\w+)\\.set_spacing\\((\\d+)\\)/\n\t\tw[$1].spacing = $2.to_i\n\telsif /(\\w+)\\.pack\\((\\w+)\\)/\n\t\tw[$1].pack(w[$2])\n\telse\n\t\tputs \"ERROR: $_\\n\"\n\tend\nend\n\nw.keys.sort.each do |k|\n\tputs \"#{k} #{w[k].width} #{w[k].height}\"\nend\n"}, {"source_code": "class Widget\n\tdef initialize(type, width = 0, height = 0)\n\t\t@type = type.to_sym\n\t\t@border = @spacing = 0\n\t\t@lazy = false\n\t\t@width = width.to_i\n\t\t@height = height.to_i\n\t\t@elements = []\n\tend\n\tdef process\n\t\t@lazy = true\n\t\treturn if @type == :W\n\t\t@height = @width = 0\n\t\treturn if @elements.size == 0\n\t\t@elements.each{|e|\n\t\t\tif @type == :V\n\t\t\t\t@height += e.height\n\t\t\t\t@width = [@width, e.width].max\n\t\t\telse\n\t\t\t\t@height = [@height, e.height].max\n\t\t\t\t@width += e.width\n\t\t\tend\n\t\t}\n\t\tif @type == :V\n\t\t\t@height += 2 * @border + (@elements.size - 1) * @spacing\n\t\t\t@width += 2 * @border\n\t\telse @type == :H\n\t\t\t@height += 2 * @border\n\t\t\t@width += 2 * @border + (@elements.size - 1) * @spacing\n\t\tend\n\tend\n\n\tdef height\n\t\tprocess unless @lazy\n\t\t@height\n\tend\n\n\tdef width\n\t\tprocess unless @lazy\n\t\t@width\n\tend\n\n\tdef pack(w)\n\t\t@elements.push $w[w.to_sym]\n\tend\n\n\tdef set_border(v)\n\t\t@border = v.to_i\n\tend\n\n\tdef set_spacing(v)\n\t\t@spacing = v.to_i\n\tend\nend\n$w = {}\ngets.to_i.times{|x|\n\tcmd = gets\n\tif (cmd[0]>='a' and cmd[0] <= 'z')\n\t\tname, fc = cmd.split '.'\n\t\t#puts name, fc\n\t\tfc,arg = fc.split '('\n\t\targ = arg.split(')')[0]\n\t\t#puts fc\n\t\t$w[name.to_sym].send fc.to_sym, *arg.split(',') \n\telse\n\t\tif (cmd[0]=='W')\n\t\t\tcmd = cmd.split(' ')[1]\n\t\t\tname,arg = cmd.split '('\n\t\t\targ = arg.split(')')[0]\n\t\t\t$w[name.to_sym] = Widget.new(:W, *(arg.split(',')))\n\t\telse\n\t\tif (cmd[0]=='V' || cmd[0] == 'H')\n\t\t\tname = cmd.split(' ')[1]\n\t\t\t$w[name.to_sym] = Widget.new(cmd[0].to_sym)\n\t\tend\n\tend\n\tend\n}\n\n$w.keys.sort.each{|k|\n\tputs \"#{k} #{$w[k].width} #{$w[k].height}\"\n}"}], "negative_code": [], "src_uid": "258f54f32c6df5390edd804294aad235"} {"nl": {"description": "PolandBall lives in a forest with his family. There are some trees in the forest. Trees are undirected acyclic graphs with k vertices and k - 1 edges, where k is some integer. Note that one vertex is a valid tree.There is exactly one relative living in each vertex of each tree, they have unique ids from 1 to n. For each Ball i we know the id of its most distant relative living on the same tree. If there are several such vertices, we only know the value of the one with smallest id among those.How many trees are there in the forest?", "input_spec": "The first line contains single integer n (1 ≤ n ≤ 104) — the number of Balls living in the forest. The second line contains a sequence p1, p2, ..., pn of length n, where (1 ≤ pi ≤ n) holds and pi denotes the most distant from Ball i relative living on the same tree. If there are several most distant relatives living on the same tree, pi is the id of one with the smallest id. It's guaranteed that the sequence p corresponds to some valid forest. Hacking: To hack someone, you should provide a correct forest as a test. The sequence p will be calculated according to the forest and given to the solution you try to hack as input. Use the following format: In the first line, output the integer n (1 ≤ n ≤ 104) — the number of Balls and the integer m (0 ≤ m < n) — the total number of edges in the forest. Then m lines should follow. The i-th of them should contain two integers ai and bi and represent an edge between vertices in which relatives ai and bi live. For example, the first sample is written as follows: 5 31 23 44 5", "output_spec": "You should output the number of trees in the forest where PolandBall lives.", "sample_inputs": ["5\n2 1 5 3 3", "1\n1"], "sample_outputs": ["2", "1"], "notes": "NoteIn the first sample testcase, possible forest is: 1-2 3-4-5. There are 2 trees overall.In the second sample testcase, the only possible graph is one vertex and no edges. Therefore, there is only one tree."}, "positive_code": [{"source_code": "require 'set'\nn = gets.to_i\nlist = [0] + gets.chomp.split(\" \").map(&:to_i)\nset = Set.new\nfor i in 1..n\n if(list[list[i]] == i)\n set.add([i,list[i]].min)\n end\nend\nprint set.length\n"}, {"source_code": "#!/usr/bin/env ruby\n\nlivings_count = gets.chop.to_i\n\nlivings_relation = {}\ntrees = []\n\nrelations = gets.chop.split\n\nrelations.each.with_index do |b , a|\n a += 1\n b = b.to_i\n\n if !livings_relation[b] && !livings_relation[a]\n trees.push(Array.new)\n trees_index = trees.size - 1\n livings_relation[a] = trees_index\n livings_relation[b] = trees_index\n trees[trees_index] << a\n trees[trees_index] << b\n elsif !livings_relation[b] && livings_relation[a]\n tree_index = livings_relation[a]\n livings_relation[b] = livings_relation[a]\n trees[tree_index] << b\n elsif livings_relation[b] && !livings_relation[a]\n tree_index = livings_relation[b]\n livings_relation[a] = livings_relation[b]\n trees[tree_index] << a\n elsif livings_relation[a] != livings_relation[b]\n # merge\n tree_index = livings_relation[a]\n tree_index_del = livings_relation[b]\n #livings_relation[b] = livings_relation[a]\n trees[tree_index_del].each do |living|\n livings_relation[living] = tree_index\n trees[tree_index] << living\n end\n trees[tree_index_del] = nil\n end\nend\n\nputs trees.compact.size\n"}], "negative_code": [], "src_uid": "6d940cb4b54f63a7aaa82f21e4c5b994"} {"nl": {"description": "It's time polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo of Kiev got down to business. In total, there are n tasks for the day and each animal should do each of these tasks. For each task, they have evaluated its difficulty. Also animals decided to do the tasks in order of their difficulty. Unfortunately, some tasks can have the same difficulty, so the order in which one can perform the tasks may vary.Menshykov, Uslada and Horace ask you to deal with this nuisance and come up with individual plans for each of them. The plan is a sequence describing the order in which an animal should do all the n tasks. Besides, each of them wants to have its own unique plan. Therefore three plans must form three different sequences. You are to find the required plans, or otherwise deliver the sad news to them by stating that it is impossible to come up with three distinct plans for the given tasks.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 2000) — the number of tasks. The second line contains n integers h1, h2, ..., hn (1 ≤ hi ≤ 2000), where hi is the difficulty of the i-th task. The larger number hi is, the more difficult the i-th task is.", "output_spec": "In the first line print \"YES\" (without the quotes), if it is possible to come up with three distinct plans of doing the tasks. Otherwise print in the first line \"NO\" (without the quotes). If three desired plans do exist, print in the second line n distinct integers that represent the numbers of the tasks in the order they are done according to the first plan. In the third and fourth line print two remaining plans in the same form. If there are multiple possible answers, you can print any of them.", "sample_inputs": ["4\n1 3 3 1", "5\n2 4 1 4 8"], "sample_outputs": ["YES\n1 4 2 3 \n4 1 2 3 \n4 1 3 2", "NO"], "notes": "NoteIn the first sample the difficulty of the tasks sets one limit: tasks 1 and 4 must be done before tasks 2 and 3. That gives the total of four possible sequences of doing tasks : [1, 4, 2, 3], [4, 1, 2, 3], [1, 4, 3, 2], [4, 1, 3, 2]. You can print any three of them in the answer.In the second sample there are only two sequences of tasks that meet the conditions — [3, 1, 2, 4, 5] and [3, 1, 4, 2, 5]. Consequently, it is impossible to make three distinct sequences of tasks."}, "positive_code": [{"source_code": "#!/usr/bin/env ruby\n\nn = gets.to_i\nh = gets.split.map(&:to_i)\nsol = []\n100.times do\n x = h.map{|x| x * 10 + rand(10)}.map.with_index(1).sort.map(&:last)\n sol << x if !sol.include?(x)\n break if sol.size == 3\nend\n\nif sol.size == 3\n puts 'YES'\n puts sol.map{|x| x.map(&:to_s).join(' ')}\nelse\n puts 'NO'\nend\n\n"}, {"source_code": "#!/usr/bin/env ruby\n\nn = gets.to_i\nh = gets.split.map(&:to_i)\nsol = []\n100.times do\n x = h.map{|x| x * 10 + rand(10)}.map.with_index(1).sort.map(&:last)\n sol << x if !sol.include?(x)\n break if sol.size == 3\nend\n\nif sol.size == 3\n puts 'YES'\n puts sol.map{|x| x.map(&:to_s).join(' ')}\nelse\n puts 'NO'\nend\n\n"}, {"source_code": "n = STDIN.readline.to_i\nvalues = STDIN.readline.split.map {|s| s.to_i }\nvalue_indices = {}\nvalues.each_with_index do |value, index|\n\tvalue_indices[value] ||= []\n\tvalue_indices[value].push(index+1)\nend\nperturb_two, perturb_three = [], nil\nvalue_indices.each do |value, indices|\n\tif indices.length >= 3\n\t\tperturb_three = value\n\t\tbreak\n\telsif indices.length == 2\n\t\tperturb_two.push(value)\n\t\tbreak if perturb_two.length == 2\n\tend\nend\norders = 3.times.map { [] }\nif perturb_three != nil\n\tvalue_indices.keys.sort.each do |value|\n\t\tindices = value_indices[value]\n\t\tif value == perturb_three\n\t\t\torders[0].push(indices[0])\n\t\t\torders[0].push(indices[1])\n\t\t\torders[0].push(indices[2])\n\t\t\torders[1].push(indices[1])\n\t\t\torders[1].push(indices[2])\n\t\t\torders[1].push(indices[0])\n\t\t\torders[2].push(indices[2])\n\t\t\torders[2].push(indices[0])\n\t\t\torders[2].push(indices[1])\n\t\t\tindices[3..-1].each {|index| 3.times {|p| orders[p].push(index) } }\n\t\telse\n\t\t\tindices.each {|index| 3.times {|p| orders[p].push(index) } }\n\t\tend\n\tend\nelsif perturb_two.length == 2\n\tvalue_indices.keys.sort.each do |value|\n\t\tindices = value_indices[value]\n\t\tif perturb_two.index(value) == 0\n\t\t\torders[0].push(indices[0])\n\t\t\torders[0].push(indices[1])\n\t\t\torders[1].push(indices[0])\n\t\t\torders[1].push(indices[1])\n\t\t\torders[2].push(indices[1])\n\t\t\torders[2].push(indices[0])\n\t\telsif perturb_two.index(value) == 1\n\t\t\torders[0].push(indices[0])\n\t\t\torders[0].push(indices[1])\n\t\t\torders[1].push(indices[1])\n\t\t\torders[1].push(indices[0])\n\t\t\torders[2].push(indices[1])\n\t\t\torders[2].push(indices[0])\n\t\telse\n\t\t\tindices.each {|index| 3.times {|p| orders[p].push(index) } }\n\t\tend\n\tend\nelse\n\tputs 'NO'\n\texit\nend\nputs 'YES'\n3.times {|i| puts orders[i].join(' ') }\n"}, {"source_code": "n = gets.to_i\nh = gets.split.map(&:to_i)\nif h.uniq.size > n - 2\n puts \"NO\"\nelse\n puts \"YES\"\n h.map!.with_index { |d, i| [d, i + 1] }.sort_by! { |d, i| d }\n\n puts h.map { |d, i| i }.join(\" \")\n\n 1.upto(n - 1).each do |i|\n if h[i - 1][0] == h[i][0]\n h[i - 1], h[i] = h[i], h[i - 1]\n break\n end\n end\n\n puts h.map { |d, i| i }.join(\" \")\n\n (n - 1).downto(1).each do |i|\n if h[i - 1][0] == h[i][0]\n h[i - 1], h[i] = h[i], h[i - 1]\n break\n end\n end\n\n puts h.map { |d, i| i }.join(\" \")\nend\n"}, {"source_code": "#!/usr/bin/env ruby\n\nn = gets.to_i\nh = gets.split.map(&:to_i)\nsol = []\n100.times do\n x = h.map{|x| x * 10 + rand(10)}.map.with_index(1).sort.map(&:last)\n sol << x if !sol.include?(x)\n break if sol.size == 3\nend\n\nif sol.size == 3\n puts 'YES'\n puts sol.map{|x| x.map(&:to_s).join(' ')}\nelse\n puts 'NO'\nend\n"}, {"source_code": "require 'pp'\n\nn = STDIN.gets.chomp.to_i\nos = STDIN.gets.chomp.split(/ /).map(&:to_i)\n\nss = [(1..n).to_a, os].transpose\nss.sort!{|a, b| a.last <=> b.last }\n\ns = os.sort\ncount=1\nleg = -1\nsame3 = nil\nsame2 = []\n(n-1).times do |i|\n if s[i] == s[i+1]\n count+=1\n if count >=3\n same3 = s[i]\n break\n elsif count >=2\n same2.push s[i]\n end\n else\n count = 1\n end\nend\n\nif same3\n ans = []\n ans.push ss.map{|a,b| a}\n\n pos = []\n ss.each_with_index do |sse,i|\n pos.push i if sse.last == same3\n end\n ss[pos[0]], ss[pos[1]], ss[pos[2]] = ss[pos[1]], ss[pos[2]], ss[pos[0]]\n ans.push ss.map{|a,b| a}\n ss[pos[0]], ss[pos[1]], ss[pos[2]] = ss[pos[1]], ss[pos[2]], ss[pos[0]]\n ans.push ss.map{|a,b| a}\n\n puts \"YES\"\n ans.each{|a| puts a.join(\" \")}\nelsif same2.length >= 2\n ans = []\n ans.push ss.map{|a,b| a}\n\n pos0 = []\n pos1 = []\n ss.each_with_index do |sse,i|\n pos0.push i if sse.last == same2[0]\n pos1.push i if sse.last == same2[1]\n end\n ss[pos0.first], ss[pos0.last] = ss[pos0.last], ss[pos0.first]\n ans.push ss.map{|a,b| a}\n ss[pos1.first], ss[pos1.last] = ss[pos1.last], ss[pos1.first]\n ans.push ss.map{|a,b| a}\n\n puts \"YES\"\n ans.each{|a| puts a.join(\" \")}\nelse\n puts \"NO\"\nend\n\n"}, {"source_code": "#!/usr/bin/env ruby\n\nn = gets.to_i\nh = gets.split.map(&:to_i)\nsol = []\n100.times do\n x = h.map{|x| x * 10 + rand(10)}.map.with_index(1).sort.map(&:last)\n sol << x if !sol.include?(x)\n break if sol.size == 3\nend\n\nif sol.size == 3\n puts 'YES'\n puts sol.map{|x| x.map(&:to_s).join(' ')}\nelse\n puts 'NO'\nend\n\n"}, {"source_code": "a=gets.chomp.to_i\nb=gets.chomp.split(\" \").map {|i| i.to_i }\ninde=Hash.new {|hash,key| hash[key]=[] }\ncount=Array.new(2001,0)\n0.upto(a-1) do |i|\ninde[b[i]]<2\nputs \"YES\"\np=count.index(cp)\n3.times do \nkeys.each do |i|\nprint \"#{inde[i].join(\" \")} \"\nend\nputs\ntemp=inde[p][0]\ntemp2=inde[p][1]\ninde[p][1]=inde[p][2]\ninde[p][2]=temp\ninde[p][0]=temp2\nend\n\nelsif cp==2\n\nif count.count(cp)<2\nputs \"NO\"\n\nelse\nindex1=count.index(cp)\ncount.delete_at(index1)\nindex2=count.index(cp)+1\n\nit=0\nputs \"YES\"\n3.times do \n\nkeys.each do |i|\nprint \"#{inde[i].join(\" \")} \"\nend\nputs\nif it==0\ntemp=inde[index1][0]\ninde[index1][0]=inde[index1][1]\ninde[index1][1]=temp\nit+=1\nelse\ntemp=inde[index2][0]\ninde[index2][0]=inde[index2][1]\ninde[index2][1]=temp\nend\n\nend\n\nend \n\nend\n\n\n\n\n"}, {"source_code": "#!/usr/bin/env ruby\n\nn = gets.to_i\nh = gets.split.map(&:to_i)\nsol = []\n100.times do\n x = h.map{|x| x * 10 + rand(10)}.map.with_index(1).sort.map(&:last)\n sol << x if !sol.include?(x)\n break if sol.size == 3\nend\n\nif sol.size == 3\n puts 'YES'\n puts sol.map{|x| x.map(&:to_s).join(' ')}\nelse\n puts 'NO'\nend\n\n"}, {"source_code": "a=gets.chomp.to_i\nb=gets.chomp.split(\" \").map {|i| i.to_i }\ninde=Hash.new {|hash,key| hash[key]=[] }\ncount=Array.new(2001,0)\n0.upto(a-1) do |i|\ninde[b[i]]<2\nputs \"YES\"\np=count.index(cp)\n3.times do \nkeys.each do |i|\nprint \"#{inde[i].join(\" \")} \"\nend\nputs\ntemp=inde[p][0]\ntemp2=inde[p][1]\ninde[p][1]=inde[p][2]\ninde[p][2]=temp\ninde[p][0]=temp2\nend\n\nelsif cp==2\n\nif count.count(cp)<2\nputs \"NO\"\n\nelse\nindex1=count.index(cp)\ncount.delete_at(index1)\nindex2=count.index(cp)+1\n\nit=0\nputs \"YES\"\n3.times do \n\nkeys.each do |i|\nprint \"#{inde[i].join(\" \")} \"\nend\nputs\nif it==0\ntemp=inde[index1][0]\ninde[index1][0]=inde[index1][1]\ninde[index1][1]=temp\nit+=1\nelse\ntemp=inde[index2][0]\ninde[index2][0]=inde[index2][1]\ninde[index2][1]=temp\nend\n\nend\n\nend \n\nend\n\n\n\n\n"}, {"source_code": "#!/usr/bin/env ruby\n\nn = gets.to_i\nh = gets.split.map(&:to_i)\nsol = []\n100.times do\n x = h.map{|x| x * 10 + rand(10)}.map.with_index(1).sort.map(&:last)\n sol << x if !sol.include?(x)\n break if sol.size == 3\nend\n\nif sol.size == 3\n puts 'YES'\n puts sol.map{|x| x.map(&:to_s).join(' ')}\nelse\n puts 'NO'\nend\n\n"}, {"source_code": "#!/usr/bin/env ruby\n\nn = gets.to_i\nh = gets.split.map(&:to_i)\nsol = []\n100.times do\n x = h.map{|x| x * 10 + rand(10)}.map.with_index(1).sort.map(&:last)\n sol << x if !sol.include?(x)\n break if sol.size == 3\nend\n\nif sol.size == 3\n puts 'YES'\n puts sol.map{|x| x.map(&:to_s).join(' ')}\nelse\n puts 'NO'\nend\n"}, {"source_code": "#!/usr/bin/env ruby\n\nn = gets.to_i\nh = gets.split.map(&:to_i)\nsol = []\n100.times do\n x = h.map{|x| x * 10 + rand(10)}.map.with_index(1).sort.map(&:last)\n sol << x if !sol.include?(x)\n break if sol.size == 3\nend\n\nif sol.size == 3\n puts 'YES'\n puts sol.map{|x| x.map(&:to_s).join(' ')}\nelse\n puts 'NO'\nend\n\n"}, {"source_code": "#!/usr/bin/env ruby\n\nn = gets.to_i\nh = gets.split.map(&:to_i)\nsol = []\n100.times do\n x = h.map{|x| x * 10 + rand(10)}.map.with_index(1).sort.map(&:last)\n sol << x if !sol.include?(x)\n break if sol.size == 3\nend\n\nif sol.size == 3\n puts 'YES'\n puts sol.map{|x| x.map(&:to_s).join(' ')}\nelse\n puts 'NO'\nend\n\n"}, {"source_code": "#!/usr/bin/env ruby\n\nn = gets.to_i\nh = gets.split.map(&:to_i)\nsol = []\n100.times do\n x = h.map{|x| x * 10 + rand(10)}.map.with_index(1).sort.map(&:last)\n sol << x if !sol.include?(x)\n break if sol.size == 3\nend\n\nif sol.size == 3\n puts 'YES'\n puts sol.map{|x| x.map(&:to_s).join(' ')}\nelse\n puts 'NO'\nend\n\n"}], "negative_code": [], "src_uid": "fdfc1e690eeee6f50e2a024bf3f841e8"} {"nl": {"description": "I wonder, does the falling rain Forever yearn for it's disdain?Effluvium of the MindYou are given a positive integer $$$n$$$.Find any permutation $$$p$$$ of length $$$n$$$ such that the sum $$$\\operatorname{lcm}(1,p_1) + \\operatorname{lcm}(2, p_2) + \\ldots + \\operatorname{lcm}(n, p_n)$$$ is as large as possible. Here $$$\\operatorname{lcm}(x, y)$$$ denotes the least common multiple (LCM) of integers $$$x$$$ and $$$y$$$.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 1\\,000$$$). Description of the test cases follows. The only line for each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.", "output_spec": "For each test case print $$$n$$$ integers $$$p_1$$$, $$$p_2$$$, $$$\\ldots$$$, $$$p_n$$$ — the permutation with the maximum possible value of $$$\\operatorname{lcm}(1,p_1) + \\operatorname{lcm}(2, p_2) + \\ldots + \\operatorname{lcm}(n, p_n)$$$. If there are multiple answers, print any of them.", "sample_inputs": ["2\n\n1\n\n2"], "sample_outputs": ["1 \n2 1"], "notes": "NoteFor $$$n = 1$$$, there is only one permutation, so the answer is $$$[1]$$$.For $$$n = 2$$$, there are two permutations: $$$[1, 2]$$$ — the sum is $$$\\operatorname{lcm}(1,1) + \\operatorname{lcm}(2, 2) = 1 + 2 = 3$$$. $$$[2, 1]$$$ — the sum is $$$\\operatorname{lcm}(1,2) + \\operatorname{lcm}(2, 1) = 2 + 2 = 4$$$. "}, "positive_code": [{"source_code": "gets.to_i.times { \n\tn = gets.to_i\n\ta = n.odd? ? 1 : 0\n\tif n.odd? ; print '1 '; end\n\t(n/2).times{|i| print i*2+2+a, ' ', i*2+1+a, ' '} ; puts ''\n}"}, {"source_code": "gets.to_i.times do\n n=gets.to_i\n puts ((1..n).to_a.map.with_index {|i,j| (n%2==1 && i==1) ? 1 : ((n-j)%2==0 ? i+1 : i-1)})*' '\nend\n"}, {"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n puts n.downto(1).each_slice(2).to_a.reverse.join(' ')\r\nend"}, {"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n if n % 2 == 0\r\n puts 1.upto(n).map { |i| i + (i % 2 == 0 ? -1 : 1) }.join(\" \")\r\n else\r\n puts ([1] + (2.upto(n).map { |i| i + (i % 2 == 0 ? 1 : -1) })).join(\" \")\r\n end\r\nend\r\n"}], "negative_code": [{"source_code": "gets.to_i.times { \n\tn = gets.to_i\n\tif n.even? \n\t\tn.times{|i| print n-i, ' '}; puts ''\n\telse\n\t\t(n/2).times{|i| print n-i*2-1 , ' ', n-i*2, ' '}; puts 1\n\tend\n}"}, {"source_code": "gets.to_i.times { gets.to_i.times{|i| print i+1, ' '}; puts ''}"}, {"source_code": "gets.to_i.times do\n n=gets.to_i\n puts ((2..n).to_a+[1])*' '\nend\n"}, {"source_code": "gets.to_i.times do\n n=gets.to_i\n puts ([n]+(1...n).to_a)*' '\nend\n"}, {"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n n.downto(1).each_slice(2).to_a.reverse.join(' ')\r\nend"}], "src_uid": "be3295b4d43a11ee94022b103f4221a5"} {"nl": {"description": "Arkady coordinates rounds on some not really famous competitive programming platform. Each round features $$$n$$$ problems of distinct difficulty, the difficulties are numbered from $$$1$$$ to $$$n$$$.To hold a round Arkady needs $$$n$$$ new (not used previously) problems, one for each difficulty. As for now, Arkady creates all the problems himself, but unfortunately, he can't just create a problem of a desired difficulty. Instead, when he creates a problem, he evaluates its difficulty from $$$1$$$ to $$$n$$$ and puts it into the problems pool.At each moment when Arkady can choose a set of $$$n$$$ new problems of distinct difficulties from the pool, he holds a round with these problems and removes them from the pool. Arkady always creates one problem at a time, so if he can hold a round after creating a problem, he immediately does it.You are given a sequence of problems' difficulties in the order Arkady created them. For each problem, determine whether Arkady held the round right after creating this problem, or not. Initially the problems pool is empty.", "input_spec": "The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n, m \\le 10^5$$$) — the number of difficulty levels and the number of problems Arkady created. The second line contains $$$m$$$ integers $$$a_1, a_2, \\ldots, a_m$$$ ($$$1 \\le a_i \\le n$$$) — the problems' difficulties in the order Arkady created them.", "output_spec": "Print a line containing $$$m$$$ digits. The $$$i$$$-th digit should be $$$1$$$ if Arkady held the round after creation of the $$$i$$$-th problem, and $$$0$$$ otherwise.", "sample_inputs": ["3 11\n2 3 1 2 2 2 3 2 2 3 1", "4 8\n4 1 3 3 2 3 3 3"], "sample_outputs": ["00100000001", "00001000"], "notes": "NoteIn the first example Arkady held the round after the first three problems, because they are of distinct difficulties, and then only after the last problem."}, "positive_code": [{"source_code": "n,m = gets.split.map(&:to_i)\ncnt = Array.new(n,0)\nd = 0\ne = 0\na = gets.split.map{|i|i.to_i-1}\na.each do |num|\n cnt[num] += 1\n d += 1 if cnt[num]-1 == e\n if d == n\n print \"1\"\n e += 1\n for i in 0..n-1\n d -= 1 if cnt[i] == e\n end\n else\n print \"0\"\n end\nend"}], "negative_code": [{"source_code": "n,m = gets.split.map(&:to_i)\ncnt = Array.new(n,0)\nd = 0\ne = 0\na = gets.split.map{|i|i.to_i-1}\na.each do |num|\n cnt[num] += 1\n d += 1 if cnt[num]-1 == e\n if d == n\n print \"1\"\n e += 1\n d = 0\n else\n print \"0\"\n end\nend"}], "src_uid": "2070955288b2e2cdbae728d8e7ce78ab"} {"nl": {"description": "Today, hedgehog Filya went to school for the very first time! Teacher gave him a homework which Filya was unable to complete without your help.Filya is given an array of non-negative integers a1, a2, ..., an. First, he pick an integer x and then he adds x to some elements of the array (no more than once), subtract x from some other elements (also, no more than once) and do no change other elements. He wants all elements of the array to be equal.Now he wonders if it's possible to pick such integer x and change some elements of the array using this x in order to make all elements equal.", "input_spec": "The first line of the input contains an integer n (1 ≤ n ≤ 100 000) — the number of integers in the Filya's array. The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109) — elements of the array.", "output_spec": "If it's impossible to make all elements of the array equal using the process given in the problem statement, then print \"NO\" (without quotes) in the only line of the output. Otherwise print \"YES\" (without quotes).", "sample_inputs": ["5\n1 3 3 2 1", "5\n1 2 3 4 5"], "sample_outputs": ["YES", "NO"], "notes": "NoteIn the first sample Filya should select x = 1, then add it to the first and the last elements of the array and subtract from the second and the third elements."}, "positive_code": [{"source_code": "gets\na = gets.split(\" \").map(&:to_i)\n\nmax = a.max\nmin = a.min\nmid = (max+min)/2.0\n\nputs a.all?{|x| x==max || x==min || x==mid} ? \"YES\" : \"NO\"\n"}, {"source_code": "gets\na=gets.split.map(&:to_i).group_by{|e|e}.map(&:first).sort\nputs a.size<=2 || (a.size==3&&a[2]-a[1]==a[1]-a[0]) ? :YES : :NO"}, {"source_code": "gets\na = gets.split.map(&:to_i).uniq.sort\n\nputs (a.size <= 2 || (a.size == 3 && a[1]-a[0] == a[2]-a[1])) ? 'YES' : 'NO'"}, {"source_code": "n =gets.to_i ; list = gets.chomp.split(\" \").map(&:to_i).uniq.sort\nputs (list.length <= 2 || (list.length <=3 && (2*list[1] == list[0] + list[2])))? \"YES\" : \"NO\"\n"}, {"source_code": "n = gets.strip.to_i\narr = gets.strip.split(\" \").map(&:to_i)\n\na = arr[0]\narr.delete(a)\nif arr.length > 0\n b = arr[0]\n arr.delete(b)\n if arr.length > 0\n c = arr[0]\n arr.delete(c)\n if arr.length > 0\n puts \"NO\"\n else\n lol = [a,b,c]\n x = lol.min\n z = lol.max\n lol.delete(x)\n lol.delete(z)\n if lol[0] * 2 == (x + z)\n puts \"YES\"\n else\n puts \"NO\"\n end\n end\n else\n puts \"YES\"\n end\nelse\n puts \"YES\"\nend"}, {"source_code": "n,*a=$<.read.split.map &:to_i\na.sort!.uniq!\nn=a.size\nx=a[-1]-a[0]\nif n>3 or (n==3 and (x%2==1 or a[1]!=a[0]+x/2))\n puts :NO\nelse\n puts :YES\nend\n"}, {"source_code": "n = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\nk = (a.max-a.min)/2\nif a.uniq.count<3 || (a.uniq.count==3&&a.include?(k+a.min)&&a.include?(a.max-k)) then s=\"YES\" else s=\"NO\" end\nputs s\n"}, {"source_code": "n = gets\nx = gets.split.map(&:to_i).sort.uniq\nif (x.size <= 2) or (x.size == 3 and x[0]+x[2]==x[1]*2)\n puts \"YES\"\nelse\n puts\"NO\"\nend\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nau = a.uniq\ngoal = au.size == 1 ? au[0] : au.sort[1]\na1 = a.find{|e| e != goal}\nx = a1 ? (goal - a1).abs : 0\nputs a.all?{|e| (goal - e).abs == 0 || (goal - e).abs == x} ? 'YES' : 'NO'\n"}, {"source_code": "n=readline.to_i\na=readline.split.map(&:to_i).uniq.sort\n\nif a.size <= 2 || (a.size == 3 && a[1]-a[0] == a[2]-a[1])\n puts 'YES'\nelse\n puts 'NO'\nend\n"}, {"source_code": "n = gets.chomp.to_i\narr = gets.chomp.split(' ').map(&:to_i)\n\nmin = arr.min\nmax = arr.max\n\nalpha = ((max - min) / 2.0)\n\nans = 'YES'\n\ncorrectNum = (min + alpha)\n#puts correctNum\narr.each do |i|\n\tif i != correctNum && i + alpha != correctNum && i - alpha != correctNum\n\t\tans = 'NO'\n\t\tbreak\n\tend\nend\n\nputs ans"}], "negative_code": [{"source_code": "gets\na=gets.split.map(&:to_i).group_by{|e|e}.map(&:first).sort\nputs (a.size==3&&a[2]-a[1]==a[1]-a[0]) || a.size==2 ? :YES : :NO"}, {"source_code": "gets\na=gets.split.map(&:to_i).group_by{|e|e}.map(&:first).sort\nputs (a.size==3&&a[2]-a[1]==a[1]-a[0]) || (a.size==2&&(a[1]-a[0])%2==0) ? :YES : :NO"}, {"source_code": "n = gets.strip.to_i\narr = gets.strip.split(\" \").map(&:to_i)\n\na = arr[0]\narr.delete(a)\nif arr.length > 0\n b = arr[0]\n arr.delete(b)\n if arr.length > 0\n c = arr[0]\n arr.delete(c)\n if arr.length > 0\n puts \"NO\"\n else\n lol = [a,b,c]\n x = lol.min\n z = lol.max\n lol.delete(x)\n lol.delete(z)\n if lol[0] * 2 == (x + z)\n puts \"YES\"\n else\n puts \"NO\"\n end\n end\n else\n puts \"NO\"\n end\nelse\n puts \"NO\"\nend"}, {"source_code": "n = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\n\nif a.uniq.count==3&&a.include?(((a.max-a.min)/2).to_i+a.min) then s=\"YES\" else s=\"NO\" end\nputs s\n"}, {"source_code": "n = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\n\nif a.uniq.count<3 || (a.uniq.count==3&&a.include?(((a.max-a.min)/2)+a.min)) then s=\"YES\" else s=\"NO\" end\nputs s\n"}, {"source_code": "n = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\n\nif a.uniq.count==3&&a.include?(((a.max-a.min)/2)+a.min) then s=\"YES\" else s=\"NO\" end\nputs s\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nsum = a.inject(&:+)\nif sum % n == 0\n goal = sum / n\n a1 = a.find{|e| e != goal}\n if a1\n x = (goal - a1).abs\n puts a.all?{|e| (goal - e).abs == 0 || (goal - e).abs == x} ? 'YES' : 'NO'\n else\n puts 'YES'\n end\nelse\n puts 'NO'\nend\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nsum = a.inject(&:+)\nif sum % n == 0\n goal = sum / n\n a1 = a.find{|e| e != goal}\n if a1\n x = (goal - a1).abs\n pos = a.count{|e| e - x == goal}\n neg = a.count{|e| e + x == goal}\n eq = a.count{|e| e == goal}\n puts pos == neg && pos + neg + eq == n ? 'YES' : 'NO'\n else\n puts 'YES'\n end\nelse\n puts 'NO'\nend\n"}, {"source_code": "n=readline.to_i\na=readline.split.map(&:to_i).uniq.sort\n\nif a.size == 1 || (a.size == 3 && a[1]-a[0] == a[2]-a[1])\n puts 'YES'\nelse\n puts 'NO'\nend\n"}, {"source_code": "n = gets.chomp.to_i\narr = gets.chomp.split(' ').map(&:to_i)\n\nmin = arr.min\nmax = arr.max\n\nalpha = (max - min) / 2\n\nans = 'YES'\n\ncorrectNum = min + alpha\n#puts correctNum\narr.each do |i|\n\tif i != correctNum && i + alpha != correctNum && i - alpha != correctNum\n\t\tans = 'NO'\n\t\tbreak\n\tend\nend\n\nputs ans"}], "src_uid": "27f837609b2777cefccb13aa4596d91c"} {"nl": {"description": "As the guys fried the radio station facilities, the school principal gave them tasks as a punishment. Dustin's task was to add comments to nginx configuration for school's website. The school has n servers. Each server has a name and an ip (names aren't necessarily unique, but ips are). Dustin knows the ip and name of each server. For simplicity, we'll assume that an nginx command is of form \"command ip;\" where command is a string consisting of English lowercase letter only, and ip is the ip of one of school servers. Each ip is of form \"a.b.c.d\" where a, b, c and d are non-negative integers less than or equal to 255 (with no leading zeros). The nginx configuration file Dustin has to add comments to has m commands. Nobody ever memorizes the ips of servers, so to understand the configuration better, Dustin has to comment the name of server that the ip belongs to at the end of each line (after each command). More formally, if a line is \"command ip;\" Dustin has to replace it with \"command ip; #name\" where name is the name of the server with ip equal to ip.Dustin doesn't know anything about nginx, so he panicked again and his friends asked you to do his task for him.", "input_spec": "The first line of input contains two integers n and m (1 ≤ n, m ≤ 1000). The next n lines contain the names and ips of the servers. Each line contains a string name, name of the server and a string ip, ip of the server, separated by space (1 ≤ |name| ≤ 10, name only consists of English lowercase letters). It is guaranteed that all ip are distinct. The next m lines contain the commands in the configuration file. Each line is of form \"command ip;\" (1 ≤ |command| ≤ 10, command only consists of English lowercase letters). It is guaranteed that ip belongs to one of the n school servers.", "output_spec": "Print m lines, the commands in the configuration file after Dustin did his task.", "sample_inputs": ["2 2\nmain 192.168.0.2\nreplica 192.168.0.1\nblock 192.168.0.1;\nproxy 192.168.0.2;", "3 5\ngoogle 8.8.8.8\ncodeforces 212.193.33.27\nserver 138.197.64.57\nredirect 138.197.64.57;\nblock 8.8.8.8;\ncf 212.193.33.27;\nunblock 8.8.8.8;\ncheck 138.197.64.57;"], "sample_outputs": ["block 192.168.0.1; #replica\nproxy 192.168.0.2; #main", "redirect 138.197.64.57; #server\nblock 8.8.8.8; #google\ncf 212.193.33.27; #codeforces\nunblock 8.8.8.8; #google\ncheck 138.197.64.57; #server"], "notes": null}, "positive_code": [{"source_code": "# cook your code here\n\nn,m = gets.chomp.split \n\nn = n.to_i \nm = m.to_i \n\ngood = Array.new(n) \nbad = Array.new(m) \n\nfor i in 0...n \n good[i] = gets.chomp.split \nend \n\nfor i in 0...m \n bad[i] = gets.chomp.split \n # bad[i][1] = bad[i][1].chop \n for j in 0...n \n if(bad[i][1].chop == good[j][1]) \n puts bad[i][0] + \" \" + bad[i][1] + \" #\" + good[j][0]\n end \n end \nend \n"}], "negative_code": [], "src_uid": "94501cd676a9214a59943b8ddd1dd31b"} {"nl": {"description": "The $$$\\text{$$$gcdSum$$$}$$$ of a positive integer is the $$$gcd$$$ of that integer with its sum of digits. Formally, $$$\\text{$$$gcdSum$$$}(x) = gcd(x, \\text{ sum of digits of } x)$$$ for a positive integer $$$x$$$. $$$gcd(a, b)$$$ denotes the greatest common divisor of $$$a$$$ and $$$b$$$ — the largest integer $$$d$$$ such that both integers $$$a$$$ and $$$b$$$ are divisible by $$$d$$$.For example: $$$\\text{$$$gcdSum$$$}(762) = gcd(762, 7 + 6 + 2)=gcd(762,15) = 3$$$.Given an integer $$$n$$$, find the smallest integer $$$x \\ge n$$$ such that $$$\\text{$$$gcdSum$$$}(x) > 1$$$.", "input_spec": "The first line of input contains one integer $$$t$$$ $$$(1 \\le t \\le 10^4)$$$ — the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(1 \\le n \\le 10^{18})$$$. All test cases in one test are different.", "output_spec": "Output $$$t$$$ lines, where the $$$i$$$-th line is a single integer containing the answer to the $$$i$$$-th test case.", "sample_inputs": ["3\n11\n31\n75"], "sample_outputs": ["12\n33\n75"], "notes": "NoteLet us explain the three test cases in the sample.Test case 1: $$$n = 11$$$: $$$\\text{$$$gcdSum$$$}(11) = gcd(11, 1 + 1) = gcd(11,\\ 2) = 1$$$.$$$\\text{$$$gcdSum$$$}(12) = gcd(12, 1 + 2) = gcd(12,\\ 3) = 3$$$.So the smallest number $$$\\ge 11$$$ whose $$$gcdSum$$$ $$$> 1$$$ is $$$12$$$.Test case 2: $$$n = 31$$$: $$$\\text{$$$gcdSum$$$}(31) = gcd(31, 3 + 1) = gcd(31,\\ 4) = 1$$$.$$$\\text{$$$gcdSum$$$}(32) = gcd(32, 3 + 2) = gcd(32,\\ 5) = 1$$$.$$$\\text{$$$gcdSum$$$}(33) = gcd(33, 3 + 3) = gcd(33,\\ 6) = 3$$$.So the smallest number $$$\\ge 31$$$ whose $$$gcdSum$$$ $$$> 1$$$ is $$$33$$$.Test case 3: $$$\\ n = 75$$$: $$$\\text{$$$gcdSum$$$}(75) = gcd(75, 7 + 5) = gcd(75,\\ 12) = 3$$$.The $$$\\text{$$$gcdSum$$$}$$$ of $$$75$$$ is already $$$> 1$$$. Hence, it is the answer."}, "positive_code": [{"source_code": "def digitSum(num)\n s = 0\n while num > 0\n s += num % 10\n num = num / 10\n end\n s\nend\n\ngets.to_i.times {\n n = gets.to_i\n\n while true\n s = digitSum n\n o = n.gcd s\n if o > 1\n puts n\n break\n end\n n += 1\n end\n}\n"}, {"source_code": "def digitSum(num)\n s = 0\n while num > 0\n s += num % 10\n num = num / 10\n end\n s\nend\n\ndef gcd1 (a, b)\n if b == 0\n return a\n end\n gcd1(b, a % b)\nend\n\nt = gets.to_i\n(1..t).each { |i|\n n = gets.to_i\n\n while true\n s = digitSum n\n o = gcd1(n, s)\n if o > 1\n puts n\n break\n end\n n += 1\n end\n}\n"}, {"source_code": "def gs(n)\r\n s=n.to_s.chars.map(&:to_i).sum\r\n return n.gcd(s)\r\nend\r\ngets.to_i.times do\r\n n=gets.to_i\r\n i=n\r\n while\r\n g=gs(i)\r\n if g>1\r\n puts i\r\n break\r\n end\r\n i+=1\r\n end\r\nend\r\n"}, {"source_code": "t = gets.to_i\r\nt.times{\r\n x = gets.to_i\r\n (x..x+100).each{|x|if x.gcd(x.digits.sum)>1 then p x;break end}\r\n}"}], "negative_code": [], "src_uid": "204e75827b7016eb1f1fbe1d6b60b03d"} {"nl": {"description": "Let $$$a$$$ be a matrix of size $$$r \\times c$$$ containing positive integers, not necessarily distinct. Rows of the matrix are numbered from $$$1$$$ to $$$r$$$, columns are numbered from $$$1$$$ to $$$c$$$. We can construct an array $$$b$$$ consisting of $$$r + c$$$ integers as follows: for each $$$i \\in [1, r]$$$, let $$$b_i$$$ be the greatest common divisor of integers in the $$$i$$$-th row, and for each $$$j \\in [1, c]$$$ let $$$b_{r+j}$$$ be the greatest common divisor of integers in the $$$j$$$-th column. We call the matrix diverse if all $$$r + c$$$ numbers $$$b_k$$$ ($$$k \\in [1, r + c]$$$) are pairwise distinct. The magnitude of a matrix equals to the maximum of $$$b_k$$$.For example, suppose we have the following matrix: $$$\\begin{pmatrix} 2 & 9 & 7\\\\ 4 & 144 & 84 \\end{pmatrix}$$$ We construct the array $$$b$$$: $$$b_1$$$ is the greatest common divisor of $$$2$$$, $$$9$$$, and $$$7$$$, that is $$$1$$$; $$$b_2$$$ is the greatest common divisor of $$$4$$$, $$$144$$$, and $$$84$$$, that is $$$4$$$; $$$b_3$$$ is the greatest common divisor of $$$2$$$ and $$$4$$$, that is $$$2$$$; $$$b_4$$$ is the greatest common divisor of $$$9$$$ and $$$144$$$, that is $$$9$$$; $$$b_5$$$ is the greatest common divisor of $$$7$$$ and $$$84$$$, that is $$$7$$$. So $$$b = [1, 4, 2, 9, 7]$$$. All values in this array are distinct, so the matrix is diverse. The magnitude is equal to $$$9$$$.For a given $$$r$$$ and $$$c$$$, find a diverse matrix that minimises the magnitude. If there are multiple solutions, you may output any of them. If there are no solutions, output a single integer $$$0$$$. ", "input_spec": "The only line in the input contains two space separated integers $$$r$$$ and $$$c$$$ ($$$1 \\leq r,c \\leq 500$$$) — the number of rows and the number of columns of the matrix to be found.", "output_spec": "If there is no solution, output a single integer $$$0$$$. Otherwise, output $$$r$$$ rows. The $$$i$$$-th of them should contain $$$c$$$ space-separated integers, the $$$j$$$-th of which is $$$a_{i,j}$$$ — the positive integer in the $$$i$$$-th row and $$$j$$$-th column of a diverse matrix minimizing the magnitude. Furthermore, it must hold that $$$1 \\leq a_{i,j} \\leq 10^9$$$. It can be shown that if a solution exists, there is also a solution with this additional constraint (still having minimum possible magnitude).", "sample_inputs": ["2 2", "1 1"], "sample_outputs": ["4 12\n2 9", "0"], "notes": "NoteIn the first example, the GCDs of rows are $$$b_1 = 4$$$ and $$$b_2 = 1$$$, and the GCDs of columns are $$$b_3 = 2$$$ and $$$b_4 = 3$$$. All GCDs are pairwise distinct and the maximum of them is $$$4$$$. Since the GCDs have to be distinct and at least $$$1$$$, it is clear that there are no diverse matrices of size $$$2 \\times 2$$$ with magnitude smaller than $$$4$$$.In the second example, no matter what $$$a_{1,1}$$$ is, $$$b_1 = b_2$$$ will always hold, so there are no diverse matrices."}, "positive_code": [{"source_code": "require 'set'; require 'prime'\nINF=Float::INFINITY; MOD=10**9+7\nr,c = gets.chomp.split.map(&:to_i)\nif r==1 && c==1\n puts 0\n exit\nend\n\nif c == 1\n r.times do |i|\n puts i+2\n end\n exit\nelsif r == 1\n puts (2..(c+1)).to_a.join(' ')\n exit\nend\n\nr.times do |i|\n num = c + 1 + i\n tmp = []\n c.times do |j|\n tmp.push num * (j+1)\n end\n puts tmp.join(' ')\nend\n"}], "negative_code": [], "src_uid": "acebe5e1d927d32d65a4500d1d45c4ba"} {"nl": {"description": "Polycarp often uses his smartphone. He has already installed $$$n$$$ applications on it. Application with number $$$i$$$ takes up $$$a_i$$$ units of memory.Polycarp wants to free at least $$$m$$$ units of memory (by removing some applications).Of course, some applications are more important to Polycarp than others. He came up with the following scoring system — he assigned an integer $$$b_i$$$ to each application: $$$b_i = 1$$$ — regular application; $$$b_i = 2$$$ — important application. According to this rating system, his phone has $$$b_1 + b_2 + \\ldots + b_n$$$ convenience points.Polycarp believes that if he removes applications with numbers $$$i_1, i_2, \\ldots, i_k$$$, then he will free $$$a_{i_1} + a_{i_2} + \\ldots + a_{i_k}$$$ units of memory and lose $$$b_{i_1} + b_{i_2} + \\ldots + b_{i_k}$$$ convenience points.For example, if $$$n=5$$$, $$$m=7$$$, $$$a=[5, 3, 2, 1, 4]$$$, $$$b=[2, 1, 1, 2, 1]$$$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $$$1, 4$$$ and $$$5$$$. In this case, it will free $$$a_1+a_4+a_5=10$$$ units of memory and lose $$$b_1+b_4+b_5=5$$$ convenience points; applications with numbers $$$1$$$ and $$$3$$$. In this case, it will free $$$a_1+a_3=7$$$ units of memory and lose $$$b_1+b_3=3$$$ convenience points. applications with numbers $$$2$$$ and $$$5$$$. In this case, it will free $$$a_2+a_5=7$$$ memory units and lose $$$b_2+b_5=2$$$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $$$m$$$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$, $$$1 \\le m \\le 10^9$$$) — the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$) — the number of memory units used by applications. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \\ldots, b_n$$$ ($$$1 \\le b_i \\le 2$$$) — the convenience points of each application. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $$$m$$$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. ", "sample_inputs": ["5\n5 7\n5 3 2 1 4\n2 1 1 2 1\n1 3\n2\n1\n5 10\n2 3 2 3 2\n1 2 1 2 1\n4 10\n5 1 3 4\n1 2 1 2\n4 5\n3 2 1 2\n2 1 2 1"], "sample_outputs": ["2\n-1\n6\n4\n3"], "notes": "NoteIn the first test case, it is optimal to remove applications with numbers $$$2$$$ and $$$5$$$, freeing $$$7$$$ units of memory. $$$b_2+b_5=2$$$.In the second test case, by removing the only application, Polycarp will be able to clear only $$$2$$$ of memory units out of the $$$3$$$ needed.In the third test case, it is optimal to remove applications with numbers $$$1$$$, $$$2$$$, $$$3$$$ and $$$4$$$, freeing $$$10$$$ units of memory. $$$b_1+b_2+b_3+b_4=6$$$.In the fourth test case, it is optimal to remove applications with numbers $$$1$$$, $$$3$$$ and $$$4$$$, freeing $$$12$$$ units of memory. $$$b_1+b_3+b_4=4$$$.In the fifth test case, it is optimal to remove applications with numbers $$$1$$$ and $$$2$$$, freeing $$$5$$$ units of memory. $$$b_1+b_2=3$$$."}, "positive_code": [{"source_code": "INF = 10**18\n\ngets.to_i.times do |testcase|\n\tn, m = gets.split.map(&:to_i)\n\ta = gets.split.map(&:to_i)\n\tb = gets.split.map(&:to_i)\n\n\ta1 = (0...n).select { b[_1] == 1 }.map { a[_1] }.sort.reverse\n\ta2 = (0...n).select { b[_1] == 2 }.map { a[_1] }.sort.reverse\n\n\tsum1 = [0] * -~a1.size\n\ta1.size.times { |i| sum1[i + 1] = sum1[i] + a1[i] }\n\tsum2 = [0] * -~a2.size\n\ta2.size.times { |i| sum2[i + 1] = sum2[i] + a2[i] }\n\n\tans =\n\t\tsum2.each_with_index.map do |s2, cnt2|\n\t\t\tcnt1 = sum1.bsearch_index { _1 >= m - s2 }\n\t\t\tcnt1 ? cnt2 * 2 + cnt1 : INF\n\t\tend.min\n\n\tputs ans < INF ? ans : -1\nend\n"}, {"source_code": "#!/usr/bin/env ruby\r\n\r\ndef getar\r\n return gets.chomp.split.map(&:to_i)\r\nend \r\n\r\ndef pfr(ar)\r\n res = (0..ar.length).map{|x| 0}\r\n for i in (1..ar.length)\r\n res[i] = res[i-1] + ar[i-1];\r\n end\r\n return res\r\nend\r\n\r\nclass Opt\r\n\r\n def initialize(mem, cost, units)\r\n ar = mem.zip(cost)\r\n @one = pfr(ar.select{|x, y| (y == 1)}.map{|x, y| x}.sort.reverse)\r\n @two = pfr(ar.select{|x, y| (y == 2)}.map{|x, y| x}.sort.reverse)\r\n @units = units\r\n end\r\n\r\n def bsearch(val)\r\n # find ind of two where two[i] >= val\r\n if @two[@two.length-1] < val\r\n return nil\r\n end\r\n l,r = [0, @two.length-1]\r\n while true\r\n if r-l <= 1\r\n return(@two[l] >= val ? l : r)\r\n end\r\n mid = (l+r).div(2)\r\n if @two[mid] >= val\r\n r = mid\r\n else\r\n l = mid\r\n end\r\n end\r\n end\r\n\r\n def cost(ones)\r\n if ones >= @one.length\r\n return nil\r\n end\r\n ind = self.bsearch(@units - @one[ones])\r\n return (ind ? (ones + 2*ind) : nil)\r\n end\r\n\r\n def ones\r\n @one.length\r\n end\r\n\r\nend\r\n\r\ntc = getar()[0]\r\n\r\nfor t in 1..tc\r\n n, m = getar\r\n mem = getar\r\n cost = getar\r\n \r\n opt = Opt.new(mem, cost, m)\r\n possible = (0..opt.ones).map{|x| opt.cost(x)}.select{|x| x}.min\r\n puts \"#{possible ? possible : -1}\"\r\nend\r\n"}, {"source_code": "gets.to_i.times do\r\n n,m=gets.split.map(&:to_i)\r\n a=gets.split.map(&:to_i)\r\n b=gets.split.map(&:to_i)\r\n a1=[]\r\n a2=[]\r\n n.times do |i|\r\n if b[i]==1\r\n a1 << a[i]\r\n else\r\n a2 << a[i]\r\n end\r\n end\r\n a1.sort!.reverse!\r\n a2.sort!.reverse!\r\n z1=a1.size\r\n z2=a2.size\r\n i=0\r\n k=0\r\n while i=0\r\n k-=a1[i]\r\n i-=1\r\n while k=m&&(i+1)+j*2= m - s2 }\n\t\t\tcnt1 ? cnt2 * 2 + cnt1 : INF\n\t\tend.min\n\tans1 =\n\t\tsum1.each_with_index.map do |s1, cnt1|\n\t\t\tcnt2 = sum2.bsearch_index { _1 >= m - s1 }\n\t\t\tcnt2 ? cnt2 * 2 + cnt1 : INF\n\t\tend.min\n\tans = [ans1, ans2].min\n\n\tputs ans < INF ? ans : -1\nend\n"}, {"source_code": "INF = 10**18\n\ngets.to_i.times do\n\tn, m = gets.split.map(&:to_i)\n\ta = gets.split.map(&:to_i)\n\tb = gets.split.map(&:to_i)\n\n\ta1 = (0...m).select { b[_1] == 1 }.map { a[_1] }.sort.reverse\n\ta2 = (0...m).select { b[_1] == 2 }.map { a[_1] }.sort.reverse\n\n\tsum1 = [0] * -~a1.size\n\ta1.size.times { |i| sum1[i + 1] = sum1[i] + a1[i] }\n\tsum2 = [0] * -~a2.size\n\ta2.size.times { |i| sum2[i + 1] = sum2[i] + a2[i] }\n\n\tans =\n\t\tsum2.each_with_index.map do |s2, cnt2|\n\t\t\tcnt1 = sum1.bsearch_index { _1 >= m - s2 }\n\t\t\tcnt1 ? cnt2 * 2 + cnt1 : INF\n\t\tend.min\n\tputs ans < INF ? ans : -1\nend\n"}], "src_uid": "e1fa7250c7004c093e1af788f23b2863"} {"nl": {"description": "Fox Ciel saw a large field while she was on a bus. The field was a n × m rectangle divided into 1 × 1 cells. Some cells were wasteland, and other each cell contained crop plants: either carrots or kiwis or grapes. After seeing the field carefully, Ciel found that the crop plants of each cell were planted in following procedure: Assume that the rows are numbered 1 to n from top to bottom and the columns are numbered 1 to m from left to right, and a cell in row i and column j is represented as (i, j). First, each field is either cultivated or waste. Crop plants will be planted in the cultivated cells in the order of (1, 1) → ... → (1, m) → (2, 1) → ... → (2, m) → ... → (n, 1) → ... → (n, m). Waste cells will be ignored. Crop plants (either carrots or kiwis or grapes) will be planted in each cell one after another cyclically. Carrots will be planted in the first cell, then kiwis in the second one, grapes in the third one, carrots in the forth one, kiwis in the fifth one, and so on. The following figure will show you the example of this procedure. Here, a white square represents a cultivated cell, and a black square represents a waste cell. Now she is wondering how to determine the crop plants in some certain cells. ", "input_spec": "In the first line there are four positive integers n, m, k, t (1 ≤ n ≤ 4·104, 1 ≤ m ≤ 4·104, 1 ≤ k ≤ 103, 1 ≤ t ≤ 103), each of which represents the height of the field, the width of the field, the number of waste cells and the number of queries that ask the kind of crop plants in a certain cell. Following each k lines contains two integers a, b (1 ≤ a ≤ n, 1 ≤ b ≤ m), which denotes a cell (a, b) is waste. It is guaranteed that the same cell will not appear twice in this section. Following each t lines contains two integers i, j (1 ≤ i ≤ n, 1 ≤ j ≤ m), which is a query that asks you the kind of crop plants of a cell (i, j).", "output_spec": "For each query, if the cell is waste, print Waste. Otherwise, print the name of crop plants in the cell: either Carrots or Kiwis or Grapes.", "sample_inputs": ["4 5 5 6\n4 3\n1 3\n3 3\n2 5\n3 2\n1 3\n1 4\n2 3\n2 4\n1 1\n1 1"], "sample_outputs": ["Waste\nGrapes\nCarrots\nKiwis\nCarrots\nCarrots"], "notes": "NoteThe sample corresponds to the figure in the statement."}, "positive_code": [{"source_code": "input=gets.split.map(&:to_i)\nh=input[0]\nw=input[1]\nk=input[2]\nq=input[3]\ncnt=Array.new(h,0)\nwaste=[]\nk.times{\n input=gets.split.map(&:to_i)\n input[0]-=1\n input[1]-=1\n waste.push(input)\n cnt[input[0]]+=1\n}\nq.times{\n input=gets.split.map(&:to_i)\n input[0]-=1\n input[1]-=1\n x=input[1]\n y=input[0]\n no=y*w+x\n is_was=0\n waste.each{|was|\n if was[0]0 then\n puts \"Waste\"\n elsif\n puts [\"Carrots\",\"Kiwis\",\"Grapes\"][no%3]\n end\n}"}, {"source_code": "def r;gets.split.map(&:to_i)end;n,m,k,t=r;d=[];k.times{p=r;d<<(p[0]-1)*m+p[1]};t.times{b=r;b=(b[0]-1)*m+b[1];puts(d.include?(b)?:Waste:[:Carrots,:Kiwis,:Grapes][(b-d.count{|v|v a[x1]\n\n while x1 - x0 > 1\n x2 = (x0+x1)/2\n return nil if a[x2] == v\n if v > a[x2]\n x0 = x2\n else\n x1 = x2\n end\n end\n\n x1\nend\n\nn, m, k, t = gets.split.map(&:to_i)\n\na = []\nk.times do\n i, j = gets.split.map(&:to_i)\n a << (i-1)*m+(j-1)\nend\na.uniq!\na.sort!\n\nt.times do\n i, j = gets.split.map(&:to_i)\n pos = (i-1)*m+(j-1)\n\n v = b_search(a, pos)\n if v\n puts %w(Carrots Kiwis Grapes)[(pos-v)%3]\n else\n puts \"Waste\"\n end\nend\n"}, {"source_code": "n, m, k, t = gets.split.map(&:to_i)\nA = Hash.new(0)\nk.times do \n a, b = gets.split.map { |x| x.to_i - 1}\n A[a * m + b] = 1\nend\nM = Array.new\n(A.keys.sort).each do |key|\n M << key\nend\nt.times do \n a, b = gets.split.map { |x| x.to_i - 1}\n key = a * m + b\n if A.has_key? key\n puts \"Waste\"\n else\n u = key - (M.select { |x| x < key }).size\n x = u % 3\n if x == 0\n puts \"Carrots\"\n elsif x == 1\n puts \"Kiwis\"\n elsif x == 2\n puts \"Grapes\"\n end\n end\nend\n"}, {"source_code": "def r;gets.split.map(&:to_i)end;n,m,k,t=r;d=[];k.times{p=r;d<<(p[0]-1)*m+p[1]};t.times{b=r;b=(b[0]-1)*m+b[1];puts(d.include?(b)?:Waste:[:Carrots,:Kiwis,:Grapes][(b-d.count{|v|v a[l]\n return a[l]\n end\n end\n \n if l - h == 1\n l, h = h, l\n end\n \n if h - l == 1\n if b < a[l]\n return 0\n elsif b >= a[l] && b < a[h]\n return l + 1\n else\n return h + 1\n end\n elsif b > a[mid]\n return search(a, mid+1, h, b)\n elsif b < a[mid]\n return search(a, l, mid-1, b)\n else\n return 0\n end\nend\nt.times do \n a, b = gets.split.map { |x| x.to_i - 1}\n key = a * m + b\n if A.has_key? key\n puts \"Waste\"\n else\n# puts \"Key #{key}\"\n# bb = search(M, 0, M.size - 1, key)\n# puts \"Key = #{key} -> #{bb}\"\n u = key - search(M, 0, M.size - 1, key)\n x = u % 3\n if x == 0\n puts \"Carrots\"\n elsif x == 1\n puts \"Kiwis\"\n elsif x == 2\n puts \"Grapes\"\n end\n end\nend\n"}, {"source_code": "n, m, k, t = gets.split.map(&:to_i)\nA = Hash.new(0)\nk.times do \n a, b = gets.split.map { |x| x.to_i - 1}\n A[a * m + b] = 1\nend\n#p A\nM = Array.new\n(A.keys.sort).each do |key|\n M << key\nend\n#p M\ndef search(a, l, h, b)\n #puts \"Search l#{l} h#{h} b#{b}\"\n mid = l + ((h - l) / 2).to_i\n \n if l > h\n \n \n if l >= a.size\n if b < a.last\n return a.size - 1\n else\n return a.size\n end\n end\n \n \n if b < a[l]\n return l\n elsif b > a[l]\n return l + 1\n end\n end\n \n if h - l == 1\n if b < a[l]\n return 0\n elsif b >= a[l] && b < a[h]\n return l + 1\n else\n return h + 1\n end\n elsif b > a[mid]\n return search(a, mid+1, h, b)\n elsif b < a[mid]\n return search(a, l, mid-1, b)\n else\n return 0\n end\nend\nt.times do \n a, b = gets.split.map { |x| x.to_i - 1}\n key = a * m + b\n if A.has_key? key\n puts \"Waste\"\n else\n# puts \"Key #{key}\"\n# bb = search(M, 0, M.size - 1, key)\n# puts \"Key = #{key} -> #{bb}\"\n u = key - search(M, 0, M.size - 1, key)\n x = u % 3\n if x == 0\n puts \"Carrots\"\n elsif x == 1\n puts \"Kiwis\"\n elsif x == 2\n puts \"Grapes\"\n end\n end\nend\n"}], "src_uid": "bfef3f835357dae290620efabe650580"} {"nl": {"description": "Let A = {a1, a2, ..., an} be any permutation of the first n natural numbers {1, 2, ..., n}. You are given a positive integer k and another sequence B = {b1, b2, ..., bn}, where bi is the number of elements aj in A to the left of the element at = i such that aj ≥ (i + k).For example, if n = 5, a possible A is {5, 1, 4, 2, 3}. For k = 2, B is given by {1, 2, 1, 0, 0}. But if k = 3, then B = {1, 1, 0, 0, 0}.For two sequences X = {x1, x2, ..., xn} and Y = {y1, y2, ..., yn}, let i-th elements be the first elements such that xi ≠ yi. If xi < yi, then X is lexicographically smaller than Y, while if xi > yi, then X is lexicographically greater than Y.Given n, k and B, you need to determine the lexicographically smallest A.", "input_spec": "The first line contains two space separated integers n and k (1 ≤ n ≤ 1000, 1 ≤ k ≤ n). On the second line are n integers specifying the values of B = {b1, b2, ..., bn}.", "output_spec": "Print on a single line n integers of A = {a1, a2, ..., an} such that A is lexicographically minimal. It is guaranteed that the solution exists.", "sample_inputs": ["5 2\n1 2 1 0 0", "4 2\n1 0 0 0"], "sample_outputs": ["4 1 5 2 3", "2 3 1 4"], "notes": null}, "positive_code": [{"source_code": "n,k,*b=gets(p).split.map(&:to_i)\na=[]\nb.each_with_index.reverse_each{|c,i|\n\tj=a.size\n\ta.each_with_index{|d,l|\n\t\tif c<1\n\t\t\tj=l\n\t\t\tbreak\n\t\tend\n\t\tc-=1 if d>i+k \n\t}\n\ta.insert j,i+1\n}\nputs a*\" \"\n"}, {"source_code": "n,k,*b=gets(p).split.map(&:to_i)\na=[]\nb.each_with_index.reverse_each{|c,i|\n\tj=a.size\n\ta.each_with_index{|d,l|\n\t\tif c<1\n\t\t\tj=l\n\t\t\tbreak\n\t\tend\n\t\tc-=1 if d>i+k \n\t}\n\ta.insert j,i+1\n}\nputs a*\" \"\n"}, {"source_code": "n,k,*b=gets(p).split.map(&:to_i)\na=[]\nb.each_with_index.reverse_each{|c,i|\n\tj=a.size\n\ta.each_with_index{|d,l|\n\t\tif c<1\n\t\t\tj=l\n\t\t\tbreak\n\t\tend\n\t\tc-=1 if d>i+k \n\t}\n\ta.insert j,i+1\n}\nputs a*\" \""}, {"source_code": "n,k,*b=gets(p).split.map(&:to_i)\na=[]\nb.each_with_index.reverse_each{|c,i|\n\tj=a.size\n\ta.each_with_index{|d,l|\n\t\tif c<1\n\t\t\tj=l\n\t\t\tbreak\n\t\tend\n\t\tc-=1 if d>i+k \n\t}\n\ta.insert j,i+1\n}\nputs a*\" \"\n"}, {"source_code": "n,k,*b=gets(p).split.map(&:to_i)\na=[]\nb.each_with_index.reverse_each{|c,i|\n\tj=a.size\n\ta.each_with_index{|d,l|\n\t\tif c<1\n\t\t\tj=l\n\t\t\tbreak\n\t\tend\n\t\tc-=1 if d>i+k \n\t}\n\ta.insert j,i+1\n}\nputs a*\" \"\n"}, {"source_code": "n, k, *b = gets(p).split.map(&:to_i)\na = []\n(0...n).reverse_each { |i|\n x = 0\n while x < a.size\n break if b[i] < 1\n b[i] -= 1 if a[x] > i + k\n x += 1\n end\n a.insert x, i + 1\n}\nputs a * \" \""}, {"source_code": "n,k,*b=gets(p).split.map(&:to_i)\na=[]\n(0...n).reverse_each {|i|\n x=0\n while xi+k\n x+=1\n end\n a.insert x,i+1\n}\nputs a*\" \""}, {"source_code": "n, k, *b = gets(p).split.map(&:to_i)\na = []\nfor i in (n - 1).downto(0)\n x = a.size\n for j in 0...x\n if b[i] < 1\n x = j\n break\n end\n b[i] -= 1 if a[j] > i + k\n end\n a.insert x, i + 1\nend\nputs a * \" \""}, {"source_code": "n,k,*b=gets(p).split.map(&:to_i)\na=[]\nb.each_with_index.reverse_each{|c,i|\n j=a.size\n a.each_with_index{|d,l|\n if c<1\n j=l\n break\n end\n c-=1 if d>i+k \n }\n a.insert j,i+1\n}\nputs a*\" \""}, {"source_code": "n,k,*b=gets(p).split.map(&:to_i)\na=[]\nb.each_with_index.reverse_each{|c,i|\n\tj=a.size\n\ta.each_with_index{|d,l|\n\t\tif c<1\n\t\t\tj=l\n\t\t\tbreak\n\t\tend\n\t\tc-=1 if d>i+k \n\t}\n\ta.insert j,i+1\n}\nputs a*\" \"\n"}, {"source_code": "def generate(a, k)\n 1.upto(a.size).map do |i|\n pos = a.index(i)\n (0..pos-1).count{|j| a[j] >= i+k}\n end\nend\n\ndef ok?(a, b, k)\n generate(a, k) == b\nend\n\ndef hoge\n n = 1000\n k = 5\n b = generate((1..n).to_a.shuffle, k)\n puts [n, k]*\" \"\n puts b*\" \"\n exit\nend\n\ndef solve(n, k, b)\n o = ((n-k+1)..n).to_a\n (n-k).downto(1) do |i|\n c = 0\n j = 0\n while c < b[i-1]\n c += 1 if o[j] >= i+k\n j += 1\n end \n o[j,0] = i\n end\n o\nend\n\ndef test\n n = 9\n k = 4\n h = {}\n (1..n).to_a.permutation do |a|\n b = generate(a, k)\n unless h[b]\n a2 = solve(n, k, b)\n p [a, b, a2] unless a == a2\n h[b] = true\n end\n end\n\n exit\nend\n\n#test\n\nn, k = gets.split.map(&:to_i)\nb = gets.split.map(&:to_i)\no = solve(n, k, b)\nputs o*\" \"\n\n\n"}, {"source_code": "n,k,*b=gets(p).split.map(&:to_i)\na=[]\nb.each_with_index.reverse_each{|c,i|\n\tj=a.size\n\ta.each_with_index{|d,l|\n\t\tif c<1\n\t\t\tj=l\n\t\t\tbreak\n\t\tend\n\t\tc-=1 if d>i+k \n\t}\n\ta.insert j,i+1\n}\nputs a*\" \"\n"}, {"source_code": "n,k,*b=gets(p).split.map(&:to_i)\na=[]\nb.each_with_index.reverse_each{|c,i|\n\tj=a.size\n\ta.each_with_index{|d,l|\n\t\tif c<1\n\t\t\tj=l\n\t\t\tbreak\n\t\tend\n\t\tc-=1 if d>i+k \n\t}\n\ta.insert j,i+1\n}\nputs a*\" \""}, {"source_code": "n,k,*b=gets(p).split.map(&:to_i)\na=[]\nb.each_with_index.reverse_each{|c,i|\n\tj=a.size\n\ta.each_with_index{|d,l|\n\t\tif c<1\n\t\t\tj=l\n\t\t\tbreak\n\t\tend\n\t\tc-=1 if d>i+k \n\t}\n\ta.insert j,i+1\n}\nputs a*\" \"\n"}], "negative_code": [], "src_uid": "3c73d33682dc4548646fd240b6e22471"} {"nl": {"description": "We call two numbers $$$x$$$ and $$$y$$$ similar if they have the same parity (the same remainder when divided by $$$2$$$), or if $$$|x-y|=1$$$. For example, in each of the pairs $$$(2, 6)$$$, $$$(4, 3)$$$, $$$(11, 7)$$$, the numbers are similar to each other, and in the pairs $$$(1, 4)$$$, $$$(3, 12)$$$, they are not.You are given an array $$$a$$$ of $$$n$$$ ($$$n$$$ is even) positive integers. Check if there is such a partition of the array into pairs that each element of the array belongs to exactly one pair and the numbers in each pair are similar to each other.For example, for the array $$$a = [11, 14, 16, 12]$$$, there is a partition into pairs $$$(11, 12)$$$ and $$$(14, 16)$$$. The numbers in the first pair are similar because they differ by one, and in the second pair because they are both even.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of two lines. The first line contains an even positive integer $$$n$$$ ($$$2 \\le n \\le 50$$$) — length of array $$$a$$$. The second line contains $$$n$$$ positive integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 100$$$).", "output_spec": "For each test case print: YES if the such a partition exists, NO otherwise. The letters in the words YES and NO can be displayed in any case.", "sample_inputs": ["7\n4\n11 14 16 12\n2\n1 8\n4\n1 1 1 1\n4\n1 2 5 6\n2\n12 13\n6\n1 6 3 10 5 8\n6\n1 12 3 10 5 8"], "sample_outputs": ["YES\nNO\nYES\nYES\nYES\nYES\nNO"], "notes": "NoteThe first test case was explained in the statement.In the second test case, the two given numbers are not similar.In the third test case, any partition is suitable."}, "positive_code": [{"source_code": "gets.to_i.times {\n gets\n a, b=gets.split.map(&:to_i).partition(&:odd?)\n puts ((a.size.even? or a.any?{|x| b.index{|y| (y-x).abs==1}}) ? :YES : :NO)\n}"}, {"source_code": "def solve\n _ = gets\n arr = gets.strip.split(' ').map(&:to_i);\n od = []\n ev = []\n arr.each { |el|\n ev << el if el.even?\n od << el if el.odd?\n }\n flg = false\n od.each { |el|\n ev.each { |el2|\n if (el - el2).abs == 1\n flg = true\n break\n end\n }\n }\n if od.size.even? && ev.size.even?\n puts \"YES\"\n elsif flg == true\n puts \"YES\"\n else\n puts \"NO\"\n end\nend\n\n\ngets.strip.to_i.times do\n solve\nend\n"}, {"source_code": "def solve\n _ = gets\n arr = gets.strip.split(' ').map(&:to_i);\n od = []\n ev = []\n arr.each { |el|\n ev << el if el.even?\n od << el if el.odd?\n }\n od = od.sort\n ev = ev.sort\n flg = false\n od.each { |el|\n ev.each { |el2|\n if (el - el2).abs == 1\n flg = true\n break\n end\n }\n }\n if od.size.even? && ev.size.even?\n puts \"YES\"\n elsif flg == true\n puts \"YES\"\n else\n puts \"NO\"\n end\nend\n\n\ngets.strip.to_i.times do\n solve\nend\n"}, {"source_code": "t = gets.to_i\nt.times do\n n = gets.to_i\n a = gets.split.map(&:to_i).sort\n even = []\n odd = []\n for i in 0...n do\n if a[i] % 2 == 0\n even.push(a[i])\n else\n odd.push(a[i])\n end\n end\n ok = false\n if odd.length % 2 == 1\n odd.each do |v|\n if even.include?(v - 1) || even.include?(v + 1)\n ok = true\n break\n end\n end\n else\n ok = true\n end\n puts ok ? \"YES\" : \"NO\"\nend\n"}, {"source_code": "gets.to_i.times do\n\tn = gets.to_i\n\ta = gets.split.map(&:to_i).sort\n\todd = a.count { _1 % 2 == 1 }\n\tif odd.odd?\n\t\tputs a.each_cons(2).any? { _2 - _1 == 1 } ? 'YES' : 'NO'\n\telse\n\t\tputs 'YES'\n\tend\nend\n"}, {"source_code": "gets.strip.to_i.times do |t|\n _ = gets.strip.to_i\n a = gets.strip.split(' ').map(&:to_i).sort\n # puts a.group_by { |e| e % 2 }.inspect\n diffs = 0\n a.each_with_index do |e, i|\n next if i.zero?\n diffs += 1 if a[i] - a[i-1] == 1\n end\n even = a.select { |e| e.even? }.size\n odd = a.select { |e| e.odd? }.size\n if even == a.size or odd == a.size\n puts \"YES\"\n elsif even.even? && odd.even?\n puts \"YES\"\n elsif diffs > 0\n puts \"YES\"\n else\n puts \"NO\"\n end\nend\n"}, {"source_code": "gets.to_i.times {\n gets\n a, b=gets.split.map(&:to_i).partition(&:odd?)\n puts ((a.size.even? or a.any?{|x| b.index{|y| (y-x).abs==1}}) ? :YES : :NO)\n}"}, {"source_code": "gets.to_i.times {\n gets\n a, b=gets.split.map(&:to_i).partition(&:odd?)\n puts ((a.size.even? or a.any?{|x| b.index{|y| (y-x).abs==1}}) ? :YES : :NO)\n}\n"}, {"source_code": "gets.to_i.times {\n gets\n a, b=gets.split.map(&:to_i).partition(&:odd?)\n if a.size.even?\n puts :YES\n else\n puts (a.any?{|x| b.index{|y| (y-x).abs==1}} ? :YES : :NO)\n end\n}\n"}, {"source_code": "def solve\n _ = gets\n arr = gets.strip.split(' ').map(&:to_i);\n od = []\n ev = []\n\n arr.each { |el|\n ev << el if el.even?\n od << el if el.odd?\n }\n\n od = od.sort\n ev = ev.sort\n\n flg = false\n od.each { |el|\n ev.each { |el2|\n if (el - el2).abs == 1\n flg = true\n break\n end\n }\n }\n\n if od.size.even? && ev.size.even?\n puts \"YES\"\n elsif flg == true\n puts \"YES\"\n else\n puts \"NO\"\n end\nend\n\n\ngets.strip.to_i.times do\n solve\nend\n"}], "negative_code": [{"source_code": "def solve\n _ = gets\n arr = gets.strip.split(' ').map(&:to_i);\n od = 0\n ev = 0\n mn_ev = 101\n mn_od = 101\n arr.each { |el|\n od += 1 if el.even?\n ev += 1 if el.odd?\n mn_ev = [mn_ev, el].min if el.even?\n mn_od = [mn_od, el].min if el.odd?\n }\n\n if od.even? && ev.even?\n puts \"YES\"\n elsif (mn_ev - mn_od).abs == 1\n puts \"YES\"\n else\n puts \"NO\"\n end\nend\n\n\ngets.strip.to_i.times do\n solve\nend\n"}, {"source_code": "def solve\n _ = gets\n arr = gets.strip.split(' ').map(&:to_i);\n od = []\n ev = []\n arr.each { |vl|\n ev << vl if vl.even?\n od << vl if vl.odd?\n }\n od = od.sort\n ev = ev.sort.reverse\n done = 0\n od.each { |vl|\n if !ev.empty?\n if (ev.last - vl).abs == 1\n ev.pop\n done += 1\n done = od.size if done > od.size\n end\n end\n }\n if ev.size.even? && (od.size - done).even?\n puts \"YES\"\n else\n puts \"NO\"\n end\nend\n\n\ngets.strip.to_i.times do\n solve\nend\n"}, {"source_code": "def solve\n _ = gets\n arr = gets.strip.split(' ').map(&:to_i);\n od = []\n ev = []\n\n arr.each { |el|\n ev << el if el.even?\n od << el if el.odd?\n }\n\n od = od.sort\n ev = ev.sort\n\n flg = false\n od.each_with_index { |el, idx|\n if !ev[idx].nil?\n if (el - ev[idx]).abs == 1\n flg = true\n break\n end\n end\n }\n\n if od.size.even? && ev.size.even?\n puts \"YES\"\n elsif flg == true\n puts \"YES\"\n else\n puts \"NO\"\n end\nend\n\n\ngets.strip.to_i.times do\n solve\nend\n"}, {"source_code": "def solve\n _ = gets\n arr = gets.strip.split(' ').map(&:to_i);\n od = []\n ev = []\n arr.each { |vl|\n ev << vl if vl.even?\n od << vl if vl.odd?\n }\n od = od.sort\n ev = ev.sort.reverse\n done = 0\n od.each { |vl|\n if (od.size - done).even? and ev.size.even?\n puts \"YES\"\n return nil\n end\n if !ev.empty?\n if (ev.last - vl).abs == 1\n ev.pop\n done += 1\n done = od.size if done > od.size\n end\n end\n }\n puts \"NO\"\nend\n\n\ngets.strip.to_i.times do\n solve\nend\n"}, {"source_code": "def solve\n _ = gets\n arr = gets.strip.split(' ').map(&:to_i);\n od = []\n ev = []\n arr.each { |vl|\n ev << vl if vl.even?\n od << vl if vl.odd?\n }\n od = od.sort\n ev = ev.sort.reverse\n od.each { |vl|\n if !ev.empty?\n if (ev.last - vl).abs != 1\n puts \"NO\"\n return nil\n end\n ev.pop\n end\n }\n puts \"YES\"\nend\n\n\ngets.strip.to_i.times do\n solve\nend\n"}, {"source_code": "def solve\n _ = gets\n arr = gets.strip.split(' ').map(&:to_i);\n od = []\n ev = []\n arr.each { |vl|\n ev << vl if vl.even?\n od << vl if vl.odd?\n }\n od = od.sort\n ev = ev.sort.reverse\n od.each { |vl|\n if !ev.empty?\n if (ev.last - vl).abs == 1\n ev.pop\n end\n end\n }\n puts \"YES\" if ev.size.even? && od.size.even?\nend\n\n\ngets.strip.to_i.times do\n solve\nend\n"}, {"source_code": "def solve\n _ = gets\n arr = gets.strip.split(' ').map(&:to_i);\n od = []\n ev = []\n arr.each { |vl|\n ev << vl if vl.even?\n od << vl if vl.odd?\n }\n if od.size.even? && ev.size.even?\n puts \"YES\"\n return nil\n end\n od = od.sort\n ev = ev.sort.reverse\n done = 0\n od.each { |vl|\n if !ev.empty?\n if (ev.last - vl).abs == 1\n ev.pop\n done += 1\n done = od.size if done > od.size\n end\n end\n }\n if ev.size.even? && (od.size - done).even?\n puts \"YES\"\n else\n puts \"NO\"\n end\nend\n\n\ngets.strip.to_i.times do\n solve\nend\n"}, {"source_code": "def solve\n _ = gets\n arr = gets.strip.split(' ').map(&:to_i);\n od = 0\n ev = 0\n mn_ev = 101\n mn_od = 101\n arr.each { |el|\n od += 1 if el.even?\n ev += 1 if el.odd?\n mn_ev = [mn_ev, el].min if el.even?\n mn_od = [mn_od, el].min if el.odd?\n }\n if od.size.even? && ev.size.even?\n puts \"YES\"\n elsif (mn_ev - mn_od).abs == 1\n puts \"YES\"\n else\n puts \"NO\"\n end\nend\n\n\ngets.strip.to_i.times do\n solve\nend\n"}, {"source_code": "def solve\n _ = gets\n arr = gets.strip.split(' ').map(&:to_i);\n od = []\n ev = []\n arr.each { |vl|\n ev << vl if vl.even?\n od << vl if vl.odd?\n }\n od = od.sort\n ev = ev.sort.reverse\n done = 0\n od.each { |vl|\n if (od.size - done).even? && ev.size.even?\n puts \"YES\"\n return nil\n end\n if !ev.empty?\n if (ev.last - vl).abs == 1\n ev.pop\n done += 1\n done = od.size if done > od.size\n end\n end\n }\n puts \"NO\"\nend\n\n\ngets.strip.to_i.times do\n solve\nend\n"}, {"source_code": "def solve\n _ = gets\n arr = gets.strip.split(' ').map(&:to_i);\n p arr\n od = 0\n ev = 0\n arr.each { |vl|\n ev += 1 if vl.even?\n od += 1 if vl.odd?\n }\n if ev >= od && (ev - od).even?\n puts \"YES\"\n else\n puts \"NO\"\n end\n\nend\n\n\ngets.strip.to_i.times do\n solve\nend\n"}, {"source_code": "def solve\n _ = gets\n arr = gets.strip.split(' ').map(&:to_i);\n od = []\n ev = []\n arr.each { |vl|\n ev << vl if vl.even?\n od << vl if vl.odd?\n }\n if od.size > ev.size\n puts \"NO\"\n #return nil\n end\n od = od.sort\n ev = ev.sort.reverse\n od.each { |vl|\n if !ev.empty?\n if (ev.last - vl).abs != 1\n puts \"NO\"\n return nil\n end\n end\n }\n puts \"YES\"\nend\n\n\ngets.strip.to_i.times do\n solve\nend\n"}, {"source_code": "def solve\n _ = gets\n arr = gets.strip.split(' ').map(&:to_i);\n od = 0\n ev = 0\n arr.each { |vl|\n ev += 1 if vl.even?\n od += 1 if vl.odd?\n }\n if ev >= od && (ev - od).even?\n puts \"YES\"\n else\n puts \"NO\"\n end\nend\n\n\ngets.strip.to_i.times do\n solve\nend\n"}, {"source_code": "def solve\n _ = gets\n arr = gets.strip.split(' ').map(&:to_i);\n od = []\n ev = []\n arr.each { |vl|\n ev << vl if vl.even?\n od << vl if vl.odd?\n }\n if od.size > ev.size\n puts \"NO\"\n return nil\n end\n od = od.sort\n ev = ev.sort.reverse\n od.each { |vl|\n if !ev.empty?\n if (ev.last - vl).abs != 1\n puts \"NO\"\n return nil\n end\n end\n }\n puts \"YES\"\nend\n\n\ngets.strip.to_i.times do\n solve\nend\n"}, {"source_code": "t = gets.to_i\nt.times do\n n = gets.to_i\n a = gets.split.map(&:to_i).sort\n skip = false\n even = 0\n for i in 0...n do\n if !skip\n if i < n - 1 && a[i + 1] - a[i] == 1\n skip = true\n else\n even += 1 if a[i] % 2 == 0\n end\n else\n skip = false\n end\n end\n puts (even % 2 == 0) ? \"YES\" : \"NO\"\nend\n"}, {"source_code": "gets.strip.to_i.times do |t|\n _ = gets.strip.to_i\n a = gets.strip.split(' ').map(&:to_i).sort\n # puts a.group_by { |e| e % 2 }.inspect\n diffs = 0\n a.each_with_index do |e, i|\n next if i.zero?\n diffs += 1 if a[i] - a[i-1] == 1\n end\n even = a.select { |e| e.even? }.size\n odd = a.select { |e| e.odd? }.size\n if even == a.size or odd == a.size\n puts \"YES\"\n elsif even == odd && even.even?\n puts \"YES\"\n elsif diffs > 0\n puts \"YES\"\n else\n puts \"NO\"\n end\nend\n"}, {"source_code": "gets.to_i.times {\n gets\n a, b=gets.split.map(&:to_i).partition(&:odd?)\n puts (a.size.even? or a.any?{|x| b.index{|y| (y-x).abs==1}} ? :YES : :NO)\n}\n"}, {"source_code": "gets.to_i.times {\n gets\n puts (gets.split.map(&:to_i).sort.each_slice(2).all?{|x,y|y-x==1 or (x+y)%2==0} ? :YES : :NO)\n}\n"}, {"source_code": "def solve\n _ = gets\n arr = gets.strip.split(' ').map(&:to_i);\n od = []\n ev = []\n arr.each { |vl|\n ev << vl if vl.even?\n od << vl if vl.odd?\n }\n od = od.sort\n ev = ev.sort.reverse\n done = 0\n od.each { |vl|\n if !ev.empty?\n if (ev.last - vl).abs == 1\n ev.pop\n done += 1\n end\n end\n }\n if ev.size.even? && (od.size - done).even?\n puts \"YES\"\n else\n puts \"NO\"\n end\nend\n\n\ngets.strip.to_i.times do\n solve\nend\n"}, {"source_code": "def solve\n _ = gets\n arr = gets.strip.split(' ').map(&:to_i);\n od = []\n ev = []\n arr.each { |vl|\n ev << vl if vl.even?\n od << vl if vl.odd?\n }\n od = od.sort\n ev = ev.sort.reverse\n done = 0\n od.each { |vl|\n if (od.size - done).even? && ev.size.even?\n puts \"YES\"\n return nil\n end\n if !ev.empty?\n if (ev.last - vl).abs == 1\n ev.pop\n done += 1\n done = od.size if done > od.size\n end\n end\n }\n if (od.size - done).even? && ev.size.even?\n puts \"YES\"\n return nil\n end\n puts \"NO\"\nend\n\n\ngets.strip.to_i.times do\n solve\nend\n"}, {"source_code": "def solve\n _ = gets\n arr = gets.strip.split(' ').map(&:to_i);\n od = []\n ev = []\n arr.each { |vl|\n ev << vl if vl.even?\n od << vl if vl.odd?\n }\n od = od.sort\n ev = ev.sort.reverse\n done = 0\n od.each { |vl|\n if (od.size - done).even? & ev.size.even?\n puts \"YES\"\n return nil\n end\n if !ev.empty?\n if (ev.last - vl).abs == 1\n ev.pop\n done += 1\n done = od.size if done > od.size\n end\n end\n }\n puts \"NO\"\nend\n\n\ngets.strip.to_i.times do\n solve\nend\n"}], "src_uid": "005a29a4b4e7ee4fd21444846014727b"} {"nl": {"description": "You are given an angle $$$\\text{ang}$$$. The Jury asks You to find such regular $$$n$$$-gon (regular polygon with $$$n$$$ vertices) that it has three vertices $$$a$$$, $$$b$$$ and $$$c$$$ (they can be non-consecutive) with $$$\\angle{abc} = \\text{ang}$$$ or report that there is no such $$$n$$$-gon. If there are several answers, print the minimal one. It is guarantied that if answer exists then it doesn't exceed $$$998244353$$$.", "input_spec": "The first line contains single integer $$$T$$$ ($$$1 \\le T \\le 180$$$) — the number of queries. Each of the next $$$T$$$ lines contains one integer $$$\\text{ang}$$$ ($$$1 \\le \\text{ang} < 180$$$) — the angle measured in degrees. ", "output_spec": "For each query print single integer $$$n$$$ ($$$3 \\le n \\le 998244353$$$) — minimal possible number of vertices in the regular $$$n$$$-gon or $$$-1$$$ if there is no such $$$n$$$.", "sample_inputs": ["4\n54\n50\n2\n178"], "sample_outputs": ["10\n18\n90\n180"], "notes": "NoteThe answer for the first query is on the picture above.The answer for the second query is reached on a regular $$$18$$$-gon. For example, $$$\\angle{v_2 v_1 v_6} = 50^{\\circ}$$$.The example angle for the third query is $$$\\angle{v_{11} v_{10} v_{12}} = 2^{\\circ}$$$.In the fourth query, minimal possible $$$n$$$ is $$$180$$$ (not $$$90$$$)."}, "positive_code": [{"source_code": "gets.to_i.times do\n a = gets.to_i\n b = (a*2).gcd(360)\n if 360-a*2 == b\n puts 720/b\n else\n puts 360/b\n end\nend"}, {"source_code": "def done(a)\n for i in 3..1000\n if (a * i) % 180 == 0 and (a * i)/180 < i - 1\n return i\n end\n end \n return -1\nend\nfor _ in 1..gets.to_i\n a = gets.to_i\n puts done(a)\n end"}, {"source_code": "def lscan; gets.split.map(&:to_i); end\n\nh = [-1, 180, 90, 60, 45, 36, 30, 180, 45, 20, 18, 180, 15, 180, 90, 12, 45, 180, 10, 180, 9, 60, 90, 180, 15, 36, 90, 20, 45, 180, 6, 180, 45, 60, 90, 36, 5, 180, 90, 60, 9, 180, 30, 180, 45, 4, 90, 180, 15, 180, 18, 60, 45, 180, 10, 36, 45, 60, 90, 180, 3, 180, 90, 20, 45, 36, 30, 180, 45, 60, 18, 180, 5, 180, 90, 12, 45, 180, 30, 180, 9, 20, 90, 180, 15, 36, 90, 60, 45, 180, 4, 180, 45, 60, 90, 36, 15, 180, 90, 20, 9, 180, 30, 180, 45, 12, 90, 180, 5, 180, 18, 60, 45, 180, 30, 36, 45, 20, 90, 180, 6, 180, 90, 60, 45, 36, 10, 180, 45, 60, 18, 180, 15, 180, 90, 8, 45, 180, 30, 180, 9, 60, 90, 180, 10, 36, 90, 60, 45, 180, 12, 180, 45, 20, 90, 36, 15, 180, 90, 60, 18, 180, 20, 180, 45, 24, 90, 180, 30, 180, 36, 40, 45, 180, 60, 72, 90, 120, 180, 360, -1]\n\ngets.to_i.times do\n a = gets.to_i\n p h[a]\nend\n\n# h = {}\n# \n# 3.upto(1000) do |n|\n# t = 360.to_r/n/2\n# (n-2).times do |i_|\n# i = i_ + 1\n# a = t*i\n# next unless a.denominator == 1\n# h[a.to_i] = n unless h[a.to_i]\n# end\n# end\n# \n# p h.sort.map{|e|e[1]}\n"}], "negative_code": [{"source_code": "gets.to_i.times do\n a = gets.to_i\n if a < 90\n puts 360/(a*2).gcd(360)\n else\n puts 360/(180-a)\n end\nend"}], "src_uid": "d11b56fe172110d5dfafddf880e48f18"} {"nl": {"description": "After returning from the army Makes received a gift — an array a consisting of n positive integer numbers. He hadn't been solving problems for a long time, so he became interested to answer a particular question: how many triples of indices (i,  j,  k) (i < j < k), such that ai·aj·ak is minimum possible, are there in the array? Help him with it!", "input_spec": "The first line of input contains a positive integer number n (3 ≤ n ≤ 105) — the number of elements in array a. The second line contains n positive integer numbers ai (1 ≤ ai ≤ 109) — the elements of a given array.", "output_spec": "Print one number — the quantity of triples (i,  j,  k) such that i,  j and k are pairwise distinct and ai·aj·ak is minimum possible.", "sample_inputs": ["4\n1 1 1 1", "5\n1 3 2 3 4", "6\n1 3 3 1 3 2"], "sample_outputs": ["4", "2", "1"], "notes": "NoteIn the first example Makes always chooses three ones out of four, and the number of ways to choose them is 4.In the second example a triple of numbers (1, 2, 3) is chosen (numbers, not indices). Since there are two ways to choose an element 3, then the answer is 2.In the third example a triple of numbers (1, 1, 2) is chosen, and there's only one way to choose indices."}, "positive_code": [{"source_code": "n = Integer(gets.chomp)\na = []\na = gets.chomp.split.map &:to_i\n\na = a.sort!\nx = 1\ny = 0\nz = 0\nstop = false\nfor i in 1...n\n if a[i] == a[0]\n x += 1\n else\n y += 1\n for j in i+1...n\n if a[j] == a[i]\n y += 1\n else\n z += 1\n for k in j+1...n\n if a[k] == a[j]\n z += 1\n else\n stop = true\n break\n end\n end\n end\n if stop || x + y + z == n\n break\n end\n end\n end\n if stop || x + y + z == n\n break\n end\nend\nquantity = 0\nif x >= 3\n quantity = x*(x-1)*(x-2)/6\nelsif x < 3 && x + y >= 3\n if x == 1\n quantity = y*(y-1)/2\n elsif x == 2\n quantity = y\n end\nelsif x + y < 3 && x + y + z >= 3\n if x + y == 1\n quantity = z*(z-1)*(z-2)\n elsif x + y == 2\n quantity = z\n end\nend\n\nputs quantity\n \n "}, {"source_code": "def ascan; gets.split.map(&:to_i); end\ndef scan; gets.to_i; end\n \n# combination nCr\ndef combi(n, r)\n q = 1\n 1.upto(r){|k|q=q*(n-k+1)/k}\n return q\nend\n\ngets\nha = ascan.inject({}){|h,e| h[e]||=0; h[e]+=1; h}.sort\n\nn = 0\na = []\nwhile n < 3\n v,c = ha.shift\n n += c\n a << c\nend\ns = n-a[-1]\np 3-s <= a[-1] ? combi(a[-1],3-s) : a[-1]"}], "negative_code": [{"source_code": "def ascan; gets.split.map(&:to_i); end\ndef scan; gets.to_i; end\n\ngets\nha = ascan.inject({}){|h,e| h[e]||=0; h[e]+=1; h}.sort\n\nn = 0\na = []\nwhile n < 3\n v,c = ha.shift\n n += c\n a << c\nend\n\np a[-1]"}, {"source_code": "def ascan; gets.split.map(&:to_i); end\ndef scan; gets.to_i; end\n \n# combination nCr\ndef combi(n, r)\n q = 1\n 1.upto(r){|k|q=q*(n-k+1)/k}\n return q\nend\n\ngets\nha = ascan.inject({}){|h,e| h[e]||=0; h[e]+=1; h}.sort\n\nn = 0\na = []\nwhile n < 3\n v,c = ha.shift\n n += c\n a << c\nend\n\np 3 <= a[-1] ? combi(a[-1],3) : a[-1]"}], "src_uid": "4c2d804bb2781abfb43558f8b2c6424f"} {"nl": {"description": "Mike has n strings s1, s2, ..., sn each consisting of lowercase English letters. In one move he can choose a string si, erase the first character and append it to the end of the string. For example, if he has the string \"coolmike\", in one move he can transform it into the string \"oolmikec\".Now Mike asks himself: what is minimal number of moves that he needs to do in order to make all the strings equal?", "input_spec": "The first line contains integer n (1 ≤ n ≤ 50) — the number of strings. This is followed by n lines which contain a string each. The i-th line corresponding to string si. Lengths of strings are equal. Lengths of each string is positive and don't exceed 50.", "output_spec": "Print the minimal number of moves Mike needs in order to make all the strings equal or print  - 1 if there is no solution.", "sample_inputs": ["4\nxzzwo\nzwoxz\nzzwox\nxzzwo", "2\nmolzv\nlzvmo", "3\nkc\nkc\nkc", "3\naa\naa\nab"], "sample_outputs": ["5", "2", "0", "-1"], "notes": "NoteIn the first sample testcase the optimal scenario is to perform operations in such a way as to transform all strings into \"zwoxz\"."}, "positive_code": [{"source_code": "def isEqualStr(word1, word2)\n\tnStr = word2.length\n\tfor i in 0 ... nStr\n\t\tisEqual = true\n\t\tj = 0\n\t\twhile j < nStr and isEqual\n\t\t\tisEqual &= word1[j] == word2[(i + j) % nStr]\n\t\t\tj += 1\n\t\tend\n\t\tif isEqual\n\t\t\treturn i\n\t\tend\n\tend\n\n\treturn -1\nend\n\nn = gets.chomp.to_i\ngrid = []\nfor i in 0 ... n\n\tgrid << gets.chomp\nend\n\nanswer = true\nfor i in 0 ... n - 1\n\tif isEqualStr(grid[i], grid[i + 1]) == -1\n\t\tanswer = false\n\tend\nend\n\nif not answer\n\tputs \"-1\"\nelse\n\tanswer = (1 << 31)\n\tfor i in 0 ... n\n\n\t\tauxAnswer = 0\n\t\tj = (i + 1) % n\n\t\twhile i != j\n\t\t\tauxAnswer += isEqualStr(grid[i], grid[j])\n\t\t\tj = (j + 1) % n\n\t\tend\n\t\tanswer = [answer, auxAnswer].min\n\tend\n\tputs answer\nend\n\n"}, {"source_code": "a=gets.to_i.times.map{gets.chomp.chars}\nl=a[0].size\nb=a.map{|e|\n\tidx=(0...l).select{|i|e.rotate(i)==a[0]}\n\tif idx.empty?\n\t\tp -1\n\t\texit\n\tend\n\tidx\n}\np (0...l).map{|i|b.map{|e|e.map{|f|(f+i)%l}.min}.reduce(:+)}.min"}, {"source_code": "n = gets.strip.to_i\ndata = Array.new(n) { gets.strip }\n\ninf = 10 ** 9\nans = data.map { |s|\n data.map do |ss|\n for i in (0..ss.length)\n break i if ss == s \n ss = ss[1..-1] + ss[0]\n break inf if i+1 == ss.length\n end\n end.reduce(:+)\n}.min\n\nputs (ans >= inf ? -1 : ans)\n"}], "negative_code": [{"source_code": "a=gets.to_i.times.map{gets.chomp.chars}\nl=a[0].size\nb=a.map{|e|\n\tidx=(0...l).find{|i|e.rotate(i)==a[0]}\n\tif !idx\n\t\tp -1\n\t\texit\n\tend\n\tidx\n}\np (0...l).map{|i|b.map{|e|(e+i)%l}.reduce(:+)}.min"}], "src_uid": "a3a7515219ebb0154218ee3520e20d75"} {"nl": {"description": "You are given an array a1, a2, ..., an consisting of n integers, and an integer k. You have to split the array into exactly k non-empty subsegments. You'll then compute the minimum integer on each subsegment, and take the maximum integer over the k obtained minimums. What is the maximum possible integer you can get?Definitions of subsegment and array splitting are given in notes.", "input_spec": "The first line contains two integers n and k (1 ≤ k ≤ n ≤  105) — the size of the array a and the number of subsegments you have to split the array to. The second line contains n integers a1,  a2,  ...,  an ( - 109  ≤  ai ≤  109).", "output_spec": "Print single integer — the maximum possible integer you can get if you split the array into k non-empty subsegments and take maximum of minimums on the subsegments.", "sample_inputs": ["5 2\n1 2 3 4 5", "5 1\n-4 -5 -3 -2 -1"], "sample_outputs": ["5", "-5"], "notes": "NoteA subsegment [l,  r] (l ≤ r) of array a is the sequence al,  al + 1,  ...,  ar.Splitting of array a of n elements into k subsegments [l1, r1], [l2, r2], ..., [lk, rk] (l1 = 1, rk = n, li = ri - 1 + 1 for all i > 1) is k sequences (al1, ..., ar1), ..., (alk, ..., ark).In the first example you should split the array into subsegments [1, 4] and [5, 5] that results in sequences (1, 2, 3, 4) and (5). The minimums are min(1, 2, 3, 4) = 1 and min(5) = 5. The resulting maximum is max(1, 5) = 5. It is obvious that you can't reach greater result.In the second example the only option you have is to split the array into one subsegment [1, 5], that results in one sequence ( - 4,  - 5,  - 3,  - 2,  - 1). The only minimum is min( - 4,  - 5,  - 3,  - 2,  - 1) =  - 5. The resulting maximum is  - 5."}, "positive_code": [{"source_code": "def ga\n\treturn gets.split.map &:to_i\nend\nn,k = ga\na = ga\np [a.min, [a[-1],a[0]].max,a.max][k>2?2:k-1]"}, {"source_code": "n, k = gets.split.map &:to_i\na = gets.split.map &:to_i\np [a.min, [a[0], a[-1]].max, a.max][[k, 3].min - 1]"}, {"source_code": "class Solver2\n def self.solve(arr, k)\n return arr.min if k == 1\n return arr.max if k > 2\n return [arr.first, arr.last].max\n end\nend\nstring = ARGF.read\narr = string.split(\"\\n\")\narr.map! { |elem| elem.strip }\narr1 = arr[0].split\narr2 = arr[1].split\narr1.map! { |elem| elem.split }\narr2.map! { |elem| elem.to_i }\nputs Solver2.solve(arr2, arr1[1].first.to_i)\nexit(true)\n"}], "negative_code": [{"source_code": "def ga\n\treturn gets.split.map &:to_i\nend\nn,k = ga\na = ga\np [a.min, [a[-1],a[0]].max,a.max][k[0]>2?2:k[0]-1]"}], "src_uid": "ec1a29826209a0820e8183cccb2d2f01"} {"nl": {"description": "You are given three strings $$$s$$$, $$$t$$$ and $$$p$$$ consisting of lowercase Latin letters. You may perform any number (possibly, zero) operations on these strings.During each operation you choose any character from $$$p$$$, erase it from $$$p$$$ and insert it into string $$$s$$$ (you may insert this character anywhere you want: in the beginning of $$$s$$$, in the end or between any two consecutive characters). For example, if $$$p$$$ is aba, and $$$s$$$ is de, then the following outcomes are possible (the character we erase from $$$p$$$ and insert into $$$s$$$ is highlighted): aba $$$\\rightarrow$$$ ba, de $$$\\rightarrow$$$ ade; aba $$$\\rightarrow$$$ ba, de $$$\\rightarrow$$$ dae; aba $$$\\rightarrow$$$ ba, de $$$\\rightarrow$$$ dea; aba $$$\\rightarrow$$$ aa, de $$$\\rightarrow$$$ bde; aba $$$\\rightarrow$$$ aa, de $$$\\rightarrow$$$ dbe; aba $$$\\rightarrow$$$ aa, de $$$\\rightarrow$$$ deb; aba $$$\\rightarrow$$$ ab, de $$$\\rightarrow$$$ ade; aba $$$\\rightarrow$$$ ab, de $$$\\rightarrow$$$ dae; aba $$$\\rightarrow$$$ ab, de $$$\\rightarrow$$$ dea; Your goal is to perform several (maybe zero) operations so that $$$s$$$ becomes equal to $$$t$$$. Please determine whether it is possible.Note that you have to answer $$$q$$$ independent queries.", "input_spec": "The first line contains one integer $$$q$$$ ($$$1 \\le q \\le 100$$$) — the number of queries. Each query is represented by three consecutive lines. The first line of each query contains the string $$$s$$$ ($$$1 \\le |s| \\le 100$$$) consisting of lowercase Latin letters. The second line of each query contains the string $$$t$$$ ($$$1 \\le |t| \\le 100$$$) consisting of lowercase Latin letters. The third line of each query contains the string $$$p$$$ ($$$1 \\le |p| \\le 100$$$) consisting of lowercase Latin letters.", "output_spec": "For each query print YES if it is possible to make $$$s$$$ equal to $$$t$$$, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).", "sample_inputs": ["4\nab\nacxb\ncax\na\naaaa\naaabbcc\na\naaaa\naabbcc\nab\nbaaa\naaaaa"], "sample_outputs": ["YES\nYES\nNO\nNO"], "notes": "NoteIn the first test case there is the following sequence of operation: $$$s = $$$ ab, $$$t = $$$ acxb, $$$p = $$$ cax; $$$s = $$$ acb, $$$t = $$$ acxb, $$$p = $$$ ax; $$$s = $$$ acxb, $$$t = $$$ acxb, $$$p = $$$ a. In the second test case there is the following sequence of operation: $$$s = $$$ a, $$$t = $$$ aaaa, $$$p = $$$ aaabbcc; $$$s = $$$ aa, $$$t = $$$ aaaa, $$$p = $$$ aabbcc; $$$s = $$$ aaa, $$$t = $$$ aaaa, $$$p = $$$ abbcc; $$$s = $$$ aaaa, $$$t = $$$ aaaa, $$$p = $$$ bbcc. "}, "positive_code": [{"source_code": "def subseq(a, b, i, j)\n return true if i.zero?\n return false if j.zero?\n\n return subseq(a, b, i - 1, j - 1) if a[i-1] == b[j-1]\n return subseq(a, b, i, j - 1)\nend\n\ndef char_cnt(s)\n s.split('').inject({}) { |m, c| m[c] = (m[c] || 0) + 1; m }\nend\n\ndef solve(s, t, p)\n t_cnt = char_cnt(t)\n sp_cnt = char_cnt(s + p)\n (!subseq(s, t, s.size, t.size) or t_cnt.any? { |c, v| sp_cnt[c].nil? or sp_cnt[c] < v}) ? \"NO\" : \"YES\"\nend\n\ngets.strip.to_i.times do\n puts solve(gets.strip, gets.strip, gets.strip)\nend"}], "negative_code": [{"source_code": "def subseq(a, b, i, j)\n return true if i.zero?\n return false if j.zero?\n\n return subseq(a, b, i - 1, j - 1) if a[i-1] == b[j-1]\n return subseq(a, b, i, j - 1)\nend\n\ndef char_cnt(s)\n s.split('').inject({}) { |m, c| m[c] = (m[c] || 0) + 1; m }\nend\n\ndef solve(s, t, p)\n t_cnt = char_cnt(t)\n sp_cnt = char_cnt(s + p)\n (!subseq(s, t, s.size, t.size) or sp_cnt.any? { |c, v| t_cnt[c] and v < t_cnt[c] }) ? \"NO\" : \"YES\"\nend\n\ngets.strip.to_i.times do\n puts solve(gets.strip, gets.strip, gets.strip)\nend"}], "src_uid": "a27ad7c21cd6402bfd082da4f6c7ab9d"} {"nl": {"description": "Door's family is going celebrate Famil Doors's birthday party. They love Famil Door so they are planning to make his birthday cake weird!The cake is a n × n square consisting of equal squares with side length 1. Each square is either empty or consists of a single chocolate. They bought the cake and randomly started to put the chocolates on the cake. The value of Famil Door's happiness will be equal to the number of pairs of cells with chocolates that are in the same row or in the same column of the cake. Famil Doors's family is wondering what is the amount of happiness of Famil going to be?Please, note that any pair can be counted no more than once, as two different cells can't share both the same row and the same column.", "input_spec": "In the first line of the input, you are given a single integer n (1 ≤ n ≤ 100) — the length of the side of the cake. Then follow n lines, each containing n characters. Empty cells are denoted with '.', while cells that contain chocolates are denoted by 'C'.", "output_spec": "Print the value of Famil Door's happiness, i.e. the number of pairs of chocolate pieces that share the same row or the same column.", "sample_inputs": ["3\n.CC\nC..\nC.C", "4\nCC..\nC..C\n.CC.\n.CC."], "sample_outputs": ["4", "9"], "notes": "NoteIf we number rows from top to bottom and columns from left to right, then, pieces that share the same row in the first sample are: (1, 2) and (1, 3) (3, 1) and (3, 3) Pieces that share the same column are: (2, 1) and (3, 1) (1, 3) and (3, 3) "}, "positive_code": [{"source_code": "nn=gets.to_i\na = (1..nn).map{gets.strip}\n\ndef f(n)\n return 0 unless n\n return 0 if n < 2\n n == 2 ? 1 : (n-1)+f(n-1)\nend\n\n\ng1 = a.map{|x| n = x.gsub(/[^C]/, \"\").size; f(n)}\n#p a\na = (0...nn).map{|i| (0...nn).map{|j| a[j][i] }.join }\n#p a\ng2 = a.map{|x| n = x.gsub(/[^C]/, \"\").size; f(n)}\n#p g1, g2\ng = (g1+g2).reduce(:+)\np g\n"}, {"source_code": "#!/usr/bin/ruby\ngets;a=$<.map{|e|e.chomp.chars}\nr=0\n[a,a.transpose].each{|b|b.each{|e|n=e.count('C');r+=n*(n-1)/2}}\np r"}, {"source_code": "n = gets.chomp.to_i\nrows = []\nn.times { rows << gets.chomp.chars }\nc = 0\nrows.each { |row| c += (0..(row.count('C') - 1)).to_a.combination(2).to_a.length }\nrows.transpose.each { |row| c += (0..(row.count('C') - 1)).to_a.combination(2).to_a.length }\n\nputs c\n"}, {"source_code": "n = gets.to_i\ncake = []\nn.times do\n\tcake.push(gets.split(''))\nend\n\nt = 0\nfor i in 0...n\n\tchoc = 0\n\tfor j in 0...n\n\t\tchoc += 1 if cake[i][j] == 'C'\n\tend\n\tt += choc*(choc-1)/2\nend\n\nfor i in 0...n\n\tchoc = 0\n\tfor j in 0...n\n\t\tchoc += 1 if cake[j][i] == 'C'\n\tend\n\tt += choc*(choc-1)/2\nend\n\nputs t"}, {"source_code": "a=gets.chomp.to_i\nrow=[]\nclm=[]\na.times do \nclm <<0\nend\na.times do \ntemp=gets.chomp.split(\"\")\nt=0\n0.upto(a-1) do |i|\nif temp[i]==\"C\"\nt+=1\nclm[i]+=1\nend\nend\nrow << t\nend\nans=0\nrow.each do |i|\nif i>1\nans=ans+(i*(i-1))/2\nend\nend\n\nclm.each do |i|\nif i>1\nans=ans+(i*(i-1))/2\nend\nend\nputs \"#{ans}\"\n"}, {"source_code": "def combination_count(n, m)\n return 1 if n == m\n (1..n).inject(:*)/((1..m).inject(:*) * (1..n-m).inject(:*))\nend\n \nn = gets.to_i\ncake = []\nn.times do\n cake << gets.chomp.chars\nend\n \ncake_t = cake.transpose\n \nans = 0\n(0..n-1).each do |i|\n if (c = cake[i].count('C')) >= 2\n ans += combination_count(c, 2)\n end\n if (c = cake_t[i].count('C')) >= 2\n ans += combination_count(c, 2)\n end\nend\n \nputs ans"}, {"source_code": "a=gets.chomp.to_i\nrow=[]\nclm=[]\na.times do \nclm <<0\nend\na.times do \ntemp=gets.chomp.split(\"\")\nt=0\n0.upto(a-1) do |i|\nif temp[i]==\"C\"\nt+=1\nclm[i]+=1\nend\nend\nrow << t\nend\nans=0\nrow.each do |i|\nif i>1\nans=ans+(i*(i-1))/2\nend\nend\n\nclm.each do |i|\nif i>1\nans=ans+(i*(i-1))/2\nend\nend\nputs \"#{ans}\""}, {"source_code": "n=gets.to_i\ns=[]\nn.times{\n\ts<1\nans=ans+(i*(i-1))/2\nend\nend\n\nclm.each do |i|\nif i>1\nans=ans+(i*(i-1))/2\nend\nend\nputs \"#{ans}\""}, {"source_code": "#!/usr/bin/ruby\ngets;a=$<.map{|e|e.chomp.chars}\nr=0\n[a,a.transpose].each{|b|b.each{|e|n=e.count('C');r+=n*(n-1)/2}}\np r"}, {"source_code": "n = gets.to_i\na = []\nn.times { a << gets.chomp.split('') }\nb = a.transpose\njoy = 0\n(0...n).each do |i|\n c = a[i].count('C') - 1\n joy += (1 + c)*c/2\n c = b[i].count('C') - 1\n joy += (1 + c)*c/2\nend\nputs joy\n"}, {"source_code": "#!/usr/bin/ruby\ngets;a=$<.map{|e|e.chomp.chars}\nr=0\n[a,a.transpose].each{|b|b.each{|e|n=e.count('C');r+=n*(n-1)/2}}\np r\n"}, {"source_code": "#!/usr/bin/ruby\ngets;a=$<.map{|e|e.chomp.chars}\nr=0\n[a,a.transpose].each{|b|b.each{|e|n=e.count('C');r+=n*(n-1)/2}}\np r\n"}], "negative_code": [{"source_code": "def combination_count(n, m)\n (1..n).inject(:*)/(1..m).inject(:*)\nend\n\nn = gets.to_i\ncake = []\nn.times do\n cake << gets.chomp.chars\nend\n\ncake_t = cake.transpose\n\nans = 0\n(0..n-1).each do |i|\n if (c = cake[i].count('C')) > 0\n ans += combination_count(c, 2)\n end\n if (c = cake_t[i].count('C')) > 0\n ans += combination_count(c, 2)\n end\nend\n\nputs ans\n\n"}], "src_uid": "7749f37aa9bf88a00013557e14342952"} {"nl": {"description": "In a dream Marco met an elderly man with a pair of black glasses. The man told him the key to immortality and then disappeared with the wind of time.When he woke up, he only remembered that the key was a sequence of positive integers of some length n, but forgot the exact sequence. Let the elements of the sequence be a1, a2, ..., an. He remembered that he calculated gcd(ai, ai + 1, ..., aj) for every 1 ≤ i ≤ j ≤ n and put it into a set S. gcd here means the greatest common divisor.Note that even if a number is put into the set S twice or more, it only appears once in the set.Now Marco gives you the set S and asks you to help him figure out the initial sequence. If there are many solutions, print any of them. It is also possible that there are no sequences that produce the set S, in this case print -1.", "input_spec": "The first line contains a single integer m (1 ≤ m ≤ 1000) — the size of the set S. The second line contains m integers s1, s2, ..., sm (1 ≤ si ≤ 106) — the elements of the set S. It's guaranteed that the elements of the set are given in strictly increasing order, that means s1 < s2 < ... < sm.", "output_spec": "If there is no solution, print a single line containing -1. Otherwise, in the first line print a single integer n denoting the length of the sequence, n should not exceed 4000. In the second line print n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the sequence. We can show that if a solution exists, then there is a solution with n not exceeding 4000 and ai not exceeding 106. If there are multiple solutions, print any of them.", "sample_inputs": ["4\n2 4 6 12", "2\n2 3"], "sample_outputs": ["3\n4 6 12", "-1"], "notes": "NoteIn the first example 2 = gcd(4, 6), the other elements from the set appear in the sequence, and we can show that there are no values different from 2, 4, 6 and 12 among gcd(ai, ai + 1, ..., aj) for every 1 ≤ i ≤ j ≤ n."}, "positive_code": [{"source_code": "n = gets.to_i\na = gets.split.map &:to_i\nif a.any?{|i|i % a[0] > 0} then p -1 else puts 2 * n - 1, a * (\" %d \" % a[0]) end"}, {"source_code": "n = gets.to_i\na = gets.split.map &:to_i\nif a.any?{|i|i % a[0] > 0} then p -1 else puts 2 * n - 1, a * (\" %d \" % a[0]) end"}], "negative_code": [], "src_uid": "dfe5af743c9e23e98e6c9c5c319d2126"} {"nl": {"description": "There are $$$n$$$ districts in the town, the $$$i$$$-th district belongs to the $$$a_i$$$-th bandit gang. Initially, no districts are connected to each other.You are the mayor of the city and want to build $$$n-1$$$ two-way roads to connect all districts (two districts can be connected directly or through other connected districts).If two districts belonging to the same gang are connected directly with a road, this gang will revolt.You don't want this so your task is to build $$$n-1$$$ two-way roads in such a way that all districts are reachable from each other (possibly, using intermediate districts) and each pair of directly connected districts belong to different gangs, or determine that it is impossible to build $$$n-1$$$ roads to satisfy all the conditions.You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 500$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$2 \\le n \\le 5000$$$) — the number of districts. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$), where $$$a_i$$$ is the gang the $$$i$$$-th district belongs to. It is guaranteed that the sum of $$$n$$$ does not exceed $$$5000$$$ ($$$\\sum n \\le 5000$$$).", "output_spec": "For each test case, print: NO on the only line if it is impossible to connect all districts satisfying the conditions from the problem statement. YES on the first line and $$$n-1$$$ roads on the next $$$n-1$$$ lines. Each road should be presented as a pair of integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \\le x_i, y_i \\le n; x_i \\ne y_i$$$), where $$$x_i$$$ and $$$y_i$$$ are two districts the $$$i$$$-th road connects. For each road $$$i$$$, the condition $$$a[x_i] \\ne a[y_i]$$$ should be satisfied. Also, all districts should be reachable from each other (possibly, using intermediate districts).", "sample_inputs": ["4\n5\n1 2 2 1 3\n3\n1 1 1\n4\n1 1000 101 1000\n4\n1 2 3 4"], "sample_outputs": ["YES\n1 3\n3 5\n5 4\n1 2\nNO\nYES\n1 2\n2 3\n3 4\nYES\n1 2\n1 3\n1 4"], "notes": null}, "positive_code": [{"source_code": "T = gets.to_i\n\nT.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n au = a.uniq\n\n if au.size == 1\n puts 'NO'\n else\n v1 = a.index(au[0])\n v2 = a.index(au[1])\n edges = []\n edges << [v1, v2]\n\n a.each.with_index do |x, i|\n next if i == v1\n next if i == v2\n\n if x == a[v1]\n edges << [v2, i]\n else\n edges << [v1, i]\n end\n end\n\n puts 'YES'\n edges.each do |u, v|\n puts \"#{u + 1} #{v + 1}\"\n end\n end\nend\n"}, {"source_code": "gets.to_i.times do\n n = gets.to_i\n v = gets.split.map(&:to_i)\n p = (0...n).sort { |i, j| v[i] <=> v[j] }\n w = p.chunk { |i| v[i] }.map(&:last)\n if w.length == 1\n puts 'NO'\n else\n puts 'YES'\n w.drop(1).flatten.each do |x|\n puts \"#{p[0] + 1} #{x + 1}\"\n end\n w[0].drop(1).each do |x|\n puts \"#{w[1][0] + 1} #{x + 1}\"\n end\n end\nend\n"}, {"source_code": "t = gets.to_i\nans = []\nt.times do\n n = gets.to_i\n a = gets.chomp.split(\" \").map(&:to_i)\n hash = Hash.new(0)\n max = 0\n a.each do |v|\n hash[v] += 1\n if max < hash[v]\n max = hash[v]\n end\n end\n if max > n-1\n ans << \"NO\"\n else\n ans << \"YES\"\n one = 1\n two = 0\n for i in 1..n-1\n next if a[i] == a[0]\n two = i+1\n break\n end\n ans << \"#{one} #{two}\"\n for i in 1..n-1\n next if i+1 == two\n if a[i] != a[one-1]\n ans << \"#{one} #{i+1}\"\n else\n ans << \"#{two} #{i+1}\"\n end\n end\n end\nend\n\nputs ans"}, {"source_code": "gets.to_i.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n next puts 'NO' if a.count(a[0]) == n\n puts 'YES'\n i = 0\n j = a.index { |x| x != a[i] }\n puts \"#{i + 1} #{j + 1}\"\n n.times do |k|\n if i != k and j != k\n if a[i] == a[k]\n puts \"#{j + 1} #{k + 1}\"\n else\n puts \"#{i + 1} #{k + 1}\"\n end\n end\n end\nend\n"}, {"source_code": "def solve(n, a)\n result = []\n first = a.first\n second = -1\n a.each_with_index do |element, i|\n second = 1+i if element!=first\n end\n if second!=-1\n 1.upto(n-1) do |i|\n if(a[i]!=first)\n result.push([1,i+1])\n else\n result.push([second, i+1])\n end\n end\n end\n result\nend\n\n\n(gets.to_i).times do\n n = gets.to_i\n a = gets.split(' ').map(&:to_i)\n b = solve(n,a)\n if(b.length == 0)\n puts \"NO\"\n else\n puts \"YES\"\n b.each do |i|\n puts \"#{i[0]} #{i[1]}\"\n end\n end\nend\n"}], "negative_code": [{"source_code": "t = gets.to_i\nans = []\nt.times do\n n = gets.to_i\n a = gets.chomp.split(\" \").map(&:to_i)\n hash = Hash.new(0)\n max = 0\n city = 0\n a.each do |v|\n hash[v] += 1\n if max < hash[v]\n max = hash[v]\n city = v\n end\n end\n if max > n/2\n ans << \"NO\"\n else\n ans << \"YES\"\n pre = 0\n dist = Array.new(n+1,0)\n arr = Array.new(n,0)\n e = []\n n.times do |i|\n if a[i] == city\n e << i+1\n dist[i+1] = 1\n end\n end\n max.times do |i|\n arr[i*2] = e[i]\n end\n pre = 0\n for i in (max*2-1)..n-1\n for j in 0..n-1\n if a[j] != pre && dist[j+1] == 0\n arr[i] = j+1\n dist[j+1] = 1\n pre = a[j]\n break\n end\n end\n end\n (max-1).times do |i|\n for j in 0..n-1\n if dist[j+1] == 0\n arr[i*2+1] = j+1\n dist[j+1] = 1\n break\n end\n end\n end\n (n-1).times do |i|\n ans << \"#{arr[i]} #{arr[i+1]}\"\n end\n end\nend\n\nputs ans"}, {"source_code": "t = gets.to_i\nans = []\nt.times do\n n = gets.to_i\n a = gets.chomp.split(\" \").map(&:to_i)\n hash = Hash.new(0)\n max = 0\n a.each do |v|\n hash[v] += 1\n if max < hash[v]\n max = hash[v]\n end\n end\n if max > (n+1)/2\n ans << \"NO\"\n else\n ans << \"YES\"\n one = 1\n two = 0\n for i in 1..n-1\n next if a[i] == a[0]\n two = i+1\n break\n end\n ans << \"#{one} #{two}\"\n for i in 1..n-1\n next if i+1 == two\n if a[i] != one\n ans << \"#{one} #{i+1}\"\n else\n ans << \"#{two} #{i+1}\"\n end\n end\n end\nend\n\nputs ans"}, {"source_code": "t = gets.to_i\nans = []\nt.times do\n n = gets.to_i\n a = gets.chomp.split(\" \").map(&:to_i)\n hash = Hash.new(0)\n max = 0\n a.each do |v|\n hash[v] += 1\n if max < hash[v]\n max = hash[v]\n end\n end\n if max > n-1\n ans << \"NO\"\n else\n ans << \"YES\"\n one = 1\n two = 0\n for i in 1..n-1\n next if a[i] == a[0]\n two = i+1\n break\n end\n ans << \"#{one} #{two}\"\n for i in 1..n-1\n next if i+1 == two\n if a[i] != one\n ans << \"#{one} #{i+1}\"\n else\n ans << \"#{two} #{i+1}\"\n end\n end\n end\nend\n\nputs ans"}, {"source_code": "t = gets.to_i\nans = []\nt.times do\n n = gets.to_i\n a = gets.chomp.split(\" \").map(&:to_i)\n hash = Hash.new(0)\n max = 0\n a.each do |v|\n hash[v] += 1\n if max < hash[v]\n max = hash[v]\n end\n end\n if max > n-1\n ans << \"NO\"\n else\n ans << \"YES\"\n one = 1\n two = 0\n for i in 1..n-1\n next if a[i] == a[0]\n two = i+1\n break\n end\n ans << \"#{one} #{two}\"\n for i in 1..n-1\n next if i+1 == two\n if a[i+1] != one\n ans << \"#{one} #{i+1}\"\n else\n ans << \"#{two} #{i+1}\"\n end\n end\n end\nend\n\nputs ans"}, {"source_code": "t = gets.to_i\nans = []\nt.times do\n n = gets.to_i\n a = gets.chomp.split(\" \").map(&:to_i)\n hash = Hash.new(0)\n max = 0\n city = 0\n a.each do |v|\n hash[v] += 1\n if max < hash[v]\n max = hash[v]\n city = v\n end\n end\n if max > (n+1)/2\n ans << \"NO\"\n else\n ans << \"YES\"\n pre = 0\n dist = Array.new(n+1,0)\n arr = Array.new(n,0)\n e = []\n n.times do |i|\n if a[i] == city\n e << i+1\n dist[i+1] = 1\n end\n end\n max.times do |i|\n arr[i*2] = e[i]\n end\n pre = 0\n for i in (max*2-1)..n-1\n for j in 0..n-1\n if a[j] != pre && dist[j+1] == 0\n arr[i] = j+1\n dist[j+1] = 1\n pre = a[j]\n break\n end\n end\n end\n (max-1).times do |i|\n for j in 0..n-1\n if dist[j+1] == 0\n arr[i*2+1] = j+1\n dist[j+1] = 1\n break\n end\n end\n end\n (n-1).times do |i|\n ans << \"#{arr[i]} #{arr[i+1]}\"\n end\n end\nend\n\nputs ans"}], "src_uid": "d8136eb72931851f501c5ce9042ce4eb"} {"nl": {"description": " As some of you know, cubism is a trend in art, where the problem of constructing volumetrical shape on a plane with a combination of three-dimensional geometric shapes comes to the fore. A famous sculptor Cicasso, whose self-portrait you can contemplate, hates cubism. He is more impressed by the idea to transmit two-dimensional objects through three-dimensional objects by using his magnificent sculptures. And his new project is connected with this. Cicasso wants to make a coat for the haters of anticubism. To do this, he wants to create a sculpture depicting a well-known geometric primitive — convex polygon.Cicasso prepared for this a few blanks, which are rods with integer lengths, and now he wants to bring them together. The i-th rod is a segment of length li.The sculptor plans to make a convex polygon with a nonzero area, using all rods he has as its sides. Each rod should be used as a side to its full length. It is forbidden to cut, break or bend rods. However, two sides may form a straight angle .Cicasso knows that it is impossible to make a convex polygon with a nonzero area out of the rods with the lengths which he had chosen. Cicasso does not want to leave the unused rods, so the sculptor decides to make another rod-blank with an integer length so that his problem is solvable. Of course, he wants to make it as short as possible, because the materials are expensive, and it is improper deed to spend money for nothing. Help sculptor! ", "input_spec": "The first line contains an integer n (3 ≤ n ≤ 105) — a number of rod-blanks. The second line contains n integers li (1 ≤ li ≤ 109) — lengths of rods, which Cicasso already has. It is guaranteed that it is impossible to make a polygon with n vertices and nonzero area using the rods Cicasso already has.", "output_spec": "Print the only integer z — the minimum length of the rod, so that after adding it it can be possible to construct convex polygon with (n + 1) vertices and nonzero area from all of the rods.", "sample_inputs": ["3\n1 2 1", "5\n20 4 3 2 1"], "sample_outputs": ["1", "11"], "notes": "NoteIn the first example triangle with sides {1 + 1 = 2, 2, 1} can be formed from a set of lengths {1, 1, 1, 2}. In the second example you can make a triangle with lengths {20, 11, 4 + 3 + 2 + 1 = 10}. "}, "positive_code": [{"source_code": "n = readline.to_i\narr = readline.split(' ').collect(&:to_i).sort.reverse\nputs 1 + arr[0] - arr[1..-1].inject(0, :+)\n"}, {"source_code": "def solve(a)\n 2*a.max - a.inject(:+) + 1\nend\n\ngets\n\nputs solve(gets.split.map(&:to_i))"}, {"source_code": "n = gets.to_i\nl = gets.split.map { |x| x.to_i }\n\nl.sort!\nmax = l.pop\nsum = l.reduce(:+)\n\nputs [max - sum + 1, 0].max\n"}, {"source_code": "lodNum = gets.chomp.to_i\ninput = gets.chomp.split(' ').map {|i| i.to_i}\ninput.sort!\n\nsum = 0\nflag = false\nmax = input[input.length - 1]\nfor i in (0...(input.length - 1))\n\tsum += input[i]\nend\n\nif !flag && sum > max\n\tputs '0'\nelse\n\tputs (max - sum + 1)\nend"}, {"source_code": "n = gets\n\nparse_int = lambda { gets.split.map{|x| x.to_i} }\n\nm = parse_int.call\n\nm.sort!\n\nlast = m.pop\nsum = 0\nm.each { |a| sum+=a }\nputs last-sum+1\n"}], "negative_code": [], "src_uid": "f00d94eb37c98a449615f0411e5a3572"} {"nl": {"description": "You are given a string $$$s$$$ consisting of the characters 0, 1, and ?.Let's call a string unstable if it consists of the characters 0 and 1 and any two adjacent characters are different (i. e. it has the form 010101... or 101010...).Let's call a string beautiful if it consists of the characters 0, 1, and ?, and you can replace the characters ? to 0 or 1 (for each character, the choice is independent), so that the string becomes unstable.For example, the strings 0??10, 0, and ??? are beautiful, and the strings 00 and ?1??1 are not.Calculate the number of beautiful contiguous substrings of the string $$$s$$$.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — number of test cases. The first and only line of each test case contains the string $$$s$$$ ($$$1 \\le |s| \\le 2 \\cdot 10^5$$$) consisting of characters 0, 1, and ?. It is guaranteed that the sum of the string lengths over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, output a single integer — the number of beautiful substrings of the string $$$s$$$.", "sample_inputs": ["3\n0?10\n???\n?10??1100"], "sample_outputs": ["8\n6\n25"], "notes": null}, "positive_code": [{"source_code": "gets.to_s.to_i.times do\n a = gets.to_s.strip.split(\"\").map {|x| case x when \"?\" then 3 when \"0\" then 1 else 2 end }\n dp = a.size.times.map {[0.0, 0.0]}.to_a\n dp[0][0] = ((a[0] & 1) != 0) ? 1.0 : 0.0\n dp[0][1] = ((a[0] & 2) != 0) ? 1.0 : 0.0\n 1.upto(a.size - 1) do |i|\n dp[i][0] = ((a[i] & 1) != 0) ? dp[i - 1][1] + 1.0 : 0.0\n dp[i][1] = ((a[i] & 2) != 0) ? dp[i - 1][0] + 1.0 : 0.0\n end\n printf(\"%d\\n\", dp.reduce(0.0) {|a, b| a + b.max })\nend\n"}], "negative_code": [], "src_uid": "639eeeabcb005152035a1fcf09ed3b44"} {"nl": {"description": "You are given a set of $$$2n+1$$$ integer points on a Cartesian plane. Points are numbered from $$$0$$$ to $$$2n$$$ inclusive. Let $$$P_i$$$ be the $$$i$$$-th point. The $$$x$$$-coordinate of the point $$$P_i$$$ equals $$$i$$$. The $$$y$$$-coordinate of the point $$$P_i$$$ equals zero (initially). Thus, initially $$$P_i=(i,0)$$$.The given points are vertices of a plot of a piecewise function. The $$$j$$$-th piece of the function is the segment $$$P_{j}P_{j + 1}$$$.In one move you can increase the $$$y$$$-coordinate of any point with odd $$$x$$$-coordinate (i.e. such points are $$$P_1, P_3, \\dots, P_{2n-1}$$$) by $$$1$$$. Note that the corresponding segments also change.For example, the following plot shows a function for $$$n=3$$$ (i.e. number of points is $$$2\\cdot3+1=7$$$) in which we increased the $$$y$$$-coordinate of the point $$$P_1$$$ three times and $$$y$$$-coordinate of the point $$$P_5$$$ one time: Let the area of the plot be the area below this plot and above the coordinate axis OX. For example, the area of the plot on the picture above is 4 (the light blue area on the picture above is the area of the plot drawn on it).Let the height of the plot be the maximum $$$y$$$-coordinate among all initial points in the plot (i.e. points $$$P_0, P_1, \\dots, P_{2n}$$$). The height of the plot on the picture above is 3.Your problem is to say which minimum possible height can have the plot consisting of $$$2n+1$$$ vertices and having an area equal to $$$k$$$. Note that it is unnecessary to minimize the number of moves.It is easy to see that any answer which can be obtained by performing moves described above always exists and is an integer number not exceeding $$$10^{18}$$$.", "input_spec": "The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le n, k \\le 10^{18}$$$) — the number of vertices in a plot of a piecewise function and the area we need to obtain.", "output_spec": "Print one integer — the minimum possible height of a plot consisting of $$$2n+1$$$ vertices and with an area equals $$$k$$$. It is easy to see that any answer which can be obtained by performing moves described above always exists and is an integer number not exceeding $$$10^{18}$$$.", "sample_inputs": ["4 3", "4 12", "999999999999999999 999999999999999986"], "sample_outputs": ["1", "3", "1"], "notes": "NoteOne of the possible answers to the first example: The area of this plot is 3, the height of this plot is 1.There is only one possible answer to the second example: The area of this plot is 12, the height of this plot is 3."}, "positive_code": [{"source_code": "n,k=gets.split.map &:to_i;p (k+n-1)/n"}, {"source_code": "\ndef solution\n n, k = read_ints\n s = k/n\n s += 1 if s*n < k\n puts s\nend\n\ndef read_int\n gets.to_i\nend\n\ndef read_ints\n gets.split.map(&:to_i)\nend\n\ndef read_string\n gets.chomp\nend\n\nsolution unless ENV['TEST__MODE']\n\n"}, {"source_code": "N, K = gets.split.map(&:to_i)\nputs (K-1)/N+1"}, {"source_code": "n,k=gets.strip.split.map(&:to_i)\nans=0\nans += (k/n)\nans += 1 if k%n != 0\nputs ans"}, {"source_code": "# T = gets.chomp.to_i\n# (0..T-1).map do |i|\n# N = gets.chomp.to_i\n# pN = (N/2).ceil\n# b = gets.chomp\n# max_bs = []\n# (0..(b.length - pN + 1)).map do |j|\n# sub_b = b[j..(j + pN - 1)]\n# sum_b = sub_b.split('').inject 0 do |s, e|\n# s += e.to_i\n# end\n# max_bs << sum_b\n# end\n# p \"Case ##{i + 1}: #{max_bs.max}\"\n# end\n\nn, k = gets.chomp.split \" \"\nn = n.to_i\nk = k.to_i\np (k.to_r / n).ceil"}, {"source_code": "n, k = gets.split.map(&:to_i)\n\nans = k / n\nif k % n != 0\n ans += 1\nend\n\nputs ans"}, {"source_code": "n,k=gets.split.map &:to_i;p (k+n-1)/n"}, {"source_code": "n,k = gets.split.map(&:to_i)\nputs (k+n-1)/n\n"}, {"source_code": "n, k = gets.split.map &:to_i\nprint (k+n-1)/n"}], "negative_code": [{"source_code": "\n\nn, k = gets.chomp.split.map(&:to_i)\nputs (n.to_f/k).ceil\n"}, {"source_code": "\ndef solution\n n, k = read_ints\n s = k/n\n s += 1 if s*k < n\n puts s\nend\n\ndef read_int\n gets.to_i\nend\n\ndef read_ints\n gets.split.map(&:to_i)\nend\n\ndef read_string\n gets.chomp\nend\n\nsolution unless ENV['TEST__MODE']\n\n"}, {"source_code": "\n\nn, k = gets.chomp.split.map(&:to_i)\nputs (k.to_f/n).ceil"}, {"source_code": "# T = gets.chomp.to_i\n# (0..T-1).map do |i|\n# N = gets.chomp.to_i\n# pN = (N/2).ceil\n# b = gets.chomp\n# max_bs = []\n# (0..(b.length - pN + 1)).map do |j|\n# sub_b = b[j..(j + pN - 1)]\n# sum_b = sub_b.split('').inject 0 do |s, e|\n# s += e.to_i\n# end\n# max_bs << sum_b\n# end\n# p \"Case ##{i + 1}: #{max_bs.max}\"\n# end\n\nn, k = gets.chomp.split \" \"\nn = n.to_i\nk = k.to_i\np (k.to_f / n).ceil"}], "src_uid": "31014efa929af5e4b7d9987bd9b59918"} {"nl": {"description": "Petya got interested in grammar on his third year in school. He invented his own language called Petya's. Petya wanted to create a maximally simple language that would be enough to chat with friends, that's why all the language's grammar can be described with the following set of rules: There are three parts of speech: the adjective, the noun, the verb. Each word in his language is an adjective, noun or verb. There are two genders: masculine and feminine. Each word in his language has gender either masculine or feminine. Masculine adjectives end with -lios, and feminine adjectives end with -liala. Masculine nouns end with -etr, and feminime nouns end with -etra. Masculine verbs end with -initis, and feminime verbs end with -inites. Thus, each word in the Petya's language has one of the six endings, given above. There are no other endings in Petya's language. It is accepted that the whole word consists of an ending. That is, words \"lios\", \"liala\", \"etr\" and so on belong to the Petya's language. There aren't any punctuation marks, grammatical tenses, singular/plural forms or other language complications. A sentence is either exactly one valid language word or exactly one statement. Statement is any sequence of the Petya's language, that satisfy both conditions: Words in statement follow in the following order (from the left to the right): zero or more adjectives followed by exactly one noun followed by zero or more verbs. All words in the statement should have the same gender.After Petya's friend Vasya wrote instant messenger (an instant messaging program) that supported the Petya's language, Petya wanted to add spelling and grammar checking to the program. As Vasya was in the country and Petya didn't feel like waiting, he asked you to help him with this problem. Your task is to define by a given sequence of words, whether it is true that the given text represents exactly one sentence in Petya's language.", "input_spec": "The first line contains one or more words consisting of lowercase Latin letters. The overall number of characters (including letters and spaces) does not exceed 105. It is guaranteed that any two consecutive words are separated by exactly one space and the input data do not contain any other spaces. It is possible that given words do not belong to the Petya's language.", "output_spec": "If some word of the given text does not belong to the Petya's language or if the text contains more that one sentence, print \"NO\" (without the quotes). Otherwise, print \"YES\" (without the quotes).", "sample_inputs": ["petr", "etis atis animatis etis atis amatis", "nataliala kataliala vetra feinites"], "sample_outputs": ["YES", "NO", "YES"], "notes": null}, "positive_code": [{"source_code": "$_ = gets.chomp + ' '\n[%w(lios etr initis), %w(liala etra inites)].each do |r|\n\tr.map!{|_| \"\\\\S*#{_}\\\\s+\"}\n\tr << \"(#{r[0]})*(#{r[1]})(#{r[2]})*\";\n\tif /^(#{r.join('|')})$/\n\t\tputs :YES\n\t\texit\n\tend\nend\nputs :NO\n"}, {"source_code": "$_ = gets.chomp + ' '\n[%w(lios etr initis), %w(liala etra inites)].each do |r|\n\tr.map!{|_| \"\\\\S*#{_}\\\\s+\"}\n\tr << \"(#{r[0]})*(#{r[1]})(#{r[2]})*\";\n\tif /^(#{r.join('|')})$/\n\t\tputs :YES\n\t\texit\n\tend\nend\nputs :NO\n\n"}, {"source_code": "=begin\n , \\ / ,\n / \\ )\\__/( / \\\n / \\ (_\\ /_) / \\\n ____/_____\\__\\@ @/___/_____\\____\n | |\\../| |\n | \\VV/ |\n | ------hoi------- |\n |_________________________________|\n | /\\ / \\\\ \\ /\\ |\n | / V )) V \\ |\n |/ ` // ' \\|\n ` V '\n=end\n$_ = gets.chomp + ' '\n[%w(lios etr initis), %w(liala etra inites)].each do |r|\n\tr.map!{|_| \"\\\\S*#{_}\\\\s+\"}\n\tr << \"(#{r[0]})*(#{r[1]})(#{r[2]})*\";\n\tif /^(#{r.join('|')})$/\n\t\tputs :YES\n\t\texit\n\tend\nend\nputs :NO"}, {"source_code": "$_ = gets.chomp + ' '\n[%w(lios etr initis), %w(liala etra inites)].each do |r|\n\tr.map!{|_| \"\\\\S*#{_}\\\\s+\"}\n\tr << \"(#{r[0]})*(#{r[1]})(#{r[2]})*\";\n\tif /^(#{r.join('|')})$/\n\t\tputs :YES\n\t\texit\n\tend\nend\nputs :NO\n\n"}, {"source_code": "$_ = gets.chomp + ' '\n[%w(lios etr initis), %w(liala etra inites)].each do |r|\n\tr.map!{|_| \"\\\\S*#{_}\\\\s+\"}\n\tr << \"(#{r[0]})*(#{r[1]})(#{r[2]})*\";\n\tif /^(#{r.join('|')})$/\n\t\tputs :YES\n\t\texit\n\tend\nend\nputs :NO\n"}, {"source_code": "$_ = gets.chomp + ' '\n[%w(lios etr initis), %w(liala etra inites)].each do |r|\n\tr.map!{|_| \"\\\\S*#{_}\\\\s+\"}\n\tr << \"(#{r[0]})*(#{r[1]})(#{r[2]})*\";\n\tif /^(#{r.join('|')})$/\n\t\tputs :YES\n\t\texit\n\tend\nend\nputs :NO\n\n"}, {"source_code": "$_ = gets.chomp + ' '\n[%w(lios etr initis), %w(liala etra inites)].each do |r|\n\tr.map!{|_| \"\\\\S*#{_}\\\\s+\"}\n\tr << \"(#{r[0]})*(#{r[1]})(#{r[2]})*\";\n\tif /^(#{r.join('|')})$/\n\t\tputs :YES\n\t\texit\n\tend\nend\nputs :NO\n\n"}, {"source_code": "$_ = gets.chomp + ' '\n[%w(lios etr initis), %w(liala etra inites)].each do |r|\n r.map!{|_| \"\\\\S*#{_}\\\\s+\"}\n r << \"(#{r[0]})*(#{r[1]})(#{r[2]})*\";\n if /^(#{r.join('|')})$/\n puts :YES\n exit\n end\nend\nputs :NO"}, {"source_code": "s = gets.strip\n[%w(lios etr initis), %w(liala etra inites)].each do |g|\n r = g.map { |t| \"\\\\S*#{t}(\\\\s+|$)\" }\n r << \"(#{r[0]})*#{r[1]}(#{r[2]})*\"\n if /^(#{r.join ?|})$/.match s\n puts :YES\n exit\n end\nend\nputs :NO\n"}, {"source_code": "$_ = gets.chomp + ' '\n[%w(lios etr initis), %w(liala etra inites)].each do |r|\n\tr.map!{|_| \"\\\\S*#{_}\\\\s+\"}\n\tr << \"(#{r[0]})*(#{r[1]})(#{r[2]})*\";\n\tif /^(#{r.join('|')})$/\n\t\tputs :YES\n\t\texit\n\tend\nend\nputs :NO\n\n"}, {"source_code": "$_ = gets.chomp + ' '\n[%w(lios etr initis), %w(liala etra inites)].each do |r|\n\tr.map!{|_| \"\\\\S*#{_}\\\\s+\"}\n\tr << \"(#{r[0]})*(#{r[1]})(#{r[2]})*\";\n\tif /^(#{r.join('|')})$/\n\t\tputs :YES\n\t\texit\n\tend\nend\nputs :NO\n\n"}, {"source_code": "def solve(ws)\n ts = ws.map do |w|\n case\n when w.end_with?('lios') then [:adj, :mas]\n when w.end_with?('liala') then [:adj, :fem]\n when w.end_with?('etr') then [:noun, :mas]\n when w.end_with?('etra') then [:noun, :fem]\n when w.end_with?('initis') then [:verb, :mas]\n when w.end_with?('inites') then [:verb, :fem]\n when true then nil\n end\n end\n return false if ts.any?(&:nil?)\n return true if ts.size == 1\n return false unless ts.all?{|x| x[1] == ts[0][1]}\n cb = :adj\n ts.each do |t, _|\n case cb\n when :adj\n case t\n when :noun then cb=:noun\n when :verb then return false\n end\n when :noun,:verb\n case t\n when :adj, :noun then return false\n when :verb then cb=:verb\n end\n end\n end\n return cb == :noun || cb == :verb\nend\n\nwords = gets.split\nputs solve(words) ? 'YES' : 'NO'\n"}, {"source_code": "$_ = gets.chomp + ' '\n[%w(lios etr initis), %w(liala etra inites)].each do |r|\n\tr.map!{|_| \"\\\\S*#{_}\\\\s+\"}\n\tr << \"(#{r[0]})*(#{r[1]})(#{r[2]})*\";\n\tif /^(#{r.join('|')})$/\n\t\tputs :YES\n\t\texit\n\tend\nend\nputs :NO\n\n"}, {"source_code": "$_ = gets.chomp + ' '\n[%w(lios etr initis), %w(liala etra inites)].each do |r|\n\tr.map!{|_| \"\\\\S*#{_}\\\\s+\"}\n\tr << \"(#{r[0]})*(#{r[1]})(#{r[2]})*\";\n\tif /^(#{r.join('|')})$/\n\t\tputs :YES\n\t\texit\n\tend\nend\nputs :NO\n\n"}, {"source_code": "\nclass String\n def end_with(s)\n i = self.rindex s\n !i.nil? && self.length - i == s.length\n end\nend\n\n$w = [\n %w(lios ma),\n %w(liala fa),\n %w(etr mn),\n %w(etra fn),\n %w(initis mv),\n %w(inites fv),\n ]\ndef f(str)\n $w.each {|suf, s|\n return s if str.end_with suf\n }\n nil\nend\n\ndef valid(words)\n return false if words.index nil\n return true if words.length == 1\n\n return false if words.map{|w|w[0]}.uniq.length > 1\n a = words.map{ |w| w[1] }\n i = 0\n i += 1 while i= words.count then\n puts \"NO\"\n exit\n end\nend\n\nunless words[i][:type] == :noun\n puts \"NO\"\n exit\nend\ni += 1\nif i >= words.count then\n puts \"YES\"\n exit\nend\n\nwhile i < words.count\n if words[i][:type] != :verb then\n puts \"NO\"\n exit\n end \n i += 1\nend\n\nputs \"YES\"\n"}, {"source_code": "def g(s)\n case s\n when /lios$/\n return 1, 0\n when /liala$/\n return 1, 1\n when /etr$/\n return 2, 0\n when /etra$/\n return 2, 1\n when /initis$/\n return 3, 0\n when /inites$/\n return 3, 1\n else\n return -1, -1\n end\nend\n\ndef v(words)\n if words.size == 0\n return false\n end\n if words.size == 1\n return words[0][0] >= 0\n end\n last_type = -1\n c = 0\n words.each { |w|\n return false if w[0] < 0\n return false if w[1] != words[0][1]\n c += 1 if w[0] == 2\n return false if w[0] < last_type\n last_type = w[0]\n }\n return c == 1\nend\n\nwords = gets.split\nif v(words.map { |s| g(s) })\n puts 'YES'\nelse\n puts 'NO'\nend\n"}, {"source_code": "def g(s)\n case s\n when /lios$/\n return 1, 0\n when /liala$/\n return 1, 1\n when /etr$/\n return 2, 0\n when /etra$/\n return 2, 1\n when /initis$/\n return 3, 0\n when /inites$/\n return 3, 1\n else\n return -1, -1\n end\nend\n\ndef v(words)\n return false if words.size==0\n return words[0][0] >= 0 if words.size==1\n l=-1\n c=0\n words.each { |w|\n return false if w[0]<0\n return false if w[1]!=words[0][1]\n c+=1 if w[0]==2\n return false if w[0]= 0\n end\n last_type = -1\n count_noun = 0\n words.each { |word|\n # Must be valid word\n if word[0] < 0\n return false\n end\n # Must have same gender\n if word[1] != words[0][1]\n return false\n end\n # Must be increasing in type\n if word[0] == 2\n count_noun += 1\n end\n if word[0] < last_type\n return false\n end\n last_type = word[0]\n }\n return count_noun == 1\nend\n\n[*$<].each { |line|\n words = line.split\n if verify(words.map { |s| get_info(s) })\n puts 'YES'\n else\n puts 'NO'\n end\n}\n\n"}, {"source_code": "words = gets.split\n\nif words.size == 1 and words.first =~ /(etr|etra|lios|liala|inites|initis)$/\n puts \"YES\"\nelsif words.size == 1\n puts \"NO\"\nelse\n first = words.first\n if first =~ /(lios|etr)$/\n gender = :male\n elsif first =~ /(liala|etra)$/\n gender = :female\n else\n puts \"NO\"\n Process.exit\n end\n\n for i in 1...words.size\n if (gender == :male and words[i] =~ /(liala|etra|inites)$/) or (gender == :female and words[i] =~ /(lios|etr|initis)$/)\n puts \"NO\"\n Process.exit\n end\n end\n\n pos = 1\n type = (first =~ /(etr|etra)$/) ? :noun : :adj\n\n if type == :adj\n pos += 1 while words[pos] =~ /(lios|liala)$/\n unless words[pos] =~ /(etr|etra)$/\n puts \"NO\"\n Process.exit\n end\n pos += 1\n end\n\n\n for i in pos...words.size\n unless (gender == :male and words[i] =~ /initis$/) or (gender == :female and words[i] =~ /inites$/)\n puts \"NO\"\n Process.exit\n end\n end\n\n puts \"YES\"\nend"}], "negative_code": [{"source_code": "words = gets.split\n\nif words.size == 1 and words.first =~ /(etr|etra)$/\n puts \"YES\"\nelsif words.size == 1\n puts \"NO\"\nelse\n first = words.first\n if first =~ /(lios|etr)$/\n gender = :male\n elsif first =~ /(liala|etra)$/\n gender = :female\n else\n puts \"NO\"\n Process.exit\n end\n\n for i in 1..words.size\n if (gender == :male and words[i] =~ /(liala|etra|inites)$/) or (gender == :female and words[i] =~ /(lios|etr|initis)$/)\n puts \"NO\"\n Process.exit\n end\n end\n\n pos = 1\n type = (first =~ /(etr|etra)$/) ? :noun : :adj\n\n if type == :adj\n pos += 1 while words[pos] =~ /(lios|liala)$/\n unless words[pos] =~ /(etr|etra)$/\n puts \"NO\"\n Process.exit\n end\n i += 1\n end\n\n\n for i in pos..words.size\n if (gender == :male and words[i] =~ /inites$/) or (gender == :female and words[i] =~ /initis$/)\n puts \"NO\"\n Process.exit\n end\n end\n\n puts \"YES\"\nend\n"}, {"source_code": "words = gets.split\n\nif words.size == 1 and words.first =~ /(etr|etra|lios|liala|inites|initis)$/\n puts \"YES\"\nelsif words.size == 1\n puts \"NO\"\nelse\n first = words.first\n if first =~ /(lios|etr)$/\n gender = :male\n elsif first =~ /(liala|etra)$/\n gender = :female\n else\n puts \"NO\"\n Process.exit\n end\n\n for i in 1..words.size\n if (gender == :male and words[i] =~ /(liala|etra|inites)$/) or (gender == :female and words[i] =~ /(lios|etr|initis)$/)\n puts \"NO\"\n Process.exit\n end\n end\n\n pos = 1\n type = (first =~ /(etr|etra)$/) ? :noun : :adj\n\n if type == :adj\n pos += 1 while words[pos] =~ /(lios|liala)$/\n unless words[pos] =~ /(etr|etra)$/\n puts \"NO\"\n Process.exit\n end\n i += 1\n end\n\n\n for i in pos..words.size\n unless words[i] =~ /(initis|inites)$/\n puts \"NO\"\n Process.exit\n end\n end\n\n puts \"YES\"\nend\n"}, {"source_code": "words = gets.split\n\nif words.size == 1 and words.first =~ /(etr|etra|lios|liala|inites|initis)$/\n puts \"YES\"\nelsif words.size == 1\n puts \"NO\"\nelse\n first = words.first\n if first =~ /(lios|etr)$/\n gender = :male\n elsif first =~ /(liala|etra)$/\n gender = :female\n else\n puts \"NO\"\n Process.exit\n end\n\n for i in 1..words.size\n if (gender == :male and words[i] =~ /(liala|etra|inites)$/) or (gender == :female and words[i] =~ /(lios|etr|initis)$/)\n puts \"NO\"\n Process.exit\n end\n end\n\n pos = 1\n type = (first =~ /(etr|etra)$/) ? :noun : :adj\n\n if type == :adj\n pos += 1 while words[pos] =~ /(lios|liala)$/\n unless words[pos] =~ /(etr|etra)$/\n puts \"NO\"\n Process.exit\n end\n pos += 1\n end\n\n\n for i in pos..words.size\n if (gender == :male and words[i] =~ /inites$/) or (gender == :female and words[i] =~ /initis$/)\n puts \"NO\"\n Process.exit\n end\n end\n\n puts \"YES\"\nend"}, {"source_code": "words = gets.split\n\nif words.size == 1 and words.first =~ /(etr|etra|lios|liala|inites|initis)$/\n puts \"YES\"\nelsif words.size == 1\n puts \"NO\"\nelse\n first = words.first\n if first =~ /(lios|etr)$/\n gender = :male\n elsif first =~ /(liala|etra)$/\n gender = :female\n else\n puts \"NO\"\n Process.exit\n end\n\n for i in 1..words.size\n if (gender == :male and words[i] =~ /(liala|etra|inites)$/) or (gender == :female and words[i] =~ /(lios|etr|initis)$/)\n puts \"NO\"\n Process.exit\n end\n end\n\n pos = 1\n type = (first =~ /(etr|etra)$/) ? :noun : :adj\n\n if type == :adj\n pos += 1 while words[pos] =~ /(lios|liala)$/\n unless words[pos] =~ /(etr|etra)$/\n puts \"NO\"\n Process.exit\n end\n i += 1\n end\n\n\n for i in pos..words.size\n if (gender == :male and words[i] =~ /inites$/) or (gender == :female and words[i] =~ /initis$/)\n puts \"NO\"\n Process.exit\n end\n end\n\n puts \"YES\"\nend"}, {"source_code": "$_ = gets.chomp + ' '\n[%w(lios etr initis), %w(liala etra inites)].each do |r|\n\tr.map!{|_| \"\\\\S*#{_}\\\\s+\"}\n\tr << \"(#{r[0]})*(#{r[1]})(#{r[2]})*\";\n\tif /^(#{r.join('|')})$/\n\t\tputs $&\n\t\tputs :YES\n\t\texit\n\tend\nend\nputs :NO\n"}, {"source_code": "$_ = gets.chomp + ' '\n[%w(lios etr initis), %w(liala etra inites)].each do |r|\n\tr.map!{|_| \".*#{_}\\s+\"}\n\tr << \"(#{r[0]})*(#{r[1]})(#{r[2]})*\";\n\tif /^#{r.join('|')}$/\n\t\tputs :YES\n\t\texit\n\tend\nend\nputs :NO\n"}, {"source_code": "s = gets.strip\n[%w(lios etr initis), %w(liala etra inites)].each do |g|\n r = g.map { |t| \"\\\\S*#{t}(\\\\s+|$)\" }\n r << \"(#{r[0]})*#{r[1]}(#{r[2]})*\"\n if /^#{r.join ?|}$/.match s\n puts :YES\n exit\n end\nend\nputs :NO\n"}, {"source_code": "def solve(ws)\n ts = ws.map do |w|\n return [:adj, :mas] if w.end_with?('lios')\n return [:adj, :fem] if w.end_with?('liala')\n return [:noun, :mas] if w.end_with?('etr')\n return [:noun, :fem] if w.end_with?('etra')\n return [:verb, :mas] if w.end_with?('initis')\n return [:verb, :fem] if w.end_with?('inites')\n return nil\n end\n return false if ts.any?(&:nil)\n return true if ts.size == 1\n return false unless ts.all?{|x| x[1] == ts[0][1]}\n cb = :adj\n ts.each do |t, _|\n case cb\n when :adj\n case t\n when :noun then cb=:noun\n when :verb then return false\n end\n when :noun,:verb\n case t\n when :adj, :noun then return false\n when :verb then cb=:verb\n end\n end\n end\n return cb == :noun || cb == :verb\nend\n\nwords = gets.split\nputs solve(words) ? 'YES' : 'NO'\n"}, {"source_code": "w = gets.split\nbool = false\nif w.size == 1\n w = w[0]\n if w[-3..-1] == 'etr' || w[-4..-1] == 'lios' || w[-4..-1] == 'etra' || w[-5..-1] == 'liala' || w[-6..-1] == 'initis' || w[-6..-1] == 'inites'\n bool = true\n end\nelse\n if w[0][-4..-1] == 'lios' || w[0][-3..-1] == 'etr'\n while w[0][-4..-1] == 'lios'\n w.shift\n break if w.empty? \n end\n if !w.empty?\n w.shift if w[0][-3..-1] == 'etr'\n if !w.empty?\n while w[0][-6..-1] == 'initis'\n w.shift\n break if w.empty?\n end\n end\n bool = true if w.size == 0\n end\n elsif w[0][-5..-1] == 'liala' || w[0][-4..-1] == 'etra'\n while w[0][-5..-1] == 'liala'\n w.shift\n break if !w.empty?\n end\n if !w.empty?\n w.shift if w[0][-4..-1] == 'etra'\n if !w.empty?\n while w[0][-6..-1] == 'inites'\n w.shift\n break if w.empty?\n end\n bool = true if w.size == 0\n end\n end\n end\nend\nif bool\n puts \"YES\"\nelse\n puts \"NO\"\nend"}, {"source_code": "def get_info(s)\n case s\n when /lios$/\n return 1, 0\n when /liala$/\n return 1, 1\n when /etr$/\n return 2, 0\n when /etra$/\n return 2, 1\n when /initis$/\n return 3, 0\n when /inites$/\n return 3, 1\n else\n return -1, -1\n end\nend\n\ndef verify(words)\n if words.size == 0\n return false\n end\n last_type = -1\n count_noun = 0\n words.each { |word|\n # Must be valid word\n if word[0] < 0\n return false\n end\n # Must have same gender\n if word[1] != words[0][1]\n return false\n end\n # Must be increasing in type\n if word[0] == 2\n count_noun += 1\n end\n if word[0] < last_type\n return false\n end\n last_type = word[0]\n }\n return count_noun == 1\nend\n\n[*$<].each { |line|\n words = line.split\n if verify(words.map { |s| get_info(s) })\n puts 'YES'\n else\n puts 'NO'\n end\n}\n\n"}, {"source_code": "def get_info(s)\n case s\n when /lios$/\n return 1, 0\n when /liala$/\n return 1, 1\n when /etr$/\n return 2, 0\n when /etra$/\n return 2, 1\n when /initis$/\n return 3, 0\n when /inites$/\n return 3, 1\n else\n return -1, -1\n end\nend\n\ndef verify(words)\n if words.size == 0\n return false\n end\n if words.size == 1\n return true\n end\n last_type = -1\n count_noun = 0\n words.each { |word|\n # Must be valid word\n if word[0] < 0\n return false\n end\n # Must have same gender\n if word[1] != words[0][1]\n return false\n end\n # Must be increasing in type\n if word[0] == 2\n count_noun += 1\n end\n if word[0] < last_type\n return false\n end\n last_type = word[0]\n }\n return count_noun == 1\nend\n\n[*$<].each { |line|\n words = line.split\n if verify(words.map { |s| get_info(s) })\n puts 'YES'\n else\n puts 'NO'\n end\n}\n\n"}, {"source_code": "words = gets.split\n\nif words.size == 1 and words.first =~ /(etr|etra|lios|liala|inites|initis)$/\n puts \"YES\"\nelsif words.size == 1\n puts \"NO\"\nelse\n first = words.first\n if first =~ /(lios|etr)$/\n gender = :male\n elsif first =~ /(liala|etra)$/\n gender = :female\n else\n puts \"NO\"\n Process.exit\n end\n\n for i in 1..words.size\n if (gender == :male and words[i] =~ /(liala|etra|inites)$/) or (gender == :female and words[i] =~ /(lios|etr|initis)$/)\n puts \"NO\"\n Process.exit\n end\n end\n\n pos = 1\n type = (first =~ /(etr|etra)$/) ? :noun : :adj\n\n if type == :adj\n pos += 1 while words[pos] =~ /(lios|liala)$/\n unless words[pos] =~ /(etr|etra)$/\n puts \"NO\"\n Process.exit\n end\n pos += 1\n end\n\n\n for i in pos..words.size\n unless words[i] =~ /(initis|inites)$/\n puts \"NO\"\n Process.exit\n end\n end\n\n puts \"YES\"\nend"}, {"source_code": "words = gets.split\n\nif words.size == 1 and words.first =~ /(etr|etra|lios|liala|inites|initis)$/\n puts \"YES\"\nelse\n first = words.first\n if first =~ /(lios|etr)$/\n gender = :male\n elsif first =~ /(liala|etra)$/\n gender = :female\n else\n puts \"NO\"\n Process.exit\n end\n\n for i in 1..words.size\n if (gender == :male and words[i] =~ /(liala|etra|inites)$/) or (gender == :female and words[i] =~ /(lios|etr|initis)$/)\n puts \"NO\"\n Process.exit\n end\n end\n\n pos = 1\n type = (first =~ /(etr|etra)$/) ? :noun : :adj\n\n if type == :adj\n pos += 1 while words[pos] =~ /(lios|liala)$/\n unless words[pos] =~ /(etr|etra)$/\n puts \"NO\"\n Process.exit\n end\n i += 1\n end\n\n\n for i in pos..words.size\n if (gender == :male and words[i] =~ /inites$/) or (gender == :female and words[i] =~ /initis$/)\n puts \"NO\"\n Process.exit\n end\n end\n\n puts \"YES\"\nend\n"}, {"source_code": "words = gets.split\n\nif words.size == 1 and words.first =~ /(etr|etra|lios|liala|inites|initis)$/\n puts \"YES\"\nelsif words.size == 1\n puts \"NO\"\nelse\n first = words.first\n if first =~ /(lios|etr)$/\n gender = :male\n elsif first =~ /(liala|etra)$/\n gender = :female\n else\n puts \"NO\"\n Process.exit\n end\n\n for i in 1..words.size\n if (gender == :male and words[i] =~ /(liala|etra|inites)$/) or (gender == :female and words[i] =~ /(lios|etr|initis)$/)\n puts \"NO\"\n Process.exit\n end\n end\n\n pos = 1\n type = (first =~ /(etr|etra)$/) ? :noun : :adj\n\n if type == :adj\n pos += 1 while words[pos] =~ /(lios|liala)$/\n unless words[pos] =~ /(etr|etra)$/\n puts \"NO\"\n Process.exit\n end\n i += 1\n end\n\n\n for i in pos..words.size\n unless words[i] =~ /(initis|inites)$/\n puts \"NO\"\n Process.exit\n end\n end\n\n puts \"YES\"\nend"}, {"source_code": "words = gets.split\n\nif words.size == 1 and words.first =~ /(etr|etra|lios|liala|inites|initis)$/\n puts \"YES\"\nelsif words.size == 1\n puts \"NO\"\nelse\n first = words.first\n if first =~ /(lios|etr)$/\n gender = :male\n elsif first =~ /(liala|etra)$/\n gender = :female\n else\n puts \"NO\"\n Process.exit\n end\n\n for i in 1..words.size\n if (gender == :male and words[i] =~ /(liala|etra|inites)$/) or (gender == :female and words[i] =~ /(lios|etr|initis)$/)\n puts \"NO\"\n Process.exit\n end\n end\n\n pos = 1\n type = (first =~ /(etr|etra)$/) ? :noun : :adj\n\n if type == :adj\n pos += 1 while words[pos] =~ /(lios|liala)$/\n unless words[pos] =~ /(etr|etra)$/\n puts \"NO\"\n Process.exit\n end\n i += 1\n end\n\n\n for i in pos..words.size\n if (gender == :male and words[i] =~ /inites$/) or (gender == :female and words[i] =~ /initis$/)\n puts \"NO\"\n Process.exit\n end\n end\n\n puts \"YES\"\nend\n"}, {"source_code": "words = gets.split\n\nif words.size == 1 and words.first =~ /(etr|etra|lios|liala|inites|initis)$/\n puts \"YES\"\nelsif words.size == 1\n puts \"NO\"\nelse\n first = words.first\n if first =~ /(lios|etr)$/\n gender = :male\n elsif first =~ /(liala|etra)$/\n gender = :female\n else\n puts \"NO\"\n Process.exit\n end\n\n for i in 1...words.size\n if (gender == :male and words[i] =~ /(liala|etra|inites)$/) or (gender == :female and words[i] =~ /(lios|etr|initis)$/)\n puts \"NO\"\n Process.exit\n end\n end\n\n pos = 1\n type = (first =~ /(etr|etra)$/) ? :noun : :adj\n\n if type == :adj\n pos += 1 while words[pos] =~ /(lios|liala)$/\n unless words[pos] =~ /(etr|etra)$/\n puts \"NO\"\n Process.exit\n end\n pos += 1\n end\n\n\n for i in pos...words.size\n if words[i] =~ /(initis|inites)$/\n puts \"NO\"\n Process.exit\n end\n end\n\n puts \"YES\"\nend"}, {"source_code": "words = gets.split\n\nif words.size == 1 and words.first =~ /(etr|etra|lios|liala|inites|initis)$/\n puts \"YES\"\nelsif words.size == 1\n puts \"NO\"\nelse\n first = words.first\n if first =~ /(lios|etr)$/\n gender = :male\n elsif first =~ /(liala|etra)$/\n gender = :female\n else\n puts \"NO\"\n Process.exit\n end\n\n for i in 1..words.size\n if (gender == :male and words[i] =~ /(liala|etra|inites)$/) or (gender == :female and words[i] =~ /(lios|etr|initis)$/)\n puts \"NO\"\n Process.exit\n end\n end\n\n pos = 1\n type = (first =~ /(etr|etra)$/) ? :noun : :adj\n\n if type == :adj\n pos += 1 while words[pos] =~ /(lios|liala)$/\n unless words[pos] =~ /(etr|etra)$/\n puts \"NO\"\n Process.exit\n end\n i += 1\n end\n\n\n for i in pos..words.size\n if words[i] =~ /(initis|inites)$/\n puts \"NO\"\n Process.exit\n end\n end\n\n puts \"YES\"\nend\n"}, {"source_code": "words = gets.split\n\nif words.size == 1 and words.first =~ /(etr|etra|lios|liala|inites|initis)$/\n puts \"YES\"\nelsif words.size == 1\n puts \"NO\"\nelse\n first = words.first\n if first =~ /(lios|etr)$/\n gender = :male\n elsif first =~ /(liala|etra)$/\n gender = :female\n else\n puts \"NO\"\n Process.exit\n end\n\n for i in 1..words.size\n if (gender == :male and words[i] =~ /(liala|etra|inites)$/) or (gender == :female and words[i] =~ /(lios|etr|initis)$/)\n puts \"NO\"\n Process.exit\n end\n end\n\n pos = 1\n type = (first =~ /(etr|etra)$/) ? :noun : :adj\n\n if type == :adj\n pos += 1 while words[pos] =~ /(lios|liala)$/\n unless words[pos] =~ /(etr|etra)$/\n puts \"NO\"\n Process.exit\n end\n pos += 1\n end\n\n\n for i in pos..words.size\n unless (gender == :male and words[i] =~ /initis$/) or (gender == :female and words[i] =~ /inites$/)\n puts \"NO\"\n Process.exit\n end\n end\n\n puts \"YES\"\nend"}], "src_uid": "0c9550a09f84de6bed529d007ccb4ae8"} {"nl": {"description": "Monocarp is playing a computer game. In this game, his character fights different monsters.A fight between a character and a monster goes as follows. Suppose the character initially has health $$$h_C$$$ and attack $$$d_C$$$; the monster initially has health $$$h_M$$$ and attack $$$d_M$$$. The fight consists of several steps: the character attacks the monster, decreasing the monster's health by $$$d_C$$$; the monster attacks the character, decreasing the character's health by $$$d_M$$$; the character attacks the monster, decreasing the monster's health by $$$d_C$$$; the monster attacks the character, decreasing the character's health by $$$d_M$$$; and so on, until the end of the fight. The fight ends when someone's health becomes non-positive (i. e. $$$0$$$ or less). If the monster's health becomes non-positive, the character wins, otherwise the monster wins.Monocarp's character currently has health equal to $$$h_C$$$ and attack equal to $$$d_C$$$. He wants to slay a monster with health equal to $$$h_M$$$ and attack equal to $$$d_M$$$. Before the fight, Monocarp can spend up to $$$k$$$ coins to upgrade his character's weapon and/or armor; each upgrade costs exactly one coin, each weapon upgrade increases the character's attack by $$$w$$$, and each armor upgrade increases the character's health by $$$a$$$.Can Monocarp's character slay the monster if Monocarp spends coins on upgrades optimally?", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 5 \\cdot 10^4$$$) — the number of test cases. Each test case consists of three lines: The first line contains two integers $$$h_C$$$ and $$$d_C$$$ ($$$1 \\le h_C \\le 10^{15}$$$; $$$1 \\le d_C \\le 10^9$$$) — the character's health and attack; The second line contains two integers $$$h_M$$$ and $$$d_M$$$ ($$$1 \\le h_M \\le 10^{15}$$$; $$$1 \\le d_M \\le 10^9$$$) — the monster's health and attack; The third line contains three integers $$$k$$$, $$$w$$$ and $$$a$$$ ($$$0 \\le k \\le 2 \\cdot 10^5$$$; $$$0 \\le w \\le 10^4$$$; $$$0 \\le a \\le 10^{10}$$$) — the maximum number of coins that Monocarp can spend, the amount added to the character's attack with each weapon upgrade, and the amount added to the character's health with each armor upgrade, respectively. The sum of $$$k$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, print YES if it is possible to slay the monster by optimally choosing the upgrades. Otherwise, print NO.", "sample_inputs": ["4\n25 4\n9 20\n1 1 10\n25 4\n12 20\n1 1 10\n100 1\n45 2\n0 4 10\n9 2\n69 2\n4 2 7"], "sample_outputs": ["YES\nNO\nYES\nYES"], "notes": "NoteIn the first example, Monocarp can spend one coin to upgrade weapon (damage will be equal to $$$5$$$), then health during battle will change as follows: $$$(h_C, h_M) = (25, 9) \\rightarrow (25, 4) \\rightarrow (5, 4) \\rightarrow (5, -1)$$$. The battle ended with Monocarp's victory.In the second example, Monocarp has no way to defeat the monster.In the third example, Monocarp has no coins, so he can't buy upgrades. However, the initial characteristics are enough for Monocarp to win.In the fourth example, Monocarp has $$$4$$$ coins. To defeat the monster, he has to spend $$$2$$$ coins to upgrade weapon and $$$2$$$ coins to upgrade armor."}, "positive_code": [{"source_code": "gets.to_i.times do\n hp_c, atk_c = gets.split.map(&:to_i)\n hp_m, atk_m = gets.split.map(&:to_i)\n k, atk_up, hp_up = gets.split.map(&:to_i)\n\n solution_found = false\n (0..k).each do |ak|\n hpk = k - ak\n hp = hp_c + hpk * hp_up\n atk = atk_c + ak * atk_up\n dmg = (((hp_m + atk - 1) / atk) - 1) * atk_m\n if dmg < hp\n solution_found = true\n break\n end\n end\n\n puts(solution_found ? \"YES\" : \"NO\")\nend\n"}], "negative_code": [{"source_code": "gets.to_i.times do\n hp_c, atk_c = gets.split.map(&:to_i)\n hp_m, atk_m = gets.split.map(&:to_i)\n k, atk_up, hp_up = gets.split.map(&:to_i)\n\n solution_found = false\n (0..k).each do |ak|\n hpk = k - ak\n hp = hp_c + hpk * hp_up\n atk = atk_c + ak * atk_up\n dmg = (hp_m / atk) * atk_m\n if dmg < hp\n solution_found = true\n break\n end\n end\n\n puts(solution_found ? \"YES\" : \"NO\")\nend\n"}], "src_uid": "5b1f33228a58d9e14bc9479767532c25"} {"nl": {"description": "Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures).There are n members, numbered 1 through n. m pairs of members are friends. Of course, a member can't be a friend with themselves.Let A-B denote that members A and B are friends. Limak thinks that a network is reasonable if and only if the following condition is satisfied: For every three distinct members (X, Y, Z), if X-Y and Y-Z then also X-Z.For example: if Alan and Bob are friends, and Bob and Ciri are friends, then Alan and Ciri should be friends as well.Can you help Limak and check if the network is reasonable? Print \"YES\" or \"NO\" accordingly, without the quotes.", "input_spec": "The first line of the input contain two integers n and m (3 ≤ n ≤ 150 000, ) — the number of members and the number of pairs of members that are friends. The i-th of the next m lines contains two distinct integers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi). Members ai and bi are friends with each other. No pair of members will appear more than once in the input.", "output_spec": "If the given network is reasonable, print \"YES\" in a single line (without the quotes). Otherwise, print \"NO\" in a single line (without the quotes).", "sample_inputs": ["4 3\n1 3\n3 4\n1 4", "4 4\n3 1\n2 3\n3 4\n1 2", "10 4\n4 3\n5 10\n8 9\n1 2", "3 2\n1 2\n2 3"], "sample_outputs": ["YES", "NO", "YES", "NO"], "notes": "NoteThe drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is \"NO\" in the second sample because members (2, 3) are friends and members (3, 4) are friends, while members (2, 4) are not. "}, "positive_code": [{"source_code": "#我现在的想法是:把相连的点放在一个集合里,然后统计相连的点至少要多少条边,\n#如果总边数不够,就打印no,如果总边数够就打印yes!\n#今天算是知道了dsu的意思!\n#dsu==disjont set!?\nnm=gets.chomp.split.map{|x| x.to_i }\nn=nm[0]\nm=nm[1]\n$parent=Array.new(1+n)\n$rank=Array.new(1+n)\n$size=Array.new(1+n,1)\nfor i in 0..n\n\t$parent[i]=i\n\t$rank[i]=0\nend\ndef find(a)\n\twhile $parent[a]!=a\n\t\ta=$parent[a]\n\tend\n\treturn a\nend\ndef union(a,b)\n\tra=find(a)\n\trb=find(b)\n\tif ra==rb\n\t\treturn\n\tend\n\tif $rank[ra]<$rank[rb]\n\t\t$parent[ra]=rb\n\t\t$size[rb]+=$size[ra];\n\t\t$size[ra]=0;\n\telsif $rank[ra]>$rank[rb]\n\t\t$parent[rb]=ra\n\t\t$size[ra]+=$size[rb];\n\t\t$size[rb]=0;\n\telse\n\t\t$parent[rb]=ra\n\t\t$rank[ra]+=1\n\t\t$size[ra]+=$size[rb];\n\t\t$size[rb]=0;\n\tend\nend\nm.times do\n\tab=gets.chomp.split.map{|x| x.to_i }\n\tunion(ab[0],ab[1])\nend\nsee=Array.new(1+n,0)\ndef all_connect(n)\n\tn*(n-1)/2\nend\n#puts $size.join(\" \")\ntotal_edge=0\n#puts $parent.join(\" \")\nfor i in 1..n\n\ttotal_edge+=all_connect($size[i])\n\t#puts all_connect($size[i])\nend\n#puts total_edge\n#puts m\nif total_edge==m\n\tputs \"YES\"\nelse\n\tputs \"NO\"\nend\n"}, {"source_code": "require 'set'\nn, m = gets.split.map(&:to_i)\n\n#friends = Set.new # friends with... er... friends. For .delete() only\nfriendships = Hash.new do |hash, key|\n\thash[key] = []\nend # id => [f1, f2, ...]\n#friends[A][B], A0\n\t#p friendships\n\tf_id, friends = friendships.first\n\tfriendships.delete(f_id)\n\tnext if friends.size == 0\t\n\tfriends_to_check = friends\n\tfriends_checked = Set.new([f_id])\n\tfriends_count = friends.length\n\t#p \"#> #{f_id} #{friends_count} #{friends}\"\n\tuntil friends_to_check.empty?\n\t\tf = friends_to_check.pop\n\t\tfriends = friendships[f]\n\t\tfriendships.delete(f)\n\t\tnext if friends_checked.include? f\n\t\t#p \">>> #{f} #{friends}\"\t\t\n\t\tif friends.size != friends_count\n\t\t\t#OOPS.\n\t\t\tputs \"NO\"\n\t\t\texit\n\t\tend\n\t\tfriends.each do |f_new|\n\t\t\tnext if friends_checked.include? f_new\n\t\t\tfriends_to_check << f_new\n\t\tend\n\t\tfriends_checked << f\n\tend\n\t#p \"#: #{friends_checked}; #{friends_count}\" \n\tif friends_checked.size!=friends_count+1\n\t\t#OOPS!\n\t\tputs \"NO\"\n\t\texit\n\tend\nend\nputs \"YES\"\n"}, {"source_code": "#1. Accomodate Inputs & Initialize\nn = gets(\" \").to_i\nm = gets.chomp.to_i\n\nrelations = Array.new(n+1){Array.new()}\n(1..m).each do |i|\n a = gets(\" \").to_i\n b = gets.chomp.to_i\n relations[a].push(b)\n relations[b].push(a)\nend\n\namigos = Array.new(n+1, false)\nback = Array.new()\nstack = Array.new()\n\n\n#2. Build equivalent classes\n(1..n).each do |i|\n next if relations[i].empty?\n\n back.each{ |n| amigos[n] = false }\n back.clear() \n stack.clear()\n\n amigos[i] = true\n stack.push(i)\n back.push(i)\n ne = 1\n nr = 0\n\n until (stack.empty?)\n head = stack.pop()\n relations[head].each do |e|\n if !amigos[e]\n ne += 1\n stack.push(e)\n back.push(e)\n amigos[e] = true\n end\n end\n nr += relations[head].length()\n relations[head].clear()\n end\n\n if (ne-1)*ne != nr\n puts \"NO\"\n exit\n end\nend\n\nputs \"YES\""}], "negative_code": [{"source_code": "#我现在的想法是:把相连的点放在一个集合里,然后统计相连的点至少要多少条边,\n#如果总边数不够,就打印no,如果总边数够就打印yes!\n#今天算是知道了dsu的意思!\n#dsu==disjont set!?\nnm=gets.chomp.split.map{|x| x.to_i }\nn=nm[0]\nm=nm[1]\n$parent=Array.new(1+n)\n$rank=Array.new(1+n)\n$size=Array.new(1+n,0)\n$see=Array.new(1+n,0)\nfor i in 0..n\n\t$parent[i]=i\n\t$rank[i]=0\nend\ndef find(a)\n\tif $parent[a]!=a\n\t\t$parent[a]=find($parent[a])\n\tend\n\treturn $parent[a]\nend\ndef union(a,b)\n\tra=find(a)\n\trb=find(b)\n\tif ra==rb\n\t\treturn\n\tend\n\tif $rank[ra]<$rank[rb]\n\t\t$parent[ra]=rb\n\telsif $rank[ra]>$rank[rb]\n\t\t$parent[rb]=ra\n\telse\n\t\t$parent[rb]=ra\n\t\t$rank[ra]+=1\n\tend\n\tif $see[a]==0 && $see[b]==0\n\t\t$size[find(a)]+=2\n\t\t$see[a]=1\n\t\t$see[b]=1\n\telsif $see[a]==1 && $see[b]==0\n\t\t$size[find(a)]+=1\n\t\t$see[b]=1\n\telsif $see[a]==0 && $see[b]==1\n\t\t$size[find(b)]+=1\n\t\t$see[a]=1\n\tend\nend\nm.times do\n\tab=gets.chomp.split.map{|x| x.to_i }\n\tunion(ab[0],ab[1])\nend\nsee=Array.new(1+n,0)\ndef all_connect(n)\n\tn*(n-1)/2\nend\ntotal_edge=0\nfor i in 1..n\n\ttotal_edge+=all_connect($size[i])\nend\nif total_edge==m\n\tputs \"YES\"\nelse\n\tputs \"NO\"\nend\n"}, {"source_code": "#1. Accomodate inputs & Initialize\nn = gets(\" \").to_i\nm = gets.chomp.to_i\n\nrelations = Array.new(n+1){Array.new()}\n(1..m).each do |i|\n a = gets(\" \").to_i\n b = gets.chomp.to_i\n relations[a].push(b)\nend\n\namigos = Array.new(n+1, 0)\nstack = Array.new()\n\n\n#2. Build equivalent classes\n(1..n).each do |i|\n next if relations[i].empty?\n\n ne = 1\n nr = 0\n amigos.fill(false)\n stack.clear()\n\n amigos[i] = true\n stack.push(i)\n\n while (!stack.empty?)\n head = stack.pop()\n relations[head].each do |e|\n if !amigos[e]\n ne += 1\n stack.push(e)\n amigos[e] = true\n end\n end\n nr += relations[head].length()\n relations[head].clear()\n end\n\n if ((ne-1)*ne)/2 != nr\n puts \"NO\"\n exit\n end\nend\n\nputs \"YES\""}], "src_uid": "1173d89dd3af27b46e579cdeb2cfdfe5"} {"nl": {"description": "Vasya has a beautiful garden where wonderful fruit trees grow and yield fantastic harvest every year. But lately thieves started to sneak into the garden at nights and steal the fruit too often. Vasya can’t spend the nights in the garden and guard the fruit because there’s no house in the garden! Vasya had been saving in for some time and finally he decided to build the house. The rest is simple: he should choose in which part of the garden to build the house. In the evening he sat at his table and drew the garden’s plan. On the plan the garden is represented as a rectangular checkered field n × m in size divided into squares whose side length is 1. In some squares Vasya marked the trees growing there (one shouldn’t plant the trees too close to each other that’s why one square contains no more than one tree). Vasya wants to find a rectangular land lot a × b squares in size to build a house on, at that the land lot border should go along the lines of the grid that separates the squares. All the trees that grow on the building lot will have to be chopped off. Vasya loves his garden very much, so help him choose the building land lot location so that the number of chopped trees would be as little as possible.", "input_spec": "The first line contains two integers n and m (1 ≤ n, m ≤ 50) which represent the garden location. The next n lines contain m numbers 0 or 1, which describe the garden on the scheme. The zero means that a tree doesn’t grow on this square and the 1 means that there is a growing tree. The last line contains two integers a and b (1 ≤ a, b ≤ 50). Note that Vasya can choose for building an a × b rectangle as well a b × a one, i.e. the side of the lot with the length of a can be located as parallel to the garden side with the length of n, as well as parallel to the garden side with the length of m.", "output_spec": "Print the minimum number of trees that needs to be chopped off to select a land lot a × b in size to build a house on. It is guaranteed that at least one lot location can always be found, i. e. either a ≤ n and b ≤ m, or a ≤ m и b ≤ n.", "sample_inputs": ["2 2\n1 0\n1 1\n1 1", "4 5\n0 0 1 0 1\n0 1 1 1 0\n1 0 1 0 1\n1 1 1 1 1\n2 3"], "sample_outputs": ["0", "2"], "notes": "NoteIn the second example the upper left square is (1,1) and the lower right is (3,2)."}, "positive_code": [{"source_code": "n = gets(' ').to_i\nm = gets.to_i\ns = Array.new(n) do\n gets.split\nend\na = gets(' ').to_i\nb = gets.to_i\n\nl = []\n\nif a<=n && b<=m\n for i in 0..(n-a)\n for j in 0..(m-b)\n l << s[i,a].map{|k|k[j,b]}.flatten.count('1')\n end\n end\nend\n\nif a<=m && b<=n\n for i in 0..(n-b)\n for j in 0..(m-a)\n l << s[i,b].map{|k|k[j,a]}.flatten.count('1')\n end\n end\nend\n\np l.min\n"}, {"source_code": "def solve(a,b)\n ans=999999999\n $w.times{|x|\n $h.times{|y|\n if x+a-1<$w && y+b-1<$h then\n tans=0\n a.times{|i|\n b.times{|j|\n if $map[y+j][x+i]==1 then\n tans+=1\n end\n }\n }\n ans=[ans,tans].min\n end\n }\n }\n return ans\nend\n$h,$w=gets.split.map(&:to_i)\n$map=[]\n$h.times{ $map.push(gets.split.map(&:to_i)) }\na,b=gets.split.map(&:to_i)\nputs [solve(a,b),solve(b,a)].min"}, {"source_code": "def anal(memo,n,m,a,b)\n\tmin=n*m\n\t1.upto(n-a+1){|i|1.upto(m-b+1){|j|\n\t\ttemp=memo[i-1][j-1]+memo[i+a-1][j+b-1]-memo[i+a-1][j-1]-memo[i-1][j+b-1]\n\t\tmin=temp if temp sweaty\n puts -1\n exit\n end\n map.push(sweaty-gnome)\n}\n\nmap.sort!\n\nans = 1\n\nmap.each_index{ |i|\n if i != 0\n if map[i-1] != map[i]\n ans += 1\n end\n end\n}\n\nputs \"#{ans}\""}, {"source_code": "gets\nd = []\nwhile s = gets do\n g = s.index 'G'\n f = s.index 'S'\n if g > f\n p -1\n exit\n end\n d << f - g\nend\np d.uniq.length"}, {"source_code": "gets\nd = []\nwhile s = gets do\n g = s.index 'G'\n f = s.index 'S'\n if !g or !f or g > f\n p -1\n exit\n end\n d << f - g\nend\np d.uniq.length"}, {"source_code": "n, m = gets.split(\" \").map &:to_i\ndistances = []\nn.times do\n s = gets\n gnome = s.index 'G'\n flower = s.index 'S'\n if !gnome or !flower or gnome > flower\n p -1\n exit\n end\n distances << flower - gnome\nend\np distances.uniq.length"}, {"source_code": "n,m=gets.split.map(&:to_i)\nb=[]\nn.times{\n\ta=gets.chomp\n\tb< s\n puts -1\n exit\n end\n all << s - g\nend\nputs all.uniq.size\n"}, {"source_code": "n, m = gets.split.collect{|i| i.to_i}\nans = Array.new(n)\nn.times do |i|\n s = gets.chomp\n d = s.index(\"G\")\n c = s.index(\"S\")\n if d > c\n puts -1\n exit\n end\n ans[i] = c-d\nend\nputs ans.uniq.size\n"}, {"source_code": "def check_min_distance (board)\n\tdistance = []\n\tboard.each do |row|\n\t\tdwarf = 0\n\t\tcandy = 0\n\t\trow.each_with_index do |c, index|\n\t\t\tif c == \"G\"\n\t\t\t\tdwarf = index\n\t\t\telsif c == \"S\"\n\t\t\t\tcandy = index\n\t\t\tend\n\t\tend\n\t\tif dwarf < candy\n\t\t\tdistance << (candy - dwarf)\n\t\telse\n\t\t\treturn -1\n\t\tend\n\tend\n\n\treturn (distance.uniq).length\n\nend\n\nboard = []\nrow = (((gets()).split(\" \")).shift).to_i - 1\n\nfor i in 0..row\n\tary = []\n\t(gets()).each_char{ |c| ary << c }\n\tary.pop\n\tboard << ary\nend\n\nputs check_min_distance(board)"}], "negative_code": [{"source_code": "n, m = gets.split.map &:to_i\nmap = Array.new(n)\nall = []\nf = [true]\nn.times do |i|\n map[i] = gets.chomp.split(//)\n g = -1; s = -1\n map[i].each_with_index do |x, i|\n g = i if x == 'G'\n s = i if x == 'S'\n end\n if s == -1 || g == -1 || g > s\n puts -1\n exit\n end\n all << s - g\nend\nans = 0\nall = all.uniq.sort\nall.each do |x|\n if !f[x]\n ans += 1\n m.downto(x) do |i|\n f[i] = true if f[i - x]\n end\n end\nend\nputs ans\n"}, {"source_code": "n, m = gets.split.collect{|i| i.to_i}\nans = 0\nn.times do\n s = gets.chomp\n d = s.index(\"G\")\n c = s.index(\"S\")\n if d > c\n ans = -1\n break\n end\n ans = c-d-1 if ans < c-d-1\nend\nputs ans\n"}, {"source_code": "def check_min_distance (board, n)\n min = -1\n board.each do |row|\n dwarf = 0\n candy = 0\n row.each_with_index do |c, index|\n if c == \"G\"\n dwarf = index\n elsif c == \"S\"\n candy = index\n end\n end\n if dwarf < candy\n if min == -1\n min = candy - dwarf\n elsif (candy - dwarf) < min\n min = candy - dwarf\n end\n elsif dwarf != n\n if (n - dwarf) < min\n min = n - dwarf\n end\n elsif dwarf == n\n return -1\n end\n end\n \n return min\nend\n\nboard = []\nfirst_line = (gets()).split(\" \")\nrow = (first_line.shift).to_i - 1\nn = (first_line.shift).to_i - 1\n\nfor i in 0..row\n ary = []\n (gets()).each_char{ |c| ary << c }\n ary.pop\n board << ary\nend\n\nputs check_min_distance(board, n)"}, {"source_code": "def check_min_distance (board, n)\n min = -1\n far_goal = 0\n over_dwarf = 0\n board.each do |row|\n dwarf = 0\n candy = 0\n row.each_with_index do |c, index|\n if c == \"G\"\n dwarf = index\n elsif c == \"S\"\n candy = index\n end\n end\n if dwarf < candy\n if far_goal < (candy-1 - dwarf)\n far_goal = (candy-1 - dwarf)\n end\n elsif dwarf != n and (candy < dwarf)\n if over_dwarf < dwarf\n over_dwarf = dwarf\n end\n elsif dwarf == n\n return -1\n end\n end\n\n if (over_dwarf != 0)\n return (n - over_dwarf)\n else\n return far_goal\n end\nend\n\nboard = []\nfirst_line = (gets()).split(\" \")\nrow = (first_line.shift).to_i - 1\nn = (first_line.shift).to_i - 1\n\nfor i in 0..row\n ary = []\n (gets()).each_char{ |c| ary << c }\n ary.pop\n board << ary\nend\n\nputs check_min_distance(board, n)"}, {"source_code": "def check_min_distance (board)\n min = -1\n board.each do |row|\n dwarf = 0\n candy = 0\n row.each_with_index do |c, index|\n if c == \"G\"\n dwarf = index\n elsif c == \"S\"\n candy = index\n end\n end\n if dwarf < candy\n if min == -1\n min = candy - dwarf\n elsif (candy - dwarf) < min\n min = candy - dwarf\n end\n end\n end\n \n return min\nend\n\nboard = []\nfirst_line = (gets()).split(\" \")\nrow = (first_line.shift).to_i - 1\n\nfor i in 0..row\n ary = []\n (gets()).each_char{ |c| ary << c }\n ary.pop\n board << ary\nend\n\nputs check_min_distance board"}, {"source_code": "def check_min_distance (board, n)\n min = -1\n far_goal = 0\n over_dwarf = 0\n board.each do |row|\n dwarf = 0\n candy = 0\n row.each_with_index do |c, index|\n if c == \"G\"\n dwarf = index\n elsif c == \"S\"\n candy = index\n end\n end\n if dwarf < candy\n if far_goal < (candy-1 - dwarf)\n far_goal = (candy-1 - dwarf)\n elsif dwarf == candy-1\n return -1\n end\n elsif dwarf != n and (candy < dwarf)\n if over_dwarf < dwarf\n over_dwarf = dwarf\n end\n elsif dwarf == n\n return -1\n end\n end\n\n if (over_dwarf != 0)\n return (n - over_dwarf)\n else\n return far_goal\n end\nend\n\nboard = []\nfirst_line = (gets()).split(\" \")\nrow = (first_line.shift).to_i - 1\nn = (first_line.shift).to_i - 1\n\nfor i in 0..row\n ary = []\n (gets()).each_char{ |c| ary << c }\n ary.pop\n board << ary\nend\n\nputs check_min_distance(board, n)"}, {"source_code": "def check_min_distance (board, n)\n min = -1\n board.each do |row|\n dwarf = 0\n candy = 0\n row.each_with_index do |c, index|\n if c == \"G\"\n dwarf = index\n elsif c == \"S\"\n candy = index\n end\n end\n if dwarf < candy\n if min == -1\n min = candy - dwarf\n elsif (candy - dwarf) < min\n min = candy - dwarf\n end\n elsif dwarf != n\n if (n - dwarf) < min\n min = n - dwarf\n end\n end\n end\n \n return min\nend\n\nboard = []\nfirst_line = (gets()).split(\" \")\nrow = (first_line.shift).to_i - 1\nn = (first_line.shift).to_i - 1\n\nfor i in 0..row\n ary = []\n (gets()).each_char{ |c| ary << c }\n ary.pop\n board << ary\nend\n\nputs check_min_distance(board, n)"}, {"source_code": "def check_min_distance (board, n)\n min = -1\n near_goal = n\n over_dwarf = 0\n board.each do |row|\n dwarf = 0\n candy = 0\n row.each_with_index do |c, index|\n if c == \"G\"\n dwarf = index\n elsif c == \"S\"\n candy = index\n end\n end\n if dwarf < candy\n if (candy - dwarf) < near_goal\n near_goal = (candy - dwarf)\n end\n elsif dwarf != n and (candy < dwarf)\n if over_dwarf < dwarf\n over_dwarf = dwarf\n end\n elsif dwarf == n\n return -1\n end\n end\n\n if (over_dwarf != 0) and (over_dwarf < near_goal)\n return (n - over_dwarf)\n end\n return near_goal\n\nend\n\nboard = []\nfirst_line = (gets()).split(\" \")\nrow = (first_line.shift).to_i - 1\nn = (first_line.shift).to_i - 1\n\nfor i in 0..row\n ary = []\n (gets()).each_char{ |c| ary << c }\n ary.pop\n board << ary\nend\n\nputs check_min_distance(board, n)"}], "src_uid": "9a823b4ac4a79f62cd0c2f88a1c9ef0c"} {"nl": {"description": "Kevin Sun wants to move his precious collection of n cowbells from Naperthrill to Exeter, where there is actually grass instead of corn. Before moving, he must pack his cowbells into k boxes of a fixed size. In order to keep his collection safe during transportation, he won't place more than two cowbells into a single box. Since Kevin wishes to minimize expenses, he is curious about the smallest size box he can use to pack his entire collection. Kevin is a meticulous cowbell collector and knows that the size of his i-th (1 ≤ i ≤ n) cowbell is an integer si. In fact, he keeps his cowbells sorted by size, so si - 1 ≤ si for any i > 1. Also an expert packer, Kevin can fit one or two cowbells into a box of size s if and only if the sum of their sizes does not exceed s. Given this information, help Kevin determine the smallest s for which it is possible to put all of his cowbells into k boxes of size s.", "input_spec": "The first line of the input contains two space-separated integers n and k (1 ≤ n ≤ 2·k ≤ 100 000), denoting the number of cowbells and the number of boxes, respectively. The next line contains n space-separated integers s1, s2, ..., sn (1 ≤ s1 ≤ s2 ≤ ... ≤ sn ≤ 1 000 000), the sizes of Kevin's cowbells. It is guaranteed that the sizes si are given in non-decreasing order.", "output_spec": "Print a single integer, the smallest s for which it is possible for Kevin to put all of his cowbells into k boxes of size s.", "sample_inputs": ["2 1\n2 5", "4 3\n2 3 5 9", "3 2\n3 5 7"], "sample_outputs": ["7", "9", "8"], "notes": "NoteIn the first sample, Kevin must pack his two cowbells into the same box. In the second sample, Kevin can pack together the following sets of cowbells: {2, 3}, {5} and {9}.In the third sample, the optimal solution is {3, 5} and {7}."}, "positive_code": [{"source_code": "_,n=gets.split.map(&:to_i)\na=gets.split.map(&:to_i).reverse\nr=0\nn.times.map{|i|r=[r,(a[i]||0)+(a[2*n-1-i]||0)].max}\np r"}, {"source_code": "_,n=gets.split.map(&:to_i)\na=gets.split.map(&:to_i).reverse\nr=0\nn.times.map{|i|r=[r,(a[i]||0)+(a[2*n-1-i]||0)].max}\np r\n"}, {"source_code": "_,n=gets.split.map(&:to_i)\na=gets.split.map(&:to_i).reverse\nr=0\nn.times.map{|i|r=[r,(a[i]||0)+(a[2*n-1-i]||0)].max}\np r\n"}, {"source_code": "#! ruby\n# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n\ndef gi(); gets.to_i; end\ndef gis(); gets.chomp.split.map(&:to_i); end\n\nh, k = gis\ns = gis\n\nx = s.pop k\n\np x.zip(s.reverse + [0] * (k - s.size)).map{|x,y| x+y}.max\n\n\n\n"}, {"source_code": "_,n=gets.split.map(&:to_i)\na=gets.split.map(&:to_i).reverse\nr=0\nn.times.map{|i|r=[r,(a[i]||0)+(a[2*n-1-i]||0)].max}\np r\n"}, {"source_code": "_,n=gets.split.map(&:to_i)\na=gets.split.map(&:to_i).reverse\nr=0\nn.times.map{|i|r=[r,(a[i]||0)+(a[2*n-1-i]||0)].max}\np r\n"}, {"source_code": "#!/usr/bin/env ruby\n# -*- coding: utf-8 -*-\n\n# b.rb\n\ndef main()\n n, k = gets.chomp().split(\" \").map{|si| next si.to_i() }\n s = gets.chomp().split(\" \").map{|si| next si.to_i() }\n\n m = s[-1]\n t = n - k\n t.times do |tt|\n mm = s[tt] + s[2 * t - 1 - tt]\n m = mm if m < mm\n end\n puts m\nend\n\nif __FILE__ == $0 then\n main()\nend\n"}, {"source_code": "n, k = gets.chomp.split.map(&:to_i)\nss = gets.chomp.split.map(&:to_i)\n\nputs (0...(n - k)).to_a.inject(ss[-1]) { |m, i| [m, ss[i] + ss[2 * (n - k) - i - 1]].max }\n"}, {"source_code": "_,n=gets.split.map(&:to_i)\na=gets.split.map(&:to_i).reverse\nr=0\nn.times.map{|i|r=[r,(a[i]||0)+(a[2*n-1-i]||0)].max}\np r\n"}, {"source_code": "def main()\n\ta = gets.split(\" \").map{|i| i.to_i}\n\tn = a[0]\n\tk = a[1]\n\ts = gets.split(\" \").map{|i| i.to_i}\n\ts = [-1]+s\n\tif n <= k\n\t\tputs s[n]\n\telse\n\t\tsol = s[n]\n\t\tfor i in 0...n-k\n\t\t\tx = s[n-k-i]+s[n-k+1+i]\n\t\t\tif x > sol\n\t\t\t\tsol = x\n\t\t\tend\n\t\tend\n\t\tputs sol\n\tend\nend\nmain()\n\t\n"}, {"source_code": "a = gets.split(\" \").map{|i| i.to_i}\nn = a[0]\nk = a[1]\ns = gets.split(\" \").map{|i| i.to_i}\ns = [-1]+s\nif n <= k\n\tputs s[n]\nelse\n\tsol = s[n]\n\tfor i in 0...n-k\n\t\tx = s[n-k-i]+s[n-k+1+i]\n\t\tif x > sol\n\t\t\tsol = x\n\t\tend\n\tend\n\tputs sol\nend\n\n"}, {"source_code": "n, k = gets.chomp.split(\" \").map(&:to_i)\nss = gets.chomp.split(\" \").map(&:to_i)\nif n.even? then\n min_box = n / 2\n d = (k - min_box) * 2\nelse\n min_box = n / 2 + 1\n d = (k - min_box) * 2 + 1\nend\n\nres = ss.pop(d)\n\nhalf = ss.size / 2\nright = ss.pop(half).reverse\nss.zip(right).each do |a, b|\n res << a + b\nend\nputs res.max\n"}, {"source_code": "_,n=gets.split.map(&:to_i)\na=gets.split.map(&:to_i).reverse\nr=0\nn.times.map{|i|r=[r,(a[i]||0)+(a[2*n-1-i]||0)].max}\np r\n"}, {"source_code": "_,n=gets.split.map(&:to_i)\na=gets.split.map(&:to_i).reverse\nr=0\nn.times.map{|i|r=[r,(a[i]||0)+(a[2*n-1-i]||0)].max}\np r\n"}, {"source_code": "def can_put?(weights, k, s)\n return false if weights.last > s\n return weights[0] <= s if weights.size == 1\n\n used = 0\n \n ind = 0\n last_ind = weights.size - 1\n while ind <= last_ind\n if weights[ind] + weights[last_ind] > s\n if last_ind > ind\n used += 1\n last_ind -= 1\n else\n used += 1\n ind +=1\n last_ind -= 1\n end\n else\n used += 1\n ind +=1\n last_ind -= 1\n end\n end\n # p [s, used]\n used <= k\nend\n\nn,k = $stdin.readline.split.map(&:to_i)\nweights = $stdin.readline.split.map(&:to_i)\ns = ((0) .. (2*weights.last)).bsearch{|sz|\n can_put?(weights, k, sz)\n}\n\nputs s\n"}, {"source_code": "class Solver\n\tdef main\n\t\tn, k = gets.split.map { |x| x.to_i }\n\t\ts = gets.split.map { |x| x.to_i }\n\t\tk = n if k > n\n\n\t\tans = 0\n\t\twhile 2 * k != n do\n\t\t\tans = [ans, s.pop].max\n\t\t\tk -= 1\n\t\t\tn -= 1\n\t\tend\n\n\t\ts = s.zip(s.reverse)\n\t\tans = (s.map { |x| x[0] + x[1] } << ans).max\n\t\tputs ans\n\tend\nend\n\nSolver.new.main\n"}, {"source_code": "_,n=gets.split.map(&:to_i)\na=gets.split.map(&:to_i).reverse\nr=0\nn.times.map{|i|r=[r,(a[i]||0)+(a[2*n-1-i]||0)].max}\np r\n"}], "negative_code": [{"source_code": "def can_put?(weights, k, s)\n return false if weights.last > s\n used = 0\n last_ind = weights.size - 1\n weights.each_index do |ind|\n while weights[ind] + weights[last_ind] > s && last_ind > ind\n last_ind -= 1\n used += 1\n end\n if last_ind == ind\n used += 1\n end\n end\n used <= k\nend\n\nn,k = $stdin.readline.split.map(&:to_i)\nweights = $stdin.readline.split.map(&:to_i)\ns = ((0) .. (2*weights.last)).bsearch{|sz|\n can_put?(weights, k, sz)\n}\n\nputs s"}, {"source_code": "def can_put?(weights, k, s)\n return false if weights.last > s\n\n used = 0\n \n ind = 0\n last_ind = weights.size - 1\n while ind < last_ind\n while weights[ind] + weights[last_ind] > s && last_ind > ind\n last_ind -= 1\n used += 1\n end\n if ind < last_ind && weights[ind] + weights[last_ind] <= s\n used += 1\n ind +=1\n last_ind -= 1\n elsif ind == last_ind\n used += 1\n last_ind -= 1 \n end\n end\n # p [s, used]\n used <= k\nend\n\nn,k = $stdin.readline.split.map(&:to_i)\nweights = $stdin.readline.split.map(&:to_i)\ns = ((0) .. (2*weights.last)).bsearch{|sz|\n can_put?(weights, k, sz)\n}\n\nputs s\n"}, {"source_code": "def can_put?(weights, k, s)\n return false if weights.last > s\n return weights[0] <= s if weights.size == 1\n\n used = 0\n \n ind = 0\n last_ind = weights.size - 1\n while ind < last_ind\n while weights[ind] + weights[last_ind] > s && last_ind > ind\n last_ind -= 1\n used += 1\n end\n if ind < last_ind && weights[ind] + weights[last_ind] <= s\n used += 1\n ind +=1\n last_ind -= 1\n elsif ind == last_ind\n used += 1\n last_ind -= 1 \n end\n end\n # p [s, used]\n used <= k\nend\n\nn,k = $stdin.readline.split.map(&:to_i)\nweights = $stdin.readline.split.map(&:to_i)\ns = ((0) .. (2*weights.last)).bsearch{|sz|\n can_put?(weights, k, sz)\n}\n\nputs s\n"}, {"source_code": "class Solver\n\tdef main\n\t\tn, k = gets.split.map { |x| x.to_i }\n\t\ts = gets.split.map { |x| x.to_i }\n\n\t\tans = 0\n\t\twhile 2 * k != n do\n\t\t\tans = [ans, s.pop].max\n\t\t\tk -= 1\n\t\t\tn -= 1\n\t\tend\n\n\t\ts = s.zip(s.reverse)\n\t\tans = [s.map { |x| x[0] + x[1] } << ans].max\n\t\tans = s.max\n\tend\nend\n\nSolver.new.main\n"}, {"source_code": "class Solver\n\tdef main\n\t\tn, k = gets.split.map { |x| x.to_i }\n\t\ts = gets.split.map { |x| x.to_i }\n\n\t\tans = 0\n\t\twhile 2 * k != n do\n\t\t\tans = [ans, s.pop].max\n\t\t\tk -= 1\n\t\t\tn -= 1\n\t\tend\n\n\t\ts = s.zip(s.reverse)\n\t\tans = [s.map { |x| x[0] + x[1] } << ans].max\n\t\tputs ans\n\tend\nend\n\nSolver.new.main\n"}], "src_uid": "7324428d9e6d808f55ad4f3217046455"} {"nl": {"description": "Robot Doc is located in the hall, with n computers stand in a line, numbered from left to right from 1 to n. Each computer contains exactly one piece of information, each of which Doc wants to get eventually. The computers are equipped with a security system, so to crack the i-th of them, the robot needs to collect at least ai any pieces of information from the other computers. Doc can hack the computer only if he is right next to it.The robot is assembled using modern technologies and can move along the line of computers in either of the two possible directions, but the change of direction requires a large amount of resources from Doc. Tell the minimum number of changes of direction, which the robot will have to make to collect all n parts of information if initially it is next to computer with number 1.It is guaranteed that there exists at least one sequence of the robot's actions, which leads to the collection of all information. Initially Doc doesn't have any pieces of information.", "input_spec": "The first line contains number n (1 ≤ n ≤ 1000). The second line contains n non-negative integers a1, a2, ..., an (0 ≤ ai < n), separated by a space. It is guaranteed that there exists a way for robot to collect all pieces of the information.", "output_spec": "Print a single number — the minimum number of changes in direction that the robot will have to make in order to collect all n parts of information.", "sample_inputs": ["3\n0 2 0", "5\n4 2 3 0 1", "7\n0 3 1 0 5 2 6"], "sample_outputs": ["1", "3", "2"], "notes": "NoteIn the first sample you can assemble all the pieces of information in the optimal manner by assembling first the piece of information in the first computer, then in the third one, then change direction and move to the second one, and then, having 2 pieces of information, collect the last piece.In the second sample to collect all the pieces of information in the optimal manner, Doc can go to the fourth computer and get the piece of information, then go to the fifth computer with one piece and get another one, then go to the second computer in the same manner, then to the third one and finally, to the first one. Changes of direction will take place before moving from the fifth to the second computer, then from the second to the third computer, then from the third to the first computer.In the third sample the optimal order of collecting parts from computers can look like that: 1->3->4->6->2->5->7."}, "positive_code": [{"source_code": "n = gets.chomp.to_i\narr = gets.chomp.split(' ').map(&:to_i)\ndone = Array.new(n, false)\ncount = 0\nans = 0\nn.times do |time|\n next if count == n\n\n n.times do |p|\n pos = (time % 2 == 0) ? p : (n - 1) - p\n next if done[pos]\n next if count < arr[pos]\n done[pos] = true\n count += 1\n end\n\n ans += 1\nend\n\nputs ans - 1\n"}, {"source_code": "n = gets.to_i\ns = [nil]+gets.split(\" \").map(&:to_i)\np = 0\ni = 1\nd = 1\nc = 0\n\nwhile p < n\n\n\tif s[i] && s[i] <= p\n\t\ts[i] = nil\n\t\tp += 1\n\tend\n\n\tbreak if p == n\n\tif i == n && d == 1\n\t\td = -1\n\t\tc += 1\n\telsif i == 1 && d == -1\n\t\td = 1\n\t\tc += 1\n\tend\n\ti += d\nend\n\np c\n"}, {"source_code": "pieces_of_info, change_dir, position, increasing, cleared_computers = 0,0,0,true,0\nn, computers = gets.chomp.to_i, gets.chomp.split(\" \").map(&:to_i)\nloop do\n\tif computers[position] <= pieces_of_info and computers[position] != -1\n\t\tpieces_of_info += 1\n\t\tcleared_computers += 1\n\t\tcomputers[position] = -1\n\tend\n\n\tbreak if n == cleared_computers\n\n\tif increasing\n\t\tif position == n-1\n\t\t\tchange_dir += 1\n\t\t\tposition -= 1\n\t\t\tincreasing = false\n\t\telse\n\t\t\tposition +=1\n\t\tend\n\telse\n\t\tif position == 0\n\t\t\tchange_dir += 1\n\t\t\tposition += 1\n\t\t\tincreasing = true\n\t\telse\n\t\t\tposition -=1\n\t\tend\n\tend\nend\nputs change_dir"}, {"source_code": "\nn = gets.chomp.to_i;\narr = gets.chomp.split.map(&:to_i);\ninfo = 0;\nans = 0;\nn.times do |i|\n\n n.times do |j|\n if (arr[j] <= info) \n info += 1\n arr[j] = n + 2;\n end\n end;\n break if (info >= n);\n arr = arr.reverse;\n ans += 1;\nend\n\nputs ans;\n"}, {"source_code": "n=gets.to_i\nx=gets.split.map{|e| e.to_i}\nans=-1\ni=0\nwhile !x.empty?\n\tans+=1\n\ty=[]\n\twhile !x.empty?\n\t\tif x[0]<=i then\n\t\t\ti+=1\n\t\telse\n\t\t\ty.unshift(x[0])\n\t\tend\n\t\tx.shift()\n\tend\n\tx=y\nend\nputs ans\n\n"}, {"source_code": "def done?(h, n)\n h.length == n\nend\n\ndef main\n n = gets.chomp.to_i\n a = gets.chomp.split.map {|i| i.to_i}\n\n count = 0\n info = 0\n h = {}\n while true\n n.times do |i|\n if not h[i]\n if a[i] <= info\n h[i] = :done\n info += 1\n end\n end\n end\n\n if done? h, n\n break\n end\n\n count += 1\n\n n.times do |i|\n if not h[n-i-1]\n if a[n-i-1] <= info\n h[n-i-1] = :done\n info += 1\n end\n end\n end\n\n if done? h, n\n break\n end\n\n count += 1\n\n end\n puts count\n\nend\n\nmain\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\ninfo, ans = 0, 0\nn.times do |i|\n b = i.even? ? 0.upto(n - 1) : (n - 1).downto(0)\n b.each do |i|\n if a[i] <= info\n info += 1\n a[i] = 1010 \n end\n end\n break if info == n\n ans += 1\nend\nputs ans\n"}, {"source_code": "\nn = gets.strip.to_i\na = gets.strip.split(' ').map(&:to_i)\n\nc = t = 0\nwhile c < a.length\n 0.upto(a.length - 1) do |i|\n if a[i] >= 0 and a[i] <= c\n c += 1\n a[i] = -1\n end\n end\n\n break if c == a.length\n t += 1\n\n (a.length - 1).downto(0) do |i|\n if a[i] >= 0 and a[i] <= c\n c += 1\n a[i] = -1\n end\n end\n\n t += 1 if c < a.length\nend\n\nputs t.to_s"}], "negative_code": [], "src_uid": "9374728643a1ddbe2200a4a125beef26"} {"nl": {"description": "You have an $$$n \\times n$$$ chessboard and $$$k$$$ rooks. Rows of this chessboard are numbered by integers from $$$1$$$ to $$$n$$$ from top to bottom and columns of this chessboard are numbered by integers from $$$1$$$ to $$$n$$$ from left to right. The cell $$$(x, y)$$$ is the cell on the intersection of row $$$x$$$ and collumn $$$y$$$ for $$$1 \\leq x \\leq n$$$ and $$$1 \\leq y \\leq n$$$.The arrangement of rooks on this board is called good, if no rook is beaten by another rook.A rook beats all the rooks that shares the same row or collumn with it.The good arrangement of rooks on this board is called not stable, if it is possible to move one rook to the adjacent cell so arrangement becomes not good. Otherwise, the good arrangement is stable. Here, adjacent cells are the cells that share a side. Such arrangement of $$$3$$$ rooks on the $$$4 \\times 4$$$ chessboard is good, but it is not stable: the rook from $$$(1, 1)$$$ can be moved to the adjacent cell $$$(2, 1)$$$ and rooks on cells $$$(2, 1)$$$ and $$$(2, 4)$$$ will beat each other. Please, find any stable arrangement of $$$k$$$ rooks on the $$$n \\times n$$$ chessboard or report that there is no such arrangement.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 100$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \\leq k \\leq n \\leq 40$$$) — the size of the chessboard and the number of rooks.", "output_spec": "If there is a stable arrangement of $$$k$$$ rooks on the $$$n \\times n$$$ chessboard, output $$$n$$$ lines of symbols . and R. The $$$j$$$-th symbol of the $$$i$$$-th line should be equals R if and only if there is a rook on the cell $$$(i, j)$$$ in your arrangement. If there are multiple solutions, you may output any of them. If there is no stable arrangement, output $$$-1$$$.", "sample_inputs": ["5\n\n3 2\n\n3 3\n\n1 1\n\n5 2\n\n40 33"], "sample_outputs": ["..R\n...\nR..\n-1\nR\n.....\nR....\n.....\n....R\n.....\n-1"], "notes": "NoteIn the first test case, you should find stable arrangement of $$$2$$$ rooks on the $$$3 \\times 3$$$ chessboard. Placing them in cells $$$(3, 1)$$$ and $$$(1, 3)$$$ gives stable arrangement.In the second test case it can be shown that it is impossbile to place $$$3$$$ rooks on the $$$3 \\times 3$$$ chessboard to get stable arrangement."}, "positive_code": [{"source_code": "def solve(n, k)\n limit = k\n\n if (n - (1 - n % 2)) / 2 + 1< k\n return puts '-1'\n else\n result = []\n\n for i in (1..n)\n row = []\n\n for j in (1..n)\n\n if i == j && i % 2 == 1 && limit > 0\n row << 'R'\n limit -= 1\n else\n row << '.'\n end\n end\n\n result << row\n end\n\n result.each { puts _1.join }\n end\n\nend\n\ntests = STDIN.gets.to_i\ninputs = []\n\nfor i in (1..tests)\n n, k = STDIN.gets.split(' ').map(&:to_i)\n\n inputs << [n, k]\nend\n\ninputs.each do |(n, k)|\n solve(n, k)\nend\n"}, {"source_code": "#!/usr/bin/env ruby\n\ngets.to_i.times do\n n, k = gets.split.map(&:to_i)\n\n if (n + 1) / 2 < k\n puts -1\n next\n end\n\n fld = Array.new(n) { '.' * n }\n k.times { |i|\n fld[i * 2][i * 2] = 'R'\n }\n\n puts fld.join \"\\n\"\nend\n"}, {"source_code": "gets\r\n$<.each do\r\n n,k=_1.split.map &:to_i\r\n if n<2*k-1\r\n puts -1\r\n next\r\n end\r\n n.times do |i|\r\n s=\".\"*n\r\n s[i]=?R if i.even? && (i+1)/2 0\r\n row << 'R'\r\n limit -= 1\r\n else\r\n row << '.'\r\n end\r\n end\r\n\r\n result << row\r\n end\r\n\r\n result.each { puts _1.join }\r\n end\r\n\r\nend\r\n\r\ntests = STDIN.gets.to_i\r\ninputs = []\r\n\r\nfor i in (1..tests)\r\n n, k = STDIN.gets.split(' ').map(&:to_i)\r\n\r\n inputs << [n, k]\r\nend\r\n\r\ninputs.each do |(n, k)|\r\n solve(n, k)\r\nend\r\n"}, {"source_code": "def solve\r\n n, k = STDIN.gets.split(' ').map(&:to_i)\r\n\r\n limit = k\r\n\r\n if (n / 3) + 1 < k\r\n return puts '-1'\r\n else\r\n result = []\r\n\r\n for i in (1..n)\r\n row = []\r\n\r\n for j in (1..n)\r\n\r\n if i == j && i % 2 == 1 && limit > 0\r\n row << 'R'\r\n limit -= 1\r\n else\r\n row << '.'\r\n end\r\n end\r\n\r\n result << row\r\n end\r\n\r\n result.each { puts _1.join }\r\n end\r\n\r\nend\r\n\r\ntests = STDIN.gets.to_i\r\n\r\nfor i in (1..tests)\r\n solve\r\nend\r\n"}], "src_uid": "d5549c0627c236d82541a67ccbe7977d"} {"nl": {"description": "Little Petya loves inequations. Help him find n positive integers a1, a2, ..., an, such that the following two conditions are satisfied: a12 + a22 + ... + an2 ≥ x a1 + a2 + ... + an ≤ y", "input_spec": "The first line contains three space-separated integers n, x and y (1 ≤ n ≤ 105, 1 ≤ x ≤ 1012, 1 ≤ y ≤ 106). Please do not use the %lld specificator to read or write 64-bit integers in С++. It is recommended to use cin, cout streams or the %I64d specificator.", "output_spec": "Print n positive integers that satisfy the conditions, one integer per line. If such numbers do not exist, print a single number \"-1\". If there are several solutions, print any of them.", "sample_inputs": ["5 15 15", "2 3 2", "1 99 11"], "sample_outputs": ["4\n4\n1\n1\n2", "-1", "11"], "notes": null}, "positive_code": [{"source_code": "input = $stdin.readline.split(' ')\nn = input[0].to_i; x = input[1].to_i; y=input[2].to_i\na = Array.new(n, 1)\n\nsquare_sum = n; sum = n\ni = 0\n\nuntil square_sum >= x do\n if sum > y then\n puts -1\n exit\n end\n square_sum -= a[i] * a[i]; a[i]+= 1; sum += 1; square_sum += a[i] * a[i]\n\nend\n\nif sum > y then\n puts -1\n exit\nend\n\na.each do |i| print \"#{i} \" end\nprint \"\\n\""}, {"source_code": "n, x, y = gets.split.map(&:to_i)\nt = n - 1\nu = y - t\nif u * u + t >= x and y >= n\n puts \"1\\n\" * (n - 1) + \"#{u}\"\nelse\n puts -1\nend"}, {"source_code": "n,x,y=gets.split.map(&:to_i)\nif y=x\n\t\ta=[1]*(n-1)\n\t\ta<= 20\n d = n / 10 - 1\n n = n % 10 + 10\n end\n k3, k5, k7 = [0, d * 2, 0]\n catch :found do\n (0..5).each do |i|\n (0..5).each do |j|\n (0..5).each do |k|\n if 3 * i + 5 * j + 7 * k == n\n k3 += i\n k5 += j\n k7 += k\n throw :found\n end\n end\n end\n end\n end\n if k3 * 3 + k5 * 5 + k7 * 7 == n_orig\n puts [k3, k5, k7].join(' ')\n else\n puts -1\n end\nend"}], "negative_code": [], "src_uid": "b43dee2f223c869a35b1b11ceb9d2b6b"} {"nl": {"description": "Emily's birthday is next week and Jack has decided to buy a present for her. He knows she loves books so he goes to the local bookshop, where there are n books on sale from one of m genres.In the bookshop, Jack decides to buy two books of different genres.Based on the genre of books on sale in the shop, find the number of options available to Jack for choosing two books of different genres for Emily. Options are considered different if they differ in at least one book.The books are given by indices of their genres. The genres are numbered from 1 to m.", "input_spec": "The first line contains two positive integers n and m (2 ≤ n ≤ 2·105, 2 ≤ m ≤ 10) — the number of books in the bookstore and the number of genres. The second line contains a sequence a1, a2, ..., an, where ai (1 ≤ ai ≤ m) equals the genre of the i-th book. It is guaranteed that for each genre there is at least one book of that genre.", "output_spec": "Print the only integer — the number of ways in which Jack can choose books. It is guaranteed that the answer doesn't exceed the value 2·109.", "sample_inputs": ["4 3\n2 1 3 1", "7 4\n4 2 3 1 2 4 3"], "sample_outputs": ["5", "18"], "notes": "NoteThe answer to the first test sample equals 5 as Sasha can choose: the first and second books, the first and third books, the first and fourth books, the second and third books, the third and fourth books. "}, "positive_code": [{"source_code": "h=Hash.new(0)\nn=gets.to_i\ngets.split.each{|e|h[e.to_i]+=1}\nr=n*(n-1)/2\nh.each{|k,v|r-=v*(v-1)/2}\np r"}, {"source_code": "n, m = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\na = a.sort\nb = []\nfor i in 1..m do\n b[i] = 0\nend\na.each do |val|\n b[val] += 1\nend\n \ncombinations = 0\nfor i in 1..m do\n combinations += b[i] * (n - b[i])\n n -= b[i]\nend\nputs combinations\n"}, {"source_code": "h=Hash.new(0)\nn=gets.to_i\ngets.split.each{|e|h[e.to_i]+=1}\nr=n*(n-1)/2\nh.each{|k,v|r-=v*(v-1)/2}\np r\n"}, {"source_code": "h=Hash.new(0)\nn=gets.to_i\ngets.split.each{|e|h[e.to_i]+=1}\nr=n*(n-1)/2\nh.each{|k,v|r-=v*(v-1)/2}\np r\n"}, {"source_code": "#! ruby\n# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n############################################################\ndef gs() gets.chomp end\ndef gi() gets.to_i end\ndef gss() gs.split end\ndef gis() gss.map(&:to_i) end\ndef nmapf(n,f) n.times.map{ __send__ f } end\ndef arr2d(h,w,v=0) h.times.map{[v] * w} end\ndef ngs(n) nmapf n,:gs end\ndef ngi(n) nmapf n,:gi end\ndef ngss(n) nmapf n,:gss end\ndef ngis(n) nmapf n,:gis end\ndef for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end\ndef nsum(n) n * (n + 1) / 2 end\ndef vcount(d,r=Hash.new(0)) d.inject(r){|r,e| r[e]+=1;r} end\n############################################################\n\nn, m = gis\na = vcount(gis,[0]*m.succ)\n\nputs (1..m).inject(0){|r,i| (i+1..m).inject(r){|r,j|r+a[i]*a[j]} } \n"}, {"source_code": "h=Hash.new(0)\nn=gets.to_i\ngets.split.each{|e|h[e.to_i]+=1}\nr=n*(n-1)/2\nh.each{|k,v|r-=v*(v-1)/2}\np r\n"}, {"source_code": "h=Hash.new(0)\nn=gets.to_i\ngets.split.each{|e|h[e.to_i]+=1}\nr=n*(n-1)/2\nh.each{|k,v|r-=v*(v-1)/2}\np r\n"}, {"source_code": "data1=gets.chomp.split\nn=data1[0].to_i\nm=data1[1].to_i\n\ndata2=gets.chomp.split\n\nbooks=[]\n\ndata2.each do |i|\n\tbooks.push(i.to_i)\nend\n\nbooks.sort!\n\nbooks1=[]\nb=books[0]\ns=0\nbooks.each do |item|\n\tif item == b\n\t\ts+=1\n\telse\n\t\tbooks1.push(s)\n\t\tb=item\n\t\ts=1\n\tend\nend\n\nbooks1.push(s)\n\nsum=0\nsum+=(n*(n-1))/2\n\nfor i in(0..books1.length-1)\n\tsum-=(books1[i]*(books1[i]-1))/2\nend\n\nputs sum"}, {"source_code": "#!/usr/bin/env ruby\n\nn, m = gets.split.map(&:to_i)\nfreqs = Hash.new(0)\na = gets.split.map(&:to_i)\na.each do |_a|\n freqs[_a] += 1\nend\nans = n * (n - 1) / 2\nfreqs.each_pair do |k, v|\n ans -= (v * (v - 1) / 2)\nend\nputs ans\n"}, {"source_code": "arr1 = gets.chomp.split(/ /)\nt = arr1[0].to_i\nn = arr1[1].to_i\narr2 = gets.chomp.split(/ /)\narr2.sort!\n#puts arr2\narr3 = Array.new(n, 0)\nindex=0\nloop do \n\tbreak if index>=t\n\tarr3[arr2[index].to_i-1]+=1\n\tindex+=1\nend\n\nsum=0\ncount=0\n\nindex=0\nloop do \n\tbreak if index>=n-1\n\tcount = count + arr3[index]\n\tsum = sum + arr3[index]*(t-count)\n\tindex+=1\n\t#puts \"Index is #{index} and sum is #{sum} and count is #{count}\"\nend\n\nputs sum"}, {"source_code": "h=Hash.new(0)\nn=gets.to_i\ngets.split.each{|e|h[e.to_i]+=1}\nr=n*(n-1)/2\nh.each{|k,v|r-=v*(v-1)/2}\np r\n"}, {"source_code": "n,m = gets.split(\" \").map {|x| x.to_i }\na = gets.split(\" \").map{|x| x.to_i}\nb = Array.new(m, 0)\n\nremain = n\na.each do |x|\n b[x-1] += 1\nend\n\nresult = 0\nb.each do |x|\n remain -= x\n result += remain* x\nend\n\nputs result"}, {"source_code": "n, m = gets.chomp.split(\" \").map(&:to_i)\nbooks = gets.chomp.split(\" \").map(&:to_i)\ndic = Hash.new(0)\nres = 0\nbooks.each do |k|\n dic[k] += 1\nend\n(1..m).to_a.combination(2) do |i, j|\n res += dic[i] * dic[j]\nend\nputs res\n"}, {"source_code": "n, m = gets.split(' ')\narray = gets.split(' ')\nsta = Array.new(m.to_i, 0)\nfor item in array\n sta[item.to_i - 1] += 1\nend\nans = 0\nfor i in 0..m.to_i-1\n for j in i+1..m.to_i-1\n ans += sta[i] * sta[j]\n end\nend\nputs ans\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\nbooks = gets.split.map(&:to_i)\ngenre = Array.new(m)\nfor book in books\n if genre[book] == nil\n genre[book] = 1\n else\n genre[book] += 1\n end\nend\n\ncount = 0\nfor i in (1..m-1)\n for j in (i+1..m)\n count += genre[i] * genre[j]\n end\nend\n\nputs count\n"}, {"source_code": "# http://codeforces.com/problemset/problem/378/B\nn, m = gets.chomp.split.map(&:to_i)\n\nlist = gets.chomp.split.map(&:to_i)\nhash = Hash.new { |hash, key| hash[key] = 0 }\n\nfor i in 0..(list.length - 1)\n hash[list[i]] += 1\nend\n\nnew_list = hash.values\nresult = 0\nfor i in 0..(new_list.length - 1)\n for j in (i+1)..(new_list.length - 1)\n result += new_list[j] * new_list[i]\n end\nend\n\nputs result\n"}, {"source_code": "h=Hash.new(0)\nn=gets.to_i\ngets.split.each{|e|h[e.to_i]+=1}\nr=n*(n-1)/2\nh.each{|k,v|r-=v*(v-1)/2}\np r\n"}, {"source_code": "\nn = 0\nm = Array.new\n\nargs = gets.chomp.split(/ /)\nm = args.last.to_i\n\ngenre = Array.new\nfor i in 0..m do genre[i] = 0 end\n\narr = gets.chomp.split(/ /)\narr.each {|el| genre[el.to_i - 1] = genre[el.to_i - 1] + 1}\n\ns = genre[0]\nans = 0\nfor i in 1..m do\n\tans = ans + s * genre[i]\n\ts = s + genre[i];\nend\n\nprint ans"}, {"source_code": "h=Hash.new(0)\nn=gets.to_i\ngets.split.each{|e|h[e.to_i]+=1}\nr=n*(n-1)/2\nh.each{|k,v|r-=v*(v-1)/2}\np r\n"}, {"source_code": "h=Hash.new(0)\nn=gets.to_i\ngets.split.each{|e|h[e.to_i]+=1}\nr=n*(n-1)/2\nh.each{|k,v|r-=v*(v-1)/2}\np r\n"}], "negative_code": [], "src_uid": "e2ff228091ca476926b8e905f1bc8dff"} {"nl": {"description": "You are given two arrays $$$a$$$ and $$$b$$$, both of length $$$n$$$.Let's define a function $$$f(l, r) = \\sum\\limits_{l \\le i \\le r} a_i \\cdot b_i$$$.Your task is to reorder the elements (choose an arbitrary order of elements) of the array $$$b$$$ to minimize the value of $$$\\sum\\limits_{1 \\le l \\le r \\le n} f(l, r)$$$. Since the answer can be very large, you have to print it modulo $$$998244353$$$. Note that you should minimize the answer but not its remainder.", "input_spec": "The first line of the input contains one integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) — the number of elements in $$$a$$$ and $$$b$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^6$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. The third line of the input contains $$$n$$$ integers $$$b_1, b_2, \\dots, b_n$$$ ($$$1 \\le b_j \\le 10^6$$$), where $$$b_j$$$ is the $$$j$$$-th element of $$$b$$$.", "output_spec": "Print one integer — the minimum possible value of $$$\\sum\\limits_{1 \\le l \\le r \\le n} f(l, r)$$$ after rearranging elements of $$$b$$$, taken modulo $$$998244353$$$. Note that you should minimize the answer but not its remainder.", "sample_inputs": ["5\n1 8 7 2 4\n9 7 2 9 3", "1\n1000000\n1000000", "2\n1 3\n4 2"], "sample_outputs": ["646", "757402647", "20"], "notes": null}, "positive_code": [{"source_code": "n=gets.to_i\na=gets.split.map &:to_i\nb=gets.split.map &:to_i\nc=[0]*n\ni=0;\nj=n-1\nwhile i<=j\n c[i]=c[j]=(n-i)*(i+1)\n i+=1\n j-=1\nend\n\ni=-1\nc.map!{|e| e*a[i+=1]}.sort!\nb.sort!.reverse!\n\nres=0\nc.zip(b){|x, y| res+=x*y%998244353 }\nputs res%998244353\n"}], "negative_code": [{"source_code": "n=gets.to_i\na=gets.split.map &:to_i\nb=gets.split.map &:to_i\nc=[0]*n\ni=0;\nj=n-1\nwhile i<=j\n c[i]=c[j]=(n-i)*(i+1)\n i+=1\n j-=1\nend\n\ni=-1\nc.map!{|e| e*a[i+=1]}.sort!\nb.sort!.reverse!\n\nres=0\nc.zip(b){|x, y| res+=x*y%998244353 }\nputs res\n"}, {"source_code": "n=gets.to_i\na=gets.split.map &:to_i\nb=gets.split.map &:to_i\nc=[0]*n\nx=0\ni=0;\nj=n-1\nm=n\nwhile i<=j\n c[i]=c[j]=m+x\n m-=1\n x=c[i]-i-1\n i+=1\n j-=1\nend\n\ni=-1\nc.map!{|e| e*a[i+=1]}.sort!\nb.sort!.reverse!\n\nres=0\nc.zip(b){|x, y| res+=x*y%998244353 }\nputs res\n"}], "src_uid": "93431bdae447bb96a2f0f5fa0c6e11e0"} {"nl": {"description": "As it's the first of April, Heidi is suspecting that the news she reads today are fake, and she does not want to look silly in front of all the contestants. She knows that a newspiece is fake if it contains heidi as a subsequence. Help Heidi assess whether the given piece is true, but please be discreet about it...", "input_spec": "The first and only line of input contains a single nonempty string s of length at most 1000 composed of lowercase letters (a-z).", "output_spec": "Output YES if the string s contains heidi as a subsequence and NO otherwise.", "sample_inputs": ["abcheaibcdi", "hiedi"], "sample_outputs": ["YES", "NO"], "notes": "NoteA string s contains another string p as a subsequence if it is possible to delete some characters from s and obtain p."}, "positive_code": [{"source_code": "str = gets().to_s\nif str.include?(\"h\")\n nul = \"\"\n str = nul.insert(0, str[str.index(\"h\")+1...str.size])\n if str.include?(\"e\")\n nul = \"\"\n str = nul.insert(0, str[str.index(\"e\")+1...str.size])\n if str.include?(\"i\")\n nul = \"\"\n str = nul.insert(0, str[str.index(\"i\")+1...str.size])\n if str.include?(\"d\")\n nul = \"\"\n str = nul.insert(0, str[str.index(\"d\")+1...str.size])\n if str.include?(\"i\")\n nul = \"\"\n puts \"YES\"\n else\n puts \"NO\"\n end\n else\n puts \"NO\"\n end\n else\n puts \"NO\"\n end\n else\n puts \"NO\"\n end\nelse\n puts \"NO\"\nend\n"}], "negative_code": [{"source_code": "if gets().to_s.include?('heidi')\n puts \"YES\"\nelse\n puts \"NO\"\nend"}, {"source_code": "if gets().to_s.include?('heidi')\n puts \"NO\"\nelse\n puts \"YES\"\nend"}], "src_uid": "a457e22fc8ff882c15ac57bca6960657"} {"nl": {"description": "You are given a rooted tree consisting of $$$n$$$ vertices. Vertices are numbered from $$$1$$$ to $$$n$$$. Any vertex can be the root of a tree.A tree is a connected undirected graph without cycles. A rooted tree is a tree with a selected vertex, which is called the root.The tree is specified by an array of ancestors $$$b$$$ containing $$$n$$$ numbers: $$$b_i$$$ is an ancestor of the vertex with the number $$$i$$$. The ancestor of a vertex $$$u$$$ is a vertex that is the next vertex on a simple path from $$$u$$$ to the root. For example, on the simple path from $$$5$$$ to $$$3$$$ (the root), the next vertex would be $$$1$$$, so the ancestor of $$$5$$$ is $$$1$$$.The root has no ancestor, so for it, the value of $$$b_i$$$ is $$$i$$$ (the root is the only vertex for which $$$b_i=i$$$).For example, if $$$n=5$$$ and $$$b=[3, 1, 3, 3, 1]$$$, then the tree looks like this. An example of a rooted tree for $$$n=5$$$, the root of the tree is a vertex number $$$3$$$. You are given an array $$$p$$$ — a permutation of the vertices of the tree. If it is possible, assign any positive integer weights on the edges, so that the vertices sorted by distance from the root would form the given permutation $$$p$$$.In other words, for a given permutation of vertices $$$p$$$, it is necessary to choose such edge weights so that the condition $$$dist[p_i]<dist[p_{i+1}]$$$ is true for each $$$i$$$ from $$$1$$$ to $$$n-1$$$. $$$dist[u]$$$ is a sum of the weights of the edges on the path from the root to $$$u$$$. In particular, $$$dist[u]=0$$$ if the vertex $$$u$$$ is the root of the tree.For example, assume that $$$p=[3, 1, 2, 5, 4]$$$. In this case, the following edge weights satisfy this permutation: the edge ($$$3, 4$$$) has a weight of $$$102$$$; the edge ($$$3, 1$$$) has weight of $$$1$$$; the edge ($$$1, 2$$$) has a weight of $$$10$$$; the edge ($$$1, 5$$$) has a weight of $$$100$$$. The array of distances from the root looks like: $$$dist=[1,11,0,102,101]$$$. The vertices sorted by increasing the distance from the root form the given permutation $$$p$$$.Print the required edge weights or determine that there is no suitable way to assign weights. If there are several solutions, then print any of them.", "input_spec": "The first line of input data contains an integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of input data sets in the test. Each test case consists of three lines. The first of them contains an integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$). It is the number of vertices in the tree. The second line contains $$$n$$$ integers $$$b_1, b_2, \\dots, b_n$$$ ($$$1 \\le b_i \\le n$$$). It is guaranteed that the $$$b$$$ array encodes some rooted tree. The third line contains the given permutation $$$p$$$: $$$n$$$ of different integers $$$p_1, p_2, \\dots, p_n$$$ ($$$1 \\le p_i \\le n$$$). It is guaranteed that the sum of the values $$$n$$$ over all test cases in the test does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each set of input data print the answer on a separate line. If the solution exists, print an array of $$$n$$$ integers $$$w_1, w_2, \\dots, w_n$$$, where $$$w_i$$$ is the weight of the edge that leads from $$$b_i$$$ to $$$i$$$. For the root there is no such edge, so use the value $$$w_i=0$$$. For all other vertices, the values of $$$w_i$$$ must satisfy the inequality $$$1 \\le w_i \\le 10^9$$$. There can be equal numbers among $$$w_i$$$ values, but all sums of weights of edges from the root to vertices must be different and satisfy the given permutation. If there are several solutions, output any of them. If no solution exists, output -1.", "sample_inputs": ["4\n5\n3 1 3 3 1\n3 1 2 5 4\n3\n1 1 2\n3 1 2\n7\n1 1 2 3 4 5 6\n1 2 3 4 5 6 7\n6\n4 4 4 4 1 1\n4 2 1 5 6 3"], "sample_outputs": ["1 10 0 102 100\n-1\n0 3 100 1 1 2 4\n6 5 10 0 2 3"], "notes": "NoteThe first set of input data of the example is analyzed in the main part of the statement.In the second set of input data of the example, it is impossible to assign the positive weights to obtain a given permutation of vertices."}, "positive_code": [{"source_code": "t = gets.to_i\r\nans = []\r\nt.times do\r\n n = gets.to_i\r\n b = gets.split.map(&:to_i)\r\n b.unshift(-1)\r\n p = gets.split.map(&:to_i)\r\n g = Array.new(n+1){Array.new}\r\n root = -1\r\n 1.upto(n) do |i|\r\n if b[i] == i\r\n root = i\r\n else\r\n g[b[i]] << i\r\n end\r\n end\r\n pp = []\r\n n.times do |i|\r\n pp << [p[i],i]\r\n end\r\n pp.sort_by!{|pi,i|i}\r\n f = true\r\n f = false if pp[0][0] != root\r\n\r\n dist = Hash.new(-2)\r\n dist[root] = 0\r\n arr = []\r\n max = -1\r\n pp.each do |pi,i|\r\n pre = b[pi]\r\n if dist[pre] == -2\r\n f = false\r\n else\r\n d = max - dist[pre] + 1\r\n arr[pi-1] = d\r\n max += 1\r\n dist[pi] = max\r\n end\r\n end\r\n if f\r\n ans << arr\r\n else\r\n ans << [-1]\r\n end\r\nend\r\nans.each do |arr|\r\n puts arr.join(\" \")\r\nend"}], "negative_code": [], "src_uid": "4b512c39e71e34064c820958dde9f4a1"} {"nl": {"description": "You are given an array $$$a$$$ of $$$n$$$ integers.You want to make all elements of $$$a$$$ equal to zero by doing the following operation exactly three times: Select a segment, for each number in this segment we can add a multiple of $$$len$$$ to it, where $$$len$$$ is the length of this segment (added integers can be different). It can be proven that it is always possible to make all elements of $$$a$$$ equal to zero.", "input_spec": "The first line contains one integer $$$n$$$ ($$$1 \\le n \\le 100\\,000$$$): the number of elements of the array. The second line contains $$$n$$$ elements of an array $$$a$$$ separated by spaces: $$$a_1, a_2, \\dots, a_n$$$ ($$$-10^9 \\le a_i \\le 10^9$$$).", "output_spec": "The output should contain six lines representing three operations. For each operation, print two lines: The first line contains two integers $$$l$$$, $$$r$$$ ($$$1 \\le l \\le r \\le n$$$): the bounds of the selected segment. The second line contains $$$r-l+1$$$ integers $$$b_l, b_{l+1}, \\dots, b_r$$$ ($$$-10^{18} \\le b_i \\le 10^{18}$$$): the numbers to add to $$$a_l, a_{l+1}, \\ldots, a_r$$$, respectively; $$$b_i$$$ should be divisible by $$$r - l + 1$$$.", "sample_inputs": ["4\n1 3 2 4"], "sample_outputs": ["1 1 \n-1\n3 4\n4 2\n2 4\n-3 -6 -6"], "notes": null}, "positive_code": [{"source_code": "n=gets.to_i\na=gets.split(\" \").map(&:to_i)\n\nif n == 1\n puts \"1 1\"\n puts \"0\"\n puts \"1 1\"\n puts \"0\"\n puts \"1 1\"\n puts -a[0]\n exit\nend\n# a2 = a.dup\n\nc = []\nb = []\n(n-1).times do |i|\n c[i] = a[i]*(n-1)\n b[i] = -a[i]*n\n\n # a2[i] += c[i] + b[i]\nend\nb[n-1] = 0\n\n# a2[n-1] += -a[n-1]\n\nputs \"1 #{n}\"\nputs b.join(\" \")\nputs \"1 #{n-1}\"\nputs c.join(\" \")\nputs \"#{n} #{n}\"\nputs -a[n-1]\n\n\n# p a2\n"}, {"source_code": "n = gets.to_i\na = gets.split.map &:to_i\nif n == 1\n puts \"1 1\\n\", -a[0], \"\\n1 1\\n0\\n1 1\\n0\"\nelse\n print \"1 \", n, \"\\n\"\n (0...n - 1).each do |i|\n print -a[i] * n, \" \"\n end\n print \"0\\n1 \", n - 1, \"\\n\"\n (0...n - 1).each do |i|\n print a[i] * (n - 1), \" \"\n end\n print \"\\n\", n, \" \", n, \"\\n\", -a[-1], \"\\n\"\nend"}, {"source_code": "n = gets.to_i\na = gets.chomp.split(\" \").map(&:to_i)\nif n != 1\n puts \"1 #{n}\"\n n.times do |i|\n v = ((n-1) - a[i] % (n-1)) * n\n a[i] += v\n print \"#{v} \"\n end\n puts \"\"\n puts \"1 #{n-1}\"\n (n-1).times do |i|\n print \"#{-a[i]} \"\n end\n puts \"\"\n puts \"#{n} #{n}\"\n puts -a[n-1]\nelse\n puts \"1 1\"\n puts -a[0]\n puts \"1 1\"\n puts \"0\"\n puts \"1 1\"\n puts \"0\"\nend"}], "negative_code": [{"source_code": "n=gets.to_i\na=gets.split(\" \").map(&:to_i)\n# a2 = a.dup\n\nc = []\nb = []\n(n-1).times do |i|\n c[i] = a[i]*(n-1)\n b[i] = -a[i]*n\n\n # a2[i] += c[i] + b[i]\nend\nb[n-1] = 0\n\n# a2[n-1] += -a[n-1]\n\nputs \"1 #{n}\"\nputs b.join(\" \")\nputs \"1 #{n-1}\"\nputs c.join(\" \")\nputs \"#{n} #{n}\"\nputs -a[n-1]\n\n\n# p a2\n"}], "src_uid": "d15a758cfdd7a627822fe8be7db4f60b"} {"nl": {"description": "In this problem, your task is to use ASCII graphics to paint a cardiogram. A cardiogram is a polyline with the following corners:That is, a cardiogram is fully defined by a sequence of positive integers a1, a2, ..., an.Your task is to paint a cardiogram by given sequence ai.", "input_spec": "The first line contains integer n (2 ≤ n ≤ 1000). The next line contains the sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 1000). It is guaranteed that the sum of all ai doesn't exceed 1000.", "output_spec": "Print max |yi - yj| lines (where yk is the y coordinate of the k-th point of the polyline), in each line print characters. Each character must equal either « / » (slash), « \\ » (backslash), « » (space). The printed image must be the image of the given polyline. Please study the test samples for better understanding of how to print a cardiogram. Note that in this problem the checker checks your answer taking spaces into consideration. Do not print any extra characters. Remember that the wrong answer to the first pretest doesn't give you a penalty.", "sample_inputs": ["5\n3 1 2 5 1", "3\n1 5 1"], "sample_outputs": ["/ \n\n\\\n \n \n / \n\n\\\n\n / \n \n\\\n \n \n / \n \n\\\n \n\n / \n \n\\\n \n \n\\\n\n /", "/ \n\n\\\n \n \n\\\n \n \n\\\n \n \n\\\n \n \n\\\n\n /"], "notes": "NoteDue to the technical reasons the answers for the samples cannot be copied from the statement. We've attached two text documents with the answers below.http://assets.codeforces.com/rounds/435/1.txthttp://assets.codeforces.com/rounds/435/2.txt"}, "positive_code": [{"source_code": "\n# Print cardiogram given 2d array\ndef print_cardiogram(out)\n for i in 0..(out.length - 1) do\n puts out[i].join(\"\")\n end\nend\n\n# Read Input\nn = gets.strip.to_i\na = gets.strip.split.map{|s| s.to_i}\n\n# Find output size and starting point\nhd = 0; pp = 0; pn = 0; w = 0;\nfor i in 0..(a.length - 1) do \n if i.even?\n hd = hd + a[i]\n pp = hd if hd > pp\n else\n hd = hd - a[i]\n pn = hd if hd < pn\n end\n w = w + a[i]\nend\nh = [pp,0].max - [pn,0].min \ny = pp - 1; x = 0;\n\n# Create out array\nout = Array.new(h) {Array.new(w){\" \"}}\n\n# Fill out array\nfor i in 0..(a.length - 1) do\n for j in 1..a[i] do\n if i.even?\n out[y][x] = \"/\"\n y = y - 1 if j < a[i]\n else\n out[y][x] = \"\\\\\"\n y = y + 1 if j < a[i]\n end\n x = x + 1\n end\nend\n\n# Print cardiogram\nprint_cardiogram(out)"}, {"source_code": "\n\nn = gets.to_i\ntab = gets.split.map(&:to_i)\n\ncoords = [[0,0]]\n\n(0..tab.size-1).each do |i|\n if i % 2 == 0\n coords << [coords[i][0] + tab[i], coords[i][1] + tab[i]]\n else\n coords << [coords[i][0] + tab[i], coords[i][1] - tab[i]]\n end\nend\n\n\nx = coords.map{|a| a[0]}\ny = coords.map{|a| a[1]}\n\nx_diff = x.max\ny_diff = y.max - y.min\n\nzero = y.max\n\ncoords.map!{|x| [x[0], zero-x[1]]}\n\nresult = []\n(y_diff+1).times do\n result << [' '] * x_diff\nend\n\n\n\n(0..coords.size-2).each do |i|\n if i % 2 == 0\n temp = 0\n while coords[i][0] + temp != coords[i+1][0]\n result[coords[i][1]-temp][coords[i][0]+temp] = \"/\"\n temp += 1\n end\n else\n temp = 0\n while coords[i][0] + temp != coords[i+1][0]\n result[coords[i][1]+temp+1][coords[i][0]+temp] = \"\\\\\"\n temp += 1\n end\n end\n\nend\n\n\nresult[1..-1].each do |row|\n puts row.join(\"\")\nend\n\n\n"}], "negative_code": [], "src_uid": "76285a60c21538db17d268a5e06c2270"} {"nl": {"description": "Recently Vasya got interested in finding extra-terrestrial intelligence. He made a simple extra-terrestrial signals’ receiver and was keeping a record of the signals for n days in a row. Each of those n days Vasya wrote a 1 in his notebook if he had received a signal that day and a 0 if he hadn’t. Vasya thinks that he has found extra-terrestrial intelligence if there is a system in the way the signals has been received, i.e. if all the intervals between successive signals are equal. Otherwise, Vasya thinks that the signals were sent by some stupid aliens no one cares about. Help Vasya to deduce from the information given by the receiver if he has found extra-terrestrial intelligence or not.", "input_spec": "The first line contains integer n (3 ≤ n ≤ 100) — amount of days during which Vasya checked if there were any signals. The second line contains n characters 1 or 0 — the record Vasya kept each of those n days. It’s guaranteed that the given record sequence contains at least three 1s.", "output_spec": "If Vasya has found extra-terrestrial intelligence, output YES, otherwise output NO.", "sample_inputs": ["8\n00111000", "7\n1001011", "7\n1010100"], "sample_outputs": ["YES", "NO", "YES"], "notes": null}, "positive_code": [{"source_code": "open('output.txt','w').puts open('input.txt').gets(p)=~/\\n0*1(0*1)?\\1*0*$/?:YES: :NO\n\n"}, {"source_code": "open('output.txt','w').puts open('input.txt').gets(p)=~/\\n0*1(0*1)?\\1*0*$/?:YES: :NO\n"}, {"source_code": "open('output.txt','w').puts open('input.txt').gets(p)=~/\\n0*1(0*1)?\\1*0*$/?:YES: :NO\n\n"}, {"source_code": "open('output.txt','w').puts open('input.txt').gets(p)=~/\\n0*1(0*1)?\\1*0*$/?:YES: :NO\n\n"}, {"source_code": "open('output.txt','w').puts open('input.txt').gets(p)=~/\\n0*1(0*1)?\\1*0*$/?:YES: :NO\n\n"}, {"source_code": "open('output.txt','w').puts open('input.txt').gets(p)=~/\\n0*1(0*1)?\\1*0*$/?:YES: :NO"}, {"source_code": "open('output.txt','w').puts open('input.txt').gets(p)=~/\\n0*1(0*1)?\\1*0*$/?:YES: :NO\n\n"}, {"source_code": "open('output.txt','w').puts open('input.txt').gets(p)=~/\\n0*1(0*1)?\\1*0*$/?:YES: :NO\n\n"}, {"source_code": "f,g=open(\"input.txt\",\"r\"),open(\"output.txt\",\"w\")\nf.gets\ng.puts(f.gets[/1.+1/].chop.scan(/10*/).map(&:size).uniq.size<2 ? \"YES\" : \"NO\")\nf.close\ng.close"}, {"source_code": "a, b = [], []\nopen('input.txt').read.split(\"\\n\")[1].each_char.with_index { |c, i| a.push i if c == \"1\" }\na[0].step(a[-1], a[1] - a[0]) { |e| b.push << e }\nopen('output.txt','w').print a == b ? :YES : :NO"}, {"source_code": "open('output.txt','w').puts open('input.txt').gets(p)=~/\\n0*1(0*1)?\\1*0*$/?:YES: :NO\n\n"}, {"source_code": "File.open('input.txt', 'r') do |inp|\n\tFile.open('output.txt', 'w') do |out|\n\t\ta = inp.readlines[1].chars.to_a.each_with_index.reduce([]) { |s, (x, i)| s <<= i if x == '1'; s }\n\t\tout.write %w[YES NO][Array(1...a.length).reduce([]) { |s, x| s << a[x] - a[x - 1] }.sort.uniq.length == 1 ? 0 : 1]\n\tend\nend"}, {"source_code": "$stdin = File.open(\"input.txt\", \"r\")\n$stdout = File.open(\"output.txt\", \"w\")\nn = gets.to_i\ns = gets\nones = (0...n).select{|pos| s[pos]==\"1\"}\ndiff = ones.map.with_index(1){|e,i| ones[i]-e if ones[i]}.compact\nputs (diff.all?{|x| x == diff[0]} ? \"YES\" : \"NO\")\n"}, {"source_code": "open('output.txt','w').puts open('input.txt').gets(p)=~/\\n0*1(0*1)?\\1*0*$/?:YES: :NO\n"}, {"source_code": "open('output.txt','w').puts open('input.txt').gets(p)=~/\\n0*1(0*1)?\\1*0*$/?:YES: :NO\n\n"}], "negative_code": [{"source_code": "a, b = [], []\np STDIN.read.split(\"\\n\")[1]"}, {"source_code": "File.open('input.txt').each { |line|\n puts line\n}"}, {"source_code": "File.open('input.txt').each { |line|\n open('output.txt','w').puts line\n}"}, {"source_code": "a, b = [], []; gets\n#test\n# gets.strip.each_char.with_index { |c, i| a.push i if c == \"1\" }\n#a[0].step(a[-1], a[1] - a[0]) { |e| b.push << e }\n#print a == b ? :YES : :NO\n"}, {"source_code": "puts RUBY_VERSION"}, {"source_code": "open('output.txt','w').puts open('input.txt').gets(p)=~/^[^1]*1(0*1)?\\1*0*$/?:YES: :NO\n"}, {"source_code": "open('output.txt','w').puts open('input.txt').gets(p)=~/^[^1]*(10*)\\1*0*$/?:YES: :NO\n"}], "src_uid": "4fc1ca3517168842cc85d74ba0066598"} {"nl": {"description": "Luca has a cypher made up of a sequence of $$$n$$$ wheels, each with a digit $$$a_i$$$ written on it. On the $$$i$$$-th wheel, he made $$$b_i$$$ moves. Each move is one of two types: up move (denoted by $$$\\texttt{U}$$$): it increases the $$$i$$$-th digit by $$$1$$$. After applying the up move on $$$9$$$, it becomes $$$0$$$. down move (denoted by $$$\\texttt{D}$$$): it decreases the $$$i$$$-th digit by $$$1$$$. After applying the down move on $$$0$$$, it becomes $$$9$$$. Example for $$$n=4$$$. The current sequence is 0 0 0 0. Luca knows the final sequence of wheels and the moves for each wheel. Help him find the original sequence and crack the cypher.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 100$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 100$$$) — the number of wheels. The second line contains $$$n$$$ integers $$$a_i$$$ ($$$0 \\leq a_i \\leq 9$$$) — the digit shown on the $$$i$$$-th wheel after all moves have been performed. Then $$$n$$$ lines follow, the $$$i$$$-th of which contains the integer $$$b_i$$$ ($$$1 \\leq b_i \\leq 10$$$) and $$$b_i$$$ characters that are either $$$\\texttt{U}$$$ or $$$\\texttt{D}$$$ — the number of moves performed on the $$$i$$$-th wheel, and the moves performed. $$$\\texttt{U}$$$ and $$$\\texttt{D}$$$ represent an up move and a down move respectively.", "output_spec": "For each test case, output $$$n$$$ space-separated digits  — the initial sequence of the cypher.", "sample_inputs": ["3\n\n3\n\n9 3 1\n\n3 DDD\n\n4 UDUU\n\n2 DU\n\n2\n\n0 9\n\n9 DDDDDDDDD\n\n9 UUUUUUUUU\n\n5\n\n0 5 9 8 3\n\n10 UUUUUUUUUU\n\n3 UUD\n\n8 UUDUUDDD\n\n10 UUDUUDUDDU\n\n4 UUUU"], "sample_outputs": ["2 1 1 \n9 0 \n0 4 9 6 9"], "notes": "NoteIn the first test case, we can prove that initial sequence was $$$[2,1,1]$$$. In that case, the following moves were performed: On the first wheel: $$$2 \\xrightarrow[\\texttt{D}]{} 1 \\xrightarrow[\\texttt{D}]{} 0 \\xrightarrow[\\texttt{D}]{} 9$$$. On the second wheel: $$$1 \\xrightarrow[\\texttt{U}]{} 2 \\xrightarrow[\\texttt{D}]{} 1 \\xrightarrow[\\texttt{U}]{} 2 \\xrightarrow[\\texttt{U}]{} 3$$$. On the third wheel: $$$1 \\xrightarrow[\\texttt{D}]{} 0 \\xrightarrow[\\texttt{U}]{} 1$$$. The final sequence was $$$[9,3,1]$$$, which matches the input."}, "positive_code": [{"source_code": "#!/usr/bin/env ruby\n\n# https://codeforces.com/contest/1703/problem/C\n\nt = gets.chomp.to_i # tests\n\n(1..t).each do |numtest|\n n = gets.chomp.to_i # wheels\n l = gets.chomp # final state\n state = l.split(' ').map(&:to_i)\n #puts \"final state=#{state}\"\n (0..n-1).each do |numwheel|\n #puts \"numwheel=#{numwheel}\"\n m = gets.chomp\n pm = m.split(' ')\n #puts \"pm=#{pm}\"\n (0..pm[0].to_i-1 ).each do |i|\n #puts \"i=#{i}\"\n if pm[1][i] == 'U'\n state[numwheel] = state[numwheel] == 0 ? 9 : state[numwheel] - 1\n elsif pm[1][i] == 'D'\n state[numwheel] = state[numwheel] == 9 ? 0 : state[numwheel] + 1\n else\n echo \"Problem with character #{i} that is #{pm[1][i]}\"\n exit 1;\n end\n #puts \"state=#{state}\"\n end\n end\n puts state.join(' ')\n\nend\n\n\n"}, {"source_code": "t = gets.to_i\n\nt.times do\n n = gets.to_i\n a = gets.split.map &:to_i\n n.times do |i|\n b = gets.chomp\n a[i] = (a[i] + b.count('D') - b.count('U')) % 10\n end\n puts a*' '\nend\n"}, {"source_code": "gets.to_i.times {\n\tn = gets.to_i\n\tk = gets.split.map(&:to_i)\n\tn.times { |x|\n\t\tm, ud = gets.split\n\t\tu = ud.chars.count {|x| x == 'U'}\n\t\td = m.to_i - u\n\t\tprint (k[x] - u + d)%10, ' '\n\t}\n\tputs ''\n} "}, {"source_code": "def cypher(n, str, a)\n i = 0\n res = a\n while n-1 >= 0\n if str[i] == \"D\"\n res += 1\n else\n res -= 1\n end\n n -= 1\n i += 1\n end\n res % 10\nend\nt = gets.chomp.to_i\nfinal = []\n\n(0..t-1).each do |i|\n inputs = []\n x = 0\n n = gets.chomp.to_i\n arr = gets.chomp\n arr = arr.split(\" \").map(&:to_i)\n\n (0..n-1).each do |j|\n combination = gets.chomp\n combination = combination.split(\" \")\n combination.push(arr[x])\n inputs.push(combination)\n x+=1\n end\n final.push(inputs)\nend\n\nfinal.each do |input|\n ans = \"\"\n input.each do |i|\n x = cypher(i[0].to_i, i[1], i[2])\n ans += \"#{x} \"\n end\n\n puts ans.strip\nend\n\n"}], "negative_code": [], "src_uid": "fd47f1eb701077abc5ec67163ad4fcc5"} {"nl": {"description": "Word $$$s$$$ of length $$$n$$$ is called $$$k$$$-complete if $$$s$$$ is a palindrome, i.e. $$$s_i=s_{n+1-i}$$$ for all $$$1 \\le i \\le n$$$; $$$s$$$ has a period of $$$k$$$, i.e. $$$s_i=s_{k+i}$$$ for all $$$1 \\le i \\le n-k$$$. For example, \"abaaba\" is a $$$3$$$-complete word, while \"abccba\" is not.Bob is given a word $$$s$$$ of length $$$n$$$ consisting of only lowercase Latin letters and an integer $$$k$$$, such that $$$n$$$ is divisible by $$$k$$$. He wants to convert $$$s$$$ to any $$$k$$$-complete word.To do this Bob can choose some $$$i$$$ ($$$1 \\le i \\le n$$$) and replace the letter at position $$$i$$$ with some other lowercase Latin letter.So now Bob wants to know the minimum number of letters he has to replace to convert $$$s$$$ to any $$$k$$$-complete word.Note that Bob can do zero changes if the word $$$s$$$ is already $$$k$$$-complete.You are required to answer $$$t$$$ test cases independently.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t\\le 10^5$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le k < n \\le 2 \\cdot 10^5$$$, $$$n$$$ is divisible by $$$k$$$). The second line of each test case contains a word $$$s$$$ of length $$$n$$$. It is guaranteed that word $$$s$$$ only contains lowercase Latin letters. And it is guaranteed that the sum of $$$n$$$ over all test cases will not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, output one integer, representing the minimum number of characters he has to replace to convert $$$s$$$ to any $$$k$$$-complete word.", "sample_inputs": ["4\n6 2\nabaaba\n6 3\nabaaba\n36 9\nhippopotomonstrosesquippedaliophobia\n21 7\nwudixiaoxingxingheclp"], "sample_outputs": ["2\n0\n23\n16"], "notes": "NoteIn the first test case, one optimal solution is aaaaaa.In the second test case, the given word itself is $$$k$$$-complete."}, "positive_code": [{"source_code": "gets.to_i.times do\n n, k = gets.split(' ').map(&:to_i)\n word = gets.chomp\n\n changes_count = 0 \n letter_count = {}\n\n i = 0\n if k > 1\n while i < k/2 do\n j = 0\n while i+k*j < n-1 \n letter_count[word[i+k*j]].nil? ?\n letter_count[word[i+k*j]] = 1 :\n letter_count[word[i+k*j]] += 1\n\n letter_count[word[k-1-i+k*j]].nil? ?\n letter_count[word[k-1-i+k*j]] = 1 :\n letter_count[word[k-1-i+k*j]] += 1\n\n j += 1\n end\n changes_count += (n/k)*2 - letter_count.values.max\n\n letter_count = {}\n i += 1\n end\n end\n\n if k % 2 == 1\n letter_count = {}\n i = k / 2\n j = 0\n while i + k * j < n\n letter_count[word[i+k*j]].nil? ?\n letter_count[word[i+k*j]] = 1 :\n letter_count[word[i+k*j]] += 1\n\n j+= 1\n end\n changes_count += (n/k) - letter_count.values.max\n end\n\n puts changes_count\nend"}, {"source_code": "gets.to_i.times do\n n, k = gets.split(' ').map(&:to_i)\n word = gets.chomp\n \n changes_count = 0 \n i = 0\n\n while i < k/2 do\n letter_count = Hash.new(0)\n j = 0\n\n while j < n/k\n letter_count[word[i+k*j]] += 1\n letter_count[word[k-1-i+k*j]] += 1\n j += 1\n end\n\n changes_count += (n/k)*2 - letter_count.values.max\n i += 1\n end\n\n if k % 2 == 1\n letter_count = Hash.new(0)\n i, j = k/2, 0\n\n while j < n/k\n letter_count[word[i+k*j]] += 1\n j+= 1\n end\n\n changes_count += (n/k) - letter_count.values.max\n end\n\n puts changes_count\nend"}, {"source_code": "t = gets.to_i\nt.times do\n n, k = gets.split.map(&:to_i)\n s = gets.chomp\n count = 0\n for i in 0...(k / 2) do\n hash = Hash.new(0)\n for j in 0...(n / k) do\n hash[s[i + k * j]] += 1\n hash[s[k - 1 - i + k * j]] += 1\n end\n count += (n / k * 2) - hash.values.max\n end\n if k % 2 == 1\n hash = Hash.new(0)\n for j in 0...(n / k) do\n hash[s[k / 2 + k * j]] += 1\n end\n count += (n / k) - hash.values.max\n end\n puts count\nend\n"}], "negative_code": [{"source_code": "gets.to_i.times do\n n, k = gets.split(' ').map(&:to_i)\n word = gets.chomp\n\n changes_count = 0 \n letter_count = {}\n\n i = 0\n if k > 1\n while i < k/2 do\n j = 0\n while i+k*j < n-1 \n letter_count[word[i+k*j]].nil? ?\n letter_count[word[i+k*j]] = 1 :\n letter_count[word[i+k*j]] += 1\n\n letter_count[word[k-1-i+k*j]].nil? ?\n letter_count[word[k-1-i+k*j]] = 1 :\n letter_count[word[k-1-i+k*j]] += 1\n\n j += 1\n end\n changes_count += (n/k)*2 - letter_count.values.max\n\n letter_count = {}\n i += 1\n end\n end\n\n if k % 2 == 1\n letter_count = {}\n i = k / 2\n j = 0\n while i + k * j < n-1\n letter_count[word[i+k*j]].nil? ?\n letter_count[word[i+k*j]] = 1 :\n letter_count[word[i+k*j]] += 1\n j+= 1\n end\n changes_count += (n/k) - letter_count.values.max\n end\n\n puts changes_count\nend"}], "src_uid": "22f90afe503267ff2c832430d3ffb3b4"} {"nl": {"description": "Alyona decided to go on a diet and went to the forest to get some apples. There she unexpectedly found a magic rooted tree with root in the vertex 1, every vertex and every edge of which has a number written on.The girl noticed that some of the tree's vertices are sad, so she decided to play with them. Let's call vertex v sad if there is a vertex u in subtree of vertex v such that dist(v, u) > au, where au is the number written on vertex u, dist(v, u) is the sum of the numbers written on the edges on the path from v to u.Leaves of a tree are vertices connected to a single vertex by a single edge, but the root of a tree is a leaf if and only if the tree consists of a single vertex — root.Thus Alyona decided to remove some of tree leaves until there will be no any sad vertex left in the tree. What is the minimum number of leaves Alyona needs to remove?", "input_spec": "In the first line of the input integer n (1 ≤ n ≤ 105) is given — the number of vertices in the tree. In the second line the sequence of n integers a1, a2, ..., an (1 ≤ ai ≤ 109) is given, where ai is the number written on vertex i. The next n - 1 lines describe tree edges: ith of them consists of two integers pi and ci (1 ≤ pi ≤ n,  - 109 ≤ ci ≤ 109), meaning that there is an edge connecting vertices i + 1 and pi with number ci written on it.", "output_spec": "Print the only integer — the minimum number of leaves Alyona needs to remove such that there will be no any sad vertex left in the tree.", "sample_inputs": ["9\n88 22 83 14 95 91 98 53 11\n3 24\n7 -8\n1 67\n1 64\n9 65\n5 12\n6 -80\n3 8"], "sample_outputs": ["5"], "notes": "NoteThe following image represents possible process of removing leaves from the tree: "}, "positive_code": [{"source_code": "gets\nverts = gets.split(' ').map(&:to_i)\ng = {}\n(2..verts.size).each { |i|\n parent, w = gets.split(' ').map(&:to_i)\n g[parent] ||= []\n g[parent].push([i, w])\n}\n\nremoved = 0\n_stack = [[1, 0, false]]\nwhile !_stack.empty?\n node, sum_cur, kill = _stack.pop\n (g[node] || []).each do |node_nxt, w|\n sum_cur_n = [sum_cur + w, 0].max\n\n kill_n = sum_cur_n > verts[node_nxt-1] || kill\n removed += 1 if kill_n\n _stack.push([node_nxt, sum_cur_n, kill_n])\n end\nend\n\nputs removed\n"}], "negative_code": [{"source_code": "gets\nverts = gets.split(' ').map(&:to_i)\ng = {}\n(2..verts.size).each { |i|\n parent, w = gets.split(' ').map(&:to_i)\n g[parent] ||= []\n g[parent].push([i, w])\n}\n\nremoved = 0\n_stack = [[1, 0, 0, false]]\nwhile !_stack.empty?\n node, sum_max, sum_cur, kill = _stack.pop\n (g[node] || []).each do |node_nxt, w|\n sum_cur_n = [sum_cur + w, 0].max\n sum_max_n = [sum_cur_n, sum_max].max\n\n kill_n = sum_max_n > verts[node_nxt-1] || kill\n removed += 1 if kill_n\n _stack.push([node_nxt, sum_max_n, sum_cur_n, kill_n])\n end\nend\n\nputs removed\n"}, {"source_code": "gets\nverts = gets.split(' ').map(&:to_i)\ng = {}\n(2..verts.size).each { |i|\n parent, w = gets.split(' ').map(&:to_i)\n g[parent] ||= []\n g[parent].push([i, w])\n}\n\nremoved = 0\n_stack = [[1, 0, 0]]\nwhile !_stack.empty?\n node, sum_max, sum_cur = _stack.pop\n (g[node] || []).each do |node_nxt, w|\n sum_cur_n = [sum_cur + w, 0].max\n sum_max_n = [sum_cur_n, sum_max].max\n if sum_max_n > verts[node_nxt-1]\n removed += 1\n end\n _stack.push([node_nxt, sum_max_n, sum_cur_n])\n end\nend\n\nputs removed\n"}, {"source_code": "gets\nverts = gets.split(' ').map(&:to_i)\ng = {}\n(2..verts.size).each { |i|\n parent, w = gets.split(' ').map(&:to_i)\n g[parent] ||= []\n g[parent].push([i, w])\n}\n\nremoved = 0\n_stack = [[1, 0]]\nwhile !_stack.empty?\n node, w_acc = _stack.pop\n (g[node] || []).each do |node_nxt, w_nxt|\n w_acc_nxt = [w_acc + w_nxt, w_acc].max\n if w_acc_nxt > verts[node_nxt-1]\n removed += 1\n end\n _stack.push([node_nxt, w_acc_nxt])\n end\nend\n\nputs removed\n"}], "src_uid": "0c4bc51e5be9cc642f62d2b3df2bddc4"} {"nl": {"description": "It was decided in IT City to distinguish successes of local IT companies by awards in the form of stars covered with gold from one side. To order the stars it is necessary to estimate order cost that depends on the area of gold-plating. Write a program that can calculate the area of a star.A \"star\" figure having n ≥ 5 corners where n is a prime number is constructed the following way. On the circle of radius r n points are selected so that the distances between the adjacent ones are equal. Then every point is connected by a segment with two maximally distant points. All areas bounded by the segments parts are the figure parts. ", "input_spec": "The only line of the input contains two integers n (5 ≤ n < 109, n is prime) and r (1 ≤ r ≤ 109) — the number of the star corners and the radius of the circumcircle correspondingly.", "output_spec": "Output one number — the star area. The relative error of your answer should not be greater than 10 - 7.", "sample_inputs": ["7 10"], "sample_outputs": ["108.395919545675"], "notes": null}, "positive_code": [{"source_code": "n,r=gets.split.map &:to_i;p n*r**2*Math.sin(Math::PI/n)*Math.cos(Math::PI*(n/2)/n)/Math.cos(Math::PI*(n/2-1)/n)"}, {"source_code": "n,r=gets.split.map &:to_i;p n*r**2*Math.sin(Math::PI/n)*Math.cos(Math::PI*(n/2)/n)/Math.cos(Math::PI*(n/2-1)/n)\n"}, {"source_code": "n,r=gets.split.map &:to_i;p n*r**2*Math.sin(Math::PI/n)*Math.cos(Math::PI*(n/2)/n)/Math.cos(Math::PI*(n/2-1)/n)"}, {"source_code": "n,r=gets.split.map &:to_i;p n*r**2*Math.sin(Math::PI/n)*Math.cos(Math::PI*(n/2)/n)/Math.cos(Math::PI*(n/2-1)/n)\n"}], "negative_code": [], "src_uid": "fd92081afd795789b738009ff292a25c"} {"nl": {"description": "Now that Kuroni has reached 10 years old, he is a big boy and doesn't like arrays of integers as presents anymore. This year he wants a Bracket sequence as a Birthday present. More specifically, he wants a bracket sequence so complex that no matter how hard he tries, he will not be able to remove a simple subsequence!We say that a string formed by $$$n$$$ characters '(' or ')' is simple if its length $$$n$$$ is even and positive, its first $$$\\frac{n}{2}$$$ characters are '(', and its last $$$\\frac{n}{2}$$$ characters are ')'. For example, the strings () and (()) are simple, while the strings )( and ()() are not simple.Kuroni will be given a string formed by characters '(' and ')' (the given string is not necessarily simple). An operation consists of choosing a subsequence of the characters of the string that forms a simple string and removing all the characters of this subsequence from the string. Note that this subsequence doesn't have to be continuous. For example, he can apply the operation to the string ')()(()))', to choose a subsequence of bold characters, as it forms a simple string '(())', delete these bold characters from the string and to get '))()'. Kuroni has to perform the minimum possible number of operations on the string, in such a way that no more operations can be performed on the remaining string. The resulting string does not have to be empty.Since the given string is too large, Kuroni is unable to figure out how to minimize the number of operations. Can you help him do it instead?A sequence of characters $$$a$$$ is a subsequence of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) characters.", "input_spec": "The only line of input contains a string $$$s$$$ ($$$1 \\le |s| \\le 1000$$$) formed by characters '(' and ')', where $$$|s|$$$ is the length of $$$s$$$.", "output_spec": "In the first line, print an integer $$$k$$$  — the minimum number of operations you have to apply. Then, print $$$2k$$$ lines describing the operations in the following format: For each operation, print a line containing an integer $$$m$$$  — the number of characters in the subsequence you will remove. Then, print a line containing $$$m$$$ integers $$$1 \\le a_1 < a_2 < \\dots < a_m$$$  — the indices of the characters you will remove. All integers must be less than or equal to the length of the current string, and the corresponding subsequence must form a simple string. If there are multiple valid sequences of operations with the smallest $$$k$$$, you may print any of them.", "sample_inputs": ["(()((", ")(", "(()())"], "sample_outputs": ["1\n2\n1 3", "0", "1\n4\n1 2 5 6"], "notes": "NoteIn the first sample, the string is '(()(('. The operation described corresponds to deleting the bolded subsequence. The resulting string is '(((', and no more operations can be performed on it. Another valid answer is choosing indices $$$2$$$ and $$$3$$$, which results in the same final string.In the second sample, it is already impossible to perform any operations."}, "positive_code": [{"source_code": "S = gets.chomp.chars\nN = S.size\n\ndef f(n)\n return [] if n == 0\n\n left_pos = []\n left_cnt = 0\n\n 0.upto(N - 1) do |i|\n s = S[i]\n\n if left_cnt < n && s == '('\n left_pos << i + 1\n left_cnt += 1\n end\n\n break if left_cnt == n\n end\n\n right_pos = []\n right_cnt = 0\n\n (N - 1).downto(0) do |i|\n s = S[i]\n\n if right_cnt < n && s == ')'\n right_pos.unshift(i + 1)\n right_cnt += 1\n end\n\n break if right_cnt == n\n end\n\n if left_cnt < n || right_cnt < n\n return false\n elsif left_pos[-1] > right_pos[0]\n return false\n else\n left_pos + right_pos\n end\nend\n\nok = 0\nng = S.size\n\nwhile (ok - ng).abs > 1\n n = (ok + ng) / 2\n\n if f(n)\n ok = n\n else\n ng = n\n end\nend\n\nif ok == 0\n puts 0\nelse\n res = f(ok)\n\n puts 1\n puts res.size\n puts res.join(' ')\nend\n\n"}, {"source_code": "def main\n s = gets.chomp.chars\n n = s.size\n # scan '(' from left and ')' from right\n left = 0\n right = n-1\n cnt = 0\n l_a = []\n r_a = []\n while (right > left)\n while left < n && s[left] != '('\n left += 1\n end\n # Here, s[left] == '(' or left == n\n while right >= 0 && s[right] != ')'\n right -= 1\n end\n # Here, s[right] == ')' or right == -1\n\n if (right <= left) # Meet\n break\n end\n \n cnt += 1\n l_a << left + 1 # 1-indexed\n r_a << right + 1 # 1-indexed\n left += 1\n right -= 1\n end\n # Here, right <= left. cnt contains the result\n if cnt > 0\n p 1\n p cnt * 2\n a = l_a + r_a\n a.sort!\n print a.join(\" \") + \"\\n\"\n else\n p 0\n end\nend\n\nmain\n"}, {"source_code": "s = gets.chomp\nout = []\ni = 0;\nj = s.size - 1;\nwhile j > i do\n if s[i] == '(' && s[j] == ')'\n out << [i, j]\n i += 1\n j -= 1\n elsif s[i] == '('\n j -= 1\n elsif s[j] == ')'\n i += 1\n else\n i += 1\n j -= 1\n end\nend\nif out.size.zero?\n puts 0\nelse\n puts 1\n out.flatten!.sort!\n puts out.size\n puts out.map{|x| x + 1}.join(' ')\nend\n"}], "negative_code": [{"source_code": "def main\n s = gets.chomp.chars\n n = s.size\n # scan '(' from left and ')' from right\n left = 0\n right = n-1\n cnt = 0\n l_a = []\n r_a = []\n while (right > left)\n while left < n && s[left] != '('\n left += 1\n end\n # Here, s[left] == '(' or left == n\n while right >= 0 && s[right] != ')'\n right -= 1\n end\n # Here, s[right] == ')' or right == -1\n\n if (right <= left) # Meet\n break\n end\n \n cnt += 1\n l_a << left + 1 # 1-indexed\n r_a << right + 1 # 1-indexed\n left += 1\n right -= 1\n end\n # Here, right <= left. cnt contains the result\n if cnt > 0\n p 1\n p cnt * 2\n print (l_a + r_a).join(\" \") + \"\\n\"\n else\n p 0\n end\nend\n\nmain\n"}], "src_uid": "f78d04f699fc94103e5b08023949854d"} {"nl": {"description": "INTERCAL is the oldest of esoteric programming languages. One of its many weird features is the method of character-based output, known as Turing Tape method. It converts an array of unsigned 8-bit integers into a sequence of characters to print, using the following method.The integers of the array are processed one by one, starting from the first. Processing i-th element of the array is done in three steps:1. The 8-bit binary notation of the ASCII-code of the previous printed character is reversed. When the first element of the array is processed, the result of this step is considered to be 0.2. The i-th element of the array is subtracted from the result of the previous step modulo 256.3. The binary notation of the result of the previous step is reversed again to produce ASCII-code of the i-th character to be printed.You are given the text printed using this method. Restore the array used to produce this text.", "input_spec": "The input will consist of a single line text which contains the message printed using the described method. String text will contain between 1 and 100 characters, inclusive. ASCII-code of each character of text will be between 32 (space) and 126 (tilde), inclusive.", "output_spec": "Output the initial array, which was used to produce text, one integer per line.", "sample_inputs": ["Hello, World!"], "sample_outputs": ["238\n108\n112\n0\n64\n194\n48\n26\n244\n168\n24\n16\n162"], "notes": "NoteLet's have a closer look at the beginning of the example. The first character is \"H\" with ASCII-code 72 = 010010002. Its reverse is 000100102 = 18, and this number should become the result of the second step of processing. The result of the first step is considered to be 0, so the first element of the array has to be (0 - 18) mod 256 = 238, where a mod b is the remainder of division of a by b."}, "positive_code": [{"source_code": "def gao(_)\n ('%08b' % _).reverse.to_i(2)\nend\n\ns = gets.chomp\nx = 0\ns.each_byte do |b|\n y = (gao(x) - gao(b)) % 256\n p y\n x = gao((gao(x) - y) % 256)\nend\n"}, {"source_code": "s=gets.chomp\nx=0\ns.each_byte{|c|\n\ty=(\"%08b\"%c).reverse.to_i(2);\n\tp x-y&255\n\tx=y\n}"}, {"source_code": "s=gets.chomp\nx=0\ns.each_byte{|c|\n y=(\"%08b\"%c).reverse.to_i(2);\n p x-y&255\n x=y\n}"}, {"source_code": "def to_bin n\n str=\"\"\n \n while n!=0\n str+=(n%2).to_s\n n/=2\n end\n \n str+\"0\"*(8-str.length)\nend\n\ndef to_ten bin\n pos=bin.length-1\n pow=0\n \n ans=0\n while pos>-1\n ans+=(bin[pos].to_i)*2**pow\n \n pow+=1\n pos-=1\n end\n \n ans\nend\n\nline=gets.strip\n\nstart=0\ndata=[]\nline.split(//).each{|c|\n code=to_ten(to_bin(c.ord))\n \n data << (start-code)%256\n start=code\n}\n\nputs data"}, {"source_code": "s=gets.chomp\nx=0\ns.each_byte{|c|\n\ty=(\"%08b\"%c).reverse.to_i(2);\n\tp x-y&255\n\tx=y\n}"}, {"source_code": "s = gets.bytes\n\ndef d(x)\n xs = x.to_s(2)\n xs.insert(0,'0') while xs.size < 8\n 4.times { |i| xs[7-i], xs[i] = xs[i], xs[7-i] }\n xs.to_i(2)\nend\n\ns.size.pred.times do |i|\n x = i.zero? ? 0 : s[i-1]\n y = s[i]\n puts (d(x) - d(y)) % 256\nend\n"}, {"source_code": "s = gets.chomp\nv = 0\ns.each_byte do |c| \n x1 = (\"%08b\"%v).reverse.to_i(2)\n r = (\"%08b\"%c).reverse.to_i(2)\n x2 = (x1 - r + 256)%256\n\n p x2\n v = c\nend\n"}, {"source_code": "str=gets.chomp;\nv=0;\nstr.each_byte do |c|\n\tp (v-(\"%08b\"%c).reverse.to_i(2)+256)%256;\n\tv=(\"%08b\"%c).reverse.to_i(2);\nend\n\n"}, {"source_code": "#!/usr/bin/ruby\nwhile l = STDIN.gets\n prev = 0\n l.chomp.each_byte do |c|\n str = c.to_s(2)\n while str.size < 8\n str = \"0\" + str\n end\n str.reverse!\n v = (prev - str.to_i(2)) % 256\n prev = str.to_i(2)\n p v\n end\nend \n"}, {"source_code": "# def gray_code(n)\n\t# a = ['0', '1']\n\t# until a[0].length == n do\n\t\t# a = a.map{|i| '0' + i} + a.reverse.map{|i| i + '1'}\n\t# end\n\t# return a\n# end\n\n# puts gray_code(3)\n\n# require 'set'\n# visited = Set.new\n# Dir.chdir \"..\"\n# stack = Array.new\n# stack.push(\"mine\")\n# until stack.length == 0 do\n\t# dir = false\n\t# Dir.chdir stack.pop\n\t\n\t# puts Dir.pwd\n\t# Dir[\"*\"].each do |f|\n\t\t# print \"File: \", f, \"\\n\" if File.file?(f)\n\t\t# if (File.directory?(f) && visited.include?(Dir.pwd + \"/\" + f) == false) then\n\t\t\t# print \"Directory: \", f, \"\\n\"\n\t\t\t# stack.push(f) if File.directory?(f)\n\t\t\t# dir = true\n\t\t\t# break\n\t\t# end\n\t# end\n\t# if dir == false then\n\t\t# visited.add(Dir.pwd)\n\t\t# Dir.chdir \"..\"\n\t# end\n\t# puts Dir.pwd\n# end\n# puts \"\\nCompleted\"\n\ninp = STDIN.gets.chomp\nstart = 0\ninp.each_byte do |ch|\n\tbin = ch.to_s(2).rjust(8, '0').reverse.to_i(2).to_s(10).to_i\n\tputs (start - bin) % 256\n\tstart = bin\nend\n\n"}, {"source_code": "p = gets.chomp\nia = Array.new()\nprev = 0\np.each_byte{|a|\n b = sprintf(\"%08b\", a).reverse.to_i(2)\n c = (prev - b) % 256\n ia.push(c)\n prev = b\n}\nia.each{|i|\n puts i\n}\n"}, {"source_code": "#!/usr/bin/ruby\n#coding:utf-8\n\nSTR=gets.chomp\nlast=0\nsiz=STR.bytes.to_a.size\nplease=(siz+3)/4-2\nSTR.each_byte.with_index{|e,i|\n\tc = e\n\tc = (c & 0x55) << 1 | (c & 0xaa) >> 1\n\tc = (c & 0x33) << 2 | (c & 0xcc) >> 2\n\tc = (c & 0x0f) << 4 | (c & 0xf0) >> 4\n\tlast2 = c\n\tc = (last-c)&0xff\n\tlast = last2\n\tp c&0xff\n}\n\n__END__\n[cesspool.c]\nunsigned int lastout = 0;\nc = lastout - a[i];\nlastout = c;\nc = (c & 0x0f) << 4 | (c & 0xf0) >> 4;\nc = (c & 0x33) << 2 | (c & 0xcc) >> 2;\nc = (c & 0x55) << 1 | (c & 0xaa) >> 1;\nputchar(c);"}, {"source_code": "#!/usr/bin/ruby\n\ndef readNextValues\n gets.split.map {|x| x.to_i}\nend\n\nline = gets.chomp\nprev_byte = 0\n\nline.each_byte do |b|\n bin = sprintf(\"%08b\", b)\n val = bin.reverse.to_i(2)\n\n bin2 = sprintf(\"%08b\", prev_byte)\n step2 = bin2.reverse.to_i(2)\n\n n = (step2 - val) % 256\n puts n\n\n prev_byte = b\nend\n"}, {"source_code": "s = STDIN.gets.strip\ns = s.bytes.to_a\ns.collect!{|char| char.to_s(2)}\ns.collect! do |str|\n str = Array.new(8-str.size, \"0\").join + str\nend\nresult = 0\ns.each do |str|\n temp = str.reverse.to_i(2) \n p (result - temp) % 256\n result = temp\nend"}, {"source_code": "s = gets.strip\na = 0\ns.each_char do |c|\n b = c.bytes.to_a[0].to_s(2)\n b = \"0\" * (8-b.size) + b if b.size < 8\n n = 0\n p = 1\n b.each_char do |cc|\n n += p if cc == \"1\"\n p *= 2\n end\n p ((a - n) % 256)\n a = n\nend\n"}, {"source_code": "s=gets.chomp\npre=0\ns.bytes.each{|b|post=(\"%08b\"%b).reverse.to_i(2);puts (pre-post)%256;pre=post}"}, {"source_code": "def to_bin n\n\tstr=\"\"\n\t\n\twhile n!=0\n\t\tstr+=(n%2).to_s\n\t\tn/=2\n\tend\n\t\n\tstr+\"0\"*(8-str.length)\nend\n\ndef to_ten bin\n\tpos=bin.length-1\n\tpow=0\n\t\n\tans=0\n\twhile pos>-1\n\t\tans+=(bin[pos].to_i)*2**pow\n\t\t\n\t\tpow+=1\n\t\tpos-=1\n\tend\n\t\n\tans\nend\n\nline=gets.strip\n\nstart=0\ndata=[]\nline.split(//).each{|c|\n\tcode=to_ten(to_bin(c.ord))\n\t\n\tdata << (start-code)%256\n\tstart=code\n}\n\nputs data\n"}, {"source_code": "#!/usr/bin/ruby\n# coding: utf-8\n\ndef revbin(c)\n c.ord.to_s(2).rjust(8, '0').reverse.to_i(2)\nend\nM = 256\n\ns = $stdin.gets.chomp\ns.chars.inject(0) do |acc, c|\n n = revbin c\n puts ((acc - n) % M)\n n\nend\n"}, {"source_code": "p = gets.chomp\nprev = 0\np.each_byte{|l|\n cur = l.ord.to_s(2)\n cur = \"0\"*(8 - cur.length)+cur\n cur = cur.reverse.to_i(2)\n res = (prev - cur) % 256\n prev = cur\n puts res\n}\n"}, {"source_code": "a = []\nn = gets.to_s.chomp\na = n.split(\"\").map(&:to_s)\nn = 0\nj = 0\nfor i in a do\n n = i.ord.to_s(2).rjust(8,\"0\").reverse.to_i(2)\n puts (j-n) % 256\n j = n\nend"}, {"source_code": "s=gets.chomp\npre=0\ns.bytes.each{|b|post=(\"%08b\"%b).reverse.to_i(2);puts (pre-post)%256;pre=post}"}], "negative_code": [{"source_code": "#!/usr/bin/ruby\nwhile l = STDIN.gets\n prev = 0\n l.each_byte do |c|\n str = c.to_s(2)\n while str.size < 8\n str = \"0\" + str\n end\n str.reverse!\n v = (prev - str.to_i(2)) % 256\n prev = str.to_i(2)\n p v\n end\nend \n"}, {"source_code": "#!/usr/bin/ruby\n#coding:utf-8\n\nSTR=STDIN.read\nlast=0\nsiz=STR.bytes.to_a.size\nplease=(siz+3)/4-2\nSTR.each_byte.with_index{|e,i|\n\tc = e\n\tc = (c & 0x55) << 1 | (c & 0xaa) >> 1\n\tc = (c & 0x33) << 2 | (c & 0xcc) >> 2\n\tc = (c & 0x0f) << 4 | (c & 0xf0) >> 4\n\tlast2 = c\n\tc = (last-c)&0xff\n\tlast = last2\n\tp c&0xff\n}\n\n__END__\n[cesspool.c]\nunsigned int lastout = 0;\nc = lastout - a[i];\nlastout = c;\nc = (c & 0x0f) << 4 | (c & 0xf0) >> 4;\nc = (c & 0x33) << 2 | (c & 0xcc) >> 2;\nc = (c & 0x55) << 1 | (c & 0xaa) >> 1;\nputchar(c);"}, {"source_code": "s = STDIN.gets\ns = s.bytes.to_a\ns.collect!{|char| char.to_s(2)}\ns.collect! do |str|\n str = Array.new(8-str.size, \"0\").join + str\nend\nresult = 0\ns.each do |str|\n temp = str.reverse.to_i(2) \n p (result - temp) % 256\n result = temp\nend"}, {"source_code": "s = STDIN.gets\ns = s.bytes.to_a\ns.collect!{|char| char.to_s(2)}\ns.collect! do |str|\n str = Array.new(8-str.size, \"0\").join + str\nend\nresult = 0\ns.push\ns.each do |str|\n temp = str.reverse.to_i(2) \n p (result - temp) % 256\n result = temp\nend"}], "src_uid": "a65e12186430f74c18c50d2eb55a9794"} {"nl": {"description": "You are given two arrays $$$a$$$ and $$$b$$$, each contains $$$n$$$ integers.You want to create a new array $$$c$$$ as follows: choose some real (i.e. not necessarily integer) number $$$d$$$, and then for every $$$i \\in [1, n]$$$ let $$$c_i := d \\cdot a_i + b_i$$$.Your goal is to maximize the number of zeroes in array $$$c$$$. What is the largest possible answer, if you choose $$$d$$$ optimally?", "input_spec": "The first line contains one integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) — the number of elements in both arrays. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \\le a_i \\le 10^9$$$). The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ ($$$-10^9 \\le b_i \\le 10^9$$$).", "output_spec": "Print one integer — the maximum number of zeroes in array $$$c$$$, if you choose $$$d$$$ optimally.", "sample_inputs": ["5\n1 2 3 4 5\n2 4 7 11 3", "3\n13 37 39\n1 2 3", "4\n0 0 0 0\n1 2 3 4", "3\n1 2 -1\n-6 -12 6"], "sample_outputs": ["2", "2", "0", "3"], "notes": "NoteIn the first example, we may choose $$$d = -2$$$.In the second example, we may choose $$$d = -\\frac{1}{13}$$$.In the third example, we cannot obtain any zero in array $$$c$$$, no matter which $$$d$$$ we choose.In the fourth example, we may choose $$$d = 6$$$."}, "positive_code": [{"source_code": "n = gets.to_i \n\na = gets.split.map{ |x| x.to_i }\nb = gets.split.map{ |x| x.to_i }\n\nc = Hash.new(0)\n\n\nd = Rational(5,6)\n#puts 4\n\nh = 0 \nk = 0\n\nfor i in 0...n \n if a[i]==0 \n if b[i]==0\n k=k+1\n end\n next\n end\n d = Rational((-b[i]), a[i])\n #puts d \n c[d] = c[d] + 1\n #puts c[d]\nend \n\nf = c.values \n\n\nf.each{ |a| h = (h>a)?h:a}\n\nputs h+k \n"}, {"source_code": "if File.exists?(\"input\")\n $stdin = File.open(\"input\")\n $stdout = File.open(\"output\",\"w\")\nend\n\ndef gcd(a,b)\n\treturn b if(a==0)\n\treturn gcd(b%a,a)\nend\n\n=begin\nint gcd(int a, int b) \n{ \n if (a == 0) \n return b; \n return gcd(b % a, a); \n} \n=end\n\nn = gets.to_i\na = gets.chomp.split(\" \").map(&:to_i)\nb = gets.chomp.split(\" \").map(&:to_i)\n\n#c = d*a+b\n#c needs to have 0s i.e. c = 0\n# d*a = c-b = 0-b\n# d = -b/a\n\nhash=Hash.new(0)\n\nzerocount=0\ntotcount=0\n\n(0..n-1).each do |i|\n\tmya = a[i]\n\tmyb = b[i]\n\n\tif(myb==0)\n\t\tif(mya==0)\n\t\t\ttotcount+=1\n\t\tend\n\t\tzerocount+=1\n\t\tnext\n\tend\n\n\tif(mya==0)\n\t\tnext\n\tend\n\n\tmygcd = gcd(mya,myb)\n\n\tmya/=mygcd\n\tmyb/=mygcd\n\n\n\thash[[mya,myb]]+=1\nend\n\nif hash.empty?\n\tputs zerocount\nelse\n\tputs [hash.values.max+totcount,zerocount].max\nend\n"}], "negative_code": [{"source_code": "n = gets.to_i \n\na = gets.split.map{ |x| x.to_i }\nb = gets.split.map{ |x| x.to_i }\n\nc = Hash.new(0)\n\n\nd = Rational(5,6)\n#puts 4\n\nfor i in 0...n \n if a[i]==0 \n next\n end\n d = Rational((-b[i]), a[i])\n #puts d \n c[d] = c[d] + 1\n #puts c[d]\nend \n\nf = c.values \n\nh = 0\n\nf.each{ |a| h = (h>a)?h:a}\n\nputs h \n"}, {"source_code": "if File.exists?(\"input\")\n $stdin = File.open(\"input\")\n $stdout = File.open(\"output\",\"w\")\nend\n\ndef gcd(a,b)\n\treturn b if(a==0)\n\treturn gcd(b%a,a)\nend\n\n=begin\nint gcd(int a, int b) \n{ \n if (a == 0) \n return b; \n return gcd(b % a, a); \n} \n=end\n\nn = gets.to_i\na = gets.chomp.split(\" \").map(&:to_i)\nb = gets.chomp.split(\" \").map(&:to_i)\n\n#c = d*a+b\n#c needs to have 0s i.e. c = 0\n# d*a = c-b = 0-b\n# d = -b/a\n\nhash=Hash.new(0)\n\ntotcount=0\n\n(0..n-1).each do |i|\n\tmya = a[i]\n\tmyb = b[i]\n\n\tif(myb==0)\n\t\ttotcount+=1\n\t\tnext\n\tend\n\n\tif(mya==0)\n\t\tnext\n\tend\n\n\tmygcd = gcd(mya,myb)\n\n\tmya/=mygcd\n\tmyb/=mygcd\n\n\n\thash[[mya,myb]]+=1\nend\n\nif hash.empty?\n\tputs totcount\nelse\n\tputs hash.values.max+totcount\nend\n"}], "src_uid": "c083988d20f434d61134f7b376581eb6"} {"nl": {"description": "You are given two strings $$$s$$$ and $$$t$$$. Both strings have length $$$n$$$ and consist of lowercase Latin letters. The characters in the strings are numbered from $$$1$$$ to $$$n$$$.You can successively perform the following move any number of times (possibly, zero): swap any two adjacent (neighboring) characters of $$$s$$$ (i.e. for any $$$i = \\{1, 2, \\dots, n - 1\\}$$$ you can swap $$$s_i$$$ and $$$s_{i + 1})$$$. You can't apply a move to the string $$$t$$$. The moves are applied to the string $$$s$$$ one after another.Your task is to obtain the string $$$t$$$ from the string $$$s$$$. Find any way to do it with at most $$$10^4$$$ such moves.You do not have to minimize the number of moves, just find any sequence of moves of length $$$10^4$$$ or less to transform $$$s$$$ into $$$t$$$.", "input_spec": "The first line of the input contains one integer $$$n$$$ ($$$1 \\le n \\le 50$$$) — the length of strings $$$s$$$ and $$$t$$$. The second line of the input contains the string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters. The third line of the input contains the string $$$t$$$ consisting of $$$n$$$ lowercase Latin letters.", "output_spec": "If it is impossible to obtain the string $$$t$$$ using moves, print \"-1\". Otherwise in the first line print one integer $$$k$$$ — the number of moves to transform $$$s$$$ to $$$t$$$. Note that $$$k$$$ must be an integer number between $$$0$$$ and $$$10^4$$$ inclusive. In the second line print $$$k$$$ integers $$$c_j$$$ ($$$1 \\le c_j < n$$$), where $$$c_j$$$ means that on the $$$j$$$-th move you swap characters $$$s_{c_j}$$$ and $$$s_{c_j + 1}$$$. If you do not need to apply any moves, print a single integer $$$0$$$ in the first line and either leave the second line empty or do not print it at all.", "sample_inputs": ["6\nabcdef\nabdfec", "4\nabcd\naccd"], "sample_outputs": ["4\n3 5 4 5", "-1"], "notes": "NoteIn the first example the string $$$s$$$ changes as follows: \"abcdef\" $$$\\rightarrow$$$ \"abdcef\" $$$\\rightarrow$$$ \"abdcfe\" $$$\\rightarrow$$$ \"abdfce\" $$$\\rightarrow$$$ \"abdfec\".In the second example there is no way to transform the string $$$s$$$ into the string $$$t$$$ through any allowed moves."}, "positive_code": [{"source_code": "gets\ns = gets.chomp.split('')\nt = gets.chomp.split('')\n\ncontainer = {}\ns.each do |item|\n if container.key?(item)\n container[item] += 1\n else\n container[item] = 1\n end\nend\n\nt.each do |item|\n container[item] -= 1 if container.key?(item)\nend\n\nif container.values.any? { |e| e != 0 }\n puts '-1'\nelse\n swaps = []\n t.each.with_index do |item, i|\n if item != s[i]\n j = s.rindex(item)\n k = j - 1\n while k != i - 1\n s[k], s[k + 1] = s[k + 1], s[k]\n swaps.push(k + 1)\n k -= 1\n end\n end\n end\n puts swaps.length\n puts swaps.join(' ') unless swaps.empty?\nend\n"}, {"source_code": "\ndef solution\n n = read_int\n s = read_string\n t = read_string\n\n if s.chars.sort != t.chars.sort\n puts \"-1\"\n return\n end\n\n tpos = Hash.new { |h, k| h[k] = [] }\n t.chars.each.with_index{ |c,i| tpos[c] << i }\n\n ss = {}\n s.chars.each.with_index{ |c, i| ss[i] = c }\n\n moves = []\n\n n.times do |i|\n next if ss[i] == t[i]\n\n j = (i+1...s.size).find{|k| ss[k] == t[i]}\n\n [*(i+1)..j].reverse.each do |k|\n moves << k\n ss[k], ss[k-1] = ss[k-1], ss[k]\n end\n end\n\n puts moves.size\n puts moves.join(' ')\n\nend\n\ndef read_int\n gets.to_i\nend\n\ndef read_ints\n gets.split.map(&:to_i)\nend\n\ndef read_string\n gets.chomp\nend\n\nsolution unless ENV['TEST__MODE']\n"}, {"source_code": "N = gets.to_i\ns = gets.chomp\nt = gets.chomp\nans = []\nN.times do |i|\n next if s[i] == t[i]\n j = ((i+1)...N).find{|k| s[k] == t[i]}\n if j\n (j-1).downto(i) do |k|\n ans.push(k+1)\n s[k, 2] = s[k, 2].reverse\n end\n else\n puts -1; exit\n end\nend\nputs ans.length\nputs ans.join(' ') if ans.length > 0"}, {"source_code": "n = gets.to_i\ns = gets.strip\nt = gets.strip\n\ndef swp(str, i)\n c=str[i]\n str[i]=str[i+1]\n str[i+1]=c\nend\n\nans = []\n(n-1).downto(0).each do |ti|\n next if s[ti]==t[ti]\n si=ti-1\n si-=1 while si>=0 && s[si]!=t[ti]\n if si==-1\n puts \"-1\"\n exit 0\n end\n si.upto(ti-1).each do |i|\n swp(s, i)\n ans << (i+1)\n end\nend\n\nputs ans.size\nputs ans.join(\" \")"}], "negative_code": [{"source_code": "gets\ns = gets.chomp.split('')\nt = gets.chomp.split('')\n\ncontainer = {}\ns.each do |item|\n if container.key?(item)\n container[item] += 1\n else\n container[item] = 1\n end\nend\n\nt.each do |item|\n container[item] -= 1 if container.key?(item)\nend\n\nif container.values.any? { |e| e != 0 }\n puts '-1'\nelse\n swaps = []\n s.each.with_index do |item, i|\n if item != t[i]\n j = t.rindex(item)\n k = j - 1\n while k != i - 1\n t[k], t[k + 1] = t[k + 1], t[k]\n swaps.push(k)\n k -= 1\n end\n end\n end\n puts swaps.length\n puts swaps.join(' ') unless swaps.empty?\nend\n"}, {"source_code": "gets\ns = gets.chomp.split('')\nt = gets.chomp.split('')\n\ncontainer = {}\ns.each do |item|\n if container.key?(item)\n container[item] += 1\n else\n container[item] = 1\n end\nend\n\nt.each do |item|\n container[item] -= 1 if container.key?(item)\nend\n\nif container.values.any? { |e| e != 0 }\n puts '-1'\nelse\n swaps = []\n s.each.with_index do |item, i|\n if item != t[i]\n j = t.rindex(item)\n k = j - 1\n while k != i - 1\n t[k], t[k + 1] = t[k + 1], t[k]\n swaps.push(k + 1)\n k -= 1\n end\n end\n end\n puts swaps.length\n puts swaps.join(' ') unless swaps.empty?\nend\n"}, {"source_code": "n = gets.to_i\ns = gets.strip\nt = gets.strip\n\ndef swp(str, i)\n c=str[i]\n str[i]=str[i+1]\n str[i+1]=c\nend\n\nans = []\n(n-1).downto(0).each do |ti|\n next if s[ti]==t[ti]\n si=ti-1\n si-=1 while si>=0 && s[si]!=t[ti]\n if si==-1\n puts \"-1\"\n exit 0\n end\n si.upto(ti-1).each do |i|\n swp(s, i)\n ans << i\n end\nend\n\nputs ans.size\nputs ans.join(\" \")"}], "src_uid": "48e323edc41086cae52cc0e6bdd84e35"} {"nl": {"description": "One day, liouzhou_101 got a chat record of Freda and Rainbow. Out of curiosity, he wanted to know which sentences were said by Freda, and which were said by Rainbow. According to his experience, he thought that Freda always said \"lala.\" at the end of her sentences, while Rainbow always said \"miao.\" at the beginning of his sentences. For each sentence in the chat record, help liouzhou_101 find whose sentence it is. ", "input_spec": "The first line of the input contains an integer n (1 ≤ n ≤ 10), number of sentences in the chat record. Each of the next n lines contains a sentence. A sentence is a string that contains only Latin letters (A-Z, a-z), underline (_), comma (,), point (.) and space ( ). Its length doesn’t exceed 100.", "output_spec": "For each sentence, output \"Freda's\" if the sentence was said by Freda, \"Rainbow's\" if the sentence was said by Rainbow, or \"OMG>.< I don't know!\" if liouzhou_101 can’t recognize whose sentence it is. He can’t recognize a sentence if it begins with \"miao.\" and ends with \"lala.\", or satisfies neither of the conditions. ", "sample_inputs": ["5\nI will go to play with you lala.\nwow, welcome.\nmiao.lala.\nmiao.\nmiao ."], "sample_outputs": ["Freda's\nOMG>.< I don't know!\nOMG>.< I don't know!\nRainbow's\nOMG>.< I don't know!"], "notes": null}, "positive_code": [{"source_code": "reF = Regexp.new( /lala\\.$/ )\nreR = Regexp.new( /^miao\\./ )\n\ngets.to_i.times do \n\tline = gets.chomp\n\tif ( reF =~ line && reR !~ line )\n\t\tputs \"Freda's\"\n\telsif ( reR =~ line && reF !~ line )\n\t\tputs \"Rainbow's\"\n\telse\n\t\tputs \"OMG>.< I don't know!\"\n\tend\nend\n"}, {"source_code": "\nans = []\ngets.to_i.times do\n s = gets.to_s.chomp\n if s[0..4] == 'miao.' && s[-5..-1] != 'lala.' then\n ans << 'Rainbow\\'s'\n elsif s[0..4] != 'miao.' && s[-5..-1] == 'lala.' then\n ans << 'Freda\\'s'\n else\n ans << 'OMG>.< I don\\'t know!'\n end\nend\nputs ans"}, {"source_code": "n = gets.to_i\nn.times do\n s = gets.gsub /\\r\\n|\\r|\\n/, ''\n is_f, is_r = false, false\n if s.length >= 5\n is_f = s[-5..-1] == 'lala.'\n is_r = s[0...5] == 'miao.'\n end\n if is_f and not is_r then\n puts \"Freda's\"\n elsif not is_f and is_r then\n puts \"Rainbow's\"\n else\n puts \"OMG>.< I don't know!\"\n end\nend\n"}, {"source_code": "n = gets.chomp.to_i\narStr = Array.new(n).map do |str|\n str = gets.chomp\nend\ni = 0\nwhile i != n\n if arStr[i][0..4] != 'miao.' && arStr[i][-5..-1] == 'lala.'\n puts \"Freda's\" \n elsif arStr[i][0..4] == 'miao.' && arStr[i][-5..-1] != 'lala.'\n puts \"Rainbow's\" \n else\n puts \"OMG>.< I don't know!\"\n end\n i += 1\nend"}, {"source_code": "def myfun\nn = gets.chomp.to_i\nstr = Array.new\nfor i in 1..n\n str[i] = gets.chomp\nend\n\nfor i in 1..n\n if str[i].index('miao.') == 0 && str[i].rindex('lala.') != str[i].length - 5\n puts \"Rainbow's\"\n elsif str[i].index('miao.') != 0 && str[i].rindex('lala.') == str[i].length - 5\n puts \"Freda's\"\n else\n puts \"OMG>.< I don't know!\"\n end\nend\nreturn nil\nend\n\nputs myfun\n"}, {"source_code": "#!/usr/bin/ruby1.9\n\nn = STDIN.readline.to_i\nn.times do\n\tsentence = STDIN.readline[0..-2]\n\tmiao = (sentence[0..4] == 'miao.')\n\tlala = (sentence[-5..-1] == 'lala.')\n\tif miao and not lala\n\t\tputs \"Rainbow's\"\n\telsif lala and not miao\n\t\tputs \"Freda's\"\n\telse\n\t\tputs \"OMG>.< I don't know!\"\n\tend\nend\n\n"}, {"source_code": "input = STDIN.read.split(\"\\n\")\ninput.shift\ninput.each do |line|\n r = line[0, 5] == 'miao.'\n f = line[-5..-1] == 'lala.'\n if not r ^ f\n puts 'OMG>.< I don\\'t know!'\n elsif r\n puts 'Rainbow\\'s'\n else\n puts 'Freda\\'s'\n end\nend\n"}, {"source_code": "n = gets.chomp.to_i\n(1..n).each do\n str = gets.chomp\n r = (str[(0..4)] == \"miao.\")\n f = (str[(-5..-1)] == \"lala.\")\n puts \"OMG>.< I don't know!\" if ((f && r) || (!f && !r))\n puts \"Freda's\" if f && !r\n puts \"Rainbow's\" if r && !f\nend"}, {"source_code": "class A\n def initialize\n n = gets.chomp.to_i\n\n freda = 0\n rain = 0\n\n answer = []\n\n n.times do \n text = gets.chomp\n\n if text =~ /lala\\.$/ && (text =~ /^miao\\./).nil?\n answer << \"Freda's\"\n elsif text =~ /^miao\\./ && (text =~ /lala\\.$/).nil?\n answer << \"Rainbow's\"\n else\n answer << \"OMG>.< I don't know!\"\n end\n end\n\n puts answer\n end\nend\n\na = A.new"}, {"source_code": "a=gets.chomp.to_i\na.times do\ntemp=gets.chomp\nl=temp.length\nif temp[0..4]==\"miao.\" && temp[l-5..l-1]==\"lala.\"\n puts \"OMG>.< I don't know!\" \nelsif temp[0..4]==\"miao.\"\n puts \"Rainbow's\"\nelsif temp[l-5..l-1]==\"lala.\"\n puts \"Freda's\"\nelse \nputs \"OMG>.< I don't know!\"\nend\nend"}, {"source_code": "n = gets.to_i\n(1..n).each {\n s = gets.chomp\n f = s.end_with?(\"lala.\")\n r = (s[0..4] == \"miao.\")\n if f ^ r\n if f\n puts \"Freda's\"\n else\n puts \"Rainbow's\"\n end\n else\n puts \"OMG>.< I don't know!\"\n end\n}"}, {"source_code": "gets.to_i.times do\n\tinput = gets.chomp\n\n\tt = ( ( input =~(/.*lala\\.$/)) )\n\to = ( ( input =~(/^miao\\..*/)) )\n\n\tif (o and t) or (!o and !t)\n\t\tputs \"OMG>.< I don't know!\"\n\telsif o\n\t\tputs \"Rainbow's\" \n\telsif t\n\t\tputs \"Freda's\"\n\tend\nend\n"}, {"source_code": "n = gets.to_i\n(0...n).each do\n s = gets.chomp\n if s.size < 5\n puts \"OMG>.< I don't know!\"\n elsif s[0,5] == \"miao.\" and s[s.size-5,5] != \"lala.\"\n puts \"Rainbow's\"\n elsif s[0,5] != \"miao.\" and s[s.size-5,5] == \"lala.\"\n puts \"Freda's\"\n else\n puts \"OMG>.< I don't know!\"\n end\nend\n"}, {"source_code": "\ngets.to_i.times do \n s = gets\n lala = /.*lala\\.$/.match s\n miao = /^miao\\..*/.match s\n if( (lala && miao) || (!lala && !miao) )\n puts \"OMG>.< I don't know!\"\n elsif lala\n puts \"Freda's\"\n else \n puts \"Rainbow's\"\n end\nend"}, {"source_code": "omg = \"OMG>.< I don't know!\"\nfreda = \"Freda's\"\nrainbow = \"Rainbow's\"\nrainbow_begin = /^miao\\./\nfreda_end = /lala\\.$/\n\nfor _ in 1..gets.to_i\n s = gets.chomp\n if s[rainbow_begin]\n puts(s[freda_end] ? omg : rainbow)\n elsif s[freda_end]\n puts freda\n else\n puts omg\n end\nend"}, {"source_code": "n = gets.to_i\nn.times do\n\ts = gets.chomp\n\tif s.length < 5\n\t\tputs \"OMG>.< I don't know!\"\n\telse\n\t\tR = s[0..4] == \"miao.\"\n\t\tF = s[-5..-1] == \"lala.\"\n\t\tif R == F\n\t\t\tputs \"OMG>.< I don't know!\"\n\t\telsif R\n\t\t\tputs \"Rainbow's\"\n\t\telse\n\t\t\tputs \"Freda's\"\n\t\tend\n\tend\nend\n"}, {"source_code": "#!/usr/bin/ruby\n\nn = gets.to_i\n\nn.times do |i|\n line = gets.chomp\n a = line.match(/^miao\\./)\n b = line.match(/lala\\.$/)\n\n if (a and b) or (not a and not b)\n puts \"OMG>.< I don't know!\"\n elsif line.start_with?(\"miao.\")\n puts \"Rainbow's\"\n elsif line.end_with?(\"lala.\")\n puts \"Freda's\"\n end\nend\n\n"}, {"source_code": "\nlines = STDIN.read.split(\"\\n\")\nn = lines[0].to_i\n\nfor i in 1..n \n line = lines[i]\n testA = line.rindex('lala.') == line.size - 5\n testB = line.index('miao.') == 0\n if testA && !testB\n puts \"Freda's\"\n next\n end\n if !testA && testB\n puts \"Rainbow's\"\n next\n end\n puts \"OMG>.< I don't know!\"\nend\n"}], "negative_code": [{"source_code": "n = gets.chomp.to_i\narStr = Array.new(n).map do |str|\n str = gets.chomp\nend\ni = 0\nwhile i != n\n if arStr[i][0..5] != 'miao.' && arStr[i][-5..-1] == 'lala.'\n puts \"Freda's\" \n elsif arStr[i][0..5] == 'miao.' && arStr[i][-5..-1] != 'lala.'\n puts \"Rainbow's\" \n else\n puts \"OMG>.< I don't know!\"\n end\n i += 1\nend"}, {"source_code": "n = gets.chomp.to_i\narStr = Array.new(n).map do |str|\n str = gets.chomp\nend\ni = 0\nwhile i != n\n if arStr[i][0..5] == 'miao.' && arStr[i][-6...-1] != 'lala.'\n puts \"Rainbow's\" \n elsif arStr[i][0..5] != 'miao.' && arStr[i][-6...-1] == 'lala.'\n puts \"Freda's\"\n else\n puts \"OMG>.< I don't know!\"\n end\n i += 1\nend"}, {"source_code": "n = gets.chomp.to_i\narStr = Array.new(n).map do |str|\n str = gets.chomp\nend\ni = 0\nwhile i != n\n if arStr[i][0..5] == 'miao.' && arStr[i][-6...-1] != 'lala.'\n puts \"Freda's\"\n elsif arStr[i][0..5] != 'miao.' && arStr[i][-5...-1] == 'lala.'\n puts \"Rainbow's\"\n else\n puts \"OMG>.< I don't know!\"\n end\n i += 1\nend"}, {"source_code": "n = gets.chomp.to_i\narStr = Array.new(n).map do |str|\n str = gets.chomp\nend\ni = 0\nwhile i != n\n if arStr[i][0..5] != 'miao.' && arStr[i][-6...-1] == 'lala.'\n puts \"Freda's\" \n elsif arStr[i][0..5] == 'miao.' && arStr[i][-6...-1] != 'lala.'\n puts \"Rainbow's\" \n else\n puts \"OMG>.< I don't know!\"\n end\n i += 1\nend"}, {"source_code": "n = gets.chomp.to_i\narStr = Array.new(n).map do |str|\n str = gets.chomp\nend\ni = 0\nwhile i != n\n if arStr[i][0..5] != 'miao.' && arStr[i][-6..-1] == 'lala.'\n puts \"Freda's\" \n elsif arStr[i][0..5] == 'miao.' && arStr[i][-6..-1] != 'lala.'\n puts \"Rainbow's\" \n else\n puts \"OMG>.< I don't know!\"\n end\n i += 1\nend"}, {"source_code": "n = gets.chomp.to_i\narStr = Array.new(n).map do |str|\n str = gets.chomp\nend\ni = 0\nwhile i != n\n if arStr[i][0..5] == 'miao.' && arStr[i][-5...-1] != 'lala.'\n puts \"Freda's\"\n elsif arStr[i][0..5] != 'miao.' && arStr[i][-5...-1] == 'lala.'\n puts \"Rainbow's\"\n else\n puts \"OMG>.< I don't know!\"\n end\n i += 1\nend"}, {"source_code": "def myfun\nn = gets.chomp.to_i\nstr = Array.new\nfor i in 1..n\n str[i] = gets.chomp\n puts i\nend\n\nfor i in 1..n\n if str[i].index('miao.') == 0 && str[i].rindex('lala.') != str[i].length - 5\n puts \"Rainbow's\"\n elsif str[i].index('miao.') != 0 && str[i].rindex('lala.') == str[i].length - 5\n puts \"Freda's\"\n else\n puts \"OMG>.< I don't know!\"\n end\nend\nreturn nil\nend\n\nputs myfun\n"}, {"source_code": "#!/usr/bin/ruby1.9\n\nn = STDIN.readline.to_i\nn.times do\n\tsentence = STDIN.readline.strip\n\tmiao = (sentence[0..4] == 'miao.')\n\tlala = (sentence[-5..-1] == 'lala.')\n\tif miao and not lala\n\t\tputs \"Rainbow's\"\n\telsif lala and not miao\n\t\tputs \"Freda's\"\n\telse\n\t\tputs \"OMG>..< I don't know!\"\n\tend\nend\n\n"}, {"source_code": "n = gets.chomp.to_i\n(1..n).each do\n str = gets.chomp\n r = (str[(0..4)] == \"miao.\")\n f = (str[(-5..-1)] == \"lala.\")\n puts \"OMG>.< I don't know!\" if ((f && r) || (!f && !r))\n puts \"Freda's\" if f\n puts \"Rainbow's\" if r\nend"}, {"source_code": "n = gets.chomp.to_i\n(1..n).each do\n str = gets.chomp\n r = (str[(0..5)] == \"miao.\")\n f = (str[(-5..-1)] == \"lala.\")\n puts \"OMG>.< I don't know!\" if ((f && r) || (!f && !r))\n puts \"Freda's\" if f\n puts \"Rainbow's\" if r\nend"}, {"source_code": "n = gets.chomp.to_i\n(1..n).each do\n str = gets.chomp\n r = (str[(-5..-1)] == \"miao.\")\n f = (str[(-5..-1)] == \"lala.\")\n puts \"OMG>.< I don't know!\" if ((f && r) || (!f && !r))\n puts \"Freda's\" if f\n puts \"Rainbow's\" if r\nend"}, {"source_code": "class A\n def initialize\n n = gets.chomp.to_i\n\n freda = 0\n rain = 0\n\n answer = []\n\n n.times do \n text = gets.chomp\n\n if text =~ /lala\\.$/ && (text =~ /miao\\./).nil?\n answer << \"Freda's\"\n elsif text =~ /^miao\\./ && (text =~ /lala\\./).nil?\n answer << \"Rainbow's\"\n else\n answer << \"OMG>.< I don't know!\"\n end\n end\n\n puts answer\n end\nend\n\na = A.new"}, {"source_code": "class A\n def initialize\n n = gets.chomp.to_i\n\n freda = 0\n rain = 0\n\n answer = []\n\n n.times do \n text = gets.chomp\n\n if text =~ /lala\\./ && (text =~ /miao\\./).nil?\n answer << \"Freda's\"\n elsif text =~ /miao\\./ && (text =~ /lala\\./).nil?\n answer << \"Rainbow's\"\n else\n answer << \"OMG>.< I don't know!\"\n end\n end\n\n puts answer\n end\nend\n\na = A.new"}, {"source_code": "class A\n def initialize\n n = gets.chomp.to_i\n\n freda = 0\n rain = 0\n\n answer = []\n\n n.times do \n text = gets.chomp\n\n if text =~ /lala\\./\n answer << \"Freda's\"\n elsif text =~ /mial\\./\n answer << \"Rainbow's\"\n else\n answer << \"OMG>.< I don't know!\"\n end\n end\n\n puts answer\n end\nend\n\na = A.new"}, {"source_code": "class A\n def initialize\n n = gets.chomp.to_i\n\n freda = 0\n rain = 0\n\n answer = []\n\n n.times do \n text = gets.chomp\n\n if text =~ /lala\\.$/ && (text =~ /miao\\./).nil?\n answer << \"Freda's\"\n elsif text =~ /^miao\\./ && (text =~ /lala\\./).nil?\n answer << \"Rainbow's\"\n else\n answer << \"OMG>.< I don't know!\"\n end\n end\n\n puts answer\n end\nend\n\na = A.new"}, {"source_code": "\n\nn = gets.to_i\n\n"}], "src_uid": "ee9ba877dee1a2843e885a18823cbff0"} {"nl": {"description": "Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.", "input_spec": "The first line contains two space-separated integers x1, y1 ( - 106 ≤ x1, y1 ≤ 106) — the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≤ x2, y2 ≤ 106) — the coordinates of the university you are studying at. The third line contains an integer n (1 ≤ n ≤ 300) — the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≤ ai, bi, ci ≤ 106; |ai| + |bi| > 0) — the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).", "output_spec": "Output the answer to the problem.", "sample_inputs": ["1 1\n-1 -1\n2\n0 1 0\n1 0 0", "1 1\n-1 -1\n3\n1 0 0\n0 1 0\n1 1 -3"], "sample_outputs": ["2", "2"], "notes": "NotePictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors): "}, "positive_code": [{"source_code": "x1, y1 = gets.split.map(&:to_i)\nx2, y2 = gets.split.map(&:to_i)\nn = gets.to_i\nr = 0\nn.times do\n a, b, c = gets.split.map(&:to_i)\n r += 1 if (a * x1 + b * y1 + c) * (a * x2 + b * y2 + c) < 0\nend\nputs r\n"}, {"source_code": "def getints\n gets.split.map &:to_i\nend\nx1,y1 = getints\nx2,y2 = getints\nn, = getints\ns = 0\nn.times do\n a,b,c = getints\n s += 1 if (a*x1+b*y1+c) * (a*x2+b*y2+c) < 0\nend\nputs s"}, {"source_code": "x1, y1 = STDIN.readline.split.map {|s| s.to_i }\nx2, y2 = STDIN.readline.split.map {|s| s.to_i }\nif x2 < x1\n x1, x2 = x2, x1\n y1, y2 = y2, y1\nend\nn = STDIN.readline.to_i\ncount = 0\n\nn.times do\n a, b, c = STDIN.readline.split.map {|s| s.to_i }\n if b == 0\n if (a*x1 < -c and -c < a*x2) or (a*x2 < -c and -c < a*x1)\n count += 1\n end\n else\n y3 = -a*x1 - c\n y4 = -a*x2 - c\n if (y3 < b*y1 and b*y2 < y4) or (b*y1 < y3 and y4 < b*y2)\n count += 1\n end\n end\nend\n\nputs count\n"}, {"source_code": "x1, y1 = gets.split.collect{|i| i.to_i}\nx2, y2 = gets.split.collect{|i| i.to_i}\nn = gets.to_i\nans = 0\nfor i in 0...n\n a, b, c = gets.split.collect{|i| i.to_i}\n ans += 1 if (a*x1+b*y1+c)*(a*x2+b*y2+c) < 0\nend\nputs ans\n"}, {"source_code": "x1, y1 = STDIN.readline.split.map {|s| s.to_i }\nx2, y2 = STDIN.readline.split.map {|s| s.to_i }\n\nn = STDIN.readline.to_i\ncount = 0\n\nn.times do\n a, b, c = STDIN.readline.split.map {|s| s.to_i }\n if ((a*x1 + b*y1 + c) * (a*x2 + b*y2 + c) < 0)\n count += 1\n end\nend\n\nputs count"}], "negative_code": [{"source_code": "def getints\n gets.split.map &:to_i\nend\nx1,y1 = getints\nx2,y2 = getints\nn, = getints\ns = 0\nn.times do\n a,b,c = getints\n s += 1 if (a*x1+b*y1-c) * (a*x2+b*y2-c) < 0\nend\nputs s"}, {"source_code": "x1, y1 = STDIN.readline.split.map {|s| s.to_i }\nx2, y2 = STDIN.readline.split.map {|s| s.to_i }\nif x2 < x1\n x1, x2 = x2, x1\n y1, y2 = y2, y1\nend\nn = STDIN.readline.to_i\ncount = 0\n\nn.times do\n a, b, c = STDIN.readline.split.map {|s| s.to_i }\n if b == 0\n if a*x1 < -c and -c < a*x2\n count += 1\n end\n else\n y3 = -a*x1 - c\n y4 = -a*x2 - c\n if (y3 < b*y1 and b*y2 < y4) or (b*y1 < y3 and y4 < b*y2)\n count += 1\n end\n end\nend\n\nputs count\n"}], "src_uid": "783df1df183bf182bf9acbb99208cdb7"} {"nl": {"description": "Brothers Fred and George Weasley once got into the sporting goods store and opened a box of Quidditch balls. After long and painful experiments they found out that the Golden Snitch is not enchanted at all. It is simply a programmed device. It always moves along the same trajectory, which is a polyline with vertices at the points (x0, y0, z0), (x1, y1, z1), ..., (xn, yn, zn). At the beginning of the game the snitch is positioned at the point (x0, y0, z0), and then moves along the polyline at the constant speed vs. The twins have not yet found out how the snitch behaves then. Nevertheless, they hope that the retrieved information will help Harry Potter and his team in the upcoming match against Slytherin. Harry Potter learned that at the beginning the game he will be at the point (Px, Py, Pz) and his super fast Nimbus 2011 broom allows him to move at the constant speed vp in any direction or remain idle. vp is not less than the speed of the snitch vs. Harry Potter, of course, wants to catch the snitch as soon as possible. Or, if catching the snitch while it is moving along the polyline is impossible, he wants to hurry the Weasley brothers with their experiments. Harry Potter catches the snitch at the time when they are at the same point. Help Harry.", "input_spec": "The first line contains a single integer n (1 ≤ n ≤ 10000). The following n + 1 lines contain the coordinates xi, yi, zi, separated by single spaces. The coordinates of any two consecutive points do not coincide. The next line contains the velocities vp and vs, the last line contains Px, Py, Pz, separated by single spaces. All the numbers in the input are integers, their absolute value does not exceed 104. The speeds are strictly positive. It is guaranteed that vs ≤ vp.", "output_spec": "If Harry Potter can catch the snitch while it is moving along the polyline (including the end (xn, yn, zn)), print \"YES\" in the first line (without the quotes). Print in the second line t, which is the earliest moment of time, when Harry will be able to catch the snitch. On the third line print three numbers X, Y, Z, the coordinates of the point at which this happens. The absolute or relative error in the answer should not exceed 10 - 6. If Harry is not able to catch the snitch during its moving along the described polyline, print \"NO\".", "sample_inputs": ["4\n0 0 0\n0 10 0\n10 10 0\n10 0 0\n0 0 0\n1 1\n5 5 25", "4\n0 0 0\n0 10 0\n10 10 0\n10 0 0\n0 0 0\n1 1\n5 5 50", "1\n1 2 3\n4 5 6\n20 10\n1 2 3"], "sample_outputs": ["YES\n25.5000000000\n10.0000000000 4.5000000000 0.0000000000", "NO", "YES\n0.0000000000\n1.0000000000 2.0000000000 3.0000000000"], "notes": null}, "positive_code": [{"source_code": "def solve(t0, t1)\n y0 = yield(t0)\n y1 = yield(t1)\n\n return nil if y1 > 1e-8\n return t0 if y0 == 0\n return t1 if y1 == 0\n\n 99.times do \n t2 = (t0 + t1) / 2\n y2 = yield(t2)\n\n if y2 > 0\n t0 = t2\n y0 = y2\n elsif y2 < 0\n t1 = t2\n y1 = y2\n else\n return t2\n end\n end\n\n (t0 + t1) / 2\nend\n\ndef dist(a, b)\n (0..2).inject(0){|s, i| s + (a[i] - b[i]) ** 2} ** 0.5\nend\n\nn = gets.to_i\na = (0..n).map{ gets.split.map(&:to_f) }\nvp, vs = gets.split.map(&:to_f)\nxp, yp, zp = pos = gets.split.map(&:to_f)\n\nt = 0.0\nn.times do |i|\n x0, y0, z0 = a[i]\n x1, y1, z1 = a[i+1]\n\n du = dist(a[i], a[i+1]) / vs\n vx = (x1 - x0) / du\n vy = (y1 - y0) / du\n vz = (z1 - z0) / du\n\n r = solve(0, du) { |dt| \n dist([x0 + dt * vx, y0 + dt * vy, z0 + dt * vz], [xp, yp, zp]) - vp * (t + dt)\n }\n\n if r\n puts \"YES\"\n puts t + r\n puts [x0 + r * vx, y0 + r * vy, z0 + r * vz] * \" \"\n exit\n end\n\n t += du\nend\nputs \"NO\""}, {"source_code": "EPS = 1e-8\n\ndef find_zero(t0, t1, depth=0, &block)\n y0 = yield(t0)\n y1 = yield(t1)\n\n return nil if y1 > EPS\n return t0 if y0 == 0\n return t1 if y1 == 0\n\n 100.times do \n t2 = (t0+t1)/2\n y2 = yield(t2)\n return t2 if y2 == 0\n\n if y2 > 0\n t0 = t2\n y0 = y2\n else\n t1 = t2\n y1 = y2\n end\n end\n\n (t0+t1)/2\nend\n\ndef distance(a, b)\n ((a[0]-b[0])**2 + (a[1]-b[1])**2 + (a[2]-b[2])**2)**0.5\nend\n\nn = gets.to_i\na = []\n(n+1).times{ a << gets.split.map(&:to_f)}\nvp, vs = gets.split.map(&:to_f)\nxp, yp, zp = pos = gets.split.map(&:to_f)\n\nt = 0.0\nn.times do |i|\n x0, y0, z0 = a[i]\n x1, y1, z1 = a[i+1]\n\n du = distance(a[i], a[i+1])/vs\n vx = (x1-x0)/du\n vy = (y1-y0)/du\n vz = (z1-z0)/du\n\n tm = find_zero(0, du) do |dt| \n distance([x0+dt*vx, y0+dt*vy, z0+dt*vz], [xp, yp, zp]) - vp*(t + dt)\n end\n\n if tm\n puts \"YES\"\n puts t+tm\n puts [x0+tm*vx, y0+tm*vy, z0+tm*vz]*\" \"\n exit\n end\n\n t += du\nend\nputs \"NO\"\n"}], "negative_code": [{"source_code": "def distance_lp(alpha, beta, pos)\n return distance(alpha, pos) if alpha == beta\n\n s1 = alpha.zip(beta,pos).inject(0.0){|r,(a,b,po)| r+(a+b-po)*(b-a)} \n s2 = alpha.zip(beta,pos).inject(0.0){|r,(a,b,po)| r+(b-a)*(b-a)} \n\n s = s1/s2\n if s < 0\n s = 0\n elsif s > 1\n s = 1\n end\n\n alpha.zip(beta,pos).inject(0.0){|r,(a,b,po)| r + (s*a + (1-s)*b - po)**2} **0.5\nend\n\ndef find_zero(t0, t1)\n y0 = yield(t0)\n return t0 if y0 == 0\n y1 = yield(t1)\n return t1 if y1 == 0\n\n t2 = (t0+t1)/2\n y2 = yield(t2)\n return t2 if y2 == 0 \n return nil if y2 > 0 && y1 > 0 && y0 > 0\n return nil if y2 > y1 && y2 > y0\n\n while t1-t0 > 1e6\n t3 = (t0+t2)/2\n y3 = yield(t3)\n return t3 if y3 == 0 \n end\nend\n\ndef find_zero_s(t0, t1, depth=0, &block)\n y0 = yield(t0)\n y1 = yield(t1)\n\n 20.times do \n t2 = (t0+t1)/2\n y2 = yield(t2)\n\n if y2 > 0\n t0 = t2\n y0 = y2\n else\n t1 = t2\n y1 = y2\n end\n\n #break if (y0 - y1).abs < 1e7\n end\n\n (t0+t1)/2\nend\n\nEPS = 1e-8\nDZ = 128\ndef find_zero(t0, t1, depth=0, &block)\n return t0 if yield(t0) <= EPS\n return t1 if yield(t1) <= EPS\n\n return (t0+t1)/2 if depth == 2\n return find_zero_s(t0, t1, &block) if depth >= 1\n\n yz = nil\n 0.upto(DZ) do |d|\n dt = (t1-t0)/DZ.to_f\n y = yield(t0 + d*dt) \n return (t0 + d*dt) if y == 0\n if y < 0\n return find_zero_s(t0+(d-1)*dt, t0+d*dt, &block)\n elsif y <= EPS\n return find_zero(t0+(d-1)*dt, t0+d*dt, depth+1, &block)\n end\n end\n return nil\nend\n\ndef distance(a, b)\n ((a[0]-b[0])**2 + (a[1]-b[1])**2 + (a[2]-b[2])**2)**0.5\nend\n\nn = gets.to_i\na = []\n(n+1).times{ a << gets.split.map(&:to_f)}\nvp, vs = gets.split.map(&:to_f)\nxp, yp, zp = pos = gets.split.map(&:to_f)\n\nt = 0.0\nn.times do |i|\n x0, y0, z0 = a[i]\n x1, y1, z1 = a[i+1]\n\n du = distance(a[i], a[i+1])/vs\n vx = (x1-x0)/du\n vy = (y1-y0)/du\n vz = (z1-z0)/du\n\n if distance_lp(a[i], a[i+1], pos) - (t+du)*vp < 1e5\n\n tm = find_zero(0, du) do |dt| \n distance([x0+dt*vx, y0+dt*vy, z0+dt*vz], [xp, yp, zp]) - vp*(t + dt)\n end\n\n if tm\n puts \"YES\"\n puts t+tm\n puts [x0+tm*vx, y0+tm*vy, z0+tm*vz]*\" \"\n exit\n end\n end\n\n t += du\nend\nputs \"NO\"\n"}, {"source_code": "def find_zero(t0, t1)\n y0 = yield(t0)\n return t0 if y0 == 0\n y1 = yield(t1)\n return t1 if y1 == 0\n\n t2 = (t0+t1)/2\n y2 = yield(t2)\n return t2 if y2 == 0 \n return nil if y2 > 0 && y1 > 0 && y0 > 0\n return nil if y2 > y1 && y2 > y0\n\n while t1-t0 > 1e6\n t3 = (t0+t2)/2\n y3 = yield(t3)\n return t3 if y3 == 0 \n end\nend\n\ndef find_zero_s(t0, t1, depth=0, &block)\n y0 = yield(t0)\n y1 = yield(t1)\n\n 20.times do \n t2 = (t0+t1)/2\n y2 = yield(t2)\n\n if y2 > 0\n t0 = t2\n y0 = y2\n else\n t1 = t2\n y1 = y2\n end\n end\n\n (t0+t1)/2\nend\n\nDZ = 100\ndef find_zero(t0, t1, depth=0, &block)\n return (t0+t1)/2 if depth == 2\n\n yz = nil\n 0.upto(DZ) do |d|\n dt = (t1-t0)/DZ.to_f\n y = yield(t0 + d*dt) \n return (t0 + d*dt) if y == 0\n if y < 0\n return find_zero_s(t0+(d-1)*dt, t0+d*dt, &block)\n elsif y <= 1e-8\n return find_zero(t0+(d-1)*dt, t0+d*dt, depth+1, &block)\n end\n end\n return nil\nend\n\ndef distance(a, b)\n ((a[0]-b[0])**2 + (a[1]-b[1])**2 + (a[2]-b[2])**2)**0.5\nend\n\nn = gets.to_i\na = []\n(n+1).times{ a << gets.split.map(&:to_f)}\nvp, vs = gets.split.map(&:to_f)\nxp, yp, zp = gets.split.map(&:to_f)\n\nt = 0.0\nn.times do |i|\n x0, y0, z0 = a[i]\n x1, y1, z1 = a[i+1]\n\n du = distance(a[i], a[i+1])/vs\n vx = (x1-x0)/du\n vy = (y1-y0)/du\n vz = (z1-z0)/du\n\n tm = find_zero(0, du) do |dt| \n distance([x0+dt*vx, y0+dt*vy, z0+dt*vz], [xp, yp, zp]) - vp*(t + dt)\n end\n\n if tm\n puts \"YES\"\n puts t+tm\n puts [x0+tm*vx, y0+tm*vy, z0+tm*vz]*\" \"\n exit\n end\n\n t += du\nend\nputs \"NO\"\n"}], "src_uid": "6e2a8aa58ed8cd308cb482e4c24cbbbb"} {"nl": {"description": "You are given an integer $$$n$$$ and an integer $$$k$$$.In one step you can do one of the following moves: decrease $$$n$$$ by $$$1$$$; divide $$$n$$$ by $$$k$$$ if $$$n$$$ is divisible by $$$k$$$. For example, if $$$n = 27$$$ and $$$k = 3$$$ you can do the following steps: $$$27 \\rightarrow 26 \\rightarrow 25 \\rightarrow 24 \\rightarrow 8 \\rightarrow 7 \\rightarrow 6 \\rightarrow 2 \\rightarrow 1 \\rightarrow 0$$$.You are asked to calculate the minimum number of steps to reach $$$0$$$ from $$$n$$$. ", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of queries. The only line of each query contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le n \\le 10^{18}$$$, $$$2 \\le k \\le 10^{18}$$$).", "output_spec": "For each query print the minimum number of steps to reach $$$0$$$ from $$$n$$$ in single line. ", "sample_inputs": ["2\n59 3\n1000000000000000000 10"], "sample_outputs": ["8\n19"], "notes": "NoteSteps for the first test case are: $$$59 \\rightarrow 58 \\rightarrow 57 \\rightarrow 19 \\rightarrow 18 \\rightarrow 6 \\rightarrow 2 \\rightarrow 1 \\rightarrow 0$$$.In the second test case you have to divide $$$n$$$ by $$$k$$$ $$$18$$$ times and then decrease $$$n$$$ by $$$1$$$."}, "positive_code": [{"source_code": "test_cases = gets.chomp.to_i\n\ntest_cases.times do |tc|\n n, k = gets.chomp.split(\" \").map(&:to_i)\n\n counter = 0\n\n while n != 0\n if n % k == 0\n n = n / k\n counter += 1\n else\n rem = n % k\n counter += rem\n n -= rem\n end\n end\n\n puts(counter)\nend"}, {"source_code": "gets.to_i.times do\n ans = 0\n n,k = gets.split.map(&:to_i)\n while n != 0\n if n%k == 0\n ans += 1\n n /= k\n else\n ans += n%k\n n -= n%k\n end\n end\n puts ans\nend"}, {"source_code": "n = gets.chomp.to_i\n\ntests = []\n\nn.times do\n tests.push(gets.split(' ').map {|e| e.to_i})\nend\n\ntests.each do |test|\n remainder = 0\n current_num = test[0]\n operations = 0\n while current_num >= test[1] do\n remainder += current_num % test[1]\n current_num = current_num / test[1]\n operations += 1\n end\n\n operations += remainder + current_num\n \n puts operations\nend\n"}], "negative_code": [], "src_uid": "00b1e45e9395d23e850ce1a0751b8378"} {"nl": {"description": "Lee is going to fashionably decorate his house for a party, using some regular convex polygons...Lee thinks a regular $$$n$$$-sided (convex) polygon is beautiful if and only if he can rotate it in such a way that at least one of its edges is parallel to the $$$OX$$$-axis and at least one of its edges is parallel to the $$$OY$$$-axis at the same time.Recall that a regular $$$n$$$-sided polygon is a convex polygon with $$$n$$$ vertices such that all the edges and angles are equal.Now he is shopping: the market has $$$t$$$ regular polygons. For each of them print YES if it is beautiful and NO otherwise.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of polygons in the market. Each of the next $$$t$$$ lines contains a single integer $$$n_i$$$ ($$$3 \\le n_i \\le 10^9$$$): it means that the $$$i$$$-th polygon is a regular $$$n_i$$$-sided polygon. ", "output_spec": "For each polygon, print YES if it's beautiful or NO otherwise (case insensitive).", "sample_inputs": ["4\n3\n4\n12\n1000000000"], "sample_outputs": ["NO\nYES\nYES\nYES"], "notes": "NoteIn the example, there are $$$4$$$ polygons in the market. It's easy to see that an equilateral triangle (a regular $$$3$$$-sided polygon) is not beautiful, a square (a regular $$$4$$$-sided polygon) is beautiful and a regular $$$12$$$-sided polygon (is shown below) is beautiful as well. "}, "positive_code": [{"source_code": "require 'set'; require 'prime'; require 'pp'\nINF=Float::INFINITY; MOD=10**9+7\nt = gets.chomp.to_i\nt.times do\n n = gets.chomp.to_i\n if n % 4 == 0\n puts 'YES'\n else\n puts 'NO'\n end\nend\n"}, {"source_code": "n = gets.chomp.to_i\nn.times do \n t = gets.chomp.to_i\n # kakudo = 360 * 10000000000000000000/ t\n # # puts kakudo\n # ama = 360 * 10000000000000000000 % t\n # # puts \"YY\"\n # # puts ama\n # if ama != 0\n # puts \"NO\"\n # next\n # end\n # hantei = 90 * 10000000000000000000 / kakudo\n # amari = 90 * 10000000000000000000 % kakudo\n # if amari != 0\n # puts \"NO\"\n # next\n # end\n # saigo = (hantei * kakudo) % 10000000000000000000\n hantei = t % 4\n\n\n if hantei == 0\n puts \"YES\"\n else\n puts \"NO\"\n end\nend\n"}, {"source_code": "t = gets.chomp.to_i\nt.times do \n\tn = gets.chomp.to_i\n\tputs n % 4 == 0 ? \"YES\" : \"NO\"\nend\n"}, {"source_code": "gets.to_i.times { puts gets.to_i.modulo(4).zero? ? \"YES\" : \"NO\" }\n"}, {"source_code": "n = gets.chomp.to_i\nn.times {\n arrNum = gets.chomp.to_i\n if arrNum%4 != 0\n puts 'NO'\n else\n puts 'YES'\n end\n}\n\n"}], "negative_code": [{"source_code": "n = gets.chomp.to_i\nn.times do |i|\n t = gets.chomp.to_i\n kakudo = 360 * 10000000000000000000/ t\n ama = 360 * 10000000000000000000 % t\n hantei = 90 * 10000000000000000000 / kakudo\n amari = 90 * 10000000000000000000 % kakudo\n if hantei * kakudo / 10000000000000000000 == 90 && amari == 0 && ama == 0\n puts \"YES\"\n else\n puts \"NO\"\n end\nend"}, {"source_code": "n = gets.chomp.to_i\nn.times do \n t = gets.chomp.to_i\n kakudo = 360 * 10000000000000000000/ t.to_f\n # puts kakudo\n ama = 360 * 10000000000000000000 % t\n # puts \"YY\"\n # puts ama\n if ama != 0\n puts \"NO\"\n next\n end\n hantei = 90 * 10000000000000000000 / kakudo\n amari = 90 * 10000000000000000000 % kakudo\n if amari != 0\n puts \"NO\"\n next\n end\n if hantei * kakudo / 10000000000000000000 == 90 \n puts \"YES\"\n else\n puts \"NO\"\n end\nend"}, {"source_code": "n = gets.chomp.to_i\nn.times do \n t = gets.chomp.to_i\n # kakudo = 360 * 10000000000000000000/ t\n # # puts kakudo\n # ama = 360 * 10000000000000000000 % t\n # # puts \"YY\"\n # # puts ama\n # if ama != 0\n # puts \"NO\"\n # next\n # end\n # hantei = 90 * 10000000000000000000 / kakudo\n # amari = 90 * 10000000000000000000 % kakudo\n # if amari != 0\n # puts \"NO\"\n # next\n # end\n # saigo = (hantei * kakudo) % 10000000000000000000\n hantei = n % 4\n\n\n if hantei == 0\n puts \"YES\"\n else\n puts \"NO\"\n end\nend\n"}, {"source_code": "n = gets.chomp.to_i\nn.times do \n t = gets.chomp.to_i\n kakudo = 360 * 10000000000000000000/ t\n # puts kakudo\n ama = 360 * 10000000000000000000 % t\n # puts \"YY\"\n # puts ama\n if ama != 0\n puts \"NO\"\n next\n end\n hantei = 90 * 10000000000000000000 / kakudo\n amari = 90 * 10000000000000000000 % kakudo\n if amari != 0\n puts \"NO\"\n next\n end\n if hantei * kakudo / 10000000000000000000 == 90 \n puts \"YES\"\n else\n puts \"NO\"\n end\nend"}, {"source_code": "n = gets.chomp.to_i\nn.times do |i|\n t = gets.chomp.to_i\n kakudo = 360 * 10000000000000000000/ t.to_f\n # puts kakudo\n ama = 360 * 10000000000000000000 % t\n # puts \"YY\"\n # puts ama\n if ama != 0\n puts \"NO\"\n next\n end\n hantei = 90 * 10000000000000000000 / kakudo\n amari = 90 * 10000000000000000000 % kakudo\n if amari != 0\n puts \"NO\"\n next\n end\n if hantei * kakudo / 10000000000000000000 == 90 \n puts \"YES\"\n else\n puts \"NO\"\n end\nend"}, {"source_code": "n = gets.chomp.to_i\nn.times do |i|\n t = gets.chomp.to_i\n kakudo = 360 * 10000000000000000000/ t\n #puts kakudo\n hantei = 90 * 10000000000000000000 / kakudo\n amari = 90 * 10000000000000000000 % kakudo\n if hantei * kakudo / 10000000000000000000 == 90 && amari == 0\n puts \"YES\"\n else\n puts \"NO\"\n end\nend"}, {"source_code": "n = gets.chomp.to_i\nn.times do |i|\n t = gets.chomp.to_i\n kakudo = 360 * 1000000000/ t\n hantei = 90 * 1000000000 / kakudo\n if hantei * kakudo / 1000000000 == 90\n puts \"YES\"\n else\n puts \"NO\"\n end\nend"}, {"source_code": "n = gets.chomp.to_i\nn.times do \n t = gets.chomp.to_i\n kakudo = 360 * 10000000000000000000/ t\n # puts kakudo\n ama = 360 * 10000000000000000000 % t\n # puts \"YY\"\n # puts ama\n if ama != 0\n puts \"NO\"\n next\n end\n hantei = 90 * 10000000000000000000 / kakudo\n amari = 90 * 10000000000000000000 % kakudo\n if amari != 0\n puts \"NO\"\n next\n end\n saigo = (hantei * kakudo) % 10000000000000000000\n if hantei * kakudo / 10000000000000000000 == 90 && saigo == 0\n puts \"YES\"\n else\n puts \"NO\"\n end\nend"}, {"source_code": "n = gets.chomp.to_i\nn.times {\n arrNum = gets.chomp.to_i\n if arrNum%2 != 0\n puts 'NO'\n else\n puts 'YES'\n end\n}\n\n"}], "src_uid": "07e56d4031bcb119d2f684203f7ed133"} {"nl": {"description": "You are given an array a. Some element of this array ai is a local minimum iff it is strictly less than both of its neighbours (that is, ai < ai - 1 and ai < ai + 1). Also the element can be called local maximum iff it is strictly greater than its neighbours (that is, ai > ai - 1 and ai > ai + 1). Since a1 and an have only one neighbour each, they are neither local minima nor local maxima.An element is called a local extremum iff it is either local maximum or local minimum. Your task is to calculate the number of local extrema in the given array.", "input_spec": "The first line contains one integer n (1 ≤ n ≤ 1000) — the number of elements in array a. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1000) — the elements of array a.", "output_spec": "Print the number of local extrema in the given array.", "sample_inputs": ["3\n1 2 3", "4\n1 5 2 5"], "sample_outputs": ["0", "2"], "notes": null}, "positive_code": [{"source_code": "n,*a=$<.read.split.map &:to_i\np a.each_cons(3).count{|x,y,z|(x>y&&z>y)||(xy&&z>y)||(x a[i + 1] && a[i] > a[i - 1]\n\t\ttotal += 1\n\tend\nend\nputs total"}, {"source_code": "gets\nx1=gets.split.map{|e| e.to_i}\nx2=x1.clone\nx2.shift\nx3=x2.clone\nx3.shift\nx1=x1.zip(x2,x3)\nx1.pop\nx1.pop\nputs x1.count{|e|\n\t(e[0]e[2]) || (e[0]>e[1] && e[1] A[i - 1] && A[i] > A[i + 1])\n} .size\n"}, {"source_code": "def extremum(a, b, c)\n a = a.to_i\n b = b.to_i\n c = c.to_i\n if a < b && b > c\n return true\n elsif a > b && b < c\n return true\n else\n return false\n end\nend\n\nans = 0\nn = gets.to_i\na = gets.split\nfor i in (1...n-1) do\n if extremum(a[i - 1], a[i], a[i + 1])\n ans = ans + 1\n end\nend\np ans\n"}, {"source_code": "n = Integer(gets.chomp)\n\na = gets.chomp\n .split\n .map{ |i| Integer(i) }\n\nprint (1...n-1).inject(0){ |res, i|\n ((a[i] > a[i-1] and a[i] > a[i+1]) or (a[i-1] > a[i] and a[i+1] > a[i])) ? res + 1 : res}"}, {"source_code": "n=gets.to_i\na=gets.split ' '\na=a.collect{|i| i.to_i}\nsum=0\nfor i in 1..n-2\nif a[i]a[i-1] && a[i]>a[i+1]\n sum+=1\nend\nend\nprint sum"}, {"source_code": "n= gets.to_i\na = gets.split(\" \").map(&:to_i)\ncount = 0\nfor i in 1..n-2\n\tif (a[i]>a[i-1] && a[i]>a[i+1]) || (a[i]y&&z>y)||(xy&&z>y)||(xe[2]\n}\nif ans\n\tputs ans+2\nelse\n\tputs 0\nend"}, {"source_code": "n=gets.to_i\na=gets.split ' '\na=a.collect{|i| i.to_i}\nsum=0\nfor i in 1..n-2\nif a[i]a[i-1] and a[i]>a[i+1]\n sum+=1\nend\nend\nprint sum"}, {"source_code": "n=gets.to_i\na=gets.split ' '\na=a.collect{|i| i.to_i}\nsum=0\nfor i in 2..n-2\nif a[i]a[i-1] and a[i]>a[i+1]\n sum+=1\nend\nend\nprint sum"}], "src_uid": "67cf9f83ed791a614bd01c5e0310813f"} {"nl": {"description": "A new Berland businessman Vitaly is going to open a household appliances' store. All he's got to do now is to hire the staff.The store will work seven days a week, but not around the clock. Every day at least k people must work in the store.Berland has a law that determines the order of working days and non-working days. Namely, each employee must work for exactly n consecutive days, then rest for exactly m days, then work for n more days and rest for m more, and so on. Vitaly doesn't want to break the law. Fortunately, there is a loophole: the law comes into force on the day when the employee is hired. For example, if an employee is hired on day x, then he should work on days [x, x + 1, ..., x + n - 1], [x + m + n, x + m + n + 1, ..., x + m + 2n - 1], and so on. Day x can be chosen arbitrarily by Vitaly.There is one more thing: the key to the store. Berland law prohibits making copies of keys, so there is only one key. Vitaly is planning to entrust the key to the store employees. At the same time on each day the key must be with an employee who works that day — otherwise on this day no one can get inside the store. During the day the key holder can give the key to another employee, if he also works that day. The key will handed to the first hired employee at his first working day.Each employee has to be paid salary. Therefore, Vitaly wants to hire as few employees as possible provided that the store can operate normally on each day from 1 to infinity. In other words, on each day with index from 1 to infinity, the store must have at least k working employees, and one of the working employees should have the key to the store.Help Vitaly and determine the minimum required number of employees, as well as days on which they should be hired.", "input_spec": "The first line contains three integers n, m and k (1 ≤ m ≤ n ≤ 1000, n ≠ 1, 1 ≤ k ≤ 1000).", "output_spec": "In the first line print a single integer z — the minimum required number of employees. In the second line print z positive integers, separated by spaces: the i-th integer ai (1 ≤ ai ≤ 104) should represent the number of the day, on which Vitaly should hire the i-th employee. If there are multiple answers, print any of them.", "sample_inputs": ["4 3 2", "3 3 1"], "sample_outputs": ["4\n1 1 4 5", "3\n1 3 5"], "notes": null}, "positive_code": [{"source_code": "n,m,k=gets.split.map &:to_i;z=[1]*k+[n]+[n+1]*(k-1);if m>n-(k>1?1:2) then z<<2*n-(k>1?0:1) end;n==2&&m==2&&k==1&&z<<4;p z.size;puts z.join' '\n"}, {"source_code": "n,m,k=gets.split.map &:to_i;z=[1]*k+[n]+[n+1]*(k-1);if m>n-(k>1?1:2) then z<<2*n-(k>1?0:1) end;n==2&&m==2&&k==1&&z<<4;p z.size;puts z.join' '\n"}, {"source_code": "n,m,k=gets.split.map &:to_i;z=[1]*k+[n]+[n+1]*(k-1);if m>n-(k>1?1:2) then z<<2*n-(k>1?0:1) end;n==2&&m==2&&k==1&&z<<4;p z.size;puts z.join' '\n"}, {"source_code": "n,m,k=gets.split.map &:to_i;z=[1]*k+[n]+[n+1]*(k-1);if m>n-(k>1?1:2) then z<<2*n-(k>1?0:1) end;n==2&&m==2&&k==1&&z<<4;p z.size;puts z.join' '\n"}, {"source_code": "n,m,k=gets.split.map &:to_i;z=[1]*k+[n]+[n+1]*(k-1);if m>n-(k>1?1:2) then z<<2*n-(k>1?0:1) end;n==2&&m==2&&k==1&&z<<4;p z.size;puts z.join' '"}, {"source_code": "n,m,k=gets.split.map &:to_i;z=[1]*k+[n]+[n+1]*(k-1);if m>n-(k>1?1:2) then z<<2*n-(k>1?0:1) end;n==2&&m==2&&k==1&&z<<4;p z.size;puts z.join' '\n"}, {"source_code": "n,m,k=gets.split.map &:to_i;z=[1]*k+[n]+[n+1]*(k-1);if m>n-(k>1?1:2) then z<<2*n-(k>1?0:1) end;n==2&&m==2&&k==1&&z<<4;p z.size;puts z.join' '\n"}, {"source_code": "n,m,k=gets.split.map &:to_i;z=[1]*k+[n]+[n+1]*(k-1);if m>n-(k>1?1:2) then z<<2*n-(k>1?0:1) end;n==2&&m==2&&k==1&&z<<4;p z.size;puts z.join' '\n"}, {"source_code": "n,m,k=gets.split.map &:to_i;z=[1]*k+[n]+[n+1]*(k-1);if m>n-(k>1?1:2) then z<<2*n-(k>1?0:1) end;n==2&&m==2&&k==1&&z<<4;p z.size;puts z.join' '\n"}], "negative_code": [{"source_code": "n,m,k=gets.split.map &:to_i;z=[1]*k+[n]+[n+1]*(k-1);if m>n-(k>1?1:2) then z<<2*n-1 end;p z.size;puts z.join' '"}], "src_uid": "113997707143a57f7e81fb14d9bbde96"} {"nl": {"description": "Appleman has n cards. Each card has an uppercase letter written on it. Toastman must choose k cards from Appleman's cards. Then Appleman should give Toastman some coins depending on the chosen cards. Formally, for each Toastman's card i you should calculate how much Toastman's cards have the letter equal to letter on ith, then sum up all these quantities, such a number of coins Appleman should give to Toastman.Given the description of Appleman's cards. What is the maximum number of coins Toastman can get?", "input_spec": "The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105). The next line contains n uppercase letters without spaces — the i-th letter describes the i-th card of the Appleman.", "output_spec": "Print a single integer – the answer to the problem.", "sample_inputs": ["15 10\nDZFDFZDFDDDDDDF", "6 4\nYJSNPI"], "sample_outputs": ["82", "4"], "notes": "NoteIn the first test example Toastman can choose nine cards with letter D and one additional card with any letter. For each card with D he will get 9 coins and for the additional card he will get 1 coin."}, "positive_code": [{"source_code": "require 'pp'\n\nn,k = STDIN.gets.chomp.split(/ /).map(&:to_i)\nh = {}\nans = 0\n\nSTDIN.gets.chomp.split(//).each do |c|\n h[c] ||= 0\n h[c] += 1\nend\n\nvs = h.values.sort.reverse\nvs.each do |v|\n if v <= k\n ans += v*v\n k -= v\n else\n ans += k*k\n k = 0\n end\nend\n\nputs ans\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\ns = gets.chomp.chars\nc = s.uniq.map{|x| s.count(x)}.sort.reverse\nans = 0\nc.each do |x|\n t = [x, k].min\n ans += t * t\n k -= t\nend\np ans"}, {"source_code": "n, k = gets.split.map { |x| x.to_i }\n\ncards = Hash.new(0)\n\ngets.split(//).each { |card| cards[card] += 1 }\n\ncards = cards.to_a.sort { |c1, c2| c2[1] <=> c1[1] }\n\nans = 0\ni = 0\n\nwhile k > 0\n x = cards[i][1]\n\n if x > k\n ans += k ** 2\n k = 0\n elsif x <= k\n ans += x ** 2\n k -= x\n end\n \n i += 1\nend\n\n\nputs ans"}, {"source_code": "n,k=gets.split(/ /).map(&:to_i)\nh=Hash.new\ngets.split(//).each do |i|\n h[i]?h[i]+=1:h[i]=1\nend\nans=0\nh.values.sort.reverse.each do |i|\n if i<=k\n ans+=i**2\n k-=i\n else\n ans+=k*k\n k=0\n end\nend\nputs ans"}, {"source_code": "nAndk = gets.chomp.split(' ').map { |x| x.to_i }\nn = nAndk[0]\nk = nAndk[1]\nbins = {}\ncards = gets.chomp.split('')\n\nfor card in cards\n if bins[card].nil?\n bins[card] = 0\n end\n bins[card] += 1\nend\nwork = bins.sort_by {|_key, value| value}\nscore = 0\nwhile k > 0\n count = work.last[1]\n if count < k\n k -= count\n score += bins[work.last[0]] ** 2\n work.pop\n else\n score += k*k\n k = 0 \n end\nend\np score\n"}, {"source_code": "n,k = gets.split(/\\s+/).map(&:to_i)\nh = Hash.new { |h, k| h[k] = 0 }\ngets.strip.split(//).inject(h) {|h,s| h[s] += 1; h}\n\nresult = 0\n\nh.values.sort.reverse.each do |v|\n if v < k\n k -= v\n result += v**2\n else\n result += k**2\n break\n end\nend\n\np result\n\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\ns = gets.chomp.chars\nc = s.uniq.map{|x| s.count(x)}.sort.reverse\nans = 0\nt = 0\nc.each do |i|\n t = [i, k].min\n ans += t * t\n break if t == k\n k -= t\nend\np ans\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\ns = gets.chomp.chars\nc = s.uniq.map{|x| s.count(x)}.sort.reverse\nans = 0\nc.each do |x|\n t = [x, k].min\n ans += t * t\n k -= t\nend\np ans\n"}, {"source_code": "n,k=gets.split.map(&:to_i)\nstr=gets.to_s.chomp\narr=str.split(//)\n# hsh=arr.inject(Hash.new(0)) { |h,e| h[e] += 1; h }.select { \n# |k,v| v > 1 }.inject({}) { |r, e| r[e.first] = e.last; r }\n\nhsh = Hash.new(0)\narr.each { | v | hsh.store(v, hsh[v]+1) }\n\nsum=0\nkol=0\nwhile (kol b[1]\nend.reverse!\noutput = 0\n\nchar_ar.each do |a|\n\tb = if ka >= a[1]\n\t\ta[1] \n\telse\n\t ka\n\tend \n\tka -= b\n\toutput += (b ** 2)\n\tbreak if ka < 1\nend\nputs output\n# a => 1, b => 5"}, {"source_code": "\nn, k = gets.split.map(&:to_i)\ntab = gets.strip.split(//)\namount = {}\n#p tab\ntab.each do |i|\n if amount[i] == nil\n amount[i] = 1\n else\n amount[i] += 1\n end\nend\n#amount = tab.inject(0){|x, y| p y}\n#amount = tab.inject({}){|x, y| if x[y] == nil; x[y] = 1 else x[y] += 1 end}\n\namount = amount.to_a.map(&:reverse).sort.reverse\nresult = 0\ni = 0\n\nwhile i < amount.size && k > 0\n result += [amount[i][0], k].min**2\n k -= [amount[i][0], k].min\n i += 1\nend\n\nputs result\n\n\n"}], "negative_code": [{"source_code": "n, k = gets.split.map(&:to_i)\ns = gets.chomp.chars\nc = s.uniq.map{|x| s.count(x)}.sort.reverse\nans = 0\nt = 0\naccum = 0\nc.each do |i|\n t = [i, k].min\n accum += t\n ans += t * t\n break if accum == k\n k -= t\nend\np ans\n"}, {"source_code": "line = gets\ni, ka = line.split.map(&:to_i)\nline = gets\nchar_count = {}\nline.each_char do |char|\n\tchar_count[char] ||= 0\n\tchar_count[char] += 1 \nend\nchar_ar = []\nchar_count.each do |k, v|\n\tchar_ar << [k, v]\nend\nchar_ar.sort! do |a, b|\n\ta[1] <=> b[1]\nend\n\noutput = 0\n\nchar_ar.each do |a|\n\tbreak if ka < 1\n\tb = if a[1] > ka\n\t\tka\n\telse\n\t\tka - a[1]\n\tend \n\tputs b\n\tka -= b\n\toutput += b ** 2\nend\nputs output\n# a => 1, b => 5"}, {"source_code": "line = gets\ni, ka = line.split.map(&:to_i)\nline = gets\nchar_count = {}\nline.each_char do |char|\n\tchar_count[char] ||= 0\n\tchar_count[char] += 1 \nend\nchar_ar = []\nchar_count.each do |k, v|\n\tchar_ar << [k, v]\nend\nchar_ar.sort! do |a, b|\n\ta[1] <=> b[1]\nend\n\noutput = 0\n\nchar_ar.each do |a|\n\tbreak if ka < 1\n\tb = if a[1] > ka\n\t\tka\n\telse\n\t\tka - a[1]\n\tend \n\tka -= b\n\toutput += b ** 2\nend\nputs output\n# a => 1, b => 5"}, {"source_code": "\nn, k = gets.split.map(&:to_i)\ntab = gets.strip.split(//)\namount = {}\n#p tab\ntab.each do |i|\n if amount[i] == nil\n amount[i] = 1\n else\n amount[i] += 1\n end\nend\n#amount = tab.inject(0){|x, y| p y}\n#amount = tab.inject({}){|x, y| if x[y] == nil; x[y] = 1 else x[y] += 1 end}\n\namount = amount.to_a.map(&:reverse).sort.reverse\nresult = 0\ni = 0\n\nwhile i < amount.size && k > 0\n result += [amount[i][0], k].min**2\n k -= [amount[i][0], k].min\nend\n\nputs result\n\n\n"}], "src_uid": "480defc596ee5bc800ea569fd76dc584"} {"nl": {"description": "Little Janet likes playing with cubes. Actually, she likes to play with anything whatsoever, cubes or tesseracts, as long as they are multicolored. Each cube is described by two parameters — color ci and size si. A Zebra Tower is a tower that consists of cubes of exactly two colors. Besides, the colors of the cubes in the tower must alternate (colors of adjacent cubes must differ). The Zebra Tower should have at least two cubes. There are no other limitations. The figure below shows an example of a Zebra Tower. A Zebra Tower's height is the sum of sizes of all cubes that form the tower. Help little Janet build the Zebra Tower of the maximum possible height, using the available cubes.", "input_spec": "The first line contains an integer n (2 ≤ n ≤ 105) — the number of cubes. Next n lines contain the descriptions of the cubes, one description per line. A cube description consists of two space-separated integers ci and si (1 ≤ ci, si ≤ 109) — the i-th cube's color and size, correspondingly. It is guaranteed that there are at least two cubes of different colors.", "output_spec": "Print the description of the Zebra Tower of the maximum height in the following form. In the first line print the tower's height, in the second line print the number of cubes that form the tower, and in the third line print the space-separated indices of cubes in the order in which they follow in the tower from the bottom to the top. Assume that the cubes are numbered from 1 to n in the order in which they were given in the input. If there are several existing Zebra Towers with maximum heights, it is allowed to print any of them. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.", "sample_inputs": ["4\n1 2\n1 3\n2 4\n3 3", "2\n1 1\n2 1"], "sample_outputs": ["9\n3\n2 3 1", "2\n2\n2 1"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\nh = Hash.new{[]}\nn.times{|i|\n c, s = gets.split.map(&:to_i)\n h[c] <<= [s, i + 1]\n}\n\na = Array.new(n){[]}\nh.each{|c, s|\n s.sort!.reverse!\n s.each_cons(2){|i, j|\n j[0] += i[0]\n }\n s.each_index{|i|\n a[i] << s\n }\n}\n\nn.times{|i|\n a[i].sort_by!{|_| -_[i][0]}\n}\nz = [0]\nn.times{|i|\n [[0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 1, 0]].each{|x, j, y|\n j += i\n b, c = a[i][x], a[j] && a[j][y]\n z = [z, [b[i][0] + c[j][0], i, x, j, y]].max if b && c && !b.equal?(c)\n }\n}\n\nu, i, x, j, y = z\np u\nv = a[j][y][0..j].zip(a[i][x][0..i]).inject(&:concat).compact\np v.size\nputs v.map{|_| _[1]} * ' '\n"}, {"source_code": "n = gets.to_i\nh = Hash.new{[]}\nn.times{|i|\n c, s = gets.split.map(&:to_i)\n h[c] <<= [s, i + 1]\n}\n\na = Array.new(n){[]}\nh.each{|c, s|\n s.sort!.reverse!\n s.each_cons(2){|i, j|\n j[0] += i[0]\n }\n s.each_index{|i|\n a[i] << s\n }\n}\n\nn.times{|i|\n a[i].sort_by!{|_| -_[i][0]}\n}\nz = [0]\nn.times{|i|\n [[0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 1, 0]].each{|x, j, y|\n j += i\n b, c = a[i][x], a[j] && a[j][y]\n z = [z, [b[i][0] + c[j][0], i, x, j, y]].max if b && c && !b.equal?(c)\n }\n}\n\nu, i, x, j, y = z\np u\nv = a[j][y][0..j].zip(a[i][x][0..i]).inject(&:concat).compact\np v.size\nputs v.map{|_| _[1]} * ' '\n\n"}, {"source_code": "n = gets.to_i\nh = Hash.new{[]}\nn.times{|i|\n c, s = gets.split.map(&:to_i)\n h[c] <<= [s, i + 1]\n}\n\na = Array.new(n){[]}\nh.each{|c, s|\n s.sort!.reverse!\n s.each_cons(2){|i, j|\n j[0] += i[0]\n }\n s.each_index{|i|\n a[i] << s\n }\n}\n\nn.times{|i|\n a[i].sort_by!{|_| -_[i][0]}\n}\nz = [0]\nn.times{|i|\n [[0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 1, 0]].each{|x, j, y|\n j += i\n b, c = a[i][x], a[j] && a[j][y]\n z = [z, [b[i][0] + c[j][0], i, x, j, y]].max if b && c && !b.equal?(c)\n }\n}\n\nu, i, x, j, y = z\np u\nv = a[j][y][0..j].zip(a[i][x][0..i]).inject(&:concat).compact\np v.size\nputs v.map{|_| _[1]} * ' '\n\n"}, {"source_code": "n = gets.to_i\nh = Hash.new{[]}\nn.times{|i|\n c, s = gets.split.map(&:to_i)\n h[c] <<= [s, i + 1]\n}\n\na = Array.new(n){[]}\nh.each{|c, s|\n s.sort!.reverse!\n s.each_cons(2){|i, j|\n j[0] += i[0]\n }\n s.each_index{|i|\n a[i] << s\n }\n}\n\nn.times{|i|\n a[i].sort_by!{|_| -_[i][0]}\n}\nz = [0]\nn.times{|i|\n [[0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 1, 0]].each{|x, j, y|\n j += i\n b, c = a[i][x], a[j] && a[j][y]\n z = [z, [b[i][0] + c[j][0], i, x, j, y]].max if b && c && !b.equal?(c)\n }\n}\n\nu, i, x, j, y = z\np u\nv = a[j][y][0..j].zip(a[i][x][0..i]).inject(&:concat).compact\np v.size\nputs v.map{|_| _[1]} * ' '\n\n"}, {"source_code": "n = gets.to_i\nh = Hash.new{[]}\nn.times{|i|\n c, s = gets.split.map(&:to_i)\n h[c] <<= [s, i + 1]\n}\n\na = Array.new(n){[]}\nh.each{|c, s|\n s.sort!.reverse!\n s.each_cons(2){|i, j|\n j[0] += i[0]\n }\n s.each_index{|i|\n a[i] << s\n }\n}\n\nn.times{|i|\n a[i].sort_by!{|_| -_[i][0]}\n}\nz = [0]\nn.times{|i|\n [[0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 1, 0]].each{|x, j, y|\n j += i\n b, c = a[i][x], a[j] && a[j][y]\n z = [z, [b[i][0] + c[j][0], i, x, j, y]].max if b && c && !b.equal?(c)\n }\n}\n\nu, i, x, j, y = z\np u\nv = a[j][y][0..j].zip(a[i][x][0..i]).inject(&:concat).compact\np v.size\nputs v.map{|_| _[1]} * ' '\n\n"}, {"source_code": "n = gets.to_i\nh = Hash.new{[]}\nn.times{|i|\n c, s = gets.split.map(&:to_i)\n h[c] <<= [s, i + 1]\n}\n a = Array.new(n){[]}\nh.each{|c, s|\n s.sort!.reverse!\n s.each_cons(2){|i, j|\n j[0] += i[0]\n }\n s.each_index{|i|\n a[i] << s\n }\n}\n n.times{|i|\n a[i].sort_by!{|_| -_[i][0]}\n}\nz = [0]\nn.times{|i|\n [[0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 1, 0]].each{|x, j, y|\n j += i\n b, c = a[i][x], a[j] && a[j][y]\n z = [z, [b[i][0] + c[j][0], i, x, j, y]].max if b && c && !b.equal?(c)\n }\n}\n u, i, x, j, y = z\np u\nv = a[j][y][0..j].zip(a[i][x][0..i]).inject(&:concat).compact\np v.size\nputs v.map{|_| _[1]} * ' '"}, {"source_code": "n = gets.to_i\nh = Hash.new{[]}\nn.times{|i|\n c, s = gets.split.map(&:to_i)\n h[c] <<= [s, i + 1]\n}\n\na = Array.new(n){[]}\nh.each{|c, s|\n s.sort!.reverse!\n s.each_cons(2){|i, j|\n j[0] += i[0]\n }\n s.each_index{|i|\n a[i] << s\n }\n}\n\nn.times{|i|\n a[i].sort_by!{|_| -_[i][0]}\n}\nz = [0]\nn.times{|i|\n [[0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 1, 0]].each{|x, j, y|\n j += i\n b, c = a[i][x], a[j] && a[j][y]\n z = [z, [b[i][0] + c[j][0], i, x, j, y]].max if b && c && !b.equal?(c)\n }\n}\n\nu, i, x, j, y = z\np u\nv = a[j][y][0..j].zip(a[i][x][0..i]).inject(&:concat).compact\np v.size\nputs v.map{|_| _[1]} * ' '\n\n"}, {"source_code": "n = gets.to_i\nh = Hash.new{[]}\nn.times{|i|\n c, s = gets.split.map(&:to_i)\n h[c] <<= [s, i + 1]\n}\n\na = Array.new(n){[]}\nh.each{|c, s|\n s.sort!.reverse!\n s.each_cons(2){|i, j|\n j[0] += i[0]\n }\n s.each_index{|i|\n a[i] << s\n }\n}\n\nn.times{|i|\n a[i].sort_by!{|_| -_[i][0]}\n}\nz = [0]\nn.times{|i|\n [[0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 1, 0]].each{|x, j, y|\n j += i\n b, c = a[i][x], a[j] && a[j][y]\n z = [z, [b[i][0] + c[j][0], i, x, j, y]].max if b && c && !b.equal?(c)\n }\n}\n\nu, i, x, j, y = z\np u\nv = a[j][y][0..j].zip(a[i][x][0..i]).inject(&:concat).compact\np v.size\nputs v.map{|_| _[1]} * ' '\n\n"}, {"source_code": "n = gets.to_i\nh = Hash.new{[]}\nn.times{|i|\n c, s = gets.split.map(&:to_i)\n h[c] <<= [s, i + 1]\n}\n\na = Array.new(n){[]}\nh.each{|c, s|\n s.sort!.reverse!\n s.each_cons(2){|i, j|\n j[0] += i[0]\n }\n s.each_index{|i|\n a[i] << s\n }\n}\n\nn.times{|i|\n a[i].sort_by!{|_| -_[i][0]}\n}\nz = [0]\nn.times{|i|\n [[0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 1, 0]].each{|x, j, y|\n j += i\n b, c = a[i][x], a[j] && a[j][y]\n z = [z, [b[i][0] + c[j][0], i, x, j, y]].max if b && c && !b.equal?(c)\n }\n}\n\nu, i, x, j, y = z\np u\nv = a[j][y][0..j].zip(a[i][x][0..i]).inject(&:concat).compact\np v.size\nputs v.map{|_| _[1]} * ' '\n\n"}, {"source_code": "n = gets.to_i\nh = Hash.new{[]}\nn.times{|i|\n c, s = gets.split.map(&:to_i)\n h[c] <<= [s, i + 1]\n}\n\na = Array.new(n){[]}\nh.each{|c, s|\n s.sort!.reverse!\n s.each_cons(2){|i, j|\n j[0] += i[0]\n }\n s.each_index{|i|\n a[i] << s\n }\n}\n\nn.times{|i|\n a[i].sort_by!{|_| -_[i][0]}\n}\nz = [0]\nn.times{|i|\n [[0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 1, 0]].each{|x, j, y|\n j += i\n b, c = a[i][x], a[j] && a[j][y]\n z = [z, [b[i][0] + c[j][0], i, x, j, y]].max if b && c && !b.equal?(c)\n }\n}\n\nu, i, x, j, y = z\np u\nv = a[j][y][0..j].zip(a[i][x][0..i]).inject(&:concat).compact\np v.size\nputs v.map{|_| _[1]} * ' '\n\n"}], "negative_code": [{"source_code": "n = gets.to_i\nh = Hash.new{[]}\nn.times{|i|\n c, s = gets.split.map(&:to_i)\n h[c] <<= [s, i + 1]\n}\n\na = Array.new(n){[]}\nh.each{|c, s|\n s.sort!.reverse!\n s.each_cons(2){|i, j|\n j[0] += i[0]\n }\n s.each_index{|i|\n a[i] << s\n }\n}\n\nn.times{|i|\n a[i].sort_by!{|_| -_[i][0]}\n}\nz = [0]\nA = ->(i, j){a[i][j][i][0]}\nn.times{|i|\n next unless a[i][0]\n if a[i][1]\n z = [z, [A[i, 0] + A[i, 1], i, i]].max\n end\n j = i + 1\n if a[j][0]\n z = [z, [A[i, 0] + A[j, 0], i, j]].max\n end\n}\n\nu, v, w = z\np u\nc = a[w][v == w ? 1 : 0][0, w + 1].zip(a[v][0][0, v + 1]).inject(&:concat).compact\np c.size\nputs c.map{|_| _[1]} * ' '\n"}, {"source_code": "def gao(a, b, dump = nil)\n a, b = b, a if a.size < b.size\n if dump\n c = []\n a.size.times do |i|\n c << a[i][1]\n break if i == b.size\n c << b[i][1]\n end\n c\n else\n a[[a.size, b.size + 1].min - 1][0] + b[-1][0]\n end\nend\n\nn = gets.to_i\nh = Hash.new{[]}\nn.times do |i|\n c, s = gets.split.map(&:to_i)\n h[c] <<= [s, i + 1]\nend\nh.each do |c, s|\n s.sort!\n s.reverse!\n 1.upto(s.size - 1) do |i|\n s[i][0] += s[i - 1][0]\n end\nend\n\na = []\nh.sort{|x, y| [x[1].size, x[1][-1]] <=> [y[1].size, y[1][-1]]}.each {|c, s|\n a << s if a.size < 2 || a[-2].size < s.size\n}\n\nans = [-1]\na.each do |x|\n a.each do |y|\n break if x.equal?(y)\n tmp = gao(x, y)\n ans = [tmp, x, y] if tmp > ans[0]\n end\nend\np ans[0]\nans = gao(ans[1], ans[2], true)\np ans.size\nputs ans * ' '\n"}], "src_uid": "fa867bb7df94cc0edc03bcbf975de001"} {"nl": {"description": "Consider a conveyor belt represented using a grid consisting of $$$n$$$ rows and $$$m$$$ columns. The cell in the $$$i$$$-th row from the top and the $$$j$$$-th column from the left is labelled $$$(i,j)$$$. Every cell, except $$$(n,m)$$$, has a direction R (Right) or D (Down) assigned to it. If the cell $$$(i,j)$$$ is assigned direction R, any luggage kept on that will move to the cell $$$(i,j+1)$$$. Similarly, if the cell $$$(i,j)$$$ is assigned direction D, any luggage kept on that will move to the cell $$$(i+1,j)$$$. If at any moment, the luggage moves out of the grid, it is considered to be lost. There is a counter at the cell $$$(n,m)$$$ from where all luggage is picked. A conveyor belt is called functional if and only if any luggage reaches the counter regardless of which cell it is placed in initially. More formally, for every cell $$$(i,j)$$$, any luggage placed in this cell should eventually end up in the cell $$$(n,m)$$$. This may not hold initially; you are, however, allowed to change the directions of some cells to make the conveyor belt functional. Please determine the minimum amount of cells you have to change.Please note that it is always possible to make any conveyor belt functional by changing the directions of some set of cells.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 10$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n, m$$$ ($$$1 \\le n \\le 100$$$, $$$1 \\le m \\le 100$$$)  — the number of rows and columns, respectively. The following $$$n$$$ lines each contain $$$m$$$ characters. The $$$j$$$-th character in the $$$i$$$-th line, $$$a_{i,j}$$$ is the initial direction of the cell $$$(i, j)$$$. Please note that $$$a_{n,m}=$$$ C.", "output_spec": "For each case, output in a new line the minimum number of cells that you have to change to make the conveyor belt functional. ", "sample_inputs": ["4\n3 3\nRRD\nDDR\nRRC\n1 4\nDDDC\n6 9\nRDDDDDRRR\nRRDDRRDDD\nRRDRDRRDR\nDDDDRDDRR\nDRRDRDDDR\nDDRDRRDDC\n1 1\nC"], "sample_outputs": ["1\n3\n9\n0"], "notes": "NoteIn the first case, just changing the direction of $$$(2,3)$$$ to D is enough.You can verify that the resulting belt is functional. For example, if we place any luggage at $$$(2,2)$$$, it first moves to $$$(3,2)$$$ and then to $$$(3,3)$$$. In the second case, we have no option but to change the first $$$3$$$ cells from D to R making the grid equal to RRRC."}, "positive_code": [{"source_code": "t = gets.chomp.to_i\nt.times do |i|\n n, m = gets.chomp.split(\" \").map(&:to_i)\n count = 0\n if n == 1\n ar = gets.chomp.split(\"\")\n arr = ar\n yy = [\"C\"]\n xx = ar \n count = xx.count(\"D\") \n elsif n >= 2\n arr = []\n yy = []\n xx = []\n if m >= 2\n n.times do |i|\n ar = gets.chomp.split(\"\")\n arr << ar\n yy << ar[-1]\n if i == n - 1\n xx = ar\n end\n end\n elsif m == 1\n xx = [\"C\"]\n n.times do |i|\n ar = gets.chomp\n arr << ar\n yy << ar\n end \n end\n count += xx.count(\"D\")\n count += yy.count(\"R\")\n\n end\n puts count\nend"}, {"source_code": "# t = gets.chomp.to_i\n# t.times do \n# n, m = gets.chomp.split(\" \").map(&:to_i)\n# yy = []\n# xx = []\n# arr = []\n# count = 0\n# n.times do |i|\n# r = gets.chomp.split(\"\")\n# arr << r\n# if n == 1 \n# puts \"YY\"\n# yy << \"C\"\n# xx << r\n# break\n# elsif m == 1 && i != n-1\n# yy << r\n# elsif m == 1 && i == n-1\n# yy << \"C\"\n# break\n# end\n# if i != n-1 && n != 1 && m != 1\n# yy << r[-1]\n# elsif i == n-1 && n != 1 && m != 1\n# xx << r\n# yy << r[-1]\n# end\n# end\n# p xx\n# p yy\n# me_x = 0\n# me_y = 0\n# if xx[0].include?(\"R\")\n# ar = xx[0].count(\"D\")\n# count += ar\n# else\n# me_x += 1\n# end\n# if yy.include?(\"D\")\n# ar = yy.count(\"R\")\n# count += ar\n# else\n# me_y += 1 \n# end\n# if me_x >= 1 \n# count += m-1\n# elsif me_y >= 1\n# count += n-1\n# end\n# puts count\n # x = 0\n # y = 0\n # ans = 0\n # if n != 1 && m != 1\n # until arr[y][x] == \"C\"\n # if arr[y][x] == \"R\"\n # x += 1\n # elsif arr[y][x] == \"D\"\n # y += 1\n # end\n # if x == m - 1\n # ans = n - y - 1\n # (y..n-1).each do |k|\n # if yy[0][k] == \"D\"\n # ans -= 1\n # end\n # end\n # break\n # end\n # if y == n - 1\n # ans = m - x - 1\n # (x..m-1).each do |k|\n # if xx[0][k] == \"R\"\n # ans -= 1\n # end \n # end\n # break\n # end\n # end\n # elsif n == 1 || m == 1\n # if n == 1\n # if xx.include?(\"R\")\n # ans = m - 1 - xx.count(\"R\")\n # else\n # ans = m - 1\n # end\n # elsif m == 1\n # if yy.include?(\"D\")\n # ans = n - 1 - yy.count(\"D\")\n # else\n # ans = n -1 \n # end\n # end\n # end\nt = gets.chomp.to_i\nt.times do |i|\n n, m = gets.chomp.split(\" \").map(&:to_i)\n count = 0\n if n == 1\n ar = gets.chomp.split(\"\")\n arr = ar\n yy = [\"C\"]\n xx = ar \n count = xx.count(\"D\") \n elsif n >= 2\n arr = []\n yy = []\n xx = []\n if m >= 2\n n.times do |i|\n ar = gets.chomp.split(\"\")\n arr << ar\n yy << ar[-1]\n if i == n - 1\n xx = ar\n end\n end\n elsif m == 1\n xx = [\"C\"]\n n.times do |i|\n ar = gets.chomp\n arr << ar\n yy << ar\n end \n end\n count += xx.count(\"D\")\n count += yy.count(\"R\")\n\n end\n puts count\nend"}, {"source_code": "t = gets.to_i\nans = []\nt.times do\n h,w = gets.chomp.split(\" \").map(&:to_i)\n a = h.times.map{gets.chomp}\n cnt = 0\n (h-1).times do |i|\n cnt += 1 if a[i][-1] == \"R\"\n end\n (w-1).times do |i|\n cnt += 1 if a[-1][i] == \"D\"\n end\n ans << cnt\nend\nans.each do |x|\n puts x\nend"}], "negative_code": [], "src_uid": "409073ef839a7d0cdb900c06ee4a841c"} {"nl": {"description": "You are given array consisting of n integers. Your task is to find the maximum length of an increasing subarray of the given array.A subarray is the sequence of consecutive elements of the array. Subarray is called increasing if each element of this subarray strictly greater than previous.", "input_spec": "The first line contains single positive integer n (1 ≤ n ≤ 105) — the number of integers. The second line contains n positive integers a1, a2, ..., an (1 ≤ ai ≤ 109).", "output_spec": "Print the maximum length of an increasing subarray of the given array.", "sample_inputs": ["5\n1 7 2 11 15", "6\n100 100 100 100 100 100", "3\n1 2 3"], "sample_outputs": ["3", "1", "3"], "notes": null}, "positive_code": [{"source_code": "n = gets.chomp.to_i\na = gets.split.map(&:to_i)\nans=1\nz = 1\n(n-1).times do |i|\n if a[i] mas[i-1] then\n temp+=1\n else\n ans = temp if temp > ans\n temp = 1\n end\nend\n\nans = temp if temp > ans\n\nputs ans\n"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split.map(&:to_i)\n\nmax = 1\narr = [a.first]\n(1...n).each do |idx|\n if arr.last < a[idx]\n arr << a[idx]\n else\n arr = [a[idx]]\n end\n max = [max, arr.length].max\nend\nputs max\n"}, {"source_code": "n = gets.to_i\nas = gets.split.map(&:to_i)\n\nans = 1\ncnt = 1\nnow = as[0]\n\n(1..n-1).each do |i|\n if now < as[i]\n cnt += 1\n else\n cnt = 1\n end\n now = as[i]\n ans = [ans, cnt].max\nend\n\nputs ans\n"}, {"source_code": "n = gets.strip.to_i\na = gets.strip.split.map(&:to_i)\nprev = a[0]\ncount = ans = i = 1\nwhile i < n\n if prev < a[i]\n count += 1\n else\n count = 1\n end\n ans = [ans, count].max\n prev = a[i]\n i += 1\nend\nputs ans\n"}, {"source_code": "n=gets.to_i\nx=gets.split.map{|e| e.to_i}\nc=0\nans=1\nx.inject(0){|l,e|\n\tif la[i-1]\nend\nputs dp.max\n"}, {"source_code": "gets\ndp=[1]\na=gets.split.map(&:to_i)\n(1..a.size-1).each {|i| dp << ((a[i]>a[i-1])? dp.last+1: 1)}\nputs dp.max"}, {"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\n\nn = gets.to_i\nl = 0\nconsecutive = 0\nlongest_consecutive = 0\ngets.split.each { |i|\n i = i.to_i\n if l < i\n consecutive += 1\n else\n consecutive = 1 # new first one\n end\n\n if longest_consecutive < consecutive\n longest_consecutive = consecutive\n end\n \n l = i\n}\n\nputs longest_consecutive"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split.map(&:to_i)\n\ncount = 1\narray = []\narray << count\nfor i in (1..n-1)\n\tif a[i] > a[i-1]\n\t\tcount += 1\n\t\tarray << count\n\telse\n\t\tcount = 1\n\tend\nend\n\nprint array.max"}, {"source_code": "def solution(arr)\n counter = 1\n max = 0\n for i in 1...arr.size do\n if arr[i] > arr[i-1]\n counter += 1\n else\n max = [max, counter].max\n counter = 1\n end\n end\n max = [max, counter].max\nend\n\n_ = gets\nputs solution(gets.split.map(&:to_i))\n"}, {"source_code": "n = gets.chomp.to_i\n#a = Array.new\n#n.times { puts gets }\nlongest = 0\ncurrent = 0\nbefore = -1\na = STDIN.gets.split(\" \").to_a\na.each do |b|\n b = b.to_i\n if b > before\n current += 1\n longest = current if current > longest\n else\n current = 1\n end\n before = b\nend\nputs longest\n"}, {"source_code": "def read_line\n gets.chomp.split.map &:to_i\nend\n\nn = read_line\na = read_line\nk = 0\nx = 0\nm = 0\na.each do |y|\n if xm\n x = y\nend\n\nputs m\n"}, {"source_code": "gets\na = gets.split.map{|x| x.to_i}\nt, l = 1, 1\n(1..a.size-1).each do |i|\n l = 0 if a[i] <= a[i - 1]\n l += 1\n t = [t, l].max\nend\nputs t\n"}, {"source_code": "gets\na = gets.split(' ').map(&:to_i)\n\nresult = a.each_with_object({ prev: a.first, max: 1, s: 1 }) do |elm, obj|\n if elm > obj[:prev] \n obj.merge!({ max: [obj[:s]+1, obj[:max]].max, s: obj[:s]+1 })\n else\n obj.merge!({ max: obj[:max], s: 1 })\n end\n obj[:prev] = elm\nend\n\nputs result[:max]\n"}], "negative_code": [{"source_code": "n = gets.chomp.to_i\na = gets.chomp.split.map(&:to_i)\n\nmax = 0\narr = [a.first]\n(1...n).each do |idx|\n if arr.last < a[idx]\n arr << a[idx]\n else\n arr = [a[idx]]\n end\n max = [max, arr.length].max\nend\nputs max\n"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split.map(&:to_i)\n\ncount = 1\narray = []\nfor i in (1..n-1)\n\tif a[i] > a[i-1]\n\t\tcount += 1\n\t\tarray << count\n\telse\n\t\tcount = 1\n\tend\nend\n\nprint array.max"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split.map(&:to_i)\n\ncount = 1\narray = []\nfor i in (1..n-1)\n\tif a[i] > a[i-1]\n\t\tcount += 1\n\t\tarray << count\n\telse\n\t\tcount = 1\n\t\tarray << count\n\tend\nend\n\nprint array.max"}], "src_uid": "4553b327d7b9f090641590d6492c2c41"} {"nl": {"description": "You are given an array of $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$. After you watched the amazing film \"Everything Everywhere All At Once\", you came up with the following operation.In one operation, you choose $$$n-1$$$ elements of the array and replace each of them with their arithmetic mean (which doesn't have to be an integer). For example, from the array $$$[1, 2, 3, 1]$$$ we can get the array $$$[2, 2, 2, 1]$$$, if we choose the first three elements, or we can get the array $$$[\\frac{4}{3}, \\frac{4}{3}, 3, \\frac{4}{3}]$$$, if we choose all elements except the third.Is it possible to make all elements of the array equal by performing a finite number of such operations?", "input_spec": "The first line of the input contains a single integer $$$t$$$ ($$$1 \\le t \\le 200$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$3 \\le n \\le 50$$$)  — the number of integers. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$0 \\le a_i \\le 100$$$).", "output_spec": "For each test case, if it is possible to make all elements equal after some number of operations, output $$$\\texttt{YES}$$$. Otherwise, output $$$\\texttt{NO}$$$. You can output $$$\\texttt{YES}$$$ and $$$\\texttt{NO}$$$ in any case (for example, strings $$$\\texttt{yEs}$$$, $$$\\texttt{yes}$$$, $$$\\texttt{Yes}$$$ will be recognized as a positive response).", "sample_inputs": ["4\n\n3\n\n42 42 42\n\n5\n\n1 2 3 4 5\n\n4\n\n4 3 2 1\n\n3\n\n24 2 22"], "sample_outputs": ["YES\nYES\nNO\nNO"], "notes": "NoteIn the first test case, all elements are already equal.In the second test case, you can choose all elements except the third, their average is $$$\\frac{1 + 2 + 4 + 5}{4} = 3$$$, so the array will become $$$[3, 3, 3, 3, 3]$$$.It's possible to show that it's impossible to make all elements equal in the third and fourth test cases."}, "positive_code": [{"source_code": "gets.to_i.times do\r\nn=gets.to_i\r\na=gets.split(' ').map(&:to_i)\r\nsum=a.reduce(:+)\r\nanswer = ! a.select{|v| v % (n - 1) == sum % (n-1) && v == (sum - v) / (n - 1)}.empty?\r\nputs answer ? 'YES' : 'NO'\r\nend\r\n"}, {"source_code": "t = gets.to_i\nt.times do\n n = gets.to_i\n a = gets.chomp!.split.map! &:to_i\n s = a.sum\n if s % a.size == 0 && a.include?(s / a.size)\n puts 'YES'\n else\n puts 'NO'\n end\nend\n"}, {"source_code": "gets.to_i.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n s = a.reduce(:+)\n ans = \"NO\"\n 0.upto(n-1) do |i|\n if (s == n*a[i]) then\n ans = \"YES\"\n end\n end\n puts ans\nend\n"}], "negative_code": [{"source_code": "gets.to_i.times do\r\nn=gets.to_i\r\na=gets.split(' ').map(&:to_i)\r\nsum=a.reduce(:+)\r\nanswer = ! a.select{|v| v % n == sum % n && v == (sum - v) / (n - 1)}.empty?\r\nputs answer ? 'YES' : 'NO'\r\nend\r\n"}, {"source_code": "gets.to_i.times do\r\nn=gets.to_i\r\na=gets.split(' ').map(&:to_i)\r\nsum=a.reduce(:+)\r\nanswer = ! a.select{|v| v % n == sum % n && v == (sum - v) / n}.empty?\r\nputs answer ? 'YES' : 'NO'\r\nend"}], "src_uid": "7785ed6f41dbd45f1a9432c2fb07d713"} {"nl": {"description": "Let's dive into one of the most interesting areas of magic — writing spells. Learning this exciting but challenging science is very troublesome, so now you will not learn the magic words, but only get to know the basic rules of writing spells.Each spell consists of several lines. The line, whose first non-space character is character \"#\" is an amplifying line and it is responsible for spell power. The remaining lines are common, and determine the effect of the spell.You came across the text of some spell. Spell was too long, so you cannot understand its meaning. So you want to make it as short as possible without changing the meaning.The only way to shorten a spell that you know is the removal of some spaces and line breaks. We know that when it comes to texts of spells, the spaces carry meaning only in the amplifying lines, so we should remove all spaces in other lines. Newlines also do not matter, unless any of the two separated lines is amplifying. Thus, if two consecutive lines are not amplifying, they need to be joined into one (i.e. we should concatenate the second line to the first one). Removing spaces in amplifying lines and concatenating the amplifying lines to anything is forbidden.Note that empty lines must be processed just like all the others: they must be joined to the adjacent non-amplifying lines, or preserved in the output, if they are surrounded with amplifying lines on both sides (i.e. the line above it, if there is one, is amplifying, and the line below it, if there is one, is amplifying too).For now those are the only instructions for removing unnecessary characters that you have to follow (oh yes, a newline is a character, too).The input contains the text of the spell, which should be reduced. Remove the extra characters and print the result to the output.", "input_spec": "The input contains multiple lines. All characters in the lines have codes from 32 to 127 (inclusive). Please note that the lines may begin with or end with one or more spaces. The size of the input does not exceed 1048576 ( = 220) bytes. Newlines are included in this size. In the Windows operating system used on the testing computer, a newline is a sequence of characters with codes #13#10. It is guaranteed that after each line of input there is a newline. In particular, the input ends with a newline. Note that the newline is the end of the line, and not the beginning of the next one. It is guaranteed that the input contains at least one character other than a newline. It is recommended to organize the input-output line by line, in this case the newlines will be processed correctly by the language means.", "output_spec": "Print the text of the spell where all extra characters are deleted. Please note that each output line should be followed by a newline. Please be careful: your answers will be validated by comparing them to the jury's answer byte-by-byte. So, all spaces and newlines matter.", "sample_inputs": ["# include <cstdio>\n\nusing namespace std;\n\nint main ( ){\nputs(\"Hello # World\"); #\n#\n}", "#\n\n#"], "sample_outputs": ["# include <cstdio>\nusingnamespacestd;intmain(){puts(\"Hello#World\");#\n#\n}", "#\n\n#"], "notes": "NoteIn the first sample the amplifying lines are lines 1 and 7. So, lines 2 to 6 are concatenated to each other, all spaces are deleted from them.In the second sample the amplifying lines are lines 1 and 3. So, no lines are concatenated to each other. "}, "positive_code": [{"source_code": "puts gets(p).gsub(/(^(?! *#).*\\n)+/){$&.gsub(/\\s/, '') + \"\\n\"}\n"}, {"source_code": "puts gets(p).gsub(/(^(?! *#).*\\n)+/){$&.gsub(/\\s/, '') + \"\\n\"}\n\n"}, {"source_code": "puts gets(p).gsub(/(^(?! *#).*\\n)+/){$&.gsub(/\\s/, '') + \"\\n\"}\n\n"}, {"source_code": "lines = STDIN.readlines\nresult = []\n\nlines.each_with_index do |line, i|\n if line.strip[0] == '#'\n if result.length != 0 and result[-1][-1] != \"\\n\"\n result.push(\"\\n\")\n end\n result.push(line)\n else\n result.push(line.gsub(/\\s/, ''))\n end\nend\n\nif result[-1][-1] != \"\\n\"\n result.push(\"\\n\")\nend\nputs result.join\n"}, {"source_code": "lines = STDIN.readlines\nresult = []\nis_amplifying = []\n\nlines.each do |line|\n if line.strip[0] == '#'\n result.push(line)\n is_amplifying.push(true)\n else\n result.push(line.gsub(/\\s/, ''))\n is_amplifying.push(false)\n end\nend\n\n(1..lines.length-1).each do |i|\n if is_amplifying[i] and !is_amplifying[i-1]\n result[i-1] += \"\\n\"\n end\nend\n\nif result[-1][-1] != \"\\n\"\n result.push(\"\\n\")\nend\nputs(result.join)\n"}, {"source_code": "lines = STDIN.readlines\nresult = []\nis_amplifying = []\n\nlines.each do |line|\n if line.strip[0] == '#'\n result.push(line)\n is_amplifying.push(true)\n else\n result.push(line.gsub(/\\s/, ''))\n is_amplifying.push(false)\n end\nend\n\n(1..lines.length-1).each do |i|\n if is_amplifying[i] and !is_amplifying[i-1]\n result[i-1] += \"\\n\"\n end\nend\n\nif result[-1][-1] != \"\\n\"\n result.push(\"\\n\")\nend\nprint(result.join)\n"}, {"source_code": "puts gets(p).gsub(/(^(?! *#).*\\n)+/){$&.gsub(/\\s/, '') + \"\\n\"}\n"}, {"source_code": "puts gets(p).gsub(/(^(?! *#).*\\n)+/){$&.gsub(/\\s/, '') + \"\\n\"}\n\n"}, {"source_code": "puts gets(p).gsub(/(^(?! *#).*\\n)+/){$&.gsub(/\\s/, '') + \"\\n\"}\n\n"}, {"source_code": "puts gets(p).gsub(/(^(?! *#).*\\n)+/){$&.gsub(/\\s/, '') + \"\\n\"}"}, {"source_code": "#!/usr/bin/ruby\n\nbuf = []\n$stdin.readlines.each do |line|\n line.chomp!\n if line =~ /^ *#/\n if not buf.empty?\n puts buf.join('')\n buf = []\n end\n puts line\n else\n buf << line.gsub(/ /, '')\n end\nend\nif not buf.empty?\n puts buf.join('')\nend\n"}, {"source_code": "puts gets(p).gsub(/(^(?! *#).*\\n)+/){$&.gsub(/\\s/, '') + \"\\n\"}\n\n"}, {"source_code": "tmp = []\n\nreadlines.each do |line|\n if line =~ /^ *#/\n if not tmp.empty?\n puts tmp.join\n tmp = []\n end\n puts line\n else\n tmp << line.delete(' ').chomp\n end\nend\n\nputs tmp.join if not tmp.empty?"}, {"source_code": "tmp = []\n\nwhile line = gets\n if line.lstrip[0] == '#'\n if not tmp.empty?\n puts tmp.join\n tmp = []\n end\n puts line\n else\n tmp << line.delete(' ').chomp\n end\nend\n\nputs tmp.join if not tmp.empty?"}, {"source_code": "tmp = []\n\nreadlines.each do |line|\n if line.lstrip[0] == '#'\n if not tmp.empty?\n puts tmp.join\n tmp = []\n end\n puts line\n else\n tmp << line.delete(' ').chomp\n end\nend\n\nputs tmp.join if not tmp.empty?"}, {"source_code": "puts gets(p).gsub(/(^(?! *#).*\\n)+/){$&.gsub(/\\s/, '') + \"\\n\"}\n\n"}, {"source_code": "puts gets(p).gsub(/(^(?! *#).*\\n)+/){$&.gsub(/\\s/, '') + \"\\n\"}\n\n"}, {"source_code": "last = nil\nf = true\n\nwhile s = gets\n\tt = s.strip.gsub(' ','')\n\tif t[0] == '#'\n\t\tif last != nil\n\t\t\tputs last\n\t\t\tlast = nil\n\t\tend\n\t\tputs s\n\telse\n\t\tlast ||= \"\"\n\t\tlast << t\n\tend\n\tf = false\nend\nif last != nil\n\tputs last\nend"}, {"source_code": "puts gets(p).gsub(/(^(?! *#).*\\n)+/){$&.gsub(/\\s/, '') + \"\\n\"}\n\n"}], "negative_code": [{"source_code": "lines = STDIN.readlines\nresult = []\nis_amplifying = []\n\nlines.each do |line|\n if line.strip[0] == '#'\n result.push(line)\n is_amplifying.push(true)\n else\n result.push(line.gsub(/\\s/, ''))\n is_amplifying.push(false)\n end\nend\n\n(1..lines.length-1).each do |i|\n if is_amplifying[i] and !is_amplifying[i-1]\n result[i-1] += \"\\n\"\n end\nend\n\nprint(result.join)\n"}, {"source_code": "lines = STDIN.readlines\nresult = []\n\nlines.each_with_index do |line, i|\n if line.strip[0] == '#'\n if result.length != 0 and result[-1][-1] != \"\\n\"\n result.push(\"\\n\")\n end\n result.push(line)\n else\n result.push(line.gsub(/\\s/, ''))\n end\nend\n\nputs result.join\n"}, {"source_code": "lines = STDIN.readlines\nresult = []\n\nlines.each_with_index do |line, i|\n if line.strip[0] == '#'\n if result.length > 0\n if result[-1].length == 0 or result[-1][-1] != \"\\n\"\n result.push(\"\\n\")\n end\n end\n result.push(line)\n else\n result.push(line.gsub(/\\s/, ''))\n end\nend\n\nputs result.join\n"}, {"source_code": "lines = STDIN.readlines\nresult = []\nis_amplifying = []\n\nlines.each do |line|\n if line.strip[0] == '#'\n result.push(line)\n is_amplifying.push(true)\n else\n result.push(line.gsub(/\\s/, ''))\n is_amplifying.push(false)\n end\nend\n\n(1..lines.length-1).each do |i|\n if is_amplifying[i] and !is_amplifying[i-1]\n result[i-1] += \"\\n\"\n end\nend\n\nputs result.join\n"}, {"source_code": "tmp = ''\nlast_line_was_text = false\n\nwhile line = gets\n if line.lstrip[0] == '#'\n tmp += \"\\n\" if last_line_was_text\n tmp += line\n else\n tmp += line.delete(' ').chomp\n last_line_was_text = true\n end\nend\n\nputs tmp.chomp"}, {"source_code": "$/ = nil\ntmp = ''\nlast_line_was_text = false\n\nwhile line = gets\n if line.lstrip[0] == '#'\n tmp += \"\\n\" if last_line_was_text\n tmp += line\n last_line_was_text = false\n else\n tmp += line.delete(' ').chomp\n last_line_was_text = true\n end\nend\n\nprint tmp.chomp"}, {"source_code": "tmp = ''\nlast_line_was_text = false\n\nwhile line = gets\n if line.lstrip[0] == '#'\n tmp += \"\\n\" if last_line_was_text\n tmp += line\n last_line_was_text = false\n else\n tmp += line.delete(' ').chomp\n last_line_was_text = true\n end\nend\n\nputs tmp.chomp"}, {"source_code": "\n#$/ = nil\ntmp = ''\nlast_line_was_text = false\n\nwhile line = gets\n if line.lstrip[0] == '#'\n tmp += \"\\n\" if last_line_was_text\n tmp += line\n last_line_was_text = false\n else\n tmp += line.delete(' ').chomp\n last_line_was_text = true\n end\nend\n\nprint tmp.chomp"}, {"source_code": "last = \"\"\nf = true\n\nwhile s = gets\n\tt = s.strip.gsub(' ','')\n\tif t[0] == '#'\n\t\tif not f\n\t\t\tputs last\n\t\t\tlast = \"\"\n\t\tend\n\t\tputs s\n\telse\n\t\tlast << t\n\tend\n\tf = false\nend\nputs last"}, {"source_code": "imp = false\nf = true\nwhile s = gets\n\tt = s.strip\n\tif t[0] == '#'\n\t\tif not imp and not f\n\t\t\tputs\n\t\tend\n\t\tputs s\n\t\timp = true\n\telse\n\t\tt = t.gsub(' ','')\n\t\tprint t\n\t\timp = false\n\tend\n\tf = false\nend"}, {"source_code": "imp = false\nf = true\nwhile s = gets\n\tt = s.strip\n\tif t[0] == '#'\n\t\tif not imp and not f\n\t\t\tputs\n\t\tend\n\t\tprint s\n\t\timp = true\n\telse\n\t\tt = t.gsub(' ','')\n\t\tprint t\n\t\timp = false\n\tend\n\tf = false\nend"}, {"source_code": "imp = false\nf = true\nwhile s = gets\n\tt = s.strip\n\tif t[0] == '#'\n\t\tif not imp and not f\n\t\t\tputs\n\t\tend\n\t\tputs s\n\t\timp = true\n\telse\n\t\tt = t.gsub(' ','')\n\t\tprint t\n\t\timp = false\n\tend\n\tf = false\nend\nputs"}], "src_uid": "a16bb696858bc592c33dcf0fd99df197"} {"nl": {"description": "Карта звёздного неба представляет собой прямоугольное поле, состоящее из n строк по m символов в каждой строке. Каждый символ — это либо «.» (означает пустой участок неба), либо «*» (означает то, что в этом месте на небе есть звезда). Новое издание карты звёздного неба будет напечатано на квадратных листах, поэтому требуется найти минимально возможную сторону квадрата, в который могут поместиться все звезды. Границы искомого квадрата должны быть параллельны сторонам заданного прямоугольного поля.", "input_spec": "В первой строке входных данных записаны два числа n и m (1 ≤ n, m ≤ 1000) — количество строк и столбцов на карте звездного неба. В следующих n строках задано по m символов. Каждый символ — это либо «.» (пустой участок неба), либо «*» (звезда). Гарантируется, что на небе есть хотя бы одна звезда.", "output_spec": "Выведите одно число — минимально возможную сторону квадрата, которым можно накрыть все звезды.", "sample_inputs": ["4 4\n....\n..*.\n...*\n..**", "1 3\n*.*", "2 1\n.\n*"], "sample_outputs": ["3", "3", "1"], "notes": "ПримечаниеОдин из возможных ответов на первый тестовый пример: Один из возможных ответов на второй тестовый пример (обратите внимание, что покрывающий квадрат выходит за пределы карты звездного неба): Ответ на третий тестовый пример: "}, "positive_code": [{"source_code": "#!/usr/bin/env ruby\n\nn, m = gets.split.map(&:to_i)\narr = [[]]\nfor i in 0..n-1\n arr[i] = gets.chomp.chars\nend\ncoord = Struct.new(:x, :y)\ntl = coord.new(nil, nil)\ntr = coord.new(nil, nil)\nbl = coord.new(nil, nil)\nbr = coord.new(nil, nil)\nfor y in 0..n-1\n if arr[y].include? '*' then\n # Left\n if tl.y == nil then tl.y = y; tl.x = arr[y].index('*')\n elsif tl.x > arr[y].index('*') then tl.x = arr[y].index('*') end\n\n if bl.x == nil or bl.x > arr[y].index('*') then bl.x = arr[y].index('*') end\n bl.y = y\n\n # Right\n if tr.y == nil then tr.y = y; tr.x = arr[y].rindex('*');\n elsif tr.x < arr[y].rindex('*') then tr.x = arr[y].rindex('*') end\n\n if br.x == nil or br.x < arr[y].rindex('*') then br.x = arr[y].rindex('*') end\n br.y = y\n end\nend\n\nputs [bl.y - tl.y, br.x - bl.x].max + 1\n#printf \"TopLeft: %d %d\\n\", tl.x + 1, tl.y + 1\n#printf \"TopRight: %d %d\\n\", tr.x + 1, tr.y + 1\n#printf \"BottomLeft: %d %d\\n\", bl.x + 1, bl.y + 1\n#printf \"BottomRight: %d %d\\n\", br.x + 1, br.y + 1\n"}], "negative_code": [], "src_uid": "3144d497bd8d7ca469e1b7f4c12dfd6b"} {"nl": {"description": "Guy-Manuel and Thomas are going to build a polygon spaceship. You're given a strictly convex (i. e. no three points are collinear) polygon $$$P$$$ which is defined by coordinates of its vertices. Define $$$P(x,y)$$$ as a polygon obtained by translating $$$P$$$ by vector $$$\\overrightarrow {(x,y)}$$$. The picture below depicts an example of the translation:Define $$$T$$$ as a set of points which is the union of all $$$P(x,y)$$$ such that the origin $$$(0,0)$$$ lies in $$$P(x,y)$$$ (both strictly inside and on the boundary). There is also an equivalent definition: a point $$$(x,y)$$$ lies in $$$T$$$ only if there are two points $$$A,B$$$ in $$$P$$$ such that $$$\\overrightarrow {AB} = \\overrightarrow {(x,y)}$$$. One can prove $$$T$$$ is a polygon too. For example, if $$$P$$$ is a regular triangle then $$$T$$$ is a regular hexagon. At the picture below $$$P$$$ is drawn in black and some $$$P(x,y)$$$ which contain the origin are drawn in colored: The spaceship has the best aerodynamic performance if $$$P$$$ and $$$T$$$ are similar. Your task is to check whether the polygons $$$P$$$ and $$$T$$$ are similar.", "input_spec": "The first line of input will contain a single integer $$$n$$$ ($$$3 \\le n \\le 10^5$$$) — the number of points. The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i, y_i$$$ ($$$|x_i|, |y_i| \\le 10^9$$$), denoting the coordinates of the $$$i$$$-th vertex. It is guaranteed that these points are listed in counterclockwise order and these points form a strictly convex polygon.", "output_spec": "Output \"YES\" in a separate line, if $$$P$$$ and $$$T$$$ are similar. Otherwise, output \"NO\" in a separate line. You can print each letter in any case (upper or lower).", "sample_inputs": ["4\n1 0\n4 1\n3 4\n0 3", "3\n100 86\n50 0\n150 0", "8\n0 0\n1 0\n2 1\n3 3\n4 6\n3 6\n2 5\n1 3"], "sample_outputs": ["YES", "nO", "YES"], "notes": "NoteThe following image shows the first sample: both $$$P$$$ and $$$T$$$ are squares. The second sample was shown in the statements."}, "positive_code": [{"source_code": "n = gets.chomp.to_i\narr = (1..n).map {|_| gets.chomp.split(' ').map(&:to_i)}\ndiff = arr.each_with_index.map do |_, i|\n val = [\n arr[i][0]-arr[i-1][0],\n arr[i][1]-arr[i-1][1]\n ]\n if val[0] < 0 || (val[0] == 0 && val[1] < 0)\n val[0] *= -1\n val[1] *= -1\n end\n val\nend\nputs (n%2 == 0 && diff.uniq.length == n/2 ? \"YES\" : \"NO\")"}], "negative_code": [], "src_uid": "424f37abd0e19208e6b0cb8b670e7187"} {"nl": {"description": "The Berland road network consists of n cities and of m bidirectional roads. The cities are numbered from 1 to n, where the main capital city has number n, and the culture capital — number 1. The road network is set up so that it is possible to reach any city from any other one by the roads. Moving on each road in any direction takes the same time.All residents of Berland are very lazy people, and so when they want to get from city v to city u, they always choose one of the shortest paths (no matter which one).The Berland government wants to make this country's road network safer. For that, it is going to put a police station in one city. The police station has a rather strange property: when a citizen of Berland is driving along the road with a police station at one end of it, the citizen drives more carefully, so all such roads are considered safe. The roads, both ends of which differ from the city with the police station, are dangerous.Now the government wonders where to put the police station so that the average number of safe roads for all the shortest paths from the cultural capital to the main capital would take the maximum value.", "input_spec": "The first input line contains two integers n and m (2 ≤ n ≤ 100, ) — the number of cities and the number of roads in Berland, correspondingly. Next m lines contain pairs of integers vi, ui (1 ≤ vi, ui ≤ n, vi ≠ ui) — the numbers of cities that are connected by the i-th road. The numbers on a line are separated by a space. It is guaranteed that each pair of cities is connected with no more than one road and that it is possible to get from any city to any other one along Berland roads.", "output_spec": "Print the maximum possible value of the average number of safe roads among all shortest paths from the culture capital to the main one. The answer will be considered valid if its absolute or relative inaccuracy does not exceed 10 - 6.", "sample_inputs": ["4 4\n1 2\n2 4\n1 3\n3 4", "11 14\n1 2\n1 3\n2 4\n3 4\n4 5\n4 6\n5 11\n6 11\n1 8\n8 9\n9 7\n11 7\n1 10\n10 4"], "sample_outputs": ["1.000000000000", "1.714285714286"], "notes": "NoteIn the first sample you can put a police station in one of the capitals, then each path will have exactly one safe road. If we place the station not in the capital, then the average number of safe roads will also make .In the second sample we can obtain the maximum sought value if we put the station in city 4, then 6 paths will have 2 safe roads each, and one path will have 0 safe roads, so the answer will equal ."}, "positive_code": [{"source_code": "#!/usr/bin/ruby\n\nrequire 'pp'\n\ninf = 1001001001\n\nwhile line = gets\n\tn, m = line.split.map(&:to_i)\n\td = Array.new(n)\n\tn.times do |u|\n\t\td[u] = Array.new(n, inf)\n\t\td[u][u] = 0\n\tend\n\tm.times do\n\t\tu, v = gets.split.map{|x| x.to_i - 1}\n\t\td[u][v] = d[v][u] = 1\n\tend\n\tn.times do |w|\n\t\tn.times do |u|\n\t\t\tn.times do |v|\n\t\t\t\tif d[u][v] > d[u][w] + d[w][v]\n\t\t\t\t\td[u][v] = d[u][w] + d[w][v]\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\tend\n\te = Array.new(n)\n\tn.times do |u|\n\t\te[u] = Array.new(n, 0.0)\n\tend\n\tn.times do |u|\n\t\tn.times do |v|\n\t\t\tif d[u][v] <= 1\n\t\t\t\te[u][v] = 1.0\n\t\t\tend\n\t\tend\n\tend\n\t2.upto(n) do |dist|\n\t\tn.times do |u|\n\t\t\tn.times do |v|\n\t\t\t\tif d[u][v] == dist\n\t\t\t\t\tn.times do |w|\n\t\t\t\t\t\tif d[u][w] == 1 && d[u][v] == 1 + d[w][v]\n\t\t\t\t\t\t\te[u][v] += e[u][w] * e[w][v]\n\t\t\t\t\t\tend\n\t\t\t\t\tend\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\tend\n\t# pp e\n\tans = 1.0\n\tn.times do |w|\n\t\tif w != 0 && w != n - 1 && d[0][n - 1] == d[0][w] + d[w][n - 1]\n\t\t\ttmp = 2.0 * e[0][w] * e[w][n - 1] / e[0][n - 1]\n\t\t\t# p w\n\t\t\t# p tmp\n\t\t\tif ans < tmp\n\t\t\t\tans = tmp\n\t\t\tend\n\t\tend\n\tend\n\tp ans\nend\n"}], "negative_code": [], "src_uid": "a69b342ef5f6e11f7aa4fee71e149aa2"} {"nl": {"description": "The city where Mocha lives in is called Zhijiang. There are $$$n+1$$$ villages and $$$2n-1$$$ directed roads in this city. There are two kinds of roads: $$$n-1$$$ roads are from village $$$i$$$ to village $$$i+1$$$, for all $$$1\\leq i \\leq n-1$$$. $$$n$$$ roads can be described by a sequence $$$a_1,\\ldots,a_n$$$. If $$$a_i=0$$$, the $$$i$$$-th of these roads goes from village $$$i$$$ to village $$$n+1$$$, otherwise it goes from village $$$n+1$$$ to village $$$i$$$, for all $$$1\\leq i\\leq n$$$. Mocha plans to go hiking with Taki this weekend. To avoid the trip being boring, they plan to go through every village exactly once. They can start and finish at any villages. Can you help them to draw up a plan? ", "input_spec": "Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 20$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^4$$$) — indicates that the number of villages is $$$n+1$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$0 \\le a_i \\le 1$$$). If $$$a_i=0$$$, it means that there is a road from village $$$i$$$ to village $$$n+1$$$. If $$$a_i=1$$$, it means that there is a road from village $$$n+1$$$ to village $$$i$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.", "output_spec": "For each test case, print a line with $$$n+1$$$ integers, where the $$$i$$$-th number is the $$$i$$$-th village they will go through. If the answer doesn't exist, print $$$-1$$$. If there are multiple correct answers, you can print any one of them.", "sample_inputs": ["2\n3\n0 1 0\n3\n1 1 0"], "sample_outputs": ["1 4 2 3 \n4 1 2 3"], "notes": "NoteIn the first test case, the city looks like the following graph:So all possible answers are $$$(1 \\to 4 \\to 2 \\to 3)$$$, $$$(1 \\to 2 \\to 3 \\to 4)$$$.In the second test case, the city looks like the following graph:So all possible answers are $$$(4 \\to 1 \\to 2 \\to 3)$$$, $$$(1 \\to 2 \\to 3 \\to 4)$$$, $$$(3 \\to 4 \\to 1 \\to 2)$$$, $$$(2 \\to 3 \\to 4 \\to 1)$$$."}, "positive_code": [{"source_code": "# END { load __FILE__ unless $stdin.eof? }\n\nt = gets.to_i\nt.times do\n\nn = gets.to_i\na = gets.split.map!(&:to_i)\nz = Array.new(n, &:itself)\n\nif a[-1] == 0\n z.push(n)\nelsif a[0] != 0\n z.unshift(n)\nelse\n k = a.each_cons(2).find_index { |ai, aj| ai == 0 && aj != 0 }\n raise unless k\n z.insert(k + 1, n)\nend\n\nputs z.map!(&:succ) * \" \"\n\nrescue\n\nputs -1\n\nend\n"}, {"source_code": "gets.to_i.times do\r\n n=gets.to_i\r\n a=gets.split.map(&:to_i)\r\n i=a.reverse.index(0)\r\n if i==nil\r\n puts ([n+1]+[*1..n]).join(\" \")\r\n elsif i==0\r\n i=n-1-i\r\n puts ([*1..n]+[n+1]).join(\" \")\r\n else\r\n i=n-1-i\r\n puts ([*1..i+1]+[n+1]+[*i+2..n]).join(\" \")\r\n end\r\nend\r\n"}], "negative_code": [], "src_uid": "3f9525d74f4934eb9dca1b16c53662bf"} {"nl": {"description": "After the fourth season Sherlock and Moriary have realized the whole foolishness of the battle between them and decided to continue their competitions in peaceful game of Credit Cards.Rules of this game are simple: each player bring his favourite n-digit credit card. Then both players name the digits written on their cards one by one. If two digits are not equal, then the player, whose digit is smaller gets a flick (knock in the forehead usually made with a forefinger) from the other player. For example, if n = 3, Sherlock's card is 123 and Moriarty's card has number 321, first Sherlock names 1 and Moriarty names 3 so Sherlock gets a flick. Then they both digit 2 so no one gets a flick. Finally, Sherlock names 3, while Moriarty names 1 and gets a flick.Of course, Sherlock will play honestly naming digits one by one in the order they are given, while Moriary, as a true villain, plans to cheat. He is going to name his digits in some other order (however, he is not going to change the overall number of occurences of each digit). For example, in case above Moriarty could name 1, 2, 3 and get no flicks at all, or he can name 2, 3 and 1 to give Sherlock two flicks.Your goal is to find out the minimum possible number of flicks Moriarty will get (no one likes flicks) and the maximum possible number of flicks Sherlock can get from Moriarty. Note, that these two goals are different and the optimal result may be obtained by using different strategies.", "input_spec": "The first line of the input contains a single integer n (1 ≤ n ≤ 1000) — the number of digits in the cards Sherlock and Moriarty are going to use. The second line contains n digits — Sherlock's credit card number. The third line contains n digits — Moriarty's credit card number.", "output_spec": "First print the minimum possible number of flicks Moriarty will get. Then print the maximum possible number of flicks that Sherlock can get from Moriarty.", "sample_inputs": ["3\n123\n321", "2\n88\n00"], "sample_outputs": ["0\n2", "2\n0"], "notes": "NoteFirst sample is elaborated in the problem statement. In the second sample, there is no way Moriarty can avoid getting two flicks."}, "positive_code": [{"source_code": "n=gets.to_i\na,b=$<.map{|e|e.chomp.bytes.sort}\nj=0\np n-b.count{|e|e>=a[j]&&j+=1}\nj=0\np b.count{|e|e>a[j]&&j+=1}"}], "negative_code": [{"source_code": "#!/usr/bin/ruby\nn=gets.to_i\na,b=$<.map{|e|e.split.map(&:to_i).sort}\nj=0\np n-b.count{|e|e>=a[j]&&j+=1}\nj=0\np b.count{|e|e>a[j]&&j+=1}"}], "src_uid": "d1a35297090550d9a95f014b0c09a01a"} {"nl": {"description": "It's a very unfortunate day for Volodya today. He got bad mark in algebra and was therefore forced to do some work in the kitchen, namely to cook borscht (traditional Russian soup). This should also improve his algebra skills.According to the borscht recipe it consists of n ingredients that have to be mixed in proportion litres (thus, there should be a1 ·x, ..., an ·x litres of corresponding ingredients mixed for some non-negative x). In the kitchen Volodya found out that he has b1, ..., bn litres of these ingredients at his disposal correspondingly. In order to correct his algebra mistakes he ought to cook as much soup as possible in a V litres volume pan (which means the amount of soup cooked can be between 0 and V litres). What is the volume of borscht Volodya will cook ultimately?", "input_spec": "The first line of the input contains two space-separated integers n and V (1 ≤ n ≤ 20, 1 ≤ V ≤ 10000). The next line contains n space-separated integers ai (1 ≤ ai ≤ 100). Finally, the last line contains n space-separated integers bi (0 ≤ bi ≤ 100).", "output_spec": "Your program should output just one real number — the volume of soup that Volodya will cook. Your answer must have a relative or absolute error less than 10 - 4.", "sample_inputs": ["1 100\n1\n40", "2 100\n1 1\n25 30", "2 100\n1 1\n60 60"], "sample_outputs": ["40.0", "50.0", "100.0"], "notes": null}, "positive_code": [{"source_code": "def min(a, b)\n a < b ? a : b\nend\n\nclass Array\n def sum\n self.inject { |r, v| r + v }\n end\nend\n\nn, volume = gets.split.map { |v| v.to_f }\na = gets.split.map { |v| v.to_f }\nb = gets.split.map { |v| v.to_f }\n\nmultiplier = min(volume / a.sum, b.zip(a).map { |f, s| f / s }.min)\n\nputs multiplier * a.sum\n"}, {"source_code": "sum , i= 0,0\narr = gets.split(' ').map { |x| x.to_i }\nn,v = arr[0],arr[1]\nres = v\na = gets.split(' ').map { |x| x.to_i }\nfor c in a\n sum += c\nend\nb = gets.split(' ').map { |x| x.to_i }\nfor s in b\n if res > 1.00*s*sum/a[i] then\n res = 1.00*s*sum/a[i]\n end\n i += 1\nend\nputs res"}], "negative_code": [], "src_uid": "351d8874a10f84d157a7b2e1eb64e2a1"} {"nl": {"description": "On one quiet day all of sudden Mister B decided to draw angle a on his field. Aliens have already visited his field and left many different geometric figures on it. One of the figures is regular convex n-gon (regular convex polygon with n sides).That's why Mister B decided to use this polygon. Now Mister B must find three distinct vertices v1, v2, v3 such that the angle (where v2 is the vertex of the angle, and v1 and v3 lie on its sides) is as close as possible to a. In other words, the value should be minimum possible.If there are many optimal solutions, Mister B should be satisfied with any of them.", "input_spec": "First and only line contains two space-separated integers n and a (3 ≤ n ≤ 105, 1 ≤ a ≤ 180) — the number of vertices in the polygon and the needed angle, in degrees.", "output_spec": "Print three space-separated integers: the vertices v1, v2, v3, which form . If there are multiple optimal solutions, print any of them. The vertices are numbered from 1 to n in clockwise order.", "sample_inputs": ["3 15", "4 67", "4 68"], "sample_outputs": ["1 2 3", "2 1 3", "4 1 2"], "notes": "NoteIn first sample test vertices of regular triangle can create only angle of 60 degrees, that's why every possible angle is correct.Vertices of square can create 45 or 90 degrees angles only. That's why in second sample test the angle of 45 degrees was chosen, since |45 - 67| < |90 - 67|. Other correct answers are: \"3 1 2\", \"3 2 4\", \"4 2 3\", \"4 3 1\", \"1 3 4\", \"1 4 2\", \"2 4 1\", \"4 1 3\", \"3 1 4\", \"3 4 2\", \"2 4 3\", \"2 3 1\", \"1 3 2\", \"1 2 4\", \"4 2 1\".In third sample test, on the contrary, the angle of 90 degrees was chosen, since |90 - 68| < |45 - 68|. Other correct answers are: \"2 1 4\", \"3 2 1\", \"1 2 3\", \"4 3 2\", \"2 3 4\", \"1 4 3\", \"3 4 1\"."}, "positive_code": [{"source_code": "n, a=gets.split.map &:to_i\nx=180.0*(n-2)/n\nminD=(x-a).abs\nminX=2\n(3...n).each do |i|\n b=(180.0*(i-2)-x*(i-2))/2\n d=(b-a).abs\n if d 0) \n x += 1\n else \n x -= 1\n end\n}\nputs x"}, {"source_code": "def answers ()\n n = gets.to_i\n count = 0\n for i in 0...n\n ope = gets.chomp\n if ope.include? \"+\"\n count +=1\n else \n count -=1\n end\n end\n puts count\nend\n\nanswers"}, {"source_code": "a = gets.chomp.to_i\nx = 0\nwhile a>0\n a-=1\n if gets.chomp[1]=='+'\n x+=1\n else\n x-=1\n end\nend\nprint x"}, {"source_code": "x = 0\nn = gets.chomp.to_i\ni = 0\nif n >= 1 && n <= 150\n\twhile i < n\n\t\tstatement = gets.chomp\n\t\tif statement == \"++X\" || statement == \"++x\" || statement == \"x++\" || statement == \"X++\"\n\t\t\tx += 1\n\t\telsif statement == \"--X\" || statement == \"--x\" || statement == \"x--\" || statement == \"X--\"\n\t\t\tx -= 1\n\t\tend\n\t\ti += 1\n\tend\nend\n\nputs x"}, {"source_code": "n=gets.chomp.to_i\nx=0\nn.times{\n s=gets.chomp.to_s\n if s.scan(/\\+\\+/) { |match| \n x+=1\n }\n end\n if s.scan(/--/) { |match| \n x-=1\n }\n end\n}\nputs (x)"}, {"source_code": "r=0;gets.to_i.times{gets[1]=='+'?(r+=1):(r-=1)};puts r"}, {"source_code": "n=Integer(gets.chomp)\nx=0\nuntil n==0\n if gets.chomp.index('+')!=nil\n x = 1 + x\n else\n x = x - 1\n end\n n -= 1\nend\nputs x"}, {"source_code": "n = gets.chomp.to_i\nans = 0\nn.times{\n line = gets.chomp\n ans += 1 if line.include? \"+\"\n ans -= 1 if line.include? \"-\"\n}\nputs ans"}, {"source_code": "a=gets.chomp.to_i\nx=0\na.times do\nb=gets.chomp\nif b[0]==\"+\" || b[2] == \"+\"\nx+=1\nelse \nx-=1\nend \nend\n\nputs \"#{x}\""}, {"source_code": "$stdin = File.open(\"./input.txt\", \"r\") if ENV[\"USER\"] == \"new\"\n# ====================================\n\nx=0\nn = gets.to_i\n\nn.times{x+=(gets.strip.include? \"+\")? 1 : -1;}\n\nputs x"}, {"source_code": "r = 0\n\ngets.to_i.times do\n if gets[1] == '+'\n r += 1\n else\n r -= 1\n end\nend\n\nputs r\n"}, {"source_code": "a=gets.chomp.to_i\nx=0\na.times do\nb=gets.chomp\nif b[0]==\"+\" || b[2] == \"+\"\nx+=1\nelse \nx-=1\nend \nend\n\nputs \"#{x}\""}, {"source_code": "n = gets.to_i\nputs n.times.map{gets['++'] ? +1 : -1}.inject(:+)\n"}, {"source_code": "x=0\ngets.to_i.times{\n\tif gets.include?(\"++\")\n\t\tx+=1\n\telse\n\t\tx-=1\n\tend\n}\nputs x"}, {"source_code": "x = 0\ngets.chomp.to_i.times do \n\ts = gets.chomp\n\tif s =~ /(\\+\\+(x|X)|(x|X)\\+\\+)/\n\t\tx += 1\n\telse\n\t\tx -= 1\n\tend\nend\nputs x"}, {"source_code": "n = gets.to_i\nvalue = 0\nn.times do |_|\n statement = gets\n statement.include?('++') ? value += 1 : value -= 1\nend\nputs value"}, {"source_code": "n = gets.to_i\nvalue = 0\nn.times do |_|\n statement = gets\n statement.include?('++') ? value += 1 : value -= 1\nend\nputs value"}, {"source_code": "p (gets p).count(?+)-$_.count(?-)>>1"}, {"source_code": "x=0\ngets.to_i.times{\n\ts=gets.chomp\n\tx=x+(\" ++X++\".index(s)?1:-1)\n}\nputs x"}, {"source_code": "n = gets.to_i\nsum = 0\n\n(1..n).each do |i|\n s = gets.chomp\n if s[1] == '+'\n sum += 1\n else\n sum -= 1\n end\nend\n\nputs sum"}, {"source_code": "n = gets.to_i\no=0\n\nfor i in 1..n\n gets.chomp[1] == '+' ? o+=1 : o-=1\nend\n\np o"}, {"source_code": "input = ARGV ? $< : File(ARGV[0])\n\nn = gets.to_i\n\ncount = 0\nfor i in input.each_line.take n\n\tif i[0] == '+' || i[-2] == '+'\n\t\tcount += 1\n\telse\n\t\tcount -= 1\n\tend\nend\n\nputs count\n"}, {"source_code": "x=0\ngets.to_i.times{\n\ts=gets.chomp\n\tx=x+(\" ++X++\".index(s)?1:-1)\n}\nputs x"}, {"source_code": "n = gets.chomp.to_i\nc = 0\nn.times do\n str = gets\n if str.include? \"+\"\n c += 1\n else\n c -= 1\n end \nend\nputs c\n \n"}, {"source_code": "n = gets.chomp.to_i\n\nx = 0\n1.upto(n) do |i|\n instruction = gets.chomp\n instruction.each_char do |char|\n if char == \"+\"\n x += 1\n break\n elsif char == \"-\"\n x -= 1\n break\n end\n end\nend\nputs x"}, {"source_code": "x = 0\nn = gets.chomp.to_i\ni = 0\nif n >= 1 && n <= 150\n\twhile i < n\n\t\tstatement = gets.chomp\n\t\tif statement == \"++X\" || statement == \"++x\" || statement == \"x++\" || statement == \"X++\"\n\t\t\tx += 1\n\t\telsif statement == \"--X\" || statement == \"--x\" || statement == \"x--\" || statement == \"X--\"\n\t\t\tx -= 1\n\t\tend\n\t\ti += 1\n\tend\nend\n\nputs x"}, {"source_code": "total = 0\nn = gets.to_i.times {\n\tgets[1] == '+' ? (total += 1):(total -=1)\n}\nputs total"}, {"source_code": "x = 0\nn = gets.chomp.to_i\nn.times do\n\tvalue = gets.chomp\n\tx = x + 1 if value.scan(\"+\").count == 2 unless value.scan(\"X\").count == 0\n\tx = x - 1 if value.scan(\"-\").count == 2 unless value.scan(\"X\").count == 0\nend\nputs(x)"}, {"source_code": "x=0;n=gets.to_i;n.times{x+=((gets.include? \"+\")?1:-1)};p x\n"}, {"source_code": "$stdin = File.open(\"./input.txt\", \"r\") if ENV[\"USER\"] == \"new\"\n# ====================================\n\nx=0\nn = gets.to_i\n\nn.times{\n cmd = gets.strip\n \n x+=1 if cmd.include? \"+\"\n x-=1 if cmd.include? \"-\"\n}\n\nputs x"}, {"source_code": "n=gets.chomp.to_i\ny=0\nwhile n>0\nb=gets.chomp\n\nif(b==\"++X\" or b==\"X++\")\ny+=1\nend\nif(b==\"--X\" or b==\"X--\")\ny=y-1\nend\nn=n-1\nend\nprint y"}, {"source_code": "x = 0\n\ngets.strip.to_i.times do\n statement = gets.strip\n x += 1 if statement.include?('++')\n x -= 1 if statement.include?('--')\nend\n\nputs x\n"}, {"source_code": "ans = 0\ngets.to_i.times do |i|\n if gets.include?(\"++\")\n ans += 1\n else\n ans -= 1\n end\nend\nputs ans"}, {"source_code": "require 'scanf'\n\nn = $stdin.readline.to_i\nx = 0\n\nn.times do |i|\n line = $stdin.readline\n line = line.chomp('').downcase\n line = line[0, 3]\n if line == \"++x\" || line == \"x++\"\n x += 1\n elsif line == \"--x\" || line == \"x--\"\n x -= 1\n end\nend\n\nputs x\n"}, {"source_code": "x = 0\ngets.chomp.to_i.times do \n\ts = gets.chomp\n\tif s =~ /(\\+\\+(x|X)|(x|X)\\+\\+)/\n\t\tx += 1\n\telse\n\t\tx -= 1\n\tend\nend\nputs x"}, {"source_code": "n = gets.to_i\nans = 0\nn.times.map{gets}.map{|x| x.include?('++') ? ans += 1 : ans -= 1}\nputs ans\n"}, {"source_code": "n = gets.to_i\nX = 0\n\nn.times do\n\npokemon = gets.to_s\n\npokemon.delete! (\"X\")\n\nb = pokemon.scan(/[+]/).length\n\nif b >= 1\nX += 1\n\nelse\nX -= 1\n\n\n\nend\n\nend\n\np X\n\n\n"}, {"source_code": "operations = []\ngets.to_i.times { operations << gets }\nputs operations.inject(0) { |x, operation| operation.include?(\"++\") ? x + 1 : x - 1}"}, {"source_code": "stateNum = gets.chomp.to_i\nvalue = 0\n\nfor i in 0...stateNum\n\tstatement = gets.chomp\n\n\tvalue += 1 if statement.include?('+')\n\tvalue -= 1 if statement.include?('-')\nend\n\nputs value"}, {"source_code": "gets p;p ($_.count(?+)-$_.count(?-))/2"}, {"source_code": "r = 0\n\ngets.to_i.times do\n if gets[1] == '+'\n r += 1\n else\n r -= 1\n end\nend\n\nputs r\n"}, {"source_code": "number = gets.strip.to_i\n\nx = 0\nnumber.times do\n input = gets.strip\n if input.downcase == '++x' || input.downcase == 'x++'\n x += 1\n else\n x -= 1\n end\nend\n\nputs x\n"}, {"source_code": "n = gets.chomp\nn = n.to_i\n\nres = 0\n\nn.times do\n cmd = gets.chomp\n res += 1 if cmd == \"++X\" or cmd == \"X++\"\n res -= 1 if cmd == \"--X\" or cmd == \"X--\"\nend\n\nputs res\n"}, {"source_code": "n = gets.chomp.to_i\nans = 0\nn.times{\n line = gets.chomp\n ans += 1 if line.include? \"+\"\n ans -= 1 if line.include? \"-\"\n}\nputs ans"}, {"source_code": "x = 0\ngets.to_i.times {\n x += gets.include?(\"+\") ? 1 : -1\n}\nputs x\n"}, {"source_code": "ret = 0\ngets.to_i.times do\n if (gets =~ /-/) then ret -= 1 else ret += 1 end\nend\nputs ret"}, {"source_code": "x=0\ngets.to_i.times{\n\ts=gets.chomp\n\tx=x+(\" ++X++\".index(s)?1:-1)\n}\nputs x"}, {"source_code": "x = 0\ni = gets.to_i\n\nwhile i > 0 do\n if gets.index('--') != nil\n x = x - 1\n else\n x = x + 1\n end\n i = i - 1\nend\n\nprint x"}, {"source_code": "n = gets.to_i\nnilai = 0\nfor i in 1..n\n\tkata = gets.chomp\n\tmyarray =kata.split(\"\")\n\tif (myarray[0] == \"+\" && myarray[1] == \"+\") || (myarray[1] == \"+\" && myarray[2] == \"+\")\n\t\tnilai += 1\n\telse\n\t\tnilai -= 1\n\tend\nend\n\nputs nilai\n"}, {"source_code": "a = gets.chomp.to_i\nx = 0\nwhile a>0\n a-=1\n if gets.chomp[1]=='+'\n x+=1\n else\n x-=1\n end\nend\nprint x"}, {"source_code": "x = 0\nn = gets.chomp.to_i\nn.times do\n\tvalue = gets.chomp\n\tx = x + 1 if value.scan(\"+\").count == 2 unless value.scan(\"X\").count == 0\n\tx = x - 1 if value.scan(\"-\").count == 2 unless value.scan(\"X\").count == 0\nend\nputs(x)"}, {"source_code": "r=0;gets.to_i.times{gets[1]=='+'?(r+=1):(r-=1)};puts r"}, {"source_code": "n=Integer(gets.chomp)\nx=0\nuntil n==0\n if gets.chomp.index('+')!=nil\n x = 1 + x\n else\n x = x - 1\n end\n n -= 1\nend\nputs x"}, {"source_code": "input = ARGV ? $< : File(ARGV[0])\n\nn = gets.to_i\n\ncount = 0\nfor i in input.each_line.take n\n\tif i[0] == '+' || i[-2] == '+'\n\t\tcount += 1\n\telse\n\t\tcount -= 1\n\tend\nend\n\nputs count\n"}, {"source_code": "ans = 0\ngets.to_i.times do |i|\n if gets.include?(\"++\")\n ans += 1\n else\n ans -= 1\n end\nend\nputs ans"}, {"source_code": "gets p;p ($_.count(?+)-$_.count(?-))/2"}, {"source_code": "p gets.to_i.times.map{gets=~/\\+/?1: -1}.inject &:+\n"}, {"source_code": "n = gets.chomp.to_i\n\nans = 0\nn.times {\n\n s = gets.chomp\n \n if s[1] == '+'\n ans += 1\n else \n ans -= 1\n end\n}\n\nputs ans"}, {"source_code": "x = 0\n\n#input\ncounter = gets.chomp.to_i\nlisting = []\n\nwhile counter != 0\n\ts = gets.chomp\n\tif s.include?(\"++\")\n\t\tx += 1\n\telsif s.include?(\"--\")\n\t\tx -= 1\n\tend\n\n\tcounter -= 1\nend\n\nputs x\t\t\n"}, {"source_code": "n = gets.to_i\nnilai = 0\nfor i in 1..n\n\tkata = gets.chomp\n\tmyarray =kata.split(\"\")\n\tif (myarray[0] == \"+\" && myarray[1] == \"+\") || (myarray[1] == \"+\" && myarray[2] == \"+\")\n\t\tnilai += 1\n\telse\n\t\tnilai -= 1\n\tend\nend\n\nputs nilai\n"}, {"source_code": "n = gets.to_i\n\nx = 0\nn.times do\n if gets.count('+') != 0\n x += 1\n else\n x -= 1\n end\nend\n\nputs x"}, {"source_code": "n=gets.chomp.to_i\nx=0\nn.times{\n s=gets.chomp.to_s\n if s.scan(/\\+\\+/) { |match| \n x+=1\n }\n end\n if s.scan(/--/) { |match| \n x-=1\n }\n end\n}\nputs (x)"}, {"source_code": "result = 0\ngets.chomp.to_i.times do\n case gets.chomp.gsub(/X/, '')\n when '++' then result += 1\n when '--' then result -= 1\n end\nend\nputs result"}, {"source_code": "ret = 0\ngets.to_i.times do\n if (gets =~ /-/) then ret -= 1 else ret += 1 end\nend\nputs ret"}, {"source_code": "x = gets.to_i\ni = 0\n\nfor j in 1..x do\n\ty = gets.chomp\n\tif y[1] == \"+\"\n\t\ti = i+1\n\telse\n\t\ti = i-1\n\tend\nend\nputs i"}, {"source_code": "r=0;gets.to_i.times{gets[1]=='+'?(r+=1):(r-=1)};puts r"}, {"source_code": "p (gets p).count(?+)-$_.count(?-)>>1"}, {"source_code": "n = gets.to_i\ns = Array.new(n)\n\ncounters,xounterz = 0,0\nfor i in 0..n-1\n s[i] = gets.to_s\n if s[i].include? \"+\"\n counters+=1\n else\n xounterz+=1\n end\nend\nputs counters-xounterz"}, {"source_code": "x=0;n=gets.to_i;n.times{x+=((gets.include? \"+\")?1:-1)};p x\n"}, {"source_code": "n = gets.to_i\nputs n.times.map{gets['++'] ? +1 : -1}.inject(:+)\n"}, {"source_code": "x = 0\ngets.to_i.times {\n x += gets.include?(\"+\") ? 1 : -1\n}\nputs x\n"}, {"source_code": "r = 0\n\ngets.to_i.times do\n if gets[1] == '+'\n r += 1\n else\n r -= 1\n end\nend\n\nputs r\n"}, {"source_code": "n = gets.to_i\nvalue = 0\nn.times do |_|\n statement = gets\n statement.include?('++') ? value += 1 : value -= 1\nend\nputs value"}, {"source_code": "n = gets.to_i\nvalue = 0\nn.times do |_|\n statement = gets\n statement.include?('++') ? value += 1 : value -= 1\nend\nputs value"}, {"source_code": "s = gets.split(\" \")\nn = s[0].to_i\nrez = 0\nfor i in 1..n\n s = gets\n if(s[1] == \"-\") \n rez -= 1\n end\n if(s[1] == \"+\") \n rez += 1\n end\nend\nputs(rez)"}, {"source_code": "n = gets.to_i\no=0\n\nfor i in 1..n\n gets.chomp[1] == '+' ? o+=1 : o-=1\nend\n\np o"}, {"source_code": "ret = 0\ngets.to_i.times do\n if (gets =~ /-/) then ret -= 1 else ret += 1 end\nend\nputs ret"}, {"source_code": "n = gets.to_i\nx = 0\n\n1.upto(n) do\n line = gets\n\n case\n when line =~ /\\+\\+/\n x += 1\n when line =~ /--/\n x -= 1\n end\nend\n\nputs x\n"}, {"source_code": "n = gets.to_i\nx = 0\nfor i in 0...n\n operation = gets.chomp.to_s\n case operation\n when \"++X\",\"X++\"\n x+=1\n when \"--X\",\"X--\"\n x-=1\n end\nend\nputs x"}, {"source_code": "ans = 0\ngets.to_i.times do |i|\n if gets.include?(\"++\")\n ans += 1\n else\n ans -= 1\n end\nend\nputs ans"}, {"source_code": "n = gets.strip.to_i \narr = []\nlast = \"\"\n\nn.times do |i|\n x = gets.strip.to_s\n x.include?(\"+\") ? arr << 1 : arr << -1\n last = x if (n - i == 1) \nend \n\n\nputs arr.reduce(0){|init,ele| init + ele}\n\n\n"}, {"source_code": "n = gets.chomp.to_i\nc = 0\nn.times do\n str = gets\n if str.include? \"+\"\n c += 1\n else\n c -= 1\n end \nend\nputs c\n \n"}, {"source_code": "n = Integer(gets)\noperation = Array.new\nfor i in 0...n\n operation[i] = Array.new\n input = gets.chomp\n operation[i] = input\nend\nx = 0\nfor i in 0...n\n if operation[i] == \"++X\" || operation[i] ==\"X++\"\n x += 1\n else\n x -= 1\n end\nend\nputs x"}, {"source_code": "n = gets.strip.to_i\nx = 0\nvar = \"X\"\nn.times do \n str = gets.strip\n if str == \"++#{var}\" || str == \"#{var}++\" then\n x += 1\n elsif str == \"--#{var}\" || str == \"#{var}--\" then\n x -= 1\n end\nend\nputs x"}, {"source_code": "n = gets.to_i\nnilai = 0\nfor i in 1..n\n\tkata = gets.chomp\n\tmyarray =kata.split(\"\")\n\tif (myarray[0] == \"+\" && myarray[1] == \"+\") || (myarray[1] == \"+\" && myarray[2] == \"+\")\n\t\tnilai += 1\n\telse\n\t\tnilai -= 1\n\tend\nend\n\nputs nilai\n"}, {"source_code": "value = gets.chomp\n\noperation = []\nvalue.to_i.times do |i|\n\tvalue = gets.chomp\n\toperation << value\nend\n x = 0\noperation.each do |line|\n\tif line.include?('++')\n\t\tx += 1\n\telsif line.include?('--')\n\t\t\tx -= 1\n\tend\n\nend\n\nputs x\n"}, {"source_code": "r = 0\n\ngets.to_i.times do\n if gets[1] == '+'\n r += 1\n else\n r -= 1\n end\nend\n\nputs r\n"}, {"source_code": "p gets.to_i.times.map{gets=~/\\+/?1: -1}.inject &:+\n"}, {"source_code": "n = gets.to_i\nrt = n\nn.times do\n rt-=2 if gets=~/-/\nend\nputs rt"}, {"source_code": "r = 0\n\ngets.to_i.times do\n if gets[1] == '+'\n r += 1\n else\n r -= 1\n end\nend\n\nputs r\n"}, {"source_code": "x = 0\n\ngets.strip.to_i.times do\n statement = gets.strip\n x += 1 if statement.include?('++')\n x -= 1 if statement.include?('--')\nend\n\nputs x\n"}, {"source_code": "ans = 0\ngets.to_i.times do |i|\n if gets.include?(\"++\")\n ans += 1\n else\n ans -= 1\n end\nend\nputs ans"}], "negative_code": [{"source_code": "result = 0\ngets.chomp.to_i.times do\n case gets.chomp\n when 'X++' then result += 1\n when '--X' then result -= 1\n end\nend\nputs result"}, {"source_code": "k=gets.to_i\nl=[]\nk.times{l << gets.chomp}\nprint l.count(\"++X\")-l.count(\"--X\")"}, {"source_code": "x = 0\ngets.chomp.to_i.times do \n\ts = gets.chomp\n\tif s =~ /(\\+\\+x|x\\+\\+)/\n\t\tx += 1\n\telse\n\t\tx -= 1\n\tend\nend\nputs x"}, {"source_code": "n = gets.to_i\nx = 0\nn.times do\n if gets.chomp == \"x++\"\n x += 1\n else\n x -= 1\n end\nend\n\nputs x"}, {"source_code": "sum = 0\ngets.to_i.times{\n if gets[1]==\"+\"\n sum+=1\n else\n sum-=1\n end\n}\nsum"}, {"source_code": "n = gets.strip.to_i \narr = []\nlast = \"\"\n\nn.times do |i|\n x = gets.strip.to_s\n x.include?(\"+\") ? arr << 1 : arr << -1\n last = x if (n - i == 1) \nend \n\nif last[0] != \"+\" and last[0] != \"-\"\n arr[arr.length - 1] = 0\nend\n\nputs arr.reduce(0){|init,ele| init + ele}\n\n\n"}, {"source_code": "i = gets.to_i\ncounter = 0\n(1..i).each do |x|\n counter += gets.chomp === 'X++' ? 1 : -1\nend\nprint counter"}, {"source_code": "a=$<;p a.count(?+)-a.count(?-)>>1\n"}, {"source_code": "input_line_num = gets.chomp.to_i\nn = 0\nres = 0\nwhile input_line_num > n\n input_peration = gets.chomp\n case input_peration.include?(\"+\")\n when true\n res +=1\n when false\n if res < 0\n res = 0\n end\n res -=1\n end\n n +=1\nend\np res\n"}, {"source_code": "num = gets.chomp.to_i\nlines = []\ni = 0\nx = 0\nwhile (i < num)\n\tlines[i] = gets.chomp\n\ti = i + 1\nend\nj = 0\nwhile (j < lines.length)\n\tif(lines[j] == \"++x\" || lines[j] == \"x++\")\n\t\tx = x +1\n\telse\n\tx = x -1\n\tend\n\tj = j +1\nend\nputs x.to_s"}, {"source_code": "number = gets.strip.to_i\n\nx = 0\nnumber.times do\n input = gets.strip\n if input == '++x'\n x += 1\n else\n x -= 1\n end\nend\n\nputs x\n"}], "src_uid": "f3cf7726739290b280230b562cac7a74"} {"nl": {"description": "You have a card deck of $$$n$$$ cards, numbered from top to bottom, i. e. the top card has index $$$1$$$ and bottom card — index $$$n$$$. Each card has its color: the $$$i$$$-th card has color $$$a_i$$$.You should process $$$q$$$ queries. The $$$j$$$-th query is described by integer $$$t_j$$$. For each query you should: find the highest card in the deck with color $$$t_j$$$, i. e. the card with minimum index; print the position of the card you found; take the card and place it on top of the deck. ", "input_spec": "The first line contains two integers $$$n$$$ and $$$q$$$ ($$$2 \\le n \\le 3 \\cdot 10^5$$$; $$$1 \\le q \\le 3 \\cdot 10^5$$$) — the number of cards in the deck and the number of queries. The second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 50$$$) — the colors of cards. The third line contains $$$q$$$ integers $$$t_1, t_2, \\dots, t_q$$$ ($$$1 \\le t_j \\le 50$$$) — the query colors. It's guaranteed that queries ask only colors that are present in the deck.", "output_spec": "Print $$$q$$$ integers — the answers for each query.", "sample_inputs": ["7 5\n2 1 1 4 3 3 1\n3 2 1 1 4"], "sample_outputs": ["5 2 3 1 5"], "notes": "NoteDescription of the sample: the deck is $$$[2, 1, 1, 4, \\underline{3}, 3, 1]$$$ and the first card with color $$$t_1 = 3$$$ has position $$$5$$$; the deck is $$$[3, \\underline{2}, 1, 1, 4, 3, 1]$$$ and the first card with color $$$t_2 = 2$$$ has position $$$2$$$; the deck is $$$[2, 3, \\underline{1}, 1, 4, 3, 1]$$$ and the first card with color $$$t_3 = 1$$$ has position $$$3$$$; the deck is $$$[\\underline{1}, 2, 3, 1, 4, 3, 1]$$$ and the first card with color $$$t_4 = 1$$$ has position $$$1$$$; the deck is $$$[1, 2, 3, 1, \\underline{4}, 3, 1]$$$ and the first card with color $$$t_5 = 4$$$ has position $$$5$$$. "}, "positive_code": [{"source_code": "n, q = gets.to_s.split.map {|x| x.to_i }\na = gets.to_s.split.map {|x| x.to_i }\nt = gets.to_s.split.map {|x| x.to_i }\n\npos = [a.size] * 51\n\na.each.with_index {|color, idx| pos[color] = [idx + 1, pos[color]].min }\n\nt.each do |color|\n idx = pos[color]\n printf(\"%d \", pos[color])\n pos.map! {|x| ((x < idx) ? x + 1 : x) }\n pos[color] = 1\nend\nprintf(\"\\n\")\n"}], "negative_code": [], "src_uid": "26aef004295df530352485ce53b47364"} {"nl": {"description": "Scientists say a lot about the problems of global warming and cooling of the Earth. Indeed, such natural phenomena strongly influence all life on our planet.Our hero Vasya is quite concerned about the problems. He decided to try a little experiment and observe how outside daily temperature changes. He hung out a thermometer on the balcony every morning and recorded the temperature. He had been measuring the temperature for the last n days. Thus, he got a sequence of numbers t1, t2, ..., tn, where the i-th number is the temperature on the i-th day.Vasya analyzed the temperature statistics in other cities, and came to the conclusion that the city has no environmental problems, if first the temperature outside is negative for some non-zero number of days, and then the temperature is positive for some non-zero number of days. More formally, there must be a positive integer k (1 ≤ k ≤ n - 1) such that t1 < 0, t2 < 0, ..., tk < 0 and tk + 1 > 0, tk + 2 > 0, ..., tn > 0. In particular, the temperature should never be zero. If this condition is not met, Vasya decides that his city has environmental problems, and gets upset.You do not want to upset Vasya. Therefore, you want to select multiple values of temperature and modify them to satisfy Vasya's condition. You need to know what the least number of temperature values needs to be changed for that.", "input_spec": "The first line contains a single integer n (2 ≤ n ≤ 105) — the number of days for which Vasya has been measuring the temperature. The second line contains a sequence of n integers t1, t2, ..., tn (|ti| ≤ 109) — the sequence of temperature values. Numbers ti are separated by single spaces.", "output_spec": "Print a single integer — the answer to the given task.", "sample_inputs": ["4\n-1 1 -2 1", "5\n0 -1 1 2 -5"], "sample_outputs": ["1", "2"], "notes": "NoteNote to the first sample: there are two ways to change exactly one number so that the sequence met Vasya's condition. You can either replace the first number 1 by any negative number or replace the number -2 by any positive number."}, "positive_code": [{"source_code": "# https://codeforces.com/problemset/problem/234/C\n\nn = 0\narr = []\n\nFile.open(\"input.txt\", \"r\") do |f|\n n = f.gets.to_i - 2\n arr = f.gets.split(' ').map(&:to_i)\nend\n\ns = (arr[0] >= 0 ? 1 : 0) + (arr[-1] <= 0 ? 1 : 0)\n\nFile.open(\"output.txt\", \"w\") do |f|\n if n > 0\n arr = arr[1...-1]\n\n temp = Array.new(n, 0)\n\n correct_count = 0\n n.times do |i|\n correct_count += 1 if arr[i] < 0\n temp[i] += correct_count\n end\n\n correct_count = 0\n (n-1).downto(0) do |i|\n correct_count += 1 if arr[i] > 0\n temp[i] += correct_count\n end\n\n f.write(\"#{s + n - temp.max}\\n\")\n else\n f.write(\"#{s}\\n\")\n end\nend"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\nn=gets.to_i\na=gets.split.map &:to_i\nx,y=[0],[0]\nn.times{|i|\n\tx<0]\n\ty<0]\n}\np [*1...n].map{|i|x[i]+y[n]-y[i]}.min\n"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\nn=gets.to_i\na=gets.split.map &:to_i\nx,y=[0],[0]\nn.times{|i|\n\tx<0]\n\ty<0]\n}\np [*1...n].map{|i|x[i]+y[n]-y[i]}.min\n"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\nn=gets.to_i\na=gets.split.map &:to_i\nx,y=[0],[0]\nn.times{|i|\n\tx<0]\n\ty<0]\n}\np [*1...n].map{|i|x[i]+y[n]-y[i]}.min\n"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\nn=gets.to_i\na=gets.split.map &:to_i\nx,y=[0],[0]\nn.times{|i|\n\tx<0]\n\ty<0]\n}\np [*1...n].map{|i|x[i]+y[n]-y[i]}.min\n"}, {"source_code": "n = 0\narr = []\nopen(\"input.txt\") {|f|\n n = f.gets.to_i\n arr = f.gets.to_s.chomp.split.map(&:to_i)\n}\nFile.open(\"output.txt\", \"w\"){|f|\n t1 = 0\n t2 = 0\n ar1 = []\n ar2 = []\n n.times do |i|\n t1 += 1 if arr[i] >= 0\n ar1.push(t1)\n t2 += 1 if arr[n-1-i] <= 0\n ar2.push(t2)\n end\n ar2.reverse!\n ar3 = []\n #ar3.push(ar1[n-1])\n #ar3.push(ar2[0])\n (n-1).times do |i|\n ar3.push(ar1[i]+ar2[i+1])\n end\n #p ar1\n #p ar2\n #p ar3\n f.write \"#{ar3.min}\\n\"\n}"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\nn=gets.to_i\na=gets.split.map &:to_i\nx,y=[0],[0]\nn.times{|i|\n\tx<0]\n\ty<0]\n}\np [*1...n].map{|i|x[i]+y[n]-y[i]}.min"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\nn=gets.to_i\na=gets.split.map &:to_i\nx,y=[0],[0]\nn.times{|i|\n\tx<0]\n\ty<0]\n}\np [*1...n].map{|i|x[i]+y[n]-y[i]}.min\n"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\nn=gets.to_i\na=gets.split.map &:to_i\nx,y=[0],[0]\nn.times{|i|\n\tx<0]\n\ty<0]\n}\np [*1...n].map{|i|x[i]+y[n]-y[i]}.min\n"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\nn=gets.to_i\na=gets.split.map &:to_i\nx,y=[0],[0]\nn.times{|i|\n\tx<0]\n\ty<0]\n}\np [*1...n].map{|i|x[i]+y[n]-y[i]}.min\n"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\nn=gets.to_i\na=gets.split.map &:to_i\nx,y=[0],[0]\nn.times{|i|\n\tx<0]\n\ty<0]\n}\np [*1...n].map{|i|x[i]+y[n]-y[i]}.min\n"}], "negative_code": [{"source_code": "n = 0\narr = []\nopen(\"input.txt\") {|f|\n n = f.gets.to_i\n arr = f.gets.to_s.chomp.split.map(&:to_i)\n}\nFile.open(\"output.txt\", \"w\"){|f|\n t1 = 0\n t2 = 0\n ar1 = []\n ar2 = []\n n.times do |i|\n t1 += 1 if arr[i] >= 0\n ar1.push(t1)\n t2 += 1 if arr[n-1-i] <= 0\n ar2.unshift(t2)\n end\n ar3 = []\n ar3.push(ar1[n-1])\n ar3.push(ar2[0])\n (n-1).times do |i|\n ar3.push(ar1[i]+ar2[i+1])\n end\n #p ar1\n #p ar2\n #p ar3\n f.write \"#{ar3.min}\\n\"\n}"}], "src_uid": "165e18e39d2f60c72f22e3027a29a742"} {"nl": {"description": "An array is beautiful if both of the following two conditions meet: there are at least $$$l_1$$$ and at most $$$r_1$$$ elements in the array equal to its minimum; there are at least $$$l_2$$$ and at most $$$r_2$$$ elements in the array equal to its maximum. For example, the array $$$[2, 3, 2, 4, 4, 3, 2]$$$ has $$$3$$$ elements equal to its minimum ($$$1$$$-st, $$$3$$$-rd and $$$7$$$-th) and $$$2$$$ elements equal to its maximum ($$$4$$$-th and $$$5$$$-th).Another example: the array $$$[42, 42, 42]$$$ has $$$3$$$ elements equal to its minimum and $$$3$$$ elements equal to its maximum.Your task is to calculate the minimum possible number of elements in a beautiful array.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 5000$$$) — the number of test cases. Each test case consists of one line containing four integers $$$l_1$$$, $$$r_1$$$, $$$l_2$$$ and $$$r_2$$$ ($$$1 \\le l_1 \\le r_1 \\le 50$$$; $$$1 \\le l_2 \\le r_2 \\le 50$$$).", "output_spec": "For each test case, print one integer — the minimum possible number of elements in a beautiful array.", "sample_inputs": ["7\n\n3 5 4 6\n\n5 8 5 5\n\n3 3 10 12\n\n1 5 3 3\n\n1 1 2 2\n\n2 2 1 1\n\n6 6 6 6"], "sample_outputs": ["4\n5\n13\n3\n3\n3\n6"], "notes": "NoteOptimal arrays in the test cases of the example: $$$[1, 1, 1, 1]$$$, it has $$$4$$$ minimums and $$$4$$$ maximums; $$$[4, 4, 4, 4, 4]$$$, it has $$$5$$$ minimums and $$$5$$$ maximums; $$$[1, 2, 1, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2]$$$, it has $$$3$$$ minimums and $$$10$$$ maximums; $$$[8, 8, 8]$$$, it has $$$3$$$ minimums and $$$3$$$ maximums; $$$[4, 6, 6]$$$, it has $$$1$$$ minimum and $$$2$$$ maximums; $$$[3, 4, 3]$$$, it has $$$2$$$ minimums and $$$1$$$ maximum; $$$[5, 5, 5, 5, 5, 5]$$$, it has $$$6$$$ minimums and $$$6$$$ maximums. "}, "positive_code": [{"source_code": "gets.to_i.times do\r\n l1, r1, l2, r2 = gets.split.map(&:to_i)\r\n if l1 <= r2 && r1 >= l2\r\n p [l1, l2].max\r\n else\r\n p l1 + l2\r\n end\r\nend\r\n"}, {"source_code": "gets.to_i.times do\n l1,r1,l2,r2 = gets.chomp.split.map(&:to_i)\n if (l2 > r1) or (r2= 0\n if c[i] == 0\n a = [potential, (k1 - k2).abs].min\n min += a * a\n else\n if c[i] >= potential\n c[i] -= potential\n potential = 0\n min += c[i] * c[i]\n else\n potential -= c[i]\n c[i] = 0\n end\n end\n i -= 1\nend\n\nputs min\n"}, {"source_code": "n, k1, k2 = gets.chomp.split.map(&:to_i)\n\na = gets.chomp.split.map(&:to_i)\nb = gets.chomp.split.map(&:to_i)\n\nc = Array.new(n) { |i| (a[i] - b[i]).abs }.sort\ni = c.length - 1\np c\nmin = 0\npotential = k1 + k2\n\nwhile i >= 0\n if c[i] == 0\n a = [potential, (k1 - k2).abs].min\n min += a * a\n else\n if c[i] >= potential\n c[i] -= potential\n potential = 0\n min += c[i] * c[i]\n else\n potential -= c[i]\n c[i] = 0\n end\n end\n i -= 1\nend\n\nputs min\n"}, {"source_code": "n, k1, k2 = gets.chomp.split.map(&:to_i)\n\na = gets.chomp.split.map(&:to_i)\nb = gets.chomp.split.map(&:to_i)\n\nc = Array.new(n) { |i| (a[i] - b[i]).abs }.sort\ni = c.length - 1\n\nmin = 0\npotential = k1 + k2\n\nwhile i >= 0\n if c[i] == 0\n min += potential * potential\n else\n if c[i] >= potential\n c[i] -= potential\n potential = 0\n min += c[i] * c[i]\n else\n potential -= c[i]\n c[i] = 0\n end\n end\n i -= 1\nend\n\nputs min\n"}], "src_uid": "88d54818fd8bab2f5d0bd8d95ec860db"} {"nl": {"description": "There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers.You know the direction of each particle movement — it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time.Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point.", "input_spec": "The first line contains the positive integer n (1 ≤ n ≤ 200 000) — the number of particles. The second line contains n symbols \"L\" and \"R\". If the i-th symbol equals \"L\", then the i-th particle will move to the left, otherwise the i-th symbol equals \"R\" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≤ xi ≤ 109) — the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. ", "output_spec": "In the first line print the only integer — the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. ", "sample_inputs": ["4\nRLRL\n2 4 6 10", "3\nLLR\n40 50 60"], "sample_outputs": ["1", "-1"], "notes": "NoteIn the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point."}, "positive_code": [{"source_code": "n = gets.chomp.to_i\ns = gets.chomp.to_s\na = gets.split.map(&:to_i)\n\nans = 1234567890\n\n(n-1).times do |i|\n if s[i]=='R' && s[i+1]=='L'\n ans = [ans, (a[i+1]-a[i])/2].min\n end\nend\n\nif ans==1234567890\n p -1\nelse\n p ans\nend"}, {"source_code": "gets\nstr = gets.chomp\nmas = gets.split.map(&:to_i)\n\nn = str.length\n\nans = 10000000000000\nl = false\nfor i in 0..n-1 do\n if str[i]=='R' and str[i+1]=='L' then\n l = true\n out = (mas[i+1] - mas[i])/2\n ans = out if ans > out\n end\nend\n\nputs (l==true ? \"#{ans}\": \"-1\")\n"}, {"source_code": "n = gets.chomp.to_i\nsyms = gets.chomp.chars\npoints = gets.chomp.split.map(&:to_i)\n\narr = syms.each_cons(2).with_index.select { |s, _| s.join == \"RL\" }.map do |_, idx|\n (points[idx + 1] - points[idx]) / 2\nend\n\nputs arr.empty? ? -1 : arr.min\n"}, {"source_code": "#! ruby\n# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n############################################################\ndef gs() gets.chomp end\ndef gi() gets.to_i end\ndef gss() gs.split end\ndef gis() gss.map(&:to_i) end\ndef nmapf(n,f) n.times.map{ __send__ f } end\ndef ngs(n) nmapf n,:gs end\ndef ngi(n) nmapf n,:gi end\ndef ngss(n) nmapf n,:gss end\ndef ngis(n) nmapf n,:gis end\ndef arr2d(h,w,v=0) h.times.map{[v] * w} end\ndef for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end\ndef nsum(n) n * (n + 1) / 2 end\ndef vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end\n############################################################\n\n\nn = gi\ns = gs\nx = gis\n\nl = nil\nr = nil\n\nans = nil\n\nn.times do |i|\n if s[i] == 'L'\n l = x[i]\n else\n r = x[i]\n end\n next if l.nil?\n next if r.nil?\n next if r > l\n k = (l - r) / 2\n if ans.nil?\n ans = k\n else\n ans = [ans, k].min\n end\nend\n\nif ans.nil?\n puts -1\nelse\n puts ans\nend"}, {"source_code": "def solve\n\tmin = 10 ** 10\n\tfor i in 0..$s.size - 2\n\t\tif $s[i] == 'R' and $s[i + 1] == 'L'\n\t\t\tt = ($a[i + 1] - $a[i]) / 2\n\t\t\tmin = t if t < min\n\t\tend\n\tend\n\treturn min == 10 ** 10 ? -1 : min;\nend\n\n$n = gets.to_i\n$s = gets\n$a = gets.split.map &:to_i\nputs solve"}, {"source_code": "n=gets.to_i\ns=gets.chomp\nx=gets.split.map{|e| e.to_i}\n\nans=-1\n(n-1).times{|i|\n\tif s[i]==\"R\" && s[i+1]==\"L\" then\n\t\tt1=(x[i+1]-x[i])/2\n\t\tif ans==-1 || ans>t1 then\n\t\t\tans=t1\n\t\tend\n\tend\n}\nputs ans\n"}, {"source_code": "n = gets.to_i\ndirections = gets\nnumbers = gets\n\nnumbers = numbers.split(' ')\npr = -1\nmin = 20000000000\nflag = 0\npl = -1\nnumbers.each_with_index do |r,i|\n if (directions[i] == 'R')\n pr = i\n else\n if (pr != -1)\n pl = i\n #calc magic\n delta = numbers[pl].to_i - numbers[pr].to_i\n min = delta if delta < min\n flag = 1;\n end \n end\nend\n\nputs flag == 0 ? -1 : min/2"}, {"source_code": "n = gets.chomp.to_i\nss = gets.chomp.split('')\nxs = gets.chomp.split.map(&:to_i)\n\nls = []\nrs = []\n(0...n).to_a.each do |i|\n if ss[i] == 'L'\n ls << xs[i]\n else\n rs << xs[i]\n end\nend\n\nls.sort!\nans = -1\n\nrs.each do |r|\n l = ls.bsearch { |l| r < l }\n if !l.nil?\n t = (l - r) / 2\n if ans < 0 || t < ans\n ans = t\n end\n end\nend\n\nputs ans\n\n"}, {"source_code": "n = gets.to_i\nd = gets.chomp.chars\nt = gets.split.map(&:to_i)\nans = 1e9\n(n - 1).times do |i|\n if d[i] == 'R' && d[i + 1] == 'L'\n ans = [( t[i + 1] - t[i] ) / 2, ans].min\n end\nend\nputs ans == 1e9 ? -1 : ans\n"}, {"source_code": "n = gets.to_i\nd = gets\nx = gets.split.map(&:to_i)\n\nans = nil\nlast = nil\nn.times do |i|\n if d[i] == 'L'\n ans = x[i] - last if last && (ans.nil? || x[i] - last < ans)\n else\n last = x[i]\n end\nend\n\nputs ans ? ans / 2 : -1\n"}, {"source_code": "particleNum = gets.chomp.to_i\ndirections = gets.chomp.split('')\nparticles = gets.chomp.split(' ').map(&:to_i)\n\nfoundRight = false\nposRight = 0\n\nans = -1\ncandAns = []\n\nfor i in 0...particleNum\n\tfoundRight = true if directions[i] == 'R'\n\tposRight = i if directions[i] == 'R'\n\n\tif foundRight && directions[i] == 'L'\n\t\tcandAns << particles[i] - ((particles[posRight] + particles[i]) / 2)\n\t\tfoundRight = false\n\tend\nend\n\nif candAns.length > 0\n\tputs candAns.min\nelse\n\tputs -1\nend"}], "negative_code": [{"source_code": "particleNum = gets.chomp.to_i\ndirections = gets.chomp.split('')\nparticles = gets.chomp.split(' ').map(&:to_i)\n\nfoundRight = false\nposRight = 0\n\nans = -1\n\nfor i in 0...particleNum\n\tfoundRight = true if directions[i] == 'R'\n\tposRight = i if directions[i] == 'R'\n\n\tif foundRight && directions[i] == 'L'\n\t\tans = particles[i] - ((particles[posRight] + particles[i]) / 2)\n\t\tbreak\n\tend\nend\n\nputs ans"}], "src_uid": "ff0843cbd7c3a07c7d7d0be46b03a700"} {"nl": {"description": "After celebrating the midcourse the students of one of the faculties of the Berland State University decided to conduct a vote for the best photo. They published the photos in the social network and agreed on the rules to choose a winner: the photo which gets most likes wins. If multiple photoes get most likes, the winner is the photo that gets this number first.Help guys determine the winner photo by the records of likes.", "input_spec": "The first line of the input contains a single integer n (1 ≤ n ≤ 1000) — the total likes to the published photoes. The second line contains n positive integers a1, a2, ..., an (1 ≤ ai ≤ 1 000 000), where ai is the identifier of the photo which got the i-th like.", "output_spec": "Print the identifier of the photo which won the elections.", "sample_inputs": ["5\n1 3 2 2 1", "9\n100 200 300 200 100 300 300 100 200"], "sample_outputs": ["2", "300"], "notes": "NoteIn the first test sample the photo with id 1 got two likes (first and fifth), photo with id 2 got two likes (third and fourth), and photo with id 3 got one like (second). Thus, the winner is the photo with identifier 2, as it got: more likes than the photo with id 3; as many likes as the photo with id 1, but the photo with the identifier 2 got its second like earlier. "}, "positive_code": [{"source_code": "#!/usr/bin/ruby\ngets;a=gets.split.map(&:to_i)\nb=a.group_by{|e|e}.map{|k,v|[-v.size,k]}.sort\nc=b.take_while{|s,k|s==b[0][0]}.map(&:last)\nh={}\na.reverse.each_with_index{|e,i|h[e]||=i if c.include?(e)}\np h.max_by{|k,v|v}[0]"}, {"source_code": "n = gets.to_i\na = gets.split.map{|x| x.to_i}\nh = Hash.new(0)\nx = 0\ny = -1\nfor i in 0...n\n\n\th[a[i]] += 1\n\tif h[a[i]] > x then\n\t\tx = h[a[i]]\n\t\ty = a[i]\n\tend\n\nend\n\nputs y"}, {"source_code": "n=gets.to_i\na=gets.split(\" \")\ns=Array.new(1111111,0)\nans=id=0\na.each do |v|\n\tx=v.to_i\n\ty = (s[x]+=1)\n\tif ans x[1].size\n m = x[0].size\n puts x[1][0..-m-2].inject(:+)\nend"}, {"source_code": "gets.chomp\na = gets.chomp.split(\" \").map(&:to_i)\neven = a.select(&:even?).sort\nodd = a.select(&:odd?).sort\n\nelements_sum = 0\neven_size_diff = even.size - odd.size - 1\nodd_size_diff = odd.size - even.size - 1\n\nelements_sum = even[0..even_size_diff-1].reduce(0, :+) if even_size_diff > 0 \nelements_sum = odd[0..odd_size_diff-1].reduce(0,:+) if odd_size_diff > 0 \n\nputs elements_sum\n"}, {"source_code": "\nn = gets.strip.to_i\na = gets.strip.split(\" \").map(&:to_i)\n\nodds = a.select(&:odd?)\nevens = a.select(&:even?)\n\nres = 0\ndiff_size = (odds.size - evens.size).abs\nif diff_size <= 1\n res = 0\nelse\n arr = (odds.size > evens.size) ? odds : evens\n res = arr.sort[0..(diff_size-2)].reduce(&:+)\nend\n\nputs res\n\n"}], "negative_code": [], "src_uid": "b80fed46a9e2356dad03dd3ec01523d4"} {"nl": {"description": "Your friend Ivan asked you to help him rearrange his desktop. The desktop can be represented as a rectangle matrix of size $$$n \\times m$$$ consisting of characters '.' (empty cell of the desktop) and '*' (an icon).The desktop is called good if all its icons are occupying some prefix of full columns and, possibly, the prefix of the next column (and there are no icons outside this figure). In other words, some amount of first columns will be filled with icons and, possibly, some amount of first cells of the next (after the last full column) column will be also filled with icons (and all the icons on the desktop belong to this figure). This is pretty much the same as the real life icons arrangement.In one move, you can take one icon and move it to any empty cell in the desktop.Ivan loves to add some icons to his desktop and remove them from it, so he is asking you to answer $$$q$$$ queries: what is the minimum number of moves required to make the desktop good after adding/removing one icon?Note that queries are permanent and change the state of the desktop.", "input_spec": "The first line of the input contains three integers $$$n$$$, $$$m$$$ and $$$q$$$ ($$$1 \\le n, m \\le 1000; 1 \\le q \\le 2 \\cdot 10^5$$$) — the number of rows in the desktop, the number of columns in the desktop and the number of queries, respectively. The next $$$n$$$ lines contain the description of the desktop. The $$$i$$$-th of them contains $$$m$$$ characters '.' and '*' — the description of the $$$i$$$-th row of the desktop. The next $$$q$$$ lines describe queries. The $$$i$$$-th of them contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \\le x_i \\le n; 1 \\le y_i \\le m$$$) — the position of the cell which changes its state (if this cell contained the icon before, then this icon is removed, otherwise an icon appears in this cell).", "output_spec": "Print $$$q$$$ integers. The $$$i$$$-th of them should be the minimum number of moves required to make the desktop good after applying the first $$$i$$$ queries.", "sample_inputs": ["4 4 8\n..**\n.*..\n*...\n...*\n1 3\n2 3\n3 1\n2 3\n3 4\n4 3\n2 3\n2 2", "2 5 5\n*...*\n*****\n1 3\n2 2\n1 3\n1 5\n2 3"], "sample_outputs": ["3\n4\n4\n3\n4\n5\n5\n5", "2\n3\n3\n3\n2"], "notes": null}, "positive_code": [{"source_code": "H, W, Q = gets.split.map(&:to_i)\nD = Array.new(H) { gets.chomp }\n\nicons_to_move = 0\nicon = Array.new(H) { [false] * W }\n\nicons = []\nH.times do |i|\n W.times do |j|\n if D[i][j] == ?*\n icons << (j * H + i)\n icon[i][j] = true\n end\n end\nend\n\nicons.each do |k|\n icons_to_move += 1 if k >= icons.size\nend\n\nn_icons = icons.size\n\nQ.times do\n i, j = gets.split.map { _1.to_i - 1 }\n k = j * H + i\n \n if icon[i][j]\n icons_to_move -= 1 if k >= n_icons\n icon[i][j] = false\n n_icons -= 1\n icons_to_move += 1 if icon[n_icons % H][n_icons / H]\n else\n icons_to_move -= 1 if icon[n_icons % H][n_icons / H]\n icon[i][j] = true\n n_icons += 1\n icons_to_move += 1 if (j * H + i) >= n_icons\n end\n \n puts icons_to_move\nend"}], "negative_code": [], "src_uid": "9afb205f542c0d8ba4f7fa03faa617ae"} {"nl": {"description": "A card pyramid of height $$$1$$$ is constructed by resting two cards against each other. For $$$h>1$$$, a card pyramid of height $$$h$$$ is constructed by placing a card pyramid of height $$$h-1$$$ onto a base. A base consists of $$$h$$$ pyramids of height $$$1$$$, and $$$h-1$$$ cards on top. For example, card pyramids of heights $$$1$$$, $$$2$$$, and $$$3$$$ look as follows: You start with $$$n$$$ cards and build the tallest pyramid that you can. If there are some cards remaining, you build the tallest pyramid possible with the remaining cards. You repeat this process until it is impossible to build another pyramid. In the end, how many pyramids will you have constructed?", "input_spec": "Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1\\le t\\le 1000$$$) — the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. Each test case contains a single integer $$$n$$$ ($$$1\\le n\\le 10^9$$$) — the number of cards. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^9$$$.", "output_spec": "For each test case output a single integer — the number of pyramids you will have constructed in the end.", "sample_inputs": ["5\n3\n14\n15\n24\n1"], "sample_outputs": ["1\n2\n1\n3\n0"], "notes": "NoteIn the first test, you construct a pyramid of height $$$1$$$ with $$$2$$$ cards. There is $$$1$$$ card remaining, which is not enough to build a pyramid.In the second test, you build two pyramids, each of height $$$2$$$, with no cards remaining.In the third test, you build one pyramid of height $$$3$$$, with no cards remaining.In the fourth test, you build one pyramid of height $$$3$$$ with $$$9$$$ cards remaining. Then you build a pyramid of height $$$2$$$ with $$$2$$$ cards remaining. Then you build a final pyramid of height $$$1$$$ with no cards remaining.In the fifth test, one card is not enough to build any pyramids."}, "positive_code": [{"source_code": "t = gets.to_i\n\n0.upto t-1 do\n card = gets.to_i\n count = 0\n h = 1 # height\n while card > 1 do\n h = 1\n while card > 1 do\n if card >= (2*h + (h-1))\n card -= (2*h + (h-1))\n h += 1\n if card == 0 or card == 1\n count += 1\n end\n else\n count += 1\n break\n end\n end\n end\n puts count\nend"}, {"source_code": "# coding: utf-8\n\nt = gets.to_i\n\ndef required(h)\n 2 * (h*(h+1)/2) + ((h-1)*h/2)\nend\n\ndef near_max(ar, v)\n b, e = 0, ar.length\n while b < e\n mid = (b + e)/2\n if v < ar[mid]\n if e - b == 1\n return ar[b]\n else\n e = mid\n end\n elsif v > ar[mid]\n if e - b == 1\n return ar[b]\n else\n b = mid\n end\n else\n return ar[mid]\n end\n end\nend\nprecalc = [0]\nfor i in 1..25900\n precalc << required(i)\nend\n# p precalc\n# puts near_max(precalc, 10**9)\nt.times {\n n = gets.to_i # the number of cards (1<=n<=10**9)\n # if h, required_cards = 2*(from 1 to h, sum) + (from 0 to h-1, sum)\n # h*h + h + h*h/2 - h/2\n pyramids = 0\n while n >= 2\n subtractor = near_max(precalc, n)\n\n if subtractor == 0\n break\n else\n pyramids += 1\n n -= subtractor\n end\n end\n puts pyramids\n\n}"}], "negative_code": [], "src_uid": "02062d1afe85e3639edddedceac304f4"} {"nl": {"description": "Alice and Bob are playing a game. The game involves splitting up game pieces into two teams. There are n pieces, and the i-th piece has a strength pi.The way to split up game pieces is split into several steps: First, Alice will split the pieces into two different groups A and B. This can be seen as writing the assignment of teams of a piece in an n character string, where each character is A or B. Bob will then choose an arbitrary prefix or suffix of the string, and flip each character in that suffix (i.e. change A to B and B to A). He can do this step at most once. Alice will get all the pieces marked A and Bob will get all the pieces marked B. The strength of a player is then the sum of strengths of the pieces in the group.Given Alice's initial split into two teams, help Bob determine an optimal strategy. Return the maximum strength he can achieve.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 5·105) — the number of game pieces. The second line contains n integers pi (1 ≤ pi ≤ 109) — the strength of the i-th piece. The third line contains n characters A or B — the assignment of teams after the first step (after Alice's step).", "output_spec": "Print the only integer a — the maximum strength Bob can achieve.", "sample_inputs": ["5\n1 2 3 4 5\nABABA", "5\n1 2 3 4 5\nAAAAA", "1\n1\nB"], "sample_outputs": ["11", "15", "1"], "notes": "NoteIn the first sample Bob should flip the suffix of length one.In the second sample Bob should flip the prefix or the suffix (here it is the same) of length 5.In the third sample Bob should do nothing."}, "positive_code": [{"source_code": "#!/usr/bin/ruby\nn=gets.to_i\na=gets.split.map(&:to_i)\ns=gets.chomp.bytes\nr=0\nn.times{|i|r+=a[i] if s[i]==66}\nr0=r1=r\nn.times{|i|\n\tif s[i]==65\n\t\tr0+=a[i]\n\telse\n\t\tr0-=a[i]\n\tend\n\tr=[r,r0].max\n}\n(n-1).downto(0){|i|\n\tif s[i]==65\n\t\tr1+=a[i]\n\telse\n\t\tr1-=a[i]\n\tend\n\tr=[r,r1].max\n}\np r"}, {"source_code": "n = gets.to_i\na = gets.split(' ')\ns = gets\nori = 0\nfor i in 0..n-1\n\ta[i] = a[i].to_i\n\tif s[i]=='B'\n\t\tori+=a[i]\n\t\ta[i]=-a[i]\n\tend\nend\ntot = ans = ori\nfor i in 0..n-1\n\ttot+=a[i]\n\tans = [ans, tot].max\nend\ntot = ori\ni=n-1\nwhile i>=0\n\ttot+=a[i]\n\tans = [ans, tot].max\n\ti-=1\nend\nputs ans"}, {"source_code": "n = gets.to_i\na = gets.split(' ')\ns = gets\nori = 0\nfor i in 0..n-1\n\ta[i] = a[i].to_i\n\tif s[i]=='B'\n\t\tori+=a[i]\n\t\ta[i]=-a[i]\n\tend\nend\ntot = ans = ori\na.each do |v|\n\ttot+=v\n\tans = [ans, tot].max\nend\ntot = ori\na.reverse_each do |v|\n\ttot+=v\n\tans = [ans, tot].max\nend\nputs ans"}, {"source_code": "n = gets.chomp.to_i\npieces = gets.chomp.split(\" \").map(&:to_i)\nteams = gets.chomp\n\ninit = 0\n\nn.times do |i|\n init += pieces[i] if teams[i] == \"B\"\nend\n\nret = init\n\n# left\ncurr = init\nn.times do |i|\n if teams[i] == \"A\" then\n curr += pieces[i]\n else\n curr -= pieces[i]\n end\n if curr > ret then\n ret = curr\n end\nend\n\n\n# right\ncurr = init\n(n - 1).downto(0) do |i|\n if teams[i] == \"A\" then\n curr += pieces[i]\n else\n curr -= pieces[i]\n end\n if curr > ret then\n ret = curr\n end\nend\n\nputs ret\n"}], "negative_code": [{"source_code": "n = gets.to_i\na = gets.split(' ')\ns = gets\nexit if n>400000\nori = 0\nfor i in 0..n-1\n\tori+=a[i].to_i if s[i]=='B'\nend\ntot = ans = ori\nfor i in 0..n-1\n\ttot+=a[i].to_i if s[i]=='A'\n\ttot-=a[i].to_i if s[i]=='B'\n\tans = [ans, tot].max\nend\ntot = ori\ni=n-1;\nwhile i>=0\n\ttot+=a[i].to_i if s[i]=='A'\n\ttot-=a[i].to_i if s[i]=='B'\n\tans = [ans, tot].max\n\ti-=1\nend\nputs ans"}, {"source_code": "n = gets.to_i\na = gets.split(' ')\ns = gets\nori = 0\nfor i in 0..n-1\n\ta[i] = a[i].to_i\n\tori+=a[i] if s[i]=='B'\nend\nexit if n>400000\ntot = ans = ori\nfor i in 0..n-1\n\ttot+=a[i] if s[i]=='A'\n\ttot-=a[i] if s[i]=='B'\n\tans = [ans, tot].max\nend\ntot = ori\ni=n-1\nwhile i>=0\n\ttot+=a[i] if s[i]=='A'\n\ttot-=a[i] if s[i]=='B'\n\tans = [ans, tot].max\n\ti-=1\nend\nputs ans"}, {"source_code": "n = gets.to_i\na = gets.split(' ')\ns = gets\nori = 0\nfor i in 0..n-1\n\ta[i] = a[i].to_i\n\tori+=a[i] if s[i]=='B'\nend\ntot = ans = ori\nfor i in 0..n-1\n\ttot+=a[i] if s[i]=='A'\n\ttot-=a[i] if s[i]=='B'\n\tans = [ans, tot].max\nend\nexit if n>400000\ntot = ori\ni=n-1\nwhile i>=0\n\ttot+=a[i] if s[i]=='A'\n\ttot-=a[i] if s[i]=='B'\n\tans = [ans, tot].max\n\ti-=1\nend\nputs ans"}, {"source_code": "n = gets.chomp.to_i\npieces = gets.chomp.split(\" \").map(&:to_i)\nteams = gets.chomp.split(\"\")\n\ninit = 0\n\nn.times do |i|\n init += pieces[i] if teams[i] == \"B\"\nend\nret = init\n\n# left\ncurr = init\nn.times do |i|\n break if teams[i] == \"B\"\n curr += pieces[i]\nend\nret = [ret, curr].max\n\n# right\ncurr = init\n(n - 1).downto(0) do |i|\n break if teams[i] == \"B\"\n curr += pieces[i]\nend\nret = [ret, curr].max\n\nputs ret"}], "src_uid": "d1926feaeec151f4a2f186a46a863d90"} {"nl": {"description": "Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. The park can be represented as a rectangular n × m field. The park has k spiders, each spider at time 0 is at some cell of the field. The spiders move all the time, and each spider always moves in one of the four directions (left, right, down, up). In a unit of time, a spider crawls from his cell to the side-adjacent cell in the corresponding direction. If there is no cell in the given direction, then the spider leaves the park. The spiders do not interfere with each other as they move. Specifically, one cell can have multiple spiders at the same time.Om Nom isn't yet sure where to start his walk from but he definitely wants: to start walking at time 0 at an upper row cell of the field (it is guaranteed that the cells in this row do not contain any spiders); to walk by moving down the field towards the lowest row (the walk ends when Om Nom leaves the boundaries of the park). We know that Om Nom moves by jumping. One jump takes one time unit and transports the little monster from his cell to either a side-adjacent cell on the lower row or outside the park boundaries.Each time Om Nom lands in a cell he sees all the spiders that have come to that cell at this moment of time. Om Nom wants to choose the optimal cell to start the walk from. That's why he wonders: for each possible starting cell, how many spiders will he see during the walk if he starts from this cell? Help him and calculate the required value for each possible starting cell.", "input_spec": "The first line contains three integers n, m, k (2 ≤ n, m ≤ 2000; 0 ≤ k ≤ m(n - 1)). Each of the next n lines contains m characters — the description of the park. The characters in the i-th line describe the i-th row of the park field. If the character in the line equals \".\", that means that the corresponding cell of the field is empty; otherwise, the character in the line will equal one of the four characters: \"L\" (meaning that this cell has a spider at time 0, moving left), \"R\" (a spider moving right), \"U\" (a spider moving up), \"D\" (a spider moving down). It is guaranteed that the first row doesn't contain any spiders. It is guaranteed that the description of the field contains no extra characters. It is guaranteed that at time 0 the field contains exactly k spiders.", "output_spec": "Print m integers: the j-th integer must show the number of spiders Om Nom will see if he starts his walk from the j-th cell of the first row. The cells in any row of the field are numbered from left to right.", "sample_inputs": ["3 3 4\n...\nR.L\nR.U", "2 2 2\n..\nRL", "2 2 2\n..\nLR", "3 4 8\n....\nRRLL\nUUUU", "2 2 2\n..\nUU"], "sample_outputs": ["0 2 2", "1 1", "0 0", "1 3 3 1", "0 0"], "notes": "NoteConsider the first sample. The notes below show how the spider arrangement changes on the field over time:... ... ..U ...R.L -> .*U -> L.R -> ...R.U .R. ..R ...Character \"*\" represents a cell that contains two spiders at the same time. If Om Nom starts from the first cell of the first row, he won't see any spiders. If he starts from the second cell, he will see two spiders at time 1. If he starts from the third cell, he will see two spiders: one at time 1, the other one at time 2. "}, "positive_code": [{"source_code": "$n, $m, $k = gets.split.map(&:to_i)\n\n$results = Array.new($m, 0)\n# $results = []\n\ndef spiders_indersection(line, position, direction)\n index = nil\n case direction\n when 'L'\n index = position - line\n when 'R'\n index = position + line\n when 'U'\n index = position unless line.odd?\n end\n # if index\n # $results[index] = ($results[index] || 0) + 1\n # end\n if (index && index >= 0 && index < $m)\n # puts [index, position, line].join(' ')\n $results[index] += 1\n end\nend\n\n$n.times do |i|\n gets.chars.first($m).each_with_index do |char, j|\n spiders_indersection(i, j, char)\n end\nend\n\nputs $results.first($m).join(' ')"}, {"source_code": "n,m,k = readline.strip.split.map(&:to_i)\n\ninit_field = n.times.map {\n readline.strip\n}\nres = Array.new(m, 0)\n\ninit_field.each_with_index{|line, row_idx|\n line.each_char.with_index{|cell, col_idx|\n if cell == 'L'\n col_corr = col_idx - row_idx\n res[col_corr] += 1 if col_corr >= 0\n elsif cell == 'R'\n col_corr = col_idx + row_idx\n res[col_corr] += 1 if col_corr < m\n elsif cell == 'U' && row_idx % 2 == 0\n res[col_idx] += 1\n end\n }\n}\n\nputs res.join(' ')\n\n"}], "negative_code": [{"source_code": "$n, $m, $k = gets.split.map(&:to_i)\n\n$results = Array.new(2*$m, 0)\n\ndef spiders_indersection(line, position, direction)\n # $m.times do |omnom_start_position|\n # met = case direction\n # when 'L'\n # position - line == omnom_start_position\n # when 'R'\n # omnom_start_position == line + position\n # when 'U'\n # omnom_start_position == position\n # else\n # false\n # end\n # $results[omnom_start_position] += 1 if met\n # end\n\n case direction\n when 'L'\n $results[position - line] += 1\n when 'R'\n $results[position + line] += 1\n when 'U'\n $results[position] += 1\n end\nend\n\n$n.times do |i|\n gets.chars.first($m).each_with_index do |char, j|\n spiders_indersection(i, j, char)\n end\nend\n\nputs $results.first($m).join(' ')"}, {"source_code": "$n, $m, $k = gets.split.map(&:to_i)\n\n$results = Array.new($m, 0)\n\ndef spiders_indersection(line, position, direction)\n index = -1\n case direction\n when 'L'\n index = position - line\n when 'R'\n index = position + line\n when 'U'\n index = position if line > 1\n end\n $results[index] += 1 if (index >= 0 && index < $m)\nend\n\n$n.times do |i|\n gets.chars.first($m).each_with_index do |char, j|\n spiders_indersection(i, j, char)\n end\nend\n\nputs $results.first($m).join(' ')"}, {"source_code": "$n, $m, $k = gets.split.map(&:to_i)\n\n$results = Array.new($m, 0)\n# $results = []\n\ndef spiders_indersection(line, position, direction)\n index = nil\n case direction\n when 'L'\n index = position - line\n when 'R'\n index = position + line\n when 'U'\n index = position if line > 1\n end\n # if index\n # $results[index] = ($results[index] || 0) + 1\n # end\n if (index && index >= 0)\n # puts [index, position, line].join(' ')\n # $results[index] += 1\n $results[index] = ($results[index] || 0) + 1\n end\nend\n\n$n.times do |i|\n gets.chars.first($m).each_with_index do |char, j|\n spiders_indersection(i, j, char)\n end\nend\n\nputs $results.first($m).join(' ')"}], "src_uid": "d8c89bb83592a1ff1b639f7d53056d67"} {"nl": {"description": "A lot of people in Berland hates rain, but you do not. Rain pacifies, puts your thoughts in order. By these years you have developed a good tradition — when it rains, you go on the street and stay silent for a moment, contemplate all around you, enjoy freshness, think about big deeds you have to do. Today everything had changed quietly. You went on the street with a cup contained water, your favorite drink. In a moment when you were drinking a water you noticed that the process became quite long: the cup still contained water because of rain. You decided to make a formal model of what was happening and to find if it was possible to drink all water in that situation. Thus, your cup is a cylinder with diameter equals d centimeters. Initial level of water in cup equals h centimeters from the bottom. You drink a water with a speed equals v milliliters per second. But rain goes with such speed that if you do not drink a water from the cup, the level of water increases on e centimeters per second. The process of drinking water from the cup and the addition of rain to the cup goes evenly and continuously. Find the time needed to make the cup empty or find that it will never happen. It is guaranteed that if it is possible to drink all water, it will happen not later than after 104 seconds.Note one milliliter equals to one cubic centimeter.", "input_spec": "The only line of the input contains four integer numbers d, h, v, e (1 ≤ d, h, v, e ≤ 104), where: d — the diameter of your cylindrical cup, h — the initial level of water in the cup, v — the speed of drinking process from the cup in milliliters per second, e — the growth of water because of rain if you do not drink from the cup. ", "output_spec": "If it is impossible to make the cup empty, print \"NO\" (without quotes). Otherwise print \"YES\" (without quotes) in the first line. In the second line print a real number — time in seconds needed the cup will be empty. The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 4. It is guaranteed that if the answer exists, it doesn't exceed 104.", "sample_inputs": ["1 2 3 100", "1 1 1 1"], "sample_outputs": ["NO", "YES\n3.659792366325"], "notes": "NoteIn the first example the water fills the cup faster than you can drink from it.In the second example area of the cup's bottom equals to , thus we can conclude that you decrease the level of water by centimeters per second. At the same time water level increases by 1 centimeter per second due to rain. Thus, cup will be empty in seconds."}, "positive_code": [{"source_code": "d,h,v,e=gets.split.map(&:to_f)\nv/=Math::PI*d*d/4\nputs v= v\n puts \"NO\"\nelse\n puts \"YES\"\n puts (pi * d * d / 4.0 * h) / (v - inc)\nend\n"}, {"source_code": "a=gets.chomp.split(\" \").map{|i| i.to_i}\npi=3.141592653589793\nx=(4*a[2])/((a[0]**2)*pi)\nif x>a[3]\nk=x-a[3]\ntime=a[1]/k\n\nif time<=10000\nputs\"YES\"\nputs\"#{time}\"\nelse\nputs\"NO\"\nend\n\nelse\nputs \"NO\"\nend\n"}, {"source_code": "a=gets.chomp.split(\" \").map{|i| i.to_i}\npi=3.141592653589793\nx=(4*a[2])/((a[0]**2)*pi)\nif x>a[3]\nk=x-a[3]\ntime=a[1]/k\n\nif time<=10000\nputs\"YES\"\nputs\"#{time}\"\nelse\nputs\"NO\"\nend\n\nelse\nputs \"NO\"\nend"}, {"source_code": "d,h,v,e=gets.split.map(&:to_f)\nv/=Math::PI*d*d/4\nputs v e\n\tputs \"YES\"\n\tans = h / (v / s - e)\n\tputs \"%.12f\" % ans\nelse\n\tputs \"NO\"\nend"}, {"source_code": "input = gets.chomp.split(' ').map {|i| i.to_i}\nd, h, v, e = input[0], input[1], input[2], input[3]\n\ndecreaseSpeed = (4 * v) / ((d ** 2) * Math::PI)\nans = h / (decreaseSpeed - e)\n\nif ans < 0\n\tputs 'NO'\nelse\n\tputs 'YES'\n\tputs ans\nend"}], "negative_code": [{"source_code": "input = gets.chomp.split(' ').map {|i| i.to_i}\nd, h, v, e = input[0], input[1], input[2], input[3]\n\ndecreaseSpeed = (4 * v) / ((d ** 2) * Math::PI)\nans = h / (decreaseSpeed - e)\n\nif ans < 0\n\tputs 'NO'\nelse\n\tputs ans\nend"}], "src_uid": "fc37ef81bb36f3ac07ce2c4c3ec10d98"} {"nl": {"description": "You are given $$$q$$$ queries in the following form:Given three integers $$$l_i$$$, $$$r_i$$$ and $$$d_i$$$, find minimum positive integer $$$x_i$$$ such that it is divisible by $$$d_i$$$ and it does not belong to the segment $$$[l_i, r_i]$$$.Can you answer all the queries?Recall that a number $$$x$$$ belongs to segment $$$[l, r]$$$ if $$$l \\le x \\le r$$$.", "input_spec": "The first line contains one integer $$$q$$$ ($$$1 \\le q \\le 500$$$) — the number of queries. Then $$$q$$$ lines follow, each containing a query given in the format $$$l_i$$$ $$$r_i$$$ $$$d_i$$$ ($$$1 \\le l_i \\le r_i \\le 10^9$$$, $$$1 \\le d_i \\le 10^9$$$). $$$l_i$$$, $$$r_i$$$ and $$$d_i$$$ are integers.", "output_spec": "For each query print one integer: the answer to this query.", "sample_inputs": ["5\n2 4 2\n5 10 4\n3 10 1\n1 2 3\n4 6 5"], "sample_outputs": ["6\n4\n1\n3\n10"], "notes": null}, "positive_code": [{"source_code": "gets\n$<.map{|e|l,r,d=e.split.map &:to_i;p d+(d= l && re <= r\n re += d * (1 + (r - re) / d)\n end\n re\nend\n\n# Input\nq = gets.to_i\nresult = Array.new(q)\nq.times { |i| result[i] = process(*gets.split(\" \").map { |e| e.to_i }) }\n\n# Output\nputs result\n"}, {"source_code": "gets.to_i.times {\na = gets.split(' ').map{|i|i.to_i}\n (eval('puts a[2];') && next) if a[2] 1\n for i in (1..(low - 1)) do\n if i % div == 0\n min_int = i\n break\n end\n end\n end\n\n return min_int if min_int > 0\n\n for i in ((high + 1)..MAX_NUMBER) do\n if i % div == 0\n min_int = i\n break\n end\n end\n\n return min_int\nend\n\n# Codeforces required setup\ns = gets.rstrip\n\nresult = minimum_integer s\nputs result\n\n\n\n\n\n\n"}, {"source_code": "# You are given 𝑞 queries in the following form:\n\n# Given three integers 𝑙𝑖, 𝑟𝑖 and 𝑑𝑖, find minimum\n# positive integer 𝑥𝑖 such that it is divisible by 𝑑𝑖 and\n# it does not belong to the segment [𝑙𝑖,𝑟𝑖].\n\n# Can you answer all the queries?\n\n# Recall that a number 𝑥 belongs to segment [𝑙,𝑟] if 𝑙≤𝑥≤𝑟.\n\n# Input\n# The first line contains one integer 𝑞 (1≤𝑞≤500) — the number of queries.\n\n# Then 𝑞 lines follow, each containing a query given in\n# the format 𝑙𝑖 𝑟𝑖 𝑑𝑖 (1≤𝑙𝑖≤𝑟𝑖≤109, 1≤𝑑𝑖≤109). 𝑙𝑖, 𝑟𝑖 and 𝑑𝑖 are integers.\n\n# Output\n# For each query print one integer: the answer to this query.\n\n# Example\n# Input\n# 5\n# 2 4 2\n# 5 10 4\n# 3 10 1\n# 1 2 3\n# 4 6 5\n\n# Output\n# 6\n# 4\n# 1\n# 3\n# 10\n\nMAX_NUMBER = 1_000_000_000\n\ndef minimum_integer(queries)\n queries.each do |query|\n query = query.split(' ')\n\n return 0 unless query.length == 3\n\n low = query.first.to_i\n high = query[1].to_i\n div = query.last.to_i\n\n min_int = 0\n\n if low > 1\n for i in (1..(low - 1)) do\n if i % div == 0\n min_int = i\n break\n end\n end\n end\n\n return min_int if min_int > 0\n\n for i in ((high + 1)..MAX_NUMBER) do\n if i % div == 0\n min_int = i\n break\n end\n end\n\n puts min_int.to_i\n end\nend\n\n# Codeforces required setup\nnumber_of_queries = gets.rstrip.to_i\n\nqueries = []\n\nnumber_of_queries.times do |query|\n queries << gets.rstrip\nend\n\nresult = minimum_integer queries\nputs result\n\n\n\n\n\n\n"}, {"source_code": "# You are given 𝑞 queries in the following form:\n\n# Given three integers 𝑙𝑖, 𝑟𝑖 and 𝑑𝑖, find minimum\n# positive integer 𝑥𝑖 such that it is divisible by 𝑑𝑖 and\n# it does not belong to the segment [𝑙𝑖,𝑟𝑖].\n\n# Can you answer all the queries?\n\n# Recall that a number 𝑥 belongs to segment [𝑙,𝑟] if 𝑙≤𝑥≤𝑟.\n\n# Input\n# The first line contains one integer 𝑞 (1≤𝑞≤500) — the number of queries.\n\n# Then 𝑞 lines follow, each containing a query given in\n# the format 𝑙𝑖 𝑟𝑖 𝑑𝑖 (1≤𝑙𝑖≤𝑟𝑖≤109, 1≤𝑑𝑖≤109). 𝑙𝑖, 𝑟𝑖 and 𝑑𝑖 are integers.\n\n# Output\n# For each query print one integer: the answer to this query.\n\n# Example\n# Input\n# 5\n# 2 4 2\n# 5 10 4\n# 3 10 1\n# 1 2 3\n# 4 6 5\n\n# Output\n# 6\n# 4\n# 1\n# 3\n# 10\n\nMAX_NUMBER = 1_000_000_000\n\ndef minimum_integer(queries)\n queries.each do |query|\n query = query.split(' ')\n\n return 0 unless query.length == 3\n\n low = query.first.to_i\n high = query[1].to_i\n div = query.last.to_i\n\n min_int = 0\n\n if low > 1\n for i in (1..(low - 1)) do\n if i % div == 0\n min_int = i\n break\n end\n end\n end\n\n if min_int == 0\n for i in ((high + 1)..MAX_NUMBER) do\n if i % div == 0\n min_int = i\n break\n end\n end\n end\n\n puts min_int.to_i\n end\n\n return nil\nend\n\n# Codeforces required setup\nnumber_of_queries = gets.rstrip.to_i\n\nqueries = []\n\nnumber_of_queries.times do |query|\n queries << gets.rstrip\nend\n\nresult = minimum_integer queries\nputs result\n\n\n\n\n\n\n"}, {"source_code": "# You are given 𝑞 queries in the following form:\n\n# Given three integers 𝑙𝑖, 𝑟𝑖 and 𝑑𝑖, find minimum\n# positive integer 𝑥𝑖 such that it is divisible by 𝑑𝑖 and\n# it does not belong to the segment [𝑙𝑖,𝑟𝑖].\n\n# Can you answer all the queries?\n\n# Recall that a number 𝑥 belongs to segment [𝑙,𝑟] if 𝑙≤𝑥≤𝑟.\n\n# Input\n# The first line contains one integer 𝑞 (1≤𝑞≤500) — the number of queries.\n\n# Then 𝑞 lines follow, each containing a query given in\n# the format 𝑙𝑖 𝑟𝑖 𝑑𝑖 (1≤𝑙𝑖≤𝑟𝑖≤109, 1≤𝑑𝑖≤109). 𝑙𝑖, 𝑟𝑖 and 𝑑𝑖 are integers.\n\n# Output\n# For each query print one integer: the answer to this query.\n\n# Example\n# Input\n# 5\n# 2 4 2\n# 5 10 4\n# 3 10 1\n# 1 2 3\n# 4 6 5\n\n# Output\n# 6\n# 4\n# 1\n# 3\n# 10\n\nMAX_NUMBER = 1_000_000_000\n\ndef minimum_integer(queries)\n queries.each do |query|\n query = query.split(' ')\n\n return 0 unless query.length == 3\n\n low = query.first.to_i\n high = query[1].to_i\n div = query.last.to_i\n\n min_int = 0\n\n if low > 1\n for i in (1..(low - 1)) do\n if i % div == 0\n min_int = i\n break\n end\n end\n end\n\n if min_int == 0\n for i in ((high + 1)..Float::INFINITY) do\n if i % div == 0\n min_int = i\n break\n end\n end\n end\n\n puts min_int.to_i\n end\nend\n\n# Codeforces required setup\nnumber_of_queries = gets.rstrip.to_i\n\nqueries = []\n\nnumber_of_queries.times do |query|\n queries << gets.rstrip\nend\n\nresult = minimum_integer queries\nputs result\n\n\n\n\n\n\n"}], "src_uid": "091e91352973b18040e2d57c46f2bf8a"} {"nl": {"description": "Hooray! Berl II, the king of Berland is making a knight tournament. The king has already sent the message to all knights in the kingdom and they in turn agreed to participate in this grand event.As for you, you're just a simple peasant. There's no surprise that you slept in this morning and were late for the tournament (it was a weekend, after all). Now you are really curious about the results of the tournament. This time the tournament in Berland went as follows: There are n knights participating in the tournament. Each knight was assigned his unique number — an integer from 1 to n. The tournament consisted of m fights, in the i-th fight the knights that were still in the game with numbers at least li and at most ri have fought for the right to continue taking part in the tournament. After the i-th fight among all participants of the fight only one knight won — the knight number xi, he continued participating in the tournament. Other knights left the tournament. The winner of the last (the m-th) fight (the knight number xm) became the winner of the tournament. You fished out all the information about the fights from your friends. Now for each knight you want to know the name of the knight he was conquered by. We think that the knight number b was conquered by the knight number a, if there was a fight with both of these knights present and the winner was the knight number a.Write the code that calculates for each knight, the name of the knight that beat him.", "input_spec": "The first line contains two integers n, m (2 ≤ n ≤ 3·105; 1 ≤ m ≤ 3·105) — the number of knights and the number of fights. Each of the following m lines contains three integers li, ri, xi (1 ≤ li < ri ≤ n; li ≤ xi ≤ ri) — the description of the i-th fight. It is guaranteed that the input is correct and matches the problem statement. It is guaranteed that at least two knights took part in each battle.", "output_spec": "Print n integers. If the i-th knight lost, then the i-th number should equal the number of the knight that beat the knight number i. If the i-th knight is the winner, then the i-th number must equal 0.", "sample_inputs": ["4 3\n1 2 1\n1 3 3\n1 4 4", "8 4\n3 5 4\n3 7 6\n2 8 8\n1 8 1"], "sample_outputs": ["3 1 4 0", "0 8 4 6 4 8 6 1"], "notes": "NoteConsider the first test case. Knights 1 and 2 fought the first fight and knight 1 won. Knights 1 and 3 fought the second fight and knight 3 won. The last fight was between knights 3 and 4, knight 4 won."}, "positive_code": [{"source_code": "input = lambda {\n gets.chomp.split.map(&:to_i)\n}\n\nn , m = input.call\n\nnext_i = (1..n).to_a + [-1]\nwhoKilled = [0] * (n+1)\n\nm.times do \n l , r , w = input.call\n while l <= r && l != -1\n x = next_i[l]\n if l < w\n whoKilled[l] = w if whoKilled[l] == 0\n next_i[l] = w\n elsif l > w\n whoKilled[l] = w if whoKilled[l] == 0\n next_i[l] = next_i[r]\n end\n l = x\n end\nend\n\n\nprint whoKilled[1..n].join(\" \")"}], "negative_code": [], "src_uid": "a608eda195166231d14fbeae9c8b31fa"} {"nl": {"description": "Bob watches TV every day. He always sets the volume of his TV to $$$b$$$. However, today he is angry to find out someone has changed the volume to $$$a$$$. Of course, Bob has a remote control that can change the volume.There are six buttons ($$$-5, -2, -1, +1, +2, +5$$$) on the control, which in one press can either increase or decrease the current volume by $$$1$$$, $$$2$$$, or $$$5$$$. The volume can be arbitrarily large, but can never be negative. In other words, Bob cannot press the button if it causes the volume to be lower than $$$0$$$.As Bob is so angry, he wants to change the volume to $$$b$$$ using as few button presses as possible. However, he forgets how to do such simple calculations, so he asks you for help. Write a program that given $$$a$$$ and $$$b$$$, finds the minimum number of presses to change the TV volume from $$$a$$$ to $$$b$$$.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \\le T \\le 1\\,000$$$). Then the descriptions of the test cases follow. Each test case consists of one line containing two integers $$$a$$$ and $$$b$$$ ($$$0 \\le a, b \\le 10^{9}$$$) — the current volume and Bob's desired volume, respectively.", "output_spec": "For each test case, output a single integer — the minimum number of presses to change the TV volume from $$$a$$$ to $$$b$$$. If Bob does not need to change the volume (i.e. $$$a=b$$$), then print $$$0$$$.", "sample_inputs": ["3\n4 0\n5 14\n3 9"], "sample_outputs": ["2\n3\n2"], "notes": "NoteIn the first example, Bob can press the $$$-2$$$ button twice to reach $$$0$$$. Note that Bob can not press $$$-5$$$ when the volume is $$$4$$$ since it will make the volume negative. In the second example, one of the optimal ways for Bob is to press the $$$+5$$$ twice, then press $$$-1$$$ once.In the last example, Bob can press the $$$+5$$$ once, then press $$$+1$$$. "}, "positive_code": [{"source_code": "t = gets.chomp.to_i\nt.times do |testcase|\n a,b = gets.split.map(&:to_i)\n c = (a-b).abs\n puts ( c/5 + (c%5)/2 + (c%5)%2 )\nend"}, {"source_code": "t = gets.to_i\nop = [-5, 2, -1, 1, 2, 5]\nt.times do\n a, b = gets.split.map(&:to_i)\n ans = (a-b).abs/5\n a += (a-b) < 0 ? ans*5 : -ans*5\n if (a-b).abs == 0\n elsif (a-b).abs <= 2\n ans += 1\n else\n ans += 2\n end\n puts ans\nend\n"}, {"source_code": "def lol\n\tb, a = gets.chomp.split(\" \").map { |e| e.to_i}\n\tif a > b\n\t\ta -= b\n\telse\n\t\ta = b - a\n\tend\n\tk = a/5\n\tif a%5 == 0 \n\t\tp k\n\telsif a%5 == 1\n\t\tp k + 1\n\telsif a%5 == 2\n\t\tp k + 1\n\telsif a%5 == 3\n\t\tp k + 2\n\telsif a%5 == 4\n\t\tp k + 2\n\tend\n\t\t\t\n\nend\n\nn = gets.chomp.to_i\n\nn.times do\n\tlol\nend"}], "negative_code": [], "src_uid": "ccfe798f5dc63c492ff54cf40bb40613"} {"nl": {"description": "You are given a bracket sequence $$$s$$$ of length $$$n$$$, where $$$n$$$ is even (divisible by two). The string $$$s$$$ consists of $$$\\frac{n}{2}$$$ opening brackets '(' and $$$\\frac{n}{2}$$$ closing brackets ')'.In one move, you can choose exactly one bracket and move it to the beginning of the string or to the end of the string (i.e. you choose some index $$$i$$$, remove the $$$i$$$-th character of $$$s$$$ and insert it before or after all remaining characters of $$$s$$$).Your task is to find the minimum number of moves required to obtain regular bracket sequence from $$$s$$$. It can be proved that the answer always exists under the given constraints.Recall what the regular bracket sequence is: \"()\" is regular bracket sequence; if $$$s$$$ is regular bracket sequence then \"(\" + $$$s$$$ + \")\" is regular bracket sequence; if $$$s$$$ and $$$t$$$ are regular bracket sequences then $$$s$$$ + $$$t$$$ is regular bracket sequence. For example, \"()()\", \"(())()\", \"(())\" and \"()\" are regular bracket sequences, but \")(\", \"()(\" and \")))\" are not.You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 2000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$2 \\le n \\le 50$$$) — the length of $$$s$$$. It is guaranteed that $$$n$$$ is even. The second line of the test case containg the string $$$s$$$ consisting of $$$\\frac{n}{2}$$$ opening and $$$\\frac{n}{2}$$$ closing brackets.", "output_spec": "For each test case, print the answer — the minimum number of moves required to obtain regular bracket sequence from $$$s$$$. It can be proved that the answer always exists under the given constraints.", "sample_inputs": ["4\n2\n)(\n4\n()()\n8\n())()()(\n10\n)))((((())"], "sample_outputs": ["1\n0\n1\n3"], "notes": "NoteIn the first test case of the example, it is sufficient to move the first bracket to the end of the string.In the third test case of the example, it is sufficient to move the last bracket to the beginning of the string.In the fourth test case of the example, we can choose last three openning brackets, move them to the beginning of the string and obtain \"((()))(())\"."}, "positive_code": [{"source_code": "gets.to_i.times do\n\tn = gets.to_i\n\ts = gets.chomp\n\twhile s.gsub!('()', ''); end\n\tputs s.size / 2\nend\n"}, {"source_code": "t = (gets || \"\").to_i\nt.times do\n\tcnt = 0\n\tneg = 0\n\tn = (gets || \"\").to_i\n\ts = (gets || \"\").chomp\n\n\ts.each_char do |c|\n\t\tcase c\n\t\twhen '('\n\t\t\tcnt += 1\n\t\twhen ')'\n\t\t\tcnt -= 1\n\t\tend\n\t\tif cnt < 0\n\t\t\tneg -= cnt\n\t\t\tcnt = 0\n\t\tend\n\tend\n\n\tp [neg, cnt].max\nend\n"}, {"source_code": "def run\n n = gets.chomp.to_i\n s = gets.chomp\n st = 0\n min_st = 0\n\n s.each_char do |ch|\n if ch == \"(\"\n st += 1\n else\n st -= 1\n if st < min_st\n min_st = st\n end\n end\n end\n\n puts min_st.abs\nend\n\n\ndef main\n t = gets.chomp.to_i\n\n t.times do\n run\n end\nend\n\nmain\n"}, {"source_code": "n = gets.chomp.to_i\nn.times do \n nn = gets.chomp.to_i\n arr = Array.new(nn,0)\n kakko = gets.chomp.split(\"\")\n mezirusi = 1\n count = 0\n while true\n if kakko[mezirusi-1] == \"(\" && kakko[mezirusi] == \")\"\n kakko[mezirusi-1] = \" \" \n kakko[mezirusi] = \" \"\n kakko = kakko.join(\"\").gsub(\" \", \"\")\n kakko = kakko.split(\"\")\n ookisa = kakko.size\n if kakko == []\n kakko_size = 0\n break\n end\n mezirusi = 1\n else\n mezirusi += 1\n if kakko[mezirusi] == nil\n break\n end\n end\n \n end\n kakko_size = kakko.size\n if kakko.count(\"(\") != kakko.count(\")\")\n puts \"-1\"\n next\n elsif kakko_size % 2 == 0\n puts kakko_size /2\n next\n end\nend"}, {"source_code": "n = gets.chomp.to_i\nn.times do \n nn = gets.chomp.to_i\n arr = Array.new(nn,0)\n kakko = gets.chomp.split(\"\")\n#p kakko\n mezirusi = 1\n #while\n count = 0\n #2.times do\n while true\n if kakko[mezirusi-1] == \"(\" && kakko[mezirusi] == \")\"\n kakko[mezirusi-1] = \" \" \n kakko[mezirusi] = \" \"\n kakko = kakko.join(\"\").gsub(\" \", \"\")\n #p kakko\n kakko = kakko.split(\"\")\n # p kakko\n ookisa = kakko.size\n #kakko = kakko.delte(\" \")\n #p kakko\n if kakko == []\n kakko_size = 0\n break\n end\n #mezirusi -= 1\n # if kakko[mezirusi] == nil && mezirusi >= 1\n # mezirusi -= 1\n # end\n mezirusi = 1\n else\n mezirusi += 1\n #p mezirusi\n #puts \"UU\"\n if kakko[mezirusi] == nil\n break\n end\n end\n \n end\n #p kakko.size\n #puts \"UUUUUUUUU\"\n kakko_size = kakko.size\n # p kakko\n # p kakko.count(\"(\")\n if kakko.count(\"(\") != kakko.count(\")\")\n puts \"-1\"\n next\n elsif kakko_size % 2 == 0\n puts kakko_size /2\n next\n end\n \nend"}, {"source_code": "n = gets.chomp.to_i\nn.times do \n nn = gets.chomp.to_i\n kakko = gets.chomp.split(\"\")\n mezirusi = 1\n while true\n if kakko[mezirusi-1] == \"(\" && kakko[mezirusi] == \")\"\n kakko[mezirusi-1] = \" \" \n kakko[mezirusi] = \" \"\n kakko = kakko.join(\"\").gsub(\" \", \"\")\n kakko = kakko.split(\"\")\n ookisa = kakko.size\n if kakko == []\n kakko_size = 0\n break\n end\n mezirusi = 1\n else\n mezirusi += 1\n if kakko[mezirusi] == nil\n break\n end\n end\n end\n kakko_size = kakko.size\n if kakko.count(\"(\") != kakko.count(\")\")\n puts \"-1\"\n next\n else\n puts kakko_size /2\n next\n end\nend"}, {"source_code": "t = gets.chomp.to_i\n\n\nfor i in 1..t\n n = gets.chomp.to_i\n s = gets.chomp\n \n p = 0\n c = 0\n for j in 0..n-1\n ch = s[j]\n if ch == '('\n c+=1\n else\n c-=1\n end\n\n if c < 0\n p+=1 \n c = 0\n end \n end\n puts p\nend\n"}, {"source_code": "gets.to_i.times do\n ans = 0\n n = gets.to_i\n s = gets.chomp\n s.chars.inject(0) do |state, x|\n state += 1 if x == '('\n state -= 1 if x == ')'\n if state < 0\n state += 1\n ans += 1\n end\n state\n end\n puts ans\nend\n\n"}, {"source_code": "gets.to_i.times do\n gets\n a = gets.chomp.split('')\n result = 0\n\n while !a.size.zero?\n x = a.shift\n\n case x\n when '('\n a.delete_at(a.index(')')) if a.index(')')\n when ')'\n result += 1\n end\n end\n\n puts result\nend\n"}], "negative_code": [{"source_code": "n = gets.chomp.to_i\nn.times do \n nn = gets.chomp.to_i\n arr = Array.new(nn,0)\n kakko = gets.chomp.split(\"\")\np kakko\n mezirusi = 1\n #while\n count = 0\n #2.times do\n while true\n if kakko[mezirusi-1] == \"(\" && kakko[mezirusi] == \")\"\n kakko[mezirusi-1] = \" \" \n kakko[mezirusi] = \" \"\n kakko = kakko.join(\"\").gsub(\" \", \"\")\n #p kakko\n kakko = kakko.split(\"\")\n # p kakko\n ookisa = kakko.size\n #kakko = kakko.delte(\" \")\n #p kakko\n if kakko == []\n kakko_size = 0\n break\n end\n #mezirusi -= 1\n if kakko[mezirusi] == nil && mezirusi >= 1\n mezirusi -= 1\n end\n #p mezirusi\n else\n mezirusi += 1\n #p mezirusi\n #puts \"UU\"\n if kakko[mezirusi] == nil\n break\n end\n end\n\n end\n #p kakko.size\n #puts \"UUUUUUUUU\"\n kakko_size = kakko.size\n if kakko_size % 2 == 1\n puts \"-1\"\n elsif kakko_size % 2 == 0\n puts kakko_size /2\n end\n\nend"}, {"source_code": "\nn = gets.chomp.to_i\nn.times do \n nn = gets.chomp.to_i\n arr = Array.new(nn,0)\n kakko = gets.chomp.split(\"\")\n#p kakko\n mezirusi = 1\n #while\n #count = 0\n #2.times do\n while true\n if kakko[mezirusi-1] == \"(\" && kakko[mezirusi] == \")\"\n kakko[mezirusi-1] = \" \" \n kakko[mezirusi] = \" \"\n kakko = kakko.join(\"\").gsub(\" \", \"\")\n #p kakko\n kakko = kakko.split(\"\")\n # p kakko\n ookisa = kakko.size\n #kakko = kakko.delte(\" \")\n #p kakko\n if kakko == []\n kakko_size = 0\n break\n end\n #mezirusi -= 1\n if kakko[mezirusi] == nil && mezirusi >= 1\n mezirusi -= 1\n end\n #p mezirusi\n else\n mezirusi += 1\n #p mezirusi\n #puts \"UU\"\n if kakko[mezirusi] == nil\n break\n end\n end\n \n end\n #p kakko.size\n #puts \"UUUUUUUUU\"\n kakko_size = kakko.size\n # p kakko\n # p kakko.count(\"(\")\n if kakko.count(\"(\") != kakko.count(\")\")\n puts \"-1\"\n next\n elsif kakko_size % 2 == 0\n puts kakko_size /2\n next\n end\n \nend"}, {"source_code": "n = gets.chomp.to_i\nn.times do \n nn = gets.chomp.to_i\n arr = Array.new(nn,0)\n kakko = gets.chomp.split(\"\")\n#p kakko\n mezirusi = 1\n #while\n count = 0\n #2.times do\n while true\n if kakko[mezirusi-1] == \"(\" && kakko[mezirusi] == \")\"\n kakko[mezirusi-1] = \" \" \n kakko[mezirusi] = \" \"\n kakko = kakko.join(\"\").gsub(\" \", \"\")\n #p kakko\n kakko = kakko.split(\"\")\n # p kakko\n ookisa = kakko.size\n #kakko = kakko.delte(\" \")\n #p kakko\n if kakko == []\n kakko_size = 0\n break\n end\n #mezirusi -= 1\n if kakko[mezirusi] == nil && mezirusi >= 1\n mezirusi -= 1\n end\n #p mezirusi\n else\n mezirusi += 1\n #p mezirusi\n #puts \"UU\"\n if kakko[mezirusi] == nil\n break\n end\n end\n \n end\n #p kakko.size\n #puts \"UUUUUUUUU\"\n kakko_size = kakko.size\n # p kakko\n # p kakko.count(\"(\")\n if kakko.count(\"(\") != kakko.count(\")\")\n puts \"-1\"\n next\n elsif kakko_size % 2 == 0\n puts kakko_size /2\n next\n end\n \nend"}, {"source_code": "\nn = gets.chomp.to_i\nn.times do \n nn = gets.chomp.to_i\n arr = Array.new(nn,0)\n kakko = gets.chomp.split(\"\")\n#p kakko\n mezirusi = 1\n #while\n count = 0\n #2.times do\n while true\n if kakko[mezirusi-1] == \"(\" && kakko[mezirusi] == \")\"\n kakko[mezirusi-1] = \" \" \n kakko[mezirusi] = \" \"\n kakko = kakko.join(\"\").gsub(\" \", \"\")\n #p kakko\n kakko = kakko.split(\"\")\n # p kakko\n ookisa = kakko.size\n #kakko = kakko.delte(\" \")\n #p kakko\n if kakko == []\n kakko_size = 0\n break\n end\n #mezirusi -= 1\n if kakko[mezirusi] == nil && mezirusi >= 1\n mezirusi -= 1\n end\n #p mezirusi\n else\n mezirusi += 1\n #p mezirusi\n #puts \"UU\"\n if kakko[mezirusi] == nil\n break\n end\n end\n \n end\n #p kakko.size\n #puts \"UUUUUUUUU\"\n kakko_size = kakko.size\n # p kakko\n # p kakko.count(\"(\")\n if kakko.count(\"(\") != kakko.count(\")\")\n puts \"-1\"\n elsif kakko_size % 2 == 0\n puts kakko_size /2\n end\n \nend"}, {"source_code": "n = gets.chomp.to_i\nn.times do \n nn = gets.chomp.to_i\n arr = Array.new(nn,0)\n kakko = gets.chomp.split(\"\")\n#p kakko\n mezirusi = 1\n #while\n count = 0\n #2.times do\n while true\n if kakko[mezirusi-1] == \"(\" && kakko[mezirusi] == \")\"\n kakko[mezirusi-1] = \" \" \n kakko[mezirusi] = \" \"\n kakko = kakko.join(\"\").gsub(\" \", \"\")\n #p kakko\n kakko = kakko.split(\"\")\n # p kakko\n ookisa = kakko.size\n #kakko = kakko.delte(\" \")\n p kakko\n if kakko == []\n kakko_size = 0\n break\n end\n #mezirusi -= 1\n # if kakko[mezirusi] == nil && mezirusi >= 1\n # mezirusi -= 1\n # end\n mezirusi = 1\n else\n mezirusi += 1\n #p mezirusi\n #puts \"UU\"\n if kakko[mezirusi] == nil\n break\n end\n end\n \n end\n #p kakko.size\n #puts \"UUUUUUUUU\"\n kakko_size = kakko.size\n # p kakko\n # p kakko.count(\"(\")\n if kakko.count(\"(\") != kakko.count(\")\")\n puts \"-1\"\n next\n elsif kakko_size % 2 == 0\n puts kakko_size /2\n next\n end\n \nend"}, {"source_code": "n = gets.chomp.to_i\nn.times do \n nn = gets.chomp.to_i\n arr = Array.new(nn,0)\n kakko = gets.chomp.split(\"\")\n#p kakko\n mezirusi = 1\n #while\n count = 0\n #2.times do\n while true\n if kakko[mezirusi-1] == \"(\" && kakko[mezirusi] == \")\"\n kakko[mezirusi-1] = \" \" \n kakko[mezirusi] = \" \"\n kakko = kakko.join(\"\").gsub(\" \", \"\")\n #p kakko\n kakko = kakko.split(\"\")\n # p kakko\n ookisa = kakko.size\n #kakko = kakko.delte(\" \")\n #p kakko\n if kakko == []\n kakko_size = 0\n break\n end\n #mezirusi -= 1\n if kakko[mezirusi] == nil && mezirusi >= 1\n mezirusi -= 1\n end\n #p mezirusi\n else\n mezirusi += 1\n #p mezirusi\n #puts \"UU\"\n if kakko[mezirusi] == nil\n break\n end\n end\n\n end\n #p kakko.size\n #puts \"UUUUUUUUU\"\n kakko_size = kakko.size\n if kakko_size % 2 == 1\n puts \"-1\"\n elsif kakko_size % 2 == 0\n puts kakko_size /2\n end\n\nend"}, {"source_code": "class String\n def regular_bracket_sequence?\n if self.chars.inject(0) do |state, c|\n state += 1 if c == '('\n state -= 1 if c == ')'\n break false if state < 0\n state\n end then true else false end\n end\nend\ngets.to_i.times do\n ans = 0\n n = gets.to_i\n s = gets.chomp\n while not s.regular_bracket_sequence? do\n ans += 1\n idx = s.index(')')\n s[idx, idx + 1] = ''\n s << ')'\n end\n puts ans\nend\n\n"}], "src_uid": "7a724f327c6202735661be25ef9328d2"} {"nl": {"description": "A well-known art union called \"Kalevich is Alive!\" manufactures objects d'art (pictures). The union consists of n painters who decided to organize their work as follows.Each painter uses only the color that was assigned to him. The colors are distinct for all painters. Let's assume that the first painter uses color 1, the second one uses color 2, and so on. Each picture will contain all these n colors. Adding the j-th color to the i-th picture takes the j-th painter tij units of time.Order is important everywhere, so the painters' work is ordered by the following rules: Each picture is first painted by the first painter, then by the second one, and so on. That is, after the j-th painter finishes working on the picture, it must go to the (j + 1)-th painter (if j < n); each painter works on the pictures in some order: first, he paints the first picture, then he paints the second picture and so on; each painter can simultaneously work on at most one picture. However, the painters don't need any time to have a rest; as soon as the j-th painter finishes his part of working on the picture, the picture immediately becomes available to the next painter. Given that the painters start working at time 0, find for each picture the time when it is ready for sale.", "input_spec": "The first line of the input contains integers m, n (1 ≤ m ≤ 50000, 1 ≤ n ≤ 5), where m is the number of pictures and n is the number of painters. Then follow the descriptions of the pictures, one per line. Each line contains n integers ti1, ti2, ..., tin (1 ≤ tij ≤ 1000), where tij is the time the j-th painter needs to work on the i-th picture.", "output_spec": "Print the sequence of m integers r1, r2, ..., rm, where ri is the moment when the n-th painter stopped working on the i-th picture.", "sample_inputs": ["5 1\n1\n2\n3\n4\n5", "4 2\n2 5\n3 1\n5 3\n10 1"], "sample_outputs": ["1 3 6 10 15", "7 8 13 21"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/ruby\nn,m=gets.split.map(&:to_i)\na=n.times.map{gets.split.map(&:to_i)}.transpose\n1.step(m-1){|i|a[i][0]+=a[i-1][0]}\n1.step(n-1){|j|a[0][j]+=a[0][j-1]}\n1.step(m-1){|i|1.step(n-1){|j|a[i][j]=[a[i-1][j],a[i][j-1]].map{|e|e+a[i][j]}.max}}\nputs a.last*' '"}, {"source_code": "m,n=gets.chomp.split.map(&:to_i)\nt=[0,0,0,0,0]\nm.times do\n\ta=gets.chomp.split.map(&:to_i)\n\ta.length.times {|j| (j!=0 && t[j] 0\n\t\tcalc << dp[i-1][j] if i > 0\n\t\t\n\t\tdp[i][j] = calc.max + arr[i][j]\n\tend\nend\n\nans = []\ndp.each do |a|\n\tans << a[-1]\nend\nputs ans.join(\" \")"}], "negative_code": [], "src_uid": "43f4ea06d8b8b0b6b6795a3d526c5a2d"} {"nl": {"description": "Little time is left before Berland annual football championship. Therefore the coach of team \"Losewille Rangers\" decided to resume the practice, that were indefinitely interrupted for uncertain reasons. Overall there are n players in \"Losewille Rangers\". Each player on the team has a number — a unique integer from 1 to n. To prepare for the championship, the coach Mr. Floppe decided to spend some number of practices.Mr. Floppe spent some long nights of his holiday planning how to conduct the practices. He came to a very complex practice system. Each practice consists of one game, all n players of the team take part in the game. The players are sorted into two teams in some way. In this case, the teams may have different numbers of players, but each team must have at least one player.The coach wants to be sure that after the series of the practice sessions each pair of players had at least one practice, when they played in different teams. As the players' energy is limited, the coach wants to achieve the goal in the least number of practices.Help him to schedule the practices.", "input_spec": "A single input line contains integer n (2 ≤ n ≤ 1000).", "output_spec": "In the first line print m — the minimum number of practices the coach will have to schedule. Then print the descriptions of the practices in m lines. In the i-th of those lines print fi — the number of players in the first team during the i-th practice (1 ≤ fi < n), and fi numbers from 1 to n — the numbers of players in the first team. The rest of the players will play in the second team during this practice. Separate numbers on a line with spaces. Print the numbers of the players in any order. If there are multiple optimal solutions, print any of them.", "sample_inputs": ["2", "3"], "sample_outputs": ["1\n1 1", "2\n2 1 2\n1 1"], "notes": null}, "positive_code": [{"source_code": "if ENV['USER'] != 'watashi'\n $stdin = open('input.txt')\n $> = open('output.txt', 'w')\nend\n\nn = gets.to_i\nm = 0\na = []\nwhile 1 << m < n\n a << [*1..n].select{|_| _[m] == 1}\n m += 1\nend\n\np a.size\na.each{|_|\n puts \"#{_.size} #{_ * ' '}\"\n}\n"}], "negative_code": [], "src_uid": "31a64a84b5cc4e27737eea2c14f6dbd7"} {"nl": {"description": "All techniques in the ninja world consist of hand seals. At the moment Naruto is learning a new technique, which consists of $$$n\\cdot m$$$ different seals, denoted by distinct numbers. All of them were written in an $$$n\\times m$$$ table.The table is lost now. Naruto managed to remember elements of each row from left to right, and elements of each column from top to bottom, but he doesn't remember the order of rows and columns. Please restore the table consistent with this data so that Naruto will be able to learn the new technique.", "input_spec": "The first line of the input contains the only integer $$$t$$$ ($$$1\\leq t\\leq 100\\,000$$$) denoting the number of test cases. Their descriptions follow. The first line of each test case description consists of two space-separated integers $$$n$$$ and $$$m$$$ ($$$1 \\leq n, m \\leq 500$$$) standing for the number of rows and columns in the table, respectively. All hand seals are encoded by the positive integers from $$$1$$$ to $$$n\\cdot m$$$. The following $$$n$$$ lines contain $$$m$$$ space separated integers each, denoting elements of an arbitrary row in the table left to right. The following $$$m$$$ lines contain $$$n$$$ space separated integers each, denoting elements of an arbitrary column in the table top to bottom. Sum of $$$nm$$$ over all test cases does not exceed $$$250\\,000$$$. It is guaranteed that each row occurs in the input exactly once, as well as each column. It is also guaranteed that each number from $$$1$$$ to $$$nm$$$ occurs exactly once in all rows, as well as in all columns. Finally, it is guaranteed that a table consistent with the input exists.", "output_spec": "For each test case, output $$$n$$$ lines with $$$m$$$ space-separated integers each, denoting the restored table. One can show that the answer is always unique.", "sample_inputs": ["2\n2 3\n6 5 4\n1 2 3\n1 6\n2 5\n3 4\n3 1\n2\n3\n1\n3 1 2"], "sample_outputs": ["1 2 3 \n6 5 4 \n3 \n1 \n2"], "notes": "NoteConsider the first test case. The matrix is $$$2 \\times 3$$$. You are given the rows and columns in arbitrary order.One of the rows is $$$[6, 5, 4]$$$. One of the rows is $$$[1, 2, 3]$$$.One of the columns is $$$[1, 6]$$$. One of the columns is $$$[2, 5]$$$. One of the columns is $$$[3, 4]$$$.You are to reconstruct the matrix. The answer is given in the output."}, "positive_code": [{"source_code": "t = gets.chomp.to_i\nt.times do \n n, m = gets.chomp.split(' ').map(&:to_i)\n rows = []\n n.times do\n rows << gets.chomp.split(' ').map(&:to_i)\n end\n cols = []\n m.times do\n cols << gets.chomp.split(' ').map(&:to_i)\n end\n res = Array.new(n){[]}\n col_1 = cols.find {|el| el.include? rows[0][0]}\n hsh = {} \n col_1.each_with_index do |el, ind|\n hsh[el] = ind\n end\n rows.each do |row|\n res[hsh[row[0]]] = row\n end\n res.each do |el|\n puts el.join ' '\n end\nend "}, {"source_code": "t = gets.to_i\nt.times do\n n, m = gets.split.map(&:to_i)\n rows = n.times.map { gets.split.map(&:to_i) }\n cols = m.times.map { gets.split.map(&:to_i) }\n x = rows[0][0]\n k = 0\n while cols[k].index(x).nil? do\n k += 1\n end\n n.times do |i|\n v = rows[i][0]\n pos = cols[k].index(v) + 1\n rows[i].unshift(pos)\n end\n rows.sort! do |e, f|\n e[0] <=> f[0]\n end\n n.times do |i|\n rows[i].shift\n puts rows[i].join(\" \")\n end\nend\n"}], "negative_code": [], "src_uid": "0eab9d2dd60d38f68d49f30cff918fce"} {"nl": {"description": "Two players A and B have a list of $$$n$$$ integers each. They both want to maximize the subtraction between their score and their opponent's score. In one turn, a player can either add to his score any element from his list (assuming his list is not empty), the element is removed from the list afterward. Or remove an element from his opponent's list (assuming his opponent's list is not empty).Note, that in case there are equal elements in the list only one of them will be affected in the operations above. For example, if there are elements $$$\\{1, 2, 2, 3\\}$$$ in a list and you decided to choose $$$2$$$ for the next turn, only a single instance of $$$2$$$ will be deleted (and added to the score, if necessary). The player A starts the game and the game stops when both lists are empty. Find the difference between A's score and B's score at the end of the game, if both of the players are playing optimally.Optimal play between two players means that both players choose the best possible strategy to achieve the best possible outcome for themselves. In this problem, it means that each player, each time makes a move, which maximizes the final difference between his score and his opponent's score, knowing that the opponent is doing the same.", "input_spec": "The first line of input contains an integer $$$n$$$ ($$$1 \\le n \\le 100\\,000$$$) — the sizes of the list. The second line contains $$$n$$$ integers $$$a_i$$$ ($$$1 \\le a_i \\le 10^6$$$), describing the list of the player A, who starts the game. The third line contains $$$n$$$ integers $$$b_i$$$ ($$$1 \\le b_i \\le 10^6$$$), describing the list of the player B.", "output_spec": "Output the difference between A's score and B's score ($$$A-B$$$) if both of them are playing optimally.", "sample_inputs": ["2\n1 4\n5 1", "3\n100 100 100\n100 100 100", "2\n2 1\n5 6"], "sample_outputs": ["0", "0", "-3"], "notes": "NoteIn the first example, the game could have gone as follows: A removes $$$5$$$ from B's list. B removes $$$4$$$ from A's list. A takes his $$$1$$$. B takes his $$$1$$$. Hence, A's score is $$$1$$$, B's score is $$$1$$$ and difference is $$$0$$$.There is also another optimal way of playing: A removes $$$5$$$ from B's list. B removes $$$4$$$ from A's list. A removes $$$1$$$ from B's list. B removes $$$1$$$ from A's list. The difference in the scores is still $$$0$$$.In the second example, irrespective of the moves the players make, they will end up with the same number of numbers added to their score, so the difference will be $$$0$$$."}, "positive_code": [{"source_code": "n = gets.strip.to_i\na1 = gets.strip.split.map(&:to_i)\na2 = gets.strip.split.map(&:to_i)\n\na1.sort!\na2.sort!\n\ns1=s2=0\ni1=i2=n-1\nt=0\n\nwhile i1>=0 || i2>=0 do\n if i1>=0 && i2>=0\n if a1[i1] > a2[i2]\n if t.even?\n s1 += a1[i1]\n end\n i1 -= 1\n else\n if t.odd?\n s2 += a2[i2]\n end\n i2 -= 1\n end\n else\n if i1<0\n s2 += a2[i2] if t.odd?\n i2 -= 1\n end\n if i2<0\n s1 += a1[i1] if t.even?\n i1 -= 1\n end\n end\n t += 1\nend\n\nputs s1-s2\n"}, {"source_code": "n = gets.to_i\na = gets.chomp.split(\" \").map(&:to_i).sort\nb = gets.chomp.split(\" \").map(&:to_i).sort\n\ndiff = 0\n\nturn = 1\n\n(2*n).times do\n\n if a[-1].nil?\n amax = 0\n else\n amax = a[-1]\n end\n\n if b[-1].nil?\n bmax = 0\n else\n bmax = b[-1]\n end\n=begin\n puts turn\n puts amax\n p a\n puts bmax\n p b\n puts\n=end\n if turn==1\n if amax>bmax\n diff += amax\n a.pop\n else\n b.pop\n end\n else\n if amax>bmax\n a.pop\n else\n diff -= bmax\n b.pop\n end\n end\n \n turn = 3-turn\n \nend\n\nputs diff"}], "negative_code": [], "src_uid": "d740f4ee1b18eeb74dfb253125c52762"} {"nl": {"description": "There are $$$n$$$ left boots and $$$n$$$ right boots. Each boot has a color which is denoted as a lowercase Latin letter or a question mark ('?'). Thus, you are given two strings $$$l$$$ and $$$r$$$, both of length $$$n$$$. The character $$$l_i$$$ stands for the color of the $$$i$$$-th left boot and the character $$$r_i$$$ stands for the color of the $$$i$$$-th right boot.A lowercase Latin letter denotes a specific color, but the question mark ('?') denotes an indefinite color. Two specific colors are compatible if they are exactly the same. An indefinite color is compatible with any (specific or indefinite) color.For example, the following pairs of colors are compatible: ('f', 'f'), ('?', 'z'), ('a', '?') and ('?', '?'). The following pairs of colors are not compatible: ('f', 'g') and ('a', 'z').Compute the maximum number of pairs of boots such that there is one left and one right boot in a pair and their colors are compatible.Print the maximum number of such pairs and the pairs themselves. A boot can be part of at most one pair.", "input_spec": "The first line contains $$$n$$$ ($$$1 \\le n \\le 150000$$$), denoting the number of boots for each leg (i.e. the number of left boots and the number of right boots). The second line contains the string $$$l$$$ of length $$$n$$$. It contains only lowercase Latin letters or question marks. The $$$i$$$-th character stands for the color of the $$$i$$$-th left boot. The third line contains the string $$$r$$$ of length $$$n$$$. It contains only lowercase Latin letters or question marks. The $$$i$$$-th character stands for the color of the $$$i$$$-th right boot.", "output_spec": "Print $$$k$$$ — the maximum number of compatible left-right pairs of boots, i.e. pairs consisting of one left and one right boot which have compatible colors. The following $$$k$$$ lines should contain pairs $$$a_j, b_j$$$ ($$$1 \\le a_j, b_j \\le n$$$). The $$$j$$$-th of these lines should contain the index $$$a_j$$$ of the left boot in the $$$j$$$-th pair and index $$$b_j$$$ of the right boot in the $$$j$$$-th pair. All the numbers $$$a_j$$$ should be distinct (unique), all the numbers $$$b_j$$$ should be distinct (unique). If there are many optimal answers, print any of them.", "sample_inputs": ["10\ncodeforces\ndodivthree", "7\nabaca?b\nzabbbcc", "9\nbambarbia\nhellocode", "10\ncode??????\n??????test"], "sample_outputs": ["5\n7 8\n4 9\n2 2\n9 10\n3 1", "5\n6 5\n2 3\n4 6\n7 4\n1 2", "0", "10\n6 2\n1 6\n7 3\n3 5\n4 8\n9 7\n5 1\n2 4\n10 9\n8 10"], "notes": null}, "positive_code": [{"source_code": "N = gets.to_i\nL = gets.chomp\nR = gets.chomp\nlh,rh = (0..1).map{\n h = {}\n (\"a\"..\"z\").each{ |c| h[c] = [] }\n h[\"?\"] = []\n h\n}\n(1..N).each{ |i|\n cl = L[i-1]\n lh[cl] << i\n cr = R[i-1]\n rh[cr] << i\n}\nans = []\n(\"a\"..\"z\").each{ |c|\n while lh[c].size > 0 && rh[c].size > 0 do\n ans << [lh[c].pop, rh[c].pop]\n end\n}\n(\"a\"..\"z\").each{ |c|\n while lh[c].size > 0 && rh[\"?\"].size > 0 do\n ans << [lh[c].pop, rh[\"?\"].pop]\n end\n while lh[\"?\"].size > 0 && rh[c].size > 0 do\n ans << [lh[\"?\"].pop, rh[c].pop]\n end\n}\nwhile lh[\"?\"].size > 0 && rh[\"?\"].size > 0 do\n ans << [lh[\"?\"].pop, rh[\"?\"].pop]\nend\np ans.size\nans.each{ |l,r| puts \"#{l} #{r}\" }"}, {"source_code": "n = gets.to_i\na = gets.chomp\nb = gets.chomp\nl = Hash.new{|h,k|h[k] = []}\nr = Hash.new{|h,k|h[k] = []}\nn.times do |i|\n l[a[i]].push(i+1)\n r[b[i]].push(i+1)\nend\nans = []\ns = \"abcdefghijklmnopqrstuvwxyz?\"\n26.times do |i|\n t = [l[s[i]].size,r[s[i]].size].min\n t.times do |j|\n ans << [l[s[i]][-1],r[s[i]][-1]]\n l[s[i]].pop\n r[s[i]].pop\n end\nend\n26.times do |i|\n t = [l[s[i]].size,r[s[-1]].size].min\n t.times do |j|\n ans << [l[s[i]][-1],r[s[-1]][-1]]\n l[s[i]].pop\n r[s[-1]].pop\n end\nend\n26.times do |i|\n t = [l[s[-1]].size,r[s[i]].size].min\n t.times do |j|\n ans << [l[s[-1]][-1],r[s[i]][-1]]\n l[s[-1]].pop\n r[s[i]].pop\n end\nend\nt = [l[s[-1]].size,r[s[-1]].size].min\nt.times do |i|\n ans << [l[s[-1]][i],r[s[-1]][i]]\nend\nputs ans.size\nans.each do |arr|\n puts arr.join(\" \")\nend"}, {"source_code": "def main\n n = gets.chomp.to_i\n\n left = {}\n gets.chomp.chars.each_with_index {|c, i| if left[c].nil? then left[c] = [i + 1] else left[c] << i + 1 end}\n\n right = {}\n gets.chomp.chars.each_with_index {|c, i| if right[c].nil? then right[c] = [i + 1] else right[c] << i + 1 end}\n\n pairs = []\n\n for l in left.keys\n if l != '?'\n while left.key?(l) and right.key?(l)\n pairs << [left[l].last, right[l].last]\n left[l].pop\n right[l].pop\n left.delete(l) if left[l].empty? \n right.delete(l) if right[l].empty?\n end\n end\n end\n\n for l in left.keys\n while left.key?(l) and right.key?('?')\n pairs << [left[l].last, right['?'].last]\n left[l].pop\n right['?'].pop\n left.delete(l) if left[l].empty?\n right.delete('?') if right['?'].empty?\n end\n end\n\n for r in right.keys\n while right.key?(r) and left.key?('?')\n pairs << [left['?'].last, right[r].last]\n left['?'].pop\n right[r].pop\n left.delete('?') if left['?'].empty?\n right.delete(r) if right[r].empty?\n end\n end\n\n if left.key('?')\n while left.key('?') and right.key?('?')\n pairs << [left['?'].last, right['?'].last]\n left['?'].pop\n right['?'].pop\n left.delete('?') if left['?'].empty?\n right.delete('?') if right['?'].empty?\n end \n end\n\n puts pairs.size\n for p in pairs\n puts p.join(\" \")\n end\nend\n\nmain\n"}], "negative_code": [], "src_uid": "6bf3e5a542ebce81c1e6ce7260644a3c"} {"nl": {"description": "Andrewid the Android is a galaxy-famous detective. He is now investigating a case of frauds who make fake copies of the famous Stolp's gears, puzzles that are as famous as the Rubik's cube once was.Its most important components are a button and a line of n similar gears. Each gear has n teeth containing all numbers from 0 to n - 1 in the counter-clockwise order. When you push a button, the first gear rotates clockwise, then the second gear rotates counter-clockwise, the the third gear rotates clockwise an so on.Besides, each gear has exactly one active tooth. When a gear turns, a new active tooth is the one following after the current active tooth according to the direction of the rotation. For example, if n = 5, and the active tooth is the one containing number 0, then clockwise rotation makes the tooth with number 1 active, or the counter-clockwise rotating makes the tooth number 4 active.Andrewid remembers that the real puzzle has the following property: you can push the button multiple times in such a way that in the end the numbers on the active teeth of the gears from first to last form sequence 0, 1, 2, ..., n - 1. Write a program that determines whether the given puzzle is real or fake.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 1000) — the number of gears. The second line contains n digits a1, a2, ..., an (0 ≤ ai ≤ n - 1) — the sequence of active teeth: the active tooth of the i-th gear contains number ai.", "output_spec": "In a single line print \"Yes\" (without the quotes), if the given Stolp's gears puzzle is real, and \"No\" (without the quotes) otherwise.", "sample_inputs": ["3\n1 0 0", "5\n4 2 1 4 3", "4\n0 2 3 1"], "sample_outputs": ["Yes", "Yes", "No"], "notes": "NoteIn the first sample test when you push the button for the first time, the sequence of active teeth will be 2 2 1, when you push it for the second time, you get 0 1 2."}, "positive_code": [{"source_code": "#!/usr/bin/ruby\nN=gets.to_i\nD,*A=gets.split.map(&:to_i)\nputs (1...N).all?{|i|i==(A[i-1]+[-D,D][i&1])%N} ? :Yes : :No"}, {"source_code": "def function\narr=[]\na=gets.chomp.to_i\nb=gets.chomp\narr=b.split.map(&:to_i)\nl=arr.length\nm=true\nl.times do |i|\nif arr==Array(0..arr.length-1)\nm=false\nbreak\nend\n0.upto l-1 do |i|\narr[i]=(arr[i]+1)%l if i%2==0\narr[i]=(arr[i]-1)%l if i%2!=0\nend\nend\nif m==true\nprint \"No\"\nelse\nprint \"Yes\"\nend\nend\nfunction\n"}, {"source_code": "n = gets.to_i\ngears = gets.split(' ')\ngears = gears.map{|i| i.to_i}\nshifts = n - gears.first\nstack = []\nreal = nil\ngears.each_with_index do |gear, index|\n if index%2==0 \n new_num = (gear+shifts)%n\n else\n new_num = (gear-shifts)%n\n end\n if stack.empty?\n stack.push(new_num)\n else\n if stack.last == new_num - 1\n stack.push(new_num)\n else\n real = 0\n break \n end\n end\nend\nif real == 0\n puts 'No'\nelse\n puts 'Yes'\nend\n"}, {"source_code": "n = gets.to_i\na = gets.split(\" \").map(&:to_i)\nr = (n - a[0]) % n\na.each_with_index{|x, i|\n if (x + r * (i.even? ? 1 : -1)) % n != i\n puts 'No'\n exit\n end\n}\nputs 'Yes'\n"}, {"source_code": "n = Integer(gets)\nnums = gets.chomp.split\na = []\nfor i in 0...n\n a[i] = Integer(nums[i])\nend\n\ngear = []\ngear[0] = a\nfor i in 1...n\n gear[i] = []\n for j in 0...n\n if j % 2 == 0\n if gear[i-1][j] + 1 == n\n gear[i].push(0)\n else\n gear[i].push(gear[i-1][j] + 1)\n end\n else\n if gear[i-1][j] == 0\n gear[i].push(n-1)\n else\n gear[i].push(gear[i-1][j] - 1)\n end\n end\n end\nend\npointer = 0\nfor i in 0...n\n for j in 0...n-1\n if gear[i][j + 1] > gear[i][j]\n pointer = 1\n else\n pointer = 0\n break\n end\n end\n if pointer == 1\n break\n end\nend\nif n == 1\n puts \"YES\"\nelse\n if pointer == 0\n puts \"NO\"\n else\n puts \"YES\"\n end\nend\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nv = a[0]\nn.times do |i|\n a[i] += i.odd? ? v : -v\n a = a.map{|x| x % n}\nend\nputs a == (0...n).to_a ? 'Yes' : 'No'\n"}, {"source_code": "n = gets.to_i\nm, *a = gets.split.map(&:to_i)\n(n - 1).times{|i| a[i] += i.odd? ? -m : m}\na = a.map{|x| x % n}\nputs a == (1...n).to_a ? 'Yes' : 'No'\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\n\nx = a[0]\ndir = 1\n\nfor i in (0...n)\n\ta[i] += dir * x\n\t\n\tif a[i] < 0\n\t\ta[i] += n\n\telsif a[i] >= n\n\t\ta[i] -= n\n\tend\n\t\n\tdir *= -1\nend\n\nok = true\n\nfor i in (0...n)\n\tif a[i] != i\n\t\tok = false\n\t\tbreak\n\tend\nend\n\nif ok == true\n\tputs \"Yes\"\nelse\n\tdir = -1\n\tfor i in (0...n)\n\t\ta[i] += dir * 2 * x\n\t\t\n\t\twhile a[i] < 0\n\t\t\ta[i] += n\n\t\tend\n\t\t\n\t\twhile a[i] >= n\n\t\t\ta[i] -= n\n\t\tend\n\t\n\t\tdir *= -1\n\tend\n\t\n\tok = true\n\t\n\tfor i in (0...n)\n\t\tif a[i] != i\n\t\t\tok = false\n\t\t\tbreak\n\t\tend\n\tend\n\t\n\tputs ok == true ? \"Yes\" : \"No\"\nend\n"}, {"source_code": "gearNum = gets.chomp.to_i\nactive = gets.chomp.split(' ').map { |item| item.to_i}\nflag = true\nloopCount = 0\n\ndef proceedSwitch(active = nil, gearNum)\n=begin\n\tactive.each do |item|\n\t\tprint \"#{item} \"\n\tend\n\n\tputs \"\"\n=end\n\tfor i in 0...active.length\n\t\tactive[i] += 1 if i % 2 == 0\n\t\tactive[i] -= 1 if i % 2 == 1\n\n\t\tactive[i] = 0 if active[i] == gearNum\n\t\tactive[i] = gearNum - 1 if active[i] < 0\n\tend\nend\n\nloop do\n\tproceedSwitch(active, gearNum)\n\n\tfor i in 0...active.length\n\t\tflag = false if active[i] != i\n\t\tbreak if flag == false\n\tend\n\n\tbreak if loopCount == gearNum\n\tbreak if flag == true\n\n\tloopCount += 1\n\tflag = true\nend\n\nputs \"YES\" if flag\nputs \"NO\" if !flag"}], "negative_code": [{"source_code": "def function\narr=[]\na=gets.chomp.to_i\nb=gets.chomp\narr=b.split.map(&:to_i)\nl=arr.length\nm=true\nl.times do |i|\nif arr==Array(0..arr.length-1)\nm=false\nbreak\nend\n0.upto l-1 do |i|\narr[i]=(arr[i]+1)%l if i%2==0\narr[i]=(arr[i]-1)%l if i%2!=0\nend\nend\nif m==true\np \"No\"\nelse\np \"Yes\"\nend\nend\nfunction\n"}, {"source_code": "n = gets.to_i\ngears = gets.split(' ')\ngears = gears.map{|i| i.to_i}\nshifts = n - gears.first\nstack = []\nreal = nil\ngears.each_with_index do |gear, index|\n\tif index%2==0 \n\t\tnew_num = (gear+shifts)%n\n\telse\n\t\tnew_num = (gear-shifts)%n\n\tend\n\tif new_num == 0\n\t\tstack.push(new_num)\n\telse\n\t\tif stack.last == new_num - 1\n\t\t\tstack.push(new_num)\n\t\telse\n\t\t\treal = 0\n\t\t\tbreak\t\n\t\tend\n\tend\nend\nif real == 0\n\tputs 'No'\nelse\n\tputs 'Yes'\nend\n"}, {"source_code": "n = Integer(gets)\nnums = gets.chomp.split\na = []\nfor i in 0...n\n a[i] = Integer(nums[i])\nend\n\ngear = []\ngear[0] = a\nfor i in 1...n\n gear[i] = []\n for j in 0...n\n if j % 2 == 0\n if gear[i-1][j] + 1 == n\n gear[i].push(0)\n else\n gear[i].push(gear[i-1][j] + 1)\n end\n else\n if gear[i-1][j] == 0\n gear[i].push(n-1)\n else\n gear[i].push(gear[i-1][j] - 1)\n end\n end\n end\nend\npointer = 0\nfor i in 0...n\n for j in 0...n-1\n if gear[i][j + 1] > gear[i][j]\n pointer = 1\n else\n pointer = 0\n break\n end\n end\n if pointer == 1\n break\n end\nend\nif pointer == 0\n puts \"NO\"\nelse\n puts \"YES\"\nend\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nans = false\nv = a[0]\nn.times do |i|\n a[i] += i.odd? ? v : -v\n a = a.map{|x| x % n}\n ans = true if a == (0...n).to_a\nend\nputs ans ? 'Yes' : 'No'\n"}], "src_uid": "6c65ca365352380052b0c9d693e6d161"} {"nl": {"description": "Andre has very specific tastes. Recently he started falling in love with arrays.Andre calls an nonempty array $$$b$$$ good, if sum of its elements is divisible by the length of this array. For example, array $$$[2, 3, 1]$$$ is good, as sum of its elements — $$$6$$$ — is divisible by $$$3$$$, but array $$$[1, 1, 2, 3]$$$ isn't good, as $$$7$$$ isn't divisible by $$$4$$$. Andre calls an array $$$a$$$ of length $$$n$$$ perfect if the following conditions hold: Every nonempty subarray of this array is good. For every $$$i$$$ ($$$1 \\le i \\le n$$$), $$$1 \\leq a_i \\leq 100$$$. Given a positive integer $$$n$$$, output any perfect array of length $$$n$$$. We can show that for the given constraints such an array always exists.An array $$$c$$$ is a subarray of an array $$$d$$$ if $$$c$$$ can be obtained from $$$d$$$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 100$$$). Description of the test cases follows. The first and only line of every test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 100$$$).", "output_spec": "For every test, output any perfect array of length $$$n$$$ on a separate line. ", "sample_inputs": ["3\n1\n2\n4"], "sample_outputs": ["24\n19 33\n7 37 79 49"], "notes": "NoteArray $$$[19, 33]$$$ is perfect as all $$$3$$$ its subarrays: $$$[19]$$$, $$$[33]$$$, $$$[19, 33]$$$, have sums divisible by their lengths, and therefore are good."}, "positive_code": [{"source_code": "def debug(var)\n\tp \"debug #{var}\" if ARGV[0]==\"LOCAL_DEV\"\nend\n\nQ=STDIN.gets.to_i\nQ.times do\n\tn=STDIN.gets.to_i\n\tn.times do\n\t\tprint \"1 \"\n\tend\n\tprint \"\\n\"\nend\n"}, {"source_code": "n = gets.chomp.to_i\nn.times do\n arr = []\n count = 0\n t = gets.chomp.to_i\n 1.upto(t-1) do |i|\n arr << 1\n count += 1\n end\n arr << t - count\n puts arr.join(\" \")\nend"}], "negative_code": [{"source_code": "def debug(var)\n\tp \"debug #{var}\" if ARGV[0]==\"LOCAL_DEV\"\nend\n\nQ=STDIN.gets.to_i\nQ.times do\n\tn=STDIN.gets.to_i\n\tn.times do\n\t\tprint \"1 \"\n\tend\n\tprint '\\n'\nend\n"}], "src_uid": "da2ac80c2ad6abae676d60a506eb05fc"} {"nl": {"description": "C*++ language is quite similar to C++. The similarity manifests itself in the fact that the programs written in C*++ sometimes behave unpredictably and lead to absolutely unexpected effects. For example, let's imagine an arithmetic expression in C*++ that looks like this (expression is the main term): expression ::= summand | expression + summand | expression - summand summand ::= increment | coefficient*increment increment ::= a++ | ++a coefficient ::= 0|1|2|...|1000 For example, \"5*a++-3*++a+a++\" is a valid expression in C*++.Thus, we have a sum consisting of several summands divided by signs \"+\" or \"-\". Every summand is an expression \"a++\" or \"++a\" multiplied by some integer coefficient. If the coefficient is omitted, it is suggested being equal to 1.The calculation of such sum in C*++ goes the following way. First all the summands are calculated one after another, then they are summed by the usual arithmetic rules. If the summand contains \"a++\", then during the calculation first the value of the \"a\" variable is multiplied by the coefficient, then value of \"a\" is increased by 1. If the summand contains \"++a\", then the actions on it are performed in the reverse order: first \"a\" is increased by 1, then — multiplied by the coefficient.The summands may be calculated in any order, that's why sometimes the result of the calculation is completely unpredictable! Your task is to find its largest possible value.", "input_spec": "The first input line contains an integer a ( - 1000 ≤ a ≤ 1000) — the initial value of the variable \"a\". The next line contains an expression in C*++ language of the described type. The number of the summands in the expression does not exceed 1000. It is guaranteed that the line describing the expression contains no spaces and tabulation. ", "output_spec": "Output a single number — the maximal possible value of the expression.", "sample_inputs": ["1\n5*a++-3*++a+a++", "3\na+++++a"], "sample_outputs": ["11", "8"], "notes": "NoteConsider the second example. Initially a = 3. Suppose that at first the first summand is calculated, and then the second one is. The first summand gets equal to 3, and the value of a is increased by 1. At the calculation of the second summand a is increased once more (gets equal to 5). The value of the second summand is 5, and together they give 8. If we calculate the second summand first and the first summand later, then the both summands equals to 4, and the result is 8, too."}, "positive_code": [{"source_code": "class Summand\n attr_accessor :coeff, :incr\nend\n\nclass Solution\n def initialize()\n @summands = []\n @a = 0\n end\n\n def parse_summand(s)\n match_data = /^(\\+|-)?([0-9]*)\\*?(a\\+\\+|\\+\\+a)/.match(s)\n sign = match_data[1].nil?? '+' : match_data[1]\n coeff = match_data[2].empty?? 1 : match_data[2].to_i\n coeff = -coeff if sign == '-'\n incr = match_data[3] == 'a++' ? 0 : 1 # 0 - post, 1 - pre\n\n summand = Summand.new\n summand.coeff = coeff\n summand.incr = incr\n @summands << summand\n\n s[match_data[0].size..-1]\n end\n\n def solve()\n @a = gets.to_i\n s = gets.chomp\n while not s.empty?\n s = parse_summand(s)\n end\n @summands.sort_by!{|s| s.coeff}\n\n result = 0\n a = @a\n @summands.each do |s|\n a += s.incr\n result += s.coeff * a\n a += (1 - s.incr)\n end\n puts result\n end\nend\n\nsolution = Solution.new\nsolution.solve()\n"}, {"source_code": "a = gets.to_i\ngets.scan(/([+-])?(?:(\\d*)\\*)?(a\\+\\+|\\+\\+a)/){$* << [(($1 || '+') + ($2 || '1')).to_i, $3]}\ns = 0\n$*.sort.each do |c, d|\n\tif d == 'a++'\n\t\ts += c * a\n\telse\n\t\ts += c * (a + 1)\n\tend\n\ta += 1\nend\np s\n"}, {"source_code": "a = gets.to_i\ngets.scan(/([+-])?(?:(\\d*)\\*)?(a\\+\\+|\\+\\+a)/){$* << [(($1 || '+') + ($2 || '1')).to_i, $3]}\ns = 0\n$*.sort.each do |c, d|\n\tif d == 'a++'\n\t\ts += c * a\n\telse\n\t\ts += c * (a + 1)\n\tend\n\ta += 1\nend\np s\n\n"}, {"source_code": "a = gets.to_i\ngets.scan(/([+-])?(?:(\\d*)\\*)?(a\\+\\+|\\+\\+a)/){$* << [(($1 || '+') + ($2 || '1')).to_i, $3]}\ns = 0\n$*.sort.each do |c, d|\n\tif d == 'a++'\n\t\ts += c * a\n\telse\n\t\ts += c * (a + 1)\n\tend\n\ta += 1\nend\np s\n\n"}, {"source_code": "a = gets.to_i\ngets.scan(/([+-])?(?:(\\d*)\\*)?(a\\+\\+|\\+\\+a)/){$* << [(($1 || '+') + ($2 || '1')).to_i, $3]}\ns = 0\n$*.sort.each do |c, d|\n\tif d == 'a++'\n\t\ts += c * a\n\telse\n\t\ts += c * (a + 1)\n\tend\n\ta += 1\nend\np s\n"}, {"source_code": "a = gets.to_i\ngets.scan(/([+-])?(?:(\\d*)\\*)?(a\\+\\+|\\+\\+a)/){$* << [(($1 || '+') + ($2 || '1')).to_i, $3]}\ns = 0\n$*.sort.each do |c, d|\n\tif d == 'a++'\n\t\ts += c * a\n\telse\n\t\ts += c * (a + 1)\n\tend\n\ta += 1\nend\np s\n\n"}, {"source_code": "a = gets.to_i\ngets.scan(/([+-])?(?:(\\d*)\\*)?(a\\+\\+|\\+\\+a)/){$* << [(($1 || '+') + ($2 || '1')).to_i, $3]}\ns = 0\n$*.sort.each do |c, d|\n\tif d == 'a++'\n\t\ts += c * a\n\telse\n\t\ts += c * (a + 1)\n\tend\n\ta += 1\nend\np s\n\n"}, {"source_code": "a = gets.to_i\ngets.scan(/([+-])?(?:(\\d*)\\*)?(a\\+\\+|\\+\\+a)/){$* << [(($1 || '+') + ($2 || '1')).to_i, $3]}\ns = 0\n$*.sort.each do |c, d|\n if d == 'a++'\n s += c * a\n else\n s += c * (a + 1)\n end\n a += 1\nend\np s"}, {"source_code": "a = gets.to_i\ngets.scan(/([+-])?(?:(\\d*)\\*)?(a\\+\\+|\\+\\+a)/){$* << [(($1 || '+') + ($2 || '1')).to_i, $3]}\ns = 0\n$*.sort.each do |c, d|\n\tif d == 'a++'\n\t\ts += c * a\n\telse\n\t\ts += c * (a + 1)\n\tend\n\ta += 1\nend\np s\n\n"}, {"source_code": "a = gets.to_i\ngets.scan(/([+-])?(?:(\\d*)\\*)?(a\\+\\+|\\+\\+a)/){$* << [(($1 || '+') + ($2 || '1')).to_i, $3]}\ns = 0\n$*.sort.each do |c, d|\n\tif d == 'a++'\n\t\ts += c * a\n\telse\n\t\ts += c * (a + 1)\n\tend\n\ta += 1\nend\np s\n\n"}, {"source_code": "a = gets.to_i\ngets.scan(/([+-])?(?:(\\d*)\\*)?(a\\+\\+|\\+\\+a)/){$* << [(($1 || '+') + ($2 || '1')).to_i, $3]}\ns = 0\n$*.sort.each do |c, d|\n\tif d == 'a++'\n\t\ts += c * a\n\telse\n\t\ts += c * (a + 1)\n\tend\n\ta += 1\nend\np s\n\n"}, {"source_code": "a = gets.to_i\ngets.scan(/([+-])?(?:(\\d*)\\*)?(a\\+\\+|\\+\\+a)/){$* << [(($1 || '+') + ($2 || '1')).to_i, $3]}\ns = 0\n$*.sort.each do |c, d|\n\tif d == 'a++'\n\t\ts += c * a\n\telse\n\t\ts += c * (a + 1)\n\tend\n\ta += 1\nend\np s\n\n"}], "negative_code": [], "src_uid": "766196c156234cc169206dbb7a932fc7"} {"nl": {"description": "A sequence $$$a_1, a_2, \\dots, a_n$$$ is called good if, for each element $$$a_i$$$, there exists an element $$$a_j$$$ ($$$i \\ne j$$$) such that $$$a_i+a_j$$$ is a power of two (that is, $$$2^d$$$ for some non-negative integer $$$d$$$).For example, the following sequences are good: $$$[5, 3, 11]$$$ (for example, for $$$a_1=5$$$ we can choose $$$a_2=3$$$. Note that their sum is a power of two. Similarly, such an element can be found for $$$a_2$$$ and $$$a_3$$$), $$$[1, 1, 1, 1023]$$$, $$$[7, 39, 89, 25, 89]$$$, $$$[]$$$. Note that, by definition, an empty sequence (with a length of $$$0$$$) is good.For example, the following sequences are not good: $$$[16]$$$ (for $$$a_1=16$$$, it is impossible to find another element $$$a_j$$$ such that their sum is a power of two), $$$[4, 16]$$$ (for $$$a_1=4$$$, it is impossible to find another element $$$a_j$$$ such that their sum is a power of two), $$$[1, 3, 2, 8, 8, 8]$$$ (for $$$a_3=2$$$, it is impossible to find another element $$$a_j$$$ such that their sum is a power of two). You are given a sequence $$$a_1, a_2, \\dots, a_n$$$. What is the minimum number of elements you need to remove to make it good? You can delete an arbitrary set of elements.", "input_spec": "The first line contains the integer $$$n$$$ ($$$1 \\le n \\le 120000$$$) — the length of the given sequence. The second line contains the sequence of integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$).", "output_spec": "Print the minimum number of elements needed to be removed from the given sequence in order to make it good. It is possible that you need to delete all $$$n$$$ elements, make it empty, and thus get a good sequence.", "sample_inputs": ["6\n4 7 1 5 4 9", "5\n1 2 3 4 5", "1\n16", "4\n1 1 1 1023"], "sample_outputs": ["1", "2", "1", "0"], "notes": "NoteIn the first example, it is enough to delete one element $$$a_4=5$$$. The remaining elements form the sequence $$$[4, 7, 1, 4, 9]$$$, which is good."}, "positive_code": [{"source_code": "require 'set'\n\nn = gets.chomp.to_i\na = gets.chomp.split().map(&:to_i)\n\nx = 1\nz = []\nwhile (x<= 1e10)\n\tz << x\n\tx *= 2\nend\n\nmap_a = {}\na.each do |x|\n\tmap_a[x] ||= 0\n\tmap_a[x] += 1\nend\n\nac = 0\na.each do |i|\n\tflag = 1\n\tz.each do |j|\n\t\tif ((j-i == i) && map_a[i] && (map_a[i] >= 2)) || ((j-i != i) && map_a[j-i] && (map_a[j-i] >= 1))\n\t\t\tflag = 0\n\t\tend\n\tend\n\tac += flag\nend\n\nprint ac"}, {"source_code": "POWERS = (0..31).map { |i| 2**i }\n\ndef is_good(arr)\n elem_indices = Hash.new { |h, k| h[k] = [] }\n\n arr.each_with_index do |x, i|\n elem_indices[x] << i\n end\n\n count_to_remove = 0\n\n arr.each_with_index do |x, i|\n\n good = POWERS.any? { |pow|\n complement = pow - x\n complement > 0 && elem_indices[complement].any? { |j| j != i }\n }\n\n count_to_remove += 1 unless good\n\n end\n\n count_to_remove\n\nend\n\ngets\narr = gets.split.map(&:to_i)\n\nputs(is_good(arr))"}, {"source_code": "n = gets.to_i\nary = gets.strip.split.map(&:to_i)\n\nif n==1\n puts 1\n exit\nend\n\nhsh = {}\n0.upto(n-1).each do |i|\n x = ary[i]\n if hsh[x].nil?\n hsh[x]=i\n else\n hsh[x]=-1\n end\nend\npossible = {}\nto_delete = 0\nmaxai = ((10**9) + 0)\n\n0.upto(n-1).each do |i|\n x = ary[i]\n if possible[x].nil?\n tp = 2\n while true do\n if x < tp\n rem = tp - x\n if rem > maxai\n possible[x] = false\n break\n end\n hsh_rem = hsh[rem]\n if hsh_rem!=nil && (hsh_rem==-1 || hsh_rem!=i)\n possible[x] = true\n break\n end\n end\n tp = (tp << 1)\n end\n end\n to_delete += 1 if possible[x] == false\nend\n\nputs to_delete"}], "negative_code": [{"source_code": "require 'set'\n\nn = gets.chomp.to_i\na = gets.chomp.split().map(&:to_i)\n\nx = 1\nz = []\nwhile (x<= 1e9)\n\tz << x\n\tx *= 2\nend\n\nmap_a = {}\na.each do |x|\n\tmap_a[x] ||= 0\n\tmap_a[x] += 1\nend\n\nac = 0\na.each do |i|\n\tflag = 1\n\tz.each do |j|\n\t\tif ((j-i == i) && map_a[i] && (map_a[i] >= 2)) || ((j-i != i) && map_a[j-i] && (map_a[j-i] >= 1))\n\t\t\tflag = 0\n\t\tend\n\tend\n\tac += flag\nend\n\nprint ac"}], "src_uid": "ed308777f7122ca6279b522acd3e58f9"} {"nl": {"description": "On a chessboard with a width of $$$10^9$$$ and a height of $$$10^9$$$, the rows are numbered from bottom to top from $$$1$$$ to $$$10^9$$$, and the columns are numbered from left to right from $$$1$$$ to $$$10^9$$$. Therefore, for each cell of the chessboard you can assign the coordinates $$$(x,y)$$$, where $$$x$$$ is the column number and $$$y$$$ is the row number.Every day there are fights between black and white pieces on this board. Today, the black ones won, but at what price? Only the rook survived, and it was driven into the lower left corner — a cell with coordinates $$$(1,1)$$$. But it is still happy, because the victory has been won and it's time to celebrate it! In order to do this, the rook needs to go home, namely — on the upper side of the field (that is, in any cell that is in the row with number $$$10^9$$$).Everything would have been fine, but the treacherous white figures put spells on some places of the field before the end of the game. There are two types of spells: Vertical. Each of these is defined by one number $$$x$$$. Such spells create an infinite blocking line between the columns $$$x$$$ and $$$x+1$$$. Horizontal. Each of these is defined by three numbers $$$x_1$$$, $$$x_2$$$, $$$y$$$. Such spells create a blocking segment that passes through the top side of the cells, which are in the row $$$y$$$ and in columns from $$$x_1$$$ to $$$x_2$$$ inclusive. The peculiarity of these spells is that it is impossible for a certain pair of such spells to have a common point. Note that horizontal spells can have common points with vertical spells. An example of a chessboard. Let's recall that the rook is a chess piece that in one move can move to any point that is in the same row or column with its initial position. In our task, the rook can move from the cell $$$(r_0,c_0)$$$ into the cell $$$(r_1,c_1)$$$ only under the condition that $$$r_1 = r_0$$$ or $$$c_1 = c_0$$$ and there is no blocking lines or blocking segments between these cells (For better understanding, look at the samples).Fortunately, the rook can remove spells, but for this it has to put tremendous efforts, therefore, it wants to remove the minimum possible number of spells in such way, that after this it can return home. Find this number!", "input_spec": "The first line contains two integers $$$n$$$ and $$$m$$$ ($$$0 \\le n,m \\le 10^5$$$) — the number of vertical and horizontal spells. Each of the following $$$n$$$ lines contains one integer $$$x$$$ ($$$1 \\le x < 10^9$$$) — the description of the vertical spell. It will create a blocking line between the columns of $$$x$$$ and $$$x+1$$$. Each of the following $$$m$$$ lines contains three integers $$$x_1$$$, $$$x_2$$$ and $$$y$$$ ($$$1 \\le x_{1} \\le x_{2} \\le 10^9$$$, $$$1 \\le y < 10^9$$$) — the numbers that describe the horizontal spell. It will create a blocking segment that passes through the top sides of the cells that are in the row with the number $$$y$$$, in columns from $$$x_1$$$ to $$$x_2$$$ inclusive. It is guaranteed that all spells are different, as well as the fact that for each pair of horizontal spells it is true that the segments that describe them do not have common points.", "output_spec": "In a single line print one integer — the minimum number of spells the rook needs to remove so it can get from the cell $$$(1,1)$$$ to at least one cell in the row with the number $$$10^9$$$", "sample_inputs": ["2 3\n6\n8\n1 5 6\n1 9 4\n2 4 2", "1 3\n4\n1 5 3\n1 9 4\n4 6 6", "0 2\n1 1000000000 4\n1 1000000000 2", "0 0", "2 3\n4\n6\n1 4 3\n1 5 2\n1 6 5"], "sample_outputs": ["1", "1", "2", "0", "2"], "notes": "NoteIn the first sample, in order for the rook return home, it is enough to remove the second horizontal spell. Illustration for the first sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the second horizontal spell. It also shows the path, on which the rook would be going home. In the second sample, in order for the rook to return home, it is enough to remove the only vertical spell. If we tried to remove just one of the horizontal spells, it would not allow the rook to get home, because it would be blocked from above by one of the remaining horizontal spells (either first one or second one), and to the right it would be blocked by a vertical spell. Illustration for the second sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletion of the vertical spell. It also shows the path, on which the rook would be going home. In the third sample, we have two horizontal spells that go through the whole field. These spells can not be bypassed, so we need to remove both of them. Illustration for the third sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the horizontal spells. It also shows the path, on which the rook would be going home. In the fourth sample, we have no spells, which means that we do not need to remove anything.In the fifth example, we can remove the first vertical and third horizontal spells. Illustration for the fifth sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletions. It also shows the path, on which the rook would be going home. "}, "positive_code": [{"source_code": "nm = gets.chomp.split(\" \").map(&:to_i)\nn = nm[0]\nm = nm[1]\n\nvertiarr = []\nhoriarr = []\n\nn.times {vertiarr.push gets.to_i}\nm.times {horiarr.push gets.chomp.split(\" \").map(&:to_i)}\n\nnewhoriarr = []\n\nhoriarr.each do |z|\n newhoriarr.push z if z[0]==1\nend\n\n#vertiarr.push 0\n#vertiarr.push 10**9\nvertiarr = vertiarr.sort.reverse\n\nhorihash = Hash.new(0)\ncountver = 0\n\nnewhoriarr = newhoriarr.sort_by{|x| x[1]}.reverse\n\n#p vertiarr\n#p newhoriarr\n\nnextone = 10**9\nnewhoriarr.each_with_index do |hori,i|\n break if countver >= n\n endpoint = hori[1]\n\n while(endpoint=vertiarr[n-1]\n horihash[0]+=1\n end\n end\n\nend\n\nvertiarr.each_with_index do |x,i|\n next if i==0\n if horihash[x]==0\n horihash[x] = horihash[vertiarr[i-1]]\n end\nend\n\nif n==0\n horihash[0] = newhoriarr.count{|x| x[1]>=10**9}\nelse\n horihash[0] = newhoriarr.count{|x| x[1]>=vertiarr[n-1]}\nend\n\n#p horihash\n\nmin = 1.0/0.0\nvertiarr.push 0\nvertiarr.reverse.each_with_index do |vert,i|\n ans = i+horihash[vert]\n# puts \"#{i} #{ans}\"\n if ans= n\n endpoint = hori[1]\n while(endpoint=vertiarr[n-1]\n horihash[0]+=1\n end\n end\n\nend\n\nif n==0\n horihash[0] = newhoriarr.count{|x| x[1]>=10**9}\nend\n\n#p horihash\n\nmin = 1.0/0.0\nvertiarr.push 0\nvertiarr.reverse.each_with_index do |vert,i|\n ans = i+horihash[vert]\n if ans= n\n endpoint = hori[1]\n\n while(endpoint=vertiarr[n-1]\n horihash[0]+=1\n end\n end\n\nend\n\nif n==0\n horihash[0] = newhoriarr.count{|x| x[1]>=10**9}\nelse\n horihash[0] = newhoriarr.count{|x| x[1]>=vertiarr[n-1]}\nend\n\n#p horihash\n\nmin = 1.0/0.0\nvertiarr.push 0\nvertiarr.reverse.each_with_index do |vert,i|\n ans = i+horihash[vert]\n# puts \"#{i} #{ans}\"\n if ans= n\n endpoint = hori[1]\n while(endpoint=vertiarr[n-1]\n horihash[0]+=1\n end\n end\n\nend\n\nif n==0\n horihash[0] = newhoriarr.count{|x| x[1]>=10**9}\nelse\n horihash[0] = newhoriarr.count{|x| x[1]>=vertiarr[n-1]}\nend\n\n#p horihash\n\nmin = 1.0/0.0\nvertiarr.push 0\nvertiarr.reverse.each_with_index do |vert,i|\n ans = i+horihash[vert]\n if ans 8\n ret = nil\n break\n end\n pp = s[i]\n elsif i == s.length-1\n if s[i].length > 3\n ret = nil\n break\n end\n ret << pp + \".\" + s[i]\n else\n if s[i].length > 11 || s[i].length <= 1\n ret = nil\n break\n end\n if s[i].length == 2\n ret << pp + \".\" + s[i].slice(0,1)\n pp = s[i].slice(1,1)\n elsif s[i].length == 3\n ret << pp + \".\" + s[i].slice(0,2)\n pp = s[i].slice(2,1)\n else\n ret << pp + \".\" + s[i].slice(0,3)\n pp = s[i].slice(3,s[i].length-3)\n end\n end\nend\n\nif ret == nil || ret.length == 0\n puts \"NO\"\nelse\n puts \"YES\"\n for i in 0..ret.length-1\n puts ret[i]\n end\nend\n\n\n"}, {"source_code": "s = gets.chop\na = (s + '_' * 8).split('.')\nif s[-1] != '.' && a.size > 1 && (1..8).cover?(a[0].size) && a[1..-1].all?{|_| (2..11).cover?(_.size)}\n puts :YES\n (1...a.size).each do |i|\n n = [1, a[i].size - 8].max\n puts \"#{a[i - 1]}.#{a[i].slice!(0, n)}\"\n end\nelse\n puts :NO\nend\n"}], "negative_code": [{"source_code": "s = gets.chomp.split(\".\")\nret = []\npp = \"\"\nfor i in 0..s.length-1\n if s[i].length <= 0\n ret = nil\n break\n end\n if i == 0\n if s[i].length > 8\n ret = nil\n break\n end\n pp = s[i]\n elsif i == s.length-1\n if s[i].length > 3\n ret = nil\n break\n end\n ret << pp + \".\" + s[i]\n else\n if s[i].length > 11 || s[i].length <= 1\n ret = nil\n break\n end\n if s[i].length == 2\n ret << pp + \".\" + s[i].slice(0,1)\n pp = s[i].slice(1,1)\n elsif s[i].length == 3\n ret << pp + \".\" + s[i].slice(0,2)\n pp = s[i].slice(2,1)\n else\n ret << pp + \".\" + s[i].slice(0,3)\n pp = s[i].slice(3,s[i].length-3)\n end\n end\nend\n\nif ret == nil\n puts \"NO\"\nelse\n puts \"YES\"\n for i in 0..ret.length-1\n puts ret[i]\n end\nend\n\n\n"}, {"source_code": "ss = gets.chomp\nif ss.slice(ss.length-1) == \".\"\n puts \"NO\"\n exit\nend\ns = ss.split(\".\")\nret = []\npp = \"\"\nfor i in 0..s.length-1\n if s[i].length <= 0\n ret = nil\n break\n end\n if i == 0\n if s[i].length > 8\n ret = nil\n break\n end\n pp = s[i]\n elsif i == s.length-1\n if s[i].length > 3\n ret = nil\n break\n end\n ret << pp + \".\" + s[i]\n else\n if s[i].length > 11 || s[i].length <= 1\n ret = nil\n break\n end\n if s[i].length == 2\n ret << pp + \".\" + s[i].slice(0,1)\n pp = s[i].slice(1,1)\n elsif s[i].length == 3\n ret << pp + \".\" + s[i].slice(0,2)\n pp = s[i].slice(2,1)\n else\n ret << pp + \".\" + s[i].slice(0,3)\n pp = s[i].slice(3,s[i].length-3)\n end\n end\nend\n\nif ret == nil\n puts \"NO\"\nelse\n puts \"YES\"\n for i in 0..ret.length-1\n puts ret[i]\n end\nend\n\n\n"}, {"source_code": "s = gets.chop\na = (s + '_' * 8).split('.')\nif s[0] != '.' && s[-1] != '.' && a.size > 1 && a[1..-1].all?{|_| (2..11).cover?(_.size)}\n puts :YES\n (1...a.size).each do |i|\n n = [1, a[i].size - 8].max\n puts \"#{a[i - 1]}.#{a[i].slice!(0, n)}\"\n end\nelse\n puts :NO\nend\n"}], "src_uid": "9c30697e71102ae10c55c14d9c1db006"} {"nl": {"description": "Поликарп мечтает стать программистом и фанатеет от степеней двойки. Среди двух чисел ему больше нравится то, которое делится на большую степень числа 2. По заданной последовательности целых положительных чисел a1, a2, ..., an требуется найти r — максимальную степень числа 2, на которую делится хотя бы одно из чисел последовательности. Кроме того, требуется вывести количество чисел ai, которые делятся на r.", "input_spec": "В первой строке записано целое число n (1 ≤ n ≤ 100) — длина последовательности a. Во второй строке записана последовательность целых чисел a1, a2, ..., an (1 ≤ ai ≤ 109).", "output_spec": "Выведите два числа: r — максимальную степень двойки, на которую делится хотя бы одно из чисел заданной последовательности, количество элементов последовательности, которые делятся на r. ", "sample_inputs": ["5\n80 7 16 4 48", "4\n21 5 3 33"], "sample_outputs": ["16 3", "1 4"], "notes": "ПримечаниеВ первом тестовом примере максимальная степень двойки, на которую делится хотя бы одно число, равна 16 = 24, на неё делятся числа 80, 16 и 48.Во втором тестовом примере все четыре числа нечётные, поэтому делятся только на 1 = 20. Это и будет максимальной степенью двойки для данного примера."}, "positive_code": [{"source_code": "#!/usr/bin/env ruby\nn = gets.to_i\na = gets.split.map(&:to_i)\npow2 = [1]\ni = 0\nret = 1\nret2 = 0\nwhile pow2.max < a.max do\n pow2[i] = 2**(i+1)\n i += 1\nend\nfor i in 0..pow2.length-1\n for j in 0..n-1\n if a[j] % pow2[i] == 0 and pow2[i] > ret then ret = pow2[i] end\n end\nend\nfor i in 0..n-1\n if a[i] % ret == 0 then ret2 += 1 end\nend\nprintf \"%d %d\", ret, ret2\nputs\n"}, {"source_code": "def doubler(a)\n k = 0\n while a % 2 == 0\n k += 1\n a /= 2\n end\n return k\nend\n\nn = gets.chomp.to_i\narr = gets.chomp.split(\" \")\n\n\narr.each_index do |i|\n arr[i] = arr[i].to_i\nend\n\n\nmaxD = 0\narr.each_index do |i|\n maxD = doubler(arr[i]) if i == 0\n maxD = doubler(arr[i]) if arr[i] % (2 ** maxD) == 0\n\nend\nk = 0\narr.each{ |iM|\n k += 1 if iM % (2 ** maxD) == 0\n}\n\nputs \"#{2 ** maxD} #{k}\""}], "negative_code": [], "src_uid": "c4fe684387076927e6d177b5178db746"} {"nl": {"description": "Little Vasya had n boxes with balls in the room. The boxes stood in a row and were numbered with numbers from 1 to n from left to right.Once Vasya chose one of the boxes, let's assume that its number is i, took all balls out from it (it is guaranteed that this box originally had at least one ball), and began putting balls (one at a time) to the boxes with numbers i + 1, i + 2, i + 3 and so on. If Vasya puts a ball into the box number n, then the next ball goes to box 1, the next one goes to box 2 and so on. He did it until he had no balls left in his hands. It is possible that Vasya puts multiple balls to the same box, and it is also possible that one or more balls will go to the box number i. If i = n, Vasya puts the first ball into the box number 1, then the next ball goes to box 2 and so on. For example, let's suppose that initially Vasya had four boxes, and the first box had 3 balls, the second one had 2, the third one had 5 and the fourth one had 4 balls. Then, if i = 3, then Vasya will take all five balls out of the third box and put them in the boxes with numbers: 4, 1, 2, 3, 4. After all Vasya's actions the balls will lie in the boxes as follows: in the first box there are 4 balls, 3 in the second one, 1 in the third one and 6 in the fourth one.At this point Vasya has completely forgotten the original arrangement of the balls in the boxes, but he knows how they are arranged now, and the number x — the number of the box, where he put the last of the taken out balls.He asks you to help to find the initial arrangement of the balls in the boxes.", "input_spec": "The first line of the input contains two integers n and x (2 ≤ n ≤ 105, 1 ≤ x ≤ n), that represent the number of the boxes and the index of the box that got the last ball from Vasya, correspondingly. The second line contains n space-separated integers a1, a2, ..., an, where integer ai (0 ≤ ai ≤ 109, ax ≠ 0) represents the number of balls in the box with index i after Vasya completes all the actions. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.", "output_spec": "Print n integers, where the i-th one represents the number of balls in the box number i before Vasya starts acting. Separate the numbers in the output by spaces. If there are multiple correct solutions, you are allowed to print any of them.", "sample_inputs": ["4 4\n4 3 1 6", "5 2\n3 2 0 2 7", "3 3\n2 3 1"], "sample_outputs": ["3 2 5 4", "2 1 4 1 6", "1 2 3"], "notes": null}, "positive_code": [{"source_code": "n,x=gets.split.map(&:to_i)\na=gets.split.map(&:to_i)\nm=2000000000\nk=-1\nx-=1\nfor i in 0..n-1 do\n t=(x-i+n)%n\n if m > a[t] then\n k=t\n m=a[t]\n end\nend\n\nfor i in 0..n-1 do\n a[i] = i==k ? m*n : a[i]-m\nend\nt=(n+x-k)%n\nfor i in 1..t do\n a[(k+i)%n]-=1\n a[k]+=1\nend\n\na.each do |v|\n print \"#{v} \"\nend\nputs\n"}, {"source_code": "n,x=gets.split.map(&:to_i); x-=1\na=gets.split.map(&:to_i)\n\nmin=a.min\na.map! {|i| i-min}\n\ncount=min*n\nwhile a[x]>0\n a[x]-=1\n x-=1\n count+=1\nend\n\na[x]=count\n\nputs a*' '\n\n"}, {"source_code": "n,x=gets.split.map(&:to_i); x-=1\na=gets.split.map(&:to_i)\n\nmin=a.min\na.map! {|i| i-min}\n\ncount=min*n\nwhile a[x]>0\n a[x]-=1\n x-=1\n count+=1\nend\n\na[x]=count\n\na.each {|v| print v,' '}\n"}, {"source_code": "n,x=gets.split.map(&:to_i); x-=1\na=gets.split.map(&:to_i)\n\nmin=a.min\na.map! {|i| i-min}\n\ncount=min*n\nwhile a[x]>0\n a[x]-=1\n x-=1\n count+=1\nend\n\na[x]=count\n\nputs a*' '\n"}, {"source_code": "n,x=gets.split.map(&:to_i)\nx-=1\n\na=gets.split.map(&:to_i)\n\nmin=a.min\na.length.times {|i| a[i]-=min}\n\ncount=min*n\nwhile a[x]>0\n a[x]-=1\n x-=1\n count+=1\nend\n\na[x]=count\n\na.each {|v| print \"#{v} \"}\n"}, {"source_code": "n,x=gets.split.map(&:to_i); x-=1\na=gets.split.map(&:to_i)\n\nmin=a.min\na.map! {|i| i-min}\n\ncount=min*n\nwhile a[x]>0\n a[x]-=1\n x-=1\n count+=1\nend\n\na[x]=count\n\nputs a*' '\n\n"}, {"source_code": "n,x=gets.split.map(&:to_i); x-=1\na=gets.split.map(&:to_i)\n\nmin=a.min\na.map! {|i| i-min}\n\ncount=min*n\nwhile a[x]>0\n a[x]-=1\n x-=1\n count+=1\nend\n\na[x]=count\n\nputs a*' '\n\n"}, {"source_code": "BEGIN{\ndef main()\n\tn, x = gets.split.map(&:to_i)\n\ta = gets.split.map(&:to_i)\n\tx -= 1\n\tm = a.min\n\ta.each_with_index do |v,i|\n\t\ta[i] -= m\n\tend\n\tsum = 0\n\trx = x\n\twhile a[x] != 0 do\n\t\ta[x] -= 1\n\t\tsum += 1\n\t\tx -= 1\n\tend\n\ta[x] = m * n + sum\n\ta.each do |p|\n\t\tprint p,' '\n\tend\n\tputs\nend\n1.times do\n\tmain()\nend\n}\n"}, {"source_code": "n,x=gets.split.map(&:to_i); x-=1\na=gets.split.map(&:to_i)\n\nmin=a.min\na.map! {|i| i-min}\n\ncount=min*n\nwhile a[x]>0\n a[x]-=1\n x-=1\n count+=1\nend\n\na[x]=count\n\nputs a*' '\n\n"}, {"source_code": "n,x=gets.split.map(&:to_i); x-=1\na=gets.split.map(&:to_i)\n\nmin=a.min\na.map! {|i| i-min}\n\ncount=min*n\nwhile a[x]>0\n a[x]-=1\n x-=1\n count+=1\nend\n\na[x]=count\n\nputs a*' '\n\n"}, {"source_code": "n,x=gets.split.map(&:to_i); x-=1\na=gets.split.map(&:to_i)\n\nmin=a.min\na.map! {|i| i-min}\n\ncount=min*n\nwhile a[x]>0\n a[x]-=1\n x-=1\n count+=1\nend\n\na[x]=count\n\nputs a*' '\n\n"}, {"source_code": "infile = STDIN\nn, end_index = infile.readline.split.map(&:to_i); end_index -= 1\na = infile.readline.split.map(&:to_i)\nt = a.min\na.map! { |i| i-t }\nc = 0\nwhile a[end_index] > 0 do\n a[end_index] -= 1\n end_index -= 1\n end_index = n-1 if end_index < 0\n c += 1\nend # c changes\na[end_index] = c + n*t\nputs a*' '"}, {"source_code": "infile = STDIN\nn, end_index = infile.readline.split.map(&:to_i); end_index -= 1\na = infile.readline.split.map(&:to_i)\nt = a.min\na.map! { |i| i-t }\nc = 0\nwhile a[end_index] > 0 do\n a[end_index] -= 1\n end_index -= 1\n end_index = n-1 if end_index < 0\n c += 1\nend\na[end_index] = c + n*t\nputs a*' '"}, {"source_code": "infile = STDIN\nn, end_index = infile.readline.split.map(&:to_i); end_index -= 1\na = infile.readline.split.map(&:to_i)\n\ntemp = end_index.downto(end_index-n+1).map { |i| a[i] }\nbegin_index = end_index - temp.index(temp.min)\nbegin_index = n + begin_index if begin_index < 0\nt = a[begin_index]\na.map! { |i| i-t } # n changes\nc = 0\nwhile end_index != begin_index do\n a[end_index] -= 1\n end_index -= 1\n end_index = n-1 if end_index < 0\n c += 1\nend # c changes\na[begin_index] = c + n*t\nputs a*' '"}, {"source_code": "n,x=gets.split.map(&:to_i); x-=1\na=gets.split.map(&:to_i)\n\nmin=a.min\na.map! {|i| i-min}\n\ncount=min*n\nwhile a[x]>0\n a[x]-=1\n x-=1\n count+=1\nend\n\na[x]=count\n\nputs a*' '\n\n"}, {"source_code": "n,x=gets.split.map(&:to_i); x-=1\na=gets.split.map(&:to_i)\n\nmin=a.min\na.map! {|i| i-min}\n\ncount=min*n\nwhile a[x]>0\n a[x]-=1\n x-=1\n count+=1\nend\n\na[x]=count\n\nputs a*' '\n\n"}, {"source_code": "n,x=gets.split.map(&:to_i)\na=gets.split.map(&:to_i)\nm=2000000000\nk=-1\nx-=1\nfor i in 0..n-1 do\n t=(x-i+n)%n\n if m > a[t] then\n k=t\n m=a[t]\n end\nend\n\nfor i in 0..n-1 do\n a[i] = i==k ? m*n : a[i]-m\nend\nt=(n+x-k)%n\nfor i in 1..t do\n a[(k+i)%n]-=1\n a[k]+=1\nend\n\nputs a*' '\n"}], "negative_code": [{"source_code": "n,x=gets.split.map(&:to_i)\na=gets.split.map(&:to_i)\nm=a.min\nk=a.index{|v| v==m}\nk=x-1 if a[x-1]==a[k]\n\nif n==48 && x==34 then\n for i in 20..n-1 do\n print \"#{a[i]} \"\n end\nend\n\nfor i in 0..n-1 do\n a[i] = i==k ? m*n : a[i]-m\nend\nt=(n+x-1-k)%n;\na[k]+=t\nfor i in 1..t do\n a[(i+k)%n]-=1\nend\na.each do |v|\n print \"#{v} \"\nend\nputs\n"}, {"source_code": "n,x=gets.split.map(&:to_i)\na=gets.split.map(&:to_i)\nm=a.min\nk=a.index{|v| v==m}\nif m > 0 then\n for i in 0..n-1 do\n if i==k then\n print \"#{m*n} \"\n else\n print \"#{a[i]-m} \"\n end\n end\nelse\n a[(k+1)%n]-=1\n a[k]=1;\n a.each do |v|\n print \"#{v} \"\n end\nend\nputs\n"}, {"source_code": "n,x=gets.split.map(&:to_i)\nx-=1\n\na=gets.split.map(&:to_i)\n\nmin=a.min\nstart=a.index(min)\n\nif a[start]==a[x]\n if startstart && (start+1..x)===i\n a[i]-=1\n end\n \n if xstart)\n a[i]-=1\n end\nend\n\na[start]=min*n\n\nif(x>start)\n a[start]+=x-start\nelsif(xstart && (start+1..x)===i\n a[i]-=1\n end\n \n if xstart)\n a[i]-=1\n end\nend\n\na[start]=min*n\n\nif(x>start)\n a[start]+=x-start\nelsif(xstart && (start+1..x)===i\n a[i]-=1\n end\n \n if xstart)\n a[i]-=1\n end\nend\n\na[start]=min*n\n\nif(x>start)\n a[start]+=x-start\nelsif(x 1\n\n\tfor i in 0..(indexes.count - 1)\n\n\t\tif indexes[(i+1)].to_i - indexes[i].to_i > 1\n\n\t\t\t#there is space for a new index\n\t\t\tresult = indexes[i].to_i + 1\n\t\t\tbreak;\n\n\t\tend\n\tend\n\nelse\n\n\tif indexes[0].to_i == 1\n\n\t\t#first index is not available, next availble will be first index + 1\n\t\tresult = indexes[(indexes.count - 1)].to_i + 1;\n\n\telse\n\n\t\t#first index is available\n\t\tresult = 1\n\tend\n\nend\n\nif result == -1\n\n\t#no available space was found, check for minimum value\n\n\tif indexes[0].to_i == 1\n\n\t\t#first index is one, if no space was found in the chain, then next available is max value + 1\n\t\tresult = indexes[(indexes.count - 1)].to_i + 1;\n\n\telse\n\n\t\t#first index is not equal to 1 so 1 is available\n\t\tresult = 1\n\tend\n\nend\n\n\n\nputs result\n\n"}, {"source_code": "n = -1\nindexes = []\nchars = []\n\nARGF.each_with_index do |line, i|\n\n\t#read number of indexes provided\n\tn = line if i == 0\n\n\t#read indexes\n\tchars = line.split(' ') if i == 1\n\n\nend\n\n#transfer characters to array of integers\n\nchars.each do |c|\n\tindexes.push(c.to_i)\nend\n\n#sort indexes\n\nindexes = indexes.sort\n\nresult = -1\n\nif indexes.count > 1\n\n\tfor i in 0..(indexes.count - 1)\n\n\t\tif indexes[( i+ 1)].to_i - indexes[i].to_i > 1\n\n\t\t\t#there is space for a new index\n\t\t\tresult = indexes[i].to_i + 1\n\t\t\tbreak;\n\n\t\tend\n\tend\n\nelse\n\n\tif indexes[0].to_i == 1\n\n\t\t#first index is not available, next availble will be first index + 1\n\t\tresult = indexes[(indexes.count - 1)].to_i + 1;\n\n\telse\n\n\t\t#first index is available\n\t\tresult = 1\n\tend\n\nend\n\nif result == -1\n\n\t#no available space was found, check for minimum value\n\n\tif indexes[0].to_i == 1\n\n\t\t#first index is one, if no space was found in the chain, then next available is max value + 1\n\t\tresult = indexes[(indexes.count - 1)].to_i + 1;\n\n\telse\n\n\t\t#first index is not equal to 1 so 1 is available\n\t\tresult = 1\n\tend\n\nend\n\n\n\nputs result\n\n"}, {"source_code": "n = gets.to_i\n\nnums = gets.split.map{|i| i.to_i}\n\nt = true\ni = 1\nwhile t && i <= n do\n if !nums.delete(i) then\n result = i\n t = false\n end\n i += 1\nend\n\nif t then\n result = n + 1\nend\n\nputs result"}, {"source_code": "#!/usr/bin/ruby\ngets;p ([*1..3001]-gets.split.map(&:to_i))[0]"}, {"source_code": "gets;p ((1..3002).to_a-gets.split.map(&:to_i))[0]\n"}, {"source_code": "gets;p ((1..3002).to_a-gets.split.map(&:to_i))[0]\n"}, {"source_code": "gets;p ((1..3002).to_a-gets.split.map(&:to_i))[0]"}, {"source_code": "gets;p ((1..3002).to_a-gets.split.map(&:to_i))[0]"}, {"source_code": "n=gets.to_i\na=Array.new(n+1,0)\ngets.chomp.split.map(&:to_i).each {|x| a[x]=1}\nfor i in 1..n\n\tif a[i]==0\n\t\tputs i\n\t\texit\n\tend\nend\nputs n+1\n\n"}, {"source_code": "s = (1..3001).to_a\nn = gets.to_i\na = gets.split.map{|r| r.to_i}\ns = s - a\nputs s[0]\n"}, {"source_code": "gets;p ((1..3002).to_a-gets.split.map(&:to_i))[0]\n"}, {"source_code": "gets;p ((1..3002).to_a-gets.split.map(&:to_i))[0]\n"}, {"source_code": "gets.chomp.to_i\na = gets.chomp.split(/ /).map(&:to_i)\ntests = Array.new(3001, 0)\na.each do |i|\n tests[i - 1] += 1\nend\nputs tests.index(0) + 1\n"}, {"source_code": "gets;p ((1..3002).to_a-gets.split.map(&:to_i))[0]"}, {"source_code": "gets;p ((1..3002).to_a-gets.split.map(&:to_i))[0]"}, {"source_code": "gets\np ((1..1e5).to_a-gets.split.map(&:to_i)).min\n"}, {"source_code": "def main()\n\tn=gets.chomp.to_i\n\tstin=gets.chomp.split(\" \",n)\n\tstin=stin.map{|i| i.to_i}\n\tstin=stin.sort()\n\tj=1;f=0;\n\tfor i in 0...(stin.length)\n\t\tif stin[i]==j\t\n\t\t\tj+=1;\n\t\telse\n\t\t\tf=1;\n\t\t\tputs j\n\t\t\tbreak;\n\t\tend\n\tend\n\tif f==0\n\t\tputs j\n\tend\nend\nmain\n"}, {"source_code": "gets;p ((1..3002).to_a-gets.split.map(&:to_i))[0]\n"}, {"source_code": "n = gets.to_i\na = gets.split(\" \").map(&:to_i)\na.sort!\nans = nil\n\na.each_with_index do |x,i|\n\tif x != i + 1\n\t\tans = i + 1\n\t\tbreak\n\tend\nend\n\nans = a[-1] + 1 unless ans\nputs ans\n"}, {"source_code": "gets\nputs ((1..3001).to_a - gets.chomp.split.map(&:to_i))[0]"}, {"source_code": "gets\np ((1..3001).to_a - gets.split.map(&:to_i)).min"}, {"source_code": "gets;p ((1..3002).to_a-gets.split.map(&:to_i))[0]\n"}, {"source_code": "gets;p ((1..3002).to_a-gets.split.map(&:to_i))[0]\n"}, {"source_code": "gets;p ((1..3002).to_a-gets.split.map(&:to_i))[0]\n"}], "negative_code": [{"source_code": "n = -1\nindexes = []\n\nARGF.each_with_index do |line, i|\n\n\t#read number of indexes provided\n\tn = line if i == 0\n\n\t#read indexes\n\tindexes = line.split(' ') if i == 1\n\n\nend\n\n#sort indexes\nindexes = indexes.sort\n\nresult = -1\n\nif indexes.count > 1\n\n\t0.upto(indexes.count-1) do |i|\n\n\t\tif indexes[i+1].to_i - indexes[i].to_i > 1\n\n\t\t\t#there is space for a new index\n\t\t\tresult = indexes[i].to_i + 1\n\t\t\tbreak;\n\n\t\tend\n\tend\n\nelse\n\n\tif indexes[0].to_i > 1\n\n\t\t#first index is available\n\t\tresult = 1\n\n\telse\n\n\t\t#first element is equal to 1, assign next to 2\n\t\tresult = 2\n\tend\n\nend\n\nif result == -1\n\n\t#no available space was found, check for minimum value\n\n\tif indexes[0].to_i == 1\n\n\t\t#first index is one, if no space was found in the chain, then next available is max value + 1\n\t\tresult = indexes[indexes.count -1].to_i + 1;\n\n\telse\n\n\t\t#first index is not equal to 1 so 1 is available\n\t\tresult = 1\n\tend\n\nend\n\n\nputs result\n\n"}, {"source_code": "n = -1\nindexes = []\n\nARGF.each_with_index do |line, i|\n\n\t#read number of indexes provided\n\tn = line if i == 0\n\n\t#read indexes\n\tindexes = line.split(' ') if i == 1\n\n\nend\n\n#sort indexes\nindexes = indexes.sort\n\nresult = -1\n\nif indexes.count > 1\n\n\t0.upto(indexes.count-1) do |i|\n\n\t\tif indexes[i+1].to_i - indexes[i].to_i > 1\n\n\t\t\t#there is space for a new index\n\t\t\tresult = indexes[i].to_i + 1\n\t\t\tbreak;\n\n\t\tend\n\tend\n\nelse\n\n\tif indexes[0].to_i == 1\n\n\t\t#first index is not available, next availble will be first index + 1\n\t\tresult = indexes[indexes.count -1].to_i + 1;\n\n\telse\n\n\t\t#first index is available\n\t\tresult = 1\n\tend\n\nend\n\nif result == -1\n\n\t#no available space was found, check for minimum value\n\n\tif indexes[0].to_i == 1\n\n\t\t#first index is one, if no space was found in the chain, then next available is max value + 1\n\t\tresult = indexes[indexes.count -1].to_i + 1;\n\n\telse\n\n\t\t#first index is not equal to 1 so 1 is available\n\t\tresult = 1\n\tend\n\nend\n\n\nputs result\n\n"}, {"source_code": "n = -1\nindexes = []\n\nARGF.each_with_index do |line, i|\n\n\t#read number of indexes provided\n\tn = line if i == 0\n\n\t#read indexes\n\tindexes = line.split(' ') if i == 1\n\n\nend\n\n#sort indexes\nindexes = indexes.sort\n\nresult = -1\n\nif indexes.count > 1\n\n\t0.upto(indexes.count) do |i|\n\n\t\tif indexes[i+1].to_i - indexes[i].to_i > 1\n\n\t\t\t#there is space for a new index\n\t\t\tresult = indexes[i].to_i + 1\n\t\t\tbreak;\n\n\t\tend\n\tend\n\nelse\n\n\tif indexes[0].to_i == 1\n\n\t\t#first index is not available, next availble will be first index + 1\n\t\tresult = indexes[indexes.count -1].to_i + 1;\n\n\telse\n\n\t\t#first index is available\n\t\tresult = 1\n\tend\n\nend\n\nif result == -1\n\n\t#no available space was found, check for minimum value\n\n\tif indexes[0].to_i == 1\n\n\t\t#first index is one, if no space was found in the chain, then next available is max value + 1\n\t\tresult = indexes[indexes.count -1].to_i + 1;\n\n\telse\n\n\t\t#first index is not equal to 1 so 1 is available\n\t\tresult = 1\n\tend\n\nend\n\n\nputs result\n\n"}, {"source_code": "n = -1\nindexes = []\n\nARGF.each_with_index do |line, i|\n\n\t#read number of indexes provided\n\tn = line if i == 0\n\n\t#read indexes\n\tindexes = line.split(' ') if i == 1\n\n\nend\n\n#sort indexes\nindexes = indexes.sort\n\nresult = -1\n\nif indexes.count > 1\n\n\t0.upto((indexes.count-1)) do |i|\n\n\t\tif indexes[(i+1)].to_i - indexes[i].to_i > 1\n\n\t\t\t#there is space for a new index\n\t\t\tresult = indexes[i].to_i + 1\n\t\t\tbreak;\n\n\t\tend\n\tend\n\nelse\n\n\tif indexes[0].to_i == 1\n\n\t\t#first index is not available, next availble will be first index + 1\n\t\tresult = indexes[(indexes.count - 1)].to_i + 1;\n\n\telse\n\n\t\t#first index is available\n\t\tresult = 1\n\tend\n\nend\n\nif result == -1\n\n\t#no available space was found, check for minimum value\n\n\tif indexes[0].to_i == 1\n\n\t\t#first index is one, if no space was found in the chain, then next available is max value + 1\n\t\tresult = indexes[(indexes.count - 1)].to_i + 1;\n\n\telse\n\n\t\t#first index is not equal to 1 so 1 is available\n\t\tresult = 1\n\tend\n\nend\n\n\n\nputs result\n\n"}, {"source_code": "gets;p ((1..3000).to_a-gets.split.map(&:to_i))[0]"}, {"source_code": "n=gets.to_i\na=Array.new(n+1,0)\ngets.chomp.split.map(&:to_i).each {|x| a[x]=1}\nprint a\nputs\nfor i in 1..n\n\tif a[i]==0\n\t\tputs i\n\t\texit\n\tend\nend\nputs n+1\n\n"}, {"source_code": "n=gets.to_i\nb=Array.new(n,1)\nn.times { |i| b[i]=i+1}\na=gets.chomp.split.map(&:to_i).sort\nn.times do |i|\n\tif a[i]!=b[i]\n\t\tputs b[i]\n\t\tbreak\n\tend\nend\n\n"}, {"source_code": "s = (1..3000).to_a\nn = gets.to_i\na = gets.split.map{|r| r.to_i}\ns = s - a\nputs s[0]\n"}, {"source_code": "def main()\n\tn=gets.chomp.to_i\n\tstin=gets.chomp.split(\" \",n)\n\tstin=stin.map{|i| i.to_i}\n\tstin=stin.sort()\n\tj=1;\n\tfor i in 0...(stin.length)\n\t\tif stin[i]==j\t\n\t\t\tj+=1;\n\t\telse\n\t\t\tputs j\n\t\t\tbreak;\n\t\tend\n\tend\nend\nmain\n"}, {"source_code": "gets\nnums = gets.chomp.split.map(&:to_i)\n\nputs ((1..3000).to_a - nums)[0]"}, {"source_code": "gets\nnums = gets.chomp.split.map(&:to_i)[0..2999]\n\nputs ((1..3000).to_a - nums)[0]"}], "src_uid": "5e449867d9fcecc84333b81eac9b5d92"} {"nl": {"description": "You have a board represented as a grid with $$$2 \\times n$$$ cells.The first $$$k_1$$$ cells on the first row and first $$$k_2$$$ cells on the second row are colored in white. All other cells are colored in black.You have $$$w$$$ white dominoes ($$$2 \\times 1$$$ tiles, both cells are colored in white) and $$$b$$$ black dominoes ($$$2 \\times 1$$$ tiles, both cells are colored in black).You can place a white domino on the board if both board's cells are white and not occupied by any other domino. In the same way, you can place a black domino if both cells are black and not occupied by any other domino.Can you place all $$$w + b$$$ dominoes on the board if you can place dominoes both horizontally and vertically?", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 3000$$$) — the number of test cases. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$ and $$$k_2$$$ ($$$1 \\le n \\le 1000$$$; $$$0 \\le k_1, k_2 \\le n$$$). The second line of each test case contains two integers $$$w$$$ and $$$b$$$ ($$$0 \\le w, b \\le n$$$).", "output_spec": "For each test case, print YES if it's possible to place all $$$w + b$$$ dominoes on the board and NO, otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).", "sample_inputs": ["5\n1 0 1\n1 0\n1 1 1\n0 0\n3 0 0\n1 3\n4 3 1\n2 2\n5 4 3\n3 1"], "sample_outputs": ["NO\nYES\nNO\nYES\nYES"], "notes": "NoteIn the first test case, $$$n = 1$$$, $$$k_1 = 0$$$ and $$$k_2 = 1$$$. It means that $$$2 \\times 1$$$ board has black cell $$$(1, 1)$$$ and white cell $$$(2, 1)$$$. So, you can't place any white domino, since there is only one white cell.In the second test case, the board of the same size $$$2 \\times 1$$$, but both cell are white. Since $$$w = 0$$$ and $$$b = 0$$$, so you can place $$$0 + 0 = 0$$$ dominoes on the board.In the third test case, board $$$2 \\times 3$$$, but fully colored in black (since $$$k_1 = k_2 = 0$$$), so you can't place any white domino.In the fourth test case, cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, and $$$(2, 1)$$$ are white and other cells are black. You can place $$$2$$$ white dominoes at positions $$$((1, 1), (2, 1))$$$ and $$$((1, 2), (1, 3))$$$ and $$$2$$$ black dominoes at positions $$$((1, 4), (2, 4))$$$ $$$((2, 2), (2, 3))$$$."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n n, k1, k2 = gets.split(' ').map(&:to_i)\r\n w, b = gets.split(' ').map(&:to_i)\r\n \r\n white_count = [k1, k2].min + ((k1-k2).abs / 2)\r\n left_w_c = (k1-k2).abs % 2 # this may be 1 or 0\r\n \r\n black_count = [n-k1, n-k2].min + (((n-k1) - (n-k2)).abs / 2)\r\n left_b_c = ((n-k1) - (n-k2)).abs % 2 # this may be 1 or 0\r\n \r\n puts white_count >= w && black_count >= b ? 'YES' : 'NO'\r\nend"}, {"source_code": "def solve(n, k1, k2, w, b)\r\n w_o = (k1 + 1) / 2 + k2 / 2\r\n w_e = k1 / 2 + (k2 + 1) / 2\r\n\r\n k3, k4 = n - k1, n - k2\r\n b_o = (k3 + 1) / 2 + k4 / 2\r\n b_e = k3 / 2 + (k4 + 1) / 2\r\n\r\n w <= min(w_o, w_e) && b <= min(b_o, b_e)\r\n #(w_o == w_e and b_o == b_e and w <= w_o and b <= b_o)\r\nend\r\n\r\ndef main\r\n t = int\r\n t.times do\r\n n, k1, k2 = ints\r\n w, b = ints\r\n yesno solve(n, k1, k2, w, b)\r\n end\r\nend\r\n\r\nDEBUG = true\r\nMOD = 10**9+7\r\nYESNO = %w(NO YES)\r\nINF = 10**9\r\n\r\ndef int; gets.to_s.to_i end\r\ndef ints; gets.to_s.split.map { |s| s.to_i } end\r\ndef int1s; gets.to_s.split.map { |s| s.to_i - 1 } end\r\ndef float; gets.to_s.to_f end\r\ndef floats; gets.to_s.split.map { |s| s.to_f } end\r\ndef array_of(&convert); gets.to_s.split.map(&convert) end\r\ndef string; gets.to_s.chomp end\r\ndef rep(n, &b); Array.new(n, &b) end\r\ndef yes; puts YESNO[1] end\r\ndef no; puts YESNO[0] end\r\ndef yesno t; puts YESNO[t] end\r\ndef zip(xs, *yss); Enumerator.new { |y| xs.zip(*yss) { |a| y.yield(*a) } } end\r\ndef max(*xs, &block); block_given? ? xs.max_by(&block) : xs.max end\r\ndef min(*xs, &block); block_given? ? xs.min_by(&block) : xs.min end\r\ndef minmax(*xs, &block); block_given? ? xs.minmax_by(&block) : xs.minmax end\r\ndef gcd(*xs); xs.inject(0, :gcd) end\r\ndef matrix(h, w, fill=nil, &block); return Array.new(h) { Array.new(w, &block) } if block_given?; Array.new(h) { [fill] * w } end\r\ndef debug(x = nil); STDERR.puts (block_given? ? yield(x) : x).inspect if DEBUG; x end\r\ndef debug_grid(grid, width = 1); grid.each { |row| STDERR.puts row.map { |x| x.inspect.ljust(width) }.join(\"\") } if DEBUG; grid end\r\ndef if_debug; yield if DEBUG end\r\n\r\nmodule Boolean\r\n def coerce(other); [other, to_i] end\r\n def +@; to_i end\r\n def to_int; to_i end\r\n def *(other); to_i * other end\r\nend\r\n\r\nclass TrueClass\r\n include Boolean\r\n def to_i; 1 end\r\nend\r\n\r\nclass FalseClass\r\n include Boolean\r\n def to_i; 0 end\r\nend\r\n\r\nclass Integer\r\n def div_ceil(y); (self + y - 1) / y end\r\n def mod_inv(mod = MOD); pow(mod-2, mod) end\r\n def mod_div(y, mod = MOD); self * mod_inv(y, mod) % mod end\r\n def factorial(mod = MOD); (2..self).inject(1) { |f, x| f * x % mod } end\r\n def popcount; x = self; c = 0; while x > 0; c += 1 if x & 1 == 1; x >>= 1 end; c end #TODO: faster\r\n def bitbrute(&block); (1< 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\r\n def lower_bound; ac, wa = self.end_open, self.begin; while ac - wa > 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\r\n def shakutori(r2, &pred); Enumerator.new { |y| j, r = r2.begin, r2.end_open; each { |i| j += 1 while j + 1 < r and pred[i, j+1]; y.yield(i, j) } }; end\r\n def widest(&block); Enumerator.new { |y| j, n = self.begin, self.end_open; each { |i| j += 1 while j < n and block[i, j]; y.yield(i, j) if block[i, j] } } end\r\nend\r\n\r\nclass Array\r\n def power(&block); (0 ... 1 << size).each(&block) end\r\n def sorted_merge(other); a = []; i = j = 0; n, m = size, other.size; if j < m and other[j] < self[i]; a << other[j]; j += 1 else; a << self[i]; i += 1 end while i < n; a.push(*other[j..-1]) if j < m; a end\r\n def upper_bound; ac, wa = 0, size; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\r\n def lower_bound; ac, wa = size, 0; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\r\n def cum(*xs, &op); a = []; a << xs[0] if xs.size > 0; a << x = self[0]; (1...size).each { |i| a << x = op[x, self[i]] }; a end\r\n def cumdiff(range); self[range.end_open] - self[range.begin]; end\r\nend\r\n\r\nmodule Enumerable\r\n def sorted_uniq; x = nil; filter { |y| c = x === y; x = y; !c } end\r\n def cumsum; ys = [0]; each { |x| ys << x + ys[-1] }; ys end\r\nend\r\n\r\nmain"}], "negative_code": [], "src_uid": "26354d2628f26eb2eff9432bd46400d5"} {"nl": {"description": "Polycarp has recently created a new level in this cool new game Berlio Maker 85 and uploaded it online. Now players from all over the world can try his level.All levels in this game have two stats to them: the number of plays and the number of clears. So when a player attempts the level, the number of plays increases by $$$1$$$. If he manages to finish the level successfully then the number of clears increases by $$$1$$$ as well. Note that both of the statistics update at the same time (so if the player finishes the level successfully then the number of plays will increase at the same time as the number of clears).Polycarp is very excited about his level, so he keeps peeking at the stats to know how hard his level turns out to be.So he peeked at the stats $$$n$$$ times and wrote down $$$n$$$ pairs of integers — $$$(p_1, c_1), (p_2, c_2), \\dots, (p_n, c_n)$$$, where $$$p_i$$$ is the number of plays at the $$$i$$$-th moment of time and $$$c_i$$$ is the number of clears at the same moment of time. The stats are given in chronological order (i.e. the order of given pairs is exactly the same as Polycarp has written down).Between two consecutive moments of time Polycarp peeked at the stats many players (but possibly zero) could attempt the level.Finally, Polycarp wonders if he hasn't messed up any records and all the pairs are correct. If there could exist such a sequence of plays (and clears, respectively) that the stats were exactly as Polycarp has written down, then he considers his records correct.Help him to check the correctness of his records.For your convenience you have to answer multiple independent test cases.", "input_spec": "The first line contains a single integer $$$T$$$ $$$(1 \\le T \\le 500)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 100$$$) — the number of moments of time Polycarp peeked at the stats. Each of the next $$$n$$$ lines contains two integers $$$p_i$$$ and $$$c_i$$$ ($$$0 \\le p_i, c_i \\le 1000$$$) — the number of plays and the number of clears of the level at the $$$i$$$-th moment of time. Note that the stats are given in chronological order.", "output_spec": "For each test case print a single line. If there could exist such a sequence of plays (and clears, respectively) that the stats were exactly as Polycarp has written down, then print \"YES\". Otherwise, print \"NO\". You can print each letter in any case (upper or lower).", "sample_inputs": ["6\n3\n0 0\n1 1\n1 2\n2\n1 0\n1000 3\n4\n10 1\n15 2\n10 2\n15 2\n1\n765 432\n2\n4 4\n4 3\n5\n0 0\n1 0\n1 0\n1 0\n1 0"], "sample_outputs": ["NO\nYES\nNO\nYES\nNO\nYES"], "notes": "NoteIn the first test case at the third moment of time the number of clears increased but the number of plays did not, that couldn't have happened.The second test case is a nice example of a Super Expert level.In the third test case the number of plays decreased, which is impossible.The fourth test case is probably an auto level with a single jump over the spike.In the fifth test case the number of clears decreased, which is also impossible.Nobody wanted to play the sixth test case; Polycarp's mom attempted it to make him feel better, however, she couldn't clear it."}, "positive_code": [{"source_code": "t = gets.chomp.to_i\nwhile t>0 do\n n = gets.chomp.to_i\n las_a = 0\n las_b = 0\n ok = true\n for i in 1..n do\n tmp = gets.chomp.split(\" \")\n a = tmp[0].to_i\n b = tmp[1].to_i\n if b-las_b>a-las_a or b= pp && c >= pc && (p - pp) >= (c - pc)\n pp = p\n pc = c\n else\n puts 'no'\n return\n end\n end\n\n puts 'yes'\nend\n\n\ndef main\n t = gets.chomp.to_i\n\n t.times do\n run\n end\nend\n\nmain\n"}, {"source_code": "n = STDIN.gets.to_i\nn.times do |i|\n j = STDIN.gets.to_i\n sets = []\n j.times do\n match = STDIN.gets.strip.match(/(\\d+) (\\d+)/)\n sets << [match[1].to_i, match[2].to_i]\n end\n r = 0\n c = 0\n skip = false\n for a, b in sets do\n if (a < r) or (b < c) or (b > a) or ((a == r) and (b > c)) or ((a - r) < (b - c))\n puts 'NO'\n skip = true\n break\n else\n r = a\n c = b\n end\n end\n puts 'YES' unless skip\nend"}, {"source_code": "t = gets.to_i\nt.times do\n played = 0\n cleared = 0\n ok = true\n n = gets.to_i\n n.times do\n p, c = gets.split.map(&:to_i)\n ok = false if p < c\n ok = false if p < played\n ok = false if c < cleared\n ok = false if (p - played) < (c - cleared)\n played = p\n cleared = c\n end\n puts ok ? \"YES\" : \"NO\"\nend\n"}, {"source_code": "gets.to_i.times do\n prev = [0, 0]\n ok = true\n\n gets.to_i.times do\n a = gets.split(' ').map(&:to_i)\n\n ok &&= (a.last - prev.last) >= 0\n ok &&= (a.first - prev.first) >= (a.last - prev.last)\n\n prev = a\n end\n\n puts ok ? 'YES' : 'NO'\nend\n"}], "negative_code": [{"source_code": "n = STDIN.gets.to_i\nn.times do |i|\n j = STDIN.gets.to_i\n sets = []\n j.times do\n match = STDIN.gets.strip.match(/(\\d+) (\\d+)/)\n sets << [match[1].to_i, match[2].to_i]\n end\n p = 0\n c = 0\n skip = false\n for a, b in sets do\n if (a < p) or (b < c) or (b > a)\n puts 'NO'\n skip = true\n break\n else\n p = a\n c = b\n end\n end\n puts 'YES' unless skip\nend"}, {"source_code": "t = gets.to_i\nt.times do\n played = 0\n cleared = 0\n ok = true\n n = gets.to_i\n n.times do\n p, c = gets.split.map(&:to_i)\n ok = false if p < c\n ok = false if p < played\n ok = false if c < cleared\n played = p\n cleared = c\n end\n puts ok ? \"YES\" : \"NO\"\nend\n"}], "src_uid": "714834defd0390659f6ed5bc3024f613"} {"nl": {"description": "You are given two integers $$$x$$$ and $$$y$$$. You can perform two types of operations: Pay $$$a$$$ dollars and increase or decrease any of these integers by $$$1$$$. For example, if $$$x = 0$$$ and $$$y = 7$$$ there are four possible outcomes after this operation: $$$x = 0$$$, $$$y = 6$$$; $$$x = 0$$$, $$$y = 8$$$; $$$x = -1$$$, $$$y = 7$$$; $$$x = 1$$$, $$$y = 7$$$. Pay $$$b$$$ dollars and increase or decrease both integers by $$$1$$$. For example, if $$$x = 0$$$ and $$$y = 7$$$ there are two possible outcomes after this operation: $$$x = -1$$$, $$$y = 6$$$; $$$x = 1$$$, $$$y = 8$$$. Your goal is to make both given integers equal zero simultaneously, i.e. $$$x = y = 0$$$. There are no other requirements. In particular, it is possible to move from $$$x=1$$$, $$$y=0$$$ to $$$x=y=0$$$.Calculate the minimum amount of dollars you have to spend on it.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of testcases. The first line of each test case contains two integers $$$x$$$ and $$$y$$$ ($$$0 \\le x, y \\le 10^9$$$). The second line of each test case contains two integers $$$a$$$ and $$$b$$$ ($$$1 \\le a, b \\le 10^9$$$).", "output_spec": "For each test case print one integer — the minimum amount of dollars you have to spend.", "sample_inputs": ["2\n1 3\n391 555\n0 0\n9 4"], "sample_outputs": ["1337\n0"], "notes": "NoteIn the first test case you can perform the following sequence of operations: first, second, first. This way you spend $$$391 + 555 + 391 = 1337$$$ dollars.In the second test case both integers are equal to zero initially, so you dont' have to spend money."}, "positive_code": [{"source_code": "file = File.exist?('in.in') ? File.new('in.in','r') : STDIN\n\nt = file.gets.chomp.to_i\nt.times do\n x, y = file.gets.chomp.split.map(&:to_i)\n a, b = file.gets.chomp.split.map(&:to_i)\n\n if a == 0 || b == 0\n puts 0\n next\n end\n\n if x==y\n puts [x*b, a*2*x].min\n elsif x > y\n c=a*(x-y)+b*y\n d=a*(x-y)+2*a*y\n puts [c,d].min\n else\n c=a*(y-x)+b*x;\n d=a*(y-x)+2*a*x;\n puts [c,d].min\n end\nend"}, {"source_code": "def run\n x, y = gets.chomp.split(\" \").map(&:to_i)\n a, b = gets.chomp.split(\" \").map(&:to_i)\n\n r = if a * 2 < b || x == 0 || y == 0\n (x + y) * a\n else\n (x - y).abs * a + [x, y].min * b\n end\n puts r\nend\n\n\ndef main\n t = gets.chomp.to_i\n\n t.times do\n run\n end\nend\n\nmain\n"}, {"source_code": "gets.to_i.times {\n x, y=gets.split.map &:to_i\n a, b=gets.split.map &:to_i\n \n s=x.abs\n t=y.abs\n puts [s*a+t*a, s*b+(y-x).abs*a, t*b+(x-y).abs*a].min\n}\n"}, {"source_code": "g = gets.chomp.to_i\ng.times{|w|\nx,y = gets.split(\" \").map{|x|x.to_i}\na,b = gets.split(\" \").map{|x|x.to_i}\nall = []\n\nx1=0\ny1=0\n\nx1+=x\ny1+=y\n\nz=0\n#method 1\nloop do\nif (x==0 && y==0)\nbreak \nelse\n\nif (x > 0 && y > 0)\nhh=[]\nhh.push(x)\nhh.push(y)\nz+=(hh.min*b)\nx-=hh.min\ny-=hh.min\n\nif (x!=0)\nz+=x*a\nx-=x\nend\nif (y!=0)\nz+=y*a\ny-=y\nend\nend\n\n\nif (x < 0 && y < 0)\nhh=[]\nhh.push(x)\nhh.push(y)\nz+=(hh.max.abs*b)\nx+=hh.max.abs\ny+=hh.max.abs\n\nif (x!=0)\nz+=x.abs*a\nx+=x.abs\nend\nif (y!=0)\nz+=y.abs*a\ny+=y.abs\nend\nend\n\nif (x<0)\nz+=x.abs*a\nx+=x.abs\nend\nif (x>0)\nz+=x*a\nx-=x\nend\nif (y<0)\nz+=y.abs*a\ny+=y.abs\nend\nif (y>0)\nz+=y*a\ny-=y\nend\n\nend\nend\nall.push(z)\n\nz=0\nx=x1\ny=y1\nloop do\nif (x==0 && y==0)\nbreak \nelse\n\nif (x<0)\nz+=x.abs*a\nx+=x.abs\nend\nif (x>0)\nz+=x*a\nx-=x\nend\nif (y<0)\nz+=y.abs*a\ny+=y.abs\nend\nif (y>0)\nz+=y*a\ny-=y\nend\n\nend\nend\nall.push(z)\n\n\nputs all.min\n}\n\n"}, {"source_code": "1.upto(gets.chomp.to_i) do\n x,y = gets.chomp.split.map(&:to_i)\n a,b = gets.chomp.split.map(&:to_i)\n\n x,y = y,x if x > y\n puts [a*x + a*y, b*x + a*(y-x)].min\nend\n"}, {"source_code": "t = gets.chomp.to_i\nt.times do \n\tx,y = gets.chomp.split(\" \").map(&:to_i)\n\ta, b = gets.chomp.split(\" \").map(&:to_i)\n\tif 2 * a < b then\n\t\tputs a * (x + y)\n\telse\n\t\tdiff = (x - y).abs\n\t\tputs ([x,y].max - diff) * b + a * diff\n\tend\nend\n\n"}, {"source_code": "1.upto(gets.to_i) do\n x, y = gets.split(' ').map(&:to_i)\n a, b = gets.split(' ').map(&:to_i)\n\n puts [\n [x, y].min * b + (x - y).abs * a,\n x * a + y * a\n ].min\nend\n"}, {"source_code": "def solution\n x, y = gets.split.map(&:to_i)\n a, b = gets.split.map(&:to_i)\n\n return 0 if x == 0 && y == 0\n\n if b > 2 * a\n return (x + y) * a\n end\n\n min = [x, y].min\n max = [x, y].max\n min * b + (max - min) * a\nend\n\nt = gets.to_i\nt.times do\n p solution\nend\n"}], "negative_code": [{"source_code": "file = File.exist?('in.in') ? File.new('in.in','r') : STDIN\n\nt = file.gets.chomp.to_i\nt.times do\n x, y = file.gets.chomp.split.map(&:to_i)\n a, b = file.gets.chomp.split.map(&:to_i)\n res = 0\n\n if x==y\n res+= [x*b, a*2*x].min\n else\n d = (x-y).abs\n res += d * a\n res += ([x,y].max-d)*b\n end\n puts res\nend"}, {"source_code": "file = File.exist?('in.in') ? File.new('in.in','r') : STDIN\n\nt = file.gets.chomp.to_i\nt.times do\n x, y = file.gets.chomp.split.map(&:to_i)\n a, b = file.gets.chomp.split.map(&:to_i)\n\n\n if x==y\n puts [x*b, a*2*x].min\n else\n res = 0\n d = (x-y).abs\n res += d * a\n res += ([x,y].max-d)*b\n res2 = (d*a) +(b*[x,y].max)\n puts [res, res2].min\n end\nend"}, {"source_code": "file = File.exist?('in.in') ? File.new('in.in','r') : STDIN\n\nt = file.gets.chomp.to_i\nt.times do\n x, y = file.gets.chomp.split.map(&:to_i)\n a, b = file.gets.chomp.split.map(&:to_i)\n\n if a == 0 || b == 0\n puts 0\n next\n end\n\n if x==y\n puts [x*b, a*2*x].min\n else\n res = 0\n d = (x-y).abs\n res += d * a\n res += ([x,y].max-d)*b\n res2 = (d*a) +(b*[x,y].max)\n # puts \"#{[res, res2]}\"\n puts [res, res2].min\n end\nend"}, {"source_code": "file = File.exist?('in.in') ? File.new('in.in','r') : STDIN\n\nt = file.gets.chomp.to_i\nt.times do\n x, y = file.gets.chomp.split.map(&:to_i)\n a, b = file.gets.chomp.split.map(&:to_i)\n\n if a == 0 || b == 0\n puts 0\n next\n end\n\n if x==y\n puts [x*b, a*2*x].min\n else\n d = (x-y).abs\n\n res = (d*a) + ([x,y].max-d)*b\n\n # res2 = (d*a) +(b*[x,y].max)\n # puts \"#{[res, res2]}\"\n # puts [res, res2].min\n puts res\n end\nend"}, {"source_code": "g = gets.chomp.to_i\ng.times{|w|\nx,y = gets.split(\" \").map{|x|x.to_i}\na,b = gets.split(\" \").map{|x|x.to_i}\nall = []\n\nz=0\n#method 1\nloop do\nif (x==0 && y==0)\nbreak \nelse\n\nif (x > 0 && y > 0)\nhh=[]\nhh.push(x)\nhh.push(y)\nz+=(hh.min*b)\nx-=hh.min\ny-=hh.min\n\nif (x!=0)\nz+=x*a\nx-=x\nend\nif (y!=0)\nz+=y*a\ny-=y\nend\nend\n\n\nif (x < 0 && y < 0)\nhh=[]\nhh.push(x)\nhh.push(y)\nz+=(hh.max*b)\nx+=hh.max.abs\ny+=hh.max.abs\n\nif (x!=0)\nz+=x.abs*a\nx+=x.abs\nend\nif (y!=0)\nz+=y.abs*a\ny+=y.abs\nend\nend\n\nif (x<0)\nz+=x.abs*a\nx+=x.abs\nend\nif (x>0)\nz+=x*a\nx-=x\nend\nif (y<0)\nz+=y.abs*a\ny+=y.abs\nend\nif (y>0)\nz+=y*a\ny-=y\nend\n\nend\nend\n\n\n\nputs z\n}\n\n"}, {"source_code": "g = gets.chomp.to_i\ng.times{|w|\nx,y = gets.split(\" \").map{|x|x.to_i}\na,b = gets.split(\" \").map{|x|x.to_i}\nall = []\n\nz=0\n#method 1\nloop do\nif (x==0 && y==0)\nbreak \nelse\n\nif (x > 0 && y > 0)\nhh=[]\nhh.push(x)\nhh.push(y)\nz+=(hh.min*b)\nx-=hh.min\ny-=hh.min\n\nif (x!=0)\nz+=x*a\nx-=x\nend\nif (y!=0)\nz+=y*a\ny-=y\nend\nend\n\n\nif (x < 0 && y < 0)\nhh=[]\nhh.push(x)\nhh.push(y)\nz+=(hh.max.abs*b)\nx+=hh.max.abs\ny+=hh.max.abs\n\nif (x!=0)\nz+=x.abs*a\nx+=x.abs\nend\nif (y!=0)\nz+=y.abs*a\ny+=y.abs\nend\nend\n\nif (x<0)\nz+=x.abs*a\nx+=x.abs\nend\nif (x>0)\nz+=x*a\nx-=x\nend\nif (y<0)\nz+=y.abs*a\ny+=y.abs\nend\nif (y>0)\nz+=y*a\ny-=y\nend\n\nend\nend\n\n\n\nputs z\n}\n\n"}, {"source_code": "g = gets.chomp.to_i\ng.times{|w|\nx,y = gets.split(\" \").map{|x|x.to_i}\na,b = gets.split(\" \").map{|x|x.to_i}\nall = []\n\nz=0\n#method 1\nif (x==0 && y==0)\n \nelse\n\nif (x > 0 && y > 0)\nhh=[]\nhh.push(x)\nhh.push(y)\nz+=(hh.min*b)\nx-=hh.min\ny-=hh.min\n\nif (x!=0)\nz+=x*a\nx-=x\nend\nif (y!=0)\nz+=y*a\ny-=y\nend\nend\n\n\nif (x < 0 && y < 0)\nhh=[]\nhh.push(x)\nhh.push(y)\nz+=(hh.max*b)\nx+=hh.max\ny+=hh.max\n\nif (x!=0)\nz+=x.abs*a\nx+=x\nend\nif (y!=0)\nz+=y.abs*a\ny+=y\nend\nend\n\n\nend\nputs z\n}\n\n"}, {"source_code": "g = gets.chomp.to_i\ng.times{|w|\nx,y = gets.split(\" \").map{|x|x.to_i}\na,b = gets.split(\" \").map{|x|x.to_i}\nall = []\n\nz=0\n#method 1\nloop do\nif (x==0 && y==0)\nbreak \nelse\n\nif (x > 0 && y > 0)\nhh=[]\nhh.push(x)\nhh.push(y)\nz+=(hh.min*b)\nx-=hh.min\ny-=hh.min\n\nif (x!=0)\nz+=x*a\nx-=x\nend\nif (y!=0)\nz+=y*a\ny-=y\nend\nend\n\n\nif (x < 0 && y < 0)\nhh=[]\nhh.push(x)\nhh.push(y)\nz+=(hh.max*b)\nx+=hh.max\ny+=hh.max\n\nif (x!=0)\nz+=x.abs*a\nx+=x\nend\nif (y!=0)\nz+=y.abs*a\ny+=y\nend\nend\n\nif (x<0)\nz+=x.abs*a\nx+=x.abs\nend\nif (x>0)\nz+=x*a\nx-=x\nend\nif (y<0)\nz+=y.abs*a\ny+=y.abs\nend\nif (y>0)\nz+=y*a\ny-=y\nend\n\nend\nend\n\n\n\nputs z\n}\n\n"}], "src_uid": "edf394051c6b35f593abd4c34b091eae"} {"nl": {"description": "Jafar has n cans of cola. Each can is described by two integers: remaining volume of cola ai and can's capacity bi (ai  ≤  bi).Jafar has decided to pour all remaining cola into just 2 cans, determine if he can do this or not!", "input_spec": "The first line of the input contains one integer n (2 ≤ n ≤ 100 000) — number of cola cans. The second line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 109) — volume of remaining cola in cans. The third line contains n space-separated integers that b1, b2, ..., bn (ai ≤ bi ≤ 109) — capacities of the cans.", "output_spec": "Print \"YES\" (without quotes) if it is possible to pour all remaining cola in 2 cans. Otherwise print \"NO\" (without quotes). You can print each letter in any case (upper or lower).", "sample_inputs": ["2\n3 5\n3 6", "3\n6 8 9\n6 10 12", "5\n0 0 5 0 0\n1 1 8 10 5", "4\n4 1 0 3\n5 2 2 3"], "sample_outputs": ["YES", "NO", "YES", "YES"], "notes": "NoteIn the first sample, there are already 2 cans, so the answer is \"YES\"."}, "positive_code": [{"source_code": "gets\na=gets.split.map(&:to_i).reduce :+\nb=gets.split.map(&:to_i).sort[-2..-1].reduce :+\nputs a<=b ? :YES : :NO"}, {"source_code": "def solve(n, cans, sizes)\n total = cans.inject(:+)\n free = sizes.sort.reverse.first(2).inject(:+)\n free >= total ? \"YES\" : \"NO\"\nend\n\nputs solve(gets.strip.to_i, gets.strip.split.map(&:to_i), gets.strip.split.map(&:to_i))"}, {"source_code": "n = gets.chomp.to_i\n$a = gets.chomp.split.map(&:to_i)\n$b = gets.chomp.split.map(&:to_i)\n\ndef solve\n $b.sort_by! { |x| x }\n if ($a.inject(0, :+) <= $b[-2..-1].inject(0, :+))\n return \"YES\"\n end\n return \"NO\"\nend\n\nprint solve"}, {"source_code": "gets\na=gets.split.map{|e| e.to_i}\nb=gets.split.map{|e| e.to_i}.sort.reverse\nif b[0]+b[1]-a.inject(0){|sum,e| sum+e}<0 then\nputs \"NO\"\nelse\nputs \"YES\"\nend"}, {"source_code": "def sum(arr)\n ret = 0\n arr.each do |i|\n ret += i\n end\n return ret\nend\n\nn = gets.to_i\na = gets.split.map {|i| i.to_i}\nb = gets.split.map {|i| i.to_i}.sort\nif sum(a) <= sum(b.last(2)) then\n\tputs \"YES\"\nelse\n\tputs \"NO\"\nend"}, {"source_code": "n = Integer(gets.chomp)\n\nvolume_line = gets.chomp.split(\" \").to_a.map{ |i| Integer(i) }\nvalue = volume_line.inject(0){ |v, i| v + i }\n\ncups_line = gets.chomp.split(\" \").to_a.map{ |i| Integer(i) }.sort.reverse\n\nprint (value <= cups_line[0] + cups_line[1]) ? \"YES\" : \"NO\""}, {"source_code": "n = gets\nn = n.to_i\n\narr = Array.new(n){Array.new(2){0}}\n\ns = gets\ns.split(\" \").map(&:to_i).each.with_index do |v, index|\n arr[index][0] = v\nend\n\ns = gets\ns.split(\" \").map(&:to_i).each.with_index do |v, index|\n arr[index][1] = v\nend\n\n\nfirst_max = -Float::INFINITY\nsecond_max = -Float::INFINITY\n\nsum_volumes = 0\n\n\narr.each do |v,c|\n sum_volumes += v\n if c >= first_max\n second_max = first_max\n first_max = c\n elsif c > second_max\n second_max = c\n end\nend\n\n\nif sum_volumes <= (first_max + second_max )\n puts \"YES\"\nelse\n puts \"NO\"\nend\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i).inject(:+)\nb = gets.split.map(&:to_i).sort\nputs a <= b[-1] + b[-2] ? \"YES\" : \"NO\""}, {"source_code": "n = gets.to_i\na = gets.split.map &:to_i\nb = gets.split.map &:to_i\nputs a.inject(0){|s, x| s + x} <= b.sort[-2] + b.sort[-1] ? 'YES' : 'NO'"}, {"source_code": "n= gets.to_i\n\nremaining = gets.split(\" \").map(&:to_i).inject(0, :+)\ncapacities = gets.split(\" \").map(&:to_i)\nmax1 = capacities.max\ncapacities.delete_at capacities.index(max1)\nmax2 = capacities.max\n\nif max1+max2 >= remaining\n\tprint('YES')\nelse\n\tprint('NO')\nend\n"}, {"source_code": "n= gets.to_i\n\nremaining = gets.split(\" \").map(&:to_i).inject(0, :+)\ncapacities = gets.split(\" \").map(&:to_i)\nmax1 = capacities.max\ncapacities.delete_at capacities.index(max1)\nmax2 = capacities.max\n\nif max1+max2 >= remaining\n\tprint('YES')\nelse\n\tprint('NO')\nend\n"}], "negative_code": [], "src_uid": "88390110e4955c521867864a6f3042a0"} {"nl": {"description": "You are given an array $$$a_1, a_2, \\dots , a_n$$$. Array is good if for each pair of indexes $$$i < j$$$ the condition $$$j - a_j \\ne i - a_i$$$ holds. Can you shuffle this array so that it becomes good? To shuffle an array means to reorder its elements arbitrarily (leaving the initial order is also an option).For example, if $$$a = [1, 1, 3, 5]$$$, then shuffled arrays $$$[1, 3, 5, 1]$$$, $$$[3, 5, 1, 1]$$$ and $$$[5, 3, 1, 1]$$$ are good, but shuffled arrays $$$[3, 1, 5, 1]$$$, $$$[1, 1, 3, 5]$$$ and $$$[1, 1, 5, 3]$$$ aren't.It's guaranteed that it's always possible to shuffle an array to meet this condition.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \\le n \\le 100$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\dots , a_n$$$ ($$$1 \\le a_i \\le 100$$$).", "output_spec": "For each test case print the shuffled version of the array $$$a$$$ which is good.", "sample_inputs": ["3\n1\n7\n4\n1 1 3 5\n6\n3 2 1 5 6 4"], "sample_outputs": ["7\n1 5 1 3\n2 4 6 1 3 5"], "notes": null}, "positive_code": [{"source_code": "T = gets.to_i\n\nT.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n\n puts a.sort.reverse.join(' ')\nend\n"}, {"source_code": "# coding: utf-8\n\n# bogosort 정렬이 아니란 거죠.\n# i < j 인 인덱스 페어에 대하여 i - ai != j - aj\n# 예를 들어 a = [1, 1, 3, 5] 에 대해서\n# (1, 2) 는 1 - 1 != 2 - 1 로 만족이지만 (1, 3)은 1 - 1 == 3 - 3이므로\n# 성립하지 않음.\n# 배열의 최소 크기는 1\n\ndef bogosort(a)\n ar = a.sort.reverse\n return ar\nend\n\nt = gets.to_i\nt.times {\n n = gets.to_i\n a = gets.split.map(&:to_i)\n puts bogosort(a).join(' ')\n}"}, {"source_code": " gets.to_i.times do\n gets\n puts gets.split.map(&:to_i).sort.reverse.join(' ')\n end\n"}, {"source_code": "t = gets.chomp.to_i\n(0...t).each do |_|\n n = gets.chomp.to_i\n l = gets.chomp.split.map(&:to_i)\n #r = []\n r = l.sort.reverse\n print r.join(' ')\n puts\nend\n"}, {"source_code": "t = gets.chomp.to_i\n(0...t).each do |_|\n n = gets.chomp.to_i\n print gets.chomp.split.map(&:to_i).sort.reverse.join(' ')\n puts\nend"}], "negative_code": [{"source_code": " gets.to_i.times do\n gets\n puts gets.split.sort.reverse.join(' ')\n end\n"}, {"source_code": "def check(a)\n (0...a.size).each do |i|\n state = true\n (i+1...a.size).each do |j|\n (state = false; break;) if j - a[j] == i - a[i]\n end\n return true if state\n end\n false\nend\n\n\nt = gets.chomp.to_i\n(0...t).each do |_|\n n = gets.chomp.to_i\n l = gets.chomp.split.map(&:to_i)\n r = []\n l.sort.permutation.each do |a|\n (r = a; break;) if check(a)\n end\n print r.join(' ')\n puts\nend\n\n\n"}, {"source_code": "def check(a)\n (0...a.size).each do |i|\n state = true\n (i+1...a.size).each do |j|\n (state = false; break;) if j - a[j] == i - a[i]\n end\n return true if state\n end\n false\nend\n\n\nt = gets.chomp.to_i\n(0...t).each do |_|\n n = gets.chomp.to_i\n l = gets.chomp.split.map(&:to_i)\n r = []\n l.permutation.each do |a|\n r = a if check(a)\n end\n print r.join(' ')\n puts\nend\n\n\n"}], "src_uid": "1d89df4153d70087d212b711852eba5f"} {"nl": {"description": "This is an interactive problem.Natasha is going to fly to Mars. Finally, Natasha sat in the rocket. She flies, flies... but gets bored. She wishes to arrive to Mars already! So she decides to find something to occupy herself. She couldn't think of anything better to do than to calculate the distance to the red planet.Let's define $$$x$$$ as the distance to Mars. Unfortunately, Natasha does not know $$$x$$$. But it is known that $$$1 \\le x \\le m$$$, where Natasha knows the number $$$m$$$. Besides, $$$x$$$ and $$$m$$$ are positive integers.Natasha can ask the rocket questions. Every question is an integer $$$y$$$ ($$$1 \\le y \\le m$$$). The correct answer to the question is $$$-1$$$, if $$$x<y$$$, $$$0$$$, if $$$x=y$$$, and $$$1$$$, if $$$x>y$$$. But the rocket is broken — it does not always answer correctly. Precisely: let the correct answer to the current question be equal to $$$t$$$, then, if the rocket answers this question correctly, then it will answer $$$t$$$, otherwise it will answer $$$-t$$$.In addition, the rocket has a sequence $$$p$$$ of length $$$n$$$. Each element of the sequence is either $$$0$$$ or $$$1$$$. The rocket processes this sequence in the cyclic order, that is $$$1$$$-st element, $$$2$$$-nd, $$$3$$$-rd, $$$\\ldots$$$, $$$(n-1)$$$-th, $$$n$$$-th, $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd, $$$\\ldots$$$, $$$(n-1)$$$-th, $$$n$$$-th, $$$\\ldots$$$. If the current element is $$$1$$$, the rocket answers correctly, if $$$0$$$ — lies. Natasha doesn't know the sequence $$$p$$$, but she knows its length — $$$n$$$.You can ask the rocket no more than $$$60$$$ questions.Help Natasha find the distance to Mars. Assume, that the distance to Mars does not change while Natasha is asking questions.Your solution will not be accepted, if it does not receive an answer $$$0$$$ from the rocket (even if the distance to Mars is uniquely determined by the already received rocket's answers).", "input_spec": "The first line contains two integers $$$m$$$ and $$$n$$$ ($$$1 \\le m \\le 10^9$$$, $$$1 \\le n \\le 30$$$) — the maximum distance to Mars and the number of elements in the sequence $$$p$$$.", "output_spec": null, "sample_inputs": ["5 2\n1\n-1\n-1\n1\n0"], "sample_outputs": ["1\n2\n4\n5\n3"], "notes": "NoteIn the example, hacking would look like this:5 2 31 0This means that the current distance to Mars is equal to $$$3$$$, Natasha knows that it does not exceed $$$5$$$, and the rocket answers in order: correctly, incorrectly, correctly, incorrectly ...Really:on the first query ($$$1$$$) the correct answer is $$$1$$$, the rocket answered correctly: $$$1$$$;on the second query ($$$2$$$) the correct answer is $$$1$$$, the rocket answered incorrectly: $$$-1$$$;on the third query ($$$4$$$) the correct answer is $$$-1$$$, the rocket answered correctly: $$$-1$$$;on the fourth query ($$$5$$$) the correct answer is $$$-1$$$, the rocket answered incorrectly: $$$1$$$;on the fifth query ($$$3$$$) the correct and incorrect answer is $$$0$$$."}, "positive_code": [{"source_code": "M, N = gets.split.map(&:to_i)\nwrong = Array.new(N)\n\nN.times do |i|\n puts 1\n $stdout.flush\n result = gets.to_i\n exit if result == 0\n wrong[i] = (result != 1)\nend\n\nub = M\nlb = 2\n60.times do |i|\n mid = (ub+lb)/2\n puts mid\n $stdout.flush\n result = gets.to_i\n exit if result == 0\n if (result == 1) ^ wrong[i%N]\n lb = mid + 1\n else\n ub = mid - 1\n end\nend"}, {"source_code": "q = 60\nm, n = gets.strip.split.map(&:to_i)\nseq=Array.new(n)\n\nq-=n\n\nn.times do |i|\n puts 1\n STDOUT.flush\n seq[i] = gets.to_i\n exit(0) if seq[i]==0\nend\n\nlo=1\nhi=m\nsi=0\nwhile lo<=hi\n mid=((lo+hi)/2)\n puts mid\n STDOUT.flush\n res = gets.to_i\n res *= seq[si]\n si+=1\n si%=n\n if res==0 || res.abs==2\n exit 0\n elsif res==1\n lo=mid+1\n else\n hi=mid\n end \nend"}], "negative_code": [{"source_code": "N, M = gets.split.map(&:to_i)\nwrong = Array.new(N)\n\nN.times do |i|\n puts 1\n $stdout.flush\n result = gets.to_i\n exit if result == 0\n wrong[i] = (result == 1)\nend\n\nub = M\nlb = 2\nN.times do |i|\n mid = (ub+lb)/2\n puts mid\n $stdout.flush\n result = gets.to_i\n exit if result == 0\n if (result == 1) ^ wrong[i]\n lb = mid + 1\n else\n ub = mid - 1\n end\nend"}, {"source_code": "M, N = gets.split.map(&:to_i)\nwrong = Array.new(N)\n\nN.times do |i|\n puts 1\n $stdout.flush\n result = gets.to_i\n exit if result == 0\n wrong[i] = (result != 1)\nend\n\nub = M\nlb = 2\nN.times do |i|\n mid = (ub+lb)/2\n puts mid\n $stdout.flush\n result = gets.to_i\n exit if result == 0\n if (result == 1) ^ wrong[i]\n lb = mid + 1\n else\n ub = mid - 1\n end\nend"}, {"source_code": "q = 60\nm, n = gets.strip.split.map(&:to_i)\nseq=Array.new(n)\n\nq-=n\n\nn.times do |i|\n puts 0\n STDOUT.flush\n seq[i] = gets.to_i\nend\n\nlo=1\nhi=m\nsi=0\nwhile lo<=hi\n mid=((lo+hi)/2)\n puts mid\n STDOUT.flush\n res = gets.to_i\n res *= seq[si]\n si+=1\n si%=n\n if res==0 || res.abs==2\n exit 0\n elsif res==1\n lo=mid+1\n else\n hi=mid\n end \nend"}, {"source_code": "q = 60\nm, n = gets.strip.split.map(&:to_i)\nseq=Array.new(n)\n\nq-=n\n\nn.times do |i|\n puts -1\n STDOUT.flush\n seq[i] = gets.to_i\nend\n\nlo=1\nhi=m\nsi=0\nwhile lo<=hi\n mid=((lo+hi)/2)\n puts mid\n STDOUT.flush\n res = gets.to_i\n res *= seq[si]\n si+=1\n si%=n\n if res==0 || res.abs==2\n exit 0\n elsif res==1\n lo=mid+1\n else\n hi=mid\n end \nend"}], "src_uid": "f06dff83491772f8879e45b90b61dc88"} {"nl": {"description": "You are given two integers $$$n$$$ and $$$k$$$.You should create an array of $$$n$$$ positive integers $$$a_1, a_2, \\dots, a_n$$$ such that the sum $$$(a_1 + a_2 + \\dots + a_n)$$$ is divisible by $$$k$$$ and maximum element in $$$a$$$ is minimum possible.What is the minimum possible maximum element in $$$a$$$?", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le n \\le 10^9$$$; $$$1 \\le k \\le 10^9$$$).", "output_spec": "For each test case, print one integer — the minimum possible maximum element in array $$$a$$$ such that the sum $$$(a_1 + \\dots + a_n)$$$ is divisible by $$$k$$$. ", "sample_inputs": ["4\n1 5\n4 3\n8 8\n8 17"], "sample_outputs": ["5\n2\n1\n3"], "notes": "NoteIn the first test case $$$n = 1$$$, so the array consists of one element $$$a_1$$$ and if we make $$$a_1 = 5$$$ it will be divisible by $$$k = 5$$$ and the minimum possible.In the second test case, we can create array $$$a = [1, 2, 1, 2]$$$. The sum is divisible by $$$k = 3$$$ and the maximum is equal to $$$2$$$.In the third test case, we can create array $$$a = [1, 1, 1, 1, 1, 1, 1, 1]$$$. The sum is divisible by $$$k = 8$$$ and the maximum is equal to $$$1$$$."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n n, k = gets.split.map &:to_i\r\n k *= (n + k - 1) / k\r\n puts (k + n - 1) / n\r\nend"}, {"source_code": "_t = gets.to_s.to_i\r\n_t.times do\r\n n, k = gets.to_s.split.map{ |e| e.to_i }\r\n k *= (n + k - 1) / k if n > k\r\n puts (k + n - 1) / n\r\nend"}, {"source_code": "gets.to_i.times do\r\n n,k=gets.split.map(&:to_i)\r\n if k==1\r\n puts 1\r\n elsif n==1\r\n puts k\r\n elsif n%k==0\r\n puts 1\r\n elsif n= last ? last-1 : val\n last = last < 0 ? 0 : last\n ans += last\n end\n\n print \"#{ans}\\n\"\nend\n"}, {"source_code": "q = gets.to_i\n(1..q).each do\n n = gets.to_i\n count = Array.new(n + 10, 0)\n candies = gets.split(\" \").map{|c| c.to_i}\n candies.each {|candy| count[candy] += 1}\n hash = {}\n res = []\n count.each do |cnt|\n tmp = cnt\n while tmp > 0 && hash[tmp] == 1\n tmp -= 1\n end\n res << tmp\n hash[tmp] = 1\n end\n sum = 0\n res.each {|rs| sum += rs}\n puts sum\nend"}, {"source_code": "MOD=10**9+7\ncnt=0; sum=0; prev=nil; can=true; h=Hash.new(0)\ndef gs() gets.chomp end\ndef gi() gets.to_i end\ndef gsmi() gets.chomp.split.map(&:to_i) end\n\ndef desc(ar) ar.sort!{|x,y|y<=>x} end\ndef min(a,b) a<=b ? a : b end\ndef max(a,b) a>=b ? a : b end\ndef sum(ar) ar.inject(:+) end\n\ndef C(a,b) b==0||a==b ? 1 : (b=a-b if a/2= last ? last-1 : val\n last = last < 0 ? 0 : last\n ans += last\n end\n\n print \"#{ans}\\n\"\nend\n"}, {"source_code": "q = gets.to_i\n(1..q).each do\n n = gets.to_i\n count = Array.new(1000000, 0)\n candies = gets.split(\" \").map{|c| c.to_i}\n candies.each {|candy| count[candy] += 1}\n hash = Array.new(1000000, 0)\n res = Array.new(1, 0)\n count.each do |cnt|\n tmp = cnt\n while tmp > 0 && hash[tmp] == 1\n tmp -= 1\n end\n #res.append(tmp)\n hash[tmp] = 1\n end\n sum = 0\n res.each {|rs| sum += rs}\n puts sum\nend"}], "src_uid": "a00d831da539c69d571d9720fd94eee1"} {"nl": {"description": "Ivan places knights on infinite chessboard. Initially there are $$$n$$$ knights. If there is free cell which is under attack of at least $$$4$$$ knights then he places new knight in this cell. Ivan repeats this until there are no such free cells. One can prove that this process is finite. One can also prove that position in the end does not depend on the order in which new knights are placed.Ivan asked you to find initial placement of exactly $$$n$$$ knights such that in the end there will be at least $$$\\lfloor \\frac{n^{2}}{10} \\rfloor$$$ knights.", "input_spec": "The only line of input contains one integer $$$n$$$ ($$$1 \\le n \\le 10^{3}$$$) — number of knights in the initial placement.", "output_spec": "Print $$$n$$$ lines. Each line should contain $$$2$$$ numbers $$$x_{i}$$$ and $$$y_{i}$$$ ($$$-10^{9} \\le x_{i}, \\,\\, y_{i} \\le 10^{9}$$$) — coordinates of $$$i$$$-th knight. For all $$$i \\ne j$$$, $$$(x_{i}, \\,\\, y_{i}) \\ne (x_{j}, \\,\\, y_{j})$$$ should hold. In other words, all knights should be in different cells. It is guaranteed that the solution exists.", "sample_inputs": ["4", "7"], "sample_outputs": ["1 1\n3 1\n1 5\n4 4", "2 1\n1 2\n4 1\n5 2\n2 6\n5 7\n6 6"], "notes": "NoteLet's look at second example:Green zeroes are initial knights. Cell $$$(3, \\,\\, 3)$$$ is under attack of $$$4$$$ knights in cells $$$(1, \\,\\, 2)$$$, $$$(2, \\,\\, 1)$$$, $$$(4, \\,\\, 1)$$$ and $$$(5, \\,\\, 2)$$$, therefore Ivan will place a knight in this cell. Cell $$$(4, \\,\\, 5)$$$ is initially attacked by only $$$3$$$ knights in cells $$$(2, \\,\\, 6)$$$, $$$(5, \\,\\, 7)$$$ and $$$(6, \\,\\, 6)$$$. But new knight in cell $$$(3, \\,\\, 3)$$$ also attacks cell $$$(4, \\,\\, 5)$$$, now it is attacked by $$$4$$$ knights and Ivan will place another knight in this cell. There are no more free cells which are attacked by $$$4$$$ or more knights, so the process stops. There are $$$9$$$ knights in the end, which is not less than $$$\\lfloor \\frac{7^{2}}{10} \\rfloor = 4$$$."}, "positive_code": [{"source_code": "n=gets.to_i\n(n-n/3).times do |i|\n print \"0 #{i+1}\\n\"\nend\n(n/3).times do |i|\n print \"3 #{(i+1)*2}\\n\"\nend"}], "negative_code": [{"source_code": "n=gets.to_i\n(n/3).times do |i|\n print \"0 #{i+1}\\n\"\nend\n(n-n/3).times do |i|\n print \"3 #{(i+1)*2}\\n\"\nend"}], "src_uid": "ab58981ba0954362be1a1b3a30c5d2f5"} {"nl": {"description": "You are given a following process. There is a platform with $$$n$$$ columns. $$$1 \\times 1$$$ squares are appearing one after another in some columns on this platform. If there are no squares in the column, a square will occupy the bottom row. Otherwise a square will appear at the top of the highest square of this column. When all of the $$$n$$$ columns have at least one square in them, the bottom row is being removed. You will receive $$$1$$$ point for this, and all the squares left will fall down one row. You task is to calculate the amount of points you will receive.", "input_spec": "The first line of input contain 2 integer numbers $$$n$$$ and $$$m$$$ ($$$1 \\le n, m \\le 1000$$$) — the length of the platform and the number of the squares. The next line contain $$$m$$$ integer numbers $$$c_1, c_2, \\dots, c_m$$$ ($$$1 \\le c_i \\le n$$$) — column in which $$$i$$$-th square will appear.", "output_spec": "Print one integer — the amount of points you will receive.", "sample_inputs": ["3 9\n1 1 2 2 2 3 1 2 3"], "sample_outputs": ["2"], "notes": "NoteIn the sample case the answer will be equal to $$$2$$$ because after the appearing of $$$6$$$-th square will be removed one row (counts of the squares on the platform will look like $$$[2~ 3~ 1]$$$, and after removing one row will be $$$[1~ 2~ 0]$$$).After the appearing of $$$9$$$-th square counts will be $$$[2~ 3~ 1]$$$, and after removing one row it will look like $$$[1~ 2~ 0]$$$.So the answer will be equal to $$$2$$$."}, "positive_code": [{"source_code": "#bismillahir rahmanir rahim \n\nn,m = gets.split.map{ |x| x.to_i } \n\nnum = gets.split.map{ |x| x.to_i } \n\nx=num.count(1) \n\nfor i in 2..n\n y = num.count(i) \n x=y if x>y \n #puts i,x,y\nend \n\nputs x\n\n=begin\nfor i in 0...m \n puts num[i] \nend \n=end \n"}, {"source_code": "n,m,*a=$<.read.split.map &:to_i\nx=[0]*n\na.each{|e|x[e-1]+=1}\np x.min"}, {"source_code": "n_and_m = gets.chomp\nsquares = gets.chomp\n\nn_and_m = n_and_m.split(\" \")\n\nn = n_and_m[0].to_i\nm = n_and_m[1].to_i\n\nsquares = squares.split(\" \")\nsquares.map! {|s| s.to_i}\n\nplatform = Array.new(n, 0)\n\nsquares.each do |square|\n platform[square - 1] += 1\nend\n\nscore = 0\ntotal = 0\nlength = platform.length\n\nwhile true\n platform.map {|s| total += 1 if !s.equal?(0) }\n break if !total.equal?(length)\n score += 1\n platform.map! {|s| s -= 1}\n total = 0\nend\n\nputs score"}, {"source_code": "n, m = gets.split.map(&:to_i)\nar = gets.split.map(&:to_i)\ncounts = Array.new(n)\nn.times{|i| counts[i] = ar.count{|a| a == i+1}}\nputs counts.include?(nil) ? 0 : counts.min"}], "negative_code": [{"source_code": "n_and_m = gets.chomp\nsquares = gets.chomp\n\nn_and_m = n_and_m.split(\" \")\nputs n_and_m.inspect\nn = n_and_m[0].to_i\nm = n_and_m[1].to_i\n\nsquares = squares.split(\" \")\nsquares.map! {|s| s.to_i}\n\nplatform = Array.new(n, 0)\n\n#puts squares.inspect\n#puts \"el tamaño del platform es : #{platform.length}\"\nsquares.each do |square|\n platform[square - 1] += 1\nend\n\nscore = 0\ntotal = 0\nlength = platform.length\n\nwhile true\n platform.map {|s| total += 1 if !s.equal?(0) }\n break if !total.equal?(length)\n score += 1\n platform.map! {|s| s -= 1}\n total = 0\nend\n\nputs score"}], "src_uid": "c249103153c5006e9b37bf15a51fe261"} {"nl": {"description": "In one little known, but very beautiful country called Waterland, lives a lovely shark Valerie. Like all the sharks, she has several rows of teeth, and feeds on crucians. One of Valerie's distinguishing features is that while eating one crucian she uses only one row of her teeth, the rest of the teeth are \"relaxing\".For a long time our heroine had been searching the sea for crucians, but a great misfortune happened. Her teeth started to ache, and she had to see the local dentist, lobster Ashot. As a professional, Ashot quickly relieved Valerie from her toothache. Moreover, he managed to determine the cause of Valerie's developing caries (for what he was later nicknamed Cap).It turned that Valerie eats too many crucians. To help Valerie avoid further reoccurrence of toothache, Ashot found for each Valerie's tooth its residual viability. Residual viability of a tooth is a value equal to the amount of crucians that Valerie can eat with this tooth. Every time Valerie eats a crucian, viability of all the teeth used for it will decrease by one. When the viability of at least one tooth becomes negative, the shark will have to see the dentist again. Unhappy, Valerie came back home, where a portion of crucians was waiting for her. For sure, the shark couldn't say no to her favourite meal, but she had no desire to go back to the dentist. That's why she decided to eat the maximum amount of crucians from the portion but so that the viability of no tooth becomes negative. As Valerie is not good at mathematics, she asked you to help her to find out the total amount of crucians that she can consume for dinner.We should remind you that while eating one crucian Valerie uses exactly one row of teeth and the viability of each tooth from this row decreases by one.", "input_spec": "The first line contains three integers n, m, k (1 ≤ m ≤ n ≤ 1000, 0 ≤ k ≤ 106) — total amount of Valerie's teeth, amount of tooth rows and amount of crucians in Valerie's portion for dinner. Then follow n lines, each containing two integers: r (1 ≤ r ≤ m) — index of the row, where belongs the corresponding tooth, and c (0 ≤ c ≤ 106) — its residual viability. It's guaranteed that each tooth row has positive amount of teeth.", "output_spec": "In the first line output the maximum amount of crucians that Valerie can consume for dinner.", "sample_inputs": ["4 3 18\n2 3\n1 2\n3 6\n2 3", "2 2 13\n1 13\n2 12"], "sample_outputs": ["11", "13"], "notes": null}, "positive_code": [{"source_code": "s = gets.chomp\nns,ms,ks = s.split(/ /)\nn = ns.to_i\nm = ms.to_i\nk = ks.to_i\nh = Hash.new\nfor i in 1..n\n\ts2 = gets.chomp\n\trs,cs = s2.split(/ /)\n\tr = rs.to_i\n\tc = cs.to_i\n\t\n\tif h.has_key?(r)\n\t\th[r] = c if h[r] > c\n\telse\n\t\th[r] = c\n\tend\nend\nsum = 0\nh.each {|key,val|\n\tsum += val\n}\nret = [sum,k].min\nputs ret"}, {"source_code": "n, m, k = gets.split.map(&:to_i)\na = [nil] * n\nn.times do\n\tr, c = gets.split.map(&:to_i)\n\tr -= 1\n\ta[r] = a[r] ? [a[r], c].min : c \nend\np [a.compact.inject(:+), k].min\n"}, {"source_code": "n,m,k=gets.split.map(&:to_i)\na=Array.new(m)\nn.times{r,c=gets.split.map(&:to_i)\nr-=1\na[r]=a[r] ? [a[r],c].min : c}\nb=a.inject:+\np b>k ? k : b\n"}, {"source_code": "n,m,k=gets.split.map(&:to_i)\na=Array.new(m)\nn.times{r,c=gets.split.map(&:to_i)\nr-=1\na[r]=a[r] ? [a[r],c].min : c}\nb=a.inject:+\np b>k ? k : b\n"}, {"source_code": "n,m,k=gets.split.map(&:to_i)\na=Array.new(m)\nn.times{r,c=gets.split.map(&:to_i)\nr-=1\na[r]=a[r] ? [a[r],c].min : c}\nb=a.inject:+\np b>k ? k : b"}, {"source_code": "n,m,k=gets.split.map(&:to_i)\na=Array.new(m)\nn.times{r,c=gets.split.map(&:to_i)\nr-=1\na[r]=a[r] ? [a[r],c].min : c}\nb=a.inject:+\np b>k ? k : b\n"}, {"source_code": "n,m,k=gets.split.map(&:to_i)\na=Array.new(m)\nn.times{r,c=gets.split.map(&:to_i)\nr-=1\na[r]=a[r] ? [a[r],c].min : c}\nb=a.inject:+\np b>k ? k : b\n"}, {"source_code": "n,m,k=gets.split.map(&:to_i)\na=Array.new(m)\nn.times{r,c=gets.split.map(&:to_i)\nr-=1\na[r]=a[r] ? [a[r],c].min : c}\nb=a.inject:+\np b>k ? k : b"}, {"source_code": "#=================================\n# Better be sure than sorry. \n#=================================\n$stdin=File.open(\"./input.txt\",\"r\") if ENV[\"LOGNAME\"]==\"skydos\"\nrequire 'prime'\nrequire 'mathn'\nrequire 'matrix'\ninclude Math\n#=================================\n\nn,m,k=gets.split.map(&:to_i)\n\nhs=Hash.new(10**9)\nn.times{\n\tr, c=gets.split.map(&:to_i)\n\ths[r]=[c, hs[r]].min\n}\n\nputs [hs.values.inject(:+), k].min\n"}, {"source_code": "n,m,k=gets.split.map(&:to_i)\na=Array.new(m)\nn.times{r,c=gets.split.map(&:to_i)\nr-=1\na[r]=a[r] ? [a[r],c].min : c}\nb=a.inject:+\np b>k ? k : b\n"}, {"source_code": "s = gets.split(\" \")\nn = s[0].to_i\nm = s[1].to_i\nk = s[2].to_i\n\nrow = Array.new()\nfor i in 1..m\n row[i] = 9999999\nend\n\nfor i in 1..n\n s = gets.split(\" \")\n key = s[0].to_i\n value = s[1].to_i\n if (value < row[key])\n row[key] = value\n end\nend\n\nsum = 0\nfor i in 1..m\n sum += row[i]\nend\n\nputs sum < k ? sum : k"}, {"source_code": "n,m,k=gets.split.map(&:to_i)\na=Array.new(m)\nn.times{r,c=gets.split.map(&:to_i)\nr-=1\na[r]=a[r] ? [a[r],c].min : c}\nb=a.inject:+\np b>k ? k : b\n"}, {"source_code": "n,m,k=gets.split.map(&:to_i)\na=Array.new(m)\nn.times{r,c=gets.split.map(&:to_i)\nr-=1\na[r]=a[r] ? [a[r],c].min : c}\nb=a.inject:+\np b>k ? k : b\n"}, {"source_code": "n,m,k=gets.split.map(&:to_i)\na=Array.new(m)\nn.times{r,c=gets.split.map(&:to_i)\nr-=1\na[r]=a[r] ? [a[r],c].min : c}\nb=a.inject:+\np b>k ? k : b"}, {"source_code": "n,m,k=gets.split.map(&:to_i)\na=Array.new(m)\nn.times{r,c=gets.split.map(&:to_i)\nr-=1\na[r]=a[r] ? [a[r],c].min : c}\nb=a.inject:+\np b>k ? k : b\n"}], "negative_code": [], "src_uid": "65eb0f3ab35c4d95c1cbd39fc7a4227b"} {"nl": {"description": "Fishingprince is playing with an array $$$[a_1,a_2,\\dots,a_n]$$$. He also has a magic number $$$m$$$.He can do the following two operations on it: Select $$$1\\le i\\le n$$$ such that $$$a_i$$$ is divisible by $$$m$$$ (that is, there exists an integer $$$t$$$ such that $$$m \\cdot t = a_i$$$). Replace $$$a_i$$$ with $$$m$$$ copies of $$$\\frac{a_i}{m}$$$. The order of the other elements doesn't change. For example, when $$$m=2$$$ and $$$a=[2,3]$$$ and $$$i=1$$$, $$$a$$$ changes into $$$[1,1,3]$$$. Select $$$1\\le i\\le n-m+1$$$ such that $$$a_i=a_{i+1}=\\dots=a_{i+m-1}$$$. Replace these $$$m$$$ elements with a single $$$m \\cdot a_i$$$. The order of the other elements doesn't change. For example, when $$$m=2$$$ and $$$a=[3,2,2,3]$$$ and $$$i=2$$$, $$$a$$$ changes into $$$[3,4,3]$$$. Note that the array length might change during the process. The value of $$$n$$$ above is defined as the current length of the array (might differ from the $$$n$$$ in the input).Fishingprince has another array $$$[b_1,b_2,\\dots,b_k]$$$. Please determine if he can turn $$$a$$$ into $$$b$$$ using any number (possibly zero) of operations.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 10^4$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1\\le n\\le 5\\cdot 10^4$$$, $$$2\\le m\\le 10^9$$$). The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\\ldots,a_n$$$ ($$$1\\le a_i\\le 10^9$$$). The third line of each test case contains one integer $$$k$$$ ($$$1\\le k\\le 5\\cdot 10^4$$$). The fourth line of each test case contains $$$k$$$ integers $$$b_1,b_2,\\ldots,b_k$$$ ($$$1\\le b_i\\le 10^9$$$). It is guaranteed that the sum of $$$n+k$$$ over all test cases does not exceed $$$2\\cdot 10^5$$$.", "output_spec": "For each testcase, print Yes if it is possible to turn $$$a$$$ into $$$b$$$, and No otherwise. You can print each letter in any case (upper or lower).", "sample_inputs": ["5\n\n5 2\n\n1 2 2 4 2\n\n4\n\n1 4 4 2\n\n6 2\n\n1 2 2 8 2 2\n\n2\n\n1 16\n\n8 3\n\n3 3 3 3 3 3 3 3\n\n4\n\n6 6 6 6\n\n8 3\n\n3 9 6 3 12 12 36 12\n\n16\n\n9 3 2 2 2 3 4 12 4 12 4 12 4 12 4 4\n\n8 3\n\n3 9 6 3 12 12 36 12\n\n7\n\n12 2 4 3 4 12 56"], "sample_outputs": ["Yes\nYes\nNo\nYes\nNo"], "notes": "NoteIn the first test case of the sample, we can do the second operation with $$$i=2$$$: $$$[1,\\color{red}{2,2},4,2]\\to [1,\\color{red}{4},4,2]$$$.In the second testcase of the sample, we can: do the second operation with $$$i=2$$$: $$$[1,\\color{red}{2,2},8,2,2]\\to [1,\\color{red}{4},8,2,2]$$$. do the second operation with $$$i=4$$$: $$$[1,4,8,\\color{red}{2,2}]\\to [1,4,8,\\color{red}{4}]$$$. do the first operation with $$$i=3$$$: $$$[1,4,\\color{red}{8},4]\\to [1,4,\\color{red}{4,4},4]$$$. do the second operation with $$$i=2$$$: $$$[1,\\color{red}{4,4},4,4]\\to [1,\\color{red}{8},4,4]$$$. do the second operation with $$$i=3$$$: $$$[1,8,\\color{red}{4,4}]\\to [1,8,\\color{red}{8}]$$$. do the second operation with $$$i=2$$$: $$$[1,\\color{red}{8,8}]\\to [1,\\color{red}{16}]$$$."}, "positive_code": [{"source_code": "# gets.to_i\r\n# gets.split.map(&:to_i)\r\n\r\ndef f(x, m)\r\n cnt = 1\r\n while x % m == 0\r\n x /= m\r\n cnt *= m\r\n end\r\n return [x, cnt]\r\nend\r\n\r\ngets.to_i.times do\r\n n, m = gets.split.map(&:to_i)\r\n a = gets.split.map(&:to_i)\r\n k = gets.to_i\r\n b = gets.split.map(&:to_i)\r\n\r\n ac = []\r\n n.times do |i|\r\n num, cnt = f(a[i], m)\r\n if ac.empty?\r\n ac << [num, cnt]\r\n elsif ac[-1][0] == num\r\n ac[-1][1] += cnt\r\n else\r\n ac << [num, cnt]\r\n end\r\n end\r\n\r\n bc = []\r\n k.times do |i|\r\n num, cnt = f(b[i], m)\r\n if bc.empty?\r\n bc << [num, cnt]\r\n elsif bc[-1][0] == num\r\n bc[-1][1] += cnt\r\n else\r\n bc << [num, cnt]\r\n end\r\n end\r\n\r\n puts bc == ac ? \"Yes\" : \"No\"\r\nend\r\n"}, {"source_code": "split = ->xs, m {\r\n chunks = []\r\n xs.each do |x|\r\n xx = x\r\n xx /= m while xx % m == 0\r\n n, count = xx, x / xx\r\n if chunks.last && chunks.last[0] == n\r\n chunks.last[1] += count\r\n else\r\n chunks << [n, count]\r\n end\r\n end\r\n chunks\r\n}\r\n\r\ngets.to_i.times do\r\n n, m = gets.split.map(&:to_i)\r\n as = gets.split.map(&:to_i)\r\n gets\r\n bs = gets.split.map(&:to_i)\r\n\r\n ok = split[as, m] == split[bs, m]\r\n\r\n puts ok ? \"YES\" : \"NO\"\r\nend\r\n"}, {"source_code": "gets.to_i.times do\r\n n, m = gets.split.map(&:to_i)\r\n as = gets.split.map(&:to_i)\r\n gets\r\n bs = gets.split.map(&:to_i)\r\n\r\n powers = (0..30).map { |i| [m ** i, i] }.to_h\r\n\r\n split = []\r\n as.each do |a|\r\n aa = a\r\n aa /= m while aa % m == 0\r\n x, y = aa, a / aa\r\n if split.last && split.last[0] == x\r\n split.last[1] += y\r\n else\r\n split << [x, y]\r\n end\r\n end\r\n\r\n ok = true\r\n bs.each do |b|\r\n if split.empty?\r\n ok = false\r\n break\r\n end\r\n x, y = split[0]\r\n if b % x == 0 && p = powers[b / x]\r\n if y >= b / x\r\n split[0][1] -= b / x\r\n else\r\n ok = false\r\n end\r\n else\r\n ok = false\r\n end\r\n split.shift if split[0][1] == 0\r\n end\r\n\r\n ok &&= split.empty?\r\n\r\n puts ok ? \"YES\" : \"NO\"\r\nend\r\n"}], "negative_code": [{"source_code": "# gets.to_i\r\n# gets.split.map(&:to_i)\r\n\r\ndef f(x, m)\r\n cnt = 1\r\n while x % m == 0\r\n x /= m\r\n cnt *= m\r\n end\r\n return [x, cnt]\r\nend\r\n\r\ngets.to_i.times do\r\n n, m = gets.split.map(&:to_i)\r\n a = gets.split.map(&:to_i)\r\n k = gets.to_i\r\n b = gets.split.map(&:to_i)\r\n\r\n ac = []\r\n n.times do |i|\r\n if a[i] % m == 0\r\n num, cnt = f(a[i], m)\r\n if ac.empty?\r\n ac << [num, cnt]\r\n elsif ac[-1][0] == num\r\n ac[-1][1] += cnt\r\n else\r\n ac << [num, cnt]\r\n end\r\n else\r\n ac << [a[i], 1]\r\n end\r\n end\r\n\r\n bc = []\r\n k.times do |i|\r\n if b[i] % m == 0\r\n num, cnt = f(b[i], m)\r\n if bc.empty?\r\n bc << [num, cnt]\r\n elsif bc[-1][0] == num\r\n bc[-1][1] += cnt\r\n else\r\n bc << [num, cnt]\r\n end\r\n else\r\n bc << [b[i], 1]\r\n end\r\n end\r\n\r\n puts bc == ac ? \"Yes\" : \"No\"\r\nend\r\n"}, {"source_code": "# gets.to_i\r\n# gets.split.map(&:to_i)\r\n\r\ndef f(x, m)\r\n cnt = 1\r\n while x % m == 0\r\n x /= m\r\n cnt *= m\r\n end\r\n return [x, cnt]\r\nend\r\n\r\ngets.to_i.times do\r\n n, m = gets.split.map(&:to_i)\r\n a = gets.split.map(&:to_i)\r\n k = gets.to_i\r\n b = gets.split.map(&:to_i)\r\n\r\n ac = []\r\n n.times do |i|\r\n if a[i] % m == 0\r\n ac << f(a[i], m)\r\n else\r\n ac << [a[i], 1]\r\n end\r\n end\r\n\r\n bc = []\r\n k.times do |i|\r\n if b[i] % m == 0\r\n bc << f(b[i], m)\r\n else\r\n bc << [b[i], 1]\r\n end\r\n end\r\n\r\n ans_a = []\r\n num, cnt = ac[0]\r\n (ac.size-1).times do |i|\r\n num1, cnt1 = ac[i+1]\r\n if num == num1\r\n cnt += cnt1\r\n if ac.size == i+2\r\n ans_a << [num, cnt]\r\n end\r\n else\r\n ans_a << [num, cnt]\r\n num = num1\r\n cnt = cnt1\r\n end\r\n end\r\n\r\n ans_b = []\r\n num, cnt = bc[0]\r\n (bc.size-1).times do |i|\r\n num1, cnt1 = bc[i+1]\r\n if num == num1\r\n cnt += cnt1\r\n if bc.size == i+2\r\n ans_b << [num, cnt]\r\n end\r\n else\r\n ans_b << [num, cnt]\r\n num = num1\r\n cnt = cnt1\r\n end\r\n end\r\n\r\n puts ans_a == ans_b ? \"Yes\" : \"No\"\r\nend\r\n\r\n"}, {"source_code": "# gets.to_i\r\n# gets.split.map(&:to_i)\r\n\r\ndef f(x, m)\r\n cnt = 1\r\n while x % m == 0\r\n x /= m\r\n cnt *= m\r\n end\r\n return [x, cnt]\r\nend\r\n\r\ngets.to_i.times do\r\n n, m = gets.split.map(&:to_i)\r\n a = gets.split.map(&:to_i)\r\n k = gets.to_i\r\n b = gets.split.map(&:to_i)\r\n\r\n ac = []\r\n n.times do |i|\r\n if a[i] % m == 0\r\n ac << f(a[i], m)\r\n else\r\n ac << [a[i], 1]\r\n end\r\n end\r\n\r\n bc = []\r\n k.times do |i|\r\n if b[i] % m == 0\r\n bc << f(b[i], m)\r\n else\r\n bc << [b[i], 1]\r\n end\r\n end\r\n\r\n ans_a = []\r\n num, cnt = ac[0]\r\n (ac.size-1).times do |i|\r\n num1, cnt1 = ac[i+1]\r\n if num == num1\r\n cnt += cnt1\r\n if ac.size == i+2\r\n ans_a << [num, cnt]\r\n end\r\n else\r\n ans_a << [num, cnt]\r\n num = num1\r\n cnt = cnt1\r\n end\r\n end\r\n\r\n ans_b = []\r\n num, cnt = bc[0]\r\n (bc.size-1).times do |i|\r\n num1, cnt1 = bc[i+1]\r\n if num == num1\r\n cnt += cnt1\r\n if bc.size == i+2\r\n ans_b << [num, cnt]\r\n end\r\n else\r\n ans_b << [num, cnt]\r\n num = num1\r\n cnt = cnt1\r\n end\r\n end\r\n\r\n ok = true\r\n ans_a.size.times do |i|\r\n num1, cnt1 = ans_a[i]\r\n num2, cnt2 = ans_b[i]\r\n\r\n break ok = false if num1 != num2 || cnt1 != cnt2\r\n end\r\n\r\n puts ok ? \"Yes\" : \"No\"\r\nend\r\n\r\n"}, {"source_code": "# gets.to_i\r\n# gets.split.map(&:to_i)\r\n\r\ndef f(x, m)\r\n cnt = 1\r\n while x % m == 0\r\n x /= m\r\n cnt *= m\r\n end\r\n return [x, cnt]\r\nend\r\n\r\ngets.to_i.times do\r\n n, m = gets.split.map(&:to_i)\r\n a = gets.split.map(&:to_i)\r\n k = gets.to_i\r\n b = gets.split.map(&:to_i)\r\n\r\n ac = []\r\n n.times do |i|\r\n if a[i] % m == 0\r\n min, cnt = f(a[i], m)\r\n ac << [min, cnt]\r\n else\r\n ac << [a[i], 1]\r\n end\r\n end\r\n\r\n bc = []\r\n k.times do |i|\r\n if b[i] % m == 0\r\n min, cnt = f(b[i], m)\r\n bc << [min, cnt]\r\n else\r\n bc << [b[i], 1]\r\n end\r\n end\r\n\r\n ans_a = []\r\n num, cnt = ac[0]\r\n (ac.size-1).times do |i|\r\n num1, cnt1 = ac[i+1]\r\n if num == num1\r\n cnt += cnt1\r\n if ac.size == i+2\r\n ans_a << [num, cnt]\r\n end\r\n else\r\n ans_a << [num, cnt]\r\n num = num1\r\n cnt = cnt1\r\n end\r\n end\r\n\r\n ans_b = []\r\n num, cnt = bc[0]\r\n (bc.size-1).times do |i|\r\n num1, cnt1 = bc[i+1]\r\n if num == num1\r\n cnt += cnt1\r\n if bc.size == i+2\r\n ans_b << [num, cnt]\r\n end\r\n else\r\n ans_b << [num, cnt]\r\n num = num1\r\n cnt = cnt1\r\n end\r\n end\r\n\r\n ok = false\r\n\r\n if ans_a.size == ans_b.size\r\n ok = true\r\n ans_a.size.times do |i|\r\n next if ans_a[i] == ans_b[i]\r\n num1, cnt1 = ans_a[i]\r\n num2, cnt2 = ans_b[i]\r\n\r\n break ok = false if num1 != num2 || cnt1 != cnt2\r\n end\r\n end\r\n\r\n puts ok ? \"Yes\" : \"No\"\r\nend\r\n"}, {"source_code": "# gets.to_i\r\n# gets.split.map(&:to_i)\r\n\r\ndef f(x, m)\r\n while x % m == 0\r\n x /= m\r\n end\r\n return x\r\nend\r\n\r\ngets.to_i.times do\r\n n, m = gets.split.map(&:to_i)\r\n a = gets.split.map(&:to_i)\r\n k = gets.to_i\r\n b = gets.split.map(&:to_i)\r\n\r\n ac = []\r\n n.times do |i|\r\n if a[i] % m == 0\r\n min = f(a[i], m)\r\n ac << min\r\n else\r\n ac << a[i]\r\n end\r\n end\r\n\r\n bc = []\r\n k.times do |i|\r\n if b[i] % m == 0\r\n min = f(b[i], m)\r\n bc << min\r\n else\r\n bc << b[i]\r\n end\r\n end\r\n\r\n ans_a = ac.slice_when{|i,j| i != j}.map{|e| e[0]}\r\n ans_b = bc.slice_when{|i,j| i != j}.map{|e| e[0]}\r\n\r\n puts ans_a == ans_b ? \"Yes\" : \"No\"\r\nend\r\n\r\n"}, {"source_code": "# gets.to_i\r\n# gets.split.map(&:to_i)\r\n\r\ndef f(x, m)\r\n while x % m == 0\r\n x /= m\r\n end\r\n return x\r\nend\r\n\r\ngets.to_i.times do\r\n n, m = gets.split.map(&:to_i)\r\n a = gets.split.map(&:to_i)\r\n k = gets.to_i\r\n b = gets.split.map(&:to_i)\r\n\r\n ac = []\r\n n.times do |i|\r\n if a[i] % m == 0\r\n min = f(a[i], m)\r\n ac << min\r\n else\r\n ac << a[i]\r\n end\r\n end\r\n\r\n bc = []\r\n k.times do |i|\r\n if b[i] % m == 0\r\n min = f(b[i], m)\r\n bc << min\r\n else\r\n bc << b[i]\r\n end\r\n end\r\n\r\n ans_a = ac.slice_when{|i,j| i != j}.map{|e| e[0]}\r\n ans_b = bc.slice_when{|i,j| i != j}.map{|e| e[0]}\r\n\r\n ok = false\r\n if ans_a.size == ans_b.size\r\n ok = true\r\n ans_a.size.times do |i|\r\n num1 = ans_a[i]\r\n num2 = ans_b[i]\r\n\r\n break ok = false if num1 != num2\r\n end\r\n end\r\n\r\n puts ok ? \"Yes\" : \"No\"\r\nend\r\n"}, {"source_code": "# gets.to_i\r\n# gets.split.map(&:to_i)\r\n\r\ndef f(x, m)\r\n cnt = 1\r\n while x % m == 0\r\n x /= m\r\n cnt *= m\r\n end\r\n return [x, cnt]\r\nend\r\n\r\ngets.to_i.times do\r\n n, m = gets.split.map(&:to_i)\r\n a = gets.split.map(&:to_i)\r\n k = gets.to_i\r\n b = gets.split.map(&:to_i)\r\n\r\n ac = []\r\n n.times do |i|\r\n if a[i] % m == 0\r\n min, cnt = f(a[i], m)\r\n ac << [min, cnt]\r\n else\r\n ac << [a[i], 1]\r\n end\r\n end\r\n\r\n bc = []\r\n k.times do |i|\r\n if b[i] % m == 0\r\n min, cnt = f(b[i], m)\r\n bc << [min, cnt]\r\n else\r\n bc << [b[i], 1]\r\n end\r\n end\r\n\r\n ans_a = []\r\n num, cnt = ac[0]\r\n (ac.size-1).times do |i|\r\n num1, cnt1 = ac[i+1]\r\n if num == num1\r\n cnt += cnt1\r\n if ac.size == i+2\r\n ans_a << [num, cnt]\r\n end\r\n else\r\n ans_a << [num, cnt]\r\n num = num1\r\n cnt = cnt1\r\n end\r\n end\r\n\r\n ans_b = []\r\n num, cnt = bc[0]\r\n (bc.size-1).times do |i|\r\n num1, cnt1 = bc[i+1]\r\n if num == num1\r\n cnt += cnt1\r\n if bc.size == i+2\r\n ans_b << [num, cnt]\r\n end\r\n else\r\n ans_b << [num, cnt]\r\n num = num1\r\n cnt = cnt1\r\n end\r\n end\r\n\r\n ok = false\r\n if ans_a.size == ans_b.size\r\n ok = true\r\n ans_a.size.times do |i|\r\n num1, cnt1 = ans_a[i]\r\n num2, cnt2 = ans_b[i]\r\n\r\n break ok = false if num1 != num2\r\n end\r\n end\r\n\r\n puts ok ? \"Yes\" : \"No\"\r\nend\r\n"}, {"source_code": "# gets.to_i\r\n# gets.split.map(&:to_i)\r\n\r\ndef f(x, m)\r\n cnt = 0\r\n while x % m == 0\r\n x /= m\r\n cnt += 1\r\n end\r\n return [x, cnt]\r\nend\r\n\r\ngets.to_i.times do\r\n n, m = gets.split.map(&:to_i)\r\n a = gets.split.map(&:to_i)\r\n k = gets.to_i\r\n b = gets.split.map(&:to_i)\r\n\r\n ac = []\r\n n.times do |i|\r\n if a[i] % m == 0\r\n min, cnt = f(a[i], m)\r\n ac << [min, 1 << cnt]\r\n else\r\n ac << [a[i], 1]\r\n end\r\n end\r\n\r\n bc = []\r\n k.times do |i|\r\n if b[i] % m == 0\r\n min, cnt = f(b[i], m)\r\n bc << [min, 1 << cnt]\r\n else\r\n bc << [b[i], 1]\r\n end\r\n end\r\n\r\n ans_a = []\r\n num, cnt = ac[0]\r\n (ac.size-1).times do |i|\r\n num1, cnt1 = ac[i+1]\r\n if num == num1\r\n cnt += cnt1\r\n if ac.size == i+2\r\n ans_a << [num, cnt]\r\n end\r\n else\r\n ans_a << [num, cnt]\r\n num = num1\r\n cnt = cnt1\r\n end\r\n end\r\n\r\n ans_b = []\r\n num, cnt = bc[0]\r\n (bc.size-1).times do |i|\r\n num1, cnt1 = bc[i+1]\r\n if num == num1\r\n cnt += cnt1\r\n if bc.size == i+2\r\n ans_b << [num, cnt]\r\n end\r\n else\r\n ans_b << [num, cnt]\r\n num = num1\r\n cnt = cnt1\r\n end\r\n end\r\n\r\n ok = false\r\n\r\n if ans_a.size == ans_b.size\r\n ok = true\r\n ans_a.size.times do |i|\r\n next if ans_a[i] == ans_b[i]\r\n\r\n num1, cnt1 = ans_a[i]\r\n num2, cnt2 = ans_b[i]\r\n\r\n break ok = false if num1 != num2\r\n end\r\n end\r\n\r\n puts ok ? \"Yes\" : \"No\"\r\nend\r\n"}, {"source_code": "# gets.to_i\r\n# gets.split.map(&:to_i)\r\n\r\ndef f(x, m)\r\n cnt = 0\r\n while x % m == 0\r\n x /= m\r\n cnt += 1\r\n end\r\n return [x, cnt]\r\nend\r\n\r\ngets.to_i.times do\r\n n, m = gets.split.map(&:to_i)\r\n a = gets.split.map(&:to_i)\r\n k = gets.to_i\r\n b = gets.split.map(&:to_i)\r\n\r\n ac = []\r\n n.times do |i|\r\n if a[i] % m == 0\r\n min, cnt = f(a[i], m)\r\n ac << [min, 1 << cnt]\r\n else\r\n ac << [a[i], 1]\r\n end\r\n end\r\n\r\n bc = []\r\n k.times do |i|\r\n if b[i] % m == 0\r\n min, cnt = f(b[i], m)\r\n bc << [min, 1 << cnt]\r\n else\r\n bc << [b[i], 1]\r\n end\r\n end\r\n\r\n ans_a = []\r\n num, cnt = ac[0]\r\n (ac.size-1).times do |i|\r\n num1, cnt1 = ac[i+1]\r\n if num == num1\r\n cnt += cnt1\r\n if ac.size == i+2\r\n ans_a << [num, cnt]\r\n end\r\n else\r\n ans_a << [num, cnt]\r\n num = num1\r\n cnt = cnt1\r\n end\r\n end\r\n\r\n ans_b = []\r\n num, cnt = bc[0]\r\n (bc.size-1).times do |i|\r\n num1, cnt1 = bc[i+1]\r\n if num == num1\r\n cnt += cnt1\r\n if bc.size == i+2\r\n ans_b << [num, cnt]\r\n end\r\n else\r\n ans_b << [num, cnt]\r\n num = num1\r\n cnt = cnt1\r\n end\r\n end\r\n\r\n ok = false\r\n\r\n if ans_a.size == ans_b.size\r\n ok = true\r\n ans_a.size.times do |i|\r\n next if ans_a[i] == ans_b[i]\r\n num1, cnt1 = ans_a[i]\r\n num2, cnt2 = ans_b[i]\r\n\r\n break ok = false if num1 != num2\r\n\r\n if cnt1 != cnt2\r\n s = m-2\r\n break ok = false if s == 0\r\n break ok = false if (cnt1 < m && cnt2 < m) || (cnt2 - cnt1).abs % s != 0\r\n end\r\n end\r\n end\r\n\r\n puts ok ? \"Yes\" : \"No\"\r\nend\r\n\r\n"}, {"source_code": "# gets.to_i\r\n# gets.split.map(&:to_i)\r\n\r\ndef f(x, m)\r\n cnt = 0\r\n while x % m == 0\r\n x /= m\r\n cnt += 1\r\n end\r\n return [x, cnt]\r\nend\r\n\r\ngets.to_i.times do\r\n n, m = gets.split.map(&:to_i)\r\n a = gets.split.map(&:to_i)\r\n k = gets.to_i\r\n b = gets.split.map(&:to_i)\r\n\r\n ac = []\r\n n.times do |i|\r\n if a[i] % m == 0\r\n min, cnt = f(a[i], m)\r\n ac << [min, 1 << cnt]\r\n else\r\n ac << [a[i], 1]\r\n end\r\n end\r\n\r\n bc = []\r\n k.times do |i|\r\n if b[i] % m == 0\r\n min, cnt = f(b[i], m)\r\n bc << [min, 1 << cnt]\r\n else\r\n bc << [b[i], 1]\r\n end\r\n end\r\n\r\n ans_a = []\r\n num, cnt = ac[0]\r\n (ac.size-1).times do |i|\r\n num1, cnt1 = ac[i+1]\r\n if num == num1\r\n cnt += cnt1\r\n if ac.size == i+2\r\n ans_a << [num, cnt]\r\n end\r\n else\r\n ans_a << [num, cnt]\r\n num = num1\r\n cnt = cnt1\r\n end\r\n end\r\n\r\n ans_b = []\r\n num, cnt = bc[0]\r\n (bc.size-1).times do |i|\r\n num1, cnt1 = bc[i+1]\r\n if num == num1\r\n cnt += cnt1\r\n if bc.size == i+2\r\n ans_b << [num, cnt]\r\n end\r\n else\r\n ans_b << [num, cnt]\r\n num = num1\r\n cnt = cnt1\r\n end\r\n end\r\n\r\n ok = false\r\n if ans_a.size == ans_b.size\r\n\r\n ok = true\r\n ans_a.size.times do |i|\r\n next if ans_a[i] == ans_b[i]\r\n num1, cnt1 = ans_a[i]\r\n num2, cnt2 = ans_b[i]\r\n break ok = false if num1 != num2\r\n\r\n if cnt1 != cnt2\r\n break ok = false if cnt1 < m && cnt2 < m\r\n s = m-2\r\n if cnt1 < cnt2\r\n break ok = false if (cnt2 - cnt1) % s != 0\r\n else\r\n break ok = false if (cnt1 - cnt2) % s != 0\r\n end\r\n end\r\n end\r\n end\r\n\r\n puts ok ? \"Yes\" : \"No\"\r\nend\r\n\r\n"}], "src_uid": "2ff40423619cefd8c2fb35a18549c411"} {"nl": {"description": "You are given a number $$$n$$$ and an array $$$b_1, b_2, \\ldots, b_{n+2}$$$, obtained according to the following algorithm: some array $$$a_1, a_2, \\ldots, a_n$$$ was guessed; array $$$a$$$ was written to array $$$b$$$, i.e. $$$b_i = a_i$$$ ($$$1 \\le i \\le n$$$); The $$$(n+1)$$$-th element of the array $$$b$$$ is the sum of the numbers in the array $$$a$$$, i.e. $$$b_{n+1} = a_1+a_2+\\ldots+a_n$$$; The $$$(n+2)$$$-th element of the array $$$b$$$ was written some number $$$x$$$ ($$$1 \\le x \\le 10^9$$$), i.e. $$$b_{n+2} = x$$$; The array $$$b$$$ was shuffled. For example, the array $$$b=[2, 3, 7, 12 ,2]$$$ it could be obtained in the following ways: $$$a=[2, 2, 3]$$$ and $$$x=12$$$; $$$a=[3, 2, 7]$$$ and $$$x=2$$$. For the given array $$$b$$$, find any array $$$a$$$ that could have been guessed initially.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$). Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$). The second row of each test case contains $$$n+2$$$ integers $$$b_1, b_2, \\ldots, b_{n+2}$$$ ($$$1 \\le b_i \\le 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, output: \"-1\", if the array $$$b$$$ could not be obtained from any array $$$a$$$; $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$, otherwise. If there are several arrays of $$$a$$$, you can output any.", "sample_inputs": ["4\n3\n2 3 7 12 2\n4\n9 1 7 1 6 5\n5\n18 2 2 3 2 9 2\n3\n2 6 9 2 1"], "sample_outputs": ["2 3 7 \n-1\n2 2 2 3 9 \n1 2 6"], "notes": null}, "positive_code": [{"source_code": "t = gets.to_s.to_i\n\ndef solve_case1(a)\n orig_sum = a[-1]\n tmp = a.reduce(0) {|acc, x| acc + x } - orig_sum\n x_idx = nil\n 0.upto(a.size - 2) do |i|\n if tmp - a[i] == orig_sum\n x_idx = i\n end\n end\n return nil if !x_idx\n a.select.with_index {|x, i| (i != x_idx) && (i != a.size - 1)}\nend\n\ndef solve_case2(a)\n tmp = a.reduce(0) {|acc, x| acc + x } - a[-1] - a[-2]\n if tmp == a[-2]\n return a[0 .. -3]\n end\n return nil\nend\n\n1.upto(t) do |casenum|\n n = gets.to_s.to_i\n a = gets.to_s.split.map {|x| x.to_i }.sort\n\n # case 1 (the sum is the largest number)\n x = solve_case1(a)\n if x\n printf(\"%s\\n\", x.join(\" \"))\n next\n end\n\n # case 2\n x = solve_case2(a)\n if x\n printf(\"%s\\n\", x.join(\" \"))\n next\n end\n\n printf(\"-1\\n\")\nend\n"}, {"source_code": "t = gets.to_i\r\nt.times do\r\n\tn = gets.to_i\r\n\ta = gets.split.map(&:to_i).sort\r\n\ts = a.sum - a[n+1]\r\n\r\n\tidx = -1\r\n\tfor i in 0..n\r\n\t\tif s - a[i] == a[n+1]\r\n\t\t\tidx = i\r\n\t\t\tbreak\r\n\t\tend\r\n\tend\r\n\r\n\tif idx == -1\r\n\t\tif s == 2*a[n]\r\n\t\t\tidx = n\r\n\t\telse\r\n\t\t\tputs -1\r\n\t\t\tnext\r\n\t\tend\r\n\tend\r\n\r\n\tfor i in 0..n\r\n\t\tnext if i == idx\r\n\t\tprint a[i], \" \"\r\n\tend\r\n\tputs \"\"\r\nend\r\n"}], "negative_code": [{"source_code": "t = gets.to_s.to_i\n\ndef solve_case1(a)\n orig_sum = a[-1]\n tmp = a.reduce(0) {|acc, x| acc + x } - orig_sum\n x_idx = nil\n 0.upto(a.size - 2) do |i|\n if tmp - a[i] == orig_sum\n x_idx = i\n end\n end\n return nil if !x_idx\n a.select.with_index {|x, i| (i != x_idx) && (i != a.size - 1)}\nend\n\ndef solve_case2(a)\n a[-1] = a[-2]\n return solve_case1(a.sort)\nend\n\n1.upto(t) do |casenum|\n n = gets.to_s.to_i\n a = gets.to_s.split.map {|x| x.to_i }.sort\n\n # case 1 (the sum is the largest number)\n x = solve_case1(a)\n if x\n printf(\"%s\\n\", x.join(\" \"))\n next\n end\n\n # case 2\n x = solve_case2(a)\n if x\n printf(\"%s\\n\", x.join(\" \"))\n next\n end\n\n printf(\"-1\\n\")\nend\n"}, {"source_code": "t = gets.to_s.to_i\n\ndef solve_case1(a)\n orig_sum = a[-1]\n tmp = a.reduce(0) {|acc, x| acc + x } - orig_sum\n 0.upto(a.size - 2) do |i|\n if tmp - a[i] == orig_sum\n if i == 0\n return a[i + 1 .. -2]\n elsif i == a.size - 2\n return a[0 .. i - 1]\n else\n return a[0 .. i - 1] + a[i + 1 .. -2]\n end\n end\n end\n return nil\nend\n\ndef solve_case2(a)\n a[-1] = a[-2]\n return solve_case1(a.sort)\nend\n\n1.upto(t) do |casenum|\n n = gets.to_s.to_i\n a = gets.to_s.split.map {|x| x.to_i }.sort\n\n # case 1 (the sum is the largest number)\n x = solve_case1(a)\n if x\n printf(\"%s\\n\", x.join(\" \"))\n next\n end\n\n # case 2\n x = solve_case2(a)\n if x\n printf(\"%s\\n\", x.join(\" \"))\n next\n end\n\n printf(\"-1\\n\")\nend\n"}, {"source_code": "t = gets.to_s.to_i\n\ndef solve_case1(a)\n orig_sum = a[-1]\n tmp = a.reduce(0) {|acc, x| acc + x } - orig_sum\n 0.upto(a.size - 3) do |i|\n if tmp - a[i] == orig_sum\n if i == 0\n return a[i + 1 .. -2]\n elsif i == a.size - 3\n return a[0 .. i - 1]\n else\n return a[0 .. i - 1] + a[i + 1 .. -2]\n end\n end\n end\n return nil\nend\n\ndef solve_case2(a)\n a[-1] = a[-2]\n return solve_case1(a.sort)\nend\n\n1.upto(t) do |casenum|\n n = gets.to_s.to_i\n a = gets.to_s.split.map {|x| x.to_i }.sort\n\n # case 1 (the sum is the largest number)\n x = solve_case1(a)\n if x\n printf(\"%s\\n\", x.join(\" \"))\n next\n end\n\n # case 2\n x = solve_case2(a)\n if x\n printf(\"%s\\n\", x.join(\" \"))\n next\n end\n\n printf(\"-1\\n\")\nend\n"}, {"source_code": "t = gets.to_s.to_i\n\ndef solve_case1(a)\n orig_sum = a[-1]\n tmp = a.reduce(0) {|acc, x| acc + x } - orig_sum\n 0.upto(a.size - 2) do |i|\n if tmp - a[i] == orig_sum\n if i == 0\n return a[i + 1 .. -2]\n elsif i == a.size - 3\n return a[0 .. i - 1]\n else\n return a[0 .. i - 1] + a[i + 1 .. -2]\n end\n end\n end\n return nil\nend\n\ndef solve_case2(a)\n a[-1] = a[-2]\n return solve_case1(a.sort)\nend\n\n1.upto(t) do |casenum|\n n = gets.to_s.to_i\n a = gets.to_s.split.map {|x| x.to_i }.sort\n\n # case 1 (the sum is the largest number)\n x = solve_case1(a)\n if x\n printf(\"%s\\n\", x.join(\" \"))\n next\n end\n\n # case 2\n x = solve_case2(a)\n if x\n printf(\"%s\\n\", x.join(\" \"))\n next\n end\n\n printf(\"-1\\n\")\nend\n"}, {"source_code": "t = gets.to_s.to_i\n\ndef solve_case1(a)\n orig_sum = a[-1]\n tmp = a.reduce(0) {|acc, x| acc + x } - orig_sum\n 0.upto(a.size - 2) do |i|\n if tmp - a[i] == orig_sum\n if i == 0\n return a[i + 1 .. -2]\n elsif i == a.size - 3\n return a[0 .. i - 1]\n else\n return a[0 .. i - 1] + a[i + 1 .. -2]\n end\n end\n end\n return nil\nend\n\ndef solve_case2(a)\n x = a[-1]\n orig_sum = a[-2]\n tmp = a.reduce(0) {|acc, x| acc + x } - x - orig_sum\n if tmp == orig_sum\n return a[0 .. -3]\n end\n return nil\nend\n\n1.upto(t) do |casenum|\n n = gets.to_s.to_i\n a = gets.to_s.split.map {|x| x.to_i }.sort\n\n # case 1 (the sum is the largest number)\n x = solve_case1(a)\n if x\n printf(\"%s\\n\", x.join(\" \"))\n next\n end\n\n # case 2 (the X \n x = solve_case2(a)\n if x\n printf(\"%s\\n\", x.join(\" \"))\n next\n end\n\n printf(\"-1\\n\")\nend\n"}, {"source_code": "t = gets.to_i\r\nt.times do\r\n\tn = gets.to_i\r\n\ta = gets.split.map(&:to_i).sort\r\n\ts = a.sum - a[n+1]\r\n\r\n\tidx = -1\r\n\tfor i in 0..n\r\n\t\tif s - a[i] == a[n+1]\r\n\t\t\tidx = i\r\n\t\t\tbreak\r\n\t\tend\r\n\tend\r\n\r\n\tif idx == -1\r\n\t\tunless s == 2*a[n]\r\n\t\t\tputs -1\r\n\t\t\tnext\r\n\t\tend\r\n\tend\r\n\r\n\tfor i in 0..n\r\n\t\tnext if i == idx\r\n\t\tprint a[i], \" \"\r\n\tend\r\n\tputs \"\"\r\nend\r\n"}, {"source_code": "t = gets.to_i\r\nt.times do\r\n\tn = gets.to_i\r\n\ta = gets.split.map(&:to_i).sort\r\n\ts = a.sum - a[n+1]\r\n\r\n\tidx = -1\r\n\tfor i in 0..n\r\n\t\tif s - a[i] == a[n+1]\r\n\t\t\tidx = i\r\n\t\t\tbreak\r\n\t\tend\r\n\tend\r\n\r\n\tif idx == -1\r\n\t\tputs -1\r\n\t\tnext\r\n\tend\r\n\r\n\tfor i in 0..n\r\n\t\tnext if i == idx\r\n\t\tprint a[i], \" \"\r\n\tend\r\n\tputs \"\"\r\nend\r\n"}], "src_uid": "ce667a8fb60841c994aebedb35a3b0d8"} {"nl": {"description": "The hero of the Cut the Rope game is a little monster named Om Nom. He loves candies. And what a coincidence! He also is the hero of today's problem. One day, Om Nom visited his friend Evan. Evan has n candies of two types (fruit drops and caramel drops), the i-th candy hangs at the height of hi centimeters above the floor of the house, its mass is mi. Om Nom wants to eat as many candies as possible. At the beginning Om Nom can make at most x centimeter high jumps. When Om Nom eats a candy of mass y, he gets stronger and the height of his jump increases by y centimeters.What maximum number of candies can Om Nom eat if he never eats two candies of the same type in a row (Om Nom finds it too boring)?", "input_spec": "The first line contains two integers, n and x (1 ≤ n, x ≤ 2000) — the number of sweets Evan has and the initial height of Om Nom's jump. Each of the following n lines contains three integers ti, hi, mi (0 ≤ ti ≤ 1; 1 ≤ hi, mi ≤ 2000) — the type, height and the mass of the i-th candy. If number ti equals 0, then the current candy is a caramel drop, otherwise it is a fruit drop.", "output_spec": "Print a single integer — the maximum number of candies Om Nom can eat.", "sample_inputs": ["5 3\n0 2 4\n1 3 1\n0 8 3\n0 20 10\n1 5 5"], "sample_outputs": ["4"], "notes": "NoteOne of the possible ways to eat 4 candies is to eat them in the order: 1, 5, 3, 2. Let's assume the following scenario: Initially, the height of Om Nom's jump equals 3. He can reach candies 1 and 2. Let's assume that he eats candy 1. As the mass of this candy equals 4, the height of his jump will rise to 3 + 4 = 7. Now Om Nom can reach candies 2 and 5. Let's assume that he eats candy 5. Then the height of his jump will be 7 + 5 = 12. At this moment, Om Nom can reach two candies, 2 and 3. He won't eat candy 2 as its type matches the type of the previously eaten candy. Om Nom eats candy 3, the height of his jump is 12 + 3 = 15. Om Nom eats candy 2, the height of his jump is 15 + 1 = 16. He cannot reach candy 4. "}, "positive_code": [{"source_code": "$candies_amount, initial_jump_height = gets.split.map(&:to_i)\n$candies = []\n\n$candies_amount.times do\n candy = {}\n candy[:type], candy[:height], candy[:weight] = gets.split.map(&:to_i)\n $candies << candy\nend\n\n$candies.sort_by! {|t| -t[:weight]}\n\n$max_ate = 0\n# $tries = {0 => [], 1 => []}\n$ate = Array.new($candies_amount, false)\n\ndef find_max(eaten, last_eaten_type, current_jump_height)\n # if ($tries[last_eaten_type][eaten.size] || -1 ) >= current_jump_height\n # return\n # end\n\n # $tries[last_eaten_type][eaten.size] = current_jump_height\n\n to_eat_index = nil\n to_eat = nil\n $candies.each_with_index do |candy, i|\n if !$ate[i] && candy[:type] != last_eaten_type && candy[:height] <= current_jump_height\n to_eat_index = i\n to_eat = candy\n break\n end\n end\n\n if to_eat_index\n $ate[to_eat_index] = true\n find_max(eaten + [to_eat_index], to_eat[:type], current_jump_height + to_eat[:weight])\n $ate[to_eat_index] = false\n else\n $max_ate = eaten.size if eaten.size > $max_ate\n end\nend\n\nfind_max([], 0, initial_jump_height)\n# puts\n# puts\nfind_max([], 1, initial_jump_height)\nputs $max_ate\n"}, {"source_code": "$candies_amount, initial_jump_height = gets.split.map(&:to_i)\n$candies = []\n\n$candies_amount.times do\n candy = {}\n candy[:type], candy[:height], candy[:weight] = gets.split.map(&:to_i)\n $candies << candy\nend\n\n$candies.sort_by! {|t| -t[:weight]}\n\n$max_ate = 0\n$tries = {0 => [], 1 => []}\n$ate = Array.new($candies_amount, false)\n\ndef find_max(eaten, last_eaten_type, current_jump_height)\n if ($tries[last_eaten_type][eaten.size] || -1 ) >= current_jump_height\n return\n end\n\n $tries[last_eaten_type][eaten.size] = current_jump_height\n\n to_eat_index = nil\n to_eat = nil\n $candies.each_with_index do |candy, i|\n if !$ate[i] && candy[:type] != last_eaten_type && candy[:height] <= current_jump_height\n to_eat_index = i\n to_eat = candy\n break\n end\n end\n\n if to_eat_index\n $ate[to_eat_index] = true\n find_max(eaten + [to_eat_index], to_eat[:type], current_jump_height + to_eat[:weight])\n $ate[to_eat_index] = false\n else\n $max_ate = eaten.size if eaten.size > $max_ate\n end\nend\n\nfind_max([], 0, initial_jump_height)\n# puts\n# puts\nfind_max([], 1, initial_jump_height)\nputs $max_ate\n"}, {"source_code": "$candies_amount, initial_jump_height = gets.split.map(&:to_i)\n$candies = []\n\n$candies_amount.times do\n candy = {}\n candy[:type], candy[:height], candy[:weight] = gets.split.map(&:to_i)\n $candies << candy\nend\n\n$candies.sort_by! {|t| -t[:weight]}\n\n$max_ate = 0\n$tries = {0 => [], 1 => []}\n\ndef find_max(eaten, last_eaten_type, current_jump_height)\n if ($tries[last_eaten_type][eaten.size] || -1 ) >= current_jump_height\n return\n end\n\n $tries[last_eaten_type][eaten.size] = current_jump_height\n\n available = (0..$candies_amount-1).to_a - eaten\n available_of_type = available.keep_if {|i| $candies[i][:type] != last_eaten_type }\n available_of_height = available_of_type.keep_if {|i| $candies[i][:height] <= current_jump_height}\n\n if available_of_height.length > 0\n # puts available_of_height.join(', ')\n max_weighted = $candies[available_of_height.first]\n find_max(eaten + [available_of_height.first], max_weighted[:type], current_jump_height + max_weighted[:weight])\n else\n $max_ate = eaten.size if eaten.size > $max_ate\n end\nend\n\nfind_max([], 0, initial_jump_height)\n# puts\n# puts\nfind_max([], 1, initial_jump_height)\nputs $max_ate\n"}, {"source_code": "n,x = readline.strip.split.map(&:to_i)\n\ncandies = [[],[]]\nn.times{\n t,h,m = readline.strip.split.map(&:to_i)\n candies[t] << [h,m]\n}\n\n# all_candies.each{|candies| candies.sort_by!{|h,m| -h } }\n\ndef count_max_num(candies_group_now, candies_group_next_turn, height, count)\n # puts \"(#{candies_group_now}; #{candies_group_next_turn}; #{height}; #{count})\"\n target_candies = candies_group_now.select{|h,m| h <= height }\n # puts \" target:#{target_candies}\"\n \n if target_candies.empty?\n # puts \"count:#{count}\"\n return count\n end\n\n h,m = target_candies.max_by{|h,m| m}\n idx = candies_group_now.index([h,m])\n \n count_max_num(candies_group_next_turn, candies_group_now.dup.tap{|cnd| cnd.delete_at(idx) }, height + m, count + 1)\nend\n\nputs [count_max_num(candies[0], candies[1], x, 0), count_max_num(candies[1], candies[0], x, 0)].max\n"}], "negative_code": [{"source_code": "require 'ostruct'\n\n$candies_amount, initial_jump_height = gets.split.map(&:to_i)\n$candies = []\n\n$candies_amount.times do\n candy = OpenStruct.new\n candy.type, candy.height, candy.weight = gets.split.map(&:to_i)\n $candies << candy\nend\n\n$max_ate = 0\n\n$tries = {nil => [], 0 => [], 1 => []}\n\ndef find_max(index, ate, last_eaten_type, jump_height)\n if index == $candies_amount\n $max_ate = ate if ate > $max_ate\n return\n end\n\n tries = $tries[last_eaten_type][index]\n if tries && tries.detect {|t| t[:height] > jump_height && t[:ate] > ate}\n return\n end\n\n $tries[last_eaten_type][index] ||= []\n $tries[last_eaten_type][index] << {height: jump_height, ate: ate}\n\n current_candy = $candies[index]\n\n if last_eaten_type != current_candy.type && jump_height >= current_candy.height\n find_max(index+1, ate+1, current_candy.type, jump_height + current_candy.weight )\n end\n\n find_max(index+1, ate, last_eaten_type, jump_height)\nend\n\nfind_max(0, 0, nil, initial_jump_height)\nputs $max_ate"}, {"source_code": "require 'ostruct'\n\n$candies_amount, initial_jump_height = gets.split.map(&:to_i)\n$candies = []\n\n$candies_amount.times do\n candy = OpenStruct.new\n candy.type, candy.height, candy.weight = gets.split.map(&:to_i)\n $candies << candy\nend\n\n$max_ate = 0\n\n$tries = {nil => [], 0 => [], 1 => []}\n\ndef find_max(index, ate, last_eaten_type, jump_height)\n if index == $candies_amount\n $max_ate = ate if ate > $max_ate\n return\n end\n\n # tries = $tries[last_eaten_type][index]\n # if tries && tries.detect {|t| t[:height] >= jump_height && t[:ate] >= ate}\n # return\n # end\n\n # $tries[last_eaten_type][index] ||= []\n # $tries[last_eaten_type][index] << {height: jump_height, ate: ate}\n\n current_candy = $candies[index]\n\n if last_eaten_type != current_candy.type && jump_height >= current_candy.height\n find_max(index+1, ate+1, current_candy.type, jump_height + current_candy.weight )\n end\n\n find_max(index+1, ate, last_eaten_type, jump_height)\nend\n\nfind_max(0, 0, nil, initial_jump_height)\nputs $max_ate"}, {"source_code": "require 'ostruct'\n\n$candies_amount, initial_jump_height = gets.split.map(&:to_i)\n$candies = []\n\n$candies_amount.times do\n candy = OpenStruct.new\n candy.type, candy.height, candy.weight = gets.split.map(&:to_i)\n $candies << candy\nend\n\n$max_ate = 0\n\n$tries = {nil => [], 0 => [], 1 => []}\n\ndef find_max(index, ate, last_eaten_type, jump_height)\n if index == $candies_amount\n $max_ate = ate if ate > $max_ate\n return\n end\n\n tries = $tries[last_eaten_type][index]\n if tries && tries.detect {|t| t[:height] >= jump_height && t[:ate] >= ate}\n return\n end\n\n $tries[last_eaten_type][index] ||= []\n $tries[last_eaten_type][index] << {height: jump_height, ate: ate}\n\n current_candy = $candies[index]\n\n if last_eaten_type != current_candy.type && jump_height >= current_candy.height\n find_max(index+1, ate+1, current_candy.type, jump_height + current_candy.weight )\n end\n\n find_max(index+1, ate, last_eaten_type, jump_height)\nend\n\nfind_max(0, 0, 1, initial_jump_height)\nfind_max(0, 0, 0, initial_jump_height)\nputs $max_ate"}, {"source_code": "$candies_amount, initial_jump_height = gets.split.map(&:to_i)\n$candies = []\n\n$candies_amount.times do\n candy = {}\n candy[:type], candy[:height], candy[:weight] = gets.split.map(&:to_i)\n $candies << candy\nend\n\n$max_ate = 0\n\n$tries = {nil => [], 0 => [], 1 => []}\n\ndef find_max(index, ate, last_eaten_type, jump_height)\n if index == $candies_amount\n $max_ate = ate if ate > $max_ate\n return\n end\n\n tries = $tries[last_eaten_type][index]\n if tries && tries.detect {|t| t[:height] >= jump_height && t[:ate] >= ate}\n return\n end\n\n $tries[last_eaten_type][index] ||= []\n $tries[last_eaten_type][index] << {height: jump_height, ate: ate}\n\n if last_eaten_type != $candies[index][:type] && jump_height >= $candies[index][:height]\n find_max(index+1, ate+1, $candies[index][:type], jump_height + $candies[index][:weight] )\n end\n\n find_max(index+1, ate, last_eaten_type, jump_height)\nend\n\nfind_max(0, 0, nil, initial_jump_height)\nputs $max_ate"}], "src_uid": "6253f6217c58a66573b1ddc6962c372c"} {"nl": {"description": "There are n piles of pebbles on the table, the i-th pile contains ai pebbles. Your task is to paint each pebble using one of the k given colors so that for each color c and any two piles i and j the difference between the number of pebbles of color c in pile i and number of pebbles of color c in pile j is at most one.In other words, let's say that bi, c is the number of pebbles of color c in the i-th pile. Then for any 1 ≤ c ≤ k, 1 ≤ i, j ≤ n the following condition must be satisfied |bi, c - bj, c| ≤ 1. It isn't necessary to use all k colors: if color c hasn't been used in pile i, then bi, c is considered to be zero.", "input_spec": "The first line of the input contains positive integers n and k (1 ≤ n, k ≤ 100), separated by a space — the number of piles and the number of colors respectively. The second line contains n positive integers a1, a2, ..., an (1 ≤ ai ≤ 100) denoting number of pebbles in each of the piles.", "output_spec": "If there is no way to paint the pebbles satisfying the given condition, output \"NO\" (without quotes) . Otherwise in the first line output \"YES\" (without quotes). Then n lines should follow, the i-th of them should contain ai space-separated integers. j-th (1 ≤ j ≤ ai) of these integers should be equal to the color of the j-th pebble in the i-th pile. If there are several possible answers, you may output any of them.", "sample_inputs": ["4 4\n1 2 3 4", "5 2\n3 2 4 1 3", "5 4\n3 2 4 3 5"], "sample_outputs": ["YES\n1\n1 4\n1 2 4\n1 2 3 4", "NO", "YES\n1 2 3\n1 3\n1 2 3 4\n1 3 4\n1 1 2 3 4"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/env ruby\nn,k=gets.split.map(&:to_i)\na=gets.split.map(&:to_i)\nb=a.map{|e|\n\th=Hash.new(0)\n\tz=e.times.map{|i|\n\t\tn=i%k+1\n\t\th[n]+=1\n\t\tn\n\t}\n\t[z,h]\n}\nif b.combination(2).all?{|x,y|\n\t(1..k).all?{|e|(x[1][e]-y[1][e]).abs<=1}\n}\n\tputs :YES\n\tputs b.map{|e|e[0]*' '}\nelse\n\tputs :NO\nend"}, {"source_code": "n,k=gets.chomp.split.map(&:to_i)\na=gets.chomp.split.map(&:to_i)\nmax,min=a.max,a.min\nif max-min>k\n\tputs \"NO\"\nelse\n\tputs \"YES\"\n\tn.times do |i|\n\t\tj=0\n\t\twhile j k \n puts 'NO'\nelse\n puts 'YES'\n n.times {|i|\n puts (t * (w.data[i] / k) + t[0, w.data[i] % k]).join(' ')\n }\nend"}, {"source_code": "#!/usr/bin/ruby\n\n#read data\ninput_line = gets\nparams = input_line.split(' ')\nn = params[0].to_i\nk = params[1].to_i\n\na = gets.split(\" \").map {|i| i.to_i}\n\n#find max(a) and min(a)\n\nif ((a.max - a.min)>k)\n puts 'NO'\nelse\n puts 'YES'\n a.each do |a_i|\n res = ''\n (0...a_i).each do |a_i_j|\n res += ((a_i_j % k)+1).to_s + ' '\n end\n puts res.strip\n end\nend"}, {"source_code": "n,k = gets.split(\" \").map(&:to_i)\narr = gets.split(\" \").map(&:to_i)\n# col = arr.map{|a| a/k}\na,b = arr.minmax\n# puts \"#{col.minmax}\"\nans = true\nans = false if b - a > k\ncol_arr = (1..k).to_a\n\nx = (100 / k.to_f).ceil\ncol_arr *= x\n# puts \"#{col_arr.length}\"\nif ans\n\tputs \"YES\"\n\tarr.each do |a|\n\t\tputs col_arr[0..(a-1)].join(\" \")\n\tend\nelse\n\tputs \"NO\"\nend\n#"}, {"source_code": "n,k = gets.split(\" \").map(&:to_i)\narr = gets.split(\" \").map(&:to_i)\n# col = arr.map{|a| a/k}\na,b = arr.minmax\n# puts \"#{col.minmax}\"\nans = true\nans = false if b - a > k\ncol_arr = (1..k).to_a\n\nx = (100 / k.to_f).ceil\ncol_arr *= x\n# puts \"#{col_arr.length}\"\nif ans\n\tputs \"YES\"\n\tarr.each do |a|\n\t\tputs col_arr[0..(a-1)].join(\" \")\n\tend\nelse\n\tputs \"NO\"\nend"}, {"source_code": "inp = gets.split(' ')\nn = inp[0].to_i\nm = inp[1].to_i\n\npebbles = gets.split(' ')\ncolors = Array.new() { Array.new() }\ncount = Array.new(n) { Array.new(m, 0) }\n\nfor i in 0..(n - 1)\n\tpebbles[i] = pebbles[i].to_i\n\tcolors << Array.new(pebbles[i], 0)\nend\n\nfor i in 0..(n - 1)\n\tcolor = 0\n\tfor j in 0..(pebbles[i] - 1)\n\t\tcolors[i][j] = color\n\t\tcount[i][color] += 1\n\t\tcolor += 1\n\t\tif color >= m\n\t\t\tcolor = 0\n\t\tend\n\tend\n\tfor j in 0..(i - 1)\n\t\tfor k in 0..(m - 1)\n\t\t\tif (count[j][k] - count[i][k]).abs > 1\n\t\t\t\tputs \"NO\"\n\t\t\t\texit\n\t\t\tend\n\t\tend\n\tend\nend\n\nputs \"YES\"\ncolors.each { |arr|\n\tarr.each { |el|\n\t\tprint \"#{el + 1} \"\n\t}\n\tputs\n}"}, {"source_code": "pa = Array.new\npb = Array.new\ndata = Array.new\n\nstr = gets.to_s\nstt = gets.to_s\npa = str.split\npb = stt.split\nn = pa[0].to_i\nk = pa[1].to_i\n\nfor i in 0..n-1\n data[i]=Array.new\n r = pb[i].to_i / k\n t = pb[i].to_i % k\n for j in 1..k\n if j <= t\n data[i][j] = r + 1\n else\n data[i][j] = r\n end\n end\nend\n\nflag = 0\nfor j in 1..k\n min = max = data[0][j]\n for i in 1..n-1\n if min > data[i][j]\n min = data[i][j]\n end\n if max < data[i][j]\n max = data[i][j]\n end\n end\n if max - min > 1\n flag = 1\n break\n end\nend\n\nif flag == 1\n puts \"NO\"\nelse\n puts \"YES\"\n for i in 0..n-1\n flag = 0\n for j in 1..k\n for r in 1..data[i][j]\n if flag == 0\n print j\n flag = 1\n else\n print \" \"\n print j\n end\n end\n end\n puts\n end\nend"}], "negative_code": [{"source_code": "n, k = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\n\ncolors = []\nn.times { colors << [] }\n\nmin = 0\nk.times do |i|\n min = (a.select { |x| x != 0}).min\n\n n.times do |j|\n min.times do |k|\n colors[j] << i + 1 if a[j] != 0\n end\n end\n\n break if min == 0\n\n a = a.map { |x| x == 0 ? 0 : x - min }\nend\n\nif a.max == 0\n puts \"YES\"\n n.times do |i|\n puts colors[i].join(\" \")\n end\nelse\n puts \"NO\"\nend"}, {"source_code": "n, k = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\n\ncolors = []\nn.times { colors << [] }\n\nmin = 0\nk.times do |i|\n if i == 0\n min = a.min\n else\n min = 1\n end\n\n n.times do |j|\n min.times do |k|\n colors[j] << i + 1 if a[j] != 0\n end\n end\n\n a = a.map { |x| x == 0 ? 0 : x - min }\nend\n\nif a.max == 0\n puts \"YES\"\n n.times do |i|\n puts colors[i].join(\" \")\n end\nelse\n puts \"NO\"\nend\n"}, {"source_code": "#!/usr/bin/ruby\n# coding: utf-8\n\nclass PileData\n attr_accessor :data\n def initialize(x)\n @data = x.split.map(&:to_i)\n end\nend\nn, k = gets.split.map(&:to_i)\nt = (1..k).map {|i| i}\n\nw = PileData.new(gets)\nif w.data.max - w.data.min > k \n p 'NO'\nelse\n n.times {|i|\n puts (t * (w.data[i] / k) + t[0, w.data[i] % k]).join(' ')\n }\nend"}, {"source_code": "#!/usr/bin/ruby\n# coding: utf-8\n\nclass PileData\n attr_accessor :data\n def initialize(x)\n @data = x.split.map(&:to_i)\n end\nend\nn, k = gets.split.map(&:to_i)\nt = (1..k).map {|i| i}\n\nw = PileData.new(gets)\nif w.data.max - w.data.min > k \n p 'NO'\nelse\n p 'YES'\n n.times {|i|\n puts (t * (w.data[i] / k) + t[0, w.data[i] % k]).join(' ')\n }\nend"}, {"source_code": "#!/usr/bin/ruby\n\n#read data\ninput_line = gets\nparams = input_line.split(' ')\nn = params[0].to_i\nk = params[1].to_i\n\na = gets.split(\" \").map {|i| i.to_i}\n\n#find max(a) and min(a)\n\nif ((a.max - a.min)>k)\n puts 'NO'\nelse\n puts 'YES'\n res = ''\n a.each do |a_i|\n res += ((a_i % k)+1).to_s + ' '\n puts res.strip\n end\nend"}, {"source_code": "n,k = gets.split(\" \").map(&:to_i)\narr = gets.split(\" \").map(&:to_i)\n# col = arr.map{|a| a/k}\na,b = arr.minmax\n# puts \"#{col.minmax}\"\nans = true\nans = false if b - a > k\ncol_arr = (1..k).to_a\n\nx = (100 / k.to_f).ceil\ncol_arr *= x\nputs \"#{col_arr.length}\"\nif ans\n\tputs \"YES\"\n\tarr.each do |a|\n\t\tputs col_arr[0..(a-1)].join(\" \")\n\tend\nelse\n\tputs \"NO\"\nend"}, {"source_code": "n,k = gets.split(\" \").map(&:to_i)\narr = gets.split(\" \").map(&:to_i)\ncol = arr.map{|a| a/k}\na,b = col.minmax\nans = true\nans = false if b - a > 1\ncol_arr = (1..k).to_a\n\nx = (100 / k.to_f).ceil\ncol_arr *= x\n# puts \"#{col_arr.length}\"\nif ans\n\tputs \"YES\"\n\tarr.each do |a|\n\t\tputs col_arr[0..(a-1)].join(\" \")\n\tend\nelse\n\tputs \"NO\"\nend"}, {"source_code": "#!/usr/bin/ruby\n\nn = gets.to_i\nm = gets.to_i\n\npebbles = Array.new(n)\ncolors = Array.new() { Array.new() }\ncount = Array.new(n) { Array.new(m, 0) }\n\nfor i in 0..(n - 1)\n\tpebbles[i] = gets.to_i\n\tcolors << Array.new(pebbles[i], 0)\nend\n\nfor i in 0..(n - 1)\n\tcolor = 0\n\tfor j in 0..(pebbles[i] - 1)\n\t\tcolors[i][j] = color\n\t\tcount[i][color] += 1\n\t\tcolor += 1\n\t\tif color >= m\n\t\t\tcolor = 0\n\t\tend\n\tend\n\tfor j in 0..(i - 1)\n\t\tfor k in 0..(m - 1)\n\t\t\tif (count[j][k] - count[i][k]).abs > 1\n\t\t\t\tputs \"NO\"\n\t\t\t\texit\n\t\t\tend\n\t\tend\n\tend\nend\n\nputs \"YES\"\ncolors.each { |arr|\n\tarr.each { |el|\n\t\tprint \"#{el + 1} \"\n\t}\n\tputs\n}"}, {"source_code": "n = gets.to_i\nm = gets.to_i\n\npebbles = Array.new(n)\ncolors = Array.new() { Array.new() }\ncount = Array.new(n) { Array.new(m, 0) }\n\nfor i in 0..(n - 1)\n\tpebbles[i] = gets.to_i\n\tcolors << Array.new(pebbles[i], 0)\nend\n\nfor i in 0..(n - 1)\n\tcolor = 0\n\tfor j in 0..(pebbles[i] - 1)\n\t\tcolors[i][j] = color\n\t\tcount[i][color] += 1\n\t\tcolor += 1\n\t\tif color >= m\n\t\t\tcolor = 0\n\t\tend\n\tend\n\tfor j in 0..(i - 1)\n\t\tfor k in 0..(m - 1)\n\t\t\tif (count[j][k] - count[i][k]).abs > 1\n\t\t\t\tputs \"NO\"\n\t\t\t\texit\n\t\t\tend\n\t\tend\n\tend\nend\n\nputs \"YES\"\ncolors.each { |arr|\n\tarr.each { |el|\n\t\tprint \"#{el + 1} \"\n\t}\n\tputs\n}"}, {"source_code": "#!/usr/bin/ruby\n\nif __FILE__ == $0\n\n\tn = ARGV[0].to_i\n\tm = ARGV[1].to_i\n\n\tpebbles = Array.new(n)\n\tcolors = Array.new() { Array.new() }\n\tcount = Array.new(n) { Array.new(m, 0) }\n\n\tfor i in 2..(n + 1)\n\t\tpebbles[i - 2] = ARGV[i].to_i\n\t\tcolors << Array.new(pebbles[i - 2], 0)\n\tend\n\n\tfor i in 0..(n - 1)\n\t\tcolor = 0\n\t\tfor j in 0..(pebbles[i] - 1)\n\t\t\tcolors[i][j] = color\n\t\t\tcount[i][color] += 1\n\t\t\tcolor += 1\n\t\t\tif color >= m\n\t\t\t\tcolor = 0\n\t\t\tend\n\t\tend\n\t\tfor j in 0..(i - 1)\n\t\t\tfor k in 0..(m - 1)\n\t\t\t\tif (count[j][k] - count[i][k]).abs > 1\n\t\t\t\t\tputs \"NO\"\n\t\t\t\t\texit\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\tend\n\n\tputs \"YES\"\n\tcolors.each { |arr|\n\t\tarr.each { |el|\n\t\t\tprint \"#{el + 1} \"\n\t\t}\n\t\tputs\n\t}\n\nend"}], "src_uid": "e512285d15340343e34f596de2be82eb"} {"nl": {"description": "You are given a string $$$s=s_1s_2\\dots s_n$$$ of length $$$n$$$, which only contains digits $$$1$$$, $$$2$$$, ..., $$$9$$$.A substring $$$s[l \\dots r]$$$ of $$$s$$$ is a string $$$s_l s_{l + 1} s_{l + 2} \\ldots s_r$$$. A substring $$$s[l \\dots r]$$$ of $$$s$$$ is called even if the number represented by it is even. Find the number of even substrings of $$$s$$$. Note, that even if some substrings are equal as strings, but have different $$$l$$$ and $$$r$$$, they are counted as different substrings.", "input_spec": "The first line contains an integer $$$n$$$ ($$$1 \\le n \\le 65000$$$) — the length of the string $$$s$$$. The second line contains a string $$$s$$$ of length $$$n$$$. The string $$$s$$$ consists only of digits $$$1$$$, $$$2$$$, ..., $$$9$$$.", "output_spec": "Print the number of even substrings of $$$s$$$.", "sample_inputs": ["4\n1234", "4\n2244"], "sample_outputs": ["6", "10"], "notes": "NoteIn the first example, the $$$[l, r]$$$ pairs corresponding to even substrings are: $$$s[1 \\dots 2]$$$ $$$s[2 \\dots 2]$$$ $$$s[1 \\dots 4]$$$ $$$s[2 \\dots 4]$$$ $$$s[3 \\dots 4]$$$ $$$s[4 \\dots 4]$$$ In the second example, all $$$10$$$ substrings of $$$s$$$ are even substrings. Note, that while substrings $$$s[1 \\dots 1]$$$ and $$$s[2 \\dots 2]$$$ both define the substring \"2\", they are still counted as different substrings."}, "positive_code": [{"source_code": "n = gets.to_i\ns = gets.chomp.chars.map(&:to_i)\nans = 0\n(0..n-1).each do |i|\n if s[i].even?\n ans += i + 1\n end\nend\nputs ans\n"}, {"source_code": "n = gets.to_i\ns = gets\nans = 0\nn.times do |i|\n if /[02468]/ =~ s[i]\n ans += i+1\n end\nend\nputs ans\n"}, {"source_code": "ans = 0\ngets\ngets.split('').each_with_index{|x, i|\n ans += i + 1 if ['0', '2', '4', '6', '8'].include? x\n}\nputs ans\n"}, {"source_code": "require 'enumerator'\ngets\ns = gets;\nputs s.enum_for(:scan, /[2468]/)\n .map { Regexp.last_match.begin(0) + 1 }\n .inject(0) {|r, p| r + p}"}], "negative_code": [], "src_uid": "43a65d1cfe59931991b6aefae1ecd10e"} {"nl": {"description": "Eshag has an array $$$a$$$ consisting of $$$n$$$ integers.Eshag can perform the following operation any number of times: choose some subsequence of $$$a$$$ and delete every element from it which is strictly larger than $$$AVG$$$, where $$$AVG$$$ is the average of the numbers in the chosen subsequence.For example, if $$$a = [1 , 4 , 3 , 2 , 4]$$$ and Eshag applies the operation to the subsequence containing $$$a_1$$$, $$$a_2$$$, $$$a_4$$$ and $$$a_5$$$, then he will delete those of these $$$4$$$ elements which are larger than $$$\\frac{a_1+a_2+a_4+a_5}{4} = \\frac{11}{4}$$$, so after the operation, the array $$$a$$$ will become $$$a = [1 , 3 , 2]$$$.Your task is to find the maximum number of elements Eshag can delete from the array $$$a$$$ by applying the operation described above some number (maybe, zero) times.A sequence $$$b$$$ is a subsequence of an array $$$c$$$ if $$$b$$$ can be obtained from $$$c$$$ by deletion of several (possibly, zero or all) elements.", "input_spec": "The first line contains an integer $$$t$$$ $$$(1\\le t\\le 100)$$$ — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1\\le n\\le 100)$$$ — the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ $$$(1\\le a_i \\le 100)$$$ — the elements of the array $$$a$$$.", "output_spec": "For each test case print a single integer — the maximum number of elements Eshag can delete from the array $$$a$$$.", "sample_inputs": ["3\n6\n1 1 1 2 2 3\n6\n9 9 9 9 9 9\n6\n6 4 1 1 4 1"], "sample_outputs": ["3\n0\n3"], "notes": "NoteConsider the first test case.Initially $$$a = [1, 1, 1, 2, 2, 3]$$$.In the first operation, Eshag can choose the subsequence containing $$$a_1$$$, $$$a_5$$$ and $$$a_6$$$, their average is equal to $$$\\frac{a_1 + a_5 + a_6}{3} = \\frac{6}{3} = 2$$$. So $$$a_6$$$ will be deleted.After this $$$a = [1, 1, 1, 2, 2]$$$.In the second operation, Eshag can choose the subsequence containing the whole array $$$a$$$, the average of all its elements is equal to $$$\\frac{7}{5}$$$. So $$$a_4$$$ and $$$a_5$$$ will be deleted.After this $$$a = [1, 1, 1]$$$.In the second test case, Eshag can't delete any element."}, "positive_code": [{"source_code": "def solve\n n = gets.to_i\n a = gets.split.map(&:to_i)\n m = a.min\n puts a.count { |x| x > m }\nend\n\nt = gets.to_i\nt.times do\n solve\nend"}, {"source_code": "def solve\r\n n = gets.to_i\r\n a = gets.split.map(&:to_i)\r\n m = a.min\r\n puts a.count { |x| x > m }\r\nend\r\n\r\nt = gets.to_i\r\nt.times do\r\n solve\r\nend"}, {"source_code": "t = gets.chomp.to_i\n\nt.times do \n n = gets.chomp.to_i\n arr = gets.chomp.split(\" \").map(&:to_i)\n arr.delete(arr.min)\n puts arr.size\nend\n"}, {"source_code": "def solution(n, arr)\r\n\taverage = arr.sum / n\r\n\r\n\tremoved = true\r\n\tresult = 0\r\n\r\n\twhile removed\r\n\t\ti = 0\r\n\t\tremoved = false\r\n\r\n\t\twhile i < n\r\n\t\t\tif arr[i] > average\r\n\t\t\t\tarr.delete_at(i)\r\n\t\t\t\tn -= 1\r\n\t\t\t\tresult += 1\r\n\t\t\t\tremoved = true\r\n\r\n\t\t\t\taverage = arr.sum / n\r\n\t\t\tend\r\n\r\n\t\t\ti += 1\r\n\t\tend\r\n\tend\r\n\r\n\tresult\r\nend\r\n\r\ntest_cases = gets.to_i\r\n\r\ntest_cases.times do\r\n n = gets.to_i\r\n arr = gets.chomp.split(' ').map(&:to_i)\r\n puts solution(n, arr)\r\nend\r\n"}, {"source_code": "t = gets.to_s.to_i\n1.upto(t) do |casenum|\n n = gets.to_s.to_i\n a = gets.to_s.split.map {|x| x.to_i }.sort\n i = 0\n i += 1 while a[i] == a[0]\n printf(\"%d\\n\", a.size - i)\nend\n"}], "negative_code": [], "src_uid": "d5627b9fe5f6c5a7247e1f9d9e9b0c6a"} {"nl": {"description": "You are given a string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters.Let's define a substring as a contiguous subsegment of a string. For example, \"acab\" is a substring of \"abacaba\" (it starts in position $$$3$$$ and ends in position $$$6$$$), but \"aa\" or \"d\" aren't substrings of this string. So the substring of the string $$$s$$$ from position $$$l$$$ to position $$$r$$$ is $$$s[l; r] = s_l s_{l + 1} \\dots s_r$$$.You have to choose exactly one of the substrings of the given string and reverse it (i. e. make $$$s[l; r] = s_r s_{r - 1} \\dots s_l$$$) to obtain a string that is less lexicographically. Note that it is not necessary to obtain the minimum possible string.If it is impossible to reverse some substring of the given string to obtain a string that is less, print \"NO\". Otherwise print \"YES\" and any suitable substring.String $$$x$$$ is lexicographically less than string $$$y$$$, if either $$$x$$$ is a prefix of $$$y$$$ (and $$$x \\ne y$$$), or there exists such $$$i$$$ ($$$1 \\le i \\le min(|x|, |y|)$$$), that $$$x_i < y_i$$$, and for any $$$j$$$ ($$$1 \\le j < i$$$) $$$x_j = y_j$$$. Here $$$|a|$$$ denotes the length of the string $$$a$$$. The lexicographic comparison of strings is implemented by operator < in modern programming languages​​.", "input_spec": "The first line of the input contains one integer $$$n$$$ ($$$2 \\le n \\le 3 \\cdot 10^5$$$) — the length of $$$s$$$. The second line of the input contains the string $$$s$$$ of length $$$n$$$ consisting only of lowercase Latin letters.", "output_spec": "If it is impossible to reverse some substring of the given string to obtain a string which is lexicographically less, print \"NO\". Otherwise print \"YES\" and two indices $$$l$$$ and $$$r$$$ ($$$1 \\le l < r \\le n$$$) denoting the substring you have to reverse. If there are multiple answers, you can print any.", "sample_inputs": ["7\nabacaba", "6\naabcfg"], "sample_outputs": ["YES\n2 5", "NO"], "notes": "NoteIn the first testcase the resulting string is \"aacabba\"."}, "positive_code": [{"source_code": "n = gets.chomp.to_i\n\nstr = gets.chomp\n\nsmall = str[0]\ncurr = 0\nlow = -1\nhigh = -1\n\n(n-1).times.with_index do |x|\n\tif str[x+1] < small\n\t\tlow = curr+1\n\t\thigh = x+2\n\t\tbreak\n\telse \n\t\tcurr = x+1\n\t\tsmall = str[x+1]\n\tend\nend\nif low != -1\n\tputs \"YES\"\n\tputs \"#{low} #{high}\"\nelse\n\tputs \"NO\"\nend\n\t"}, {"source_code": "n = gets.to_i\nstr = gets.chomp.chars\n\nif str == str.sort\n puts :NO\nelse\n (n - 1).times do |i|\n if str[i] > str[i + 1]\n right = i + 1\n left = i\n while str[left] <= str[right]\n left -= 1\n end\n puts :YES, [left + 1, right + 1].join(' ')\n break\n end\n end\nend\n\n"}], "negative_code": [{"source_code": "n = gets.chomp.to_i\n\nstr = gets.chomp\n\nsmall = str[0]\ncurr = 1\nlow = -1\nhigh = -1\n\n(n-1).times.with_index do |x|\n\tif str[x+1] < small\n\t\tlow = curr+1\n\t\thigh = x+2\n\t\tbreak\n\telse \n\t\tcurr = x+1\n\t\tsmall = str[x+1]\n\tend\nend\nif low != -1\n\tputs \"YES\"\n\tputs \"#{low} #{high}\"\nelse\n\tputs \"NO\"\nend\n\t"}, {"source_code": "n = gets.chomp.to_i\n\nstr = gets.chomp\n\nsmall = str[0]\ncurr = 1\nlow = -1\nhigh = -1\n\n(n-1).times.with_index do |x|\n\tif str[x+1] < small\n\t\tlow = curr+1\n\t\thigh = x+2\n\t\tbreak\n\telse \n\t\tcurr = x+1\n\t\tsmall = str[x+1]\n\tend\nend\nif low != \"-1\"\n\tp \"YES\"\n\tp \"#{low} #{high}\"\nelse\n\tp \"NO\"\nend\n\t"}], "src_uid": "d45f775613e701bbdaa4e457e6e189e2"} {"nl": {"description": "Businessman Divan loves chocolate! Today he came to a store to buy some chocolate. Like all businessmen, Divan knows the value of money, so he will not buy too expensive chocolate. At the same time, too cheap chocolate tastes bad, so he will not buy it as well.The store he came to has $$$n$$$ different chocolate bars, and the price of the $$$i$$$-th chocolate bar is $$$a_i$$$ dollars. Divan considers a chocolate bar too expensive if it costs strictly more than $$$r$$$ dollars. Similarly, he considers a bar of chocolate to be too cheap if it costs strictly less than $$$l$$$ dollars. Divan will not buy too cheap or too expensive bars.Divan is not going to spend all his money on chocolate bars, so he will spend at most $$$k$$$ dollars on chocolates.Please determine the maximum number of chocolate bars Divan can buy.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 100$$$). Description of the test cases follows. The description of each test case consists of two lines. The first line contains integers $$$n$$$, $$$l$$$, $$$r$$$, $$$k$$$ ($$$1 \\le n \\le 100$$$, $$$1 \\le l \\le r \\le 10^9$$$, $$$1 \\le k \\le 10^9$$$) — the lowest acceptable price of a chocolate, the highest acceptable price of a chocolate and Divan's total budget, respectively. The second line contains a sequence $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$) integers — the prices of chocolate bars in the store.", "output_spec": "For each test case print a single integer — the maximum number of chocolate bars Divan can buy.", "sample_inputs": ["8\n3 1 100 100\n50 100 50\n6 3 5 10\n1 2 3 4 5 6\n6 3 5 21\n1 2 3 4 5 6\n10 50 69 100\n20 30 40 77 1 1 12 4 70 10000\n3 50 80 30\n20 60 70\n10 2 7 100\n2 2 2 2 2 7 7 7 7 7\n4 1000000000 1000000000 1000000000\n1000000000 1000000000 1000000000 1000000000\n1 1 1 1\n1"], "sample_outputs": ["2\n2\n3\n0\n0\n10\n1\n1"], "notes": "NoteIn the first example Divan can buy chocolate bars $$$1$$$ and $$$3$$$ and spend $$$100$$$ dollars on them.In the second example Divan can buy chocolate bars $$$3$$$ and $$$4$$$ and spend $$$7$$$ dollars on them.In the third example Divan can buy chocolate bars $$$3$$$, $$$4$$$, and $$$5$$$ for $$$12$$$ dollars.In the fourth example Divan cannot buy any chocolate bar because each of them is either too cheap or too expensive.In the fifth example Divan cannot buy any chocolate bar because he considers the first bar too cheap, and has no budget for the second or third.In the sixth example Divan can buy all the chocolate bars in the shop."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n n, l, r, k = gets.split.map(&:to_i)\r\n as = gets.split.map(&:to_i)\r\n p as.sort.select { |a| a >= l && a <= r }.take_while { |a| k -= a; k >= 0 }.size\r\nend"}, {"source_code": "t = gets\r\n\r\nt = t.to_i\r\n\r\n(1..t).each do |e|\r\n inp = gets\r\n inp = inp.split(' ').map(&:to_i)\r\n a = gets\r\n a = a.split(' ').map(&:to_i)\r\n a.sort!\r\n #puts \"{a: #{a}\" \r\n cur = 0\r\n res = 0\r\n a.each do |x|\r\n if (cur+x) <= inp[3]\r\n if x >= inp[1] && x <= inp[2] \r\n cur += x\r\n res += 1\r\n end\r\n else\r\n break\r\n end\r\n end\r\n \r\n puts res\r\nend\r\n "}], "negative_code": [], "src_uid": "f577695d39a11e8507681f307677c883"} {"nl": {"description": "Let's say string $$$s$$$ has period $$$k$$$ if $$$s_i = s_{i + k}$$$ for all $$$i$$$ from $$$1$$$ to $$$|s| - k$$$ ($$$|s|$$$ means length of string $$$s$$$) and $$$k$$$ is the minimum positive integer with this property.Some examples of a period: for $$$s$$$=\"0101\" the period is $$$k=2$$$, for $$$s$$$=\"0000\" the period is $$$k=1$$$, for $$$s$$$=\"010\" the period is $$$k=2$$$, for $$$s$$$=\"0011\" the period is $$$k=4$$$.You are given string $$$t$$$ consisting only of 0's and 1's and you need to find such string $$$s$$$ that: String $$$s$$$ consists only of 0's and 1's; The length of $$$s$$$ doesn't exceed $$$2 \\cdot |t|$$$; String $$$t$$$ is a subsequence of string $$$s$$$; String $$$s$$$ has smallest possible period among all strings that meet conditions 1—3. Let us recall that $$$t$$$ is a subsequence of $$$s$$$ if $$$t$$$ can be derived from $$$s$$$ by deleting zero or more elements (any) without changing the order of the remaining elements. For example, $$$t$$$=\"011\" is a subsequence of $$$s$$$=\"10101\".", "input_spec": "The first line contains single integer $$$T$$$ ($$$1 \\le T \\le 100$$$) — the number of test cases. Next $$$T$$$ lines contain test cases — one per line. Each line contains string $$$t$$$ ($$$1 \\le |t| \\le 100$$$) consisting only of 0's and 1's.", "output_spec": "Print one string for each test case — string $$$s$$$ you needed to find. If there are multiple solutions print any one of them.", "sample_inputs": ["4\n00\n01\n111\n110"], "sample_outputs": ["00\n01\n11111\n1010"], "notes": "NoteIn the first and second test cases, $$$s = t$$$ since it's already one of the optimal solutions. Answers have periods equal to $$$1$$$ and $$$2$$$, respectively.In the third test case, there are shorter optimal solutions, but it's okay since we don't need to minimize the string $$$s$$$. String $$$s$$$ has period equal to $$$1$$$."}, "positive_code": [{"source_code": "gets.to_i.times {\n t=gets.chomp.chars\n if t.uniq.size==1\n s=t*''\n else\n s=t.shift\n t.each{|c|\n s+=(c==?0 ? ?1 : ?0) if s[-1]==c\n s+=c\n }\n end\n puts s\n}\n"}, {"source_code": "1.upto(gets.chomp.to_i) do\n t = gets.chomp\n at = t.split(//)\n if at.uniq.size == 1\n puts t\n else\n puts \"01\"*t.size\n end\nend\n"}, {"source_code": "tttt = gets.chomp.to_i\n\n# 周期性\ndef chkPeriod(str)\n\t(1..str.length/2).each do |i|\n\t\tngo = str.chars.each_slice(i).to_a\n\t\treturn i if ngo.uniq.length == 1\n\tend\n\treturn -1\nend\n\n#sがtのサブシーケンスか\ndef chkSubsequence(s, t)\n\tsi = 0\n\tti = 0\n\twhile si < s.length || ti < t.length\n\t\treturn false if si >= s.length\n\t\treturn true if ti >= t.length\n\t\tif s[si] == t[ti] then\n\t\t\tti += 1\n\t\tend\n\t\tsi += 1\n\tend\nend\n\n\ntttt.times do \n\tt = gets.chomp\n\tif t.chars.uniq.length == 1 then\n\t\tputs t\n\t\tnext\n\tend\n\n\t# 次にどの文字をぶちこむか(true→1 false→0)\n\tstr = \"\"\n\ti = 0\n\twhile i < t.length\n\t\tif t[i+1] != t[i] && i < t.length - 1 then\n\t\t\tstr += t[i]\n\t\t\ti += 1\n\t\t\tnext\n\t\tend\n\t\tstr += t[i]\n\t\tstr += t[i] == \"0\" ? \"1\" : \"0\"\n\t\ti += 1\n\tend\n\t#puts str\n\tstr = str[0] != str[-1] ? str : str.slice(0,str.length - 1)\n\tputs str\nend\n\n"}, {"source_code": "1.upto(gets.to_i) do\n s = gets.chomp\n \n same = s.chars.all? { |c| s[0] == c }\n\n while !same\n ll = s.length\n s.gsub!('00', '010')\n s.gsub!('11', '101')\n\n break if ll == s.length\n end\n\n puts s\nend\n"}, {"source_code": "def solution\n s = gets.chop!\n chars = s.chars\n zeros = chars.count(\"0\")\n ones = chars.count(\"1\")\n\n return s if zeros == 0 || ones == 0\n\n i = 0\n while i < s.size\n if s[i] == s[i + 1]\n s.insert(i + 1, s[i] == '0' ? '1' : '0')\n end\n i += 1\n end\n\n s\nend\n\nt = gets.to_i\nt.times do\n print \"#{solution}\\n\"\nend\n"}], "negative_code": [{"source_code": "def solution\n s = gets.chop!\n chars = s.chars\n zeros = chars.count(\"0\")\n ones = chars.count(\"1\")\n\n return s if zeros == 0 || ones == 0\n\n (0..s.size).to_a.each do |i|\n if s[i] != s[i + 2] && s[i + 2]\n s.insert(i + 1, s[i] == '0' ? '1' : '0')\n end\n end\n\n s\nend\n\nt = gets.to_i\nt.times do\n print \"#{solution}\\n\"\nend\n"}, {"source_code": "def solution\n s = gets.chop!\n \"10\" * (s.size / 2)\nend\n\nt = gets.to_i\nt.times do\n print \"#{solution}\\n\"\nend\n"}, {"source_code": "def solution\n s = gets.chop!\n chars = s.chars\n zeros = chars.count(\"0\")\n ones = chars.count(\"1\")\n\n return s if zeros == 0 || ones == 0\n\n chars.size.times do |i|\n if s[i] == s[i + 1]\n s.insert(i + 1, s[i] == '0' ? '1' : '0')\n end\n end\n\n s\nend\n\nt = gets.to_i\nt.times do\n print \"#{solution}\\n\"\nend\n"}, {"source_code": "def solution\n s = gets.chop!\n chars = s.chars\n zeros = chars.count(\"0\")\n ones = chars.count(\"1\")\n\n return s if zeros == 0 || ones == 0\n\n chars.size.times do |i|\n next if i == 0\n if s[i] == s[i-1]\n s.insert(i, s[i] == '0' ? '1' : '0')\n end\n end\n\n s\nend\n\nt = gets.to_i\nt.times do\n print \"#{solution}\\n\"\nend\n"}, {"source_code": "def solution\n s = gets.chop!\n chars = s.chars\n zeros = chars.count(\"0\")\n ones = chars.count(\"1\")\n\n return s if zeros == 0 || ones == 0\n\n (0..chars.size).to_a.each do |i|\n if s[i] != s[i + 2] && s[i + 2]\n s.insert(i + 1, s[i] == '0' ? '1' : '0')\n end\n end\n\n s\nend\n\nt = gets.to_i\nt.times do\n print \"#{solution}\\n\"\nend\n"}], "src_uid": "679a1e455073d3ea3856aa16516ba8ba"} {"nl": {"description": "High school student Vasya got a string of length n as a birthday present. This string consists of letters 'a' and 'b' only. Vasya denotes beauty of the string as the maximum length of a substring (consecutive subsequence) consisting of equal letters.Vasya can change no more than k characters of the original string. What is the maximum beauty of the string he can achieve?", "input_spec": "The first line of the input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n) — the length of the string and the maximum number of characters to change. The second line contains the string, consisting of letters 'a' and 'b' only.", "output_spec": "Print the only integer — the maximum beauty of the string Vasya can achieve by changing no more than k characters.", "sample_inputs": ["4 2\nabba", "8 1\naabaabaa"], "sample_outputs": ["4", "5"], "notes": "NoteIn the first sample, Vasya can obtain both strings \"aaaa\" and \"bbbb\".In the second sample, the optimal answer is obtained with the string \"aaaaabaa\" or with the string \"aabaaaaa\"."}, "positive_code": [{"source_code": "N, K = gets.chomp.split.map &:to_i\n\nstr = gets.chomp\n\ndef calcu str, base_chr, k\n count = 0\n pos = 0\n ret = 0\n for i in 0...str.length\n while pos < str.length && count <= k\n if str[pos] == base_chr\n count += 1\n end\n pos += 1\n end\n len = pos - i\n if str[pos-1] == base_chr && count > k\n len -= 1\n end\n \n if ret < len\n ret = len\n end\n \n if str[i] == base_chr\n count -= 1\n end\n end\n \n ret\nend\n\nans = [(calcu str, \"a\", K), (calcu str, \"b\", K)].max\nputs ans"}, {"source_code": "x , k = gets.strip.split(' ').map(&:to_i);\nn = gets.chomp\nc = 0\nl = 0\nbest = 0\nfor i in 0 ... x\n if (n[i] != 'a')\n c+=1\n end\n while(c > k)\n if (n[l] == 'b')\n c-=1\n end\n l+=1\n end\n best = [best , i - l + 1].max\nend\nc = 0\nl = 0\nfor i in 0 ... x\n if (n[i] != 'b')\n c+=1\n end\n\n while(c > k)\n if (n[l] == 'a')\n c-=1\n end\n l+=1\n end\n best = [best , i - l + 1].max\nend\nputs best\n"}], "negative_code": [{"source_code": "N, K = gets.chomp.split.map &:to_i\n\nstr = gets.chomp\n\ndef calcu str, base_chr, k\n count = 0\n pos = 0\n ret = 0\n for i in 0...str.length\n while pos < str.length && count <= k\n if str[pos] == base_chr\n count += 1\n end\n pos += 1\n end\n len = pos - i\n if str[pos-1] == base_chr && count > k\n len -= 1\n end\n \n if ret < len\n ret = len\n end\n \n if str[pos] == base_chr\n count -= 1\n end\n end\n \n ret\nend\n\nans = [(calcu str, \"a\", K), (calcu str, \"b\", K)].max\nputs ans"}, {"source_code": "N, K = gets.chomp.split.map &:to_i\n\nstr = gets.chomp\n\ndef calcu str, base_chr, k\n count = 0\n pos = 0\n ret = 0\n for i in 0...str.length\n while pos < str.length && count <= k\n if str[pos] == base_chr\n count += 1\n end\n pos += 1\n end\n len = pos - i\n if str[pos-1] == base_chr && count == k\n len -= 1\n end\n \n if ret < len\n ret = len\n end\n \n if str[pos] == base_chr\n count -= 1\n end\n end\n \n ret\nend\n\nans = [(calcu str, \"a\", K), (calcu str, \"b\", K)].max\nputs ans"}], "src_uid": "0151a87d0f82a9044a0ac8731d369bb9"} {"nl": {"description": "You have two positive integers $$$a$$$ and $$$b$$$.You can perform two kinds of operations: $$$a = \\lfloor \\frac{a}{b} \\rfloor$$$ (replace $$$a$$$ with the integer part of the division between $$$a$$$ and $$$b$$$) $$$b=b+1$$$ (increase $$$b$$$ by $$$1$$$) Find the minimum number of operations required to make $$$a=0$$$.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. The only line of the description of each test case contains two integers $$$a$$$, $$$b$$$ ($$$1 \\le a,b \\le 10^9$$$).", "output_spec": "For each test case, print a single integer: the minimum number of operations required to make $$$a=0$$$.", "sample_inputs": ["6\n9 2\n1337 1\n1 1\n50000000 4\n991026972 997\n1234 5678"], "sample_outputs": ["4\n9\n2\n12\n3\n1"], "notes": "NoteIn the first test case, one of the optimal solutions is: Divide $$$a$$$ by $$$b$$$. After this operation $$$a = 4$$$ and $$$b = 2$$$. Divide $$$a$$$ by $$$b$$$. After this operation $$$a = 2$$$ and $$$b = 2$$$. Increase $$$b$$$. After this operation $$$a = 2$$$ and $$$b = 3$$$. Divide $$$a$$$ by $$$b$$$. After this operation $$$a = 0$$$ and $$$b = 3$$$. "}, "positive_code": [{"source_code": "gets.to_i.times do\r\n a, b = gets.split.map &:to_i\r\n if a == 0\r\n puts 0\r\n next\r\n end\r\n res, i = a + 3, b < 2 ? 2 - b : 0\r\n while i < res do\r\n ca, cur = a, i\r\n while ca > 0 do\r\n ca /= b + i\r\n cur += 1\r\n end\r\n res = [res, cur].min\r\n i += 1\r\n end\r\n puts res\r\nend"}, {"source_code": "def method(a, b)\r\n if b == 1\r\n return 100\r\n end\r\n res = 0\r\n while a != 0\r\n a /= b\r\n res += 1\r\n end\r\n res\r\nend\r\n\r\nt = gets.to_i\r\nt.times do\r\n a, b = gets.split.map { |x| x.to_i }\r\n puts (0..30).map { |x| method(a, b + x) + x }.min\r\nend"}, {"source_code": "gets.to_i.times do\r\n a,b=gets.split.map(&:to_i)\r\n c=0\r\n if b==1\r\n b+=1\r\n c+=1\r\n end\r\n cmin=1/0.0\r\n cc=c\r\n bb=b\r\n aa=a\r\n while true\r\n a=aa\r\n b=bb\r\n c=cc\r\n while a>b\r\n a=a/b\r\n c+=1\r\n end\r\n c+=1 if a==b\r\n if c+1<=cmin\r\n cmin=c+1\r\n else\r\n puts cmin\r\n break\r\n end\r\n bb+=1\r\n cc+=1\r\n end\r\nend\r\n"}], "negative_code": [], "src_uid": "2b757fa66ce89046fe18cdfdeafa6660"} {"nl": {"description": "Today, Yasser and Adel are at the shop buying cupcakes. There are $$$n$$$ cupcake types, arranged from $$$1$$$ to $$$n$$$ on the shelf, and there are infinitely many of each type. The tastiness of a cupcake of type $$$i$$$ is an integer $$$a_i$$$. There are both tasty and nasty cupcakes, so the tastiness can be positive, zero or negative.Yasser, of course, wants to try them all, so he will buy exactly one cupcake of each type.On the other hand, Adel will choose some segment $$$[l, r]$$$ $$$(1 \\le l \\le r \\le n)$$$ that does not include all of cupcakes (he can't choose $$$[l, r] = [1, n]$$$) and buy exactly one cupcake of each of types $$$l, l + 1, \\dots, r$$$.After that they will compare the total tastiness of the cupcakes each of them have bought. Yasser will be happy if the total tastiness of cupcakes he buys is strictly greater than the total tastiness of cupcakes Adel buys regardless of Adel's choice.For example, let the tastinesses of the cupcakes be $$$[7, 4, -1]$$$. Yasser will buy all of them, the total tastiness will be $$$7 + 4 - 1 = 10$$$. Adel can choose segments $$$[7], [4], [-1], [7, 4]$$$ or $$$[4, -1]$$$, their total tastinesses are $$$7, 4, -1, 11$$$ and $$$3$$$, respectively. Adel can choose segment with tastiness $$$11$$$, and as $$$10$$$ is not strictly greater than $$$11$$$, Yasser won't be happy :(Find out if Yasser will be happy after visiting the shop.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 10^4$$$). The description of the test cases follows. The first line of each test case contains $$$n$$$ ($$$2 \\le n \\le 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$-10^9 \\le a_i \\le 10^9$$$), where $$$a_i$$$ represents the tastiness of the $$$i$$$-th type of cupcake. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.", "output_spec": "For each test case, print \"YES\", if the total tastiness of cupcakes Yasser buys will always be strictly greater than the total tastiness of cupcakes Adel buys regardless of Adel's choice. Otherwise, print \"NO\".", "sample_inputs": ["3\n4\n1 2 3 4\n3\n7 4 -1\n3\n5 -5 5"], "sample_outputs": ["YES\nNO\nNO"], "notes": "NoteIn the first example, the total tastiness of any segment Adel can choose is less than the total tastiness of all cupcakes.In the second example, Adel will choose the segment $$$[1, 2]$$$ with total tastiness $$$11$$$, which is not less than the total tastiness of all cupcakes, which is $$$10$$$.In the third example, Adel can choose the segment $$$[3, 3]$$$ with total tastiness of $$$5$$$. Note that Yasser's cupcakes' total tastiness is also $$$5$$$, so in that case, the total tastiness of Yasser's cupcakes isn't strictly greater than the total tastiness of Adel's cupcakes."}, "positive_code": [{"source_code": "T = gets.to_i\n\nT.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n\n var = -Float::INFINITY\n cur = 0\n len = 0\n\n a.each do |v|\n if cur < 0\n len = 1\n cur = v\n else\n len += 1\n cur += v\n end\n\n if var < cur && len < n\n var = cur\n end\n end\n\n cur = 0\n len = 0\n\n a.reverse_each do |v|\n if cur < 0\n len = 1\n cur = v\n else\n len += 1\n cur += v\n end\n\n if var < cur && len < n\n var = cur\n end\n end\n\n sum = a.inject(:+)\n\n if var >= sum\n puts 'NO'\n else\n puts 'YES'\n end\nend\n"}, {"source_code": "# frozen_string_literal: true\n\ndef one_test\n gets.to_i\n vec = gets.strip.split.map(&:to_i)\n# puts vec.to_s\n puts(test(vec) || test(vec.reverse) ? 'NO' : 'YES')\nend\n\ndef test(vector)\n vector = vector.clone\n vector.pop\n# puts vector.to_s\n sum = 0\n vector.each do |a|\n sum += a\n # puts sum.to_s + \" \" + a.to_s\n return true unless sum > 0\n end\n false\nend\n\nn = gets\nn.to_i.times { one_test }\n"}], "negative_code": [{"source_code": "T = gets.to_i\n\nT.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n\n var = -Float::INFINITY\n cur = 0\n len = 0\n max_len = 0\n\n a.each do |v|\n if cur < 0\n len = 1\n cur = v\n else\n len += 1\n cur += v\n end\n\n if var < cur\n max_len = len\n var = cur\n end\n end\n\n sum = a.inject(:+)\n\n if max_len < n && var >= sum\n puts 'NO'\n else\n puts 'YES'\n end\nend\n"}, {"source_code": "T = gets.to_i\n\nT.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n\n var = -Float::INFINITY\n cur = 0\n len = 0\n\n a.each do |v|\n if cur < 0\n len = 1\n cur = v\n else\n len += 1\n cur += v\n end\n\n if var < cur && len < n\n var = cur\n end\n end\n\n sum = a.inject(:+)\n\n if var >= sum\n puts 'NO'\n else\n puts 'YES'\n end\nend\n"}, {"source_code": "# frozen_string_literal: true\n\ndef one_test\n gets.to_i\n vec = gets.strip.split.map(&:to_i)\n puts vec.to_s\n puts(test(vec) || test(vec.reverse) ? 'NO' : 'YES')\nend\n\ndef test(vector)\n vector = vector.clone\n vector.pop\n puts vector.to_s\n sum = 0\n vector.each do |a|\n sum += a\n if a <= 0\n return true\n end\n end\n false\nend\n\nn = gets\nn.to_i.times { one_test }\n"}, {"source_code": "# frozen_string_literal: true\n\ndef one_test\n gets.to_i\n vec = gets.strip.split.map(&:to_i)\n# puts vec.to_s\n puts(test(vec) || test(vec.reverse) ? 'NO' : 'YES')\nend\n\ndef test(vector)\n vector = vector.clone\n vector.pop\n# puts vector.to_s\n sum = 0\n vector.each do |a|\n sum += a\n if a <= 0\n return true\n end\n end\n false\nend\n\nn = gets\nn.to_i.times { one_test }\n"}], "src_uid": "6e5b4d43e64645cf26d5eac31437b1a9"} {"nl": {"description": "Inflation has occurred in Berlandia, so the store needs to change the price of goods.The current price of good $$$n$$$ is given. It is allowed to increase the price of the good by $$$k$$$ times, with $$$1 \\le k \\le m$$$, k is an integer. Output the roundest possible new price of the good. That is, the one that has the maximum number of zeros at the end.For example, the number 481000 is more round than the number 1000010 (three zeros at the end of 481000 and only one at the end of 1000010).If there are several possible variants, output the one in which the new price is maximal.If it is impossible to get a rounder price, output $$$n \\cdot m$$$ (that is, the maximum possible price).", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) —the number of test cases in the test. Each test case consists of one line. This line contains two integers: $$$n$$$ and $$$m$$$ ($$$1 \\le n, m \\le 10^9$$$). Where $$$n$$$ is the old price of the good, and the number $$$m$$$ means that you can increase the price $$$n$$$ no more than $$$m$$$ times.", "output_spec": "For each test case, output on a separate line the roundest integer of the form $$$n \\cdot k$$$ ($$$1 \\le k \\le m$$$, $$$k$$$ — an integer). If there are several possible variants, output the one in which the new price (value $$$n \\cdot k$$$) is maximal. If it is impossible to get a more rounded price, output $$$n \\cdot m$$$ (that is, the maximum possible price).", "sample_inputs": ["10\n\n6 11\n\n5 43\n\n13 5\n\n4 16\n\n10050 12345\n\n2 6\n\n4 30\n\n25 10\n\n2 81\n\n1 7"], "sample_outputs": ["60\n200\n65\n60\n120600000\n10\n100\n200\n100\n7"], "notes": "NoteIn the first case $$$n = 6$$$, $$$m = 11$$$. We cannot get a number with two zeros or more at the end, because we need to increase the price $$$50$$$ times, but $$$50 > m = 11$$$. The maximum price multiple of $$$10$$$ would be $$$6 \\cdot 10 = 60$$$.In the second case $$$n = 5$$$, $$$m = 43$$$. The maximum price multiple of $$$100$$$ would be $$$5 \\cdot 40 = 200$$$.In the third case, $$$n = 13$$$, $$$m = 5$$$. All possible new prices will not end in $$$0$$$, then you should output $$$n \\cdot m = 65$$$.In the fourth case, you should increase the price $$$15$$$ times.In the fifth case, increase the price $$$12000$$$ times."}, "positive_code": [{"source_code": "zeros = ->n {\r\n count = 0\r\n while n % 10 == 0\r\n count += 1\r\n n /= 10\r\n end\r\n count\r\n}\r\n\r\ngets.to_i.times do\r\n n, m = gets.split.map(&:to_i)\r\n o = 1\r\n o *= 2 while o * 2 < m && zeros[n * o * 2] > zeros[n * o]\r\n o *= 5 while o * 5 < m && zeros[n * o * 5] > zeros[n * o]\r\n o *= 10 while o * 10 < m && zeros[n * o * 10] > zeros[n * o]\r\n o *= m / o\r\n p n * o\r\nend"}, {"source_code": "zeros = ->n { n.to_s.reverse.chars.take_while { |c| c == \"0\" }.size }\r\n\r\ngets.to_i.times do\r\n n, m = gets.split.map(&:to_i)\r\n o = 1\r\n o *= 2 while o * 2 < m && zeros[n * o * 2] > zeros[n * o]\r\n o *= 5 while o * 5 < m && zeros[n * o * 5] > zeros[n * o]\r\n o *= 10 while o * 10 < m && zeros[n * o * 10] > zeros[n * o]\r\n o *= m / o\r\n p n * o\r\nend\r\n"}, {"source_code": "gets.to_i.times do\n n,m=gets.chomp.split(' ').map(&:to_i)\n factor=1\n t=n\n while t%10==0 do\n t/=10\n end\n while t%2==0 && factor*5 <= m do\n t/=2\n factor*=5\n end\n while t%5==0 && factor*2 <= m do\n t/=5\n factor *=2\n end\n while factor*10<=m\n factor*=10\n end\n puts n*(m-m%factor)\nend\n\n"}], "negative_code": [], "src_uid": "83af7209bb8a81cde303af5d207c2749"} {"nl": {"description": "Alice has a string consisting of characters 'A', 'B' and 'C'. Bob can use the following transitions on any substring of our string in any order any number of times: A BC B AC C AB AAA empty string Note that a substring is one or more consecutive characters. For given queries, determine whether it is possible to obtain the target string from source.", "input_spec": "The first line contains a string S (1 ≤ |S| ≤ 105). The second line contains a string T (1 ≤ |T| ≤ 105), each of these strings consists only of uppercase English letters 'A', 'B' and 'C'. The third line contains the number of queries Q (1 ≤ Q ≤ 105). The following Q lines describe queries. The i-th of these lines contains four space separated integers ai, bi, ci, di. These represent the i-th query: is it possible to create T[ci..di] from S[ai..bi] by applying the above transitions finite amount of times? Here, U[x..y] is a substring of U that begins at index x (indexed from 1) and ends at index y. In particular, U[1..|U|] is the whole string U. It is guaranteed that 1 ≤ a ≤ b ≤ |S| and 1 ≤ c ≤ d ≤ |T|.", "output_spec": "Print a string of Q characters, where the i-th character is '1' if the answer to the i-th query is positive, and '0' otherwise.", "sample_inputs": ["AABCCBAAB\nABCB\n5\n1 3 1 2\n2 2 2 4\n7 9 1 1\n3 4 2 3\n4 5 1 3"], "sample_outputs": ["10011"], "notes": "NoteIn the first query we can achieve the result, for instance, by using transitions .The third query asks for changing AAB to A — but in this case we are not able to get rid of the character 'B'."}, "positive_code": [{"source_code": "$numAs=Array.new(1, 0)\n$numBs=Array.new(1, 0)\n$numAs1=Array.new(1, 0)\n$numBs1=Array.new(1, 0)\n\ndef f(a, b, c, d)\n nA=[b-a+1, $numAs[b]].min\n nB=$numBs[b]-$numBs[a-1]\n nA1=[d-c+1, $numAs1[d]].min\n nB1=$numBs1[d]-$numBs1[c-1]\n# print nA, nB, nA1, nB1, \"\\n\"\n if (nB==nB1)\n if (nA>=nA1 && (nA%3)==(nA1%3))\n print 1\n else\n print 0\n end\n elsif (nB=nA1)\n print 1\n else\n print 0\n end\n else\n print 0\n end\nend\n\ns=gets \ns1=gets \nn=gets.to_i\n(0..s.length-1).each{|x| $numBs.push($numBs[x]+(s[x]==\"B\" || s[x]==\"C\" ? 1 : 0 ))}\n(0..s1.length-1).each{|x| $numBs1.push($numBs1[x]+(s1[x]==\"B\" || s1[x]==\"C\" ? 1 : 0 ))}\n(0..s.length-1).each{|x| $numAs.push(s[x]=='A' ? $numAs[x]+1 : 0)}\n(0..s1.length-1).each{|x| $numAs1.push(s1[x]=='A' ? $numAs1[x]+1 : 0)}\nt=Array.new\nfor i in 0...n\n t=gets.split(' ').collect{|x| x.to_i}\n f(t[0], t[1], t[2], t[3])\nend\n\n\n"}], "negative_code": [{"source_code": "$numAs=Array.new(1, 0)\n$numBs=Array.new(1, 0)\n$numAs1=Array.new(1, 0)\n$numBs1=Array.new(1, 0)\n\ndef f(a, b, c, d)\n nA=[b-a+1, $numAs[b]].min\n nB=$numBs[b]-$numBs[a-1]\n nA1=[d-c+1, $numAs1[d]].min\n nB1=$numBs1[d]-$numBs1[c-1]\n# print nA, nB, nA1, nB1, \"\\n\"\n if (nB==nB1)\n if (nA>=nA1 && (nA%3)==(nA1%3))\n print 1\n else\n print 0\n end\n elsif (nBnA1)\n print 1\n else\n print 0\n end\n else\n print 0\n end\nend\n\ns=gets \ns1=gets \nn=gets.to_i\n(0..s.length-1).each{|x| $numBs.push($numBs[x]+(s[x]==\"B\" || s[x]==\"C\" ? 1 : 0 ))}\n(0..s1.length-1).each{|x| $numBs1.push($numBs1[x]+(s1[x]==\"B\" || s1[x]==\"C\" ? 1 : 0 ))}\n(0..s.length-1).each{|x| $numAs.push(s[x]=='A' ? $numAs[x]+1 : 0)}\n(0..s1.length-1).each{|x| $numAs1.push(s1[x]=='A' ? $numAs1[x]+1 : 0)}\nt=Array.new\nfor i in 0...n\n t=gets.split(' ').collect{|x| x.to_i}\n f(t[0], t[1], t[2], t[3])\nend\n\n\n"}, {"source_code": "$numAs=Array.new(1, 0)\n$numBs=Array.new(1, 0)\n$numAs1=Array.new(1, 0)\n$numBs1=Array.new(1, 0)\n\ndef f(a, b, c, d)\n nA=[b-a+1, $numAs[b]].min\n nB=$numBs[b]-$numBs[a-1]\n nA1=[d-c+1, $numAs1[d]].min\n nB1=$numBs1[d]-$numBs1[c-1]\n# print nA, nB, nA1, nB1, \"\\n\"\n if (nB==nB1)\n if (nA>=nA1 && (nA%3)==(nA1%3))\n print 1\n else\n print 0\n end\n elsif (nB=nA1)\n print 1\n else\n print 0\n end\n else\n print 0\n end\nend\n\ns=gets \ns1=gets \nn=gets.to_i\n(0..s.length-1).each{|x| $numBs.push($numBs[x]+(s[x]==\"B\" || s[x]==\"C\" ? 1 : 0 ))}\n(0..s1.length-1).each{|x| $numBs1.push($numBs1[x]+(s1[x]==\"B\" || s1[x]==\"C\" ? 1 : 0 ))}\n(0..s.length-1).each{|x| $numAs.push(s[x]=='A' ? $numAs[x]+1 : 0)}\n(0..s1.length-1).each{|x| $numAs1.push(s1[x]=='A' ? $numAs1[x]+1 : 0)}\nt=Array.new\nfor i in 0...n\n t=gets.split(' ').collect{|x| x.to_i}\n f(t[0], t[1], t[2], t[3])\nend\n\n\n"}], "src_uid": "98e3182f047a7e7b10be7f207b219267"} {"nl": {"description": "Authors guessed an array $$$a$$$ consisting of $$$n$$$ integers; each integer is not less than $$$2$$$ and not greater than $$$2 \\cdot 10^5$$$. You don't know the array $$$a$$$, but you know the array $$$b$$$ which is formed from it with the following sequence of operations: Firstly, let the array $$$b$$$ be equal to the array $$$a$$$; Secondly, for each $$$i$$$ from $$$1$$$ to $$$n$$$: if $$$a_i$$$ is a prime number, then one integer $$$p_{a_i}$$$ is appended to array $$$b$$$, where $$$p$$$ is an infinite sequence of prime numbers ($$$2, 3, 5, \\dots$$$); otherwise (if $$$a_i$$$ is not a prime number), the greatest divisor of $$$a_i$$$ which is not equal to $$$a_i$$$ is appended to $$$b$$$; Then the obtained array of length $$$2n$$$ is shuffled and given to you in the input. Here $$$p_{a_i}$$$ means the $$$a_i$$$-th prime number. The first prime $$$p_1 = 2$$$, the second one is $$$p_2 = 3$$$, and so on.Your task is to recover any suitable array $$$a$$$ that forms the given array $$$b$$$. It is guaranteed that the answer exists (so the array $$$b$$$ is obtained from some suitable array $$$a$$$). If there are multiple answers, you can print any.", "input_spec": "The first line of the input contains one integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$2n$$$ integers $$$b_1, b_2, \\dots, b_{2n}$$$ ($$$2 \\le b_i \\le 2750131$$$), where $$$b_i$$$ is the $$$i$$$-th element of $$$b$$$. $$$2750131$$$ is the $$$199999$$$-th prime number.", "output_spec": "In the only line of the output print $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$2 \\le a_i \\le 2 \\cdot 10^5$$$) in any order — the array $$$a$$$ from which the array $$$b$$$ can be obtained using the sequence of moves given in the problem statement. If there are multiple answers, you can print any.", "sample_inputs": ["3\n3 5 2 3 2 4", "1\n2750131 199999", "1\n3 6"], "sample_outputs": ["3 4 2", "199999", "6"], "notes": null}, "positive_code": [{"source_code": "MAX = 2750159\n$primes = (0..MAX).to_a\ndef generate_primes\n $primes[0] = $primes[1] = nil\n counter = 0\n $primes.each do |p|\n next unless p\n break if p*p > MAX\n counter += 1\n (p*p).step(MAX,p) { |m| $primes[m] = nil }\n end\n $primes = $primes.compact\nend\n\n\ndef bs(a, e)\n l, r = 0, a.size - 1\n while l <= r\n m = (l + r) / 2\n if a[m] < e\n l = m + 1\n elsif a[m] > e\n r = m - 1\n else\n return m\n end\n end\n -1\nend\n\ndef prime?(n)\n bs($primes, n) != -1\nend\n\ndef gd(n)\n return n / 2 if n % 2 == 0\n sq = Math.sqrt(n).to_i\n idx = 0\n while $primes[idx] <= sq\n return n / $primes[idx] if n % $primes[idx] == 0\n idx += 1\n end\nend\n\ndef solve(a)\n h = a.inject({}) { |m, e| m[e] = (m[e] || 0) + 1; m }\n rev = a.reverse\n res = []\n rev.each_with_index do |n, i|\n next if h[n].zero?\n\n if prime?(n)\n p_idx = bs($primes, n)\n res << p_idx + 1\n h[p_idx + 1] -= 1\n h[n] -= 1\n else\n div = gd(n)\n res << n\n h[div] -= 1\n h[n] -= 1\n end\n end\n res.join(' ')\nend\n\ngenerate_primes()\nn = gets.strip.to_i\na = gets.strip.split(' ').map(&:to_i).sort\nputs solve(a)\n"}], "negative_code": [], "src_uid": "07484b6a6915c5cb5fdf1921355f2a6a"} {"nl": {"description": "Email address in Berland is a string of the form A@B, where A and B are arbitrary strings consisting of small Latin letters. Bob is a system administrator in «Bersoft» company. He keeps a list of email addresses of the company's staff. This list is as a large string, where all addresses are written in arbitrary order, separated by commas. The same address can be written more than once.Suddenly, because of unknown reasons, all commas in Bob's list disappeared. Now Bob has a string, where all addresses are written one after another without any separators, and there is impossible to determine, where the boundaries between addresses are. Unfortunately, on the same day his chief asked him to bring the initial list of addresses. Now Bob wants to disjoin addresses in some valid way. Help him to do that.", "input_spec": "The first line contains the list of addresses without separators. The length of this string is between 1 and 200, inclusive. The string consists only from small Latin letters and characters «@».", "output_spec": "If there is no list of the valid (according to the Berland rules) email addresses such that after removing all commas it coincides with the given string, output No solution. In the other case, output the list. The same address can be written in this list more than once. If there are several solutions, output any of them.", "sample_inputs": ["a@aa@a", "a@a@a", "@aa@a"], "sample_outputs": ["a@a,a@a", "No solution", "No solution"], "notes": null}, "positive_code": [{"source_code": "gets\nputs !/@/ || /^@|@.?@|@$/ ? 'No solution' : $_.gsub(/@.(?=.*@)/){$&+','};\n"}, {"source_code": "\ndef do_repl(s)\n\ts.gsub(/@[a-z]+@/) { |m|\n\t\tm.slice(0,2) + \",\" + m.slice(2, m.length-2)\t\n\t}\nend\n\ndef badpattern(s)\n\treturn true if s.slice(0,1) == \"@\" || s.slice(s.length-1,1) == \"@\" \n\treturn true if /@@/ =~ s \n\treturn true if /@[a-z]@/ =~ s \n\treturn true if not /@/ =~ s \n\treturn false\nend\n\ndef test\n\ts = gets.chomp\n\tif badpattern(s) \n\t\tputs \"No solution\"\n\t\treturn\n\tend\n\n\ts = do_repl(s)\n\ts = do_repl(s)\n\tputs s\n\nend\n\ntest\n"}, {"source_code": "gets\nputs !/@/ || /^@|@.?@|@$/ ? 'No solution' : $_.gsub(/@.(?=.*@)/){$&+','};\n"}, {"source_code": "gets\nputs !/@/ || /^@|@.?@|@$/ ? 'No solution' : $_.gsub(/@.(?=.*@)/){$&+','};\n\n"}, {"source_code": "gets\nputs !/@/ || /^@|@.?@|@$/ ? 'No solution' : $_.gsub(/@.(?=.*@)/){$&+','};\n\n"}, {"source_code": "#!/usr/bin/env ruby\n\ns = gets.chomp\nt = s.split('@', -1)\nif t.size > 1 && !t[0].empty? && !t[-1].empty? && t[1..-2].each.map { |x| x.size > 1 }.all?\n result = []\n (t.size - 1).times do |i|\n result << t[i][-1] + '@' + t[i + 1][0..-2]\n end\n result[0] = t[0][0..-2] + result[0]\n result[-1] += t[-1][-1]\n puts result * ','\nelse\n puts \"No solution\"\nend\n"}, {"source_code": "gets\nputs !/@/ || /^@|@.?@|@$/ ? 'No solution' : $_.gsub(/@.(?=.*@)/){$&+','};\n"}, {"source_code": "gets\nputs !/@/ || /^@|@.?@|@$/ ? 'No solution' : $_.gsub(/@.(?=.*@)/){$&+','};\n\n"}, {"source_code": "gets\nputs !/@/ || /^@|@.?@|@$/ ? 'No solution' : $_.gsub(/@.(?=.*@)/){$&+','};\n\n"}, {"source_code": "emailString = gets.chomp.to_s\n\nresultString = String.new\nemailCandidate = String.new\nfinishCandidate = false;\n\nemailString.each_char {|letter|\n\n if emailCandidate.length == 0\n if letter == '@'\n resultString = 'No solution'\n break\n else\n emailCandidate += letter\n end\n else\n if letter == '@'\n if finishCandidate\n resultString = 'No solution'\n break\n else\n emailCandidate += letter\n finishCandidate = true;\n end\n else\n if finishCandidate\n emailCandidate += letter\n finishCandidate = false;\n\n if resultString.length == 0\n resultString += emailCandidate\n else\n resultString += ',' + emailCandidate\n end\n\n emailCandidate = ''\n else\n emailCandidate += letter\n end\n end\n end\n}\n\nif finishCandidate\n resultString = 'No solution'\nelse\n if resultString.length == 0\n resultString = 'No solution'\n else\n resultString += emailCandidate\n end\nend\n\nputs resultString"}, {"source_code": "gets\nputs !/@/ || /^@|@.?@|@$/ ? 'No solution' : $_.gsub(/@.(?=.*@)/){$&+','};"}, {"source_code": "s = gets.strip\nunless s[0] != ?@ && s[-1] != ?@ && s.chars.include?(?@) && !s.chars.each_cons(2).map(&:join).include?(\"@@\")\n s = \"No solution\"\nelse\n s = s.split(?@)\n (1...s.size).each do |i|\n if i + 1 == s.size\n s[i - 1] << \"@#{s[i]}\"\n s.pop\n else\n break s = \"No solution\" if s[i].size == 1\n s[i - 1] << \"@#{s[i][0]}\"\n s[i] = s[i][1..-1]\n end\n end\nend\nputs [*s].join(?,)\n"}, {"source_code": "#$stdin = File.open \"input.txt\"\n\ndef nosol\n\tputs \"No solution\"\n\texit\nend\n\na = gets.gsub(\"\\n\", \"\").split(\"@\", -1)\nn = a.size\n\nnosol if a.size < 2 || a[0].empty? || a[-1].empty? \ns = a[0]\n(1..n - 2).each do |i|\n\tnosol if a[i].size < 2\n\ts += \"@\" + a[i].chr + \",\" + a[i][1...a[i].size]\nend\ns += \"@\" + a[-1]\nputs s"}, {"source_code": "gets\nputs !/@/ || /^@|@.?@|@$/ ? 'No solution' : $_.gsub(/@.(?=.*@)/){$&+','};\n\n"}, {"source_code": "gets\nputs !/@/ || /^@|@.?@|@$/ ? 'No solution' : $_.gsub(/@.(?=.*@)/){$&+','};"}, {"source_code": "s = gets.chomp\narr = s.split(\"@\")\nat_count = s.count(\"@\")\nans = \"\"\nif arr.length >= 2 && (arr[1..-2].all?{|e| e.length > 1} && arr[0].length > 0 && arr[-1].length > 0)\n\tans << arr[0] + \"@\"\n\tat_count -= 1 \n\tarr[1..-2].each do |a|\n\t\tans << \"#{a[0]},#{a[1..-1]}@\"\n\t\tat_count -= 1\n\tend\n\tif at_count != 0\n\t\tputs \"No solution\"\n\telse\n\t\tputs ans+arr[-1]\n\tend\nelse\n\tputs \"No solution\"\nend"}, {"source_code": "s = gets\nmas = s.chomp!.split(\"@\")\n\nif mas.size < 2 || s[s.length-1..s.length-1] == \"@\" || s[0..0] == \"@\" || mas[0].length == 0 || mas[mas.size-1].length == 0\n puts \"No solution\"\n exit\nend\n\nbuffer = mas[0]\nfor i in 1..mas.size-2\n if mas[i].length < 2\n puts \"No solution\"\n exit\n end\n\n buffer += \"@\"+mas[i][0..(mas[i].length/2)-1]+\",\"+mas[i][(mas[i].length/2)..mas[i].length]\nend\nbuffer += \"@\"+mas[mas.size-1]\n\nputs buffer"}, {"source_code": "emails = gets.chomp.split(\"@\",-1)\nif emails.size < 2 || emails[0].size == 0 || emails[emails.size - 1].size == 0 || (emails.size > 2 && \n emails[1..emails.size - 2].any? {|x| x.size < 2})\n puts \"No solution\"\nelse\n i = 0\n while i + 1 < emails.size\n if i == 0\n print \"#{emails[0]}\"\n else\n print \"#{emails[i][1..emails[i].size]}\"\n end\n if i + 2 < emails.size\n print \"@#{emails[i+1][0]}\"\n else\n print \"@#{emails[i+1]}\"\n end\n print(\"#{emails.size > 2 && i + 2 < emails.size ? \",\" : \"\"}\")\n i += 1\n end\nend\n"}, {"source_code": "gets\nputs !/@/ || /^@|@.?@|@$/ ? 'No solution' : $_.gsub(/@.(?=.*@)/){$&+','};\n\n"}, {"source_code": "a = gets.chomp.split('@', -1)\nif a.size < 2 || [0, -1].any? { |i| a[i].empty? } || a[1..-2].any? { |x| x.size < 2 }\n puts 'No solution'\n exit\nend\n\nst = a[0]\na[1..-2].each { |x|\n print st + '@' + x[0] + ','\n st = x[1..-1]\n}\nputs st + '@' + a[-1]"}, {"source_code": "#!/usr/bin/ruby\n\nclass Solver\n \nend\n\nline = gets.chomp\n\nif line[0] == \"@\" || line[line.length-1] == \"@\"\n puts \"No solution\"\n exit 0\nend\n\ntokens = line.split(\"@\")\n\nif tokens.length == 1\n puts \"No solution\"\n exit 0\nend\n\n1.upto(tokens.length - 2) do |i|\n if tokens[i].length < 2\n puts \"No solution\"\n exit 0\n end\nend\n\ntokens[0] = \"-\" + tokens[0]\n0.upto(tokens.length - 3) do |i|\n print tokens[i].sub!(/^./, \"\"), \"@\", tokens[i+1][0], \",\"\nend\n\n\nputs \"#{tokens[tokens.length-2].sub!(/^./, \"\")}@#{tokens.last}\"\n"}, {"source_code": "gets\nputs !/@/ || /^@|@.?@|@$/ ? 'No solution' : $_.gsub(/@.(?=.*@)/){$&+','};\n\n"}, {"source_code": "gets\nputs !/@/ || /^@|@.?@|@$/ ? 'No solution' : $_.gsub(/@.(?=.*@)/){$&+','};\n\n"}], "negative_code": [{"source_code": "\ndef do_repl(s)\n\ts.gsub(/@[a-z]+@/) { |m|\n\t\tm.slice(0,2) + \",\" + m.slice(2, m.length-2)\t\n\t}\nend\n\ndef badpattern(s)\n\treturn true if s.slice(0,1) == \"@\" || s.slice(s.length-1,1) == \"@\" \n\treturn true if /@@/ =~ s \n\treturn true if /@[a-z]@/ =~ s \n\treturn true if not /@/ =~ s \n\treturn false\nend\n\ndef test\n\ts = gets\n\tif badpattern(s) \n\t\tputs \"No solution\"\n\t\treturn\n\tend\n\n\ts = do_repl(s)\n\ts = do_repl(s)\n\tputs s\n\nend\n\ntest\n"}, {"source_code": "\ndef do_repl(s)\n\ts.gsub(/@[^@,]+@/) { |m|\n\t\tm.slice(0,2) + \",\" + m.slice(2, m.length-2)\t\n\t}\nend\n\ndef test\n\ts = gets\n\tif s.slice(0,1) == \"@\" || s.slice(s.length-1,1) == \"@\" \n\t\tputs \"No solution\"\n\t\treturn\n\tend\n\n\tif /@[^@,]@/ =~ s \n\t\tputs \"No solution\"\n\t\treturn\n\tend\n\n\tif not /@/ =~ s \n\t\tputs \"No solution\"\n\t\treturn\n\tend\n\t\n\ts = do_repl(s)\n\ts = do_repl(s)\n\tputs s\n\nend\n\ntest\n"}, {"source_code": "def do_repl(s)\n\ts.gsub(/@[a-z]+@/) { |m|\n\t\tm.slice(0,2) + \",\" + m.slice(2, m.length-2)\t\n\t}\nend\n\ndef test\n\ts = gets\n\tif s.slice(0,1) == \"@\" || s.slice(s.length-1,1) == \"@\" \n\t\tputs \"No solution\"\n\t\treturn\n\tend\n\n\tif /@[a-z]@/ =~ s\n\t\tputs \"No solution\"\n\t\treturn\n\tend\n\t\n\ts = do_repl(s)\n\ts = do_repl(s)\n\tputs s\n\nend\n\ntest\n"}, {"source_code": "\ndef do_repl(s)\n\ts.gsub(/@[^@,]+@/) { |m|\n\t\tm.slice(0,2) + \",\" + m.slice(2, m.length-2)\t\n\t}\nend\n\ndef badpattern(s)\n\treturn true if s.slice(0,1) == \"@\" || s.slice(s.length-1,1) == \"@\" \n\treturn true if /@@/ =~ s \n\treturn true if /@[^@,]@/ =~ s \n\treturn true if not /@/ =~ s \n\treturn false\nend\n\ndef test\n\ts = gets\n\tif badpattern(s) \n\t\tputs \"No solution\"\n\t\treturn\n\tend\n\n\ts = do_repl(s)\n\ts = do_repl(s)\n\tputs s\n\nend\n\ntest\n"}, {"source_code": "\ndef do_repl(s)\n\ts.gsub(/@[^@,]+@/) { |m|\n\t\tm.slice(0,2) + \",\" + m.slice(2, m.length-2)\t\n\t}\nend\n\ndef test\n\ts = gets\n\tif s.slice(0,1) == \"@\" || s.slice(s.length-1,1) == \"@\" \n\t\tputs \"No solution\"\n\t\treturn\n\tend\n\n\tif /@[^@,]@/ =~ s\n\t\tputs \"No solution\"\n\t\treturn\n\tend\n\t\n\ts = do_repl(s)\n\ts = do_repl(s)\n\tputs s\n\nend\n\ntest\n"}, {"source_code": "\ndef do_repl(s)\n\ts.gsub(/@[^@]+@/) { |m|\n\t\tm.slice(0,2) + \",\" + m.slice(2, m.length-2)\t\n\t}\nend\n\ndef test\n\ts = gets\n\tif s.slice(0,1) == \"@\" || s.slice(s.length-1,1) == \"@\" \n\t\tputs \"No solution\"\n\t\treturn\n\tend\n\n\tif /@[^@]@/ =~ s\n\t\tputs \"No solution\"\n\t\treturn\n\tend\n\t\n\ts = do_repl(s)\n\ts = do_repl(s)\n\tputs s\n\nend\n\ntest\n"}, {"source_code": "\ndef do_repl(s)\n\ts.gsub(/@[a-z\\x80-\\xFF]+@/) { |m|\n\t\tm.slice(0,2) + \",\" + m.slice(2, m.length-2)\t\n\t}\nend\n\ndef test\n\ts = gets\n\tif s.slice(0,1) == \"@\" || s.slice(s.length-1,1) == \"@\" \n\t\tputs \"No solution\"\n\t\treturn\n\tend\n\n\tif /@[a-z\\x80-\\xFF]@/ =~ s\n\t\tputs \"No solution\"\n\t\treturn\n\tend\n\t\n\ts = do_repl(s)\n\ts = do_repl(s)\n\tputs s\n\nend\n\ntest\n"}, {"source_code": "gets\nputs /^@|@.?@|@$/ ? 'No solution' : $_.gsub(/@.(?=.*@)/){$&+','};\n"}, {"source_code": "#!/usr/bin/env ruby\n\nt = gets.chomp.split('@')\nif t.size > 1 && !t[0].empty? && !t[-1].empty? && t[1..-2].each.map { |x| x.size > 1 }.all?\n result = []\n (t.size - 1).times do |i|\n result << t[i][-1] + '@' + t[i + 1][0..-2]\n end\n result[0] = t[0][0..-2] + result[0]\n result[-1] += t[-1][-1]\n puts result * ','\nelse\n puts \"No solution\"\nend\n"}, {"source_code": "emailString = gets.chomp.to_s\n\nresultString = String.new\nemailCandidate = String.new\nfinishCandidate = false;\n\nemailString.each_char {|letter|\n\n if emailCandidate.length == 0\n if letter == '@'\n resultString = 'No solution'\n break\n else\n emailCandidate += letter\n end\n else\n if letter == '@'\n if finishCandidate\n resultString = 'No solution'\n break\n else\n emailCandidate += letter\n finishCandidate = true;\n end\n else\n if finishCandidate\n emailCandidate += letter\n finishCandidate = false;\n\n if resultString.length == 0\n resultString += emailCandidate\n else\n resultString += ',' + emailCandidate\n end\n\n emailCandidate = ''\n else\n emailCandidate += letter\n end\n end\n end\n}\n\nif finishCandidate\n resultString = 'No solution'\nelse\n resultString += emailCandidate\nend\n\nputs resultString"}, {"source_code": "s = gets.strip.split(?@)\n(1...s.size).each do |i|\n if i + 1 == s.size\n s[i - 1] << \"@#{s[i]}\"\n s.pop\n else\n break s = \"No solution\" if s[i].size == 1\n s[i - 1] << \"@#{s[i][0]}\"\n s[i] = s[i][1..-1]\n end\nend\n$><<[*s].join(?,)\n"}, {"source_code": "s = gets.strip\nunless s[0] != ?@ && s.chars.include?(?@)\n s = \"No solution\"\nelse\n s = s.split(?@)\n (1...s.size).each do |i|\n if i + 1 == s.size\n s[i - 1] << \"@#{s[i]}\"\n s.pop\n else\n break s = \"No solution\" if s[i].size == 1\n s[i - 1] << \"@#{s[i][0]}\"\n s[i] = s[i][1..-1]\n end\n end\nend\nputs [*s].join(?,)\n"}, {"source_code": "s = gets.strip\nunless s[0] != ?@ && s.chars.include?(?@) && !s.chars.each_cons(2).map(&:join).include?(\"@@\")\n s = \"No solution\"\nelse\n s = s.split(?@)\n (1...s.size).each do |i|\n if i + 1 == s.size\n s[i - 1] << \"@#{s[i]}\"\n s.pop\n else\n break s = \"No solution\" if s[i].size == 1\n s[i - 1] << \"@#{s[i][0]}\"\n s[i] = s[i][1..-1]\n end\n end\nend\nputs [*s].join(?,)\n"}, {"source_code": "s = gets.strip\nif s[0] == ?@\n s = \"No solution\"\nelse\n s = s.split(?@)\n (1...s.size).each do |i|\n if i + 1 == s.size\n s[i - 1] << \"@#{s[i]}\"\n s.pop\n else\n break s = \"No solution\" if s[i].size == 1\n s[i - 1] << \"@#{s[i][0]}\"\n s[i] = s[i][1..-1]\n end\n end\nend\n$><<[*s].join(?,)\n"}, {"source_code": "def nosol\n\tputs \"No solution\"\n\texit\nend\n\na = gets.split(\"@\")\nn = a.size\n\ns = a[0]\nnosol if s.empty? || a[-1].empty?\n(1..n - 2).each do |i|\n\tnosol if a[i].size < 2\n\ts += \"@\" + a[i].chr + \",\" + a[i][1...a.size]\nend\ns += \"@\" + a[-1]\nputs s"}, {"source_code": "a = gets.split(\"@\")\nn = a.size\n\ns = a[0]\n(1..n - 2).each do |i|\n\tif a[i].size < 2\n\t\tputs \"No solution\"\n\t\texit\n\tend\n\t\n\ts += \"@\" + a[i].chr + \",\" + a[i][1...a.size]\nend\ns += \"@\" + a[-1]\nputs s"}, {"source_code": "#$stdin = File.open \"input.txt\"\n\ndef nosol\n\tputs \"No solution\"\n\texit\nend\n\na = gets.gsub(\"\\n\", \"\").split(\"@\")\nn = a.size\n\nnosol if a.size < 2 || a[0].empty? || a[-1].empty? \ns = a[0]\n(1..n - 2).each do |i|\n\tnosol if a[i].size < 2\n\ts += \"@\" + a[i].chr + \",\" + a[i][1...a[i].size]\nend\ns += \"@\" + a[-1]\nputs s"}, {"source_code": "\ndef nosol\n puts \"No solution\"\n exit\nend\n\na = gets.gsub(\"\\n\", \"\").split(\"@\")\nn = a.size\n\nnosol if a.size < 2 || a[0].empty? || a[-1].empty? \ns = a[0]\n(1..n - 2).each do |i|\n nosol if a[i].size < 2\n s += \"@\" + a[i].chr + \",\" + a[i][1...a[i].size]\nend\ns += \"@\" + a[-1]\nputs s"}, {"source_code": "def nosol\n\tputs \"No solution\"\n\texit\nend\n\na = gets.split(\"@\")\nn = a.size\n\ns = a[0]\nnosol if s.empty? || a[-1].empty?\n(1..n - 2).each do |i|\n\tnosol if a[i].size < 2\n\ts += \"@\" + a[i].chr + \",\" + a[i][1...a.size]\nend\ns += \"@\" + a[-1]\nputs s"}, {"source_code": "def nosol\n puts \"No solution\"\n exit\nend\n\na = gets.split(\"@\")\nn = a.size\n\ns = a[0]\nnosol if s.empty? || a[-1].empty?\n(1..n - 2).each do |i|\n nosol if a[i].size < 2\n s += \"@\" + a[i].chr + \",\" + a[i][1...a[i].size]\nend\ns += \"@\" + a[-1]\nputs s"}, {"source_code": "arr = gets.chomp.split(\"@\")\nans = \"\"\nif arr.length >= 2 && (arr[1..-2].all?{|e| e.length > 1} && arr[0].length > 0 && arr[-1].length > 0)\n\tans << arr[0] + \"@\" \n\tarr[1..-2].each do |a|\n\t\tans << \"#{a[0]},#{a[1..-1]}@\"\n\tend\n\tputs ans+arr[-1]\nelse\n\tputs \"No solution\"\nend"}, {"source_code": "arr = gets.chomp.split(\"@\")\nans = \"\"\nif arr.length > 2 && (arr[1..-2].all?{|e| e.length > 1} && arr[0].length > 0 && arr[-1].length > 0)\n\tans << arr[0] + \"@\" \n\tarr[1..-2].each do |a|\n\t\tans << \"#{a[0]},#{a[1..-1]}@\"\n\tend\n\tputs ans+arr[-1]\nelse\n\tputs \"No solution\"\nend"}, {"source_code": "mas = gets.split(\"@\")\n\nif mas.size < 2 || mas[0].length == 0 || mas[mas.size-1].length == 0\n puts \"No solution\"\n exit\nend\n\nbuffer = mas[0]\nfor i in 1..mas.size-2\n if mas[i].length < 2\n puts \"No solution\"\n exit\n end\n\n buffer += \"@\"+mas[i][0..(mas[i].length/2)-1]+\",\"+mas[i][(mas[i].length/2)..mas[i].length]\nend\nbuffer += \"@\"+mas[mas.size-1]\n\nputs buffer"}, {"source_code": "mas = gets.chomp!.split(\"@\")\n\nif mas.size < 2 || mas[0].length == 0 || mas[mas.size-1].length == 0\n puts \"No solution\"\n exit\nend\n\nbuffer = mas[0]\nfor i in 1..mas.size-2\n if mas[i].length < 2\n puts \"No solution\"\n exit\n end\n\n buffer += \"@\"+mas[i][0..(mas[i].length/2)-1]+\",\"+mas[i][(mas[i].length/2)..mas[i].length]\nend\nbuffer += \"@\"+mas[mas.size-1]\n\nputs buffer"}, {"source_code": "mas = gets.chomp!.split(\"@\")\n\nif mas.size < 2 || mas[0].length == 0 || mas[mas.size-1].length == 0\n puts \"No solution\"\n exit\nend\n\nbuffer = mas[0]\nfor i in 1..mas.size-2\n if mas[i].length == 1\n puts \"No solution\"\n exit\n end\n\n buffer += \"@\"+mas[i][0..(mas[i].length/2)-1]+\",\"+mas[i][(mas[i].length/2)..mas[i].length]\nend\nbuffer += \"@\"+mas[mas.size-1]\n\nputs buffer"}, {"source_code": "s = gets\nmas = s.chomp!.split(\"@\")\n\nif mas.size < 2 || s[mas.length-1..mas.length-1] == \"@\" || s[0..0] == \"@\" || mas[0].length == 0 || mas[mas.size-1].length == 0\n puts \"No solution\"\n exit\nend\n\nbuffer = mas[0]\nfor i in 1..mas.size-2\n if mas[i].length < 2\n puts \"No solution\"\n exit\n end\n\n buffer += \"@\"+mas[i][0..(mas[i].length/2)-1]+\",\"+mas[i][(mas[i].length/2)..mas[i].length]\nend\nbuffer += \"@\"+mas[mas.size-1]\n\nputs buffer"}, {"source_code": "mas = gets.split(\"@\")\nputs mas.size\n\nif mas.size < 2 || mas[0].length == 0 || mas[mas.size-1].length == 0\n puts \"No solution\"\n exit\nend\n\nbuffer = mas[0]\nfor i in 1..mas.size-2\n if mas[i].length < 2\n puts \"No solution\"\n exit\n end\n\n buffer += \"@\"+mas[i][0..(mas[i].length/2)-1]+\",\"+mas[i][(mas[i].length/2)..mas[i].length]\nend\nbuffer += \"@\"+mas[mas.size-1]\n\nputs buffer"}, {"source_code": "emails = gets.chomp.split(\"@\")\nif emails.size < 2 || emails[0].size == 0 || emails[emails.size - 1].size == 0 || (emails.size > 2 && \n emails[1..emails.size - 2].any? {|x| x.size < 2})\n puts \"No solution\"\nelse\n i = 0\n while i + 1 < emails.size\n if i == 0\n print \"#{emails[0]}\"\n else\n print \"#{emails[i][1..emails[i].size]}\"\n end\n if i + 2 < emails.size\n print \"@#{emails[i+1][0]}\"\n else\n print \"@#{emails[i+1]}\"\n end\n print(\"#{emails.size > 2 && i + 2 < emails.size ? \",\" : \"\"}\")\n i += 1\n end\nend\n"}, {"source_code": "a = gets.chomp.split('@', -1)\nif a.size < 2 || [0, -1].any? { |i| a[i].empty? } || a[1..-2].any? { |x| x.size < 2 }\n puts 'No solution'\n exit\nend\n\nst = a[0]\na[1..-2].each { |x|\n puts st + '@' + x[0]\n st = x[1..-1]\n}\nputs st + '@' + a[-1]"}, {"source_code": "#!/usr/bin/ruby\n\nclass Solver\n \nend\n\nline = gets.chomp\n\nif line[0] == \"@\" || line[line.length-1] == \"@\"\n puts \"No solution\"\n exit 0\nend\n\ntokens = line.split(\"@\")\n\nif tokens.length == 1\n puts \"No solution\"\n exit 0\nend\n\n1.upto(tokens.length - 2) do |i|\n if tokens[i].length == 1\n puts \"No solution\"\n exit 0\n end\nend\n\ntokens[0] = \"-\" + tokens[0]\n0.upto(tokens.length - 3) do |i|\n print tokens[i].sub!(/^./, \"\"), \"@\", tokens[i+1][0], \",\"\nend\n\nputs \"#{tokens[tokens.length-2].sub!(/^./, \"\")}@#{tokens.last}\"\n"}, {"source_code": "#!/usr/bin/ruby\n\nclass Solver\n \nend\n\nline = gets.chomp\n\nif line[0] == \"@\" || line[line.length-1] == \"@\"\n puts \"No solution\"\n exit 0\nend\n\ntokens = line.split(\"@\")\n\n1.upto(tokens.length - 2) do |i|\n if tokens[i].length == 1\n puts \"No solution\"\n exit 0\n end\nend\n\nprint \"#{tokens[0]}@#{tokens[1][0]}\"\n1.upto(tokens.length - 2) do |i|\n 1.upto(tokens[i].length - 1) {|j| print tokens[i][j] }\n print \"@#{tokens[i+1][0]}\"\nend\n\nputs tokens.last.sub!(/^./, \"\")\n"}, {"source_code": "#!/usr/bin/ruby\n\nclass Solver\n \nend\n\nline = gets.chomp\n\nif line[0] == \"@\" || line[line.length-1] == \"@\"\n puts \"No solution\"\n exit 0\nend\n\ntokens = line.split(\"@\")\n\n1.upto(tokens.length - 2) do |i|\n if tokens[i].length == 1\n puts \"No solution\"\n exit 0\n end\nend\n\ntokens[0] = \"-\" + tokens[0]\n0.upto(tokens.length - 3) do |i|\n print tokens[i].sub!(/^./, \"\"), \"@\", tokens[i+1][0], \",\"\nend\n\nputs \"#{tokens[tokens.length-2].sub!(/^./, \"\")}@#{tokens.last}\"\n"}, {"source_code": "#!/usr/bin/ruby\n\nclass Solver\n \nend\n\nline = gets.chomp\n\nif line[0] == \"@\" || line[line.length-1] == \"@\"\n puts \"No solution\"\n exit 0\nend\n\ntokens = line.split(\"@\")\n\ntokens.each do |token| \n if token.length == 1\n puts \"No solution\"\n exit 0\n end\nend\n\nprint \"#{tokens[0]}@#{tokens[1][0]}\"\n1.upto(tokens.length - 2) do |i|\n 1.upto(tokens[i].length - 1) {|j| print tokens[i][j] }\n print \"@#{tokens[i+1][0]}\"\nend\n\nputs tokens.last.sub!(/^./, \"\")\n"}, {"source_code": "#!/usr/bin/ruby\n\nclass Solver\n \nend\n\nline = gets.chomp\n\nif line[0] == \"@\" || line[line.length-1] == \"@\"\n puts \"No solution\"\n exit 0\nend\n\nans = true\nprev = -1\ntemp = \"\"\n0.upto(line.length - 1) do |i|\n if line[i] == \"@\"\n if prev == -1\n prev = i\n else\n if i - prev < 3\n ans = false\n break\n end\n end\n end\n temp += \",\" if (prev != -1) and (i - prev == 2)\n temp += line[i]\nend\n\nif ans\n puts temp\nelse\n puts \"No solution\"\nend\n\n"}], "src_uid": "71b4674e91e0bc5521c416cfc570a090"} {"nl": {"description": "You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.", "input_spec": "The first line contains a single integer $$$q$$$ ($$$1 \\le q \\le 10^4$$$) — the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$.", "output_spec": "For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.", "sample_inputs": ["3\n\naaaa\n\na\n\naa\n\nabc\n\na\n\nb"], "sample_outputs": ["1\n-1\n2"], "notes": "NoteIn the first example, you can replace any letter 'a' with the string \"a\", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with \"abc\". String $$$s$$$ becomes equal to \"aabc\". Then the second letter 'a' again. String $$$s$$$ becomes equal to \"aabcbc\". And so on, generating infinitely many different strings.In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with \"b\". String $$$s$$$ becomes equal to \"b\", so you can't perform more moves on it."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n s = gets.chomp\r\n t = gets.chomp\r\n answer = if t == \"a\"\r\n 1\r\n elsif t.include?(\"a\")\r\n -1\r\n else\r\n 2 ** s.size\r\n end\r\n p answer\r\nend\r\n"}, {"source_code": "\ngets.to_i.times do\n s = gets.strip.length\n t = gets.strip\n\n puts(\n if t == 'a'\n 1\n elsif t.include?('a')\n -1\n else\n 2 ** s\n end\n )\nend\n"}, {"source_code": "T = gets.to_i\nT.times do\n s = gets.chomp\n t = gets.chomp\n \n if t == \"a\"\n puts 1\n next\n end\n \n if t.include?(?a) and t.size >= 2\n puts -1\n next\n end\n \n puts 2**s.size\nend"}], "negative_code": [{"source_code": "gets.to_i.times do\r\n s = gets.chomp\r\n t = gets.chomp\r\n answer = if t.chars.all? { |c| c == \"a\" }\r\n 1\r\n elsif t.include?(\"a\")\r\n -1\r\n else\r\n 2 ** s.size\r\n end\r\n p answer\r\nend\r\n"}], "src_uid": "d6ac9ca9cc5dfd9f43f5f65ce226349e"} {"nl": {"description": "Vova's house is an array consisting of $$$n$$$ elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The $$$i$$$-th element of the array is $$$1$$$ if there is a heater in the position $$$i$$$, otherwise the $$$i$$$-th element of the array is $$$0$$$.Each heater has a value $$$r$$$ ($$$r$$$ is the same for all heaters). This value means that the heater at the position $$$pos$$$ can warm up all the elements in range $$$[pos - r + 1; pos + r - 1]$$$.Vova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater. Vova's target is to warm up the whole house (all the elements of the array), i.e. if $$$n = 6$$$, $$$r = 2$$$ and heaters are at positions $$$2$$$ and $$$5$$$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $$$3$$$ elements will be warmed up by the first heater and the last $$$3$$$ elements will be warmed up by the second heater).Initially, all the heaters are off.But from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater.Your task is to find this number of heaters or say that it is impossible to warm up the whole house.", "input_spec": "The first line of the input contains two integers $$$n$$$ and $$$r$$$ ($$$1 \\le n, r \\le 1000$$$) — the number of elements in the array and the value of heaters. The second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$0 \\le a_i \\le 1$$$) — the Vova's house description.", "output_spec": "Print one integer — the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.", "sample_inputs": ["6 2\n0 1 1 0 0 1", "5 3\n1 0 0 0 1", "5 10\n0 0 0 0 0", "10 3\n0 0 1 1 0 1 0 0 0 1"], "sample_outputs": ["3", "2", "-1", "3"], "notes": "NoteIn the first example the heater at the position $$$2$$$ warms up elements $$$[1; 3]$$$, the heater at the position $$$3$$$ warms up elements $$$[2, 4]$$$ and the heater at the position $$$6$$$ warms up elements $$$[5; 6]$$$ so the answer is $$$3$$$.In the second example the heater at the position $$$1$$$ warms up elements $$$[1; 3]$$$ and the heater at the position $$$5$$$ warms up elements $$$[3; 5]$$$ so the answer is $$$2$$$.In the third example there are no heaters so the answer is -1.In the fourth example the heater at the position $$$3$$$ warms up elements $$$[1; 5]$$$, the heater at the position $$$6$$$ warms up elements $$$[4; 8]$$$ and the heater at the position $$$10$$$ warms up elements $$$[8; 10]$$$ so the answer is $$$3$$$."}, "positive_code": [{"source_code": "n, r = gets.split.map(&:to_i)\na = gets.split.map(&:to_i).each_with_index.to_a\npos, ans = 0, 0\nwhile pos < n\n\tf = a[[0, pos - r + 1].max...pos + r].reverse_each.find { _1[0] == 1 }\n\tif f\n\t\tans += 1\n\t\tpos = f[1] + r\n\telse\n\t\tputs(-1) + exit\n\tend\nend\nputs ans\n"}, {"source_code": "def result\n n, k = read_ints\n last_idx = n-1\n heaters = read_ints\n heater_idx = [*0...heaters.size].select{ |i| heaters[i] == 1 }\n\n return -1 if heater_idx.empty?\n\n lit = 0\n left = 0\n\n (heater_idx + [nil]).each_cons(2) do |idx, next_idx|\n\n # can't light back\n return -1 if idx - k + 1 > left\n\n # next one can light; skip\n next if next_idx && next_idx - k + 1 <= left\n\n # last one can't light to the end\n return -1 if !next_idx && idx + k - 1 < last_idx\n\n lit += 1\n left = idx + k\n\n break if left > last_idx\n end\n\n lit\nend\n\ndef solution\n puts result\nend\n\ndef read_int\n gets.to_i\nend\n\ndef read_ints\n gets.split.map(&:to_i)\nend\n\ndef read_string\n gets.chomp\nend\n\nsolution unless ENV['TEST__MODE']\n\n"}], "negative_code": [{"source_code": "def result\n n, k = read_ints\n last_idx = n-1\n heaters = read_ints\n heater_idx = [*0...heaters.size].select{ |i| heaters[i] == 1 }\n\n return -1 if heater_idx.empty?\n\n lit = 0\n left = 0\n\n (heater_idx + [nil]).each_cons(2) do |idx, next_idx|\n\n # can't light back\n return -1 if idx - k + 1 > left\n\n # next one can light; skip\n next if next_idx && next_idx - k + 1 <= left\n\n # last one can't light to the end\n return -1 if !next_idx && idx + k - 1 < last_idx\n\n lit += 1\n left = idx + k\n end\n\n lit\nend\n\ndef solution\n puts result\nend\n\ndef read_int\n gets.to_i\nend\n\ndef read_ints\n gets.split.map(&:to_i)\nend\n\ndef read_string\n gets.chomp\nend\n\nsolution unless ENV['TEST__MODE']\n\n"}], "src_uid": "001d8aca7c8421c3e54863f3fb706f0d"} {"nl": {"description": "You are given a book with $$$n$$$ chapters.Each chapter has a specified list of other chapters that need to be understood in order to understand this chapter. To understand a chapter, you must read it after you understand every chapter on its required list.Currently you don't understand any of the chapters. You are going to read the book from the beginning till the end repeatedly until you understand the whole book. Note that if you read a chapter at a moment when you don't understand some of the required chapters, you don't understand this chapter.Determine how many times you will read the book to understand every chapter, or determine that you will never understand every chapter no matter how many times you read the book.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 2\\cdot10^4$$$). The first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 2\\cdot10^5$$$) — number of chapters. Then $$$n$$$ lines follow. The $$$i$$$-th line begins with an integer $$$k_i$$$ ($$$0 \\le k_i \\le n-1$$$) — number of chapters required to understand the $$$i$$$-th chapter. Then $$$k_i$$$ integers $$$a_{i,1}, a_{i,2}, \\dots, a_{i, k_i}$$$ ($$$1 \\le a_{i, j} \\le n, a_{i, j} \\ne i, a_{i, j} \\ne a_{i, l}$$$ for $$$j \\ne l$$$) follow — the chapters required to understand the $$$i$$$-th chapter. It is guaranteed that the sum of $$$n$$$ and sum of $$$k_i$$$ over all testcases do not exceed $$$2\\cdot10^5$$$.", "output_spec": "For each test case, if the entire book can be understood, print how many times you will read it, otherwise print $$$-1$$$.", "sample_inputs": ["5\n4\n1 2\n0\n2 1 4\n1 2\n5\n1 5\n1 1\n1 2\n1 3\n1 4\n5\n0\n0\n2 1 2\n1 2\n2 2 1\n4\n2 2 3\n0\n0\n2 3 2\n5\n1 2\n1 3\n1 4\n1 5\n0"], "sample_outputs": ["2\n-1\n1\n2\n5"], "notes": "NoteIn the first example, we will understand chapters $$$\\{2, 4\\}$$$ in the first reading and chapters $$$\\{1, 3\\}$$$ in the second reading of the book.In the second example, every chapter requires the understanding of some other chapter, so it is impossible to understand the book.In the third example, every chapter requires only chapters that appear earlier in the book, so we can understand everything in one go.In the fourth example, we will understand chapters $$$\\{2, 3, 4\\}$$$ in the first reading and chapter $$$1$$$ in the second reading of the book.In the fifth example, we will understand one chapter in every reading from $$$5$$$ to $$$1$$$."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n\r\nn = gets.to_i\r\n@dep = Array.new(n) { k, *a = gets.split.map!(&:to_i); a }.unshift([])\r\n\r\n@cache = Array.new(n + 1)\r\n@visited = Array.new(n + 1, false)\r\n\r\n=begin\r\ndef cnt(k)\r\n @cache[k] ||= (\r\n raise if @visited[k]\r\n @visited[k] = true\r\n\r\n @dep[k].map do |l|\r\n c = cnt(l)\r\n k > l ? c : c.succ\r\n end.max || 1\r\n )\r\nend\r\n=end\r\n\r\n#=begin\r\ndef cnt(k)\r\n return @cache[k] if @cache[k]\r\n\r\n i, j, ret = k, nil, nil\r\n stack = []\r\n\r\n loop do\r\n break unless i\r\n\r\n if ret\r\n ret += 1 if i < @dep[i][j]\r\n j += 1\r\n raise StopIteration if @dep[i][j].nil?\r\n stack << [i, j]\r\n i, j, ret = @dep[i][j], nil, nil\r\n next\r\n end\r\n\r\n if @cache[i]\r\n ret = @cache[i]\r\n i, j = stack.pop\r\n next\r\n end\r\n\r\n raise if @visited[i]\r\n @visited[i] = true\r\n\r\n j = 0\r\n raise StopIteration if @dep[i][j].nil?\r\n stack << [i, j]\r\n i, j, ret = @dep[i][j], nil, nil\r\n rescue StopIteration\r\n max = @dep[i].map do |jj|\r\n c = @cache[jj]\r\n i < jj ? c.succ : c\r\n end.max || 1\r\n ret = @cache[i] = max\r\n i, j = stack.pop\r\n end\r\n\r\n @cache[k]\r\nend\r\n#=end\r\n\r\nbegin\r\n @dep.each_index { |k| cnt(k) }\r\n puts @cache.max\r\nrescue RuntimeError\r\n puts -1\r\nend\r\n\r\nend\r\n"}, {"source_code": "# Priority Queue\r\n# Reference: https://github.com/python/cpython/blob/master/Lib/heapq.py\r\nclass PriorityQueue\r\n # By default, the priority queue returns the maximum element first.\r\n # If a block is given, the priority between the elements is determined with it.\r\n # For example, the following block is given, the priority queue returns the minimum element first.\r\n # `PriorityQueue.new { |x, y| x < y }`\r\n #\r\n # A heap is an array for which a[k] <= a[2*k+1] and a[k] <= a[2*k+2] for all k, counting elements from 0.\r\n def initialize(array = [], &comp)\r\n @heap = array\r\n @comp = comp || proc { |x, y| x > y }\r\n heapify\r\n end\r\n\r\n attr_reader :heap\r\n\r\n # Push new element to the heap.\r\n def push(item)\r\n shift_down(0, @heap.push(item).size - 1)\r\n end\r\n\r\n alias << push\r\n alias append push\r\n\r\n # Pop the element with the highest priority.\r\n def pop\r\n latest = @heap.pop\r\n return latest if empty?\r\n\r\n ret_item = heap[0]\r\n heap[0] = latest\r\n shift_up(0)\r\n ret_item\r\n end\r\n\r\n # Get the element with the highest priority.\r\n def get\r\n @heap[0]\r\n end\r\n\r\n alias top get\r\n\r\n # Returns true if the heap is empty.\r\n def empty?\r\n @heap.empty?\r\n end\r\n\r\n private\r\n\r\n def heapify\r\n (@heap.size / 2 - 1).downto(0) { |i| shift_up(i) }\r\n end\r\n\r\n def shift_up(pos)\r\n end_pos = @heap.size\r\n start_pos = pos\r\n new_item = @heap[pos]\r\n left_child_pos = 2 * pos + 1\r\n\r\n while left_child_pos < end_pos\r\n right_child_pos = left_child_pos + 1\r\n if right_child_pos < end_pos && @comp.call(@heap[right_child_pos], @heap[left_child_pos])\r\n left_child_pos = right_child_pos\r\n end\r\n # Move the higher priority child up.\r\n @heap[pos] = @heap[left_child_pos]\r\n pos = left_child_pos\r\n left_child_pos = 2 * pos + 1\r\n end\r\n @heap[pos] = new_item\r\n shift_down(start_pos, pos)\r\n end\r\n\r\n def shift_down(star_pos, pos)\r\n new_item = @heap[pos]\r\n while pos > star_pos\r\n parent_pos = (pos - 1) >> 1\r\n parent = @heap[parent_pos]\r\n break if @comp.call(parent, new_item)\r\n\r\n @heap[pos] = parent\r\n pos = parent_pos\r\n end\r\n @heap[pos] = new_item\r\n end\r\nend\r\n\r\nHeapQueue = PriorityQueue\r\n\r\ngets.to_i.times do\r\n n=gets.to_i\r\n k=[]\r\n l=[]\r\n n.times do\r\n l << []\r\n end\r\n t=[]\r\n n.times do |i|\r\n a=gets.split.map(&:to_i)\r\n k << a[0]\r\n if a[0]==0\r\n t << i\r\n end\r\n a[1..].each do |aa|\r\n l[aa-1] << i\r\n end\r\n end\r\n\r\n f=true\r\n ans=1\r\n e=t.size\r\n while epp\r\n pq.push(ll)\r\n e+=1\r\n else\r\n t << ll\r\n end\r\n end\r\n end\r\n end\r\n if e==n\r\n break\r\n end\r\n if t==[]\r\n f=false\r\n break\r\n end\r\n e+=t.size\r\n ans+=1\r\n end\r\n if f\r\n puts ans\r\n else\r\n puts \"-1\"\r\n end\r\nend\r\n"}], "negative_code": [{"source_code": "gets.to_i.times do\r\n\r\nn = gets.to_i\r\n@dep = Array.new(n) { k, *a = gets.split.map!(&:to_i); a }.unshift([])\r\n\r\n@cache = Array.new(n + 1)\r\n@visited = Array.new(n + 1, false)\r\n\r\ndef cnt(k)\r\n @cache[k] ||= (\r\n raise if @visited[k]\r\n @visited[k] = true\r\n\r\n @dep[k].map do |l|\r\n c = cnt(l)\r\n k > l ? c : c.succ\r\n end.min || 1\r\n )\r\nend\r\n\r\nbegin\r\n @dep.each_index { |k| cnt(k) }\r\n puts @cache.max\r\nrescue RuntimeError\r\n puts -1\r\nend\r\n\r\nend\r\n"}], "src_uid": "af40a0de6d3c0ff7bcd2c1b077b05d6e"} {"nl": {"description": "You're given an array $$$a$$$ of length $$$2n$$$. Is it possible to reorder it in such way so that the sum of the first $$$n$$$ elements isn't equal to the sum of the last $$$n$$$ elements?", "input_spec": "The first line contains an integer $$$n$$$ ($$$1 \\le n \\le 1000$$$), where $$$2n$$$ is the number of elements in the array $$$a$$$. The second line contains $$$2n$$$ space-separated integers $$$a_1$$$, $$$a_2$$$, $$$\\ldots$$$, $$$a_{2n}$$$ ($$$1 \\le a_i \\le 10^6$$$) — the elements of the array $$$a$$$.", "output_spec": "If there's no solution, print \"-1\" (without quotes). Otherwise, print a single line containing $$$2n$$$ space-separated integers. They must form a reordering of $$$a$$$. You are allowed to not change the order.", "sample_inputs": ["3\n1 2 2 1 3 1", "1\n1 1"], "sample_outputs": ["2 1 3 1 1 2", "-1"], "notes": "NoteIn the first example, the first $$$n$$$ elements have sum $$$2+1+3=6$$$ while the last $$$n$$$ elements have sum $$$1+1+2=4$$$. The sums aren't equal.In the second example, there's no solution."}, "positive_code": [{"source_code": "n=gets.to_i\na=gets.split.map(&:to_i).sort\nif a[0,n].inject(:+)==a[n,n].inject(:+)\n puts \"-1\"\nelse\n puts a*' '\nend\n"}], "negative_code": [], "src_uid": "1f4c057dff45f229b4dca49cd4e0438d"} {"nl": {"description": "A bow adorned with nameless flowers that bears the earnest hopes of an equally nameless person.You have obtained the elegant bow known as the Windblume Ode. Inscribed in the weapon is an array of $$$n$$$ ($$$n \\ge 3$$$) positive distinct integers (i.e. different, no duplicates are allowed).Find the largest subset (i.e. having the maximum number of elements) of this array such that its sum is a composite number. A positive integer $$$x$$$ is called composite if there exists a positive integer $$$y$$$ such that $$$1 < y < x$$$ and $$$x$$$ is divisible by $$$y$$$.If there are multiple subsets with this largest size with the composite sum, you can output any of them. It can be proven that under the constraints of the problem such a non-empty subset always exists.", "input_spec": "Each test consists of multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 100$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3 \\leq n \\leq 100$$$) — the length of the array. The second line of each test case contains $$$n$$$ distinct integers $$$a_{1},a_{2},\\dots,a_{n}$$$ ($$$1 \\leq a_{i} \\leq 200$$$) — the elements of the array.", "output_spec": "Each test case should have two lines of output. The first line should contain a single integer $$$x$$$: the size of the largest subset with composite sum. The next line should contain $$$x$$$ space separated integers representing the indices of the subset of the initial array.", "sample_inputs": ["4\n3\n8 1 2\n4\n6 9 4 2\n9\n1 2 3 4 5 6 7 8 9\n3\n200 199 198"], "sample_outputs": ["2\n2 1\n4\n2 1 4 3\n9\n6 9 1 2 3 4 5 7 8\n3\n1 2 3"], "notes": "NoteIn the first test case, the subset $$$\\{a_2, a_1\\}$$$ has a sum of $$$9$$$, which is a composite number. The only subset of size $$$3$$$ has a prime sum equal to $$$11$$$. Note that you could also have selected the subset $$$\\{a_1, a_3\\}$$$ with sum $$$8 + 2 = 10$$$, which is composite as it's divisible by $$$2$$$.In the second test case, the sum of all elements equals to $$$21$$$, which is a composite number. Here we simply take the whole array as our subset."}, "positive_code": [{"source_code": "def check(n)\n (2..Math.sqrt(n).round).each do |i|\n return true if n % i == 0\n end\n false\nend\n\ngets.to_i.times do\n\tn = gets.to_i\n\ta = gets.split.map(&:to_i)\n\tsuma = a.sum\n\tif check(suma)\n\t\tpos = \"\"\n\t\tfor i in 1..n do\n\t\t\tpos += i.to_s + \" \"\n\t\tend\n\t\tputs n\n\t\tputs pos\n\telse\n\t\tpos = \"\"\n\t\tfor i in 0..n-1 do\n\t\t\tif(check(suma - a[i]))\n\t\t\t\tputs n-[i+1,1].min\n\t\t\t\tfor j in 1..n do\n\t\t\t\t\tif(i + 1 != j)\n\t\t\t\t\t\tpos += j.to_s + \" \"\n\t\t\t\t\tend\n\t\t\t\tend\n\t\t\t\tbreak\n\t\t\tend\n\t\tend\n\t\tputs pos\n\tend\nend"}, {"source_code": "def check(x)\n\tfor i in 2..Math.sqrt(x) do\n\t\tif x % i == 0\n\t\t\treturn true\n\t\tend\n\tend\n\treturn false\nend\n\ngets.to_i.times do\n\tn = gets.to_i\n\ta = gets.split.map(&:to_i)\n\tsuma = a.sum\n\tif check(suma)\n\t\tpos = \"\"\n\t\tfor i in 1..n do\n\t\t\tpos += i.to_s + \" \"\n\t\tend\n\t\tputs n\n\t\tputs pos\n\telse\n\t\tpos = \"\"\n\t\tfor i in 0..n-1 do\n\t\t\tif(check(suma - a[i]))\n\t\t\t\tputs n-[i+1,1].min\n\t\t\t\tfor j in 1..n do\n\t\t\t\t\tif(i + 1 != j)\n\t\t\t\t\t\tpos += j.to_s + \" \"\n\t\t\t\t\tend\n\t\t\t\tend\n\t\t\t\tbreak\n\t\t\tend\n\t\tend\n\t\tputs pos\n\tend\nend"}], "negative_code": [{"source_code": "def check(x)\n\tfor i in 2..Math.sqrt(x) do\n\t\tif x % i == 0\n\t\t\treturn true\n\t\tend\n\tend\n\treturn false\nend\n\ngets.to_i.times do\n\tn = gets.to_i\n\ta = gets.split.map(&:to_i)\n\tcheck = Array.new(20001) {true}\n\tif check[a.sum]\n\t\tsavei = 0\n\t\tfor i in 0..n-1 do\n\t\t\tif !check[a.sum - a[i]]\n\t\t\t\tsavei = i\n\t\t\t\tbreak\n\t\t\tend\n\t\tend\n\t\tputs n-1\n\t\tpos = \"\"\n\t\tfor i in 0..n do\n\t\t\tif(i != savei)\n\t\t\t\tpos += (i+1).to_s + \" \"\n\t\t\tend\n\t\tend\n\t\tputs pos\n\telse\n\t\tputs n\n\t\tpos = \"\"\n\t\tfor i in 0..n do\n\t\t\tpos += (i+1).to_s + \" \"\n\t\tend\n\t\tputs pos\n\tend\nend"}, {"source_code": "def check(x)\n\tfor i in 2..Math.sqrt(x) do\n\t\tif x % i == 0\n\t\t\treturn true\n\t\tend\n\tend\n\treturn false\nend\n\ngets.to_i.times do\n\tn = gets.to_i\n\ta = gets.split.map(&:to_i)\n\tif check(a.sum)\n\t\tputs n\n\t\tpo = \"\"\n\t\tfor i in 1..n do\n\t\t\tpo += i.to_s + \" \"\n\t\tend\n\t\tputs po\n\telse\n\t\tputs n-1\n\t\tpo = \"\"\n\t\tfor i in 1..n-1 do\n\t\t\tpo += i.to_s + \" \"\n\t\tend\n\t\tputs po\n\tend\nend"}], "src_uid": "677972c7d86ce9fd0808105331f77fe0"} {"nl": {"description": "You are given a long decimal number $$$a$$$ consisting of $$$n$$$ digits from $$$1$$$ to $$$9$$$. You also have a function $$$f$$$ that maps every digit from $$$1$$$ to $$$9$$$ to some (possibly the same) digit from $$$1$$$ to $$$9$$$.You can perform the following operation no more than once: choose a non-empty contiguous subsegment of digits in $$$a$$$, and replace each digit $$$x$$$ from this segment with $$$f(x)$$$. For example, if $$$a = 1337$$$, $$$f(1) = 1$$$, $$$f(3) = 5$$$, $$$f(7) = 3$$$, and you choose the segment consisting of three rightmost digits, you get $$$1553$$$ as the result.What is the maximum possible number you can obtain applying this operation no more than once?", "input_spec": "The first line contains one integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) — the number of digits in $$$a$$$. The second line contains a string of $$$n$$$ characters, denoting the number $$$a$$$. Each character is a decimal digit from $$$1$$$ to $$$9$$$. The third line contains exactly $$$9$$$ integers $$$f(1)$$$, $$$f(2)$$$, ..., $$$f(9)$$$ ($$$1 \\le f(i) \\le 9$$$).", "output_spec": "Print the maximum number you can get after applying the operation described in the statement no more than once.", "sample_inputs": ["4\n1337\n1 2 5 4 6 6 3 1 9", "5\n11111\n9 8 7 6 5 4 3 2 1", "2\n33\n1 1 1 1 1 1 1 1 1"], "sample_outputs": ["1557", "99999", "33"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\ns = gets.chomp.chars.map(&:to_i)\na = gets.split.map(&:to_i)\n\npos = (0...n).find { |i| s[i] < a[s[i] - 1] }\n\nif pos\n\t(pos...n).each do |i|\n\t\tbreak if s[i] > a[s[i] - 1]\n\t\ts[i] = a[s[i] - 1]\n\tend\nend\nputs s.join\n"}, {"source_code": "N = gets.to_i\nA = gets.chomp\nF = gets.chomp.split\n\nf = {}\nF.each.with_index(1) do |v, i|\n f[i.to_s] = v\nend\n\nindexes = {}\n\nA.chars.each_with_index do |v, i|\n next if indexes[v]\n \n indexes[v] = i\nend\n\nindexes = Hash[indexes.sort_by { |k, v| v }]\n\nans = A\n\nindexes.each do |d, i|\n if f[d] > d\n list = A.chars\n temp = list.dup\n temp[i..-1].each.with_index(i) do |v, j|\n break if v > f[v]\n list[j] = f[v]\n end\n\n ans = list.join\n\n break\n end\nend\n\nputs ans\n"}], "negative_code": [{"source_code": "n = gets.to_i\ns = gets.chomp.chars.map(&:to_i)\na = gets.split.map(&:to_i)\naaa = (0...n).map { |i| s[i] <= a[s[i] - 1] }.chunk { _1 }.map { [_1, _2.size] }\n\nif aaa[0][0]\n\t(0...aaa[0][1]).each { |i| s[i] = a[s[i] - 1] }\nelsif aaa.size >= 2\n\t(aaa[0][1]...aaa[1][1]).each { |i| s[i] = a[s[i] - 1] }\nend\n\nputs s.join"}, {"source_code": "n = gets.to_i\ns = gets.chomp.chars.map(&:to_i)\na = gets.split.map(&:to_i)\n(0...n).each { |i| s[i] = [s[i], a[s[i] - 1]].max }\nputs s.join\n"}, {"source_code": "N = gets.to_i\nA = gets.to_i\nF = gets.split.map(&:to_i)\n\nans = A\n\n(1..9).each do |d|\n v = A.to_s.gsub(d.to_s, F[d - 1].to_s).to_i\n ans = v if ans < v\nend\n\nputs ans\n"}, {"source_code": "N = gets.to_i\nA = gets.to_i\nF = gets.split.map(&:to_i)\n\nindexes = {}\nA.to_s.each_char.with_index do |ch, i|\n next if indexes[ch]\n \n indexes[ch.to_i] = i\nend\n\nindexes = Hash[indexes.sort_by { |k, v| v }]\n\nans = A\n\nindexes.each do |d, i|\n if F[d - 1] > d\n ans = A.to_s.gsub(d.to_s, F[d - 1].to_s)\n break\n end\nend\n\nputs ans\n"}, {"source_code": "\n N = gets.to_i\nA = gets.to_i\nF = gets.split.map(&:to_i)\n\nindexes = {}\nA.to_s.chars.map(&:to_i).each_with_index do |v, i|\n next if indexes[v]\n \n indexes[v] = i\nend\n\nindexes = Hash[indexes.sort_by { |k, v| v }]\n\nans = A\n\nindexes.each do |d, i|\n if F[d - 1] > d\n list = A.to_s.chars.map(&:to_i)\n temp = list.dup\n temp[i..-1].each.with_index(i) do |v, j|\n break if v != d\n list[j] = F[d - 1]\n end\n\n ans = list.join.to_i\n\n break\n end\nend\n\nputs ans\n"}], "src_uid": "378a9ab7ad891d60f23645106d24f314"} {"nl": {"description": "One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning:a1, a2, ..., an → an, a1, a2, ..., an - 1. Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence?", "input_spec": "The first line contains an integer n (2 ≤ n ≤ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≤ ai ≤ 105).", "output_spec": "If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it.", "sample_inputs": ["2\n2 1", "3\n1 3 2", "2\n1 2"], "sample_outputs": ["1", "-1", "0"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\nsequence = gets.split.map &:to_i\n\ndirection_reverse_location = []\n\nsequence.each_index do |i|\n direction_reverse_location << i if sequence[i] - sequence[i - 1] < 0\nend\n\nif direction_reverse_location.length == 0\n puts 0\nelsif direction_reverse_location.length > 1\n puts -1\nelse\n puts (sequence.length - direction_reverse_location.first) % sequence.length\nend\n"}, {"source_code": "#!/usr/bin/env ruby\n\ndef decpt(arr)\n res = nil\n (1...arr.size).each do |i|\n if arr[i] < arr[i-1]\n return nil if res\n res = i-1\n end\n end\n res\nend\n\nn = gets.to_i\narr = gets.split.map(&:to_i)\nif arr.uniq.size == 1\n puts 0\nelse\n decpt = nil\n arr.size.times do |i|\n if arr[i] < arr[i-1]\n if decpt\n puts -1\n exit\n end\n decpt = i\n end\n end\n if decpt\n puts (arr.size - decpt) % arr.size\n else\n puts 0\n end\nend\n"}, {"source_code": "n=gets.to_i\na=gets.chomp.split.map(&:to_i)\ni=0\nwhile i=0 && a[j] <= a[j + 1] do\n j -= 1\n end\n\n if j == -1\n puts 0\n return\n end\n\n moves = i - j\n\n i = j\n j = i - 1\n while j >=0 && a[j] <= a[j + 1] do\n j -= 1\n end\n\n if j == -1 && a[n-1] <= a[0]\n puts moves\n else\n puts -1\n end\n end\n\nend\n\nB.new.run if __FILE__ == $0"}, {"source_code": "n = gets.to_i\nx = gets.split.map &:to_i\n\nls = x.last\ncnt = 0\npos = 0\nx.each_with_index do |now, i|\n if now < ls\n cnt += 1\n pos = i\n end\n ls = now\nend\nputs cnt >= 2 ? -1 : (n - pos) % n"}, {"source_code": "n=gets.to_i\nxs=gets.split.map{|e| e.to_i}\nc=0\nn.times{|i|\n\tc+=1 if xs[i]>xs[(i+1)%n]\n}\nif c<2 then\n\tmin1=xs.min\n\tmax1=xs.max\n\tif min1==max1 then\n\t\tputs 0\n\telse\n\t\tys=(0..(n-1)).map{|i|\n\t\t\t[xs[i],xs[(i+1)%n]]\n\t\t}\n\t\tputs (n-1)-ys.index([max1,min1])\n\tend\nelse\n\tputs -1\nend"}, {"source_code": "def f(n, a)\n bad_i = nil\n bad_c = 0\n n = a.size\n\n (n-1).times do |i|\n if a[i] > a[i+1]\n bad_c += 1\n bad_i = i\n end\n end\n\n case bad_c\n when 0; 0\n when 1; a[n-1] <= a[0] ? n - 1 - bad_i : -1\n else -1\n end\nend\n\nputs f(STDIN.gets.strip.to_i, STDIN.gets.strip.split.map(&:to_i))\n"}, {"source_code": "n = gets.to_i\narr = gets.split(\" \").map(&:to_i)\nprev = arr[0]\nind = nil\n\n(1..(n-1)).each do |i|\t\n\tif prev > arr[i]\n\t\tind = i\n\t\tbreak\n\tend\n\tprev = arr[i]\nend\nans = -1\n\nif ind\n\t# puts ind\n\tans = n - ind if arr[0..(ind-1)] == arr[0..(ind-1)].sort && arr[ind..(n-1)] == arr[ind..(n-1)].sort && arr[-1] <= arr[0]\nelse\n\tans = 0\nend\n\nputs ans"}, {"source_code": "n=STDIN.gets.to_i\na=gets.split.map{|x| x.to_i}\ni=1\nwhile (i v\n reverse_count += 1\n reverse_index = i\n end\n before = v\n}\n\nif reverse_count >= 2\n puts -1\nelse\n puts (n - reverse_index) % n\nend\n"}, {"source_code": "n=gets.to_i\narr=Array.new(n)\narr=gets.split.map(&:to_i)\n\nnewstartindex=0\nresult=0\n\nfor i in 0...n-1 do \n if (arr[i+1]=arr[n-1])\n puts (n-newstartindex)\n exit\nend\n\nputs -1\n"}, {"source_code": "n = gets.to_i\narr = gets.split(\" \").map &:to_i\n\npos = -1\nfor i in 1...n\n if arr[i - 1] > arr[i]\n pos = i\n break\n end\nend\n\nsorted = arr.sort\nchanged = arr[pos..n] + arr[0..pos-1]\n\nif sorted == arr\n puts 0\nelsif sorted == changed\n puts n - pos\nelse\n puts -1\nend\n"}, {"source_code": "n = gets.to_i\na = gets.chomp.split.map(&:to_i)\np = nil\n(n - 1).times { |i|\n if a[i] > a[i + 1]\n if p\n puts -1\n exit\n else\n p = i\n end\n end\n}\nputs p ? a[-1] <= a[0] ? n - p - 1 : -1 : 0"}, {"source_code": "n = gets.chomp.to_i\nseq = gets.chomp.split.map{|i| i.to_i}\n\nans = 0\n(1...n).each do |i|\n\tif seq[i] < seq[i - 1]\n\t\tans = i\n\tend\nend\n\n\nsort_seq = seq[(ans...n)] + seq[(0...ans)]\n\nif seq == seq.sort\n\tputs 0\nelsif sort_seq == seq.sort\n\tputs (n - ans)\nelse\n\tputs -1\nend\n"}, {"source_code": "\nmodule Enumerable\n def sorted?\n each_cons(2).all? { |a, b| (a <=> b) <= 0 }\n end\nend\n\nn = gets.to_i\ntab = gets.split.map(&:to_i)\npivot = 0\n(0...tab.size-1).each do |i|\n if tab[i] > tab[i+1]\n pivot = i+1\n break\n end\nend\n\nif (tab[0...pivot].sorted? || pivot == 0) &&\n tab[pivot..-1].sorted? &&\n (tab[-1] <= tab[0] || pivot == 0)\n puts (tab.size - pivot) % tab.size\nelse\n puts -1\nend\n\n\n\n"}, {"source_code": "length = gets.chomp.to_i\nsequence = gets.chomp.split(' ').map {|item| item.to_i}\n\ncount = 0\nfragmentedIdx = 0\n\n(0...length).each do |idx|\n\t#puts \"idx : #{idx}\"\n\t#puts \"sequence[idx % length] #{sequence[idx % length]}\"\n\t#puts \"sequence[(idx + 1) % length] #{sequence[(idx + 1) % length]}\"\n\tif sequence[idx % length] > sequence[(idx + 1) % length]\n\t\tcount += 1\n\t\tfragmentedIdx = idx + 1 if count == 1\n\tend\nend\n\nif count > 1\n\tputs \"-1\" \n\texit\nend\n\nif length - fragmentedIdx == length\n\tputs \"0\"\nelse\n\tputs length - fragmentedIdx\nend"}], "negative_code": [{"source_code": "#!/usr/bin/env ruby\n\ndef sortable?(arr)\n i = arr.index(arr.min)\n\n prev = arr[i]\n (1...arr.size).each do |k|\n v = arr[(i+k) % arr.size]\n return false if v < prev\n prev = v\n end\n true\nend\n\nn = gets.to_i\narr = gets.split.map(&:to_i)\nunless sortable?(arr)\n puts -1\nelse\n idx = arr.index(arr.min)\n puts (arr.size - idx) % arr.size\nend\n"}, {"source_code": "#!/usr/bin/env ruby\n\ndef sortable?(arr)\n i = arr.index(arr.min)\n\n prev = arr[i]\n (1...arr.size).each do |k|\n v = arr[(i+k) % arr.size]\n return false if v != prev+1\n prev = v\n end\n true\nend\n\nn = gets.to_i\narr = gets.split.map(&:to_i)\nunless sortable?(arr)\n puts -1\nelse\n idx = arr.index(arr.min)\n puts (arr.size - idx) % arr.size\nend\n"}, {"source_code": "#!/usr/bin/env ruby\n\ndef sortable?(arr)\n i = arr.index(arr.min)\n\n prev = arr[i]\n (1...arr.size).each do |k|\n v = arr[(i+k) % arr.size]\n return false if v < k\n prev = v\n end\n true\nend\n\nn = gets.to_i\narr = gets.split.map(&:to_i)\nunless sortable?(arr)\n puts -1\nelse\n idx = arr.index(arr.min)\n puts (arr.size - idx) % arr.size\nend\n"}, {"source_code": "class B\n\n def run\n n = $stdin.gets.to_i\n a = $stdin.gets.split(' ').map &:to_i\n\n i = n - 1\n j = i - 1\n while j >=0 && a[j] <= a[i] do\n j -= 1\n end\n\n if j == -1\n puts 0\n return\n end\n\n moves = i - j\n\n i = j\n j = i - 1\n while j >=0 && a[j] <= a[i] do\n j -= 1\n end\n\n if j == -1 && a[n-1] <= a[0]\n puts moves\n else\n puts -1\n end\n end\n\nend\n\nB.new.run if __FILE__ == $0"}, {"source_code": "class B\n\n def run\n n = $stdin.gets.to_i\n a = $stdin.gets.split(' ').map &:to_i\n b = a + a\n\n ans = 2 * n\n start = 0\n while start < n\n i = start + 1\n while b[i] >= b[i-1]\n i += 1\n end\n ans = [ans, start].min if i - start >= n\n start = i\n end\n\n puts ans == 2 * n ? -1 : ans\n end\n\nend\n\nB.new.run if __FILE__ == $0"}, {"source_code": "n=gets.to_i\nxs=gets.split.map{|e| e.to_i}\nc=0\nn.times{|i|\n\tc+=1 if xs[i]>xs[(i+1)%n]\n}\nif c<2 then\n\tmin1=xs.min\n\tmax1=xs.max\n\tys=(0..(n-1)).map{|i|\n\t\t[xs[i],xs[(i+1)%n]]\n\t}\n\tputs (n-1)-ys.index([max1,min1])\nelse\n\tputs -1\nend"}, {"source_code": "n=gets.to_i\nxs=gets.split.map{|e| e.to_i}\nc=0\nn.times{|i|\n\tc+=1 if xs[i]>xs[(i+1)%n]\n}\nif c<2 then\n\tm=xs.max\n\tif m==xs[n-1] then\n\t\tputs 0\n\telse\n\t\tputs (n-1)-xs.rindex(m)\n\tend\nelse\n\tputs -1\nend"}, {"source_code": "n=gets.to_i\narr=Array.new(n)\narr=gets.split(&:to_i)\n\nnewstartindex=0\nresult=0\n\nfor i in 0...n-1 do \n if (arr[i+1]=arr[n-1]\n puts (n-newstartindex)\n exit\nend\n\nputs -1\n\n# if arr[n-1]>arr[0] && result==0 && n>2\n# puts 0;\n# exit\n# end\n\n# if arr[n-1]>arr[0] && n>2\n# result+=1\n# end\n\n\n# if result>1\n# puts -1\n# exit\n# end\n\n\n\n# puts (n-newstartindex)\n"}, {"source_code": "n=gets.to_i\narr=Array.new(n)\narr=gets.split(&:to_i)\n\nnewstartindex=0\nresult=0\n\nfor i in 0...n-1 do \n if (arr[i+1]arr[0] && result==0 && n>2\n puts 1;\n exit\nend\n\nif arr[n-1]>arr[0] && n>2\n result+=1 \nend\n\n\nif result>1\n puts -1\n exit\nend\n\nif result==0\n puts 0\n exit\nend\n\nputs (n-newstartindex)\n"}, {"source_code": "n=gets.to_i\narr=Array.new(n)\narr=gets.split(&:to_i)\n\nnewstartindex=0\nresult=0\n\nfor i in 0...n-1 do \n if (arr[i+1]arr[0] && result==0 && n>2\n puts 0;\n exit\nend\n\nif arr[n-1]>arr[0] && n>2\n result+=1\nend\n\n\nif result>1\n puts -1\n exit\nend\n\nif result==0\n puts 0\n exit\nend\n\nputs (n-newstartindex)\n"}, {"source_code": "n = gets.to_i\narr = gets.split(\" \").map &:to_i\n\npos = -1\nfor i in 1...n\n if arr[i - 1] > arr[i]\n pos = i\n break\n end\nend\n\nsorted = arr.sort\nchanged = arr[pos..n] + arr[0..pos-1]\n\nif sorted == arr\n puts 0\nelsif sorted == changed\n puts pos\nelse\n puts -1\nend\n"}, {"source_code": "\nmodule Enumerable\n def sorted?\n each_cons(2).all? { |a, b| (a <=> b) <= 0 }\n end\nend\n\nn = gets.to_i\ntab = gets.split.map(&:to_i)\npivot = tab.index(tab.min)\n\nif (tab[0...pivot].sorted? || pivot == 0) &&\n tab[pivot..-1].sorted? &&\n (tab[-1] <= tab[0] || pivot == 0)\n puts (tab.size - pivot) % tab.size\nelse\n puts -1\nend\n\n\n\n"}, {"source_code": "\nn,tab=gets.to_i,gets.split.map(&:to_i)\npivot=tab.index(tab.min)\nif pivot == 0 && tab[0..-1].each_cons(2).all? {|a, b| (a <=> b) <= 0}\n puts 0\nelsif tab[0...pivot].each_cons(2).all? {|a, b| (a <=> b) <= 0} &&\n tab[pivot...-1].each_cons(2).all? {|a, b| (a <=> b) <= 0} &&\n tab[-1] <= tab[0]\n puts tab.size - pivot\nelse\n puts -1\nend\n\n\n\n"}, {"source_code": "\nn,tab=gets.to_i,gets.split.map(&:to_i)\npivot=tab.index(tab.min)\nif tab[0...pivot].each_cons(2).all? {|a, b| a <= b} &&\n tab[pivot..-1].each_cons(2).all? {|a, b| a <= b} &&\n (tab[-1] <= tab[0] || pivot == 0)\n puts (tab.size - pivot) % tab.size\nelse\n puts -1\nend\n\n\n\n"}, {"source_code": "\nn,tab=gets.to_i,gets.split.map(&:to_i)\npivot=tab.index(tab.min)\nif pivot == 0 && tab[0..-1].each_cons(2).all? {|a, b| (a <=> b) <= 0}\n puts 0\nelsif tab[0...pivot].each_cons(2).all? {|a, b| (a <=> b) <= 0} &&\n tab[pivot..-1].each_cons(2).all? {|a, b| (a <=> b) <= 0} &&\n tab[-1] <= tab[0]\n puts tab.size - pivot\nelse\n puts -1\nend\n\n\n\n"}, {"source_code": "\nn=gets.to_i\n(0...n).each {|x| puts '*' * ((n-1)/2 - x).abs + 'D' * (n - ((n-1) - 2*x).abs) + '*' * ((n-1)/2 - x).abs}\n\n"}, {"source_code": "\nn,tab=gets.to_i,gets.split.map(&:to_i)\npivot=tab.index(tab.min)\nif pivot == 0 && tab[0..-1].each_cons(2).all? {|a, b| a <= b}\n puts 0\nelsif tab[0...pivot].each_cons(2).all? {|a, b| a <= b} &&\n tab[pivot..-1].each_cons(2).all? {|a, b| a <= b} &&\n tab[-1] <= tab[0]\n puts tab.size - pivot\nelse\n puts -1\nend\n\n\n\n"}, {"source_code": "\nmodule Enumerable\n def sorted?\n each_cons(2).all? { |a, b| (a <=> b) <= 0 }\n end\nend\n\nn = gets.to_i\ntab = gets.split.map(&:to_i)\npivot = tab.index(tab.min)\n\nif tab[0...pivot].sorted? &&\n tab[pivot..-1].sorted? &&\n (tab[-1] <= tab[0] || pivot == 0)\n puts (tab.size - pivot) % tab.size\nelse\n puts -1\nend\n\n\n\n"}, {"source_code": "length = gets.chomp.to_i\nsequence = gets.chomp.split(' ').map {|item| item.to_i}\n#sorted = sequence.sort\n\ncount = 0\nfragmentedIdx = 0\n\nif (sequence.index(sequence.min) == 0 && sequence.rindex(sequence.max) - sequence.index(sequence.min) != length - 1)\n\tputs \"-1\"\n\texit\nend\n\n(0...(length - 1)).each do |idx|\n\tif sequence[idx] > sequence[idx + 1]\n\t\t#puts \"Join\"\n\t\tcount += 1\n\t\tfragmentedIdx = idx + 1\n\tend\nend\nif count > 1\n\tputs \"-1\" \n\texit\n\nend\n\nputs length - fragmentedIdx if sequence.index(sequence.min) != 0 \nputs \"0\" if sequence.index(sequence.min) == 0"}, {"source_code": "n = gets.chomp.to_i\nstart = (n / 2) + 1\ndNum = 0\n\n(0...n).each do |row|\n\tif row <= n / 2\n\t\tdNum = 2 * (row + 1) - 1\n\t\tstart -= 1\n\telse\n\t\tdNum = (-2 * row + (2 * n - 1))\n\t\tstart += 1 if row > n / 2\n\tend\n\n\t(0...n).each do |col|\n\t\tif col >= start && dNum > 0\n\t\t\tprint 'D'\n\t\t\tdNum -= 1\n\t\telse\n\t\t\tprint '*'\n\t\tend\n\tend\n\n\tputs ''\nend"}, {"source_code": "length = gets.chomp.to_i\nsequence = gets.chomp.split(' ').map {|item| item.to_i}\n#sorted = sequence.sort\n\n#puts \"max : #{sequence.rindex(sequence.max)}\"\n#puts \"min : #{sequence.index(sequence.min)}\"\nif (sequence.index(sequence.min) == 0 && sequence.rindex(sequence.max) - sequence.index(sequence.min) != length - 1) || (sequence.index(sequence.min) != 0 && sequence.index(sequence.min) - sequence.rindex(sequence.max) != 1)\n\tputs \"-1\"\n\texit\nend\n\nsequence.rindex(sequence.max).downto(1).each do |iterNum|\n\t#puts \"iterNum #{iterNum}\"\n\n\tif sequence[iterNum] < sequence[iterNum - 1]\n\t\tputs \"-1\"\n\t\texit\n\tend\nend\n\nanswer = if length - sequence.index(sequence.min) != length\n\tlength - sequence.index(sequence.min)\nelse\n\t0\nend\nputs answer"}, {"source_code": "length = gets.chomp.to_i\nsequence = gets.chomp.split(' ').map {|item| item.to_i}\n#sorted = sequence.sort\n\nif (sequence.index(sequence.min) == 0 && sequence.index(sequence.max) - sequence.index(sequence.min) != length - 1) && (sequence.index(sequence.min) - sequence.index(sequence.max) != 1)\n\tputs \"-1\"\n\texit\nend\n\nsequence.index(sequence.max).downto(1).each do |iterNum|\n\t#puts \"iterNum #{iterNum}\"\n\n\tif sequence[iterNum] < sequence[iterNum - 1]\n\t\tputs \"-1\"\n\t\texit\n\tend\nend\n\nanswer = if length - sequence.index(sequence.min) != length\n\tlength - sequence.index(sequence.min)\nelse\n\t0\nend\nputs answer"}, {"source_code": "length = gets.chomp.to_i\nsequence = gets.chomp.split(' ').map {|item| item.to_i}\n#sorted = sequence.sort\n\ncount = 0\nfragmentedIdx = 0\n\nif (sequence.index(sequence.min) == 0 && sequence.rindex(sequence.max) - sequence.index(sequence.min) != length - 1)\n\tputs \"-1\"\n\texit\nend\n\n(0...(length - 1)).each do |idx|\n\tif sequence[idx] > sequence[idx + 1]\n\t\t#puts \"Join\"\n\t\tcount += 1\n\t\tfragmentedIdx = idx + 1\n\tend\nend\nputs \"-1\" if count > 1\nputs length - fragmentedIdx if sequence.index(sequence.min) != 0\nputs \"0\""}, {"source_code": "length = gets.chomp.to_i\nsequence = gets.chomp.split(' ').map {|item| item.to_i}\n#sorted = sequence.sort\n\nif sequence[0] == 99997\nputs \"max : #{sequence.rindex(sequence.max)}\"\nputs \"min : #{sequence.index(sequence.min)}\"\nend\nif (sequence.index(sequence.min) == 0 && sequence.rindex(sequence.max) - sequence.index(sequence.min) != length - 1)\n\tputs \"-1\"\n\texit\nend\n\nif (sequence.index(sequence.min) != 0 && sequence.index(sequence.min) - sequence.rindex(sequence.max) != 1)\n\tputs \"-1\"\n\texit\nend\n\nsequence.rindex(sequence.max).downto(1).each do |iterNum|\n\t#puts \"iterNum #{iterNum}\"\n\n\tif sequence[iterNum] < sequence[iterNum - 1]\n\t\tputs \"-1\"\n\t\texit\n\tend\nend\n\nanswer = if length - sequence.index(sequence.min) != length\n\tlength - sequence.index(sequence.min)\nelse\n\t0\nend\nputs answer"}, {"source_code": "length = gets.chomp.to_i\nsequence = gets.chomp.split(' ').map {|item| item.to_i}\n#sorted = sequence.sort\n\ncount = 0\nfragmentedIdx = 0\n\nif (sequence.index(sequence.min) == 0 && sequence.rindex(sequence.max) - sequence.index(sequence.min) != length - 1)\n\tputs \"-1\"\n\texit\nend\n\n(0...(length - 1)).each do |idx|\n\tif sequence[idx] > sequence[idx + 1]\n\t\t#puts \"Join\"\n\t\tcount += 1\n\t\tfragmentedIdx = idx + 1\n\tend\nend\nputs \"-1\" if count > 1\nputs length - fragmentedIdx if sequence.index(sequence.min) != 0\nputs \"0\" if sequence.index(sequence.min) == 0"}, {"source_code": "length = gets.chomp.to_i\nsequence = gets.chomp.split(' ').map {|item| item.to_i}\n#sorted = sequence.sort\n\nif (sequence.index(sequence.min) == 0 && sequence.rindex(sequence.max) - sequence.index(sequence.min) != length - 1) && (sequence.index(sequence.min) - sequence.rindex(sequence.max) != 1)\n\tputs \"-1\"\n\texit\nend\n\nsequence.rindex(sequence.max).downto(1).each do |iterNum|\n\t#puts \"iterNum #{iterNum}\"\n\n\tif sequence[iterNum] < sequence[iterNum - 1]\n\t\tputs \"-1\"\n\t\texit\n\tend\nend\n\nanswer = if length - sequence.index(sequence.min) != length\n\tlength - sequence.index(sequence.min)\nelse\n\t0\nend\nputs answer"}], "src_uid": "c647e36495fb931ac72702a12c6bfe58"} {"nl": {"description": "Let's define the following recurrence: $$$$$$a_{n+1} = a_{n} + minDigit(a_{n}) \\cdot maxDigit(a_{n}).$$$$$$Here $$$minDigit(x)$$$ and $$$maxDigit(x)$$$ are the minimal and maximal digits in the decimal representation of $$$x$$$ without leading zeroes. For examples refer to notes.Your task is calculate $$$a_{K}$$$ for given $$$a_{1}$$$ and $$$K$$$.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of independent test cases. Each test case consists of a single line containing two integers $$$a_{1}$$$ and $$$K$$$ ($$$1 \\le a_{1} \\le 10^{18}$$$, $$$1 \\le K \\le 10^{16}$$$) separated by a space.", "output_spec": "For each test case print one integer $$$a_{K}$$$ on a separate line.", "sample_inputs": ["8\n1 4\n487 1\n487 2\n487 3\n487 4\n487 5\n487 6\n487 7"], "sample_outputs": ["42\n487\n519\n528\n544\n564\n588\n628"], "notes": "Note$$$a_{1} = 487$$$ $$$a_{2} = a_{1} + minDigit(a_{1}) \\cdot maxDigit(a_{1}) = 487 + \\min (4, 8, 7) \\cdot \\max (4, 8, 7) = 487 + 4 \\cdot 8 = 519$$$ $$$a_{3} = a_{2} + minDigit(a_{2}) \\cdot maxDigit(a_{2}) = 519 + \\min (5, 1, 9) \\cdot \\max (5, 1, 9) = 519 + 1 \\cdot 9 = 528$$$ $$$a_{4} = a_{3} + minDigit(a_{3}) \\cdot maxDigit(a_{3}) = 528 + \\min (5, 2, 8) \\cdot \\max (5, 2, 8) = 528 + 2 \\cdot 8 = 544$$$ $$$a_{5} = a_{4} + minDigit(a_{4}) \\cdot maxDigit(a_{4}) = 544 + \\min (5, 4, 4) \\cdot \\max (5, 4, 4) = 544 + 4 \\cdot 5 = 564$$$ $$$a_{6} = a_{5} + minDigit(a_{5}) \\cdot maxDigit(a_{5}) = 564 + \\min (5, 6, 4) \\cdot \\max (5, 6, 4) = 564 + 4 \\cdot 6 = 588$$$ $$$a_{7} = a_{6} + minDigit(a_{6}) \\cdot maxDigit(a_{6}) = 588 + \\min (5, 8, 8) \\cdot \\max (5, 8, 8) = 588 + 5 \\cdot 8 = 628$$$"}, "positive_code": [{"source_code": "1.upto(gets.to_i) do\n a, k = gets.split(' ').map(&:to_i)\n\n while k>1\n s = a.to_s\n break if s.include? '0'\n\n a += s.chars.min.to_i * s.chars.max.to_i\n k -= 1\n end\n\n puts a\nend\n"}], "negative_code": [], "src_uid": "7a48218582b90f735a09083df9e15b96"} {"nl": {"description": "You have a stripe of checkered paper of length $$$n$$$. Each cell is either white or black.What is the minimum number of cells that must be recolored from white to black in order to have a segment of $$$k$$$ consecutive black cells on the stripe?If the input data is such that a segment of $$$k$$$ consecutive black cells already exists, then print 0. ", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. Next, descriptions of $$$t$$$ test cases follow. The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le k \\le n \\le 2\\cdot10^5$$$). The second line consists of the letters 'W' (white) and 'B' (black). The line length is $$$n$$$. It is guaranteed that the sum of values $$$n$$$ does not exceed $$$2\\cdot10^5$$$.", "output_spec": "For each of $$$t$$$ test cases print an integer — the minimum number of cells that need to be repainted from white to black in order to have a segment of $$$k$$$ consecutive black cells.", "sample_inputs": ["4\n\n5 3\n\nBBWBW\n\n5 5\n\nBBWBW\n\n5 1\n\nBBWBW\n\n1 1\n\nW"], "sample_outputs": ["1\n2\n0\n1"], "notes": "NoteIn the first test case, $$$s$$$=\"BBWBW\" and $$$k=3$$$. It is enough to recolor $$$s_3$$$ and get $$$s$$$=\"BBBBW\". This string contains a segment of length $$$k=3$$$ consisting of the letters 'B'.In the second test case of the example $$$s$$$=\"BBWBW\" and $$$k=5$$$. It is enough to recolor $$$s_3$$$ and $$$s_5$$$ and get $$$s$$$=\"BBBBB\". This string contains a segment of length $$$k=5$$$ consisting of the letters 'B'.In the third test case of the example $$$s$$$=\"BBWBW\" and $$$k=1$$$. The string $$$s$$$ already contains a segment of length $$$k=1$$$ consisting of the letters 'B'."}, "positive_code": [{"source_code": "def solve(str, k)\n count_w = 0\n k.times { |i| count_w += 1 if str[i] == 'W' }\n ans = count_w\n return ans if ans == 0\n (k...str.length).each do |r|\n l = r - k\n count_w -= 1 if str[l] == 'W'\n count_w += 1 if str[r] == 'W'\n ans = count_w if count_w < ans\n end\n ans\nend\n\ntests = gets.to_i\ntests.times do\n k = gets.split(' ')[1].to_i\n str = gets.chomp\n puts solve(str, k)\nend"}], "negative_code": [], "src_uid": "6e3bacbe61775883599989d0f61367d5"} {"nl": {"description": "There are n pearls in a row. Let's enumerate them with integers from 1 to n from the left to the right. The pearl number i has the type ai.Let's call a sequence of consecutive pearls a segment. Let's call a segment good if it contains two pearls of the same type.Split the row of the pearls to the maximal number of good segments. Note that each pearl should appear in exactly one segment of the partition.As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 3·105) — the number of pearls in a row. The second line contains n integers ai (1 ≤ ai ≤ 109) – the type of the i-th pearl.", "output_spec": "On the first line print integer k — the maximal number of segments in a partition of the row. Each of the next k lines should contain two integers lj, rj (1 ≤ lj ≤ rj ≤ n) — the number of the leftmost and the rightmost pearls in the j-th segment. Note you should print the correct partition of the row of the pearls, so each pearl should be in exactly one segment and all segments should contain two pearls of the same type. If there are several optimal solutions print any of them. You can print the segments in any order. If there are no correct partitions of the row print the number \"-1\".", "sample_inputs": ["5\n1 2 3 4 1", "5\n1 2 3 4 5", "7\n1 2 1 3 1 2 1"], "sample_outputs": ["1\n1 5", "-1", "2\n1 3\n4 7"], "notes": null}, "positive_code": [{"source_code": "require 'scanf'\nEndMark = -1\nclass Table\n def initialize\n @occurList = {}\n @start = @goal = 1\n @answer = []\n end\n\n def readInput(n)\n if n == EndMark && !@answer.empty?\n @answer[-1][1]=@goal-1\n end\n\n if @occurList[n]\n @answer << [@start,@goal]\n nextPos = @goal+1\n @start = @goal = nextPos\n @occurList = {}\n else\n @occurList[n] = true\n @goal += 1\n end\n end\n\n def printAnswer\n if @answer.empty?\n puts -1\n else\n puts @answer.size\n @answer.each {|a| printPos *a }\n end\n end\n\n private\n def printPos a,b\n puts \"#{a} #{b}\"\n end\nend\n\nwhile str = gets\n tool = Table.new\n\n gets.split(' ').each do |x|\n tool.readInput x\n end\n\n tool.readInput(EndMark)\n\n tool.printAnswer\nend\n"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split(\" \").map(&:to_i)\n\nh = Hash.new([])\n\nres = []\n\na.each_with_index do |i, j|\n h[i] += [j + 1]\n if h[i].length() == 2 then\n res << h[i]\n h = Hash.new([])\n end\nend\n\nif res.empty? then\n puts -1\nelse\n puts res.length()\n res.last[1] = n\n tmp = 1\n res.each do |r|\n puts [[r[0], tmp].min, r[1]].join(\" \")\n tmp = r[1] + 1\n end\nend"}], "negative_code": [{"source_code": "n = gets.chomp.to_i\na = gets.chomp.split(\" \").map(&:to_i)\n\nh = Hash.new([])\n\nres = []\n\na.each_with_index do |i, j|\n h[i] += [j + 1]\n if h[i].length() == 2 then\n res << h[i]\n h = Hash.new([])\n end\nend\n\nif res.empty? then\n puts -1\nelse\n puts res.length()\n tmp = res[0][1]\n res.each do |r|\n puts [[r[0], tmp].min, r[1]].join(\" \")\n tmp = r[1] + 1\n end\nend"}], "src_uid": "4cacec219e4577b255ddf6d39d308e10"} {"nl": {"description": "A matrix of size $$$n \\times m$$$ is called nice, if all rows and columns of the matrix are palindromes. A sequence of integers $$$(a_1, a_2, \\dots , a_k)$$$ is a palindrome, if for any integer $$$i$$$ ($$$1 \\le i \\le k$$$) the equality $$$a_i = a_{k - i + 1}$$$ holds.Sasha owns a matrix $$$a$$$ of size $$$n \\times m$$$. In one operation he can increase or decrease any number in the matrix by one. Sasha wants to make the matrix nice. He is interested what is the minimum number of operations he needs.Help him!", "input_spec": "The first line contains a single integer $$$t$$$ — the number of test cases ($$$1 \\le t \\le 10$$$). The $$$t$$$ tests follow. The first line of each test contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n, m \\le 100$$$) — the size of the matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i, j}$$$ ($$$0 \\le a_{i, j} \\le 10^9$$$) — the elements of the matrix.", "output_spec": "For each test output the smallest number of operations required to make the matrix nice.", "sample_inputs": ["2\n4 2\n4 2\n2 4\n4 2\n2 4\n3 4\n1 2 3 4\n5 6 7 8\n9 10 11 18"], "sample_outputs": ["8\n42"], "notes": "NoteIn the first test case we can, for example, obtain the following nice matrix in $$$8$$$ operations:2 24 44 42 2In the second test case we can, for example, obtain the following nice matrix in $$$42$$$ operations:5 6 6 56 6 6 65 6 6 5"}, "positive_code": [{"source_code": "I=->{gets.split.map &:to_i}\nI[][0].times{n,m=I[]\nb=0\na=(1..n).map{I[]}\n(n/-2..-1).map{|i|(m/-2..-1).map{|j|l=[n+i,-i-1].product([m+j,-j-1]).uniq.map{|x,y|a[x][y]}.sort\nl.map{|x|b+=(l[l.size/2]-x).abs}}}\np b}\n"}, {"source_code": "T = gets.to_i\n\nT.times do\n n, m = gets.split.map(&:to_i)\n g = n.times.map { gets.split.map(&:to_i) }\n checked = Array.new(n) { Array.new(m, false) }\n ans = 0\n\n n.times do |y|\n m.times do |x|\n next if checked[y][x]\n checked[y][x] = true\n\n values = []\n values << g[y][x]\n\n y2 = y\n x2 = (m - 1) - x\n values << g[y2][x2] if !checked[y2][x2]\n checked[y2][x2] = true\n\n y3 = (n - 1) - y\n x3 = x\n values << g[y3][x3] if !checked[y3][x3]\n checked[y3][x3] = true\n\n y4 = (n - 1) - y\n x4 = (m - 1) - x\n values << g[y4][x4] if !checked[y4][x4]\n checked[y4][x4] = true\n\n ave = values.sort[values.size / 2]\n ans += values.map { |v| (v - ave).abs }.sum\n end\n end\n\n puts ans\nend\n"}, {"source_code": "I=->{gets.split.map &:to_i}\nI[][0].times{n,m=I[]\na=(1..n).map{I[]}\np [a,a.map(&:reverse)].map{|b|[b.reverse,b].map &:flatten}.flatten(1).transpose.map{|q|x,y,z,w=q.sort;w+z-x-y}.sum/4}"}], "negative_code": [{"source_code": "T = gets.to_i\n\nT.times do\n n, m = gets.split.map(&:to_i)\n g = n.times.map { gets.split.map(&:to_i) }\n checked = Array.new(n) { Array.new(m, false) }\n ans = 0\n\n n.times do |y|\n m.times do |x|\n next if checked[y][x]\n checked[y][x] = true\n\n values = []\n values << g[y][x]\n\n y2 = y\n x2 = m - x - 1\n values << g[y2][x2] if !checked[y2][x2]\n checked[y2][x2] = true\n\n y3 = n - y - 1\n x3 = x\n values << g[y3][x3] if !checked[y3][x3]\n checked[y3][x3] = true\n\n y4 = n - y - 1\n x4 = m - x - 1\n values << g[y4][x4] if !checked[y4][x4]\n checked[y4][x4] = true\n\n ave = values.sum / values.size\n ans += values.map { |v| (v - ave).abs }.sum\n end\n end\n\n puts ans\nend\n"}, {"source_code": "T = gets.to_i\n\nT.times do\n n, m = gets.split.map(&:to_i)\n g = n.times.map { gets.split.map(&:to_i) }\n checked = Array.new(n) { Array.new(m, false) }\n ans = 0\n\n n.times do |y|\n m.times do |x|\n next if checked[y][x]\n checked[y][x] = true\n\n values = []\n values << g[y][x]\n\n y2 = y\n x2 = m - x - 1\n values << g[y2][x2] if !checked[y2][x2]\n checked[y2][x2] = true\n\n y3 = n - y - 1\n x3 = x\n values << g[y3][x3] if !checked[y3][x3]\n checked[y3][x3] = true\n\n y4 = n - y - 1\n x4 = m - x - 1\n values << g[y4][x4] if !checked[y4][x4]\n checked[y4][x4] = true\n\n ave = values[values.size / 2]\n ans += values.map { |v| (v - ave).abs }.sum\n end\n end\n\n puts ans\nend\n"}], "src_uid": "5aa709f292f266799f177b174c8bc14b"} {"nl": {"description": "People in the Tomskaya region like magic formulas very much. You can see some of them below.Imagine you are given a sequence of positive integer numbers p1, p2, ..., pn. Lets write down some magic formulas:Here, \"mod\" means the operation of taking the residue after dividing.The expression means applying the bitwise xor (excluding \"OR\") operation to integers x and y. The given operation exists in all modern programming languages. For example, in languages C++ and Java it is represented by \"^\", in Pascal — by \"xor\".People in the Tomskaya region like magic formulas very much, but they don't like to calculate them! Therefore you are given the sequence p, calculate the value of Q.", "input_spec": "The first line of the input contains the only integer n (1 ≤ n ≤ 106). The next line contains n integers: p1, p2, ..., pn (0 ≤ pi ≤ 2·109).", "output_spec": "The only line of output should contain a single integer — the value of Q.", "sample_inputs": ["3\n1 2 3"], "sample_outputs": ["3"], "notes": null}, "positive_code": [{"source_code": "#$stdin.reopen('/home/chen/Desktop/input.txt', 'r')\n#$stdout.reopen('/home/chen/Desktop/output.txt', 'w')\n\n=begin\nclass RQtree\n\n# major method\n# :set(ind, value) set array[ind] = value\n# :query(i,j) query the minimum number between index i and j\n#\n\tdef initialize(array)\n\t\t@data = array.clone\n\t\t@tree = [0]*1000000\n\t\tconstruct(1, 0,@data.size-1)\n\tend\n\n\tattr_reader :data, :tree\n\tdef min(a,b)\n\t\treturn a if a < b\n\t\tb\n\tend\n\n\tdef construct(ind, i, j)\n\t\tif i==j\n\t\t\t@tree[ind] = @data[i]\n\t\t\treturn @tree[ind]\n\t\telse\n\t\t\tmid = (i+j)/2\n\t\t\ta = construct(2*ind, i, mid)\n\t\t\tb = construct(2*ind+1, mid+1,j)\n\t\t\t@tree[ind] = min(a,b)\n\t\t\treturn @tree[ind]\n\t\tend\n\tend\n\n\tdef inspect(); _inspect(1,0,@data.size-1);end\n\n\tdef _inspect(ind, b,e)\n\n\t\tputs \"#{b}:#{e} #{@tree[ind]}\"\n\t\treturn if b==e\n\t\t_inspect(2*ind, b, (b+e)/2)\n\t\t_inspect(2*ind+1, (b+e)/2+1, e)\n\tend\n\n\tdef query(i,j); _query(i,j, 1,0, @data.size-1); end\n\n\tdef _query(i,j,ind, b, e)\n\n\t\tif i>j\n\t\t\tputs 'somethong wrong with index in func _query'\n\t\t\texit 1\n\t\tend\n\n\t\tif i==b and j==e\n\t\t\treturn @tree[ind]\n\t\telse\n\t\t\tmid = (b+e)/2\n\t\t\tif i>mid\n\t\t\t\treturn _query(i, j, 2*ind+1, mid+1, e)\n\t\t\telsif j<= mid\n\t\t\t\treturn _query(i,j,2*ind,b,mid)\n\t\t\telse\n\t\t\t\ta = _query(i, mid,2*ind, b, mid)\n\t\t\t\tb = _query(mid+1,j,2*ind+1, mid+1,e)\n\t\t\t\treturn min(a,b)\n\t\t\tend\n\t\tend\n\tend\n\n\tdef set(ind, value)\n\t\t@data[ind] = value\n\t\tupdate(ind, value)\n\tend\n\n\tdef update(ind, value); _update( ind, value,1, 0, @data.size-1); end\n\n\tdef _update(ind, value,i,b,e)\n\n\t\tif b==e\n\t\t\t@tree[i] = value\n\t\t\treturn\n\t\tend\t\t\n\t\tmid = (b+e)/2\n\t\tif ind > mid\n\t\t\ta=_update(ind, value,2*i+1, mid+1, e)\t\t\n\n\t\telse\n\t\t\t_update(ind, value, 2*i, b, mid)\n\t\tend\n\t\t@tree[i] = min(@tree[2*i], @tree[2*i+1])\n\tend\n\nend\n\nclass Bitree\n\n\tdef initialize(array)\n\t\t@data=array.clone\n\t\t@tree= [0]*(array.size+1)\n\t\tconstruct()\n\tend\n\n\tattr_reader :data, :tree\n\tdef construct()\n\t\tsum = [0]*(@data.size+1)\n\t\t(1..sum.size-1).each do |ind|\n\t\t\tsum[ind] = @data[ind-1]+sum[ind-1]\n\t\tend\n\t\t@tree[0] = 0\n\t\t(1..@tree.size-1).each do |ind|\n\t\t\ti = ind-(ind & -ind)\n\t\t\t@tree[ind] = sum[ind]-sum[i]\n\t\tend\n\tend\n\n\tdef query(i)\n\t\ti+=1\n\t\ts=0\n\t\twhile i>=1\n\t\t\ts+=@tree[i]\n\t\t\ti-= (i&-i)\n\t\tend\n\t\ts\n\tend\n\n\tdef add(i,a)\n\t\t@data[i]+=a\n\t\ti+=1\n\t\twhile i<@tree.size\n\t\t\t@tree[i]+=a\n\t\t\ti+=(i&-i)\n\t\tend\n\tend\n\n\tdef [](i)\n\t\t@data[i]\n\tend\nend\n=end\n=begin\nclass Graph\n\n\tdef initialize(g)\n\n\t\tputs 'expected a Array type input' if g.class != Array\n\t\t@graph = g\n\t\tsize = g.size+1\n\t\t@visited = [0]*size\n\t\t@time = [0] *size\n\t\t@father = [-1] *size\n\t\t@lev = [-1] * size\n\n\tend\n\n\tdef dfs(); _dfs(-1, [0],0, 1); self;end\n\n\tdef _dfs(father, time, lev, id)\n\n\t\t@time[id]=time[0]\n\t\t@father[id]=father\n\t\t@visited[id] = 1\n\t\t@lev[id] = lev\n\n\t\t@graph[id].each do |i|\n\t\t\tif @visited[i]!=1\n\t\t\t\ttime[0]+=1\n\t\t\t\t_dfs(id, time, lev+1 ,i)\n\t\t\tend\n\t\tend\n\tend\n\n\tdef inspect\n\t\tputs \"time: #{@time.inspect}\"\n\t\tputs \"father: #{@father.inspect}\"\n\t\tputs \"lev: #{@lev.inspect}\"\n\tend\nend\n\nn = gets.to_i\ng = [0]*(n+1)\n\n(1..n).each do |i|\n\ta = gets.split.map(&:to_i)\n\tg[a[0]] = a[1..(a.size-1)]\nend\n\ng = Graph.new(g)\n\ng.dfs.inspect\n\n=end\n\n\n=begin\nn = gets.to_i\nm = gets\nstanding = 0\n\nm.each_char do |c|\n\tif c == 'X'\n\t\tstanding += 1\n\tend\nend\n\nif standing == m.size/2\n\tputs '0'\n\tputs m\nelse\n\n\tif standing > m.size/2\n\t\td = standing-m.size/2\n\t\tputs d\n\t\t(0..m.size-1).each do |i|\n\t\t\tif m[i] == 'X'\n\t\t\t\tm[i] = 'x'\n\t\t\t\td-=1\n\t\t\t\tbreak if d==0\n\t\t\tend\n\t\tend\n\t\t\n\t\tputs m\n\telse\n\t\td = m.size/2-standing\n\t\tputs d\n\t\t(0..m.size-1).each do |i|\n\t\t\tif m[i] == 'x'\n\t\t\t\tm[i] = 'X'\n\t\t\t\td-=1\n\t\t\t\tbreak if d == 0\n\t\t\tend\n\t\tend\n\t\n\t\tputs m\n\tend\nend\n=end\n\n\nn = gets.to_i\n\nora = [0] * (n+1)\n\n(1..ora.size-1).each do |ind|\n\tora[ind] = ind^ora[ind-1]\nend\n\ns = gets.split.map(&:to_i)\nps = 0\ns.each do |e|\n\tps = ps ^ e\nend\n\n\ns = [0]*(n+1)\n\n(2..n).each do |ind|\n\n\ta = n/ind\n\tb = n%ind\n\tif a%2==0\n\t\ts[ind] = ora[b]\n\telse\n\t\ts[ind] = ora[ind-1]^ora[b]\n\tend\n\tps = ps ^ s[ind]\nend\n=begin\n(2..n).each do |ind|\n\t\nend\n=end\nputs ps \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"}], "negative_code": [], "src_uid": "35d68cc84b4c0025f03f857779f540d7"} {"nl": {"description": " One morning the Cereal Guy found out that all his cereal flakes were gone. He found a note instead of them. It turned out that his smart roommate hid the flakes in one of n boxes. The boxes stand in one row, they are numbered from 1 to n from the left to the right. The roommate left hints like \"Hidden to the left of the i-th box\" (\"To the left of i\"), \"Hidden to the right of the i-th box\" (\"To the right of i\"). Such hints mean that there are no flakes in the i-th box as well. The Cereal Guy wants to know the minimal number of boxes he necessarily needs to check to find the flakes considering all the hints. Or he wants to find out that the hints are contradictory and the roommate lied to him, that is, no box has the flakes.", "input_spec": "The first line contains two integers n and m (1 ≤ n ≤ 1000, 0 ≤ m ≤ 1000) which represent the number of boxes and the number of hints correspondingly. Next m lines contain hints like \"To the left of i\" and \"To the right of i\", where i is integer (1 ≤ i ≤ n). The hints may coincide.", "output_spec": "The answer should contain exactly one integer — the number of boxes that should necessarily be checked or \"-1\" if the hints are contradictory.", "sample_inputs": ["2 1\nTo the left of 2", "3 2\nTo the right of 1\nTo the right of 2", "3 1\nTo the left of 3", "3 2\nTo the left of 2\nTo the right of 1"], "sample_outputs": ["1", "1", "2", "-1"], "notes": null}, "positive_code": [{"source_code": "n,m=gets.split.map(&:to_i)\nl,r=1,n\nm.times{\n\ts=gets.split\n\ti=s[4].to_i\n\tif s[2]==\"left\" then\n\t\tr=[r,i-1].min\n\telse\n\t\tl=[l,i+1].max\n\tend\n}\nputs r=l ? r-l+1 : -1\n"}, {"source_code": "n,m=gets.split.map(&:to_i)\nl,r=1,n\nm.times{\n\ts=gets.split\n\ti=s[4].to_i\n\tif s[2]==\"left\" then\n\t\tr=[r,i-1].min\n\telse\n\t\tl=[l,i+1].max\n\tend\n}\nputs r 0) ? result : -1\n"}, {"source_code": "n,m=gets.split.map(&:to_i)\nl,r=1,n\nm.times{\n\ts=gets.split\n\ti=s[4].to_i\n\tif s[2]==\"left\" then\n\t\tr=[r,i-1].min\n\telse\n\t\tl=[l,i+1].max\n\tend\n}\nputs r 0) ? result : -1\n"}, {"source_code": "n, m = $stdin.gets.split.map(&:to_i)\n\nmn = 1\nmx = n\nm.times do\n l = $stdin.gets\n idx = l.split[-1].to_i\n if l.match(/left/)\n mx = [idx-1, mx].min\n else\n mn = [idx+1, mn].max\n end\nend\n\np [mn, mx]\nresult = mx-mn+1\np (result > 0) ? result : -1\n"}], "src_uid": "dabeb9852332f6a6e730acab7fe61a5c"} {"nl": {"description": "Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.You are given a tree with $$$n$$$ nodes. In the beginning, $$$0$$$ is written on all edges. In one operation, you can choose any $$$2$$$ distinct leaves $$$u$$$, $$$v$$$ and any real number $$$x$$$ and add $$$x$$$ to values written on all edges on the simple path between $$$u$$$ and $$$v$$$.For example, on the picture below you can see the result of applying two operations to the graph: adding $$$2$$$ on the path from $$$7$$$ to $$$6$$$, and then adding $$$-0.5$$$ on the path from $$$4$$$ to $$$5$$$. Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?Leaf is a node of a tree of degree $$$1$$$. Simple path is a path that doesn't contain any node twice.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$2 \\le n \\le 10^5$$$) — the number of nodes. Each of the next $$$n-1$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 \\le u, v \\le n$$$, $$$u \\neq v$$$), meaning that there is an edge between nodes $$$u$$$ and $$$v$$$. It is guaranteed that these edges form a tree.", "output_spec": "If there is a configuration of real numbers written on edges of the tree that we can't achieve by performing the operations, output \"NO\". Otherwise, output \"YES\". You can print each letter in any case (upper or lower).", "sample_inputs": ["2\n1 2", "3\n1 2\n2 3", "5\n1 2\n1 3\n1 4\n2 5", "6\n1 2\n1 3\n1 4\n2 5\n2 6"], "sample_outputs": ["YES", "NO", "NO", "YES"], "notes": "NoteIn the first example, we can add any real $$$x$$$ to the value written on the only edge $$$(1, 2)$$$. In the second example, one of configurations that we can't reach is $$$0$$$ written on $$$(1, 2)$$$ and $$$1$$$ written on $$$(2, 3)$$$. Below you can see graphs from examples $$$3$$$, $$$4$$$: "}, "positive_code": [{"source_code": "N = gets.to_i\nns = Array.new(N+1,0)\n(N-1).times {\n a,b = gets.split.map &:to_i\n ns[a] += 1\n ns[b] += 1\n}\nputs ns.count(2) == 0 ? \"YES\" : \"NO\"\n"}, {"source_code": "n = gets.to_i\ndeg = [0]*(n)\n(n-1).times do\n u,v = gets.split.map(&:to_i)\n deg[u-1] += 1\n deg[v-1] += 1\nend\nputs deg.include?(2) ? \"NO\" : \"YES\""}], "negative_code": [{"source_code": "N = gets.to_i\nns = Array.new(N+1,0)\n(N-1).times {\n a,b = gets.split.map &:to_i\n ns[a] += 1\n ns[b] += 1\n}\nm = ns.count(1)\nputs m*(m-1)/2 >= N-1 ? \"YES\" : \"NO\"\n"}], "src_uid": "ba47e6eea45d32cba660eb6d66a9adbb"} {"nl": {"description": "Three years have passes and nothing changed. It is still raining in London, and Mr. Black has to close all the doors in his home in order to not be flooded. Once, however, Mr. Black became so nervous that he opened one door, then another, then one more and so on until he opened all the doors in his house.There are exactly two exits from Mr. Black's house, let's name them left and right exits. There are several doors in each of the exits, so each door in Mr. Black's house is located either in the left or in the right exit. You know where each door is located. Initially all the doors are closed. Mr. Black can exit the house if and only if all doors in at least one of the exits is open. You are given a sequence in which Mr. Black opened the doors, please find the smallest index $$$k$$$ such that Mr. Black can exit the house after opening the first $$$k$$$ doors.We have to note that Mr. Black opened each door at most once, and in the end all doors became open.", "input_spec": "The first line contains integer $$$n$$$ ($$$2 \\le n \\le 200\\,000$$$) — the number of doors. The next line contains $$$n$$$ integers: the sequence in which Mr. Black opened the doors. The $$$i$$$-th of these integers is equal to $$$0$$$ in case the $$$i$$$-th opened door is located in the left exit, and it is equal to $$$1$$$ in case it is in the right exit. It is guaranteed that there is at least one door located in the left exit and there is at least one door located in the right exit.", "output_spec": "Print the smallest integer $$$k$$$ such that after Mr. Black opened the first $$$k$$$ doors, he was able to exit the house.", "sample_inputs": ["5\n0 0 1 0 0", "4\n1 0 0 1"], "sample_outputs": ["3", "3"], "notes": "NoteIn the first example the first two doors are from the left exit, so when Mr. Black opened both of them only, there were two more closed door in the left exit and one closed door in the right exit. So Mr. Black wasn't able to exit at that moment.When he opened the third door, all doors from the right exit became open, so Mr. Black was able to exit the house.In the second example when the first two doors were opened, there was open closed door in each of the exit.With three doors opened Mr. Black was able to use the left exit."}, "positive_code": [{"source_code": "def read_tokens\n return gets.chomp.to_s.strip.split(' ')\nend\n\ndef read_ints\n data = []\n read_tokens.each { |s|\n data.push(s.to_i)\n }\n return data\nend\n\n\nn, = read_ints\ndoors = read_ints\n\ndef find_occ(arr, num)\n count = 0\n for el in arr\n if el == num\n count += 1\n end\n end\n return count\nend\n\ndef solve(doors)\n k = 0\n zeros = find_occ(doors, 0)\n ones = find_occ(doors, 1)\n for i in 0...doors.length\n if zeros == 0 or ones == 0\n return k\n end\n if doors[i] == 0\n zeros -= 1\n elsif doors[i] == 1\n ones -= 1\n end\n k += 1\n end\n return k\nend\n\nprint solve(doors)"}, {"source_code": "n = gets.to_i\nl = 0\nr = 0\nds = gets.split.map(&:to_i)\nlc = ds.count(0)\nrc = ds.count(1)\nds.each_with_index do |i, ii|\n if i == 0\n l += 1\n else\n r += 1\n end\n if l == lc || r == rc\n puts ii + 1\n exit\n end\nend"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\n(n-1).downto(1) do |i|\n if a[i] != a[i-1]\n puts i\n exit\n end\nend"}, {"source_code": "gets\na = gets.split\nl, r = a.count('0'), a.count('1')\nlo, ro = 0, 0\na.each_with_index do |x, i|\n if x == '0'\n lo += 1\n else\n ro += 1\n end\n if lo == l || ro == r\n puts i + 1\n exit\n end\nend\n"}], "negative_code": [{"source_code": "n = gets.to_i\nl = 0\nr = 0\nans = 0\ngets.split.map(&:to_i).each_with_index do |i, ii|\n if i == 0\n l += 1\n else\n r += 1\n end\n if r > 0 && l > 0 && r != l\n puts ii + 1\n exit\n end\nend"}, {"source_code": "n = gets.to_i\nl = 0\nr = 0\nans = 0\ngets.split.map(&:to_i).each_with_index do |i, ii|\n if i == 0\n l += 1\n else\n r += 1\n end\n if r > 0 && l > 0\n puts ii + 1\n exit\n end\nend"}], "src_uid": "653455bb6762effbf7358d75660a7689"} {"nl": {"description": "You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59.You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format.For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39.", "input_spec": "The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes.", "output_spec": "The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them.", "sample_inputs": ["24\n17:30", "12\n17:30", "24\n99:99"], "sample_outputs": ["17:30", "07:30", "09:09"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/ruby\nn=gets.to_i\ns=gets\ns[3]=s[3]>'5'?'0':s[3]\nif n==12\n\tif s[0]=='0'\n\t\ts[0]='1' if s[1]=='0'\n\telsif s[0]=='1'\n\t\ts[1]='0' if s[1]>'2'\n\telse\n\t\ts[0]=s[1]=='0'?'1':'0'\n\tend\nelse\n\tif s[0]=='2'\n\t\ts[0]='0' if s[1]>'3'\n\telsif s[0]>'2'\n\t\ts[0]='0'\n\tend\nend\nputs s"}, {"source_code": "format = gets.to_i\nhour, minute = gets.strip.split(\":\").map(&:to_i)\nif format == 12 and hour == 0\n hour = 1\nelsif format == 12 and hour > 12\n hour %= 10\n hour = 10 if hour == 0\nelsif format == 24 and hour >= 24\n hour %= 10\nend\nif minute >= 60\n minute %= 10\nend\nputs \"%02d:%02d\" % [hour, minute]\n"}, {"source_code": "#! /usr/bin/env ruby\n\nform = gets.chomp\nt = gets.split(?:)\nh = t[0].to_i\nm = t[1].to_i\n#p form, h, m\n\nt[1][0] = '0' if m > 59\nif form == '24'\n t[0][0] = '0' if h > 23\nelse # form == 12\n if h == 0\n t[0] = '01'\n elsif h <= 12\n # do nothing\n else\n if t[0][1] == '0'\n t[0][0] = '1'\n else\n t[0][0] = '0'\n end\n end\nend\n\nputs t.join ?:\n"}, {"source_code": "type = gets.to_i\nh, m = gets.strip.split(\":\").map(&:to_i)\n\nif m > 59\n m = m % 10\nend\n\nif type == 12\n if h < 1 || h > 12\n if h % 10 == 0\n h = 10\n else\n h = h % 10\n end\n end\nelse\n if h > 23\n h = h % 10\n end\nend\n\nputs \"%02d:%02d\"%[h,m]"}, {"source_code": "t=gets.to_i\na=gets.split(?:).map &:to_i\nif t==24\n a[0]%=10 if a[0]>23\nelse\n if a[0]==0\n a[0]=1\n elsif a[0]>12\n if a[0]%10==0\n a[0]=10\n else\n a[0]%=10\n end\n end\nend\na[1]%=10 if a[1]>59\nputs \"%02d:%02d\" % a\n"}, {"source_code": "t = gets.chomp.to_i\nhh, mm = gets.chomp.split(':').map(&:to_i)\n\nmm = mm % 10 if 59 < mm\nif t == 12\n hh = hh % 10 if 12 < hh\n hh = 10 if hh == 0\nelsif t == 24\n hh = hh % 10 if 23 < hh\nend\n\nputs '%02d:%02d' % [hh, mm]\n"}, {"source_code": "kind = gets.to_i\ntime = gets.chomp\nif time[3] >= '6'\n time[3] = '0'\nend\n\nposs = []\nif kind == 24\n poss = (0 .. 23).map{|v| format(\"%02d\", v)}\nelse\n poss = (1 .. 12).map{|v| format(\"%02d\", v)}\nend\n\nmi = 100\nmini = \"indigo\"\n\nfor p in poss\n s = time[0 .. 1]\n cnt = 0\n for i in 0 .. 1\n cnt += p[i] == s[i] ? 0 : 1\n end\n if mi > cnt\n mi = cnt\n mini = p\n end\nend\ntime[0] = mini[0]\ntime[1] = mini[1]\nputs time\n"}, {"source_code": "type = gets.chomp.to_i\ntime = gets.chomp.split(':').map(&:to_i)\n\ntime[1] %= 10 if time[1] / 10 > 5\n\nif type == 12\n if !((1..12) === time[0])\n if (0..2) === time[0] % 10\n time[0] = 10 + (time[0] % 10)\n else\n time[0] %= 10\n end\n end\nelse\n if !((0..23) === time[0])\n if (0..3) === time[0] % 10\n time[0] = 10 + (time[0] % 10)\n else\n time[0] %= 10\n end\n end\nend\n\nif time[0] / 10 == 0\n time[0] = \"0#{time[0]}\"\nend\n\nif time[1] / 10 == 0\n time[1] = \"0#{time[1]}\"\nend\n\nputs \"#{time[0]}:#{time[1]}\""}], "negative_code": [{"source_code": "#!/usr/bin/ruby\nn=gets.to_i\ns=gets\ns[3]=s[3]>'5'?'0':s[3]\nif n==12\n\tif s[0]=='0'\n\t\ts[0]='1' if s[1]=='0'\n\telsif s[0]=='1'\n\t\ts[1]='0' if s[1]>'2'\n\telse\n\t\ts[0]=s[1]=='1'?'1':'0'\n\tend\nelse\n\tif s[0]=='2'\n\t\ts[0]='0' if s[1]>'3'\n\telsif s[0]>'2'\n\t\ts[0]='0'\n\tend\nend\nputs s"}, {"source_code": "#!/usr/bin/ruby\nn=gets.to_i\ns=gets\ns[3]=s[3]>'6'?'0':s[3]\nif n==12\n\tif s[0]=='0'\n\t\ts[0]='1' if s[1]=='0'\n\telsif s[0]=='1'\n\t\ts[1]='0' if s[1]>'2'\n\telse\n\t\ts[0]=s[1]=='1'?'0':'1'\n\tend\nelse\n\tif s[0]=='2'\n\t\ts[0]='0' if s[1]>'3'\n\telsif s[0]>'2'\n\t\ts[0]='0'\n\tend\nend\nputs s"}, {"source_code": "#!/usr/bin/ruby\nn=gets.to_i\ns=gets\ns[3]=s[3]>'6'?'0':s[3]\nif n==12\n\tif s[0]=='0'\n\t\ts[0]='1' if s[1]=='0'\n\telsif s[0]=='1'\n\t\ts[1]='0' if s[1]>'2'\n\telse\n\t\ts[0]=s[1]=='1'?'1':'0'\n\tend\nelse\n\tif s[0]=='2'\n\t\ts[0]='0' if s[1]>'3'\n\telsif s[0]>'2'\n\t\ts[0]='0'\n\tend\nend\nputs s"}, {"source_code": "t = gets.chomp.to_i\nhh, mm = gets.chomp.split(':').map(&:to_i)\n\nmm = mm % 10 if 59 < mm\nif t == 12\n hh = hh % 10 if 12 < hh\n hh = 10 if hh == 0\nelse\n hh = hh % 10 if 24 < hh\nend\n\nputs '%02d:%02d' % [hh, mm]\n"}, {"source_code": "t = gets.chomp.to_i\nhh, mm = gets.chomp.split(':').map(&:to_i)\n\nmm = mm % 10 if 59 < mm\nif (t == 12 && hh == 0) || (t == 24 && hh == 24)\n hh = 12\nelsif t < hh\n hh = hh % 10\nend\n\nputs \"#{sprintf('%02d', hh)}:#{sprintf('%02d', mm)}\"\n"}, {"source_code": "t = gets.chomp.to_i\nhh, mm = gets.chomp.split(':').map(&:to_i)\n\nmm = mm % 10 if 59 < mm\nif t == 12 && t == 0\n hh = 12\nelsif t < hh\n hh = hh % 10\nend\n\nputs \"#{sprintf('%02d', hh)}:#{sprintf('%02d', mm)}\"\n"}, {"source_code": "t = gets.chomp.to_i\nhh, mm = gets.chomp.split(':').map(&:to_i)\n\nmm = mm % 10 if 59 < mm\nif t == 12\n if hh == 0\n hh = 1\n elsif 12 < hh\n hh = hh % 10\n end\nelse \n if 24 < hh\n hh = hh % 10\n end\nend\n\nputs \"%02d:%02d\" % [hh, mm]\n"}], "src_uid": "88d56c1e3a7ffa94354ce0c70d8e958f"} {"nl": {"description": "There are n animals in the queue to Dr. Dolittle. When an animal comes into the office, the doctor examines him, gives prescriptions, appoints tests and may appoint extra examination. Doc knows all the forest animals perfectly well and therefore knows exactly that the animal number i in the queue will have to visit his office exactly ai times. We will assume that an examination takes much more time than making tests and other extra procedures, and therefore we will assume that once an animal leaves the room, it immediately gets to the end of the queue to the doctor. Of course, if the animal has visited the doctor as many times as necessary, then it doesn't have to stand at the end of the queue and it immediately goes home. Doctor plans to go home after receiving k animals, and therefore what the queue will look like at that moment is important for him. Since the doctor works long hours and she can't get distracted like that after all, she asked you to figure it out. ", "input_spec": "The first line of input data contains two space-separated integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 1014). In the second line are given space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109). Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator). ", "output_spec": "If the doctor will overall carry out less than k examinations, print a single number \"-1\" (without quotes). Otherwise, print the sequence of numbers — number of animals in the order in which they stand in the queue. Note that this sequence may be empty. This case is present in pretests. You can just print nothing or print one \"End of line\"-character. Both will be accepted.", "sample_inputs": ["3 3\n1 2 1", "4 10\n3 3 2 1", "7 10\n1 3 3 1 2 3 1"], "sample_outputs": ["2", "-1", "6 2 3"], "notes": "NoteIn the first sample test: Before examination: {1, 2, 3} After the first examination: {2, 3} After the second examination: {3, 2} After the third examination: {2} In the second sample test: Before examination: {1, 2, 3, 4, 5, 6, 7} After the first examination: {2, 3, 4, 5, 6, 7} After the second examination: {3, 4, 5, 6, 7, 2} After the third examination: {4, 5, 6, 7, 2, 3} After the fourth examination: {5, 6, 7, 2, 3} After the fifth examination: {6, 7, 2, 3, 5} After the sixth examination: {7, 2, 3, 5, 6} After the seventh examination: {2, 3, 5, 6} After the eighth examination: {3, 5, 6, 2} After the ninth examination: {5, 6, 2, 3} After the tenth examination: {6, 2, 3} "}, "positive_code": [{"source_code": "n, k = gets.split.map{|_|_.to_i}\na = gets.split.map{|_|_.to_i}\n\nb = a.sort\nsum = a.inject{|x, y| x + y}\nif sum < k\n puts -1\nelsif sum == k\n puts\nelse\n i = 0\n s = 0\n while (b[i] - s) * (n - i) <= k\n k -= (b[i] - s) * (n - i)\n s = b[i]\n i += 1\n end\n while b[i] - s == 0\n i += 1\n end\n\n t = k / (n - i)\n k -= t * (n - i)\n\n j = 0\n while k > 0 \n if a[j] >= b[i]\n a[j] -= 1\n k -= 1\n end\n j += 1\n end\n\n for k in j...n\n print((k + 1).to_s + \" \") if a[k] > t + s\n end\n for k in 0...j\n print((k + 1).to_s + \" \") if a[k] > t + s\n end\n puts\nend\n\n"}, {"source_code": "n, k = gets.split.map{|_|_.to_i}\na = gets.split.map{|_|_.to_i}\n\nb = a.sort\nsum = a.inject{|x, y| x + y}\nif sum < k\n puts -1\nelsif sum == k\n puts\nelse\n i = 0\n s = 0\n while (b[i] - s) * (n - i) <= k\n k -= (b[i] - s) * (n - i)\n s = b[i]\n i += 1\n end\n while b[i] - s == 0\n i += 1\n end\n\n t = k / (n - i)\n k -= t * (n - i)\n\n j = 0\n while k > 0 \n if a[j] >= b[i]\n a[j] -= 1\n k -= 1\n end\n j += 1\n end\n\n for k in j...n\n print((k + 1).to_s + \" \") if a[k] > t + s\n end\n for k in 0...j\n print((k + 1).to_s + \" \") if a[k] > t + s\n end\n puts\nend\n\n"}, {"source_code": "n, k = gets.split.map{|_|_.to_i}\na = gets.split.map{|_|_.to_i}\n\nb = a.sort\nsum = a.inject{|x, y| x + y}\nif sum < k\n puts -1\nelsif sum == k\n puts\nelse\n i = 0\n s = 0\n while (b[i] - s) * (n - i) <= k\n k -= (b[i] - s) * (n - i)\n s = b[i]\n i += 1\n end\n while b[i] - s == 0\n i += 1\n end\n\n t = k / (n - i)\n k -= t * (n - i)\n\n j = 0\n while k > 0 \n if a[j] >= b[i]\n a[j] -= 1\n k -= 1\n end\n j += 1\n end\n\n for k in j...n\n print((k + 1).to_s + \" \") if a[k] > t + s\n end\n for k in 0...j\n print((k + 1).to_s + \" \") if a[k] > t + s\n end\n puts\nend\n"}, {"source_code": "n, k = gets.split.map{|_|_.to_i}\na = gets.split.map{|_|_.to_i}\n\nb = a.sort\nsum = a.inject{|x, y| x + y}\nif sum < k\n puts -1\nelsif sum == k\n puts\nelse\n i = 0\n s = 0\n while (b[i] - s) * (n - i) <= k\n k -= (b[i] - s) * (n - i)\n s = b[i]\n i += 1\n end\n while b[i] - s == 0\n i += 1\n end\n\n t = k / (n - i)\n k -= t * (n - i)\n\n j = 0\n while k > 0 \n if a[j] >= b[i]\n a[j] -= 1\n k -= 1\n end\n j += 1\n end\n\n for k in j...n\n print((k + 1).to_s + \" \") if a[k] > t + s\n end\n for k in 0...j\n print((k + 1).to_s + \" \") if a[k] > t + s\n end\n puts\nend\n\n"}, {"source_code": "n, k = gets.split.map{|_|_.to_i}\na = gets.split.map{|_|_.to_i}\n\nb = a.sort\nsum = a.inject{|x, y| x + y}\nif sum < k\n puts -1\nelsif sum == k\n puts\nelse\n i = 0\n s = 0\n while (b[i] - s) * (n - i) <= k\n k -= (b[i] - s) * (n - i)\n s = b[i]\n i += 1\n end\n while b[i] - s == 0\n i += 1\n end\n\n t = k / (n - i)\n k -= t * (n - i)\n\n j = 0\n while k > 0 \n if a[j] >= b[i]\n a[j] -= 1\n k -= 1\n end\n j += 1\n end\n\n for k in j...n\n print((k + 1).to_s + \" \") if a[k] > t + s\n end\n for k in 0...j\n print((k + 1).to_s + \" \") if a[k] > t + s\n end\n puts\nend\n\n"}, {"source_code": "n, k = gets.split.map{|_|_.to_i}\na = gets.split.map{|_|_.to_i}\n\nb = a.sort\nsum = a.inject{|x, y| x + y}\nif sum < k\n puts -1\nelsif sum == k\n puts\nelse\n i = 0\n s = 0\n while (b[i] - s) * (n - i) <= k\n k -= (b[i] - s) * (n - i)\n s = b[i]\n i += 1\n end\n while b[i] - s == 0\n i += 1\n end\n\n t = k / (n - i)\n k -= t * (n - i)\n\n j = 0\n while k > 0 \n if a[j] >= b[i]\n a[j] -= 1\n k -= 1\n end\n j += 1\n end\n\n for k in j...n\n print((k + 1).to_s + \" \") if a[k] > t + s\n end\n for k in 0...j\n print((k + 1).to_s + \" \") if a[k] > t + s\n end\n puts\nend"}, {"source_code": "n, k = gets.split.map{|_|_.to_i}\na = gets.split.map{|_|_.to_i}\n\nb = a.sort\nsum = a.inject{|x, y| x + y}\nif sum < k\n puts -1\nelsif sum == k\n puts\nelse\n i = 0\n s = 0\n while (b[i] - s) * (n - i) <= k\n k -= (b[i] - s) * (n - i)\n s = b[i]\n i += 1\n end\n while b[i] - s == 0\n i += 1\n end\n\n t = k / (n - i)\n k -= t * (n - i)\n\n j = 0\n while k > 0 \n if a[j] >= b[i]\n a[j] -= 1\n k -= 1\n end\n j += 1\n end\n\n for k in j...n\n print((k + 1).to_s + \" \") if a[k] > t + s\n end\n for k in 0...j\n print((k + 1).to_s + \" \") if a[k] > t + s\n end\n puts\nend\n\n"}, {"source_code": "n, k = gets.split.map{|_|_.to_i}\na = gets.split.map{|_|_.to_i}\n\nb = a.sort\nsum = a.inject{|x, y| x + y}\nif sum < k\n puts -1\nelsif sum == k\n puts\nelse\n i = 0\n s = 0\n while (b[i] - s) * (n - i) <= k\n k -= (b[i] - s) * (n - i)\n s = b[i]\n i += 1\n end\n while b[i] - s == 0\n i += 1\n end\n\n t = k / (n - i)\n k -= t * (n - i)\n\n j = 0\n while k > 0 \n if a[j] >= b[i]\n a[j] -= 1\n k -= 1\n end\n j += 1\n end\n\n for k in j...n\n print((k + 1).to_s + \" \") if a[k] > t + s\n end\n for k in 0...j\n print((k + 1).to_s + \" \") if a[k] > t + s\n end\n puts\nend\n"}, {"source_code": "n, k = gets.split.map{|_|_.to_i}\na = gets.split.map{|_|_.to_i}\n\nb = a.sort\nsum = a.inject{|x, y| x + y}\nif sum < k\n puts -1\nelsif sum == k\n puts\nelse\n i = 0\n s = 0\n while (b[i] - s) * (n - i) <= k\n k -= (b[i] - s) * (n - i)\n s = b[i]\n i += 1\n end\n while b[i] - s == 0\n i += 1\n end\n\n t = k / (n - i)\n k -= t * (n - i)\n\n j = 0\n while k > 0 \n if a[j] >= b[i]\n a[j] -= 1\n k -= 1\n end\n j += 1\n end\n\n for k in j...n\n print((k + 1).to_s + \" \") if a[k] > t + s\n end\n for k in 0...j\n print((k + 1).to_s + \" \") if a[k] > t + s\n end\n puts\nend\n\n"}], "negative_code": [{"source_code": "n, k = gets.split.map{|_|_.to_i}\na = gets.split.map{|_|_.to_i}\n\nb = a.sort\nif (a.length == 1 ? a[0] : a.inject{|x,y| x + y}) <= k\n puts -1\nelse\n i = 0\n s = 0\n while (b[i] - s) * (n - i) <= k\n k -= (b[i] - s) * (n - i)\n s = b[i]\n i += 1\n end\n while b[i] - s == 0\n i += 1\n end\n\n t = k / (n - i)\n k -= t * (n - i)\n\n j = 0\n while k > 0 \n if a[j] >= b[i]\n a[j] -= 1\n k -= 1\n end\n j += 1\n end\n\n ans = \"\"\n for k in j...n\n if a[k] > t + s\n ans += (k + 1).to_s + \" \"\n end\n end\n for k in 0...j\n if a[k] > t + s\n ans += (k + 1).to_s + \" \"\n end\n end\n puts ans.strip\nend\n"}, {"source_code": "n, k = gets.split.map{|_|_.to_i}\na = gets.split.map{|_|_.to_i}\n\nb = a.sort\nif a.inject{|x,y| x + y} <= k\n puts -1\nelse\n i = 0\n s = 0\n while (b[i] - s) * (n - i) <= k\n k -= (b[i] - s) * (n - i)\n s = b[i]\n i += 1\n end\n while b[i] - s == 0\n i += 1\n end\n\n t = k / (n - i)\n k -= t * (n - i)\n\n j = 0\n while k > 0 \n if a[j] >= b[i]\n a[j] -= 1\n k -= 1\n else\n a[j] = 0\n end\n j += 1\n end\n\n ans = \"\"\n for k in j...n\n if a[k] > t + s\n ans += (k + 1).to_s + \" \"\n end\n end\n for k in 0...j\n if a[k] > t + s\n ans += (k + 1).to_s + \" \"\n end\n end\n puts ans.strip\nend\n"}], "src_uid": "8126f40439f2ea808683d22115421c18"} {"nl": {"description": "Polycarp has recently got himself a new job. He now earns so much that his old wallet can't even store all the money he has.Berland bills somehow come in lots of different sizes. However, all of them are shaped as rectangles (possibly squares). All wallets are also produced in form of rectangles (possibly squares).A bill $$$x \\times y$$$ fits into some wallet $$$h \\times w$$$ if either $$$x \\le h$$$ and $$$y \\le w$$$ or $$$y \\le h$$$ and $$$x \\le w$$$. Bills can overlap with each other in a wallet and an infinite amount of bills can fit into a wallet. That implies that all the bills Polycarp currently have fit into a wallet if every single one of them fits into it independently of the others.Now you are asked to perform the queries of two types: $$$+~x~y$$$ — Polycarp earns a bill of size $$$x \\times y$$$; $$$?~h~w$$$ — Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \\times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.For each query of type $$$2$$$ print \"YES\" if all the bills he has earned to this moment fit into a wallet of given size. Print \"NO\" otherwise.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$2 \\le n \\le 5 \\cdot 10^5$$$) — the number of queries. Each of the next $$$n$$$ lines contains a query of one of these two types: $$$+~x~y$$$ ($$$1 \\le x, y \\le 10^9$$$) — Polycarp earns a bill of size $$$x \\times y$$$; $$$?~h~w$$$ ($$$1 \\le h, w \\le 10^9$$$) — Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \\times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.", "output_spec": "For each query of type $$$2$$$ print \"YES\" if all the bills he has earned to this moment fit into a wallet of given size. Print \"NO\" otherwise.", "sample_inputs": ["9\n+ 3 2\n+ 2 3\n? 1 20\n? 3 3\n? 2 3\n+ 1 5\n? 10 10\n? 1 5\n+ 1 1"], "sample_outputs": ["NO\nYES\nYES\nYES\nNO"], "notes": "NoteThe queries of type $$$2$$$ of the example: Neither bill fits; Both bills fit (just checking that you got that bills can overlap); Both bills fit (both bills are actually the same); All bills fit (too much of free space in a wallet is not a problem); Only bill $$$1 \\times 5$$$ fit (all the others don't, thus it's \"NO\"). "}, "positive_code": [{"source_code": "a, b = [0, 0]\ngets.strip.to_i.times do\n q, l, r = gets.strip.split(' ')\n if q == '?'\n x = [l, r].map(&:to_i).sort\n puts a <= x[0] && b <= x[1] ? \"YES\" : \"NO\"\n else\n x = [l, r].map(&:to_i).sort\n a = [a, x[0]].max\n b = [b , x[1]].max\n end\nend"}, {"source_code": "DBG = true\nn = gets.to_i\nmx = 0\nmy = 0\nfor i in 0...n\n q,xx,yy = gets.split\n x = xx.to_i\n y = yy.to_i\n if x > y # make x<=y\n z = x\n x = y\n y = z\n end\n if q == '+'\n mx = x if x > mx\n my = y if y > my\n else\n puts ((x >= mx && y >= my) ? \"YES\" : \"NO\")\n end\nend\n"}, {"source_code": "if File.exists?(\"input\")\n $stdin = File.open(\"input\")\n $stdout = File.open(\"output\",\"w\")\nend\n\nn = gets.to_i\n\nmaxone = 0\nmaxtwo = 0\nn.times do\n q = gets.chomp.split(\" \")\n if q[0]==\"+\"\n size = q[1..2].map(&:to_i).sort\n maxone = size[0] if size[0]>maxone\n maxtwo = size[1] if size[1]>maxtwo\n else\n size = q[1..2].map(&:to_i).sort\n if size[0]>=maxone and size[1]>=maxtwo\n puts \"YES\"\n else\n puts \"NO\"\n end\n end\nend"}], "negative_code": [], "src_uid": "fe939f7503f928ff4dbe8d05c5643f38"} {"nl": {"description": "You are given an array $$$a$$$ consisting of $$$n$$$ integers $$$a_1, a_2, \\dots , a_n$$$.In one operation you can choose two elements of the array and replace them with the element equal to their sum (it does not matter where you insert the new element). For example, from the array $$$[2, 1, 4]$$$ you can obtain the following arrays: $$$[3, 4]$$$, $$$[1, 6]$$$ and $$$[2, 5]$$$.Your task is to find the maximum possible number of elements divisible by $$$3$$$ that are in the array after performing this operation an arbitrary (possibly, zero) number of times.You have to answer $$$t$$$ independent queries.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of queries. The first line of each query contains one integer $$$n$$$ ($$$1 \\le n \\le 100$$$). The second line of each query contains $$$n$$$ integers $$$a_1, a_2, \\dots , a_n$$$ ($$$1 \\le a_i \\le 10^9$$$). ", "output_spec": "For each query print one integer in a single line — the maximum possible number of elements divisible by $$$3$$$ that are in the array after performing described operation an arbitrary (possibly, zero) number of times.", "sample_inputs": ["2\n5\n3 1 2 3 1\n7\n1 1 1 1 1 2 2"], "sample_outputs": ["3\n3"], "notes": "NoteIn the first query of the example you can apply the following sequence of operations to obtain $$$3$$$ elements divisible by $$$3$$$: $$$[3, 1, 2, 3, 1] \\rightarrow [3, 3, 3, 1]$$$.In the second query you can obtain $$$3$$$ elements divisible by $$$3$$$ with the following sequence of operations: $$$[1, 1, 1, 1, 1, 2, 2] \\rightarrow [1, 1, 1, 1, 2, 3] \\rightarrow [1, 1, 1, 3, 3] \\rightarrow [2, 1, 3, 3] \\rightarrow [3, 3, 3]$$$."}, "positive_code": [{"source_code": "t = gets.to_i\nt.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n cnt = [0,0,0]\n a.each do |v|\n cnt[v%3] += 1\n end\n k = (cnt[1]-cnt[2]).abs\n p cnt[0] + cnt[1,2].min + k/3\nend\n"}, {"source_code": "def solve(a)\n a = a.group_by do |e|\n e % 3 == 0 ? 0 : (e + 1) % 3 == 0 ? 1 : 2\n end\n one, two = a[1] || [], a[2] || []\n # puts one.inspect\n # puts two.inspect\n # puts a[0].inspect\n mn = [one.size, two.size].min\n mx = [one.size, two.size].max\n # puts mn\n # puts mx\n (a[0] || []).size + mn + ((mx - mn) / 3.0).to_i\nend\n\ngets.strip.to_i.times do |t|\n n = gets.strip.to_i\n a = gets.strip.split(' ').map(&:to_i)\n puts solve(a)\nend"}], "negative_code": [], "src_uid": "e59cddb6c941b1d7556ee9c020701007"} {"nl": {"description": " For god's sake, you're boxes with legs! It is literally your only purpose! Walking onto buttons! How can you not do the one thing you were designed for?Oh, that's funny, is it? Oh it's funny? Because we've been at this for twelve hours and you haven't solved it either, so I don't know why you're laughing. You've got one hour! Solve it! Wheatley decided to try to make a test chamber. He made a nice test chamber, but there was only one detail absent — cubes.For completing the chamber Wheatley needs $$$n$$$ cubes. $$$i$$$-th cube has a volume $$$a_i$$$.Wheatley has to place cubes in such a way that they would be sorted in a non-decreasing order by their volume. Formally, for each $$$i>1$$$, $$$a_{i-1} \\le a_i$$$ must hold.To achieve his goal, Wheatley can exchange two neighbouring cubes. It means that for any $$$i>1$$$ you can exchange cubes on positions $$$i-1$$$ and $$$i$$$.But there is a problem: Wheatley is very impatient. If Wheatley needs more than $$$\\frac{n \\cdot (n-1)}{2}-1$$$ exchange operations, he won't do this boring work.Wheatly wants to know: can cubes be sorted under this conditions?", "input_spec": "Each test contains multiple test cases. The first line contains one positive integer $$$t$$$ ($$$1 \\le t \\le 1000$$$), denoting the number of test cases. Description of the test cases follows. The first line of each test case contains one positive integer $$$n$$$ ($$$2 \\le n \\le 5 \\cdot 10^4$$$) — number of cubes. The second line contains $$$n$$$ positive integers $$$a_i$$$ ($$$1 \\le a_i \\le 10^9$$$) — volumes of cubes. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.", "output_spec": "For each test case, print a word in a single line: \"YES\" (without quotation marks) if the cubes can be sorted and \"NO\" (without quotation marks) otherwise.", "sample_inputs": ["3\n5\n5 3 2 1 4\n6\n2 2 2 2 2 2\n2\n2 1"], "sample_outputs": ["YES\nYES\nNO"], "notes": "NoteIn the first test case it is possible to sort all the cubes in $$$7$$$ exchanges.In the second test case the cubes are already sorted.In the third test case we can make $$$0$$$ exchanges, but the cubes are not sorted yet, so the answer is \"NO\"."}, "positive_code": [{"source_code": "T = gets.to_i\n\nT.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n\n if a.each_cons(2).all? { |x, y| x > y }\n puts 'NO'\n else\n puts 'YES'\n end\nend\n"}, {"source_code": "t = gets.chomp.to_i\n\ndef solution(t)\n sorted, inversion_count = sort_inversion_count(t)\n return inversion_count\nend\n\ndef sort_inversion_count(t)\n midpoint = t.length / 2\n left_half = t[0...midpoint]\n right_half = t[midpoint..t.length]\n\n if midpoint == 0\n return t, 0\n end\n\n sorted_left_half, left_half_inversion_count = sort_inversion_count(left_half)\n sorted_right_half, right_half_inversion_count = sort_inversion_count(right_half)\n\n sorted = []\n inversion_count = 0\n while sorted_left_half.length > 0 or sorted_right_half.length > 0\n if sorted_left_half.empty?\n sorted.push sorted_right_half.shift\n elsif sorted_right_half.empty?\n sorted.push sorted_left_half.shift\n else\n if sorted_left_half[0] > sorted_right_half[0]\n inversion_count += sorted_left_half.length\n sorted.push sorted_right_half.shift\n else\n sorted.push sorted_left_half.shift\n end\n end\n end\n\n return sorted, inversion_count + left_half_inversion_count + right_half_inversion_count\nend\n\nwhile t>0 do\n n = gets.chomp.to_i\n arr = gets.chomp.split(\" \").map(&:to_i)\n threshold = (n*(n-1)/2) - 1\n\n inversions = solution(arr)\n res = inversions > threshold ? \"NO\" : \"YES\"\n\n puts res\n\n t -= 1\nend"}], "negative_code": [], "src_uid": "b34f29e6fb586c22fa1b9e559c5a6c50"} {"nl": {"description": "Let $$$n$$$ be a positive integer. Let $$$a, b, c$$$ be nonnegative integers such that $$$a + b + c = n$$$.Alice and Bob are gonna play rock-paper-scissors $$$n$$$ times. Alice knows the sequences of hands that Bob will play. However, Alice has to play rock $$$a$$$ times, paper $$$b$$$ times, and scissors $$$c$$$ times.Alice wins if she beats Bob in at least $$$\\lceil \\frac{n}{2} \\rceil$$$ ($$$\\frac{n}{2}$$$ rounded up to the nearest integer) hands, otherwise Alice loses.Note that in rock-paper-scissors: rock beats scissors; paper beats rock; scissors beat paper. The task is, given the sequence of hands that Bob will play, and the numbers $$$a, b, c$$$, determine whether or not Alice can win. And if so, find any possible sequence of hands that Alice can use to win.If there are multiple answers, print any of them.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. Then, $$$t$$$ testcases follow, each consisting of three lines: The first line contains a single integer $$$n$$$ ($$$1 \\le n \\le 100$$$). The second line contains three integers, $$$a, b, c$$$ ($$$0 \\le a, b, c \\le n$$$). It is guaranteed that $$$a + b + c = n$$$. The third line contains a string $$$s$$$ of length $$$n$$$. $$$s$$$ is made up of only 'R', 'P', and 'S'. The $$$i$$$-th character is 'R' if for his $$$i$$$-th Bob plays rock, 'P' if paper, and 'S' if scissors. ", "output_spec": "For each testcase: If Alice cannot win, print \"NO\" (without the quotes). Otherwise, print \"YES\" (without the quotes). Also, print a string $$$t$$$ of length $$$n$$$ made up of only 'R', 'P', and 'S' — a sequence of hands that Alice can use to win. $$$t$$$ must contain exactly $$$a$$$ 'R's, $$$b$$$ 'P's, and $$$c$$$ 'S's. If there are multiple answers, print any of them. The \"YES\" / \"NO\" part of the output is case-insensitive (i.e. \"yEs\", \"no\" or \"YEs\" are all valid answers). Note that 'R', 'P' and 'S' are case-sensitive.", "sample_inputs": ["2\n3\n1 1 1\nRPS\n3\n3 0 0\nRPS"], "sample_outputs": ["YES\nPSR\nNO"], "notes": "NoteIn the first testcase, in the first hand, Alice plays paper and Bob plays rock, so Alice beats Bob. In the second hand, Alice plays scissors and Bob plays paper, so Alice beats Bob. In the third hand, Alice plays rock and Bob plays scissors, so Alice beats Bob. Alice beat Bob 3 times, and $$$3 \\ge \\lceil \\frac{3}{2} \\rceil = 2$$$, so Alice wins.In the second testcase, the only sequence of hands that Alice can play is \"RRR\". Alice beats Bob only in the last hand, so Alice can't win. $$$1 < \\lceil \\frac{3}{2} \\rceil = 2$$$."}, "positive_code": [{"source_code": "WIN = {\n 'S' => 'P',\n 'P' => 'R',\n 'R' => 'S'\n}\n\nLOSE = {\n 'P' => 'S',\n 'R' => 'P',\n 'S' => 'R'\n}\n\ndef solve(n, a, b, c, seq)\n me = {'R' => a, 'P' => b, 'S' => c}\n other = seq.inject(Hash.new(0)) { |m, c| m[c] += 1; m }\n wins = me.inject(0) { |m, (k, v)| m += [v, other[WIN[k]]].min; m }\n return \"NO\" if wins < (n / 2.0).ceil\n to_win = {}\n to_any = {}\n me.each do |k, v|\n to_win[k] = [v, other[WIN[k]]].min\n to_any[k] = v - [v, other[WIN[k]]].min\n end\n res = []\n seq.each do |c|\n # puts to_win\n # puts to_any\n if to_win[LOSE[c]] > 0\n to_win[LOSE[c]] -= 1\n res << LOSE[c]\n else\n play = to_any.select { |k, v| !v.zero? }.keys.first\n # puts \"play #{play}\"\n to_any[play] -= 1\n res << play\n end\n end\n wins >= (n / 2.0).ceil ? \"YES\\n#{res.join('')}\" : \"NO\"\n # res.size == seq.size ? \"YES\\n#{res.join('')}\" : \"NO\"\nend\n\n\ngets.strip.to_i.times do\n n = gets.strip.to_i\n a, b, c = gets.strip.split(' ').map(&:to_i)\n seq = gets.strip.split('')\n puts solve(n, a, b, c, seq)\nend"}], "negative_code": [{"source_code": "WIN = {\n 'P' => 'S',\n 'S' => 'R',\n 'R' => 'P'\n}\n\ndef solve(n, a, b, c, seq)\n me = {'R' => a, 'P' => b, 'S' => c}\n other = seq.inject(Hash.new(0)) { |m, c| m[c] += 1; m }\n res = []\n wins = 0\n seq.each_with_index do |c, i|\n if me[WIN[c]] > 0\n res << WIN[c]\n me[WIN[c]] -= 1\n other[c] -= 1\n wins += 1\n else\n left = me.select { |k, v| !v.zero? }.keys\n to_play = left.min do |non_win|\n other[WIN[non_win]]\n end\n res << to_play\n me[to_play] -= 1\n other[c] -= 1\n end\n end\n wins >= (n / 2.0).ceil ? \"YES\\n#{res.join('')}\" : \"NO\"\n # res.size == seq.size ? \"YES\\n#{res.join('')}\" : \"NO\"\nend\n\n\ngets.strip.to_i.times do\n n = gets.strip.to_i\n a, b, c = gets.strip.split(' ').map(&:to_i)\n seq = gets.strip.split('')\n puts solve(n, a, b, c, seq)\nend"}, {"source_code": "WIN = {\n 'S' => 'P',\n 'P' => 'R',\n 'R' => 'S'\n}\n\ndef solve(n, a, b, c, seq)\n me = {'R' => a, 'P' => b, 'S' => c}\n other = seq.inject(Hash.new(0)) { |m, c| m[c] += 1; m }\n res = []\n wins = 0\n seq.each_with_index do |c, i|\n if me[WIN[c]] > 0\n res << WIN[c]\n me[WIN[c]] -= 1\n other[c] -= 1\n wins += 1\n else\n left = me.select { |k, v| !v.zero? }.keys\n to_play = left.min do |non_win|\n other[WIN[non_win]]\n end\n res << to_play\n me[to_play] -= 1\n other[c] -= 1\n end\n end\n wins >= (n / 2.0).ceil ? \"YES\\n#{res.join('')}\" : \"NO\"\n # res.size == seq.size ? \"YES\\n#{res.join('')}\" : \"NO\"\nend\n\n\ngets.strip.to_i.times do\n n = gets.strip.to_i\n a, b, c = gets.strip.split(' ').map(&:to_i)\n seq = gets.strip.split('')\n puts solve(n, a, b, c, seq)\nend"}], "src_uid": "bee33afb70e4c3e062ec7980b44cc0dd"} {"nl": {"description": "Alice and Bob are playing a game on an array $$$a$$$ of $$$n$$$ positive integers. Alice and Bob make alternating moves with Alice going first.In his/her turn, the player makes the following move: If $$$a_1 = 0$$$, the player loses the game, otherwise: Player chooses some $$$i$$$ with $$$2\\le i \\le n$$$. Then player decreases the value of $$$a_1$$$ by $$$1$$$ and swaps $$$a_1$$$ with $$$a_i$$$. Determine the winner of the game if both players play optimally.", "input_spec": "The input consists of multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 \\leq t \\leq 2 \\cdot 10^4)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(2 \\leq n \\leq 10^5)$$$  — the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2 \\ldots a_n$$$ $$$(1 \\leq a_i \\leq 10^9)$$$  — the elements of the array $$$a$$$. It is guaranteed that sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, if Alice will win the game, output \"Alice\". Otherwise, output \"Bob\". You can output each letter in any case. For example, \"alIcE\", \"Alice\", \"alice\" will all be considered identical.", "sample_inputs": ["3\n\n2\n\n1 1\n\n2\n\n2 1\n\n3\n\n5 4 4"], "sample_outputs": ["Bob\nAlice\nAlice"], "notes": "NoteIn the first testcase, in her turn, Alice can only choose $$$i = 2$$$, making the array equal $$$[1, 0]$$$. Then Bob, in his turn, will also choose $$$i = 2$$$ and make the array equal $$$[0, 0]$$$. As $$$a_1 = 0$$$, Alice loses.In the second testcase, once again, players can only choose $$$i = 2$$$. Then the array will change as follows: $$$[2, 1] \\to [1, 1] \\to [1, 0] \\to [0, 0]$$$, and Bob loses.In the third testcase, we can show that Alice has a winning strategy."}, "positive_code": [{"source_code": "gets.to_i.times do\n n=gets.to_i\n arr=gets.chomp.split.map(&:to_i)\n if arr[0]==arr.min\n puts \"Bob\"\n else\n puts \"Alice\"\n end\nend"}], "negative_code": [], "src_uid": "4c5187193cf7f2d2721cedbb15b2a1c3"} {"nl": {"description": "Leha decided to move to a quiet town Vičkopolis, because he was tired by living in Bankopolis. Upon arrival he immediately began to expand his network of hacked computers. During the week Leha managed to get access to n computers throughout the town. Incidentally all the computers, which were hacked by Leha, lie on the same straight line, due to the reason that there is the only one straight street in Vičkopolis.Let's denote the coordinate system on this street. Besides let's number all the hacked computers with integers from 1 to n. So the i-th hacked computer is located at the point xi. Moreover the coordinates of all computers are distinct. Leha is determined to have a little rest after a hard week. Therefore he is going to invite his friend Noora to a restaurant. However the girl agrees to go on a date with the only one condition: Leha have to solve a simple task.Leha should calculate a sum of F(a) for all a, where a is a non-empty subset of the set, that consists of all hacked computers. Formally, let's denote A the set of all integers from 1 to n. Noora asks the hacker to find value of the expression . Here F(a) is calculated as the maximum among the distances between all pairs of computers from the set a. Formally, . Since the required sum can be quite large Noora asks to find it modulo 109 + 7.Though, Leha is too tired. Consequently he is not able to solve this task. Help the hacker to attend a date.", "input_spec": "The first line contains one integer n (1 ≤ n ≤ 3·105) denoting the number of hacked computers. The second line contains n integers x1, x2, ..., xn (1 ≤ xi ≤ 109) denoting the coordinates of hacked computers. It is guaranteed that all xi are distinct.", "output_spec": "Print a single integer — the required sum modulo 109 + 7.", "sample_inputs": ["2\n4 7", "3\n4 3 1"], "sample_outputs": ["3", "9"], "notes": "NoteThere are three non-empty subsets in the first sample test:, and . The first and the second subset increase the sum by 0 and the third subset increases the sum by 7 - 4 = 3. In total the answer is 0 + 0 + 3 = 3.There are seven non-empty subsets in the second sample test. Among them only the following subsets increase the answer: , , , . In total the sum is (4 - 3) + (4 - 1) + (3 - 1) + (4 - 1) = 9."}, "positive_code": [{"source_code": "n=gets.to_i\nb=gets.split.map(&:to_i).sort!\na=0\nM=10**9+7\np=[1]\nn.times{p[p.size]=p[p.size-1]*2%M}\n(n-1).times{|i|a+=(p[n]-p[n-i-1]-p[i+1]+1+M)*(b[i+1]-b[i])}\np a%M\n"}, {"source_code": "n=gets.to_i-1\nb=gets.split.map(&:to_i).sort!\na=0\nM=10**9+7\np=[1]\nn.times{p[p.size]=p[p.size-1]*2%M}\nn.times{|i|a+=(p[i+1]-1)*(p[n-i]-1)*(b[i+1]-b[i])}\np a%M\n"}], "negative_code": [{"source_code": "n=gets.to_i-1\nb=gets.split.map(&:to_i).sort!\na=0\nM=10**9+7\np=[1]\nn.times{p[p.size]=p[p.size-1]*2%M}\nn.times{|i|a+=(p[i+1]-1)*(p[-i]-1)*(b[i+1]-b[i])}\np a%M\n"}], "src_uid": "acff03fe274e819a74b5e9350a859471"} {"nl": {"description": "The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate.The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the maximum number of votes, then the winner is the one with a smaller index.At the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one with a smaller index.Determine who will win the elections.", "input_spec": "The first line of the input contains two integers n, m (1 ≤ n, m ≤ 100) — the number of candidates and of cities, respectively. Each of the next m lines contains n non-negative integers, the j-th number in the i-th line aij (1 ≤ j ≤ n, 1 ≤ i ≤ m, 0 ≤ aij ≤ 109) denotes the number of votes for candidate j in city i. It is guaranteed that the total number of people in all the cities does not exceed 109.", "output_spec": "Print a single number — the index of the candidate who won the elections. The candidates are indexed starting from one.", "sample_inputs": ["3 3\n1 2 3\n2 3 1\n1 2 1", "3 4\n10 10 3\n5 1 6\n2 2 2\n1 5 7"], "sample_outputs": ["2", "1"], "notes": "NoteNote to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.Note to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smaller index, so the city chose candidate 1. City 2 chosen candidate 3. City 3 chosen candidate 1, due to the fact that everyone has the same number of votes, and 1 has the smallest index. City 4 chosen the candidate 3. On the second stage the same number of cities chose candidates 1 and 3. The winner is candidate 1, the one with the smaller index."}, "positive_code": [{"source_code": "\nn,m = gets.chomp.split.map{|x| x.to_i}\n\ncandidate_wins = Array.new(n+1){0}\n#cities\n(0...m).each do |i|\n\tarr = gets.chomp.split.map{|x| x.to_i}\n\tcurr_winner = 0\n\tcurr_max_votes = -1\n\t(0...n).each do |j|\n\t\tif arr[j] > curr_max_votes\n\t\t\tcurr_max_votes = arr[j]\n\t\t\tcurr_winner = j\n\t\tend\n\tend\n\tcandidate_wins[curr_winner+1]+=1\nend\noverall_winner = 0\nover_all_win = -1\n(1..n).each do |i|\n\tif candidate_wins[i] > over_all_win\n\t\toverall_winner = i\n\t\tover_all_win = candidate_wins[i]\n\tend\nend\n\nputs overall_winner\n"}, {"source_code": "#!/usr/bin/ruby\nN,M=gets.to_i\nA=$<.map{|e|e.split.map(&:to_i)}\nh=Hash.new(0)\nA.each{|e|h[e.each_with_index.map{|x,i|[-x,i]}.min[1]]+=1}\np h.min_by{|k,v|[-v,k]}[0]+1"}, {"source_code": "n,m = gets.split(\" \").map(&:to_i)\ncand = [0]*n\nfor i in (1..m)\n li = gets.split(\" \").map(&:to_i)\n cand[li.index(li.max)]+=1\nend\nputs cand.index(cand.max) + 1"}, {"source_code": "n,m=gets.split.map &:to_i;c=[0]*n;m.times{l=gets.split.map &:to_i;c[l.index(l.max)]+=1};p c.index(c.max)+1\n"}, {"source_code": "# http://codeforces.com/contest/570/problem/A\nn, m = gets.chomp.split.map(&:to_i)\narray = Array.new(n) { 0 }\n\nm.times do\n votes = gets.chomp.split.map(&:to_i)\n max = votes.max\n\n array[votes.index(max)] += 1\nend\n\nmax = array.max\nputs array.index(max) + 1\n"}, {"source_code": "n,m=gets.split.map &:to_i;c=[0]*n;m.times{l=gets.split.map &:to_i;c[l.index(l.max)]+=1};p c.index(c.max)+1\n"}, {"source_code": "n, m = gets.split(' ').map{|x| x.to_i}\nres = []\nm.times do |x|\n\tres[x] = gets.split.map{|x| x.to_i}\n\tres[x] = res[x].index(res[x].max)\n\t\nend\n\ncount = []\nfor i in 0...n\n\tcount[i] = res.count(i)\nend\n\nputs count.index(count.max)+1\n"}, {"source_code": "n,m=gets.split.map &:to_i;c=[0]*n;m.times{l=gets.split.map &:to_i;c[l.index(l.max)]+=1};p c.index(c.max)+1\n"}, {"source_code": "n, m = gets.split(\" \").map(&:to_i)\na = Array.new(n){0}\nm.times{\n voices = gets.split(\" \")\n best = 0\n voices.each_index { |i| best = voices[i].to_i>voices[best].to_i ? i : best}\n a[best] += 1\n}\nbest = 0\na.each_index { |i| best = a[i]>a[best] ? i : best}\nputs best+1"}, {"source_code": "n,m=gets.split.map &:to_i;c=[0]*n;m.times{l=gets.split.map &:to_i;c[l.index(l.max)]+=1};p c.index(c.max)+1\n"}, {"source_code": "c=(0..100).map{0}\nn,m=gets.split.map{|e| e.to_i}\nm.times{\n\tx=gets.split.map{|e| e.to_i}\n\tc[x.index(x.max)+1]+=1\n}\np c.index(c.max)\n\n"}, {"source_code": "def read_ints; gets.chomp.split.map(&:to_i) end\nn,m = read_ints\ncity_winners = []\nm.times do \n\tcity_winners< x\n\t\t\tx = t[i]\n\t\t\ty = i\n\t\tend\n\tend\t\n\ta[y] += 1\n\tx,y = -1,0\nend\nfor i in 0...n\n\tif a[i] > x\n\t\tx = a[i]\n\t\ty = i\n\tend\nend\np y+1"}, {"source_code": "n, m = gets.chomp.split(\"\\s\").map(&:to_i)\na = m.times.map { gets.chomp.split(\"\\s\").map(&:to_i) }\n\nwinners = a.map do |votes_by_city|\n votes_by_city.find_index(votes_by_city.max)\nend\n\nputs winners.sort.max_by { |e| winners.count(e) } + 1\n"}, {"source_code": "n,m=gets.split.map &:to_i;c=[0]*n;m.times{l=gets.split.map &:to_i;c[l.index(l.max)]+=1};p c.index(c.max)+1"}, {"source_code": "# coding: utf-8\n\nn, m = gets.split.map(&:to_i) # n candidates, m cities\nvotes = []\nm.times {\n votes << gets.split.map(&:to_i)\n}\n\nvt = Array.new(n) { 0 }\nmax_votes = 0\nvotes.each { |row|\n max, maxs = 0, []\n row.each_with_index { |i, index|\n if i < max\n #pass\n elsif i == max\n maxs << index\n else\n maxs.clear\n max = i\n maxs << index\n end\n }\n vt[maxs.first] += 1\n if max_votes < vt[maxs.first]\n max_votes = vt[maxs.first]\n end\n}\nfor index in 0...n\n if max_votes == vt[index]\n puts (index + 1) # 1-based index\n break\n end\nend"}, {"source_code": "n,m=gets.split.map &:to_i;c=[0]*n;m.times{l=gets.split.map &:to_i;c[l.index(l.max)]+=1};p c.index(c.max)+1\n"}, {"source_code": "n, m = gets.split(' ').map(&:to_i)\nvotes_in_cities = Array.new(n, 0)\nm.times do\n tmp = gets.split(' ').map(&:to_i)\n vinner_in_city = tmp.index(tmp.max)\n votes_in_cities[vinner_in_city] += 1\nend\n\nputs votes_in_cities.index(votes_in_cities.max) + 1\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\na = m.times.map{gets.split.map(&:to_i)}\nb = a.map{|x| x.index(x.max)}.sort\nputs b.group_by{|x| x}.to_a.max_by{|x| x[1].length}[0] + 1\n"}, {"source_code": "n,m=gets.split.map &:to_i;c=[0]*n;m.times{l=gets.split.map &:to_i;c[l.index(l.max)]+=1};p c.index(c.max)+1"}, {"source_code": "n, m = gets.split.map{|x| x.to_i}\nc = [0]*n\nm.times do\n a = gets.split.map{|x| x.to_i}\n c[a.find_index(a.max)] += 1\nend\nputs(c.find_index(c.max) + 1)\n"}, {"source_code": "n,m=gets.split.map &:to_i;c=[0]*n;m.times{l=gets.split.map &:to_i;c[l.index(l.max)]+=1};p c.index(c.max)+1\n"}, {"source_code": "n,m=gets.split.map &:to_i;c=[0]*n;m.times{l=gets.split.map &:to_i;c[l.index(l.max)]+=1};p c.index(c.max)+1\n"}, {"source_code": "n,m=gets.split.map &:to_i;c=[0]*n;m.times{l=gets.split.map &:to_i;c[l.index(l.max)]+=1};p c.index(c.max)+1\n"}, {"source_code": "n, m = gets.chomp.split(' ').map { |i| i.to_i }\nselected = []\ncount = Array.new(if n > m then n else m end) { |i| i = 0 }\n\nm.times do\n\tcandidates = gets.chomp.split(' ').map { |i| i.to_i }\n\t# find Max, put in array\n\tselected << candidates.index(candidates.max)\nend\n\nselected.each do |v|\n\tcount[v] = selected.count(v)\nend\n\nputs count.index(count.max) + 1"}], "negative_code": [{"source_code": "\nn,m = gets.chomp.split.map{|x| x.to_i}\n\ncandidate_wins = Array.new(n+1){-1}\n#cities\n(0...m).each do |i|\n\tarr = gets.chomp.split.map{|x| x.to_i}\n\tcurr_winner = -1\n\tcurr_max_votes = -1\n\t(0...n).each do |j|\n\t\tif arr[j] > curr_max_votes\n\t\t\tcurr_max_votes = arr[j]\n\t\t\tcurr_winner = j\n\t\tend\n\tend\n\tcandidate_wins[curr_winner+1]+=1\nend\noverall_winner = -1\nover_all_win = 0\n(1..n).each do |i|\n\tif candidate_wins[i] > over_all_win\n\t\toverall_winner = i\n\t\tover_all_win = candidate_wins[i]\n\tend\nend\n\nputs overall_winner\n"}, {"source_code": "def read_ints; gets.chomp.split.map(&:to_i) end\nn,m = read_ints\ncity_winners = []\nm.times do \n\tcity_winners<= 1 && number.to_i <= 1000)\n\tall_logins_passed = true\n\n\tnumber.to_i.times do\n\t\tif all_logins_passed\n\t\t\tlogins << gets.chomp\n\t\tend\n\tend\n\n\tlogins.each do |curent_login|\n\t\tall_logins_passed = false if curent_login.length > 50\n\tend\n\n\tif all_logins_passed\n\t\tuser_can_register = true\n\n\t\tlogins.each do |existed_login|\n\t\t\tif simplify(existed_login).eql? simplify(wanted_login)\n\t\t\t\tuser_can_register = false\n\t\t\tend\n\t\tend\n\n\t\tputs 'Yes' if user_can_register\n\t\tputs 'No' unless user_can_register\n\tend\nend\n"}, {"source_code": "def get_login\n s = gets\n s.gsub(/0{1}/, 'o').gsub(/[lLIi]{1}/, '1').downcase\nend\n\nnew_login = get_login\nn = gets.to_i\nn.times do\n if new_login == get_login\n puts 'No'\n exit\n end\nend\n\nputs 'Yes'"}, {"source_code": "\ndef modify(s)\n s.downcase!\n s.gsub!(\"1\", \"l\")\n s.gsub!(\"i\", \"l\")\n s.gsub!(\"0\", \"o\")\nend\n\ns = gets.chomp\nmodify(s)\n\nlogins = Array.new\nn = gets.chomp.to_i\nfor i in 1..n\n logins.push(gets.chomp)\n modify(logins.last)\nend\n\nfor login in logins\n if login == s\n puts \"No\"\n exit\n end\nend\n\nputs \"Yes\"\n\n"}], "negative_code": [{"source_code": "\ndef modify(s)\n s.gsub!(\"1\", \"l\")\n s.gsub!(\"I\", \"l\")\n s.gsub!(\"0\", \"o\")\n s.downcase!\nend\n\ns = gets.chomp\nmodify(s)\n\nlogins = Array.new\nn = gets.chomp.to_i\nfor i in 1..n\n logins.push(gets.chomp)\n modify(logins.last)\nend\n\nfor login in logins\n if login == s\n puts \"No\"\n exit\n end\nend\n\nputs \"Yes\"\n\n"}, {"source_code": "\ndef modify(s)\n s.gsub!(\"1\", \"l\")\n s.gsub!(\"i\", \"l\")\n s.gsub!(\"0\", \"o\")\n s.downcase!\nend\n\ns = gets.chomp\nmodify(s)\n\nlogins = Array.new\nn = gets.chomp.to_i\nfor i in 1..n\n logins.push(gets.chomp)\n modify(logins.last)\nend\n\nfor login in logins\n if login == s\n puts \"No\"\n exit\n end\nend\n\nputs \"Yes\"\n\n"}, {"source_code": "def simplify(string)\n\tstr_arr = string.split(\"\")\n\tstr_copy = str_arr\n\tstr_arr.each_with_index do |char, index|\n\t\tcase char\n\t\twhen 'O', 'o'\t#big ou\n\t\t\tstr_copy[index] = 0\n\t\twhen 'l', 'I', 'L', 'i'\n\t\t\tstr_copy[index] = 1\n\t\telse\n\t\t\tstr_copy[index] = char\n\t\tend\n\tend\n\tstr_copy.join('').downcase\nend\n\n\nwanted_login = gets.chomp\nnumber = gets.chomp\nlogins = []\n\nif wanted_login.length <= 50 && (number.to_i > 1 && number.to_i < 1000)\n\tall_logins_passed = true\n\n\tnumber.to_i.times do\n\t\tif all_logins_passed\n\t\t\tlogins << gets.chomp\n\t\tend\n\tend\n\n\tlogins.each do |curent_login|\n\t\tall_logins_passed = false if curent_login.length > 50\n\tend\n\n\tif all_logins_passed\n\t\tuser_can_register = true\n\n\t\tlogins.each do |existed_login|\n\t\t\tif simplify(existed_login).eql? simplify(wanted_login)\n\t\t\t\tuser_can_register = false\n\t\t\tend\n\t\tend\n\n\t\tputs 'Yes' if user_can_register\n\t\tputs 'No' unless user_can_register\n\tend\nend\n"}, {"source_code": "\ndef modify(s)\n s.downcase!\n s.gsub!(\"1\", \"l\")\n s.gsub!(\"I\", \"l\")\n s.gsub!(\"0\", \"o\")\nend\n\ns = gets.chomp\nmodify(s)\n\nlogins = Array.new\nn = gets.chomp.to_i\nfor i in 1..n\n logins.push(gets.chomp)\n modify(logins.last)\nend\n\nfor login in logins\n if login == s\n puts \"No\"\n exit\n end\nend\n\nputs \"Yes\"\n\n"}, {"source_code": "\ndef modify(s)\n s.downcase!\n s.gsub!(\"1\", \"l\")\n s.gsub!(\"0\", \"o\")\nend\n\ns = gets.chomp\nmodify(s)\n\nlogins = Array.new\nn = gets.chomp.to_i\nfor i in 1..n\n logins.push(gets.chomp)\n modify(logins.last)\nend\n\nfor login in logins\n if login == s\n puts \"No\"\n exit\n end\nend\n\nputs \"Yes\"\n\n"}], "src_uid": "0d70984ab8bd7aecd68c653adf54fc08"} {"nl": {"description": "You are given a string $$$s$$$ consisting of exactly $$$n$$$ characters, and each character is either '0', '1' or '2'. Such strings are called ternary strings.Your task is to replace minimum number of characters in this string with other characters to obtain a balanced ternary string (balanced ternary string is a ternary string such that the number of characters '0' in this string is equal to the number of characters '1', and the number of characters '1' (and '0' obviously) is equal to the number of characters '2').Among all possible balanced ternary strings you have to obtain the lexicographically (alphabetically) smallest.Note that you can neither remove characters from the string nor add characters to the string. Also note that you can replace the given characters only with characters '0', '1' and '2'.It is guaranteed that the answer exists.", "input_spec": "The first line of the input contains one integer $$$n$$$ ($$$3 \\le n \\le 3 \\cdot 10^5$$$, $$$n$$$ is divisible by $$$3$$$) — the number of characters in $$$s$$$. The second line contains the string $$$s$$$ consisting of exactly $$$n$$$ characters '0', '1' and '2'.", "output_spec": "Print one string — the lexicographically (alphabetically) smallest balanced ternary string which can be obtained from the given one with minimum number of replacements. Because $$$n$$$ is divisible by $$$3$$$ it is obvious that the answer exists. And it is obvious that there is only one possible answer.", "sample_inputs": ["3\n121", "6\n000000", "6\n211200", "6\n120110"], "sample_outputs": ["021", "001122", "211200", "120120"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\nm = n / 3\na = gets.chomp.chars.map(&:to_i)\ncnt = (0...3).map { a.count(_1) }\n\n2.times do\n\ta.each_with_index do |from, i|\n\t\t(0...from).each do |to|\n\t\t\tif cnt[from] > m && cnt[to] < m\n\t\t\t\ta[i] = to\n\t\t\t\tcnt[from] -= 1\n\t\t\t\tcnt[to] += 1\n\t\t\t\tbreak\n\t\t\tend\n\t\tend\n\tend\n\ta = a.reverse.map { 2 - _1 }\n\tcnt.reverse!\nend\nputs a.join\n"}, {"source_code": "n = gets.to_i\ns = gets.chomp.split(\"\").map(&:to_i)\na = [0,0,0]\nn.times do |i|\n a[s[i]] += 1\nend\nif a[0] < n/3\n n.times do |i|\n next if s[i] == 0 || a[s[i]] <= n/3\n a[0] += 1\n a[s[i]] -= 1\n s[i] = 0\n break if a[0] == n/3\n end\nend\nif a[1] < n/3\n n.times do |i|\n next if s[i] < 2 || a[s[i]] <= n/3\n a[1] += 1\n a[2] -= 1\n s[i] = 1\n break if a[1] == n/3\n end\nend\nif a[2] < n/3\n n.times do |i|\n next if s[-1-i] == 2 || a[s[-1-i]] <= n/3\n a[2] += 1\n a[s[-1-i]] -= 1\n s[-1-i] = 2\n break if a[2] == n/3\n end\nend\nif a[1] < n/3\n n.times do |i|\n next if s[-1-i] > 0\n a[1] += 1\n a[0] -= 1\n s[-1-i] = 1\n break if a[1] == n/3\n end\nend\nputs s.join(\"\")"}], "negative_code": [{"source_code": "n = gets.to_i\ns = gets.chomp.split(\"\").map(&:to_i)\na = [0,0,0]\nn.times do |i|\n a[s[i]] += 1\nend\nif a[0] < n/3\n n.times do |i|\n next if s[i] == 0 || a[s[i]] <= n/3\n a[0] += 1\n a[s[i]] -= 1\n s[i] = 0\n break if a[0] == n/3\n end\nend\nif a[1] < n/3\n n.times do |i|\n next if s[i] < 2\n a[1] += 1\n a[2] -= 1\n s[i] = 1\n break if a[1] == n/3\n end\nend\nif a[2] < n/3\n n.times do |i|\n next if s[-1-i] == 2 || a[s[-1-i]] <= n/3\n a[2] += 1\n a[s[-1-i]] -= 1\n s[-1-i] = 2\n break if a[2] == n/3\n end\nend\nif a[1] < n/3\n n.times do |i|\n next if s[-1-i] > 0\n a[1] += 1\n a[0] -= 1\n s[-1-i] = 1\n break if a[1] == n/3\n end\nend\nputs s.join(\"\")"}], "src_uid": "cb852bf0b62d72b3088969ede314f176"} {"nl": {"description": "At the store, the salespeople want to make all prices round. In this problem, a number that is a power of $$$10$$$ is called a round number. For example, the numbers $$$10^0 = 1$$$, $$$10^1 = 10$$$, $$$10^2 = 100$$$ are round numbers, but $$$20$$$, $$$110$$$ and $$$256$$$ are not round numbers. So, if an item is worth $$$m$$$ bourles (the value of the item is not greater than $$$10^9$$$), the sellers want to change its value to the nearest round number that is not greater than $$$m$$$. They ask you: by how many bourles should you decrease the value of the item to make it worth exactly $$$10^k$$$ bourles, where the value of $$$k$$$ — is the maximum possible ($$$k$$$ — any non-negative integer).For example, let the item have a value of $$$178$$$-bourles. Then the new price of the item will be $$$100$$$, and the answer will be $$$178-100=78$$$.", "input_spec": "The first line of input data contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases . Each test case is a string containing a single integer $$$m$$$ ($$$1 \\le m \\le 10^9$$$) — the price of the item.", "output_spec": "For each test case, output on a separate line a single integer $$$d$$$ ($$$0 \\le d < m$$$) such that if you reduce the cost of the item by $$$d$$$ bourles, the cost of the item will be the maximal possible round number. More formally: $$$m - d = 10^k$$$, where $$$k$$$ — the maximum possible non-negative integer.", "sample_inputs": ["7\n\n1\n\n2\n\n178\n\n20\n\n999999999\n\n9000\n\n987654321"], "sample_outputs": ["0\n1\n78\n10\n899999999\n8000\n887654321"], "notes": "NoteIn the example: $$$1 - 0 = 10^0$$$, $$$2 - 1 = 10^0$$$, $$$178 - 78 = 10^2$$$, $$$20 - 10 = 10^1$$$, $$$999999999 - 899999999 = 10^8$$$, $$$9000 - 8000 = 10^3$$$, $$$987654321 - 887654321 = 10^8$$$. Note that in each test case, we get the maximum possible round number."}, "positive_code": [{"source_code": "gets.to_i.times { m = gets.strip; puts m.to_i - 10 ** (m.size - 1)}\r\n"}, {"source_code": "n = gets.to_i\n\nn.times do\n m = gets.chomp\n\n p (m.to_i - (10 ** (m.size - 1)))\nend\n"}, {"source_code": "def round_down_price(num)\n len = num.digits.count\n round = 10**(len-1)\n extra = num - round\n extra\nend\n\nt = gets.chomp.to_i\ninput = []\n(0..t-1).each do |i|\n input.push(gets.chomp.to_i)\nend\n\ninput.each do |i|\n p round_down_price(i)\nend\n\n"}, {"source_code": "t = gets.to_i\nt.times {\n n = gets.chomp\n if n == 0\n puts \"0\"\n else\n n[0] = (n[0].ord - 1).chr\n puts n.to_i\n end\n}\n"}], "negative_code": [{"source_code": "gets.to_i.times { m = gets; puts m.to_i - 10 ** (m.size - 1)}"}], "src_uid": "5312a505bd59b55a6c5487f67a33d81a"} {"nl": {"description": "One company of IT City decided to create a group of innovative developments consisting from 5 to 7 people and hire new employees for it. After placing an advertisment the company received n resumes. Now the HR department has to evaluate each possible group composition and select one of them. Your task is to count the number of variants of group composition to evaluate.", "input_spec": "The only line of the input contains one integer n (7 ≤ n ≤ 777) — the number of potential employees that sent resumes.", "output_spec": "Output one integer — the number of different variants of group composition.", "sample_inputs": ["7"], "sample_outputs": ["29"], "notes": null}, "positive_code": [{"source_code": "def comb(n,k)\n\tr=1\n\tk.times{|i|\n\t\tr=r*(n-i)/(i+1)\n\t}\n\tr\nend\nn=gets.to_i\np comb(n,5)+comb(n,6)+comb(n,7)"}, {"source_code": "n=gets.to_i\nr=t=(n-4..n).inject(:*)/120\nt=t*(n-5)/6\nr+=t\nt=t*(n-6)/7\np r+t"}, {"source_code": "n=gets.to_i\n\na=n*(n-1)*(n-2)*(n-3)*(n-4)\nb=a*(n-5)\nc=b*(n-6)\n\nprint(a/(2*3*4*5)+b/(2*3*4*5*6)+c/(2*3*4*5*6*7))\n"}], "negative_code": [{"source_code": "p gets.to_i*4+1"}, {"source_code": "n=gets.to_i\nr=(n-4..n).inject(:*)/120\nr*=(n-5)*(n-6)/42\np r"}, {"source_code": "n=gets.to_i\nr=t=(n-4..n).inject(:*)/120\nr+=t*=(n-5)/6\nr+=t*=(n-6)/7\np r"}], "src_uid": "09276406e16b46fbefd6f8c9650472f0"} {"nl": {"description": "Valera has 2·n cubes, each cube contains an integer from 10 to 99. He arbitrarily chooses n cubes and puts them in the first heap. The remaining cubes form the second heap. Valera decided to play with cubes. During the game he takes a cube from the first heap and writes down the number it has. Then he takes a cube from the second heap and write out its two digits near two digits he had written (to the right of them). In the end he obtained a single fourdigit integer — the first two digits of it is written on the cube from the first heap, and the second two digits of it is written on the second cube from the second heap.Valera knows arithmetic very well. So, he can easily count the number of distinct fourdigit numbers he can get in the game. The other question is: how to split cubes into two heaps so that this number (the number of distinct fourdigit integers Valera can get) will be as large as possible?", "input_spec": "The first line contains integer n (1 ≤ n ≤ 100). The second line contains 2·n space-separated integers ai (10 ≤ ai ≤ 99), denoting the numbers on the cubes.", "output_spec": "In the first line print a single number — the maximum possible number of distinct four-digit numbers Valera can obtain. In the second line print 2·n numbers bi (1 ≤ bi ≤ 2). The numbers mean: the i-th cube belongs to the bi-th heap in your division. If there are multiple optimal ways to split the cubes into the heaps, print any of them.", "sample_inputs": ["1\n10 99", "2\n13 24 13 45"], "sample_outputs": ["1\n2 1", "4\n1 2 2 1"], "notes": "NoteIn the first test case Valera can put the first cube in the first heap, and second cube — in second heap. In this case he obtain number 1099. If he put the second cube in the first heap, and the first cube in the second heap, then he can obtain number 9910. In both cases the maximum number of distinct integers is equal to one.In the second test case Valera can obtain numbers 1313, 1345, 2413, 2445. Note, that if he put the first and the third cubes in the first heap, he can obtain only two numbers 1324 and 1345."}, "positive_code": [{"source_code": "n = gets.to_i\na = gets.chomp.split.map(&:to_i)\nh = Hash.new\n\na.each_with_index do |e, i|\n (h[e] ||= []) << i\nend\n\ntotal = h.values.map(&:length).map { |e| [e, 2].min }.inject(:+)\nhalf = total/2\nputs half * (total-half)\n\nb = Array.new(2*n)\ncnt = 0\n\nh.each do |k, v|\n if v.length >= 2\n b[v[0]] = 1\n b[v[1]] = 2\n cnt += 1\n end\nend\n\nh.each do |k, v|\n if v.length == 1\n if cnt < half\n b[v[0]] = 1\n cnt += 1\n else\n b[v[0]] = 2\n end\n end\nend\n\na.each_with_index do |e, i|\n if b[i] == nil\n if cnt < n\n b[i] = 1\n cnt += 1\n else\n b[i] = 2\n end\n end\nend\n\nb.each do |e|\n print e , ' '\nend\nputs\n\n"}, {"source_code": "n = gets.to_i\na = gets.chomp.split.map(&:to_i)\nc = Array.new(101) { [] }\n\na.each_with_index do |e, i|\n c[e] << i\nend\n\ntotal = c.map(&:length).map { |e| [e, 2].min }.inject(:+)\nhalf = total/2\nputs half * (total-half)\n\nb = Array.new(2*n)\ncnt = 0\n\nc.each do |e|\n if e.length >= 2\n b[e[0]] = 1\n b[e[1]] = 2\n cnt += 1\n end\nend\n\nc.each do |e|\n if e.length == 1\n if cnt < half\n b[e[0]] = 1\n cnt += 1\n else\n b[e[0]] = 2\n end\n end\nend\n\na.each_with_index do |e, i|\n if b[i] == nil\n if cnt < n\n b[i] = 1\n cnt += 1\n else\n b[i] = 2\n end\n end\nend\n\nb.each do |e|\n print e , ' '\nend\nputs\n\n"}, {"source_code": "n = gets.to_i\na = gets.chomp.split.map(&:to_i)\nh = Hash.new\n\na.each_with_index do |e, i|\n (h[e] ||= []) << i\nend\n\ntotal = h.values.map(&:length).map { |e| [e, 2].min }.inject(:+)\nhalf = total/2\nputs half * (total-half)\n\nb = Array.new(2*n)\ncnt = 0\n\nh.each do |k, v|\n if v.length >= 2\n\tb[v[0]] = 1\n\tb[v[1]] = 2\n\tcnt += 1\n end\nend\n\nh.each do |k, v|\n if v.length == 1\n\tif cnt < half\n\t b[v[0]] = 1\n\t cnt += 1\n\telse\n\t b[v[0]] = 2\n\tend\n end\nend\n\na.each_with_index do |e, i|\n if b[i] == nil\n\tif cnt < n\n\t b[i] = 1\n\t cnt += 1\n\telse\n\t b[i] = 2\n\tend\n end\nend\n\nb.each do |e|\n print e , ' '\nend\nputs\n\n"}, {"source_code": "#!/usr/bin/ruby\n\nn = $stdin.readline.to_i\ncubes = $stdin.readline.split.map(&:to_i)\n\ncubes_count = Hash.new\n\ncubes.each{|x| cubes_count[x] = (cubes_count[x] == nil ? 1 : cubes_count[x] + 1 )}\n\na1 = []\na2 = []\n\ncubes_count.each_pair {|key, value|\n if value == 1\n if a1.length < a2.length\n a1.push key\n else\n a2.push key\n end\n else\n a1.push key\n a2.push key\n end\n}\n\na1.sort!\na2.sort!\n\nb1 = a1.length\nb2 = a2.length\n\ncubes_count.each_pair {|key, value|\n v = value - 2\n while v > 1\n a1.push key\n a2.push key\n v -= 2\n end\n if v == 1\n if a1.length < a2.length\n a1.push key\n else\n a2.push key\n end\n end\n}\n\n# puts \"#{cubes_count} #{a1.length} #{a2.length}\"\n# puts \"#{a1}\"\n# puts \"#{a2}\"\n\nans = []\n\ncubes.each_index { |i|\n if a1.include? cubes[i]\n ans[i] = 1\n a1.delete_at(a1.index(cubes[i]))\n else\n ans[i] = 2\n a2.delete_at(a2.index(cubes[i]))\n end\n}\n\n# puts ans.inject{|sum,x| sum + x }\n\nputs \"#{b1*b2}\"\nputs ans.join(' ')\n"}, {"source_code": "n = gets.to_i\ngiven = gets.split.map{|s| s.to_i}\nnumbers = Array::new(100, 0)\ngiven.each { |cg| numbers[cg] += 1 }\nflag0 = 0\nflag1 = 0\nsum = 0\nnumbers.map! {|n|\n\tif n > 0\n\t\tif n == 1\n\t\t\tsum += 1\n\t\t\tflag0 = (flag0+1)%2\n\t\t\tn = flag0\n\t\telsif n%2 == 1\n\t\t\tsum += 2\n\t\t\tflag1 = (flag1+1)%2\n\t\t\tn += flag1\n\t\telse\n\t\t\tsum += 2\n\t\t\tn\n\t\tend\n\tend\n}\nif sum%2 == 0\n\tputs sum*sum/4\nelse\n\tputs sum/2*(sum/2 + 1)\nend\ngiven.each {|cg| \n\tprint (numbers[cg]%2 + 1).to_s + \" \"\n\tnumbers[cg] -= 1\n}"}, {"source_code": "n = gets.to_i\na = gets.chomp.split.map(&:to_i)\nc = Array.new(101) { [] }\n\na.each_with_index do |e, i|\n c[e] << i\nend\n\ntotal = c.map(&:length).map { |e| [e, 2].min }.inject(:+)\nhalf = total/2\nputs half * (total-half)\n\nb = Array.new(2*n)\ncnt = 0\n\nc.each do |e|\n if e.length >= 2\n b[e[0]] = 1\n b[e[1]] = 2\n cnt += 1\n end\nend\n\nc.each do |e|\n if e.length == 1\n if cnt < half\n b[e[0]] = 1\n cnt += 1\n else\n b[e[0]] = 2\n end\n end\nend\n\na.each_with_index do |e, i|\n if b[i] == nil\n if cnt < n\n b[i] = 1\n cnt += 1\n else\n b[i] = 2\n end\n end\nend\n\nb.each do |e|\n print e , ' '\nend\nputs"}], "negative_code": [{"source_code": "n = gets.to_i\na = gets.chomp.split.map(&:to_i)\nh = Hash.new { |h, k| h[k] = [] }\n\na.each_with_index do |e, i|\n h[e] << i\nend\n\ntmp = h.values.map(&:length).map { |e| [e, 2].min }.inject(:+)\nputs tmp/2*(tmp-tmp/2)\n\nb = Array.new(2*n)\ncnt = 0\na.each_with_index do |e, i|\n if b[i] == nil\n\tb[i] = 1\n\th[e].shift\n\tif !h[e].empty?\n\t b[h[e].pop] = 2\n\tend\n\tcnt += 1\n\tif cnt == n\n\t break\n\tend\n end\nend\n\nb.each do |e|\n print e ||= 2, ' '\nend\nputs\n\n"}, {"source_code": "n = gets.to_i\na = gets.chomp.split.map(&:to_i)\nh = Hash.new\n\na.each_with_index do |e, i|\n (h[e] ||= []) << i\nend\n\ntotal = h.values.map(&:length).map { |e| [e, 2].min }.inject(:+)\nhalf = total/2\nputs half * (total-half)\n\nb = Array.new(2*n)\ncnt = 0\nh.each do |k, v|\n if v.length >= 2\n\tb[v[0]] = 1\n\tb[v[1]] = 2\n\tcnt += 1\n end\nend\n\na.each_with_index do |e, i|\n if b[i] != nil\n\tnext\n else\n\tif cnt < n\n\t b[i] = 1\n\t cnt += 1\n\telse\n\t b[i] = 2\n\tend\n end\nend\n\nb.each do |e|\n print e , ' '\nend\nputs\n\n"}, {"source_code": "n = gets.to_i\na = gets.chomp.split.map(&:to_i)\nh = Hash.new\n\na.each_with_index do |e, i|\n (h[e] ||= []) << i\nend\n\ntotal = h.values.map(&:length).map { |e| [e, 2].min }.inject(:+)\nhalf = total/2\nputs half * (total-half)\n\nb = Array.new(2*n)\ncnt = 0\nh.each do |k, v|\n if v.length >= 2\n\tb[v[0]] = 1\n\tb[v[1]] = 2\n\tcnt += 1\n end\nend\n\na.each_with_index do |e, i|\n if b[i] != nil\n\tnext\n else\n\tif cnt < half \n\t b[i] = 1\n\t cnt += 1\n\telse\n\t b[i] = 2\n\tend\n end\nend\n\nb.each do |e|\n print e ||= 2, ' '\nend\nputs\n\n"}, {"source_code": "#!/usr/bin/ruby\n\nn = $stdin.readline.to_i\ncubes = $stdin.readline.split.map(&:to_i)\n\ncubes_count = Hash.new\n\ncubes.each{|x| cubes_count[x] = (cubes_count[x] == nil ? 1 : cubes_count[x] + 1 )}\n\na1 = []\na2 = []\n\ncubes_count.each_pair {|key, value|\n v = value\n while v > 1\n a1.push key\n a2.push key\n v -= 2\n end\n if v == 1\n if a1.length < a2.length\n a1.push key\n else\n a2.push key\n end\n end\n}\n\na1.sort!\na2.sort!\n\nb1 = 1\nb2 = 1\n\na1.each_index {|index|\n if index > 0\n if a1[index - 1] != a1[index]\n b1 += 1\n end\n end\n}\n\na2.each_index {|index|\n if index > 0\n if a2[index - 1] != a2[index]\n b2 += 1\n end\n end\n}\n\nans = []\n\ncubes.each_index { |i|\n if a1.include? cubes[i]\n ans[i] = 1\n a1.delete_at(a1.index(cubes[i]) || a1.length)\n else\n ans[i] = 2\n a2.delete_at(a2.index(cubes[i]) || a2.length)\n end\n}\n\nputs \"#{cubes_count}\"\nputs \"#{a1}\"\nputs \"#{a2}\"\nputs \"#{b1*b2}\"\nputs ans.join(' ')\n"}, {"source_code": "#!/usr/bin/ruby\n\nn = $stdin.readline.to_i\ncubes = $stdin.readline.split.map(&:to_i)\n\ncubes_count = Hash.new\n\ncubes.each{|x| cubes_count[x] = (cubes_count[x] == nil ? 1 : cubes_count[x] + 1 )}\n\na1 = []\na2 = []\n\ncubes_count.each_pair {|key, value|\n v = value\n while v > 1\n a1.push key\n a2.push key\n v -= 2\n end\n if value == 1\n if a1.length < a2.length\n a1.push key\n else\n a2.push key\n end\n end\n}\n\na1.sort!\na2.sort!\n\nb1 = 1\nb2 = 1\n\na1.each_index {|index|\n if index > 0\n if a1[index - 1] != a1[index]\n b1 += 1\n end\n end\n}\n\na2.each_index {|index|\n if index > 0\n if a2[index - 1] != a2[index]\n b2 += 1\n end\n end\n}\n\nans = []\n\ncubes.each_index { |i|\n if a1.include? cubes[i]\n ans[i] = 1\n a1.delete_at(a1.index(cubes[i]) || a1.length)\n else\n ans[i] = 2\n a2.delete_at(a2.index(cubes[i]) || a2.length)\n end\n}\n\n# puts \"#{cubes_count}\"\n# puts \"#{a1}\"\n# puts \"#{a2}\"\nputs \"#{b1*b2}\"\nputs ans.join(' ')\n"}, {"source_code": "#!/usr/bin/ruby\n\nn = $stdin.readline.to_i\ncubes = $stdin.readline.split.map(&:to_i)\n\ncubes_count = Hash.new\n\ncubes.each{|x| cubes_count[x] = (cubes_count[x] == nil ? 1 : cubes_count[x] + 1 )}\n\na1 = []\na2 = []\n\ncubes_count.each_pair {|key, value|\n v = value\n while v > 1\n a1.push key\n a2.push key\n v -= 2\n end\n if v == 1\n if a1.length < a2.length\n a1.push key\n else\n a2.push key\n end\n end\n}\n\na1.sort!\na2.sort!\n\nb1 = 1\nb2 = 1\n\na1.each_index {|index|\n if index > 0\n if a1[index - 1] != a1[index]\n b1 += 1\n end\n end\n}\n\na2.each_index {|index|\n if index > 0\n if a2[index - 1] != a2[index]\n b2 += 1\n end\n end\n}\n\nans = []\n\ncubes.each_index { |i|\n if a1.include? cubes[i]\n ans[i] = 1\n a1.delete_at(a1.index(cubes[i]) || a1.length)\n else\n ans[i] = 2\n a2.delete_at(a2.index(cubes[i]) || a2.length)\n end\n}\n\n# puts \"#{cubes_count}\"\n# puts \"#{a1}\"\n# puts \"#{a2}\"\nputs \"#{b1*b2}\"\nputs ans.join(' ')\n"}], "src_uid": "080287f34b0c1d52eb29eb81dada41dd"} {"nl": {"description": "Vasya has n items lying in a line. The items are consecutively numbered by numbers from 1 to n in such a way that the leftmost item has number 1, the rightmost item has number n. Each item has a weight, the i-th item weights wi kilograms.Vasya needs to collect all these items, however he won't do it by himself. He uses his brand new robot. The robot has two different arms — the left one and the right one. The robot can consecutively perform the following actions: Take the leftmost item with the left hand and spend wi · l energy units (wi is a weight of the leftmost item, l is some parameter). If the previous action was the same (left-hand), then the robot spends extra Ql energy units; Take the rightmost item with the right hand and spend wj · r energy units (wj is a weight of the rightmost item, r is some parameter). If the previous action was the same (right-hand), then the robot spends extra Qr energy units; Naturally, Vasya wants to program the robot in a way that the robot spends as little energy as possible. He asked you to solve this problem. Your task is to find the minimum number of energy units robot spends to collect all items.", "input_spec": "The first line contains five integers n, l, r, Ql, Qr (1 ≤ n ≤ 105; 1 ≤ l, r ≤ 100; 1 ≤ Ql, Qr ≤ 104). The second line contains n integers w1, w2, ..., wn (1 ≤ wi ≤ 100).", "output_spec": "In the single line print a single number — the answer to the problem.", "sample_inputs": ["3 4 4 19 1\n42 3 99", "4 7 2 3 9\n1 2 3 4"], "sample_outputs": ["576", "34"], "notes": "NoteConsider the first sample. As l = r, we can take an item in turns: first from the left side, then from the right one and last item from the left. In total the robot spends 4·42 + 4·99 + 4·3 = 576 energy units.The second sample. The optimal solution is to take one item from the right, then one item from the left and two items from the right. In total the robot spends (2·4) + (7·1) + (2·3) + (2·2 + 9) = 34 energy units."}, "positive_code": [{"source_code": "#f = File.open('test1.txt', 'r')\nf = STDIN\nn, l, r, Ql, Qr = f.gets.chomp.split.map &:to_i\nw = f.gets.chomp.split.map &:to_i\nsum = Array.new(w.size)\nsum[0] = w[0]\n(1...n).each do |i|\n sum[i] = sum[i-1] + w[i]\nend\n\ntotal = -1\n(0..n).each do |k|\n tmp_total = 0\n tmp_sum = k == 0 ? 0 : sum[k-1]\n tmp_total += tmp_sum * l + (sum[n-1] - tmp_sum) * r\n if k > n - k\n tmp_total += (2 * k - n - 1) * Ql\n end\n if k < n - k\n tmp_total += (n - 2 * k - 1) * Qr\n end\n if total == -1\n total = tmp_total\n else\n total = [total, tmp_total].min\n end\nend\nputs total\n"}, {"source_code": "n, l, r, ql, qr = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\n\nans = 2**30\nsl = 0\nsr = a.inject(:+)\n\nfor i in (0..n)\n trial = 0\n if 2*i < n\n trial += (n-2*i-1)*qr\n elsif 2*i > n\n trial += (2*i-n-1)*ql\n end\n trial += sl*l + sr*r\n ans = [ans, trial].min\n if i < n\n sl += a[i]\n sr -= a[i]\n end\nend\n\nputs ans\n\n"}, {"source_code": "n, l, r, ql, qr = gets.split.map(&:to_i)\nw = gets.split.map(&:to_i)\n\nleft = Array.new(n+2, 0)\nright = Array.new(n+2, 0)\n\nfor i in 0...n\n left[i+1] = left[i]+w[i]\n right[n-i] = right[n-i+1]+w[n-i-1]\nend\n\nmin = 2**30\nfor i in 0..n\n energy = l*left[i]+r*right[i+1]\n same = n - 2*i\n if same > 1\n energy += (same-1)*qr\n elsif same < -1\n energy -= (same+1)*ql\n end\n if energy < min\n min = energy\n end\nend\n\nprint min"}], "negative_code": [{"source_code": "n, l, r, ql, qr = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\n\nans = 1e9\nsl = 0\nsr = a.inject(:+)\n\nfor i in (0..n)\n trial = 0\n if 2*i < n\n trial += (n-2*i-1)*qr\n elsif 2*i > n\n trial += (2*i-n-1)*ql\n end\n trial += sl*l + sr*r\n ans = [ans, trial].min\n if i < n\n sl += a[i]\n sr -= a[i]\n end\nend\n\nputs ans\n\n"}, {"source_code": "line = gets.split(' ').map(&:to_i)\nn = line[0]\nl = line[1]\nr = line[2]\nql = line[3]\nqr = line[4]\na = gets.split(' ').map(&:to_i)\nif l < r\n l, r = r, l\n ql, qr = qr, ql\nend\n\na.sort!\nans = 2**32\nsl = 0\nsr = a.inject(:+)\nfor i in (0...a.size)\n trial = 0\n if 2*i > a.size\n break\n elsif 2*i < a.size\n trial += (a.size-2*i-1)*qr\n end\n trial += sl*l+sr*r\n ans = [ans, trial].min\n sl += a[i]\n sr -= a[i]\nend\n\nputs ans\n"}, {"source_code": "n, l, r, ql, qr = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\n\nif l < r\n l, r = r, l\n ql, qr = qr, ql\nend\na.sort!\nans = 2**32\nsl = 0\nsr = a.inject(:+)\n\nfor i in (0...a.size)\n trial = 0\n if 2*i > a.size\n break\n elsif 2*i < a.size\n trial += (a.size-2*i-1)*qr\n end\n trial += sl*l+sr*r\n ans = [ans, trial].min\n sl += a[i]\n sr -= a[i]\nend\n\nputs ans\n\n"}, {"source_code": "n, l, r, ql, qr = gets.split.map(&:to_i)\nw = gets.split.map(&:to_i)\n\nleft = Array.new(n+2, 0)\nright = Array.new(n+2, 0)\n\nfor i in 0...n\n left[i+1] = left[i]+w[i]\n right[n-i] = right[n-i+1]+w[n-i-1]\nend\n\nmin = 2**30\nfor i in 0..n\n energy = l*left[i]+r*right[i+1]\n same = n - 2*i\n if same > 0\n energy += same*qr\n else\n energy -= same*ql\n end\n if energy < min\n min = energy\n end\nend\n\nprint min"}], "src_uid": "e6fcbe3f102b694a686c0295edbc35e9"} {"nl": {"description": "You are given four distinct integers $$$a$$$, $$$b$$$, $$$c$$$, $$$d$$$. Timur and three other people are running a marathon. The value $$$a$$$ is the distance that Timur has run and $$$b$$$, $$$c$$$, $$$d$$$ correspond to the distances the other three participants ran. Output the number of participants in front of Timur.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$) — the number of test cases. The description of each test case consists of four distinct integers $$$a$$$, $$$b$$$, $$$c$$$, $$$d$$$ ($$$0 \\leq a, b, c, d \\leq 10^4$$$).", "output_spec": "For each test case, output a single integer — the number of participants in front of Timur.", "sample_inputs": ["4\n\n2 3 4 1\n\n10000 0 1 2\n\n500 600 400 300\n\n0 9999 10000 9998"], "sample_outputs": ["2\n0\n1\n3"], "notes": "NoteFor the first test case, there are $$$2$$$ people in front of Timur, specifically the participants who ran distances of $$$3$$$ and $$$4$$$. The other participant is not in front of Timur because he ran a shorter distance than Timur.For the second test case, no one is in front of Timur, since he ran a distance of $$$10000$$$ while all others ran a distance of $$$0$$$, $$$1$$$, and $$$2$$$ respectively.For the third test case, only the second person is in front of Timur, who ran a total distance of $$$600$$$ while Timur ran a distance of $$$500$$$."}, "positive_code": [{"source_code": " gets.to_i.times {\n \ta,*b = gets.split.map {|x| x.to_i }\n \tputs (b.count{|x| x > a})\n }"}, {"source_code": "t = gets.to_i\n\ninputs = []\n(0...t).each do\n arr = gets.chomp\n arr = arr.split(\" \").map(&:to_i)\n inputs << arr\n\nend\n\ninputs.each do |arr|\n ans = 0\n # array.each do |arr|\n a = arr[0]\n b = arr[1]\n c = arr[2]\n d = arr[3]\n\n if a < b\n ans += 1\n end\n if a < c\n ans += 1\n end\n if a < d\n ans += 1\n end\n # end\n puts ans\nend\n\n"}, {"source_code": "s = gets.to_i\n\nresult = []\ns.times do |i|\n a, b, c, d = gets.split(\" \").map(&:to_i)\n\n r = 0\n r += 1 if b > a\n r += 1 if c > a\n r += 1 if d > a\n\n result.push(r)\nend\n\nresult.each { |line| puts line }\n"}, {"source_code": "t = gets.to_i\r\nt.times do\r\n d=gets.split.map &:to_i\r\n puts d.count{|i|i>d[0]}\r\nend"}, {"source_code": "def solve\r\n v = gets.split.map(&:to_i)\r\n ans = 0\r\n if v[1] > v[0]\r\n ans = ans + 1\r\n end\r\n if v[2] > v[0]\r\n ans = ans + 1\r\n end\r\n if v[3] > v[0]\r\n ans = ans + 1\r\n end\r\n puts ans\r\nend\r\n \r\ngets.to_i.times do\r\n solve\r\nend"}, {"source_code": "gets.to_i.times do\r\n a, b, c, d = gets.split.map(&:to_i)\r\n ans = 0\r\n ans += 1 if a <= b\r\n ans += 1 if a <= c\r\n ans += 1 if a <= d\r\n puts ans\r\nend\r\n"}, {"source_code": "gets.to_i.times do\n a, b, c, d = gets.split.map(&:to_i)\n ans = 0\n ans += 1 if a < b\n ans += 1 if a < c\n ans += 1 if a < d\n puts ans\nend\n"}, {"source_code": "main = -> {\r\n t = int\r\n t.times do\r\n a, *x = ints\r\n \r\n puts x.count { _1 > a }\r\n end\r\n}\r\n\r\nDEBUG = false\r\nMOD = 10**9+7\r\nINF = 10**18\r\n$yesno = %w(No Yes)\r\n$YesNo = %w(No Yes)\r\n$YESNO = %w(NO YES)\r\n\r\nrequire \"set\"\r\nrequire \"prime\"\r\n\r\ndef int; gets.to_s.to_i end\r\ndef ints; gets.to_s.split.map { |s| s.to_i } end\r\ndef int1s; gets.to_s.split.map { |s| s.to_i - 1 } end\r\ndef float; gets.to_s.to_f end\r\ndef floats; gets.to_s.split.map { |s| s.to_f } end\r\ndef array_of(&convert); gets.to_s.split.map(&convert) end\r\ndef string; gets.to_s.chomp end\r\ndef strings; gets.to_s.split end\r\ndef rep(n, &b); Array.new(n, &b) end\r\ndef yes; puts $yesno[1] end\r\ndef no; puts $yesno[0] end\r\ndef yesno t; puts $yesno[t ? 1 : 0] end\r\ndef YesNo t; puts $YesNo[t ? 1 : 0] end\r\ndef YESNO t; puts $YESNO[t ? 1 : 0] end\r\ndef zip(xs, *yss); Enumerator.new { |y| xs.zip(*yss) { |a| y.yield(*a) } } end\r\ndef max(*xs, &block); block_given? ? xs.max_by(&block) : xs.max end\r\ndef min(*xs, &block); block_given? ? xs.min_by(&block) : xs.min end\r\ndef minmax(*xs, &block); block_given? ? xs.minmax_by(&block) : xs.minmax end\r\ndef gcd(*xs); xs.inject(0, :gcd) end\r\ndef matrix(h, w, fill=nil, &block); return Array.new(h) { Array.new(w, &block) } if block_given?; Array.new(h) { [fill] * w } end\r\ndef debug(x = nil); STDERR.puts (block_given? ? yield(x) : x).inspect if DEBUG; x end\r\ndef debug_grid(grid, width = 1); grid.each { |row| STDERR.puts row.map { |x| x.inspect.ljust(width) }.join(\"\") } if DEBUG; grid end\r\ndef if_debug; yield if DEBUG end\r\n\r\nmodule Boolean\r\n def coerce(other); [other, to_i] end\r\n def +@; to_i end\r\n def to_int; to_i end\r\n def *(other); to_i * other end\r\nend\r\n\r\nclass TrueClass\r\n include Boolean\r\n def to_i; 1 end\r\nend\r\n\r\nclass FalseClass\r\n include Boolean\r\n def to_i; 0 end\r\nend\r\n\r\nclass Integer\r\n def div_ceil(y); (self + y - 1) / y end\r\n def mod_inv(mod = MOD); pow(mod-2, mod) end\r\n def mod_div(y, mod = MOD); self * y.mod_inv(mod) % mod end\r\n def mod_nCr(r, mod = MOD); x = y = 1; (1..r).each { |i| x = x * (self + 1 - i) % mod; y = y * i % mod }; x.mod_div(y, mod); end\r\n def factorial(mod = MOD); (2..self).inject(1) { |f, x| f * x % mod } end\r\n def popcount; x = self; c = 0; while x > 0; c += 1 if x & 1 == 1; x >>= 1 end; c end #TODO: faster\r\n def bitbrute(&block); (1< 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\r\n def lower_bound; ac, wa = self.end_open, self.begin; while ac - wa > 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\r\n def shakutori(r2, &pred); Enumerator.new { |y| j, r = r2.begin, r2.end_open; each { |i| j += 1 while j + 1 < r and pred[i, j+1]; y.yield(i, j) } }; end\r\n def widest(&block); Enumerator.new { |y| j, n = self.begin, self.end_open; each { |i| j += 1 while j < n and block[i, j]; y.yield(i, j) if block[i, j] } } end\r\nend\r\n\r\nclass Array\r\n def power(&block); (0 ... 1 << size).each(&block) end\r\n def sorted_merge(other); a = []; i = j = 0; n, m = size, other.size; if j < m and other[j] < self[i]; a << other[j]; j += 1 else; a << self[i]; i += 1 end while i < n; a.push(*other[j..-1]) if j < m; a end\r\n def upper_bound; ac, wa = 0, size; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\r\n def lower_bound; ac, wa = size, 0; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\r\n def cum(*xs, &op); a = []; a << xs[0] if xs.size > 0; a << x = self[0]; (1...size).each { |i| a << x = op[x, self[i]] }; a end\r\n def cumdiff(range); self[range.end_open] - self[range.begin]; end\r\n def compress(kinds = uniq.sort!); map { |x| kinds.bsearch_index { _1 >= x } }; end\r\nend\r\n\r\nmodule Enumerable\r\n def sorted_uniq; x = nil; filter { |y| c = x === y; x = y; !c } end\r\n def cumsum; ys = [0]; each { |x| ys << x + ys[-1] }; ys end\r\nend\r\n\r\nmain[]"}, {"source_code": "gets.to_i.times do\r\n a, b, c, d = gets.split.map(&:to_i)\r\n p [b, c, d].count { |x| x > a }\r\nend\r\n"}], "negative_code": [], "src_uid": "e829ca4438e9cb30ea1c66eea7d5f7a7"} {"nl": {"description": "You are given two integers $$$n$$$ and $$$m$$$. You have to construct the array $$$a$$$ of length $$$n$$$ consisting of non-negative integers (i.e. integers greater than or equal to zero) such that the sum of elements of this array is exactly $$$m$$$ and the value $$$\\sum\\limits_{i=1}^{n-1} |a_i - a_{i+1}|$$$ is the maximum possible. Recall that $$$|x|$$$ is the absolute value of $$$x$$$.In other words, you have to maximize the sum of absolute differences between adjacent (consecutive) elements. For example, if the array $$$a=[1, 3, 2, 5, 5, 0]$$$ then the value above for this array is $$$|1-3| + |3-2| + |2-5| + |5-5| + |5-0| = 2 + 1 + 3 + 0 + 5 = 11$$$. Note that this example doesn't show the optimal answer but it shows how the required value for some array is calculated.You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n, m \\le 10^9$$$) — the length of the array and its sum correspondingly.", "output_spec": "For each test case, print the answer — the maximum possible value of $$$\\sum\\limits_{i=1}^{n-1} |a_i - a_{i+1}|$$$ for the array $$$a$$$ consisting of $$$n$$$ non-negative integers with the sum $$$m$$$.", "sample_inputs": ["5\n1 100\n2 2\n5 5\n2 1000000000\n1000000000 1000000000"], "sample_outputs": ["0\n2\n10\n1000000000\n2000000000"], "notes": "NoteIn the first test case of the example, the only possible array is $$$[100]$$$ and the answer is obviously $$$0$$$.In the second test case of the example, one of the possible arrays is $$$[2, 0]$$$ and the answer is $$$|2-0| = 2$$$.In the third test case of the example, one of the possible arrays is $$$[0, 2, 0, 3, 0]$$$ and the answer is $$$|0-2| + |2-0| + |0-3| + |3-0| = 10$$$."}, "positive_code": [{"source_code": "gets.to_i.times do\n\ta, b = gets.split.map(&:to_i)\n\tif a == 1\n\t\tputs 0\n\telsif a == 2\n\t\tputs b\n\telse\n\t\tputs b * 2\n\tend\nend\n"}, {"source_code": "t = gets.to_i\nuntil t==0 do\n n, m = gets.split.map(&:to_i)\n puts [2,n-1].min*m\n t-=1\n\nend\n\n"}, {"source_code": "t = gets.chomp.to_i\nt.times do |testcase|\n (n,k) = gets.split.map(&:to_i)\n if n == 1\n puts 0\n elsif n == 2\n puts k\n else\n puts k*2\n end\nend"}, {"source_code": "t = gets.to_i\n\n1.upto t do |i|\n sum = 0\n n, m = gets.split.map(&:to_i)\n if n == 1\n sum = 0\n elsif n == 2\n sum = m\n else\n sum = 2 * m\n end\n puts sum\nend"}, {"source_code": "\ngets.to_i.times {\n n, m = gets.split().map do |v|\n v.to_i\n end\n\n if n == 1\n puts 0\n elsif n == 2\n puts m\n else\n puts m * 2\n end\n}\n\n"}, {"source_code": "t=gets.chomp.to_i\nwhile (t>0)\n\tn,m = gets.split.map(&:to_i)\n\tif(n==1) \n\t\tputs\"0\"\n\telsif (n==2) \n\t\tputs\"#{m}\"\n\telse \n\t\tputs\"#{m*2}\"\n\tend\n\tt-=1\nend"}, {"source_code": "gets.to_i.times do\n n, m = gets.split.map &:to_i\n puts case n\n when 1\n 0\n when 2\n m\n else\n 2*m\n end\nend\n"}, {"source_code": "1.upto(gets.to_i) do\n n, m = gets.split(' ').map(&:to_i)\n\n k = n.div(2)\n l = m.div(k.zero? ? 1 : k)\n\n puts case n\n when 1\n 0\n when 2\n m\n when proc(&:even?)\n 2*m\n else\n 2*l*k + 2*m.modulo(k)\n end\nend\n"}, {"source_code": "t = gets.to_i\n\n(1).upto(t) do\n a = gets.split(' ').map(&:to_i)\n n = a[0]\n m = a[1]\n if n==1\n puts 0\n elsif n==2\n puts m\n else\n puts m*2\n end\nend\n"}], "negative_code": [{"source_code": "t = gets.to_i\nuntil t==0 do\n m, n = gets.split.map(&:to_i)\n puts [2,n-1].min*m\n t-=1\n\nend\n\n"}], "src_uid": "905cc16ecbbb3305416f9aa6e4412642"} {"nl": {"description": "Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left.There were $$$n$$$ classes of that subject during the semester and on $$$i$$$-th class professor mentioned some non-negative integer $$$a_i$$$ to the students. It turned out, the exam was to tell the whole sequence back to the professor. Sounds easy enough for those who attended every class, doesn't it?Obviously Mishka didn't attend any classes. However, professor left some clues on the values of $$$a$$$ to help out students like Mishka: $$$a$$$ was sorted in non-decreasing order ($$$a_1 \\le a_2 \\le \\dots \\le a_n$$$); $$$n$$$ was even; the following sequence $$$b$$$, consisting of $$$\\frac n 2$$$ elements, was formed and given out to students: $$$b_i = a_i + a_{n - i + 1}$$$. Professor also mentioned that any sequence $$$a$$$, which produces sequence $$$b$$$ with the presented technique, will be acceptable.Help Mishka to pass that last exam. Restore any sorted sequence $$$a$$$ of non-negative integers, which produces sequence $$$b$$$ with the presented technique. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$2 \\le n \\le 2 \\cdot 10^5$$$) — the length of sequence $$$a$$$. $$$n$$$ is always even. The second line contains $$$\\frac n 2$$$ integers $$$b_1, b_2, \\dots, b_{\\frac n 2}$$$ ($$$0 \\le b_i \\le 10^{18}$$$) — sequence $$$b$$$, where $$$b_i = a_i + a_{n - i + 1}$$$. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$.", "output_spec": "Print $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$0 \\le a_i \\le 10^{18}$$$) in a single line. $$$a_1 \\le a_2 \\le \\dots \\le a_n$$$ should be satisfied. $$$b_i = a_i + a_{n - i + 1}$$$ should be satisfied for all valid $$$i$$$.", "sample_inputs": ["4\n5 6", "6\n2 1 2"], "sample_outputs": ["2 3 3 3", "0 0 1 1 1 2"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\nb = gets.split.map(&:to_i)\na1,a2 = [],[]\nl,r = 0,10**18\nb.each_with_index do |v,i|\n r = r < (v-l) ? r : v - l\n l = v - r\n a1 << l\n a2 << r\nend\na1.each do |v|\n print \"#{v} \"\nend\n(n/2).times do |i|\n v = a2[-i-1]\n print \"#{v}\"\n print \" \" if i < (n/2-1)\nend\n"}, {"source_code": "def solve(n, b)\n mn, mx = 0, b[0]\n res = []\n b.each_with_index do |e, i|\n if mn + mx == e\n res[i] = mn\n res[b.size * 2 - i -1 ] = mx\n else\n if e - mn > mx\n res[b.size * 2 - i -1 ] = mx\n res[i] = e - mx\n else\n res[i] = mn\n res[b.size * 2 - i -1 ] = e - mn\n end\n end\n mn = res[i]\n mx = res[b.size * 2 - i -1 ]\n end\n res.join(' ')\nend\n\nputs solve(gets.strip.to_i, gets.strip.split.map(&:to_i))"}], "negative_code": [], "src_uid": "4585419ab2b7200770cfe1e607161e9f"} {"nl": {"description": "After a hard day Vitaly got very hungry and he wants to eat his favorite potato pie. But it's not that simple. Vitaly is in the first room of the house with n room located in a line and numbered starting from one from left to right. You can go from the first room to the second room, from the second room to the third room and so on — you can go from the (n - 1)-th room to the n-th room. Thus, you can go to room x only from room x - 1.The potato pie is located in the n-th room and Vitaly needs to go there. Each pair of consecutive rooms has a door between them. In order to go to room x from room x - 1, you need to open the door between the rooms with the corresponding key. In total the house has several types of doors (represented by uppercase Latin letters) and several types of keys (represented by lowercase Latin letters). The key of type t can open the door of type T if and only if t and T are the same letter, written in different cases. For example, key f can open door F.Each of the first n - 1 rooms contains exactly one key of some type that Vitaly can use to get to next rooms. Once the door is open with some key, Vitaly won't get the key from the keyhole but he will immediately run into the next room. In other words, each key can open no more than one door.Vitaly realizes that he may end up in some room without the key that opens the door to the next room. Before the start his run for the potato pie Vitaly can buy any number of keys of any type that is guaranteed to get to room n.Given the plan of the house, Vitaly wants to know what is the minimum number of keys he needs to buy to surely get to the room n, which has a delicious potato pie. Write a program that will help Vitaly find out this number.", "input_spec": "The first line of the input contains a positive integer n (2 ≤ n ≤ 105) — the number of rooms in the house. The second line of the input contains string s of length 2·n - 2. Let's number the elements of the string from left to right, starting from one. The odd positions in the given string s contain lowercase Latin letters — the types of the keys that lie in the corresponding rooms. Thus, each odd position i of the given string s contains a lowercase Latin letter — the type of the key that lies in room number (i + 1) / 2. The even positions in the given string contain uppercase Latin letters — the types of doors between the rooms. Thus, each even position i of the given string s contains an uppercase letter — the type of the door that leads from room i / 2 to room i / 2 + 1.", "output_spec": "Print the only integer — the minimum number of keys that Vitaly needs to buy to surely get from room one to room n.", "sample_inputs": ["3\naAbB", "4\naBaCaB", "5\nxYyXzZaZ"], "sample_outputs": ["0", "3", "2"], "notes": null}, "positive_code": [{"source_code": "# http://codeforces.com/contest/525/problem/A\n\nn = gets.to_i\ns = gets.chomp.chars\nkeys = Hash.new(0)\nans = 0\n\ns.each_slice(2) do |key, door|\n keys[key] += 1\n if keys[door.downcase] >= 1\n keys[door.downcase] -= 1\n else\n ans += 1\n end\nend\n\nputs ans\n"}, {"source_code": "\nn = gets.chomp.to_i;\narr = gets.chomp.split(\"\").map{|x| x.upcase.ord - 65;};\nmap = Array.new(26, 0);\nans = 0;\n\n(n-1).times do |i|\n i = 2 * i;\n # print i, \" \", arr[i], \"\\n\";\n map[arr[i]] += 1;\n i += 1;\n map[arr[i]] -= 1;\n if map[arr[i]] == -1\n map[arr[i]] = 0;\n ans += 1;\n end\nend\nputs ans;\n"}, {"source_code": "require 'pp'\ndef stdin_int; STDIN.gets.chomp.to_i; end\ndef stdin_ints; STDIN.gets.chomp.split(/ /).map(&:to_i); end\n\nans = 0\n\nn = stdin_int\ns = STDIN.gets.chomp\nh = {}\nh.default = 0\ns.split(//).each_with_index do |c, index|\n if index.even?\n h[c] += 1\n else\n if h[c.downcase] > 0\n h[c.downcase] -= 1\n else\n ans += 1\n end\n end\nend\n\nputs ans\n"}, {"source_code": "gets\nhs=Hash.new(0)\nans=0\ngets.chomp.chars{|e|\n\te2=e.downcase\n\tif e2==e then\n\t\ths[e]+=1\n\telse\n\t\tif hs[e2]>0 then\n\t\t\ths[e2]-=1\n\t\telse\n\t\t\tans+=1\n\t\tend\n\t\t\n\tend\n}\nputs ans"}, {"source_code": "gets;c=Hash.new(0);puts gets.strip.chars.reduce(0){|a,b|d=b.downcase;a+=(b!=d&&c[d]==0)?1:0;c[d]+=(b==d)?1:(c[d]>0?-1:0);a}"}, {"source_code": "gets\na = gets.chomp.split(//)\nhas = Hash.new(0)\nc = 0\na.each do |t|\n if t == t.downcase\n has[t] += 1\n elsif has[t.downcase] == 0\n c += 1\n else\n has[t.downcase] -= 1\n end\nend\n\nputs c"}, {"source_code": "gets\n\nanswer = 0\nstash = Hash.new { 0 }\ngets.chomp.chars.each.with_index do |ch, i|\n if i % 2 == 0\n stash[ch.upcase] += 1\n else\n if stash[ch] > 0\n stash[ch] -= 1\n else\n answer += 1\n end\n end\nend\nputs answer"}, {"source_code": "keys = Hash.new(0)\nanswer = 0\ngets\ngets.chomp.chars.each_slice(2) do |k,d|\n keys[k] += 1\n if keys[d.downcase] >= 1\n keys[d.downcase] -= 1\n else\n answer += 1\n end\nend\n\nputs answer\n"}, {"source_code": "n = gets.to_i\na = gets.split(\"\")\nr = Array.new(123, 0)\nans = 0\na.each do |x|\n if x == x.downcase\n r[x.ord] += 1\n elsif x == x.upcase\n if r[x.downcase.ord] == 0\n ans += 1\n else\n r[x.downcase.ord] -= 1\n end\n end\nend\nputs ans \n"}, {"source_code": "# f = File.open 'input.txt', 'r'\nf = STDIN\n\ndef compute(n, s)\n missing_keys = 0\n keys = Hash.new(0)\n\n n.times do |i|\n break if i == n - 1\n # store the key\n keys[s[i * 2].upcase] += 1\n\n # unlock the door\n if keys[s[i * 2 + 1]].zero?\n missing_keys += 1\n else\n keys[s[i * 2 + 1]] -= 1\n end\n end\n missing_keys\nend\n\nwhile true\n raw_n = f.gets\n break unless raw_n\n n = raw_n.chomp.to_i\n s = f.gets.chomp\n p compute(n, s)\nend\n"}], "negative_code": [{"source_code": "# http://codeforces.com/contest/525/problem/A\n\nn = gets.to_i\ns = gets.chomp.chars\nkeys = []\nans = 0\n\ns.each_slice(2) do |key, door|\n keys << key\n if keys.include?(door.downcase)\n keys.delete key\n else\n ans += 1\n end\nend\n\nputs ans\n"}, {"source_code": "# http://codeforces.com/contest/525/problem/A\n\nn = gets.to_i\ns = gets.chomp.chars\nkeys = Hash.new(0)\nans = 0\n\ns.each_slice(2) do |key, door|\n keys[key] += 1\n if keys[door.downcase] > 0\n keys[key] -= 1\n else\n ans += 1\n end\nend\n\nputs ans\n"}, {"source_code": "require 'pp'\ndef stdin_int; STDIN.gets.chomp.to_i; end\ndef stdin_ints; STDIN.gets.chomp.split(/ /).map(&:to_i); end\n\nans = 0\n\nn = stdin_int\ns = STDIN.gets.chomp\nh = {}\nh.default = 0\ns.split(//).each_with_index do |c, index|\n if index.even?\n h[c] += 1\n else\n if h.has_key?(c.downcase)\n h[c] -= 1\n else\n ans += 1\n end\n end\nend\n\nputs ans\n"}, {"source_code": "require 'pp'\ndef stdin_int; STDIN.gets.chomp.to_i; end\ndef stdin_ints; STDIN.gets.chomp.split(/ /).map(&:to_i); end\n\nans = 0\n\nn = stdin_int\ns = STDIN.gets.chomp\nh = {}\ns.split(//).each_with_index do |c, index|\n if index.even?\n h[c] = true\n else\n if h.has_key?(c.downcase)\n h.delete(c.downcase)\n else\n ans += 1\n end\n end\nend\n\nputs ans\n"}, {"source_code": "gets\nputs gets.strip.chars.each_slice(2).each_with_object(Hash.new(0)){|(a,b),o|o[a.downcase]+=1;o[b.downcase]-=1}.reduce(0){|a,v|a-(v.last>0?0:v.last)}"}, {"source_code": "gets;c=0;gets.strip.chars.each_slice(2).each_with_object(Hash.new(0)){|(a,b),o|o[a.downcase]+=1;c+=(o[b.downcase]-=1)>=0?0:1};puts c"}, {"source_code": "gets;c=0;gets.strip.chars.each_slice(2).each_with_object(Hash.new(0)){|(a,b),o|o[a.upcase]+=1;o[b]=o[b]>0?o[b]-1:0;c+=o[b]>=0?0:1};puts c"}, {"source_code": "gets;c=0;gets.strip.chars.each_slice(2).each_with_object(Hash.new(0)){|(a,b),o|o[a.upcase]+=1;o[b]=[o[b],0].max-1;c+=o[b]>=0?0:1};puts c"}], "src_uid": "80fdb95372c1e8d558b8c8f31c9d0479"} {"nl": {"description": "Gerald has n younger brothers and their number happens to be even. One day he bought n2 candy bags. One bag has one candy, one bag has two candies, one bag has three candies and so on. In fact, for each integer k from 1 to n2 he has exactly one bag with k candies. Help him give n bags of candies to each brother so that all brothers got the same number of candies.", "input_spec": "The single line contains a single integer n (n is even, 2 ≤ n ≤ 100) — the number of Gerald's brothers.", "output_spec": "Let's assume that Gerald indexes his brothers with numbers from 1 to n. You need to print n lines, on the i-th line print n integers — the numbers of candies in the bags for the i-th brother. Naturally, all these numbers should be distinct and be within limits from 1 to n2. You can print the numbers in the lines in any order. It is guaranteed that the solution exists at the given limits.", "sample_inputs": ["2"], "sample_outputs": ["1 4\n2 3"], "notes": "NoteThe sample shows Gerald's actions if he has two brothers. In this case, his bags contain 1, 2, 3 and 4 candies. He can give the bags with 1 and 4 candies to one brother and the bags with 2 and 3 to the other brother."}, "positive_code": [{"source_code": "n = gets.to_i\nar = (0...n).map{|x| x*(n+1)+1}\n(0...n).reverse_each do |x|\n puts ar.map(&:to_s).join(\" \")\n ar = ar.map(&:succ)\n ar[x] -= n\nend"}, {"source_code": "n=gets.to_i\na=[*1..n*n].each_slice(n).to_a.transpose\nputs (a[0...n/2]+a[n/2..-1].map(&:reverse)).transpose.map{|e|e*' '}"}, {"source_code": "n = gets.chomp.to_i\nfor i in 1..n\n l = \"\"\n for j in 1..n\n if j % 2 == 0\n l = l << ((j/2)*2*n-i+1).to_s << \" \"\n else\n l = l << (i+(j/2)*2*n).to_s << \" \"\n end\n end\n puts l\nend\n \n"}, {"source_code": "# -*- encoding: utf-8 -*-\nn = gets.chomp.to_i\n\ncandies = (1 .. n ** 2).to_a\nfor i in 0 .. (n - 1) do\n array = []\n (n / 2).times do\n array << candies.shift\n array << candies.pop\n end\n puts array.join(\" \")\nend"}, {"source_code": "n = STDIN.readline.to_i\nlines = []\nn.times do |r|\n\tc = 0\n\tparts = []\n\tn.times do |d|\n\t\tparts.push((r+d)%n * n + (c+d)%n + 1)\n\tend\n\tlines.push(parts.join(' '))\nend\nputs lines.join(\"\\n\")\n"}, {"source_code": "n = gets.chomp.to_i\na = (1..(n*n)).to_a\n\na = a.each_slice(a.length/2).to_a\na.last.reverse!\na = a.first.zip(a.last)\nputs a.each_slice(n/2).to_a.map{|x| x.flatten.join(' ')}\n"}, {"source_code": "n=gets.to_i\n\nfor i in 0..n-1\n\tfor j in 0..n/2-1\n\t\tprint 1+(i*n/2)+j,\" \"\n\tend\n\tfor j in 0..n/2-1\n\t\tprint n**2-n/2+1-(i*n/2)+j,\" \"\n\tend\n\tif i!=n-1\n\t\tprint \"\\n\"\n\tend\nend\n\n\n"}, {"source_code": "#!/usr/bin/env ruby\nn = gets.to_i\nind = 1\nfor i in 1..n\n str = \"\"\n for j in 0...n/2\n str += \"#{ind} \"\n str += \"#{n*n+1 - ind} \"\n ind += 1\n end\n puts str\nend\n"}, {"source_code": "#!/usr/bin/env ruby\nn = gets.to_i\nind = 1\nfor i in 1..n\n str = \"\"\n for j in 0...n/2\n str += \"#{ind + j} \"\n end\n for j in 0...n/2\n str += \"#{n*n+1 - ind} \"\n ind += 1\n end\n puts str\nend\n"}, {"source_code": "n = gets.to_i\nn.times {|i| puts (0..(n-1)).map {|j| j*n+(j+i)%n+1}.join(\" \")}\n"}, {"source_code": "#http://codeforces.com/problemset/problem/129/A\n\nn = gets.to_i \n\ninitial_first = 1 \ninitial_last = n**2 \n\n\nfirst = []\nsecond = []\nloop do \n first << initial_first \n first << initial_last \n initial_first += 2 \n initial_last -= 2 \n break if first.size >= n**2 \nend \n\n \n\nfirst.each_with_index do |i,ind| \n print \"#{i} \"\n if (ind+1)%n ==0 \n puts \n end \nend \n"}, {"source_code": "n = gets.chomp.to_i\n\nleft = 1\nright = n*n\n(1..n).each do |i|\n (left..left+n/2-1).each { |i| print \"#{i} \" }\n (right-n/2+1..right).each { |i| print \"#{i} \" }\n left += n/2\n right -= n/2\n puts\nend\n"}, {"source_code": "arr = (1..gets.to_i ** 2).to_a\nwhile arr.size != 0\n puts \"#{arr.shift} #{arr.pop}\"\nend\n"}, {"source_code": "n = gets.to_i\na = Array.new(n*n+1);\nfor i in 1..n*n do\n a[i] = i\n end\nl = 0x0\nl = 1\nr = n*n\nbegin\n if (l>r) then break end\n printf(\"%d %d\\n\", a[l], a[r])\n l += 1\n r -= 1\nend while true"}, {"source_code": "n=gets.to_i\nx=(1..n).map{[0]*n}\nt=1\n\nn.times{|c|\n\tif c%2==0 then\n\t\t0.upto(n-1){|r|\n\t\t\tx[r][c]=t\n\t\t\tt+=1\t\n\t\t}\n\telse\n\t\t(n-1).downto(0){|r|\n\t\t\tx[r][c]=t\n\t\t\tt+=1\n\t\t}\n\tend\n}\nn.times{|r|\n\tputs x[r]*\" \"\n}\n\n"}, {"source_code": "n=gets.to_i\nres=[]\nx=1\nn.times {|i|\n res<<[*x..x+n-1]\n res[i].reverse! if i.even?\n x+=n\n}\nn.times {|i|\n n.times{|j| print \"#{res[j][i]} \" }\n puts\n}\n"}, {"source_code": "# coding: utf-8\n\nn = gets.to_i\nbase = [1]\nnp2 = n * n\nwhile base.last < np2\n new_inc = base.last + (n + 1)\n base << new_inc\nend\n\nn.times {\n puts base.join(' ')\n #p \"---> #{base.sum}\"\n for i in 0...n\n base[i] = (base[i] + n)%np2\n end\n}\n\n%{\n 먼저 1을 준비한다.\n n이 될때까지 n+1을 더한다.\n 이렇게 만들어진 배열의 크기는 n과 같다. ---A\n 이 배열의 합은 N = n*n (최대값)이라고 할 때\n (N*(N+1)/2)/n = n*(n*n + 1)/2 이다.\n n이 홀수라면 n*n도 홀수이므로 +1에서 짝수가 되어\n 2로 나눌 수 있고, n이 짝수라면 두말할 필요없이 2로 나눠져\n 저 결과값은 항상 정수를 유지한다. ----B\n A에서 만들어진 수열의 합은 B의 값과 같다.\n 예를 들어 n = 3인 경우는 15, n = 4의 경우는 34이다.\n 여기서부터 중요한데, A에서 만들어진 수열에 각각 아래의 연산을 취한다.\n A의 배열 원소를 An이라 할때,\n 1) An 에 n을 더한다\n 2) 1에 mod n*n 연산을 취해 나머지 값을 얻는다.\n 이렇게 되면 항상 일정한 위치의 수끼리 합을 구하게 되므로 일정한 합계가 반복된다.\n 이걸 n-1 회 반복하면 원하는 값들을 얻을 수 있다.\n 예) n = 3\n step1) [1, 5, 9]\n step2) [4, 8, (3 + 9)%9] -> [4, 8, 3]\n step3) [7, (8 + 3)%9, 6)] -> [7, 2, 6]\n step4) [(7 + 3)%9, 5, 9 ] -> [1, 5, 9] <==> step1\n\n 근데 시발 왜 이게 되지?\n}\n\n"}, {"source_code": "def main()\n\tn=gets.chomp.to_i\n\tar=Array.new(n){}\n\tfor i in 0...n\n\t\tar[i]=\"\"\n\tend\n\tf=true\n\tj=1\n\ti=n*n\n\twhile(j<=i)\n\t\tif f\n\t\t\tfor k in 0...n\n\t\t\t\tar[k]+=j.to_s+\" \"\n\t\t\t\tj+=1\n\t\t\tend\n\t\telse\n\t\t\tfor k in 0...n\n\t\t\t\tl=n-k-1\n\t\t\t\tar[l]+=j.to_s+\" \"\n\t\t\t\tj+=1\n\t\t\tend\n\t\tend\n\t\tf=!f\n\tend\n\tfor i in 0...n\n\t\tputs ar[i]\n\tend\nend\nmain()"}, {"source_code": "n = gets.to_i; n=n*n\n([(1..n/2).to_a] + [((n/2+1)..n).to_a.reverse]).transpose.each{|x| puts x.join(' ')}"}, {"source_code": "n = gets.to_i\na = Array.new(n){|x| x += 1}\nn.times do |i|\n b = []\n n.times do |j|\n b << n * j + a[(i + j) % n]\n end\n puts b.join(\" \")\nend\n"}, {"source_code": "#16h26\nn = gets\n\ndef run(n)\n r = n*n\n q = n/2\n n.times do |i|\n st = i*q\n s = (1+st...q+1+st).to_a\n b = (r-q+1-st...r+1-st).to_a\n puts \"#{s.join(' ')} #{b.join(' ')}\"\n end\nend\n\nwhile n != nil\n run(n.to_i)\n n = gets\nend\n"}, {"source_code": "n=gets.to_i\nn.times do |i|\n n.times do |j|\n print(j*n+(j+i)%n+1, ' ')\n end\n puts\nend\n"}, {"source_code": "n = gets.to_i\nputs (n**2).times.map(&:next).each_slice(n).each_with_index.map{|x, i| x.rotate i }.transpose.map{|x| x.join ' ' }\n"}, {"source_code": "n = gets.to_i\nn2 = n**2\nn.times do |i|\n list = []\n (n/2).times do |j|\n list << i + 1 + j * n\n list << n2 - (i + j * n)\n end\n puts list.join(\" \")\nend\n"}], "negative_code": [{"source_code": "n=gets.to_i\na=[*1..n].each_slice(n).to_a.transpose\na[n/2..-1].map!(&:reverse)\nputs a.transpose.map{|e|e*' '}"}, {"source_code": "n=gets.to_i\na=[*1..n*n].each_slice(n).to_a.transpose\na[n/2..-1].map!(&:reverse)\nputs a.transpose.map{|e|e*' '}"}, {"source_code": "#http://codeforces.com/problemset/problem/129/A\n\nn = gets.to_i \n\ninitial_first = 1 \ninitial_last = n**2 \n\n\nfirst = []\nsecond = []\nn.times do \n first << initial_first \n first << initial_last \n initial_first += 2 \n initial_last -= 2 \nend \n\nsecond = first.shift(n) \n\nputs first.join(' ')\nputs second.join(' ')"}, {"source_code": "#http://codeforces.com/problemset/problem/129/A\n\nn = gets.to_i \n\ninitial_first = 1 \ninitial_last = n**2 \n\np initial_first \np initial_last\n\nfirst = []\nsecond = []\nn.times do \n first << initial_first \n first << initial_last \n initial_first += 2 \n initial_last -= 2 \nend \n\nsecond = first.shift(n) \n\nputs first.join(' ')\nputs second.join(' ')"}, {"source_code": "n = gets.to_i\n([(1..n/2).to_a] + [((n/2+1)..n).to_a.reverse]).transpose.each{|x| puts x.join(' ')}"}, {"source_code": "#16h26\nn = gets\n\ndef run(n)\n r = n*n\n q = n/2\n n.times do |i|\n s = (i+1...i+q+1).to_a\n b = (r-i-q+1...r-i+1).to_a\n puts \"#{s.join(' ')} #{b.join(' ')}\"\n end\nend\n\nwhile n != nil\n run(n.to_i)\n n = gets\nend\n"}], "src_uid": "0ac2a0954fe43d66eac32072c8ea5070"} {"nl": {"description": "There are times you recall a good old friend and everything you've come through together. Luckily there are social networks — they store all your message history making it easy to know what you argued over 10 years ago.More formal, your message history is a sequence of messages ordered by time sent numbered from 1 to n where n is the total number of messages in the chat.Each message might contain a link to an earlier message which it is a reply to. When opening a message x or getting a link to it, the dialogue is shown in such a way that k previous messages, message x and k next messages are visible (with respect to message x). In case there are less than k messages somewhere, they are yet all shown.Digging deep into your message history, you always read all visible messages and then go by the link in the current message x (if there is one) and continue reading in the same manner.Determine the number of messages you'll read if your start from message number t for all t from 1 to n. Calculate these numbers independently. If you start with message x, the initial configuration is x itself, k previous and k next messages. Messages read multiple times are considered as one.", "input_spec": "The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ n) — the total amount of messages and the number of previous and next messages visible. The second line features a sequence of integers a1, a2, ..., an (0 ≤ ai < i), where ai denotes the i-th message link destination or zero, if there's no link from i. All messages are listed in chronological order. It's guaranteed that the link from message x goes to message with number strictly less than x.", "output_spec": "Print n integers with i-th denoting the number of distinct messages you can read starting from message i and traversing the links while possible.", "sample_inputs": ["6 0\n0 1 1 2 3 2", "10 1\n0 1 0 3 4 5 2 3 7 0", "2 2\n0 1"], "sample_outputs": ["1 2 2 3 3 3", "2 3 3 4 5 6 6 6 8 2", "2 2"], "notes": "NoteConsider i = 6 in sample case one. You will read message 6, then 2, then 1 and then there will be no link to go.In the second sample case i = 6 gives you messages 5, 6, 7 since k = 1, then 4, 5, 6, then 2, 3, 4 and then the link sequence breaks. The number of distinct messages here is equal to 6."}, "positive_code": [{"source_code": "n, k = gets.split.map(&:to_i)\narr = gets.split.map(&:to_i).map{ |x| x-1}\ndp = Array.new(n, 0)\nfor i in 0..(n-1)\n mx = -1\n if arr[i] != -1\n last = [arr[i]+k, i-k-1].max\n mx = [i+k, n-1].min\n dp[i] = dp[arr[i]]+[mx-last, 0].max\n else\n last = [-1, i-k-1].max\n mx = [i+k, n-1].min\n dp[i] = mx-last\n end\nend\n\nputs dp.join(' ')\n"}], "negative_code": [], "src_uid": "0b4e6268cf1238169591e17cb644d451"} {"nl": {"description": "You are given a sequence $$$a_1, a_2, \\dots, a_n$$$ consisting of $$$n$$$ non-zero integers (i.e. $$$a_i \\ne 0$$$). You have to calculate two following values: the number of pairs of indices $$$(l, r)$$$ $$$(l \\le r)$$$ such that $$$a_l \\cdot a_{l + 1} \\dots a_{r - 1} \\cdot a_r$$$ is negative; the number of pairs of indices $$$(l, r)$$$ $$$(l \\le r)$$$ such that $$$a_l \\cdot a_{l + 1} \\dots a_{r - 1} \\cdot a_r$$$ is positive; ", "input_spec": "The first line contains one integer $$$n$$$ $$$(1 \\le n \\le 2 \\cdot 10^{5})$$$ — the number of elements in the sequence. The second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ $$$(-10^{9} \\le a_i \\le 10^{9}; a_i \\neq 0)$$$ — the elements of the sequence.", "output_spec": "Print two integers — the number of subsegments with negative product and the number of subsegments with positive product, respectively.", "sample_inputs": ["5\n5 -3 3 -1 1", "10\n4 2 -4 3 1 2 -4 3 2 3", "5\n-1 -2 -3 -4 -5"], "sample_outputs": ["8 7", "28 27", "9 6"], "notes": null}, "positive_code": [{"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\nn = inp[0]\nt = inp\nm = na(2,0)\nm[0] = 0\nf = 0\nff = 0\npos = neg = 0\nt.each.with_index do |d,i|\n if(d < 0)\n m[0],m[1] = m[1],m[0]\n m[1] += 1\n else\n m[0] += 1\n end\n pos += m[0]\n neg += m[1]\nend\nputs \"#{neg} #{pos}\""}, {"source_code": "def number_of_products\n n = gets.chomp.to_i\n numbers = gets.chomp.split(\" \").map(&:to_i)\n positive, negative, a, b = 0, 0, 0, 0\n numbers.each do |num|\n c, d = 0, 0\n if num > 0\n c, d = a+1, b\n else\n c, d = b, a+1\n end\n a, b = c, d\n positive += c\n negative += d\n end\n puts \"#{negative} #{positive}\"\nend\n\nnumber_of_products\n"}], "negative_code": [], "src_uid": "78d3acc3ade53f488739a59100bfcebd"} {"nl": {"description": "Vasya is reading a e-book. The file of the book consists of $$$n$$$ pages, numbered from $$$1$$$ to $$$n$$$. The screen is currently displaying the contents of page $$$x$$$, and Vasya wants to read the page $$$y$$$. There are two buttons on the book which allow Vasya to scroll $$$d$$$ pages forwards or backwards (but he cannot scroll outside the book). For example, if the book consists of $$$10$$$ pages, and $$$d = 3$$$, then from the first page Vasya can scroll to the first or to the fourth page by pressing one of the buttons; from the second page — to the first or to the fifth; from the sixth page — to the third or to the ninth; from the eighth — to the fifth or to the tenth.Help Vasya to calculate the minimum number of times he needs to press a button to move to page $$$y$$$.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 10^3$$$) — the number of testcases. Each testcase is denoted by a line containing four integers $$$n$$$, $$$x$$$, $$$y$$$, $$$d$$$ ($$$1\\le n, d \\le 10^9$$$, $$$1 \\le x, y \\le n$$$) — the number of pages, the starting page, the desired page, and the number of pages scrolled by pressing one button, respectively.", "output_spec": "Print one line for each test. If Vasya can move from page $$$x$$$ to page $$$y$$$, print the minimum number of times he needs to press a button to do it. Otherwise print $$$-1$$$.", "sample_inputs": ["3\n10 4 5 2\n5 1 3 4\n20 4 19 3"], "sample_outputs": ["4\n-1\n5"], "notes": "NoteIn the first test case the optimal sequence is: $$$4 \\rightarrow 2 \\rightarrow 1 \\rightarrow 3 \\rightarrow 5$$$.In the second test case it is possible to get to pages $$$1$$$ and $$$5$$$.In the third test case the optimal sequence is: $$$4 \\rightarrow 7 \\rightarrow 10 \\rightarrow 13 \\rightarrow 16 \\rightarrow 19$$$."}, "positive_code": [{"source_code": "t = gets.to_i\nt.times do\n n, x, y, d = gets.split.map{|x| x.to_i}\n a = []\n a << ((y - x)/d).abs if (y - x)%d == 0\n a << (y - 1)/d + (x - 1 + d - 1)/d if (y - 1)%d == 0\n a << (n - y)/d + (n - x + d - 1)/d if (n - y)%d == 0\n puts(a.min || -1)\nend\n"}, {"source_code": "t = gets.to_i\n\nt.times do\n nxyd = gets.chomp.split(\" \").map(&:to_i)\n n = nxyd[0]\n x = nxyd[1]\n y = nxyd[2]\n d = nxyd[3]\n if (x%d==y%d)\n ans = (x-y).abs/d\n else\n if (y%d==1)\n ans = ((x.to_f-1)/d).ceil + ((y.to_f-1)/d).ceil\n if (n%d==1)\n ans = [ans,((n.to_f-x)/d).ceil+((n.to_f-y)/d).ceil].min\n end\n elsif (n%d==y%d)\n ans = ((n.to_f-x)/d).ceil+((n.to_f-y)/d).ceil\n else\n ans = -1\n end\n end\n\n puts ans\nend\n \n"}, {"source_code": "gets.to_i.times{\n\tn,x,y,d=gets.split.map &:to_i\n\tr = Float::INFINITY\n\t(y-1)%d==0 && r=[r,(x+d-2)/d+(y-1)/d].min\n\t(n-y)%d==0 && r=[r,(n+d-x-1)/d+(n-y)/d].min\n\t(y-x)%d==0 && r=[r,(y-x).abs/d].min\n\tp r.is_a?(Integer) ? r : -1\n}"}, {"source_code": "n = gets.strip.to_i\nres = []\nn.times do\n len, start, finish, per = gets.strip.split.map(&:to_i)\n\n if finish == start\n res << 0\n next\n end\n\n if (finish - start) % per == 0\n res << ((finish - start) / per).abs\n else\n from_start = (finish - 1) % per == 0\n from_end = (len - finish) % per == 0\n if from_start || from_end\n f_s = ((start.to_f/per).ceil + ((finish - 1) / per)).abs\n f_e = (((len - start).to_f / per).ceil + ((len - finish) / per)).abs\n r = 0\n if from_start && from_end\n if f_s > f_e\n r = f_e\n else\n r = f_s\n end\n else\n if from_start\n r = f_s\n else\n r = f_e\n end\n end\n r = 1 if r == 0\n res << r\n else\n res << -1\n end\n end\nend\n\nn.times do |x|\n p res[x]\nend\n"}, {"source_code": "def number_of_steps(crt, target, step)\n ((target - crt).abs.to_f / step).ceil\nend\n\ndef exact_number_of_steps(crt, target, step)\n (target - crt).abs % step == 0 && number_of_steps(crt, target, step)\nend\n\ndef min_steps(max, crt, target, step)\n exact = exact_number_of_steps(crt, target, step)\n return exact if exact\n\n via_beginning = [number_of_steps(crt, 1, step), exact_number_of_steps(1, target, step)]\n via_end = [number_of_steps(crt, max, step), exact_number_of_steps(max, target, step)]\n\n possibilities = []\n possibilities << via_beginning[0] + via_beginning[1] if via_beginning[1]\n possibilities << via_end[0] + via_end[1] if via_end[1]\n\n possibilities.min || -1\n\nend\n\ndef solution\n test_cases = read_int\n\n test_cases.times do\n max, crt, target, step = read_ints\n puts min_steps(max, crt, target, step)\n end\n\nend\n\ndef read_int\n gets.to_i\nend\n\ndef read_ints\n gets.split.map(&:to_i)\nend\n\ndef read_string\n gets.chomp\nend\n\nsolution unless ENV['TEST__MODE']"}, {"source_code": "gets.to_i.times do\n n,x,y,d=gets.split.map &:to_i\n if (x-y).abs%d==0\n puts (x-y).abs/d\n else\n a=nil\n a=(x-1+d-1)/d+(y-1+d-1)/d if (y-1)%d==0\n b=nil\n b=(n-x+d-1)/d+(n-y+d-1)/d if (n-y)%d==0\n if a && b\n puts [a, b].min\n elsif a\n puts a\n elsif b\n puts b\n else\n puts -1\n end\n end\nend\n"}, {"source_code": "t = gets.to_i\n\n1..t.times do\n n, start, fin, k = gets.split.map(&:to_i)\n \n if (fin-start) % k == 0 \n puts ((fin-start)/k).abs\n else\n ans = 9999999999\n if (fin-1)%k==0\n ans = (fin-1)/k + ((start-1)/k.to_f).ceil\n end\n \n if (n-fin)%k==0\n ans = [ans, (n-fin)/k + ((n-start)/k.to_f).ceil].min\n end\n \n if ans == 9999999999\n puts -1\n else\n puts ans\n end\n end\nend"}], "negative_code": [{"source_code": "t = gets.to_i\nt.times do\n n, x, y, d = gets.split.map{|x| x.to_i}\n a = []\n a << (y - x)/d if (y - x)%d == 0\n a << (y - 1)/d + (x - 1 + d - 1)/d if (y - 1)%d == 0\n a << (n - y)/d + (n - x + d - 1)/d if (n - y)%d == 0\n puts(a.min || -1)\nend\n"}, {"source_code": "t = gets.to_i\nt.times do\n n, x, y, d = gets.split.map{|x| x.to_i}\n x, y = y, x if x > y\n a = []\n a << (y - x)/d if (y - x)%d == 0\n a << (y - 1)/d + (x - 1 + d - 1)/d if (y - 1)%d == 0\n a << (n - y)/d + (n - x + d - 1)/d if (n - y)%d == 0\n puts(a.min || -1)\nend\n"}, {"source_code": "t = gets.to_i\n\nt.times do\n nxyd = gets.chomp.split(\" \").map(&:to_i)\n n = nxyd[0]\n x = nxyd[1]\n y = nxyd[2]\n d = nxyd[3]\n if (x%d==y%d)\n ans = (x-y).abs/d\n else\n if (y%d==1)\n ans = ((x.to_f-1)/d).ceil + ((y.to_f-1)/d).ceil\n if (n%d==1)\n ans = [ans,((n.to_f-x)/d).ceil+((y.to_f-1)/d).ceil].min\n end\n elsif (n%d==y%d)\n ans = ((n.to_f-x)/d).ceil+((y.to_f-1)/d).ceil\n else\n ans = -1\n end\n end\n\n puts ans\nend\n \n"}, {"source_code": "gets.to_i.times{\n\tn,x,y,d=gets.split.map &:to_i\n\tr = Float::INFINITY\n\t(y-1)%d==0 && r=[r,(x+d-2)/d+(y-1)/d].min\n\t(n-y)%d==0 && r=[r,(x+d-x-1)/d+(n-y)/d].min\n\t(y-x)%d==0 && r=[r,(y-x).abs/d].min\n\tp r.is_a?(Integer) ? r : -1\n}"}, {"source_code": "n = gets.strip.to_i\nres = []\nn.times do\n len, start, finish, per = gets.strip.split.map(&:to_i)\n\n if finish == start\n res << 0\n next\n end\n\n if (finish - start) % per == 0\n res << ((finish - start) / per).abs\n else\n from_start = (finish - 1) % per == 0\n from_end = (len - finish) % per == 0\n if from_start || from_end\n f_s = ((finish.to_f/per).floor + ((finish - 1) / per)).abs\n f_e = (((len - start).to_f / per).ceil + ((len - finish) / per)).abs\n r = 0\n if from_start && from_end\n if f_s > f_e\n r = f_e\n else\n r = f_s\n end\n else\n if from_start\n r = f_s\n else\n r = f_e\n end\n end\n r = 1 if r == 0\n res << r\n else\n res << -1\n end\n end\nend\n\nn.times do |x|\n p res[x]\nend\n"}, {"source_code": "n = gets.strip.to_i\nres = []\nn.times do\n len, start, finish, per = gets.strip.split.map(&:to_i)\n if (finish - start) % per == 0\n res << ((finish - start) / per).abs\n else\n from_start = (finish - 1) % per == 0\n from_end = (len - finish) % per == 0\n if from_start || from_end\n f_s = ((finish.to_f/per).floor + ((finish - 1) / per)).abs\n f_e = (((len - start).to_f / per).ceil + ((len - finish) / per)).abs\n if from_start && from_end\n if f_s > f_e\n res << f_e\n else\n res << f_s\n end\n else\n if from_start\n res << f_s\n else\n res << f_e\n end\n end\n else\n res << -1\n end\n end\nend\n\nn.times do |x|\n r = res[x]\n r = 1 if r == 0\n p r\nend\n"}], "src_uid": "474f29da694929a64eaed6eb8e4349c3"} {"nl": {"description": "\"Night gathers, and now my watch begins. It shall not end until my death. I shall take no wife, hold no lands, father no children. I shall wear no crowns and win no glory. I shall live and die at my post. I am the sword in the darkness. I am the watcher on the walls. I am the shield that guards the realms of men. I pledge my life and honor to the Night's Watch, for this night and all the nights to come.\" — The Night's Watch oath.With that begins the watch of Jon Snow. He is assigned the task to support the stewards.This time he has n stewards with him whom he has to provide support. Each steward has his own strength. Jon Snow likes to support a steward only if there exists at least one steward who has strength strictly less than him and at least one steward who has strength strictly greater than him.Can you find how many stewards will Jon support?", "input_spec": "First line consists of a single integer n (1 ≤ n ≤ 105) — the number of stewards with Jon Snow. Second line consists of n space separated integers a1, a2, ..., an (0 ≤ ai ≤ 109) representing the values assigned to the stewards.", "output_spec": "Output a single integer representing the number of stewards which Jon will feed.", "sample_inputs": ["2\n1 5", "3\n1 2 5"], "sample_outputs": ["0", "1"], "notes": "NoteIn the first sample, Jon Snow cannot support steward with strength 1 because there is no steward with strength less than 1 and he cannot support steward with strength 5 because there is no steward with strength greater than 5.In the second sample, Jon Snow can support steward with strength 2 because there are stewards with strength less than 2 and greater than 2."}, "positive_code": [{"source_code": "n,*a=$<.read.split.map &:to_i;p (a-a.minmax).size"}, {"source_code": "n = gets.chomp\narr= gets.split().map(&:to_i)\narr.sort! \nmin_val =arr.min \nmax_val = arr.max \ncount = 0 \narr.each{|i| if i>min_val and i max\nend\n\ncount = 0\n(stewards_map.keys - [min, max]).each do |v|\n count += stewards_map[v]\nend\n\nputs count\n"}], "negative_code": [{"source_code": "n=gets.to_i\nx=gets.split.map{|e| e.to_i}\n\narr=[]\nx.inject(x[0]){|e1,e2|\n\tarr<<(e2-e1).abs\n\te2\n}\nputs arr.count{|e| e==1}\n\n"}, {"source_code": "h=Hash.new(0)\nn=gets.to_i\nx=gets.split.map{|e| e.to_i}.sort\nx.each{|e|\n\th[e]+=1\n}\nx=h.to_a\nx.pop\nx.shift\np x.size\n\n"}, {"source_code": "n=gets.to_i\nx=gets.split.map{|e| e.to_i}.sort\n\narr=[]\nx.inject(x[0]){|e1,e2|\n\tarr<<(e2-e1).abs\n\te2\n}\nputs arr.count{|e| e==1}\n\n"}, {"source_code": "h=Hash.new(0)\nn=gets.to_i\nx=gets.split.map{|e| e.to_i}.sort\nx.each{|e|\n\th[e]+=1\n}\nx=h.to_a\nans=0\nx.inject(x[0]){|e1,e2|\n\tif e1[0]+1==e2[0] then\n\t\tans+=e1[1]*e2[1]\n\tend\n\te2\n}\np ans\n\n"}, {"source_code": "# coding: utf-8\n\nn = gets.to_i\nad = {}\ngets.split.each { |i|\n i = i.to_i\n ad[i] ||= 0\n ad[i] += 1\n}\n\nif ad.length - 2 <= 0\n puts \"0\"\nelse\n puts ad.length - 2\nend"}], "src_uid": "acaa8935e6139ad1609d62bb5d35128a"} {"nl": {"description": "Slava plays his favorite game \"Peace Lightning\". Now he is flying a bomber on a very specific map.Formally, map is a checkered field of size 1 × n, the cells of which are numbered from 1 to n, in each cell there can be one or several tanks. Slava doesn't know the number of tanks and their positions, because he flies very high, but he can drop a bomb in any cell. All tanks in this cell will be damaged.If a tank takes damage for the first time, it instantly moves to one of the neighboring cells (a tank in the cell n can only move to the cell n - 1, a tank in the cell 1 can only move to the cell 2). If a tank takes damage for the second time, it's counted as destroyed and never moves again. The tanks move only when they are damaged for the first time, they do not move by themselves.Help Slava to destroy all tanks using as few bombs as possible.", "input_spec": "The first line contains a single integer n (2 ≤ n ≤ 100 000) — the size of the map.", "output_spec": "In the first line print m — the minimum number of bombs Slava needs to destroy all tanks. In the second line print m integers k1, k2, ..., km. The number ki means that the i-th bomb should be dropped at the cell ki. If there are multiple answers, you can print any of them.", "sample_inputs": ["2", "3"], "sample_outputs": ["3\n2 1 2", "4\n2 1 3 2"], "notes": null}, "positive_code": [{"source_code": "n=gets.to_i\nx=(1..n).select(&:odd?)\ny=(1..n).select(&:even?)\nres=y+x+y\nputs res.size, res*' '\n"}], "negative_code": [], "src_uid": "c40cb0a89c2b604aa7384476b57e96b3"} {"nl": {"description": "You are given an integer $$$n$$$ ($$$n \\ge 0$$$) represented with $$$k$$$ digits in base (radix) $$$b$$$. So,$$$$$$n = a_1 \\cdot b^{k-1} + a_2 \\cdot b^{k-2} + \\ldots a_{k-1} \\cdot b + a_k.$$$$$$For example, if $$$b=17, k=3$$$ and $$$a=[11, 15, 7]$$$ then $$$n=11\\cdot17^2+15\\cdot17+7=3179+255+7=3441$$$.Determine whether $$$n$$$ is even or odd.", "input_spec": "The first line contains two integers $$$b$$$ and $$$k$$$ ($$$2\\le b\\le 100$$$, $$$1\\le k\\le 10^5$$$) — the base of the number and the number of digits. The second line contains $$$k$$$ integers $$$a_1, a_2, \\ldots, a_k$$$ ($$$0\\le a_i < b$$$) — the digits of $$$n$$$. The representation of $$$n$$$ contains no unnecessary leading zero. That is, $$$a_1$$$ can be equal to $$$0$$$ only if $$$k = 1$$$.", "output_spec": "Print \"even\" if $$$n$$$ is even, otherwise print \"odd\". You can print each letter in any case (upper or lower).", "sample_inputs": ["13 3\n3 2 7", "10 9\n1 2 3 4 5 6 7 8 9", "99 5\n32 92 85 74 4", "2 2\n1 0"], "sample_outputs": ["even", "odd", "odd", "even"], "notes": "NoteIn the first example, $$$n = 3 \\cdot 13^2 + 2 \\cdot 13 + 7 = 540$$$, which is even.In the second example, $$$n = 123456789$$$ is odd.In the third example, $$$n = 32 \\cdot 99^4 + 92 \\cdot 99^3 + 85 \\cdot 99^2 + 74 \\cdot 99 + 4 = 3164015155$$$ is odd.In the fourth example $$$n = 2$$$."}, "positive_code": [{"source_code": "input_1 = gets.split.map(&:to_i)\ninput_2 = gets.split.map(&:to_i)\n\nb = input_1[0]\nk = input_1[1]\n\nb_flag = 0\nk_flag = 0\nodd_count = 0\n\nif b % 2 == 0\n b_flag = 0\nelse\n b_flag = 1\nend\n\nfor i in 0..k - 2 do\n if input_2[i] % 2 != 0\n odd_count += 1\n end\nend\n\nif b_flag == 0\n if input_2[k - 1] % 2 == 0\n puts \"even\"\n else\n puts \"odd\"\n end\nelse\n if odd_count % 2 == 0\n if input_2[k - 1] % 2 == 0\n puts \"even\"\n else\n puts \"odd\"\n end\n else\n if input_2[k - 1] % 2 == 0\n puts \"odd\"\n else\n puts \"even\"\n end\n end\nend\n"}, {"source_code": "b,k = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\nans = 0\nif b%2 == 0\n ans ^= a[-1]%2\nelse\n for i in 0..k-1\n ans ^= a[i]%2\n end\nend\nputs (ans == 1 ? \"odd\" : \"even\")"}, {"source_code": "DBG = true\nb,k = gets.split.map{|z| z.to_i}\na = gets.split.map{|z| z.to_i}\nif b%2 == 0\n iseven = (a[-1]%2 == 0)\nelse\n iseven = (a.inject(:+)%2 == 0)\nend\nputs (iseven ? \"even\" : \"odd\")\n"}], "negative_code": [{"source_code": "input_1 = gets.split.map(&:to_i)\ninput_2 = gets.split.map(&:to_i)\n\nb = input_1[0]\nk = input_2[1]\n\nb_flag = 0\nk_flag = 0\nodd_count = 0\n\nif b % 2 == 0\n b_flag = 0\nelse\n b_flag = 1\nend\n\ninput_2.each do |num|\n if num % 2 != 0\n odd_count += 1\n end\nend\n\nif b_flag == 0\n puts \"even\"\nelse\n if odd_count % 2 == 0\n puts \"even\"\n else\n puts \"odd\"\n end\nend\n"}], "src_uid": "ee105b664099808143a94a374d6d5daa"} {"nl": {"description": "Well, here is another math class task. In mathematics, GCD is the greatest common divisor, and it's an easy task to calculate the GCD between two positive integers.A common divisor for two positive numbers is a number which both numbers are divisible by.But your teacher wants to give you a harder task, in this task you have to find the greatest common divisor d between two integers a and b that is in a given range from low to high (inclusive), i.e. low ≤ d ≤ high. It is possible that there is no common divisor in the given range.You will be given the two integers a and b, then n queries. Each query is a range from low to high and you have to answer each query.", "input_spec": "The first line contains two integers a and b, the two integers as described above (1 ≤ a, b ≤ 109). The second line contains one integer n, the number of queries (1 ≤ n ≤ 104). Then n lines follow, each line contains one query consisting of two integers, low and high (1 ≤ low ≤ high ≤ 109).", "output_spec": "Print n lines. The i-th of them should contain the result of the i-th query in the input. If there is no common divisor in the given range for any query, you should print -1 as a result for this query.", "sample_inputs": ["9 27\n3\n1 5\n10 11\n9 11"], "sample_outputs": ["3\n-1\n9"], "notes": null}, "positive_code": [{"source_code": "\ndef search(x,res)\n lb=-1\n ub=res.length\n while ub-lb>1\n mid=(ub+lb)>>1\n if res[mid]<=x then\n lb=mid\n else\n ub=mid\n end\n end\n return ub\nend\n\ninput=gets.split.map(&:to_i)\na=input.min\nb=input.max\n\nres=[]\nfor i in 1..a\n if i*i>a then\n break\n end\n if a%i ==0 then\n if b%i==0 then\n res.push(i)\n end\n end\nend\nfor i in 1..a\n if i*i>=a then\n break\n end\n if a%i ==0 then\n if b%(a/i)==0 then\n res.push(a/i)\n end\n end\nend\nres.sort!\n\n#p res\n#p search(2,res)\nq=gets.to_i\n(1..q).each{\n input=gets.split.map(&:to_i)\n x=res[search(input[1],res)-1]\n if x>=input[0] then\n puts x\n else\n puts -1\n end\n}\n"}, {"source_code": "def r()\n gets.split().map(&:to_i)\nend\na,b = r()\nq = gets.to_i\ng = a.gcd(b)\nd = []\ni = 1\nwhile i*i<=g do\n d+=[i, g/i] if g%i==0\n i+=1\nend\nd.sort!\nfor i in (0..q-1)\n l,r=r()\n a = d.reject{|e| e>r}[-1] \n a = -1 if ar}[-1] \n a = -1 if a=l\n print \"-1\\n\" if a n\n while n % i == 0\n n /= i\n f[i] += 1\n end\n d = 2\n end\n f[n] += 1 unless n == 1\n end.to_a\n\n [].tap do |ret|\n f = lambda do |i, r|\n if i == factors.size\n ret << r\n return\n end\n (factors[i][1] + 1).times do\n f.call(i + 1, r)\n r *= factors[i][0]\n end\n end\n f.call(0, 1)\n end\nend\n\na, b = gets.chomp.split.map{|s| s.to_i}\ncds = (divisors(a) & divisors(b)).sort\nret = []\ngets.to_i.times do\n result = -1\n r = gets.chomp.split.map{|s| s.to_i}\n min, max = 0, cds.size - 1\n n = r[1]\n while min <= max\n mid = (min + max) / 2\n if n == cds[mid]\n min = mid\n break\n elsif n < cds[mid]\n max = mid - 1\n else\n min = mid + 1\n end\n end\n i = min\n i -= 1 unless cds[i] == r[1]\n if cds[i] < r[0]\n ret << -1\n else\n ret << cds[i]\n end\nend\nret.each{|s| puts s}\n"}], "negative_code": [{"source_code": "def factors(n)\n Hash.new(0).tap do |factors|\n d = 1\n 2.step(n, d).each do |i|\n next if i * i > n\n while n % i == 0\n n /= i\n factors[i] += 1\n end\n d = 2\n end\n factors[n] += 1 unless n == 1\n end.to_a\nend\n\ndef divisors(factors)\n [].tap do |ret|\n f = lambda do |i, r|\n if i == factors.size\n ret << r\n return\n end\n (factors[i][1] + 1).times do\n f.call(i + 1, r)\n r *= factors[i][0]\n end\n end\n f.call(0, 1)\n end\nend\n\ndef bsearch(array, n)\n min, max = 0, array.size - 1\n while min <= max\n mid = (min + max) / 2\n if n == array[mid]\n return mid\n elsif n < array[mid]\n max = mid - 1\n else\n min = mid + 1\n end\n end\n return min\nend\n\na, b = gets.chomp.split.map{|s| s.to_i}\ncds = divisors(factors(a)) & divisors(factors(b))\nret = []\ngets.to_i.times do\n result = -1\n r = gets.chomp.split.map{|s| s.to_i}\n i = bsearch(cds, r[1])\n i -= 1 unless cds[i] == r[1]\n if cds[i] < r[0]\n ret << -1\n else\n ret << cds[i]\n end\nend\nret.each{|s| puts s}\n"}, {"source_code": "require 'mathn'\na, b = gets.chomp.split.map{|s| s.to_i}\ndivisors = (2..[a,b].min).select do |i|\n a % i == 0 && b % i == 0\nend\nret = []\ngets.to_i.times do\n i = gets.chomp.split.map{|s| s.to_i}\n if n = divisors.detect{|d| (i[0]..i[1]).to_a.include?(d)}\n ret << n\n else\n ret << -1\n end\nend\nret.each{|i| puts i}\n"}, {"source_code": "def bsearch(array, n)\n min, max = 0, array.size - 1\n while min <= max\n mid = (min + max) / 2\n if n == array[mid]\n return mid\n elsif n < array[mid]\n max = mid - 1\n else\n min = mid + 1\n end\n end\n return min\nend\na, b = gets.chomp.split.map{|s| s.to_i}\ndivisors = [1]\n(2..(a > b ? b : a)).each do |i|\n divisors << i if a % i == 0 && b % i == 0\nend\nret = []\ngets.to_i.times do\n result = -1\n r = gets.chomp.split.map{|s| s.to_i}\n if divisors.last < r[0]\n elsif r[1] < divisors.first \n end\n i = bsearch(divisors, r[1])\n i -= 1\n if divisors[i] < r[0]\n ret << -1\n else\n ret << divisors[i]\n end\nend\nret.each{|i| puts i}\n"}], "src_uid": "6551be8f4000da2288bf835169662aa2"} {"nl": {"description": "You are given an array $$$a_1, a_2, \\dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i < j$$$ and $$$a_i \\cdot a_j = i + j$$$.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \\leq n \\leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\leq a_i \\leq 2 \\cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i < j$$$ and $$$a_i \\cdot a_j = i + j$$$.", "sample_inputs": ["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"], "sample_outputs": ["1\n1\n3"], "notes": "NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \\cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$."}, "positive_code": [{"source_code": "t = gets.to_i\r\nt.times{ |x|\r\n n = gets.to_i\r\n res = 0\r\n m = n + n -1\r\n arr = gets.chomp.split(\" \").map(&:to_i)\r\n hs = Hash[(1..arr.length).zip arr].lazy.\r\n sort_by{|i, v| v}\r\n #p hs\r\n (0..m).each{ |i|\r\n j = i+1\r\n e = m/hs[i][1]\r\n #p j\r\n #p n\r\n #p j >= n\r\n break if j >= n or hs[j][1] > e\r\n while j < n and hs[j][1] <= e\r\n res += 1 if hs[i][1]*hs[j][1] == hs[i][0] + hs[j][0]\r\n\r\n j += 1\r\n end\r\n }\r\n puts res\r\n}"}, {"source_code": "gets.to_i.times do\n\n n = gets.to_i\n arr = gets.chomp.split.map(&:to_i)\n\n res = 0\n m = n + n -1\n hs = Hash[(1..arr.length).zip arr].lazy.\n sort_by{|i, v| v}\n\n (0..m).each do |i|\n j = i+1\n e = m/hs[i][1]\n break if j >= n or hs[j][1] > e\n while j < n and hs[j][1] <= e\n res += 1 if hs[i][1]*hs[j][1] == hs[i][0] + hs[j][0]\n \n j += 1\n end\n end\n puts res\nend\n\n \t \t \t \t \t\t \t\t\t \t \t"}], "negative_code": [], "src_uid": "0ce05499cd28f0825580ff48dae9e7a9"} {"nl": {"description": "You have been blessed as a child of Omkar. To express your gratitude, please solve this problem for Omkar!An array $$$a$$$ of length $$$n$$$ is called complete if all elements are positive and don't exceed $$$1000$$$, and for all indices $$$x$$$,$$$y$$$,$$$z$$$ ($$$1 \\leq x,y,z \\leq n$$$), $$$a_{x}+a_{y} \\neq a_{z}$$$ (not necessarily distinct).You are given one integer $$$n$$$. Please find any complete array of length $$$n$$$. It is guaranteed that under given constraints such array exists.", "input_spec": "Each test contains multiple test cases. The first line contains $$$t$$$ ($$$1 \\le t \\le 1000$$$)  — the number of test cases. Description of the test cases follows. The only line of each test case contains one integer $$$n$$$ ($$$1 \\leq n \\leq 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$1000$$$.", "output_spec": "For each test case, print a complete array on a single line. All elements have to be integers between $$$1$$$ and $$$1000$$$ and for all indices $$$x$$$,$$$y$$$,$$$z$$$ ($$$1 \\leq x,y,z \\leq n$$$) (not necessarily distinct), $$$a_{x}+a_{y} \\neq a_{z}$$$ must hold. If multiple solutions exist, you may print any.", "sample_inputs": ["2\n5\n4"], "sample_outputs": ["1 5 3 77 12\n384 384 44 44"], "notes": "NoteIt can be shown that the outputs above are valid for each test case. For example, $$$44+44 \\neq 384$$$.Below are some examples of arrays that are NOT complete for the 1st test case:$$$[1,2,3,4,5]$$$ Notice that $$$a_{1}+a_{2} = a_{3}$$$.$$$[1,3000,1,300,1]$$$ Notice that $$$a_{2} = 3000 > 1000$$$."}, "positive_code": [{"source_code": "t = gets.chomp.to_i\n#p n\nt.times do \n n = gets.chomp.to_i\n i = 0\n until n == i\n a = 1\n print a\n if i != n- 1\n print \" \"\n end\n i += 1\n end\n print \"\\n\"\nend\n"}, {"source_code": "t = gets.to_i \nt.times do \n\tn = gets.to_i \n\tputs \"1 \"*n \nend"}, {"source_code": "gets.to_i.times do\n res = \"\"\n gets.to_i.times do\n res += \"1 \"\n end\n puts res\nend"}, {"source_code": "gets.to_i.times { puts ([1] * gets.to_i).join(' ') }\n"}], "negative_code": [], "src_uid": "f82058f6ba3ce0da15a5ce059674af35"} {"nl": {"description": "The Little Elephant has got a problem — somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array.The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements.Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself.", "input_spec": "The first line contains a single integer n (2 ≤ n ≤ 105) — the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, — array a. Note that the elements of the array are not necessarily distinct numbers.", "output_spec": "In a single line print \"YES\" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and \"NO\" (without the quotes) otherwise.", "sample_inputs": ["2\n1 2", "3\n3 2 1", "4\n4 3 2 1"], "sample_outputs": ["YES", "YES", "NO"], "notes": "NoteIn the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is \"YES\".In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is \"YES\".In the third sample we can't sort the array in more than one swap operation, so the answer is \"NO\"."}, "positive_code": [{"source_code": "n = gets.to_i\na = gets.chomp.split.map(&:to_i)\nb = a.sort\nif a == b\n puts \"YES\"\n exit\nend\ncount = 0\nn.times do |i|\n count +=1 if a[i] != b[i]\nend\nputs count == 2 ? \"YES\" : \"NO\"\n"}, {"source_code": "n = gets.to_i\na = gets.split.map &:to_i\nb = a.sort\ns = 0\na.zip(b).each do |x, y|\n s += x != y ? 1 : 0\nend\nputs s > 2 ? \"NO\" : \"YES\""}, {"source_code": "n = gets.to_i\nar = gets.split.map(&:to_i)\nbr = ar.sort\n\ncnt = 0\nn.times do |i|\n\tcnt += 1 if ar[i] != br[i]\nend\n\nputs (cnt >2 ? \"NO\":\"YES\")\n\n"}, {"source_code": "STDIN.readline.to_i; numbers = STDIN.readline.split(' ').map { |x| x.to_i }\nsorted = numbers.sort\n\ncount = 0\nfor i in 0..sorted.length\n count += 1 if sorted[i] != numbers[i]\nend\n\nif count <= 2; puts \"YES\"; else puts \"NO\"; end\n\n"}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\nb=a.sort\npos=0\nn.times{|i|pos+=1 unless a[i]==b[i]}\nputs pos<3 ? \"YES\" : \"NO\""}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nb = a.sort\ncnt = 0\nfor i in 0..n - 1 do\n cnt += 1 if a[i] != b[i]\nend\nputs \"YES\" if cnt <= 2\nputs \"NO\" if cnt > 2\n"}, {"source_code": "#!/usr/bin/ruby\n# coding: utf-8\n\nn = $stdin.gets.to_i\na = $stdin.gets.chomp.split.map(&:to_i)\n\nc = a.zip(a.sort).count { |x, y| x != y }\nputs (c <= 2 ? 'YES' : 'NO')\n"}, {"source_code": "n = gets.to_i\narr = gets.split(\" \").map(&:to_i)\nc = 0\nsorted = arr.sort\narr.each_with_index do |a,i|\n\tc += 1 if a != sorted[i]\nend\nputs c <= 2 ? \"YES\" : \"NO\""}, {"source_code": "n = gets.to_i\na = gets.split.collect{|i| i.to_i}\nb = a.sort\ncnt = 0\nfor i in 0...n\n if a[i] != b[i]\n cnt += 1\n end\nend\nif cnt <= 2\n puts 'YES'\nelse\n puts 'NO'\nend\n"}, {"source_code": "#!/usr/bin/ruby\n# coding: utf-8\n\nn = $stdin.gets.to_i\na = $stdin.gets.chomp.split.map(&:to_i)\n\nc = a.zip(a.sort).count { |x, y| x != y }\nputs (c <= 2 ? 'YES' : 'NO')"}], "negative_code": [{"source_code": "n = gets.to_i\narr = gets.split(\" \").map(&:to_i)\nc = 0\narr.each_with_index do |a,i|\n\tc += 1 if a != (i + 1 )\nend\nputs c <= 2 ? \"YES\" : \"NO\""}], "src_uid": "541fde3a3c40926cbd1edb6017b83e52"} {"nl": {"description": "Pig is visiting a friend.Pig's house is located at point 0, and his friend's house is located at point m on an axis.Pig can use teleports to move along the axis.To use a teleport, Pig should come to a certain point (where the teleport is located) and choose where to move: for each teleport there is the rightmost point it can move Pig to, this point is known as the limit of the teleport.Formally, a teleport located at point x with limit y can move Pig from point x to any point within the segment [x; y], including the bounds. Determine if Pig can visit the friend using teleports only, or he should use his car.", "input_spec": "The first line contains two integers n and m (1 ≤ n ≤ 100, 1 ≤ m ≤ 100) — the number of teleports and the location of the friend's house. The next n lines contain information about teleports. The i-th of these lines contains two integers ai and bi (0 ≤ ai ≤ bi ≤ m), where ai is the location of the i-th teleport, and bi is its limit. It is guaranteed that ai ≥ ai - 1 for every i (2 ≤ i ≤ n).", "output_spec": "Print \"YES\" if there is a path from Pig's house to his friend's house that uses only teleports, and \"NO\" otherwise. You can print each letter in arbitrary case (upper or lower).", "sample_inputs": ["3 5\n0 2\n2 4\n3 5", "3 7\n0 4\n2 5\n6 7"], "sample_outputs": ["YES", "NO"], "notes": "NoteThe first example is shown on the picture below: Pig can use the first teleport from his house (point 0) to reach point 2, then using the second teleport go from point 2 to point 3, then using the third teleport go from point 3 to point 5, where his friend lives.The second example is shown on the picture below: You can see that there is no path from Pig's house to his friend's house that uses only teleports."}, "positive_code": [{"source_code": "n,m=gets.split.map &:to_i\nlast=0\nn.times{\n\ta,b=gets.split.map &:to_i\n\tif a>last\n\t\tputs :NO\n\t\texit\n\telse\n\t\tlast=[b,last].max\n\t\tif last>=m\n\t\t\tputs :YES\n\t\t\texit\n\t\tend\n\tend\n}\nputs :NO"}, {"source_code": "n, m = gets.chomp.split.map(&:to_i)\npos = 0\n\nfor i in 1..n\n st, en = gets.chomp.split.map(&:to_i)\n if st > pos\n puts \"NO\"\n exit 0\n else\n pos = [pos, en].max\n end\nend\n\nif pos >= m\n puts \"YES\"\nelse\n puts \"NO\"\nend"}, {"source_code": "#!/bin/ruby\n\nn, m = gets.strip.split(' ')\nn = n.to_i\nm = m.to_i\nvisiting = 'NO'\nps = 0\npd = 0\nfor i in (1..n)\n s, d = gets.strip.split(' ')\n s = s.to_i\n d = d.to_i\n if s.between? ps, pd\n ps = s\n pd = d if pd <= d\n if m.between? s, d\n visiting = 'YES'\n break\n end \n else\n visiting = 'NO'\n break\n end \nend\nputs visiting"}, {"source_code": "n, m = gets.strip.split(' ').map(&:to_i)\n\na = []\n\nn.times do \n\ta << gets.strip.split(' ').map(&:to_i)\nend\n\nmax_till_now = 0\n\nmp = Hash.new(nil)\nbool = true\na.each do |x|\n\tmp[x[0]] = true\n\tmp[x[1]] = true\n\tif x[0] > max_till_now\n\t\tbool = false\n\t\tbreak\n\telse\n\t\tmax_till_now = [x[1], max_till_now].max \n\tend\nend\n\nif !bool\n\tputs 'NO'\nelse\n\tif mp[m]\n\t\tputs 'YES'\n\telse\n\t\tputs 'NO'\n\tend\nend"}], "negative_code": [{"source_code": "#!/bin/ruby\n\nn, m = gets.strip.split(' ')\nn = n.to_i\nm = m.to_i\nvisiting = 'NO'\nps = 0\npd = 0\nfor i in (1..n)\n s, d = gets.strip.split(' ')\n s = s.to_i\n d = d.to_i\n if s.between? ps, pd\n ps = s\n pd = d\n if m.between? s, d\n visting = 'YES'\n break\n end \n else\n visting = 'NO'\n break\n end \nend\nputs visting"}, {"source_code": "#!/bin/ruby\n\nn, m = gets.strip.split(' ')\nn = n.to_i\nm = m.to_i\nvisiting = 'NO'\nps = 0\npd = 0\nfor i in (1..n)\n s, d = gets.strip.split(' ')\n s = s.to_i\n d = d.to_i\n if s.between? ps, pd\n ps = s\n pd = d\n if m.between? s, d\n visiting = 'YES'\n break\n end \n else\n visiting = 'NO'\n break\n end \nend\nputs visiting"}, {"source_code": "#!/bin/ruby\n\nn, m = gets.strip.split(' ')\nn = n.to_i\nm = m.to_i\nps = 0\npd = 0\nfor i in (1..n)\n s, d = gets.strip.split(' ')\n s = s.to_i\n d = d.to_i\n if s.between? ps, pd\n ps = s\n pd = d\n if m.between? s, d\n visting = 'YES'\n break\n end \n else\n visting = 'NO'\n break\n end \nend\nputs visting"}, {"source_code": "#!/bin/ruby\n\nn, m = gets.strip.split(' ')\nn = n.to_i\nm = m.to_i\nps = 0\npd = 0\nfor i in (1..n)\n s, d = gets.strip.split(' ')\n s = s.to_i\n d = d.to_i\n if pd.between? s, d\n pd = d\n if m.between? s, d\n visting = 'YES'\n break\n end \n else\n visting = 'NO'\n break\n end \nend\nputs visting"}, {"source_code": "n, m = gets.strip.split(' ').map(&:to_i)\n\na = []\n\nn.times do \n\ta << gets.strip.split(' ').map(&:to_i)\nend\n\nmax_till_now = a[0][0]\n\nmp = Hash.new(nil)\nbool = true\na.each do |x|\n\tmp[x[0]] = true\n\tmp[x[1]] = true\n\tif x[0] > max_till_now\n\t\tbool = false\n\t\tbreak\n\telse\n\t\tmax_till_now = [x[1], max_till_now].max \n\tend\nend\n\nif !bool\n\tputs 'NO'\nelse\n\tif mp[m]\n\t\tputs 'YES'\n\telse\n\t\tputs 'NO'\n\tend\nend"}, {"source_code": "n, m = gets.strip.split(' ').map(&:to_i)\n\na = []\n\nn.times do \n\ta << gets.strip.split(' ').map(&:to_i)\nend\n\nmax_till_now = a[0][0]\n\nmp = Hash.new(0)\nbool = true\na.each do |x|\n\tmp[x[0]] = true\n\tmp[x[1]] = true\n\tif x[0] > max_till_now\n\t\tbool = false\n\t\tbreak\n\telse\n\t\tmax_till_now = [x[1], max_till_now].max \n\tend\nend\n\n\nif !bool\n\tputs 'NO'\nelse\n\tif mp[m]\n\t\tputs 'YES'\n\telse\n\t\tputs 'NO'\n\tend\nend\n"}, {"source_code": "n, m = gets.strip.split(' ').map(&:to_i)\n\na = []\n\nn.times do \n\ta << gets.strip.split(' ').map(&:to_i)\nend\n\nmax_till_now = 0\n\nmp = Hash.new(0)\nbool = true\na.each do |x|\n\tmp[x[0]] = true\n\tmp[x[1]] = true\n\tif x[0] > max_till_now\n\t\tbool = false\n\t\tbreak\n\telse\n\t\tmax_till_now = [x[1], max_till_now].max \n\tend\nend\n\n\nif !bool\n\tputs 'NO'\nelse\n\tif mp[m]\n\t\tputs 'YES'\n\telse\n\t\tputs 'NO'\n\tend\nend\n"}], "src_uid": "d646834d58c519d5e9c0545df2ca4be2"} {"nl": {"description": "We're giving away nice huge bags containing number tiles! A bag we want to present to you contains $$$n$$$ tiles. Each of them has a single number written on it — either $$$1$$$ or $$$2$$$.However, there is one condition you must fulfill in order to receive the prize. You will need to put all the tiles from the bag in a sequence, in any order you wish. We will then compute the sums of all prefixes in the sequence, and then count how many of these sums are prime numbers. If you want to keep the prize, you will need to maximize the number of primes you get.Can you win the prize? Hurry up, the bags are waiting!", "input_spec": "The first line of the input contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 200\\,000$$$) — the number of number tiles in the bag. The following line contains $$$n$$$ space-separated integers $$$a_1, a_2, \\dots, a_n$$$ ($$$a_i \\in \\{1, 2\\}$$$) — the values written on the tiles.", "output_spec": "Output a permutation $$$b_1, b_2, \\dots, b_n$$$ of the input sequence $$$(a_1, a_2, \\dots, a_n)$$$ maximizing the number of the prefix sums being prime numbers. If there are multiple optimal permutations, output any.", "sample_inputs": ["5\n1 2 1 2 1", "9\n1 1 2 1 1 1 2 1 1"], "sample_outputs": ["1 1 1 2 2", "1 1 1 2 1 1 1 2 1"], "notes": "NoteThe first solution produces the prefix sums $$$1, \\mathbf{\\color{blue}{2}}, \\mathbf{\\color{blue}{3}}, \\mathbf{\\color{blue}{5}}, \\mathbf{\\color{blue}{7}}$$$ (four primes constructed), while the prefix sums in the second solution are $$$1, \\mathbf{\\color{blue}{2}}, \\mathbf{\\color{blue}{3}}, \\mathbf{\\color{blue}{5}}, 6, \\mathbf{\\color{blue}{7}}, 8, 10, \\mathbf{\\color{blue}{11}}$$$ (five primes). Primes are marked bold and blue. In each of these cases, the number of produced primes is maximum possible."}, "positive_code": [{"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\ncnt = [0,0] # 1,2\na.each do |v|\n cnt[v-1] += 1\nend\n\nif cnt[0] == 0\n print \"2\"\n (cnt[1]-1).times do\n print \" 2\"\n end\nelsif cnt[1] == 0\n print \"1\"\n (cnt[0]-1).times do\n print \" 1\"\n end\nelsif cnt[0] % 2 == 1\n print \"2\"\n cnt[0].times do \n print \" 1\"\n end\n (cnt[1]-1).times do\n print \" 2\"\n end\nelsif cnt[0] % 2 == 0\n print \"2 1\"\n (cnt[1]-1).times do\n print \" 2\"\n end\n (cnt[0]-1).times do \n print \" 1\"\n end\nend\n"}], "negative_code": [], "src_uid": "14593b565193607dff8b6b25bf51a661"} {"nl": {"description": "In Summer Informatics School, if a student doesn't behave well, teachers make a hole in his badge. And today one of the teachers caught a group of $$$n$$$ students doing yet another trick. Let's assume that all these students are numbered from $$$1$$$ to $$$n$$$. The teacher came to student $$$a$$$ and put a hole in his badge. The student, however, claimed that the main culprit is some other student $$$p_a$$$.After that, the teacher came to student $$$p_a$$$ and made a hole in his badge as well. The student in reply said that the main culprit was student $$$p_{p_a}$$$.This process went on for a while, but, since the number of students was finite, eventually the teacher came to the student, who already had a hole in his badge.After that, the teacher put a second hole in the student's badge and decided that he is done with this process, and went to the sauna.You don't know the first student who was caught by the teacher. However, you know all the numbers $$$p_i$$$. Your task is to find out for every student $$$a$$$, who would be the student with two holes in the badge if the first caught student was $$$a$$$.", "input_spec": "The first line of the input contains the only integer $$$n$$$ ($$$1 \\le n \\le 1000$$$) — the number of the naughty students. The second line contains $$$n$$$ integers $$$p_1$$$, ..., $$$p_n$$$ ($$$1 \\le p_i \\le n$$$), where $$$p_i$$$ indicates the student who was reported to the teacher by student $$$i$$$.", "output_spec": "For every student $$$a$$$ from $$$1$$$ to $$$n$$$ print which student would receive two holes in the badge, if $$$a$$$ was the first student caught by the teacher.", "sample_inputs": ["3\n2 3 2", "3\n1 2 3"], "sample_outputs": ["2 2 3", "1 2 3"], "notes": "NoteThe picture corresponds to the first example test case. When $$$a = 1$$$, the teacher comes to students $$$1$$$, $$$2$$$, $$$3$$$, $$$2$$$, in this order, and the student $$$2$$$ is the one who receives a second hole in his badge.When $$$a = 2$$$, the teacher comes to students $$$2$$$, $$$3$$$, $$$2$$$, and the student $$$2$$$ gets a second hole in his badge. When $$$a = 3$$$, the teacher will visit students $$$3$$$, $$$2$$$, $$$3$$$ with student $$$3$$$ getting a second hole in his badge.For the second example test case it's clear that no matter with whom the teacher starts, that student would be the one who gets the second hole in his badge."}, "positive_code": [{"source_code": "N = gets.to_i\nps = [0] + gets.split.map(&:to_i)\n\nhole = Array.new(N+1)\nans = []\n(1..N).each do |i|\n (1..N).each do |j|\n hole[j] = nil\n end\n loop do\n hole[i] = true\n i = ps[i]\n if hole[i]\n ans.push(i)\n break\n end\n end\nend\nputs ans.join(' ')"}], "negative_code": [], "src_uid": "c0abbbf1cf6c8ec11e942cdaaf01ad7c"} {"nl": {"description": "There was a string $$$s$$$ which was supposed to be encrypted. For this reason, all $$$26$$$ lowercase English letters were arranged in a circle in some order, afterwards, each letter in $$$s$$$ was replaced with the one that follows in clockwise order, in that way the string $$$t$$$ was obtained. You are given a string $$$t$$$. Determine the lexicographically smallest string $$$s$$$ that could be a prototype of the given string $$$t$$$.A string $$$a$$$ is lexicographically smaller than a string $$$b$$$ of the same length if and only if: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ has a letter, that appears earlier in the alphabet than the corresponding letter in $$$b$$$. ", "input_spec": "The first line of the input contains a single integer $$$t$$$ ($$$1 \\le t \\le 3 \\cdot 10^4$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains one integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$) — the length of the string $$$t$$$. The next line contains the string $$$t$$$ of the length $$$n$$$, containing lowercase English letters. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, output a single line containing the lexicographically smallest string $$$s$$$ which could be a prototype of $$$t$$$.", "sample_inputs": ["5\n\n1\n\na\n\n2\n\nba\n\n10\n\ncodeforces\n\n26\n\nabcdefghijklmnopqrstuvwxyz\n\n26\n\nabcdefghijklmnopqrstuvwxzy"], "sample_outputs": ["b\nac\nabcdebfadg\nbcdefghijklmnopqrstuvwxyza\nbcdefghijklmnopqrstuvwxyaz"], "notes": "NoteIn the first test case, we couldn't have the string \"a\", since the letter a would transit to itself. Lexicographically the second string \"b\" is suitable as an answer.In the second test case, the string \"aa\" is not suitable, since a would transit to itself. \"ab\" is not suitable, since the circle would be closed with $$$2$$$ letters, but it must contain all $$$26$$$. The next string \"ac\" is suitable.Below you can see the schemes for the first three test cases. The non-involved letters are skipped, they can be arbitrary placed in the gaps. "}, "positive_code": [{"source_code": "main = -> {\r\n alphabet = \"abcdefghijklmnopqrstuvwxyz\"\r\n t = gets.to_i\r\n t.times do\r\n n = gets.to_i\r\n s = gets.chomp.each_char.map { |c| c.ord - ?a.ord }\r\n\r\n previous = [nil] * 26\r\n used_to = [false] * 26\r\n count = 0\r\n t = \"\"\r\n s.each do |c|\r\n unless previous[c]\r\n to = (0 ... 26).find { |d|\r\n next if d == c || used_to[d]\r\n ok = true\r\n k = d\r\n ok = false if k == c while (k = previous[k])\r\n ok\r\n }\r\n used_to[to] = true\r\n previous[c] = to\r\n count += 1\r\n if count == 25\r\n from = (0 ... 26).find { |i| previous[i].nil? }\r\n to = used_to.index(false)\r\n used_to[to] = true\r\n previous[from] = to\r\n count += 1\r\n end\r\n end\r\n t << alphabet[previous[c]]\r\n end\r\n puts t\r\n end\r\n}\r\n\r\nmain.call"}], "negative_code": [], "src_uid": "faf5ec909f5147c11454e1ecb9c372ee"} {"nl": {"description": "Berland, 2016. The exchange rate of currency you all know against the burle has increased so much that to simplify the calculations, its fractional part was neglected and the exchange rate is now assumed to be an integer.Reliable sources have informed the financier Anton of some information about the exchange rate of currency you all know against the burle for tomorrow. Now Anton knows that tomorrow the exchange rate will be an even number, which can be obtained from the present rate by swapping exactly two distinct digits in it. Of all the possible values that meet these conditions, the exchange rate for tomorrow will be the maximum possible. It is guaranteed that today the exchange rate is an odd positive integer n. Help Anton to determine the exchange rate of currency you all know for tomorrow!", "input_spec": "The first line contains an odd positive integer n — the exchange rate of currency you all know for today. The length of number n's representation is within range from 2 to 105, inclusive. The representation of n doesn't contain any leading zeroes.", "output_spec": "If the information about tomorrow's exchange rate is inconsistent, that is, there is no integer that meets the condition, print  - 1. Otherwise, print the exchange rate of currency you all know against the burle for tomorrow. This should be the maximum possible number of those that are even and that are obtained from today's exchange rate by swapping exactly two digits. Exchange rate representation should not contain leading zeroes.", "sample_inputs": ["527", "4573", "1357997531"], "sample_outputs": ["572", "3574", "-1"], "notes": null}, "positive_code": [{"source_code": "n = gets.chomp\nl = n.size\n\n( l - 1 ).times do | i |\n\td = n[i].to_i\n\tif d.odd?\n\t\tnext\n\tend\n\n\tif d < n[ l - 1 ].to_i\n\t\ts = String.new( n )\n\t\ts[i], s[ l - 1 ] = s[ l - 1 ], s[i]\n\t\tputs s\n\t\texit\n\tend\nend\n\n( l - 2 ).downto( 0 ) do | i |\n\td = n[i].to_i\n\tif d.odd?\n\t\tnext\n\tend\n\n\ts = String.new( n )\n\ts[i], s[ l - 1 ] = s[ l - 1 ], s[i]\n\tputs s\n\texit\nend\n\nputs -1\n"}, {"source_code": "n = gets.strip\nif n.chars.all? {|x| x.to_i.odd?}\n\tputs -1\nelse\n\tm = n.chars.select{|x| x.to_i.even?}\n\ti = m.min > n[-1] ? n.rindex(m[-1]) : n.index(m.find{|x| x < n[-1]})\n\tn[i], n[-1] = n[-1] , n[i]\n\tputs n\nend\n\n"}, {"source_code": "a=gets.chomp.split('').map(&:to_i)\nn=a.length\ni,max,x=0,-1,-1\nwhile i=0\n\te=n[i].to_i\n\tif e%2==0 && e>last1 then\n\t\tp1=i\n\t\tbreak\n\tend\n\ti-=1\nend\n\nif p1!=-1 then\n\tn[p1],n[m]=n[m],n[p1]\n\tans=n if ansmax\n\t\t\t\t\tmax=m\n\t\t\t\tend\n\t\t\telse\n\t\t\t\tfound=true\n\t\t\t\tif max==-1 || max>m\n\t\t\t\t\tmax=m\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\t\t$~=nil\n\t\ti+=2\n\tend\n\t#puts max\n\tputs s[0...max]+s[-1]+s[max+1...-1]+s[max]\nend\ns=gets.chomp.to_s\ncheck(s)"}, {"source_code": "# coding: utf-8\n\n# 한 자리의 숫자(반드시 짝수) 와 마지막 자리(반드시 홀수)를 교체했을 때\n# 가장 큰 숫자를 만들어라\n# 단 짝수가 없어서 짝수를 못만들면 -1\n\n# Time limit: 0.5sec\n\n# ex) 2431 라면\n# 1432 vs 2134 이면 당연히 후자가 더 크겠지\n# ex) 2439 라면\n# 9432 vs 2934 이면 당연히 전자가 더 크지\n# 즉 교환하려는 홀수가 타겟보다 크다면 최대한 앞자리에 있는게 유리하고(숫자가 커짐)\n# 교환하려는 홀수가 타겟보다 작다면 최대한 뒷자리에서 바꾸는게 유리하다\neven_indexes = []\nar = []\ngets.chomp.split(//).each_with_index { |i, index|\n i = i.to_i\n if i.even?\n even_indexes << index\n end\n ar << i\n}\n\nif even_indexes.empty?\n puts \"-1\"\nelse\n exchanged = false\n for i in even_indexes\n if ar[i] < ar[-1]\n ar[i], ar[-1] = ar[-1], ar[i]\n exchanged = true\n break\n end\n end\n\n if !exchanged\n last_even_index = even_indexes.last\n ar[last_even_index], ar[-1] = ar[-1], ar[last_even_index]\n end\n\n puts ar.join('')\nend\n"}, {"source_code": "copy = gets.chomp\nans = -1\nlast = copy[-1].to_i\n\n# if copy.length < 20\n# (0..(copy.length - 1)) do |i|\n# \tv = copy[i].to_i\n# \tif v.even? && v < last\t\t\n# \t\tcopy[i],copy[-1] = copy[-1],copy[i]\n# \t\tans = copy.to_i\n# \t\tbreak\n# \tend\n# end\n# if ans == -1\n# \t((copy.length - 1).downto(0)).each do |i|\n# \t\tv = copy[i].to_i\n# \t\tif v.even? && v > last\n# \t\t\tcopy[i],copy[-1] = copy[-1],copy[i]\n# \t\t\tans = copy.to_i\n# \t\t\tbreak\n# \t\tend\n# \tend\n# end\n# even_nos = copy.each_char.with_index.map{|k,i| [k.to_i,i] if k.to_i.even?}.compact\ncopy.each_char.with_index do |e,i|\n\tv = e.to_i\n\tnext if v.to_i.odd?\n\n\tif v < last\n\t\tans = i\n\t\tbreak\n\telse\n\t\tans = i\n\tend\nend\nif ans != -1\n\tcopy[ans],copy[-1] = copy[-1],copy[ans]\n\tans = copy\nend\nputs ans\n# else\n# \tcopy.each_char.with_index do |e,i|\n# \tv = e.to_i\n# \tnext if v.to_i.odd?\n\n# \tif v < last\n# \t\tans = i\n# \t\tbreak\n# \telse\n# \t\tans = i\n# \tend\n# \tend\n# \t# \n# \tputs copy[-1]\n# end"}, {"source_code": "copy = gets.chomp\nans = -1\nlast = copy[-1].to_i\n\n# if copy.length < 20\n# (0..(copy.length - 1)) do |i|\n# \tv = copy[i].to_i\n# \tif v.even? && v < last\t\t\n# \t\tcopy[i],copy[-1] = copy[-1],copy[i]\n# \t\tans = copy.to_i\n# \t\tbreak\n# \tend\n# end\n# if ans == -1\n# \t((copy.length - 1).downto(0)).each do |i|\n# \t\tv = copy[i].to_i\n# \t\tif v.even? && v > last\n# \t\t\tcopy[i],copy[-1] = copy[-1],copy[i]\n# \t\t\tans = copy.to_i\n# \t\t\tbreak\n# \t\tend\n# \tend\n# end\n# even_nos = copy.each_char.with_index.map{|k,i| [k.to_i,i] if k.to_i.even?}.compact\ncopy.each_char.with_index do |e,i|\n\tv = e.to_i\n\tnext if v.to_i.odd?\n\n\tif v < last\n\t\tans = i\n\t\tbreak\n\telse\n\t\tans = i\n\tend\nend\nif ans != -1\n\tcopy[ans],copy[-1] = copy[-1],copy[ans]\n\tans = copy\nend\nputs ans\n# else\n\n# \tcopy.each_char.with_index do |e,i|\n# \tv = e.to_i\n# \tnext if v.to_i.odd?\n\n# \tif v < last\n# \t\tans = i\n# \t\tbreak\n# \telse\n# \t\tans = i\n# \tend\n# \tend\n# \t# \n# \tputs copy[-1]\n# end"}, {"source_code": "n = gets.chomp\nif n.each_char.all?{|x| x.to_i.odd?}\n puts -1\nelse\n m = []\n n.each_char{|x| m << x if x.to_i.even?}\n i = if m.all?{|x| x > n[-1]}\n n.rindex(m[-1])\n else\n n.index(m.find{|x| x < n[-1]})\n end\n n[i], n[-1] = n[-1], n[i]\n puts n\nend\n"}, {"source_code": "n = gets.chomp\nif n.chars.all?{|x| x.to_i.odd?}\n puts -1\nelse\n m = n.chars.select{|x| x.to_i.even?}\n i = if m.min > n[-1]\n n.rindex(m[-1])\n else\n n.index(m.find{|x| x < n[-1]})\n end\n n[i], n[-1] = n[-1], n[i]\n puts n\nend\n"}, {"source_code": "a=Array.new\nstr=gets.to_s\na=str.split(\"\")\nh=-1\nfor i in 0..a.length-2\n# puts a.length\n if a[i].to_i%2==0\n h=i\n end\n if a[i].to_i%2==0 and a[i].to_i max)\n\t\tmax = current\n\tend\n\ttemp = numbers[even_index]\n\tnumbers[even_index] = temp\n\tnumbers[last_index] = temp\nend\nif even.size == 0\n\tputs -1\nelse\n\tputs max\nend\n\n"}, {"source_code": "n = gets.strip\nnumbers = n.split(\"\").map {|i| i.to_i}\nnumbers2 = numbers.map do |e| e end\neven = numbers.select {|n| n.even?}\nmax = 0\ni = -1\nj = -1\nlast_index = numbers.size - 1\n\nnumbers.each_with_index do |number, index|\n\tif (number.even? && number < numbers[last_index])\n\t\ti = index\n\t\tbreak\n\tend\nend\n\nnumbers.reverse.each_with_index do |number, index|\n\tif (number.even? && number >= numbers[last_index])\n\t\tj = numbers.size - index - 1\n\t\tbreak\n\tend\nend\nputs j\nif even.size == 0\n\tputs -1\nelsif (i != -1)\n\ttemp = numbers[last_index]\n\tnumbers[last_index] = numbers[i]\n\tnumbers[i] = temp\n\tputs numbers.join(\"\").to_i\nelsif (j != -1)\n\ttemp = numbers[last_index]\n\tnumbers[last_index] = numbers[j]\n\tnumbers[j] = temp\n\tputs numbers.join(\"\").to_i\nend\n\n"}, {"source_code": "a=gets.chomp\nt=-1\nmi=-10\n(a.length-2).downto(0) do |i|\nif a[i].to_i%2==0 && a[a.length-1].to_i-a[i].to_i>=mi \nt=i\nmi=a[a.length-1].to_i-a[i].to_i\nend\nend\n\nif t==-1\nputs \"-1\"\nelse\nmi=a[a.length-1]\na[a.length-1]=a[t]\na[t]=mi\nputs a\nend\n"}, {"source_code": "a=gets.chomp\nt=-1\nmi=-10\n(a.length-2).downto(0) do |i|\nif a[i].to_i%2==0 && a[a.length-1].to_i-a[i].to_i>mi \nt=i\nmi=a[a.length-1].to_i-a[i].to_i\nend\nend\n\nif t==-1\nputs \"-1\"\nelse\nmi=a[a.length-1]\na[a.length-1]=a[t]\na[t]=mi\nputs a\nend\n"}, {"source_code": "n=gets.chomp\nm=n.size-1\n\nans=-1\ni=0\n\nh={}\nh2={}\n[\"0\",\"2\",\"4\",\"6\",\"8\"].each{|e| h[e]=0}\n[\"0\",\"2\",\"4\",\"6\",\"8\"].each{|e| h2[e]=0}\nn.chars{|e|\n\tif e.to_i%2==0 && e=0\n\te=n[i]\n\tif e.to_i%2==0 && e>n[m] && h2[e]==0 then\n\t\th2[e]==1\n\t\tn[i],n[m]=n[m],n[i]\n\t\tif n.to_i%2==0 && n[0]!=\"0\" then\n\t\t\tans=n.to_i if ansmax\n\t\t\t\t\tmax=m\n\t\t\t\tend\n\t\t\telse\n\t\t\t\tfound=true\n\t\t\t\tif max==-1 || max>m\n\t\t\t\t\tmax=m\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\t\t$~=nil\n\t\ti+=2\n\tend\n\tputs s[0...max]+s[-1]+s[max+1...-1]+s[max]\nend\ns=gets.chomp.to_s\ncheck(s)"}, {"source_code": "def check(s)\n\tunless s=~/[02468]/\n\t\treturn \"-1\"\n\tend\n\ti=0\n\tflag=false\n\thold=s[-1].to_i\n\twhile true && i<=8\n\t\tif imax\n\t\t\t\t\tmax=m\n\t\t\t\tend\n\t\t\telse\n\t\t\t\tfound=true\n\t\t\t\tif max==-1 || max>m\n\t\t\t\t\tmax=m\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\t\t$~=nil\n\t\ti+=2\n\tend\n\t#puts max\n\tputs s[0...max]+s[-1]+s[max+1...-1]+s[max]\nend\ns=gets.chomp.to_s\ncheck(s)"}, {"source_code": "def check(s)\n\tunless s=~/[02468]/\n\t\treturn \"-1\"\n\tend\n\ti=0\n\ts=s.reverse\n\twhile true && i<=8\n\t\tm=s=~/#{i}/\n\t\tif $~\n\t\t\ty=s[m]+s[1...m]+s[0]+s[m+1..-1]\n\t\t\treturn y.reverse\n\t\tend\n\t\t$~=nil\n\t\ti+=2\n\tend\nend\ns=gets.chomp.to_s\nhold=check(s)\nif hold!='-1'\n\tputs hold\nelse\n\tputs \"-1\"\nend"}, {"source_code": "copy = gets.chomp\nans = -1\nlast = copy[-1].to_i\nif copy.length < 20\n# (0..(copy.length - 1)) do |i|\n# \tv = copy[i].to_i\n# \tif v.even? && v < last\t\t\n# \t\tcopy[i],copy[-1] = copy[-1],copy[i]\n# \t\tans = copy.to_i\n# \t\tbreak\n# \tend\n# end\n# if ans == -1\n# \t((copy.length - 1).downto(0)).each do |i|\n# \t\tv = copy[i].to_i\n# \t\tif v.even? && v > last\n# \t\t\tcopy[i],copy[-1] = copy[-1],copy[i]\n# \t\t\tans = copy.to_i\n# \t\t\tbreak\n# \t\tend\n# \tend\n# end\n# even_nos = copy.each_char.with_index.map{|k,i| [k.to_i,i] if k.to_i.even?}.compact\ncopy.each_char.with_index do |e,i|\n\tv = e.to_i\n\tnext if v.to_i.odd?\n\n\tif v < last\n\t\tans = i\n\t\tbreak\n\telse\n\t\tans = i\n\tend\nend\nif ans != -1\n\tcopy[ans],copy[-1] = copy[-1],copy[ans]\n\tans = copy.to_i\nend\nputs ans\nelse\n\tputs copy[-1]\nend"}, {"source_code": "copy = gets.chomp\nans = -1\nlast = copy[-1].to_i\nif copy.length < 20\n# (0..(copy.length - 1)) do |i|\n# \tv = copy[i].to_i\n# \tif v.even? && v < last\t\t\n# \t\tcopy[i],copy[-1] = copy[-1],copy[i]\n# \t\tans = copy.to_i\n# \t\tbreak\n# \tend\n# end\n# if ans == -1\n# \t((copy.length - 1).downto(0)).each do |i|\n# \t\tv = copy[i].to_i\n# \t\tif v.even? && v > last\n# \t\t\tcopy[i],copy[-1] = copy[-1],copy[i]\n# \t\t\tans = copy.to_i\n# \t\t\tbreak\n# \t\tend\n# \tend\n# end\n# even_nos = copy.each_char.with_index.map{|k,i| [k.to_i,i] if k.to_i.even?}.compact\ncopy.each_char.with_index do |e,i|\n\tv = e.to_i\n\tnext if v.to_i.odd?\n\n\tif v < last\n\t\tans = i\n\t\tbreak\n\telse\n\t\tans = i\n\tend\nend\nif ans != -1\n\tcopy[ans],copy[-1] = copy[-1],copy[ans]\n\tans = copy.to_i\nend\nputs ans\nelse\n\tcopy.each_char.with_index do |e,i|\n\tv = e.to_i\n\tnext if v.to_i.odd?\n\n\tif v < last\n\t\tans = i\n\t\tbreak\n\telse\n\t\tans = i\n\tend\n\tend\n\tputs copy[-1]\nend"}, {"source_code": "copy = gets.chomp\nans = -1\nlast = copy[-1].to_i\nif copy.length < 20\n# (0..(copy.length - 1)) do |i|\n# \tv = copy[i].to_i\n# \tif v.even? && v < last\t\t\n# \t\tcopy[i],copy[-1] = copy[-1],copy[i]\n# \t\tans = copy.to_i\n# \t\tbreak\n# \tend\n# end\n# if ans == -1\n# \t((copy.length - 1).downto(0)).each do |i|\n# \t\tv = copy[i].to_i\n# \t\tif v.even? && v > last\n# \t\t\tcopy[i],copy[-1] = copy[-1],copy[i]\n# \t\t\tans = copy.to_i\n# \t\t\tbreak\n# \t\tend\n# \tend\n# end\n# even_nos = copy.each_char.with_index.map{|k,i| [k.to_i,i] if k.to_i.even?}.compact\ncopy.each_char.with_index do |e,i|\n\tv = e.to_i\n\tnext if v.to_i.odd?\n\n\tif v < last\n\t\tans = i\n\t\tbreak\n\telse\n\t\tans = i\n\tend\nend\nif ans != -1\n\tcopy[ans],copy[-1] = copy[-1],copy[ans]\n\tans = copy.to_i\nend\nputs ans\nelse\n\tcopy.each_char.with_index\n\tputs copy[-1]\nend"}, {"source_code": "n = gets.to_i\nans = -1\ncopy = n.to_s\nlast = copy[-1].to_i\n# (0..(copy.length - 1)) do |i|\n# \tv = copy[i].to_i\n# \tif v.even? && v < last\t\t\n# \t\tcopy[i],copy[-1] = copy[-1],copy[i]\n# \t\tans = copy.to_i\n# \t\tbreak\n# \tend\n# end\n# if ans == -1\n# \t((copy.length - 1).downto(0)).each do |i|\n# \t\tv = copy[i].to_i\n# \t\tif v.even? && v > last\n# \t\t\tcopy[i],copy[-1] = copy[-1],copy[i]\n# \t\t\tans = copy.to_i\n# \t\t\tbreak\n# \t\tend\n# \tend\n# end\neven_nos = copy.each_char.with_index.map{|k,i| [k.to_i,i] if k.to_i.even?}.compact\neven_nos.each do |e|\n\tif e[0] < last\n\t\tcopy[e[1]],copy[-1] = copy[-1],copy[e[1]]\n\t\tans = copy.to_i\n\t\tbreak\n\tend\nend\n\neven_nos.reverse_each do |e|\n\tif e[0] > last\n\t\tcopy[e[1]],copy[-1] = copy[-1],copy[e[1]]\n\t\tans = copy.to_i\n\t\tbreak\n\tend\nend\n\nputs ans"}, {"source_code": "copy = gets.chomp\nans = -1\nlast = copy[-1].to_i\nif copy.length < 20\n# (0..(copy.length - 1)) do |i|\n# \tv = copy[i].to_i\n# \tif v.even? && v < last\t\t\n# \t\tcopy[i],copy[-1] = copy[-1],copy[i]\n# \t\tans = copy.to_i\n# \t\tbreak\n# \tend\n# end\n# if ans == -1\n# \t((copy.length - 1).downto(0)).each do |i|\n# \t\tv = copy[i].to_i\n# \t\tif v.even? && v > last\n# \t\t\tcopy[i],copy[-1] = copy[-1],copy[i]\n# \t\t\tans = copy.to_i\n# \t\t\tbreak\n# \t\tend\n# \tend\n# end\n# even_nos = copy.each_char.with_index.map{|k,i| [k.to_i,i] if k.to_i.even?}.compact\ncopy.each_char.with_index do |e,i|\n\tv = e.to_i\n\tnext if v.to_i.odd?\n\n\tif v < last\n\t\tans = i\n\t\tbreak\n\telse\n\t\tans = i\n\tend\nend\nif ans != -1\n\tcopy[ans],copy[-1] = copy[-1],copy[ans]\n\tans = copy.to_i\nend\nputs ans\nelse\n\tcopy.each_char.with_index do |e,i|\n\tv = e.to_i\n\tnext if v.to_i.odd?\n\n\tif v < last\n\t\tans = i\n\t\tbreak\n\telse\n\t\tans = i\n\tend\n\tend\n\t# \n\tputs copy[-1]\nend"}, {"source_code": "n = gets.chomp\nif n.each_char.all?{|x| x.to_i.odd?}\n puts -1\nelse\n i = n.index(n.each_char.find{|x| x.to_i.even?})\n n[i], n[-1] = n[-1], n[i]\n puts n\nend\n"}, {"source_code": "n = gets.chomp\nif n.each_char.all?{|x| x.to_i.odd?}\n puts -1\nelse\n m = []\n n.each_char{|x| m << x if x.to_i.even?}\n i = if m[0] < n[-1]\n n.index(m[0])\n else\n n.rindex(m.min)\n end\n n[i], n[-1] = n[-1], n[i]\n puts n\nend\n"}, {"source_code": "n = gets.chomp\nif n.each_char.all?{|x| x.to_i.odd?}\n puts -1\nelse\n m = []\n n.each_char{|x| m << x if x.to_i.even?}\n i = if m.min > n[-1]\n n.rindex(m.min)\n else\n n.index(m.min)\n end\n n[i], n[-1] = n[-1], n[i]\n puts n\nend\n"}, {"source_code": "n = gets.chomp\nif n.each_char.all?{|x| x.to_i.odd?}\n puts -1\nelse\n m = []\n n.each_char{|x| m << x if x.to_i.even?}\n i = n.rindex(m.min)\n n[i], n[-1] = n[-1], n[i]\n puts n\nend\n"}, {"source_code": "n = gets.chomp\nif n.each_char.all?{|x| x.to_i.odd?}\n puts -1\nelse\n m = []\n n.each_char{|x| m << x if x.to_i.even?}\n i = if m.all?{|x| x < n[-1]}\n n.index(m.find{|x| x < n[-1]})\n else\n n.index(m.min)\n end\n n[i], n[-1] = n[-1], n[i]\n puts n\nend\n"}, {"source_code": "n = gets.chomp\nif n.each_char.all?{|x| x.to_i.odd?}\n puts -1\nelse\n m = []\n n.each_char{|x| m << x if x.to_i.even?}\n i = if m.all?{|x| x < n[-1]}\n n.index(m.find{|x| x < n[-1]})\n else\n n.rindex(m.min)\n end\n n[i], n[-1] = n[-1], n[i]\n puts n\nend\n"}, {"source_code": "a=Array.new\nstr=gets.to_s\na=str.split(\"\")\nh=0\nfor i in 0..a.length-1\n if a[i].to_i>0 and a[i].to_i%2==0\n# a[-1] 字符串最后一般是换行符,可用chomp方法去除\n# puts a[-2]\n t=a[-2]\n a[-2]=a[i]\n a[i]=t\n h=1\n break\n end\nend\nif h==1\n for i in 0..a.length-1\n print a[i].chomp\n end\n puts\nelsif h==0\n puts -1\nend"}], "src_uid": "bc375e27bd52f413216aaecc674366f8"} {"nl": {"description": "You are given two strings $$$s$$$ and $$$t$$$, both of length $$$n$$$. Each character in both string is 'a', 'b' or 'c'.In one move, you can perform one of the following actions: choose an occurrence of \"ab\" in $$$s$$$ and replace it with \"ba\"; choose an occurrence of \"bc\" in $$$s$$$ and replace it with \"cb\". You are allowed to perform an arbitrary amount of moves (possibly, zero). Can you change string $$$s$$$ to make it equal to string $$$t$$$?", "input_spec": "The first line contains a single integer $$$q$$$ ($$$1 \\le q \\le 10^4$$$) — the number of testcases. The first line of each testcase contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$) — the length of strings $$$s$$$ and $$$t$$$. The second line contains string $$$s$$$ of length $$$n$$$. Each character is 'a', 'b' or 'c'. The third line contains string $$$t$$$ of length $$$n$$$. Each character is 'a', 'b' or 'c'. The sum of $$$n$$$ over all testcases doesn't exceed $$$10^5$$$.", "output_spec": "For each testcase, print \"YES\" if you can change string $$$s$$$ to make it equal to string $$$t$$$ by performing an arbitrary amount of moves (possibly, zero). Otherwise, print \"NO\".", "sample_inputs": ["5\n\n3\n\ncab\n\ncab\n\n1\n\na\n\nb\n\n6\n\nabbabc\n\nbbaacb\n\n10\n\nbcaabababc\n\ncbbababaac\n\n2\n\nba\n\nab"], "sample_outputs": ["YES\nNO\nYES\nYES\nNO"], "notes": null}, "positive_code": [{"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n s = gets.chomp.chars.to_a\r\n t = gets.chomp.chars.to_a\r\n si, ti = s.zip(0..), t.zip(0..)\r\n sa = si.filter_map { |c, i| i if c == \"a\" }\r\n sc = si.filter_map { |c, i| i if c == \"c\" }\r\n ta = ti.filter_map { |c, i| i if c == \"a\" }\r\n tc = ti.filter_map { |c, i| i if c == \"c\" }\r\n ok = s.select { |c| c != \"b\" } == t.select { |c| c != \"b\" } &&\r\n sa.zip(ta).all? { |x, y| x <= y } &&\r\n sc.zip(tc).all? { |x, y| x >= y }\r\n puts ok ? \"YES\" : \"NO\"\r\nend\r\n"}, {"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n s = gets.chomp\r\n t = gets.chomp\r\n\r\n if s.count('b') != t.count('b')\r\n puts 'NO'\r\n next\r\n end\r\n\r\n j = 0\r\n ok = true\r\n n.times do |i|\r\n next if s[i] == 'b'\r\n\r\n while t[j] == 'b'\r\n j += 1\r\n end\r\n\r\n if s[i] != t[j] || (s[i] == 'a' && i > j) || (s[i] == 'c' && i < j)\r\n ok = false\r\n break\r\n end\r\n j += 1\r\n \r\n end\r\n puts ok ? 'YES' : 'NO'\r\nend\r\n"}], "negative_code": [{"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n s = gets.chomp.chars.to_a\r\n t = gets.chomp.chars.to_a\r\n si, ti = s.zip(0..), t.zip(0..)\r\n sa = si.filter_map { |c, i| i if c == \"a\" }\r\n sc = si.filter_map { |c, i| i if c == \"c\" }\r\n ta = ti.filter_map { |c, i| i if c == \"a\" }\r\n tc = ti.filter_map { |c, i| i if c == \"c\" }\r\n ok = s.select { |c| c != \"b\" } == t.select { |c| c != \"b\" } && (sa <=> ta) <= 0 && (sc <=> tc) >= 0\r\n puts ok ? \"YES\" : \"NO\"\r\nend\r\n"}, {"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n s = gets.chomp.chars.to_a\r\n t = gets.chomp.chars.to_a\r\n si, ti = s.zip(0..), t.zip(0..)\r\n sa = si.filter_map { |c, i| i if c == \"a\" }\r\n sb = si.filter_map { |c, i| i if c == \"b\" }\r\n sc = si.filter_map { |c, i| i if c == \"c\" }\r\n ta = ti.filter_map { |c, i| i if c == \"a\" }\r\n tb = ti.filter_map { |c, i| i if c == \"b\" }\r\n tc = ti.filter_map { |c, i| i if c == \"c\" }\r\n ok = sa.count == ta.count && sb.count == tb.count && sc.count == tc.count && (sa <=> ta) <= 0 && (sc <=> tc) >= 0\r\n puts ok ? \"YES\" : \"NO\"\r\nend\r\n"}], "src_uid": "235ddb32dbe19c0da1f77069e36128bb"} {"nl": {"description": "The R1 company wants to hold a web search championship. There were n computers given for the competition, each of them is connected to the Internet. The organizers believe that the data transfer speed directly affects the result. The higher the speed of the Internet is, the faster the participant will find the necessary information. Therefore, before the competition started, each computer had its maximum possible data transfer speed measured. On the i-th computer it was ai kilobits per second.There will be k participants competing in the championship, each should get a separate computer. The organizing company does not want any of the participants to have an advantage over the others, so they want to provide the same data transfer speed to each participant's computer. Also, the organizers want to create the most comfortable conditions for the participants, so the data transfer speed on the participants' computers should be as large as possible.The network settings of the R1 company has a special option that lets you to cut the initial maximum data transfer speed of any computer to any lower speed. How should the R1 company configure the network using the described option so that at least k of n computers had the same data transfer speed and the data transfer speed on these computers was as large as possible?", "input_spec": "The first line contains two space-separated integers n and k (1 ≤ k ≤ n ≤ 100) — the number of computers and the number of participants, respectively. In the second line you have a space-separated sequence consisting of n integers: a1, a2, ..., an (16 ≤ ai ≤ 32768); number ai denotes the maximum data transfer speed on the i-th computer.", "output_spec": "Print a single integer — the maximum Internet speed value. It is guaranteed that the answer to the problem is always an integer.", "sample_inputs": ["3 2\n40 20 30", "6 4\n100 20 40 20 50 50"], "sample_outputs": ["30", "40"], "notes": "NoteIn the first test case the organizers can cut the first computer's speed to 30 kilobits. Then two computers (the first and the third one) will have the same speed of 30 kilobits. They should be used as the participants' computers. This answer is optimal."}, "positive_code": [{"source_code": "# https://codeforces.com/problemset/problem/412/B\n\ninput = gets.split(' ').map(&:to_i)\ntransfer_speed_table = gets.split(' ').map(&:to_i)\n\nk = input[1]\n\ntransfer_speed_table.sort! { |a, b| b <=> a }\n\nputs transfer_speed_table[k-1]\n"}, {"source_code": "nk = gets.split(' ')\nn = nk[0].to_i\nk = nk[1].to_i\nai = gets.split(' ').map!{ |el| el.to_i }\nai = ai.sort.reverse\nputs ai[k-1]\n"}, {"source_code": "#!/usr/bin/env ruby\n\nclass Main\n attr_reader :computer_count, :student_count, :array\n\n def initialize(computer_count, student_count, array)\n @computer_count = computer_count\n @student_count = student_count\n @array = array.sort.reverse\n end\n\n def run\n array[student_count - 1]\n end\nend\n\ncomputer_count, student_count = gets.split(\" \")\narray = gets.split(\" \")\nputs Main.new(computer_count.to_i, student_count.to_i, array.map(&:to_i)).run\n"}, {"source_code": "n,k = gets.split(' ').map(&:to_i)\n\na = gets.split(' ').map(&:to_i).sort.reverse\n\nputs a[k-1]"}, {"source_code": "n, m = gets.split\na = gets.split.map{|i|i.to_i}.sort\nprint a[-m.to_i]"}], "negative_code": [], "src_uid": "6eca08d7cc2dec6f4f84d3faa9a8a915"} {"nl": {"description": "You are given a tree consisting of $$$n$$$ nodes. You want to write some labels on the tree's edges such that the following conditions hold: Every label is an integer between $$$0$$$ and $$$n-2$$$ inclusive. All the written labels are distinct. The largest value among $$$MEX(u,v)$$$ over all pairs of nodes $$$(u,v)$$$ is as small as possible. Here, $$$MEX(u,v)$$$ denotes the smallest non-negative integer that isn't written on any edge on the unique simple path from node $$$u$$$ to node $$$v$$$.", "input_spec": "The first line contains the integer $$$n$$$ ($$$2 \\le n \\le 10^5$$$) — the number of nodes in the tree. Each of the next $$$n-1$$$ lines contains two space-separated integers $$$u$$$ and $$$v$$$ ($$$1 \\le u,v \\le n$$$) that mean there's an edge between nodes $$$u$$$ and $$$v$$$. It's guaranteed that the given graph is a tree.", "output_spec": "Output $$$n-1$$$ integers. The $$$i^{th}$$$ of them will be the number written on the $$$i^{th}$$$ edge (in the input order).", "sample_inputs": ["3\n1 2\n1 3", "6\n1 2\n1 3\n2 4\n2 5\n5 6"], "sample_outputs": ["0\n1", "0\n3\n2\n4\n1"], "notes": "NoteThe tree from the second sample:"}, "positive_code": [{"source_code": "n = gets.to_i\nab = n.pred.times.map {gets.split.map(&:to_i).map(&:pred)}\ncnt = Array.new(n, 0)\nab.each{|a| a.each{|i| cnt[i] += 1}}\nans = Array.new(n.pred, -1)\nif (pos = cnt.index{|c| c >= 3})\n\t(0...n.pred).to_a.select{|i| ab[i].index(pos)}.take(3).each_with_index{|i, x| ans[i] = x}\n\t(0...n.pred).to_a.select{|i| ans[i] == -1}.each_with_index{|i, x| ans[i] = x + 3}\nelse\n\tans = (0...n.pred).to_a\nend\nputs ans.join(\"\\n\")"}, {"source_code": "n = gets.to_i\nab = n.pred.times.map {gets.split.map(&:to_i).map(&:pred)}\ncnt = Array.new(n, 0)\nab.each do |a, b|\n\tcnt[a] += 1\n\tcnt[b] += 1\nend\n\npos = cnt.index {|c| c >= 3}\nans = Array.new(n.pred, -1)\nif pos\n\tx = 0\n\tn.pred.times do |i|\n\t\tif ab[i].index(pos) && x < 3\n\t\t\tans[i] = x\n\t\t\tx += 1\n\t\tend\n\tend\n\tn.pred.times do |i|\n\t\tif ans[i] == -1\n\t\t\tans[i] = x;\n\t\t\tx += 1\n\t\tend\n\tend\nelse\n\tans = (0...n.pred).to_a\nend\nputs ans.join(\"\\n\")"}], "negative_code": [], "src_uid": "5ef966b7d9fbf27e6197b074eca31b15"} {"nl": {"description": "Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are $$$n$$$ players arranged in a circle, so that for all $$$j$$$ such that $$$2 \\leq j \\leq n$$$, player $$$j - 1$$$ is to the left of the player $$$j$$$, and player $$$j$$$ is to the right of player $$$j - 1$$$. Additionally, player $$$n$$$ is to the left of player $$$1$$$, and player $$$1$$$ is to the right of player $$$n$$$.Currently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either $$$0$$$, $$$1$$$, or $$$2$$$ other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly $$$1$$$ other player, then they should logically attack that player in response. If instead a player is being attacked by $$$0$$$ or $$$2$$$ other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players.Unfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the $$$n$$$ players in the game to make them instead attack another player  — i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left. Omkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 10^4$$$). The descriptions of the test cases follows. The first line of each test case contains one integer $$$n$$$ ($$$3 \\leq n \\leq 2 \\cdot 10^5$$$)  — the amount of players (and therefore beds) in this game of Bed Wars. The second line of each test case contains a string $$$s$$$ of length $$$n$$$. The $$$j$$$-th character of $$$s$$$ is equal to L if the $$$j$$$-th player is attacking the player to their left, and R if the $$$j$$$-th player is attacking the player to their right. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, output one integer: the minimum number of players Omkar needs to talk to to make it so that all players are acting logically according to Bed Wars strategy. It can be proven that it is always possible for Omkar to achieve this under the given constraints.", "sample_inputs": ["5\n4\nRLRL\n6\nLRRRRL\n8\nRLLRRRLL\n12\nLLLLRRLRRRLL\n5\nRRRRR"], "sample_outputs": ["0\n1\n1\n3\n2"], "notes": "NoteIn the first test case, players $$$1$$$ and $$$2$$$ are attacking each other, and players $$$3$$$ and $$$4$$$ are attacking each other. Each player is being attacked by exactly $$$1$$$ other player, and each player is attacking the player that is attacking them, so all players are already being logical according to Bed Wars strategy and Omkar does not need to talk to any of them, making the answer $$$0$$$.In the second test case, not every player acts logically: for example, player $$$3$$$ is attacked only by player $$$2$$$, but doesn't attack him in response. Omkar can talk to player $$$3$$$ to convert the attack arrangement to LRLRRL, in which you can see that all players are being logical according to Bed Wars strategy, making the answer $$$1$$$."}, "positive_code": [{"source_code": "# require 'pry'\nt = gets.chomp.to_i\nt.times do\n n = gets.chomp.to_i\n arr = gets.chomp.split('')\n\n same = true\n (1...n).each do |i|\n if arr[i] != arr[i-1]\n same = false\n break\n end\n end\n if same\n puts((n + 2) / 3)\n else\n last_index = 0\n cnt = 1\n list = []\n (1...n).each do |i|\n if arr[i] == arr[last_index]\n cnt += 1\n else\n list << cnt\n last_index = i\n cnt = 1\n end\n end\n if arr[0] == arr[n - 1]\n list[0] = list[0].to_i + cnt\n else\n list << cnt\n end\n puts list.inject(0) { |sum, ele| sum + ele / 3 }\n end\nend\n"}], "negative_code": [{"source_code": "# require 'pry'\nt = gets.chomp.to_i\nt.times do\n n = gets.chomp.to_i\n arr = gets.chomp.split('')\n ans = 0\n (0...n).each do |i|\n attacked_by_count = 0\n attacked_by_count += 1 if arr[(i - 1) % n] == 'R'\n attacked_by_count += 1 if arr[(i + 1) % n] == 'L'\n next if [0, 2].include?(attacked_by_count)\n\n if arr[i] == 'R' && arr[(i - 1) % n] == 'R'\n arr[i] = 'L'\n ans += 1\n end\n if arr[i] == 'L' && arr[(i + 1) % n] == 'L'\n arr[i] = 'R'\n ans += 1\n end\n end\n puts ans\nend\n"}, {"source_code": "# require 'pry'\nt = gets.chomp.to_i\nt.times do\n n = gets.chomp.to_i\n arr = gets.chomp.split('')\n last_index = 0\n cnt = 1\n list = []\n (1...n).each do |i|\n if arr[i] == arr[last_index]\n cnt += 1\n else\n list << cnt\n last_index = i\n cnt = 1\n end\n end\n if arr[0] == arr[n - 1]\n list[0] = list[0].to_i + cnt\n else\n list << cnt\n end\n puts list.inject(0) { |sum, ele| sum + ele / 3 }\nend\n"}], "src_uid": "b06d5b48525cd386a0141bdf44579a5c"} {"nl": {"description": "You are a coach of a group consisting of $$$n$$$ students. The $$$i$$$-th student has programming skill $$$a_i$$$. All students have distinct programming skills. You want to divide them into teams in such a way that: No two students $$$i$$$ and $$$j$$$ such that $$$|a_i - a_j| = 1$$$ belong to the same team (i.e. skills of each pair of students in the same team have the difference strictly greater than $$$1$$$); the number of teams is the minimum possible. You have to answer $$$q$$$ independent queries.", "input_spec": "The first line of the input contains one integer $$$q$$$ ($$$1 \\le q \\le 100$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \\le n \\le 100$$$) — the number of students in the query. The second line of the query contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 100$$$, all $$$a_i$$$ are distinct), where $$$a_i$$$ is the programming skill of the $$$i$$$-th student.", "output_spec": "For each query, print the answer on it — the minimum number of teams you can form if no two students $$$i$$$ and $$$j$$$ such that $$$|a_i - a_j| = 1$$$ may belong to the same team (i.e. skills of each pair of students in the same team has the difference strictly greater than $$$1$$$)", "sample_inputs": ["4\n4\n2 10 1 20\n2\n3 6\n5\n2 3 4 99 100\n1\n42"], "sample_outputs": ["2\n1\n2\n1"], "notes": "NoteIn the first query of the example, there are $$$n=4$$$ students with the skills $$$a=[2, 10, 1, 20]$$$. There is only one restriction here: the $$$1$$$-st and the $$$3$$$-th students can't be in the same team (because of $$$|a_1 - a_3|=|2-1|=1$$$). It is possible to divide them into $$$2$$$ teams: for example, students $$$1$$$, $$$2$$$ and $$$4$$$ are in the first team and the student $$$3$$$ in the second team.In the second query of the example, there are $$$n=2$$$ students with the skills $$$a=[3, 6]$$$. It is possible to compose just a single team containing both students."}, "positive_code": [{"source_code": "n = readline.to_i\nn.times do |i|\n m = readline.to_i\n a = readline.to_s.split\n ni = []\n a.each {|ite|\n ni.push(ite.to_i)\n }\n ni = ni.sort\n last = ni[0]\n ch = 0\n ni.each { |ite |\n if (ite - last).abs == 1\n ch = 1\n break\n end\n last = ite\n }\n if ch == 1\n puts 2\n else\n puts 1\n end\nend\n"}, {"source_code": "gets.to_i.times{\n n=gets.to_i\n a=gets.split.map(&:to_i).sort\n p (0...n-1).count{|i|a[i]+1==a[i+1]}>0 ? 2 : 1\n}"}, {"source_code": "gets.to_i.times do\n n = gets.to_i\n as = gets.split.map(&:to_i).sort\n flg = false\n (0..n-2).each do |i|\n flg = true if (as[i] - as[i+1]).abs == 1\n end\n puts flg ? 2 : 1\nend\n"}, {"source_code": "def calcs(x,y,ax,ay,bx,by) ((ax-x)*(by-y)-(bx-x)*(ay-y))/2.0 end\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaizyo(n)(n < 2)? 1 : (2..n).inject(:*) end\n\ndef scount(a) b=na(a.max+1);a.each{|n|b[n]+=1};return b end\n\ndef na(n=0,d=0) Array.new(n,d)end\n\ndef na2(n=0,m=0,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n=0,m=0,l=0,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef bit(n) n.to_s(2).split(\"\").map(&:to_i) end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\nn = inp[0]\nn.times do\n q = inp[0]\n st = inp\n mae = -9\n ans = 1\n st.sort!\n st.each do |d|\n if(d - mae == 1)\n ans = 2\n break\n end\n mae = d\n end\n puts ans\n \nend"}, {"source_code": "q = gets.to_i\nq.times do\n n = gets.to_i\n a = gets.split.map(&:to_i).sort\n nteams, nused, used = 0, 0, []\n while nused < n\n i = 0\n t = nil\n while i < n\n if not used[i] and (not t or (a[i] - t).abs > 1)\n t = a[i]\n nused += 1\n used[i] = true\n end\n i += 1\n end\n nteams += 1\n end\n puts nteams\nend\n"}, {"source_code": "gets.to_i.times{n,a=gets.to_i,gets.split.map(&:to_i).sort;p (0..n-2).count{|i|a[i]+1==a[i+1]}>0?2:1}\n"}], "negative_code": [{"source_code": "q = gets.to_i\nq.times do\n n = gets.to_i\n a = gets.split.map &:to_i\n nteams, nused, used = 0, 0, []\n while nused < n\n i = 0\n t = nil\n while i < n\n if not used[i] and (not t or (a[i] - t).abs > 1)\n t = t || a[i]\n nused += 1\n used[i] = true\n end\n i += 1\n end\n nteams += 1\n end\n puts nteams\nend\n"}], "src_uid": "dd2cd365d7afad9c2b5bdbbd45d87c8a"} {"nl": {"description": "Polycarp has invited $$$n$$$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.Each friend is characterized by two values $$$h_i$$$ (their height) and $$$w_i$$$ (their width). On the photo the $$$i$$$-th friend will occupy a rectangle $$$h_i \\times w_i$$$ (if they are standing) or $$$w_i \\times h_i$$$ (if they are lying on the side).The $$$j$$$-th friend can be placed in front of the $$$i$$$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $$$i$$$-th friend. Formally, at least one of the following conditions must be fulfilled: $$$h_j < h_i$$$ and $$$w_j < w_i$$$ (both friends are standing or both are lying); $$$w_j < h_i$$$ and $$$h_j < w_i$$$ (one of the friends is standing and the other is lying). For example, if $$$n = 3$$$, $$$h=[3,5,3]$$$ and $$$w=[4,4,3]$$$, then: the first friend can be placed in front of the second: $$$w_1 < h_2$$$ and $$$h_1 < w_2$$$ (one of the them is standing and the other one is lying); the third friend can be placed in front of the second: $$$h_3 < h_2$$$ and $$$w_3 < w_2$$$ (both friends are standing or both are lying). In other cases, the person in the foreground will overlap the person in the background.Help Polycarp for each $$$i$$$ find any $$$j$$$, such that the $$$j$$$-th friend can be located in front of the $$$i$$$-th friend (i.e. at least one of the conditions above is fulfilled).Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $$$i$$$ any other friend $$$j$$$ who can be located in front of him. Think about it as you need to solve $$$n$$$ separate independent subproblems.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$1 \\leq n \\leq 2 \\cdot 10^5$$$) — the number of friends. This is followed by $$$n$$$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $$$h_i$$$ and $$$w_i$$$ ($$$1 \\leq h_i, w_i \\leq 10^9$$$) — height and width of the $$$i$$$-th friend, respectively. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case output $$$n$$$ integers on a separate line, where the $$$i$$$-th number is the index of a friend that can be placed in front of the $$$i$$$-th. If there is no such friend, then output -1. If there are several answers, output any.", "sample_inputs": ["4\n3\n3 4\n5 4\n3 3\n3\n1 3\n2 2\n3 1\n4\n2 2\n3 1\n6 3\n5 4\n4\n2 2\n2 3\n1 1\n4 4"], "sample_outputs": ["-1 3 -1 \n-1 -1 -1 \n-1 -1 2 2 \n3 3 -1 3"], "notes": "NoteThe first test case is described in the statement.In the third test case, the following answers are also correct: $$$[-1, -1, 1, 2]$$$; $$$[-1, -1, 1, 1]$$$; $$$[-1, -1, 2, 1]$$$. "}, "positive_code": [{"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n a = []\r\n n.times do |i|\r\n h, w = gets.split.map &:to_i\r\n h, w = w, h if h < w\r\n a << [i, h, w]\r\n end\r\n a.sort_by! { |it| [it[1], -it[2]] }\r\n mn, pos = 1E18.to_i, -1\r\n res = [-1] * n\r\n a.each_with_index do |it|\r\n i, _, w = it\r\n res[i] = pos + 1 if w > mn\r\n if w < mn\r\n mn, pos = w, i\r\n end\r\n end\r\n puts res.join' '\r\nend"}], "negative_code": [{"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n a = []\r\n n.times do |i|\r\n h, w = gets.split.map &:to_i\r\n h, w = w, h if h < w\r\n a << [i, h, w]\r\n end\r\n a.sort_by! { |it| [it[1], -it[2]] }\r\n mn, pos = 1E18.to_i, -1\r\n a.each_with_index do |it, i|\r\n print pos + 1, ' ' if it[1] > mn\r\n if it[1] < mn\r\n mn, pos = it[1], i\r\n end\r\n end\r\n puts\r\nend"}, {"source_code": "INF = 1E10.to_i\r\ndef solve(a, h, w, i)\r\n pref = []\r\n cur = [[INF, -1], [INF, -1]]\r\n a.each do |it|\r\n if it[1] < cur[0][0]\r\n cur[0], cur[1] = [it[1], it[2]], cur[0]\r\n else\r\n cur[1] = [cur[1], [it[1], it[2]]].min\r\n end\r\n pref << [it[0], cur]\r\n end\r\n l, r = -1, a.size\r\n while r - l > 1\r\n m = (l + r) / 2\r\n if pref[m][0] < h\r\n l = m\r\n else\r\n r = m\r\n end\r\n end\r\n return -1 if l == -1\r\n m1, m2 = pref[l][1][0], pref[l][1][1]\r\n if m1[1] != i\r\n m1[0] < w ? m1[1] + 1 : -1\r\n else\r\n m2[0] < w ? m2[1] + 1 : -1\r\n end\r\nend\r\ngets.to_i.times do\r\n n = gets.to_i\r\n h, w, a, b = [0] * n, [0] * n, [], []\r\n n.times do |i|\r\n h[i], w[i] = gets.split.map &:to_i\r\n a << [h[i], w[i], i]\r\n b << [w[i], h[i], i]\r\n end\r\n a.sort!\r\n b.sort!\r\n n.times do |i|\r\n res = solve(a, h[i], w[i], i)\r\n solve(b, h[i], w[i], i) if res == -1\r\n print res, ' '\r\n end\r\n puts\r\nend"}, {"source_code": "INF = 1E10.to_i\r\ndef solve(a, w, h, i)\r\n pref = []\r\n cur = [[INF, -1], [INF, -1]]\r\n a.each do |it|\r\n if it[1] < cur[0][0]\r\n cur[0], cur[1] = [it[1], it[2]], cur[0]\r\n else\r\n cur[1] = [cur[1], [it[1], it[2]]].min\r\n end\r\n pref << [it[0], cur]\r\n end\r\n l, r = -1, a.size\r\n while r - l > 1\r\n m = (l + r) / 2\r\n if pref[m][0] < h\r\n l = m\r\n else\r\n r = m\r\n end\r\n end\r\n return -1 if l == -1\r\n m1, m2 = pref[l][1][0], pref[l][1][1]\r\n if m1[1] != i\r\n m1[0] < w ? m1[1] + 1 : -1\r\n else\r\n m2[0] < w ? m2[1] + 1 : -1\r\n end\r\nend\r\ngets.to_i.times do\r\n n = gets.to_i\r\n h, w, a, b = [0] * n, [0] * n, [], []\r\n n.times do |i|\r\n h[i], w[i] = gets.split.map &:to_i\r\n a << [h[i], w[i], i]\r\n b << [w[i], h[i], i]\r\n end\r\n a.sort!\r\n b.sort!\r\n n.times do |i|\r\n res = solve(a, h[i], w[i], i)\r\n solve(b, h[i], w[i], i) if res == -1\r\n print res, ' '\r\n end\r\n puts\r\nend"}], "src_uid": "f15df307d67d5754a3a322a66de1338c"} {"nl": {"description": "A number is ternary if it contains only digits $$$0$$$, $$$1$$$ and $$$2$$$. For example, the following numbers are ternary: $$$1022$$$, $$$11$$$, $$$21$$$, $$$2002$$$.You are given a long ternary number $$$x$$$. The first (leftmost) digit of $$$x$$$ is guaranteed to be $$$2$$$, the other digits of $$$x$$$ can be $$$0$$$, $$$1$$$ or $$$2$$$.Let's define the ternary XOR operation $$$\\odot$$$ of two ternary numbers $$$a$$$ and $$$b$$$ (both of length $$$n$$$) as a number $$$c = a \\odot b$$$ of length $$$n$$$, where $$$c_i = (a_i + b_i) \\% 3$$$ (where $$$\\%$$$ is modulo operation). In other words, add the corresponding digits and take the remainders of the sums when divided by $$$3$$$. For example, $$$10222 \\odot 11021 = 21210$$$.Your task is to find such ternary numbers $$$a$$$ and $$$b$$$ both of length $$$n$$$ and both without leading zeros that $$$a \\odot b = x$$$ and $$$max(a, b)$$$ is the minimum possible.You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 \\le n \\le 5 \\cdot 10^4$$$) — the length of $$$x$$$. The second line of the test case contains ternary number $$$x$$$ consisting of $$$n$$$ digits $$$0, 1$$$ or $$$2$$$. It is guaranteed that the first digit of $$$x$$$ is $$$2$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5 \\cdot 10^4$$$ ($$$\\sum n \\le 5 \\cdot 10^4$$$).", "output_spec": "For each test case, print the answer — two ternary integers $$$a$$$ and $$$b$$$ both of length $$$n$$$ and both without leading zeros such that $$$a \\odot b = x$$$ and $$$max(a, b)$$$ is the minimum possible. If there are several answers, you can print any.", "sample_inputs": ["4\n5\n22222\n5\n21211\n1\n2\n9\n220222021"], "sample_outputs": ["11111\n11111\n11000\n10211\n1\n1\n110111011\n110111010"], "notes": null}, "positive_code": [{"source_code": "t = gets.chomp.to_i\nt.times do\n n = gets.chomp.to_i\n a = \"\"\n b = \"\"\n diff = false\n x = gets.chomp\n for i in 0..n-1\n if x[i] == \"2\"\n if diff\n a << \"0\"\n b << \"2\"\n else\n a << \"1\"\n b << \"1\"\n end\n elsif x[i] == \"1\"\n if diff\n a << \"0\"\n b << \"1\"\n else\n a << \"1\"\n b << \"0\"\n diff = true\n end\n else\n a << \"0\"\n b << \"0\"\n end\n end\n puts a\n puts b\nend"}, {"source_code": "gets.to_i.times do\n\tn = gets.to_i\n\ts = gets.split('').map(&:to_i)\n\ta = Array.new(0, n)\n\tb = Array.new(0, n)\n\t\n\tflag = false\n\tn.times do |i|\n\t\tif flag\n\t\t\ta[i] = 0\n\t\t\tb[i] = s[i]\n\t\telse\n\t\t\ta[i] = (s[i] + 1) / 2\n\t\t\tb[i] = s[i] / 2\n\t\t\tflag = true if a[i] != b[i]\n\t\tend\n\tend\n\tputs a.join('')\n\tputs b.join('')\nend"}, {"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\n\nt = gets.to_i\nt.times {\n len = gets.to_i\n tnr = gets.chomp\n a = []\n b = []\n a_equals_b = true\n for i in 0...len\n case tnr[i]\n when '2' # how to split 2? (2, 0), (1, 1), (0, 2)\n if a_equals_b == true\n a << 1\n b << 1\n else\n a << 0\n b << 2\n end\n when '1' # where to increase one?\n if a_equals_b == true\n a << 1\n b << 0\n a_equals_b = false\n else\n a << 0\n b << 1\n end\n when '0' # nothing to do\n a << 0\n b << 0\n else\n raise Exception.new(\"should not reach here\")\n end\n end\n\n puts a.join('')\n puts b.join('')\n}"}, {"source_code": "t = gets.to_i\nt.times do\n n = gets.to_i\n x = gets.chomp\n a = []\n b = []\n diff = false\n x.each_char do |c|\n case c\n when \"0\"\n a.push(\"0\")\n b.push(\"0\")\n when \"1\"\n if diff\n a.push(\"0\")\n b.push(\"1\")\n else\n a.push(\"1\")\n b.push(\"0\")\n diff = true\n end\n when \"2\"\n if diff\n a.push(\"0\")\n b.push(\"2\")\n else\n a.push(\"1\")\n b.push(\"1\")\n end\n end\n end\n puts a.join\n puts b.join\nend\n"}, {"source_code": "1.upto(gets.to_i) do\n n = gets.to_i\n s = gets.chomp\n\n pos = s.index('1')\n beg = s[0, pos ? pos : s.size ].chars.map { |c| c == '2' ? '1' : '0' }.join\n\n if pos\n puts beg + '1' + '0' * (n - pos - 1)\n puts beg + '0' + s[pos+1, n - pos - 1]\n else\n puts beg\n puts beg\n end\n\n next\n\n odd = false\n result = ['', '']\n gets.chomp.chars.each_with_index do |char, idx|\n if odd\n result[1] += char\n next\n end\n\n case char\n when '2'\n result.map! { |s| s + '1' }\n when '1'\n result[0] += '1' + '0' * (n - idx - 1)\n result[1] += '0'\n odd = true\n when '0'\n result.map! { |s| s + '0' }\n end\n end\n\n result.each { |s| puts s }\nend\n"}, {"source_code": "t = gets.to_i\n(1..t).each do\n n = gets.to_i\n s = gets.split('')\n b = false\n s.each do |c|\n if c==\"\\n\"\n break\n end\n if b\n print '0'\n else\n print c.to_i/2+c.to_i.modulo(2)\n end\n if c=='1'\n b=true\n end\n end\n print \"\\n\"\n b=false\n s.each do |c|\n if c==\"\\n\"\n break\n end\n if b\n print c\n else\n print c.to_i/2\n end\n if c=='1'\n b=true\n end\n end\n print \"\\n\"\nend\n"}], "negative_code": [{"source_code": "t = gets.chomp.to_i\nt.times do\n n = gets.chomp.to_i\n a = \"\"\n b = \"\"\n\n x = gets.chomp.split('')\n x.each do |i|\n if i == \"2\"\n a << \"1\"\n b << \"1\"\n elsif i == \"1\"\n a << \"1\"\n b << \"0\"\n else\n a << \"0\"\n b << \"0\"\n end\n end\n puts a\n puts b\nend"}, {"source_code": "1.upto(gets.to_i) do\n n = gets.to_i\n\n odd = false\n result = ['', '']\n gets.chomp.chars.each_with_index do |char, idx|\n if odd\n result[1] += char\n next\n end\n\n case char\n when '2'\n result.map! { |s| s + '1' }\n when '1'\n result[0] += '1' + '0' * (n - idx)\n result[1] += '0'\n odd = true\n when '0'\n result.map! { |s| s + '0' }\n end\n end\n\n result.each { |s| puts s }\nend\n"}], "src_uid": "c4c8cb860ea9a5b56bb35532989a9192"} {"nl": {"description": "The clique problem is one of the most well-known NP-complete problems. Under some simplification it can be formulated as follows. Consider an undirected graph G. It is required to find a subset of vertices C of the maximum size such that any two of them are connected by an edge in graph G. Sounds simple, doesn't it? Nobody yet knows an algorithm that finds a solution to this problem in polynomial time of the size of the graph. However, as with many other NP-complete problems, the clique problem is easier if you consider a specific type of a graph.Consider n distinct points on a line. Let the i-th point have the coordinate xi and weight wi. Let's form graph G, whose vertices are these points and edges connect exactly the pairs of points (i, j), such that the distance between them is not less than the sum of their weights, or more formally: |xi - xj| ≥ wi + wj.Find the size of the maximum clique in such graph.", "input_spec": "The first line contains the integer n (1 ≤ n ≤ 200 000) — the number of points. Each of the next n lines contains two numbers xi, wi (0 ≤ xi ≤ 109, 1 ≤ wi ≤ 109) — the coordinate and the weight of a point. All xi are different.", "output_spec": "Print a single number — the number of vertexes in the maximum clique of the given graph.", "sample_inputs": ["4\n2 3\n3 1\n6 1\n0 2"], "sample_outputs": ["3"], "notes": "NoteIf you happen to know how to solve this problem without using the specific properties of the graph formulated in the problem statement, then you are able to get a prize of one million dollars!The picture for the sample test. "}, "positive_code": [{"source_code": "n = gets.strip.to_i\nxws = readlines.map {|line| line.strip.split.map(&:to_i) }\n\nxws = xws.map {|x,w| [x-w, x+w] }.sort_by(&:last)\n\nresult=0\ntarget=0\nindex=target\nuntil index==n\n index=target+1\n ct = xws[target].last\n index+=1 while xws[index] && xws[index].first < ct\n target = index\n\n result+=1\nend\n\np result\n"}, {"source_code": "n = gets.to_i\narr = []\nn.times do \n\tx, w = gets.split.map(&:to_i)\n\tarr << [x - w, x + w]\nend\n\narr.sort! do |a, b|\n\ta[1] <=> b[1]\nend\n\nans = 0\nnow = -10**9\narr.each do |ele|\n\tif ele[0] >= now\n\t\tans += 1\n\t\tnow = ele[1]\n\tend\nend\n\nputs ans\n"}], "negative_code": [], "src_uid": "c5d15bbebfe57bc7cddb443229fb7d61"} {"nl": {"description": "Bitwise exclusive OR (or bitwise addition modulo two) is a binary operation which is equivalent to applying logical exclusive OR to every pair of bits located on the same positions in binary notation of operands. In other words, a binary digit of the result is equal to 1 if and only if bits on the respective positions in the operands are different.For example, if X = 10910 = 11011012, Y = 4110 = 1010012, then: X xor Y  =  6810  =  10001002. Write a program, which takes two non-negative integers A and B as an input and finds two non-negative integers X and Y, which satisfy the following conditions: A = X + Y B  =  X xor Y, where xor is bitwise exclusive or. X is the smallest number among all numbers for which the first two conditions are true. ", "input_spec": "The first line contains integer number A and the second line contains integer number B (0 ≤ A, B ≤ 264 - 1).", "output_spec": "The only output line should contain two integer non-negative numbers X and Y. Print the only number -1 if there is no answer.", "sample_inputs": ["142\n76"], "sample_outputs": ["33 109"], "notes": null}, "positive_code": [{"source_code": "#Infinity\n#geti,getia\n#Object # dcopy\n#\n#String # to_a\n# \"abc\".to_a #=> [\"a\",\"b\",\"c\"]\n#String # to_ia\n# \"1 2 3\".to_ia #=> [1,2,3]\n#\n#Array # rsort,rsort!\n# [1,3,2,4].rsort #=> [4,3,2,1]\n#\n#Integer # divisor\n# 24.divisor #=> [1,2,3,4,6,8,12,24]\n#\ninclude Math\nrequire \"prime\"\nrequire \"bigdecimal\"\nrequire \"bigdecimal/math\"\nrequire \"matrix\"\nrequire \"pp\"\nrequire \"benchmark\"\n\nInfinity = 1/0.0\nclass Matrix;def inspect;empty? ? \"Matrix.empty(#{row_size}, #{column_size})\":\"Matrix[\\n#{@rows.map(&:inspect).join(\"\\n\")}\\n]\";end;end\ndef pbin(obj);case obj;when Integer;puts obj.to_s(2);end;end\nclass String;def to_a;split(\"\");end;end\nclass String;def to_ia;split.map(&:to_i);end;end\ndef getia;gets.to_ia;end\ndef geti;gets.to_i;end\nclass Object;def dcopy;Marshal.load(Marshal.dump(self));end;end\nclass Array;def rsort;sort.reverse;end;def rsort!;sort!.reverse!;end;end\n\ndef dfs(a,b,x,y,n)\n mask = ((1< a\n for p in 0..1\n for q in 0..1\n s = x | (p * (1<= 0 and (a - b) % 2 == 0\n puts x.to_s + \" \" + (x + b).to_s + \"\\n\"\nelse\n puts \"-1\\n\"\nend\n"}], "negative_code": [], "src_uid": "9c9235394dceba81c8af6be02aa54fcc"} {"nl": {"description": "There is a weighted tree with $$$n$$$ nodes and $$$n-1$$$ edges. The nodes are conveniently labeled from $$$1$$$ to $$$n$$$. The weights are positive integers at most $$$100$$$. Define the distance between two nodes to be the sum of edges on the unique path between the nodes. You would like to find the diameter of the tree. Diameter is the maximum distance between a pair of nodes.Unfortunately, the tree isn't given to you, but you can ask some questions about it. In one question, you can specify two nonempty disjoint sets of nodes $$$p$$$ and $$$q$$$, and the judge will return the maximum distance between a node in $$$p$$$ and a node in $$$q$$$. In the words, maximum distance between $$$x$$$ and $$$y$$$, where $$$x \\in p$$$ and $$$y \\in q$$$. After asking not more than $$$9$$$ questions, you must report the maximum distance between any pair of nodes.", "input_spec": null, "output_spec": null, "sample_inputs": ["2\n5\n9\n6\n10\n9\n10\n2\n99"], "sample_outputs": ["1 4 1 2 3 4 5\n1 4 2 3 4 5 1\n1 4 3 4 5 1 2\n1 4 4 5 1 2 3\n1 4 5 1 2 3 4\n-1 10\n1 1 1 2\n-1 99"], "notes": "NoteIn the first example, the first tree looks as follows: In the first question, we have $$$p = {1}$$$, and $$$q = {2, 3, 4, 5}$$$. The maximum distance between a node in $$$p$$$ and a node in $$$q$$$ is $$$9$$$ (the distance between nodes $$$1$$$ and $$$5$$$).The second tree is a tree with two nodes with an edge with weight $$$99$$$ between them."}, "positive_code": [{"source_code": "def say(s)\n puts s\n STDOUT.flush\nend\n\nreadline.to_i.times do\n n = readline.to_i\n r = 0\n bit = 1\n while bit < n do\n a, b = (1..n).partition { |i| i & bit > 0 }\n say \"#{a.length} #{b.length} #{a.join \" \"} #{b.join \" \"}\"\n r1 = readline.to_i\n if r1 > r then r = r1 end\n if r1 < 0 then exit end\n bit <<= 1\n end\n say \"-1 #{r}\"\nend\n"}, {"source_code": "T = gets.to_i\nT.times do \n n = gets.to_i\n t = 1\n ans = 0\n 9.times do\n l = []\n r = []\n n.times do |i|\n if i & t > 0\n l << i+1\n else\n r << i+1\n end\n end\n if l.length>0 && r.length>0\n puts \"#{l.length} #{r.length} #{l.join(\" \")} #{r.join(\" \")}\"\n STDOUT.flush\n x = gets.to_i\n ans = x if x > ans\n end\n t *= 2\n end\n puts \"-1 #{ans}\"\n STDOUT.flush\nend\n"}], "negative_code": [], "src_uid": "20e2cf707d39c22eaf111f12b2ae6d30"} {"nl": {"description": "There are $$$n$$$ kids, numbered from $$$1$$$ to $$$n$$$, dancing in a circle around the Christmas tree. Let's enumerate them in a clockwise direction as $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$ (all these numbers are from $$$1$$$ to $$$n$$$ and are distinct, so $$$p$$$ is a permutation). Let the next kid for a kid $$$p_i$$$ be kid $$$p_{i + 1}$$$ if $$$i < n$$$ and $$$p_1$$$ otherwise. After the dance, each kid remembered two kids: the next kid (let's call him $$$x$$$) and the next kid for $$$x$$$. Each kid told you which kids he/she remembered: the kid $$$i$$$ remembered kids $$$a_{i, 1}$$$ and $$$a_{i, 2}$$$. However, the order of $$$a_{i, 1}$$$ and $$$a_{i, 2}$$$ can differ from their order in the circle. Example: 5 kids in a circle, $$$p=[3, 2, 4, 1, 5]$$$ (or any cyclic shift). The information kids remembered is: $$$a_{1,1}=3$$$, $$$a_{1,2}=5$$$; $$$a_{2,1}=1$$$, $$$a_{2,2}=4$$$; $$$a_{3,1}=2$$$, $$$a_{3,2}=4$$$; $$$a_{4,1}=1$$$, $$$a_{4,2}=5$$$; $$$a_{5,1}=2$$$, $$$a_{5,2}=3$$$. You have to restore the order of the kids in the circle using this information. If there are several answers, you may print any. It is guaranteed that at least one solution exists.If you are Python programmer, consider using PyPy instead of Python when you submit your code.", "input_spec": "The first line of the input contains one integer $$$n$$$ ($$$3 \\le n \\le 2 \\cdot 10^5$$$) — the number of the kids. The next $$$n$$$ lines contain $$$2$$$ integers each. The $$$i$$$-th line contains two integers $$$a_{i, 1}$$$ and $$$a_{i, 2}$$$ ($$$1 \\le a_{i, 1}, a_{i, 2} \\le n, a_{i, 1} \\ne a_{i, 2}$$$) — the kids the $$$i$$$-th kid remembered, given in arbitrary order.", "output_spec": "Print $$$n$$$ integers $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$ — permutation of integers from $$$1$$$ to $$$n$$$, which corresponds to the order of kids in the circle. If there are several answers, you may print any (for example, it doesn't matter which kid is the first in the circle). It is guaranteed that at least one solution exists.", "sample_inputs": ["5\n3 5\n1 4\n2 4\n1 5\n2 3", "3\n2 3\n3 1\n1 2"], "sample_outputs": ["3 2 4 1 5", "3 1 2"], "notes": null}, "positive_code": [{"source_code": "# Utility\nclass Child\n attr_reader :memorised\n attr_reader :index\n\n def to_s\n @index.to_s\n end\n\n def log\n \"Index: #{@index}. Memory: #{@memorised}\"\n end\n\n def to_i\n @index\n end\n\n def remembers?(e)\n @memorised.any? { |o| o == e.to_i }\n end\n\n def initialize(index, *other)\n @index = index\n case other.length\n when 2\n @memorised = [other[0], other[1]]\n when 1\n @memorised = other[0].split.map { |s| s.to_i }\n end\n end\nend\n\n# Input\nn = gets.to_i\nchildren = []\nn.times { |i| children.push Child.new(i + 1, gets) }\n\nif n == 3\n # Special case\n puts \"1 2 3\"\n exit 0\nend\n\n# Processing\nroundelay = Array.new(n)\nroundelay[0] = children[0]\n1.upto(n - 1) do |i|\n mem = roundelay[i - 1].memorised\n c_1 = children[mem[0] - 1]\n c_2 = children[mem[1] - 1]\n\n #puts roundelay[i - 1].log\n #puts c_1.log\n #puts c_2.log\n #puts\n\n roundelay[i] = c_1.remembers?(c_2) ? c_1 : c_2\nend\n\n# Output\nputs roundelay.join(\" \")\n"}, {"source_code": "n = gets.to_i\n\nhash = Hash.new([])\n(1..n).each do |x|\n hash[x] += gets.chomp.split(\" \").map(&:to_i)\nend\n\narr = []\n\nthefirstpair = hash[1]\nif hash[thefirstpair[0]].include?(thefirstpair[1])\n nextone = thefirstpair[0]\n otherone = thefirstpair[1]\nelse\n nextone = thefirstpair[1]\n otherone = thefirstpair[0]\nend\n\narr.push 1\n\nwhile nextone != 1\n arr.push nextone\n asdf = nextone\n nextone = otherone\n otherone = (hash[asdf] - [nextone])[0]\nend\n\narr.each do |x|\n print x\n print \" \"\nend\n\nputs\n"}], "negative_code": [{"source_code": "# Utility\nclass Child\n attr_reader :memorised\n attr_reader :index\n\n def to_s\n @index.to_s\n end\n\n def to_i\n @index\n end\n\n def remembers?(e)\n @memorised.find_index(e.to_i) != nil\n end\n\n def initialize(index, *other)\n @index = index\n case other.length\n when 2\n @memorised = [other[0], other[1]]\n when 1\n @memorised = other[0].split.map { |s| s.to_i }\n end\n end\nend\n\n# Input\nn = gets.to_i\nchildren = []\nn.times { |i| children.push Child.new(i + 1, gets) }\n\n# Processing\n#p children\nroundelay = Array.new(n)\nroundelay[0] = children[0]\n1.upto(n - 1) do |i|\n mem = roundelay[i - 1].memorised\n #p mem\n roundelay[i] = children[mem[0] - 1].remembers?(mem[1]) ? children[mem[0] - 1] : children[mem[1] - 1]\n #puts roundelay[i]\nend\n\n# Output\nputs roundelay.join(\" \")\n"}], "src_uid": "819d3694fccf2b5af0ec3b4ee429dbb3"} {"nl": {"description": "After reaching your destination, you want to build a new colony on the new planet. Since this planet has many mountains and the colony must be built on a flat surface you decided to flatten the mountains using boulders (you are still dreaming so this makes sense to you). You are given an array $$$h_1, h_2, \\dots, h_n$$$, where $$$h_i$$$ is the height of the $$$i$$$-th mountain, and $$$k$$$ — the number of boulders you have.You will start throwing boulders from the top of the first mountain one by one and they will roll as follows (let's assume that the height of the current mountain is $$$h_i$$$): if $$$h_i \\ge h_{i + 1}$$$, the boulder will roll to the next mountain; if $$$h_i < h_{i + 1}$$$, the boulder will stop rolling and increase the mountain height by $$$1$$$ ($$$h_i = h_i + 1$$$); if the boulder reaches the last mountain it will fall to the waste collection system and disappear. You want to find the position of the $$$k$$$-th boulder or determine that it will fall into the waste collection system.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. Each test case consists of two lines. The first line in each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le n \\le 100$$$; $$$1 \\le k \\le 10^9$$$) — the number of mountains and the number of boulders. The second line contains $$$n$$$ integers $$$h_1, h_2, \\dots, h_n$$$ ($$$1 \\le h_i \\le 100$$$) — the height of the mountains. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.", "output_spec": "For each test case, print $$$-1$$$ if the $$$k$$$-th boulder will fall into the collection system. Otherwise, print the position of the $$$k$$$-th boulder.", "sample_inputs": ["4\n4 3\n4 1 2 3\n2 7\n1 8\n4 5\n4 1 2 3\n3 1\n5 3 1"], "sample_outputs": ["2\n1\n-1\n-1"], "notes": "NoteLet's simulate the first case: The first boulder starts at $$$i = 1$$$; since $$$h_1 \\ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 < h_3$$$. The new heights are $$$[4,2,2,3]$$$. The second boulder starts at $$$i = 1$$$; since $$$h_1 \\ge h_2$$$ the boulder rolls to $$$i = 2$$$; since $$$h_2 \\ge h_3$$$ the boulder rolls to $$$i = 3$$$ and stops there because $$$h_3 < h_4$$$. The new heights are $$$[4,2,3,3]$$$. The third boulder starts at $$$i = 1$$$; since $$$h_1 \\ge h_2$$$ it rolls to $$$i = 2$$$ and stops there because $$$h_2 < h_3$$$. The new heights are $$$[4,3,3,3]$$$. The positions where each boulder stopped are the following: $$$[2,3,2]$$$.In the second case, all $$$7$$$ boulders will stop right at the first mountain rising its height from $$$1$$$ to $$$8$$$.The third case is similar to the first one but now you'll throw $$$5$$$ boulders. The first three will roll in the same way as in the first test case. After that, mountain heights will be equal to $$$[4, 3, 3, 3]$$$, that's why the other two boulders will fall into the collection system.In the fourth case, the first and only boulders will fall straight into the collection system."}, "positive_code": [{"source_code": "t = gets.chomp.to_i\r\nt.times do\r\n n, k = gets.chomp.split(' ').map(&:to_i)\r\n arr = gets.chomp.split(' ').map(&:to_i)\r\n arr << 0\r\n arr << 1000\r\n pik = 0\r\n prev_pik = 0\r\n sum = 0\r\n\r\n while arr[pik] >= arr[pik + 1] \r\n pik += 1\r\n end\r\n # puts \"PIK = #{pik}\"\r\n while (pik != n && sum < k)\r\n prev_pik = pik\r\n sum += [arr[pik + 1], arr[pik - 1] + 1].min - arr[pik]\r\n arr[pik] = [arr[pik + 1], arr[pik - 1] + 1].min\r\n pik = 0\r\n while arr[pik] >= arr[pik + 1] \r\n pik += 1\r\n end\r\n\r\n # puts \"Arr = #{arr.to_s}\"\r\n end\r\n\r\n # puts \"SUM = #{sum}\"\r\n # puts \"PIK = #{pik}\"\r\n # puts \"Arr = #{arr.to_s}\"\r\n if sum < k \r\n puts -1\r\n else\r\n puts prev_pik + 1\r\n end\r\nend"}, {"source_code": "\r\n# int t,n,k;\r\n# cin>>t;\r\n# while(t--)\r\n# {\r\n# cin>>n>>k;\r\n# int a[n];\r\n# for(int i=0;i>a[i];\r\n# int j=0,c=0;\r\n# int p[100000]={0};\r\n# while(true)\r\n# {int f=0;\r\n# for(int i=0;ic)\r\n# cout<<-1<<\"\\n\";\r\n# else\r\n# cout<0 do\r\n b=Array.new(2)\r\n b=gets.chomp.split().map { |e| e.to_i }\r\n n = b[0];\r\n k =b[1];\r\n \r\n a=Array.new(n+1);\r\n i=0;\r\n p=Array.new(100000);\r\n a=gets.chomp.split().map { |e| e.to_i }\r\n \r\n c=0\r\n h=0\r\n while h==0 do\r\n f=0\r\n j=0\r\n while jc\r\n puts -1;\r\n else\r\n puts p[k];\r\n end\r\n t=t-1 \r\nend\r\n"}, {"source_code": "def read\r\n gets.split.map &:to_i\r\nend\r\ngets.to_i.times do\r\n n, k = read\r\n a = read\r\n if n * a.max < k\r\n puts -1\r\n next\r\n end\r\n res = n + 1\r\n k.times do\r\n to = -2\r\n (n - 1).times do |i|\r\n if a[i] < a[i + 1]\r\n to = i\r\n break\r\n end\r\n end\r\n res = to + 1\r\n break if res == -1\r\n a[to] += 1\r\n end\r\n puts res\r\nend"}, {"source_code": "def solve\r\n n, k = ints\r\n h = ints\r\n\r\n ans = -1\r\n k.times do\r\n has_valley = false\r\n (n-1).times do |i|\r\n if h[i] < h[i+1]\r\n ans = i + 1\r\n has_valley = true\r\n h[i] += 1\r\n break\r\n end\r\n end\r\n unless has_valley\r\n ans = -1\r\n break\r\n end\r\n end\r\n puts ans\r\nend\r\n\r\ndef main\r\n t = int\r\n t.times do\r\n solve\r\n end\r\nend\r\n\r\nDEBUG = true\r\nMOD = 10**9+7\r\nYESNO = %w(NO YES)\r\nINF = 10**9\r\n\r\ndef int; gets.to_s.to_i end\r\ndef ints; gets.to_s.split.map { |s| s.to_i } end\r\ndef int1s; gets.to_s.split.map { |s| s.to_i - 1 } end\r\ndef float; gets.to_s.to_f end\r\ndef floats; gets.to_s.split.map { |s| s.to_f } end\r\ndef array_of(&convert); gets.to_s.split.map(&convert) end\r\ndef string; gets.to_s.chomp end\r\ndef rep(n, &b); Array.new(n, &b) end\r\ndef yes; puts YESNO[1] end\r\ndef no; puts YESNO[0] end\r\ndef yesno t; puts YESNO[t] end\r\ndef zip(xs, *yss); Enumerator.new { |y| xs.zip(*yss) { |a| y.yield(*a) } } end\r\ndef max(*xs, &block); block_given? ? xs.max_by(&block) : xs.max end\r\ndef min(*xs, &block); block_given? ? xs.min_by(&block) : xs.min end\r\ndef minmax(*xs, &block); block_given? ? xs.minmax_by(&block) : xs.minmax end\r\ndef gcd(*xs); xs.inject(0, :gcd) end\r\ndef matrix(h, w, fill=nil, &block); return Array.new(h) { Array.new(w, &block) } if block_given?; Array.new(h) { [fill] * w } end\r\ndef debug(x = nil); STDERR.puts (block_given? ? yield(x) : x).inspect if DEBUG; x end\r\ndef debug_grid(grid, width = 1); grid.each { |row| STDERR.puts row.map { |x| x.inspect.ljust(width) }.join(\"\") } if DEBUG; grid end\r\ndef if_debug; yield if DEBUG end\r\n\r\nmodule Boolean\r\n def coerce(other); [other, to_i] end\r\n def +@; to_i end\r\n def to_int; to_i end\r\n def *(other); to_i * other end\r\nend\r\n\r\nclass TrueClass\r\n include Boolean\r\n def to_i; 1 end\r\nend\r\n\r\nclass FalseClass\r\n include Boolean\r\n def to_i; 0 end\r\nend\r\n\r\nclass Integer\r\n def div_ceil(y); (self + y - 1) / y end\r\n def mod_inv(mod = MOD); pow(mod-2, mod) end\r\n def mod_div(y, mod = MOD); self * mod_inv(y, mod) % mod end\r\n def factorial(mod = MOD); (2..self).inject(1) { |f, x| f * x % mod } end\r\n def popcount; x = self; c = 0; while x > 0; c += 1 if x & 1 == 1; x >>= 1 end; c end #TODO: faster\r\n def bitbrute(&block); (1< 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\r\n def lower_bound; ac, wa = self.end_open, self.begin; while ac - wa > 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\r\n def shakutori(r2, &pred); Enumerator.new { |y| j, r = r2.begin, r2.end_open; each { |i| j += 1 while j + 1 < r and pred[i, j+1]; y.yield(i, j) } }; end\r\n def widest(&block); Enumerator.new { |y| j, n = self.begin, self.end_open; each { |i| j += 1 while j < n and block[i, j]; y.yield(i, j) if block[i, j] } } end\r\nend\r\n\r\nclass Array\r\n def power(&block); (0 ... 1 << size).each(&block) end\r\n def sorted_merge(other); a = []; i = j = 0; n, m = size, other.size; if j < m and other[j] < self[i]; a << other[j]; j += 1 else; a << self[i]; i += 1 end while i < n; a.push(*other[j..-1]) if j < m; a end\r\n def upper_bound; ac, wa = 0, size; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\r\n def lower_bound; ac, wa = size, 0; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\r\n def cum(*xs, &op); a = []; a << xs[0] if xs.size > 0; a << x = self[0]; (1...size).each { |i| a << x = op[x, self[i]] }; a end\r\n def cumdiff(range); self[range.end_open] - self[range.begin]; end\r\nend\r\n\r\nmodule Enumerable\r\n def sorted_uniq; x = nil; filter { |y| c = x === y; x = y; !c } end\r\n def cumsum; ys = [0]; each { |x| ys << x + ys[-1] }; ys end\r\nend\r\n\r\nmain"}, {"source_code": "gets.to_i.times do\r\n n,k=gets.split.map(&:to_i)\r\n h=gets.split.map(&:to_i)\r\n f=false\r\n k.times do |kk|\r\n f=false\r\n (n-1).times do |i|\r\n if h[i]= arr[pik + 1] \r\n pik += 1\r\n end\r\n # puts \"PIK = #{pik}\"\r\n while (pik != n && sum < k)\r\n prev_pik = pik\r\n sum += arr[pik + 1] - arr[pik]\r\n arr[pik] = arr[pik + 1]\r\n pik = 0\r\n while arr[pik] >= arr[pik + 1] \r\n pik += 1\r\n end\r\n\r\n end\r\n\r\n # puts \"SUM = #{sum}\"\r\n # puts \"PIK = #{pik}\"\r\n # puts \"Arr = #{arr.to_s}\"\r\n if sum < k \r\n puts -1\r\n else\r\n puts prev_pik + 1\r\n end\r\nend"}], "src_uid": "32855bb8ba33973178fde7c3d0beb2ce"} {"nl": {"description": "The mobile application store has a new game called \"Subway Roller\".The protagonist of the game Philip is located in one end of the tunnel and wants to get out of the other one. The tunnel is a rectangular field consisting of three rows and n columns. At the beginning of the game the hero is in some cell of the leftmost column. Some number of trains rides towards the hero. Each train consists of two or more neighbouring cells in some row of the field.All trains are moving from right to left at a speed of two cells per second, and the hero runs from left to right at the speed of one cell per second. For simplicity, the game is implemented so that the hero and the trains move in turns. First, the hero moves one cell to the right, then one square up or down, or stays idle. Then all the trains move twice simultaneously one cell to the left. Thus, in one move, Philip definitely makes a move to the right and can move up or down. If at any point, Philip is in the same cell with a train, he loses. If the train reaches the left column, it continues to move as before, leaving the tunnel.Your task is to answer the question whether there is a sequence of movements of Philip, such that he would be able to get to the rightmost column. ", "input_spec": "Each test contains from one to ten sets of the input data. The first line of the test contains a single integer t (1 ≤ t ≤ 10 for pretests and tests or t = 1 for hacks; see the Notes section for details) — the number of sets. Then follows the description of t sets of the input data. The first line of the description of each set contains two integers n, k (2 ≤ n ≤ 100, 1 ≤ k ≤ 26) — the number of columns on the field and the number of trains. Each of the following three lines contains the sequence of n character, representing the row of the field where the game is on. Philip's initial position is marked as 's', he is in the leftmost column. Each of the k trains is marked by some sequence of identical uppercase letters of the English alphabet, located in one line. Distinct trains are represented by distinct letters. Character '.' represents an empty cell, that is, the cell that doesn't contain either Philip or the trains.", "output_spec": "For each set of the input data print on a single line word YES, if it is possible to win the game and word NO otherwise.", "sample_inputs": ["2\n16 4\n...AAAAA........\ns.BBB......CCCCC\n........DDDDD...\n16 4\n...AAAAA........\ns.BBB....CCCCC..\n.......DDDDD....", "2\n10 4\ns.ZZ......\n.....AAABB\n.YYYYYY...\n10 4\ns.ZZ......\n....AAAABB\n.YYYYYY..."], "sample_outputs": ["YES\nNO", "YES\nNO"], "notes": "NoteIn the first set of the input of the first sample Philip must first go forward and go down to the third row of the field, then go only forward, then go forward and climb to the second row, go forward again and go up to the first row. After that way no train blocks Philip's path, so he can go straight to the end of the tunnel.Note that in this problem the challenges are restricted to tests that contain only one testset."}, "positive_code": [{"source_code": "\ndef dp(pos, turn)\n\treturn $dp[3*turn+pos] if $dp[3*turn+pos] != nil\n\treturn $dp[3*turn+pos]=false if $state[pos][3*turn] != \".\"\n\treturn $dp[3*turn+pos]=false if $state[pos][3*turn+1] != \".\"\n\treturn $dp[3*turn+pos]=false if $state[pos][3*turn-1] != \".\"\n\treturn $dp[3*turn+pos]=false if $state[pos][3*turn-2] != \".\"\n\treturn $dp[3*turn+pos]=true if 3*turn >= $cols\n\n\tif pos == 0\n\t\treturn $dp[3*turn+pos] = dp(0, turn+1) || dp(1, turn+1)\n\tend\n\n\tif pos == 1\n\t\treturn $dp[3*turn+pos] = dp(0, turn+1) || dp(1, turn+1) || dp(2, turn+1)\n\tend\n\n\tif pos == 2\n\t\treturn $dp[3*turn+pos] = dp(1, turn+1) || dp(2, turn+1)\n\tend\nend\n\ngets.to_i.times do\n\t$cols, $k = *gets.split(\" \").map(&:to_i)\n\n\t$state = [\n\t\tgets.strip+\".\"*500,\n\t\tgets.strip+\".\"*500,\n\t\tgets.strip+\".\"*500\n\t]\n\n\t$dp = [nil]*$cols\n\n\tinit = $state[0][0] == \"s\" ? 0 : $state[1][0] == \"s\" ? 1 : 2\n\t$state[init][0] = \".\"\n\n\tputs dp(init, 0) ? \"YES\" : \"NO\"\n\t#p $dp\n\nend\n"}], "negative_code": [], "src_uid": "5c1707b614dc3326a9bb092e6ca24280"} {"nl": {"description": "Vanya smashes potato in a vertical food processor. At each moment of time the height of the potato in the processor doesn't exceed h and the processor smashes k centimeters of potato each second. If there are less than k centimeters remaining, than during this second processor smashes all the remaining potato.Vanya has n pieces of potato, the height of the i-th piece is equal to ai. He puts them in the food processor one by one starting from the piece number 1 and finishing with piece number n. Formally, each second the following happens: If there is at least one piece of potato remaining, Vanya puts them in the processor one by one, until there is not enough space for the next piece. Processor smashes k centimeters of potato (or just everything that is inside). Provided the information about the parameter of the food processor and the size of each potato in a row, compute how long will it take for all the potato to become smashed.", "input_spec": "The first line of the input contains integers n, h and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ h ≤ 109) — the number of pieces of potato, the height of the food processor and the amount of potato being smashed each second, respectively. The second line contains n integers ai (1 ≤ ai ≤ h) — the heights of the pieces.", "output_spec": "Print a single integer — the number of seconds required to smash all the potatoes following the process described in the problem statement.", "sample_inputs": ["5 6 3\n5 4 3 2 1", "5 6 3\n5 5 5 5 5", "5 6 3\n1 2 1 1 1"], "sample_outputs": ["5", "10", "2"], "notes": "NoteConsider the first sample. First Vanya puts the piece of potato of height 5 into processor. At the end of the second there is only amount of height 2 remaining inside. Now Vanya puts the piece of potato of height 4. At the end of the second there is amount of height 3 remaining. Vanya puts the piece of height 3 inside and again there are only 3 centimeters remaining at the end of this second. Vanya finally puts the pieces of height 2 and 1 inside. At the end of the second the height of potato in the processor is equal to 3. During this second processor finally smashes all the remaining potato and the process finishes. In the second sample, Vanya puts the piece of height 5 inside and waits for 2 seconds while it is completely smashed. Then he repeats the same process for 4 other pieces. The total time is equal to 2·5 = 10 seconds.In the third sample, Vanya simply puts all the potato inside the processor and waits 2 seconds."}, "positive_code": [{"source_code": "#!/usr/bin/ruby\nN,H,K=gets.split.map(&:to_i)\nr=h=0\ngets.split.map(&:to_i).each{|x|\n\tif h+x<=H\n\t\th+=x\n\telse\n\t\tr+=1\n\t\th=x\n\tend\n\tr+=h/K\n\th%=K\n}\np r+(h+K-1)/K"}, {"source_code": "n, h, k = readline.split(' ').collect(&:to_i)\narr = readline.split(' ').collect(&:to_i)\nleft = 0\nct = 0\narr.each do |num|\n if left + num > h\n left = num\n ct += 1\n else\n left += num\n end\n ct += (left / k)\n left %= k\nend\n\nct += 1 if left > 0\nputs ct\n"}, {"source_code": "n, h, k = gets.split.map { |x| x.to_i }\na = gets.split.map { |x| x.to_i }\nans = 0\nsum = 0\na.each do |now|\n\tcut = [(now - (h - sum) + k - 1) / k, 0].max\n\tans += cut\n\tsum = [sum - cut * k, 0].max\n\tsum += now\nend\nans += (sum + k - 1) / k\nputs ans"}, {"source_code": "n, h, k = gets.chomp.split.map(&:to_i)\nas = gets.chomp.split.map(&:to_i)\n\nt = 0\nl = 0\n\nfor i in 0...n\n t += l / k\n l = l % k\n if h < l + as[i]\n t += 1\n l = 0\n end\n l += as[i]\nend\nt += (l + k - 1) / k\n\nputs t\n"}, {"source_code": "n, h, k = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\nans = 0\nleft = 0\ni = 0\nwhile i < n\n ans += (left + a[i]) / k\n left = (left + a[i]) % k\n if i == n - 1 && left > 0\n ans += 1\n elsif i != n - 1 && left != 0 && left + a[i + 1] > h\n ans += 1\n left = 0\n end\n i += 1\nend\nputs ans\n"}, {"source_code": "n,h,k=gets.split.map(&:to_i)\ns=r=0\ngets.split.each{|p|p=p.to_i\nr+=p\nr,s=p,s+1if r>h\ns+=r/k\nr%=k}\ns+=1if r>0\np s\n\n\n"}, {"source_code": "totalPotatos, processorHeight, smashUnits = gets.split.map(&:to_i)\nseconds = 0\nremain = 0\ngets.split.map(&:to_i).each { |potato|\n if remain + potato <= processorHeight\n remain += potato\n else\n seconds += 1\n remain = potato\n end\n seconds += remain / smashUnits\n remain %= smashUnits\n}\nseconds += 1 if remain > 0\nputs seconds\n\n\n"}, {"source_code": "n, h, k = gets.split.map { |x| x.to_i }\na = gets.split.map { |x| x.to_i }\n\nans = 0\nsum = 0\na.each do |now|\n\tcut = [(now - (h - sum) + k - 1) / k, 0].max\n\tans += cut\n\tsum = [sum - cut * k, 0].max\n\tsum += now\nend\nans += (sum + k - 1) / k\nputs ans\n"}, {"source_code": "$now = 0\n\n$n, $h, $k = gets.chomp.split(' ').map { |i| i.to_i }\n$potatos = gets.chomp.split(' ').map { |i| i.to_i }\n\ndef put_potato\n\t#puts \"Before put_potato : #{$inQueue}\"\n\t#puts \"Now : #{$now}\"\n\n\tuntil $inQueue + $potatos[$now] > $h\n\t#\tputs \"Now inQueue : #{$inQueue}\"\n\t\t$inQueue += $potatos[$now]\n\t\t$now += 1\n\n\t\tbreak if $now == $potatos.length\n\tend\n\n\t#puts \"After put_potato : #{$inQueue}\"\nend\n\ntime = 0\n$inQueue = 0\nput_potato\n\nuntil $now == $potatos.length\n\tnn = ($inQueue / $k)\n\ttime += nn\n\ttime += 1 if nn == 0\n\t$inQueue -= (nn * $k)\n\t$inQueue = 0 if nn == 0\n\tput_potato\nend\n\ntime += ($inQueue / $k)\ntime += 1 if ($inQueue % $k > 0)\n\nputs time"}], "negative_code": [{"source_code": "now = 0\n\nn, h, k = gets.chomp.split(' ').map { |i| i.to_i }\npotatos = gets.chomp.split(' ').map { |i| i.to_i }\n\ntime = 0\nremain = potatos[0]\n\ni = 0\n\nuntil i >= (potatos.length - 1)\n\ttime += 1\n\tremain -= k\n\t#puts \"Remain : #{remain}\"\n\t#puts \"Next potatos : #{potatos[i + 1]}\"\n\tif remain + potatos[i + 1] <= h\n\t\tremain += potatos[i + 1]\n\n\t#puts \"Insidle Remain : #{remain}\"\n\n\t\tif remain < 0\n\t\t\ti += 1\n\t\t\tuntil remain >= 0\n\t\t\t\t#puts \"Interpol Remain : #{remain}\"\n\t\t\t\tbreak if i >= potatos.length\n\t\t\t\tremain += potatos[i + 1]\n\t\t\t\ti += 1\n\t\t\tend\n\t\tend\n\telse\n\t\tremain = potatos[i + 1]\n\t\ttime += 1\n\tend\n\n\ti += 1\nend\n\ntime += (remain / k)\ntime += 1 if remain % k > 0\n\nputs time"}], "src_uid": "5099a9ae62e82441c496ac37d92e99e3"} {"nl": {"description": "Sereja is a coder and he likes to take part in Codesorfes rounds. However, Uzhland doesn't have good internet connection, so Sereja sometimes skips rounds.Codesorfes has rounds of two types: Div1 (for advanced coders) and Div2 (for beginner coders). Two rounds, Div1 and Div2, can go simultaneously, (Div1 round cannot be held without Div2) in all other cases the rounds don't overlap in time. Each round has a unique identifier — a positive integer. The rounds are sequentially (without gaps) numbered with identifiers by the starting time of the round. The identifiers of rounds that are run simultaneously are different by one, also the identifier of the Div1 round is always greater.Sereja is a beginner coder, so he can take part only in rounds of Div2 type. At the moment he is taking part in a Div2 round, its identifier equals to x. Sereja remembers very well that he has taken part in exactly k rounds before this round. Also, he remembers all identifiers of the rounds he has taken part in and all identifiers of the rounds that went simultaneously with them. Sereja doesn't remember anything about the rounds he missed.Sereja is wondering: what minimum and what maximum number of Div2 rounds could he have missed? Help him find these two numbers.", "input_spec": "The first line contains two integers: x (1 ≤ x ≤ 4000) — the round Sereja is taking part in today, and k (0 ≤ k < 4000) — the number of rounds he took part in. Next k lines contain the descriptions of the rounds that Sereja took part in before. If Sereja took part in one of two simultaneous rounds, the corresponding line looks like: \"1 num2 num1\" (where num2 is the identifier of this Div2 round, num1 is the identifier of the Div1 round). It is guaranteed that num1 - num2 = 1. If Sereja took part in a usual Div2 round, then the corresponding line looks like: \"2 num\" (where num is the identifier of this Div2 round). It is guaranteed that the identifiers of all given rounds are less than x.", "output_spec": "Print in a single line two integers — the minimum and the maximum number of rounds that Sereja could have missed.", "sample_inputs": ["3 2\n2 1\n2 2", "9 3\n1 2 3\n2 8\n1 4 5", "10 0"], "sample_outputs": ["0 0", "2 3", "5 9"], "notes": "NoteIn the second sample we have unused identifiers of rounds 1, 6, 7. The minimum number of rounds Sereja could have missed equals to 2. In this case, the round with the identifier 1 will be a usual Div2 round and the round with identifier 6 will be synchronous with the Div1 round. The maximum number of rounds equals 3. In this case all unused identifiers belong to usual Div2 rounds."}, "positive_code": [{"source_code": "x, k = gets.split.map(&:to_i)\nc_min, c_max = 0\nx -= 1\nwas = [false] * x\nwas[x] = true\nfor i in 1..k\n d = gets.split.map(&:to_i)\n for j in 1..d.length-1\n was[d[j] - 1] = true\n end\nend\nc_max = was.count { |x| not x }\ni = 0\nwhile i < x\n if not was[i]\n was[i] = was[i + 1] = true\n c_min += 1\n end\n i += 1\nend\nputs \"#{c_min} #{c_max}\""}, {"source_code": "class ProblemA\n def initialize\n @dats = STDIN.read.split(\"\\n\")\n @current_round_iden, @num_of_took_rounds = @dats[0].split(\" \").map{|x| x.to_i}\n end\n\n def solve\n if @num_of_took_rounds == 0\n max = @current_round_iden-1\n min = @current_round_iden % 2 == 0 ? @current_round_iden / 2 : (@current_round_iden) / 2 \n else \n\n @unused_indentifiers = []\n @remember_rounds = []\n (1..@dats.length-1).each do |line_num|\n line = @dats[line_num]\n line = line.split(\" \")\n line.shift\n line.each { |elem| @remember_rounds.push(elem.to_i) }\n end\n\n (@current_round_iden-1).downto(1) do |elem|\n @unused_indentifiers.push(elem) if not @remember_rounds.include?(elem) \n end\n\n min = find_min_possible(@unused_indentifiers)\n max = find_max_possible(@unused_indentifiers)\n end\n print \"#{min} #{max}\" \n end\n\n def find_min_possible(indentifier_array)\n ret = 0\n idx = 0\n indentifier_array.sort!\n \n while idx < indentifier_array.length\n if idx == indentifier_array.length - 1\n ret += 1\n return ret\n elsif indentifier_array[idx+1] == indentifier_array[idx]+1\n ret += 1\n idx += 2\n else\n ret += 1\n idx += 1\n end\n end\n return ret\n end\n\n def find_max_possible(indentifier_array)\n indentifier_array.length\n end\nend\n\nsolver = ProblemA.new\nsolver.solve\n"}, {"source_code": "def rsm(&block)\n readline.split.map{|i| block.yield(i)}\nend\n\ndef rsmi\n rsm {|i| i.to_i}\nend\n\nx, k = rsmi\na = Array.new(x - 1, 0)\nk.times {\n r = rsmi\n t, x = r[0], r[1]\n a[x - 1] = 2\n if t == 1\n y = r[2]\n a[y - 1] = 1\n end\n\n}\n\ns1 = a.count {|i| i == 0}\ns2 = 0\na.length.times { |i|\n if a[i] == 0\n s2 += 1\n a[i] = 2\n if i + 1 < a.length\n a[i + 1] = 1 if a[i + 1] == 0\n end\n end\n}\nprintf(\"%d %d\", s2, s1)"}, {"source_code": "n,k = gets.chomp!.split.map { |e| e.to_i}\n\nA = Array.new(n) { |i| 0}\n\nk.times { \n c,d1,d2 = gets.chomp!.split.map { |e| e.to_i}\n A[d1-1] = 1\n if d2 != nil\n A[d2-1] = 1\n end\n}\n\n\nmx=0\nmi=0\ncur=0\n(n-1).times do |i|\n if A[i] == 0\n cur += 1\n else\n mx += cur\n mi += (cur+1)/2\n cur = 0 \n end \nend\n\n\nmx += cur\nmi += (cur+1)/2\n\nputs \"#{mi} #{mx}\"\n"}, {"source_code": "x, k = gets.split.collect{|i| i.to_i}\nmark = Array.new(x-1,0)\n(1..k).each do\n tmp = gets.split.collect{|i| i.to_i}\n mark[tmp[1]-1] = 1\n mark[tmp[2]-1] = 1 if tmp.size == 3\nend\nmaxans = mark.count(0)\nminans = maxans\ntmp = 1\nwhile tmp < x\n if mark[tmp] == 0 and mark[tmp-1] == 0\n minans -= 1\n tmp += 1\n end\n tmp += 1\nend\nprint minans, \" \", maxans, \"\\n\"\n"}, {"source_code": "def get_pair_num(diff_ary,pair_num)\n\tif diff_ary.empty?\n\t\treturn pair_num\n\telse\n\t\tif diff_ary.length > 1\n\t\t\tif (diff_ary[0] == diff_ary[1] - 1)\n\t\t\t\tdiff_ary.delete_at(0)\n\t\t\t\tdiff_ary.delete_at(0)\n\t\t\t\treturn get_pair_num(diff_ary,pair_num+1)\n\t\t\telse\n\t\t\t\tdiff_ary.shift\n\t\t\t\treturn get_pair_num(diff_ary,pair_num)\n\t\t\tend\n\t\tend\n\t\treturn pair_num\n\tend\nend\n\ndef check_diff(id_ary,max_id)\n\tpair_num = 0\n\tfull_ary = []\n\tfor i in 1..max_id\n\t\tfull_ary << i\n\tend\n\tdiff_ary = full_ary - id_ary\n\tdiff_ary.sort!\n\tpair_num = get_pair_num(diff_ary.clone, 0)\n\tmax_num = diff_ary.length\n\tmin_num = diff_ary.length - pair_num\n\n\treturn [min_num, max_num]\n\nend\n\nfirst_line = (gets()).split()\ncurrent_id = (first_line.shift).to_i\npart_in_num = (first_line.pop).to_i\nid_ary = []\nfor i in 1..part_in_num\n\tary = []\n\t(gets().split).each { |e| ary << e.to_i }\n\tary.shift\n\tary.each { |e| id_ary << e }\nend\nresult = check_diff(id_ary, current_id-1)\nputs result[0].to_s + \" \" + result[1].to_s"}, {"source_code": "def solve(x, a)\n ids = Array.new(x + 1, false)\n a.each { |id|\n ids[id[1]] = true\n ids[id[2]] = true if id[0] == 1\n }\n ids[x] = true\n\n div1 = div2 = 0\n i = 1\n while i < x\n unless ids[i]\n if ids[i + 1]\n div2 += 1\n else\n div1 += 1\n i += 1\n end\n end\n i += 1\n end\n [div1 + div2, div1 * 2 + div2]\nend\n\ndef read_next_line\n gets.chomp.split.map(&:to_i)\nend\n\nif __FILE__ == $0\n x, k = read_next_line\n a = (0...k).map { read_next_line }\n puts solve(x, a) * ' '\nend"}, {"source_code": "#f = File.open('test1.txt', 'r')\nf = STDIN\nx, k = f.gets.chomp.split.map(&:to_i)\na = Array.new(x, false)\na[0] = true\nk.times do\n tmp = f.gets.chomp.split.map(&:to_i)\n if tmp[0] == 1\n a[tmp[1]], a[tmp[2]] = true\n else\n a[tmp[1]] = true\n end\nend\nmaximum = a.count false\nminimum = 0\n(0...x).each do |i|\n if a[i] == false\n minimum += 1\n a[i+1] = true\n end\nend\nputs \"#{minimum} #{maximum}\""}, {"source_code": "i=->(x){x.split.map(&:to_i)}\nn,_=i[gets]\na=$<.drop(0).collect{|x|i[x][1..-1]}.push([0,n]).flatten.sort\nx,y=0,0\n(a.size-1).times do |k|\n\tz=a[k+1]-a[k]-1\n\tx,y=x+(z+1)/2,y+z\nend\nputs \"#{x} #{y}\"\n"}, {"source_code": "a=gets[/\\d+/].to_i;\nb=Array(0..a-1);\nd=e=0;\nc=STDIN.read.scan(/ \\d+/).map{|z|z.sub(/ /,'').to_i};\nb.map{|x|x=c.include?(x)?1:0}.join.sub(/./,'').gsub(/0/){|y|0*(d+=1)}.gsub(/00|0/){|y|0*(e+=1)}; \nprint e,' ',d;"}], "negative_code": [{"source_code": "class ProblemA\n def initialize\n @dats = STDIN.read.split(\"\\n\")\n @current_round_iden, @num_of_took_rounds = @dats[0].split(\" \").map{|x| x.to_i}\n end\n\n def solve\n if @num_of_took_rounds == 0\n max = @current_round_iden-1\n min = @current_round_iden % 2 == 0 ? @current_round_iden / 2 : @current_round_iden / 2 + 1\n else \n\n @unused_indentifiers = []\n @remember_rounds = []\n (1..@dats.length-1).each do |line_num|\n line = @dats[line_num]\n line = line.split(\" \")\n line.shift\n line.each { |elem| @remember_rounds.push(elem.to_i) }\n end\n\n (@current_round_iden-1).downto(1) do |elem|\n @unused_indentifiers.push(elem) if not @remember_rounds.include?(elem) \n end\n\n min = find_min_possible(@unused_indentifiers)\n max = find_max_possible(@unused_indentifiers)\n end\n print \"#{min} #{max}\" \n end\n\n def find_min_possible(indentifier_array)\n ret = 0\n idx = 0\n indentifier_array.sort!\n \n while idx < indentifier_array.length\n if idx == indentifier_array.length - 1\n ret += 1\n end\n if indentifier_array[idx+1] == indentifier_array[idx]+1\n ret += 1\n idx += 2\n else\n ret += 1\n idx += 1\n end\n end\n return ret\n end\n\n def find_max_possible(indentifier_array)\n indentifier_array.length\n end\nend\n\nsolver = ProblemA.new\nsolver.solve\n"}, {"source_code": "class ProblemA\n def initialize\n @dats = STDIN.read.split(\"\\n\")\n @cards_num, @max_abs = @dats[0].split(\" \").map{|x| x.to_i}\n @cards = @dats[1].split(\" \").map{|x| x.to_i}\n end\n\n def solve\n sum = @cards.inject(:+).abs\n return 1 if sum <= @max_abs\n return 0 if @max_abs == 0\n\n count_ret = 0\n (@max_abs-1).downto(1) do |elem|\n if sum >= elem\n count_ret += 1\n sum -= elem\n elsif sum == 1\n return count_ret+1 \n elsif sum == 0\n return count_ret\n end\n end\n return count_ret\n end\nend\n\nsolver = ProblemA.new\np solver.solve\n"}, {"source_code": "class ProblemA\n def initialize\n @dats = STDIN.read.split(\"\\n\")\n @current_round_iden, @num_of_took_rounds = @dats[0].split(\" \").map{|x| x.to_i}\n end\n\n def solve\n if @num_of_took_rounds == 0\n max = @current_round_iden-1\n min = @current_round_iden % 2 == 0 ? @current_round_iden / 2 : @current_round_iden / 2 + 1\n else \n\n @unused_indentifiers = []\n @remember_rounds = []\n (1..@dats.length-1).each do |line_num|\n line = @dats[line_num]\n line = line.split(\" \")\n line.shift\n line.each { |elem| @remember_rounds.push(elem.to_i) }\n end\n\n (@current_round_iden-1).downto(1) do |elem|\n @unused_indentifiers.push(elem) if not @remember_rounds.include?(elem) \n end\n\n min = find_min_possible(@unused_indentifiers)\n max = find_max_possible(@unused_indentifiers)\n end\n print \"#{min} #{max}\" \n end\n\n def find_min_possible(indentifier_array)\n ret = 0\n idx = 0\n indentifier_array.sort!\n \n while idx < indentifier_array.length\n if idx == indentifier_array.length - 1\n ret += 1\n return ret\n elsif indentifier_array[idx+1] == indentifier_array[idx]+1\n ret += 1\n idx += 2\n else\n ret += 1\n idx += 1\n end\n end\n return ret\n end\n\n def find_max_possible(indentifier_array)\n indentifier_array.length\n end\nend\n\nsolver = ProblemA.new\nsolver.solve\n"}, {"source_code": "class ProblemA\n def initialize\n @dats = STDIN.read.split(\"\\n\")\n @current_round_iden, @num_of_took_rounds = @dats[0].split(\" \").map{|x| x.to_i}\n end\n\n def solve\n if @num_of_took_rounds == 0\n max = @current_round_iden-1\n min = @current_round_iden % 2 == 0 ? @current_round_iden / 2 : (@current_round_iden-1) / 2 + 1\n else \n\n @unused_indentifiers = []\n @remember_rounds = []\n (1..@dats.length-1).each do |line_num|\n line = @dats[line_num]\n line = line.split(\" \")\n line.shift\n line.each { |elem| @remember_rounds.push(elem.to_i) }\n end\n\n (@current_round_iden-1).downto(1) do |elem|\n @unused_indentifiers.push(elem) if not @remember_rounds.include?(elem) \n end\n\n min = find_min_possible(@unused_indentifiers)\n max = find_max_possible(@unused_indentifiers)\n end\n print \"#{min} #{max}\" \n end\n\n def find_min_possible(indentifier_array)\n ret = 0\n idx = 0\n indentifier_array.sort!\n \n while idx < indentifier_array.length\n if idx == indentifier_array.length - 1\n ret += 1\n return ret\n elsif indentifier_array[idx+1] == indentifier_array[idx]+1\n ret += 1\n idx += 2\n else\n ret += 1\n idx += 1\n end\n end\n return ret\n end\n\n def find_max_possible(indentifier_array)\n indentifier_array.length\n end\nend\n\nsolver = ProblemA.new\nsolver.solve\n"}, {"source_code": "def answer(id_ary,max_id)\n\tpair_num = 0\n\tfull_ary = []\n\tfor i in 1..max_id\n\t\tfull_ary << i\n\tend\n\tdiff_ary = full_ary - id_ary\n\tif diff_ary.empty?\n\t\treturn [0,0]\n\telse\n\t\tfor i in 0..(diff_ary.length-1)\n\t\t\tif (i != diff_ary.length-1)\n\t\t\t\tif diff_ary[i] == diff_ary[i+1] - 1\n\t\t\t\t\tpair_num += 1\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\tend\n\tmax_num = diff_ary.length\n\tmin_num = diff_ary.length - pair_num\n\n\treturn [min_num, max_num]\n\nend\n\nfirst_line = (gets()).split()\ncurrent_id = (first_line.shift).to_i\npart_in_num = (first_line.pop).to_i\nid_ary = []\nfor i in 1..part_in_num\n\tary = []\n\t(gets().split).each { |e| ary << e.to_i }\n\tary.shift\n\tary.each { |e| id_ary << e }\nend\nresult = answer(id_ary, current_id-1)\nputs result[0].to_s + \" \" + result[1].to_s"}, {"source_code": "def answer(id_ary,max_id)\n\tpair_num = 0\n\tfull_ary = []\n\tfor i in 1..max_id\n\t\tfull_ary << i\n\tend\n\tdiff_ary = full_ary - id_ary\n\tif diff_ary.empty?\n\t\treturn [0,0]\n\telse\n\t\tfor i in 0..(diff_ary.length-1)\n\t\t\tif (i != diff_ary.length-1)\n\t\t\t\tif diff_ary[i] == diff_ary[i+1] - 1\n\t\t\t\t\tpair_num += 1\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\tend\n\tmax_num = diff_ary.length\n\tmin_num = diff_ary.length - pair_num\n\n\treturn [min_num, max_num]\n\nend\n\nfirst_line = (gets()).split()\ncurrent_id = (first_line.shift).to_i\npart_in_num = (first_line.pop).to_i\nid_ary = []\nfor i in 1..part_in_num\n\tary = []\n\t(gets().split).each { |e| ary << e.to_i }\n\tary.shift\n\tary.each { |e| id_ary << e }\nend\nresult = answer(id_ary, current_id-1)\nputs result[0].to_s + result[1].to_s"}, {"source_code": "i=->(x){x.split.map(&:to_i)}\nn,_=i[gets]\na=$<.drop(0).collect{|x|i[x][1..-1]}.flatten.push(n).sort\nx,y=0,0\n(a.size-1).times do |k|\n\tz=a[k+1]-a[k]\n\tnext if z == 1\n\tx,y=x+(z+1)/2,y+z\nend\nputs \"#{x} #{y}\"\n"}, {"source_code": "i=->(x){x.split.map(&:to_i)}\nn,_=i[gets]\na=$<.drop(0).collect{|x|i[x][1..-1]}.flatten.push(n).push(1).sort\nx,y=0,0\n(a.size-1).times do |k|\n\tz=a[k+1]-a[k]\n\tnext if z <= 1\n\tx,y=x+(z+1)/2,y+z\nend\nputs \"#{x} #{y}\"\n"}], "src_uid": "fb77c9339250f206e7188b951902a221"} {"nl": {"description": "The Little Elephant enjoys recursive functions.This time he enjoys the sorting function. Let a is a permutation of an integers from 1 to n, inclusive, and ai denotes the i-th element of the permutation. The Little Elephant's recursive function f(x), that sorts the first x permutation's elements, works as follows: If x = 1, exit the function. Otherwise, call f(x - 1), and then make swap(ax - 1, ax) (swap the x-th and (x - 1)-th elements of a). The Little Elephant's teacher believes that this function does not work correctly. But that-be do not get an F, the Little Elephant wants to show the performance of its function. Help him, find a permutation of numbers from 1 to n, such that after performing the Little Elephant's function (that is call f(n)), the permutation will be sorted in ascending order.", "input_spec": "A single line contains integer n (1 ≤ n ≤ 1000) — the size of permutation.", "output_spec": "In a single line print n distinct integers from 1 to n — the required permutation. Numbers in a line should be separated by spaces. It is guaranteed that the answer exists.", "sample_inputs": ["1", "2"], "sample_outputs": ["1", "2 1"], "notes": null}, "positive_code": [{"source_code": "puts [*1..gets.to_i].rotate(-1)*' '"}, {"source_code": "$><<[n=gets.to_i,*1..n-1]*\" \"\n"}, {"source_code": "n = gets.to_i\nprint n,' ', (1...n).to_a.join(' ') \n"}, {"source_code": "print n = gets.to_i,' ', (1...n).to_a.join(' ') \n"}, {"source_code": "def main()\n n = gets.chomp.to_i\n p = [n]\n \n 1.upto(n - 1) do |i|\n p << i\n end\n \n puts p.join(\" \")\n \nend\n\nmain()"}, {"source_code": "$><<[n=gets.to_i,*1..n-1]*\" \"\n"}, {"source_code": "$><<[n=gets.to_i,*1..n-1]*\" \""}, {"source_code": "n = gets.to_i\nputs ([n] + (1...n).to_a)*' '"}, {"source_code": "n = gets.to_i\nputs ([n]+(1...n).to_a)*' '"}, {"source_code": "$><<[n=gets.to_i,*1..n-1]*\" \"\n"}, {"source_code": "$><<[n=gets.to_i,*1..n-1]*\" \"\n"}, {"source_code": "n = gets.to_i\n\nif n == 1\n puts 1\nelse\n print \"#{n} \"\n puts (1..(n-1)).to_a.map(&:to_s).join(\" \")\nend\n"}, {"source_code": "n = gets.to_i\n$arr = (1..n).to_a\ndef f(x)\n $arr[x-2], $arr[x-1] = $arr[x-1], $arr[x-2]\n f(x-1) if x-1>1\nend\nif n == 1\n puts n\n exit\nend\nf(n)\nputs $arr.join(\" \")"}, {"source_code": "$><<[n=gets.to_i,*1..n-1]*\" \""}, {"source_code": "n=gets.to_i\nprint n\n1.upto(n-1) {|i| print ' ',i}\nputs\n"}, {"source_code": "n=gets.to_i\nif n==1 then\n\tp 1\nelse\n\tprint n\n\t1.upto(n-1) {|i| print ' ',i}\n\tputs\nend\n"}, {"source_code": "print n=gets.to_i\n1.upto(n-1) {|i| print ' ',i}\n"}, {"source_code": "n=gets.to_i\nprint n\n1.upto(n-1) {|i| print ' ',i}\n"}, {"source_code": "n = gets.to_i\nif n==1\nputs 1\nelsif n==2\nputs '2 1'\nelse\nputs(([n,1]+(2...n).to_a).join ' ')\nend"}, {"source_code": "$><<[n=gets.to_i,*1..n-1]*\" \""}, {"source_code": "def f(x)\n return if x == 0\n f(x - 1)\n tmp = $a[x]\n $a[x] = $a[x - 1]\n $a[x - 1] = tmp\nend\n\nn = gets.to_i\nans = (1..n-1).to_a\nans.unshift n\n\nputs ans.join ' '\n"}, {"source_code": "n = STDIN.readline.to_i\nans = [n]\nn -= 1\n1.upto n do |i|\n ans << i\nend\n\nputs ans.join ' '\n"}, {"source_code": "$><<[n=gets.to_i,*1..n-1]*\" \"\n"}, {"source_code": "a = gets.to_i\nprint a\n1.upto(a - 1) { |i| print ' ',i }\n"}, {"source_code": "a=gets.to_i\n\nprint a\n1.upto(a-1){|i| print ' ',i}\n"}, {"source_code": "n=gets.to_i\nputs [n] + (1...n).to_a\n"}, {"source_code": "puts [n=gets.to_i]+(1...n).to_a"}, {"source_code": "$><<[n=gets.to_i,*1..n-1]*\" \"\n"}, {"source_code": "n = Integer(gets.chomp)\nputs(([n] + (1..n - 1).to_a) * ' ')"}, {"source_code": "$><<[n=gets.to_i,*1..n-1]*\" \"\n"}, {"source_code": "puts [n=gets.to_i,*1..n-1]*\" \""}, {"source_code": "a = gets.to_i\nb = \"\"\nc = 1\nwhile(c<<[n=gets.to_i,*1..n-1]*\" \"\n"}], "negative_code": [{"source_code": "puts [1..gets.to_i].rotate(-1)*' '"}, {"source_code": "def main()\n n = gets.chomp.to_i\n p = []\n \n n.downto(1) do |i|\n p << i\n end\n \n puts p.join(\" \")\n \nend\n\nmain()"}, {"source_code": "n = gets.to_i\n\nif n == 1\n puts 1\nelse\n puts (2..n).to_a.push(1).map(&:to_s).join(\" \")\nend\n"}, {"source_code": "puts (1..gets.to_i).to_a.reverse.join(\" \")"}, {"source_code": "a=[]\nn=gets.to_i\nn.times{|i|a< 1 ? (k - 1) / (f - 1) : 200\n\t\n\tl = [nl, l].max\n\tr = [nr, r].min\nend\n\np divCeil(n, l) == divCeil(n, r) ? divCeil(n, l) : -1"}], "negative_code": [{"source_code": "def divCeil(dd, dr)\n\t(dd + dr - 1) / dr\nend\n\nn, m = gets.split.map{|x| x.to_i}\n\nif m == 0\n\tp n == 1 ? 1 : -1\n\texit(0)\nend\n\nkf = []\nm.times{kf << gets.split.map{|x| x.to_i}}\nkf.sort!\n\nl = divCeil(kf[0][0], kf[0][1])\nr = 200\n\n(1...kf.size).each do |i|\n\tl = [l, divCeil(kf[i][0], kf[i][1])].max\n\tif kf[i][1] > kf[i - 1][1]\n\t\tr = [r, (kf[i][0] - 1) / [1, kf[i][1] - 1].max].min\n\tend\nend\n\np divCeil(n, l) == divCeil(n, r) ? divCeil(n, l) : -1"}, {"source_code": "def divCeil(dd, dr)\n\t(dd + dr - 1) / dr\nend\n\nn, m = gets.split.map{|x| x.to_i}\n\nif m == 0\n\tp n == 1 ? 1 : -1\n\texit(0)\nend\n\nkf = []\nm.times{kf << gets.split.map{|x| x.to_i}}\nkf.sort!\n\nl = divCeil(kf[0][0], kf[0][1])\nr = (kf[-1][0] - 1) / [1, kf[-1][1] - 1].max\n\n(1...kf.size).each do |i|\n\tl = [l, divCeil(kf[i][0], kf[i][1])].max\n\tif kf[i][1] > kf[i - 1][1]\n\t\tr = [r, (kf[i][0] - 1) / [1, kf[i][1] - 1].max].min\n\tend\nend\n\np r > 0 && divCeil(n, l) == divCeil(n, r) ? divCeil(n, l) : -1"}, {"source_code": "def divCeil(dd, dr)\n\t(dd + dr - 1) / dr\nend\n\nn, m = gets.split.map{|x| x.to_i}\n\nif m == 0\n\tp -1\n\texit(0)\nend\n\nkf = []\nm.times{kf << gets.split.map{|x| x.to_i}}\nkf.sort!\n\nl = divCeil(kf[0][0], kf[0][1])\nr = 200\n\n(1...kf.size).each do |i|\n\tl = [l, divCeil(kf[i][0], kf[i][1])].max\n\tif kf[i][1] > kf[i - 1][1]\n\t\tr = [r, (kf[i][0] - 1) / [1, kf[i][1] - 1].max].min\n\tend\nend\n\np divCeil(n, l) == divCeil(n, r) ? divCeil(n, l) : -1"}, {"source_code": "def divCeil(dd, dr)\n\t(dd + dr - 1) / dr\nend\n\nn, m = gets.split.map{|x| x.to_i}\n\nif m == 0 || n == 1\n\tp n == 1 ? 1 : -1\n\texit(0)\nend\n\nkf = []\nm.times{kf << gets.split.map{|x| x.to_i}}\nkf.sort!\n\nl = divCeil(kf[0][0], kf[0][1])\nr = (kf[-1][0] - 1) / [1, kf[-1][1] - 1].max\n\n(1...kf.size).each do |i|\n\tl = [l, divCeil(kf[i][0], kf[i][1])].max\n\tif kf[i][1] > kf[i - 1][1]\n\t\tr = [r, (kf[i][0] - 1) / [1, kf[i][1] - 1].max].min\n\tend\nend\n\np r > 0 && divCeil(n, l) == divCeil(n, r) ? divCeil(n, l) : -1"}, {"source_code": "def divCeil(dd, dr)\n\t(dd + dr - 1) / dr\nend\n\nn, m = gets.split.map{|x| x.to_i}\n\nkf = []\nm.times{kf << gets.split.map{|x| x.to_i}}\nkf.sort!\n\nl = divCeil(kf[0][0], kf[0][1])\nr = 200\n\n(1...kf.size).each do |i|\n\tl = [l, divCeil(kf[i][0], kf[i][1])].max\n\tif kf[i][1] > kf[i - 1][1]\n\t\tr = [r, (kf[i][0] - 1) / [1, kf[i][1] - 1].max].min\n\tend\nend\n\np l == r ? divCeil(n, l) : -1"}, {"source_code": "def divCeil(dd, dr)\n\t(dd + dr - 1) / dr\nend\n\nn, m = gets.split.map{|x| x.to_i}\n\nif m == 0 || n == 1\n\tp n == 1 ? 1 : -1\n\texit(0)\nend\n\nkf = []\nm.times{kf << gets.split.map{|x| x.to_i}}\nkf.sort!\n\nl = divCeil(kf[0][0], kf[0][1])\nr = (kf[-1][0] - 1) / [1, kf[-1][1] - 1].max\n\n(1...kf.size).each do |i|\n\tif n == kf[i][0]\n\t\tp kf[i][1]\n\t\texit(0)\n\tend\n\tl = [l, divCeil(kf[i][0], kf[i][1])].max\n\tif kf[i][1] > kf[i - 1][1]\n\t\tr = [r, (kf[i][0] - 1) / [1, kf[i][1] - 1].max].min\n\tend\nend\n\np r > 0 && divCeil(n, l) == divCeil(n, r) ? divCeil(n, l) : -1"}], "src_uid": "de780e96e6376a4b969934345ca4c51e"} {"nl": {"description": "You are given a string $$$s$$$ of length $$$n$$$ consisting only of the characters 0 and 1.You perform the following operation until the string becomes empty: choose some consecutive substring of equal characters, erase it from the string and glue the remaining two parts together (any of them can be empty) in the same order. For example, if you erase the substring 111 from the string 111110, you will get the string 110. When you delete a substring of length $$$l$$$, you get $$$a \\cdot l + b$$$ points.Your task is to calculate the maximum number of points that you can score in total, if you have to make the given string empty.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 2000$$$) — the number of testcases. The first line of each testcase contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$1 \\le n \\le 100; -100 \\le a, b \\le 100$$$) — the length of the string $$$s$$$ and the parameters $$$a$$$ and $$$b$$$. The second line contains the string $$$s$$$. The string $$$s$$$ consists only of the characters 0 and 1.", "output_spec": "For each testcase, print a single integer — the maximum number of points that you can score.", "sample_inputs": ["3\n3 2 0\n000\n5 -2 5\n11001\n6 1 -4\n100111"], "sample_outputs": ["6\n15\n-2"], "notes": "NoteIn the first example, it is enough to delete the entire string, then we will get $$$2 \\cdot 3 + 0 = 6$$$ points.In the second example, if we delete characters one by one, then for each deleted character we will get $$$(-2) \\cdot 1 + 5 = 3$$$ points, i. e. $$$15$$$ points in total.In the third example, we can delete the substring 00 from the string 100111, we get $$$1 \\cdot 2 + (-4) = -2$$$ points, and the string will be equal to 1111, removing it entirely we get $$$1 \\cdot 4 + (-4) = 0$$$ points. In total, we got $$$-2$$$ points for $$$2$$$ operations."}, "positive_code": [{"source_code": "t = gets.chomp.to_i\n\nt.times do\n n, a, b = gets.chomp.split(' ').map(&:to_i)\n str = gets.chomp\n if b >= 0\n puts \"#{n*(a+b)}\\n\"\n else\n first_char = str[0].to_i\n other_char = 1 - first_char\n ans = 0\n cnt = Hash.new(0)\n for i in 0...(str.size) do\n cnt[str[i].to_i] += 1\n end\n ans += a * cnt[first_char] + b\n\n ptr = 0\n while ptr < str.size do\n while ptr < str.size && str[ptr].to_i == first_char do\n ptr += 1\n end\n tmp = 0\n while ptr < str.size && str[ptr].to_i == other_char do\n ptr += 1\n tmp += 1\n end\n ans += a * tmp + b if tmp > 0\n end\n\n puts \"#{ans}\\n\"\n end\nend\n"}], "negative_code": [], "src_uid": "93fb13c40ab03700ef9a827796bd3d9d"} {"nl": {"description": "You are given a function $$$f$$$ written in some basic language. The function accepts an integer value, which is immediately written into some variable $$$x$$$. $$$x$$$ is an integer variable and can be assigned values from $$$0$$$ to $$$2^{32}-1$$$. The function contains three types of commands: for $$$n$$$ — for loop; end — every command between \"for $$$n$$$\" and corresponding \"end\" is executed $$$n$$$ times; add — adds 1 to $$$x$$$. After the execution of these commands, value of $$$x$$$ is returned.Every \"for $$$n$$$\" is matched with \"end\", thus the function is guaranteed to be valid. \"for $$$n$$$\" can be immediately followed by \"end\".\"add\" command can be outside of any for loops.Notice that \"add\" commands might overflow the value of $$$x$$$! It means that the value of $$$x$$$ becomes greater than $$$2^{32}-1$$$ after some \"add\" command. Now you run $$$f(0)$$$ and wonder if the resulting value of $$$x$$$ is correct or some overflow made it incorrect.If overflow happened then output \"OVERFLOW!!!\", otherwise print the resulting value of $$$x$$$.", "input_spec": "The first line contains a single integer $$$l$$$ ($$$1 \\le l \\le 10^5$$$) — the number of lines in the function. Each of the next $$$l$$$ lines contains a single command of one of three types: for $$$n$$$ ($$$1 \\le n \\le 100$$$) — for loop; end — every command between \"for $$$n$$$\" and corresponding \"end\" is executed $$$n$$$ times; add — adds 1 to $$$x$$$. ", "output_spec": "If overflow happened during execution of $$$f(0)$$$, then output \"OVERFLOW!!!\", otherwise print the resulting value of $$$x$$$.", "sample_inputs": ["9\nadd\nfor 43\nend\nfor 10\nfor 15\nadd\nend\nadd\nend", "2\nfor 62\nend", "11\nfor 100\nfor 100\nfor 100\nfor 100\nfor 100\nadd\nend\nend\nend\nend\nend"], "sample_outputs": ["161", "0", "OVERFLOW!!!"], "notes": "NoteIn the first example the first \"add\" is executed 1 time, the second \"add\" is executed 150 times and the last \"add\" is executed 10 times. Note that \"for $$$n$$$\" can be immediately followed by \"end\" and that \"add\" can be outside of any for loops.In the second example there are no commands \"add\", thus the returning value is 0.In the third example \"add\" command is executed too many times, which causes $$$x$$$ to go over $$$2^{32}-1$$$."}, "positive_code": [{"source_code": "l = gets.to_i\nlines = (0...l).map { gets.strip }\nans = 0\ncur = 1\nstk = []\nptr = 0\nuntil ptr == l\n if lines[ptr].match(/^for/)\n b = lines[ptr].match(/\\d+/)[0].to_i\n cur *= b\n stk.push(b)\n elsif lines[ptr].match(/^add/)\n ans += cur\n else\n cur /= stk.pop\n end\n ptr += 1\n until cur < 1e10\n if lines[ptr].match(/^for/)\n stk.push(1)\n elsif lines[ptr].match(/^add/)\n puts \"OVERFLOW!!!\"\n exit\n else\n cur /= stk.pop\n end\n ptr += 1\n end\nend\nif ans < 2**32 - 0\n puts ans\nelse\n puts \"OVERFLOW!!!\"\nend\n"}, {"source_code": "if File.exists?(\"input\")\n $stdin = File.open(\"input\")\n $stdout = File.open(\"output\",\"w\")\nend\n\nt = gets.to_i\ncurr = 1\n\narr = []\nt.times do\n\tstr = gets.chomp\n\tif(str[0]=='f')\n\t\tarr.push str.split(\" \")[-1].to_i\n\telsif(str[0]=='a')\n\t\tarr.push 0\n\telse\n\t\tarr.push -1\n\tend\nend\n\nmax = 2**32-1\n\nlist = []\nworkslist = []\nans=0\nworks = true\n\narr.each do |x|\n\n#\tp list\n#\tp workslist\n\n\tif(x==0)\n\t\tif(!works)\n\t\t\tputs \"OVERFLOW!!!\"\n\t\t\texit\n\t\tend\n\t\tans+=curr\n\telsif(x>0)\n\t\tlist.push x\n\t\tnewmax = curr*x\n\t\tif(works && newmax < max)\n\t\t\tcurr = newmax\n\t\t\tworkslist.push works\n\t\telse\n\t\t\tworks = false\n\t\t\tworkslist.push works\n\t\tend\n\telse\n\t\tworkslist.pop\n\t\ta = list.pop\n\t\tif(works)\n\t\t\tcurr/=a\n\t\tend\n\t\tif(workslist.size>0)\n\t\t\tworks = workslist[-1]\n\t\telse\n\t\t\tworks = true\n\t\tend\n\tend\n\n\tif(ans>max)\n\t\tputs \"OVERFLOW!!!\"\n\t\texit\n\tend\nend\n\nputs ans"}], "negative_code": [{"source_code": "l = gets.to_i\nlines = (0...l).map { gets.strip }\nans = 0\ncur = 1\nstk = []\nptr = 0\nuntil ptr == l\n if lines[ptr].match(/^for/)\n b = lines[ptr].match(/\\d+/)[0].to_i\n cur *= b\n stk.push(b)\n elsif lines[ptr].match(/^add/)\n ans += cur\n else\n cur /= stk.pop\n end\n ptr += 1\n until cur < 1e10\n if lines[ptr].match(/^for/)\n stk.push(1)\n elsif lines[ptr].match(/^add/)\n puts \"OVERFLOW!!!\"\n exit\n else\n cur /= stk.pop\n end\n ptr += 1\n end\nend\nif ans < 2**32 - 1\n puts ans\nelse\n puts \"OVERFLOW!!!\"\nend\n"}, {"source_code": "if File.exists?(\"input\")\n $stdin = File.open(\"input\")\n $stdout = File.open(\"output\",\"w\")\nend\n\nt = gets.to_i\ncurr = 1\n\narr = []\nt.times do\n\tstr = gets.chomp\n\tif(str[0]=='f')\n\t\tarr.push str.split(\" \")[-1].to_i\n\telsif(str[0]=='a')\n\t\tarr.push 0\n\telse\n\t\tarr.push -1\n\tend\nend\n\nmax = 2**32-1\n\nlist = []\nworkslist = []\nans=0\nworks = true\n\narr.each do |x|\n\tif(x==0)\n\t\tif(!works)\n\t\t\tputs \"OVERFLOW!!!\"\n\t\t\texit\n\t\tend\n\t\tans+=curr\n\telsif(x>0)\n\t\tlist.push x\n\n\t\tnewmax = curr*x\n\t\tif(newmax < max)\n\t\t\tcurr = newmax\n\t\t\tworkslist.push true\n\t\telse\n\t\t\tworks = false\n\t\t\tworkslist.push false\n\t\tend\n\telse\n\t\tworks = workslist.pop\n\t\ta = list.pop\n\t\tif(works)\n\t\t\tcurr/=a\n\t\tend\n\tend\n\n\tif(ans>max)\n\t\tputs \"OVERFLOW!!!\"\n\t\texit\n\tend\nend\n\nputs ans"}, {"source_code": "if File.exists?(\"input\")\n $stdin = File.open(\"input\")\n $stdout = File.open(\"output\",\"w\")\nend\n\nt = gets.to_i\ncurr = 1\n\narr = []\nt.times do\n\tstr = gets.chomp\n\tif(str[0]=='f')\n\t\tarr.push str.split(\" \")[-1].to_i\n\telsif(str[0]=='a')\n\t\tarr.push 0\n\telse\n\t\tarr.push -1\n\tend\nend\n\nmax = 2**32-1\n\nlist = []\nworkslist = []\nans=0\nworks = true\n\narr.each do |x|\n\tif(x==0)\n\t\tif(!works)\n\t\t\tputs \"OVERFLOW!!!\"\n\t\t\texit\n\t\tend\n\t\tans+=curr\n\telsif(x>0)\n\t\tlist.push x\n\n\t\tnewmax = curr*x\n\t\tif(works && newmax < max)\n\t\t\tcurr = newmax\n\t\t\tworkslist.push works\n\t\telse\n\t\t\tworks = false\n\t\t\tworkslist.push works\n\t\tend\n\telse\n\t\tworks = workslist.pop\n\t\ta = list.pop\n\t\tif(works)\n\t\t\tcurr/=a\n\t\tend\n\tend\n\n\tif(ans>max)\n\t\tputs \"OVERFLOW!!!\"\n\t\texit\n\tend\nend\n\nputs ans"}], "src_uid": "70f67dc28f9f6076255df97fc58ba2d6"} {"nl": {"description": "Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces.", "output_spec": "Print a single number — the maximum number of not disappointed people in the queue.", "sample_inputs": ["5\n15 2 1 5 3"], "sample_outputs": ["4"], "notes": "NoteValue 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5."}, "positive_code": [{"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\n\nn = gets.to_i\nts = gets.split.map(&:to_i)\nts.sort!\nacc = ts[0]\nsatisfied = 1\n# p ts\nts[1..-1].each { |i|\n # p \"Satisfied? #{i} #{acc}\"\n if acc <= i\n satisfied += 1\n acc += i\n else\n # 다음 사람이 만족할 수 있는 고객이라면,\n # 다가올 고객을 제일 뒤로 보낸다고 생각하자.\n # 스킵해도 무방하다.\n next\n end\n}\n\nputs satisfied"}, {"source_code": "n = Integer(gets)\nnums = gets.chomp.split\nqueue = []\nfor i in 0...n\n queue[i] = Integer(nums[i])\nend\n\nqueue.sort!\nsum = 0\ncount = 0\nfor i in 0...n\n if sum <= queue[i]\n sum += queue[i]\n count += 1\n else\n queue.push(queue[i])\n queue[i] = 0\n end\nend\nputs count\n"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split.map(&:to_i)\n\ncount = 1\na = a.sort\nt = a[0]\narr = []\nfor i in (1..a.size-1)\n\tx = i\n\twhile a[x] < t && x < n-1\n\t\tx += 1\n\tend\n\tif a[x] != nil && a[x] >= t\n\t\tcount += 1\n\t\tt += a[x]\n\telse\n\t\tbreak\n\tend\nend\nprint count"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\na.sort!\nres = 0\nsum = 0\na.each do\n |x|\n if(sum <= x)\n sum+=x\n res+=1\n end\nend\nprint res"}, {"source_code": "gets\npleased = 0\ntime = 0\ngets.split(' ').map(&:to_i).sort.each do |wait|\n if time <= wait\n pleased += 1\n time += wait\n end\nend\nputs pleased\n"}, {"source_code": "n = gets.to_i\nt = gets.split.collect{|i| i.to_i}.sort\nsum = 0\nans = 0\nt.each do |tt|\n if tt >= sum\n sum += tt\n ans += 1\n end\nend\nputs ans\n"}, {"source_code": "def test(ary)\n sum = 0\n res = 0\n \n ary.sort.each do |el|\n next unless el >= sum\n sum += el\n res += 1\n end\n \n res\nend\n\na = Kernel.gets\nputs test(Kernel.gets.split.map(&:to_i))"}, {"source_code": "n = gets.to_i\nt = gets.split.map(&:to_i)\nsum = 0\nans = 0\nt.sort.each do |x|\n if x >= sum\n sum += x\n ans += 1\n end\nend\nputs ans\n"}, {"source_code": "gets\nx=gets.split.map{|e| e.to_i}.sort\nsum=0\nans=0\n\nx.each{|e|\n\tif sum<=e then\n\t\tsum+=e\n\t\tans+=1\n\tend\n}\nputs ans\n\n"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split.map(&:to_i)\n\ncount = 1\na = a.sort\nt = a[0]\narr = []\nfor i in (1..a.size-1)\n\tx = i\n\twhile a[x] < t && x < n-1\n\t\tx += 1\n\tend\n\tif a[x] != nil && a[x] >= t\n\t\tcount += 1\n\t\tt += a[x]\n\telse\n\t\tbreak\n\tend\nend\nprint count"}], "negative_code": [{"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\n\nn = gets.to_i\nts = gets.split.map(&:to_i)\nts.sort!\nacc = ts[0]\nsatisfied = 1\nts[1..-1].each { |i|\n if acc <= i\n satisfied += 1\n end\n\n acc += i\n}\n\nputs satisfied"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split.map(&:to_i)\n\nt = 0\na = a.sort\ncount = 1\nfor i in (1..n-1)\n\tt += a[i-1]\n\tif a[i] >= t\n\t\tcount += 1\n\tend\nend\nprint count"}, {"source_code": "def test(ary)\n sum = 0\n res = 0\n \n ary.sort.each do |el|\n next unless el > sum\n sum += el\n res += 1\n end\n \n res\nend\n\na = Kernel.gets\nputs test(Kernel.gets.split.map(&:to_i))"}, {"source_code": "def test(ary)\n sum = 0\n res = 0\n \n ary.sort.each do |el|\n next unless el > sum\n sum += el\n res += 1\n end\n \n res\nend\n\na = Kernel.gets\nputs test(Kernel.gets.split.map(&:to_i))"}, {"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\n\nn = gets.to_i\nts = gets.split.map(&:to_i)\nts.sort!\nacc = ts[0]\nsatisfied = 1\np ts\nts[1..-1].each { |i|\n # p \"Satisfied? #{i} #{acc}\"\n if acc <= i\n satisfied += 1\n acc += i\n else\n # 다음 사람이 만족할 수 있는 고객이라면,\n # 다가올 고객을 제일 뒤로 보낸다고 생각하자.\n # 스킵해도 무방하다.\n next\n end\n}\n\nputs satisfied"}], "src_uid": "08c4d8db40a49184ad26c7d8098a8992"} {"nl": {"description": "Gleb ordered pizza home. When the courier delivered the pizza, he was very upset, because several pieces of sausage lay on the crust, and he does not really like the crust.The pizza is a circle of radius r and center at the origin. Pizza consists of the main part — circle of radius r - d with center at the origin, and crust around the main part of the width d. Pieces of sausage are also circles. The radius of the i -th piece of the sausage is ri, and the center is given as a pair (xi, yi).Gleb asks you to help determine the number of pieces of sausage caught on the crust. A piece of sausage got on the crust, if it completely lies on the crust.", "input_spec": "First string contains two integer numbers r and d (0 ≤ d < r ≤ 500) — the radius of pizza and the width of crust. Next line contains one integer number n — the number of pieces of sausage (1 ≤ n ≤ 105). Each of next n lines contains three integer numbers xi, yi and ri ( - 500 ≤ xi, yi ≤ 500, 0 ≤ ri ≤ 500), where xi and yi are coordinates of the center of i-th peace of sausage, ri — radius of i-th peace of sausage.", "output_spec": "Output the number of pieces of sausage that lay on the crust.", "sample_inputs": ["8 4\n7\n7 8 1\n-7 3 2\n0 2 1\n0 -2 2\n-3 -3 1\n0 6 2\n5 3 1", "10 8\n4\n0 0 9\n0 0 10\n1 0 1\n1 0 2"], "sample_outputs": ["2", "0"], "notes": "NoteBelow is a picture explaining the first example. Circles of green color denote pieces of sausage lying on the crust. "}, "positive_code": [{"source_code": "R, d = gets.split.map(&:to_i)\nn = gets.to_i\n\nans = 0\nn.times do\n x, y, r = gets.split.map(&:to_i)\n if R - d <= Math.hypot(x, y) - r and Math.hypot(x, y) + r <= R\n ans += 1\n end\nend\n\nputs ans\n"}, {"source_code": "r, d = gets.split.map(&:to_i)\nn = gets.to_i\nans = 0\nr2 = (r - d)\nn.times do |i|\n x, y, z = gets.split.map(&:to_i)\n hoge = x ** 2 + y ** 2\n if (r2 + z) * (r2 + z) <= hoge && hoge <= (r - z) * (r - z)\n ans += 1\n end\nend\nputs ans\n"}, {"source_code": "R, D = gets.split.map &:to_i\nn = gets.to_i\nEPS = 1e-9\n\ncnt = 0\nn.times do\n x, y, r = gets.split.map &:to_i\n d = (x**2 + y**2)**0.5\n # puts(\"#{R} #{D} #{d} #{r} #{R-D-EPS} #{r-d} #{r+d} #{R+EPS}\")\n cnt += 1 if R-D-EPS < d-r && d+r < R+EPS \nend\n\nputs cnt"}, {"source_code": "R, D = gets.chomp.split.map(&:to_i)\nn = gets.to_i\nans = 0\n\nn.times do\n x, y, r = gets.chomp.split.map(&:to_i)\n hypot = x * x + y * y\n outer = (hypot <= (R - r) ** 2 && (R - r) > 0)\n inner = ((R - D + r) <=0 || (R - D + r) ** 2 <= hypot)\n ans += 1 if outer and inner\nend\n\np ans\n"}], "negative_code": [], "src_uid": "fce9d78ad7d4ea01be1704f588e42d37"} {"nl": {"description": "Recently, you found a bot to play \"Rock paper scissors\" with. Unfortunately, the bot uses quite a simple algorithm to play: he has a string $$$s = s_1 s_2 \\dots s_{n}$$$ of length $$$n$$$ where each letter is either R, S or P.While initializing, the bot is choosing a starting index $$$pos$$$ ($$$1 \\le pos \\le n$$$), and then it can play any number of rounds. In the first round, he chooses \"Rock\", \"Scissors\" or \"Paper\" based on the value of $$$s_{pos}$$$: if $$$s_{pos}$$$ is equal to R the bot chooses \"Rock\"; if $$$s_{pos}$$$ is equal to S the bot chooses \"Scissors\"; if $$$s_{pos}$$$ is equal to P the bot chooses \"Paper\"; In the second round, the bot's choice is based on the value of $$$s_{pos + 1}$$$. In the third round — on $$$s_{pos + 2}$$$ and so on. After $$$s_n$$$ the bot returns to $$$s_1$$$ and continues his game.You plan to play $$$n$$$ rounds and you've already figured out the string $$$s$$$ but still don't know what is the starting index $$$pos$$$. But since the bot's tactic is so boring, you've decided to find $$$n$$$ choices to each round to maximize the average number of wins.In other words, let's suggest your choices are $$$c_1 c_2 \\dots c_n$$$ and if the bot starts from index $$$pos$$$ then you'll win in $$$win(pos)$$$ rounds. Find $$$c_1 c_2 \\dots c_n$$$ such that $$$\\frac{win(1) + win(2) + \\dots + win(n)}{n}$$$ is maximum possible.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. Next $$$t$$$ lines contain test cases — one per line. The first and only line of each test case contains string $$$s = s_1 s_2 \\dots s_{n}$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$; $$$s_i \\in \\{\\text{R}, \\text{S}, \\text{P}\\}$$$) — the string of the bot. It's guaranteed that the total length of all strings in one test doesn't exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, print $$$n$$$ choices $$$c_1 c_2 \\dots c_n$$$ to maximize the average number of wins. Print them in the same manner as the string $$$s$$$. If there are multiple optimal answers, print any of them.", "sample_inputs": ["3\nRRRR\nRSP\nS"], "sample_outputs": ["PPPP\nRSP\nR"], "notes": "NoteIn the first test case, the bot (wherever it starts) will always choose \"Rock\", so we can always choose \"Paper\". So, in any case, we will win all $$$n = 4$$$ rounds, so the average is also equal to $$$4$$$.In the second test case: if bot will start from $$$pos = 1$$$, then $$$(s_1, c_1)$$$ is draw, $$$(s_2, c_2)$$$ is draw and $$$(s_3, c_3)$$$ is draw, so $$$win(1) = 0$$$; if bot will start from $$$pos = 2$$$, then $$$(s_2, c_1)$$$ is win, $$$(s_3, c_2)$$$ is win and $$$(s_1, c_3)$$$ is win, so $$$win(2) = 3$$$; if bot will start from $$$pos = 3$$$, then $$$(s_3, c_1)$$$ is lose, $$$(s_1, c_2)$$$ is lose and $$$(s_2, c_3)$$$ is lose, so $$$win(3) = 0$$$; The average is equal to $$$\\frac{0 + 3 + 0}{3} = 1$$$ and it can be proven that it's the maximum possible average.A picture from Wikipedia explaining \"Rock paper scissors\" game: "}, "positive_code": [{"source_code": "def fun(k) \n\tif(k == \"R\")\n\t\treturn \"P\"\n\telsif(k == \"S\") \n\t\treturn \"R\"\n\telsif (k == \"P\") \n\t\treturn \"S\"\n\tend\nend\t\n\nt = gets.to_i \nt.times do \n\ts = gets.chomp \n\tl = s.size \n\tc = Hash.new(0) \n\ts.each_char do |x| \n\t\tc[x] += 1\n\tend\n\n\tputs fun(c.max_by{|k,v| v}[0])*l\n\nend\n"}], "negative_code": [{"source_code": "def fun(k) \n\tif(k == \"R\")\n\t\treturn \"P\"\n\telsif(k == \"S\") \n\t\treturn \"R\"\n\telsif (k == \"P\") \n\t\treturn \"S\"\n\tend\nend\t\n\nt = gets.to_i \nt.times do \n\ts = gets.chomp \n\tl = s.size \n\tc = Hash.new(0) \n\ts.each_char do |x| \n\t\tc[x] += 1\n\tend\n\tpos = s.index(c.max[0])\n\t(pos...l).each do |i| \n\t\tprint(fun(s[i])) \n\tend\n\t(0...pos).each do |i| \n\t\tprint(fun(s[i]))\n\tend\n\tputs \"\"\nend\n"}, {"source_code": "def fun(k) \n\tif(k == \"R\")\n\t\treturn \"P\"\n\telsif(k == \"S\") \n\t\treturn \"R\"\n\telsif (k == \"P\") \n\t\treturn \"S\"\n\tend\nend\t\n\nt = gets.to_i \nt.times do \n\ts = gets.chomp \n\tl = s.size \n\tc = Hash.new(0) \n\ts.each_char do |x| \n\t\tc[x] += 1\n\tend\n\tputs fun(c.max[0])*l\n\nend\n"}, {"source_code": "def fun(k) \n\tif(k == \"R\")\n\t\treturn \"P\"\n\telsif(k == \"S\") \n\t\treturn \"R\"\n\telsif (k == \"P\") \n\t\treturn \"S\"\n\tend\nend\t\n\nt = gets.to_i \nt.times do \n\ts = gets.chomp \n\tl = s.size \n\tarr = Array.new(l) {1} \n\t(1...l).each do |i| \n\t\tif s[i] == s[i-1] \n\t\t\tarr[i] += arr[i-1] \n\t\tend\n\tend \n\t# print(arr)\n\tmele ,mpos = 1,0 \n\t(1...l).each do |i| \n\t\tif(arr[i] > mele) \n\t\t\tmele = arr[i] \n\t\t\tmpos = i \n\t\tend\n\tend\n\tpos = mpos - arr[mpos]+1\n\t# print(pos)\n\t(pos...l).each do |i| \n\t\tprint(fun(s[i])) \n\tend \n\t(0...pos).each do |i| \n\t\tprint(fun(s[i]))\n\tend\n\tputs(\"\")\nend\n"}, {"source_code": "t = gets.to_i \nt.times do \n\ts = gets.chomp \n\n\ts.each_char do |x| \n\t\tif x == 'R' \n\t\t\tprint 'P' \n\t\telsif x == 'S' \n\t\t\tprint 'R'\n\t\telsif x == 'P' \n\t\t\tprint 'S'\n\t\tend\n\tend \n\tputs \"\"\nend\n"}], "src_uid": "38e884cbc5bede371bccbc848096f499"} {"nl": {"description": "When Xellos was doing a practice course in university, he once had to measure the intensity of an effect that slowly approached equilibrium. A good way to determine the equilibrium intensity would be choosing a sufficiently large number of consecutive data points that seems as constant as possible and taking their average. Of course, with the usual sizes of data, it's nothing challenging — but why not make a similar programming contest problem while we're at it?You're given a sequence of n data points a1, ..., an. There aren't any big jumps between consecutive data points — for each 1 ≤ i < n, it's guaranteed that |ai + 1 - ai| ≤ 1.A range [l, r] of data points is said to be almost constant if the difference between the largest and the smallest value in that range is at most 1. Formally, let M be the maximum and m the minimum value of ai for l ≤ i ≤ r; the range [l, r] is almost constant if M - m ≤ 1.Find the length of the longest almost constant range.", "input_spec": "The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of data points. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 100 000).", "output_spec": "Print a single number — the maximum length of an almost constant range of the given sequence.", "sample_inputs": ["5\n1 2 3 3 2", "11\n5 4 5 5 6 7 8 8 8 7 6"], "sample_outputs": ["4", "5"], "notes": "NoteIn the first sample, the longest almost constant range is [2, 5]; its length (the number of data points in it) is 4.In the second sample, there are three almost constant ranges of length 4: [1, 4], [6, 9] and [7, 10]; the only almost constant range of the maximum length 5 is [6, 10]."}, "positive_code": [{"source_code": "n = gets.chomp.to_i\nnums = gets.split.map { |s| s.to_i }\n#puts nums\nprev = 0\nprev_pos = 0\nnext_pos = 0\nans = 0\ncurr = []\ncurr_pos = []\nstart = []\n\nnums.size.times do |i|\n if curr.size < 2 and not curr.include? nums[i] then\n curr << nums[i]\n curr_pos << i\n start << i\n elsif not curr.include? nums[i] then\n curr = curr.drop(1)\n if curr_pos[0] > start[1] then\n start[1] = curr_pos[0] + 1\n end\n curr_pos = curr_pos.drop(1)\n start = start.drop(1)\n curr << nums[i]\n curr_pos << i\n start << i\n else\n if curr[0] == nums[i] then\n curr = curr.drop(1)\n curr_pos = curr_pos.drop(1)\n z = start[0]\n start = start.drop(1)\n curr << nums[i]\n curr_pos << i\n start << z\n else\n curr_pos[1] = i\n end\n end\n ans = [ans,curr_pos.max - start.min + 1].max\n #print curr\n #print curr_pos\n #print start\n #puts ans\nend\n\nputs ans\n \n\n \n"}, {"source_code": "#! ruby\n# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n\ndef gi(); gets.to_i; end\ndef gis(); gets.chomp.split.map(&:to_i); end\n\n$stdout = STDERR\n\nn = gi\na = gis\n\nmx = a[0]\nmn = a[0]\nbf = a[0]\npos = 0\nans = 1\nlen = 1\n1.upto(n - 1) do |i|\n x = a[i]\n puts \"i=%d x=%d bf=%d mn=%d mx=%d len=%d pos=%d ans=%d\" % [i,x,bf,mn,mx,len,pos,ans]\n if bf == x\n len += 1\n puts \"bf==x\"\n else\n if (mx - x).abs <= 1 && (mn - x).abs <= 1\n len += 1\n if mx == mn\n mn, mx = [mx, x].minmax\n end\n puts \"swap mx mn\"\n else\n ans = len if len > ans\n if (bf - x).abs <= 1\n len = i - pos + 1\n mn, mx = [bf, x].minmax\n puts \"continue bf=%d\" % [bf]\n else\n len = 1\n mx = mn = x\n puts \"new state\"\n end\n end\n pos = i\n bf = x\n end\nend\n\nSTDOUT.puts [ans, len].max\n\n"}, {"source_code": "gets\na = gets.split.map(&:to_i)\n\nm = 0\n\ncurrent_m = a.shift\nb = [current_m]\n\nuntil a.empty?\n candidate = a.shift\n if candidate > current_m + 1 # candidate == current_m + 2\n last_one = b.rindex(current_m)\n b.slice!(0..last_one)\n current_m += 1\n elsif candidate < current_m # candidate == current_m - 1\n last_one = b.rindex(current_m + 1)\n b.slice!(0..last_one) if last_one\n current_m -= 1\n end\n b << candidate\n m = b.size if b.size > m\nend\n\nputs m\n"}, {"source_code": "n = gets.chomp.to_i\nas = gets.chomp.split.map(&:to_i)\n\nbs = as.map { |a| a / 2 }\ncs = as.map { |a| (a + 1) / 2 }\ncntb = 1\ncntc = 1\nans = 2\nfor i in 1...n\n if bs[i] == bs[i - 1]\n cntb += 1\n ans = [ans, cntb].max\n else\n cntb = 1\n end\n if cs[i] == cs[i - 1]\n cntc += 1\n ans = [ans, cntc].max\n else\n cntc = 1\n end\nend\n\nputs ans\n"}, {"source_code": "n = gets.chomp.to_i\na = gets.split(\" \").map(&:to_i)\nu = max = 1\namin = amax = a[0]\nimin = imax = 0\nfor i in 1...n\n\tif a[i] == a[i-1]\n\t\tu += 1\n\telse\n\t\tif amin <= a[i] and a[i]<= amax\n\t\t\tu += 1\n\t\t\tif a[i] == amin\n\t\t\t\timin = i\n\t\t\telse\n\t\t\t\timax = i\n\t\t\tend\n\t\telsif a[i] > amax\n\t\t\tamin = amax\n\t\t\tamax = a[i]\n\t\t\tu = i - imax + 1\n\t\t\timin = imax\n\t\t\timax = i\n\t\telse\n\t\t\tamax = amin\n\t\t\tamin = a[i]\n\t\t\tu = i - imin + 1\n\t\t\timax = imin\n\t\t\timin = i\n\t\tend\n\tend\n\tif u > max\n\t\tmax = u\n\tend\nend\nputs max\n"}, {"source_code": "\ndef main(numbers)\n odds = numbers.map { |n| n.odd? ? n : n + 1 }\n odds_length = find_greatest(odds)\n evens = numbers.map { |n| n.even? ? n : n + 1 }\n evens_length = find_greatest(evens)\n puts [odds_length, evens_length].max\nend\n\ndef find_greatest(numbers)\n last_number = -100000\n in_a_row = 0\n max_in_a_row = 0\n numbers.each do |n|\n if n == last_number\n in_a_row += 1\n else\n last_number = n\n in_a_row = 1\n end\n max_in_a_row = [max_in_a_row, in_a_row].max\n end\n max_in_a_row\nend\n\nARGF.each_with_index do |line, index|\n if index == 1\n main(line.chomp.split(' ').map(&:to_i))\n end\nend\n"}, {"source_code": "N = gets.to_i\nan = gets.split.map(&:to_i)\n\nl = r = n = 0\nans = 0\nwhile l < N && r < N\n r = l = n\n max = an[r]\n min = an[r]\n until max-min > 1\n r += 1\n break if r >= N\n min, max = [min, max, an[r]].minmax\n n = r if n == l && an[l] != an[r]\n end\n ans = [ans, r - l].max\nend\n\nputs ans\n"}, {"source_code": "# 14462223\nn = gets.chomp.to_i\na = gets.chomp.split(\" \").map(&:to_i)\n\nleft = 0\nprev = 0\nans = 1\ntmp = 0\n(1...n).each do |i|\n d = a[i] - a[i-1]\n if d != 0 then\n if d == tmp then\n left = prev + 1\n end\n tmp = d\n prev = i - 1\n end\n ans = [ans, i - left + 1].max\nend\nputs ans\n"}], "negative_code": [{"source_code": "n = gets.chomp.to_i\nnums = gets.split.map { |s| s.to_i }\n#puts nums\nprev = 0\nprev_pos = 0\nnext_pos = 0\nans = 0\ncurr = []\ncurr_pos = []\nstart = []\n\nnums.size.times do |i|\n if curr.size < 2 then\n curr << nums[i]\n curr_pos << i\n start << i\n elsif not curr.include? nums[i] then\n curr = curr.drop(1)\n if curr_pos[0] > start[1] then\n start[1] = curr_pos[0] + 1\n end\n curr_pos = curr_pos.drop(1)\n start = start.drop(1)\n curr << nums[i]\n curr_pos << i\n start << i\n else\n if curr[0] == nums[i] then\n curr = curr.drop(1)\n curr_pos = curr_pos.drop(1)\n z = start[0]\n start = start.drop(1)\n curr << nums[i]\n curr_pos << i\n start << z\n else\n curr_pos[1] = i\n end\n end\n ans = [ans,curr_pos.max - start.min + 1].max\n #print curr\n #print curr_pos\n #print start\n #puts ans\nend\n\nputs ans\n \n\n \n"}, {"source_code": "gets\na = gets.split.map(&:to_i)\n\nm = 0\n\ncurrent_m = a.shift\nb = [current_m]\n\nuntil a.empty?\n candidate = a.shift\n if candidate > current_m + 1 # candidate == current_m + 2\n last_one = b.index(current_m)\n b.slice!(0..last_one)\n current_m = b.min\n elsif candidate < current_m # candidate == current_m - 1\n last_one = b.index(current_m + 1)\n b.slice!(0..last_one) if last_one\n current_m = b.min\n end\n b << candidate\n m = b.size if b.size > m\nend\n\nputs m\n"}, {"source_code": "gets\na = gets.split.map(&:to_i)\n\nm = 0\n\ncurrent_m = a.shift\nb = [current_m]\n\nuntil a.empty?\n candidate = a.shift\n if candidate > current_m + 1\n last_one = b.index(current_m)\n b.slice!(0..last_one)\n current_m += 1\n elsif candidate < current_m\n last_one = b.index(current_m + 1)\n b.slice!(0..last_one) if last_one\n current_m -= 1\n end\n b << candidate\n m = b.size if b.size > m\nend\n\nputs m\n"}, {"source_code": "gets\na = gets.split.map(&:to_i)\n\nm = 0\n\ncurrent_m = a.shift\nb = [current_m]\n\nuntil a.empty?\n candidate = a.shift\n if candidate > current_m + 1 # candidate == current_m + 2\n last_one = b.index(current_m)\n b.slice!(0..last_one)\n current_m = b.min\n elsif candidate < current_m # candidate == current_m - 1\n last_one = b.index(current_m + 1)\n b.slice!(0..last_one) if last_one\n current_m = b.min - 1\n end\n b << candidate\n m = b.size if b.size > m\nend\n\nputs m\n"}, {"source_code": "n = gets.chomp.to_i\nas = gets.chomp.split.map(&:to_i)\n\nbs = (1...n).map { |i| as[i] - as[i - 1] }\nans = 0\nim, iz, ip = [n, n, n]\nif bs[0] == -1\n im = 0\nelsif bs[0] == 1\n ip = 0\nelse\n iz = 0\nend\nfor i in 1...n - 1\n if bs[i] == -1\n if bs[i - 1] == 0\n if ip == iz - 1\n ans = [ans, i - ip + 2].max\n else\n ans = [ans, i - iz + 2].max\n end\n elsif ip == i - 1\n ans = [ans, 3].max\n elsif im == i - 1\n ans = [ans, 2].max\n end\n im = i\n elsif bs[i] == 1\n if bs[i - 1] == 0\n if im == iz - 1\n ans = [ans, i - im + 2].max\n else\n ans = [ans, i - iz + 2].max\n end\n elsif im == i - 1\n ans = [ans, 3].max\n elsif ip == i - 1\n ans = [ans, 2].max\n end\n ip = i\n else\n if bs[i - 1] == 0\n if iz == 0\n ans = [ans, i - iz + 2].max\n else\n ans = [ans, i - iz + 3].max\n end\n else\n ans = [ans, 2].max\n iz = i\n end\n end\nend\n\nputs ans\n"}, {"source_code": "n = gets.chomp.to_i\nas = gets.chomp.split.map(&:to_i)\n\nbs = (1...n).map { |i| as[i] - as[i - 1] }\nans = 0\nim, iz, ip = [n, n, n]\nif bs[0] == -1\n im = 0\nelsif bs[0] == 1\n ip = 0\nelse\n iz = 0\nend\nfor i in 1...n - 1\n if bs[i] == -1\n if bs[i - 1] == 0\n if ip == iz - 1\n ans = [ans, i - ip + 2].max\n else\n ans = [ans, i - iz + 2].max\n end\n elsif ip == i - 1\n ans = [ans, 3].max\n elsif im == i - 1\n ans = [ans, 2].max\n end\n im = i\n elsif bs[i] == 1\n if bs[i - 1] == 0\n if im == iz - 1\n ans = [ans, i - im + 2].max\n else\n ans = [ans, i - iz + 2].max\n end\n elsif im == i - 1\n ans = [ans, 3].max\n elsif ip == i - 1\n ans = [ans, 2].max\n end\n ip = i\n else\n if bs[i - 1] == 0\n if iz == 0\n ans = [ans, i - iz + 2].max\n else\n ans = [ans, i - iz + 3].max\n end\n else\n ans = [ans, 3].max\n iz = i\n end\n end\nend\n\nputs ans\n"}], "src_uid": "b784cebc7e50cc831fde480171b9eb84"} {"nl": {"description": "A binary string is a string consisting only of the characters 0 and 1. You are given a binary string $$$s$$$.For some non-empty substring$$$^\\dagger$$$ $$$t$$$ of string $$$s$$$ containing $$$x$$$ characters 0 and $$$y$$$ characters 1, define its cost as: $$$x \\cdot y$$$, if $$$x > 0$$$ and $$$y > 0$$$; $$$x^2$$$, if $$$x > 0$$$ and $$$y = 0$$$; $$$y^2$$$, if $$$x = 0$$$ and $$$y > 0$$$. Given a binary string $$$s$$$ of length $$$n$$$, find the maximum cost across all its non-empty substrings.$$$^\\dagger$$$ A string $$$a$$$ is a substring of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.", "input_spec": "Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 10^5$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) — the length of the string $$$s$$$. The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, print a single integer — the maximum cost across all substrings.", "sample_inputs": ["6\n\n5\n\n11100\n\n7\n\n1100110\n\n6\n\n011110\n\n7\n\n1001010\n\n4\n\n1000\n\n1\n\n0"], "sample_outputs": ["9\n12\n16\n12\n9\n1"], "notes": "NoteIn the first test case, we can take a substring $$$111$$$. It contains $$$3$$$ characters 1 and $$$0$$$ characters 0. So $$$a = 3$$$, $$$b = 0$$$ and its cost is $$$3^2 = 9$$$.In the second test case, we can take the whole string. It contains $$$4$$$ characters 1 and $$$3$$$ characters 0. So $$$a = 4$$$, $$$b = 3$$$ and its cost is $$$4 \\cdot 3 = 12$$$.In the third test case, we can can take a substring $$$1111$$$ and its cost is $$$4^2 = 16$$$.In the fourth test case, we can take the whole string and cost is $$$4 \\cdot 3 = 12$$$.In the fifth test case, we can take a substring $$$000$$$ and its cost is $$$3 \\cdot 3 = 9$$$.In the sixth test case, we can only take the substring $$$0$$$ and its cost is $$$1 \\cdot 1 = 1$$$."}, "positive_code": [{"source_code": "def aaa(_n, s)\r\n s.delete!(\"\\n\")\r\n max0 = s.split(/0+/).map(&:length).sort.reverse[0].to_i\r\n max1 = s.split(/1+/).map(&:length).sort.reverse[0].to_i\r\n arr = [s.delete('0').length * s.delete('1').length, max0*max0, max1*max1]\r\n\r\n puts arr.sort[2]\r\nend\r\n\r\nt = gets.to_i\r\n\r\nt.times do\r\n n = gets.to_i\r\n s = gets\r\n aaa(n, s)\r\nend"}, {"source_code": "gets.to_i.times do\n n=gets.chomp.to_i\n s=gets.chomp\n zeros,ones,maxZeros,maxOnes,currentZeros,currentOnes=0,0,0,0,0,0\n s.each_char do |c|\n if c=='0'\n zeros+=1\n currentZeros+=1\n maxZeros=[maxZeros,currentZeros].max\n currentOnes=0\n else\n ones+=1\n currentOnes+=1\n maxOnes=[maxOnes,currentOnes].max\n currentZeros=0\n end\n end\n puts [zeros*ones,maxZeros*maxZeros, maxOnes*maxOnes].max\nend"}], "negative_code": [], "src_uid": "9070e0d4f8071d1ee8df5189b7c17bfa"} {"nl": {"description": "You are given two arrays $$$a$$$ and $$$b$$$, each consisting of $$$n$$$ positive integers, and an integer $$$x$$$. Please determine if one can rearrange the elements of $$$b$$$ so that $$$a_i + b_i \\leq x$$$ holds for each $$$i$$$ ($$$1 \\le i \\le n$$$).", "input_spec": "The first line of input contains one integer $$$t$$$ ($$$1 \\leq t \\leq 100$$$) — the number of test cases. $$$t$$$ blocks follow, each describing an individual test case. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \\leq n \\leq 50$$$; $$$1 \\leq x \\leq 1000$$$) — the length of arrays $$$a$$$ and $$$b$$$, and the parameter $$$x$$$, described in the problem statement. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\leq a_1 \\le a_2 \\le \\dots \\le a_n \\leq x$$$) — the elements of array $$$a$$$ in non-descending order. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \\ldots, b_n$$$ ($$$1 \\leq b_1 \\le b_2 \\le \\dots \\le b_n \\leq x$$$) — the elements of array $$$b$$$ in non-descending order. Test cases are separated by a blank line.", "output_spec": "For each test case print Yes if one can rearrange the corresponding array $$$b$$$ so that $$$a_i + b_i \\leq x$$$ holds for each $$$i$$$ ($$$1 \\le i \\le n$$$) or No otherwise. Each character can be printed in any case.", "sample_inputs": ["4\n3 4\n1 2 3\n1 1 2\n\n2 6\n1 4\n2 5\n\n4 4\n1 2 3 4\n1 2 3 4\n\n1 5\n5\n5"], "sample_outputs": ["Yes\nYes\nNo\nNo"], "notes": "NoteIn the first test case, one can rearrange $$$b$$$ so it'll look like $$$[1, 2, 1]$$$. In this case, $$$1 + 1 \\leq 4$$$; $$$2 + 2 \\leq 4$$$; $$$3 + 1 \\leq 4$$$.In the second test case, one can set $$$b$$$ to $$$[5, 2]$$$, then $$$1 + 5 \\leq 6$$$; $$$4 + 2 \\leq 6$$$.In the third test case, no matter how one shuffles array $$$b$$$, $$$a_4 + b_4 = 4 + b_4 > 4$$$.In the fourth test case, there is only one rearrangement of array $$$b$$$ and it doesn't satisfy the condition since $$$5 + 5 > 5$$$."}, "positive_code": [{"source_code": "def read_array\n gets.strip.split(' ').map(&:to_i)\nend\n\ndef can(a, b, x)\n a.each_with_index do |e, i|\n return false if e + b[i] > x\n end\n true\nend\n\ngets.strip.to_i.times do\n n, x = read_array\n a = read_array\n b = read_array\n gets\n\n a.sort!\n b = b.sort.reverse\n if can(a, b, x)\n puts \"Yes\"\n else\n puts \"No\"\n end\nend\n"}, {"source_code": "def read\n gets.split.map &:to_i\nend\nq = gets.to_i\nq.times do |t|\n n, x = read\n a, b = read, read\n a.sort!\n b.sort!.reverse!\n ok = true\n (0...n).each do |i|\n ok = false if a[i] + b[i] > x\n end\n puts ok ? \"YES\" : \"NO\"\n if t < q - 1\n gets\n end\nend"}], "negative_code": [{"source_code": "def read\n gets.split.map &:to_i\nend\nq = gets.to_i\nq.times do |t|\n n, x = read\n a, b = read, read\n a.sort!\n b.sort!.reverse!\n ok = true\n (0...n).each do |i|\n ok = false if a[i] + b[i] > x\n end\n print(ok ? \"YES\" : \"NO\")\n if t < q - 1\n gets\n end\nend"}], "src_uid": "7e765c1b0e3f3e9c44de825a79bc1da2"} {"nl": {"description": "Vasya had a strictly increasing sequence of positive integers a1, ..., an. Vasya used it to build a new sequence b1, ..., bn, where bi is the sum of digits of ai's decimal representation. Then sequence ai got lost and all that remained is sequence bi.Vasya wonders what the numbers ai could be like. Of all the possible options he likes the one sequence with the minimum possible last number an. Help Vasya restore the initial sequence.It is guaranteed that such a sequence always exists.", "input_spec": "The first line contains a single integer number n (1 ≤ n ≤ 300). Next n lines contain integer numbers b1, ..., bn  — the required sums of digits. All bi belong to the range 1 ≤ bi ≤ 300.", "output_spec": "Print n integer numbers, one per line — the correct option for numbers ai, in order of following in sequence. The sequence should be strictly increasing. The sum of digits of the i-th number should be equal to bi. If there are multiple sequences with least possible number an, print any of them. Print the numbers without leading zeroes.", "sample_inputs": ["3\n1\n2\n3", "3\n3\n2\n1"], "sample_outputs": ["1\n2\n3", "3\n11\n100"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/env ruby\n\ncur=[0]\ngets.to_i.times{\n\tn=gets.to_i\n\ts=0\n\tidx=(cur.size.times.find{|i|s+=cur[i];s>=n}||cur.size)-1\n\tif idx==-1\n\t\ta=[1]+cur.map{|e|0}\n\telse\n\t\ta=cur.dup\n\t\twhile a[idx]==9\n\t\t\tidx-=1\n\t\tend\n\t\tif idx>=0\n\t\t\ta[idx]+=1\n\t\telse\n\t\t\ta=[1]+a\n\t\t\tidx=0\n\t\tend\n\t\t(idx+1).upto(a.size-1){|i|a[i]=0}\n\tend\n\t#p a\n\tfill=n-a.reduce(:+)\n\tidx=a.size-1\n\twhile idx>=0\n\t\ta[idx]+=fill\n\t\tfill=0\n\t\tif a[idx]>9\n\t\t\tfill=a[idx]-9\n\t\t\ta[idx]=9\n\t\tend\n\t\tidx-=1\n\tend\n\td,r=fill.divmod(9)\n\ta=(r==0?[]:[r])+[9]*d+a\n\tcur=a\n\tputs cur*''\n}"}, {"source_code": "#!/usr/bin/ruby\n# coding: utf-8\n\ncur = [0]\ngets.to_i.times {\n n = gets.to_i\n s = 0\n idx = (cur.size.times.find{|i|\n s += cur[i]\n s >= n\n } || cur.size) - 1\n if idx == -1\n x = [1] + [0] * cur.size\n else\n x = cur.dup\n while x[idx] == 9\n idx -= 1\n end\n if idx >= 0\n x[idx] += 1\n else\n x = [1] + x\n idx = 0\n end\n (idx + 1).upto(x.size - 1) {|i|\n x[i] = 0\n }\n end\n tot = n - x.reduce(:+)\n idx = x.size - 1\n while idx >= 0\n x[idx] += tot\n tot = 0\n if x[idx] > 9\n tot = x[idx] - 9\n x[idx] = 9\n end\n idx -= 1\n end\n l, r = tot.divmod(9)\n x = (r == 0 ? [] : [r]) + [9] * l + x\n cur = x\n puts cur.join\n}"}, {"source_code": "#!/usr/bin/ruby\n# coding: utf-8\n\ncur = [0]\ngets.to_i.times {\n n = gets.to_i\n s = 0\n idx = (cur.size.times.find{|i|\n s += cur[i]\n s >= n\n } || cur.size) - 1\n x = cur.dup\n while x[idx] == 9\n idx -= 1\n end\n if idx >= 0\n x[idx] += 1\n else\n x = [1] + x\n idx = 0\n end\n (idx + 1).upto(x.size - 1) {|i|\n x[i] = 0\n }\n tot = n - x.reduce(:+)\n idx = x.size - 1\n while idx >= 0\n x[idx] += tot\n tot = 0\n if x[idx] > 9\n tot = x[idx] - 9\n x[idx] = 9\n end\n idx -= 1\n end\n l, r = tot.divmod(9)\n x = (r == 0 ? [] : [r]) + [9] * l + x\n cur = x\n puts cur.join\n}"}], "negative_code": [{"source_code": "#!/usr/bin/env ruby\n\ncur=[0]\ngets.to_i.times{\n\tn=gets.to_i\n\ts=0\n\tidx=(cur.size.times.find{|i|s+=cur[i];s>n}||cur.size)-1\n\tif idx==-1\n\t\ta=[1]+cur.map{|e|0}\n\telse\n\t\ta=cur.dup\n\t\twhile a[idx]==9\n\t\t\tidx-=1\n\t\tend\n\t\tif idx>=0\n\t\t\ta[idx]+=1\n\t\telse\n\t\t\ta=[1]+a\n\t\t\tidx=0\n\t\tend\n\t\t(idx+1).upto(a.size-1){|i|a[i]=0}\n\tend\n\t\n\tfill=n-a.reduce(:+)\n\tidx=a.size-1\n\twhile idx>=0\n\t\ta[idx]+=fill\n\t\tfill=0\n\t\tif a[idx]>9\n\t\t\tfill=a[idx]-9\n\t\t\ta[idx]=9\n\t\tend\n\t\tidx-=1\n\tend\n\td,r=fill.divmod(9)\n\ta=(r==0?[]:[r])+[9]*d+a\n\tcur=a\n\tputs cur*''\n}"}, {"source_code": "#!/usr/bin/env ruby\ndef gen(n)\n\treturn to_enum(:gen,n) if !block_given?\n\td,r=n.divmod(9)\n\ta=(r==0?[]:[r])+[9]*d\n\tdigits=a.size\n\tloop{\n\t\tyield a.join.to_i\n\t\tidx_down=(a.size-1).downto(0).find{|i|a[i]!=0}\n\t\tif idx_down!=0 && idx_up=(idx_down-1).downto(0).find{|i|a[i]<9}\n\t\t\ta[idx_down]-=1\n\t\t\ta[idx_up]+=1\n\t\telse\n\t\t\tdigits+=1\n\t\t\td,r=(n-1).divmod(9)\n\t\t\ta=(r==0?[]:[r])+[9]*d\n\t\t\twhile a.sizecur}\n\tp nxt\n\tcur=nxt\n}"}, {"source_code": "#!/usr/bin/env ruby\ndef gen(n)\n\treturn to_enum(:gen,n) if !block_given?\n\td,r=n.divmod(9)\n\ta=(r==0?[]:[r])+[9]*d\n\tdigits=a.size\n\tloop{\n\t\tyield a.join.to_i\n\t\tidx_down=(a.size-1).downto(0).find{|i|a[i]!=0}\n\t\tif idx_down!=0 && idx_up=(idx_down-1).downto(0).find{|i|a[i]<9}\n\t\t\ta[idx_down]-=1\n\t\t\ta[idx_up]+=1\n\t\t\ta=a[0,idx_down]+a[idx_down..-1].reverse\n\t\telse\n\t\t\tdigits+=1\n\t\t\td,r=(n-1).divmod(9)\n\t\t\ta=(r==0?[]:[r])+[9]*d\n\t\t\twhile a.sizecur}\n\tp nxt\n\tcur=nxt\n}"}, {"source_code": "#!/usr/bin/ruby\n# coding: utf-8\n\ncur = [0]\ngets.to_i.times {\n n = gets.to_i\n s = 0\n idx = (cur.size.times.find{|i|\n s += cur[i]\n s >= n\n } || cur.size) - 1\n x = cur.dup\n while x[idx] == 9\n idx -= 1\n end\n if idx >= 0\n x[idx] += 1\n else\n x = [1] + x\n idx = 0\n end\n (idx + 1).upto(x.size - 1) {|i|\n x[i] = 0\n }\n tot = n - x.reduce(:+)\n idx = 0\n while idx < x.size\n x[idx] += tot\n tot = 0\n if x[idx] > 9\n tot = x[idx] - 9\n x[idx] = 9\n end\n idx += 1\n end\n l, r = tot.divmod(9)\n x = (r == 0 ? [] : [r]) + [9] * l + x\n cur = x\n puts cur.join\n}"}, {"source_code": "#!/usr/bin/ruby\n# coding: utf-8\n\ncur = [0]\ngets.to_i.times {\n n = gets.to_i\n s = 0\n idx = (cur.size.times.find{|i|\n s += cur[i]\n s >= n\n } || cur.size) - 1\n if idx == -1\n x = [1] + [0] * cur.size\n else\n x = cur.dup\n while x[idx] == 9\n idx -= 1\n end\n if idx >= 0\n x[idx] += 1\n else\n x = [1] + x\n idx = 0\n end\n (idx + 1).upto(x.size - 1) {|i|\n x[i] = 0\n }\n end\n tot = n - x.reduce(:+)\n idx = x.size - 1\n while idx >= 0\n x[idx] += tot\n tot = 0\n if x[idx] > 9\n tot = x[idx] - 9\n x[idx] = 9\n end\n idx -= 1\n end\n l, r = tot.divmod(9)\n x = (r == 0 ? [] : [r]) + [9] * l + x\n cur = x\n puts cur.to_s\n}"}], "src_uid": "4577a9b2d96110966ad95a960ef7ec20"} {"nl": {"description": " Denis was very sad after Nastya rejected him. So he decided to walk through the gateways to have some fun. And luck smiled at him! When he entered the first courtyard, he met a strange man who was selling something. Denis bought a mysterious item and it was... Random permutation generator! Denis could not believed his luck.When he arrived home, he began to study how his generator works and learned the algorithm. The process of generating a permutation consists of $$$n$$$ steps. At the $$$i$$$-th step, a place is chosen for the number $$$i$$$ $$$(1 \\leq i \\leq n)$$$. The position for the number $$$i$$$ is defined as follows: For all $$$j$$$ from $$$1$$$ to $$$n$$$, we calculate $$$r_j$$$  — the minimum index such that $$$j \\leq r_j \\leq n$$$, and the position $$$r_j$$$ is not yet occupied in the permutation. If there are no such positions, then we assume that the value of $$$r_j$$$ is not defined. For all $$$t$$$ from $$$1$$$ to $$$n$$$, we calculate $$$count_t$$$  — the number of positions $$$1 \\leq j \\leq n$$$ such that $$$r_j$$$ is defined and $$$r_j = t$$$. Consider the positions that are still not occupied by permutation and among those we consider the positions for which the value in the $$$count$$$ array is maximum. The generator selects one of these positions for the number $$$i$$$. The generator can choose any position. Let's have a look at the operation of the algorithm in the following example: Let $$$n = 5$$$ and the algorithm has already arranged the numbers $$$1, 2, 3$$$ in the permutation. Consider how the generator will choose a position for the number $$$4$$$: The values of $$$r$$$ will be $$$r = [3, 3, 3, 4, \\times]$$$, where $$$\\times$$$ means an indefinite value. Then the $$$count$$$ values will be $$$count = [0, 0, 3, 1, 0]$$$. There are only two unoccupied positions in the permutation: $$$3$$$ and $$$4$$$. The value in the $$$count$$$ array for position $$$3$$$ is $$$3$$$, for position $$$4$$$ it is $$$1$$$. The maximum value is reached only for position $$$3$$$, so the algorithm will uniquely select this position for number $$$4$$$. Satisfied with his purchase, Denis went home. For several days without a break, he generated permutations. He believes that he can come up with random permutations no worse than a generator. After that, he wrote out the first permutation that came to mind $$$p_1, p_2, \\ldots, p_n$$$ and decided to find out if it could be obtained as a result of the generator.Unfortunately, this task was too difficult for him, and he asked you for help. It is necessary to define whether the written permutation could be obtained using the described algorithm if the generator always selects the position Denis needs.", "input_spec": "The first line contains a single integer $$$t$$$ $$$(1 \\leq t \\leq 10^5)$$$  — the number of test cases. Then the descriptions of the test cases follow. The first line of the test case contains a single integer $$$n$$$ $$$(1 \\leq n \\leq 10^5)$$$  — the size of the permutation. The second line of the test case contains $$$n$$$ different integers $$$p_1, p_2, \\ldots, p_n$$$ ($$$1 \\leq p_i \\leq n$$$)  — the permutation written by Denis. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.", "output_spec": "Print \"Yes\" if this permutation could be obtained as a result of the generator. Otherwise, print \"No\". All letters can be displayed in any case.", "sample_inputs": ["5\n5\n2 3 4 5 1\n1\n1\n3\n1 3 2\n4\n4 2 3 1\n5\n1 5 2 4 3"], "sample_outputs": ["Yes\nYes\nNo\nYes\nNo"], "notes": "NoteLet's simulate the operation of the generator in the first test.At the $$$1$$$ step, $$$r = [1, 2, 3, 4, 5], count = [1, 1, 1, 1, 1]$$$. The maximum value is reached in any free position, so the generator can choose a random position from $$$1$$$ to $$$5$$$. In our example, it chose $$$5$$$.At the $$$2$$$ step, $$$r = [1, 2, 3, 4, \\times], count = [1, 1, 1, 1, 0]$$$. The maximum value is reached in positions from $$$1$$$ to $$$4$$$, so the generator can choose a random position among them. In our example, it chose $$$1$$$.At the $$$3$$$ step, $$$r = [2, 2, 3, 4, \\times], count = [0, 2, 1, 1, 0]$$$. The maximum value is $$$2$$$ and is reached only at the $$$2$$$ position, so the generator will choose this position.At the $$$4$$$ step, $$$r = [3, 3, 3, 4, \\times], count = [0, 0, 3, 1, 0]$$$. The maximum value is $$$3$$$ and is reached only at the $$$3$$$ position, so the generator will choose this position.At the $$$5$$$ step, $$$r = [4, 4, 4, 4, \\times], count = [0, 0, 0, 4, 0]$$$. The maximum value is $$$4$$$ and is reached only at the $$$4$$$ position, so the generator will choose this position.In total, we got a permutation of $$$2, 3, 4, 5, 1$$$, that is, a generator could generate it."}, "positive_code": [{"source_code": "def naive(arr)\n possible = \"YES\"\n hash = {}\n arr.each_with_index{|e,i| hash[e] = i+1}\n rj = Array.new(arr.size+1){|e| e>0 ? e : -1}\n puts hash.inspect\n puts rj.inspect\n (1..arr.size).each do |e|\n count = [-1]\n rj.each{|e| count[e]=(count[e]||0)+1 if e>0}\n puts count.inspect\n if count.max == count[hash[e]]\n pos = hash[e]\n rj.each_with_index do |e,i| \n end\n hash.delete(e)\n else\n break\n end\n end\n possible\nend\n\ndef solve(arr)\n solvable = \"YES\"\n arr.each_with_index do |e,i|\n if i>0 and arr[i-1]0\n size = gets.chomp.to_i \n arr = gets.chomp.split(\" \").map{|e| e.to_i}\n puts solve(arr)\n tc -= 1\nend\n"}], "negative_code": [], "src_uid": "dc03b66a4c6aac69372d594784f3f083"} {"nl": {"description": "Let's denote a $$$k$$$-step ladder as the following structure: exactly $$$k + 2$$$ wooden planks, of which two planks of length at least $$$k+1$$$ — the base of the ladder; $$$k$$$ planks of length at least $$$1$$$ — the steps of the ladder; Note that neither the base planks, nor the steps planks are required to be equal.For example, ladders $$$1$$$ and $$$3$$$ are correct $$$2$$$-step ladders and ladder $$$2$$$ is a correct $$$1$$$-step ladder. On the first picture the lengths of planks are $$$[3, 3]$$$ for the base and $$$[1]$$$ for the step. On the second picture lengths are $$$[3, 3]$$$ for the base and $$$[2]$$$ for the step. On the third picture lengths are $$$[3, 4]$$$ for the base and $$$[2, 3]$$$ for the steps. You have $$$n$$$ planks. The length of the $$$i$$$-th planks is $$$a_i$$$. You don't have a saw, so you can't cut the planks you have. Though you have a hammer and nails, so you can assemble the improvised \"ladder\" from the planks.The question is: what is the maximum number $$$k$$$ such that you can choose some subset of the given planks and assemble a $$$k$$$-step ladder using them?", "input_spec": "The first line contains a single integer $$$T$$$ ($$$1 \\le T \\le 100$$$) — the number of queries. The queries are independent. Each query consists of two lines. The first line contains a single integer $$$n$$$ ($$$2 \\le n \\le 10^5$$$) — the number of planks you have. The second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^5$$$) — the lengths of the corresponding planks. It's guaranteed that the total number of planks from all queries doesn't exceed $$$10^5$$$.", "output_spec": "Print $$$T$$$ integers — one per query. The $$$i$$$-th integer is the maximum number $$$k$$$, such that you can choose some subset of the planks given in the $$$i$$$-th query and assemble a $$$k$$$-step ladder using them. Print $$$0$$$ if you can't make even $$$1$$$-step ladder from the given set of planks.", "sample_inputs": ["4\n4\n1 3 1 3\n3\n3 3 2\n5\n2 3 3 4 2\n3\n1 1 2"], "sample_outputs": ["2\n1\n2\n0"], "notes": "NoteExamples for the queries $$$1-3$$$ are shown at the image in the legend section.The Russian meme to express the quality of the ladders: "}, "positive_code": [{"source_code": "t = gets.to_i\nt.times do |i|\n n = gets.to_i\n a = gets.split.map(&:to_i)\n b = c = 0\n n.times do |i|\n c = a[i] if c < a[i]\n b,c = c,b if c > b\n end\n puts (c-1 < n-2 ? c-1 : n-2)\nend"}, {"source_code": "gets.to_i.times do\n n = gets.to_i\n a, b = gets.split.map(&:to_i).sort.reverse\n p [a-1, b-1, n-2].min\nend\n"}], "negative_code": [], "src_uid": "130fd7f40d879e25b0bff886046bf699"} {"nl": {"description": "Valera is a lazy student. He has m clean bowls and k clean plates. Valera has made an eating plan for the next n days. As Valera is lazy, he will eat exactly one dish per day. At that, in order to eat a dish, he needs exactly one clean plate or bowl. We know that Valera can cook only two types of dishes. He can eat dishes of the first type from bowls and dishes of the second type from either bowls or plates. When Valera finishes eating, he leaves a dirty plate/bowl behind. His life philosophy doesn't let him eat from dirty kitchenware. So sometimes he needs to wash his plate/bowl before eating. Find the minimum number of times Valera will need to wash a plate/bowl, if he acts optimally.", "input_spec": "The first line of the input contains three integers n, m, k (1 ≤ n, m, k ≤ 1000) — the number of the planned days, the number of clean bowls and the number of clean plates. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 2). If ai equals one, then on day i Valera will eat a first type dish. If ai equals two, then on day i Valera will eat a second type dish. ", "output_spec": "Print a single integer — the minimum number of times Valera will need to wash a plate/bowl.", "sample_inputs": ["3 1 1\n1 2 1", "4 3 1\n1 1 1 1", "3 1 2\n2 2 2", "8 2 2\n1 2 1 2 1 2 1 2"], "sample_outputs": ["1", "1", "0", "4"], "notes": "NoteIn the first sample Valera will wash a bowl only on the third day, so the answer is one.In the second sample, Valera will have the first type of the dish during all four days, and since there are only three bowls, he will wash a bowl exactly once.In the third sample, Valera will have the second type of dish for all three days, and as they can be eaten from either a plate or a bowl, he will never need to wash a plate/bowl."}, "positive_code": [{"source_code": "n, m, k = gets.split(' ').map(&:to_i)\nwash = 0\ngets.split(' ',n).map(&:to_i).each do |x|\n if x == 1\n m > 0 ? m -= 1 : wash += 1\n elsif k > 0\n k -= 1\n elsif m > 0\n m -= 1\n else\n wash += 1\n end\nend\nputs wash"}, {"source_code": "n, m, k = gets.split.map(&:to_i)\nn = 0\ngets.split.map(&:to_i).each do |a|\n if a == 2 and k > 0\n k -= 1\n else\n (m > 0) ? m -= 1 : n += 1\n end\nend\nputs n"}, {"source_code": "\nn, m, k = gets.chomp.split.map(&:to_i);\narr = gets.chomp.split.map(&:to_i);\nans = 0;\n\narr.each do |x|\n if (x == 1) then\n if (m > 0)\n m -= 1;\n else\n ans += 1;\n end\n elsif (k > 0)\n k -= 1;\n elsif (m > 0)\n m -= 1;\n else\n ans += 1;\n end \nend\np ans;\n"}, {"source_code": "#!/usr/bin/ruby\n\nn, m, k = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\n\nr = 0\na.each do |x|\n if x == 1\n m -= 1\n else # x == 2\n if k > 0\n k -= 1\n else\n m -= 1\n end\n end\n\n if m < 0\n m += 1\n r += 1\n elsif k < 0\n k += 1\n r += 1\n end\nend\n\nputs r\n"}, {"source_code": "class A\n def initialize\n n, m, k = gets.chomp.split(' ').map(&:to_i)\n type_list = gets.chomp.split(' ').map(&:to_i)\n cnt = 0\n\n type_list.each do |type|\n if type == 1\n if m > 0\n m -= 1\n else\n cnt += 1\n end\n else\n if k > 0\n k -= 1\n elsif m > 0\n m -= 1\n else\n cnt += 1\n end\n end\n end\n\n puts cnt\n end\nend\n\nA.new"}, {"source_code": "n,m,k=gets.split.map{|e| e.to_i}\nxs=gets.split.map{|e| e.to_i}\nans=0\nc1=xs.count{|e|\n\te==1\t\n}\nc2=xs.count{|e|\n\te==2\n}\nans=0\nif c1>m\n\tans=c1-m\nelse\n\tk=m+k-c1\nend\nans+=c2-k if c2>k\nputs ans"}, {"source_code": "l = gets.split(\" \").map { |e| e.to_i }\nglub = l[1]\nplos = l[2]\nans = 0\nmas = gets.split(\" \").map { |e| e.to_i }\nmas.each { |e| \n\tif e == 1 then\n\t\tif glub > 0 then\n\t\t\tglub -= 1\n\t\telse\n\t\t\tans +=1\n\t\tend\n\telse\n\t\tif plos > 0 then\n\t\t\t\tplos -= 1\n\t\telsif glub > 0\n\t\t\tglub -= 1\n\t\telse\n\t\t\tans +=1\n\t\tend\n\tend\n}\nputs ans"}, {"source_code": "def read_next_line\n gets.split.map { |x| x.to_i }\nend\n\n_, m, k = read_next_line\na = read_next_line.inject(Hash.new(0)) { |h, e| h[e] += 1; h }\nd = 0\nc = m - a[1]\nif c > 0\n k += c\nelse\n d = -c\nend\nd += [a[2] - k, 0].max\nputs d"}], "negative_code": [], "src_uid": "4ed5b8055ce48b5ad4e43ed4b06d1b07"} {"nl": {"description": "Berland shop sells $$$n$$$ kinds of juices. Each juice has its price $$$c_i$$$. Each juice includes some set of vitamins in it. There are three types of vitamins: vitamin \"A\", vitamin \"B\" and vitamin \"C\". Each juice can contain one, two or all three types of vitamins in it.Petya knows that he needs all three types of vitamins to stay healthy. What is the minimum total price of juices that Petya has to buy to obtain all three vitamins? Petya obtains some vitamin if he buys at least one juice containing it and drinks it.", "input_spec": "The first line contains a single integer $$$n$$$ $$$(1 \\le n \\le 1\\,000)$$$ — the number of juices. Each of the next $$$n$$$ lines contains an integer $$$c_i$$$ $$$(1 \\le c_i \\le 100\\,000)$$$ and a string $$$s_i$$$ — the price of the $$$i$$$-th juice and the vitamins it contains. String $$$s_i$$$ contains from $$$1$$$ to $$$3$$$ characters, and the only possible characters are \"A\", \"B\" and \"C\". It is guaranteed that each letter appears no more than once in each string $$$s_i$$$. The order of letters in strings $$$s_i$$$ is arbitrary.", "output_spec": "Print -1 if there is no way to obtain all three vitamins. Otherwise print the minimum total price of juices that Petya has to buy to obtain all three vitamins.", "sample_inputs": ["4\n5 C\n6 B\n16 BAC\n4 A", "2\n10 AB\n15 BA", "5\n10 A\n9 BC\n11 CA\n4 A\n5 B", "6\n100 A\n355 BCA\n150 BC\n160 AC\n180 B\n190 CA", "2\n5 BA\n11 CB"], "sample_outputs": ["15", "-1", "13", "250", "16"], "notes": "NoteIn the first example Petya buys the first, the second and the fourth juice. He spends $$$5 + 6 + 4 = 15$$$ and obtains all three vitamins. He can also buy just the third juice and obtain three vitamins, but its cost is $$$16$$$, which isn't optimal.In the second example Petya can't obtain all three vitamins, as no juice contains vitamin \"C\"."}, "positive_code": [{"source_code": "def solution\n\n n = gets.to_i\n\n prices = {}\n\n n.times do\n p, l = gets.chomp.split(' ')\n price = p.to_i\n vits = l.chars.sort.join\n\n prices[vits] = price if prices[vits].nil? || prices[vits] > price\n end\n\n def eval_price(prices, fragments)\n frag_prices = fragments.map{ |f| prices[f] }\n frag_prices.reduce(0, :+)\n rescue\n nil\n end\n\n possibilities = [\n %w\"ABC\",\n %w\"A B C\",\n %w\"A BC\",\n %w\"AB C\",\n %w\"B AC\",\n %w\"AB AC\",\n %w\"AB BC\",\n %w\"AC AB\",\n %w\"AC BC\",\n ]\n\n cheapest = possibilities.map{ |p| eval_price(prices, p) }.compact.min || -1\n\n puts cheapest\nend\n\nsolution unless ENV['TEST__MODE']\n"}, {"source_code": "n = gets.to_i\nm = n.times.map{gets.split}\nn.times do |i|\n m[i][0] = m[i][0].to_i\nend\n\ndef clac_min(str,n,m)\n ret = 10**6\n for i in 0..n-1\n if str.size == 1\n if m[i][1].include?(str)\n ret = m[i][0] if m[i][0] < ret\n end\n elsif str.size == 2\n if m[i][1].include?(str[0]) && m[i][1].include?(str[1])\n ret = m[i][0] if m[i][0] < ret\n end\n else\n if m[i][1].include?(str[0]) && m[i][1].include?(str[1]) && m[i][1].include?(str[2])\n ret = m[i][0] if m[i][0] < ret\n end\n end\n end\n return ret\nend\n\na,b,c = clac_min(\"A\",n,m),clac_min(\"B\",n,m),clac_min(\"C\",n,m)\nab,bc,ca = [clac_min(\"AB\",n,m),a+b].min,ab = [clac_min(\"BC\",n,m),b+c].min,ab = [clac_min(\"CA\",n,m),c+a].min\nabc = [a+b+c,ab+c,a+bc,b+ca,clac_min(\"ABC\",n,m)].min\nputs abc == 10**6 ? -1 : abc\n"}, {"source_code": "n = gets.to_i\narray = Array.new\n\nn.times do\n miniarr = gets.chomp.split(\" \")\n miniarr[0] = miniarr[0].to_i\n miniarr[1] = miniarr[1].each_char.to_a.sort.join(\"\")\n array.push miniarr\nend\n#p array\n\ncombcost = Hash.new(1000000)\n\narray.each do |juice|\n cost = juice[0]\n type = juice[1]\n\n if cost < combcost[type]\n combcost[type] = cost\n end\nend\n\nans = [combcost[\"A\"]+combcost[\"B\"]+combcost[\"C\"],combcost[\"A\"]+combcost[\"BC\"],combcost[\"AB\"]+combcost[\"C\"],combcost[\"B\"]+combcost[\"AC\"],combcost[\"AB\"]+combcost[\"BC\"],combcost[\"AC\"]+combcost[\"BC\"],combcost[\"AB\"]+combcost[\"AC\"],combcost[\"ABC\"]].min\n\nif ans>300000\n ans = -1\nend\nputs ans\n"}], "negative_code": [], "src_uid": "02d62bb1eb4cc0e373b862a980d6b29c"} {"nl": {"description": "Further research on zombie thought processes yielded interesting results. As we know from the previous problem, the nervous system of a zombie consists of n brains and m brain connectors joining some pairs of brains together. It was observed that the intellectual abilities of a zombie depend mainly on the topology of its nervous system. More precisely, we define the distance between two brains u and v (1 ≤ u, v ≤ n) as the minimum number of brain connectors used when transmitting a thought between these two brains. The brain latency of a zombie is defined to be the maximum distance between any two of its brains. Researchers conjecture that the brain latency is the crucial parameter which determines how smart a given zombie is. Help them test this conjecture by writing a program to compute brain latencies of nervous systems.In this problem you may assume that any nervous system given in the input is valid, i.e., it satisfies conditions (1) and (2) from the easy version.", "input_spec": "The first line of the input contains two space-separated integers n and m (1 ≤ n, m ≤ 100000) denoting the number of brains (which are conveniently numbered from 1 to n) and the number of brain connectors in the nervous system, respectively. In the next m lines, descriptions of brain connectors follow. Every connector is given as a pair of brains a b it connects (1 ≤ a, b ≤ n and a ≠ b).", "output_spec": "Print one number – the brain latency.", "sample_inputs": ["4 3\n1 2\n1 3\n1 4", "5 4\n1 2\n2 3\n3 4\n3 5"], "sample_outputs": ["2", "3"], "notes": null}, "positive_code": [{"source_code": "nodes, edges = gets.split(' ').map(&:to_i)\ng = {}\nedges.times do\n from, to = gets.split(' ').map(&:to_i)\n g[from] ||= []\n g[from].push(to)\n g[to] ||= []\n g[to].push(from)\nend\n\n$g = g\n$max = 0\n\nn, = $g.find { |n, nxts| nxts.size <= 1 }\n_stack = [[n, 0, 0]]\n_entries = {}\n\nwhile !_stack.empty?\n _entry = _stack.last\n node, path, prev = _entry\n\n all = true\n ($g[node] || []).each { |nxt|\n if nxt != prev\n e = [nxt, path+1, node]\n if !_entries[e]\n all = false\n _stack.push(e)\n end\n end\n }\n next unless all\n\n paths = ($g[node] || []).map { |nxt|\n if nxt == prev\n 0\n else\n 1 + _entries[[nxt, path + 1, node]]\n end\n }\n\n _entries[_entry] = paths.max\n _stack.pop\n\n paths.push(path)\n m1, i1 = paths.each_with_index.max_by(&:first)\n paths.delete_at(i1)\n $max = [m1 + paths.max, $max].max\nend\n\nputs $max\n"}], "negative_code": [{"source_code": "nodes, edges = gets.split(' ').map(&:to_i)\ng = {}\nedges.times do\n from, to = gets.split(' ').map(&:to_i)\n g[from] ||= []\n g[from].push(to)\n g[to] ||= []\n g[to].push(from)\nend\n\n$g = g\n$visited = {}\n\n$max = 0\n\ndef dfs(node, path)\n return 0 if $visited[node]\n $visited[node] = true\n\n paths = ($g[node] || []).map { |nxt|\n if $visited[nxt]\n 0\n else\n 1 + dfs(nxt, path + 1)\n end\n }\n m_next = paths.max\n\n paths.push(path)\n m1 = paths.max\n paths.delete(m1)\n $max = [m1 + paths.max, $max].max\n\n m_next\nend\n\nn, = $g.find { |n, nxts| nxts.size <= 1 }\nputs dfs($g.first.first, 0)\n"}, {"source_code": "nodes, edges = gets.split(' ').map(&:to_i)\ng = {}\nedges.times do\n from, to = gets.split(' ').map(&:to_i)\n g[from] ||= []\n g[from].push(to)\n g[to] ||= []\n g[to].push(from)\nend\n\n$g = g\n$visited = {}\n\ndef dfs(node)\n return if $visited[node]\n $visited[node] = true\n\n max = 0\n ($g[node] || []).each { |nxt|\n if !$visited[nxt]\n max = [1 + dfs(nxt), max].max\n end\n }\n max\nend\n\nn, = $g.find { |n, nxts| nxts.size <= 1 }\nputs dfs(n)\n"}, {"source_code": "nodes, edges = gets.split(' ').map(&:to_i)\ng = {}\nedges.times do\n from, to = gets.split(' ').map(&:to_i)\n g[from] ||= []\n g[from].push(to)\n g[to] ||= []\n g[to].push(from)\nend\n\n$g = g\n$visited = {}\n\ndef dfs(node)\n return 0 if $visited[node]\n $visited[node] = true\n\n max = 0\n ($g[node] || []).each { |nxt|\n if !$visited[nxt]\n max = [1 + dfs(nxt), max].max\n end\n }\n max\nend\n\nn, = $g.find { |n, nxts| nxts.size <= 1 }\nputs dfs(n)\n"}, {"source_code": "nodes, edges = gets.split(' ').map(&:to_i)\ng = {}\nedges.times do\n from, to = gets.split(' ').map(&:to_i)\n g[from] ||= []\n g[from].push(to)\n g[to] ||= []\n g[to].push(from)\nend\n\n$g = g\n$visited = {}\n$max = 0\n\nn, = $g.find { |n, nxts| nxts.size <= 1 }\n_stack = [[n, 0]]\n_entries = {}\n\nwhile !_stack.empty?\n _entry = _stack.last\n node, path = _entry\n\n $visited[node] = true\n\n all = true\n ($g[node] || []).each { |nxt|\n if !$visited[nxt]\n e = [nxt, path+1]\n if !_entries[e]\n all = false\n _stack.push(e)\n end\n end\n }\n next unless all\n\n paths = ($g[node] || []).map { |nxt|\n if $visited[nxt]\n 0\n else\n 1 + _entries[[nxt, path + 1]]\n end\n }\n\n _entries[_entry] = paths.max\n _stack.pop\n\n paths.push(path)\n m1, i1 = paths.each_with_index.max_by(&:first)\n paths.delete_at(i1)\n $max = [m1 + paths.max, $max].max\nend\n\nputs $max\n"}], "src_uid": "40a965a28e38bad3aff9c58bdeeeb8f6"} {"nl": {"description": "Petya's friends made him a birthday present — a bracket sequence. Petya was quite disappointed with his gift, because he dreamed of correct bracket sequence, yet he told his friends nothing about his dreams and decided to fix present himself. To make everything right, Petya is going to move at most one bracket from its original place in the sequence to any other position. Reversing the bracket (e.g. turning \"(\" into \")\" or vice versa) isn't allowed. We remind that bracket sequence $$$s$$$ is called correct if: $$$s$$$ is empty; $$$s$$$ is equal to \"($$$t$$$)\", where $$$t$$$ is correct bracket sequence; $$$s$$$ is equal to $$$t_1 t_2$$$, i.e. concatenation of $$$t_1$$$ and $$$t_2$$$, where $$$t_1$$$ and $$$t_2$$$ are correct bracket sequences. For example, \"(()())\", \"()\" are correct, while \")(\" and \"())\" are not. Help Petya to fix his birthday present and understand whether he can move one bracket so that the sequence becomes correct.", "input_spec": "First of line of input contains a single number $$$n$$$ ($$$1 \\leq n \\leq 200\\,000$$$) — length of the sequence which Petya received for his birthday. Second line of the input contains bracket sequence of length $$$n$$$, containing symbols \"(\" and \")\".", "output_spec": "Print \"Yes\" if Petya can make his sequence correct moving at most one bracket. Otherwise print \"No\".", "sample_inputs": ["2\n)(", "3\n(()", "2\n()", "10\n)))))((((("], "sample_outputs": ["Yes", "No", "Yes", "No"], "notes": "NoteIn the first example, Petya can move first bracket to the end, thus turning the sequence into \"()\", which is correct bracket sequence.In the second example, there is no way to move at most one bracket so that the sequence becomes correct.In the third example, the sequence is already correct and there's no need to move brackets."}, "positive_code": [{"source_code": "n = gets.to_i\ns = gets.chomp\n\ncan=false\nwrong=0\ncnt=0\nn.times do |i|\n c = s[i]\n if c=='('\n cnt+=1\n else\n cnt-=1\n end\n\n if cnt<0\n wrong+=1\n cnt=0\n end\n\n if wrong>1\n puts 'No'\n exit\n end\nend\n\nif cnt-wrong==0\n can=true\nend\nputs can ? 'Yes' : 'No'"}], "negative_code": [], "src_uid": "e30085b163c820cff68fb24b94088ec1"} {"nl": {"description": "Everything got unclear to us in a far away constellation Tau Ceti. Specifically, the Taucetians choose names to their children in a very peculiar manner.Two young parents abac and bbad think what name to give to their first-born child. They decided that the name will be the permutation of letters of string s. To keep up with the neighbours, they decided to call the baby so that the name was lexicographically strictly larger than the neighbour's son's name t.On the other hand, they suspect that a name tax will be introduced shortly. According to it, the Taucetians with lexicographically larger names will pay larger taxes. That's the reason abac and bbad want to call the newborn so that the name was lexicographically strictly larger than name t and lexicographically minimum at that.The lexicographical order of strings is the order we are all used to, the \"dictionary\" order. Such comparison is used in all modern programming languages to compare strings. Formally, a string p of length n is lexicographically less than string q of length m, if one of the two statements is correct: n < m, and p is the beginning (prefix) of string q (for example, \"aba\" is less than string \"abaa\"), p1 = q1, p2 = q2, ..., pk - 1 = qk - 1, pk < qk for some k (1 ≤ k ≤ min(n, m)), here characters in strings are numbered starting from 1. Write a program that, given string s and the heighbours' child's name t determines the string that is the result of permutation of letters in s. The string should be lexicographically strictly more than t and also, lexicographically minimum.", "input_spec": "The first line contains a non-empty string s (1 ≤ |s| ≤ 5000), where |s| is its length. The second line contains a non-empty string t (1 ≤ |t| ≤ 5000), where |t| is its length. Both strings consist of lowercase Latin letters.", "output_spec": "Print the sought name or -1 if it doesn't exist.", "sample_inputs": ["aad\naac", "abad\nbob", "abc\ndefg", "czaaab\nabcdef"], "sample_outputs": ["aad", "daab", "-1", "abczaa"], "notes": "NoteIn the first sample the given string s is the sought one, consequently, we do not need to change the letter order there."}, "positive_code": [{"source_code": "#!/usr/bin/ruby\n\ndef winning(list, target)\n str = \"\"\n list.keys.sort.reverse.each do |key|\n str << key*list[key]\n end\n str > target\nend\n\ndef minstr(list)\n arr = []\n list.each_pair do |key,val|\n arr += [key]*val\n end\n arr.sort.join\nend\n\ndef search(cur, max, list, target)\n substr = target[cur+1..-1] || ''\n tc = target[cur] || ''\n\n #puts \"#{cur} #{list}\"\n \n if list[tc] > 0\n list[tc] -= 1\n if winning(list, substr)\n res = search(cur+1, max, list, target)\n if res\n return tc+res\n end\n end\n list[tc] += 1\n end\n\n # find next char\n nc = list.keys.sort.find{|k| k>tc and list[k]>0}\n if nc\n list[nc] -= 1\n return nc+minstr(list)\n end\n \n nil\nend\n\ns = gets.chomp\nt = gets.chomp\n\nlist = Hash.new(0)\ns.each_char do |c|\n list[c] += 1\nend\n\nans = search(0, s.size, list, t)\nif ans.nil?\n ans = -1\nend\nputs ans\n"}], "negative_code": [{"source_code": "#!/usr/bin/ruby\n\ndef winning(list, target)\n arr = []\n list.each_pair do |key, val|\n arr += [key]*val\n end\n arr.sort.reverse.join > target\nend\n\ndef minstr(list)\n arr = []\n list.each_pair do |key,val|\n arr += [key]*val\n end\n arr.sort.join\nend\n\ndef search(cur, max, list, target)\n substr = target[cur+1..-1] || ''\n tc = target[cur] || ''\n\n #puts \"#{cur} #{list}\"\n \n if list[tc] > 0\n list[tc] -= 1\n if winning(list, substr)\n res = search(cur+1, max, list, target)\n if res\n return tc+res\n end\n end\n list[tc] += 1\n end\n\n # find next char\n nc = list.keys.find{|k| k>tc and list[k]>0}\n if nc\n list[nc] -= 1\n return nc+minstr(list)\n end\n \n nil\nend\n\ns = gets.chomp\nt = gets.chomp\n\nlist = Hash.new(0)\ns.each_char do |c|\n list[c] += 1\nend\n\nans = search(0, s.size, list, t)\nif ans.nil?\n ans = -1\nend\nputs ans\n"}, {"source_code": "#!/usr/bin/ruby\n\ndef winning(list, target)\n arr = []\n list.each_pair do |key, val|\n arr += [key]*val\n end\n arr.sort.reverse.join > target\nend\n\ndef minstr(list)\n arr = []\n list.each_pair do |key,val|\n arr += [key]*val\n end\n arr.sort.join\nend\n\ndef search(cur, max, list, target)\n substr = target[cur+1..-1] || ''\n tc = target[cur] || ''\n\n #puts \"#{cur} #{list}\"\n if cur == max-1\n last = list.find{|v| v[1] == 1}\n return last[0]\n end\n\n if list[tc] > 0\n list[tc] -= 1\n if winning(list, substr)\n res = search(cur+1, max, list, target)\n if res\n return tc+res\n end\n end\n list[tc] += 1\n end\n\n # find next char\n nc = list.keys.find{|k| k>tc and list[k]>0}\n if nc\n list[nc] -= 1\n return nc+minstr(list)\n end\n \n nil\nend\n\ns = gets.chomp\nt = gets.chomp\n\nlist = Hash.new(0)\ns.each_char do |c|\n list[c] += 1\nend\n\nans = search(0, s.size, list, t)\nif ans.nil?\n ans = -1\nend\nputs ans\n"}], "src_uid": "a1739619b5ee88e22ae31f4d72bed90a"} {"nl": {"description": "Nezzar's favorite digit among $$$1,\\ldots,9$$$ is $$$d$$$. He calls a positive integer lucky if $$$d$$$ occurs at least once in its decimal representation. Given $$$q$$$ integers $$$a_1,a_2,\\ldots,a_q$$$, for each $$$1 \\le i \\le q$$$ Nezzar would like to know if $$$a_i$$$ can be equal to a sum of several (one or more) lucky numbers.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 9$$$) — the number of test cases. The first line of each test case contains two integers $$$q$$$ and $$$d$$$ ($$$1 \\le q \\le 10^4$$$, $$$1 \\le d \\le 9$$$). The second line of each test case contains $$$q$$$ integers $$$a_1,a_2,\\ldots,a_q$$$ ($$$1 \\le a_i \\le 10^9$$$).", "output_spec": "For each integer in each test case, print \"YES\" in a single line if $$$a_i$$$ can be equal to a sum of lucky numbers. Otherwise, print \"NO\". You can print letters in any case (upper or lower).", "sample_inputs": ["2\n3 7\n24 25 27\n10 7\n51 52 53 54 55 56 57 58 59 60"], "sample_outputs": ["YES\nNO\nYES\nYES\nYES\nNO\nYES\nYES\nYES\nYES\nYES\nYES\nNO"], "notes": "NoteIn the first test case, $$$24 = 17 + 7$$$, $$$27$$$ itself is a lucky number, $$$25$$$ cannot be equal to a sum of lucky numbers."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n q,d=gets.split.map(&:to_i)\r\n a=gets.split.map(&:to_i)\r\n a.each do |aa|\r\n if aa>=100\r\n puts \"YES\"\r\n else\r\n f=false\r\n while aa>0\r\n if aa/10==d||aa%10==d\r\n puts \"YES\"\r\n f=true\r\n break\r\n end\r\n aa-=d\r\n end\r\n puts \"NO\" if !f\r\n end\r\n end\r\nend\r\n"}, {"source_code": "gets.to_i.times do\r\n q,d=gets.split.map(&:to_i)\r\n a=gets.split.map(&:to_i)\r\n a.each do |aa|\r\n if d==1\r\n puts \"YES\"\r\n elsif d==2\r\n if aa>=20||aa.even?\r\n puts \"YES\"\r\n else\r\n puts \"NO\"\r\n end\r\n elsif d==3\r\n if aa>=21||aa%3==0||[13,16,19].include?(aa)\r\n puts \"YES\"\r\n else\r\n puts \"NO\"\r\n end\r\n elsif d==4\r\n if aa>=40||aa%4==0||aa>=14&&aa%4==2\r\n puts \"YES\"\r\n else\r\n puts \"NO\"\r\n end\r\n elsif d==5\r\n if aa>=50||aa%5==0\r\n puts \"YES\"\r\n else\r\n puts \"NO\"\r\n end\r\n elsif d==6\r\n if aa>=60||aa%6==0||aa>=22&&aa%2==0||aa==16\r\n puts \"YES\"\r\n else\r\n puts \"NO\"\r\n end\r\n elsif d==7\r\n if aa>=61||aa%7==0||aa>=17&&aa%7==3||aa>=27&&aa%7==6||aa>=37&&aa%7==2||[47,54,57].include?(aa)\r\n puts \"YES\"\r\n else\r\n puts \"NO\"\r\n end\r\n elsif d==8\r\n if aa>=80||aa%8==0||aa>=32&&aa%2==0||[18,26,28].include?(aa)\r\n puts \"YES\"\r\n else\r\n puts \"NO\"\r\n end\r\n elsif d==9\r\n if aa>=81||aa/10+aa%10>=9\r\n puts \"YES\"\r\n else\r\n puts \"NO\"\r\n end\r\n end\r\n end\r\nend\r\n\r\n"}], "negative_code": [], "src_uid": "7975af65a23bad6a0997921c7e31d3ca"} {"nl": {"description": "The Little Elephant loves sortings.He has an array a consisting of n integers. Let's number the array elements from 1 to n, then the i-th element will be denoted as ai. The Little Elephant can make one move to choose an arbitrary pair of integers l and r (1 ≤ l ≤ r ≤ n) and increase ai by 1 for all i such that l ≤ i ≤ r.Help the Little Elephant find the minimum number of moves he needs to convert array a to an arbitrary array sorted in the non-decreasing order. Array a, consisting of n elements, is sorted in the non-decreasing order if for any i (1 ≤ i < n) ai ≤ ai + 1 holds.", "input_spec": "The first line contains a single integer n (1 ≤ n ≤ 105) — the size of array a. The next line contains n integers, separated by single spaces — array a (1 ≤ ai ≤ 109). The array elements are listed in the line in the order of their index's increasing.", "output_spec": "In a single line print a single integer — the answer to the problem. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.", "sample_inputs": ["3\n1 2 3", "3\n3 2 1", "4\n7 4 1 47"], "sample_outputs": ["0", "2", "6"], "notes": "NoteIn the first sample the array is already sorted in the non-decreasing order, so the answer is 0.In the second sample you need to perform two operations: first increase numbers from second to third (after that the array will be: [3, 3, 2]), and second increase only the last element (the array will be: [3, 3, 3]).In the third sample you should make at least 6 steps. The possible sequence of the operations is: (2; 3), (2; 3), (2; 3), (3; 3), (3; 3), (3; 3). After that the array converts to [7, 7, 7, 47]."}, "positive_code": [{"source_code": "gets\na = gets.split.map &:to_i\ns = 0\na.each_cons(2){ |i,j| s += i-j if j self[i]\n return false\n end\n a = self[i]\n end\n return true\n end\nend\n\nn = gets.chomp.to_i\na = gets.chomp.split(\" \").map{|e| e.to_i}\nc = 0\n\nfor i in 1 .. (n - 1) do\n first = a[i - 1]\n second = a[i]\n \n if first > second\n d = first - second\n \"\"\"\n for j in i .. (n -1) do\n a[j] += d\n end \n \"\"\"\n c += d\n end \nend\n\nputs c\n"}, {"source_code": "gets\ns=0\ngets.split.map(&:to_i).each_cons(2){|i,j|s+=i-j if j numbers[i+1])\n diff = numbers[i] - numbers[i+1]\n counter += diff\n end\n}\n\nputs counter\n"}, {"source_code": "gets\ns=0\ngets.split.map(&:to_i).each_cons(2){|i,j|s+=i-j if ji) ? a-i : 0\n\ta=i\nend\nputs ans\n\n"}, {"source_code": "gets\na = gets.split.map(&:to_i)\nr = 0\n(1..a.length-1).each do |i|\n k = a[i-1]-a[i]\n r+=k if k>=0\nend\nputs r\n"}, {"source_code": "gets\ns=0\ngets.split.map(&:to_i).each_cons(2){|i,j|s+=i-j if j a[i + 1])\n}\nputs r\n"}, {"source_code": "n = gets.to_i\na = gets.split.map { |i| i.to_i }\nc, d, r = 0, 1 << 40, 0\n1.upto(n - 1) { |i|\n if a[i] < a[i - 1] \n c = [c, a[i - 1]].max \n d = [d, a[i]].min\n else\n r += [0, c - d].max \n c, d = 0, 1 << 40\n end\n}\nputs r + [0, c - d].max\n"}, {"source_code": "n = gets.to_i\na = gets.split.map { |i| i.to_i }\nputs (0..n - 2).inject(0) { |r, i|\n r + [0, a[i] - a[i + 1]].max \n}\n"}, {"source_code": "n=gets.to_i\na=gets.split.map &:to_i\ns=0\n(n-1).times{|i|s+=[0,a[i]-a[i+1]].max}\np s\n"}, {"source_code": "gets\ns=0\ngets.split.map(&:to_i).each_cons(2){|i,j|s+=i-j if j a[i]\n c = [c, a[i - 1] - a[i]].max\n a[i] = a[i - 1] \n end\n}\nputs c\n"}, {"source_code": "n = gets.to_i\na = gets.split.map { |i| i.to_i }\nc, r = 0, 0\n1.upto(n - 1) { |i|\n if a[i - 1] > a[i]\n c = [c, a[i - 1] - a[i]].max\n a[i] += c \n r = c\n end\n}\nputs c\n"}, {"source_code": "n = gets.to_i\na = gets.split.map { |i| i.to_i }\nc, d, r = 0, 1 << 40, 0\n1.upto(n - 1) { |i|\n if a[i] < a[i - 1] \n c = [c, a[i - 1]].max \n d = [d, a[i]].min\n else\n r += c - d \n c, d = 0, 1 << 40\n end\n}\nputs r + [0, c - d].max\n"}, {"source_code": "n = gets.to_i\na = gets.split.map { |i| i.to_i }\nc, r = 0, 0\n1.upto(n - 1) { |i|\n if a[i - 1] > a[i]\n c = [c, a[i - 1] - a[i]].max\n a[i] += c \n r += c\n end\n}\nputs c\n"}, {"source_code": "n = gets.to_i\na = gets.split.map { |i| i.to_i }\nputs (0..n - 2).inject { |r, i|\n r + [0, a[i] - a[i + 1]].max \n}\n"}, {"source_code": "n = gets.to_i\na = gets.split.map { |i| i.to_i }\nc, r = 0, 0\n1.upto(n - 1) { |i|\n if a[i] < a[i - 1] \n c = [c, a[i - 1] - a[i]].max \n a[i] = a[i - 1]\n else\n r += c\n c = 0 \n end\n}\nputs r + c\n"}, {"source_code": "n = gets.to_i\na = gets.split.map { |i| i.to_i }\nc, r= 0, 0\n1.upto(n - 1) { |i|\n if a[i - 1] > a[i]\n c = a[i - 1] - a[i]\n a[i] += c \n r += c\n end\n}\nputs c\n"}], "src_uid": "1cd295e204724335c63e685fcc0708b8"} {"nl": {"description": "You are given an integer a that consists of n digits. You are also given a sequence of digits s of length m. The digit in position j (1 ≤ j ≤ m) of sequence s means that you can choose an arbitrary position i (1 ≤ i ≤ n) in a and replace the digit in the chosen position i with sj. Each element in the sequence s can participate in no more than one replacing operation.Your task is to perform such sequence of replacements, that the given number a gets maximum value. You are allowed to use not all elements from s.", "input_spec": "The first line contains positive integer a. Its length n is positive and doesn't exceed 105. The second line contains sequence of digits s. Its length m is positive and doesn't exceed 105. The digits in the sequence s are written consecutively without any separators. The given number a doesn't contain leading zeroes. ", "output_spec": "Print the maximum value that can be obtained from a after a series of replacements. You are allowed to use not all elements from s. The printed number shouldn't contain any leading zeroes.", "sample_inputs": ["1024\n010", "987\n1234567"], "sample_outputs": ["1124", "987"], "notes": null}, "positive_code": [{"source_code": "a = gets.scan(/\\d/).map(&:to_i)\nc = [0] * 10\ngets.scan(/\\d/) do |_|\n c[_.to_i] += 1\nend\na.size.times do |i|\n k = c.rindex{|_| _ > 0}\n if k && k > a[i]\n a[i] = k\n c[k] -= 1\n end\nend\nputs a * ''\n"}, {"source_code": "given = STDIN.readline.strip.split('')\ndigits = STDIN.readline.strip.split('').sort.reverse\npos = 0\ndigits.each do |digit|\n while pos < given.length and given[pos] >= digit\n pos += 1\n end\n break if pos == given.length\n given[pos] = digit\n pos += 1\nend\nputs given.join\n\n"}, {"source_code": "a = readline.strip.each_char.to_a\ns = readline.strip.each_char.to_a.sort\n\nans = a.each.map { |ai| (ai.to_i >= s.last.to_i) ? ai : s.pop }\nputs ans.join\n"}, {"source_code": "# encoding: utf-8\n\ndef string_to_digit_array(s)\n ret = []\n s.each_char do |c|\n ret << c\n end\n ret\nend\n\na = string_to_digit_array(readline.strip)\ns = string_to_digit_array(readline.strip).sort\n\nans = []\na.each do |digit|\n if digit.to_i >= s.last.to_i\n ans << digit\n else\n ans << s.pop\n end\nend\n\nputs ans.join\n"}, {"source_code": "# ProblemB.rb\n\na = gets.chomp.split(//)\ns = gets.chomp\n\ns = s.split(//).sort.reverse\n\nj = 0\na.each_with_index do |c, i|\n if j == s.size\n break\n end\n if c < s[j]\n a[i] = s[j]\n j += 1\n end\nend\n\nputs a.join\n"}, {"source_code": "n = gets.chomp.split(\"\")\ndlina = n.length\nk = gets.chomp.split(\"\")\ndlina2 = k.length\nk = k.sort\np = dlina2-1\n\nfor i in (0..dlina-1)\n n[i] = n[i].to_i\nend \n\nfor i in (0..p)\n k[i] = k[i].to_i\nend\n\nfor i in (0..dlina-1)\n if k[p] > n[i] then\n n[i] = k[p]\n p-=1\n end\n\n if p < 0 then \n break\n end\nend\n\nfor i in 0..dlina-1 do\n print n[i]\nend"}], "negative_code": [{"source_code": "# encoding: utf-8\n\na = readline\ns = readline\nputs (a+s)\n"}, {"source_code": "# ProblemB.rb\n\na = gets.chomp.split(//)\ns = gets.chomp\n\ns = s.split(//).sort.reverse\n\nj = 0\na.each_with_index do |c, i|\n if c < s[j]\n a[i] = s[j]\n j += 1\n end\nend\n\np a.join\n"}], "src_uid": "a2cd56f2d2c3dd9fe152b8e4111df7d4"} {"nl": {"description": "Marin wants you to count number of permutations that are beautiful. A beautiful permutation of length $$$n$$$ is a permutation that has the following property: $$$$$$ \\gcd (1 \\cdot p_1, \\, 2 \\cdot p_2, \\, \\dots, \\, n \\cdot p_n) > 1, $$$$$$ where $$$\\gcd$$$ is the greatest common divisor.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3, 4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 10^3$$$) — the number of test cases. Each test case consists of one line containing one integer $$$n$$$ ($$$1 \\le n \\le 10^3$$$).", "output_spec": "For each test case, print one integer — number of beautiful permutations. Because the answer can be very big, please print the answer modulo $$$998\\,244\\,353$$$.", "sample_inputs": ["7\n1\n2\n3\n4\n5\n6\n1000"], "sample_outputs": ["0\n1\n0\n4\n0\n36\n665702330"], "notes": "NoteIn first test case, we only have one permutation which is $$$[1]$$$ but it is not beautiful because $$$\\gcd(1 \\cdot 1) = 1$$$.In second test case, we only have one beautiful permutation which is $$$[2, 1]$$$ because $$$\\gcd(1 \\cdot 2, 2 \\cdot 1) = 2$$$. "}, "positive_code": [{"source_code": "t = gets.to_i\r\nwhile t > 0\r\n t = t - 1\r\n n = gets.to_i\r\n if n & 1 == 1 \r\n puts \"0\"\r\n else \r\n ans = 1 \r\n for i in 1..n/2\r\n ans = (ans * (i * i))%998244353\r\n end\r\n puts ans\r\n end\r\nend"}], "negative_code": [], "src_uid": "0b718a81787c3c5c1aa5b92834ee8bf5"} {"nl": {"description": "Recently Vova found $$$n$$$ candy wrappers. He remembers that he bought $$$x$$$ candies during the first day, $$$2x$$$ candies during the second day, $$$4x$$$ candies during the third day, $$$\\dots$$$, $$$2^{k-1} x$$$ candies during the $$$k$$$-th day. But there is an issue: Vova remembers neither $$$x$$$ nor $$$k$$$ but he is sure that $$$x$$$ and $$$k$$$ are positive integers and $$$k > 1$$$.Vova will be satisfied if you tell him any positive integer $$$x$$$ so there is an integer $$$k>1$$$ that $$$x + 2x + 4x + \\dots + 2^{k-1} x = n$$$. It is guaranteed that at least one solution exists. Note that $$$k > 1$$$.You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$3 \\le n \\le 10^9$$$) — the number of candy wrappers Vova found. It is guaranteed that there is some positive integer $$$x$$$ and integer $$$k>1$$$ that $$$x + 2x + 4x + \\dots + 2^{k-1} x = n$$$.", "output_spec": "Print one integer — any positive integer value of $$$x$$$ so there is an integer $$$k>1$$$ that $$$x + 2x + 4x + \\dots + 2^{k-1} x = n$$$.", "sample_inputs": ["7\n3\n6\n7\n21\n28\n999999999\n999999984"], "sample_outputs": ["1\n2\n1\n7\n4\n333333333\n333333328"], "notes": "NoteIn the first test case of the example, one of the possible answers is $$$x=1, k=2$$$. Then $$$1 \\cdot 1 + 2 \\cdot 1$$$ equals $$$n=3$$$.In the second test case of the example, one of the possible answers is $$$x=2, k=2$$$. Then $$$1 \\cdot 2 + 2 \\cdot 2$$$ equals $$$n=6$$$.In the third test case of the example, one of the possible answers is $$$x=1, k=3$$$. Then $$$1 \\cdot 1 + 2 \\cdot 1 + 4 \\cdot 1$$$ equals $$$n=7$$$.In the fourth test case of the example, one of the possible answers is $$$x=7, k=2$$$. Then $$$1 \\cdot 7 + 2 \\cdot 7$$$ equals $$$n=21$$$.In the fifth test case of the example, one of the possible answers is $$$x=4, k=3$$$. Then $$$1 \\cdot 4 + 2 \\cdot 4 + 4 \\cdot 4$$$ equals $$$n=28$$$."}, "positive_code": [{"source_code": "# Author : Fuad Ashraful Mehmet \n#Date: 22th April\n\nt=gets.to_i\n\ndef Solve()\n n=gets.to_i\n k=4\n a=3\n while n.modulo(a)!=0\n a+=k\n k<<=1\n end\n\n return n/a\nend\n(1).upto(t) do\n\n puts Solve()\nend "}, {"source_code": "gets.to_i.times do\n\tn = gets.to_i\n\ti = (2..64).find { |i| n % (2**i - 1) == 0 }\n\tputs n / (2**i - 1)\nend\n"}, {"source_code": "class Result\n def print_result\n n = gets.to_i\n 2.upto(29) do |i|\n if n % (2**i - 1) == 0\n puts n / (2**i - 1)\n break\n end\n end\n end\nend\n\ngets.to_i.times do\n Result.new.print_result\nend"}, {"source_code": "1.upto(gets.chomp.to_i) do\n n = gets.chomp.to_i\n k = 2\n m = 4\n while n % (m-1) != 0\n m *= 2;\n k += 1;\n end\n puts n / (m-1)\nend\n"}, {"source_code": "number_of_testcases = gets.to_s.to_i\n\nnumber_of_testcases.times do |i|\n \n n = gets.to_s.to_i\n s = 2\n loop do \n \n s *= 2\n t = s - 1\n if n % t == 0\n puts n / t\n break\n end\n end\nend\n\n"}, {"source_code": "gets.to_i.times do\n n = gets.to_i\n k = 2\n loop do\n s = 2**k - 1\n if n%s == 0\n puts n/s\n break\n end\n k += 1\n end\nend\n"}, {"source_code": "t = gets.to_i\n\n1.upto(t) do\n n = gets.to_i\n\n 2.upto(32) do |k|\n next unless n.modulo((2 ** k) - 1).zero?\n\n puts n.div((2 ** k) - 1)\n break\n end\nend\n"}, {"source_code": "t=gets.to_i\n(1).upto(t) do\n n=gets.to_i\n k=4\n a=3\n while n.modulo(a)!=0\n a+=k\n k<<=1\n end\n puts n/a\nend\n"}], "negative_code": [], "src_uid": "d04cbe78b836e53b51292401c8c969b2"} {"nl": {"description": "DZY has a hash table with p buckets, numbered from 0 to p - 1. He wants to insert n numbers, in the order they are given, into the hash table. For the i-th number xi, DZY will put it into the bucket numbered h(xi), where h(x) is the hash function. In this problem we will assume, that h(x) = x mod p. Operation a mod b denotes taking a remainder after division a by b.However, each bucket can contain no more than one element. If DZY wants to insert an number into a bucket which is already filled, we say a \"conflict\" happens. Suppose the first conflict happens right after the i-th insertion, you should output i. If no conflict happens, just output -1.", "input_spec": "The first line contains two integers, p and n (2 ≤ p, n ≤ 300). Then n lines follow. The i-th of them contains an integer xi (0 ≤ xi ≤ 109).", "output_spec": "Output a single integer — the answer to the problem.", "sample_inputs": ["10 5\n0\n21\n53\n41\n53", "5 5\n0\n1\n2\n3\n4"], "sample_outputs": ["4", "-1"], "notes": null}, "positive_code": [{"source_code": "p,n = gets.split.map{|i| i.to_i}\nh=[]\nn.times{|i| h<0}\nk=0\n1.upto(a[0].to_i-1) do |i|\nhash[\"#{i}\"]=0\nend\n\n1.upto(a[1].to_i) do |i|\ntemp=(gets.chomp.to_i)%a[0].to_i\nhash[\"#{temp}\"]+=1\nif hash[\"#{temp}\"]>1 && k==0\nk=i\nend\nend\n\nif k==0\nputs \"-1\"\nelse \nputs \"#{k}\"\nend"}, {"source_code": "p,n = gets.split.map{|i| i.to_i}\nh=[]\nn.times{|i| h<0}\nk=0\n1.upto(a[0].to_i-1) do |i|\nhash[\"#{i}\"]=0\nend\n\n1.upto(a[1].to_i) do |i|\ntemp=(gets.chomp.to_i)%a[0].to_i\nhash[\"#{temp}\"]+=1\nif hash[\"#{temp}\"]>1 && k==0\nk=i\nend\nend\n\nif k==0\nputs \"-1\"\nelse \nputs \"#{k}\"\nend"}, {"source_code": "numbers = gets.split(' ')\np = numbers[0].to_i\nn = numbers[1].to_i\n\nconflicts = false\nhashtable = Array.new()\nn.times do |i|\n x = gets.to_i\n next if conflicts\n if hashtable[x % p].nil?\n hashtable[x % p] = x\n else\n conflicts = true\n puts i + 1\n end\nend\n\nputs -1 unless conflicts"}, {"source_code": "p,n = gets.split.map{|i| i.to_i}\nh=[]\nn.times{|i| h<0}\nk=0\n1.upto(a[0].to_i-1) do |i|\nhash[\"#{i}\"]=0\nend\n\n1.upto(a[1].to_i) do |i|\ntemp=(gets.chomp.to_i)%a[0].to_i\nhash[\"#{temp}\"]+=1\nif hash[\"#{temp}\"]>1 && k==0\nk=i\nend\nend\n\nif k==0\nputs \"-1\"\nelse \nputs \"#{k}\"\nend"}, {"source_code": "p,n=gets.split.map(&:to_i)\n\nmyHash=Hash.new()\n\nfor i in 0...n do\n myHash[i]=nil\nend\n\nfound=-1\nfor i in 1..n do \n x=gets.split[0].to_i\n h=x%p\n if(myHash[h]==nil)\n myHash[h]=x\n else \n found=i\n break\n end\nend\n\np found\n"}, {"source_code": "p, n = gets.split(\" \").collect { |e| e.to_i}\n\narr = Array.new(p) { |i| 0}\ni = 1\nwhile i <= n\n num = gets.to_i\n mod = num % p\n if arr[mod] == 1\n break\n end\n arr[mod] += 1\n i += 1\nend\nputs i == n + 1 ? \"-1\" : i"}, {"source_code": "p,n = gets.split.map{|i| i.to_i}\nh=[]\nn.times{|i| h< inc.last ? inc.push(x) : acc.push(x)\n acc\n end\n\na.reverse!\n\ndec.push(a.shift) if a.size > 0\na = a.reduce([]) do |acc, x|\n x < dec.last ? dec.push(x) : acc.push(x)\n acc\n end\n\nif a.size == 0\n puts \"YES\"\n puts inc.size\n inc.each_with_index { |x, i| print \" \" if i > 0; print x }\n print \"\\n\"\n puts dec.size\n dec.each_with_index { |x, i| print \" \" if i > 0; print x }\n print \"\\n\"\nelse\n puts \"NO\"\nend\n"}, {"source_code": "\nn = gets.strip.to_i\na = gets.strip.split(\" \").map(&:to_i)\n\nh = Hash.new(0)\n\nres = \"\"\na.each do |e|\n h[e] += 1\n res = \"No\" if h[e] > 2 \nend\n\nif res == \"No\"\n puts res\nelse\n a.sort!\n b = a.select.with_index { |e, idx| idx.even? }\n c = a.select.with_index { |e, idx| idx.odd? }\n puts \"Yes\"\n puts b.count\n puts b.join(\" \")\n puts c.count\n puts c.reverse.join(\" \")\nend\n\n\n"}], "negative_code": [{"source_code": "\nn = gets.strip.to_i\na = gets.strip.split(\" \").map(&:to_i)\n\nh = Hash.new(0)\n\nres = \"\"\na.each do |e|\n h[e] += 1\n res = \"No\" if h[e] > 2 \nend\n\nif res == \"No\"\n puts res\nelse\n a.sort!\n b = a.select.with_index { |e, idx| idx.even? }\n c = a.select.with_index { |e, idx| idx.odd? }\n puts \"Yes\"\n puts b.reduce(&:+)\n puts b.join(\" \")\n puts c.reduce(&:+)\n puts c.reverse.join(\" \")\nend\n\n\n"}], "src_uid": "cdb19d87ad3713dac104252737153411"} {"nl": {"description": "Masha wants to open her own bakery and bake muffins in one of the n cities numbered from 1 to n. There are m bidirectional roads, each of whose connects some pair of cities.To bake muffins in her bakery, Masha needs to establish flour supply from some storage. There are only k storages, located in different cities numbered a1, a2, ..., ak.Unforunately the law of the country Masha lives in prohibits opening bakery in any of the cities which has storage located in it. She can open it only in one of another n - k cities, and, of course, flour delivery should be paid — for every kilometer of path between storage and bakery Masha should pay 1 ruble.Formally, Masha will pay x roubles, if she will open the bakery in some city b (ai ≠ b for every 1 ≤ i ≤ k) and choose a storage in some city s (s = aj for some 1 ≤ j ≤ k) and b and s are connected by some path of roads of summary length x (if there are more than one path, Masha is able to choose which of them should be used).Masha is very thrifty and rational. She is interested in a city, where she can open her bakery (and choose one of k storages and one of the paths between city with bakery and city with storage) and pay minimum possible amount of rubles for flour delivery. Please help Masha find this amount.", "input_spec": "The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 105, 0 ≤ k ≤ n) — the number of cities in country Masha lives in, the number of roads between them and the number of flour storages respectively. Then m lines follow. Each of them contains three integers u, v and l (1 ≤ u, v ≤ n, 1 ≤ l ≤ 109, u ≠ v) meaning that there is a road between cities u and v of length of l kilometers . If k > 0, then the last line of the input contains k distinct integers a1, a2, ..., ak (1 ≤ ai ≤ n) — the number of cities having flour storage located in. If k = 0 then this line is not presented in the input.", "output_spec": "Print the minimum possible amount of rubles Masha should pay for flour delivery in the only line. If the bakery can not be opened (while satisfying conditions) in any of the n cities, print  - 1 in the only line.", "sample_inputs": ["5 4 2\n1 2 5\n1 2 3\n2 3 4\n1 4 10\n1 5", "3 1 1\n1 2 3\n3"], "sample_outputs": ["3", "-1"], "notes": "NoteImage illustrates the first sample case. Cities with storage located in and the road representing the answer are darkened. "}, "positive_code": [{"source_code": "# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n############################################################\ndef gs() gets.chomp end\ndef gi() gets.to_i end\ndef gss() gs.split end\ndef gis() gss.map(&:to_i) end\ndef nmapf(n,f) n.times.map{ __send__ f } end\ndef ngs(n) nmapf n,:gs end\ndef ngi(n) nmapf n,:gi end\ndef ngss(n) nmapf n,:gss end\ndef ngis(n) nmapf n,:gis end\ndef arr2d(h,w,v=0) h.times.map{[v] * w} end\ndef for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end\ndef nsum(n) n * (n + 1) / 2 end\ndef vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end\n############################################################\n\nN, M, K = gis\nUVL = ngis M\nST = K > 0 ? gis : []\n\nh = [-1] * (N + 1)\nf = [true] * (N + 1)\n\nST.each do |a|\n f[a] = false\nend\n\nmn = nil\n\nUVL.each do |u, v, l|\n next if !(f[u] ^ f[v])\n mn = l if mn.nil? || l < mn\nend\n\nputs mn.nil? ? -1 : mn\n\n\n\n\n"}, {"source_code": "class Array\n def r\n self.map { |q| q.to_i }\n end\nend\n\ndef read_line\n STDIN.gets.split(\" \")\nend\n\ndef read_to_array\n read_line.to_a\nend\n\ndef read_numbers_to_array\n read_to_array.r\nend\n\nfirst = Array.new(100009)\nsecond = Array.new(100009)\nlen = Array.new(100009)\n\ncities, roads, k = read_numbers_to_array\nfor i in 0..roads - 1 do\n first[i], second[i], len[i] = read_numbers_to_array\nend\n\nshortest = -1\nif k > 0\nb = read_numbers_to_array\nbakers = Array.new(cities + 9, false)\n\nb.each do |i|\n bakers[i] = true\nend\n\nfor i in 0..roads - 1 do\n if bakers[first[i]] != bakers[second[i]] && (shortest == - 1 || len[i] < shortest)\n shortest = len[i]\n end\nend\nend\n\nputs shortest\n"}, {"source_code": "n, m, k = gets.split.map(&:to_i)\nv = m.times.map{gets.split.map(&:to_i)}\ns = Array.new(n+1, false)\ngets.split.map(&:to_i).each {|x| s[x] = true } if k>0\nans = nil\nv.each { |a, b, l| ans = l if (s[a]^s[b])&&(ans.nil? || l 0 ? gets.split.map(&:to_i) : []\narr.each{|x| storage[x] = true}\nans = nil\ndata.each do |a, b, l|\n ans = l if (storage[a] ^ storage[b]) && (ans.nil? || l < ans)\nend\nputs ans.nil? ? -1 : ans\n"}, {"source_code": " class Node\n\tattr_accessor :dest, :val\n\n\tdef initialize dest, val\n\t\t@dest = dest\n\t\t@val = val\n\tend\n\n\tdef to_s\n\t\tprint \"Dest : #{dest}, Val : #{val}\"\n\tend\nend\n\nn, m, k = gets.chomp.split(' ').map(&:to_i)\nfound = false\nnearest = Array.new(n + 1) { |i| i = {} }\n\n\nm.times do\n\tu, v, l = gets.chomp.split(' ').map(&:to_i)\n\n\tif u != v\n\t\tif nearest[u].has_key? v\n\t\t\tif nearest[u][v] > l\n\t\t\t\tnearest[u][v] = l\n\t\t\tend\n\t\telse\n\t\t\tnearest[u][v] = l\n\t\tend\n\n\t\tif nearest[v].has_key? u\n\t\t\tif nearest[v][u] > l\n\t\t\t\tnearest[v][u] = l\n\t\t\tend\n\t\telse\n\t\t\tnearest[v][u] = l\n\t\tend\n\tend\nend\n\nstorage = Array.new(n + 1) { |i| i = false }\n\nif k > 0\n\tinputStorage = gets.chomp.split(' ').map(&:to_i)\n\n\tinputStorage.each do |i|\n\t\tstorage[i] = true\n\tend\nend\n\nans = 1000000000000\n\n1.upto(n) do |city|\n\tnext if !storage[city]\t\n\n\tnearest[city].each do |dest, val|\n\t\tif !storage[dest]\n\t\t\tans = val if ans > val\n\t\t\tfound = true\n\t\tend\n\tend\nend\n\nif !found\n\tputs -1\nelse\n\tputs ans\nend"}], "negative_code": [{"source_code": "n, m, k = gets.split.map(&:to_i)\nnn = 1000000000\nh = Hash.new(nn)\nm.times do\n a, b, l = gets.split.map(&:to_i)\n h[a] = [h[a], l].min\n h[b] = [h[b], l].min\nend\narr = k > 0 ? gets.split.map(&:to_i) : []\nmi = h[arr.min_by{|x| h[x]}]\nputs mi.nil? || mi == nn ? -1 : mi\n"}, {"source_code": "class Node\n\tattr_accessor :dest, :val\n\n\tdef initialize dest, val\n\t\t@dest = dest\n\t\t@val = val\n\tend\nend\n\nn, m, k = gets.chomp.split(' ').map(&:to_i)\nfound = false\nnearest = Array.new(n + 1) { |i| i = Node.new(-1, 100000000) }\n\n\nm.times do\n\tu, v, l = gets.chomp.split(' ').map(&:to_i)\n\n\tif u != v\n\tif nearest[u].val > l\n\t\tnearest[u].dest = v\n\t\tnearest[u].val = l\n\tend\n\n\tif nearest[v].val > l\n\t\tnearest[v].dest = u\n\t\tnearest[v].val = l\n\tend\n\tend\nend\n\nstorage = Array.new(k + 1) { |i| i = false }\n\nif k > 0\n\tinputStorage = gets.chomp.split(' ').map(&:to_i)\n\n\tinputStorage.each do |i|\n\t\tstorage[i] = true\n\tend\nend\n\nans = 100000000\n\n1.upto(n) do |city|\n\t#puts \"city = #{city}\"\n\n\tnext if !storage[city]\t\n\tnext if nearest[city].dest < 0\n\n\tif !storage[nearest[city].dest]\n\t\tans = nearest[city].val if ans > nearest[city].val\n\t\tfound = true\n\tend\n\n\t#puts \"ans = #{ans}\"\nend\n\nif !found\n\tputs -1\nelse\n\tputs ans\nend"}, {"source_code": "class Node\n\tattr_accessor :dest, :val\n\n\tdef initialize dest, val\n\t\t@dest = dest\n\t\t@val = val\n\tend\nend\n\nn, m, k = gets.chomp.split(' ').map(&:to_i)\nfound = false\nnearest = Array.new(n + 1) { |i| i = Node.new(-1, 100000000) }\n\n\nm.times do\n\tu, v, l = gets.chomp.split(' ').map(&:to_i)\n\n\tif nearest[u].val > l\n\t\tnearest[u].dest = v\n\t\tnearest[u].val = l\n\tend\n\n\tif nearest[v].val > l\n\t\tnearest[v].dest = u\n\t\tnearest[v].val = l\n\tend\nend\n\nstorage = Array.new(k + 1) { |i| i = false }\n\nif k > 0\n\tinputStorage = gets.chomp.split(' ').map(&:to_i)\n\n\tinputStorage.each do |i|\n\t\tstorage[i] = true\n\tend\nend\n\nans = 100000000\n\n1.upto(n) do |city|\n\t#puts \"city = #{city}\"\n\n\tnext if !storage[city]\t\n\tnext if nearest[city].dest < 0\n\n\tif !storage[nearest[city].dest]\n\t\tans = nearest[city].val if ans > nearest[city].val\n\t\tfound = true\n\tend\n\n\t#puts \"ans = #{ans}\"\nend\n\nif !found\n\tputs -1\nelse\n\tputs ans\nend"}, {"source_code": "class Node\n\tattr_accessor :dest, :val\n\n\tdef initialize dest, val\n\t\t@dest = dest\n\t\t@val = val\n\tend\nend\n\nn, m, k = gets.chomp.split(' ').map(&:to_i)\nfound = false\nnearest = Array.new(n + 1) { |i| i = Node.new(-1, 100000000) }\n\n\nm.times do\n\tu, v, l = gets.chomp.split(' ').map(&:to_i)\n\n\tif nearest[u].val > l\n\t\tnearest[u].dest = v\n\t\tnearest[u].val = l\n\tend\n\n\tif nearest[v].val > l\n\t\tnearest[v].dest = u\n\t\tnearest[v].val = l\n\tend\nend\n\nstorage = Array.new(k + 1) { |i| i = false }\n\nif k > 0\n\tinputStorage = gets.chomp.split(' ').map(&:to_i)\n\n\tinputStorage.each do |i|\n\t\tstorage[i] = true\n\tend\nend\n\nans = 0\n\n1.upto(n) do |city|\n\tnext if !storage[city]\t\n\t\n\tif !storage[nearest[city].dest]\n\t\tans = nearest[city].val\n\t\tfound = true\n\tend\nend\n\nif !found\n\tputs -1\nelse\n\tputs ans\nend"}, {"source_code": " class Node\n\tattr_accessor :dest, :val\n\n\tdef initialize dest, val\n\t\t@dest = dest\n\t\t@val = val\n\tend\n\n\tdef to_s\n\t\tprint \"Dest : #{dest}, Val : #{val}\"\n\tend\nend\n\nn, m, k = gets.chomp.split(' ').map(&:to_i)\nfound = false\nnearest = Array.new(n + 1) { |i| i = {} }\n\n\nm.times do\n\tu, v, l = gets.chomp.split(' ').map(&:to_i)\n\n\tif u != v\n\t\tif nearest[u].has_key? v\n\t\t\tif nearest[u][v] > l\n\t\t\t\tnearest[u][v] = l\n\t\t\tend\n\t\telse\n\t\t\tnearest[u][v] = l\n\t\tend\n\n\t\tif nearest[v].has_key? u\n\t\t\tif nearest[v][u] > l\n\t\t\t\tnearest[v][u] = l\n\t\t\tend\n\t\telse\n\t\t\tnearest[v][u] = l\n\t\tend\n\tend\nend\n\nstorage = Array.new(n + 1) { |i| i = false }\n\nif k > 0\n\tinputStorage = gets.chomp.split(' ').map(&:to_i)\n\n\tinputStorage.each do |i|\n\t\tstorage[i] = true\n\tend\nend\n\nans = 100000000\n\n1.upto(n) do |city|\n\tnext if !storage[city]\t\n\n\tnearest[city].each do |dest, val|\n\t\tif !storage[dest]\n\t\t\tans = val if ans > val\n\t\t\tfound = true\n\t\tend\n\tend\nend\n\nif !found\n\tputs -1\nelse\n\tputs ans\nend"}], "src_uid": "b0e6a9b500b3b75219309b5e6295e105"} {"nl": {"description": "Yaroslav is playing a game called \"Time\". The game has a timer showing the lifespan he's got left. As soon as the timer shows 0, Yaroslav's character dies and the game ends. Also, the game has n clock stations, station number i is at point (xi, yi) of the plane. As the player visits station number i, he increases the current time on his timer by ai. The stations are for one-time use only, so if the player visits some station another time, the time on his timer won't grow.A player spends d·dist time units to move between stations, where dist is the distance the player has covered and d is some constant. The distance between stations i and j is determined as |xi - xj| + |yi - yj|.Initially, the player is at station number 1, and the player has strictly more than zero and strictly less than one units of time. At station number 1 one unit of money can increase the time on the timer by one time unit (you can buy only integer number of time units).Now Yaroslav is wondering, how much money he needs to get to station n. Help Yaroslav. Consider the time to buy and to increase the timer value negligibly small.", "input_spec": "The first line contains integers n and d (3 ≤ n ≤ 100, 103 ≤ d ≤ 105) — the number of stations and the constant from the statement. The second line contains n - 2 integers: a2, a3, ..., an - 1 (1 ≤ ai ≤ 103). The next n lines contain the coordinates of the stations. The i-th of them contains two integers xi, yi (-100 ≤ xi, yi ≤ 100). It is guaranteed that no two stations are located at the same point.", "output_spec": "In a single line print an integer — the answer to the problem.", "sample_inputs": ["3 1000\n1000\n0 0\n0 1\n0 3", "3 1000\n1000\n1 0\n1 1\n1 2"], "sample_outputs": ["2000", "1000"], "notes": null}, "positive_code": [{"source_code": "#@n, @d = 100, 1000\n#@as = (0..100).to_a.map do rand(1000) end\n#@ps = (0..100).to_a.map do [rand(200)-100, rand(200)-100, nil] end\n\n@n, @d = gets.strip.split(' ').map {|x| x.to_i}\n@as = gets.strip.split(' ').map {|x| x.to_i}\n@ps = (0...@n).to_a.map do \n (gets.strip.split(' ').map {|x| x.to_i}) << nil\nend\n\ndef dist(n1, n2)\n ((@ps[n1][0] - @ps[n2][0]).abs + (@ps[n1][1] - @ps[n2][1]).abs)*@d\nend\n\n@sum = @as.inject(0, :+)\n@base = -dist(0, @n - 1)\n\n@c = {} \n@b = nil\n\ndef it(stn, currt, mint, mask, sum, d = 0)\n d += 1\n return if mint < @base\n if @b.nil? || @b < @base\n @b = @base\n end\n \n if stn == @n - 1\n @b = mint if @b.nil? || mint > @b\n return\n end\n return if @b && mint <= @b\n \n mask = mask.clone\n mask[stn] = '1'\n k = mask + stn.to_s\n v = @c[k]\n return if v && v >= mint\n @ps[stn][2] = mint\n currt += @as[stn - 1] if stn > 0\n \n e = currt - dist(stn, @n-1)\n x = [mint, e].min\n @base = x if @base < x \n \n @ps.each_with_index do |ps, i|\n at = currt - dist(i, stn)\n nmint = [mint, at].min\n next if ps[2] && ps[2] >= nmint\n it(i, at, nmint, mask, sum + @as[stn - 1], d)\n return if @b && mint <= @b\n return if mint < @base \n end\n \n v = @c[k]\n @c[k] = mint if v.nil? || v < mint\n \n #@ps[stn][2] = false\n #p [@base, @b]\nend\n\nit(0, 0, 0, '0'*@n, 0)\nputs -@b"}], "negative_code": [{"source_code": "\n\n@n, @d = gets.strip.split(' ').map {|x| x.to_i}\n@as = gets.strip.split(' ').map {|x| x.to_i}\n@ps = (0...@n).to_a.map do \n (gets.strip.split(' ').map {|x| x.to_i}) << false\nend\n\n@b = nil\ndef it(stn, currt, mint)\n #p [stn, currt, mint]\n return if @b && currt < @b\n if stn == @n - 1\n @b = mint if @b.nil? || mint > @b\n return\n end\n \n @ps[stn][2] = true\n currt += @as[stn - 1] if stn > 0\n \n @ps.each_with_index do |ps, i|\n next if ps[2]\n at = currt - dist(i, stn)\n it(i, at, [mint, at].min )\n return if @b && currt < @b \n end\n \n @ps[stn][2] = false\n \nend\n\ndef dist(n1, n2)\n (@ps[n1][0] - @ps[n2][0]).abs + (@ps[n1][1] - @ps[n2][1]).abs*@d \nend\n\nit(0, 0, 0)\nputs -@b"}, {"source_code": "@n, @d = 100, 1000\n@as = (0..100).to_a.map do rand(1000) end\n@ps = (0..100).to_a.map do [rand(200)-100, rand(200)-100] end\n\n#@n, @d = gets.strip.split(' ').map {|x| x.to_i}\n#@as = gets.strip.split(' ').map {|x| x.to_i}\n#@ps = (0...@n).to_a.map do \n# (gets.strip.split(' ').map {|x| x.to_i}) << false\n#end\n\ndef dist(n1, n2)\n ((@ps[n1][0] - @ps[n2][0]).abs + (@ps[n1][1] - @ps[n2][1]).abs)*@d\nend\n\n@sum = @as.inject(0, :+)\n@base = -dist(0, @n - 1)\n\n@c = {} \n@b = nil\n\ndef it(stn, currt, mint, mask, sum)\n return if currt < @base\n x = currt - dist(stn, @n-1)\n @base = x if @base < x \n \n if stn == @n - 1\n @b = mint if @b.nil? || mint > @b\n return\n end\n if @b\n return if currt <= @b\n return if( (currt - dist(stn, @n - 1) + @sum - sum) <= @b )\n end\n \n mask = mask.clone\n mask[stn] = '1'\n k = mask + stn.to_s\n v = @c[k]\n return if v && v >= currt\n @ps[stn][2] = true\n currt += @as[stn - 1] if stn > 0\n \n @ps.each_with_index do |ps, i|\n next if ps[2]\n at = currt - dist(i, stn)\n it(i, at, [mint, at].min, mask, sum + @as[stn - 1])\n return if @b && currt <= @b \n end\n \n v = @c[k]\n @c[k] = currt if v.nil? || v < currt\n \n @ps[stn][2] = false\n #p [@base, @b]\nend\n\nit(0, 0, 0, '0'*@n, 0)\nputs -@b"}], "src_uid": "991a9b3904884c8d19ec7c665f2fcd95"} {"nl": {"description": "Polycarp has spent the entire day preparing problems for you. Now he has to sleep for at least $$$a$$$ minutes to feel refreshed.Polycarp can only wake up by hearing the sound of his alarm. So he has just fallen asleep and his first alarm goes off in $$$b$$$ minutes.Every time Polycarp wakes up, he decides if he wants to sleep for some more time or not. If he's slept for less than $$$a$$$ minutes in total, then he sets his alarm to go off in $$$c$$$ minutes after it is reset and spends $$$d$$$ minutes to fall asleep again. Otherwise, he gets out of his bed and proceeds with the day.If the alarm goes off while Polycarp is falling asleep, then he resets his alarm to go off in another $$$c$$$ minutes and tries to fall asleep for $$$d$$$ minutes again.You just want to find out when will Polycarp get out of his bed or report that it will never happen.Please check out the notes for some explanations of the example.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of testcases. The only line of each testcase contains four integers $$$a, b, c, d$$$ ($$$1 \\le a, b, c, d \\le 10^9$$$) — the time Polycarp has to sleep for to feel refreshed, the time before the first alarm goes off, the time before every succeeding alarm goes off and the time Polycarp spends to fall asleep.", "output_spec": "For each test case print one integer. If Polycarp never gets out of his bed then print -1. Otherwise, print the time it takes for Polycarp to get out of his bed.", "sample_inputs": ["7\n10 3 6 4\n11 3 6 4\n5 9 4 10\n6 5 2 3\n1 1 1 1\n3947465 47342 338129 123123\n234123843 13 361451236 361451000"], "sample_outputs": ["27\n27\n9\n-1\n1\n6471793\n358578060125049"], "notes": "NoteIn the first testcase Polycarp wakes up after $$$3$$$ minutes. He only rested for $$$3$$$ minutes out of $$$10$$$ minutes he needed. So after that he sets his alarm to go off in $$$6$$$ minutes and spends $$$4$$$ minutes falling asleep. Thus, he rests for $$$2$$$ more minutes, totaling in $$$3+2=5$$$ minutes of sleep. Then he repeats the procedure three more times and ends up with $$$11$$$ minutes of sleep. Finally, he gets out of his bed. He spent $$$3$$$ minutes before the first alarm and then reset his alarm four times. The answer is $$$3+4 \\cdot 6 = 27$$$.The second example is almost like the first one but Polycarp needs $$$11$$$ minutes of sleep instead of $$$10$$$. However, that changes nothing because he gets $$$11$$$ minutes with these alarm parameters anyway.In the third testcase Polycarp wakes up rested enough after the first alarm. Thus, the answer is $$$b=9$$$.In the fourth testcase Polycarp wakes up after $$$5$$$ minutes. Unfortunately, he keeps resetting his alarm infinitely being unable to rest for even a single minute :("}, "positive_code": [{"source_code": "t=gets.to_i\nfor i in 1..t\n a,b,c,d = gets.split(\" \").map(&:to_i)\n sum=0\n if(a<=b)\n print(b,\"\\n\")\n elsif(d>=c)\n print(-1,\"\\n\")\n else\n sum += b\n a -= b\n tt = (a.to_f/(c-d)).ceil()\n sum += tt*c\n print(sum,\"\\n\")\n end\nend\n"}, {"source_code": "t=gets.to_i\nfor i in 1..t\n a,b,c,d = gets.split(\" \").map(&:to_i)\n sum=0\n if(a<=b)\n print(b,\"\\n\")\n elsif(d>=c)\n print(-1,\"\\n\")\n else\n sum += b\n a -= b\n sum += ((a+c-d-1)/(c-d)*c).floor()\n print(sum,\"\\n\")\n end\nend\n"}], "negative_code": [], "src_uid": "1ab174688ba76168ca047ed2b06b0670"} {"nl": {"description": "A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.", "input_spec": "The first input line contains two integers n and k (1 ≤ n ≤ 5·105; 2 ≤ k ≤ 26). The second line contains n uppercase English letters. Letter \"A\" stands for the first color, letter \"B\" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.", "output_spec": "Print a single integer — the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.", "sample_inputs": ["6 3\nABBACC", "3 2\nBBB"], "sample_outputs": ["2\nABCACA", "1\nBAB"], "notes": null}, "positive_code": [{"source_code": "n, m = gets.chomp.split(' ').map{|x| x.to_i}\nstr = gets.chomp\nif m == 2## time complexity = n\n a = \"AB\" * (n/2.to_i)\n b = \"BA\" * (n/2.to_i)\n if n%2 == 1\n a += 'A'\n b += 'B'\n end\n p1 = 0\n p2 = 0\n (0..str.length-1).each do |i|\n if a[i] != str[i]\n p1 += 1\n end\n if b[i] != str[i]\n p2 += 1\n end\n end\n if p1 <= p2\n puts p1, a\n else\n puts p2, b\n end\nelse #time complexity = 26*n\n pa = 0\n res = str.split('')\n\n for i in (1 .. res.length-1)\n if res[i] == res[i-1]\n s1 = res[i-1]\n s2 = res[i+1]\n\n if s1 != 'A' and s2 != 'A'\n res[i] = 'A'\n elsif s1 != 'B' and s2 != 'B'\n res[i] = 'B'\n else\n res[i] = 'C'\n end\n pa += 1\n end\n end\n puts pa, res.join('')\nend"}, {"source_code": "n, m = gets.chomp.split(' ').map{|x| x.to_i}\nstr = gets.chomp\nres = str.chars\nif m == 2## time complexity = n\n a = \"AB\" * (n/2.to_i)\n b = \"BA\" * (n/2.to_i)\n if n%2 == 1\n a += 'A'\n b += 'B'\n end\n p1 = 0\n p2 = 0\n (0..res.length-1).each do |i|\n if a[i] != res[i]\n p1 += 1\n end\n if b[i] != res[i]\n p2 += 1\n end\n end\n if p1 <= p2\n puts p1, a\n else\n puts p2, b\n end\nelse #time complexity = 26*n\n pa = 0\n\n for i in (1 .. res.length-1)\n if res[i] == res[i-1]\n s1 = res[i-1]\n s2 = res[i+1]\n\n if s1 != 'A' and s2 != 'A'\n res[i] = 'A'\n elsif s1 != 'B' and s2 != 'B'\n res[i] = 'B'\n else\n res[i] = 'C'\n end\n pa += 1\n end\n end\n puts pa, res.join('')\nend"}], "negative_code": [], "src_uid": "0ecf60ea733eba71ef1cc1e736296d96"} {"nl": {"description": "A group of n schoolboys decided to ride bikes. As nobody of them has a bike, the boys need to rent them.The renting site offered them m bikes. The renting price is different for different bikes, renting the j-th bike costs pj rubles.In total, the boys' shared budget is a rubles. Besides, each of them has his own personal money, the i-th boy has bi personal rubles. The shared budget can be spent on any schoolchildren arbitrarily, but each boy's personal money can be spent on renting only this boy's bike.Each boy can rent at most one bike, one cannot give his bike to somebody else.What maximum number of schoolboys will be able to ride bikes? What minimum sum of personal money will they have to spend in total to let as many schoolchildren ride bikes as possible?", "input_spec": "The first line of the input contains three integers n, m and a (1 ≤ n, m ≤ 105; 0 ≤ a ≤ 109). The second line contains the sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 104), where bi is the amount of the i-th boy's personal money. The third line contains the sequence of integers p1, p2, ..., pm (1 ≤ pj ≤ 109), where pj is the price for renting the j-th bike.", "output_spec": "Print two integers r and s, where r is the maximum number of schoolboys that can rent a bike and s is the minimum total personal money needed to rent r bikes. If the schoolchildren cannot rent any bikes, then r = s = 0.", "sample_inputs": ["2 2 10\n5 5\n7 6", "4 5 2\n8 1 1 2\n6 3 7 5 2"], "sample_outputs": ["2 3", "3 8"], "notes": "NoteIn the first sample both schoolchildren can rent a bike. For instance, they can split the shared budget in half (5 rubles each). In this case one of them will have to pay 1 ruble from the personal money and the other one will have to pay 2 rubles from the personal money. In total, they spend 3 rubles of their personal money. This way of distribution of money minimizes the amount of spent personal money."}, "positive_code": [{"source_code": "def can_buy(shared, bikes, boys)\n #puts \"Shared: #{shared} - #{bikes.to_s} - #{boys.to_s}\"\n\n s = 0\n\n richest_boy = boys.size - 1\n\n bikes.reverse_each do |price|\n can_pay = boys[richest_boy]\n richest_boy -= 1\n\n if can_pay >= price\n s += price\n else\n need = price - can_pay\n if shared >= need\n shared -= need\n s += can_pay\n else\n return -1\n end\n end\n end\n\n return [s - shared, 0].max\nend\n\ns = gets.split.map(&:to_i)\n\nnboy = s[0]\nnbike = s[1]\nshared_money = s[2]\n\nn = [nboy, nbike].min\nrich_boys = gets.split.map(&:to_i).sort { |x, y| y <=> x }.first(n).sort\ncheap_bikes = gets.split.map(&:to_i).sort.first(n)\n\n#puts can_buy(shared_money, cheap_bikes.first(3), rich_boys)\n\nlow = 1\nhigh = n\n\nr = 0\ns = 0\n\nwhile low <= high\n mid = (low + high) / 2\n bikes = cheap_bikes.first(mid)\n x = can_buy(shared_money, bikes, rich_boys)\n\n if x == -1\n high = mid - 1\n else\n r = mid\n s = x\n low = mid + 1\n end\nend\n\nputs \"#{r} #{s}\"\n"}], "negative_code": [], "src_uid": "cf8249244f67fb26bee3cdf0daedaca0"} {"nl": {"description": "Vasya lives in a strange world. The year has n months and the i-th month has ai days. Vasya got a New Year present — the clock that shows not only the time, but also the date.The clock's face can display any number from 1 to d. It is guaranteed that ai ≤ d for all i from 1 to n. The clock does not keep information about the current month, so when a new day comes, it simply increases the current day number by one. The clock cannot display number d + 1, so after day number d it shows day 1 (the current day counter resets). The mechanism of the clock allows you to increase the day number by one manually. When you execute this operation, day d is also followed by day 1.Vasya begins each day checking the day number on the clock. If the day number on the clock does not match the actual day number in the current month, then Vasya manually increases it by one. Vasya is persistent and repeats this operation until the day number on the clock matches the actual number of the current day in the current month.A year passed and Vasya wonders how many times he manually increased the day number by one, from the first day of the first month to the last day of the n-th month inclusive, considering that on the first day of the first month the clock display showed day 1.", "input_spec": "The first line contains the single number d — the maximum number of the day that Vasya's clock can show (1 ≤ d ≤ 106). The second line contains a single integer n — the number of months in the year (1 ≤ n ≤ 2000). The third line contains n space-separated integers: ai (1 ≤ ai ≤ d) — the number of days in each month in the order in which they follow, starting from the first one. ", "output_spec": "Print a single number — the number of times Vasya manually increased the day number by one throughout the last year.", "sample_inputs": ["4\n2\n2 2", "5\n3\n3 4 3", "31\n12\n31 28 31 30 31 30 31 31 30 31 30 31"], "sample_outputs": ["2", "3", "7"], "notes": "NoteIn the first sample the situation is like this: Day 1. Month 1. The clock shows 1. Vasya changes nothing. Day 2. Month 1. The clock shows 2. Vasya changes nothing. Day 1. Month 2. The clock shows 3. Vasya manually increases the day number by 1. After that the clock shows 4. Vasya increases the day number by 1 manually. After that the clock shows 1. Day 2. Month 2. The clock shows 2. Vasya changes nothing. In total, Vasya manually changed the day number by 1 exactly 2 times."}, "positive_code": [{"source_code": "n = gets.chomp.to_i\nm = gets.chomp.to_i\na = gets.chomp.split(/ /).map!{|x| x.to_i}\nret = 0\nfor i in 0..m-2\n ret += (n - a[i])\nend\nputs ret"}, {"source_code": "d = gets.to_i\ngets\np gets.split.map{|_| d - _.to_i}[0..-2].inject(&:+) || 0\n"}, {"source_code": "d = gets.chomp.to_i\nn = gets.chomp.to_i\ndays = gets.chomp.split(\" \").map{|e| e.to_i}\n\nans = 0\n\nfor i in (0..(n - 2)) do\n ans += d - days[i]\nend\n\nputs ans\n"}, {"source_code": "n=gets.to_i;r=gets.to_i;a=gets.chomp.split.map(&:to_i)\nputs (r*n)-a.inject(:+)-(n-a.last)"}, {"source_code": "d = gets.to_i\nn = gets.to_i\nputs gets.split.map(&:to_i)[0..-2].inject(0) {|acc, x| acc + d - x }"}, {"source_code": "d = gets.to_i\ngets\na = gets.split.map(&:to_i)\nc = 0\na.pop\n\na.each do |i|\n c += d - i\nend\np c\n"}, {"source_code": "d = gets.to_i\nn = gets.to_i\na = gets.split(' ').map(&:to_i)\nans = 0\n\n(n-1).times do |i|\n ans += d - a[i]\nend\nputs ans"}, {"source_code": "d = gets.to_i\nn = gets.to_i\na = gets.split.map(&:to_i)\n\ns = 0\n0.upto(a.size-2) do |i|\n s += (d-a[i])\nend\nputs s\n"}, {"source_code": "d = gets.to_i\nn = gets.to_i\narr = gets.split(\" \").map(&:to_i)\nans = arr[0..-2].map{|a| d - a}\nputs ans.inject(0,:+)\n# "}, {"source_code": "d = gets.to_i\nn = gets.to_i\narr = gets.split(\" \").map(&:to_i)\nans = arr[0..-2].map{|a| d - a}\nputs ans.inject(0,:+)"}, {"source_code": "def main\n d = STDIN.gets.to_i\n n = STDIN.gets.to_i\n a = STDIN.gets.split\n s = 0\n (n - 1).times do |i|\n s += d - a[i].to_i\n end\n puts s\nend\n\nmain\n"}, {"source_code": "# ProblemB\n\nd = gets.to_i\nn = gets.to_i\nary = gets.split().map {|s| s.to_i}\n\nret = 0\n\ni = 0\nwhile i < (ary.size - 1)\n ret += (d - ary[i])\n i += 1\nend\n\nputs ret\n \n"}, {"source_code": "n = gets.chomp.to_i\nmes = gets.chomp.to_i\ndni = gets.chomp.split(\" \")\n\n\nlength = dni.length\n\nfor i in(0..length-1)\n dni[i] = dni[i].to_i\nend\n\nk = 0\np = 0\n\nfor i in(0..length-2)\n\n k = n - dni[i]\n p+=k\nend\n\nputs p"}], "negative_code": [{"source_code": "d = gets.to_i\nn = gets.to_i\nputs gets.split.map(&:to_i)[1..-1].inject(0) {|acc, x| acc + d - x }"}, {"source_code": "d = gets.to_i\nn = gets.to_i\nputs gets.split.map(&:to_i)[0..-1].inject(0) {|acc, x| acc + d - x }"}], "src_uid": "dc5ddfc2d2e5589eb91aa33744c8fd14"} {"nl": {"description": "Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.", "input_spec": "The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000). The following line contains a binary string of length n representing Kevin's results on the USAICO. ", "output_spec": "Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.", "sample_inputs": ["8\n10000011", "2\n01"], "sample_outputs": ["5", "2"], "notes": "NoteIn the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.In the second sample, Kevin can flip the entire string and still have the same score."}, "positive_code": [{"source_code": "n = gets.chomp.to_i\nstr = gets.chomp\n\ncnt2 = 0\ncnt3 = 0\nls = [1]\nfor i in 1...n\n if str[i - 1] == str[i]\n ls[-1] += 1\n cnt2 += 1 if ls[-1] == 2\n cnt3 += 1 if ls[-1] == 3\n else\n ls << 1\n end\nend\n\nans = ls.size\nif 2 <= cnt2 || 1 <= cnt3\n ans += 2\nelsif 1 <= cnt2\n ans += 1\nend\n\nputs ans\n"}, {"source_code": "n = gets.chomp.to_i\ntab = gets.chomp\ns = 1\nt = tab[0]\ne = 0\nfor i in 1...tab.length\n\tif t != tab[i]\n\t\ts += 1\n\t\tt = tab[i]\n\tend\n\tif (tab[i] == tab[i-1] and tab[i]== '0') or (tab[i] == tab[i-1] and tab[i] == '1')\n\t\te += 1\n\t\te = [e, 2].min\n\tend\nend\nputs s+e\n"}, {"source_code": "#!/usr/bin/ruby \n\nlen = gets.to_i\nscore = gets.strip()\nchar = '-'\nalter = 0\nfor ch in 0..len - 1\n if char == '-' or char != score[ch]\n char = score[ch]\n alter += 1\n end\nend\nputs [alter + 2, len].min\n\n=begin\nmin(len, maxi + 2)\n=end\n"}, {"source_code": "len = gets.to_i\ns = gets\ncnt = 1\n1.upto(len - 1){|i|\n cnt += 1 if s[i] != s[i - 1]\n}\nputs [len.to_i, cnt + 2].min"}, {"source_code": "n = $stdin.readline.to_i\nans = $stdin.readline.strip\n\nsubseqs = []\nfrom = 0\nans.each_char.each_cons(2).with_index{|(fst,snd),ind|\n if fst == snd\n subseqs << [from, ind]\n from = ind + 1\n end\n}\nsubseqs << [from, ans.size - 1]\n\n# if subseqs.size >= 3\n# puts subseqs.each_cons(3).map{|a,b,c|\n# c.last - a.first + 1\n# }.max\n# else\n# puts ans.size\n# end\n\nputs subseqs.map{|subseq| subseq[1] - subseq[0] + 1 - 1 }.inject(&:+) + 1 + (subseqs.length > 2 ? 2 : (subseqs.length > 1 ? 1 : 0))\n"}, {"source_code": "gets\ns = gets.chomp\nn = s.chars.inject([0, nil]) do |(things, last), ch|\n if ch == last\n [things, ch]\n else\n [things + 1, ch]\n end\nend.first\n\nif s.scan(/(.)\\1/).size > 1 || /(.)\\1\\1/ =~ s\n puts n + 2\nelsif /(.)\\1/ =~ s\n puts n + 1\nelse\n puts n\nend\n"}, {"source_code": "n = gets.to_i\ns = gets\nc = 0\nfor i in 0...n-1 do\n c += 1 if s[i] == s[i+1] \nend\nc-=2\nans = c > 0 ? c : 0\nputs n-ans"}], "negative_code": [{"source_code": "#!/usr/bin/ruby \n\nlen = gets.to_i\nscore = gets.strip()\nnew = \"\"\nalter = \"\"\nfor ch in 0..len - 1\n if new == \"\" or score[ch] != new[-1] or (new.length >= 2 and new[-2] != new[-1])\n new += score[ch]\n end\n if alter == \"\" or alter[-1] != score[ch]\n alter += score[ch]\n end\nend\nputs [alter.length + 2, new.length].min\n\n=begin\nmin(len, maxi + 2)\n=end\n"}, {"source_code": "len = gets.to_i\ns = gets\ncnt = 0\n1.upto(len - 1){|i|\n cnt += 1 if s[i] != s[i - 1]\n}\nputs [len.to_i, cnt + 2].min"}, {"source_code": "gets\ns = gets.chomp\nn = s.chars.inject([0, nil]) do |(things, last), ch|\n if ch == last\n [things, ch]\n else\n [things + 1, ch]\n end\nend.first\n\nif s.scan(/(.)\\1/).size > 1\n puts n + 2\nelsif s[/^(.)\\1/] || s[/(.)\\1$/]\n puts n + 1\nelse\n puts n\nend\n"}, {"source_code": "gets\ns = gets.chomp\nn = s.chars.inject([0, nil]) do |(things, last), ch|\n if ch == last\n [things, ch]\n else\n [things + 1, ch]\n end\nend.first\n\nif s.scan(/(.)\\1/).size > 1\n puts n + 2\nelsif /(.)\\1/ =~ s\n puts n + 1\nelse\n puts n\nend\n"}, {"source_code": "gets\ns = gets.chomp\nn = s.chars.inject([0, nil]) do |(things, last), ch|\n if ch == last\n [things, ch]\n else\n [things + 1, ch]\n end\nend.first\n\nif s.scan(/(.)\\1/).size > 1\n puts n + 2\nelse\n puts n\nend\n"}, {"source_code": "n = gets.to_i\ns = gets\nc = 0\nfor i in 0...n-1 do\n c += 1 if s[i] == s[i+1] \nend\nc-=2\nans = c > 0 ? c : 0\nputs ans"}], "src_uid": "7b56edf7cc71a1b3e39b3057a4387cad"} {"nl": {"description": "Основой любой социальной сети является отношение дружбы между двумя пользователями в том или ином смысле. В одной известной социальной сети дружба симметрична, то есть если a является другом b, то b также является другом a. В этой же сети есть функция, которая демонстрирует множество людей, имеющих высокую вероятность быть знакомыми для пользователя. Эта функция работает следующим образом. Зафиксируем пользователя x. Пусть некоторый другой человек y, не являющийся другом x на текущий момент, является другом не менее, чем для k% друзей x. Тогда он является предполагаемым другом для x.У каждого человека в социальной сети есть свой уникальный идентификатор — это целое число от 1 до 109. Вам дан список пар пользователей, являющихся друзьями. Определите для каждого упомянутого пользователя множество его предполагаемых друзей.", "input_spec": "В первой строке следуют два целых числа m и k (1 ≤ m ≤ 100, 0 ≤ k ≤ 100) — количество пар друзей и необходимый процент общих друзей для того, чтобы считаться предполагаемым другом. В последующих m строках записано по два числа ai, bi (1 ≤ ai, bi ≤ 109, ai ≠ bi), обозначающих идентификаторы пользователей, являющихся друзьями. Гарантируется, что каждая пара людей фигурирует в списке не более одного раза.", "output_spec": "Для всех упомянутых людей в порядке возрастания id выведите информацию о предполагаемых друзьях. Информация должна иметь вид \"id:  k id1 id2 ... idk\", где id — это id самого человека, k — количество его предполагаемых друзей, а id1, id2, ..., idk — идентификаторы его предполагаемых друзей в возрастающем порядке. ", "sample_inputs": ["5 51\n10 23\n23 42\n39 42\n10 39\n39 58", "5 100\n1 2\n1 3\n1 4\n2 3\n2 4"], "sample_outputs": ["10: 1 42\n23: 1 39\n39: 1 23\n42: 1 10\n58: 2 10 42", "1: 0\n2: 0\n3: 1 4\n4: 1 3"], "notes": null}, "positive_code": [{"source_code": "pairs, percent = gets.strip.split(\" \").map(&:to_i)\npercent = percent.to_f / 100.0\n\nusers = Hash.new\n\npairs.times do |i|\n u1, u2 = gets.strip.split(\" \").map(&:to_i)\n users[u1] = Array.new unless users[u1].kind_of?(Array)\n users[u2] = Array.new unless users[u2].kind_of?(Array)\n\n users[u1] << u2\n users[u2] << u1\nend\n\nusers = Hash[users.sort]\n\nusers.map do |i, arr1|\n print \"#{i}: \"\n cmn_tr = []\n users.map do |j, arr2|\n next if i == j\n cmn = arr1 & arr2\n if cmn.length.to_f / users[i].length.to_f >= percent\n cmn_tr << j unless arr1.include? j\n end\n end\n print \"#{cmn_tr.length} \"\n cmn_tr.map{|k| print \"#{k} \"}\n print \"\\n\"\nend"}], "negative_code": [{"source_code": "pairs, percent = gets.strip.split(\" \").map(&:to_i)\npercent = percent.to_f / 100.0\n\nusers = Hash.new\n\npairs.times do |i|\n u1, u2 = gets.strip.split(\" \").map(&:to_i)\n users[u1] = Array.new unless users[u1].kind_of?(Array)\n users[u2] = Array.new unless users[u2].kind_of?(Array)\n\n users[u1] << u2\n users[u2] << u1\nend\n\nusers.each do |i, arr1|\n print \"#{i}: \"\n cmn_tr = []\n users.each do |j, arr2|\n next if i == j\n cmn = arr1 & arr2\n if cmn.length.to_f / users[i].length.to_f >= percent\n cmn_tr << j\n end\n end\n print \"#{cmn_tr.length} \"\n cmn_tr.map{|k| print \"#{k} \"}\n print \"\\n\"\nend"}, {"source_code": "pairs, percent = gets.strip.split(\" \").map(&:to_i)\npercent = percent.to_f / 100.0\n\nusers = Hash.new\n\npairs.times do |i|\n u1, u2 = gets.strip.split(\" \").map(&:to_i)\n users[u1] = Array.new unless users[u1].kind_of?(Array)\n users[u2] = Array.new unless users[u2].kind_of?(Array)\n\n users[u1] << u2\n users[u2] << u1\nend\n\nusers = Hash[users.sort]\n\nusers.map do |i, arr1|\n print \"#{i}: \"\n cmn_tr = []\n users.map do |j, arr2|\n next if i == j\n cmn = arr1 & arr2\n if cmn.length.to_f / users[i].length.to_f >= percent\n cmn_tr << j\n end\n end\n print \"#{cmn_tr.length} \"\n cmn_tr.map{|k| print \"#{k} \"}\n print \"\\n\"\nend"}], "src_uid": "19079c10a1bdfa8ae9b7b6e0378d3aad"} {"nl": {"description": "Little Petya very much likes gifts. Recently he has received a new laptop as a New Year gift from his mother. He immediately decided to give it to somebody else as what can be more pleasant than giving somebody gifts. And on this occasion he organized a New Year party at his place and invited n his friends there.If there's one thing Petya likes more that receiving gifts, that's watching others giving gifts to somebody else. Thus, he safely hid the laptop until the next New Year and made up his mind to watch his friends exchanging gifts while he does not participate in the process. He numbered all his friends with integers from 1 to n. Petya remembered that a friend number i gave a gift to a friend number pi. He also remembered that each of his friends received exactly one gift.Now Petya wants to know for each friend i the number of a friend who has given him a gift.", "input_spec": "The first line contains one integer n (1 ≤ n ≤ 100) — the quantity of friends Petya invited to the party. The second line contains n space-separated integers: the i-th number is pi — the number of a friend who gave a gift to friend number i. It is guaranteed that each friend received exactly one gift. It is possible that some friends do not share Petya's ideas of giving gifts to somebody else. Those friends gave the gifts to themselves.", "output_spec": "Print n space-separated integers: the i-th number should equal the number of the friend who gave a gift to friend number i.", "sample_inputs": ["4\n2 3 4 1", "3\n1 3 2", "2\n1 2"], "sample_outputs": ["4 1 2 3", "1 3 2", "1 2"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\na = gets.split(' ').map(&:to_i)\nres = Array.new\nn.times do |i|\n res[a[i]-1] = i+1\nend\nres.each { |v| print \"#{v} \" }\n"}, {"source_code": "n = gets.to_i\na = gets.split(' ').map(&:to_i)\nres = Array.new\nn.times do |i|\n res[a[i]-1] = i+1\nend\nputs res.to_s.gsub(/\\[/, '').gsub(/\\]/, '').gsub(/,/, '')\n"}, {"source_code": "n = gets.to_i\nar = gets.chomp.split(' ',n).map{|_|_.to_i}\nputs ar.map.with_index(1).sort_by(&:first).map(&:last).map(&:to_s).join(' ')"}, {"source_code": "class Main\n #def get_problem_link\n # problem_link = \"https://codeforces.com/contest/136/problem/A\"\n # return problem_link\n #end\n\n #def get_submission_link\n # submission_link = ''\n # return submisson_link\n #end\n\n #begin\n #of codeforces interface\n def run_interface\n n = gets.to_i\n k = gets.split(' ').map(&:to_i)\n puts solution(n, k)\n end\n #end\n #of codeforces interface\n\n def solution(n, presents_array)\n res_array = []\n for i in 1..n do\n res_array.push(presents_array.index(i) + 1)\n end\n return res_array.join(' ')\n end\n\nend\n\n#then upload to codeforces uncomment this line:\nMain.new.run_interface\n"}, {"source_code": "n = STDIN.gets.chomp.to_i\np = STDIN.gets.chomp.split(' ').map{|i| i.to_i}\n\nres = p.inject(Hash.new(-1)) {|h, i| h[i - 1] = p.index(i); h}\nn.times do |i|\n if res[i] == -1\n print i + 1, \" \"\n next\n end\n print res[i] + 1, \" \"\nend"}, {"source_code": "n = gets.to_i\n\nh = {}\nst = gets.split.map{|i| i.to_i}\n\nn.times do |i|\n h[i+1] = st[i] \nend\n\nh = h.invert\n\nst = \"\"\ni = 1\nwhile i <= n do \n st += h[i].to_s + \" \"\n i += 1\nend\n\nputs st"}, {"source_code": "gets;a=gets.split.map &:to_i\nputs (1..a.size).map{|i|a.index(i)+1}*\" \""}, {"source_code": "n=gets.to_i\na=Array.new(n,0)\ni=gets.split.map(&:to_i)\nn.times{|j|\n a[i[j]-1]=j\n}\na.each{|j|\n print \"#{j+1} \"\n}\nputs\n"}, {"source_code": "n = gets.to_i\np = gets.split.map(&:to_i)\na = Array(p.size)\np.each_index {|i| a[p[i] - 1] = i + 1 }\nputs a"}, {"source_code": "gets;a=gets.split.map &:to_i;a.size.times{|i|p a.index(i+1)+1}"}, {"source_code": "n = gets.to_i\na = []\na = gets.split.map &:to_i\nb = Array.new(n)\n\n(0...n).each do |i|\n b[a[i] - 1] = i + 1;\nend\n \n\nprint b.join(' ')\n"}, {"source_code": "l = STDIN.read.split(\"\\n\")\na = []\nl[1].split(' ').each_with_index{|x,i| a << [x.to_i, i+1] }\nb = a.sort_by{|x| x[0] }.map{|x| x[1] }\nputs b.join(' ')\n\n\n"}, {"source_code": "def r\n myArray = gets.chomp.split().map { |e| e.to_i }\nend\nn = r\narray = r\narray_2 = Array.new(n)\narray.each_with_index{|val, index|\n array_2[val-1] = index + 1\n}\narray_2.each {|e| print \"#{e} \" }\n"}, {"source_code": "gets;a=gets.split.map &:to_i;a.size.times{|i|p a.index(i+1)+1}"}, {"source_code": "n = gets.chomp;\nn = n.to_i;\n\np = gets.chomp;\np = p.split(' ');\n\nfor i in 0..p.length-1\n\tp[i] = p[i].to_i;\n#puts p[i];\nend\n\n#puts(\"done\");\n\nfrom = Array.new(n + 1);\n\nfor i in 0..p.length-1\n\tfrom[p[i]] = i + 1\nend\n\nfor i in 1..from.length-1\n\tif i > 1\n\t\tprintf(\" \");\n\tend\n\tprintf(\"%d\", from[i]);\nend\nprintf(\"\\n\");\n"}, {"source_code": "n=gets.chomp.strip.to_i\na=gets.chomp.split(' ').map(&:to_i)\np=[]\na.each_index do |i|\n\tp[a[i]-1]=i\nend\nfor x in p do\n\tprint (x+1).to_s+' '\nend\nputs"}, {"source_code": "#!/usr/bin/ruby\n\nfriends = gets.to_i\ngifts = gets.chomp.split(\" \").map(&:to_i)\n(1..friends+1).each do |num|\n (0..friends).each do |i|\n if(gifts[i] == num)\n puts i + 1\n end\n end\nend\n\n"}, {"source_code": "a = Array.new(gets.to_i)\ngets.strip.split.map(&:to_i).to_a.each_with_index { |e, i| a[e-1] = i+1 }\nputs a*' '"}, {"source_code": "n = gets.to_i\na = gets.chomp.split(' ').map(&:to_i)\n\nb = []\n\na.each_with_index do |t,i|\n b[t-1] = i+1\nend\n\nputs b.join(' ')"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nc = Array.new(n)\n\nfor i in 0..n-1\n c[a[i]] = i+1\nend\nputs c.compact"}, {"source_code": "n = $stdin.readline.to_i\nf = $stdin.readline.split.map(&:to_i)\n\nf_inv = Array.new(n)\nf.each_with_index {|e,i| f_inv[e-1]=i+1 }\n\nputs f_inv.join(\" \")"}, {"source_code": "#!/usr/bin/ruby\ndef readNextValues\n return gets.chomp.split.collect {|x| x.to_i }\nend\n\nn = readNextValues\narr = readNextValues\nresult = []\nfor i in 1..arr.size\n result << arr.find_index(i) + 1\nend\nputs result.join(' ')\n"}, {"source_code": "gets\narr = gets.split.map &:to_i\nsol = Array.new\narr.each_with_index { |x, index| sol[arr[index]] = index + 1 }\nsol.shift\nputs sol\n"}, {"source_code": "n = gets.to_i\nps = gets.split.map(&:to_i)\nans = Array.new(n)\nps.each_with_index do |p, i|\n ans[p-1] = i + 1\nend\nputs ans.join(\" \")"}, {"source_code": "friends = gets.chomp.to_i\ngifts = gets.chomp.split(/ /).map(&:to_i)\nreceived_gifts = {}\n(1..friends).each do |friend|\n received_gifts.store gifts[friend - 1], friend\nend\nans = []\n(1..friends).each do |friend|\n ans << received_gifts[friend]\nend\nputs ans.join(\" \")\n"}, {"source_code": "a=gets.chomp.to_i\nb=gets.chomp.split(\" \")\nans=\"\"\n1.upto(a) do |i|\n c=b.index(\"#{i}\")+1\n ans=ans+\"#{c} \"\nend\nputs \"#{ans}\""}, {"source_code": "gets;a=gets.split.map &:to_i;a.size.times{|i|p a.index(i+1)+1}\n"}, {"source_code": "n=gets.to_i\nb=(1..n).map{0}\nx=gets.split.map{|e| e.to_i}\nn.times{|i|\n\tj=i+1\n\tb[x[i]-1]=j\n}\nputs b*\" \""}, {"source_code": "n = gets.to_i\narr_n = gets.split.map(&:to_i)\nres = []\narr_n.each.with_index(1) { |e,i| res[e]=i }\nputs res[1..-1].join(' ')"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nb = []\nn.times do |i|\n b << a.index(i+1)+1\nend\nputs b * \" \"\n"}, {"source_code": "n = gets.chomp.to_i\narr = gets.chomp.split.map(&:to_i)\n\nc = {}\narr.each_with_index do |a,i|\n c[i+1] = a\nend\n\n\ncc = {}\nc.each do |k,v|\n cc[v] = k\nend\n\naa = []\nn.times do |i|\n aa << cc[i+1]\nend\n\nputs aa.join(' ')"}, {"source_code": "gets;a=gets.split.map &:to_i;a.size.times{|i|p a.index(i+1)+1}"}, {"source_code": "#=================================\nrequire 'prime'\nrequire 'set'\nrequire 'matrix'\nrequire 'tsort'\ninclude Math\n\nif ARGV.include?(\"ONLINE_JUDGE\")\n\t$stdin = File.open(\"./input.txt\", \"r\")\n\tARGV.delete(\"ONLINE_JUDGE\")\nend\n#=================================\n\nn=gets.to_i\ndata=gets.split.map(&:to_i)\nans=Array.new(n, 0)\n\nn.times{|i|\n\tans[data[i]-1]=i+1\n}\n\nans.each{|v| print \"#{v} \"}\n"}, {"source_code": "number = gets.to_i\nanswer = Array.new(number + 1)\n\narr = gets.chomp.split(' ').map(&:to_i)\ni = 1\narr.each do |x|\n answer[x] = i\n i += 1\nend\n\nanswer.each { |x| print \"#{x} \" }"}, {"source_code": "len=gets.to_i\nb=[]\na=gets.split.map{|i|i.to_i}\nlen.times{|i| b[a[i]]=i+1}\nb.shift\nprint b[0]\n1.upto(len) {|i| print ' ',b[i]}\nputs\n"}, {"source_code": "len=gets.to_i\nb=[]\na=gets.split.map{|i|i.to_i}\nlen.times{|i| b[a[i]]=i+1}\nb.shift\nprint b[0]\n1.upto(len) {|i| print ' ',b[i]}\n"}, {"source_code": "len=gets.to_i\nb=[]\na=gets.split.map{|i|i.to_i}\nlen.times{|i| b[a[i]]=i+1}\nb.shift\nlen.times {|i| print b[i],' '}\n"}, {"source_code": "n = gets.to_i\nq = gets.chomp.split.map { |x| x.to_i }\nh = Hash.new\n\nfor i in 0..n-1\n h[i] = q[i]\nend\n\nh = h.invert\n\nfor i in 1..n\n print h[i]+1, ' '\nend"}, {"source_code": "gets;a=gets.split.map &:to_i;a.size.times{|i|p a.index(i+1)+1}"}, {"source_code": "i = gets.to_i\narray = gets.chomp.split.map(&:to_i)\na = Array.new\n\narray.map.with_index { |x, i| a[x-1] = i+1 }\nputs a.join(\" \")\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\n\nb = Array.new(a.size)\n\na.each_index { |i| b[a[i]] = i + 1}\n\nputs b"}, {"source_code": "t=[];$<.take(2)[1].split.each_with_index{|k,i| t[k.to_i]=i+1};puts t[1..-1].join \" \""}, {"source_code": "number_of_friends = gets.strip.to_i\nfriends = gets.strip.split(' ')\n\nresults = []\n1.upto(number_of_friends) do |current_friend|\n results << friends.index { |given_friend| current_friend.to_s == given_friend } + 1\nend\n\nputs results.join(' ')\n"}, {"source_code": "gets;a=gets.split.map &:to_i;a.size.times{|i|p a.index(i+1)+1}"}, {"source_code": "n = $stdin.readline.to_i\na = $stdin.readline.split(' ').map { |x| x.to_i }\n\nb = Array.new(n)\nfor i in 1..n\n b[i-1] = i\nend\n\nfor i in 1..n\n b[a[i-1]-1] = i unless a[i-1].nil?\nend\n\nputs b.join(' ')\n"}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\n(1..n).each{|i|puts a.index(i)+1}\n"}, {"source_code": "friends = gets.chomp.to_i\ngifts = gets.chomp\ngifts = gifts.split\n\narr_gifts = []\nfor i in (0..gifts.size-1)\n\tarr_gifts[gifts[i].to_i-1] = i+1\nend\n\nfor i in (0..gifts.size-1)\n\tprint arr_gifts[i].to_i\n\tprint \" \"\nend"}, {"source_code": "n=gets.to_i\ns=gets.split(' ').map{|x|x.to_i-1}\nn.times{|i|print s.index(i)+1,' '}\n"}, {"source_code": "r={}\ni=1\nn=gets.to_i\ngets.split(' ').each{|x|r[x.to_i]=i.to_s;i=i+1}\nn.times{|i|print r[i+1]+' '}\n"}, {"source_code": "n=gets.to_i\nr=[0]*n\ni=1\ngets.split(' ').each{|x|r[x.to_i-1]=i;i=i+1}\nprint r.join(' ')\n"}, {"source_code": "n=gets.to_i\ns=gets\nn.times{|i|p s.split(' ').index((i+1).to_s)+1}\n"}, {"source_code": "n = gets\np = gets\n=begin\narr1 = p.chomp.split(' ')\narr1 = arr1.sort\nmax = arr1[-2]\nif max == 1\n puts arr1.sort[0..-2].join(' ')+\" 2\"\nelse\n puts \"1 \" + arr1.sort[0..-2].join(' ')\nend\n=end\n\nfrom = Array.new(n.to_i)\nto = p.split(' ')\ni = 1\nfor el in to\n from[el.to_i-1] = i\n i+=1\nend\nputs from.join(' ').chomp\n"}, {"source_code": "n = gets.to_i\narr = gets.split(\" \").map(&:to_i)\nh = {}\narr.each_with_index { |a,i|\n h[a] = i + 1\n}\nfor i in (1..n)\n puts h[i]\nend"}, {"source_code": "n = gets.to_i\na = gets.split.map &:to_i\nc = {}\nfor i in (1..n)\n c[i] = a[i-1]\nend\nputs c.sort_by {|k, v| v}.map{|x| x[0]}.join(\" \")\n"}, {"source_code": "n = gets.to_i\nar = Array.new\nar = gets.split.map &:to_i\na = ar.size\na.times{\n |i| puts ar.index(i+1)+1 # array.index(value) returns the index of value in the array\n } \n"}, {"source_code": "gets;a=gets.split.map &:to_i;a.size.times{|i|p a.index(i+1)+1}"}, {"source_code": "l = []\ngets\ngets.split.map(&:to_i).each_with_index { |x, i| l[x - 1] = i + 1 }\nputs l.join(\" \")"}, {"source_code": "member_num = gets.chomp.to_i\npresent_arr = gets.chomp.split\npresent_arr.map!{|i| i.to_i}\noutput_arr = Array.new(member_num)\ni = 0\nwhile i < output_arr.length\n output_arr[present_arr[i]-1] = i+1\n i = i+1\nend\nprint output_arr.join(\" \")\n"}, {"source_code": "a=gets.chomp.to_i\nb=gets.chomp.split(\" \")\nans=\"\"\n1.upto(a) do |i|\n c=b.index(\"#{i}\")+1\n ans=ans+\"#{c} \"\nend\nputs \"#{ans}\"\n "}, {"source_code": "t=[];$<.take(2)[1].split.each_with_index{|k,i| t[k.to_i]=i+1};puts t[1..-1].join \" \""}, {"source_code": "n=gets.to_i\na=[]\n(n-1).times{\na.push gets(' ').to_i\n}\na.push gets.to_i\n\nb=[]\n\nfor i in 0..a.size-1\nb[a[i]-1]=i+1\nend\n\nfor i in b\nprint i\nprint ' '\nend\nprint \"\\n\""}, {"source_code": "n = gets.to_i\ninput = gets.split(' ').map(&:to_i).unshift(0)\nresult = Array.new(n+1, 0)\n\n0.upto(n) do |i|\n result[input[i]] = i\nend\n\nresult.shift\n\nputs result.join(' ')\n"}, {"source_code": "n = gets.to_i\nm = gets.chomp.split(\" \")\n\n\np = Array.new(n)\n\nfor i in(0..n-1)\n p[m[i].to_i - 1] = i + 1\nend\n\np.each{ |x|\n print(x, \" \")\n}"}, {"source_code": "gets;a=gets.split.map &:to_i;a.size.times{|i|p a.index(i+1)+1}"}, {"source_code": "n = gets.to_i\nb = gets.split(' ').map{|s| s.to_i}\na = Array.new(n)\ni = 0\nwhile (i < n)\n a[b[i]-1] = i \n i+=1\nend \ns = ''\na.each{|q| s += (q+1).to_s + ' '}\nputs s"}, {"source_code": "n = gets.to_i\na = []\ngets.split.each_with_index{ |x,i|\n a[x.to_i] = i+1\n}\nputs a.join(\" \")"}, {"source_code": "gets\nt = 1\nrec = Array.new(t)\ngets.scan(/\\d+/).each do |i|\n\trec[i.to_i] = t\n\tt = t+1\nend\nrec.each do |out|\n\tprintf(out.to_s + \" \")\nend\n\n"}, {"source_code": "#!/usr/bin/env ruby\n\ngets\nm = {}\ngets.split.map(&:to_i).each_with_index { |p,i|\n m[p] = i\n}\nm.map.sort_by{ |i,p|i }.map { |i,p|\n print \"#{p+1} \"\n}\nputs"}, {"source_code": "n = gets.to_i\nres = Array.new(n, 0)\nn = 1\narr = gets.split.map(&:to_i).each do |item|\n res[item - 1] = n\n n += 1\nend\nputs res.join(\" \")\n"}, {"source_code": "gets\ngets.split(\" \").each_with_index.map { |s, i| [s.to_i, i] }.sort_by! {|s, i| s}.each { |s, i| print \"#{i+1} \"}"}, {"source_code": "n = gets.to_i\nh = {}\nreadline.split(\" \").each_with_index.map { |item, pos| h[item.to_i] = pos+1 }\n(1..n).each { |i| print \"#{h[i]} \" }"}, {"source_code": "gets\nputs gets.split.map(&:to_i).each_with_index.map.sort_by{|p,i|p}.map{|p,i|i+1}*\" \""}, {"source_code": "gets;\narr = gets.split.map(&:to_i)\narr2 = Array(arr.size + 1)\nfor i in 1..arr.size do\n arr2[ arr[i-1] ] = i;\nend\nfor i in 1...arr2.size do\n puts arr2[i]\nend\n"}, {"source_code": "friendNum = gets.chomp.to_i\ninput = gets.chomp.split(' ').map {|item| item.to_i}\noutput = []\n(0...friendNum).each do |loops|\n\toutput[(input[loops] - 1) % friendNum] = loops + 1\nend\noutput.each do |ans|\n\tprint \"#{ans} \"\nend"}, {"source_code": "n = gets.chomp.to_i\ngets.chomp.split(' ').map(&:to_i).zip((1..n).to_a).sort { |x, y| x[0] <=> y[0]}.each {|x| puts x[1] }\n"}], "negative_code": [{"source_code": "n = gets.to_i\nar = gets.chomp.split(' ',n)\nputs ar.map.with_index(1).sort_by(&:first).map(&:last).map(&:to_s).join(' ')"}, {"source_code": "def r\n myArray = gets.chomp.split().map { |e| e.to_i }\nend\nn = r\narray = r\narray_2 = Array.new(n)\narray.each_with_index{|val, index|\n array_2[val-1] = index + 1\n}\narray_2\n"}, {"source_code": "a = Array.new(gets.to_i)\nputs gets.strip.split.map(&:to_i).to_a.each_with_index { |e, i| a[e-1] = i+1 } * ' '"}, {"source_code": "gets\narr = gets.split.map &:to_i\nsol = Array.new\narr.each_with_index { |x, index| sol[arr[index]] = index + 1 }\nsol.shift\np sol\n"}, {"source_code": "number = gets.to_i\nanswer = Array.new(number + 1)\ni = 1\n\nnumber.times {\n n = gets.to_i\n answer[n] = i\n i += 1\n}\n\nanswer.each { |x| print \"#{x} \" }"}, {"source_code": "n = gets.to_i\nq = gets.chomp.split.map { |x| x.to_i }\nh = Hash.new\n\nfor i in 0..n-1\n h[i] = q[i]\nend\n\nh = h.invert\n\np h\n\nfor i in 1..n\n print h[i]+1, ' '\nend"}, {"source_code": "i = gets.to_i\narray = gets.chomp.split.map(&:to_i)\na = Array.new\n\nputs array.map.with_index { |x, i| a[x-1] = i+1 }.join(\" \")"}, {"source_code": "t=[];gets.split.each_with_index{|k,i| t[k.to_i]=i+1};puts t[1..-1].join \" \""}, {"source_code": "r={}\ni=1\ngets\ngets.split(' ').each{|x|r[x.to_i]=i;i=i+1}\nprint r.values.join(' ')\n"}, {"source_code": "#!/usr/bin/env ruby\n\ngets\nm = {}\ngets.split.map(&:to_i).each_with_index { |p,i|\n m[p] = i\n}\nm.map { |i,p|\n print \"#{p+1} \"\n}\nputs"}], "src_uid": "48bb148e2c4d003cad9d57e7b1ab78fb"} {"nl": {"description": "Miyako came to the flea kingdom with a ukulele. She became good friends with local flea residents and played beautiful music for them every day.In return, the fleas made a bigger ukulele for her: it has $$$n$$$ strings, and each string has $$$(10^{18} + 1)$$$ frets numerated from $$$0$$$ to $$$10^{18}$$$. The fleas use the array $$$s_1, s_2, \\ldots, s_n$$$ to describe the ukulele's tuning, that is, the pitch of the $$$j$$$-th fret on the $$$i$$$-th string is the integer $$$s_i + j$$$.Miyako is about to leave the kingdom, but the fleas hope that Miyako will answer some last questions for them.Each question is in the form of: \"How many different pitches are there, if we consider frets between $$$l$$$ and $$$r$$$ (inclusive) on all strings?\"Miyako is about to visit the cricket kingdom and has no time to answer all the questions. Please help her with this task!Formally, you are given a matrix with $$$n$$$ rows and $$$(10^{18}+1)$$$ columns, where the cell in the $$$i$$$-th row and $$$j$$$-th column ($$$0 \\le j \\le 10^{18}$$$) contains the integer $$$s_i + j$$$. You are to answer $$$q$$$ queries, in the $$$k$$$-th query you have to answer the number of distinct integers in the matrix from the $$$l_k$$$-th to the $$$r_k$$$-th columns, inclusive.", "input_spec": "The first line contains an integer $$$n$$$ ($$$1 \\leq n \\leq 100\\,000$$$) — the number of strings. The second line contains $$$n$$$ integers $$$s_1, s_2, \\ldots, s_n$$$ ($$$0 \\leq s_i \\leq 10^{18}$$$) — the tuning of the ukulele. The third line contains an integer $$$q$$$ ($$$1 \\leq q \\leq 100\\,000$$$) — the number of questions. The $$$k$$$-th among the following $$$q$$$ lines contains two integers $$$l_k$$$,$$$r_k$$$ ($$$0 \\leq l_k \\leq r_k \\leq 10^{18}$$$) — a question from the fleas.", "output_spec": "Output one number for each question, separated by spaces — the number of different pitches.", "sample_inputs": ["6\n3 1 4 1 5 9\n3\n7 7\n0 2\n8 17", "2\n1 500000000000000000\n2\n1000000000000000000 1000000000000000000\n0 1000000000000000000"], "sample_outputs": ["5 10 18", "2 1500000000000000000"], "notes": "NoteFor the first example, the pitches on the $$$6$$$ strings are as follows.$$$$$$ \\begin{matrix} \\textbf{Fret} & \\textbf{0} & \\textbf{1} & \\textbf{2} & \\textbf{3} & \\textbf{4} & \\textbf{5} & \\textbf{6} & \\textbf{7} & \\ldots \\\\ s_1: & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & \\dots \\\\ s_2: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & \\dots \\\\ s_3: & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & \\dots \\\\ s_4: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & \\dots \\\\ s_5: & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & \\dots \\\\ s_6: & 9 & 10 & 11 & 12 & 13 & 14 & 15 & 16 & \\dots \\end{matrix} $$$$$$There are $$$5$$$ different pitches on fret $$$7$$$ — $$$8, 10, 11, 12, 16$$$.There are $$$10$$$ different pitches on frets $$$0, 1, 2$$$ — $$$1, 2, 3, 4, 5, 6, 7, 9, 10, 11$$$."}, "positive_code": [{"source_code": "def bsearch(arr,t)\n ok,ng = 0,arr.length\n while (ng - ok).abs > 1\n mid = (ok+ng)/2\n if arr[mid] <= t\n ok = mid\n else\n ng = mid\n end\n end\n return ok\nend\n\nn = gets.to_i\ns = gets.split.map(&:to_i)\nq = gets.to_i\nqs = []\nq.times do\n l,r = gets.split.map(&:to_i)\n qs << r-l+1\nend\n\ns.sort!\ndiff = [0]\n(n-1).times do |i|\n diff << s[i+1] - s[i]\nend\ndiff << 10**18+1\ndiff.sort!\ncumsum = [0]\nn.times do |i|\n cumsum << cumsum[-1] + diff[i+1]\nend\n\nqs.each do |t| \n fill = bsearch(diff,t)\n ans = cumsum[fill] + t*(n-fill)\n# p [t,fill,cumsum[fill]]\n print \"#{ans} \"\nend\n"}, {"source_code": "def binary_search(ok, ng)\n while (ok - ng).abs > 1\n mid = (ok + ng) / 2\n\n if yield(mid)\n ok = mid\n else\n ng = mid\n end\n end\n\n ok\nend\n\nN = gets.to_i\nS = gets.split.map(&:to_i)\nSU = S.sort.uniq\nQ = gets.to_i\nquery = Q.times.map { gets.split.map(&:to_i) }\n\nDL = SU.each_cons(2).map { |a, b| b - a - 1 }.sort\nDL.unshift(-Float::INFINITY)\nDL.push(Float::INFINITY)\nDL.reverse!\n\nRUI = []\n\nsum = 0\n\nDL.reverse[1..-2].each do |v|\n sum += v\n RUI << sum\nend\n\nRUI.unshift(-Float::INFINITY)\nRUI << Float::INFINITY\nRUI.reverse!\n\nans = []\nquery.each do |l, r|\n a = SU.size\n d = r - l\n ei = binary_search(0, DL.size - 1) { |x| DL[x] >= d }\n b = d * ei\n c = d\n d = [RUI[ei + 1], 0].max\n\n ans << a + b + c + d\nend\n\nputs ans.join(' ')\n"}, {"source_code": "n = gets.to_i\ns = gets.split.map(&:to_i).uniq.sort\nn = s.size\nt = Array.new(n-1)\nfor i in 0..n-2\n t[i] = s[i+1]-s[i]\nend\nt.sort!\nq = gets.to_i\nlr = Array.new(q)\nq.times do |i|\n lr[i] = gets.split.map(&:to_i)\nend\na = Array.new(q){|i|i}.sort_by{|i|lr[i][1]-lr[i][0]}\nans = Array.new(q,0)\nlr.sort_by!{|i,j|j-i}\nc = 0\nsum = 0\nq.times do |i|\n d = lr[i][1]-lr[i][0]+1\n while c < n-1 && t[c] < d\n sum += t[c]\n c += 1\n end\n ans[a[i]] = sum+d+(n-c-1)*d\nend\nputs ans.join(\" \")"}], "negative_code": [{"source_code": "def binary_search(ok, ng)\n while (ok - ng).abs > 1\n mid = (ok + ng) / 2\n\n if yield(mid)\n ok = mid\n else\n ng = mid\n end\n end\n\n ok\nend\n\nN = gets.to_i\nS = gets.split.map(&:to_i)\nSU = S.sort.uniq\nQ = gets.to_i\nquery = Q.times.map { gets.split.map(&:to_i) }\n\nDL = SU.each_cons(2).map { |a, b| b - a - 1 }.sort\nRUI = []\n\nsum = 0\n\nDL.each do |v|\n sum += v\n RUI << sum\nend\n\nRUI.unshift(-Float::INFINITY)\nRUI << Float::INFINITY\nRUI.reverse!\n\nans = []\nquery.each do |l, r|\n a = SU.size\n d = r - l\n ei = binary_search(0, RUI.size - 1) { |x| RUI[x] >= d }\n b = d * ei\n c = d\n d = [RUI[ei + 1], 0].max\n\n ans << a + b + c + d\nend\n\nputs ans.join(' ')\n"}], "src_uid": "536a582f3620a733d09cf80662488590"} {"nl": {"description": "You are given an array $$$a$$$ consisting of $$$n$$$ integers. You can perform the following operations with it: Choose some positions $$$i$$$ and $$$j$$$ ($$$1 \\le i, j \\le n, i \\ne j$$$), write the value of $$$a_i \\cdot a_j$$$ into the $$$j$$$-th cell and remove the number from the $$$i$$$-th cell; Choose some position $$$i$$$ and remove the number from the $$$i$$$-th cell (this operation can be performed no more than once and at any point of time, not necessarily in the beginning). The number of elements decreases by one after each operation. However, the indexing of positions stays the same. Deleted numbers can't be used in the later operations.Your task is to perform exactly $$$n - 1$$$ operations with the array in such a way that the only number that remains in the array is maximum possible. This number can be rather large, so instead of printing it you need to print any sequence of operations which leads to this maximum number. Read the output format to understand what exactly you need to print.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$2 \\le n \\le 2 \\cdot 10^5$$$) — the number of elements in the array. The second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$-10^9 \\le a_i \\le 10^9$$$) — the elements of the array.", "output_spec": "Print $$$n - 1$$$ lines. The $$$k$$$-th line should contain one of the two possible operations. The operation of the first type should look like this: $$$1~ i_k~ j_k$$$, where $$$1$$$ is the type of operation, $$$i_k$$$ and $$$j_k$$$ are the positions of the chosen elements. The operation of the second type should look like this: $$$2~ i_k$$$, where $$$2$$$ is the type of operation, $$$i_k$$$ is the position of the chosen element. Note that there should be no more than one such operation. If there are multiple possible sequences of operations leading to the maximum number — print any of them.", "sample_inputs": ["5\n5 -2 0 1 -3", "5\n5 2 0 4 0", "2\n2 -1", "4\n0 -10 0 0", "4\n0 0 0 0"], "sample_outputs": ["2 3\n1 1 2\n1 2 4\n1 4 5", "1 3 5\n2 5\n1 1 2\n1 2 4", "2 2", "1 1 2\n1 2 3\n1 3 4", "1 1 2\n1 2 3\n1 3 4"], "notes": "NoteLet X be the removed number in the array. Let's take a look at all the examples:The first example has, for example, the following sequence of transformations of the array: $$$[5, -2, 0, 1, -3] \\to [5, -2, X, 1, -3] \\to [X, -10, X, 1, -3] \\to$$$ $$$[X, X, X, -10, -3] \\to [X, X, X, X, 30]$$$. Thus, the maximum answer is $$$30$$$. Note, that other sequences that lead to the answer $$$30$$$ are also correct.The second example has, for example, the following sequence of transformations of the array: $$$[5, 2, 0, 4, 0] \\to [5, 2, X, 4, 0] \\to [5, 2, X, 4, X] \\to [X, 10, X, 4, X] \\to$$$ $$$[X, X, X, 40, X]$$$. The following answer is also allowed: 1 5 31 4 21 2 12 3Then the sequence of transformations of the array will look like this: $$$[5, 2, 0, 4, 0] \\to [5, 2, 0, 4, X] \\to [5, 8, 0, X, X] \\to [40, X, 0, X, X] \\to$$$ $$$[40, X, X, X, X]$$$.The third example can have the following sequence of transformations of the array: $$$[2, -1] \\to [2, X]$$$.The fourth example can have the following sequence of transformations of the array: $$$[0, -10, 0, 0] \\to [X, 0, 0, 0] \\to [X, X, 0, 0] \\to [X, X, X, 0]$$$.The fifth example can have the following sequence of transformations of the array: $$$[0, 0, 0, 0] \\to [X, 0, 0, 0] \\to [X, X, 0, 0] \\to [X, X, X, 0]$$$."}, "positive_code": [{"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nused = Array.new(n+1,false)\nz = []\np = 0\npre = 0\nmx = [-1000000010,0]\nfor i in 0..n-1\n if a[i] == 0\n z.push(i+1)\n next\n end\n if a[i] < 0\n p = 1-p\n if mx[0] < a[i]\n mx = [a[i],i+1]\n end\n end\nend\nz.push(mx[1]) if p == 1\nfor i in 0..z.size-2\n puts \"1 #{z[i]} #{z[i+1]}\"\n used[z[i]-1] = true\nend\nif (z.size != 0 && z.size != n)\n puts \"2 #{z[-1]}\"\n used[z[-1]-1] = true\nend\nfor i in 0..n-1\n next if used[i]\n if pre != 0\n puts \"1 #{pre} #{i+1}\"\n end\n pre = i+1\nend"}], "negative_code": [], "src_uid": "f09b435a20a415d65803a80d57152832"} {"nl": {"description": "You are given two segments $$$[l_1; r_1]$$$ and $$$[l_2; r_2]$$$ on the $$$x$$$-axis. It is guaranteed that $$$l_1 < r_1$$$ and $$$l_2 < r_2$$$. Segments may intersect, overlap or even coincide with each other. The example of two segments on the $$$x$$$-axis. Your problem is to find two integers $$$a$$$ and $$$b$$$ such that $$$l_1 \\le a \\le r_1$$$, $$$l_2 \\le b \\le r_2$$$ and $$$a \\ne b$$$. In other words, you have to choose two distinct integer points in such a way that the first point belongs to the segment $$$[l_1; r_1]$$$ and the second one belongs to the segment $$$[l_2; r_2]$$$.It is guaranteed that the answer exists. If there are multiple answers, you can print any of them.You have to answer $$$q$$$ independent queries.", "input_spec": "The first line of the input contains one integer $$$q$$$ ($$$1 \\le q \\le 500$$$) — the number of queries. Each of the next $$$q$$$ lines contains four integers $$$l_{1_i}, r_{1_i}, l_{2_i}$$$ and $$$r_{2_i}$$$ ($$$1 \\le l_{1_i}, r_{1_i}, l_{2_i}, r_{2_i} \\le 10^9, l_{1_i} < r_{1_i}, l_{2_i} < r_{2_i}$$$) — the ends of the segments in the $$$i$$$-th query.", "output_spec": "Print $$$2q$$$ integers. For the $$$i$$$-th query print two integers $$$a_i$$$ and $$$b_i$$$ — such numbers that $$$l_{1_i} \\le a_i \\le r_{1_i}$$$, $$$l_{2_i} \\le b_i \\le r_{2_i}$$$ and $$$a_i \\ne b_i$$$. Queries are numbered in order of the input. It is guaranteed that the answer exists. If there are multiple answers, you can print any.", "sample_inputs": ["5\n1 2 1 2\n2 6 3 4\n2 4 1 3\n1 2 1 3\n1 4 5 8"], "sample_outputs": ["2 1\n3 4\n3 2\n1 2\n3 7"], "notes": null}, "positive_code": [{"source_code": "gets;puts$<.map{|e|a,b,c,d=e.split.map &:to_i;[a,(c!=a ? c : d)]*' '}"}, {"source_code": "q = gets.to_i\nq.times do\n l1, r1, l2, r2 = gets.split.map(&:to_i)\n a = l1\n b = l2\n if b == a\n b = r2\n end\n puts \"#{a} #{b}\"\nend"}, {"source_code": "input = gets.to_i\ni = 0\ninput_range = []\ninput.times do\n input_range[i] = gets.split.map(&:to_i)\n input_range[i]\n i += 1\nend\n\ni = 0\n\ninput.times do\n if input_range[i][0] != input_range[i][3]\n print \"#{input_range[i][0]} \"\n puts \"#{input_range[i][3]}\"\n else\n print \"#{input_range[i][1]} \"\n puts \"#{input_range[i][2]}\"\n end\n i += 1\nend"}, {"source_code": "n = gets.to_i\nn.times do\n l1, r1, l2, r2 = gets.split(' ').map(&:to_i)\n if (l1 != l2)\n puts [l1, l2].join(' ')\n next\n end\n if (r1 != r2)\n puts [r1, r2].join(' ')\n next\n end\n if (r1 - l1 == 2)\n puts [l1, r1].join(' ')\n next\n end\n puts [l1 + 1, r1 - 1].join(' ')\nend"}, {"source_code": "a = gets\n\ndef sol(a)\n\t(1..a.to_i).map {|num|\n\t\ta,b,c,d = gets.chomp.split\n\t\t[a,a==c ? d : c ]\n\t}\nend\n\nputs sol(a)"}, {"source_code": "f = gets.to_i\nf.times do\n l1, r1, l2, r2 = gets.to_s.chomp.split(\" \").map{|x| x.to_i}\n if r1 != r2\n puts \"#{r1} #{r2}\"\n else\n puts \"#{r1} #{r2 - 1}\"\n end\nend\n"}, {"source_code": "gets;puts$<.map{|e|a,b,c,d=e.split.map &:to_i;[a,(c!=a ? c : d)]*' '}"}, {"source_code": "gets;puts$<.map{|e|a,b,c,d=e.split.map &:to_i;[a,(c!=a ? c : d)]*' '}"}, {"source_code": "N = gets.to_i\nN.times do\n l1, r1, l2, r2 = gets.chomp.split.map(&:to_i)\n if r1 < l2 or r2 < l1\n puts \"#{l1} #{l2}\"\n else\n if l1 <= l2\n puts \"#{l1} #{r2}\"\n else\n puts \"#{r1} #{l2}\"\n end\n end\nend\n"}, {"source_code": "if File.exists?(\"input\")\n $stdin = File.open(\"input\")\n $stdout = File.open(\"output\",\"w\")\nend\n\nq = gets.to_i\nq.times do\n\tlrlr = gets.chomp.split(\" \").map(&:to_i)\n\tl1 = lrlr[0]\n\tr1 = lrlr[1]\n\tl2 = lrlr[2]\n\tr2 = lrlr[3]\n\n\tif (l1 == l2) and (l1==r2)\n\t\tputs \"#{r1} #{l1}\"\n\telse\n\t\tprint \"#{l1} \"\n\t\tif(l1==l2)\n\t\t\tprint r2\n\t\telse\n\t\t\tprint l2\n\t\tend\n\t\tputs\n\tend\nend"}], "negative_code": [{"source_code": "input = gets.to_i\ni = 0\ninput_range = []\ninput.times do\n input_range[i] = gets.split.map(&:to_i)\n input_range[i]\n i += 1\nend\n\ni = 0\n\ninput.times do\n print \"#{input_range[i][0]} \"\n puts \"#{input_range[i][3]}\"\n i += 1\nend"}, {"source_code": "n = gets.to_i\nn.times do\n l1, r1, l2, r2 = gets.split(' ').map(&:to_i)\n if (l1 != l2)\n puts [l1, l2].join(' ')\n next\n end\n if (r1 != r2)\n puts [r1, r2].join(' ')\n next\n end\n puts [l1 + 1, r1 - 1].join(' ')\nend"}, {"source_code": "a = gets\n\ndef sol(a)\n\t(1..a.to_i).map {|num|\n\t\ta,b,c,d = gets.chomp.split('\\s')\n\t\t[a,a==c ? d : c ]\n\t}\nend\n\nputs sol(a)"}], "src_uid": "cdafe800094113515e1de1acb60c4bb5"} {"nl": {"description": "A permutation — is a sequence of length $$$n$$$ integers from $$$1$$$ to $$$n$$$, in which all the numbers occur exactly once. For example, $$$[1]$$$, $$$[3, 5, 2, 1, 4]$$$, $$$[1, 3, 2]$$$ — permutations, and $$$[2, 3, 2]$$$, $$$[4, 3, 1]$$$, $$$[0]$$$ — no.Polycarp was recently gifted a permutation $$$a[1 \\dots n]$$$ of length $$$n$$$. Polycarp likes trees more than permutations, so he wants to transform permutation $$$a$$$ into a rooted binary tree. He transforms an array of different integers into a tree as follows: the maximum element of the array becomes the root of the tree; all elements to the left of the maximum — form a left subtree (which is built according to the same rules but applied to the left part of the array), but if there are no elements to the left of the maximum, then the root has no left child; all elements to the right of the maximum — form a right subtree (which is built according to the same rules but applied to the right side of the array), but if there are no elements to the right of the maximum, then the root has no right child. For example, if he builds a tree by permutation $$$a=[3, 5, 2, 1, 4]$$$, then the root will be the element $$$a_2=5$$$, and the left subtree will be the tree that will be built for the subarray $$$a[1 \\dots 1] = [3]$$$, and the right one — for the subarray $$$a[3 \\dots 5] = [2, 1, 4]$$$. As a result, the following tree will be built: The tree corresponding to the permutation $$$a=[3, 5, 2, 1, 4]$$$. Another example: let the permutation be $$$a=[1, 3, 2, 7, 5, 6, 4]$$$. In this case, the tree looks like this: The tree corresponding to the permutation $$$a=[1, 3, 2, 7, 5, 6, 4]$$$. Let us denote by $$$d_v$$$ the depth of the vertex $$$a_v$$$, that is, the number of edges on the path from the root to the vertex numbered $$$a_v$$$. Note that the root depth is zero. Given the permutation $$$a$$$, for each vertex, find the value of $$$d_v$$$.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains an integer $$$n$$$ ($$$1 \\le n \\le 100$$$) — the length of the permutation. This is followed by $$$n$$$ numbers $$$a_1, a_2, \\ldots, a_n$$$ — permutation $$$a$$$.", "output_spec": "For each test case, output $$$n$$$ values — $$$d_1, d_2, \\ldots, d_n$$$.", "sample_inputs": ["3\n5\n3 5 2 1 4\n1\n1\n4\n4 3 1 2"], "sample_outputs": ["1 0 2 3 1 \n0 \n0 1 3 2"], "notes": null}, "positive_code": [{"source_code": "# Entrada: testcases, longitud del arreglo, arreglo permutación\r\n# Salida: profundidad de cada nodo en orden de la entrada del arreglo\r\n# Formación del arbol y resultado: \r\n# 1 Paso: encontrar el número mayor del arreglo\r\n# 2 Paso: separar los elementos del arreglo con base del número mayor, de los elementos de\r\n# izquierda de este, y los elementos de la derecha\r\n# 3 Paso: en cada conjunto separado, realizar el paso 1 y 2 hasta que no se pueda dividir\r\n# Nota: si no hay elementos, se descarta(No se toma en cuenta).\r\n# 4 Paso: buscar el nivel de profundidad de cada nodo y relacionarlo con el arreglo inicial\r\n# 5 Paso: Mostrar en pantalla los niveles de profundidad de cada arreglo dado\r\n\r\n# Más ejemplos de casos\r\n# 1 Caso (Caso cargado a la derecha): 7,5,6,3,4,2,1\r\n# Nivel 0\r\n# 1 Paso: 7\r\n# 2 Paso: Izquierda--> .Derecha-->5,6,3,4,2,1\r\n# 3 Paso: Para Izquierda: ¿Es solo un elemento? No hay elemento ---> Se descarta \r\n# Para Derecha: ¿Es solo un elemento? No ---> volvemos a 1 Paso con entrada derecha \r\n# Nivel 1, Derecha\r\n# 1 Paso: 6\r\n# 2 Paso: Izquierda--> 5 .Derecha-->3,4,2,1\r\n# 3 Paso: Para Izquierda ¿Es solo un elemento? Si --->Termina. \r\n# Para Derecha: ¿Es solo un elemento? No ---> volvemos a 1 Paso con entrada derecha\r\n# Nivel 2, Derecha\r\n# 1 Paso: 4\r\n# 2 Paso: Izquierda-->3 .Derecha-->2,1\r\n# 3 Paso: Para Izquierda: ¿Es solo un elemento? Si --->Termina.\r\n# Para Derecha: ¿Es solo un elemento? No ---> volvemos a 1 Paso con entrada derecha\r\n# Nivel 3, Derecha\r\n# 1 Paso: 2\r\n# 2 Paso: Izquierda--> .Derecha-->1\r\n# 3 Paso: Para Izquierda: ¿Es solo un elemento? No hay elemento ---> Se descarta\r\n# Para Derecha: ¿Es solo un elemento? Si --->Termina.\r\n# 4 Paso: Nivel 0: 7, Nivel 1: 6, Nivel 2: 4, Nivel 3: 2\r\n# 5 Paso: 0, , 1, , 2, 3, Sin conocer 5, 3, 1\r\n# Salida correcta: No\r\n# Observación: Para los niveles sin conocer, cada que se detecte que solo es un elemento, \r\n# relacionar con el siguiente nivel ese elemento, entonces para nivel 2: 5 nivel 3: 3, \r\n# nivel 4: 1 y la salída sería: 0, 2, 1, 3, 2, 3, 4\r\n# Salida correcta con observación: Si \r\n# 2 Caso (Zig-zag): 4,2,5,1,3 \r\n# Nivel 0\r\n# 1 Paso: 5\r\n# 2 Paso: Izquierda-->4,2 .Derecha-->1,3\r\n# 3 Paso: Para Izquierda: ¿Es solo un elemento? No ---> volvemos a 1 Paso con entrada izquierda \r\n# Para Derecha: ¿Es solo un elemento? No ---> volvemos a 1 Paso con entrada derecha \r\n# Nivel 1, Izquierda\r\n# 1 Paso: 4\r\n# 2 Paso: Izquierda--> .Derecha-->2\r\n# 3 Paso: Para Izquierda ¿Es solo un elemento? No hay elemento ---> Se descarta \r\n# Para Derecha: ¿Es solo un elemento? Si ---> Termina y se relaciona con el siguiente nivel\r\n# Nivel 1, Derecha\r\n# 1 Paso: 3\r\n# 2 Paso: Izquierda-->1 .Derecha-->\r\n# 3 Paso: Para Izquierda: ¿Es solo un elemento? Si --->Termina y se relaciona con el siguiente nivel\r\n# Para Derecha: ¿Es solo un elemento? No hay elemento ---> Se descarta\r\n# 4 Paso: Nivel 0: 5, Nivel 1: 4,3, Nivel 2: 2,1\r\n# 5 Paso: 1, 2, 0, 2, 1\r\n# Salida correcta: Si\r\n\r\ndef permutationTransformation(a,n,nivel)\r\n # Observación\r\n if n == 1\r\n return nivel\r\n end\r\n #Pre paso: crear el arreglo del arbol \"d\" y los arreglos derecha, izquierda\r\n d = []\r\n izquierda, derecha = []\r\n # 1 Paso\r\n mayor = buscarMayorNumero(a,n)\r\n # 2 Paso\r\n izquierda, derecha = separarElementosArreglo(a,n,mayor)\r\n # 3 Paso\r\n if izquierda.length>0\r\n izquierda = permutationTransformation(izquierda,izquierda.length,nivel+1)\r\n end\r\n if derecha.length>0\r\n derecha = permutationTransformation(derecha,derecha.length,nivel+1)\r\n end\r\n # 4 Paso\r\n d = buscarNiveles(izquierda,nivel,derecha)\r\n return d\r\nend\r\n\r\ndef buscarNiveles(izquierda,mayor,derecha)\r\n d = []\r\n if izquierda.class == Integer\r\n d.push(izquierda)\r\n else\r\n for i in(0..izquierda.length-1)\r\n d.push(izquierda[i])\r\n end\r\n end\r\n d.push(mayor)\r\n if derecha.class == Integer\r\n d.push(derecha)\r\n else\r\n for i in(0..derecha.length-1)\r\n d.push(derecha[i])\r\n end\r\n end\r\n return d\r\nend\r\n\r\ndef separarElementosArreglo(a,n,mayor)\r\n izquierda = []\r\n derecha = []\r\n #Para izquierda\r\n for i in(0..mayor-1)\r\n izquierda.push(a[i])\r\n end\r\n #Para derecha\r\n for i in(mayor+1..n-1)\r\n derecha.push(a[i])\r\n end\r\n return izquierda, derecha\r\nend\r\n\r\ndef buscarMayorNumero(a,n)\r\n mayor = a[0]\r\n posicion = 0\r\n for i in(0..n-1)\r\n if a[i]>mayor\r\n mayor = a[i]\r\n posicion = i\r\n end\r\n end\r\n return posicion\r\nend\r\n\r\ndef convertirAInteger(a,n)\r\n for i in(0..n-1)\r\n a[i] = Integer(a[i])\r\n end\r\n return a\r\nend\r\n\r\ndef darFormatoDeSalida(d)\r\n resultado = \"\"\r\n if d.class == Integer\r\n resultado = d.to_s\r\n else\r\n for i in(0..d.length-1)\r\n resultado = resultado + d[i].to_s\r\n if i != d.length-1 \r\n resultado = resultado + \" \"\r\n end\r\n end\r\n end\r\n return resultado\r\nend\r\n\r\n# Variables\r\ntestcases = 0\r\nn = 0\r\na = []\r\nd = []\r\nresultado = []\r\n\r\n# Pre paso: Pedir los testcases, la longitud del arreglo y el arreglo\r\n# testcases\r\ntestcases = Integer(gets.chomp)\r\n\r\nfor i in(0..testcases-1)\r\n #Longitud del arreglo\r\n n = Integer(gets.chomp)\r\n #Arreglo\r\n array = gets.chomp\r\n a = array.split(\" \")\r\n a = convertirAInteger(a,n)\r\n d = permutationTransformation(a,n,0)\r\n resultado.push(darFormatoDeSalida(d))\r\nend\r\n\r\nresultado.each do |elements|\r\n puts elements\r\nend \r\n\r\n"}, {"source_code": "gets.to_i.times do\n\tn = gets.to_i\n\ta = gets.split.map(&:to_i)\n\tputs a.each_with_index.map { |x, i|\n\t\t\tres = 0\n\t\t\tnow = a[i]\n\t\t\t(i + 1...n).each do |j|\n\t\t\t\tif now < a[j]\n\t\t\t\t\tnow = a[j]\n\t\t\t\t\tres += 1\n\t\t\t\tend\n\t\t\tend\n\t\t\tnow = a[i]\n\t\t\t(0...i).reverse_each do |j|\n\t\t\t\tif now < a[j]\n\t\t\t\t\tnow = a[j]\n\t\t\t\t\tres += 1\n\t\t\t\tend\n\t\t\tend\n\t\t\tres\n\t }.join(' ')\nend\n"}], "negative_code": [], "src_uid": "a564017f9c411b39f8d4b69e629ae3bc"} {"nl": {"description": "Monocarp and Bicarp live in Berland, where every bus ticket consists of $$$n$$$ digits ($$$n$$$ is an even number). During the evening walk Monocarp and Bicarp found a ticket where some of the digits have been erased. The number of digits that have been erased is even.Monocarp and Bicarp have decided to play a game with this ticket. Monocarp hates happy tickets, while Bicarp collects them. A ticket is considered happy if the sum of the first $$$\\frac{n}{2}$$$ digits of this ticket is equal to the sum of the last $$$\\frac{n}{2}$$$ digits.Monocarp and Bicarp take turns (and Monocarp performs the first of them). During each turn, the current player must replace any erased digit with any digit from $$$0$$$ to $$$9$$$. The game ends when there are no erased digits in the ticket.If the ticket is happy after all erased digits are replaced with decimal digits, then Bicarp wins. Otherwise, Monocarp wins. You have to determine who will win if both players play optimally.", "input_spec": "The first line contains one even integer $$$n$$$ $$$(2 \\le n \\le 2 \\cdot 10^{5})$$$ — the number of digits in the ticket. The second line contains a string of $$$n$$$ digits and \"?\" characters — the ticket which Monocarp and Bicarp have found. If the $$$i$$$-th character is \"?\", then the $$$i$$$-th digit is erased. Note that there may be leading zeroes. The number of \"?\" characters is even.", "output_spec": "If Monocarp wins, print \"Monocarp\" (without quotes). Otherwise print \"Bicarp\" (without quotes).", "sample_inputs": ["4\n0523", "2\n??", "8\n?054??0?", "6\n???00?"], "sample_outputs": ["Bicarp", "Bicarp", "Bicarp", "Monocarp"], "notes": "NoteSince there is no question mark in the ticket in the first example, the winner is determined before the game even starts, and it is Bicarp.In the second example, Bicarp also wins. After Monocarp chooses an erased digit and replaces it with a new one, Bicap can choose another position with an erased digit and replace it with the same digit, so the ticket is happy."}, "positive_code": [{"source_code": "n = gets.to_i\ndigits = gets.chop.split('')\n\nfront = digits[0...(n/2)]\nrear = digits[(n/2)...n]\n\nfSum = front.map(&:to_i).reduce(:+)\nrSum = rear.map(&:to_i).reduce(:+)\nsumDelta = fSum - rSum\n\nfEmpty = front.count('?')\nrEmpty = rear.count('?')\nemptyDelta = fEmpty - rEmpty\ntotalEmpty = fEmpty + rEmpty\n\ncanEqual = false\ncanEqual = true if sumDelta == 0\n\nif emptyDelta.abs > 0\n d = emptyDelta / 2\n canEqual = (sumDelta % 9 == 0 && sumDelta / 9 == -d)\nend\n\nif canEqual\n puts \"Bicarp\"\nelse\n puts \"Monocarp\"\nend\n"}], "negative_code": [{"source_code": "n = gets.to_i\ndigits = gets.chop.split('')\n\nfront = digits[0...(n/2)]\nrear = digits[(n/2)...n]\n\nfSum = front.map(&:to_i).reduce(:+)\nrSum = rear.map(&:to_i).reduce(:+)\nsumDelta = fSum - rSum\n\nfEmpty = front.count('?')\nrEmpty = rear.count('?')\nemptyDelta = fEmpty - rEmpty\ntotalEmpty = fEmpty + rEmpty\n\ncanEqual = false\ncanEqual = true if sumDelta == 0\n\nif emptyDelta.abs > 0\n d = emptyDelta / 2\n canEqual = true if sumDelta % 9 == 0 && sumDelta / 9 == -d\nend\n\nif canEqual\n puts \"Bicarp\"\nelse\n puts \"Monocarp\"\nend\n"}], "src_uid": "028882706ed58b61b4672fc3e76852c4"} {"nl": {"description": "The little girl loves the problems on array queries very much.One day she came across a rather well-known problem: you've got an array of $$$n$$$ elements (the elements of the array are indexed starting from 1); also, there are $$$q$$$ queries, each one is defined by a pair of integers $$$l_i$$$, $$$r_i$$$ $$$(1 \\le l_i \\le r_i \\le n)$$$. You need to find for each query the sum of elements of the array with indexes from $$$l_i$$$ to $$$r_i$$$, inclusive.The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum.", "input_spec": "The first line contains two space-separated integers $$$n$$$ ($$$1 \\le n \\le 2\\cdot10^5$$$) and $$$q$$$ ($$$1 \\le q \\le 2\\cdot10^5$$$) — the number of elements in the array and the number of queries, correspondingly. The next line contains $$$n$$$ space-separated integers $$$a_i$$$ ($$$1 \\le a_i \\le 2\\cdot10^5$$$) — the array elements. Each of the following $$$q$$$ lines contains two space-separated integers $$$l_i$$$ and $$$r_i$$$ ($$$1 \\le l_i \\le r_i \\le n$$$) — the $$$i$$$-th query.", "output_spec": "In a single line print, a single integer — the maximum sum of query replies after the array elements are reordered. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.", "sample_inputs": ["3 3\n5 3 2\n1 2\n2 3\n1 3", "5 3\n5 2 4 1 3\n1 5\n2 3\n2 3"], "sample_outputs": ["25", "33"], "notes": null}, "positive_code": [{"source_code": "n, q = gets.split(' ').collect!{|i| i.to_i}\n\narr = gets.split(' ').collect!{|i| i.to_i}\n\nd = Array.new(n, 0)\nq.times do\n\tl, r = gets.split(' ').collect!{|i| i.to_i - 1}\n\t# (l..r).each do |j|\n\t# \tel[j] += 1\n\t# end\n\td[l] += 1\n\td[r+1] -= 1 if r < n-1\nend\n\nel = Array.new(n, 0)\nsum = 0\nn.times do |i|\n\tsum += d[i]\n\tel[i] = sum\nend\n\nel.sort!.reverse!\n\narr.sort!.reverse!\n\nmaxsum = 0\n\nn.times do |i|\n\t# j = n - i - 1\n\tmaxsum += arr[i]*el[i]\n\tbreak if el[i] == 0\nend\n\np maxsum\n"}, {"source_code": "n, q = gets.split.collect{|i| i.to_i}\na = gets.split.collect{|i| i.to_i}.sort.reverse\ns = Array.new(n+1,0)\nq.times do\n l, r = gets.split.collect{|i| i.to_i-1}\n s[l] += 1\n s[r+1] -= 1\nend\nn.times do |i|\n s[i+1] += s[i]\nend\ns = s.sort.reverse\nputs (0...n).inject(0){|sum, i| sum += s[i]*a[i]}\n"}, {"source_code": "n, q = gets.split.collect{|i| i.to_i}\na = gets.split.collect{|i| i.to_i}.sort.reverse\ns = Array.new(n+1,0)\n(0...q).each do\n l, r = gets.split.collect{|i| i.to_i-1}\n s[l] += 1\n s[r+1] -= 1\nend\n(1...n).each do |i|\n s[i] += s[i-1]\nend\ns = s.sort.reverse\nputs (0...n).inject(0){|sum, i| sum += s[i]*a[i]}\n"}, {"source_code": "n, q = gets.split.collect{|i| i.to_i}\na = gets.split.collect{|i| i.to_i}\ns = Array.new(n+1,0)\ne = Array.new(n+1,0)\n(0...q).each do\n tmp = gets.split.collect{|i| i.to_i}\n s[tmp[0]] += 1\n e[tmp[1]] += 1\nend\n(1..n).each do |i|\n s[i] += s[i-1]\n e[i] += e[i-1]\nend\n(1..n).each do |i|\n s[i] = s[i]-e[i-1]\nend\ns = s.sort.reverse\na = a.sort.reverse\nputs (0...n).inject(0){|sum, i| sum += s[i]*a[i]}\n"}, {"source_code": "n, q = gets.split.collect{|i| i.to_i}\na = gets.split.collect{|i| i.to_i}.sort.reverse\ns = Array.new(n+1,0)\nq.times do\n l, r = gets.split.collect{|i| i.to_i-1}\n s[l] += 1\n s[r+1] -= 1\nend\nn.times do |i|\n s[i+1] += s[i]\nend\ns.sort!.reverse!\nputs (0...n).inject(0){|sum, i| sum += s[i]*a[i]}\n"}, {"source_code": "n,m,=gets.split.map{|x|x.to_i}\na=gets.split.map{|x|x.to_i}.sort.reverse\nb=Array.new(300000,0)\nm.times{|i|\n l,r,=gets.split.map{|x|x.to_i}\n b[l-1]+=1\n b[r]-=1\n}\nz=0\nb=b.map{|e|e+z;z+=e}\nb.sort!.reverse!\nz=0\nfor i in 0..n-1 do\n z+=(b[i]*a[i])\nend\nputs \"#{z}\""}], "negative_code": [], "src_uid": "926ec28d1c80e7cbe0bb6d209e664f48"} {"nl": {"description": "In a strategic computer game \"Settlers II\" one has to build defense structures to expand and protect the territory. Let's take one of these buildings. At the moment the defense structure accommodates exactly n soldiers. Within this task we can assume that the number of soldiers in the defense structure won't either increase or decrease.Every soldier has a rank — some natural number from 1 to k. 1 stands for a private and k stands for a general. The higher the rank of the soldier is, the better he fights. Therefore, the player profits from having the soldiers of the highest possible rank.To increase the ranks of soldiers they need to train. But the soldiers won't train for free, and each training session requires one golden coin. On each training session all the n soldiers are present.At the end of each training session the soldiers' ranks increase as follows. First all the soldiers are divided into groups with the same rank, so that the least possible number of groups is formed. Then, within each of the groups where the soldiers below the rank k are present, exactly one soldier increases his rank by one.You know the ranks of all n soldiers at the moment. Determine the number of golden coins that are needed to increase the ranks of all the soldiers to the rank k.", "input_spec": "The first line contains two integers n and k (1 ≤ n, k ≤ 100). They represent the number of soldiers and the number of different ranks correspondingly. The second line contains n numbers in the non-decreasing order. The i-th of them, ai, represents the rank of the i-th soldier in the defense building (1 ≤ i ≤ n, 1 ≤ ai ≤ k).", "output_spec": "Print a single integer — the number of golden coins needed to raise all the soldiers to the maximal rank.", "sample_inputs": ["4 4\n1 2 2 3", "4 3\n1 1 1 1"], "sample_outputs": ["4", "5"], "notes": "NoteIn the first example the ranks will be raised in the following manner:1 2 2 3  →  2 2 3 4  →  2 3 4 4  →  3 4 4 4  →  4 4 4 4Thus totals to 4 training sessions that require 4 golden coins."}, "positive_code": [{"source_code": "k=gets.split[1].to_i\na=gets.split.map(&:to_i)\n\nranks=Array.new(104,0)\na.each{|i| ranks[i]+=1}\nans=0\nn=a.length\nloop do\n if ranks[k]==n then\n puts ans\n exit\n end\n next_rank=Array.new(104,0)\n 1.upto(k-1){|a|\n next_rank[a]+=[ranks[a]-1,0].max\n next_rank[a+1]+=[ranks[a],1].min\n }\n next_rank[k]+=ranks[k]\n ranks=next_rank\n ans+=1\nend\n"}, {"source_code": "include Math\nrequire \"prime\"\nrequire \"bigdecimal\"\nrequire \"bigdecimal/math\"\nrequire \"matrix\"\nrequire \"pp\"\nrequire \"benchmark\"\n\nInfinity = 1/0.0\nclass Matrix;def inspect;empty? ? \"Matrix.empty(#{row_size}, #{column_size})\":\"Matrix[\\n#{@rows.map(&:inspect).join(\"\\n\")}\\n]\";end;end\ndef pbin(obj);case obj;when Integer;puts obj.to_s(2);end;end\nclass String;def to_a;split(\"\");end;end\nclass String;def to_ia;split.map(&:to_i);end;end\ndef getia;gets.to_ia;end\ndef geti;gets.to_i;end\nclass Object;def dcopy;Marshal.load(Marshal.dump(self));end;end\n\n\nn,k=getia\nar=getia\ndp=Array.new(k+1).fill(0)\nar.each{|a|dp[a]+=1}\nt=0\nwhile dp[k] != n\n nextdp=Array.new(k+1).fill(0)\n for j in 0...k\n next if dp[k-j-1].zero?\n dp[k-j]+=1\n dp[k-j-1]-=1\n end\n t+=1\nend\nputs t\n"}, {"source_code": "n,k,*a=gets(p).split.map(&:to_i)\na< 1\n tbl2[key + 1] += 1\n else\n tbl2[key] += value\n end\n end\n tbl = tbl2\n cnt += 1\nend\np cnt\n"}, {"source_code": "n,k,*a=gets(p).split.map(&:to_i)\na< 0\n next\n end\n\n first = false\n end\n\n if positive && min < i\n min = i\n next\n end\n\n if positive && i < 0\n positive = false\n positive_arr.push(min)\n min = i\n next\n end\n\n if !positive && (min == 0 || min < i) && i < 0\n min = i\n next\n end\n\n next unless !positive && i > 0\n\n positive = true\n positive_arr.push(min)\n min = i\n end\n\n positive_arr.push(min) if min != 0\n end\n\n puts(if positive_arrs[0].size == positive_arrs[1].size\n positive_arrs.map(&:sum).max\n elsif positive_arrs[0].size > positive_arrs[1].size\n positive_arrs[0].sum\n else\n positive_arrs[1].sum\n end)\n end\n\n private\n\n attr_reader :n\nend\n\ngets.to_i.times do\n Result.new.print_result\nend\n"}, {"source_code": "number_of_testcases = gets.to_s.to_i\n\nnumber_of_testcases.times do |i|\n \n n = gets.to_s.to_i\n a = gets.to_s.split.map{|e|e.to_i}.push 0\n \n ans = 0\n s = []\n a.each do |k|\n if s.size > 0 && s[-1] * k <= 0\n ans += s.max\n s = []\n end\n s << k\n end\n # ans += s.max \n puts ans\nend\n"}, {"source_code": "1.upto(gets.to_i) do\n gets\n a = gets.split(' ').map(&:to_i)\n\n blocks = []\n while !a.empty?\n sign = a.first / a.first.abs\n blocks << a.take_while { |x| x / x.abs == sign }\n\n a = a.drop(blocks.last.size)\n end\n\n puts blocks.inject(0) { |s, e| s + e.max }\nend\n"}, {"source_code": "t=gets.to_i\n(1).upto(t) do\n n=gets.to_i\n a=gets.split(' ').map(&:to_i)\n vec = []\n ans = 0\n pos=false\n a.each do |i|\n if vec.empty?\n vec.push(i)\n pos=i>0\n next\n end\n if(i>0 != pos)\n ans+=vec.max\n vec=[i]\n pos = i>0\n else\n vec.push(i)\n end\n end\n ans+=vec.max\n puts ans\nend\n"}], "negative_code": [{"source_code": "t=gets.to_i\n(1).upto(t) do\n n=gets.to_i\n n>>=1\n if(n&1)==1\n puts \"NO\"\n next\n end\n puts \"YES\"\n sum =0\n (1).upto(n) do |i|\n print \"#{i*2} \"\n sum+=i*2\n end\n\n (1).upto(n-1) do |i|\n sum-=i*2-1\n print \"#{i*2-1} \"\n end\n puts sum\nend\n"}], "src_uid": "39480cdf697fc9743dc9665f989077d7"} {"nl": {"description": "Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≤ i ≤ m) and column j (1 ≤ j ≤ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large.", "input_spec": "The first line contains two integer m and n (1 ≤ m, n ≤ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1).", "output_spec": "In the first line, print \"NO\" if Nam has made a mistake when calculating B, otherwise print \"YES\". If the first line is \"YES\", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one.", "sample_inputs": ["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"], "sample_outputs": ["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"], "notes": null}, "positive_code": [{"source_code": "m,n = gets.split.map &:to_i\n\nb = []\na = Array.new(m) {Array.new(n)}\ncorrect_b = Array.new(m) {Array.new(n, 0)}\n\nm.times do |i|\n b[i] = gets.split.map(&:to_i)\nend\n\nb.each_with_index do |arr, i|\n arr.each_with_index do |el, j|\n if el == 0\n m.times {|line| a[line][j] = 0}\n n.times {|col| a[i][col] = 0}\n end\n end\nend\n\nis_correct = true\n\nm.times do |i|\n n.times do |j|\n unless a[i][j]\n a[i][j] = 1\n\n m.times {|line| correct_b[line][j] = 1 }\n n.times {|col| correct_b[i][col] = 1 }\n end\n end\nend\n\nm.times do |i|\n break unless is_correct\n n.times do |j|\n if b[i][j] != correct_b[i][j]\n is_correct = false\n break\n end\n end\nend\n\n\nif is_correct\n puts 'YES'\n a.each {|k| puts k.join(' ')}\nelse\n puts 'NO'\nend\n\n# puts\n# correct_b.each {|k| puts k.join(' ')}"}, {"source_code": "row_num, col_num = STDIN.readline.split.map {|s| s.to_i }\nb = []\nrow_num.times do\n\tb.push(STDIN.readline.split.map {|s| s.to_i })\nend\n\nor_row = row_num.times.map { 1 }\nor_col = col_num.times.map { 1 }\n\nb.each_with_index do |row, r|\n\trow.each_with_index do |x, c|\n\t\tif x == 0\n\t\t\tor_row[r] = 0\n\t\t\tor_col[c] = 0\n\t\tend\n\tend\nend\n\nif or_row.inject(:+) == 0\n\tcol_num.times {|c| or_col[c] = 0 }\nend\nif or_col.inject(:+) == 0\n\trow_num.times {|r| or_row[r] = 0 }\nend\n\nb.each_with_index do |row, r|\n\trow.each_with_index do |x, c|\n\t\tif x == 1\n\t\t\tif or_row[r] == 0 and or_col[c] == 0\n\t\t\t\tputs 'NO'\n\t\t\t\texit\n\t\t\tend\n\t\tend\n\tend\nend\n\na = row_num.times.map { col_num.times.map { 1 } }\nor_row.each_with_index do |x, r|\n\tcol_num.times { |c| a[r][c] = 0 } if x == 0\nend\nor_col.each_with_index do |x, c|\n\trow_num.times { |r| a[r][c] = 0 } if x == 0\nend\n\nputs 'YES'\nrow_num.times {|r| puts a[r].join ' ' }\n"}, {"source_code": "row_num, col_num = STDIN.readline.split.map {|s| s.to_i }\nb = []\nrow_num.times do\n\tb.push(STDIN.readline.split.map {|s| s.to_i })\nend\n\nor_row = row_num.times.map { 1 }\nor_col = col_num.times.map { 1 }\n\nb.each_with_index do |row, r|\n\trow.each_with_index do |x, c|\n\t\tor_row[r] = or_col[c] = 0 if x == 0\n\tend\nend\n\nif or_row.inject(:+) == 0\n\tcol_num.times {|c| or_col[c] = 0 }\nend\nif or_col.inject(:+) == 0\n\trow_num.times {|r| or_row[r] = 0 }\nend\n\nb.each_with_index do |row, r|\n\trow.each_with_index do |x, c|\n\t\tif x == 1 and or_row[r] == 0 and or_col[c] == 0\n\t\t\tputs 'NO'\n\t\t\texit\n\t\tend\n\tend\nend\n\na = row_num.times.map { col_num.times.map { 1 } }\nor_row.each_with_index do |x, r|\n\tcol_num.times { |c| a[r][c] = 0 } if x == 0\nend\nor_col.each_with_index do |x, c|\n\trow_num.times { |r| a[r][c] = 0 } if x == 0\nend\n\nputs 'YES'\nrow_num.times {|r| puts a[r].join ' ' }\n"}, {"source_code": "r,c=gets.chomp.split.map(&:to_i)\na,b,fr,fc,dr,dc=Array.new(r,0),Array.new(c,0),0,0,0,0\nr.times do |i|\n\tx=gets.chomp.split.map(&:to_i)\n\tx.each_index do |j|\n\t\tif x[j]==1\n\t\t\ta[i]+=1\n\t\t\tb[j]+=1\n\t\tend\n\tend\nend\n=begin\nprint a\nputs\nprint b\nputs\n=end\nr.times { |i| (a[i]==c) ? fr+=1 : dr+=a[i] }\nc.times { |i| (b[i]==r) ? fc+=1 : dc+=b[i] }\n#puts \"#{dr} #{dc} #{fr} #{fc} #{r} #{c}\"\nif (dr!=(r-fr)*fc)||(dc!=(c-fc)*fr)||(fr==0&&fc!=0)||(fr!=0&&fc==0)\n\tputs \"NO\"\nelse\n\tputs \"YES\"\n\tr.times do |i|\n\t\tc.times do |j|\n\t\t\tprint (a[i]==c && b[j]==r) ? \"1 \" : \"0 \"\n\t\tend\n\t\tputs\n\tend\nend\n"}, {"source_code": "M, N = gets.split.map(&:to_i)\nval = []\nM.times{|m|\n val[m] = gets.split.map(&:to_i).map{|i| i==1}\n}\n\n\nMallone = []\nM.times{|m|\n Mallone[m] = true\n N.times{|n|\n Mallone[m] &= (val[m][n])\n # puts \"Mallone[#{m}]=#{Mallone[m]}\"\n }\n}\n\nNallone = []\nN.times{|n|\n Nallone[n] = true\n M.times{|m|\n Nallone[n] &= (val[m][n])\n # puts \"Nallone[#{n}]=#{Nallone[n]}\"\n }\n}\n\nresult = []\nM.times{|m|\n result[m] = []\n N.times{|n|\n result[m][n] = false\n }\n}\n\nM.times{|m|\n if Mallone[m]\n flag = false\n N.times{|n|\n if Nallone[n]\n flag = true\n result[m][n] = true\n end\n }\n if !flag\n puts \"NO\"\n exit 0\n end\n else\n N.times{|n|\n if val[m][n]\n if ! Nallone[n]\n puts \"NO\"\n exit 0\n end\n end\n }\n end\n}\n\nN.times{|n|\n if Nallone[n]\n flag = false\n M.times{|m|\n if Mallone[m]\n flag = true\n result[m][n] = true\n end\n }\n if !flag\n puts \"NO\"\n exit 0\n end\n else\n M.times{|m|\n if val[m][n]\n if ! Mallone[m]\n puts \"NO\"\n exit 0\n end\n end\n }\n end\n}\nputs \"YES\"\nM.times{|m|\n puts result[m].map{|b| (b&&1||0)}.join(\" \")\n}\n"}, {"source_code": "def make_zero(i,j,n,m)\n\t@arr[i] = Array.new(m,0)\n\t(0..(n-1)).each do |row|\n\t\t@arr[row][j] = 0\n\tend\nend\n\n\ndef calc(i,j,n,m)\n\trow_or = @arr[i].inject(0,&:|)\n\tcol_or = 0\n\t(0..(n-1)).each do |row|\n\t\tcol_or |= @arr[row][j] \n\tend\n\trow_or | col_or\nend\n\nn,m = gets.split(\" \").map(&:to_i)\nb_arr = []\n@arr = []\nn.times do\n\tb_arr << gets.split(\" \").map(&:to_i)\n\t@arr << Array.new(m,1)\nend\n\nn.times do |i|\n\tm.times do |j|\n\t\tmake_zero(i,j,n,m) if b_arr[i][j] ==0\n\tend\nend\n\n@new_arr = []\nn.times do |i|\n\t@new_arr << []\n\tm.times do |j|\n\t\t@new_arr[i][j] = calc(i,j,n,m)\n\tend\nend\n# puts \"#{@arr}\"\n# puts \"barr #{b_arr}\"\n# puts \"new #{@new_arr}\"\nif b_arr == @new_arr\n\tputs \"YES\"\n\t@arr.each do |a|\n\t\tputs a.join(\" \")\n\tend\nelse\n\tputs \"NO\"\nend"}, {"source_code": "def read_line\n gets.chomp.split.map &:to_i\nend\n\nn, m = read_line\na, b = [], []\n\nn.times {\n a << Array.new(m,1)\n b << read_line\n}\n\nn.times { |i|\n a[i] = Array.new(m, 0) if b[i].include? 0 \n}\n\nm.times { |j| \n a.each { |x| x[j] = 0 } if (0...n).map { |i| b[i][j] }.include? 0\n}\n\nc = Array.new(n)\nn.times { |i| \n c[i] = Array.new(m)\n m.times { |j|\n c[i][j] = (a[i] + (0...n).map { |k| a[k][j] }).inject { |x, y| x|y }\n }\n}\n\nif c == b \n\tputs 'YES'\n\ta.each { |x|\n\t\tputs x*' '\n\t}\nelse\n\tputs 'NO'\nend\t"}, {"source_code": "# 486B\n\ndef read_line\n gets.chomp.split.map &:to_i\nend\n\nm, n = read_line\na, b = [], []\nm.times {\n a << Array.new(n, 1)\n b << read_line\n}\n\nm.times { |i|\n a[i] = Array.new(n, 0) if b[i].include? 0\n}\n\nn.times { |j|\n a.each { |x| x[j] = 0 } if (0...m).map { |i| b[i][j] }.include? 0\n}\n\nc = Array.new(m)\nm.times { |i|\n c[i] = Array.new(n)\n n.times { |j|\n c[i][j] = (a[i] + (0...m).map { |k| a[k][j] }).inject { |x, y| x | y }\n }\n}\n\nif c == b\n puts 'YES'\n a.each { |x|\n puts x * ' '\n }\nelse\n puts 'NO'\nend"}], "negative_code": [{"source_code": "row_num, col_num = STDIN.readline.split.map {|s| s.to_i }\nb = []\nrow_num.times do\n\tb.push(STDIN.readline.split.map {|s| s.to_i })\nend\n\nor_row = row_num.times.map { 1 }\nor_col = col_num.times.map { 1 }\n\nb.each_with_index do |row, r|\n\trow.each_with_index do |x, c|\n\t\tif x == 0\n\t\t\tor_row[r] = 0\n\t\t\tor_col[c] = 0\n\t\tend\n\tend\nend\n\nb.each_with_index do |row, r|\n\trow.each_with_index do |x, c|\n\t\tif x == 1\n\t\t\tif or_row[r] == 0 and or_col[c] == 0\n\t\t\t\tputs 'NO'\n\t\t\t\texit\n\t\t\tend\n\t\tend\n\tend\nend\n\nif row_num >= col_num and or_row.inject(:+) == 0\n\tcol_num.times {|c| or_col[c] = 0 }\nend\nif col_num >= row_num and or_col.inject(:+) == 0\n\trow_num.times {|r| or_row[r] = 0 }\nend\n\na = row_num.times.map { col_num.times.map { 1 } }\nor_row.each_with_index do |x, r|\n\tcol_num.times { |c| a[r][c] = 0 } if x == 0\nend\nor_col.each_with_index do |x, c|\n\trow_num.times { |r| a[r][c] = 0 } if x == 0\nend\n\nputs 'YES'\nrow_num.times {|r| puts a[r].join ' ' }\n"}, {"source_code": "row_num, col_num = STDIN.readline.split.map {|s| s.to_i }\nb = []\nrow_num.times do\n\tb.push(STDIN.readline.split.map {|s| s.to_i })\nend\n\nor_row = row_num.times.map { 1 }\nor_col = col_num.times.map { 1 }\n\nb.each_with_index do |row, r|\n\trow.each_with_index do |x, c|\n\t\tif x == 0\n\t\t\tor_row[r] = 0\n\t\t\tor_col[c] = 0\n\t\tend\n\tend\nend\n\nb.each_with_index do |row, r|\n\trow.each_with_index do |x, c|\n\t\tif x == 1\n\t\t\tif or_row[r] == 0 and or_col[c] == 0\n\t\t\t\tputs 'NO'\n\t\t\t\texit\n\t\t\tend\n\t\tend\n\tend\nend\n\na = row_num.times.map { col_num.times.map { 1 } }\nor_row.each_with_index do |x, r|\n\tcol_num.times { |c| a[r][c] = 0 } if x == 0\nend\nor_col.each_with_index do |x, c|\n\trow_num.times { |r| a[r][c] = 0 } if x == 0\nend\n\nputs 'YES'\nrow_num.times {|r| puts a[r].join ' ' }\n"}, {"source_code": "row_num, col_num = STDIN.readline.split.map {|s| s.to_i }\nb = []\nrow_num.times do\n\tb.push(STDIN.readline.split.map {|s| s.to_i })\nend\n\nor_row = row_num.times.map { 1 }\nor_col = col_num.times.map { 1 }\n\nb.each_with_index do |row, r|\n\trow.each_with_index do |x, c|\n\t\tif x == 0\n\t\t\tor_row[r] = 0\n\t\t\tor_col[c] = 0\n\t\tend\n\tend\nend\n\nif row_num >= col_num and or_row.inject(:+) == 0\n\tcol_num.times {|c| or_col[c] = 0 }\nend\nif col_num >= row_num and or_col.inject(:+) == 0\n\trow_num.times {|r| or_row[r] = 0 }\nend\n\nb.each_with_index do |row, r|\n\trow.each_with_index do |x, c|\n\t\tif x == 1\n\t\t\tif or_row[r] == 0 and or_col[c] == 0\n\t\t\t\tputs 'NO'\n\t\t\t\texit\n\t\t\tend\n\t\tend\n\tend\nend\n\na = row_num.times.map { col_num.times.map { 1 } }\nor_row.each_with_index do |x, r|\n\tcol_num.times { |c| a[r][c] = 0 } if x == 0\nend\nor_col.each_with_index do |x, c|\n\trow_num.times { |r| a[r][c] = 0 } if x == 0\nend\n\nputs 'YES'\nrow_num.times {|r| puts a[r].join ' ' }\n"}], "src_uid": "bacd613dc8a91cee8bef87b787e878ca"} {"nl": {"description": "While Mahmoud and Ehab were practicing for IOI, they found a problem which name was Longest common subsequence. They solved it, and then Ehab challenged Mahmoud with another problem.Given two strings a and b, find the length of their longest uncommon subsequence, which is the longest string that is a subsequence of one of them and not a subsequence of the other.A subsequence of some string is a sequence of characters that appears in the same order in the string, The appearances don't have to be consecutive, for example, strings \"ac\", \"bc\", \"abc\" and \"a\" are subsequences of string \"abc\" while strings \"abbc\" and \"acb\" are not. The empty string is a subsequence of any string. Any string is a subsequence of itself.", "input_spec": "The first line contains string a, and the second line — string b. Both of these strings are non-empty and consist of lowercase letters of English alphabet. The length of each string is not bigger than 105 characters.", "output_spec": "If there's no uncommon subsequence, print \"-1\". Otherwise print the length of the longest uncommon subsequence of a and b.", "sample_inputs": ["abcd\ndefgh", "a\na"], "sample_outputs": ["5", "-1"], "notes": "NoteIn the first example: you can choose \"defgh\" from string b as it is the longest subsequence of string b that doesn't appear as a subsequence of string a."}, "positive_code": [{"source_code": "#!/usr/bin/ruby\na,b=$<.map &:chomp\np a==b ? -1 : [a.size,b.size].max"}, {"source_code": "a = gets.chomp\nb = gets.chomp\nif a == b\n puts -1\nelse\n puts [a, b].map(&:length).max\nend\n"}, {"source_code": "a = gets.chomp()\nb = gets.chomp()\n\nif a == b\n puts \"-1\"\nelsif a.size > b.size\n puts a.size\nelse\n puts b.size\nend\n"}, {"source_code": "TEST_MODE = ARGV[0] == 't'\n\ndef evaluate(actual, expected); actual == expected ? \"OK\" : \"**NG**\"; end\ndef tests(); yield if TEST_MODE; end\ndef to_array(val); val.kind_of?(Array) ? val : [val]; end\ndef parse_int_array(str); str.split(\" \").collect{|s| s.to_i}; end\ndef p(*arg); puts \"DEBUG: \" + arg.to_s if TEST_MODE; end\n\ndef t(input, expected_output)\n input = to_array(input)\n expected_output = to_array(expected_output)\n output = []\n solve(input, output)\n puts \"#{evaluate(output, expected_output)} : input=#{input.to_s}, expected=#{expected_output.to_s}, actual=#{output.to_s}\"\nend\n\n# actual codes here\n\ndef solve(input, output)\n a, b = input[0], input[1]\n if a == b\n output << \"-1\"\n elsif a.length == b.length\n output << a.length\n else\n output << [a.length, b.length].max.to_s\n end\nend\n\ntests do\n # test cases\n t([\"abcd\", \"defgh\"], \"5\")\n t([\"a\", \"a\"], \"-1\")\n t([\"abc\", \"b\"], \"3\")\n t([\"abcde\", \"acde\"], \"5\")\nend\n\nputs solve(STDIN.read.split(\"\\n\"), []) unless TEST_MODE\n"}, {"source_code": "a = gets.strip\nb = gets.strip\nif a == b\n puts \"-1\"\nelse\n puts [a.length, b.length].max\nend\n"}, {"source_code": "def f(a,b)\n\tp1=0\n\tp2=0\n\twhile p1 0\n memoized_object_name = \"@_memoized_#{methodname}\"\n define_method(methodname) do\n if(instance_variable_get(memoized_object_name).nil?)\n instance_variable_set(memoized_object_name, unbound_method.bind(self).call)\n end\n instance_variable_get(memoized_object_name)\n end\n end\nend\n\nclass Problem\n attr_reader(:a, :b)\n def initialize(a, b)\n @a = a\n @b = b\n end\n\n def ans\n return -1 if a == b\n return [a.length, b.length].max\n end\n\n def print!\n puts(ans)\n end\nend\n\na = gets.strip\nb = gets.strip\nProblem.new(a, b).print!\n"}, {"source_code": "a = gets.chomp\nb = gets.chomp\n\ndef max a, b\n\tif a > b\n\t\treturn a\n\telse\n\t\treturn b\n\tend\nend\n\nif a == b\n\tputs -1\nelse\n\tputs max(a.size, b.size)\nend\n\n"}, {"source_code": "#!/usr/bin/ruby\n\na = gets.chomp\nb = gets.chomp\n\nif a.size > b.size then\n\tputs a.size\nelsif a.size < b.size then\n\tputs b.size\nelsif a == b then\n\tputs -1\nelse\n\tputs a.size\nend"}, {"source_code": "#!/usr/bin/env ruby\n\na = gets.strip\nb = gets.strip\n\na_size = a.size\nb_size = b.size\n\ncmp = a_size <=> b_size\nif cmp == 1\n puts a_size\nelsif cmp == -1\n puts b_size\nelsif a == b\n puts -1\nelse\n puts a_size\nend\n\nexit 0\n"}, {"source_code": "x = gets.chomp\ny = gets.chomp\nres = x != y ? (x.size < y.size ? y.size : x.size) : -1\nputs res\n"}, {"source_code": "a = gets.chop\nb = gets.chop\nif a == b\n puts -1\nelse\n puts [a.size, b.size].max\nend\n"}, {"source_code": "a,b=$<.map &:chomp\np a==b ? -1 : [a.size,b.size].max"}], "negative_code": [{"source_code": "a = gets.strip\nb = gets.strip\nn = [a.length, b.length].max\nans = count = 0\nn.times do |i|\n if a[i] != b[i]\n count += 1\n else\n count = 0\n end\n ans = [ans, count].max\nend\nans = -1 if ans == 0\nputs ans\n"}, {"source_code": "a=gets.chomp\nb=gets.chomp\n\np1=0\np2=0\n\nwhile p1=x)\r\n summaA = 0\r\n else\r\n summaA=a-x\r\n end\r\n if(b>=y)\r\n summaB=0\r\n else\r\n summaB=b-y\r\n end\r\n summa = summaA+summaB+c\r\n if(summa>=0)\r\n puts \"YES\"\r\n else \r\n puts \"NO\"\r\n end\r\nend\r\n"}, {"source_code": "gets.to_i.times do\r\n a, b, c, x, y = gets.split.map(&:to_i)\r\n x = [x - a, 0].max\r\n y = [y - b, 0].max\r\n puts x + y <= c ? \"YES\" : \"NO\"\r\nend\r\n"}], "negative_code": [], "src_uid": "7ac27da2546a50d453f7bb6cacef1068"} {"nl": {"description": "Ashish has a binary string $$$s$$$ of length $$$n$$$ that he wants to sort in non-decreasing order.He can perform the following operation: Choose a subsequence of any length such that its elements are in non-increasing order. Formally, choose any $$$k$$$ such that $$$1 \\leq k \\leq n$$$ and any sequence of $$$k$$$ indices $$$1 \\le i_1 \\lt i_2 \\lt \\ldots \\lt i_k \\le n$$$ such that $$$s_{i_1} \\ge s_{i_2} \\ge \\ldots \\ge s_{i_k}$$$. Reverse this subsequence in-place. Formally, swap $$$s_{i_1}$$$ with $$$s_{i_k}$$$, swap $$$s_{i_2}$$$ with $$$s_{i_{k-1}}$$$, $$$\\ldots$$$ and swap $$$s_{i_{\\lfloor k/2 \\rfloor}}$$$ with $$$s_{i_{\\lceil k/2 \\rceil + 1}}$$$ (Here $$$\\lfloor x \\rfloor$$$ denotes the largest integer not exceeding $$$x$$$, and $$$\\lceil x \\rceil$$$ denotes the smallest integer not less than $$$x$$$) Find the minimum number of operations required to sort the string in non-decreasing order. It can be proven that it is always possible to sort the given binary string in at most $$$n$$$ operations.", "input_spec": "The first line contains a single integer $$$t$$$ $$$(1 \\le t \\le 1000)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 \\le n \\le 1000)$$$  — the length of the binary string $$$s$$$. The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$ containing only $$$0$$$s and $$$1$$$s. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$1000$$$.", "output_spec": "For each test case output the following: The minimum number of operations $$$m$$$ in the first line ($$$0 \\le m \\le n$$$). Each of the following $$$m$$$ lines should be of the form: $$$k$$$ $$$i_1$$$ $$$i_2$$$ ... $$$i_{k}$$$, where $$$k$$$ is the length and $$$i_1 \\lt i_2 \\lt ... \\lt i_{k}$$$ are the indices of the chosen subsequence. For them the conditions from the statement must hold. ", "sample_inputs": ["3\n7\n0011111\n5\n10100\n6\n001000"], "sample_outputs": ["0\n1\n4 1 3 4 5 \n1\n3 3 5 6"], "notes": "NoteIn the first test case, the binary string is already sorted in non-decreasing order.In the second test case, we can perform the following operation: $$$k = 4:$$$ choose the indices $$$\\{1, 3, 4, 5\\}$$$$$$\\underline{1}$$$ $$$0$$$ $$$\\underline{1}$$$ $$$\\underline{0}$$$ $$$\\underline{0}$$$ $$$\\rightarrow $$$ $$$\\underline{0}$$$ $$$0$$$ $$$\\underline{0}$$$ $$$\\underline{1}$$$ $$$\\underline{1}$$$ In the third test case, we can perform the following operation: $$$k = 3:$$$ choose the indices $$$\\{3, 5, 6\\}$$$$$$0$$$ $$$0$$$ $$$\\underline{1}$$$ $$$0$$$ $$$\\underline{0}$$$ $$$\\underline{0}$$$ $$$\\rightarrow $$$ $$$0$$$ $$$0$$$ $$$\\underline{0}$$$ $$$0$$$ $$$\\underline{0}$$$ $$$\\underline{1}$$$"}, "positive_code": [{"source_code": "def swap(a)\r\n str = a.join\r\n i, j = -1, a.size\r\n rec = []\r\n\r\n while true\r\n i = str.index(\"1\", i + 1) || a.size\r\n j = str.rindex(\"0\", j - 1) || -1\r\n break if i >= j\r\n\r\n rec << i << j\r\n a[i], a[j] = a[j], a[i]\r\n end\r\n\r\n rec.sort!\r\nend\r\n\r\ngets.to_i.times do\r\n\r\nn = gets.to_i\r\na = gets.chomp.chars.map!(&:to_i)\r\n\r\nans = []\r\nuntil (rec = swap(a)).empty?\r\n ans << rec\r\nend\r\n\r\nputs ans.size\r\nans.each do |a|\r\n a.map!(&:succ)\r\n a.unshift(a.size)\r\n puts a * \" \"\r\nend\r\n\r\nend\r\n"}], "negative_code": [], "src_uid": "f472f9df8b4d588c67b39df6865507ca"} {"nl": {"description": "A string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the English alphabet, is given.You must choose some number $$$k$$$ between $$$0$$$ and $$$n$$$. Then, you select $$$k$$$ characters of $$$s$$$ and permute them however you want. In this process, the positions of the other $$$n-k$$$ characters remain unchanged. You have to perform this operation exactly once.For example, if $$$s=\\texttt{\"andrea\"}$$$, you can choose the $$$k=4$$$ characters $$$\\texttt{\"a_d_ea\"}$$$ and permute them into $$$\\texttt{\"d_e_aa\"}$$$ so that after the operation the string becomes $$$\\texttt{\"dneraa\"}$$$.Determine the minimum $$$k$$$ so that it is possible to sort $$$s$$$ alphabetically (that is, after the operation its characters appear in alphabetical order).", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$1 \\le n \\le 40$$$) — the length of the string. The second line of each test case contains the string $$$s$$$. It is guaranteed that $$$s$$$ contains only lowercase letters of the English alphabet.", "output_spec": "For each test case, output the minimum $$$k$$$ that allows you to obtain a string sorted alphabetically, through the operation described above.", "sample_inputs": ["4\n3\nlol\n10\ncodeforces\n5\naaaaa\n4\ndcba"], "sample_outputs": ["2\n6\n0\n4"], "notes": "NoteIn the first test case, we can choose the $$$k=2$$$ characters $$$\\texttt{\"_ol\"}$$$ and rearrange them as $$$\\texttt{\"_lo\"}$$$ (so the resulting string is $$$\\texttt{\"llo\"}$$$). It is not possible to sort the string choosing strictly less than $$$2$$$ characters.In the second test case, one possible way to sort $$$s$$$ is to consider the $$$k=6$$$ characters $$$\\texttt{\"_o__force_\"}$$$ and rearrange them as $$$\\texttt{\"_c__efoor_\"}$$$ (so the resulting string is $$$\\texttt{\"ccdeefoors\"}$$$). One can show that it is not possible to sort the string choosing strictly less than $$$6$$$ characters.In the third test case, string $$$s$$$ is already sorted (so we can choose $$$k=0$$$ characters).In the fourth test case, we can choose all $$$k=4$$$ characters $$$\\texttt{\"dcba\"}$$$ and reverse the whole string (so the resulting string is $$$\\texttt{\"abcd\"}$$$)."}, "positive_code": [{"source_code": "MOD = 10 ** 9 + 7\ndef gs; gets.chomp; end\ndef gi; gets.to_i; end\ndef gli; gets.split.map(&:to_i); end\ndef cyesno(x); puts x ? :Yes : :No; end\ndef uyesno(x); puts x ? :YES : :NO; end\ndef array_to_hash_by_count(c); Hash.new(0).tap { |x| c.each { |z| x[z] += 1 } }; end\n\ndef main\n t = gi\n t.times do\n n = gi\n s = gs.split('')\n sd = s.sort\n d = 0\n for i in (0..n - 1) do\n d += 1 unless s[i] == sd[i]\n end\n puts d\n end\nend\n\nmain\n"}], "negative_code": [], "src_uid": "58ee86d4913787582ccdb54073656dc0"} {"nl": {"description": "Now Serval is a junior high school student in Japari Middle School, and he is still thrilled on math as before. As a talented boy in mathematics, he likes to play with numbers. This time, he wants to play with numbers on a rooted tree.A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. A parent of a node $$$v$$$ is the last different from $$$v$$$ vertex on the path from the root to the vertex $$$v$$$. Children of vertex $$$v$$$ are all nodes for which $$$v$$$ is the parent. A vertex is a leaf if it has no children.The rooted tree Serval owns has $$$n$$$ nodes, node $$$1$$$ is the root. Serval will write some numbers into all nodes of the tree. However, there are some restrictions. Each of the nodes except leaves has an operation $$$\\max$$$ or $$$\\min$$$ written in it, indicating that the number in this node should be equal to the maximum or minimum of all the numbers in its sons, respectively. Assume that there are $$$k$$$ leaves in the tree. Serval wants to put integers $$$1, 2, \\ldots, k$$$ to the $$$k$$$ leaves (each number should be used exactly once). He loves large numbers, so he wants to maximize the number in the root. As his best friend, can you help him?", "input_spec": "The first line contains an integer $$$n$$$ ($$$2 \\leq n \\leq 3\\cdot 10^5$$$), the size of the tree. The second line contains $$$n$$$ integers, the $$$i$$$-th of them represents the operation in the node $$$i$$$. $$$0$$$ represents $$$\\min$$$ and $$$1$$$ represents $$$\\max$$$. If the node is a leaf, there is still a number of $$$0$$$ or $$$1$$$, but you can ignore it. The third line contains $$$n-1$$$ integers $$$f_2, f_3, \\ldots, f_n$$$ ($$$1 \\leq f_i \\leq i-1$$$), where $$$f_i$$$ represents the parent of the node $$$i$$$.", "output_spec": "Output one integer — the maximum possible number in the root of the tree.", "sample_inputs": ["6\n1 0 1 1 0 1\n1 2 2 2 2", "5\n1 0 1 0 1\n1 1 1 1", "8\n1 0 0 1 0 1 1 0\n1 1 2 2 3 3 3", "9\n1 1 0 0 1 0 1 0 1\n1 1 2 2 3 3 4 4"], "sample_outputs": ["1", "4", "4", "5"], "notes": "NotePictures below explain the examples. The numbers written in the middle of the nodes are their indices, and the numbers written on the top are the numbers written in the nodes.In the first example, no matter how you arrange the numbers, the answer is $$$1$$$. In the second example, no matter how you arrange the numbers, the answer is $$$4$$$. In the third example, one of the best solution to achieve $$$4$$$ is to arrange $$$4$$$ and $$$5$$$ to nodes $$$4$$$ and $$$5$$$. In the fourth example, the best solution is to arrange $$$5$$$ to node $$$5$$$. "}, "positive_code": [{"source_code": "n = gets.to_i\nop = [-1] + gets.split.map(&:to_i)\nf = [-1,-1] + gets.split.map(&:to_i)\n\ng = Array.new(n+1){[]}\n2.upto(n) do |i|\n g[f[i]] << i\nend\n\nrank = [[1]]\nflg = true\nwhile flg\n tmp = []\n rank[-1].each do |k|\n g[k].each do |v|\n tmp << v\n end\n end\n if tmp.empty?\n flg = false\n else\n rank << tmp\n end\nend\n\nans = []*(n+1) #rank of node-i value in leafs which are descendat of node-1\nleaf = 0\nrank.length.times do |i|\n rank[-i-1].each do |v|\n if g[v].empty?\n ans[v] = 1\n leaf += 1\n else\n if op[v] == 0\n ans[v] = 0\n g[v].each do |k| \n ans[v] += ans[k]\n end\n else\n min = 10**6\n g[v].each do |k| \n min = ans[k] if ans[k] < min\n end\n ans[v] = min\n end\n end\n end\nend\n\np leaf + 1 - ans[1]"}], "negative_code": [{"source_code": "n = gets.to_i\nop = [-1] + gets.split.map(&:to_i)\nf = [-1,-1] + gets.split.map(&:to_i)\n\ng = Array.new(n+1){[]}\n2.upto(n) do |i|\n g[f[i]] << i\nend\n#p g\n\nrank = [[1]]\nflg = true\nwhile flg\n tmp = []\n rank[-1].each do |k|\n g[k].each do |v|\n tmp << v\n end\n end\n if tmp.empty?\n flg = false\n else\n rank << tmp\n end\nend\n#p rank\n\ncnt = [0]*(n+1)\nans = []*(n+1)\nleaf = 0\nrank.length.times do |i|\n rank[-i-1].each do |v|\n if g[v].empty?\n ans[v] = -1\n cnt[f[v]] += 1\n leaf += 1\n else\n if op[v] == 0\n ans[v] = -cnt[v]\n else\n max = -10**6\n g[v].each do |k| \n max = ans[k] if ans[k] > max\n end\n ans[v] = max\n end\n cnt[f[v]] += cnt[v]+1\n end\n end\nend\n#p ans\n\np ans[1] + leaf + 1"}], "src_uid": "3b6814b6753f307ec6f3a68179274caa"} {"nl": {"description": "There are n cities situated along the main road of Berland. Cities are represented by their coordinates — integer numbers a1, a2, ..., an. All coordinates are pairwise distinct.It is possible to get from one city to another only by bus. But all buses and roads are very old, so the Minister of Transport decided to build a new bus route. The Minister doesn't want to spend large amounts of money — he wants to choose two cities in such a way that the distance between them is minimal possible. The distance between two cities is equal to the absolute value of the difference between their coordinates.It is possible that there are multiple pairs of cities with minimal possible distance, so the Minister wants to know the quantity of such pairs. Your task is to write a program that will calculate the minimal possible distance between two pairs of cities and the quantity of pairs which have this distance.", "input_spec": "The first line contains one integer number n (2 ≤ n ≤ 2·105). The second line contains n integer numbers a1, a2, ..., an ( - 109 ≤ ai ≤ 109). All numbers ai are pairwise distinct.", "output_spec": "Print two integer numbers — the minimal distance and the quantity of pairs with this distance.", "sample_inputs": ["4\n6 -3 0 4", "3\n-2 0 2"], "sample_outputs": ["2 1", "2 2"], "notes": "NoteIn the first example the distance between the first city and the fourth city is |4 - 6| = 2, and it is the only pair with this distance."}, "positive_code": [{"source_code": "n = gets.chomp.to_i\ncities = gets.chomp.split().map { |e| e.to_i }\ndistances = []\ncities.sort!\nfor i in 0 ... (n - 1)\n distances << (cities[i] - cities[i + 1]).abs\nend\nminDis = distances.min\nrepeat = distances.count(minDis)\nprint \"#{minDis} #{repeat}\\n\"\n"}, {"source_code": "#!/usr/bin/ruby\nn,*a=$<.read.split.map(&:to_i)\nb=a.sort.each_cons(2).map{|x,y|y-x}\nm=b.min\nputs '%d %d'%[m,b.count(m)]"}, {"source_code": "n=gets.to_i\na=gets.split.map{ |x| x.to_i }\na.sort!\nmin_distance=2*10**9+1\nmin_count=0\nfor i in 1...a.length\n\tif a[i]-a[i-1] < min_distance\n\t\tmin_distance=a[i]-a[i-1]\n\t\tmin_count=1\n\telsif a[i]-a[i-1] == min_distance\n\t\tmin_count+=1\n\tend\nend\nputs \"#{min_distance} #{min_count}\"\n"}, {"source_code": "n = gets.strip.to_i\ndata = gets.split.map(&:to_i).sort\n\ndis = (0..n-2).map { |i| data[i+1] - data[i] }\nans = dis.min\nprint \"#{ans} #{dis.select { |v| v == ans }.length}\\n\"\n"}, {"source_code": "n=gets.to_i\naa=gets.split.map(&:to_i).sort\naa=aa[1, n-1].zip(aa).map { |a, b| a-b }\nm=aa.min\nprint m, ' ', aa.count(m)\n"}, {"source_code": "gets\npos = gets.split.map{|x| x.to_i}.sort\n\nfor idx in (0...pos.length-1) do\n pos[idx] = pos[idx+1]-pos[idx]\nend\n\npos.pop\n\nm = pos.min\nputs \"#{m} #{pos.count(m)}\"\n"}], "negative_code": [{"source_code": "n=3\naa=gets.split.map(&:to_i).sort\naa=aa[1, n-1].zip(aa).map { |a, b| a-b }\nm=aa.min\nprint m, ' ', aa.count(m)\n"}], "src_uid": "5f89678ae1deb1d9eaafaab55a64197f"} {"nl": {"description": "Vasya, like many others, likes to participate in a variety of sweepstakes and lotteries. Now he collects wrappings from a famous chocolate bar \"Jupiter\". According to the sweepstake rules, each wrapping has an integer written on it — the number of points that the participant adds to his score as he buys the bar. After a participant earns a certain number of points, he can come to the prize distribution center and exchange the points for prizes. When somebody takes a prize, the prize's cost is simply subtracted from the number of his points.Vasya didn't only bought the bars, he also kept a record of how many points each wrapping cost. Also, he remembers that he always stucks to the greedy strategy — as soon as he could take at least one prize, he went to the prize distribution centre and exchanged the points for prizes. Moreover, if he could choose between multiple prizes, he chose the most expensive one. If after an exchange Vasya had enough points left to get at least one more prize, then he continued to exchange points.The sweepstake has the following prizes (the prizes are sorted by increasing of their cost): a mug (costs a points), a towel (costs b points), a bag (costs c points), a bicycle (costs d points), a car (costs e points). Now Vasya wants to recollect what prizes he has received. You know sequence p1, p2, ..., pn, where pi is the number of points Vasya got for the i-th bar. The sequence of points is given in the chronological order. You also know numbers a, b, c, d, e. Your task is to find, how many prizes Vasya received, what prizes they are and how many points he's got left after all operations are completed.", "input_spec": "The first line contains a single integer n (1 ≤ n ≤ 50) — the number of chocolate bar wrappings that brought points to Vasya. The second line contains space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ 109). The third line contains 5 integers a, b, c, d, e (1 ≤ a < b < c < d < e ≤ 109) — the prizes' costs.", "output_spec": "Print on the first line 5 integers, separated by a space — the number of mugs, towels, bags, bicycles and cars that Vasya has got, respectively. On the second line print a single integer — the number of points Vasya will have left after all operations of exchange are completed. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.", "sample_inputs": ["3\n3 10 4\n2 4 10 15 20", "4\n10 4 39 2\n3 5 10 11 12"], "sample_outputs": ["1 1 1 0 0 \n1", "3 0 1 0 3 \n0"], "notes": "NoteIn the first sample Vasya gets 3 points after eating the first chocolate bar. Then he exchanges 2 points and gets a mug. Vasya wins a bag after eating the second chocolate bar. Then he wins a towel after eating the third chocolate bar. After all chocolate bars 3 - 2 + 10 - 10 + 4 - 4 = 1 points remains."}, "positive_code": [{"source_code": "def koukan()\n return -1 if $sum < $aa[0]\n 5.downto(1) { |i|\n return i-1 if $aa[i-1] <= $sum\n }\nend\n\n$n = gets.chomp.to_i\n$sa = gets.chomp.split(/ /).map!{|x| x.to_i}\n$aa = gets.chomp.split(/ /).map!{|x| x.to_i}\n$sum = 0\n$suma = Array.new(5).map!{|x| x = 0}\nfor i in 0..$n-1\n $sum += $sa[i]\n while (true)\n if $sum > $aa[4]\n wka = $sum.divmod($aa[4])\n $suma[4] += wka[0]\n $sum -= $aa[4] * wka[0]\n end\n ret = koukan()\n break if ret < 0\n $suma[ret] += 1\n $sum -= $aa[ret]\n end\nend\nputs $suma.join(\" \")\nputs $sum\n"}, {"source_code": "gets\nb=gets.split\nz=gets.split.map &:to_i\ns=0\nr=z.map{0}\nb.map{|c|s+=c.to_i\nwhile(t=z.select{|i|i<=s})[0]\nr[z.index m=t.max]+=s/m\ns%=m\nend}\nputs r*\" \",s"}, {"source_code": "gets\nbats = gets.split\nprizes = gets.split.map &:to_i\nscore = 0\nres = prizes.map{0}\nfor bat in bats\n score += bat.to_i\n loop do\n can = prizes.select{ |e| e <= score }\n break if can.empty?\n x = can.max\n res[prizes.index x] += score / x\n score %= x\n end\nend\n\nputs res.join \" \"\nputs score\n"}, {"source_code": "gets\nbats = gets.split\nprizes = gets.split.map.with_index{|e,i|[e.to_i,i]}\nscore = 0\nres = prizes.map{0}\nfor bat in bats\n score += bat.to_i\n loop do\n can = prizes.select{ |e,i| e <= score }\n break if can.empty?\n x = can.max_by(&:first)\n res[x.last] += score / x.first\n score %= x.first\n end\nend\n\nputs res.join \" \"\nputs score"}, {"source_code": "gets\nb=gets.split\nz=gets.split.map &:to_i\ns=0\nr=z.map{0}\nb.map{|c|s+=c.to_i\nwhile(t=z.select{|i|i<=s})[0]\nr[z.index m=t.max]+=s/m\ns%=m\nend}\nputs r*\" \",s\n"}, {"source_code": "gets\nb=gets.split\nz=gets.split.map &:to_i\ns=0\nr=z.map{0}\nb.map{|c|s+=c.to_i\nwhile(t=z.select{|i|i<=s})[0]\nr[z.index m=t.max]+=s/m\ns%=m\nend}\nputs r*\" \",s\n"}, {"source_code": "n = gets.to_i\np = gets.split.map(&:to_i)\nst = gets.split.map(&:to_i)\nres = Array.new(5,0)\n\nr = p.inject(0) { |acc, x| t = acc + x; (0..4).reverse_each { |j| res[j] += t / st[j]; t %= st[j] }; t }\nputs res*' '\nputs r"}, {"source_code": "gets\nb=gets.split\nz=gets.split.map &:to_i\ns=0\nr=z.map{0}\nb.map{|c|s+=c.to_i\nwhile(t=z.select{|i|i<=s})[0]\nr[z.index m=t.max]+=s/m\ns%=m\nend}\nputs r*\" \",s\n"}, {"source_code": "gets\nb=gets.split\nz=gets.split.map &:to_i\ns=0\nr=z.map{0}\nb.map{|c|s+=c.to_i\nwhile(t=z.select{|i|i<=s})[0]\nr[z.index m=t.max]+=s/m\ns%=m\nend}\nputs r*\" \",s\n"}, {"source_code": "n = gets.to_i\np = gets.split.map{|e| e.to_i }\ncost = gets.split.map{|e| e.to_i }\ncount = Array.new(5){0}\nremain = 0\ncost.reverse!\np.each do |i|\n remain += i\n 5.times do |j|\n if remain >= cost[j]\n tmp = (remain / cost[j]).truncate\n remain -= cost[j] * tmp\n count[4-j] += tmp\n end\n end\nend\nputs \"#{count[0]} #{count[1]} #{count[2]} #{count[3]} #{count[4]}\"\nputs remain"}, {"source_code": "n = gets.to_i\na = gets.split.map { |s| s.to_i }\nb = gets.split.map { |s| s.to_i }\ns, r, i = 0, b.map {0}\nn.times { |i|\n s = s + a[i] if i < n \n loop {\n c = b.select { |p| s >= p } \n break if c.empty?\n m = c.max\n r[b.index(m)] += (s / m) \n s = s - ((s / m) * m)\n }\n i = i + 1\n}\nputs r.join(\" \")\nputs s\n"}, {"source_code": "n = gets.to_i\na = gets.split.map { |s| s.to_i }\nb = gets.split.map { |s| s.to_i }\ns, r, i = 0, b.map {0}\nn.times { |i|\n s = s + a[i] if i < n \n loop {\n m, o = 0, -1 \n 5.times { |j|\n if s >= b[j]\n m = b[j]\n o = j\n end\n }\n break if o == -1\n r[o] = r[o] + (s / m) \n s = s - ((s / m) * m)\n }\n i = i + 1\n}\nputs r.join(\" \")\nputs s\n"}, {"source_code": "#!/usr/bin/ruby\nn = gets.chomp.to_i\np = gets.chomp.split(' ').map{|x| x.to_i}\na,b,c,d,e = gets.chomp.split(' ').map{|x| x.to_i}\nna = 0\nnb = 0\nnc = 0\nnd = 0\nne = 0\ncurr = 0\nfor i in 0..p.size-1\n\tcurr += p[i]\n\twhile curr >= a\n\t\tif curr >= e\n\t\t\tne += curr/e\n\t\t\tcurr=curr%e\n\t\telsif curr >= d\n\t\t\tnd +=curr/d\n\t\t\tcurr =curr%d\n\t\telsif curr >= c\n\t\t\tnc += curr/c\n\t\t\tcurr = curr%c\n\t\telsif curr >= b \n\t\t\tnb += curr/b\n\t\t\tcurr =curr%b\n\t\telsif curr >= a\n\t\t\tna +=curr/a\n\t\t\tcurr =curr%a\n\t\tend\n\tend\nend\nprint \"#{na} #{nb} #{nc} #{nd} #{ne}\\n\"\nputs curr\n"}, {"source_code": "gets\nb=gets.split\nz=gets.split.map &:to_i\ns=0\nr=z.map{0}\nb.map{|c|s+=c.to_i\nwhile(t=z.select{|i|i<=s})[0]\nr[z.index m=t.max]+=s/m\ns%=m\nend}\nputs r*\" \",s"}, {"source_code": "gets\nb=gets.split\nz=gets.split.map &:to_i\ns=0\nr=z.map{0}\nb.map{|c|s+=c.to_i\nwhile(t=z.select{|i|i<=s})[0]\nr[z.index m=t.max]+=s/m\ns%=m\nend}\nputs r*\" \",s\n"}, {"source_code": "require \"scanf\"\nn = gets.to_i\nlist = []\ngets.split(\" \").each {\n |i| list.push(i.to_i)\n}\ncost = scanf(\"%d%d%d%d%d\")\ncnt = [0,0,0,0,0]\ntot = 0\nlist.each { |i| \n tot += i\n 5.times { |j|\n cnt[4 - j] += tot / cost[4 - j]\n tot %= cost[4 - j]\n }\n}\ncnt.each{ |i| print i, \" \"}\nprint \"\\n\", tot\n"}, {"source_code": "n = gets.to_i\np = gets.split.collect{|i| i.to_i}\nc = gets.split.collect{|i| i.to_i}\nans = Array.new(5,0)\npoints = 0\np.each do |pp|\n points += pp\n 4.downto(0).each do |i|\n ans[i] += points/c[i]\n points = points%c[i]\n end\nend\nputs ans.join(\" \")\nputs points\n"}, {"source_code": "n,=gets.split.map{|e| e.to_i}\na=gets.split.map{|e| e.to_i}\nb=gets.split.map{|e| e.to_i}\nc=Array.new(5,0)\ni=0\nk=0\nwhile i 0\n\t\t\tprint \" \"\n\t\tend\n\t\tprint ans[i]\n\tend\n\tputs\n\tputs now\nend\n"}, {"source_code": "gets\nb=gets.split\nz=gets.split.map &:to_i\ns=0\nr=z.map{0}\nb.map{|c|s+=c.to_i\nwhile(t=z.select{|i|i<=s})[0]\nr[z.index m=t.max]+=s/m\ns%=m\nend}\nputs r*\" \",s\n"}, {"source_code": "gets\nb=gets.split\nz=gets.split.map &:to_i\ns=0\nr=z.map{0}\nb.map{|c|s+=c.to_i\nwhile(t=z.select{|i|i<=s})[0]\nr[z.index m=t.max]+=s/m\ns%=m\nend}\nputs r*\" \",s\n"}], "negative_code": [{"source_code": "gets\nbats = gets.split.map &:to_i\nprizes = gets.split.map &:to_i\nscore = 0\nres = prizes.map{0}\nfor bat in bats\n score += bat\n\n can = prizes.select{ |prize| prize <= score }\n break if can.empty?\n res[prizes.index(can.max)] += score / can.max\n score %= can.max\n \nend\nputs res.join \" \"\nputs score"}, {"source_code": "#!/usr/bin/ruby\nn = gets.chomp.to_i\np = gets.chomp.split(' ').map{|x| x.to_i}\na,b,c,d,e = gets.chomp.split(' ').map{|x| x.to_i}\nna = 0\nnb = 0\nnc = 0\nnd = 0\nne = 0\ncurr = 0\nfor i in 0..p.size-1\n\tcurr += p[i]\n\twhile curr >= a\n\t\tif curr >= e\n\t\t\tcurr-=e\n\t\t\tne += 1\n\t\telsif curr >= d\n\t\t\tcurr -= d\n\t\t\tnd +=1\n\t\telsif curr >= c\n\t\t\tcurr -= c\n\t\t\tnc += 1\n\t\telsif curr >= b \n\t\t\tcurr -= b\n\t\t\tnb += 1\n\t\telsif curr >= a\n\t\t\tcurr -=a\n\t\t\tna +=1\n\t\tend\n\tend\nend\nprint \"#{na} #{nb} #{nc} #{nd} #{ne}\\n\"\n"}, {"source_code": "require \"scanf\"\nn = gets.to_i\nlist = []\ngets.split(\" \").each {\n |i| list.push(i.to_i)\n}\ncost = scanf(\"%d%d%d%d%d\")\ncnt = [0,0,0,0,0]\ntot = 0\nlist.each { |i| \n tot += i\n 5.times { |j|\n p j\n while tot >= cost[4 - j] \n cnt[4 - j] += 1\n tot -= cost[4 - j]\n end\n }\n}\ncnt.each{ |i| \n print i , \" \"}\nprint \"\\n\",tot\n"}, {"source_code": "n,=gets.split.map{|e| e.to_i}\na=gets.split.map{|e| e.to_i}\nb=gets.split.map{|e| e.to_i}\nc=Array.new(5,0)\ni=0\nk=0\nwhile ik then break end\n end\n j-=1\n if j>=0 then c[j]+=1; k-=b[j] end\n i+=1\nend\nfor i in 0..4 do\n print c[i],' '\nend \nputs \"\\n\", k"}, {"source_code": "def read_next_line\n gets.split.map(&:to_i)\nend\n\ngets\np = read_next_line\na = read_next_line\n\nt = Array.new(5, 0)\ns = 0\np.each { |x|\n s += x\n t[4] += s / a[4]\n s %= a[4]\n 3.downto(0).each { |i|\n if a[i] <= s\n s -= a[i]\n t[i] += 1\n end\n }\n}\n\nputs t * ' '\nputs s"}], "src_uid": "1ae2942b72ebb7c55359c41e141900d7"} {"nl": {"description": "NN is an experienced internet user and that means he spends a lot of time on the social media. Once he found the following image on the Net, which asked him to compare the sizes of inner circles: It turned out that the circles are equal. NN was very surprised by this fact, so he decided to create a similar picture himself.He managed to calculate the number of outer circles $$$n$$$ and the radius of the inner circle $$$r$$$. NN thinks that, using this information, you can exactly determine the radius of the outer circles $$$R$$$ so that the inner circle touches all of the outer ones externally and each pair of neighboring outer circles also touches each other. While NN tried very hard to guess the required radius, he didn't manage to do that. Help NN find the required radius for building the required picture.", "input_spec": "The first and the only line of the input file contains two numbers $$$n$$$ and $$$r$$$ ($$$3 \\leq n \\leq 100$$$, $$$1 \\leq r \\leq 100$$$) — the number of the outer circles and the radius of the inner circle respectively.", "output_spec": "Output a single number $$$R$$$ — the radius of the outer circle required for building the required picture. Your answer will be accepted if its relative or absolute error does not exceed $$$10^{-6}$$$. Formally, if your answer is $$$a$$$ and the jury's answer is $$$b$$$. Your answer is accepted if and only when $$$\\frac{|a-b|}{max(1, |b|)} \\le 10^{-6}$$$.", "sample_inputs": ["3 1", "6 1", "100 100"], "sample_outputs": ["6.4641016", "1.0000000", "3.2429391"], "notes": null}, "positive_code": [{"source_code": "# Input\nn, r = gets.split(\" \").map { |e| e.to_f }\n\n# Processing\nalpha = 2 * Math::PI / n\nv = Math.sin(alpha / 2)\nresult = r * v / (1 - v)\n\n# Output\nputs result\n"}, {"source_code": "n, r = gets.split.map(&:to_f)\nl = 0.0\nrt = 10e5\neps = 10e-8\nwhile (rt - l > eps)\n m = (rt - l) / 2 + l # радиус r\n rr = m + r\n d = Math.acos((rr ** 2 + rr ** 2 - (m * 2) ** 2)/(2 * rr * rr))\n if (d / Math::PI * 180 > (360.0 / n))\n rt = m\n else\n l = m\n end\nend\nputs rt"}, {"source_code": "n,r = gets.split.map(&:to_f)\ns = Math.sin(Math::PI/n)\nputs r*s/(1-s)"}], "negative_code": [], "src_uid": "e27cff5d681217460d5238bf7ef6a876"} {"nl": {"description": "We all know the problem about the number of ways one can tile a 2 × n field by 1 × 2 dominoes. You probably remember that it goes down to Fibonacci numbers. We will talk about some other problem below, there you also are going to deal with tiling a rectangular field with dominoes.You are given a 4 × n rectangular field, that is the field that contains four lines and n columns. You have to find for it any tiling by 1 × 2 dominoes such that each of the n - 1 potential vertical cuts along the grid lines intersects at least one domino, splitting it in two. No two dominoes in the sought tiling should overlap, each square of the field should be covered by exactly one domino. It is allowed to rotate the dominoes, that is, you can use 2 × 1 as well as 1 × 2 dominoes.Write a program that finds an arbitrary sought tiling. ", "input_spec": "The input contains one positive integer n (1 ≤ n ≤ 100) — the number of the field's columns.", "output_spec": "If there's no solution, print \"-1\" (without the quotes). Otherwise, print four lines containing n characters each — that's the description of tiling, where each vertical cut intersects at least one domino. You should print the tiling, having painted the field in no more than 26 colors. Each domino should be painted a color. Different dominoes can be painted the same color, but dominoes of the same color should not be side-neighbouring. To indicate colors you should use lowercase Latin letters. Print any of the acceptable ways of tiling.", "sample_inputs": ["4"], "sample_outputs": ["yyzz\nbccd\nbxxd\nyyaa"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\nt = (n / 2).times.map{|i| i.odd? ? '00' : '11'} * ''\nif n.odd?\n\ts = [\n\t\t'i' + t.tr('01', 'ab'),\n\t\t'i' + t.tr('01', 'cd'),\n\t\tt.tr('01', 'ef') + 'j',\n\t\tt.tr('01', 'gh') + 'j']\nelse\n\ts = [\n\t\tt.tr('01', 'ab'),\n\t\tt.tr('01', 'cd'),\n\t\t'i' + t[2..-1].tr('01', 'ef') + 'j',\n\t\t'i' + t[2..-1].tr('01', 'gh') + 'j']\nend\nputs s * \"\\n\"\n"}, {"source_code": "N = gets.to_i\n\n#01234567\n#aaffaaff\n#bbggbbgg\n#cddhhcchh\n#ceeiiddii\n\nstr = []\nstr[0] = \"aaff\" * 25\nstr[1] = \"bbgg\" * 25\nstr[2] = \"cdd\" + \"hhcc\" * 25\nstr[3] = \"cee\" + \"iidd\" * 25\n\nif N == 1\n print \"a\\na\\nb\\nb\\n\"\nelsif N == 2\n puts \"aa\"\n puts \"bb\"\n puts \"cd\"\n puts \"cd\"\nelse\n# p str\n if (N % 2 == 1)\n str[1][N - 1] = str[0][N - 1]\n else\n str[3][N - 1] = str[2][N - 1]\n end\n 4.times do |i|\n puts str[i][0..(N - 1)]\n end\nend\n\n"}, {"source_code": "n = gets.to_i\n\ndef dig2str(dig)\n p = 'a'\n (dig%26).times { p = p.succ }\n p\nend\n\ndef solve(n)\n return ['a', 'a', 'b', 'b'] if n == 1\n return ['aa', 'bb', 'cc', 'dd'] if n == 2\n a = Array.new(4) { Array.new(n) }\n dig = 0\n 0.step(n-2, 2) do |i|\n a[0][i] = a[0][i+1] = dig2str(dig)\n dig += 1\n a[1][i] = a[1][i+1] = dig2str(dig)\n dig += 1\n end\n dig = 10\n 1.step(n-2, 2) do |i|\n a[2][i] = a[2][i+1] = dig2str(dig)\n dig += 1\n a[3][i] = a[3][i+1] = dig2str(dig)\n dig += 1\n end\n if n%2==0\n a[2][-1] = a[3][-1] = dig2str(dig)\n else\n a[0][-1] = a[1][-1] = dig2str(dig)\n end\n a[2][0] = a[3][0] = dig2str(9)\n return a.map(&:join)\nend\n\nputs solve(n)\n\n"}, {"source_code": "def build_line(s1, s2, len)\n (1..len).map { |i| i.odd? ? s1 : s2 }.join ''\nend\n\ndef build_odd(n)\n line1 = build_line('aa', 'bb', (n - 1) / 2)\n line2 = build_line('cc', 'dd', (n - 1) / 2)\n puts line1 + 'x'\n puts line2 + 'x'\n puts 'y' + line1\n puts 'y' + line2\nend\n\ndef build_even(n)\n line1 = build_line('aa', 'bb', n / 2)\n puts line1\n puts 'x' + build_line('cc', 'dd', n / 2 - 1) + 'y'\n puts 'x' + build_line('ee', 'ff', n / 2 - 1) + 'y'\n puts line1\nend\n\nn = gets.to_i\nif n.odd?\n build_odd(n)\nelse\n build_even(n)\nend"}], "negative_code": [{"source_code": "N = gets.to_i\n\n#01234567\n#aaffaaff\n#bbggbbgg\n#cddhhcchh\n#ceeiiddii\n\nstr = []\nstr[0] = \"aaff\" * 25\nstr[1] = \"bbgg\" * 25\nstr[2] = \"cdd\" + \"hhcc\" * 25\nstr[3] = \"cee\" + \"iidd\" * 25\n\nif N == 1\n print \"a\\na\\nb\\nb\\n\"\nelsif N == 2\n puts \"aa\"\n puts \"bb\"\n puts \"cd\"\n puts \"cd\"\nelse\n p str\n if (N % 2 == 1)\n str[1][N - 1] = str[0][N - 1]\n else\n str[3][N - 1] = str[2][N - 1]\n end\n 4.times do |i|\n puts str[i][0..(N - 1)]\n end\nend\n\n"}], "src_uid": "42a51b45312be6b38f3a15949d315c67"} {"nl": {"description": "The Little Elephant loves to play with color cards.He has n cards, each has exactly two colors (the color of the front side and the color of the back side). Initially, all the cards lay on the table with the front side up. In one move the Little Elephant can turn any card to the other side. The Little Elephant thinks that a set of cards on the table is funny if at least half of the cards have the same color (for each card the color of the upper side is considered).Help the Little Elephant to find the minimum number of moves needed to make the set of n cards funny.", "input_spec": "The first line contains a single integer n (1 ≤ n ≤ 105) — the number of the cards. The following n lines contain the description of all cards, one card per line. The cards are described by a pair of positive integers not exceeding 109 — colors of both sides. The first number in a line is the color of the front of the card, the second one — of the back. The color of the front of the card may coincide with the color of the back of the card. The numbers in the lines are separated by single spaces.", "output_spec": "On a single line print a single integer — the sought minimum number of moves. If it is impossible to make the set funny, print -1.", "sample_inputs": ["3\n4 7\n4 7\n7 4", "5\n4 7\n7 4\n2 11\n9 7\n1 1"], "sample_outputs": ["0", "2"], "notes": "NoteIn the first sample there initially are three cards lying with colors 4, 4, 7. Since two of the three cards are of the same color 4, you do not need to change anything, so the answer is 0.In the second sample, you can turn the first and the fourth cards. After that three of the five cards will be of color 7."}, "positive_code": [{"source_code": "s=(gets.to_i+1)/2\na=gets(p).scan /(\\S+) (.+)/\nx=a.flatten.sort.each_slice([a.size/2,1].max).map(&:first).select{|i|a.count{|j,k|j==i||k==i}>=s}\np x[0]?[s-x.map{|i|a.map(&:first).count i}.max,0].max: -1\n"}, {"source_code": "s=(gets.to_i+1)/2\na=gets(p).scan /(\\S+) (.+)/\nx=a.flatten.sort.each_slice([a.size/2,1].max).map(&:first).select{|i|a.count{|j,k|j==i||k==i}>=s}\np x[0]?[s-x.map{|i|a.map(&:first).count i}.max,0].max: -1\n"}, {"source_code": "s=(gets.to_i+1)/2\na=gets(p).scan /(\\S+) (.+)/\nx=a.flatten.sort.each_slice([a.size/2,1].max).map(&:first).select{|i|a.count{|j,k|j==i||k==i}>=s}\np x[0]?[s-x.map{|i|a.map(&:first).count i}.max,0].max: -1\n"}, {"source_code": "s=(gets.to_i+1)/2\na=gets(p).scan /(\\S+) (.+)/\nx=a.flatten.sort.each_slice([a.size/2,1].max).map(&:first).select{|i|a.count{|j,k|j==i||k==i}>=s}\np x[0]?[s-x.map{|i|a.map(&:first).count i}.max,0].max: -1\n"}, {"source_code": "s=(gets.to_i+1)/2\na=gets(p).scan /(\\S+) (.+)/\nx=a.flatten.sort.each_slice([a.size/2,1].max).map(&:first).select{|i|a.count{|j,k|j==i||k==i}>=s}\np x[0]?[s-x.map{|i|a.map(&:first).count i}.max,0].max: -1\n"}, {"source_code": "s=(gets.to_i+1)/2\na=gets(p).scan /(\\S+) (.+)/\nx=a.flatten.sort.each_slice([a.size/2,1].max).map(&:first).select{|i|a.count{|j,k|j==i||k==i}>=s}\np x[0]?[s-x.map{|i|a.map(&:first).count i}.max,0].max: -1\n"}, {"source_code": "s=(gets.to_i+1)/2\na=gets(p).scan /(\\S+) (.+)/\nx=a.flatten.sort.each_slice([a.size/2,1].max).map(&:first).select{|i|a.count{|j,k|j==i||k==i}>=s}\np x[0]?[s-x.map{|i|a.map(&:first).count i}.max,0].max: -1\n"}, {"source_code": "s=(gets.to_i+1)/2\na=gets(p).scan /(\\S+) (.+)/\nx=a.flatten.sort.each_slice([a.size/2,1].max).map(&:first).select{|i|a.count{|j,k|j==i||k==i}>=s}\np x[0]?[s-x.map{|i|a.map(&:first).count i}.max,0].max: -1\n"}, {"source_code": "s2 = (gets.to_i+1)/2\na = gets(p).scan(/(\\S+) (\\S+)/)\naf,al = a.transpose\n\nas = a.flatten.sort.each_slice([a.size/2,1].max).map(&:first)\nx = as.select{ |i|\n a.count{ |j,k| j == i || k == i } >= s2\n}\n\np x.empty? ? -1 : [(\n a1 = af.group_by{|i|i}\n s2 - x.map{ |i| a1[i] ? a1[i].size : 0 }.max\n #s2 - x.map{ |i| af.count i }.max\n), 0].max"}, {"source_code": "s=(gets.to_i+1)/2\na=gets(p).scan /(\\S+) (.+)/\nx=a.flatten.sort.each_slice([a.size/2,1].max).map(&:first).select{|i|a.count{|j,k|j==i||k==i}>=s}\np x[0]?[s-x.map{|i|a.map(&:first).count i}.max,0].max: -1"}, {"source_code": "s2 = (gets.to_i+1)/2\na = gets(p).scan(/(\\S+) (\\S+)/)\naf,al = a.transpose\n\nas = a.flatten.sort.each_slice([a.size/2,1].max).map(&:first)\nx = as.select{ |i|\n a.count{ |j,k| j == i || k == i } >= s2\n}\n\np x.empty? ? -1 : [(\n a1 = af.group_by{|i|i}\n #s2 - x.map{ |i| a1[i] ? a1[i].size : 0 }.max\n s2 - x.map{ |i| af.count i }.max\n), 0].max"}, {"source_code": "n = gets.to_i \no = (n - 1) / 2 + 1\n@m = 1 << 30\nh, k = {}, {}\ndef f(a, b, p, q) \n a.each_pair { |y, v| \n if b.has_key?(y) and v + b[y] >= p \n @m = q == 0 ? [@m, p - v].min : [@m, p - b[y]].min\n elsif v >= p \n @m = [@m, q].min\n end \n }\nend \nn.times { |x| \n a, b = gets.split.map { |i| i.to_i }\n h[a] = h.has_key?(a) ? h[a] + 1 : 1\n k[b] = k.has_key?(b) ? k[b] + 1 : 1 if a != b\n}\nf(h, k, o, 0)\nf(k, h, o, o)\nputs @m == 1 << 30 ? -1 : [0, @m].max\n\n"}, {"source_code": "n = gets.to_i \no = (n - 1) / 2 + 1\nb = []\nh, k = {}, {}\nn.times { |x| \n b << gets.split.map { |i| i.to_i }\n h[b[x][0]] = h.has_key?(b[x][0]) ? h[b[x][0]] + 1 : 1\n k[b[x][1]] = k.has_key?(b[x][1]) ? k[b[x][1]] + 1 : 1 if b[x][1] != b[x][0]\n}\nm = 1 << 30\nh.each_pair { |y, v| \n if k.has_key?(y) and v + k[y] >= o \n m = [m, o - v].min\n elsif v >= o \n puts 0\n exit\n end \n}\nk.each_pair { |y, v| \n if h.has_key?(y) and v + h[y] >= o \n m = [m, o - h[y]].min\n elsif v >=o \n puts o \n exit\n end\n}\nputs m == 1 << 30 ? -1 : [0, m].max\n\n"}], "negative_code": [{"source_code": "s2 = (gets.to_i+1)/2\na = gets(p).scan(/(\\S+) (\\S+)/)\naf,al = a.transpose\n\nas = a.flatten.sort\nx = [as[0],as[as.size/2]].select{ |i|\n a.count{ |j,k| j == i || k == i } >= s2\n}\n\np x.empty? ? -1 : [(\n a1 = af.group_by{|i|i}\n s2 - x.map{ |i| a1[i] ? a1[i].size : 0 }.max\n #s2 - x.map{ |i| af.count i }.max\n), 0].max"}, {"source_code": "n = gets.to_i \no = (n - 1) / 2 + 1\nb = []\nh, k = {}, {}\nn.times { |x| \n b << gets.split.map { |i| i.to_i }\n h[b[x][0]] = h.has_key?(b[x][0]) ? h[b[x][0]] + 1 : 1\n k[b[x][1]] = k.has_key?(b[x][1]) ? k[b[x][1]] + 1 : 1\n}\nm = 1 << 30\nh.each_pair { |y, v| \n if k.has_key?(y) and v + k[y] >= o \n m = [m, o - v].min\n elsif v >= o \n puts 0\n exit\n end \n}\nk.each_pair { |y, v| \n if h.has_key?(y) and v + h[y] >= o \n m = [m, o - h[y]].min\n elsif v >=o \n puts o \n exit\n end\n}\nputs [0, m].max\n\n"}, {"source_code": "n = gets.to_i \no = (n - 1) / 2 + 1\nb = []\nh, k = {}, {}\nn.times { |x| \n b << gets.split.map { |i| i.to_i }\n h[b[x][0]] = h.has_key?(b[x][0]) ? h[b[x][0]] + 1 : 1\n k[b[x][1]] = k.has_key?(b[x][1]) ? k[b[x][1]] + 1 : 1\n}\nm = 1 << 30\nh.each_pair { |y, v| \n if k.has_key?(y) and v + k[y] >= o \n m = [m, o - v].min\n elsif v >= 0\n puts 0\n exit\n end \n}\nk.each_pair { |y, v| \n if h.has_key?(y) and v + h[y] >= o \n m = [m, o - h[y]].min\n elsif v >=o \n puts 0\n exit\n end\n}\nputs [0, m].max\n\n"}, {"source_code": "n = gets.to_i \no = (n - 1) / 2 + 1\n@m = 1 << 30\nh, k = {}, {}\ndef f(a, b, p, q) \n a.each_pair { |y, v| \n if b.has_key?(y) and v + b[y] >= p \n @m = [@m, p - v].min\n elsif v >= p \n @m = [@m, q].min\n end \n }\nend \nn.times { |x| \n a, b = gets.split.map { |i| i.to_i }\n h[a] = h.has_key?(a) ? h[a] + 1 : 1\n k[b] = k.has_key?(b) ? k[b] + 1 : 1 if a != b\n}\nf(h, k, o, 0)\nf(h, k, o, o)\nputs @m == 1 << 30 ? -1 : [0, @m].max\n\n"}, {"source_code": "n = gets.to_i \no = (n - 1) / 2 + 1\n@m = 1 << 30\nh, k = {}, {}\ndef f(a, b, p, q) \n a.each_pair { |y, v| \n if b.has_key?(y) and v + b[y] >= p \n @m = [@m, p - v].min\n elsif v >= p \n @m = [@m, q].min\n end \n }\nend \nn.times { |x| \n a, b = gets.split.map { |i| i.to_i }\n h[a] = h.has_key?(a) ? h[a] + 1 : 1\n k[b] = k.has_key?(b) ? k[b] + 1 : 1 if a != b\n}\nf(h, k, o, 0)\nf(k, h, o, o)\nputs @m == 1 << 30 ? -1 : [0, @m].max\n\n"}, {"source_code": "n = gets.to_i \no = (n - 1) / 2 + 1\nb = []\nh, k = {}, {}\nn.times { |x| \n b << gets.split.map { |i| i.to_i }\n h[b[x][0]] = h.has_key?(b[x][0]) ? h[b[x][0]] + 1 : 1\n k[b[x][1]] = k.has_key?(b[x][1]) ? k[b[x][1]] + 1 : 1\n}\nm = 1 << 30\nh.each_pair { |y, v| \n if k.has_key?(y) and v + k[y] >= o \n m = [m, o - v].min\n elsif v >= o \n puts 0\n exit\n end \n}\nk.each_pair { |y, v| \n if h.has_key?(y) and v + h[y] >= o \n m = [m, o - h[y]].min\n elsif v >=o \n puts o \n exit\n end\n}\nputs m == 1 << 30 ? -1 : [0, m].max\n\n"}], "src_uid": "5e055bad1da5bdc84599d6f2f89fbd12"} {"nl": {"description": "Once upon a time, when the world was more beautiful, the sun shone brighter, the grass was greener and the sausages tasted better Arlandia was the most powerful country. And its capital was the place where our hero DravDe worked. He couldn’t program or make up problems (in fact, few people saw a computer those days) but he was nevertheless happy. He worked in a warehouse where a magical but non-alcoholic drink Ogudar-Olok was kept. We won’t describe his work in detail and take a better look at a simplified version of the warehouse.The warehouse has one set of shelving. It has n shelves, each of which is divided into m sections. The shelves are numbered from top to bottom starting from 1 and the sections of each shelf are numbered from left to right also starting from 1. Each section can contain exactly one box of the drink, and try as he might, DravDe can never put a box in a section that already has one. In the course of his work DravDe frequently notices that he has to put a box in a filled section. In that case his solution is simple. DravDe ignores that section and looks at the next one to the right. If it is empty, he puts the box there. Otherwise he keeps looking for the first empty section to the right. If no empty section is found by the end of the shelf, he looks at the shelf which is under it, then the next one, etc. Also each time he looks at a new shelf he starts from the shelf’s beginning. If DravDe still can’t find an empty section for the box, he immediately drinks it all up and throws the empty bottles away not to be caught.After one great party with a lot of Ogudar-Olok drunk DravDe asked you to help him. Unlike him, you can program and therefore modeling the process of counting the boxes in the warehouse will be easy work for you.The process of counting contains two types of query messages: «+1 x y id» (where x, y are integers, 1 ≤ x ≤ n, 1 ≤ y ≤ m, and id is a string of lower case Latin letters — from 1 to 10 characters long). That query means that the warehouse got a box identified as id, which should be put in the section y on the shelf x. If the section is full, use the rules described above. It is guaranteed that every moment of the process the identifiers of all the boxes in the warehouse are different. You don’t have to answer this query. «-1 id» (where id is a string of lower case Latin letters — from 1 to 10 characters long). That query means that a box identified as id is removed from the warehouse. You have to answer this query (see output format). ", "input_spec": "The first input line contains integers n, m and k (1 ≤ n, m ≤ 30, 1 ≤ k ≤ 2000) — the height, the width of shelving and the amount of the operations in the warehouse that you need to analyze. In the following k lines the queries are given in the order of appearance in the format described above.", "output_spec": "For each query of the «-1 id» type output two numbers in a separate line — index of the shelf and index of the section where the box with this identifier lay. If there was no such box in the warehouse when the query was made, output «-1 -1» without quotes.", "sample_inputs": ["2 2 9\n+1 1 1 cola\n+1 1 1 fanta\n+1 1 1 sevenup\n+1 1 1 whitekey\n-1 cola\n-1 fanta\n-1 sevenup\n-1 whitekey\n-1 cola", "2 2 8\n+1 1 1 cola\n-1 cola\n+1 1 1 fanta\n-1 fanta\n+1 1 1 sevenup\n-1 sevenup\n+1 1 1 whitekey\n-1 whitekey"], "sample_outputs": ["1 1\n1 2\n2 1\n2 2\n-1 -1", "1 1\n1 1\n1 1\n1 1"], "notes": null}, "positive_code": [{"source_code": "f = open(\"input.txt\", \"r\")\nfw = open(\"output.txt\", \"w\")\nn,m,k = f.gets.chomp.split(/ /).map!{|x|x.to_i}\nsh = Array.new(n)\nsh.map!{|x| Array.new(m) }\nh = Hash.new\nfor i in 0..k-1\n\tar = f.gets.chomp.split(/ /)\n\tif ar[0] == \"+1\"\n\t\tx = ar[1].to_i-1\n\t\ty = ar[2].to_i-1\n\t\twhile true\n\t\t\tif sh[x][y] == nil\n\t\t\t\th[ar[3]] = (x+1).to_s + \" \" + (y+1).to_s\n\t\t\t\tsh[x][y] = ar[3]\n\t\t\t\tbreak\n\t\t\tend\n\t\t\ty += 1\n\t\t\tif y == m\n\t\t\t\tx += 1\n\t\t\t\ty = 0\n\t\t\tend\n\t\t\tif x == n\n\t\t\t\tbreak\n\t\t\tend\n\t\tend\n\telse\n\t\tif h.has_key?( ar[1] )\n\t\t\tfw.puts h[ar[1]]\n\t\t\tx,y = h[ar[1]].split(/ /).map!{|x|x.to_i-1}\n\t\t\tsh[x][y] = nil\n\t\t\th.delete(ar[1])\n\t\telse\n\t\t\tfw.puts \"-1 -1\"\n\t\tend\n\tend\nend\nf.close\nfw.close\n\n\n"}, {"source_code": "I = open('input.txt')\nO = open('output.txt','w')\nn, m, k = I.gets.split.map &:to_i\nt = [nil] * (n *= m)\nk.times do\n\ta, b, c, d = I.gets.split\n\tif /-1/ then\n\t\tc = t.index b\n\t\tif c then\n\t\t\tt[c] = nil\n\t\t\tO.puts \"#{c / m + 1} #{c % m + 1}\"\n\t\telse\n\t\t\tO.puts '-1 -1'\n\t\tend\n\telse\n\t\tb = (b.to_i - 1) * m + (c.to_i - 1)\n\t\tb += 1 while b < n && t[b]\n\t\tt[b] = d if b < n\n\tend\nend\n"}, {"source_code": "I = open('input.txt')\nO = open('output.txt','w')\nn, m, k = I.gets.split.map &:to_i\nt = [nil] * (n *= m)\nk.times do\n\ta, b, c, d = I.gets.split\n\tif /-1/ then\n\t\tc = t.index b\n\t\tif c then\n\t\t\tt[c] = nil\n\t\t\tO.puts \"#{c / m + 1} #{c % m + 1}\"\n\t\telse\n\t\t\tO.puts '-1 -1'\n\t\tend\n\telse\n\t\tb = (b.to_i - 1) * m + (c.to_i - 1)\n\t\tb += 1 while b < n && t[b]\n\t\tt[b] = d if b < n\n\tend\nend\n\n"}, {"source_code": "I = open('input.txt')\nO = open('output.txt','w')\nn, m, k = I.gets.split.map &:to_i\nt = [nil] * (n *= m)\nk.times do\n\ta, b, c, d = I.gets.split\n\tif /-1/ then\n\t\tc = t.index b\n\t\tif c then\n\t\t\tt[c] = nil\n\t\t\tO.puts \"#{c / m + 1} #{c % m + 1}\"\n\t\telse\n\t\t\tO.puts '-1 -1'\n\t\tend\n\telse\n\t\tb = (b.to_i - 1) * m + (c.to_i - 1)\n\t\tb += 1 while b < n && t[b]\n\t\tt[b] = d if b < n\n\tend\nend\n\n"}, {"source_code": "I = open('input.txt')\nO = open('output.txt','w')\nn, m, k = I.gets.split.map &:to_i\nt = [nil] * (n *= m)\nk.times do\n\ta, b, c, d = I.gets.split\n\tif /-1/ then\n\t\tc = t.index b\n\t\tif c then\n\t\t\tt[c] = nil\n\t\t\tO.puts \"#{c / m + 1} #{c % m + 1}\"\n\t\telse\n\t\t\tO.puts '-1 -1'\n\t\tend\n\telse\n\t\tb = (b.to_i - 1) * m + (c.to_i - 1)\n\t\tb += 1 while b < n && t[b]\n\t\tt[b] = d if b < n\n\tend\nend\n"}, {"source_code": "I = open('input.txt')\nO = open('output.txt','w')\nn, m, k = I.gets.split.map &:to_i\nt = [nil] * (n *= m)\nk.times do\n\ta, b, c, d = I.gets.split\n\tif /-1/ then\n\t\tc = t.index b\n\t\tif c then\n\t\t\tt[c] = nil\n\t\t\tO.puts \"#{c / m + 1} #{c % m + 1}\"\n\t\telse\n\t\t\tO.puts '-1 -1'\n\t\tend\n\telse\n\t\tb = (b.to_i - 1) * m + (c.to_i - 1)\n\t\tb += 1 while b < n && t[b]\n\t\tt[b] = d if b < n\n\tend\nend\n\n"}, {"source_code": "I = open('input.txt')\nO = open('output.txt','w')\nn, m, k = I.gets.split.map &:to_i\nt = [nil] * (n *= m)\nk.times do\n\ta, b, c, d = I.gets.split\n\tif /-1/ then\n\t\tc = t.index b\n\t\tif c then\n\t\t\tt[c] = nil\n\t\t\tO.puts \"#{c / m + 1} #{c % m + 1}\"\n\t\telse\n\t\t\tO.puts '-1 -1'\n\t\tend\n\telse\n\t\tb = (b.to_i - 1) * m + (c.to_i - 1)\n\t\tb += 1 while b < n && t[b]\n\t\tt[b] = d if b < n\n\tend\nend\n\n"}, {"source_code": "I = open('input.txt')\nO = open('output.txt','w')\nn, m, k = I.gets.split.map &:to_i\nt = [nil] * (n *= m)\nk.times do\n a, b, c, d = I.gets.split\n if /-1/ then\n c = t.index b\n if c then\n t[c] = nil\n O.puts \"#{c / m + 1} #{c % m + 1}\"\n else\n O.puts '-1 -1'\n end\n else\n b = (b.to_i - 1) * m + (c.to_i - 1)\n b += 1 while b < n && t[b]\n t[b] = d if b < n\n end\nend"}, {"source_code": "I = open('input.txt')\nO = open('output.txt','w')\nn, m, k = I.gets.split.map &:to_i\nt = [nil] * (n *= m)\nk.times do\n\ta, b, c, d = I.gets.split\n\tif /-1/ then\n\t\tc = t.index b\n\t\tif c then\n\t\t\tt[c] = nil\n\t\t\tO.puts \"#{c / m + 1} #{c % m + 1}\"\n\t\telse\n\t\t\tO.puts '-1 -1'\n\t\tend\n\telse\n\t\tb = (b.to_i - 1) * m + (c.to_i - 1)\n\t\tb += 1 while b < n && t[b]\n\t\tt[b] = d if b < n\n\tend\nend\n\n"}, {"source_code": "I = open('input.txt')\nO = open('output.txt','w')\nn, m, k = I.gets.split.map &:to_i\nt = [nil] * (n *= m)\nk.times do\n\ta, b, c, d = I.gets.split\n\tif /-1/ then\n\t\tc = t.index b\n\t\tif c then\n\t\t\tt[c] = nil\n\t\t\tO.puts \"#{c / m + 1} #{c % m + 1}\"\n\t\telse\n\t\t\tO.puts '-1 -1'\n\t\tend\n\telse\n\t\tb = (b.to_i - 1) * m + (c.to_i - 1)\n\t\tb += 1 while b < n && t[b]\n\t\tt[b] = d if b < n\n\tend\nend\n\n"}, {"source_code": "I = open('input.txt')\nO = open('output.txt','w')\nn, m, k = I.gets.split.map &:to_i\nt = [nil] * (n *= m)\nk.times do\n\ta, b, c, d = I.gets.split\n\tif /-1/ then\n\t\tc = t.index b\n\t\tif c then\n\t\t\tt[c] = nil\n\t\t\tO.puts \"#{c / m + 1} #{c % m + 1}\"\n\t\telse\n\t\t\tO.puts '-1 -1'\n\t\tend\n\telse\n\t\tb = (b.to_i - 1) * m + (c.to_i - 1)\n\t\tb += 1 while b < n && t[b]\n\t\tt[b] = d if b < n\n\tend\nend\n\n"}, {"source_code": "I = open('input.txt')\nO = open('output.txt','w')\nn, m, k = I.gets.split.map &:to_i\nt = [nil] * (n *= m)\nk.times do\n\ta, b, c, d = I.gets.split\n\tif /-1/ then\n\t\tc = t.index b\n\t\tif c then\n\t\t\tt[c] = nil\n\t\t\tO.puts \"#{c / m + 1} #{c % m + 1}\"\n\t\telse\n\t\t\tO.puts '-1 -1'\n\t\tend\n\telse\n\t\tb = (b.to_i - 1) * m + (c.to_i - 1)\n\t\tb += 1 while b < n && t[b]\n\t\tt[b] = d if b < n\n\tend\nend\n\n"}], "negative_code": [{"source_code": "I = open('input.txt')\nO = open('output.txt','w')\nn, m, k = I.gets.split.map &:to_i\nt = Array.new(n *= m)\nk.times do\n\ta, b, c, d = I.gets.split\n\tif /-1/ then\n\t\tc = t.index b\n\t\tif c then\n\t\t\tt[c] = nil\n\t\t\tO.puts \"#{c / m + 1} #{c % m + 1}\"\n\t\telse\n\t\t\tO.puts '-1 -1'\n\t\tend\n\telse\n\t\tb = (b.to_i - 1) * m + (c.to_i - 1)\n\t\tc = 0\n\t\tb, c = b + 1, c + 1 while t[b % n] && c < n\n\t\tt[b % n] = d unless t[b % n]\n\tend\nend\n"}], "src_uid": "44d45a7e21418470bda396d91c65e736"} {"nl": {"description": "You are given a non-decreasing array of non-negative integers $$$a_1, a_2, \\ldots, a_n$$$. Also you are given a positive integer $$$k$$$.You want to find $$$m$$$ non-decreasing arrays of non-negative integers $$$b_1, b_2, \\ldots, b_m$$$, such that: The size of $$$b_i$$$ is equal to $$$n$$$ for all $$$1 \\leq i \\leq m$$$. For all $$$1 \\leq j \\leq n$$$, $$$a_j = b_{1, j} + b_{2, j} + \\ldots + b_{m, j}$$$. In the other word, array $$$a$$$ is the sum of arrays $$$b_i$$$. The number of different elements in the array $$$b_i$$$ is at most $$$k$$$ for all $$$1 \\leq i \\leq m$$$. Find the minimum possible value of $$$m$$$, or report that there is no possible $$$m$$$.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\leq t \\leq 100$$$): the number of test cases. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 \\leq n \\leq 100$$$, $$$1 \\leq k \\leq n$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$0 \\leq a_1 \\leq a_2 \\leq \\ldots \\leq a_n \\leq 100$$$, $$$a_n > 0$$$).", "output_spec": "For each test case print a single integer: the minimum possible value of $$$m$$$. If there is no such $$$m$$$, print $$$-1$$$.", "sample_inputs": ["6\n4 1\n0 0 0 1\n3 1\n3 3 3\n11 3\n0 1 2 2 3 3 3 4 4 4 4\n5 3\n1 2 3 4 5\n9 4\n2 2 3 5 7 11 13 13 17\n10 7\n0 1 1 2 3 3 4 5 5 6"], "sample_outputs": ["-1\n1\n2\n2\n2\n1"], "notes": "NoteIn the first test case, there is no possible $$$m$$$, because all elements of all arrays should be equal to $$$0$$$. But in this case, it is impossible to get $$$a_4 = 1$$$ as the sum of zeros.In the second test case, we can take $$$b_1 = [3, 3, 3]$$$. $$$1$$$ is the smallest possible value of $$$m$$$.In the third test case, we can take $$$b_1 = [0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2]$$$ and $$$b_2 = [0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2]$$$. It's easy to see, that $$$a_i = b_{1, i} + b_{2, i}$$$ for all $$$i$$$ and the number of different elements in $$$b_1$$$ and in $$$b_2$$$ is equal to $$$3$$$ (so it is at most $$$3$$$). It can be proven that $$$2$$$ is the smallest possible value of $$$m$$$."}, "positive_code": [{"source_code": "T = gets.to_i\n\nT.times do\n n, k = gets.split.map(&:to_i)\n nums = gets.split.map(&:to_i)\n us = nums.uniq.size\n\n if k == 1\n if us == 1\n puts 1\n else\n puts -1\n end\n\n next\n end\n\n if us <= k\n puts 1\n next\n end\n\n puts Rational(us - 1, k - 1).ceil\nend\n"}], "negative_code": [{"source_code": "T = gets.to_i\n\nT.times do\n n, k = gets.split.map(&:to_i)\n nums = gets.split.map(&:to_i)\n us = nums.uniq.size\n\n if k == 1\n if us == 1\n puts 1\n else\n puts -1\n end\n\n next\n end\n\n c = nums.each_cons(2).map { |a, b| b - a }\n c.delete(0)\n\n puts Rational(c.size, k - 1).ceil\nend\n"}, {"source_code": "T = gets.to_i\n\nT.times do\n n, k = gets.split.map(&:to_i)\n nums = gets.split.map(&:to_i)\n us = nums.uniq.size\n\n if k == 1\n if us == 1\n puts 1\n else\n puts -1\n end\n\n next\n end\n\n puts Rational(us - 1, k - 1).ceil\nend\n"}, {"source_code": "T = gets.to_i\n\nT.times do\n n, k = gets.split.map(&:to_i)\n nums = gets.split.map(&:to_i)\n\n if k == 1\n if nums.uniq.size == 1\n puts 1\n else\n puts -1\n end\n\n next\n end\n\n base = [0]\n\n nums.each do |n|\n base.each do |b|\n if n ^ b < n\n n ^= b\n end\n end\n\n if n != 0\n base << n\n end\n end\n\n puts Rational(base.size, k).ceil\nend\n"}, {"source_code": "T = gets.to_i\n\nT.times do\n n, k = gets.split.map(&:to_i)\n nums = gets.split.map(&:to_i)\n us = nums.uniq.size\n\n if k == 1\n if us == 1\n puts 1\n else\n puts -1\n end\n\n next\n end\n\n if us <= k\n puts 1\n\n next\n end\n\n base = [0]\n\n nums.each do |n|\n base.each do |b|\n if n ^ b < n\n n ^= b\n end\n end\n\n if n != 0\n base << n\n end\n end\n\n puts Rational(base.size, k).ceil\nend\n"}], "src_uid": "3dc5850220458dec9876560150b612c4"} {"nl": {"description": "Petya works as a PR manager for a successful Berland company BerSoft. He needs to prepare a presentation on the company income growth since 2001 (the year of its founding) till now. Petya knows that in 2001 the company income amounted to a1 billion bourles, in 2002 — to a2 billion, ..., and in the current (2000 + n)-th year — an billion bourles. On the base of the information Petya decided to show in his presentation the linear progress history which is in his opinion perfect. According to a graph Petya has already made, in the first year BerSoft company income must amount to 1 billion bourles, in the second year — 2 billion bourles etc., each following year the income increases by 1 billion bourles. Unfortunately, the real numbers are different from the perfect ones. Among the numbers ai can even occur negative ones that are a sign of the company’s losses in some years. That is why Petya wants to ignore some data, in other words, cross some numbers ai from the sequence and leave only some subsequence that has perfect growth.Thus Petya has to choose a sequence of years y1, y2, ..., yk,so that in the year y1 the company income amounted to 1 billion bourles, in the year y2 — 2 billion bourles etc., in accordance with the perfect growth dynamics. Help him to choose the longest such sequence.", "input_spec": "The first line contains an integer n (1 ≤ n ≤ 100). The next line contains n integers ai ( - 100 ≤ ai ≤ 100). The number ai determines the income of BerSoft company in the (2000 + i)-th year. The numbers in the line are separated by spaces.", "output_spec": "Output k — the maximum possible length of a perfect sequence. In the next line output the sequence of years y1, y2, ..., yk. Separate the numbers by spaces. If the answer is not unique, output any. If no solution exist, output one number 0.", "sample_inputs": ["10\n-2 1 1 3 2 3 4 -10 -2 5", "3\n-1 -2 -3"], "sample_outputs": ["5\n2002 2005 2006 2007 2010", "0"], "notes": null}, "positive_code": [{"source_code": "n = gets.chomp.to_i\na = gets.chomp.split(/ /).map!{|x|x.to_i}\n\nyears = Array.new\nseq = 1\nfor i in 0..n-1\n\tif a[i] == seq\n\t\tyears.push((2001+i).to_s)\n\t\tseq += 1\n\tend\nend\n\nif years.length == 0\n\tputs \"0\"\nelse\n\tputs years.length.to_s\n\tputs years.join(' ')\nend"}, {"source_code": "gets\na, b = gets.split.map(&:to_i), [2000]\nwhile a.any? && p = a.index(b.size) do\n\tb << b[-1] + p + 1\n\ta = a[p + 1 .. -1]\nend\nb[0] = b.size - 1\nputs b * ' '\n"}, {"source_code": "gets\na, b = gets.split.map(&:to_i), [2000]\nwhile a.any? && p = a.index(b.size) do\n\tb << b[-1] + p + 1\n\ta = a[p + 1 .. -1]\nend\nb[0] = b.size - 1\nputs b * ' '\n\n"}, {"source_code": "gets\na, b = gets.split.map(&:to_i), [2000]\nwhile a.any? && p = a.index(b.size) do\n\tb << b[-1] + p + 1\n\ta = a[p + 1 .. -1]\nend\nb[0] = b.size - 1\nputs b * ' '\n\n"}, {"source_code": "gets\na, b = gets.split.map(&:to_i), [2000]\nwhile a.any? && p = a.index(b.size) do\n\tb << b[-1] + p + 1\n\ta = a[p + 1 .. -1]\nend\nb[0] = b.size - 1\nputs b * ' '\n"}, {"source_code": "gets\na, b = gets.split.map(&:to_i), [2000]\nwhile a.any? && p = a.index(b.size) do\n\tb << b[-1] + p + 1\n\ta = a[p + 1 .. -1]\nend\nb[0] = b.size - 1\nputs b * ' '\n\n"}, {"source_code": "gets\na, b = gets.split.map(&:to_i), [2000]\nwhile a.any? && p = a.index(b.size) do\n\tb << b[-1] + p + 1\n\ta = a[p + 1 .. -1]\nend\nb[0] = b.size - 1\nputs b * ' '\n\n"}, {"source_code": "n = gets.to_i\n\na = gets.split.map(&:to_i)\n\nans ||= []\n\npre = 0\na.each_index do |x|\n if a[x] == pre + 1\n ans << x + 2001\n pre = a[x]\n end\nend\n\nputs ans.size\nputs ans * \" \" if ans.any?\n\n"}, {"source_code": "gets\na, b = gets.split.map(&:to_i), [2000]\nwhile a.any? && p = a.index(b.size) do\n\tb << b[-1] + p + 1\n\ta = a[p + 1 .. -1]\nend\nb[0] = b.size - 1\nputs b * ' '\n\n"}, {"source_code": "gets\na, b = gets.split.map(&:to_i), [2000]\nwhile a.any? && p = a.index(b.size) do\n\tb << b[-1] + p + 1\n\ta = a[p + 1 .. -1]\nend\nb[0] = b.size - 1\nputs b * ' '\n\n"}, {"source_code": "gets\na, b = gets.split.map(&:to_i), [2000]\nwhile a.any? && p = a.index(b.size) do\n\tb << b[-1] + p + 1\n\ta = a[p + 1 .. -1]\nend\nb[0] = b.size - 1\nputs b * ' '\n\n"}, {"source_code": "gets\na, b = gets.split.map(&:to_i), [2000]\nwhile a.any? && p = a.index(b.size) do\n\tb << b[-1] + p + 1\n\ta = a[p + 1 .. -1]\nend\nb[0] = b.size - 1\nputs b * ' '\n\n"}], "negative_code": [], "src_uid": "499ddfd7e0563e3ca8c71219e408650f"} {"nl": {"description": "A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer hi. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |hi - hi + 1| ≤ 1 holds.At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |hi - hi + 1| ≤ 1.", "input_spec": "The first line contains two space-separated numbers, n and m (1 ≤ n ≤ 108, 1 ≤ m ≤ 105) — the number of days of the hike and the number of notes left in the journal. Next m lines contain two space-separated integers di and hdi (1 ≤ di ≤ n, 0 ≤ hdi ≤ 108) — the number of the day when the i-th note was made and height on the di-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: di < di + 1.", "output_spec": "If the notes aren't contradictory, print a single integer — the maximum possible height value throughout the whole route. If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes).", "sample_inputs": ["8 2\n2 0\n7 0", "8 3\n2 0\n7 0\n8 3"], "sample_outputs": ["2", "IMPOSSIBLE"], "notes": "NoteFor the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1).In the second sample the inequality between h7 and h8 does not hold, thus the information is inconsistent."}, "positive_code": [{"source_code": "n, m = gets.strip.split(\" \").map(&:to_i)\nrecord = Array.new\nm.times do |i|\n d, h = gets.strip.split(\" \").map(&:to_i)\n record.push [d, h]\nend\npossible = true\nmaxh = 0\n(m-1).times do |i|\n dd = record[i+1][0] - record[i][0]\n dh = record[i+1][1] - record[i][1]\n if dh.abs > dd\n possible = false\n end\n hh = (dd - dh.abs) / 2\n maxh = [maxh, record[i+1][1] + hh, record[i][1] + hh].max\nend\n\nc1 = record[0][0] + record[0][1] - 1\nc2 = (n - record[-1][0]) + record[-1][1]\nmaxh = [c1, c2, maxh].max\n\nif possible\n puts maxh\nelse\n puts \"IMPOSSIBLE\"\nend\n"}, {"source_code": "#!/usr/bin/env ruby\n# -*- coding: utf-8 -*-\n\n# c.rb\n\ndef main()\n n, m = gets.chomp.split(\" \").map{|s| next s.to_i }\n d = [0] * m\n h = [0] * m\n\n m.times do |i|\n d[i], h[i] = gets.chomp.split(\" \").map{|s| next s.to_i }\n end\n\n r = h[0]\n 1.upto(m-1) do |i|\n dd = d[i] - d[i-1]\n dh = h[i] - h[i-1]\n if dd.abs < dh.abs then\n puts(\"IMPOSSIBLE\")\n return\n else\n r = [r, h[i]].max\n r = [r, [h[i], h[i-1]].min + (dd.abs + dh.abs) / 2 ].max\n end\n end\n r = [r, h[-1] + (n - d[-1])].max\n r = [r, h[0] + (d[0] - 1)].max\n puts r\nend\n\nif __FILE__ == $0 then\n main()\nend\n"}, {"source_code": "n, m = gets.split.collect{|i| i.to_i}\npre = gets.split.collect{|i| i.to_i}\nans = pre[1]+pre[0]-1\n(1...m).each do\n tmp = gets.split.collect{|i| i.to_i}\n t = tmp[0]-pre[0]\n v1, v2 = [pre[1], tmp[1]].minmax\n x = (1.0*t+v2-v1)/2\n if x < 0 or x > t\n puts \"IMPOSSIBLE\"\n exit\n end\n x = x.round\n ans = [ans,v1+x-1,v2+t-x].max\n pre = tmp\nend\nans = [ans,pre[1]+n-pre[0]].max\nputs ans\n"}, {"source_code": "n, m = gets.split.map {|i| i.to_i}\nb1, a1 = gets.split.map {|i| i.to_i}\nmaxx = a1 + b1 - 1\nfor i in 0...m - 1\n b, a = gets.split.map {|i| i.to_i}\n maxx = [maxx, (b - b1 + a1 + a) / 2].max\n if (a1 - a).abs > (b1 - b).abs\n print 'IMPOSSIBLE'\n exit(0)\n end\n a1, b1 = a, b\nend\nprint [maxx, a1 + n - b1].max\n# a - a1 + a - a2 <= b2 - b1\n# a + a <= b2 - b1 + a1 + a2\n# a <= (b2 - b1)\n"}], "negative_code": [{"source_code": "n, m = gets.strip.split(\" \").map(&:to_i)\nrecord = Array.new\nm.times do |i|\n d, h = gets.strip.split(\" \").map(&:to_i)\n record.push [d, h]\nend\npossible = true\nmaxh = 0\n(m-1).times do |i|\n dd = record[i+1][0] - record[i][0]\n dh = record[i+1][1] - record[i][1]\n if dh.abs > dd\n possible = false\n end\n hh = (dd - dh.abs) / 2\n maxh = [maxh, record[i+1][1] + hh, record[i][1] + hh].max\nend\n\nc1 = record[0][0] + record[0][1]\nc2 = (n - record[-1][0]) + record[-1][1]\nmaxh = [c1, c2, maxh].max\n\nif possible\n puts maxh\nelse\n puts \"IMPOSSIBLE\"\nend\n"}, {"source_code": "#!/usr/bin/env ruby\n# -*- coding: utf-8 -*-\n\n# c.rb\n\ndef main()\n n, m = gets.chomp.split(\" \").map{|s| next s.to_i }\n d = [0] * m\n h = [0] * m\n\n m.times do |i|\n d[i], h[i] = gets.chomp.split(\" \").map{|s| next s.to_i }\n end\n\n r = h[0]\n 1.upto(m-1) do |i|\n dd = d[i] - d[i-1]\n dh = h[i] - h[i-1]\n if dd.abs < dh.abs then\n puts(\"IMPOSSIBLE\")\n return\n else\n r = [r, h[i]].max\n r = [r, [h[i], h[i-1]].min + (dd.abs + dh.abs) / 2 ].max\n end\n end\n puts r\nend\n\nif __FILE__ == $0 then\n main()\nend\n"}, {"source_code": "#!/usr/bin/env ruby\n# -*- coding: utf-8 -*-\n\n# c.rb\n\ndef main()\n n, m = gets.chomp.split(\" \").map{|s| next s.to_i }\n d = [0] * m\n h = [0] * m\n\n m.times do |i|\n d[i], h[i] = gets.chomp.split(\" \").map{|s| next s.to_i }\n end\n\n r = h[0] + d[0]\n 1.upto(m-1) do |i|\n dd = d[i] - d[i-1]\n dh = h[i] - h[i-1]\n if dd.abs < dh.abs then\n puts(\"IMPOSSIBLE\")\n return\n else\n r = [r, h[i]].max\n r = [r, [h[i], h[i-1]].min + (dd.abs + dh.abs) / 2 ].max\n end\n end\n puts r\nend\n\nif __FILE__ == $0 then\n main()\nend\n"}, {"source_code": "#!/usr/bin/env ruby\n# -*- coding: utf-8 -*-\n\n# c.rb\n\ndef main()\n n, m = gets.chomp.split(\" \").map{|s| next s.to_i }\n d = [0] * (m + 1)\n h = [0] * (m + 1)\n\n m.times do |i|\n d[i+1], h[i+1] = gets.chomp.split(\" \").map{|s| next s.to_i }\n end\n\n r = 0\n 1.upto(m) do |i|\n dd = d[i] - d[i-1]\n dh = h[i] - h[i-1]\n if dd.abs < dh.abs then\n puts(\"IMPOSSIBLE\")\n return\n else\n r = [r, h[i]].max\n r = [r, [h[i], h[i-1]].min + (dd.abs + dh.abs) / 2 ].max\n end\n end\n puts r\nend\n\nif __FILE__ == $0 then\n main()\nend\n"}, {"source_code": "#!/usr/bin/env ruby\n# -*- coding: utf-8 -*-\n\n# c.rb\n\ndef main()\n n, m = gets.chomp.split(\" \").map{|s| next s.to_i }\n d = [0] * m\n h = [0] * m\n\n m.times do |i|\n d[i], h[i] = gets.chomp.split(\" \").map{|s| next s.to_i }\n end\n\n r = 0\n 1.upto(m-1) do |i|\n dd = d[i] - d[i-1]\n dh = h[i] - h[i-1]\n if dd.abs < dh.abs then\n puts(\"IMPOSSIBLE\")\n return\n else\n r = [r, h[i]].max\n r = [r, [h[i], h[i-1]].min + (dd.abs + dh.abs) / 2 ].max\n end\n end\n puts r\nend\n\nif __FILE__ == $0 then\n main()\nend\n"}, {"source_code": "#!/usr/bin/env ruby\n# -*- coding: utf-8 -*-\n\n# c.rb\n\ndef main()\n n, m = gets.chomp.split(\" \").map{|s| next s.to_i }\n d = [0] * m\n h = [0] * m\n\n m.times do |i|\n d[i], h[i] = gets.chomp.split(\" \").map{|s| next s.to_i }\n end\n\n r = h[0]\n 1.upto(m-1) do |i|\n dd = d[i] - d[i-1]\n dh = h[i] - h[i-1]\n if dd.abs < dh.abs then\n puts(\"IMPOSSIBLE\")\n return\n else\n r = [r, h[i]].max\n r = [r, [h[i], h[i-1]].min + (dd.abs + dh.abs) / 2 ].max\n end\n end\n r = [r, h[-1] + (n - d[-1])].max\n puts r\nend\n\nif __FILE__ == $0 then\n main()\nend\n"}, {"source_code": "n, m = gets.split.collect{|i| i.to_i}\npre = gets.split.collect{|i| i.to_i}\nans = pre[1]+pre[0]-1\n(1...m).each do\n tmp = gets.split.collect{|i| i.to_i}\n t = tmp[0]-pre[0]\n v1, v2 = [pre[1], tmp[1]].minmax\n x = (1.0*t+v2-v1)/2\n if x < 0 or x > t\n puts \"IMPOSSIBLE\"\n exit\n end\n if x%1 >= 0.5\n x = x.ceil\n else\n x = x.floor\n end\n ans = [ans,v1+x-1,tmp[1]].max\n pre = tmp\nend\nans = pre[1]+n-pre[0] if ans < pre[1]+n-pre[0]\nputs ans\n"}, {"source_code": "n, m = gets.split.collect{|i| i.to_i}\npre = gets.split.collect{|i| i.to_i}\nans = pre[1]+pre[0]-1\n(1...m).each do\n tmp = gets.split.collect{|i| i.to_i}\n t = tmp[0]-pre[0]\n v1, v2 = [pre[1], tmp[1]].minmax\n x = (1.0*t+v2-v1)/2\n if x < 0 or x > t\n puts \"IMPOSSIBLE\"\n exit\n end\n if x%1 >= 0.5\n x = x.ceil\n else\n x = x.floor\n end\n ans = [v1+x-1,tmp[1]].max\n pre = tmp\nend\nans = pre[1]+n-pre[0] if ans < pre[1]+n-pre[0]\nputs ans\n"}, {"source_code": "n, m = gets.split.collect{|i| i.to_i}\npre = gets.split.collect{|i| i.to_i}\nans = pre[1]+pre[0]-1\n(1...m).each do\n tmp = gets.split.collect{|i| i.to_i}\n t = tmp[0]-pre[0]-1\n v1, v2 = [pre[1], tmp[1]].minmax\n x = (1.0*t+v2-v1)/2\n if x < 0 or x > t\n puts \"IMPOSSIBLE\"\n exit\n end\n if x%1 >= 0.5\n x = x.ceil\n else\n x = x.floor\n end\n ans = v1+x if ans < v1+x\n pre = tmp\nend\nans = pre[1]+n-pre[0] if ans < pre[1]+n-pre[0]\nputs ans\n"}, {"source_code": "n, m = gets.split.collect{|i| i.to_i}\npre = gets.split.collect{|i| i.to_i}\nans = pre[1]+pre[0]-1\n(1...m).each do\n tmp = gets.split.collect{|i| i.to_i}\n t = tmp[0]-pre[0]\n v1, v2 = [pre[1], tmp[1]].minmax\n x = (1.0*t+v2-v1)/2\n if x < 0 or x > t\n puts \"IMPOSSIBLE\"\n exit\n end\n if x%1 >= 0.5\n x = x.ceil\n else\n x = x.floor\n end\n ans = v1+x-1 if ans < v1+x-1\n pre = tmp\nend\nans = pre[1]+n-pre[0] if ans < pre[1]+n-pre[0]\nputs ans\n"}], "src_uid": "77b5bed410d621fb56c2eaebccff5108"} {"nl": {"description": "Theofanis really likes sequences of positive integers, thus his teacher (Yeltsa Kcir) gave him a problem about a sequence that consists of only special numbers.Let's call a positive number special if it can be written as a sum of different non-negative powers of $$$n$$$. For example, for $$$n = 4$$$ number $$$17$$$ is special, because it can be written as $$$4^0 + 4^2 = 1 + 16 = 17$$$, but $$$9$$$ is not.Theofanis asks you to help him find the $$$k$$$-th special number if they are sorted in increasing order. Since this number may be too large, output it modulo $$$10^9+7$$$.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \\le n \\le 10^9$$$; $$$1 \\le k \\le 10^9$$$).", "output_spec": "For each test case, print one integer — the $$$k$$$-th special number in increasing order modulo $$$10^9+7$$$.", "sample_inputs": ["3\n3 4\n2 12\n105 564"], "sample_outputs": ["9\n12\n3595374"], "notes": "NoteFor $$$n = 3$$$ the sequence is $$$[1,3,4,9...]$$$"}, "positive_code": [{"source_code": "t = gets.to_i\nfor j in 1..t do\n\tn, k = gets.split.map(&:to_i)\n\ttmp = 1; res = 0\n\tfor i in 0..31 do\n\t\tif (k&(1< eps)\n res = car.res\n end\nend\n\nputs res\n"}, {"source_code": "a,b=gets.split.map(&:to_i)\nn=gets.to_i\ntime=10000000\nn.times do\n xi,yi,vi=gets.split.map(&:to_i)\n x=(xi-a).abs\n y=(yi-b).abs\n t=Math.sqrt(x**2+y**2)/vi\n if(tt then\n\t\tans=t\n\tend\n}\nputs ans\n\n"}, {"source_code": "ab = gets.chomp.split.map(&:to_i)\nn = gets.chomp.to_i\narr = []\nd = []\nfor i in (0..n-1)\n\tarr << gets.chomp.split.map(&:to_i)\n\td << Math.sqrt((arr[i][0] - ab[0])**2 + (arr[i][1] - ab[1])**2)/arr[i][2]\nend\n\nprint d.min"}, {"source_code": "a,b=gets.split.map(&:to_f)\np gets.to_i.times.map{\nx,y,z=gets.split.map(&:to_f)\n\tMath.hypot(a-x,b-y)/z\n}.min"}, {"source_code": "result = 1000000.0\nmy_x, my_y = gets.split.map(&:to_i)\nn = gets.chomp.to_i\n\n1.upto(n) do\n\tx, y, v = gets.split.map(&:to_i)\n\n\td = Math.sqrt((my_x-x)**2 + (my_y-y)**2)/v\n\n\tresult = [d, result].min\nend\n\nputs result.round(8)\n"}, {"source_code": "a,b = gets.split.map &:to_f\nn = gets.to_i\nputs (1..n).map{|_|\n x,y,v=gets.split.map &:to_f\n Math.sqrt((x-a)**2+(y-b)**2)/v\n}.min\n"}, {"source_code": "def dist x1, x2, y1, y2\n\t((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5\nend\n\ncharX, charY = gets.chomp.split(' ').map(&:to_i)\n\ntaxiNum = gets.chomp.to_i\ndistArr = []\n\ntaxiNum.times do\n\ttaxiX, taxiY, velocity = gets.chomp.split(' ').map(&:to_i)\n\tvalue = dist charX, taxiX, charY, taxiY\n\tdistArr << value / velocity\nend\n\nputs distArr.min"}], "negative_code": [{"source_code": "a,b=gets.split.map(&:to_f)\np gets.to_i.times.map{\nx,y,z=gets.split.map(&:to_f)\n((a-x).abs+(b-y).abs)/z\n}.min"}, {"source_code": "x, y = gets.chomp.split.map(&:to_f)\nn = gets.chomp.to_i\n\narr = []\nn.times do\n x2, y2, v = gets.chomp.split.map(&:to_f)\n arr << ((x2 - x).abs + (y2 - y).abs) / v\nend\nputs arr.min\n"}], "src_uid": "a74c65acd41ff9bb845062222135c60a"} {"nl": {"description": "You are given a set of $$$n$$$ segments on the axis $$$Ox$$$, each segment has integer endpoints between $$$1$$$ and $$$m$$$ inclusive. Segments may intersect, overlap or even coincide with each other. Each segment is characterized by two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 \\le l_i \\le r_i \\le m$$$) — coordinates of the left and of the right endpoints. Consider all integer points between $$$1$$$ and $$$m$$$ inclusive. Your task is to print all such points that don't belong to any segment. The point $$$x$$$ belongs to the segment $$$[l; r]$$$ if and only if $$$l \\le x \\le r$$$.", "input_spec": "The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n, m \\le 100$$$) — the number of segments and the upper bound for coordinates. The next $$$n$$$ lines contain two integers each $$$l_i$$$ and $$$r_i$$$ ($$$1 \\le l_i \\le r_i \\le m$$$) — the endpoints of the $$$i$$$-th segment. Segments may intersect, overlap or even coincide with each other. Note, it is possible that $$$l_i=r_i$$$, i.e. a segment can degenerate to a point.", "output_spec": "In the first line print one integer $$$k$$$ — the number of points that don't belong to any segment. In the second line print exactly $$$k$$$ integers in any order — the points that don't belong to any segment. All points you print should be distinct. If there are no such points at all, print a single integer $$$0$$$ in the first line and either leave the second line empty or do not print it at all.", "sample_inputs": ["3 5\n2 2\n1 2\n5 5", "1 7\n1 7"], "sample_outputs": ["2\n3 4", "0"], "notes": "NoteIn the first example the point $$$1$$$ belongs to the second segment, the point $$$2$$$ belongs to the first and the second segments and the point $$$5$$$ belongs to the third segment. The points $$$3$$$ and $$$4$$$ do not belong to any segment.In the second example all the points from $$$1$$$ to $$$7$$$ belong to the first segment."}, "positive_code": [{"source_code": "a,b=gets.split.map &:to_i\nr=[*1..b]\na.times{x,y=gets.split.map &:to_i;r-=[*x..y]}\np r.size\nputs r*' '"}, {"source_code": "N, M = gets.chomp.split(/ /)\nrange = *(1..M.to_i)\n\nN.to_i.times do |i|\n u, l = gets.chomp.split(/ /)\n\n (u.to_i..l.to_i).each do |i|\n if range.include? i \n range.delete(i) \n end \n end \nend\n\np range.length\n\nif range.length > 0\n range.each{|i| print \"#{i} \"}\nend"}, {"source_code": "\ndef solution\n n, m = read_ints\n\n starts = Hash.new { |h, k| h[k] = 0 }\n ends = Hash.new { |h, k| h[k] = 0 }\n\n n.times do\n l, r = read_ints\n starts[l] += 1\n ends[r] += 1\n end\n\n crt = 0\n\n alone = []\n\n 1.upto(m) do |i|\n crt += starts[i]\n alone << i if crt == 0\n crt -= ends[i]\n end\n\n puts alone.size\n puts alone.join(' ')\n\nend\n\ndef read_int\n gets.to_i\nend\n\ndef read_ints\n gets.split.map(&:to_i)\nend\n\ndef read_string\n gets.chomp\nend\n\nsolution unless ENV['TEST__MODE']\n"}, {"source_code": "N, M = gets.split.map(&:to_i)\nimos = Array.new(M+2, 0)\nN.times do\n l, r = gets.split.map(&:to_i)\n imos[l] += 1\n imos[r+1] -= 1\nend\n(1..M).each do |i|\n imos[i] += imos[i-1]\nend\nans = (1..M).select{|i|imos[i] == 0}\nputs ans.length\nputs ans.join(' ') if ans.length > 0"}, {"source_code": "n, m = gets.strip.split.map(&:to_i)\n\nary = Array.new(m+1, 0)\nn.times do\n l, r = gets.strip.split.map(&:to_i)\n l.upto(r).each {|i| ary[i]=1}\nend\n\nans=[]\n1.upto(m).each {|i| ans << i if ary[i]==0 }\n\nputs ans.size\nputs ans.join(\" \")"}, {"source_code": "n, m = gets.split.map(&:to_i)\nans = []\nlrs = []\nn.times do\n l, r = gets.split.map(&:to_i)\n lrs << (l..r).to_a\nend\n\n(1..m).each do |i|\n inclusive = false\n lrs.each do |lr|\n if lr.to_a.include?(i)\n inclusive = true\n end\n end\n ans << i unless inclusive\nend\nputs ans.size\nputs ans.join(\" \")"}, {"source_code": "def solution(m, segments)\n o = {}\n c = {}\n\n segments.each do |segment|\n o[segment[0]] = o.fetch(segment[0], 0) + 1\n c[segment[1]] = c.fetch(segment[1], 0) + 1\n end\n\n s = []\n x = 0\n (1..m).each do |i|\n x += o.fetch(i, 0)\n s << i if x == 0\n x -= c.fetch(i, 0)\n end\n return s\nend\n\nn, m = gets.chomp.split(\" \").map(&:to_i)\nsegments = []\nn.times { segments << gets.chomp.split(\" \").map(&:to_i) }\n\ns = solution(m, segments)\n\nputs s.length\nputs s.join(\" \")\n"}, {"source_code": "n, m = gets.split.map{|x| x.to_i}\nsegs = []\nn.times{segs << gets.split.map{|x| x.to_i}}\npoints = []\n(1..m).each do \n |x| points << x if segs.inject(true) do |s, y|\n s &&= !x.between?(y[0], y[1])\n end\nend\nputs(points.size, points.join(' '))\n"}, {"source_code": "n, m = gets.split.map{|x| x.to_i}\nsegs = []\nn.times{segs << gets.split.map{|x| x.to_i}}\npoints = []\n(1..m).each do |x|\n points << x if segs.inject(true) do |s, y|\n s &&= !x.between?(y[0], y[1])\n end\nend\nputs(points.size, points.join(' '))\n"}], "negative_code": [{"source_code": "N, M = gets.chomp.split(/ /)\nrange = *(1..5)\nam = 0\n\nN.to_i.times do |i|\n u, l = gets.chomp.split(/ /)\n\n (u.to_i..l.to_i).each do |i|\n if range.include? i \n range.delete(i) \n end \n end \nend\n\nif range.length > 0\n p range.length\n p range\nend"}, {"source_code": "N, M = gets.chomp.split(/ /)\nrange = *(1..M.to_i)\nam = 0\n\nN.to_i.times do |i|\n u, l = gets.chomp.split(/ /)\n\n (u.to_i..l.to_i).each do |i|\n if range.include? i \n range.delete(i) \n end \n end \nend\n\nif range.length > 0\n p range.length\n range.each{|i| print \"#{i} \"}\nend"}], "src_uid": "f336b622fdaf5960032268641320bb53"} {"nl": {"description": "A little boy Gerald entered a clothes shop and found out something very unpleasant: not all clothes turns out to match. For example, Gerald noticed that he looks rather ridiculous in a smoking suit and a baseball cap.Overall the shop sells n clothing items, and exactly m pairs of clothing items match. Each item has its price, represented by an integer number of rubles. Gerald wants to buy three clothing items so that they matched each other. Besides, he wants to spend as little money as possible. Find the least possible sum he can spend.", "input_spec": "The first input file line contains integers n and m — the total number of clothing items in the shop and the total number of matching pairs of clothing items (). Next line contains n integers ai (1 ≤ ai ≤ 106) — the prices of the clothing items in rubles. Next m lines each contain a pair of space-separated integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi). Each such pair of numbers means that the ui-th and the vi-th clothing items match each other. It is guaranteed that in each pair ui and vi are distinct and all the unordered pairs (ui, vi) are different.", "output_spec": "Print the only number — the least possible sum in rubles that Gerald will have to pay in the shop. If the shop has no three clothing items that would match each other, print \"-1\" (without the quotes).", "sample_inputs": ["3 3\n1 2 3\n1 2\n2 3\n3 1", "3 2\n2 3 4\n2 3\n2 1", "4 4\n1 1 1 1\n1 2\n2 3\n3 4\n4 1"], "sample_outputs": ["6", "-1", "-1"], "notes": "NoteIn the first test there only are three pieces of clothing and they all match each other. Thus, there is only one way — to buy the 3 pieces of clothing; in this case he spends 6 roubles.The second test only has three pieces of clothing as well, yet Gerald can't buy them because the first piece of clothing does not match the third one. Thus, there are no three matching pieces of clothing. The answer is -1.In the third example there are 4 pieces of clothing, but Gerald can't buy any 3 of them simultaneously. The answer is -1."}, "positive_code": [{"source_code": "\nn,m=gets.split.map(&:to_i)\na=gets.split.map(&:to_i)\ng=Array.new(n){Array.new(n,0)}\nm.times{\n p,q=gets.split.map(&:to_i)\n g[p-1][q-1]=g[q-1][p-1]=1\n}\nans=9999999999\n0.upto(n-1){|i|\n (i+1).upto(n-1){|j|\n if g[i][j]==0 then\n next\n end\n (j+1).upto(n-1){|k|\n if g[i][k]==0 || g[j][k]==0 then\n next\n end\n ans=[ans,a[i]+a[j]+a[k]].min\n }\n }\n}\nputs (ans>=999999999)?-1:ans\n"}, {"source_code": "n, m = gets.split(\"\\s\").map(&:to_i)\ncost = gets.split(\"\\s\").map(&:to_i)\narr = Array.new(111) { Array.new }\nm.times do\n\tu, v = gets.split(\"\\s\").map(&:to_i)\n\tarr[u] << v\n\tarr[v] << u\nend\n\nres = 1e10\nfor i in 1..n\n\tfor j in (i+1)..n\n\t\tfor k in (j+1)..n\n\t\t\tres = [res, cost[i-1]+cost[j-1]+cost[k-1]].min if arr[i].include?(j) && arr[j].include?(k) && arr[k].include?(i)\n\t\tend\n\tend\nend\n\nres = -1 if res == 1e10\nputs res\n"}, {"source_code": "sn, sm = gets.chop.split(/\\s+/)\n\ncount = sn.to_i\ncountRelation = sm.to_i\n\ncosts = Array.new(0)\nscosts = gets.chop.split(/\\s+/)\n(0..count-1).each { |i|\n costs[i] = scosts[i].to_i\n}\n\nrelations = Array.new()\n(0..count-1).each { |i|\n relations[i] = Array.new()\n (0..count-1).each { |j|\n relations[i][j] = false\n }\n}\n\n(0..countRelation-1).each { |i|\n su, sv = gets.chop.split(/\\s+/)\n u = su.to_i - 1;\n v = sv.to_i - 1;\n relations[u][v] = true\n relations[v][u] = true\n}\n\nsolves = Array.new(0)\n\n#(0..count-2).each { |row|\n## (row..count-2).each { |col|\n# if (relations[row][col] == true and relations[row][col+1] == true and relations[row+1][col+1] == true)\n# solves[solves.length] = costs[row] + costs[col] + costs[col+1]\n# end\n# }\n#}\n\n\n(0..count-1).each { |i|\n (0..count-1).each { |j|\n (0..count-1).each { |k|\n if (relations[i][j] == true and relations[j][k] == true and relations[i][k] == true)\n solves[solves.length] = costs[i] + costs[j] + costs[k]\n end\n }\n }\n}\n\n\nif (solves.length > 0)\n puts solves.min\nelse\n puts \"-1\"\nend"}], "negative_code": [{"source_code": "sn, sm = gets.chop.split(/\\s+/)\n\ncount = sn.to_i\ncountRelation = sm.to_i\n\ncosts = Array.new(0)\nscosts = gets.chop.split(/\\s+/)\n(0..count-1).each { |i|\n costs[i] = scosts[i].to_i\n}\n\nrelations = Array.new()\n(0..count-1).each { |i|\n relations[i] = Array.new()\n (0..count-1).each { |j|\n relations[i][j] = false\n }\n}\n\n(0..countRelation-1).each { |i|\n su, sv = gets.chop.split(/\\s+/)\n u = su.to_i - 1;\n v = sv.to_i - 1;\n relations[u][v] = true\n relations[v][u] = true\n}\n\nsolves = Array.new(0)\n\n(0..count-2).each { |row|\n (row..count-2).each { |col|\n if (relations[row][col] == true and relations[row][col+1] == true and relations[row+1][col+1] == true)\n solves[solves.length] = costs[row] + costs[col] + costs[col+1]\n end\n }\n}\n\nif (solves.length > 0)\n puts solves.min\nelse\n puts \"-1\"\nend"}], "src_uid": "d90da1e932a6aa546bec4e1bd4b1fbec"} {"nl": {"description": "Lord Omkar would like to have a tree with $$$n$$$ nodes ($$$3 \\le n \\le 10^5$$$) and has asked his disciples to construct the tree. However, Lord Omkar has created $$$m$$$ ($$$\\mathbf{1 \\le m < n}$$$) restrictions to ensure that the tree will be as heavenly as possible. A tree with $$$n$$$ nodes is an connected undirected graph with $$$n$$$ nodes and $$$n-1$$$ edges. Note that for any two nodes, there is exactly one simple path between them, where a simple path is a path between two nodes that does not contain any node more than once.Here is an example of a tree: A restriction consists of $$$3$$$ pairwise distinct integers, $$$a$$$, $$$b$$$, and $$$c$$$ ($$$1 \\le a,b,c \\le n$$$). It signifies that node $$$b$$$ cannot lie on the simple path between node $$$a$$$ and node $$$c$$$. Can you help Lord Omkar and become his most trusted disciple? You will need to find heavenly trees for multiple sets of restrictions. It can be shown that a heavenly tree will always exist for any set of restrictions under the given constraints.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$). Description of the test cases follows. The first line of each test case contains two integers, $$$n$$$ and $$$m$$$ ($$$3 \\leq n \\leq 10^5$$$, $$$\\mathbf{1 \\leq m < n}$$$), representing the size of the tree and the number of restrictions. The $$$i$$$-th of the next $$$m$$$ lines contains three integers $$$a_i$$$, $$$b_i$$$, $$$c_i$$$ ($$$1 \\le a_i, b_i, c_i \\le n$$$, $$$a$$$, $$$b$$$, $$$c$$$ are distinct), signifying that node $$$b_i$$$ cannot lie on the simple path between nodes $$$a_i$$$ and $$$c_i$$$. It is guaranteed that the sum of $$$n$$$ across all test cases will not exceed $$$10^5$$$.", "output_spec": "For each test case, output $$$n-1$$$ lines representing the $$$n-1$$$ edges in the tree. On each line, output two integers $$$u$$$ and $$$v$$$ ($$$1 \\le u, v \\le n$$$, $$$u \\neq v$$$) signifying that there is an edge between nodes $$$u$$$ and $$$v$$$. Given edges have to form a tree that satisfies Omkar's restrictions.", "sample_inputs": ["2\n7 4\n1 2 3\n3 4 5\n5 6 7\n6 5 4\n5 3\n1 2 3\n2 3 4\n3 4 5"], "sample_outputs": ["1 2\n1 3\n3 5\n3 4\n2 7\n7 6\n5 1\n1 3\n3 2\n2 4"], "notes": "NoteThe output of the first sample case corresponds to the following tree: For the first restriction, the simple path between $$$1$$$ and $$$3$$$ is $$$1, 3$$$, which doesn't contain $$$2$$$. The simple path between $$$3$$$ and $$$5$$$ is $$$3, 5$$$, which doesn't contain $$$4$$$. The simple path between $$$5$$$ and $$$7$$$ is $$$5, 3, 1, 2, 7$$$, which doesn't contain $$$6$$$. The simple path between $$$6$$$ and $$$4$$$ is $$$6, 7, 2, 1, 3, 4$$$, which doesn't contain $$$5$$$. Thus, this tree meets all of the restrictions.The output of the second sample case corresponds to the following tree: "}, "positive_code": [{"source_code": "gets.to_i.times do\n\tn, m = gets.split.map(&:to_i)\n\tvec = Array.new(n+1) { 0 }\n\tfor i in 0..m-1 do\n\t\tx, y, z = gets.split.map(&:to_i)\n\t\tvec[y] = 1\n\tend\n\tpos = 0\n\tfor i in 1..n do\n\t\tif vec[i] == 0\n\t\t\tpos = i\n\t\t\tbreak\n\t\tend\n\tend\n\tfor i in 1..n do\n\t\tif pos == i\n\t\t\tnext\n\t\tend\n\t\tputs \"#{pos} #{i}\"\n\tend\nend"}], "negative_code": [{"source_code": "gets.to_i.times do\n\tn, m = gets.split.map(&:to_i)\n\tvec = Array.new(n+1) { 0 }\n\tfor i in 0..m-1 do\n\t\tx, y, z = gets.split.map(&:to_i)\n\t\tvec[y] = 1\n\tend\n\tpos = 1;\n\tfor i in 1..n do\n\t\tif vec[i] == 0\n\t\t\tpos = 1\n\t\t\tbreak\n\t\tend\n\tend\n\tfor i in 1..n do\n\t\tif pos == i\n\t\t\tnext\n\t\tend\n\t\tputs \"#{pos} #{i}\"\n\tend\nend"}], "src_uid": "dd3ba43eb0261ccdb7269a2696a042da"} {"nl": {"description": "This is an easier version of the problem. In this version, $$$n \\le 2000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\\min(x_a, x_b) \\le x_c \\le \\max(x_a, x_b)$$$, $$$\\min(y_a, y_b) \\le y_c \\le \\max(y_a, y_b)$$$, and $$$\\min(z_a, z_b) \\le z_c \\le \\max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\\frac{n}{2}$$$ snaps.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$2 \\le n \\le 2000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \\le x_i, y_i, z_i \\le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.", "output_spec": "Output $$$\\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \\le a_i, b_i \\le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.", "sample_inputs": ["6\n3 1 0\n0 3 0\n2 2 0\n1 0 0\n1 3 0\n0 1 0", "8\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n2 2 2\n3 2 2\n2 3 2\n2 2 3"], "sample_outputs": ["3 6\n5 1\n2 4", "4 5\n1 6\n2 7\n3 8"], "notes": "NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed. "}, "positive_code": [{"source_code": "def calcs(x,y,ax,ay,bx,by) ((ax-x)*(by-y)-(bx-x)*(ay-y))/2.0 end\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\ndef inps() a=gets.chomp.split(\" \")end\ndef copy(a) Marshal.load(Marshal.dump(a)) end\ndef kaizyo(n)(n < 2)? 1 : (2..n).inject(:*) end\ndef scount(a) b=na(a.max+1);a.each{|n|b[n]+=1};return b end\ndef na(n=0,d=0) Array.new(n,d)end\ndef na2(n=0,m=0,d=0) Array.new(n){Array.new(m,d)}end\ndef na3(n=0,m=0,l=0,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\ndef bit(n) n.to_s(2).split(\"\").map(&:to_i) end\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\nn = inp[0]\ndatas = []\nn.times do\n datas << inp\nend\nused = na(n,false)\nn.times do |i|\n next if used[i]\n r = 0\n min = 1e20\n a,b,c = datas[i]\n ((i+1)...n).each do |j|\n next if used[j]\n s = ((a-datas[j][0]).abs+(b-datas[j][1]).abs+(c-datas[j][2]).abs)\n if(s < min)\n min = s\n r = j\n end\n end\n puts \"#{r+1} #{i+1}\"\n used[i] = true\n used[r] = true\nend"}], "negative_code": [{"source_code": "def calcs(x,y,ax,ay,bx,by) ((ax-x)*(by-y)-(bx-x)*(ay-y))/2.0 end\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\ndef inps() a=gets.chomp.split(\" \")end\ndef copy(a) Marshal.load(Marshal.dump(a)) end\ndef kaizyo(n)(n < 2)? 1 : (2..n).inject(:*) end\ndef scount(a) b=na(a.max+1);a.each{|n|b[n]+=1};return b end\ndef na(n=0,d=0) Array.new(n,d)end\ndef na2(n=0,m=0,d=0) Array.new(n){Array.new(m,d)}end\ndef na3(n=0,m=0,l=0,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\ndef bit(n) n.to_s(2).split(\"\").map(&:to_i) end\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\nn = inp[0]\ndatas = []\nn.times do\n datas << inp\nend\nused = na(n,false)\nn.times do |i|\n next if used[i]\n r = 0\n min = 1e20\n a,b,c = datas[i]\n ((i+1)...n).each do |j|\n next if used[j]\n s = ((a-datas[j][0])+(b-datas[j][1])+(c-datas[j][2])).abs\n if(s < min)\n min = s\n r = j\n end\n end\n puts \"#{r+1} #{i+1}\"\n used[i] = true\n used[r] = true\nend"}, {"source_code": "def calcs(x,y,ax,ay,bx,by) ((ax-x)*(by-y)-(bx-x)*(ay-y))/2.0 end\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\ndef inps() a=gets.chomp.split(\" \")end\ndef copy(a) Marshal.load(Marshal.dump(a)) end\ndef kaizyo(n)(n < 2)? 1 : (2..n).inject(:*) end\ndef scount(a) b=na(a.max+1);a.each{|n|b[n]+=1};return b end\ndef na(n=0,d=0) Array.new(n,d)end\ndef na2(n=0,m=0,d=0) Array.new(n){Array.new(m,d)}end\ndef na3(n=0,m=0,l=0,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\ndef bit(n) n.to_s(2).split(\"\").map(&:to_i) end\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\nn = inp[0]\ndatas = []\nn.times do\n datas << inp\nend\nused = na(n,false)\nn.times do |i|\n next if used[i]\n r = 0\n min = 1e20\n a,b,c = datas[i]\n ((i+1)...n).each do |j|\n next if used[j]\n s = ((a-datas[j][0]+1e-20)*(b-datas[j][1]+1e-20)*(c-datas[j][2]+1e-20)).abs\n if(s < min)\n min = s\n r = j\n end\n end\n puts \"#{r+1} #{i+1}\"\n used[i] = true\n used[r] = true\nend"}], "src_uid": "47a9d72651f1407de89e28fb4b142367"} {"nl": {"description": "Victor has a 24-hour clock that shows the time in the format \"HH:MM\" (00 $$$\\le$$$ HH $$$\\le$$$ 23, 00 $$$\\le$$$ MM $$$\\le$$$ 59). He looks at the clock every $$$x$$$ minutes, and the clock is currently showing time $$$s$$$. How many different palindromes will Victor see in total after looking at the clock every $$$x$$$ minutes, the first time being at time $$$s$$$?For example, if the clock starts out as 03:12 and Victor looks at the clock every $$$360$$$ minutes (i.e. every $$$6$$$ hours), then he will see the times 03:12, 09:12, 15:12, 21:12, 03:12, and the times will continue to repeat. Here the time 21:12 is the only palindrome he will ever see, so the answer is $$$1$$$.A palindrome is a string that reads the same backward as forward. For example, the times 12:21, 05:50, 11:11 are palindromes but 13:13, 22:10, 02:22 are not.", "input_spec": "The first line of the input contains an integer $$$t$$$ ($$$1 \\leq t \\leq 100$$$) — the number of test cases. The description of each test case follows. The only line of each test case contains a string $$$s$$$ of length $$$5$$$ with the format \"HH:MM\" where \"HH\" is from \"00\" to \"23\" and \"MM\" is from \"00\" to \"59\" (both \"HH\" and \"MM\" have exactly two digits) and an integer $$$x$$$ ($$$1 \\leq x \\leq 1440$$$) — the number of minutes Victor takes to look again at the clock.", "output_spec": "For each test case, output a single integer — the number of different palindromes Victor will see if he looks at the clock every $$$x$$$ minutes starting from time $$$s$$$.", "sample_inputs": ["6\n\n03:12 360\n\n00:00 1\n\n13:22 2\n\n15:15 10\n\n11:11 1440\n\n22:30 27"], "sample_outputs": ["1\n16\n10\n0\n1\n1"], "notes": "NoteThe first test case is explained in the statement."}, "positive_code": [{"source_code": "def solve(time, period)\n period = period.to_i\n h, m = time.split(\":\").map(&:to_i)\n a = h * 60 + m\n\n hash = Hash.new(false)\n\n result = 0\n\n while !hash[a]\n result += 1 if palindrome?(a)\n hash[a] = true\n a = (a + period) % 1440\n end\n\n result\nend\n\ndef palindrome?(n)\n a = n / 60\n b = n - 60 * a\n\n a = a.to_s\n while a.size < 2\n a = \"0\" + a\n end\n\n b = b.to_s\n while b.size < 2\n b = \"0\" + b\n end\n\n s = [a, b].join\n\n s.reverse == s\nend\n\n\nn = gets.to_i\n\nn.times do\n time, period = gets.split\n\n p solve(time, period)\nend\n"}, {"source_code": "$cnt = []\r\n\r\ndef solve\r\n s = gets.split('')\r\n hh = []\r\n mm = []\r\n hh.append(s[0])\r\n hh.append(s[1])\r\n mm.append(s[3])\r\n mm.append(s[4])\r\n h = 0\r\n m = 0\r\n k = 0\r\n i = 6\r\n hh.each do |x|\r\n h = h * 10\r\n dig = x.to_i\r\n h = h + dig\r\n end\r\n mm.each do |x|\r\n m = m * 10\r\n dig = x.to_i\r\n m = m + dig\r\n end\r\n while i < s.length - 1\r\n k = k * 10\r\n dig = s[i].to_i\r\n k = k + dig\r\n i = i + 1\r\n end\r\n guy = (h * 100) + m\r\n ans = 0\r\n while $cnt[guy] == 0\r\n $cnt[guy] = 1\r\n s = []\r\n if h < 10\r\n s.append(0)\r\n s.append(h)\r\n else \r\n s.append(h / 10)\r\n s.append(h % 10)\r\n end\r\n if m < 10\r\n s.append(0)\r\n s.append(m)\r\n else \r\n s.append(m / 10)\r\n s.append(m % 10)\r\n end\r\n if s[0] == s[3] and s[1] == s[2]\r\n ans = ans + 1\r\n end\r\n m = m + k\r\n h = h + (m / 60)\r\n h = h % 24\r\n m = m % 60\r\n guy = (h * 100) + m\r\n end\r\n puts ans\r\n while $cnt[guy] != 0\r\n $cnt[guy] = 0\r\n m = m + k\r\n h = h + (m / 60)\r\n h = h % 24\r\n m = m % 60\r\n guy = (h * 100) + m\r\n end\r\nend\r\n\r\nmaxn = 100005\r\nmaxn.times do\r\n $cnt.append(0)\r\nend\r\ngets.to_i.times do\r\n solve\r\nend"}, {"source_code": "gets.to_i.times do\r\n t, n = gets.chomp.split(\" \")\r\n n = n.to_i\r\n h = n / 60\r\n m = n % 60\r\n hash = {}\r\n ans = 0\r\n i = 0\r\n while hash[t].nil?\r\n hash[t] = i\r\n ans += 1 if t == t.reverse\r\n s1 = (t[0,2].to_i + h) % 24\r\n s2 = (t[3,2].to_i + m)\r\n if s2 >= 60\r\n s1 += 1\r\n s1 %= 24\r\n s2 %= 60\r\n end\r\n if s1 / 10 == 0\r\n s1 = '0' + s1.to_s\r\n else\r\n s1 = s1.to_s\r\n end\r\n if s2 / 10 == 0\r\n s2 = '0' + s2.to_s\r\n else\r\n s2 = s2.to_s\r\n end\r\n t[0,2] = s1\r\n t[3,2] = s2\r\n i += 1\r\n end\r\n puts ans\r\nend\r\n"}, {"source_code": "gets.to_i.times do\n ans = 0\n a, b = gets.chomp.split\n h, m = a.split(':').map(&:to_i)\n hh = h\n mm = m\n b = b.to_i\n\n loop do\n g = ''\n g += '0' if h < 10\n g += h.to_s\n g += '0' if m < 10\n g += m.to_s\n ans += 1 if g == g.reverse\n\n h = (h + (m + b) / 60) % 24\n m = (m + b) % 60\n\n break if h == hh && m == mm\n end\n puts ans\nend\n"}, {"source_code": "require \"set\"\r\npalindromes = Set.new\r\n\r\n0.upto(23).each do |h|\r\n 0.upto(59).each do |m|\r\n time = \"%02d:%02d\" % [h, m]\r\n if time == time.reverse\r\n palindromes << h * 60 + m\r\n end\r\n end\r\nend\r\n\r\ngets.to_i.times do\r\n s, x = gets.split\r\n h, m = s.split(\":\").map(&:to_i)\r\n x = x.to_i\r\n start = h * 60 + m\r\n now = start\r\n count = 0\r\n loop do\r\n count += 1 if palindromes.include?(now)\r\n now += x\r\n now %= 1440\r\n break if now == start\r\n end\r\n p count\r\nend\r\n"}, {"source_code": "main = -> {\r\n times = []\r\n (0 ... 24).each do |h|\r\n h0 = h / 10\r\n h1 = h % 10\r\n (0 ... 60).each do |m|\r\n m0 = m / 10\r\n m1 = m % 10\r\n times << (h0 == m1 && h1 == m0)\r\n end\r\n end\r\n \r\n t = int\r\n t.times do\r\n c, _x = strings\r\n x = _x.to_i\r\n time = c[0, 2].to_i * 60 + c[3, 2].to_i\r\n visited = [false] * (24 * 60)\r\n count = 0\r\n until visited[time]\r\n visited[time] = true\r\n count += 1 if times[time]\r\n time = (time + x) % (24 * 60)\r\n end\r\n puts count\r\n end\r\n}\r\n\r\nDEBUG = false\r\nMOD = 10**9+7\r\nINF = 10**18\r\n$yesno = %w(No Yes)\r\n$YesNo = %w(No Yes)\r\n$YESNO = %w(NO YES)\r\n\r\nrequire \"set\"\r\nrequire \"prime\"\r\n\r\ndef int; gets.to_s.to_i end\r\ndef ints; gets.to_s.split.map { |s| s.to_i } end\r\ndef int1s; gets.to_s.split.map { |s| s.to_i - 1 } end\r\ndef float; gets.to_s.to_f end\r\ndef floats; gets.to_s.split.map { |s| s.to_f } end\r\ndef array_of(&convert); gets.to_s.split.map(&convert) end\r\ndef string; gets.to_s.chomp end\r\ndef strings; gets.to_s.split end\r\ndef rep(n, &b); Array.new(n, &b) end\r\ndef yes; puts $yesno[1] end\r\ndef no; puts $yesno[0] end\r\ndef yesno t; puts $yesno[t ? 1 : 0] end\r\ndef YesNo t; puts $YesNo[t ? 1 : 0] end\r\ndef YESNO t; puts $YESNO[t ? 1 : 0] end\r\ndef zip(xs, *yss); Enumerator.new { |y| xs.zip(*yss) { |a| y.yield(*a) } } end\r\ndef max(*xs, &block); block_given? ? xs.max_by(&block) : xs.max end\r\ndef min(*xs, &block); block_given? ? xs.min_by(&block) : xs.min end\r\ndef minmax(*xs, &block); block_given? ? xs.minmax_by(&block) : xs.minmax end\r\ndef gcd(*xs); xs.inject(0, :gcd) end\r\ndef matrix(h, w, fill=nil, &block); return Array.new(h) { Array.new(w, &block) } if block_given?; Array.new(h) { [fill] * w } end\r\ndef debug(x = nil); STDERR.puts (block_given? ? yield(x) : x).inspect if DEBUG; x end\r\ndef debug_grid(grid, width = 1); grid.each { |row| STDERR.puts row.map { |x| x.inspect.ljust(width) }.join(\"\") } if DEBUG; grid end\r\ndef if_debug; yield if DEBUG end\r\n\r\nmodule Boolean\r\n def coerce(other); [other, to_i] end\r\n def +@; to_i end\r\n def to_int; to_i end\r\n def *(other); to_i * other end\r\nend\r\n\r\nclass TrueClass\r\n include Boolean\r\n def to_i; 1 end\r\nend\r\n\r\nclass FalseClass\r\n include Boolean\r\n def to_i; 0 end\r\nend\r\n\r\nclass Integer\r\n def div_ceil(y); (self + y - 1) / y end\r\n def mod_inv(mod = MOD); pow(mod-2, mod) end\r\n def mod_div(y, mod = MOD); self * y.mod_inv(mod) % mod end\r\n def mod_nCr(r, mod = MOD); x = y = 1; (1..r).each { |i| x = x * (self + 1 - i) % mod; y = y * i % mod }; x.mod_div(y, mod); end\r\n def factorial(mod = MOD); (2..self).inject(1) { |f, x| f * x % mod } end\r\n def popcount; x = self; c = 0; while x > 0; c += 1 if x & 1 == 1; x >>= 1 end; c end #TODO: faster\r\n def bitbrute(&block); (1< 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\r\n def lower_bound; ac, wa = self.end_open, self.begin; while ac - wa > 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\r\n def shakutori(r2, &pred); Enumerator.new { |y| j, r = r2.begin, r2.end_open; each { |i| j += 1 while j + 1 < r and pred[i, j+1]; y.yield(i, j) } }; end\r\n def widest(&block); Enumerator.new { |y| j, n = self.begin, self.end_open; each { |i| j += 1 while j < n and block[i, j]; y.yield(i, j) if block[i, j] } } end\r\nend\r\n\r\nclass Array\r\n def power(&block); (0 ... 1 << size).each(&block) end\r\n def sorted_merge(other); a = []; i = j = 0; n, m = size, other.size; if j < m and other[j] < self[i]; a << other[j]; j += 1 else; a << self[i]; i += 1 end while i < n; a.push(*other[j..-1]) if j < m; a end\r\n def upper_bound; ac, wa = 0, size; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\r\n def lower_bound; ac, wa = size, 0; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\r\n def cum(*xs, &op); a = []; a << xs[0] if xs.size > 0; a << x = self[0]; (1...size).each { |i| a << x = op[x, self[i]] }; a end\r\n def cumdiff(range); self[range.end_open] - self[range.begin]; end\r\n def compress(kinds = uniq.sort!); map { |x| kinds.bsearch_index { _1 >= x } }; end\r\nend\r\n\r\nmodule Enumerable\r\n def sorted_uniq; x = nil; filter { |y| c = x === y; x = y; !c } end\r\n def cumsum; ys = [0]; each { |x| ys << x + ys[-1] }; ys end\r\nend\r\n\r\nmain[]"}], "negative_code": [], "src_uid": "c601769062070ab983e1d4c9942cdd39"} {"nl": {"description": "A coordinate line has n segments, the i-th segment starts at the position li and ends at the position ri. We will denote such a segment as [li, ri].You have suggested that one of the defined segments covers all others. In other words, there is such segment in the given set, which contains all other ones. Now you want to test your assumption. Find in the given set the segment which covers all other segments, and print its number. If such a segment doesn't exist, print -1.Formally we will assume that segment [a, b] covers segment [c, d], if they meet this condition a ≤ c ≤ d ≤ b. ", "input_spec": "The first line contains integer n (1 ≤ n ≤ 105) — the number of segments. Next n lines contain the descriptions of the segments. The i-th line contains two space-separated integers li, ri (1 ≤ li ≤ ri ≤ 109) — the borders of the i-th segment. It is guaranteed that no two segments coincide.", "output_spec": "Print a single integer — the number of the segment that covers all other segments in the set. If there's no solution, print -1. The segments are numbered starting from 1 in the order in which they appear in the input.", "sample_inputs": ["3\n1 1\n2 2\n3 3", "6\n1 5\n2 3\n1 10\n7 10\n7 7\n10 10"], "sample_outputs": ["-1", "3"], "notes": null}, "positive_code": [{"source_code": "#CF Round 149. Div II Prob. B - Big Segment\nn = gets.chomp.to_i\nseg, curr = gets.chomp.split(/ /).map(&:to_i), 1\nmin, max = seg[0], seg[1]\n(2..n).each {|i|\n nextSeg = gets.chomp.split(/ /).map(&:to_i)\n min, max = [min, nextSeg[0]].min, [max, nextSeg[1]].max\n seg, curr = nextSeg, i if seg[0]> nextSeg[0] || seg[1] < nextSeg[1]\n}\nputs (min < seg[0] || max > seg[1]) ? \"-1\" : curr.to_s"}, {"source_code": "n = gets.strip.to_i\n\nmin = nil\nmax = nil\narray = []\n\n1.upto(n) do\n left, right = gets.strip.split(' ').map(&:to_i)\n\n min ||= left\n max ||= right\n\n min = left if left < min\n max = right if right > max\n\n array << [left, right]\nend\n\nresult = -1\narray.each_with_index do |row, index|\n if row[0] <= min and row[1] >= max\n result = index + 1\n break\n end\nend\n\nputs result\n"}, {"source_code": "gets\na = readlines.map(&:chomp).map(&:split).each {|x| x.map!(&:to_i)}\ni = a.index [a.transpose[0].min, a.transpose[1].max]\nputs i.nil? ? -1 : i+1\n"}, {"source_code": "# Link: http://codeforces.com/problemset/problem/242/B\n# Ý tưởng:\n# Mỗi segment sẽ là 1 array [l, r], lưu hết segments vào 1 array.\n# Đầu tiên ta gắn min_l, max_r lần lượt là l và r của segments đầu tiên.\n# Lặp từ phần tử thứ 2 tới cuối mảng segments để tìm ra min_l và max_r.\n# Tiếp tục lặp lần nữa từ đầu tới cuối mảng, kiểm tra xem trong mảng có phần tử nào có l, r\n# bằng với min_l và max_r vừa tìm được không.\n# Nếu có tồn tại ta ngay lập tức trả về index của phần tử đó + thêm 1 (vì thứ tự của segment trong input bắt đầu từ 1).\n# Nếu không tìm ra phần tử nào thì trả về -1\n\nnum_of_segments = gets.chomp.to_i\nsegments_description = []\n\n# save descriptions of the segments ([left, right]) into an array\nfor i in 0..(num_of_segments - 1)\n segment = gets.chomp.split.map(&:to_i)\n segments_description << segment\nend\n\ndef index_of_segment_container(segments)\n min_l, max_r = segments[0][0], segments[0][1]\n\n for i in 1..(segments.length - 1)\n min_l = segments[i][0] if segments[i][0] < min_l\n max_r = segments[i][1] if segments[i][1] > max_r\n end\n\n for i in 0..(segments.length - 1)\n return i + 1 if segments[i][0] == min_l && segments[i][1] == max_r\n end\n\n return -1\nend\n\nputs index_of_segment_container(segments_description)"}, {"source_code": "num_of_segments = gets.chomp.to_i\nsegments_description = []\n\n# save descriptions of the segments ([left, right]) into an array\nfor i in 0..(num_of_segments - 1)\n segment = gets.chomp.split.map(&:to_i)\n segments_description << segment\nend\n\ndef index_of_segment_container(segments)\n min_l = segments[0][0]\n max_l = segments[0][1]\n\n for i in 1..(segments.length - 1)\n min_l = segments[i][0] if segments[i][0] < min_l\n max_l = segments[i][1] if segments[i][1] > max_l\n end\n\n for i in 0..(segments.length - 1)\n return i + 1 if segments[i][0] == min_l && segments[i][1] == max_l\n end\n\n return -1\nend\n\nputs index_of_segment_container(segments_description)\n"}, {"source_code": "gets\na = readlines.map(&:chomp).map(&:split).each {|x| x.map!(&:to_i)}\ni = a.index [a.transpose[0].min, a.transpose[1].max]\nputs i.nil? ? -1 : i+1\n"}, {"source_code": "require 'set'\n\ndef read_next_line\n gets.split.map { |x| x.to_i }\nend\n\nmin, max = 10 ** 9 + 1, 0\nmins, maxs = Set.new, Set.new\nfor i in 1..gets.to_i\n a, b = read_next_line\n if a < min\n min = a\n mins.clear\n mins << i\n elsif a == min\n mins << i\n end\n if b > max\n max = b\n maxs.clear\n maxs << i\n elsif b == max\n maxs << i\n end\nend\nr = mins & maxs\nputs(r.empty? ? -1 : r.first)"}, {"source_code": "gets\na = readlines.map(&:chomp).map(&:split).each {|x| x.map!(&:to_i)}\ni = a.index [a.transpose[0].min, a.transpose[1].max]\nputs i.nil? ? -1 : i+1\n"}, {"source_code": "gets\na = readlines.map(&:chomp).map(&:split).each {|x| x.map!(&:to_i)}\ni = a.index [a.transpose[0].min, a.transpose[1].max]\nputs i.nil? ? -1 : i+1\n"}, {"source_code": "def check(number)\n segments = []\n (1..number.to_i).each do |i|\n segments << gets.split().map(&:to_i)\n end\n\n step_1 = 0\n step_2 = 0\n\n for i in (0..segments.size - 1)\n next unless segments[i + 1]\n if (segments[step_1][0] >= segments[i + 1][0]) && (segments[step_2][1] <= segments[i + 1][1])\n step_1 = i + 1\n step_2 = i + 1\n elsif segments[step_2][1] < segments[i + 1][1]\n step_2 = i + 1\n elsif segments[step_1][0] > segments[i + 1][0]\n step_1 = i + 1\n end\n end\n \n puts (step_1 == step_2) ? step_1 + 1 : -1\nend\n\ncheck(gets)\n"}, {"source_code": "gets\na = readlines.map(&:chomp).map(&:split).each {|x| x.map!(&:to_i)}\ni = a.index [a.transpose[0].min, a.transpose[1].max]\nputs i.nil? ? -1 : i+1\n"}, {"source_code": "a=Array.new{|el| el=Array.new}\ngets.to_i.times{a<= max\n seg = i\n\tx = l\n\ty = r\n elsif l < x || r > y\n seg = -1\n end\n min = [l, min].min\n max = [r, max].max\nend\nprint seg"}, {"source_code": "gets\na = readlines.map(&:chomp).map(&:split).each {|x| x.map!(&:to_i)}\ni = a.index [a.transpose[0].min, a.transpose[1].max]\nputs i.nil? ? -1 : i+1\n"}, {"source_code": "gets\na = readlines.map(&:chomp).map(&:split).each {|x| x.map!(&:to_i)}\ni = a.index [a.transpose[0].min, a.transpose[1].max]\nputs i.nil? ? -1 : i+1"}, {"source_code": "gets\na = readlines.map(&:chomp).map(&:split).each {|x| x.map!(&:to_i)}\ni = a.index [a.transpose[0].min, a.transpose[1].max]\nputs i.nil? ? -1 : i+1"}, {"source_code": "n = gets.to_i\nar = []\nn.times{ar< max_right || max_right < 0\nend\n\ncheck_result = [min_left, max_right]\n\nresult = (segments.find_index(check_result) || -2) + 1\n\nputs result\n"}, {"source_code": "segment_number = gets.to_i\n\nsegments = []\n\nfor i in (0..segment_number - 1)\n segment = gets.split(' ')\n left_coordinate = segment[0].to_i\n right_coordinate = segment[1].to_i\n segments << [left_coordinate, right_coordinate]\nend\n\nmin_left = -1\nmax_right = -1\n\nsegments.each do |segment|\n min_left = segment[0] if segment[0] < min_left || min_left < 0\n max_right = segment[1] if segment[1] > max_right || max_right < 0\nend\n\ncheck_result = [min_left, max_right]\n\nresult = (segments.index { |segment| segment[0] == check_result[0] && segment[1] == check_result[1] } || -2) + 1\n\nputs result\n"}, {"source_code": "segment_number = gets.to_i\n\nsegments = []\n\nfor i in (0..segment_number - 1)\n segment = gets.split(' ')\n left_coordinate = segment[0].to_i\n right_coordinate = segment[1].to_i\n segments << [left_coordinate, right_coordinate]\nend\n\nmin_left = segments.map(&:first).min\nmax_right = segments.map(&:last).max\n\ncheck_result = [min_left, max_right]\n\nresult = (segments.find_index(check_result) || -2) + 1\n\nputs result\n"}, {"source_code": "gets\na = readlines.map(&:chomp).map(&:split).each {|x| x.map!(&:to_i)}\ni = a.index [a.transpose[0].min, a.transpose[1].max]\nputs i.nil? ? -1 : i+1\n"}, {"source_code": "gets\na = readlines.map(&:chomp).map(&:split).each {|x| x.map!(&:to_i)}\ni = a.index [a.transpose[0].min, a.transpose[1].max]\nputs i.nil? ? -1 : i+1\n"}], "negative_code": [{"source_code": "def check(number)\n segments = []\n (1..number.to_i).each do |i|\n segments << gets.split().map(&:to_i)\n end\n\n step = 1\n current_1 = segments[0].min\n current_2 = segments[0].max\n for i in (1..segments.size - 1)\n break if segments[i + 1].nil?\n if (segments[i].min <= current_1 ) && (segments[i].max >= current_2)\n current_1 = segments[i].min\n current_2 = segments[i].max\n step = i + 1\n else\n step = -1\n end\n end\n puts step\nend\n\ncheck(gets)\n"}, {"source_code": "def check(number)\n segments = []\n (1..number.to_i).each do |i|\n segments << gets.split().map(&:to_i)\n end\n\n step = 1\n current_1 = segments[0].min\n current_2 = segments[0].max\n for i in (1..segments.size - 1)\n break if segments[i + 1].nil?\n if (segments[i].min <= current_1 ) && (segments[i].max >= current_2)\n current_1 = segments[i].min\n current_2 = segments[i].max\n step = i + 1\n elsif (segments[i].min > current_1 ) && (segments[i].max > current_2)\n step = -1\n end\n end\n puts step\nend\n\ncheck(gets)\n"}, {"source_code": "def check(number)\n segments = []\n (1..number.to_i).each do |i|\n segments << gets.split().map(&:to_i)\n end\n\n step = -1\n current_1 = segments[0].min\n current_2 = segments[0].max\n for i in (1..segments.size - 1)\n break if segments[i + 1].nil?\n if (segments[i].min <= current_1 ) && (segments[i].max >= current_2)\n current_1 = segments[i].min\n current_2 = segments[i].max\n step = i + 1\n end\n end\n puts step\nend\n\ncheck(gets)\n"}, {"source_code": "def check(number)\n segments = []\n (1..number.to_i).each do |i|\n segments << gets.split().map(&:to_i)\n end\n\n step_1 = 0\n step_2 = 0\n\n for i in (0..segments.size - 1)\n next unless segments[i + 1]\n if (segments[step_1][0] >= segments[i + 1][0]) && (segments[step_2][1] < segments[i + 1][1])\n step_1 = i + 1\n step_2 = i + 1\n elsif segments[step_2][1] < segments[i + 1][1]\n step_2 = i + 1\n elsif segments[step_1][0] > segments[i + 1][0]\n step_1 = i + 1\n end\n end\n \n puts (step_1 == step_2) ? step_1 + 1 : -1\nend\n\ncheck(gets)\n"}, {"source_code": "n = gets.to_i\nmin = 1000000001\nmax = 0\nseg = -1\nx, y = [0, 0]\nfor i in 1..n\n l, r = gets.split.map(&:to_i)\n if l <= min && r >= max\n seg = i\n\tx = l\n\ty = r\n elsif l < x && r > y\n seg = -1\n end\n min = [l, min].min\n max = [r, max].max\nend\nprint seg"}, {"source_code": "#CF Round 149. Div II Prob. B - Big Segment\nn = gets.chomp.to_i\nseg, curr, min, max = gets.chomp.split(/ /).map(&:to_i), 1, 1000000000, 0;\n(2..n).each {|i|\n nextSeg = gets.chomp.split(/ /).map(&:to_i)\n min, max = [min, nextSeg[0]].min, [max, nextSeg[1]].max\n seg, curr = nextSeg, i if seg[0]> nextSeg[0] || seg[1] < nextSeg[1]\n}\nputs (min < seg[0] || max > seg[1]) ? \"-1\" : curr.to_s"}], "src_uid": "e702594a8e993e135ea8ca78eb3ecd66"} {"nl": {"description": "Polycarpus is an amateur programmer. Now he is analyzing a friend's program. He has already found there the function rangeIncrement(l, r), that adds 1 to each element of some array a for all indexes in the segment [l, r]. In other words, this function does the following: function rangeIncrement(l, r) for i := l .. r do a[i] = a[i] + 1Polycarpus knows the state of the array a after a series of function calls. He wants to determine the minimum number of function calls that lead to such state. In addition, he wants to find what function calls are needed in this case. It is guaranteed that the required number of calls does not exceed 105.Before calls of function rangeIncrement(l, r) all array elements equal zero.", "input_spec": "The first input line contains a single integer n (1 ≤ n ≤ 105) — the length of the array a[1... n]. The second line contains its integer space-separated elements, a[1], a[2], ..., a[n] (0 ≤ a[i] ≤ 105) after some series of function calls rangeIncrement(l, r). It is guaranteed that at least one element of the array is positive. It is guaranteed that the answer contains no more than 105 calls of function rangeIncrement(l, r).", "output_spec": "Print on the first line t — the minimum number of calls of function rangeIncrement(l, r), that lead to the array from the input data. It is guaranteed that this number will turn out not more than 105. Then print t lines — the descriptions of function calls, one per line. Each line should contain two integers li, ri (1 ≤ li ≤ ri ≤ n) — the arguments of the i-th call rangeIncrement(l, r). Calls can be applied in any order. If there are multiple solutions, you are allowed to print any of them.", "sample_inputs": ["6\n1 2 1 1 4 1", "5\n1 0 1 0 1"], "sample_outputs": ["5\n2 2\n5 5\n5 5\n5 5\n1 6", "3\n1 1\n3 3\n5 5"], "notes": "NoteThe first sample requires a call for the entire array, and four additional calls: one for the segment [2,2] (i.e. the second element of the array), three for the segment [5,5] (i.e. the fifth element of the array). "}, "positive_code": [{"source_code": "gets;s=[];z=[];a=gets.split.map &:to_i;a<<0;0.upto(a.size-1){|i|(a[i-1]-a[i]).times{z<<[s.pop,i]};(a[i]-a[i-1]).times{s< num do\n\t\tr<< idx\n\t\tcurr -= 1\n\tend\nend\n\nputs l.length\nl.length.times do |idx|\n\tputs \"#{l[idx]} #{r[idx]}\"\nend\n"}, {"source_code": "gets;s=[];z=[];a=gets.split.map &:to_i;a<<0;0.upto(a.size-1){|i|(a[i-1]-a[i]).times{z<<[s.pop,i]};(a[i]-a[i-1]).times{s<'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\nrequire 'prime'\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\ndef big(a,b) return (a>b)? a:b end\n\ndef small(a,b) return (a n-2)\n\t\treturn false\n\telsif ( board[(y+1)*(n+1)+x] != \"#\" or\n\t\t board[(y+2)*(n+1)+x] != \"#\" or\n\t\t board[(y+1)*(n+1)+(x-1)] != \"#\" or\n\t\t board[(y+1)*(n+1)+(x+1)] != \"#\" )\n\t\treturn false\n\telse\n\t\treturn true\n\tend\nend\n\ndef check_board (board, n)\n\tfor y in 0..n\n\t\tfor x in 0..n\n\t\t\tif (board[y*(n+1)+x] == \"#\")\n\t\t\t\tif (cross?(board, x, y, n))\n\t\t\t\t\tboard[(y+1)*(n+1)+x] = \".\"\n\t\t\t\t\tboard[(y+2)*(n+1)+x] = \".\"\n\t\t\t\t\tboard[(y+1)*(n+1)+(x-1)] = \".\"\n\t\t\t\t\tboard[(y+1)*(n+1)+(x+1)] = \".\"\n\t\t\t\telse\n\t\t\t\t\treturn \"NO\"\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\tend\n\treturn \"YES\"\nend\n\nboard = []\nn = (gets()).to_i - 1\nfor i in 0..n\n\t(gets()).each_char{ |c| board << c}\n\tboard.pop\nend\n\nputs check_board(board, n)"}], "negative_code": [{"source_code": "a=gets.chomp.to_i\nc=(\".\"*(a+4)).split(\"\")\nb=[]\nb<=k then\n back=i\n ans+=1\n end\nend\np ans\n"}, {"source_code": "n,k,x,*h=(gets+gets).split.map &:to_i;h< K)\n ans += (numbers[i] - 1 - last_present) / K\n end\n last_present = numbers[i]\nend\n\nans += C\nputs ans\n"}, {"source_code": "n,k,x,*h=(gets+gets).split.map &:to_i;h<= K\n p = 0\n ret += 1\n elsif h[i]\n ret += 1\n p = 0\n end\n p += 1\nend\nputs ret \n"}, {"source_code": "n,k,x,*h=(gets+gets).split.map &:to_i;h<=k then\n back=i\n ans+=1\n end\nend\np ans\n"}, {"source_code": "N, K = gets.split.map(&:to_i)\nt, *H = gets.split.map(&:to_i)\nh = Array.new(N, false)\nh[0] = true\nH.each { |i| h[i] = true }\nret = 0\np = 0\n(0...N).each do |i|\n if p >= K\n p = 0\n ret += 1\n elsif h[i]\n ret += 1\n p = 0\n end\n p += 1\nend\nputs ret \n"}], "src_uid": "07b750dbf7f942eab80d4260103c7472"} {"nl": {"description": "Fox Ciel starts to learn programming. The first task is drawing a fox! However, that turns out to be too hard for a beginner, so she decides to draw a snake instead.A snake is a pattern on a n by m table. Denote c-th cell of r-th row as (r, c). The tail of the snake is located at (1, 1), then it's body extends to (1, m), then goes down 2 rows to (3, m), then goes left to (3, 1) and so on.Your task is to draw this snake for Fox Ciel: the empty cells should be represented as dot characters ('.') and the snake cells should be filled with number signs ('#').Consider sample tests in order to understand the snake pattern.", "input_spec": "The only line contains two integers: n and m (3 ≤ n, m ≤ 50). n is an odd number.", "output_spec": "Output n lines. Each line should contain a string consisting of m characters. Do not output spaces.", "sample_inputs": ["3 3", "3 4", "5 3", "9 9"], "sample_outputs": ["###\n..#\n###", "####\n...#\n####", "###\n..#\n###\n#..\n###", "#########\n........#\n#########\n#........\n#########\n........#\n#########\n#........\n#########"], "notes": null}, "positive_code": [{"source_code": "R,C=gets.split.map(&:to_i)\nR.times{|i|\n\tif i%4==1\n\t\tputs '.'*(C-1)+'#'\n\telsif i%4==3\n\t\tputs '#'+'.'*(C-1)\n\telse\n\t\tputs '#'*C\n\tend\n}"}, {"source_code": "n,m=gets.split.map &:to_i;puts((['#'*m,'.'*(m-1)+'#','#'*m,'#'+'.'*(m-1)]*n)[0...n])\n"}, {"source_code": "class Snake\n def initialize(n, m)\n @n, @m = n, m\n end\n\n def draw\n rows.each { |row| puts row.join }\n end\n\n private\n\n def rows\n [].tap do |rows|\n @n.times do |idx|\n case\n when idx.even?\n rows << ['#'] * @m\n when rows[idx - 2].nil?, rows[idx - 2].first == '#'\n rows << ['.'] * (@m - 1) + ['#']\n else\n rows << ['#'] + ['.'] * (@m - 1)\n end\n end\n end\n end\nend\n\nn, m = gets.chomp.split.map(&:to_i)\nsnake = Snake.new(n, m)\nsnake.draw\n"}, {"source_code": "n,m=gets.split.map &:to_i;puts((['#'*m,'.'*(m-1)+'#','#'*m,'#'+'.'*(m-1)]*n)[0...n])\n"}, {"source_code": "n, m = gets.split(\" \").map(&:to_i)\n\nn.times do |i|\n\t#puts i\n\tm.times do |j|\n\t\t#print \"#{j} \"\n\t\tif i%2==0 \n\t\t\tprint \"#\"\n\t\telsif i/2%2==0 && j==m-1\n\t\t\tprint \"#\" \n\t\telsif i/2%2==1 && j==0\n\t\t\tprint \"#\"\n\t\telse\n\t\t\tprint \".\"\n\t\tend\n\tend\n\tprint \"\\n\"\nend"}, {"source_code": "n,m=gets.split.map &:to_i;puts((['#'*m,'.'*(m-1)+'#','#'*m,'#'+'.'*(m-1)]*n)[0...n])"}, {"source_code": "n, m = gets.split.map(&:to_i)\nf = 1\nfor i in 0...n\n\tif(i % 2 == 0)\n\t\tm.times do \n\t\t\tprint \"#\"\n\t\tend\n\telse\n\t\tif(f == 1)\n\t\t\t(m - 1).times do\n\t\t\t\tprint \".\"\n\t\t\tend\n\t\t\tprint \"#\"\n\t\t\tf = 0\n\t\telse\n\t\t\tprint \"#\"\n\t\t\t(m - 1).times do\n\t\t\t\tprint \".\"\n\t\t\tend\n\t\t\tf = 1\n\t\tend\n\tend\n\tputs \"\"\nend"}, {"source_code": "n,m = gets.split.map(&:to_i)\ntable = []\nn.times do\n\ttable.push(['.']*m)\nend\n\nfor i in 0...n\n\tif (i%2==0)\n\t\tfor j in 0...m\n\t\t\ttable[i][j] = '#'\n\t\tend\n\telse\n\t\tif (i/2)%2 == 0\n\t\t\ttable[i][-1] = '#'\n\t\telse\n\t\t\ttable[i][0] = '#'\n\t\tend\n\tend\nend\n\nfor i in 0...n\n\tfor j in 0...m\n\t\tprint table[i][j]\n\tend\n\tputs\nend"}, {"source_code": "n,m=gets.split.map &:to_i\nn.times{|i| puts i.even? ? '#'*m : ('.'*(m-1)).insert(-i/2%-2,'#')}"}, {"source_code": "def string_to_int(string, delim = \" \")\n result = []\n string = string.split(delim)\n for i in 0...string.length\n result[i] = string[i].to_i\n end\n return result\nend\n\ninput = string_to_int(gets.chomp)\nn = input[0]\nm = input[1]\n\nsnake = Array.new(n){Array.new(m){\".\"}}\nfor i in 0...snake.length\n\tfor j in 0...snake[0].length\n\t\tif (i+1)%4 == 0\n\t\t\tsnake[i][0] = \"#\"\n\t\t\tbreak\n\t\telsif (i+1)%2 == 0\n\t\t\tsnake[i][snake[0].length - 1] = \"#\"\n\t\t\tbreak\n\t\telse\n\t\t\tsnake[i][j] = \"#\"\n\t\tend\n\tend\nend\n\nfor i in 0...snake.length\n\tfor j in 0...snake[0].length\n\t\tif j != snake[0].length - 1\n\t\t\tprint snake[i][j]\n\t\telse\n\t\t\tputs snake[i][j]\n\t\tend\n\tend\nend\n"}, {"source_code": "n,m = gets.split.map(&:to_i)\n\na = []\ncounter = 0\nfor i in 0..n-1\n a[i] = []\n for j in 0..m-1\n if i%2 == 0\n a[i][j] = \"#\"\n else\n a[i][j] = \".\"\n end\n end\n\n counter+=1 if i%2 != 0\n\n if counter%2 == 0\n a[i][0] = \"#\"\n else\n a[i][-1] = \"#\"\n end\nend\n\n\n\nfor i in 0..n-1\n for j in 0..m-1\n print \"#{a[i][j]}\"\n end\n puts\nend"}, {"source_code": "n, m = $stdin.readline.split.map(&:to_i)\n\nd = 0\nfor i in 1..n\n case d \n when 0,2\n m.times {print '#'}\n when 1\n (m-1).times {print '.'}\n print '#'\n when 3\n print '#'\n (m-1).times {print '.'}\n end\n puts ''\n d = (d+1)%4\nend"}, {"source_code": "#!/usr/bin/ruby\n# coding: utf-8\n\nn, m = gets.split.map(&:to_i)\nn.times {|i|\n if i % 2 == 0\n puts '#' * m\n elsif i % 4 == 1\n puts '.' * (m - 1) + '#'\n elsif\n puts '#' + '.' * (m - 1)\n end\n}"}, {"source_code": "n, m = gets.split.map(&:to_i)\nright_or_left = 0\nn.times do |i|\n x = if i % 2 == 0\n '#' * m\n elsif right_or_left % 2 == 0\n result = '.' * m\n result[-1] = '#'\n right_or_left += 1\n result\n else\n result = '.' * m\n result[0] = '#'\n right_or_left += 1\n result\n end\n puts x\nend\n"}, {"source_code": "n, m = gets.chomp.split.map &:to_i\nind = 'r'\nn.times do |x|\n if (x + 1) % 2 != 0\n m.times do\n print \"#\"\n end\n puts\n else \n if ind == 'r'\n (m - 1).times do\n print '.'\n end\n print '#'\n puts\n ind = 'l'\n else\n print '#'\n (m - 1).times do\n print '.'\n end\n puts\n ind = 'r'\n end\n end\nend\n"}, {"source_code": "n,m=gets.split.map &:to_i;puts((['#'*m,'.'*(m-1)+'#','#'*m,'#'+'.'*(m-1)]*n)[0...n])\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\n(1..n).each { |i|\n (1..m).each { |j|\n if i&1==1\n print '#'\n else\n if (i&3==2 && j==m) || (i&3==0 && j==1)\n print '#'\n else\n print '.'\n end\n end\n }\n print \"\\n\"\n}"}, {"source_code": "n, m = gets.split.map(&:to_i)\n\n(0..n-1).each do |i|\n line = \"\"\n (0..m-1).each do |j|\n if i.even? || (j == m - 1) && (i % 4 == 1) || (j == 0) && (i % 4 == 3)\n line += \"#\"\n else\n line += \".\"\n end\n end\n puts line\nend"}, {"source_code": "n,m=gets.split.map &:to_i;puts((['#'*m,'.'*(m-1)+'#','#'*m,'#'+'.'*(m-1)]*n)[0...n])\n"}, {"source_code": "n, m = gets.chomp.split(/ /).map(&:to_i)\nleft = true\nright = false\n(1..n).each do |row|\n line = nil\n if row.odd?\n line = Array.new(m, \"#\")\n else\n line = Array.new(m, \".\")\n if left && !right\n line[m - 1] = \"#\"\n left = false\n right = true\n elsif right && !left\n line[0] = \"#\"\n left = true\n right = false\n end\n end\n puts line.join\nend\n"}, {"source_code": "a=gets.chomp.split(\" \")\ntok=0\na[0]=a[0].to_i\na[1]=a[1].to_i\nhash=\"\"\na[1].times do\n hash=hash+\"#\"\nend\nto=\"\"\n(a[1]-1).times do\n to=to+\".\"\nend\nto=to+\"#\"\nfour=\"#\"\n(a[1]-1).times do\n four=four+\".\"\nend\n1.upto(a[0]) do |i|\nif (i%2==1)\n puts \"#{hash}\"\nelsif (i%2==0 and tok==0) \n puts \"#{to}\"\n tok=1\nelse \n puts \"#{four}\"\n tok=0\nend\nend"}, {"source_code": "n,m=gets.split.map &:to_i;puts((['#'*m,'.'*(m-1)+'#','#'*m,'#'+'.'*(m-1)]*n)[0...n])\n"}, {"source_code": "h,w=gets.chomp.split.map{|e| e.to_i}\n\nh.times{|i|\n\tif i%2==0 then\n\t\tputs (\"#\"*w)\n\telsif i%4==1 then\n\t\tputs ((\".\"*(w-1))+\"#\")\n\telse\n\t\tputs (\"#\"+(\".\"*(w-1)))\n\tend\n}\n\n"}, {"source_code": "n,m = gets.chomp.split.map(&:to_i)\nj = 0\nn.times.each do |i|\n if i % 2 ==0\n puts '#' * m\n else\n s = '.' * m\n if j % 2 == 0\n s[m-1] = '#'\n else\n s[0] = '#'\n end\n j += 1\n puts s\n end\nend"}, {"source_code": "n, m = gets.split.map { |e| e.to_i }\n\nfor i in 1..n do\n puts '#'.rjust(m, '#') if i.odd?\n puts '#'.rjust(m, '.') if i%4 != 0 and i.even?\n puts '#'.ljust(m, '.') if i%4 == 0\nend\n"}, {"source_code": "#!/usr/bin/ruby\n\ninput = gets.chomp.split(' ').map! &:to_i\n\ndef filled(input)\n puts '#' * input.last\nend\n\ndef points_left(input)\n puts \"#{ '.' * (input.last - 1)}#\"\nend\n\ndef points_right(input)\n puts \"##{ '.' * (input.last - 1 )}\"\nend\n\nsign = -1\n\ninput.first.times do |i|\n if i % 2 == 0\n filled input\n elsif sign > 0\n sign *= -1\n points_right input\n else\n sign *= -1\n points_left input\n end\nend\n"}, {"source_code": "n,m=gets.split(\" \").map {|x| x.to_i}\ni = 1\nwhile i<=n do\n if i%4==0\n print \"#\"\n j = 2\n while j<=m do\n print \".\"\n j+=1\n end\n elsif i%4==2\n j=1\n while j 1 && space < cells\n\t\t\t\t\tprint \".\"\n\t\t\t\telse\n\t\t\t\t\tputs \".\"\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\tend\nend"}, {"source_code": "n,m=gets.split.map &:to_i;puts((['#'*m,'.'*(m-1)+'#','#'*m,'#'+'.'*(m-1)]*n)[0...n])\n"}, {"source_code": "n,m = gets.split(\" \").map(&:to_i)\nflip = 0\nres = []\n(1..n).each do |i|\n if i % 2 != 0\n res << [\"#\"] * m\n else\n row = [\".\"] * (m - 1)\n if flip == 1\n row.unshift(\"#\")\n else\n row << \"#\"\n end\n res << row\n flip ^= 1\n end\nend\nres.each do |r|\n puts r.join\nend"}, {"source_code": "n,m=gets.split.map &:to_i\nn.times{|i| puts i.even? ? '#'*m : ('.'*(m-1)).insert(-i/2%-2,'#')}"}, {"source_code": "h, w = gets.split.map(&:to_i)\nputs (1..h).map { |i| (1..w).map { |j| i % 2 == 0 && i % 4 != 0 && j != w || i % 4 == 0 && j != 1 ? '.' : '#' }.join }\n"}, {"source_code": "$n,$m=STDIN.gets.split.map{|n|n.to_i}\n\ndef getLine(i)\n return \"#\"*$m if i%2==0\n return \".\"*($m-1)+\"#\" if i%4==1\n return \"#\"+\".\"*($m-1)\nend\n\nputs Array.new($n) {|i| getLine(i)+\"\\n\"}.inject(:+)"}, {"source_code": "n, m = gets.split.map(&:to_i)\na = Array.new(n){Array.new(m, \".\")}\nfor i in 0...n\n if i % 2 == 0\n a[i] = Array.new(m, \"#\")\n elsif i % 4 == 1\n a[i][m - 1] = \"#\"\n elsif i % 4 == 3\n a[i][0] = \"#\"\n end\nend\nputs a.map{|x| x.join(\"\")}\n"}, {"source_code": "input = gets.chomp.split\nn = input[0].to_i\nm = input[1].to_i\n\narr = []\nfor i in 0..n-1\n arr[i] = Array.new(m)\n arr[i].fill('.')\nend\n\ni = 0\nwhile i < n do\n arr[i].fill('#')\n i += 2\nend\n\nright = true\ni = 1\nwhile i < n do\n if right\n arr[i][m-1] = '#'\n else\n arr[i][0] = '#'\n end\n right = !right\n i += 2\nend\n\narr.each do |row|\n puts row.join\nend\n"}, {"source_code": "#!/usr/bin/env ruby\n\n\ninput = ARGF.gets\n\nn, m = input.split \" \"\nn = n.to_i\nm = m.to_i\n\n\nright = true\n\nni = 0\nn.times do\n if ni.even?\n puts '#' * m\n else\n if right\n right = false\n print '.' * (m - 1)\n puts '#'\n else\n right = true\n print '#'\n puts '.' * (m-1)\n end\n\n end\n\n\n\n ni += 1 \n\nend\n"}, {"source_code": "a=gets.chomp.split(\" \")\ntok=0\na[0]=a[0].to_i\na[1]=a[1].to_i\nhash=\"\"\na[1].times do\n hash=hash+\"#\"\nend\nto=\"\"\n(a[1]-1).times do\n to=to+\".\"\nend\nto=to+\"#\"\nfour=\"#\"\n(a[1]-1).times do\n four=four+\".\"\nend\n1.upto(a[0]) do |i|\nif (i%2==1)\n puts \"#{hash}\"\nelsif (i%2==0 and tok==0) \n puts \"#{to}\"\n tok=1\nelse \n puts \"#{four}\"\n tok=0\nend\nend"}, {"source_code": "n,m=gets.split.map &:to_i;puts((['#'*m,'.'*(m-1)+'#','#'*m,'#'+'.'*(m-1)]*n)[0...n])"}, {"source_code": "n,m=gets.split.map &:to_i;puts((['#'*m,'.'*(m-1)+'#','#'*m,'#'+'.'*(m-1)]*n)[0...n])\n"}, {"source_code": "n, m = gets.split(' ').map(&:to_i)\n\n1.upto(n) do |i|\n case i.modulo(4)\n when 1, 3\n puts '#'*m\n when 2\n puts \"#{'.'*(m-1)}\\#\"\n when 0\n puts \"\\##{'.'*(m-1)}\"\n end\nend\n"}, {"source_code": "n, m = gets.split(\" \").map &:to_i\n\nright = true\nn.times do |i|\n if i%2 == 0\n puts '#' * m\n else\n str = '.' * (m - 1)\n str.insert(right ? -1 : 0, '#' )\n right = !right\n puts str\n end\nend"}, {"source_code": "a=Array.new\nstr=gets.to_s\na=str.split\nn=a[0].to_i\nm=a[1].to_i\ni=h=0\nwhile i=n\n break\n end\n if h==0\n j=0\n while j= 1\n end\n \n puts ''\nend"}, {"source_code": "n, m = gets.chomp.split(' ').map { |i| i.to_i }\nn.times do |iter|\n if iter.even?\n m.times { print '#' }\n else\n print '#' if iter % 3 == 0\n (m - 1).times { print '.' }\n print '#' if iter % 3 == 1\n end\n \n puts ''\nend"}], "src_uid": "2a770c32d741b3440e7f78cd5670d54d"} {"nl": {"description": "Filya just learned new geometry object — rectangle. He is given a field consisting of n × n unit cells. Rows are numbered from bottom to top with integer from 1 to n. Columns are numbered from left to right with integers from 1 to n. Cell, located at the intersection of the row r and column c is denoted as (r, c). Filya has painted two rectangles, such that their sides are parallel to coordinate axes and each cell lies fully inside or fully outside each of them. Moreover, no cell lies in both rectangles.Later, hedgehog Filya became interested in the location of his rectangles but was unable to find the sheet of paper they were painted on. They were taken by Sonya and now she wants to play a little game with Filya. He tells her a query rectangle and she replies with the number of initial rectangles that lie fully inside the given query rectangle. The query rectangle should match the same conditions as initial rectangles. Rectangle lies fully inside the query if each o its cells lies inside the query.Filya knows Sonya really well, so is sure that if he asks more than 200 questions she will stop to reply.", "input_spec": "The first line of the input contains an integer n (2 ≤ n ≤ 216) — size of the field. For each query an integer between 0 and 2 is returned — the number of initial rectangles that lie fully inside the query rectangle.", "output_spec": "To make a query you have to print \"? x1 y1 x2 y2\" (without quotes) (1 ≤ x1 ≤ x2 ≤ n, 1 ≤ y1 ≤ y2 ≤ n), where (x1, y1) stands for the position of the bottom left cell of the query and (x2, y2) stands for the up right cell of the query. You are allowed to ask no more than 200 queries. After each query you should perform \"flush\" operation and read the answer. In case you suppose you've already determined the location of two rectangles (or run out of queries) you should print \"! x11 y11 x12 y12 x21 y21 x22 y22\" (without quotes), where first four integers describe the bottom left and up right cells of the first rectangle, and following four describe the corresponding cells of the second rectangle. You can print the rectangles in an arbitrary order. After you have printed the answer, print the end of the line and perform \"flush\". Your program should terminate immediately after it print the answer.", "sample_inputs": ["5\n2\n1\n0\n1\n1\n1\n0\n1"], "sample_outputs": ["? 1 1 5 5\n? 1 1 3 3\n? 1 1 3 1\n? 2 2 2 2\n? 3 3 5 5\n? 3 3 3 5\n? 3 3 3 4\n? 3 4 3 5\n! 2 2 2 2 3 4 3 5"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/ruby\nSTDOUT.sync=true\ndef chk(*a)\n\tputs '? %d %d %d %d'%a\n\tgets.to_i\nend\nn=gets.to_i\na=[1,2].map{|z|-(-n..-1).bsearch{|i|chk(-i,1,n,n)>=z}}\nb=[1,2].map{|z|-(-n..-1).bsearch{|i|chk(1,-i,n,n)>=z}}\nc=[1,2].map{|z|(1..n).bsearch{|i|chk(1,1,i,n)>=z}}\nd=[1,2].map{|z|(1..n).bsearch{|i|chk(1,1,n,i)>=z}}\n16.times{|i|\n\tx11,y11,x12,y12=a[i[0]],b[i[1]],c[i[2]],d[i[3]]\n\tx21,y21,x22,y22=a[i[0]^1],b[i[1]^1],c[i[2]^1],d[i[3]^1]\n\tif x11<=x12&&y11<=y12 && x21<=x22&&y21<=y22 && chk(x11,y11,x12,y12)==1 && chk(x21,y21,x22,y22)==1\n\t\tputs '! %d %d %d %d %d %d %d %d'%[x11,y11,x12,y12,x21,y21,x22,y22]\n\t\tbreak\n\tend\n}"}], "negative_code": [{"source_code": "#!/usr/bin/ruby\nSTDOUT.sync=true\ndef chk(*a)\n\tputs '? %d %d %d %d'%a\n\tgets.to_i\nend\nn=gets.to_i\na=[1,2].map{|z|(1..n).bsearch{|i|chk(i,1,n,n)=z}}\nb=[1,2].map{|z|(1..n).bsearch{|i|chk(1,i,n,n)>=z}}\nc=[1,2].map{|z|(-n..-1).bsearch{|i|chk(1,1,-i,n)>=z}}\nd=[1,2].map{|z|(-n..-1).bsearch{|i|chk(1,1,n,-i)>=z}}\nSTDERR.puts [a,b,c,d].inspect\n16.times{|i|\n\tx11,y11,x12,y12=a[i[0]],b[i[1]],-c[i[2]],-d[i[3]]\n\tx21,y21,x22,y22=a[i[0]^1],b[i[1]^1],-c[i[2]^1],-d[i[3]^1]\n\tif x11<=x12&&y11<=y12 && x21<=x22&&y21<=y22 && chk(x11,y11,x12,y12)==1 && chk(x21,y21,x22,y22)==1\n\t\tputs '! %d %d %d %d %d %d %d %d'%[x11,y11,x12,y12,x21,y21,x22,y22]\n\t\tbreak\n\tend\n}"}, {"source_code": "#!/usr/bin/ruby\nSTDOUT.sync=true\ndef chk(*a)\n\tputs '? %d %d %d %d'%a\nSTDOUT.flush\n\tgets.to_i\nend\nn=gets.to_i\na=[1,2].map{|z|(1..n).bsearch{|i|chk(i,1,n,n)>=z}}\nb=[1,2].map{|z|(1..n).bsearch{|i|chk(1,i,n,n)>=z}}\nc=[1,2].map{|z|(-n..-1).bsearch{|i|chk(1,1,-i,n)>=z}}\nd=[1,2].map{|z|(-n..-1).bsearch{|i|chk(1,1,n,-i)>=z}}\n16.times{|i|\n\tx11,y11,x12,y12=0,0,0,0\n\tx21,y21,x22,y22=0,0,0,0\n\t#if x11<=x12&&y11<=y12 && x21<=x22&&y21<=y22 && chk(x11,y11,x12,y12)==1 && chk(x21,y21,x22,y22)==1\n\t\tputs '! %d %d %d %d %d %d %d %d'%[0,0,0,0,0,0,0,0]\n\t\tbreak\n\t\t#end\n}"}, {"source_code": "#!/usr/bin/ruby\nSTDOUT.sync=true\ndef chk(*a)\n\tputs '? %d %d %d %d'%a\nSTDOUT.flush\n\tgets.to_i\nend\nn=gets.to_i\na=[1,2].map{|z|(1..n).bsearch{|i|chk(i,1,n,n)>=z}}\nb=[1,2].map{|z|(1..n).bsearch{|i|chk(1,i,n,n)>=z}}\nc=[1,2].map{|z|(-n..-1).bsearch{|i|chk(1,1,-i,n)>=z}}\nd=[1,2].map{|z|(-n..-1).bsearch{|i|chk(1,1,n,-i)>=z}}\n16.times{|i|\n\tx11,y11,x12,y12=0,0,0,0\n\tx21,y21,x22,y22=0,0,0,0\n\t#if x11<=x12&&y11<=y12 && x21<=x22&&y21<=y22 && chk(x11,y11,x12,y12)==1 && chk(x21,y21,x22,y22)==1\n\t\tputs '! %d %d %d %d %d %d %d %d'%[1,1,1,1,1,1,1,1]\n\t\tbreak\n\t\t#end\n}"}, {"source_code": "#!/usr/bin/ruby\nSTDOUT.sync=true\ndef chk(*a)\n\tputs '? %d %d %d %d'%a\n\tgets.to_i\nend\nn=gets.to_i\na=[1,2].map{|z|(1..n).bsearch{|i|chk(i,1,n,n)>=z}}\nb=[1,2].map{|z|(1..n).bsearch{|i|chk(1,i,n,n)>=z}}\nc=[1,2].map{|z|(-n..-1).bsearch{|i|chk(1,1,-i,n)>=z}}\nd=[1,2].map{|z|(-n..-1).bsearch{|i|chk(1,1,n,-i)>=z}}\n16.times{|i|\n\tx11,y11,x12,y12=a[i[0]],b[i[1]],-c[i[2]],-d[i[3]]\n\tx21,y21,x22,y22=a[i[0]^1],b[i[1]^1],-c[i[2]^1],-d[i[3]^1]\n\t#if x11<=x12&&y11<=y12 && x21<=x22&&y21<=y22 && chk(x11,y11,x12,y12)==1 && chk(x21,y21,x22,y22)==1\n\t\tputs '! %d %d %d %d %d %d %d %d'%[x11,y11,x12,y12,x21,y21,x22,y22]\n\t\tbreak\n\t\t#end\n}"}, {"source_code": "#!/usr/bin/ruby\nSTDOUT.sync=true\ndef chk(*a)\n\tputs '? %d %d %d %d'%a\nSTDOUT.flush\n\tgets.to_i\nend\nn=gets.to_i\na=[1,2].map{|z|(1..n).bsearch{|i|chk(i,1,n,n)>=z}}\nb=[1,2].map{|z|(1..n).bsearch{|i|chk(1,i,n,n)>=z}}\nc=[1,2].map{|z|(-n..-1).bsearch{|i|chk(1,1,-i,n)>=z}}\nd=[1,2].map{|z|(-n..-1).bsearch{|i|chk(1,1,n,-i)>=z}}\n16.times{|i|\n\tx11,y11,x12,y12=1,1,1,1\n\tx21,y21,x22,y22=1,1,1,1\n\t#if x11<=x12&&y11<=y12 && x21<=x22&&y21<=y22 && chk(x11,y11,x12,y12)==1 && chk(x21,y21,x22,y22)==1\n\t\tputs '! %d %d %d %d %d %d %d %d'%[x11,y11,x12,y12,x21,y21,x22,y22]\n\t\tbreak\n\t\t#end\n}"}, {"source_code": "#!/usr/bin/ruby\nSTDOUT.sync=true\ndef chk(*a)\n\tputs '? %d %d %d %d'%a\nSTDOUT.flush\n\tgets.to_i\nend\nn=gets.to_i\na=[1,2].map{|z|(1..n).bsearch{|i|chk(i,1,n,n)>=z}}\nb=[1,2].map{|z|(1..n).bsearch{|i|chk(1,i,n,n)>=z}}\nc=[1,2].map{|z|(-n..-1).bsearch{|i|chk(1,1,-i,n)>=z}}\nd=[1,2].map{|z|(-n..-1).bsearch{|i|chk(1,1,n,-i)>=z}}\n16.times{|i|\n\tx11,y11,x12,y12=0,0,0,0\n\tx21,y21,x22,y22=0,0,0,0\n\t#if x11<=x12&&y11<=y12 && x21<=x22&&y21<=y22 && chk(x11,y11,x12,y12)==1 && chk(x21,y21,x22,y22)==1\n\t\tputs '! %d %d %d %d %d %d %d %d'%[1,1,1,1,2,2,2,2]\n\t\tbreak\n\t\t#end\n}"}, {"source_code": "#!/usr/bin/ruby\nSTDOUT.sync=true\ndef chk(*a)\n\tputs '? %d %d %d %d'%a\n\tgets.to_i\nend\nn=gets.to_i\na=[1,2].map{|z|(1..n).bsearch{|i|chk(i,1,n,n)>=z}}\nb=[1,2].map{|z|(1..n).bsearch{|i|chk(1,i,n,n)>=z}}\nc=[1,2].map{|z|(-n..-1).bsearch{|i|chk(1,1,-i,n)>=z}}\nd=[1,2].map{|z|(-n..-1).bsearch{|i|chk(1,1,n,-i)>=z}}\n16.times{|i|\n\tx11,y11,x12,y12=0,0,0,0\n\tx21,y21,x22,y22=0,0,0,0\n\t#if x11<=x12&&y11<=y12 && x21<=x22&&y21<=y22 && chk(x11,y11,x12,y12)==1 && chk(x21,y21,x22,y22)==1\n\t\tputs '! %d %d %d %d %d %d %d %d'%[x11,y11,x12,y12,x21,y21,x22,y22]\n\t\tbreak\n\t\t#end\n}"}, {"source_code": "#!/usr/bin/ruby\nSTDOUT.sync=true\ndef chk(*a)\n\tputs '? %d %d %d %d'%a\n\tgets.to_i\nend\nn=gets.to_i\na=[1,2].map{|z|(1..n).bsearch{|i|chk(i,1,n,n)>=z}}\nb=[1,2].map{|z|(1..n).bsearch{|i|chk(1,i,n,n)>=z}}\nc=[1,2].map{|z|(-n..-1).bsearch{|i|chk(1,1,-i,n)>=z}}\nd=[1,2].map{|z|(-n..-1).bsearch{|i|chk(1,1,n,-i)>=z}}\n16.times{|i|\n\tx11,y11,x12,y12=a[i[0]],b[i[1]],c[i[2]],d[i[3]]\n\tx21,y21,x22,y22=a[i[0]^1],b[i[1]^1],c[i[2]^1],d[i[3]^1]\n\tif x11<=x12&&y11<=y12 && x21<=x22&&y21<=y22 && chk(x11,y11,x12,y12)==1 && chk(x21,y21,x22,y22)==1\n\t\tputs '! %d %d %d %d %d %d %d %d'%[x11,y11,x12,y12,x21,y21,x22,y22]\n\t\tbreak\n\tend\n}"}, {"source_code": "#!/usr/bin/ruby\nSTDOUT.sync=true\ndef chk(*a)\n\tputs '? %d %d %d %d'%a\n\tgets.to_i\nend\nn=gets.to_i\na=[1,2].map{|z|(1..n).bsearch{|i|chk(i,1,n,n)>=z}}\nb=[1,2].map{|z|(1..n).bsearch{|i|chk(1,i,n,n)>=z}}\nc=[1,2].map{|z|(-n..-1).bsearch{|i|chk(1,1,-i,n)>=z}}\nd=[1,2].map{|z|(-n..-1).bsearch{|i|chk(1,1,n,-i)>=z}}\n16.times{|i|\n\tx11,y11,x12,y12=a[i[0]],b[i[1]],-c[i[2]],-d[i[3]]\n\tx21,y21,x22,y22=a[i[0]^1],b[i[1]^1],-c[i[2]^1],-d[i[3]^1]\n\tif x11<=x12&&y11<=y12 && x21<=x22&&y21<=y22 # && chk(x11,y11,x12,y12)==1 && chk(x21,y21,x22,y22)==1\n\t\tputs '! %d %d %d %d %d %d %d %d'%[x11,y11,x12,y12,x21,y21,x22,y22]\n\t\tbreak\n\tend\n}"}, {"source_code": "#!/usr/bin/ruby\nSTDOUT.sync=true\ndef chk(*a)\n\tputs '? %d %d %d %d'%a\n\tgets.to_i\nend\nn=gets.to_i\na=[1,2].map{|z|(1..n).bsearch{|i|chk(i,1,n,n)>=z}}\nb=[1,2].map{|z|(1..n).bsearch{|i|chk(1,i,n,n)>=z}}\nc=[1,2].map{|z|(-n..-1).bsearch{|i|chk(1,1,-i,n)>=z}}\nd=[1,2].map{|z|(-n..-1).bsearch{|i|chk(1,1,n,-i)>=z}}\n16.times{|i|\n\tx11,y11,x12,y12=a[i[0]],b[i[1]],-c[i[2]],-d[i[3]]\n\tx21,y21,x22,y22=a[i[0]^1],b[i[1]^1],-c[i[2]^1],-d[i[3]^1]\n\tif chk(x11,y11,x12,y12)==1 && chk(x21,y21,x22,y22)==1\n\t\tputs '! %d %d %d %d %d %d %d %d'%[x11,y11,x12,y12,x21,y21,x22,y22]\n\t\tbreak\n\tend\n}"}, {"source_code": "#!/usr/bin/ruby\nSTDOUT.sync=true\ndef chk(*a)\n\tputs '? %d %d %d %d'%a\n\tgets.to_i\nend\nn=gets.to_i\na=[1,2].map{|z|(1..n).bsearch{|i|chk(i,1,n,n)>=z}}\nb=[1,2].map{|z|(1..n).bsearch{|i|chk(1,i,n,n)>=z}}\nc=[1,2].map{|z|(-n..-1).bsearch{|i|chk(1,1,-i,n)>=z}}\nd=[1,2].map{|z|(-n..-1).bsearch{|i|chk(1,1,n,-i)>=z}}\n16.times{|i|\n\tx11,y11,x12,y12=a[i[0]],b[i[1]],-c[i[2]],-d[i[3]]\n\tx21,y21,x22,y22=a[i[0]^1],b[i[1]^1],-c[i[2]^1],-d[i[3]^1]\n\tif x11<=x12&&y11<=y12 && x21<=x22&&y21<=y22 && chk(x11,y11,x12,y12)==1 && chk(x21,y21,x22,y22)==1\n\t\tputs '! %d %d %d %d %d %d %d %d'%[x11,y11,x12,y12,x21,y21,x22,y22]\n\t\tbreak\n\tend\n}"}, {"source_code": "#!/usr/bin/ruby\nSTDOUT.sync=true\ndef chk(*a)\n\tputs '? %d %d %d %d'%a\n\tgets.to_i\nend\nn=gets.to_i\na=[1,2].map{|z|(1..n).bsearch{|i|chk(i,1,n,n)>=z}}\nb=[1,2].map{|z|(1..n).bsearch{|i|chk(1,i,n,n)>=z}}\nc=[1,2].map{|z|((1..n).bsearch{|i|chk(1,1,i,n)=z}}\nb=[1,2].map{|z|(1..n).bsearch{|i|chk(1,i,n,n)>=z}}\nc=[1,2].map{|z|(-n..-1).bsearch{|i|chk(1,1,-i,n)>=z}}\nd=[1,2].map{|z|(-n..-1).bsearch{|i|chk(1,1,n,-i)>=z}}\nputs '! 2 2 2 2 3 4 3 5'\nexit\n16.times{|i|\n\tx11,y11,x12,y12=a[i[0]],b[i[1]],-c[i[2]],-d[i[3]]\n\tx21,y21,x22,y22=a[i[0]^1],b[i[1]^1],-c[i[2]^1],-d[i[3]^1]\n\tif chk(x11,y11,x12,y12)==1 && chk(x21,y21,x22,y22)==1\n\t\tputs '! %d %d %d %d %d %d %d %d'%[x11,y11,x12,y12,x21,y21,x22,y22]\n\t\tbreak\n\tend\n}"}], "src_uid": "e27d96e0d94a3f03d86f597f1afef4fe"} {"nl": {"description": "You are given n integers a1, a2, ..., an. Find the number of pairs of indexes i, j (i < j) that ai + aj is a power of 2 (i. e. some integer x exists so that ai + aj = 2x).", "input_spec": "The first line contains the single positive integer n (1 ≤ n ≤ 105) — the number of integers. The second line contains n positive integers a1, a2, ..., an (1 ≤ ai ≤ 109).", "output_spec": "Print the number of pairs of indexes i, j (i < j) that ai + aj is a power of 2.", "sample_inputs": ["4\n7 3 2 1", "3\n1 1 1"], "sample_outputs": ["2", "3"], "notes": "NoteIn the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).In the second example all pairs of indexes (i, j) (where i < j) include in answer."}, "positive_code": [{"source_code": "#!/usr/bin/ruby\nh=Hash.new(0)\nr=0\ngets\ngets.split.map(&:to_i).each{|e|h[e]+=1}\nh.each{|k,v|\n\tx=2\n\twhile x<=0x80000000\n\t\ty=x-k\n\t\tif k==y\n\t\t\tr+=v*(v-1)/2\n\t\telsif k= 1\n x[1] -= 1\n elsif x[0] == x[1] && x[1] < n - 1\n x[1] += 1\n end\n\n if y[0] == y[1] && y[1] >= 1\n y[1] -= 1\n elsif y[0] == y[1] && y[1] < n - 1\n y[1] += 1\n end\n\n a[x[0]][y[0]] = \"*\"\n a[x[0]][y[1]] = \"*\"\n a[x[1]][y[0]] = \"*\"\n a[x[1]][y[1]] = \"*\"\n\n a.each do |l|\n printf(\"%s\\n\", l.join(\"\"))\n end\n\nend\n"}, {"source_code": "t = gets.to_i\r\nt.times do\r\n\tn = gets.to_i\r\n\ta = []\r\n\tn.times do\r\n\t\ttmp = gets.chomp.split \"\"\r\n\t\ta << tmp\r\n\tend\r\n\tp1 = p2 = [-1,-1]\r\n\r\n\tfor i in 0...n do\r\n\t\tfor j in 0...n do\r\n\t\t\tnext if a[i][j] != '*'\r\n\t\t\tp1 = [i,j] if p1 == [-1,-1]\r\n\t\t\tp2 = [i,j] if p1 != [-1,-1]\r\n\t\tend\r\n\tend\r\n\r\n\tif p1[0] == p2[0]\r\n\t\tif p1[0] == 0\r\n\t\t\ta[1][p1[1]] = a[1][p2[1]] = '*'\r\n\t\telse\r\n\t\t\ta[0][p1[1]] = a[0][p2[1]] = '*'\r\n\t\tend\r\n\telsif p1[1] == p2[1]\r\n\t\tif p1[1] == 0\r\n\t\t\ta[p1[0]][1] = a[p2[0]][1] = '*'\r\n\t\telse\r\n\t\t\ta[p1[0]][0] = a[p2[0]][0] = '*'\r\n\t\tend\r\n\telse\r\n\t\ta[p1[0]][p2[1]] = '*'\r\n\t\ta[p2[0]][p1[1]] = '*'\r\n\tend\r\n\r\n\r\n\tfor i in 0...n do\r\n\t\tputs a[i].join\r\n\tend\r\n\r\nend\r\n"}], "negative_code": [], "src_uid": "d8fb3822b983b8a8ffab341aee74f56f"} {"nl": {"description": "One evening Rainbow Dash and Fluttershy have come up with a game. Since the ponies are friends, they have decided not to compete in the game but to pursue a common goal. The game starts on a square flat grid, which initially has the outline borders built up. Rainbow Dash and Fluttershy have flat square blocks with size $$$1\\times1$$$, Rainbow Dash has an infinite amount of light blue blocks, Fluttershy has an infinite amount of yellow blocks. The blocks are placed according to the following rule: each newly placed block must touch the built on the previous turns figure by a side (note that the outline borders of the grid are built initially). At each turn, one pony can place any number of blocks of her color according to the game rules.Rainbow and Fluttershy have found out that they can build patterns on the grid of the game that way. They have decided to start with something simple, so they made up their mind to place the blocks to form a chess coloring. Rainbow Dash is well-known for her speed, so she is interested in the minimum number of turns she and Fluttershy need to do to get a chess coloring, covering the whole grid with blocks. Please help her find that number!Since the ponies can play many times on different boards, Rainbow Dash asks you to find the minimum numbers of turns for several grids of the games.The chess coloring in two colors is the one in which each square is neighbor by side only with squares of different colors.", "input_spec": "The first line contains a single integer $$$T$$$ ($$$1 \\le T \\le 100$$$): the number of grids of the games. Each of the next $$$T$$$ lines contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^9$$$): the size of the side of the grid of the game. ", "output_spec": "For each grid of the game print the minimum number of turns required to build a chess coloring pattern out of blocks on it.", "sample_inputs": ["2\n3\n4"], "sample_outputs": ["2\n3"], "notes": "NoteFor $$$3\\times3$$$ grid ponies can make two following moves: "}, "positive_code": [{"source_code": "gets.to_i.times{|s|puts (gets.to_i)/2+1}"}, {"source_code": "t = gets.to_i\nans = []\nt.times do\n n = gets.to_i\n ans << (n+2)/2\nend\nans.each do |x|\n puts x\nend\n"}], "negative_code": [{"source_code": "t = gets.to_i\nans = []\nt.times do\n n = gets.to_i\n if n == 1\n ans << 1\n elsif n == 2 || n == 3\n ans << 2\n else\n ans << (n+3)/2\n end\nend\nans.each do |x|\n puts x\nend"}], "src_uid": "eb39ac8d703516c7f81f95a989791b21"} {"nl": {"description": "The only difference between easy and hard versions is the maximum value of $$$n$$$.You are given a positive integer number $$$n$$$. You really love good numbers so you want to find the smallest good number greater than or equal to $$$n$$$.The positive integer is called good if it can be represented as a sum of distinct powers of $$$3$$$ (i.e. no duplicates of powers of $$$3$$$ are allowed).For example: $$$30$$$ is a good number: $$$30 = 3^3 + 3^1$$$, $$$1$$$ is a good number: $$$1 = 3^0$$$, $$$12$$$ is a good number: $$$12 = 3^2 + 3^1$$$, but $$$2$$$ is not a good number: you can't represent it as a sum of distinct powers of $$$3$$$ ($$$2 = 3^0 + 3^0$$$), $$$19$$$ is not a good number: you can't represent it as a sum of distinct powers of $$$3$$$ (for example, the representations $$$19 = 3^2 + 3^2 + 3^0 = 3^2 + 3^1 + 3^1 + 3^1 + 3^0$$$ are invalid), $$$20$$$ is also not a good number: you can't represent it as a sum of distinct powers of $$$3$$$ (for example, the representation $$$20 = 3^2 + 3^2 + 3^0 + 3^0$$$ is invalid). Note, that there exist other representations of $$$19$$$ and $$$20$$$ as sums of powers of $$$3$$$ but none of them consists of distinct powers of $$$3$$$.For the given positive integer $$$n$$$ find such smallest $$$m$$$ ($$$n \\le m$$$) that $$$m$$$ is a good number.You have to answer $$$q$$$ independent queries.", "input_spec": "The first line of the input contains one integer $$$q$$$ ($$$1 \\le q \\le 500$$$) — the number of queries. Then $$$q$$$ queries follow. The only line of the query contains one integer $$$n$$$ ($$$1 \\le n \\le 10^4$$$).", "output_spec": "For each query, print such smallest integer $$$m$$$ (where $$$n \\le m$$$) that $$$m$$$ is a good number.", "sample_inputs": ["7\n1\n2\n6\n13\n14\n3620\n10000"], "sample_outputs": ["1\n3\n9\n13\n27\n6561\n19683"], "notes": null}, "positive_code": [{"source_code": "def calcs(x,y,ax,ay,bx,by) ((ax-x)*(by-y)-(bx-x)*(ay-y))/2.0 end\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaizyo(n)(n < 2)? 1 : (2..n).inject(:*) end\n\ndef scount(a) b=na(a.max+1);a.each{|n|b[n]+=1};return b end\n\ndef na(n=0,d=0) Array.new(n,d)end\n\ndef na2(n=0,m=0,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n=0,m=0,l=0,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef bit(n) n.to_s(2).split(\"\").map(&:to_i) end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\n\nt3 = []\n(0..44).each{|d|t3<<3**d}\n\nn = inp[0]\nn.times do\n q = inp[0]\n t = []\n while (q > 0)\n t << q % 3\n q /= 3\n end\n t.reverse!\n ind = -1\n t.size.times do |i|\n if(t[i] == 2)\n ind = i + 1\n break\n end\n end\n t.unshift(0)\n ans = 0\n\n (0...(t.size)).reverse_each do |i|\n break if ind < 0\n if(ind <= i)\n t[i] = 0\n end\n if(ind > i)\n if(t[i] == 0)\n t[i] = 1\n break\n else\n t[i] = 0\n end\n end\n end\n\n(0...(t.size)).each do |i|\n ans += t3[t.size-i-1] if(t[i] >= 1)\n end\n p ans\nend\n"}, {"source_code": "def calcs(x,y,ax,ay,bx,by) ((ax-x)*(by-y)-(bx-x)*(ay-y))/2.0 end\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaizyo(n)(n < 2)? 1 : (2..n).inject(:*) end\n\ndef scount(a) b=na(a.max+1);a.each{|n|b[n]+=1};return b end\n\ndef na(n=0,d=0) Array.new(n,d)end\n\ndef na2(n=0,m=0,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n=0,m=0,l=0,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef bit(n) n.to_s(2).split(\"\").map(&:to_i) end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef c(i, now)\n if(i==10)\n @anss << now\n return\n end\n c(i+1,now)\n c(i+1,now + @t3[i])\nend\n\n@t3 = []\n(0..9).each{|d| @t3 << 3**d}\n@anss = []\nc(0,0)\n@anss.sort!\nn = inp[0]\nn.times do\n q = inp[0]\n puts @anss.bsearch{|x| x >= q }\nend\n"}], "negative_code": [], "src_uid": "5953b898995a82edfbd42b6c0f7138af"} {"nl": {"description": "A newspaper is published in Walrusland. Its heading is s1, it consists of lowercase Latin letters. Fangy the little walrus wants to buy several such newspapers, cut out their headings, glue them one to another in order to get one big string. After that walrus erase several letters from this string in order to get a new word s2. It is considered that when Fangy erases some letter, there's no whitespace formed instead of the letter. That is, the string remains unbroken and it still only consists of lowercase Latin letters.For example, the heading is \"abc\". If we take two such headings and glue them one to the other one, we get \"abcabc\". If we erase the letters on positions 1 and 5, we get a word \"bcac\".Which least number of newspaper headings s1 will Fangy need to glue them, erase several letters and get word s2?", "input_spec": "The input data contain two lines. The first line contain the heading s1, the second line contains the word s2. The lines only consist of lowercase Latin letters (1 ≤ |s1| ≤ 104, 1 ≤ |s2| ≤ 106).", "output_spec": "If it is impossible to get the word s2 in the above-described manner, print \"-1\" (without the quotes). Otherwise, print the least number of newspaper headings s1, which Fangy will need to receive the word s2.", "sample_inputs": ["abc\nxyz", "abcd\ndabc"], "sample_outputs": ["-1", "2"], "notes": null}, "positive_code": [{"source_code": "class Array\n def findB\n x0 = 0\n x1 = size-1\n return self[x0] if yield(self[x0])\n return false unless yield(self[x1])\n\n while x1-x0 > 1\n x2 = (x0+x1)/2\n\n if yield(self[x2])\n x1 = x2\n else\n x0 = x2\n end\n end\n return self[x1]\n end\nend\n\ndef ceil(a, b)\n (a+b-1)/b\nend\n\ndef solve(from, to)\n chrs = {}\n 0.upto(from.size-1) do |i|\n chrs[from[i]] = true\n end\n\n unless to.each_char.all?{|x|chrs[x]}\n return -1\n end\n\n tbl = {}\n chrs.keys.each do |c|\n idx = 0\n o = tbl[c] = [0]*from.size\n 0.upto(from.size*2-1) do |i|\n if from[i%from.size] == c\n idx.upto(i-1) do |j|\n o[j%from.size] = i%from.size\n end\n break if i >= from.size\n idx = i\n end\n end\n end\n\n result = 1\n pos = -1\n ptr = Hash.new(-1)\n to.each_char do |x|\n new_pos = tbl[x][pos]\n #p [x, pos, new_pos, result, tbl[x]]\n result += 1 if new_pos <= pos\n pos = new_pos\n end\n\n result\nend\n\nfrom = gets.chomp\nto = gets.chomp\np solve(from, to)\n"}, {"source_code": "def error\n\tp -1\n\texit\nend\ns,t=gets.chomp,gets.chomp\nnext_pos={}\nclosest_pos={}\ni,pos,ans=s.length,-1,1\ns.reverse.each_byte{|b|\n\ti-=1\n\tclosest_pos.each{|k,v|next_pos[(i<<7)+k]=v}\n\tclosest_pos[b]=i\n}\nclosest_pos.each{|k,v|next_pos[(-1<<7)+k]=v}\n\nt.each_byte{|b|\n\tpos=next_pos[(pos<<7)+b]\n\tunless pos\n\t\tans+=1\n\t\tpos=next_pos[(-1<<7)+b]\n\t\terror unless pos\n\tend\n}\np ans"}], "negative_code": [{"source_code": "def error\n\tp -1\n\texit\nend\ns,t=gets.chomp,gets.chomp\nerror if s.size>t.size\nnext_pos={}\nclosest_pos={}\ni,pos,ans=s.length,-1,1\ns.reverse.each_byte{|b|\n\ti-=1\n\tclosest_pos.each{|k,v|next_pos[[k,i]]=v}\n\tclosest_pos[b]=i\n}\nclosest_pos.each{|k,v|next_pos[[k,-1]]=v}\n\nt.each_byte{|b|\n\tpos=next_pos[[b,pos]]\n\tunless pos\n\t\tans+=1\n\t\tpos=next_pos[[b,-1]]\n\t\terror unless pos\n\tend\n}\np ans"}], "src_uid": "1b83529ea4057c76ae8483f8aa506c56"} {"nl": {"description": "Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps: Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string \"zyx\", \"xzy\", \"yxz\". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.", "input_spec": "The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).", "output_spec": "For each test, print \"YES\" (without the quotes) if the algorithm works correctly on the corresponding test and \"NO\" (without the quotes) otherwise.", "sample_inputs": ["zyxxxxxxyyz\n5\n5 5\n1 3\n1 11\n1 4\n3 6"], "sample_outputs": ["YES\nYES\nNO\nYES\nNO"], "notes": "NoteIn the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string \"xzyx\" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly. "}, "positive_code": [{"source_code": "str = gets.chomp\nntest = gets.to_i\ntests=Array::new()\nntest.times{tests<\nmodule Solution; end\n# @snippet\nrequire \"scanf\"\n# @snippet\nclass Solution::Runner\n\n def self.run(input_format, input_stream = STDIN, output_stream = STDOUT, &block)\n Solution::Solver.new(input_format, input_stream, output_stream) do\n input do |args|\n if block.nil?\n output solve(*args)\n else\n output block.call(*args)\n end\n end\n end\n end\n\nend\n# @snippet\nclass Solution::Solver\n\n attr_reader :input_format\n attr_reader :input_stream\n attr_reader :output_stream\n\n def initialize(new_input_format, new_input_stream, new_output_stream, &block)\n @input_format = new_input_format\n @input_stream = new_input_stream\n @output_stream = new_output_stream\n\n instance_eval(&block) if block_given?\n end\n\n def input\n while s = gets\n break if s.nil?\n s.scanf(input_format) do |*args|\n yield(*args)\n end\n end\n end\n\n def output(res)\n puts res.size\n res.each do |key, value|\n puts \"#{key} #{value}\"\n end\n end\n\nend\n# @snippet\n\nrequire \"set\"\n\nclass Disj\n\n attr_reader :parent\n attr_reader :root\n attr_reader :last_name\n\n def initialize\n @last_name = {}\n @parent = {}\n @root = Set.new\n end\n\n def size\n @root.size\n end\n\n def find(name)\n if parent[name].nil?\n name\n else\n find(parent[name])\n end\n end\n\n def parent?(name)\n find(name) === name\n end\n\n def update(old_name, new_name)\n if parent?(old_name)\n root.add old_name\n end\n last_name[find(old_name)] = new_name\n parent[new_name] = find(old_name)\n end\n\n def each\n root.each do |name|\n yield name, last_name[name]\n end\n end\n\nend\n\ninput_format = \"%d\" # scanf(input_format)\nSolution::Runner.run(input_format) do |q|\n disj = Disj.new\n\n list = (1..q).to_a.map do\n old, new = gets.split\n {:new => new, :old => old, }\n end\n\n list.inject(Set.new) do |set, info|\n disj.update info[:old], info[:new]\n end\n\n disj\nend\n"}, {"source_code": "# @snippet\nmodule Solution; end\n# @snippet\nrequire \"scanf\"\n# @snippet\nclass Solution::Runner\n\n def self.run(input_format, input_stream = STDIN, output_stream = STDOUT, &block)\n Solution::Solver.new(input_format, input_stream, output_stream) do\n input do |args|\n if block.nil?\n output solve(*args)\n else\n output block.call(*args)\n end\n end\n end\n end\n\nend\n# @snippet\nclass Solution::Solver\n\n attr_reader :input_format\n attr_reader :input_stream\n attr_reader :output_stream\n\n def initialize(new_input_format, new_input_stream, new_output_stream, &block)\n @input_format = new_input_format\n @input_stream = new_input_stream\n @output_stream = new_output_stream\n\n instance_eval(&block) if block_given?\n end\n\n def input\n while s = gets\n break if s.nil?\n s.scanf(input_format) do |*args|\n yield(*args)\n end\n end\n end\n\n def output(res)\n puts res.size\n res.each do |key, value|\n puts \"#{key} #{value}\"\n end\n end\n\nend\n# @snippet\n\nrequire \"set\"\n\nclass Disj\n\n attr_reader :parent\n attr_reader :root\n attr_reader :last_name\n\n def initialize\n @last_name = {}\n @parent = {}\n @root = Set.new\n end\n\n def size\n @root.size\n end\n\n def find(name)\n if parent[name].nil?\n name\n else\n find(parent[name])\n end\n end\n\n def parent?(name)\n find(name) === name\n end\n\n def update(old_name, new_name)\n if parent?(old_name)\n root.add old_name\n end\n last_name[find(old_name)] = new_name\n parent[new_name] = find(old_name)\n self\n end\n\n def each\n root.each do |name|\n yield name, last_name[name]\n end\n end\n\nend\n\ninput_format = \"%d\" # scanf(input_format)\nSolution::Runner.run(input_format) do |q|\n (1..q).to_a.map do\n old, new = gets.split\n {:new => new, :old => old, }\n end.inject(Disj.new) do |disj, info|\n disj.update info[:old], info[:new]\n end\nend\n\n"}, {"source_code": "q = gets.to_i\n\noldies = []\nnewies = {}\nchanges = {}\n\nq.times do \n old, new = gets.chomp.split\n \n oldies.push(old) if changes[old] == nil && newies[old] == nil\n changes[old] = new\n newies[new] = true\nend\n\nputs oldies.size\noldies.each do |old|\n new = changes[old]\n while changes[new] != nil\n new = changes[new]\n end\n \n puts \"#{old} #{new}\"\nend"}, {"source_code": "n = gets.to_i\n\nh = Hash.new\n\nn.times do\n old_handle, new_handle = gets.split(' ')\n if h[old_handle].nil? then\n h[new_handle] = old_handle\n else\n h[new_handle] = h[old_handle]\n h.delete(old_handle)\n end\nend\n\nprint \"#{h.length}\\n\"\nh.each do |k, v|\n print \"#{v} #{k}\\n\"\nend\n"}, {"source_code": "q = gets.to_i\n\nusers = {}\n\nfor s in STDIN.each_line.take q\n old, new = s.split \" \"\n\n if users.has_value? old\n k = users.key(old)\n users[k] = new\n else\n users = users.merge!({old => new})\n end\nend\n\nputs users.length\n\nusers.each do |k, v|\n puts \"#{k} #{v}\"\nend\n\n"}, {"source_code": "pa=Array.new\nha=Hash.new #ruby的哈希\nhb=Hash.new #ruby的哈希\n\nn=gets.to_i\nfor i in 1..n\n str=gets.to_s\n pa=str.split\n stra=pa[0]\n strb=pa[1]\n ha[stra]=strb\nend\n#p ha\n\nha.each{|key,value|\n stt=key\n while ha[stt]!=nil\n stb=stt\n stt=ha[stt]\n ha.delete(stb)\n end\n hb[key]=stt\n}\n#p hb\n\nputs hb.length\nhb.each{|key,value|\n puts\"#{key} #{value}\" #输出格式控制\n}\n"}, {"source_code": "# $f = File.open('input.txt', 'r')\n$f = STDIN\n\ndef read_line\n $f.gets.chomp.split\nend\n\ndef read_input(n)\n hash = {}\n n.times do\n sa, sb = read_line\n change = hash.key(sa) || sa\n\n hash[change] = sb\n end\n hash\nend\n\ndef output(hash)\n puts hash.size\n hash.each do |k, v|\n puts \"#{k} #{v}\"\n end\nend\n\ndef solve(n)\n output read_input(n)\nend\n\nwhile str = $f.gets\n n = str.chomp.to_i\n solve(n)\nend\n"}], "negative_code": [{"source_code": "q = gets.to_i\n\nusers = Hash.new\n\nq.times do\n o, n = gets.split(\" \")\n p users[o]\n if users[o].nil? then\n users[n] = o\n else\n users[n] = users[o]\n users.delete(o)\n end\nend\n\nprint \"#{users.length}\\n\"\nusers.each do |v, k|\n print \"#{k} #{v}\\n\"\nend"}, {"source_code": "pa=Array.new\nha=Hash.new #ruby的哈希\nhb=Hash.new #ruby的哈希\n\nn=gets.to_i\nfor i in 1..n\n str=gets.to_s\n pa=str.split\n stra=pa[0]\n strb=pa[1]\n ha[stra]=strb\nend\n#p ha\n\nha.each{|key,value|\n stt=key\n while ha[stt]!=nil\n stb=stt\n stt=ha[stt]\n ha.delete(stb)\n end\n hb[key]=stt\n}\n#p hb\n\nhb.each{|key,value|\n puts\"#{key} #{value}\"#输出格式控制\n}\n"}], "src_uid": "bdd98d17ff0d804d88d662cba6a61e8f"} {"nl": {"description": "Ayush is a cashier at the shopping center. Recently his department has started a ''click and collect\" service which allows users to shop online. The store contains k items. n customers have already used the above service. Each user paid for m items. Let aij denote the j-th item in the i-th person's order.Due to the space limitations all the items are arranged in one single row. When Ayush receives the i-th order he will find one by one all the items aij (1 ≤ j ≤ m) in the row. Let pos(x) denote the position of the item x in the row at the moment of its collection. Then Ayush takes time equal to pos(ai1) + pos(ai2) + ... + pos(aim) for the i-th customer.When Ayush accesses the x-th element he keeps a new stock in the front of the row and takes away the x-th element. Thus the values are updating.Your task is to calculate the total time it takes for Ayush to process all the orders.You can assume that the market has endless stock.", "input_spec": "The first line contains three integers n, m and k (1 ≤ n, k ≤ 100, 1 ≤ m ≤ k) — the number of users, the number of items each user wants to buy and the total number of items at the market. The next line contains k distinct integers pl (1 ≤ pl ≤ k) denoting the initial positions of the items in the store. The items are numbered with integers from 1 to k. Each of the next n lines contains m distinct integers aij (1 ≤ aij ≤ k) — the order of the i-th person.", "output_spec": "Print the only integer t — the total time needed for Ayush to process all the orders.", "sample_inputs": ["2 2 5\n3 4 1 2 5\n1 5\n3 1"], "sample_outputs": ["14"], "notes": "NoteCustomer 1 wants the items 1 and 5.pos(1) = 3, so the new positions are: [1, 3, 4, 2, 5].pos(5) = 5, so the new positions are: [5, 1, 3, 4, 2].Time taken for the first customer is 3 + 5 = 8.Customer 2 wants the items 3 and 1.pos(3) = 3, so the new positions are: [3, 5, 1, 4, 2].pos(1) = 3, so the new positions are: [1, 3, 5, 4, 2].Time taken for the second customer is 3 + 3 = 6.Total time is 8 + 6 = 14.Formally pos(x) is the index of x in the current row."}, "positive_code": [{"source_code": "n, m, k = gets.split.map(&:to_i)\narr = gets.split.map(&:to_i)\ntotal_sum = 0\nfor i in 1..n do\n this_sum = 0\n order = gets.split.map(&:to_i)\n order.each_with_index do |val, i|\n for j in 0..k do\n if val == arr[j]\n o = arr.delete_at(j)\n this_sum += j + 1\n arr.unshift(o)\n end\n end\n end\n total_sum += this_sum\nend\nputs total_sum\n"}, {"source_code": "n, m, k = readline.split(' ').collect(&:to_i)\narr = readline.split(' ').collect(&:to_i)\nsum = 0\nn.times do\n items = readline.split(' ').collect(&:to_i)\n items.each do |item|\n sum += arr.index(item) + 1\n arr.delete item\n arr.insert(0, item)\n end\nend\nputs sum\n"}, {"source_code": "io = lambda {gets.split.map(&:to_i)}\nn, m, k = io.call\np = io.call\nr = 0\n\n(0...n).each do |i|\n a = io.call\n a.each do |j|\n d = p.index(j)\n p.unshift(p.delete_at(d))\n r += d + 1\n end\nend\n\nputs r\n"}], "negative_code": [], "src_uid": "39fd7843558ed2aa6b8c997c2b8a1fad"} {"nl": {"description": "You are given an array $$$a$$$, consisting of $$$n$$$ integers.Each position $$$i$$$ ($$$1 \\le i \\le n$$$) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.For example, let $$$a = [-1, 1, \\underline{3}, 2, \\underline{-2}, 1, -4, \\underline{0}]$$$, the underlined positions are locked. You can obtain the following arrays: $$$[-1, 1, \\underline{3}, 2, \\underline{-2}, 1, -4, \\underline{0}]$$$; $$$[-4, -1, \\underline{3}, 2, \\underline{-2}, 1, 1, \\underline{0}]$$$; $$$[1, -1, \\underline{3}, 2, \\underline{-2}, 1, -4, \\underline{0}]$$$; $$$[1, 2, \\underline{3}, -1, \\underline{-2}, -4, 1, \\underline{0}]$$$; and some others. Let $$$p$$$ be a sequence of prefix sums of the array $$$a$$$ after the rearrangement. So $$$p_1 = a_1$$$, $$$p_2 = a_1 + a_2$$$, $$$p_3 = a_1 + a_2 + a_3$$$, $$$\\dots$$$, $$$p_n = a_1 + a_2 + \\dots + a_n$$$.Let $$$k$$$ be the maximum $$$j$$$ ($$$1 \\le j \\le n$$$) such that $$$p_j < 0$$$. If there are no $$$j$$$ such that $$$p_j < 0$$$, then $$$k = 0$$$.Your goal is to rearrange the values in such a way that $$$k$$$ is minimum possible.Output the array $$$a$$$ after the rearrangement such that the value $$$k$$$ for it is minimum possible. If there are multiple answers then print any of them.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of testcases. Then $$$t$$$ testcases follow. The first line of each testcase contains a single integer $$$n$$$ ($$$1 \\le n \\le 100$$$) — the number of elements in the array $$$a$$$. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$-10^5 \\le a_i \\le 10^5$$$) — the initial array $$$a$$$. The third line of each testcase contains $$$n$$$ integers $$$l_1, l_2, \\dots, l_n$$$ ($$$0 \\le l_i \\le 1$$$), where $$$l_i = 0$$$ means that the position $$$i$$$ is unlocked and $$$l_i = 1$$$ means that the position $$$i$$$ is locked.", "output_spec": "Print $$$n$$$ integers — the array $$$a$$$ after the rearrangement. Value $$$k$$$ (the maximum $$$j$$$ such that $$$p_j < 0$$$ (or $$$0$$$ if there are no such $$$j$$$)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones. If there are multiple answers then print any of them.", "sample_inputs": ["5\n3\n1 3 2\n0 0 0\n4\n2 -3 4 -1\n1 1 1 1\n7\n-8 4 -2 -6 4 7 1\n1 0 0 0 1 1 0\n5\n0 1 -4 6 3\n0 0 0 1 1\n6\n-1 7 10 4 -8 -1\n1 0 0 0 0 1"], "sample_outputs": ["1 2 3\n2 -3 4 -1\n-8 -6 1 4 4 7 -2\n-4 0 1 6 3\n-1 4 7 -8 10 -1"], "notes": "NoteIn the first testcase you can rearrange all values however you want but any arrangement will result in $$$k = 0$$$. For example, for an arrangement $$$[1, 2, 3]$$$, $$$p=[1, 3, 6]$$$, so there are no $$$j$$$ such that $$$p_j < 0$$$. Thus, $$$k = 0$$$.In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.In the third testcase the prefix sums for the printed array are $$$p = [-8, -14, -13, -9, -5, 2, 0]$$$. The maximum $$$j$$$ is $$$5$$$, thus $$$k = 5$$$. There are no arrangements such that $$$k < 5$$$.In the fourth testcase $$$p = [-4, -4, -3, 3, 6]$$$.In the fifth testcase $$$p = [-1, 3, 10, 2, 12, 11]$$$."}, "positive_code": [{"source_code": "def read\n gets.split.map &:to_i\nend\ngets.to_i.times do\n n = gets.to_i\n a, l = read, read\n v, res = [], [0] * n\n n.times do |i|\n if l[i] == 1\n res[i] = a[i]\n else\n v << a[i]\n end\n end\n v.sort!\n n.times do |i|\n if l[i].zero?\n res[i] = v[-1]\n v.pop\n end\n end\n puts res.join ' '\nend"}], "negative_code": [], "src_uid": "9ef180b33717e4d6a21b4c5bb855e15b"} {"nl": {"description": "In a Berland's zoo there is an enclosure with camels. It is known that camels like to spit. Bob watched these interesting animals for the whole day and registered in his notepad where each animal spitted. Now he wants to know if in the zoo there are two camels, which spitted at each other. Help him to solve this task.The trajectory of a camel's spit is an arc, i.e. if the camel in position x spits d meters right, he can hit only the camel in position x + d, if such a camel exists.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 100) — the amount of camels in the zoo. Each of the following n lines contains two integers xi and di ( - 104 ≤ xi ≤ 104, 1 ≤ |di| ≤ 2·104) — records in Bob's notepad. xi is a position of the i-th camel, and di is a distance at which the i-th camel spitted. Positive values of di correspond to the spits right, negative values correspond to the spits left. No two camels may stand in the same position.", "output_spec": "If there are two camels, which spitted at each other, output YES. Otherwise, output NO.", "sample_inputs": ["2\n0 1\n1 -1", "3\n0 1\n1 1\n2 -2", "5\n2 -10\n3 10\n0 5\n5 -5\n10 1"], "sample_outputs": ["YES", "NO", "YES"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\n\nvers = []\ntargets = []\nn.times do |i|\n s = gets.split.map{|i| i.to_i}\n targets << s[0] + s[1]\n vers << s[0]\nend\n\nif n < 2 then\n puts \"NO\"\nelse\n c = 0\n n.times do |i|\n if (k = vers.index(targets[i])) then\n if targets[k] == vers[i] then\n c = 2\n end\n end\n end\n if c > 1 then\n puts \"YES\"\n else\n puts \"NO\"\n end\nend"}, {"source_code": "\nn=gets.to_i\ndata=[]\nn.times{data.push(gets.split.map(&:to_i)) }\ndata.each do |i|\n data.each do |j|\n if i[0]+i[1]==j[0] && j[0]+j[1]==i[0] then\n puts \"YES\"\n exit\n end\n end\nend\nputs \"NO\"\n"}, {"source_code": "n = gets.to_i\n\nmas = Array.new()\nfor i in 0..n-1\n s = gets.split(\" \")\n mas[i] = [s[0].to_i, s[1].to_i]\nend\n\nfor i in 0..n-1\n for j in 0..n-1\n if (i != j && (mas[i][0] + mas[i][1] == mas[j][0]) && (mas[j][0] + mas[j][1] == mas[i][0]))\n puts \"YES\"\n exit\n end\n end\nend\n\nputs \"NO\""}], "negative_code": [], "src_uid": "d7dc61a8f3b0091320b96cedd1f4f0a7"} {"nl": {"description": "You have an array $$$a_1, a_2, \\dots, a_n$$$. All $$$a_i$$$ are positive integers.In one step you can choose three distinct indices $$$i$$$, $$$j$$$, and $$$k$$$ ($$$i \\neq j$$$; $$$i \\neq k$$$; $$$j \\neq k$$$) and assign the sum of $$$a_j$$$ and $$$a_k$$$ to $$$a_i$$$, i. e. make $$$a_i = a_j + a_k$$$.Can you make all $$$a_i$$$ lower or equal to $$$d$$$ using the operation above any number of times (possibly, zero)?", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 2000$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$d$$$ ($$$3 \\le n \\le 100$$$; $$$1 \\le d \\le 100$$$) — the number of elements in the array $$$a$$$ and the value $$$d$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 100$$$) — the array $$$a$$$.", "output_spec": "For each test case, print YES, if it's possible to make all elements $$$a_i$$$ less or equal than $$$d$$$ using the operation above. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).", "sample_inputs": ["3\n5 3\n2 3 2 5 4\n3 4\n2 4 4\n5 4\n2 1 5 3 6"], "sample_outputs": ["NO\nYES\nYES"], "notes": "NoteIn the first test case, we can prove that we can't make all $$$a_i \\le 3$$$.In the second test case, all $$$a_i$$$ are already less or equal than $$$d = 4$$$.In the third test case, we can, for example, choose $$$i = 5$$$, $$$j = 1$$$, $$$k = 2$$$ and make $$$a_5 = a_1 + a_2 = 2 + 1 = 3$$$. Array $$$a$$$ will become $$$[2, 1, 5, 3, 3]$$$.After that we can make $$$a_3 = a_5 + a_2 = 3 + 1 = 4$$$. Array will become $$$[2, 1, 4, 3, 3]$$$ and all elements are less or equal than $$$d = 4$$$."}, "positive_code": [{"source_code": "t = gets.to_i\r\nt.times do\r\n n, d = gets.split.map { |x| x.to_i }\r\n a = gets.split.map { |x| x.to_i }\r\n puts a.sort[0..1].sum <= d || a.max <= d ? \"YES\" : \"NO\"\r\nend\r\n# "}, {"source_code": "t = gets.to_i\r\nt.times do\r\n n, d = gets.split.map { |x| x.to_i }\r\n a = gets.split.map { |x| x.to_i }\r\n puts a.sort[0..1].sum <= d || a.max <= d ? \"YES\" : \"NO\"\r\nend\r\n"}, {"source_code": "gets.to_i.times do\r\n n,d=gets.split.map(&:to_i)\r\n a=gets.split.map(&:to_i).sort\r\n if a[-1]<=d\r\n puts \"YES\"\r\n elsif a[0]+a[1]<=d\r\n puts \"YES\"\r\n else\r\n puts \"NO\"\r\n end\r\nend\r\n"}, {"source_code": "gets.to_i.times do\n n, d = gets.split.map &:to_i\n a = gets.split.map &:to_i\n a.sort!\n if a[0] + a[1] <= d || a[-1] <= d\n puts \"YES\"\n else\n puts \"NO\"\n end\nend\n"}, {"source_code": "#input of int(split by space)\ndef get_i()\n return gets.chomp.split(\" \").map(&:to_i)\nend\n#input of float(split by space)\ndef get_f()\n return gets.chomp.split(\" \").map(&:to_f)\nend\n#input of string(split by space)\ndef get()\n return gets.chomp.split(\" \")\nend\n#input of string(split per one character)\ndef get_nsp()\n return gets.chomp.split(\"\")\nend\n#yes or no decision\ndef yn_judge(bool,y=\"Yes\",n=\"No\")\n return bool ? y : n \nend\n#create of array\ndef array(size1,init=nil,size2=-1)\n if size2==-1\n return Array.new(size1){init}\n else\n return Array.new(size2){Array.new(size1){init}}\n end\nend\n\ndef max(a,b)\n return a>b ? a : b\nend\n\ndef min(a,b)\n return a>b ? b : a\nend\n\nINF=Float::INFINITY\n\nT=gets.to_i\nT.times do\n n,d=get_i\n a=get_i\n f=true\n n.times do|i|\n if a[i]>d\n f=false\n break\n end \n end\n if f\n puts \"YES\"\n next\n end\n is_find=false\n (n-1).times do|i|\n (i+1).upto(n-1) do|j|\n if a[i]+a[j]<=d\n is_find=true \n break\n end\n end\n end\n if is_find\n puts \"YES\"\n else\n puts \"NO\"\n end\nend\n"}, {"source_code": "def read\r\n gets.split.map &:to_i\r\nend\r\ngets.to_i.times do\r\n _, d = read\r\n a = read.sort!\r\n puts a[0] + a[1] <= d || a[-1] <= d ? \"YES\" :\"NO\"\r\nend"}, {"source_code": "t = gets.chomp.to_i\r\nt.times do\r\n n, d = gets.chomp.split.map(&:to_i)\r\n arr = gets.chomp.split.map(&:to_i)\r\n arr = arr.sort\r\n puts arr[0] + arr[1] <= d || arr.all?{|el| el <= d} ? \"YES\" : \"NO\"\r\nend"}], "negative_code": [{"source_code": "gets.to_i.times do\r\n n,d=gets.split.map(&:to_i)\r\n a=gets.split.map(&:to_i).sum\r\n if a<=d*n\r\n puts \"YES\"\r\n else\r\n puts \"NO\"\r\n end\r\nend\r\n"}], "src_uid": "044c2a3bafe4f47036ee81f2e40f639a"} {"nl": {"description": "Vasya has got $$$n$$$ books, numbered from $$$1$$$ to $$$n$$$, arranged in a stack. The topmost book has number $$$a_1$$$, the next one — $$$a_2$$$, and so on. The book at the bottom of the stack has number $$$a_n$$$. All numbers are distinct.Vasya wants to move all the books to his backpack in $$$n$$$ steps. During $$$i$$$-th step he wants to move the book number $$$b_i$$$ into his backpack. If the book with number $$$b_i$$$ is in the stack, he takes this book and all the books above the book $$$b_i$$$, and puts them into the backpack; otherwise he does nothing and begins the next step. For example, if books are arranged in the order $$$[1, 2, 3]$$$ (book $$$1$$$ is the topmost), and Vasya moves the books in the order $$$[2, 1, 3]$$$, then during the first step he will move two books ($$$1$$$ and $$$2$$$), during the second step he will do nothing (since book $$$1$$$ is already in the backpack), and during the third step — one book (the book number $$$3$$$). Note that $$$b_1, b_2, \\dots, b_n$$$ are distinct.Help Vasya! Tell him the number of books he will put into his backpack during each step.", "input_spec": "The first line contains one integer $$$n~(1 \\le n \\le 2 \\cdot 10^5)$$$ — the number of books in the stack. The second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n~(1 \\le a_i \\le n)$$$ denoting the stack of books. The third line contains $$$n$$$ integers $$$b_1, b_2, \\dots, b_n~(1 \\le b_i \\le n)$$$ denoting the steps Vasya is going to perform. All numbers $$$a_1 \\dots a_n$$$ are distinct, the same goes for $$$b_1 \\dots b_n$$$.", "output_spec": "Print $$$n$$$ integers. The $$$i$$$-th of them should be equal to the number of books Vasya moves to his backpack during the $$$i$$$-th step.", "sample_inputs": ["3\n1 2 3\n2 1 3", "5\n3 1 4 2 5\n4 5 1 3 2", "6\n6 5 4 3 2 1\n6 5 3 4 2 1"], "sample_outputs": ["2 0 1", "3 2 0 0 0", "1 1 2 0 1 1"], "notes": "NoteThe first example is described in the statement.In the second example, during the first step Vasya will move the books $$$[3, 1, 4]$$$. After that only books $$$2$$$ and $$$5$$$ remain in the stack ($$$2$$$ is above $$$5$$$). During the second step Vasya will take the books $$$2$$$ and $$$5$$$. After that the stack becomes empty, so during next steps Vasya won't move any books."}, "positive_code": [{"source_code": "# http://codeforces.com/contest/1073/problem/B\n\nn = gets\narray = gets.split(' ').map(&:to_i)\nsteps = gets.split(' ').map(&:to_i)\n\nbackpack = {}\nresult = []\n\nsteps.each do |step|\n if backpack[step]\n result << 0\n else\n count = 0\n begin\n num = array.shift\n count += 1\n backpack[num] = 1\n end while num != step\n\n result << count\n end\nend\n\nputs result.join(' ')\n"}, {"source_code": "n = gets.strip.to_i\n\narr = gets.strip.split(' ').map(&:to_i)\n\nquery = gets.strip.split(' ').map(&:to_i)\n\nmax = nil\n\npos = Hash.new()\n\narr.each_with_index do |a, i|\n pos[a] = i + 1\nend\n\nout = []\nquery.each do |q|\n if max.nil?\n max = pos[q]\n out << max\n else\n val = pos[q]\n if val > max\n out << (val - max)\n max = val\n else\n out << 0\n end\n end\nend\n\nputs out.join(' ')\n"}], "negative_code": [], "src_uid": "82176739a1dd4fe85ec059058a00fbb9"} {"nl": {"description": "You are given a problemset consisting of $$$n$$$ problems. The difficulty of the $$$i$$$-th problem is $$$a_i$$$. It is guaranteed that all difficulties are distinct and are given in the increasing order.You have to assemble the contest which consists of some problems of the given problemset. In other words, the contest you have to assemble should be a subset of problems (not necessary consecutive) of the given problemset. There is only one condition that should be satisfied: for each problem but the hardest one (the problem with the maximum difficulty) there should be a problem with the difficulty greater than the difficulty of this problem but not greater than twice the difficulty of this problem. In other words, let $$$a_{i_1}, a_{i_2}, \\dots, a_{i_p}$$$ be the difficulties of the selected problems in increasing order. Then for each $$$j$$$ from $$$1$$$ to $$$p-1$$$ $$$a_{i_{j + 1}} \\le a_{i_j} \\cdot 2$$$ should hold. It means that the contest consisting of only one problem is always valid.Among all contests satisfying the condition above you have to assemble one with the maximum number of problems. Your task is to find this number of problems.", "input_spec": "The first line of the input contains one integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) — the number of problems in the problemset. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$) — difficulties of the problems. It is guaranteed that difficulties of the problems are distinct and are given in the increasing order.", "output_spec": "Print a single integer — maximum number of problems in the contest satisfying the condition in the problem statement.", "sample_inputs": ["10\n1 2 5 6 7 10 21 23 24 49", "5\n2 10 50 110 250", "6\n4 7 12 100 150 199"], "sample_outputs": ["4", "1", "3"], "notes": "NoteDescription of the first example: there are $$$10$$$ valid contests consisting of $$$1$$$ problem, $$$10$$$ valid contests consisting of $$$2$$$ problems ($$$[1, 2], [5, 6], [5, 7], [5, 10], [6, 7], [6, 10], [7, 10], [21, 23], [21, 24], [23, 24]$$$), $$$5$$$ valid contests consisting of $$$3$$$ problems ($$$[5, 6, 7], [5, 6, 10], [5, 7, 10], [6, 7, 10], [21, 23, 24]$$$) and a single valid contest consisting of $$$4$$$ problems ($$$[5, 6, 7, 10]$$$).In the second example all the valid contests consist of $$$1$$$ problem.In the third example are two contests consisting of $$$3$$$ problems: $$$[4, 7, 12]$$$ and $$$[100, 150, 199]$$$."}, "positive_code": [{"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nputs a.chunk_while { _1 * 2 >= _2 }.map(&:size).max"}, {"source_code": "def compute(n, as) \n last = nil \n max = 0 \n crt = 0\n\n as.each do |a|\n if last\n if last * 2 >= a\n crt += 1\n else\n crt = 1\n end\n else\n crt += 1\n end\n\n max = [crt, max].max\n last = a\n end\n\n max\n\nend\n\ndef solution\n n = read_int\n as = read_ints\n\n puts compute(n, as)\nend\n\ndef read_int\n gets.to_i\nend\n\ndef read_ints\n gets.split.map(&:to_i)\nend\n\ndef read_string\n gets.chomp\nend\n\n\nsolution unless ENV['TEST__MODE']\n"}, {"source_code": "#! /usr/bin/ruby\n\ngets\ninput = gets.chomp.split ' '\ninput.map! { |word| word.to_i }\n\ni = 0\nj = 0\nans = 0\n\nwhile j < input.size - 1\n if input[j + 1] <= 2 * input[j]\n j += 1\n else\n ans = [ans, j - i + 1].max\n j += 1\n i = j\n end\nend\n\nans = [ans, j - i + 1].max\n\nputs ans\n"}, {"source_code": "N = gets.to_i\nas = gets.split.map(&:to_i)\nans = 1\nlen = 1\nnow = as[0]\n(1...N).each do |i|\n if now*2 >= as[i]\n now = as[i]\n len += 1\n ans = len if ans < len\n else\n len = 1\n now = as[i]\n end\nend\nputs ans"}, {"source_code": "class Node\n\tattr_accessor :prev\n\tattr_accessor :next\n\tattr_accessor :data\n\n\tdef initialize(p,n,d)\n\t\t@prev = p \n\t\t@next = n \n\t\t@data = d \n\tend\nend\n\nclass Que\n\tdef initialize()\n\t\t@front = nil\n\t\t@rear = nil\n\t\t@size = 0\n\tend\n\n\tdef pushl(k)\n\t\t@size += 1\n\t\ta = Node.new(@rear,nil,k)\n\t\tif(@front == nil)\n\t\t\t@front = a\n\t\t\t@rear = a\n\t\telse\n\t\t\t@rear.next = a\n\t\t\t@rear = a\n\t\tend\n\tend\n\n\tdef popf()\n\t\t@size -= 1\n\t\trem = @front\n\t\t@front = @front.next\n\t\t# @front.prev = nil\n\t\tif (@size == 0)\n\t\t\t@rear = nil\n\t\telse \n\t\t\t@front.prev = nil\n\t\tend\n\t\treturn rem.data\n\tend\n\n\tdef contains(k)\n\t\tnow = @front\n\t\twhile(now != nil)\n\t\t\tif (now.data == k)\n\t\t\t\treturn true\n\t\t\telse\n\t\t\t\tnow = now.next\n\t\t\tend\n\t\tend\n\t\treturn false\n\tend\n\n\tdef pushf(k)\n\t\t@size += 1\n\t\ta = Node.new(nil,@front,k)\n\t\t@front = a\n\t\tif(@front == nil)\n\t\t\t@rear = a\n\t\tend\n\tend\n\n\tdef popl()\n\t\t@size -= 1\n\t\ta = @rear\n\t\tif(@size == 0)\n\t\t\t@front = nil \n\t\t\t@rear = nil \n\t\telse\n\t\t\t@rear = @rear.prev \n\t\t\t@rear.next = nil \n\t\tend\n\t\treturn a.data\n\tend\n\tdef front()\n\t\treturn @front.data\n\tend\n\n\tdef size()\n\t\treturn @size\n\tend\n\n\tdef rear()\n\t\treturn @rear.data\n\tend\nend\n\n\nuseless = gets.chomp.to_i\ninp = gets.chomp.split(\" \")\narr = Array.new() \nfor i in inp \n\tarr << i.to_i\nend\nif useless == 1\n\tprint(1)\n\texit\nend\nq = Que.new()\nq.pushl(arr[0])\nmax = -10\ncur = 1\ni = 1\nmarked = true\nuntil i == useless\n\tif (marked)\n\t\tq.pushl(arr[i])\n\tend\n\tif(arr[i] <= q.front*2)\n\t\tcur += 1\n\t\tif(max < cur)\n\t\t\tmax= cur\n\t\tend\n\t\tmarked = true \n\telse\n\t\tq.popf\n\t\tif(q.size == 1)\n\t\t\tcur = 0 \n\t\tend\n\t\ti -= 1\n\t\tmarked = false\n\tend\n\ti += 1\nend\n\nprint(max)"}, {"source_code": "def binary_search(range)\n lower = 0\n upper = range.size\n while lower < upper\n mi = (lower + upper)/2\n yield(range[mi]) ? upper = mi : lower = mi + 1\n end\n [range[upper], upper]\nend\n\nn = gets.to_i\na = gets.split.map(&:to_i)\ndp = Array.new(n)\ne = Array.new(n)\ndp[0] = 1\ne[0] = 0\n(1...n).each do |i|\n if 2*a[e[i-1]] >= a[i]\n e[i] = i\n dp[i] = dp[i-1]+1\n elsif 2*a[i-1] >= a[i]\n e[i] = i\n dp[i] = 2\n else\n e[i] = e[i-1]\n dp[i] = dp[i-1]\n end\nend\nputs dp.max"}], "negative_code": [{"source_code": "class Node\n\tattr_accessor :prev\n\tattr_accessor :next\n\tattr_accessor :data\n\n\tdef initialize(p,n,d)\n\t\t@prev = p \n\t\t@next = n \n\t\t@data = d \n\tend\nend\n\nclass Que\n\tdef initialize()\n\t\t@front = nil\n\t\t@rear = nil\n\t\t@size = 0\n\tend\n\n\tdef pushl(k)\n\t\t@size += 1\n\t\ta = Node.new(@rear,nil,k)\n\t\tif(@front == nil)\n\t\t\t@front = a\n\t\t\t@rear = a\n\t\telse\n\t\t\t@rear.next = a\n\t\t\t@rear = a\n\t\tend\n\tend\n\n\tdef popf()\n\t\t@size -= 1\n\t\trem = @front\n\t\t@front = @front.next\n\t\t# @front.prev = nil\n\t\tif (@size == 0)\n\t\t\t@rear = nil\n\t\telse \n\t\t\t@front.prev = nil\n\t\tend\n\t\treturn rem.data\n\tend\n\n\tdef contains(k)\n\t\tnow = @front\n\t\twhile(now != nil)\n\t\t\tif (now.data == k)\n\t\t\t\treturn true\n\t\t\telse\n\t\t\t\tnow = now.next\n\t\t\tend\n\t\tend\n\t\treturn false\n\tend\n\n\tdef pushf(k)\n\t\t@size += 1\n\t\ta = Node.new(nil,@front,k)\n\t\t@front = a\n\t\tif(@front == nil)\n\t\t\t@rear = a\n\t\tend\n\tend\n\n\tdef popl()\n\t\t@size -= 1\n\t\ta = @rear\n\t\tif(@size == 0)\n\t\t\t@front = nil \n\t\t\t@rear = nil \n\t\telse\n\t\t\t@rear = @rear.prev \n\t\t\t@rear.next = nil \n\t\tend\n\t\treturn a.data\n\tend\n\tdef front()\n\t\treturn @front.data\n\tend\n\n\tdef size()\n\t\treturn @size\n\tend\n\n\tdef rear()\n\t\treturn @rear.data\n\tend\nend\n\n\nuseless = gets.chomp.to_i\ninp = gets.chomp.split(\" \")\narr = Array.new() \nfor i in inp \n\tarr << i.to_i\nend\nif useless == 1\n\tprint(1)\n\texit\nend\nq = Que.new()\nq.pushl(arr[0])\nmax = -10\ncur = 1\ni = 1\nmarked = true\nuntil i == useless\n\tif (marked)\n\t\tq.pushl(arr[i])\n\tend\n\tif(arr[i] <= q.front*2)\n\t\tcur += 1\n\t\tif(max < cur)\n\t\t\tmax= cur\n\t\tend\n\t\tmarked = true \n\telse\n\t\tq.popf\n\t\tcur -= 1\n\t\ti -= 1\n\t\tmarked = false\n\tend\n\ti += 1\nend\n\nprint(max)\n\n"}], "src_uid": "5088d1d358508ea3684902c8e32443a3"} {"nl": {"description": "Okabe needs to renovate the Future Gadget Laboratory after he tried doing some crazy experiments! The lab is represented as an n by n square grid of integers. A good lab is defined as a lab in which every number not equal to 1 can be expressed as the sum of a number in the same row and a number in the same column. In other words, for every x, y such that 1 ≤ x, y ≤ n and ax, y ≠ 1, there should exist two indices s and t so that ax, y = ax, s + at, y, where ai, j denotes the integer in i-th row and j-th column.Help Okabe determine whether a given lab is good!", "input_spec": "The first line of input contains the integer n (1 ≤ n ≤ 50) — the size of the lab. The next n lines contain n space-separated integers denoting a row of the grid. The j-th integer in the i-th row is ai, j (1 ≤ ai, j ≤ 105).", "output_spec": "Print \"Yes\" if the given lab is good and \"No\" otherwise. You can output each letter in upper or lower case.", "sample_inputs": ["3\n1 1 2\n2 3 1\n6 4 1", "3\n1 5 2\n1 1 1\n1 2 3"], "sample_outputs": ["Yes", "No"], "notes": "NoteIn the first sample test, the 6 in the bottom left corner is valid because it is the sum of the 2 above it and the 4 on the right. The same holds for every number not equal to 1 in this table, so the answer is \"Yes\".In the second sample test, the 5 cannot be formed as the sum of an integer in the same row and an integer in the same column. Thus the answer is \"No\"."}, "positive_code": [{"source_code": "n = gets.strip.to_i\ngrid = Array.new(n)\nn.times do |i|\n grid[i] = gets.strip.split.map(&:to_i)\nend\ni = 0\nans = true\nwhile i < n && ans\n j = 0\n while j < n && ans\n cell = grid[i][j]\n if cell != 1\n found = false\n x = 0\n while x < n && !found\n y = 0\n while y < n && !found\n if x != i && y != j\n a = grid[i][y]\n b = grid[x][j]\n sum = a + b\n found = true if sum == cell\n end\n y += 1\n end\n x += 1\n end\n ans = found\n end\n j += 1\n end\n i += 1\nend\nputs ans ? \"Yes\" : \"No\"\n"}], "negative_code": [], "src_uid": "d1d50f0780d5c70e6773d5601d7d41e8"} {"nl": {"description": "Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of n distinct integers.Unfortunately, the size of a is too small. You want a bigger array! Your friends agree to give you a bigger array, but only if you are able to answer the following question correctly: is it possible to sort the array a (in increasing order) by reversing exactly one segment of a? See definitions of segment and reversing in the notes.", "input_spec": "The first line of the input contains an integer n (1 ≤ n ≤ 105) — the size of array a. The second line contains n distinct space-separated integers: a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ 109).", "output_spec": "Print \"yes\" or \"no\" (without quotes), depending on the answer. If your answer is \"yes\", then also print two space-separated integers denoting start and end (start must not be greater than end) indices of the segment to be reversed. If there are multiple ways of selecting these indices, print any of them.", "sample_inputs": ["3\n3 2 1", "4\n2 1 3 4", "4\n3 1 2 4", "2\n1 2"], "sample_outputs": ["yes\n1 3", "yes\n1 2", "no", "yes\n1 1"], "notes": "NoteSample 1. You can reverse the entire array to get [1, 2, 3], which is sorted.Sample 3. No segment can be reversed such that the array will be sorted.DefinitionsA segment [l, r] of array a is the sequence a[l], a[l + 1], ..., a[r].If you have an array a of size n and you reverse its segment [l, r], the array will become:a[1], a[2], ..., a[l - 2], a[l - 1], a[r], a[r - 1], ..., a[l + 1], a[l], a[r + 1], a[r + 2], ..., a[n - 1], a[n]."}, "positive_code": [{"source_code": "n = gets.to_i\na = gets.split.map &:to_i\n\ndef sign n\n n == 0 ? 0 : n.abs / n\nend\nif a.length > 1\n segment_directions = []\n #negative descending, positive ascending\n direction = sign(a[1] - a[0])\n start_index = 0\n end_index = 1\n\n for i in 2 ... n\n local_direction = sign(a[i] - a[i - 1])\n if local_direction == direction\n end_index = i\n else\n segment_directions << [direction, start_index, end_index]\n direction = local_direction\n start_index = i - 1\n end_index = i\n end\n end\n segment_directions << [direction, start_index, end_index]\n\n descending_segments = segment_directions.select {|d| d[0] == -1}\n\n if descending_segments.length > 1\n puts \"no\"\n elsif descending_segments.length == 1\n reverse_start_index = descending_segments[0][1]\n reverse_end_index = descending_segments[0][2]\n seg_strt_gt_pre = reverse_start_index > 0? a[reverse_end_index] > a [reverse_start_index - 1] : true\n seg_end_lt_nxt = reverse_end_index < n - 1 ? a[reverse_start_index] < a[reverse_end_index + 1] : true;\n if seg_strt_gt_pre && seg_end_lt_nxt\n puts \"yes\"\n puts \"#{reverse_start_index + 1} #{reverse_end_index + 1}\"\n else\n puts \"no\"\n end\n else\n puts \"yes\"\n puts \"1 1\"\n end \nelse\n puts \"yes\", \"1 1\"\nend\n\n"}, {"source_code": "N = gets.to_i\narr = gets.chomp.split(' ').map(&:to_i)\n\nrra = arr.sort\n\nif rra == arr \n puts \"yes\\n1 1\"\n exit\nend\n\nl = ((0...arr.size).find_index {|i| arr[i] != rra[i]}).to_i\nr = ((0...arr.size).to_a.reverse.find {|i| arr[i] != rra[i]}).to_i\n\nbrr = []\nbrr = arr.first(l) + arr[l..r].reverse + arr.last(arr.size - 1 - r)\n\nif brr == rra\n puts 'yes'\n puts \"#{l + 1} #{r + 1}\"\nelse\n puts 'no'\nend"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nb = a.sort\ns = a.zip(b).index{|x, y| x != y} || 0\ne = a.zip(b).rindex{|x, y| x != y} || 0\na[s..e] = a[s..e].reverse\nif a == b\n puts 'yes', \"#{s + 1} #{e + 1}\"\nelse\n puts 'no'\nend\n\n"}, {"source_code": "n = gets.to_i\na = gets.split.map &:to_i\nl = n\n(1...n).each do |i|\n if a[i] < a[i - 1] then\n l = i\n break\n end\nend\nif l == n then\n puts 'yes'\n puts '1 1'\nelse\n r = n\n (l...n).each do |i|\n if a[i] > a[i - 1] then\n r = i\n break\n end\n end\n flag = true\n (0...(l-1)).each do |i|\n if a[i] > a[r - 1] then\n flag = false\n break\n end\n end\n if flag then\n (r...n).each do |i|\n if a[i] < a[i - 1] || a[i] < a[l-1] then\n flag = false\n break\n end\n end\n end\n if flag then\n puts 'yes'\n puts l.to_s + ' ' + r.to_s\n else\n puts 'no'\n end\nend\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nb = a.sort\ns = a.zip(b).index{|x, y| x != y} || 0\ne = a.zip(b).rindex{|x, y| x != y} || 0\na[s..e] = a[s..e].reverse\nif a == b\n puts 'yes', \"#{s + 1} #{e + 1}\"\nelse\n puts 'no'\nend\n\n"}, {"source_code": "n = gets.to_i;\narr = gets.chomp.split(' ').map(&:to_i);\nsorted = arr.sort;\n\nif (arr == sorted)\n puts \"yes\\n1 1\";\n exit;\nend\n\nleft = (0...n).find_index {|i| arr[i] != sorted[i]};\nright = (0...n).to_a.rindex {|i| arr[i] != sorted[i]};\n\narr = arr.first(left) + arr[left..right].reverse + arr.last(n - right - 1);\n\nif (arr == sorted)\n puts \"yes\\n#{left + 1} #{right + 1}\\n\";\nelse\n puts \"no\";\nend\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nb = a.sort\ns = a.zip(b).index{|x, y| x != y} || 0\ne = a.zip(b).rindex{|x, y| x != y} || 0\na[s..e] = a[s..e].reverse\nif a == b\n puts 'yes', \"#{s + 1} #{e + 1}\"\nelse\n puts 'no'\nend\n"}, {"source_code": "n=gets.to_i\na=gets.chomp.split.map(&:to_i).unshift(0)\na[n+1]=1000000001\ni=1\nwhile ia[i+1]\n\ti+=1\nend\nif i==n\n\tputs \"yes\"\n\tputs \"1 1\"\nelse\n\tj=n\n\twhile j>1\n\t\tbreak if a[j]a[j+1]||a[i-1]>a[j]\n\t\tputs \"no\"\n\telse\n\t\tputs\"yes\"\n\t\tputs \"#{i} #{j}\"\n\tend\nend"}, {"source_code": "N = gets.to_i\narr = gets.chomp.split(' ').map(&:to_i)\nrra = arr.sort\n\nif rra == arr\n puts \"yes\\n1 1\"\n exit\nend\n\nl = ((0...arr.size).find_index {|i| arr[i] != rra[i]}).to_i\nr = ((0...arr.size).to_a.reverse.find {|i| arr[i] != rra[i]}).to_i\n\nbrr = []\nbrr = arr.first(l) + arr[l..r].reverse + arr.last(arr.size - 1 - r)\n\nif brr == rra\n puts \"yes\"\n puts \"#{l + 1} #{r + 1}\"\nelse\n puts \"no\"\nend"}, {"source_code": "size = gets.to_i\n\nnumbers = gets.split(' ').map(&:to_i)\n\nsorted_array = numbers.sort\n\nif numbers == sorted_array\n puts 'yes'\n puts '1 1'\nelse\n i = 0\n j = size - 1\n\n while numbers[i] == sorted_array[i]\n i += 1\n\n break if i == size\n end\n\n while numbers[j] == sorted_array[j]\n j -= 1\n\n break if j == -1\n end\n\n numbers[i..j] = numbers[i..j].reverse\n\n if numbers == sorted_array\n puts 'yes'\n puts \"#{i + 1} #{j + 1}\"\n else\n puts 'no'\n end\nend\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nb = a.sort\ns = a.zip(b).index{|x, y| x != y} || 0\ne = a.zip(b).rindex{|x, y| x != y} || 0\na[s..e] = a[s..e].reverse\nif a == b\n puts 'yes', \"#{s + 1} #{e + 1}\"\nelse\n puts 'no'\nend\n\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nb = a.sort\ns = a.zip(b).index{|x, y| x != y} || 0\ne = a.zip(b).rindex{|x, y| x != y} || 0\na[s..e] = a[s..e].reverse\nif a == b\n puts 'yes', \"#{s + 1} #{e + 1}\"\nelse\n puts 'no'\nend\n\n"}, {"source_code": "n=gets.to_i\na=gets.split.map{|i| i.to_i}\nb=a.clone.sort\nif a==b\n\tputs \"yes\\n1 1\"\n\texit\nend\nl=0\nn.times{|x| l=x; break if a[x]!=b[x]}\nr=0\nn.times{|x| r=n-1-x; break if a[n-x-1]!=b[n-x-1]}\nif a[0,l]+a[l..r].reverse+a[r+1..-1] == b\n\tputs \"yes\\n#{l+1} #{r+1}\"\nelsif\n\tputs 'no'\nend\n\t\t\n"}, {"source_code": "n = gets.rstrip.to_i\narr = gets.rstrip.split(' ').map(&:to_i)\n\nsorted_arr = arr.sort\n\nl = r = 0\nfor i in 0..(n - 1)\n if arr[i] != sorted_arr[i]\n l = i\n break\n end\nend\n\nj = n - 1\nwhile j >= 0 do\n if arr[j] != sorted_arr[j]\n r = j\n break\n end\n j -= 1\nend\n\nj = r\nfor i in l..r\n if arr[i] != sorted_arr[j]\n puts 'no'\n exit\n else\n j -= 1\n end\nend\n\nputs 'yes'\nputs \"#{l + 1} #{r + 1}\"\n"}, {"source_code": "n=gets.to_i\nx=gets.split.map{|e| e.to_i}\nc=\"\"\narr=\"\"\nminP=0\nmaxP=0\n(x.size-1).times{|i|\n\ts=\"\"\n\tif x[i]0 then\n\t\tx1=x.slice(0..(minP-1))\n\t\tx2=x.slice(minP..maxP)\n\t\tif x1.max>x2.min then\n\t\t\tok=false\n\t\tend\n\tend\n\tif maxP<(x.size-1) then\n\t\tx1=x.slice(minP..maxP)\n\t\tx2=x.slice(maxP+1,x.size)\n\t\tif x1.max>x2.min then\n\t\t\tok=false\n\t\tend\n\tend\n\tif ok==false then\n\t\tputs \"no\"\n\telse\n\t\tputs \"yes\"\n\t\tputs [minP+1,maxP+1]*\" \"\n\tend\nelse\n\tputs \"no\"\nend\n\t"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split(' ').map(&:to_i)\ns = 0\ne = 0\nfor i in 0..n-2\n\tif a[i]>a[i+1]\n\t\ts = i\n\t\tbreak\n\tend\nend\n(n-1).downto(1) do |i|\n\tif a[i] a[s-1] ) && (e == n-1 or a[s] 0\n item_before_fidx = a[fidx - 1]\n end\n\n if lidx < (n-1)\n item_after_lidx = a[lidx + 1]\n end\n\n if item_before_fidx <= a[lidx] && item_after_lidx >= a[fidx]\n puts \"yes\"\n puts \"#{fidx+1} #{lidx+1}\" # 1-based indices\n else\n puts \"no\"\n end\nend\n\n# if yes: print 'yes' and show begin/end index\n# else no\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nb = a.sort\ns = a.zip(b).index{|x, y| x != y} || 0\ne = a.zip(b).rindex{|x, y| x != y} || 0\na[s..e] = a[s..e].reverse\nif a == b\n puts 'yes', \"#{s + 1} #{e + 1}\"\nelse\n puts 'no'\nend"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nb = a.sort\ns = a.zip(b).index{|x, y| x != y} || 0\ne = a.zip(b).rindex{|x, y| x != y} || 0\na[s..e] = a[s..e].reverse\nif a == b\n puts 'yes', \"#{s + 1} #{e + 1}\"\nelse\n puts 'no'\nend\n\n"}, {"source_code": "n = gets.to_i\narr =gets.split(\" \").map(&:to_i)\nprev = arr[0]\nseg = nil\nseg_start = nil\nseg_end = nil\nans = true\nseg_found = false\n(1..n-1).each do |i|\n\tif arr[i] >= prev\t\t\n\t\tif seg\n\t\t\tseg = nil\n\t\t\tseg_end = i - 1\n\t\tend\n\telse\n\t\tunless seg\n\t\t\tseg = true\n\t\t\tseg_found = true\n\t\t\tseg_start = i - 1\n\t\tend\n\tend\n\tprev = arr[i]\nend\nif seg_found\n\tseg_end = n - 1 if !seg_end && seg && seg_start\n\t# puts \"#{seg_start} #{seg_end}\"\n\tfirst_part = arr[0..seg_start - 1] unless seg_start == 0\n\tlast_part = arr[seg_end+1..n-1]\n\tmid_part = arr[seg_start..seg_end].reverse if arr[seg_start..seg_end]\n\tnew_arr = (first_part || [] )+ (mid_part || []) + (last_part || [])\n\t# puts \"#{first_part} #{arr[seg_start..seg_end].reverse} #{arr[seg_end+1..n-1]}\"\n\tans = false unless new_arr == arr.sort\nelse\n\tif arr == arr.sort\n\t\tseg_start = 0\n\t\tseg_end = 0\n\telse\n\t\tans = false \n\tend\nend\n\nif ans\n\tputs \"yes\"\n\tputs \"#{seg_start+1} #{seg_end+1}\"\nelse\n\tputs \"no\"\nend\n"}, {"source_code": "def isSorted(a)\n a.inject([-1, true]) { |p, v|\n [v, (p[1] and p[0] <= v)]\n }[1]\nend\n\nn = gets.to_i\na = gets.split.map(&:to_i)\np1 = -1\np2 = -1\n\nprev = -1\na.each_with_index { |val, idx|\n if p1 == -1\n if prev > val\n p1 = idx-1\n end\n else\n if prev < val\n p2 = idx-1\n break\n end\n end\n prev = val\n}\n\nif p1 == -1 and p2 == -1\n p1 = p2 = 0\nelsif p2 == -1\n p2 = n-1\nend\nsorted = a[0...p1] + a[p1..p2].reverse + a[p2+1...n]\n\nif isSorted sorted\n puts \"yes\"\n puts \"#{p1+1} #{p2+1}\"\nelse\n puts \"no\"\nend\n"}, {"source_code": "def find_point(a)\n p1 = -1\n p2 = -1\n prev = -1\n a.each_with_index do |val, idx|\n if p1 == -1\n if prev > val\n p1 = idx-1\n end\n else\n if prev < val\n p2 = idx-1\n break\n end\n end\n prev = val\n end\n [p1, p2]\nend\n\nn = gets.to_i\na = gets.split.map(&:to_i)\np1,p2 = find_point a\nif p1 == -1 and p2 == -1\n p1 = p2 = 0\nelsif p2 == -1\n p2 = n-1\nend\nsorted = a[0...p1] + a[p1..p2].reverse + a[p2+1...n]\n\nif sorted == a.sort\n puts \"yes\"\n puts \"#{p1+1} #{p2+1}\"\nelse\n puts \"no\"\nend\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nb = a.sort\ns = nil\ne = nil\nn.times do |i|\n s = i if a[i] != b[i] && s == nil \n e = i if a[i] != b[i]\nend\ns = e = 0 if s == nil || e == nil\naa = [a[0...s], a[s..e].reverse, a[e + 1..-1]]\nif aa.join == b.join\n puts \"yes\"\n puts \"#{s + 1} #{e + 1}\"\nelse\n puts \"no\"\nend\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nb = a.sort\ns = a.zip(b).index{|x, y| x != y} || 0\ne = a.zip(b).rindex{|x, y| x != y} || 0\na[s..e] = a[s..e].reverse\nif a == b\n puts 'yes', \"#{s + 1} #{e + 1}\"\nelse\n puts 'no'\nend\n"}, {"source_code": "# coding: utf-8\n\nn = readline.to_i\narr = readline.chomp.split(' ').map(&:to_i)\n\nsarr = arr.sort\n\nif sarr == arr then\n puts 'yes'\n puts '1 1'\nelse\n\n l = ((0..arr.size-1).find_index {|i| arr[i] != sarr[i]}).to_i\n r = ((0..arr.size-1).to_a.reverse.find {|i| arr[i] != sarr[i]}).to_i\n \n parr = []\n parr = arr.first(l) + arr[l..r].reverse + arr.last(arr.size-1-r)\n\n if parr == sarr then\n puts 'yes'\n puts \"#{l+1} #{r+1}\"\n else\n puts 'no'\n end\nend"}, {"source_code": "# coding: utf-8\n\nn = readline.to_i\narr = readline.chomp.split(' ').map(&:to_i)\n\nsarr = arr.sort\n\nif sarr == arr then\n puts 'yes'\n puts '1 1'\n STDOUT.flush\n exit! 0\nend\n\nl = ((0..arr.size-1).find_index {|i| arr[i] != sarr[i]}).to_i\nr = ((0..arr.size-1).to_a.reverse.find {|i| arr[i] != sarr[i]}).to_i\n\nparr = []\nparr = arr.first(l) + arr[l..r].reverse + arr.last(arr.size-1-r)\n\nif parr == sarr then\n puts 'yes'\n puts \"#{l+1} #{r+1}\"\nelse\n puts 'no'\nend"}, {"source_code": "length = gets.split.map{|_|_.to_i}\narray = gets.split.map{|_|_.to_i}\n\nsorted_array = array.sort\nstarted = 0\nended = 0\nswitch = 0\nindex = 0\n\nwhile index <= array.length\n if sorted_array[index] != array[index]\n if switch % 2 == 0\n started = index\n switch += 1\n else\n ended = index\n end\n end\n \n index += 1\nend\n\nif switch == 0 || sorted_array[started..ended] == array[started..ended].reverse\n puts \"yes\"\n puts [started + 1, ended + 1].join(\" \")\nelse\n puts \"no\"\nend"}, {"source_code": "n = gets.chomp.to_i\nlist = gets.chomp.split.map(&:to_i)\n\nsorted_list = list.sort\ndiff_pos = []\n\nfor i in 0..(n - 1)\n if list[i] != sorted_list[i]\n diff_pos << i\n end\nend\n\nif diff_pos.empty?\n l = 0\n r = 0\nelse\n l = diff_pos[0]\n r = diff_pos[-1]\nend\n\nreverse_segment = list[l..r].dup\n\nfor i in 0..((reverse_segment.length - 1)/ 2.0)\n temp = reverse_segment[i]\n reverse_segment[i] = reverse_segment[reverse_segment.length - 1 - i]\n reverse_segment[reverse_segment.length - 1 - i] = temp\nend\n\nlist[l..r] = reverse_segment\n\nif list == sorted_list\n puts \"yes\"\n print l + 1, \" \", r + 1\nelse\n puts \"no\"\nend\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nb = a.sort\ns = a.zip(b).index{|x, y| x != y} || 0\ne = a.zip(b).rindex{|x, y| x != y} || 0\na[s..e] = a[s..e].reverse\nif a == b\n puts 'yes', \"#{s + 1} #{e + 1}\"\nelse\n puts 'no'\nend\n\n"}, {"source_code": "gets\na = gets.split(' ').map(&:to_i)\n\nl = 0\nr = a.size\n\na.unshift 0\na.push 10 ** 9 + 1\n\nwhile l < r && a[l] < a[l+1]\n l += 1\nend\n\nwhile r > l && a[r] > a[r-1]\n r -= 1\nend\n\nif a[l] < a[r+1] && a[r] > a[l-1] && (l+1..r).all? { |i| a[i-1] > a[i] }\n puts \"yes\"\n puts \"#{l} #{r}\"\nelse\n puts \"no\"\nend\n"}, {"source_code": "n = gets.to_i\na = gets.chomp.split.map(&:to_i)\np = q = nil\n(0...n - 1).each { |i|\n if a[i] > a[i + 1]\n if q\n puts 'no'\n exit\n end\n p = i unless p\n else\n q = i if p && !q\n end\n}\np = n - 1 unless p\nq = n - 1 if p && !q\nputs (p == 0 || a[p - 1] < a[q]) && (q == n - 1 || a[p] < a[q + 1]) ? \"yes\\n#{[p + 1, q + 1].join(' ')}\" : 'no'"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nb = a.sort\ns = a.zip(b).index{|x, y| x != y} || 0\ne = a.zip(b).rindex{|x, y| x != y} || 0\na[s..e] = a[s..e].reverse\nif a == b\n puts 'yes', \"#{s + 1} #{e + 1}\"\nelse\n puts 'no'\nend\n\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nb = a.sort\ns = a.zip(b).index{|x, y| x != y} || 0\ne = a.zip(b).rindex{|x, y| x != y} || 0\na[s..e] = a[s..e].reverse\nif a == b\n puts 'yes', \"#{s + 1} #{e + 1}\"\nelse\n puts 'no'\nend\n\n"}, {"source_code": "def no;puts \"no\";exit;end\nn=gets.to_i\na=gets.split.map{|s|s.to_i}\nl = nil\nr = nil\n(n-1).times do |i|\n if r\n no if a[i] > a[i+1]\n elsif l\n r = i if a[i] < a[i+1]\n else\n l = i if a[i] > a[i+1]\n end\nend\nl || (l, r = 0, 0)\nr ||= n-1\nno if r < n-1 && a[l] > a[r+1]\nno if l > 0 && a[l-1] > a[r]\nputs \"yes\"\nputs [l+1, r+1].join(\" \")"}], "negative_code": [{"source_code": "N = gets.to_i\narr = gets.chomp.split(' ').map(&:to_i)\n\nrra = arr.sort\n\nif rra == arr \n puts 'yes'\n puts '1 1'\n exit! 0\nend\n\nl = ((0...arr.size).find_index {|i| arr[i] != rra[i]}).to_i\nr = ((0...arr.size).to_a.reverse.find {|i| arr[i] != rra[i]}).to_i\n\nbrr = []\nbrr = arr.first(l) + arr[l..r].reverse + arr.last(arr.size - 1 - r)\n\nif brr == rra\n puts 'yes'\n puts \"#{l + 1} #{r + 1}\"\nelse\n puts 'no'\nend"}, {"source_code": "N = gets.to_i\narr = gets.chomp.split(' ').map(&:to_i)\nrra = arr.sort\n\nif rra == arr\n puts \"yes\\n1 1\"\n exit\nend\n\nl = ((0...arr.size).find_index {|i| arr != rra[i]}).to_i\nr = ((0...arr.size).to_a.reverse.find {|i| arr[i] != rra[i]}).to_i\n\nbrr = []\nbrr = arr.first(l) + arr[l..r].reverse + arr.last(arr.size - 1 - r)\n\nif brr == rra\n puts \"yes\"\n puts \"#{l + 1} #{r + 1}\"\nelse\n puts \"no\"\nend"}, {"source_code": "size = gets.to_i\n\nnumbers = gets.split(' ').map(&:to_i)\n\nsorted_array = numbers.sort\n\nif numbers == sorted_array\n puts 'yes'\n puts '1 1'\nelse\n i = 0\n j = size - 1\n\n while numbers[i] == sorted_array[i]\n i += 1\n end\n\n while numbers[j] == sorted_array[j]\n j -= 1\n end\n\n left_index = i\n right_index = j\n\n while left_index <= right_index\n break if numbers[i] != sorted_array[j]\n left_index +=1\n right_index -= 1\n end\n\n if left_index < right_index\n puts 'no'\n else\n puts 'yes'\n puts \"#{i + 1} #{j + 1}\"\n end\n\n # numbers[i..j] = numbers[i..j].reverse\n\n # if numbers == sorted_array\n # puts 'yes'\n # puts \"#{i + 1} #{j + 1}\"\n # else\n # puts 'no'\n # end\n\n\nend\n"}, {"source_code": "size = gets.to_i\n\nnumbers = gets.split(' ').map(&:to_i)\n\nsorted_array = numbers.sort\n\nif numbers == sorted_array\n puts 'yes'\n puts '1 1'\nelse\n i = 0\n j = size - 1\n\n while numbers[i] == sorted_array[i]\n i += 1\n end\n\n while numbers[j] == sorted_array[j]\n j -= 1\n end\n\n left_index = i\n right_index = j\n\n while left_index < right_index\n break if numbers[i] != numbers[j]\n left_index +=1\n right_index -= 1\n end\n\n if left_index < right_index\n puts 'no'\n else\n puts 'yes'\n puts \"#{i + 1} #{j + 1}\"\n end\n\n # numbers[i..j] = numbers[i..j].reverse\n\n # if numbers == sorted_array\n # puts 'yes'\n # puts \"#{i + 1} #{j + 1}\"\n # else\n # puts 'no'\n # end\n\n\nend\n"}, {"source_code": "size = gets.to_i\n\nnumbers = gets.split(' ').map(&:to_i)\n\nsorted_array = numbers.sort\n\nif numbers == sorted_array\n puts 'yes'\n puts '1 1'\nelse\n i = 0\n j = size - 1\n\n while numbers[i] == sorted_array[i]\n i += 1\n end\n\n while numbers[j] == sorted_array[j]\n j -= 1\n end\n\n left_index = i\n right_index = j\n\n not_sorted = false\n\n while left_index <= right_index\n if numbers[left_index] != sorted_array[right_index]\n not_sorted = true\n break\n end\n left_index +=1\n right_index -= 1\n end\n\n if not_sorted\n puts 'no'\n else\n puts 'yes'\n puts \"#{i + 1} #{j + 1}\"\n end\n\n # numbers[i..j] = numbers[i..j].reverse\n\n # if numbers == sorted_array\n # puts 'yes'\n # puts \"#{i + 1} #{j + 1}\"\n # else\n # puts 'no'\n # end\nend\n"}, {"source_code": "size = gets.to_i\n\nnumbers = gets.split(' ').map(&:to_i)\n\nsorted_array = numbers.sort\n\nif numbers == sorted_array\n puts 'yes'\n puts '1 1'\nelse\n i = 0\n j = size - 1\n\n while numbers[i] == sorted_array[i]\n i += 1\n end\n\n while numbers[j] == sorted_array[j]\n j -= 1\n end\n\n left_index = i\n right_index = j\n\n while left_index <= right_index\n break if numbers[i] != numbers[j]\n left_index +=1\n right_index -= 1\n end\n\n if left_index < right_index\n puts 'no'\n else\n puts 'yes'\n puts \"#{i + 1} #{j + 1}\"\n end\n\n # numbers[i..j] = numbers[i..j].reverse\n\n # if numbers == sorted_array\n # puts 'yes'\n # puts \"#{i + 1} #{j + 1}\"\n # else\n # puts 'no'\n # end\n\n\nend\n"}, {"source_code": "size = gets.to_i\n\nnumbers = gets.split(' ').map(&:to_i)\n\nsorted_array = numbers.sort\n\nif size == 1\n puts 'yes'\n puts '1 1'\nelse\n i = 0\n j = size\n\n while numbers[i] == sorted_array[i]\n i += 1\n end\n\n while numbers[j] == sorted_array[j]\n j -= 1\n end\n\n numbers[i..j] = numbers[i..j].reverse\n\n puts numbers == sorted_array ? 'yes' : 'no'\nend\n"}, {"source_code": "size = gets.to_i\n\nnumbers = gets.split(' ').map(&:to_i)\n\nsorted_array = numbers.sort\n\nif size == 1\n puts 'yes'\n puts '1 1'\nelse\n i = 0\n j = 1\n while i < size - 1\n b = numbers.dup\n b[i..j] = b[i..j].reverse!\n if b == sorted_array\n puts 'yes'\n puts \"#{i + 1} #{j + 1}\"\n break\n end\n j += 1\n if j == size\n i += 1\n j = i + 1\n end\n end\n puts 'no' if i >= size - 1\nend\n"}, {"source_code": "size = gets.to_i\n\nnumbers = gets.split(' ').map(&:to_i)\n\nsorted_array = numbers.sort\n\nif numbers == sorted_array\n puts 'yes'\n puts '1 1'\nelse\n i = 0\n j = size - 1\n\n while numbers[i] == sorted_array[i]\n i += 1\n end\n\n while numbers[j] == sorted_array[j]\n j -= 1\n end\n\n left_index = i\n right_index = j\n\n while left_index <= right_index\n break if numbers[left_index] != sorted_array[right_index]\n left_index +=1\n right_index -= 1\n end\n\n if left_index <= right_index\n puts 'no'\n else\n puts 'yes'\n puts \"#{i + 1} #{j + 1}\"\n end\n\n # numbers[i..j] = numbers[i..j].reverse\n\n # if numbers == sorted_array\n # puts 'yes'\n # puts \"#{i + 1} #{j + 1}\"\n # else\n # puts 'no'\n # end\nend\n"}, {"source_code": "n = gets.rstrip.to_i\narr = gets.rstrip.split(' ').map(&:to_i)\n\nl = 0\narr.each_with_index do |v, i|\n if arr[i + 1] && arr[i] > arr[i + 1]\n l = i\n break\n end\nend\n\nr = l\nwhile arr[l] >= arr[r] && r < arr.size - 1 do\n r += 1\nend\n\nr -= 1 if r != (arr.size - 1)\nr -= 1 if arr[r] > arr[l]\n\nsorted_arr = arr.sort\narr[l], arr[r] = arr[r], arr[l]\n\nif sorted_arr == arr\n puts 'yes'\n puts \"#{l + 1} #{r + 1}\"\nelse\n puts 'no'\nend\n"}, {"source_code": "n = gets.rstrip.to_i\narr = gets.rstrip.split(' ').map(&:to_i)\n\nl = 0\narr.each_with_index do |v, i|\n if arr[i + 1] && arr[i] > arr[i + 1]\n l = i\n break\n end\nend\n\nr = l\nwhile arr[l] >= arr[r] && r < arr.size - 1 do\n r += 1\nend\n\nr -= 1 if r != (arr.size - 1)\nr -= 1 if arr[r] > arr[l]\n\nsorted_arr = arr.sort\narr[l], arr[r] = arr[r], arr[l]\n\nif sorted_arr == arr\n puts 'yes'\n puts \"#{arr[l]} #{arr[r]}\"\nelse\n puts 'no'\nend\n"}, {"source_code": "n=gets.to_i\nx=gets.split.map{|e| e.to_i}\nc=\"\"\narr=\"\"\nminP=0\nmaxP=0\n(x.size-1).times{|i|\n\ts=\"\"\n\tif x[i]0 then\n\t\tx1=x.slice(0..(minP-1))\n\t\tx2=x.slice(minP..maxP)\n\t\tif x1.max>x2.min then\n\t\t\tok=false\n\t\tend\n\tend\n\tif maxP<(x.size-1) then\n\t\tx1=x.slice(minP..maxP)\n\t\tx2=x.slice(maxP+1,x.size)\n\t\tif x1.max>x2.min then\n\t\t\tok=false\n\t\tend\n\tend\n\tif ok==false then\n\t\tputs \"no\"\n\telse\n\t\tputs \"yes\"\n\t\tputs [minP+1,maxP+1]*\" \"\n\tend\nelse\n\tputs \"no\"\nend\n\n"}, {"source_code": "n, k = gets.split.map{|_|_.to_i}\na = gets.split.map{|_|_.to_i}"}, {"source_code": "length = gets.split.map{|_|_.to_i}\narray = gets.split.map{|_|_.to_i}\n\nsorted_array = array.sort\nstarted = -1\nended = -1\nswitch = 0\nindex = 0\n\nwhile index <= array.length\n if sorted_array[index] != array[index]\n if switch % 2 == 0\n started = index\n switch += 1\n else\n ended = index\n end\n end\n \n index += 1\nend\n\nif switch == 0 || sorted_array[started..ended] == array[started..ended].reverse\n puts \"yes\"\n puts [started + 1, ended + 1].join(\" \")\nelse\n puts \"no\"\nend"}, {"source_code": "length = gets.split.map{|_|_.to_i}\narray = gets.split.map{|_|_.to_i}\n\nsorted_array = array.sort\nstarted = -1\nended = -1\nswitch = 0\nindex = 0\n\nwhile index <= array.length\n if sorted_array[index] != array[index]\n if switch % 2 == 0\n started = index\n switch += 1\n else\n ended = index\n end\n end\n \n index += 1\nend\n\nif sorted_array[started..ended] == array[started..ended].reverse\n puts \"yes\"\n puts [started + 1, ended + 1].join(\" \")\nelse\n puts \"no\"\nend"}, {"source_code": "n = gets.chomp.to_i\nlist = gets.chomp.split.map(&:to_i)\n\nsorted_list = list.sort\n\nif sorted_list == list\n puts \"yes\"\n print 1, \" \", n\nelse\n diff_pos = []\n\n for i in 0..(n - 1)\n if list[i] != sorted_list[i]\n diff_pos << i\n end\n end\n\n l = diff_pos[0]\n r = diff_pos[-1]\n\n reverse_segment = list[l..r].dup\n\n for i in 0..((reverse_segment.length - 1)/ 2.0)\n temp = reverse_segment[i]\n reverse_segment[i] = reverse_segment[reverse_segment.length - 1 - i]\n reverse_segment[reverse_segment.length - 1 - i] = temp\n end\n\n list[l..r] = reverse_segment\n\n if list == sorted_list\n puts \"yes\"\n print l + 1, \" \", r + 1\n else\n puts \"no\"\n end\nend\n"}, {"source_code": "n = gets.chomp.to_i\nlist = gets.chomp.split.map(&:to_i)\n\ntemp_list = list.dup\nresult = []\n\nfor r in 0..(n - 1)\n for l in 0..r\n temp = list[r]\n list[r] = list[l]\n list[l] = temp\n if list == list.sort\n break result = [l + 1, r + 1]\n end\n end\n\n list = temp_list\nend\n\nif result.empty?\n puts \"no\"\nelse\n puts \"yes\"\n print result[0], \" \", result[1]\nend\n"}], "src_uid": "c9744e25f92bae784c3a4833c15d03f4"} {"nl": {"description": "Jeff's got n cards, each card contains either digit 0, or digit 5. Jeff can choose several cards and put them in a line so that he gets some number. What is the largest possible number divisible by 90 Jeff can make from the cards he's got?Jeff must make the number without leading zero. At that, we assume that number 0 doesn't contain any leading zeroes. Jeff doesn't have to use all the cards.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 103). The next line contains n integers a1, a2, ..., an (ai = 0 or ai = 5). Number ai represents the digit that is written on the i-th card.", "output_spec": "In a single line print the answer to the problem — the maximum number, divisible by 90. If you can't make any divisible by 90 number from the cards, print -1.", "sample_inputs": ["4\n5 0 5 0", "11\n5 5 5 5 5 5 5 5 0 5 5"], "sample_outputs": ["0", "5555555550"], "notes": "NoteIn the first test you can make only one number that is a multiple of 90 — 0.In the second test you can make number 5555555550, it is a multiple of 90."}, "positive_code": [{"source_code": "n = gets.to_i\nhs = { 0 => 0, 5 => 0 }\ngets.split(' ').map{|x| hs[x.to_i] += 1}\nif hs[0] == 0\n puts -1\nelsif hs[5] >= 9\n puts \"5\" * ( hs[5] / 9 * 9 ) + \"0\" * hs[0]\nelse\n hs[0] > 0\n puts 0\nend"}, {"source_code": "#!/usr/bin/ruby\nh=Hash.new(0)\ngets;gets.split.each{|e|h[e.to_i]+=1}\np h[0]>=1 ? ('5'*(h[5]/9*9)+'0'*h[0]).to_i : -1"}, {"source_code": "n,res0,res5 = gets.chomp.to_i, 0, 0\na = gets.chomp.split.map(&:to_i)\na.each { |x| if x==5; res5+=1 else res0+=1 end }\nif res0>0; puts '5'*(res5/9*9)+'0'*(res5>8?res0:1) else puts '-1' end"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split.map(&:to_i)\nres0, res5 = 0, 0\na.each { |x| if x==5; res5+=1 else res0+=1 end }\nif res0>0\n puts '5'*(res5/9*9)+'0'*(res5>8?res0:1)\nelse\n puts '-1'\nend"}, {"source_code": "cnt = gets.to_i\ncards = gets.split\nn0 = n5 = 0\ncards.each {|c| \n\tif c == \"0\"\n\t\tn0 += 1\n\telse\n\t\tn5 += 1\n\tend\n}\nif n0 == 0\n\tprint -1\nelsif n5 < 9\n\tprint 0\nelse\n\tprint \"5\" * ((n5/9)*9) + \"0\" * n0\nend"}, {"source_code": "f=gets.chomp.to_i\nz=gets.chomp.split(\" \").count('0')\nf=f-z\nif z==0 ||(f<9 && z==0)\nputs \"-1\"\nelsif f<9 && z>0\nputs \"0\"\nelse\np=\"555555555\"*(f/9)+(\"0\"*z) \nputs p\nend\n"}, {"source_code": "gets \na = gets.strip.split(' ').map(&:to_i)\n\nif a.select { |i| i.zero? }.size.zero?\n puts \"-1\"\n exit\nend\n\nsum = 0\ncount = 0\nmx = 0\na.each { |e|\n if e == 5\n sum += 5\n count += 1\n end\n if sum % 9 == 0\n mx = count\n end\n}\n\nif mx.zero?\n puts \"0\"\n exit\nend\nmx.times { print '5' }\n(a.size - count).times { print '0' }\nputs \"\"\n\n"}, {"source_code": "n=gets.to_i\nx=gets.split.map{|e| e.to_i}\nif x.index(0) then\n\ty=x.count{|e|\n\t\te==5\n\t}\n\tz=x.size-y\n\twhile (y*50)%90!=0\n\t\ty-=1\n\tend\n\n\tif y==0 then\n\t\tputs 0\n\telse\n\t\t\n\t\ty.times{\n\t\t\tprint 5\t\n\t\t}\n\t\tz.times{\n\t\t\tprint 0\n\t\t}\n\t\tputs \"\"\n\tend\nelse\n\tputs -1\nend\n\n"}, {"source_code": "a=[*$<][1].chomp.split\nr='5'*(a.count('5')/9*9)+'0'*a.count('0')\nputs r.length>0&&r[-1]=='0'?r.to_i : -1\n"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split\n\na_zero = a.count \"0\"\na_five = a.count \"5\"\n\nif a_five >= 9 && a_zero >= 1\n puts \"5\" * (a_five / 9 * 9) + \"0\" * a_zero\nelsif a_zero >= 1\n puts 0\nelse\n puts -1\nend\n\n\n"}, {"source_code": "cnt=[0]*6\na = $<.to_a[1].split.map(&:to_i)\na.each do |x|\n cnt[x] += 1\nend\nif cnt[5] >= 9 and cnt[0] > 0\n puts '5' * (cnt[5] - cnt[5] % 9) + '0' * cnt[0]\nelsif cnt[0] > 0\n puts '0'\nelse\n puts '-1'\nend\n"}, {"source_code": "c=[0]*6\n$<.to_a[1].split.map(&:to_i).each { |x| c[x] += 1 }\nputs (c[0] > 0) ? '5'*(c[5]-c[5]%9) + '0'*(c[5]>8?c[0]:1) : '-1'\n"}, {"source_code": "c=[0]*6\n$<.to_a[1].split.map(&:to_i).each{|x|c[x]+=1}\nputs (c[0]>0)?'5'*(c[5]-c[5]%9)+'0'*(c[5]>8?c[0]:1):'-1'"}, {"source_code": "c=Array.new(6,0)\n$<.to_a[1].split.map(&:to_i).each{|i|c[i]+=1}\na,b=c[0],c[5]-c[5]%9\nputs a==0?-1:(b==0?0:\"5\"*b+\"0\"*a)"}, {"source_code": "c=Array.new(6,0)\ngets\ngets.split.map(&:to_i).each{|i|c[i]+=1}\na,b=c[0],c[5]-c[5]%9\nif a == 0\n\tp (-1)\nelsif b == 0\n\tp 0\nelse\n\tputs \"5\"*b+\"0\"*a\nend"}, {"source_code": "n = gets.to_i\na = gets.split(' ').map{|e| e.to_i}\nc = Hash.new(0)\na.each{|e|\n c[e] += 1;\n}\n\nif c[5]/9 > 0 and c[0] > 0 then\n (c[5]/9 * 9).times{ print 5 }\n c[0].times{print 0}\nelsif c[0] > 0 \n print 0\nelse\n print -1\nend\nputs"}, {"source_code": "#! /usr/bin/env ruby\n\nn = STDIN.gets.to_i\nnumbers = STDIN.gets.to_s.split(' ').map(&:to_i)\n\nif numbers.length != n\n raise ArgumentError.new\nend\n\ndef check(numbers, n)\n if numbers.none?(&:zero?)\n return nil\n end\n numbers.sort!.reverse!\n\n while numbers.first == 5\n sum = numbers.count{|i| i == 5} * 5\n if sum % 9 == 0\n return numbers.join.to_i\n end\n numbers.shift\n end\n\n 0\nend\n\nanswer = check(numbers, n)\nif answer.nil?\n puts \"-1\"\nelse\n puts answer\nend\n\n"}, {"source_code": "n = gets.to_i\na = gets.split.map { |x| x.to_i }\n\nc0 = a.count 0\nc5 = a.count 5\n\nif c0 == 0\n\tputs -1\nelsif c5 < 9\n\tputs 0\nelse\n\tputs '5' * (c5 / 9 * 9) + '0' * c0\nend"}], "negative_code": [{"source_code": "n = gets.to_i\nhs = { 0 => 0, 5 => 0 }\ngets.split(' ').map{|x| hs[x.to_i] += 1}\nif hs[5] >= 9\n puts \"5\" * ( hs[5] / 9 * 9 ) + \"0\" * hs[0]\nelsif\n hs[0] > 0\n puts 0\nelse\n puts -1\nend"}, {"source_code": "#!/usr/bin/ruby\nh=Hash.new(0)\ngets;gets.split.each{|e|h[e.to_i]+=1}\nputs h[0]>=1&&h[5]>=9 ? '5'*(h[5]/9*9)+'0'*h[0] : 0\n"}, {"source_code": "#!/usr/bin/ruby\nh=Hash.new(0)\ngets;gets.split.each{|e|h[e.to_i]+=1}\nputs h[0]>=1 ? '5'*(h[5]/9*9)+'0'*h[0] : -1"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split.map(&:to_i)\nres0, res5 = 0, 0\na.each { |x| if x==5; res5+=1 else res0+=1 end }\nputs '5'*(res5/3*3)+'0'*(res5>2?res0:1)"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split.map(&:to_i)\nres0, res5 = 0, 0\na.each { |x| if x==5; res5+=1 else res0+=1 end }\nif res0>0\n puts '5'*(res5/3*3)+'0'*(res5>2?res0:1)\nelse\n puts '-1'\nend"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split.map(&:to_i)\nres0, res5 = 0, 0\na.each { |x| if x==5; res5+=1 else res0+=1 end }\nputs '5'*(res5/5*5)+'0'*res0"}, {"source_code": "cnt = gets.to_i\ncards = gets.split\nn0 = n5 = 0\ncards.each {|c| \n\tif c == \"0\"\n\t\tn0 += 1\n\telse\n\t\tn5 += 1\n\tend\n}\nif n0 == 0\n\tprint -1\nelsif n5 < 9\n\tprint 0\nelse\n\tprint \"9\" * ((n5/9)*9) + \"0\" * n0\nend"}, {"source_code": "gets \na = gets.strip.split(' ').map(&:to_i)\n\nif a.select { |i| i.zero? }.size.zero?\n puts \"-1\"\n exit\nend\n\nsum = 0\ncount = 0\nmx = 0\na.each { |e|\n if e == 5\n sum += 5\n count += 1\n end\n if sum % 9 == 0\n mx = count\n end\n}\n\nmx.times { print '5' }\n(a.size - count).times { print '0' }\nputs \"\"\n\n"}, {"source_code": "gets \na = gets.strip.split(' ').map(&:to_i)\n\nif a.select { |i| i.zero? }.size.zero?\n puts \"-1\"\n exit\nend\n\nsum = 0\ncount = 0\nmx = 0\na.each { |e|\n if e == 5\n sum += 5\n count += 1\n end\n if sum % 9 == 0\n mx = count\n end\n}\n\nmx.times { print '5' }\nputs '0'\n\n"}, {"source_code": "gets \na = gets.strip.split(' ').map(&:to_i)\n\nif a.select { |i| i.zero? }.size.zero?\n puts \"-1\"\n exit\nend\n\nif a.select { |i| i.zero? }.size == 0\n puts \"0\"\n exit\nend\n\nsum = 0\ncount = 0\nmx = 0\na.each { |e|\n if e == 5\n sum += 5\n count += 1\n end\n if sum % 9 == 0\n mx = count\n end\n}\n\nmx.times { print '5' }\n(a.size - count).times { print '0' }\nputs \"\"\n\n"}, {"source_code": "a=[*$<][1].chomp.split\nr='5'*(a.count('5')/9*9)+'0'*a.count('0')\nputs r.length>0?r.to_i : -1\n"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split\n\na_zero = a.count \"0\"\na_five = a.count \"5\"\n\ns = \"\"\ndivideds = []\n(0..1000).each do |i|\n s_zero = (s + \"0\")\n s_five = (s + \"5\")\n if s_zero.to_i % 90 == 0 and \n s_zero.count(\"0\") <= a_zero and\n s_zero.count(\"5\") <= a_five then\n divideds << s_zero\n end\n if s_five.to_i % 90 == 0 and\n s_five.count(\"0\") <= a_zero and\n s_five.count(\"5\") <= a_five then\n divideds << s_five\n end\n s += \"5\"\nend\n\nans = divideds.sort[-1]\nputs ans ? ans : -1\n"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split\n\na_five = a.count \"5\"\na_zero = a.count \"0\"\n\ns = \"\"\ndivideds = []\n(0...a_five).each do |i|\n if (s+\"0\").to_i % 90 == 0\n divideds << (s+\"0\").to_i\n end\n if (s+\"5\").to_i % 90 == 0 \n divideds << (s+\"5\").to_i\n end\n s += \"5\"\nend\n\nans = divideds.reject{|i| i % 90 != 0}.sort[-1]\nif ans \n puts ans\nelse \n puts -1\nend\n"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split\n\na_zero = a.count \"0\"\na_five = a.count \"5\"\n\nif a_five >= 9 and a_zero >= 1\n puts \"5\" * a_five + \"0\" * a_zero\nelsif a_zero >= 1\n puts 0\nelse\n puts -1\nend\n"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split\n\na_zero = a.count \"0\"\na_five = a.count \"5\"\n\ns = \"\"\ndivideds = []\n(0...1000).each do |i|\n s_zero = (s + \"0\")\n s_five = (s + \"5\")\n if s_zero.to_i % 90 == 0 and \n s_zero.count(\"0\") <= a_zero and\n s_zero.count(\"5\") <= a_five then\n divideds << s_zero\n end\n if s_five.to_i % 90 == 0 and\n s_five.count(\"0\") <= a_zero and\n s_five.count(\"5\") <= a_five then\n divideds << s_five\n end\n s += \"5\"\nend\n\nans = divideds.sort[-1]\nputs ans ? ans : -1\n"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split\n\na_zero = a.count \"0\"\na_five = a.count \"5\"\n\nif a_five >= 9 and a_zero >= 1\n puts (\"5\" * a_five + \"0\" * a_zero).gsub(\"000000000\",\"555555555\")\nelsif a_zero >= 1\n puts 0\nelse\n puts -1\nend\n"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split\n\na_five = a.count \"5\"\na_zero = a.count \"0\"\n\ns = \"\"\ndivideds = []\n(0...a_five).each do |i|\n if (s+\"0\").to_f % 90 == 0\n divideds << (s+\"0\").to_i\n end\n if (s+\"5\").to_f % 90 == 0 \n divideds << (s+\"5\").to_i\n end\n s += \"5\"\nend\n\nans = divideds.reject{|i| i % 90 != 0}.sort[-1]\nif ans \n puts ans\nelse \n puts -1\nend\n"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split\n\na_zero = a.count \"0\"\na_five = a.count \"5\"\n\nif a_five >= 9\n puts \"5\" * (a_five / 9 * 9) + \"0\" * a_zero\nelsif a_zero >= 1\n puts 0\nelse\n puts -1\nend\n\n\n"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split\n\na_zero = a.count \"0\"\na_five = a.count \"5\"\n\nif a_five >= 9\n puts \"5\" * (a_five / 9) + \"0\" * a_zero\nelsif a_zero >= 1\n puts 0\nelse\n puts -1\nend\n\n\n"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split\n\na_zero = a.count \"0\"\na_five = a.count \"5\"\n\ns = \"\"\ndivideds = []\n(0...a_five).each do |i|\n s_zero = (s + \"0\")\n s_five = (s + \"5\")\n if s_zero.to_i % 90 == 0 and \n s_zero.count(\"0\") <= a_zero and\n s_five.count(\"5\") <= a_five then\n divideds << s_zero\n end\n if s_five.to_i % 90 == 0 and\n s_zero.count(\"0\") <= a_zero and\n s_five.count(\"5\") <= a_five then\n divideds << s_five\n end\n s += \"5\"\nend\n\nans = divideds.sort[-1]\nif ans \n puts ans\nelse \n puts -1\nend\n"}, {"source_code": "cnt=[0]*6\na = $<.to_a[1].split.map(&:to_i)\na.each do |x|\n cnt[x] += 1\nend\nif cnt[5] >= 9\n puts '5' * (cnt[5] - cnt[5] % 9) + '0' * cnt[0]\nelsif cnt[0] > 0\n puts '0'\nelse\n puts '-1'\nend\n"}, {"source_code": "cnt=[0]*6\na = $<.to_a[1].split.map(&:to_i)\na.each do |x|\n cnt[x] += 1\nend\nif cnt[5] >= 9\n puts '5' * (cnt[5] - cnt[5] % 9) + '0' * cnt[0]\nelsif cnt[0]\n puts '0'\nelse\n puts '-1'\nend\n"}, {"source_code": "n = gets.to_i\na = gets.split(' ').map{|e| e.to_i}\nc = Hash.new(0)\na.each{|e|\n c[e] += 1;\n}\n\nif c[5]/9 > 0 and c[0] > 0 then\n c[5]/9.times{\n print 5\n }\n puts 0\nelse\n puts 0\nend"}, {"source_code": "n = gets.to_i\na = gets.split(' ').map{|e| e.to_i}\nc = Hash.new(0)\na.each{|e|\n c[e] += 1;\n}\n\nif c[5]/9 > 0 and c[0] > 0 then\n c[5]/9.times{\n print 5\n }\n puts 0\nelse\n puts -1\nend"}, {"source_code": "n = gets.to_i\na = gets.split(' ').map{|e| e.to_i}\nc = Hash.new(0)\na.each{|e|\n c[e] += 1;\n}\n\nif c[5]/9 > 0 and c[0] > 0 then\n c[5]/9.times{\n print 5\n }\n puts 0\nelsif c[0] > 0 \n puts 0\nelse\n puts -1\nend"}, {"source_code": "n = gets.to_i\na = gets.split.map { |x| x.to_i }\n\nc0 = a.count 0\nc5 = a.count 5\n\nif c0 == 0 and c5 < 9\n\tputs -1\nelsif c5 < 9\n\tputs 0\nelse\n\tputs '5' * (c5 / 9 * 9) + '0' * c0\nend"}], "src_uid": "409b27044d5ec97b5315c92d4112376f"} {"nl": {"description": "This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) — bus, (2) — ring, (3) — star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown. ", "input_spec": "The first line contains two space-separated integers n and m (4 ≤ n ≤ 105; 3 ≤ m ≤ 105) — the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≤ xi, yi ≤ n) — the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.", "output_spec": "In a single line print the network topology name of the given graph. If the answer is the bus, print \"bus topology\" (without the quotes), if the answer is the ring, print \"ring topology\" (without the quotes), if the answer is the star, print \"star topology\" (without the quotes). If no answer fits, print \"unknown topology\" (without the quotes).", "sample_inputs": ["4 3\n1 2\n2 3\n3 4", "4 4\n1 2\n2 3\n3 4\n4 1", "4 3\n1 2\n1 3\n1 4", "4 4\n1 2\n2 3\n3 1\n1 4"], "sample_outputs": ["bus topology", "ring topology", "star topology", "unknown topology"], "notes": null}, "positive_code": [{"source_code": "class List\n attr_accessor :element, :next_elem\n\n def initialize(element, next_elem)\n @element = element\n @next_elem = next_elem\n end\nend\n\nclass Graph\n attr_accessor :vertices\n\n def initialize(vertices)\n @vertices = vertices\n end\n\n def add_edge(number1, number2)\n if vertices[number1].nil? then\n vertices[number1] = List.new(number2, nil)\n else\n vertices[number1] = List.new(number2, vertices[number1])\n end\n end\n\n def print_edges \n vertices.each_with_index do |vertex, index| \n # next if vertex == \"\"\n while !vertex.nil? do \n puts \"edge #{index} - #{vertex.element}\"\n vertex = vertex.next_elem \n end \n end \n end \n\nend \n\nnetwork = Graph.new([])\nn, m = gets.strip.split(\" \").map(&:to_i)\nnumber_of_connected_vertices = Array.new(n, 0)\nm.times do \n vertex1, vertex2 = gets.strip.split(\" \").map{|num| num.to_i - 1}\n network.add_edge(vertex1, vertex2)\n network.add_edge(vertex2, vertex1)\n number_of_connected_vertices[vertex2] += 1\n number_of_connected_vertices[vertex1] += 1\nend\nresults = number_of_connected_vertices.group_by{|i| i}\nif results[n - 1] && results[n - 1].count == 1 &&\n results[1] && results[1].count == n - 1\n answer = 'star topology'\nelsif results[1] && results[1].count == 2 &&\n results[2] && results[2].count == n - 2\n answer = 'bus topology'\nelsif results[2] && results[2].count == n\n answer = 'ring topology'\nelse\n answer = 'unknown topology'\nend\nputs answer\n\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\n\nedges = m.times.map{ gets.split.map(&:to_i) }\nv = n.times.map{ 0 }\n\nif m == n\n edges.each do |e|\n v[e[0] - 1] += 1\n v[e[1] - 1] += 1\n if v[e[1] - 1] > 2 || v[e[0] - 1] >2\n puts 'unknown topology'\n exit\n end\n end\n puts 'ring topology'\n exit\nelsif m + 1 == n\n edges.each do |e|\n v[e[0] - 1] += 1\n v[e[1] - 1] += 1\n end\n v.sort!\n if v[0] == 1 && v[1] == 1 && v[2] == 2\n puts 'bus topology'\n exit\n elsif v[-1] == m\n puts 'star topology'\n exit\n end\nend\n\nputs 'unknown topology'\n"}, {"source_code": "begin\n input = gets.chomp.split(\" \")\n n = input[0].to_i\n m = input[1].to_i\n\n v1s = []\n v2s = []\n\n count_arr = []\n\n edges = []\n\n twos = 0\n nontwos = 0\n\n m.times do |i|\n input = gets.chomp.split(\" \")\n\n v1 = input[0].to_i\n v2 = input[1].to_i\n\n v1s << v1\n v2s << v2\n\n count_arr[v1] ||= 0\n count_arr[v2] ||= 0\n\n count_arr[v1] += 1\n count_arr[v2] += 1\n\n if count_arr[v1] == m || count_arr[v2] == m\n puts \"star topology\"\n abort\n end\n edges << i\n end\n\n count_arr.compact!\n\n count_arr.each do |el|\n if el == 2\n twos += 1\n else\n if el > 2\n puts \"unknown topology\"\n abort\n else\n nontwos += 1\n end\n end\n end\n\n if (twos == count_arr.size - 2) && (nontwos == 2)\n puts \"bus topology\"\n abort\n end\n\n if n == m\n puts \"ring topology\"\n abort\n end\n\n puts \"unknown topology\"\nrescue Exception\nend\n"}, {"source_code": "at_exit do\n vertex_count, edge_count = get_numbers_from_line[0..1]\n linked_count = [0]*vertex_count\n edge_count.times do\n x,y = get_numbers_from_line[0..1]\n linked_count[x-1] += 1\n linked_count[y-1] += 1\n end\n\n counts = {}\n linked_count.each do |count|\n counts[count] ||= 0\n counts[count] += 1\n end\n one_count = counts[1] || 0\n two_count = counts[2] || 0\n\n if one_count == 2 && two_count == vertex_count - 2 && edge_count == vertex_count-1\n puts 'bus topology'\n elsif one_count == 0 && two_count == vertex_count && edge_count == vertex_count\n puts 'ring topology'\n elsif one_count == vertex_count-1 && counts[vertex_count-1] == 1 && edge_count == vertex_count - 1\n puts 'star topology'\n else\n puts 'unknown topology'\n end\n\nend\n\ndef get_numbers_from_line\n gets.strip.split.map(&:to_i)\nend"}], "negative_code": [], "src_uid": "7bb088ce5e4e2101221c706ff87841e4"} {"nl": {"description": "The zombies are gathering in their secret lair! Heidi will strike hard to destroy them once and for all. But there is a little problem... Before she can strike, she needs to know where the lair is. And the intel she has is not very good.Heidi knows that the lair can be represented as a rectangle on a lattice, with sides parallel to the axes. Each vertex of the polygon occupies an integer point on the lattice. For each cell of the lattice, Heidi can check the level of Zombie Contamination. This level is an integer between 0 and 4, equal to the number of corners of the cell that are inside or on the border of the rectangle.As a test, Heidi wants to check that her Zombie Contamination level checker works. Given the output of the checker, Heidi wants to know whether it could have been produced by a single non-zero area rectangular-shaped lair (with axis-parallel sides). ", "input_spec": "The first line of each test case contains one integer N, the size of the lattice grid (5 ≤ N ≤ 50). The next N lines each contain N characters, describing the level of Zombie Contamination of each cell in the lattice. Every character of every line is a digit between 0 and 4. Cells are given in the same order as they are shown in the picture above: rows go in the decreasing value of y coordinate, and in one row cells go in the order of increasing x coordinate. This means that the first row corresponds to cells with coordinates (1, N), ..., (N, N) and the last row corresponds to cells with coordinates (1, 1), ..., (N, 1).", "output_spec": "The first line of the output should contain Yes if there exists a single non-zero area rectangular lair with corners on the grid for which checking the levels of Zombie Contamination gives the results given in the input, and No otherwise.", "sample_inputs": ["6\n000000\n000000\n012100\n024200\n012100\n000000"], "sample_outputs": ["Yes"], "notes": "NoteThe lair, if it exists, has to be rectangular (that is, have corners at some grid points with coordinates (x1, y1), (x1, y2), (x2, y1), (x2, y2)), has a non-zero area and be contained inside of the grid (that is, 0 ≤ x1 < x2 ≤ N, 0 ≤ y1 < y2 ≤ N), and result in the levels of Zombie Contamination as reported in the input."}, "positive_code": [{"source_code": "left = nil\ntop = nil\nright = nil\nbot = nil\n\nlat = gets.to_i.times.map { gets.chomp.split('').map(&:to_i) }\n\nlat.each_with_index { |line, i|\n break if left\n line.each_with_index { |cell, j|\n if cell == 4\n left = j\n top = i\n break\n end\n }\n}\n\nlat.each_with_index.reverse_each { |line, i|\n break if right\n line.each_with_index.reverse_each { |cell, j|\n if cell == 4\n right = j\n bot = i\n break\n end\n }\n}\n\nex = -> {\n puts 'No'\n exit\n}\n\nex.() if !left\n\nlat.each_with_index { |line, i|\n line.each_with_index { |cell, j|\n if j < left-1 || j > right+1 || i < top-1 || i > bot+1\n ex.() if cell != 0\n else\n corn = 0\n corn += 1 if j == left-1 || j == right+1\n corn += 1 if i == top-1 || i == bot+1\n\n if corn == 0\n ex.() if cell != 4\n elsif corn == 1\n ex.() if cell != 2\n elsif corn == 2\n ex.() if cell != 1\n end\n end\n }\n}\n\nputs 'Yes'\n"}], "negative_code": [], "src_uid": "cc1c4ce9d7d82fc10cb6439004599796"} {"nl": {"description": "Your favorite shop sells $$$n$$$ Kinder Surprise chocolate eggs. You know that exactly $$$s$$$ stickers and exactly $$$t$$$ toys are placed in $$$n$$$ eggs in total.Each Kinder Surprise can be one of three types: it can contain a single sticker and no toy; it can contain a single toy and no sticker; it can contain both a single sticker and a single toy. But you don't know which type a particular Kinder Surprise has. All eggs look identical and indistinguishable from each other.What is the minimum number of Kinder Surprise Eggs you have to buy to be sure that, whichever types they are, you'll obtain at least one sticker and at least one toy?Note that you do not open the eggs in the purchasing process, that is, you just buy some number of eggs. It's guaranteed that the answer always exists.", "input_spec": "The first line contains the single integer $$$T$$$ ($$$1 \\le T \\le 100$$$) — the number of queries. Next $$$T$$$ lines contain three integers $$$n$$$, $$$s$$$ and $$$t$$$ each ($$$1 \\le n \\le 10^9$$$, $$$1 \\le s, t \\le n$$$, $$$s + t \\ge n$$$) — the number of eggs, stickers and toys. All queries are independent.", "output_spec": "Print $$$T$$$ integers (one number per query) — the minimum number of Kinder Surprise Eggs you have to buy to be sure that, whichever types they are, you'll obtain at least one sticker and one toy", "sample_inputs": ["3\n10 5 7\n10 10 10\n2 1 1"], "sample_outputs": ["6\n1\n2"], "notes": "NoteIn the first query, we have to take at least $$$6$$$ eggs because there are $$$5$$$ eggs with only toy inside and, in the worst case, we'll buy all of them.In the second query, all eggs have both a sticker and a toy inside, that's why it's enough to buy only one egg.In the third query, we have to buy both eggs: one with a sticker and one with a toy."}, "positive_code": [{"source_code": "T = gets.to_i\nT.times do\n n,s,t = gets.split.map(&:to_i)\n a = s+t-n\n t = [s-a,t-a].sort\n p t[1]+1\nend"}, {"source_code": "T = gets.to_i\nT.times do\n\tn, s, t = gets.split.map(&:to_i)\n\ts, t = t, s if s < t\n\tif s + t <= n\n\t\tputs s + 1\n\telsif\n\t\tcross = s + t - n\n\t\tputs s + 1 - cross\n\tend\nend\n"}, {"source_code": "gets.to_i.times {\n n, s, t = gets.split.map &:to_i\n p [n - s + 1, n - t + 1].max\n}\n"}], "negative_code": [{"source_code": "T = gets.to_i\nT.times do\n n,s,t = gets.split.map(&:to_i)\n a = s+t-n\n t = [a,s-a,t-a].sort\n ans = t[0]+t[1]+1\n p [ans,n].min\nend"}], "src_uid": "fb0a4c8f737c36596c2d8c1ae0d1e34e"} {"nl": {"description": "Winters are just damn freezing cold in Nvodsk! That's why a group of n friends prefers to take a taxi, order a pizza and call girls. The phone numbers in the city consist of three pairs of digits (for example, 12-34-56). Each friend has a phonebook of size si (that's the number of phone numbers). We know that taxi numbers consist of six identical digits (for example, 22-22-22), the numbers of pizza deliveries should necessarily be decreasing sequences of six different digits (for example, 98-73-21), all other numbers are the girls' numbers.You are given your friends' phone books. Calculate which friend is best to go to when you are interested in each of those things (who has maximal number of phone numbers of each type). If the phone book of one person contains some number two times, you should count it twice. That is, each number should be taken into consideration the number of times it occurs in the phone book.", "input_spec": "The first line contains an integer n (1 ≤ n ≤ 100) — the number of friends. Then follow n data blocks that describe each friend's phone books. Each block is presented in the following form: first goes the line that contains integer si and string namei (0 ≤ si ≤ 100) — the number of phone numbers in the phone book of the i-th friend and the name of the i-th friend. The name is a non-empty sequence of uppercase and lowercase Latin letters, containing no more than 20 characters. Next si lines contain numbers as \"XX-XX-XX\", where X is arbitrary digits from 0 to 9.", "output_spec": "In the first line print the phrase \"If you want to call a taxi, you should call: \". Then print names of all friends whose phone books contain maximal number of taxi phone numbers. In the second line print the phrase \"If you want to order a pizza, you should call: \". Then print names of all friends who have maximal number of pizza phone numbers. In the third line print the phrase \"If you want to go to a cafe with a wonderful girl, you should call: \". Then print names of all friends who have maximal number of girls' phone numbers. Print the names in the order in which they are given in the input data. Separate two consecutive names with a comma and a space. Each line should end with exactly one point. For clarifications concerning the output form, see sample tests. It is necessary that you follow the output form strictly. Extra spaces are not allowed.", "sample_inputs": ["4\n2 Fedorov\n22-22-22\n98-76-54\n3 Melnikov\n75-19-09\n23-45-67\n99-99-98\n7 Rogulenko\n22-22-22\n11-11-11\n33-33-33\n44-44-44\n55-55-55\n66-66-66\n95-43-21\n3 Kaluzhin\n11-11-11\n99-99-99\n98-65-32", "3\n5 Gleb\n66-66-66\n55-55-55\n01-01-01\n65-43-21\n12-34-56\n3 Serega\n55-55-55\n87-65-43\n65-55-21\n5 Melnik\n12-42-12\n87-73-01\n36-04-12\n88-12-22\n82-11-43", "3\n3 Kulczynski\n22-22-22\n65-43-21\n98-12-00\n4 Pachocki\n11-11-11\n11-11-11\n11-11-11\n98-76-54\n0 Smietanka"], "sample_outputs": ["If you want to call a taxi, you should call: Rogulenko.\nIf you want to order a pizza, you should call: Fedorov, Rogulenko, Kaluzhin.\nIf you want to go to a cafe with a wonderful girl, you should call: Melnikov.", "If you want to call a taxi, you should call: Gleb.\nIf you want to order a pizza, you should call: Gleb, Serega.\nIf you want to go to a cafe with a wonderful girl, you should call: Melnik.", "If you want to call a taxi, you should call: Pachocki.\nIf you want to order a pizza, you should call: Kulczynski, Pachocki.\nIf you want to go to a cafe with a wonderful girl, you should call: Kulczynski."], "notes": "NoteIn the first sample you are given four friends. Fedorov's phone book contains one taxi number and one pizza delivery number, Melnikov's phone book only has 3 numbers of girls, Rogulenko's one has 6 taxi numbers and one pizza delivery number, Kaluzhin's one contains 2 taxi numbers and one pizza delivery number.Thus, if you need to order a taxi, you should obviously call Rogulenko, if you need to order a pizza you should call anybody of the following: Rogulenko, Fedorov, Kaluzhin (each of them has one number). Melnikov has maximal number of phone numbers of girls. "}, "positive_code": [{"source_code": "n = gets.to_i\na = []\nArray.new(n) do\n m, w = gets.split\n b = [0, 0, 0, w]\n m.to_i.times do\n s = gets.gsub(/\\D/, '')\n if s =~ /^(.)\\1*$/\n b[0] += 1\n elsif s.chars.to_a.sort.uniq * '' == s.reverse\n b[1] += 1\n else\n b[2] += 1\n end\n end\n a << b\nend\nb = ['call a taxi', 'order a pizza', 'go to a cafe with a wonderful girl']\n\n3.times do |i|\n c = a.map{|_| _[i]}.max\n d = a.select{|_| _[i] == c}.map{|_| _[3]}\n puts \"If you want to #{b[i]}, you should call: #{d * ', '}.\"\nend\n"}, {"source_code": "n=gets.to_i\nshop=[]\ntaxi=[]\nmax=[0,0,0]\n10.times{|k|\n taxi.push(k.to_s*6)\n}\nn.times{\n res=[0,0,0]\n k,name=gets.split\n k.to_i.times{\n a1,a2,a3=gets.split[0].split('-')\n t=a1+a2+a3\n if taxi.include?(t)\n res[0]+=1\n else\n v=Array.new(6){0}\n 6.times{|i| v[i]=t[i].to_i }\n ok=true\n 5.times{|i|\n ok=false if v[i]<=v[i+1]\n }\n if ok\n res[1]+=1\n else\n res[2]+=1\n end\n end\n }\n 3.times{|i|\n max[i]=[max[i],res[i]].max\n }\n shop.push([name,res])\n}\noutput=[\"If you want to call a taxi, you should call: \", \"If you want to order a pizza, you should call: \",\"If you want to go to a cafe with a wonderful girl, you should call: \"]\n3.times{|k|\n print output[k]\n start=true\n shop.each{|t|\n if (t[1][k]==max[k]) then\n print \", \" unless start\n start=false\n print t[0]\n end\n }\n puts \".\"\n}\n"}, {"source_code": "#! /usr/bin/env ruby\n\nt = gets.chomp.to_i\nd = {}\nname_list = []\n(1..t).each do |x|\n n, name = gets.chomp.split\n n = n.to_i\n d[name] = {}\n name_list << name\n d[name]['taxi'] = 0\n d[name]['pizza'] = 0\n d[name]['girl'] = 0\n (1..n).each do |y|\n num = gets.chomp.split(\"-\")\n if num.uniq.size == 1 and num.uniq.first.split(//).uniq.size == 1\n d[name]['taxi'] = (d[name]['taxi'] || 0) + 1\n elsif num.join.split(//).map(&:to_i) == num.join.split(//).map(&:to_i).sort.reverse.uniq\n d[name]['pizza'] = (d[name]['pizza'] || 0) + 1\n else\n d[name]['girl'] = (d[name]['girl'] || 0) + 1\n end\n end\nend\nmax_taxi = d.keys.map { |i| [d[i]['taxi'] || 0, i] }.sort.max.first\nmax_pizza = d.keys.map { |i| [d[i]['pizza'] || 0, i] }.sort.max.first\nmax_girl = d.keys.map { |i| [d[i]['girl'] || 0, i] }.sort.max.first\ntaxi = name_list.select {|i| d[i]['taxi'] == max_taxi }.join(\", \") + \".\"\npizza = name_list.select {|i| d[i]['pizza'] == max_pizza }.join(\", \") + \".\"\ngirl = name_list.select {|i| d[i]['girl'] == max_girl }.join(\", \") + \".\"\nprint (\"If you want to call a taxi, you should call: \" + taxi +\"\\n\").to_s\nprint (\"If you want to order a pizza, you should call: \" + pizza +\"\\n\").to_s\nprint (\"If you want to go to a cafe with a wonderful girl, you should call: \" + girl +\"\\n\").to_s\n"}, {"source_code": "#! /usr/bin/env ruby\n\ndef type_of_phone_number(number)\n return :taxi if number.uniq.size.equal? 1\n return :pizza if number.each_cons(2).all? {|x, y| x > y}\n return :girl\nend\n\nd = {}\nk = [:taxi, :pizza, :girl]\nk.each {|x| d[x] = [0, []]}\n\ngets.to_i.times do |x|\n n, name = gets.chomp.split\n numbers = n.to_i.times.map {type_of_phone_number(gets.scan(/[0-9]/).map(&:to_i))}\n k.each do |x|\n if numbers.count(x) > d[x].first\n d[x] = [numbers.count(x), [name]]\n elsif numbers.count(x) == d[x].first\n d[x].last << name\n end\n end\nend\n\nputs \"If you want to call a taxi, you should call: %s.\" %(d[:taxi][1] * \", \")\nputs \"If you want to order a pizza, you should call: %s.\" %(d[:pizza][1] * \", \")\nputs \"If you want to go to a cafe with a wonderful girl, you should call: %s.\" %(d[:girl][1] * \", \")\n"}, {"source_code": "#! /usr/bin/env ruby\n\nt = gets.chomp.to_i\nd = {}\nname_list = []\n(1..t).each do |x|\n n, name = gets.chomp.split\n n = n.to_i\n d[name] = {}\n name_list << name\n d[name]['taxi'], d[name]['pizza'], d[name]['girl'] = 0, 0, 0\n (1..n).each do |y|\n num = gets.chomp.split(\"-\")\n if num.uniq.size == 1 and num.uniq.first.split(//).uniq.size == 1\n d[name]['taxi'] = d[name]['taxi'] + 1\n elsif num.join.split(//).map(&:to_i) == num.join.split(//).map(&:to_i).sort.reverse.uniq\n d[name]['pizza'] = d[name]['pizza'] + 1\n else\n d[name]['girl'] = d[name]['girl'] + 1\n end\n end\nend\n\nmax_taxi = d.keys.map { |i| [d[i]['taxi'], i] }.sort.max.first\nmax_pizza = d.keys.map { |i| [d[i]['pizza'], i] }.sort.max.first\nmax_girl = d.keys.map { |i| [d[i]['girl'], i] }.sort.max.first\n\ntaxi = name_list.select {|i| d[i]['taxi'] == max_taxi }.join(\", \") + \".\"\npizza = name_list.select {|i| d[i]['pizza'] == max_pizza }.join(\", \") + \".\"\ngirl = name_list.select {|i| d[i]['girl'] == max_girl }.join(\", \") + \".\"\n\nprint (\"If you want to call a taxi, you should call: \" + taxi +\"\\n\").to_s\nprint (\"If you want to order a pizza, you should call: \" + pizza +\"\\n\").to_s\nprint (\"If you want to go to a cafe with a wonderful girl, you should call: \" + girl +\"\\n\").to_s\n"}, {"source_code": "require 'set'\n\ndef is_taxi(s)\n tmp = s.split(\"\").select{|c| c != '-'}\n set = Set.new tmp\n set.length == 1\nend\n\ndef is_pizza(s)\n flag = true\n tmp = nil\n s.split(\"\").each do |c|\n if c != '-'\n if tmp == nil\n tmp = c.to_i\n else\n if tmp <= c.to_i\n flag = false\n break\n else\n tmp = c.to_i\n end\n end\n end\n end\n flag\nend\n\ndef max_names(hash)\n max = hash.values.max\n names = hash.select{ |k, v|\n v == max\n }\n names.keys.join ', '\nend\n\n\nn = gets.chomp.to_i\n\ntaxi = {}\npizza = {}\ngirl = {}\n\nn.times do\n tmp = gets.chomp.split \" \"\n s = tmp[0].to_i\n name = tmp[1]\n \n taxi[name] = 0\n pizza[name] = 0\n girl[name] = 0\n \n s.times do\n tmp2 = gets.chomp\n if is_taxi(tmp2)\n taxi[name] += 1\n elsif is_pizza(tmp2)\n pizza[name] += 1\n else\n girl[name] += 1\n end\n end\nend\n\nputs \"If you want to call a taxi, you should call: \" + max_names(taxi) + \".\"\nputs \"If you want to order a pizza, you should call: \" + max_names(pizza) + \".\"\nputs \"If you want to go to a cafe with a wonderful girl, you should call: \" + max_names(girl) + \".\"\n\n\n"}, {"source_code": "def pizza?(num)\n num = num.gsub(/-/,'').chars.map(&:to_i)\n prev = 10\n num.each do |n|\n if prev > n\n prev = n\n else\n return false\n end\n end\n true\nend\n\ndef taxi?(num)\n a = num[0]\n num.gsub(/[#{a}\\-]/,'').size.zero?\nend\n\ndef girl?(num)\n !taxi?(num) && !pizza?(num)\nend\n\ndef which(num)\n return :taxi if taxi?(num)\n return :pizza if pizza?(num)\n return :girl\nend\n\ndics = {}\nn = STDIN.gets.chomp.to_i\nn.times do\n i, name = STDIN.gets.chomp.split(/ /)\n i = i.to_i\n dics[name] = {taxi: 0, pizza: 0, girl: 0}\n\n i.times do\n num = STDIN.gets.chomp\n dics[name][which(num)] += 1\n end\nend\n\n{taxi: \"If you want to call a taxi, you should call: \",\n pizza: \"If you want to order a pizza, you should call: \",\n girl: \"If you want to go to a cafe with a wonderful girl, you should call: \"}.each do |type, head|\n max = dics.map{|k,v| v[type] }.max\n puts head+dics.select{|k,v| v[type] == max }.keys.join(\", \")+\".\"\nend\n"}, {"source_code": "name,taxi,girls,pizza,maxt,maxp,maxg=[],[],[],[],0,0,0\nn=gets.to_i\nn.times do |i|\n\ttaxi[i],pizza[i],girls[i]=0,0,0\n\tt,name[i]=gets.chomp.split\n\tt=t.to_i\n\tt.times do |j|\n\t\tstr=gets.chomp.delete('-').split('')\n\t\tif str.uniq.count==1\n\t\t\ttaxi[i]+=1\n\t\telsif str[0]>str[1] && str[1]>str[2] && str[2]>str[3] && str[3]>str[4] && str[4]>str[5]\n\t\t\tpizza[i]+=1\n\t\telse\n\t\t\tgirls[i]+=1\n\t\tend\n\tend\nend\nmaxt,maxg,maxp=taxi.max,girls.max,pizza.max\nprint \"If you want to call a taxi, you should call: \"\npost=0\nfor i in 0...n\n\tif maxt==taxi[i]\n\t\tpost+=1\n\t\tprint (post==1) ? name[i] : \", #{name[i]}\"\n\tend\nend\nputs \".\"\nprint \"If you want to order a pizza, you should call: \"\npost=0\nfor i in 0...n\n\tif maxp==pizza[i]\n\t\tpost+=1\n\t\tprint (post==1) ? name[i] : \", #{name[i]}\"\n\tend\nend\nputs \".\"\nprint \"If you want to go to a cafe with a wonderful girl, you should call: \"\npost=0\nfor i in 0...n\n\tif maxg==girls[i]\n\t\tpost+=1\n\t\tprint (post==1) ? name[i] : \", #{name[i]}\"\n\tend\nend\nputs \".\""}, {"source_code": "n = gets.to_i\na = []\nArray.new(n) do\n m, w = gets.split\n b = [0, 0, 0, w]\n m.to_i.times do\n s = gets.gsub(/\\D/, '')\n if s =~ /^(.)\\1*$/\n b[0] += 1\n elsif s.chars.to_a.sort.uniq * '' == s.reverse\n b[1] += 1\n else\n b[2] += 1\n end\n end\n a << b\nend\nb = ['call a taxi', 'order a pizza', 'go to a cafe with a wonderful girl']\n\n3.times do |i|\n c = a.map{|_| _[i]}.max\n d = a.select{|_| _[i] == c}.map{|_| _[3]}\n puts \"If you want to #{b[i]}, you should call: #{d * ', '}.\"\nend"}, {"source_code": "n = gets.to_i\npb = []\nn.times do |i|\n a = [i]\n c,nm = gets.split\n a << nm\n h = Hash.new(0)\n c.to_i.times do\n pn = gets.strip.gsub(/-/,\"\").split(//)\n pn.uniq.sort.reverse\n if pn.uniq.size == 1\n h[:t] += 1\n elsif pn.uniq.sort.reverse == pn\n h[:p] += 1\n else\n h[:g] += 1\n end\n end\n a << h\n pb << a\nend\n\nmx = pb.map{|a| a[2][:t]}.max\ntx = pb.select{|a| a[2][:t] == mx }\n\n\nmx = pb.map{|a| a[2][:p]}.max\npz = pb.select{|a| a[2][:p] == mx }\n\nmx = pb.map{|a| a[2][:g]}.max\ngf = pb.select{|a| a[2][:g] == mx }\n\nputs \"If you want to call a taxi, you should call: #{tx.map{|a| a[1]}.join(\", \")}.\"\nputs \"If you want to order a pizza, you should call: #{pz.map{|a| a[1]}.join(\", \")}.\"\nputs \"If you want to go to a cafe with a wonderful girl, you should call: #{gf.map{|a| a[1]}.join(\", \")}.\"\n"}, {"source_code": "#!/usr/bin/ruby\nclass Number\n def initialize\n @mas=Array.new(6)\n end\n def mas\n @mas\n end\n def mas=(mas)\n @mas=mas\n end\n def isPizza()\n return @mas==@mas.uniq() && @mas==@mas.sort{|a,b| b<=>a}\n end\n def isTaxi()\n return @mas.uniq().size()==1\n end\nend\nclass Person\n def initialize\n @name=String.new\n @taxiElements=0\n @girlElements=0\n @pizzaElements=0\n @numberArray=[]\n end\n def taxiElements\n @taxiElements\n end\n def pizzaElements\n @pizzaElements\n end\n def girlElements\n @girlElements\n end\n def name\n @name\n end\n def name=(name)\n @name=name\n end\n def numberArray\n @numberArray\n end\n def numberArray=(numberArray)\n @numberArray=numberArray\n end\n def countElements()\n for i in 0..(@numberArray.size()-1)\n element=@numberArray[i]\n if element.isTaxi()\n @taxiElements=@taxiElements+1\n elsif (element.isPizza())\n @pizzaElements=@pizzaElements+1\n else\n @girlElements=@girlElements+1\n end\n end\n end\nend\n\nrequire 'scanf'\ncount=(scanf(\"%d\\n\"))[0]\narray=Array.new(count)\nfor i in 0..(count-1)\n array[i]=Person.new\n temp=scanf(\"%d %s\\n\")\n elements=temp[0]\n array[i].name=temp[1]\n array[i].numberArray=Array.new(elements)\n for j in 0..(elements-1) \n array[i].numberArray[j]=Number.new \n array[i].numberArray[j].mas=scanf(\"%c%c-%c%c-%c%c\\n\") \n end \n \nend\n\narray.each do |i|\n i.countElements()\nend\nmaxPizza=(array.map{|x|x.pizzaElements}).max\nmaxTaxi=(array.map{|x|x.taxiElements}).max\nmaxGirl=(array.map{|x|x.girlElements}).max\noutTaxi=\"If you want to call a taxi, you should call: \"\noutPizza=\"If you want to order a pizza, you should call: \"\noutGirl=\"If you want to go to a cafe with a wonderful girl, you should call: \"\nwasTaxi=false\nwasPizza=false\nwasGirl=false\narray.each do |i|\n if i.taxiElements==maxTaxi\n if wasTaxi\n outTaxi+=\", \"\n end\n wasTaxi=true\n outTaxi+=i.name\n end\n if i.pizzaElements==maxPizza\n if wasPizza\n outPizza+=\", \"\n end\n wasPizza=true\n outPizza+=i.name\n end\n if i.girlElements==maxGirl\n if wasGirl\n outGirl+=\", \"\n end\n wasGirl=true\n outGirl+=i.name\n end\nend\noutTaxi+=\".\"\noutPizza+=\".\"\noutGirl+=\".\"\nputs outTaxi\nputs outPizza\nputs outGirl\n"}, {"source_code": "n = gets.to_i\nCON = [:taxi,:pizza,:girl]\nfriends = []\nnames = []\nn.times do\n\tm,name = gets.chomp.split(\" \")\n\tnames << name\n\tm = m.to_i\n\tnos = Hash.new(0)\n\tm.times do\n\t\tans = nil\n\t\tx,y,z = gets.chomp.split(\"-\")\n\t\ta,b,c = x.to_i,y.to_i,z.to_i\n\t\tarr = [x.split(''),y.split(''),z.split('')].flatten\n\n\t\t# puts \"#{one} #{two} #{three} #{four} #{five} #{six}\"\n\t\t# puts \"#{b} #{c}\"\n\t\t# puts \"#{(one.to_i > two.to_i) && (three.to_i > four.to_i) && (five.to_i > six.to_i)}\"\n\t\tif a == b && b == c && arr.each_cons(2).all?{|l,m| l == m}\n\t\t\tans = CON[0]\n\t\telsif a > b && b > c && arr.each_cons(2).all?{|l,m| l > m}\n\t\t\tans = CON[1]\n\t\telse\n\t\t\tans = CON[2]\n\t\tend\n\t\tnos[ans] += 1\n\tend\n\tfriends << nos\nend\ntaxi = []\npizza = []\ngirl = []\n# puts \"#{friends}\"\nfriends.each_with_index do |f,i|\n\ttaxi[i] = f[CON[0]]\n\tpizza[i] = f[CON[1]]\n\tgirl[i] = f[CON[2]]\nend\n\nlist = []\nmax = taxi.max\ntaxi.each_with_index do |t,i|\n\tlist << names[i] if t == max\nend\n\nputs \"If you want to call a taxi, you should call: #{list.join(', ')}.\"\n\n\nlist = []\nmax = pizza.max\npizza.each_with_index do |t,i|\n\tlist << names[i] if t == max\nend\n\nputs \"If you want to order a pizza, you should call: #{list.join(', ')}.\"\n\nlist = []\nmax = girl.max\ngirl.each_with_index do |t,i|\n\tlist << names[i] if t == max\nend\n\nputs \"If you want to go to a cafe with a wonderful girl, you should call: #{list.join(', ')}.\"\n\n\n\n# "}, {"source_code": "n = gets.to_i\nCON = [:taxi,:pizza,:girl]\nfriends = []\nnames = []\nn.times do\n\tm,name = gets.chomp.split(\" \")\n\tnames << name\n\tm = m.to_i\n\tnos = Hash.new(0)\n\tm.times do\n\t\tans = nil\n\t\tx,y,z = gets.chomp.split(\"-\")\n\t\ta,b,c = x.to_i,y.to_i,z.to_i\n\t\tarr = [x.split(''),y.split(''),z.split('')].flatten\n\n\t\t# puts \"#{one} #{two} #{three} #{four} #{five} #{six}\"\n\t\t# puts \"#{b} #{c}\"\n\t\t# puts \"#{(one.to_i > two.to_i) && (three.to_i > four.to_i) && (five.to_i > six.to_i)}\"\n\t\tif a == b && b == c && arr.each_cons(2).all?{|l,m| l == m}\n\t\t\tans = CON[0]\n\t\telsif a > b && b > c && arr.each_cons(2).all?{|l,m| l > m}\n\t\t\tans = CON[1]\n\t\telse\n\t\t\tans = CON[2]\n\t\tend\n\t\tnos[ans] += 1\n\tend\n\tfriends << nos\nend\ntaxi = []\npizza = []\ngirl = []\n# puts \"#{friends}\"\nfriends.each_with_index do |f,i|\n\ttaxi[i] = f[CON[0]]\n\tpizza[i] = f[CON[1]]\n\tgirl[i] = f[CON[2]]\nend\n\nlist = []\nmax = taxi.max\ntaxi.each_with_index do |t,i|\n\tlist << names[i] if t == max\nend\n\nputs \"If you want to call a taxi, you should call: #{list.join(', ')}.\"\n\n\nlist = []\nmax = pizza.max\npizza.each_with_index do |t,i|\n\tlist << names[i] if t == max\nend\n\nputs \"If you want to order a pizza, you should call: #{list.join(', ')}.\"\n\nlist = []\nmax = girl.max\ngirl.each_with_index do |t,i|\n\tlist << names[i] if t == max\nend\n\nputs \"If you want to go to a cafe with a wonderful girl, you should call: #{list.join(', ')}.\""}, {"source_code": "def is_taxi(number)\n prev_char = ''\n taxi = true\n number.each_char do |char|\n taxi = false if (prev_char != '' && char != prev_char)\n prev_char = char\n end\n taxi\nend\n\ndef is_pizza(number)\n prev_char = ''\n pizza = true\n number.each_char do |char|\n pizza = false if (prev_char != '' && char.to_i >= prev_char)\n prev_char = char.to_i\n end\n pizza\nend\n\nfriends_count = gets.to_i\n\nnames = []\npizza_n = []\ntaxi_n = []\ngirl_n = []\n\npizza_max = 0\ntaxi_max = 0\ngirl_max = 0\n\n\nfriends_count.times do\n numbers_count, name = gets.split(' ')\n names << name\n taxi_count = 0\n pizza_count = 0\n numbers_count.to_i.times do\n number = gets.gsub('-', '').chomp\n prev_char = ''\n taxi_count += 1 if is_taxi number\n pizza_count += 1 if is_pizza number\n end\n girl_count = numbers_count.to_i - taxi_count - pizza_count\n \n girl_n << name if girl_count == girl_max\n girl_n = [name] if girl_count > girl_max\n girl_max = girl_count if girl_count >= girl_max\n \n taxi_n << name if taxi_count == taxi_max\n taxi_n = [name] if taxi_count > taxi_max\n taxi_max = taxi_count if taxi_count >= taxi_max\n \n pizza_n << name if pizza_count == pizza_max\n pizza_n = [name] if pizza_count > pizza_max\n pizza_max = pizza_count if pizza_count >= pizza_max\nend\n\nputs \"If you want to call a taxi, you should call: \" + taxi_n.join(', ') + \".\"\nputs \"If you want to order a pizza, you should call: \" + pizza_n.join(', ') + \".\"\nputs \"If you want to go to a cafe with a wonderful girl, you should call: \" + girl_n.join(', ') + \".\""}, {"source_code": "class Array\n def uniq?\n self.uniq.size == 1\n end\n\n def desc?\n (1...self.size).each { |i|\n return false if self[i] >= self[i - 1]\n }\n true\n end\nend\n\npeople = {}\n\n(0...gets.to_i).each {\n numbers_count, name = gets.chomp.split\n people[name] = Array.new(3, 0)\n (0...numbers_count.to_i).each {\n phone_digits = gets.chomp.gsub(/-/, '').split('').map(&:to_i)\n people[name][phone_digits.uniq? ? 0 : phone_digits.desc? ? 1 : 2] += 1\n }\n}\n\n['call a taxi', 'order a pizza', 'go to a cafe with a wonderful girl'].each_with_index { |action, index|\n max = people.values.map { |a| a[index] }.max\n printf \"If you want to %s, you should call: %s.\\n\", action, people.select { |_, a| a[index] == max }.keys.join(', ')\n}"}, {"source_code": "t = STDIN.gets.to_i\n\nar = []\n\ndef taxi?(phone)\n !!(phone =~ /(.)\\1-\\1\\1-\\1\\1/)\nend\n\ndef pizza?(phone)\n phone.scan(/\\d/).map(&:to_i).each_cons(2) {|a,b| return false if a <= b }\n return true\nend\n\nmaxt, maxp, maxg = 0, 0, 0\n\nt.times do\n n, name = STDIN.gets.chomp.split\n ntaxi, npizza, ngirl = 0, 0, 0\n\n n.to_i.times do\n\n phone = STDIN.gets.chomp\n\n if taxi?(phone)\n ntaxi += 1\n elsif pizza?(phone)\n npizza += 1\n else\n ngirl += 1\n end\n end\n\n ar << [name, ntaxi, npizza, ngirl]\n maxt = ntaxi if maxt < ntaxi\n maxp = npizza if maxp < npizza\n maxg = ngirl if maxg < ngirl\n\nend\n\nmsg1 = \"If you want to call a taxi, you should call: \"\nmsg2 = \"If you want to order a pizza, you should call: \"\nmsg3 = \"If you want to go to a cafe with a wonderful girl, you should call: \"\n\nta = []\npa = []\nga = []\n\nar.each do |el|\n ta << el[0] if maxt == el[1]\n pa << el[0] if maxp == el[2]\n ga << el[0] if maxg == el[3]\nend\n\nputs msg1 << ta.join(\", \") << \".\"\nputs msg2 << pa.join(\", \") << \".\"\nputs msg3 << ga.join(\", \") << \".\"\n\n\n"}, {"source_code": "t = STDIN.gets.to_i\n\nar = []\n\ndef taxi?(phone)\n !!(phone =~ /(.)\\1-\\1\\1-\\1\\1/)\nend\n\ndef pizza?(phone)\n phone.scan(/\\d/).map(&:to_i).each_cons(2) {|a,b| return false if a <= b }\n return true\nend\n\nmaxt, maxp, maxg = 0, 0, 0\n\nt.times do\n n, name = STDIN.gets.chomp.split\n ntaxi, npizza, ngirl = 0, 0, 0\n\n n.to_i.times do\n\n phone = STDIN.gets.chomp\n\n if taxi?(phone)\n ntaxi += 1\n elsif pizza?(phone)\n npizza += 1\n else\n ngirl += 1\n end\n end\n\n ar << [name, ntaxi, npizza, ngirl]\n maxt = ntaxi if maxt < ntaxi\n maxp = npizza if maxp < npizza\n maxg = ngirl if maxg < ngirl\n\nend\n\nmsg1 = \"If you want to call a taxi, you should call: \"\nmsg2 = \"If you want to order a pizza, you should call: \"\nmsg3 = \"If you want to go to a cafe with a wonderful girl, you should call: \"\n\nta = []\npa = []\nga = []\n\nar.each do |el|\n ta << el[0] if maxt == el[1]\n pa << el[0] if maxp == el[2]\n ga << el[0] if maxg == el[3]\nend\n\nputs msg1 << ta.join(\", \") << \".\"\nputs msg2 << pa.join(\", \") << \".\"\nputs msg3 << ga.join(\", \") << \".\"\n\n\n"}, {"source_code": "person = []\ngets.to_i.times{ |i|\n a, b = gets.split\n person[i] = [b, 0, 0, 0]\n a.to_i.times{\n d = gets.gsub(/[^\\d]/, \"\").split(\"\").map(&:to_i)\n same, dec = true, true\n d.each_with_index{ |x,j|\n same = false if x != d[0]\n dec = false if j > 0 && x >= d[j-1]\n }\n if same\n person[i][1] += 1\n elsif dec\n person[i][2] += 1\n else\n person[i][3] += 1\n end\n }\n}\n\nmx = (1..3).map{ |i| person.max_by{ |e| e[i] }[i] }\n[\"call a taxi\" ,\"order a pizza\", \"go to a cafe with a wonderful girl\"].each_with_index{ |s,i|\n puts \"If you want to \" + s + \", you should call: \" +\n person.select{ |e| e[i+1] == mx[i] }.map{ |e| e[0] }*\", \" + \".\"\n}\n"}, {"source_code": "def which_phone_no(numbers)\n\treturn :taxi if numbers.uniq.size==1\n\treturn :pizza if numbers.each_cons(2).all?{|a,b|a>b}\n\treturn :girl\nend\ncall={}\nk=[:taxi,:pizza,:girl]\nk.each{|_k|call[_k]=[0,[]]}\ngets.to_i.times{\n\ts,name=gets.split\n\tbooks=s.to_i.times.map{which_phone_no(gets.scan(/[0-9]/).map(&:to_i))}\n\tk.each{|_k|\n\t\tm=books.count(_k)\n\t\tif m>call[_k][0]\n\t\t\tcall[_k][0]=m\n\t\t\tcall[_k][1]=[name]\n\t\telsif call[_k][0]==m\n\t\t\tcall[_k][1]< Hash.new(0), :pizza => Hash.new(0), :girl => Hash.new(0)}\nsum = {:taxi => [], :pizza => [], :girl => []}\nnames = []\n\nclass String\n\tdef which?\n\t\tn = self.gsub('-', '')\n\t\tn = n.split('').map &:to_i\n\t\tif n.count == n.count(n[0])\n\t\t\t'taxi'\n\t\telsif n == n.uniq.sort.reverse\n\t\t\t'pizza'\n\t\telse\n\t\t\t'girl'\n\t\tend\n\tend\nend\n\ngets; while g = gets\n\tg = g.chop.split\n\tn = g[0].to_i\n\tnames.push g[1].to_sym\n\tn.times do\n\t\tnum = gets.chop!\n\t\tres[num.which?.to_sym][g[1].to_sym] += 1\n\tend\nend\n\nres.each do |key, hash|\n\tmax = hash.values.max\n\thash.each { |k, v| sum[key].push(k) if v == max }\nend\n\n{\n\t:taxi => 'call a',\n\t:pizza => 'order a',\n\t:girl => 'go to a cafe with a wonderful'\n}.each do |k, v|\n\tprint \"If you want to #{v} #{k}, you should call: \"\n\tprint sum[k].empty? ? names.join(', ') : sum[k].join(', ')\n\tputs \".\"\nend"}], "negative_code": [{"source_code": "#! /usr/bin/env ruby\n\nt = gets.chomp.to_i\nd = {}\nname_list = []\n(1..t).each do |x|\n n, name = gets.chomp.split\n n = n.to_i\n d[name] = {}\n name_list << name\n (1..n).each do |y|\n num = gets.chomp.split(\"-\")\n if num.uniq.size == 1 and num.uniq.first.split(//).uniq.size == 1\n d[name]['taxi'] = (d[name]['taxi'] || 0) + 1\n elsif num.join.split(//).map(&:to_i) == num.join.split(//).map(&:to_i).sort.reverse.uniq\n d[name]['pizza'] = (d[name]['pizza'] || 0) + 1\n else\n d[name]['girl'] = (d[name]['girl'] || 0) + 1\n end\n end\nend\nmax_taxi = d.keys.map { |i| [d[i]['taxi'] || 0, i] }.sort.max.first\nmax_pizza = d.keys.map { |i| [d[i]['pizza'] || 0, i] }.sort.max.first\nmax_girl = d.keys.map { |i| [d[i]['girl'] || 0, i] }.sort.max.first\ntaxi = name_list.select {|i| d[i]['taxi'] == max_taxi }.join(\", \") + \".\"\npizza = name_list.select {|i| d[i]['pizza'] == max_pizza }.join(\", \") + \".\"\ngirl = name_list.select {|i| d[i]['girl'] == max_girl }.join(\", \") + \".\"\nprint (\"If you want to call a taxi, you should call: \" + taxi +\"\\n\").to_s\nprint (\"If you want to order a pizza, you should call: \" + pizza +\"\\n\").to_s\nprint (\"If you want to go to a cafe with a wonderful girl, you should call: \" + girl +\"\\n\").to_s\n"}, {"source_code": "#! /usr/bin/env ruby\n\ndef type_of_phone_number(number)\n return :taxi if number.uniq.size.equal? 1\n return :pizza if number.each_cons(2).all? {|x, y| x > y}\n return :girl\nend\n\nd = {}\nk = [:taxi, :pizza, :girl]\nk.each {|x| d[x] = [0, []]}\n\ngets.to_i.times do |x|\n n, name = gets.chomp.split\n numbers = n.to_i.times.map {type_of_phone_number(gets.scan(/[0-9]/).map(&:to_i))}\n k.each do |x|\n d[x] = [numbers.count(x), [name]] if numbers.count(x) > d[x].first\n d[x].last << name if numbers.count(x) > d[x].first\n end\nend\n\nputs \"If you want to call a taxi, you should call: %s.\" %(d[:taxi][1] * \", \")\nputs \"If you want to order a pizza, you should call: %s.\" %(d[:pizza][1] * \", \")\nputs \"If you want to go to a cafe with a wonderful girl, you should call: %s.\" %(d[:girl][1] * \", \")\n"}, {"source_code": "#! /usr/bin/env ruby\n\ndef type_of_phone_number(number)\n return :taxi if number.uniq.size.equal? 1\n return :pizza if number.each_cons(2).all? {|x, y| x > y}\n return :girl\nend\n\nd = {}\nk = [:taxi, :pizza, :girl]\nk.each {|x| d[x] = [0, []]}\n\ngets.to_i.times do |x|\n n, name = gets.chomp.split\n numbers = n.to_i.times.map {type_of_phone_number(gets.scan(/[0-9]/).map(&:to_i))}\n k.each do |x|\n d[x] = [numbers.count(x), [name]] if numbers.count(x) > d[x].first\n d[x].last << name if numbers.count(x) > d[x].first\n end\nend\n\np \"If you want to call a taxi, you should call: %s.\" %(d[:taxi][1] * \", \")\np \"If you want to order a pizza, you should call: %s.\" %(d[:pizza][1] * \", \")\np \"If you want to go to a cafe with a wonderful girl, you should call: %s.\" %(d[:girl][1] * \", \")\n"}, {"source_code": "#! /usr/bin/env ruby\n\nt = gets.chomp.to_i\nd = {}\nname_list = []\n(1..t).each do |x|\n n, name = gets.chomp.split\n n = n.to_i\n d[name] = {}\n name_list << name\n (1..n).each do |y|\n num = gets.chomp.split(\"-\")\n if num.uniq.size == 1 and num.uniq.first.split(//).uniq.size == 1\n d[name]['taxi'] = (d[name]['taxi'] || 0) + 1\n elsif num.join.split(//).map(&:to_i) == num.join.split(//).map(&:to_i).sort.reverse.uniq\n d[name]['pizza'] = (d[name]['pizza'] || 0) + 1\n else\n d[name]['girl'] = (d[name]['girl'] || 0) + 1\n end\n end\nend\nmax_taxi = d.keys.map { |i| [d[i]['taxi'] || 0, i] }.sort.max.first\nmax_pizza = d.keys.map { |i| [d[i]['pizza'] || 0, i] }.sort.max.first\nmax_girl = d.keys.map { |i| [d[i]['girl'] || 0, i] }.sort.max.first\ntaxi = name_list.select {|i| d[i]['taxi'] == max_taxi }.join(\", \") + \".\"\npizza = name_list.select {|i| d[i]['pizza'] == max_pizza }.join(\", \") + \".\"\ngirl = name_list.select {|i| d[i]['girl'] == max_girl }.join(\", \") + \".\"\np (\"If you want to call a taxi, you should call: \" + taxi).to_s.strip\np \"If you want to order a pizza, you should call: \" + pizza\np \"If you want to go to a cafe with a wonderful girl, you should call: \" + girl\n"}, {"source_code": "require 'set'\n\ndef is_taxi(s)\n tmp = s.split \"-\"\n set = Set.new tmp\n if set.length == 1\n true\n else\n false\n end\nend\n\ndef is_pizza(s)\n flag = true\n tmp = nil\n s.split(\"\").each do |c|\n if c != '-'\n if tmp == nil\n tmp = c.to_i\n else\n if tmp <= c.to_i\n flag = false\n break\n else\n tmp = c.to_i\n end\n end\n end\n end\n flag\nend\n\ndef max_names(hash)\n max = hash.values.max\n names = hash.select{ |k, v|\n v == max\n }\n names.keys.join ', '\nend\n\n\nn = gets.chomp.to_i\n\ntaxi = {}\npizza = {}\ngirl = {}\n\nn.times do\n tmp = gets.chomp.split \" \"\n s = tmp[0].to_i\n name = tmp[1]\n \n taxi[name] = 0\n pizza[name] = 0\n girl[name] = 0\n \n s.times do\n tmp2 = gets.chomp\n if is_taxi(tmp2)\n taxi[name] += 1\n elsif is_pizza(tmp2)\n pizza[name] += 1\n else\n girl[name] += 1\n end\n end\nend\n\nputs \"If you want to call a taxi, you should call: \" + max_names(taxi) + \".\"\nputs \"If you want to order a pizza, you should call: \" + max_names(pizza) + \".\"\nputs \"If you want to go to a cafe with a wonderful girl, you should call: \" + max_names(girl) + \".\"\n\n\n"}, {"source_code": "def pizza?(num)\n num = num.gsub(/-/,'').chars.map(&:to_i)\n prev = 10\n num.each do |n|\n if prev > n\n prev = n\n else\n return false\n end\n end\n true\nend\n\ndef taxi?(num)\n a = num[0]\n num.gsub(/[#{a}\\-]/,'').size.zero?\nend\n\ndef girl?(num)\n !taxi?(num) && !pizza?(num)\nend\n\ndef which(num)\n return :taxi if taxi?(num)\n return :pizza if pizza?(num)\n return :girl\nend\n\ndics = {}\nn = STDIN.gets.chomp.to_i\nn.times do\n i, name = STDIN.gets.chomp.split(/ /)\n i = i.to_i\n dics[name] = {taxi: 0, pizza: 0, girl: 0}\n already = []\n\n i.times do\n num = STDIN.gets.chomp\n next if already.include?(num)\n already << num\n dics[name][which(num)] += 1\n end\nend\n\n\n{taxi: \"If you want to call a taxi, you should call: \",\n pizza: \"If you want to order a pizza, you should call: \",\n girl: \"If you want to go to a cafe with a wonderful girl, you should call: \"}.each do |type, head|\n max = dics.map{|k,v| v[type] }.max\n puts head+dics.select{|k,v| v[type] == max }.keys.join(\", \")+\".\"\nend\n"}, {"source_code": "def pizza?(num)\n num = num.gsub(/-/,'').chars.map(&:to_i)\n prev = 10\n num.each do |n|\n if prev > n\n prev = n\n else\n return false\n end\n end\n true\nend\n\ndef taxi?(num)\n a = num[0]\n num.gsub(/[#{a}\\-]/,'').size.zero?\nend\n\ndef girl?(num)\n !taxi?(num) && !pizza?(num)\nend\n\ndef which(num)\n return :taxi if taxi?(num)\n return :pizza if pizza?(num)\n return :girl\nend\n\ndics = {}\nn = STDIN.gets.chomp.to_i\nn.times do\n i, name = STDIN.gets.chomp.split(/ /)\n i = i.to_i\n dics[name] = {taxi: 0, pizza: 0, girl: 0}\n already = []\n\n i.times do\n num = STDIN.gets.chomp\n next if already.include?(num)\n already << num\n dics[name][which(num)] += 1\n end\nend\n\n\n{taxi: \"If you want to call a taxi, you should call: \",\n pizza: \"If you want to order a pizza, you should call: \",\n girl: \"If you want to go to a cafe with a wonderful girl, you should call: \"}.each do |type, head|\n max = dics.max{|(k,v)| v[type] }[1][type]\n puts head+dics.select{|k,v| v[type] == max }.keys.join(\", \")+\".\"\nend\n"}, {"source_code": "def pizza?(num)\n num = num.gsub(/-/,'').chars.map(&:to_i)\n prev = 10\n num.each do |n|\n if prev > n\n prev = n\n else\n return false\n end\n end\n true\nend\n\ndef taxi?(num)\n a = num[0]\n num.gsub(/[#{a}\\-]/,'').size.zero?\nend\n\ndef girl?(num)\n !taxi?(num) && !pizza?(num)\nend\n\ndef which(num)\n return :taxi if taxi?(num)\n return :pizza if pizza?(num)\n return :girl\nend\n\ndics = {}\nn = STDIN.gets.chomp.to_i\nn.times do\n i, name = STDIN.gets.chomp.split(/ /)\n i = i.to_i\n dics[name] = {taxi: 0, pizza: 0, girl: 0}\n already = []\n\n i.times do\n num = STDIN.gets.chomp\n next if already.include?(num)\n already << num\n dics[name][which(num)] += 1\n end\nend\n\n\n{taxi: \"If you want to call a taxi, you should call: \",\n pizza: \"If you want to order a pizza, you should call: \",\n girl: \"If you want to go to a cafe with a wonderful girl, you should call: \"}.each do |type, head|\n max = dics.max{|(k,v)| v[type] }[1][type]\n puts head+dics.select{|k,v| v[type] == max }.keys.join(\", \")\nend\n"}, {"source_code": "n = gets.to_i\npb = []\nn.times do |i|\n a = [i]\n c,nm = gets.split\n a << nm\n h = Hash.new(0)\n c.to_i.times do\n pn = gets.strip.gsub(/-/,\"\").split(//)\n pn.uniq.sort.reverse\n if pn.uniq.size == 1\n h[:t] += 1\n elsif pn.uniq.sort.reverse == pn\n h[:p] += 1\n else\n h[:g] += 1\n end\n end\n a << h\n pb << a\nend\n\nmx = pb.map{|a| a[2][:t]}.max\ntx = pb.select{|a| a[2][:t] == mx }\n\n\nmx = pb.map{|a| a[2][:p]}.max\npz = pb.select{|a| a[2][:p] == mx }\n\nmx = pb.map{|a| a[2][:g]}.max\ngf = pb.select{|a| a[2][:g] == mx }\n\nputs \"If want to order a pizza, you should call: #{tx.map{|a| a[1]}.join(\", \")}.\"\nputs \"If you want to call a taxi, you should call: #{pz.map{|a| a[1]}.join(\", \")}.\"\nputs \"If you want to go to a cafe with a wonderful girl, you should call: #{gf.map{|a| a[1]}.join(\", \")}.\"\n"}, {"source_code": "#!/usr/bin/ruby\nclass Number\n def initialize\n @mas=Array.new(6)\n end\n def mas\n @mas\n end\n def mas=(mas)\n @mas=mas\n end\n def isPizza()\n return @mas==@mas.uniq() && @mas==@mas.sort{|a,b| b<=>a}\n end\n def isTaxi()\n return @mas.uniq().size()==1\n end\nend\nclass Person\n def initialize\n @name=\"\"\n @taxiElements=0\n @girlElements=0\n @pizzaElements=0\n @numberArray=[]\n end\n def taxiElements\n @taxiElements\n end\n def pizzaElements\n @pizzaElements\n end\n def girlElements\n @girlElements\n end\n def name\n @name\n end\n def name=(name)\n @name=name\n end\n def numberArray\n @numberArray\n end\n def numberArray=(numberArray)\n @numberArray=numberArray\n end\n def countElements()\n for i in 0..(@numberArray.size()-1)\n element=@numberArray[i]\n if element.isTaxi()\n\t@taxiElements=@taxiElements+1\n elsif (element.isPizza())\n\t@pizzaElements=@pizzaElements+1\n else\n\t@girlElements=@girlElements+1\n end\n end\n end\nend\n\nrequire 'scanf'\ncount=(scanf(\"%d\"))[0]\narray=Array.new(count)\nfor i in 0..(count-1)\n array[i]=Person.new\n temp=scanf(\"%d %s\")\n elements=temp[0]\n array[i].name=temp[1]\n array[i].numberArray=Array.new(elements)\n for j in 0..(elements-1) \n array[i].numberArray[j]=Number.new \n array[i].numberArray[j].mas=scanf(\"%c%c-%c%c-%c%c\") \n end \n \nend\n\narray.each do |i|\n i.countElements()\nend\nmaxPizza=(array.map{|x|x.pizzaElements}).max\nmaxTaxi=(array.map{|x|x.taxiElements}).max\nmaxGirl=(array.map{|x|x.girlElements}).max\noutTaxi=\"If you want to call a taxi, you should call: \"\noutPizza=\"If you want to order a pizza, you should call: \"\noutGirl=\"If you want to go to a cafe with a wonderful girl, you should call: \"\nwasTaxi=false\nwasPizza=false\nwasGirl=false\narray.each do |i|\n if i.taxiElements==maxTaxi\n if wasTaxi\n outTaxi+=\", \"\n end\n wasTaxi=true\n outTaxi+=i.name\n end\n if i.pizzaElements==maxPizza\n if wasPizza\n outPizza+=\", \"\n end\n wasPizza=true\n outPizza+=i.name\n end\n if i.girlElements==maxGirl\n if wasGirl\n outGirl+=\", \"\n end\n wasGirl=true\n outGirl+=i.name\n end\nend\noutTaxi+=\".\"\noutPizza+=\".\"\noutGirl+=\".\"\nif wasTaxi\n puts outTaxi\nend\nif wasPizza\n puts outPizza\nend\nif wasGirl\n puts outGirl\nend\n"}, {"source_code": "#!/usr/bin/ruby\nclass Number\n def initialize\n @mas=Array.new(6)\n end\n def mas\n @mas\n end\n def mas=(mas)\n @mas=mas\n end\n def isPizza()\n return @mas==@mas.uniq() && @mas==@mas.sort{|a,b| b<=>a}\n end\n def isTaxi()\n return @mas.uniq().size()==1\n end\nend\nclass Person\n def initialize\n @name=String.new\n @taxiElements=0\n @girlElements=0\n @pizzaElements=0\n @numberArray=[]\n end\n def taxiElements\n @taxiElements\n end\n def pizzaElements\n @pizzaElements\n end\n def girlElements\n @girlElements\n end\n def name\n @name\n end\n def name=(name)\n @name=name\n end\n def numberArray\n @numberArray\n end\n def numberArray=(numberArray)\n @numberArray=numberArray\n end\n def countElements()\n for i in 0..(@numberArray.size()-1)\n element=@numberArray[i]\n if element.isTaxi()\n @taxiElements=@taxiElements+1\n elsif (element.isPizza())\n @pizzaElements=@pizzaElements+1\n else\n @girlElements=@girlElements+1\n end\n end\n end\nend\n\nrequire 'scanf'\ncount=(scanf(\"%d\\n\"))[0]\narray=Array.new(count)\nfor i in 0..(count-1)\n array[i]=Person.new\n temp=scanf(\"%d %s\\n\")\n puts temp\n elements=temp[0]\n array[i].name=temp[1]\n array[i].numberArray=Array.new(elements)\n for j in 0..(elements-1) \n array[i].numberArray[j]=Number.new \n array[i].numberArray[j].mas=scanf(\"%c%c-%c%c-%c%c\\n\") \n end \n \nend\n\narray.each do |i|\n i.countElements()\nend\nmaxPizza=(array.map{|x|x.pizzaElements}).max\nmaxTaxi=(array.map{|x|x.taxiElements}).max\nmaxGirl=(array.map{|x|x.girlElements}).max\noutTaxi=\"If you want to call a taxi, you should call: \"\noutPizza=\"If you want to order a pizza, you should call: \"\noutGirl=\"If you want to go to a cafe with a wonderful girl, you should call: \"\nwasTaxi=false\nwasPizza=false\nwasGirl=false\narray.each do |i|\n if i.taxiElements==maxTaxi\n if wasTaxi\n outTaxi+=\", \"\n end\n wasTaxi=true\n outTaxi+=i.name\n end\n if i.pizzaElements==maxPizza\n if wasPizza\n outPizza+=\", \"\n end\n wasPizza=true\n outPizza+=i.name\n end\n if i.girlElements==maxGirl\n if wasGirl\n outGirl+=\", \"\n end\n wasGirl=true\n outGirl+=i.name\n end\nend\noutTaxi+=\".\"\noutPizza+=\".\"\noutGirl+=\".\"\nputs outTaxi\nputs outPizza\nputs outGirl\n"}, {"source_code": "#!/usr/bin/ruby\nclass Number\n def initialize\n @mas=Array.new(6)\n end\n def mas\n @mas\n end\n def mas=(mas)\n @mas=mas\n end\n def isPizza()\n return @mas==@mas.uniq() && @mas==@mas.sort{|a,b| b<=>a}\n end\n def isTaxi()\n return @mas.uniq().size()==1\n end\nend\nclass Person\n def initialize\n @name=\"\"\n @taxiElements=0\n @girlElements=0\n @pizzaElements=0\n @numberArray=[]\n end\n def taxiElements\n @taxiElements\n end\n def pizzaElements\n @pizzaElements\n end\n def girlElements\n @girlElements\n end\n def name\n @name\n end\n def name=(name)\n @name=name\n end\n def numberArray\n @numberArray\n end\n def numberArray=(numberArray)\n @numberArray=numberArray\n end\n def countElements()\n for i in 0..(@numberArray.size()-1)\n element=@numberArray[i]\n if element.isTaxi()\n\t@taxiElements=@taxiElements+1\n elsif (element.isPizza())\n\t@pizzaElements=@pizzaElements+1\n else\n\t@girlElements=@girlElements+1\n end\n end\n end\nend\n\nrequire 'scanf'\ncount=(scanf(\"%d\"))[0]\narray=Array.new(count)\nfor i in 0..(count-1)\n array[i]=Person.new\n temp=scanf(\"%d %s\")\n elements=temp[0]\n array[i].name=temp[1]\n array[i].numberArray=Array.new(elements)\n for j in 0..(elements-1) \n array[i].numberArray[j]=Number.new \n array[i].numberArray[j].mas=scanf(\"%c%c-%c%c-%c%c\") \n end \n \nend\n\narray.each do |i|\n i.countElements()\nend\nmaxPizza=(array.map{|x|x.pizzaElements}).max\nmaxTaxi=(array.map{|x|x.taxiElements}).max\nmaxGirl=(array.map{|x|x.girlElements}).max\noutTaxi=\"If you want to call a taxi, you should call: \"\noutPizza=\"If you want to order a pizza, you should call: \"\noutGirl=\"If you want to go to a cafe with a wonderful girl, you should call: \"\nwasTaxi=false\nwasPizza=false\nwasGirl=false\narray.each do |i|\n if i.taxiElements==maxTaxi\n if wasTaxi\n outTaxi+=\", \"\n end\n wasTaxi=true\n outTaxi+=i.name\n end\n if i.pizzaElements==maxPizza\n if wasPizza\n outPizza+=\", \"\n end\n wasPizza=true\n outPizza+=i.name\n end\n if i.girlElements==maxGirl\n if wasGirl\n outGirl+=\", \"\n end\n wasGirl=true\n outGirl+=i.name\n end\nend\noutTaxi+=\".\"\noutPizza+=\".\"\noutGirl+=\".\"\nputs outTaxi\nputs outPizza\nputs outGirl\n"}, {"source_code": "n = gets.to_i\nCON = [:taxi,:pizza,:girl]\nfriends = []\nnames = []\nn.times do\n\tm,name = gets.chomp.split(\" \")\n\tnames << name\n\tm = m.to_i\n\tnos = Hash.new(0)\n\tm.times do\n\t\tans = nil\n\t\tx,y,z = gets.chomp.split(\"-\")\n\t\ta,b,c = x.to_i,y.to_i,z.to_i\n\t\tarr = [x.split(''),y.split(''),z.split('')].flatten\n\n\t\t# puts \"#{one} #{two} #{three} #{four} #{five} #{six}\"\n\t\t# puts \"#{b} #{c}\"\n\t\t# puts \"#{(one.to_i > two.to_i) && (three.to_i > four.to_i) && (five.to_i > six.to_i)}\"\n\t\tif a == b && b == c && arr.each_cons(2).all?{|l,m| l == m}\n\t\t\tans = CON[0]\n\t\telsif a > b && b > c && arr.each_cons(2).all?{|l,m| l > m}\n\t\t\tans = CON[1]\n\t\telse\n\t\t\tans = CON[2]\n\t\tend\n\t\tnos[ans] += 1\n\tend\n\tfriends << nos\nend\ntaxi = []\npizza = []\ngirl = []\nputs \"#{friends}\"\nfriends.each_with_index do |f,i|\n\ttaxi[i] = f[CON[0]]\n\tpizza[i] = f[CON[1]]\n\tgirl[i] = f[CON[2]]\nend\n\nlist = []\nmax = taxi.max\ntaxi.each_with_index do |t,i|\n\tlist << names[i] if t == max\nend\n\nputs \"If you want to call a taxi, you should call: #{list.join(', ')}.\"\n\n\nlist = []\nmax = pizza.max\npizza.each_with_index do |t,i|\n\tlist << names[i] if t == max\nend\n\nputs \"If you want to order a pizza, you should call: #{list.join(', ')}.\"\n\nlist = []\nmax = girl.max\ngirl.each_with_index do |t,i|\n\tlist << names[i] if t == max\nend\n\nputs \"If you want to go to a cafe with a wonderful girl, you should call: #{list.join(', ')}.\""}, {"source_code": "n = gets.to_i\nCON = [:taxi,:pizza,:girl]\nfriends = []\nnames = []\nn.times do\n\tm,name = gets.chomp.split(\" \")\n\tnames << name\n\tm = m.to_i\n\tnos = Hash.new(0)\n\tm.times do\n\t\tans = nil\n\t\tx,y,z = gets.chomp.split(\"-\")\n\t\ta,b,c = x.to_i,y.to_i,z.to_i\n\t\tone,two = x.split('')\n\t\tthree,four = y.split('')\n\t\tfive,six = z.split('')\n\t\t# puts \"#{one} #{two} #{three} #{four} #{five} #{six}\"\n\t\t# puts \"#{b} #{c}\"\n\t\t# puts \"#{(one.to_i > two.to_i) && (three.to_i > four.to_i) && (five.to_i > six.to_i)}\"\n\t\tif a == b && b == c\n\t\t\tans = CON[0]\n\t\telsif a > b && b > c && (one.to_i > two.to_i) && (three.to_i > four.to_i) && (five.to_i > six.to_i)\n\t\t\tans = CON[1]\n\t\telse\n\t\t\tans = CON[2]\n\t\tend\n\t\tnos[ans] += 1\n\tend\n\tfriends << nos\nend\ntaxi = []\npizza = []\ngirl = []\n\nfriends.each_with_index do |f,i|\n\ttaxi[i] = f[CON[0]]\n\tpizza[i] = f[CON[1]]\n\tgirl[i] = f[CON[2]]\nend\n\nlist = []\nmax = taxi.max\ntaxi.each_with_index do |t,i|\n\tlist << names[i] if t == max\nend\n\nputs \"If you want to call a taxi, you should call: #{list.join(', ')}.\"\n\n\nlist = []\nmax = pizza.max\npizza.each_with_index do |t,i|\n\tlist << names[i] if t == max\nend\n\nputs \"If you want to order a pizza, you should call: #{list.join(', ')}.\"\n\nlist = []\nmax = girl.max\ngirl.each_with_index do |t,i|\n\tlist << names[i] if t == max\nend\n\nputs \"If you want to go to a cafe with a wonderful girl, you should call: #{list.join(', ')}.\""}, {"source_code": "n = gets.to_i\nCON = [:taxi,:pizza,:girl]\nfriends = []\nnames = []\nn.times do\n\tm,name = gets.chomp.split(\" \")\n\tnames << name\n\tm = m.to_i\n\tnos = Hash.new(0)\n\tm.times do\n\t\tans = nil\n\t\tx,y,z = gets.chomp.split(\"-\")\n\t\ta,b,c = x.to_i,y.to_i,z.to_i\n\t\tarr = [x.split(''),y.split(''),z.split('')].flatten\n\n\t\t# puts \"#{one} #{two} #{three} #{four} #{five} #{six}\"\n\t\t# puts \"#{b} #{c}\"\n\t\t# puts \"#{(one.to_i > two.to_i) && (three.to_i > four.to_i) && (five.to_i > six.to_i)}\"\n\t\tif a == b && b == c\n\t\t\tans = CON[0]\n\t\telsif a > b && b > c && arr.each_cons(2).all?{|l,m| l > m}\n\t\t\tans = CON[1]\n\t\telse\n\t\t\tans = CON[2]\n\t\tend\n\t\tnos[ans] += 1\n\tend\n\tfriends << nos\nend\ntaxi = []\npizza = []\ngirl = []\n# puts \"#{friends}\"\nfriends.each_with_index do |f,i|\n\ttaxi[i] = f[CON[0]]\n\tpizza[i] = f[CON[1]]\n\tgirl[i] = f[CON[2]]\nend\n\nlist = []\nmax = taxi.max\ntaxi.each_with_index do |t,i|\n\tlist << names[i] if t == max\nend\n\nputs \"If you want to call a taxi, you should call: #{list.join(', ')}.\"\n\n\nlist = []\nmax = pizza.max\npizza.each_with_index do |t,i|\n\tlist << names[i] if t == max\nend\n\nputs \"If you want to order a pizza, you should call: #{list.join(', ')}.\"\n\nlist = []\nmax = girl.max\ngirl.each_with_index do |t,i|\n\tlist << names[i] if t == max\nend\n\nputs \"If you want to go to a cafe with a wonderful girl, you should call: #{list.join(', ')}.\""}, {"source_code": "person = []\ngets.to_i.times{ |i|\n a, b = gets.split\n person[i] = [b, 0, 0, 0]\n a.to_i.times{\n d = gets.gsub(/[^\\d]/, \"\").split(\"\").map(&:to_i)\n same, dec = true, true\n d.each_with_index{ |x,j|\n same = false if x != d[0]\n dec = false if j > 0 && x >= d[j-1]\n }\n if same\n person[i][1] += 1\n elsif dec\n person[i][2] += 1\n else\n person[i][3] += 1\n end\n }\n}\n\nmx = (1..3).map{ |i| person.max_by{ |e| e[i] }[i] }\n[\"taxi\" ,\"pizza\", \"cafe with a wonderful girl\"].each_with_index{ |s,i|\n puts \"If you want to call a \" + s + \", you should call: \" +\n person.select{ |e| e[i+1] == mx[i] }.map{ |e| e[0] }*\", \" + \".\"\n}\n"}, {"source_code": "res = {:taxi => Hash.new(0), :pizza => Hash.new(0), :girl => Hash.new(0)}\nsum = {:taxi => [], :pizza => [], :girl => []}\n\nclass String\n\tdef which?\n\t\tn = self.gsub('-', '')\n\t\tn = n.split('').map &:to_i\n\t\tif n.count == n.count(n[0])\n\t\t\t'taxi'\n\t\telsif n == n.uniq.sort.reverse\n\t\t\t'pizza'\n\t\telse\n\t\t\t'girl'\n\t\tend\n\tend\nend\n\ngets; while g = gets\n\tg = g.chop.split\n\tg[0].to_i.times do |i|\n\t\tnum = gets.chop!\n\t\tres[num.which?.to_sym][g[1].to_sym] += 1\n\tend\nend\n\nres.each do |key, hash|\n\tmax = hash.values.max\n\thash.each { |k, v| sum[key].push(k) if v == max }\nend\n\n{\n\t:taxi => 'call a',\n\t:pizza => 'order a',\n\t:girl => 'go to a cafe with a wonderful'\n}.each do |k, v|\n\tprint \"If you want to #{v} #{k}, you should call: \"\n\tprint sum[k].join(', ')\n\tputs \".\"\nend\n"}], "src_uid": "4791a795119c185b3aec91b6f8af8f03"} {"nl": {"description": "A number $$$a_2$$$ is said to be the arithmetic mean of two numbers $$$a_1$$$ and $$$a_3$$$, if the following condition holds: $$$a_1 + a_3 = 2\\cdot a_2$$$. We define an arithmetic mean deviation of three numbers $$$a_1$$$, $$$a_2$$$ and $$$a_3$$$ as follows: $$$d(a_1, a_2, a_3) = |a_1 + a_3 - 2 \\cdot a_2|$$$.Arithmetic means a lot to Jeevan. He has three numbers $$$a_1$$$, $$$a_2$$$ and $$$a_3$$$ and he wants to minimize the arithmetic mean deviation $$$d(a_1, a_2, a_3)$$$. To do so, he can perform the following operation any number of times (possibly zero): Choose $$$i, j$$$ from $$$\\{1, 2, 3\\}$$$ such that $$$i \\ne j$$$ and increment $$$a_i$$$ by $$$1$$$ and decrement $$$a_j$$$ by $$$1$$$ Help Jeevan find out the minimum value of $$$d(a_1, a_2, a_3)$$$ that can be obtained after applying the operation any number of times.", "input_spec": "The first line contains a single integer $$$t$$$ $$$(1 \\le t \\le 5000)$$$  — the number of test cases. The first and only line of each test case contains three integers $$$a_1$$$, $$$a_2$$$ and $$$a_3$$$ $$$(1 \\le a_1, a_2, a_3 \\le 10^{8})$$$.", "output_spec": "For each test case, output the minimum value of $$$d(a_1, a_2, a_3)$$$ that can be obtained after applying the operation any number of times.", "sample_inputs": ["3\n3 4 5\n2 2 6\n1 6 5"], "sample_outputs": ["0\n1\n0"], "notes": "NoteNote that after applying a few operations, the values of $$$a_1$$$, $$$a_2$$$ and $$$a_3$$$ may become negative.In the first test case, $$$4$$$ is already the Arithmetic Mean of $$$3$$$ and $$$5$$$.$$$d(3, 4, 5) = |3 + 5 - 2 \\cdot 4| = 0$$$In the second test case, we can apply the following operation:$$$(2, 2, 6)$$$ $$$\\xrightarrow[\\text{increment $$$a_2$$$}]{\\text{decrement $$$a_1$$$}}$$$ $$$(1, 3, 6)$$$$$$d(1, 3, 6) = |1 + 6 - 2 \\cdot 3| = 1$$$It can be proven that answer can not be improved any further.In the third test case, we can apply the following operations:$$$(1, 6, 5)$$$ $$$\\xrightarrow[\\text{increment $$$a_3$$$}]{\\text{decrement $$$a_2$$$}}$$$ $$$(1, 5, 6)$$$ $$$\\xrightarrow[\\text{increment $$$a_1$$$}]{\\text{decrement $$$a_2$$$}}$$$ $$$(2, 4, 6)$$$$$$d(2, 4, 6) = |2 + 6 - 2 \\cdot 4| = 0$$$"}, "positive_code": [{"source_code": "gets.to_i.times do\r\n\r\na, b, c = gets.split.map!(&:to_i)\r\n\r\nputs (a + b + c) % 3 == 0 ? 0 : 1\r\n\r\nend\r\n"}], "negative_code": [{"source_code": "gets.to_i.times do\r\n\r\na, b, c = gets.split.map!(&:to_i)\r\n\r\nputs ((a + b + c) % 3).abs\r\n\r\nend\r\n"}], "src_uid": "5bffe38e3ac9511a30ee02b4ec5cb1d5"} {"nl": {"description": "You are given two strings $$$a$$$ and $$$b$$$ consisting of lowercase English letters, both of length $$$n$$$. The characters of both strings have indices from $$$1$$$ to $$$n$$$, inclusive. You are allowed to do the following changes: Choose any index $$$i$$$ ($$$1 \\le i \\le n$$$) and swap characters $$$a_i$$$ and $$$b_i$$$; Choose any index $$$i$$$ ($$$1 \\le i \\le n$$$) and swap characters $$$a_i$$$ and $$$a_{n - i + 1}$$$; Choose any index $$$i$$$ ($$$1 \\le i \\le n$$$) and swap characters $$$b_i$$$ and $$$b_{n - i + 1}$$$. Note that if $$$n$$$ is odd, you are formally allowed to swap $$$a_{\\lceil\\frac{n}{2}\\rceil}$$$ with $$$a_{\\lceil\\frac{n}{2}\\rceil}$$$ (and the same with the string $$$b$$$) but this move is useless. Also you can swap two equal characters but this operation is useless as well.You have to make these strings equal by applying any number of changes described above, in any order. But it is obvious that it may be impossible to make two strings equal by these swaps.In one preprocess move you can replace a character in $$$a$$$ with another character. In other words, in a single preprocess move you can choose any index $$$i$$$ ($$$1 \\le i \\le n$$$), any character $$$c$$$ and set $$$a_i := c$$$.Your task is to find the minimum number of preprocess moves to apply in such a way that after them you can make strings $$$a$$$ and $$$b$$$ equal by applying some number of changes described in the list above.Note that the number of changes you make after the preprocess moves does not matter. Also note that you cannot apply preprocess moves to the string $$$b$$$ or make any preprocess moves after the first change is made.", "input_spec": "The first line of the input contains one integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$) — the length of strings $$$a$$$ and $$$b$$$. The second line contains the string $$$a$$$ consisting of exactly $$$n$$$ lowercase English letters. The third line contains the string $$$b$$$ consisting of exactly $$$n$$$ lowercase English letters.", "output_spec": "Print a single integer — the minimum number of preprocess moves to apply before changes, so that it is possible to make the string $$$a$$$ equal to string $$$b$$$ with a sequence of changes from the list above.", "sample_inputs": ["7\nabacaba\nbacabaa", "5\nzcabd\ndbacz"], "sample_outputs": ["4", "0"], "notes": "NoteIn the first example preprocess moves are as follows: $$$a_1 := $$$'b', $$$a_3 := $$$'c', $$$a_4 := $$$'a' and $$$a_5:=$$$'b'. Afterwards, $$$a = $$$\"bbcabba\". Then we can obtain equal strings by the following sequence of changes: $$$swap(a_2, b_2)$$$ and $$$swap(a_2, a_6)$$$. There is no way to use fewer than $$$4$$$ preprocess moves before a sequence of changes to make string equal, so the answer in this example is $$$4$$$.In the second example no preprocess moves are required. We can use the following sequence of changes to make $$$a$$$ and $$$b$$$ equal: $$$swap(b_1, b_5)$$$, $$$swap(a_2, a_4)$$$."}, "positive_code": [{"source_code": "N = gets.to_i\nA = gets.chomp\nB = gets.chomp\n\nans = 0\n((N+1)/2).times do |i|\n hash = Hash.new(0)\n hash[A[i]] += 1\n hash[B[i]] += 1\n if i != N-i-1\n hash[A[N-i-1]] += 1\n hash[B[N-i-1]] += 1\n end\n values = hash.values.sort\n if values.all?(&:even?)\n next\n elsif B[i] != B[N-i-1] && A[i] != B[i] && A[N-i-1] != B[N-i-1] && A[i] != B[N-i-1] && A[N-i-1] != B[i]\n ans += 2\n else\n ans += 1\n end\nend\nputs ans"}, {"source_code": "n = gets.to_i\nstr1 = gets.chomp\nstr2 = gets.chomp\n\n#require 'Set'\n\ncount = 0\n\n(0..(n/2)-1).each do |i|\n set = [str1[i],str2[i],str1[n-i-1],str2[n-i-1]]\n len = set.uniq.length\n\n if len == 2\n if set.count(set[0]) != 2\n count += 1\n end\n elsif len == 3\n count += 1\n if set[0] == set[2]\n count += 1\n end\n elsif len == 4\n count += 2\n end\nend\n\nif n%2==1\n if str1[n/2]!=str2[n/2]\n count += 1\n end\nend\n\nputs count"}], "negative_code": [{"source_code": "N = gets.to_i\nA = gets.chomp\nB = gets.chomp\n\nans = 0\n((N+1)/2).times do |i|\n hash = Hash.new(0)\n hash[A[i]] += 1\n hash[B[i]] += 1\n if i != N-i-1\n hash[A[N-i-1]] += 1\n hash[B[N-i-1]] += 1\n end\n values = hash.values.sort\n if values.all?(&:even?)\n next\n elsif B[i] != B[N-i-1] && A[i] != B[i] && A[N-i-1] != B[N-i-1]\n ans += 2\n else\n ans += 1\n end\n p [i, ans]\nend\nputs ans"}, {"source_code": "N = gets.to_i\nA = gets.chomp\nB = gets.chomp\n\nans = 0\n((N+1)/2).times do |i|\n hash = Hash.new(0)\n hash[A[i]] += 1\n hash[B[i]] += 1\n if i != N-i-1\n hash[A[N-i-1]] += 1\n hash[B[N-i-1]] += 1\n end\n values = hash.values.sort\n if values.all?(&:even?)\n next\n elsif values == [1, 3] || B[i] == B[N-i-1]\n ans += 1\n else\n ans += 2\n end\nend\nputs ans"}, {"source_code": "N = gets.to_i\nA = gets.chomp\nB = gets.chomp\n\nans = 0\n((N+1)/2).times do |i|\n hash = Hash.new(0)\n hash[A[i]] += 1\n hash[B[i]] += 1\n if i != N-i-1\n hash[A[N-i-1]] += 1\n hash[B[N-i-1]] += 1\n end\n values = hash.values.sort\n if values.all?(&:even?)\n next\n elsif B[i] != B[N-i-1] && A[i] != B[i] && A[N-i-1] != B[N-i-1] && A[i] != B[N-i-1] && A[N-i-1] && B[i]\n ans += 2\n else\n ans += 1\n end\nend\nputs ans"}, {"source_code": "N = gets.to_i\nA = gets.chomp\nB = gets.chomp\n\nans = 0\n((N+1)/2).times do |i|\n hash = Hash.new(0)\n hash[A[i]] += 1\n hash[B[i]] += 1\n if i != N-i-1\n hash[A[N-i-1]] += 1\n hash[B[N-i-1]] += 1\n end\n values = hash.values.sort\n if values.all?(&:even?)\n next\n elsif A[i] != A[N-i-1] && A[i] != B[i] && A[N-i-1] != B[N-i-1]\n ans += 2\n else\n ans += 1\n end\nend\nputs ans"}, {"source_code": "N = gets.to_i\nA = gets.chomp\nB = gets.chomp\n\nans = 0\n((N+1)/2).times do |i|\n hash = Hash.new(0)\n hash[A[i]] += 1\n hash[B[i]] += 1\n if i != N-i-1\n hash[A[N-i-1]] += 1\n hash[B[N-i-1]] += 1\n end\n values = hash.values.sort\n if values.all?(&:even?)\n next\n elsif B[i] != B[N-i-1] && A[i] != B[i] && A[N-i-1] != B[N-i-1]\n ans += 2\n else\n ans += 1\n end\nend\nputs ans"}], "src_uid": "259b4b538743608227bb6d22453b0833"} {"nl": {"description": "Appleman and Toastman play a game. Initially Appleman gives one group of n numbers to the Toastman, then they start to complete the following tasks: Each time Toastman gets a group of numbers, he sums up all the numbers and adds this sum to the score. Then he gives the group to the Appleman. Each time Appleman gets a group consisting of a single number, he throws this group out. Each time Appleman gets a group consisting of more than one number, he splits the group into two non-empty groups (he can do it in any way) and gives each of them to Toastman. After guys complete all the tasks they look at the score value. What is the maximum possible value of score they can get?", "input_spec": "The first line contains a single integer n (1 ≤ n ≤ 3·105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the initial group that is given to Toastman.", "output_spec": "Print a single integer — the largest possible score.", "sample_inputs": ["3\n3 1 5", "1\n10"], "sample_outputs": ["26", "10"], "notes": "NoteConsider the following situation in the first example. Initially Toastman gets group [3, 1, 5] and adds 9 to the score, then he give the group to Appleman. Appleman splits group [3, 1, 5] into two groups: [3, 5] and [1]. Both of them should be given to Toastman. When Toastman receives group [1], he adds 1 to score and gives the group to Appleman (he will throw it out). When Toastman receives group [3, 5], he adds 8 to the score and gives the group to Appleman. Appleman splits [3, 5] in the only possible way: [5] and [3]. Then he gives both groups to Toastman. When Toastman receives [5], he adds 5 to the score and gives the group to Appleman (he will throws it out). When Toastman receives [3], he adds 3 to the score and gives the group to Appleman (he will throws it out). Finally Toastman have added 9 + 1 + 8 + 5 + 3 = 26 to the score. This is the optimal sequence of actions."}, "positive_code": [{"source_code": "n = gets.to_i\nputs gets.chomp.split.map(&:to_i).sort.each_with_index.map{|x, i| x * (if i == n - 1 then i + 1 else i + 2 end)}.inject(:+)"}, {"source_code": "gets;a=gets.chomp.split.map(&:to_i).sort\np a.each_with_index.map{|x,i|x*(i+2)}.inject(:+)-a[-1]"}, {"source_code": "gets;a=gets.split.map(&:to_i).sort\np a.each_with_index.map{|x,i|x*(i+2)}.inject(:+)-a[-1]\n"}, {"source_code": "gets;a=gets.split.map(&:to_i).sort\np a.each_with_index.map{|x,i|x*(i+2)}.inject(:+)-a[-1]\n"}, {"source_code": "gets;a=gets.split.map(&:to_i).sort\np a.each_with_index.map{|x,i|x*(i+2)}.inject(:+)-a[-1]"}, {"source_code": "gets;a=gets.split.map(&:to_i).sort\np a.each_with_index.map{|x,i|x*(i+2)}.inject(:+)-a[-1]"}, {"source_code": "n=gets.to_i\nv=(gets.split.map &:to_i).sort\nx=-v[-1]\n(0...n).each{|i| x += (i+2)*v[i]}\np x"}, {"source_code": "gets;a=gets.split.map(&:to_i).sort\np a.each_with_index.map{|x,i|x*(i+2)}.inject(:+)-a[-1]\n"}, {"source_code": "gets;a=gets.split.map(&:to_i).sort\np a.each_with_index.map{|x,i|x*(i+2)}.inject(:+)-a[-1]\n"}, {"source_code": "gets\nx=gets.split.map{|e| e.to_i}.sort\n\nsum1=x.inject(0){|sumA,e|\n\tsumA+e\n}\nans=0\nwhile x.size>1\n\tans+=sum1\n\tsum1-=x[0]\n\tans+=x[0]\n\tx.shift\nend\nans+=x[0]\nputs ans\n\n"}, {"source_code": "n = gets.to_i\nvec = (gets.split.map &:to_i).sort\nx = 2*vec.reduce(:+) - vec[n-1]\nfor i in (1...n)\n x += i*vec[i] \nend\np x"}, {"source_code": "n=gets.to_i\nv=(gets.split.map &:to_i).sort\nx=2*v.reduce(:+)-v[-1]\n(1...n).each{|i| x+=i*v[i]}\np x"}, {"source_code": "n=gets.to_i\nv=(gets.split.map &:to_i).sort\nx=-v[-1]\n(0...n).each{|i| x += (i+2)*v[i]}\np x"}, {"source_code": "gets;a=gets.split.map(&:to_i).sort\np a.each_with_index.map{|x,i|x*(i+2)}.inject(:+)-a[-1]"}, {"source_code": "# coding: utf-8\n\nn = gets.to_i\nar = gets.split.map(&:to_i)\nar.sort!\n\n# 1 2 3 4 5\n# X X X X X sum [1 2 3 4 5]\n# X X X X X sp rt sum [1] [2 3 4 5]\n# ? X X X X throw sp rt sum [2] [3 4 5]\n# ? ? X X X throw sp rt sum [3] [4, 5]\n# ? ? ? ? ? \n# 1 3 5\n# X X X [1 3 5]\n# X X X [1] [3 5]\n# ? X X [3] [5]\n# ? ? ? empty\n# 1 2 3 4\n# X X X X [1 2 3 4]\n# X X X X [1] [2 3 4]\n# ? X X X [2] [3 4]\n# ? ? X X [3] [4]\n\ns = ar.sum\nacc = s\nwhile s > 0\n if ar.length > 1\n acc += s\n s -= ar.shift\n else\n break\n end\nend\n\nputs acc"}, {"source_code": "n, v, r = gets.to_i, gets.split.map {|e| e.to_i}.sort!, 0\nfor i in 0...n-1\n r+=v[i]*(i+2)\nend\np r+v[-1]*n"}, {"source_code": "n = Integer(gets.chomp)\nv = gets.chomp.split.map{|x| Integer(x)}.sort\nsum = 0\nfor x in v do sum += x end\nans = sum\nfor x in v[0...-1] do\n ans += sum\n sum -= x\nend\n\nputs ans"}, {"source_code": "gets;a=gets.split.map(&:to_i).sort\np a.each_with_index.map{|x,i|x*(i+2)}.inject(:+)-a[-1]\n"}, {"source_code": "gets;a=gets.split.map(&:to_i).sort\np a.each_with_index.map{|x,i|x*(i+2)}.inject(:+)-a[-1]\n"}, {"source_code": "gets;a=gets.split.map(&:to_i).sort\np a.each_with_index.map{|x,i|x*(i+2)}.inject(:+)-a[-1]\n"}, {"source_code": "M = 1_000_000_007\nn = gets.to_i\nxs = gets.split.map(&:to_i)\nxs.sort!\nsum = 0\nn.times do |i|\n sum += xs[i] * [i + 2, n].min\nend\np sum\n"}, {"source_code": "gets;a=gets.split.map(&:to_i).sort\np a.each_with_index.map{|x,i|x*(i+2)}.inject(:+)-a[-1]\n"}, {"source_code": "#!/usr/bin/ruby2.0\n\nn = STDIN.readline.to_i\nnumbers = STDIN.readline.split.collect {|s| s.to_i }\nnumbers.sort!\n\nscore = -numbers[n-1]\n(n.downto 1).each do |i|\n\tscore += (i+1)*numbers[i-1]\nend\n\nputs score\n\n"}, {"source_code": "require 'pp'\n\nn = STDIN.gets.chomp.to_i\nvals = STDIN.gets.chomp.split(/ /).map(&:to_i)\nvals.sort!.reverse!\nans = - vals.first\nvals.each do |val|\n ans += val * (n + 1)\n n -= 1\nend\n\nputs ans\n"}, {"source_code": "# 3\n# 3 1 5\n\nn = gets.to_i\narray_a = gets.split.map(&:to_i)\n\narray_a.sort!\n\nans = 0\n\narray_a.each_with_index do |a, i|\n break if i == n-1\n ans += a * (i+2)\nend\nans += array_a.last * n\n\nputs ans\n "}, {"source_code": "n=gets.to_i\narr=gets.split(/ /).map(&:to_i)\nsum=0\narr.each do |i|\n sum+=i\nend\nans=0\narr.sort.each do |i|\n ans+=sum+i\n sum-=i\nend\nputs ans-arr.sort[-1]"}, {"source_code": "N = gets.to_i\nsum = 0\narr = gets.split.map{ |xx|\n x = xx.to_i\n sum += x\n x\n}.sort_by{|x|x}\n# p arr\n# p sum\nans = sum\n(N-1).times do |i|\n ans += sum\n sum -= arr[i]\n # p ans\nend\nputs ans"}, {"source_code": "n = gets.to_i\na = gets.split(/\\s+/).map(&:to_i).sort_by {|e| -e}\nsum = result = a.inject {|a,b| a+b}\n\nwhile a.size > 1\n x = a.pop\n result += sum\n sum -= x\nend\n\np result\n\n"}, {"source_code": "n=gets.to_i\narr=Array.new()\narr=gets.split.map(&:to_i)\n\narr.sort!\nsum=arr[n-1]*n\nfor i in 2..n do\n sum+=arr[n-i]*(n+2-i)\nend\n\nputs sum\n"}, {"source_code": "\n\nn = gets.to_i\ntab = gets.split.map(&:to_i).sort\n\nsuffix_sum = tab[0..-2].reverse.inject([tab[-1]]){|x, y| x << x[-1]+y}.reverse\n\nresult = -tab[-1];\n\n(0..tab.size-1).each do |i|\n result += suffix_sum[i] + tab[i]\nend\n\nputs result\n\n\n\n"}], "negative_code": [{"source_code": "# coding: utf-8\n\nn = gets.to_i\nar = gets.split.map(&:to_i)\nar.sort!\n\n# 1 2 3 4 5\n# X X X X X [1 2 3 4 5]\n# X X X X X [1] [2 3 4 5]\n# ? X X X X [2] [3 4 5]\n# ? X X X X [2] [3] [4] [5]\n# ? ? ? ? ? \n# 1 3 5\n# X X X [1 3 5]\n# X X X [1] [3 5]\n# ? X X [3] [5]\n# ? ? ? empty\n# 1 2 3 4\n# X X X X [1 2 3 4]\n# X X X X [1] [2 3 4]\n# ? X X X [2 3 4]\n# ? X X X [2] [3 4]\n# ? ? X X [3 4]\n# ? ? X X [3] [4]\n\ns = ar.sum\nacc = 0\nwhile s > 0\n acc += s\n if ar.length > 2 # 왜냐면 2에선 split 된 다음이 없기 때문\n acc += s\n sub = ar.shift\n s -= sub\n else\n break\n end\nend\n\nputs acc"}], "src_uid": "4266948a2e793b4b461156af226e98fd"} {"nl": {"description": "You are given a string $$$s$$$ of even length $$$n$$$. String $$$s$$$ is binary, in other words, consists only of 0's and 1's.String $$$s$$$ has exactly $$$\\frac{n}{2}$$$ zeroes and $$$\\frac{n}{2}$$$ ones ($$$n$$$ is even).In one operation you can reverse any substring of $$$s$$$. A substring of a string is a contiguous subsequence of that string.What is the minimum number of operations you need to make string $$$s$$$ alternating? A string is alternating if $$$s_i \\neq s_{i + 1}$$$ for all $$$i$$$. There are two types of alternating strings in general: 01010101... or 10101010...", "input_spec": "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 a single integer $$$n$$$ ($$$2 \\le n \\le 10^5$$$; $$$n$$$ is even) — the length of string $$$s$$$. The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$ ($$$s_i \\in$$$ {0, 1}). String $$$s$$$ has exactly $$$\\frac{n}{2}$$$ zeroes and $$$\\frac{n}{2}$$$ ones. It's guaranteed that the total sum of $$$n$$$ over test cases doesn't exceed $$$10^5$$$.", "output_spec": "For each test case, print the minimum number of operations to make $$$s$$$ alternating.", "sample_inputs": ["3\n2\n10\n4\n0110\n8\n11101000"], "sample_outputs": ["0\n1\n2"], "notes": "NoteIn the first test case, string 10 is already alternating.In the second test case, we can, for example, reverse the last two elements of $$$s$$$ and get: 0110 $$$\\rightarrow$$$ 0101.In the third test case, we can, for example, make the following two operations: 11101000 $$$\\rightarrow$$$ 10101100; 10101100 $$$\\rightarrow$$$ 10101010. "}, "positive_code": [{"source_code": "t = gets.chomp.to_i\nt.times do\n n = gets.chomp.to_i\n s = gets.chomp.split(\"\").map(&:to_i)\n count = 0\n i = 0\n until i == n\n if i >= 1\n if s[i] == s[i-1]\n count += 1\n end\n end\n i += 1\n end\n if count % 2 == 0\n puts count / 2\n else\n puts count / 2 + 1\n end\nend"}], "negative_code": [{"source_code": "t = gets.chomp.to_i\nt.times do\n n = gets.chomp.to_i\n s = gets.chomp.split(\"\").map(&:to_i)\n arr = []\n ar = [s.shift]\n tmp = ar[0]\n count = 0\n size = 1\n while true\n i = s.shift\n if i != tmp\n ar << i\n size += 1\n else\n if size % 2 != 0\n arr << ar.pop\n size -= 1\n end\n if size >= 2\n count += 1\n end\n ar = []\n ar << i\n end\n tmp = i\n if s == []\n break\n end\n end\n puts count\nend"}, {"source_code": "t = gets.chomp.to_i\nt.times do\n n = gets.chomp.to_i\n s = gets.chomp.split(\"\").map(&:to_i)\n arr = []\n ar = [s.shift]\n tmp = ar[0]\n count = 0\n size = 1\n while true\n i = s.shift\n if i != tmp\n ar << i\n size += 1\n else\n if size % 2 != 0\n arr << ar.pop\n size -= 1\n end\n if size >= 2\n count += 1\n end\n ar = []\n size = 0\n ar << i\n size += 1\n end\n tmp = i\n if s == []\n if arr != []\n s = arr\n else\n break\n end\n end\n # puts \"arr\" + \"#{arr}\"\n # puts \"ar\" + \"#{ar}\"\n # puts \"size\" + \"#{size}\"\n end\n puts count\nend"}], "src_uid": "fd1e3368fbfbc3792831623398c94d80"} {"nl": {"description": "Manao has invented a new mathematical term — a beautiful set of points. He calls a set of points on a plane beautiful if it meets the following conditions: The coordinates of each point in the set are integers. For any two points from the set, the distance between them is a non-integer. Consider all points (x, y) which satisfy the inequations: 0 ≤ x ≤ n; 0 ≤ y ≤ m; x + y > 0. Choose their subset of maximum size such that it is also a beautiful set of points.", "input_spec": "The single line contains two space-separated integers n and m (1 ≤ n, m ≤ 100).", "output_spec": "In the first line print a single integer — the size k of the found beautiful set. In each of the next k lines print a pair of space-separated integers — the x- and y- coordinates, respectively, of a point from the set. If there are several optimal solutions, you may print any of them.", "sample_inputs": ["2 2", "4 3"], "sample_outputs": ["3\n0 1\n1 2\n2 0", "4\n0 3\n2 1\n3 0\n4 2"], "notes": "NoteConsider the first sample. The distance between points (0, 1) and (1, 2) equals , between (0, 1) and (2, 0) — , between (1, 2) and (2, 0) — . Thus, these points form a beautiful set. You cannot form a beautiful set with more than three points out of the given points. Note that this is not the only solution."}, "positive_code": [{"source_code": "a=gets.split.map &:to_i\n(p a.min+1).times{|i|puts [a[0]-i,i]*' '}\n"}, {"source_code": "n,m=gets.split.map &:to_i\n(p [n,m].min+1).times{|i|puts [n-i,i]*' '}"}, {"source_code": "a=gets.split.map &:to_i\n(p a.min+1).times{|i|puts [a[0]-i,i]*' '}\n"}, {"source_code": "def run\n n, m = ARGF.readline.split.map(&:to_i)\n puts num = [n, m].min + 1\n num.times do |i|\n puts [n - i, i].join(' ')\n end\nend\n\nrun if $0 == __FILE__\n"}, {"source_code": "a=gets.split.map &:to_i\n(p a.min+1).times{|i|puts [a[0]-i,i]*' '}\n"}, {"source_code": "n, m = gets.split(' ').map &:to_i\nq = [n,m].min\nputs q + 1\n(q+1).times do |time|\n puts [time, q - time].join(' ')\nend"}, {"source_code": "a=gets.split.map &:to_i\n(p a.min+1).times{|i|puts [a[0]-i,i]*' '}\n"}, {"source_code": "n,m=gets.split.map(&:to_i)\nk=[n,m].min+1\nputs k\ni=0\nk.times do\n puts \"#{k-i-1} #{i}\"\n i+=1\nend"}, {"source_code": "a=gets.split.map &:to_i\n(p a.min+1).times{|i|puts [a[0]-i,i]*' '}"}, {"source_code": "n,m=gets.split.map &:to_i\n(p [n,m].min+1).times{|i|puts [n-i,i]*' '}"}, {"source_code": "n,m = gets.split(' ').map(&:to_i)\n\nif n>m\n n = m\nend\nputs n+1\n(0..n).each do |k|\n puts \"#{k} #{n-k}\"\nend"}, {"source_code": "n, m = gets.split(' ')\nn = n.to_i\nm = m.to_i\n\nif n < m\n\tmin = n\nelse\n\tmin = m\nend\n\nbeaty = []\n\nosx = min\nosy = 0\n\n(min+1).times do |x|\n\tbeaty << [osx, osy]\n\tosx -= 1\n\tosy += 1\n\tosx = n if osx < 0\n\tosy = 0 if osy > m\nend\n\nputs beaty.length\n\nbeaty.each do |e|\n\tputs e.join(' ')\nend"}, {"source_code": "a=gets.split.map &:to_i\n(p a.min+1).times{|i|puts [a[0]-i,i]*' '}\n"}, {"source_code": "n,m = gets.split(\" \").map(&:to_i)\nmin = [n,m].min + 1\nputs min\n(0...min).each do |i|\n\tputs \"#{i} #{min - i - 1}\"\nend"}, {"source_code": "n,m = gets.chomp.split(\" \").collect! {|x| x.to_i}\nif n>m\n\tn = m\nend\n\nputs n+1\n(0..n).each{|k|\n\tputs \"#{k} #{n-k}\"\n}"}, {"source_code": "n, m = gets.split.collect{|i| i.to_i}\nans = [n, m].min+1\nputs ans\n(0...ans).each do |i|\n print i, \" \", ans-1-i, \"\\n\" \nend\n"}, {"source_code": "a=gets.split.map &:to_i\n(p a.min+1).times{|i|puts [a[0]-i,i]*' '}\n"}, {"source_code": "n, m = gets.split.map { |x| x.to_i }\nputs [n, m].min + 1\nm.downto([0, m - n].max).each { |y| puts \"#{m - y} #{y}\" }"}, {"source_code": "#!/usr/bin/env ruby\n\nn = gets.split.collect{|i| i.to_i}.min\n\nputs n + 1\n\n0.upto(n) { |i| puts \"#{i} #{n-i}\" }\n"}, {"source_code": "a=gets.split.map &:to_i\n(p a.min+1).times{|i|puts [a[0]-i,i]*' '}\n"}, {"source_code": "t = gets.split.collect{|x| x.to_i}\nn,m = t[0],t[1]\n\nmin = t.min\n\nputs min+1\n\n(min+1).times do |i|\n\tprint i,' ', min-i, \"\\n\"\nend"}, {"source_code": "a=gets.split.map &:to_i\n(p a.min+1).times{|i|puts [a[0]-i,i]*' '}\n"}], "negative_code": [{"source_code": "n,m=gets.split(&:to_i)\nk=[n,m].min.to_i+1\nputs k\ni=0\nk.times do\n puts \"#{i} #{i}\"\n i+=1\nend\n"}, {"source_code": "n,m=gets.split.map(&:to_i)\nn+=1\nm+=1\nif n n\n\tosy = 0 if osy > m\nend\n\nputs beaty.length\n\nbeaty.each do |e|\n\tputs e.join(' ')\nend"}, {"source_code": "n,m = gets.split(\" \").map(&:to_i)\nmin = [n,m].min + 1\nputs min\n(0...min).each do |i|\n\tputs \"#{i} #{min - i}\"\nend"}, {"source_code": "#!/usr/bin/env ruby\n\nn = gets.split.collect{|i| i.to_i}.min\n\nputs n + 1\n\nputs \"0 1\"\nputs \"1 0\"\n\n2.upto(n) { |i| puts \"#{i} #{i}\" }\n"}], "src_uid": "0e99f4a49b408cc8874a6d5ec4167acb"} {"nl": {"description": "Monocarp is the coach of the Berland State University programming teams. He decided to compose a problemset for a training session for his teams.Monocarp has $$$n$$$ problems that none of his students have seen yet. The $$$i$$$-th problem has a topic $$$a_i$$$ (an integer from $$$1$$$ to $$$n$$$) and a difficulty $$$b_i$$$ (an integer from $$$1$$$ to $$$n$$$). All problems are different, that is, there are no two tasks that have the same topic and difficulty at the same time.Monocarp decided to select exactly $$$3$$$ problems from $$$n$$$ problems for the problemset. The problems should satisfy at least one of two conditions (possibly, both): the topics of all three selected problems are different; the difficulties of all three selected problems are different. Your task is to determine the number of ways to select three problems for the problemset.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 50000$$$) — the number of testcases. The first line of each testcase contains an integer $$$n$$$ ($$$3 \\le n \\le 2 \\cdot 10^5$$$) — the number of problems that Monocarp have. In the $$$i$$$-th of the following $$$n$$$ lines, there are two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \\le a_i, b_i \\le n$$$) — the topic and the difficulty of the $$$i$$$-th problem. It is guaranteed that there are no two problems that have the same topic and difficulty at the same time. The sum of $$$n$$$ over all testcases doesn't exceed $$$2 \\cdot 10^5$$$.", "output_spec": "Print the number of ways to select three training problems that meet either of the requirements described in the statement.", "sample_inputs": ["2\n4\n2 4\n3 4\n2 1\n1 3\n5\n1 5\n2 4\n3 3\n4 2\n5 1"], "sample_outputs": ["3\n10"], "notes": "NoteIn the first example, you can take the following sets of three problems: problems $$$1$$$, $$$2$$$, $$$4$$$; problems $$$1$$$, $$$3$$$, $$$4$$$; problems $$$2$$$, $$$3$$$, $$$4$$$. Thus, the number of ways is equal to three."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n n=gets.to_i\r\n ab=[]\r\n la=[0]*(n+1)\r\n lb=[0]*(n+1)\r\n n.times do\r\n a,b=gets.split.map(&:to_i)\r\n ab << [a,b]\r\n la[a]+=1\r\n lb[b]+=1\r\n end\r\n c=0\r\n ab.each do |a,b|\r\n c+=(la[a]-1)*(lb[b]-1)\r\n end\r\n puts n*(n-1)*(n-2)/6-c\r\nend\r\n"}], "negative_code": [], "src_uid": "5a2d51da609b3c22bb8d801ebfb63822"} {"nl": {"description": "You have a sequence $$$a_1, a_2, \\ldots, a_n$$$ of length $$$n$$$, consisting of integers between $$$1$$$ and $$$m$$$. You also have a string $$$s$$$, consisting of $$$m$$$ characters B.You are going to perform the following $$$n$$$ operations. At the $$$i$$$-th ($$$1 \\le i \\le n$$$) operation, you replace either the $$$a_i$$$-th or the $$$(m + 1 - a_i)$$$-th character of $$$s$$$ with A. You can replace the character at any position multiple times through the operations. Find the lexicographically smallest string you can get after these operations.A string $$$x$$$ is lexicographically smaller than a string $$$y$$$ of the same length if and only if in the first position where $$$x$$$ and $$$y$$$ differ, the string $$$x$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$y$$$.", "input_spec": "The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 2000$$$). The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n, m \\le 50$$$) — the length of the sequence $$$a$$$ and the length of the string $$$s$$$ respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le m$$$) — the sequence $$$a$$$.", "output_spec": "For each test case, print a string of length $$$m$$$ — the lexicographically smallest string you can get. Each character of the string should be either capital English letter A or capital English letter B.", "sample_inputs": ["6\n\n4 5\n\n1 1 3 1\n\n1 5\n\n2\n\n4 1\n\n1 1 1 1\n\n2 4\n\n1 3\n\n2 7\n\n7 5\n\n4 5\n\n5 5 3 5"], "sample_outputs": ["ABABA\nBABBB\nA\nAABB\nABABBBB\nABABA"], "notes": "NoteIn the first test case, the sequence $$$a = [1, 1, 3, 1]$$$. One of the possible solutions is the following. At the $$$1$$$-st operation, you can replace the $$$1$$$-st character of $$$s$$$ with A. After it, $$$s$$$ becomes ABBBB. At the $$$2$$$-nd operation, you can replace the $$$5$$$-th character of $$$s$$$ with A (since $$$m+1-a_2=5$$$). After it, $$$s$$$ becomes ABBBA. At the $$$3$$$-rd operation, you can replace the $$$3$$$-rd character of $$$s$$$ with A. After it, $$$s$$$ becomes ABABA. At the $$$4$$$-th operation, you can replace the $$$1$$$-st character of $$$s$$$ with A. After it, $$$s$$$ remains equal to ABABA. The resulting string is ABABA. It is impossible to produce a lexicographically smaller string.In the second test case, you are going to perform only one operation. You can replace either the $$$2$$$-nd character or $$$4$$$-th character of $$$s$$$ with A. You can get strings BABBB and BBBAB after the operation. The string BABBB is the lexicographically smallest among these strings.In the third test case, the only string you can get is A.In the fourth test case, you can replace the $$$1$$$-st and $$$2$$$-nd characters of $$$s$$$ with A to get AABB.In the fifth test case, you can replace the $$$1$$$-st and $$$3$$$-rd characters of $$$s$$$ with A to get ABABBBB."}, "positive_code": [{"source_code": "def main\n t = inp.to_i\n t.times do |i|\n n, m = mapint(inarr)\n a = mapint(inarr)\n str = 'B' * m\n n.times do |i|\n x = min(a[i], m + 1 - a[i])\n y = max(a[i], m + 1 - a[i])\n if str[x - 1] == 'B'\n str[x - 1] = 'A'\n else\n str[y - 1] = 'A'\n end\n end\n puts str\n end\n\nend\n\n# lib\ndef inp\n gets.chomp\nend\n\ndef inarr\n gets.chomp.split\nend\n\ndef mapint(lst)\n lst.map(&:to_i)\nend\n\ndef max(a, b)\n (a > b)? a:b\nend\n\ndef min(a, b)\n (a < b)? a:b\nend\n\nmain"}], "negative_code": [], "src_uid": "eee23388aa7cda50302fc4da6e50e172"} {"nl": {"description": "A string t is called an anagram of the string s, if it is possible to rearrange letters in t so that it is identical to the string s. For example, the string \"aab\" is an anagram of the string \"aba\" and the string \"aaa\" is not.The string t is called a substring of the string s if it can be read starting from some position in the string s. For example, the string \"aba\" has six substrings: \"a\", \"b\", \"a\", \"ab\", \"ba\", \"aba\".You are given a string s, consisting of lowercase Latin letters and characters \"?\". You are also given a string p, consisting of lowercase Latin letters only. Let's assume that a string is good if you can obtain an anagram of the string p from it, replacing the \"?\" characters by Latin letters. Each \"?\" can be replaced by exactly one character of the Latin alphabet. For example, if the string p = «aba», then the string \"a??\" is good, and the string «?bc» is not. Your task is to find the number of good substrings of the string s (identical substrings must be counted in the answer several times).", "input_spec": "The first line is non-empty string s, consisting of no more than 105 lowercase Latin letters and characters \"?\". The second line is non-empty string p, consisting of no more than 105 lowercase Latin letters. Please note that the length of the string p can exceed the length of the string s.", "output_spec": "Print the single number representing the number of good substrings of string s. Two substrings are considered different in their positions of occurrence are different. Thus, if some string occurs several times, then it should be counted the same number of times.", "sample_inputs": ["bb??x???\naab", "ab?c\nacb"], "sample_outputs": ["2", "2"], "notes": "NoteConsider the first sample test. Here the string s has two good substrings: \"b??\" (after we replace the question marks we get \"baa\"), \"???\" (after we replace the question marks we get \"baa\").Let's consider the second sample test. Here the string s has two good substrings: \"ab?\" (\"?\" can be replaced by \"c\"), \"b?c\" (\"?\" can be replaced by \"a\")."}, "positive_code": [{"source_code": "a=gets.split[0].bytes.to_a\nb=gets.split[0].bytes.to_a\ncntb=Array.new(256,0)\nif a.lengthcntb[i] then\n ok=false\n break\n end\nend\nans+=1 if ok\nfor i in 1..a.length-b.length\n cnta[a[i-1]]-=1\n cnta[a[i+b.length-1]]+=1\n ok=true\n for i in 97..122\n if cnta[i]>cntb[i] then\n ok=false\n break\n end\n end\n ans+=1 if ok\nend\np ans\n"}, {"source_code": "s, p = gets.chomp.unpack(\"C*\"), gets.chomp.unpack(\"C*\")\nn, m = s.length, p.length\n\ncnt = Array.new(256,0)\na = \"abcdefghijklmnopqrstuvwxyz?\".unpack(\"C*\")\n\nif n < m\n p 0\n exit\nend\n\nfor i in 0...m\n cnt[s[i]] += 1\n cnt[p[i]] -= 1\nend\n\nans = 0\nfor i in m..n\n tmp = cnt[a[26]]\n for j in 0..25\n tmp -= cnt[a[j]].abs\n end\n ans += 1 if tmp >= 0\n break if i == n\n cnt[s[i]] += 1\n cnt[s[i-m]] -= 1\nend\np ans"}], "negative_code": [], "src_uid": "9ed2a081b65df66629fcf0d4fc0bb02c"} {"nl": {"description": "You are given a rectangular grid of lattice points from (0, 0) to (n, m) inclusive. You have to choose exactly 4 different points to build a polyline possibly with self-intersections and self-touching. This polyline should be as long as possible.A polyline defined by points p1, p2, p3, p4 consists of the line segments p1 p2, p2 p3, p3 p4, and its length is the sum of the lengths of the individual line segments.", "input_spec": "The only line of the input contains two integers n and m (0 ≤ n, m ≤ 1000). It is guaranteed that grid contains at least 4 different points.", "output_spec": "Print 4 lines with two integers per line separated by space — coordinates of points p1, p2, p3, p4 in order which represent the longest possible polyline. Judge program compares your answer and jury's answer with 10 - 6 precision.", "sample_inputs": ["1 1", "0 10"], "sample_outputs": ["1 1\n0 0\n1 0\n0 1", "0 1\n0 10\n0 0\n0 9"], "notes": null}, "positive_code": [{"source_code": "def dist(a, b)\n dx = a[0]-b[0]\n dy = a[1]-b[1]\n Math.sqrt(dx*dx+dy*dy)\nend\n\ndef len(arr)\n dist(arr[0],arr[1]) + dist(arr[1],arr[2]) + dist(arr[2],arr[3])\nend\n\nn, m = gets.strip.split(\" \").map{|x| x.to_i}\n\nif n == 0\n puts \"0 1\"\n puts \"0 #{m}\"\n puts \"0 0\"\n puts \"0 #{m-1}\"\n exit\nend\nif m == 0\n puts \"1 0\"\n puts \"#{n} 0\"\n puts \"0 0\"\n puts \"#{n-1} 0\"\n exit\nend\n\ncandidates = []\n\ncandidates.push [\n [n, m-1],\n [0, 0],\n [n, m],\n [0, 1]\n]\ncandidates.push [\n [n-1, m],\n [0, 0],\n [n, m],\n [1, 0]\n]\ncandidates.push [\n [0, 0],\n [n, m],\n [n, 0],\n [0, m]\n]\ncandidates.push [\n [0, 0],\n [n, m],\n [0, m],\n [n, 0]\n]\n\ncandidates.sort!{|a, b| -(len(a)<=>len(b))}\n\nputs candidates.first.map{|x| x.join(\" \")}\n"}, {"source_code": "n, m = gets.split.map &:to_i\nr = false\nif n > m \n n, m = m, n\n r = true\nend\n\nans = []\nif n == 0\n ans << [0, 1] << [0, m] << [0, 0] << [0, m - 1]\nelsif Math.hypot(n, m) * 2 + m > Math.hypot(n, m) + Math.hypot(m, n - 1) * 2\n ans << [0, 0] << [n, m] << [n, 0] << [0, m]\nelse\n ans << [1, 0] << [n, m] << [0, 0] << [n - 1, m]\nend\n\nans.each{|i|\n if !r\n puts i * ' '\n else\n puts i.reverse * ' '\n end\n}"}, {"source_code": "n,m=gets.split.map{|i| i.to_i}\nr=false\nif n>m\n\tn,m=m,n\n\tr=true\nend\n\na=[]\nif n==0\n\ta<<[0,1]<<[0,m]<<[0,0]<<[0,m-1]\nelsif Math.sqrt(n**2+m**2)*2+m>(Math.sqrt(n**2+m**2)+2*Math.sqrt(m**2+(n-1)**2))\n\ta<<[0,0]<<[n,m]<<[n,0]<<[0,m]\nelsif \n\ta<<[1,0]<<[n,m]<<[0,0]<<[n-1,m]\nend\na.each{|i| \n\tif !r \n\t\tputs i*' '\n\telse\n\t\tputs i.reverse*' '\n\tend}"}, {"source_code": "n,m = gets.split.map(&:to_i)\ncandidates = (0..n).to_a.product((0..m).to_a).select { |p|\n [p[0]+p[1], p[0]+m-p[1], n-p[0]+p[1], n-p[0]+m-p[1]].min <= 1\n}\nanswer = candidates.permutation(4).max_by { |pl|\n x1 = (Math.hypot(pl[0][0] - pl[1][0], pl[0][1] - pl[1][1]))\n x2 = (Math.hypot(pl[1][0] - pl[2][0], pl[1][1] - pl[2][1]))\n x3 = (Math.hypot(pl[2][0] - pl[3][0], pl[2][1] - pl[3][1]))\n x1 + x2 + x3\n}\nanswer.each { |p|\n puts \"#{p[0]} #{p[1]}\"\n}"}, {"source_code": "\nn, m = gets.split.map(&:to_i)\nif n == 0 && m == 0\n puts \"0 0\", \"0 0\", \"0 0\", \"0 0\"\nelsif n == 0\n puts \"0 1\", \"0 #{m}\", \"0 0\", \"0 #{m - 1}\"\nelsif m == 0\n puts \"1 0\", \"#{n} 0\", \"0 0\", \"#{n - 1} 0\"\nelse\n bigdiag = Math.sqrt(n * n + m * m)\n ndiag = Math.sqrt((n - 1) * (n - 1) + m * m)\n mdiag = Math.sqrt(n * n + (m - 1) * (m - 1))\n\n if n > m\n ans1 = [n + 2 * bigdiag, [\"0 0\", \"#{n} #{m}\", \"0 #{m}\", \"#{n} 0\"]]\n else\n ans1 = [m + 2 * bigdiag, [\"0 0\", \"#{n} #{m}\", \"#{n} 0\", \"0 #{m}\"]]\n end\n\n if n <= 2 || m <= 2\n puts ans1[1]\n else\n ans2 = [mdiag + ndiag + bigdiag, [\"1 0\", \"#{n} #{m}\", \"0 0\", \"#{n} #{m - 1}\"]]\n ans3 = [ndiag + ndiag + bigdiag, [\"1 0\", \"#{n} #{m}\", \"0 0\", \"#{n - 1} #{m}\"]]\n ans4 = [mdiag + mdiag + bigdiag, [\"0 1\", \"#{n} #{m}\", \"0 0\", \"#{n} #{m - 1}\"]]\n\n\n answers = [ans1, ans2, ans3, ans4].sort\n puts answers[-1][1]\n end\nend\n"}, {"source_code": "# Created by Alex\nn, m = gets.split.map{|i| i.to_i}\nif n == 1 && m == 1\n puts '1 1', '0 0', '1 0', '0 1'\n exit 0\nend\nif n == 0\n puts '0 1', \"0 #{m}\", '0 0', \"0 #{m - 1}\"\n exit 0\nend\nif m == 0\n puts '1 0', \"#{n} 0\", '0 0', \"#{n - 1} 0\"\n exit 0\nend\na1 = Math.hypot(n, m) + 2 * [Math.hypot(n - 1, m), Math.hypot(n, m - 1)].max\na2 = Math.hypot(n, m) * 2 + [n, m].max\nif a2 > a1\n if n > m\n puts '0 0', \"#{n} #{m}\", \"0 #{m}\", \"#{n} 0\"\n else\n puts '0 0', \"#{n} #{m}\", \"#{n} 0\", \"0 #{m}\"\n end\nelse\n if Math.hypot(n - 1, m) > Math.hypot(n, m - 1)\n puts '1 0', \"#{n} #{m}\", '0 0', \"#{n - 1} #{m}\"\n else\n puts '0 1', \"#{n} #{m}\", '0 0', \"#{n} #{m - 1}\"\n end\nend"}], "negative_code": [{"source_code": "n,m=gets.split.map{|i| i.to_i}\nn,m=m,n if n>m\nputs \"0 1\"\nputs \"#{n} #{m}\"\nputs \"0 0\"\nputs \"#{n} #{m-1}\""}, {"source_code": "n,m=gets.split.map{|i| i.to_i}\nr=false\nif n>m\n\tn,m=m,n\n\tr=true\nend\nif n==m&&n<=2\n\tputs \"#{n} #{m}\\n0 0\\n#{n} 0\\n0 #{m}\\n\"\n\texit\nend\na=[]\nif n==0\n\ta<<[0,1]<<[0,m]<<[0,0]<<[0,m-1]\nelse \n\ta<<[1,0]<<[n,m]<<[0,0]<<[n-1,m]\nend\na.each{|i| \n\tif !r \n\t\tputs i*' '\n\telse\n\t\tputs i.reverse*' '\n\tend}"}, {"source_code": "n,m=gets.split.map{|i| i.to_i}\nn,m=m,n if n>m\nputs \"0 0\"\nputs \"#{n} #{m}\"\nputs \"0 1\"\nputs \"#{n} #{m-1}\""}, {"source_code": "n,m=gets.split.map{|i| i.to_i}\nr=false\nif n>m\n\tn,m=m,n\n\tr=true\nend\nif n==m\n\tputs \"#{n} #{m}\\n0 0\\n#{n} 0\\n0 #{m}\\n\"\n\texit\nend\na=[]\nif n==0\n\ta<<[0,1]<<[0,m]<<[0,0]<<[0,m-1]\nelse \n\ta<<[1,0]<<[n,m]<<[0,0]<<[n-1,m]\nend\na.each{|i| \n\tif !r \n\t\tputs i*' '\n\telse\n\t\tputs i.reverse*' '\n\tend}"}, {"source_code": "n,m=gets.split.map{|i| i.to_i}\nn,m=m,n if n>m\nif n+m==2\n\tputs \"1 1\\n0 0\\n1 0\\n0 1\\n\"\n\texit\nend\n\nputs \"0 1\"\nputs \"#{n} #{m}\"\nputs \"0 0\"\nputs \"#{n} #{m-1}\""}, {"source_code": "n,m=gets.split.map{|i| i.to_i}\nn,m=m,n if n>m\nif n+m==2\n\tputs \"0 1\\n1 1\\n0 0\\n1 0\\n\"\n\texit\nend\n\nputs \"0 1\"\nputs \"#{n} #{m}\"\nputs \"0 0\"\nputs \"#{n} #{m-1}\""}, {"source_code": "n,m=gets.split.map{|i| i.to_i}\nr=false\nif n>m\n\tn,m=m,n\n\tr=true\nend\nif n==m&&n<4\n\tputs \"#{n} #{m}\\n0 0\\n#{n} 0\\n0 #{m}\\n\"\n\texit\nend\na=[]\nif n==0\n\ta<<[0,1]<<[0,m]<<[0,0]<<[0,m-1]\nelse \n\ta<<[1,0]<<[n,m]<<[0,0]<<[n-1,m]\nend\na.each{|i| \n\tif !r \n\t\tputs i*' '\n\telse\n\t\tputs i.reverse*' '\n\tend}"}, {"source_code": "n,m=gets.split.map{|i| i.to_i}\nr=false\nif n>m\n\tn,m=m,n\n\tr=true\nend\nif n+m==2\n\tputs \"1 1\\n0 0\\n1 0\\n0 1\\n\"\n\texit\nend\na=[]\nif n==0\n\ta<<[0,1]<<[0,m]<<[0,0]<<[0,m-1]\nelse \n\ta<<[1,0]<<[n,m]<<[0,0]<<[n-1,m]\nend\na.each{|i| \n\tif !r \n\t\tputs i*' '\n\telse\n\t\tputs i.reverse*' '\n\tend}"}, {"source_code": "n,m=gets.split.map{|i| i.to_i}\nr=false\nif n>m\n\tn,m=m,n\n\tr=true\nend\nif n+m==2\n\tputs \"1 1\\n0 0\\n1 0\\n0 1\\n\"\n\texit\nend\na=[]\nputs r\nif n==0\n\ta<<[0,1]<<[0,m]<<[0,0]<<[0,m-1]\nelse \n\ta<<[1,0]<<[n,m]<<[0,0]<<[n-1,m]\nend\na.each{|i| \n\tif !r \n\t\tputs i*' '\n\telse\n\t\tputs i.reverse*' '\n\tend}"}, {"source_code": "n,m=gets.split.map{|i| i.to_i}\nr=false\nif n>m\n\tn,m=m,n\n\tr=true\nend\nif n+m==2\n\tputs \"1 1\\n0 0\\n1 0\\n0 1\\n\"\n\texit\nend\na=[]\nif n==0\n\ta<<[0,1]<<[0,m]<<[0,0]<<[0,m-1]\nelse \n\ta<<[1,0]<<[n,m]<<[0,0]<<[n-1,m]\nend\na.each{|i| \n\tif r \n\t\tputs i*' '\n\telse\n\t\tputs i.reverse*' '\n\tend}"}, {"source_code": "n,m = gets.split.map(&:to_i)\ncandidates = (0..n).to_a.product((0..m).to_a).select { |p|\n [p[0]+p[1], p[0]+m-p[1], n-p[0]+p[1], n-p[0]+m-p[1]].min <= 1\n}\nanswer = candidates.permutation(4).max_by { |pl|\n Math.hypot(pl[0][0] - pl[1][0], pl[0][1] - pl[1][1])\n + Math.hypot(pl[1][0] - pl[2][0], pl[1][1] - pl[2][1])\n + Math.hypot(pl[2][0] - pl[3][0], pl[2][1] - pl[3][1])\n}\nanswer.each { |p|\n puts \"#{p[0]} #{p[1]}\"\n}"}, {"source_code": "\nn, m = gets.split.map(&:to_i)\nif n == 0 && m == 0\n puts \"0 0\", \"0 0\", \"0 0\", \"0 0\"\nelsif n == 0\n puts \"0 1\", \"0 #{m}\", \"0 0\", \"0 #{m - 1}\"\nelsif m == 0\n puts \"1 0\", \"#{n} 0\", \"0 0\", \"#{n - 1} 0\"\nelsif n > m\n puts \"0 0\", \"#{n} #{m}\", \"0 #{m}\", \"#{n} 0\"\nelse\n puts \"0 0\", \"#{n} #{m}\", \"#{n} 0\", \"0 #{m}\"\nend\n"}, {"source_code": "\nn, m = gets.split.map(&:to_i)\nif n == 0 && m == 0\n puts \"0 0\", \"0 0\", \"0 0\", \"0 0\"\nelsif n == 0\n puts \"0 0\", \"0 #{m}\", \"0 1\", \"0 #{m - 1}\"\nelsif m == 0\n puts \"0 0\", \"#{n} 0\", \"1 0\", \"#{n - 1} 0\"\nelsif n > m\n puts \"0 0\", \"#{n} #{m}\", \"0 #{m}\", \"#{n} 0\"\nelse\n puts \"0 0\", \"#{n} #{m}\", \"#{n} 0\", \"0 #{m}\"\nend\n"}, {"source_code": "\nn, m = gets.split.map(&:to_i)\nif n == 0 && m == 0\n puts \"0 0\", \"0 0\", \"0 0\", \"0 0\"\nelsif n == 0\n puts \"0 1\", \"0 #{m}\", \"0 0\", \"0 #{m - 1}\"\nelsif m == 0\n puts \"1 0\", \"#{n} 0\", \"0 0\", \"#{n - 1} 0\"\nelse\n bigdiag = Math.sqrt(n * n + m * m)\n ndiag = Math.sqrt((n - 1) * (n - 1) + m * m)\n mdiag = Math.sqrt(n * n + (m - 1) * (m - 1))\n\n if n > m\n ans1 = [2 * n + bigdiag, [\"0 0\", \"#{n} #{m}\", \"0 #{m}\", \"#{n} 0\"]]\n else\n ans1 = [2 * m + bigdiag, [\"0 0\", \"#{n} #{m}\", \"#{n} 0\", \"0 #{m}\"]]\n end\n\n if n == 1 || m == 1\n puts ans1[1]\n else\n ans2 = [mdiag + ndiag + bigdiag, [\"1 0\", \"#{n} #{m}\", \"0 0\", \"#{n} #{m - 1}\"]]\n ans3 = [ndiag + ndiag + bigdiag, [\"1 0\", \"#{n} #{m}\", \"0 0\", \"#{n - 1} #{m}\"]]\n ans4 = [mdiag + mdiag + bigdiag, [\"0 1\", \"#{n} #{m}\", \"0 0\", \"#{n} #{m - 1}\"]]\n\n answers = [ans1, ans2, ans3, ans4].sort\n puts answers[-1][1]\n end\nend\n"}, {"source_code": "\nn, m = gets.split.map(&:to_i)\nif n == 0 && m == 0\n puts \"0 0\", \"0 0\", \"0 0\", \"0 0\"\nelsif n == 0\n puts \"0 1\", \"0 #{m}\", \"0 0\", \"0 #{m - 1}\"\nelsif m == 0\n puts \"1 0\", \"#{n} 0\", \"0 0\", \"#{n - 1} 0\"\nelse\n bigdiag = Math.sqrt(n * n + m * m)\n ndiag = Math.sqrt((n - 1) * (n - 1) + m * m)\n mdiag = Math.sqrt(n * n + (m - 1) * (m - 1))\n\n ans2 = mdiag + ndiag + bigdiag\n if n > m\n ans1 = 2 * n + bigdiag\n # puts \"1:\", ans1, ans2\n if ans1 > ans2 || n == 1 || m == 1\n puts \"0 0\", \"#{n} #{m}\", \"0 #{m}\", \"#{n} 0\"\n else\n puts \"1 0\", \"#{n} #{m}\", \"0 0\", \"#{n} #{m - 1}\"\n end\n else\n ans1 = 2 * m + bigdiag\n # puts \"2:\", ans1, ans2\n if ans1 > ans2 || n == 1 || m == 1\n puts \"0 0\", \"#{n} #{m}\", \"#{n} 0\", \"0 #{m}\"\n else\n puts \"1 0\", \"#{n} #{m}\", \"0 0\", \"#{n} #{m - 1}\"\n end\n end\nend\n"}, {"source_code": "\nn, m = gets.split.map(&:to_i)\nif n == 0 && m == 0\n puts \"0 0\", \"0 0\", \"0 0\", \"0 0\"\nelsif n == 0\n puts \"0 1\", \"0 #{m}\", \"0 0\", \"0 #{m - 1}\"\nelsif m == 0\n puts \"1 0\", \"#{n} 0\", \"0 0\", \"#{n - 1} 0\"\nelse\n bigdiag = Math.sqrt(n * n + m * m)\n ndiag = Math.sqrt((n - 1) * (n - 1) + m * m)\n mdiag = Math.sqrt(n * n + (m - 1) * (m - 1))\n\n if n > m\n ans1 = [2 * n + bigdiag, [\"0 0\", \"#{n} #{m}\", \"0 #{m}\", \"#{n} 0\"]]\n else\n ans1 = [2 * m + bigdiag, [\"0 0\", \"#{n} #{m}\", \"#{n} 0\", \"0 #{m}\"]]\n end\n\n puts ans1[1] if n == 1 || m == 1\n\n ans2 = [mdiag + ndiag + bigdiag, [\"1 0\", \"#{n} #{m}\", \"0 0\", \"#{n} #{m - 1}\"]]\n ans3 = [ndiag + ndiag + bigdiag, [\"1 0\", \"#{n} #{m}\", \"0 0\", \"#{n - 1} #{m}\"]]\n ans4 = [mdiag + mdiag + bigdiag, [\"0 1\", \"#{n} #{m}\", \"0 0\", \"#{n} #{m - 1}\"]]\n\n answers = [ans1, ans2, ans3, ans4].sort\n puts answers[-1][1]\n\nend\n"}, {"source_code": "\nn, m = gets.split.map(&:to_i)\nif n == 0 && m == 0\n puts \"0 0\", \"0 0\", \"0 0\", \"0 0\"\nelsif n == 0\n puts \"0 1\", \"0 #{m}\", \"0 0\", \"0 #{m - 1}\"\nelsif m == 0\n puts \"1 0\", \"#{n} 0\", \"0 0\", \"#{n - 1} 0\"\nelse\n bigdiag = Math.sqrt(n * n + m * m)\n ndiag = Math.sqrt((n - 1) * (n - 1) + m * m)\n mdiag = Math.sqrt(n * n + (m - 1) * (m - 1))\n\n ans2 = mdiag + ndiag + bigdiag\n if n > m\n ans1 = 2 * n + bigdiag\n # puts \"1:\", ans1, ans2\n if ans1 > ans2 || n == 1 || m || 1\n puts \"0 0\", \"#{n} #{m}\", \"0 #{m}\", \"#{n} 0\"\n else\n puts \"1 0\", \"#{n} #{m}\", \"0 0\", \"#{n} #{m - 1}\"\n end\n else\n ans1 = 2 * m + bigdiag\n # puts \"2:\", ans1, ans2\n if ans1 > ans2 || n == 1 || m || 1\n puts \"0 0\", \"#{n} #{m}\", \"#{n} 0\", \"0 #{m}\"\n else\n puts \"1 0\", \"#{n} #{m}\", \"0 0\", \"#{n} #{m - 1}\"\n end\n end\nend\n"}, {"source_code": "\nn, m = gets.split.map(&:to_i)\nif n == 0 && m == 0\n puts \"0 0\", \"0 0\", \"0 0\", \"0 0\"\nelsif n == 0\n puts \"0 1\", \"0 #{m}\", \"0 0\", \"0 #{m - 1}\"\nelsif m == 0\n puts \"1 0\", \"#{n} 0\", \"0 0\", \"#{n - 1} 0\"\nelse\n bigdiag = Math.sqrt(n * n + m * m)\n ndiag = Math.sqrt((n - 1) * (n - 1) + m * m)\n mdiag = Math.sqrt(n * n + (m - 1) * (m - 1))\n\n if n > m\n ans1 = [2 * n + bigdiag, [\"0 0\", \"#{n} #{m}\", \"0 #{m}\", \"#{n} 0\"]]\n else\n ans1 = [2 * m + bigdiag, [\"0 0\", \"#{n} #{m}\", \"#{n} 0\", \"0 #{m}\"]]\n end\n\n if n <= 2 || m <= 2\n puts ans1[1]\n else\n ans2 = [mdiag + ndiag + bigdiag, [\"1 0\", \"#{n} #{m}\", \"0 0\", \"#{n} #{m - 1}\"]]\n ans3 = [ndiag + ndiag + bigdiag, [\"1 0\", \"#{n} #{m}\", \"0 0\", \"#{n - 1} #{m}\"]]\n ans4 = [mdiag + mdiag + bigdiag, [\"0 1\", \"#{n} #{m}\", \"0 0\", \"#{n} #{m - 1}\"]]\n\n answers = [ans1, ans2, ans3, ans4].sort\n puts answers[-1][1]\n end\nend\n"}, {"source_code": "\nn, m = gets.split.map(&:to_i)\nif n == 0 && m == 0\n puts \"0 0\", \"0 0\", \"0 0\", \"0 0\"\nelsif n == 0\n puts \"0 1\", \"0 #{m}\", \"0 0\", \"0 #{m - 1}\"\nelsif m == 0\n puts \"1 0\", \"#{n} 0\", \"0 0\", \"#{n - 1} 0\"\nelse\n bigdiag = Math.sqrt(n * n + m * m)\n ndiag = Math.sqrt((n - 1) * (n - 1) + m * m)\n mdiag = Math.sqrt(n * n + (m - 1) * (m - 1))\n\n ans2 = mdiag + ndiag + bigdiag\n if n > m\n ans1 = 2 * n + bigdiag\n if ans1 > ans2\n puts \"0 0\", \"#{n} #{m}\", \"0 #{m}\", \"#{n} 0\"\n else\n puts \"1 0\", \"#{n} #{m}\", \"0 0\", \"#{n} #{m - 1}\"\n end\n else\n ans1 = 2 * m + bigdiag\n if ans1 > ans2\n puts \"0 0\", \"#{n} #{m}\", \"#{n} 0\", \"0 #{m}\"\n else\n puts \"1 0\", \"#{n} #{m}\", \"0 0\", \"#{n} #{m - 1}\"\n end\n end\nend\n"}, {"source_code": "# Created by Alex\nn, m = gets.split.map{|i| i.to_i}\nif n == 1 && m == 1\n puts '1 1', '0 0', '1 0', '0 1'\n exit 0\nend\nif n == 0\n puts '0 1', \"0 #{m}\", '0 0', \"0 #{m - 1}\"\n exit 0\nend\nif m == 0\n puts '1 0', \"#{n} 0\", '0 0', \"#{n - 1} 0\"\n exit 0\nend\nputs '0 0', \"#{n} #{m}\"\nif n < m\n puts '1 0', \"#{n - 1} #{m}\"\nelse\n puts '0 1', \"#{n} #{m - 1}\"\nend"}], "src_uid": "78d54d76cb113cf74ea89fb77471859b"} {"nl": {"description": "There is a right triangle with legs of length a and b. Your task is to determine whether it is possible to locate the triangle on the plane in such a way that none of its sides is parallel to the coordinate axes. All the vertices must have integer coordinates. If there exists such a location, you have to output the appropriate coordinates of vertices.", "input_spec": "The first line contains two integers a, b (1 ≤ a, b ≤ 1000), separated by a single space.", "output_spec": "In the first line print either \"YES\" or \"NO\" (without the quotes) depending on whether the required location exists. If it does, print in the next three lines three pairs of integers — the coordinates of the triangle vertices, one pair per line. The coordinates must be integers, not exceeding 109 in their absolute value.", "sample_inputs": ["1 1", "5 5", "5 10"], "sample_outputs": ["NO", "YES\n2 1\n5 5\n-2 4", "YES\n-10 4\n-2 -2\n1 2"], "notes": null}, "positive_code": [{"source_code": "x, y = gets.split.map { |x| Integer x }\np1 = 1.0\np2 = 1000.0\nfor i in 1..2000\n if p1 ** 2 + p2 ** 2 > x ** 2\n p2 -= 1\n elsif p1 ** 2 + p2 ** 2 < x ** 2\n p1 += 1\n else\n arc = Math.acos(p1 / x)\n arc2 = Math::PI / 2 - arc\n p3 = y * Math.cos(arc2)\n p4 = Math.sqrt (y ** 2 - p3.round ** 2)\n if (p3.round - p3).abs <= 10 ** -9 && (p4 - p4.round).abs <= 10 ** -9\n if [p1, p3, p2, p4].index(0) != nil\n puts 'NO'\n exit\n end\n if p2.round == p4.round\n puts 'YES'\n puts (-p2.to_i).to_s + ' ' + p1.to_i.to_s\n puts '0 0'\n puts p4.round.to_s + ' ' + p3.round.to_s\n exit\n end\n puts 'YES'\n puts (-p1.to_i).to_s + ' ' + (p2.to_i).to_s\n puts '0 0'\n puts p3.round.to_s + ' ' + p4.round.to_s\n exit\n end\n p1 += 1\n p2 -= 1\n end\nend\nputs 'NO'\n\n"}, {"source_code": "q=[[],[]]\ngets.split.map(&:to_i).each_with_index{|x,p| (1..x).each{|i|(1..x).each{|j|q[p].push [i,j]if i*i+j*j==x*x}}}\nq[0].each {|i|q[1].each{|j|exit !puts('YES',0,0,i[0],i[1],j[0],-j[1])if i[0]*j[0]==i[1]*j[1]&&i[0]!=j[0]}}\nputs'NO'"}, {"source_code": "a,b = gets.split.map &:to_i\nas = a*a\nbs = b*b\n\nav = []\n1.upto(a-1).each do |x|\n break if as <= x*x\n y = Math.sqrt(as - x*x).round\n #print [x,y]\n if as == x*x + y*y\n av << [x,y] \n end\nend\n\nbv = []\n1.upto(b-1).each do |x|\n break if bs <= x*x\n y = Math.sqrt(bs - x*x).round\n bv << [x,y] if bs == x*x + y*y\nend\n\n#print av\n#print bv\n\nav.each do |as|\n bv.each do |bs|\n x1,y1 = *as\n x2,y2 = *bs\n if x1*x2 == y1*y2 && (x1 != -x2) && (y1 != y2)\n puts \"YES\"\n print \"0 0\\n#{x1} #{y1}\\n-#{x2} #{y2}\\n\"\n exit\n end\n end\nend\n\nputs \"NO\"\n"}, {"source_code": "a,b=gets.split.map &:to_i\nq1,q2=[],[]\ndef cal(x)\n q=[]\n (1..x-1).each{|i| (1..x-1).each{ |j| q.push [i,j] if i*i+j*j==x*x}}\n return q\nend\nq1=cal(a)\nq2=cal(b)\nq1.each do |i|\n q2.each do |j|\n if j[0]*i[0]-j[1]*i[1]==0&&j[0]!=i[0]\n j[1]=-j[1]\n puts 'YES','0 0',i.join(' '),j.join(' ')\n exit\n end\n end\nend\nputs 'NO'"}, {"source_code": "a,b=gets.split.map &:to_i\nq1,q2=[],[]\n(1..a-1).each do |i|\n (1..a-1).each do |j|\n if i*i+j*j==a*a\n q1.push [i,j]\n end\n end\nend\n(1..b-1).each do |i|\n (-(b-1)..-1).each do |j|\n if i*i+j*j==b*b\n q2.push [i,j]\n end\n end\nend\nq1.each do |i|\n q2.each do |j|\n if j[0]*i[0]+j[1]*i[1]==0&&j[0]!=i[0]\n puts 'YES'\n puts '0 0'\n puts i.join' '\n puts j.join' '\n exit\n end\n end\nend\nputs 'NO'"}, {"source_code": "$q=[]\n$q[0],$q[1]=[],[]\ndef cal(x,p)\n (1..x-1).each{|i| (1..x-1).each{|j| $q[p].push [i,j]if i*i+j*j==x*x}}\nend\ngets.split.map(&:to_i).each_with_index{|i,id|cal(i,id)}\n$q[0].each {|i|$q[1].each{|j|exit(!puts('YES',0,0,i[0],i[1],j[0],-j[1]))if i[0]*j[0]==i[1]*j[1]&&i[0]!=j[0]}}\nputs 'NO'"}, {"source_code": "$q=[[],[]]\ndef cal(x,p)\n (1..x).each{|i|(1..x).each{|j|$q[p].push [i,j]if i*i+j*j==x*x}}\nend\ngets.split.map(&:to_i).each_with_index{|i,id|cal(i,id)}\n$q[0].each {|i|$q[1].each{|j|exit !puts('YES',0,0,i[0],i[1],j[0],-j[1])if i[0]*j[0]==i[1]*j[1]&&i[0]!=j[0]}}\nputs'NO'"}, {"source_code": "q=[[],[]]\ngets.split.map(&:to_i).each_with_index{|x,p| (1..x).each{|i|(1..x).each{|j|q[p].push [i,j]if i*i+j*j==x*x}}}\nq[0].each {|i|q[1].each{|j|exit !puts('YES',0,0,i[0],i[1],j[0],-j[1])if i[0]*j[0]==i[1]*j[1]&&i[0]!=j[0]}}\nputs'NO'"}, {"source_code": "$q=[[],[]]\ngets.split.map(&:to_i).each_with_index{|x,p| (1..x).each{|i|(1..x).each{|j|$q[p].push [i,j]if i*i+j*j==x*x}}}\n$q[0].each {|i|$q[1].each{|j|exit !puts('YES',0,0,i[0],i[1],j[0],-j[1])if i[0]*j[0]==i[1]*j[1]&&i[0]!=j[0]}}\nputs'NO'"}, {"source_code": "f = STDIN.readline.split.map { |x| x.to_i }\np = Array.new(2) { [] }\n0.upto(1) do |i|\n\ta = f[i]-1\n\tb = 0\n\ta.downto(1) do |a|\n\t\tb += 1 while (b+1)**2 + a**2 <= f[i]**2\n\t\tif a**2 + b**2 == f[i]**2 ;\n\t\t\tp[i] << ( [a,b])\n\t\tend\n\tend\nend\n\n# print p\n\np[0].each do |a|\n\tp[1].each do |b|\n\t\tif a[0]*b[1] == a[1]*b[0] && a[1] != b[0] ;\n\t\t\tprint \"YES\\n\"\n\t\t\tprint \"0 0\\n\"\n\t\t\tprint \"#{a[0]} #{a[1]}\\n\"\n\t\t\tprint \"#{-b[1]} #{b[0]}\\n\"\n\t\t\texit\n\t\tend\n\tend\nend\nprint \"NO\\n\""}, {"source_code": "a,b = gets.split.map(&:to_i)\nans = (1...a).map { |dx|\n dy = Math.sqrt(a * a - dx * dx).to_i\n next if dx * dx + dy * dy != a * a\n dx2 = dy * b / a;\n dy2 = dx * b / a;\n next if dx2 * dx2 + dy2 * dy2 != b * b || dy == dy2\n \n [dx, dy, -dx2, dy2]\n}.select { |d|\n d != nil\n}.first\n\nunless ans\n puts \"NO\"\nelse\n puts \"YES\"\n puts \"0 0\"\n puts ans[0..1].join(\" \")\n puts ans[2..3].join(\" \")\nend\n"}, {"source_code": "def pita(a)\n ps = []\n x = 1\n while x * x < a * a\n y = Math.sqrt(a * a - x * x).to_i\n break if y < x\n ps << [x, y] if x * x + y * y == a * a\n x += 1\n end\n ps\nend\n\na, b = gets.split.map(&:to_i)\n# p pita(a)\n# p pita(b)\n# exit\n\n\ndef check(p, q)\n px, py = p\n qx, qy = q\n if px * qx + py * qy == 0 && px != qx && py != qy\n puts :YES\n puts \"0 0\"\n puts \"#{px} #{py}\"\n puts \"#{qx} #{qy}\"\n true\n else\n false\n end\nend\n\nok = false\n\npita(a).each do |px, py|\n pita(b).each do |qx, qy|\n\n if check([px, py], [-qx, qy])\n ok = true\n break\n end\n if check([px, py], [qx, -qy])\n ok = true\n break\n end\n if check([px, py], [-qy, qx])\n ok = true\n break\n end\n if check([px, py], [qy, -qx])\n ok = true\n break\n end\n\n break if ok\n end\n break if ok\nend\n\nif !ok\n puts :NO\nend\n"}], "negative_code": [{"source_code": "x, y = gets.split.map {|x| Integer x}\np1 = 1.0\np2 = 1000.0\nwhile p2 - p1 > 1\n if p1 ** 2 + p2 ** 2 > x ** 2\n p2 -= 1\n elsif p1 ** 2 + p2 ** 2 < x ** 2\n p1 += 1\n else\n break\n end\nend\nif p1 ** 2 + p2 ** 2 != x ** 2\n puts 'NO'\n exit\nend\narc = Math.acos(p1 / x)\narc2 = Math::PI / 2 - arc\np3 = y * Math.cos(arc2)\nif (p3.round - p3).abs > 10 ** -8\n puts 'NO'\n exit\nend\np4 = Math.sqrt (y ** 2 - p3.round ** 2)\nif (p4 - p4.round).abs > 10 ** -8\n puts 'NO'\n exit\nend\nif p2.round == p4.round\n puts 'YES'\n puts (-p2.to_i).to_s + ' ' + p1.to_i.to_s\n puts '0 0'\n puts p4.round.to_s + ' ' + p3.round.to_s\n exit\nend\nputs 'YES'\nputs (-p1.to_i).to_s + ' ' + (p2.to_i).to_s\nputs '0 0'\nputs p3.round.to_s + ' ' + p4.round.to_s\n"}, {"source_code": "x, y = gets.split.map {|x| Integer x}\np1 = 1.0\np2 = 1000.0\nwhile p2 - p1 > 1\n if p1 ** 2 + p2 ** 2 > x ** 2\n p2 -= 1\n elsif p1 ** 2 + p2 ** 2 < x ** 2\n p1 += 1\n else\n break\n end\nend\nif p1 ** 2 + p2 ** 2 != x ** 2\n puts 'NO'\n exit\nend\narc = Math.acos(p1 / x)\narc2 = Math::PI / 2 - arc\np3 = y * Math.cos(arc2)\nif (p3.round - p3).abs > 10 ** -9\n puts 'NO'\n exit\nend\np4 = Math.sqrt (y ** 2 - p3.round ** 2)\nif (p4 - p4.round).abs > 10 ** 9\n puts 'NO'\n exit\nend\nif p3.round == p4.round\n puts 'NO'\n exit\nend\nputs 'YES'\nputs (-p1.to_i).to_s + ' ' + (p2.to_i).to_s\nputs '0 0'\nputs p3.round.to_s + ' ' + p4.round.to_s\n"}, {"source_code": "x, y = gets.split.map { |x| Integer x }\np1 = 1.0\np2 = 1000.0\nwhile p2 - p1 > 1\n if p1 ** 2 + p2 ** 2 > x ** 2\n p2 -= 1\n elsif p1 ** 2 + p2 ** 2 < x ** 2\n p1 += 1\n else\n arc = Math.acos(p1 / x)\n arc2 = Math::PI / 2 - arc\n p3 = y * Math.cos(arc2)\n p4 = Math.sqrt (y ** 2 - p3.round ** 2)\n if (p3.round - p3).abs <= 10 ** -9 && (p4 - p4.round).abs <= 10 ** -9\n if p2.round == p4.round\n puts 'YES'\n puts (-p2.to_i).to_s + ' ' + p1.to_i.to_s\n puts '0 0'\n puts p4.round.to_s + ' ' + p3.round.to_s\n exit\n end\n puts 'YES'\n puts (-p1.to_i).to_s + ' ' + (p2.to_i).to_s\n puts '0 0'\n puts p3.round.to_s + ' ' + p4.round.to_s\n exit\n end\n p1 += 1\n p2 -= 1\n end\nend\nputs 'NO'\n\n"}, {"source_code": "x, y = gets.split.map { |x| Integer x }\np1 = 1.0\np2 = 1000.0\nif x % 5 != 0 || y % 5 != 0\n puts 'NO'\n exit\nend\nfor i in 1..2000\n if p1 ** 2 + p2 ** 2 > x ** 2\n p2 -= 1\n elsif p1 ** 2 + p2 ** 2 < x ** 2\n p1 += 1\n else\n arc = Math.acos(p1 / x)\n arc2 = Math::PI / 2 - arc\n p3 = y * Math.cos(arc2)\n p4 = Math.sqrt (y ** 2 - p3.round ** 2)\n if (p3.round - p3).abs <= 10 ** -9 && (p4 - p4.round).abs <= 10 ** -9\n if p2.round == p4.round\n puts 'YES'\n puts (-p2.to_i).to_s + ' ' + p1.to_i.to_s\n puts '0 0'\n puts p4.round.to_s + ' ' + p3.round.to_s\n exit\n end\n puts 'YES'\n puts (-p1.to_i).to_s + ' ' + (p2.to_i).to_s\n puts '0 0'\n puts p3.round.to_s + ' ' + p4.round.to_s\n exit\n end\n p1 += 1\n end\nend\nputs 'NO'\n\n"}, {"source_code": "x, y = gets.split.map {|x| Integer x}\np1 = 1.0\np2 = 1000.0\nwhile p2 - p1 > 1\n if p1 ** 2 + p2 ** 2 > x ** 2\n p2 -= 1\n elsif p1 ** 2 + p2 ** 2 < x ** 2\n p1 += 1\n else\n break\n end\nend\nif p1 ** 2 + p2 ** 2 != x ** 2\n puts 'NO'\n exit\nend\narc = Math.acos(p1 / x)\narc2 = Math::PI / 2 - arc\np3 = y * Math.cos(arc2)\nif (p3.round - p3).abs > 10 ** -9\n puts 'NO'\n exit\nend\np4 = Math.sqrt (y ** 2 - p3.round ** 2)\nif (p4 - p4.round).abs > 10 ** -9\n puts 'NO'\n exit\nend\nif p2.round == p4.round\n puts 'YES'\n puts (-p2.to_i).to_s + ' ' + p1.to_i.to_s\n puts '0 0'\n puts p4.round.to_s + ' ' + p3.round.to_s\n exit\nend\nputs 'YES'\nputs (-p1.to_i).to_s + ' ' + (p2.to_i).to_s\nputs '0 0'\nputs p3.round.to_s + ' ' + p4.round.to_s\n"}, {"source_code": "x, y = gets.split.map {|x| Integer x}\np1 = 1.0\np2 = 1000.0\nwhile p2 - p1 > 1\n if p1 ** 2 + p2 ** 2 > x ** 2\n p2 -= 1\n elsif p1 ** 2 + p2 ** 2 < x ** 2\n p1 += 1\n else\n break\n end\nend\nif p1 ** 2 + p2 ** 2 != x ** 2\n puts 'NO'\n exit\nend\narc = Math.acos(p1 / x)\narc2 = Math::PI / 2 - arc\np3 = y * Math.cos(arc2)\nif (p3.round - p3).abs > 10 ** -9\n puts 'NO'\n exit\nend\np4 = Math.sqrt (y ** 2 - p3.round ** 2)\nif (p4 - p4.round).abs > 10 ** 9\n puts 'NO'\n exit\nend\nif x == 5 && y == 5\n puts 'YES'\n puts '2 1'\n puts '5 5'\n puts '-2 4'\n exit\nend\nputs 'YES'\nputs (-p1.to_i).to_s + ' ' + (-p2.to_i).to_s\nputs '0 0'\nputs p3.round.to_s + ' ' + p4.round.to_s\n"}, {"source_code": "x, y = gets.split.map {|x| Integer x}\np1 = 1.0\np2 = 1000.0\nwhile p2 - p1 > 1\n if p1 ** 2 + p2 ** 2 > x ** 2\n p2 -= 1\n elsif p1 ** 2 + p2 ** 2 < x ** 2\n p1 += 1\n else\n break\n end\nend\nif p1 ** 2 + p2 ** 2 != x ** 2\n puts 'NO'\n exit\nend\narc = Math.acos(p1 / x)\narc2 = Math::PI / 2 - arc\np3 = y * Math.cos(arc2)\nif (p3.round - p3).abs > 10 ** -9\n puts 'NO'\n exit\nend\np4 = Math.sqrt (y ** 2 - p3.round ** 2)\nif (p4 - p4.round).abs > 10 ** 9\n puts 'NO'\n exit\nend\nputs 'YES'\nputs (-p1.to_i).to_s + ' ' + (-p2.to_i).to_s\nputs '0 0'\nputs p3.round.to_s + ' ' + p4.round.to_s\n"}, {"source_code": "x, y = gets.split.map { |x| Integer x }\np1 = 1.0\np2 = 1000.0\nif x == 1 && y == 1\n puts 'NO'\n exit\nend\nfor i in 1..2000\n if p1 ** 2 + p2 ** 2 > x ** 2\n p2 -= 1\n elsif p1 ** 2 + p2 ** 2 < x ** 2\n p1 += 1\n else\n arc = Math.acos(p1 / x)\n arc2 = Math::PI / 2 - arc\n p3 = y * Math.cos(arc2)\n p4 = Math.sqrt (y ** 2 - p3.round ** 2)\n if (p3.round - p3).abs <= 10 ** -9 && (p4 - p4.round).abs <= 10 ** -9\n if p2.round == p4.round\n puts 'YES'\n puts (-p2.to_i).to_s + ' ' + p1.to_i.to_s\n puts '0 0'\n puts p4.round.to_s + ' ' + p3.round.to_s\n exit\n end\n puts 'YES'\n puts (-p1.to_i).to_s + ' ' + (p2.to_i).to_s\n puts '0 0'\n puts p3.round.to_s + ' ' + p4.round.to_s\n exit\n end\n p1 += 1\n end\nend\nputs 'NO'\n\n"}, {"source_code": "x, y = gets.split.map { |x| Integer x }\np1 = 1.0\np2 = 1000.0\nfor i in 1..2000\n if p1 ** 2 + p2 ** 2 > x ** 2\n p2 -= 1\n elsif p1 ** 2 + p2 ** 2 < x ** 2\n p1 += 1\n else\n arc = Math.acos(p1 / x)\n arc2 = Math::PI / 2 - arc\n p3 = y * Math.cos(arc2)\n p4 = Math.sqrt (y ** 2 - p3.round ** 2)\n if (p3.round - p3).abs <= 10 ** -9 && (p4 - p4.round).abs <= 10 ** -9\n if p2.round == p4.round\n puts 'YES'\n puts (-p2.to_i).to_s + ' ' + p1.to_i.to_s\n puts '0 0'\n puts p4.round.to_s + ' ' + p3.round.to_s\n exit\n end\n puts 'YES'\n puts (-p1.to_i).to_s + ' ' + (p2.to_i).to_s\n puts '0 0'\n puts p3.round.to_s + ' ' + p4.round.to_s\n exit\n end\n p1 += 1\n end\nend\nputs 'NO'\n\n"}, {"source_code": "x, y = gets.split.map {|x| Integer x}\np1 = 1.0\np2 = 1000.0\nwhile p2 - p1 > 1\n if p1 ** 2 + p2 ** 2 > x ** 2\n p2 -= 1\n elsif p1 ** 2 + p2 ** 2 < x ** 2\n p1 += 1\n else\n break\n end\nend\nif p1 ** 2 + p2 ** 2 != x ** 2\n puts 'NO'\n exit\nend\narc = Math.acos(p1 / x)\narc2 = Math::PI / 2 - arc\np3 = y * Math.cos(arc2)\nif (p3.round - p3).abs > 10 ** -9\n puts 'NO'\n exit\nend\np4 = Math.sqrt (y ** 2 - p3.round ** 2)\nif (p4 - p4.round).abs > 10 ** 9\n puts 'NO'\n exit\nend\nif p2.round == p4.round\n puts 'YES'\n puts (-p2.to_i).to_s + ' ' + p1.to_i.to_s\n puts '0 0'\n puts p4.round.to_s + ' ' + p3.round.to_s\n exit\nend\nputs 'YES'\nputs (-p1.to_i).to_s + ' ' + (p2.to_i).to_s\nputs '0 0'\nputs p3.round.to_s + ' ' + p4.round.to_s\n"}, {"source_code": "x, y = gets.split.map {|x| Integer x}\np1 = 1.0\np2 = 1000.0\nwhile p2 - p1 > 1\n if p1 ** 2 + p2 ** 2 > x ** 2\n p2 -= 1\n elsif p1 ** 2 + p2 ** 2 < x ** 2\n p1 += 1\n else\n break\n end\nend\nif p1 ** 2 + p2 ** 2 != x ** 2\n puts 'NO'\n exit\nend\narc = Math.acos(p1 / x)\narc2 = Math::PI / 2 - arc\np3 = y * Math.cos(arc2)\nif (p3.round - p3).abs > 10 ** -9\n puts 'NO'\n exit\nend\np4 = Math.sqrt (y ** 2 - p3.round ** 2)\nif (p4 - p4.round).abs > 10 ** 9\n puts 'NO'\n exit\nend\nif p2.round == p4.round\n puts 'YES'\n puts (-p2.to_i).to_s + ' ' + p1.to_i.to_s\n puts p4.round.to_s + ' ' + p3.round.to_s\n exit\nend\nputs 'YES'\nputs (-p1.to_i).to_s + ' ' + (p2.to_i).to_s\nputs '0 0'\nputs p3.round.to_s + ' ' + p4.round.to_s\n"}, {"source_code": "x, y = gets.split.map {|x| Integer x}\np1 = 1.0\np2 = 1000.0\nwhile p2 - p1 > 1\n if p1 ** 2 + p2 ** 2 > x ** 2\n p2 -= 1\n elsif p1 ** 2 + p2 ** 2 < x ** 2\n p1 += 1\n else\n break\n end\nend\nif p1 ** 2 + p2 ** 2 != x ** 2\n puts 'NO'\n exit\nend\narc = Math.acos(p1 / x)\narc2 = Math::PI / 2 - arc\np3 = y * Math.cos(arc2)\nif (p3.round - p3).abs > 10 ** -9\n puts 'NO'\n exit\nend\np4 = Math.sqrt (y ** 2 - p3.round ** 2)\nif (p4 - p4.round).abs > 10 ** 9\n puts 'NO'\n exit\nend\nputs 'YES'\nputs (-p1).to_s + ' ' + (-p2).to_s\nputs '0 0'\nputs p3.round.to_s + ' ' + p4.round.to_s\n"}, {"source_code": "x, y = gets.split.map {|x| Integer x}\np1 = 1.0\np2 = 1000.0\nwhile p2 - p1 > 1\n if p1 ** 2 + p2 ** 2 > x ** 2\n p2 -= 1\n elsif p1 ** 2 + p2 ** 2 < x ** 2\n p1 += 1\n else\n break\n end\nend\nif p1 ** 2 + p2 ** 2 != x ** 2\n puts 'NO'\n exit\nend\narc = Math.acos(p1 / x)\narc2 = Math::PI / 2 - arc\np3 = y * Math.cos(arc2)\nif (p3.round - p3).abs > 10 ** -9\n puts 'NO'\n exit\nend\np4 = Math.sqrt (y ** 2 - p3.round ** 2)\nif (p4 - p4.round).abs > 10 ** 9\n puts 'NO'\n exit\nend\nputs 'YES'\nputs (-p1.to_i).to_s + ' ' + (p2.to_i).to_s\nputs '0 0'\nputs p3.round.to_s + ' ' + p4.round.to_s\n"}, {"source_code": "a,b = gets.split.map &:to_i\nas = a*a\nbs = b*b\n\nav = []\n1.upto(a-1).each do |x|\n break if as <= x*x\n y = Math.sqrt(as - x*x).round\n #print [x,y]\n if as == x*x + y*y\n av << [x,y] \n end\nend\n\nbv = []\n1.upto(b-1).each do |x|\n break if bs <= x*x\n y = Math.sqrt(bs - x*x).round\n bv << [x,y] if bs == x*x + y*y\nend\n\n#print av\n#print bv\n\nav.each do |as|\n bv.each do |bs|\n x1,y1 = *as\n x2,y2 = *bs\n if x1*x2 == y1*y2\n puts \"YES\"\n print \"0 0\\n#{x1} #{y1}\\n-#{x2} #{y2}\\n\"\n exit\n end\n end\nend\n\nputs \"NO\"\n"}, {"source_code": "a,b = gets.split.map &:to_i\nas = a*a\nbs = b*b\n\nav = []\n1.upto(Math.sqrt(a).ceil).each do |x|\n break if as <= x*x\n y = Math.sqrt(as - x*x).round\n #print [x,y]\n if as == x*x + y*y\n av << [x,y] \n end\nend\n\nbv = []\n1.upto(b-1).each do |x|\n break if bs <= x*x\n y = Math.sqrt(bs - x*x).round\n bv << [x,y] if bs == x*x + y*y\nend\n\n#print av\n#print bv\n\nav.each do |as|\n bv.each do |bs|\n x1,y1 = *as\n x2,y2 = *bs\n if x1*x2 == y1*y2\n puts \"YES\"\n print \"0 0\\n#{x1} #{y1}\\n-#{x2} #{y2}\\n\"\n exit\n end\n end\nend\n\nputs \"NO\"\n"}, {"source_code": "$q=[]\n$q[0],$q[1]=[],[]\ndef cal(x,p)\n (1..x-1).each{|i| (1..x-1).each{|j| $q[p].push [i,j]if i*i+j*j==x*x}}\nend\ngets.split.map(&:to_i).each_with_index{|i,id|cal(i,id)}\n$q[0].each {|i|$q[1].each{|j|exit(!puts('YES',0,0,i[0],i[1],-j[0],j[1]))if i[0]*j[0]==i[1]*j[1]&&i[0]!=j[0]}}\nputs 'NO'"}, {"source_code": "a,b=gets.split.map &:to_i\nq1,q2=[],[]\n(1..a-1).each do |i|\n (1..a-1).each do |j|\n if i*i+j*j==a*a\n q1.push [i,j]\n end\n end\nend\n(1..b-1).each do |i|\n (-(b-1)..-1).each do |j|\n if i*i+j*j==b*b\n q2.push [i,j]\n end\n end\nend\nq1.each do |i|\n q2.each do |j|\n if j[0]*i[0]+j[1]*i[1]==0\n puts 'YES'\n puts '0 0'\n puts i.join' '\n puts j.join' '\n exit\n end\n end\nend\nputs 'NO'"}, {"source_code": "a,b=gets.split.map(&:to_i)\n$q=[]\n$q[0]=$q[1]=[]\ndef cal(x,p)\n (1..x-1).each{|i| (1..x-1).each{ |j| $q[p].push [i,j] if i*i+j*j==x*x}}\nend\ncal(a,0)\ncal(b,1)\n$q[0].each {|i|$q[1].each{|j| exit(!puts('YES',0,0,i[0],i[1],-j[0],j[1])) if i[0]*j[0]==i[1]*j[1]}}\nputs 'NO'"}, {"source_code": "f = STDIN.readline.split.map { |x| x.to_i }\np = Array.new(2) { [] }\n0.upto(1) do |i|\n\ta = f[i]-1\n\tb = 0\n\ta.downto(1) do |a|\n\t\tb += 1 while (b+1)**2 + a**2 <= f[i]**2\n\t\tif a**2 + b**2 == f[i]**2 ;\n\t\t\tp[i] << ( [a,b])\n\t\tend\n\tend\nend\n\n# print p\n\np[0].each do |a|\n\tp[1].each do |b|\n\t\tif a[0]*b[1] == a[1]*b[0] ;\n\t\t\tprint \"YES\\n\"\n\t\t\tprint \"0 0\\n\"\n\t\t\tprint \"#{a[0]} #{a[1]}\\n\"\n\t\t\tprint \"#{-b[1]} #{b[0]}\\n\"\n\t\t\texit\n\t\tend\n\tend\nend\nprint \"NO\\n\""}, {"source_code": "f = STDIN.readline.split.map { |x| x.to_i }\np = Array.new(2) { [] }\n0.upto(1) do |i|\n\ta = f[i]-1\n\tb = 0\n\ta.downto(1) do |a|\n\t\tb += 1 while (b+1)**2 + a**2 <= f[i]**2\n\t\tif a**2 + b**2 == f[i]**2 ;\n\t\t\tp[i] << ( [a,b])\n\t\tend\n\tend\nend\n\n# print p\n\np[0].each do |a|\n\tp[1].each do |b|\n\t\tif a[0]*b[1] == a[1]*b[0] && a[1] != b[1] ;\n\t\t\tprint \"YES\\n\"\n\t\t\tprint \"0 0\\n\"\n\t\t\tprint \"#{a[0]} #{a[1]}\\n\"\n\t\t\tprint \"#{-b[1]} #{b[0]}\\n\"\n\t\t\texit\n\t\tend\n\tend\nend\nprint \"NO\\n\""}, {"source_code": "def pita(a)\n ps = []\n (1...a * a).each do |x|\n break if a * a - x * x <= 0\n y = Math.sqrt(a * a - x * x).to_i\n break if y < x\n if x * x + y * y == a * a\n ps << [x, y]\n end\n end\n # p ps\n ps\nend\n\na, b = gets.split.map(&:to_i)\n# p pita(a)\n# p pita(b)\n# exit\n\nok = false\n\npita(a).each do |px, py|\n pita(b).each do |qx, qy|\n\n\n if px * qx - py * qy == 0\n ok = true\n puts \"0 0\"\n puts \"#{px} #{py}\"\n puts \"#{-qx} #{qy}\"\n break\n end\n\n if px * qy - py * qx == 0\n puts \"0 0\"\n puts \"#{px} #{py}\"\n puts \"#{-qy} #{qx}\"\n ok = true\n break\n end\n\n break if ok\n end\n break if ok\nend\n\nif !ok\n puts :NO\nend\n"}, {"source_code": "def pita(a)\n ps = []\n x = 1\n while x * x < a * a\n y = Math.sqrt(a * a - x * x).to_i\n break if y < x\n ps << [x, y] if x * x + y * y == a * a\n x += 1\n end\n ps\nend\n\na, b = gets.split.map(&:to_i)\n# p pita(a)\n# p pita(b)\n# exit\n\nok = false\n\npita(a).each do |px, py|\n pita(b).each do |qx, qy|\n\n if px * qx - py * qy == 0\n ok = true\n puts :YES\n puts \"0 0\"\n puts \"#{px} #{py}\"\n puts \"#{-qx} #{qy}\"\n break\n end\n\n if px * qy - py * qx == 0\n puts :YES\n puts \"0 0\"\n puts \"#{px} #{py}\"\n puts \"#{-qy} #{qx}\"\n ok = true\n break\n end\n\n break if ok\n end\n break if ok\nend\n\nif !ok\n puts :NO\nend\n"}], "src_uid": "a949ccae523731f601108d4fa919c112"} {"nl": {"description": "Vasya the Great Magician and Conjurer loves all kinds of miracles and wizardry. In one wave of a magic wand he can turn an object into something else. But, as you all know, there is no better magic in the Universe than the magic of numbers. That's why Vasya adores math and spends a lot of time turning some numbers into some other ones.This morning he has n cards with integers lined up in front of him. Each integer is not less than 1, but not greater than l. When Vasya waves his magic wand, two rightmost cards vanish from the line and a new card magically appears in their place. It contains the difference between the left and the right numbers on the two vanished cards. Vasya was very interested to know what would happen next, and so he waved with his magic wand on and on, until the table had a single card left.Suppose that Vasya originally had the following cards: 4, 1, 1, 3 (listed from left to right). Then after the first wave the line would be: 4, 1, -2, and after the second one: 4, 3, and after the third one the table would have a single card with number 1.Please note that in spite of the fact that initially all the numbers on the cards were not less than 1 and not greater than l, the numbers on the appearing cards can be anything, no restrictions are imposed on them.It is now evening. Vasya is very tired and wants to return everything back, but does not remember which cards he had in the morning. He only remembers that there were n cards, they contained integers from 1 to l, and after all magical actions he was left with a single card containing number d.Help Vasya to recover the initial set of cards with numbers.", "input_spec": "The single line contains three space-separated integers: n (2 ≤ n ≤ 100) — the initial number of cards on the table, d (|d| ≤ 104) — the number on the card that was left on the table after all the magical actions, and l (1 ≤ l ≤ 100) — the limits for the initial integers.", "output_spec": "If Vasya is mistaken, that is, if there doesn't exist a set that meets the requirements given in the statement, then print a single number -1, otherwise print the sought set containing n integers from 1 to l. Separate the integers by spaces. Print the integers in the order, in which they were written on the cards from left to right. If there are several suitable sets of numbers, you can print any of them.", "sample_inputs": ["3 3 2", "5 -4 3", "5 -4 4"], "sample_outputs": ["2 1 2", "-1", "2 4 1 4 1"], "notes": null}, "positive_code": [{"source_code": "a,b,c = gets.chomp.split(/ /).map!{|x| x.to_i}\npnum = a / 2\nmnum = pnum\nif a % 2 == 1\n pnum += 1\nend\nar = []\nfor i in 0..a-1\n ar[i] = 1\nend\nipb = false\nif b > 0\n wksum = b + mnum - pnum\n i = 1\n while wksum > 0\n ar[i - 1] += 1\n if ar[i-1] > c\n ipb = true\n break\n end\n wksum -= 1\n i += 1\n i += 1 if i % 2 == 0\n i = 1 if (i > a)\n end\nelse\n wksum = -(b - pnum) - mnum\n i = 2\n while wksum > 0\n ar[i - 1] += 1\n if ar[i-1] > c\n ipb = true\n break\n end\n wksum -= 1\n i += 1\n i += 1 if i % 2 == 1\n i = 2 if (i > a)\n end\nend\nif ipb\n puts \"-1\"\nelse\n puts ar.join(\" \")\nend\n"}, {"source_code": "n, d, m = gets.split.map(&:to_i)\ny = n / 2\nx = n - y\nd -= x - y\na = Array.new(n) do |i|\n t = i.odd? == (d > 0) ? 0 : [d.abs, m - 1].min\n if d > 0\n d -= t\n else\n d += t\n end\n t + 1\nend\nputs d == 0 ? a * ' ' : -1\n"}, {"source_code": "#!/usr/bin/ruby\n\nn, d, lim = STDIN.readline.split.collect {|s| s.to_i }\narr = n.times.map {|i| i%2 == 0 ? 1 : lim }\ny = n / 2\nx = n - y\nmin = x - y*lim\nmax = x*lim - y\nif d < min or d > max\n\tputs -1\nelse\n\tneed = d - min\n\tn.times do |i|\n\t\tdelta = [need, lim-1].min\n\t\tif i%2 == 0\n\t\t\tarr[i] += delta\n\t\telse\n\t\t\tarr[i] -= delta\n\t\tend\n\t\tneed -= delta\n\t\tbreak if need == 0\n\tend\n\tputs arr.join(' ')\nend\n"}, {"source_code": "n, d, l = gets.split.map(&:to_i)\n\nxl = (n+1)/2\nyl = n/2\nsum = (xl-yl)*l - d\n\nx = Array.new(xl, l)\ny = Array.new(yl, l)\n\nwhile sum != 0\n if 0 < sum\n if x.count(1) == xl\n x = [-1]; y = []\n break\n end\n\n x[x.index{|v| 1 < v }] -= 1\n end\n\n if sum < 0\n if y.count(1) == yl\n x = [-1]; y = []\n break\n end\n y[y.index{|v| 1 < v }] -= 1 if sum < 0\n end\n\n sum = sum / sum.abs * (sum.abs - 1)\nend\n\nputs x.zip(y).flatten.compact.join(\" \")\n"}, {"source_code": "#!/usr/bin/ruby\ninclude Math\nn,d,l = gets.split.map(&:to_i)\n#puts [n,d,l].join ' '\nmins = [1]\nmaxs = [l]\n(n-1).times do |i|\n maxs[i+1] = l-mins[i]\n mins[i+1] = 1-maxs[i]\nend\n#p maxs\n#p mins\ndef solve (d,l,mx,mn)\n #puts 'err' if x>l\n if(d>0) then return [l,d+mx].min\n else return [1,d+mn].max\n end\nend\nif d < mins.last || d > maxs.last\n puts -1\nelsif l==1\n puts Array.new(n,1).join ' '\nelse\n ans = []\n (n-1).times do |i|\n ans << solve(d,l,maxs[-i-2],mins[-i-2])\n d = ans.last-d\n# p [ans.last,d]\n end\n ans << d\n puts ans.join ' '\nend\n"}, {"source_code": "#!/usr/bin/env ruby\n\ninput = gets.chomp.split(' ').map { |e| e.to_i }\nn = input[0]\nd = input[1]\nl = input[2]\nresAns = Array.new(n, 1)\n\nd -= n % 2\n\neven = true\nfor i in (0..(n-1)) do\n\tif even && d > 0\n\t\tresAns[i] += [l - 1, d].min\n\t\td -= (resAns[i] - 1)\n\tend\n\tif !even && d < 0\n\t\tresAns[i] += [l - 1, -d].min\n\t\td += (resAns[i] - 1)\n\tend\n\teven = !even\nend\n\nif d != 0 \n\tputs -1\n\texit\nend\n\nputs resAns.join(' ')"}], "negative_code": [{"source_code": "n, d, l = gets.split.map(&:to_i)\n\nxl, yl = (n+1)/2, n/2\nsum = (xl-yl)*l-d\nx, y = Array.new(xl, l), Array.new(yl, l)\n\n(0 < sum ? x : y).map! do |v|\n v = [1, v-sum.abs].max\n sum = (sum/sum.abs)*(sum.abs-[l-1, sum.abs].min) if sum != 0\n v\nend\n\nx, y = [-1], [] if 0 < sum\n\nputs x.zip(y).flatten.compact.join(\" \")"}, {"source_code": "n, d, l = gets.split.map(&:to_i)\n\nxl, yl = (n+1)/2, n/2\nsum = (xl-yl)*l-d\nx, y = Array.new(xl, l), Array.new(yl, l)\n\n(0 < sum ? x : y).map! do |v|\n v = [1, v-sum.abs].max\n sum = (sum/sum.abs)*(sum.abs-[l-1, sum.abs].min) if sum != 0\n v\nend\n\nputs x.zip(y).flatten.compact.join(\" \")"}, {"source_code": "#!/usr/bin/ruby\ninclude Math\nn,d,l = gets.split.map(&:to_i)\n#puts [n,d,l].join ' '\nmins = [1]\nmaxs = [l]\n(n-1).times do |i|\n maxs[i+1] = l-mins[i]\n mins[i+1] = 1-maxs[i]\nend\ndef solve (d,l,mn,mx)\n #puts 'err' if x>l\n if(d>0) then return [l,d+mx].min\n else return [1,d+mn].max\n end\nend\nif d < mins.last || d > maxs.last\n puts -1\nelsif l==1\n puts Array.new(n,1).join ' '\nelse\n ans = []\n (n-1).times do |i|\n ans << solve(d,l,maxs[-i-2],mins[-i-2])\n d = ans.last-d\n# p [ans.last,d]\n end\n ans << d\n puts ans.join ' '\nend\n"}, {"source_code": "#!/usr/bin/env ruby\n\ninput = gets.chomp.split(' ').map { |e| e.to_i }\nn = input[0]\nd = input[1]\nl = input[2]\nresAns = Array.new(n, 1)\n\nif d == 0 && n % 2 == 1\n\tputs -1\n\texit 0\nend\t\n\nd -= n % 2\n\neven = true\nfor i in (0..(n-1)) do\n\tif even && d > 0\n\t\tresAns[i] += [l - 1, d].min\n\t\td -= (resAns[i] - 1)\n\tend\n\tif !even && d < 0\n\t\tresAns[i] += [l - 1, -d].min\n\t\td += (resAns[i] - 1)\n\tend\n\teven = !even\nend\n\nif d != 0 \n\tputs -1\n\texit\nend\n\nputs resAns.join(' ')"}], "src_uid": "a20d59a0db07cbb5692f01d28e41a3a1"} {"nl": {"description": "Polycarp bought a new expensive painting and decided to show it to his $$$n$$$ friends. He hung it in his room. $$$n$$$ of his friends entered and exited there one by one. At one moment there was no more than one person in the room. In other words, the first friend entered and left first, then the second, and so on.It is known that at the beginning (before visiting friends) a picture hung in the room. At the end (after the $$$n$$$-th friend) it turned out that it disappeared. At what exact moment it disappeared — there is no information.Polycarp asked his friends one by one. He asked each one if there was a picture when he entered the room. Each friend answered one of three: no (response encoded with 0); yes (response encoded as 1); can't remember (response is encoded with ?). Everyone except the thief either doesn't remember or told the truth. The thief can say anything (any of the three options).Polycarp cannot understand who the thief is. He asks you to find out the number of those who can be considered a thief according to the answers.", "input_spec": "The first number $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases in the test. The following is a description of test cases. The first line of each test case contains one string $$$s$$$ (length does not exceed $$$2 \\cdot 10^5$$$) — a description of the friends' answers, where $$$s_i$$$ indicates the answer of the $$$i$$$-th friend. Each character in the string is either 0 or 1 or ?. The given regularity is described in the actual situation. In particular, on the basis of answers, at least one friend can be suspected of stealing a painting. It is guaranteed that the sum of string lengths over the entire input data set does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "Output one positive (strictly more zero) number – the number of people who could steal the picture based on the data shown.", "sample_inputs": ["8\n0\n1\n1110000\n?????\n1?1??0?0\n0?0???\n??11\n??0??"], "sample_outputs": ["1\n1\n2\n5\n4\n1\n1\n3"], "notes": "NoteIn the first case, the answer is $$$1$$$ since we had exactly $$$1$$$ friend.The second case is similar to the first.In the third case, the suspects are the third and fourth friends (we count from one). It can be shown that no one else could be the thief.In the fourth case, we know absolutely nothing, so we suspect everyone."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n s = gets.chomp\r\n a, b = [s.rindex(\"1\") || 0, s.index(\"0\") || s.size - 1]\r\n p (b - a + 1)\r\nend\r\n"}], "negative_code": [], "src_uid": "0c9f2301629726870a0ab57299773fd6"} {"nl": {"description": "DZY loves chessboard, and he enjoys playing with it.He has a chessboard of n rows and m columns. Some cells of the chessboard are bad, others are good. For every good cell, DZY wants to put a chessman on it. Each chessman is either white or black. After putting all chessmen, DZY wants that no two chessmen with the same color are on two adjacent cells. Two cells are adjacent if and only if they share a common edge.You task is to find any suitable placement of chessmen on the given chessboard.", "input_spec": "The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 100). Each of the next n lines contains a string of m characters: the j-th character of the i-th string is either \".\" or \"-\". A \".\" means that the corresponding cell (in the i-th row and the j-th column) is good, while a \"-\" means it is bad.", "output_spec": "Output must contain n lines, each line must contain a string of m characters. The j-th character of the i-th string should be either \"W\", \"B\" or \"-\". Character \"W\" means the chessman on the cell is white, \"B\" means it is black, \"-\" means the cell is a bad cell. If multiple answers exist, print any of them. It is guaranteed that at least one answer exists.", "sample_inputs": ["1 1\n.", "2 2\n..\n..", "3 3\n.-.\n---\n--."], "sample_outputs": ["B", "BW\nWB", "B-B\n---\n--B"], "notes": "NoteIn the first sample, DZY puts a single black chessman. Of course putting a white one is also OK.In the second sample, all 4 cells are good. No two same chessmen share an edge in the sample output.In the third sample, no good cells are adjacent. So you can just put 3 chessmen, no matter what their colors are."}, "positive_code": [{"source_code": "n, m = gets.split(' ').map(&:to_i)\n(0...n).each do |x|\n ar = gets.chomp\n (0...m).each do |y|\n if ( ar[y] == '-' )\n print '-'\n elsif ( (x+y)%2==0 )\n print 'B'\n else\n print 'W'\n end\n end\n print \"\\n\"\nend"}, {"source_code": "r, c = gets.split.map(&:to_i)\nchessboard=[]\nfor i in 1..r do\n\tx = gets\n\tchessboard << x\nend\nfor i in 0...r do\n\tfor j in 0...c do\n\t if chessboard[i][j]=='-' then\n\t \tnext\n\t elsif ((i+j)%2==0)\n\t \tchessboard[i][j]='B'\n\t\telse\n\t\t\tchessboard[i][j]='W'\n\t\tend\n\tend\nend\nputs chessboard"}, {"source_code": "n,m=gets.split.map(&:to_i)\na=[]\nn.times do |i|\n\tj=0\n\tfor x in gets.strip.split('')\n\t\tif x=='-'\n\t\t\ta='-'\n\t\telsif (i+j)&1!=0\n\t\t\ta='B';\n\t\telse\n\t\t\ta='W'\n\t\tend\n\t\tj+=1\n\t\tprint a\n\tend\n\tputs\nend"}, {"source_code": "n,m=gets.split.map(&:to_i)\nn.times{|i|\n\ts=gets.chop\n\t(0..s.size-1).each{|j|\n\t\tnext if s[j] != '.'\n\t\ts[j]=( (i+j)%2 == 1 ? 'B' : 'W' )\n\t}\n\tputs s\n}"}, {"source_code": "n, m = gets.chomp.split(/ /).map(&:to_i)\nboard = []\nn.times do\n line = gets.chomp\n line = line.gsub '.', 'B'\n board << line\nend\nn.times do |i|\n m.times do |j|\n if (i + j).odd? && board[i][j] != '-'\n board[i][j] = 'W'\n end\n end\nend\nboard.each do |line|\n puts line\nend\n"}, {"source_code": "n,m=gets.split.map{|e| e.to_i}\nn.times{|i|\n\ts=gets.chomp\n\tm.times{|j|\n\t\tif s[j]==\"-\" then\n\t\t\tprint \"-\"\n\t\telse\n\t\t\tprint \"BW\"[(i+j)%2]\n\t\tend\n\t}\n\tprint \"\\n\"\n}\n"}, {"source_code": "# coding: utf-8\n\nn, m = gets.split.map(&:to_i) # m cols, n rows\ntoggle = true\nmap = { true => 'B', false => 'W'}\nbuffer = []\nn.times {\n gets.chomp.split(//).each { |c|\n if c == \".\"\n buffer << map[toggle]\n else\n buffer << c\n end\n toggle = !toggle\n }\n buffer << \"\\n\"\n\n if m.even?\n toggle = !toggle # prevent cyclic\n end\n}\nbuffer.pop # remove last \\n\nputs buffer.join('')\n"}, {"source_code": "numbers = gets.split(' ')\nn = numbers[0].to_i\nm = numbers[1].to_i\n\nboard = []\nn.times do\n row = gets.split('')\n row.pop\n board.push row\nend\n\nboard.each_index do |i|\n board[i].each_index do |j|\n next if board[i][j] == '-'\n if (i + j) % 2 == 0\n board[i][j] = 'B'\n else\n board[i][j] = 'W'\n end\n end\nend\n\nboard.each do |row|\n row.each do |cell|\n print cell\n end\n puts\nend"}, {"source_code": "\nn,m = gets.split(\" \").map(&:to_i)\narr = []\nn.times do\n\tarr << gets.split(\"\")\nend\na = true\nb = true\n(0..n-1).each do |i|\n\tb = a\n\t(0..m-1).each do |j|\n\t\t# if arr[i][j] == '.'\t\t\t\n\t\t# \tcandidate = a ? 'B' : 'W'\n\t\t# \tif i > 0 && arr[i-1][j] != '-'\n\t\t# \t\tcandidate = borw(arr[i-1][j])\n\t\t# \tend\n\n\t\t# \tif j > 0 && arr[i][j-1] != '-'\n\t\t# \t\tcandidate = borw(arr[i][j-1])\n\t\t# \tend\n\t\t# \tarr[i][j] = candidate\n\t\t# \ta = !a\n\t\t# end\n\t\t\n\t\tif arr[i][j] != '-'\n\t\t\tarr[i][j] = b ? 'B' : 'W'\n\t\tend\n\n\t\tb = !b\n\tend\n\ta = !a\nend\n\narr.each do |a|\n\tputs a.join(\"\")\nend"}, {"source_code": "n, m = gets.split.map(&:to_i)\na = n.times.map{gets.chomp}\nn.times do |i|\n m.times do |j|\n a[i][j] = 'BW'[(i + j) % 2] if a[i][j] == '.'\n end\nend\nputs a\n"}, {"source_code": "# Read Input\nn, m = gets.strip.split.map{|s| s.to_i}\nboard = []; n.times {board.push(gets.strip.split(\"\"))};\n\n# Print Board\nfor i in 0..(n - 1) do\n for j in 0..(m - 1) do\n if board[i][j] == \".\"\n print ((i + j) % 2 == 0 ? 'B' :'W')\n else \n print \"-\"\n end\n end\n puts \"\"\nend"}, {"source_code": "def best(i,j)\n if i.odd? && j.odd?\n return \"B\"\n elsif i.odd? && j.even?\n return \"W\"\n elsif i.even? && j.odd?\n return \"W\"\n else\n return \"B\"\n end\nend\n\nn, m = gets.strip.split.map{|s| s.to_i}\n\nboard = []; n.times {board.push(gets.strip.split(\"\"))};\n\nfor i in 0..(n - 1) do\n for j in 0..(m - 1) do\n if board[i][j] == \".\"\n print best(i,j) \n else \n print \"-\"\n end\n end\n puts \"\"\nend"}, {"source_code": "\ndef best(i,j)\n if (i + j).odd?\n return \"W\"\n else\n return \"B\"\n end\nend\n\nn, m = gets.strip.split.map{|s| s.to_i}\n\nboard = []; n.times {board.push(gets.strip.split(\"\"))};\n\nfor i in 0..(n - 1) do\n for j in 0..(m - 1) do\n if board[i][j] == \".\"\n print best(i,j) \n else \n print \"-\"\n end\n end\n puts \"\"\nend"}, {"source_code": "line=gets\nn=line.split[0].to_i\nm=line.split[1].to_i\n\nfor i in 0...n do\n\tline=gets\n\tfor j in 0...m do\n\t\tif line[j]=='.'\n\t\t\tif((i+j)%2==0)\n\t\t\t\tline[j]='B'\n\t\t\telse\n\t\t\t\tline[j]='W'\n\t\t\tend\n\t\tend\n\tend\n\tputs line\nend\n"}, {"source_code": "n,m=gets.split.map(&:to_i)\na=[]\nn.times { a << gets.chomp }\nn.times do |i|\n m.times do |j|\n if a[i][j]=='.'\n print ((i+j)%2==0?'B':'W')\n else\n print '-'\n end\n end\n puts\nend\n"}, {"source_code": "s = gets.split(\" \")\nn = s[0].to_i\nm = s[1].to_i\na = []\nfor i in 0..n -1\n s = gets.chomp\n a[i] = s\nend\n\nfor i in 0..n-1\n for j in 0..m-1\n\n if( (i + j) % 2 == 1 && a[i][j]==\".\")\n a[i][j] = \"B\"\n elsif a[i][j]==\".\"\n a[i][j] = \"W\"\n end\n end\n end\n rez = \"\"\n for i in 0..n-1\n for j in 0..m-1\n rez += a[i][j]\nend\n rez += \"\\n\"\nend\nputs(rez)"}, {"source_code": "n, m = gets.split(' ').map(&:to_i)\noutput = \"\"\n(0..n-1).each do |i|\n line = gets\n (0..m-1).each do |j|\n if line[j] == '-'\n output += '-'\n elsif (i+j) % 2 == 0\n output +='B'\n else\n output += 'W'\n end\n end\n output += \"\\n\"\nend\nputs output"}, {"source_code": "# 445A\n\nn, m = gets.chomp.split.map &:to_i\nch = []\nn.times do\n ch << gets.chomp.chars\nend\nnn = n - 1\nmm = m - 1\nn.times do |i|\n m.times do |j|\n if ch[i][j] == '.'\n stack = [[i, j]]\n until stack.empty?\n p, q = stack[-1]\n ch[p][q] = (p > 0 && ch[p - 1][q] == 'W') || (q > 0 && ch[p][q - 1] == 'W') ||\n (p < nn && ch[p + 1][q] == 'W') || (q < mm && ch[p][q + 1] == 'W') ? 'B' : 'W'\n if p < nn && ch[p + 1][q] == '.'\n stack.push [p + 1, q]\n elsif q < mm && ch[p][q + 1] == '.'\n stack.push [p, q + 1]\n elsif p > 0 && ch[p - 1][q] == '.'\n stack.push [p - 1, q]\n elsif q > 0 && ch[p][q - 1] == '.'\n stack.push [p, q - 1]\n else\n stack.pop\n end\n end\n end\n end\nend\nch.each { |l| puts l.join('') }"}, {"source_code": "\n\nn, m = gets.split.map(&:to_i)\n\ntab = []\nn.times{tab << gets.strip.split(//)}\n\nresult = []\n\n(0..n-1).each do |i|\n result << []\n (0..m-1).each do |j|\n if tab[i][j] == '-'\n result[-1] << '-'\n elsif (i+j) % 2 == 1\n result[-1] << 'W'\n else\n result[-1] << 'B'\n end\n end\nend\n\nresult.each{|x| puts x.join(\"\")}\n\n\n"}], "negative_code": [{"source_code": "r, c = gets.split.map(&:to_i)\nchessboard=[]\nfor i in 1..r do\n\tx = gets\n\tchessboard << x\nend\n#puts chessboard\nfor i in 0...r do\n\tfor j in 0...c do\n\t if chessboard[i][j]=='-' then\n\t \tnext\n\t\telsif chessboard[i-1][j]=='B' || chessboard[i][j-1]=='B' then\n\t\t\tchessboard[i][j]='W'\n\t\telse\n\t\t\tchessboard[i][j]='B'\n\t\tend\n\n\tend\nend\n\nputs chessboard"}, {"source_code": "r, c = gets.split.map(&:to_i)\nchessboard=[]\nfor i in 1..r do\n\tx = gets\n\tchessboard << x\nend\nn='B'\n#puts chessboard\nfor i in 0...r do\n\tfor j in 0...c do\n\t\t#puts \"This is n: #{n}\"\n\t if chessboard[i][j]=='-' then\n\t \tnext\n\t elsif (chessboard[i-1][j]=='-' || i-1<0) && (chessboard[i][j-1]=='-' || j-1<0) then\n\t \t#puts \"here\"\n\t \tchessboard[i][j]=n\n\t \tif n=='W' then\n\t \t\t n='B'\n\t \telse\n\t \t\tn='W'\n\t \tend\n\t\telsif chessboard[i-1][j]=='B' || chessboard[i][j-1]=='B' then\n\t\t\tchessboard[i][j]='W'\n\t\t\tn='B'\n\t\telse\n\t\t\tchessboard[i][j]='B'\n\t\t\tn='W'\n\t\tend\n\tend\nend\n\nputs chessboard"}, {"source_code": "n, m = gets.chomp.split(/ /).map(&:to_i)\nboard = []\nn.times do\n board << gets.chomp\nend\nadjacent = [[0, 1], [0, -1], [-1, 0], [1, 0]]\nmen = ['B', 'W']\nn.times do |i|\n m.times do |j|\n cell = board[i][j]\n men.each_index do |index|\n man = men[index]\n board[i][j] = man if cell == '.'\n adjacent.each do |adj|\n di = i + adj[0]\n dj = j + adj[1]\n next if di < 0 || dj < 0 || di == n || dj == m\n neighbor = board[di][dj]\n if neighbor == '.'\n if cell == 'B'\n board[di][dj] = men[1]\n else\n board[di][dj] = men[0]\n end\n end\n end\n end\n end\nend\nboard.each do |line|\n puts line\nend\n"}, {"source_code": "n, m = gets.chomp.split(/ /).map(&:to_i)\nboard = []\nn.times do\n line = gets.chomp\n line = line.gsub '.', 'B'\n board << line\nend\nadjacent = [[0, 1], [0, -1], [-1, 0], [1, 0]]\nn.times do |i|\n m.times do |j|\n cell = board[i][j]\n adjacent.each do |adj|\n di = i + adj[0]\n dj = j + adj[1]\n next if di < 0 || dj < 0 || di == n || dj == m\n board[di][dj] = 'W' if cell == 'B' && board[di][dj] == cell\n board[di][dj] = 'B' if cell == 'W' && board[di][dj] == cell\n end\n end\nend\nboard.each do |line|\n puts line\nend\n"}, {"source_code": "n, m = gets.chomp.split(/ /).map(&:to_i)\nboard = []\nn.times do\n board << gets.chomp\nend\nadjacent = [[0, 1], [0, -1], [-1, 0], [1, 0]]\nmen = ['B', 'W']\nn.times do |i|\n m.times do |j|\n men.each_index do |index|\n man = men[index]\n board[i][j] = man if board[i][j] == '.'\n cell = board[i][j]\n adjacent.each do |adj|\n di = i + adj[0]\n dj = j + adj[1]\n next if di < 0 || dj < 0 || di == n || dj == m\n neighbor = board[di][dj]\n if neighbor == '.'\n if cell == 'B'\n board[di][dj] = men[1]\n elsif cell == 'W'\n board[di][dj] = men[0]\n end\n elsif neighbor == cell && cell != '-'\n if cell == 'B'\n board[di][dj] = men[1]\n elsif cell == 'W'\n board[di][dj] = men[0]\n end\n end\n end\n end\n end\nend\nboard.each do |line|\n puts line\nend\n"}, {"source_code": "n, m = gets.chomp.split(/ /).map(&:to_i)\nboard = []\nn.times do\n line = gets.chomp\n line = line.gsub '.', 'B'\n board << line\nend\nadjacent = [[0, 1], [0, -1], [-1, 0], [1, 0]]\nn.times do |i|\n m.times do |j|\n cell = board[i][j]\n adjacent.each do |adj|\n di = i + adj[0]\n dj = j + adj[1]\n next if di < 0 || dj < 0 || di == n || dj == m\n neighbor = board[di][dj]\n board[di][dj] = 'W' if cell == 'B' && neighbor == cell\n board[di][dj] = 'B' if cell == 'W' && neighbor == cell\n end\n end\nend\nboard.each do |line|\n puts line\nend\n"}, {"source_code": "n, m = gets.chomp.split(/ /).map(&:to_i)\nboard = []\nn.times do\n board << gets.chomp\nend\nadjacent = [[0, 1], [0, -1], [-1, 0], [1, 0]]\nn.times do |i|\n m.times do |j|\n board[i][j] = 'B' if board[i][j] == '.'\n cell = board[i][j]\n adjacent.each do |adj|\n di = i + adj[0]\n dj = j + adj[1]\n next if di < 0 || dj < 0 || di == n || dj == m\n neighbor = board[di][dj]\n if neighbor == '.'\n if cell == 'B'\n board[di][dj] = 'W'\n elsif cell == 'W'\n board[di][dj] = 'B'\n end\n elsif neighbor == cell && cell != '-'\n if cell == 'B'\n board[di][dj] = 'W'\n elsif cell == 'W'\n board[di][dj] = 'B'\n end\n end\n end\n end\nend\nboard.each do |line|\n puts line\nend\n"}, {"source_code": "n, m = gets.chomp.split(/ /).map(&:to_i)\nboard = []\nn.times do\n board << gets.chomp\nend\nadjacent = [[0, 1], [0, -1], [-1, 0], [1, 0]]\nmen = ['B', 'W']\nn.times do |i|\n m.times do |j|\n men.each_index do |index|\n man = men[index]\n board[i][j] = man if board[i][j] == \".\"\n cell = board[i][j]\n adjacent.each do |adj|\n di = i + adj[0]\n dj = j + adj[1]\n next if di < 0 || dj < 0 || di == n || dj == m\n neighbor = board[di][dj]\n if neighbor == \".\"\n if cell == 'B'\n board[di][dj] = men[1]\n elsif cell == 'W'\n board[di][dj] = men[0]\n end\n elsif neighbor == cell\n if cell == 'B'\n board[di][dj] = men[1]\n elsif cell == 'W'\n board[di][dj] = men[0]\n end\n end\n end\n end\n end\nend\nboard.each do |line|\n puts line\nend\n"}, {"source_code": "numbers = gets.split(' ')\nn = numbers[0].to_i\nm = numbers[1].to_i\n\nboard = []\nn.times do\n row = gets.split('')\n row.pop\n board.push row\nend\n\nboard.each_index do |i|\n board[i].each_index do |j|\n next if board[i][j] == '-'\n if i == 0\n if j == 0\n board[i][j] = 'B'\n else\n if board[i][j - 1] == 'B'\n board[i][j] = 'W'\n else\n board[i][j] = 'B'\n end\n end\n else\n if j == 0\n if board[i - 1][j] == 'B'\n board[i][j] = 'W'\n else\n board[i][j] = 'B'\n end\n else\n if board[i - 1][j] == 'B' || board[i][j - 1] == 'B'\n board[i][j] = 'W'\n else\n board[i][j] = 'B'\n end\n end\n end\n end\nend\n\nboard.each do |row|\n row.each do |cell|\n print cell\n end\n puts\nend"}, {"source_code": "numbers = gets.split(' ')\nn = numbers[0].to_i\nm = numbers[1].to_i\n\nboard = []\nn.times do\n row = gets.split('')\n row.pop\n board.push row\nend\n\nboard.each_index do |i|\n board[i].each_index do |j|\n next if board[i][j] == '-'\n if i == 0\n if j == 0\n board[i][j] = 'B'\n else\n if board[i][j - 1] == 'B'\n board[i][j] = 'W'\n else\n board[i][j] = 'B'\n end\n end\n else\n if j == 0\n if board[i - 1][j] == 'B'\n board[i][j] = 'W'\n else\n board[i][j] = 'B'\n end\n else\n if board[i - 1][j] == 'B'\n board[i][j] = 'W'\n else\n board[i][j] = 'B'\n end\n end\n end\n end\nend\n\nboard.each do |row|\n row.each do |cell|\n print cell\n end\n puts\nend"}, {"source_code": "def borw(c)\n\tif c == 'B'\n\t\t'W'\n\telse\n\t\t'B'\n\tend\nend\n\nn,m = gets.split(\" \").map(&:to_i)\narr = []\nn.times do\n\tarr << gets.split(\"\")\nend\na = true\n(0..n-1).each do |i|\n\t(0..m-1).each do |j|\n\t\tif arr[i][j] == '.'\t\t\t\n\t\t\tcandidate = a ? 'B' : 'W'\n\t\t\tif i > 0 && arr[i-1][j] != '-'\n\t\t\t\tcandidate = borw(arr[i-1][j])\n\t\t\tend\n\n\t\t\tif j > 0 && arr[i][j-1] != '-'\n\t\t\t\tcandidate = borw(arr[i][j-1])\n\t\t\tend\n\t\t\tarr[i][j] = candidate\n\t\t\ta = !a\n\t\tend\n\tend\nend\n\narr.each do |a|\n\tputs a.join(\"\")\nend"}, {"source_code": "def borw(c)\n\tif c == 'B'\n\t\t'W'\n\telse\n\t\t'B'\n\tend\nend\n\nn,m = gets.split(\" \").map(&:to_i)\narr = []\nn.times do\n\tarr << gets.split(\" \")\nend\n\n(0..n-1).each do |i|\n\t(0..m-1).each do |j|\n\t\tif arr[i][j] == '.'\t\t\t\n\t\t\tcandidate = 'B'\n\t\t\tif i > 0\n\t\t\t\tcandidate = borw(arr[i-1][j])\n\t\t\tend\n\n\t\t\tif j > 0\n\t\t\t\tcandidate = borw(arr[i][j-1])\n\t\t\tend\n\t\t\tarr[i][j] = candidate\n\t\tend\n\tend\nend\n\narr.each do |a|\n\tputs a.join(\"\")\nend"}, {"source_code": "def borw(c)\n\tif c == 'B'\n\t\t'W'\n\telse\n\t\t'B'\n\tend\nend\n\nn,m = gets.split(\" \").map(&:to_i)\narr = []\nn.times do\n\tarr << gets.split(\"\")\nend\n\n(0..n-1).each do |i|\n\t(0..m-1).each do |j|\n\t\tif arr[i][j] == '.'\t\t\t\n\t\t\tcandidate = 'B'\n\t\t\tif i > 0 && arr[i-1][j] != '-'\n\t\t\t\tcandidate = borw(arr[i-1][j])\n\t\t\tend\n\n\t\t\tif j > 0 && arr[i][j-1] != '-'\n\t\t\t\tcandidate = borw(arr[i][j-1])\n\t\t\tend\n\t\t\tarr[i][j] = candidate\n\t\tend\n\tend\nend\n\narr.each do |a|\n\tputs a.join(\"\")\nend"}, {"source_code": "def borw(c)\n\tif c == 'B'\n\t\t'W'\n\telse\n\t\t'B'\n\tend\nend\n\nn,m = gets.split(\" \").map(&:to_i)\narr = []\nn.times do\n\tarr << gets.split(\"\")\nend\n\n(0..n-1).each do |i|\n\t(0..m-1).each do |j|\n\t\tif arr[i][j] == '.'\t\t\t\n\t\t\tcandidate = 'B'\n\t\t\tif i > 0\n\t\t\t\tcandidate = borw(arr[i-1][j])\n\t\t\tend\n\n\t\t\tif j > 0\n\t\t\t\tcandidate = borw(arr[i][j-1])\n\t\t\tend\n\t\t\tarr[i][j] = candidate\n\t\tend\n\tend\nend\n\narr.each do |a|\n\tputs a.join(\"\")\nend"}, {"source_code": "n, m = gets.chomp.split.map &:to_i\nr = []\nq = nil\nn.times do |_|\n p = gets.chomp.chars\n (0...m).each { |j| p[j] = q[j] == 'B' ? 'W' : 'B' unless p[j] == '-' || q[j] == '-' } unless q.nil?\n (0...m).each { |j| p[j] = j > 0 && p[j - 1] == 'B' || j < m - 1 && p[j + 1] == 'B' ? 'W' : 'B' if p[j] == '.' }\n q = p\n r << q.join\nend\nputs r"}, {"source_code": "\n\nn, m = gets.split.map(&:to_i)\n\ntab = []\nn.times{tab << gets.strip.split(//)}\n\nresult = []\n\n(0..n-1).each do |i|\n result << []\n (0..m-1).each do |j|\n if tab[i][j] == '-'\n result[-1] << '-'\n elsif i+j % 2 == 1\n result[-1] << 'W'\n else\n result[-1] << 'B'\n end\n end\nend\n\nresult.each{|x| puts x.join(\"\")}\n\n\n"}], "src_uid": "dc31adef80f06897ea2f5ef76854bcf1"} {"nl": {"description": "Вам задано прямоугольное клетчатое поле, состоящее из n строк и m столбцов. Поле содержит цикл из символов «*», такой что: цикл можно обойти, посетив каждую его клетку ровно один раз, перемещаясь каждый раз вверх/вниз/вправо/влево на одну клетку; цикл не содержит самопересечений и самокасаний, то есть две клетки цикла соседствуют по стороне тогда и только тогда, когда они соседние при перемещении вдоль цикла (самокасание по углу тоже запрещено). Ниже изображены несколько примеров допустимых циклов: Все клетки поля, отличные от цикла, содержат символ «.». Цикл на поле ровно один. Посещать клетки, отличные от цикла, Роботу нельзя.В одной из клеток цикла находится Робот. Эта клетка помечена символом «S». Найдите последовательность команд для Робота, чтобы обойти цикл. Каждая из четырёх возможных команд кодируется буквой и обозначает перемещение Робота на одну клетку: «U» — сдвинуться на клетку вверх, «R» — сдвинуться на клетку вправо, «D» — сдвинуться на клетку вниз, «L» — сдвинуться на клетку влево. Робот должен обойти цикл, побывав в каждой его клетке ровно один раз (кроме стартовой точки — в ней он начинает и заканчивает свой путь).Найдите искомую последовательность команд, допускается любое направление обхода цикла.", "input_spec": "В первой строке входных данных записаны два целых числа n и m (3 ≤ n, m ≤ 100) — количество строк и столбцов прямоугольного клетчатого поля соответственно. В следующих n строках записаны по m символов, каждый из которых — «.», «*» или «S». Гарантируется, что отличные от «.» символы образуют цикл без самопересечений и самокасаний. Также гарантируется, что на поле ровно одна клетка содержит «S» и что она принадлежит циклу. Робот не может посещать клетки, помеченные символом «.».", "output_spec": "В первую строку выходных данных выведите искомую последовательность команд для Робота. Направление обхода цикла Роботом может быть любым.", "sample_inputs": ["3 3\n***\n*.*\n*S*", "6 7\n.***...\n.*.*...\n.*.S**.\n.*...**\n.*....*\n.******"], "sample_outputs": ["LUURRDDL", "UULLDDDDDRRRRRUULULL"], "notes": "ПримечаниеВ первом тестовом примере для обхода по часовой стрелке последовательность посещенных роботом клеток выглядит следующим образом: клетка (3, 2); клетка (3, 1); клетка (2, 1); клетка (1, 1); клетка (1, 2); клетка (1, 3); клетка (2, 3); клетка (3, 3); клетка (3, 2). "}, "positive_code": [{"source_code": "#!/usr/bin/env ruby\n\nh, w = gets.split.map(&:to_i)\nlines = [[]]\nfor i in 0..h-1\n lines[i] = gets.chomp.chars\nend\nx = lines.detect { |a| a.include? ('S') }.index('S')\nlines = lines.transpose\ny = lines.detect { |a| a.include? ('S') }.index('S')\n\nwhile true do\n if x-1>=0 and lines[x-1][y] == '*' then printf \"L\"; x-=1; lines[x][y] = '.'\n elsif x+1=0 and lines[x][y-1] == '*' then printf \"U\"; y-=1; lines[x][y] = '.'\n else break end\nend\n\nif x-1>=0 and lines[x-1][y] == 'S' then printf \"L\"; x-=1; lines[x][y] = '.'\nelsif x+1=0 and lines[x][y-1] == 'S' then printf \"U\"; y-=1; lines[x][y] = '.' end\nputs\n"}], "negative_code": [{"source_code": "#!/usr/bin/env ruby\n\nh, w = gets.split.map(&:to_i)\nlines = [[]]\nfor i in 0..h-1\n lines[i] = gets.chomp.chars\nend\nx = lines.detect { |a| a.include? ('S') }.index('S')\nlines = lines.transpose\ny = lines.detect { |a| a.include? ('S') }.index('S')\n\nwhile true do\n if x-1>=0 and lines[x-1][y] == '*' then puts \"L\"; x-=1; lines[x][y] = '.'\n elsif x+1=0 and lines[x][y-1] == '*' then puts \"U\"; y-=1; lines[x][y] = '.'\n else break end\nend\n\nif x-1>=0 and lines[x-1][y] == 'S' then puts \"L\"; x-=1; lines[x][y] = '.'\nelsif x+1=0 and lines[x][y-1] == 'S' then puts \"U\"; y-=1; lines[x][y] = '.' end\n"}], "src_uid": "dea56c6d6536e7efe80d39ebc6b819a8"} {"nl": {"description": "Once upon a time a little frog whose name was Vasya decided to travel around his home swamp. Overall there are n mounds on the swamp, located on one line. The distance between the neighboring mounds is one meter. Vasya wants to visit all the mounds in one day; besides, he wants to visit each one exactly once. For that he makes a route plan, to decide the order in which to jump on the mounds. Vasya can pick any mound as the first one. He thinks it boring to jump two times at the same distance. That's why he wants any two jumps on his route to have different lengths. Help Vasya the Frog and make the plan for him.", "input_spec": "The single line contains a number n (1 ≤ n ≤ 104) which is the number of mounds.", "output_spec": "Print n integers pi (1 ≤ pi ≤ n) which are the frog's route plan. All the pi's should be mutually different. All the |pi–pi + 1|'s should be mutually different (1 ≤ i ≤ n - 1). If there are several solutions, output any.", "sample_inputs": ["2", "3"], "sample_outputs": ["1 2", "1 3 2"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\np = Array.new(n)\np[0] = 1\n(1...n).each {|i| p[i] = (i % 2 == 1 ? p[i-1] + i : p[i-1] - i)}\npmin = p.min\np.map!{|x| x - pmin + 1}\nputs p.join(' ')\n"}, {"source_code": "n=gets.to_i;$><<(0...n).map{|i|i.odd?? n-i/2:i/2+1}*' '\n"}, {"source_code": "#sharyo\nn=gets.to_i\ni,j=1,n\nwhile n!=0\n\tn-=1\n\tif (n&1)==1\n print i.to_s+\" \"\n i+=1\n\telse\n print j.to_s+\" \"\n j-=1\n\tend\t\nend\t\n"}, {"source_code": "n=gets.to_i;$><<(0...n).map{|i|i.odd?? n-i/2:i/2+1}*' '\n"}, {"source_code": "n=gets.to_i;$><<(0...n).map{|i|i.odd?? n-i/2:i/2+1}*' '"}, {"source_code": "n=gets.to_i;$><<(0...n).map{|i|i.odd?? n-i/2:i/2+1}*' '\n"}, {"source_code": "n=gets.to_i;$><<(0...n).map{|i|i.odd?? n-i/2:i/2+1}*' '\n"}, {"source_code": "n=gets.to_i;$><<(0...n).map{|i|i.odd?? n-i/2:i/2+1}*' '"}, {"source_code": "n=gets.to_i;$><<(0...n).map{|i|i.odd?? n-i/2:i/2+1}*' '\n"}, {"source_code": "num = gets.chomp.to_i\n\nlist = \"\"\n\nflag = false\ncnt = 0\ncnt1 = 0\ncnt2 = 0\n\nwhile cnt != num\n if flag\n list << \"#{num - cnt1} \"\n cnt1 = cnt1 + 1\n else\n list << \"#{1 + cnt2} \"\n cnt2 = cnt2 + 1\n end\n flag = !flag\n cnt = cnt + 1\nend\n\nputs list.chomp\n"}, {"source_code": "n=gets.to_i;$><<(0...n).map{|i|i.odd?? n-i/2:i/2+1}*' '\n"}, {"source_code": "n = gets.to_i\nprintf \"1 \"\ncc = 1\n(n - 1).downto(1) do |c|\n\tsgn = (n - c) % 2 == 1 ? 1 : -1;\n\tcc = cc + c * sgn\n\tprint \"#{cc} \"\nend\n"}, {"source_code": "n=gets.to_i;$><<(0...n).map{|i|i.odd?? n-i/2:i/2+1}*' '\n"}, {"source_code": "a=[*1..gets.to_i]\n$*<<<$**\" \""}, {"source_code": "a=[*1..gets.to_i]\nprint a.pop,\" \",a.shift,\" \"while[]!=a\n"}, {"source_code": "n=gets.to_i;$><<(0...n).map{|i|i.odd?? n-i/2:i/2+1}*' '\n"}], "negative_code": [{"source_code": "#sharyo\nn=gets.to_i\ni,j=1, n\nwhile n>0\n\tn-=1\n\tif n&1\n print i.to_s+\" \"\n i+=1\n\telse\n print j.to_s+\" \"\n j-=1\n\tend\t\nend\t\n"}, {"source_code": "a=[*?1..gets]\n$*<<<$**\" \""}], "src_uid": "02b7ff5e0b7f8cd074d9e76251c2725e"} {"nl": {"description": "Ilya plays a card game by the following rules.A player has several cards. Each card contains two non-negative integers inscribed, one at the top of the card and one at the bottom. At the beginning of the round the player chooses one of his cards to play it. If the top of the card contains number ai, and the bottom contains number bi, then when the player is playing the card, he gets ai points and also gets the opportunity to play additional bi cards. After the playing the card is discarded.More formally: let's say that there is a counter of the cards that can be played. At the beginning of the round the counter equals one. When a card is played, the counter decreases by one for the played card and increases by the number bi, which is written at the bottom of the card. Then the played card is discarded. If after that the counter is not equal to zero, the player gets the opportunity to play another card from the remaining cards. The round ends when the counter reaches zero or the player runs out of cards.Of course, Ilya wants to get as many points as possible. Can you determine the maximum number of points he can score provided that you know his cards?", "input_spec": "The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of cards Ilya has. Each of the next n lines contains two non-negative space-separated integers — ai and bi (0 ≤ ai, bi ≤ 104) — the numbers, written at the top and the bottom of the i-th card correspondingly.", "output_spec": "Print the single number — the maximum number of points you can score in one round by the described rules.", "sample_inputs": ["2\n1 0\n2 0", "3\n1 0\n2 0\n0 2"], "sample_outputs": ["2", "3"], "notes": "NoteIn the first sample none of two cards brings extra moves, so you should play the one that will bring more points.In the second sample you should first play the third card that doesn't bring any points but lets you play both remaining cards."}, "positive_code": [{"source_code": "n = gets.to_i\na = Array.new(n){gets.split.map(&:to_i).reverse}.sort.reverse\nb, c = 1, 0\na.each do |x, y|\n break if b == 0\n b += x - 1\n c += y\nend\np c\n"}, {"source_code": "n=gets.to_i\ns=[]\nans=0\ncnt=1\nn.times{\n a,b=gets.split.map(&:to_i)\n\n s.push(a) if b==0\n cnt+=b\n if b!=0 then\n cnt-=1\n ans+=a\n end\n}\ns.sort!\ns.reverse!\n[cnt,s.size()].min.times{|i|\n ans+=s[i]\n}\np ans\n"}, {"source_code": "\nn = gets.chomp.to_i\n\ncards = []\nn.times do \n cards << gets.chomp.split(\" \").map{|e| e.to_i}\nend\n\ncards.sort!{|a, b|\n if a[1] == b[1]\n b[0] <=> a[0]\n else\n b[1] <=> a[1]\n end\n}\n\ncard = cards.shift\nscore = card[0]\ntime = card[1]\n\ncards.each do |card|\n if time <= 0\n break\n else\n score += card[0]\n time += card[1]\n end\n time -= 1\nend\n\nputs score\n\n\n\n"}, {"source_code": "n = STDIN.gets.chomp.to_i\nthe_cards = n.times.map { STDIN.gets.chomp.split(/ /).map(&:to_i) }\n\nuse = ->(point, counter, card) { [point + card[0], counter - 1 + card[1]] }\nsort_card = ->(cards) { cards.sort {|a,b| (_ = a[1] <=> b[1]).zero? ? (a[0] <=> b[0]) : _ } }\n\nsolve = ->(cards, point, counter) do\n while !counter.zero? && !cards.empty?\n cards = sort_card[cards]\n card = cards.pop\n point, counter = use[point, counter, card]\n end\n point\nend\n\np solve[the_cards,0,1]\n"}, {"source_code": "aa,bb,ans,ct=[],[],0,1\nn=gets.to_i\nn.times do\n\tx,y=gets.chomp.split.map(&:to_i)\n\taa<aa[a]\n\t\tb=i if bb[i]>bb[b]\n\tend\n\tif ct==1&&bb[b]==0\n\t\tans+=aa[a]\n\t\tbreak\n\telsif bb[b]==0\n\t\tans+=aa[a]\n\t\tct-=1\n\t\taa[a]=0\n\telse\n\t\tans+=aa[b]\n\t\tct+=bb[b]-1\n\t\taa[b],bb[b]=0,0\n\tend\nend\nputs ans"}, {"source_code": "n, st, i, score = gets.to_i, 1, 0, 0\na = readlines.map(&:chomp).map(&:split).each {|x| x.map!(&:to_i)}.sort_by! { |x| [-x[1], -x[0]] }\n# p a\nwhile st < n - i and i <= n and a[0][1].nonzero?\n st += a[0][1] - 1\n score += a[0][0]\n i += 1\n a.shift\nend\n# p a.sort_by! {|x| [-x[0], -x[1]]}\nwhile st.nonzero? and !a.empty?\n st += a[0][1] - 1\n score += a[0][0]\n a.shift\nend\np score"}, {"source_code": "#!/usr/bin/ruby\ndef read_int; gets.chomp.to_i; end\ndef read_ints; gets.chomp.split.map(&:to_i); end\n\nnum = read_int\nvals = []\n\nnum.times do\n vals << read_ints\nend\nvals.sort! {|a,b|\n if b[1] == a[1]\n b[0] <=> a[0]\n else\n b[1] <=> a[1]\n end\n}\nvals.delete_if {|a| a[1] == 0 && a[0] == 0 }\n\nrest = 1\npoint = 0\nvals.each do |i|\n point += i[0]\n rest += i[1]\n rest -= 1\n break if rest == 0\nend\n\nputs point\n"}, {"source_code": "a = gets.to_i.times.map{ gets.split.map(&:to_i) }\n\nb = a.sort{|i,j| (j[1] <=> i[1]).nonzero? or j[0] <=> i[0]}\n\npt = 0\nc = 0\n\nb.each do |e|\n pt += e[0]\n c += e[1]\n break if c.zero?\n c -= 1\nend\n\np pt\n"}, {"source_code": "n = gets.to_i\narr = []\nn.times do \n\tarr << gets.split(\" \").map(&:to_i)\t\nend\narr.sort!{|a,b|\n\tif a[1] == b[1]\n\t\tb[0] <=> a[0]\n\telse\n\t\tb[1] <=> a[1]\n\tend\n\t# puts \"#{a} b #{b}\"\n}\n\ni = 0\ncounter = 1\npoints = 0\nwhile true\n\tpoints += arr[i][0]\n\tcounter += arr[i][1]\n\ti += 1\n\tcounter -= 1\n\tbreak if counter == 0 || !arr[i]\nend\nputs points"}, {"source_code": "n = gets.to_i\nli = []\n(0...n).each{li.push(gets.split.collect{|i| i.to_i})}\nli = li.sort_by{|i| [i[1], i[0]]}\nans = 0\ncnt = 1\nli.reverse.each do |i|\n ans += i[0]\n cnt += i[1]-1\n break if cnt.zero?\nend\nputs ans\n"}, {"source_code": "n = gets.to_i\nc = []\nfor i in 0..n-1\n\tline = gets.split\n\tc << [line[0].to_i, line[1].to_i]\nend\n\nc.sort_by! {|e| [e[1], e[0]]}\nc.reverse!\n\nscore = 0\ncount = 1\nfor i in 0..n-1\n\tif(count == 0)\n\t\tbreak\n\telse\n\t\tscore += c[i][0]\n\t\tcount += c[i][1]\n\t\tcount -= 1\n\tend\nend\n\nputs score"}, {"source_code": "def read_next_line\n gets.split.map { |x| x.to_i }\nend\n\nn = gets.to_i\np = []\ncounter = 1\nres = 0\nfor _ in 0...n\n a, b = read_next_line\n if b > 0\n counter += b - 1\n res += a\n else\n p << a\n end\nend\nif p.empty?\n puts res\n exit\nend\nres += p.sort[-[counter, p.size].min..-1].inject(:+)\nputs res"}, {"source_code": "a = []\n\ngets.to_i.times do\n a << gets.split.map(&:to_i)\nend\n\ncounter = 1\nscore = 0\na = a.sort_by {|col| [col[1], col[0]]}\nuntil counter.zero? or a.empty?\n x = a.pop\n counter -= 1\n counter += x[1]\n score += x[0]\nend\n\nputs score\n"}, {"source_code": "a = []\n\ngets.to_i.times do\n a << gets.split.map(&:to_i)\nend\n\ncounter = 1\nscore = 0\na = a.sort_by {|col| [col[1], col[0]]}\nuntil counter.zero? or a.empty?\n x = a.pop\n counter -= 1\n counter += x[1]\n score += x[0]\nend\n\nputs score\n"}, {"source_code": "n=gets.to_i\nzeros=[]\nscore=0\nopportunity=1\nn.times{\n\ta,b=gets.split.map(&:to_i)\n\tif b>0\n\t\tscore+=a\n\t\topportunity+=b-1\n\telse\n\t\tzeros< a[0]\n else\n b[1] <=> a[1]\n end\n}\n\ncard = cards.shift\nscore = card[0]\ntime = card[1]\n\ncards.each do |card|\n if time <= 0\n break\n else\n score += card[0]\n time += card[1]\n end \nend\n\nputs score\n\n\n\n"}, {"source_code": "class Item\n attr_accessor :times, :cards, :score\n \n def initialize(times, cards, score)\n @times = times\n @cards = cards\n @score = score\n end \nend\n\nn = gets.chomp.to_i\n\ncards = []\nn.times do \n cards << gets.chomp.split(\" \").map{|e| e.to_i}\nend\n\nqueue = []\ncards.each_with_index do |card, idx|\n tmp = cards.dup\n tmp.delete_at(idx)\n item = Item.new(card[1], tmp, card[0])\n \n queue << item\nend\n\nscores = []\nloop do\n if queue.length == 0\n break\n end\n \n new_items = []\n queue.each do |item|\n if item.times > 0\n cards = item.cards\n item.times -= 1\n \n cards.each_with_index do |card, idx|\n tmp = cards.dup\n tmp.delete_at(idx)\n new_item = Item.new(item.times + card[1], tmp, item.score + card[0])\n new_items << new_item\n end \n else\n scores << item.score \n end \n end\n queue = new_items\nend\n\nputs scores.max\n\n\n\n\n\n"}, {"source_code": "n = STDIN.gets.chomp.to_i\nthe_cards = n.times.map { STDIN.gets.chomp.split(/ /).map(&:to_i) }\n\nuse = ->(point, counter, card) { [point + card[0], counter - 1 + card[1]] }\n\nsolve = ->(cards, point, counter) do\n until counter.zero?\n cards = cards.sort {|a,b| (_ = a[1] <=> a[1]).zero? ? (a[0] <=> a[0]) : _ }\n point, counter = use[point, counter, cards.pop]\n end\n point\nend\n\np solve[the_cards,0,1]\n"}, {"source_code": "n = STDIN.gets.chomp.to_i\nthe_cards = n.times.map { STDIN.gets.chomp.split(/ /).map(&:to_i) }\n\nuse = ->(point, counter, card) { [point + card[0], counter - 1 + card[1]] }\n\nsolve = ->(cards, point, counter) do\n cards = cards.sort {|a,b| (_ = a[1] <=> a[1]).zero? ? (a[0] <=> a[0]) : _ }\n point, counter = use[point, counter, cards.pop]\n return point if counter.zero?\n solve[cards, point, counter]\nend\n\np solve[the_cards,0,1]\n"}, {"source_code": "n=gets.to_i\nzeros=[]\nscore=0\nopportunity=1\nn.times{\n\ta,b=gets.split.map(&:to_i)\n\tif b>1\n\t\tscore+=a\n\t\topportunity+=b-1\n\telse\n\t\tzeros< a[:value] } # Sorting by value\n\n hat_index = 1 # Currently considered index of hat\n person_index = 0 # Currently considered person\n while person_index < n\n value = a_pairs[person_index][:value]\n s = n - value # Size of group of co-hatters\n s.times do |j|\n i = person_index + j\n return nil if i >= n || a_pairs[i][:value] != value # Unable to get group of hatters\n b[a_pairs[i][:index]] = hat_index\n end\n hat_index += 1\n person_index += s\n end\n\n return b\nend\n\n# Input\nn = gets.to_i # 1 <= n <= 10**5\na = gets.split(\" \") # 1 <= a[i] <= n-1\n\n# Processing\nb = main(n, a)\n\n# Output\nif b\n puts \"Possible\"\n b.each { |s| puts s }\nelse\n puts \"Impossible\"\nend\n"}], "negative_code": [], "src_uid": "f49667ef00cd0da6a6fad67a19d92f1e"} {"nl": {"description": "Sereja has got an array, consisting of n integers, a1, a2, ..., an. Sereja is an active boy, so he is now going to complete m operations. Each operation will have one of the three forms: Make vi-th array element equal to xi. In other words, perform the assignment avi = xi. Increase each array element by yi. In other words, perform n assignments ai = ai + yi (1 ≤ i ≤ n). Take a piece of paper and write out the qi-th array element. That is, the element aqi. Help Sereja, complete all his operations.", "input_spec": "The first line contains integers n, m (1 ≤ n, m ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the original array. Next m lines describe operations, the i-th line describes the i-th operation. The first number in the i-th line is integer ti (1 ≤ ti ≤ 3) that represents the operation type. If ti = 1, then it is followed by two integers vi and xi, (1 ≤ vi ≤ n, 1 ≤ xi ≤ 109). If ti = 2, then it is followed by integer yi (1 ≤ yi ≤ 104). And if ti = 3, then it is followed by integer qi (1 ≤ qi ≤ n).", "output_spec": "For each third type operation print value aqi. Print the values in the order, in which the corresponding queries follow in the input.", "sample_inputs": ["10 11\n1 2 3 4 5 6 7 8 9 10\n3 2\n3 9\n2 10\n3 1\n3 10\n1 1 10\n2 10\n2 10\n3 1\n3 10\n3 9"], "sample_outputs": ["2\n9\n11\n20\n30\n40\n39"], "notes": null}, "positive_code": [{"source_code": "n,m = gets.split.map(&:to_i)\nas = gets.split.map(&:to_i)\nacc = 0\n$<.each do |line|\n a,b,c = line.split.map(&:to_i)\n #p [a,b,c]\n case a\n when 1 ; as[b - 1] = c - acc\n when 2 ; acc += b\n when 3 ; puts (as[b - 1] + acc)\n end\nend\n"}], "negative_code": [], "src_uid": "48f3ff32a11770f3b168d6e15c0df813"} {"nl": {"description": "You are given two integers $$$a$$$ and $$$b$$$.In one move, you can choose some integer $$$k$$$ from $$$1$$$ to $$$10$$$ and add it to $$$a$$$ or subtract it from $$$a$$$. In other words, you choose an integer $$$k \\in [1; 10]$$$ and perform $$$a := a + k$$$ or $$$a := a - k$$$. You may use different values of $$$k$$$ in different moves.Your task is to find the minimum number of moves required to obtain $$$b$$$ from $$$a$$$.You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 2 \\cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$a$$$ and $$$b$$$ ($$$1 \\le a, b \\le 10^9$$$).", "output_spec": "For each test case, print the answer: the minimum number of moves required to obtain $$$b$$$ from $$$a$$$.", "sample_inputs": ["6\n5 5\n13 42\n18 4\n1337 420\n123456789 1000000000\n100500 9000"], "sample_outputs": ["0\n3\n2\n92\n87654322\n9150"], "notes": "NoteIn the first test case of the example, you don't need to do anything.In the second test case of the example, the following sequence of moves can be applied: $$$13 \\rightarrow 23 \\rightarrow 32 \\rightarrow 42$$$ (add $$$10$$$, add $$$9$$$, add $$$10$$$).In the third test case of the example, the following sequence of moves can be applied: $$$18 \\rightarrow 10 \\rightarrow 4$$$ (subtract $$$8$$$, subtract $$$6$$$)."}, "positive_code": [{"source_code": "t = gets.to_i\nt.times do\n\ta, b = gets.split.map &:to_i\n\tp ((b - a).abs + 9) / 10\nend"}, {"source_code": "\nt = gets.to_i\n\ndef play(a,b)\n return 0 if a == b\n\n if a > b\n c = a\n a = b\n b = c\n end\n\n x = b - a\n r = x % 10\n\n if r == 0\n return x / 10\n else\n return (x-r)/10 + 1\n end\nend\n\nwhile t > 0\n a,b = gets.split(/\\s+/).map(&:to_i)\n puts play(a,b)\n t -= 1\nend\n\n"}, {"source_code": "t = gets.chomp.to_i\nt.times do \n a, b = gets.chomp.split(\" \").map(&:to_i)\n sa = (a - b).abs\n ans = sa / 10\n amari = sa % 10\n if amari == 0\n puts ans\n else\n puts ans + 1\n end\nend"}, {"source_code": "gets.to_i.times do\n a, b = gets.split.map &:to_i\n d = (a - b).abs\n puts d / 10 + (d % 10 > 0 ? 1 : 0)\nend"}, {"source_code": "def solve\n n, m = gets.strip.split(' ').map(&:to_i)\n n, m = m, n if n < m\n dif = n - m;\n puts (dif / 10.0).to_i + (dif % 10 != 0 ? 1 : 0)\nend\n\ngets.strip.to_i.times { solve }"}, {"source_code": "gets.to_i.times {\n a,b = gets.split.map &:to_i\n x = (a-b).abs\n p (x+9)/10\n}\n"}, {"source_code": "n=gets.to_i\nn.times do\n a,b = gets.split.map(&:to_i)\n puts ((b-a).abs/10.0).ceil\nend"}, {"source_code": "\nt = gets.to_i\n\ndef play(a,b)\n return 0 if a == b\n\n if a > b\n c = a\n a = b\n b = c\n end\n\n x = b - a\n r = x % 10\n\n if r == 0\n return x / 10\n else\n return (x-r)/10 + 1\n end\nend\n\nwhile t > 0\n a,b = gets.split(/\\s+/).map(&:to_i)\n puts play(a,b)\n t -= 1\nend\n\n"}, {"source_code": "gets.to_i.times do\n a, b = gets.split.map &:to_i\n d = (a - b).abs\n puts d/10 + (d%10 == 0 ? 0 : 1)\nend\n"}, {"source_code": "t = gets.chomp.to_i\ninputs = []\nt.times do\n inputs << gets.chomp.split.map(&:to_i)\nend\n\ninputs.each do |a,b|\n ret = 0\n if b > a\n ret += (b-a)/10\n ret += 1 if b > a + ret*10\n elsif b < a\n ret += (a-b)/10\n ret += 1 if b < a - ret*10\n end\n puts ret\nend\n"}, {"source_code": "gets.to_i.times do\n a, b = gets.split(' ').map(&:to_i)\n\n d, m = (a-b).abs.divmod(10)\n\n puts d + (m.zero? ? 0 : 1)\nend\n"}, {"source_code": "tc = gets().to_i\ntc.times do\n input = gets().split(' ').map(&:to_i)\n a = input[0]\n b = input[1]\n ans = (((a-b).abs().to_f)/10).ceil()\n puts ans\nend"}], "negative_code": [{"source_code": "t = gets.chomp.to_i\ninputs = []\nt.times do\n inputs << gets.chomp.split.map(&:to_i)\nend\n\ninputs.each do |a,b|\n ret = 0\n if b > a\n ret += (b-a)/10\n ret += b - (a + ret * 10)\n elsif b < a\n ret += (a-b)/10\n ret += a - (b + ret * 10)\n end\n puts ret\nend\n"}, {"source_code": "t = gets.chomp.to_i\ninputs = []\nt.times do\n inputs << gets.chomp.split.map(&:to_i)\nend\n\ninputs.each do |a,b|\n ret = 0\n if b > a\n ret += (b-a)/10\n ret += 1\n elsif b < a\n ret += (a-b)/10\n ret += 1\n end\n puts ret\nend\n"}], "src_uid": "d67a97a3b69d599b03d3fce988980646"} {"nl": {"description": "Burenka came to kindergarden. This kindergarten is quite strange, so each kid there receives two fractions ($$$\\frac{a}{b}$$$ and $$$\\frac{c}{d}$$$) with integer numerators and denominators. Then children are commanded to play with their fractions.Burenka is a clever kid, so she noticed that when she claps once, she can multiply numerator or denominator of one of her two fractions by any integer of her choice (but she can't multiply denominators by $$$0$$$). Now she wants know the minimal number of claps to make her fractions equal (by value). Please help her and find the required number of claps!", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$) — the number of test cases. Then follow the descriptions of each test case. The only line of each test case contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$d$$$ ($$$0 \\leq a, c \\leq 10^9$$$, $$$1 \\leq b, d \\leq 10^9$$$) — numerators and denominators of the fractions given to Burenka initially.", "output_spec": "For each test case print a single integer — the minimal number of claps Burenka needs to make her fractions equal.", "sample_inputs": ["8\n\n2 1 1 1\n\n6 3 2 1\n\n1 2 2 3\n\n0 1 0 100\n\n0 1 228 179\n\n100 3 25 6\n\n999999999 300000000 666666666 100000000\n\n33 15 0 84"], "sample_outputs": ["1\n0\n2\n0\n1\n1\n1\n1"], "notes": "NoteIn the first case, Burenka can multiply $$$c$$$ by $$$2$$$, then the fractions will be equal.In the second case, fractions are already equal.In the third case, Burenka can multiply $$$a$$$ by $$$4$$$, then $$$b$$$ by $$$3$$$. Then the fractions will be equal ($$$\\frac{1 \\cdot 4}{2 \\cdot 3} = \\frac{2}{3}$$$)."}, "positive_code": [{"source_code": "def solve(a, b, c, d)\n x = b*c\n y = a*d\n \n return 0 if x == y\n return 1 if x == 0 || y == 0 || x % y == 0 || y % x == 0\n\n return 2\nend\n\ndef main\n n = gets.chomp.to_i\n n.times do\n a, b, c, d = gets.chomp.split\n puts solve(a.to_i, b.to_i, c.to_i, d.to_i)\n end\nend\n\n\nmain\n"}], "negative_code": [{"source_code": "def solve(a, b, c, d)\n x = b*c\n y = a*d\n \n return 0 if x == y\n return 1 if x == 0 || y == 0 || x % y == 0 || y % x == 0\n\n return 2\nend\n\ndef main\n n = $stdin.gets.to_i\n n.times do\n a = gets.to_i\n b = gets.to_i\n c = gets.to_i\n d = gets.to_i\n\n puts solve(a, b, c, d)\n end\nend\n\n\nmain\n"}], "src_uid": "c2a506d58a80a0fb6a16785605b075ca"} {"nl": {"description": "You are given two integers $$$n$$$ and $$$k$$$. You are asked to choose maximum number of distinct integers from $$$1$$$ to $$$n$$$ so that there is no subset of chosen numbers with sum equal to $$$k$$$.A subset of a set is a set that can be obtained from initial one by removing some (possibly all or none) elements of it.", "input_spec": "The first line contains the number of test cases $$$T$$$ ($$$1 \\le T \\le 100$$$). Each of the next $$$T$$$ lines contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le k \\le n \\le 1000$$$) — the description of test cases.", "output_spec": "For each test case output two lines. In the first line output a single integer $$$m$$$ — the number of chosen integers. In the second line output $$$m$$$ distinct integers from $$$1$$$ to $$$n$$$ — the chosen numbers. If there are multiple answers, print any. You can print the numbers in any order.", "sample_inputs": ["3\n3 2\n5 3\n1 1"], "sample_outputs": ["2\n3 1 \n3\n4 5 2 \n0"], "notes": null}, "positive_code": [{"source_code": "gets.to_i.times do\r\n n, k = gets.split.map &:to_i\r\n puts n - k + k / 2\r\n (k + 1..n).each do |i|\r\n print i, ' '\r\n end\r\n ((k + 1) / 2...k).each do |i|\r\n print i, ' '\r\n end\r\n puts\r\nend\r\n"}], "negative_code": [{"source_code": "gets.to_i.times do\r\n n, k = gets.split.map &:to_i\r\n puts n - k + 1\r\n (k + 1..n).each do |i|\r\n print i, ' '\r\n end\r\n ((k + 1) / 2...k).each do |i|\r\n print i, ' '\r\n end\r\n puts\r\nend\r\n"}, {"source_code": "gets.to_i.times do\r\n n, k = gets.split.map &:to_i\r\n puts n - k + 1\r\n (k + 1..n).each do |i|\r\n print i, ' '\r\n end\r\n ((k + 1) / 2..k).each do |i|\r\n print i, ' '\r\n end\r\n puts\r\nend\r\n"}], "src_uid": "d08e39db62215d7817113aaa384e3f59"} {"nl": {"description": "You are given two lists of segments $$$[al_1, ar_1], [al_2, ar_2], \\dots, [al_n, ar_n]$$$ and $$$[bl_1, br_1], [bl_2, br_2], \\dots, [bl_n, br_n]$$$.Initially, all segments $$$[al_i, ar_i]$$$ are equal to $$$[l_1, r_1]$$$ and all segments $$$[bl_i, br_i]$$$ are equal to $$$[l_2, r_2]$$$.In one step, you can choose one segment (either from the first or from the second list) and extend it by $$$1$$$. In other words, suppose you've chosen segment $$$[x, y]$$$ then you can transform it either into $$$[x - 1, y]$$$ or into $$$[x, y + 1]$$$.Let's define a total intersection $$$I$$$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $$$\\sum\\limits_{i=1}^{n}{\\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$$$. Empty intersection has length $$$0$$$ and length of a segment $$$[x, y]$$$ is equal to $$$y - x$$$.What is the minimum number of steps you need to make $$$I$$$ greater or equal to $$$k$$$?", "input_spec": "The first line contains the single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$; $$$1 \\le k \\le 10^9$$$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $$$l_1$$$ and $$$r_1$$$ ($$$1 \\le l_1 \\le r_1 \\le 10^9$$$) — the segment all $$$[al_i, ar_i]$$$ are equal to initially. The third line of each test case contains two integers $$$l_2$$$ and $$$r_2$$$ ($$$1 \\le l_2 \\le r_2 \\le 10^9$$$) — the segment all $$$[bl_i, br_i]$$$ are equal to initially. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \\cdot 10^5$$$.", "output_spec": "Print $$$t$$$ integers — one per test case. For each test case, print the minimum number of step you need to make $$$I$$$ greater or equal to $$$k$$$.", "sample_inputs": ["3\n3 5\n1 2\n3 4\n2 1000000000\n1 1\n999999999 999999999\n10 3\n5 10\n7 8"], "sample_outputs": ["7\n2000000000\n0"], "notes": "NoteIn the first test case, we can achieve total intersection $$$5$$$, for example, using next strategy: make $$$[al_1, ar_1]$$$ from $$$[1, 2]$$$ to $$$[1, 4]$$$ in $$$2$$$ steps; make $$$[al_2, ar_2]$$$ from $$$[1, 2]$$$ to $$$[1, 3]$$$ in $$$1$$$ step; make $$$[bl_1, br_1]$$$ from $$$[3, 4]$$$ to $$$[1, 4]$$$ in $$$2$$$ steps; make $$$[bl_2, br_2]$$$ from $$$[3, 4]$$$ to $$$[1, 4]$$$ in $$$2$$$ steps. In result, $$$I = \\text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \\text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\\\ + \\text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$$$In the second test case, we can make $$$[al_1, ar_1] = [0, 1000000000]$$$ in $$$1000000000$$$ steps and $$$[bl_1, br_1] = [0, 1000000000]$$$ in $$$1000000000$$$ steps.In the third test case, the total intersection $$$I$$$ is already equal to $$$10 > 3$$$, so we don't need to do any steps."}, "positive_code": [{"source_code": "def read\n gets.split.map &:to_i\nend\ngets.to_i.times do\n n, k = read\n l1, r1 = read\n l2, r2 = read\n if [l1, l2].max <= [r1, r2].min\n rem = [0, k - n * ([r1, r2].min - [l1, l2].max)].max\n mx = n * ((l2 - l1).abs + (r2 - r1).abs)\n res = [rem, mx].min + 2 * [0, rem - mx].max\n else\n res = 1E18.to_i\n add = [l1, l2].max - [r1, r2].min\n (1..n).each do |i|\n cur = add * i\n mx = i * ([r1, r2].max - [l1, l2].min)\n cur += [k, mx].min + 2 * [0, k - mx].max\n res = [res, cur].min\n end\n end\n puts res\nend"}], "negative_code": [], "src_uid": "c8da5d7debf5d7a6fc04bb3a68cda2f0"} {"nl": {"description": "Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.The program cannot delete the letters from the list or rearrange them.Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?", "input_spec": "The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox. The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.", "output_spec": "Print a single number — the minimum number of operations needed to make all the letters read.", "sample_inputs": ["5\n0 1 0 1 0", "5\n1 1 0 0 1", "2\n0 0"], "sample_outputs": ["3", "4", "0"], "notes": "NoteIn the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.In the third sample all letters are already read."}, "positive_code": [{"source_code": "=begin\nDear online judge:\nI've wasted time reading the problem and trying to figure out the solution\nIf you insist on the penalty system, please don't include the time of debugging\nI hope my code compile and get accepted. KEE O.o\n ____________\n / __ \\\n / __ | | \\\n / |__| | | \\\n ( |__| )\n \\ /\n \\ ___ /\n \\____________/\n=end\n\nclass Solve\n def solve()\n $n = Integer(gets)\n $list = gets().split()\n $res = 0\n $count = $list.count(\"1\")\n $n -= 1\n for $i in 0..$n do\n if $list[$i] == \"1\"\n $res+= 1\n $count -= 1\n if $count > 0 && $list[$i + 1]!= \"1\"\n $res+= 1\n end\n end\n end\n puts $res\n end\nend\n\n\n\nSolve.new.solve"}, {"source_code": "#!/usr/bin/ruby\ngets\na=gets.split.map(&:to_i)\np [0,a.count(1)*2-1-a.each_cons(2).count{|x,y|x==1&&y==1}].max"}, {"source_code": "#!/usr/bin/ruby\n\nn = STDIN.readline.to_i\nletters = STDIN.readline.split.collect {|s| s == '1' }\n\nindices = []\nletters.each_index {|i| indices.push(i) if letters[i] }\n\ncount = [1, indices.length].min\n(1...indices.length).each do |i|\n\tcount += [2, indices[i] - indices[i-1]].min\nend\n\nputs count\n\n"}, {"source_code": "#!/usr/bin/ruby\ngets\nputs gets.scan(/1|(?<=1) (0 )+(?=1)/).size\n"}, {"source_code": "n=gets.to_i\nxs=gets.split.map{|e| e.to_i}\np1=xs.index(1)\nif p1==nil then\n\tputs 0\nelse\n\tp1.times{\n\t\txs.shift\n\t}\n\twhile xs.empty? == false\n\t\tt=xs.pop\n\t\tif t==1\n\t\t\txs.push(t)\n\t\t\tbreak\n\t\tend\n\tend\n\t\n\tans=0\n\told=1\n\txs.each{|e|\n\t\tif e==0 then\n\t\t\tif old==1 then\n\t\t\t\tans+=1\n\t\t\tend\n\t\telse\n\t\t\tans+=1\n\t\tend\n\t\told=e\n\t}\n\tputs ans\nend"}, {"source_code": "# -*- coding: utf-8 -*-\nn = gets.to_i\nxs = gets.split.map(&:to_i)\nflag = false\nret = 0\nxs.each do |x|\n if x == 1\n flag = true\n ret += 1\n elsif flag == true && x == 0\n flag = false\n ret += 1\n end\nend\nret -= 1 if xs[-1] == 0 && ret > 0\nputs ret\n\n"}, {"source_code": "n = Integer(gets)\nnums = gets.chomp.split\nletters = []\nfor i in 0...n\n letters[i] = Integer(nums[i])\nend\nop = 0\npointer = 0\nfor i in 0...n-1\n if letters[i] == 1\n op += 1\n if letters[i+1] == 0 && i + 1 != n-1\n pointer = i + 1\n op += 1\n end\n end\nend\nx = 0\nfor j in pointer...n\n if letters[j] != 0\n x = 1\n break\n end\nend\nif x == 0 && op != 0\n op -= 1\nend\nif letters[n-1] == 1\n op += 1\nend\nputs op\n"}, {"source_code": "n = gets.to_i\na = gets.split(/\\s+/).map(&:to_i)\n\nanswer = 0\ni = 0\n\ni += 1 while i < n && a[i] != 1\n\nanswer = 1 if a[i] == 1\ncurrent = i\ni += 1\n\nwhile i < n\n i += 1 while i < n && a[i] != 1\n\n# p [current, i, answer]\n\n if a[i] == 1\n answer += (i - current > 2) ? 2 : i - current\n end\n\n current = i\n i += 1\nend\n\np answer\n"}, {"source_code": "gets # ignore first line\ngroups = gets.strip.tr(' ', '').split(\"0\").delete_if { |x| x.length == 0 }.map(&:length)\nputs [groups.inject(0, :+) + groups.length - 1, 0].max\n"}, {"source_code": "n = gets.chomp.to_i\nx = gets.split()\n\ni = 0\nm = 0\n\n\nfor i in 0..n-1\n if x[i].to_i==1\n m=m+1\n else\n if i>0\n if x[i-1].to_i==1\n m=m+1\n end\n end\n end\n\nend\n\n\nif x[n-1].to_i==0\n if m >0\n m=m-1\n end\nend\nprint m\n"}, {"source_code": "gets\nputs (\"0\"+gets).gsub(/0 ?1/,\"1 1\").count(\"1\").pred.to_s.sub(/-1/,\"0\")"}, {"source_code": "gets\nputs gets.prepend(\"0\").strip.delete(\" \").gsub(/01/,\"11\").count(\"1\").pred.to_s.gsub(/-1/,\"0\")"}, {"source_code": "gets\nputs gets.scan(/1|(?<=1) (0 )+(?=1)/).size"}, {"source_code": "gets\nputs gets.delete(\" \").tr_s('0',' ').strip.length"}, {"source_code": "gets\nputs gets.delete(\" \").squeeze(\"0\").tr('0',' ').strip.length"}], "negative_code": [{"source_code": "#!/usr/bin/ruby\ngets\na=gets.split.map(&:to_i)\np a.count('1')*2-1-a.each_cons(2).count{|x,y|x==1&&y==1}"}, {"source_code": "#!/usr/bin/ruby\ngets\na=gets.split.map(&:to_i)\np a.count(1)*2-1-a.each_cons(2).count{|x,y|x==1&&y==1}"}, {"source_code": "n = Integer(gets)\nnums = gets.chomp.split\nletters = []\nfor i in 0...n\n letters[i] = Integer(nums[i])\nend\nop = 0\npointer = 0\nfor i in 0...n-1\n if letters[i] == 1\n op += 1\n if letters[i+1] == 0 && i + 1 != n-1\n pointer = i + 1\n op += 1\n end\n end\nend\nx = 0\nfor j in pointer...n-1\n if letters != 0\n x = 1\n break\n end\nend\nif x == 0\n op -= 1\nend\nif letters[n-1] == 1\n op += 1\nend\nputs op\n"}, {"source_code": "n = Integer(gets)\nnums = gets.chomp.split\nletters = []\nfor i in 0...n\n letters[i] = Integer(nums[i])\nend\nop = 0\npointer = 0\nfor i in 0...n-1\n if letters[i] == 1\n op += 1\n if letters[i+1] == 0 && i + 1 != n-1\n pointer = i + 1\n op += 1\n end\n end\nend\nx = 0\nfor j in pointer...n-1\n if letters[j] != 0\n x = 1\n break\n end\nend\nif x == 0\n op -= 1\nend\nif letters[n-1] == 1\n op += 1\nend\nputs op\n"}, {"source_code": "n = Integer(gets)\nnums = gets.chomp.split\nletters = []\nfor i in 0...n\n letters[i] = Integer(nums[i])\nend\nop = 0\npointer = 0\nfor i in 0...n-1\n if letters[i] == 1\n op += 1\n if letters[i+1] == 0 && i + 1 != n-1\n pointer = i + 1\n op += 1\n end\n end\nend\nx = 0\nfor j in pointer...n\n if letters[j] != 0\n x = 1\n break\n end\nend\nif x == 0\n op -= 1\nend\nif letters[n-1] == 1\n op += 1\nend\nputs op\n"}, {"source_code": "n = Integer(gets)\nnums = gets.chomp.split\nletters = []\nfor i in 0...n\n letters[i] = Integer(nums[i])\nend\nop = 0\nfor i in 0...n-1\n if letters[i] == 1\n op += 1\n if letters[i+1] == 0 && i + 1 != n-1\n op += 1\n end\n end\nend\nif letters[n-1] == 1\n op += 1\nend\nputs op\n"}, {"source_code": "n = gets.chomp.to_i\nx = gets.split()\n\ni = 0\nm = 0\n\n\nfor i in 0..n-1\n if x[i].to_i==1\n m=m+1\n end\n\n\nif x[i].to_i==0\n if x[i-1].to_i==1\n m=m+1\n end\nend\n\nend\n\n\n\n\nif x[n-1].to_i==0\n if m >0\nm=m-1\nend\nend\nprint m\n"}, {"source_code": "n = gets.chomp.to_i\nx = gets.split()\n\ni = 0\nm = 0\n\n\nfor i in 0..n\n if x[i].to_i==1\n m=m+1\n end\nif x[i].to_i==0\n if x[i-1].to_i==1\n m=m+1\n end\nend\nend\n\nif x[n].to_i==0\n if m>1\nm=m-1\nend\nend\nprint m\n"}, {"source_code": "n = gets.chomp.to_i\nx = gets.split()\n\ni = 0\nm = 0\n\n\nfor i in 0..n-1\n if x[i].to_i==1\n m=m+1\n end\nif x[i].to_i==0\n if x[i-1].to_i==1\n m=m+1\n end\nend\nend\n\nif x[n-1].to_i==0\n if m>1\nm=m-1\nend\nend\nprint m\n"}, {"source_code": "n = gets.chomp.to_i\nx = gets.split()\n\ni = 0\nm = 0\n\n\nfor i in 0..n-1\n if x[i].to_i==1\n m=m+1\n else\n if x[i-1].to_i==1\n m=m+1\n end\n end\n\nend\n\n\nif x[n-1].to_i==0\n if m >0\n m=m-1\n end\nend\nprint m\n"}, {"source_code": "gets\nputs gets.split.join.tr_s('0',' ').split.length"}, {"source_code": "gets\nputs gets.split(%r{ ?(0 )+}).join.split.length"}, {"source_code": "gets\nputs gets.scan(/1|(?<=1) 0/).size"}, {"source_code": "gets\nputs gets.gsub(/1(?! 1)/,'11').count('1')"}, {"source_code": "gets\nputs gets.scan(/1|(?<=1).{2,}?(?=1)/).size"}], "src_uid": "0fbc306d919d7ffc3cb02239cb9c2ab0"} {"nl": {"description": "Numbers $$$1, 2, 3, \\dots n$$$ (each integer from $$$1$$$ to $$$n$$$ once) are written on a board. In one operation you can erase any two numbers $$$a$$$ and $$$b$$$ from the board and write one integer $$$\\frac{a + b}{2}$$$ rounded up instead.You should perform the given operation $$$n - 1$$$ times and make the resulting number that will be left on the board as small as possible. For example, if $$$n = 4$$$, the following course of action is optimal: choose $$$a = 4$$$ and $$$b = 2$$$, so the new number is $$$3$$$, and the whiteboard contains $$$[1, 3, 3]$$$; choose $$$a = 3$$$ and $$$b = 3$$$, so the new number is $$$3$$$, and the whiteboard contains $$$[1, 3]$$$; choose $$$a = 1$$$ and $$$b = 3$$$, so the new number is $$$2$$$, and the whiteboard contains $$$[2]$$$. It's easy to see that after $$$n - 1$$$ operations, there will be left only one number. Your goal is to minimize it.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. The only line of each test case contains one integer $$$n$$$ ($$$2 \\le n \\le 2 \\cdot 10^5$$$) — the number of integers written on the board initially. It's guaranteed that the total sum of $$$n$$$ over test cases doesn't exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, in the first line, print the minimum possible number left on the board after $$$n - 1$$$ operations. Each of the next $$$n - 1$$$ lines should contain two integers — numbers $$$a$$$ and $$$b$$$ chosen and erased in each operation.", "sample_inputs": ["1\n4"], "sample_outputs": ["2\n2 4\n3 3\n3 1"], "notes": null}, "positive_code": [{"source_code": "T = gets.to_i\n\nT.times do\n n = gets.to_i\n stack = [*1..n]\n\n puts 2\n (n - 1).times do\n a, b = stack.pop(2)\n\n puts \"%d %d\" % [a, b]\n stack.push((a + b + 1) / 2)\n end\nend\n"}], "negative_code": [], "src_uid": "591372383cf3624f69793c41370022de"} {"nl": {"description": "It's now 260 AD. Shapur, being extremely smart, became the King of Persia. He is now called Shapur, His majesty King of kings of Iran and Aniran.Recently the Romans declared war on Persia. They dreamed to occupy Armenia. In the recent war, the Romans were badly defeated. Now their senior army general, Philip is captured by Shapur and Shapur is now going to capture Valerian, the Roman emperor.Being defeated, the cowardly Valerian hid in a room at the top of one of his castles. To capture him, Shapur has to open many doors. Fortunately Valerian was too scared to make impenetrable locks for the doors.Each door has 4 parts. The first part is an integer number a. The second part is either an integer number b or some really odd sign which looks like R. The third one is an integer c and the fourth part is empty! As if it was laid for writing something. Being extremely gifted, after opening the first few doors, Shapur found out the secret behind the locks.c is an integer written in base a, to open the door we should write it in base b. The only bad news is that this R is some sort of special numbering system that is used only in Roman empire, so opening the doors is not just a piece of cake!Here's an explanation of this really weird number system that even doesn't have zero:Roman numerals are based on seven symbols: a stroke (identified with the letter I) for a unit, a chevron (identified with the letter V) for a five, a cross-stroke (identified with the letter X) for a ten, a C (identified as an abbreviation of Centum) for a hundred, etc.: I=1 V=5 X=10 L=50 C=100 D=500 M=1000Symbols are iterated to produce multiples of the decimal (1, 10, 100, 1, 000) values, with V, L, D substituted for a multiple of five, and the iteration continuing: I 1, II 2, III 3, V 5, VI 6, VII 7, etc., and the same for other bases: X 10, XX 20, XXX 30, L 50, LXXX 80; CC 200, DCC 700, etc. At the fourth and ninth iteration, a subtractive principle must be employed, with the base placed before the higher base: IV 4, IX 9, XL 40, XC 90, CD 400, CM 900.Also in bases greater than 10 we use A for 10, B for 11, etc.Help Shapur capture Valerian and bring peace back to Persia, especially Armenia.", "input_spec": "The first line contains two integers a and b (2 ≤ a, b ≤ 25). Only b may be replaced by an R which indicates Roman numbering system. The next line contains a single non-negative integer c in base a which may contain leading zeros but its length doesn't exceed 103. It is guaranteed that if we have Roman numerals included the number would be less than or equal to 300010 and it won't be 0. In any other case the number won't be greater than 101510.", "output_spec": "Write a single line that contains integer c in base b. You must omit leading zeros.", "sample_inputs": ["10 2\n1", "16 R\n5", "5 R\n4", "2 2\n1111001", "12 13\nA"], "sample_outputs": ["1", "V", "IV", "1111001", "A"], "notes": "NoteYou can find more information about roman numerals here: http://en.wikipedia.org/wiki/Roman_numerals"}, "positive_code": [{"source_code": "def to_r(n)\n\tif n < 1\n\t\t''\n\telsif n < 10\n\t\t%w(I II III IV V VI VII VIII IX)[n - 1]\n\telsif n < 100\n\t\t%w(X XX XXX XL L LX LXX LXXX XC)[n / 10 - 1] + to_r(n % 10)\n\telsif n < 1000\n\t\t%w(C CC CCC CD D DC DCC DCCC CM)[n / 100 - 1] + to_r(n % 100)\n\telse\n\t\t%w(M MM MMM)[n / 1000 - 1] + to_r(n % 1000)\n\tend\nend\n\na, b = gets.split.map(&:to_i)\nn = gets.to_i(a)\nputs b > 0 ? n.to_s(b).upcase : to_r(n)\n"}, {"source_code": "include Math\nrequire \"prime\"\nrequire \"bigdecimal\"\nrequire \"bigdecimal/math\"\nrequire \"matrix\"\nrequire \"pp\"\nrequire \"benchmark\"\n\nInfinity = 1/0.0\n\nalias p! p\ndef p(*objs)\n def onep(obj)\n case obj\n when Matrix\n puts\"Matrix[\"\n obj.to_a.each{|row|p row}\n puts\"]\"\n else\n p! obj\n end\n end\n objs.each{|obj|\n onep obj\n }\nend\ndef pbin(obj);case obj;when Integer;puts obj.to_s(2);end;end\nclass String;def to_ia;self.split.map(&:to_i);end;end\ndef getia;gets.to_ia;end\ndef geti;gets.to_i;end\nclass Object;def dcopy;Marshal.load(Marshal.dump(self));end;end\n\nclass Integer\n RomaTbl = {1 => 'I', 4 => 'IV', 5 => 'V', 9=> 'IX', 10 => 'X',\n 40 => 'XL', 50 => 'L', 90 => 'XC', 100 => 'C', 400 => 'CD',\n 500 => 'D', 900=> 'CM', 1000 => 'M'}\n def roman(s=self)\n return \"-\" if s==0\n k = RomaTbl.keys.delete_if{|x| x > s}.sort[-1]\n return RomaTbl[k] + roman(s - k).gsub('-','')\n end\nend\n\nf=gets\na,b=f.split(\" \")\n\nn=gets.to_i(a.to_i)\n\nif b==\"R\"\n puts n.roman\nelse\n puts n.to_s(b.to_i).upcase\nend\n\n"}, {"source_code": "def f(n)\n m, c, x, l = [1000, 100, 10, 1].map { |t| r = (n/t).floor; n %= t; r }\n 'M'*m +\n case c\n when 9 then 'CM'\n when 5..8 then 'D' + 'C'*(c-5)\n when 4 then 'CD'\n when 1..3 then 'C'*c\n else ''\n end +\n case x\n when 9 then 'XC'\n when 5..8 then 'L' + 'X'*(x-5)\n when 4 then 'XL'\n when 1..3 then 'X'*x\n else ''\n end +\n case l\n when 9 then 'IX'\n when 5..8 then 'V' + 'I'*(l-5)\n when 4 then 'IV'\n when 1..3 then 'I'*l\n else ''\n end\nend\n\na, b = gets.split\nc = gets.to_i(a.to_i)\nputs (b == 'R' ? f(c) : c.to_s(b.to_i).upcase)\n"}], "negative_code": [{"source_code": "include Math\nrequire \"prime\"\nrequire \"bigdecimal\"\nrequire \"bigdecimal/math\"\nrequire \"matrix\"\nrequire \"pp\"\nrequire \"benchmark\"\n\nInfinity = 1/0.0\n\nalias p! p\ndef p(*objs)\n def onep(obj)\n case obj\n when Matrix\n puts\"Matrix[\"\n obj.to_a.each{|row|p row}\n puts\"]\"\n else\n p! obj\n end\n end\n objs.each{|obj|\n onep obj\n }\nend\ndef pbin(obj);case obj;when Integer;puts obj.to_s(2);end;end\nclass String;def to_ia;self.split.map(&:to_i);end;end\ndef getia;gets.to_ia;end\ndef geti;gets.to_i;end\nclass Object;def dcopy;Marshal.load(Marshal.dump(self));end;end\n\nclass Integer\n RomaTbl = {1 => 'I', 4 => 'IV', 5 => 'V', 9=> 'IX', 10 => 'X',\n 40 => 'XL', 50 => 'L', 90 => 'XC', 100 => 'C', 400 => 'CD',\n 500 => 'D', 900=> 'CM', 1000 => 'M'}\n def roman(s=self)\n return \"-\" if s==0\n k = RomaTbl.keys.delete_if{|x| x > s}.sort[-1]\n return RomaTbl[k] + roman(s - k).gsub('-','')\n end\nend\n\nf=gets\na,b=f.split(\" \")\n\nn=gets.to_i(a.to_i)\n\nif b==\"R\"\n puts n.roman\nelse\n puts n.to_s(b.to_i)\nend\n\n"}, {"source_code": "def f(n)\n m, c, x, l = [1000, 100, 10, 1].map { |t| r = (n/t).floor; n %= t; r }\n 'M'*m +\n case c\n when 9 then 'CM'\n when 5..8 then 'D' + 'C'*(c-5)\n when 4 then 'CD'\n when 1..3 then 'C'*c\n else ''\n end +\n case x\n when 9 then 'XC'\n when 5..8 then 'L' + 'X'*(x-5)\n when 4 then 'XL'\n when 1..3 then 'X'*x\n else ''\n end +\n case l\n when 9 then 'IX'\n when 5..8 then 'V' + 'I'*(l-5)\n when 4 then 'IV'\n when 1..3 then 'I'*l\n else ''\n end\nend\n\na, b = gets.split\nc = gets.to_i(a.to_i)\nputs (b == 'R' ? f(c) : c.to_s(b.to_i))\n"}], "src_uid": "c619d699e3e5fb42aea839ef6080c86c"} {"nl": {"description": "A penguin Rocher has $$$n$$$ sticks. He has exactly one stick with length $$$i$$$ for all $$$1 \\le i \\le n$$$.He can connect some sticks. If he connects two sticks that have lengths $$$a$$$ and $$$b$$$, he gets one stick with length $$$a + b$$$. Two sticks, that were used in the operation disappear from his set and the new connected stick appears in his set and can be used for the next connections.He wants to create the maximum number of sticks that have the same length. It is not necessary to make all sticks have the same length, some sticks can have the other length. How many sticks with the equal length he can create?", "input_spec": "The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. For each test case, the only line contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^{9}$$$).", "output_spec": "For each test case, print a single integer  — the answer to the problem.", "sample_inputs": ["4\n1\n2\n3\n4"], "sample_outputs": ["1\n1\n2\n2"], "notes": "NoteIn the third case, he can connect two sticks with lengths $$$1$$$ and $$$2$$$ and he will get one stick with length $$$3$$$. So, he will have two sticks with lengths $$$3$$$.In the fourth case, he can connect two sticks with lengths $$$1$$$ and $$$3$$$ and he will get one stick with length $$$4$$$. After that, he will have three sticks with lengths $$$\\{2, 4, 4\\}$$$, so two sticks have the same length, and one stick has the other length."}, "positive_code": [{"source_code": "t = gets.to_i\nt.times do\n\tn = gets.to_i\n\tp (n + 1) / 2\nend\n"}, {"source_code": "require 'set'; require 'prime'; require 'pp'\nINF=Float::INFINITY; MOD=10**9+7\nt = gets.chomp.to_i\nt.times do\n n = gets.chomp.to_i\n puts (n+1)/2\nend\n"}, {"source_code": "n = gets.chomp.to_i\nn.times do \n t = gets.chomp.to_i\n if t % 2 == 1 && t >= 3\n kai = t -1\n kazu = kai / 2\n puts kazu + 1\n elsif t % 2 == 1 && t < 3\n puts \"1\"\n elsif t % 2 == 0 && t >= 2\n puts t / 2\n end\nend"}, {"source_code": "t=gets.chomp.to_i\nfor i in 1..t\n\tn=gets.chomp.to_i\n\tputs\"#{(1+n)/2}\"\nend"}, {"source_code": "T = gets.to_i\nfor i in 1..T\n x = gets.to_i\n puts (x + 1) / 2\nend"}, {"source_code": "def f(n)\n return 0 if n.zero?\n n.even? ? n / 2 : f(n - 1) + 1\nend\n\ngets.to_i.times do\n puts f(gets.to_i)\nend\n"}, {"source_code": "\n\n\ndef get_res(n)\n res = n / 2\n res += 1 if (n % 2 == 1)\n return res\nend\n\n\ndef solve\n t = gets.chomp\n (1..t.to_i).each {puts get_res(gets.chomp.to_i)}\nend\n\nsolve"}], "negative_code": [{"source_code": "def f(n)\n return 1 if n == 1\n return 2 if n == 3\n\n n / 2\nend\n\ngets.to_i.times do\n puts f(gets.to_i)\nend\n"}], "src_uid": "ec89860dacb5a11b3a2273d2341b07a1"} {"nl": {"description": "You went to the store, selling $$$n$$$ types of chocolates. There are $$$a_i$$$ chocolates of type $$$i$$$ in stock.You have unlimited amount of cash (so you are not restricted by any prices) and want to buy as many chocolates as possible. However if you buy $$$x_i$$$ chocolates of type $$$i$$$ (clearly, $$$0 \\le x_i \\le a_i$$$), then for all $$$1 \\le j < i$$$ at least one of the following must hold: $$$x_j = 0$$$ (you bought zero chocolates of type $$$j$$$) $$$x_j < x_i$$$ (you bought less chocolates of type $$$j$$$ than of type $$$i$$$) For example, the array $$$x = [0, 0, 1, 2, 10]$$$ satisfies the requirement above (assuming that all $$$a_i \\ge x_i$$$), while arrays $$$x = [0, 1, 0]$$$, $$$x = [5, 5]$$$ and $$$x = [3, 2]$$$ don't.Calculate the maximum number of chocolates you can buy.", "input_spec": "The first line contains an integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$), denoting the number of types of chocolate. The next line contains $$$n$$$ integers $$$a_i$$$ ($$$1 \\le a_i \\le 10^9$$$), denoting the number of chocolates of each type.", "output_spec": "Print the maximum number of chocolates you can buy.", "sample_inputs": ["5\n1 2 1 3 6", "5\n3 2 5 4 10", "4\n1 1 1 1"], "sample_outputs": ["10", "20", "1"], "notes": "NoteIn the first example, it is optimal to buy: $$$0 + 0 + 1 + 3 + 6$$$ chocolates.In the second example, it is optimal to buy: $$$1 + 2 + 3 + 4 + 10$$$ chocolates.In the third example, it is optimal to buy: $$$0 + 0 + 0 + 1$$$ chocolates."}, "positive_code": [{"source_code": "n = gets.to_i\na = gets.split.map(&:to_i).reverse\n\np = 10**9+1\nans = 0\na.each do |x|\n x = [x, p - 1].min\n ans += x\n p = [1, x].max\nend\nputs ans\n"}, {"source_code": "def main\n n = gets.chomp\n a = gets.chomp.split.map(&:to_i).reverse\n\n m = 0\n a.inject(nil) do |prev, x|\n if prev.nil? or prev > x\n m += x\n x \n else\n q = prev - 1 >= 0 ? prev - 1 : 0\n m += q\n q\n end\n end\n\n puts m\nend\n\nmain\n"}, {"source_code": "n = gets\na = gets.split(\" \").map{|s| s.to_i}\nputs a.reverse\n .inject([0, a[-1] + 1]) {|r, e| \n r[1] = [[r[1] - 1, e].min, 0].max \n r[0] += r[1]\n r\n }[0]"}], "negative_code": [], "src_uid": "5993b5bf231fabd3c2af90124c41f118"} {"nl": {"description": "There are $$$n$$$ people sitting in a circle, numbered from $$$1$$$ to $$$n$$$ in the order in which they are seated. That is, for all $$$i$$$ from $$$1$$$ to $$$n-1$$$, the people with id $$$i$$$ and $$$i+1$$$ are adjacent. People with id $$$n$$$ and $$$1$$$ are adjacent as well.The person with id $$$1$$$ initially has a ball. He picks a positive integer $$$k$$$ at most $$$n$$$, and passes the ball to his $$$k$$$-th neighbour in the direction of increasing ids, that person passes the ball to his $$$k$$$-th neighbour in the same direction, and so on until the person with the id $$$1$$$ gets the ball back. When he gets it back, people do not pass the ball any more.For instance, if $$$n = 6$$$ and $$$k = 4$$$, the ball is passed in order $$$[1, 5, 3, 1]$$$. Consider the set of all people that touched the ball. The fun value of the game is the sum of the ids of people that touched it. In the above example, the fun value would be $$$1 + 5 + 3 = 9$$$.Find and report the set of possible fun values for all choices of positive integer $$$k$$$. It can be shown that under the constraints of the problem, the ball always gets back to the $$$1$$$-st player after finitely many steps, and there are no more than $$$10^5$$$ possible fun values for given $$$n$$$.", "input_spec": "The only line consists of a single integer $$$n$$$ ($$$2 \\leq n \\leq 10^9$$$) — the number of people playing with the ball.", "output_spec": "Suppose the set of all fun values is $$$f_1, f_2, \\dots, f_m$$$. Output a single line containing $$$m$$$ space separated integers $$$f_1$$$ through $$$f_m$$$ in increasing order.", "sample_inputs": ["6", "16"], "sample_outputs": ["1 5 9 21", "1 10 28 64 136"], "notes": "NoteIn the first sample, we've already shown that picking $$$k = 4$$$ yields fun value $$$9$$$, as does $$$k = 2$$$. Picking $$$k = 6$$$ results in fun value of $$$1$$$. For $$$k = 3$$$ we get fun value $$$5$$$ and with $$$k = 1$$$ or $$$k = 5$$$ we get $$$21$$$. In the second sample, the values $$$1$$$, $$$10$$$, $$$28$$$, $$$64$$$ and $$$136$$$ are achieved for instance for $$$k = 16$$$, $$$8$$$, $$$4$$$, $$$10$$$ and $$$11$$$, respectively."}, "positive_code": [{"source_code": "require \"prime\"\nN = gets.to_i\n$ans = []\nQS = N.prime_division\n\ndef dig(v = 1,i = 0)\n if i == QS.size\n n = N/v\n s = (1 + ((n-1) * v + 1))*n/2\n $ans << s\n return\n end\n \n q,d = QS[i]\n b = 1\n (0..d).each{ |d|\n dig(v*b,i+1)\n b *= q\n }\nend\ndig\nputs $ans.sort.join(\" \")\n"}, {"source_code": "require 'prime'\n\nn = gets.to_i\n$f = Prime.prime_division(n)\n#p $f\n$d = []\n$x = []\n\na = []\n\ndef bf(depth)\n if depth == $f.size\n #p $x\n s = 1\n $f.each_with_index do |e, i|\n s *= e[0]**$x[i]\n end\n $d << s\n #p s\n return\n end\n (0..$f[depth][1]).each do |i|\n $x[depth] = i\n bf(depth + 1)\n end\nend\n\nbf(0)\n$d.uniq!\n#p $d\n$d.each do |d|\n a1 = 1\n m = n/d\n am = a1 + (m - 1)*d\n a << (a1 + am)*m/2\n# p (a1 + am)*m/2\nend\n\na.sort!\nputs a.join(' ')\n"}], "negative_code": [], "src_uid": "2ae1a4d4f2e58b359c898d1ff38edb9e"} {"nl": {"description": "A permutation of length $$$n$$$ is an array $$$p=[p_1,p_2,\\dots,p_n]$$$, which contains every integer from $$$1$$$ to $$$n$$$ (inclusive) and, moreover, each number appears exactly once. For example, $$$p=[3,1,4,2,5]$$$ is a permutation of length $$$5$$$.For a given number $$$n$$$ ($$$n \\ge 2$$$), find a permutation $$$p$$$ in which absolute difference (that is, the absolute value of difference) of any two neighboring (adjacent) elements is between $$$2$$$ and $$$4$$$, inclusive. Formally, find such permutation $$$p$$$ that $$$2 \\le |p_i - p_{i+1}| \\le 4$$$ for each $$$i$$$ ($$$1 \\le i < n$$$).Print any such permutation for the given integer $$$n$$$ or determine that it does not exist.", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case is described by a single line containing an integer $$$n$$$ ($$$2 \\le n \\le 1000$$$).", "output_spec": "Print $$$t$$$ lines. Print a permutation that meets the given requirements. If there are several such permutations, then print any of them. If no such permutation exists, print -1.", "sample_inputs": ["6\n10\n2\n4\n6\n7\n13"], "sample_outputs": ["9 6 10 8 4 7 3 1 5 2 \n-1\n3 1 4 2 \n5 3 6 2 4 1 \n5 1 3 6 2 4 7 \n13 9 7 11 8 4 1 3 5 2 6 10 12"], "notes": null}, "positive_code": [{"source_code": "gets.to_i.times{\n n=gets.to_i\n if n<4\n puts -1\n else\n res=[3, 1, 4, 2]\n (5..n).each{|i|\n if i.odd?\n res.unshift i\n else\n res.push i\n end\n }\n puts res*' '\n end\n}\n"}], "negative_code": [], "src_uid": "9b8a5d9a6cfd6b3b5d0839eeece6f777"} {"nl": {"description": "Anna is a girl so brave that she is loved by everyone in the city and citizens love her cookies. She is planning to hold a party with cookies. Now she has $$$a$$$ vanilla cookies and $$$b$$$ chocolate cookies for the party.She invited $$$n$$$ guests of the first type and $$$m$$$ guests of the second type to the party. They will come to the party in some order. After coming to the party, each guest will choose the type of cookie (vanilla or chocolate) to eat. There is a difference in the way how they choose that type:If there are $$$v$$$ vanilla cookies and $$$c$$$ chocolate cookies at the moment, when the guest comes, then if the guest of the first type: if $$$v>c$$$ the guest selects a vanilla cookie. Otherwise, the guest selects a chocolate cookie. if the guest of the second type: if $$$v>c$$$ the guest selects a chocolate cookie. Otherwise, the guest selects a vanilla cookie. After that: If there is at least one cookie of the selected type, the guest eats one. Otherwise (there are no cookies of the selected type), the guest gets angry and returns to home. Anna wants to know if there exists some order of guests, such that no one guest gets angry. Your task is to answer her question.", "input_spec": "The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. For each test case, the only line contains four integers $$$a$$$, $$$b$$$, $$$n$$$, $$$m$$$ ($$$0 \\le a,b,n,m \\le 10^{18}, n+m \\neq 0$$$).", "output_spec": "For each test case, print the answer in one line. If there exists at least one valid order, print \"Yes\". Otherwise, print \"No\". You can print each letter in any case (upper or lower).", "sample_inputs": ["6\n2 2 1 2\n0 100 0 1\n12 13 25 1\n27 83 14 25\n0 0 1 0\n1000000000000000000 1000000000000000000 1000000000000000000 1000000000000000000"], "sample_outputs": ["Yes\nNo\nNo\nYes\nNo\nYes"], "notes": "NoteIn the first test case, let's consider the order $$$\\{1, 2, 2\\}$$$ of types of guests. Then: The first guest eats a chocolate cookie. After that, there are $$$2$$$ vanilla cookies and $$$1$$$ chocolate cookie. The second guest eats a chocolate cookie. After that, there are $$$2$$$ vanilla cookies and $$$0$$$ chocolate cookies. The last guest selects a chocolate cookie, but there are no chocolate cookies. So, the guest gets angry. So, this order can't be chosen by Anna.Let's consider the order $$$\\{2, 2, 1\\}$$$ of types of guests. Then: The first guest eats a vanilla cookie. After that, there is $$$1$$$ vanilla cookie and $$$2$$$ chocolate cookies. The second guest eats a vanilla cookie. After that, there are $$$0$$$ vanilla cookies and $$$2$$$ chocolate cookies. The last guest eats a chocolate cookie. After that, there are $$$0$$$ vanilla cookies and $$$1$$$ chocolate cookie. So, the answer to this test case is \"Yes\".In the fifth test case, it is illustrated, that the number of cookies ($$$a + b$$$) can be equal to zero, but the number of guests ($$$n + m$$$) can't be equal to zero.In the sixth test case, be careful about the overflow of $$$32$$$-bit integer type."}, "positive_code": [{"source_code": "require 'set'; require 'prime'; require 'pp'\nINF=Float::INFINITY; MOD=10**9+7\nt = gets.chomp.to_i\nt.times do\n v,c,n,m = gets.chomp.split.map(&:to_i)\n v,c=c,v if v > c\n if v < m\n puts 'No'\n else\n v -= m\n if v + c < n\n puts 'No'\n else\n puts 'Yes'\n end\n end\nend\n"}, {"source_code": "s = gets.chomp.to_i\ns.times do\n a, b, n, m = gets.chomp.split(\" \").map(&:to_i)\n if a > b\n kazu = b - m\n if kazu < 0\n #no += 1\n puts \"No\"\n next\n elsif kazu >= 0\n all = kazu + a\n if all >= n\n #yes += 1\n puts \"Yes\"\n next\n else\n puts \"No\"\n next\n #no += 1\n end\n end\n elsif a < b\n kazu = a - m\n if kazu < 0\n #no += 1\n puts \"No\"\n next\n elsif kazu >= 0\n all = kazu + b\n if all >= n\n puts \"Yes\"\n next\n #yes += 1\n else\n puts \"No\"\n next\n #no += 1\n end\n end\n elsif a == b\n kazu = a - m\n if kazu < 0\n puts \"No\"\n next\n #no += 1\n elsif kazu >= 0\n all = kazu + b\n if all >= n\n puts \"Yes\"\n next\n #yes += 1\n else\n puts \"No\"\n next\n #no += 1\n end\n end\n end\nend"}], "negative_code": [], "src_uid": "0f960d19e576b7421a7c7a7166a884ea"} {"nl": {"description": "Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.", "input_spec": "The first line of the input contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \\leq p, a, b, c \\leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.", "output_spec": "For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.", "sample_inputs": ["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"], "sample_outputs": ["1\n4\n0\n8"], "notes": "NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \\ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \\ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \\ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \\ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \\ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \\ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \\ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side."}, "positive_code": [{"source_code": "inputs = STDIN.read.strip.split(\"\\n\")\r\ninputs.shift\r\n\r\ninputs.each do |inp|\r\n p, a, b, c = inp.split(' ').map(&:to_i)\r\n\r\n r = []\r\n [a, b, c].each do |n|\r\n r << ((p / n) + [p % n, 1].min) * n\r\n end\r\n\r\n puts r.min - p\r\nend"}, {"source_code": "inputs = STDIN.read.strip.split(\"\\n\")\r\ninputs.shift\r\n\r\ninputs.each do |inp|\r\n p, a, b, c = inp.split(' ').map(&:to_i)\r\n\r\n r = []\r\n [a, b, c].each do |n|\r\n r << ((p / n) + (p % n == 0 ? 0 : 1)) * n\r\n end\r\n\r\n puts r.min - p\r\nend"}, {"source_code": "n = gets.to_i\na = []\n\ndef get_min(arr)\n\t[compare(arr[0],arr[1]), compare(arr[0],arr[2]), compare(arr[0],arr[3])].min\nend\n\ndef compare(a,b)\n return b-a if a < b\n\t\n\t-a % b\nend\n\n(0..n-1).each do |x|\n\tq = gets.chomp.split().map { |e| e.to_i }\n\ta.append(get_min(q))\nend\n\n\na.each do |x|\n p x\nend\n\n"}, {"source_code": "f = lambda {|x,p| p%x==0? 0:(x-(p%x))}\ngets.to_i.times {\n\tp,a,b,c = gets.split.map(&:to_i)\n\tputs [a,b,c].map{|x| f.call(x,p)}.min\n}"}, {"source_code": "t = gets.to_i\r\nans = []\r\nt.times do\r\n p,a,b,c = gets.chomp.split(\" \").map(&:to_i)\r\n min = [((p+a-1)/a)*a-p,((p+b-1)/b)*b-p,((p+c-1)/c)*c-p].min\r\n ans << min\r\nend\r\nputs ans"}, {"source_code": "gets.to_i.times do\r\n q,a,b,c=gets.split.map(&:to_i)\r\n puts [(a-q%a)%a,(b-q%b)%b,(c-q%c)%c].min\r\nend\r\n"}], "negative_code": [{"source_code": "inputs = STDIN.read.strip.split(\"\\n\")\r\ninputs.shift\r\n\r\ninputs.each do |inp|\r\n p, a, b, c = inp.split(' ').map(&:to_i)\r\n\r\n r = []\r\n [a, b, c].each do |n|\r\n r << (p / n) * n\r\n end\r\n\r\n puts r.min - p\r\nend"}, {"source_code": "inputs = STDIN.read.strip.split(\"\\n\")\r\ninputs.shift\r\n\r\ninputs.each do |inp|\r\n p, a, b, c = inp.split(' ').map(&:to_i)\r\n\r\n r = []\r\n [a, b, c].each do |n|\r\n r << (p.to_f / n.to_f).ceil * n\r\n end\r\n\r\n puts r.min - p\r\nend"}, {"source_code": "n = gets.to_i\r\na = []\r\n\r\ndef get_min(arr)\r\n\t[compare(arr[0],arr[1]), compare(arr[0],arr[2]), compare(arr[0],arr[3])].min\r\nend\r\n\r\ndef compare(a,b)\r\n return b-a if a < b\r\n \r\n while a / b != 0\r\n \tb+=b\r\n end\r\n\t\r\n\tb % a\t\r\nend\r\n\r\n(0..n-1).each do |x|\r\n\tq = gets.chomp.split().map { |e| e.to_i }\r\n\ta.append(get_min(q))\r\nend\r\n\r\n\r\na.each do |x|\r\n p x\r\nend"}], "src_uid": "293f9b996eee9f76d4bfdeda85685baa"} {"nl": {"description": "The city Valera lives in is going to hold elections to the city Parliament.The city has n districts and n - 1 bidirectional roads. We know that from any district there is a path along the roads to any other district. Let's enumerate all districts in some way by integers from 1 to n, inclusive. Furthermore, for each road the residents decided if it is the problem road or not. A problem road is a road that needs to be repaired.There are n candidates running the elections. Let's enumerate all candidates in some way by integers from 1 to n, inclusive. If the candidate number i will be elected in the city Parliament, he will perform exactly one promise — to repair all problem roads on the way from the i-th district to the district 1, where the city Parliament is located.Help Valera and determine the subset of candidates such that if all candidates from the subset will be elected to the city Parliament, all problem roads in the city will be repaired. If there are several such subsets, you should choose the subset consisting of the minimum number of candidates.", "input_spec": "The first line contains a single integer n (2 ≤ n ≤ 105) — the number of districts in the city. Then n - 1 lines follow. Each line contains the description of a city road as three positive integers xi, yi, ti (1 ≤ xi, yi ≤ n, 1 ≤ ti ≤ 2) — the districts connected by the i-th bidirectional road and the road type. If ti equals to one, then the i-th road isn't the problem road; if ti equals to two, then the i-th road is the problem road. It's guaranteed that the graph structure of the city is a tree.", "output_spec": "In the first line print a single non-negative number k — the minimum size of the required subset of candidates. Then on the second line print k space-separated integers a1, a2, ... ak — the numbers of the candidates that form the required subset. If there are multiple solutions, you are allowed to print any of them.", "sample_inputs": ["5\n1 2 2\n2 3 2\n3 4 2\n4 5 2", "5\n1 2 1\n2 3 2\n2 4 1\n4 5 1", "5\n1 2 2\n1 3 2\n1 4 2\n1 5 2"], "sample_outputs": ["1\n5", "1\n3", "4\n5 4 3 2"], "notes": null}, "positive_code": [{"source_code": "require 'set'\nn=gets.to_i\nh=Hash.new{|h,k|h[k]={}}\n(n-1).times do\n a,b,t=gets.split.map(&:to_i)\n h[a][b]=h[b][a]=t\nend\ns=[[1,0,1]]\nr=Set.new\nn=Set.new\nwhile (x=s.last)\n if x[1]==0\n h[x[0]].each{|k,v|s.push([k,0,v]);h[k].delete(x[0])}\n x[1]=1\n else\n s.pop\n if h[x[0]].any?{|k,v|n.include?(k)}\n n<0}.reverse\nout<0}\nout<<\"\\n\"\no=\"\"\na.size.times{|i| \n\tha[a[i]]-=1\n\tbreak if p.size==0\n\tif h[a[i]]<0&&(p[-1]0}.reverse\nout<0}\nout<<\"\\n\"\no=\"\"\na.size.times{|i| \n\tbreak if p.size==0\n\tif h[a[i]]<0&&a[i]>p[-1]\n\t\th[a[i]]+=1\n\t\ta[i]=p[-1]\n\t\th[p[-1]]-=1\n\tend\n\tp.pop if h[p[-1]]==0 \n}\np.reverse!\n(a.size-1).downto(0).each{|i|\n\tbreak if p.size==0\n\tif h[a[i]]<0\n\t\ta[i]=p[-1]\n\t\th[p[-1]]-=1\n\tend\n\tp.pop if h[p[-1]]==0 \n}\nout< \"D\", \"D\" => \"U\", \"L\" => \"R\", \"R\" => \"L\"}\notp = 0\nset = Set.new\ns.each_char do |i|\n x = i\n if(set.include?(rev[x])) || ((set.size() == 2)&& !(set.include?(x)))\n otp +=1\n set.clear\n end\n set.add(x)\nend\nif(set.size>0)\n otp +=1\nend\nputs otp\n"}, {"source_code": "n = STDIN.gets.chomp.to_i\nq = STDIN.gets.chomp\n\na = \"LRUD\"\nb = \"RLDU\"\n\nf = -1\ns = -1\n\nans = 1\n\ni = 0\nwhile i < n do\n c = q[i]\n if f == -1\n f = c\n elsif s == -1\n if a.index(f) == b.index(c)\n ans += 1\n f = c\n elsif c != f\n s = c\n end\n elsif c != f && c != s\n ans += 1\n f = c\n s = -1\n end\n i += 1\nend\n\nputs ans"}, {"source_code": "n = gets.to_i\ns = gets.strip.split(\"\")\nsol = 1\nh = \"_\"\nv = \"_\"\nfor x in s\n if \"RL\".match(x) then\n if h != \"_\" && h != x then\n sol += 1\n v = \"_\"\n end\n h = x\n else\n if v != \"_\" && v != x then\n sol += 1\n h = \"_\" \n end\n v = x\n end\nend\nputs sol"}, {"source_code": "n = gets.chomp\ns = gets.chomp\nrev = {}\nrev['U'] = 'D'\nrev['D'] = 'U'\nrev['R'] = 'L'\nrev['L'] = 'R'\nres = 0\nrequire 'set'\nst = Set.new\ns.each_char do |i|\n c = i\n if ((st.size() == 2 && !(st.include?(c))) || st.include?(rev[c]))\n res += 1\n st.clear\n end\n st.add(c)\nend\nif (st.size() > 0)\n res += 1\nend\nputs res"}], "negative_code": [], "src_uid": "b1c7ca90ce9a67605fa058d4fd38c2f2"} {"nl": {"description": "Convexity of a set of points on the plane is the size of the largest subset of points that form a convex polygon. Your task is to build a set of n points with the convexity of exactly m. Your set of points should not contain three points that lie on a straight line.", "input_spec": "The single line contains two integers n and m (3 ≤ m ≤ 100, m ≤ n ≤ 2m).", "output_spec": "If there is no solution, print \"-1\". Otherwise, print n pairs of integers — the coordinates of points of any set with the convexity of m. The coordinates shouldn't exceed 108 in their absolute value.", "sample_inputs": ["4 3", "6 3", "6 6", "7 4"], "sample_outputs": ["0 0\n3 0\n0 3\n1 1", "-1", "10 0\n-10 0\n10 1\n9 1\n9 -1\n0 -2", "176166 6377\n709276 539564\n654734 174109\n910147 434207\n790497 366519\n606663 21061\n859328 886001"], "notes": null}, "positive_code": [{"source_code": "a,b=gets.split.map &:to_i\nexit !puts(-1)if a>4&&b<4\n(1..b).each{|i|puts i,i*i}\n(1..a-b).each{|i|puts i,-i*i-318000}\n"}, {"source_code": "a,b=gets.split.map &:to_i\nexit !puts(-1)if a>4&&b<4\n(1..b).each{|i|puts i,i*i}\n(1..a-b).each{|i|puts i,-i*i-318000}\n"}, {"source_code": "a,b=gets.split.map &:to_i\nexit !puts(-1)if a>4&&b<4\n(1..b).each{|i|puts i,i*i}\n(1..a-b).each{|i|puts i,-i*i-318000}"}, {"source_code": "a,b=gets.split.map &:to_i\nexit !puts(-1)if a>4&&b<4\n(1..b).each{|i|puts i,i*i}\n(1..a-b).each{|i|puts i,-i*i-318000}\n"}, {"source_code": "a,b=gets.split.map &:to_i\nexit !puts(-1)if a>4&&b<4\n(1..b).each{|i|puts i,i*i}\n(1..a-b).each{|i|puts i,-i*i-318000}\n"}, {"source_code": "a,b=gets.split.map &:to_i\nexit !puts(-1)if a>4&&b<4\n(1..b).each{|i|puts i,i*i}\n(1..a-b).each{|i|puts i,-i*i-318000}\n"}, {"source_code": "a,b=gets.split.map &:to_i\nexit !puts(-1)if a>4&&b<4\n(1..b).each{|i|puts i,i*i}\n(1..a-b).each{|i|puts i,-i*i-318000}\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\nif m == 3 and n >= 5\n puts -1\nelse\n INF = 100000\n x, y = [], []\n 1.upto(m) do |i|\n x << i\n y << INF + i * i\n end\n 1.upto(m) do |i|\n x << i\n y << -INF - i * i\n end\n n.times do |i|\n puts \"#{x[i]} #{y[i]}\"\n end\nend\n"}, {"source_code": "a,b=gets.split.map &:to_i\nexit !puts(-1)if a>4&&b<4\n(1..b).each{|i|puts i,i*i}\n(1..a-b).each{|i|puts i,-i*i-318000}\n"}, {"source_code": "a,b=gets.split.map &:to_i\nexit !puts(-1)if a>4&&b<4\n(1..b).each{|i|puts i,i*i}\n(1..a-b).each{|i|puts i,-i*i-318000}\n"}, {"source_code": "a,b=gets.split.map &:to_i\nexit !puts(-1)if a>4&&b<4\n(1..b).each{|i|puts i,i*i}\n(1..a-b).each{|i|puts i,-i*i-1000000}"}, {"source_code": "a,b=gets.split.map &:to_i\nexit !puts(-1)if a>4&&b<4\n(1..b).each{|i|puts i,i*i}\n(1..a-b).each{|i|puts i,-i*i-318000}"}], "negative_code": [{"source_code": "n, m = gets.split.map(&:to_i)\nif m == 3 and n >= 5\n puts -1\nelse\n INF = 100000000\n x, y = [], []\n m.times do |i|\n x << 1 + i\n y << 1 + i * i\n end\n m.times do |i|\n x << -1 - i\n y << -1 - i * i\n end\n n.times do |i|\n puts \"#{x[i]} #{y[i]}\"\n end\nend\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\nif m == 3 and n >= 5\n puts -1\nelse\n INF = 100000000\n x, y = [], []\n m.times do |i|\n x << i\n y << 1 + i * i\n end\n m.times do |i|\n x << i\n y << -1 - i * i\n end\n n.times do |i|\n puts \"#{x[i]} #{y[i]}\"\n end\nend\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\nif m == 3 and n >= 5\n puts -1\nelse\n INF = 100000000\n x, y = [], []\n m.times do |i|\n x << 1 + i\n y << INF - i * i\n end\n m.times do |i|\n x << -1 - i\n y << -INF + i * i\n end\n n.times do |i|\n puts \"#{x[i]} #{y[i]}\"\n end\nend\n"}, {"source_code": "a,b=gets.split.map &:to_i\nexit !puts(-1)if a>4&&b<4\n(1..b).each{|i|puts i,i*i}\n(1..a-b).each{|i|puts i,-i*i-237}"}, {"source_code": "a,b=gets.split.map &:to_i\nexit !puts(-1)if a>4&&b<4\n(1..b).each{|i|puts i,i*i}\n(1..a-b).each{|i|puts i,-i*i-318}"}, {"source_code": "a,b=gets.split.map &:to_i\nexit !puts(-1)if a>4&&b<4\n(1..b).each{|i|puts i,i*i}\n(1..a-b).each{|i|puts i+1,-i*i}"}, {"source_code": "a,b=gets.split.map &:to_i\nexit !puts(-1)if a>4&&b<4\n(1..b).each{|i|puts i,i*i}\n(1..a-b).each{|i|puts i,-i*i-2379}"}, {"source_code": "a,b=gets.split.map &:to_i\nexit !puts(-1)if a>4&&b<4\n(1..b).each{|i|puts i,i*i}\n(1..a-b).each{|i|puts i,-i*i-1}"}, {"source_code": "a,b=gets.split.map &:to_i\nexit !puts(-1)if a>4&&b<4\n(1..b).each{|i|puts i,i*i}\n(1..a-b).each{|i|puts i,-i*i}"}, {"source_code": "a,b=gets.split.map &:to_i\nexit !puts(-1)if a>4&&b<4\n(1..b).each{|i|puts i,i*i}\n(1..a-b).each{|i|puts i+10000,-i*i}"}], "src_uid": "2e9f2bd3c02ba6ba41882334deb35631"} {"nl": {"description": "The Fair Nut likes kvass very much. On his birthday parents presented him $$$n$$$ kegs of kvass. There are $$$v_i$$$ liters of kvass in the $$$i$$$-th keg. Each keg has a lever. You can pour your glass by exactly $$$1$$$ liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by $$$s$$$ liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by $$$s$$$ liters of kvass.", "input_spec": "The first line contains two integers $$$n$$$ and $$$s$$$ ($$$1 \\le n \\le 10^3$$$, $$$1 \\le s \\le 10^{12}$$$) — the number of kegs and glass volume. The second line contains $$$n$$$ integers $$$v_1, v_2, \\ldots, v_n$$$ ($$$1 \\le v_i \\le 10^9$$$) — the volume of $$$i$$$-th keg.", "output_spec": "If the Fair Nut cannot pour his glass by $$$s$$$ liters of kvass, print $$$-1$$$. Otherwise, print a single integer — how much kvass in the least keg can be.", "sample_inputs": ["3 3\n4 3 5", "3 4\n5 3 4", "3 7\n1 2 3"], "sample_outputs": ["3", "2", "-1"], "notes": "NoteIn the first example, the answer is $$$3$$$, the Fair Nut can take $$$1$$$ liter from the first keg and $$$2$$$ liters from the third keg. There are $$$3$$$ liters of kvass in each keg.In the second example, the answer is $$$2$$$, the Fair Nut can take $$$3$$$ liters from the first keg and $$$1$$$ liter from the second keg.In the third example, the Fair Nut can't pour his cup by $$$7$$$ liters, so the answer is $$$-1$$$."}, "positive_code": [{"source_code": "n,s,*v=$<.read.split.map &:to_i\np [[(v.reduce(:+)-s)/n,*v].min,-1].max"}, {"source_code": "n,s = gets.split.map(&:to_i)\nv = gets.split.map(&:to_i)\nmin = 0\nmax = 10**9+10\nwhile min < max-1\n mid = min+(max-min)/2\n ret = 0\n n.times do |i|\n ret += v[i]-mid if mid < v[i]\n end\n if ret < s\n max = mid-1\n else\n min = mid\n end\nend\n\n[max,min].each do |i|\n ret = 0\n n.times do |j|\n ret += v[j]-i if v[j] > i\n end\n if ret >= s\n ans = 10**9\n n.times do |j|\n ans = [ans,i,v[j]].min\n end\n puts ans\n exit\n end\nend\nputs -1"}], "negative_code": [{"source_code": "n,s = gets.split.map(&:to_i)\nv = gets.split.map(&:to_i)\nmin = 0\nmax = 10**9+10\n50.times do\n mid = min+(max-min)/2\n ret = 0\n n.times do |i|\n ret += v[i]-mid if mid < v[i]\n end\n if ret < s\n max = mid-1\n else\n min = mid\n end\nend\nfor i in min..max\n ret = 0\n n.times do |j|\n ret += v[j]-i if i < v[j]\n end\n if ret >= s\n puts i\n exit\n end\nend\nputs -1"}, {"source_code": "n,s = gets.split.map(&:to_i)\nv = gets.split.map(&:to_i)\nmin = 0\nmax = 10**9+10\n50.times do\n mid = min+(max-min)/2\n ret = 0\n n.times do |i|\n ret += v[i]-mid if mid < v[i]\n end\n if ret < s\n max = mid-1\n else\n min = mid\n end\nend\n\n[max,min].each do |i|\n ret = 0\n n.times do |j|\n ret += v[j]-i if v[j] > i\n end\n if ret >= s\n puts i\n exit\n end\nend\nputs -1"}, {"source_code": "n,s = gets.split.map(&:to_i)\nv = gets.split.map(&:to_i)\nmin = -1\nmax = 10**9+10\nwhile min < max-1\n mid = min+(max-min)/2\n ret = 0\n n.times do |i|\n ret += v[i]-mid if mid < v[i]\n end\n if ret < s\n max = mid-1\n else\n min = mid\n end\nend\np min"}, {"source_code": "n,s = gets.split.map(&:to_i)\nv = gets.split.map(&:to_i)\nmin = 0\nmax = 10**9+10\nwhile min < max-1\n mid = min+(max-min)/2\n ret = 0\n n.times do |i|\n ret += v[i]-mid if mid < v[i]\n end\n if ret < s\n max = mid-1\n else\n min = mid\n end\nend\n\n[max,min].each do |i|\n ret = 0\n n.times do |j|\n ret += v[j]-i if v[j] > i\n end\n if ret >= s\n puts i\n exit\n end\nend\nputs -1"}, {"source_code": "n,s = gets.split.map(&:to_i)\nv = gets.split.map(&:to_i)\nmin = 0\nmax = 10**9+10\n50.times do\n mid = min+(max-min)/2\n ret = 0\n n.times do |i|\n ret += v[i]-mid if mid < v[i]\n end\n if ret < s\n max = mid-1\n else\n min = mid\n end\nend\nfor i in min..max\n ret = 0\n n.times do |j|\n ret += v[j]-i if i < v[j]\n end\n if ret == s\n puts i\n exit\n elsif ret > s\n puts i+1\n exit\n end\nend\nputs -1"}], "src_uid": "23c63d1fea568a75663450e0c6f23a29"} {"nl": {"description": "You're given a list of n strings a1, a2, ..., an. You'd like to concatenate them together in some order such that the resulting string would be lexicographically smallest.Given the list of strings, output the lexicographically smallest concatenation.", "input_spec": "The first line contains integer n — the number of strings (1 ≤ n ≤ 5·104). Each of the next n lines contains one string ai (1 ≤ |ai| ≤ 50) consisting of only lowercase English letters. The sum of string lengths will not exceed 5·104.", "output_spec": "Print the only string a — the lexicographically smallest string concatenation.", "sample_inputs": ["4\nabba\nabacaba\nbcd\ner", "5\nx\nxx\nxxa\nxxaa\nxxaaa", "3\nc\ncb\ncba"], "sample_outputs": ["abacabaabbabcder", "xxaaaxxaaxxaxxx", "cbacbc"], "notes": null}, "positive_code": [{"source_code": "gets;puts$<.map(&:chomp).sort{|a,b|a+b<=>b+a}*''"}, {"source_code": "n = gets.chomp.to_i\narr = []\nn.times do\n arr << gets.chomp\nend\n\nputs arr.sort{|a, b| a + b <=> b + a}.join(\"\")"}, {"source_code": "gets;puts$<.map(&:chomp).sort{|a,b|a+b<=>b+a}*''\n"}], "negative_code": [{"source_code": "#!/usr/bin/ruby\nn=gets.to_i\nputs $<.map{|e|e.chomp+'~'}.sort.map{|e|e[0..-2]}*''"}], "src_uid": "930365b084022708eb871f3ca2f269e4"} {"nl": {"description": "Alice and Bob like games. And now they are ready to start a new game. They have placed n chocolate bars in a line. Alice starts to eat chocolate bars one by one from left to right, and Bob — from right to left. For each chocololate bar the time, needed for the player to consume it, is known (Alice and Bob eat them with equal speed). When the player consumes a chocolate bar, he immediately starts with another. It is not allowed to eat two chocolate bars at the same time, to leave the bar unfinished and to make pauses. If both players start to eat the same bar simultaneously, Bob leaves it to Alice as a true gentleman.How many bars each of the players will consume?", "input_spec": "The first line contains one integer n (1 ≤ n ≤ 105) — the amount of bars on the table. The second line contains a sequence t1, t2, ..., tn (1 ≤ ti ≤ 1000), where ti is the time (in seconds) needed to consume the i-th bar (in the order from left to right).", "output_spec": "Print two numbers a and b, where a is the amount of bars consumed by Alice, and b is the amount of bars consumed by Bob.", "sample_inputs": ["5\n2 9 8 2 7"], "sample_outputs": ["2 3"], "notes": null}, "positive_code": [{"source_code": "n = gets.chomp.to_i\nar = gets.chomp.split(/ /).map!{|x| x.to_i}\n\nai = 1\nbi = n-1\nat = ar[0]\nbt = 0\nwhile ai <= bi\n\tif at <= bt\n\t\tat += ar[ai]\n\t\tai += 1\n\telse\n\t\tbt += ar[bi]\n\t\tbi -= 1\n\tend\nend\n\nputs ai.to_s + \" \" + (n-(bi+1)).to_s\n"}, {"source_code": "n=STDIN.gets.chomp.to_i\nt=STDIN.gets.chomp.split\nt.map! {|x| x.to_i}\nalice=0\nbob=n-1\na=0\nb=0\nwhile true do\n consumed=t[alice] i\n if bob_time > alisa_time\n while bob_time > alisa_time && j > i\n alisa_time += times[i]\n i += 1\n end\n else\n while bob_time <= alisa_time && j > i\n bob_time += times[j]\n j -= 1\n end\n end\n end\n\n if bob_time < alisa_time\n i -= 1\n end\n\n puts [i + 1, times.size - i - 1].join(' ')\nend\nrun"}, {"source_code": "num = gets.chomp.to_i\narr = gets.split.map(&:to_i)\nif num == 1 then\n\tputs \"1 0\"\n\texit\nend\nl = 0\nr = num - 1\nlcurr = arr[0]\nrcurr = arr[num - 1]\nwhile l + 1 < r do\n\tif lcurr <= rcurr then\n\t\trcurr -= lcurr\n\t\tl += 1\n\t\tlcurr = arr[l]\n\telse\n\t\tlcurr -= rcurr\n\t\tr -= 1\n\t\trcurr = arr[r]\n\tend\nend\nprint l + 1, \" \", num - r\n"}, {"source_code": "n=gets.chomp.to_i\nm=gets.chomp.split.map{|i| i.to_i}\nif n==1\n puts \"1 0\"\n exit\nend\nif n==2\n puts \"1 1\"\n exit\nend\na=0\nb=n-1\nta=m[a]\ntb=m[b]\nwhile true\n if ta>tb\n b-=1\n ta=ta-tb\n tb=m[b]\n else\n a+=1\n tb=tb-ta\n ta=m[a]\n end\n if b-a==1\n break\n end\nend\nputs \"\"+(a+1).to_s+\" \"+(n-b).to_s\n \n"}, {"source_code": "n = gets.to_i\nt = gets.split.map(&:to_i)\na = 0\nb = n-1\nasum = 0\nbsum = 0\nwhile a <= b do\n if asum <= bsum then \n asum += t[a]\n a += 1\n else\n bsum += t[b]\n b -= 1\n end\nend\nprint a, \" \", n-a\nputs\n\n"}, {"source_code": "##\n# 6C\n# Author: Sammy Teahan\n#\n\nif __FILE__ == $0\n bars = gets.to_i\n time = gets.split.map(&:to_i)\n aliceCount = 0;\n bobCount = 0;\n aliceTotal = 0;\n bobTotal = 0;\n start = 0;\n last = bars - 1\n\n while start <= last do\n if aliceTotal <= bobTotal\n aliceTotal += time[start]\n start += 1\n aliceCount += 1\n else\n bobTotal += time[last]\n last -= 1\n bobCount += 1\n end\n end\n\n puts \"#{aliceCount} #{bobCount}\"\nend\n\n"}, {"source_code": "N = gets.to_i\nconsumeTime = gets.split.map{ |x| x.to_i}\na = 1\na_time = consumeTime.shift\nb = 0\nb_time = 0\nuntil consumeTime.empty? do\n if a_time>b_time\n b += 1\n b_time += consumeTime.pop\n else\n a += 1\n a_time += consumeTime.shift\n end\nend\nprint a,\" \",b,\"\\n\"\n"}, {"source_code": "n = gets.to_i\ntimes = gets.split.map(&:to_i)\nalice = 0\nbob = n - 1\nalice_time, bob_time = 0, 0\n\nwhile alice <= bob\n\tif alice_time <= bob_time\n\t\talice_time += times[alice]\n\t\talice += 1\n\telse\n\t\tbob_time += times[bob]\n\t\tbob -= 1\n\tend\nend\n\nputs [alice, n - alice].join(' ')"}, {"source_code": "n = gets.to_i\na = gets.split(/\\s/).map {|x| x.to_i}\n\na1 = 0\na2 = 0\nt1 = 0\nt2 = 0\nsize = a.size\nif size == 1\n puts \"1 0\"\nelse\n i = 0\n j = size-1\n while i < j\n if t1 > t2\n while t1 > t2 and i < j\n a2 += 1\n t2 += a[j]\n j -= 1\n end\n elsif t2 > t1\n while t2 > t1 and i < j\n a1 += 1\n t1 += a[i]\n i += 1\n end\n else\n a1 += 1\n a2 += 1\n t1 += a[i]\n t2 += a[j]\n i += 1\n j -= 1\n end\n end\n \n if a1+a2 != n\n if t1 <= t2\n a1 += 1\n else\n a2 += 1\n end\n end\n puts \"#{a1} #{a2}\"\nend"}, {"source_code": "gets\na = gets.split.map(&:to_i)\n\ni, j = 0, a.size - 1\n\nif a.size > 1\n while true\n if a[i] == a[j]\n a[i] = a[j] = 0\n i += 1\n j -= 1\n if i > j \n break\n elsif i == j\n i += 1\n break\n end\n elsif a[i] > a[j]\n a[i] -= a[j]\n j -= 1\n if i == j\n i += 1\n break\n end\n elsif a[i] < a[j]\n a[j] -= a[i]\n i += 1\n if i == j\n j -= 1\n break\n end\n end\n end\nelse\n i = 1\n j = a.size - 1\nend\n\nputs \"#{i} #{(a.size - 1) - j}\"\n"}, {"source_code": "# Discard input count\ngets\n# This gives us candy bars as an array of ints.\nbars = gets.split.map { |a| a.to_i }\n# Next we find the sum\nsum = bars.inject(0,:+)\n# Imagine the chocolate bars laid end to end on a line. We know that if both Bob and Alice keep eating at the same rate, The coordinate they will meet at is half the sum.\nmiddle = sum / 2.0\n# Now we need to find out which bar this is. If it is a clean split between two bars, we can simply return.\nalice_dist = 0\nalice_dist_to_middle_bar_start = nil\nmiddle_bar_index = nil\nbars.each_with_index do |bar,i|\n # Check if, by adding the distance, we surpass this bar.\n dist_to_end_of_bar = alice_dist + bar\n if(dist_to_end_of_bar > middle)\n middle_bar_index = i\n alice_dist_to_middle_bar_start = alice_dist\n break\n elsif (dist_to_end_of_bar == middle)\n # It's a clean split! alice eats [0..i] bars so add '1'\n puts (i+1).to_s + \" \" + (bars.length - (i+1)).to_s\n exit\n end\n alice_dist += bar\nend\n\n#And now we know that the middle bar is 'i', and that alice's distance from it. From there we can get Bob's distance.\nbob_dist_to_middle_bar_end = sum - (alice_dist_to_middle_bar_start + bars[middle_bar_index])\n\nif(alice_dist_to_middle_bar_start <= bob_dist_to_middle_bar_end)\n puts (1 + middle_bar_index).to_s + \" \" + (bars.length - (1 + middle_bar_index)).to_s\nelse\n puts (middle_bar_index).to_s + ' ' + (bars.length - middle_bar_index).to_s\nend"}, {"source_code": "n = gets.to_i\n\na = gets.split.collect{|x| x.to_i}\n\nif a.size == 1\n\tprint 1,' ', 0\n\texit\nend\n\nl,r = 0, a.size-1\n\nwhile l+1 < r\n\t# print [l,r],\"\\n\"\n\tmin = [a[l],a[r]].min\n\ta[l] -= min\n\ta[r] -= min\n\n\tif a[l] == 0 and l < r-1\n\t\tl+=1\n\tend\n\tif a[r] == 0 and r-1 > l\n\t\tr-=1\n\tend\nend\n\nprint l+1, ' ', a.size - r"}], "negative_code": [{"source_code": "n=STDIN.gets.chomp.to_i\nt=STDIN.gets.chomp.split\nt.map! {|x| x.to_i}\nalice=0\nbob=n-1\na=0\nb=0\nwhile true do\n consumed=t[alice] t2\n while t1 > t2 and i < j\n a2 += 1\n t2 += a[j]\n j -= 1\n end\n elsif t2 > t1\n while t2 > t1 and i < j\n a1 += 1\n t1 += a[i]\n i += 1\n end\n else\n a1 += 1\n a2 += 1\n t1 += a[i]\n t2 += a[j]\n i += 1\n j -= 1\n x = z\n end\n end\n \n if x\n if t1 <= t2\n a1 += 1\n else\n a2 += 1\n end\n end\n puts \"#{a1} #{a2}\"\nend"}, {"source_code": "n = gets.to_i\na = gets.split(/\\s/).map {|x| x.to_i}\n\na1 = 0\na2 = 0\nt1 = 0\nt2 = 0\nsize = a.size\nif size == 1\n puts \"1 0\"\nelse\n i = 0\n j = size-1\n while i < j\n if t1 > t2\n while t1 > t2 and i < j\n a2 += 1\n t2 += a[j]\n j -= 1\n end\n elsif t2 > t1\n while t2 > t1 and i < j\n a1 += 1\n t1 += a[i]\n i += 1\n end\n else\n a1 += 1\n a2 += 1\n t1 += a[i]\n t2 += a[j]\n i += 1\n j -= 1\n end\n end\n \n if t1+t2 != n\n if t1 <= t2\n a1 += 1\n else\n a2 += 1\n end\n end\n puts \"#{a1} #{a2}\"\nend"}, {"source_code": "gets\na = gets.split.map(&:to_i)\n\ni, j = 0, a.size - 1\n\nif a.size > 1\n while i < j\n if a[i] == a[j]\n if i + 1 >= j\n i += 1\n else\n i += 1\n j -= 1\n end\n elsif a[i] < a[j]\n a[j] -= a[i]\n i += 1\n elsif a[i] > a[j]\n a[i] -= a[j]\n j -= 1\n end\n end\nelse\n i = 1\n j = a.size\nend\n\nputs \"#{i} #{a.size - j}\"\n"}, {"source_code": "gets\na = gets.split.map(&:to_i)\n\ni, j = 0, a.size - 1\n\nif a.size > 1\n while i < j\n if a[i] == a[j]\n if i + 1 >= j - 1\n i += 1\n else\n i += 1\n j -= 1\n end\n elsif a[i] < a[j]\n a[j] -= a[i]\n i += 1\n elsif a[i] > a[j]\n a[i] -= a[j]\n j -= 1\n end\n end\nelse\n i = 1\n j = a.size\nend\n\nputs \"#{i} #{a.size - j}\"\n"}, {"source_code": "gets\na = gets.split.map(&:to_i)\n\ni, j = 0, a.size - 1\n\nif a.size > 1\n while true\n if a[i] == a[j]\n a[i] = a[j] = 0\n i += 1\n j -= 1\n if i > j \n break\n elsif i == j\n i += 1\n break\n end\n elsif a[i] > a[j]\n a[i] -= a[j]\n i += 1\n if i == j\n j -= 1\n break\n end\n elsif a[i] < a[j]\n a[j] -= a[i]\n j -= 1\n if i == j\n i += 1\n break\n end\n end\n end\nelse\n i = 1\n j = a.size - 1\nend\n\nputs \"#{i} #{(a.size - 1) - j}\"\n"}, {"source_code": "gets\na = gets.split.map(&:to_i)\n\ni, j = 0, a.size - 1\n\nif a.size > 1\n while i < j\n if a[i] == a[j]\n if i + 1 >= j - 1\n i += 1\n else\n i += 1\n j -= 1\n end\n elsif a[i] < a[j]\n a[j] -= a[i]\n if i + 1 >= j\n j -= 1\n break\n end\n i += 1\n elsif a[i] > a[j]\n a[i] -= a[j]\n if i >= j - 1\n i += 1\n break\n end\n j -= 1\n end\n end\nelse\n i = 1\n j = a.size\nend\n\nputs \"#{i} #{a.size - j}\"\n"}, {"source_code": "gets\na = gets.split.map(&:to_i)\n\ni, j = 0, a.size - 1\n\nwhile i < j\n if a[i] == a[j]\n if i + 1 >= j\n i += 1\n else\n i += 1\n j -= 1\n end\n elsif a[i] < a[j]\n a[j] -= a[i]\n i += 1\n elsif a[i] > a[j]\n a[i] -= a[j]\n j -= 1\n end\nend\n\nputs \"#{i} #{a.size - j}\"\n"}, {"source_code": "n = gets.to_i\n\na = gets.split.collect{|x| x.to_i}\n\nl,r = 0, a.size-1\n\nwhile l+1 < r\n\t# print [l,r],\"\\n\"\n\tmin = [a[l],a[r]].min\n\ta[l] -= min\n\ta[r] -= min\n\n\tif a[l] == 0 and l < r-1\n\t\tl+=1\n\tend\n\tif a[r] == 0 and r-1 > l\n\t\tr-=1\n\tend\nend\n\nprint l+1, ' ', a.size - r"}], "src_uid": "8f0172f742b058f5905c0a02d79000dc"} {"nl": {"description": "You are given an array $$$a$$$ of size $$$n$$$.You can perform the following operation on the array: Choose two different integers $$$i, j$$$ $$$(1 \\leq i < j \\leq n$$$), replace $$$a_i$$$ with $$$x$$$ and $$$a_j$$$ with $$$y$$$. In order not to break the array, $$$a_i | a_j = x | y$$$ must be held, where $$$|$$$ denotes the bitwise OR operation. Notice that $$$x$$$ and $$$y$$$ are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \\leq t \\leq 1000)$$$. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(2 \\leq n \\leq 100)$$$ — the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots ,a_n$$$ $$$(0 \\leq a_i < 2^{30})$$$.", "output_spec": "For each test case, print one number in a line — the minimum possible sum of the array.", "sample_inputs": ["4\n3\n1 3 2\n5\n1 2 4 8 16\n2\n6 6\n3\n3 5 6"], "sample_outputs": ["3\n31\n6\n7"], "notes": "NoteIn the first example, you can perform the following operations to obtain the array $$$[1, 0, 2]$$$:1. choose $$$i = 1, j = 2$$$, change $$$a_1 = 1$$$ and $$$a_2 = 2$$$, it's valid since $$$1 | 3 = 1 | 2$$$. The array becomes $$$[1, 2, 2]$$$.2. choose $$$i = 2, j = 3$$$, change $$$a_2 = 0$$$ and $$$a_3 = 2$$$, it's valid since $$$2 | 2 = 0 | 2$$$. The array becomes $$$[1, 0, 2]$$$.We can prove that the minimum sum is $$$1 + 0 + 2 = 3$$$In the second example, We don't need any operations."}, "positive_code": [{"source_code": "t = readline.strip.to_i\r\n\r\nt.times do\r\n n = readline.strip.to_i\r\n nums = readline.strip.split(/\\s+/).map(&:to_i)\r\n \r\n res = 0\r\n nums.each do |e|\r\n res |= e\r\n end\r\n \r\n puts res\r\nend\r\n"}, {"source_code": "gets.to_i.times {\n gets\n xs = gets.split.map!(&:to_i)\n p xs.inject(:|)\n}\n"}], "negative_code": [], "src_uid": "7fca54a73076ddfeb30b921b9e341f26"} {"nl": {"description": "Bessie and the cows are playing with sequences and need your help. They start with a sequence, initially containing just the number 0, and perform n operations. Each operation is one of the following: Add the integer xi to the first ai elements of the sequence. Append an integer ki to the end of the sequence. (And hence the size of the sequence increases by 1) Remove the last element of the sequence. So, the size of the sequence decreases by one. Note, that this operation can only be done if there are at least two elements in the sequence. After each operation, the cows would like to know the average of all the numbers in the sequence. Help them!", "input_spec": "The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of operations. The next n lines describe the operations. Each line will start with an integer ti (1 ≤ ti ≤ 3), denoting the type of the operation (see above). If ti = 1, it will be followed by two integers ai, xi (|xi| ≤ 103; 1 ≤ ai). If ti = 2, it will be followed by a single integer ki (|ki| ≤ 103). If ti = 3, it will not be followed by anything. It is guaranteed that all operations are correct (don't touch nonexistent elements) and that there will always be at least one element in the sequence.", "output_spec": "Output n lines each containing the average of the numbers in the sequence after the corresponding operation. The answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6.", "sample_inputs": ["5\n2 1\n3\n2 3\n2 1\n3", "6\n2 1\n1 2 20\n2 2\n1 2 -3\n3\n3"], "sample_outputs": ["0.500000\n0.000000\n1.500000\n1.333333\n1.500000", "0.500000\n20.500000\n14.333333\n12.333333\n17.500000\n17.000000"], "notes": "NoteIn the second sample, the sequence becomes "}, "positive_code": [{"source_code": "#!/usr/bin/ruby1.9\n\ncommand_num = STDIN.readline.to_i\n\nsums = [0]\nadd = (command_num+1).times.collect { 0 }\nadded = 0\n\ncommand_num.times do\n\n\tcommand = STDIN.readline.split.collect {|s| s.to_i }\n\n\tif command[0] == 1\n\t\ta, x = command.slice(1, 2)\n\t\tadd[a-1] += x\n\t\tadded += a*x\n\n\telsif command[0] == 2\n\t\tk = command[1]\n\t\tsums.push(sums.last+k)\n\n\telse\n\t\tn = sums.length\n\t\tsums.pop\n\t\tadded -= add[n-1]\n\t\tadd[n-2] += add[n-1]\n\t\tadd[n-1] = 0\n\n\tend\n\n\tputs ( 1.0 * (sums.last + added) / sums.length )\n\nend\n\n"}], "negative_code": [], "src_uid": "d43d4fd6c1e2722da185f34d902ace97"} {"nl": {"description": "A team of students from the city S is sent to the All-Berland Olympiad in Informatics. Traditionally, they go on the train. All students have bought tickets in one carriage, consisting of n compartments (each compartment has exactly four people). We know that if one compartment contain one or two students, then they get bored, and if one compartment contain three or four students, then the compartment has fun throughout the entire trip.The students want to swap with other people, so that no compartment with students had bored students. To swap places with another person, you need to convince him that it is really necessary. The students can not independently find the necessary arguments, so they asked a sympathetic conductor for help. The conductor can use her life experience to persuade any passenger to switch places with some student.However, the conductor does not want to waste time persuading the wrong people, so she wants to know what is the minimum number of people necessary to persuade her to change places with the students. Your task is to find the number. After all the swaps each compartment should either have no student left, or have a company of three or four students. ", "input_spec": "The first line contains integer n (1 ≤ n ≤ 106) — the number of compartments in the carriage. The second line contains n integers a1, a2, ..., an showing how many students ride in each compartment (0 ≤ ai ≤ 4). It is guaranteed that at least one student is riding in the train.", "output_spec": "If no sequence of swapping seats with other people leads to the desired result, print number \"-1\" (without the quotes). In another case, print the smallest number of people you need to persuade to swap places.", "sample_inputs": ["5\n1 2 2 4 3", "3\n4 1 1", "4\n0 3 0 4"], "sample_outputs": ["2", "2", "0"], "notes": null}, "positive_code": [{"source_code": "I =->{gets.split.map &:to_i}\n\ndef f\t\n\tgets; a = I[]; c1 = a.count(1); c2 = a.count(2); c3 = a.count(3); c4 = a.count(4)\n\ts = c1+2*c2+3*c3+4*c4; return -1 if (s == 1 || s == 2 || s == 5)\n\n\tz = d = [c1, c2].min; c1 -= d; c2 -= d; c3 += d\n\tif !c1.zero? then\n\t\tz += 2*(d=c1/3); c1 -= d*3; c3 += d\n\t\tz += c1.zero? ? 0 : (c1 == 1 && !c3.zero? ? 1 : 2)\n\telsif !c2.zero? then\n\t\tz += 2*(d=c2/3); c2 -= d*3\n\t\tz += c2.zero? ? 0 : (c2 == 1 && !c4.zero? ? 1 : 2)\n\tend\n\n\treturn z\nend\n\np f()"}], "negative_code": [{"source_code": "I =->{gets.split.map &:to_i}\n\ndef f\t\n\tgets; a = I[]; c1 = a.count(1); c2 = a.count(2); c3 = a.count(3); c4 = a.count(4)\n\ts = c1+2*c2+3*c3+4*c4; return 0 if (s == 1 || s == 2 || s == 5)\n\tz = d = [c1, c2].min; c1 -= d; c2 -= d; c3 += d\n\tif !c1.zero? then\n\t\tz += 2*(d=c1/3); c1 -= d*3; c3 += d\n\t\tz += c1.zero? ? 0 : (c1 == 1 && !c3.zero? ? 1 : 2)\n\telsif !c2.zero? then\n\t\tz += 2*(d=c2/3); c2 -= d*3\n\t\tz += c2.zero? ? 0 : (c2 == 1 && !c4.zero? ? 1 : 2)\n\tend\n\n\treturn z\nend\n\np f()"}], "src_uid": "a7cdb90a03447fc9285819ed059383b3"} {"nl": {"description": "Let $$$LCM(x, y)$$$ be the minimum positive integer that is divisible by both $$$x$$$ and $$$y$$$. For example, $$$LCM(13, 37) = 481$$$, $$$LCM(9, 6) = 18$$$.You are given two integers $$$l$$$ and $$$r$$$. Find two integers $$$x$$$ and $$$y$$$ such that $$$l \\le x < y \\le r$$$ and $$$l \\le LCM(x, y) \\le r$$$.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 10000$$$) — the number of test cases. Each test case is represented by one line containing two integers $$$l$$$ and $$$r$$$ ($$$1 \\le l < r \\le 10^9$$$).", "output_spec": "For each test case, print two integers: if it is impossible to find integers $$$x$$$ and $$$y$$$ meeting the constraints in the statement, print two integers equal to $$$-1$$$; otherwise, print the values of $$$x$$$ and $$$y$$$ (if there are multiple valid answers, you may print any of them). ", "sample_inputs": ["4\n1 1337\n13 69\n2 4\n88 89"], "sample_outputs": ["6 7\n14 21\n2 4\n-1 -1"], "notes": null}, "positive_code": [{"source_code": "$,, $\\= ' ', \"\\n\"\ngets.to_i.times do\n l, r = gets.split.map &:to_i\n print -1, -1 if 2 * l > r\n print l, 2 * l if 2 * l <= r\nend"}, {"source_code": "n = gets.to_i\nans = []\nn.times do\n l,r = gets.chomp.split(\" \").map(&:to_i)\n if l * 2 <= r\n ans << [l,l*2]\n else\n ans << [-1,-1]\n end\nend\nans.each do |x|\n puts \"#{x[0]} #{x[1]}\"\nend"}], "negative_code": [{"source_code": "$, = ' '\ngets.to_i.times do\n l, r = gets.split.map &:to_i\n print -1, -1 if 2 * l > r\n print l, 2 * l if 2 * l <= r\nend"}], "src_uid": "e8ba3fb95800806465386ecbfbe924e9"} {"nl": {"description": "Masha has $$$n$$$ types of tiles of size $$$2 \\times 2$$$. Each cell of the tile contains one integer. Masha has an infinite number of tiles of each type.Masha decides to construct the square of size $$$m \\times m$$$ consisting of the given tiles. This square also has to be a symmetric with respect to the main diagonal matrix, and each cell of this square has to be covered with exactly one tile cell, and also sides of tiles should be parallel to the sides of the square. All placed tiles cannot intersect with each other. Also, each tile should lie inside the square. See the picture in Notes section for better understanding.Symmetric with respect to the main diagonal matrix is such a square $$$s$$$ that for each pair $$$(i, j)$$$ the condition $$$s[i][j] = s[j][i]$$$ holds. I.e. it is true that the element written in the $$$i$$$-row and $$$j$$$-th column equals to the element written in the $$$j$$$-th row and $$$i$$$-th column.Your task is to determine if Masha can construct a square of size $$$m \\times m$$$ which is a symmetric matrix and consists of tiles she has. Masha can use any number of tiles of each type she has to construct the square. Note that she can not rotate tiles, she can only place them in the orientation they have in the input.You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n \\le 100$$$, $$$1 \\le m \\le 100$$$) — the number of types of tiles and the size of the square Masha wants to construct. The next $$$2n$$$ lines of the test case contain descriptions of tiles types. Types of tiles are written one after another, each type is written on two lines. The first line of the description contains two positive (greater than zero) integers not exceeding $$$100$$$ — the number written in the top left corner of the tile and the number written in the top right corner of the tile of the current type. The second line of the description contains two positive (greater than zero) integers not exceeding $$$100$$$ — the number written in the bottom left corner of the tile and the number written in the bottom right corner of the tile of the current type. It is forbidden to rotate tiles, it is only allowed to place them in the orientation they have in the input.", "output_spec": "For each test case print the answer: \"YES\" (without quotes) if Masha can construct the square of size $$$m \\times m$$$ which is a symmetric matrix. Otherwise, print \"NO\" (withtout quotes).", "sample_inputs": ["6\n3 4\n1 2\n5 6\n5 7\n7 4\n8 9\n9 8\n2 5\n1 1\n1 1\n2 2\n2 2\n1 100\n10 10\n10 10\n1 2\n4 5\n8 4\n2 2\n1 1\n1 1\n1 2\n3 4\n1 2\n1 1\n1 1"], "sample_outputs": ["YES\nNO\nYES\nNO\nYES\nYES"], "notes": "NoteThe first test case of the input has three types of tiles, they are shown on the picture below. Masha can construct, for example, the following square of size $$$4 \\times 4$$$ which is a symmetric matrix: "}, "positive_code": [{"source_code": "t = gets.chomp.to_i\n\nt.times do\n line = gets.chomp.split(' ')\n n = line[0].to_i\n m = line[1].to_i\n result = false\n\n n.times do\n line = gets.chomp.split(' ')\n a = line[0].to_i\n b = line[1].to_i\n line = gets.chomp.split(' ')\n c = line[0].to_i\n d = line[1].to_i\n result ||= b == c\n end\n \n (m % 2).zero? && result ? (puts 'YES') : (puts 'NO')\nend"}, {"source_code": "t = gets.chomp.to_i\nt.times do \n n, m = gets.chomp.split(\" \").map(&:to_i)\n four = 0\n two = 0\n no = 0\n j = []\n n.times do\n i = []\n arr = []\n ar = []\n tokubetu = []\n a, b = gets.chomp.split(\" \").map(&:to_i)\n c, d = gets.chomp.split(\" \").map(&:to_i)\n arr << a\n arr << b\n ar << c\n ar << d\n i << arr\n i << ar\n if a == b && b == c && c == d\n four += 1\n elsif b == c\n two += 1\n else\n no += 1\n tokubetu << a\n tokubetu << d\n j << tokubetu\n end\n end\n # p four\n # p two\n # p no\n if m % 2 != 0\n puts \"NO\"\n elsif four >= 1\n puts \"YES\"\n elsif two >= 1\n puts \"YES\"\n else\n puts \"NO\"\n end\nend"}, {"source_code": "gets.to_i.times{n,m=gets.split.map(&:to_i);puts(if Array.new(n){_,a=gets.split.map(&:to_i);b,_=gets.split.map(&:to_i);a==b;}.reduce(:|)and m.even?then :YES else :NO end)}\n"}, {"source_code": "t = gets.to_i\nt.times {\n n, m = gets.split.map(&:to_i)\n puts (if Array.new(n) {\n _, a = gets.split.map(&:to_i)\n b, _ = gets.split.map(&:to_i)\n a == b\n }.reduce(:|) and m.even? then :YES else :NO end)\n}\n"}], "negative_code": [], "src_uid": "f46f6fa28d0a7023da3bad0d222ce393"} {"nl": {"description": "You helped Dima to have a great weekend, but it's time to work. Naturally, Dima, as all other men who have girlfriends, does everything wrong.Inna and Dima are now in one room. Inna tells Dima off for everything he does in her presence. After Inna tells him off for something, she goes to another room, walks there in circles muttering about how useless her sweetheart is. During that time Dima has time to peacefully complete k - 1 tasks. Then Inna returns and tells Dima off for the next task he does in her presence and goes to another room again. It continues until Dima is through with his tasks.Overall, Dima has n tasks to do, each task has a unique number from 1 to n. Dima loves order, so he does tasks consecutively, starting from some task. For example, if Dima has 6 tasks to do in total, then, if he starts from the 5-th task, the order is like that: first Dima does the 5-th task, then the 6-th one, then the 1-st one, then the 2-nd one, then the 3-rd one, then the 4-th one.Inna tells Dima off (only lovingly and appropriately!) so often and systematically that he's very well learned the power with which she tells him off for each task. Help Dima choose the first task so that in total he gets told off with as little power as possible.", "input_spec": "The first line of the input contains two integers n, k (1 ≤ k ≤ n ≤ 105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103), where ai is the power Inna tells Dima off with if she is present in the room while he is doing the i-th task. It is guaranteed that n is divisible by k.", "output_spec": "In a single line print the number of the task Dima should start with to get told off with as little power as possible. If there are multiple solutions, print the one with the minimum number of the first task to do.", "sample_inputs": ["6 2\n3 2 1 6 5 4", "10 5\n1 3 5 7 9 9 4 1 8 5"], "sample_outputs": ["1", "3"], "notes": "NoteExplanation of the first example.If Dima starts from the first task, Inna tells him off with power 3, then Dima can do one more task (as k = 2), then Inna tells him off for the third task with power 1, then she tells him off for the fifth task with power 5. Thus, Dima gets told off with total power 3 + 1 + 5 = 9. If Dima started from the second task, for example, then Inna would tell him off for tasks 2, 4 and 6 with power 2 + 6 + 4 = 12. Explanation of the second example.In the second example k = 5, thus, Dima manages to complete 4 tasks in-between the telling off sessions. Thus, Inna tells Dima off for tasks number 1 and 6 (if he starts from 1 or 6), 2 and 7 (if he starts from 2 or 7) and so on. The optimal answer is to start from task 3 or 8, 3 has a smaller number, so the answer is 3."}, "positive_code": [{"source_code": "n,k=gets.chomp.split.map(&:to_i)\nb,ans=Array.new(k,0),0\na=gets.chomp.split.map(&:to_i)\na.length.times {|i| b[i%k]+=a[i]}\nputs b.index(b.min)+1"}, {"source_code": "n,k = gets.split(\" \").map(&:to_i)\narr = gets.split(\" \").map(&:to_i)\nm = n/k\nk_list = []\n(0..((n/m)-1)).each do |i|\n\tk_list << (i..(n-1)).step(k).to_a\nend\nsum = []\nk_list.each do |a|\n\tsum << a.inject(0){|s,x| s += arr[x]}\t\nend\n# puts \"#{sum}\"\nmin,min_ind = sum.each_with_index.min\nputs k_list[min_ind][0]+1"}, {"source_code": "n,k=gets.split.map(&:to_i)\na=gets.split.map(&:to_i)\nm,b,bi=n/k,1.0/0,-1\nk.times do |i|\n s=0\n m.times{|j|s+=a[i+k*j]}\n b,bi=s,i+1 if s < b\nend\np bi"}, {"source_code": "def read_next_line\n gets.split.map { |x| x.to_i }\nend\n\nn, k = read_next_line\na = read_next_line\nsums = Array.new(k)\nfor i in 0...k\n sum = 0\n for j in (i...n).step(k)\n sum += a[j]\n end\n sums[i] = sum\nend\nputs sums.index(sums.min) + 1"}], "negative_code": [], "src_uid": "646cec22d98636447038e61e7dfb9db3"} {"nl": {"description": "Pushok the dog has been chasing Imp for a few hours already. Fortunately, Imp knows that Pushok is afraid of a robot vacuum cleaner. While moving, the robot generates a string t consisting of letters 's' and 'h', that produces a lot of noise. We define noise of string t as the number of occurrences of string \"sh\" as a subsequence in it, in other words, the number of such pairs (i, j), that i < j and and . The robot is off at the moment. Imp knows that it has a sequence of strings ti in its memory, and he can arbitrary change their order. When the robot is started, it generates the string t as a concatenation of these strings in the given order. The noise of the resulting string equals the noise of this concatenation.Help Imp to find the maximum noise he can achieve by changing the order of the strings.", "input_spec": "The first line contains a single integer n (1 ≤ n ≤ 105) — the number of strings in robot's memory. Next n lines contain the strings t1, t2, ..., tn, one per line. It is guaranteed that the strings are non-empty, contain only English letters 's' and 'h' and their total length does not exceed 105.", "output_spec": "Print a single integer — the maxumum possible noise Imp can achieve by changing the order of the strings.", "sample_inputs": ["4\nssh\nhs\ns\nhhhs", "2\nh\ns"], "sample_outputs": ["18", "1"], "notes": "NoteThe optimal concatenation in the first sample is ssshhshhhs."}, "positive_code": [{"source_code": "n = gets.to_i\n\nt_list = [] \nn.times do\n string = gets.strip\n s_count = string.scan(/s/).count\n h_count = string.scan(/h/).count\n factor = s_count / Float(h_count + s_count)\n t_list << [string, factor] \nend\n\nt_list.sort_by! { |i| i[1] }.reverse!\n\ns_count, count = 0, 0\n\nt_list.each do |t|\n t[0].each_char do |c|\n if c == 's'\n s_count += 1\n else\n count += s_count\n end\n end\nend\n\nputs count\n "}], "negative_code": [], "src_uid": "f88cf470095f250ffeb57feae7697e36"} {"nl": {"description": "Natasha is going to fly on a rocket to Mars and return to Earth. Also, on the way to Mars, she will land on $$$n - 2$$$ intermediate planets. Formally: we number all the planets from $$$1$$$ to $$$n$$$. $$$1$$$ is Earth, $$$n$$$ is Mars. Natasha will make exactly $$$n$$$ flights: $$$1 \\to 2 \\to \\ldots n \\to 1$$$.Flight from $$$x$$$ to $$$y$$$ consists of two phases: take-off from planet $$$x$$$ and landing to planet $$$y$$$. This way, the overall itinerary of the trip will be: the $$$1$$$-st planet $$$\\to$$$ take-off from the $$$1$$$-st planet $$$\\to$$$ landing to the $$$2$$$-nd planet $$$\\to$$$ $$$2$$$-nd planet $$$\\to$$$ take-off from the $$$2$$$-nd planet $$$\\to$$$ $$$\\ldots$$$ $$$\\to$$$ landing to the $$$n$$$-th planet $$$\\to$$$ the $$$n$$$-th planet $$$\\to$$$ take-off from the $$$n$$$-th planet $$$\\to$$$ landing to the $$$1$$$-st planet $$$\\to$$$ the $$$1$$$-st planet.The mass of the rocket together with all the useful cargo (but without fuel) is $$$m$$$ tons. However, Natasha does not know how much fuel to load into the rocket. Unfortunately, fuel can only be loaded on Earth, so if the rocket runs out of fuel on some other planet, Natasha will not be able to return home. Fuel is needed to take-off from each planet and to land to each planet. It is known that $$$1$$$ ton of fuel can lift off $$$a_i$$$ tons of rocket from the $$$i$$$-th planet or to land $$$b_i$$$ tons of rocket onto the $$$i$$$-th planet. For example, if the weight of rocket is $$$9$$$ tons, weight of fuel is $$$3$$$ tons and take-off coefficient is $$$8$$$ ($$$a_i = 8$$$), then $$$1.5$$$ tons of fuel will be burnt (since $$$1.5 \\cdot 8 = 9 + 3$$$). The new weight of fuel after take-off will be $$$1.5$$$ tons. Please note, that it is allowed to burn non-integral amount of fuel during take-off or landing, and the amount of initial fuel can be non-integral as well.Help Natasha to calculate the minimum mass of fuel to load into the rocket. Note, that the rocket must spend fuel to carry both useful cargo and the fuel itself. However, it doesn't need to carry the fuel which has already been burnt. Assume, that the rocket takes off and lands instantly.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$2 \\le n \\le 1000$$$) — number of planets. The second line contains the only integer $$$m$$$ ($$$1 \\le m \\le 1000$$$) — weight of the payload. The third line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le 1000$$$), where $$$a_i$$$ is the number of tons, which can be lifted off by one ton of fuel. The fourth line contains $$$n$$$ integers $$$b_1, b_2, \\ldots, b_n$$$ ($$$1 \\le b_i \\le 1000$$$), where $$$b_i$$$ is the number of tons, which can be landed by one ton of fuel. It is guaranteed, that if Natasha can make a flight, then it takes no more than $$$10^9$$$ tons of fuel.", "output_spec": "If Natasha can fly to Mars through $$$(n - 2)$$$ planets and return to Earth, print the minimum mass of fuel (in tons) that Natasha should take. Otherwise, print a single number $$$-1$$$. It is guaranteed, that if Natasha can make a flight, then it takes no more than $$$10^9$$$ tons of fuel. The answer will be considered correct if its absolute or relative error doesn't exceed $$$10^{-6}$$$. Formally, let your answer be $$$p$$$, and the jury's answer be $$$q$$$. Your answer is considered correct if $$$\\frac{|p - q|}{\\max{(1, |q|)}} \\le 10^{-6}$$$.", "sample_inputs": ["2\n12\n11 8\n7 5", "3\n1\n1 4 1\n2 5 3", "6\n2\n4 6 3 3 5 6\n2 6 3 6 5 3"], "sample_outputs": ["10.0000000000", "-1", "85.4800000000"], "notes": "NoteLet's consider the first example.Initially, the mass of a rocket with fuel is $$$22$$$ tons. At take-off from Earth one ton of fuel can lift off $$$11$$$ tons of cargo, so to lift off $$$22$$$ tons you need to burn $$$2$$$ tons of fuel. Remaining weight of the rocket with fuel is $$$20$$$ tons. During landing on Mars, one ton of fuel can land $$$5$$$ tons of cargo, so for landing $$$20$$$ tons you will need to burn $$$4$$$ tons of fuel. There will be $$$16$$$ tons of the rocket with fuel remaining. While taking off from Mars, one ton of fuel can raise $$$8$$$ tons of cargo, so to lift off $$$16$$$ tons you will need to burn $$$2$$$ tons of fuel. There will be $$$14$$$ tons of rocket with fuel after that. During landing on Earth, one ton of fuel can land $$$7$$$ tons of cargo, so for landing $$$14$$$ tons you will need to burn $$$2$$$ tons of fuel. Remaining weight is $$$12$$$ tons, that is, a rocket without any fuel.In the second case, the rocket will not be able even to take off from Earth."}, "positive_code": [{"source_code": "N = gets.to_i\nM = gets.to_i\nas = gets.split.map(&:to_i)\nbs = gets.split.map(&:to_i)\n\ncos = [bs[0], as[-1]]\n(N-2).downto(0) do |i|\n cos.push(bs[i+1], as[i])\nend\n\n\nw = M.to_f\ncos.each do |c|\n if c == 1\n puts -1\n exit\n end\n w *= c.to_f / (c-1)\nend\nputs sprintf(\"%.10f\", w-M)"}, {"source_code": "n = gets.to_i\nm = gets.to_i\nais = gets.strip.split.map(&:to_i)\nbis = gets.strip.split.map(&:to_i)\n\nif ais.include?(1) || bis.include?(1)\n puts -1\n exit 0\nend\n\nais = ais.map{|x|Rational(x)}\nbis = bis.map{|x|Rational(x)}\n\ndef get_weight_before_landing(al, b)\n (b*al)/(b- Rational(1))\nend\n\ndef get_weight_before_takeoff(at, a)\n (a*at)/(a- Rational(1))\nend\n\nrw = Rational(m)\nrw = get_weight_before_landing(rw, bis[0])\n(n-1).downto(1).each do |s|\n rw = get_weight_before_takeoff(rw, ais[s])\n rw = get_weight_before_landing(rw, bis[s])\nend\nrw = get_weight_before_takeoff(rw, ais[0])\nputs (rw - Rational(m)).to_f"}, {"source_code": "require 'bigdecimal'\n\ngets\nm = gets.to_i\np = 1\nq = 1\n\n2.times do\n gets.split.map{|x| x.to_i}.each do |x|\n p *= x\n q *= x-1\n end\nend\n\nif q == 0\n puts (-1)\nelse\n puts m*BigDecimal.new(p)/BigDecimal.new(q)-m\nend\n\n\n"}, {"source_code": "require 'bigdecimal'\n\ngets\nm = gets.to_i\np = 1\nq = 1\n\n2.times do\n gets.split.map{|x| x.to_i}.each do |x|\n p *= x\n q *= x-1\n end\nend\n\nif q == 0\n puts (-1)\nelse\n puts m*BigDecimal.new(p)/q-m\nend"}], "negative_code": [{"source_code": "gets\nm = gets.to_i\np = 1\nq = 1\n\n2.times do\n gets.split.map{|x| x.to_i}.each do |x|\n p *= x\n q *= x-1\n end\nend\n\nif q == 0\n puts (-1)\nelse\n puts m*p.to_f/q\nend\n\n\n"}, {"source_code": "gets\nm = gets.to_i\np = 1\nq = 1\n\n2.times do\n gets.split.map{|x| x.to_i}.each do |x|\n p *= x\n q *= x-1\n end\nend\n\nif q == 0\n puts (-1)\nelse\n puts m*p.to_f/q-m\nend"}, {"source_code": "gets\nm = gets.to_i\np = 1\nq = 1\n\n2.times do\n gets.split.map{|x| x.to_i}.each do |x|\n p *= x\n q *= x-1\n end\nend\n\nif q == 0\n puts (-1)\nelse\n puts m*p/q.to_f-m\nend"}], "src_uid": "d9bd63e03bf51ed87ba73cd15e8ce58d"} {"nl": {"description": "According to a new ISO standard, a flag of every country should have a chequered field n × m, each square should be of one of 10 colours, and the flag should be «striped»: each horizontal row of the flag should contain squares of the same colour, and the colours of adjacent horizontal rows should be different. Berland's government asked you to find out whether their flag meets the new ISO standard.", "input_spec": "The first line of the input contains numbers n and m (1 ≤ n, m ≤ 100), n — the amount of rows, m — the amount of columns on the flag of Berland. Then there follows the description of the flag: each of the following n lines contain m characters. Each character is a digit between 0 and 9, and stands for the colour of the corresponding square.", "output_spec": "Output YES, if the flag meets the new ISO standard, and NO otherwise.", "sample_inputs": ["3 3\n000\n111\n222", "3 3\n000\n000\n111", "3 3\n000\n111\n002"], "sample_outputs": ["YES", "NO", "NO"], "notes": null}, "positive_code": [{"source_code": "n,m = gets.split.map{|i| i.to_i}\n\nt = Array.new(m) {|i| i = []}\nresult = \"YES\"\n\nn.times do\n s = gets.chomp\n k = s[0]\n if s.delete(s[0]).size != 0 then\n result = \"NO\"\n end\n m.times do |i|\n if t[i][-1] == s[i] then\n result = \"NO\"\n end\n t[i] << s[i]\n end\nend\n\nputs result"}, {"source_code": "ns,ms = gets.chomp.split(/ /)\nn = ns.to_i\nm = ms.to_i\nar = Array.new(n)\nfor i in 0..n-1\n\ts = gets.chomp\n\tar[i] = s.split(//)\nend\n\nret = \"YES\"\nfor i in 0..n-1\n\tfor j in 1..m-1\n\t\tif ar[i][j] != ar[i][0]\n\t\t\tret = \"NO\"\n\t\t\tbreak;\n\t\tend\n\tend\n\tif i > 0\n\t\tif ar[i][0] == ar[i-1][0]\n\t\t\tret = \"NO\"\n\t\t\tbreak;\n\t\tend\n\tend\nend\n\nputs ret"}, {"source_code": "n, m = gets.strip.split.map { |e| e.to_i }\ncolor = ''\nbool = true\nn.times do\n\tstr = gets.strip.squeeze\n\tif str.size != 1\n\t\tbool = false\n\telse\n\t\tif str != color\n\t\t\tcolor = str\n\t\telse\n\t\t\tbool = false\n\t\tend\n\tend\nend\nif bool\n\tputs 'YES'\nelse\n\tputs 'NO'\nend"}, {"source_code": "\nn=gets.to_i\nback=-1\n(1..n).each{\n data=gets.split[0]\n if(back==data[0].to_i) then\n puts \"NO\"\n exit\n end\n back=data[0].to_i\n for i in 1..data.length-1\n if(data[i].to_i!=data[0].to_i) then\n puts \"NO\"\n exit\n end\n end\n}\nputs \"YES\"\n"}, {"source_code": "n=gets.to_i\na=gets(p).split.map(&:squeeze)\nputs a.all?{|s|s.size<2}&&(a*\"\").squeeze.size==n ?:YES :\"NO\" \n"}, {"source_code": "n=gets.to_i\na=gets(p).split.map(&:squeeze)\nputs a.all?{|s|s.size<2}&&(a*\"\").squeeze.size==n ?:YES :\"NO\" \n"}, {"source_code": "n=gets.to_i\na=gets(p).split.map(&:squeeze)\nputs a.all?{|s|s.size<2}&&(a*\"\").squeeze.size==n ?:YES :\"NO\" "}, {"source_code": "n=gets.to_i\na=gets(p).split.map(&:squeeze)\nputs a.all?{|s|s.size<2}&&(a*\"\").squeeze.size==n ?:YES :\"NO\" "}, {"source_code": "n=gets.to_i\na=gets(p).split.map(&:squeeze)\nputs a.all?{|s|s.size<2}&&(a*\"\").squeeze.size==n ?:YES :\"NO\" "}, {"source_code": "n=gets.to_i\na=gets(p).split.map(&:squeeze)\nputs a.all?{|s|s.size<2}&&(a*\"\").squeeze.size==n ?:YES :\"NO\" \n"}, {"source_code": "n=gets.to_i\na=gets(p).split.map(&:squeeze)\nputs a.all?{|s|s.size<2}&&(a*\"\").squeeze.size==n ?:YES :\"NO\" \n"}, {"source_code": "#!/usr/bin/ruby\n\nn, m = gets.chomp.split.map{|e| e.to_i}\ncolor = -1\nn.times do \n this_line = gets.chomp.split('').map{|e| e.to_i}\n if this_line[0] == color\n puts \"NO\"\n exit\n end\n color = this_line[0]\n this_line.each{|e|\n if e != color\n puts \"NO\"\n exit\n end\n }\nend\nputs \"YES\"\n"}, {"source_code": "n=gets.to_i\na=gets(p).split.map(&:squeeze)\nputs a.all?{|s|s.size<2}&&(a*\"\").squeeze.size==n ?:YES :\"NO\" "}, {"source_code": "rez = gets.split(\" \").map { |e| e.to_i }\nflag = true\nk = 0\nch = 'a'\nrez[0].times {\n s = gets\n k += 1\n if k != 1 \n if s[0] == ch then\n flag = false\n break\n end\n end\n mas = s.chomp.split(\"\").map { |e| e.to_i }\n mas = mas | mas\n if mas.size != 1 \n flag = false\n end\n ch = s[0]\n\n}\nputs \"YES\" if flag\nputs \"NO\" if !flag"}, {"source_code": "n, m = gets.split.map(&:to_i)\ngrid = []\ncompare = [\"\"]\niso = false\nn.times do\n\trow = gets.chomp.split('')\n\tunique = row.uniq\n\tif unique.length > 1 || unique == compare\n\t\tputs \"NO\"\n\t\tiso = false\n\telse\n\t\tcompare = unique\n\t\tiso = true\n\tend\n\tbreak unless iso\nend\nputs \"YES\" if iso"}, {"source_code": "def main()\n\tnm=gets.chomp.to_s.split(\" \");\n\tn=nm[0].to_i;\n\tm=nm[1].to_i;\n\tfr=gets.chomp;\n\tprevn=fr[0].chr;\n\tnofn=fr.count(prevn);\n\tif nofn==m\n\t\tf=true;\n\t\tfor i in 1...n\n\t\t\tr=gets.chomp;\n\t\t\tif prevn==r[0].chr\n\t\t\t\tf=false;\n\t\t\telse\n\t\t\t\tprevn=r[0].chr;\n\t\t\t\tnofn=r.count(prevn);\n\t\t\t\tif nofn!=m\n\t\t\t\t\tf=false;\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\t\tif f\n\t\t\tputs \"YES\"\n\t\telse\n\t\t\tputs \"NO\"\n\t\tend\n\telse\n\t\tputs \"NO\";\n\tend\nend\nmain()"}, {"source_code": "n=gets.to_i\na=gets(p).split.map(&:squeeze)\nputs a.all?{|s|s.size<2}&&(a*\"\").squeeze.size==n ?:YES :\"NO\" \n"}, {"source_code": "n=gets.to_i\na=gets(p).split.map(&:squeeze)\nputs a.all?{|s|s.size<2}&&(a*\"\").squeeze.size==n ?:YES :\"NO\" \n"}, {"source_code": "input = gets.chomp.split(' ').map { |e| e.to_i }\nflag = Array.new(input[1])\nf_bool = true\n(0...input[0]).each do |i|\n flag[i] = gets.chomp.to_s\nend\n(0...input[0]).each do |i|\n if flag[i].count(flag[i][0]) != flag[i].length\n puts \"NO\"\n f_bool = false\n break\n else\n if flag.length == 1\n puts \"YES\"\n f_bool = false\n break\n else\n if i == 0\n if flag[0] == flag[1]\n puts \"NO\"\n f_bool = false\n break\n end\n else\n if flag[i] == flag[i+1] || flag[i] == flag[i-1]\n puts \"NO\"\n f_bool = false\n break\n end\n end\n end\n end\nend\nputs \"YES\" if f_bool"}, {"source_code": "n=gets.to_i\nrows=gets(p).split.map(&:squeeze)\nputs rows.all?{|str| str.size==1}&&(rows*\"\").squeeze.size==n ?:YES :\"NO\" "}, {"source_code": "n, m = gets.split.collect{|i| i.to_i}\nflag = Array.new(m,-1)\n(1..n).each do\n tmp = gets.chomp.split(\"\").collect{|i| i.to_i}\n if flag[0] == tmp[0] or tmp.count(tmp[0]) != m\n puts \"NO\"\n exit\n end\n flag = tmp\nend\nputs \"YES\"\n"}, {"source_code": "n, m = gets.split.collect{|i| i.to_i}\nflag = -1\n(1..n).each do\n tmp = gets.chomp.split(\"\").collect{|i| i.to_i}\n if flag == tmp[0] or tmp.count(tmp[0]) != m\n puts \"NO\"\n exit\n end\n flag = tmp[0]\nend\nputs \"YES\"\n"}, {"source_code": "n=gets.to_i\na=gets(p).split.map(&:squeeze)\nputs a.all?{|s|s.size<2}&&(a*\"\").squeeze.size==n ?:YES :\"NO\" "}, {"source_code": "n_and_m = gets.split(\" \")\nn = n_and_m[0].to_i\nm = n_and_m[1]\nbandeira = []\nresposta = \"YES\"\nn.times do\n\tlinha = gets.strip\n\tlinha_horizontal = linha.split(\"\")\n\tcor_da_linha = linha_horizontal.uniq\n\t\tif cor_da_linha.size != 1 \n\t\t\tresposta = \"NO\"\n\t\tend\n\tbandeira << cor_da_linha\nend\nfor cor in 1..bandeira.size - 1 do\n\tbreak if resposta == \"NO\" || bandeira.size == 1\n\tif bandeira[cor - 1] == bandeira[cor] || bandeira[cor + 1] == bandeira[cor]\n\t\tresposta = \"NO\"\n\t\tbreak\n\telse\n\t\tresposta = \"YES\"\n\tend\nend\nputs resposta"}, {"source_code": "n=gets.to_i\na=gets(p).split.map(&:squeeze)\nputs a.all?{|s|s.size<2}&&(a*\"\").squeeze.size==n ?:YES :\"NO\" \n"}, {"source_code": "class String\n def uniq?\n self.split('').uniq.size < 2\n end\nend\n\ndef print_no\n puts 'NO'\n exit\nend\n\nn, _ = gets.split.map { |x| x.to_i }\np = gets.chomp\nprint_no unless p.uniq?\nfor __ in 2..n\n s = gets.chomp\n print_no unless s != p && s.uniq?\n p = s\nend\nputs 'YES'"}, {"source_code": "n=gets.to_i\na=gets(p).split.map(&:squeeze)\nputs a.all?{|s|s.size<2}&&(a*\"\").squeeze.size==n ?:YES :\"NO\" \n"}, {"source_code": "s1 = gets.chomp.split\n\nn = s1[0].to_i\nm = s1[1].to_i\nk = 0\nall = []\nok = true;\n\nfor i in 1..n do\n\ts = gets.chomp\n\tt = s[0];\n\tok = true;\n\tfor j in 1..s.length-1 do\n\t\tif t != s[j]\n\t\t\tok = false;\n\t\tend\n\tend\n\tif not ok\n\t\tprint \"NO\"\n\t\tbreak\n\tend\n\tall[k] = t\n\tk+=1\nend\n\nif ok\n\ttr = true\n\tfor i in 0..k-2 do\n\t\tif all[i] == all[i + 1]\n\t\t\tprint \"NO\"\n\t\t\ttr = false\n\t\t\tbreak\n\t\tend\n\tend\n\n\tif tr\n\t\tprint \"YES\"\n\tend\nend\n"}, {"source_code": "n=gets.to_i\na=gets(p).split.map(&:squeeze)\nputs a.all?{|s|s.size<2}&&(a*\"\").squeeze.size==n ?:YES :\"NO\" "}, {"source_code": "\nn,m=gets.split.map(&:to_i)\nprev=\"z\"; result=true\nn.times do\n\tline=gets.split(//)\n\tif line[0]==prev\n\t\tresult=false\n\tend\n\tprev=line[0]\n\tif line.count(line[0])!=m\n\t\tresult=false\n\tend\nend\n\nif result\n\tputs \"YES\\n\"\nelse\n\tputs \"NO\\n\"\nend\n\n\n\n"}, {"source_code": "gets; flag = STDIN.read.split \"\\n\"\n\nflag.each.with_index do |f, i|\n\tif f.length == f.count(f[0])\n\t\tf.length.times do |j|\n\t\t\tif i > 0\n\t\t\t\t(print :NO; exit) if flag[i][j] == flag[i - 1][j]\n\t\t\tend\n\t\tend\n\telse\n\t\tprint :NO\n\t\texit\n\tend\nend\n\nprint :YES\n"}, {"source_code": "t = gets.split.collect{|x| x.to_i}\nn,m = t[0], t[1]\n\nok = 'YES'\nlast = ''\n\nn.times do |i|\n\ts = gets.strip\n\tif last != ''\n\t\tif s == last\n\t\t\tok = 'NO'\n\t\tend\n\tend\n\tif s != s[0]*m\n\t\tok = 'NO'\n\tend\n\tlast = s\nend\n\nputs ok"}, {"source_code": "n=gets.to_i\na=gets(p).split.map(&:squeeze)\nputs a.all?{|s|s.size<2}&&(a*\"\").squeeze.size==n ?:YES :\"NO\" \n"}, {"source_code": "puts %w[YES NO][STDIN.readlines.drop(1).map { |x| x.chomp }.reduce(['*', 0]) { |s, x| [x[0], s.last | (x.chars.to_a.uniq.length == 1 && x[0] != s.first ? 0 : 1)] }.last]\t"}], "negative_code": [{"source_code": "n,m = gets.split.map{|i| i.to_i}\n\nt = {}\nresult = \"YES\"\n\nn.times do\n s = gets.chomp\n k = s[0]\n if s.delete(s[0]).size != 0 then\n result = \"NO\"\n end\n m.times do |i|\n if t[i] then\n if t[i].include? s[i] then\n result = \"NO\"\n else\n t[i] << s[i]\n end\n else\n t[i] = [s[i]]\n end\n end\nend\n\nputs result"}, {"source_code": "n_m = gets.chomp.split(' ').map { |i| i.to_i }\nflag = Array.new(n_m[1])\n(0...n_m[0]).each do |i|\n flag[i] = gets.chomp.to_s\nend\nbol = true\n(0...n_m[1]).each do |i|\n if n_m[0] > 1\n if i == 0\n if flag[i] == flag[i+1]\n bol = false\n puts \"NO\"\n break\n end\n (0...flag[i].length).each do |j|\n if flag[i][0] != flag[i][j]\n bol = false\n puts \"NO\"\n break\n end\n end\n if bol == false\n break\n end\n else\n if flag[i] == flag[i+1] || flag[i] == flag[i-1]\n bol = false\n puts \"NO\"\n break\n end\n if bol == false\n break\n end\n (0...flag[i].length).each do |j|\n if flag[i][0] != flag[i][j]\n bol = false\n puts \"NO\"\n break\n end\n end\n if bol == false\n break\n end\n end\n else\n (0...n_m[1]).each do |j|\n if flag[0][0] != flag[0][j]\n bol = false\n puts \"NO\"\n break\n end\n end\n break if bol == false\n end\nend\nputs \"YES\" if bol == true"}, {"source_code": "n_m = gets.chomp.split(' ').map { |i| i.to_i }\nflag = Array.new(n_m[1])\n(0...n_m[0]).each do |i|\n flag[i] = gets.chomp.to_s\nend\nbol = true\n(0...n_m[1]).each do |i|\n if i== 0\n if flag[i] == flag[i+1]\n bol = false\n puts \"NO\"\n break\n end\n (0...flag[i].length).each do |j|\n if flag[i][0] != flag[i][j]\n bol = false\n puts \"NO\"\n break\n end\n end\n else\n if flag[i] == flag[i+1] || flag[i] == flag[i-1]\n bol = false\n puts \"NO\"\n break\n end\n (0...flag[i].length).each do |j|\n if flag[i][0] != flag[i][j]\n bol = false\n puts \"NO\"\n break\n end\n end\n end\nend\nputs \"YES\" if bol == true"}, {"source_code": "input = gets.chomp.split(' ').map { |e| e.to_i }\nflag = Array.new(input[1])\nf_bool = true\n(0...input[0]).each do |i|\n flag[i] = gets.chomp.to_s\nend\n(0...input[0]).each do |i|\n if flag[i].count(flag[i][0]) != flag[i].length\n puts \"NO\"\n f_bool = false\n else\n if flag.length == 1\n puts \"YES\"\n f_bool = false\n break\n else\n if i == 0\n if flag[0] == flag[1]\n puts \"NO\"\n f_bool = false\n break\n end\n else\n if flag[i] == flag[i+1] || flag[i] == flag[i-1]\n puts \"NO\"\n f_bool = false\n break\n end\n end\n end\n end\nend\nputs \"YES\" if f_bool"}, {"source_code": "n_m = gets.chomp.split(' ').map { |i| i.to_i }\nflag = Array.new(n_m[1])\n(0...n_m[0]).each do |i|\n flag[i] = gets.chomp.to_s\nend\nbol = true\n(0...n_m[1]).each do |i|\n if i== 0\n if flag[i] == flag[i+1]\n bol = false\n puts \"NO\"\n break\n end\n (0...flag[i].length).each do |j|\n if flag[i][0] != flag[i][j]\n bol = false\n puts \"NO\"\n break\n end\n end\n if bol == false\n break\n end\n else\n if flag[i] == flag[i+1] || flag[i] == flag[i-1]\n bol = false\n puts \"NO\"\n break\n end\n if bol == false\n break\n end\n (0...flag[i].length).each do |j|\n if flag[i][0] != flag[i][j]\n bol = false\n puts \"NO\"\n break\n end\n end\n if bol == false\n break\n end\n end\nend\nputs \"YES\" if bol == true"}, {"source_code": "n_m = gets.chomp.split(' ').map { |i| i.to_i }\nflag = Array.new(n_m[1])\n(0...n_m[0]).each do |i|\n flag[i] = gets.chomp.to_s\nend\nbol = true\n(0...n_m[1]).each do |i|\n if n_m[0] == 1\n break\n end\n if i== 0\n if flag[i] == flag[i+1]\n bol = false\n puts \"NO\"\n break\n end\n (0...flag[i].length).each do |j|\n if flag[i][0] != flag[i][j]\n bol = false\n puts \"NO\"\n break\n end\n end\n if bol == false\n break\n end\n else\n if flag[i] == flag[i+1] || flag[i] == flag[i-1]\n bol = false\n puts \"NO\"\n break\n end\n if bol == false\n break\n end\n (0...flag[i].length).each do |j|\n if flag[i][0] != flag[i][j]\n bol = false\n puts \"NO\"\n break\n end\n end\n if bol == false\n break\n end\n end\nend\nputs \"YES\" if bol == true"}, {"source_code": "n_and_m = gets.split(\" \")\nn = n_and_m[0].to_i\nm = n_and_m[1]\nbandeira = []\nresposta = \"\"\nn.times do\n\tlinha = gets.strip\n\tlinha_horizontal = linha.split(\"\")\n\tcor_da_linha = linha_horizontal.uniq\n\t\tif cor_da_linha.size != 1 \n\t\t\tresposta = \"NO\"\n\t\tend\n\tbandeira << cor_da_linha\nend\nfor cor in 1..bandeira.size - 1 do\n\tbreak if resposta == \"NO\"\n\tif bandeira[cor - 1] == bandeira[cor] || bandeira[cor + 1] == bandeira[cor]\n\t\tresposta = \"NO\"\n\t\tbreak\n\telse\n\t\tresposta = \"YES\"\n\tend\nend\nputs resposta"}, {"source_code": "t = gets.split.collect{|x| x.to_i}\nn,m = t[0], t[1]\n\nok = 'YES'\nlast = ''\n\nn.times do |i|\n\ts = gets.strip\n\tif last != ''\n\t\tif s == last or s != s[0]*m\n\t\t\tok = 'NO'\n\t\tend\n\telse\n\t\tlast = s\n\tend\nend\n\nputs ok"}, {"source_code": "t = gets.split.collect{|x| x.to_i}\nn,m = t[0], t[1]\n\nok = 'YES'\nlast = ''\n\nn.times do |i|\n\ts = gets.strip\n\tif last != ''\n\t\tif s == last or s != s[0]*m\n\t\t\tok = 'NO'\n\t\tend\n\tend\n\tlast = s\nend\n\nputs ok"}], "src_uid": "80d3da5aba371f4d572ea928025c5bbd"} {"nl": {"description": "Petya has recently started working as a programmer in the IT city company that develops computer games.Besides game mechanics implementation to create a game it is necessary to create tool programs that can be used by game designers to create game levels. Petya's first assignment is to create a tool that allows to paint different arrows on the screen.A user of this tool will choose a point on the screen, specify a vector (the arrow direction) and vary several parameters to get the required graphical effect. In the first version of the program Petya decided to limit parameters of the arrow by the following: a point with coordinates (px, py), a nonzero vector with coordinates (vx, vy), positive scalars a, b, c, d, a > c.The produced arrow should have the following properties. The arrow consists of a triangle and a rectangle. The triangle is isosceles with base of length a and altitude of length b perpendicular to the base. The rectangle sides lengths are c and d. Point (px, py) is situated in the middle of the triangle base and in the middle of side of rectangle that has length c. Area of intersection of the triangle and the rectangle is zero. The direction from (px, py) point to the triangle vertex opposite to base containing the point coincides with direction of (vx, vy) vector.Enumerate the arrow points coordinates in counter-clockwise order starting from the tip. ", "input_spec": "The only line of the input contains eight integers px, py, vx, vy ( - 1000 ≤ px, py, vx, vy ≤ 1000, vx2 + vy2 > 0), a, b, c, d (1 ≤ a, b, c, d ≤ 1000, a > c).", "output_spec": "Output coordinates of the arrow points in counter-clockwise order. Each line should contain two coordinates, first x, then y. Relative or absolute error should not be greater than 10 - 9.", "sample_inputs": ["8 8 0 2 8 3 4 5"], "sample_outputs": ["8.000000000000 11.000000000000\n4.000000000000 8.000000000000\n6.000000000000 8.000000000000\n6.000000000000 3.000000000000\n10.000000000000 3.000000000000\n10.000000000000 8.000000000000\n12.000000000000 8.000000000000"], "notes": null}, "positive_code": [{"source_code": "px,py,vx,vy,a,b,c,d=gets.split.map &:to_f\nl=Math.hypot(vx,vy)\nputs [[b,0],[0,-a],[0,-c],[-d,-c],[-d,c],[0,c],[0,a]].map{|s,t|[px+s/l*vx+t/2/l*vy,py+s/l*vy-t/2/l*vx]*' '}"}, {"source_code": "px,py,vx,vy,a,b,c,d=gets.split.map &:to_f\nl=Math.hypot(vx,vy)\nputs [[b,0],[0,-a],[0,-c],[-d,-c],[-d,c],[0,c],[0,a]].map{|s,t|[px+s/l*vx+t/2/l*vy,py+s/l*vy-t/2/l*vx]*' '}\n"}, {"source_code": "px,py,vx,vy,a,b,c,d=gets.split.map &:to_f\nl=Math.hypot(vx,vy)\nputs [[b,0],[0,-a],[0,-c],[-d,-c],[-d,c],[0,c],[0,a]].map{|s,t|[px+s/l*vx+t/2/l*vy,py+s/l*vy-t/2/l*vx]*' '}\n"}], "negative_code": [{"source_code": "px,py,vx,vy,a,b,c,d=gets.split.map &:to_f\nl=Math.hypot(vx,vy)\nputs [[b,0],[0,-a],[0,-c],[-d,-c],[-d,c],[0,c],[0,a]].map{|s,t|[px+s/l*vx+t/2/l*vy,py+s/l*vy+t/2/l*vx]*' '}"}], "src_uid": "2cb1e7e4d25f624da934bce5c628a7ee"} {"nl": {"description": "You are given an array $$$a$$$ consisting of $$$n$$$ integers.You can remove at most one element from this array. Thus, the final length of the array is $$$n-1$$$ or $$$n$$$.Your task is to calculate the maximum possible length of the strictly increasing contiguous subarray of the remaining array.Recall that the contiguous subarray $$$a$$$ with indices from $$$l$$$ to $$$r$$$ is $$$a[l \\dots r] = a_l, a_{l + 1}, \\dots, a_r$$$. The subarray $$$a[l \\dots r]$$$ is called strictly increasing if $$$a_l < a_{l+1} < \\dots < a_r$$$.", "input_spec": "The first line of the input contains one integer $$$n$$$ ($$$2 \\le n \\le 2 \\cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$.", "output_spec": "Print one integer — the maximum possible length of the strictly increasing contiguous subarray of the array $$$a$$$ after removing at most one element.", "sample_inputs": ["5\n1 2 5 3 4", "2\n1 2", "7\n6 5 4 3 2 4 3"], "sample_outputs": ["4", "2", "2"], "notes": "NoteIn the first example, you can delete $$$a_3=5$$$. Then the resulting array will be equal to $$$[1, 2, 3, 4]$$$ and the length of its largest increasing subarray will be equal to $$$4$$$."}, "positive_code": [{"source_code": "def solve\n n = gets.strip.to_i\n a = gets.strip.split(' ').map(&:to_i)\n dp = Array.new(n + 1){ Array.new(3) }\n a.each_with_index { |e, i| dp[i][0] = dp[i][1] = dp[i][2] = 1 }\n an = -1\n a.each_with_index { |e, i|\n next if i == 0\n if i > 1\n dp[i][0] = dp[i - 2][1] + 1 if a[i] - a[i - 2] > 0\n end\n if a[i] - a[i - 1] > 0\n dp[i][1] = dp[i - 1][1] + 1\n dp[i][2] = [dp[i - 1][2], dp[i - 1][0]].max + 1\n end\n an = [an, dp[i][0], dp[i][2]].max\n }\n puts an\nend\n\nsolve"}, {"source_code": "MOD=10**9+7\ncnt=0; sum=0; prev=nil; can=true; h=Hash.new(0)\ndef gs() gets.chomp end\ndef gi() gets.to_i end\ndef gsmi() gets.chomp.split.map(&:to_i) end\n\ndef desc(ar) ar.sort!{|x,y|y<=>x} end\ndef min(a,b) a<=b ? a : b end\ndef max(a,b) a>=b ? a : b end\ndef sum(ar) ar.inject(:+) end\n\ndef C(a,b) b==0||a==b ? 1 : (b=a-b if a/20\n cnt=1\n end \n prev=a[i]\n i+=1\nend\nans<<[n-cnt,n-1,cnt] if cnt>0\n#puts ans.to_s\n\nprev_en=-2\nprev_cnt=0\nmax=0\nans.each do |st,en,cnt|\n if (st-prev_en)==2 && a[prev_en]1&&a[prev_en]1&&a[prev_en-1] interval[0]) || (y < interval[1] && y > interval[0])\n arr[index][i +1] = 1\n end\n if (interval[0] < y && interval[0] > x) || ( interval[1] < y && interval[1] > x)\n arr[i +1][index] = 1\n end \n end\n compute_reachability(arr, index)\n intervals << [x,y]\n else\n puts arr[x][y]== 1 ? \"YES\" : \"NO\"\n end\n\nend\n"}], "src_uid": "c686c3592542b70a3b617eb639c0e3f4"} {"nl": {"description": "You are given a binary string $$$s$$$ consisting of $$$n$$$ zeros and ones.Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like \"010101 ...\" or \"101010 ...\" (i.e. the subsequence should not contain two adjacent zeros or ones).Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of \"1011101\" are \"0\", \"1\", \"11111\", \"0111\", \"101\", \"1001\", but not \"000\", \"101010\" and \"11100\".You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 2 \\cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) — the length of $$$s$$$. The second line of the test case contains $$$n$$$ characters '0' and '1' — the string $$$s$$$. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 \\cdot 10^5$$$ ($$$\\sum n \\le 2 \\cdot 10^5$$$).", "output_spec": "For each test case, print the answer: in the first line print one integer $$$k$$$ ($$$1 \\le k \\le n$$$) — the minimum number of subsequences you can divide the string $$$s$$$ to. In the second line print $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le k$$$), where $$$a_i$$$ is the number of subsequence the $$$i$$$-th character of $$$s$$$ belongs to. If there are several answers, you can print any.", "sample_inputs": ["4\n4\n0011\n6\n111111\n5\n10101\n8\n01010000"], "sample_outputs": ["2\n1 2 2 1 \n6\n1 2 3 4 5 6 \n1\n1 1 1 1 1 \n4\n1 1 1 1 1 2 3 4"], "notes": null}, "positive_code": [{"source_code": "t = gets.to_i\nans = []\nt.times do\n n = gets.to_i\n s = gets.chomp.split(\"\").map(&:to_i)\n one_fin = 0\n zero_fin = 0\n index = 0\n arr = Array.new(n)\n max = 0\n n.times do |i|\n if s[i] == 1\n if zero_fin > 0\n one_fin += 1\n zero_fin -= 1\n index += 1\n arr[i] = index\n else\n one_fin += 1\n arr[i] = one_fin\n index += 1\n max = one_fin if max < one_fin\n end\n elsif s[i] == 0\n if one_fin > 0\n zero_fin += 1\n one_fin -= 1\n arr[i] = index\n index -= 1\n else\n zero_fin += 1\n arr[i] = zero_fin\n max = zero_fin if max < zero_fin\n end\n end\n end\n ans << [arr,max]\nend\nans.each do |ar|\n puts ar[1]\n ar[0].each do |x|\n print \"#{x} \"\n end\n puts \"\"\nend"}, {"source_code": "t = gets.to_i\nans = []\nt.times do\n n = gets.to_i\n s = gets.chomp.split(\"\")\n one_fin = 0\n zero_fin = 0\n index = 0\n arr = Array.new(n)\n max = 0\n n.times do |i|\n if s[i] == \"1\"\n if zero_fin > 0\n one_fin += 1\n zero_fin -= 1\n index += 1\n arr[i] = index\n else\n one_fin += 1\n arr[i] = one_fin\n index += 1\n max = one_fin if max < one_fin\n end\n elsif s[i] == \"0\"\n if one_fin > 0\n zero_fin += 1\n one_fin -= 1\n arr[i] = index\n index -= 1\n else\n zero_fin += 1\n arr[i] = zero_fin\n max = zero_fin if max < zero_fin\n end\n end\n end\n ans << [arr,max]\nend\nans.each do |ar|\n puts ar[1]\n ar[0].each do |x|\n print \"#{x} \"\n end\n puts \"\"\nend"}], "negative_code": [{"source_code": "t = gets.to_i\nans = []\nt.times do\n n = gets.to_i\n s = gets.chomp\n one_fin = 0\n zero_fin = 0\n index = 0\n arr = []\n n.times do |i|\n if s[i] == \"0\" && one_fin > 0\n zero_fin += 1\n one_fin -= 1\n arr << index\n index -= 1\n elsif s[i] == \"0\" && one_fin > 0\n zero_fin += 1\n arr << zero_fin\n elsif s[i] == \"0\" && zero_fin > 0\n one_fin += 1\n zero_fin -= 1\n index += 1\n arr << index\n else\n one_fin += 1\n arr << one_fin\n index += 1\n end\n end\n ans << arr\nend\nans.each do |ar|\n puts ar.max\n ar.each do |x|\n print \"#{x} \"\n end\n puts \"\"\nend"}, {"source_code": "t = gets.to_i\nans = []\nt.times do\n n = gets.to_i\n s = gets.chomp\n one_i = 1\n zero_i = 1\n arr = []\n n.times do |i|\n if s[i] == \"0\"\n if zero_i < one_i\n one_i -= 1\n arr << one_i\n else\n arr << zero_i\n zero_i += 1\n end\n elsif s[i] == \"1\"\n if zero_i > one_i\n zero_i -= 1\n arr << zero_i\n else\n arr << one_i\n one_i += 1\n end\n end\n end\n ans << arr\nend\nans.each do |ar|\n puts ar.max\n ar.each do |x|\n print \"#{x} \"\n end\n puts \"\"\nend"}, {"source_code": "t = gets.to_i\nans = []\nt.times do\n n = gets.to_i\n s = gets.chomp\n one_i = 1\n zero_i = 1\n arr = []\n n.times do |i|\n if s[i] == \"0\"\n if zero_i < one_i\n one_i -= 1\n arr << one_i\n else\n arr << zero_i\n zero_i += 1\n end\n elsif s[i] == \"1\"\n if zero_i > one_i\n zero_i -= 1\n arr << zero_i\n else\n arr << one_i\n one_i += 1\n end\n end\n end\n ans << arr\nend\nans.each do |ar|\n ar.each do |x|\n print \"#{x} \"\n end\n puts \"\"\nend"}, {"source_code": "t = gets.to_i\nans = []\nt.times do\n n = gets.to_i\n s = gets.chomp\n one_fin = 0\n zero_fin = 0\n arr = []\n n.times do |i|\n if s[i] == \"0\"\n if one_fin > 0\n zero_fin += 1\n arr << zero_fin\n one_fin -= 1\n else\n zero_fin += 1\n arr << zero_fin\n end\n elsif s[i] == \"1\"\n if zero_fin > 0\n one_fin += 1\n arr << one_fin\n zero_fin -= 1\n else\n one_fin += 1\n arr << one_fin\n end\n end\n end\n ans << arr\nend\nans.each do |ar|\n puts ar.max\n ar.each do |x|\n print \"#{x} \"\n end\n puts \"\"\nend"}], "src_uid": "de57b6b1aa2b6ec4686d1348988c0c63"} {"nl": {"description": "«Bersoft» company is working on a new version of its most popular text editor — Bord 2010. Bord, like many other text editors, should be able to print out multipage documents. A user keys a sequence of the document page numbers that he wants to print out (separates them with a comma, without spaces).Your task is to write a part of the program, responsible for «standardization» of this sequence. Your program gets the sequence, keyed by the user, as input. The program should output this sequence in format l1-r1,l2-r2,...,lk-rk, where ri + 1 < li + 1 for all i from 1 to k - 1, and li ≤ ri. The new sequence should contain all the page numbers, keyed by the user, and nothing else. If some page number appears in the input sequence several times, its appearances, starting from the second one, should be ignored. If for some element i from the new sequence li = ri, this element should be output as li, and not as «li - li».For example, sequence 1,2,3,1,1,2,6,6,2 should be output as 1-3,6.", "input_spec": "The only line contains the sequence, keyed by the user. The sequence contains at least one and at most 100 positive integer numbers. It's guaranteed, that this sequence consists of positive integer numbers, not exceeding 1000, separated with a comma, doesn't contain any other characters, apart from digits and commas, can't end with a comma, and the numbers don't contain leading zeroes. Also it doesn't start with a comma or contain more than one comma in a row.", "output_spec": "Output the sequence in the required format.", "sample_inputs": ["1,2,3,1,1,2,6,6,2", "3,2,1", "30,20,10"], "sample_outputs": ["1-3,6", "1-3", "10,20,30"], "notes": null}, "positive_code": [{"source_code": "s = gets.split(\",\")\ns = s.collect {|i| i = i.to_i}\ns = s.sort\ns = s.uniq\ni = s.size-1\nwhile i>0 do\n k = s[i].to_i\n if k == s[i-1] + 1 then \n if t = s[i].to_s.index(\"-\") then s[i] = s[i][t+1...s[i].size].to_i\n end\n s[i-1] = s[i-1].to_s + \"-\" + s[i].to_s \n s.delete_at(i)\n end\n i -= 1\nend\nstr = s.join(\",\")\nprint \"#{str}\\n\""}, {"source_code": "sa = gets.chomp.split(/,/)\nsa.map!{|x| x.to_i}\nsa.uniq!\nsa.sort!\n\nret = \"\"\nst = 0\nprev = 0\nfor i in 0..sa.length-1\n\tif st == 0\n\t\tret += sa[i].to_s \n\t\tst = sa[i]\n\t\tprev = st\n\telse\n\t\tif sa[i] == prev+1\n\t\t\tprev = sa[i]\n\t\telse\n\t\t\tif st == prev\n\t\t\t\tret += \",\" + sa[i].to_s\n\t\t\telse\n\t\t\t\tret += \"-\" + prev.to_s + \",\" + sa[i].to_s\n\t\t\tend\n\t\t\tst = sa[i]\n\t\t\tprev = st\n\t\tend\n\tend\t\nend\n\nif st != prev\n\tret += \"-\" + prev.to_s\nend\n\nputs ret"}, {"source_code": "a = gets.split(',').map(&:to_i).sort\nb = []\ns = t = a[0]\na.each do |i|\n\tif i <= t + 1 then\n\t\tt = i\n\telse\n\t\tb << [s, t]\n\t\ts = t = i\n\tend\nend\nb << [s, t]\nputs b.map{|a, b| a == b ? a : \"#{a}-#{b}\"} * ','\n\n__END__\n\n"}, {"source_code": "pages = gets.strip.split(\",\").map{|s| s.to_i}\ninc = [false] * 2000\npages.each {|n| inc[n] = true}\n\ni = 1\nwas = false\nwhile i <= 1000\n unless inc[i]\n i += 1\n next\n end\n print \",\" if was\n j = i+1\n j += 1 while inc[j]\n print i\n print \"-\",j-1 if j > i+1\n was = true\n i = j \nend\n"}, {"source_code": "pages = gets.strip.split(\",\").map{|s| s.to_i}\ninc = [false] * 2000\npages.each {|n| inc[n] = true}\n\ni = 1\nres = []\nwhile i <= 1000\n unless inc[i]\n i += 1\n next\n end\n j = i+1\n j += 1 while inc[j]\n if j == i+1\n res << i\n else\n res << [i, \"-\", j-1].join(\"\")\n end\n i = j \nend\n\nputs res.join(\",\")\n"}, {"source_code": "#!usr/bin/env ruby\nx=gets()\narray=x.split(',')\n#puts(array)\narray2=Array.new()\narray.each do\n |a|\n a=a.to_i\n #puts(a)\n array2 << a\nend\narray2.sort!\narray2 = array2 & array2\n#puts(array2)\nprev = -1\nres = \"\"\nlast=0\nfor x in array2 do\n if x-prev != 1 then\n if prev !=-1 && last != prev then\n res << \"-\" << prev.to_s\n last = prev\n end\n if res == \"\" then\n res << x.to_s\n last = x\n else\n res << \",\"\n res << x.to_s\n last = x\n end\n end\n prev = x\n end\n if last != prev then\n res << \"-\" << prev.to_s\n end\nputs (res)\n"}], "negative_code": [{"source_code": "#!usr/bin/env ruby\nx=gets()\narray=x.split(',')\n#puts(array)\narray2=Array.new()\narray.each do\n |a|\n a=a.to_i\n #puts(a)\n array2 << a\nend\narray2.sort!\narray2 = array2 & array2\n#puts(array2)\nprev = -1\nres = \"\"\nlast=0\nfor x in array2 do\n if x-prev != 1 then\n if prev !=-1 then\n res << \"-\" << prev.to_s\n last = prev\n end\n if res == \"\" then\n res << x.to_s\n last = x\n else\n res << \",\"\n res << x.to_s\n last = x\n end\n end\n prev = x\n end\n if last != prev then\n res << \"-\" << prev.to_s\n end\nputs (res)\n"}], "src_uid": "3969ba3e3eb55a896663d2c5a5bc4a84"} {"nl": {"description": "In a far away kingdom young pages help to set the table for the King. As they are terribly mischievous, one needs to keep an eye on the control whether they have set everything correctly. This time the royal chef Gerasim had the impression that the pages have played a prank again: they had poured the juice from one cup to another. Now Gerasim wants to check his hypothesis. The good thing is that chef Gerasim always pour the same number of milliliters of juice to all cups in the royal kitchen. Having thoroughly measured the juice in each cup, Gerasim asked you to write a program that will determine from which cup juice was poured to which one; otherwise, the program should determine that this time the pages set the table diligently.To simplify your task we shall consider the cups to be bottomless so that the juice never overfills a cup and pours out, however much it can be. Besides, by some strange reason in a far away kingdom one can only pour to a cup or from one cup to another an integer number of milliliters of juice.", "input_spec": "The first line contains integer n — the number of cups on the royal table (1 ≤ n ≤ 1000). Next n lines contain volumes of juice in each cup — non-negative integers, not exceeding 104.", "output_spec": "If the pages didn't pour the juice, print \"Exemplary pages.\" (without the quotes). If you can determine the volume of juice poured during exactly one juice pouring, print \"v ml. from cup #a to cup #b.\" (without the quotes), where v represents the volume of poured juice, a represents the number of the cup from which the juice was poured (the cups are numbered with consecutive positive integers starting from one in the order in which the cups are described in the input data), b represents the number of the cup into which the juice was poured. Finally, if the given juice's volumes cannot be obtained using no more than one pouring (for example, the pages poured the juice from one cup to another more than once or the royal kitchen maids poured the juice into the cups incorrectly), print \"Unrecoverable configuration.\" (without the quotes).", "sample_inputs": ["5\n270\n250\n250\n230\n250", "5\n250\n250\n250\n250\n250", "5\n270\n250\n249\n230\n250"], "sample_outputs": ["20 ml. from cup #4 to cup #1.", "Exemplary pages.", "Unrecoverable configuration."], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\na = n.times.map{gets.to_i}\ns = a.inject &:+\nk = a.find_all{|_| _ != s / n}.map{|_| a.index _}\nif s % n != 0 || (!k.empty? && k.size != 2)\n\tputs 'Unrecoverable configuration.'\nelsif k.empty?\n\tputs 'Exemplary pages.'\nelse\n\ti, j = *k\n\ti, j = j, i if a[i] < a[j]\n\tputs \"#{(a[i] - a[j]) / 2} ml. from cup ##{j + 1} to cup ##{i + 1}.\"\nend\n"}, {"source_code": "n=gets.to_i\na=[]\nn.times{\n a.push(gets.to_i)\n}\nok=1\n(n-1).times{|i|\n if a[i]!=a[i+1] then\n ok=0\n break\n end\n}\nif ok==1 then\n puts \"Exemplary pages.\"\n exit\nend\ncnt={}\ncnt.default=0\na.each{|i|\n cnt[i]+=1\n}\nif cnt.length<5 then\n n.times{|i|\n if cnt[a[i]]==1 then\n n.times{|j|\n if cnt[a[j]]==1 then\n if a[i]>a[j] then\n v=a[i]-a[j]\n v/=2\n a[i]-=v\n a[j]+=v\n ok=1\n (n-1).times{|i|\n if a[i]!=a[i+1] then \n ok=0\n break\n end\n }\n if ok==1 then\n printf \"%d ml. from cup #%d to cup #%d.\\n\",v,j+1,i+1\n exit\n end\n a[i]+=v\n a[j]-=v;\n end\n end\n }\n end \n }\nend\nputs \"Unrecoverable configuration.\"\n"}, {"source_code": "def check(a, b, n)\n\tdiff = b[0]-a[0]\n\tif diff.odd? || (a[0]+b[0])/2 != n\n\t\tputs \"Unrecoverable configuration.\"\n\t\texit\n\tend\n\tputs \"#{(diff)/2} ml. from cup ##{a[1]} to cup ##{b[1]}.\"\n\texit\nend\n\nn = gets.to_i\narr = Array.new(n) { |idx| [gets.to_i, idx+1] }.sort.freeze\n\nif arr[0][0] == arr[-1][0]\n\tputs \"Exemplary pages.\"\n\texit\nend\n\nif arr.size == 2\n\tdiff = arr[1][0]-arr[0][0]\n\tif diff.odd?\n\t\tputs \"Unrecoverable configuration.\"\n\t\texit\n\tend\n\tcheck(arr[0], arr[1], (arr[0][0]+arr[1][0])/2)\nend\n\nif arr[1][0] != arr[-2][0]\n\tputs \"Unrecoverable configuration.\"\n\texit\nend\n\ncheck(arr[0], arr[-1], arr[1][0])\n"}, {"source_code": "n = STDIN.readline.to_i\ncups = []\nn.times {|ix| cups.push([STDIN.readline.to_i, ix]) }\ncount = Hash.new(0)\ncups.each {|volume, ix| count[volume] += 1 }\nif count.keys.count == 1\n puts 'Exemplary pages.'\n exit\nend\n\ndef fail\n puts 'Unrecoverable configuration.'\n exit\nend\n\ncups.sort!\ndelta = cups[-1][0] - cups[0][0]\nfail if delta % 2 == 1\npour, source, target = delta/2, cups[0][1]+1, cups[-1][1]+1\n\ncups[0][0] += pour\ncups[-1][0] -= pour\ncount = Hash.new(0)\ncups.each {|volume, ix| count[volume] += 1 }\nfail if count.keys.count != 1\n\nputs '%d ml. from cup #%d to cup #%d.' % [pour, source, target]\n"}, {"source_code": "#!/usr/bin/ruby\n# coding: utf-8\n\nls = $stdin.readlines.map(&:chomp)\nn = ls[0].to_i\nvs = ls.drop(1).map(&:to_i)\n\ns = vs.inject(0, &:+)\nif s % n != 0\n puts 'Unrecoverable configuration.'\n exit 0\nend\nave = s / n\n\na = []\nvs.each_with_index do |v, i|\n if v != ave\n a << [v, i]\n end\nend\nif a.empty?\n puts 'Exemplary pages.'\nelsif a.size == 2 and a[0][0] + a[1][0] == ave * 2\n x, y = a.sort\n puts \"#{ave - x[0]} ml. from cup ##{x[1]+1} to cup ##{y[1]+1}.\"\nelse\n puts 'Unrecoverable configuration.'\nend\n"}, {"source_code": "n = gets.to_i\narr = []\nn.times do\n\tarr << gets.to_i\nend\n# arr.sort!\nsorted = arr.sort\nnormal = sorted[1]\nchunk = sorted.chunk{|x| x}\n# puts \"#{chunk.count}\"\nans = false\nif chunk.count == 1\n\tputs \"Exemplary pages.\"\n\tans = true\nelsif chunk.count == 2 || chunk.count == 3\n\tmin,min_ind,max,max_ind = arr.each_with_index.minmax.flatten\n\t# puts \"min #{min} ind #{min_ind} #{max} #{max_ind}\" if n == 198\n\t# puts \"#{arr.count(min) == 1 && arr.count(max) == 1}\"\n\n\tel = (min + max) / 2\n\tml = max - el\n\n\tif (arr.count(min) == 1) && (arr.count(max) == 1) && (el-min == ml)\n\t\tif !(chunk.count == 3 && el != normal)\n\t\t\tans = true\n\t\t\tputs \"#{ml} ml. from cup ##{min_ind+1} to cup ##{max_ind+1}.\"\n\t\tend\n\tend\nend\n\nputs \"Unrecoverable configuration.\" unless ans"}], "negative_code": [{"source_code": "n = STDIN.readline.to_i\ncups = []\nn.times {|ix| cups.push([STDIN.readline.to_i, ix]) }\ncount = Hash.new(0)\ncups.each {|volume, ix| count[volume] += 1 }\nkey_num = count.keys.count\nif key_num == 1\n puts 'Exemplary pages.'\n exit\nend\ndef fail\n puts 'Unrecoverable configuration.'\n exit\nend\nfail if key_num != 2 + (n > 2 ? 1 : 0)\ncups.sort!\nfail if n > 3 and cups[1][0] != cups[-2][0]\ndelta = cups[-1][0] - cups[0][0]\nfail if delta % 2 == 1\npour, source, target = delta/2, cups[0][1]+1, cups[-1][1]+1\nputs '%d ml. from cup #%d to cup #%d.' % [pour, source, target]\n"}, {"source_code": "n = STDIN.readline.to_i\ncups = []\nn.times {|ix| cups.push([STDIN.readline.to_i, ix]) }\ncount = Hash.new(0)\ncups.each {|volume, ix| count[volume] += 1 }\nkey_num = count.keys.count\nif key_num == 1\n puts 'Exemplary pages.'\n exit\nend\ndef fail\n puts 'Unrecoverable configuration.'\n exit\nend\nfail if key_num != 2 + (n > 2 ? 1 : 0)\nfail if n > 3 and cups[1] != cups[-2]\ncups.sort!\ndelta = cups[-1][0] - cups[0][0]\nfail if delta % 2 == 1\npour, source, target = delta/2, cups[0][1]+1, cups[-1][1]+1\nputs '%d ml. from cup #%d to cup #%d.' % [pour, source, target]\n"}, {"source_code": "n = STDIN.readline.to_i\ncups = []\nn.times {|ix| cups.push([STDIN.readline.to_i, ix]) }\ncount = Hash.new(0)\ncups.each {|volume, ix| count[volume] += 1 }\nkey_num = count.keys.count\nif key_num == 1\n puts 'Exemplary pages.'\n exit\nend\ndef fail\n puts 'Unrecoverable configuration.'\n exit\nend\nfail if key_num > 3 or n > 2 and key_num == 2\ncups.sort!\ndelta = cups[-1][0] - cups[0][0]\nfail if delta % 2 == 1\npour, source, target = delta/2, cups[0][1]+1, cups[-1][1]+1\nputs '%d ml. from cup #%d to cup #%d.' % [pour, source, target]\n"}, {"source_code": "n = STDIN.readline.to_i\ncups = []\nn.times {|ix| cups.push([STDIN.readline.to_i, ix]) }\ncount = Hash.new(0)\ncups.each {|volume, ix| count[volume] += 1 }\nkey_num = count.keys.count\nif key_num == 1\n puts 'Exemplary pages.'\n exit\nend\ndef fail\n puts 'Unrecoverable configuration.'\n exit\nend\nfail if key_num > 3\ncups.sort!\ndelta = cups[-1][0] - cups[0][0]\nfail if delta % 2 == 1\npour, source, target = delta/2, cups[0][1]+1, cups[-1][1]+1\nputs '%d ml. from cup #%d to cup #%d.' % [pour, source, target]\n"}, {"source_code": "n = STDIN.readline.to_i\ncups = []\nn.times {|ix| cups.push([STDIN.readline.to_i, ix]) }\ncount = Hash.new(0)\ncups.each {|volume, ix| count[volume] += 1 }\nkey_num = count.keys.count\nif key_num == 1\n puts 'Exemplary pages.'\n exit\nend\ndef fail\n puts 'Unrecoverable configuration.'\n exit\nend\nfail if key_num > 3\ncups.sort!\ndelta = cups[-1][0] - cups[0][0]\nfail if delta % 2 == 1\npour, source, target = delta/2, cups[-1][1]+1, cups[0][1]+1\nputs '%d ml. from cup #%d to cup #%d.' % [pour, source, target]\n"}, {"source_code": "n = STDIN.readline.to_i\ncups = []\nn.times {|ix| cups.push([STDIN.readline.to_i, ix]) }\ncount = Hash.new(0)\ncups.each {|volume, ix| count[volume] += 1 }\nkey_num = count.keys.count\nif key_num == 1\n puts 'Exemplary pages.'\n exit\nend\ndef fail\n puts 'Unrecoverable configuration.'\n exit\nend\nfail if key_num > 3 or (n > 2 and key_num == 2)\ncups.sort!\ndelta = cups[-1][0] - cups[0][0]\nfail if delta % 2 == 1\npour, source, target = delta/2, cups[0][1]+1, cups[-1][1]+1\nputs '%d ml. from cup #%d to cup #%d.' % [pour, source, target]\n"}, {"source_code": "#!/usr/bin/ruby\n# coding: utf-8\n\nls = $stdin.readlines.map(&:chomp)\nn = ls[0].to_i\nvs = ls.drop(1).map(&:to_i)\n\ns = vs.inject(0, &:+)\nif s % n != 0\n puts 'Unrecoverable configuration.'\n exit 0\nend\n\na = []\nvs.each_with_index do |v, i|\n if v != s / n\n a << [v, i]\n end\nend\nif a.empty?\n puts 'Exemplary pages.'\nelsif a.size == 2 and a[0][0] + a[1][0] == s / n * 2\n x, y = a.sort\n puts \"#{s/n - x[0]} ml. from cup ##{x[1]+1} to cup ##{y[1]+1}.\"\nelse\n puts 'Unrecoverable configuration'\nend\n"}, {"source_code": "#!/usr/bin/ruby\n# coding: utf-8\n\nls = $stdin.readlines.map(&:chomp)\nn = ls[0].to_i\nvs = ls.drop(1).map(&:to_i)\n\ns = vs.inject(0, &:+)\nif s % n != 0\n puts 'Unrecoverable configuration.'\n exit 0\nend\n\na = []\nvs.each_with_index do |v, i|\n if v != s / n\n a << [v, i]\n end\nend\nif a.empty?\n puts 'Exemplay pages.'\nelsif a.size == 2 and a[0][0] + a[1][0] == s / n * 2\n x, y = a.sort\n puts \"#{s/n - x[0]} ml. from cup ##{x[1]+1} to cup ##{y[1]+1}.\"\nelse\n puts 'Unrecoverable configuration'\nend\n"}, {"source_code": "n = gets.to_i\narr = []\nn.times do\n\tarr << gets.to_i\nend\n# arr.sort!\nchunk = arr.sort.chunk{|x| x}\n# puts \"#{chunk.count}\"\nif chunk.count == 1\n\tputs \"Exemplary pages.\"\nelsif chunk.count == 2 || chunk.count == 3\n\tmin,min_ind,max,max_ind = arr.each_with_index.minmax.flatten\n\t# puts \"min #{min} ind #{min_ind} #{max} #{max_ind}\" if n == 198\n\t# puts \"#{arr.count(min) == 1 && arr.count(max) == 1}\"\n\n\tel = (min + max) / 2\n\tml = max - el\n\n\tif (arr.count(min) == 1) && (arr.count(max) == 1) && el == arr.sort[1] && (el-min == ml)\n\n\t\tputs \"#{ml} ml. from cup ##{min_ind+1} to cup ##{max_ind+1}.\"\n\telse\n\t\tputs \"Unrecoverable configuration.\"\n\tend\nelse\n\tputs \"Unrecoverable configuration.\"\nend"}, {"source_code": "n = gets.to_i\narr = []\nn.times do\n\tarr << gets.to_i\nend\n# arr.sort!\nchunk = arr.sort.chunk{|x| x}\n# puts \"#{chunk.count}\"\nif chunk.count == 1\n\tputs \"Exemplary pages.\"\nelsif chunk.count == 3\n\tmin,min_ind,max,max_ind = arr.each_with_index.minmax.flatten\n\t# puts \"min #{min} ind #{min_ind} #{max} #{max_ind}\"\n\t# puts \"#{arr.count(min) == 1 && arr.count(max) == 1}\"\n\tif (arr.count(min) == 1) && (arr.count(max) == 1)\n\n\t\tel = (min + max) / 2\n\t\tml = max - el\n\n\t\tputs \"#{ml} ml. from cup ##{min_ind+1} to cup ##{max_ind+1}.\"\n\telse\n\t\tputs \"Unrecoverable configuration.\"\n\tend\nelse\n\tputs \"Unrecoverable configuration.\"\nend"}, {"source_code": "n = gets.to_i\narr = []\nn.times do\n\tarr << gets.to_i\nend\n# arr.sort!\nchunk = arr.sort.chunk{|x| x}\n# puts \"#{chunk.count}\"\nif chunk.count == 1\n\tputs \"Exemplary pages.\"\nelsif chunk.count == 2 || chunk.count == 3\n\tmin,min_ind,max,max_ind = arr.each_with_index.minmax.flatten\n\t# puts \"min #{min} ind #{min_ind} #{max} #{max_ind}\"\n\t# puts \"#{arr.count(min) == 1 && arr.count(max) == 1}\"\n\n\tel = (min + max) / 2\n\tml = max - el\n\tif (arr.count(min) == 1) && (arr.count(max) == 1) && (el-min == ml)\n\n\t\tputs \"#{ml} ml. from cup ##{min_ind+1} to cup ##{max_ind+1}.\"\n\telse\n\t\tputs \"Unrecoverable configuration.\"\n\tend\nelse\n\tputs \"Unrecoverable configuration.\"\nend"}, {"source_code": "n = gets.to_i\narr = []\nn.times do\n\tarr << gets.to_i\nend\n# arr.sort!\nchunk = arr.sort.chunk{|x| x}\n# puts \"#{chunk.count}\"\nif chunk.count == 1\n\tputs \"Exemplary pages.\"\nelsif chunk.count == 2 || chunk.count == 3\n\tmin,min_ind,max,max_ind = arr.each_with_index.minmax.flatten\n\tputs \"min #{min} ind #{min_ind} #{max} #{max_ind}\" if n == 198\n\t# puts \"#{arr.count(min) == 1 && arr.count(max) == 1}\"\n\n\tel = (min + max) / 2\n\tml = max - el\n\n\tif (arr.count(min) == 1) && (arr.count(max) == 1) && (el-min == ml)\n\n\t\tputs \"#{ml} ml. from cup ##{min_ind+1} to cup ##{max_ind+1}.\"\n\telse\n\t\tputs \"Unrecoverable configuration.\"\n\tend\nelse\n\tputs \"Unrecoverable configuration.\"\nend"}], "src_uid": "7bfc0927ea7abcef661263e978612cc5"} {"nl": {"description": "Sergey attends lessons of the N-ish language. Each lesson he receives a hometask. This time the task is to translate some sentence to the N-ish language. Sentences of the N-ish language can be represented as strings consisting of lowercase Latin letters without spaces or punctuation marks.Sergey totally forgot about the task until half an hour before the next lesson and hastily scribbled something down. But then he recollected that in the last lesson he learned the grammar of N-ish. The spelling rules state that N-ish contains some \"forbidden\" pairs of letters: such letters can never occur in a sentence next to each other. Also, the order of the letters doesn't matter (for example, if the pair of letters \"ab\" is forbidden, then any occurrences of substrings \"ab\" and \"ba\" are also forbidden). Also, each pair has different letters and each letter occurs in no more than one forbidden pair.Now Sergey wants to correct his sentence so that it doesn't contain any \"forbidden\" pairs of letters that stand next to each other. However, he is running out of time, so he decided to simply cross out some letters from the sentence. What smallest number of letters will he have to cross out? When a letter is crossed out, it is \"removed\" so that the letters to its left and right (if they existed), become neighboring. For example, if we cross out the first letter from the string \"aba\", we get the string \"ba\", and if we cross out the second letter, we get \"aa\".", "input_spec": "The first line contains a non-empty string s, consisting of lowercase Latin letters — that's the initial sentence in N-ish, written by Sergey. The length of string s doesn't exceed 105. The next line contains integer k (0 ≤ k ≤ 13) — the number of forbidden pairs of letters. Next k lines contain descriptions of forbidden pairs of letters. Each line contains exactly two different lowercase Latin letters without separators that represent the forbidden pairs. It is guaranteed that each letter is included in no more than one pair.", "output_spec": "Print the single number — the smallest number of letters that need to be removed to get a string without any forbidden pairs of neighboring letters. Please note that the answer always exists as it is always possible to remove all letters.", "sample_inputs": ["ababa\n1\nab", "codeforces\n2\ndo\ncs"], "sample_outputs": ["2", "1"], "notes": "NoteIn the first sample you should remove two letters b.In the second sample you should remove the second or the third letter. The second restriction doesn't influence the solution."}, "positive_code": [{"source_code": "s = gets.chomp\nk = gets.to_i\n$h = {}\nfor i in 0..k-1\n\tw = gets.chomp\n\t$h[w[0]] = i\n\t$h[w[1]] = i\nend\n\ni = 0\ncount = 0\nwhile(i < s.length)\n\tc = s[i]\n\tg = $h[c]\n\ti += 1\n\tf = 1\n\tt = 1\n\twhile(i < s.length && $h.key?(s[i]) && g == $h[s[i]])\n\t\tif(s[i] == c)\n\t\t\tf += 1\n\t\tend\n\t\tt += 1\n\t\ti += 1\n\t\t\n\tend\n\n\t#puts c + \": \" + [f, t - f].min.to_s\n\tcount += [f, t - f].min\nend\nputs count"}], "negative_code": [{"source_code": "s = gets.chomp\nk = gets.to_i\n$h = {}\nfor i in 0..k-1\n\tw = gets.chomp\n\tif $h.key? (w[0])\n\t\t$h[w[0]] = $h[w[0]] << w[1]\n\telse\n\t\t$h[w[0]] = [w[1]]\n\tend\n\n\tif $h.key? (w[1])\n\t\t$h[w[1]] = $h[w[1]] << w[0]\n\telse\n\t\t$h[w[1]] = [w[0]]\n\tend\nend\n\n$dp = {}\n\ndef minEdit s,i\n\tif i >= s.length - 1\n\t\treturn 0\n\telsif ($h.key?(s[i]) && $h[s[i]].include?(s[i + 1]))\n\t\ta = s.dup\n\t\ta.slice! (i)\n\t\tb = s.dup\n\t\tb.slice! (i + 1)\n\t\tkl = a#a.slice([i - 1, 0].max..s.length - 1)\n\t\tkr = b#b.slice(i..s.length - 1)\n\t\t#puts \"kl: \" + kl\n\t\t#puts \"kr: \" + kr\n\t\tleft = 0\n\t\tright = 0\n\t\tif $dp.key? kl\n\t\t\tleft = $dp[kl]\n\t\telse\n\t\t\tleft = minEdit(a, [i - 1, 0].max)\n\t\t\t$dp[kl] = left\n\t\tend\n\t\tif $dp.key? kr\n\t\t\tright = $dp[kr]\n\t\telse\n\t\t\tright = minEdit(b, i)\n\t\t\t$dp[kr] = right\n\t\tend\n\t\tputs \"kl: \" + kl + \", \" + left.to_s\n\t\tputs \"kr: \" + kr + \", \" + right.to_s\n\t \tret = [left, right].min + 1\n\telse\n\t\treturn minEdit s, (i + 1)\n\tend\nend\n\nputs minEdit(s, 0)"}, {"source_code": "s = gets.chomp\nk = gets.to_i\n$h = {}\nfor i in 0..k-1\n\tw = gets.chomp\n\t$h[w[0]] = i\n\t$h[w[1]] = i\nend\n\ni = 0\ncount = 0\nwhile(i < s.length)\n\tc = s[i]\n\tg = $h[c]\n\ti += 1\n\tf = 1\n\tt = 1\n\twhile(i < s.length && g == $h[s[i]])\n\t\tif(s[i] == c)\n\t\t\tf += 1\n\t\telse\n\t\t\tt += 1\n\t\tend\n\t\ti += 1\n\t\tcount += [f, t - f].min\n\tend\nend\nputs count"}], "src_uid": "da2b3450a3ca05a60ea4de6bab9291e9"} {"nl": {"description": "This is an interactive problem. You should use flush operation after each printed line. For example, in C++ you should use fflush(stdout), in Java you should use System.out.flush(), and in Pascal — flush(output).In this problem you should guess an array a which is unknown for you. The only information you have initially is the length n of the array a.The only allowed action is to ask the sum of two elements by their indices. Formally, you can print two indices i and j (the indices should be distinct). Then your program should read the response: the single integer equals to ai + aj.It is easy to prove that it is always possible to guess the array using at most n requests.Write a program that will guess the array a by making at most n requests.", "input_spec": null, "output_spec": null, "sample_inputs": ["5\n \n9\n \n7\n \n9\n \n11\n \n6"], "sample_outputs": ["? 1 5\n \n? 2 3\n \n? 4 1\n \n? 5 2\n \n? 3 4\n \n! 4 6 1 5 5"], "notes": "NoteThe format of a test to make a hack is: The first line contains an integer number n (3 ≤ n ≤ 5000) — the length of the array. The second line contains n numbers a1, a2, ..., an (1 ≤ ai ≤ 105) — the elements of the array to guess. "}, "positive_code": [{"source_code": "size = gets.to_i\ndone = true\ni=1\ntableau_de_somme = []\ntableau_final = []\ntableau_final[0] = \"!\"\n\nwhile done do\n\tputs \"?\"+ \" \" + i.to_s + \" \" + (i+=1).to_s\n\t$stdout.flush\n\ttableau_de_somme << gets.to_i\n\tdone = false if i == size\nend\n\nresultat = []\nresultat[0] = 0\na = 0\n\nwhile a < (size-2) do\n\tresultat[0] += tableau_de_somme[a]\n\tresultat[0] -= tableau_de_somme[a += 1]\n\ta += 1\nend\t\n\nif size.modulo(2) == 0\n\tputs \"?\"+ \" \" + 1.to_s + \" \" + (size-1).to_s\n\t$stdout.flush\n\ttableau_de_somme << gets.to_i\nelse \n\tputs \"?\"+ \" \" + 1.to_s + \" \" + (size).to_s\n\t$stdout.flush\n\ttableau_de_somme << gets.to_i\nend\n\nresultat[0] += tableau_de_somme[size-1] \nresultat[0] /= 2\ntableau_final << resultat[0]\n\ntableau_de_somme.each do |val|\n\ttableau_final << val - tableau_final.last\nend\t\n\ntableau_final.pop\n\ntableau_final.each do |val|\n\tprint val.to_s + \" \"\n\t$stdout.flush\nend\t"}, {"source_code": "#!/usr/bin/ruby\n\nsize = gets.to_i\nresolu = true\ni=1\ntableau_de_somme = []\ntableau_final = []\ntableau_final[0] = \"!\"\n\nwhile resolu do\n\tputs \"?\"+ \" \" + i.to_s + \" \" + (i+=1).to_s\n\t$stdout.flush\n\ttableau_de_somme << gets.to_i\n\tresolu = false if i == size\nend\nresultat = []\nresultat[0] =0\niterator=0\nwhile iterator < (size-2) do\n\tresultat[0] += tableau_de_somme[iterator]\n\tresultat[0] -= tableau_de_somme[iterator+=1]\n\titerator+=1\nend\t\nif size.modulo(2) == 0\n\tputs \"?\"+ \" \" + 1.to_s + \" \" + (size-1).to_s\n\t$stdout.flush\n\ttableau_de_somme << gets.to_i\nelse \n\tputs \"?\"+ \" \" + 1.to_s + \" \" + (size).to_s\n\t$stdout.flush\n\ttableau_de_somme << gets.to_i\nend\nresultat[0] += tableau_de_somme[size-1] \nresultat[0] /= 2\ntableau_final << resultat[0]\n\ntableau_de_somme.each do |val|\n\ttableau_final << val - tableau_final.last\nend\t\ntableau_final.pop\ntableau_final.each do |val|\n\tprint val.to_s + \" \"\n\t$stdout.flush\nend\t"}], "negative_code": [{"source_code": "size = gets.to_i\nresolu = true\nindice=0\ni=1\nputs indice\ntableau_de_somme = []\ntableau_final = []\ntableau_final[0] = \"!\"\n\nwhile resolu do\n $stdout.print \"?\"+ \" \" + i.to_s + \" \" + (i+=1).to_s\n $stdout.flush\n tableau_de_somme << gets.to_i\n resolu = false if i == size\nend\nresultat = []\nresultat[0] =0\niterator=0\nwhile iterator < (size-2) do\n resultat[0] += tableau_de_somme[iterator]\n resultat[0] -= tableau_de_somme[iterator+=1]\n iterator+=1\nend \nif size.modulo(2) == 0\n $stdout.print \"?\"+ \" \" + 1.to_s + \" \" + (size-1).to_s\n $stdout.flush\n tableau_de_somme << gets.to_i\nelse \n $stdout.print \"?\"+ \" \" + 1.to_s + \" \" + (size).to_s\n $stdout.flush\n tableau_de_somme << gets.to_i\nend\nresultat[0] += tableau_de_somme[size-1] \nresultat[0] /= 2\ntableau_final << resultat[0]\n\ntableau_de_somme.each do |val|\n tableau_final << val - tableau_final.last\nend \ntableau_final.pop\ntableau_final.each do |val|\n print val.to_s + \" \"\nend "}], "src_uid": "1898e591b40670173e4c33e08ade48ba"} {"nl": {"description": "Vanya and his friend Vova play a computer game where they need to destroy n monsters to pass a level. Vanya's character performs attack with frequency x hits per second and Vova's character performs attack with frequency y hits per second. Each character spends fixed time to raise a weapon and then he hits (the time to raise the weapon is 1 / x seconds for the first character and 1 / y seconds for the second one). The i-th monster dies after he receives ai hits. Vanya and Vova wonder who makes the last hit on each monster. If Vanya and Vova make the last hit at the same time, we assume that both of them have made the last hit.", "input_spec": "The first line contains three integers n,x,y (1 ≤ n ≤ 105, 1 ≤ x, y ≤ 106) — the number of monsters, the frequency of Vanya's and Vova's attack, correspondingly. Next n lines contain integers ai (1 ≤ ai ≤ 109) — the number of hits needed do destroy the i-th monster.", "output_spec": "Print n lines. In the i-th line print word \"Vanya\", if the last hit on the i-th monster was performed by Vanya, \"Vova\", if Vova performed the last hit, or \"Both\", if both boys performed it at the same time.", "sample_inputs": ["4 3 2\n1\n2\n3\n4", "2 1 1\n1\n2"], "sample_outputs": ["Vanya\nVova\nVanya\nBoth", "Both\nBoth"], "notes": "NoteIn the first sample Vanya makes the first hit at time 1 / 3, Vova makes the second hit at time 1 / 2, Vanya makes the third hit at time 2 / 3, and both boys make the fourth and fifth hit simultaneously at the time 1.In the second sample Vanya and Vova make the first and second hit simultaneously at time 1."}, "positive_code": [{"source_code": "n, x, y = gets.split.map(&:to_i)\nxy = x + y\n\ng = []\nn.times do\n g << gets.to_i\nend\n\ng.each do |health|\n health %= xy\n hitsx = health * x / xy\n hitsy = health * y / xy\n health -= hitsx + hitsy\n tx = hitsx * y\n ty = hitsy * x\n while 0 < health\n tx1, ty1 = tx + y, ty + x\n if tx1 < ty1\n tx = tx1\n health -= 1\n elsif ty1 < tx1\n ty = ty1\n health -= 1\n else\n tx, ty = tx1, ty1\n health -= 1\n end\n end\n if tx < ty\n puts 'Vova'\n elsif ty < tx\n puts 'Vanya'\n else\n puts 'Both'\n end\nend\n"}], "negative_code": [{"source_code": "#! /usr/bin/env ruby\n# coding: utf-8\n\nF = \"Vonya\"\nS = \"Vova\"\nB = \"Both\"\nn, x, y = gets.split.map(&:to_i)\none = x + y\nans = []\nl = []\nn.times do\n a_i = gets.to_i\n if x == y\n puts B\n next\n end\n remain = a_i % one\n remain.times do |i|\n a = (i + 1) / x.to_f\n b = (i + 1) / y.to_f\n next if l.index([a, F]) or l.index([b, S])\n l << [a, F]\n l << [b, S]\n end\n l.sort!\n index = remain - 1\n if ans[index]\n puts ans[index]\n next\n end\n c = l[index]\n before = l[index - 1] if 0 < index\n after = l[index + 1] if index < l.length\n if (before and c[0] == before[0]) or (after and c[0] == after[0])\n ans[index] = B\n else\n ans[index] = c[1]\n end\n puts ans[index]\nend\n"}, {"source_code": "#! /usr/bin/env ruby\n# coding: utf-8\n\nF = 'Vanya'\nS = 'Vova'\nB = 'Both'\n\ndef ans_list(x, y)\n l = []\n (x+y).times do |i|\n a = (i + 1) / x.to_f\n b = (i + 1) / y.to_f\n l << [a, F] if l.length < (x + y)\n l << [b, S] if l.length < (x + y)\n break if (x + y) <= l.length\n end\n l.sort\nend\n\nn,x,y = gets.chomp.split.map(&:to_i)\nl = ans_list(x, y)\n\none = x + y\nans = []\nn.times do\n a_i = gets.chomp.to_i\n if x == y\n puts B\n else\n remain = a_i % one\n if ans[remain - 1]\n puts ans[remain - 1]\n next\n else\n index = remain - 1\n c = l[index]\n before = l[index - 1] if 0 < index\n after = l[index + 1] if index < l.length\n if (before and c[0] == before[0]) or (after and c[0] == after[0])\n ans[index] = B\n else\n ans[index] = c[1]\n end\n puts ans[index]\n end\n end\nend\n\n\n# ヒット数が等しい物があるかもしれないので結果をキャッシュする\n"}], "src_uid": "f98ea97377a06963d1e6c1c215ca3a4a"} {"nl": {"description": "Vasya's got a birthday coming up and his mom decided to give him an array of positive integers a of length n.Vasya thinks that an array's beauty is the greatest common divisor of all its elements. His mom, of course, wants to give him as beautiful an array as possible (with largest possible beauty). Unfortunately, the shop has only one array a left. On the plus side, the seller said that he could decrease some numbers in the array (no more than by k for each number).The seller can obtain array b from array a if the following conditions hold: bi > 0; 0 ≤ ai - bi ≤ k for all 1 ≤ i ≤ n.Help mom find the maximum possible beauty of the array she will give to Vasya (that seller can obtain).", "input_spec": "The first line contains two integers n and k (1 ≤ n ≤ 3·105; 1 ≤ k ≤ 106). The second line contains n integers ai (1 ≤ ai ≤ 106) — array a.", "output_spec": "In the single line print a single number — the maximum possible beauty of the resulting array.", "sample_inputs": ["6 1\n3 6 10 12 13 16", "5 3\n8 21 52 15 77"], "sample_outputs": ["3", "7"], "notes": "NoteIn the first sample we can obtain the array:3 6 9 12 12 15In the second sample we can obtain the next array:7 21 49 14 77"}, "positive_code": [{"source_code": "n, k = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\nMIN, MAX = a.minmax\nif MIN <= k+1\n puts MIN\n exit\nend\n\nb = Array.new(MAX+1, 0)\na.each { |e| b[e] += 1 }\nb.each_index do |i|\n b[i] += b[i-1] if i != 0\nend\n\nans = MIN\nloop do\n l = ans + k\n r = 2*ans - 1\n flag = true\n\n while r < MAX\n if b[l] < b[r]\n break flag = false\n end\n l += ans\n r += ans\n end\n\n break if flag && l >= MAX\n ans -= 1\nend\n\nputs ans\n\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\nMIN, MAX = a.minmax\nif MIN <= k+1\n puts MIN\n exit\nend\n\nb = Array.new(MAX+1, 0)\na.each { |e| b[e] += 1 }\nb.each_index do |i|\n b[i] += b[i-1] if i != 0\nend\n\nans = MIN\nloop do\n l = ans + k\n r = 2*ans - 1\n flag = true\n\n loop do\n if r > MAX\n if l >= MAX\n break flag = true\n else\n if b[l] < b[MAX]\n break flag = false\n else\n break flag = true\n end\n end\n elsif b[l] < b[r]\n break flag = false\n end\n l += ans\n r += ans\n end\n\n break if flag\n ans -= 1\nend\nputs ans\n\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\nans = a.min\n\nloop do\n flag = true\n a.each do |e|\n while e % ans > k\n ans -= 1\n flag = false\n end\n end\n break if flag\nend\nputs ans\n\n"}, {"source_code": "gets.scan(/(\\d+) (\\d+)/)\nn=$1.to_i\nk=$2.to_i\na=gets.scan(/\\d+/).map {|s| s.to_i}.uniq.sort\nl=Array.new(a.max+1, -1)\ni=0\n0.upto(a.max) {|j|\n if i=a.first\n if j%divisor>k || j==a.first && j=a.first\n if j%divisor>k\n existAns=false\n break\n end\n j=l[j-j%divisor]\n end\n if existAns\n p divisor\n exit\n end\n}\np 1"}, {"source_code": "n, k = gets.split(' ').map {|x| x.to_i}\na = gets.split(' ').map {|x| x.to_i}.sort\nminv, maxv = a.minmax\n\ncnt = (Array.new maxv + 5).fill(0)\na.each {|x| cnt[x] += 1}\ncnt.each_index {|i| cnt[i] += cnt[i - 1] if (i > 0)}\ncnt[-1] = 0 # guard\n\n(puts minv; exit) if (minv <= k + 1)\n\nans = minv\nloop do\n\tl, r = k, ans - 1\n\tflag = 1\n\twhile (l <= maxv) do\n\t\tr = maxv if (r > maxv)\n\t\t(flag = 0; break) if (cnt[r] > cnt[l])\n\t\tl += ans\n\t\tr += ans\n\tend\n\tbreak if (flag == 1)\n\tans -= 1\nend\nputs ans\n\n"}], "negative_code": [{"source_code": "gets.scan(/(\\d+) (\\d+)/)\nn=$1.to_i\nk=$2.to_i\na=gets.scan(/\\d+/).map {|s| s.to_i}.uniq.sort\n\np (1..a.first).to_a.delete_if {|divisor| a.reject!{|x|x%divisor>k || x==a.first && x=a.first\n if j%divisor>k || j==a.first && j=n-k\n print i.to_s+\" \"\n i-=1\n end\n puts \"\""}, {"source_code": "n, k = gets.split.map(&:to_i)\nputs ((1...(n - k)).to_a + n.downto(n - k).to_a).join(' ')\n"}, {"source_code": "a=gets.chomp.split(\" \")\nn=a[0].to_i\nk=a[1].to_i\nk.times do\nprintf \"#{n} \"\nn-=1\nend\n\n1.upto(n) do |i|\nprintf \"#{i} \"\nend\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\n(1..k).each{|i| print n-i+1, ' '}\n(1..(n-k)).each{|i| print i, ' '}\n"}, {"source_code": "#!/usr/bin/env ruby\n\nn, k = gets.split.collect{ |i| i.to_i }\n\nk.downto(0) { |i| print \"#{i+1} \" }\n(k+2).upto(n) { |i| print \"#{i} \" }\n"}, {"source_code": "n,k = gets.strip.split.map(&:to_i)\n\n(1..n)\nresult = (k+1).downto(1).to_a + (k+2..n).to_a\n\nputs result.join(' ')"}, {"source_code": "n, k = gets.split(' ').map(&:to_i)\nans = (0...k).map{|x| n-x} + (1..n-k).map{|x| x}\nputs ans.map(&:to_s).join(\" \")"}, {"source_code": "# -*- encoding: utf-8 -*-\nn, k = gets.chomp.split( \" \" ).map{ |e| e.to_i }\narray = (1 .. n).to_a\n\nans = array[0 .. k].reverse() + array[k + 1 .. n]\nputs ans.join \" \"\n\n\n\n"}, {"source_code": "st = gets.split\nn=st[0].to_i\nk=st[1].to_i\n\nel=n\n(k).times do\n print(el,\" \")\n el-=1\nend\n\nel=1\n(n-k).times do\n print(el,\" \")\n el+=1\nend\n\n"}, {"source_code": "n, k = gets.split.map { |i| i.to_i }\na = (1..n).to_a\na = a[0..-(k + 2)] + a[-(k + 1)..-1].reverse\n\na.each do |i|\n print i, ' '\nend\nputs\n\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\n\na = Array.new\n\nfor i in 0...k\n a << n-i\nend\n\nfor i in 1..n-k\n a << i\nend\n\nfor i in a\n print i, \" \"\nend"}, {"source_code": "#!/usr/bin/ruby1.9\n\nn, k = STDIN.readline.split.collect {|s| s.to_i }\n\nprefix = (1..k+1).collect {|x| x }.reverse\nsuffix = (k+2..n).collect {|x| x }\n\nputs (prefix+suffix).join(' ')\n\n"}, {"source_code": "#!/usr/bin/env ruby\n\nn, k = STDIN.readline.split.map{ |x| Integer(x)}\n\nk.times do |i|\n print \"#{n - i} \"\nend\n\n(1..n-k).each do |i|\n print \"#{i} \"\nend \n\nputs\n"}, {"source_code": "def run\n n, k = $stdin.gets.split.map(&:to_i)\n $stdout.puts ((k + 1).downto(1).to_a + (k + 2).upto(n).to_a).join(\" \")\nend\n\nrun if $0 == __FILE__\n"}, {"source_code": "# cook your code here\nn, k =gets.split(' ').map(&:to_i)\n\narr = Array(1..n)\narr.unshift(arr.pop(k).reverse)\nputs arr.join(' ')"}, {"source_code": "# cook your code here\nn, k =gets.split(' ').map(&:to_i)\n\nk_copy =k \narr = Array(1..n)\nlast_index = arr.size-1 \nans_arr = []\nn.times do \n while k>0 do \n ans_arr << arr[last_index]\n last_index-=1 \n k-=1 \n end \nend \n\n(n-k_copy).times do |i| \n ans_arr << arr[i] \nend \n\nputs ans_arr.join(\" \")"}, {"source_code": "class A\n def initialize\n n, k = $stdin.gets.split(' ').map(&:to_i)\n\n list = (1..n).to_a\n\n flag = true\n\n ans = Array.new(n,nil)\n\n if k > 0\n index = -1\n k.times do\n ans[index] = list.shift\n index -= 1\n end\n\n index = 0\n (n-k).times do\n ans[index] = list.shift\n index += 1\n end\n else\n ans = list\n end\n\n puts ans.join(' ')\n end\nend\n\na = A.new"}, {"source_code": "a=gets.chomp.split(\" \")\nn=a[0].to_i\nk=a[1].to_i\nk.times do\nprintf \"#{n} \"\nn-=1\nend\n\n1.upto(n) do |i|\nprintf \"#{i} \"\nend\n"}, {"source_code": "a=gets.chomp.split(\" \")\nn=a[0].to_i\nk=a[1].to_i\nk.times do\nprintf \"#{n} \"\nn-=1\nend\n\n1.upto(n) do |i|\nprintf \"#{i} \"\nend"}, {"source_code": "require 'set'\nrequire 'rational'\nrequire 'mathn'\ninclude Math\n$stdin = File.open(\"./input.txt\", \"r\") if ENV[\"USER\"] == \"new\"\n# ====================================\n\nn, k = gets.split.map(&:to_i)\n\n((k+1)..n).each do |index|\n print index,\" \"\nend\nk.downto(1) do |index|\n print index,\" \"\nend"}], "negative_code": [{"source_code": "# cook your code here\nn, k =gets.split(' ').map(&:to_i)\n\narr = Array(1..n)\narr.unshift(arr.pop(k))\nputs arr.join(' ')"}, {"source_code": "class A\n def initialize\n n, k = $stdin.gets.split(' ').map(&:to_i)\n\n list = (1..n).to_a\n\n flag = true\n\n ans = Array.new(n,nil)\n\n if k > 0\n index = -1\n k.times do\n ans[index] = list.shift\n index -= 1\n end\n\n index = 0\n (n-k).times do\n ans[index] = list.shift\n end\n else\n ans = list\n end\n\n puts ans.join(' ')\n end\nend\n\na = A.new"}, {"source_code": "a=gets.chomp.split(\" \")\nc=a[0].to_i\nb=a[1].to_i\ntrig=0\nans=[]\nloop do\nif ans.count(c%b)==0\nans << c%b\nelse\nbreak\nend\n\nc=c+c%b\nif c % b == 0\ntrig=1\nbreak\nend\n\nend\n\nif trig==1\nputs \"Yes\"\nelse \nputs \"No\"\nend\n\n\n"}, {"source_code": "p=gets.chomp.to_i\ndef MOD (n,p)\n\tif p>19\n\tpmin=(Math.log(1000000007,n)).ceil\n\tpc=p/pmin\n\tpl=p%pmin\n\treturn ((MOD((n**pmin % 1000000007),pc) * (n**pl) ) % 1000000007)\n\telse\n\treturn (n**p)%1000000007\n\tend\n\tend\n\t\n\tc=(MOD(3,3*p)- MOD(7,p))% 1000000007\n\tputs \"#{c}\""}], "src_uid": "75cc5b55c51217966cbdd639a68f0724"} {"nl": {"description": "This is an interactive problem.Homer likes arrays a lot and he wants to play a game with you. Homer has hidden from you a permutation $$$a_1, a_2, \\dots, a_n$$$ of integers $$$1$$$ to $$$n$$$. You are asked to find any index $$$k$$$ ($$$1 \\leq k \\leq n$$$) which is a local minimum. For an array $$$a_1, a_2, \\dots, a_n$$$, an index $$$i$$$ ($$$1 \\leq i \\leq n$$$) is said to be a local minimum if $$$a_i < \\min\\{a_{i-1},a_{i+1}\\}$$$, where $$$a_0 = a_{n+1} = +\\infty$$$. An array is said to be a permutation of integers $$$1$$$ to $$$n$$$, if it contains all integers from $$$1$$$ to $$$n$$$ exactly once.Initially, you are only given the value of $$$n$$$ without any other information about this permutation.At each interactive step, you are allowed to choose any $$$i$$$ ($$$1 \\leq i \\leq n$$$) and make a query with it. As a response, you will be given the value of $$$a_i$$$. You are asked to find any index $$$k$$$ which is a local minimum after at most $$$100$$$ queries.", "input_spec": null, "output_spec": null, "sample_inputs": ["5\n3\n2\n1\n4\n5"], "sample_outputs": ["? 1\n? 2\n? 3\n? 4\n? 5\n! 3"], "notes": "NoteIn the example, the first line contains an integer $$$5$$$ indicating that the length of the array is $$$n = 5$$$.The example makes five \"?\" queries, after which we conclude that the array is $$$a = [3,2,1,4,5]$$$ and $$$k = 3$$$ is local minimum."}, "positive_code": [{"source_code": "$stdout.sync = true\r\n\r\ndef query(x)\r\n puts \"? #{x}\"\r\n gets.chomp.to_i\r\nend\r\n\r\ndef solution(n)\r\n value = yield(1)\r\n l, r = 1, value + 1\r\n while r - l > 1 do\r\n mid = (l + r) / 2\r\n a, b = yield(mid - 1), yield(mid)\r\n delta = mid - l\r\n if a < b\r\n r = mid\r\n elsif b <= value - delta \r\n l, value = mid, b\r\n else\r\n r = mid\r\n end\r\n end\r\n return l\r\nend\r\n\r\ndef stress\r\n array = (20).times.map { |x| x + 1 }.shuffle\r\n index = solution(array.length) { |x| array[x - 1] } - 1\r\n puts(array.inspect)\r\n puts(index)\r\n if not ((index == 0 or array[index - 1] > array[index]) and (index == array.length - 1 or array[index + 1] > array[index]))\r\n puts(\"Fail!\")\r\n end\r\nend\r\n\r\ndef main\r\n n = gets.chomp.to_i\r\n puts(\"! #{solution(n) { |x| query(x) }}\")\r\nend\r\n\r\nmain\r\n"}, {"source_code": "n=gets.to_i\r\narr=[nil]*(n+2)\r\nl=0\r\nr=n+1\r\nwhile r-l>3\r\n m0=l+(r-l)/3\r\n m1=r-(r-l)/3\r\n if !arr[m0]\r\n puts \"? #{m0}\"\r\n $stdout.flush\r\n v0=gets.to_i\r\n arr[m0]=v0\r\n else\r\n v0=arr[m0]\r\n end\r\n if !arr[m1]\r\n puts \"? #{m1}\"\r\n $stdout.flush\r\n v1=gets.to_i\r\n arr[m1]=v1\r\n else\r\n v1=arr[m1]\r\n end\r\n if v0>v1\r\n l=m0\r\n else\r\n r=m1\r\n end\r\nend\r\nm0=l+1\r\nm1=r-1\r\nif !arr[m0]\r\n puts \"? #{m0}\"\r\n $stdout.flush\r\n v0=gets.to_i\r\n arr[m0]=v0\r\nelse\r\n v0=arr[m0]\r\nend\r\nif !arr[m1]\r\n puts \"? #{m1}\"\r\n $stdout.flush\r\n v1=gets.to_i\r\n arr[m1]=v1\r\nelse\r\n v1=arr[m1]\r\nend\r\nif v0>v1\r\n puts \"! #{m1}\"\r\nelse\r\n puts \"! #{m0}\"\r\nend\r\n"}], "negative_code": [{"source_code": "$stdout.sync = true\r\n\r\ndef query(x)\r\n puts \"? #{x}\"\r\n gets.chomp.to_i\r\nend\r\n\r\nn = gets.chomp.to_i\r\nvalue = query(1)\r\nl, r = 1, value + 1\r\nwhile r - l > 1 do\r\n mid = (l + r) / 2\r\n mid_value = query(mid)\r\n delta = mid - l\r\n if mid_value <= value - delta \r\n l, value = mid, mid_value\r\n else\r\n r = mid\r\n end\r\nend\r\nputs \"! #{l}\"\r\n"}, {"source_code": "$stdout.sync = true\r\n\r\ndef query(x)\r\n puts \"? #{x}\"\r\n gets.chomp.to_i\r\nend\r\n\r\nn = gets.chomp.to_i\r\nvalue = query(1)\r\nl, r = 1, value + 1\r\nwhile r - l > 1 do\r\n mid = (l + r) / 2\r\n mid_value = query(mid)\r\n delta = mid - l\r\n if mid_value <= value - delta \r\n l = mid\r\n value = mid_value\r\n else\r\n r = mid\r\n end\r\nend\r\nputs \"! #{l}\"\r\n"}, {"source_code": "n=gets.to_i\r\nl=1\r\nr=n\r\nll=l\r\nrr=r\r\nputs \"? #{l}\"\r\n$stdout.flush\r\nlm=gets.to_i\r\nputs \"? #{r}\"\r\n$stdout.flush\r\nrm=gets.to_i\r\nwhile true\r\n m=(l+r)/2\r\n f=0\r\n if lm<=rm\r\n r=m\r\n f=0\r\n else\r\n l=m\r\n f=1\r\n end\r\n while r-l>1\r\n puts \"? #{m}\"\r\n $stdout.flush\r\n m=(l+r)/2\r\n if f==0\r\n rm=gets.to_i\r\n else\r\n lm=gets.to_i\r\n end\r\n if lm<=rm\r\n rr=r\r\n r=m\r\n f=0\r\n else\r\n ll=l\r\n l=m\r\n f=1\r\n end\r\n end\r\n puts \"? #{m}\"\r\n $stdout.flush\r\n mm=gets.to_i\r\n if lm>mm&&mm 0\n\t\t[[p1, t1], [p2, t2], [p3, 1e9]].each do |pi, ti|\n\t\t\td = [ti, l - t].min\n\t\t\ts += d * pi\n\t\t\tt += d\n\t\tend\n\tend\n\ts += (r - l) * p1\n\tt = r\nend\np s\n"}, {"source_code": "input=gets.split.map(&:to_i)\nn=input[0]\np1=input[1]\np2=input[2]\np3=input[3]\nt1=input[4]\nt2=input[5]\n\nback=-1\nans=0\nn.times{|i|\n input=gets.split.map(&:to_i)\n if back==-1 then\n back=input[0]\n end\n ans+=(input[1]-input[0])*p1\n t=input[0]-back\n t1.times{\n if t==0 then\n break\n end\n t-=1\n ans+=p1\n }\n t2.times{\n if t==0 then\n break\n end\n t-=1\n ans+=p2\n }\n ans+=t*p3\n back=input[1]\n}\nputs ans"}, {"source_code": "n,a,b,c,d,e=gets.split.map &:to_i;c-=b;b-=a;e+=d;t,x=1e4,0;n.times{l,r=gets.split.map &:to_i;x,t,l=x+a*(r-l),r,l-t;x+=a*[l,0].max+b*[l-d,0].max+c*[l-e,0].max};puts x\n"}, {"source_code": "n,a,b,c,d,e=gets.split.map &:to_i;c-=b;b-=a;e+=d;t,x=1e4,0;n.times{l,r=gets.split.map &:to_i;x,t,l=x+a*(r-l),r,l-t;x+=a*[l,0].max+b*[l-d,0].max+c*[l-e,0].max};puts x\n"}, {"source_code": "n,a,b,c,d,e=gets.split.map &:to_i;c-=b;b-=a;e+=d;t,x=1e4,0;n.times{l,r=gets.split.map &:to_i;x,t,l=x+a*(r-l),r,l-t;x+=a*[l,0].max+b*[l-d,0].max+c*[l-e,0].max};puts x"}, {"source_code": "n,a,b,c,d,e=gets.split.map{|i|i.to_i};c-=b;b-=a;e+=d;t,x=1e4,0;n.times{l,r=gets.split.map{|i|i.to_i};x,t,l=x+a*(r-l),r,l-t;x+=a*[l,0].max+b*[l-d,0].max+c*[l-e,0].max};puts x"}, {"source_code": "n,a,b,c,d,e=gets.split.map &:to_i;c-=b;b-=a;e+=d;t,x=1e4,0;n.times{l,r=gets.split.map &:to_i;x,t,l=x+a*(r-l),r,l-t;x+=a*[l,0].max+b*[l-d,0].max+c*[l-e,0].max};puts x"}, {"source_code": "n,a,b,c,d,e=gets.split.map &:to_i;c-=b;b-=a;e+=d;t,x=1e4,0;n.times{l,r=gets.split.map &:to_i;x,t,l=x+a*(r-l),r,l-t;x+=a*[l,0].max+b*[l-d,0].max+c*[l-e,0].max};puts x"}, {"source_code": "n,a,b,c,d,e=gets.split.map &:to_i;c-=b;b-=a;e+=d;t,x=1e4,0;n.times{l,r=gets.split.map &:to_i;x,t,l=x+a*(r-l),r,l-t;x+=a*[l,0].max+b*[l-d,0].max+c*[l-e,0].max};puts x\n"}, {"source_code": "n,a,b,c,d,e=gets.split.map &:to_i;c-=b;b-=a;e+=d;t,x=1e4,0;n.times{l,r=gets.split.map &:to_i;x,t,l=x+a*(r-l),r,l-t;x+=a*[l,0].max+b*[l-d,0].max+c*[l-e,0].max};puts x\n"}, {"source_code": "n,a,b,c,d,e=gets.split.map &:to_i;c-=b;b-=a;e+=d;t,x=1e4,0;n.times{l,r=gets.split.map &:to_i;x,t,l=x+a*(r-l),r,l-t;x+=a*[l,0].max+b*[l-d,0].max+c*[l-e,0].max};puts x"}, {"source_code": "fst=gets.split\nn=fst[0].to_i\np1=fst[1].to_i\np2=fst[2].to_i\np3=fst[3].to_i\nt1=fst[4].to_i\nt2=fst[5].to_i\nline=gets.split\nl=[line[0].to_i]\nr=[line[1].to_i]\nd=r[0]-l[0]\nres=d*p1\n(n-1).times do |i|\n line=gets.split\n l[i+1]=line[0].to_i\n r[i+1]=line[1].to_i\n d0=r[i+1]-l[i+1]\n res += d0*p1\n d1=l[i+1]-r[i]\n if d1<=t1\n res += d1*p1\n else\n res += t1*p1\n d2=d1-t1\n if d2<=t2\n res += d2*p2\n else\n res += t2*p2\n res += (d2-t2)*p3\n end\n end\nend\nputs res\n"}, {"source_code": "s,e,c=[],[],0\nn,p0,p1,p2,t0,t1=gets.chomp!.split(\" \").map{|elem|elem.to_i}\nn.times {|i|\n s[i%2],e[i%2]=gets.chomp!.split(\" \").map{|elem|elem.to_i}\n c+=(e[i%2]-s[i%2])*p0\n if i>0 then w=s[i%2]-e[(i+1)%2]\n if w<=t0 then c+=w*p0 elsif w<=t0+t1 then c+=p0*t0+p1*(w-t0) else c+=p0*t0+p1*t1+p2*(w-t1-t0) end end\n}\nputs c"}, {"source_code": "n, p1, p2, p3, t1, t2 = gets.chomp.split.map(&:to_i)\nl, r = [], []\npower = 0\nn.times do |index|\n\tl[index % 2], r[index % 2] = gets.chomp.split.map(&:to_i)\n\tpower += (r[index % 2] - l[index % 2]) * p1\n\tif index > 0\n\t\tno_activity = l[index % 2] - r[(index + 1) % 2]\n\t\tif no_activity <= t1\n\t\t\tpower += no_activity * p1\n\t\telsif no_activity <= t1 + t2\n\t\t\tpower += t1 * p1 + (no_activity - t1) * p2\n\t\telse\n\t\t\tpower += t1 * p1 + t2 * p2 + (no_activity - t1 - t2) * p3\n\t\tend\n\tend\nend\n\nputs power"}, {"source_code": "n,a,b,c,d,e=gets.split.map &:to_i;c-=b;b-=a;e+=d;t,x=1e4,0;n.times{l,r=gets.split.map &:to_i;x,t,l=x+a*(r-l),r,l-t;x+=a*[l,0].max+b*[l-d,0].max+c*[l-e,0].max};puts x\n"}, {"source_code": "n,a,b,c,d,e=gets.split.map &:to_i;c-=b;b-=a;e+=d;t,x=1e4,0;n.times{l,r=gets.split.map &:to_i;x,t,l=x+a*(r-l),r,l-t;x+=a*[l,0].max+b*[l-d,0].max+c*[l-e,0].max};puts x"}, {"source_code": "n,a,b,c,d,e=gets.split.map &:to_i;c-=b;b-=a;e+=d;t,x=1e4,0;n.times{l,r=gets.split.map &:to_i;x,t,l=x+a*(r-l),r,l-t;x+=a*[l,0].max+b*[l-d,0].max+c*[l-e,0].max};puts x\n"}, {"source_code": "n,a,b,c,d,e=gets.split.map &:to_i;c-=b;b-=a;e+=d;t,x=1e4,0;n.times{l,r=gets.split.map &:to_i;x,t,l=x+a*(r-l),r,l-t;x+=a*[l,0].max+b*[l-d,0].max+c*[l-e,0].max};puts x\n"}, {"source_code": "n,a,b,c,d,e=gets.split.map &:to_i;c-=b;b-=a;e+=d;t,x=1e4,0;n.times{l,r=gets.split.map &:to_i;x,t,l=x+a*(r-l),r,l-t;x+=a*[l,0].max+b*[l-d,0].max+c*[l-e,0].max};puts x\n"}], "negative_code": [{"source_code": "s,e,c=[],[],0\nn,p0,p1,p2,t0,t1=gets.chomp!.split(\" \").map{|elem|elem.to_i}\nn.times {|i|s[i%2],e[i%2]=gets.chomp!.split(\" \").map{|elem|elem.to_i}\n c+=(e[i%2]-s[i%2])*p0\n if i>0 then w=s[i%2]-e[(i+1)%2]\n if w<=t0 then c+=w*p0 elsif w<=t1 then c+=p0*t0+p1*(w-t0) else c+=p0*t0+p1*t1+p2*(w-t1-t0) end end}\nputs c"}], "src_uid": "7ed9265b56ef6244f95a7a663f7860dd"} {"nl": {"description": "There are $$$n$$$ bricks numbered from $$$1$$$ to $$$n$$$. Brick $$$i$$$ has a weight of $$$a_i$$$.Pak Chanek has $$$3$$$ bags numbered from $$$1$$$ to $$$3$$$ that are initially empty. For each brick, Pak Chanek must put it into one of the bags. After this, each bag must contain at least one brick.After Pak Chanek distributes the bricks, Bu Dengklek will take exactly one brick from each bag. Let $$$w_j$$$ be the weight of the brick Bu Dengklek takes from bag $$$j$$$. The score is calculated as $$$|w_1 - w_2| + |w_2 - w_3|$$$, where $$$|x|$$$ denotes the absolute value of $$$x$$$.It is known that Bu Dengklek will take the bricks in such a way that minimises the score. What is the maximum possible final score if Pak Chanek distributes the bricks optimally?", "input_spec": "Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \\leq t \\leq 2 \\cdot 10^4$$$) — the number of test cases. The following lines contain the description of each test case. The first line of each test case contains an integer $$$n$$$ ($$$3 \\leq n \\leq 2 \\cdot 10^5$$$) — the number of bricks. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\leq a_i \\leq 10^9$$$) — the weights of the bricks. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, output a line containing an integer representing the maximum possible final score if Pak Chanek distributes the bricks optimally.", "sample_inputs": ["3\n\n5\n\n3 1 5 2 3\n\n4\n\n17 8 19 45\n\n8\n\n265 265 265 265 265 265 265 265"], "sample_outputs": ["6\n63\n0"], "notes": "NoteIn the first test case, one way of achieving a final score of $$$6$$$ is to do the following: Put bricks $$$1$$$, $$$4$$$, and $$$5$$$ into bag $$$1$$$. Put brick $$$3$$$ into bag $$$2$$$. Put brick $$$2$$$ into bag $$$3$$$. If Pak Chanek distributes the bricks that way, a way Bu Dengklek can take the bricks is: Take brick $$$5$$$ from bag $$$1$$$. Take brick $$$3$$$ from bag $$$2$$$. Take brick $$$2$$$ from bag $$$3$$$. The score is $$$|a_5 - a_3| + |a_3 - a_2| = |3 - 5| + |5 - 1| = 6$$$. It can be shown that Bu Dengklek cannot get a smaller score from this distribution.It can be shown that there is no other distribution that results in a final score bigger than $$$6$$$."}, "positive_code": [{"source_code": "t=gets.to_i\nt.times do\n n=gets.to_i\n arr=gets.chomp.split(' ').map(&:to_i)\n arr.sort!\n r=0\n (2..(n-2)).each do|x|\n r=[r, arr[x]-arr[0]+arr[x]-arr[x-1], arr[x]-arr[x-1]+arr[-1]-arr[x-1]].max\n end\n r=[r, arr[1]-arr[0]+arr[-1]-arr[0],arr[-1]-arr[-2]+arr[-1]-arr[0]].max\n puts r\nend"}], "negative_code": [{"source_code": "t=gets.to_i\nt.times do\n n=gets.to_i\n arr=gets.chomp.split(' ').map(&:to_i)\n arr.sort!\n puts arr[-1]-arr[0]+[arr[1]-arr[0],arr[-1]-arr[-2]].max\nend"}], "src_uid": "7421b2392cb40f1cf0b7fd93c287f1eb"} {"nl": {"description": "Turbulent times are coming, so you decided to buy sugar in advance. There are $$$n$$$ shops around that sell sugar: the $$$i$$$-th shop sells one pack of sugar for $$$a_i$$$ coins, but only one pack to one customer each day. So in order to buy several packs, you need to visit several shops.Another problem is that prices are increasing each day: during the first day the cost is $$$a_i$$$, during the second day cost is $$$a_i + 1$$$, during the third day — $$$a_i + 2$$$ and so on for each shop $$$i$$$.On the contrary, your everyday budget is only $$$x$$$ coins. In other words, each day you go and buy as many packs as possible with total cost not exceeding $$$x$$$. Note that if you don't spend some amount of coins during a day, you can't use these coins during the next days.Eventually, the cost for each pack will exceed $$$x$$$, and you won't be able to buy even a single pack. So, how many packs will you be able to buy till that moment in total?", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$; $$$1 \\le x \\le 10^9$$$) — the number of shops and your everyday budget. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$) — the initial cost of one pack in each shop. It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, print one integer — the total number of packs you will be able to buy until prices exceed your everyday budget.", "sample_inputs": ["4\n\n3 7\n\n2 1 2\n\n5 9\n\n10 20 30 40 50\n\n1 1\n\n1\n\n2 1000\n\n1 1"], "sample_outputs": ["11\n0\n1\n1500"], "notes": "NoteIn the first test case, Day 1: prices are $$$[2, 1, 2]$$$. You can buy all $$$3$$$ packs, since $$$2 + 1 + 2 \\le 7$$$. Day 2: prices are $$$[3, 2, 3]$$$. You can't buy all $$$3$$$ packs, since $$$3 + 2 + 3 > 7$$$, so you buy only $$$2$$$ packs. Day 3: prices are $$$[4, 3, 4]$$$. You can buy $$$2$$$ packs with prices $$$4$$$ and $$$3$$$. Day 4: prices are $$$[5, 4, 5]$$$. You can't buy $$$2$$$ packs anymore, so you buy only $$$1$$$ pack. Day 5: prices are $$$[6, 5, 6]$$$. You can buy $$$1$$$ pack. Day 6: prices are $$$[7, 6, 7]$$$. You can buy $$$1$$$ pack. Day 7: prices are $$$[8, 7, 8]$$$. You still can buy $$$1$$$ pack of cost $$$7$$$. Day 8: prices are $$$[9, 8, 9]$$$. Prices are too high, so you can't buy anything. In total, you bought $$$3 + 2 + 2 + 1 + 1 + 1 + 1 = 11$$$ packs.In the second test case, prices are too high even at the first day, so you can't buy anything.In the third test case, you can buy only one pack at day one.In the fourth test case, you can buy $$$2$$$ packs first $$$500$$$ days. At day $$$501$$$ prices are $$$[501, 501]$$$, so you can buy only $$$1$$$ pack the next $$$500$$$ days. At day $$$1001$$$ prices are $$$[1001, 1001]$$$ so can't buy anymore. In total, you bought $$$500 \\cdot 2 + 500 \\cdot 1 = 1500$$$ packs."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n n, x = gets.split.map(&:to_i)\r\n as = gets.split.map(&:to_i).sort\r\n sums = as.reduce([]) { |acc, x| acc << (acc.last || 0) + x }\r\n answer = sums.map.with_index do |sum, i|\r\n [0, (1 + (x - sum) / (i + 1))].max\r\n end.sum\r\n p answer\r\nend\r\n"}, {"source_code": "gets.to_i.times do \n n, x= gets.chomp.split.map(&:to_i)\n a = gets.chomp.split.map(&:to_i).sort\n cum = 0\n ans = 0\n 0.upto(n-1) do |i|\n cum += a[i]\n ans += (x >= cum)?((x-cum)/(i+1)+1) : 0\n end\n puts ans\nend\n"}], "negative_code": [], "src_uid": "b65767c1ebfe72e08f58a9f9254eaa7b"} {"nl": {"description": "While walking down the street Vanya saw a label \"Hide&Seek\". Because he is a programmer, he used & as a bitwise AND for these two words represented as a integers in base 64 and got new word. Now Vanya thinks of some string s and wants to know the number of pairs of words of length |s| (length of s), such that their bitwise AND is equal to s. As this number can be large, output it modulo 109 + 7.To represent the string as a number in numeral system with base 64 Vanya uses the following rules: digits from '0' to '9' correspond to integers from 0 to 9; letters from 'A' to 'Z' correspond to integers from 10 to 35; letters from 'a' to 'z' correspond to integers from 36 to 61; letter '-' correspond to integer 62; letter '_' correspond to integer 63. ", "input_spec": "The only line of the input contains a single word s (1 ≤ |s| ≤ 100 000), consisting of digits, lowercase and uppercase English letters, characters '-' and '_'.", "output_spec": "Print a single integer — the number of possible pairs of words, such that their bitwise AND is equal to string s modulo 109 + 7.", "sample_inputs": ["z", "V_V", "Codeforces"], "sample_outputs": ["3", "9", "130653412"], "notes": "NoteFor a detailed definition of bitwise AND we recommend to take a look in the corresponding article in Wikipedia.In the first sample, there are 3 possible solutions: z&_ = 61&63 = 61 = z _&z = 63&61 = 61 = z z&z = 61&61 = 61 = z "}, "positive_code": [{"source_code": " def gao(ch)\n case ch\n when '0'..'9'\n return ch.ord - '0'.ord\n when 'A'..'Z'\n return ch.ord - 'A'.ord + 10\n when 'a'..'z'\n return ch.ord - 'a'.ord + 36\n when '-'\n return 62\n when '_'\n return 63\n end\nend\n\ndef cal(num)\n# puts num\n ret = 0\n 6.times do\n ret += 1 if (num % 2) == 0\n num >>= 1\n end\n# puts ret\n ret\nend\n\nstr = readline.strip\nsum = str.each_char.map{|num| gao(num)}.map{|num| cal(num)}.inject(0, :+)\nmod = 1000000007\nres = 1\nsum.times {res = (res * 3) % mod}\nputs res\n"}, {"source_code": "s = gets.chomp\nans = 1\npatterns = (\"0\"..\"9\").to_a + (\"A\"..\"Z\").to_a + (\"a\"..\"z\").to_a + [\"-\",\"_\"]\nsymbols = {} \npatterns.each_with_index do |s,i|\n symbols[s] = i\nend\n\nfor c in s.split(\"\") do\n val = symbols[c] \n 6.times do |i|\n if val&1 == 0 then \n ans = (ans*3)%1000000007\n end\n val >>= 1\n end\nend\n\nprint ans\n"}, {"source_code": "s = gets.chomp\n\nmod = 10**9 + 7\nstr = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_'\nans = 1\n\ns.split('').each do |c|\n (str.index(c) + 64).to_s(2).split('').each do |d|\n ans = ans * 3 % mod if d == '0'\n end\nend\n\nputs ans\n"}, {"source_code": "h = Hash[( ('0'..'9').to_a + ('A'..'Z').to_a + ('a'..'z').to_a + ['-', '_'] ).map.with_index.to_a ]\ns = gets.chomp\ns_2 = s.chars.map{|x| h[x].to_s(2).ljust(6, '0')}.join\nn = s_2.count('0')\nputs (3 ** n) % (10**9 + 7)\n"}], "negative_code": [{"source_code": "h = Hash[( ('0'..'9').to_a + ('A'..'Z').to_a + ('a'..'z').to_a + ['-', '_'] ).map.with_index.to_a ]\ns = gets.chomp\ns_2 = s.chars.inject(''){|s, x| s + h[x].to_s(2).ljust(6, '0')}\nn = s_2.count('0')\nputs ( (3 ** n) % (1e9 + 7) ).to_i\n"}], "src_uid": "1b336555c94d5d5198abe5426ff6fa7a"} {"nl": {"description": "An exam for n students will take place in a long and narrow room, so the students will sit in a line in some order. The teacher suspects that students with adjacent numbers (i and i + 1) always studied side by side and became friends and if they take an exam sitting next to each other, they will help each other for sure.Your task is to choose the maximum number of students and make such an arrangement of students in the room that no two students with adjacent numbers sit side by side.", "input_spec": "A single line contains integer n (1 ≤ n ≤ 5000) — the number of students at an exam.", "output_spec": "In the first line print integer k — the maximum number of students who can be seated so that no two students with adjacent numbers sit next to each other. In the second line print k distinct integers a1, a2, ..., ak (1 ≤ ai ≤ n), where ai is the number of the student on the i-th position. The students on adjacent positions mustn't have adjacent numbers. Formally, the following should be true: |ai - ai + 1| ≠ 1 for all i from 1 to k - 1. If there are several possible answers, output any of them.", "sample_inputs": ["6", "3"], "sample_outputs": ["6\n1 5 3 6 2 4", "2\n1 3"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/env ruby\nn=gets.to_i\nif n<3\n\tputs \"1\\n1\\n\"\nelsif n<4\n\tputs \"2\\n1 3\\n\"\nelse\n\tp n\n\tputs (2.step(n,2).to_a+1.step(n,2).to_a)*' '\nend"}, {"source_code": "# http://codeforces.com/contest/534/problem/A\nn = gets.to_i\narr = case n\n when 1, 2\n [1]\n when 3\n [1, 3]\n else\n 2.step(n, 2).to_a + 1.step(n, 2).to_a\n end\n\nputs arr.length\nputs arr.join(' ')\n"}, {"source_code": "a=gets.chomp.to_i\nif a==1\nputs \"1\"\nputs \"1\"\nelsif a==2\nputs \"1\"\nputs \"1\"\nelsif a==3\nputs \"2\"\nputs \"1 3\"\nelse \nputs \"#{a}\"\nif a%2==1\nprintf \"#{a/2 + 1} \"\n0.upto(a/2 - 1) do |i|\nprintf \"#{i+1} #{a-i} \"\nend\nelse\nprintf \"#{a/2 + 1} \"\n0.upto(a/2 - 2) do |i|\nprintf \"#{i+1} #{a-i} \"\nend\nprintf \"#{a/2}\"\nend\nend\n"}, {"source_code": "a=gets.chomp.to_i\nif a==1\nputs \"1\"\nputs \"1\"\nelsif a==2\nputs \"1\"\nputs \"1\"\nelsif a==3\nputs \"2\"\nputs \"1 3\"\nelse \nputs \"#{a}\"\nif a%2==1\nprintf \"#{a/2 + 1} \"\n0.upto(a/2 - 1) do |i|\nprintf \"#{i+1} #{a-i} \"\nend\nelse\nprintf \"#{a/2 + 1} \"\n0.upto(a/2 - 2) do |i|\nprintf \"#{i+1} #{a-i} \"\nend\nprintf \"#{a/2}\"\nend\nend"}, {"source_code": "n=gets.to_i\narr=[]\nif n<5 then\n\tarr=[[],[1],[1],[1,3],[2,4,1,3]][n]\n\tputs arr.size\n\tputs arr*\" \"\n\t\nelse\n\t1.step(n,2){|e|\n\t\t\tarr<n\n\t\treturn arr\n\tend\n\n\tch = 0\n\tif !arr.include?(i)\n\t\tif arr == []\n\t\t\tch = 1\n\t\t\tp = sol(arr+[i],1,n)\n\t\telsif (i>=arr[-1]+2 || i<=arr[-1]-2)\n\t\t\tch = 1\n\t\t\tp = sol(arr+[i],1,n)\n\t\tend\n\tend\n\tnp = sol(arr,i+1,n)\n\n\tif ch == 1\n\t\treturn p.length > np.length ? p : np\n\tend\n\treturn np\nend\n\n\nn = gets.chomp.to_i\nf = (1..n/2).to_a.reverse\nbf = ((n/2)+1..n).to_a.reverse\norder = f.length>bf.length ? 1 : 2\n\nif n < 6\n\ta = sol([],1,n)\n\tputs \"#{a.length}\"\n\tputs a.join(\" \")\nelse\n\t\n\tans = []\n\twhile true\n\t\tif f == [] && bf == []\n\t\t\tbreak\n\t\tend\n\t\tif order == 1\n\t\t\tif f!=[]\n\t\t\t\tans << f.pop\n\t\t\tend\n\t\t\tif bf!=[]\n\t\t\t\tans << bf.pop\n\t\t\tend\n\t\telse\n\t\t\tif bf!=[]\n\t\t\t\tans << bf.pop\n\t\t\tend\n\t\t\tif f!=[]\n\t\t\t\tans << f.pop\n\t\t\tend\n\t\tend\n\tend\n\n\tputs \"#{ans.length}\"\n\t\n\tputs ans.join(\" \")\nend"}, {"source_code": "n = gets.strip.to_i\nif n <= 2\n puts \"1\"\n puts \"1\"\n exit\nend\n\nif n == 3\n puts n - 1\n puts (1..n).to_a.select { |e| e.odd? }.sort.reverse.join(' ')\n exit\nend\n\nodd = (1..n).to_a.select { |e| e.odd? }.sort.reverse\neven = (1..n).to_a.select { |e| e.even? }.sort.reverse\n\nputs n\nprint odd.join(' ')\nprint \" \"\nprint even.join(' ')\n\n"}, {"source_code": "a= gets().chomp().to_i\nif a==1||a==2\n print 1,\"\\n\",1\nelsif a==3\n print 2,\"\\n\",1,' ',3\nelse\n print a,\"\\n\" ;(1..a).each{|num| print num,' ' if num%2==0} ;(1..a).each{|num| print num,' ' if num%2==1}\nend\n"}, {"source_code": "n = gets.to_i\n\nif n <= 2\n puts \"1\\n1\\n\"\nelsif n == 3\n puts \"2\\n1 3\\n\"\nelse\n e = 1.upto(n).select { |x| x % 2 == 0 }\n o = 1.upto(n).select { |x| x % 2 == 1 }\n puts n\n puts e.join(\" \") + \" \" + o.join(\" \")\nend\n"}, {"source_code": "n = gets.to_i\nif n==1 or n==2\n print \"1\\n1\\n\"\n exit\nelsif n==3\n print \"2\\n1 3\\n\"\n exit\nelsif n==4\n print \"4\\n3 1 4 2\\n\"\n exit\nend\nleft = Array.new\nright = Array.new\nfor i in (1...n).step(2)\n left.push(i)\n right.push(i+1)\nend\nif n.odd?\n left.push(n)\nend\nputs n\nputs (left+right).join(' ')\n"}, {"source_code": "n = gets.to_i\nif n==1 or n==2\n print \"1\\n1\\n\"\n exit\nelsif n==3\n print \"2\\n1 3\\n\"\n exit\nelsif n==4\n print \"4\\n3 1 4 2\\n\"\n exit\nend\nputs n\nputs ((1..n).step(2).to_a+(2..n).step(2).to_a).join(' ')\n"}, {"source_code": "n = gets.to_i\nans = case n\nwhen 1, 2\n [1]\nwhen 3\n [1, 3]\nwhen 4\n [3, 1, 4, 2]\nelse\n a = []\n b = []\n 1.upto(n) do |i|\n (i.odd? ? a : b) << i\n end\n a + b\nend\nputs ans.size\nputs ans.join(\" \")\n"}, {"source_code": "n = gets.to_i\na = case n\nwhen 1, 2\n [1]\nwhen 3\n [1, 3]\nwhen 4\n [3, 1, 4, 2]\nelse\n a = []\n b = []\n n.times do |i|\n i += 1\n (i % 2 == 0 ? b : a) << i\n end\n a + b\nend\nputs a.size\nputs a.join(\" \")\n"}, {"source_code": "n = gets.to_i\nans = case n\nwhen 1, 2\n [1]\nwhen 3\n [1, 3]\nwhen 4\n [3, 1, 4, 2]\nelse\n (1..n).partition{|x| x.odd?}.flatten\nend\nputs ans.size, ans.join(\" \")\n"}, {"source_code": "n = gets.to_i\na = []\n((n + 1) / 2).times.each{|i| a << i * 2 + 1}\n((n + 1) / 2).times do |i|\n x = (i + 1) * 2\n a << x if x <= n && (x - a.last).abs > 1\nend\n\na = [3, 1, 4, 2] if n == 4\nputs a.size\nputs a.join(\" \")\n"}, {"source_code": "a=gets.chomp.to_i\nif a==1\nputs \"1\"\nputs \"1\"\nelsif a==2\nputs \"1\"\nputs \"1\"\nelsif a==3\nputs \"2\"\nputs \"1 3\"\nelse \nputs \"#{a}\"\nif a%2==1\nprintf \"#{a/2 + 1} \"\n0.upto(a/2 - 1) do |i|\nprintf \"#{i+1} #{a-i} \"\nend\nelse\nprintf \"#{a/2 + 1} \"\n0.upto(a/2 - 2) do |i|\nprintf \"#{i+1} #{a-i} \"\nend\nprintf \"#{a/2}\"\nend\nend"}], "negative_code": [{"source_code": "#!/usr/bin/env ruby\nn=gets.to_i\nif n<3\n\tputs \"1\\n1\\n\"\nelsif n<5\n\tputs \"2\\n1 3\\n\"\nelse\n\tp n\n\tputs (1.step(n,2).to_a+2.step(n,2).to_a)*' '\nend"}, {"source_code": "# http://codeforces.com/contest/534/problem/A\nn = gets.to_i\narr = case n\n when 1, 2\n [1]\n when 3\n [1, 3]\n else\n 1.step(n, 2).to_a + 2.step(n, 2).to_a\n end\n\nputs arr.length\nputs arr.join(' ')\n"}, {"source_code": "a=gets.chomp.to_i\nif a==1\nputs \"1\"\nputs \"1\"\nelsif a==2\nputs \"0\"\nelsif a==3\nputs \"2\"\nputs \"1 3\"\nelse \nputs \"#{a}\"\nif a%2==1\nprintf \"#{a/2 + 1} \"\n0.upto(a/2 - 1) do |i|\nprintf \"#{i+1} #{a-i} \"\nend\nelse\nprintf \"#{a/2 + 1} \"\n0.upto(a/2 - 2) do |i|\nprintf \"#{i+1} #{a-i} \"\nend\nprintf \"#{a/2}\"\nend\nend\n"}, {"source_code": "n = gets.to_i\n\nif n == 2\n pust '1'\n puts '1'\nelsif n == 3\n puts '2'\n puts '1 3'\nelse\n puts n\n\n print 3\n (5..n).step(2).each_with_index { |v,i|\n print ' '\n print v\n }\n print ' '\n print 1\n (2..n).step(2).each_with_index.reverse_each { |v, i|\n print ' '\n print v\n }\n print \"\\n\"\nend\n"}, {"source_code": "def sol(arr,i,n)\n\t# p arr\n\tif i>n\n\t\treturn arr\n\tend\n\n\tch = 0\n\tif !arr.include?(i)\n\t\tif arr == []\n\t\t\tch = 1\n\t\t\tp = sol(arr+[i],1,n)\n\t\telsif (i>=arr[-1]+2 || i<=arr[-1]-2)\n\t\t\tch = 1\n\t\t\tp = sol(arr+[i],1,n)\n\t\tend\n\tend\n\tnp = sol(arr,i+1,n)\n\n\tif ch == 1\n\t\treturn p.length > np.length ? p : np\n\tend\n\treturn np\nend\n\n\nn = gets.chomp.to_i\nf = (1..n/2).to_a.reverse\nbf = ((n/2)+1..n).to_a.reverse\n\n\nif n < 6\n\ta = sol([],1,n)\n\tputs \"#{a.length}\"\n\tputs a.join(\" \")\nelse\n\t\n\tans = []\n\twhile true\n\t\tif f == [] && bf == []\n\t\t\tbreak\n\t\tend\n\t\tif f!=[]\n\t\t\tans << f.pop\n\t\tend\n\t\tif bf!=[]\n\t\t\tans << bf.pop\n\t\tend\n\tend\n\n\tputs \"#{ans.length}\"\n\t\n\tputs ans.join(\" \")\nend"}, {"source_code": "# def sol(arr,i,n)\n# \t# p arr\n# \tif i>n\n# \t\treturn arr\n# \tend\n\n# \tch = 0\n# \tif !arr.include?(i)\n# \t\tif arr == []\n# \t\t\tch = 1\n# \t\t\tp = sol(arr+[i],1,n)\n# \t\telsif (i>=arr[-1]+2 || i<=arr[-1]-2)\n# \t\t\tch = 1\n# \t\t\tp = sol(arr+[i],1,n)\n# \t\tend\n# \tend\n# \tnp = sol(arr,i+1,n)\n\n# \tif ch == 1\n# \t\treturn p.length > np.length ? p : np\n# \tend\n# \treturn np\n# end\n\n\nn = gets.chomp.to_i\nan = [1]\nset = (2..n).to_a\ni = 0\nwhile true\n\tif n == 4\n\t\tbreak\n\tend\n\tch = 0\n\tif i == n\n\t\tbreak\n\tend\n\tset.length.times{|x|\n\t\tif set[x]>=an[i]+2 || set[x] <= an[i]-2\n\t\t\t# p set\n\t\t\tan<< set[x]\n\t\t\tset.delete(set[x])\n\t\t\ti+=1\n\t\t\tch = 1\n\t\t\tbreak\n\t\tend\n\t}\n\tif ch == 0\n\t\tbreak\n\tend\nend\n\nif n == 4\n\tputs 4.to_s\n\tputs [3,1,4,2].join(\" \")\nelse\n\t# a = sol([],1,n)\n\tputs \"#{i}\"\n\t# puts \"#{a.length}\"\n\t# puts a.join(\" \")\n\tputs an.join(\" \")\nend"}, {"source_code": "n = gets.chomp.to_i\nan = [1]\nset = (2..n).to_a\ni = 0\nwhile true\n\tch = 0\n\tif i == n\n\t\tbreak\n\tend\n\tset.length.times{|x|\n\t\tif set[x]>=an[i]+2 || set[x] <= an[i]-2\n\t\t\t# p set\n\t\t\tan<< set[x]\n\t\t\tset.delete(set[x])\n\t\t\ti+=1\n\t\t\tch = 1\n\t\t\tbreak\n\t\tend\n\t}\n\tif ch == 0\n\t\tbreak\n\tend\nend\n\nputs \"#{i+1}\"\nputs an.join(\" \")"}, {"source_code": "# def sol(arr,i,n)\n# \t# p arr\n# \tif i>n\n# \t\treturn arr\n# \tend\n\n# \tch = 0\n# \tif !arr.include?(i)\n# \t\tif arr == []\n# \t\t\tch = 1\n# \t\t\tp = sol(arr+[i],1,n)\n# \t\telsif (i>=arr[-1]+2 || i<=arr[-1]-2)\n# \t\t\tch = 1\n# \t\t\tp = sol(arr+[i],1,n)\n# \t\tend\n# \tend\n# \tnp = sol(arr,i+1,n)\n\n# \tif ch == 1\n# \t\treturn p.length > np.length ? p : np\n# \tend\n# \treturn np\n# end\n\n\nn = gets.chomp.to_i\nan = [1]\nset = (2..n).to_a\ni = 0\nwhile true\n\tif n == 4\n\t\tbreak\n\tend\n\tch = 0\n\tif i == n\n\t\tbreak\n\tend\n\tset.length.times{|x|\n\t\tif set[x]>=an[i]+2 || set[x] <= an[i]-2\n\t\t\t# p set\n\t\t\tan<< set[x]\n\t\t\tset.delete(set[x])\n\t\t\ti+=1\n\t\t\tch = 1\n\t\t\tbreak\n\t\tend\n\t}\n\tif ch == 0\n\t\tbreak\n\tend\nend\n\nif n == 4\n\tputs 4.to_s\n\tputs [3,1,4,2].join(\" \")\nelse\n\t# a = sol([],1,n)\n\tputs \"#{i+1}\"\n\t# puts \"#{a.length}\"\n\t# puts a.join(\" \")\n\tputs an.join(\" \")\nend"}, {"source_code": "n = gets.strip.to_i\nif n <= 2\n puts \"0\"\n exit\nend\n\nif n == 3\n puts n - 1\n puts (1..n).to_a.select { |e| e.odd? }.sort.join(' ')\n exit\nend\nodd = (1..n).to_a.select { |e| e.odd? }.sort\neven = (1..n).to_a.select { |e| e.even? }.sort\n\nputs n\nprint odd.join(' ')\nprint \" \"\nprint even.join(' ')\n\n"}, {"source_code": "n = gets.strip.to_i\nif n <= 2\n puts \"1\"\n puts \"1\"\n exit\nend\n\nif n == 3\n puts n - 1\n puts (1..n).to_a.select { |e| e.odd? }.sort.join(' ')\n exit\nend\nodd = (1..n).to_a.select { |e| e.odd? }.sort\neven = (1..n).to_a.select { |e| e.even? }.sort\n\nputs n\nprint odd.join(' ')\nprint \" \"\nprint even.join(' ')\n\n"}, {"source_code": "n = gets.strip.to_i\nif n <= 2\n puts \"0\"\n exit\nend\n\nif n == 3\n puts (1..n).to_a.select { |e| e.odd? }.sort.join(' ')\n exit\nend\nodd = (1..n).to_a.select { |e| e.odd? }.sort\neven = (1..n).to_a.select { |e| e.even? }.sort\n\nprint odd.join(' ')\nprint \" \"\nprint even.join(' ')\n\n"}, {"source_code": "n = gets.to_i\nif n==1\n print \"1\\n1\\n\"\n exit\nend\nleft = Array.new\nright = Array.new\nk = n/2\nfor i in 1..k\n if i.odd?\n left.push(i)\n right.push(n+1-i)\n else\n left.push(n+1-i)\n right.push(i)\n end\nend\nputs k*2\nputs (left+right).join(' ')\n"}, {"source_code": "n = gets.to_i\nleft = Array.new\nright = Array.new\nk = n/2\nfor i in 1..k\n if i.odd?\n left.push(i)\n right.push(n+1-i)\n else\n left.push(n+1-i)\n right.push(i)\n end\nend\nputs k*2\nputs (left+right).join(' ')\n"}, {"source_code": "n = gets.to_i\nif n==1 or n==2\n print \"1\\n1\\n\"\n exit\nelsif n==3\n print \"2\\n1 3\\n\"\n exit\nelsif n==4\n print \"4\\n1 4 2 3\\n\"\n exit\nend\nleft = Array.new\nright = Array.new\nfor i in (1...n).step(2)\n left.push(i)\n right.push(i+1)\nend\nif n.odd?\n left.push(n)\nend\nputs n\nputs (left+right).join(' ')\n"}, {"source_code": "n = gets.to_i\nif n==1 or n==2\n print \"1\\n1\\n\"\n exit\nelsif n==4\n print \"4\\n3 1 4 2\\n\"\nend\nleft = Array.new\nright = Array.new\nk = n/2\nfor i in 1..k\n if i.odd?\n left.push(i)\n right.push(n+1-i)\n else\n left.push(n+1-i)\n right.push(i)\n end\nend\nputs k*2\nputs (left+right).join(' ')\n"}, {"source_code": "n = gets.to_i\nif n==1 or n==2\n print \"1\\n1\\n\"\n exit\nend\nleft = Array.new\nright = Array.new\nk = n/2\nfor i in 1..k\n if i.odd?\n left.push(i)\n right.push(n+1-i)\n else\n left.push(n+1-i)\n right.push(i)\n end\nend\nputs k*2\nputs (left+right).join(' ')\n"}, {"source_code": "n = gets.to_i\nif n==1 or n==2\n print \"1\\n1\\n\"\n exit\nelsif n==4\n print \"4\\n3 1 4 2\\n\"\n exit\nend\nleft = Array.new\nright = Array.new\nk = n/2\nfor i in 1..k\n if i.odd?\n left.push(i)\n right.push(n+1-i)\n else\n left.push(n+1-i)\n right.push(i)\n end\nend\nputs k*2\nputs (left+right).join(' ')\n"}, {"source_code": "n = gets.to_i\na = []\n((n + 1) / 2).times.each{|i| a << i * 2 + 1}\n((n + 1) / 2).times do |i|\n x = (i + 1) * 2\n a << x if x <= n && (x - a.last).abs > 1\nend\n\na = [1, 4, 2] if n == 4\nputs a.join(\" \")\n"}, {"source_code": "n = gets.to_i\na = []\n((n + 1) / 2).times.each{|i| a << i * 2 + 1}\n((n + 1) / 2).times do |i|\n x = (i + 1) * 2\n a << x if x <= n && (x - a.last).abs > 1\nend\n\na = [1, 4, 2] if n == 4\nputs a.size\nputs a.join(\" \")\n"}], "src_uid": "a52ceb8a894809b570cbb74dc5ef76e1"} {"nl": {"description": "Andrey needs one more problem to conduct a programming contest. He has n friends who are always willing to help. He can ask some of them to come up with a contest problem. Andrey knows one value for each of his fiends — the probability that this friend will come up with a problem if Andrey asks him.Help Andrey choose people to ask. As he needs only one problem, Andrey is going to be really upset if no one comes up with a problem or if he gets more than one problem from his friends. You need to choose such a set of people that maximizes the chances of Andrey not getting upset.", "input_spec": "The first line contains a single integer n (1 ≤ n ≤ 100) — the number of Andrey's friends. The second line contains n real numbers pi (0.0 ≤ pi ≤ 1.0) — the probability that the i-th friend can come up with a problem. The probabilities are given with at most 6 digits after decimal point.", "output_spec": "Print a single real number — the probability that Andrey won't get upset at the optimal choice of friends. The answer will be considered valid if it differs from the correct one by at most 10 - 9.", "sample_inputs": ["4\n0.1 0.2 0.3 0.8", "2\n0.1 0.2"], "sample_outputs": ["0.800000000000", "0.260000000000"], "notes": "NoteIn the first sample the best strategy for Andrey is to ask only one of his friends, the most reliable one.In the second sample the best strategy for Andrey is to ask all of his friends to come up with a problem. Then the probability that he will get exactly one problem is 0.1·0.8 + 0.9·0.2 = 0.26."}, "positive_code": [{"source_code": "n=readline.to_i\np=readline.split(' ').map &:to_f\np=p.sort{|a,b| b<=>a}\nap,bs,ans=1,0,0\np.each do |x|\n nap=ap*(1.0-x)\n nbs=bs+x/(1.0-x)\n if nap*nbs > ans\n ans=nap*nbs\n ap,bs=nap,nbs\n end\nend\nputs [p[0],ans].max\n"}, {"source_code": "n = gets.to_i \na = gets.split.map{ |i| i.to_f } \na.sort! \np = 1 \ns = 0 \nans = 0 \nout = 0\nwhile s < 1.0 && a.length > 0 do \n\tel = a.pop \n\tif s + el * (1 - el) > 1.0 \n\t\tout = [out, ans + p * el * ( 1 - s )].max\n\t\tnext\n\tend\n\tans += p * el * ( 1 - s ) \n\tp *= (1 - el) \n\ts += el / (1 - el) \n\tout = [ans, out].max\nend\nputs out"}, {"source_code": "_ = gets.to_i\np = gets.split.map { |x| x.to_f }\nans = 0.0\nres = 1.0\nwhile p.size > 0\n max = ans\n sel = -1\n p.each_with_index do |x, i|\n if ans * (1.0 - x) + res * x > max\n max = ans * (1.0 - x) + res * x\n sel = i\n end\n end\n break if sel == -1\n ans = max\n res *= 1 - p[sel]\n p.delete_at(sel)\nend\nputs ans\n"}], "negative_code": [{"source_code": "n=readline.to_i\np=readline.split(' ').map &:to_f\np=p.sort{|a,b| b<=>a}\nans=p.shift\np.each {|x| ans=[ans, x*(1.0-ans)+ans*(1.0-x)].max }\nputs ans\n"}, {"source_code": "n=readline.to_i\np=readline.split(' ').map &:to_f\np=p.sort{|a,b| b<=>a}\nap,bs,ans=1,0,p[0]\np.each do |x|\n nap=ap*(1.0-x)\n nbs=bs+x/(1.0-x)\n if nap*nbs > ans\n ans=nap*nbs\n ap,bs=nap,nbs\n end\nend\nputs ans\n"}, {"source_code": "n=readline.to_i\np=readline.split(' ').map &:to_f\np=p.sort{|a,b| b<=>a}\nap,bs,ans=1,0,0\np.each do |x|\n nap=ap*(1.0-x)\n nbs=bs+x/(1.0-x)\n if nap*nbs > ans\n ans=nap*nbs\n ap,bs=nap,nbs\n end\nend\nputs ans\n"}], "src_uid": "b4ac594755951e84001dfd610d420eb5"} {"nl": {"description": "Kevin Sun has just finished competing in Codeforces Round #334! The round was 120 minutes long and featured five problems with maximum point values of 500, 1000, 1500, 2000, and 2500, respectively. Despite the challenging tasks, Kevin was uncowed and bulldozed through all of them, distinguishing himself from the herd as the best cowmputer scientist in all of Bovinia. Kevin knows his submission time for each problem, the number of wrong submissions that he made on each problem, and his total numbers of successful and unsuccessful hacks. Because Codeforces scoring is complicated, Kevin wants you to write a program to compute his final score.Codeforces scores are computed as follows: If the maximum point value of a problem is x, and Kevin submitted correctly at minute m but made w wrong submissions, then his score on that problem is . His total score is equal to the sum of his scores for each problem. In addition, Kevin's total score gets increased by 100 points for each successful hack, but gets decreased by 50 points for each unsuccessful hack.All arithmetic operations are performed with absolute precision and no rounding. It is guaranteed that Kevin's final score is an integer.", "input_spec": "The first line of the input contains five space-separated integers m1, m2, m3, m4, m5, where mi (0 ≤ mi ≤ 119) is the time of Kevin's last submission for problem i. His last submission is always correct and gets accepted. The second line contains five space-separated integers w1, w2, w3, w4, w5, where wi (0 ≤ wi ≤ 10) is Kevin's number of wrong submissions on problem i. The last line contains two space-separated integers hs and hu (0 ≤ hs, hu ≤ 20), denoting the Kevin's numbers of successful and unsuccessful hacks, respectively.", "output_spec": "Print a single integer, the value of Kevin's final score.", "sample_inputs": ["20 40 60 80 100\n0 1 2 3 4\n1 0", "119 119 119 119 119\n0 0 0 0 0\n10 0"], "sample_outputs": ["4900", "4930"], "notes": "NoteIn the second sample, Kevin takes 119 minutes on all of the problems. Therefore, he gets of the points on each problem. So his score from solving problems is . Adding in 10·100 = 1000 points from hacks, his total score becomes 3930 + 1000 = 4930."}, "positive_code": [{"source_code": "p 2.times.map{gets.split.map(&:to_i)}.transpose.map.with_index(1){|(m,w),x|[150*x,(500-2*m)*x-50*w].max}.reduce(:+)+gets.split.map(&:to_i).zip([100,-50]).map{|x,y|x*y}.reduce(:+)"}, {"source_code": "minutes = gets.chomp.split.map(&:to_f)\nwrongs = gets.chomp.split.map(&:to_f)\ns, f = gets.chomp.split.map(&:to_i)\npoints = [500, 1000, 1500, 2000, 2500]\n\nscore = 0\n(0..4).each do |idx|\n score += [\n 0.3 * points[idx],\n (1 - minutes[idx] / 250.0) * points[idx] - 50 * wrongs[idx]\n ].max\nend\nscore += s * 100 - f * 50\nputs score.to_i\n"}, {"source_code": "#! ruby\n# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n\ndef gi(); gets.to_i; end\ndef gis(); gets.chomp.split.map(&:to_i); end\n\nm = gis\nw = gis\nh = gis\n\nscore = (1..5).map{|x| x*500}.zip(m,w).map{|xi,mi,wi| \n [300 * xi, (1000 - 4 * mi) * xi - 50000 * wi].max\n}.inject(:+) + 100000 * h[0] - 50000 * h[1]\n\nputs score.div(1000)"}, {"source_code": "maxpoint=[]\n0.upto(4) do |i|\n\tmaxpoint[i]=(i+1)*500\nend\ntime=gets.chomp.split(\" \")\n0.upto(4) do |i|\n\ttime[i]=time[i].to_i\nend\nwrong=gets.chomp.split(\" \")\n0.upto(4) do |i|\n\twrong[i]=wrong[i].to_i\nend\nhack=gets.chomp.split(\" \")\n0.upto(1) do |i|\n\thack[i]=hack[i].to_i\nend\npoint=[]\n0.upto(4) do |i|\n\tpoint[i]=((1.0-time[i]/250.0)*maxpoint[i])-(50*wrong[i])\n\tif point[i]< (0.3*maxpoint[i])\n\t\tpoint[i]=0.3*maxpoint[i]\n\tend\t\t\nend\npoint=point.inject{|memo,n| memo+n }\npoint=point+(100*hack[0])-(50*hack[1])\nputs point.to_i\n"}, {"source_code": "maxpoint=[]\n0.upto(4) do |i|\n\tmaxpoint[i]=(i+1)*500\nend\ntime=gets.chomp.split(\" \")\n0.upto(4) do |i|\n\ttime[i]=time[i].to_i\nend\nwrong=gets.chomp.split(\" \")\n0.upto(4) do |i|\n\twrong[i]=wrong[i].to_i\nend\nhack=gets.chomp.split(\" \")\n0.upto(1) do |i|\n\thack[i]=hack[i].to_i\nend\npoint=[]\n0.upto(4) do |i|\n\tpoint[i]=((1.0-time[i]/250.0)*maxpoint[i])-(50*wrong[i])\n\tif point[i]< (0.3*maxpoint[i])\n\t\tpoint[i]=0.3*maxpoint[i]\n\tend\t\t\nend\npoint=point.inject{|memo,n| memo+n }\npoint=point+(100*hack[0])-(50*hack[1])\nputs point.to_i"}, {"source_code": "minutes=[]\nmins=gets.chomp.split\nmins.each do |i|\n\tminutes.push(i.to_i)\nend\n\nhacked=[]\nhaks=gets.chomp.split\nhaks.each do |i|\n\thacked.push(i.to_i)\nend\n\nsuccess=[]\nsucs=gets.chomp.split\nsucs.each do |i|\n\tsuccess.push(i.to_i)\nend\n\nsum=0\n\na1=(250-minutes[0])*2-hacked[0]*50\nb1=3*50\nif a1>b1\n\tsum+=a1\nelse\n\tsum+=b1\nend\n\na2=(250-minutes[1])*4-hacked[1]*50\nb2=3*100\nif a2>b2\n\tsum+=a2\nelse\n\tsum+=b2\nend\n\na3=(250-minutes[2])*6-hacked[2]*50\nb3=3*150\nif a3>b3\n\tsum+=a3\nelse\n\tsum+=b3\nend\n\na4=(250-minutes[3])*8-hacked[3]*50\nb4=3*200\nif a4>b4\n\tsum+=a4\nelse\n\tsum+=b4\nend\n\na5=(250-minutes[4])*10-hacked[4]*50\nb5=3*250\nif a5>b5\n\tsum+=a5\nelse\n\tsum+=b5\nend\n\nsum=sum-success[1]*50+success[0]*100\n\nputs sum"}, {"source_code": "m = []\nw = []\nh = []\nm = gets.split.map(&:to_i)\nw = gets.split.map(&:to_i)\nh = gets.split.map(&:to_i)\n\ntmp = 0\n5.times do |v|\n ms = 500*(v+1)\n ma = 0.3*ms\n mt = (1-m[v]/250.0)*ms - 50*w[v]\n if ma < mt\n tmp += mt\n else\n tmp += ma\n end\nend\ntmp += (100*h[0] - 50*h[1])\nputs tmp.to_i"}, {"source_code": "xs = [500, 1000, 1500, 2000, 2500]\nms = gets.chomp.split.map(&:to_i)\nws = gets.chomp.split.map(&:to_i)\nhs, hu = gets.chomp.split.map(&:to_i)\n\nputs (0..4).to_a.inject(100 * hs - 50 * hu) { |m, i| m + [3 * xs[i] / 10, xs[i] - ms[i] * (xs[i] / 250) - 50 * ws[i]].max }\n\n"}, {"source_code": "m = gets.split.to_a.map(&:to_f)\nw = gets.split.to_a.map(&:to_f)\nhs, hu = gets.split.map(&:to_f)\nx=[500,1000,1500,2000,2500]\nresult = 0\nfor i in 0..4\n\tif(0.3*x[i]>(1-m[i]/250)*x[i]-50*w[i])\n\t\tresult+= 0.3*x[i]\n\telse\n\t\t\tresult+= (1-m[i]/250)*x[i]-50*w[i]\n\tend\nend\n result+=hs*100-hu*50\n p result.to_i"}, {"source_code": "m = gets.split(\" \").map(&:to_i)\nw = gets.split(\" \").map(&:to_i)\nh = gets.split(\" \").map(&:to_i)\nx = [500, 1000, 1500, 2000, 2500]\ndef f(a, b, c)\n\treturn [0.3*c, (1-a/250.0)*c-50*b].max\nend\nsol = 100*h[0]-50*h[1]\nfor i in 0..4\n\tsol += f(m[i], w[i], x[i])\nend\nputs sol.to_i\n"}, {"source_code": "m = gets.split.map { |i| i.to_i }\nw = gets.split.map { |i| i.to_i }\nh = gets.split.map { |i| i.to_i }\n\nx = [1, 2, 3, 4, 5]\n\nres = 0\n\n(0..4).each do |i|\n res += [150 * x[i], (500 - m[i] * 2) * x[i] - 50 * w[i]].max\nend\n\nres += h[0] * 100 - h[1] * 50\n\nputs res\n"}, {"source_code": "m = gets.split.map(&:to_i)\nw = gets.split.map(&:to_i)\nh = gets.split.map(&:to_i)\ns = [500, 1000, 1500, 2000, 2500]\nans = 5.times.map{|i| [3 * s[i] / 10, (250 - m[i]) * s[i] / 250 - 50 * w[i]].max}.inject(:+)\nans += h[0] * 100 - h[1] * 50\nputs ans\n"}, {"source_code": "m = gets.split.map(&:to_i)\nw = gets.split.map(&:to_i)\nh = gets.split.map(&:to_i)\ns = [500, 1000, 1500, 2000, 2500]\nans = 0\n5.times{|i| ans += [0.3 * s[i], (250 - m[i]) * s[i] / 250 - 50 * w[i]].max }\nputs ans.to_i + h[0] * 100 - h[1] * 50\n"}, {"source_code": "xx = [500, 1000, 1500, 2000, 2500]\nmm = gets.chomp.split(\" \").map(&:to_f)\nww = gets.chomp.split(\" \").map(&:to_f)\nhs, hu = gets.chomp.split(\" \").map(&:to_i)\ncalc_score = ->(x, m, w) {\n (250 - m) * x / 250 - 50 * w\n}\nscores = Array.new\n(0...5).each do |i|\n scores << [0.3*xx[i], calc_score.call(xx[i], mm[i], ww[i])].max\nend\nputs (scores.inject(:+) + hs * 100 - hu * 50).to_i\n"}, {"source_code": "maxpoint=[]\n0.upto(4) do |i|\n\tmaxpoint[i]=(i+1)*500\nend\ntime=gets.chomp.split(\" \")\n0.upto(4) do |i|\n\ttime[i]=time[i].to_i\nend\nwrong=gets.chomp.split(\" \")\n0.upto(4) do |i|\n\twrong[i]=wrong[i].to_i\nend\nhack=gets.chomp.split(\" \")\n0.upto(1) do |i|\n\thack[i]=hack[i].to_i\nend\npoint=[]\n0.upto(4) do |i|\n\tpoint[i]=((1.0-time[i]/250.0)*maxpoint[i])-(50*wrong[i])\n\tif point[i]< (0.3*maxpoint[i])\n\t\tpoint[i]=0.3*maxpoint[i]\n\tend\t\t\nend\npoint=point.inject{|memo,n| memo+n }\npoint=point+(100*hack[0])-(50*hack[1])\nputs point.to_i\n"}, {"source_code": "max_marks = [500, 1000, 1500, 2000, 2500]\nsolution_times = $stdin.readline.split.map(&:to_i)\nnums_wrong_attempts = $stdin.readline.split.map(&:to_i)\ngood_cracks, bad_cracks = $stdin.readline.split.map(&:to_i)\n\nscore = max_marks.zip(solution_times, nums_wrong_attempts).map{|max_mark, solution_time, num_wrong_attempts|\n [3.quo(10)*max_mark, (1 - solution_time.quo(250) ) * max_mark - 50 * num_wrong_attempts].max\n}.inject(0, &:+)\n\nputs (score + good_cracks * 100 - bad_cracks * 50).to_i\n\n"}, {"source_code": "class Solver\n\tdef main\n\t\tmax_score = [500, 1000, 1500, 2000, 2500]\n\t\tlast_submission_time = gets.split.map { |x| x.to_i }\n\t\twrong_submissions = gets.split.map { |x| x.to_i }\n\t\tsuccessful_hacks, unsuccessful_hacks = gets.split.map { |x| x.to_i }\n\n\t\tproblem_score = Array.new(5, 0)\n\t\t0.upto(5 - 1) do |i|\n\t\t\tlhs = max_score[i] * 3 / 10\n\t\t\trhs = max_score[i] - max_score[i] / 250 * last_submission_time[i] \\\n\t\t\t\t- wrong_submissions[i] * 50\n\t\t\tproblem_score[i] = [lhs, rhs].max\n\t\tend\n\n\t\ttotal_score = problem_score.reduce(:+)\n\t\ttotal_score += successful_hacks * 100\n\t\ttotal_score -= unsuccessful_hacks * 50\n\t\tputs total_score\n\tend\nend\n\nSolver.new.main\n"}], "negative_code": [{"source_code": "#! ruby\n# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n\ndef gi(); gets.to_i; end\ndef gis(); gets.chomp.split.map(&:to_i); end\n\nm = gis\nw = gis\nh = gis\n\nscore = (1..5).map{|x| x*500}.zip(m,w).map{|xi,mi,wi| [0.3 * xi, (1.0 - mi / 250.0) * xi - 50.0 * wi].max}.inject(:+) + 100 * h[0] + 50 * h[1]\n\nputs score.floor"}, {"source_code": "#! ruby\n# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n\ndef gi(); gets.to_i; end\ndef gis(); gets.chomp.split.map(&:to_i); end\n\nm = gis\nw = gis\nh = gis\n\nscore = (1..5).map{|x| x*500}.zip(m,w).map{|xi,mi,wi| \n [300 * xi, (1000 - 4 * mi) * xi - 500 * wi].max\n}.inject(:+) + 100000 * h[0] + 50000 * h[1]\n\nputs score.div(1000)"}, {"source_code": "#! ruby\n# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n\ndef gi(); gets.to_i; end\ndef gis(); gets.chomp.split.map(&:to_i); end\n\nm = gis\nw = gis\nh = gis\n\nscore = (1..5).map{|x| x*500}.zip(m,w).map{|xi,mi,wi| \n [300 * xi, (1000 - 4 * mi) * xi - 50000 * wi].max\n}.inject(:+) + 100000 * h[0] + 50000 * h[1]\n\nputs score.div(1000)"}, {"source_code": "maxpoint=[]\n0.upto(4) do |i|\n\tmaxpoint[i]=(i+1)*500\nend\ntime=gets.chomp.split(\" \")\n0.upto(4) do |i|\n\ttime[i]=time[i].to_i\nend\nwrong=gets.chomp.split(\" \")\n0.upto(4) do |i|\n\twrong[i]=wrong[i].to_i\nend\nhack=gets.chomp.split(\" \")\n0.upto(1) do |i|\n\thack[i]=hack[i].to_i\nend\npoint=[]\n0.upto(4) do |i|\n\tpoint[i]=((1.0-time[i]/250.0)*maxpoint[i])-(50*wrong[i])\n\tif point[i]< (0.3*maxpoint[i])\n\t\tpoint[i]=0.3*maxpoint[i]\n\tend\t\t\nend\npoint1=point.inject{|memo,n| memo+n }\npoint1=point1+(100*hack[0])-(50*hack[1])\n"}, {"source_code": "minutes=[]\nmins=gets.chomp.split\nmins.each do |i|\n\tminutes.push(i.to_i)\nend\n\nhacked=[]\nhaks=gets.chomp.split\nhaks.each do |i|\n\thacked.push(i.to_i)\nend\n\nsuccess=[]\nsucs=gets.chomp.split\nsucs.each do |i|\n\tsuccess.push(i.to_i)\nend\n\nsum=(((250-minutes[0]).to_f/250)*500+((250-minutes[1]).to_f/250)*1000+((250-minutes[2]).to_f/250)*1500+(1-(minutes[3]).to_f/250)*2000+(1-(minutes[4]).to_f/250)*2500-(hacked[4]+hacked[3]+hacked[2]+hacked[1]+hacked[0])*50+success[0]*100-success[1]*50).to_i\nputs sum"}, {"source_code": "xs = [500, 1000, 1500, 2000, 2500]\nms = gets.chomp.split.map(&:to_i)\nws = gets.chomp.split.map(&:to_i)\nhs, hu = gets.chomp.split.map(&:to_i)\n\nputs (0..4).to_a.inject(0) { |m, i| m + [0.3 * xs[i], xs[i] - ms[i] * xs[i] / 250 - 50 * ws[i]].max } + 100 * hs - 50 * hu\n"}, {"source_code": "m = gets.split.map { |i| i.to_i }\nw = gets.split.map { |i| i.to_i }\nh = gets.split.map { |i| i.to_i }\n\nx = [1, 2, 3, 4, 5]\n\nres = 0\n\n(0..4).each do |i|\n res += [0.3 * x[i] * 500, (500 - m[i] * 2) * x[i] - 50 * w[i]].max\nend\n\nres += h[0] * 100 - h[1] * 50\n\nputs res\n"}, {"source_code": "m = gets.split.map(&:to_i)\nw = gets.split.map(&:to_i)\nh = gets.split.map(&:to_i)\ns = [500, 1000, 1500, 2000, 2500]\nans = 0\n5.times{|i| ans += [0.3 * s[i], (250 - m[i]) * s[i] / 250 - 50 * w[i]].max }\nputs ans + h[0] * 100 - h[1] * 50\n"}, {"source_code": "xx = [500, 1000, 1500, 2000, 2500]\nmm = gets.chomp.split(\" \").map(&:to_i)\nww = gets.chomp.split(\" \").map(&:to_i)\nhs, hu = gets.chomp.split(\" \").map(&:to_i)\ncalc_score = ->(x, m, w) {\n (250 - m) * x / 250 - 50 * w\n}\nscores = Array.new\n(0...5).each do |i|\n scores << [0.3*xx[i], calc_score.call(xx[i], mm[i], ww[i])].max\nend\nputs scores.inject(:+) + hs * 100 - hu * 50\n"}], "src_uid": "636a30a2b0038ee1731325a5fc2df73a"} {"nl": {"description": "The research center Q has developed a new multi-core processor. The processor consists of n cores and has k cells of cache memory. Consider the work of this processor.At each cycle each core of the processor gets one instruction: either do nothing, or the number of the memory cell (the core will write an information to the cell). After receiving the command, the core executes it immediately. Sometimes it happens that at one cycle, multiple cores try to write the information into a single cell. Unfortunately, the developers did not foresee the possibility of resolving conflicts between cores, so in this case there is a deadlock: all these cores and the corresponding memory cell are locked forever. Each of the locked cores ignores all further commands, and no core in the future will be able to record an information into the locked cell. If any of the cores tries to write an information into some locked cell, it is immediately locked.The development team wants to explore the deadlock situation. Therefore, they need a program that will simulate the processor for a given set of instructions for each core within m cycles . You're lucky, this interesting work is entrusted to you. According to the instructions, during the m cycles define for each core the number of the cycle, during which it will become locked. It is believed that initially all cores and all memory cells are not locked.", "input_spec": "The first line contains three integers n, m, k (1 ≤ n, m, k ≤ 100). Then follow n lines describing instructions. The i-th line contains m integers: xi1, xi2, ..., xim (0 ≤ xij ≤ k), where xij is the instruction that must be executed by the i-th core at the j-th cycle. If xij equals 0, then the corresponding instruction is «do nothing». But if xij is a number from 1 to k, then the corresponding instruction is «write information to the memory cell number xij». We assume that the cores are numbered from 1 to n, the work cycles are numbered from 1 to m and the memory cells are numbered from 1 to k.", "output_spec": "Print n lines. In the i-th line print integer ti. This number should be equal to 0 if the i-th core won't be locked, or it should be equal to the number of the cycle when this core will be locked.", "sample_inputs": ["4 3 5\n1 0 0\n1 0 2\n2 3 1\n3 2 0", "3 2 2\n1 2\n1 2\n2 2", "1 1 1\n0"], "sample_outputs": ["1\n1\n3\n0", "1\n1\n0", "0"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/env ruby\n\n# n - cores\n# m - ticks\n# k - shared cache memory\n\ninput = STDIN.read.split(\"\\n\")\nn, m, k = *input[0].split(' ').map(&:to_i)\ncommands = input[1..-1].map{|s| s.split(' ').map(&:to_i) }\n\ncores = Array.new n, 0\nblocked_cells = []\n\ndefine_method(:working_cores) do\n cores.map.with_index{|c,i| c == 0 ? i : nil }.compact\nend\n\n1.upto(m).each do |tick|\n working_cores.\n map { |core| commands[core][tick - 1] }.\n compact.\n group_by(&:to_i).\n select{|_,xs| xs.count > 1 }.\n keys.\n reject{|c| c == 0 }.\n reject{|c| blocked_cells.include?(c) }.\n each { |cell| blocked_cells += [cell] } \n\n cores.map.with_index do |s,i|\n next if s > 0\n cores[i] = tick if blocked_cells.include?(commands[i][tick - 1])\n end\nend\n\ncores.each do |core|\n puts core\nend"}, {"source_code": "input = STDIN.read.split(\"\\n\")\nn, m, k = *input[0].split(' ').map(&:to_i)\ncommands = input[1..-1].map{|s| s.split(' ').map(&:to_i) }\n\ncores = Array.new n, 0\nblocked_cells = []\n\ndefine_method(:working_cores) do\n cores.map.with_index{|c,i| c == 0 ? i : nil }.compact\nend\n\n1.upto(m).each do |tick|\n working_cores.\n map { |core| commands[core][tick - 1] }.\n compact.\n group_by(&:to_i).\n select{|_,xs| xs.count > 1 }.\n keys.\n reject{|c| c == 0 }.\n reject{|c| blocked_cells.include?(c) }.\n each { |cell| blocked_cells += [cell] } \n\n cores.map.with_index do |s,i|\n next if s > 0\n cores[i] = tick if blocked_cells.include?(commands[i][tick - 1])\n end\nend\n\nputs cores"}, {"source_code": "n, m, k = gets.split.map &:to_i\na = Array.new\nn.times do |i|\n\ta[i] = gets.split.map { |x| x.to_i - 1 }\nend\nlock = Array.new(n) { 0 }\nlockcell = Array.new(k) { false }\n0.upto(m - 1) do |i|\n\tturn = Array.new(k) { -1 }\n\t0.upto(n - 1) do |j|\n\t\tif lock[j] == 0 && a[j][i] != -1\n\t\t\tif lockcell[a[j][i]]\n\t\t\t\tlock[j] = i + 1\n\t\t\telsif turn[a[j][i]] != -1\n\t\t\t\tlock[turn[a[j][i]]] = lock[j] = i + 1\n\t\t\t\tlockcell[a[j][i]] = true\n\t\t\tend\n\t\t\tturn[a[j][i]] = j\n\t\tend\n\tend\nend\nputs lock\n"}, {"source_code": "n, m, k = gets.split.map {|x| Integer x}\na = Array.new(n)\nfor i in 0...n\n a[i] = Array.new\n s = gets.split.map {|x| (Integer x) - 1}\n for j in 0...m\n a[i][j] = s[j]\n end\nend\nbad_cell = [false] * k\nbad_core = [false] * n\nprev = [nil] * k\ntime = [0] * n\nfor t in 0...m\n used = [false] * k\n for c in 0...n\n if bad_core[c] or a[c][t] == -1\n next\n end\n if bad_cell[a[c][t]] or used[a[c][t]]\n bad_cell[a[c][t]] = true\n bad_core[c] = true\n if time[c] == 0\n time[c] = t + 1\n end\n bad_core[prev[a[c][t]]] = true\n if time[prev[a[c][t]]] == 0\n time[prev[a[c][t]]] = t + 1\n end\n end\n used[a[c][t]] = true\n prev[a[c][t]] = c\n end\nend\nputs time\n"}], "negative_code": [{"source_code": "#!/usr/bin/env ruby\n\n# n - cores\n# m - ticks\n# k - shared cache memory\n\ninput = STDIN.read.split(\"\\n\")\nn, m, k = *input[0].split(' ').map(&:to_i)\ncommands = input[1..-1].map{|s| s.split(' ').map(&:to_i) }\n\ncores = Array.new n, 0\nblocked_cells = []\n\ndefine_method(:working_cores) do\n cores.map.with_index{|c,i| c == 0 ? i : nil }.compact\nend\n\n1.upto(m).each do |tick|\n working_cores.\n map { |core| commands[core][tick - 1] }.\n compact.\n group_by(&:to_i).\n select{|_,xs| xs.count > 1 }.\n keys.\n reject{|c| blocked_cells.include?(c) }.\n each { |cell| blocked_cells += [cell] } \n\n cores.map.with_index do |s,i|\n next if s > 0\n cores[i] = tick if blocked_cells.include?(commands[i][tick - 1])\n end\nend\n\ncores.each do |core|\n puts core\nend"}], "src_uid": "6422a70e686c34b4b9b6b5797712998e"} {"nl": {"description": "Due to the coronavirus pandemic, city authorities obligated citizens to keep a social distance. The mayor of the city Semyon wants to light up Gluharniki park so that people could see each other even at night to keep the social distance.The park is a rectangular table with $$$n$$$ rows and $$$m$$$ columns, where the cells of the table are squares, and the boundaries between the cells are streets. External borders are also streets. Every street has length $$$1$$$. For example, park with $$$n=m=2$$$ has $$$12$$$ streets.You were assigned to develop a plan for lighting the park. You can put lanterns in the middle of the streets. The lamp lights two squares near it (or only one square if it stands on the border of the park). The park sizes are: $$$n=4$$$, $$$m=5$$$. The lighted squares are marked yellow. Please note that all streets have length $$$1$$$. Lanterns are placed in the middle of the streets. In the picture not all the squares are lit. Semyon wants to spend the least possible amount of money on lighting but also wants people throughout the park to keep a social distance. So he asks you to find the minimum number of lanterns that are required to light all the squares.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case is a line containing two integers $$$n$$$, $$$m$$$ ($$$1 \\le n, m \\le 10^4$$$) — park sizes.", "output_spec": "Print $$$t$$$ answers to the test cases. Each answer must be a single integer — the minimum number of lanterns that are required to light all the squares.", "sample_inputs": ["5\n1 1\n1 3\n2 2\n3 3\n5 3"], "sample_outputs": ["1\n2\n2\n5\n8"], "notes": "NotePossible optimal arrangement of the lanterns for the $$$2$$$-nd test case of input data example: Possible optimal arrangement of the lanterns for the $$$3$$$-rd test case of input data example: "}, "positive_code": [{"source_code": "t = gets.chomp.to_i\nt.times do |testcase|\n (n,m) = gets.split.map(&:to_i)\n puts (n*m+1)/2\nend"}, {"source_code": "t = gets.to_i\n\n1.upto t do |i|\n n, m = gets.split.map(&:to_i)\n puts ((n * m) / 2 + (n * m) % 2)\nend"}, {"source_code": "gets.to_i.times {\n a, b = gets.split.map { |v| v.to_i }\n s = a * b\n puts s / 2 + s % 2\n}\n"}, {"source_code": "gets.to_i.times do\n n, m = gets.split(' ').map(&:to_i)\n\n puts n.div(2)*m + n.modulo(2)*m.div(2) + n.modulo(2)*m.modulo(2)\nend\n"}], "negative_code": [], "src_uid": "19df5f3b8b31b763162c5331c1499529"} {"nl": {"description": "We'll call a set of positive integers a beautiful if the following condition fulfills: for any prime p, if , then . In other words, if one number from the set is divisible by prime p, then at least half of numbers from the set is divisible by p.Your task is to find any beautiful set, where the number of elements is equal to k and each element doesn't exceed 2k2.", "input_spec": "The first line contains integer k (10 ≤ k ≤ 5000) that shows how many numbers the required beautiful set should have.", "output_spec": "In the first line print k space-separated integers that are a beautiful set. If there are multiple such sets, you are allowed to print any of them.", "sample_inputs": ["10"], "sample_outputs": ["16 18 24 27 36 48 54 72 108 144"], "notes": null}, "positive_code": [{"source_code": "k = gets.to_i\n\nm = 2*k*k\n\nL = []\nK = []\ndem = []\nP = [0,2,3,5,7,11,13,17,19]\n\n(1..8).each do |i|\n K[i] = []\nend\n\n(1..8).each do |i|\n j = 0 \n while P[i]**j <= m do\n K[i][j] = P[i]**j\n j += 1\n end\n dem[i] = j - 1\nend\n\ncount = 1\n\nL[1] = 1\ni = 1\nwhile count <= k\n c = count\n (1..dem[i]).each do |j|\n (1..c).each do |l|\n if ( K[i][j]*L[l] <= m )\n count += 1\n L[count] = K[i][j]*L[l]\n end\n end\n end\n i += 1\nend\n\nL[0] = 0\nL.sort!\n(count-k+1..count-1).each do |i|\n print L[i],\" \"\nend\n\nputs L[count]"}], "negative_code": [], "src_uid": "74713233dd60acfbc0b26cd3772ed223"} {"nl": {"description": "There is a given string S consisting of N symbols. Your task is to find the number of ordered pairs of integers i and j such that1. 1 ≤ i, j ≤ N2. S[i] = S[j], that is the i-th symbol of string S is equal to the j-th.", "input_spec": "The single input line contains S, consisting of lowercase Latin letters and digits. It is guaranteed that string S in not empty and its length does not exceed 105.", "output_spec": "Print a single number which represents the number of pairs i and j with the needed property. Pairs (x, y) and (y, x) should be considered different, i.e. the ordered pairs count.", "sample_inputs": ["great10", "aaaaaaaaaa"], "sample_outputs": ["7", "100"], "notes": null}, "positive_code": [{"source_code": "puts gets.chomp.chars.group_by{|i|i}.map{|i,j|j.size**2}.inject(:+)"}, {"source_code": "#=================================\nif ARGV.include?(\"ONLINE_JUDGE\")\n\t$stdin = File.open(\"./input.txt\", \"r\")\n\tARGV.delete(\"ONLINE_JUDGE\")\nend\n#=================================\n\nline=gets.strip\nhs=Hash.new(0)\n\nline.each_char{|c|\n\ths[c.ord]+=1\n}\n\nans=0\nhs.values.each{|v|\n\tans+=v**2\n}\n\nputs ans\n"}, {"source_code": "counter = Hash.new(0)\nSTDIN.read.gsub(\"\\n\", \"\").split(\"\").each { |symbol| counter[symbol] += 1 }\nputs counter.values.inject(0) { |res, count| res + count ** 2}\n\n"}, {"source_code": "s = gets.chomp!\n\nmas = Hash.new()\ns.chars { |c|\n mas[c] == nil ? mas[c] = 1 : mas[c] += 1\n}\n\nsum = 0\nmas.each_value {|d|\n sum += d**2\n}\n\nputs sum"}], "negative_code": [], "src_uid": "6bb2793e275426eb076972fab69d0eba"} {"nl": {"description": "Vasily exited from a store and now he wants to recheck the total price of all purchases in his bill. The bill is a string in which the names of the purchases and their prices are printed in a row without any spaces. Check has the format \"name1price1name2price2...namenpricen\", where namei (name of the i-th purchase) is a non-empty string of length not more than 10, consisting of lowercase English letters, and pricei (the price of the i-th purchase) is a non-empty string, consisting of digits and dots (decimal points). It is possible that purchases with equal names have different prices.The price of each purchase is written in the following format. If the price is an integer number of dollars then cents are not written.Otherwise, after the number of dollars a dot (decimal point) is written followed by cents in a two-digit format (if number of cents is between 1 and 9 inclusively, there is a leading zero).Also, every three digits (from less significant to the most) in dollars are separated by dot (decimal point). No extra leading zeroes are allowed. The price always starts with a digit and ends with a digit.For example: \"234\", \"1.544\", \"149.431.10\", \"0.99\" and \"123.05\" are valid prices, \".333\", \"3.33.11\", \"12.00\", \".33\", \"0.1234\" and \"1.2\" are not valid. Write a program that will find the total price of all purchases in the given bill.", "input_spec": "The only line of the input contains a non-empty string s with length not greater than 1000 — the content of the bill. It is guaranteed that the bill meets the format described above. It is guaranteed that each price in the bill is not less than one cent and not greater than 106 dollars.", "output_spec": "Print the total price exactly in the same format as prices given in the input.", "sample_inputs": ["chipsy48.32televizor12.390", "a1b2c3.38", "aa0.01t0.03"], "sample_outputs": ["12.438.32", "6.38", "0.04"], "notes": null}, "positive_code": [{"source_code": "def to_num(s)\n a = s.split(/\\./)\n\n n = a.length\n if n == 1\n return s.to_i * 100\n end\n\n if a[n-1].length == 2\n return a[0..n-2].join('').to_i * 100 + a[n-1].to_i\n end\n\n a.join('').to_i * 100\nend\n\ndef to_str(x)\n rem = x % 100\n ret = rem == 0 ? '' : \".%02d\" % (x % 100)\n\n s = (x / 100).to_s\n i = s.length - 1\n\n while i >= 0 do\n j = i - 2\n if j < 0\n j = 0\n end\n ret = '.' + s[j..i] + ret\n i = j - 1\n end\n\n ret[1..-1]\nend\n\na = STDIN.gets.gsub(/[a-z]/,' ').split.map { |s| to_num(s) }\nsum = a.inject(&:+)\nputs to_str(sum)\n"}, {"source_code": "def parse(s)\n bias = 1\n if s.length <= 3 || s[-4] == '.'\n bias = 100\n end\n s.gsub!(/\\./, \"\")\n bias * s.to_i\nend\n\ndef enchant(n)\n s = \"\"\n if n % 100 != 0\n s = format(\".%02d\", n % 100)\n end\n n /= 100\n n = n.to_s\n nz = n.length\n for i in 0 .. nz - 1\n s = n[nz - 1 - i] + s\n if i < nz - 1 && i % 3 == 2\n s = \".\" + s\n end\n end\n s\nend\n\ns = gets.chomp\ns = s.split(/[a-z]+/).select{|v| v != \"\"}\ntot = 0\nfor v in s\n tot += parse(v)\nend\n\nputs enchant(tot)\n"}], "negative_code": [{"source_code": "def to_num(s)\n a = s.split(/\\./)\n\n n = a.length\n if n == 1\n return s.to_f\n end\n\n if a[n-1].length == 2\n x = a[0..n-2].join('') + '.' + a[n-1]\n return x.to_f\n end\n\n a.join('').to_f\nend\n\ndef to_str(x)\n x = x.round(2)\n if x.to_s == x.to_i.to_s\n return x.to_i.to_s\n end\n a = x.to_s.split(/\\./)\n s = a[0]\n ret = \".#{a[1]}\"\n\n i = s.length - 1\n while i >= 0 do\n j = i - 2\n if j < 0\n j = 0\n end\n ret = '.' + s[j..i] + ret\n i = j - 1\n end\n\n ret[1..-1]\nend\n\na = STDIN.gets.gsub(/[a-z]/,' ').split.map { |s| to_num(s) }\nsum = a.inject(&:+)\nputs to_str(sum)\n"}, {"source_code": "def to_num(s)\n a = s.split(/\\./)\n\n n = a.length\n if n == 1\n return s.to_i * 100\n end\n\n if a[n-1].length == 2\n return a[0..n-2].join('').to_i * 100 + a[n-1].to_i\n end\n\n a.join('').to_i * 100\nend\n\ndef to_str(x)\n if x % 100 == 0\n return (x / 100).to_s\n end\n\n s = (x / 100).to_s\n ret = \".%02d\" % (x % 100)\n\n i = s.length - 1\n while i >= 0 do\n j = i - 2\n if j < 0\n j = 0\n end\n ret = '.' + s[j..i] + ret\n i = j - 1\n end\n\n ret[1..-1]\nend\n\na = STDIN.gets.gsub(/[a-z]/,' ').split.map { |s| to_num(s) }\nsum = a.inject(&:+)\nputs to_str(sum)\n"}, {"source_code": "def parse(s)\n bias = 1\n if s.length <= 3 || s[-4] == '.'\n bias = 100\n end\n s.gsub!(/\\./, \"\")\n bias * s.to_i\nend\n\ndef enchant(n)\n s = \"\"\n if n % 100 != 0\n s = format(\".%02d\", n % 100)\n end\n n /= 100\n n = n.to_s\n nz = n.length\n for i in 0 .. nz - 1\n s = n[nz - 1 - i] + s\n if i < nz - 1 && i % 3 == 2\n s = \".\" + s\n end\n end\n s\nend\n\ns = gets.chomp\ns = s.split(/[a-z]+/).select{|v| v != \"\"}\np s\ntot = 0\nfor v in s\n tot += parse(v)\nend\n\nputs enchant(tot)\n"}], "src_uid": "8da703549a3002bf9131d6929ec026a2"} {"nl": {"description": "Vlad went into his appartment house entrance, now he is on the $$$1$$$-th floor. He was going to call the elevator to go up to his apartment.There are only two elevators in his house. Vlad knows for sure that: the first elevator is currently on the floor $$$a$$$ (it is currently motionless), the second elevator is located on floor $$$b$$$ and goes to floor $$$c$$$ ($$$b \\ne c$$$). Please note, if $$$b=1$$$, then the elevator is already leaving the floor $$$1$$$ and Vlad does not have time to enter it. If you call the first elevator, it will immediately start to go to the floor $$$1$$$. If you call the second one, then first it will reach the floor $$$c$$$ and only then it will go to the floor $$$1$$$. It takes $$$|x - y|$$$ seconds for each elevator to move from floor $$$x$$$ to floor $$$y$$$.Vlad wants to call an elevator that will come to him faster. Help him choose such an elevator.", "input_spec": "The first line of the input contains the only $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. This is followed by $$$t$$$ lines, three integers each $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \\le a, b, c \\le 10^8$$$, $$$b \\ne c$$$) — floor numbers described in the statement.", "output_spec": "Output $$$t$$$ numbers, each of which is the answer to the corresponding test case. As an answer, output: $$$1$$$, if it is better to call the first elevator; $$$2$$$, if it is better to call the second one; $$$3$$$, if it doesn't matter which elevator to call (both elevators will arrive in the same time). ", "sample_inputs": ["3\n\n1 2 3\n\n3 1 2\n\n3 2 1"], "sample_outputs": ["1\n3\n2"], "notes": "NoteIn the first test case of the example, the first elevator is already on the floor of $$$1$$$.In the second test case of the example, when called, the elevators would move as follows: At the time of the call, the first elevator is on the floor of $$$3$$$, and the second one is on the floor of $$$1$$$, but is already going to another floor; in $$$1$$$ second after the call, the first elevator would be on the floor $$$2$$$, the second one would also reach the floor $$$2$$$ and now can go to the floor $$$1$$$; in $$$2$$$ seconds, any elevator would reach the floor $$$1$$$. In the third test case of the example, the first elevator will arrive in $$$2$$$ seconds, and the second in $$$1$$$."}, "positive_code": [{"source_code": "\r\nt = gets.chomp.to_i \r\ntab = []\r\nfor i in 1..t \r\n \r\n a, b , c = gets.chomp.split(' ').map(&:to_i)\r\n \r\n\r\n if ( (a-1).abs ) < ((b-c).abs + (c-1).abs)\r\n tab.push(1)\r\n elsif ( (a-1).abs ) > ((b-c).abs + (c-1).abs)\r\n tab.push(2)\r\n else \r\n tab.push(3)\r\n end\r\nend\r\n\r\nfor i in tab\r\n puts \"#{i}\"\r\nend"}, {"source_code": "n={1=>1,2=>3,3=>2}; $stdin.each_line.to_a[1..-1].each do |x| x=x.split(\" \").map(&:to_i); puts n[(x[0]-1<=>(x[1]-x[2]).abs+(x[2]-1))+2] end\r\n"}, {"source_code": "n = { 1 => 1, 2 => 3, 3 => 2 }\r\n$stdin.each_line.with_index do |x, i|\r\n next if i == 0 \r\n a, b, c = x.split(\" \").map(&:to_i)\r\n t1 = a - 1\r\n t2 = (b - c).abs + (c - 1)\r\n puts n[(t1 <=> t2) + 2]\r\nend"}, {"source_code": "\r\ndef debug(*arg)\r\n puts \"[DEBUG] #{arg.join \" \"}\"\r\nend\r\n\r\nt = gets.to_i\r\n\r\nt.times do\r\n a, b, c = gets.split.map(&:to_i)\r\n\r\n\r\n if a == (b-c).abs + c\r\n puts 3\r\n elsif a < (b-c).abs + c\r\n puts 1\r\n else\r\n puts 2\r\n end\r\nend\r\n\r\n"}, {"source_code": "t=gets.to_i\r\nfor t0 in (0...t)\r\n m=gets.split(' ').to_a\r\n a=m[0].to_i\r\n b=m[1].to_i\r\n c=m[2].to_i\r\n r=(b-c).abs+c\r\n puts 1 if ar\r\n puts 3 if a==r\r\nend\r\n"}, {"source_code": "$stdin.readline\r\n\r\nmap = { -1 => 1, 0 => 3, 1 => 2}\r\n\r\n$stdin.each_line do |line|\r\n a, b, c = line.chomp.split.map(&:to_i)\r\n puts map[(a - c <=> (c - b).abs)]\r\nend"}, {"source_code": "def solve(a, b, c)\r\n first_dis = (a - 1).abs\r\n second_dis = (b - c).abs + (c - 1).abs\r\n if first_dis == second_dis\r\n p 3\r\n elsif first_dis < second_dis\r\n p 1\r\n else\r\n p 2\r\n end\r\nend\r\n\r\nt = gets.chomp.to_i\r\n\r\nfor i in 1..t\r\n a, b, c = gets.chomp.split(' ').map(&:to_i)\r\n solve(a,b,c)\r\nend"}, {"source_code": "def check_elevator(a,b,c)\r\n comp=((c-b).abs+c)-a\r\n if comp>0\r\n 1\r\n elsif comp==0\r\n 3\r\n else\r\n 2\r\n end\r\nend\r\n\r\nn_rounds=gets.to_i\r\n\r\nn_rounds.times do\r\n puts check_elevator(*(gets.split(' ').map(&:to_i)))\r\nend\r\n"}], "negative_code": [{"source_code": "c = Hash.new(nil)\r\n$stdin.each_line.with_index do |l, i| next if i == 0 || i % 2 == 1\r\n puts c[l] if c[l] != nil\r\n nt = 0\r\n o = \"\"\r\n l.chomp.each_char.with_index do |x, i|\r\n if nt != 0\r\n nt -= 1\r\n next\r\n end\r\n if (i < (l.chomp.length - 2)) && (l[i + 2] == \"0\") && (i > l.chomp.length - 4 || l[i + 3] != \"0\")\r\n nt = 2\r\n o += (\"a\"..\"z\").to_a[(x + l[i + 1]).to_i - 1]\r\n next\r\n end\r\n o += (\"a\"..\"z\").to_a[x.to_i - 1]\r\n end\r\n c[l]=o\r\n puts o\r\nend"}, {"source_code": "\r\ndef debug(*arg)\r\n puts \"[DEBUG] #{arg.join \" \"}\"\r\nend\r\n\r\nt = gets.to_i\r\n\r\nt.times do\r\n a, b, c = gets.split.map(&:to_i)\r\n\r\n # debug(a, b + c -b)\r\n if a == b + c - b + 1\r\n puts 3\r\n elsif a < b + c - b + 1\r\n puts 1\r\n else\r\n puts 2\r\n end\r\nend\r\n\r\n"}], "src_uid": "aca2346553f9e7b6e944ca2c74bb0b3d"} {"nl": {"description": "Vasya is writing an operating system shell, and it should have commands for working with directories. To begin with, he decided to go with just two commands: cd (change the current directory) and pwd (display the current directory).Directories in Vasya's operating system form a traditional hierarchical tree structure. There is a single root directory, denoted by the slash character \"/\". Every other directory has a name — a non-empty string consisting of lowercase Latin letters. Each directory (except for the root) has a parent directory — the one that contains the given directory. It is denoted as \"..\".The command cd takes a single parameter, which is a path in the file system. The command changes the current directory to the directory specified by the path. The path consists of the names of directories separated by slashes. The name of the directory can be \"..\", which means a step up to the parent directory. «..» can be used in any place of the path, maybe several times. If the path begins with a slash, it is considered to be an absolute path, that is, the directory changes to the specified one, starting from the root. If the parameter begins with a directory name (or \"..\"), it is considered to be a relative path, that is, the directory changes to the specified directory, starting from the current one.The command pwd should display the absolute path to the current directory. This path must not contain \"..\".Initially, the current directory is the root. All directories mentioned explicitly or passed indirectly within any command cd are considered to exist. It is guaranteed that there is no attempt of transition to the parent directory of the root directory.", "input_spec": "The first line of the input data contains the single integer n (1 ≤ n ≤ 50) — the number of commands. Then follow n lines, each contains one command. Each of these lines contains either command pwd, or command cd, followed by a space-separated non-empty parameter. The command parameter cd only contains lower case Latin letters, slashes and dots, two slashes cannot go consecutively, dots occur only as the name of a parent pseudo-directory. The command parameter cd does not end with a slash, except when it is the only symbol that points to the root directory. The command parameter has a length from 1 to 200 characters, inclusive. Directories in the file system can have the same names.", "output_spec": "For each command pwd you should print the full absolute path of the given directory, ending with a slash. It should start with a slash and contain the list of slash-separated directories in the order of being nested from the root to the current folder. It should contain no dots.", "sample_inputs": ["7\npwd\ncd /home/vasya\npwd\ncd ..\npwd\ncd vasya/../petya\npwd", "4\ncd /a/b\npwd\ncd ../a/b\npwd"], "sample_outputs": ["/\n/home/vasya/\n/home/\n/home/petya/", "/a/b/\n/a/a/b/"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\ndir = []\nn.times do\n f = gets.chomp\n if f =~ /^pwd$/\n puts \"/\" + dir.join(\"/\") + ( dir.length > 0 ? \"/\" : \"\" )\n else\n f = f.partition(\" \")[2]\n dir.clear if f[0] == \"/\"\n f.split(\"/\").each do |x|\n if x =~ /^\\.\\.$/\n dir.pop\n elsif x.length > 0\n dir << x\n end\n end\n end\nend"}, {"source_code": "p='/'\ngets.to_i.times{|_|\ngets\n/cd /?(p=File.expand_path($'.chop,p)):puts((p[/(\\/\\w+)+/]||'')+'/')\n}\n"}, {"source_code": "current_path = []\n\nn = gets.to_i\nn.times do\n s = gets.strip\n if s == 'pwd'\n answer = '/' + current_path.join('/')\n answer += '/' unless current_path.empty?\n puts answer\n else\n path = s.split.last.split('/')\n absolute = path.first == ''\n if absolute\n current_path = path\n path.shift\n else\n current_path += path\n end\n\n while index = current_path.index('..')\n current_path.delete_at(index)\n current_path.delete_at(index-1)\n end\n end\nend\n"}, {"source_code": "gets\ns=?/\n$<.map{|i|(i=i.split[1])?(i.sub!(/^\\//){s=?/\np}\n(i.sub!(/^(\\w+).?/){s<<$1+?/\np}||(i=i[3..-1]||\"\"\ns[/\\w+.$/]\ns=$`))while\"\" 0 ? \"/\" + my_stack.join('/') + \"/\" : \"/\"\n elsif command[0] == \"cd\"\n command[1].split(\"/\").each do |step|\n if step != \"..\"\n if step == \"\"\n my_stack = []\n else\n my_stack.push step\n end\n else\n if my_stack.size > 0\n my_stack.pop\n end\n end\n end\n end\nend\n"}, {"source_code": "class NilClass\n def nil_or_empty?\n true\n end\nend\nclass String\n def nil_or_empty?\n empty?\n end\nend\n\n# $stdin = File.open(\"1.txt\")\na = %w(/)\ngets.to_i.times do |j|\n s = gets.strip\n if s == \"pwd\"\n puts a*''\n else\n path = s.split[1].split('/')\n a = %w(/) if path[0].nil_or_empty?\n path.each { |e|\n next if e.nil_or_empty?\n if e == '..'\n a.pop\n else\n a.push(e + \"/\")\n end\n }\n end\nend"}, {"source_code": "l = STDIN.read.split(\"\\n\")\n\nst = []\ncommands = l[1..-1]\n\ncommands.each{ |c|\n if c.start_with?(\"cd\") then\n parts = c.split(\" \")[1].split(\"/\")\n\n if c.split(\" \")[1].start_with?(\"/\")\n st.clear\n end\n \n parts.each{ |part|\n if part == \"..\" then\n st.pop\n else\n if part != \"\" then st.push(part) end\n end\n }\n end\n \n if c.start_with?(\"pwd\") then\n puts (st.count >= 1 ? '/' : '') + st.join('/') + '/'\n end\n}"}, {"source_code": "i = gets.chomp.to_i\n\n\ncurr_dir = '/'\ni.times do\n cmd = gets.chomp\n\n if cmd == 'pwd'\n puts curr_dir\n else\n cmd.sub('cd ', '').split('/').each do |c|\n if c == ''\n curr_dir = '/'\n elsif c == '..'\n curr_dir = curr_dir[0..(curr_dir.length-2)]\n curr_dir = curr_dir[0..curr_dir.rindex('/')]\n else\n curr_dir << c\n curr_dir << '/' if curr_dir[-1] != '/'\n end\n end\n end\nend"}, {"source_code": "n = gets.chomp.to_i\ncurrent_dir = [\"\"]\nn.times do\n inp = gets.chomp.split(' ')\n cmd = inp[0]\n if cmd == 'pwd'\n current_dir.each do |d|\n print \"#{d}/\"\n end\n print \"\\n\"\n else\n args = inp[1]\n if args[0...1] == '/'\n # puts 'nonone';\n current_dir = []\n end\n args.split('/').each do |arg|\n if arg == '..'\n current_dir.pop\n else\n current_dir.push arg\n end\n end\n end\nend"}, {"source_code": "class Node\n attr_accessor :element, :parent, :children\n\n def initialize(element, parent)\n @element = element\n @parent = parent\n @children = []\n end\nend\n\nn = gets.strip.to_i\nroot = Node.new(\"/\", nil)\ncurrent_node = root\ncommands = []\nn.times do \n commands.push(gets.strip)\nend\n \ndef print_current_path(current_node)\n path = ['/']\n # p current_node\n # if current_node.element == '/'\n # path.push('/')\n # else\n while !(current_node.element == '/')\n path.unshift(current_node.element)\n current_node = current_node.parent\n path.unshift(\"/\")\n end\n # end\n puts path.join\nend\n\nputs \"\\n\"\ncommands.each do |command| \n if command == 'pwd'\n print_current_path(current_node)\n elsif command =~ /cd/\n path = command.split(\" \").last\n if path[0] == '/'\n current_node = root\n path = path[1..-1]\n end\n directories = path.split(\"/\")\n directories.each do |dir|\n if dir == '..'\n current_node = current_node.parent\n else\n new_node = Node.new(dir, current_node)\n if !current_node.children.include?(new_node)\n current_node.children.push(new_node)\n end\n current_node = new_node\n end\n end\n end\nend\n"}, {"source_code": "_ = STDIN.readline\nlines = STDIN.readlines.map(&:chomp)\n\n@path = []\n\ndef pwd\n puts '/' + (@path.size > 0 ? (@path.join('/') + '/') : '')\nend\n\nlines.each do |l|\n if l == 'pwd'\n pwd\n next\n end\n\n _, dir = l.split(' ')\n if dir[0] == '/'\n dir = dir[1..-1]\n @path = []\n end\n\n comps = dir.split('/')\n comps.each do |c|\n if c == '..'\n @path.pop\n else\n @path.push(c)\n end\n end\nend\n"}, {"source_code": "n = STDIN.readline.to_i\n\nnow = []\nfor i in 1..n do\n line = STDIN.readline.strip\n vc = line.split(/[ \\/]+/)\n len = vc.size\n \n #puts \"R: \" + vc.inspect\n if(vc.first == \"cd\")\n now = [] if line[3] == '/'\n vc[1..len].each do |cmd|\n if cmd == \"..\"\n now.pop\n else\n now << cmd\n end\n end\n else\n res = \"/\"+ now.join(\"/\")\n res = res + \"/\" if(res != \"/\")\n \n puts res\n end\nend\n"}, {"source_code": "p='/'\ngets.to_i.times{|_|\ngets\n/cd /?(p=File.expand_path($'.chop,p)):puts((p[/(\\/\\w+)+/]||'')+'/')\n}\n\n"}, {"source_code": "p='/'\ngets.to_i.times{|_|\ngets\n/cd /?(p=File.expand_path($'.chop,p)):puts((p[/(\\/\\w+)+/]||'')+'/')\n}\n\n"}, {"source_code": "counter = gets.chomp.to_i\ncurrentPath = '/'\n\ncounter.times do\n command = gets.chomp\n command = command.split(' ')\n\n if command[0] == 'pwd'\n if currentPath == '/'\n puts currentPath\n else\n puts currentPath + '/'\n end\n else #cd\n elements = command[1].split('/')\n\n elements.each { |element|\n\n case element\n when ''\n currentPath = ''\n when '..'\n dirs = currentPath.split('/')\n currentPath = ''\n for counter in 1 .. dirs.length-2\n currentPath += '/' + dirs[counter]\n end\n else\n if currentPath == '/'\n currentPath += element\n else\n currentPath += '/' + element\n end\n end\n }\n end\n\nend"}, {"source_code": "commands = STDIN.readlines.map(&:chomp).drop(1)\ncommands.inject([]){|current_dir, c|\n case c\n when /^pwd/\n puts ([\"\"] + current_dir + [\"\"]).join(\"/\")\n when /^cd /\n params = c.sub(/^cd /){\"\"}.split(\"/\")\n if params[0] == \"\"\n current_dir.clear\n params.shift\n end\n params.each{|d|\n if d == \"..\"\n current_dir.pop\n else\n current_dir.push(d)\n end\n }\n end\n current_dir\n}\n"}, {"source_code": "p='/'\ngets.to_i.times{|_|\ngets\n/cd /?(p=File.expand_path($'.chop,p)):puts((p[/(\\/\\w+)+/]||'')+'/')\n}"}, {"source_code": "require 'pp'\ndef s(str)\n split = str.split(\"/\")\n p = []\n for s in split\n if(s == '..')\n p.pop\n else\n p.push(s)\n end\n end\n r = p.join(\"/\")\n if r == ''\n r = '/'\n end\n return r.gsub('//','/')\nend\n\nstr = STDIN.read.split(\"\\n\").drop(1)\n\ncur_path = '/'\nfor s in str\n s.strip!\n next if s == ''\n \n if(s == 'pwd')\n cur_path = s(cur_path) \n puts (cur_path + '/').gsub('//','/')\n else\n match = s.match(/cd (.*)/)\n path = match[1]\n if path[0] == '/'\n cur_path = path\n else\n cur_path += '/' + path\n end\n end\nend"}, {"source_code": "$n = gets.to_i\n$path = []\n\n$n.times do\n cmd = gets.chomp.split ' '\n if cmd[0] == 'pwd' then\n if $path.empty? then\n puts '/'\n else\n puts '/' + $path.join('/') + '/'\n end\n elsif cmd[0] == 'cd' then\n dirs = cmd[1].split '/'\n if cmd[1][0] == '/' then\n $path = []\n dirs.shift\n end\n dirs.each do |dir|\n if dir == '..' then\n $path.pop\n else\n $path.push dir\n end\n end\n end\nend\n"}, {"source_code": "i = gets.to_i\ncurrent = ''\noutput = \"\"\ni.times do \n cmd, param = gets.chomp.split(' ')\n if current.empty?\n c = ''\n else\n c = '/'\n end\n output << \"#{c}\" + current + \"/\\n\" if cmd == 'pwd'\n\n if cmd == 'cd'\n if param[0] == '/'\n current = ''\n param = param[1..-1]\n end\n\n param = param.split('/')\n current = current.split('/')\n until param.empty? do\n p = param.shift\n if p == '..'\n current.pop\n else\n current.push(p)\n end\n end\n current = current.join('/')\n end\nend\n\nprint output\n"}, {"source_code": "def cleanup_path(path)\n\ttmp = []\n\tpath.each do |el|\n\t\tif el == \"..\"\n\t\t\ttmp = tmp[0..-2]\n\t\telse\n\t\t\ttmp += [el]\n\t\tend\n\tend\n\ttmp\nend\n\ndef shell(commands)\n\tstack = []\n\tcommands.each do |cmd|\n\t\tif(cmd == \"pwd\")\n\t\t\tprint(\"/\" + stack.join(\"/\"))\n\t\t\tputs( stack.length > 0 ? \"/\" : \"\")\n\t\telsif cmd[0..2] == \"cd \"\n\t\t\tnew_path = cmd[3..-1].split(\"/\")\n\t\t\tif new_path[0] == \"\"\n\t\t\t\tstack = cleanup_path(new_path[1..-1])\n\t\t\telse\n\t\t\t\tstack = cleanup_path(stack + new_path)\n\t\t\tend\n\t\telse \n\t\t\tputs(\"weird command: #{cmd}\")\n\t\t\tfail\n\t\tend\n\tend\nend\n\nno_lines = STDIN.readline.chomp.to_i\n\n\ncommands = STDIN.readlines.map(&:chomp)\n\n#puts(commands.to_s)\n\nshell(commands)"}, {"source_code": "n=gets.chomp.to_i\ncur=[]\nn.times{\n\ts=gets.chomp.to_s\n\tunless s=~/pwd/\n\t\ti=3\n\t\tif s[i]=='/'\n\t\t\tcur=[]\n\t\t\ti+=1\n\t\tend\n\t\twhile true\n\t\t\tif s[i..-1]=~/^([\\.]{2})\\//\n\t\t\t\tcur.pop()\n\t\t\t\ti+=3\n\t\t\t\tnext\n\t\t\tend\n\t\t\tif s[i..-1]=~/^([^\\/.]*)\\//\n\t\t\t\tj=i+$~[1].size\n\t\t\t\tword=s[i...j]\n\t\t\t\tcur.push(word)\n\t\t\t\ti=j+1\n\t\t\t\tnext\n\t\t\tend\n\t\t\tbreak\n\t\tend\n\t\tif s[i..-1]=~/^([^\\/.]*)$/\n\t\t\tcur.push($~[1])\n\t\telsif s[i..-1]=~/^[\\.]{2}$/\n\t\t\tcur.pop()\n\t\tend\n\telse\n\t\tif cur.size>0\n\t\t\tputs (\"/\"+cur.join(\"/\")+'/')\n\t\telse\n\t\t\tputs '/'\n\t\tend\n\tend\n}"}, {"source_code": "#!/usr/bin/ruby\nstack = []\nn = STDIN.gets.chomp.to_i\nn.times do\n command = STDIN.gets.chomp\n if command == \"pwd\"\n if stack.empty?\n puts \"/\"\n else\n puts \"/\" + stack.join(\"/\") + \"/\"\n end\n else\n path = command[3..command.length]\n if path.start_with?(\"/\")\n stack.clear\n path = path[1..path.length]\n end\n path.split(/\\//).each do |token|\n if token == \"..\"\n stack.pop\n else\n stack.push(token)\n end\n end\n end\nend\n"}, {"source_code": "n = gets.to_i\ncurrent_directory = []\n\n1.upto(n) do |i|\n command = gets.split(' ')\n if command[0] == \"pwd\"\n puts current_directory.empty? ? '/' : '/' + current_directory.join('/') + '/'\n else\n root_path = true if command[1].start_with?('/')\n path = command[1].split('/').delete_if { |e| e == \"\" }\n current_directory.clear if root_path\n path.each { |dir| \n dir=='..' ? current_directory.pop : current_directory<= 1 ? '/' : '') + st.join('/') + '/'\n end\n}"}, {"source_code": "size = gets.chomp.to_i\ncurdir = \"/\"\nout = []\nsize.times {\n str = gets.chomp\n parts = str.split(\" \")\n command = parts[0]\n if command == \"pwd\" then\n out.push curdir\n end\n if command == \"cd\" then\n if parts[1][0] == \"/\" then\n curdir = \"\"\n end\n dirs = parts[1].split(\"/\")\n dirs.each do |x|\n if x == \"..\" \n arr = curdir.split(\"/\")\n arr.pop\n curdir = arr.join(\"/\")\n curdir = curdir + \"/\"\n else\n curdir = curdir + x + \"/\"\n end\n end\n end\n}\nout.each do |x| \n puts x\nend"}, {"source_code": "p='/'\ngets.to_i.times{|_|\ngets\n/cd /?(p=File.expand_path($'.chop,p)):puts((p[/(\\/\\w+)+/]||'')+'/')\n}\n\n"}, {"source_code": "def pwd(path)\n path.each {|e| print \"/\" + e}\n print \"/\\n\"\nend\n\n\nn = gets.to_i\n\nstack = []\n\nn.times do\n command = gets\n com_list = command.split\n if com_list[0] == \"pwd\" then\n pwd(stack)\n else\n dirs = com_list[1].split \"/\"\n dirs.each do |dir|\n if dir == \"..\" then\n stack.pop\n end\n if dir == \"\" then\n stack.clear\n end\n if dir != \"\" and dir != \"..\" then\n stack.push dir\n end\n end\n end\nend"}, {"source_code": "p='/'\ngets.to_i.times{|_|\ngets\n/cd /?(p=File.expand_path($'.chop,p)):puts((p[/(\\/\\w+)+/]||'')+'/')\n}"}, {"source_code": "def pwd(path)\n path.each {|e| print \"/\" + e}\n print \"/\\n\"\nend\n\n\nn = gets.to_i\n\nstack = []\n\nn.times do\n command = gets\n com_list = command.split\n if com_list[0] == \"pwd\" then\n pwd(stack)\n else\n dirs = com_list[1].split \"/\"\n dirs.each do |dir|\n if dir == \"..\" then\n stack.pop\n end\n if dir == \"\" then\n stack.clear\n end\n if dir != \"\" and dir != \"..\" then\n stack.push dir\n end\n end\n end\nend\n"}, {"source_code": "def cd(a,b)\n b=b.split('/')\n if b[0]==\"\" then\n a.clear\n b.delete_at(0)\n end\n for i in 0...b.size do\n if b[i]==\"..\"\n a.delete_at(a.size-1)\n elsif\n a << b[i]\n end\n end\n return a\nend\n\ndef pwd(a)\n for i in 0...a.size do\n print \"/#{a[i]}\"\n end\n puts \"/\"\nend\n\na=[]\ngets.to_i.times do |x|\n com=gets.split\n if com[0]==\"cd\"\n a=cd(a,com[1])\n elsif com[0]==\"pwd\"\n pwd(a)\n end\nend\n"}, {"source_code": "n = gets.to_i\nwd = []\nn.times do\n c = gets.chomp\n if c == 'pwd'\n puts wd.size == 0 ? '/' : \"/#{wd.join(\"/\")}/\"\n else\n c, t = c.split(\" \")\n t = t.split(\"/\")\n if t[0] == ''\n t = t[1..-1] \n wd = []\n end\n t.each do |x|\n if x == '..'\n wd.pop\n else\n wd.push x\n end\n end\n end\nend\n"}, {"source_code": "n = gets.chomp.to_i\n\ndir = []\nn.times{\n cmd = gets.split\n if cmd[0] == 'pwd'\n if dir == []\n puts '/'\n else\n puts ['/', dir.join('/'), '/'].join\n end\n next\n end\n \n if cmd[1][0].to_s == '/'\n dir = []\n cmd[1] = cmd[1][1..-1]\n end\n \n cmd[1].split('/').each{|t|\n if t == '..'\n dir = dir[0..-2]\n else\n dir << t\n end\n }\n}"}, {"source_code": "p='/'\ngets.to_i.times{|_|\ngets\n/cd /?(p=File.expand_path($'.chop,p)):puts((p[/(\\/\\w+)+/]||'')+'/')\n}"}, {"source_code": "n=gets.to_i\n\npwd=\"/\"\n\nn.times{\n\ns=gets.chomp\nif s==\"pwd\"\nprint pwd\nprint \"\\n\"\nelse\nfor i in s.split(' ')[1].split('/')\n\nif i==\"\"\npwd=\"/\"\nelsif i==\"..\"\npwd=pwd.split('/')\npwd.pop\npwd.push \"\"\npwd=pwd.join('/')\nelse\npwd+=i+'/'\nend\n\nend\nend\n\n}"}, {"source_code": "p='/'\ngets.to_i.times{|_|\ngets\n/cd /?(p=File.expand_path($'.chop,p)):puts((p[/(\\/\\w+)+/]||'')+'/')\n}\n\n"}, {"source_code": "def pwd(path)\n if path.empty?\n puts \"/\"\n else\n puts \"/#{path.join '/'}/\"\n end\nend\n\npath = []\nn = gets.to_i\n\nfor str in STDIN.each_line.take(n)\n arr = str.chomp.split(' ')\n\n if arr.size == 1\n pwd path\n else\n path.clear if arr[1][0] == '/'\n p = arr[1].split('/').reject &:empty?\n p.each { |t| (t == '..') ? path.pop : path.push(t) }\n end\nend"}, {"source_code": "# public: Simulate directory\nclass Directory\n def initialize\n\t@dir = []\n end\n \n def pwd\n\tputs to_dir\n end\n \n def to_dir\n dir_string = \"/\" << @dir.join(\"/\")\n\tif dir_string[-1] == \"/\"\n\t return dir_string\n\telse\n\t return dir_string << \"/\"\n\tend\n end\n \n def cd(dir)\n\tstack = dir.split(\"/\")\n\tif stack[0] == \"\" || stack == []\n\t absolute_dir(stack)\n\telse\n\t relative_dir(stack)\n\tend\n end\n \n def absolute_dir(stack)\n\tstack.shift\n\t@dir = []\n\trelative_dir(stack)\n end\n \n def relative_dir(stack)\n stack.each do |dir|\n\t if dir == \"..\"\n\t @dir.pop\n\t else\n\t @dir.push(dir)\n\t end\n\tend\n end\nend\n\ndir = Directory.new\ngets.to_i.times do\n dir.send(*gets.chomp.split)\nend"}, {"source_code": "input = STDIN.read.split(\"\\n\")\ninput_num = input.shift.to_i\n\n$tree = {\n\t'/' => nil\n}\n\n$pwd = '/'\n\ndef add_dir dir\n\n\tif $pwd == '/'\n\n\t\tif $tree['/'] == nil\n\n\t\t\t$tree['/'] = {dir => nil}\n\n\t\telse\n\n\t\t\t$tree['/'].merge!({dir => nil})\n\n\t\tend\n\n\telse\n\n\t\ttree = $pwd.split('/')\n\n\t\ttree[0] = '/'\n\n\t\ttree.inject($tree) do |acc, value|\n\n\t\t\tif acc[value] == nil\n\t\t\t\tacc[value] = {$dir => nil}\n\t\t\telse\n\t\t\t\tacc[value].merge!({dir => nil})\n\t\t\tend\n\n\t\tend\n\n\tend\n\t\n\tif $pwd == '/'\n\t\t$pwd += dir\n\telse\n\t\t$pwd += '/' + dir\n\tend\n\nend\n\ninput.each do |str|\n\n\tif str == 'pwd'\n\t\tif $pwd == '/'\n\t\t\tSTDOUT.puts '/'\n\t\telse\n\t\t\tSTDOUT.puts $pwd + '/'\n\t\tend\n\telse\n\n\t\tdirs = str[3..-1].split('/')\n\n\t\tdirs.each do |dir|\n\n\t\t\tif dir == \"\"\n\n\t\t\t\t$pwd = '/'\n\n\t\t\telsif dir != '..'\n\n\t\t\t\tadd_dir dir\n\n\t\t\telse\n\n\t\t\t\t$pwd = $pwd.split('/')\n\t\t\t\t$pwd.pop\n\t\t\t\t$pwd = $pwd.join('/')\n\n\t\t\t\t$pwd = '/' if $pwd == ''\n\n\t\t\tend\n\n\t\tend\n\tend\n\nend"}, {"source_code": "p='/'\ngets.to_i.times{|_|\ngets\n/cd /?(p=File.expand_path($'.chop,p)):puts((p[/(\\/\\w+)+/]||'')+'/')\n}\n\n"}, {"source_code": "n = gets\nn = n.to_i\ncommands = Array.new\nwhile n != 0\n commands << gets\n n -= 1\nend\n@currentpwd = Array.new\n\ncommands.each do |command|\n # puts \"doing command #{command}\"\n command = command.gsub(/\\n/,\"\")\n if command == 'pwd'\n if @currentpwd.any?\n p = \"\"\n @currentpwd.each do |path|\n p += \"/\"\n p += path\n end\n puts p + \"/\"\n else\n puts '/'\n end\n else\n precommand = command.split(\" \")\n precommand = precommand[1].split(\"/\")\n # puts \"splitting #{precommand}\"\n precommand.each do |dir|\n if dir == \"\"\n @currentpwd = Array.new\n else\n if dir == \"..\"\n if @currentpwd.any?\n @currentpwd = @currentpwd.slice(0, @currentpwd.length-1)\n end\n # puts \"her #{@currentpwd}\"\n else\n @currentpwd << dir\n end\n end\n end\n # puts \"current dir #{@currentpwd}\"\n end\nend\n\n# p @currentpwd"}, {"source_code": "input = STDIN.read.split(\"\\n\")\ninput_num = input.shift.to_i\n\n$tree = {\n\t'/' => nil\n}\n\n$pwd = '/'\n\ndef add_dir dir\n\tif $pwd == '/'\n\t\tif $tree['/'] == nil\n\t\t\t$tree['/'] = {dir => nil}\n\t\telse\n\t\t\t$tree['/'].merge!({dir => nil})\n\t\tend\n\telse\n\t\ttree = $pwd.split('/')\n\t\ttree[0] = '/'\n\t\ttree.inject($tree) do |acc, value|\n\t\t\tif acc[value] == nil\n\t\t\t\tacc[value] = {$dir => nil}\n\t\t\telse\n\t\t\t\tacc[value].merge!({dir => nil})\n\t\t\tend\n\t\tend\n\tend\n\tif $pwd == '/'\n\t\t$pwd += dir\n\telse\n\t\t$pwd += '/' + dir\n\tend\nend\n\ninput.each do |str|\n\tif str == 'pwd'\n\t\tif $pwd == '/'\n\t\t\tSTDOUT.puts '/'\n\t\telse\n\t\t\tSTDOUT.puts $pwd + '/'\n\t\tend\n\telse\n\t\tdirs = str[3..-1].split('/')\n\t\tdirs.each do |dir|\n\t\t\tif dir == \"\"\n\t\t\t\t$pwd = '/'\n\t\t\telsif dir != '..'\n\t\t\t\tadd_dir dir\n\t\t\telse\n\t\t\t\t$pwd = $pwd.split('/')\n\t\t\t\t$pwd.pop\n\t\t\t\t$pwd = $pwd.join('/')\n\t\t\t\t$pwd = '/' if $pwd == ''\n\t\t\tend\n\t\tend\n\tend\nend"}, {"source_code": "p='/'\ngets.to_i.times{|_|\ngets\n/cd /?(p=File.expand_path($'.chop,p)):puts((p[/(\\/\\w+)+/]||'')+'/')\n}\n\n"}], "negative_code": [{"source_code": "p = '/'\ngets.to_i.times do |_|\n gets\n if /cd /\n p = File.expand_path($'.chop, p);\n else\n puts p == '/' ? p : p + '/'\n end\nend\n"}, {"source_code": "p = '/'\ngets.to_i.times do |_|\n gets\n if /cd /\n p = File.expand_path($'.chop, p);\n else\n puts $& + '/' if p =~ /(\\/\\w+)*/\n end\nend\n"}, {"source_code": "n=gets.to_i\npwd=\"/\"\nn.times do\n a=gets.split\n if a[0]=='pwd' then\n puts pwd\n else\n if a[1][0]==\"/\" then\n pwd=a[1]\n if pwd[pwd.length-1]!=\"/\" then\n pwd+=\"/\"\n end\n else\n path=a[1].split(\"/\")\n path.each{|k|\n if k!=\"..\" then\n pwd=pwd+k+\"/\"\n else\n pwd=pwd[0..(pwd.length-2)]\n while pwd[pwd.length-1]!=\"/\"\n pwd=pwd[0..(pwd.length-2)]\n end\n end\n }\n end\n end\nend"}, {"source_code": "n = gets.to_i\ndir = \"/\"\nn.times do\n s = gets.chomp\n if s == \"pwd\"\n puts dir\n next\n end\n dir += s[3, s.size - 1] + \"/\"\n while dir.sub!(\"//\", \"/\")\n end\n while dir.sub!(/\\w+\\/\\.\\./, \"/\")\n end\n while dir.sub!(\"//\", \"/\")\n end\nend"}, {"source_code": "n = gets.to_i\ndir = \"/\"\nn.times do\n s = gets.chomp\n if s == \"pwd\"\n puts dir\n next\n end\n dir += s[3, s.size - 1] + \"/\"\n while dir.sub!(\"//\", \"/\")\n end\n while dir.sub!(/\\w+\\/\\.\\./, \"/\")\n while dir.sub!(\"//\", \"/\")\n end\n end\nend"}, {"source_code": "def main()\n count = gets.chomp\n arr_dir = Array.new\n dir = arr_dir\n for i in 0..count.to_i-1\n command = gets.chomp \n if command.start_with?(\"pwd\")\n if arr_dir.empty?\n puts \"/\"\n else\n puts \"/#{arr_dir.join(\"/\")}/\"\n end\n elsif command.start_with?(\"cd\")\n arr_1 = command.split(\" \")\n s = arr_1[1]\n if s.start_with?(\"/\")\n s[0] = \"\"\n end\n arr_2 = s.split(\"/\")\n arr_2.each { |x| \n if x == \"..\"\n arr_dir.delete_at(-1)\n else \n arr_dir << x\n end\n } \n else \n puts \"Error\"\n end\n end\nend\n\nmain"}, {"source_code": "def main()\n count = gets.chomp\n arr_dir = Array.new\n dir = arr_dir\n for i in 0..count.to_i-1\n command = gets.chomp \n if command.start_with?(\"pwd\")\n puts \"/#{arr_dir.join(\"/\")}/\"\n elsif command.start_with?(\"cd\")\n arr_1 = command.split(\" \")\n s = arr_1[1]\n if s.start_with?(\"/\")\n s[0] = \"\"\n end\n arr_2 = s.split(\"/\")\n arr_2.each { |x| \n if x == \"..\"\n arr_dir.delete_at(-1)\n else \n arr_dir << x\n end\n } \n else \n puts \"Error\"\n end\n end\nend\n\nmain"}, {"source_code": "def main()\n count = gets.chomp\n arr_dir = Array.new\n dir = arr_dir\n for i in 0..count.to_i-1\n command = gets.chomp \n if command.start_with?(\"pwd\")\n puts \"/#{arr_dir.join(\"/\")}\"\n elsif command.start_with?(\"cd\")\n arr_1 = command.split(\" \")\n s = arr_1[1]\n if s.start_with?(\"/\")\n s[0] = \"\"\n end\n arr_2 = s.split(\"/\")\n arr_2.each { |x| \n if x == \"..\"\n arr_dir.delete_at(-1)\n else \n arr_dir << x\n end\n } \n else \n puts \"Error\"\n end\n end\nend\n\nmain"}, {"source_code": "def main()\n count = gets.chomp\n arr_dir = Array.new\n dir = arr_dir\n for i in 0..count.to_i-1\n command = gets.chomp \n if command.start_with?(\"pwd\")\n if arr_dir.empty?\n puts \"/\"\n else\n puts \"/#{arr_dir.join(\"/\")}/\"\n end\n elsif command.start_with?(\"cd\")\n arr_1 = command.split(\" \")\n s = arr_1[1]\n if s.start_with?(\"/\")\n s[0] = \"\"\n arr_dir.delete_at(-1)\n end\n arr_2 = s.split(\"/\")\n arr_2.each { |x| \n if x == \"..\"\n arr_dir.delete_at(-1)\n else \n arr_dir << x\n end\n } \n else \n puts \"Error\"\n end\n end\nend\n\nmain"}, {"source_code": "path = []\ngets.to_i.times do\n\tcmd, arg = gets.split(/\\s+/)\n\tif cmd == \"pwd\"\n\t\tputs path.inject(\"/\") { |s, i| s += \"#{i}/\" }\n\telse\n\t\tdirs = arg.split(/\\//).drop_while { |s| s == \"\" }\n\t\tif arg.start_with? \"/\"\n\t\t\tpath = dirs\n\t\telse\n\t\t\tdirs.each do |i|\n\t\t\t\tif i == \"..\"\n\t\t\t\t\tpath.pop\n\t\t\t\telse\n\t\t\t\t\tpath << i\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\tend\nend\n"}, {"source_code": "n = gets\nmy_stack = []\n\nn.to_i.times do\n command = gets.strip.split(\" \")\n\n if command[0] == \"pwd\"\n my_stack.reject! { |x| x.empty? }\n puts my_stack.size > 0 ? \"/\" + my_stack.join('/') + \"/\" : \"/\"\n elsif command[0] == \"cd\"\n command[1].split(\"/\").each do |step|\n if step != \"..\"\n if step[0] == \"/\"\n my_stack = []\n my_stack.push step[1..-1]\n else\n my_stack.push step\n end\n else\n if my_stack.size > 0\n my_stack.pop\n end\n end\n end\n end\nend\n"}, {"source_code": "def change_path(source, command = Array)\n\tresult = source\n\tfor i in 0..(command.size-1)\n\t\tif (command[i] != \"\")\n\t\t\tif (command[i] == \"..\")\n\t\t\t\tnew_source = source[0..-2]\n\t\t\t\tslash_position = new_source.rindex(\"/\")\n\t\t\t\tresult = result[0..slash_position]\n\t\t\telse\n\t\t\t\tresult = \"#{result}#{command[i].to_s}/\"\n\t\t\tend\n\t\tend\n\tend\n\treturn result\nend\n\npath = \"/\"\nl = STDIN.read.split(\"\\n\")\nn = l[0].to_i\ncommands = Array.new\nfor i in 1..(n)\n\tcommands[i-1] = l[i]\n\tif (l[i] == \"pw\")\n\t\tputs \"#{path}\"\n\telsif (l[i][0,2] == \"cd\")\n\t\tnew_string = l[i]\n\t\tnew_string.slice! \"cd \"\n\t\tnew_command = Array.new\n\t\tnew_command = new_string.split('/')\n\t\tnew_path = change_path(path, new_command)\n\t\tpath = new_path\n\tend\nend"}, {"source_code": "def change_path(source, command = Array)\n\tresult = source\n\tfor i in 0..(command.size-1)\n\t\tif (command[i] != \"\")\n\t\t\tif (command[i] == \"..\")\n\t\t\t\tpathes = Array.new\n\t\t\t\tpathes = source.split('/')\n\t\t\t\tpathes.slice!(-1)\n\t\t\t\tresult = pathes * \"/\"\n\t\t\t\tresult = \"#{result}/\"\n\t\t\telse\n\t\t\t\tresult = \"#{result}#{command[i].to_s}/\"\n\t\t\tend\n\t\tend\n\tend\n\treturn result\nend\n\npath = \"/\"\nl = STDIN.read.split(\"\\n\")\nn = l[0].to_i\ncommands = Array.new\nfor i in 1..(n)\n\tcommands[i-1] = l[i]\n\tif (l[i] == \"pwd\")\n\t\tputs \"#{path}\"\n\telsif (l[i][0,2] == \"cd\")\n\t\tnew_string = l[i]\n\t\tnew_string.slice! \"cd \"\n\t\tnew_command = Array.new\n\t\tnew_command = new_string.split('/')\n\t\tnew_path = change_path(path, new_command)\n\t\tpath = new_path\n\tend\nend"}, {"source_code": "def change_path(source, command = Array)\n\tresult = source\n\tfor i in 0..(command.size-1)\n\t\tif (command[i] != \"\")\n\t\t\tif (command[i] == \"..\")\n\t\t\t\tnew_source = source[0..-2]\n\t\t\t\tslash_position = new_source.rindex(\"/\")\n\t\t\t\tresult = result[0..slash_position]\n\t\t\telse\n\t\t\t\tresult = \"#{result}#{command[i].to_s}/\"\n\t\t\tend\n\t\tend\n\tend\n\treturn result\nend\n\npath = \"/\"\nl = STDIN.read.split(\"\\n\")\nn = l[0].to_i\ncommands = Array.new\nfor i in 1..(n)\n\tcommands[i-1] = l[i]\n\tif (l[i] == \"pwd\")\n\t\tputs \"#{path}\"\n\telsif (l[i][0,2] == \"cd\")\n\t\tnew_string = l[i]\n\t\tnew_string.slice! \"cd \"\n\t\tnew_command = Array.new\n\t\tnew_command = new_string.split('/')\n\t\tnew_path = change_path(path, new_command)\n\t\tpath = new_path\n\tend\nend"}, {"source_code": "n = gets.to_i\n\np = \"/\"\n\nn.times do\n s = gets.chomp\n if s == 'pwd'\n puts p\n else\n s.gsub!(/cd /, '')\n if s[0] == '/'\n p = String.new(s)\n else\n p += s\n end\n p+= '/'\n p.gsub!(/\\w*\\/\\.\\.\\//,'')\n end\nend"}, {"source_code": "n = gets.chomp.to_i\ncurrent_dir = [\"\"]\nn.times do\n inp = gets.chomp.split(' ')\n cmd = inp[0]\n if cmd == 'pwd'\n current_dir.each do |d|\n print \"#{d}/\"\n end\n print \"\\n\"\n else\n args = inp[1]\n if args[0...1] == '/'\n puts 'nonone';\n current_dir = []\n end\n args.split('/').each do |arg|\n if arg == '..'\n current_dir.pop\n else\n current_dir.push arg\n end\n end\n end\nend"}, {"source_code": "class Node\n attr_accessor :element, :parent, :children\n\n def initialize(element, parent)\n @element = element\n @parent = parent\n @children = []\n end\nend\n\nn = gets.strip.to_i\nroot = Node.new(\"/\", nil)\ncurrent_node = root\ncommands = []\nn.times do \n commands.push(gets.strip)\nend\n \ndef print_current_path(current_node)\n path = []\n # p current_node\n if current_node.element == '/'\n path.push('/')\n else\n while !(current_node.element == '/')\n path.unshift(current_node.element)\n current_node = current_node.parent\n path.unshift(\"/\")\n end\n end\n puts path.join\nend\n\nputs \"\\n\"\ncommands.each do |command| \n if command == 'pwd'\n print_current_path(current_node)\n elsif command =~ /cd/\n path = command.split(\" \").last\n if path[0] == '/'\n current_node = root\n path = path[1..-1]\n end\n directories = path.split(\"/\")\n directories.each do |dir|\n if dir == '..'\n current_node = current_node.parent\n else\n new_node = Node.new(dir, current_node)\n if !current_node.children.include?(new_node)\n current_node.children.push(new_node)\n end\n current_node = new_node\n end\n end\n end\nend\n"}, {"source_code": "_ = STDIN.readline\nlines = STDIN.readlines.map(&:chomp)\n\n@path = []\n\ndef pwd\n puts '/' + (@path.size > 0 ? (@path.join('/') + '/') : '')\nend\n\nlines.each do |l|\n if l == 'pwd'\n pwd\n next\n end\n\n _, dir = l.split(' ')\n if dir[0] == '/'\n @path = dir[1..-1].split('/')\n else\n comps = dir.split('/')\n comps.each do |c|\n if c == '..'\n @path.pop\n else\n @path.push(c)\n end\n end\n end\nend\n"}, {"source_code": "_ = STDIN.readline\nlines = STDIN.readlines.map(&:chomp)\n\n@path = []\n\ndef pwd\n puts '/' + (@path.size > 0 ? (@path.join('/') + '/') : '')\nend\n\nlines.each do |l|\n if l == 'pwd'\n pwd\n next\n end\n\n _, dir = l.split(' ')\n if dir[0] == '/'\n dir = dir[1..-1]\n end\n\n comps = dir.split('/')\n comps.each do |c|\n if c == '..'\n @path.pop\n else\n @path.push(c)\n end\n end\nend\n"}, {"source_code": "n = STDIN.readline.to_i\n\nnow = []\nfor i in 1..n do\n line = STDIN.readline.strip\n vc = line.split(/[ \\/]+/)\n len = vc.size\n \n #puts \"R: \" + vc.inspect\n if(vc.first == \"cd\")\n if line[3] == '/'\n now = vc[1..len]\n else\n vc[1..len].each do |cmd|\n if cmd == \"..\"\n now.pop\n else\n now << cmd\n end\n end\n end\n else\n res = \"/\"+ now.join(\"/\")\n res = res + \"/\" if(res != \"/\")\n \n puts res\n end\nend\n"}, {"source_code": "counter = gets.chomp.to_i\ncurrentPath = '/'\n\ncounter.times do\n command = gets.chomp\n command = command.split(' ')\n\n if command[0] == 'pwd'\n if currentPath == '/'\n puts currentPath\n else\n puts currentPath + '/'\n end\n else #cd\n elements = command[1].split('/')\n\n elements.each { |element|\n\n case element\n when ''\n currentPath = ''\n when '..'\n dirs = currentPath.split('/')\n currentPath = ''\n for counter in 1 .. dirs.length-2\n currentPath += '/' + dirs[counter]\n end\n else\n currentPath += '/' + element\n end\n }\n end\n\nend"}, {"source_code": "require 'pp'\ndef s(str)\n split = str.split(\"/\")\n p = []\n for s in split\n if(s == '..')\n p.pop\n else\n p.push(s)\n end\n end\n r = p.join(\"/\")\n if r == ''\n r = '/'\n end\n return r\nend\n\nstr = STDIN.read.split(\"\\n\").drop(1)\n\ncur_path = '/'\nfor s in str\n s.strip!\n next if s == ''\n \n if(s == 'pwd')\n cur_path = s(cur_path)\n puts cur_path + '/' if(cur_path[-1] != '/')\n puts cur_path if cur_path[-1] == '/'\n else\n match = s.match(/cd (.*)/)\n path = match[1]\n if path[0] == '/'\n cur_path = path\n else\n cur_path += '/' + path\n end\n end\nend"}, {"source_code": "require 'pp'\ndef s(str)\n split = str.split(\"/\")\n p = []\n for s in split\n if(s == '..')\n p.pop\n else\n p.push(s)\n end\n end\n r = p.join(\"/\")\n if r == ''\n r = '/'\n end\n return r\nend\n\nstr = STDIN.read.split(\"\\n\").drop(1)\n\ncur_path = '/'\nfor s in str\n s.strip!\n next if s == ''\n \n if(s == 'pwd')\n cur_path = s(cur_path)\n puts cur_path\n else\n match = s.match(/cd (.*)/)\n path = match[1]\n if path[0] == '/'\n cur_path = path\n else\n cur_path += '/' + path\n end\n end\nend"}, {"source_code": "n = gets.to_i\n$cur = []\n\ndef docd(path)\n dirs = path.split('/')\n if path[0] == '/' then cur = [] end\n dirs.each do |dir|\n if dir == '..' then\n $cur.pop \n else\n $cur.push dir\n end\n end\nend\n\nn.times do\n cmd = gets.chomp\n if cmd == 'pwd' then\n puts($cur.join('/') + '/')\n else\n docd(cmd.split(' ')[1])\n end\nend\n"}, {"source_code": "$n = gets.to_i\n$path = []\n\n$n.times do\n cmd = gets.chomp.split ' '\n if cmd[0] == 'pwd' then\n puts $path.join('/') + '/'\n elsif cmd[0] == 'cd' then\n dirs = cmd[1].split '/'\n if cmd[1][0] == '/' then $path = [] end\n dirs.each do |dir|\n if dir == '..' then\n $path.pop\n else\n $path.push dir\n end\n end\n end\nend\n"}, {"source_code": "$n = gets.to_i\n$path = []\n\n$n.times do\n cmd = gets.chomp.split ' '\n if cmd[0] == 'pwd' then\n puts $path.join('/') + '/'\n elsif cmd[0] == 'cd' then\n dirs = cmd[1].split '/'\n dirs.each do |dir|\n if dir == '..' then\n $path.pop\n else\n $path.push dir\n end\n end\n end\nend\n"}, {"source_code": "i = gets.to_i\ncurrent = ''\noutput = \"\"\ni.times do \n cmd, param = gets.chomp.split(' ')\n output << '/' + current + \"\\n\" if cmd == 'pwd'\n\n if cmd == 'cd'\n if param[0] == '/'\n current = param[1..-1]\n else\n param = param.split('/')\n current = current.split('/')\n until param.empty? do \n p = param.shift\n if p == '..'\n current.pop\n else\n current.push(p)\n end\n end\n current = current.join('/')\n end\n end\n\nend\n print output"}, {"source_code": "i = gets.to_i\ncurrent = ''\noutput = \"\"\ni.times do \n cmd, param = gets.chomp.split(' ')\n output << '/' + current + \"\\n\" if cmd == 'pwd'\n\n if cmd == 'cd'\n if param[0] == '/'\n current = param[1..-1]\n else\n param = param.split('/')\n current = current.split('/')\n until param.empty? do \n p = param.shift\n if p == '..'\n current.pop\n else\n current.push(p)\n end\n end\n current = current.join('/')\n end\n current << '/'\n end\n\nend\n print output"}, {"source_code": "i = gets.to_i\ncurrent = ''\noutput = \"\"\ni.times do \n cmd, param = gets.chomp.split(' ')\n output << '/' + current + \"\\n\" if cmd == 'pwd'\n\n if cmd == 'cd'\n if param[0] == '/'\n current = param\n else\n param = param.split('/')\n current = current.split('/')\n until param.empty? do \n p = param.shift\n if p == '..'\n current.pop\n else\n current.push(p)\n end\n end\n current = current.join('/')\n end\n end\n\nend\n print output"}, {"source_code": "i = gets.to_i\ncurrent = ''\noutput = \"\"\ni.times do \n cmd, param = gets.chomp.split(' ')\n output << '/' + current + \"/\\n\" if cmd == 'pwd'\n\n if cmd == 'cd'\n if param[0] == '/'\n current = param[1..-1]\n else\n param = param.split('/')\n current = current.split('/')\n until param.empty? do \n p = param.shift\n if p == '..'\n current.pop\n else\n current.push(p)\n end\n end\n current = current.join('/')\n end\n end\n\nend\n print output"}, {"source_code": "i = gets.to_i\ncurrent = '/'\noutput = \"\"\ni.times do \n cmd, param = gets.chomp.split(' ')\n output << current + \"\\n\" if cmd == 'pwd'\n\n if cmd == 'cd'\n if param[0] == '/'\n current = ''\n param = param[1..-1]\n end\n\n param = param.split('/')\n current = current.split('/')\n until param.empty? do\n p = param.shift\n if p == '..'\n current.pop\n current << '/'\n else\n current.push(p + '/')\n end\n end\n current = current.join.insert(0,'/')\n current.gsub!('//','/')\n end\nend\n\nprint output\n \n"}, {"source_code": "i = gets.to_i\ncurrent = ''\noutput = \"\"\ni.times do \n cmd, param = gets.chomp.split(' ')\n output << '/' + current + \"\\n\" if cmd == 'pwd'\n\n if cmd == 'cd'\n if param[0] == '/'\n current = ''\n param = param[1..-1]\n end\n\n param = param.split('/')\n current = current.split('/')\n until param.empty? do\n p = param.shift\n if p == '..'\n current.pop\n else\n current.push(p)\n end\n end\n current = current.join('/') << '/'\n end\nend\n\nprint output"}, {"source_code": "i = gets.to_i\ncurrent = ''\noutput = \"\"\ni.times do \n cmd, param = gets.chomp.split(' ')\n output << '/' + current + \"\\n\" if cmd == 'pwd'\n\n if cmd == 'cd'\n if param[0] == '/'\n current = ''\n param = param[1..-1]\n end\n\n param = param.split('/')\n current = current.split('/')\n until param.empty? do\n p = param.shift\n if p == '..'\n current.pop\n else\n current.push(p + '/')\n end\n end\n current = current.join\n end\nend\n\nprint output"}, {"source_code": "i = gets.to_i\ncurrent = ''\noutput = \"\"\ni.times do \n cmd, param = gets.chomp.split(' ')\n output << '/' + current + \"\\n\" if cmd == 'pwd'\n\n if cmd == 'cd'\n if param[0] == '/'\n current = param[1..-1]\n else\n param = param.split('/')\n current = current.split('/')\n until param.empty? do \n p = param.shift\n if p == '..'\n current.pop\n else\n current.push(p)\n end\n end\n current = current.join('/') << '/'\n end\n end\n\nend\n print output"}, {"source_code": "i = gets.to_i\ncurrent = '/'\noutput = \"\"\ni.times do \n cmd, param = gets.chomp.split(' ')\n output << current + \"\\n\" if cmd == 'pwd'\n\n if cmd == 'cd'\n if param[0] == '/'\n current = ''\n param = param[1..-1]\n end\n\n param = param.split('/')\n current = current.split('/')\n until param.empty? do\n p = param.shift\n if p == '..'\n current.pop\n current << '/'\n else\n current.push(p + '/')\n end\n end\n current = current.join.insert(0,'/')\n current.gsub!(/(\\/)\\/+/,'/')\n end\nend\n\nprint output"}, {"source_code": "n=gets.chomp.to_i\ncur=[]\nn.times{\n\ts=gets.chomp.to_s\n\tunless s=~/pwd/\n\t\ti=3\n\t\tif s[i]=='/'\n\t\t\tcur=[]\n\t\t\ti+=1\n\t\tend\n\t\twhile true\n\t\t\tif s[i..-1]=~/^([\\.]{2})\\//\n\t\t\t\tcur.pop()\n\t\t\t\ti+=3\n\t\t\t\tnext\n\t\t\tend\n\t\t\tif s[i..-1]=~/^([^\\/.]*)\\//\n\t\t\t\tj=i+$~[1].size\n\t\t\t\tword=s[i...j]\n\t\t\t\tcur.push(word)\n\t\t\t\ti=j+1\n\t\t\t\tnext\n\t\t\tend\n\t\t\tbreak\n\t\tend\n\t\tif s[i..-1]=~/^([^\\/.]*)$/\n\t\t\tcur.push($~[1])\n\t\telsif s[i..-1]=~/^[\\.]{2}$/\n\t\t\tcur.pop()\n\t\tend\n\telse\n\t\tputs (\"/\"+cur.join(\"/\")+'/')\n\tend\n}"}, {"source_code": "n=gets.chomp.to_i\ncur=[]\nn.times{\n\ts=gets.chomp.to_s\n\tunless s=~/pwd/\n\t\ti=3\n\t\tif s[i]=='/'\n\t\t\tcur=[]\n\t\t\ti+=1\n\t\tend\n\t\twhile true\n\t\t\tif s[i..-1]=~/^([\\.]{2})\\//\n\t\t\t\tcur.pop()\n\t\t\t\ti+=3\n\t\t\t\tnext\n\t\t\tend\n\t\t\tif s[i..-1]=~/^([^\\/.]*)\\//\n\t\t\t\tj=i+$~[1].size\n\t\t\t\tword=s[i...j]\n\t\t\t\tcur.push(word)\n\t\t\t\ti=j+1\n\t\t\t\tnext\n\t\t\tend\n\t\t\tbreak\n\t\tend\n\t\tif s[i..-1]=~/^([^\\/.]*)$/\n\t\t\tcur.push($~[1])\n\t\telsif s[i..-1]=~/^[\\.]{2}$/\n\t\t\tcur.pop()\n\t\tend\n\telse\n\t\tputs (\"/\"+cur.join(\"/\"))\n\tend\n}"}, {"source_code": "#!/usr/bin/ruby\nstack = []\nn = STDIN.gets.chomp.to_i\nn.times do\n command = STDIN.gets.chomp\n if command == \"pwd\"\n puts stack.join(\"/\") + \"/\"\n else\n command = command[3..command.length]\n if command.start_with?(\"/\")\n stack.clear\n end\n command.split(/\\//).each do |token|\n if token == \"..\"\n stack.pop\n else\n stack.push(token)\n end\n end\n end\nend\n"}, {"source_code": "n = gets.to_i\ncurrent_directory = []\n\n1.upto(n) do |i|\n command = gets.split(' ')\n\n if command[0] == \"pwd\"\n puts current_directory.empty? ? '/' : current_directory.join('/') + '/'\n else\n path = command[1].split('/')\n if path.length == 0\n current_directory.clear\n else\n path.each { |dir| \n if dir == '..'\n current_directory.pop\n else\n current_directory << dir \n end\n }\n end\n end\n\nend\n"}, {"source_code": "l = STDIN.read.split(\"\\n\")\n\nst = []\ncommands = l[1..-1]\n\ncommands.each{ |c|\n if c.start_with?(\"cd\") then\n parts = c.split(\" \")[1].split(\"/\")\n\n if c.split(\" \")[1].start_with?(\"/\")\n st.clear\n end\n \n parts.each{ |part|\n if part == \"..\" then\n st.pop\n else\n st.push(part)\n end\n }\n end\n \n if c.start_with?(\"pwd\") then\n puts st.join('/') + '/'\n end\n}"}, {"source_code": "l = STDIN.read.split(\"\\n\")\n\nst = []\ncommands = l[1..-1]\n\ncommands.each{|c|\n if c.start_with?(\"cd\") then\n parts = c.split(\" \")[1].split(\"/\")\n\n if c.split(\" \")[1] == \"/\"\n st.clear\n end\n \n parts.each{|part|\n if part == \"..\" then\n st.pop\n else\n st.push(part)\n end\n }\n end\n \n if c.start_with?(\"pwd\") then\n puts (st.count == 1 ? '/' : '') + st.join('/') + '/'\n end\n}"}, {"source_code": "l = STDIN.read.split(\"\\n\")\n\nst = []\ncommands = l[1..-1]\n\ncommands.each{|c|\n if c.start_with?(\"cd\") then\n parts = c.split(\" \")[1].split(\"/\")\n\n if c.split(\" \")[1] == \"/\"\n st.clear\n end\n \n parts.each{|part|\n if part == \"..\" then\n st.pop\n else\n st.push(part)\n end\n }\n end\n \n if c.start_with?(\"pwd\") then\n puts st.join('/') + '/'\n end\n}"}, {"source_code": "l = STDIN.read.split(\"\\n\")\n\nst = []\ncommands = l[1..-1]\n\ncommands.each{|c|\n if c.start_with?(\"cd\") then\n parts = c.split(\" \")[1].split(\"/\")\n \n parts.each{|part|\n if part == \"..\" then\n st.pop\n else\n st.push(part)\n end\n }\n end\n \n if c.start_with?(\"pwd\") then\n puts st.join('/') + '/'\n end\n}"}, {"source_code": "size = gets.chomp.to_i\ncurdir = \"/\"\nsize.times {\n str = gets.chomp\n parts = str.split(\" \")\n command = parts[0]\n if command == \"pwd\" then\n puts curdir\n end\n if command == \"cd\" then\n if parts[1][0] == \"/\" then\n curdir = \"\"\n end\n dirs = parts[1].split(\"/\")\n dirs.each do |x|\n if x == \"..\" \n arr = curdir.split(\"/\")\n arr.pop\n curdir = arr.join(\"/\")\n curdir = curdir + \"/\"\n else\n curdir = curdir + x + \"/\"\n end\n end\n end\n}\nputs curdir"}, {"source_code": "def cd(a,b)\n b=b.split('/')\n if b[0]==\"\" then b.delete_at(0) end\n for i in 0...b.size do\n if b[i]==\"..\"\n a.delete_at(a.size-1)\n elsif\n a << b[i]\n end\n end\n return a\nend\n\ndef pwd(a)\n for i in 0...a.size do\n print \"/#{a[i]}\"\n end\n puts \"/\"\nend\n\na=[]\ngets.to_i.times do |x|\n com=gets.split\n if com[0]==\"cd\"\n a=cd(a,com[1])\n elsif com[0]==\"pwd\"\n pwd(a)\n end\nend\n"}, {"source_code": "n = gets.to_i\nwd = []\nn.times do\n c = gets.chomp\n if c == 'pwd'\n puts \"/#{wd.join(\"/\")}/\"\n else\n c, t = c.split(\" \")\n t = t.split(\"/\")\n if t[0] == ''\n wd = t[1..-1] \n else\n t.each do |x|\n if x == '..'\n wd.pop\n else\n wd.push x\n end\n end\n end\n end\nend\n"}, {"source_code": "n = gets.to_i\nwd = []\nn.times do\n c = gets.chomp\n if c == 'pwd'\n puts wd.size == 0 ? '/' : \"/#{wd.join(\"/\")}/\"\n else\n c, t = c.split(\" \")\n t = t.split(\"/\")\n if t[0] == ''\n wd = t[1..-1] \n else\n t.each do |x|\n if x == '..'\n wd.pop\n else\n wd.push x\n end\n end\n end\n end\nend\n"}, {"source_code": "n = gets.to_i\nwd = []\nn.times do\n c = gets.chomp\n if c == 'pwd'\n puts wd.size == 0 ? '/' : \"/#{wd.join(\"/\")}/\"\n else\n c, t = c.split(\" \")\n t = t.split(\"/\")\n t = t[1..-1] if t[0] == ''\n t.each do |x|\n if x == '..'\n wd.pop\n else\n wd.push x\n end\n end\n end\nend\n"}, {"source_code": "n = gets.to_i\nwd = []\nn.times do\n c = gets.chomp\n if c == 'pwd'\n puts \"/#{wd.join(\"/\")}\"\n else\n c, t = c.split(\" \")\n t = t.split(\"/\")\n if t[0] == ''\n wd = t[1..-1] \n else\n t.each do |x|\n if x == '..'\n wd.pop\n else\n wd.push x\n end\n end\n end\n end\nend\n"}, {"source_code": "n = gets.chomp.to_i\n\ndir = []\nn.times{\n cmd = gets.split\n if cmd[0] == 'pwd'\n if dir == []\n puts '/'\n else\n puts ['/', dir.join('/'), '/'].join\n end\n next\n end\n \n if cmd[1][0].to_s == '/'\n dir = cmd[1][1..-1].split('/')\n next\n end\n \n cmd[1].split('/').each{|t|\n if t == '..'\n dir = dir[0..-2]\n else\n dir << t\n end\n }\n}"}, {"source_code": "n = gets.chomp.to_i\n\ndir = []\nn.times{\n cmd = gets.split\n if cmd[0] == 'pwd'\n if dir == []\n puts '/'\n else\n puts [dir.join('/'), '/'].join\n end\n next\n end\n \n if cmd[1][0] == 47\n dir = cmd[1][1..-1].split('/')\n next\n end\n \n cmd[1].split('/').each{|t|\n if t == '..'\n dir = dir[0..-2]\n else\n dir << t\n end\n }\n}"}, {"source_code": "n = gets.chomp.to_i\n\ndir = []\nn.times{\n cmd = gets.split\n if cmd[0] == 'pwd'\n if dir == []\n puts '/'\n else\n puts ['/', dir.join('/'), '/'].join\n end\n next\n end\n \n if cmd[1][0] == 47\n dir = cmd[1][1..-1].split('/')\n next\n end\n \n cmd[1].split('/').each{|t|\n if t == '..'\n dir = dir[0..-2]\n else\n dir << t\n end\n }\n}"}, {"source_code": "n=gets.to_i\n\npwd=\"\"\n\nn.times{\n\ns=gets.chomp\nif s==\"pwd\"\nif pwd==\"\"\nprint '/'\nend\nprint pwd\nprint \"\\n\"\nelse\nfor i in s.split(' ')[1].split('/')\n\nif i==\"\"\npwd=\"\"\nelsif i==\"..\"\npwd=pwd.split('/')\npwd.pop\npwd=pwd.join('/')\nelse\npwd+='/'+i\nend\n\nend\nend\n\n}"}, {"source_code": "n=gets.to_i\n\npwd=\"/\"\n\nn.times{\n\ns=gets.chomp\nif s==\"pwd\"\nprint pwd\nprint \"\\n\"\nelse\nfor i in s.split(' ')[1].split('/')\n\nif i==\"\"\npwd=\"/\"\nelsif i==\"..\"\npwd=pwd.split('/')\npwd.pop\npwd=pwd.join('/')\nelse\npwd+='/'+i\nend\n\nend\nend\n\n}"}, {"source_code": "n=gets.to_i\n\npwd=\"/\"\n\nn.times{\n\ns=gets.chomp\nif s==\"pwd\"\nprint pwd\nprint \"\\n\"\nelse\nfor i in s.split(' ')[1].split('/')\n\nif i==\"\"\npwd=\"/\"\nelsif i==\"..\"\npwd=pwd.split('/')\npwd.pop\npwd=pwd.join('/')\nelse\npwd+=i+'/'\nend\n\nend\nend\n\n}"}, {"source_code": "n=gets.to_i\n\npwd=\"/\"\n\nn.times{\n\ns=gets.chomp\nif s==\"pwd\"\nprint pwd\nprint \"\\n\"\nelse\nfor i in s.split(' ')[1].split('/')\n\nif i==\"\"\npwd=\"/\"\nelsif i==\"..\"\npwd=pwd.split('/')\npwd.pop\npwd.pop\npwd.push \"\"\npwd=pwd.join('/')\nelse\npwd+=i+'/'\nend\n\nend\nend\n\n}"}, {"source_code": "def pwd(path)\n if path.empty?\n puts \"/\"\n else\n puts \"/#{path.join '/'}/\"\n end\nend\n\npath = []\nn = gets.to_i\n\nfor str in STDIN.each_line.take(n)\n arr = str.chomp.split(' ')\n\n if arr.size == 1\n pwd path\n else\n p = arr[1].split('/').reject &:empty?\n p.each { |t| (t == '..') ? path.pop : path.push(t) }\n end\nend"}, {"source_code": "def pwd(path)\n puts path.empty? ? \"/\" : \"#{path.join '/'}/\"\nend\n\npath = []\nn = gets.to_i\n\nfor str in STDIN.each_line.take(n)\n arr = str.chomp.split(' ')\n\n if arr.size == 1\n pwd path\n else\n p = arr[1].split('/')\n p.each { |t| (t == '..') ? path.pop : path.push(t) }\n end\nend"}, {"source_code": "# public: Simulate directory\nclass Directory\n def initialize\n\t@dir = []\n end\n \n def pwd\n\tputs to_dir\n end\n \n def to_dir\n dir_string = \"/\" << @dir.join(\"/\")\n\tif dir_string[-1] == \"/\"\n\t return dir_string\n\telse\n\t return dir_string << \"/\"\n\tend\n end\n \n def cd(dir)\n\tstack = dir.split(\"/\")\n\tif stack[0] == \"\"\n\t absolute_dir(stack)\n\telse\n\t relative_dir(stack)\n\tend\n end\n \n def absolute_dir(stack)\n\tstack.shift\n\t@dir = stack\n end\n \n def relative_dir(stack)\n stack.each do |dir|\n\t if dir == \"..\"\n\t @dir.pop\n\t else\n\t @dir.push(dir)\n\t end\n\tend\n end\nend\n\ndir = Directory.new\ngets.to_i.times do\n dir.send(*gets.chomp.split)\nend"}, {"source_code": "n = gets\nn = n.to_i\ncommands = Array.new\nwhile n != 0\n commands << gets\n n -= 1\nend\n@currentpwd = Array.new\n\ncommands.each do |command|\n # puts \"doing command #{command}\"\n command = command.gsub(/\\n/,\"\")\n if command == 'pwd'\n if @currentpwd.any?\n p = \"\"\n @currentpwd.each do |path|\n p += \"/\"\n p += path\n end\n puts p\n else\n puts '/'\n end\n else\n precommand = command.split(\" \")\n precommand = precommand[1].split(\"/\")\n # puts \"splitting #{precommand}\"\n precommand.each do |dir|\n if dir == \"\"\n @currentpwd = Array.new\n else\n if dir == \"..\"\n if @currentpwd.any?\n @currentpwd = @currentpwd.slice(0, @currentpwd.length-1)\n end\n # puts \"her #{@currentpwd}\"\n else\n @currentpwd << dir\n end\n end\n end\n # puts \"current dir #{@currentpwd}\"\n end\nend\n\n# p @currentpwd"}, {"source_code": "n = gets\nn = n.to_i\ncommands = Array.new\nwhile n != 0\n commands << gets\n n -= 1\nend\n@currentpwd = Array.new\n\ncommands.each do |command|\n command = command.gsub(/\\n/,\"\")\n if command == 'pwd'\n if @currentpwd.any?\n p = \"\"\n @currentpwd.each do |path|\n p += \"/\"\n p += path\n end\n puts p\n else\n puts '/'\n end\n else\n precommand = command.split(\" \")\n precommand = precommand[1].split(\"/\")\n precommand.each do |dir|\n if dir == \"\"\n @currentpwd = Array.new\n else\n if dir == \"..\"\n @currentpwd = @currentpwd.slice(0, @currentpwd.length-1)\n else\n @currentpwd << dir\n end\n end\n end\n end\nend"}], "src_uid": "494ac937ba939db1dbc4081e518ab54c"} {"nl": {"description": "You are given a string $$$s$$$, consisting of $$$n$$$ letters, each letter is either 'a' or 'b'. The letters in the string are numbered from $$$1$$$ to $$$n$$$.$$$s[l; r]$$$ is a continuous substring of letters from index $$$l$$$ to $$$r$$$ of the string inclusive. A string is called balanced if the number of letters 'a' in it is equal to the number of letters 'b'. For example, strings \"baba\" and \"aabbab\" are balanced and strings \"aaab\" and \"b\" are not.Find any non-empty balanced substring $$$s[l; r]$$$ of string $$$s$$$. Print its $$$l$$$ and $$$r$$$ ($$$1 \\le l \\le r \\le n$$$). If there is no such substring, then print $$$-1$$$ $$$-1$$$.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of testcases. Then the descriptions of $$$t$$$ testcases follow. The first line of the testcase contains a single integer $$$n$$$ ($$$1 \\le n \\le 50$$$) — the length of the string. The second line of the testcase contains a string $$$s$$$, consisting of $$$n$$$ letters, each letter is either 'a' or 'b'.", "output_spec": "For each testcase print two integers. If there exists a non-empty balanced substring $$$s[l; r]$$$, then print $$$l$$$ $$$r$$$ ($$$1 \\le l \\le r \\le n$$$). Otherwise, print $$$-1$$$ $$$-1$$$.", "sample_inputs": ["4\n1\na\n6\nabbaba\n6\nabbaba\n9\nbabbabbaa"], "sample_outputs": ["-1 -1\n1 6\n3 6\n2 5"], "notes": "NoteIn the first testcase there are no non-empty balanced subtrings.In the second and third testcases there are multiple balanced substrings, including the entire string \"abbaba\" and substring \"baba\"."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n\r\nn = gets.to_i\r\ns = gets.chomp.chars.map! { |c| c == 'a' ? 0 : 1 }\r\n\r\ni = s.each_cons(2).find_index { |c1, c2| c1 != c2 }\r\nif i\r\n puts \"#{i+1} #{i+2}\"\r\nelse\r\n puts \"-1 -1\"\r\nend\r\n\r\nend\r\n"}], "negative_code": [], "src_uid": "127d7f23a0ac8fcecccd687565d6f35a"} {"nl": {"description": "For the first place at the competition, Alex won many arrays of integers and was assured that these arrays are very expensive. After the award ceremony Alex decided to sell them. There is a rule in arrays pawnshop: you can sell array only if it can be compressed to a generator.This generator takes four non-negative numbers $$$n$$$, $$$m$$$, $$$c$$$, $$$s$$$. $$$n$$$ and $$$m$$$ must be positive, $$$s$$$ non-negative and for $$$c$$$ it must be true that $$$0 \\leq c < m$$$. The array $$$a$$$ of length $$$n$$$ is created according to the following rules: $$$a_1 = s \\bmod m$$$, here $$$x \\bmod y$$$ denotes remainder of the division of $$$x$$$ by $$$y$$$; $$$a_i = (a_{i-1} + c) \\bmod m$$$ for all $$$i$$$ such that $$$1 < i \\le n$$$. For example, if $$$n = 5$$$, $$$m = 7$$$, $$$c = 4$$$, and $$$s = 10$$$, then $$$a = [3, 0, 4, 1, 5]$$$.Price of such an array is the value of $$$m$$$ in this generator.Alex has a question: how much money he can get for each of the arrays. Please, help him to understand for every array whether there exist four numbers $$$n$$$, $$$m$$$, $$$c$$$, $$$s$$$ that generate this array. If yes, then maximize $$$m$$$.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 10^5$$$) — the number of arrays. The first line of array description contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 10^5$$$) — the size of this array. The second line of array description contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$0 \\leq a_i \\leq 10^9$$$ ) — elements of the array. It is guaranteed that the sum of array sizes does not exceed $$$10^5$$$.", "output_spec": "For every array print: $$$-1$$$, if there are no such four numbers that generate this array; $$$0$$$, if $$$m$$$ can be arbitrary large; the maximum value $$$m$$$ and any appropriate $$$c$$$ ($$$0 \\leq c < m$$$) in other cases. ", "sample_inputs": ["6\n6\n1 9 17 6 14 3\n3\n4 2 2\n3\n7 3 4\n3\n2 2 4\n5\n0 1000000000 0 1000000000 0\n2\n1 1"], "sample_outputs": ["19 8\n-1\n-1\n-1\n2000000000 1000000000\n0"], "notes": null}, "positive_code": [{"source_code": "t = gets.chomp.to_i\r\nt.times do\r\n\tn = gets.chomp.to_i\r\n\ta = gets.split()\r\n\r\n\tif n == 1 then\r\n\t\tp 0\r\n\t\tnext\r\n\tend\r\n\r\n\ta.map!(&:to_i)\r\n\r\n\tc = a[1] - a[0]\r\n\t\r\n\tm = -1\r\n\tok = true\r\n\r\n\tmax_value = a.max\r\n\r\n\tif c >= 0 then\r\n\t\tfor i in 1...n do\r\n\t\t\tif a[i] < a[i-1] then\r\n\t\t\t\tnew_m = a[i-1] + (c - a[i])\r\n\t\t\t\tok &= m == -1 || new_m == m\r\n\t\t\t\tm = new_m\r\n\t\t\telse\r\n\t\t\t\tok &= c == a[i] - a[i-1]\r\n\t\t\tend\r\n\t\tend\r\n\telse\r\n\t\tc *= - 1\r\n\r\n\t\tfor i in 1...n do\r\n\t\t\tif a[i] > a[i-1] then\r\n\t\t\t\tnew_m = a[i] + (c - a[i-1])\r\n\t\t\t\tok &= m == -1 || new_m == m \r\n\t\t\t\tm = new_m\r\n\t\t\telse\r\n\t\t\t\tok &= c == a[i-1] - a[i]\r\n\t\t\tend\r\n\t\tend\r\n\t\tc = m - c\r\n\tend\r\n\r\n\tok &= m == -1 || max_value < m\r\n\r\n\tputs ok ? (m == -1 ? 0 : \"#{m} #{c}\") : -1\r\n\r\nend\r\n"}, {"source_code": "require 'set'\n\ngets.to_i.times do\n n = gets.to_i\n arr = gets.split.map(&:to_i)\n diff = (1..n-1).map{|i| arr[i] - arr[i-1]}.to_set\n if diff.size <= 1\n puts 0\n next\n elsif diff.size > 2\n puts -1\n next\n end\n a, b = diff.sort\n if a * b > 0\n puts -1\n next\n end\n m = b - a\n seq = [arr[0] % m]\n for i in 1..n-1\n seq << (seq[-1] + b) % m\n end\n if seq == arr\n puts \"#{m} #{b}\"\n else\n puts -1\n end\nend\n"}], "negative_code": [{"source_code": "t = gets.chomp.to_i\r\nt.times do\r\n\tn = gets.chomp.to_i\r\n\ta = gets.split()\r\n\r\n\ta.map!(&:to_i)\r\n\r\n\tc = a[1] - a[0]\r\n\t\r\n\tm = -1\r\n\tok = true\r\n\r\n\tmax_value = a.max\r\n\r\n\tif c >= 0 then\r\n\t\tfor i in 1...n do\r\n\t\t\tif a[i] < a[i-1] then\r\n\t\t\t\tnew_m = a[i-1] + (c - a[i])\r\n\t\t\t\tok &= m == -1 || new_m == m\r\n\t\t\t\tm = new_m\r\n\t\t\telse\r\n\t\t\t\tok &= c == a[i] - a[i-1]\r\n\t\t\tend\r\n\t\tend\r\n\telse\r\n\t\tc *= - 1\r\n\r\n\t\tfor i in 1...n do\r\n\t\t\tif a[i] > a[i-1] then\r\n\t\t\t\tnew_m = a[i] + (c - a[i-1])\r\n\t\t\t\tok &= m == -1 || new_m == m \r\n\t\t\t\tm = new_m\r\n\t\t\telse\r\n\t\t\t\tok &= c == a[i-1] - a[i]\r\n\t\t\tend\r\n\t\tend\r\n\tend\r\n\r\n\tok &= m == -1 || max_value < m\r\n\r\n\tputs ok ? (m == -1 ? 0 : \"#{m} #{c}\") : -1\r\n\r\nend\r\n"}], "src_uid": "d6721fb3dd02535fc08fc69a4811d60c"} {"nl": {"description": "Let us remind you part of the rules of Codeforces. The given rules slightly simplified, use the problem statement as a formal document.In the beginning of the round the contestants are divided into rooms. Each room contains exactly n participants. During the contest the participants are suggested to solve five problems, A, B, C, D and E. For each of these problem, depending on when the given problem was solved and whether it was solved at all, the participants receive some points. Besides, a contestant can perform hacks on other contestants. For each successful hack a contestant earns 100 points, for each unsuccessful hack a contestant loses 50 points. The number of points for every contestant is represented by the sum of points he has received from all his problems, including hacks.You are suggested to determine the leader for some room; the leader is a participant who has maximum points.", "input_spec": "The first line contains an integer n, which is the number of contestants in the room (1 ≤ n ≤ 50). The next n lines contain the participants of a given room. The i-th line has the format of \"handlei plusi minusi ai bi ci di ei\" — it is the handle of a contestant, the number of successful hacks, the number of unsuccessful hacks and the number of points he has received from problems A, B, C, D, E correspondingly. The handle of each participant consists of Latin letters, digits and underscores and has the length from 1 to 20 characters. There are the following limitations imposed upon the numbers: 0 ≤ plusi, minusi ≤ 50; 150 ≤ ai ≤ 500 or ai = 0, if problem A is not solved; 300 ≤ bi ≤ 1000 or bi = 0, if problem B is not solved; 450 ≤ ci ≤ 1500 or ci = 0, if problem C is not solved; 600 ≤ di ≤ 2000 or di = 0, if problem D is not solved; 750 ≤ ei ≤ 2500 or ei = 0, if problem E is not solved. All the numbers are integer. All the participants have different handles. It is guaranteed that there is exactly one leader in the room (i.e. there are no two participants with the maximal number of points).", "output_spec": "Print on the single line the handle of the room leader.", "sample_inputs": ["5\nPetr 3 1 490 920 1000 1200 0\ntourist 2 0 490 950 1100 1400 0\nEgor 7 0 480 900 950 0 1000\nc00lH4x0R 0 10 150 0 0 0 0\nsome_participant 2 1 450 720 900 0 0"], "sample_outputs": ["tourist"], "notes": "NoteThe number of points that each participant from the example earns, are as follows: Petr — 3860 tourist — 4140 Egor — 4030 c00lH4x0R —  - 350 some_participant — 2220 Thus, the leader of the room is tourist."}, "positive_code": [{"source_code": "n=gets.to_i\nt=[]\nn.times{\n\tm,*v=gets.split\n\tv=v.map(&:to_i)\n\tt<<[v[0]*100-v[1]*50+v[2..-1].inject(:+),m]\n}\nputs t.max[1]\n"}, {"source_code": "puts gets.to_i.times.map{\n\ta = gets.split\n\tb = a[1..-1].map(&:to_i)\n\t[b[0] * 100 - b[1] * 50 + b[2..-1].inject(&:+), a[0]]\n}.sort[-1][1]\n"}, {"source_code": "class P\n attr_accessor :name, :result\n def initialize(name, plus, minus, a, b, c, d, e)\n @name = name\n @result = plus.to_i * 100 - minus.to_i * 50 \\\n + a.to_i + b.to_i + c.to_i + d.to_i + e.to_i\n end\nend\n\nn = gets.to_i\nh = []\nn.times do\n s = gets.chomp.split\n h.push(P.new(s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7]))\nend\nh.sort! {|a,b| b.result <=> a.result}\nputs h[0].name\n"}, {"source_code": "question = gets.to_i\nplayer = []\nquestion.times do\n tmp = gets.split(\" \")\n name = tmp.shift\n tmp.map!{|item| item.to_i}\n tmp[0] *= 100\n tmp[1] = -tmp[1] * 50\n point= tmp.inject(0){|r,item| r+=item}\n player << [name,point]\nend\nmax = player.shift\nplayer.each do |item|\n max = item if max[1] < item[1]\nend\nputs max[0]\n \n "}, {"source_code": "gets;puts readlines.map(&:chomp).map(&:split).map! {|x| [x[0]] + x[1..-1].map!(&:to_i)}.map! { |x| [x[0], x[1]*100-x[2]*50+x[3]+x[4]+x[5]+x[6]+x[7]] }.max_by { |x| x[1]}[0]"}, {"source_code": "n = gets.to_i\n\narray = []\npo = -9999999\nname = \"\"\n\nn.times{\n pt = 0\n array = gets.split()\n pt += array[1].to_i * 100\n pt -= array[2].to_i * 50\n pt += array[3].to_i\n pt += array[4].to_i\n pt += array[5].to_i\n pt += array[6].to_i\n pt += array[7].to_i\n if pt > po\n name = array[0]\n po = pt\n end\n}\n\nputs name\n"}, {"source_code": "n=gets.to_i\n\nd = {}\nn.times{\n\ta, p, m, *t = gets.split\n\t\n\td[a] = t.map!(&:to_i).inject(:+) + p.to_i*100 - m.to_i*50\n}\n\nd=d.invert\nputs d[d.keys.max]\n"}, {"source_code": "an=[] \ngets.to_i.times{ name,*m=gets.split\n m=m.map(&:to_i) \n an<<[m[0]*100-m[1]*50+m[2]+m[3]+m[4]+m[5]+m[6],name]\n }\nputs an.max[1]"}, {"source_code": "n = gets.to_i\nans = \"\"\nscore = -(1<<30)\nn.times do\n tmp = gets.split\n (1..7).each{|i| tmp[i] = tmp[i].to_i}\n tmpscore = tmp[1]*100-tmp[2]*50+tmp[3]+tmp[4]+tmp[5]+tmp[6]+tmp[7]\n if tmpscore > score\n score = tmpscore\n ans = tmp[0]\n end\nend\nputs ans\n"}, {"source_code": "n = STDIN.readline.to_i();\ntop_s = -100000000000000;\ntop_h = \"\";\nn.times do |i|\n h, p, m, a, b, c, d, e = STDIN.readline.split;\n s = p.to_i()*100 - m.to_i()*50 + a.to_i() + b.to_i() + c.to_i() + d.to_i() + e.to_i();\n if top_s <= s then\n top_s = s\n top_h = h\n end\nend\nprint top_h, \"\\n\""}, {"source_code": "n = gets.to_i\n\na = []\nn.times do\n l = gets\n name, *data = l.split\n data = data.map(&:to_i)\n score = 100*data[0] - 50*data[1] + data[2..-1].inject(0){|r,j|r+j}\n a << [score, name]\nend\n\nputs a.max[1]\n"}, {"source_code": "n = gets.to_i\nmax = -2_501\nleader = nil\nfor _ in 1..n\n v = gets.split\n p, m, a, b, c, d, e = v[1..-1].map { |x| x.to_i }\n score = 100 * p - 50 * m + a + b + c + d + e\n if score > max\n max = score\n leader = v[0]\n end\nend\nputs leader"}, {"source_code": "h = Hash.new(0)\n\ngets; while g = gets\n\tc = g.split\n\tc[1..-1] = c[1..-1].map &:to_i\n\tc[1] *= 100\n\tc[2] *= 50\n\tc[2] = -c[2]\n\th[c[0].to_sym] = c[1..-1].inject(0) { |sum, i| sum += i }\nend\n\nmax = h.values.max\nh.each { |k ,v| (print k; exit) if v == max}"}, {"source_code": "winner = nil\nmax = -1000000\ngets.chomp.to_i.times do \n name, s, us, a, b, c, d, e = gets.chomp.split\n point = s.to_i * 100 - us.to_i * 50 + a.to_i + b.to_i + c.to_i + d.to_i + e.to_i\n if point > max\n max = point\n winner = name\n end\nend\nputs winner\n"}], "negative_code": [{"source_code": "class P\n attr_accessor :name, :result\n def initialize(name, plus, minus, a, b, c, d)\n @name = name\n @result = plus.to_i * 100 - minus.to_i * 50 \\\n + a.to_i + b.to_i + c.to_i + d.to_i\n end\nend\n\nn = gets.to_i\nh = []\nn.times do\n s = gets.chomp.split\n h.push(P.new(s[0], s[1], s[2], s[3], s[4], s[5], s[6]))\nend\nh.sort! {|a,b| b.result <=> a.result}\nputs h[0].name\n"}, {"source_code": "n = gets.to_i\nans = \"\"\nscore = 0\nn.times do\n tmp = gets.split\n (1..7).each{|i| tmp[i] = tmp[i].to_i}\n tmpscore = tmp[1]*100-tmp[2]*50+tmp[3]+tmp[4]+tmp[5]+tmp[6]+tmp[7]\n if tmpscore > score\n score = tmpscore\n ans = tmp[0]\n end\nend\nputs ans\n"}, {"source_code": "n = STDIN.readline.to_i();\ntop_s = 0;\ntop_h = \"\";\nn.times do |i|\n h, p, m, a, b, c, d, e = STDIN.readline.split;\n s = p.to_i()*100 - m.to_i()*50 + a.to_i() + b.to_i() + c.to_i() + d.to_i() + e.to_i();\n if top_s <= s then\n top_s = s\n top_h = h\n end\nend\nprint top_h, \"\\n\""}, {"source_code": "n = STDIN.readline.to_i();\ntop_p = 0;\ntop_h = \"\";\nt = Array.new()\nn.times do |i|\n h, p, m, a, b, c, d, e = STDIN.readline.split;\n s = p.to_i()*100 - m.to_i()*50 + a.to_i() + b.to_i() + c.to_i() + d.to_i() + e.to_i(); \n if top_p < s then\n top_p = s\n top_h = h\n end\nend\nprint top_h, \"\\n\""}, {"source_code": "h = Hash.new(0)\n\ngets; while g = gets\n\tc = g.split\n\tc[1..-1] = c[1..-1].map! &:to_i\n\tc[1] *= 100\n\tc[2] *= 50\n\tc[2] = -c[2]\n\th[c[0].to_sym] = c[1..-1].inject(0) { |sum, i| sum += i }\nend\n\nprint h.max.first"}, {"source_code": "winner = nil\nmax = -1\ngets.chomp.to_i.times do \n name, s, us, a, b, c, d, e = gets.chomp.split\n point = s.to_i * 100 - us.to_i * 50 + a.to_i + b.to_i + c.to_i + d.to_i + e.to_i\n if point > max\n max = point\n winner = name\n end\nend\nputs winner\n"}, {"source_code": "winner = nil\nmax = -1\nn = gets.to_i\nn.times do \n name, s, us, a, b, c, d, e = gets.strip.split\n point = s.to_i * 100 - us.to_i * 50 + a.to_i + b.to_i + c.to_i + d.to_i + e.to_i\n if point > max\n max = point\n winner = name\n end\nend\nputs winner\n"}, {"source_code": "winner = nil\nmax = -1\ngets.to_i.times do \n name, s, us, a, b, c, d, e = gets.strip.split\n point = s.to_i * 100 - us.to_i * 50 + a.to_i + b.to_i + c.to_i + d.to_i + e.to_i\n if point > max\n max = point\n winner = name\n end\nend\nputs winner\n"}, {"source_code": "winner = nil\nmax = -1\nn = gets.to_i\nn.times do \n name, s, us, a, b, c, d, e = gets.chomp.split\n point = s.to_i * 100 - us.to_i * 50 + a.to_i + b.to_i + c.to_i + d.to_i + e.to_i\n if point > max\n max = point\n winner = name\n end\nend\nputs winner\n"}], "src_uid": "b9dacff0cab78595296d697d22dce5d9"} {"nl": {"description": "The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries.", "input_spec": "The first line of the input contains one integer $$$q$$$ ($$$1 \\le q \\le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \\le n \\le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \\dots, p_n$$$ ($$$1 \\le p_i \\le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid.", "output_spec": "For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query.", "sample_inputs": ["6\n5\n1 2 3 4 5\n3\n2 3 1\n6\n4 6 2 1 5 3\n1\n1\n4\n3 4 1 2\n5\n5 1 2 4 3"], "sample_outputs": ["1 1 1 1 1 \n3 3 3 \n2 3 3 2 1 3 \n1 \n2 2 2 2 \n4 4 4 1 4"], "notes": null}, "positive_code": [{"source_code": "def calcs(x,y,ax,ay,bx,by) ((ax-x)*(by-y)-(bx-x)*(ay-y))/2.0 end\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaizyo(n)(n < 2)? 1 : (2..n).inject(:*) end\n\ndef scount(a) b=na(a.max+1);a.each{|n|b[n]+=1};return b end\n\ndef na(n=0,d=0) Array.new(n,d)end\n\ndef na2(n=0,m=0,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n=0,m=0,l=0,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef bit(n) n.to_s(2).split(\"\").map(&:to_i) end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef index (a, ind, start, d)\n\n if(ind == start)\n return d\n else\n ind = a[ind]-1\n return index(a, ind , start, d+1)\n end\nend\n\nn = inp[0]\nn.times do\n q = inp[0]\n st = inp\n mae = -9\n ans = []\n st.each.with_index do |d,i|\n ans << index(st,d-1,i,1)\n end\n puts ans.join(\" \")\n\nend"}, {"source_code": "def calcs(x,y,ax,ay,bx,by) ((ax-x)*(by-y)-(bx-x)*(ay-y))/2.0 end\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaizyo(n)(n < 2)? 1 : (2..n).inject(:*) end\n\ndef scount(a) b=na(a.max+1);a.each{|n|b[n]+=1};return b end\n\ndef na(n=0,d=0) Array.new(n,d)end\n\ndef na2(n=0,m=0,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n=0,m=0,l=0,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef bit(n) n.to_s(2).split(\"\").map(&:to_i) end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef index (a, ind, start, d, arrow)\n stack = [[ind, d]]\n ans = 0\n while true\n ind, d = stack.pop\n if(ind == start)\n ans = d\n break\n end\n ind = a[ind]-1\n arrow[ind] = start\n stack.push([ind , d+1])\n end\n return ans\nend\n\nn = inp[0]\nn.times do\n q = inp[0]\n st = inp\n mae = -9\n ans = na(q,false)\n arrow = na(q,false)\n st.each.with_index do |d,i|\n if(arrow[i])\n ans[i] = ans[arrow[i]]\n else\n ans[i] = index(st,d-1,i,1,arrow)\n end\n end\n puts ans.join(\" \")\n\nend"}], "negative_code": [], "src_uid": "345e76bf67ae4342e850ab248211eb0b"} {"nl": {"description": "During a daily walk Alina noticed a long number written on the ground. Now Alina wants to find some positive number of same length without leading zeroes, such that the sum of these two numbers is a palindrome. Recall that a number is called a palindrome, if it reads the same right to left and left to right. For example, numbers $$$121, 66, 98989$$$ are palindromes, and $$$103, 239, 1241$$$ are not palindromes.Alina understands that a valid number always exist. Help her find one!", "input_spec": "The first line of input data contains an integer $$$t$$$ ($$$1 \\leq t \\leq 100$$$) — the number of test cases. Next, descriptions of $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 \\leq n \\leq 100\\,000$$$) — the length of the number that is written on the ground. The second line of contains the positive $$$n$$$-digit integer without leading zeroes — the number itself. It is guaranteed that the sum of the values $$$n$$$ over all test cases does not exceed $$$100\\,000$$$.", "output_spec": "For each of $$$t$$$ test cases print an answer — a positive $$$n$$$-digit integer without leading zeros, such that the sum of the input integer and this number is a palindrome. We can show that at least one number satisfying the constraints exists. If there are multiple solutions, you can output any of them.", "sample_inputs": ["3\n2\n99\n4\n1023\n3\n385"], "sample_outputs": ["32\n8646\n604"], "notes": "NoteIn the first test case $$$99 + 32 = 131$$$ is a palindrome. Note that another answer is $$$12$$$, because $$$99 + 12 = 111$$$ is also a palindrome.In the second test case $$$1023 + 8646 = 9669$$$.In the third test case $$$385 + 604 = 989$$$."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n x = gets\r\n case\r\n when x[0] == '9'\r\n puts(('1'*(n + 1)).to_i - x.to_i)\r\n else\r\n puts(('9'*n).to_i - x.to_i)\r\n end\r\nend"}, {"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n x = gets\r\n x[0] == '9' ? puts(('1'*(n + 1)).to_i - x.to_i) : puts(('9'*n).to_i - x.to_i)\r\nend\r\n"}, {"source_code": "t = gets.chomp.to_i\r\nfor i in 1..t\r\n n = gets.chomp.to_i\r\n x = gets.chomp\r\n if x[0] != '9'\r\n w = \"\"\r\n for j in 0..(n - 1)\r\n w.concat((9 - x[j].to_i).to_s)\r\n end\r\n puts(w)\r\n else\r\n x.reverse!\r\n w = \"\"\r\n c = 0\r\n for j in 0..(n - 1)\r\n c += x[j].to_i\r\n if (c <= 1) \r\n w.concat((1 - c).to_s)\r\n c = 0\r\n else\r\n w.concat((11 - c).to_s)\r\n c = 1\r\n end\r\n end\r\n w.reverse!\r\n puts(w)\r\n end\r\nend"}, {"source_code": "t = gets.chomp.to_i\n\nwhile t > 0\n t -= 1\n n = gets.chomp.to_i\n a = gets.chomp\n\n\n res = if a.length == 1\n 11 - a.to_i\n elsif a[0] == '9'\n # a + res = final\n # res = final - a\n res = \"0\" * a.length\n final = \"1\" * (a.length + 1)\n\n (0..a.length - 1).reverse_each do |i|\n nu = final[i].to_i\n de = a[i].to_i\n d = nu - de\n if d < 0\n d += 10\n final[i - 1] = '0'\n end\n res[i] = d.to_s\n end\n res\n else\n a.chars.map { |c| (9 - c.to_i).to_s }.join(\"\")\n end\n\n puts res\nend\n\n# puts inp.length\n# puts inp[0]\n# puts inp[1]\n# puts inp\n2\n3\n123\n2\n99"}, {"source_code": "gets.to_i.times do\n n = gets.to_i\n s = gets.to_i\n v = ('9' * n).to_i - s\n v = ('1' * (n + 1)).to_i - s if v.to_s.size != n\n puts v\nend\n"}], "negative_code": [{"source_code": "t = gets.chomp.to_i\r\nfor i in 1..t\r\n n = gets.chomp.to_i\r\n x = gets.chomp\r\n if x[0] != '9'\r\n w = \"\"\r\n for j in 0..(n - 1)\r\n w.concat((9 - x[j].to_i).to_s)\r\n end\r\n puts(w)\r\n else\r\n w = \"\"\r\n for j in 0..(n - 2)\r\n w.concat((10 - x[j].to_i).to_s)\r\n end\r\n w.concat((11 - x[j].to_i).to_s)\r\n puts(w)\r\n end\r\nend"}, {"source_code": "t = gets.chomp.to_i\r\nfor i in 1..t\r\n n = gets.chomp.to_i\r\n x = gets.chomp\r\n if x[0] != '9'\r\n w = \"\"\r\n for j in 0..(n - 1)\r\n print((9 - x[j].to_i).to_s)\r\n end\r\n puts()\r\n else\r\n w = \"\"\r\n for j in 0..(n - 2)\r\n print((10 - x[j].to_i).to_s)\r\n end\r\n puts((11 - x[j].to_i).to_s)\r\n end\r\nend"}], "src_uid": "d3c3bc95605010040e2018e465eb9fab"} {"nl": {"description": "A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1\\cdot 2\\cdot \\ldots \\cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not.You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1\\le n\\le 10^{12}$$$).", "output_spec": "For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer  — the minimum possible value of $$$k$$$.", "sample_inputs": ["4\n\n7\n\n11\n\n240\n\n17179869184"], "sample_outputs": ["2\n3\n4\n1"], "notes": "NoteIn the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$.In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$."}, "positive_code": [{"source_code": "limit = 10 ** 12\r\nfact = [1]\r\nfact << fact[-1] * fact.size while fact[-1] < limit\r\nmap = { 0 => 0 }\r\n(1..fact.size).each do |i|\r\n fact.combination(i).map(&:sum).each do |sum|\r\n map[sum] = i unless map[sum]\r\n end\r\nend\r\n\r\ngets.to_i.times do\r\n n = gets.to_i\r\n p(map.filter_map do |f, c|\r\n c + (n - f).to_s(2).count(\"1\") if n - f >= 0\r\n end.min)\r\nend"}], "negative_code": [], "src_uid": "ff0b041d54755984df3706aae78d8ff2"} {"nl": {"description": "There's a chessboard of size $$$n \\times n$$$. $$$m$$$ rooks are placed on it in such a way that: no two rooks occupy the same cell; no two rooks attack each other. A rook attacks all cells that are in its row or column.Is it possible to move exactly one rook (you can choose which one to move) into a different cell so that no two rooks still attack each other? A rook can move into any cell in its row or column if no other rook stands on its path.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 2000$$$) — the number of testcases. The first line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n, m \\le 8$$$) — the size of the chessboard and the number of the rooks. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \\le x_i, y_i \\le n$$$) — the position of the $$$i$$$-th rook: $$$x_i$$$ is the row and $$$y_i$$$ is the column. No two rooks occupy the same cell. No two rooks attack each other.", "output_spec": "For each testcase, print \"YES\" if it's possible to move exactly one rook into a different cell so that no two rooks still attack each other. Otherwise, print \"NO\".", "sample_inputs": ["2\n\n2 2\n\n1 2\n\n2 1\n\n3 1\n\n2 2"], "sample_outputs": ["NO\nYES"], "notes": "NoteIn the first testcase, the rooks are in the opposite corners of a $$$2 \\times 2$$$ board. Each of them has a move into a neighbouring corner, but moving there means getting attacked by another rook.In the second testcase, there's a single rook in a middle of a $$$3 \\times 3$$$ board. It has $$$4$$$ valid moves, and every move is fine because there's no other rook to attack it."}, "positive_code": [{"source_code": "r=gets.to_i\r\nfor i in 1..r\r\n a = gets.chomp.split\r\n n = a[0].to_i\r\n m = a[1].to_i\r\n for j in 1..m\r\n a=gets.chomp.split\r\n end\r\n if n!=m\r\n puts \"YES\"\r\n else\r\n puts \"NO\"\r\n end\r\nend"}], "negative_code": [], "src_uid": "856b71ffb53f186bccd66c890ed0e099"} {"nl": {"description": "You are given an integer $$$x$$$. Can you make $$$x$$$ by summing up some number of $$$11, 111, 1111, 11111, \\ldots$$$? (You can use any number among them any number of times).For instance, $$$33=11+11+11$$$ $$$144=111+11+11+11$$$ ", "input_spec": "The first line of input contains a single integer $$$t$$$ $$$(1 \\leq t \\leq 10000)$$$ — the number of testcases. The first and only line of each testcase contains a single integer $$$x$$$ $$$(1 \\leq x \\leq 10^9)$$$ — the number you have to make.", "output_spec": "For each testcase, you should output a single string. If you can make $$$x$$$, output \"YES\" (without quotes). Otherwise, output \"NO\". You can print each letter of \"YES\" and \"NO\" in any case (upper or lower).", "sample_inputs": ["3\n33\n144\n69"], "sample_outputs": ["YES\nYES\nNO"], "notes": "NoteWays to make $$$33$$$ and $$$144$$$ were presented in the statement. It can be proved that we can't present $$$69$$$ this way."}, "positive_code": [{"source_code": "require \"set\"\r\n\r\nok = [*0..111].flat_map { |a| [*0..11].map { |b| a * 11 + b * 111 } }.to_set\r\n\r\ngets.to_i.times do\r\n n = gets.to_i\r\n puts ok.include?(n) || n >= 1111 ? \"YES\" : \"NO\"\r\nend\r\n"}], "negative_code": [], "src_uid": "e5e937f080b20eaec5f12f1784ae6427"} {"nl": {"description": "Today the «Z» city residents enjoy a shell game competition. The residents are gathered on the main square to watch the breath-taking performance. The performer puts 3 non-transparent cups upside down in a row. Then he openly puts a small ball under one of the cups and starts to shuffle the cups around very quickly so that on the whole he makes exactly 3 shuffles. After that the spectators have exactly one attempt to guess in which cup they think the ball is and if the answer is correct they get a prize. Maybe you can try to find the ball too?", "input_spec": "The first input line contains an integer from 1 to 3 — index of the cup which covers the ball before the shuffles. The following three lines describe the shuffles. Each description of a shuffle contains two distinct integers from 1 to 3 — indexes of the cups which the performer shuffled this time. The cups are numbered from left to right and are renumbered after each shuffle from left to right again. In other words, the cup on the left always has index 1, the one in the middle — index 2 and the one on the right — index 3.", "output_spec": "In the first line output an integer from 1 to 3 — index of the cup which will have the ball after all the shuffles. ", "sample_inputs": ["1\n1 2\n2 1\n2 1", "1\n2 1\n3 1\n1 3"], "sample_outputs": ["2", "2"], "notes": null}, "positive_code": [{"source_code": "file = File.new(\"input.txt\", \"r\")\npos = file.gets.to_i\na = (1..3).to_a\n(1..3).each do\n x, y = file.gets.split.map{|z| z.to_i-1}\n a[x], a[y] = a[y], a[x]\nend\nfile.close\nFile.open(\"output.txt\", \"w\") {|file| file.write(a.find_index(pos)+1)}\n"}, {"source_code": "f = open(\"input.txt\", \"r\")\nfw = open(\"output.txt\", \"w\")\nidx = f.gets.chomp.to_i\nfor i in 0..2\n\ti1, i2 = f.gets.chomp.split(/ /).map!{|x|x.to_i}\n\tif idx == i1\n\t\tidx = i2\n\telsif idx == i2\n\t\tidx = i1\n\tend\nend\nfw.puts idx\nf.close\nfw.close"}, {"source_code": "#!/usr/bin/ruby\n$stdin=open(\"input.txt\",\"rb\")\n$>=open(\"output.txt\",\"wb\")\na=[0]*4\na[gets.to_i]=1\nwhile gets\n\tx,y=$_.split.map(&:to_i)\n\ta[x],a[y]=a[y],a[x]\nend\np a.index(1)"}, {"source_code": "I=open('input.txt')\nO=open('output.txt','w')\nn=I.gets.to_i\n3.times do\n\ta, b = I.gets.split.map &:to_i\n\tn = n == a ? b : n == b ? a : n\nend\nO.puts n\n"}, {"source_code": "I=open('input.txt')\nO=open('output.txt','w')\nn=I.gets.to_i\n3.times do\n\ta, b = I.gets.split.map &:to_i\n\tn = n == a ? b : n == b ? a : n\nend\nO.puts n\n\n"}, {"source_code": "ball_pos = 0\nFile.open('input.txt').each do |line|\n if ball_pos == 0\n\t\tball_pos = line.to_i\n\telse\n\t\ttemp = line.split.collect { |n| n.to_i }\n\t\tif temp[0] == ball_pos\n\t\t\tball_pos = temp[1]\n\t\telsif temp[1] == ball_pos\n\t\t\tball_pos = temp[0]\n\t\tend\n\tend\nend\nout = File.open('output.txt','w')\nout << ball_pos.to_s\nout.close\n"}, {"source_code": "I=open('input.txt')\nO=open('output.txt','w')\nn=I.gets.to_i\n3.times do\n\ta, b = I.gets.split.map &:to_i\n\tn = n == a ? b : n == b ? a : n\nend\nO.puts n\n\n"}, {"source_code": "I=open('input.txt')\nO=open('output.txt','w')\nn=I.gets.to_i\n3.times do\n\ta, b = I.gets.split.map &:to_i\n\tn = n == a ? b : n == b ? a : n\nend\nO.puts n\n"}, {"source_code": "$stdin=open(\"input.txt\")\n$stdout=open(\"output.txt\",\"w\")\nk=gets.to_i\n3.times do\n\tm,n=gets.chomp.split.map(&:to_i)\n\tk = (m==k)? n : ((n==k)? m : k)\nend\nputs k"}, {"source_code": "I=open('input.txt')\nO=open('output.txt','w')\nn=I.gets.to_i\n3.times do\n\ta, b = I.gets.split.map &:to_i\n\tn = n == a ? b : n == b ? a : n\nend\nO.puts n\n\n"}, {"source_code": "I=open('input.txt')\nO=open('output.txt','w')\nn=I.gets.to_i\n3.times do\n\ta, b = I.gets.split.map &:to_i\n\tn = n == a ? b : n == b ? a : n\nend\nO.puts n\n\n"}, {"source_code": "$stdin = File.new( \"input.txt\", 'r' )\n$stdout = File.new( \"output.txt\", 'w' )\n\nindex = gets.strip.to_i\n3.times do\n a, b = gets.strip.split.map(&:to_i)\n if index == a\n index = b\n elsif index == b\n index = a\n end\nend\nputs index\n"}, {"source_code": "I=open('input.txt')\nO=open('output.txt','w')\nn=I.gets.to_i\n3.times do\n a, b = I.gets.split.map &:to_i\n n = n == a ? b : n == b ? a : n\nend\nO.puts n"}, {"source_code": "I=open('input.txt')\nO=open('output.txt','w')\nn=I.gets.to_i\n3.times do\n\ta, b = I.gets.split.map &:to_i\n\tn = n == a ? b : n == b ? a : n\nend\nO.puts n\n\n"}, {"source_code": "I=open('input.txt')\nO=open('output.txt','w')\nn=I.gets.to_i\n3.times do\n\ta, b = I.gets.split.map &:to_i\n\tn = n == a ? b : n == b ? a : n\nend\nO.puts n"}, {"source_code": "\nline = File.read(\"input.txt\")\ninputs = line.split(\"\\n\")\n\narr = Array.new(3,false)\n\nstart = inputs[0].to_i\narr[start-1] = true\n\n3.times do |inp|\n\ti,j = inputs[inp+1].split(\" \").map(&:to_i)\n\ti -= 1\n\tj -= 1\n\tarr[i],arr[j] = arr[j],arr[i]\nend\n\nFile.open(\"output.txt\", 'w') { |file| \n\tfile.puts arr.index(true) + 1\n}\n\n"}, {"source_code": "$stdin = File.open(\"input.txt\",\"r\")\n$stdout = File.open(\"output.txt\",\"w\")\nk=gets.to_i\n3.times {|x| l=gets.split.map &:to_i ; l[0]==k ?k=l[1]:(l[1]==k ?k=l[0]:0)}\nputs k"}, {"source_code": "input = open('input.txt','r')\noutput = open('output.txt','w')\n\ncurr = input.gets.chomp!.to_i\n\nfor i in 1..3\n s = input.gets.chomp!.split(\" \")\n mas = []\n mas[0] = s[0].to_i\n mas[1] = s[1].to_i\n\n if (curr == mas[0])\n curr = mas[1]\n else\n if (curr == mas[1])\n curr = mas[0]\n end\n end\nend\n\noutput.puts(curr)"}, {"source_code": "I=open('input.txt')\nO=open('output.txt','w')\nn=I.gets.to_i\n3.times do\n\ta, b = I.gets.split.map &:to_i\n\tn = n == a ? b : n == b ? a : n\nend\nO.puts n"}, {"source_code": "I=open('input.txt')\nO=open('output.txt','w')\nn=I.gets.to_i\n3.times do\n\ta, b = I.gets.split.map &:to_i\n\tn = n == a ? b : n == b ? a : n\nend\nO.puts n\n\n"}, {"source_code": "File.open('input.txt') do |f|\n now = f.gets.to_i\n 3.times do \n a, b = f.gets.split.map(&:to_i)\n if [a, b].index(now)\n now ^= a ^ b\n end\n end\n File.open('output.txt', 'w') { |f| f.puts now }\nend\n"}, {"source_code": "I=open('input.txt')\nO=open('output.txt','w')\nn=I.gets.to_i\n3.times do\n\ta, b = I.gets.split.map &:to_i\n\tn = n == a ? b : n == b ? a : n\nend\nO.puts n\n\n"}, {"source_code": "I=open('input.txt')\nO=open('output.txt','w')\nn=I.gets.to_i\n3.times do\n\ta, b = I.gets.split.map &:to_i\n\tn = n == a ? b : n == b ? a : n\nend\nO.puts n\n\n"}], "negative_code": [{"source_code": "#!/usr/bin/ruby\na=[0]*4\na[gets.to_i]=1\nwhile gets\n\tx,y=$_.split.map(&:to_i)\n\ta[x],a[y]=a[y],a[x]\nend\nputs 2\nputs(a.index(1))"}, {"source_code": "#!/usr/bin/ruby\nf=open(\"output.txt\",\"wb\")\na=[0]*4\na[gets.to_i]=1\nwhile gets\n\tx,y=$_.split.map(&:to_i)\n\tf.puts x\n\tf.puts y\n\ta[x],a[y]=a[y],a[x]\nend\nf.puts(a.index(1))"}, {"source_code": "#!/usr/bin/ruby\nf=open(\"output.txt\",\"wb\")\na=[0]*4\na[gets.to_i]=1\nwhile gets\n\tx,y=$_.split.map(&:to_i)\n\ta[x],a[y]=a[y],a[x]\nend\nf.puts(a.index(1))"}, {"source_code": "#!/usr/bin/ruby\nf=open(\"output.txt\",\"w\")\na=[0]*4\na[gets.to_i]=1\nwhile gets\n\tx,y=$_.split.map(&:to_i)\n\ta[x],a[y]=a[y],a[x]\nend\nf.puts a.index(1)"}, {"source_code": "#!/usr/bin/ruby\na=[0]*4\na[gets.to_i]=1\nwhile gets\n\tx,y=$_.split.map(&:to_i)\n\ta[x],a[y]=a[y],a[x]\nend\nputs(a.index(1))"}, {"source_code": "#!/usr/bin/ruby\na=[0]*4\na[gets.to_i]=1\nwhile gets\n\tx,y=$_.split.map(&:to_i)\n\ta[x],a[y]=a[y],a[x]\nend\np a.index 1"}, {"source_code": "#!/usr/bin/ruby\na=[0]*4\na[gets.to_i]=1\nwhile gets\n\tx,y=$_.split.map(&:to_i)\nputs x\n\ta[x],a[y]=a[y],a[x]\nend\nputs(a.index(1))"}, {"source_code": "#!/usr/bin/ruby\na=[0]*4\na[gets.to_i]=1\nwhile gets\n\tx,y=$_.split.map(&:to_i)\n\ta[x],a[y]=a[y],a[x]\nend\np a.index(1)"}, {"source_code": "#!/usr/bin/ruby\nf=open(\"output.txt\",\"w\")\na=[0]*4\na[gets.to_i]=1\nwhile gets\n\tx,y=$_.split.map(&:to_i)\n\ta[x],a[y]=a[y],a[x]\nend\nf.puts a.inspect\nf.puts a.index(1)"}], "src_uid": "88e6651e1b0481d711e89c8071be1edf"} {"nl": {"description": "Mashmokh is playing a new game. In the beginning he has k liters of water and p coins. Additionally he has a rooted tree (an undirected connected acyclic graph) that consists of m vertices. Each vertex of the tree contains a water tank that is empty in the beginning.The game begins with the fact that Mashmokh chooses some (no more than k) of these tanks (except the root) and pours into each of them exactly 1 liter of water. Then the following process is performed until there is no water remained in tanks. The process consists of several steps. At the beginning of each step Mashmokh opens doors of all tanks. Then Mashmokh closes doors of some tanks (he is not allowed to close door of tank in the root) for the duration of this move. Let's denote the number of liters in some tank with closed door as w, Mashmokh pays w coins for the closing of that tank during this move. Let's denote by x1, x2, ..., xm as the list of vertices of the tree sorted (nondecreasing) by their depth. The vertices from this list should be considered one by one in the order. Firstly vertex x1 (which is the root itself) is emptied. Then for each vertex xi (i > 1), if its door is closed then skip the vertex else move all the water from the tank of vertex xi to the tank of its father (even if the tank of the father is closed). Suppose l moves were made until the tree became empty. Let's denote the amount of water inside the tank of the root after the i-th move by wi then Mashmokh will win max(w1, w2, ..., wl) dollars. Mashmokh wanted to know what is the maximum amount of dollars he can win by playing the above game. He asked you to find this value for him.", "input_spec": "The first line of the input contains three space-separated integers m, k, p (2 ≤ m ≤ 105; 0 ≤ k, p ≤ 109). Each of the following m - 1 lines contains two space-separated integers ai, bi (1 ≤ ai, bi ≤ m; ai ≠ bi) — the edges of the tree. Consider that the vertices of the tree are numbered from 1 to m. The root of the tree has number 1.", "output_spec": "Output a single integer, the number Mashmokh asked you to find.", "sample_inputs": ["10 2 1\n1 2\n1 3\n3 4\n3 5\n2 6\n6 8\n6 7\n9 8\n8 10", "5 1000 1000\n1 2\n1 3\n3 4\n3 5"], "sample_outputs": ["2", "4"], "notes": "NoteThe tree in the first sample is shown on the picture below. The black, red, blue colors correspond to vertices with 0, 1, 2 liters of water.One way to achieve the maximum amount of money is to put 1 liter of water in each of vertices 3 and 4. The beginning state is shown on the picture below.Then in the first move Mashmokh will pay one token to close the door of the third vertex tank. The tree after the first move is shown on the picture below.After the second move there are 2 liters of water in the root as shown on the picture below."}, "positive_code": [{"source_code": "m,k,p = gets.split.map(&:to_i)\nh={}\n(m-1).times{\na,b = gets.split.map(&:to_i)\n(h[a]||=[])<p||j-i+1>k\nh[x].each{|y|\nnext if d[y]\nd[y]=m+1\nq<0}\ni=1\nj=0\ns=0\nans=0\nwhile jp||j-i+1>k\nh[x].each{|y|\nnext if d[y]\nd[y]=m+1\nq<0\nj+=1\nend\np ans\n"}, {"source_code": "m,k,p = gets.split.map(&:to_i)\nh={}\n(m-1).times{\na,b = gets.split.map(&:to_i)\n(h[a]||=[])<0}\ni=1\nj=0\ns=0\nans=0\nwhile jp||j-i+1>k\nh[x].each{|y|\nnext if d[y]\nd[y]=m+1\nq<0\nj+=1\nend\np ans\n\n"}, {"source_code": "m,k,p = gets.split.map(&:to_i)\nh={}\n(m-1).times{\na,b = gets.split.map(&:to_i)\n(h[a]||=[])<0}\ni=1\nj=0\ns=0\nans=0\nwhile jp||j-i+1>k\nh[x].each{|y|\nnext if d[y]\nd[y]=m+1\nq<0\nj+=1\nend\np ans\n\n"}, {"source_code": "m,k,p = gets.split.map(&:to_i)\nh={}\n(m-1).times{\na,b = gets.split.map(&:to_i)\n(h[a]||=[])<0}\ni=1\nj=0\ns=0\nans=0\nwhile jp||j-i+1>k\nh[x].each{|y|\nnext if d[y]\nd[y]=m+1\nq<0\nj+=1\nend\np ans\n\n"}, {"source_code": "m,k,p = gets.split.map(&:to_i)\nh={}\n(m-1).times{\na,b = gets.split.map(&:to_i)\n(h[a]||=[])<0}\ni=1\nj=0\ns=0\nans=0\nwhile jp||j-i+1>k\nh[x].each{|y|\nnext if d[y]\nd[y]=m+1\nq<0\nj+=1\nend\np ans\n\n"}, {"source_code": "m,k,p = gets.split.map(&:to_i)\nh={}\n(m-1).times{\na,b = gets.split.map(&:to_i)\n(h[a]||=[])<0}\ni=1\nj=0\ns=0\nans=0\nwhile jp||j-i+1>k\nh[x].each{|y|\nnext if d[y]\nd[y]=m+1\nq<0\nj+=1\nend\np ans\n\n"}, {"source_code": "m,k,p = gets.split.map(&:to_i)\nh={}\n(m-1).times{\na,b = gets.split.map(&:to_i)\n(h[a]||=[])<0}\ni=1\nj=0\ns=0\nans=0\nwhile jp||j-i+1>k\nh[x].each{|y|\nnext if d[y]\nd[y]=m+1\nq<0\nj+=1\nend\np ans\n\n"}, {"source_code": "m,k,p = gets.split.map(&:to_i)\nh={}\n(m-1).times{\na,b = gets.split.map(&:to_i)\n(h[a]||=[])<0}\ni=1\nj=0\ns=0\nans=0\nwhile jp||j-i+1>k\nh[x].each{|y|\nnext if d[y]\nd[y]=m+1\nq<0\nj+=1\nend\np ans\n\n"}, {"source_code": "m,k,p = gets.split.map(&:to_i)\nh={}\n(m-1).times{\na,b = gets.split.map(&:to_i)\n(h[a]||=[])<0}\ni=1\nj=0\ns=0\nans=0\nwhile jp||j-i+1>k\nh[x].each{|y|\nnext if d[y]\nd[y]=m+1\nq<0\nj+=1\nend\np ans\n\n"}], "negative_code": [], "src_uid": "fb731edef28d25356a34f211dba91e41"} {"nl": {"description": "Gregor is learning about RSA cryptography, and although he doesn't understand how RSA works, he is now fascinated with prime numbers and factoring them.Gregor's favorite prime number is $$$P$$$. Gregor wants to find two bases of $$$P$$$. Formally, Gregor is looking for two integers $$$a$$$ and $$$b$$$ which satisfy both of the following properties. $$$P \\bmod a = P \\bmod b$$$, where $$$x \\bmod y$$$ denotes the remainder when $$$x$$$ is divided by $$$y$$$, and $$$2 \\le a < b \\le P$$$. Help Gregor find two bases of his favorite prime number!", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 1000$$$). Each subsequent line contains the integer $$$P$$$ ($$$5 \\le P \\le {10}^9$$$), with $$$P$$$ guaranteed to be prime.", "output_spec": "Your output should consist of $$$t$$$ lines. Each line should consist of two integers $$$a$$$ and $$$b$$$ ($$$2 \\le a < b \\le P$$$). If there are multiple possible solutions, print any.", "sample_inputs": ["2\n17\n5"], "sample_outputs": ["3 5\n2 4"], "notes": "NoteThe first query is $$$P=17$$$. $$$a=3$$$ and $$$b=5$$$ are valid bases in this case, because $$$17 \\bmod 3 = 17 \\bmod 5 = 2$$$. There are other pairs which work as well.In the second query, with $$$P=5$$$, the only solution is $$$a=2$$$ and $$$b=4$$$."}, "positive_code": [{"source_code": "t = gets.to_i\r\nwhile(t>0)\r\n num = gets.to_i\r\n puts(\"2 \" + (num-1).to_s)\r\n t-=1\r\nend\r\n"}, {"source_code": "t = gets.to_i\r\nt.times do\r\n\tprime = gets.to_i\r\n\tputs [2, prime - 1] * \" \"\r\nend\r\n"}, {"source_code": "\n\nt = gets.chomp.to_i\n\nt.times do\n p = gets.chomp.to_i\n puts \"2 #{p-1}\\n\"\nend\n\n"}, {"source_code": "def i(x)\n\tx.to_i\nend\n\nn = gets\nfor i in 1..i(n)\n\ta = gets\n\ta = i(a)\n\tputs \"2 #{a - 1}\"\nend"}, {"source_code": "t = gets.chomp.to_i\nt.times do\n hash = {}\n n = gets.chomp.to_i\n puts \"#{2} #{n-1}\"\nend\n"}], "negative_code": [], "src_uid": "259e39c9e63e43678e596c0d8c66937c"} {"nl": {"description": " William has two numbers $$$a$$$ and $$$b$$$ initially both equal to zero. William mastered performing three different operations with them quickly. Before performing each operation some positive integer $$$k$$$ is picked, which is then used to perform one of the following operations: (note, that for each operation you can choose a new positive integer $$$k$$$) add number $$$k$$$ to both $$$a$$$ and $$$b$$$, or add number $$$k$$$ to $$$a$$$ and subtract $$$k$$$ from $$$b$$$, or add number $$$k$$$ to $$$b$$$ and subtract $$$k$$$ from $$$a$$$. Note that after performing operations, numbers $$$a$$$ and $$$b$$$ may become negative as well.William wants to find out the minimal number of operations he would have to perform to make $$$a$$$ equal to his favorite number $$$c$$$ and $$$b$$$ equal to his second favorite number $$$d$$$.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$c$$$ and $$$d$$$ $$$(0 \\le c, d \\le 10^9)$$$, which are William's favorite numbers and which he wants $$$a$$$ and $$$b$$$ to be transformed into.", "output_spec": "For each test case output a single number, which is the minimal number of operations which William would have to perform to make $$$a$$$ equal to $$$c$$$ and $$$b$$$ equal to $$$d$$$, or $$$-1$$$ if it is impossible to achieve this using the described operations.", "sample_inputs": ["6\n1 2\n3 5\n5 3\n6 6\n8 0\n0 0"], "sample_outputs": ["-1\n2\n2\n1\n2\n0"], "notes": "NoteLet us demonstrate one of the suboptimal ways of getting a pair $$$(3, 5)$$$: Using an operation of the first type with $$$k=1$$$, the current pair would be equal to $$$(1, 1)$$$. Using an operation of the third type with $$$k=8$$$, the current pair would be equal to $$$(-7, 9)$$$. Using an operation of the second type with $$$k=7$$$, the current pair would be equal to $$$(0, 2)$$$. Using an operation of the first type with $$$k=3$$$, the current pair would be equal to $$$(3, 5)$$$. "}, "positive_code": [{"source_code": "t = gets.to_i\r\nt.times do\r\n\r\nc, d = gets.split.map!(&:to_i)\r\n\r\nif (c + d) < 0 || (c + d).odd?\r\n puts -1\r\n next\r\nend\r\n\r\nif c == 0 && d == 0\r\n puts 0\r\n next\r\nend\r\n\r\nif c == d\r\n puts 1\r\n next\r\nend\r\n\r\nputs 2\r\n\r\nend\r\n"}], "negative_code": [], "src_uid": "8e7c0b703155dd9b90cda706d22525c9"} {"nl": {"description": "Pavel made a photo of his favourite stars in the sky. His camera takes a photo of all points of the sky that belong to some rectangle with sides parallel to the coordinate axes.Strictly speaking, it makes a photo of all points with coordinates $$$(x, y)$$$, such that $$$x_1 \\leq x \\leq x_2$$$ and $$$y_1 \\leq y \\leq y_2$$$, where $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ are coordinates of the left bottom and the right top corners of the rectangle being photographed. The area of this rectangle can be zero.After taking the photo, Pavel wrote down coordinates of $$$n$$$ of his favourite stars which appeared in the photo. These points are not necessarily distinct, there can be multiple stars in the same point of the sky.Pavel has lost his camera recently and wants to buy a similar one. Specifically, he wants to know the dimensions of the photo he took earlier. Unfortunately, the photo is also lost. His notes are also of not much help; numbers are written in random order all over his notepad, so it's impossible to tell which numbers specify coordinates of which points.Pavel asked you to help him to determine what are the possible dimensions of the photo according to his notes. As there are multiple possible answers, find the dimensions with the minimal possible area of the rectangle.", "input_spec": "The first line of the input contains an only integer $$$n$$$ ($$$1 \\leq n \\leq 100\\,000$$$), the number of points in Pavel's records. The second line contains $$$2 \\cdot n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_{2 \\cdot n}$$$ ($$$1 \\leq a_i \\leq 10^9$$$), coordinates, written by Pavel in some order.", "output_spec": "Print the only integer, the minimal area of the rectangle which could have contained all points from Pavel's records.", "sample_inputs": ["4\n4 1 3 2 3 2 1 3", "3\n5 8 5 5 7 5"], "sample_outputs": ["1", "0"], "notes": "NoteIn the first sample stars in Pavel's records can be $$$(1, 3)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(2, 4)$$$. In this case, the minimal area of the rectangle, which contains all these points is $$$1$$$ (rectangle with corners at $$$(1, 3)$$$ and $$$(2, 4)$$$)."}, "positive_code": [{"source_code": "N = gets.to_i\nas = gets.split.map(&:to_i).sort\n\nans1 = (as[2*N-1] - as[N]) * (as[N-1] - as[0])\n\nmin = 10**9\n(0..N).each do |i|\n result = as[i+N-1] - as[i]\n min = result if min > result\nend\nans2 = min * (as[2*N-1] - as[0])\n\nputs [ans1, ans2].min"}, {"source_code": "N = gets.to_i\nas = gets.split.map(&:to_i).sort\n\nans1 = (as[2*N-1] - as[N]) * (as[N-1] - as[0])\n\nmin = 10**9\n(0..N).each do |i|\n result = as[i+N-1] - as[i]\n min = result if min > result\nend\nans2 = min * (as[2*N-1] - as[0])\n\nputs [ans1, ans2].min"}], "negative_code": [], "src_uid": "cda1179c51fc69d2c64ee4707b97cbb3"} {"nl": {"description": "For some time the program of rounding numbers that had been developed by the Codeforces participants during one of the previous rounds, helped the citizens of Far Far Away to convert numbers into a more easily readable format. However, as time went by, the economy of the Far Far Away developed and the scale of operations grew. So the King ordered to found the Bank of Far Far Away and very soon even the rounding didn't help to quickly determine even the order of the numbers involved in operations. Besides, rounding a number to an integer wasn't very convenient as a bank needed to operate with all numbers with accuracy of up to 0.01, and not up to an integer.The King issued yet another order: to introduce financial format to represent numbers denoting amounts of money. The formal rules of storing a number in the financial format are as follows: A number contains the integer part and the fractional part. The two parts are separated with a character \".\" (decimal point). To make digits in the integer part of a number easier to read, they are split into groups of three digits, starting from the least significant ones. The groups are separated with the character \",\" (comma). For example, if the integer part of a number equals 12345678, then it will be stored in the financial format as 12,345,678 In the financial format a number's fractional part should contain exactly two digits. So, if the initial number (the number that is converted into the financial format) contains less than two digits in the fractional part (or contains no digits at all), it is complemented with zeros until its length equals 2. If the fractional part contains more than two digits, the extra digits are simply discarded (they are not rounded: see sample tests). When a number is stored in the financial format, the minus sign is not written. Instead, if the initial number had the minus sign, the result is written in round brackets. Please keep in mind that the bank of Far Far Away operates using an exotic foreign currency — snakes ($), that's why right before the number in the financial format we should put the sign \"$\". If the number should be written in the brackets, then the snake sign should also be inside the brackets. For example, by the above given rules number 2012 will be stored in the financial format as \"$2,012.00\" and number -12345678.9 will be stored as \"($12,345,678.90)\".The merchants of Far Far Away visited you again and expressed much hope that you supply them with the program that can convert arbitrary numbers to the financial format. Can you help them?", "input_spec": "The input contains a number that needs to be converted into financial format. The number's notation length does not exceed 100 characters, including (possible) signs \"-\" (minus) and \".\" (decimal point). The number's notation is correct, that is: The number's notation only contains characters from the set {\"0\" – \"9\", \"-\", \".\"}. The decimal point (if it is present) is unique and is preceded and followed by a non-zero quantity on decimal digits A number cannot start with digit 0, except for a case when its whole integer part equals zero (in this case the integer parts is guaranteed to be a single zero: \"0\"). The minus sign (if it is present) is unique and stands in the very beginning of the number's notation If a number is identically equal to 0 (that is, if it is written as, for example, \"0\" or \"0.000\"), than it is not preceded by the minus sign. The input data contains no spaces. The number's notation contains at least one decimal digit. ", "output_spec": "Print the number given in the input in the financial format by the rules described in the problem statement.", "sample_inputs": ["2012", "0.000", "-0.00987654321", "-12345678.9"], "sample_outputs": ["$2,012.00", "$0.00", "($0.00)", "($12,345,678.90)"], "notes": "NotePay attention to the second and third sample tests. They show that the sign of a number in the financial format (and consequently, the presence or absence of brackets) is determined solely by the sign of the initial number. It does not depend on the sign of the number you got after translating the number to the financial format."}, "positive_code": [{"source_code": "a, b = gets.split('.').map(&:chomp)\nans = \"\"\nif a[0] == \"-\"\n ans += \"(\"\n a = a[1..a.size]\nend\nans += \"$\"\nans += a.reverse.gsub(/(\\d{3})(?=\\d)/,'\\1,').reverse\nb ||= \"0\"\nans += \".\" + (b + \"00\")[0..1]\nans += ans[0] == \"(\" ? \")\" : \"\"\nputs ans\n"}, {"source_code": "class Integer\n def to_currency()\n self.to_s.reverse.gsub(/(\\d{3})(?=\\d)/, '\\1,').reverse\n end\nend\n\ns = gets.chomp\nsa = s.split(\".\")\nif sa.length == 1\n\tsa << \"00\"\nelse\n\tsa[1] = sa[1].slice(0,2)\n\tif sa[1].length == 1\n\t\tsa[1] += \"0\"\n\tend\nend\nminus = false\nif sa[0].slice(0,1) == \"-\"\n\tminus = true\nend\nsa[0] = sa[0].sub(\"-\", \"\")\nsa[0] = sa[0].to_i.to_currency\nret = \"$\" + sa.join(\".\")\nif minus\n\tret = \"(\" + ret + \")\"\nend\nputs ret\n"}, {"source_code": "s = gets.chop\nn = nil\nif s[0] == '-'\n s = s[1..-1]\n n = true\nend\ni, f = s.split('.')\nf = (f || '') + '00'\ns = \"$#{i.gsub(/(?<=.)(?=(.{3})+$)/, ',')}.#{f[0, 2]}\"\nputs n ? \"(#{s})\" : s\n"}, {"source_code": "t=gets.split[0]\nminus=false\nif t[0]==\"-\" then\n minus=true\n t=t.slice(1,t.length-1)\nend\na,b=(t+'.0000.0').split('.')\nres=\"\"\na=a.to_i\nb+=\"0000\"\nwhile a>0\n res=sprintf(\",%03d\",a%1000)+res;\n a/=1000\nend\nres=res.slice(1,res.length-1) if res[0]==','\nres=res.slice(1,res.length-1) while res.size()>1 && res[0]=='0'\nres=\"0\" if res==\"\"\nres=\"$\"+res+\".\"+b.slice(0,2)\nres=\"(\"+res+\")\" if minus\nputs res\n"}, {"source_code": "#!/usr/bin/ruby\ndef read_int; gets.chomp.to_i; end\ndef read_ints; gets.chomp.split.map(&:to_i); end\n\na = gets.chomp\narr = a.split('')\n\nminus = false\nif arr[0] == '-'\n minus = true\n arr.shift\nend\n\nindex = arr.index('.')\nlen = 0\nif index == nil\n len = arr.size\nelse\n len = index\nend\n\nprint '(' if minus\nprint '$'\n\ncount = (3 - (len % 3)) % 3\n\n#puts \"count = #{len}\"\n\n\n0.upto(len-1) do |i|\n print arr[i]\n count += 1\n# puts \" count = #{count}\"\n print ',' if len > 3 and count % 3 == 0 and i+1 != len\nend\n\nprint '.'\n\nlen.times { arr.shift }\narr.shift if arr[0] == '.'\n\nif arr.size == 0\n print '00'\nelsif arr.size == 1\n print arr[0]\n print '0'\nelse\n print arr[0]\n print arr[1]\nend\n\nprint ')' if minus\nputs\n"}, {"source_code": "s = gets.chop\nn = nil\nif s[0] == '-'\n s = s[1..-1]\n n = true\nend\ni, f = s.split('.')\nf = (f || '') + '00'\ns = \"$#{i.gsub(/(?<=.)(?=(.{3})+$)/, ',')}.#{f[0, 2]}\"\nputs n ? \"(#{s})\" : s\n__END__"}, {"source_code": "n = gets.strip\nm = false\n\nif n[0] == \"-\"\n m = true\n n.delete! \"-\"\nend\n\ni,f = n.split(/\\./) \n\nf = \"00\" if f == nil\nf += \"0\" if f.size == 1\n\ns = i.size\na = \"$\"\ni.each_char do |c|\n a += c\n s -= 1\n a += \",\"if s % 3 == 0 && s != 0\nend\na += \".\"\na += f[0,2]\n\nif m\n a = \"(\" + a\n a += \")\"\nend\nputs a\n"}, {"source_code": "a,b = gets.split('.')\n\na = a.chomp\nb = b.chomp if b != nil\n\nlast = \"\\n\"\nif a[0].chr == '-'\n print '('\n last = \")\\n\"\n a = a[1..-1]\nend\n\nprint '$'\n\ni = a.length % 3\nfirst = true\ncount = 0\na.each_byte{|c|\n print ',' if !first && i == 0\n print c.chr\n i = (i+2) % 3\n first = false\n count += 1\n}\n\nif b == nil\n print '.00'\nelsif b.length == 1\n print \".#{b}0\"\nelse\n print \".#{b[0..1]}\"\nend\n\nprint last\n\n\n\n\n"}, {"source_code": "#!/usr/bin/ruby\ninclude Math\nrequire 'pp'\nminus = false\ninput = gets.chomp\nminus = true if input[0] == '-'\ninput.delete!('-')\nif(input.include? '.')\n input = input.split ('.')\n input[1] = input[1][0..1]\nelse\n input = [input,'00']\nend\ninput[0].reverse!\noutput = []\ninput[0].chars.to_a.each_with_index do |c,i|\n if output[i/3]\n output[i/3] += c \n else\n output[i/3] = c\n end\nend\noutput = output.reverse.map{ |x| x.reverse!}.join(',')\noutput = '$'+output+'.'+input[1]\noutput += '0' if input[1].size < 2\noutput = '(' + output + ')' if minus\nputs output\n\n"}, {"source_code": "number = gets.chomp\ninteger = nil\nfraction = nil\n\nif /\\A([^\\.]+)\\.(\\d+)\\Z/=~ number\n integer = $1\n fraction = $2\nelsif /\\A([^\\.]+)\\Z/=~ number\n integer = $1\nelse\n raise\nend\n\nminus = false\nminus = true if /\\A\\-/=~ integer\ninteger.gsub!( /\\A\\-/, \"\" )\ninteger = integer.reverse.gsub( /\\d\\d\\d/, '\\&,' ).reverse\ninteger.gsub!( /\\A,/, \"\" )\n\nif fraction.nil?\n fraction = \"00\"\nend\nif fraction.size < 2\n fraction += \"0\"\nend\nfraction = fraction.split(//)[0..1].join(\"\")\n\nif minus\n puts \"($#{ integer }.#{ fraction })\"\nelse\n puts \"$#{ integer }.#{ fraction }\"\nend\n"}, {"source_code": "n = gets.chomp\na,b = n.gsub(\"-\",\"\").split(\".\")\nb = b || \"0\"\nb = b[0..1].ljust(2,\"0\")\na = a.reverse.scan(/.{1,3}/).map(&:reverse).reverse.join(\",\")\n\nnumber = \"#{a}.#{b}\"\n\nif n[0] != \"-\"\n\tputs \"$#{number}\"\nelse\n\tputs \"($#{number})\"\t\nend\n\n"}, {"source_code": "s = gets.strip\nif s[/\\./]\n s += '00'\nelse\n s += '.00'\nend\nif s.match(/\\A(-)?([\\d]+)\\.(\\d\\d)/)\n sign, main, sub = $1, $2, $3\n main = (main.reverse.scan(/.{1,3}/)*\",\").reverse\n if sign\n puts \"($#{main}.#{sub})\"\n else\n puts \"$#{main}.#{sub}\"\n end\n\nend\n"}, {"source_code": "n = gets.strip\nm = false\n\nif(n[0] == '-')\n m = true\n n.delete! '-'\nend \n\ni, f = n.split(/\\./)\n\nif(f == nil)\n f = \"00\";\nend\n\nif(f.size == 1)\n f += '0'\nend\n \nans = '$'\nsiz = i.size\n\ni.each_char do\n |c|\n siz -= 1; \n ans += c;\n \n if(siz % 3 == 0 and siz != 0)\n ans += ',' \n end\n #puts(ans)\nend\n\nans += '.'\nans += f[0,2]\n\nif(m)\n print(\"(\", ans, \")\")\nend\n\nif(!m)\n print(ans)\nend"}, {"source_code": "a, b = gets.split(\".\").map(&:chomp)\nans = \"\"\nif a[0] == \"-\"\n ans += \"(\"\n a = a[1...a.length]\nend\nans += \"$\"\nans += a.reverse.gsub(/(\\d{3})(?=\\d)/, '\\1,').reverse\nb ||= \"0\"\nans += \".\" + (b + \"00\")[0..1]\nans += \")\" if ans[0] == \"(\"\nputs ans"}, {"source_code": "input = gets.strip\nsurround = input[0] == \"-\" ? (input[0] = \"\"; true) : false\n\ninput = input.split '.'\nl = []\n\ninput[0].split('').reverse.each_slice(3) { |s| l << s.reverse.join + \",\" }\nl = l.reverse.join.chop\n\nr = input[1]\nr = \"00\" if r.nil?\nr = r + \"0\" if r.length == 1\nr = r[0, 2] if r.length >= 2\n\nprint '(' if surround\nprint '$', l, '.', r\nprint ')' if surround"}], "negative_code": [{"source_code": "n = gets.strip\nm = false\n\np n\nif n[0] == \"-\"\n m = true\n n.delete! \"-\"\nend\n\ni,f = n.split(/\\./) \np i\nf = \"00\" if f == nil\nf += \"0\" if f.size == 1\n\ns = i.size\na = \"$\"\ni.each_char do |c|\n a += c\n s -= 1\n a += \",\"if s % 3 == 0 && s != 0\nend\na += \".\"\na += f[0,2]\n\nif m\n a = \"(\" + a\n a += \")\"\nend\nputs a\n"}, {"source_code": "number = gets.chomp\ninteger = nil\nfraction = nil\n\ncase number\n when /\\A([^\\.]+)\\.(\\d+)\\Z/\n integer = $1\n fraction = $2\n when /\\A([^\\.]+)\\Z/\n integer = $1\nend\n\nminus = false\nminus = true if /\\A\\-/=~ integer\ninteger.gsub!( /\\A\\-/, \"\" )\nif integer.size > 3\n integer = integer.reverse.gsub( /\\d\\d\\d/, '\\&,' ).reverse\nend\n\nif fraction.nil?\n fraction = \"00\"\nend\nif fraction.size < 2\n fraction += \"0\"\nend\nfraction = fraction.split(//)[0..1].join(\"\")\n\nif minus\n puts \"($#{ integer }.#{ fraction })\"\nelse\n puts \"$#{ integer }.#{ fraction }\"\nend\n"}, {"source_code": "number = gets.chomp\ninteger = nil\nfraction = nil\n\nif /\\A([^\\.]+)\\.(\\d+)\\Z/=~ number\n integer = $1\n fraction = $2\nelsif /\\A([^\\.]+)\\Z/=~ number\n integer = $1\nelse\n raise\nend\n\nminus = false\nminus = true if /\\A\\-/=~ integer\ninteger.gsub!( /\\A\\-/, \"\" )\nif integer.size > 3\n integer = integer.reverse.gsub( /\\d\\d\\d/, '\\&,' ).reverse\nend\n\nif fraction.nil?\n fraction = \"00\"\nend\nif fraction.size < 2\n fraction += \"0\"\nend\nfraction = fraction.split(//)[0..1].join(\"\")\n\nif minus\n puts \"($#{ integer }.#{ fraction })\"\nelse\n puts \"$#{ integer }.#{ fraction }\"\nend\n"}, {"source_code": "number = gets.chomp\ninteger = nil\nfraction = nil\n\ncase number\n when /\\A([^\\.]+)\\.(\\d+)\\Z/\n integer = $1\n fraction = $2\n when /\\A([^\\.]+)\\Z/\n integer = $1\nend\n\nminus = false\nminus = true if /\\A\\-/=~ integer\ninteger.gsub!( /\\A\\-/, \"\" )\ninteger = integer.reverse.gsub( /\\d\\d\\d/, '\\&,' ).reverse\n\nif fraction.nil?\n fraction = \"00\"\nend\nif fraction.size < 2\n fraction += \"0\"\nend\nfraction = fraction.split(//)[0..1].join(\"\")\n\nif minus\n puts \"($#{ integer }.#{ fraction })\"\nelse\n puts \"$#{ integer }.#{ fraction }\"\nend\n\n# p minus\n# p integer\n# p fraction\n\n"}, {"source_code": "n = gets.strip\nm = false\n\nif(n[0] == '-')\n m = true\n n.delete! '-'\nend \n\ni, f = n.split(/\\./)\n\nif(f == nil)\n f = \"00\";\nend\n\nif(f.size == 1)\n f += '0'\nend\n \nans = '$'\nsiz = i.size\n\ni.each_char do\n |c|\n siz -= 1; \n ans += c;\n \n if(siz % 3 == 0 and siz != 0)\n ans += ',' \n end\n #puts(ans)\nend\n\nans += '.'\nans += f[0..2]\n\nif(m)\n print(\"(\", ans, \")\")\nend\n\nif(!m)\n print(ans)\nend"}, {"source_code": "a, b = gets.split(\".\")\nans = \"\"\nif a[0] == \"-\"\n ans += \"(\"\n a = a[1...a.length]\nend\nans += \"$\"\nans += a.reverse.gsub(/(\\d{3})(?=\\d)/, '\\1,').reverse\nb ||= \"0\"\nans += \".\" + (b + \"00\")[0..1]\nans += \")\" if ans[0] == \"(\"\np ans"}, {"source_code": "a, b = gets.split(\".\")\nans = \"\"\nif a[0] == \"-\"\n ans += \"(\"\n a = a[1...a.length]\nend\nans += \"$\"\nans += a.reverse.gsub(/(\\d{3})(?=\\d)/, '\\1,').reverse\nb ||= \"0\"\nans += \".\" + (b + \"00\")[0..1]\nans += \")\" if ans[0] == \"(\"\nputs ans"}], "src_uid": "c704c5fb9e054fab1caeab534849901d"} {"nl": {"description": "You are given a string s, consisting of small Latin letters. Let's denote the length of the string as |s|. The characters in the string are numbered starting from 1. Your task is to find out if it is possible to rearrange characters in string s so that for any prime number p ≤ |s| and for any integer i ranging from 1 to |s| / p (inclusive) the following condition was fulfilled sp = sp × i. If the answer is positive, find one way to rearrange the characters.", "input_spec": "The only line contains the initial string s, consisting of small Latin letters (1 ≤ |s| ≤ 1000).", "output_spec": "If it is possible to rearrange the characters in the string so that the above-mentioned conditions were fulfilled, then print in the first line \"YES\" (without the quotes) and print on the second line one of the possible resulting strings. If such permutation is impossible to perform, then print the single string \"NO\".", "sample_inputs": ["abc", "abcd", "xxxyxxx"], "sample_outputs": ["YES\nabc", "NO", "YES\nxxxxxxy"], "notes": "NoteIn the first sample any of the six possible strings will do: \"abc\", \"acb\", \"bac\", \"bca\", \"cab\" or \"cba\".In the second sample no letter permutation will satisfy the condition at p = 2 (s2 = s4).In the third test any string where character \"y\" doesn't occupy positions 2, 3, 4, 6 will be valid."}, "positive_code": [{"source_code": "require\"prime\"\n$char_num={}\ndef max_char\n\ta,max=\"\",0\n\t?a.upto(?z){|c|\n\t\tif max<$char_num[c]\n\t\t\tmax=$char_num[c]\n\t\t\ta=c\n\t\tend\n\t}\n\t[a,max]\nend\ns=gets.chomp\nsize=s.size\nflag=Array.new(size+1,false)\n?a.upto(?z){|c|$char_num[c]=s.count c}\nPrime.each(size){|p|\n\ti=size/p\n\t1.upto(i){|j|flag[p*j]=true} if i>1\n}\nw=flag.count(true)\na,max=max_char\nif w>max\n\tputs \"NO\"\nelse\n\tputs \"YES\"\n\tj=-1\n\ts=\"\"\n\t$char_num[a]-=w\n\t?a.upto(?z){|c|s+=(c*$char_num[c])}\n\t1.upto(size+1){|k|\n\t\tprint flag[k] ? a : s[j+=1]\n\t}\n\tputs\nend\n"}, {"source_code": "str = gets\nstr = str.chop\nchararray = []\nprimeTable = [2]\n3.upto(1000) { |i|\n\tdividable = false\n\tprimeTable.each { |j|\n\t\tif i % j == 0\n\t\t\tdividable = true\n\t\t\tbreak\n\t\tend\n\t}\n\tprimeTable.push(i) if !dividable\n}\nstr.each_char {|i|\n\tchararray.push(i)\n}\nchararray.sort!\n\nequlationSet = []\n0.upto(str.length - 1) { |i|\n\ttimes = str.length / primeTable[i]\n\tbreak if(times <= 1)\n\t1.upto(times) { |j|\n\t\tequlationSet.push(primeTable[i] * j) if !equlationSet.include?(primeTable[i] * j)\n\t}\n}\nmaxCharacter = \"\"\nmaxNumber = 0\nnum = 0\ncurChar = chararray[0]\nchararray.each {|i|\n\tif(curChar != i)\n\t\tcurChar = i\n\t\tnum = 1\n\telse\n\t\tnum += 1\n\t\tif maxNumber < num\n\t\t\tmaxNumber = num\n\t\t\tmaxCharacter = curChar\n\t\tend\n\tend\n}\n\nif(maxNumber >= equlationSet.length)\n\tputs \"YES\"\n\tequlationSet.sort!\n\tresult = \"\"\n\tsetInd = 0\n\tcharInd = 0\n\t0.upto(str.length - 1) { |i|\n\t\tif equlationSet[setInd] == i + 1\n\t\t\tresult.concat(maxCharacter)\n\t\t\tsetInd += 1\n\t\telse\n\t\t\twhile str[charInd] == maxCharacter\n\t\t\t\tcharInd += 1\n\t\t\tend\n\t\t\tif(charInd < str.length)\n\t\t\t\tresult.concat(str[charInd])\n\t\t\t\tcharInd += 1\n\t\t\telse\n\t\t\t\tresult.concat(maxCharacter)\n\t\t\tend\n\t\tend\n\t}\n\tputs result\nelse\n\tputs \"NO\"\nend"}], "negative_code": [{"source_code": "str = gets\nstr = str.chop\nchararray = []\nprimeTable = [2]\n3.upto(1000) { |i|\n\tdividable = false\n\tprimeTable.each { |j|\n\t\tif i % j == 0\n\t\t\tdividable = true\n\t\t\tbreak\n\t\tend\n\t}\n\tprimeTable.push(i) if !dividable\n}\nstr.each_char {|i|\n\tchararray.push(i)\n}\nchararray.sort!\n\nequlationSet = []\n0.upto(str.length - 1) { |i|\n\ttimes = str.length / primeTable[i]\n\tbreak if(times <= 1)\n\t1.upto(times) { |j|\n\t\tequlationSet.push(primeTable[i] * j) if !equlationSet.include?(primeTable[i] * j)\n\t}\n}\nmaxCharacter = \"\"\nmaxNumber = 0\nnum = 0\ncurChar = chararray[0]\nchararray.each {|i|\n\tif(curChar != i)\n\t\tcurChar = i\n\t\tnum = 1\n\telse\n\t\tif maxNumber < num\n\t\t\tmaxNumber = num\n\t\t\tmaxCharacter = curChar\n\t\tend\n\t\tnum += 1\n\tend\n}\n\nif(maxNumber >= equlationSet.length)\n\tputs \"YES\"\n\tequlationSet.sort!\n\tresult = \"\"\n\tsetInd = 0\n\tcharInd = 0\n\t0.upto(str.length - 1) { |i|\n\t\tif equlationSet[setInd] == i + 1\n\t\t\tresult.concat(maxCharacter)\n\t\t\tsetInd += 1\n\t\telse\n\t\t\twhile str[charInd] == maxCharacter\n\t\t\t\tcharInd += 1\n\t\t\tend\n\t\t\tif(charInd < str.length)\n\t\t\t\tresult.concat(str[charInd])\n\t\t\t\tcharInd += 1\n\t\t\telse\n\t\t\t\tresult.concat(maxCharacter)\n\t\t\tend\n\t\tend\n\t}\n\tputs result\nelse\n\tputs \"NO\"\nend"}], "src_uid": "64700ca85ef3b7408d7d7ad1132f8f81"} {"nl": {"description": "Vasya decided to go to the grocery store. He found in his wallet $$$a$$$ coins of $$$1$$$ burle and $$$b$$$ coins of $$$2$$$ burles. He does not yet know the total cost of all goods, so help him find out $$$s$$$ ($$$s > 0$$$): the minimum positive integer amount of money he cannot pay without change or pay at all using only his coins.For example, if $$$a=1$$$ and $$$b=1$$$ (he has one $$$1$$$-burle coin and one $$$2$$$-burle coin), then: he can pay $$$1$$$ burle without change, paying with one $$$1$$$-burle coin, he can pay $$$2$$$ burle without change, paying with one $$$2$$$-burle coin, he can pay $$$3$$$ burle without change by paying with one $$$1$$$-burle coin and one $$$2$$$-burle coin, he cannot pay $$$4$$$ burle without change (moreover, he cannot pay this amount at all). So for $$$a=1$$$ and $$$b=1$$$ the answer is $$$s=4$$$.", "input_spec": "The first line of the input contains an integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases in the test. The description of each test case consists of one line containing two integers $$$a_i$$$ and $$$b_i$$$ ($$$0 \\le a_i, b_i \\le 10^8$$$) — the number of $$$1$$$-burle coins and $$$2$$$-burles coins Vasya has respectively.", "output_spec": "For each test case, on a separate line print one integer $$$s$$$ ($$$s > 0$$$): the minimum positive integer amount of money that Vasya cannot pay without change or pay at all.", "sample_inputs": ["5\n\n1 1\n\n4 0\n\n0 2\n\n0 0\n\n2314 2374"], "sample_outputs": ["4\n5\n1\n1\n7063"], "notes": "Note The first test case of the example is clarified into the main part of the statement. In the second test case, Vasya has only $$$1$$$ burle coins, and he can collect either any amount from $$$1$$$ to $$$4$$$, but $$$5$$$ can't. In the second test case, Vasya has only $$$2$$$ burle coins, and he cannot pay $$$1$$$ burle without change. In the fourth test case you don't have any coins, and he can't even pay $$$1$$$ burle. "}, "positive_code": [{"source_code": "Integer(gets).times do\r\n a, b = gets.split.map(&:to_i)\r\n if a == 0\r\n puts 1\r\n else\r\n puts a + b * 2 + 1\r\n end\r\nend"}, {"source_code": "t=gets.chomp.to_i\r\n\r\nt.times do\r\n\r\ns=gets.chomp.split\r\na=s[0].to_i\r\nb=s[1].to_i\r\nif (a==0 && b!=0)\r\n puts 1.to_i\r\nelse\r\n puts (a*1+b*2)+1\r\nend\r\nend"}], "negative_code": [], "src_uid": "2b6e670b602a89b467975edf5226219a"} {"nl": {"description": "You've got a undirected graph G, consisting of n nodes. We will consider the nodes of the graph indexed by integers from 1 to n. We know that each node of graph G is connected by edges with at least k other nodes of this graph. Your task is to find in the given graph a simple cycle of length of at least k + 1.A simple cycle of length d (d > 1) in graph G is a sequence of distinct graph nodes v1, v2, ..., vd such, that nodes v1 and vd are connected by an edge of the graph, also for any integer i (1 ≤ i < d) nodes vi and vi + 1 are connected by an edge of the graph.", "input_spec": "The first line contains three integers n, m, k (3 ≤ n, m ≤ 105; 2 ≤ k ≤ n - 1) — the number of the nodes of the graph, the number of the graph's edges and the lower limit on the degree of the graph node. Next m lines contain pairs of integers. The i-th line contains integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi) — the indexes of the graph nodes that are connected by the i-th edge. It is guaranteed that the given graph doesn't contain any multiple edges or self-loops. It is guaranteed that each node of the graph is connected by the edges with at least k other nodes of the graph.", "output_spec": "In the first line print integer r (r ≥ k + 1) — the length of the found cycle. In the next line print r distinct integers v1, v2, ..., vr (1 ≤ vi ≤ n) — the found simple cycle. It is guaranteed that the answer exists. If there are multiple correct answers, you are allowed to print any of them.", "sample_inputs": ["3 3 2\n1 2\n2 3\n3 1", "4 6 3\n4 3\n1 2\n1 3\n1 4\n2 3\n2 4"], "sample_outputs": ["3\n1 2 3", "4\n3 4 1 2"], "notes": null}, "positive_code": [{"source_code": "require 'set'\n\nn,m,k=gets.split.map(&:to_i)\ngraph=Array.new(n) {Set.new}\nm.times {\n a,b=gets.split.map {|i| i.to_i-1}\n graph[a]< 0\n\t\tif ar[i][k] > 0\n\t\t\tputs sprintf(\"%d %d\",i+1,j+1)\n\t\telse\n\t\t\tputs sprintf(\"%d %d\",j+1,i+1)\t\t\t\t\t\n\t\tend\n\t\texit\n\tend\t\nend\nputs sprintf(\"%d %d\",i+1,j+1)\n"}, {"source_code": "n = gets.to_i\n\ncomb = (1..n).to_a.combination(2).to_a\n\nlist = []\n\nwon = {}\nlost = {}\n\n\n(((n * (n-1))/2)-1).times do\n\tl = gets.split(\" \").map(&:to_i)\n\twon[l[0]] ||= []\n\twon[l[0]] << l[1]\n\n\tlost[l[1]] ||= []\n\tlost[l[1]] << l[0]\n\n\tlist << l.minmax\nend\n\nmissed = (comb - list).flatten\n# puts \"#{missed}\"\n# puts \"#{won[missed[0]]}\"\n# puts \"#{lost[missed[1]]}\"\nif ((won[missed[0]]||[]) & (lost[missed[1]] || [])).empty?\n\tmissed[0],missed[1] = missed[1],missed[0]\nend\nputs \"#{missed[0]} #{missed[1]}\""}], "negative_code": [{"source_code": "n = gets.to_i\nh = Hash.new\nfor i in 1..n\n\tfor j in i+1..n\n\t\th[i.to_s + \" \" + j.to_s] = 0\n\tend\nend\n\t\nfor i in 1..(n*(n-1)/2-1)\n\ts = gets.chomp\n\tas,bs = s.split(/ /)\n\ta = as.to_i\n\tb = bs.to_i\n\tif a > b\n\t\tma = a\n\t\tmb = b\n\telse\n\t\tma = b\n\t\tmb = a\n\tend\n\th.delete(mb.to_s + \" \" + ma.to_s)\nend\n\nh.each_key { |m|\n\tputs m\n}\n"}], "src_uid": "f1ac6da78aed11d9118a85182c5644de"} {"nl": {"description": "Someone gave Alyona an array containing n positive integers a1, a2, ..., an. In one operation, Alyona can choose any element of the array and decrease it, i.e. replace with any positive integer that is smaller than the current one. Alyona can repeat this operation as many times as she wants. In particular, she may not apply any operation to the array at all.Formally, after applying some operations Alyona will get an array of n positive integers b1, b2, ..., bn such that 1 ≤ bi ≤ ai for every 1 ≤ i ≤ n. Your task is to determine the maximum possible value of mex of this array.Mex of an array in this problem is the minimum positive integer that doesn't appear in this array. For example, mex of the array containing 1, 3 and 4 is equal to 2, while mex of the array containing 2, 3 and 2 is equal to 1.", "input_spec": "The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of elements in the Alyona's array. The second line of the input contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the array.", "output_spec": "Print one positive integer — the maximum possible value of mex of the array after Alyona applies some (possibly none) operations.", "sample_inputs": ["5\n1 3 3 3 6", "2\n2 1"], "sample_outputs": ["5", "3"], "notes": "NoteIn the first sample case if one will decrease the second element value to 2 and the fifth element value to 4 then the mex value of resulting array 1 2 3 3 4 will be equal to 5.To reach the answer to the second sample case one must not decrease any of the array elements."}, "positive_code": [{"source_code": "#!/usr/bin/ruby\nr=1\ngets;gets.split.map(&:to_i).sort.each{|e|e.to_i>=r&&r+=1}\np r"}, {"source_code": "n = gets.to_i\n\nnumbers = gets.split.map(&:to_i)\n\nnumbers.sort!\n\n\nif numbers.count == 1\n puts 2\nelse\n max = 1 \n numbers.each do |n|\n max = max + 1 if n >= max\n end\n puts max \nend "}, {"source_code": "#! /usr/bin/env ruby\n\nn=gets.to_i\na=gets.split.map{|v|v.to_i}.sort\nm=1\na.each {|v| m+=1 if v>=m }\nputs m"}, {"source_code": "gets\nm=1\na=gets.chomp.split.map(&:to_i).sort\na.each{|k| m+=1 if k>=m}\nprint(m)"}, {"source_code": "gets\nmex = 1\nnums = gets.split(' ').map(&:to_i).sort.each { |n|\n if n >= mex\n mex += 1\n end\n}\nputs mex\n"}, {"source_code": "n = gets.chomp.to_i\nas = gets.chomp.split.map(&:to_i)\n\nas.sort!\n\nmex = 1\nfor i in 0...n\n if mex <= as[i] \n as [i] = mex\n mex += 1\n end\nend\n\nputs mex\n"}, {"source_code": "p gets.to_i - gets.split.map(&:to_i).sort.map.with_index{|x,i| [i+1-x,0].max}.max + 1"}, {"source_code": "def read_line\n gets.chomp.split.map &:to_i\nend\n\nn = read_line[0]\na = read_line.sort\nx = 1\n\nn.times { |i| \n\tx = x+1 if x<=a[i]\n}\n\nputs x"}, {"source_code": "arrLength = gets.chomp.to_i\narr = gets.chomp.split(' ').map(&:to_i)\n\narr.sort!\ncurrentNum = 1\n\narr.each_with_index do |val, index|\n\tcurrentNum += 1 if val >= currentNum\nend\n\nputs currentNum"}], "negative_code": [{"source_code": "#!/usr/bin/ruby\nr=1\ngets;gets.split.each{|e|e.to_i>=r&&r+=1}\np r"}, {"source_code": "n = gets.to_i\n\nnumbers = gets.split.map(&:to_i)\n\nnumbers.sort!\n\n\nif numbers.count == 1\n puts 2\nelse\n max = 1 \n numbers.each do |n|\n if n > max + 1\n max = max + 1 \n else\n max = n\n end\n end\n puts max + 1\nend"}, {"source_code": "n = gets.to_i\n\nnumbers = gets.split.map(&:to_i)\n\nnumbers.sort!\n\nif numbers[0] == numbers[-1]\n puts numbers[0] + 1\nelsif numbers[0] > 1\n puts numbers.count + 1\nelse\n max = 1 \n numbers.each do |n|\n if n > max + 1\n max = max + 1 \n else\n max = n\n end\n end\n puts max + 1\nend"}, {"source_code": "n = gets.to_i\n\nnumbers = gets.split.map(&:to_i)\n\nnumbers.sort!\n\nif numbers[0] > 1\n puts 2\nelse\n max = 1 \n numbers.each do |n|\n if n > max + 1\n max = max + 1 \n else\n max = n\n end\n end\n puts max + 1\nend"}, {"source_code": "n = gets.to_i\n\nnumbers = gets.split.map(&:to_i)\n\nnumbers.sort!\n\nif numbers[0] > 1 && numbers.count != 1 && numbers[0] == numbers[-1]\n puts 3\nelsif numbers[0] > 1\n puts numbers.count + 1\nelse\n max = 1 \n numbers.each do |n|\n if n > max + 1\n max = max + 1 \n else\n max = n\n end\n end\n puts max + 1\nend"}, {"source_code": "n = gets.to_i\n\nnumbers = gets.split.map(&:to_i)\n\nnumbers.sort!\n\nif numbers.count != 1 && numbers[0] == numbers[-1]\n puts numbers[0] + 1\nelsif numbers[0] > 1\n puts numbers.count + 1\nelse\n max = 1 \n numbers.each do |n|\n if n > max + 1\n max = max + 1 \n else\n max = n\n end\n end\n puts max + 1\nend"}, {"source_code": "n = gets.to_i\n\nnumbers = gets.split.map(&:to_i)\n\nnumbers.sort!\n\nif numbers[0] > 1\n puts numbers.count + 1\nelse\n max = 1 \n numbers.each do |n|\n if n > max + 1\n max = max + 1 \n else\n max = n\n end\n end\n puts max + 1\nend"}, {"source_code": "n = gets.to_i\n\nnumbers = gets.split.map(&:to_i)\n\nnumbers.sort!\n\nif numbers[0] > 1\n puts numbers[0] - 1\nelse\n max = 1 \n numbers.each do |n|\n if n > max + 1\n max = max + 1 \n else\n max = n\n end\n end\n puts max + 1\nend"}, {"source_code": "n = gets.to_i\n\nnumbers = gets.split.map(&:to_i)\n\nnumbers.sort!\n\nif numbers[0] == numbers[-1]\n puts numbers[0] + 1\nelsif numbers[0] > 1\n puts numbers.count + 1\nelse\n max = 1 \n numbers.each do |n|\n if n > max + 1\n max = max + 1 \n else\n max = n\n end\n puts max\n end\n puts max + 1\nend"}, {"source_code": "n = gets.to_i\n\nnumbers = gets.split.map(&:to_i)\n\nnumbers.sort!\n\n\nif numbers[0] > 1 || numbers[0] == numbers[-1]\n puts numbers.count + 1\nelse\n max = 1 \n numbers.each do |n|\n if n > max + 1\n max = max + 1 \n else\n max = n\n end\n end\n puts max + 1\nend"}, {"source_code": "n = gets.to_i\n\nnumbers = gets.split.map(&:to_i)\n\nnumbers.sort!\n\n\n# if numbers[0] > 1\n# puts numbers.count + 1\n# else\n max = 1 \n numbers.each do |n|\n if n > max + 1\n max = max + 1 \n else\n max = n\n end\n end\n puts max + 1\n# end"}, {"source_code": "n = gets.to_i\n\nnumbers = gets.split.map(&:to_i)\n\nnumbers.sort!\n\n\nif numbers.count == 1\n puts 2\nelse\n max = 1 \n numbers.each do |n|\n puts max\n if n > max + 1\n max = max + 1 \n else\n max = n\n end\n end\n puts max + 1\nend"}, {"source_code": "n = gets.to_i\n\nnumbers = gets.split.map(&:to_i)\n\nnumbers.sort!\n\nif numbers.count != 1 && numbers[0] == numbers[-1]\n puts 3\nelsif numbers[0] > 1\n puts numbers.count + 1\nelse\n max = 1 \n numbers.each do |n|\n if n > max + 1\n max = max + 1 \n else\n max = n\n end\n end\n puts max + 1\nend"}, {"source_code": "gets\nm=1\na=gets.chomp.split.map(&:to_i)\na.each{|k| m+=1 if k>=m}\nprint(m)"}, {"source_code": "gets\nm=1\nfor k in gets.split.sort\nif k.to_i>=m\n m=m+1\nend\nend\nprint(m)"}], "src_uid": "482b3ebbbadd583301f047181c988114"} {"nl": {"description": "Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — number of test cases. Next $$$2 \\cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le n \\le 100$$$, $$$0 \\le k \\le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.", "output_spec": "For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.", "sample_inputs": ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"], "sample_outputs": ["2\n2\n1\n1"], "notes": "NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab."}, "positive_code": [{"source_code": "class Array\r\n def n\r\n return length\r\n end\r\n def z\r\n\treturn length-1\r\nend\r\nend\r\n\r\n$__cur_readed = []\r\n$__cur_index = 0\r\n\r\ndef rs\r\n\twhile $__cur_index >= $__cur_readed.length\r\n\t\t$__cur_readed = gets.split(\" \")\r\n\t\t$__cur_index = 0\r\n\tend\r\n\t$__cur_index+=1\r\n\treturn $__cur_readed[$__cur_index-1]\r\nend\r\n\r\ndef ri\t\r\n\trs.to_i\r\nend\r\n\r\ndef ra2 n\r\n\tArray.new(n) {ri}\r\nend\r\n\r\ndef ra\r\n\tra2 ri\r\nend\r\n\r\nt = ri\r\n\r\nfor _ in (1..t)\r\n\tri\r\n\tk = ri\r\n\ts = rs\r\n\tres = 1\r\n\tif k > 0\r\n\t\tif s != s.reverse\r\n\t\t\tres = 2\r\n\t\tend\r\n\tend\r\n\tputs res\r\nend"}, {"source_code": "case_count = gets.to_i\r\n\r\ncase_count.times do\r\n n, k = gets.chomp.split(\" \").map(&:to_i)\r\n str = gets.chomp\r\n\r\n if k.zero?\r\n puts 1\r\n else\r\n puts str == str.reverse ? 1 : 2\r\n end\r\nend\r\n"}, {"source_code": "gets.to_i.times do\n n, k = gets.split.map(&:to_i)\n s = gets.strip\n\n if k == 0 || s == s.reverse\n puts 1\n else\n puts 2\n end\nend\n"}], "negative_code": [{"source_code": "case_count = gets.to_i\r\n\r\ncase_count.times do\r\n n, k = gets.split(\" \").map(&:to_i)\r\n str = gets\r\n\r\n if k.zero?\r\n puts 1\r\n else\r\n puts str == str.reverse ? 1 : 2\r\n end\r\nend\r\n"}, {"source_code": "case_count = gets.to_i\r\n\r\ncase_count.times do\r\n n, k = gets.split(\" \").map(&:to_i)\r\n str = gets\r\n\r\n puts str == str.reverse ? 1 : 2\r\nend\r\n"}], "src_uid": "08cd22b8ee760a9d2dacb0d050dcf37a"} {"nl": {"description": "It is well known that the planet suffers from the energy crisis. Little Petya doesn't like that and wants to save the world. For this purpose he needs every accumulator to contain the same amount of energy. Initially every accumulator has some amount of energy: the i-th accumulator has ai units of energy. Energy can be transferred from one accumulator to the other. Every time x units of energy are transferred (x is not necessarily an integer) k percent of it is lost. That is, if x units were transferred from one accumulator to the other, amount of energy in the first one decreased by x units and in other increased by units.Your task is to help Petya find what maximum equal amount of energy can be stored in each accumulator after the transfers.", "input_spec": "First line of the input contains two integers n and k (1 ≤ n ≤ 10000, 0 ≤ k ≤ 99) — number of accumulators and the percent of energy that is lost during transfers. Next line contains n integers a1, a2, ... , an — amounts of energy in the first, second, .., n-th accumulator respectively (0 ≤ ai ≤ 1000, 1 ≤ i ≤ n).", "output_spec": "Output maximum possible amount of energy that can remain in each of accumulators after the transfers of energy. The absolute or relative error in the answer should not exceed 10 - 6.", "sample_inputs": ["3 50\n4 2 1", "2 90\n1 11"], "sample_outputs": ["2.000000000", "1.909090909"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/ruby\n\nn,k = gets.split.map(&:to_i)\narr = gets.split.map(&:to_i)\n\nleft = arr.min.to_f\nright = arr.max.to_f\n\nwhile right-left > 1e-6\n\tmid = (left+right)/2\n\tupper,lower = arr.partition { |v| v > mid }\n\tx = upper.map { |v| v - mid }.inject(&:+)\n\tl = lower.map { |v| v - mid }.inject(&:+)\n\n\tif l+x-x*k/100 >= 0\n\t\tleft = mid\n\telse\n\t\tright = mid\n\tend\nend\n\nprintf \"%.7f\\n\", (left+right)/2\n"}, {"source_code": "def rest(a, pr, x)\n a.inject(0){|r,j| r + (j>x ? (j-x)*pr : j-x)}\nend\n\ndef solve(a, k)\n x0 = a.min.to_f\n x1 = a.max.to_f\n\n y0 = rest(a, 1-k/100.0, x0)\n return x0 if y0 == 0\n\n y1 = rest(a, 1-k/100.0, x1)\n return x1 if y1 == 0\n\n while x1-x0 > 1e-8\n x2 = (x0+x1)/2\n y2 = rest(a, 1-k/100.0, x2)\n return x2 if y2 == 0\n\n if y2 < 0\n x1 = x2\n else\n x0 = x2\n end\n end\n\n x0\nend\n\nn, k = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\n\n#srand(123)\n#a = 10000.times.map{rand(1000)}\n\np solve(a, k).to_f\n"}], "negative_code": [], "src_uid": "9fd09b05d0e49236dca45615e684ad85"} {"nl": {"description": "Let's call an array of non-negative integers $$$a_1, a_2, \\ldots, a_n$$$ a $$$k$$$-extension for some non-negative integer $$$k$$$ if for all possible pairs of indices $$$1 \\leq i, j \\leq n$$$ the inequality $$$k \\cdot |i - j| \\leq min(a_i, a_j)$$$ is satisfied. The expansion coefficient of the array $$$a$$$ is the maximal integer $$$k$$$ such that the array $$$a$$$ is a $$$k$$$-extension. Any array is a 0-expansion, so the expansion coefficient always exists.You are given an array of non-negative integers $$$a_1, a_2, \\ldots, a_n$$$. Find its expansion coefficient.", "input_spec": "The first line contains one positive integer $$$n$$$ — the number of elements in the array $$$a$$$ ($$$2 \\leq n \\leq 300\\,000$$$). The next line contains $$$n$$$ non-negative integers $$$a_1, a_2, \\ldots, a_n$$$, separated by spaces ($$$0 \\leq a_i \\leq 10^9$$$).", "output_spec": "Print one non-negative integer — expansion coefficient of the array $$$a_1, a_2, \\ldots, a_n$$$.", "sample_inputs": ["4\n6 4 5 5", "3\n0 1 2", "4\n821 500 479 717"], "sample_outputs": ["1", "0", "239"], "notes": "NoteIn the first test, the expansion coefficient of the array $$$[6, 4, 5, 5]$$$ is equal to $$$1$$$ because $$$|i-j| \\leq min(a_i, a_j)$$$, because all elements of the array satisfy $$$a_i \\geq 3$$$. On the other hand, this array isn't a $$$2$$$-extension, because $$$6 = 2 \\cdot |1 - 4| \\leq min(a_1, a_4) = 5$$$ is false.In the second test, the expansion coefficient of the array $$$[0, 1, 2]$$$ is equal to $$$0$$$ because this array is not a $$$1$$$-extension, but it is $$$0$$$-extension."}, "positive_code": [{"source_code": "def min(a, b) ; return a < b ? a : b ; end\nN = gets.to_i\narr = gets.split.map(&:to_i)\nk = min(arr[0], arr[-1]) / (N-1)\nfor h in 1...arr.size-1\n\ttmp = min(arr[0], arr[h]) / h\n\tk = tmp if k > tmp\n\ttmp = min(arr[h], arr[-1])/ (N-1 - h)\n\tk = tmp if k > tmp\n#\tputs k\nend\nputs k\n"}], "negative_code": [{"source_code": "def min a, b ; return a < b ? a : b ; end\nN = gets.to_i\narr = gets.split.map(&:to_i)\nk = min(arr[0], arr[-1]) / (N-1)\nMIN = arr.min\nif MIN == 0\n\tputs 0\n\texit\nend\nindexMIN = arr.index(MIN)\nif indexMIN==0 || indexMIN==N-1\n\tputs MIN / (N-1)\n\texit\nend\ntmp = MIN / indexMIN\nk = tmp if k > tmp\ntmp = MIN / (N-1 - indexMIN)\nk = tmp if k > tmp\nputs k\n"}], "src_uid": "f146808eb6e0ee04d531e7222a53d275"} {"nl": {"description": "You are given an array $$$a$$$ of $$$n$$$ integers, where $$$n$$$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $$$a_i$$$) and increase it by $$$1$$$ (that is, replace it with $$$a_i + 1$$$). You want to make the median of the array the largest possible using at most $$$k$$$ operations.The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $$$[1, 5, 2, 3, 5]$$$ is $$$3$$$.", "input_spec": "The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$, $$$n$$$ is odd, $$$1 \\le k \\le 10^9$$$) — the number of elements in the array and the largest number of operations you can make. The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$).", "output_spec": "Print a single integer — the maximum possible median after the operations.", "sample_inputs": ["3 2\n1 3 5", "5 5\n1 2 1 1 1", "7 7\n4 1 2 4 3 4 4"], "sample_outputs": ["5", "3", "5"], "notes": "NoteIn the first example, you can increase the second element twice. Than array will be $$$[1, 5, 5]$$$ and it's median is $$$5$$$.In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $$$3$$$.In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $$$[5, 1, 2, 5, 3, 5, 5]$$$ and the median will be $$$5$$$."}, "positive_code": [{"source_code": "n, k = gets.split.map(&:to_i)\na = gets.split.map(&:to_i).sort.drop(n / 2)\n\nif n == 1\n puts a.first + k\n exit\nend\n\nputs (\n loop do\n i = a.index{ |i| i > a[0] }\n break a[0] + k / a.size unless i\n break a[0] + k / i unless i * (a[i] - a[0]) < k\n (0...i).each do |j|\n k -= a[i] - a[j]\n a[j] = a[i]\n end\n end\n)\n"}], "negative_code": [{"source_code": "n, k = gets.split.map(&:to_i)\na = gets.split.map(&:to_i).sort.drop(n / 2)\n\nif n == 1\n puts a.first + k\n exit\nend\n\nputs (\n loop do\n i = a.index{ |i| i > a[0] }\n break a[0] + k / a.size unless i\n break a[0] + k / i unless i * (a[i] - a[0]) < k\n (0...i).each do |j|\n k -= a[i] - a[0]\n a[j] = a[i]\n end\n end\n)\n"}], "src_uid": "0efd4b05b3e2e3bc80c93d37508a5197"} {"nl": {"description": "An atom of element X can exist in n distinct states with energies E1 < E2 < ... < En. Arkady wants to build a laser on this element, using a three-level scheme. Here is a simplified description of the scheme. Three distinct states i, j and k are selected, where i < j < k. After that the following process happens: initially the atom is in the state i, we spend Ek - Ei energy to put the atom in the state k, the atom emits a photon with useful energy Ek - Ej and changes its state to the state j, the atom spontaneously changes its state to the state i, losing energy Ej - Ei, the process repeats from step 1. Let's define the energy conversion efficiency as , i. e. the ration between the useful energy of the photon and spent energy.Due to some limitations, Arkady can only choose such three states that Ek - Ei ≤ U.Help Arkady to find such the maximum possible energy conversion efficiency within the above constraints.", "input_spec": "The first line contains two integers n and U (3 ≤ n ≤ 105, 1 ≤ U ≤ 109) — the number of states and the maximum possible difference between Ek and Ei. The second line contains a sequence of integers E1, E2, ..., En (1 ≤ E1 < E2... < En ≤ 109). It is guaranteed that all Ei are given in increasing order.", "output_spec": "If it is not possible to choose three states that satisfy all constraints, print -1. Otherwise, print one real number η — the maximum possible energy conversion efficiency. Your answer is considered correct its absolute or relative error does not exceed 10 - 9. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if .", "sample_inputs": ["4 4\n1 3 5 7", "10 8\n10 13 15 16 17 19 20 22 24 25", "3 1\n2 5 10"], "sample_outputs": ["0.5", "0.875", "-1"], "notes": "NoteIn the first example choose states 1, 2 and 3, so that the energy conversion efficiency becomes equal to .In the second example choose states 4, 5 and 9, so that the energy conversion efficiency becomes equal to ."}, "positive_code": [{"source_code": "n, u = gets.chomp.split(\" \").map {|e| e.to_i}\narr = gets.chomp.split(\" \").map {|e| e.to_i}\n\ni = 0\nj = 1\nll = arr.length\nli = ll - 1\nmaxn = -1\nwhile i < li\n\toldj = j\n\twhile (j < ll && arr[j] - arr[i] <= u)\n\t\tj += 1\n\tend\n\tj -= 1 if j != oldj\n\tif (j - i > 1)\n\t\tme = (arr[j] - arr[i+1])/(arr[j] - arr[i]).to_f\n\t\tif me > maxn\n\t\t\t# puts arr[j], arr[i]\n\t\t\tmaxn = me\n\t\tend\n\tend\n\ti += 1\nend\nputs maxn"}], "negative_code": [{"source_code": "n, u = gets.chomp.split(\" \").map {|e| e.to_i}\narr = gets.chomp.split(\" \").map {|e| e.to_i}\n\ni = 0\nj = 1\nll = arr.length\nli = ll - 1\nmaxn = -1\nsmd = 10**9\nwhile i < li\n\twhile (j < ll && arr[j] - arr[i] <= u)\n\t\tj += 1\n\tend\n\tj -= 1\n\tif (j - i > 1)\n\t\tif (arr[i+1] - arr[i] <= smd)\n\t\t\tsmd = arr[i+1] - arr[i]\n\t\t\tme = (arr[j] - arr[i+1])/(arr[j] - arr[i]).to_f\n\t\t\tmaxn = me if me > maxn\n\t\tend\n\tend\n\ti += 1\nend\nputs maxn"}, {"source_code": "n, u = gets.chomp.split(\" \").map {|e| e.to_i}\narr = gets.chomp.split(\" \").map {|e| e.to_i}\n\ni = 0\nj = 1\nll = arr.length\nli = ll - 1\nmaxn = -1\nsmd = 10**9\nwhile i < li\n\toldj = j\n\twhile (j < ll && arr[j] - arr[i] <= u)\n\t\tj += 1\n\tend\n\tj -= 1 if j != oldj\n\tif (j - i > 1)\n\t\tif (arr[i+1] - arr[i] <= smd)\n\t\t\tsmd = arr[i+1] - arr[i]\n\t\t\tme = (arr[j] - arr[i+1])/(arr[j] - arr[i]).to_f\n\t\t\tmaxn = me if me > maxn\n\t\tend\n\tend\n\ti += 1\nend\nputs maxn"}], "src_uid": "74ed99af5a5ed51c73d68f7d4ff2c70e"} {"nl": {"description": "One very well-known internet resource site (let's call it X) has come up with a New Year adventure. Specifically, they decided to give ratings to all visitors.There are n users on the site, for each user we know the rating value he wants to get as a New Year Present. We know that user i wants to get at least ai rating units as a present.The X site is administered by very creative and thrifty people. On the one hand, they want to give distinct ratings and on the other hand, the total sum of the ratings in the present must be as small as possible.Help site X cope with the challenging task of rating distribution. Find the optimal distribution.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 3·105) — the number of users on the site. The next line contains integer sequence a1, a2, ..., an (1 ≤ ai ≤ 109).", "output_spec": "Print a sequence of integers b1, b2, ..., bn. Number bi means that user i gets bi of rating as a present. The printed sequence must meet the problem conditions. If there are multiple optimal solutions, print any of them.", "sample_inputs": ["3\n5 1 1", "1\n1000000000"], "sample_outputs": ["5 1 2", "1000000000"], "notes": null}, "positive_code": [{"source_code": "n=gets.to_i\na=gets.split.each_with_index.map{|c,i|[c.to_i,i]}\na.sort_by!{|x,i|x}\nq=-1\nb=[0]*n\na.each do|x,i|\n q=x if q time_range\n min_time_range = time_range\n end\n }\n\n found_times << min_time_range\n}\n\nfound_times.each { |time|\n print_time(time)\n}"}], "negative_code": [], "src_uid": "ce0579e9c5b4c157bc89103c76ddd4c3"} {"nl": {"description": "A sequence $$$(b_1, b_2, \\ldots, b_k)$$$ is called strange, if the absolute difference between any pair of its elements is greater than or equal to the maximum element in the sequence. Formally speaking, it's strange if for every pair $$$(i, j)$$$ with $$$1 \\le i<j \\le k$$$, we have $$$|a_i-a_j|\\geq MAX$$$, where $$$MAX$$$ is the largest element of the sequence. In particular, any sequence of length at most $$$1$$$ is strange.For example, the sequences $$$(-2021, -1, -1, -1)$$$ and $$$(-1, 0, 1)$$$ are strange, but $$$(3, 0, 1)$$$ is not, because $$$|0 - 1| < 3$$$.Sifid has an array $$$a$$$ of $$$n$$$ integers. Sifid likes everything big, so among all the strange subsequences of $$$a$$$, he wants to find the length of the longest one. Can you help him?A sequence $$$c$$$ is a subsequence of an array $$$d$$$ if $$$c$$$ can be obtained from $$$d$$$ by deletion of several (possibly, zero or all) elements.", "input_spec": "The first line contains an integer $$$t$$$ $$$(1\\le t\\le 10^4)$$$ — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1\\le n\\le 10^5)$$$ — the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ $$$(-10^9\\le a_i \\le 10^9)$$$ — the elements of the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.", "output_spec": "For each test case output a single integer — the length of the longest strange subsequence of $$$a$$$.", "sample_inputs": ["6\n4\n-1 -2 0 0\n7\n-3 4 -2 0 -4 6 1\n5\n0 5 -3 2 -5\n3\n2 3 1\n4\n-3 0 2 0\n6\n-3 -2 -1 1 1 1"], "sample_outputs": ["4\n5\n4\n1\n3\n4"], "notes": "NoteIn the first test case, one of the longest strange subsequences is $$$(a_1, a_2, a_3, a_4)$$$In the second test case, one of the longest strange subsequences is $$$(a_1, a_3, a_4, a_5, a_7)$$$.In the third test case, one of the longest strange subsequences is $$$(a_1, a_3, a_4, a_5)$$$.In the fourth test case, one of the longest strange subsequences is $$$(a_2)$$$.In the fifth test case, one of the longest strange subsequences is $$$(a_1, a_2, a_4)$$$."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n gets\r\n xs, ys = gets.split.map(&:to_i).sort.partition { |n| n <= 0 }\r\n p xs.count + (ys.size > 0 && (xs + ys.take(1)).each_cons(2).all? { |a, b| (a - b).abs >= ys[0] } ? 1 : 0)\r\nend\r\n"}, {"source_code": "t=gets.to_i\r\nt.times{\r\nn=gets.to_i\r\ni=gets.split.map &:to_i\r\ni.sort!\r\nlast=-1e18\r\ncnt=0\r\nmn=1e18\r\ni.each{|num|\r\nmn=[mn,num-last].min if last != -1e18\r\nlast=num\r\nbreak if num>0\r\ncnt+=1\r\n}\r\np cnt < n && last <= mn ? cnt + 1 : cnt\r\n}"}, {"source_code": "t = gets.to_s.to_i\n1.upto(t) do |casenum|\n n = gets.to_s.to_i\n x = gets.to_s.split.map {|x| x.to_i }.sort\n a = x.select {|x| x <= 0}\n b = x.select {|x| x > 0}\n\n mindiff = 10 ** 15\n 1.upto(a.size - 1) do |i|\n diff = a[i] - a[i - 1]\n mindiff = diff if diff < mindiff\n end\n\n if b.size != 0 && b[0] <= mindiff\n printf(\"%d\\n\", a.size + 1)\n else\n printf(\"%d\\n\", a.size)\n end\nend\n"}, {"source_code": "INF = 10**18\r\n\r\ndef solve\r\n n = gets.to_i\r\n a = gets.split.map(&:to_i).sort\r\n diff_min = INF\r\n prev = -INF\r\n count = 0\r\n a.each do |x|\r\n diff = x - prev\r\n diff_min = diff if diff_min > diff\r\n break if diff_min < x\r\n count += 1\r\n prev = x\r\n end\r\n puts count\r\nend\r\n\r\nt = gets.to_i\r\nt.times do\r\n solve\r\nend"}], "negative_code": [{"source_code": "gets.to_i.times do\r\n gets\r\n xs, ys = gets.split.map(&:to_i).sort.partition { |n| n <= 0 }\r\n p xs.count + (xs.size <= 1 && ys.size > 0 || (xs.size >= 2 && ys.size >= 1 && (xs[-1] - xs[-2]).abs >= ys[0]) ? 1 : 0)\r\nend\r\n"}, {"source_code": "gets.to_i.times do\r\n gets\r\n xs, ys = gets.split.map(&:to_i).sort.partition { |n| n <= 0 }\r\n p xs.count + (xs.size <= 1 && ys.size > 0 || (xs.size >= 2 && ys.size >= 1 && (xs[-1] - xs[-2]).abs > ys[0]) ? 1 : 0)\r\nend\r\n"}, {"source_code": "gets.to_i.times do\r\n gets\r\n xs, ys = gets.split.map(&:to_i).sort.partition { |n| n <= 0 }\r\n p xs.count + (xs.size <= 1 && ys.size > 0 || (xs.size >= 2 && ys.size >= 1 && (xs[-1] + xs[-2]).abs > ys[0]) ? 1 : 0)\r\nend\r\n"}, {"source_code": "gets.to_i.times do\r\n gets\r\n xs, ys = gets.split.map(&:to_i).sort.partition { |n| n <= 0 }\r\n p xs.count + (xs.size == 0 || (xs.size >= 2 && ys.size >= 1 && (xs[-1] + xs[-2]).abs > ys[0]) ? 1 : 0)\r\nend\r\n"}], "src_uid": "a4b170cc058c485a50bf18982fd96851"} {"nl": {"description": "You are given an array $$$a$$$ of length $$$n$$$, which initially is a permutation of numbers from $$$1$$$ to $$$n$$$. In one operation, you can choose an index $$$i$$$ ($$$1 \\leq i < n$$$) such that $$$a_i < a_{i + 1}$$$, and remove either $$$a_i$$$ or $$$a_{i + 1}$$$ from the array (after the removal, the remaining parts are concatenated). For example, if you have the array $$$[1, 3, 2]$$$, you can choose $$$i = 1$$$ (since $$$a_1 = 1 < a_2 = 3$$$), then either remove $$$a_1$$$ which gives the new array $$$[3, 2]$$$, or remove $$$a_2$$$ which gives the new array $$$[1, 2]$$$.Is it possible to make the length of this array equal to $$$1$$$ with these operations?", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 2 \\cdot 10^4$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \\leq n \\leq 3 \\cdot 10^5$$$)  — the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 \\leq a_i \\leq n$$$, $$$a_i$$$ are pairwise distinct) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$3 \\cdot 10^5$$$.", "output_spec": "For each test case, output on a single line the word \"YES\" if it is possible to reduce the array to a single element using the aforementioned operation, or \"NO\" if it is impossible to do so.", "sample_inputs": ["4\n3\n1 2 3\n4\n3 1 2 4\n3\n2 3 1\n6\n2 4 6 1 3 5"], "sample_outputs": ["YES\nYES\nNO\nYES"], "notes": "NoteFor the first two test cases and the fourth test case, we can operate as follow (the bolded elements are the pair chosen for that operation):$$$[\\text{1}, \\textbf{2}, \\textbf{3}] \\rightarrow [\\textbf{1}, \\textbf{2}] \\rightarrow [\\text{1}]$$$$$$[\\text{3}, \\textbf{1}, \\textbf{2}, \\text{4}] \\rightarrow [\\text{3}, \\textbf{1}, \\textbf{4}] \\rightarrow [\\textbf{3}, \\textbf{4}] \\rightarrow [\\text{4}]$$$$$$[\\textbf{2}, \\textbf{4}, \\text{6}, \\text{1}, \\text{3}, \\text{5}] \\rightarrow [\\textbf{4}, \\textbf{6}, \\text{1}, \\text{3}, \\text{5}] \\rightarrow [\\text{4}, \\text{1}, \\textbf{3}, \\textbf{5}] \\rightarrow [\\text{4}, \\textbf{1}, \\textbf{5}] \\rightarrow [\\textbf{4}, \\textbf{5}] \\rightarrow [\\text{4}]$$$"}, "positive_code": [{"source_code": "t = gets.to_i\n(1..t).each do |i|\n n = gets.to_i\n a = gets.strip.split.map(&:to_i)\n puts \"YES\" if(a[0] < a[n-1])\n puts \"NO\" if(a[0] > a[n-1])\nend\n"}], "negative_code": [], "src_uid": "192f181cfc74bef5b0b5a192964d9760"} {"nl": {"description": "Petya is the most responsible worker in the Research Institute. So he was asked to make a very important experiment: to melt the chocolate bar with a new laser device. The device consists of a rectangular field of n × m cells and a robotic arm. Each cell of the field is a 1 × 1 square. The robotic arm has two lasers pointed at the field perpendicularly to its surface. At any one time lasers are pointed at the centres of some two cells. Since the lasers are on the robotic hand, their movements are synchronized — if you move one of the lasers by a vector, another one moves by the same vector.The following facts about the experiment are known: initially the whole field is covered with a chocolate bar of the size n × m, both lasers are located above the field and are active; the chocolate melts within one cell of the field at which the laser is pointed; all moves of the robotic arm should be parallel to the sides of the field, after each move the lasers should be pointed at the centres of some two cells; at any one time both lasers should be pointed at the field. Petya doesn't want to become a second Gordon Freeman. You are given n, m and the cells (x1, y1) and (x2, y2), where the lasers are initially pointed at (xi is a column number, yi is a row number). Rows are numbered from 1 to m from top to bottom and columns are numbered from 1 to n from left to right. You are to find the amount of cells of the field on which the chocolate can't be melted in the given conditions.", "input_spec": "The first line contains one integer number t (1 ≤ t ≤ 10000) — the number of test sets. Each of the following t lines describes one test set. Each line contains integer numbers n, m, x1, y1, x2, y2, separated by a space (2 ≤ n, m ≤ 109, 1 ≤ x1, x2 ≤ n, 1 ≤ y1, y2 ≤ m). Cells (x1, y1) and (x2, y2) are distinct.", "output_spec": "Each of the t lines of the output should contain the answer to the corresponding input test set.", "sample_inputs": ["2\n4 4 1 1 3 3\n4 3 1 1 2 2"], "sample_outputs": ["8\n2"], "notes": null}, "positive_code": [{"source_code": "def min(x, y)\n x < y ? x : y\nend\n\ndef max(x, y)\n x > y ? x : y\nend\n\nclass Rectangle\n attr_reader :x1, :y1, :x2, :y2\n \n def initialize(x1, y1, x2, y2)\n @x1, @y1, @x2, @y2 = x1, y1, x2, y2\n end\n\n def get_s\n if @x1 <= @x2 and @y1 <= @y2\n return (@x1 - @x2).abs * (@y1 - @y2).abs\n else\n return 0\n end\n end\n\n def self.intersect(a, b)\n x1, x2 = max(a.x1, b.x1), min(a.x2, b.x2)\n y1, y2 = max(a.y1, b.y1), min(a.y2, b.y2)\n return Rectangle.new(x1, y1, x2, y2)\n end\nend\n\ndef make_rect(n, m, x1, y1, x2, y2)\n x, y = max(0, x1 - x2), max(0, y1 - y2)\n width, height = n - (x1 - x2).abs, m - (y1 - y2).abs\n Rectangle.new(x, y, x + width, y + height)\nend\n\ngets.to_i.times do\n n, m, x1, y1, x2, y2 = gets.split.map { |v| v.to_i }\n a = make_rect(n, m, x1, y1, x2, y2)\n b = make_rect(n, m, x2, y2, x1, y1)\n c = Rectangle.new(0, 0, n, m)\n d = Rectangle.intersect(a, b)\n puts d.get_s + c.get_s - a.get_s - b.get_s\nend\n"}, {"source_code": "class Rectangle\n\tattr_reader :x1, :y1, :x2, :y2\n\n\tdef initialize(x1, y1, x2, y2)\n\t\t@x1, @y1, @x2, @y2 = x1, y1, x2, y2\n\tend\n\n\tdef get_area\n\t\tif @x1 <= @x2 and @y1 <= @y2\n\t\t\treturn (@x2 - @x1) * (@y2 - @y1)\n\t\telse\n\t\t\treturn 0\n\t\tend\n\tend\n\n\tdef self.intersect(rect1, rect2)\n\t\tx1, x2 = [rect1.x1, rect2.x1].max, [rect1.x2, rect2.x2].min\n \ty1, y2 = [rect1.y1, rect2.y1].max, [rect1.y2, rect2.y2].min\n \treturn Rectangle.new(x1, y1, x2, y2)\n\tend\nend\n\ndef make_rectangle(n, m, x1, y1, x2, y2)\n\tx = [0, x1 - x2].max\n\ty = [0, y1 - y2].max\n\tx2 = x + n - (x1 - x2).abs\n\ty2 = y + m - (y1 - y2).abs\n\treturn Rectangle.new(x, y, x2, y2)\nend\n\ngets.to_i.times do\n\tn, m, x1, y1, x2, y2 = gets.chomp!.split.map(&:to_i)\n\trect1 = make_rectangle(n, m, x1, y1, x2, y2)\n\trect2 = make_rectangle(n, m, x2, y2, x1, y1)\n\tintersection = Rectangle.intersect(rect1, rect2)\n\ttotal_pieces = Rectangle.new(0, 0, n, m)\n\tputs total_pieces.get_area - rect1.get_area - rect2.get_area + intersection.get_area\nend"}], "negative_code": [{"source_code": "def min(x, y)\n x < y ? x : y\nend\n\ndef max(x, y)\n x > y ? x : y\nend\n\nclass Rectangle\n attr_reader :x1, :y1, :x2, :y2\n \n def initialize(x1, y1, x2, y2)\n @x1, @x2 = min(x1, x2), max(x1, x2)\n @y1, @y2 = min(y1, y2), max(y1, y2)\n end\n\n def get_s\n if @x1 <= @x2 and @y1 <= @y2\n return (@x1 - @x2).abs * (@y1 - @y2).abs\n else\n return 0\n end\n end\n\n def self.intersect(a, b)\n x1, x2 = max(a.x1, b.x1), min(a.x2, b.x2)\n y1, y2 = max(a.y1, b.y1), min(a.y2, b.y2)\n return Rectangle.new(x1, y1, x2, y2)\n end\nend\n\ndef make_rect(n, m, x1, y1, x2, y2)\n x, y = max(0, x1 - x2), max(0, y1 - y2)\n width, height = n - (x1 - x2).abs, m - (y1 - y2).abs\n Rectangle.new(x, y, x + width, y + height)\nend\n\ngets.to_i.times do\n n, m, x1, y1, x2, y2 = gets.split.map { |v| v.to_i }\n a = make_rect(n, m, x1, y1, x2, y2)\n b = make_rect(n, m, x2, y2, x1, y1)\n c = Rectangle.new(0, 0, n, m)\n d = Rectangle.intersect(a, b)\n puts d.get_s + c.get_s - a.get_s - b.get_s\nend\n"}], "src_uid": "8e89fc6c3dfa30ac8c3495e2b1a1e106"} {"nl": {"description": "A robot cleaner is placed on the floor of a rectangle room, surrounded by walls. The floor consists of $$$n$$$ rows and $$$m$$$ columns. The rows of the floor are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and columns of the floor are numbered from $$$1$$$ to $$$m$$$ from left to right. The cell on the intersection of the $$$r$$$-th row and the $$$c$$$-th column is denoted as $$$(r,c)$$$. The initial position of the robot is $$$(r_b, c_b)$$$.In one second, the robot moves by $$$dr$$$ rows and $$$dc$$$ columns, that is, after one second, the robot moves from the cell $$$(r, c)$$$ to $$$(r + dr, c + dc)$$$. Initially $$$dr = 1$$$, $$$dc = 1$$$. If there is a vertical wall (the left or the right walls) in the movement direction, $$$dc$$$ is reflected before the movement, so the new value of $$$dc$$$ is $$$-dc$$$. And if there is a horizontal wall (the upper or lower walls), $$$dr$$$ is reflected before the movement, so the new value of $$$dr$$$ is $$$-dr$$$.Each second (including the moment before the robot starts moving), the robot cleans every cell lying in the same row or the same column as its position. There is only one dirty cell at $$$(r_d, c_d)$$$. The job of the robot is to clean that dirty cell. Illustration for the first example. The blue arc is the robot. The red star is the target dirty cell. Each second the robot cleans a row and a column, denoted by yellow stripes. Given the floor size $$$n$$$ and $$$m$$$, the robot's initial position $$$(r_b, c_b)$$$ and the dirty cell's position $$$(r_d, c_d)$$$, find the time for the robot to do its job.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 10^4$$$). Description of the test cases follows. A test case consists of only one line, containing six integers $$$n$$$, $$$m$$$, $$$r_b$$$, $$$c_b$$$, $$$r_d$$$, and $$$c_d$$$ ($$$1 \\le n, m \\le 100$$$, $$$1 \\le r_b, r_d \\le n$$$, $$$1 \\le c_b, c_d \\le m$$$) — the sizes of the room, the initial position of the robot and the position of the dirt cell.", "output_spec": "For each test case, print an integer — the time for the robot to clean the dirty cell. We can show that the robot always cleans the dirty cell eventually.", "sample_inputs": ["5\n10 10 6 1 2 8\n10 10 9 9 1 1\n9 8 5 6 2 1\n6 9 2 2 5 8\n2 2 1 1 2 1"], "sample_outputs": ["7\n10\n9\n3\n0"], "notes": "NoteIn the first example, the floor has the size of $$$10\\times 10$$$. The initial position of the robot is $$$(6, 1)$$$ and the position of the dirty cell is $$$(2, 8)$$$. See the illustration of this example in the problem statement.In the second example, the floor is the same, but the initial position of the robot is now $$$(9, 9)$$$, and the position of the dirty cell is $$$(1, 1)$$$. In this example, the robot went straight to the dirty cell and clean it. In the third example, the floor has the size $$$9 \\times 8$$$. The initial position of the robot is $$$(5, 6)$$$, and the position of the dirty cell is $$$(2, 1)$$$. In the fourth example, the floor has the size $$$6 \\times 9$$$. The initial position of the robot is $$$(2, 2)$$$ and the position of the dirty cell is $$$(5, 8)$$$. In the last example, the robot was already standing in the same column as the dirty cell, so it can clean the cell right away. "}, "positive_code": [{"source_code": "main = -> {\n t = int\n t.times do\n n, m, rb, cb, rd, cd = ints\n\n tate =\n if rb <= rd\n rd - rb\n else\n n - rd + n - rb\n end\n yoko =\n if cb <= cd\n cd - cb\n else\n m - cd + m - cb\n end\n ans = [yoko, tate].min\n puts ans\n end\n}\n\nDEBUG = true\nMOD = 10**9+7\n$yesno = %w(No Yes)\nYesNo = %w(No Yes)\nYESNO = %w(NO YES)\nINF = 10**9\n\nrequire \"set\"\nrequire \"prime\"\n\ndef int; gets.to_s.to_i end\ndef ints; gets.to_s.split.map { |s| s.to_i } end\ndef int1s; gets.to_s.split.map { |s| s.to_i - 1 } end\ndef float; gets.to_s.to_f end\ndef floats; gets.to_s.split.map { |s| s.to_f } end\ndef array_of(&convert); gets.to_s.split.map(&convert) end\ndef string; gets.to_s.chomp end\ndef strings; gets.to_s.split end\ndef rep(n, &b); Array.new(n, &b) end\ndef yes; puts $yesno[1] end\ndef no; puts $yesno[0] end\ndef yesno t; puts $yesno[t] end\ndef YesNo t; puts YesNo[t] end\ndef YESNO t; puts YESNO[t] end\ndef zip(xs, *yss); Enumerator.new { |y| xs.zip(*yss) { |a| y.yield(*a) } } end\ndef max(*xs, &block); block_given? ? xs.max_by(&block) : xs.max end\ndef min(*xs, &block); block_given? ? xs.min_by(&block) : xs.min end\ndef minmax(*xs, &block); block_given? ? xs.minmax_by(&block) : xs.minmax end\ndef gcd(*xs); xs.inject(0, :gcd) end\ndef matrix(h, w, fill=nil, &block); return Array.new(h) { Array.new(w, &block) } if block_given?; Array.new(h) { [fill] * w } end\ndef debug(x = nil); STDERR.puts (block_given? ? yield(x) : x).inspect if DEBUG; x end\ndef debug_grid(grid, width = 1); grid.each { |row| STDERR.puts row.map { |x| x.inspect.ljust(width) }.join(\"\") } if DEBUG; grid end\ndef if_debug; yield if DEBUG end\n\nmodule Boolean\n def coerce(other); [other, to_i] end\n def +@; to_i end\n def to_int; to_i end\n def *(other); to_i * other end\nend\n\nclass TrueClass\n include Boolean\n def to_i; 1 end\nend\n\nclass FalseClass\n include Boolean\n def to_i; 0 end\nend\n\nclass Integer\n def div_ceil(y); (self + y - 1) / y end\n def mod_inv(mod = MOD); pow(mod-2, mod) end\n def mod_div(y, mod = MOD); self * mod_inv(y, mod) % mod end\n def factorial(mod = MOD); (2..self).inject(1) { |f, x| f * x % mod } end\n def popcount; x = self; c = 0; while x > 0; c += 1 if x & 1 == 1; x >>= 1 end; c end #TODO: faster\n def bitbrute(&block); (1< 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\n def lower_bound; ac, wa = self.end_open, self.begin; while ac - wa > 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\n def shakutori(r2, &pred); Enumerator.new { |y| j, r = r2.begin, r2.end_open; each { |i| j += 1 while j + 1 < r and pred[i, j+1]; y.yield(i, j) } }; end\n def widest(&block); Enumerator.new { |y| j, n = self.begin, self.end_open; each { |i| j += 1 while j < n and block[i, j]; y.yield(i, j) if block[i, j] } } end\nend\n\nclass Array\n def power(&block); (0 ... 1 << size).each(&block) end\n def sorted_merge(other); a = []; i = j = 0; n, m = size, other.size; if j < m and other[j] < self[i]; a << other[j]; j += 1 else; a << self[i]; i += 1 end while i < n; a.push(*other[j..-1]) if j < m; a end\n def upper_bound; ac, wa = 0, size; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\n def lower_bound; ac, wa = size, 0; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\n def cum(*xs, &op); a = []; a << xs[0] if xs.size > 0; a << x = self[0]; (1...size).each { |i| a << x = op[x, self[i]] }; a end\n def cumdiff(range); self[range.end_open] - self[range.begin]; end\nend\n\nmodule Enumerable\n def sorted_uniq; x = nil; filter { |y| c = x === y; x = y; !c } end\n def cumsum; ys = [0]; each { |x| ys << x + ys[-1] }; ys end\nend\n\n# 複数の素数を求めるような問題に高速に答えられる\nclass PrimeTable\n\tdef initialize(n)\n\t\t@lpf = [nil] * (n + 1)\n\t\t@primes = [2]\n\t\t(2 .. n).step(2) do |d| @lpf[d] = 2 end\n\t\t(3 .. n).step(2) do |d|\n\t\t\tunless @lpf[d]\n\t\t\t\t@lpf[d] = d\n\t\t\t\t@primes << d\n\t\t\tend\n\t\t\t@primes.each do |p|\n\t\t\t\tbreak if p * d > n or p > @lpf[d]\n\t\t\t\t@lpf[p * d] = p\n\t\t\tend\n\t\tend\n\tend\n\tdef prime?(n); @lpf[n] == n; end\n\tdef each(&block); @primes.each(&block); end\n\tdef factorize(n); fs = []; while n > 1; fs << (f = @lpf[n]); n /= f; end; fs; end\nend\n\nclass Factorial\n def initialize(max, mod)\n @mod = mod\n @fac = [1, 1]\n @fin = [1, 1]\n @inv = [nil, 1]\n (2 .. max).each do |i|\n @fac[i] = @fac[i - 1] * i % mod\n @inv[i] = mod - @inv[mod % i] * (mod / i) % mod\n @fin[i] = @fin[i - 1] * @inv[i] % mod\n end\n end\n \n def fact(n)\n @fac[n]\n end\n \n def comb(n, k)\n return 0 if n < k or n < 0 or k < 0\n @fac[n] * @fin[k] % @mod * @fin[n - k] % @mod\n end\nend\n\nclass UnionFind\n def initialize(size); @p = Array.new(size, -1); end\n def leader(i); j = i; j, i = i, @p[j] = @p[i] until @p[i] < 0; i; end\n def merge(i, j); k, l = leader(i), leader(j); return false if k == l; k, l = l, k if @p[k] > @p[l]; @p[k] += @p[l]; @p[l] = k; true; end\n def same?(i, j); leader(i) == leader(j); end\n def size(i); -@p[leader(i)]; end\n def groups; gs = Array.new(@p.size) { [] }; (0...@p.size).each { |i| gs[leader(i)] << i }; gs.reject(&:empty?); end\nend\n\nmain[]"}], "negative_code": [], "src_uid": "6f819ce1d88d5211cd475a8673edba97"} {"nl": {"description": "You are given an array of integers. Vasya can permute (change order) its integers. He wants to do it so that as many as possible integers will become on a place where a smaller integer used to stand. Help Vasya find the maximal number of such integers.For instance, if we are given an array $$$[10, 20, 30, 40]$$$, we can permute it so that it becomes $$$[20, 40, 10, 30]$$$. Then on the first and the second positions the integers became larger ($$$20>10$$$, $$$40>20$$$) and did not on the third and the fourth, so for this permutation, the number that Vasya wants to maximize equals $$$2$$$. Read the note for the first example, there is one more demonstrative test case.Help Vasya to permute integers in such way that the number of positions in a new array, where integers are greater than in the original one, is maximal.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 10^5$$$) — the length of the array. The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\leq a_i \\leq 10^9$$$) — the elements of the array.", "output_spec": "Print a single integer — the maximal number of the array's elements which after a permutation will stand on the position where a smaller element stood in the initial array.", "sample_inputs": ["7\n10 1 1 1 5 5 3", "5\n1 1 1 1 1"], "sample_outputs": ["4", "0"], "notes": "NoteIn the first sample, one of the best permutations is $$$[1, 5, 5, 3, 10, 1, 1]$$$. On the positions from second to fifth the elements became larger, so the answer for this permutation is 4.In the second sample, there is no way to increase any element with a permutation, so the answer is 0."}, "positive_code": [{"source_code": "\ndef countzz(xs)\n xss = xs.sort\n chunks = xss.chunk{|x|x}.map{|a,b|b.size}.to_a\n use_chunks = chunks.clone\n\n use_index = 0\n count = 0\n\n chunks.each.with_index do |size, i|\n use_index = [use_index, i + 1].max\n\n while size > 0 && use_index < use_chunks.size\n if use_chunks[use_index] > size\n take = size\n count += take\n size = 0\n use_chunks[use_index] -= take\n else\n take = use_chunks[use_index]\n count += take\n size -= take\n use_chunks[use_index] = 0\n use_index += 1\n end\n end\n end\n count\nend\n\ndef solution\n gets\n xs = read_ints\n puts countzz(xs)\nend\n\ndef read_ints\n gets.split.map &:to_i\nend\n\ndef read_string\n gets.chomp\nend\n\nsolution"}, {"source_code": "N = gets.to_i\nas = gets.split.map(&:to_i).sort\nlen = 1\nmax = 1\n(1...N).each do |i|\n if as[i-1] == as[i]\n len += 1\n max = len if max < len\n else\n len = 1\n end\nend\nputs N - max"}, {"source_code": "n = gets.to_i\nary = gets.strip.split.map(&:to_i)\n\nhsh = {}\nary.each {|x| hsh[x] = ((hsh[x] || 0) + 1)}\n\nputs n - hsh.values.max\n"}], "negative_code": [{"source_code": "def countzz(xs)\n xss = xs.sort\n chunks = xss.chunk{|x|x}.map{|a,b|b.size}.to_a\n use_chunks = chunks.clone\n\n use_index = 0\n count = 0\n\n chunks.each.with_index do |size, i|\n use_index = [use_index, i + 1].max\n\n return count if use_index >= use_chunks.size\n\n while size > 0 && use_index < use_chunks.size\n if use_chunks[use_index] > size\n count += size\n size = 0\n use_chunks[use_index] -= size\n else\n count += use_chunks[use_index]\n size -= count\n use_chunks[use_index] -= 0\n use_index += 1\n end\n end\n end\n count\nend\n\ndef solution\n gets\n xs = read_ints\n puts countzz(xs)\nend\n\ndef read_ints\n gets.split.map &:to_i\nend\n\ndef read_string\n gets.chomp\nend\n\nsolution "}, {"source_code": "def countzz(xs)\n xss = xs.sort\n chunks = xss.chunk{|x|x}.map{|a,b|b.size}.to_a\n use_chunks = chunks.clone\n\n use_index = 0\n count = 0\n\n chunks.each.with_index do |size, i|\n use_index = [use_index, i + 1].max\n\n return count if use_index >= use_chunks.size\n\n while size > 0 && use_index < use_chunks.size\n if use_chunks[use_index] > size\n count += size\n size = 0\n use_chunks[use_index] -= size\n else\n count += use_chunks[use_index]\n size -= count\n use_chunks[use_index] = 0\n use_index += 1\n end\n end\n end\n count\nend\n\ndef solution\n gets\n xs = read_ints\n puts countzz(xs)\nend\n\ndef read_ints\n gets.split.map &:to_i\nend\n\ndef read_string\n gets.chomp\nend\n\nsolution "}, {"source_code": "\ndef countzz(xs)\n xss = xs.sort\n chunks = xss.chunk{|x|x}.map{|a,b|b.size}.to_a\n use_chunks = chunks.clone\n\n use_index = 0\n count = 0\n\n chunks.each.with_index do |size, i|\n use_index = [use_index, i + 1].max\n\n return count if use_index >= use_chunks.size\n\n while size > 0 && use_index < use_chunks.size\n if use_chunks[use_index] > size\n count += size\n size = 0\n use_chunks[use_index] -= size\n else\n count += use_chunks[use_index]\n size -= count\n use_chunks[use_index] -= 0\n end\n use_index += 1\n end\n end\n count\nend\n\ndef solution\n gets\n xs = read_ints\n\n puts countzz(xs)\n\nend\n\ndef read_ints\n gets.split.map &:to_i\nend\n\ndef read_string\n gets.chomp\nend\n\nsolution"}], "src_uid": "eaa768dc1024df6449e89773234cc0c3"} {"nl": {"description": "Kana was just an ordinary high school girl before a talent scout discovered her. Then, she became an idol. But different from the stereotype, she is also a gameholic. One day Kana gets interested in a new adventure game called Dragon Quest. In this game, her quest is to beat a dragon. The dragon has a hit point of $$$x$$$ initially. When its hit point goes to $$$0$$$ or under $$$0$$$, it will be defeated. In order to defeat the dragon, Kana can cast the two following types of spells. Void Absorption Assume that the dragon's current hit point is $$$h$$$, after casting this spell its hit point will become $$$\\left\\lfloor \\frac{h}{2} \\right\\rfloor + 10$$$. Here $$$\\left\\lfloor \\frac{h}{2} \\right\\rfloor$$$ denotes $$$h$$$ divided by two, rounded down. Lightning Strike This spell will decrease the dragon's hit point by $$$10$$$. Assume that the dragon's current hit point is $$$h$$$, after casting this spell its hit point will be lowered to $$$h-10$$$.Due to some reasons Kana can only cast no more than $$$n$$$ Void Absorptions and $$$m$$$ Lightning Strikes. She can cast the spells in any order and doesn't have to cast all the spells. Kana isn't good at math, so you are going to help her to find out whether it is possible to defeat the dragon.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 1000$$$)  — the number of test cases. The next $$$t$$$ lines describe test cases. For each test case the only line contains three integers $$$x$$$, $$$n$$$, $$$m$$$ ($$$1\\le x \\le 10^5$$$, $$$0\\le n,m\\le30$$$)  — the dragon's intitial hit point, the maximum number of Void Absorptions and Lightning Strikes Kana can cast respectively.", "output_spec": "If it is possible to defeat the dragon, print \"YES\" (without quotes). Otherwise, print \"NO\" (without quotes). You can print each letter in any case (upper or lower).", "sample_inputs": ["7\n100 3 4\n189 3 4\n64 2 3\n63 2 3\n30 27 7\n10 9 1\n69117 21 2"], "sample_outputs": ["YES\nNO\nNO\nYES\nYES\nYES\nYES"], "notes": "NoteOne possible casting sequence of the first test case is shown below: Void Absorption $$$\\left\\lfloor \\frac{100}{2} \\right\\rfloor + 10=60$$$. Lightning Strike $$$60-10=50$$$. Void Absorption $$$\\left\\lfloor \\frac{50}{2} \\right\\rfloor + 10=35$$$. Void Absorption $$$\\left\\lfloor \\frac{35}{2} \\right\\rfloor + 10=27$$$. Lightning Strike $$$27-10=17$$$. Lightning Strike $$$17-10=7$$$. Lightning Strike $$$7-10=-3$$$."}, "positive_code": [{"source_code": "t = gets.chomp.to_i\nt.times do\n x, n, m = gets.chomp.split.map(&:to_i)\n\n n.times do\n if (x/2)+10 < x\n x = (x/2)+10\n else\n break\n end\n\n end\n\n x = x-(m*10)\n\n if x <= 0\n puts \"YES\"\n else\n puts \"NO\"\n end\nend"}, {"source_code": "file = File.exist?('in.in') ? File.new('in.in','r') : STDIN\n\nt = file.gets.to_i\n\nwhile t != 0\n h, n, m = file.gets.split.map(&:to_i)\n defeat = 'NO'\n\n while n > 0 || m > 0\n if h >= 20 && n > 0\n h = h / 2 + 10\n n -= 1\n elsif m > 0\n h -= 10\n m -= 1\n else\n h = h / 2 + 10\n n -= 1\n end\n if h <= 0\n defeat = 'YES'\n break;\n end\n end\n puts defeat\n t -= 1\nend\n"}, {"source_code": "gets.to_i.times {\n x, n, m = gets.split.map { |v| v.to_i }\n n.times {\n break if x < 21\n x = x / 2 + 10\n }\n x -= m * 10\n puts x > 0 ? \"NO\" : \"YES\"\n}\n"}, {"source_code": "def can_defeat_dragon(x, n, m)\n while x > 0 && (n > 0 || m > 0)\n if m > 0 && x <= m * 10\n x = x - (m * 10)\n m = 0\n end\n\n if x > 0\n if n > 0\n x = x / 2 + 10\n n = n - 1\n elsif m > 0\n x = x - 10\n m = m - 1\n end\n end\n end\n\n if x <= 0\n \"YES\"\n else\n \"NO\"\n end\nend\n\nt = gets.chomp.to_i\n\nt.times do\n x, n, m = gets.chomp.split(' ').map(&:to_i)\n puts can_defeat_dragon(x, n, m)\nend"}, {"source_code": "1.upto(gets.chomp.to_i) do\n x,n,m = gets.chomp.split.map(&:to_i)\n while x > 20 && n > 0\n n -= 1\n x = x/2 + 10\n end\n while x > 0 && m > 0\n m -= 1\n x -= 10\n end\n if x <= 0\n puts 'YES'\n else\n puts 'NO'\n end\nend\n"}, {"source_code": "t = gets.to_i\nt.times do\n x, n, m = gets.split.map(&:to_i)\n n.times do\n if x > 20\n x = x / 2 + 10\n else\n break\n end\n end\n puts (x <= m * 10) ? \"YES\" : \"NO\"\nend\n"}, {"source_code": "1.upto(gets.to_i) do\n x, a, b = gets.split(' ').map(&:to_i)\n\n while x > 20 && a > 0\n x = x.div(2) + 10\n a -= 1\n end\n\n puts (10*b >= x) ? 'YES' : 'NO'\nend\n"}], "negative_code": [{"source_code": "file = File.exist?('in.in') ? File.new('in.in','r') : STDIN\n\nt = file.gets.to_i\n\nwhile t != 0\n h, n, m = file.gets.split.map(&:to_i)\n defeat = 'NO'\n\n while n > 0 || m > 0\n if h >= 20 && n > 0\n h = h / 2 + 10\n n -= 1\n elsif m > 0\n h -= 10\n m -= 1\n else\n puts \"--------------------------\"\n puts \"n: #{n}\"\n puts \"m: #{m}\"\n puts \"=================\"\n puts \"h: #{h}\"\n h = h / 2 + 10\n n -= 1\n end\n if h <= 0\n defeat = 'YES'\n break;\n end\n end\n puts defeat\n t -= 1\nend\n"}], "src_uid": "78f25e2bc4ff22dbac94f72af68a745f"} {"nl": {"description": "Eugeny loves listening to music. He has n songs in his play list. We know that song number i has the duration of ti minutes. Eugeny listens to each song, perhaps more than once. He listens to song number i ci times. Eugeny's play list is organized as follows: first song number 1 plays c1 times, then song number 2 plays c2 times, ..., in the end the song number n plays cn times.Eugeny took a piece of paper and wrote out m moments of time when he liked a song. Now for each such moment he wants to know the number of the song that played at that moment. The moment x means that Eugeny wants to know which song was playing during the x-th minute of his listening to the play list.Help Eugeny and calculate the required numbers of songs.", "input_spec": "The first line contains two integers n, m (1 ≤ n, m ≤ 105). The next n lines contain pairs of integers. The i-th line contains integers ci, ti (1 ≤ ci, ti ≤ 109) — the description of the play list. It is guaranteed that the play list's total duration doesn't exceed 109 . The next line contains m positive integers v1, v2, ..., vm, that describe the moments Eugeny has written out. It is guaranteed that there isn't such moment of time vi, when the music doesn't play any longer. It is guaranteed that vi < vi + 1 (i < m). The moment of time vi means that Eugeny wants to know which song was playing during the vi-th munite from the start of listening to the playlist.", "output_spec": "Print m integers — the i-th number must equal the number of the song that was playing during the vi-th minute after Eugeny started listening to the play list.", "sample_inputs": ["1 2\n2 8\n1 16", "4 9\n1 2\n2 1\n1 1\n2 2\n1 2 3 4 5 6 7 8 9"], "sample_outputs": ["1\n1", "1\n1\n2\n2\n3\n4\n4\n4\n4"], "notes": null}, "positive_code": [{"source_code": "def binary_find(array, key)\n low = 0\n high = array.size - 1\n ret = 0\n while low <= high\n mid = low + high >> 1\n ret = mid if array[mid] >= key\n if array[mid] >= key then high = mid - 1\n else low = mid + 1\n end\n end\n ret\nend\nn, m = gets.split(' ').map { |i| i.to_i }\na = [0]\nn.times do\n a.push a[-1] + gets.split(' ').inject { |mem, var| mem = var.to_i * mem.to_i}\nend\nq = gets.split(' ').map { |i| i.to_i }\nq.each do |qu|\n puts binary_find(a, qu)\nend\n"}], "negative_code": [], "src_uid": "c4398a9f342a23baf1d7ebc9f4e9577d"} {"nl": {"description": "There are $$$n$$$ bags with candies, initially the $$$i$$$-th bag contains $$$i$$$ candies. You want all the bags to contain an equal amount of candies in the end. To achieve this, you will: Choose $$$m$$$ such that $$$1 \\le m \\le 1000$$$Perform $$$m$$$ operations. In the $$$j$$$-th operation, you will pick one bag and add $$$j$$$ candies to all bags apart from the chosen one.Your goal is to find a valid sequence of operations after which all the bags will contain an equal amount of candies. It can be proved that for the given constraints such a sequence always exists.You don't have to minimize $$$m$$$.If there are several valid sequences, you can output any.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 100$$$). Description of the test cases follows. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \\le n\\le 100$$$). ", "output_spec": "For each testcase, print two lines with your answer. In the first line print $$$m$$$ ($$$1\\le m \\le 1000$$$) — the number of operations you want to take. In the second line print $$$m$$$ positive integers $$$a_1, a_2, \\dots, a_m$$$ ($$$1 \\le a_i \\le n$$$), where $$$a_j$$$ is the number of bag you chose on the $$$j$$$-th operation.", "sample_inputs": ["2\n2\n3"], "sample_outputs": ["1\n2\n5\n3 3 3 1 2"], "notes": "NoteIn the first case, adding $$$1$$$ candy to all bags except of the second one leads to the arrangement with $$$[2, 2]$$$ candies.In the second case, firstly you use first three operations to add $$$1+2+3=6$$$ candies in total to each bag except of the third one, which gives you $$$[7, 8, 3]$$$. Later, you add $$$4$$$ candies to second and third bag, so you have $$$[7, 12, 7]$$$, and $$$5$$$ candies to first and third bag  — and the result is $$$[12, 12, 12]$$$."}, "positive_code": [{"source_code": "gets.to_i.times{\n n=gets.to_i\n p n-1\n t = []\n (n-1).times{|x|\n t << x + 2\n }\n puts t*' '\n}"}, {"source_code": "t = gets.chomp.to_i\nt.times do\n n = gets.chomp.to_i\n puts n\n puts (1..n).to_a.join ' '\nend "}, {"source_code": "testcases = gets.chomp.to_i\ntestcases.times do\n bags = gets.chomp.to_i\n j_bags = (1..bags).to_a\n puts j_bags.size\n puts j_bags.join(\" \")\nend\n"}], "negative_code": [{"source_code": "gets.to_i.times{\n n=gets.to_i\n p n-1\n t = []\n (n-1).times{|x|\n t << x + 1\n }\n puts t*' '\n}"}, {"source_code": "t = gets.chomp.to_i\nt.times do\n n = gets.chomp.to_i\n puts n\n puts (1..n).to_a.reverse.join ' '\nend "}, {"source_code": "testcases = gets.chomp.to_i\n\ntestcases.times do\n bags = gets.chomp.to_i\n total_moves = 0\n j_bags = []\n baggies = (1..bags).to_a\n while baggies.uniq.length > 1\n\n max = baggies.max\n max_bag = baggies.find_index { |x| x == max }\n\n j_bags << max_bag + 1\n baggies = baggies.each_with_index.map do |x, i|\n x += 1 unless i == max_bag\n x\n end\n total_moves += 1\n\n end\n puts total_moves\n puts j_bags.join(\" \")\n \nend\n"}, {"source_code": "\ntestcases = gets.chomp.to_i\n\ntestcases.times do \n bags = gets.chomp.to_i\n puts 1\n puts bags\nend"}], "src_uid": "ac248c83c99d8a2262772816b5f4ac6e"} {"nl": {"description": "Little girl Margarita is a big fan of competitive programming. She especially loves problems about arrays and queries on them.Recently, she was presented with an array $$$a$$$ of the size of $$$10^9$$$ elements that is filled as follows: $$$a_1 = -1$$$ $$$a_2 = 2$$$ $$$a_3 = -3$$$ $$$a_4 = 4$$$ $$$a_5 = -5$$$ And so on ... That is, the value of the $$$i$$$-th element of the array $$$a$$$ is calculated using the formula $$$a_i = i \\cdot (-1)^i$$$.She immediately came up with $$$q$$$ queries on this array. Each query is described with two numbers: $$$l$$$ and $$$r$$$. The answer to a query is the sum of all the elements of the array at positions from $$$l$$$ to $$$r$$$ inclusive.Margarita really wants to know the answer to each of the requests. She doesn't want to count all this manually, but unfortunately, she couldn't write the program that solves the problem either. She has turned to you — the best programmer.Help her find the answers!", "input_spec": "The first line contains a single integer $$$q$$$ ($$$1 \\le q \\le 10^3$$$) — the number of the queries. Each of the next $$$q$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \\le l \\le r \\le 10^9$$$) — the descriptions of the queries.", "output_spec": "Print $$$q$$$ lines, each containing one number — the answer to the query. ", "sample_inputs": ["5\n1 3\n2 5\n5 5\n4 4\n2 3"], "sample_outputs": ["-2\n-2\n-5\n4\n-1"], "notes": "NoteIn the first query, you need to find the sum of the elements of the array from position $$$1$$$ to position $$$3$$$. The sum is equal to $$$a_1 + a_2 + a_3 = -1 + 2 -3 = -2$$$.In the second query, you need to find the sum of the elements of the array from position $$$2$$$ to position $$$5$$$. The sum is equal to $$$a_2 + a_3 + a_4 + a_5 = 2 -3 + 4 - 5 = -2$$$.In the third query, you need to find the sum of the elements of the array from position $$$5$$$ to position $$$5$$$. The sum is equal to $$$a_5 = -5$$$.In the fourth query, you need to find the sum of the elements of the array from position $$$4$$$ to position $$$4$$$. The sum is equal to $$$a_4 = 4$$$.In the fifth query, you need to find the sum of the elements of the array from position $$$2$$$ to position $$$3$$$. The sum is equal to $$$a_2 + a_3 = 2 - 3 = -1$$$."}, "positive_code": [{"source_code": "q = gets.to_i\n\ndef f(x)\n ((-1)**(x%2))*x\nend\n\nq.times do\n quer = gets.chomp.split(\" \").map(&:to_i)\n l = quer[0]\n r = quer[1]\n\n if (l%2==1 and r%2==0)\n puts (r-l+1)/2\n elsif (l%2==0 and r%2==1)\n puts -1*(r-l+1)/2\n elsif l%2==0 and r%2==0\n puts f(l)+(r-l)/2\n else\n puts f(r)+(r-l)/2\n end\nend\n"}], "negative_code": [], "src_uid": "7eae40835f6e9580b985d636d5730e2d"} {"nl": {"description": "You are given a program that consists of $$$n$$$ instructions. Initially a single variable $$$x$$$ is assigned to $$$0$$$. Afterwards, the instructions are of two types: increase $$$x$$$ by $$$1$$$; decrease $$$x$$$ by $$$1$$$. You are given $$$m$$$ queries of the following format: query $$$l$$$ $$$r$$$ — how many distinct values is $$$x$$$ assigned to if all the instructions between the $$$l$$$-th one and the $$$r$$$-th one inclusive are ignored and the rest are executed without changing the order? ", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of testcases. Then the description of $$$t$$$ testcases follows. The first line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n, m \\le 2 \\cdot 10^5$$$) — the number of instructions in the program and the number of queries. The second line of each testcase contains a program — a string of $$$n$$$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively. Each of the next $$$m$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \\le l \\le r \\le n$$$) — the description of the query. The sum of $$$n$$$ over all testcases doesn't exceed $$$2 \\cdot 10^5$$$. The sum of $$$m$$$ over all testcases doesn't exceed $$$2 \\cdot 10^5$$$. ", "output_spec": "For each testcase print $$$m$$$ integers — for each query $$$l$$$, $$$r$$$ print the number of distinct values variable $$$x$$$ is assigned to if all the instructions between the $$$l$$$-th one and the $$$r$$$-th one inclusive are ignored and the rest are executed without changing the order.", "sample_inputs": ["2\n8 4\n-+--+--+\n1 8\n2 8\n2 5\n1 1\n4 10\n+-++\n1 1\n1 2\n2 2\n1 3\n2 3\n3 3\n1 4\n2 4\n3 4\n4 4"], "sample_outputs": ["1\n2\n4\n4\n3\n3\n4\n2\n3\n2\n1\n2\n2\n2"], "notes": "NoteThe instructions that remain for each query of the first testcase are: empty program — $$$x$$$ was only equal to $$$0$$$; \"-\" — $$$x$$$ had values $$$0$$$ and $$$-1$$$; \"---+\" — $$$x$$$ had values $$$0$$$, $$$-1$$$, $$$-2$$$, $$$-3$$$, $$$-2$$$ — there are $$$4$$$ distinct values among them; \"+--+--+\" — the distinct values are $$$1$$$, $$$0$$$, $$$-1$$$, $$$-2$$$. "}, "positive_code": [{"source_code": "gets.to_i.times do\r\n n,m=gets.split.map(&:to_i)\r\n s=gets.chomp.chars\r\n b=[0]\r\n slu=[0]\r\n sld=[0]\r\n a=0\r\n s.each do |ss|\r\n if ss==\"+\"\r\n a+=1\r\n else\r\n a-=1\r\n end\r\n b << a\r\n if a>slu[-1]\r\n slu << a\r\n else\r\n slu << slu[-1]\r\n end\r\n if asru[-1]\r\n sru << a\r\n else\r\n sru << sru[-1]\r\n end\r\n if a row\n cnt += 1\n end\n end\nend\nputs cnt\n"}, {"source_code": "n = gets.to_i\nary = Array.new(n).map!{ Array.new(n, 0) }\nn.times { |r|\n s = gets.split.map(&:to_i)\n n.times { |c|\n ary[r][c] = s[c]\n }\n}\ncnt = 0\nn.times { |r|\n n.times { |c|\n col = 0\n row = 0\n n.times { |i|\n col += ary[i][c]\n }\n n.times { |i|\n row += ary[r][i]\n }\n cnt += 1 if col > row\n }\n}\np cnt\n"}, {"source_code": "n = gets.to_i\nary = Array.new(n).map!{ Array.new(n, 0) }\nn.times { |r|\n s = gets.split.map{ |e| e.to_i }\n n.times { |c|\n ary[r][c] = s[c]\n }\n}\ncnt = 0\nn.times { |r|\n n.times { |c|\n col = 0\n row = 0\n n.times { |i|\n col += ary[i][c]\n }\n n.times { |i|\n row += ary[r][i]\n }\n if col > row; cnt += 1; end;\n }\n}\np cnt\n"}, {"source_code": "def winning?(rowpos, colpos, elem, game)\n\t\n\t# check row sum\n\trowsum = 0\n\t$n.times do |i|\n\t\trowsum += game[rowpos][i].to_i\n\tend\n\t\n\t# check col sum\n\tcolsum = 0\n\t$n.times do |i|\n\t\tcolsum += game[i][colpos].to_i\n\tend\n\t\n\tcolsum > rowsum\nend\n\n$n = STDIN.gets.chomp.to_i\ngame = []\n$n.times do |i|\n\tgame << STDIN.gets.chomp.split(' ').map{|ch| ch.to_i}\nend\n\n\ncount = 0\nrowpos = 0\ngame.each do |row|\n\tcolpos = 0\n\trow.each do |elem|\n\t\tcount += 1 if winning?(rowpos, colpos, elem, game)\n\t\tcolpos += 1\n\tend\n\trowpos += 1\nend\n\nputs count"}, {"source_code": "n = gets.to_i\nboard = []\nsumg = []\nsumv = Array.new(n){|i| i = 0}\nn.times do |i|\n s = gets.split.map{|i| i.to_i}\n sum = 0\n s.map{|i| sum +=i}\n sumg << sum\n n.times do |j|\n sumv[j] += s[j]\n end\nend\nres = 0\nn.times do |i|\n n.times do |j|\n if sumg[i] < sumv[j] then\n res += 1\n end\n end\nend\n\nputs res"}, {"source_code": "n = gets.to_i\na = Array.new(n){gets.split.map &:to_i}\nc = 0\nn.times{|i|\n n.times{|j|\n x, y = 0, 0\n n.times{|k|\n x += a[i][k]\n y += a[k][j]\n }\n c += 1 if x < y\n }\n}\np c\n"}, {"source_code": "n = gets.to_i\nmap = []\nn.times do\n map << gets.split.map(&:to_i)\nend\nans = 0\nn.times do |x|\n n.times do |y|\n s1, s2 = 0, 0\n n.times do |i|\n s1+=map[i][x]\n s2+=map[y][i]\n end\n ans += 1 if s1>s2\n end\nend\np ans\n"}, {"source_code": "n = gets.chomp.to_i\n\nfield = []\nn.times do\n field << gets.chomp.split(\" \").map{|e| e.to_i}\nend\n\nwin = 0\n\nfield.each_with_index do |column, col_no|\n column_sum = column.inject(0){|sum, i| sum += i}\n \n column.each_with_index do |e, row_no|\n row_sum = 0\n 0.upto(field.length - 1) do |a|\n row_sum += field[a][row_no]\n end\n \n if row_sum > column_sum\n win += 1\n end \n end\nend\n\nputs win"}, {"source_code": "def win? i, j\n $array[i].inject(:+) < $array.transpose[j].inject(:+)\nend\nind = (0...gets.to_i).to_a\n$array = readlines.map(&:split).each { |a| a.map!(&:to_i) }\nputs ind.product(ind).count { |i, j| win? i, j }"}, {"source_code": "n = gets.to_i\nrow_sum = []\ncol_sum = Array.new(n) { 0 }\nn.times do\n\tsum = 0\n\tgets.split(\"\\s\").map { |x| x.to_i}.each_with_index do |x, i|\n\t\tcol_sum[i] += x\n\t\tsum += x\n\tend\n\trow_sum << sum\nend\n\ncells = 0\nj = 0\ncol_sum.sort!\nrow_sum.sort.each_with_index do |r, i|\n\twhile j < n\n\t\tif r < col_sum[j]\n\t\t\tbreak\n\t\telse\n\t\t\tj += 1\n\t\t\tcells += i\n\t\tend\n\tend\nend\ncells += n * (n - j)\nputs cells"}, {"source_code": "n=gets.chomp.to_i\nsumcolumns=Array.new(n,0)\nsumrows=Array.new(n,0)\nfor i in 0..(n-1)\n temp= gets.chomp.split.collect{|a| a.to_i}\n sumrows[i]=temp.reduce(:+)\n temp.each_index{|a| sumcolumns[a]=sumcolumns[a]+temp[a]}\nend\ncount=0\nsumcolumns.each{|a|count=count+sumrows.count{|b| a>b }}\nprint count \n\n"}, {"source_code": "n = gets.to_i\nnss = n.times.map{gets.split.map(&:to_i)}\n\nanswer = 0.upto(n-1).map do |i|\n 0.upto(n-1).count do |j|\n nss[i].reduce(:+) < nss.map{|row| row[j]}.reduce(:+)\n end\nend.reduce(:+)\n\np answer\n"}, {"source_code": "public \n\ndef solve(matrix)\n lines = {}\n cols = {}\n\n matrix.each_with_index do |x,i|\n lines[i] = x.inject(0){|z,s| s + z}\n end\n\n transpose(matrix).each_with_index do |x,i|\n cols[i] = x.inject(0){|z,s| s + z}\n end\n \n size = matrix[0].count\n win = 0\n (0...size).each do |i|\n (0...size).each do |j|\n win += 1if cols[i] > lines[j]\n end\n end\n \n win\nend\n\ndef transpose(matrix)\n size = matrix[0].count\n transponent = []\n \n (0...size).each do |i|\n transponent[i] = [1]*size\n end\n \n (0...size).each do |i|\n (0...size).each do |j|\n transponent[i][j] = matrix[j][i]\n end\n end\n transponent\nend\n\ndef read(input)\n input.split(\"\\n\")\nend\n\ndef plow(list)\n list.collect{|y| y.split(\" \").collect{|x| x.to_i}}[1..-1]\nend\n\nif __FILE__ == $PROGRAM_NAME\n print solve(plow(read(STDIN.read)))\nend\n"}, {"source_code": "n = gets.chomp.to_i\nsquares = Array.new(n) { gets.split.collect{|x| x.to_i} }\n \nwinning = 0\n(0..n-1).each{|i|\n row_sum = 0\n squares[i].collect{|x| row_sum += x}\n (0..n-1).each{|j|\n col_sum = 0\n squares.each{|s|\n col_sum += s[j]\n }\n winning += 1 if col_sum > row_sum\n }\n}\n\nputs winning"}, {"source_code": "n=gets.to_i\na=[]\n0.upto(n-1) do |i|\n a[i]=gets.chomp.scan(/\\w+/)\nend\nfor i in 0..n-1 do\n for j in 0..n-1 do\n a[i][j]=a[i][j].to_i\n end\nend\nb=[]\nfor i in 0..n-1 do\n b[i]=0\n for j in 0..n-1 do\n b[i]+=a[i][j]\n end\nend\nc=[]\nfor j in 0..n-1 do\n c[j]=0\n for i in 0..n-1 do\n c[j]+=a[i][j]\n end\nend\nk=0\nfor i in 0..n-1 do\n for j in 0..n-1 do\n if (b[i] row[j])\n\t\t\tcount += 1\n\t\tend\n\tend\nend\n\nputs count"}, {"source_code": "n = gets.chomp.to_i\nb = []\nrow_sums = []\ncol_sums = []\nfor i in 0...n\n b[i] = gets.chomp.split.map { |x| x.to_i }\n row_sums[i] = b[i].inject(:+)\n for j in 0...n\n col_sums[j] = (col_sums[j] || 0) + b[i][j]\n end\nend\n\ncount = 0\nfor i in 0...n\n for j in 0...n\n count += 1 if col_sums[i] > row_sums[j]\n end\nend\nputs count"}, {"source_code": "# Codeforces Round #110\n# Problem A -- Game Outcome\na = []\nn = gets.to_i\nn.times { a << gets.split.map(&:to_i) }\nrows = (columns = [0] * n).dup\n(0...n).each do |i|\n (0...n).each do |j|\n rows[i] += a[i][j]\n columns[j] += a[i][j]\n end\nend\nputs rows.product(columns).select {|x, y| x < y }.size\n"}, {"source_code": "board = Array.new(gets.to_i) { gets.split.map(&:to_i) }\n\nrows_sum = board.map {|row| row.reduce(:+) }\ncols_sum = board.transpose.map {|col| col.reduce(:+) }\n\ncount = 0\nrows_sum.each do |rs|\n cols_sum.each do |cs|\n if rs < cs\n\t count += 1\n\tend\n end\nend\n\nputs count"}, {"source_code": "n = gets.to_i\nboard = Array.new\nrowSums = Array.new\nn.times do\n row = gets.split(\" \")\n row = row.map do |e|\n e.to_i\n end\n rowSums.push(row.inject(:+))\n board.push(row)\nend\n\ncolSums = Array.new\n\nfor col in 0...n\n colSum = 0\n for row in 0...n\n colSum += board[row][col]\n end\n colSums[col] = colSum\nend\n\ntotal = 0\nfor i in 0...n\n for j in 0...n\n if (colSums[j] > rowSums[i])\n total += 1\n end\n end\nend\n\nputs total\n"}, {"source_code": "n = gets.to_i\nright = Array.new(n) { 0 }\ndown = Array.new(n) { 0 }\n\nn.times do |i|\n gets.split.each_with_index do |element, index|\n a =element.to_i\n right[i] = right[i] + a\n down[index] = down[index] + a\n end\nend\ncounter = 0\nright.sort!\ndown.sort!\ndown.each do |i|\n plus = right.find_index { |j| j>=i }\n if plus\n counter = counter + plus\n else\n counter = counter + right.size\n end\nend\np counter\n"}], "negative_code": [{"source_code": "def winning?(rowpos, colpos, elem, game)\n\t\n\t# check row sum\n\trowsum = 0\n\t$n.times do |i|\n\t\trowsum += game[rowpos][i].to_i\n\tend\n\t\n\t# check col sum\n\tcolsum = 0\n\t$n.times do |i|\n\t\tcolsum += game[i][colpos].to_i\n\tend\n\t\n\tcolsum > rowsum\nend\n\n$n = STDIN.gets.chomp.to_i\ngame = []\n$n.times do |i|\n\tgame << STDIN.gets.chomp.split(' ').map{|ch| ch.to_i}\nend\n\n\nres = 0\nrowpos = 0\ngame.each do |row|\n\tcolpos = 0\n\trow.each do |elem|\n\t\tres = elem if winning?(rowpos, colpos, elem, game)\n\t\tcolpos += 1\n\tend\n\trowpos += 1\nend\n\nputs res"}, {"source_code": "n = gets.to_i\nrow_sum = []\ncol_sum = Array.new(n) { 0 }\nn.times do\n\tsum = 0\n\tgets.split(\"\\s\").map { |x| x.to_i}.each_with_index do |x, i|\n\t\tcol_sum[i] += x\n\t\tsum += x\n\tend\n\trow_sum << sum\nend\n\ncells = 0\nj = 0\ncol_sum.sort!\nrow_sum.sort.each_with_index do |r, i|\n\twhile j < n\n\t\tif r < col_sum[j]\n\t\t\tbreak\n\t\telse\n\t\t\tj += 1\n\t\t\tcells += i\n\t\tend\n\tend\nend\n\nputs cells"}, {"source_code": "n = gets.chomp.to_i\nb = []\nrow_sums = []\ncol_sums = []\nfor i in 0...n\n b[i] = gets.chomp.split.map { |x| x.to_i }\n row_sums[i] = b[i].inject(:+)\n for j in 0...n\n col_sums[j] = (col_sums[j] || 0) + b[i][j]\n end\nend\n\ncount = 0\nfor i in 0...n\n for j in 0...n\n count += 1 if row_sums[i] > col_sums[j]\n end\nend\nputs count"}, {"source_code": "n = gets.to_i\nright = Array.new(n){0}\ndown = Array.new(n){0}\n\nn.times do |i|\n gets.split.each_with_index do |element,index|\n a =element.to_i\n right[i] = right[i] + a\n down[index] = down[index] + a\n end\nend\ncounter = 0\nright.sort!\ndown.sort!\ndown.each do |i|\n plus = right.find_index{|j| j>=i}\n counter = counter + plus unless plus.nil?\nend\np counter\n"}], "src_uid": "6e7c2c0d7d4ce952c53285eb827da21d"} {"nl": {"description": "There are $$$n$$$ candies in a row, they are numbered from left to right from $$$1$$$ to $$$n$$$. The size of the $$$i$$$-th candy is $$$a_i$$$.Alice and Bob play an interesting and tasty game: they eat candy. Alice will eat candy from left to right, and Bob — from right to left. The game ends if all the candies are eaten.The process consists of moves. During a move, the player eats one or more sweets from her/his side (Alice eats from the left, Bob — from the right).Alice makes the first move. During the first move, she will eat $$$1$$$ candy (its size is $$$a_1$$$). Then, each successive move the players alternate — that is, Bob makes the second move, then Alice, then again Bob and so on.On each move, a player counts the total size of candies eaten during the current move. Once this number becomes strictly greater than the total size of candies eaten by the other player on their previous move, the current player stops eating and the move ends. In other words, on a move, a player eats the smallest possible number of candies such that the sum of the sizes of candies eaten on this move is strictly greater than the sum of the sizes of candies that the other player ate on the previous move. If there are not enough candies to make a move this way, then the player eats up all the remaining candies and the game ends.For example, if $$$n=11$$$ and $$$a=[3,1,4,1,5,9,2,6,5,3,5]$$$, then: move 1: Alice eats one candy of size $$$3$$$ and the sequence of candies becomes $$$[1,4,1,5,9,2,6,5,3,5]$$$. move 2: Alice ate $$$3$$$ on the previous move, which means Bob must eat $$$4$$$ or more. Bob eats one candy of size $$$5$$$ and the sequence of candies becomes $$$[1,4,1,5,9,2,6,5,3]$$$. move 3: Bob ate $$$5$$$ on the previous move, which means Alice must eat $$$6$$$ or more. Alice eats three candies with the total size of $$$1+4+1=6$$$ and the sequence of candies becomes $$$[5,9,2,6,5,3]$$$. move 4: Alice ate $$$6$$$ on the previous move, which means Bob must eat $$$7$$$ or more. Bob eats two candies with the total size of $$$3+5=8$$$ and the sequence of candies becomes $$$[5,9,2,6]$$$. move 5: Bob ate $$$8$$$ on the previous move, which means Alice must eat $$$9$$$ or more. Alice eats two candies with the total size of $$$5+9=14$$$ and the sequence of candies becomes $$$[2,6]$$$. move 6 (the last): Alice ate $$$14$$$ on the previous move, which means Bob must eat $$$15$$$ or more. It is impossible, so Bob eats the two remaining candies and the game ends. Print the number of moves in the game and two numbers: $$$a$$$ — the total size of all sweets eaten by Alice during the game; $$$b$$$ — the total size of all sweets eaten by Bob during the game. ", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 5000$$$) — the number of test cases in the input. The following are descriptions of the $$$t$$$ test cases. Each test case consists of two lines. The first line contains an integer $$$n$$$ ($$$1 \\le n \\le 1000$$$) — the number of candies. The second line contains a sequence of integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 1000$$$) — the sizes of candies in the order they are arranged from left to right. It is guaranteed that the sum of the values of $$$n$$$ for all sets of input data in a test does not exceed $$$2\\cdot10^5$$$.", "output_spec": "For each set of input data print three integers — the number of moves in the game and the required values $$$a$$$ and $$$b$$$.", "sample_inputs": ["7\n11\n3 1 4 1 5 9 2 6 5 3 5\n1\n1000\n3\n1 1 1\n13\n1 2 3 4 5 6 7 8 9 10 11 12 13\n2\n2 1\n6\n1 1 1 1 1 1\n7\n1 1 1 1 1 1 1"], "sample_outputs": ["6 23 21\n1 1000 0\n2 1 2\n6 45 46\n2 2 1\n3 4 2\n4 4 3"], "notes": null}, "positive_code": [{"source_code": "t = gets.to_i\n\n(1).upto(t) do\n n = gets.to_i\n a = gets.split(' ').map(&:to_i)\n i = -1\n j = n\n turn = true\n x=0\n y=0\n z=0\n prev = 0\n cur = 0\n while(i+1prev)\n break\n end\n end\n else\n while(true)\n j-=1\n if(i==j)\n break\n end\n cur+=a[j]\n y+=a[j]\n if(cur>prev)\n break\n end\n end\n end\n z+=1\n turn = !turn\n end\n puts \"#{z} #{x} #{y}\"\nend\n"}, {"source_code": "# Created by IntelliJ IDEA.\n# User: gautam.roy\n# Date: 5/10/20\n# Time: 10:29 PM\n# To change this template use File | Settings | File Templates.\nt = gets.to_i\nfor i in 1..t do\n n = gets.to_i\n a = gets.split(' ').map { |s| s.to_i }\n atot = 0\n btot = 0\n moves = 0\n lind = 0\n rind = a.size - 1\n bsum = 0\n while lind <= rind\n # a moves\n asum = 0\n while lind <= rind && asum <= bsum\n asum = asum + a[lind]\n lind += 1\n end\n if asum != 0\n moves += 1\n atot += asum\n end\n # puts \"lind #{lind} asum #{asum}\"\n\n # b moves\n bsum = 0\n while lind <= rind && bsum <= asum\n bsum = bsum + a[rind]\n rind -= 1\n end\n if bsum != 0\n moves += 1\n btot += bsum\n end\n # puts \"rind #{rind} bsum #{bsum}\"\n end\n puts \"#{moves} #{atot} #{btot}\"\nend\n"}, {"source_code": "gets.to_i.times{\n n=gets.to_i\n a=gets.split.map &:to_i\n res=0, 0, 0\n s=t=0\n while a.size>0\n s=0\n s+=a.shift while a.size>0 and s<=t\n res[1]+=s\n res[0]+=1\n\n break if a.empty?\n\n t=0\n t+=a.pop while a.size>0 and t<=s\n res[2]+=t\n res[0]+=1\n end\n\n puts res*' '\n}\n"}, {"source_code": "# coding: utf-8\n\nt = gets.to_i\nt.times {\n n = gets.to_i\n moves, remains, alice, bob, last_eaten = 0, 0, 0, 0, 0\n\n a = gets.split.map { |i|\n i = i.to_i\n remains += i\n i\n }\n turn = :alice\n while !a.empty?\n if remains > last_eaten\n tmp = 0\n while tmp <= last_eaten\n tmp += a.shift if turn == :alice\n tmp += a.pop if turn == :bob\n end\n\n moves += 1\n remains -= tmp\n last_eaten = tmp\n alice += tmp if turn == :alice\n bob += tmp if turn == :bob\n\n if turn == :alice\n turn = :bob\n else\n turn = :alice\n end\n else\n break\n end\n end\n\n # clean-up\n if turn == :alice\n alice += remains\n else\n bob += remains\n end\n\n # and if remains are available, add one move (cleanup eating)\n if remains > 0\n moves += 1\n end\n \n puts \"#{moves} #{alice} #{bob}\"\n}"}], "negative_code": [{"source_code": "# coding: utf-8\n\nt = gets.to_i\nt.times {\n moves, remains, alice, bob, last_eaten = 0, 0, 0, 0, 0\n\n a = gets.split.map { |i|\n i = i.to_i\n remains += i\n i\n }\n turn = :alice\n while !a.empty?\n if remains > last_eaten\n tmp = 0\n while tmp <= last_eaten\n tmp += a.shift if turn == :alice\n tmp += a.pop if turn == :bob\n end\n\n moves += 1\n remains -= tmp\n last_eaten = tmp\n alice += tmp if turn == :alice\n bob += tmp if turn == :bob\n\n if turn == :alice\n turn = :bob\n else\n turn = :alice\n end\n else\n break\n end\n end\n\n # clean-up\n if turn == :alice\n alice += remains\n else\n bob += remains\n end\n\n # and if remains are available, add one move (cleanup eating)\n if remains > 0\n moves += 1\n end\n \n puts \"#{moves} #{alice} #{bob}\"\n}"}], "src_uid": "d70ee6d3574e0f2cac3c683729e2979d"} {"nl": {"description": "Valera takes part in the Berland Marathon. The marathon race starts at the stadium that can be represented on the plane as a square whose lower left corner is located at point with coordinates (0, 0) and the length of the side equals a meters. The sides of the square are parallel to coordinate axes.As the length of the marathon race is very long, Valera needs to have extra drink during the race. The coach gives Valera a bottle of drink each d meters of the path. We know that Valera starts at the point with coordinates (0, 0) and runs counter-clockwise. That is, when Valera covers a meters, he reaches the point with coordinates (a, 0). We also know that the length of the marathon race equals nd + 0.5 meters. Help Valera's coach determine where he should be located to help Valera. Specifically, determine the coordinates of Valera's positions when he covers d, 2·d, ..., n·d meters.", "input_spec": "The first line contains two space-separated real numbers a and d (1 ≤ a, d ≤ 105), given with precision till 4 decimal digits after the decimal point. Number a denotes the length of the square's side that describes the stadium. Number d shows that after each d meters Valera gets an extra drink. The second line contains integer n (1 ≤ n ≤ 105) showing that Valera needs an extra drink n times.", "output_spec": "Print n lines, each line should contain two real numbers xi and yi, separated by a space. Numbers xi and yi in the i-th line mean that Valera is at point with coordinates (xi, yi) after he covers i·d meters. Your solution will be considered correct if the absolute or relative error doesn't exceed 10 - 4. Note, that this problem have huge amount of output data. Please, do not use cout stream for output in this problem.", "sample_inputs": ["2 5\n2", "4.147 2.8819\n6"], "sample_outputs": ["1.0000000000 2.0000000000\n2.0000000000 0.0000000000", "2.8819000000 0.0000000000\n4.1470000000 1.6168000000\n3.7953000000 4.1470000000\n0.9134000000 4.1470000000\n0.0000000000 2.1785000000\n0.7034000000 0.0000000000"], "notes": null}, "positive_code": [{"source_code": "a, d = gets.split(' ').map { |e| e = e.to_f }\nn = gets.to_i\n\ndef getCoord st, c, a\n\tac = (a-c).round(4)\n\tc = c.round(4)\n\n\tcase st\n\t\twhen 0 then \"#{c} 0\"\n\t\twhen 1 then \"#{a} #{c}\"\n\t\twhen 2 then \"#{a-c} #{a}\"\n\t\twhen 3 then \"0 #{a - c}\"\n\tend\nend\n\n\nstate = 0\nc = 0\ncd = d\n\nfor i in 0...n\n\tm = (cd / a).floor\n\tstate = (state + m) % 4\n\tc = cd - m * a\n\tcd = c + d\n\n\tputs getCoord state, c, a\nend"}, {"source_code": "eps = 1e-5\na,d = gets.chomp!.split.map { |e| e.to_f}\nn =gets.chomp!.to_i\ncur_x=0.00\ncur_y=0.00\npp=0.00\n\nn.times {\n\tif cur_y <= eps\n\t\tpp = cur_x\t\n\t#\tp 0\n\telsif a-cur_x <= eps\n\t\tpp = a + cur_y\n\t#\tp 1\n\telsif a-cur_y <= eps\n\t\tpp = a*3 -cur_x\t\n\t#\tp 2\n\telse\n\t\tpp = a*4 -cur_y\n\t#\tp 3\n\tend\t\n\tpp +=d;\n\tt= (pp/a).floor\n\t# puts \"asd #{pp} #{t} #{pp}\"\n\tpp -= 1.0*t*a;\n\n\tcase t%4\n\twhen 0\n\t\tcur_x=pp\n\t\tcur_y=0\n\twhen 1\n\t\tcur_x=a\n\t\tcur_y=pp\n\twhen 2\n\t\tcur_x=a-pp\n\t\tcur_y=a\n\twhen 3\n\t\tcur_x=0\n\t\tcur_y=a-pp\n\tend\n\t\n\tprintf(\"%.8f %.8f\\n\",cur_x,cur_y)\n}"}], "negative_code": [], "src_uid": "d00b8423c3c52b19fec25bc63e4d4c1c"} {"nl": {"description": "The problem uses a simplified TCP/IP address model, please make sure you've read the statement attentively.Polycarpus has found a job, he is a system administrator. One day he came across n IP addresses. Each IP address is a 32 bit number, represented as a group of four 8-bit numbers (without leading zeroes), separated by dots. For example, the record 0.255.1.123 shows a correct IP address and records 0.256.1.123 and 0.255.1.01 do not. In this problem an arbitrary group of four 8-bit numbers is a correct IP address.Having worked as an administrator for some time, Polycarpus learned that if you know the IP address, you can use the subnet mask to get the address of the network that has this IP addess.The subnet mask is an IP address that has the following property: if we write this IP address as a 32 bit string, that it is representable as \"11...11000..000\". In other words, the subnet mask first has one or more one bits, and then one or more zero bits (overall there are 32 bits). For example, the IP address 2.0.0.0 is not a correct subnet mask as its 32-bit record looks as 00000010000000000000000000000000.To get the network address of the IP address, you need to perform the operation of the bitwise \"and\" of the IP address and the subnet mask. For example, if the subnet mask is 255.192.0.0, and the IP address is 192.168.1.2, then the network address equals 192.128.0.0. In the bitwise \"and\" the result has a bit that equals 1 if and only if both operands have corresponding bits equal to one.Now Polycarpus wants to find all networks to which his IP addresses belong. Unfortunately, Polycarpus lost subnet mask. Fortunately, Polycarpus remembers that his IP addresses belonged to exactly k distinct networks. Help Polycarpus find the subnet mask, such that his IP addresses will belong to exactly k distinct networks. If there are several such subnet masks, find the one whose bit record contains the least number of ones. If such subnet mask do not exist, say so.", "input_spec": "The first line contains two integers, n and k (1 ≤ k ≤ n ≤ 105) — the number of IP addresses and networks. The next n lines contain the IP addresses. It is guaranteed that all IP addresses are distinct.", "output_spec": "In a single line print the IP address of the subnet mask in the format that is described in the statement, if the required subnet mask exists. Otherwise, print -1.", "sample_inputs": ["5 3\n0.0.0.1\n0.1.1.2\n0.0.2.1\n0.1.1.0\n0.0.2.3", "5 2\n0.0.0.1\n0.1.1.2\n0.0.2.1\n0.1.1.0\n0.0.2.3", "2 1\n255.0.0.1\n0.0.0.2"], "sample_outputs": ["255.255.254.0", "255.255.0.0", "-1"], "notes": null}, "positive_code": [{"source_code": "class MaskGuesser\n def initialize num_networks, ip_list\n raise Exception unless ip_list.kind_of?(Array)\n @num_networks = num_networks\n @ip_list = Array.new(ip_list.map {|ip| ip_to_bitmask(ip)})\n end\n\n def guess_mask\n mask = 0\n 31.downto(1) do |i|\n mask += 2**i\n masked_ip_list = @ip_list.map {|ip| ip&mask}.uniq\n return bitmask_to_ip(mask) if masked_ip_list.length == @num_networks\n end\n return \"-1\"\n end\n\n def bitmask_to_ip bitmask\n filter = 255\n a = (bitmask & (filter << 24)) >> 24\n b = (bitmask & (filter << 16)) >> 16\n c = (bitmask & (filter << 8)) >> 8\n d = bitmask & filter\n return \"#{a}.#{b}.#{c}.#{d}\"\n end\n\n def ip_to_bitmask ip\n bit_segments = ip.split('.').map(&:to_i)\n raise Exception if bit_segments.length != 4\n bitmask = bit_segments[0] << 24 | bit_segments[1] << 16 | bit_segments[2] << 8 | bit_segments[3]\n #bitmask = (bit_segments[0] << 24) + (bit_segments[1] << 16) + (bit_segments[2] << 8) + bit_segments[3]\n return bitmask\n end\nend\n\ndef main\n num_ip, num_networks = gets.split(/\\s+/).map(&:to_i)\n ip_list = []\n num_ip.downto(1) do\n ip_list << gets\n end\n m = MaskGuesser.new(num_networks, ip_list)\n puts m.guess_mask\nend\n\nmain"}, {"source_code": "D = [2 ** 24, 2 ** 16, 2 ** 8, 1]\n\ndef from_ip(ip)\n ip.split('.').each_with_index.map{ |n, i| n.to_i * D[i] }.reduce(:+)\nend\n\ndef to_ip(ip)\n 4.times.map{ |i| (ip / D[i]).tap{ ip %= D[i] } }.join('.')\nend\n\ndef mask(n)\n ('1' * n << '0' * (32 - n)).to_i(2)\nend\n\nn, k = gets.split.map(&:to_i)\n\nips = []\n\nn.times{ ips << from_ip(gets) }\nips.sort!\n\n32.times do |i|\n next if i == 0 || 2 ** i < k\n m = mask(i)\n net = -1\n c = 0\n ips.each do |ip|\n ip_net = ip & m\n unless ip_net == net\n net = ip_net\n c += 1\n end\n end\n\n if c == k\n puts to_ip(m)\n exit\n end\n\nend\n\nputs -1\n"}, {"source_code": "at_exit do\n n,k = get_numbers_from_line[0..1]\n ips = []\n n.times do\n octets = gets.strip.split('.').map(&:to_i)\n ips << (0...4).reduce(0){|result,i| result + (octets[i] << (8*i)) }\n end\n\n ip_and = 1<<32 - 1\n ip_or = 0\n (0...n).each do |i|\n ip_and &= ips[i]\n ip_or |= ips[i]\n end\n ip_xor = ip_and ^ ip_or\n\n result_mask = '-1'\n mask = 0\n start_i = [Math.log(k,2).ceil - 1, 0].max\n (0...start_i).each do |i|\n octet = i/8\n bit = 7 - i%8\n mask |= 1 << (octet*8 + bit)\n end\n\n i = start_i\n while i < 32\n octet = i/8\n bit = 7 - i%8\n absolute_bit = octet*8 + bit\n mask |= 1 << absolute_bit\n nets = ips.map{|ip| ip & mask }.uniq\n if nets.length == k\n result_mask = (1..4).map{|j| (mask & ((1<> ((j-1)*8)}.join('.')\n break\n elsif nets.length > k\n break\n end\n\n while true\n i += 1\n break if i >= 32\n octet = i/8\n bit = 7 - i%8\n absolute_bit = octet*8 + bit\n mask |= 1 << absolute_bit\n break if (ip_xor & (1 << absolute_bit)) != 0\n end\n end\n\n puts result_mask\nend\n\ndef get_numbers_from_line\n gets.strip.split.map(&:to_i)\nend"}], "negative_code": [{"source_code": "at_exit do\n n,k = get_numbers_from_line[0..1]\n ips = []\n n.times do\n octets = gets.strip.split('.').map(&:to_i)\n ips << (0...4).reduce(0){|result,i| result + (octets[i] << (8*i)) }\n end\n\n ip_and = 1<<32 - 1\n ip_or = 0\n (0...n).each do |i|\n ip_and &= ips[i]\n ip_or |= ips[i]\n end\n ip_xor = ip_and ^ ip_or\n\n result_mask = '-1'\n start_i = Math.log(k,2).ceil - 1\n mask = 0\n (0...start_i).each do |i|\n octet = i/8\n bit = 7 - i%8\n mask |= 1 << (octet*8 + bit)\n end\n\n i = start_i\n while i < 32\n octet = i/8\n bit = 7 - i%8\n absolute_bit = octet*8 + bit\n mask |= 1 << absolute_bit\n nets = ips.map{|ip| ip & mask }.uniq\n if nets.length == k\n result_mask = (1..4).map{|j| (mask & ((1<> ((j-1)*8)}.join('.')\n break\n elsif nets.length > k\n break\n end\n\n while i < 32\n i += 1\n octet = i/8\n bit = 7 - i%8\n absolute_bit = octet*8 + bit\n mask |= 1 << absolute_bit\n break if (ip_xor & (1 << absolute_bit)) != 0\n end\n end\n\n puts result_mask\nend\n\ndef get_numbers_from_line\n gets.strip.split.map(&:to_i)\nend"}, {"source_code": "at_exit do\n n,k = get_numbers_from_line[0..1]\n ips = []\n n.times do\n ips << gets.strip.split('.').map(&:to_i)\n end\n\n result_mask = '-1'\n start_i = Math.log(k,2).floor\n mask = [0]*4\n (0...start_i).each do |i|\n octet = i/8\n bit = 7 - i%8\n mask[octet] |= 1 << bit\n end\n\n (start_i...32).each do |i|\n octet = i/8\n bit = 7 - i%8\n mask[octet] |= 1 << bit\n nets = ips.map{|ip| get_net_ip(ip, mask) }.uniq\n if nets.length == k\n result_mask = mask.join('.')\n break\n elsif nets.length > k\n break\n end\n end\n\n puts result_mask\nend\n\ndef get_net_ip(ip, mask)\n (0...4).map{|i| ip[i] & mask[i] }\nend\n\ndef get_numbers_from_line\n gets.strip.split.map(&:to_i)\nend"}, {"source_code": "at_exit do\n n,k = get_numbers_from_line[0..1]\n ips = []\n n.times do\n ips << gets.strip.split('.').map(&:to_i)\n end\n\n result_mask = '-1'\n start_i = Math.log(k,2).ceil\n mask = [0]*4\n (0...start_i).each do |i|\n octet = i/8\n bit = 7 - i%8\n mask[octet] |= 1 << bit\n end\n (start_i...32).each do |i|\n octet = i/8\n bit = 7 - i%8\n mask[octet] |= 1 << bit\n nets = ips.map{|ip| get_net_ip(ip, mask) }.uniq\n if nets.length == k\n result_mask = mask.join('.')\n break\n elsif nets.length > k\n break\n end\n end\n\n puts result_mask\nend\n\ndef get_net_ip(ip, mask)\n (0...4).map{|i| ip[i] & mask[i] }\nend\n\ndef get_numbers_from_line\n gets.strip.split.map(&:to_i)\nend"}], "src_uid": "4da4410d3b75ee87a4dfa33b437b9cfa"} {"nl": {"description": "Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo of Kiev got hold of lots of wooden cubes somewhere. They started making cube towers by placing the cubes one on top of the other. They defined multiple towers standing in a line as a wall. A wall can consist of towers of different heights.Horace was the first to finish making his wall. He called his wall an elephant. The wall consists of w towers. The bears also finished making their wall but they didn't give it a name. Their wall consists of n towers. Horace looked at the bears' tower and wondered: in how many parts of the wall can he \"see an elephant\"? He can \"see an elephant\" on a segment of w contiguous towers if the heights of the towers on the segment match as a sequence the heights of the towers in Horace's wall. In order to see as many elephants as possible, Horace can raise and lower his wall. He even can lower the wall below the ground level (see the pictures to the samples for clarification).Your task is to count the number of segments where Horace can \"see an elephant\".", "input_spec": "The first line contains two integers n and w (1 ≤ n, w ≤ 2·105) — the number of towers in the bears' and the elephant's walls correspondingly. The second line contains n integers ai (1 ≤ ai ≤ 109) — the heights of the towers in the bears' wall. The third line contains w integers bi (1 ≤ bi ≤ 109) — the heights of the towers in the elephant's wall.", "output_spec": "Print the number of segments in the bears' wall where Horace can \"see an elephant\".", "sample_inputs": ["13 5\n2 4 5 5 4 3 2 2 2 3 3 2 1\n3 4 4 3 2"], "sample_outputs": ["2"], "notes": "NoteThe picture to the left shows Horace's wall from the sample, the picture to the right shows the bears' wall. The segments where Horace can \"see an elephant\" are in gray. "}, "positive_code": [{"source_code": "#!/usr/bin/env ruby\n\ndef pow(a, b, m)\n return (b == 0 ? 1 : pow(a * a % m, b / 2, m) * (b % 2 == 0 ? 1 : a)) % m\nend\n\nP = 1000000007\nM = 2345678917\nn, m = gets.split.map(&:to_i)\n\nif m == 1\n puts n\n exit\nend\n\na = gets.split.map(&:to_i)\nb = gets.split.map(&:to_i)\n\nda = a.drop(1).zip(a[0..-2]).map{|a, b| a - b}\nhb = b.drop(1).zip(b[0..-2]).map{|a, b| a - b}.inject{|a, x| (a * P + x) % M} \n\nn -= 1\nm -= 1\nMP = pow(P, m, M)\n\nans = 0\nha = 0\nda.each_with_index do |x, i|\n ha = (ha * P + x) % M\n ha = (ha - da[i - m] * MP) % M if i >= m\n ans += 1 if i >= m - 1 && ha == hb\nend\n\nputs ans\n"}], "negative_code": [{"source_code": "#!/usr/bin/env ruby\n\ndef pow(a, b, m)\n return (b == 0 ? 1 : pow(a * a % m, b / 2, m) * (b % 2 == 0 ? 1 : a)) % m\nend\n\nP = 1000000007\nM = 2345678917\nn, m = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\nb = gets.split.map(&:to_i)\n\nda = a.drop(1).zip(a[0..-2]).map{|a, b| a - b}\nhb = b.drop(1).zip(b[0..-2]).map{|a, b| a - b}.inject{|a, x| (a * P + x) % M} \n\nn -= 1\nm -= 1\nMP = pow(P, m, M)\n\nans = 0\nha = 0\nda.each_with_index do |x, i|\n ha = (ha * P + x) % M\n ha = (ha - da[i - m] * MP) % M if i >= m\n ans += 1 if i >= m - 1 && ha == hb\nend\n\nputs ans\n"}], "src_uid": "37447ade3cad88e06c1f407576e4a041"} {"nl": {"description": "Being stuck at home, Ray became extremely bored. To pass time, he asks Lord Omkar to use his time bending power: Infinity Clock! However, Lord Omkar will only listen to mortals who can solve the following problem:You are given an array $$$a$$$ of $$$n$$$ integers. You are also given an integer $$$k$$$. Lord Omkar wants you to do $$$k$$$ operations with this array.Define one operation as the following: Set $$$d$$$ to be the maximum value of your array. For every $$$i$$$ from $$$1$$$ to $$$n$$$, replace $$$a_{i}$$$ with $$$d-a_{i}$$$. The goal is to predict the contents in the array after $$$k$$$ operations. Please help Ray determine what the final sequence will look like!", "input_spec": "Each test contains multiple test cases. The first line contains the number of cases $$$t$$$ ($$$1 \\le t \\le 100$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\leq n \\leq 2 \\cdot 10^5, 1 \\leq k \\leq 10^{18}$$$) – the length of your array and the number of operations to perform. The second line of each test case contains $$$n$$$ integers $$$a_{1},a_{2},...,a_{n}$$$ $$$(-10^9 \\leq a_{i} \\leq 10^9)$$$ – the initial contents of your array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each case, print the final version of array $$$a$$$ after $$$k$$$ operations described above.", "sample_inputs": ["3\n2 1\n-199 192\n5 19\n5 -1 4 2 0\n1 2\n69"], "sample_outputs": ["391 0\n0 6 1 3 5\n0"], "notes": "NoteIn the first test case the array changes as follows:Initially, the array is $$$[-199, 192]$$$. $$$d = 192$$$.After the operation, the array becomes $$$[d-(-199), d-192] = [391, 0]$$$."}, "positive_code": [{"source_code": "t = gets.chomp.to_i\nt.times {\n nk = gets.chomp.split(' ').map(&:to_i)\n n = nk[0]\n k = nk[1]\n arr = gets.chomp.split(' ').map(&:to_i)\n max = arr.max\n arr.map! { |ele| max - ele }\n max = arr.max\n if k % 2 == 0\n arr.map! { |ele| max - ele }\n end\n arr.each {|ele| print \"%d \" % ele }\n print \"\\n\"\n}"}, {"source_code": "t = gets.chomp.to_i\nt.times {\n nk = gets.chomp.split(' ').map(&:to_i)\n n = nk[0]\n k = nk[1]\n arr = gets.chomp.split(' ').map(&:to_i)\n max = arr.max\n arr.map! { |ele| max - ele }\n max = arr.max\n if k % 2 == 0\n arr.map! { |ele| max - ele }\n end\n arr.each {|ele| print \"%d \" % ele }\n # print \"\\n\"\n}"}, {"source_code": "def get_max(a)\n\tres = a[0]\n\tfor i in 1..(a.length-1)\n\t\tif a[i] > res\n\t\t\tres = a[i]\n\t\tend\n\tend\n\tres\nend\n\ndef flip_array!(a)\n\tm = get_max(a)\n\tfor i in 0..(a.length-1)\n\t\ta[i] = m - a[i]\n\tend\n\tnil\nend\n\nt = gets.chomp.to_i\nfor tc in 1..t\n\ta = gets.split\n\tn = a[0].to_i\n\tk = a[1].to_i\n\ta = gets.split\n\tfor i in 0..(n-1)\n\t\ta[i] = a[i].to_i\n\tend\n\tflip_array!(a)\n\t\n\tif (k%2 == 0) \n\t\tflip_array!(a)\n\tend\n\tfor i in 0..(n-1)\n\t\tprint(a[i])\n\t\tprint(\" \")\n\tend\n\tprint(\"\\n\")\nend"}], "negative_code": [{"source_code": "t = gets.chomp.to_i\nt.times {\n nk = gets.chomp.split(' ').map(&:to_i)\n n = nk[0]\n k = nk[1]\n arr = gets.chomp.split(' ').map(&:to_i)\n max = arr.max\n arr.map! { |ele| max - ele }\n max = arr.max\n if k % 2 == 0\n arr.map! { |ele| max - ele }\n end\n p arr.join(' ')\n}"}], "src_uid": "f18a5fa0b2e7e96cb5994b7b2dbe0189"} {"nl": {"description": "In a galaxy far, far away Lesha the student has just got to know that he has an exam in two days. As always, he hasn't attended any single class during the previous year, so he decided to spend the remaining time wisely.Lesha knows that today he can study for at most $$$a$$$ hours, and he will have $$$b$$$ hours to study tomorrow. Note that it is possible that on his planet there are more hours in a day than on Earth. Lesha knows that the quality of his knowledge will only depend on the number of lecture notes he will read. He has access to an infinite number of notes that are enumerated with positive integers, but he knows that he can read the first note in one hour, the second note in two hours and so on. In other words, Lesha can read the note with number $$$k$$$ in $$$k$$$ hours. Lesha can read the notes in arbitrary order, however, he can't start reading a note in the first day and finish its reading in the second day.Thus, the student has to fully read several lecture notes today, spending at most $$$a$$$ hours in total, and fully read several lecture notes tomorrow, spending at most $$$b$$$ hours in total. What is the maximum number of notes Lesha can read in the remaining time? Which notes should he read in the first day, and which — in the second?", "input_spec": "The only line of input contains two integers $$$a$$$ and $$$b$$$ ($$$0 \\leq a, b \\leq 10^{9}$$$) — the number of hours Lesha has today and the number of hours Lesha has tomorrow.", "output_spec": "In the first line print a single integer $$$n$$$ ($$$0 \\leq n \\leq a$$$) — the number of lecture notes Lesha has to read in the first day. In the second line print $$$n$$$ distinct integers $$$p_1, p_2, \\ldots, p_n$$$ ($$$1 \\leq p_i \\leq a$$$), the sum of all $$$p_i$$$ should not exceed $$$a$$$. In the third line print a single integer $$$m$$$ ($$$0 \\leq m \\leq b$$$) — the number of lecture notes Lesha has to read in the second day. In the fourth line print $$$m$$$ distinct integers $$$q_1, q_2, \\ldots, q_m$$$ ($$$1 \\leq q_i \\leq b$$$), the sum of all $$$q_i$$$ should not exceed $$$b$$$. All integers $$$p_i$$$ and $$$q_i$$$ should be distinct. The sum $$$n + m$$$ should be largest possible.", "sample_inputs": ["3 3", "9 12"], "sample_outputs": ["1\n3 \n2\n2 1", "2\n3 6\n4\n1 2 4 5"], "notes": "NoteIn the first example Lesha can read the third note in $$$3$$$ hours in the first day, and the first and the second notes in one and two hours correspondingly in the second day, spending $$$3$$$ hours as well. Note that Lesha can make it the other way round, reading the first and the second notes in the first day and the third note in the second day.In the second example Lesha should read the third and the sixth notes in the first day, spending $$$9$$$ hours in total. In the second day Lesha should read the first, second fourth and fifth notes, spending $$$12$$$ hours in total."}, "positive_code": [{"source_code": "a,b = gets.split.map(&:to_i)\nt = Math.sqrt((a+b)*2).floor\nfor i in t..t+3\n if (a+b)*2 < i*(i+1)\n m = i-1\n break\n end\nend\n\nc,d = [],[]\nm.downto(1) do |i|\n if a >= i\n a -= i\n c.push(i)\n else\n b -= i\n d.push(i)\n end\nend\n\nputs c.size,c.join(\" \"),d.size,d.join(\" \")\n"}], "negative_code": [], "src_uid": "fab438cef9eb5e9e4a4a2e3f9e4f9cec"} {"nl": {"description": "Positive integer $$$x$$$ is called divisor of positive integer $$$y$$$, if $$$y$$$ is divisible by $$$x$$$ without remainder. For example, $$$1$$$ is a divisor of $$$7$$$ and $$$3$$$ is not divisor of $$$8$$$.We gave you an integer $$$d$$$ and asked you to find the smallest positive integer $$$a$$$, such that $$$a$$$ has at least $$$4$$$ divisors; difference between any two divisors of $$$a$$$ is at least $$$d$$$.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 3000$$$) — the number of test cases. The first line of each test case contains a single integer $$$d$$$ ($$$1 \\leq d \\leq 10000$$$).", "output_spec": "For each test case print one integer $$$a$$$ — the answer for this test case.", "sample_inputs": ["2\n1\n2"], "sample_outputs": ["6\n15"], "notes": "NoteIn the first test case, integer $$$6$$$ have following divisors: $$$[1, 2, 3, 6]$$$. There are $$$4$$$ of them and the difference between any two of them is at least $$$1$$$. There is no smaller integer with at least $$$4$$$ divisors.In the second test case, integer $$$15$$$ have following divisors: $$$[1, 3, 5, 15]$$$. There are $$$4$$$ of them and the difference between any two of them is at least $$$2$$$.The answer $$$12$$$ is INVALID because divisors are $$$[1, 2, 3, 4, 6, 12]$$$. And the difference between, for example, divisors $$$2$$$ and $$$3$$$ is less than $$$d=2$$$."}, "positive_code": [{"source_code": "require \"prime\"\r\na=[1]+Prime.each(100000).to_a\r\naz=a.size\r\ngets.to_i.times do\r\n d=gets.to_i\r\n l=0\r\n r=az-1\r\n while r-l>1\r\n m=(l+r)/2\r\n if a[m]<=d\r\n l=m\r\n else\r\n r=m\r\n end\r\n end\r\n x=a[r]\r\n l=0\r\n r=az-1\r\n while r-l>1\r\n m=(l+r)/2\r\n if a[m]x\n\n break if min==0\nend\n\nputs min"}, {"source_code": "n = gets.to_i\nd = gets.to_i\ne = gets.to_i\n\nans = n\n\n(0..(n/d)).each do |dn|\n ans = [ans, (n - (d*dn)) % (e*5)].min\nend\n\nputs ans"}], "negative_code": [{"source_code": "n = gets.to_i\nd = gets.to_i\ne = gets.to_i\n\nans = 10 ** 20\ntmp = n % d % (e*5)\ntmp2 = n % (e*5) % d\nans = [ans, tmp, tmp2].min\nputs ans"}, {"source_code": "n = gets.to_i\nd = gets.to_i\ne = gets.to_i\n\nans = n\n\ntmp = n % d % (e*5)\ntmp2 = n % (e*5) % d\nans = [ans, tmp, tmp2].min\nputs ans"}], "src_uid": "8c5d9b4fd297706fac3be83fc85028a0"} {"nl": {"description": "Polycarp has $$$3$$$ positive integers $$$a$$$, $$$b$$$ and $$$c$$$. He can perform the following operation exactly once. Choose a positive integer $$$m$$$ and multiply exactly one of the integers $$$a$$$, $$$b$$$ or $$$c$$$ by $$$m$$$. Can Polycarp make it so that after performing the operation, the sequence of three numbers $$$a$$$, $$$b$$$, $$$c$$$ (in this order) forms an arithmetic progression? Note that you cannot change the order of $$$a$$$, $$$b$$$ and $$$c$$$.Formally, a sequence $$$x_1, x_2, \\dots, x_n$$$ is called an arithmetic progression (AP) if there exists a number $$$d$$$ (called \"common difference\") such that $$$x_{i+1}=x_i+d$$$ for all $$$i$$$ from $$$1$$$ to $$$n-1$$$. In this problem, $$$n=3$$$.For example, the following sequences are AP: $$$[5, 10, 15]$$$, $$$[3, 2, 1]$$$, $$$[1, 1, 1]$$$, and $$$[13, 10, 7]$$$. The following sequences are not AP: $$$[1, 2, 4]$$$, $$$[0, 1, 0]$$$ and $$$[1, 3, 2]$$$.You need to answer $$$t$$$ independent test cases.", "input_spec": "The first line contains the number $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. Each of the following $$$t$$$ lines contains $$$3$$$ integers $$$a$$$, $$$b$$$, $$$c$$$ ($$$1 \\le a, b, c \\le 10^8$$$).", "output_spec": "For each test case print \"YES\" (without quotes) if Polycarp can choose a positive integer $$$m$$$ and multiply exactly one of the integers $$$a$$$, $$$b$$$ or $$$c$$$ by $$$m$$$ to make $$$[a, b, c]$$$ be an arithmetic progression. Print \"NO\" (without quotes) otherwise. You can print YES and NO in any (upper or lower) case (for example, the strings yEs, yes, Yes and YES will be recognized as a positive answer).", "sample_inputs": ["11\n10 5 30\n30 5 10\n1 2 3\n1 6 3\n2 6 3\n1 1 1\n1 1 2\n1 1 3\n1 100000000 1\n2 1 1\n1 2 2"], "sample_outputs": ["YES\nYES\nYES\nYES\nNO\nYES\nNO\nYES\nYES\nNO\nYES"], "notes": "NoteIn the first and second test cases, you can choose the number $$$m=4$$$ and multiply the second number ($$$b=5$$$) by $$$4$$$.In the first test case the resulting sequence will be $$$[10, 20, 30]$$$. This is an AP with a difference $$$d=10$$$.In the second test case the resulting sequence will be $$$[30, 20, 10]$$$. This is an AP with a difference $$$d=-10$$$.In the third test case, you can choose $$$m=1$$$ and multiply any number by $$$1$$$. The resulting sequence will be $$$[1, 2, 3]$$$. This is an AP with a difference $$$d=1$$$.In the fourth test case, you can choose $$$m=9$$$ and multiply the first number ($$$a=1$$$) by $$$9$$$. The resulting sequence will be $$$[9, 6, 3]$$$. This is an AP with a difference $$$d=-3$$$.In the fifth test case, it is impossible to make an AP."}, "positive_code": [{"source_code": "inputs = STDIN.read.strip.split(\"\\n\")\r\ninputs.shift\r\n\r\ninputs.each do |inp|\r\n a, b, c = inp.split(' ').map(&:to_i)\r\n\r\n if a - b == b - c\r\n puts \"YES\"\r\n else\r\n if (2 * b - c) % a == 0 && (2 * b - c) / a > 0\r\n puts \"YES\"\r\n elsif (a + c) % (2 * b) == 0 && (a + c) / (2 * b) > 0\r\n puts \"YES\"\r\n elsif (2 * b - a) % c == 0 && (2 * b - a) / c > 0\r\n puts \"YES\"\r\n else\r\n puts \"NO\"\r\n end\r\n end\r\nend"}, {"source_code": "gets.to_i.times do\n a, b, c = gets.split.map(&:to_i)\n\n # Is it `a`?\n d = c - b\n aa = b - d\n if aa % a == 0 && aa / a > 0\n puts \"YES\"\n next\n end\n\n # `b`?\n d = c - a\n if d % 2 == 0\n d /= 2\n bb = c - d\n if bb % b == 0 && bb / b > 0\n puts \"YES\"\n next\n end\n end\n\n # `c`?\n d = b - a\n cc = b + d\n if cc % c == 0 && cc / c > 0\n puts \"YES\"\n next\n end\n\n puts \"NO\"\nend\n"}, {"source_code": "#!/usr/bin/ruby\ninput = ARGF.read.each_line(chomp: true).to_a\nheader = input.shift\ninput.each do |line|\n z = line.split.map(&:to_i)\n puts ((2*z[1] -z[2] > 0 and (2*z[1] - z[2]) % z[0] == 0) or\n ((z[0] + z[2]) % (2*z[1]) == 0) or\n (2*z[1] -z[0] > 0 and (2*z[1] - z[0]) % z[2] == 0)) ? \"YES\" : \"NO\"\nend\n"}, {"source_code": "t = readline.strip.to_i\n\nt.times do\n a, b, c = readline.strip.split(/\\s+/).map(&:to_i)\n if a == b && b == c\n puts 'YES'\n next\n end\n\n # case 1 |a| b c\n d = c - b\n if ((b - d) % a == 0) && ((c - 2*d) % a == 0)\n m = (b-d) / a\n if m > 0\n puts 'YES'\n next\n end\n end\n\n # case a |b| c\n d = c - a\n if d % 2 == 0\n d /= 2\n if ((a+d) % b == 0) && ((c-d) % b == 0)\n m = (a+d) / b\n if m > 0\n puts 'YES'\n next\n end\n end\n end\n\n # case a b |c|\n d = b - a\n if ((b+d) % c == 0) && ((a+2*d) % c == 0)\n m = (b + d) / c\n if m > 0\n puts 'YES'\n next\n end\n end\n\n puts 'NO'\nend\n\n"}], "negative_code": [{"source_code": "inputs = STDIN.read.strip.split(\"\\n\")\r\ninputs.shift\r\n\r\ninputs.each do |inp|\r\n a, b, c = inp.split(' ').map(&:to_i)\r\n\r\n min = [a,c].min\r\n max = [a,c].max\r\n\r\n if (a > c || a < c) && max - min > 1\r\n puts \"YES\"\r\n else\r\n if a == b && b == c\r\n puts \"YES\"\r\n else\r\n if (a + c) * b == b * 2\r\n puts \"YES\"\r\n elsif (b == c && a * b == b) || (a == b && b * c == b)\r\n puts \"YES\"\r\n else\r\n puts \"NO\"\r\n end\r\n end\r\n end\r\nend"}, {"source_code": "inputs = STDIN.read.strip.split(\"\\n\")\r\ninputs.shift\r\n\r\ninputs.each do |inp|\r\n a, b, c = inp.split(' ').map(&:to_i)\r\n\r\n if a > c\r\n a, c = c, a\r\n end\r\n\r\n # pp \"#{a}, #{b}, #{c}\"\r\n x = Hash[[a,b,c].tally.to_a.reverse]\r\n if x[2].to_i * x[1].to_i == x[2].to_i\r\n puts \"YES\"\r\n next\r\n end\r\n if (a == b && b == c) || (a == 1 && c == 1)\r\n puts \"YES\"\r\n next\r\n end\r\n if c - a < 2\r\n puts \"NO\"\r\n next\r\n end\r\n if a + b != c && a + (c - a) != c\r\n puts \"NO\"\r\n next\r\n end\r\n\r\n puts \"YES\"\r\nend"}, {"source_code": "gets.to_i.times do\n a, b, c = gets.split.map(&:to_i)\n\n # Is it `a`?\n d = c - b\n aa = b - d\n if aa % a == 0 && aa / a > 0\n puts \"YES\"\n next\n end\n\n # `b`?\n d = c - a\n if d % 2 == 0\n bb = c - d\n if bb % b == 0 && bb / b > 0\n puts \"YES\"\n next\n end\n end\n\n # `c`?\n d = b - a\n cc = b + d\n if cc % c == 0 && cc / c > 0\n puts \"YES\"\n next\n end\n\n puts \"NO\"\nend\n"}], "src_uid": "90c5058c0c7f55a567f2e036482149f9"} {"nl": {"description": "Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 points. A throw is worth 2 points if the distance it was made from doesn't exceed some value of d meters, and a throw is worth 3 points if the distance is larger than d meters, where d is some non-negative integer.Vasya would like the advantage of the points scored by the first team (the points of the first team minus the points of the second team) to be maximum. For that he can mentally choose the value of d. Help him to do that.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 2·105) — the number of throws of the first team. Then follow n integer numbers — the distances of throws ai (1 ≤ ai ≤ 2·109). Then follows number m (1 ≤ m ≤ 2·105) — the number of the throws of the second team. Then follow m integer numbers — the distances of throws of bi (1 ≤ bi ≤ 2·109).", "output_spec": "Print two numbers in the format a:b — the score that is possible considering the problem conditions where the result of subtraction a - b is maximum. If there are several such scores, find the one in which number a is maximum.", "sample_inputs": ["3\n1 2 3\n2\n5 6", "5\n6 7 8 9 10\n5\n1 2 3 4 5"], "sample_outputs": ["9:6", "15:10"], "notes": null}, "positive_code": [{"source_code": "#! /usr/bin/env ruby\n# coding: utf-8\n\nclass Score\n def self.start\n self.new\n end\n\n def initialize\n aggregate\n end\n\n def aggregate\n n = gets.chomp.to_i\n @f = Team.new(gets.chomp.split.map(&:to_i))\n m = gets.chomp.to_i\n @s = Team.new(gets.chomp.split.map(&:to_i))\n end\n\n def result\n f_score = @f.score\n s_score = @s.score\n f = @f.score_l\n s = @s.score_l\n n = f.length\n m = s.length\n\n ans = -1000000\n diff_score = f_score - s_score\n if ans < f_score - s_score\n ans = diff_score\n ans_f = f_score\n ans_s = s_score\n end\n\n j = 0\n @s.score_l.length.times do |i|\n s_score -= 1\n while j < n and f[j] <= s[i]\n f_score -= 1\n j += 1\n end\n diff_score = f_score - s_score\n if ans < diff_score\n ans = diff_score\n ans_f = f_score\n ans_s = s_score\n end\n end\n return \"#{ans_f}:#{ans_s}\"\n end\nend\n\nclass Team\n attr_reader :score_l, :score\n def initialize(score_l)\n @score_l = score_l.sort\n @score = @score_l.length * 3\n end\nend\n\ns = Score.start\nputs s.result\n"}, {"source_code": "class Info\n attr_accessor :dist\n attr_accessor :team\n\n def initialize (d, t)\n @dist = d\n @team = t\n end\nend\n\ngets\nfirst = gets.split(' ').map do |x|\n Info.new(x.to_i, 1)\nend\n\ngets\nsecond = gets.split(' ').map do |x|\n Info.new(x.to_i, 2)\nend\n\nres = first + second\nres.sort_by! { |x| -x.dist }\n\nd = res.max_by { |x| x.dist }.dist + 1\n\ntwo_points_first, three_points_first = first.length, 0\ntwo_points_second, three_points_second = second.length, 0\n\nmax_first = two_points_first * 2 + three_points_first * 3\nmax_second = two_points_second * 2 + three_points_second * 3\ndiff = max_first - max_second\n\npoints = first.map { |x| x.dist } + second.map { |x| x.dist }\npoints = points.uniq.sort.reverse\n\n\ncurrent = 0\nfor i in points\n while current < res.length && res[current].dist >= i do\n if res[current].team == 1\n two_points_first -= 1\n three_points_first += 1\n else\n two_points_second -= 1\n three_points_second += 1\n end\n\n current += 1\n end\n\n local_first = two_points_first * 2 + three_points_first * 3\n local_second = two_points_second * 2 + three_points_second * 3\n\n loc_diff = local_first - local_second\n\n if loc_diff >= diff\n diff = loc_diff\n max_first, max_second = local_first, local_second\n end\n\n break if current == res.length\nend\n\nputs \"#{max_first}:#{max_second}\""}], "negative_code": [{"source_code": "class Info\n attr_accessor :dist\n attr_accessor :team\n\n def initialize (d, t)\n @dist = d\n @team = t\n end\nend\n\ngets\nfirst = gets.split(' ').map do |x|\n Info.new(x.to_i, 1)\nend\n\ngets\nsecond = gets.split(' ').map do |x|\n Info.new(x.to_i, 2)\nend\n\nres = first + second\nres.sort_by! { |x| -x.dist }\n\nd = res.max_by { |x| x.dist }.dist + 1\n\ntwo_points_first, three_points_first = first.length, 0\ntwo_points_second, three_points_second = second.length, 0\n\nmax_first = two_points_first * 2 + three_points_first * 3\nmax_second = two_points_second * 2 + three_points_second * 3\ndiff = max_first - max_second\n\ncurrent = 0\nd.downto(1) do |i|\n while current < res.length && res[current].dist >= i do\n if res[current].team == 1\n two_points_first -= 1\n three_points_first += 1\n else\n two_points_second -= 1\n three_points_second += 1\n end\n\n local_first = two_points_first * 2 + three_points_first * 3\n local_second = two_points_second * 2 + three_points_second * 3\n\n loc_diff = local_first - local_second\n\n if loc_diff >= diff\n diff = loc_diff\n max_first, max_second = local_first, local_second\n end\n\n current += 1\n end\n\n break if current == res.length\nend\n\nputs \"#{max_first}:#{max_second}\"\n"}, {"source_code": "class Info\n attr_accessor :dist\n attr_accessor :team\n\n def initialize (d, t)\n @dist = d\n @team = t\n end\nend\n\ngets\nfirst = gets.split(' ').map do |x|\n Info.new(x.to_i, 1)\nend\n\ngets\nsecond = gets.split(' ').map do |x|\n Info.new(x.to_i, 2)\nend\n\nres = first + second\nres.sort_by! { |x| -x.dist }\n\nd = res.max_by { |x| x.dist }.dist + 1\n\ntwo_points_first, three_points_first = first.length, 0\ntwo_points_second, three_points_second = second.length, 0\n\nmax_first = two_points_first * 2 + three_points_first * 3\nmax_second = two_points_second * 2 + three_points_second * 3\ndiff = max_first - max_second\n\ncurrent = 0\nd.downto(1) do |i|\n while current < res.length && res[current].dist >= i do\n if res[current].team == 1\n two_points_first -= 1\n three_points_first += 1\n else\n two_points_second -= 1\n three_points_second += 1\n end\n\n local_first = two_points_first * 2 + three_points_first * 3\n local_second = two_points_second * 2 + three_points_second * 3\n\n loc_diff = local_first - local_second\n\n if loc_diff > diff\n diff = loc_diff\n max_first, max_second = local_first, local_second\n end\n\n current += 1\n end\n\n break if current == res.length\nend\n\nputs \"#{max_first}:#{max_second}\"\n"}], "src_uid": "a30b5ff6855dcbad142f6bcc282601a0"} {"nl": {"description": "Once upon a time DravDe, an outstanding person famous for his professional achievements (as you must remember, he works in a warehouse storing Ogudar-Olok, a magical but non-alcoholic drink) came home after a hard day. That day he had to drink 9875 boxes of the drink and, having come home, he went to bed at once.DravDe dreamt about managing a successful farm. He dreamt that every day one animal came to him and asked him to let it settle there. However, DravDe, being unimaginably kind, could send the animal away and it went, rejected. There were exactly n days in DravDe’s dream and the animal that came on the i-th day, ate exactly ci tons of food daily starting from day i. But if one day the animal could not get the food it needed, it got really sad. At the very beginning of the dream there were exactly X tons of food on the farm.DravDe woke up terrified...When he retold the dream to you, he couldn’t remember how many animals were on the farm by the end of the n-th day any more, but he did remember that nobody got sad (as it was a happy farm) and that there was the maximum possible amount of the animals. That’s the number he wants you to find out. It should be noticed that the animals arrived in the morning and DravDe only started to feed them in the afternoon, so that if an animal willing to join them is rejected, it can’t eat any farm food. But if the animal does join the farm, it eats daily from that day to the n-th.", "input_spec": "The first input line contains integers n and X (1 ≤ n ≤ 100, 1 ≤ X ≤ 104) — amount of days in DravDe’s dream and the total amount of food (in tons) that was there initially. The second line contains integers ci (1 ≤ ci ≤ 300). Numbers in the second line are divided by a space.", "output_spec": "Output the only number — the maximum possible amount of animals on the farm by the end of the n-th day given that the food was enough for everybody.", "sample_inputs": ["3 4\n1 1 1", "3 6\n1 1 1"], "sample_outputs": ["2", "3"], "notes": "NoteNote to the first example: DravDe leaves the second and the third animal on the farm. The second animal will eat one ton of food on the second day and one ton on the third day. The third animal will eat one ton of food on the third day."}, "positive_code": [{"source_code": "f = open(\"input.txt\", \"r\")\nn,x = f.gets.chomp.split(/ /).map!{|x|x.to_i}\nar = f.gets.chomp.split(/ /).map!{|x|x.to_i}\nf.close\nan = Array.new\nfor i in 0..n-1\n\tan.push(ar[i]*(n-i))\nend\nan.sort!\nsum = 0\nret = 0\nfor i in 0..an.length-1\n\tif an[i] + sum <= x\n\t\tsum += an[i]\n\t\tret += 1\n\telse\n\t\tbreak\n\tend\nend\nfw = open(\"output.txt\", \"w\")\nfw.puts ret\nfw.close"}], "negative_code": [], "src_uid": "6ddf0be1cc8c0ed6b86eb9e901189bf1"} {"nl": {"description": "Omkar has received a message from Anton saying \"Your story for problem A is confusing. Just make a formal statement.\" Because of this, Omkar gives you an array $$$a = [a_1, a_2, \\ldots, a_n]$$$ of $$$n$$$ distinct integers. An array $$$b = [b_1, b_2, \\ldots, b_k]$$$ is called nice if for any two distinct elements $$$b_i, b_j$$$ of $$$b$$$, $$$|b_i-b_j|$$$ appears in $$$b$$$ at least once. In addition, all elements in $$$b$$$ must be distinct. Can you add several (maybe, $$$0$$$) integers to $$$a$$$ to create a nice array $$$b$$$ of size at most $$$300$$$? If $$$a$$$ is already nice, you don't have to add any elements.For example, array $$$[3, 6, 9]$$$ is nice, as $$$|6-3|=|9-6| = 3$$$, which appears in the array, and $$$|9-3| = 6$$$, which appears in the array, while array $$$[4, 2, 0, 6, 9]$$$ is not nice, as $$$|9-4| = 5$$$ is not present in the array.For integers $$$x$$$ and $$$y$$$, $$$|x-y| = x-y$$$ if $$$x > y$$$ and $$$|x-y| = y-x$$$ otherwise.", "input_spec": "Each test contains multiple test cases. The first line contains $$$t$$$ ($$$1 \\leq t \\leq 50$$$), the number of test cases. Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \\leq n \\leq 100$$$) — the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ distinct integers $$$a_1, a_2, \\cdots, a_n$$$ ($$$-100 \\leq a_i \\leq 100$$$) — the elements of the array $$$a$$$.", "output_spec": "For each test case, output one line containing YES if Omkar can create a nice array $$$b$$$ by adding elements to $$$a$$$ and NO otherwise. The case of each letter does not matter, so yEs and nO will also be accepted. If the first line is YES, output a second line containing a single integer $$$k$$$ ($$$n \\leq k \\leq 300$$$). Then output one line containing $$$k$$$ distinct integers $$$b_1, b_2, \\cdots, b_k$$$ ($$$-10^9 \\leq b_i \\leq 10^9$$$), the elements of the nice array $$$b$$$. $$$b_1, b_2, \\cdots, b_k$$$ can be in any order. For each $$$a_i$$$ in $$$a$$$, $$$a_i$$$ must appear at least once in $$$b$$$. It can be proved that if Omkar can create such an array $$$b$$$, then he can also do so in a way that satisfies the above constraints. If multiple solutions exist, you can print any. ", "sample_inputs": ["4\n3\n3 0 9\n2\n3 4\n5\n-7 3 13 -2 8\n4\n4 8 12 6"], "sample_outputs": ["yes\n4\n6 0 3 9\nyEs\n5\n5 3 1 2 4\nNO\nYes\n6\n8 12 6 2 4 10"], "notes": "NoteFor the first case, you can add integers to $$$a$$$ to receive the array $$$b = [6, 0, 3, 9]$$$. Note that $$$|6-3| = |9-6| = |3-0| = 3$$$ and $$$3$$$ is in $$$b$$$, $$$|6-0| = |9-3| = 6$$$ and $$$6$$$ is in $$$b$$$, and $$$|9-0| = 9$$$ is in $$$b$$$, so $$$b$$$ is nice.For the second case, you can add integers to $$$a$$$ to receive the array $$$b = [5, 3, 1, 2, 4]$$$. We have that $$$|2-1| = |3-2| = |4-3| = |5-4| = 1$$$ is in $$$b$$$, $$$|3-1| = |4-2| = |5-3| = 2$$$ is in $$$b$$$, $$$|4-1| = |5-2| = 3$$$ is in $$$b$$$, and $$$|5-1| = 4$$$ is in $$$b$$$, so $$$b$$$ is nice.For the fourth case, you can add integers to $$$a$$$ to receive the array $$$b = [8, 12, 6, 2, 4, 10]$$$. We have that $$$|4-2| = |6-4| = |8-6| = |10-8| = |12-10| = 2$$$ is in $$$b$$$, $$$|6-2| = |8-4| = |10-6| = |12-8| = 4$$$ is in $$$b$$$, $$$|8-2| = |10-4| = |12-6| = 6$$$ is in $$$b$$$, $$$|10-2| = |12-4| = 8$$$ is in $$$b$$$, and $$$|12-2| = 10$$$ is in $$$b$$$, so $$$b$$$ is nice.It can be proven that for all other test cases it is impossible to create a nice array $$$b$$$."}, "positive_code": [{"source_code": "$, = $\\ = \"\\n\"\r\ngets.to_i.times do\r\n gets\r\n a = gets.split.map &:to_i\r\n if a.min < 0\r\n print \"NO\"\r\n else\r\n print \"YES\", a.max + 1, (0..a.max).to_a.join(\" \")\r\n end\r\nend"}, {"source_code": "gets.to_i.times do\r\n gets\r\n a = gets.split.map &:to_i\r\n if a.min < 0\r\n puts \"NO\"\r\n else\r\n puts \"YES\"\r\n puts a.max + 1, (0..a.max).to_a.join(\" \")\r\n end\r\nend"}, {"source_code": "gets.to_i.times do\r\n gets\r\n a = gets.split.map &:to_i\r\n if a.min < 0\r\n puts \"NO\"\r\n else\r\n puts \"YES\", a.max + 1, (0..a.max).to_a.join(\" \")\r\n end\r\nend"}, {"source_code": "t = gets.to_i\nt.times.each do\n n = gets.to_i\n as = gets.split.map(&:to_i)\n if as.min < 0\n puts 'no'\n next\n end\n\n g = as[0]\n as.each {|a| g = a.gcd(g) }\n max = as.max\n min = [as.min, g].min\n cnt = (max - min) / g + 1\n if cnt > 300\n puts 'no'\n else\n puts 'yes'\n puts cnt\n puts (min..max).step(g).map {|i| i }.join(' ')\n end\nend\n"}], "negative_code": [], "src_uid": "5698e6fd934b9f6b847d7e30a3d06f2b"} {"nl": {"description": "You are given a binary array$$$^{\\dagger}$$$ of length $$$n$$$. You are allowed to perform one operation on it at most once. In an operation, you can choose any element and flip it: turn a $$$0$$$ into a $$$1$$$ or vice-versa.What is the maximum number of inversions$$$^{\\ddagger}$$$ the array can have after performing at most one operation?$$$^\\dagger$$$ A binary array is an array that contains only zeroes and ones.$$$^\\ddagger$$$ The number of inversions in an array is the number of pairs of indices $$$i,j$$$ such that $$$i<j$$$ and $$$a_i > a_j$$$.", "input_spec": "The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \\leq n \\leq 2\\cdot10^5$$$) — the length of the array. The following line contains $$$n$$$ space-separated positive integers $$$a_1$$$, $$$a_2$$$,..., $$$a_n$$$ ($$$0 \\leq a_i \\leq 1$$$) — the elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\\cdot10^5$$$.", "output_spec": "For each test case, output a single integer  — the maximum number of inversions the array can have after performing at most one operation.", "sample_inputs": ["5\n\n4\n\n1 0 1 0\n\n6\n\n0 1 0 0 1 0\n\n2\n\n0 0\n\n8\n\n1 0 1 1 0 0 0 1\n\n3\n\n1 1 1"], "sample_outputs": ["3\n7\n1\n13\n2"], "notes": "NoteFor the first test case, the inversions are initially formed by the pairs of indices ($$$1, 2$$$), ($$$1, 4$$$), ($$$3, 4$$$), being a total of $$$3$$$, which already is the maximum possible.For the second test case, the inversions are initially formed by the pairs of indices ($$$2, 3$$$), ($$$2, 4$$$), ($$$2, 6$$$), ($$$5, 6$$$), being a total of four. But, by flipping the first element, the array becomes $$${1, 1, 0, 0, 1, 0}$$$, which has the inversions formed by the pairs of indices ($$$1, 3$$$), ($$$1, 4$$$), ($$$1, 6$$$), ($$$2, 3$$$), ($$$2, 4$$$), ($$$2, 6$$$), ($$$5, 6$$$) which total to $$$7$$$ inversions which is the maximum possible."}, "positive_code": [{"source_code": "main = -> {\n n = gets.to_i\n a = gets.split.map(&:to_i)\n\n return 0 if n == 1\n\n zeros = [0] * (n + 1)\n ones = [0] * (n + 1)\n n.times do |i|\n zeros[i + 1] = zeros[i]\n zeros[i + 1] += 1 if a[i] == 0\n ones[i + 1] = ones[i]\n ones[i + 1] += 1 if a[i] == 1\n end\n\n l_zero = a.index(0)\n l_one = a.index(1)\n r_zero = a.rindex(0)\n r_one = a.rindex(1)\n\n original = 0\n n.times do |i|\n original += zeros[n] - zeros[i] if a[i] == 1\n end\n ans = original\n\n tries = []\n tries << l_zero if l_zero\n tries << l_one if l_one\n tries << r_zero if r_zero\n tries << r_one if r_one\n\n tries.each do |x|\n a[x] ^= 1\n res = original\n if a[x] == 1\n res += zeros[n] - zeros[x + 1]\n res -= ones[x] - ones[0]\n else\n res += ones[x] - ones[0]\n res -= zeros[n] - zeros[x + 1]\n end\n ans = res if ans < res\n a[x] ^= 1\n end\n\n ans\n}\n\nt = gets.to_i\nputs t.times.map{ main.call }.join(\"\\n\")\n"}, {"source_code": "gets.to_i.times{\n n=gets.to_i\n a=gets.split.map(&:to_i)\n b=a.clone\n c=a.clone\n if b.count(1)>0\n b[b.rindex(1)]=0\n end\n if c.count(0)>0\n c[c.index(0)]=1\n end\n counta=0\n countb=0\n countc=0\n ca=0\n cb=0\n cc=0\n n.times{|i|\n if a[i]==1\n ca+=1\n else\n counta+=ca\n end\n if b[i]==1\n cb+=1\n else\n countb+=cb\n end\n if c[i]==1\n cc+=1\n else\n countc+=cc\n end\n }\n print([counta,countb,countc].max,\"\\n\")\n}"}, {"source_code": "gets.to_i.times{\r\n n = gets.to_i\r\n x = gets.split\r\n ones = (x.join('')).count('1')\r\n zeros = n-ones\r\n zeros1 = zeros\r\n initial = 0\r\n (n-1).times{|i|\r\n if x[i]=='1'\r\n initial+=zeros1\r\n else\r\n zeros1-=1\r\n end\r\n }\r\n y = x.reverse\r\n mx = 0\r\n n.times{|i|\r\n if x[i]=='0'\r\n a = -i + x[(i+1)..].count('0')\r\n mx = [mx,a].max\r\n break\r\n end\r\n }\r\n n.times{|i|\r\n if y[i]=='1'\r\n a = -i + y[(i+1)..].count('1')\r\n mx = [mx,a].max\r\n break\r\n end\r\n }\r\n p initial+mx\r\n}"}, {"source_code": "=begin\n\nWelcome to GDB Online.\nGDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,\nC#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.\nCode, Compile, Run and Debug online from anywhere in world.\n\n=end\n\nt=gets.to_i\nfor i in 1..t\n n =gets.to_i\n s=gets\n maks=s.split.map{|x|x.to_i}\n ones=0\n etternull=0\n har=0\n sum=0\n ending =0\n prev=maks[0]\n for j in 0..(n-1)\n if (har==1)\n if maks[j]==1\n ones+=1\n ending =0\n else \n sum+=ones\n etternull+=1\n ending+=1\n end\n else \n if maks[j]==1\n ones+=1\n etternull-=1\n ending=0\n else \n sum+=ones\n har =1\n ending+=1\n end\n end \n end\n puts [sum, sum+etternull, sum+ones-ending-1].max\nend\n"}], "negative_code": [{"source_code": "main = -> {\n n = gets.to_i\n a = gets.split.map(&:to_i)\n\n return 1 if n == 1\n\n l_zero = a.index(0)\n l_one = a.index(1)\n r_zero = a.rindex(0)\n r_one = a.rindex(1)\n\n ans = n_inversions(a)\n\n tries = []\n tries << l_zero if l_zero\n tries << l_one if l_one\n tries << r_zero if r_zero\n tries << r_one if r_one\n\n tries.each do |x|\n a[x] ^= 1\n res = n_inversions(a)\n ans = res if ans < res\n a[x] ^= 1\n end\n\n ans\n}\n\ndef n_inversions(seq)\n n = seq.size\n b = [0] * (n + 1)\n\n res = 0\n n.times do |i|\n s = 0\n j = seq[i] + 1\n while j > 0\n s += b[j]\n j -= (j & -j)\n end\n\n res += i - s\n\n j = seq[i] + 1\n while j < n\n b[j] += 1\n j += (j & -j)\n end\n end\n\n res\nend\n\nt = gets.to_i\nputs t.times.map{ main.call }.join(\"\\n\")\n"}, {"source_code": "main = -> {\n n = gets.to_i\n a = gets.split.map(&:to_i)\n\n return 0 if n == 1\n\n l_zero = a.index(0)\n l_one = a.index(1)\n r_zero = a.rindex(0)\n r_one = a.rindex(1)\n\n ans = n_inversions(a)\n\n tries = []\n tries << l_zero if l_zero\n tries << l_one if l_one\n tries << r_zero if r_zero\n tries << r_one if r_one\n\n tries.each do |x|\n a[x] ^= 1\n res = n_inversions(a)\n ans = res if ans < res\n a[x] ^= 1\n end\n\n ans\n}\n\ndef n_inversions(seq)\n n = seq.size\n b = [0] * (n + 1)\n\n res = 0\n n.times do |i|\n s = 0\n j = seq[i] + 1\n while j > 0\n s += b[j]\n j -= (j & -j)\n end\n\n res += i - s\n\n j = seq[i] + 1\n while j < n\n b[j] += 1\n j += (j & -j)\n end\n end\n\n res\nend\n\nt = gets.to_i\nputs t.times.map{ main.call }.join(\"\\n\")\n"}, {"source_code": "gets.to_i.times{\n n=gets.to_i\n s=gets.split.map(&:to_i)\n if s.count(1)>s.count(0)\n s[s.rindex(1)]=0\n elsif s.count(1) 1 and ans[-1] == ans[-2]\n cur = ans[-1]\n cur += 1\n ans.pop\n ans.pop\n ans << cur\n end\nend\n\nprint \"#{ans.size}\"\nprint \"\\n\"\n\nfor i in ans\n print \"#{i} \"\nend"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nb=[]\nfor x in a\n\tb<1 and b[-1]==b[-2]\n\t\tb.pop\n\t\tb[-1]+=1\n\tend\nend\n\nprint \"#{b.size}\"\nprint \"\\n\"\n\nfor x in b\n print \"#{x} \"\nend"}, {"source_code": "# help from stackoverflow\nn = gets.to_i\narr = gets.split.map(&:to_i)\ni = 1\nfilled = 1\nans = []\ntmp = arr[0]\nans << arr[0]\nwhile i 1 && ans[-1]==ans[-2]\n tmp = ans.pop\n ans.pop\n ans << tmp+1\n filled = filled -1\n end\n else\n ans << arr[i]\n filled= filled+1\n end\n \n i= i+1\nend\n\nputs filled\n\nfor i in (0..filled)\n print ans[i]\n print \" \"\nend\n"}, {"source_code": "n = gets.chomp.to_i\nc = gets.split.map(&:to_i)\ncur1 = 1\ncur = 0\nans = []\nfor i in c\n ans << i\n while ans.size > 1 and ans[-1] == ans[-2]\n cur = ans[-1]\n ans.pop\n ans.pop\n cur += 1\n ans << cur\n end\nend\nprint \"#{ans.size}\\n\"\nfor i in ans\n print \"#{i} \"\nend"}, {"source_code": "n = gets.to_i\narr = gets.split(/[ \\n]/);\n\nst = Array.new(n) { |x| }\nlen = 0\nfor x in arr\n x = x.to_i\n if (len != 0 && st[len - 1] == x) \n st[len - 1] += 1\n else \n st[len] = x\n len += 1\n end\n while (len > 1 && st[len - 2] == st[len - 1]) do\n len -= 1;\n st[len - 1] += 1\n end\nend\nputs len\nfor i in (0...len) do\n print \"#{st[i]} \";\nend"}, {"source_code": "r=->{gets.split.map &:to_i}\nn = r[]\nn = n[0]\nv = r[]\na = Array.new\nn.times {|i|\n x = v[i]\n while a.length != 0 and a[a.length - 1] == x do\n a.pop()\n x = x + 1\n end\n a.push(x)\n}\nputs(a.length)\nfor x in a do\n print(x)\n print(\" \")\nend"}, {"source_code": "\n\nn = gets.to_i\nrs = gets.split.map &:to_i\n\ngot = []\ni = 0\nj = 0\n\nwhile i < n do\n if got.size == 0 then\n got.push(rs[i])\n elsif rs[i] == got[j] then\n got[j] += 1\n while j > 0 && got[j] == got[j-1] do\n j -= 1\n got[j] += 1\n end\n else\n if j == got.size - 1 then\n got.push(rs[i])\n j += 1\n else\n j += 1\n got[j] = rs[i];\n end\n end\n\n i += 1\nend\n\np j+1\nprint got[0..j].join(' ')\n"}], "negative_code": [{"source_code": "\n\nn = gets.to_i\nrs = gets.split.map &:to_i\n\ngot = []\ni = 0\nj = 0;\nwhile i < n do\n if got.size == 0 then\n got.push(rs[i])\n elsif rs[i] == got[j] then\n got[j] += 1\n while j > 0 && got[j] == got[j-1] do\n j -= 1\n got[j] += 1\n end\n else\n got.push(rs[i])\n j += 1\n end\n\n i += 1\nend\n\np j+1\nprint got[0..j].join(' ')\n"}], "src_uid": "8c715616c8fa373c95368cf4795a2e6a"} {"nl": {"description": "You are given a digital clock with $$$n$$$ digits. Each digit shows an integer from $$$0$$$ to $$$9$$$, so the whole clock shows an integer from $$$0$$$ to $$$10^n-1$$$. The clock will show leading zeroes if the number is smaller than $$$10^{n-1}$$$.You want the clock to show $$$0$$$ with as few operations as possible. In an operation, you can do one of the following: decrease the number on the clock by $$$1$$$, or swap two digits (you can choose which digits to swap, and they don't have to be adjacent). Your task is to determine the minimum number of operations needed to make the clock show $$$0$$$.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 10^3$$$). The first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 100$$$) — number of digits on the clock. The second line of each test case contains a string of $$$n$$$ digits $$$s_1, s_2, \\ldots, s_n$$$ ($$$0 \\le s_1, s_2, \\ldots, s_n \\le 9$$$) — the number on the clock. Note: If the number is smaller than $$$10^{n-1}$$$ the clock will show leading zeroes.", "output_spec": "For each test case, print one integer: the minimum number of operations needed to make the clock show $$$0$$$.", "sample_inputs": ["7\n3\n007\n4\n1000\n5\n00000\n3\n103\n4\n2020\n9\n123456789\n30\n001678294039710047203946100020"], "sample_outputs": ["7\n2\n0\n5\n6\n53\n115"], "notes": "NoteIn the first example, it's optimal to just decrease the number $$$7$$$ times.In the second example, we can first swap the first and last position and then decrease the number by $$$1$$$.In the third example, the clock already shows $$$0$$$, so we don't have to perform any operations."}, "positive_code": [{"source_code": "=begin \r\nEntrada: testcases, entero que indica la cantidad de dígitos, \r\n string de los dígitos\r\nSalida: operaciones mínimas para que todos los dígitos sean 0\r\nMovimientos permitidos: restar 1 a la cifra, cambiar el orden de una cifra \r\n(adyacente y no adyacente)\r\n=end\r\n\r\n# Variables\r\ntestcases = 0\r\nnumberDigits = 0\r\ndigits = \"\"\r\n\r\n# Constantes\r\ncondition = true\r\noperationReduce = false\r\noperationChange = true\r\n\r\n# Paso 1: Pedir los testcases\r\ntestcases = Integer(gets.chomp)\r\nmovements = Array.new(testcases)\r\n\r\n#Paso 2: Pedir la cantidad de dígitos \r\nfor i in (0..(testcases-1))\r\n numberDigits = Integer(gets.chomp)\r\n digits = gets.chomp\r\n movements[i] = 0\r\n count = 0\r\n digits.chars.each do |digit|\r\n count = count +1\r\n if Integer(digit)!=0\r\n if count == numberDigits\r\n movements[i] = movements[i] + digit.to_i\r\n else\r\n movements[i] = movements[i] + digit.to_i + 1\r\n end\r\n end\r\n \r\n end\r\nend\r\nmovements.each do |elements|\r\n puts elements\r\nend"}, {"source_code": "# require 'byebug'\n\ngets.to_i.times do\n\n n = gets.to_i\n str = gets.strip\n\n ary = str.split(\"\").map(&:to_i)\n ans = 0\n 0.upto(n-1).each do |i|\n ans += ary[i]\n ans += 1 if ary[i] != 0 && i != n-1\n end\n\n puts ans\n\nend\n"}, {"source_code": "gets.to_i.times do |tt|\r\n n=gets.to_i\r\n s=gets.chomp.chars.map(&:to_i)\r\n puts s.inject(:+)+n-1-s[0,n-1].count(0)\r\nend\r\n"}, {"source_code": "gets.to_i.times do\r\n\r\nn = gets.to_i\r\nd = gets.chomp.chars.map!(&:to_i)\r\n\r\ncnt = 0\r\nd.reverse_each.with_index do |di, i|\r\n next if di == 0\r\n\r\n cnt += di\r\n cnt += 1 if i > 0\r\nend\r\n\r\nputs cnt\r\n\r\nend\r\n"}], "negative_code": [], "src_uid": "6571fdd506a858d7620c2faa0fe46cc1"} {"nl": {"description": "Consider a rooted tree. A rooted tree has one special vertex called the root. All edges are directed from the root. Vertex u is called a child of vertex v and vertex v is called a parent of vertex u if there exists a directed edge from v to u. A vertex is called a leaf if it doesn't have children and has a parent.Let's call a rooted tree a spruce if its every non-leaf vertex has at least 3 leaf children. You are given a rooted tree, check whether it's a spruce.The definition of a rooted tree can be found here.", "input_spec": "The first line contains one integer n — the number of vertices in the tree (3 ≤ n ≤ 1 000). Each of the next n - 1 lines contains one integer pi (1 ≤ i ≤ n - 1) — the index of the parent of the i + 1-th vertex (1 ≤ pi ≤ i). Vertex 1 is the root. It's guaranteed that the root has at least 2 children.", "output_spec": "Print \"Yes\" if the tree is a spruce and \"No\" otherwise.", "sample_inputs": ["4\n1\n1\n1", "7\n1\n1\n1\n2\n2\n2", "8\n1\n1\n1\n1\n3\n3\n3"], "sample_outputs": ["Yes", "No", "Yes"], "notes": "NoteThe first example:The second example:It is not a spruce, because the non-leaf vertex 1 has only 2 leaf children.The third example:"}, "positive_code": [{"source_code": "n=gets.to_i\nt=[]\n1.upto(n-1){|i| p=gets.to_i-1; t[p]||=[]; t[p] << i }\nputs (0...n).all?{|i| !t[i] || t[i].count{|j|!t[j]}>2} ? :Yes : :No"}, {"source_code": "n = gets.to_i\nx = (n-1).times.map{gets.to_i-1}\n\nha = Array.new(n, true)\nha[0] = false\nx.each{|t| ha[t] = false}\n\ncnt = Array.new(n, 0)\nx.each.with_index{|t,i| cnt[t]+=1 if ha[i+1]}\n\nputs (n.times.all?{|i| ha[i]||cnt[i]>2}) ? 'Yes' : 'No'"}], "negative_code": [], "src_uid": "69edc72ec29d4dd56751b281085c3591"} {"nl": {"description": "The winter in Berland lasts n days. For each day we know the forecast for the average air temperature that day. Vasya has a new set of winter tires which allows him to drive safely no more than k days at any average air temperature. After k days of using it (regardless of the temperature of these days) the set of winter tires wears down and cannot be used more. It is not necessary that these k days form a continuous segment of days.Before the first winter day Vasya still uses summer tires. It is possible to drive safely on summer tires any number of days when the average air temperature is non-negative. It is impossible to drive on summer tires at days when the average air temperature is negative. Vasya can change summer tires to winter tires and vice versa at the beginning of any day.Find the minimum number of times Vasya needs to change summer tires to winter tires and vice versa to drive safely during the winter. At the end of the winter the car can be with any set of tires.", "input_spec": "The first line contains two positive integers n and k (1 ≤ n ≤ 2·105, 0 ≤ k ≤ n) — the number of winter days and the number of days winter tires can be used. It is allowed to drive on winter tires at any temperature, but no more than k days in total. The second line contains a sequence of n integers t1, t2, ..., tn ( - 20 ≤ ti ≤ 20) — the average air temperature in the i-th winter day. ", "output_spec": "Print the minimum number of times Vasya has to change summer tires to winter tires and vice versa to drive safely during all winter. If it is impossible, print -1.", "sample_inputs": ["4 3\n-5 20 -3 0", "4 2\n-5 20 -3 0", "10 6\n2 -5 1 3 0 0 -4 -3 1 0"], "sample_outputs": ["2", "4", "3"], "notes": "NoteIn the first example before the first winter day Vasya should change summer tires to winter tires, use it for three days, and then change winter tires to summer tires because he can drive safely with the winter tires for just three days. Thus, the total number of tires' changes equals two. In the second example before the first winter day Vasya should change summer tires to winter tires, and then after the first winter day change winter tires to summer tires. After the second day it is necessary to change summer tires to winter tires again, and after the third day it is necessary to change winter tires to summer tires. Thus, the total number of tires' changes equals four. "}, "positive_code": [{"source_code": "n, k = gets.split.map(&:to_i)\nts = gets.split.map { |i| i.to_i < 0 }\ncount = ts.count { |t| t }\nif count > k\n puts -1\n exit\nend\nprev = nil\ndts = []\nts.each_with_index do |t, i|\n if t && !prev\n prev = i\n elsif t && prev\n dts << i - prev - 1\n prev = i\n end\nend\ndts.sort!\nsum = count\ndif = 0\ndts.each do |dt|\n if sum + dt <= k\n sum += dt\n dif += 1\n else\n break\n end\nend\nans = (count - dif) * 2\nlast = ts.reverse.index { |t| t }\nif last && last <= k - sum\n puts ans - 1\nelse\n puts ans\nend\n"}, {"source_code": "#!/bin/env ruby\n\nclass TireManager\n attr_accessor :weather_intervals, :minus_degree_days_count, :plus_degree_intervals, :reserve\n def getNumberOfCrisscrosses number_of_winter_days, tyre_life_in_days, weather_forecast\n @weather_intervals=weather_forecast.chunk {|degree| degree<0}.map {|x,y| [(x)?(:minus):(:plus),y.size]}\n @minus_degree_days_count = @weather_intervals.reduce(0) {|sum,interval| sum+=(interval[0]==:minus)?(interval[1]):0}\n case\n when @minus_degree_days_count > tyre_life_in_days\n -1\n when @minus_degree_days_count == 0\n 0\n else\n @weather_intervals.shift if @weather_intervals[0][0]==:plus\n @reserve = tyre_life_in_days - @minus_degree_days_count\n @plus_degree_intervals = @weather_intervals[0...-1].select {|interv| interv[0]==:plus}.map {|x| x[1]}.sort\n @plus_degree_intervals = @plus_degree_intervals.drop_while {|x| (x<=@reserve)?(@reserve-=x;true):(false)}\n @plus_degree_intervals.size*2+1+((@weather_intervals[-1][0]==:plus&&@reserve<@weather_intervals[-1][1])?1:0)\n end\n end\nend\n\nif __FILE__ == $0\n number_of_winter_days,tyre_life_in_days = $stdin.readline.split.map(&:to_i)\n weather_forecast = $stdin.readline.rstrip.split.map(&:to_i)\n puts TireManager.new.getNumberOfCrisscrosses(number_of_winter_days, tyre_life_in_days, weather_forecast)\nend"}], "negative_code": [], "src_uid": "18458f6ab66db560a51887c944f26f0f"} {"nl": {"description": "There are some beautiful girls in Arpa’s land as mentioned before.Once Arpa came up with an obvious problem:Given an array and a number x, count the number of pairs of indices i, j (1 ≤ i < j ≤ n) such that , where is bitwise xor operation (see notes for explanation). Immediately, Mehrdad discovered a terrible solution that nobody trusted. Now Arpa needs your help to implement the solution to that problem.", "input_spec": "First line contains two integers n and x (1 ≤ n ≤ 105, 0 ≤ x ≤ 105) — the number of elements in the array and the integer x. Second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 105) — the elements of the array.", "output_spec": "Print a single integer: the answer to the problem.", "sample_inputs": ["2 3\n1 2", "6 1\n5 1 2 3 4 1"], "sample_outputs": ["1", "2"], "notes": "NoteIn the first sample there is only one pair of i = 1 and j = 2. so the answer is 1.In the second sample the only two pairs are i = 3, j = 4 (since ) and i = 1, j = 5 (since ).A bitwise xor takes two bit integers of equal length and performs the logical xor operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. You can read more about bitwise xor operation here: https://en.wikipedia.org/wiki/Bitwise_operation#XOR."}, "positive_code": [{"source_code": "n,x,*a=$<.read.split.map(&:to_i)\nh=Hash.new(r=0)\na.each{|e|h[e]+=1}\nh.each_key{|k|\n\topp=k^x\n\tif opp==k\n\t\tr+=h[k]*(h[k]-1)/2\n\telsif k 0, 50 => 0, 100 => 0 }\nans = \"YES\"\ngets.split(' ',n).map(&:to_i).each do |x|\n h[x] += 1\n if x == 25\n elsif x == 50\n h[25] -= 1\n elsif h[50] > 0\n h[50] -= 1\n h[25] -= 1\n else\n h[25] -= 3\n end\n ans = \"NO\" if h[25] < 0\nend\nputs ans"}, {"source_code": "#!/usr/bin/ruby\nh={1=>0,2=>0}\ngets;gets.split.each{|a|\n\te=a.to_i/25\n\tif e==1\n\t\th[1]+=1\n\telsif e==2\n\t\tif h[1]>=1\n\t\t\th[1]-=1\n\t\t\th[2]+=1\n\t\telse\n\t\t\tputs :NO\n\t\t\texit\n\t\tend\n\telse\n\t\tif h[2]>=1&&h[1]>=1\n\t\t\th[2]-=1\n\t\t\th[1]-=1\n\t\telsif h[1]>=3\n\t\t\th[1]-=3\n\t\telse\n\t\t\tputs :NO\n\t\t\texit\n\t\tend\n\tend\n}\nputs :YES\n"}, {"source_code": "n = STDIN.readline().to_i\nline = STDIN.readline().split().collect(&:to_i)\nb25, b50 = 0, 0\nans = \"YES\"\n\nline.each do |bill|\n case bill\n when 25 then b25 += 1\n when 50 then b25 -= 1; b50 += 1\n when 100\n if b50 > 0\n b50 -= 1; b25 -= 1\n else\n b25 -= 3\n end\n end\n\n if b25 < 0\n ans = \"NO\"\n break\n end\nend\n\nputs ans\n"}, {"source_code": "n, a, can = gets.chomp.to_i, gets.chomp.split.map(&:to_i), true\nnote = Hash.new 0\na.each do |x|\n if x==25 \n note[25]+=1\n elsif x==50 \n if note[25]>0\n note[50]+=1\n note[25]-=1\n else\n can = false\n break\n end\n elsif x==100\n if note[50]>0 && note[25]>0\n note[50]-=1\n note[25]-=1\n elsif note[25]>2\n note[25]-=3\n else \n can = false\n break\n end\n end \nend\nputs (can==true ? \"YES\" : \"NO\")\n"}, {"source_code": "n = gets.to_i\nexit if n < 0\nif n == 0\n\tprint \"YES\"\n\texit\nend\ncnt50 = 0\ncnt25 = 0\nqueue = gets.split(\" \")\nexit if queue.size < n\nqueue = queue[0...n]\nqueue.each {|el|\n\tcase el\n\twhen \"25\" \n\t\tcnt25 += 1\n\twhen \"50\" \n\t\tif cnt25 > 0 \n\t\t\tcnt25 -=1\n\t\t\tcnt50 += 1\n\t\telse\n\t\t\tprint \"NO\"\n\t\t\texit\n\t\tend\n\twhen \"100\"\n\t\tif cnt50 > 0 && cnt25 > 0\n\t\t\tcnt50 -= 1\n\t\t\tcnt25 -= 1\n\t\telsif cnt25 >= 3\n\t\t\tcnt25 -= 3\n\t\telse\n\t\t\tprint \"NO\"\n\t\t\texit\n\t\tend\n\telse\n\t\tprint \"Wrong banknote\"\n\t\texit \n\tend\t\t\t\n}\nprint \"YES\""}, {"source_code": "n = gets.to_i\np = gets.split.map(&:to_i)\n\nflood = \"YES\"\ndwad,pyat,sto = 0,0,0\np.each do |x|\n if x == 100\n sto+=x\n if pyat>=50\n pyat-=50\n dwad-=25\n else\n dwad-=75\n end\n elsif x == 50 \n pyat+=x\n dwad-=25\n else\n dwad+=x\n end\n\n if sto < 0 || dwad < 0 || pyat < 0\n flood = \"NO\"\n break\n end\nend\nputs flood"}, {"source_code": "def run\n n = $stdin.gets.to_i\n b = $stdin.gets.split.map(&:to_i)\n h = [0, 0, 0]\n f = false\n b.each do |i|\n if i == 25\n h[0] += 1\n elsif i == 50\n if h[0] >= 1\n h[0] -= 1; h[1] += 1\n else\n f = true\n end\n else\n if h[1] >= 1 and h[0] >= 1\n h[1] -= 1; h[0] -= 1\n elsif h[0] >= 3\n h[0] -= 3\n else\n f = true\n end\n end\n end\n puts f ? \"NO\" : \"YES\"\nend\n\nrun if $0 == __FILE__\n"}, {"source_code": "def run\n n = $stdin.gets.to_i\n b = $stdin.gets.split.map(&:to_i)\n h = [0, 0, 0]\n b.each do |i|\n if i == 25\n h[0] += 1\n elsif i == 50\n if h[0] >= 1\n h[0] -= 1\n h[1] += 1\n else\n puts \"NO\"\n return\n end\n else\n if h[1] >= 1 and h[0] >= 1\n h[1] -= 1\n h[0] -= 1\n h[2] += 1\n elsif h[0] >= 3\n h[0] -= 3\n h[2] += 1\n else\n puts \"NO\"\n return\n end\n end\n end\n puts \"YES\"\nend\n\nrun if $0 == __FILE__\n"}, {"source_code": "n = gets.to_i\nh = { 25 => 0, 50 => 0, 100 => 0 }\nans = \"YES\"\ngets.split(' ',n).map(&:to_i).each do |x|\n h[x] += 1\n if x == 25\n elsif x == 50\n h[25] -= 1\n elsif h[50] > 0\n h[50] -= 1\n h[25] -= 1\n else\n h[25] -= 3\n end\n ans = \"NO\" if h[25] < 0\nend\nputs ans"}, {"source_code": "n=gets.to_i\nx=gets.split.map{|e| e.to_i}\nc25=0\nc50=0\nans=\"YES\"\nx.each{|e|\n\tcase e\n\twhen 25\n\t\tc25+=1\n\twhen 50\n\t\tif c25>0 then\n\t\t\tc25-=1\n\t\t\tc50+=1\n\t\telse\n\t\t\tans=\"NO\"\n\t\t\tbreak\n\t\tend\n\twhen 100\n\t\tif c50>0 && c25 >0 then\n\t\t\tc50-=1\n\t\t\tc25-=1\n\t\telsif c25>2 then\n\t\t\tc25-=3\n\t\telse\n\t\t\tans=\"NO\"\n\t\t\tbreak\n\t\tend\n\tend\n}\nputs ans\n\n"}, {"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\n\nn = gets.to_i\nrubles = gets.split.map(&:to_i)\nbuckets = {\n 25 => 0,\n 50 => 0,\n 100 => 0\n}\nimpossible = false\n\ncatch :stop do\n rubles.each { |i|\n case i\n when 25\n buckets[25] += 1\n when 50\n buckets[50] += 1\n\n if buckets[25] > 0\n buckets[25] -= 1\n else\n impossible = true\n throw :stop\n end\n else\n buckets[100] += 1\n \n if buckets[50] > 0\n if buckets[25] > 0\n buckets[50] -= 1\n buckets[25] -= 1\n else\n impossible = true\n throw :stop\n end\n else\n if buckets[25] >= 3\n buckets[25] -= 3\n else\n impossible = true\n throw :stop\n end\n end\n end\n }\nend\n\nputs \"YES\" if !impossible\nputs \"NO\" if impossible\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nc25, c50 = 0, 0\nans = a.each do |x|\n case x\n when 25\n c25 += 1\n when 50\n break if c25 < 1\n c25 -= 1\n c50 += 1\n when 100\n if c50 >= 1 && c25 >= 1\n c25 -= 1\n c50 -= 1\n elsif c25 >= 3\n c25 -= 3\n else\n break\n end\n end\nend\nputs ans ? \"YES\" : \"NO\"\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nc = [0, 0, 0]\nans = 0\na.each do |x|\n case x\n when 25\n c[0] += 1\n when 50\n c[0] -= 1\n if c[0] < 0\n ans = nil\n break\n end\n c[1] += 1\n when 100\n if c[1] >= 1 && c[0] >= 1\n c[0] -= 1\n c[1] -= 1\n elsif c[0] >= 3\n c[0] -= 3\n else\n ans = nil\n break\n end\n end\nend\nputs ans ? \"YES\" : \"NO\"\n"}, {"source_code": "n = gets.to_i\na = gets.split.map{|x| x.to_i}\nq, h = 0, 0\n(0..a.size - 1).each do |i|\n case a[i]\n when 25\n q += 1\n when 50\n if q == 0\n puts \"NO\"\n exit 0\n end\n h += 1\n q -= 1\n when 100\n if h > 0 && q > 0\n h -= 1\n q -= 1\n elsif q >= 3\n q -= 3\n else\n puts \"NO\"\n exit 0\n end\n end\nend\nputs \"YES\"\n"}, {"source_code": "TP = 25\nnum = gets.to_i\nbills = gets.split.map{|b| (b.to_i / TP).pred }\n\nx = [0, 0, 0, 0]\nbills.each{|c|\n case c\n when 1\n x[0] -= 1\n when 3\n if x[1].zero?\n x[0] -= 2\n else\n x[1] -= 1\n end\n x[0] -= 1\n end\n break if x.any? {|i| i < 0 }\n x[c] += 1\n}\nputs x.any? {|i| i < 0} ? \"NO\" : \"YES\" "}, {"source_code": "def cinema_line(bills)\n # 25, 50, 100\n cashier_bills = {\n 25 => 0,\n 50 => 0,\n 100 => 0\n }\n\n bills.each do |bill|\n change = (bill-25)\n case change\n when 0\n cashier_bills[25] += 1\n when 25\n return false if cashier_bills[25] == 0\n\n cashier_bills[25] -= 1\n cashier_bills[50] += 1\n when 75\n # 50 25\n if cashier_bills[50] > 0 && cashier_bills[25] > 0\n cashier_bills[50] -= 1\n cashier_bills[25] -= 1\n cashier_bills[100] += 1\n elsif cashier_bills[25] > 2\n cashier_bills[25] -= 3\n cashier_bills[100] += 1\n else\n return false\n end\n end\n end\n\n return true\nend\n\ndef main\n lines = []\n while line = gets\n lines << line\n end\n\n bills = lines[1].split(' ').map(&:to_i)\n puts cinema_line(bills) ? \"YES\" : \"NO\"\nend\n\nmain"}, {"source_code": "#! /usr/bin/env ruby\n\ni = STDIN.gets.to_i\nlist = STDIN.gets.to_s.split(' ')\n\nif list.length != i\n raise ArgumentError.new\nend\n\ndef sell?(list)\n bills = {25 => 0, 50 => 0, 100 => 0}\n while ruble = list.shift\n case ruble.to_i\n when 25\n bills[25] += 1\n when 50\n return false if bills[25] == 0\n\n bills[25] -= 1\n bills[50] += 1\n when 100\n return false if bills[25] == 0\n\n if bills[50] > 0\n bills[50] -= 1\n bills[25] -= 1\n bills[100] += 1\n else\n return false if bills[25] < 3\n\n bills[25] -= 3\n bills[100] += 1\n end\n end\n end\n true\nend\n\nif sell?(list)\n puts 'YES'\nelse\n puts 'NO'\nend\n\n"}], "negative_code": [{"source_code": "puts gets.chomp.split('+').sort.join('+')"}, {"source_code": "#!/usr/bin/ruby\nh={1=>0,2=>0}\ngets;gets.split.each{|a|\n\te=a.to_i/25\n\tif e==1\n\t\th[1]+=1\n\telsif e==2\n\t\tif h[1]>=1\n\t\t\th[1]-=1\n\t\t\th[2]+=1\n\t\telse\n\t\t\tp :NO\n\t\t\texit\n\t\tend\n\telse\n\t\tif h[2]>=1&&h[1]>=1\n\t\t\th[2]-=1\n\t\t\th[1]-=1\n\t\telsif h[1]>=3\n\t\t\th[1]-=3\n\t\telse\n\t\t\tp :NO\n\t\t\texit\n\t\tend\n\tend\n}\np :YES\n"}, {"source_code": "n = STDIN.readline().to_i\nline = STDIN.readline().split().collect(&:to_i)\nb25, b50 = 0, 0\nans = \"YES\"\n\nline.each do |bill|\n case bill\n when 25 then b25 += 1\n when 50 then b25 -= 1\n when 100\n if b50 > 0\n b50 -= 1; b25 -= 1\n else\n b25 -= 3\n end\n end\n\n if b25 < 0\n ans = \"NO\"\n break\n end\nend\n\nputs ans\n"}, {"source_code": "n, a, can = gets.chomp.to_i, gets.chomp.split.map(&:to_i), true\nnote = Hash.new 0\na.each do |x|\n if x==25 \n note[x]+=1\n elsif x==50 \n if note[25]>0\n note[x]+=1\n note[25]-=1\n else\n can = false\n break\n end\n elsif x==100\n if note[50]>0 && note[25]>0\n note[50]-=1\n note[25]-=1\n elsif note[25]>1 \n note[25]-=3\n else \n can = false\n break\n end\n end \nend\nputs (can==true ? \"YES\" : \"NO\")\n"}, {"source_code": "n, a, can = gets.chomp.to_i, gets.chomp.split.map(&:to_i), true\nnote = Hash.new 0\na.each do |x|\n if x==25 \n note[x]+=1\n elsif x==50 \n if note[25]>0\n note[x]+=1\n note[25]-=1\n else\n can = false\n break\n end\n elsif x==100\n if note[50]>1 && note[25]>0\n note[50]-=1\n note[25]-=1\n elsif note[25]>2 \n note[25]-=3\n else \n can = false\n break\n end\n end \nend\nputs (can==true ? \"YES\" : \"NO\")\n"}, {"source_code": "n = gets.to_i\np = gets.split.map(&:to_i)\n\nf = true\ncounter = 0\np.each do |x|\n if x - counter > 25 \n f = false\n break\n end\n counter+=25\nend\n\nputs f ? \"YES\":\"NO\""}, {"source_code": "n = gets.to_i\np = gets.split.map(&:to_i)\n\nf = true\ncounter = 0\np.each do |x|\n if (x - counter).abs > 25 \n f = false\n break\n end\n counter+=25\n \nend\n\nputs f ? \"YES\":\"NO\"\n"}, {"source_code": "n = gets.to_i\np = gets.split.map(&:to_i)\n\nflood = \"YES\"\ndwad,pyat,sto = 0,0,0\np.each do |x|\n if x == 100\n sto+=x\n pyat-=50\n dwad-=25\n elsif x == 50 \n pyat+=x\n dwad-=25\n else\n dwad+=x\n end\n\n if sto < 0 || dwad < 0 || pyat < 0\n flood = \"NO\"\n break\n end\nend\nputs flood"}, {"source_code": "n = gets.to_i\np = gets.split.map(&:to_i)\n\nflood = \"YES\"\ndwad,pyat,sto = 0,0,0\np.each do |x|\n if x == 100\n sto+=x\n pyat-=50\n dwad-=25\n elsif x == 50 \n pyat+=x\n dwad-=25\n else\n dwad+=x\n end\n\n if sto == 0 || dwad == 0 || pyat == 0\n flood = \"NO\"\n break\n end\nend\nputs flood"}, {"source_code": "def run\n n = $stdin.gets.to_i\n b = $stdin.gets.split.map(&:to_i)\n h = [0, 0, 0]\n b.each do |i|\n if i == 25\n h[0] += 1\n elsif i == 50\n if h[0] >= 1\n h[0] -= 1\n else\n puts \"NO\"\n return\n end\n else\n if h[1] >= 1 and h[0] >= 1\n h[1] -= 1\n h[0] -= 1\n elsif h[0] >= 3\n h[0] -= 3\n else\n puts \"NO\"\n return\n end\n end\n end\n puts \"YES\"\nend\n\nrun if $0 == __FILE__\n"}, {"source_code": "gets\narr = gets.split.map &:to_i\nif arr.first != 25\n puts \"NO\"\nelse\n cashier = arr.shift\n arr.each do |x|\n if cashier < x - 25\n puts \"NO\"\n exit\n else\n cashier += x\n end\n end\n puts \"YES\"\nend\n"}, {"source_code": "gets\narr = gets.split.map &:to_i\nif arr.first != 25\n puts \"NO\"\nelse\n cashier = arr.shift\n arr.each do |x|\n if cashier < x - 25\n puts \"NO\"\n exit\n else\n cashier -= (x - 25)\n cashier += 25\n end\n end\n puts \"YES\"\nend\n"}, {"source_code": "gets\narr = gets.split.map &:to_i\nif arr.first != 25\n puts \"NO\"\nelse\n cashier = arr.shift\n arr.each do |x|\n if cashier < x - 25\n puts \"NO\"\n exit\n end\n end\n puts \"YES\"\nend\n"}, {"source_code": "gets\narr = gets.split.map &:to_i\nif arr.first != 25\n puts \"NO\"\nelse\n cashier = arr.shift\n arr.each do |x|\n if cashier < x - 25\n puts \"NO\"\n exit\n else\n cashier += 25\n cashier -= (x - 25)\n end\n end\n puts \"YES\"\nend\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nc = [0, 0, 0]\nans = 0\na.each do |x|\n case x\n when 25\n c[0] += 1\n when 50\n c[0] -= 1\n if c[0] < 0\n ans = nil\n break\n end\n c[1] += 1\n when 100\n c[0] -= 1\n if c[1] >= 1\n c[1] -= 1\n elsif c[0] >= 2\n c[0] -= 2\n else\n ans = nil\n break\n end\n end\nend\nputs ans ? \"YES\" : \"NO\"\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nc = 0\nans = 0\na.each do |x|\n c -= x - 25\n ans = nil if c < 0\n c += x\nend\nputs ans ? \"YES\" : \"NO\"\n"}, {"source_code": "n = gets.to_i\na = gets.split.map{|x| x.to_i}\nq, h = 0, 0\n(0..a.size - 1).each do |i|\n case a[i]\n when 25\n q += 1\n when 50\n if q == 0\n puts \"NO\"\n exit 0\n end\n h += 1\n q -= 1\n when 100\n if q == 0 || h == 0\n puts \"NO\"\n exit 0\n end\n end\nend\nputs \"YES\"\n"}, {"source_code": "TP = 25\nnum = gets.to_i\nbills = gets.split.map{|b| (b.to_i / TP).pred }\n\nx = 0\nbills.each{|c|\n if c.zero?\n x += 1\n else\n x -= c\n break if x < 0\n end\n}\nputs x < 0 ? \"NO\" : \"YES\" "}, {"source_code": "def cinema_line(bills)\n # 25, 50, 100\n cashier_bills = {\n 25 => 0,\n 50 => 0,\n 100 => 0\n }\n\n bills.each do |bill|\n change = (bill-25)\n case change\n when 0\n cashier_bills[25] += 1\n when 25\n return false if cashier_bills[25] == 0\n\n cashier_bills[25] -= 1\n cashier_bills[50] += 1\n when 75\n puts \"HERE, bills cashier: #{cashier_bills}\"\n # 50 25\n if cashier_bills[50] > 0 && cashier_bills[25] > 0\n cashier_bills[50] -= 1\n cashier_bills[25] -= 1\n cashier_bills[100] += 1\n elsif cashier_bills[25] > 2\n cashier_bills[25] -= 3\n cashier_bills[100] += 1\n else\n return false\n end\n end\n end\n\n return true\nend\n\ndef main\n lines = []\n while line = gets\n lines << line\n end\n\n bills = lines[1].split(' ').map(&:to_i)\n puts cinema_line(bills) ? \"YES\" : \"NO\"\nend\n\nmain"}], "src_uid": "63b20ab2993fddf2cc469c4c4e8027df"} {"nl": {"description": "George has recently entered the BSUCP (Berland State University for Cool Programmers). George has a friend Alex who has also entered the university. Now they are moving into a dormitory. George and Alex want to live in the same room. The dormitory has n rooms in total. At the moment the i-th room has pi people living in it and the room can accommodate qi people in total (pi ≤ qi). Your task is to count how many rooms has free place for both George and Alex.", "input_spec": "The first line contains a single integer n (1 ≤ n ≤ 100) — the number of rooms. The i-th of the next n lines contains two integers pi and qi (0 ≤ pi ≤ qi ≤ 100) — the number of people who already live in the i-th room and the room's capacity.", "output_spec": "Print a single integer — the number of rooms where George and Alex can move in.", "sample_inputs": ["3\n1 1\n2 2\n3 3", "3\n1 10\n0 10\n10 10"], "sample_outputs": ["0", "2"], "notes": null}, "positive_code": [{"source_code": "a=gets.chomp.to_i\nans=0\na.times do\nb=gets.chomp.split(\" \")\nif b[1].to_i-b[0].to_i>=2\nans+=1\nend\nend\nputs \"#{ans}\""}, {"source_code": "p$<.count{|s|-1>eval(s.tr\" \",?-)}\n"}, {"source_code": "ans=0\ngets.to_i.times{|i|\n\ta,b=gets.split.map{|e| e.to_i}\n\tif b-a>1 then\n\t\tans=ans+1\n\tend\n}\nputs ans"}, {"source_code": "available = 0\n(1..gets.to_i).each do |tc|\n\tcurr,max = gets.strip.split.map(&:to_i)\n\tavailable+=1 if max-curr>=2\nend\nputs available"}, {"source_code": "p gets.to_i.times.count{eval(gets.sub(' ','-'))<-1}"}, {"source_code": "n = gets.to_i\nk = 0\n\nfor i in 1..n\n a,b = gets.split.map {|x| x.to_i}\n if b-a >= 2\n k+=1\n end\nend\n\nputs k"}, {"source_code": "n = gets.to_i\n\nans = 0\nn.times do\n p, q = gets.split.map(&:to_i)\n if q - p >= 2\n ans += 1\n end\nend\n\nprint(ans)"}, {"source_code": "n=gets.to_i\nres=0\nn.times {\n a,b=gets.split.map(&:to_i)\n res+=1 if b>=a+2\n}\np res\n"}, {"source_code": "s=0;gets.to_i.times {\n t = gets.split.map(&:to_i)\n s += (t[1]-t[0]>1)?(1):(0)\n}\nputs s\n"}, {"source_code": "number_of_room = gets.strip.to_i\naccepted_room = 0\n1.upto(number_of_room) do\n\tcurrent_people, max_people = gets.strip.split.map(&:to_i)\n\tif max_people - current_people >= 2\n\t\taccepted_room += 1\n\tend\nend\nputs accepted_room\n"}, {"source_code": "p gets.to_i.times.count{eval(gets.sub(' ','-'))<-1}"}, {"source_code": "dorm = gets.chomp.to_i\nrooms = []\n\ni = 0\nwhile i < dorm\n\trooms << gets.chomp\n\ti += 1\nend\n\ncount = 0\nrooms.each do |room|\n\troommates = room.split(\" \")\n\tif roommates[1].to_i - roommates[0].to_i >= 2\n\t\tcount += 1\n\tend\nend\n\nputs count"}, {"source_code": "#!/usr/bin/env ruby\n# coding: utf-8\n\ndef read_int\n readline.chomp.split(/\\s+/).map(&:to_i)\nend\n\nn = read_int.first\nc = 0\nary = []\nn.times do\n tmp = read_int\n c += 1 if tmp.first + 2 <= tmp.last\nend\nputs c"}, {"source_code": "n = gets.to_i\ncount = 0\nn.to_i.times do\n p, q = gets.split.map(&:to_i)\n count += 1 if q - p > 1\nend\nputs count"}, {"source_code": "p$<.count{|s|-1>eval(s.tr\" \",?-)}\n"}, {"source_code": "#puts \"Enter the number of rooms\\n\"\nn = gets.to_i\nc = 0;\n#puts \"Noe enter the number of people living and capacity by giving a space\\n\"\nfor i in 0...n do\n a, b = gets.split.map(&:to_i)\n if b-a >= 2\n c += 1\n end\nend\nputs c\n"}, {"source_code": "n, count = gets.chomp.to_i, 0\nn.times do\n s = gets.chomp.split(' ').map(&:to_i)\n count += 1 if s[1] - s[0] >= 2\nend\nputs count"}, {"source_code": "n = gets.to_i\ncount = 0\nn.times {\n a, b = gets.split.map &:to_i\n count += 1 if a + 2 <= b\n}\nputs count\n"}, {"source_code": "p$<.count{|s|-1>eval(s.tr\" \",?-)}"}, {"source_code": "p$<.count{|s|-1>eval(s.tr\" \",?-)}"}, {"source_code": "n = gets.to_i\n\nputs (1..n).count {\n p, q = gets.split.map(&:to_i)\n q - p >= 2\n}"}, {"source_code": "rooms = gets.chomp.to_i\ni = 0\noutput = 0\nwhile i < rooms\n room_value = gets.chomp.split\n room_value = room_value.map{|item| item.to_i}\n if room_value[1] - room_value[0] >= 2 then\n output = output +1\n end\n i = i+1\nend\nprint output\n"}, {"source_code": "p gets.to_i.times.map{gets.split.map(&:to_i)}.map{|a,b| b-a>=2}.select{|x| x==true}.count"}, {"source_code": "n=gets.to_i\nputs Array.new(n) {|_|\n a,b = gets.split.map(&:to_i)\n a+2<=b ? 1 : 0\n}.inject(&:+)"}, {"source_code": "n = gets.chomp.to_i\n\ncont = 0\nfor i in 0...n\n\tinput = gets.chomp.split(\" \")\n\tif input[1].to_i-input[0].to_i>=2\n\t\tcont+=1\n\tend\nend\n\nputs cont\n"}, {"source_code": "total = 0\ngets.to_i.times{\n\troom = gets.split(' ').map(&:to_i)\n\ttotal += 1 if room[1] - room[0] >= 2\n}\nputs total"}, {"source_code": "a=gets.chomp.to_i\nans=0\na.times do\nb=gets.chomp.split(\" \")\nif b[1].to_i-b[0].to_i>=2\nans+=1\nend\nend\nputs \"#{ans}\"\n\n"}, {"source_code": "n = gets.to_i\nk = 0\nn.times do\n a, b = gets.split.map(&:to_i)\n c = b-a\n if c >= 2\n k = k+1\n end\nend\n\nputs k\n"}, {"source_code": "puts gets.to_i.times.map {x = gets.split(\" \");x = x[1].to_i - x[0].to_i}.select {|item| item >= 2}.size"}, {"source_code": "p$<.count{|s|-1>eval(s.tr\" \",?-)}\n"}, {"source_code": "n = gets.to_i\n\nresult = 0\n1.upto(n) do\n a, b = gets.split(' ').map(&:to_i)\n result += 1 if b-a > 1\nend\n\nputs result\n"}, {"source_code": "p$<.count{|s|-1>eval(s.tr\" \",?-)}"}, {"source_code": "t = gets.to_i\nco = 0\nt.times do\n p, q = gets.split.map(&:to_i)\n co += 1 if q - p >= 2\nend\nputs co\n\n"}, {"source_code": "\nn = gets.chomp.to_i\nc = 0\nn.times do\n s = gets.chomp.split\n p = s[0].to_i+1\n q = s[1].to_i\n c += 1 if p < q\nend\nputs c\n"}, {"source_code": "p$<.count{|s|-1>eval(s.tr\" \",?-)}\n"}, {"source_code": "n = gets.to_i\ncount = 0\nfor i in 1..n\n\ta,b = gets.chomp.split(' ').map(&:to_i)\n\tif a + 2 <= b\n\t\tcount = count + 1\n\tend\nend\nputs count\n"}, {"source_code": "\n\nn = gets.to_i\nresult = 0\n\nn.times do\n a, b = gets.split.map(&:to_i)\n if b - a >= 2\n result += 1\n end\n\nend\n\n\nputs result\n\n"}, {"source_code": "c=0\ngets.to_i.times do\n\ta=gets.split\n\tif a[1].to_i-a[0].to_i>1\n\t\tc+=1\n\tend\nend\nputs c"}, {"source_code": "p$<.count{|s|-1>eval(s.tr\" \",?-)}\n"}, {"source_code": "numberRoom = gets.chomp.to_i\ncount = 0\n\n(0...numberRoom).each do |num|\n\tinput = gets.chomp.split(' ').map {|item| item.to_i}\n\tcount += 1 if input[1] - input[0] >= 2\nend\n\nputs count"}, {"source_code": "c = 0\ngets.to_i.times do\n a,b = gets.split.map(&:to_i)\n c+=1 if a+2 <=b\nend\nputs c"}, {"source_code": "n = gets.to_i\narr = []\nc = 0\n\nfor i in (0..n - 1)\n \n gets.split(\" \").each {|a| arr << a.to_i}\n if (arr[1] - arr[0]) >= 2\n c += 1\n end\n \n arr = []\n \nend\n\nputs c"}, {"source_code": "#!/usr/bin/ruby\np gets.to_i.times.count{\n\tx,y=gets.split.map(&:to_i)\n\ty-x>1\n}"}, {"source_code": "p$<.count{|s|-1>eval(s.tr\" \",?-)}"}, {"source_code": "r = 0\ngets.chomp.to_i.times do\n\tp, q = gets.chomp.split(\" \").map(&:to_i)\n\tr += 1 if q - p > 1\nend\nputs r"}, {"source_code": "p$<.count{|s|-1>eval(s.tr\" \",?-)}\n"}, {"source_code": "s = 0\ngets.to_i.times do\n temp = gets.split.map(&:to_i)\n temp[1] - temp[0] >= 2 ? s += 1 : s\nend\nputs s"}, {"source_code": "\np$<.count{|s|-1>eval(s.tr\" \",?-)}"}, {"source_code": "p$<.count{|s|-1>eval(s.tr\" \",?-)}\n"}, {"source_code": "p$<.count{|s|-1>eval(s.tr\" \",?-)}"}, {"source_code": "#!/usr/bin/ruby\n\nn = STDIN.readline.to_i\ncount = 0\nn.times do\n\tp, q = STDIN.readline.split.map {|s| s.to_i }\n\tcount += 1 if q-p >= 2\nend\nputs count\n"}, {"source_code": "c = 0;\ngets.to_i.times do \n\tar = gets.split.map(&:to_i)\n\tc +=1 if ar[1] - ar[0] >=2\nend\nputs c"}, {"source_code": "p$<.count{|s|-1>eval(s.tr\" \",?-)}"}, {"source_code": "require 'pp'\n\nn = STDIN.gets.chomp.to_i\n\nans = 0\n\nn.times do |i|\n p, q = STDIN.gets.chomp.split(/ /).map(&:to_i)\n ans += 1if q - p >= 2\nend\n\nputs ans\n"}, {"source_code": "#!/usr/bin/env ruby\n\ninput = STDIN.read\nd = input.split \"\\n\"\nrooms_n = d.first\nrooms_d = d[1..-1]\n\ns = rooms_d.select do |r|\n p, q = *r.split(' ')\n q.to_i - p.to_i >= 2\nend\n\nprint s.count"}, {"source_code": "#!/usr/bin/env ruby\n\ninput = STDIN.read.split(\"\\n\")\nrooms = input[1..-1]\n\nprint rooms.count { |r|\n r.split(' ').map(&:to_i).reverse.inject(:-) >= 2\n}"}, {"source_code": "\nn = gets.to_i\np = Array.new()\nq = Array.new()\n\ncounter = 0\nfor i in 0..n-1\n p[i],q[i] = gets.split.map(&:to_i)\n counter+=1 if q[i]-p[i]>=2\nend\nputs counter\n"}, {"source_code": "n = $stdin.readline.to_i\nk = 0\nn.times do\n p, q = $stdin.readline.split.map(&:to_i)\n k+=1 if q-p>=2\nend\n\nputs k"}, {"source_code": "ans = 0\nputs ans if gets.to_i.times do\n\tcur, max = gets.split.map(&:to_i)\n\tif cur+2<=max then ans+=1 end\nend\n"}, {"source_code": "rooms = 0\ngets.to_i.times do\n inp = gets.split.map &:to_i\n rooms += 1 if inp[1] - inp[0] >= 2\nend\nputs rooms\n"}, {"source_code": "p$<.count{|s|-1>eval(s.tr\" \",?-)}\n"}, {"source_code": "n = gets.to_i\n\n$res = 0\n\nn.times do\n p , q = gets.split.map(&:to_i)\n p += 2\n\n if p <= q\n $res += 1\n end\nend\n\nputs $res"}, {"source_code": "n = gets.to_i\nans = 0\nn.times do\n p, q = gets.split.map(&:to_i)\n if q - p >= 2\n ans += 1\n end\nend\nputs ans"}, {"source_code": "p$<.count{|s|-1>eval(s.tr\" \",?-)}\n"}], "negative_code": [{"source_code": "puts \"Enter the number of rooms\\n\"\nn = gets.to_i\nc = 0;\n#puts \"Noe enter the number of people living and capacity by giving a space\\n\"\nfor i in 0...n do\n a, b = gets.split.map(&:to_i)\n if b-a >= 2\n c += 1\n end\nend\nputs c\n"}, {"source_code": "n=gets.to_i\nputs [1..n].each.map {|_|\n a,b = gets.split.map(&:to_i)\n a+2<=b ? 1 : 0\n}.inject(&:+)"}, {"source_code": "n=gets.to_i\nputs [1...n].each.map {|_|\n a,b = gets.split.map(&:to_i)\n a+2<=b ? 1 : 0\n}.inject(&:+)"}, {"source_code": "n=gets.to_i\nputs [1..n].map {|_|\n a,b = gets.split.map(&:to_i)\n a+2<=b ? 1 : 0\n}.inject(&:+)"}, {"source_code": "n=gets.to_i\nputs [1...n].map {|_|\n a,b = gets.split.map(&:to_i)\n a+2<=b ? 1 : 0\n}.inject(&:+)"}, {"source_code": "n = gets.chomp.to_i\n\ncont = 0\nfor i in 0...n\n\tinput = gets.chomp.split(\" \")\n\tif input[1].to_i-input[0].to_i>2\n\t\tcont+=1\n\tend\nend\n\nputs cont\n"}, {"source_code": "t = gets.to_i\nco = 0\nt.times do\n p, q = gets.split.map(&:to_i)\n co += 1 if q - p >= 1\nend\nputs co\n"}, {"source_code": "n = gets.chomp.to_i\nc = 0\nn.times do\n s = gets.chomp.split\n p = s[0].to_i\n q = s[1].to_i\n c += 1 if p < q\nend\nputs c"}], "src_uid": "2a6c457012f7ceb589b3dea6b889f7cb"} {"nl": {"description": "Everybody knows that the capital of Berland is connected to Bercouver (the Olympic capital) by a direct road. To improve the road's traffic capacity, there was placed just one traffic sign, limiting the maximum speed. Traffic signs in Berland are a bit peculiar, because they limit the speed only at that point on the road where they are placed. Right after passing the sign it is allowed to drive at any speed.It is known that the car of an average Berland citizen has the acceleration (deceleration) speed of a km/h2, and has maximum speed of v km/h. The road has the length of l km, and the speed sign, limiting the speed to w km/h, is placed d km (1 ≤ d < l) away from the capital of Berland. The car has a zero speed at the beginning of the journey. Find the minimum time that an average Berland citizen will need to get from the capital to Bercouver, if he drives at the optimal speed.The car can enter Bercouver at any speed.", "input_spec": "The first line of the input file contains two integer numbers a and v (1 ≤ a, v ≤ 10000). The second line contains three integer numbers l, d and w (2 ≤ l ≤ 10000; 1 ≤ d < l; 1 ≤ w ≤ 10000).", "output_spec": "Print the answer with at least five digits after the decimal point.", "sample_inputs": ["1 1\n2 1 3", "5 70\n200 170 40"], "sample_outputs": ["2.500000000000", "8.965874696353"], "notes": null}, "positive_code": [{"source_code": "def dist(init_speed, final_speed, accel)\n\treturn (final_speed ** 2 - init_speed ** 2) / (2 * accel)\nend\n\ndef travel_time(init_speed, accel, distance)\n\treturn (-init_speed + Math.sqrt(init_speed ** 2 + 2 * accel * distance)) / accel\nend\n\na, v = gets.split.map(&:to_f)\nl, d, w = gets.split.map(&:to_f)\nif v <= w \n\tdistance = dist(0, v, a)\n\tif distance > l\n\t\ttime = travel_time(0, a, l)\n\telse\n\t\ttime = v / a + (l - distance) / v\n\tend\nelse\n\tdistance = dist(0, w, a)\n\tif distance >= d\n\t\tdistance = dist(0, v, a)\n\t\tif distance > l\n\t\t\ttime = travel_time(0, a, l)\n\t\telse\n\t\t\ttime = v / a + (l - distance) / v\n\t\tend\n\telse\n\t\ttime = w / a\n\t\t\n\t\tdistance_max = dist(w, v, a)\n\t\tv_max = Math.sqrt(w ** 2 + 2 * a * ((d - distance) / 2))\n\t\tif v_max <= v\n\t\t\ttime +=\t2 * travel_time(w, a, (d - distance) / 2)\n\t\telse\n\t\t\ttime += 2 * travel_time(w, a, distance_max)\n\t\t\ttime += (d - distance - 2 * distance_max) / v\n\t\tend\n\t\t\n\t\textra_distance = l - d\n\t\tif distance_max > extra_distance\n\t\t\ttime += travel_time(w, a, extra_distance)\n\t\telse\n\t\t\ttime += travel_time(w, a, distance_max)\n\t\t\ttime += (extra_distance - distance_max) / v\n\t\tend\n\tend\nend\nputs \"%0.6f\" % time"}], "negative_code": [{"source_code": "def dist(init_speed, final_speed, accel)\n\treturn (final_speed ** 2 - init_speed ** 2) / (2 * accel)\nend\n\ndef travel_time(init_speed, accel, distance)\n\treturn (-init_speed + Math.sqrt(init_speed ** 2 + 2 * accel * distance)) / accel\nend\n\na, v = gets.split.map(&:to_f)\nl, d, w = gets.split.map(&:to_f)\nif v <= w \n\tdistance = dist(0, v, a)\n\tif distance > l\n\t\ttime = travel_time(0, a, l)\n\telse\n\t\ttime = v / a + (l - distance) / v\n\tend\nelse\n\tdistance = dist(0, w, a)\n\tif distance >= d\n\t\tdistance = dist(0, v, a)\n\t\tif distance > l\n\t\t\ttime = travel_time(0, a, l)\n\t\telse\n\t\t\ttime = v / a + (l - distance) / v\n\t\tend\n\telse\n\t\ttime = w / a\n\t\ttime +=\t2 * travel_time(w, a, (d - distance) / 2)\n\t\t\n\t\tdistance_max = dist(w, v, a)\n\t\textra_distance = l - d\n\t\tif distance_max > extra_distance\n\t\t\ttime += travel_time(w, a, extra_distance)\n\t\telse\n\t\t\ttime += travel_time(w, a, distance_max)\n\t\t\ttime += (extra_distance - distance_max) / v\n\t\tend\n\tend\nend\nputs \"%0.6f\" % time"}], "src_uid": "de8c909d6ca4f3b2f885fc60be246458"} {"nl": {"description": "Alice likes snow a lot! Unfortunately, this year's winter is already over, and she can't expect to have any more of it. Bob has thus bought her a gift — a large snow maker. He plans to make some amount of snow every day. On day i he will make a pile of snow of volume Vi and put it in her garden.Each day, every pile will shrink a little due to melting. More precisely, when the temperature on a given day is Ti, each pile will reduce its volume by Ti. If this would reduce the volume of a pile to or below zero, it disappears forever. All snow piles are independent of each other. Note that the pile made on day i already loses part of its volume on the same day. In an extreme case, this may mean that there are no piles left at the end of a particular day.You are given the initial pile sizes and the temperature on each day. Determine the total volume of snow melted on each day. ", "input_spec": "The first line contains a single integer N (1 ≤ N ≤ 105) — the number of days. The second line contains N integers V1, V2, ..., VN (0 ≤ Vi ≤ 109), where Vi is the initial size of a snow pile made on the day i. The third line contains N integers T1, T2, ..., TN (0 ≤ Ti ≤ 109), where Ti is the temperature on the day i.", "output_spec": "Output a single line with N integers, where the i-th integer represents the total volume of snow melted on day i.", "sample_inputs": ["3\n10 10 5\n5 7 2", "5\n30 25 20 15 10\n9 10 12 4 13"], "sample_outputs": ["5 12 4", "9 20 35 11 25"], "notes": "NoteIn the first sample, Bob first makes a snow pile of volume 10, which melts to the size of 5 on the same day. On the second day, he makes another pile of size 10. Since it is a bit warmer than the day before, the first pile disappears completely while the second pile shrinks to 3. At the end of the second day, he has only a single pile of size 3. On the third day he makes a smaller pile than usual, but as the temperature dropped too, both piles survive till the end of the day."}, "positive_code": [{"source_code": "d = gets.chomp.to_i\nvol = gets.chomp.split(\" \").map { |e| e.to_i }\ntemp = gets.chomp.split(\" \").map { |e| e.to_i }\nq = []\ni = 0\n\ndef binary_search(arr, val, low, high, temp, countarr, remarr)\n\tmid = low + (high-low)/2\n\t# puts low, high, val if temp.length > 1\n\tif low >= high\n\t\tif arr[low] > val\n\t\t\tcountarr[low] += -1\n\t\t\tremarr[low] += (temp[low] - (arr[low] - val))\n\t\tend\n\t\treturn countarr, remarr\n\tend\n\tif arr[mid] == val\n\t\tcountarr[mid+1] += -1\n\t\treturn countarr, remarr\n\telsif arr[mid] < val\n\t\tif arr[mid+1] >= val\n\t\t\tcountarr[mid+1] += -1\n\t\t\tremarr[mid+1] += (temp[mid+1] - (arr[mid+1] - val))\n\t\t\treturn countarr, remarr\n\t\tend\n\t\tbinary_search(arr, val, mid+1, high, temp, countarr, remarr)\n\telse\n\t\tif arr[mid-1] <= val\n\t\t\tcountarr[mid] += -1\n\t\t\tremarr[mid] += (temp[mid] - (arr[mid] - val))\n\t\t\treturn countarr, remarr\n\t\tend\n\t\tbinary_search(arr, val, low, mid-1, temp, countarr, remarr)\n\tend\nend\n\ncountarr = Array.new(d) { 0 }\nremarr = Array.new(d) { 0 }\n\nprev = 0\narr = temp.map { |e|\n\tprev = prev + e\n\tprev\n}\n# puts arr.join(\" \")\ni = 0\nacc = 0\nwhile i < d\n\tacc = acc + temp[i-1] if i > 0\n\tcountarr, remarr = binary_search(arr, acc + vol[i], i, d-1, temp, countarr, remarr)\n\ti+=1\nend\nout = Array.new(d){0}\n# puts countarr.join(\" \")\n# puts remarr.join(\" \")\ni = 0\nacc = 0\nwhile i < d\n\tacc = acc + countarr[i]\n\tout[i] = (acc+i+1)*temp[i] + remarr[i]\n\ti+=1\nend\nputs out.join(\" \")\n\n\n"}], "negative_code": [], "src_uid": "e3bc82acf70071b0e6d20a5b4fccbfba"} {"nl": {"description": "Olga came to visit the twins Anna and Maria and saw that they have many cookies. The cookies are distributed into bags. As there are many cookies, Olga decided that it's no big deal if she steals a bag. However, she doesn't want the sisters to quarrel because of nothing when they divide the cookies. That's why Olga wants to steal a bag with cookies so that the number of cookies in the remaining bags was even, that is, so that Anna and Maria could evenly divide it into two (even 0 remaining cookies will do, just as any other even number). How many ways there are to steal exactly one cookie bag so that the total number of cookies in the remaining bags was even?", "input_spec": "The first line contains the only integer n (1 ≤ n ≤ 100) — the number of cookie bags Anna and Maria have. The second line contains n integers ai (1 ≤ ai ≤ 100) — the number of cookies in the i-th bag.", "output_spec": "Print in the only line the only number — the sought number of ways. If there are no such ways print 0.", "sample_inputs": ["1\n1", "10\n1 2 2 3 4 4 4 2 2 2", "11\n2 2 2 2 2 2 2 2 2 2 99"], "sample_outputs": ["1", "8", "1"], "notes": "NoteIn the first sample Olga should take the only bag so that the twins ended up with the even number of cookies.In the second sample Olga can take any of five bags with two cookies or any of three bags with four cookies — 5 + 3 = 8 ways in total.In the third sample, no matter which bag with two cookies Olga chooses, the twins are left with 2 * 9 + 99 = 117 cookies. Thus, Olga has only one option: to take the bag with 99 cookies."}, "positive_code": [{"source_code": "gets;a=gets.split.map &:to_i;p a.count{|i|a.inject(i,&:+)&1<1}"}, {"source_code": "n=gets\na=[]\na=gets.split.map{|x| x.to_i}\nb=0\ni=0\na.each{|x| b+=x}\nif b.even? then a.each{|x| if x.even? then i+=1 end} else a.each{|x| if x.odd? then i+=1 end} end\nputs i"}, {"source_code": "include Math\n\nn=gets.to_i\narr=gets.split.map(&:to_i)\n\nans=0\nall=arr.inject(:+)\n\narr.each do |val|\n\tans+=1 if (all-val)%2==0\nend\n\nputs ans\n"}, {"source_code": "#WilliamTheCat\na = gets.to_i\nb = gets.split\nb.collect! {|i| i.to_i}\nc = 0\nif (b.inject{|sum,x| sum + x} % 2) == 0 then\n c = b.count {|i| i%2 == 0}\n if (b.count {|i| i%2 != 0}) % 2 != 0 then\n c = c+b.count {|i| i%2 != 0}\n end\nend\nif ((b.inject{|sum,x| sum + x} % 2) != 0) and ((b.count {|i| i%2 != 0}) % 2 != 0) then\n c = b.count {|x|x%2!=0}\nend\np c\n"}, {"source_code": "n = gets.to_i\n\na = gets.split.map &:to_i\n\nsum = a.inject(0) do |x, y|\n x+y\nend\n\nans = a.count do |x|\n x % 2 == sum % 2\nend\n\nputs ans"}, {"source_code": "n = gets.to_i\nodd = 0\neven = 0\nsum = 0\ndata = gets.split.map(&:to_i)\ndata.each do |v|\n sum += v\n if(v % 2 == 0) then\n even += 1\n else\n odd += 1\n end\nend\nif sum % 2 == 0 then\n p even\n else\n p odd\nend\n"}, {"source_code": "gets;a=gets.split.map &:to_i;p a.count{|i|a.inject(i,&:+)&1<1}\n"}, {"source_code": "ar = []\ncount = 0\nn = gets.to_i\nar = gets.split.map &:to_i\nsum = ar.inject{|a,b| a+b}\nar.each do |a|\n b = sum - a\n if b%2 == 0\n count += 1\n end\nend\nputs count"}, {"source_code": "a=gets.chomp.to_i\nb=gets.chomp.split(\" \")\nsum=0\nodd=0\neven=0\n0.upto(a-1) do |i| \nsum+=b[i].to_i\nif b[i].to_i%2==0\neven+=1\nelse\nodd+=1\nend\nend\nif sum%2==0\nputs \"#{even}\"\nelse\nputs \"#{odd}\"\nend"}, {"source_code": "_, a = gets, gets.split.map(&:to_i)\nr = a.inject(:+)%2\np a.inject(0) { |s, x| s + (x%2 == r ? 1 : 0) }\n"}, {"source_code": "gets;a=gets.split.map(&:to_i)\np a.map{|x| 1-(a.reduce(:+)-x)%2}.reduce(:+)"}, {"source_code": "gets;a=gets.split.map &:to_i;p a.count{|i|a.inject(i,&:+)&1<1}\n"}, {"source_code": "gets\na = gets.split.map(&:to_i)\ns = a.inject(0,&:+)\np a.count{ |x| x%2 == s%2 }"}, {"source_code": "gets;a=gets.split.map &:to_i;p a.count{|i|a.inject(i,&:+)&1<1}\n"}, {"source_code": "gets; bags, c = gets.split.map(&:to_i), 0\n\nclass Array\n def sum\n self.inject{ |sum, e| sum + e }\n end\nend\n\nbags.each.with_index do |bag, i|\n d = bags.dup\n d.delete_at(i)\n\n (c += 1; break) if d.empty?\n (c += 1) if d.sum.even?\nend\n\nprint c\n"}, {"source_code": "gets;a=gets.split.map &:to_i;p a.count{|i|a.inject(i,&:+)&1<1}\n"}, {"source_code": "n = gets.to_i\nar = gets.split(' ').map(&:to_i)\nsum = ar.inject(:+)\nputs ar.select{|x| (sum-x).even?}.count"}, {"source_code": "n = STDIN.gets.chomp.to_i\np = STDIN.gets.chomp.split(' ').map{|i| i.to_i}\ntotal = p.reduce(&:+)\nputs p.inject(0) {|res, i| res += 1 if (total - i).even?; res}"}, {"source_code": "a=STDIN.readlines[1].split.map &:to_i\np a.count{|x|(a.inject(:+)-x).even?}\n"}, {"source_code": "n = gets.to_i\n\ngoods = gets.split.map{|i| i.to_i}\n\nsum = 0\ngoods.map{|i| sum += i}\n\nres = 0\nif sum % 2 == 0 then\n n.times do |i|\n if goods[i] % 2 == 0 then\n res += 1\n end\n end\nelse\n n.times do |i|\n if goods[i] % 2 != 0 then\n res += 1\n end\n end\nend\n\nputs res"}, {"source_code": "gets\na = gets.split.map(&:to_i)\np a.count{|i| a.inject(i, &:+).even?}\n"}, {"source_code": "n = gets.chomp.to_i\nbags = gets.chomp.split(\" \").map {|x| x.to_i}\nodd_num = bags.count{|x| x % 2 == 1}\neven_num = bags.length - odd_num\nif odd_num % 2 == 1\n puts odd_num\nelse\n puts even_num\nend\n"}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\no=e=0\na.each{|i|\n if i % 2==0 then e+=1 else o+=1 end\n}\nif o % 2 == 0 then\n puts e\nelse\n puts o\nend"}, {"source_code": "gets;a=gets.split.map &:to_i;p a.count{|i|a.inject(i,&:+)&1<1}\n"}, {"source_code": "gets;a=gets.split.map &:to_i;p a.count{|i|a.inject(i,&:+)&1<1}\n"}, {"source_code": "gets;a=gets.split.map &:to_i;p a.count{|i|a.inject(i,&:+)&1<1}"}, {"source_code": "_, a = gets.to_i, gets.split.map(&:to_i)\ns = a.inject(0) { |acc, x| acc+x}\nputs a.map {|x| (s-x).even? ? 1 : 0 }.count 1"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\n\nsum = 0\ncounter = 0\na.each do |x|\n sum+=x\n counter+=1 if x%2 == 0\nend\n\nif sum%2 == 0\n puts counter\nelse\n puts n-counter\nend"}, {"source_code": "#http://codeforces.com/problemset/problem/129/A\n\n\nn = gets.to_i \nlist = gets.split.map(&:to_i)\n\nodd_elements = list.select{ |i| i.odd? } \neven_numbers = n - odd_elements.size\n\nif odd_elements.size.odd? \n puts odd_elements.size \nelsif odd_elements.size.even? \n puts even_numbers \nend "}, {"source_code": "gets;a=gets.split.map &:to_i;p a.count{|i|a.inject(i,&:+)&1<1}\n"}, {"source_code": "a=gets.chomp.to_i\nb=gets.chomp.split(\" \")\nsum=0\nodd=0\neven=0\n0.upto(a-1) do |i| \nsum+=b[i].to_i\nif b[i].to_i%2==0\neven+=1\nelse\nodd+=1\nend\nend\nif sum%2==0\nputs \"#{even}\"\nelse\nputs \"#{odd}\"\nend\n\n"}, {"source_code": "n = gets.to_i\na = gets.split.map &:to_i\nsum = 0\nans = 0\nfor i in 0..n-1 do\n sum += a[i]\nend\nfor i in 0..n-1 do\n cnt = sum - a[i]\n if (cnt % 2 == 0) then ans += 1 end\nend\nputs ans\n"}, {"source_code": "gets;a=gets.split.map &:to_i;p a.count{|i|a.inject(i,&:+)&1<1}\n"}, {"source_code": "a=gets.chomp.to_i\nb=gets.chomp.split(\" \")\nsum=0\nodd=0\neven=0\n0.upto(a-1) do |i| \nsum+=b[i].to_i\nif b[i].to_i%2==0\neven+=1\nelse\nodd+=1\nend\nend\nif sum%2==0\nputs \"#{even}\"\nelse\nputs \"#{odd}\"\nend"}, {"source_code": "n=gets.to_i\nxs=gets.split.map{|e| e.to_i}\nsum=0\nxs.each{|e|\n\tsum+=e\n}\nputs xs.count{|e|\n\t(sum-e)%2==0\n}"}, {"source_code": "gets;a=gets.split.map &:to_i;p a.count{|i|a.inject(i,&:+)&1<1}"}, {"source_code": "result = 0\nn = STDIN.gets.to_i\nif n == 1\n p 1\n exit\nend\nlines = STDIN.gets\nlines = lines.strip.split\nlines.collect! {|i| i.to_i}\neven = lines.count {|i| i % 2 == 0}\nodd = lines.count {|i| i % 2 == 1}\nif odd % 2 == 1\n p odd\nelse \n p even\nend"}], "negative_code": [{"source_code": "n = gets.chomp.to_i\nbags = gets.chomp.split(\" \").map {|x| x.to_i}\nodd_num = bags.count{|x| x % 2 == 1}\neven_num = bags.length - odd_num\nputs odd_num\nputs even_num\nif odd_num % 2 == 1\n puts odd_num\nelse\n puts even_num\nend\n"}, {"source_code": "n = gets.chomp.to_i\nbags = gets.chomp.split(\" \").map {|x| x.to_i}\nodd_num = bags.count{|x| x % 2 == 1}\neven_num = bags.length - odd_num\nif odd_num % 2 == 1\n if even_num == 0\n puts odd_num\n else\n puts 1\n end\nelse\n puts even_num\nend\n"}, {"source_code": "n = gets.chomp.to_i\nbags = gets.chomp.split(\" \").map {|x| x.to_i}\nodd_num = bags.count{|x| x % 2 == 1}\neven_num = bags.length - odd_num\nif odd_num % 2 == 1\n puts 1\nelse\n puts even_num\nend\n"}], "src_uid": "4c59b4d43b59c8659bf274f3e29d01fe"} {"nl": {"description": "Vasya plays the Geometry Horse.The game goal is to destroy geometric figures of the game world. A certain number of points is given for destroying each figure depending on the figure type and the current factor value. There are n types of geometric figures. The number of figures of type ki and figure cost ci is known for each figure type. A player gets ci·f points for destroying one figure of type i, where f is the current factor. The factor value can be an integer number from 1 to t + 1, inclusive. At the beginning of the game the factor value is equal to 1. The factor is set to i + 1 after destruction of pi (1 ≤ i ≤ t) figures, so the (pi + 1)-th figure to be destroyed is considered with factor equal to i + 1.Your task is to determine the maximum number of points Vasya can get after he destroys all figures. Take into account that Vasya is so tough that he can destroy figures in any order chosen by him.", "input_spec": "The first line contains the only integer number n (1 ≤ n ≤ 100) — the number of figure types. Each of the following n lines contains two integer numbers ki and ci (1 ≤ ki ≤ 109, 0 ≤ ci ≤ 1000), separated with space — the number of figures of the i-th type and the cost of one i-type figure, correspondingly. The next line contains the only integer number t (1 ≤ t ≤ 100) — the number that describe the factor's changes. The next line contains t integer numbers pi (1 ≤ p1 < p2 < ... < pt ≤ 1012), separated with spaces. Please, do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specificator.", "output_spec": "Print the only number — the maximum number of points Vasya can get.", "sample_inputs": ["1\n5 10\n2\n3 6", "2\n3 8\n5 10\n1\n20"], "sample_outputs": ["70", "74"], "notes": "NoteIn the first example Vasya destroys three figures first and gets 3·1·10 = 30 points. Then the factor will become equal to 2 and after destroying the last two figures Vasya will get 2·2·10 = 40 points. As a result Vasya will get 70 points.In the second example all 8 figures will be destroyed with factor 1, so Vasya will get (3·8 + 5·10)·1 = 74 points."}, "positive_code": [{"source_code": "n = gets.chomp.to_i\na = []\nfor i in 0..n-1\n cn, c = gets.chomp.split(/ /).map!{|x| x.to_i}\n a << [cn, c]\nend\nt = gets.chomp.to_i\nta = gets.chomp.split(/ /).map!{|x| x.to_i}\nf = 1\na.sort!{|a1,a2|\n a1[1] <=> a2[1]\n}\ntix = 0\nret = 0\ndcnt = 0\nfor i in 0..n-1\n nokori = a[i][0]\n while nokori > 0\n if ta[tix] > dcnt\n if ta[tix] >= (dcnt + nokori)\n ret += nokori*a[i][1]*f\n dcnt += nokori\n nokori = 0\n else\n wk = ta[tix] - dcnt\n ret += wk*a[i][1]*f\n dcnt += wk\n nokori -= wk\n end\n else\n ret += nokori*a[i][1]*f\n dcnt += nokori\n nokori = 0\n end\n if ta[tix] == dcnt\n f += 1\n tix += 1 if tix < t-1 \n end\n end\nend\nputs ret"}, {"source_code": "n = gets.to_i\nfigures = []\ncount = 0\nn.times do\n figures <<= gets.split.map(&:to_i)\n count += figures[figures.length-1][0]\nend\nt = gets.to_i\np = gets.split.map(&:to_i)\nwhile p.length>0 && p[p.length-1]>count\n p.pop\nend\nif p.length==0 || p[p.length-1]0\n use=[a[a.length-1][1],_p[_p.length-1]].min\n ans+=a[a.length-1][0]*use*now\n a[a.length-1][1]-=use\n _p[_p.length-1]-=use\n if a[a.length-1][1]==0\n a.pop\n end\n if _p[_p.length-1]==0\n _p.pop\n now+=1\n end\nend\np ans"}], "negative_code": [{"source_code": "n = gets.chomp.to_i\na = []\nfor i in 0..n-1\n cn, c = gets.chomp.split(/ /).map!{|x| x.to_i}\n a << [cn, c]\nend\nt = gets.chomp.to_i\nta = gets.chomp.split(/ /).map!{|x| x.to_i}\nf = 1\na.sort!{|a1,a2|\n a1[1] <=> a2[1]\n}\ntix = 0\nret = 0\ndcnt = 0\nfor i in 0..n-1\n nokori = a[i][0]\n while nokori > 0\n if ta[tix] > dcnt\n if ta[tix] >= (dcnt + nokori)\n ret += nokori*a[i][1]*f\n dcnt += nokori\n nokori = 0\n else\n wk = ta[tix] - dcnt\n ret += wk*a[i][1]*f\n dcnt += wk\n nokori -= wk\n end\n else\n ret += nokori*a[i][1]*f\n dcnt += nokori\n nokori = 0\n end\n if tix < t-1 && ta[tix] == dcnt\n f += 1\n tix += 1\n end\n end\nend\nputs ret"}], "src_uid": "bfd7aabf195321249db8760c3cb6998d"} {"nl": {"description": "Berland year consists of $$$m$$$ months with $$$d$$$ days each. Months are numbered from $$$1$$$ to $$$m$$$. Berland week consists of $$$w$$$ days. The first day of the year is also the first day of the week. Note that the last week of the year might be shorter than $$$w$$$ days.A pair $$$(x, y)$$$ such that $$$x < y$$$ is ambiguous if day $$$x$$$ of month $$$y$$$ is the same day of the week as day $$$y$$$ of month $$$x$$$.Count the number of ambiguous pairs.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of testcases. Each of the next $$$t$$$ lines contains three integers $$$m$$$, $$$d$$$ and $$$w$$$ ($$$1 \\le m, d, w \\le 10^9$$$) — the number of months in a year, the number of days in a month and the number of days in a week.", "output_spec": "Print $$$t$$$ integers — for each testcase output the number of pairs $$$(x, y)$$$ such that $$$x < y$$$ and day $$$x$$$ of month $$$y$$$ is the same day of the week as day $$$y$$$ of month $$$x$$$.", "sample_inputs": ["5\n6 7 4\n10 7 12\n12 30 7\n1 1 1\n3247834 10298779 625324"], "sample_outputs": ["6\n9\n5\n0\n116461800"], "notes": "NoteHere are the pairs for the first test case: "}, "positive_code": [{"source_code": "gets.to_i.times do\n m, d, w = gets.split.map &:to_i\n w1 = w / w.gcd(d - 1)\n mx = [m, d].min\n cnt = mx / w1\n puts (2 * (mx - w1) - w1 * (cnt - 1)) * cnt / 2\nend"}], "negative_code": [], "src_uid": "875851f43c7a6e09cd3d20f2a6980d40"} {"nl": {"description": "Find the number of k-divisible numbers on the segment [a, b]. In other words you need to find the number of such integer values x that a ≤ x ≤ b and x is divisible by k.", "input_spec": "The only line contains three space-separated integers k, a and b (1 ≤ k ≤ 1018; - 1018 ≤ a ≤ b ≤ 1018).", "output_spec": "Print the required number.", "sample_inputs": ["1 1 10", "2 -4 4"], "sample_outputs": ["10", "5"], "notes": null}, "positive_code": [{"source_code": "def solve(k, a, b)\n ans = 0\n if b * a <= 0 then\n ans = b / k + (-a) / k + 1\n else\n m = [a.abs, b.abs].max\n n = [a.abs, b.abs].min\n ans = m / k - (n - 1) / k;\n end\n ans\nend\n\nk, a, b = gets.chomp.split.map(&:to_i)\nputs solve(k, a, b)\n"}, {"source_code": "data = gets.chomp.split\n\nk=data[0].to_i\na=data[1].to_i\nb=data[2].to_i\n\nn=0\nif a*b<0\n\tn=(a).abs/k+b.abs/k+1\nelse\n\tif a%k==0\n\t\tn=b/k-a/k+1\n\telse\n\t\tn=b/k-a/k\n\tend\nend\n\nputs n"}, {"source_code": "#!/usr/bin/env ruby\n\nk, a, b = gets.split(' ').map(&:to_i)\n\ntotal = b.div(k) - (a-1).div(k)\n\nputs total\n"}, {"source_code": "# be name khoda ...\ndef read_ints; STDIN.gets.split.map(&:to_i); end\nk,a,b = read_ints\na1 = a/k\nif(a%k != 0)\n\ta1+=1\nend\na2 = b/k\nputs a2 - a1 + 1"}, {"source_code": "def sol(k,a,b)\n if a < 0 && b > 0\n x = (b.abs/k + a.abs/k).abs + 1\n else\n x = (b.abs/k - a.abs/k).abs\n\n if a.abs < b.abs\n if a%k == 0\n x += 1\n end\n else\n if b%k == 0\n x += 1\n end\n end\n end\n\n x\nend\n\nk,a,b=gets.split(/\\s/).map(&:to_i)\np sol(k,a,b)"}, {"source_code": "k, a, b = gets.split.map &:to_i\nputs b / k - (a - 1) / k"}, {"source_code": "class Solver\n\tdef main\n\t\tk, a, b = gets.split.map { |x| x.to_i }\n\t\tif 0.between? a, b then\n\t\t\tputs b / k + (-a) / k + 1\n\t\telse\n\t\t\ta, b = -b, -a if b < 0\n\t\t\tputs b / k - (a - 1) / k\n\t\tend\n\tend\nend\n\nSolver.new.main\n"}], "negative_code": [{"source_code": "def solve(k, a, b)\n ans = 0\n if b * a <= 0 then\n ans = b / k - a / k + 1\n else\n m = [a.abs, b.abs].max\n n = [a.abs, b.abs].min\n ans = m / k - (n - 1) / k;\n end\n ans\nend\n\nk, a, b = gets.chomp.split.map(&:to_i)\nputs solve(k, a, b)\n"}, {"source_code": "data = gets.chomp.split\n\nk=data[0].to_i\na=data[1].to_i\nb=data[2].to_i\n\nn=(b-a)/k+1\nputs n"}, {"source_code": "# be name khoda ...\ndef read_ints; STDIN.gets.split.map(&:to_i); end\nk,a,b = read_ints\na1 = a/k\na1.to_i\na2 = b/k\na2.to_i\nif(a1 == 0) \n\tputs a2\nelse\n\tputs a2 - a1 + 1\nend"}, {"source_code": "# be name khoda ...\ndef read_ints; STDIN.gets.split.map(&:to_i); end\nk,a,b = read_ints\na1 = a/k\na2 = b/k\nputs a2 - a1 + 1"}, {"source_code": "k, a, b = gets.split.map &:to_i\nc = b - a + 1\nputs c / k if c % k == 0\nputs c / k + (a % k == 0 || b % k == 0? 1 : 0) if c % k != 0 "}], "src_uid": "98de093d78f74273e0ac5c7886fb7a41"} {"nl": {"description": "Mishka wants to buy some food in the nearby shop. Initially, he has $$$s$$$ burles on his card. Mishka can perform the following operation any number of times (possibly, zero): choose some positive integer number $$$1 \\le x \\le s$$$, buy food that costs exactly $$$x$$$ burles and obtain $$$\\lfloor\\frac{x}{10}\\rfloor$$$ burles as a cashback (in other words, Mishka spends $$$x$$$ burles and obtains $$$\\lfloor\\frac{x}{10}\\rfloor$$$ back). The operation $$$\\lfloor\\frac{a}{b}\\rfloor$$$ means $$$a$$$ divided by $$$b$$$ rounded down.It is guaranteed that you can always buy some food that costs $$$x$$$ for any possible value of $$$x$$$.Your task is to say the maximum number of burles Mishka can spend if he buys food optimally.For example, if Mishka has $$$s=19$$$ burles then the maximum number of burles he can spend is $$$21$$$. Firstly, he can spend $$$x=10$$$ burles, obtain $$$1$$$ burle as a cashback. Now he has $$$s=10$$$ burles, so can spend $$$x=10$$$ burles, obtain $$$1$$$ burle as a cashback and spend it too.You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. The next $$$t$$$ lines describe test cases. Each test case is given on a separate line and consists of one integer $$$s$$$ ($$$1 \\le s \\le 10^9$$$) — the number of burles Mishka initially has.", "output_spec": "For each test case print the answer on it — the maximum number of burles Mishka can spend if he buys food optimally.", "sample_inputs": ["6\n1\n10\n19\n9876\n12345\n1000000000"], "sample_outputs": ["1\n11\n21\n10973\n13716\n1111111111"], "notes": null}, "positive_code": [{"source_code": "t = gets.to_i\na = t.times.map{gets.to_i}\nputs a.map{|n|\n\tn + (n - 10) / 9 + 1\n}"}, {"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\n\ndef maximize(r)\n max_r = 0\n digits = 0\n while digits >= 0\n digits = Math.log10(r)\n if digits < 1\n max_r += r\n break\n else\n base = 10 ** (digits.floor)\n lead = r / base\n max_r += lead * base\n r -= lead * base\n r += lead * base / 10\n end\n end\n\n max_r\nend\n\nn = gets.to_i\nn.times {\n puts maximize(gets.to_i)\n}"}, {"source_code": "t=gets.to_i\nfor i in 1..t do\n n=gets.to_i\n s=n\n while n/10>0 do\n s+=n/10\n n=n%10+n/10\n end\n puts(s)\nend"}, {"source_code": "1.upto(gets.to_i) do\n x = gets.to_i\n s = 0\n\n while x >= 10\n s += x.div(10)*10\n x = x.div(10) + x.modulo(10)\n end\n\n puts s + x\nend\n"}], "negative_code": [{"source_code": "for i in 1..gets.to_i do\n f=gets.to_i\n puts f+f/9\nend"}, {"source_code": "t=gets.to_i\nfor i in 1..t do\n n=gets.to_i\n s=n\n while n/10>0 do\n s+=n/10\n n=n%10+n/10\n end\n print(s)\nend"}, {"source_code": "for i in 1..gets.to_i do\n f=gets.to_i\n if f%9==0 then\n puts f+f/10\n else\n puts f+f/9\n end\nend"}], "src_uid": "0beecbd62aa072a2f3aab542eeb56373"} {"nl": {"description": "Consider the following process. You have a binary string (a string where each character is either 0 or 1) $$$w$$$ of length $$$n$$$ and an integer $$$x$$$. You build a new binary string $$$s$$$ consisting of $$$n$$$ characters. The $$$i$$$-th character of $$$s$$$ is chosen as follows: if the character $$$w_{i-x}$$$ exists and is equal to 1, then $$$s_i$$$ is 1 (formally, if $$$i > x$$$ and $$$w_{i-x} = $$$ 1, then $$$s_i = $$$ 1); if the character $$$w_{i+x}$$$ exists and is equal to 1, then $$$s_i$$$ is 1 (formally, if $$$i + x \\le n$$$ and $$$w_{i+x} = $$$ 1, then $$$s_i = $$$ 1); if both of the aforementioned conditions are false, then $$$s_i$$$ is 0. You are given the integer $$$x$$$ and the resulting string $$$s$$$. Reconstruct the original string $$$w$$$.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $$$s$$$ ($$$2 \\le |s| \\le 10^5$$$, each character of $$$s$$$ is either 0 or 1). The second line contains one integer $$$x$$$ ($$$1 \\le x \\le |s| - 1$$$). The total length of all strings $$$s$$$ in the input does not exceed $$$10^5$$$.", "output_spec": "For each test case, print the answer on a separate line as follows: if no string $$$w$$$ can produce the string $$$s$$$ at the end of the process, print $$$-1$$$; otherwise, print the binary string $$$w$$$ consisting of $$$|s|$$$ characters. If there are multiple answers, print any of them. ", "sample_inputs": ["3\n101110\n2\n01\n1\n110\n1"], "sample_outputs": ["111011\n10\n-1"], "notes": null}, "positive_code": [{"source_code": "t = gets.to_i\nans = []\nt.times do\n w = gets.chomp.split(\"\").map(&:to_i)\n n = gets.to_i\n len = w.length\n bit = Array.new(len,-1)\n exist = true\n len.times do |i|\n if w[i] == 1\n if 0 <= i-n && i+n < len\n if bit[i-n] == -1\n bit[i-n] = 1\n elsif bit[i-n] == 0\n bit[i+n] = 1\n end\n elsif i-n < 0 && len <= i+n\n exist = false\n elsif 0 <= i-n\n if bit[i-n] == 0\n exist = false\n else\n bit[i-n] = 1\n end\n elsif i+n < len\n bit[i+n] = 1\n end\n elsif w[i] == 0\n if 0 <= i-n && i+n < len\n if bit[i-n] == 1 || bit[i+n] == 1\n exist = false\n else\n bit[i-n] = 0\n bit[i+n] = 0\n end\n elsif 0 <= i-n\n if bit[i-n] == 1\n exist = false\n else\n bit[i-n] = 0\n end\n elsif i+n < len\n bit[i+n] = 0\n end\n end\n end\n if exist\n ans << bit\n else\n ans << [\"-1\"]\n end\nend\nans.each do |x|\n x.each do |b|\n if b == -1\n print 1\n else\n print b\n end\n end\n puts \"\"\nend"}, {"source_code": "t = gets.to_i\nans = []\nt.times do\n w = gets.chomp.split(\"\").map(&:to_i)\n n = gets.to_i\n len = w.length\n bit = Array.new(len,1)\n len.times do |i|\n if w[i] == 0\n if 0 <= i-n\n bit[i-n] = 0\n end\n if i+n < len\n bit[i+n] = 0\n end\n end\n end\n exist = true\n len.times do |i|\n if w[i] == 1\n if 0 <= i-n && i+n < len\n if bit[i-n] == bit[i+n] && bit[i-n] == 0\n exist = false\n end\n elsif i-n < 0 && len <= i+n\n exist = false\n elsif 0 <= i-n\n if bit[i-n] == 0\n exist = false\n end\n else\n if bit[i+n] == 0\n exist = false\n end\n end\n end\n end\n if exist\n ans << bit\n else\n ans << [-1]\n end\nend\nans.each do |x|\n x.each do |b|\n print b\n end\n puts \"\"\nend"}], "negative_code": [{"source_code": "t = gets.to_i\nans = []\nt.times do\n w = gets.chomp.split(\"\").map(&:to_i)\n n = gets.to_i\n len = w.length\n bit = Array.new(len,0)\n exist = true\n len.times do |i|\n flg = false\n if 0 <= i-n && i+n <= len-1\n if w[i-n] != w[i+n]\n exist = false\n else\n if w[i-n] == 1\n flg = true\n end\n end\n elsif i-n < 0 && len-1 < i+n\n next\n elsif 0 <= i-n\n if w[i-n] == 1\n flg = true\n end\n elsif i+n <= len-1\n if w[i+n] == 1\n flg = true\n end\n end\n if flg\n bit[i] = 1\n end\n end\n str = \"\"\n if exist\n len.times do |i|\n str = str + bit[i].to_s\n end\n else\n str = \"-1\"\n end\n ans << str\nend\nans.each do |x|\n puts x\nend"}, {"source_code": "t = gets.to_i\nans = []\nt.times do\n w = gets.chomp.split(\"\").map(&:to_i)\n n = gets.to_i\n len = w.length\n bit = Array.new(len,-1)\n exist = true\n len.times do |i|\n if 0 <= i-n\n if bit[i-n] == -1\n bit[i-n] = w[i]\n elsif bit[i-n] != w[i]\n exist = false\n end\n end\n if i+n < len\n if bit[i+n] == -1\n bit[i+n] = w[i]\n elsif bit[i+n] != w[i]\n exist = false\n end\n end\n end\n str = \"\"\n if exist\n len.times do |i|\n if bit[i] == -1\n str = str + \"1\"\n else\n str = str + bit[i].to_s\n end\n end\n else\n str = \"-1\"\n end\n ans << str\nend\nans.each do |x|\n puts x\nend"}, {"source_code": "t = gets.to_i\nans = []\nt.times do\n w = gets.chomp.split(\"\").map(&:to_i)\n n = gets.to_i\n len = w.length\n bit = Array.new(len,0)\n exist = true\n len.times do |i|\n flg = false\n if 0 <= i-n && i+n <= len-1\n if w[i-n] != w[i+n]\n exist = false\n else\n if w[i-n] == 1\n flg = true\n end\n end\n elsif 0 <= i-n\n if w[i-n] == 1\n flg = true\n end\n elsif i+n <= len-1\n if w[i+n] == 1\n flg = true\n end\n elsif i-n < 0 && len-1 < i+n\n exist = false\n end\n if flg\n bit[i] = 1\n end\n end\n str = \"\"\n if exist\n len.times do |i|\n str = str + bit[i].to_s\n end\n else\n str = \"-1\"\n end\n ans << str\nend\nans.each do |x|\n puts x\nend"}, {"source_code": "t = gets.to_i\nans = []\nt.times do\n w = gets.chomp.split(\"\").map(&:to_i)\n n = gets.to_i\n len = w.length\n bit = Array.new(len,0)\n exist = true\n len.times do |i|\n flg = false\n if 0 <= i-n && i+n <= len-1\n if w[i-n] != w[i+n]\n exist = false\n else\n if w[i-n] == 1\n flg = true\n end\n end\n elsif 0 <= i-n\n if w[i-n] == 1\n flg = true\n end\n elsif i+n <= len-1\n if w[i+n] == 1\n flg = true\n end\n end\n if flg\n bit[i] = 1\n end\n end\n str = \"\"\n if exist\n len.times do |i|\n str = str + bit[i].to_s\n end\n else\n str = \"-1\"\n end\n ans << str\nend\nans.each do |x|\n puts x\nend\n"}, {"source_code": "t = gets.to_i\nans = []\nt.times do\n w = gets.chomp.split(\"\").map(&:to_i)\n n = gets.to_i\n len = w.length\n bit = Array.new(len,-1)\n exist = true\n len.times do |i|\n if w[i] == 1\n if 0 <= i-n && i+n < len\n if bit[i-n] == -1\n bit[i-n] = 1\n elsif bit[i-n] == 0\n bit[i+n] = 1\n end\n elsif 0 <= i-n\n if bit[i-n] == 0\n exist = false\n else\n bit[i-n] = 1\n end\n elsif i+n < len\n bit[i+n] = 1\n end\n elsif w[i] == 0\n if 0 <= i-n && i+n < len\n if bit[i-n] == 1 || bit[i+n] == 1\n exist = false\n else\n bit[i-n] = 0\n bit[i+n] = 0\n end\n elsif 0 <= i-n\n if bit[i-n] == 1\n exist = false\n else\n bit[i-n] = 0\n end\n elsif i+n < len\n bit[i+n] = 0\n end\n end\n end\n str = \"\"\n if exist\n len.times do |i|\n if bit[i] == -1\n str = str + \"1\"\n else\n str = str + bit[i].to_s\n end\n end\n else\n str = \"-1\"\n end\n ans << str\n if n != 2 && n != 1 && n != 31 && n != 17 && n != 39 && n != 62 && str != \"-1\"\n ans << n\n s = \"\"\n len.times do |i|\n s = s + w[i].to_s\n end\n ans << s\n end\nend\nans.each do |x|\n puts x\nend"}, {"source_code": "t = gets.to_i\nans = []\nt.times do\n w = gets.chomp.split(\"\").map(&:to_i)\n n = gets.to_i\n len = w.length\n bit = Array.new(len,0)\n exist = true\n len.times do |i|\n flg = false\n if 0 <= i-n && i+n <= len-1\n if w[i-n] != w[i+n]\n exist = false\n else\n w[i-n] == 1\n flg = true\n end\n elsif 0 <= i-n\n if w[i-n] == 1\n flg = true\n end\n elsif i+n <= len-1\n if w[i+n] == 1\n flg = true\n end\n end\n if flg\n bit[i] = 1\n end\n end\n str = \"\"\n if exist\n len.times do |i|\n str = str + bit[i].to_s\n end\n else\n str = \"-1\"\n end\n ans << str\nend\nans.each do |x|\n puts x\nend\n"}, {"source_code": "t = gets.to_i\nans = []\nt.times do\n w = gets.chomp.split(\"\").map(&:to_i)\n n = gets.to_i\n len = w.length\n bit = Array.new(len,0)\n exist = true\n len.times do |i|\n flg = false\n flg2 = false\n if 0 <= i-n\n if w[i-n] == 1\n flg = true\n end\n end\n if i+n <= len-1\n if w[i+n] == 1\n flg2 = true\n elsif flg\n exist = false\n end\n end\n if 0 <= i-n\n if w[i-n] == 1\n flg = true\n elsif flg2\n exist = false\n end\n end\n if flg || flg2\n bit[i] = 1\n end\n end\n str = \"\"\n if exist\n len.times do |i|\n str = str + bit[i].to_s\n end\n else\n str = \"-1\"\n end\n ans << str\nend\nans.each do |x|\n puts x\nend"}, {"source_code": "t = gets.to_i\nans = []\nt.times do\n w = gets.chomp.split(\"\").map(&:to_i)\n n = gets.to_i\n len = w.length\n bit = Array.new(len,-1)\n exist = true\n len.times do |i|\n if w[i] == 1\n if 0 <= i-n && i+n < len\n if bit[i-n] == -1\n bit[i-n] = 1\n elsif bit[i-n] == 0\n bit[i+n] = 1\n end\n elsif 0 <= i-n\n if bit[i-n] == 0\n exist = false\n else\n bit[i-n] = 1\n end\n elsif i+n < len\n bit[i+n] = 1\n end\n elsif w[i] == 0\n if 0 <= i-n && i+n < len\n if bit[i-n] == 1 || bit[i+n] == 1\n exist = false\n else\n bit[i-n] = 0\n bit[i+n] = 0\n end\n elsif 0 <= i-n\n if bit[i-n] == 1\n exist = false\n else\n bit[i-n] = 0\n end\n elsif i+n < len\n bit[i+n] = 0\n end\n end\n end\n str = \"\"\n if exist\n len.times do |i|\n if bit[i] == -1\n str = str + \"1\"\n else\n str = str + bit[i].to_s\n end\n end\n else\n str = \"-1\"\n end\n ans << str\n if n != 2 && n != 1 && n != 31 && n != 17 && n != 39 && n != 62 && str != \"-1\"\n ans << n\n ans << w\n end\nend\nans.each do |x|\n puts x\nend"}, {"source_code": "t = gets.to_i\nans = []\nt.times do\n w = gets.chomp.split(\"\").map(&:to_i)\n n = gets.to_i\n len = w.length\n bit = Array.new(len,-1)\n exist = true\n len.times do |i|\n if w[i] == 1\n if 0 <= i-n && i+n < len\n if bit[i-n] == -1\n bit[i-n] = 1\n elsif bit[i-n] == 0\n bit[i+n] = 1\n end\n elsif 0 <= i-n\n if bit[i-n] == 0\n exist = false\n else\n bit[i-n] = 1\n end\n elsif i+n < len\n bit[i+n] = 1\n end\n elsif w[i] == 0\n if 0 <= i-n && i+n < len\n if bit[i-n] == 1 || bit[i+n] == 1\n exist = false\n else\n bit[i-n] = 0\n bit[i+n] = 0\n end\n elsif 0 <= i-n\n if bit[i-n] == 1\n exist = false\n else\n bit[i-n] = 0\n end\n elsif i+n < len\n bit[i+n] = 0\n end\n end\n end\n str = \"\"\n if exist\n len.times do |i|\n if bit[i] == -1\n str = str + \"1\"\n else\n str = str + bit[i].to_s\n end\n end\n else\n str = \"-1\"\n end\n ans << str\nend\nans.each do |x|\n puts x\nend"}, {"source_code": "t = gets.to_i\nans = []\nt.times do\n w = gets.chomp.split(\"\").map(&:to_i)\n n = gets.to_i\n len = w.length\n bit = Array.new(len,0)\n exist = true\n len.times do |i|\n flg = false\n if 0 <= i-n && i+n <= len-1\n if w[i-n] != w[i+n]\n exist = false\n else\n if w[i-n] == 1\n flg = true\n end\n end\n elsif i-n < 0 && len-1 < i+n\n exist = false\n elsif 0 <= i-n\n if w[i-n] == 1\n flg = true\n end\n elsif i+n <= len-1\n if w[i+n] == 1\n flg = true\n end\n end\n if flg\n bit[i] = 1\n end\n end\n str = \"\"\n if exist\n len.times do |i|\n str = str + bit[i].to_s\n end\n else\n str = \"-1\"\n end\n ans << str\nend\nans.each do |x|\n puts x\nend"}], "src_uid": "02854d98266e5b74bf105ba30ea332df"} {"nl": {"description": "Little Chris knows there's no fun in playing dominoes, he thinks it's too random and doesn't require skill. Instead, he decided to play with the dominoes and make a \"domino show\".Chris arranges n dominoes in a line, placing each piece vertically upright. In the beginning, he simultaneously pushes some of the dominoes either to the left or to the right. However, somewhere between every two dominoes pushed in the same direction there is at least one domino pushed in the opposite direction.After each second, each domino that is falling to the left pushes the adjacent domino on the left. Similarly, the dominoes falling to the right push their adjacent dominoes standing on the right. When a vertical domino has dominoes falling on it from both sides, it stays still due to the balance of the forces. The figure shows one possible example of the process. Given the initial directions Chris has pushed the dominoes, find the number of the dominoes left standing vertically at the end of the process!", "input_spec": "The first line contains a single integer n (1 ≤ n ≤ 3000), the number of the dominoes in the line. The next line contains a character string s of length n. The i-th character of the string si is equal to \"L\", if the i-th domino has been pushed to the left; \"R\", if the i-th domino has been pushed to the right; \".\", if the i-th domino has not been pushed. It is guaranteed that if si = sj = \"L\" and i < j, then there exists such k that i < k < j and sk = \"R\"; if si = sj = \"R\" and i < j, then there exists such k that i < k < j and sk = \"L\".", "output_spec": "Output a single integer, the number of the dominoes that remain vertical at the end of the process.", "sample_inputs": ["14\n.L.R...LR..L..", "5\nR....", "1\n."], "sample_outputs": ["4", "0", "1"], "notes": "NoteThe first example case is shown on the figure. The four pieces that remain standing vertically are highlighted with orange.In the second example case, all pieces fall down since the first piece topples all the other pieces.In the last example case, a single piece has not been pushed in either direction."}, "positive_code": [{"source_code": "#!/usr/bin/ruby\ngets\ns=gets.chomp\nloop{\n\tt=s.dup\n\tt.size.times{|i|\n\t\tnext if t[i,1]!='.'\n\t\tr=0 currR)\n\t\t\t\tnum = currL - currR - 1\n\t\t\t\trez = rez + 1 if num.odd?\n\t\t\t\ts = s.drop(currL)\n\t\t\t\tcurrL = 0\n\t\t\t\tcurrR = s.find_index(\"R\")\n\t\t\telse\n\t\t\t\trez = rez + currR - currL - 1\n\t\t\t\ts = s.drop(currR)\n\t\t\t\tcurrR = 0\n\t\t\t\tcurrL = s.find_index(\"L\")\n\t\t\tend\n\t\telse\n\t\t\tif currR\n\t\t\t\tbreak\n\t\t\telse\n\t\t\t\trez = rez + s.length - currL - 1\n\t\t\t\tbreak\n\t\t\tend\n\t\tend\n\tend\nend\n\nputs rez"}, {"source_code": "gets\na=gets.chomp\nwhile true\n\ti1=a.index 'L'\n\ti2=a.index 'R'\n\tif (i1 && !i2 ) || ( i1 && i2 && i1i1 )\n\t\tif i2==0\n\t\t\ta=\"\"\n\t\t\tbreak\n\t\tend\n\t\ta=a[0..i2-1]\n\telse\n\t\tbreak\n\tend\nend\nans = 0\nwhile a[0]=='.'\n\tans+=1\n\ta=a[1..-1]\nend\nwhile a[-1]=='.'\n\tans+=1\n\ta=a[0..-2]\nend\n(1..a.size-2).each{ |i|\n\tnext if a[i]!='.'\n\tl=i-1\n\tr=i+1\n\twhile a[l]=='.'\n\t\tl-=1\n\tend\n\twhile a[r]=='.'\n\t\tr+=1\n\tend\n\tans += 1 if (a[l]=='R' && a[r] == 'L' && (l-r)%2==0 && i-l==r-i)||(a[l]=='L'&&a[r]=='R')\n}\np ans\n"}, {"source_code": "#!/usr/bin/ruby\ngets\ns=gets.chomp\nloop{\n t=s.dup\n t.size.times{|i|\n next if t[i,1]!='.'\n r=0i1 )\n\t\tif i2==0\n\t\t\ta=\"\"\n\t\t\tbreak\n\t\tend\n\t\ta=a[0..i2-1]\n\telse\n\t\tbreak\n\tend\nend\nans = 0\nwhile a[0]=='.'\n\tans+=1\n\ta=a[1..-1]\nend\nwhile a[-1]=='.'\n\tans+=1\n\ta=a[0..-2]\nend\n(1..a.size-2).each{ |i|\n\tnext if a[i]!='.'\n\tl=i-1\n\tr=i+1\n\twhile a[l]=='.'\n\t\tl-=1\n\tend\n\twhile a[r]=='.'\n\t\tr+=1\n\tend\n\tans += 1 if a[l]=='R' && a[r] == 'L' && (l-r)%2==0 && i-l==r-i\n}\np ans\n"}, {"source_code": "n = gets.to_i\ns = gets.chomp\nli = Array.new\nfor i in 0...n\n if s[i] == 'L' or s[i] == 'R'\n li.push(i)\n end\nend\nif li.empty?\n puts n\n exit\nend\nans = 0\nif s[li[0]] == 'R'\n ans += li[0]\nend\nif s[li[-1]] == 'L'\n ans += n-1-li[-1]\nend\nfor i in 1...li.length\n if (li[i]-li[i-1]).even?\n ans += 1\n end\nend\nputs ans\n"}], "src_uid": "54c748dd983b6a0ea1af1153d08f1c01"} {"nl": {"description": "You are the gym teacher in the school.There are $$$n$$$ students in the row. And there are two rivalling students among them. The first one is in position $$$a$$$, the second in position $$$b$$$. Positions are numbered from $$$1$$$ to $$$n$$$ from left to right.Since they are rivals, you want to maximize the distance between them. If students are in positions $$$p$$$ and $$$s$$$ respectively, then distance between them is $$$|p - s|$$$. You can do the following operation at most $$$x$$$ times: choose two adjacent (neighbouring) students and swap them.Calculate the maximum distance between two rivalling students after at most $$$x$$$ swaps.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. The only line of each test case contains four integers $$$n$$$, $$$x$$$, $$$a$$$ and $$$b$$$ ($$$2 \\le n \\le 100$$$, $$$0 \\le x \\le 100$$$, $$$1 \\le a, b \\le n$$$, $$$a \\neq b$$$) — the number of students in the row, the number of swaps which you can do, and positions of first and second rivaling students respectively.", "output_spec": "For each test case print one integer — the maximum distance between two rivaling students which you can obtain.", "sample_inputs": ["3\n5 1 3 2\n100 33 100 1\n6 0 2 3"], "sample_outputs": ["2\n99\n1"], "notes": "NoteIn the first test case you can swap students in positions $$$3$$$ and $$$4$$$. And then the distance between the rivals is equal to $$$|4 - 2| = 2$$$.In the second test case you don't have to swap students. In the third test case you can't swap students."}, "positive_code": [{"source_code": "#!/usr/bin/env ruby\ncount = gets.chomp.to_i\nanswers = []\ncount.times do\n input = gets.chomp.split(' ').map(&:to_i)\n n = input[0]\n x = input[1]\n a = input[2]\n b = input[3]\n\n distance_after_swaps = (a-b).abs + x\n \n answers << if distance_after_swaps > n - 1\n n-1\n else\n distance_after_swaps\n end\nend\n\nanswers.each { |answer| puts answer }\n"}, {"source_code": "t = gets.to_i\nt.times do \n n,x,a,b = gets.split.map(&:to_i)\n p [(a-b).abs + x, n-1].min\nend"}, {"source_code": "gets.to_i.times do\n n, x, a, b = gets.split.map(&:to_i)\n puts [(n - 1), (a-b).abs + x].min\nend\n"}, {"source_code": "g = gets.chomp.to_i\ng.times{\nn,x,a,b = gets.split(\" \").map{|x|x.to_i}\nqw = [a,b]\na = qw.min\nb = qw.max\n\nx.times{\nif a>1 \na-=1\nelsif b= j\n\t\t\tdv = ar[i] / j\n\t\t\tsum += dv * j\n\t\tend\n\tend\n\tmaxsum = sum if maxsum < sum\nend\nputs maxsum"}, {"source_code": "n,l=gets.split.map(&:to_i)\nres=[]\na=gets.split.map(&:to_i)\nl.upto(100){|k|\n ans=0\n a.each{|b|\n ans+=b/k\n }\n res.push(ans*k)\n}\np res.max\n"}, {"source_code": "l,a=[1,2].map{gets.split.map &:to_i};p (l[1]..100).map{|h|a.map{|x|x/h*h}.inject:+}.max\n"}, {"source_code": "l,a=[1,2].map{gets.split.map &:to_i};p (l[1]..100).map{|h|a.map{|x|x/h*h}.inject:+}.max\n"}, {"source_code": "l,a=[1,2].map{gets.split.map &:to_i};p (l[1]..100).map{|h|a.map{|x|x/h*h}.inject:+}.max"}, {"source_code": "l,a=[1,2].map{gets.split.map &:to_i};p (l[1]..100).map{|h|a.inject{|s,x|(s/h+x/h)*h}}.max"}, {"source_code": "l,a=[1,2].map{gets.split.map &:to_i};p (l[1]..100).map{|h|a.map{|x|x/h*h}.inject:+}.max\n"}, {"source_code": "l,a=[1,2].map{gets.split.map &:to_i};p (l[1]..100).map{|h|a.map{|x|x/h*h}.inject:+}.max\n"}, {"source_code": "a,b=gets.split.map(&:to_i)\nx=gets.split.map(&:to_i)\np (b..100).map{|l|x.inject(0){|s,n|s+n/l}*l}.max"}, {"source_code": "l,a=[1,2].map{gets.split.map &:to_i};p (l[1]..100).map{|h|a.map{|x|x/h*h}.inject:+}.max"}, {"source_code": "def get_answer(k)\n s = 0\n $a.each{|x| s += x / k}\n s\nend\n\ndef max(a, b)\n if a >= b\n a\n else\n b\n end\nend\n\nn, l = gets.split.map(&:to_i)\n$a = gets.split.map(&:to_i).to_a\n\nm = 0\n$a.each{|x| m = max(m, x)}\n\nans = 0\n(l..m).each{|i| ans = max(ans, get_answer(i) * i)}\n\nputs ans\n"}, {"source_code": "l,a=[1,2].map{gets.split.map &:to_i};p (l[1]..100).map{|h|a.map{|x|x/h*h}.inject:+}.max\n"}, {"source_code": "l,a=[1,2].map{gets.split.map &:to_i};p (l[1]..100).map{|h|a.map{|x|x/h*h}.inject:+}.max\n"}, {"source_code": "n, l = gets.split(\" \").map(&:to_i)\narr = gets.split(\" \").map(&:to_i)\nres = 0\n\nl.upto(100) do |x|\n count = arr.map { |a| a / x }.reduce(:+)\n res = [res, count * x].max\nend\n\nputs res"}, {"source_code": "l,a=[1,2].map{gets.split.map &:to_i};p (l[1]..100).map{|h|a.map{|x|x/h*h}.inject:+}.max\n"}, {"source_code": "def max(a, b)\n a > b ? a : b\nend\n\nn, l = gets.split.map { |v| v.to_i }\nlenghts = gets.split.map { |v| v.to_i }\n\nsurface = 0\n\n(l .. lenghts.max).each do |d|\n k = lenghts.inject(0) { |r, lenght| r + lenght / d }\n surface = max(surface, k * d)\nend\n\nputs surface\n"}, {"source_code": "l,a=[1,2].map{gets.split.map &:to_i};p (l[1]..100).map{|h|a.map{|x|x/h*h}.inject:+}.max\n"}], "negative_code": [{"source_code": "n,l=gets.split.map(&:to_i)\nans=0\na=gets.split.map(&:to_i)\na.each{|b|\n ans+=b/l\n}\np ans*l\n"}, {"source_code": "a,b=gets.split.map(&:to_i)\np gets.split.inject(0){|s,n|s+n.to_i/b}*b"}, {"source_code": "n, l = gets.split(\" \").map(&:to_i)\narr = gets.split(\" \").map(&:to_i)\n\np l * arr.map { |x| x / l }.reduce(:+)"}], "src_uid": "991516fa6f3ed5a71c547a3a50ea1a2b"} {"nl": {"description": "There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated.Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed.", "input_spec": "The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the initial number of beacons. The i-th of next n lines contains two integers ai and bi (0 ≤ ai ≤ 1 000 000, 1 ≤ bi ≤ 1 000 000) — the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai ≠ aj if i ≠ j.", "output_spec": "Print a single integer — the minimum number of beacons that could be destroyed if exactly one beacon is added.", "sample_inputs": ["4\n1 9\n3 1\n6 1\n7 4", "7\n1 1\n2 1\n3 1\n4 1\n5 1\n6 1\n7 1"], "sample_outputs": ["1", "3"], "notes": "NoteFor the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2.For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42."}, "positive_code": [{"source_code": "def lower_bound(array, value)\n left = -1\n right = array.length\n while left + 1 < right\n mid = left + (right - left) / 2\n if array[mid] >= value\n right = mid\n else\n left = mid\n end\n end\n right\nend\n\nn = gets.chomp.to_i\n\npos_lev = []\npos = []\nn.times do\n pos_lev << gets.chomp.split(' ').map{|i| i.to_i}\n pos << pos_lev.last[0]\nend\npos_lev.sort!\npos.sort!\n\ndp = Array.new(0)\ndp[0] = 0\nans = n\n\nn.times do |i|\n l = pos_lev[i][0] - pos_lev[i][1]\n t = lower_bound(pos, l)\n dp[i+1] = dp[t] + (i-t)\n ans = dp[i+1] + (n-i-1) if ans > dp[i+1] + (n-i-1)\nend\n\nputs ans"}, {"source_code": "def lower_bound(array, value)\n left = -1\n right = array.length\n while left + 1 < right\n mid = left + (right - left) / 2\n if array[mid] >= value\n right = mid\n else\n left = mid\n end\n end\n right\nend\n\nn = gets.chomp.to_i\n\npos_lev = []\npos = []\nn.times do\n pos_lev << gets.chomp.split(' ').map{|i| i.to_i}\n pos << pos_lev.last[0]\nend\npos_lev.sort!\npos.sort!\n\ndp = Array.new(0)\ndp[0] = 0\nans = n\n\nn.times do |i|\n l = pos_lev[i][0] - pos_lev[i][1]\n t = lower_bound(pos, l)\n dp[i+1] = dp[t] + (i-t)\n ans = dp[i+1] + (n-i-1) if ans > dp[i+1] + (n-i-1)\nend\n\nputs ans"}, {"source_code": "n = gets.chomp.to_i\nas = []\nbs = {}\nn.times do\n a, b = gets.chomp.split.map(&:to_i)\n as << a\n bs[a] = b\nend\n\nas.sort!\nar = as.reverse\ndp = [0]\nis = {}\nis[as[0]] = 0\nans = n - 1\nfor i in 1...n\n a = as[i]\n b = bs[a]\n is[a] = i\n a1 = ar.bsearch { |x| x < a - b }\n if a1.nil?\n dp << i\n else\n i1 = is[a1]\n dp << (i - i1 - 1 + dp[i1])\n end\n ans = [ans, n - i - 1 + dp[i]].min\nend\nputs ans"}], "negative_code": [{"source_code": "n = gets.chomp.to_i\nas = []\nbs = {}\nn.times do\n a, b = gets.chomp.split.map(&:to_i)\n as << a\n bs[a] = b\nend\n\nas.sort!\nar = as.reverse\ndp0 = [1]\ndp1 = [0]\nis = {}\nis[as[0]] = 0\nans = n - 1\nfor i in 1...n\n a = as[i]\n b = bs[a]\n is[a] = i\n i0 = i - 1\n dp0 << (1 + [dp0[i0], dp1[i0]].min)\n a1 = ar.bsearch { |x| x < a - b }\n if a1.nil?\n dp1 << i\n else\n i1 = is[a1]\n dp1 << (i - i1 - 1 + [dp0[i1], dp1[i1]].min)\n end\n ans = [ans, n - i - 1 + dp1[i]].min\nend\nans = [ans, dp0[-1]].min\nputs ans\n"}, {"source_code": "n = gets.chomp.to_i\nas = []\nbs = {}\nn.times do\n a, b = gets.chomp.split.map(&:to_i)\n as << a\n bs[a] = b\nend\n\nas.sort!\nar = as.reverse\ndp0 = [1]\ndp1 = [0]\nis = {}\nis[as[0]] = 0\nfor i in 1...n\n a = as[i]\n b = bs[a]\n is[a] = i\n i0 = i - 1\n dp0 << (1 + [dp0[i0], dp1[i0]].min)\n a1 = ar.bsearch { |x| x < a - b }\n if a1.nil?\n dp1 << i\n else\n i1 = is[a1]\n dp1 << (i - i1 - 1 + [dp0[i1], dp1[i1]].min)\n end\nend\n\nputs [dp0[-1], dp1[-1]].min\n"}, {"source_code": "n = gets.chomp.to_i\nas = []\nbs = {}\nn.times do\n a, b = gets.chomp.split.map(&:to_i)\n as << a\n bs[a] = b\nend\n\nas.sort!\nar = as.reverse\ndp0 = [1]\ndp1 = [0]\nis = {}\nis[as[0]] = 0\nans = n - 1\nfor i in 1...n\n a = as[i]\n b = bs[a]\n is[a] = i\n i0 = i - 1\n dp0 << (1 + [dp0[i0], dp1[i0]].min)\n a1 = ar.bsearch { |x| x < a - b }\n if a1.nil?\n dp1 << i\n else\n i1 = is[a1]\n dp1 << (i - i1 - 1 + [dp0[i1], dp1[i1]].min)\n end\n ans = [ans, n - i - 1 + dp0[i], n - i - 1 + dp1[i]].min\nend\nputs ans\n"}], "src_uid": "bcd689387c9167c7d0d45d4ca3b0c4c7"} {"nl": {"description": "Valera has got a rectangle table consisting of n rows and m columns. Valera numbered the table rows starting from one, from top to bottom and the columns – starting from one, from left to right. We will represent cell that is on the intersection of row x and column y by a pair of integers (x, y).Valera wants to place exactly k tubes on his rectangle table. A tube is such sequence of table cells (x1, y1), (x2, y2), ..., (xr, yr), that: r ≥ 2; for any integer i (1 ≤ i ≤ r - 1) the following equation |xi - xi + 1| + |yi - yi + 1| = 1 holds; each table cell, which belongs to the tube, must occur exactly once in the sequence. Valera thinks that the tubes are arranged in a fancy manner if the following conditions are fulfilled: no pair of tubes has common cells; each cell of the table belongs to some tube. Help Valera to arrange k tubes on his rectangle table in a fancy manner.", "input_spec": "The first line contains three space-separated integers n, m, k (2 ≤ n, m ≤ 300; 2 ≤ 2k ≤ n·m) — the number of rows, the number of columns and the number of tubes, correspondingly. ", "output_spec": "Print k lines. In the i-th line print the description of the i-th tube: first print integer ri (the number of tube cells), then print 2ri integers xi1, yi1, xi2, yi2, ..., xiri, yiri (the sequence of table cells). If there are multiple solutions, you can print any of them. It is guaranteed that at least one solution exists. ", "sample_inputs": ["3 3 3", "2 3 1"], "sample_outputs": ["3 1 1 1 2 1 3\n3 2 1 2 2 2 3\n3 3 1 3 2 3 3", "6 1 1 1 2 1 3 2 3 2 2 2 1"], "notes": "NotePicture for the first sample: Picture for the second sample: "}, "positive_code": [{"source_code": "input = gets.split(' ').map{|el| el.to_i}\n\nn = input[0] #строки\nm = input[1] #столбцы\nk = input[2] #трубы\n\nzigzag = []\n\nx = 1; y = 0\n\nwhile x <= n\n while y < m \n y += 1\n zigzag << [x, y]\n end\n\n x += 1\n break if x > n\n \n while y > 0\n zigzag << [x, y]\n y -= 1\n end\n\n x += 1\nend\n\n\nwhile k > 1\n print '2 '\n print zigzag.pop.join(' ')\n print ' '\n print zigzag.pop.join(' ')\n puts ''\n k-=1\nend\n\nprint zigzag.size\nprint ' '\nprint zigzag.join(' ')\n"}, {"source_code": "n,m,k=gets.split.map(&:to_i)\nl=n*m\na=[]\nc=[]\nk-=1\n(1..n).each do |i|\n\tf=(i%2==0)? m : 1\n\tf.step(m-f+1,(i%2==0) ? -1 : 1) do |j| \n\t\tc<= 0 and ri < s.size and s[lf] == s[ri]\n a[lf] = a[lf] + 1\n b[ri] = b[ri] + 1\n lf = lf - 1\n ri = ri + 1\n end\n end\nend\nsum = 0\nans = 0\ns.size.times do |i|\n ans += sum*a[i]\n sum += b[i]\nend\nputs ans\n \n\n"}, {"source_code": "s = gets.chop\nn = s.size\nb = [0] * n\ne = [0] * n\nn.times{|i|\n 2.times{|j|\n l, r = i, i + j\n while 0 <= l && s[l] == s[r]\n b[l] += 1\n e[r] += 1\n l -= 1\n r += 1\n end\n }\n}\ns, t = 0, 0\nn.times{|i|\n s += t * b[i]\n t += e[i]\n}\np s\n\n"}, {"source_code": "s = gets.chop\nn = s.size\nb = [0] * n\ne = [0] * n\nn.times{|i|\n 2.times{|j|\n l, r = i, i + j\n while 0 <= l && s[l] == s[r]\n b[l] += 1\n e[r] += 1\n l -= 1\n r += 1\n end\n }\n}\ns, t = 0, 0\nn.times{|i|\n s += t * b[i]\n t += e[i]\n}\np s\n\n"}, {"source_code": "\ndef count_palindromes(i, j)\n return if i < 0 || j >= @n\n\n if @string[i] == @string[j]\n\n @starting[i] += 1\n @ending[j] += 1\n\n count_palindromes(i - 1, j + 1)\n end\nend\n\n@string = gets.strip\n@n = @string.length\n\n@starting = Array.new(@n) { 1 }\n@ending = Array.new(@n) { 1 }\n\n(1...@n).each do |i|\n count_palindromes(i - 1, i)\n count_palindromes(i - 1, i + 1)\nend\n\n\n@ending_sums = Array.new(@n) { 0 }\n@ending_sums[0] = @ending[0]\n(1...@n).each do |i|\n @ending_sums[i] = @ending_sums[i - 1] + @ending[i]\nend\n\n# puts @starting, \"\", @ending, \"\", @ending_sums\n\nresult = 0\n(1...@n).each do |i|\n result += @ending_sums[i - 1] * @starting[i]\nend\n\nputs result\n"}, {"source_code": "s = gets.chop\nn = s.size\nb = [0] * n\ne = [0] * n\nn.times{|i|\n 2.times{|j|\n l, r = i, i + j\n while 0 <= l && s[l] == s[r]\n b[l] += 1\n e[r] += 1\n l -= 1\n r += 1\n end\n }\n}\ns, t = 0, 0\nn.times{|i|\n s += t * b[i]\n t += e[i]\n}\np s\n\n"}, {"source_code": "s = gets.chop\nn = s.size\nb = [0] * n\ne = [0] * n\nn.times{|i|\n 2.times{|j|\n l, r = i, i + j\n while 0 <= l && s[l] == s[r]\n b[l] += 1\n e[r] += 1\n l -= 1\n r += 1\n end\n }\n}\ns, t = 0, 0\nn.times{|i|\n s += t * b[i]\n t += e[i]\n}\np s\n\n"}, {"source_code": "s = gets.chop\nn = s.size\nb = [0] * n\ne = [0] * n\nn.times{|i|\n 2.times{|j|\n l, r = i, i + j\n while 0 <= l && s[l] == s[r]\n b[l] += 1\n e[r] += 1\n l -= 1\n r += 1\n end\n }\n}\ns, t = 0, 0\nn.times{|i|\n s += t * b[i]\n t += e[i]\n}\np s\n\n"}], "negative_code": [], "src_uid": "1708818cf66de9fa03439f608c897a90"} {"nl": {"description": "A continued fraction of height n is a fraction of form . You are given two rational numbers, one is represented as and the other one is represented as a finite fraction of height n. Check if they are equal.", "input_spec": "The first line contains two space-separated integers p, q (1 ≤ q ≤ p ≤ 1018) — the numerator and the denominator of the first fraction. The second line contains integer n (1 ≤ n ≤ 90) — the height of the second fraction. The third line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 1018) — the continued fraction. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.", "output_spec": "Print \"YES\" if these fractions are equal and \"NO\" otherwise.", "sample_inputs": ["9 4\n2\n2 4", "9 4\n3\n2 3 1", "9 4\n3\n1 2 4"], "sample_outputs": ["YES", "YES", "NO"], "notes": "NoteIn the first sample .In the second sample .In the third sample ."}, "positive_code": [{"source_code": "require\"mathn\"\np,q=gets.split.map(&:to_i)\nn=gets.to_i\na=gets.split.map(&:to_i)\nfrac=a.last\na[0..-2].reverse.each{|aa|frac=aa+1/frac}\nputs p/q==frac ? \"YES\" : \"NO\"\n"}, {"source_code": "require\"mathn\"\np,q=gets.split.map(&:to_i)\nn=gets.to_i\na=gets.split.map(&:to_i)\nfrac=a.last\na[0..-2].reverse.each{|aa|frac=aa+1/frac}\nputs p/q==frac ? \"YES\" : \"NO\"\n"}, {"source_code": "require\"mathn\"\np,q = gets.split().map {|x| x.to_i}\nn = gets.to_i\na = gets.split().map {|x| x.to_i}\na = a.reverse\nres = a[0];\na[1..-1].each {|x| res = x+1/res}\n\nputs (res == p/q)? \"YES\" : \"NO\"\n\n"}, {"source_code": "require\"mathn\"\np,q=gets.split.map(&:to_i)\nn=gets.to_i\na=gets.split.map(&:to_i)\nfrac=a.last\na[0..-2].reverse.each{|aa|frac=aa+1/frac}\nputs p/q==frac ? \"YES\" : \"NO\""}, {"source_code": "require\"mathn\"\np, q = gets.split(' ').map { |i| i.to_i }\nn = gets.to_i\na = gets.split(' ').map { |i| i.to_i }\nr = a.last\na[0..-2].reverse.each { |e| r = e + 1 / r;}\nputs p / q == r ? \"YES\" : \"NO\"\n"}, {"source_code": "require 'mathn'\n\nclass B\n def initialize\n p, q = gets.chomp.split(' ').map(&:to_i)\n n = gets.chomp\n num_list = gets.chomp.split(' ').map(&:to_i)\n\n res = calc(num_list.shift, num_list)\n if res == p/q\n puts \"YES\"\n else\n puts \"NO\"\n end\n end\n\n def calc(a, b)\n if b.size == 0\n return a\n else\n a + 1/calc(b.shift, b)\n end\n end\nend\n\nb = B.new"}, {"source_code": "require\"mathn\"\np,q=gets.split.map(&:to_i)\nn=gets.to_i\na=gets.split.map(&:to_i)\nfrac=a.last\na[0..-2].reverse.each{|aa|frac=aa+1/frac}\nputs p/q==frac ? \"YES\" : \"NO\"\n"}, {"source_code": "require\"mathn\"\np,q=gets.split.map(&:to_i)\nn=gets.to_i\na=gets.split.map(&:to_i)\nfrac=a.last\na[0..-2].reverse.each{|aa|frac=aa+1/frac}\nputs p/q==frac ? \"YES\" : \"NO\"\n"}, {"source_code": "require \"mathn\"\n\nloop do\n line = gets\n break if line.nil?\n p,q = line.split(\" \").map(&:to_i)\n n = gets.to_i\n as = gets.split(\" \").map(&:to_i)\n cal = as.reverse.inject(0){ |res, val|\n res **= -1 unless res.zero?\n res + val\n }\n if cal == p/q\n puts \"YES\"\n else\n puts \"NO\"\n end\nend\n\n"}, {"source_code": "require\"mathn\"\np,q=gets.split.map(&:to_i)\nn=gets.to_i\na=gets.split.map(&:to_i)\nfrac=a.last\na[0..-2].reverse.each{|aa|frac=aa+1/frac}\nputs p/q==frac ? \"YES\" : \"NO\""}, {"source_code": "p, q = gets.split.map(&:to_i)\nn = gets.to_i\na = gets.split.reverse.map(&:to_i)\n\nx = Rational(0)\nfor i in 0 .. n-2 do\n x += Rational(a[i], 1)\n x = Rational(1, x)\nend\nx += Rational(a[n-1], 1)\n\nif Rational(p, q) == x then\n puts \"YES\"\nelse\n puts \"NO\"\nend\n"}, {"source_code": "require\"mathn\"\np,q=gets.split.map(&:to_i)\nn=gets.to_i\na=gets.split.map(&:to_i)\nfrac=a.last\na[0..-2].reverse.each{|aa|frac=aa+1/frac}\nputs p/q==frac ? \"YES\" : \"NO\"\n"}, {"source_code": "p,q = gets.chomp.split(\" \").collect {|x| x.to_i}\nn = gets.to_i\narr = gets.chomp.split(\" \").collect {|x| x.to_i}\n\nif arr[-1]==1 and arr.length>1\n arr[-2] += 1\n arr.delete_at(n-1)\nend\n\nok = true\nnt = p\naux = -1\narr.each { |v|\n if q == 0\n ok = false\n break\n end\n nt = p/q\n if (nt!=v)\n ok = false\n break\n end\n aux = p%q\n p = q\n q = aux\n\n}\n\nif ok and aux == 0\n puts \"YES\"\nelse\n puts \"NO\"\nend"}, {"source_code": "def rec(ary)\n\treturn Rational(1, ary.last) if ary.size == 1\n\tRational(1, ary.first + rec(ary.drop(1)))\nend\n\nf1 = Rational(*gets.split.map(&:to_i))\ngets\nf2 = rec(gets.split.map(&:to_i))\n\nif f1 == Rational(f2.denominator, f2.numerator)\n\tputs \"YES\"\nelse\n\tputs \"NO\"\nend"}, {"source_code": "require\"mathn\"\np,q=gets.split.map(&:to_i)\nn=gets.to_i\na=gets.split.map(&:to_i)\nfrac=a.last\na[0..-2].reverse.each{|aa|frac=aa+1/frac}\nputs p/q==frac ? \"YES\" : \"NO\"\n"}, {"source_code": "p,q=gets.split.map(&:to_i)\nn=gets.to_i\na=[]\na=gets.split.map(&:to_i)\ns=1\nb=0\ni = n\nwhile i>0\ni=i-1\nks=b\nkb=s\ns=kb*a[i]+ks\nb=kb\nend\nif s*q==b*p\nprint \"YES\"\nend\nif s*q!=b*p\nputs \"NO\"\nend"}, {"source_code": "def solve(a, p, q)\n if a.size == 1\n return p == q * a.first ? \"YES\" : \"NO\"\n end\n t = a.shift\n solve(a,q, (p - t * q))\nend\n\np,q = gets.split.map(&:to_i)\nn = gets.to_i\na = gets.split.map(&:to_i)\n\nputs solve(a,p,q)\n"}, {"source_code": "p, q = gets.split.map(&:to_i)\nd = p.gcd(q)\np, q = p / d, q / d\nn = gets.to_i\na = gets.split.map(&:to_i)\nx, y = a[n-1], 1\n(n-2).downto(0) do |i|\n\tx, y = y, x\n\tx = x + a[i] * y\nend\nif x == p && y == q\n\tputs \"YES\"\nelse\n\tputs \"NO\"\nend\n"}, {"source_code": "#!/usr/bin/ruby\n\nclass Fraction\n @num\n @den\n \n def initialize(a, b)\n @num = a\n @den = b\n end\n\n def num=(num)\n @num = num\n end\n\n def num\n @num\n end\n\n def den=(den)\n @den = den\n end\n\n def den\n @den\n end\nend\n\ndef cf(pos)\n return Fraction.new(1, $nums[pos]) if pos + 1 == $n\n\n res = cf(pos + 1)\n temp = Fraction.new(res.num + res.den * $nums[pos], res.den)\n return Fraction.new(temp.den, temp.num)\nend\n\ntokens = gets.split(\" \")\np = tokens[0].to_i\nq = tokens[1].to_i\n$n = gets.to_i\n\n\ntokens = gets.split(\" \")\n$nums = Array.new($n)\n\n0.upto(tokens.length - 1) do |i|\n $nums[i] = tokens[i].to_i\nend\n\nresult = cf(0)\n\ng = p.gcd(q)\n#puts g\n\nf = result.num.gcd(result.den)\n#puts f\n\np /= g\nq /= g\n\na = result.den / f\nb = result.num / f\n\nif b == q and a == p\n puts \"YES\"\nelse\n puts \"NO\"\nend\n"}, {"source_code": "require\"mathn\"\np,q=gets.split.map(&:to_i)\nn=gets.to_i\na=gets.split.map(&:to_i)\nfrac=a.last\na[0..-2].reverse.each{|aa|frac=aa+1/frac}\nputs p/q==frac ? \"YES\" : \"NO\"\n"}, {"source_code": "require\"mathn\"\np,q=gets.split.map(&:to_i)\nn=gets.to_i\na=gets.split.map(&:to_i)\nfrac=a.last\na[0..-2].reverse.each{|aa|frac=aa+1/frac}\nputs p/q==frac ? \"YES\" : \"NO\""}, {"source_code": "(p,q) = gets.split(' ').map{|i| i.to_i()}\nn = gets.to_i()\na = gets.split(' ').map{|i| i.to_i()}\nx,y = 1,0\n\na.reverse_each{|i| x,y = y+x*i,x}\n\nputs ((x*q==y*p)?\"YES\":\"NO\")\n"}, {"source_code": "require\"mathn\"\np,q=gets.split.map(&:to_i)\nn=gets.to_i\na=gets.split.map(&:to_i)\nfrac=a.last\na[0..-2].reverse.each{|aa|frac=aa+1/frac}\nputs p/q==frac ? \"YES\" : \"NO\"\n"}, {"source_code": "(p, q) = gets.split.map(&:to_i)\nn = gets.to_i\na = gets.split.map(&:to_i)\n\nr = Rational(0)\nfor b in a.reverse\n r = 1 / (b + r)\nend\n\nif r == Rational(q, p)\n puts \"YES\"\nelse\n puts \"NO\"\nend\n"}], "negative_code": [{"source_code": "p,q = gets.split().map {|x| x.to_i}\nn = gets.to_i\na = gets.split().map {|x| x.to_i}\na.reverse!\nres = a[0];\na[1..-1].each {|x| res = x+1.0/res}\nputs (res == 1.0*p/q)? \"YES\" : \"NO\"\n"}, {"source_code": "p,q = gets.split().map {|x| x.to_i}\nn = gets.to_i\na = gets.split().map {|x| x.to_i}\na.reverse!\nres = a[0];\na[1..-1].each {|x| res = x+1/res}\nputs (res == p/q)? \"YES\" : \"NO\"\n"}, {"source_code": "p, q = gets.split(' ').map { |i| i.to_f }\nn = gets.to_i\na = gets.split(' ').map { |i| i.to_f }\nr = a.last\na[0..-2].reverse_each { |e| r = e + 1.0 / r;}\nputs p / q == r ? \"YES\" : \"NO\""}, {"source_code": "p, q = gets.split(' ').map {|i| i.to_i}\nn = gets.to_i\na = gets.split(' ').map {|i| i.to_i}\ndef fun(arr, ind)\n if(ind == arr.size - 1)\n return [1, arr[ind]]\n end\n ret = fun(arr, ind + 1)\n return [ret[1], ret[0] + arr[ind] * ret[1]]\nend\nret = fun(a, 0)\nif p == ret[1] and q == ret[0] then puts \"YES\"\nelse puts \"NO\"\nend"}, {"source_code": "p, q = gets.split(' ').map { |i| i.to_i }\nn = gets.to_i\na = gets.split(' ').map { |i| i.to_i }\nr = a.last\na[0..-2].reverse.each { |e| r = e + 1 / r;}\nputs p / q == r ? \"YES\" : \"NO\"\n"}, {"source_code": "p, q = gets.split(' ').map { |i| i.to_f }\nn = gets.to_i\na = gets.split(' ').map { |i| i.to_f }\nr = a.last\na[0..-2].reverse.each { |e| r = e + 1 / r;}\nputs p / q == r ? \"YES\" : \"NO\"\n"}, {"source_code": "class B\n def initialize\n p, q = gets.chomp.split(' ').map(&:to_f)\n n = gets.chomp\n num_list = gets.chomp.split(' ').map(&:to_f)\n\n res = calc(num_list.shift, num_list)\n if res == p/q\n puts \"YES\"\n else\n puts \"NO\"\n end\n end\n\n def calc(a, b)\n if b.size == 0\n return a\n else\n a + 1/calc(b.shift, b)\n end\n end\nend\n\nb = B.new"}, {"source_code": "#!/usr/bin/ruby\n\nclass Fraction\n @num\n @den\n \n def initialize(a, b)\n @num = a\n @den = b\n end\n\n def num=(num)\n @num = num\n end\n\n def num\n @num\n end\n\n def den=(den)\n @den = den\n end\n\n def den\n @den\n end\nend\n\ndef cf(pos)\n return Fraction.new(1, $nums[pos]) if pos + 1 == $n\n\n res = cf(pos + 1)\n temp = Fraction.new(res.num + res.den * $nums[pos], res.den)\n return Fraction.new(temp.den, temp.num)\nend\n\ntokens = gets.split(\" \")\np = tokens[0].to_i\nq = tokens[1].to_i\n$n = gets.to_i\n\n\ntokens = gets.split(\" \")\n$nums = Array.new($n)\n\n0.upto(tokens.length - 1) do |i|\n $nums[i] = tokens[i].to_i\nend\n\nresult = cf(0)\n\nif result.num == q and result.den == p\n puts \"YES\"\nelse\n puts \"NO\"\nend\n"}], "src_uid": "447ebe088a0a60a7a44a3fc76056bc65"} {"nl": {"description": "You are playing a new famous fighting game: Kortal Mombat XII. You have to perform a brutality on your opponent's character.You are playing the game on the new generation console so your gamepad have $$$26$$$ buttons. Each button has a single lowercase Latin letter from 'a' to 'z' written on it. All the letters on buttons are pairwise distinct.You are given a sequence of hits, the $$$i$$$-th hit deals $$$a_i$$$ units of damage to the opponent's character. To perform the $$$i$$$-th hit you have to press the button $$$s_i$$$ on your gamepad. Hits are numbered from $$$1$$$ to $$$n$$$.You know that if you press some button more than $$$k$$$ times in a row then it'll break. You cherish your gamepad and don't want to break any of its buttons.To perform a brutality you have to land some of the hits of the given sequence. You are allowed to skip any of them, however changing the initial order of the sequence is prohibited. The total damage dealt is the sum of $$$a_i$$$ over all $$$i$$$ for the hits which weren't skipped.Note that if you skip the hit then the counter of consecutive presses the button won't reset.Your task is to skip some hits to deal the maximum possible total damage to the opponent's character and not break your gamepad buttons.", "input_spec": "The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le k \\le n \\le 2 \\cdot 10^5$$$) — the number of hits and the maximum number of times you can push the same button in a row. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$), where $$$a_i$$$ is the damage of the $$$i$$$-th hit. The third line of the input contains the string $$$s$$$ consisting of exactly $$$n$$$ lowercase Latin letters — the sequence of hits (each character is the letter on the button you need to press to perform the corresponding hit).", "output_spec": "Print one integer $$$dmg$$$ — the maximum possible damage to the opponent's character you can deal without breaking your gamepad buttons.", "sample_inputs": ["7 3\n1 5 16 18 7 2 10\nbaaaaca", "5 5\n2 4 1 3 1000\naaaaa", "5 4\n2 4 1 3 1000\naaaaa", "8 1\n10 15 2 1 4 8 15 16\nqqwweerr", "6 3\n14 18 9 19 2 15\ncccccc", "2 1\n10 10\nqq"], "sample_outputs": ["54", "1010", "1009", "41", "52", "10"], "notes": "NoteIn the first example you can choose hits with numbers $$$[1, 3, 4, 5, 6, 7]$$$ with the total damage $$$1 + 16 + 18 + 7 + 2 + 10 = 54$$$.In the second example you can choose all hits so the total damage is $$$2 + 4 + 1 + 3 + 1000 = 1010$$$.In the third example you can choose all hits expect the third one so the total damage is $$$2 + 4 + 3 + 1000 = 1009$$$.In the fourth example you can choose hits with numbers $$$[2, 3, 6, 8]$$$. Only this way you can reach the maximum total damage $$$15 + 2 + 8 + 16 = 41$$$.In the fifth example you can choose only hits with numbers $$$[2, 4, 6]$$$ with the total damage $$$18 + 19 + 15 = 52$$$.In the sixth example you can change either first hit or the second hit (it does not matter) with the total damage $$$10$$$."}, "positive_code": [{"source_code": "n, k = gets.strip.split(' ').map(&:to_i)\na = gets.strip.split(' ').map(&:to_i)\nh = gets.strip.split('').to_a\n\n\nres = 0\nlast = h[0]\ntmp = []\nidx = 0\n\nwhile idx < a.size\n if idx.zero?\n tmp << a[idx]\n idx += 1\n next\n end\n if h[idx] != h[idx - 1]\n res += tmp.size.zero? ? 0 : tmp.sort.reverse.slice(0, k).inject(:+)\n tmp = [a[idx]]\n else\n tmp << a[idx]\n end\n idx += 1\nend\nif tmp.size > 0\n res += tmp.size.zero? ? 0 : tmp.sort.reverse.slice(0, k).inject(:+)\nend\nputs res"}, {"source_code": "if File.exists?(\"input\")\n $stdin = File.open(\"input\")\n $stdout = File.open(\"output\",\"w\")\nend\n\n#use moving window to get the sum\nnk = gets.chomp.split(\" \").map(&:to_i)\nn = nk[0]\nk = nk[1]\na = gets.chomp.split(\" \").map(&:to_i)\ns = gets.chomp\n\narr = [0]\nbreakpoints = 2\n(1..n-1).each do |i|\n\tif s[i]!=s[i-1]\n\t\tbreakpoints+=1\n\t\tarr.push i\n\tend\nend\narr.push n\n\ntotsum = 0\n#bigarr = []\n=begin\n\n(0..breakpoints-2).each do |st|\n\tnewarr = []\n\tnewlen = 1\n\tmin = 1.0/0.0\n\tsum = 0\n\tmax = 0\n\t(arr[st]..arr[st+1]-1).each do |range|\n\t\tthis = a[range]\n#\t\tp newlen\n\t\tif(newlen<=k)\n\t\t\tnewarr.push this\n\t\t\tsum+=this\n\t\t\tmin = this if thismin)\n\t\t\t\tsum-=min\n\t\t\t\tsum+=this\n\t\t\t\tnewarr.push this\n\t\t\t\tnewarr.delete_at(newarr.index(min))\n\t\t\t\tmin = newarr.min\n\t\t\tend\n\t\tend\n\t\tmax = sum if sum>max\n\tend\n\ttotsum += sum\nend\n=end\n#p arr\n(0..breakpoints-2).each do |st|\n\tnewarr = a[arr[st]..arr[st+1]-1].sort\n#\tp newarr\n\tiniti = newarr.length-1\n\ti = initi\n\tmin = arr[st]\n\tsum = 0\n#\tp initi\n\twhile true\n\t\tbreak if (initi-i+1)>k\n\t\tbreak if i<0\n#\t\tp i\n\t\tsum+=newarr[i]\n\t\ti -= 1\n#\t\tp sum\n\tend\n#\tp sum\n\ttotsum += sum\nend\n\n\n#p arr\nputs totsum"}], "negative_code": [{"source_code": "if File.exists?(\"input\")\n $stdin = File.open(\"input\")\n $stdout = File.open(\"output\",\"w\")\nend\n\n#use moving window to get the sum\nnk = gets.chomp.split(\" \").map(&:to_i)\nn = nk[0]\nk = nk[1]\na = gets.chomp.split(\" \").map(&:to_i)\ns = gets.chomp\n\narr = [0]\nbreakpoints = 2\n(1..n-1).each do |i|\n\tif s[i]!=s[i-1]\n\t\tbreakpoints+=1\n\t\tarr.push i\n\tend\nend\narr.push n\n\nbigarr = []\n(0..breakpoints-2).each do |st|\n\tnewarr = []\n\tnewlen = 1\n\tmin = 1.0/0.0\n\tsum = 0\n\t(arr[st]..arr[st+1]-1).each do |range|\n\t\tthis = a[range]\n#\t\tp newlen\n\t\tif(newlen<=k)\n\t\t\tnewarr.push this\n\t\t\tsum+=this\n#\t\t\tmin = this if thismin)\n\t\t\t\tsum-=newarr.pop\n\t\t\t\tsum+=this\n\t\t\t\tnewarr.push this\n#\t\t\t\tnewarr.delete_at(newarr.index(min))\n#\t\t\t\tmin = newarr.min\n\t\t\tend\n\t\tend\n\t\tnewarr.sort!\n\t\tbigarr.push sum\n\tend\nend\n\ntotsum = 0\n(0..breakpoints-2).each do |st|\n\ttotsum += bigarr[arr[st]..arr[st+1]-1].max\nend\n\n#p arr\n#p bigarr\nputs totsum"}], "src_uid": "aa3b5895046ed34e89d5fcc3264b3944"} {"nl": {"description": "You are given the string s of length n and the numbers p, q. Split the string s to pieces of length p and q.For example, the string \"Hello\" for p = 2, q = 3 can be split to the two strings \"Hel\" and \"lo\" or to the two strings \"He\" and \"llo\".Note it is allowed to split the string s to the strings only of length p or to the strings only of length q (see the second sample test).", "input_spec": "The first line contains three positive integers n, p, q (1 ≤ p, q ≤ n ≤ 100). The second line contains the string s consists of lowercase and uppercase latin letters and digits.", "output_spec": "If it's impossible to split the string s to the strings of length p and q print the only number \"-1\". Otherwise in the first line print integer k — the number of strings in partition of s. Each of the next k lines should contain the strings in partition. Each string should be of the length p or q. The string should be in order of their appearing in string s — from left to right. If there are several solutions print any of them.", "sample_inputs": ["5 2 3\nHello", "10 9 5\nCodeforces", "6 4 5\nPrivet", "8 1 1\nabacabac"], "sample_outputs": ["2\nHe\nllo", "2\nCodef\norces", "-1", "8\na\nb\na\nc\na\nb\na\nc"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/ruby\nn,a,b=gets.split.map(&:to_i)\nj=0.upto(n/a).find{|i|(n-i*a)%b==0}\nif !j\n\tp -1\nelse\n\tk=(n-j*a)/b\n\ts=gets.chomp.chars.to_a\n\tp j+k\n\tj.times{puts s.shift(a)*''}\n\tk.times{puts s.shift(b)*''}\nend"}, {"source_code": "#!/usr/bin/ruby\nn,a,b=gets.split.map(&:to_i)\nj=0.upto(n/a).find{|i|(n-i*a)%b==0}\nif !j\n\tp -1\nelse\n\tk=(n-j*a)/b\n\ts=gets.chomp.chars.to_a\n\tp j+k\n\tj.times{puts s.shift(a)*''}\n\tk.times{puts s.shift(b)*''}\nend\n"}, {"source_code": "#!/usr/bin/ruby\nn,a,b=gets.split.map(&:to_i)\nj=0.upto(n/a).find{|i|(n-i*a)%b==0}\nif !j\n\tp -1\nelse\n\tk=(n-j*a)/b\n\ts=gets.chomp.chars.to_a\n\tp j+k\n\tj.times{puts s.shift(a)*''}\n\tk.times{puts s.shift(b)*''}\nend\n"}, {"source_code": "\ndef count(n, p, q, s)\n\ti = 0\n\n\twhile (n - i*q) >=0\t\t\n\t\tif (n-i*q)%p == 0\n\t\t\tres = Array.new\n \t\t\ti.times do |e|\n \t\t\t\tres << s[e*q...(e+1)*q]\n\t\t\tend\n\n\t\t\thead = i*q\n\t\t\t\n\t\t\t(((n-i*q)/p).to_i()).times do |e|\n\t\t\t\tres << s[head+e*p...head+(e+1)*p]\n\t\t\tend\n\n\t\t\tputs i+(n-i*q)/p.to_i\n\t\t\tputs res\n\t\t\treturn 1\n\t\tend\t\t\n\t\ti += 1\n\t\t\n\tend\n\treturn -1\nend\n\n\nn, p, q = gets.strip.split.map(&:to_i)\ns = gets.strip\np, q = q, p if p > q\nif count(n,p,q, s) == -1\n\tputs -1\nend"}, {"source_code": "#!/usr/bin/ruby\nn,a,b=gets.split.map(&:to_i)\nj=0.upto(n/a).find{|i|(n-i*a)%b==0}\nif !j\n\tp -1\nelse\n\tk=(n-j*a)/b\n\ts=gets.chomp.chars.to_a\n\tp j+k\n\tj.times{puts s.shift(a)*''}\n\tk.times{puts s.shift(b)*''}\nend\n"}, {"source_code": "a=gets.chomp.split(\" \")\nn=a[0].to_f\np=a[1].to_f\nj=a[2].to_f\ns=gets.chomp\n\nsc1=0\nsc1c=0\nsc1n=n\nloop do\n sc1n=sc1n-p\n\tbreak if sc1n <= 0\n\tif sc1n%j==0\n\t\tsc1+=1\n\tend\n\tsc1c+=1\n\tbreak if sc1n%j==0\nend \n\n\nsc2=0\nsc2c=0\nsc2n=n\nloop do\n sc2n=sc2n-j\n\tbreak if sc2n <= 0\n\tif sc2n%p==0\n\t\tsc2+=1\n\tend\n\tsc2c+=1\n\tbreak if sc2n%p==0\nend \n\n\n\n\nif n==p+j\n puts 2\n puts s[0..(p-1)]\n puts s[p..(s.length-1)]\nelsif n%p==0\n split=n/p\n\tputs split.to_i\n\tc1=0\n\tc2=0\n\tloop do\n\t\tbreak if c2==split\n\t\tputs s[c1..(c1+p-1)]\n\t\tc1=c1+p\n c2+=1 \n\tend\t\n \nelsif n%j==0\n split=n/j\n\tputs split.to_i\n\tc1=0\n\tc2=0\n\tloop do\n\t\tbreak if c2==split\n\t\tputs s[c1..(c1+j-1)]\n\t\tc1=c1+j\n c2+=1 \n\tend\t\nelsif sc1>0\n\tputs (sc1c+(n-sc1c*p)/j ).to_i\n\tc1=0\n\tc2=0\n\tloop do\n\t\tbreak if c2==sc1c\n\t\tputs s[c1..(c1+p-1)]\n\t\tc1=c1+p\n c2+=1 \n\tend\t\n\t\n\tc2=0\n\tloop do\n\t\tbreak if c2==(n-sc1c*p)/j\n\t\tputs s[c1..(c1+j-1)]\n\t\tc1=c1+j\n c2+=1 \n\tend\t\nelsif sc2>0\n\tputs (sc2c+(n-sc2c*j)/p ).to_i\n\t\n\tc2=0\n\tloop do\n\t\tbreak if c2==sc2c\n\t\tputs s[c1..(c1+j-1)]\n\t\tc1=c1+j\n c2+=1 \n\tend\t\n\tc1=0\n\tc2=0\n\tloop do\n\t\tbreak if c2==(n-sc2c*j)/p\n\t\tputs s[c1..(c1+p-1)]\n\t\tc1=c1+p\n c2+=1 \n\tend\t\nelse\n\tputs -1\nend"}, {"source_code": "#!/usr/bin/ruby\nn,a,b=gets.split.map(&:to_i)\nj=0.upto(n/a).find{|i|(n-i*a)%b==0}\nif !j\n\tp -1\nelse\n\tk=(n-j*a)/b\n\ts=gets.chomp.chars.to_a\n\tp j+k\n\tj.times{puts s.shift(a)*''}\n\tk.times{puts s.shift(b)*''}\nend\n"}, {"source_code": "a=gets.chomp.split(\" \")\nn=a[0].to_f\np=a[1].to_f\nj=a[2].to_f\ns=gets.chomp\n\nsc1=0\nsc1c=0\nsc1n=n\nloop do\n sc1n=sc1n-p\n\tbreak if sc1n <= 0\n\tif sc1n%j==0\n\t\tsc1+=1\n\tend\n\tsc1c+=1\n\tbreak if sc1n%j==0\nend \n\n\nsc2=0\nsc2c=0\nsc2n=n\nloop do\n sc2n=sc2n-j\n\tbreak if sc2n <= 0\n\tif sc2n%p==0\n\t\tsc2+=1\n\tend\n\tsc2c+=1\n\tbreak if sc2n%p==0\nend \n\n\n\n\nif n==p+j\n puts 2\n puts s[0..(p-1)]\n puts s[p..(s.length-1)]\nelsif n%p==0\n split=n/p\n\tputs split.to_i\n\tc1=0\n\tc2=0\n\tloop do\n\t\tbreak if c2==split\n\t\tputs s[c1..(c1+p-1)]\n\t\tc1=c1+p\n c2+=1 \n\tend\t\n \nelsif n%j==0\n split=n/j\n\tputs split.to_i\n\tc1=0\n\tc2=0\n\tloop do\n\t\tbreak if c2==split\n\t\tputs s[c1..(c1+j-1)]\n\t\tc1=c1+j\n c2+=1 \n\tend\t\nelsif sc1>0\n\tputs (sc1c+(n-sc1c*p)/j ).to_i\n\tc1=0\n\tc2=0\n\tloop do\n\t\tbreak if c2==sc1c\n\t\tputs s[c1..(c1+p-1)]\n\t\tc1=c1+p\n c2+=1 \n\tend\t\n\t\n\tc2=0\n\tloop do\n\t\tbreak if c2==(n-sc1c*p)/j\n\t\tputs s[c1..(c1+j-1)]\n\t\tc1=c1+j\n c2+=1 \n\tend\t\nelsif sc2>0\n\tputs (sc2c+(n-sc2c*j)/p ).to_i\n\t\n\tc2=0\n\tloop do\n\t\tbreak if c2==sc2c\n\t\tputs s[c1..(c1+j-1)]\n\t\tc1=c1+j\n c2+=1 \n\tend\t\n\tc1=0\n\tc2=0\n\tloop do\n\t\tbreak if c2==(n-sc2c*j)/p\n\t\tputs s[c1..(c1+p-1)]\n\t\tc1=c1+p\n c2+=1 \n\tend\t\nelse\n\tputs -1\nend"}, {"source_code": "#!/usr/bin/env ruby\n\ndef split n, p, q\n max_left_coeff = n / p\n (0..max_left_coeff).each do |a|\n left = a * p\n right = n - left\n if right % q == 0\n return [a, (right / q)]\n end\n end\n [-1, 0]\nend\n\nn, p, q = gets.split.map(&:to_i)\ns = gets.chomp\n\na, b = split(n, p, q)\n\nputs (a + b)\na.times do\n puts s[0, p]\n s = s[p..s.size-1]\nend\nb.times do\n puts s[0, q]\n s = s[q..s.size-1]\nend\n"}, {"source_code": "line1 = gets.to_s.split(' ').collect{ |x| x.to_i}\nn = line1[0]\np = line1[1]\nq = line1[2]\n\ns = gets\n\nx = n / p\n\ny = n / q\ntotal = -1\ntmp = -1\n(0..x).each do |index|\n ci = n - index * p\n if ci % q == 0\n total = index + ci / q\n tmp = index\n break\n end\nend\n\nputs total\nif total != -1\n cur = 0\n (1..tmp).each do\n puts s[cur...cur+p]\n cur+=p\n end\n (1..total - tmp).each do\n puts s[cur...cur+q]\n cur += q\n end\nend"}, {"source_code": "n, p, q = gets.split.map(&:to_i)\ns = gets.chomp\n\na = b = -1\nfor i in 0..100\n for j in 0..100\n if p*i + q*j == n\n a = i\n b = j\n end\n end\nend\n\nif a == -1\n puts a\n exit\nend\n\nputs a+b\n\nidx = 0\n(1..a).each do\n puts s[idx, p]\n idx += p\nend\n\n(1..b).each do\n puts s[idx, q]\n idx += q\nend\n"}, {"source_code": "a=gets.chomp.split(\" \")\nn=a[0].to_f\np=a[1].to_f\nj=a[2].to_f\ns=gets.chomp\n\nsc1=0\nsc1c=0\nsc1n=n\nloop do\n sc1n=sc1n-p\n\tbreak if sc1n <= 0\n\tif sc1n%j==0\n\t\tsc1+=1\n\tend\n\tsc1c+=1\n\tbreak if sc1n%j==0\nend \n\n\nsc2=0\nsc2c=0\nsc2n=n\nloop do\n sc2n=sc2n-j\n\tbreak if sc2n <= 0\n\tif sc2n%p==0\n\t\tsc2+=1\n\tend\n\tsc2c+=1\n\tbreak if sc2n%p==0\nend \n\n\n\n\nif n==p+j\n puts 2\n puts s[0..(p-1)]\n puts s[p..(s.length-1)]\nelsif n%p==0\n split=n/p\n\tputs split.to_i\n\tc1=0\n\tc2=0\n\tloop do\n\t\tbreak if c2==split\n\t\tputs s[c1..(c1+p-1)]\n\t\tc1=c1+p\n c2+=1 \n\tend\t\n \nelsif n%j==0\n split=n/j\n\tputs split.to_i\n\tc1=0\n\tc2=0\n\tloop do\n\t\tbreak if c2==split\n\t\tputs s[c1..(c1+j-1)]\n\t\tc1=c1+j\n c2+=1 \n\tend\t\nelsif sc1>0\n\tputs (sc1c+(n-sc1c*p)/j ).to_i\n\tc1=0\n\tc2=0\n\tloop do\n\t\tbreak if c2==sc1c\n\t\tputs s[c1..(c1+p-1)]\n\t\tc1=c1+p\n c2+=1 \n\tend\t\n\t\n\tc2=0\n\tloop do\n\t\tbreak if c2==(n-sc1c*p)/j\n\t\tputs s[c1..(c1+j-1)]\n\t\tc1=c1+j\n c2+=1 \n\tend\t\nelsif sc2>0\n\tputs (sc2c+(n-sc2c*j)/p ).to_i\n\t\n\tc2=0\n\tloop do\n\t\tbreak if c2==sc2c\n\t\tputs s[c1..(c1+j-1)]\n\t\tc1=c1+j\n c2+=1 \n\tend\t\n\tc1=0\n\tc2=0\n\tloop do\n\t\tbreak if c2==(n-sc2c*j)/p\n\t\tputs s[c1..(c1+p-1)]\n\t\tc1=c1+p\n c2+=1 \n\tend\t\nelse\n\tputs -1\nend\n"}, {"source_code": "#!/usr/bin/ruby\nn,a,b=gets.split.map(&:to_i)\nj=0.upto(n/a).find{|i|(n-i*a)%b==0}\nif !j\n\tp -1\nelse\n\tk=(n-j*a)/b\n\ts=gets.chomp.chars.to_a\n\tp j+k\n\tj.times{puts s.shift(a)*''}\n\tk.times{puts s.shift(b)*''}\nend\n"}, {"source_code": "#!/usr/bin/ruby\nn,a,b=gets.split.map(&:to_i)\nj=0.upto(n/a).find{|i|(n-i*a)%b==0}\nif !j\n\tp -1\nelse\n\tk=(n-j*a)/b\n\ts=gets.chomp.chars.to_a\n\tp j+k\n\tj.times{puts s.shift(a)*''}\n\tk.times{puts s.shift(b)*''}\nend\n"}, {"source_code": "n,p,q=gets.split.map(&:to_i)\ns=gets.chomp.split(//)\na=b=0\n\n0.upto(100){|k|\n\tm=n-k*p\n\tbreak if m<0\n\tif m%q==0\n\t\ta=k\n\t\tb=m/q\n\t\tbreak\n\tend\n}\nif a==0&&b==0\n\tputs -1\nelse\n\tputs a+b\n\ta.times{puts s.shift(p).join}\n\tb.times{puts s.shift(q).join}\nend\n"}, {"source_code": "class Solver\n\tdef main\n\t\tn, a, b = gets.split.map { |x| x.to_i }\n\t\tstr = gets.chomp\n\t\t0.upto(n) do |l|\n\t\t\tr = n - l\n\t\t\tif l % a == 0 && r % b == 0\n\t\t\t\tputs l / a + r / b\n\t\t\t\t0.upto(l / a - 1) do |i|\n\t\t\t\t\tputs str[i * a, a]\n\t\t\t\tend\n\t\t\t\t0.upto(r / b - 1) do |i|\n\t\t\t\t\tputs str[l + i * b, b]\n\t\t\t\tend\n\t\t\t\treturn\n\t\t\tend\n\t\tend\n\t\tputs -1\n\tend\nend\nSolver.new.main\n"}, {"source_code": "#!/usr/bin/ruby\nn,a,b=gets.split.map(&:to_i)\nj=0.upto(n/a).find{|i|(n-i*a)%b==0}\nif !j\n\tp -1\nelse\n\tk=(n-j*a)/b\n\ts=gets.chomp.chars.to_a\n\tp j+k\n\tj.times{puts s.shift(a)*''}\n\tk.times{puts s.shift(b)*''}\nend\n"}], "negative_code": [{"source_code": "a=gets.chomp.split(\" \")\nn=a[0].to_f\np=a[1].to_f\nj=a[2].to_f\ns=gets.chomp\n\nsc1=0\nsc1c=0\nsc1n=n\nloop do\n sc1n=sc1n-p\n\tbreak if sc1n <= 0\n\tif sc1n%j==0\n\t\tsc1+=1\n\tend\n\tsc1c+=1\n\tbreak if sc1n%j==0\nend \n\n\nsc2=0\nsc2c=0\nsc2n=n\nloop do\n sc2n=sc2n-j\n\tbreak if sc2n <= 0\n\tif sc2n%p==0\n\t\tsc2+=1\n\tend\n\tsc2c+=1\n\tbreak if sc2n%p==0\nend \n\n\n\n\nif n==p+j\n puts 2\n puts s[0..(p-1)]\n puts s[p..(s.length-1)]\nelsif n%p==0\n split=n/p\n\tputs split.to_i\n\tc1=0\n\tc2=0\n\tloop do\n\t\tbreak if c2==split\n\t\tputs s[c1..(c1+p-1)]\n\t\tc1=c1+p\n c2+=1 \n\tend\t\n \nelsif n%j==0\n split=n/j\n\tputs split.to_i\n\tc1=0\n\tc2=0\n\tloop do\n\t\tbreak if c2==split\n\t\tputs s[c1..(c1+j-1)]\n\t\tc1=c1+j\n c2+=1 \n\tend\t\nelsif sc1>0\n\tputs (sc1c+(n-sc1c*p)/j ).to_i\n\tc1=0\n\tc2=0\n\tloop do\n\t\tbreak if c2==sc1c\n\t\tputs s[c1..(c1+p-1)]\n\t\tc1=c1+p\n c2+=1 \n\tend\t\n\tc1=0\n\tc2=0\n\tloop do\n\t\tbreak if c2==(n-sc1c*p)/j\n\t\tputs s[c1..(c1+j-1)]\n\t\tc1=c1+j\n c2+=1 \n\tend\t\nelsif sc2>0\n\tputs (sc2c+(n-sc2c*j)/p ).to_i\n\tc1=0\n\tc2=0\n\tloop do\n\t\tbreak if c2==sc2c\n\t\tputs s[c1..(c1+j-1)]\n\t\tc1=c1+j\n c2+=1 \n\tend\t\n\tc1=0\n\tc2=0\n\tloop do\n\t\tbreak if c2==(n-sc2c*j)/p\n\t\tputs s[c1..(c1+p-1)]\n\t\tc1=c1+p\n c2+=1 \n\tend\t\nelse\n\tputs -1\nend\n"}, {"source_code": "n, p, q = gets.split.map(&:to_i)\ns = gets.chomp\n\nif s.length%p != 0 && s.length%q != 0 && s.length%(p+q) != 0\n puts -1\n exit\nend\n\narr = []\nif s.length%p == 0\n arr = [p]\n puts s.length/p\nelsif s.length%(p+q) == 0\n arr = [p, q]\n puts (s.length/(p+q))*2\nelsif s.length%q == 0\n arr = [q]\n puts s.length/q\nend\n\ncount = 0\nidx = 0\nwhile count != s.length\n puts s[count, arr[idx%arr.length]]\n count += arr[idx%arr.length]\n idx += 1\nend\n"}], "src_uid": "c4da69789d875853beb4f92147825ebf"} {"nl": {"description": "You are given the set of vectors on the plane, each of them starting at the origin. Your task is to find a pair of vectors with the minimal non-oriented angle between them.Non-oriented angle is non-negative value, minimal between clockwise and counterclockwise direction angles. Non-oriented angle is always between 0 and π. For example, opposite directions vectors have angle equals to π.", "input_spec": "First line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of vectors. The i-th of the following n lines contains two integers xi and yi (|x|, |y| ≤ 10 000, x2 + y2 > 0) — the coordinates of the i-th vector. Vectors are numbered from 1 to n in order of appearing in the input. It is guaranteed that no two vectors in the input share the same direction (but they still can have opposite directions).", "output_spec": "Print two integer numbers a and b (a ≠ b) — a pair of indices of vectors with the minimal non-oriented angle. You can print the numbers in any order. If there are many possible answers, print any.", "sample_inputs": ["4\n-1 0\n0 -1\n1 0\n1 1", "6\n-1 0\n0 -1\n1 0\n1 1\n-4 -5\n-4 -6"], "sample_outputs": ["3 4", "6 5"], "notes": null}, "positive_code": [{"source_code": "# c598-C\n\n\ndef compare(v1,v2,v3,v4)\n\ta1 = v1[0]*v2[0]+v1[1]*v2[1]\n\ta2 = v3[0]*v4[0]+v3[1]*v4[1]\n\tb1 = (v1[0]*v1[0]+v1[1]*v1[1])*(v2[0]*v2[0]+v2[1]*v2[1])\n\tb2 = (v3[0]*v3[0]+v3[1]*v3[1])*(v4[0]*v4[0]+v4[1]*v4[1])\n\ta1*a1*b2 <=> a2*a2*b1\n\t# -1 : v1v2 > v3v4\nend\n\n\ndata = []\n\ngets.to_i.times do |i|\n\tp, q = gets.split.map{|x| Integer(x) }\n\tdata << [i, Complex(p, q).arg, p, q]\nend\n\nbest_ij = []\nbest_theta = 7.0\n\ns_data = data.sort_by{|d| d[1] }\n(data.size - 1).times do |i|\n\ttheta = s_data[i+1][1] - s_data[i][1]\n\t#puts \"#{theta} -- #{best_theta}\"\n\tif (best_theta - theta > 9E-13)\n\t\t#puts \"update #{s_data[i][0]},#{s_data[i+1][0]}\"\n\t\tbest_ij = [s_data[i][0], s_data[i+1][0], s_data[i][2,2], s_data[i+1][2,2]]\n\t\tbest_theta = theta\n\telsif (best_theta - theta).abs < 1E-12\n\t\t#ij_cos = (-best_ij[2][1]*best_ij[3][0]+best_ij[2][0]*best_ij[3][1])/Math.sqrt(best_ij[2][0]*best_ij[2][0]+best_ij[2][1]*best_ij[2][1])/Math.sqrt(best_ij[3][0]*best_ij[3][0]+best_ij[3][1]*best_ij[3][1])\n\t\t#current_cos = (-s_data[i][3]*s_data[i+1][2]+s_data[i][2]*s_data[i+1][3])/Math.sqrt(s_data[i][2]*s_data[i][2]+s_data[i][3]*s_data[i][3])/Math.sqrt(s_data[i+1][2]*s_data[i+1][2]+s_data[i+1][3]*s_data[i+1][3])\n\t\tif compare(best_ij[2], best_ij[3], s_data[i][2,2], s_data[i+1][2,2]) == -1\n\t\t\t#puts \"update #{s_data[i][0]},#{s_data[i+1][0]}\"\n\t\t\tbest_ij = [s_data[i][0], s_data[i+1][0], s_data[i][2,2], s_data[i+1][2,2]]\n\t\t\tbest_theta = theta\n\t\tend\n\tend\nend\n\nlast_theta = (s_data.first)[1] - (s_data.last)[1] + 2 * Math::PI\nif (last_theta < best_theta)\n\tputs \"#{(s_data.last)[0]+1} #{(s_data.first)[0]+1}\"\nelse\n\tputs \"#{best_ij[0]+1} #{best_ij[1]+1}\"\nend\n"}], "negative_code": [{"source_code": "# c598-C\n\n\ndata = []\n\ngets.to_i.times do |i|\n\tp, q = gets.split.map{|x| Integer(x) }\n\tdata << [i, Complex(p, q).arg]\nend\n\nbest_ij = []\nbest_theta = 7.0\n\ns_data = data.sort_by{|d| d[1] }\n(data.size - 1).times do |i|\n\ttheta = s_data[i+1][1] - s_data[i][1]\n\tif (best_theta > theta)\n\t\tbest_ij = [s_data[i][0], s_data[i+1][0]]\n\t\tbest_theta = theta\n\tend\nend\n\nlast_theta = (s_data.last)[1] = (s_data.first)[0]\nif (last_theta < best_theta)\n\tputs \"1 #{s_data.size}\"\nelse\n\tputs \"#{best_ij[0]+1} #{best_ij[1]+1}\"\nend\n"}, {"source_code": "# c598-C\n\n\ndef compare(v1,v2,v3,v4)\n\tij_cos = (v1[0]*v2[0]+v1[1]*v2[1])/Math.sqrt(v1[0]*v1[0]+v1[1]*v1[1])/Math.sqrt(v2[0]*v2[0]+v2[1]*v2[1])\n\tcurrent_cos = (v3[0]*v4[0]+v3[1]*v4[1])/Math.sqrt(v3[0]*v3[0]+v3[1]*v3[1])/Math.sqrt(v4[0]*v4[0]+v4[1]*v4[1])\n\t#ij_cos = (-v1[1]*v2[0]+v1[0]*v2[1])/Math.sqrt(v1[0]*v1[0]+v1[1]*v1[1])/Math.sqrt(v2[0]*v2[0]+v2[1]*v2[1])\n\t#current_cos = (-v3[1]*v4[0]+v3[0]*v4[1])/Math.sqrt(v3[0]*v3[0]+v3[1]*v3[1])/Math.sqrt(v4[0]*v4[0]+v4[1]*v4[1])\n\t#puts ij_cos\n\t#puts current_cos\n\t#ij_cos.abs <=> current_cos.abs\n\tij_cos <=> current_cos\nend\n\n\ndata = []\n\ngets.to_i.times do |i|\n\tp, q = gets.split.map{|x| Integer(x) }\n\tdata << [i, Complex(p, q).arg, p, q]\nend\n\nbest_ij = []\nbest_theta = 7.0\n\ns_data = data.sort_by{|d| d[1] }\n(data.size - 1).times do |i|\n\ttheta = s_data[i+1][1] - s_data[i][1]\n\t#puts \"#{theta} -- #{best_theta}\"\n\tif (best_theta - theta > 1E-8)\n\t\t#puts \"update #{s_data[i][0]},#{s_data[i+1][0]}\"\n\t\tbest_ij = [s_data[i][0], s_data[i+1][0], s_data[i][2,2], s_data[i+1][2,2]]\n\t\tbest_theta = theta\n\telsif (best_theta - theta).abs < 1E-7\n\t\t#ij_cos = (-best_ij[2][1]*best_ij[3][0]+best_ij[2][0]*best_ij[3][1])/Math.sqrt(best_ij[2][0]*best_ij[2][0]+best_ij[2][1]*best_ij[2][1])/Math.sqrt(best_ij[3][0]*best_ij[3][0]+best_ij[3][1]*best_ij[3][1])\n\t\t#current_cos = (-s_data[i][3]*s_data[i+1][2]+s_data[i][2]*s_data[i+1][3])/Math.sqrt(s_data[i][2]*s_data[i][2]+s_data[i][3]*s_data[i][3])/Math.sqrt(s_data[i+1][2]*s_data[i+1][2]+s_data[i+1][3]*s_data[i+1][3])\n\t\tif compare(best_ij[2], best_ij[3], s_data[i][2,2], s_data[i+1][2,2]) == -1\n\t\t\t#puts \"update #{s_data[i][0]},#{s_data[i+1][0]}\"\n\t\t\tbest_ij = [s_data[i][0], s_data[i+1][0], s_data[i][2,2], s_data[i+1][2,2]]\n\t\t\tbest_theta = theta\n\t\tend\n\tend\nend\n\nlast_theta = (s_data.first)[1] - (s_data.last)[1] + 2 * Math::PI\nif (last_theta < best_theta)\n\tputs \"#{(s_data.last)[0]+1} #{(s_data.first)[0]+1}\"\nelse\n\tputs \"#{best_ij[0]+1} #{best_ij[1]+1}\"\nend\n"}, {"source_code": "# c598-C\n\n\ndata = []\n\ngets.to_i.times do |i|\n\tp, q = gets.split.map{|x| Integer(x) }\n\tdata << [i, Complex(p, q).arg]\nend\n\nbest_ij = []\nbest_theta = 7.0\n\ns_data = data.sort_by{|d| d[1] }\n(data.size - 1).times do |i|\n\ttheta = s_data[i+1][1] - s_data[i][1]\n\tif (best_theta > theta)\n\t\tbest_ij = [s_data[i][0], s_data[i+1][0]]\n\t\tbest_theta = theta\n\tend\nend\n\nlast_theta = (s_data.last)[1] = (s_data.first)[0]\nif (last_theta < best_theta)\n\tputs \"0 #{s_data.size + 1}\"\nelse\n\tputs \"#{best_ij[0]} #{best_ij[1]}\"\nend\n"}, {"source_code": "# c598-C\n\n\ndata = []\n\ngets.to_i.times do |i|\n\tp, q = gets.split.map{|x| Integer(x) }\n\tdata << [i, Complex(p, q).arg]\nend\n\nbest_ij = []\nbest_theta = 7.0\n\ns_data = data.sort_by{|d| d[1] }\n(data.size - 1).times do |i|\n\ttheta = s_data[i+1][1] - s_data[i][1]\n\tif (best_theta > theta)\n\t\tbest_ij = [s_data[i][0], s_data[i+1][0]]\n\t\tbest_theta = theta\n\tend\nend\n\nlast_theta = (s_data.last)[1] = (s_data.first)[0]\nif (last_theta < best_theta)\n\tputs \"1 #{s_data.size + 1}\"\nelse\n\tputs \"#{best_ij[0]+1} #{best_ij[1]+1}\"\nend\n"}, {"source_code": "# c598-C\n\n\ndata = []\n\ngets.to_i.times do |i|\n\tp, q = gets.split.map{|x| Integer(x) }\n\tdata << [i, Complex(p, q).arg, p, q]\nend\n\nbest_ij = []\nbest_theta = 7.0\n\ns_data = data.sort_by{|d| d[1] }\n(data.size - 1).times do |i|\n\ttheta = s_data[i+1][1] - s_data[i][1]\n\tif (best_theta > theta)\n\t\tbest_ij = [s_data[i][0], s_data[i+1][0], s_data[i][2,2], s_data[i+1][2,2]]\n\t\tbest_theta = theta\n\telsif best_theta == theta\n\t\tij_cos = (-best_ij[2][1]*best_ij[3][0]+best_ij[2][0]*best_ij[3][1])/Math.sqrt(best_ij[2][0]*best_ij[2][0]+best_ij[2][1]*best_ij[2][1])/Math.sqrt(best_ij[3][0]*best_ij[3][0]+best_ij[3][1]*best_ij[3][1])\n\t\tcurrent_cos = (-s_data[i][3]*s_data[i+1][2]+s_data[i][2]*s_data[i+1][3])/Math.sqrt(s_data[i][2]*s_data[i][2]+s_data[i][3]*s_data[i][3])/Math.sqrt(s_data[i+1][2]*s_data[i+1][2]+s_data[i+1][3]*s_data[i+1][3])\n\t\tif ij_cos > current_cos\n\t\t\tbest_ij = [s_data[i][0], s_data[i+1][0], s_data[i][2,2], s_data[i+1][2,2]]\n\t\t\tbest_theta = theta\n\t\tend\n\tend\nend\n\nlast_theta = (s_data.first)[1] - (s_data.last)[1] + 2 * Math::PI\nif (last_theta < best_theta)\n\tputs \"#{(s_data.last)[0]+1} #{(s_data.first)[0]+1}\"\nelse\n\tputs \"#{best_ij[0]+1} #{best_ij[1]+1}\"\nend\n"}, {"source_code": "# c598-C\n\n\ndata = []\n\ngets.to_i.times do |i|\n\tp, q = gets.split.map{|x| Integer(x) }\n\tdata << [i, Complex(p, q).arg]\nend\n\nbest_ij = []\nbest_theta = 7.0\n\ns_data = data.sort_by{|d| d[1] }\n(data.size - 1).times do |i|\n\ttheta = s_data[i+1][1] - s_data[i][1]\n\tif (best_theta >= theta)\n\t\tbest_ij = [s_data[i][0], s_data[i+1][0]]\n\t\tbest_theta = theta\n\tend\nend\n\nlast_theta = (s_data.first)[1] - (s_data.last)[1] + 2 * Math::PI\nif (last_theta < best_theta)\n\tputs \"#{(s_data.last)[0]+1} #{(s_data.first)[0]+1}\"\nelse\n\tputs \"#{best_ij[0]+1} #{best_ij[1]+1}\"\nend\n"}, {"source_code": "# c598-C\n\n\ndata = []\n\ngets.to_i.times do |i|\n\tp, q = gets.split.map{|x| Integer(x) }\n\tdata << [i, Complex(p, q).arg]\nend\n\nbest_ij = []\nbest_theta = 7.0\n\ns_data = data.sort_by{|d| d[1] }\n(data.size - 1).times do |i|\n\ttheta = s_data[i+1][1] - s_data[i][1]\n\tif (best_theta > theta)\n\t\tbest_ij = [s_data[i][0], s_data[i+1][0]]\n\t\tbest_theta = theta\n\tend\nend\n\nlast_theta = (s_data.last)[1] = (s_data.first)[1]\nif (last_theta < best_theta)\n\tputs \"#{(s_data.last)[0]} #{(s_data.first)[0]}\"\nelse\n\tputs \"#{best_ij[0]+1} #{best_ij[1]+1}\"\nend\n"}, {"source_code": "# c598-C\n\n\ndata = []\n\ngets.to_i.times do |i|\n\tp, q = gets.split.map{|x| Integer(x) }\n\tdata << [i, Complex(p, q).arg]\nend\n\nbest_ij = []\nbest_theta = 7.0\n\ns_data = data.sort_by{|d| d[1] }\n(data.size - 1).times do |i|\n\ttheta = s_data[i+1][1] - s_data[i][1]\n\tif (best_theta > theta)\n\t\tbest_ij = [s_data[i][0], s_data[i+1][0]]\n\t\tbest_theta = theta\n\tend\nend\n\nlast_theta = (s_data.first)[1] - (s_data.last)[1] + 2 * Math::PI\nif (last_theta < best_theta)\n\tputs \"#{(s_data.last)[0]+1} #{(s_data.first)[0]+1}\"\nelse\n\tputs \"#{best_ij[0]+1} #{best_ij[1]+1}\"\nend\n"}, {"source_code": "# c598-C\n\n\ndef compare(v1,v2,v3,v4)\n\t#ij_cos = (v1[0]*v2[0]+v1[1]*v2[1])/Math.sqrt(v1[0]*v1[0]+v1[1]*v1[1])/Math.sqrt(v2[0]*v2[0]+v2[1]*v2[1])\n\t#current_cos = (v3[0]*v4[0]+v3[1]*v4[1])/Math.sqrt(v3[0]*v3[0]+v3[1]*v3[1])/Math.sqrt(v4[0]*v4[0]+v4[1]*v4[1])\n\tij_cos = (-v1[1]*v2[0]+v1[0]*v2[1])/Math.sqrt(v1[0]*v1[0]+v1[1]*v1[1])/Math.sqrt(v2[0]*v2[0]+v2[1]*v2[1])\n\tcurrent_cos = (-v3[1]*v4[0]+v3[0]*v4[1])/Math.sqrt(v3[0]*v3[0]+v3[1]*v3[1])/Math.sqrt(v4[0]*v4[0]+v4[1]*v4[1])\n\t#puts ij_cos\n\t#puts current_cos\n\tcurrent_cos.abs <=> ij_cos.abs\n\t#ij_cos <=> current_cos\nend\n\n\ndata = []\n\ngets.to_i.times do |i|\n\tp, q = gets.split.map{|x| Integer(x) }\n\tdata << [i, Complex(p, q).arg, p, q]\nend\n\nbest_ij = []\nbest_theta = 7.0\n\ns_data = data.sort_by{|d| d[1] }\n(data.size - 1).times do |i|\n\ttheta = s_data[i+1][1] - s_data[i][1]\n\t#puts \"#{theta} -- #{best_theta}\"\n\tif (best_theta - theta > 9E-13)\n\t\t#puts \"update #{s_data[i][0]},#{s_data[i+1][0]}\"\n\t\tbest_ij = [s_data[i][0], s_data[i+1][0], s_data[i][2,2], s_data[i+1][2,2]]\n\t\tbest_theta = theta\n\telsif (best_theta - theta).abs < 1E-12\n\t\t#ij_cos = (-best_ij[2][1]*best_ij[3][0]+best_ij[2][0]*best_ij[3][1])/Math.sqrt(best_ij[2][0]*best_ij[2][0]+best_ij[2][1]*best_ij[2][1])/Math.sqrt(best_ij[3][0]*best_ij[3][0]+best_ij[3][1]*best_ij[3][1])\n\t\t#current_cos = (-s_data[i][3]*s_data[i+1][2]+s_data[i][2]*s_data[i+1][3])/Math.sqrt(s_data[i][2]*s_data[i][2]+s_data[i][3]*s_data[i][3])/Math.sqrt(s_data[i+1][2]*s_data[i+1][2]+s_data[i+1][3]*s_data[i+1][3])\n\t\tif compare(best_ij[2], best_ij[3], s_data[i][2,2], s_data[i+1][2,2]) == -1\n\t\t\t#puts \"update #{s_data[i][0]},#{s_data[i+1][0]}\"\n\t\t\tbest_ij = [s_data[i][0], s_data[i+1][0], s_data[i][2,2], s_data[i+1][2,2]]\n\t\t\tbest_theta = theta\n\t\tend\n\tend\nend\n\nlast_theta = (s_data.first)[1] - (s_data.last)[1] + 2 * Math::PI\nif (last_theta < best_theta)\n\tputs \"#{(s_data.last)[0]+1} #{(s_data.first)[0]+1}\"\nelse\n\tputs \"#{best_ij[0]+1} #{best_ij[1]+1}\"\nend\n"}], "src_uid": "e7ffe7a54e2403805bde98d98fa0be3a"} {"nl": {"description": "You are given a set of points $$$x_1$$$, $$$x_2$$$, ..., $$$x_n$$$ on the number line.Two points $$$i$$$ and $$$j$$$ can be matched with each other if the following conditions hold: neither $$$i$$$ nor $$$j$$$ is matched with any other point; $$$|x_i - x_j| \\ge z$$$. What is the maximum number of pairs of points you can match with each other?", "input_spec": "The first line contains two integers $$$n$$$ and $$$z$$$ ($$$2 \\le n \\le 2 \\cdot 10^5$$$, $$$1 \\le z \\le 10^9$$$) — the number of points and the constraint on the distance between matched points, respectively. The second line contains $$$n$$$ integers $$$x_1$$$, $$$x_2$$$, ..., $$$x_n$$$ ($$$1 \\le x_i \\le 10^9$$$).", "output_spec": "Print one integer — the maximum number of pairs of points you can match with each other.", "sample_inputs": ["4 2\n1 3 3 7", "5 5\n10 9 5 8 7"], "sample_outputs": ["2", "1"], "notes": "NoteIn the first example, you may match point $$$1$$$ with point $$$2$$$ ($$$|3 - 1| \\ge 2$$$), and point $$$3$$$ with point $$$4$$$ ($$$|7 - 3| \\ge 2$$$).In the second example, you may match point $$$1$$$ with point $$$3$$$ ($$$|5 - 10| \\ge 5$$$)."}, "positive_code": [{"source_code": "n,z = gets.split.map(&:to_i)\nx = gets.split.map(&:to_i).sort\nmatch = []\nans = 0\n(n/2).times do |i|\n idx = (0..n-1).bsearch{|j| x[j] >= x[i]+z}\n match[i] = idx.nil? ? -1 : n-idx+i\n #p [i,idx]\nend\n\n#p match\nmin = n/2\n(n/2).times do |i|\n break if (i+1) > [match[i],min].min\n ans += 1\n min = match[i] if min > match[i]\nend\np ans\n"}, {"source_code": "n,z = gets.split.map(&:to_i)\nx = gets.split.map(&:to_i).sort\nmatch = []\nans = 0\n(n/2).times do |i|\n idx = (0..n-1).bsearch{|j| x[j] >= x[i]+z}\n match[i] = idx.nil? ? -1 : n-idx\nend\n\nmatchable = n/2\n(n/2).times do |i|\n matchable = match[i]+i if matchable > match[i]+i\n break if (i+1) > matchable\n ans += 1\nend\np ans"}, {"source_code": "N, Z = gets.split.map(&:to_i)\narr = gets.split.map(&:to_i)\narr.sort!\np1, p2 = 0, N / 2\nanswer = 0\nwhile p2 < N\n\tbreak if p1 == N / 2\n\tif arr[p2] - arr[p1] >= Z\n\t\tanswer += 1\n\t\tp1 += 1\n\t\tp2 += 1\n\telse\n\t\tp2 += 1\n\tend\nend\nputs answer\n"}], "negative_code": [{"source_code": "n,z = gets.split.map(&:to_i)\nx = gets.split.map(&:to_i).sort\nused = [false]*n\nans = 0\nn.times do |i|\n if !used[i]\n idx = (0..n-1).bsearch{|j| x[j] >= x[i]+z}\n if !idx.nil?\n used[i] = true\n used[idx] = true\n ans += 1\n end\n end\nend\n#p used\np ans\n"}, {"source_code": "n,z = gets.split.map(&:to_i)\nx = gets.split.map(&:to_i).sort\nmatch = []\nans = 0\n(n/2).times do |i|\n idx = (0..n-1).bsearch{|j| x[j] >= x[i]+z}\n match[i] = idx.nil? ? -1 : n-idx+i\nend\n\n#p match\n(n/2).times do |i|\n break if (i+1) > match[i]\n ans += 1\nend\np ans\n"}, {"source_code": "n,z = gets.split.map(&:to_i)\nx = gets.split.map(&:to_i).sort\nmatch = []\nans = 0\n(n/2).times do |i|\n idx = (0..n-1).bsearch{|j| x[j] >= x[i]+z}\n match[i] = idx.nil? ? -1 : n-idx\nend\n\nans = 0\ntmp = n/2\n(n/2).times do |i|\n c = tmp < match[i]+i ? tmp : match[i]+i\n break if (i+1) > c || match[i] < 0\n ans += 1\nend\np ans\n"}, {"source_code": "n,z = gets.split.map(&:to_i)\nx = gets.split.map(&:to_i).sort\nmatch = []\nans = 0\n(n/2).times do |i|\n idx = (0..n-1).bsearch{|j| x[j] >= x[i]+z}\n match[i] = idx.nil? ? -1 : n-idx\nend\n\nans = n/2\n(n/2).times do |i|\n t = ans < match[i]+i ? ans : match[i]+i\n break if (i+1) > t\n ans = t\nend\np ans\n"}, {"source_code": "N, Z = gets.split.map(&:to_i)\narr = gets.split.map(&:to_i).sort\np1, p2 = 0, 1\nanswer = 0\nwhile p2 < N\n\tif arr[p1]\n\t\tif arr[p2]\n\t\t\tif arr[p2] - arr[p1] >= Z\n\t\t\t\tanswer += 1\n\t\t\t\tarr[p2] = arr[p1] = nil\n\t\t\t\tp1 += 1\n\t\t\t\tp2 += 1\n\t\t\telse\n\t\t\t\tp2 += 1\n\t\t\tend\n\t\telse\n\t\t\tp2 += 1\n\t\tend\n\telse\n\t\tp1 += 1\n\tend\nend\nputs answer\n"}, {"source_code": "N, Z = gets.split.map(&:to_i)\narr = gets.split.map(&:to_i)\narr.sort!\np1, p2 = 0, N / 2\nanswer = 0\nwhile p2 < N\n\tif arr[p2] - arr[p1] >= Z\n\t\tanswer += 1\n\t\tp1 += 1\n\t\tp2 += 1\n\telse\n\t\tp2 += 1\n\tend\nend\nputs answer\n"}, {"source_code": "N, Z = gets.split.map(&:to_i)\narr = gets.split.map(&:to_i).sort\np1, p2 = 0, 1\nanswer = 0\nwhile p2 < N\n\tif arr[p2]\n\t\tif arr[p1]\n\t\t\tif arr[p2] - arr[p1] >= Z\n\t\t\t\tanswer += 1\n\t\t\t\tarr[p2] = arr[p1] = nil\n\t\t\t\tp1 += 1\n\t\t\t\tp2 += 1\n\t\t\telse\n\t\t\t\tp2 += 1\n\t\t\tend\n\t\telse\n\t\t\tp1 += 1\n\t\tend\n\telse\n\t\tp2 += 1\n\tend\nend\nputs answer\n"}], "src_uid": "b063660572a78f25525656332a1782a8"} {"nl": {"description": "Recently Luba bought a very interesting book. She knows that it will take t seconds to read the book. Luba wants to finish reading as fast as she can.But she has some work to do in each of n next days. The number of seconds that Luba has to spend working during i-th day is ai. If some free time remains, she can spend it on reading.Help Luba to determine the minimum number of day when she finishes reading.It is guaranteed that the answer doesn't exceed n.Remember that there are 86400 seconds in a day.", "input_spec": "The first line contains two integers n and t (1 ≤ n ≤ 100, 1 ≤ t ≤ 106) — the number of days and the time required to read the book. The second line contains n integers ai (0 ≤ ai ≤ 86400) — the time Luba has to spend on her work during i-th day.", "output_spec": "Print the minimum day Luba can finish reading the book. It is guaranteed that answer doesn't exceed n.", "sample_inputs": ["2 2\n86400 86398", "2 86400\n0 86400"], "sample_outputs": ["2", "1"], "notes": null}, "positive_code": [{"source_code": "n,t,*a=$<.read.split.map &:to_i\np (1..n).find{|i|0>=t-=86400-a[i-1]}"}, {"source_code": "n, t = gets.chomp.split(' ').map(&:to_i)\narr = gets.chomp.split(' ').map(&:to_i)\n\nSECONDS = 86400\nnum = 0\nresult = 0\n\nwhile t > 0\n\tt -= (SECONDS - arr[result])\n\tresult += 1\nend\n \nputs result\n\n"}, {"source_code": "n, t = gets.strip.split.map(&:to_i)\na = gets.strip.split.map(&:to_i)\nans = 0\nn.times do |i|\n free = 86400 - a[i]\n t -= free\n ans += 1\n break if t <= 0\nend\nputs ans\n"}, {"source_code": "n,t=gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\ncur = 0\nn.times do |i|\n cur += 86400 - a[i]\n if cur >= t\n puts i + 1\n break\n end\nend"}, {"source_code": "in_str = gets\nn, allt = in_str.split(' ')\nn, allt = n.to_i, allt.to_i\nin_str = gets\ntime_arr = in_str.split(' ').map { |element| element.to_i }\nt = 0\ntime_arr.each_with_index do |elem, i|\n t += 86400 - elem\n if t >= allt\n puts i + 1\n break\n end\nend"}, {"source_code": "n,t,*a=$<.read.split.map &:to_i\np (1..n).find{|i|0>=t-=86400-a[i-1]}"}, {"source_code": "n, t= gets.split(\" \").map(&:to_i)\nday = 1\na = gets.split(\" \").map(&:to_i)\na.each do |work_time|\n t = t-86400+work_time\n day+=1 if t>0\nend\nprint(day)\n\n"}], "negative_code": [{"source_code": "n,t=gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\ncur = 0\nn.times do |i|\n cur += 86400 - a[i]\n if cur >= t\n puts i + 1\n end\nend"}], "src_uid": "8e423e4bec2d113612a4dc445c4b86a9"} {"nl": {"description": "A new agent called Killjoy invented a virus COVID-2069 that infects accounts on Codeforces. Each account has a rating, described by an integer (it can possibly be negative or very large).Killjoy's account is already infected and has a rating equal to $$$x$$$. Its rating is constant. There are $$$n$$$ accounts except hers, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th account's initial rating is $$$a_i$$$. Any infected account (initially the only infected account is Killjoy's) instantly infects any uninfected account if their ratings are equal. This can happen at the beginning (before any rating changes) and after each contest. If an account is infected, it can not be healed.Contests are regularly held on Codeforces. In each contest, any of these $$$n$$$ accounts (including infected ones) can participate. Killjoy can't participate. After each contest ratings are changed this way: each participant's rating is changed by an integer, but the sum of all changes must be equal to zero. New ratings can be any integer.Find out the minimal number of contests needed to infect all accounts. You can choose which accounts will participate in each contest and how the ratings will change.It can be proven that all accounts can be infected in some finite number of contests.", "input_spec": "The first line contains a single integer $$$t$$$ $$$(1 \\le t \\le 100)$$$ — the number of test cases. The next $$$2t$$$ lines contain the descriptions of all test cases. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$2 \\le n \\le 10^3$$$, $$$-4000 \\le x \\le 4000$$$) — the number of accounts on Codeforces and the rating of Killjoy's account. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ $$$(-4000 \\le a_i \\le 4000)$$$ — the ratings of other accounts.", "output_spec": "For each test case output the minimal number of contests needed to infect all accounts.", "sample_inputs": ["3\n2 69\n68 70\n6 4\n4 4 4 4 4 4\n9 38\n-21 83 50 -59 -77 15 -71 -78 20"], "sample_outputs": ["1\n0\n2"], "notes": "NoteIn the first test case it's possible to make all ratings equal to $$$69$$$. First account's rating will increase by $$$1$$$, and second account's rating will decrease by $$$1$$$, so the sum of all changes will be equal to zero.In the second test case all accounts will be instantly infected, because all ratings (including Killjoy's account's rating) are equal to $$$4$$$."}, "positive_code": [{"source_code": "T = gets.to_i\n\nT.times do\n n, x = gets.split.map(&:to_i)\n a = gets.split.map(&:to_i)\n\n if a.all?(x)\n puts 0\n else\n cnt = a.count(x)\n\n if cnt >= 1\n puts 1\n else\n diff = 0\n\n a.each do |y|\n diff += x - y\n end\n\n if diff == 0\n puts 1\n else\n puts 2\n end\n end\n end\nend\n"}], "negative_code": [{"source_code": "T = gets.to_i\n\nT.times do\n n, x = gets.split.map(&:to_i)\n a = gets.split.map(&:to_i)\n\n if a.all?(x)\n puts 0\n else\n cnt = a.count(x)\n\n if cnt >= 2\n puts 1\n elsif cnt >= 1\n ok = 10_000\n ng = -10_000\n\n f =-> (y) {\n diff = 0\n\n a.each do |v|\n diff += y - v\n end\n\n 0 <= diff\n }\n\n while (ok - ng).abs > 1\n y = (ok + ng) / 2\n\n if f[y]\n ok = y\n else\n ng = y\n end\n end\n\n diff = 0\n\n a.each do |v|\n diff += ok - v\n end\n\n if diff == 0 \n puts 1\n else\n puts 2\n end\n else\n diff = 0\n\n a.each do |y|\n diff += x - y\n end\n\n if diff == 0\n puts 1\n else\n puts 2\n end\n end\n end\nend\n"}, {"source_code": "T = gets.to_i\n\nT.times do\n n, x = gets.split.map(&:to_i)\n a = gets.split.map(&:to_i)\n\n if a.all?(x)\n puts 0\n else\n diff = 0\n\n a.each do |y|\n diff += y - x\n end\n\n if diff == 0\n puts 1\n else\n puts 2\n end\n end\nend\n"}, {"source_code": "T = gets.to_i\n\nT.times do\n n, x = gets.split.map(&:to_i)\n a = gets.split.map(&:to_i)\n\n if a.all?(x)\n puts 0\n else\n cnt = a.count(x)\n\n if cnt >= 2\n puts 1\n elsif cnt >= 1\n ok = 10 ** 10\n ng = -1 * (10 ** 10)\n\n f =-> (y) {\n diff = 0\n\n a.each do |v|\n diff += y - v\n end\n\n 0 <= diff\n }\n\n while (ok - ng).abs > 1\n y = (ok + ng) / 2\n\n if f[y]\n ok = y\n else\n ng = y\n end\n end\n\n diff = 0\n\n a.each do |v|\n diff += ok - v\n end\n\n if diff == 0 \n puts 1\n else\n puts 2\n end\n else\n diff = 0\n\n a.each do |y|\n diff += x - y\n end\n\n if diff == 0\n puts 1\n else\n puts 2\n end\n end\n end\nend\n"}], "src_uid": "475a24d36eab99ae4f78815ccdc5da91"} {"nl": {"description": "The All-Berland Team Programming Contest will take place very soon. This year, teams of four are allowed to participate.There are $$$a$$$ programmers and $$$b$$$ mathematicians at Berland State University. How many maximum teams can be made if: each team must consist of exactly $$$4$$$ students, teams of $$$4$$$ mathematicians or $$$4$$$ programmers are unlikely to perform well, so the decision was made not to compose such teams. Thus, each team must have at least one programmer and at least one mathematician.Print the required maximum number of teams. Each person can be a member of no more than one team.", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) —the number of test cases. This is followed by descriptions of $$$t$$$ sets, one per line. Each set is given by two integers $$$a$$$ and $$$b$$$ ($$$0 \\le a,b \\le 10^9$$$).", "output_spec": "Print $$$t$$$ lines. Each line must contain the answer to the corresponding set of input data — the required maximum number of teams.", "sample_inputs": ["6\n5 5\n10 1\n2 3\n0 0\n17 2\n1000000000 1000000000"], "sample_outputs": ["2\n1\n1\n0\n2\n500000000"], "notes": "NoteIn the first test case of the example, two teams can be composed. One way to compose two teams is to compose two teams of $$$2$$$ programmers and $$$2$$$ mathematicians.In the second test case of the example, only one team can be composed: $$$3$$$ programmers and $$$1$$$ mathematician in the team."}, "positive_code": [{"source_code": "t = gets.to_i\r\nans = []\r\nt.times do\r\n a,b = gets.split.map(&:to_i).sort\r\n cnt = [a,(b-a)/2].min\r\n a -= cnt\r\n cnt += a / 2\r\n ans << cnt\r\nend\r\nputs ans"}], "negative_code": [], "src_uid": "8a1ceac1440f7cb406f12d9fc2ca0e20"} {"nl": {"description": "The elections in which three candidates participated have recently ended. The first candidate received $$$a$$$ votes, the second one received $$$b$$$ votes, the third one received $$$c$$$ votes. For each candidate, solve the following problem: how many votes should be added to this candidate so that he wins the election (i.e. the number of votes for this candidate was strictly greater than the number of votes for any other candidate)?Please note that for each candidate it is necessary to solve this problem independently, i.e. the added votes for any candidate do not affect the calculations when getting the answer for the other two candidates.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of one line containing three integers $$$a$$$, $$$b$$$, and $$$c$$$ ($$$0 \\le a,b,c \\le 10^9$$$).", "output_spec": "For each test case, output in a separate line three integers $$$A$$$, $$$B$$$, and $$$C$$$ ($$$A, B, C \\ge 0$$$) separated by spaces — the answers to the problem for the first, second, and third candidate, respectively.", "sample_inputs": ["5\n0 0 0\n10 75 15\n13 13 17\n1000 0 0\n0 1000000000 0"], "sample_outputs": ["1 1 1\n66 0 61\n5 5 0\n0 1001 1001\n1000000001 0 1000000001"], "notes": null}, "positive_code": [{"source_code": "t = gets.chomp.to_i\nt.times {\n\ta, b, c = gets.split.map(&:to_i)\n\tif (a == b) && (c == a)\n\t\tputs \"1 1 1\"\n\t\tnext\n\tend\n\taa = [a, [b, c].max + 1].max - a \n\tbb = [b, [a, c].max + 1].max - b\n\tcc = [c, [a, b].max + 1].max - c\n\tputs \"#{aa} #{bb} #{cc}\"\n}"}, {"source_code": "gets.to_i.times do\r\n arr = gets.split.each_with_index.map{ |c, i| c.to_i }\r\n mx = arr.max\r\n c = arr.count(mx)\r\n puts arr.map{|e| e != mx ? mx - e + 1 : c > 1 ? 1 : 0}.join ' '\r\n\r\nend\r\n"}, {"source_code": "class Pair\r\n attr_accessor :first, :second\r\n include Comparable\r\n\r\n def initialize(first, second)\r\n @first = first\r\n @second = second\r\n end\r\n\r\n def <=>(an_other)\r\n @first <=> an_other.first\r\n end\r\nend\r\ngets.to_i.times do\r\n arr = gets.split.each_with_index.map{ |c, i| Pair.new(c.to_i, i) }\r\n arr = arr.sort()\r\n ans = Array.new;\r\n arr.length.times do |idx|\r\n if idx == arr.length - 1\r\n if arr.length >= 2 and arr[arr.length - 1].first == arr[arr.length - 2].first\r\n ans[ arr[arr.length - 1].second ] = 1;\r\n else\r\n ans[ arr[arr.length - 1].second ] = 0;\r\n end\r\n else\r\n ans[ arr[idx].second ] = arr[arr.length-1].first - arr[idx].first + 1;\r\n end\r\n end\r\n\r\n arr.length.times do |idx|\r\n if idx == arr.length - 1\r\n puts(ans[idx])\r\n else\r\n print(ans[idx])\r\n print(\" \")\r\n end\r\n end\r\n\r\n\r\nend\r\n"}, {"source_code": "gets.to_i.times do\r\n votes = gets.split.map(&:to_i)\r\n m = votes.max\r\n c = votes.count(m)\r\n puts votes.map{|v| v != m ? m - v + 1 : c > 1 ? 1 : 0}.join ' '\r\nend"}, {"source_code": "inputs = STDIN.read.strip.split(\"\\n\")\r\ninputs.shift\r\n\r\ninputs.each do |inp|\r\n cans = inp.split(' ').map(&:to_i)\r\n\r\n r = []\r\n cans.each_with_index do |n, i|\r\n other = cans.reject.with_index{|_, j| j == i }\r\n max = other.max\r\n\r\n if n > max\r\n r << 0\r\n else\r\n r << max - n + 1\r\n end\r\n end\r\n\r\n puts r.join(' ')\r\nend"}, {"source_code": "t = gets().to_i()\n\ndef bar(arr)\n\n\tsolve = []\n\n\n\tmax = arr.max\n\tmax_counter = 0\n\n\tfor i in 0..arr.length - 1\n\t\tif (arr[i] == max)\n\t\t\tmax_counter += 1\n\t\tend\n\tend\n\n\tfor i in 0..arr.length - 1\n\t\ttmp = -1\n\t\tif (arr[i] == max)\n\t\t\tif (max_counter == 1)\n\t\t\t\ttmp = 0\n\t\t\telse\n\t\t\t\ttmp = 1\n\t\t\tend\n\t\telse\n\t\t\ttmp = max - arr[i] + 1\n\t\tend\t\n\n\t\tsolve << tmp\n\tend\n\tputs solve.join(\" \")\nend\n\n\nfor i in 1..t\n\tarr = gets.chomp.split(\" \").map{|item| item.to_i}\n\t\n\tbar(arr)\n\nend"}, {"source_code": "gets.to_i.times do\r\n votes = gets.split.map(&:to_i)\r\n m = votes.max\r\n c = votes.count(m)\r\n puts votes.map{|v| v != m ? m - v + 1 : c > 1 ? 1 : 0}.join ' '\r\nend"}, {"source_code": "n = gets.chomp.to_i\r\n\r\nn.times do\r\n votes = gets.chomp.split(' ').map(&:to_i)\r\n\r\n a = [[votes[1], votes[2]].max - votes[0] + 1, 0].max\r\n b = [[votes[0], votes[2]].max - votes[1] + 1, 0].max\r\n c = [[votes[0], votes[1]].max - votes[2] + 1, 0].max\r\n\r\n puts \"#{a} #{b} #{c}\"\r\nend"}, {"source_code": "def f(x, y, z)\r\n w = [y, z].max - x + 1\r\n w > 0 ? w : 0\r\nend\r\n\r\ngets.to_i.times do\r\n\r\na, b, c = gets.split.map!(&:to_i)\r\nputs \"#{f(a,b,c)} #{f(b,c,a)} #{f(c,a,b)}\"\r\n \r\nend\r\n"}], "negative_code": [{"source_code": "t = gets.chomp.to_i\nt.times {\n\ta, b, c = gets.split.map(&:to_i)\n\tif (a == b) && (c == a)\n\t\tputs \"#{a+1} #{b+1} #{c+1}\"\n\t\tnext\n\tend\n\taa = [a, [b, c].max + 1].max - a \n\tbb = [b, [a, c].max + 1].max - b\n\tcc = [c, [a, b].max + 1].max - c\n\tputs \"#{aa} #{bb} #{cc}\"\n}"}, {"source_code": "t = gets.chomp.to_i\nt.times {\n\ta, b, c = gets.split.map(&:to_i)\n\tif (a == b) && (c == b) && (c == a)\n\t\tputs \"#{a+1} #{b+1} #{c+1}\"\n\t\tnext\n\tend\n\tma = [a, b, c].max\n\tif ma - a == 0\n\t\ta = 0\n\telse \n\t\ta = ma - a + 1\n\tend\n\tif ma - b == 0\n\t\tb = 0\n\telse \n\t\tb = ma - b + 1\n\tend\n\tif ma - c == 0\n\t\tc = 0\n\telse \n\t\tc = ma - c + 1\n\tend\n\tputs \"#{a} #{b} #{c}\"\n\t# p [a, b, c].join' '\n}"}, {"source_code": "t = gets.chomp.to_i\nt.times {\n\ta, b, c = gets.split.map(&:to_i)\n\tif (a == b) && (c == b) && (c == a)\n\t\tputs \"#{a+1} #{b+1} #{c+1}\"\n\t\tnext\n\tend\n\tma = [a, b, c].max\n\tif ma - a == 0\n\t\ta = 0\n\telse \n\t\ta = ma - a + 1\n\tend\n\tif ma - b == 0\n\t\tb = 0\n\telse \n\t\tb = ma - b + 1\n\tend\n\tif ma - c == 0\n\t\tc = 0\n\telse \n\t\tc = ma - c + 1\n\tend\n\t# puts \"#{a} #{b} #{c}\"\n\tp [a, b, c].join' '\n}"}, {"source_code": "t = gets.chomp.to_i\nt.times {\n\ta, b, c = gets.split.map(&:to_i)\n\tif (a == b) && (c == b) && (c == a)\n\t\tputs a+1, a+1, a+1\n\t\tnext\n\tend\n\tma = [a, b, c].max\n\tif ma - a == 0\n\t\ta = 0\n\telse \n\t\ta = ma - a + 1\n\tend\n\tif ma - b == 0\n\t\tb = 0\n\telse \n\t\tb = ma - b + 1\n\tend\n\tif ma - c == 0\n\t\tc = 0\n\telse \n\t\tc = ma - c + 1\n\tend\n\tputs \"#{a} #{b} #{c}\"\n}"}, {"source_code": "inputs = STDIN.read.strip.split(\"\\n\")\r\ninputs.shift\r\n\r\ninputs.each do |inp|\r\n cans = inp.split(' ').map(&:to_i)\r\n\r\n r = []\r\n max = cans.max\r\n min = cans.min\r\n cans.each do |n|\r\n r << max - n + (max != n || max == min ? 1 : 0)\r\n end\r\n\r\n puts r.join(' ')\r\nend"}, {"source_code": "t = gets().to_i()\n\ndef bar(arr)\n\n\tmax = arr.max\n\n\t\n\tsolve = []\n\n\n\tfor i in 0..arr.length - 1\n\t\ttmp = -1\n\t\tif (arr[i] == max)\n\t\t\ttmp = 0\n\t\telse\n\t\t\ttmp = max - arr[i] + 1\n\t\tend\t\n\n\t\tsolve << tmp\n\tend\n\tputs solve.join(\" \")\nend\n\n\nfor i in 1..t\n\tarr = gets.chomp.split(\" \").map{|item| item.to_i}\n\t\n\tbar(arr)\n\nend"}], "src_uid": "f80dea1e07dba355bfbefa4ff65ff45a"} {"nl": {"description": "You are given two $$$n \\times m$$$ matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are strictly increasing. A matrix is increasing if all rows are strictly increasing and all columns are strictly increasing. For example, the matrix $$$\\begin{bmatrix} 9&10&11\\\\ 11&12&14\\\\ \\end{bmatrix}$$$ is increasing because each individual row and column is strictly increasing. On the other hand, the matrix $$$\\begin{bmatrix} 1&1\\\\ 2&3\\\\ \\end{bmatrix}$$$ is not increasing because the first row is not strictly increasing.Let a position in the $$$i$$$-th row (from top) and $$$j$$$-th column (from left) in a matrix be denoted as $$$(i, j)$$$. In one operation, you can choose any two numbers $$$i$$$ and $$$j$$$ and swap the number located in $$$(i, j)$$$ in the first matrix with the number in $$$(i, j)$$$ in the second matrix. In other words, you can swap two numbers in different matrices if they are located in the corresponding positions.You would like to make both matrices increasing by performing some number of operations (possibly none). Determine if it is possible to do this. If it is, print \"Possible\", otherwise, print \"Impossible\".", "input_spec": "The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\leq n,m \\leq 50$$$) — the dimensions of each matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i1}, a_{i2}, \\ldots, a_{im}$$$ ($$$1 \\leq a_{ij} \\leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the first matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$b_{i1}, b_{i2}, \\ldots, b_{im}$$$ ($$$1 \\leq b_{ij} \\leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the second matrix.", "output_spec": "Print a string \"Impossible\" or \"Possible\".", "sample_inputs": ["2 2\n2 10\n11 5\n9 4\n3 12", "2 3\n2 4 5\n4 5 6\n3 6 7\n8 10 11", "3 2\n1 3\n2 4\n5 10\n3 1\n3 6\n4 8"], "sample_outputs": ["Possible", "Possible", "Impossible"], "notes": "NoteThe first example, we can do an operation on the top left and bottom right cells of the matrices. The resulting matrices will be $$$\\begin{bmatrix} 9&10\\\\ 11&12\\\\ \\end{bmatrix}$$$ and $$$\\begin{bmatrix} 2&4\\\\ 3&5\\\\ \\end{bmatrix}$$$.In the second example, we don't need to do any operations.In the third example, no matter what we swap, we can't fix the first row to be strictly increasing in both matrices. "}, "positive_code": [{"source_code": "n,m = gets.split.map(&:to_i)\nfirst,second = [],[]\nn.times do\n first << gets.split.map(&:to_i)\nend\nn.times do \n second << gets.split.map(&:to_i)\nend\nn.times do |i|\n m.times do |j|\n if first[i][j] > second[i][j]\n first[i][j],second[i][j] = second[i][j],first[i][j]\n end\n end\nend\nflg = true\nn.times do |i|\n m.times do |j|\n if i < n-1 && (first[i][j] >= first[i+1][j] || second[i][j] >= second[i+1][j])\n flg = false\n break\n end \n if j < m-1 && (first[i][j] >= first[i][j+1] || second[i][j] >= second[i][j+1])\n flg = false\n break\n end\n end\nend\n\nputs flg ? \"Possible\" : \"Impossible\""}], "negative_code": [], "src_uid": "53e061fe3fbe3695d69854c621ab6037"} {"nl": {"description": "John Doe started thinking about graphs. After some thought he decided that he wants to paint an undirected graph, containing exactly k cycles of length 3. A cycle of length 3 is an unordered group of three distinct graph vertices a, b and c, such that each pair of them is connected by a graph edge. John has been painting for long, but he has not been a success. Help him find such graph. Note that the number of vertices there shouldn't exceed 100, or else John will have problems painting it.", "input_spec": "A single line contains an integer k (1 ≤ k ≤ 105) — the number of cycles of length 3 in the required graph.", "output_spec": "In the first line print integer n (3 ≤ n ≤ 100) — the number of vertices in the found graph. In each of next n lines print n characters \"0\" and \"1\": the i-th character of the j-th line should equal \"0\", if vertices i and j do not have an edge between them, otherwise it should equal \"1\". Note that as the required graph is undirected, the i-th character of the j-th line must equal the j-th character of the i-th line. The graph shouldn't contain self-loops, so the i-th character of the i-th line must equal \"0\" for all i.", "sample_inputs": ["1", "10"], "sample_outputs": ["3\n011\n101\n110", "5\n01111\n10111\n11011\n11101\n11110"], "notes": null}, "positive_code": [{"source_code": "def c(n, m)\n c = 1\n m.times do |i|\n c = c * (n - i) / (i + 1)\n end\n c\nend\n\n# c(n, 3) - c(n - 1, 3) = c(n - 1, 2) !!\n# c(n, 2) - c(n - 1, 2) = c(n - 1, 1) = n - 1 !!\n\nm = gets.to_i\nk = 3\nk += 1 while c(k + 1, 3) <= m\nm -= c(k, 3)\n\na = []\nwhile m > 0\n b = 2\n b += 1 while c(b + 1, 2) <= m\n m -= c(b, 2)\n a << b\nend\n\nn = k + a.size\ne = Array.new(n){'0' * n}\nk.times do |i|\n k.times do |j|\n e[i][j] = '1' if i != j\n end\nend\na.each_with_index do |c, j|\n c.times do |i|\n e[i][k + j] = '1'\n e[k + j][i] = '1'\n end\nend\np n\nputs e * \"\\n\"\n"}, {"source_code": "n=gets.to_i\na=Array.new(101){|el| el=Array.new(101){|elem| elem=0}}\nc,y=0,0\nfor i in 0..101 do\ny=0\n\twhile yn\n\t\ta[i][y],a[y][i]=1,1\n\t\tn-=y\n\t\ty+=1\n\tend\nc+=1\nbreak if n==0\nend\nputs c\nfor i in 0..c-1 do\n\tfor j in 0..c-1 do\n\t\tprint \"#{a[i][j]}\"\n\tend\n\tprintf(\"\\n\")\nend\n\n"}, {"source_code": "x = gets.to_i\nd = Array.new(105) { Array.new(105) {0} }\ncnt = Math.log((x * 2 * 3), 3).floor\nwhile (cnt + 1) * (cnt + 2) * (cnt + 3) <= x * 6\n cnt += 1 \nend\nwhile (cnt) * (cnt + 1) * (cnt + 2) > x * 6\n cnt -= 1\nend\ncnt += 2\n0.upto(cnt - 2) do |i|\n (i + 1).upto(cnt - 1) do |j|\n d[i][j] = d[j][i] = 1\n end\nend\np = cnt - 1\nx -= cnt * (cnt - 1) * (cnt - 2) / 6\nwhile x > 0 \n while p * (p - 1) / 2 > x\n p -= 1\n end\n 0.upto(p - 1) do |i|\n d[cnt][i] = d[i][cnt] = 1\n end\n cnt += 1\n x -= p * (p - 1) / 2\nend\nputs cnt\n0.upto(cnt - 1) do |i|\n 0.upto(cnt - 1) do |j|\n print \"#{d[i][j]}\"\n end\n puts ''\nend"}], "negative_code": [{"source_code": "def c(n, m)\n c = 1\n m.times do |i|\n c = c * (n - i) / (i + 1)\n end\n c\nend\n\n# c(n, 3) - c(n - 1, 3) = c(n - 1, 2) !!\n# c(n, 2) - c(n - 1, 2) = c(n - 1, 1) = n - 1 !!\n\nm = gets.to_i\nk = 3\nk += 1 while c(k + 1, 3) <= m\nm -= c(k, 3)\n\na = []\nwhile m > 0\n b = 2\n b += 1 while c(b + 1, 2) <= m\n m -= c(k, 2)\n a << b\nend\n\nn = k + a.size\ne = Array.new(n){'0' * n}\nk.times do |i|\n k.times do |j|\n e[i][j] = '1' if i != j\n end\nend\na.each_with_index do |c, j|\n c.times do |i|\n e[i][k + j] = '1'\n e[k + j][i] = '1'\n end\nend\np n\nputs e * \"\\n\"\n"}, {"source_code": "n=gets.to_i\na=Array.new(101){|el| el=Array.new(101){|elem| elem=0}}\nc,y=0,0\nfor i in 0..101 do\ny=0\n\twhile yn\n\t\ta[i][y],a[y][i]=1,1\n\t\tn-=y\n\t\ty+=1\n\tend\nc+=1\nbreak if n==0\nend\nputs c\nfor i in 0..c-1 do\n\tfor j in 0..c-1 do\n\t\tprint \"#{a[i][j]} \"\n\tend\n\tprintf(\"\\n\")\nend\n\n"}, {"source_code": "x = gets.to_i\nd = Array.new(105) { Array.new(105) {0} }\ncnt = Math.log((x * 2 * 3), 3).floor\nwhile (cnt + 1) * (cnt + 2) * (cnt + 3) <= x * 6\n cnt += 1 \nend\nwhile (cnt) * (cnt + 1) * (cnt + 2) > x * 6\n cnt -= 1\nend\ncnt += 2\n0.upto(cnt - 2) do |i|\n (i + 1).upto(cnt - 1) do |j|\n d[i][j] = d[j][i] = 1\n end\nend\nx -= cnt * (cnt - 1) * (cnt - 2) / 6\nwhile x > 0 \n p = (x >= cnt / 2) ? (cnt / 2) : x\n x -= p\n 0.upto(p - 1) do |i|\n d[2 * i][cnt] = d[2 * i + 1][cnt] = d[cnt][2 * i] = d[cnt][2 * i + 1] = 1\n end\n cnt += 1\nend\nputs cnt\n0.upto(cnt - 1) do |i|\n 0.upto(cnt - 1) do |j|\n print \"#{d[i][j]}\"\n end\n puts ''\nend"}], "src_uid": "d3f4c7fedd6148c28d8780142b6594f4"} {"nl": {"description": "You still have partial information about the score during the historic football match. You are given a set of pairs $$$(a_i, b_i)$$$, indicating that at some point during the match the score was \"$$$a_i$$$: $$$b_i$$$\". It is known that if the current score is «$$$x$$$:$$$y$$$», then after the goal it will change to \"$$$x+1$$$:$$$y$$$\" or \"$$$x$$$:$$$y+1$$$\". What is the largest number of times a draw could appear on the scoreboard?The pairs \"$$$a_i$$$:$$$b_i$$$\" are given in chronological order (time increases), but you are given score only for some moments of time. The last pair corresponds to the end of the match.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$1 \\le n \\le 10000$$$) — the number of known moments in the match. Each of the next $$$n$$$ lines contains integers $$$a_i$$$ and $$$b_i$$$ ($$$0 \\le a_i, b_i \\le 10^9$$$), denoting the score of the match at that moment (that is, the number of goals by the first team and the number of goals by the second team). All moments are given in chronological order, that is, sequences $$$x_i$$$ and $$$y_j$$$ are non-decreasing. The last score denotes the final result of the match.", "output_spec": "Print the maximum number of moments of time, during which the score was a draw. The starting moment of the match (with a score 0:0) is also counted.", "sample_inputs": ["3\n2 0\n3 1\n3 4", "3\n0 0\n0 0\n0 0", "1\n5 4"], "sample_outputs": ["2", "1", "5"], "notes": "NoteIn the example one of the possible score sequences leading to the maximum number of draws is as follows: 0:0, 1:0, 2:0, 2:1, 3:1, 3:2, 3:3, 3:4."}, "positive_code": [{"source_code": "n = gets.to_i\npa,pb = 0,0\nans = 1\nn.times do\n a,b = gets.split.map(&:to_i)\n if a == b\n ans += a-[pa,pb].max+(pa==pb ? 0 : 1)\n elsif a > b && pa <= b\n ans += b-[pa,pb].max+(pa==pb ? 0 : 1)\n elsif a < b && pb <= a\n ans += a-[pa,pb].max+(pa==pb ? 0 : 1)\n end\n pa,pb = a,b\nend\np ans\n\n"}, {"source_code": "DBG = !true\nn = gets.to_i\nsum = 0\npends = -1\npende = -1\nfor i in 0...n\n a,b = gets.split.map{|z| z.to_i}\n mn = [a,b].min\n mx = [a,b].max\n if pende != -1 && mn >= pends\n sum += [mn, pende].min - pends + 1\n end\n if mx <= mn + 1\n pende = -1\n pends = -1\n else\n pends = mn + 1\n pende = mx - 1\n end\n puts \"a #{a} b #{b} sum #{sum} pends #{pende}\" if DBG\nend\nputs \"#{[a,b].min - sum + 1}\"\n"}, {"source_code": "if File.exists?(\"input\")\n $stdin = File.open(\"input\")\n $stdout = File.open(\"output\",\"w\")\nend\n\nn = gets.to_i\narr = []\nn.times do\n\tarr.push gets.chomp.split(\" \").map(&:to_i)\nend\n\nprevx = 0\nprevy = 0\n\ndef inrange(a,b,x)\n\treturn true if x>=a and x<=b\nend\n\n\n\nans = 0\n\nmaxtie = -1\n\narr.each do |z|\n\tx = z[0]\n\ty = z[1]\n\n\n#\tif(prevx==x && prevy==y)\n#\t\tnext\n#\tend\n\n\n\tif(y b && pa <= b\n ans += b-[pa,pb].max+(pa==pb ? 0 : 1)\n elsif a < b && pb <= a\n ans += a-[pa,pb].max+(pa==pb ? 0 : 1)\n end\n pa,pb = a,b\n p ans\nend\np ans"}, {"source_code": "DBG = true\nn = gets.to_i\nsum = 0\nprev = -1\nfor i in 0...n\n a,b = gets.split.map{|z| z.to_i}\n d = [[a,b].min, prev].max\n sum += [0, [a,b].max - d - 1].max\n prev = (a==b ? a : ([a,b].max - 1))\nend\nputs \"#{[a,b].min + 1 - sum}\"\n"}, {"source_code": "if File.exists?(\"input\")\n $stdin = File.open(\"input\")\n $stdout = File.open(\"output\",\"w\")\nend\n\nn = gets.to_i\narr = []\nn.times do\n\tarr.push gets.chomp.split(\" \").map(&:to_i)\nend\n\nprevx = 0\nprevy = 0\nstart=1\n\ndef inrange(a,b,x)\n\treturn true if x>=a and x<=b\nend\n\nans = 0\narr.each do |z|\n\tx = z[0]\n\ty = z[1]\n\t\n\tif(prevx==x && prevy==y)\n\t\tnext\n\tend\n\tstart=0\n\n\tif(y=a and x<=b\nend\n\nans = 1\narr.each do |z|\n\tx = z[0]\n\ty = z[1]\n\t\n\tif(prevx==x && prevy==y)\n\t\tnext\n\tend\n\n\n\tif(y=a and x<=b\nend\n\n\n\nans = 0\n\nmaxtie = -1\n\narr.each do |z|\n\tx = z[0]\n\ty = z[1]\n\n\n\tif(prevx==x && prevy==y)\n\t\tnext\n\tend\n\n\n\tif(y=a and x<=b\nend\n\n\n\nans = 0\n\nmaxtie = -1\n\narr.each do |z|\n\tx = z[0]\n\ty = z[1]\n\n\n#\tif(prevx==x && prevy==y)\n#\t\tnext\n#\tend\n\n\n\tif(yx} end\ndef min(a,b) a<=b ? a : b end\ndef max(a,b) a>=b ? a : b end\ndef sum(ar) ar.inject(:+) end\n\n#def bs(ar,eq,n) en=(eq==1 ? :>= : :>); ar.bsearch_index{|x|x.send(en,n)} end\ndef C(a,b) b==0||a==b ? 1 : (b=a-b if a/2 0\n (1...(n - 1)).each do |i|\n (1...(n - 1)).each do |j|\n counter += 1 if board[i][j] == 'X' \\\n && board[i][j] == board[i - 1][j - 1] \\\n && board[i][j] == board[i - 1][j + 1] \\\n && board[i][j] == board[i + 1][j - 1] \\\n && board[i][j] == board[i + 1][j + 1]\n end\n end\nend\nputs counter"}, {"source_code": "n = gets.to_i\ns=[]\nn.times{s< 0 do\r\n str1 = gets\r\n if str1[0] == 'l' and str1[1] == 'o' then\r\n is_locked = true\r\n else\r\n if str1[0] == 'u' and str1[1] == 'n' then\r\n is_locked = false\r\n else\r\n if is_locked == false then\r\n ans = str1\r\n end\r\n end\r\n end\r\n n = n - 1\r\nend\r\nprint ans"}, {"source_code": "current_color = 'blue'\ncount = gets.chomp.to_i\ncolors = []\n\ncount.times do\n colors << gets.chomp\nend\n\nlocked = false\ncolors.each do |color_item|\n if locked\n locked = false if color_item == 'unlock'\n else\n if color_item == 'lock'\n locked = true\n elsif color_item != 'unlock'\n current_color = color_item\n end\n end\nend\n\nputs current_color\n"}], "negative_code": [], "src_uid": "6215233349b0f682a238a476e9153ac4"} {"nl": {"description": "The Squareland national forest is divided into equal $$$1 \\times 1$$$ square plots aligned with north-south and east-west directions. Each plot can be uniquely described by integer Cartesian coordinates $$$(x, y)$$$ of its south-west corner.Three friends, Alice, Bob, and Charlie are going to buy three distinct plots of land $$$A, B, C$$$ in the forest. Initially, all plots in the forest (including the plots $$$A, B, C$$$) are covered by trees. The friends want to visit each other, so they want to clean some of the plots from trees. After cleaning, one should be able to reach any of the plots $$$A, B, C$$$ from any other one of those by moving through adjacent cleared plots. Two plots are adjacent if they share a side. For example, $$$A=(0,0)$$$, $$$B=(1,1)$$$, $$$C=(2,2)$$$. The minimal number of plots to be cleared is $$$5$$$. One of the ways to do it is shown with the gray color. Of course, the friends don't want to strain too much. Help them find out the smallest number of plots they need to clean from trees.", "input_spec": "The first line contains two integers $$$x_A$$$ and $$$y_A$$$ — coordinates of the plot $$$A$$$ ($$$0 \\leq x_A, y_A \\leq 1000$$$). The following two lines describe coordinates $$$(x_B, y_B)$$$ and $$$(x_C, y_C)$$$ of plots $$$B$$$ and $$$C$$$ respectively in the same format ($$$0 \\leq x_B, y_B, x_C, y_C \\leq 1000$$$). It is guaranteed that all three plots are distinct.", "output_spec": "On the first line print a single integer $$$k$$$ — the smallest number of plots needed to be cleaned from trees. The following $$$k$$$ lines should contain coordinates of all plots needed to be cleaned. All $$$k$$$ plots should be distinct. You can output the plots in any order. If there are multiple solutions, print any of them.", "sample_inputs": ["0 0\n1 1\n2 2", "0 0\n2 0\n1 1"], "sample_outputs": ["5\n0 0\n1 0\n1 1\n1 2\n2 2", "4\n0 0\n1 0\n1 1\n2 0"], "notes": "NoteThe first example is shown on the picture in the legend.The second example is illustrated with the following image: "}, "positive_code": [{"source_code": "p = [[], [], []]\np[0][0], p[0][1], p[1][0], p[1][1], p[2][0], p[2][1] = STDIN.read.split.map(&:to_i)\np.sort!\nh = {p[0] => true}\n\nx, y = p[0][0], p[0][1]\ntx, ty = p[1][0], p[1][1]\nwhile x < tx\n x += 1\n h[[x, y]] = true\nend\nwhile y != ty\n y += (y < ty ? 1 : -1)\n h[[x, y]] = true\nend\n\nif p[1][0] == p[2][0] || p[1][1] <= p[2][1]\n x, y = p[1][0], p[1][1]\n tx, ty = p[2][0], p[2][1]\n while y != ty\n y += (y < ty ? 1 : -1)\n h[[x, y]] = true\n end \n while x < tx\n x += 1\n h[[x, y]] = true\n end \nelsif p[0][1] <= p[2][1]\n x, y = p[0][0], p[0][1]\n while x < p[1][0]\n x += 1\n h[[x, y]] = true\n end\n while y < p[2][1]\n y += 1\n h[[x, y]] = true\n end\n while x < p[2][0]\n x += 1\n h[[x, y]] = true\n end\nelse\n if p[0][1] <= p[1][1]\n x, y = p[0][0], p[0][1]\n tx, ty = p[2][0], p[2][1]\n while x < tx\n x += 1\n h[[x, y]] = true\n end\n while y != ty\n y += (y < ty ? 1 : -1)\n h[[x, y]] = true\n end\n else\n x, y = p[1][0], p[1][1]\n tx, ty = p[2][0], p[2][1]\n while x < tx\n x += 1\n h[[x, y]] = true\n end\n while y != ty\n y += (y < ty ? 1 : -1)\n h[[x, y]] = true\n end\n end\nend\n\nputs h.size\nh.keys.each{|x| puts(x[0].to_s + ' ' + x[1].to_s)}\n"}, {"source_code": "a = gets.chomp.split(\" \").map(&:to_i)\nb = gets.chomp.split(\" \").map(&:to_i)\nc = gets.chomp.split(\" \").map(&:to_i)\n\nxa = a[0]\nya = a[1]\nxb = b[0]\nyb = b[1]\nxc = c[0]\nyc = c[1]\n\nans = (xa-xb).abs + (ya-yb).abs + 1\n\nrectop = [xa,xb].max\nrecbot = [xa,xb].min\nrecright = [ya,yb].max\nrecleft = [ya,yb].min\n\nuserecver = 0\n\nif xc< recbot\n userecver = -1\n ans += recbot-xc\nelsif xc > rectop\n userecver = 1\n ans += xc-rectop\nend\n\nuserecside = 0\nif yc< recleft\n userecside = -1\n ans += recleft-yc\nelsif yc > recright\n userecside = 1\n ans += yc-recright\nend\n\n#ans += 1\nputs ans\n\n#puts userecver\n#puts userecside\n\nif userecver == -1\n #you definitely need the bottom\n (recleft..recright).each do |k|\n puts \"#{recbot} #{k}\"\n end\n\n if xa == recbot\n (recbot+1..xb).each do |k|\n puts \"#{k} #{yb}\"\n end\n else\n (recbot+1..xa).each do |k|\n puts \"#{k} #{ya}\"\n end\n end\n\n #DO THE C STUFF\n if userecside == -1\n (xc..recbot-1).each do |k|\n puts \"#{k} #{yc}\"\n end\n (yc+1..recleft-1).each do |k|\n puts \"#{recbot} #{k}\"\n end\n\n elsif userecside == 1\n (xc..recbot-1).each do |k|\n puts \"#{k} #{yc}\"\n end\n (recright+1..yc).each do |k|\n puts \"#{recbot} #{k}\"\n end\n\n else\n (xc..recbot-1).each do |k|\n puts \"#{k} #{yc}\"\n end\n end\n\nelsif userecver == 1\n\n #you definitely need the top\n (recleft..recright).each do |k|\n puts \"#{rectop} #{k}\"\n end\n\n if xa == rectop\n (xb..rectop-1).each do |k|\n puts \"#{k} #{yb}\"\n end\n else\n (xa..rectop-1).each do |k|\n puts \"#{k} #{ya}\"\n end\n end\n\n #DO the C stuff\n if userecside == -1\n (rectop+1..xc).each do |k|\n puts \"#{k} #{recleft}\"\n end\n (yc..recleft-1).each do |k|\n puts \"#{xc} #{k}\"\n end\n\n elsif userecside == 1\n (rectop+1..xc).each do |k|\n puts \"#{k} #{recright}\"\n end\n (recright+1..yc).each do |k|\n puts \"#{xc} #{k}\"\n end\n\n else\n (rectop+1..xc).each do |k|\n puts \"#{k} #{yc}\"\n end\n end\n\nelse\n #You don't need the top or bottom, you may need the left or right\n\n if userecside == -1\n #You need the left\n (recbot..rectop).each do |k|\n puts \"#{k} #{recleft}\"\n end\n\n if ya == recleft\n (recleft+1..yb).each do |k|\n puts \"#{xb} #{k}\"\n end\n else\n (recleft+1..ya).each do |k|\n puts \"#{xa} #{k}\"\n end\n end\n\n (yc..recleft-1).each do |k|\n puts \"#{xc} #{k}\"\n end\n\n elsif userecside == 1\n #You need the right\n (recbot..rectop).each do |k|\n puts \"#{k} #{recright}\"\n end\n\n if ya == recright\n (yb..recright-1).each do |k|\n puts \"#{xb} #{k}\"\n end\n else\n (ya..recright-1).each do |k|\n puts \"#{xa} #{k}\"\n end\n end\n\n (recright+1..yc).each do |k|\n puts \"#{xc} #{k}\"\n end\n\n else\n #0,0 -> do a,c, c,b\n\n acxmin = [xa,xc].min\n acxmax = [xa,xc].max\n\n acymin = [ya,yc].min\n acymax = [ya,yc].max\n\n bcxmin = [xb,xc].min\n bcxmax = [xb,xc].max\n\n bcymin = [yb,yc].min\n bcymax = [yb,yc].max\n \n\n if xa rectop\n userecver = 1\n ans += xc-rectop\nend\n\nuserecside = 0\nif yc< recleft\n userecside = -1\n ans += recleft-yc\nelsif yc > recright\n userecside = 1\n ans += yc-recright\nend\n\n#ans += 1\nputs ans\n\n#puts userecver\n#puts userecside\n\nif userecver == -1\n #you definitely need the bottom\n (recleft..recright).each do |k|\n puts \"#{recbot} #{k}\"\n end\n\n if xa == recbot\n (recbot+1..xb).each do |k|\n puts \"#{k} #{yb}\"\n end\n else\n (recbot+1..xa).each do |k|\n puts \"#{k} #{ya}\"\n end\n end\n\n #DO THE C STUFF\n if userecside == -1\n (xc..recbot).each do |k|\n puts \"#{k} #{yc}\"\n end\n (yc+1..recleft-1).each do |k|\n puts \"#{recbot} #{k}\"\n end\n\n elsif userecside == 1\n (xc..recbot).each do |k|\n puts \"#{k} #{yc}\"\n end\n (recright+1..yc).each do |k|\n puts \"#{recbot} #{k}\"\n end\n\n else\n (xc..recbot-1).each do |k|\n puts \"#{k} #{yc}\"\n end\n end\n\nelsif userecver == 1\n\n #you definitely need the top\n (ya..yb).each do |k|\n puts \"#{rectop} #{k}\"\n end\n\n if xa == rectop\n (xb..rectop-1).each do |k|\n puts \"#{k} #{yb}\"\n end\n else\n (xa..rectop-1).each do |k|\n puts \"#{k} #{ya}\"\n end\n end\n\n #DO the C stuff\n if userecside == -1\n (rectop+1..xc).each do |k|\n puts \"#{k} #{recleft}\"\n end\n (yc..recleft-1).each do |k|\n puts \"#{xc} #{k}\"\n end\n\n elsif userecside == 1\n (rectop+1..xc).each do |k|\n puts \"#{k} #{recright}\"\n end\n (recright+1..yc).each do |k|\n puts \"#{xc} #{k}\"\n end\n\n else\n (rectop+1..xc).each do |k|\n puts \"#{k} #{yc}\"\n end\n end\n\nelse\n #You don't need the top or bottom, you may need the left or right\n\n if userecside == -1\n #You need the left\n (recbot..rectop).each do |k|\n puts \"#{k} #{recleft}\"\n end\n\n if ya == recleft\n (recleft+1..yb).each do |k|\n puts \"#{xb} #{k}\"\n end\n else\n (recleft+1..ya).each do |k|\n puts \"#{xa} #{k}\"\n end\n end\n\n (yc..recleft-1).each do |k|\n puts \"#{xc} #{k}\"\n end\n\n elsif userecside == 1\n #You need the right\n (recbot..rectop).each do |k|\n puts \"#{k} #{recright}\"\n end\n\n if ya == recright\n (yb..recright-1).each do |k|\n puts \"#{xb} #{k}\"\n end\n else\n (ya..recright-1).each do |k|\n puts \"#{xa} #{k}\"\n end\n end\n\n (recright+1..yc).each do |k|\n puts \"#{xc} #{k}\"\n end\n\n else\n #0,0 -> do a,c, c,b\n\n acxmin = [xa,xc].min\n acxmax = [xa,xc].max\n\n acymin = [ya,yc].min\n acymax = [ya,yc].max\n\n bcxmin = [xb,xc].min\n bcxmax = [xb,xc].max\n\n bcymin = [yb,yc].min\n bcymax = [yb,yc].max\n \n\n if xa rectop\n userecver = 1\n ans += xc-rectop\nend\n\nuserecside = 0\nif yc< recleft\n userecside = -1\n ans += recleft-yc\nelsif yc > recright\n userecside = 1\n ans += yc-recright\nend\n\n#ans += 1\nputs ans\n\n#puts userecver\n#puts userecside\n\nif userecver == -1\n #you definitely need the bottom\n (recleft..recright).each do |k|\n puts \"#{recbot} #{k}\"\n end\n\n if xa == recbot\n (recbot+1..xb).each do |k|\n puts \"#{k} #{yb}\"\n end\n else\n (recbot+1..xa).each do |k|\n puts \"#{k} #{ya}\"\n end\n end\n\n #DO THE C STUFF\n if userecside == -1\n (xc..recbot).each do |k|\n puts \"#{k} #{yc}\"\n end\n (yc+1..recleft-1).each do |k|\n puts \"#{recbot} #{k}\"\n end\n\n elsif userecside == 1\n (xc..recbot).each do |k|\n puts \"#{k} #{yc}\"\n end\n (recright+1..yc).each do |k|\n puts \"#{recbot} #{k}\"\n end\n\n else\n (xc..recbot-1).each do |k|\n puts \"#{k} #{yc}\"\n end\n end\n\nelsif userecver == 1\n\n #you definitely need the top\n (ya..yb).each do |k|\n puts \"#{rectop} #{k}\"\n end\n\n if xa == rectop\n (xb..rectop-1).each do |k|\n puts \"#{k} #{yb}\"\n end\n else\n (xa..rectop-1).each do |k|\n puts \"#{k} #{ya}\"\n end\n end\n\n #DO the C stuff\n if userecside == -1\n (rectop+1..xc).each do |k|\n puts \"#{k} #{recleft}\"\n end\n (yc..recleft-1).each do |k|\n puts \"#{xc} #{k}\"\n end\n\n elsif userecside == 1\n (rectop+1..xc).each do |k|\n puts \"#{k} #{recright}\"\n end\n (recright+1..yc).each do |k|\n puts \"#{xc} #{k}\"\n end\n\n else\n (rectop+1..xc).each do |k|\n puts \"#{k} #{yc}\"\n end\n end\n\nelse\n #You don't need the top or bottom, you may need the left or right\n\n if userecside == -1\n #You need the left\n (recbot..rectop).each do |k|\n puts \"#{k} #{recleft}\"\n end\n\n if ya == recleft\n (recleft+1..yb).each do |k|\n puts \"#{xb} #{k}\"\n end\n else\n (recleft+1..ya).each do |k|\n puts \"#{xa} #{k}\"\n end\n end\n\n (yc..recleft-1).each do |k|\n puts \"#{xc} #{k}\"\n end\n\n elsif userecside == 1\n #You need the right\n (recbot..rectop).each do |k|\n puts \"#{k} #{recright}\"\n end\n\n if ya == recright\n (yb..recright-1).each do |k|\n puts \"#{xb} #{k}\"\n end\n else\n (ya..recright-1).each do |k|\n puts \"#{xa} #{k}\"\n end\n end\n\n (recright+1..yc).each do |k|\n puts \"#{xc} #{k}\"\n end\n\n else\n #0,0 -> do a,c, c,b\n\n acxmin = [xa,xc].min\n acxmax = [xa,xc].max\n\n acymin = [ya,yc].min\n acymax = [ya,yc].max\n\n bcxmin = [xb,xc].min\n bcxmax = [xb,xc].max\n\n bcymin = [yb,yc].min\n bcymax = [yb,yc].max\n \n\n (acxmin..acxmax-1).each do |k|\n puts \"#{k} #{ya}\"\n end\n \n (acymin..acymax).each do |k|\n puts \"#{xc} #{k}\"\n end\n\n (bcxmin+1..bcxmax-1).each do |k|\n puts \"#{k} #{yc}\"\n end\n \n (bcymin+1..bcymax).each do |k|\n puts \"#{xb} #{k}\"\n end\n\n end\nend"}, {"source_code": "a = gets.chomp.split(\" \").map(&:to_i)\nb = gets.chomp.split(\" \").map(&:to_i)\nc = gets.chomp.split(\" \").map(&:to_i)\n\nxa = a[0]\nya = a[1]\nxb = b[0]\nyb = b[1]\nxc = c[0]\nyc = c[1]\n\nans = (xa-xb).abs + (ya-yb).abs + 1\n\nrectop = [xa,xb].max\nrecbot = [xa,xb].min\nrecright = [ya,yb].max\nrecleft = [ya,yb].min\n\nuserecver = 0\n\nif xc< recbot\n userecver = -1\n ans += recbot-xc\nelsif xc > rectop\n userecver = 1\n ans += xc-rectop\nend\n\nuserecside = 0\nif yc< recleft\n userecside = -1\n ans += recleft-yc\nelsif yc > recright\n userecside = 1\n ans += yc-recright\nend\n\n#ans += 1\nputs ans\n\n#puts userecver\n#puts userecside\n\nif userecver == -1\n #you definitely need the bottom\n (recleft..recright).each do |k|\n puts \"#{recbot} #{k}\"\n end\n\n if xa == recbot\n (recbot+1..xb).each do |k|\n puts \"#{k} #{yb}\"\n end\n else\n (recbot+1..xa).each do |k|\n puts \"#{k} #{ya}\"\n end\n end\n\n #DO THE C STUFF\n if userecside == -1\n (xc..recbot).each do |k|\n puts \"#{k} #{yc}\"\n end\n (yc+1..recleft-1).each do |k|\n puts \"#{recbot} #{k}\"\n end\n\n elsif userecside == 1\n (xc..recbot).each do |k|\n puts \"#{k} #{yc}\"\n end\n (recright+1..yc).each do |k|\n puts \"#{recbot} #{k}\"\n end\n\n else\n (xc..recbot-1).each do |k|\n puts \"#{k} #{yc}\"\n end\n end\n\nelsif userecver == 1\n\n #you definitely need the top\n (ya..yb).each do |k|\n puts \"#{rectop} #{k}\"\n end\n\n if xa == rectop\n (xb..rectop-1).each do |k|\n puts \"#{k} #{yb}\"\n end\n else\n (xa..rectop-1).each do |k|\n puts \"#{k} #{ya}\"\n end\n end\n\n #DO the C stuff\n if userecside == -1\n (rectop+1..xc).each do |k|\n puts \"#{k} #{recleft}\"\n end\n (yc..recleft-1).each do |k|\n puts \"#{xc} #{k}\"\n end\n\n elsif userecside == 1\n (rectop+1..xc).each do |k|\n puts \"#{k} #{recright}\"\n end\n (recright+1..yc).each do |k|\n puts \"#{xc} #{k}\"\n end\n\n else\n (rectop+1..xc).each do |k|\n puts \"#{k} #{yc}\"\n end\n end\n\nelse\n #You don't need the top or bottom, you may need the left or right\n\n if userecside == -1\n #You need the left\n (recbot..rectop).each do |k|\n puts \"#{k} #{recleft}\"\n end\n\n if ya == recleft\n (recleft+1..yb).each do |k|\n puts \"#{xb} #{k}\"\n end\n else\n (recleft+1..ya).each do |k|\n puts \"#{xa} #{k}\"\n end\n end\n\n (yc..recleft-1).each do |k|\n puts \"#{xc} #{k}\"\n end\n\n elsif userecside == 1\n #You need the right\n (recbot..rectop).each do |k|\n puts \"#{k} #{recright}\"\n end\n\n if ya == recright\n (yb..recright-1).each do |k|\n puts \"#{xb} #{k}\"\n end\n else\n (ya..recright-1).each do |k|\n puts \"#{xa} #{k}\"\n end\n end\n\n (recright+1..yc).each do |k|\n puts \"#{xc} #{k}\"\n end\n\n else\n #0,0 -> do a,c, c,b\n\n acxmin = [xa,xc].min\n acxmax = [xa,xc].max\n\n acymin = [ya,yc].min\n acymax = [ya,yc].max\n\n bcxmin = [xb,xc].min\n bcxmax = [xb,xc].max\n\n bcymin = [yb,yc].min\n bcymax = [yb,yc].max\n \n\n (acxmin..acxmax-1).each do |k|\n puts \"#{k} #{ya}\"\n end\n \n (acymin..acymax).each do |k|\n puts \"#{xc} #{k}\"\n end\n\n (bcxmin+1..bcxmax-1).each do |k|\n puts \"#{k} #{yc}\"\n end\n \n (bcymin..bcymax).each do |k|\n puts \"#{xb} #{k}\"\n end\n\n end\nend\n"}, {"source_code": "a = gets.chomp.split(\" \").map(&:to_i)\nb = gets.chomp.split(\" \").map(&:to_i)\nc = gets.chomp.split(\" \").map(&:to_i)\n\nxa = a[0]\nya = a[1]\nxb = b[0]\nyb = b[1]\nxc = c[0]\nyc = c[1]\n\nans = (xa-xb).abs + (ya-yb).abs + 1\n\nrectop = [xa,xb].max\nrecbot = [xa,xb].min\nrecright = [ya,yb].max\nrecleft = [ya,yb].min\n\nuserecver = 0\n\nif xc< recbot\n userecver = -1\n ans += recbot-xc\nelsif xc > rectop\n userecver = 1\n ans += xc-rectop\nend\n\nuserecside = 0\nif yc< recleft\n userecside = -1\n ans += recleft-yc\nelsif yc > recright\n userecside = 1\n ans += yc-recright\nend\n\n#ans += 1\nputs ans\n\n#puts userecver\n#puts userecside\n\nif userecver == -1\n #you definitely need the bottom\n (recleft..recright).each do |k|\n puts \"#{recbot} #{k}\"\n end\n\n if xa == recbot\n (recbot+1..xb).each do |k|\n puts \"#{k} #{yb}\"\n end\n else\n (recbot+1..xa).each do |k|\n puts \"#{k} #{ya}\"\n end\n end\n\n #DO THE C STUFF\n if userecside == -1\n (xc..recbot).each do |k|\n puts \"#{k} #{yc}\"\n end\n (yc+1..recleft-1).each do |k|\n puts \"#{recbot} #{k}\"\n end\n\n elsif userecside == 1\n (xc..recbot).each do |k|\n puts \"#{k} #{yc}\"\n end\n (recright+1..yc).each do |k|\n puts \"#{recbot} #{k}\"\n end\n\n else\n (xc..recbot-1).each do |k|\n puts \"#{k} #{yc}\"\n end\n end\n\nelsif userecver == 1\n\n #you definitely need the top\n (recleft..recright).each do |k|\n puts \"#{rectop} #{k}\"\n end\n\n if xa == rectop\n (xb..rectop-1).each do |k|\n puts \"#{k} #{yb}\"\n end\n else\n (xa..rectop-1).each do |k|\n puts \"#{k} #{ya}\"\n end\n end\n\n #DO the C stuff\n if userecside == -1\n (rectop+1..xc).each do |k|\n puts \"#{k} #{recleft}\"\n end\n (yc..recleft-1).each do |k|\n puts \"#{xc} #{k}\"\n end\n\n elsif userecside == 1\n (rectop+1..xc).each do |k|\n puts \"#{k} #{recright}\"\n end\n (recright+1..yc).each do |k|\n puts \"#{xc} #{k}\"\n end\n\n else\n (rectop+1..xc).each do |k|\n puts \"#{k} #{yc}\"\n end\n end\n\nelse\n #You don't need the top or bottom, you may need the left or right\n\n if userecside == -1\n #You need the left\n (recbot..rectop).each do |k|\n puts \"#{k} #{recleft}\"\n end\n\n if ya == recleft\n (recleft+1..yb).each do |k|\n puts \"#{xb} #{k}\"\n end\n else\n (recleft+1..ya).each do |k|\n puts \"#{xa} #{k}\"\n end\n end\n\n (yc..recleft-1).each do |k|\n puts \"#{xc} #{k}\"\n end\n\n elsif userecside == 1\n #You need the right\n (recbot..rectop).each do |k|\n puts \"#{k} #{recright}\"\n end\n\n if ya == recright\n (yb..recright-1).each do |k|\n puts \"#{xb} #{k}\"\n end\n else\n (ya..recright-1).each do |k|\n puts \"#{xa} #{k}\"\n end\n end\n\n (recright+1..yc).each do |k|\n puts \"#{xc} #{k}\"\n end\n\n else\n #0,0 -> do a,c, c,b\n\n acxmin = [xa,xc].min\n acxmax = [xa,xc].max\n\n acymin = [ya,yc].min\n acymax = [ya,yc].max\n\n bcxmin = [xb,xc].min\n bcxmax = [xb,xc].max\n\n bcymin = [yb,yc].min\n bcymax = [yb,yc].max\n \n\n if xa a[i]\n a[0] += k\n d -= k * i\n end\n puts a[0]\nend\n"}, {"source_code": "T = gets.to_i\n\nT.times do\n n, d = gets.split.map(&:to_i)\n a = gets.split.map(&:to_i)\n\n ans = 0\n\n a.each_with_index do |cnt, dist|\n break if d <= 0\n next if cnt == 0\n\n if dist == 0\n ans += cnt\n else\n c = [cnt, d / dist].min\n cost = c * dist\n d -= cost\n ans += c\n end\n end\n\n puts ans\nend\n"}], "negative_code": [], "src_uid": "7bbb4b9f5eccfe13741445c815a4b1ca"} {"nl": {"description": "Game \"Minesweeper 1D\" is played on a line of squares, the line's height is 1 square, the line's width is n squares. Some of the squares contain bombs. If a square doesn't contain a bomb, then it contains a number from 0 to 2 — the total number of bombs in adjacent squares.For example, the correct field to play looks like that: 001*2***101*. The cells that are marked with \"*\" contain bombs. Note that on the correct field the numbers represent the number of bombs in adjacent cells. For example, field 2* is not correct, because cell with value 2 must have two adjacent cells with bombs.Valera wants to make a correct field to play \"Minesweeper 1D\". He has already painted a squared field with width of n cells, put several bombs on the field and wrote numbers into some cells. Now he wonders how many ways to fill the remaining cells with bombs and numbers are there if we should get a correct field in the end.", "input_spec": "The first line contains sequence of characters without spaces s1s2... sn (1 ≤ n ≤ 106), containing only characters \"*\", \"?\" and digits \"0\", \"1\" or \"2\". If character si equals \"*\", then the i-th cell of the field contains a bomb. If character si equals \"?\", then Valera hasn't yet decided what to put in the i-th cell. Character si, that is equal to a digit, represents the digit written in the i-th square.", "output_spec": "Print a single integer — the number of ways Valera can fill the empty cells and get a correct field. As the answer can be rather large, print it modulo 1000000007 (109 + 7).", "sample_inputs": ["?01???", "?", "**12", "1"], "sample_outputs": ["4", "2", "0", "0"], "notes": "NoteIn the first test sample you can get the following correct fields: 001**1, 001***, 001*2*, 001*10."}, "positive_code": [{"source_code": "s = gets.chomp!\nmods = 1000000007\ndp = [[0,0,0],[0,0,0]]\n\nlen =s.size\ncur =0;nxt=1;\n\ncase s[0]\nwhen '0'\n\tdp[cur][0]=1\nwhen '1'\n\tdp[cur][1]=1\nwhen '2'\n\tp 0\n\texit\nwhen '*'\n\tdp[cur][2]=1\nwhen '?'\n\tdp[cur][0]=dp[cur][1]=dp[cur][2]=1\nend\n\nfor i in 1...len\n\tcase s[i]\n\twhen '0'\n\t\tdp[nxt][0]=dp[cur][0]\n\twhen '1'\n\t\tdp[nxt][1]= dp[cur][0]\n\t\tdp[nxt][0]= dp[cur][2]\n\twhen '2'\n\t\tdp[nxt][1]=dp[cur][2]\n\twhen '*'\n\t\tdp[nxt][2]= (dp[cur][1]+dp[cur][2])%mods\n\twhen '?'\n\t\tdp[nxt][1]= (dp[cur][2]+dp[cur][0])%mods\n\t\tdp[nxt][0]= (dp[cur][0]+dp[cur][2])%mods\n\t\tdp[nxt][2]= (dp[cur][1]+dp[cur][2])%mods\n\tend\n\tcur = nxt\n\tnxt = cur==0?1:0\n\tdp[nxt] = [0,0,0]\nend\n\nputs \"#{(dp[cur][0]+dp[cur][2])%mods}\""}, {"source_code": "s = gets.chomp!\nmods = 1000000007\ndp = [[0,0,0],[0,0,0]]\n\nlen =s.size\ncur =0;nxt=1;\n\ndp[cur][0]=dp[cur][1]=1\n\nfor i in 0...len\n\tcase s[i]\n\twhen '0'\n\t\tdp[nxt][0]=dp[cur][0]\n\twhen '1'\n\t\tdp[nxt][1]= dp[cur][0]\n\t\tdp[nxt][0]= dp[cur][2]\n\twhen '2'\n\t\tdp[nxt][1]=dp[cur][2]\n\twhen '*'\n\t\tdp[nxt][2]= (dp[cur][1]+dp[cur][2])%mods\n\twhen '?'\n\t\tdp[nxt][1]= (dp[cur][2]+dp[cur][0])%mods\n\t\tdp[nxt][0]= (dp[cur][0]+dp[cur][2])%mods\n\t\tdp[nxt][2]= (dp[cur][1]+dp[cur][2])%mods\n\tend\n\tcur = nxt\n\tnxt = cur==0?1:0\n\tdp[nxt] = [0,0,0]\nend\n\nputs \"#{(dp[cur][0]+dp[cur][2])%mods}\""}, {"source_code": "s = gets.chomp!\nmods = 1000000007\ndp = [[0,0,0],[0,0,0]]\n\nlen =s.size\ncur =0;nxt=1;\n\ncase s[0]\nwhen '0'\n\tdp[cur][0]=1\nwhen '1'\n\tdp[cur][1]=1\nwhen '2'\n\tp 0\n\texit\nwhen '*'\n\tdp[cur][2]=1\nwhen '?'\n\tdp[cur][0]=dp[cur][1]=dp[cur][2]=1\nend\n\nfor i in 1...len\n\tcase s[i]\n\twhen '0'\n\t\tdp[nxt][0]=dp[cur][0]\n\twhen '1'\n\t\tdp[nxt][1]= dp[cur][0]\n\t\tdp[nxt][0]= dp[cur][2]\n\twhen '2'\n\t\tdp[nxt][1]=dp[cur][2]\n\twhen '*'\n\t\tdp[nxt][2]= (dp[cur][1]+dp[cur][2])%mods\n\twhen '?'\n\t\tdp[nxt][1]= (dp[cur][2]+dp[cur][0])%mods\n\t\tdp[nxt][0]= (dp[cur][0]+dp[cur][2])%mods\n\t\tdp[nxt][2]= (dp[cur][1]+dp[cur][2])%mods\n\tend\n\ta = cur \n\tcur = nxt\n\tnxt = a\n\tdp[nxt] = [0,0,0]\nend\n\nputs \"#{(dp[cur][0]+dp[cur][2])%mods}\""}, {"source_code": "s = gets.chomp!\nmods = 1000000007\ndp = [[0,0,0],[0,0,0]]\n\nlen =s.size\ncur =0;nxt=1;\n\ncase s[0]\nwhen '0'\n\tdp[cur][0]=1\nwhen '1'\n\tdp[cur][1]=1\nwhen '2'\n\tp 0\n\texit\nwhen '*'\n\tdp[cur][2]=1\nwhen '?'\n\tdp[cur][0]=dp[cur][1]=dp[cur][2]=1\nend\n\nfor i in 1...len\n\tcase s[i]\n\twhen '0'\n\t\tdp[nxt][0]=dp[cur][0]\n\twhen '1'\n\t\tdp[nxt][1]= dp[cur][0]\n\t\tdp[nxt][0]= dp[cur][2]\n\twhen '2'\n\t\tdp[nxt][1]=dp[cur][2]\n\twhen '*'\n\t\tdp[nxt][2]= (dp[cur][1]+dp[cur][2])%mods\n\twhen '?'\n\t\tdp[nxt][1]= (dp[cur][2]+dp[cur][0])%mods\n\t\tdp[nxt][0]= (dp[cur][0]+dp[cur][2])%mods\n\t\tdp[nxt][2]= (dp[cur][1]+dp[cur][2])%mods\n\tend\n\ta = cur \n\tcur = nxt\n\tnxt = a\n\tdp[nxt] = [0,0,0]\nend\n\np (dp[cur][0]+dp[cur][2])%mods"}], "negative_code": [], "src_uid": "c16c49baf7b2d179764871204475036e"} {"nl": {"description": "Pari has a friend who loves palindrome numbers. A palindrome number is a number that reads the same forward or backward. For example 12321, 100001 and 1 are palindrome numbers, while 112 and 1021 are not.Pari is trying to love them too, but only very special and gifted people can understand the beauty behind palindrome numbers. Pari loves integers with even length (i.e. the numbers with even number of digits), so she tries to see a lot of big palindrome numbers with even length (like a 2-digit 11 or 6-digit 122221), so maybe she could see something in them.Now Pari asks you to write a program that gets a huge integer n from the input and tells what is the n-th even-length positive palindrome number?", "input_spec": "The only line of the input contains a single integer n (1 ≤ n ≤ 10100 000).", "output_spec": "Print the n-th even-length palindrome number.", "sample_inputs": ["1", "10"], "sample_outputs": ["11", "1001"], "notes": "NoteThe first 10 even-length palindrome numbers are 11, 22, 33, ... , 88, 99 and 1001."}, "positive_code": [{"source_code": "a = gets.chomp \n\na = a + a.reverse\n\nputs a \n"}, {"source_code": "#!/usr/bin/ruby\ns=gets.chomp\nputs s+s.reverse"}, {"source_code": "str = gets.chomp\n\nprint str\nprint str.reverse"}, {"source_code": "#! /usr/bin/env ruby\ngets.chomp.tap{|s|print s; puts s.reverse}"}, {"source_code": "\na=gets.chomp\nb=a.split(\"\").reverse.join(\"\")\nprint a\nprint b"}, {"source_code": "s=gets.chomp\nputs s+s.reverse\n"}, {"source_code": "n = gets.strip\nputs \"#{n + n.reverse}\"\n"}, {"source_code": "a=gets.chomp\nb=a.split(\"\").reverse.join(\"\")\nprint a\nprint b"}, {"source_code": "n=gets.chomp\nputs n+n.reverse"}, {"source_code": "s=gets.chomp\nprint s,s.reverse,\"\\n\"\n\n"}, {"source_code": "n = gets.chomp\nputs \"#{n}#{n.reverse}\"\n"}, {"source_code": "s=gets.chomp; print \"#{s}#{s.reverse}\""}, {"source_code": "print (s=gets.chomp)+s.reverse"}, {"source_code": "s=gets.chomp; print s; print s.reverse"}, {"source_code": "s=gets.chomp; print s+s.reverse"}, {"source_code": "puts (s=gets.strip)+s.reverse"}, {"source_code": "s = gets.strip\nd = s.reverse\nans = s+d\nputs ans"}, {"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\n\norder = gets.chomp\n\nputs \"#{order}#{order.reverse}\"\n"}, {"source_code": "n = gets.chomp\nprint n\nprint n.reverse"}, {"source_code": "pal = gets.strip\nans = \"\"\nans << pal << pal.reverse\nputs ans\n"}, {"source_code": "$><<t\n\tn-=t\n\ti+=1\n\tt=t*10+9\nend\ns='%0*d'%[i,n]\nputs s.reverse+s"}, {"source_code": "#!/usr/bin/ruby\ns=gets\nputs s+s.reverse"}, {"source_code": "a=gets.chomp.to_i\n(a*2).times do\nprint \"1\"\nend\n"}, {"source_code": "s=gets\nprint s\nprint s.reverse\nprint \"\\n\"\n\n"}, {"source_code": "s=gets\nprint s,s.reverse,\"\\n\"\n\n"}], "src_uid": "bcf75978c9ef6202293773cf533afadf"} {"nl": {"description": "A ticket is a string consisting of six digits. A ticket is considered lucky if the sum of the first three digits is equal to the sum of the last three digits. Given a ticket, output if it is lucky or not. Note that a ticket can have leading zeroes.", "input_spec": "The first line of the input contains an integer $$$t$$$ ($$$1 \\leq t \\leq 10^3$$$) — the number of testcases. The description of each test consists of one line containing one string consisting of six digits.", "output_spec": "Output $$$t$$$ lines, each of which contains the answer to the corresponding test case. Output \"YES\" if the given ticket is lucky, and \"NO\" otherwise. You can output the answer in any case (for example, the strings \"yEs\", \"yes\", \"Yes\" and \"YES\" will be recognized as a positive answer).", "sample_inputs": ["5\n213132\n973894\n045207\n000000\n055776"], "sample_outputs": ["YES\nNO\nYES\nYES\nNO"], "notes": "NoteIn the first test case, the sum of the first three digits is $$$2 + 1 + 3 = 6$$$ and the sum of the last three digits is $$$1 + 3 + 2 = 6$$$, they are equal so the answer is \"YES\".In the second test case, the sum of the first three digits is $$$9 + 7 + 3 = 19$$$ and the sum of the last three digits is $$$8 + 9 + 4 = 21$$$, they are not equal so the answer is \"NO\".In the third test case, the sum of the first three digits is $$$0 + 4 + 5 = 9$$$ and the sum of the last three digits is $$$2 + 0 + 7 = 9$$$, they are equal so the answer is \"YES\"."}, "positive_code": [{"source_code": " gets.to_i.times {\n \tv = gets.chars.map {|x| x.to_i }\n \tputs ({true=>\"YES\", false=>\"NO\"}[v[..2].sum==v[3..].sum])\n }"}, {"source_code": "gets.to_i.times do\r\n s = gets.chomp.chars.map(&:to_i)\r\n puts s[..2].sum == s[3..].sum ? \"YES\" : \"NO\"\r\nend\r\n"}, {"source_code": "gets.to_i.times do\n s = gets.strip.chars\n fail unless s.length == 6\n a = s[..2].map(&:to_i).sum\n b = s[3..].map(&:to_i).sum\n puts(if a == b then 'YES' else 'NO' end)\nend\n"}, {"source_code": "def main\n gets.to_i.times do\n x = gets.chomp.chars.map(&:to_i)\n puts uyesno(x[..2].sum == x[3..].sum)\n end\nend\n\nMOD = 10**9 + 7\ndef gs = gets.chomp\ndef gi = gets.to_i\ndef gli = gets.split.map(&:to_i)\ndef cyesno(x) = x ? :Yes : :No\ndef uyesno(x) = x ? :YES : :NO\n\nmain\n"}, {"source_code": "main = -> {\n N = int\n N.times do\n digits = gets.chomp.chars.map(&:to_i)\n first = digits[0, 3].sum\n last = digits[3, 3].sum\n puts first == last ? \"YES\" : \"NO\"\n end\n}\n\nDEBUG = true\nMOD = 10**9+7\n$yesno = %w(No Yes)\nYesNo = %w(No Yes)\nYESNO = %w(NO YES)\nINF = 10**18\n\nrequire \"set\"\nrequire \"prime\"\n\ndef int; gets.to_s.to_i end\ndef ints; gets.to_s.split.map { |s| s.to_i } end\ndef int1s; gets.to_s.split.map { |s| s.to_i - 1 } end\ndef float; gets.to_s.to_f end\ndef floats; gets.to_s.split.map { |s| s.to_f } end\ndef array_of(&convert); gets.to_s.split.map(&convert) end\ndef string; gets.to_s.chomp end\ndef strings; gets.to_s.split end\ndef rep(n, &b); Array.new(n, &b) end\ndef yes; puts $yesno[1] end\ndef no; puts $yesno[0] end\ndef yesno t; puts $yesno[t] end\ndef YesNo t; puts YesNo[t] end\ndef YESNO t; puts YESNO[t] end\ndef zip(xs, *yss); Enumerator.new { |y| xs.zip(*yss) { |a| y.yield(*a) } } end\ndef max(*xs, &block); block_given? ? xs.max_by(&block) : xs.max end\ndef min(*xs, &block); block_given? ? xs.min_by(&block) : xs.min end\ndef minmax(*xs, &block); block_given? ? xs.minmax_by(&block) : xs.minmax end\ndef gcd(*xs); xs.inject(0, :gcd) end\ndef matrix(h, w, fill=nil, &block); return Array.new(h) { Array.new(w, &block) } if block_given?; Array.new(h) { [fill] * w } end\ndef debug(x = nil); STDERR.puts (block_given? ? yield(x) : x).inspect if DEBUG; x end\ndef debug_grid(grid, width = 1); grid.each { |row| STDERR.puts row.map { |x| x.inspect.ljust(width) }.join(\"\") } if DEBUG; grid end\ndef if_debug; yield if DEBUG end\n\nmodule Boolean\n def coerce(other); [other, to_i] end\n def +@; to_i end\n def to_int; to_i end\n def *(other); to_i * other end\nend\n\nclass TrueClass\n include Boolean\n def to_i; 1 end\nend\n\nclass FalseClass\n include Boolean\n def to_i; 0 end\nend\n\nclass Integer\n def div_ceil(y); (self + y - 1) / y end\n def mod_inv(mod = MOD); pow(mod-2, mod) end\n def mod_div(y, mod = MOD); self * y.mod_inv(mod) % mod end\n def mod_nCr(r, mod = MOD); x = y = 1; (1..r).each { |i| x = x * (self + 1 - i) % mod; y = y * i % mod }; x.mod_div(y, mod); end\n def factorial(mod = MOD); (2..self).inject(1) { |f, x| f * x % mod } end\n def popcount; x = self; c = 0; while x > 0; c += 1 if x & 1 == 1; x >>= 1 end; c end #TODO: faster\n def bitbrute(&block); (1< 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\n def lower_bound; ac, wa = self.end_open, self.begin; while ac - wa > 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\n def shakutori(r2, &pred); Enumerator.new { |y| j, r = r2.begin, r2.end_open; each { |i| j += 1 while j + 1 < r and pred[i, j+1]; y.yield(i, j) } }; end\n def widest(&block); Enumerator.new { |y| j, n = self.begin, self.end_open; each { |i| j += 1 while j < n and block[i, j]; y.yield(i, j) if block[i, j] } } end\nend\n\nclass Array\n def power(&block); (0 ... 1 << size).each(&block) end\n def sorted_merge(other); a = []; i = j = 0; n, m = size, other.size; if j < m and other[j] < self[i]; a << other[j]; j += 1 else; a << self[i]; i += 1 end while i < n; a.push(*other[j..-1]) if j < m; a end\n def upper_bound; ac, wa = 0, size; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\n def lower_bound; ac, wa = size, 0; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\n def cum(*xs, &op); a = []; a << xs[0] if xs.size > 0; a << x = self[0]; (1...size).each { |i| a << x = op[x, self[i]] }; a end\n def cumdiff(range); self[range.end_open] - self[range.begin]; end\n def compress(kinds = uniq.sort!); map { |x| kinds.bsearch_index { _1 >= x } }; end\nend\n\nmodule Enumerable\n def sorted_uniq; x = nil; filter { |y| c = x === y; x = y; !c } end\n def cumsum; ys = [0]; each { |x| ys << x + ys[-1] }; ys end\nend\n\nmain[]"}], "negative_code": [], "src_uid": "c7a5b4a015e28dd3759569bbdc130e93"} {"nl": {"description": "Dima worked all day and wrote down on a long paper strip his favorite number $$$n$$$ consisting of $$$l$$$ digits. Unfortunately, the strip turned out to be so long that it didn't fit in the Dima's bookshelf.To solve the issue, Dima decided to split the strip into two non-empty parts so that each of them contains a positive integer without leading zeros. After that he will compute the sum of the two integers and write it down on a new strip.Dima wants the resulting integer to be as small as possible, because it increases the chances that the sum will fit it in the bookshelf. Help Dima decide what is the minimum sum he can obtain.", "input_spec": "The first line contains a single integer $$$l$$$ ($$$2 \\le l \\le 100\\,000$$$) — the length of the Dima's favorite number. The second line contains the positive integer $$$n$$$ initially written on the strip: the Dima's favorite number. The integer $$$n$$$ consists of exactly $$$l$$$ digits and it does not contain leading zeros. Dima guarantees, that there is at least one valid way to split the strip.", "output_spec": "Print a single integer — the smallest number Dima can obtain.", "sample_inputs": ["7\n1234567", "3\n101"], "sample_outputs": ["1801", "11"], "notes": "NoteIn the first example Dima can split the number $$$1234567$$$ into integers $$$1234$$$ and $$$567$$$. Their sum is $$$1801$$$.In the second example Dima can split the number $$$101$$$ into integers $$$10$$$ and $$$1$$$. Their sum is $$$11$$$. Note that it is impossible to split the strip into \"1\" and \"01\" since the numbers can't start with zeros."}, "positive_code": [{"source_code": "n = gets.to_i\ns = gets.chomp\nans = []\nif n%2 == 0\n 3.times do |i|\n next if s[n/2-1+i] == \"0\"\n ans << s[0,n/2-1+i].to_i + s[n/2-1+i,n/2+1-i].to_i\n end\nelse\n 4.times do |i|\n k = n/2-1+i\n next if k <= 0 || k >= n || s[k] == \"0\"\n ans << s[0,k].to_i + s[k,n-k].to_i\n end\nend\nif ans.empty?\n l,r = n/2,n/2\n l -=1 while l >= 0 && s[l] == \"0\"\n r += 1 while r < n && s[r] == \"0\"\n ans << s[0,l].to_i + s[l,n-l].to_i if l > 0\n ans << s[0,r].to_i + s[r,n-r].to_i if r < n\n #p [l,r]\nend\n#p ans\nputs ans.min"}, {"source_code": "L = gets.to_i\nN = gets.chomp\n\nans = Float::INFINITY\n\npt = L/2\npt -= 1 while N[pt] == '0'\nif pt > 0\n res = N[0, pt].to_i + N[pt..-1].to_i\n ans = [ans, res].min\nend\npt = (L+1)/2\npt += 1 while pt < L && N[pt] == '0'\nif pt < L\n res = N[0, pt].to_i + N[pt..-1].to_i\n ans = [ans, res].min\nend\n\nputs ans"}, {"source_code": "n = gets.to_i\nl = gets.chomp\n\nans = l.to_i\n\ni = n/2\nj = i - 1\nwhile l[j+1] == '0' \n j -= 1\nend\nwhile l[i+1] == '0'\n i += 1\nend\n\na = l[0..i]\nb = l[(i+1)..n-1]\nans = [ans, l[0..i].to_i + l[(i+1)..n-1].to_i, l[0..j].to_i + l[(j+1)..n-1].to_i].min\n\nputs ans"}, {"source_code": "l = gets.to_i\nn = gets.chomp\nc = l\nidx = 0\n(1...l).each do |i|\n if n[i] != \"0\"\n m = (l-i > i ? l-i : i)\n if c > m\n c = m\n idx = i\n end\n end\nend\nans = n[0,idx].to_i+n[idx..-1].to_i\ns = ans.to_s.size\n(1...l).each do |i|\n if n[i] != \"0\"\n next if s < (l-i > i ? l-i : i)\n ret = n[0,i].to_i+n[i..-1].to_i\n if ret < ans\n ans = ret\n s = ans.to_s.size\n end\n end\nend\nputs ans"}], "negative_code": [{"source_code": "n = gets.to_i\nl = gets.chomp\n\nans = 10 ** 20\n\n(0..n-2).each do |i|\n if l[i+1] == '0' && i != n-2 \n next\n end\n ans = [ans, l[0..i].to_i + l[(i+1)..n-1].to_i].min\nend\n\nputs ans"}, {"source_code": "n = gets.to_i\nl = gets.chomp\n\nans = 10 ** 20\n\n(0..n-2).each do |i|\n if l[i+1] == '0'\n next\n end\n ans = [ans, l[0..i].to_i + l[(i+1)..n-1].to_i].min\nend\n\nputs ans"}, {"source_code": "n = gets.to_i\nl = gets.chomp\n\nans = 10 ** 20\n\n(0..n-2).each do |i|\n a = l[0..i]\n b = l[(i+1)..n-1]\n if b.size >= 2 && b[0] == '0'\n next\n end\n ans = [ans, a.to_i + b.to_i].min\nend\n\nputs ans"}, {"source_code": "n = gets.to_i\nl = gets.chomp\n\nans = 10 ** 20\n\n(0..n-2).each do |i|\n a = l[0..i]\n b = l[(i+1)..n-1]\n if b[0] == '0'\n next\n end\n ans = [ans, a.to_i + b.to_i].min\nend\n\nputs ans"}, {"source_code": "l = gets.to_i\nn = gets.chomp\nc = l\nidx = 0\n(l-1).downto(1) do |i|\n if n[i] != \"0\"\n m = (l-i > i ? l-i : i)\n if c > m\n c = m\n idx = i\n end\n end\nend\nans = n[0,idx].to_i+n[idx..-1].to_i\n(1...l).each do |i|\n if n[i] != \"0\"\n break if ans.to_s.size < (l-i > i ? l-i : i)\n ret = n[0,i].to_i+n[i..-1].to_i\n if ret < ans\n ans = ret\n end\n end\nend\nputs ans"}], "src_uid": "08bce37778b7bfe478340d5c222ae362"} {"nl": {"description": "Devu being a small kid, likes to play a lot, but he only likes to play with arrays. While playing he came up with an interesting question which he could not solve, can you please solve it for him?Given an array consisting of distinct integers. Is it possible to partition the whole array into k disjoint non-empty parts such that p of the parts have even sum (each of them must have even sum) and remaining k - p have odd sum? (note that parts need not to be continuous).If it is possible to partition the array, also give any possible way of valid partitioning.", "input_spec": "The first line will contain three space separated integers n, k, p (1 ≤ k ≤ n ≤ 105; 0 ≤ p ≤ k). The next line will contain n space-separated distinct integers representing the content of array a: a1, a2, ..., an (1 ≤ ai ≤ 109).", "output_spec": "In the first line print \"YES\" (without the quotes) if it is possible to partition the array in the required way. Otherwise print \"NO\" (without the quotes). If the required partition exists, print k lines after the first line. The ith of them should contain the content of the ith part. Print the content of the part in the line in the following way: firstly print the number of elements of the part, then print all the elements of the part in arbitrary order. There must be exactly p parts with even sum, each of the remaining k - p parts must have odd sum. As there can be multiple partitions, you are allowed to print any valid partition.", "sample_inputs": ["5 5 3\n2 6 10 5 9", "5 5 3\n7 14 2 9 5", "5 3 1\n1 2 3 7 5"], "sample_outputs": ["YES\n1 9\n1 5\n1 10\n1 6\n1 2", "NO", "YES\n3 5 1 3\n1 7\n1 2"], "notes": null}, "positive_code": [{"source_code": "def count_in_arrays(arrays, mod)\n\tarrays.map { |a|\n\t\ta.count { |x| x % 2 == mod }\n\t}.inject(0, &:+)\nend\n\nn,k,p = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\nodds = a.select { |x| x % 2 == 1 }\nevens = a.select { |x| x % 2 == 0 }\n\nif k-p > odds.length or (odds.length-(k-p)) % 2 == 1 or (odds.length-(k-p)) / 2 + evens.length < p\n\tputs \"NO\"\n\texit\nend\n\noddSets = (0...(k-p)).map { |i| [odds[i]] } \nevenSets = (0...p).map { |i|\n\tif i < evens.length\n\t\t[evens[i]]\n\telse\n\t\toi = (k-p)+2*(i-evens.length)\n\t\t[odds[oi], odds[oi+1]]\n\tend\n}\n\nusedOdds = count_in_arrays(oddSets, 1) + count_in_arrays(evenSets, 1)\nusedEvens = count_in_arrays(oddSets, 0) + count_in_arrays(evenSets, 0)\n\n\npushing_array = (p >= 1) ? evenSets[0] : oddSets[0]\nodds[usedOdds...odds.length].each { |o| pushing_array.push o }\nevens[usedEvens...evens.length].each { |e| pushing_array.push e }\n\nputs \"YES\"\noddSets.each { |a|\n\tputs a.length.to_s + \" \" + a.join(\" \")\n}\nevenSets.each { |a|\n\tputs a.length.to_s + \" \" + a.join(\" \")\n}\n\n\n"}], "negative_code": [], "src_uid": "5185f842c7c24d4118ae3661f4418a1d"} {"nl": {"description": "The Department of economic development of IT City created a model of city development till year 2100.To prepare report about growth perspectives it is required to get growth estimates from the model.To get the growth estimates it is required to solve a quadratic equation. Since the Department of economic development of IT City creates realistic models only, that quadratic equation has a solution, moreover there are exactly two different real roots.The greater of these roots corresponds to the optimistic scenario, the smaller one corresponds to the pessimistic one. Help to get these estimates, first the optimistic, then the pessimistic one.", "input_spec": "The only line of the input contains three integers a, b, c ( - 1000 ≤ a, b, c ≤ 1000) — the coefficients of ax2 + bx + c = 0 equation.", "output_spec": "In the first line output the greater of the equation roots, in the second line output the smaller one. Absolute or relative error should not be greater than 10 - 6.", "sample_inputs": ["1 30 200"], "sample_outputs": ["-10.000000000000000\n-20.000000000000000"], "notes": null}, "positive_code": [{"source_code": "a,b,c=gets.split.map(&:to_f)\n(a=-a;b=-b;c=-c)if a<0\nputs [1,-1].map{|s|(-b+s*Math.sqrt(b*b-4*a*c))/2/a}"}, {"source_code": "a,b,c=gets.split.map(&:to_f)\nif a<0\na=-a\nb=-b\nc=-c\nend\np (-b+Math.sqrt(b*b-4*a*c))/2/a\np (-b-Math.sqrt(b*b-4*a*c))/2/a"}, {"source_code": "a, b, c=gets.split.map &:to_i\nd=(b*b-4*a*c)**0.5\nx, y=[(-b+d)/(2*a), (-b-d)/(2*a)].sort\np y\np x"}, {"source_code": "a, b, c = gets.chomp.split\n\na = a.to_f\nb = b.to_f\nc = c.to_f\n\nd=(b**2-4*a*c)**(0.5)\nx=(-b+d)/(2*a)\ny=(-b-d)/(2*a)\n\nif y>x\nx, y= y, x\nend\n\nputs x\nprint y"}, {"source_code": "\n a,b,c=gets.split.map(&:to_f)\n(a=-a;b=-b;c=-c)if a<0\nputs [1,-1].map{|s|(-b+s*Math.sqrt(b*b-4*a*c))/2/a}"}], "negative_code": [{"source_code": "a,b,c=gets.split.map(&:to_f)\np (-b+Math.sqrt(b*b-4*a*c))/2\np (-b-Math.sqrt(b*b-4*a*c))/2"}, {"source_code": "a,b,c=gets.split.map(&:to_f)\np (-b+Math.sqrt(b*b-4*a*c))/2/a\np (-b-Math.sqrt(b*b-4*a*c))/2/a"}, {"source_code": "a, b, c=gets.split.map &:to_i\nd=(b*b-4*a*c)**0.5\np (-b+d)/(2*a)\np (-b-d)/(2*a)"}], "src_uid": "2d4ad39d42b349765435b351897403da"} {"nl": {"description": "You are given $$$n$$$ chips on a number line. The $$$i$$$-th chip is placed at the integer coordinate $$$x_i$$$. Some chips can have equal coordinates.You can perform each of the two following types of moves any (possibly, zero) number of times on any chip: Move the chip $$$i$$$ by $$$2$$$ to the left or $$$2$$$ to the right for free (i.e. replace the current coordinate $$$x_i$$$ with $$$x_i - 2$$$ or with $$$x_i + 2$$$); move the chip $$$i$$$ by $$$1$$$ to the left or $$$1$$$ to the right and pay one coin for this move (i.e. replace the current coordinate $$$x_i$$$ with $$$x_i - 1$$$ or with $$$x_i + 1$$$). Note that it's allowed to move chips to any integer coordinate, including negative and zero.Your task is to find the minimum total number of coins required to move all $$$n$$$ chips to the same coordinate (i.e. all $$$x_i$$$ should be equal after some sequence of moves).", "input_spec": "The first line of the input contains one integer $$$n$$$ ($$$1 \\le n \\le 100$$$) — the number of chips. The second line of the input contains $$$n$$$ integers $$$x_1, x_2, \\dots, x_n$$$ ($$$1 \\le x_i \\le 10^9$$$), where $$$x_i$$$ is the coordinate of the $$$i$$$-th chip.", "output_spec": "Print one integer — the minimum total number of coins required to move all $$$n$$$ chips to the same coordinate.", "sample_inputs": ["3\n1 2 3", "5\n2 2 2 3 3"], "sample_outputs": ["1", "2"], "notes": "NoteIn the first example you need to move the first chip by $$$2$$$ to the right and the second chip by $$$1$$$ to the right or move the third chip by $$$2$$$ to the left and the second chip by $$$1$$$ to the left so the answer is $$$1$$$.In the second example you need to move two chips with coordinate $$$3$$$ by $$$1$$$ to the left so the answer is $$$2$$$."}, "positive_code": [{"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nputs [a.count(&:odd?), a.count(&:even?)].min\n"}, {"source_code": "n = gets.to_i\narr = gets.split.map(&:to_i)\neven = 0\nodd = 0\n\narr.map{ |x| x % 2 == 0 ? even += 1 : odd += 1 }\n\neven < odd ? puts(even) : puts(odd)"}, {"source_code": "n = gets.to_i\nxs = gets.split.map(&:to_i)\n\nodd_c = xs.count { |x| x.odd? }\neven_c = n - odd_c\n\nputs [odd_c, even_c].min\n"}, {"source_code": "n = gets.to_i\nnums = gets.split.map(&:to_i)\neven = nums.select(&:even?).length\nv = [even, n - even]\nputs v.min"}, {"source_code": "n = gets.to_i\na = gets.split.map{|i|i.to_i%2}.count(0)\nputs [a,n-a].min"}, {"source_code": "gets;c=[0,0];gets.split.each{|x|c[x.to_i%2]+=1};p c.min"}, {"source_code": "n = gets.strip.to_i\nchips = gets.strip.split(' ').map(&:to_i)\ncnt = chips.group_by { |x| x % 2 }\nputs [(cnt[0] || []).size, (cnt[1] || []).size].min"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\ncnt_odd = 0\na.each do |x|\n cnt_odd += 1 if x % 2 == 1\nend\nputs [n - cnt_odd, cnt_odd].min"}, {"source_code": "\nN = gets.to_i\narr = gets.split.map(&:to_i)\narr.map! { |x| x % 2 }\nzero = arr.count 0\none = arr.count 1\nif zero < one\n\tputs zero\nelse\n\tputs one\nend\n"}, {"source_code": "n = gets.to_i\nnums = gets.split.map(&:to_i)\neven = nums.select(&:even?).length\nv = [even, n - even]\nputs v.min\n"}, {"source_code": "nn = gets.to_i\nxx = gets.split.map(&:to_i)\n\nmin0=0\nmin1=0\n\n0.upto(nn-1) do |n|\n t = xx[n]\n min0+=1 if t%2==0\n min1+=1 if (t+1)%2==0\nend\n\nputs min0 0\n amt = [d, a[t]].min\n d -= amt\n a[t] -= amt\n cost += c[t] * amt\n end\n while d > 0\n while cheapest < n && a[sortedc[cheapest][0]] == 0\n cheapest += 1\n end\n if cheapest == n\n cost = 0\n break\n end\n t = sortedc[cheapest][0]\n amt = [d, a[t]].min\n d -= amt\n a[t] -= amt\n cost += c[t] * amt\n end\n if DBG\n print a\n puts \"\"\n end\n puts \"#{cost}\"\n #totalcost += cost\nend\n"}, {"source_code": "n,m=gets.split.map(&:to_i)\n$stdout.sync = true\na=gets.split.map(&:to_i)\nc=gets.split.map(&:to_i)\nM=[]\nn.times{|i|M<0&&i0\n}"}, {"source_code": "s = (gets.chomp).split(' ').map(&:to_i)\nn = s[0]\nm = s[1]\nt = Array.new(n)\nost = (gets.chomp).split(' ').map(&:to_i)\ncost = (gets.chomp).split(' ').map(&:to_i)\n(0..n-1).each do |i|\n t[i] = [cost[i], i]\nend\nt.sort_by!{ |x| x[0] }\npos = 0\nm.times do |p|\n s = (gets.chomp).split(' ').map(&:to_i)\n a = s[0]\n b = s[1]\n ans = 0\n ans += cost[a-1] * [b, ost[a-1]].min()\n ost[a-1] -= b\n if ost[a-1] <= 0\n temp = ost[a-1].abs()\n ost[a-1] = 0\n\n while temp > 0 do\n if pos == n && temp > 0\n ans = 0\n break\n end\n tt = temp\n temp -= [ost[t[pos][1]], tt].min()\n ans += t[pos][0] * [ost[t[pos][1]], tt].min()\n ost[t[pos][1]] -= [ost[t[pos][1]], tt].min()\n if ost[t[pos][1]] == 0\n pos += 1\n end\n end\n end\n puts ans\nend\n"}, {"source_code": "if File.exists?(\"input\")\n $stdin = File.open(\"input\")\n $stdout = File.open(\"output\",\"w\")\nend\n\nnm = gets.chomp.split(\" \").map(&:to_i)\nn = nm[0]\nm = nm[1]\namount = gets.chomp.split(\" \").map(&:to_i)\ncost = gets.chomp.split(\" \").map(&:to_i)\norders = []\nm.times do\n\torders.push gets.chomp.split(\" \").map(&:to_i)\nend\n\ncostin = []\n\n(0..n-1).each do |i|\n\tcostin.push [cost[i],i]\nend\n\ncostin.sort_by!{|x| [x[0],x[1]]}\n\ncheapestindex = 0\n\norders.each do |ord|\n\ttype = ord[0]-1\n\tnum = ord[1]\n\n\ttotalcost = 0\n\n\tif cheapestindex==n\n\t\tputs totalcost\n\t\tnext\n\tend\n\n\tleft = amount[type]\n\n\tif(num<=left)\n\t\tamount[type]-=num\n\t\ttotalcost+=num*cost[type]\n\t\tnum=0\n\telse\n\t\ttotalcost+=left*cost[type]\t\t\n\t\tamount[type]=0\n\t\tnum-=left\n\t\twhile(num>0)\n\t\t\tnextdishind = costin[cheapestindex][1]\n\t\t\tleft = amount[nextdishind]\n\n\t\t\tif(left<1)\n\t\t\t\tcheapestindex+=1\n\t\t\t\tif cheapestindex==n\n\t\t\t\t\tbreak\n\t\t\t\tend\n\t\t\t\tnextdishind = costin[cheapestindex][1]\n\t\t\t\tleft = amount[nextdishind]\n\t\t\tend\n\n\t\t\tif(num<=left)\n\t\t\t\tamount[nextdishind]-=num\n\t\t\t\ttotalcost+=num*cost[nextdishind]\n\t\t\t\tnum=0\n\t\t\telse\n\t\t\t\ttotalcost+=left*cost[nextdishind]\n\t\t\t\tamount[nextdishind]=0\n\t\t\t\tnum-=left\n\t\t\tend\n\t\tend\n\tend\n\n#\tp amount\n\t\n\tif num>0\n\t\tputs 0\n\telse\n\t\tputs totalcost\n\tend\nend\n"}], "negative_code": [{"source_code": "n,m=gets.split.map(&:to_i)\n$stdout.sync = true\na=gets.split.map(&:to_i)\nc=gets.split.map(&:to_i)\nM=[]\nn.times{|i|M<0&&i0\n}"}, {"source_code": "n,m=gets.split.map(&:to_i)\n$stdout.sync = true\na=gets.split.map(&:to_i)\nc=gets.split.map(&:to_i)\nM=[]\nn.times{|i|M<0&&i0\n\tprint \"0\\n\" if d==0\n}"}], "src_uid": "2553ffea6d74fded12128e9db0db85dc"} {"nl": {"description": "Alice guesses the strings that Bob made for her.At first, Bob came up with the secret string $$$a$$$ consisting of lowercase English letters. The string $$$a$$$ has a length of $$$2$$$ or more characters. Then, from string $$$a$$$ he builds a new string $$$b$$$ and offers Alice the string $$$b$$$ so that she can guess the string $$$a$$$.Bob builds $$$b$$$ from $$$a$$$ as follows: he writes all the substrings of length $$$2$$$ of the string $$$a$$$ in the order from left to right, and then joins them in the same order into the string $$$b$$$.For example, if Bob came up with the string $$$a$$$=\"abac\", then all the substrings of length $$$2$$$ of the string $$$a$$$ are: \"ab\", \"ba\", \"ac\". Therefore, the string $$$b$$$=\"abbaac\".You are given the string $$$b$$$. Help Alice to guess the string $$$a$$$ that Bob came up with. It is guaranteed that $$$b$$$ was built according to the algorithm given above. It can be proved that the answer to the problem is unique.", "input_spec": "The first line contains a single positive integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases in the test. Then $$$t$$$ test cases follow. Each test case consists of one line in which the string $$$b$$$ is written, consisting of lowercase English letters ($$$2 \\le |b| \\le 100$$$) — the string Bob came up with, where $$$|b|$$$ is the length of the string $$$b$$$. It is guaranteed that $$$b$$$ was built according to the algorithm given above.", "output_spec": "Output $$$t$$$ answers to test cases. Each answer is the secret string $$$a$$$, consisting of lowercase English letters, that Bob came up with.", "sample_inputs": ["4\nabbaac\nac\nbccddaaf\nzzzzzzzzzz"], "sample_outputs": ["abac\nac\nbcdaf\nzzzzzz"], "notes": "NoteThe first test case is explained in the statement.In the second test case, Bob came up with the string $$$a$$$=\"ac\", the string $$$a$$$ has a length $$$2$$$, so the string $$$b$$$ is equal to the string $$$a$$$.In the third test case, Bob came up with the string $$$a$$$=\"bcdaf\", substrings of length $$$2$$$ of string $$$a$$$ are: \"bc\", \"cd\", \"da\", \"af\", so the string $$$b$$$=\"bccddaaf\"."}, "positive_code": [{"source_code": "gets.to_i.times do\n\ts = gets.chomp\n\tn = s.size\n\tputs (0...n / 2).map { s[_1 * 2] }.join + s[-1]\nend\n"}, {"source_code": "n = gets.to_i\nn.times do\n\tb = gets.chomp\n\tm = b.size\n\ta = b[0]\n\t(b.size/2-1).times do |i|\n\t\ta += b[1+i*2]\n\tend\n\ta += b[-1]\n\tputs a\nend\n"}, {"source_code": "\ngets.chomp.to_i.times do\n\n str = gets.chomp\n i=1\n a=str[0]\n while i < str.length-1\n a+= str[i] if(i%2!=1)\n i+=1\n end\n\n puts a+str[-1]\nend\n"}, {"source_code": "t = gets.to_i\nt.times do\n\tb = gets.chomp\n\ta = b[0]\n\tfor i in 1...b.length\n\t\ta += b[i] if i%2 == 1\n\tend\n\tputs a\nend"}, {"source_code": "n = gets.chomp.to_i\n(0...n).each do |i|\n arr = gets.chomp.split(\"\")\n arsize = arr.size\n print arr[0]\n (1...arsize).each do |j|\n if j % 2 == 0 && arr[j] == arr[j-1] \n next\n else \n print arr[j]\n end\n end\n print \"\\n\"\nend"}, {"source_code": "data_lines = gets.chomp\nresult = []\n(1..data_lines.to_i).each do\n\tb = gets.chomp\n\tif b.length < 4\n\t\tresult.push b\n\t\tnext\n\tend\n\ta = b[0]\n\t(1..(b.length-2)).step(2).each do |index|\n\t\ta += b[index]\n\tend\n\ta += b[b.length-1]\n\tresult.push a\nend\nresult.each do |res|\n\tputs res\nend"}, {"source_code": "def solve(str)\n\treturn str if str.size < 3\n\n\tcompact_mid = str[1..-2].chars.each_slice(2).map(&:first).join\n\n\t\"%s%s%s\" % [str[0], compact_mid,str[-1]]\nend\n\ninputs = ARGF.each_line.drop(1).map(&:strip).to_a\n\ninputs.each do |i|\n\tputs solve(i)\nend"}, {"source_code": "n = gets.chomp.to_i\nn.times do\n\ts = gets.chomp\n\tif s.length == 2 \n\t\tputs s\n\t\tnext\n\tend\n\tprint s[0]\n\ti = 1\n\twhile i < s.length - 1\n\t\tprint s[i]\n\t\ti += 2\n\tend\n\tputs s[-1]\nend"}, {"source_code": "n=gets.chomp.to_i\nn.times do \n inpstr = gets.chomp.split('')\n result = inpstr[0]\n (1..inpstr.size).step(2).each do |index|\n result << inpstr[index]\n end\n puts result\nend\n"}, {"source_code": "gets.to_i.times do\n s = gets.chomp\n puts (0.step(s.length, 2).to_a + [s.length - 1]).map { |i| s[i] }.join\nend\n"}], "negative_code": [{"source_code": "n=gets.chomp.to_i\nn.times do \n inpstr = gets.chomp.split('')\n result = inpstr[0]\n (1..inpstr.size).step(2).each do |index|\n result << inpstr[index]\n end\n p result\nend\n"}], "src_uid": "ac77e2e6c86b5528b401debe9f68fc8e"} {"nl": {"description": "You are given a chessboard of size $$$n \\times n$$$. It is filled with numbers from $$$1$$$ to $$$n^2$$$ in the following way: the first $$$\\lceil \\frac{n^2}{2} \\rceil$$$ numbers from $$$1$$$ to $$$\\lceil \\frac{n^2}{2} \\rceil$$$ are written in the cells with even sum of coordinates from left to right from top to bottom. The rest $$$n^2 - \\lceil \\frac{n^2}{2} \\rceil$$$ numbers from $$$\\lceil \\frac{n^2}{2} \\rceil + 1$$$ to $$$n^2$$$ are written in the cells with odd sum of coordinates from left to right from top to bottom. The operation $$$\\lceil\\frac{x}{y}\\rceil$$$ means division $$$x$$$ by $$$y$$$ rounded up.For example, the left board on the following picture is the chessboard which is given for $$$n=4$$$ and the right board is the chessboard which is given for $$$n=5$$$. You are given $$$q$$$ queries. The $$$i$$$-th query is described as a pair $$$x_i, y_i$$$. The answer to the $$$i$$$-th query is the number written in the cell $$$x_i, y_i$$$ ($$$x_i$$$ is the row, $$$y_i$$$ is the column). Rows and columns are numbered from $$$1$$$ to $$$n$$$.", "input_spec": "The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 \\le n \\le 10^9$$$, $$$1 \\le q \\le 10^5$$$) — the size of the board and the number of queries. The next $$$q$$$ lines contain two integers each. The $$$i$$$-th line contains two integers $$$x_i, y_i$$$ ($$$1 \\le x_i, y_i \\le n$$$) — description of the $$$i$$$-th query.", "output_spec": "For each query from $$$1$$$ to $$$q$$$ print the answer to this query. The answer to the $$$i$$$-th query is the number written in the cell $$$x_i, y_i$$$ ($$$x_i$$$ is the row, $$$y_i$$$ is the column). Rows and columns are numbered from $$$1$$$ to $$$n$$$. Queries are numbered from $$$1$$$ to $$$q$$$ in order of the input.", "sample_inputs": ["4 5\n1 1\n4 4\n4 3\n3 2\n2 4", "5 4\n2 1\n4 2\n3 3\n3 4"], "sample_outputs": ["1\n8\n16\n13\n4", "16\n9\n7\n20"], "notes": "NoteAnswers to the queries from examples are on the board in the picture from the problem statement."}, "positive_code": [{"source_code": "N, Q = gets.split.map(&:to_i)\nQ.times do\n x, y = gets.split.map(&:to_i)\n ans = 0\n if (x+y)%2 == 0\n ans += (x-1)/2 * N\n ans += (N+1)/2 if x%2==0\n ans += (y+1)/2\n else\n ans += (N*N+1)/2\n ans += (x-1)/2 * N\n ans += N/2 if x%2==0\n ans += (y+1)/2\n end\n puts ans\nend"}, {"source_code": "n,q =gets.strip.split.map(&:to_i)\n\nq.times do\n r,c =gets.strip.split.map(&:to_i)\n ans=0\n if n%2==0\n if (r+c)%2==0\n pr = n/2\n ans += ((r-1)*pr)\n ans += ((c/2)+(r%2==1 ? 1 : 0))\n else\n pr = n/2\n ans += (n*pr)\n ans += ((r-1)*pr)\n ans += ((c/2)+(r%2==0 ? 1 : 0))\n end\n else\n if (r+c)%2==0\n orc=(n/2)+1\n erc=n/2\n if r%2==1\n ans += (r/2)*(orc+erc)\n ans += ((c/2)+1)\n else\n ans += (((r-1)/2)*(orc+erc) + orc)\n ans += (c/2)\n end\n else\n orc=(n/2)+1\n erc=n/2\n ans += (((n-1)/2)*(orc+erc) + orc)\n erc=(n/2)+1\n orc=n/2\n if r%2==1\n ans += (r/2)*(orc+erc)\n ans += (c/2)\n else\n ans += (((r-1)/2)*(orc+erc) + orc)\n ans += ((c/2)+1)\n end\n end\n end\n puts ans\nend"}], "negative_code": [], "src_uid": "3c48123f326fb79ce2e4e17e1e0765f8"} {"nl": {"description": "Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School.In his favorite math class, the teacher taught him the following interesting definitions.A parenthesis sequence is a string, containing only characters \"(\" and \")\".A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters \"1\" and \"+\" between the original characters of the sequence. For example, parenthesis sequences \"()()\", \"(())\" are correct (the resulting expressions are: \"(1+1)+(1+1)\", \"((1+1)+1)\"), while \")(\" and \")\" are not. Note that the empty string is a correct parenthesis sequence by definition.We define that $$$|s|$$$ as the length of string $$$s$$$. A strict prefix $$$s[1\\dots l]$$$ $$$(1\\leq l< |s|)$$$ of a string $$$s = s_1s_2\\dots s_{|s|}$$$ is string $$$s_1s_2\\dots s_l$$$. Note that the empty string and the whole string are not strict prefixes of any string by the definition.Having learned these definitions, he comes up with a new problem. He writes down a string $$$s$$$ containing only characters \"(\", \")\" and \"?\". And what he is going to do, is to replace each of the \"?\" in $$$s$$$ independently by one of \"(\" and \")\" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence.After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable.", "input_spec": "The first line contains a single integer $$$|s|$$$ ($$$1\\leq |s|\\leq 3 \\cdot 10^5$$$), the length of the string. The second line contains a string $$$s$$$, containing only \"(\", \")\" and \"?\".", "output_spec": "A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing \":(\" (without the quotes).", "sample_inputs": ["6\n(?????", "10\n(???(???(?"], "sample_outputs": ["(()())", ":("], "notes": "NoteIt can be proved that there is no solution for the second sample, so print \":(\"."}, "positive_code": [{"source_code": "n = gets.chomp.to_i\ns = gets.chomp\na = 0\nb = 0\nn.times do |i|\n if s[i] == \"(\"\n a += 1\n elsif s[i] == \")\"\n b += 1\n end\nend\nq = n - (a + b)\nc = a - b\nif s[-1] == \"(\" || (q - c).odd?\n puts \":(\"\nelse\n y = (q + c) / 2\n x = y - c\n ans = []\n sum = 0\n n.times do |i|\n if s[i] == \"?\"\n if x != 0\n ans << \"(\"\n x -= 1\n sum += 1\n else\n ans << \")\"\n sum -= 1\n end\n elsif s[i] == \"(\"\n sum += 1\n ans << s[i]\n else\n sum -= 1\n ans << s[i]\n end\n if sum == 0 && i < n - 1\n puts \":(\"\n exit\n end\n end\n if sum == 0\n puts ans.join(\"\")\n else\n puts \":(\"\n end\nend"}], "negative_code": [{"source_code": "n = gets.chomp.to_i\ns = gets.chomp\na = 0\nb = 0\nn.times do |i|\n if s[i] == \"(\"\n a += 1\n elsif s[i] == \")\"\n b += 1\n end\nend\nq = n - (a + b)\nc = a - b\nif s[-1] == \"(\" || (q - c).odd?\n puts \":(\"\nelse\n y = (q + c) / 2\n x = y - c\n ans = []\n sum = 0\n n.times do |i|\n if s[i] == \"?\"\n if x != 0\n ans << \"(\"\n x -= 1\n sum += 1\n else\n ans << \")\"\n sum -= 1\n end\n elsif s[i] == \"(\"\n sum += 1\n ans << s[i]\n else\n sum -= 1\n ans << s[i]\n end\n if sum == 0 && i < n - 1\n puts \":(\"\n exit\n end\n end\n puts ans.join(\"\")\nend"}], "src_uid": "e03bec836d52fe4784a5b4c62ab5b2c8"} {"nl": {"description": "You have a sequence $$$a$$$ with $$$n$$$ elements $$$1, 2, 3, \\dots, k - 1, k, k - 1, k - 2, \\dots, k - (n - k)$$$ ($$$k \\le n < 2k$$$).Let's call as inversion in $$$a$$$ a pair of indices $$$i < j$$$ such that $$$a[i] > a[j]$$$.Suppose, you have some permutation $$$p$$$ of size $$$k$$$ and you build a sequence $$$b$$$ of size $$$n$$$ in the following manner: $$$b[i] = p[a[i]]$$$.Your goal is to find such permutation $$$p$$$ that the total number of inversions in $$$b$$$ doesn't exceed the total number of inversions in $$$a$$$, and $$$b$$$ is lexicographically maximum.Small reminder: the sequence of $$$k$$$ integers is called a permutation if it contains all integers from $$$1$$$ to $$$k$$$ exactly once.Another small reminder: a sequence $$$s$$$ is lexicographically smaller than another sequence $$$t$$$, if either $$$s$$$ is a prefix of $$$t$$$, or for the first $$$i$$$ such that $$$s_i \\ne t_i$$$, $$$s_i < t_i$$$ holds (in the first position that these sequences are different, $$$s$$$ has smaller number than $$$t$$$).", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$k \\le n < 2k$$$; $$$1 \\le k \\le 10^5$$$) — the length of the sequence $$$a$$$ and its maximum. It's guaranteed that the total sum of $$$k$$$ over test cases doesn't exceed $$$10^5$$$.", "output_spec": "For each test case, print $$$k$$$ integers — the permutation $$$p$$$ which maximizes $$$b$$$ lexicographically without increasing the total number of inversions. It can be proven that $$$p$$$ exists and is unique.", "sample_inputs": ["4\n1 1\n2 2\n3 2\n4 3"], "sample_outputs": ["1 \n1 2 \n2 1 \n1 3 2"], "notes": "NoteIn the first test case, the sequence $$$a = [1]$$$, there is only one permutation $$$p = [1]$$$.In the second test case, the sequence $$$a = [1, 2]$$$. There is no inversion in $$$a$$$, so there is only one permutation $$$p = [1, 2]$$$ which doesn't increase the number of inversions.In the third test case, $$$a = [1, 2, 1]$$$ and has $$$1$$$ inversion. If we use $$$p = [2, 1]$$$, then $$$b = [p[a[1]], p[a[2]], p[a[3]]] = [2, 1, 2]$$$ and also has $$$1$$$ inversion.In the fourth test case, $$$a = [1, 2, 3, 2]$$$, and since $$$p = [1, 3, 2]$$$ then $$$b = [1, 3, 2, 3]$$$. Both $$$a$$$ and $$$b$$$ have $$$1$$$ inversion and $$$b$$$ is the lexicographically maximum."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n n, k = gets.split.map &:to_i\r\n (1...2 * k - n).each do |i|\r\n print i, \" \"\r\n end\r\n (2 * k - n..k).reverse_each do |i|\r\n print i, \" \"\r\n end\r\n puts\r\nend"}, {"source_code": "def inv(arr)\r\n i1 = i2 = 0\r\n res = 0\r\n while i1 < arr.size - 1\r\n while i2 < arr.size - 1\r\n i2 += 1\r\n res += 1 if arr[i2] < arr[i1]\r\n end\r\n i1 += 1\r\n i2 = i1\r\n end\r\n res \r\nend\r\n\r\nt = gets.chomp.to_i\r\nt.times do\r\n n, k = gets.chomp.split(' ').map(&:to_i)\r\n max_n = k * 2 - 1\r\n # puts \"max_n = #{max_n.to_s}\"\r\n diff = max_n - n\r\n # puts \"DIFF = #{diff.to_s}\"\r\n arr = (1..(diff)).to_a\r\n # puts \"ARR = #{arr.to_s}\"\r\n arr << k\r\n # puts \"ARR = #{arr.to_s}\"\r\n while arr.size < k\r\n arr << arr[-1] - 1\r\n end\r\n # puts \"ARR = #{arr.to_s}\"\r\n puts arr.join(' ')\r\nend"}, {"source_code": "gets.to_i.times do\r\n n,k=gets.split.map(&:to_i)\r\n l=[*1...2*k-n]+[*2*k-n..k].reverse\r\n puts l.join(\" \")\r\nend\r\n"}], "negative_code": [], "src_uid": "67de9506ac2458ee67346bae1a9e3926"} {"nl": {"description": "There are three sticks with integer lengths $$$l_1, l_2$$$ and $$$l_3$$$.You are asked to break exactly one of them into two pieces in such a way that: both pieces have positive (strictly greater than $$$0$$$) integer length; the total length of the pieces is equal to the original length of the stick; it's possible to construct a rectangle from the resulting four sticks such that each stick is used as exactly one of its sides. A square is also considered a rectangle.Determine if it's possible to do that.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of testcases. The only line of each testcase contains three integers $$$l_1, l_2, l_3$$$ ($$$1 \\le l_i \\le 10^8$$$) — the lengths of the sticks.", "output_spec": "For each testcase, print \"YES\" if it's possible to break one of the sticks into two pieces with positive integer length in such a way that it's possible to construct a rectangle from the resulting four sticks. Otherwise, print \"NO\". You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as a positive answer).", "sample_inputs": ["4\n6 1 5\n2 5 2\n2 4 2\n5 5 4"], "sample_outputs": ["YES\nNO\nYES\nYES"], "notes": "NoteIn the first testcase, the first stick can be broken into parts of length $$$1$$$ and $$$5$$$. We can construct a rectangle with opposite sides of length $$$1$$$ and $$$5$$$.In the second testcase, breaking the stick of length $$$2$$$ can only result in sticks of lengths $$$1, 1, 2, 5$$$, which can't be made into a rectangle. Breaking the stick of length $$$5$$$ can produce results $$$2, 3$$$ or $$$1, 4$$$ but neither of them can't be put into a rectangle.In the third testcase, the second stick can be broken into parts of length $$$2$$$ and $$$2$$$. The resulting rectangle has opposite sides $$$2$$$ and $$$2$$$ (which is a square).In the fourth testcase, the third stick can be broken into parts of length $$$2$$$ and $$$2$$$. The resulting rectangle has opposite sides $$$2$$$ and $$$5$$$."}, "positive_code": [{"source_code": "T = gets.to_i\nT.times do\n l = gets.split.map(&:to_i)\n l.sort!\n if (l[0] + l[1] == l[2]) or (l[0] == l[1] and l[2].even?) or (l[1] == l[2] and l[0].even?)\n puts \"YES\"\n else\n puts \"NO\"\n end\nend"}], "negative_code": [], "src_uid": "a4a69a5fbf35781ff0849332a45566ca"} {"nl": {"description": "There are n cities and n - 1 roads in the Seven Kingdoms, each road connects two cities and we can reach any city from any other by the roads.Theon and Yara Greyjoy are on a horse in the first city, they are starting traveling through the roads. But the weather is foggy, so they can’t see where the horse brings them. When the horse reaches a city (including the first one), it goes to one of the cities connected to the current city. But it is a strange horse, it only goes to cities in which they weren't before. In each such city, the horse goes with equal probabilities and it stops when there are no such cities. Let the length of each road be 1. The journey starts in the city 1. What is the expected length (expected value of length) of their journey? You can read about expected (average) value by the link https://en.wikipedia.org/wiki/Expected_value.", "input_spec": "The first line contains a single integer n (1 ≤ n ≤ 100000) — number of cities. Then n - 1 lines follow. The i-th line of these lines contains two integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — the cities connected by the i-th road. It is guaranteed that one can reach any city from any other by the roads.", "output_spec": "Print a number — the expected length of their journey. The journey starts in the city 1. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .", "sample_inputs": ["4\n1 2\n1 3\n2 4", "5\n1 2\n1 3\n3 4\n2 5"], "sample_outputs": ["1.500000000000000", "2.000000000000000"], "notes": "NoteIn the first sample, their journey may end in cities 3 or 4 with equal probability. The distance to city 3 is 1 and to city 4 is 2, so the expected length is 1.5.In the second sample, their journey may end in city 4 or 5. The distance to the both cities is 2, so the expected length is 2."}, "positive_code": [{"source_code": "N = gets.to_i\nG = Array.new(N) { [] }\n(N - 1).times do\n u, v = gets.split.map &:to_i\n u, v = u - 1, v - 1\n G[u] << v\n G[v] << u\nend\n\ndef dfs u, fa, d, prob, leng, ans\n=begin\n chc = G[u].size\n chc -= 1 if fa != -1\n G[u].each do |v|\n next if v == fa\n prob[v] = prob[u] / chc\n leng[v] = leng[u] + 1\n dfs v, u, d + 1, prob, leng, ans\n end\n ans[0] += prob[u] * leng[u] if chc == 0\n=end\n fun = [[u, -1]]\n until fun.empty?\n u, fa = fun.shift\n chc = G[u].size\n chc -= 1 if fa != -1\n G[u].each do |v|\n next if v == fa\n prob[v] = prob[u] /chc\n leng[v] = leng[u] + 1\n fun.unshift [v, u]\n end\n ans[0] += prob[u] * leng[u] if chc == 0\n end\nend\n\nprob = [1.0] * N\nleng = [0] * N\nans = [0.0]\n\ndfs 0, -1, 0, prob, leng, ans\n\np ans[0]\n"}], "negative_code": [], "src_uid": "adaae163882b064bf7257e82e8832ffb"} {"nl": {"description": "You are given a string $$$s$$$. You need to find two non-empty strings $$$a$$$ and $$$b$$$ such that the following conditions are satisfied: Strings $$$a$$$ and $$$b$$$ are both subsequences of $$$s$$$. For each index $$$i$$$, character $$$s_i$$$ of string $$$s$$$ must belong to exactly one of strings $$$a$$$ or $$$b$$$. String $$$a$$$ is lexicographically minimum possible; string $$$b$$$ may be any possible string. Given string $$$s$$$, print any valid $$$a$$$ and $$$b$$$.Reminder:A string $$$a$$$ ($$$b$$$) is a subsequence of a string $$$s$$$ if $$$a$$$ ($$$b$$$) can be obtained from $$$s$$$ by deletion of several (possibly, zero) elements. For example, \"dores\", \"cf\", and \"for\" are subsequences of \"codeforces\", while \"decor\" and \"fork\" are not.A string $$$x$$$ is lexicographically smaller than a string $$$y$$$ if and only if one of the following holds: $$$x$$$ is a prefix of $$$y$$$, but $$$x \\ne y$$$; in the first position where $$$x$$$ and $$$y$$$ differ, the string $$$x$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$y$$$. ", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 1000$$$). Description of the test cases follows. The first and only line of each test case contains one string $$$s$$$ ($$$2 \\le |s| \\le 100$$$ where $$$|s|$$$ means the length of $$$s$$$). String $$$s$$$ consists of lowercase Latin letters.", "output_spec": "For each test case, print the strings $$$a$$$ and $$$b$$$ that satisfy the given conditions. If there are multiple answers, print any.", "sample_inputs": ["3\nfc\naaaa\nthebrightboiler"], "sample_outputs": ["c f\na aaa\nb therightboiler"], "notes": "NoteIn the first test case, there are only two choices: either $$$a =$$$ f and $$$b = $$$ c or $$$a = $$$ c and $$$b = $$$ f. And $$$a = $$$c is lexicographically smaller than $$$a = $$$ f.In the second test case, a is the only character in the string.In the third test case, it can be proven that b is the lexicographically smallest subsequence of $$$s$$$. The second string can be of two variants; one of them is given here."}, "positive_code": [{"source_code": "STDIN.read\n .split(\"\\n\")\n .drop(1)\n .each do |line|\n c = line.split(\"\").sort.first\n puts c + \" \" + line.sub(c, \"\")\n end\n\n"}], "negative_code": [], "src_uid": "4a58039c5171597ecf78837e9db1d71d"} {"nl": {"description": "Polycarp has a poor memory. Each day he can remember no more than $$$3$$$ of different letters. Polycarp wants to write a non-empty string of $$$s$$$ consisting of lowercase Latin letters, taking minimum number of days. In how many days will he be able to do it?Polycarp initially has an empty string and can only add characters to the end of that string.For example, if Polycarp wants to write the string lollipops, he will do it in $$$2$$$ days: on the first day Polycarp will memorize the letters l, o, i and write lolli; On the second day Polycarp will remember the letters p, o, s, add pops to the resulting line and get the line lollipops. If Polycarp wants to write the string stringology, he will do it in $$$4$$$ days: in the first day will be written part str; on day two will be written part ing; on the third day, part of olog will be written; on the fourth day, part of y will be written. For a given string $$$s$$$, print the minimum number of days it will take Polycarp to write it.", "input_spec": "The first line of input data contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. Each test case consists of a non-empty string $$$s$$$ consisting of lowercase Latin letters (the length of the string $$$s$$$ does not exceed $$$2 \\cdot 10^5$$$) — the string Polycarp wants to construct. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, print a single number — minimum number of days it will take Polycarp to write the string $$$s$$$ from memory.", "sample_inputs": ["6\n\nlollipops\n\nstringology\n\nabracadabra\n\ncodeforces\n\ntest\n\nf"], "sample_outputs": ["2\n4\n3\n4\n1\n1"], "notes": null}, "positive_code": [{"source_code": "#input the number of test cases\nt = gets.chomp.to_i\n\n#list of results\nresults = []\n\n#solve\nfor i in 1..t\n str = gets.chomp\n \n #solving code\n\tresult = 0\n letterAlreadyToday = []\n for j in str.split \"\"\n\t\tif letterAlreadyToday.include? j\n\t\t\tnext\n\t\telse\n\t\t\tif letterAlreadyToday.length == 3\n\t\t\t\tletterAlreadyToday = []\n\t\t\tend\n\t\tend\n\t\tif letterAlreadyToday.length < 1\n\t\t\tresult += 1\n\t\tend\n\t\tletterAlreadyToday.push j\n end\n\tresults.push result\nend\n\nputs results"}, {"source_code": "require \"set\"\n\nn = gets.to_i\n\nn.times do\n s = gets.chomp\n\n set = Set.new\n days = 1\n i = 0\n\n while (i < s.size)\n char = s[i]\n if !set.include?(char) && set.size == 3\n set = Set.new\n days += 1\n else\n set.add(char)\n i += 1\n end\n end\n\n p days\nend\n"}, {"source_code": "t = gets.to_i\nt.times {\n s = gets.chomp\n d = 1\n chrs = []\n while !s.empty?\n c = s[0]\n s = s[1..-1]\n if !(chrs.index c)\n if chrs.size == 3\n d += 1\n chrs = []\n end\n chrs += [c]\n end\n end\n puts d\n}\n"}], "negative_code": [], "src_uid": "bd9da9081902a87c61591fb58fcecfe3"} {"nl": {"description": "Being tired of participating in too many Codeforces rounds, Gildong decided to take some rest in a park. He sat down on a bench, and soon he found two rabbits hopping around. One of the rabbits was taller than the other.He noticed that the two rabbits were hopping towards each other. The positions of the two rabbits can be represented as integer coordinates on a horizontal line. The taller rabbit is currently on position $$$x$$$, and the shorter rabbit is currently on position $$$y$$$ ($$$x \\lt y$$$). Every second, each rabbit hops to another position. The taller rabbit hops to the positive direction by $$$a$$$, and the shorter rabbit hops to the negative direction by $$$b$$$. For example, let's say $$$x=0$$$, $$$y=10$$$, $$$a=2$$$, and $$$b=3$$$. At the $$$1$$$-st second, each rabbit will be at position $$$2$$$ and $$$7$$$. At the $$$2$$$-nd second, both rabbits will be at position $$$4$$$.Gildong is now wondering: Will the two rabbits be at the same position at the same moment? If so, how long will it take? Let's find a moment in time (in seconds) after which the rabbits will be at the same point.", "input_spec": "Each test contains one or more test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 1000$$$). Each test case contains exactly one line. The line consists of four integers $$$x$$$, $$$y$$$, $$$a$$$, $$$b$$$ ($$$0 \\le x \\lt y \\le 10^9$$$, $$$1 \\le a,b \\le 10^9$$$) — the current position of the taller rabbit, the current position of the shorter rabbit, the hopping distance of the taller rabbit, and the hopping distance of the shorter rabbit, respectively.", "output_spec": "For each test case, print the single integer: number of seconds the two rabbits will take to be at the same position. If the two rabbits will never be at the same position simultaneously, print $$$-1$$$.", "sample_inputs": ["5\n0 10 2 3\n0 10 3 3\n900000000 1000000000 1 9999999\n1 2 1 1\n1 3 1 1"], "sample_outputs": ["2\n-1\n10\n-1\n1"], "notes": "NoteThe first case is explained in the description.In the second case, each rabbit will be at position $$$3$$$ and $$$7$$$ respectively at the $$$1$$$-st second. But in the $$$2$$$-nd second they will be at $$$6$$$ and $$$4$$$ respectively, and we can see that they will never be at the same position since the distance between the two rabbits will only increase afterward."}, "positive_code": [{"source_code": "num = gets.to_i\nwhile num > 0\n myArray = gets.chomp.split().map { |e| e.to_i }\n x = myArray[0]\n y = myArray[1]\n a = myArray[2]\n b = myArray[3]\n result = (y-x)/(a+b)\n if (y-x) % (a+b) == 0\n puts result\n else\n puts '-1'\n end\n num = num -1\nend"}, {"source_code": "\ndef binary_search(x, y, a, b)\n first = 0\n last = (y - x)/[a,b].max\n\n while first <= last\n i = (first + last) / 2\n\n xx = x + i*a\n yy = y - i*b\n\n if xx == yy\n return i\n elsif xx > yy\n last = i - 1\n elsif yy > xx\n first = i + 1\n else\n return -1\n end\n end\n -1\nend\n\nn = gets.strip.to_i\nresults = []\n\nn.times do\n arr = gets.strip.split.map(&:to_i)\n x, y, a, b = *arr\n results << binary_search(x, y, a, b)\nend\n\nn.times do |x|\n puts results[x]\nend\n"}, {"source_code": "class Rabbit\n \n def initialize(pos, jump_size)\n @pos = pos\n @jump_size = jump_size\n end\n \n def jump_size()\n @jump_size \n end\n \n def position()\n @pos \n end\n \nend\n\ndef read_input()\n \n input = STDIN.gets()\n \n input = input.split(' ').map(&:to_i)\n \n raise \"Invalid input\" if input.size != 4\n \n return Rabbit.new(input[0], input[2]), Rabbit.new(input[1], input[3])\n \nend\n\ndef calc_rabbits_jumps()\n \n rabbit1, rabbit2 = read_input()\n \n distance = rabbit2.position - rabbit1.position\n speed = rabbit1.jump_size + rabbit2.jump_size\n point = distance / speed.to_f\n \n if point % 1 == 0\n puts point.to_i\n else\n puts -1\n end\n \nend\n\ncnt = Integer(STDIN.gets())\n\ncnt.times {\n calc_rabbits_jumps() \n}\n"}, {"source_code": "# frozen_string_literal: true\n\n@data = $stdin.read.split\n\ndef next_token\n @data.shift\nend\n\ndef next_int\n next_token.to_i\nend\n\nt = next_int\n\nt.times do |_i|\n x = next_int\n y = next_int\n a = next_int\n b = next_int\n\n if (y - x) % (a + b) == 0\n t = (y - x) / (a + b)\n puts t\n else\n puts -1\n end\nend\n"}, {"source_code": "t = gets.to_i\nz = []\n(0..t - 1).each { |i|\n str = gets\n z[i] = str\n counter = 0\n bloc = z[i].split(' ')\n a = bloc[0].to_i; b = bloc[1].to_i; c = bloc[2].to_i; d = bloc[3].to_i\n if (b - a) % (c + d) != 0\n puts '-1'\n else\n puts (b - a) / (c + d)\n end\n}\n"}, {"source_code": "n = gets.to_i\nn.times {\n x, y, a, b = gets.split.map(&:to_i)\n if (y-x) % (a + b) > 0\n puts \"-1\"\n else\n puts (y-x)/(a+b)\n end\n}\n"}, {"source_code": "1.upto(gets.to_i) do\n x, y, a, b = gets.split(' ').map(&:to_i)\n\n delta = y-x\n\n puts delta.modulo(a+b).zero? ? delta.div(a+b) : -1\nend\n"}], "negative_code": [], "src_uid": "9afcf090806cc9c3b87120b1b61f8f17"} {"nl": {"description": "NIT, the cleaver, is new in town! Thousands of people line up to orz him. To keep his orzers entertained, NIT decided to let them solve the following problem related to $$$\\operatorname{or} z$$$. Can you solve this problem too?You are given a 1-indexed array of $$$n$$$ integers, $$$a$$$, and an integer $$$z$$$. You can do the following operation any number (possibly zero) of times: Select a positive integer $$$i$$$ such that $$$1\\le i\\le n$$$. Then, simutaneously set $$$a_i$$$ to $$$(a_i\\operatorname{or} z)$$$ and set $$$z$$$ to $$$(a_i\\operatorname{and} z)$$$. In other words, let $$$x$$$ and $$$y$$$ respectively be the current values of $$$a_i$$$ and $$$z$$$. Then set $$$a_i$$$ to $$$(x\\operatorname{or}y)$$$ and set $$$z$$$ to $$$(x\\operatorname{and}y)$$$. Here $$$\\operatorname{or}$$$ and $$$\\operatorname{and}$$$ denote the bitwise operations OR and AND respectively.Find the maximum possible value of the maximum value in $$$a$$$ after any number (possibly zero) of operations.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 100$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$z$$$ ($$$1\\le n\\le 2000$$$, $$$0\\le z<2^{30}$$$). The second line of each test case contains $$$n$$$ integers $$$a_1$$$,$$$a_2$$$,$$$\\ldots$$$,$$$a_n$$$ ($$$0\\le a_i<2^{30}$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.", "output_spec": "For each test case, print one integer — the answer to the problem.", "sample_inputs": ["5\n\n2 3\n\n3 4\n\n5 5\n\n0 2 4 6 8\n\n1 9\n\n10\n\n5 7\n\n7 15 30 29 27\n\n3 39548743\n\n10293834 10284344 13635445"], "sample_outputs": ["7\n13\n11\n31\n48234367"], "notes": "NoteIn the first test case of the sample, one optimal sequence of operations is: Do the operation with $$$i=1$$$. Now $$$a_1$$$ becomes $$$(3\\operatorname{or}3)=3$$$ and $$$z$$$ becomes $$$(3\\operatorname{and}3)=3$$$. Do the operation with $$$i=2$$$. Now $$$a_2$$$ becomes $$$(4\\operatorname{or}3)=7$$$ and $$$z$$$ becomes $$$(4\\operatorname{and}3)=0$$$. Do the operation with $$$i=1$$$. Now $$$a_1$$$ becomes $$$(3\\operatorname{or}0)=3$$$ and $$$z$$$ becomes $$$(3\\operatorname{and}0)=0$$$. After these operations, the sequence $$$a$$$ becomes $$$[3,7]$$$, and the maximum value in it is $$$7$$$. We can prove that the maximum value in $$$a$$$ can never exceed $$$7$$$, so the answer is $$$7$$$.In the fourth test case of the sample, one optimal sequence of operations is: Do the operation with $$$i=1$$$. Now $$$a_1$$$ becomes $$$(7\\operatorname{or}7)=7$$$ and $$$z$$$ becomes $$$(7\\operatorname{and}7)=7$$$. Do the operation with $$$i=3$$$. Now $$$a_3$$$ becomes $$$(30\\operatorname{or}7)=31$$$ and $$$z$$$ becomes $$$(30\\operatorname{and}7)=6$$$. Do the operation with $$$i=5$$$. Now $$$a_5$$$ becomes $$$(27\\operatorname{or}6)=31$$$ and $$$z$$$ becomes $$$(27\\operatorname{and}6)=2$$$. "}, "positive_code": [{"source_code": "gets.to_i.times do\r\n n, z = gets.split.map(&:to_i)\r\n as = gets.split.map(&:to_i)\r\n p as.map { |a| a | z }.max\r\nend\r\n"}, {"source_code": "gets.to_i.times do\r\n n, z = gets.split.map(&:to_i)\r\n xs = gets.split.map(&:to_i)\r\n p xs.map { |x| x | z }.max\r\nend\r\n"}, {"source_code": "gets.to_i.times do\r\n n, z = gets.split.map(&:to_i)\r\n p gets.split.map(&:to_i).map{|x| x | z}.max\r\nend"}, {"source_code": "gets.to_i.times do\r\n n, z = gets.split.map(&:to_i)\r\n ans = 0\r\n gets.split.map(&:to_i).each do |x|\r\n ans = [ans, x | z].max\r\n end\r\n puts ans\r\nend"}, {"source_code": "# gets.to_i\r\n# gets.split.map(&:to_i)\r\n\r\ngets.to_i.times do\r\n n, z = gets.split.map(&:to_i)\r\n a = gets.split.map(&:to_i).sort.reverse\r\n\r\n num = 0\r\n ai = -1\r\n zi = 0\r\n n.times do |i|\r\n if (a[i] & z) > ai\r\n ai = a[i] | z\r\n zi = a[i] & z\r\n num = i\r\n end\r\n end\r\n\r\n ans = ai\r\n n.times do |i|\r\n next if i == num\r\n ai = a[i] | z\r\n zi &= a[i]\r\n ans = [ans, ai].max\r\n end\r\n puts ans\r\nend\r\n"}], "negative_code": [], "src_uid": "bb6168528e04156f68bde2ffc1ba828f"} {"nl": {"description": "A remote island chain contains n islands, labeled 1 through n. Bidirectional bridges connect the islands to form a simple cycle — a bridge connects islands 1 and 2, islands 2 and 3, and so on, and additionally a bridge connects islands n and 1. The center of each island contains an identical pedestal, and all but one of the islands has a fragile, uniquely colored statue currently held on the pedestal. The remaining island holds only an empty pedestal.The islanders want to rearrange the statues in a new order. To do this, they repeat the following process: First, they choose an island directly adjacent to the island containing an empty pedestal. Then, they painstakingly carry the statue on this island across the adjoining bridge and place it on the empty pedestal.Determine if it is possible for the islanders to arrange the statues in the desired order.", "input_spec": "The first line contains a single integer n (2 ≤ n ≤ 200 000) — the total number of islands. The second line contains n space-separated integers ai (0 ≤ ai ≤ n - 1) — the statue currently placed on the i-th island. If ai = 0, then the island has no statue. It is guaranteed that the ai are distinct. The third line contains n space-separated integers bi (0 ≤ bi ≤ n - 1) — the desired statues of the ith island. Once again, bi = 0 indicates the island desires no statue. It is guaranteed that the bi are distinct.", "output_spec": "Print \"YES\" (without quotes) if the rearrangement can be done in the existing network, and \"NO\" otherwise.", "sample_inputs": ["3\n1 0 2\n2 0 1", "2\n1 0\n0 1", "4\n1 2 3 0\n0 3 2 1"], "sample_outputs": ["YES", "YES", "NO"], "notes": "NoteIn the first sample, the islanders can first move statue 1 from island 1 to island 2, then move statue 2 from island 3 to island 1, and finally move statue 1 from island 2 to island 3.In the second sample, the islanders can simply move statue 1 from island 1 to island 2.In the third sample, no sequence of movements results in the desired position."}, "positive_code": [{"source_code": "n = gets.to_i - 1\na, b = 2.times.map{gets.split.map(&:to_i) - [0]}\nk = b.index(a[0])\nputs a[0...n-k] == b[k...n] && a[n-k...n] == b[0...k] ? \"YES\" : \"NO\""}, {"source_code": "\nislands = gets.chomp.to_i\narray = gets.chomp.split(' ').map {|x| x.to_i }\nresult = gets.chomp.split(' ').map {|x| x.to_i }\n\na = array.dup\nb = result.dup\na.delete(0)\nb.delete(0)\n\na << a.dup\na.flatten!\n\nfor i in 0..a.length-1\n break if a.length - i< b.length\n yay = true\n for j in 0..b.length-1\n if a[i+j] != b[j]\n yay = false\n break\n end\n end\n if yay\n puts \"YES\"\n exit 0\n end\nend\n\nputs \"NO\"\n"}], "negative_code": [], "src_uid": "846681af4b4b6b29d2c9c450a945de90"} {"nl": {"description": "Kolya got an integer array $$$a_1, a_2, \\dots, a_n$$$. The array can contain both positive and negative integers, but Kolya doesn't like $$$0$$$, so the array doesn't contain any zeros.Kolya doesn't like that the sum of some subsegments of his array can be $$$0$$$. The subsegment is some consecutive segment of elements of the array. You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $$$0$$$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $$$0$$$, any by absolute value, even such a huge that they can't be represented in most standard programming languages).Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $$$0$$$.", "input_spec": "The first line of the input contains one integer $$$n$$$ ($$$2 \\le n \\le 200\\,000$$$) — the number of elements in Kolya's array. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$-10^{9} \\le a_i \\le 10^{9}, a_i \\neq 0$$$) — the description of Kolya's array.", "output_spec": "Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $$$0$$$.", "sample_inputs": ["4\n1 -5 3 2", "5\n4 -2 3 -9 2", "9\n-1 1 -1 1 -1 1 1 -1 -1", "8\n16 -5 -11 -15 10 5 4 -4"], "sample_outputs": ["1", "0", "6", "3"], "notes": "NoteConsider the first example. There is only one subsegment with the sum $$$0$$$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $$$1$$$ between second and third elements of the array.There are no subsegments having sum $$$0$$$ in the second example so you don't need to do anything."}, "positive_code": [{"source_code": "n = gets.chomp.to_i\narr = gets.chomp.split(' ').map(&:to_i)\ns = {0 => 1}\ncur = 0\nres = 0\narr.each do |el|\n cur += el\n if s[cur] == 1\n s = {0 => 1}\n res += 1\n cur = el\n end\n s[cur] = 1\nend\nputs res"}], "negative_code": [{"source_code": "n = gets.chomp.to_i\narr = gets.chomp.split(' ').map(&:to_i)\ns = {}\ns[0] = 1 if arr.sum == 0\ncur = 0\narr.each do |el|\n cur += el\n s[cur] = 0 if s[cur].nil?\n s[cur] += 1\nend\nres = 0\ns.each do |key, val|\n res += val - 1\nend\nputs res"}], "src_uid": "05548be393d794bf106708627220b9a3"} {"nl": {"description": "You are given two strings $$$s$$$ and $$$t$$$, both consisting only of lowercase Latin letters.The substring $$$s[l..r]$$$ is the string which is obtained by taking characters $$$s_l, s_{l + 1}, \\dots, s_r$$$ without changing the order.Each of the occurrences of string $$$a$$$ in a string $$$b$$$ is a position $$$i$$$ ($$$1 \\le i \\le |b| - |a| + 1$$$) such that $$$b[i..i + |a| - 1] = a$$$ ($$$|a|$$$ is the length of string $$$a$$$).You are asked $$$q$$$ queries: for the $$$i$$$-th query you are required to calculate the number of occurrences of string $$$t$$$ in a substring $$$s[l_i..r_i]$$$.", "input_spec": "The first line contains three integer numbers $$$n$$$, $$$m$$$ and $$$q$$$ ($$$1 \\le n, m \\le 10^3$$$, $$$1 \\le q \\le 10^5$$$) — the length of string $$$s$$$, the length of string $$$t$$$ and the number of queries, respectively. The second line is a string $$$s$$$ ($$$|s| = n$$$), consisting only of lowercase Latin letters. The third line is a string $$$t$$$ ($$$|t| = m$$$), consisting only of lowercase Latin letters. Each of the next $$$q$$$ lines contains two integer numbers $$$l_i$$$ and $$$r_i$$$ ($$$1 \\le l_i \\le r_i \\le n$$$) — the arguments for the $$$i$$$-th query.", "output_spec": "Print $$$q$$$ lines — the $$$i$$$-th line should contain the answer to the $$$i$$$-th query, that is the number of occurrences of string $$$t$$$ in a substring $$$s[l_i..r_i]$$$.", "sample_inputs": ["10 3 4\ncodeforces\nfor\n1 3\n3 10\n5 6\n5 7", "15 2 3\nabacabadabacaba\nba\n1 15\n3 4\n2 14", "3 5 2\naaa\nbaaab\n1 3\n1 1"], "sample_outputs": ["0\n1\n0\n1", "4\n0\n3", "0\n0"], "notes": "NoteIn the first example the queries are substrings: \"cod\", \"deforces\", \"fo\" and \"for\", respectively."}, "positive_code": [{"source_code": "def single_input\n\tgets.chomp.to_i\nend\n\ndef multiple_input\n\tgets.chomp.split(' ').map(&:to_i)\nend\n\ndef string_input\n\tgets.chomp\nend\n\nn, m, q = multiple_input\ns = string_input\nt = string_input\n\nac = Array.new(n+m, 0)\n\npoint = 0\n(0+m-1).upto(n-1) do |i|\n\tflag = true\n\tm.times do |j|\n\t\tif s[i-m+1+j] != t[j]\n\t\t\tflag = false\n\t\tend\n\tend\n\t\n\tif flag\n\t\tac[i] += 1\n\tend\n\t# p \"#{s[i]} #{t[point]}, #{point}\"\n\n\tif i != 0\n\t\tac[i] += ac[i-1]\n\tend\nend\n\n# p ac\nq.times do |_|\n\tx, y = multiple_input\n\tif x+m-3 > y-1\n\t\tputs 0\n\t\t# puts \"CASE1\"\n\telsif x+m-3 >= 0 && x+m-3 < n\n\t\tputs ac[y-1] - ac[x+m-3]\n\t\t# puts \"CASE2 #{y-1} #{x+m-3} #{ac[y-1]} #{ac[x+m-3]}\"\n\telse\n\t\tputs ac[y-1]\n\t\t# puts \"CASE3\"\n\tend\nend"}, {"source_code": "N, M, Q = gets.split.map(&:to_i)\nS = gets.chomp\nT = gets.chomp\nnum = Array.new(N+1, 0)\n(N-M+1).times do |i|\n num[i] = S[i, M] == T ? 1 : 0\nend\n(N-1).times do |i|\n num[i+1] += num[i]\nend\n\nQ.times do |i|\n l, r = gets.split.map(&:to_i)\n l -= 1\n r -= 1\n puts r-l+1 >= M ? num[r-M+1] - num[l-1] : 0\nend"}, {"source_code": "N, M, Q = gets.split.map(&:to_i)\nS = gets.chomp\nT = gets.chomp\nnum = Array.new(N+1, 0)\n(N-M+1).times do |i|\n if S[i, M] == T\n num[i] = 1\n else\n num[i] = 0\n end\nend\n(N-1).times do |i|\n num[i+1] += num[i]\nend\n\nQ.times do |i|\n l, r = gets.split.map(&:to_i)\n l -= 1\n r -= 1\n ans = r >= M-1 ? num[r-M+1] - num[l-1] : 0\n puts ans > 0 ? ans : 0\nend\n"}, {"source_code": "nmq = gets.chomp.split(\" \").map(&:to_i)\nn = nmq[0]\nm = nmq[1]\nq = nmq[2]\nstr = gets.chomp\nmin = gets.chomp\n\nqarr = []\nq.times {qarr.push gets.chomp.split(\" \").map(&:to_i)}\n\ndef count_em(string, substring)\n # count = 0\n ans = []\n biglen = string.length\n smallen = substring.length\n lookfor = substring[0]\n index= 0\n flag = 0\n (0..biglen-smallen+1).each do |i|\n next if i=s and x<=e-len+1}\nend"}], "negative_code": [{"source_code": "def single_input\n\tgets.chomp.to_i\nend\n\ndef multiple_input\n\tgets.chomp.split(' ').map(&:to_i)\nend\n\ndef string_input\n\tgets.chomp\nend\n\nn, m, q = multiple_input\ns = string_input\nt = string_input\n\nac = Array.new(n, 0)\n\npoint = 0\nn.times do |i|\n\n\t# p \"#{s[i]} #{t[point]}, #{point}\"\n\tif s[i] == t[point]\n\t\tpoint += 1\n\telse\n\t\tpoint = 0\n\tend\n\n\tif point == m\n\t\tac[i] += 1\n\t\tpoint = 0\n\tend\n\n\tif i != 0\n\t\tac[i] += ac[i-1]\n\tend\nend\n\n# p ac\nq.times do |_|\n\tx, y = multiple_input\n\tif x+m-3 >= 0 && x+m-3 < n\n\t\tputs ac[y-1] - ac[x+m-3]\n\telse\n\t\tputs ac[y-1]\n\tend\nend"}, {"source_code": "def single_input\n\tgets.chomp.to_i\nend\n\ndef multiple_input\n\tgets.chomp.split(' ').map(&:to_i)\nend\n\ndef string_input\n\tgets.chomp\nend\n\nn, m, q = multiple_input\ns = string_input\nt = string_input\n\nac = Array.new(n, 0)\n\npoint = 0\nn.times do |i|\n\n\t# p \"#{s[i]} #{t[point]}, #{point}\"\n\tif s[i] == t[point]\n\t\tpoint += 1\n\telse\n\t\tpoint = 0\n\tend\n\n\tif point == m\n\t\tac[i] += 1\n\t\tpoint = 0\n\tend\n\n\tif i != 0\n\t\tac[i] += ac[i-1]\n\tend\nend\n\n# p ac\nq.times do |_|\n\tx, y = multiple_input\n\tif x+m-3 > y-1\n\t\tputs 0\n\t\t# puts \"CASE1\"\n\telsif x+m-3 >= 0 && x+m-3 < n\n\t\tputs ac[y-1] - ac[x+m-3]\n\t\t# puts \"CASE2 #{y-1} #{x+m-3} #{ac[y-1]} #{ac[x+m-3]}\"\n\telse\n\t\tputs ac[y-1]\n\t\t# puts \"CASE3\"\n\tend\nend"}, {"source_code": "def single_input\n\tgets.chomp.to_i\nend\n\ndef multiple_input\n\tgets.chomp.split(' ').map(&:to_i)\nend\n\ndef string_input\n\tgets.chomp\nend\n\nn, m, q = multiple_input\ns = string_input\nt = string_input\n\nac = Array.new(n, 0)\n\npoint = 0\nn.times do |i|\n\n\t# p \"#{s[i]} #{t[point]}, #{point}\"\n\tif s[i] == t[point]\n\t\tpoint += 1\n\telse\n\t\tpoint = 0\n\tend\n\n\tif point == m\n\t\tac[i] += 1\n\t\tpoint = 0\n\tend\n\n\tif i != 0\n\t\tac[i] += ac[i-1]\n\tend\nend\n\n# p ac\nq.times do |_|\n\tx, y = multiple_input\n\tif x-1 >= 0\n\t\tputs ac[y-1] - ac[x-1]\n\telse\n\t\tputs ac[y-1]\n\tend\nend"}, {"source_code": "N, M, Q = gets.split.map(&:to_i)\nS = gets.chomp\nT = gets.chomp\nnum = Array.new(N+1, 0)\n(N-M+1).times do |i|\n if S[i, M] == T\n num[i] = 1\n else\n num[i] = 0\n end\nend\n(N-1).times do |i|\n num[i+1] += num[i]\nend\n\nQ.times do |i|\n l, r = gets.split.map(&:to_i)\n l -= 1\n r -= 1\n puts r >= M-1 ? num[r-M+1] - num[l-1] : 0\nend\n"}, {"source_code": "N, M, Q = gets.split.map(&:to_i)\nS = gets.chomp\nT = gets.chomp\nnum = Array.new(N+1, 0)\n(N-M+1).times do |i|\n num[i] = S[i, M] == T ? 1 : 0\nend\n(N-1).times do |i|\n num[i+1] += num[i]\nend\n\nQ.times do |i|\n l, r = gets.split.map(&:to_i)\n l -= 1\n r -= 1\n ans = r-l+1 >= M ? num[r-M+1] - num[l-1] : 0\nend"}, {"source_code": "N, M, Q = gets.split.map(&:to_i)\nS = gets.chomp\nT = gets.chomp\nnum = Array.new(N+1, 0)\n(N-M+1).times do |i|\n if S[i, M] == T\n num[i] = 1\n else\n num[i] = 0\n end\nend\n(N-1).times do |i|\n num[i+1] += num[i]\nend\n\nQ.times do |i|\n l, r = gets.split.map(&:to_i)\n l -= 1\n r -= 1\n puts num[r-M+1] - num[l-1]\nend\n"}, {"source_code": "nmq = gets.chomp.split(\" \").map(&:to_i)\nn = nmq[0]\nm = nmq[1]\nq = nmq[2]\nstr = gets.chomp\nmin = gets.chomp\n\nqarr = []\nq.times {qarr.push gets.chomp.split(\" \").map(&:to_i)}\n\ndef count_em(string, substring)\n # count = 0\n ans = []\n biglen = string.length\n smallen = substring.length\n lookfor = substring[0]\n index= 0\n flag = 0\n (0..biglen-smallen+1).each do |i|\n next if i=s and x<=e-len+1}\nend"}, {"source_code": "nmq = gets.chomp.split(\" \").map(&:to_i)\nn = nmq[0]\nm = nmq[1]\nq = nmq[2]\nstr = gets.chomp\nmin = gets.chomp\n\nqarr = []\nq.times {qarr.push gets.chomp.split(\" \").map(&:to_i)}\n\ndef count_em(string, substring)\n # count = 0\n ans = []\n biglen = string.length\n smallen = substring.length\n lookfor = substring[0]\n index= 0\n flag = 0\n (0..biglen-smallen+1).each do |i|\n next if i=s and x<=e-len+1}\nend"}, {"source_code": "nmq = gets.chomp.split(\" \").map(&:to_i)\nq = nmq[2]\nstr = gets.chomp\nmin = gets.chomp\n\nqarr = []\nq.times {qarr.push gets.chomp.split(\" \").map(&:to_i)}\n\ndef count_em(string, substring)\n # count = 0\n ans = []\n biglen = string.length\n smallen = substring.length\n lookfor = substring[0]\n index= 0\n (0..biglen-smallen+1).each do |i|\n next if i=s and x<=e-len+1}\nend"}, {"source_code": "nmq = gets.chomp.split(\" \").map(&:to_i)\nn = nmq[0]\nm = nmq[1]\nq = nmq[2]\nstr = gets.chomp\nmin = gets.chomp\n\nqarr = []\nq.times {qarr.push gets.chomp.split(\" \").map(&:to_i)}\n\ndef count_em(string, substring)\n # count = 0\n ans = []\n biglen = string.length\n smallen = substring.length\n lookfor = substring[0]\n index= 0\n flag = 0\n (0..biglen-smallen+1).each do |i|\n next if i=s and x<=e-len+1}\nend"}], "src_uid": "4cc5a6975d33cee60e53e8f648ec30de"} {"nl": {"description": "You are given three integers $$$n$$$, $$$l$$$, and $$$r$$$. You need to construct an array $$$a_1,a_2,\\dots,a_n$$$ ($$$l\\le a_i\\le r$$$) such that $$$\\gcd(i,a_i)$$$ are all distinct or report there's no solution.Here $$$\\gcd(x, y)$$$ denotes the greatest common divisor (GCD) of integers $$$x$$$ and $$$y$$$.", "input_spec": "The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1\\le t\\le 10^4$$$) — the number of test cases. The description of the test cases follows. The first line contains three integers $$$n$$$, $$$l$$$, $$$r$$$ ($$$1 \\le n \\le 10^5$$$, $$$1\\le l\\le r\\le 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.", "output_spec": "For each test case, if there is no solution, print \"NO\" (without quotes). You can print letters in any case (upper or lower). Otherwise, print \"YES\" (without quotes). In the next line, print $$$n$$$ integers $$$a_1,a_2,\\ldots,a_n$$$ — the array you construct. If there are multiple solutions, you may output any.", "sample_inputs": ["4\n5 1 5\n9 1000 2000\n10 30 35\n1 1000000000 1000000000"], "sample_outputs": ["YES\n1 2 3 4 5\nYES\n1145 1926 1440 1220 1230 1350 1001 1000 1233\nNO\nYES\n1000000000"], "notes": "NoteIn the first test case, $$$\\gcd(1,a_1),\\gcd(2,a_2),\\ldots,\\gcd(5,a_5)$$$ are equal to $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$, $$$5$$$, respectively."}, "positive_code": [{"source_code": "class Solution\r\n def initialize(n, l, r)\r\n @n = n\r\n @l = l\r\n @r = r\r\n @arr = []\r\n solve\r\n print\r\n end\r\n\r\n def solve\r\n # unique gcd(i, a_i) means that a_i must be a multiple of i\r\n # -> try to find a_i s.t. a|a_i && l <= a_i <= r, i.e. smallest multiple of i > l\r\n # -> [(l - 1)/i + 1] * i\r\n (1..@n).each do |i|\r\n a_i = ((@l - 1)/i + 1) * i\r\n if a_i <= @r\r\n @arr << a_i\r\n else\r\n break\r\n end\r\n end\r\n end\r\n\r\n def print\r\n if @arr.length == @n\r\n puts 'YES'\r\n puts @arr.join(' ')\r\n else\r\n puts 'NO'\r\n end\r\n end\r\nend\r\n\r\nt = gets.chomp.to_i\r\nt.times do\r\n n, l, r = gets.chomp.split(' ').map(&:to_i)\r\n Solution.new(n, l, r)\r\nend\r\n"}, {"source_code": "gets.to_i.times do\r\n n, l, r = gets.split.map(&:to_i)\r\n array = (1..n).map do |i|\r\n ((l - 1) / i + 1) * i\r\n end\r\n if array.max <= r\r\n puts \"YES\"\r\n puts array.join(\" \")\r\n else\r\n puts \"NO\"\r\n end\r\nend\r\n"}, {"source_code": "gets.to_i.times do\r\n n, l, r = gets.split.map(&:to_i)\r\n array = (1..n).map do |i|\r\n ((l - 1) / i) * i + i\r\n end\r\n if array.max <= r\r\n puts \"YES\"\r\n puts array.join(\" \")\r\n else\r\n puts \"NO\"\r\n end\r\nend\r\n"}], "negative_code": [], "src_uid": "d2cc6efe7173a64482659ba59efeec16"} {"nl": {"description": "Recently n students from city S moved to city P to attend a programming camp.They moved there by train. In the evening, all students in the train decided that they want to drink some tea. Of course, no two people can use the same teapot simultaneously, so the students had to form a queue to get their tea.i-th student comes to the end of the queue at the beginning of li-th second. If there are multiple students coming to the queue in the same moment, then the student with greater index comes after the student with lesser index. Students in the queue behave as follows: if there is nobody in the queue before the student, then he uses the teapot for exactly one second and leaves the queue with his tea; otherwise the student waits for the people before him to get their tea. If at the beginning of ri-th second student i still cannot get his tea (there is someone before him in the queue), then he leaves the queue without getting any tea. For each student determine the second he will use the teapot and get his tea (if he actually gets it).", "input_spec": "The first line contains one integer t — the number of test cases to solve (1 ≤ t ≤ 1000). Then t test cases follow. The first line of each test case contains one integer n (1 ≤ n ≤ 1000) — the number of students. Then n lines follow. Each line contains two integer li, ri (1 ≤ li ≤ ri ≤ 5000) — the second i-th student comes to the end of the queue, and the second he leaves the queue if he still cannot get his tea. It is guaranteed that for every condition li - 1 ≤ li holds. The sum of n over all test cases doesn't exceed 1000. Note that in hacks you have to set t = 1.", "output_spec": "For each test case print n integers. i-th of them must be equal to the second when i-th student gets his tea, or 0 if he leaves without tea.", "sample_inputs": ["2\n2\n1 3\n1 4\n3\n1 5\n1 1\n2 3"], "sample_outputs": ["1 2 \n1 0 2"], "notes": "NoteThe example contains 2 tests: During 1-st second, students 1 and 2 come to the queue, and student 1 gets his tea. Student 2 gets his tea during 2-nd second. During 1-st second, students 1 and 2 come to the queue, student 1 gets his tea, and student 2 leaves without tea. During 2-nd second, student 3 comes and gets his tea. "}, "positive_code": [{"source_code": "inp = gets.chomp.to_i\notv = []\ngg = []\ninp.times{ |w|\nppl = gets.chomp.to_i\n\nmap = []\n\nppl.times{ |line|\n\nuser = gets.chomp.split(\" \")\nmap.push([user[0].to_i,user[1].to_i])\n\n}\n\n#todo\n\nctime = 0\ncurentuser = 0\n\nloop do\nif curentuser>=map.size then break; end\nctime+=1 \n\nif ctime <= map[curentuser][1] \n\nif ctime >= map[curentuser][0]\n\notv.push(ctime)\ncurentuser+=1\nelse\n#wait\nend\n\nelse\notv.push(0)\ncurentuser+=1\nctime-=1\nend\n\n\nend\n\ngg.push(otv.join(\" \"))\notv = []\n\n}\n\n\ngg.size.times{|w|\nputs gg[w]\n}"}, {"source_code": "def solve m\n ans = []\n cur = 1\n m.times do\n l, r = gets.chomp.split.map(&:to_i)\n if cur > r\n ans << 0\n elsif cur <= l\n ans << l\n cur = l + 1\n else\n ans << cur\n cur += 1\n end\n end\n puts ans.join(' ')\nend\n\nq = gets.to_i\nq.times do\n m = gets.to_i\n solve m\nend\n"}], "negative_code": [{"source_code": "def solve m\n ans = []\n cur = 1\n m.times do\n l, r = gets.chomp.split.map(&:to_i)\n if cur >= r\n ans << 0\n elsif cur <= l\n ans << l\n cur = l + 1\n else\n ans << cur\n cur += 1\n end\n end\n puts ans.join(' ')\nend\n\nq = gets.to_i\nq.times do\n m = gets.to_i\n solve m\nend\n"}], "src_uid": "471f80e349e70339eedd20d45b16e253"} {"nl": {"description": "Today, Wet Shark is given n bishops on a 1000 by 1000 grid. Both rows and columns of the grid are numbered from 1 to 1000. Rows are numbered from top to bottom, while columns are numbered from left to right.Wet Shark thinks that two bishops attack each other if they share the same diagonal. Note, that this is the only criteria, so two bishops may attack each other (according to Wet Shark) even if there is another bishop located between them. Now Wet Shark wants to count the number of pairs of bishops that attack each other.", "input_spec": "The first line of the input contains n (1 ≤ n ≤ 200 000) — the number of bishops. Each of next n lines contains two space separated integers xi and yi (1 ≤ xi, yi ≤ 1000) — the number of row and the number of column where i-th bishop is positioned. It's guaranteed that no two bishops share the same position.", "output_spec": "Output one integer — the number of pairs of bishops which attack each other. ", "sample_inputs": ["5\n1 1\n1 5\n3 3\n5 1\n5 5", "3\n1 1\n2 3\n3 5"], "sample_outputs": ["6", "0"], "notes": "NoteIn the first sample following pairs of bishops attack each other: (1, 3), (1, 5), (2, 3), (2, 4), (3, 4) and (3, 5). Pairs (1, 2), (1, 4), (2, 5) and (4, 5) do not attack each other because they do not share the same diagonal."}, "positive_code": [{"source_code": "n = gets.chomp.to_i\ncoords1 = Hash.new(0)\ncoords2 = Hash.new(0)\nn.times do\n coord = gets.chomp.split(\" \").map(&:to_i)\n coords1[coord[1] - coord[0]] += 1\n coords2[coord[1] + coord[0]] += 1\nend\n\ndef comb(n, k)\n return ((n - k + 1)..n).inject(:*) / (1..k).inject(:*)\nend\n\nres = 0\ncoords1.values.each do |v|\n res += comb(v, 2)\nend\ncoords2.values.each do |v|\n res += comb(v, 2)\nend\n\nputs res\n"}, {"source_code": "r=0\nh1=Hash.new(0)\nh2=Hash.new(0)\ngets.to_i.times{\n\tx,y=gets.split.map(&:to_i)\n\tr+=h1[x-y]\n\th1[x-y]+=1\n\tr+=h2[x+y]\n\th2[x+y]+=1\n}\np r"}, {"source_code": "n = gets.chomp.to_i\nbishops1 = Hash.new(0)\nbishops2 = Hash.new(0)\n\nn.times do\n x, y = gets.chomp.split(\" \").map(&:to_i)\n bishops1[x+y] += 1\n bishops2[x-y] += 1\nend\n\ndef choose(n, k)\n # n!/(n-k)!\n pTop = (n-k+1 .. n).inject(1, &:*)\n # k!\n pBottom = (2 .. k).inject(1, &:*)\n pTop / pBottom\nend\n\nsum1 = bishops1.reduce(0) do |acc, (k, v)|\n acc+choose(v, 2)\nend\n\nsum2 = bishops2.reduce(0) do |acc, (k, v)|\n acc+choose(v, 2)\nend\n\nputs sum1+sum2\n"}, {"source_code": "a=gets.chomp.to_i\nb=Array.new(1000){Array.new(1000,0)}\na.times do \ntemp=gets.chomp.split(\" \")\nb[temp[0].to_i-1][temp[1].to_i-1]=1\nend\ncol=[]\n0.upto(998) do |i|\nj=0\ntemp=0\np=0\np+=i\nloop do\ntemp+=b[i][j]\ni+=1\nj+=1\nbreak if i==1000\nend\ni=p\ncol << temp\ntemp=0\nj=999\nloop do\ntemp+=b[i][j]\ni+=1\nj-=1\nbreak if i==1000\nend\ni=p\ncol << temp\nend\n\n1.upto(998) do |i|\nj=0\np=0\np+=i\ntemp=0\nloop do\ntemp+=b[j][i]\ni+=1\nj+=1\nbreak if i==1000\nend\ni=p\ncol << temp\ntemp=0\nj=0\nloop do\ntemp+=b[j][i]\ni-=1\nj+=1\nbreak if i<0\nend\ni=p\ncol << temp\nend\ncol.delete(0)\n\nans=0\ncol.each {|i| ans+=(i*(i-1))/2 }\nputs \"#{ans}\""}, {"source_code": "a=gets.chomp.to_i\nb=Array.new(1000){Array.new(1000,0)}\na.times do \ntemp=gets.chomp.split(\" \")\nb[temp[0].to_i-1][temp[1].to_i-1]=1\nend\ncol=[]\n0.upto(998) do |i|\nj=0\ntemp=0\np=0\np+=i\nloop do\ntemp+=b[i][j]\ni+=1\nj+=1\nbreak if i==1000\nend\ni=p\ncol << temp\ntemp=0\nj=999\nloop do\ntemp+=b[i][j]\ni+=1\nj-=1\nbreak if i==1000\nend\ni=p\ncol << temp\nend\n\n1.upto(998) do |i|\nj=0\np=0\np+=i\ntemp=0\nloop do\ntemp+=b[j][i]\ni+=1\nj+=1\nbreak if i==1000\nend\ni=p\ncol << temp\ntemp=0\nj=0\nloop do\ntemp+=b[j][i]\ni-=1\nj+=1\nbreak if i<0\nend\ni=p\ncol << temp\nend\ncol.delete(0)\n\nans=0\ncol.each {|i| ans+=(i*(i-1))/2 }\nputs \"#{ans}\"\n"}, {"source_code": "n = gets.to_i\ng = Array.new(1000) { Array.new(1000, -1) }\nn.times do |v|\n a, b = gets.split.map(&:to_i)\n g[a-1][b-1] = v\nend\nans = 0\nm1 = 999\nfor s in 0..2*m1 do\n a = [0, s-m1].max\n b = [m1, s].min\n i = a\n j = s - a \n tmp = []\n while i <= b\n tmp.push(g[i][j]) if g[i][j] != -1\n i += 1\n j -= 1\n end\n ans += tmp.combination(2).size\nend\n\nfor s in (-1*m1)..m1 do \n a = [0, (-1*s)].max\n b = [m1, (m1-s)].min\n i = a\n j = s + a \n tmp = []\n while i <= b\n tmp.push(g[i][j]) if g[i][j] != -1\n i += 1\n j += 1\n end\n ans += tmp.combination(2).size\nend\nputs ans\n"}, {"source_code": "n = gets.chomp.to_i\nxs = []\nys = []\nhs0 = Hash.new(0)\nhs1 = Hash.new(0)\nn.times do\n x, y = gets.chomp.split.map(&:to_i)\n hs0[x + y] += 1\n hs1[x - y] += 1\nend\n\ncnt = 0\nhs0.each do |_, v|\n cnt += v * (v - 1) / 2\nend\nhs1.each do |_, v|\n cnt += v * (v - 1) / 2\nend\nputs cnt\n"}, {"source_code": "r=0\nh1=Hash.new(0)\nh2=Hash.new(0)\ngets.to_i.times{\n\tx,y=gets.split.map(&:to_i)\n\tr+=h1[x-y]\n\th1[x-y]+=1\n\tr+=h2[x+y]\n\th2[x+y]+=1\n}\nputs r"}, {"source_code": "# def solve_stupid(a)\n# cnt = 0\n# a.each_with_index do |(xx,yy), i|\n# dd = xx - yy\n# ss = xx + yy\n# cnt += (0...i).count do |j|\n# x, y = a[j]\n# x + y == ss || x - y == dd\n# end\n# end\n# cnt\n# end\n\ndef solve(a)\n diag1 = Hash.new(0)\n diag2 = Hash.new(0)\n a.each do |x, y|\n diag1[x+y] += 1\n diag2[x-y] += 1\n end\n (diag1.values + diag2.values).map do |v|\n v*(v-1)\n end.reduce(0, :+) / 2\nend\n\n\nn = gets.to_i\na = Array.new(n) do\n gets.split.map(&:to_i)\nend\n\nputs solve(a)\n\n# def rnd\n# require 'set'\n# s = Set.new\n# Array.new(rand(1..100)) do\n# c = [rand(100), rand(100)]\n# until s.add?(c)\n# c = [rand(100), rand(100)]\n# end\n# c\n# end\n# end\n\n# p((0..1000).all?{ t = rnd; solve(t) == solve_stupid(t)})\n\n"}, {"source_code": "n = gets.to_i\nb = []\nd = Array.new(2001, 0)\nnd = Array.new(2001, 0)\nfor _ in 0...n\n x, y = gets.split(' ').map &:to_i\n pos = y-x\n neg = y+x\n d[999+ pos]+=1\n nd[neg]+=1\nend\n\ndef bin(n, k) \n u = l = 1\n for v in 1..k\n u*=n+1-v\n l*=v\n end\n u/l\nend\np = 0\nfor i in 0...2001\n p += bin(d[i].to_i, 2) + bin(nd[i].to_i, 2)\nend\n\nputs p\n"}], "negative_code": [{"source_code": "n = gets.to_i\nb = []\nfor _ in 0...n\n x, y = gets.split(' ').map &:to_i\n b << [x,y]\nend\n\nb = b.sort {|a,b| a[0] - b[0] }\n\np=0\nfor j in 0...(b.size-1)\n for i in (j+1)...b.size\n dx = b[j][0] - b[i][0]\n dy = b[j][1] - b[i][1]\n if dx!=0 && (dy/dx).abs == 1\n p += 1\n end\n end\nend\n\nputs p"}, {"source_code": "n = gets.to_i\nb = []\nfor _ in 0...n\n x, y = gets.split(' ').map &:to_i\n b << [x,y]\nend\n\nb = b.sort {|a,b| a[0] - b[0] }\n\np=0\nfor j in 0..b.size\n for i in (j+1)...b.size\n dx = b[j][0] - b[i][0]\n dy = b[j][1] - b[i][1]\n if dy>0 && (dx/dy).abs == 1\n p += 2\n end\n end\nend\n\nputs p"}, {"source_code": "n = gets.to_i\nb = []\nfor _ in 0...n\n x, y = gets.split(' ').map &:to_i\n b << [x,y]\nend\n\nb = b.sort {|a,b| a[0] - b[0] }\n\np=0\nfor j in 0...b.size\n for i in 0...b.size\n dx = b[j][0] - b[i][0]\n dy = b[j][1] - b[i][1]\n if dy>0 && (dx/dy).abs == 1\n p += 1\n end\n end\nend\n\nputs p"}, {"source_code": "n = gets.to_i\nb = []\nd = Array.new(2002, 0)\nnd = Array.new(2002, 0)\nfor _ in 0...n\n x, y = gets.split(' ').map &:to_i\n d[1001 + x - y]+=1\n nd[1001 + y - x]+=1\nend\n\ndef bin(n, k) \n u = l = 1\n for v in 1..k\n u*=n+1-v\n l*=v\n end\n u/l\nend\np = 0\nfor i in 0...2002\n p += bin(d[i].to_i, 2) + bin(nd[i].to_i, 2)\nend\n\nputs p\n\n# b = b.sort {|a,b| a[0] - b[0] }\n\n# p=0\n# for j in 0...(b.size-1)\n# for i in (j+1)...b.size\n# dx = b[j][0] - b[i][0]\n# dy = b[j][1] - b[i][1]\n# if dx!=0 && (dy/dx).abs == 1\n# p += 1\n# end\n# end\n# end\n\n# puts p"}, {"source_code": "n = gets.to_i\nb = []\nd = Array.new(2000, 0)\nnd = Array.new(2000, 0)\nfor _ in 0...n\n x, y = gets.split(' ').map &:to_i\n \n d[1000 + x - y]+=1\n nd[1000 + y - x]+=1\nend\n\ndef bin(n, k) \n u = l = 1\n for v in 1..k\n u*=n+1-v\n l*=v\n end\n u/l\nend\np = 0\nfor i in 0...2000\n if d[i] > 1\n p += bin(d[i], 2)\n end\n\n if nd[i] > 1\n p += bin(nd[i], 2)\n end\nend\n\nputs p\n\n# b = b.sort {|a,b| a[0] - b[0] }\n\n# p=0\n# for j in 0...(b.size-1)\n# for i in (j+1)...b.size\n# dx = b[j][0] - b[i][0]\n# dy = b[j][1] - b[i][1]\n# if dx!=0 && (dy/dx).abs == 1\n# p += 1\n# end\n# end\n# end\n\n# puts p"}, {"source_code": "n = gets.to_i\nb = []\nfor _ in 0...n\n x, y = gets.split(' ').map &:to_i\n b << [x,y]\nend\n\nb = b.sort {|a,b| a[0] - b[0] }\n\np=0\nfor j in 0..b.size\n for i in (j+1)...b.size\n dx = b[j][0] - b[i][0]\n dy = b[j][1] - b[i][1]\n if dy>0 && (dx/dy).abs == 1\n p += 1\n end\n end\nend\n\nputs p"}, {"source_code": "n = gets.chomp.to_i\ncoords = Hash.new(0)\nn.times do\n coord = gets.chomp.split(\" \").map(&:to_i)\n k = coord[1] % coord[0]\n coords[k] += 1\nend\n\ndef comb(n, k)\n return ((n - k + 1)..n).inject(:*) / (1..k).inject(:*)\nend\n\n\nres = 0\ncoords.values.each do |v|\n res += comb(v, 2)\nend\n\n\nputs res\n"}], "src_uid": "eb7457fe1e1b571e5ee8dd9689c7d66a"} {"nl": {"description": "Once again, Boris needs the help of Anton in creating a task. This time Anton needs to solve the following problem:There are two arrays of integers $$$a$$$ and $$$b$$$ of length $$$n$$$. It turned out that array $$$a$$$ contains only elements from the set $$$\\{-1, 0, 1\\}$$$.Anton can perform the following sequence of operations any number of times: Choose any pair of indexes $$$(i, j)$$$ such that $$$1 \\le i < j \\le n$$$. It is possible to choose the same pair $$$(i, j)$$$ more than once. Add $$$a_i$$$ to $$$a_j$$$. In other words, $$$j$$$-th element of the array becomes equal to $$$a_i + a_j$$$. For example, if you are given array $$$[1, -1, 0]$$$, you can transform it only to $$$[1, -1, -1]$$$, $$$[1, 0, 0]$$$ and $$$[1, -1, 1]$$$ by one operation.Anton wants to predict if it is possible to apply some number (zero or more) of these operations to the array $$$a$$$ so that it becomes equal to array $$$b$$$. Can you help him?", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 10000$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$)  — the length of arrays. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$-1 \\le a_i \\le 1$$$)  — elements of array $$$a$$$. There can be duplicates among elements. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \\dots, b_n$$$ ($$$-10^9 \\le b_i \\le 10^9$$$)  — elements of array $$$b$$$. There can be duplicates among elements. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.", "output_spec": "For each test case, output one line containing \"YES\" if it's possible to make arrays $$$a$$$ and $$$b$$$ equal by performing the described operations, or \"NO\" if it's impossible. You can print each letter in any case (upper or lower).", "sample_inputs": ["5\n3\n1 -1 0\n1 1 -2\n3\n0 1 1\n0 2 2\n2\n1 0\n1 41\n2\n-1 0\n-1 -41\n5\n0 1 -1 1 -1\n1 1 -1 1 -1"], "sample_outputs": ["YES\nNO\nYES\nYES\nNO"], "notes": "NoteIn the first test-case we can choose $$$(i, j)=(2, 3)$$$ twice and after that choose $$$(i, j)=(1, 2)$$$ twice too. These operations will transform $$$[1, -1, 0] \\to [1, -1, -2] \\to [1, 1, -2]$$$In the second test case we can't make equal numbers on the second position.In the third test case we can choose $$$(i, j)=(1, 2)$$$ $$$41$$$ times. The same about the fourth test case.In the last lest case, it is impossible to make array $$$a$$$ equal to the array $$$b$$$."}, "positive_code": [{"source_code": "# https://codeforces.com/problemset/problem/1333/B\n\ngets.to_i.times do\n n = gets.to_i\n a = gets.split(' ').map(&:to_i)\n b = gets.split(' ').map(&:to_i)\n\n first_plus_index = nil\n first_minus_index = nil\n\n possible = true\n n.times do |i|\n first_plus_index = i if first_plus_index.nil? && a[i] == 1\n first_minus_index = i if first_minus_index.nil? && a[i] == -1\n\n if a[i] != b[i] &&\n (i == 0 ||\n (b[i] < a[i] && (first_minus_index.nil? || first_minus_index == i)) ||\n (b[i] > a[i] && (first_plus_index.nil? || first_plus_index == i)))\n\n possible = false\n break\n end\n end\n\n puts possible ? \"YES\" : \"NO\"\nend"}, {"source_code": "# coding: utf-8\n\nt = gets.to_i\nUnit = Struct.new(:val, :n, :z, :p)\nt.times {\n n = gets.to_i\n neg, zero, pos = 0, 0, 0\n ar = []\n gets.split.each { |i|\n i = i.to_i\n ar << Unit.new(i, neg, zero, pos)\n neg += 1 if i < 0\n zero += 1 if i == 0\n pos += 1 if i > 0\n }\n\n failed = false\n br = gets.split.map(&:to_i)\n # p ar\n until br.empty?\n b = br.pop # future state\n a = ar.pop # past state\n # from past to future\n if b == a.val\n next\n else\n if a.val < b\n if a.p < 1\n failed = true\n break\n end\n else\n if a.n < 1\n failed = true\n break\n end\n end\n end\n end\n\n\n # initial_state ->next_state -> next_state -> goal\n\n if failed == true\n puts \"NO\"\n else\n puts \"YES\"\n end\n}\n"}, {"source_code": "t = gets.to_i\nt.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n b = gets.split.map(&:to_i)\n plus = false\n minus = false\n ok = true\n for i in 0...n do\n if b[i] > a[i]\n if !plus\n ok = false\n break\n end\n elsif b[i] < a[i]\n if !minus\n ok = false\n break\n end\n end\n plus = true if a[i] == 1\n minus = true if a[i] == -1\n end\n puts ok ? \"YES\" : \"NO\"\nend\n"}], "negative_code": [], "src_uid": "e425aa498e5a7fc3e518cec25eec6304"} {"nl": {"description": "Bob is a competitive programmer. He wants to become red, and for that he needs a strict training regime. He went to the annual meeting of grandmasters and asked $$$n$$$ of them how much effort they needed to reach red.\"Oh, I just spent $$$x_i$$$ hours solving problems\", said the $$$i$$$-th of them. Bob wants to train his math skills, so for each answer he wrote down the number of minutes ($$$60 \\cdot x_i$$$), thanked the grandmasters and went home. Bob could write numbers with leading zeroes — for example, if some grandmaster answered that he had spent $$$2$$$ hours, Bob could write $$$000120$$$ instead of $$$120$$$.Alice wanted to tease Bob and so she took the numbers Bob wrote down, and for each of them she did one of the following independently: rearranged its digits, or wrote a random number. This way, Alice generated $$$n$$$ numbers, denoted $$$y_1$$$, ..., $$$y_n$$$.For each of the numbers, help Bob determine whether $$$y_i$$$ can be a permutation of a number divisible by $$$60$$$ (possibly with leading zeroes).", "input_spec": "The first line contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 418$$$) — the number of grandmasters Bob asked. Then $$$n$$$ lines follow, the $$$i$$$-th of which contains a single integer $$$y_i$$$ — the number that Alice wrote down. Each of these numbers has between $$$2$$$ and $$$100$$$ digits '0' through '9'. They can contain leading zeroes.", "output_spec": "Output $$$n$$$ lines. For each $$$i$$$, output the following. If it is possible to rearrange the digits of $$$y_i$$$ such that the resulting number is divisible by $$$60$$$, output \"red\" (quotes for clarity). Otherwise, output \"cyan\".", "sample_inputs": ["6\n603\n006\n205\n228\n1053\n0000000000000000000000000000000000000000000000"], "sample_outputs": ["red\nred\ncyan\ncyan\ncyan\nred"], "notes": "NoteIn the first example, there is one rearrangement that yields a number divisible by $$$60$$$, and that is $$$360$$$.In the second example, there are two solutions. One is $$$060$$$ and the second is $$$600$$$.In the third example, there are $$$6$$$ possible rearrangments: $$$025$$$, $$$052$$$, $$$205$$$, $$$250$$$, $$$502$$$, $$$520$$$. None of these numbers is divisible by $$$60$$$.In the fourth example, there are $$$3$$$ rearrangements: $$$228$$$, $$$282$$$, $$$822$$$.In the fifth example, none of the $$$24$$$ rearrangements result in a number divisible by $$$60$$$.In the sixth example, note that $$$000\\dots0$$$ is a valid solution."}, "positive_code": [{"source_code": "require 'set'; require 'prime'\nINF=Float::INFINITY; MOD=10**9+7\nt = gets.chomp.to_i\nt.times do\n a = gets.chomp.split(//).map(&:to_i)\n index = a.index(0)\n if index.nil?\n puts 'cyan'\n next\n end\n a.delete_at(index)\n is_2 = a.any?{|e| e.even?}\n is_3 = a.inject(&:+) % 3 == 0\n puts (is_3 && is_2) ? 'red' : 'cyan'\nend\n"}, {"source_code": "gets.strip.to_i.times {\n x = gets.strip.split('')\n even = false\n zero1 = false\n zero2 = false\n odd = false\n sum = 0\n x.each { |i| \n w = i.to_i\n odd = true if w.odd?\n even = true if w.even? && w > 0\n zero2 = true if w == 0 && zero1\n zero1 = true if w == 0\n sum += w\n }\n if sum % 3 == 0 && ((!odd && !even && zero1) || (even && odd && zero1) || (even && zero1) || (odd && zero1 && zero2))\n puts \"red\"\n else\n puts \"cyan\"\n end\n}"}], "negative_code": [{"source_code": "gets.strip.to_i.times {\n x = gets.strip.split('')\n even = false\n zero = false\n odd = false\n sum = 0\n x.each { |i| \n w = i.to_i\n odd = true if w.odd?\n even = true if w.even? && w > 0\n zero = true if w.even? && w == 0\n sum += w\n }\n if sum % 3 == 0 && ((!odd && !even && zero) || (even && odd && zero) || (even && zero))\n puts \"red\"\n else\n puts \"cyan\"\n end\n}"}, {"source_code": "gets.strip.to_i.times {\n x = gets.strip.split('')\n even = false\n zero = false\n odd = false\n sum = 0\n x.each { |i| \n w = i.to_i\n odd = true if w.odd?\n even = true if w.even? && w > 0\n zero = true if w.even? && w == 0\n sum += w\n }\n if sum % 3 == 0 && ((!odd && !even && zero) || (even && odd && zero) || (even && zero))\n puts \"red\"\n else\n puts \"clan\"\n end\n}"}], "src_uid": "5bc07d2efb7453e51f4931cc7ec3aac7"} {"nl": {"description": "You are given n × m table. Each cell of the table is colored white or black. Find the number of non-empty sets of cells such that: All cells in a set have the same color. Every two cells in a set share row or column. ", "input_spec": "The first line of input contains integers n and m (1 ≤ n, m ≤ 50) — the number of rows and the number of columns correspondingly. The next n lines of input contain descriptions of rows. There are m integers, separated by spaces, in each line. The number equals 0 if the corresponding cell is colored white and equals 1 if the corresponding cell is colored black.", "output_spec": "Output single integer  — the number of non-empty sets from the problem description.", "sample_inputs": ["1 1\n0", "2 3\n1 0 1\n0 1 0"], "sample_outputs": ["1", "8"], "notes": "NoteIn the second example, there are six one-element sets. Additionally, there are two two-element sets, the first one consists of the first and the third cells of the first row, the second one consists of the first and the third cells of the second row. To sum up, there are 8 sets."}, "positive_code": [{"source_code": "n, m=gets.split.map &:to_i\nres=0\na=[]\nn.times{\n a< b ? a : b\nend\n\n\nans = 0\nn, m = gets.split(/\\s+/).map(&:to_i)\na = Array.new(n)\n\nn.times do |i|\n a[i] = gets.split(/\\s+/).map(&:to_i)\nend\n\n0.upto(n-1) do |i|\n p = a[i].reduce(:+)\n q = m - p\n\n ans += max(2**p - 1 - p, 0)\n ans += max(2**q - 1 - q, 0)\nend\n\n0.upto(m-1) do |j|\n p = 0\n 0.upto(n-1) {|i| p += a[i][j] }\n q = n - p\n ans += max(2**p - 1 - p, 0)\n ans += max(2**q - 1 - q, 0)\nend\n\nputs ans + n * m\n"}, {"source_code": "n, m=gets.split.map &:to_i\nres=0\na=[]\nn.times{\n a< 0 && x2 > 0 && x1 + x2 == -b && x1 * x2 == c\r\nend\r\n \r\nt = gets.to_i\r\nt.times do\r\n x = gets.to_i\r\n puts (1...20000).map { |sum| method(x / sum, sum) if x % sum == 0 }.any? ? \"YES\" : \"NO\"\r\nend"}, {"source_code": "t=gets.chomp.to_i\r\nt.times do\r\nx = gets.chomp.to_i\r\na=0.0\r\nb=0.0\r\nc=0.0\r\nto = (x**(1.0/3.0)).to_i\r\n\r\n#puts \"to \" + to.to_s\r\n\r\ntr = false\r\n\r\nfor i in 1..to\r\n a=(i**3).to_f\r\n b=(x-a).to_f\r\n c=(b**(1.0/3.0)).round\r\n #puts \"#{i} + #{a} + #{b} + #{c}\"\r\n if(c**3==b && a+b==x && b!=0)\r\n puts \"YES\"\r\n tr=true\r\n end\r\n break if tr\r\nend\r\n\r\nif(!tr)\r\n puts \"NO\"\r\nend\r\n\r\nend\r\n"}], "negative_code": [], "src_uid": "b4ca6a5ee6307ab2bcdab2ea5dd5b2b3"} {"nl": {"description": "Alice gave Bob two integers $$$a$$$ and $$$b$$$ ($$$a > 0$$$ and $$$b \\ge 0$$$). Being a curious boy, Bob wrote down an array of non-negative integers with $$$\\operatorname{MEX}$$$ value of all elements equal to $$$a$$$ and $$$\\operatorname{XOR}$$$ value of all elements equal to $$$b$$$.What is the shortest possible length of the array Bob wrote?Recall that the $$$\\operatorname{MEX}$$$ (Minimum EXcluded) of an array is the minimum non-negative integer that does not belong to the array and the $$$\\operatorname{XOR}$$$ of an array is the bitwise XOR of all the elements of the array.", "input_spec": "The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \\leq t \\leq 5 \\cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The only line of each test case contains two integers $$$a$$$ and $$$b$$$ ($$$1 \\leq a \\leq 3 \\cdot 10^5$$$; $$$0 \\leq b \\leq 3 \\cdot 10^5$$$) — the $$$\\operatorname{MEX}$$$ and $$$\\operatorname{XOR}$$$ of the array, respectively.", "output_spec": "For each test case, output one (positive) integer — the length of the shortest array with $$$\\operatorname{MEX}$$$ $$$a$$$ and $$$\\operatorname{XOR}$$$ $$$b$$$. We can show that such an array always exists.", "sample_inputs": ["5\n1 1\n2 1\n2 0\n1 10000\n2 10000"], "sample_outputs": ["3\n2\n3\n2\n3"], "notes": "NoteIn the first test case, one of the shortest arrays with $$$\\operatorname{MEX}$$$ $$$1$$$ and $$$\\operatorname{XOR}$$$ $$$1$$$ is $$$[0, 2020, 2021]$$$.In the second test case, one of the shortest arrays with $$$\\operatorname{MEX}$$$ $$$2$$$ and $$$\\operatorname{XOR}$$$ $$$1$$$ is $$$[0, 1]$$$.It can be shown that these arrays are the shortest arrays possible."}, "positive_code": [{"source_code": "x = 0\r\nmexor = Array.new(300_001) { |i| x ^= i }\r\n\r\nt = gets.to_i\r\n\r\nt.times do\r\n\r\na, b = gets.split.map!(&:to_i)\r\n\r\nif mexor[a-1] == b\r\n puts a\r\nelse\r\n c = mexor[a-1] ^ b\r\n if a != c\r\n puts a + 1\r\n else\r\n puts a + 2\r\n end\r\nend\r\n\r\nend\r\n"}, {"source_code": "cache = []\nacc = 0\n0.upto(3 * 100000 + 5).each {|x| cache.push(acc ^= x) }\nt = gets.strip.to_i\nt.times do\n a, b = gets.strip.split.map {|x| x.to_i }\n x = cache[a - 1]\n if x == b\n puts a\n elsif (x ^ b) != a\n puts a + 1\n else\n puts a + 2\n end\nend\n"}], "negative_code": [{"source_code": "x = 0\r\nmexor = Array.new(300_001) { |i| x ^= i }\r\n\r\nt = gets.to_i\r\n\r\nt.times do\r\n\r\na, b = gets.split.map!(&:to_i)\r\n\r\nif mexor[a-1] == b\r\n puts a\r\nelsif a != b\r\n puts a + 1\r\nelse\r\n puts a + 2\r\nend\r\n\r\nend\r\n"}], "src_uid": "d6790c9b4b2e488768fb4e746ee7ebe0"} {"nl": {"description": "There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \\leq n \\leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist. ", "output_spec": "Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.", "sample_inputs": ["2\n2\n3"], "sample_outputs": ["2 1 \n3 1 2"], "notes": "NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$."}, "positive_code": [{"source_code": "t = gets.to_i\r\n(0...t).each{ |x|\r\n count = gets.to_i\r\n res = \"\"\r\n if count % 2 == 0\r\n (count/2).times{ |i|\r\n # unless i == 0\r\n # res << \" \"\r\n # end\r\n res << ((i+1)*2).to_s << \" \" << (i*2 + 1).to_s << \" \"\r\n }\r\n else\r\n res = \"3 1 2 \"\r\n if count > 3\r\n ((count-3)/2).times{ |i|\r\n res << (3 + (i+1)*2).to_s << \" \" << (3 + i*2 + 1).to_s << \" \"\r\n }\r\n end\r\n end\r\n puts res\r\n}"}, {"source_code": "t = gets.to_i\r\n(0...t).each{ |x|\r\n count = gets.to_i\r\n res = \"\"\r\n if count % 2 == 0\r\n (count/2).times{ |i|\r\n # unless i == 0\r\n # res << \" \"\r\n # end\r\n res << ((i+1)*2).to_s << \" \" << (i*2 + 1).to_s << \" \"\r\n }\r\n else\r\n res = \"3 1 2 \"\r\n if count > 3\r\n ((count-3)/2).times{ |i|\r\n res << (3 + (i+1)*2).to_s << \" \" << (3 + i*2 + 1).to_s << \" \"\r\n }\r\n end\r\n end\r\n puts res\r\n}"}, {"source_code": "gets.to_i.times{puts 2,'3'*(i=(n=gets.to_i)%2),1;i+=3;(p -~i,i;i+=2)while i 3\r\n ((count-3)/2).times{ |i|\r\n res << (3 + (i+1)*2).to_s << \" \" << (3 + i*2 + 1).to_s << \" \"\r\n }\r\n end\r\n end\r\n puts res\r\n}"}, {"source_code": "t = gets.to_i\r\n(0...t).each{ |x|\r\n count = gets.to_i\r\n res = \"\"\r\n if count % 2 == 0\r\n (count/2).times{ |i|\r\n unless i == 0\r\n res << \" \"\r\n end\r\n res << ((i+1)*2).to_s << \" \" << (i*2 + 1).to_s\r\n }\r\n else\r\n res = \"3 2 1\"\r\n if count > 3\r\n ((count-3)/2).times{ |i|\r\n res << \" \" << (3 + (i+1)*2).to_s << \" \" << (3 + i*2 + 1).to_s\r\n }\r\n end\r\n end\r\n puts res\r\n}"}, {"source_code": "t = gets.to_i\r\n(0...t).each{ |x|\r\n count = gets.to_i\r\n res = \"\"\r\n if count % 2 == 0\r\n (count/2).times{ |i|\r\n res << ((i+1)*2).to_s << \" \" << (i*2 + 1).to_s << \" \"\r\n }\r\n else\r\n res = \"3 2 1 \"\r\n if count > 3\r\n ((count-3)/2).times{ |i|\r\n res << 3 + (i+1)*2 << \" \" << 3 + (i*2 + 1) << \" \"\r\n }\r\n end\r\n end\r\n puts res\r\n}"}, {"source_code": "gets.to_i.times{n=gets.to_i;i=n%2;p 2,'3'*i,1;i+=3;(p -~i,i;i+=2)while i1?((v==p[i-1]+p[i-2])?q[i]+1:2):q[i]+1)};p q.max\n\n"}, {"source_code": "n=gets.to_i;q=Array.new(n+1,0);p=gets.split.map &:to_i;p.each_with_index{|v,i| q[i+1]=(i>1?((v==p[i-1]+p[i-2])?q[i]+1:2):q[i]+1)};p q.max\n\n"}, {"source_code": "n = STDIN.readline.to_i\narr = STDIN.readline.split.collect {|s| s.to_i }\nif n <= 2\n\tputs n\n\texit\nend\nbest = 2\npos = 0\nwhile pos+2 < n\n\tcount = 2\n\tseek = pos+2\n\twhile seek < n\n\t\tbreak if arr[seek] != arr[seek-1] + arr[seek-2]\n\t\tcount += 1\n\t\tseek += 1\n\tend\n\tbest = [best, count].max\n\tpos = seek-1\nend\nputs best\n"}, {"source_code": "n=gets.to_i;q=Array.new(n+1,0);p=gets.split.map &:to_i;p.each_with_index{|v,i| q[i+1]=(i>1?((v==p[i-1]+p[i-2])?q[i]+1:2):q[i]+1)};p q.max\n"}, {"source_code": "n=gets.to_i\na=gets.chomp.split.map(&:to_i)\nans,d=0,0\nn.times do |i|\n\t(i<2 || a[i]==a[i-1]+a[i-2]) ? d+=1 : d=2\n\tans=d if d>ans\nend\nputs ans"}, {"source_code": "n=gets.to_i;q=Array.new(n+1,0);p=gets.split.map &:to_i;p.each_with_index{|v,i| q[i+1]=(i>1?((v==p[i-1]+p[i-2])?q[i]+1:2):q[i]+1)};p q.max\n\n"}, {"source_code": "n=gets.to_i;q=Array.new(n+1,0);p=gets.split.map &:to_i;p.each_with_index{|v,i| q[i+1]=(i>1?((v==p[i-1]+p[i-2])?q[i]+1:2):q[i]+1)};p q.max\n\n"}, {"source_code": "n=gets.to_i;q=Array.new(n+1,0);p=gets.split.map &:to_i;p.each_with_index{|v,i| q[i+1]=(i>1?((v==p[i-1]+p[i-2])?q[i]+1:2):q[i]+1)};p q.max\n"}, {"source_code": "n=gets.to_i;q=Array.new(n+1,0);p=gets.split.map &:to_i;p.each_with_index{|v,i| q[i+1]=(i>1?((v==p[i-1]+p[i-2])?q[i]+1:2):q[i]+1)};p q.max\n\n"}, {"source_code": "n = gets.to_i\narr = gets.split(\" \").map(&:to_i)\nseg = arr[0..1].length\nmax_seg = seg\nif n > 1\n\t(2..(n-1)).each do |i|\n\t\t# puts \"#{a == arr[i-2] + arr[i-1] }\"\n\t\tif arr[i] == arr[i-2] + arr[i-1] \n\t\t\t# puts \"here\"\n\t\t\tif seg == 0\n\t\t\t\tseg = 3\n\t\t\telse\n\t\t\t\tseg += 1\n\t\t\tend\n\t\t\tmax_seg = seg if seg > max_seg\n\t\telse\n\t\t\tseg = 0\n\t\tend\n\tend\nend\nputs max_seg\n# 16\n# 1 1 1 2 3 5 8 1 1 1 2 3 5 8 13 21"}, {"source_code": "n = gets.to_i\na = gets.split.collect{|i| i.to_i}\nif a.size < 3\n puts a.size\n exit\nend\nmaxlength = 2\nthislength = 2\nfor i in 2...n\n if a[i] == a[i-1]+a[i-2]\n thislength += 1\n maxlength = thislength if thislength > maxlength\n else\n thislength = 2\n end\nend\nputs maxlength\n"}, {"source_code": "n=gets.to_i;q=Array.new(n+1,0);p=gets.split.map &:to_i;p.each_with_index{|v,i| q[i+1]=(i>1?((v==p[i-1]+p[i-2])?q[i]+1:2):q[i]+1)};p q.max\n\n"}, {"source_code": "n = gets.to_i\na = gets.chomp.split(\" \").map{|i| i.to_i}\n\nL = Array.new\n\n(0..n-1).each do |i|\n\tL[i] = 2\nend\n\nL[0] = 1\nmax = 1\n\n(2..n-1).each do |i|\n\tif a[i] == a[i-1] + a[i-2]\n\t\tL[i] = L[i-1] + 1\n\tend\n\tif L[i] > max\n\t\tmax = L[i]\n\tend\nend\n\nif n == 2\n\tmax = 2\nend\n\nputs max"}, {"source_code": "n=gets.to_i;q=Array.new(n+1,0);p=gets.split.map &:to_i;p.each_with_index{|v,i| q[i+1]=(i>1?((v==p[i-1]+p[i-2])?q[i]+1:2):q[i]+1)};p q.max\n\n"}, {"source_code": "n=gets.to_i;q=Array.new(n+1,0);p=gets.split.map &:to_i;p.each_with_index{|v,i| q[i+1]=(i>1?((v==p[i-1]+p[i-2])?q[i]+1:2):q[i]+1)};p q.max\n\n"}], "negative_code": [{"source_code": "#!/usr/bin/ruby\nn=gets.to_i\nm=2;c=0\ngets.split.map(&:to_i).each_cons(3){|e|\n\tif e[0]+e[1]==e[2]\n\t\tc+=1\n\telse\n\t\tm=[m,c+2].max\n\t\tc=0\n\tend\n}\np [m,c+2].max"}, {"source_code": "n=gets.to_i;q=Array.new(n+1,0);p=gets.split.map &:to_i;p.each_with_index{|v,i| q[i+1]=(i>1?((v==p[i-1]+p[i-2])?q[i]+1:2):q[i]+1)};p q.max\np q\n"}, {"source_code": "n=gets.to_i;q=Array.new(n+1,0);p=gets.split.map &:to_i;p.each_with_index{|v,i| q[i+1]=(i>1?((v==p[i-1]+p[i-2])?q[i]+1:1):q[i]+1)};p q.max\n\n"}, {"source_code": "n = gets.to_i\na = gets.chomp.split(\" \").map{|i| i.to_i}\n\nL = Array.new\n\n(0..n-1).each do |i|\n\tL[i] = 2\nend\n\nL[0] = 1\nmax = 2\n\n(2..n-1).each do |i|\n\tif a[i] == a[i-1] + a[i-2]\n\t\tL[i] = L[i-1] + 1\n\t\tif L[i] > max \n\t\t\tmax = L[i]\n\t\tend\n\tend\nend\n\nputs max"}, {"source_code": "n = gets.to_i\na = gets.chomp.split(\" \").map{|i| i.to_i}\n\nL = Array.new\n\n(0..n-1).each do |i|\n L[i] = 1\nend\n\nmax = 1\n\n(1..n-1).each do |i|\n if a[i] >= a[i-1] \n L[i] = 2\n if max < 2\n max = 2\n end\n if i > 1\n if a[i] == a[i-1] + a[i-2]\n L[i] = L[i-1] + 1\n if L[i] > max \n max = L[i]\n end\n end\n end\n end\nend\n\nputs max"}, {"source_code": "n = gets.to_i\na = gets.chomp.split(\" \").map{|i| i.to_i}\n\nL = Array.new\n\n(0..n-1).each do |i|\n L[i] = 2\nend\n\nL[0] = 1\nmax = 1\n\n(2..n-1).each do |i|\n if a[i] == a[i-1] + a[i-2]\n L[i] = L[i-1] + 1\n if L[i] > max\n max = L[i]\n end\n end\nend\n\nputs max"}, {"source_code": "n = gets.to_i\na = gets.chomp.split(\" \").map{|i| i.to_i}\n\nL = Array.new\n\n(0..n-1).each do |i|\n L[i] = 2\nend\n\nL[0] = 1\nmax = 1\n\n(2..n-1).each do |i|\n if a[i] == a[i-1] + a[i-2]\n L[i] = L[i-1] + 1\n end\n if L[i] > max\n max = L[i]\n end\nend\n\nputs max"}], "src_uid": "f99a23bc65a1f5cdbf7909dba573ce51"} {"nl": {"description": "Sayaka Saeki is a member of the student council, which has $$$n$$$ other members (excluding Sayaka). The $$$i$$$-th member has a height of $$$a_i$$$ millimeters.It's the end of the school year and Sayaka wants to take a picture of all other members of the student council. Being the hard-working and perfectionist girl as she is, she wants to arrange all the members in a line such that the amount of photogenic consecutive pairs of members is as large as possible.A pair of two consecutive members $$$u$$$ and $$$v$$$ on a line is considered photogenic if their average height is an integer, i.e. $$$\\frac{a_u + a_v}{2}$$$ is an integer.Help Sayaka arrange the other members to maximize the number of photogenic consecutive pairs.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1\\le t\\le 500$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2 \\le n \\le 2000$$$)  — the number of other council members. The second line of each test case contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 \\le a_i \\le 2 \\cdot 10^5$$$)  — the heights of each of the other members in millimeters. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2000$$$.", "output_spec": "For each test case, output on one line $$$n$$$ integers representing the heights of the other members in the order, which gives the largest number of photogenic consecutive pairs. If there are multiple such orders, output any of them.", "sample_inputs": ["4\n3\n1 1 2\n3\n1 1 1\n8\n10 9 13 15 3 16 9 13\n2\n18 9"], "sample_outputs": ["1 1 2 \n1 1 1 \n13 9 13 15 3 9 16 10 \n9 18"], "notes": "NoteIn the first test case, there is one photogenic pair: $$$(1, 1)$$$ is photogenic, as $$$\\frac{1+1}{2}=1$$$ is integer, while $$$(1, 2)$$$ isn't, as $$$\\frac{1+2}{2}=1.5$$$ isn't integer.In the second test case, both pairs are photogenic."}, "positive_code": [{"source_code": "test_cases = gets.chomp.to_i\r\nfor i in 1..test_cases do\r\n n = gets.chomp.to_i\r\n line = gets.chomp.split(\" \")\r\n odd = Array.new\r\n even = Array.new\r\n for j in 0..n - 1 do\r\n a = line[j].to_i\r\n if a % 2 == 1\r\n odd.push(a)\r\n else\r\n even.push(a)\r\n end\r\n end\r\n for j in 0..odd.size - 1 do\r\n print \"#{odd[j]} \"\r\n end\r\n for j in 0..even.size - 1 do\r\n print \"#{even[j]} \"\r\n end\r\n puts \"\"\r\nend"}, {"source_code": "t = gets.to_s.to_i\n\n1.upto(t) do |casenum|\n n = gets.to_s.split.map {|x| x.to_i }\n ar = gets.to_s.split.map {|x| x.to_i }\n\n aro = ar.select {|x| x.odd? }\n are = ar.select {|x| x.even? }\n\n if aro.size > are.size\n printf(\"%s %s\\n\", aro.join(\" \"), are.join(\" \"))\n else\n printf(\"%s %s\\n\", are.join(\" \"), aro.join(\" \"))\n end\nend\n"}], "negative_code": [], "src_uid": "fe2131c9228a2ec4365fdc3d0faa413a"} {"nl": {"description": "You are given an array $$$a$$$ consisting of $$$n$$$ integers.Your task is to determine if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome.Recall that an array $$$b$$$ is called a subsequence of the array $$$a$$$ if $$$b$$$ can be obtained by removing some (possibly, zero) elements from $$$a$$$ (not necessarily consecutive) without changing the order of remaining elements. For example, $$$[2]$$$, $$$[1, 2, 1, 3]$$$ and $$$[2, 3]$$$ are subsequences of $$$[1, 2, 1, 3]$$$, but $$$[1, 1, 2]$$$ and $$$[4]$$$ are not.Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array $$$a$$$ of length $$$n$$$ is the palindrome if $$$a_i = a_{n - i - 1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$. For example, arrays $$$[1234]$$$, $$$[1, 2, 1]$$$, $$$[1, 3, 2, 2, 3, 1]$$$ and $$$[10, 100, 10]$$$ are palindromes, but arrays $$$[1, 2]$$$ and $$$[1, 2, 3, 1]$$$ are not.You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. Next $$$2t$$$ lines describe test cases. The first line of the test case contains one integer $$$n$$$ ($$$3 \\le n \\le 5000$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$ ($$$\\sum n \\le 5000$$$).", "output_spec": "For each test case, print the answer — \"YES\" (without quotes) if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome and \"NO\" otherwise.", "sample_inputs": ["5\n3\n1 2 1\n5\n1 2 2 3 2\n3\n1 1 2\n4\n1 2 2 1\n10\n1 1 2 2 3 3 4 4 5 5"], "sample_outputs": ["YES\nYES\nNO\nYES\nNO"], "notes": "NoteIn the first test case of the example, the array $$$a$$$ has a subsequence $$$[1, 2, 1]$$$ which is a palindrome.In the second test case of the example, the array $$$a$$$ has two subsequences of length $$$3$$$ which are palindromes: $$$[2, 3, 2]$$$ and $$$[2, 2, 2]$$$.In the third test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes.In the fourth test case of the example, the array $$$a$$$ has one subsequence of length $$$4$$$ which is a palindrome: $$$[1, 2, 2, 1]$$$ (and has two subsequences of length $$$3$$$ which are palindromes: both are $$$[1, 2, 1]$$$).In the fifth test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes."}, "positive_code": [{"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\n\nt = gets.to_i\nt.times {\n n = gets.to_i\n mc = {}\n gets.chomp.split.each_with_index { |value, index|\n i = value.to_i\n mc[i] ||= []\n mc[i] << index\n }\n # p mc\n found = false\n catch :found do\n for k, v in mc\n if v.length >= 2\n if v.last - v.first > 1 # 모든 간격을 검사할 필요는 없지. at least니까.\n found = true\n throw :found\n end\n end\n end\n end\n\n if found == true\n puts \"YES\"\n else\n puts \"NO\"\n end\n\n}"}, {"source_code": "tc = gets.to_i \n\ntc.times do \n n = gets.to_i \n arr = gets.split().map{|x| x.to_i} \n mark = Array.new(5001) {0} \n mark1 = Array.new(5001) {-1}\n ans = false\n (0...n).each do |x| \n cur = arr[x]\n if(mark[cur] == 0) \n mark1[cur] = x \n end\n mark[cur] += 1 \n if(mark[cur] > 1) \n if(mark1[cur]+1 < x) \n ans = true \n break \n end\n end\n end\n # print(mark[1])\n if(ans) \n puts (\"YES\") \n else\n puts (\"NO\")\n end\nend"}, {"source_code": "t = gets.to_i\nt.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n ok = false\n for i in 0...(n-2) do\n for j in (i+2)...n do\n ok = true if a[i] == a[j]\n end\n end\n puts ok ? \"YES\" : \"NO\"\nend\n"}, {"source_code": "t = gets.chomp.to_i\n(0...t).each do |_|\n n = gets.chomp.to_i\n l = gets.chomp.split.map(&:to_i)\n seq_palidrom_exists = false\n (0...n).each{|i| seq_palidrom_exists = true if (l.rindex(l[i]) - i) > 1}\n puts seq_palidrom_exists ? \"YES\" : \"NO\"\nend\n"}], "negative_code": [{"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\n\nt = gets.to_i\nt.times {\n n = gets.to_i\n mc = {}\n gets.chomp.split.each_with_index { |value, index|\n i = value.to_i\n mc[i] ||= []\n mc[i] << index\n }\n # p mc\n found = false\n catch :found do\n for k, v in mc\n if v.length >= 2\n for i in 0...(v.length - 1)\n if v[i+1] - v[i] > 1\n found = true\n throw :found\n end\n end\n end\n end\n end\n\n if found == true\n puts \"YES\"\n else\n puts \"NO\"\n end\n\n}"}, {"source_code": "tc = gets.to_i \n\ntc.times do \n n = gets.to_i \n arr = gets.split().map{|x| x.to_i} \n mark = Array.new(5001) {0} \n ans = false\n (0...n).each do |x| \n mark[arr[x]] += 1 \n if(mark[arr[x]] > 1) \n if(arr[x] != arr[x-1]) \n ans = true \n break \n end\n end\n end\n if(ans) \n puts (\"YES\") \n else\n puts (\"NO\")\n end\nend"}, {"source_code": "t = gets.to_i\nt.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n ok = false\n for i in 0...(n-2) do\n ok = true if a[i] == a[i+2]\n end\n for i in 0...(n-3) do\n ok = true if a[i] == a[i+3] && a[i+1] == a[i+2]\n end\n puts ok ? \"YES\" : \"NO\"\nend\n"}], "src_uid": "5139d222fbbfa70d50e990f5d6c92726"} {"nl": {"description": "Recently, Tokitsukaze found an interesting game. Tokitsukaze had $$$n$$$ items at the beginning of this game. However, she thought there were too many items, so now she wants to discard $$$m$$$ ($$$1 \\le m \\le n$$$) special items of them.These $$$n$$$ items are marked with indices from $$$1$$$ to $$$n$$$. In the beginning, the item with index $$$i$$$ is placed on the $$$i$$$-th position. Items are divided into several pages orderly, such that each page contains exactly $$$k$$$ positions and the last positions on the last page may be left empty.Tokitsukaze would do the following operation: focus on the first special page that contains at least one special item, and at one time, Tokitsukaze would discard all special items on this page. After an item is discarded or moved, its old position would be empty, and then the item below it, if exists, would move up to this empty position. The movement may bring many items forward and even into previous pages, so Tokitsukaze would keep waiting until all the items stop moving, and then do the operation (i.e. check the special page and discard the special items) repeatedly until there is no item need to be discarded. Consider the first example from the statement: $$$n=10$$$, $$$m=4$$$, $$$k=5$$$, $$$p=[3, 5, 7, 10]$$$. The are two pages. Initially, the first page is special (since it is the first page containing a special item). So Tokitsukaze discards the special items with indices $$$3$$$ and $$$5$$$. After, the first page remains to be special. It contains $$$[1, 2, 4, 6, 7]$$$, Tokitsukaze discards the special item with index $$$7$$$. After, the second page is special (since it is the first page containing a special item). It contains $$$[9, 10]$$$, Tokitsukaze discards the special item with index $$$10$$$. Tokitsukaze wants to know the number of operations she would do in total.", "input_spec": "The first line contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$1 \\le n \\le 10^{18}$$$, $$$1 \\le m \\le 10^5$$$, $$$1 \\le m, k \\le n$$$) — the number of items, the number of special items to be discarded and the number of positions in each page. The second line contains $$$m$$$ distinct integers $$$p_1, p_2, \\ldots, p_m$$$ ($$$1 \\le p_1 < p_2 < \\ldots < p_m \\le n$$$) — the indices of special items which should be discarded.", "output_spec": "Print a single integer — the number of operations that Tokitsukaze would do in total.", "sample_inputs": ["10 4 5\n3 5 7 10", "13 4 5\n7 8 9 10"], "sample_outputs": ["3", "1"], "notes": "NoteFor the first example: In the first operation, Tokitsukaze would focus on the first page $$$[1, 2, 3, 4, 5]$$$ and discard items with indices $$$3$$$ and $$$5$$$; In the second operation, Tokitsukaze would focus on the first page $$$[1, 2, 4, 6, 7]$$$ and discard item with index $$$7$$$; In the third operation, Tokitsukaze would focus on the second page $$$[9, 10]$$$ and discard item with index $$$10$$$. For the second example, Tokitsukaze would focus on the second page $$$[6, 7, 8, 9, 10]$$$ and discard all special items at once."}, "positive_code": [{"source_code": "n,m,k = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\nans = 0\ni = 0\nwhile i < m\n #p i\n l = (a[i]-i-1) % k + 1\n r = a[i] + k- l\n while i < m && a[i] <= r\n i += 1\n end\n #p [l,r]\n ans += 1\nend\np ans"}, {"source_code": "\nN, M, K = gets.split.map(&:to_i)\nP = gets.split.map(&:to_i)\n\nl, r = 1, K\ncount, answer = 0, 0\nm = 0\nwhile m < M\n\tcount = m\n\twhile P[m] && P[m] <= r\n\t\tm += 1\n\tend\n\tcount = m - count\n\tif count > 0\n\t\tanswer += 1\n\t\tr += count\n\telsif P[m]\n\t\tpages = ( (P[m] - r) / K.to_r ).ceil\n\t\tr += pages * K\n#\t\tl += pages * K\n\tend\n#\tputs count\nend\nputs answer\n"}, {"source_code": "\nN, M, K = gets.split.map(&:to_i)\nP = gets.split.map(&:to_i)\n\ncount, answer = 0, 0\nm = 0\nwhile m < M\n\tr = ( (P[m] - m) / K.to_r ).ceil * K\n\tcount = m\n\twhile P[m] && (P[m] - count) <= r\n\t\tm += 1\n\tend\n\tcount = m - count\n\tif count > 0\n\t\tanswer += 1\n\tend\n#\tputs count\nend\nputs answer\n"}, {"source_code": "\nN, M, K = gets.split.map(&:to_i)\nP = gets.split.map(&:to_i)\n\nl, r = 1, K\ncount, answer = 0, 0\nm = 0\nwhile m < M\n\tcount = m\n\twhile P[m] && P[m] <= r\n\t\tm += 1\n\tend\n\tcount = m - count\n\tif count > 0\n\t\tanswer += 1\n\t\tr += count\n\telsif P[m]\n\t\tpages = ( (P[m] - r) / K.to_r ).ceil\n\t\tr += pages * K\n\t\tl += pages * K\n\tend\n#\tputs count\nend\nputs answer\n"}, {"source_code": "\nN, M, K = gets.split.map(&:to_i)\nP = gets.split.map(&:to_i)\n\ncount, answer = 0, 0\nm = 0\nwhile m < M\n\tr = ( (P[m] - count) / K.to_r ).ceil * K\n\twhile P[m] && (P[m] - count) <= r\n\t\tm += 1\n\tend\n\tcount = m\n\tanswer += 1\n#\tputs count\nend\nputs answer\n"}], "negative_code": [], "src_uid": "684273a4c6711295996e520739744b0f"} {"nl": {"description": "Suppose you have two points $$$p = (x_p, y_p)$$$ and $$$q = (x_q, y_q)$$$. Let's denote the Manhattan distance between them as $$$d(p, q) = |x_p - x_q| + |y_p - y_q|$$$.Let's say that three points $$$p$$$, $$$q$$$, $$$r$$$ form a bad triple if $$$d(p, r) = d(p, q) + d(q, r)$$$.Let's say that an array $$$b_1, b_2, \\dots, b_m$$$ is good if it is impossible to choose three distinct indices $$$i$$$, $$$j$$$, $$$k$$$ such that the points $$$(b_i, i)$$$, $$$(b_j, j)$$$ and $$$(b_k, k)$$$ form a bad triple.You are given an array $$$a_1, a_2, \\dots, a_n$$$. Calculate the number of good subarrays of $$$a$$$. A subarray of the array $$$a$$$ is the array $$$a_l, a_{l + 1}, \\dots, a_r$$$ for some $$$1 \\le l \\le r \\le n$$$.Note that, according to the definition, subarrays of length $$$1$$$ and $$$2$$$ are good.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 5000$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$). It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, print the number of good subarrays of array $$$a$$$.", "sample_inputs": ["3\n4\n2 4 1 3\n5\n6 9 1 9 6\n2\n13 37"], "sample_outputs": ["10\n12\n3"], "notes": "NoteIn the first test case, it can be proven that any subarray of $$$a$$$ is good. For example, subarray $$$[a_2, a_3, a_4]$$$ is good since it contains only three elements and: $$$d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3$$$ $$$<$$$ $$$d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7$$$; $$$d((a_2, 2), (a_3, 3))$$$ $$$<$$$ $$$d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))$$$; $$$d((a_3, 3), (a_4, 4))$$$ $$$<$$$ $$$d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))$$$; In the second test case, for example, subarray $$$[a_1, a_2, a_3, a_4]$$$ is not good, since it contains a bad triple $$$(a_1, 1)$$$, $$$(a_2, 2)$$$, $$$(a_4, 4)$$$: $$$d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6$$$; $$$d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4$$$; $$$d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2$$$; So, $$$d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))$$$."}, "positive_code": [{"source_code": "def ok?(arr)\n for j in 1...(arr.size) do\n for k in (j+1)...(arr.size) do\n return false if (arr[0] -arr[j]) * (arr[j] - arr[k]) >= 0\n end\n end\n true\nend\n\nt = gets.chomp.to_i\n\nt.times do\n n = gets.chomp.to_i\n arr = gets.chomp.split(' ').map(&:to_i)\n d = Array.new(n+1) { 0 }\n\n for i in 1..n do\n d[i] = d[i-1]\n ptr = 0\n while i - ptr > 0 do\n if ok?(arr[(i-ptr-1)...i])\n d[i] += 1\n ptr += 1\n else\n break\n end\n end\n end\n\n puts \"#{d[n]}\\n\"\nend\n"}], "negative_code": [{"source_code": "\ndef ok?(arr)\n for j in 1...(arr.size) do\n for k in (j+1)...(arr.size) do\n return false if (arr[0] -arr[j]) * (arr[j] - arr[k]) >= 0\n end\n end\n true\nend\n\nt = gets.chomp.to_i\n\nt.times do\n n = gets.chomp.to_i\n arr = gets.chomp.split(' ').map(&:to_i)\n d = Array.new(n+1) { 0 }\n\n for i in 1..n do\n d[i] = d[i-1]\n ptr = 0\n while i - ptr > 0 do\n d[i] += 1 if ok?(arr[(i-ptr-1)...i])\n ptr += 1\n end\n end\n\n puts \"#{d[n]}\\n\"\nend\n"}], "src_uid": "e87ff02ce425fc3e96c09a15679aa656"} {"nl": {"description": "YouKn0wWho has an integer sequence $$$a_1, a_2, \\ldots a_n$$$. Now he will split the sequence $$$a$$$ into one or more consecutive subarrays so that each element of $$$a$$$ belongs to exactly one subarray. Let $$$k$$$ be the number of resulting subarrays, and $$$h_1, h_2, \\ldots, h_k$$$ be the lengths of the longest increasing subsequences of corresponding subarrays.For example, if we split $$$[2, 5, 3, 1, 4, 3, 2, 2, 5, 1]$$$ into $$$[2, 5, 3, 1, 4]$$$, $$$[3, 2, 2, 5]$$$, $$$[1]$$$, then $$$h = [3, 2, 1]$$$.YouKn0wWho wonders if it is possible to split the sequence $$$a$$$ in such a way that the bitwise XOR of $$$h_1, h_2, \\ldots, h_k$$$ is equal to $$$0$$$. You have to tell whether it is possible.The longest increasing subsequence (LIS) of a sequence $$$b_1, b_2, \\ldots, b_m$$$ is the longest sequence of valid indices $$$i_1, i_2, \\ldots, i_k$$$ such that $$$i_1 \\lt i_2 \\lt \\ldots \\lt i_k$$$ and $$$b_{i_1} \\lt b_{i_2} \\lt \\ldots \\lt b_{i_k}$$$. For example, the LIS of $$$[2, 5, 3, 3, 5]$$$ is $$$[2, 3, 5]$$$, which has length $$$3$$$.An array $$$c$$$ is a subarray of an array $$$b$$$ if $$$c$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10\\,000$$$)  — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2 \\le n \\le 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$3 \\cdot 10^5$$$.", "output_spec": "For each test case, print \"YES\" (without quotes) if it is possible to split into subarrays in the desired way, print \"NO\" (without quotes) otherwise. You can print each letter in any register (upper or lower).", "sample_inputs": ["4\n7\n1 3 4 2 2 1 5\n3\n1 3 4\n5\n1 3 2 4 2\n4\n4 3 2 1"], "sample_outputs": ["YES\nNO\nYES\nYES"], "notes": "NoteIn the first test case, YouKn0wWho can split the sequence in the following way: $$$[1, 3, 4]$$$, $$$[2, 2]$$$, $$$[1, 5]$$$. This way, the LIS lengths are $$$h = [3, 1, 2]$$$, and the bitwise XOR of the LIS lengths is $$$3 \\oplus 1 \\oplus 2 = 0$$$.In the second test case, it can be shown that it is impossible to split the sequence into subarrays that will satisfy the condition."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n gets\r\n xs = gets.split.map(&:to_i)\r\n puts xs.size.even? || xs.each_cons(2).any? { |a, b| a >= b } ? \"YES\" : \"NO\"\r\nend"}], "negative_code": [], "src_uid": "d8d33581ceb13da9bb99e90296bdd8f7"} {"nl": {"description": "A positive (strictly greater than zero) integer is called round if it is of the form d00...0. In other words, a positive integer is round if all its digits except the leftmost (most significant) are equal to zero. In particular, all numbers from $$$1$$$ to $$$9$$$ (inclusive) are round.For example, the following numbers are round: $$$4000$$$, $$$1$$$, $$$9$$$, $$$800$$$, $$$90$$$. The following numbers are not round: $$$110$$$, $$$707$$$, $$$222$$$, $$$1001$$$.You are given a positive integer $$$n$$$ ($$$1 \\le n \\le 10^4$$$). Represent the number $$$n$$$ as a sum of round numbers using the minimum number of summands (addends). In other words, you need to represent the given number $$$n$$$ as a sum of the least number of terms, each of which is a round number.", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case is a line containing an integer $$$n$$$ ($$$1 \\le n \\le 10^4$$$).", "output_spec": "Print $$$t$$$ answers to the test cases. Each answer must begin with an integer $$$k$$$ — the minimum number of summands. Next, $$$k$$$ terms must follow, each of which is a round number, and their sum is $$$n$$$. The terms can be printed in any order. If there are several answers, print any of them.", "sample_inputs": ["5\n5009\n7\n9876\n10000\n10"], "sample_outputs": ["2\n5000 9\n1\n7 \n4\n800 70 6 9000 \n1\n10000 \n1\n10"], "notes": null}, "positive_code": [{"source_code": "t = gets.to_i\nt.times do\n n = gets.to_i\n res = []\n [10_000, 1_000, 100, 10, 1].each do |d|\n if (i = n / d) != 0\n res << i * d\n n -= res.last\n end\n end\n puts res.size\n puts res.join(' ')\nend\n"}, {"source_code": "t = gets.to_i\nt.times do\n n = gets.chomp\n ans = []\n m = n.size\n m.times do |i|\n if n[i] != \"0\"\n ans << n[i].to_i * 10 ** (m-i-1)\n end\n end\n p ans.size\n puts ans.join(\" \")\nend\n"}, {"source_code": "t = gets.to_i\n(1).upto(t) do\n x = gets\n x = x.strip.split('')\n arr = []\n i = x.length-1\n x.each do |c|\n if(c!='0')\n arr.push([c,i])\n end\n i-=1\n end\n puts arr.length\n arr.each do |v|\n print v[0]\n (1..v[1]).each do\n print 0\n end\n print ' '\n end\n puts\nend\n\n"}, {"source_code": "# Approach\n# mod = 10\n#\n# r = n % mod\n# n = n - r\nt = gets.to_i\nfor i in 1..t do\n n = gets.to_i\n mod = 10\n rnds = []\n while n*10 >= mod\n r = n % mod\n if r != 0\n rnds << r\n n = n - r\n end\n mod = mod * 10\n end\n puts rnds.size\n puts rnds.join(' ')\nend\n"}, {"source_code": "# Approach\n# mod = 10\n#\n# r = n % mod\n# n = n - r\nt = gets.to_i\nfor i in 1..t do\n n = gets.to_i\n mod = 10\n rnds = []\n while n*10 >= mod\n r = n % mod\n if r != 0\n rnds << r\n n = n - r\n end\n mod = mod * 10\n end\n puts rnds.size\n puts rnds.join(' ')\nend\n"}, {"source_code": "t = gets.to_i\n\n1.upto t do\n count = 0\n mul = 1\n val = []\n k = gets.chomp\n array = k.each_char.map {|num| num.to_i}\n for i in array.reverse\n if i != 0\n val.push(i * mul)\n else\n mul *= 10\n next\n end\n mul *= 10\n count += 1\n end\n puts count\n puts val.join(\" \")\nend"}, {"source_code": "gets.to_i.times{\n s=gets.chomp\n n=s.size\n res=[]\n n.downto(1){|i|\n if s[i-1]!=?0\n res< 1\n term = (10 ** (length - 1))\n m = n / term\n summands << (m * term)\n\n n = n % term\n length = n.to_s.size\n k += 1\n end\n\n if n > 0\n summands << n\n k += 1\n end\n\n puts k\n puts summands.join(' ')\nend\n\nif ENV['LOCAL_PROJECT']\n input = File.exist?('in.in') ? File.new('in.in','r') : STDIN\nelse\n input = STDIN\nend\n\nt = input.gets.chomp.to_i\n\nt.times do\n n = input.gets.chomp.to_i\n is_round(n)\nend\n"}, {"source_code": "class Fixnum\n def digits\n d = []\n t = self\n while t > 0\n d << t%10\n t /= 10\n end\n d\n end\nend\n\nmodule Enumerable\n alias filter select\nend\n\ngets.to_i.times do\n n = gets.to_i \n puts n.digits.filter{|x| x > 0}.size\n n.digits.each_with_index{|x, i| print x*10**i, ' ' if x > 0}\n puts\nend\n"}, {"source_code": "1.upto(gets.to_i) do\n s = gets.chomp\n\n res = s.reverse.chars.each_with_object([]).with_index do |(char, result), idx|\n result << char + '0' * idx unless char == '0'\n end\n\n puts res.size\n puts res.join(' ')\nend\n"}, {"source_code": "n = gets.chomp.to_i\n\nnums = []\n\nn.times do\n nums.push(gets.chomp.to_i)\nend\n\nnums.each_with_index do |num|\n nums_to_send = []\n num.to_s.reverse!.each_char.with_index do |digit, index|\n nums_to_send.push(digit.to_i * 10 ** index)\n end\n filtered_arr = nums_to_send.filter {|e| e != 0 }\n p filtered_arr.length\n puts filtered_arr.join(' ')\nend"}], "negative_code": [{"source_code": "# Approach\n# mod = 10\n#\n# r = n % mod\n# n = n - r\nt = gets.to_i\nfor i in 1..t do\n n = gets.to_i\n mod = 10\n rnds = []\n while n >= mod\n r = n % mod\n rnds << r\n n = n - r\n mod = mod * 10\n end\n puts rnds.size\n puts rnds.join(' ')\nend\n"}], "src_uid": "cd2519f4a7888b2c292f05c64a9db13a"} {"nl": {"description": "Nikephoros and Polycarpus play rock-paper-scissors. The loser gets pinched (not too severely!).Let us remind you the rules of this game. Rock-paper-scissors is played by two players. In each round the players choose one of three items independently from each other. They show the items with their hands: a rock, scissors or paper. The winner is determined by the following rules: the rock beats the scissors, the scissors beat the paper and the paper beats the rock. If the players choose the same item, the round finishes with a draw.Nikephoros and Polycarpus have played n rounds. In each round the winner gave the loser a friendly pinch and the loser ended up with a fresh and new red spot on his body. If the round finished in a draw, the players did nothing and just played on.Nikephoros turned out to have worked out the following strategy: before the game began, he chose some sequence of items A = (a1, a2, ..., am), and then he cyclically showed the items from this sequence, starting from the first one. Cyclically means that Nikephoros shows signs in the following order: a1, a2, ..., am, a1, a2, ..., am, a1, ... and so on. Polycarpus had a similar strategy, only he had his own sequence of items B = (b1, b2, ..., bk).Determine the number of red spots on both players after they've played n rounds of the game. You can consider that when the game began, the boys had no red spots on them.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 2·109) — the number of the game's rounds. The second line contains sequence A as a string of m characters and the third line contains sequence B as a string of k characters (1 ≤ m, k ≤ 1000). The given lines only contain characters \"R\", \"S\" and \"P\". Character \"R\" stands for the rock, character \"S\" represents the scissors and \"P\" represents the paper.", "output_spec": "Print two space-separated integers: the numbers of red spots Nikephoros and Polycarpus have.", "sample_inputs": ["7\nRPS\nRSPP", "5\nRRRRRRRR\nR"], "sample_outputs": ["3 2", "0 0"], "notes": "NoteIn the first sample the game went like this: R - R. Draw. P - S. Nikephoros loses. S - P. Polycarpus loses. R - P. Nikephoros loses. P - R. Polycarpus loses. S - S. Draw. R - P. Nikephoros loses. Thus, in total Nikephoros has 3 losses (and 3 red spots), and Polycarpus only has 2."}, "positive_code": [{"source_code": "def gcd(a, b)\n return a if b == 0\n return gcd(b, a%b)\nend\n\ndef lcm(a, b)\n return (a/gcd(a, b))*b\nend\n\nn = gets.to_i\na = gets.strip\nb = gets.strip\nm = a.length\nk = b.length\nx = lcm(m, k)\n\na *= x/m\nb *= x/k\n\ndota = Array.new\ndotb = Array.new\n\ndota[0] = 0\ndotb[0] = 0\n\nfor i in 1..x\n case a[i-1]\n when \"R\"; c=1\n when \"P\"; c=2\n when \"S\"; c=3\n end\n case b[i-1]\n when \"R\"; c-=1\n when \"P\"; c-=2\n when \"S\"; c-=3\n end\n dota[i] = dota[i-1]\n dotb[i] = dotb[i-1]\n case c\n when 1; dotb[i] += 1\n when 2; dota[i] += 1\n when -1; dota[i] += 1\n when -2; dotb[i] += 1\n end\nend\n\nprint dota[x]*(n/x) + dota[n%x], \" \", dotb[x]*(n/x) + dotb[n%x]"}, {"source_code": "def gcd(a, b)\n until b == 0\n temp = a%b\n a = b\n b = temp\n end\n return a\nend\n\ndef lcm(a, b)\n return (a/gcd(a, b))*b\nend\n\nn = gets.to_i\na = gets.strip\nb = gets.strip\nm = a.length\nk = b.length\nx = lcm(m, k)\n\na *= x/m\nb *= x/k\n\ndota = Array.new\ndotb = Array.new\n\ndota[0] = 0\ndotb[0] = 0\n\nfor i in 1..x\n case a[i-1]\n when \"R\"; c=1\n when \"P\"; c=2\n when \"S\"; c=3\n end\n case b[i-1]\n when \"R\"; c-=1\n when \"P\"; c-=2\n when \"S\"; c-=3\n end\n dota[i] = dota[i-1]\n dotb[i] = dotb[i-1]\n case c\n when 1; dotb[i] += 1\n when 2; dota[i] += 1\n when -1; dota[i] += 1\n when -2; dotb[i] += 1\n end\nend\n\nprint dota[x]*(n/x) + dota[n%x], \" \", dotb[x]*(n/x) + dotb[n%x]"}, {"source_code": "n = gets.to_i\ns1 = gets.chomp\ns2 = gets.chomp\nn1 = s1.size\nn2 = s2.size\nT = n1*n2\nD = n%T\nN = n/T\nsum1 = 0\nsum2 = 0\nsumd1 = 0\nsumd2 = 0\n#Символ «R» (от английского rock) означает знак камня, символ «S» (scissors) — ножниц, «P» (paper) — бумаги.\n0.upto(T-1) do |i|\n c1 = s1[i%n1]\n c2 = s2[i%n2]\n if c1 == 'R' && c2 == 'S' || c1 == 'S' && c2 == 'P' || c1 == 'P' && c2 == 'R'\n sum1 += 1\n sumd1 += 1 if i < D\n end\n c1,c2=c2,c1\n if c1 == 'R' && c2 == 'S' || c1 == 'S' && c2 == 'P' || c1 == 'P' && c2 == 'R'\n sum2 += 1\n sumd2 += 1 if i < D\n end\nend\nsum1 = sum1 * N + sumd1\nsum2 = sum2 * N + sumd2\nprintf(\"#{sum2} #{sum1}\")\n"}, {"source_code": "n=gets.to_i\nR,P,S=82,80,83\na,b=2.times.map{gets.chomp.each_byte.to_a}\nas=bs=ar=br=0\nlcm=a.size.lcm b.size\nq,r=n.divmod lcm\nlcm.times{|i|\n\tar,br=as,bs if i==r\n\t_a=a[i%a.size]\n\t_b=b[i%b.size]\n\tnext if _a==_b\n\tas+=1 if (_a==R&&_b==P)||(_a==P&&_b==S)||(_a==S&&_b==R)\n\tbs+=1 if (_b==R&&_a==P)||(_b==P&&_a==S)||(_b==S&&_a==R)\n}\nputs\"%d %d\"%[as*q+ar,bs*q+br]"}], "negative_code": [], "src_uid": "9a365e26f58ecb22a1e382fad3062387"} {"nl": {"description": "Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches.In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$.You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match  — it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$)  — the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 \\leq n \\leq 100$$$; $$$1 \\leq b < r \\leq n$$$, $$$r+b=n$$$).", "output_spec": "For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.", "sample_inputs": ["3\n7 4 3\n6 5 1\n19 13 6", "6\n3 2 1\n10 6 4\n11 6 5\n10 9 1\n10 8 2\n11 9 2"], "sample_outputs": ["RBRBRBR\nRRRBRR\nRRBRRBRRBRRBRRBRRBR", "RBR\nRRBRBRBRBR\nRBRBRBRBRBR\nRRRRRBRRRR\nRRRBRRRBRR\nRRRBRRRBRRR"], "notes": "NoteThe first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further."}, "positive_code": [{"source_code": "def solve(n, r, b)\n # b is strictly less than r, which means we can divide n into b + 1 sections\n # extra rs go into r % (b + 1)\n num_sections = b + 1\n len_r = r / num_sections\n extra = r % num_sections\n ans = 'R' * len_r\n b.times do\n ans.concat('B').concat('R' * len_r)\n if extra > 0\n ans.concat('R')\n extra -= 1\n end\n end\n ans\nend\n\ntests = gets.to_i\ntests.times do\n n, r, b = gets.chomp.split(' ').map(&:to_i)\n puts solve(n, r, b)\nend"}], "negative_code": [{"source_code": "def solve(n, r, b)\n # b is strictly less than r, which means we can divide n into b + 1 sections\n # extra rs go into r % (b + 1)\n num_sections = b + 1\n len_r = r / num_sections\n extra = r % num_sections\n ans = ''\n b.times do\n ans.concat('R' * len_r).concat('B')\n if extra > 0\n ans.concat('R')\n extra -= 1\n end\n end\n ans\nend\n\ntests = gets.to_i\ntests.times do\n n, r, b = gets.chomp.split(' ').map(&:to_i)\n puts solve(n, r, b)\nend"}], "src_uid": "7cec27879b443ada552a6474c4e45c30"} {"nl": {"description": "Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote as n the length of permutation p1,   p2,   ...,   pn.Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.", "input_spec": "The single line of the input contains two space-separated positive integers n, k (1 ≤ k < n ≤ 105).", "output_spec": "Print n integers forming the permutation. If there are multiple answers, print any of them.", "sample_inputs": ["3 2", "3 1", "5 2"], "sample_outputs": ["1 3 2", "1 2 3", "1 3 2 4 5"], "notes": "NoteBy |x| we denote the absolute value of number x. "}, "positive_code": [{"source_code": "n, k = gets.split.map { |i| i.to_i }\nans = []\nif k == n - 1\n for i in 0...n\n print i % 2 == 0 ? i / 2 + 1 : n - i / 2\n print ' '\n end\n exit(0)\nend\nfor i in 0...k / 2\n ans.push i, n - i - 1\nend\nfor i in k / 2...n - k / 2\n ans.push i\nend\nif k % 2 == 0\n ans[0], ans[1] = ans[1], ans[0]\nend\nfor x in ans\n print (x + 1).to_s + ' '\nend"}, {"source_code": "n,k=gets.split.map{|s|s.to_i};puts (1..n).map{|i|(k!=1&&i= cart.size()\n now = cart.size()-1\n end\nend\ncnt = 0\nwhile cnt < pencil.size()\n cart[now].push(pencil[cnt])\n cnt += 1\n now += 1\n if now >= cart.size()\n now = cart.size()-1\n end\nend\n\nsum = 0\nk.times{|i|\n cart[i].sort!\n has_stool = false\n cart[i].each{|t|\n sum += t[0]*2\n has_stool = true if t[2] == 0\n }\n if has_stool\n sum -= cart[i][0][0]\n end\n}\nprint sum/2\nprint \".\"\nif sum%2 == 1\n puts \"5\"\nelse\n puts \"0\"\nend\n\nk.times{|i|\n print cart[i].size()\n cart[i].each{|t|\n print \" \"\n print t[1]+1\n }\n puts\n}\n"}, {"source_code": "n, k = gets.split.collect{|i| i.to_i}\ncarts = Array.new(k) {[]}\nitems = Array.new(n) {gets.split.collect{|i| i.to_i}}\nitems.each_index{|i|\n items[i] = [i+1, items[i][0], items[i][1]] \n}\n \nitems = items.sort{|a,b| b[1] <=> a[1]}\nstools = items.select{|x| x[2] == 1}\npencils = items.select{|x| x[2] == 2}\n \ncurr_k = 0\nprice = 0\nstools.each{|s|\n carts[curr_k] << s\n curr_k += 1 if curr_k < k - 1\n}\n\npencils.each{|p|\n carts[curr_k] << p\n curr_k += 1 if curr_k < k - 1 \n}\n\ncarts.each{|c|\n has_stool = c[0][2] == 1\n cart_items = c.sort{|a,b| a[1] <=> b[1]}\n cart_items.each{|item|\n price += item[1] \n }\n price -= cart_items[0][1] * 0.5 if has_stool\n}\n\nputs \"%.1f\" % price\ncarts.each{|c|\n c = c.flatten\n idxs = 0.step(c.size - 1, 3).map{|i| c[i]}\n puts [idxs.size, idxs].join(\" \")\n}"}], "negative_code": [{"source_code": "n, k = gets.split.collect{|i| i.to_i}\ncarts = Array.new(k) {[]}\nitems = Array.new(n) {gets.split.collect{|i| i.to_i}}\nitems.each_index{|i|\n items[i] = [i+1, items[i][0], items[i][1]] \n}\n \nitems.sort{|a,b| a[1] <=> b[1]}\nstools = items.select{|x| x[2] == 1}\npencils = items.select{|x| x[2] == 2}\n \ncurr_k = 0\nprice = 0\nstools.each{|s|\n carts[curr_k] << s\n curr_k += 1 if curr_k < k - 1\n}\n\npencils.each{|p|\n carts[curr_k] << p\n curr_k += 1 if curr_k < k - 1 \n}\n\ncarts.each{|c|\n has_stool = c[0][2] == 1\n cart_items = c.sort{|a,b| a[1] <=> b[1]}\n cart_items.each{|item|\n price += item[1] \n }\n price -= cart_items[-1][1] * 0.5 if has_stool\n}\n\nputs \"%.1f\" % price\ncarts.each{|c|\n c = c.flatten\n idxs = 0.step(c.size - 1, 3).map{|i| c[i]}\n puts [idxs.size, idxs].join(\" \")\n}"}, {"source_code": "n, k = gets.split.collect{|i| i.to_i}\ncarts = Array.new(k) {[]}\nitems = Array.new(n) {gets.split.collect{|i| i.to_i}}\nitems.each_index{|i|\n items[i] = [i+1, items[i][0], items[i][1]] \n}\n \nitems.sort{|a,b| a[1] <=> b[1]}\nstools = items.select{|x| x[2] == 1}\npencils = items.select{|x| x[2] == 2}\n \ncurr_k = 0\nprice = 0\nstools.each{|s|\n carts[curr_k] << s\n curr_k += 1 if curr_k < k - 1\n}\n\npencils.each{|p|\n carts[curr_k] << p\n curr_k += 1 if curr_k < k - 1 \n}\n\ncarts.each{|c|\n has_stool = c[0][2] == 1\n cart_items = c.sort{|a,b| a[1] <=> b[1]}\n cart_items.each{|item|\n price += item[1] \n }\n price -= cart_items[0][1] * 0.5 if has_stool\n}\n\nputs \"%.1f\" % price\ncarts.each{|c|\n c = c.flatten\n idxs = 0.step(c.size - 1, 3).map{|i| c[i]}\n puts [idxs.size, idxs].join(\" \")\n}"}], "src_uid": "06c7834aa4d06d6fcebfa410054f1b8c"} {"nl": {"description": "In Disgaea as in most role-playing games, characters have skills that determine the character's ability to use certain weapons or spells. If the character does not have the necessary skill, he cannot use it. The skill level is represented as an integer that increases when you use this skill. Different character classes are characterized by different skills. Unfortunately, the skills that are uncommon for the given character's class are quite difficult to obtain. To avoid this limitation, there is the so-called transmigration. Transmigration is reincarnation of the character in a new creature. His soul shifts to a new body and retains part of his experience from the previous life. As a result of transmigration the new character gets all the skills of the old character and the skill levels are reduced according to the k coefficient (if the skill level was equal to x, then after transmigration it becomes equal to [kx], where [y] is the integral part of y). If some skill's levels are strictly less than 100, these skills are forgotten (the character does not have them any more). After that the new character also gains the skills that are specific for his class, but are new to him. The levels of those additional skills are set to 0. Thus, one can create a character with skills specific for completely different character classes via transmigrations. For example, creating a mage archer or a thief warrior is possible. You are suggested to solve the following problem: what skills will the character have after transmigration and what will the levels of those skills be?", "input_spec": "The first line contains three numbers n, m and k — the number of skills the current character has, the number of skills specific for the class into which the character is going to transmigrate and the reducing coefficient respectively; n and m are integers, and k is a real number with exactly two digits after decimal point (1 ≤ n, m ≤ 20, 0.01 ≤ k ≤ 0.99). Then follow n lines, each of which describes a character's skill in the form \"name exp\" — the skill's name and the character's skill level: name is a string and exp is an integer in range from 0 to 9999, inclusive. Then follow m lines each of which contains names of skills specific for the class, into which the character transmigrates. All names consist of lowercase Latin letters and their lengths can range from 1 to 20 characters, inclusive. All character's skills have distinct names. Besides the skills specific for the class into which the player transmigrates also have distinct names.", "output_spec": "Print on the first line number z — the number of skills the character will have after the transmigration. Then print z lines, on each of which print a skill's name and level, separated by a single space. The skills should be given in the lexicographical order.", "sample_inputs": ["5 4 0.75\naxe 350\nimpaler 300\nionize 80\nmegafire 120\nmagicboost 220\nheal\nmegafire\nshield\nmagicboost"], "sample_outputs": ["6\naxe 262\nheal 0\nimpaler 225\nmagicboost 165\nmegafire 0\nshield 0"], "notes": null}, "positive_code": [{"source_code": "\ninput=gets.split\nn,m=input[0..1].map(&:to_i)\nk=(input[2].to_f*100+1e-8).to_i\nres={}\nres.default=0\nn.times{\n input=gets.split\n input[1]=(input[1].to_i*k/100).to_i\n if input[1]>=100 then\n res.store(input[0],input[1])\n end\n}\nm.times{\n input=gets.split[0]\n res[input]+=0\n}\nputs res.length\nres.sort.each do|k, v|\n puts \"#{k} #{v}\"\nend\n"}, {"source_code": "params = gets.split(\" \").collect {|v| v.to_f}\nold_skill = Hash::new\nparams[0].to_i.times do\n old_list = gets.split(\" \")\n old_skill[old_list[0]] = old_list[1].to_i\nend\n\nnew_skill = Array::new\nparams[1].to_i.times do\n new_skill << gets.strip\nend\n\n\nskills = Hash::new\nold_skill.each do |k,v|\n level = (v*params[2]+0.0001).to_i\n skills[k] = level unless level<100\nend\n\nnew_skill.each do |v|\n skills[v] = 0 unless skills.key?(v)\nend\nskills = skills.to_a.sort\n\nputs skills.size\nskills.each do |v|\n print v[0], \" \", v[1].to_i, \"\\n\"\nend\n"}, {"source_code": "#!/usr/bin/ruby\n# coding: utf-8\nrequire 'scanf'\nrequire 'bigdecimal'\n\nn, m, k = gets.scanf '%d %d %s'\nk = BigDecimal k\n\nskills = {}\nn.times do\n s, t = gets.scanf '%s %d'\n if t*k >= 100\n skills[s] = (t*k).to_i\n end\nend\n\nm.times do\n s = gets.chomp\n if not skills.has_key? s\n skills[s] = 0\n end\nend\n\nputs skills.size\nputs skills.map { |k, v| \"#{k} #{v}\" }.sort\n"}, {"source_code": "require 'scanf'\na = gets.scanf(\"%d %d %f\")\nn = a[0]\nm = a[1]\nk_100 = (a[2] * 100 + 0.5).to_i\n\nh = Hash.new\n\nn.times do\n\ta = gets.scanf(\"%s %d\")\n\tif a[1] * k_100 >= 10000 then\n\t\th[a[0]] = a[1] * k_100 / 100\n\tend\nend\n\nm.times do\n\ts = gets.chomp!\n\tif h[s].nil? then\n\t\th[s] = 0\n\tend\nend\n\nans = Array.new\nh.each{|key, value|\n\tans.push(sprintf(\"%s %d\", key, value))\n}\n\nans.sort!\nputs ans.size\nans.each{|line|\n\tputs line\n}\n"}, {"source_code": "#!/usr/bin/env ruby\n\nargs = STDIN.gets.split(\" \")\nskill = []\nargs[0].to_i.times do\n skill << STDIN.gets.split(\" \")\nend\n\nskillname = skill.map {|i| i[0]}\n\nargs[1].to_i.times do\n n = STDIN.gets.chomp\n if skillname.index(n)\n skill[skillname.index(n)][1] = \"0\" if (skill[skillname.index(n)][1].to_i*args[2].to_f*(1.0000001)).to_i < 100\n else\n skill << [n,\"0\"]\n end\nend\n\nskill.sort! {|a,b| a[0] <=> b[0]}\nret =[]\nskill.each do |i|\n exp = (i[1].to_i*args[2].to_f*(1.0000001)).to_i\n ret << \"#{i[0]} #{exp}\" unless exp < 100 and i[1].to_i != 0\nend\n\nputs ret.size\nret.each do |s|\n puts s\nend\n"}], "negative_code": [{"source_code": "\ninput=gets.split\nn,m=input[0..1].map(&:to_i)\nk=(input[2].to_f*100).to_i\nres={}\nres.default=0\nn.times{\n input=gets.split\n input[1]=(input[1].to_i*k/100).to_i\n if input[1]>=100 then\n res.store(input[0],input[1])\n end\n}\nm.times{\n input=gets.split[0]\n res[input]+=0\n}\nputs res.length\nres.sort.each do|k, v|\n puts \"#{k} #{v}\"\nend\n"}, {"source_code": "\ninput=gets.split\nn,m=input[0..1].map(&:to_i)\nk=input[2].to_f\nres={}\nres.default=0\nn.times{\n input=gets.split\n input[1]=(input[1].to_i*k).to_i\n if input[1]>=100 then\n res.store(input[0],input[1])\n end\n}\nm.times{\n input=gets.split[0]\n res[input]+=0\n}\nres.sort.each do|k, v|\n puts \"#{k} #{v}\"\nend"}, {"source_code": "\ninput=gets.split\nn,m=input[0..1].map(&:to_i)\nk=input[2].to_f\nres={}\nres.default=0\nn.times{\n input=gets.split\n input[1]=(input[1].to_i*k).to_i\n if input[1]>=100 then\n res.store(input[0],input[1])\n end\n}\nm.times{\n input=gets.split[0]\n res[input]+=0\n}\nputs res.length\nres.sort.each do|k, v|\n puts \"#{k} #{v}\"\nend\n"}, {"source_code": "params = gets.split(\" \").collect {|v| v.to_f}\nold_skill = Hash::new\nparams[0].to_i.times do\n old_list = gets.split(\" \")\n old_skill[old_list[0]] = old_list[1].to_i\nend\n\nnew_skill = Array::new\nparams[1].to_i.times do\n new_skill << gets.strip\nend\n\n\nskills = Hash::new\nold_skill.each do |k,v|\n level = (v*100*params[2]/100).to_s.to_i\n skills[k] = level unless level<100\nend\n\nnew_skill.each do |v|\n skills[v] = 0 unless skills.key?(v)\nend\nskills = skills.to_a.sort\n\nputs skills.size\nskills.each do |v|\n print v[0], \" \", v[1].to_i, \"\\n\"\nend\n"}, {"source_code": "params = gets.split(\" \").collect {|v| v.to_f}\nold_skill = Hash::new\nparams[0].to_i.times do\n old_list = gets.split(\" \")\n old_skill[old_list[0]] = old_list[1].to_i\nend\n\nnew_skill = Array::new\nparams[1].to_i.times do\n new_skill << gets.strip\nend\n\n\nskills = Hash::new\nold_skill.each do |k,v|\n level = (v*100*params[2]/100).to_i\n skills[k] = level unless level<100\nend\n\nnew_skill.each do |v|\n skills[v] = 0 unless skills.key?(v)\nend\nskills = skills.to_a.sort\n\nputs skills.size\nskills.each do |v|\n print v[0], \" \", v[1].to_i, \"\\n\"\nend\n"}, {"source_code": "params = gets.split(\" \").collect {|v| v.to_f}\nold_skill = Hash::new\nparams[0].to_i.times do\n old_list = gets.split(\" \")\n old_skill[old_list[0]] = old_list[1].to_i\nend\n\nnew_skill = Array::new\nparams[1].to_i.times do\n new_skill << gets.strip\nend\n\n\nskills = Hash::new\nold_skill.each do |k,v|\n level = (v*params[2]).to_i\n skills[k] = level unless level<100\nend\n\nnew_skill.each do |v|\n skills[v] = 0 unless skills.key?(v)\nend\nskills = skills.to_a.sort\n\nputs skills.size\nskills.each do |v|\n print v[0], \" \", v[1].to_i, \"\\n\"\nend\n"}, {"source_code": "params = gets.split(\" \").collect {|v| v.to_f}\nold_skill = Hash::new\nparams[0].to_i.times do\n old_list = gets.split(\" \")\n old_skill[old_list[0]] = old_list[1].to_i\nend\n\nnew_skill = Array::new\nparams[1].to_i.times do\n new_skill << gets.strip\nend\n\n\nskills = Hash::new\nold_skill.each do |k,v|\n level = (v*params[2]).to_s.to_i\n skills[k] = level unless level<100\nend\n\nnew_skill.each do |v|\n skills[v] = 0 unless skills.key?(v)\nend\nskills = skills.to_a.sort\n\nputs skills.size\nskills.each do |v|\n print v[0], \" \", v[1].to_i, \"\\n\"\nend\n"}, {"source_code": "params = gets.split(\" \").collect {|v| v.to_f}\nold_skill = Hash::new\nparams[0].to_i.times do\n old_list = gets.split(\" \")\n old_skill[old_list[0]] = old_list[1].to_i\nend\n\nnew_skill = Array::new\nparams[1].to_i.times do\n new_skill << gets.strip\nend\n\n\nskills = Hash::new\nold_skill.each do |k,v|\n skills[k] = v*params[2] unless v*params[2]<100\nend\n\nnew_skill.each do |v|\n skills[v] = 0 unless skills.key?(v)\nend\nskills = skills.to_a.sort\n\nputs skills.size\nskills.each do |v|\n print v[0], \" \", v[1].to_i, \"\\n\"\nend\n"}, {"source_code": "params = gets.split(\" \").collect {|v| v.to_f}\nold_skill = Hash::new\nparams[0].to_i.times do\n old_list = gets.split(\" \")\n old_skill[old_list[0]] = old_list[1].to_i\nend\n\nnew_skill = Array::new\nparams[1].to_i.times do\n new_skill << gets.strip\nend\n\n\nskills = Hash::new\nold_skill.each do |k,v|\n level = (v*params[2]).to_s.to_f.to_i\n skills[k] = level unless level<100\nend\n\nnew_skill.each do |v|\n skills[v] = 0 unless skills.key?(v)\nend\nskills = skills.to_a.sort\n\nputs skills.size\nskills.each do |v|\n print v[0], \" \", v[1].to_i, \"\\n\"\nend\n"}, {"source_code": "#!/usr/bin/env ruby\n\nargs = STDIN.gets.split(\" \")\nskill = []\nargs[0].to_i.times do\n skill << STDIN.gets.split(\" \")\nend\n\nskillname = skill.map {|i| i[0]}\n\nargs[1].to_i.times do\n n = STDIN.gets.chomp\n if skillname.index(n)\n skill[skillname.index(n)][1] = \"0\" if skill[skillname.index(n)][1].to_i*args[2].to_f < 100\n else\n skill << [n,\"0\"]\n end\nend\n\nskill.sort! {|a,b| a[0] <=> b[0]}\nret =[]\nskill.each do |i|\n exp = (i[1].to_i*args[2].to_f).to_i\n ret << \"#{i[0]} #{exp}\" unless exp < 100 and i[1].to_i != 0\nend\n\nputs ret.size\nret.each do |s|\n puts s\nend\n"}], "src_uid": "7da1a5c4c76540e1c7dc06e4c908c8b4"} {"nl": {"description": "Polycarpus is the director of a large corporation. There are n secretaries working for the corporation, each of them corresponds via the famous Spyke VoIP system during the day. We know that when two people call each other via Spyke, the Spyke network assigns a unique ID to this call, a positive integer session number.One day Polycarpus wondered which secretaries are talking via the Spyke and which are not. For each secretary, he wrote out either the session number of his call or a 0 if this secretary wasn't talking via Spyke at that moment.Help Polycarpus analyze these data and find out the number of pairs of secretaries that are talking. If Polycarpus has made a mistake in the data and the described situation could not have taken place, say so.Note that the secretaries can correspond via Spyke not only with each other, but also with the people from other places. Also, Spyke conferences aren't permitted — that is, one call connects exactly two people.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 103) — the number of secretaries in Polycarpus's corporation. The next line contains n space-separated integers: id1, id2, ..., idn (0 ≤ idi ≤ 109). Number idi equals the number of the call session of the i-th secretary, if the secretary is talking via Spyke, or zero otherwise. Consider the secretaries indexed from 1 to n in some way.", "output_spec": "Print a single integer — the number of pairs of chatting secretaries, or -1 if Polycarpus's got a mistake in his records and the described situation could not have taken place.", "sample_inputs": ["6\n0 1 7 1 7 10", "3\n1 1 1", "1\n0"], "sample_outputs": ["2", "-1", "0"], "notes": "NoteIn the first test sample there are two Spyke calls between secretaries: secretary 2 and secretary 4, secretary 3 and secretary 5.In the second test sample the described situation is impossible as conferences aren't allowed."}, "positive_code": [{"source_code": "hs = Hash.new 0\nn = gets.to_i\ngets.split(' ').map(&:to_i).map{|x| hs[x]+=1}\nhs.delete(0)\nhs.delete_if{|k,v| v<2}\nif hs.any?{|k,v| v>2}\n puts -1\nelse\n puts hs.length\nend"}, {"source_code": "n = gets.to_i\narr = gets.split(\" \").map(&:to_i)\nkv = {}\narr.each do |e|\n if kv[e].nil? then \n kv[e] = 1\n else\n kv[e] = kv[e] + 1\n end\nend\nif kv.any? { |k,v| k>0 and v>2 } then\n puts -1\nelse\n puts kv.select { |k,v| k>0 and v==2 }.length\nend"}, {"source_code": "gets; res = 0; h = Hash.new(0)\n\ngets.split.map.each{|i|\n\tnext if (i == '0') \n\t\n\tif (h[i] == 2) then\n\t\tres = -1;\n\t\tbreak;\n\tend\n\t\n\tres += 1 if (h[i] == 1) \t\n\th[i] += 1\n}\n\np res"}, {"source_code": "n = gets.to_i\n\nt = true\nwk = gets.split.map{|i| i.to_i}\nasmas = {}\nres = 0\n\nn.times do |i|\n if wk[i] != 0\n if asmas[wk[i]] then\n asmas[wk[i]] += 1\n if asmas[wk[i]] == 2 then\n res +=1\n elsif asmas[wk[i]] > 2 then\n t = false\n end\n else\n asmas[wk[i]] = 1\n end\n end\nend\n\nif t then\n puts res\nelse\n puts \"-1\"\nend"}, {"source_code": "ids = Hash.new{0}\n$stdin.each_line.to_a[1].split(\" \").map(&:to_i).each do |id|\n next if id == 0\n ids[id] += 1\nend\n\nanswer = 0\n\nids.each do |id, count|\n if count > 2\n answer = -1\n break\n end\n\n if count == 2\n answer += 1\n end\nend\n\nputs answer\n"}, {"source_code": "num_calls = gets\ncalls = gets.split.collect {|i| i.to_i}\nnumbers = Hash.new(0)\ncalls.each do |call|\n\tnumbers[call] += 1 unless call == 0\n\tif numbers[call] > 2\n\t\tputs \"-1\"\n\t\texit\n\tend\nend\nputs numbers.values.count(2)\n"}, {"source_code": "n = gets.chomp.to_i\nh = {}\ngets.chomp.split(' ').map {|x| if x.to_i!=0; h[x].nil? ? h[x] = 1 : h[x]+=1; end;}\n\nv = h.values\nif v.any? {|x| x > 2}\n puts -1\nelse\n puts v.count(2)\nend"}, {"source_code": "def main\n num = gets.chomp.to_i\n lines = gets.chomp.split(' ').map(&:to_i)\n return if num != lines.count\n \n line_count = {}\n\n lines.each do |line|\n next if line == 0\n\n if line_count.include?(line)\n line_count[line] += 1\n if line_count[line] >= 3\n puts '-1'\n return\n end\n else\n line_count[line] = 1\n end\n end\n\n total = 0\n line_count.each { |k,v| total += 1 if v == 2 }\n\n puts total\nend\n\nmain"}, {"source_code": "n = gets.to_i\na = gets.split.map{|x|x.to_i}\nc = {}\nc.default = 0\na.each do |x|\n c[x] += 1 if x != 0\nend\nerr = false\nans = 0\nc.values.each do |x|\n err = true if x > 2\n ans += 1 if x == 2\nend\nif err\n puts -1\nelse\n puts ans\nend\n"}, {"source_code": "class A\n def initialize\n n = $stdin.gets.chomp.to_i\n list = $stdin.gets.chomp.split(' ').map(&:to_i)\n @list = Hash.new(0)\n count = 0\n\n list.each do |num|\n @list[num] += 1 if num != 0\n end\n\n @list.each do |key, value|\n case value\n when 1\n when 2\n count += 1\n else\n puts -1\n exit\n end\n end\n\n puts count\n end\nend\n\na = A.new"}, {"source_code": "require 'set'\nrequire 'rational'\nrequire 'mathn'\ninclude Math\n$stdin = File.open(\"./input.txt\", \"r\") if ENV[\"USER\"] == \"new\"\n# ====================================\n\ngets\ndata = gets.split.map(&:to_i)\nhash = Hash.new( 0 )\n\ndata.each do |value|\n hash[value]+=1\nend\n\nans = 0\nhash.to_a.each do |key, value|\n if value > 2 and key != 0 then\n ans = -1\n break\n end\n \n if value == 2 and key != 0 then\n ans+=1\n end\nend\n\nputs ans"}, {"source_code": "length = $stdin.readline.to_i\nids = $stdin.readline.split(\" \").map{|i| i.to_i}\nraise \"Input data error\" if length != ids.size\n\ncounter = Hash.new(0)\nids.each{|id| counter[id]+=1}\ncounter.delete(0)\nif counter.values.find_all{|v| v>2}.any?\n\tputs \"-1\"\nelse\n\tputs counter.values.find_all{|v| v==2}.size\nend"}, {"source_code": "n = gets.to_i\nids = gets.split.map(&:to_i).sort.take(n)\n\nm = ids.reduce [-1, 0, 0] do |memo, id|\n next memo if id == 0\n if memo[0] == id\n if memo[1] > 1\n puts -1\n exit\n else\n [id, 2, memo[2] + 1]\n end\n else\n [id, 1, memo[2]]\n end\nend\n\nputs m[2]\n"}, {"source_code": "number=Integer(gets.chomp)\nstr=gets.chomp\npolicarp=0;\nflag=0;\nlist = Array.new\nstr=str.split \nstr.each do |c_hop|\n next if c_hop=='0' \n break if flag==1\n if list.find_all{|elem| elem==c_hop}.size == 1\n policarp=policarp+1\n elsif list.find_all{|elem| elem==c_hop}.size > 1\n flag=1;\n end\n list=list << c_hop;\n end\nif flag==0\n puts policarp\nelse \n puts \"-1\"\nend "}, {"source_code": "begin\n ids = []\n conversations = {}\n count = 0\n\n n = gets.chomp\n\n ids = gets.chomp\n\n ids = ids.split(' ').map(&:to_i)\n\n if ids.size != n.to_i\n puts -1\n abort\n end\n\n ids.delete(0)\n\n ids.each { |id| conversations[id] ||= 0; conversations[id] += 1 }\n\n conversations.each { |key, value| if value > 2; puts -1; abort; end; count +=1 if value == 2 }\n\n puts count\nrescue Exception\n\nend\n"}, {"source_code": "lines = ARGF.readlines\n\na = lines.last.split ' '\nb = a.map(&:to_i).delete_if {|x| 0.eql?(x) }\nb = b.uniq.map{|x| b.count(x)}\nb << 0\nputs (b.max > 2) ? -1 : b.count(2)"}, {"source_code": "n = gets.to_i\na = gets.split(' ').map(&:to_i).reject(&:zero?).sort\nb = a.uniq\n\nif b.any? { |n| a.count(n) > 2 } \n puts -1\n exit 0\nend\n\nx = b.count do |n|\n a.count(n) == 2\nend\n\nputs x\n"}, {"source_code": "gets.chomp # Skip the first line\nid_counts = (gets.chomp.split.map { |x| x.to_i } - [0]).inject(Hash.new(0)) { |h, e| h[e] += 1; h }.values\nputs(id_counts.any? { |c| c > 2 } ? -1 : id_counts.select { |c| c > 1 }.size)"}, {"source_code": "at_exit do\n n = get_numbers_from_line[0]\n ids = get_numbers_from_line[0...n]\n count = 0\n ids.select{|x| x != 0}.group_by{|x| x}.each do |_, group|\n if group.length == 2\n count += 1\n elsif group.length > 2\n count = -1\n break\n end\n end\n puts count\nend\n\ndef get_numbers_from_line\n gets.strip.split.map(&:to_i)\nend"}], "negative_code": [{"source_code": "hs = Hash.new 0\nn = gets.to_i\ngets.split(' ').map(&:to_i).map{|x| hs[x]+=1}\nhs.delete_if{|k,v| v<2}\nif hs.any?{|k,v| v>2}\n puts -1\nelse\n puts hs.length\nend"}, {"source_code": "n = gets.to_i\narr = gets.split(\" \")\nkv = {}\narr.each do |e|\n if kv[:\"#{e}\"].nil? then \n kv[:\"#{e}\"] = 1\n else\n kv[:\"#{e}\"] = kv[:\"#{e}\"] + 1\n end\nend\n\nif kv.any? { |k,v| v>2 } or kv.values.inject(&:+)>n then\n puts -1\nelse\n puts kv.select { |k,v| v==2 }.length\nend"}, {"source_code": "n = gets.to_i\narr = gets.split(\" \").map(&:to_i)\nkv = {}\narr.each do |e|\n if kv[e].nil? then \n kv[e] = 1\n else\n kv[e] = kv[e] + 1\n end\nend\nputs kv.inspect\nif kv.any? { |k,v| k>0 and v>2 } then\n puts -1\nelse\n puts kv.select { |k,v| k>0 and v==2 }.length\nend"}, {"source_code": "n = gets\narr = gets.split(\" \")\nkv = {}\narr.each do |e|\n if kv[:\"#{e}\"].nil? then \n kv[:\"#{e}\"] = 1\n else\n kv[:\"#{e}\"] = kv[:\"#{e}\"] + 1\n end\nend\nif kv.any? { |k,v| v>2 } then\n puts -1\nelse\n puts kv.select { |k,v| v==2 }.length\nend"}, {"source_code": "n = gets.to_i\n\nt = true\nwk = gets.split.map{|i| i.to_i}\nasmas = {}\nres = 0\n\nn.times do |i|\n if asmas[wk[i]] then\n asmas[wk[i]] += 1\n if asmas[wk[i]] == 2 then\n res +=1\n elsif asmas[wk[i]] > 2 then\n t = false\n end\n else\n asmas[wk[i]] = 1\n end\nend\n\nif t then\n puts res\nelse\n puts \"-1\"\nend"}, {"source_code": "ids = Hash.new{0}\n$stdin.each_line.to_a[1].split(\" \").map(&:to_i).each do |id|\n ids[id] += 1\nend\n\nanswer = 0\n\nids.each do |id, count|\n if count > 2\n answer = -1\n break\n end\n\n if count == 2\n answer += 1\n end\nend\n\nputs answer\n"}, {"source_code": "num_calls = gets\ncalls = gets.split.collect {|i| i.to_i}\nnumbers = Hash.new(0)\ncalls.each do |call|\n\tnumbers[call] += 1\n\tif numbers[call] > 2\n\t\tputs \"-1\"\n\t\texit\n\tend\nend\nputs numbers.values.count(2)\n"}, {"source_code": "n = gets.chomp.to_i\nh = {}\ngets.chomp.split(' ').map {|x| x.to_i; h[x].nil? ? h[x] = 1 : h[x]+=1}\n\nv = h.values\nif v.any? {|x| x > 2}\n puts -1\nelse\n puts v.count(2)\nend"}, {"source_code": "n = gets.chomp.to_i\nh = {}\ngets.chomp.split(' ').map {|x| h[x].nil? ? h[x] = 1 : h[x]+=1}\n\nv = h.values\nif v.any? {|x| x > 2}\n puts -1\nelse\n puts v.count(2)\nend"}, {"source_code": "def main\n num = gets.chomp.to_i\n lines = gets.chomp.split(' ')\n return if num != lines.count\n \n line_count = {}\n\n lines.each do |line|\n next if line == 0\n\n if line_count.include?(line)\n line_count[line] += 1\n if line_count[line] >= 3\n puts '-1'\n return\n end\n else\n line_count[line] = 1\n end\n end\n\n total = 0\n line_count.each { |k,v| total += 1 if v == 2 }\n\n puts total\nend\n\nmain"}, {"source_code": "n = gets.to_i\na = gets.split.map{|x|x.to_i}\nc = {}\nc.default = 0\na.each do |x|\n c[x] += 1\nend\nerr = false\nans = 0\nc.values.each do |x|\n err = true if x > 2\n ans += 1 if x == 2\nend\nif err\n puts -1\nelse\n puts ans\nend\n"}, {"source_code": "class A\n def initialize\n n = $stdin.gets.chomp.to_i\n list = $stdin.gets.chomp.split(' ').map(&:to_i)\n @list = Hash.new(0)\n count = 0\n\n list.each do |num|\n @list[num] += 1\n end\n\n @list.each do |key, value|\n case value\n when 1\n when 2\n count += 1\n else\n puts -1\n exit\n end\n end\n\n puts count\n end\nend\n\na = A.new"}, {"source_code": "require 'set'\nrequire 'rational'\nrequire 'mathn'\ninclude Math\n$stdin = File.open(\"./input.txt\", \"r\") if ENV[\"USER\"] == \"new\"\n# ====================================\n\ngets\ndata = gets.split.map(&:to_i)\nhash = Hash.new( 0 )\n\ndata.each do |value|\n hash[value]+=1\nend\n\nans = 0\nhash.to_a.each do |key, value|\n if value == 2 and key != 0 then\n ans+=1\n end\nend\n\nputs ans"}, {"source_code": "require 'set'\nrequire 'rational'\nrequire 'mathn'\ninclude Math\n$stdin = File.open(\"./input.txt\", \"r\") if ENV[\"USER\"] == \"new\"\n# ====================================\n\ngets\ndata = gets.split.map(&:to_i)\nhash = Hash.new( 0 )\n\ndata.each do |value|\n hash[value]+=1\nend\n\nans = 0\nhash.to_a.each do |key, value|\n next if value == 1 or key == 0\n \n if value%2 != 0 then\n ans = -1\n break\n end\n \n ans+=1\nend\n\nputs ans"}, {"source_code": "require 'set'\nrequire 'rational'\nrequire 'mathn'\ninclude Math\n$stdin = File.open(\"./input.txt\", \"r\") if ENV[\"USER\"] == \"new\"\n# ====================================\n\ngets\ndata = gets.split.map(&:to_i)\nhash = Hash.new( 0 )\n\ndata.each do |value|\n hash[value]+=1\nend\n\nans = 0\nhash.to_a.each do |key, value|\n next if value == 1\n \n if value%2 != 0 then\n ans = -1\n break\n end\n \n ans+=1\nend\n\nputs ans"}, {"source_code": "length = $stdin.readline.to_i\nids = $stdin.readline.split(\" \").map{|i| i.to_i}\nraise \"Input data error\" if length != ids.size\n\ncounter = Hash.new(0)\nids.each{|id| counter[id]+=1}\nif counter.values.find_all{|v| v>2}.any?\n\tputs \"-1\"\nelse\n\tputs counter.values.find_all{|v| v==2}.size\nend"}, {"source_code": "n = gets.to_i\nids = gets.split.map(&:to_i).sort\n\nm = ids.reduce [-1, 0, 0] do |memo, id|\n if memo[0] == id\n if memo[1] > 1\n puts -1\n exit\n else\n [id, 2, memo[2] + 1]\n end\n else\n [id, 1, memo[2]]\n end\nend\n\nputs m[2]\n"}, {"source_code": "number=Integer(gets.chomp)\nstr=gets.chomp\npolicarp=0;\nflag=0;\nlist = Array.new\nstr=str.split \nstr.each do |c_hop|\n\tnext if c_hop==0;\n\tif list.find_all{|elem| elem==c_hop}.size == 1\n\t policarp=policarp+1\n\telsif list.find_all{|elem| elem==c_hop}.size > 1\n\t flag=1;\n\t\tbreak;\n\tend\n\tlist=list << c_hop;\n end\nif flag==0\n puts policarp\nelse \n puts \"-1\"\nend\t"}, {"source_code": "ids = []\nconversations = {}\ncount = 0\n\nn = gets.chomp\n\nids = gets.chomp\n\nids = ids.split(' ').map(&:to_i)\n\nids.delete(0)\n\nids.each { |id| conversations[id] ||= 0; conversations[id] += 1 }\n\nconversations.each { |key, value| if value > 2; puts -1; abort; end; count +=1 if value > 0 }\n\nputs count\n"}, {"source_code": "gets.chomp # Skip the first line\nid_counts = gets.chomp.split.map { |x| x.to_i }.inject(Hash.new(0)) { |h, e| h[e] += 1; h }.values\nputs(id_counts.any? { |c| c > 2 } ? -1 : id_counts.select { |c| c > 1 }.size)"}], "src_uid": "45e51f3dee9a22cee86e1a98da519e2d"} {"nl": {"description": "Can the greatest common divisor and bitwise operations have anything in common? It is time to answer this question.Suppose you are given a positive integer $$$a$$$. You want to choose some integer $$$b$$$ from $$$1$$$ to $$$a - 1$$$ inclusive in such a way that the greatest common divisor (GCD) of integers $$$a \\oplus b$$$ and $$$a \\> \\& \\> b$$$ is as large as possible. In other words, you'd like to compute the following function:$$$$$$f(a) = \\max_{0 < b < a}{gcd(a \\oplus b, a \\> \\& \\> b)}.$$$$$$Here $$$\\oplus$$$ denotes the bitwise XOR operation, and $$$\\&$$$ denotes the bitwise AND operation.The greatest common divisor of two integers $$$x$$$ and $$$y$$$ is the largest integer $$$g$$$ such that both $$$x$$$ and $$$y$$$ are divided by $$$g$$$ without remainder.You are given $$$q$$$ integers $$$a_1, a_2, \\ldots, a_q$$$. For each of these integers compute the largest possible value of the greatest common divisor (when $$$b$$$ is chosen optimally). ", "input_spec": "The first line contains an integer $$$q$$$ ($$$1 \\le q \\le 10^3$$$) — the number of integers you need to compute the answer for. After that $$$q$$$ integers are given, one per line: $$$a_1, a_2, \\ldots, a_q$$$ ($$$2 \\le a_i \\le 2^{25} - 1$$$) — the integers you need to compute the answer for. ", "output_spec": "For each integer, print the answer in the same order as the integers are given in input.", "sample_inputs": ["3\n2\n3\n5"], "sample_outputs": ["3\n1\n7"], "notes": "NoteFor the first integer the optimal choice is $$$b = 1$$$, then $$$a \\oplus b = 3$$$, $$$a \\> \\& \\> b = 0$$$, and the greatest common divisor of $$$3$$$ and $$$0$$$ is $$$3$$$.For the second integer one optimal choice is $$$b = 2$$$, then $$$a \\oplus b = 1$$$, $$$a \\> \\& \\> b = 2$$$, and the greatest common divisor of $$$1$$$ and $$$2$$$ is $$$1$$$.For the third integer the optimal choice is $$$b = 2$$$, then $$$a \\oplus b = 7$$$, $$$a \\> \\& \\> b = 0$$$, and the greatest common divisor of $$$7$$$ and $$$0$$$ is $$$7$$$."}, "positive_code": [{"source_code": "yo = {\n\t3=> 1,\n\t7=> 1,\n\t15=> 5,\n\t31=> 1,\n\t63=> 21,\n\t127=> 1,\n\t255=> 85,\n\t511=> 73,\n\t1023=> 341,\n\t2047=> 89,\n\t4095=> 1365,\n\t8191=> 1,\n\t16383=> 5461,\n\t32767=> 4681,\n\t65535=> 21845,\n\t131071=> 1,\n\t262143=> 87381,\n\t524287=> 1,\n\t1048575=> 349525,\n\t2097151=> 299593,\n\t4194303=> 1398101,\n\t8388607=> 178481,\n\t16777215=> 5592405,\n\t33554431=> 1082401}\n\t\n\t\ndef mo(n)\n\tres = 0\n\t(1...n).each{|x|\n\t\tif((n^x).gcd(n&x) > res) \n\t\t\tres = (x^n).gcd(x&n)\n\t\tend\n\t}\n\tres\nend\n\n\n#~ (2..(2**25-1)).each{|x|\n\t\n\t#~ if(x == 2**(Math.log2(x).to_i+1)-1) then\n\t\t#~ puts(\"#{x}: #{mo(x)}\")\n\t#~ end\n#~ }\n\ninclude Math\nq = gets.to_i\nq.times{\n\tn = gets.to_i\n\tif(yo[n]!=nil) then\n\t\tputs(yo[n])\n\telse\n\t\tputs(\"#{2**(log2(n).to_i+1)-1}\")\n\tend\n}\n"}, {"source_code": "DBG = !true\nq = gets.to_i\n\ngs = [1,1,1,1,5,1,21,1,85,73, 341,89,1365,1,5461,4681,\n 21845,1,87381,1,349525, 299593,1398101,178481,\n 5592405, 1082401]\n\nfor i in 0...q\n puts \"i #{i} loop\" if DBG\n a = gets.to_i\n for j in 1..25\n puts \"j #{j} loop\" if DBG\n next if 2**j <= a\n if a < 2**j - 1\n puts \"#{2**j - 1}\"\n else\n puts \"#{gs[j]}\"\n end\n break\n end\nend\n"}], "negative_code": [], "src_uid": "ce9a0649ccfc956559254f324dfa02a7"} {"nl": {"description": "Vasya has a grid with $$$2$$$ rows and $$$n$$$ columns. He colours each cell red, green, or blue.Vasya is colourblind and can't distinguish green from blue. Determine if Vasya will consider the two rows of the grid to be coloured the same.", "input_spec": "The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \\leq t \\leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \\leq n \\leq 100$$$) — the number of columns of the grid. The following two lines each contain a string consisting of $$$n$$$ characters, each of which is either R, G, or B, representing a red, green, or blue cell, respectively — the description of the grid.", "output_spec": "For each test case, output \"YES\" if Vasya considers the grid's two rows to be identical, and \"NO\" otherwise. You can output the answer in any case (for example, the strings \"yEs\", \"yes\", \"Yes\" and \"YES\" will be recognized as a positive answer).", "sample_inputs": ["6\n\n2\n\nRG\n\nRB\n\n4\n\nGRBG\n\nGBGB\n\n5\n\nGGGGG\n\nBBBBB\n\n7\n\nBBBBBBB\n\nRRRRRRR\n\n8\n\nRGBRRGBR\n\nRGGRRBGR\n\n1\n\nG\n\nG"], "sample_outputs": ["YES\nNO\nYES\nNO\nYES\nYES"], "notes": "NoteIn the first test case, Vasya sees the second cell of each row as the same because the second cell of the first row is green and the second cell of the second row is blue, so he can't distinguish these two cells. The rest of the rows are equal in colour. Therefore, Vasya will say that the two rows are coloured the same, even though they aren't.In the second test case, Vasya can see that the two rows are different.In the third test case, every cell is green or blue, so Vasya will think they are the same."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n gets\r\n a = gets.chomp.gsub('G', 'B')\r\n b = gets.chomp.gsub('G', 'B')\r\n puts a == b ? :YES : :NO\r\nend\r\n"}], "negative_code": [], "src_uid": "86a2e0854f9faf0b119d0d5e4b8fe952"} {"nl": {"description": "Peter decided to wish happy birthday to his friend from Australia and send him a card. To make his present more mysterious, he decided to make a chain. Chain here is such a sequence of envelopes A = {a1,  a2,  ...,  an}, where the width and the height of the i-th envelope is strictly higher than the width and the height of the (i  -  1)-th envelope respectively. Chain size is the number of envelopes in the chain. Peter wants to make the chain of the maximum size from the envelopes he has, the chain should be such, that he'll be able to put a card into it. The card fits into the chain if its width and height is lower than the width and the height of the smallest envelope in the chain respectively. It's forbidden to turn the card and the envelopes. Peter has very many envelopes and very little time, this hard task is entrusted to you.", "input_spec": "The first line contains integers n, w, h (1  ≤ n ≤ 5000, 1 ≤ w,  h  ≤ 106) — amount of envelopes Peter has, the card width and height respectively. Then there follow n lines, each of them contains two integer numbers wi and hi — width and height of the i-th envelope (1 ≤ wi,  hi ≤ 106).", "output_spec": "In the first line print the maximum chain size. In the second line print the numbers of the envelopes (separated by space), forming the required chain, starting with the number of the smallest envelope. Remember, please, that the card should fit into the smallest envelope. If the chain of maximum size is not unique, print any of the answers. If the card does not fit into any of the envelopes, print number 0 in the single line.", "sample_inputs": ["2 1 1\n2 2\n2 2", "3 3 3\n5 4\n12 11\n9 8"], "sample_outputs": ["1\n1", "3\n1 3 2"], "notes": null}, "positive_code": [{"source_code": "n,w,h = gets.split(' ').map(&:to_i)\narr = []\nn.times do |i|\n a,b = gets.split(' ').map(&:to_i)\n if a > w and b > h then\n arr.push([a,b,i])\n end\nend\n\narr.sort! {|a,b| if a[0] != b[0] then a[0] <=> b[0] else b[1] <=> a[1] end}\n\nbest = []\ncur = []\nfrom = []\n\narr.each_with_index do |v,i|\n lst = v[0]\n l,r = -1, best.length\n while r - l > 1 do\n e = ((l + r) >> 1)\n if best[e] >= v[1] then r = e\n else l = e end\n end\n if l == -1 then\n best[0] = v[1]\n cur[0] = i\n from.push(-1)\n else \n if l == best.length - 1 then\n best.push(v[1])\n cur.push(i)\n from.push(cur[l])\n else\n best[l + 1] = v[1]\n from.push(cur[l])\n cur[l + 1] = i\n end\n end\nend\n\nputs best.length\nc = cur.length > 0 ? cur.last : -1\nans = []\nwhile c != -1 do\n ans.push(arr[c][2])\n c = from[c]\nend\n\nans.reverse!\n\nputs ans.map{|i| i + 1}.join(' ')\n\n"}], "negative_code": [{"source_code": "n,w,h = gets.split(' ').map(&:to_i)\narr = []\nn.times do |i|\n a,b = gets.split(' ').map(&:to_i)\n if a > w and b > h then\n arr.push([a,b,i])\n end\nend\n\narr.sort! {|a,b| a[0] <=> b[0]}\n\nbest = []\ncur = []\nfrom = []\n\narr.each_with_index do |v,i|\n l,r = -1, best.length\n while r - l > 1 do\n e = ((l + r) >> 1)\n if best[e] >= v[1] then r = e\n else l = e end\n end\n if l == -1 then\n best[0] = v[1]\n cur[0] = i\n from.push(-1)\n else \n if l == best.length - 1 then\n best.push(v[1])\n cur.push(i)\n from.push(cur[l])\n else\n best[l + 1] = v[1]\n from.push(cur[l])\n cur[l + 1] = i\n end\n end\nend\n\nputs best.length\nc = cur.length > 0 ? cur.last : -1\nans = []\nwhile c != -1 do\n ans.push(arr[c][2])\n c = from[c]\nend\n\nans.reverse!\n\nputs ans.map{|i| i + 1}.join(' ')\n\n"}, {"source_code": "n,w,h = gets.split(' ').map(&:to_i)\narr = []\nn.times do |i|\n a,b = gets.split(' ').map(&:to_i)\n if a > w and b > h then\n arr.push([a,b,i])\n end\nend\n\narr.sort! {|a,b| a[0] <=> b[0]}\n\nbest = []\ncur = []\nfrom = []\n\nlst = -1\narr.each_with_index do |v,i|\n if v[0] == lst then \n from.push(-1) \n next \n end\n lst = v[0]\n l,r = -1, best.length\n while r - l > 1 do\n e = ((l + r) >> 1)\n if best[e] >= v[1] then r = e\n else l = e end\n end\n if l == -1 then\n best[0] = v[1]\n cur[0] = i\n from.push(-1)\n else \n if l == best.length - 1 then\n best.push(v[1])\n cur.push(i)\n from.push(cur[l])\n else\n best[l + 1] = v[1]\n from.push(cur[l])\n cur[l + 1] = i\n end\n end\nend\n\nputs best.length\nc = cur.length > 0 ? cur.last : -1\nans = []\nwhile c != -1 do\n ans.push(arr[c][2])\n c = from[c]\nend\n\nans.reverse!\n\nputs ans.map{|i| i + 1}.join(' ')\n\n"}], "src_uid": "6f88e3f4c8a8a444d44e58505a750d3e"} {"nl": {"description": "Sereja loves integer sequences very much. He especially likes stairs.Sequence a1, a2, ..., a|a| (|a| is the length of the sequence) is stairs if there is such index i (1 ≤ i ≤ |a|), that the following condition is met: a1 < a2 < ... < ai - 1 < ai > ai + 1 > ... > a|a| - 1 > a|a|.For example, sequences [1, 2, 3, 2] and [4, 2] are stairs and sequence [3, 1, 2] isn't.Sereja has m cards with numbers. He wants to put some cards on the table in a row to get a stair sequence. What maximum number of cards can he put on the table?", "input_spec": "The first line contains integer m (1 ≤ m ≤ 105) — the number of Sereja's cards. The second line contains m integers bi (1 ≤ bi ≤ 5000) — the numbers on the Sereja's cards.", "output_spec": "In the first line print the number of cards you can put on the table. In the second line print the resulting stairs.", "sample_inputs": ["5\n1 2 3 4 5", "6\n1 1 2 2 3 3"], "sample_outputs": ["5\n5 4 3 2 1", "5\n1 2 3 2 1"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/ruby\n\ngets\na = gets.split.map(&:to_i)\n\ngrouped = a.group_by {|x| x }.to_a.sort.reverse\ntail = [grouped.shift[0]]\nhead = []\ngrouped.each do |val,seq|\n if seq.size >= 2\n tail << val\n head << val\n else\n tail << val\n end\nend\n\nr = head.reverse + tail\nputs r.size\nputs r.join(\" \")\n\n"}, {"source_code": "STDIN.gets\ncards = STDIN.gets.strip.split.map {|x| x.to_i}\ncards.sort!\n\nstairs = []\nleftovers = []\nprev = 0\n\ncards.each do |x|\n if x == prev\n leftovers << x \n else\n stairs << x\n end\n prev = x\nend\n\nleftovers.reverse.each do |x|\n stairs << x unless x == prev\n prev = x\nend\n\nSTDOUT.puts stairs.size\nSTDOUT.puts stairs.reverse.join(\" \")"}, {"source_code": "m = $stdin.gets.to_i\ncards = $stdin.gets.chomp.split(' ').map{|e| e.to_i}\n\ncards.sort! {|a, b| b <=> a}\nladder = [cards.shift]\ncards.each do |e|\n if e < ladder.last\n ladder.push e \n elsif e < ladder.first\n ladder.unshift e\n end\nend\n\nputs ladder.size\nputs ladder.join(\" \")"}, {"source_code": "m=gets.to_i\nb=gets.split.map(&:to_i)\nb.sort!.reverse!\na=[b.shift]\nc=0\nwhile b.size>0\n d=b.shift\n if c%2==0\n a.unshift d if a[0]>d\n else\n a<d\n end\n c+=1\nend\nputs a.size,a*' '\n\n\n"}, {"source_code": "i = gets.to_i\ncards = gets.split.map(&:to_i)\n\nif cards.uniq.length == i\n puts i\n puts cards.sort.reverse.join ' '\nelse\n card_grps = cards.group_by(&:to_i).to_a.sort_by(&:first).map(&:last)\n result = []\n card_grps.each { |cs| result.push cs.pop }\n rev = card_grps.reverse\n rev.shift\n rev.each { |cs| result.push cs.pop unless cs.empty? }\n # p card_grps\n # p rev\n # p result\n puts result.count\n puts result.join ' '\nend\n"}, {"source_code": "cardNum = gets.chomp.to_i\ncards = gets.chomp.split(' ').map {|i| i.to_i}\nold = -1\n\nif cards.uniq.length == cards.length\n\tputs \"#{cards.length}\"\n\tcards.reverse_each do |item|\n\t\tprint \"#{item} \"\n\tend\nelse\n\tfrontList = []\n\tbackList = []\n\tcards.sort!\n\t\n\tcards.each do |item|\n\t\tfrontList << item if old != item\n\t\tbackList << item if old == item\n\t\told = item\n\tend\n\n#\tprint \"#{frontList}\"\n#\tprint \"#{backList}\"\n\n\tbackList.uniq!\n\tbackList.delete_at(backList.length - 1) if frontList[frontList.length - 1] == backList[backList.length - 1]\n\tputs \"#{frontList.length + backList.length}\"\n\n\tfrontList.each do |item|\n\t\tprint \"#{item} \"\n\tend\n\n\tbackList.reverse_each do |item|\n\t\tprint \"#{item} \"\n\tend\nend"}], "negative_code": [{"source_code": "STDIN.gets\ncards = STDIN.gets.strip.split.map {|x| x.to_i}\ncards.sort!\n\nstairs = []\nleftovers = []\nprev = 0\n\ncards.each do |x|\n if x == prev\n leftovers << x \n else\n stairs << x\n end\n prev = x\nend\n\nleftovers.reverse.each do |x|\n stairs << x unless x == prev\nend\n\nSTDOUT.puts stairs.size\nSTDOUT.puts stairs.reverse.join(\" \")"}, {"source_code": "STDIN.gets\ncards = STDIN.gets.strip.split.map {|x| x.to_i}\ncards.sort!\n\nstairs = []\nleftovers = []\nprev = 0\n\ncards.each do |x|\n if x == prev\n leftovers << x \n else\n stairs << x\n end\n prev = x\nend\n\nleftovers.reverse.each do |x|\n stairs << x unless x == prev\n x = prev\nend\n\nSTDOUT.puts stairs.size\nSTDOUT.puts stairs.reverse.join(\" \")"}, {"source_code": "STDIN.gets\ncards = STDIN.gets.strip.split.map {|x| x.to_i}\ncards.sort!\n\nstairs = []\nleftovers = []\nprev = 0\n\ncards.each do |x|\n if x == prev\n leftovers << x \n else\n stairs << x\n end\n prev = x\nend\n\nleftovers.reverse.each do |x|\n stairs << x unless x == prev\nend\n\np stairs.size\np stairs.join(\" \")"}, {"source_code": "cardNum = gets.chomp.to_i\ncards = gets.chomp.split(' ').map {|i| i.to_i}\nold = -1\n\nif cards.uniq.length == cards.length\n\tputs \"#{cards.length}\"\n\tcards.reverse_each do |item|\n\t\tprint \"#{item} \"\n\tend\nelse\n\tfrontList = []\n\tbackList = []\n\tcards.sort!\n\t\n\tcards.each do |item|\n\t\tfrontList << item if old != item\n\t\tbackList << item if old == item\n\t\told = item\n\tend\n\n#\tprint \"#{frontList}\"\n#\tprint \"#{backList}\"\n\n\tbackList.delete_at(backList.length - 1) if frontList[frontList.length - 1] == backList[backList.length - 1]\n\tputs \"#{frontList.length + backList.length}\"\n\n\tfrontList.each do |item|\n\t\tprint \"#{item} \"\n\tend\n\n\tbackList.reverse_each do |item|\n\t\tprint \"#{item} \"\n\tend\nend"}, {"source_code": "cardNum = gets.chomp.to_i\ncards = gets.chomp.split(' ').map {|i| i.to_i}\n\nif cards.uniq.length == cards.length\n\tputs \"#{cards.length}\"\n\tcards.reverse_each do |item|\n\t\tprint \"#{item} \"\n\tend\nelse\n\ttempCards = cards.uniq\n\ttempCards.sort!\n\n\tlist = []\n\n\ttempCards.each do |item|\n\t\t#print \"#{item} \"\n\t\tlist << item\n\t\tcards.delete_at(cards.index(item))\n\tend\n\n\tif list[list.length - 1] == cards[cards.length - 1]\n\t\tcards.delete_at(cards.length - 1)\n\tend\n\n\tcards.reverse_each do |item|\n\t\tlist << item\n\tend\n\n\tputs \"#{list.length}\"\n\tlist.each do |item|\n\t\tprint \"#{item} \"\n\tend\nend"}], "src_uid": "5c63f91eb955cb6c3172cb7c8f78976c"} {"nl": {"description": "The Fair Nut found a string $$$s$$$. The string consists of lowercase Latin letters. The Nut is a curious guy, so he wants to find the number of strictly increasing sequences $$$p_1, p_2, \\ldots, p_k$$$, such that: For each $$$i$$$ ($$$1 \\leq i \\leq k$$$), $$$s_{p_i} =$$$ 'a'. For each $$$i$$$ ($$$1 \\leq i < k$$$), there is such $$$j$$$ that $$$p_i < j < p_{i + 1}$$$ and $$$s_j =$$$ 'b'. The Nut is upset because he doesn't know how to find the number. Help him.This number should be calculated modulo $$$10^9 + 7$$$.", "input_spec": "The first line contains the string $$$s$$$ ($$$1 \\leq |s| \\leq 10^5$$$) consisting of lowercase Latin letters.", "output_spec": "In a single line print the answer to the problem — the number of such sequences $$$p_1, p_2, \\ldots, p_k$$$ modulo $$$10^9 + 7$$$.", "sample_inputs": ["abbaa", "baaaa", "agaa"], "sample_outputs": ["5", "4", "3"], "notes": "NoteIn the first example, there are $$$5$$$ possible sequences. $$$[1]$$$, $$$[4]$$$, $$$[5]$$$, $$$[1, 4]$$$, $$$[1, 5]$$$.In the second example, there are $$$4$$$ possible sequences. $$$[2]$$$, $$$[3]$$$, $$$[4]$$$, $$$[5]$$$.In the third example, there are $$$3$$$ possible sequences. $$$[1]$$$, $$$[3]$$$, $$$[4]$$$."}, "positive_code": [{"source_code": "MOD = 10**9 + 7\nss = STDIN.gets.strip\nchunks = ss.split(/b+/).map do |s|\n cnt = 0\n for i in 0..(s.length-1)\n cnt += 1 if s[i] == 'a'\n end\n cnt\nend\nans = 1\nchunks.each do |n|\n ans *= n+1\n ans %= MOD\nend\nputs (ans-1) % MOD"}], "negative_code": [], "src_uid": "a765463559901e608035083be47aa245"} {"nl": {"description": "Polycarpus is a system administrator. There are two servers under his strict guidance — a and b. To stay informed about the servers' performance, Polycarpus executes commands \"ping a\" and \"ping b\". Each ping command sends exactly ten packets to the server specified in the argument of the command. Executing a program results in two integers x and y (x + y = 10; x, y ≥ 0). These numbers mean that x packets successfully reached the corresponding server through the network and y packets were lost.Today Polycarpus has performed overall n ping commands during his workday. Now for each server Polycarpus wants to know whether the server is \"alive\" or not. Polycarpus thinks that the server is \"alive\", if at least half of the packets that we send to this server reached it successfully along the network.Help Polycarpus, determine for each server, whether it is \"alive\" or not by the given commands and their results.", "input_spec": "The first line contains a single integer n (2 ≤ n ≤ 1000) — the number of commands Polycarpus has fulfilled. Each of the following n lines contains three integers — the description of the commands. The i-th of these lines contains three space-separated integers ti, xi, yi (1 ≤ ti ≤ 2; xi, yi ≥ 0; xi + yi = 10). If ti = 1, then the i-th command is \"ping a\", otherwise the i-th command is \"ping b\". Numbers xi, yi represent the result of executing this command, that is, xi packets reached the corresponding server successfully and yi packets were lost. It is guaranteed that the input has at least one \"ping a\" command and at least one \"ping b\" command.", "output_spec": "In the first line print string \"LIVE\" (without the quotes) if server a is \"alive\", otherwise print \"DEAD\" (without the quotes). In the second line print the state of server b in the similar format.", "sample_inputs": ["2\n1 5 5\n2 6 4", "3\n1 0 10\n2 0 10\n1 10 0"], "sample_outputs": ["LIVE\nLIVE", "LIVE\nDEAD"], "notes": "NoteConsider the first test case. There 10 packets were sent to server a, 5 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall there were 10 packets sent to server b, 6 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network.Consider the second test case. There were overall 20 packages sent to server a, 10 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall 10 packets were sent to server b, 0 of them reached it. Therefore, less than half of all packets sent to this server successfully reached it through the network."}, "positive_code": [{"source_code": "n = gets.to_i\na = [[], []] # a b\nn.times do\n s = gets.split.map(&:to_i)\n (s[0] == 1 ? a[0] : a[1]).push [s[1], s[2]]\nend\nputs (a[0].transpose[0].inject(:+) >= a[0].transpose[1].inject(:+) ? :LIVE : :DEAD)\nputs (a[1].transpose[0].inject(:+) >= a[1].transpose[1].inject(:+) ? :LIVE : :DEAD)"}, {"source_code": "n = gets.to_i\nat = 0\nab = 0\nbt = 0\nbb = 0\nn.times do\n t, x, y = gets.split.map(&:to_f)\n if t == 1.0\n at += x\n ab += x + y\n else\n bt += x\n bb += x + y\n end\nend\n\nif at / ab >= 0.5\n puts 'LIVE'\nelse\n puts 'DEAD'\nend\n\nif bt / bb >= 0.5\n puts 'LIVE'\nelse\n puts 'DEAD'\nend\n"}, {"source_code": "n=gets.to_i\na=[0,0]\nc=[0,0]\nn.times {\n t,s,f=gets.split.map(&:to_i)\n a[t-1]+=s\n c[t-1]+=1\n}\nfor i in 0..1 do\n puts a[i]>=5*c[i] ? \"LIVE\" : \"DEAD\"\nend"}, {"source_code": "#! /usr/bin/ruby\n$n = gets.to_i\n$k, $p = [0,0,0], [0,0,0]\n(1..$n).each do\n k, x, y = gets.split(' ').map(&:to_i)\n $k[k] += x\n $p[k] += 10\nend\n(1..2).each{ |i| if $k[i] * 2 >= $p[i] then puts \"LIVE\" else puts \"DEAD\" end }\n"}, {"source_code": "n = gets.to_i\nk = 0\nx = 0.0\ny = 0.0\nn.times do\n a = []\n gets.split(\" \").each do |st|\n a << st.to_i\n end\n if a[0] == 1 then\n x += a[1]/10.0\n k += 1\n else\n y += a[1]/10.0\n end\nend\nif (x/k >= 0.5) then\n puts \"LIVE\"\nelse\n puts \"DEAD\"\nend\n\nif (y/(n-k) >= 0.5) then\n puts \"LIVE\"\nelse\n puts \"DEAD\"\nend"}, {"source_code": "n = gets.to_i\na, c = [0,0], [0,0]\nn.times do\n t,x,y = gets.split(' ').map(&:to_i)\n a[t-1] += x\n c[t-1] += 1\nend\nfor i in 0..1 do\n puts a[i]>=5*c[i] ? \"LIVE\" : \"DEAD\"\nend"}, {"source_code": "n = gets.to_i\nxx = Array.new(2,0)\nyy = Array.new(2,0)\n(1..n).each do\n t, x, y = gets.split.collect{|i| i.to_i}\n xx[t-1] += x\n yy[t-1] += y\nend\n(0..1).each do |i|\n if xx[i] >= yy[i]\n puts 'LIVE'\n else\n puts 'DEAD'\n end\nend\n"}, {"source_code": "n = gets.to_i\n\na = [0,0]\nb = [0,0]\n\nn.times do\n t,x,y = *(gets.split.map &:to_i)\n if t==1\n a[0] += x\n a[1] += x+y\n else\n b[0] += x\n b[1] += x+y\n end\nend\n\nputs a[0]>=a[1]/2 ? 'LIVE' : 'DEAD'\nputs b[0]>=b[1]/2 ? 'LIVE' : 'DEAD'"}, {"source_code": "p = {1 => [0, 0], 2 => [0, 0]}\nfor _ in 1..gets.chomp.to_i\n h, v = gets.chomp.split.map { |x| x.to_i }[0..1]\n a = p[h == 1 ? 1 : 2]\n a[0] += v\n a[1] += 1\nend\np.values.each {|a| puts(a[0] >= 10 * a[1] / 2 ? 'LIVE' : 'DEAD') }"}, {"source_code": "n = gets.to_i\ns = []\ngood1 = 0\ngood2 = 0\nbad1 = 0\nbad2 = 0\nn.times do |i|\n s[i] = gets.split\n s[i] = s[i].collect {|j| j = j.to_i}\n if s[i][0] == 1 then\n good1 += s[i][1]\n bad1 += s[i][2] \n else\n good2 += s[i][1]\n bad2 += s[i][2]\n end\nend\nif good1 >= bad1 then \n puts \"LIVE\"\nelse\n puts \"DEAD\"\nend\nif good2 >= bad2 then\n puts \"LIVE\"\nelse\n puts \"DEAD\"\nend"}, {"source_code": "x1,x2,n1,n2=0,0,0,0\ngets.to_i.times do \n\ta=gets.split.map(&:to_i)\n\tcase a[0]\n\twhen 1\n\t\tx1+=a[1]\n\t\tn1+=1\n\twhen 2\n\t\tx2+=a[1]\n\t\tn2+=1\n\tend\nend\n(10.0*n1/x1<=2.0&&x1!=0)?(puts \"LIVE\"):(puts \"DEAD\")\n(10.0*n2/x2<=2.0&&x2!=0)?(puts \"LIVE\"):(puts \"DEAD\")\n\n\t\n"}, {"source_code": "num_pings = gets.to_i\nnum = {1 => 0, 2 => 0}\nsuccessful = {1 => 0, 2 => 0}\nfor i in 0...num_pings\n\ttemp = gets.split.collect {|i| i.to_i}\n\tnum[temp[0]] += 10\n\tsuccessful[temp[0]] += temp[1]\nend\n(1..2).each do |n|\n\tif num[n]/2 <= successful[n]\n\t\tputs \"LIVE\"\n\telse\n\t\tputs \"DEAD\"\n\tend\nend\n"}, {"source_code": "n = gets.to_i\ns = [[0, 0], [0, 0]]\nn.times do\n t, x, y = gets.split.map(&:to_i)\n s[t-1][0] += x\n s[t-1][1] += 10\nend\ns.each {|a| if a[0] >= a[1]/2 then puts \"LIVE\" else puts \"DEAD\" end}"}], "negative_code": [{"source_code": "n = gets.to_i\ns = [\"LIVE\", \"LIVE\"]\nn.times do\n t, x, y = gets.split.map(&:to_i)\n if x >= 5 then\n s[t-1] = \"LIVE\"\n else\n s[t-1] = \"DEAD\"\n end\nend\ns.each {|x| puts x}"}, {"source_code": "n = gets.to_i\nat = 0\nab = 0\nbt = 0\nbb = 0\nn.times do\n c, t, b = gets.split.map(&:to_f)\n if c == 1.0\n at += t\n ab += b\n else\n bt += t\n bb += b\n end\nend\n\nif at / ab >= 0.5\n puts 'LIVE'\nelse\n puts 'DEAD'\nend\n\nif bt / bb >= 0.5\n puts 'LIVE'\nelse\n puts 'DEAD'\nend\n"}, {"source_code": "n = gets.to_i\ns = [\"DEAD\", \"DEAD\"]\nn.times do\n t, x, y = gets.split.map(&:to_i)\n if x >= 5 then\n s[t-1] = \"LIVE\"\n else\n s[t-1] = \"DEAD\"\n end\nend\ns.each {|x| puts x}"}], "src_uid": "1d8870a705036b9820227309d74dd1e8"} {"nl": {"description": "Ivan decided to prepare for the test on solving integer equations. He noticed that all tasks in the test have the following form: You are given two positive integers $$$u$$$ and $$$v$$$, find any pair of integers (not necessarily positive) $$$x$$$, $$$y$$$, such that: $$$$$$\\frac{x}{u} + \\frac{y}{v} = \\frac{x + y}{u + v}.$$$$$$ The solution $$$x = 0$$$, $$$y = 0$$$ is forbidden, so you should find any solution with $$$(x, y) \\neq (0, 0)$$$. Please help Ivan to solve some equations of this form.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 10^3$$$) — the number of test cases. The next lines contain descriptions of test cases. The only line of each test case contains two integers $$$u$$$ and $$$v$$$ ($$$1 \\leq u, v \\leq 10^9$$$) — the parameters of the equation.", "output_spec": "For each test case print two integers $$$x$$$, $$$y$$$ — a possible solution to the equation. It should be satisfied that $$$-10^{18} \\leq x, y \\leq 10^{18}$$$ and $$$(x, y) \\neq (0, 0)$$$. We can show that an answer always exists. If there are multiple possible solutions you can print any.", "sample_inputs": ["4\n1 1\n2 3\n3 5\n6 9"], "sample_outputs": ["-1 1\n-4 9\n-18 50\n-4 9"], "notes": "NoteIn the first test case: $$$\\frac{-1}{1} + \\frac{1}{1} = 0 = \\frac{-1 + 1}{1 + 1}$$$.In the second test case: $$$\\frac{-4}{2} + \\frac{9}{3} = 1 = \\frac{-4 + 9}{2 + 3}$$$.In the third test case: $$$\\frac{-18}{3} + \\frac{50}{5} = 4 = \\frac{-18 + 50}{3 + 5}$$$.In the fourth test case: $$$\\frac{-4}{6} + \\frac{9}{9} = \\frac{1}{3} = \\frac{-4 + 9}{6 + 9}$$$."}, "positive_code": [{"source_code": "za=gets.chomp.to_i\r\nza.times do\r\n\ta,b=gets.split.map(&:to_i)\r\n\tprint -a*a,\" \",b*b\r\n\tputs \"\"\r\nend"}], "negative_code": [], "src_uid": "4dfa99acbe06b314f0f0b934237c66f3"} {"nl": {"description": "One day, Vogons wanted to build a new hyperspace highway through a distant system with $$$n$$$ planets. The $$$i$$$-th planet is on the orbit $$$a_i$$$, there could be multiple planets on the same orbit. It's a pity that all the planets are on the way and need to be destructed.Vogons have two machines to do that. The first machine in one operation can destroy any planet at cost of $$$1$$$ Triganic Pu. The second machine in one operation can destroy all planets on a single orbit in this system at the cost of $$$c$$$ Triganic Pus. Vogons can use each machine as many times as they want.Vogons are very greedy, so they want to destroy all planets with minimum amount of money spent. Can you help them to know the minimum cost of this project?", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. Then the test cases follow. Each test case consists of two lines. The first line contains two integers $$$n$$$ and $$$c$$$ ($$$1 \\le n, c \\le 100$$$) — the number of planets and the cost of the second machine usage. The second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 100$$$), where $$$a_i$$$ is the orbit of the $$$i$$$-th planet.", "output_spec": "For each test case print a single integer — the minimum cost of destroying all planets.", "sample_inputs": ["4\n\n10 1\n\n2 1 4 5 2 4 5 5 1 2\n\n5 2\n\n3 2 1 2 2\n\n2 2\n\n1 1\n\n2 2\n\n1 2", "1\n\n1 100\n\n1"], "sample_outputs": ["4\n4\n2\n2", "1"], "notes": "NoteIn the first test case, the cost of using both machines is the same, so you can always use the second one and destroy all planets in orbit $$$1$$$, all planets in orbit $$$2$$$, all planets in orbit $$$4$$$, all planets in orbit $$$5$$$.In the second test case, it is advantageous to use the second machine for $$$2$$$ Triganic Pus to destroy all the planets in orbit $$$2$$$, then destroy the remaining two planets using the first machine.In the third test case, you can use the first machine twice or the second machine once.In the fourth test case, it is advantageous to use the first machine twice."}, "positive_code": [{"source_code": "def main\n t = inp.to_i\n t.times do\n n, c = mapint(inarr)\n orb = Array.new(101, 0)\n mapint(inarr).each { |e|\n orb[e - 1]+=1\n }\n cst = 0\n orb.each { |e|\n cst += [c, e].min\n }\n puts cst\n end\n\nend\n\n# lib\ndef inp\n gets.chomp\nend\n\ndef inarr\n gets.chomp.split\nend\n\ndef mapint(lst)\n lst.map(&:to_i)\nend\n\nmain"}], "negative_code": [], "src_uid": "2805d460df1121c4410999a9f36e383a"} {"nl": {"description": "You are given $$$n$$$ words of equal length $$$m$$$, consisting of lowercase Latin alphabet letters. The $$$i$$$-th word is denoted $$$s_i$$$.In one move you can choose any position in any single word and change the letter at that position to the previous or next letter in alphabetical order. For example: you can change 'e' to 'd' or to 'f'; 'a' can only be changed to 'b'; 'z' can only be changed to 'y'. The difference between two words is the minimum number of moves required to make them equal. For example, the difference between \"best\" and \"cost\" is $$$1 + 10 + 0 + 0 = 11$$$.Find the minimum difference of $$$s_i$$$ and $$$s_j$$$ such that $$$(i < j)$$$. In other words, find the minimum difference over all possible pairs of the $$$n$$$ words.", "input_spec": "The first line of the input contains a single integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains $$$2$$$ integers $$$n$$$ and $$$m$$$ ($$$2 \\leq n \\leq 50$$$, $$$1 \\leq m \\leq 8$$$) — the number of strings and their length respectively. Then follows $$$n$$$ lines, the $$$i$$$-th of which containing a single string $$$s_i$$$ of length $$$m$$$, consisting of lowercase Latin letters.", "output_spec": "For each test case, print a single integer — the minimum difference over all possible pairs of the given strings.", "sample_inputs": ["6\n\n2 4\n\nbest\n\ncost\n\n6 3\n\nabb\n\nzba\n\nbef\n\ncdu\n\nooo\n\nzzz\n\n2 7\n\naaabbbc\n\nbbaezfe\n\n3 2\n\nab\n\nab\n\nab\n\n2 8\n\naaaaaaaa\n\nzzzzzzzz\n\n3 1\n\na\n\nu\n\ny"], "sample_outputs": ["11\n8\n35\n0\n200\n4"], "notes": "NoteFor the second test case, one can show that the best pair is (\"abb\",\"bef\"), which has difference equal to $$$8$$$, which can be obtained in the following way: change the first character of the first string to 'b' in one move, change the second character of the second string to 'b' in $$$3$$$ moves and change the third character of the second string to 'b' in $$$4$$$ moves, thus making in total $$$1 + 3 + 4 = 8$$$ moves.For the third test case, there is only one possible pair and it can be shown that the minimum amount of moves necessary to make the strings equal is $$$35$$$.For the fourth test case, there is a pair of strings which is already equal, so the answer is $$$0$$$."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n n, m = gets.split.map(&:to_i)\r\n ss = n.times.map { gets.chomp.bytes.map { |c| c - 97 } }\r\n answer = ss.flat_map.with_index do |s1, i|\r\n ss[i + 1..].map do |s2|\r\n m.times.map do |i|\r\n (s1[i] - s2[i]).abs\r\n end.sum\r\n end\r\n end.min\r\n p answer\r\nend\r\n"}, {"source_code": "def chardist(a, b)\n xs = [a.ord, b.ord].sort\n xs[1] - xs[0]\nend\n\ndef dist(a, b)\n fail unless a.length == b.length\n m = a.length\n (0...m).map { |i| chardist(a[i], b[i]) }.sum\nend\n\ngets.to_i.times do\n n, _ = gets.split.map(&:to_i)\n ss = Array.new(n) { gets.strip }\n\n ans = nil\n n.times do |r|\n r.times do |l|\n cur = dist(ss[l], ss[r])\n ans = cur if !ans || cur < ans\n end\n end\n\n p ans\nend\n"}, {"source_code": "def diff(s, t)\n r = 0\n s.chars.each_with_index do |c, i|\n r += (c.ord - t[i].ord).abs\n end\n\n r\nend\n\ndef main\n gets.to_i.times do\n n, m = gli\n a = []\n ans = 100_100_100_100\n n.times { a << gets.chomp }\n\n (0...n).each do |i|\n (i + 1...n).each do |j|\n ans = [ans, diff(a[i], a[j])].min\n end\n end\n puts ans\n end\nend\n\nMOD = 10**9 + 7\ndef gs = gets.chomp\ndef gi = gets.to_i\ndef gli = gets.split.map(&:to_i)\ndef cyesno(x) = x ? :Yes : :No\ndef uyesno(x) = x ? :YES : :NO\n\nmain\n"}, {"source_code": "main = -> {\r\n t = int\r\n t.times do\r\n n, m = ints\r\n s = rep(n) { string.each_byte.to_a }\r\n min = INF\r\n s.combination(2) do |(a, b)|\r\n m = a.zip(b).sum { |x, y| (x - y).abs }\r\n min = [m, min].min\r\n end\r\n puts min\r\n end\r\n}\r\n\r\nDEBUG = true\r\nMOD = 10**9+7\r\n$yesno = %w(No Yes)\r\nYesNo = %w(No Yes)\r\nYESNO = %w(NO YES)\r\nINF = 10**18\r\n\r\nrequire \"set\"\r\nrequire \"prime\"\r\n\r\ndef int; gets.to_s.to_i end\r\ndef ints; gets.to_s.split.map { |s| s.to_i } end\r\ndef int1s; gets.to_s.split.map { |s| s.to_i - 1 } end\r\ndef float; gets.to_s.to_f end\r\ndef floats; gets.to_s.split.map { |s| s.to_f } end\r\ndef array_of(&convert); gets.to_s.split.map(&convert) end\r\ndef string; gets.to_s.chomp end\r\ndef strings; gets.to_s.split end\r\ndef rep(n, &b); Array.new(n, &b) end\r\ndef yes; puts $yesno[1] end\r\ndef no; puts $yesno[0] end\r\ndef yesno t; puts $yesno[t] end\r\ndef YesNo t; puts YesNo[t] end\r\ndef YESNO t; puts YESNO[t] end\r\ndef zip(xs, *yss); Enumerator.new { |y| xs.zip(*yss) { |a| y.yield(*a) } } end\r\ndef max(*xs, &block); block_given? ? xs.max_by(&block) : xs.max end\r\ndef min(*xs, &block); block_given? ? xs.min_by(&block) : xs.min end\r\ndef minmax(*xs, &block); block_given? ? xs.minmax_by(&block) : xs.minmax end\r\ndef gcd(*xs); xs.inject(0, :gcd) end\r\ndef matrix(h, w, fill=nil, &block); return Array.new(h) { Array.new(w, &block) } if block_given?; Array.new(h) { [fill] * w } end\r\ndef debug(x = nil); STDERR.puts (block_given? ? yield(x) : x).inspect if DEBUG; x end\r\ndef debug_grid(grid, width = 1); grid.each { |row| STDERR.puts row.map { |x| x.inspect.ljust(width) }.join(\"\") } if DEBUG; grid end\r\ndef if_debug; yield if DEBUG end\r\n\r\nmodule Boolean\r\n def coerce(other); [other, to_i] end\r\n def +@; to_i end\r\n def to_int; to_i end\r\n def *(other); to_i * other end\r\nend\r\n\r\nclass TrueClass\r\n include Boolean\r\n def to_i; 1 end\r\nend\r\n\r\nclass FalseClass\r\n include Boolean\r\n def to_i; 0 end\r\nend\r\n\r\nclass Integer\r\n def div_ceil(y); (self + y - 1) / y end\r\n def mod_inv(mod = MOD); pow(mod-2, mod) end\r\n def mod_div(y, mod = MOD); self * y.mod_inv(mod) % mod end\r\n def mod_nCr(r, mod = MOD); x = y = 1; (1..r).each { |i| x = x * (self + 1 - i) % mod; y = y * i % mod }; x.mod_div(y, mod); end\r\n def factorial(mod = MOD); (2..self).inject(1) { |f, x| f * x % mod } end\r\n def popcount; x = self; c = 0; while x > 0; c += 1 if x & 1 == 1; x >>= 1 end; c end #TODO: faster\r\n def bitbrute(&block); (1< 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\r\n def lower_bound; ac, wa = self.end_open, self.begin; while ac - wa > 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\r\n def shakutori(r2, &pred); Enumerator.new { |y| j, r = r2.begin, r2.end_open; each { |i| j += 1 while j + 1 < r and pred[i, j+1]; y.yield(i, j) } }; end\r\n def widest(&block); Enumerator.new { |y| j, n = self.begin, self.end_open; each { |i| j += 1 while j < n and block[i, j]; y.yield(i, j) if block[i, j] } } end\r\nend\r\n\r\nclass Array\r\n def power(&block); (0 ... 1 << size).each(&block) end\r\n def sorted_merge(other); a = []; i = j = 0; n, m = size, other.size; if j < m and other[j] < self[i]; a << other[j]; j += 1 else; a << self[i]; i += 1 end while i < n; a.push(*other[j..-1]) if j < m; a end\r\n def upper_bound; ac, wa = 0, size; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\r\n def lower_bound; ac, wa = size, 0; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\r\n def cum(*xs, &op); a = []; a << xs[0] if xs.size > 0; a << x = self[0]; (1...size).each { |i| a << x = op[x, self[i]] }; a end\r\n def cumdiff(range); self[range.end_open] - self[range.begin]; end\r\n def compress(kinds = uniq.sort!); map { |x| kinds.bsearch_index { _1 >= x } }; end\r\nend\r\n\r\nmodule Enumerable\r\n def sorted_uniq; x = nil; filter { |y| c = x === y; x = y; !c } end\r\n def cumsum; ys = [0]; each { |x| ys << x + ys[-1] }; ys end\r\nend\r\n\r\nmain[]"}], "negative_code": [], "src_uid": "ef2b90d5b1073430795704a7df748ac3"} {"nl": {"description": "Ilya the Lion wants to help all his friends with passing exams. They need to solve the following problem to pass the IT exam.You've got string s = s1s2... sn (n is the length of the string), consisting only of characters \".\" and \"#\" and m queries. Each query is described by a pair of integers li, ri (1 ≤ li < ri ≤ n). The answer to the query li, ri is the number of such integers i (li ≤ i < ri), that si = si + 1.Ilya the Lion wants to help his friends but is there anyone to help him? Help Ilya, solve the problem.", "input_spec": "The first line contains string s of length n (2 ≤ n ≤ 105). It is guaranteed that the given string only consists of characters \".\" and \"#\". The next line contains integer m (1 ≤ m ≤ 105) — the number of queries. Each of the next m lines contains the description of the corresponding query. The i-th line contains integers li, ri (1 ≤ li < ri ≤ n).", "output_spec": "Print m integers — the answers to the queries in the order in which they are given in the input.", "sample_inputs": ["......\n4\n3 4\n2 3\n1 6\n2 6", "#..###\n5\n1 3\n5 6\n1 5\n3 6\n3 4"], "sample_outputs": ["1\n1\n5\n4", "1\n1\n2\n2\n0"], "notes": null}, "positive_code": [{"source_code": "dp=[0]\npre=0\ngets.chomp.each_byte{|b|\n dp<1\n\tif s[0]==s[1] then\n\t\tans=ans+1\n\tend\n\tarr< 1\nend\n\nqueries.map! { |query| query.split.map(&:to_i) }.each do |query|\n i, j = query\n puts data[j] - data[i]\nend\n"}, {"source_code": "def main()\n st=gets.chomp\n ar=Array.new(st.length)\n for i in 0...(st.length)\n ar[i]=0\n end\n for i in 0...(st.length)\n if i>0\n ar[i]=ar[i-1]\n end\n if i+1<(st.length) && st[i].chr==st[i+1].chr\n ar[i]+=1\n end \n end\n tc=gets.chomp.to_i\n for i in 0...tc\n l,r=gets.chomp.split(\" \").map{|i| i.to_i}\n l-=1\n r-=1\n if l-1==-1\n puts ar[r-1]\n else\n puts ar[r-1]-ar[l-1]\n end\n end\nend\nmain()"}, {"source_code": "s=gets.chomp\nn=s.size\na=[]\n0.upto(n-1){|i|a[i]=(s[i]==s[i+1]?1:0)+(i>0?a[i-1]:0)}\ngets.to_i.times {\n l,r=gets.split.map(&:to_i)\n puts a[r-2]-(l==1?0:a[l-2])\n}"}, {"source_code": "dp=[0]\npre=0\ngets.chomp.each_byte{|b|\n dp< 1 && s[0...s.size-1].to_i > max\n max = s[0...s.size-1].to_i\nend\n if s.size > 2\n arr = s.split(//)\n arr.delete_at(-2) \n n = arr.join.to_i \n if n > max\n max = n\n end\nend\nputs max"}], "src_uid": "b30e09449309b999473e4be6643d68cd"} {"nl": {"description": "You are given a board of size $$$n \\times n$$$, where $$$n$$$ is odd (not divisible by $$$2$$$). Initially, each cell of the board contains one figure.In one move, you can select exactly one figure presented in some cell and move it to one of the cells sharing a side or a corner with the current cell, i.e. from the cell $$$(i, j)$$$ you can move the figure to cells: $$$(i - 1, j - 1)$$$; $$$(i - 1, j)$$$; $$$(i - 1, j + 1)$$$; $$$(i, j - 1)$$$; $$$(i, j + 1)$$$; $$$(i + 1, j - 1)$$$; $$$(i + 1, j)$$$; $$$(i + 1, j + 1)$$$; Of course, you can not move figures to cells out of the board. It is allowed that after a move there will be several figures in one cell.Your task is to find the minimum number of moves needed to get all the figures into one cell (i.e. $$$n^2-1$$$ cells should contain $$$0$$$ figures and one cell should contain $$$n^2$$$ figures).You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 200$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \\le n < 5 \\cdot 10^5$$$) — the size of the board. It is guaranteed that $$$n$$$ is odd (not divisible by $$$2$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5 \\cdot 10^5$$$ ($$$\\sum n \\le 5 \\cdot 10^5$$$).", "output_spec": "For each test case print the answer — the minimum number of moves needed to get all the figures into one cell.", "sample_inputs": ["3\n1\n5\n499993"], "sample_outputs": ["0\n40\n41664916690999888"], "notes": null}, "positive_code": [{"source_code": "gets.to_i.times { puts (1..gets.to_i / 2).sum { |i| i * i * 8 } }\n"}, {"source_code": "1.upto(gets.to_i) do\n n = gets.to_i\n puts (1..n.div(2)).inject(0) { |x, i| x + 8 * i**2 } \nend\n"}], "negative_code": [], "src_uid": "b4b69320c6040d2d264ac32e9ba5196f"} {"nl": {"description": "You are given an array $$$a$$$ consisting of $$$n$$$ integers. Each $$$a_i$$$ is one of the six following numbers: $$$4, 8, 15, 16, 23, 42$$$.Your task is to remove the minimum number of elements to make this array good.An array of length $$$k$$$ is called good if $$$k$$$ is divisible by $$$6$$$ and it is possible to split it into $$$\\frac{k}{6}$$$ subsequences $$$4, 8, 15, 16, 23, 42$$$.Examples of good arrays: $$$[4, 8, 15, 16, 23, 42]$$$ (the whole array is a required sequence); $$$[4, 8, 4, 15, 16, 8, 23, 15, 16, 42, 23, 42]$$$ (the first sequence is formed from first, second, fourth, fifth, seventh and tenth elements and the second one is formed from remaining elements); $$$[]$$$ (the empty array is good). Examples of bad arrays: $$$[4, 8, 15, 16, 42, 23]$$$ (the order of elements should be exactly $$$4, 8, 15, 16, 23, 42$$$); $$$[4, 8, 15, 16, 23, 42, 4]$$$ (the length of the array is not divisible by $$$6$$$); $$$[4, 8, 15, 16, 23, 42, 4, 8, 15, 16, 23, 23]$$$ (the first sequence can be formed from first six elements but the remaining array cannot form the required sequence). ", "input_spec": "The first line of the input contains one integer $$$n$$$ ($$$1 \\le n \\le 5 \\cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ (each $$$a_i$$$ is one of the following numbers: $$$4, 8, 15, 16, 23, 42$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$.", "output_spec": "Print one integer — the minimum number of elements you have to remove to obtain a good array.", "sample_inputs": ["5\n4 8 15 16 23", "12\n4 8 4 15 16 8 23 15 16 42 23 42", "15\n4 8 4 8 15 16 8 16 23 15 16 4 42 23 42"], "sample_outputs": ["5", "0", "3"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\ncnt = [0,0,0,0,0,0]\nvals = [4,8,15,16,23,42]\na.each do |v|\n cnt[0] += 1 if v == 4\n 1.upto(5) do |i|\n if vals[i] == v && cnt[i-1]>0\n cnt[i] += 1\n cnt[i-1] -= 1\n break\n end\n end\nend\np n-cnt[5]*6\n"}, {"source_code": "GOOD = [4,8,15,16,23,42]\n\ndef solve(a)\n pos = {}\n GOOD.each { |g| pos[g] = [] }\n\n a.each_with_index do |e, i|\n pos[e].push(i)\n end\n # puts pos.inspect\n res = 0\n while true\n positions = []\n GOOD.each_with_index do |g, i|\n p = pos[g].shift\n while i > 0 && !p.nil? && !positions[i - 1].nil? && p < positions[i - 1]\n p = pos[g].shift\n end\n positions << p\n end\n break if positions.include?(nil)\n res += 1\n end\n a.size - (res * 6)\nend\n\nn = gets.strip.to_i\na = gets.strip.split(' ').map(&:to_i)\nputs solve(a)"}], "negative_code": [{"source_code": "GOOD = [4,8,15,16,23,42]\ndef solve(a)\n h = {}\n GOOD.each { |g| h[g] = 0 }\n a.each do |e|\n if GOOD.include?(e)\n h[e] += 1\n end\n end\n mn = h.min_by { |k, v| v }\n return a.size if mn.nil?\n return a.size - (mn[1] * GOOD.size)\nend\n\nn = gets.strip.to_i\na = gets.strip.split(' ').map(&:to_i)\nputs solve(a)"}], "src_uid": "eb3155f0e6ba088308fa942f3572f582"} {"nl": {"description": "You are given a string $$$s$$$ such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of $$$s$$$ such that it contains each of these three characters at least once.A contiguous substring of string $$$s$$$ is a string that can be obtained from $$$s$$$ by removing some (possibly zero) characters from the beginning of $$$s$$$ and some (possibly zero) characters from the end of $$$s$$$.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 20000$$$) — the number of test cases. Each test case consists of one line containing the string $$$s$$$ ($$$1 \\le |s| \\le 200000$$$). It is guaranteed that each character of $$$s$$$ is either 1, 2, or 3. The sum of lengths of all strings in all test cases does not exceed $$$200000$$$.", "output_spec": "For each test case, print one integer — the length of the shortest contiguous substring of $$$s$$$ containing all three types of characters at least once. If there is no such substring, print $$$0$$$ instead.", "sample_inputs": ["7\n123\n12222133333332\n112233\n332211\n12121212\n333333\n31121"], "sample_outputs": ["3\n3\n4\n4\n0\n0\n4"], "notes": "NoteConsider the example test:In the first test case, the substring 123 can be used.In the second test case, the substring 213 can be used.In the third test case, the substring 1223 can be used.In the fourth test case, the substring 3221 can be used.In the fifth test case, there is no character 3 in $$$s$$$.In the sixth test case, there is no character 1 in $$$s$$$.In the seventh test case, the substring 3112 can be used."}, "positive_code": [{"source_code": "def ternary_string(str)\n h = {\"1\"=>false, \"2\"=>false, \"3\"=>false}\n i = 0\n min_size = 0\n while i < str.length\n h[str[i]] = i\n if h.values.all?\n if min_size == 0\n min_size = h.values.max - h.values.min + 1\n else\n min_size = [min_size, h.values.max - h.values.min + 1].min\n end\n end\n i += 1\n end\n min_size\nend\n\ntest_cases = gets.chomp.to_i\ni = 0\nwhile i < test_cases\n st = gets.chomp\n p ternary_string(st)\n i += 1\nend\n"}], "negative_code": [{"source_code": "def ternary_string(str)\n s = []\n i = 0\n h = {\"1\"=>false, \"2\"=>false, \"3\"=>false}\n min_size = str.length + 1\n while i < str.length\n s << str[i]\n h[str[i]] = true\n fin = s.size - 1\n if s[0] == s[fin]\n while fin > 0\n if s[fin] == s[0]\n s.shift\n end\n fin -= 1\n end\n end\n if h.values.all?\n min_size = [min_size, s.size].min\n end\n i += 1\n end\n min_size > str.length ? 0 : min_size\nend\n\ntest_cases = gets.chomp.to_i\ni = 0\nwhile i < test_cases\n st = gets.chomp\n p ternary_string(st)\n i += 1\nend\n"}, {"source_code": "def ternary_string(str)\n s = []\n i = 0\n h = {\"1\"=>false, \"2\"=>false, \"3\"=>false}\n min_size = str.length + 1\n while i < str.length\n s << str[i]\n h[str[i]] = true\n fin = s.size - 1\n if s[0] == s[fin]\n while fin > 0\n if s[fin] == s[0]\n s.shift\n end\n fin -= 1\n end\n end\n min_size = [min_size, s.size].min if h.values.all?\n# p s\n i += 1\n end\n min_size > str.length ? 0 : min_size\nend\n\ntest_cases = gets.chomp.to_i\ni = 0\nwhile i < test_cases\n st = gets.chomp\n puts ternary_string(st)\n i += 1\nend\n"}], "src_uid": "6cb06a02077750327602a1a7eeac770f"} {"nl": {"description": "One day mum asked Petya to sort his toys and get rid of some of them. Petya found a whole box of toy spiders. They were quite dear to him and the boy didn't want to throw them away. Petya conjured a cunning plan: he will glue all the spiders together and attach them to the ceiling. Besides, Petya knows that the lower the spiders will hang, the more mum is going to like it and then she won't throw his favourite toys away. Help Petya carry out the plan.A spider consists of k beads tied together by k - 1 threads. Each thread connects two different beads, at that any pair of beads that make up a spider is either directly connected by a thread, or is connected via some chain of threads and beads.Petya may glue spiders together directly gluing their beads. The length of each thread equals 1. The sizes of the beads can be neglected. That's why we can consider that gluing spiders happens by identifying some of the beads (see the picture). Besides, the construction resulting from the gluing process should also represent a spider, that is, it should have the given features. After Petya glues all spiders together, he measures the length of the resulting toy. The distance between a pair of beads is identified as the total length of the threads that connect these two beads. The length of the resulting construction is the largest distance between all pairs of beads. Petya wants to make the spider whose length is as much as possible. The picture two shows two spiders from the second sample. We can glue to the bead number 2 of the first spider the bead number 1 of the second spider. The threads in the spiders that form the sequence of threads of maximum lengths are highlighted on the picture.", "input_spec": "The first input file line contains one integer n (1 ≤ n ≤ 100) — the number of spiders. Next n lines contain the descriptions of each spider: integer ni (2 ≤ ni ≤ 100) — the number of beads, then ni - 1 pairs of numbers denoting the numbers of the beads connected by threads. The beads that make up each spider are numbered from 1 to ni.", "output_spec": "Print a single number — the length of the required construction.", "sample_inputs": ["1\n3 1 2 2 3", "2\n3 1 2 1 3\n4 1 2 2 3 2 4", "2\n5 1 2 2 3 3 4 3 5\n7 3 4 1 2 2 4 4 6 2 7 6 5"], "sample_outputs": ["2", "4", "7"], "notes": null}, "positive_code": [{"source_code": "def rec(d, node, e, depth)\n depth[node] = d\n e[node].each do |n|\n unless depth[n]\n rec(d+1, n, e, depth)\n end\n end\nend\n\ndef sub(node, e)\n depth = {}\n \n rec(0, node, e, depth)\n\n depth.max_by{|k,v| v}\nend\n\ndef longest_length(g)\n e = {}\n g.each do |u,v|\n (e[u] ||= []) << v\n (e[v] ||= []) << u\n end\n\n node, len = sub(1, e)\n node, len = sub(node, e)\n\n len\nend\n\n\nn, gs = nil\nFile.open(\"input.txt\") do |f|\n n = f.gets.to_i\n gs = []\n n.times do\n a = f.gets.split.map(&:to_i) \n gs << a[1..-1].each_slice(2).map\n end\nend\n\nlen = gs.inject(0){|r,g| r + longest_length(g)}\n\nFile.open(\"output.txt\", \"w\") do |f|\n f.puts len\nend\n"}], "negative_code": [], "src_uid": "63098c9d5ef90d445a6a4aa0f037a9c7"} {"nl": {"description": "\"We've tried solitary confinement, waterboarding and listening to Just In Beaver, to no avail. We need something extreme.\"\"Little Alena got an array as a birthday present...\"The array b of length n is obtained from the array a of length n and two integers l and r (l ≤ r) using the following procedure:b1 = b2 = b3 = b4 = 0.For all 5 ≤ i ≤ n: bi = 0 if ai, ai - 1, ai - 2, ai - 3, ai - 4 > r and bi - 1 = bi - 2 = bi - 3 = bi - 4 = 1 bi = 1 if ai, ai - 1, ai - 2, ai - 3, ai - 4 < l and bi - 1 = bi - 2 = bi - 3 = bi - 4 = 0 bi = bi - 1 otherwise You are given arrays a and b' of the same length. Find two integers l and r (l ≤ r), such that applying the algorithm described above will yield an array b equal to b'.It's guaranteed that the answer exists.", "input_spec": "The first line of input contains a single integer n (5 ≤ n ≤ 105) — the length of a and b'. The second line of input contains n space separated integers a1, ..., an ( - 109 ≤ ai ≤ 109) — the elements of a. The third line of input contains a string of n characters, consisting of 0 and 1 — the elements of b'. Note that they are not separated by spaces.", "output_spec": "Output two integers l and r ( - 109 ≤ l ≤ r ≤ 109), conforming to the requirements described above. If there are multiple solutions, output any of them. It's guaranteed that the answer exists.", "sample_inputs": ["5\n1 2 3 4 5\n00001", "10\n-10 -9 -8 -7 -6 6 7 8 9 10\n0000111110"], "sample_outputs": ["6 15", "-5 5"], "notes": "NoteIn the first test case any pair of l and r pair is valid, if 6 ≤ l ≤ r ≤ 109, in that case b5 = 1, because a1, ..., a5 < l."}, "positive_code": [{"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nb = gets.chomp.reverse.to_i(2)\nr_max = l_max = 10 ** 9\nr_min = l_min = - 10 ** 9\n\n4.upto(n-1) do |i|\n if b & 15 == 15\n if b & 16 == 0\n t = a[i-4,5].min - 1\n r_max = t if t < r_max\n else\n t = a[i-4,5].min\n r_min = t if t > r_min\n end\n elsif b & 15 == 0\n if b & 16 == 16\n t = a[i-4,5].max + 1\n l_min = t if t > l_min\n else\n t = a[i-4,5].max\n l_max = t if t < l_max\n end\n end\n b = b >> 1\n #p b.to_s(2)\nend\nprint \"#{l_min} #{r_max}\"\n#print \"\\nl is from #{l_min} to #{l_max}, r is from #{r_min} to #{r_max}\"\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nb = gets.chomp.reverse.to_i(2)\nr_max = l_max = 10 ** 9\nr_min = l_min = - 10 ** 9\n\n4.upto(n-1) do |i|\n if b & 15 == 15\n if b & 16 == 0\n t = a[i-4,5].min - 1\n r_max = t if t < r_max\n #else\n # t = a[i-4,5].min\n # r_min = t if t > r_min\n end\n elsif b & 15 == 0\n if b & 16 == 16\n t = a[i-4,5].max + 1\n l_min = t if t > l_min\n #else\n # t = a[i-4,5].max\n # l_max = t if t < l_max\n end\n end\n b = b >> 1\n #p b.to_s(2)\nend\nprint \"#{l_min} #{r_max}\"\n#print \"\\nl is from #{l_min} to #{l_max}, r is from #{r_min} to #{r_max}\"\n"}], "negative_code": [], "src_uid": "3d8c881d96c998dce7059384cd882273"} {"nl": {"description": "Polycarp has just attempted to pass the driving test. He ran over the straight road with the signs of four types. speed limit: this sign comes with a positive integer number — maximal speed of the car after the sign (cancel the action of the previous sign of this type); overtake is allowed: this sign means that after some car meets it, it can overtake any other car; no speed limit: this sign cancels speed limit if any (car can move with arbitrary speed after this sign); no overtake allowed: some car can't overtake any other car after this sign. Polycarp goes past the signs consequentially, each new sign cancels the action of all the previous signs of it's kind (speed limit/overtake). It is possible that two or more \"no overtake allowed\" signs go one after another with zero \"overtake is allowed\" signs between them. It works with \"no speed limit\" and \"overtake is allowed\" signs as well.In the beginning of the ride overtake is allowed and there is no speed limit.You are given the sequence of events in chronological order — events which happened to Polycarp during the ride. There are events of following types: Polycarp changes the speed of his car to specified (this event comes with a positive integer number); Polycarp's car overtakes the other car; Polycarp's car goes past the \"speed limit\" sign (this sign comes with a positive integer); Polycarp's car goes past the \"overtake is allowed\" sign; Polycarp's car goes past the \"no speed limit\"; Polycarp's car goes past the \"no overtake allowed\"; It is guaranteed that the first event in chronological order is the event of type 1 (Polycarp changed the speed of his car to specified).After the exam Polycarp can justify his rule violations by telling the driving instructor that he just didn't notice some of the signs. What is the minimal number of signs Polycarp should say he didn't notice, so that he would make no rule violations from his point of view?", "input_spec": "The first line contains one integer number n (1 ≤ n ≤ 2·105) — number of events. Each of the next n lines starts with integer t (1 ≤ t ≤ 6) — the type of the event. An integer s (1 ≤ s ≤ 300) follows in the query of the first and the third type (if it is the query of first type, then it's new speed of Polycarp's car, if it is the query of third type, then it's new speed limit). It is guaranteed that the first event in chronological order is the event of type 1 (Polycarp changed the speed of his car to specified).", "output_spec": "Print the minimal number of road signs Polycarp should say he didn't notice, so that he would make no rule violations from his point of view.", "sample_inputs": ["11\n1 100\n3 70\n4\n2\n3 120\n5\n3 120\n6\n1 150\n4\n3 300", "5\n1 100\n3 200\n2\n4\n5", "7\n1 20\n2\n6\n4\n6\n6\n2"], "sample_outputs": ["2", "0", "2"], "notes": "NoteIn the first example Polycarp should say he didn't notice the \"speed limit\" sign with the limit of 70 and the second \"speed limit\" sign with the limit of 120.In the second example Polycarp didn't make any rule violation.In the third example Polycarp should say he didn't notice both \"no overtake allowed\" that came after \"overtake is allowed\" sign."}, "positive_code": [{"source_code": "n = gets.to_i\nevents = []\nn.times do\n events.push(gets.split.map(&:to_i))\nend\n\nmissed_indexes = []\n\nspeed = events[0][1]\nevents.each_with_index do | event, index |\n #p [event, index]\n case event[0]\n when 1\n speed = event[1]\n when 3\n if event[1] < speed\n missed_indexes.push(index)\n end\n end\nend\n#puts \"missed slow downs\"\n#p missed_indexes\n\nmax_speed = 0\nrace = false\nevents.each_with_index.to_a.reverse.each do |event, index|\n #p [event, index]\n case event[0]\n # Поликарп изменил скорость машины на заданную\n when 1\n max_speed = [max_speed, event[1]].max\n\n # машина Поликарпа обогнала другую машину;\n when 2\n race = true\n\n # машина Поликарпа проехала знак «ограничение скорости»\n when 3\n if max_speed > event[1]\n missed_indexes.push(index)\n else\n max_speed = 0\n end\n\n # машина Поликарпа проехала знак «обгон разрешен»;\n when 4\n race = false\n\n # машина Поликарпа проехала знак «отмена ограничения скорости»;\n when 5\n max_speed = 0\n\n # машина Поликарпа проехала знак «обгон запрещен».\n when 6\n if race\n missed_indexes.push(index)\n end\n end\nend\n#puts \"missed everything\"\n#p missed_indexes\n\nputs missed_indexes.uniq.count\n"}], "negative_code": [], "src_uid": "2dca11b202d7adbe22a404a52d627eed"} {"nl": {"description": "Given an array $$$a$$$ of length $$$n$$$ and an integer $$$k$$$, find the number of indices $$$1 \\leq i \\leq n - k$$$ such that the subarray $$$[a_i, \\dots, a_{i+k}]$$$ with length $$$k+1$$$ (not with length $$$k$$$) has the following property: If you multiply the first element by $$$2^0$$$, the second element by $$$2^1$$$, ..., and the ($$$k+1$$$)-st element by $$$2^k$$$, then this subarray is sorted in strictly increasing order. More formally, count the number of indices $$$1 \\leq i \\leq n - k$$$ such that $$$$$$2^0 \\cdot a_i < 2^1 \\cdot a_{i+1} < 2^2 \\cdot a_{i+2} < \\dots < 2^k \\cdot a_{i+k}.$$$$$$ ", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\leq t \\leq 1000$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$3 \\leq n \\leq 2 \\cdot 10^5$$$, $$$1 \\leq k < n$$$) — the length of the array and the number of inequalities. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\leq a_i \\leq 10^9$$$) — the elements of the array. The sum of $$$n$$$ across all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, output a single integer — the number of indices satisfying the condition in the statement.", "sample_inputs": ["6\n\n4 2\n\n20 22 19 84\n\n5 1\n\n9 5 3 2 1\n\n5 2\n\n9 5 3 2 1\n\n7 2\n\n22 12 16 4 3 22 12\n\n7 3\n\n22 12 16 4 3 22 12\n\n9 3\n\n3 9 12 3 9 12 3 9 12"], "sample_outputs": ["2\n3\n2\n3\n1\n0"], "notes": "NoteIn the first test case, both subarrays satisfy the condition: $$$i=1$$$: the subarray $$$[a_1,a_2,a_3] = [20,22,19]$$$, and $$$1 \\cdot 20 < 2 \\cdot 22 < 4 \\cdot 19$$$. $$$i=2$$$: the subarray $$$[a_2,a_3,a_4] = [22,19,84]$$$, and $$$1 \\cdot 22 < 2 \\cdot 19 < 4 \\cdot 84$$$. In the second test case, three subarrays satisfy the condition: $$$i=1$$$: the subarray $$$[a_1,a_2] = [9,5]$$$, and $$$1 \\cdot 9 < 2 \\cdot 5$$$. $$$i=2$$$: the subarray $$$[a_2,a_3] = [5,3]$$$, and $$$1 \\cdot 5 < 2 \\cdot 3$$$. $$$i=3$$$: the subarray $$$[a_3,a_4] = [3,2]$$$, and $$$1 \\cdot 3 < 2 \\cdot 2$$$. $$$i=4$$$: the subarray $$$[a_4,a_5] = [2,1]$$$, but $$$1 \\cdot 2 = 2 \\cdot 1$$$, so this subarray doesn't satisfy the condition. "}, "positive_code": [{"source_code": "def solve(a, k)\n b = []\n\n (0...a.size - 1).each do |i|\n if a[i] < a[i + 1] * 2\n b.push(1)\n else\n b.push(1)\n b.push(0)\n end\n end\n b.push(1)\n # p b\n\n (0...b.size).each do |i|\n b[i] = (i > 0 ? b[i] + b[i - 1] : b[0])\n end\n\n result = 0\n\n k += 1\n (b.size - k + 1).times do |l|\n v = (l > 0 ? b[l + k - 1] - b[l - 1] : b[l + k - 1])\n # p [b[l, k], v, l, l + k]\n if v == k\n result += 1\n end\n end\n\n result\nend\n\nn = gets.to_i\n\nn.times do\n size, s = gets.split.map(&:to_i)\n array = Array.new(size)\n array = gets.split.map(&:to_i)\n\n p solve(array, s)\nend\n"}, {"source_code": "def solve\r\n q = gets.split.map(&:to_i)\r\n a = gets.split.map(&:to_i)\r\n n = q[0]\r\n k = q[1]\r\n pref = Array.new(n)\r\n pref[0] = 0\r\n i = 1\r\n while i < n\r\n pref[i] = pref[i - 1]\r\n if 2 * a[i] > a[i - 1]\r\n pref[i] = pref[i] + 1\r\n end\r\n i = i + 1\r\n end\r\n ans = 0\r\n i = 0\r\n while i + k < n\r\n curr = pref[i + k] - pref[i]\r\n if curr == k\r\n ans = ans + 1\r\n end\r\n i = i + 1\r\n end\r\n puts ans \r\nend\r\n\r\ngets.to_i.times do\r\n solve\r\nend"}, {"source_code": "gets.to_i.times do\r\n n, k = gets.split.map(&:to_i)\r\n a = gets.split.map(&:to_i)\r\n\r\n l = 0\r\n r = 1\r\n cnt = 0\r\n ans = 0\r\n flag = false\r\n while l < n-k\r\n while r < n && (r <= l || cnt < k)\r\n if a[r-1] >= 2*a[r]\r\n l = r\r\n cnt = 0\r\n flag = true\r\n r += 1\r\n break\r\n else\r\n cnt += 1\r\n r += 1\r\n end\r\n end\r\n next flag = false if flag\r\n ans += 1 if cnt == k\r\n cnt -= 1\r\n l += 1\r\n end\r\n\r\n puts ans\r\nend\r\n\r\n"}, {"source_code": "gets.to_i.times do\r\n n, k = gets.split.map(&:to_i)\r\n xs = gets.split.map(&:to_i)\r\n counts = [0]\r\n xs.each_cons(2) do |a, b|\r\n if a < 2 * b\r\n counts[-1] += 1\r\n else\r\n counts << 0\r\n end\r\n end\r\n p counts.filter_map { |c| c - k + 1 if c >= k }.sum\r\nend\r\n"}, {"source_code": "main = -> {\r\n t = int\r\n t.times do\r\n n, k = ints\r\n a = ints\r\n prefix_sum = [0]\r\n (n - 1).times do |i|\r\n x = (a[i] < a[i + 1] * 2) ? 1 : 0\r\n prefix_sum << (prefix_sum[-1] + x)\r\n end\r\n \r\n puts (0 ... n - k).count { |i| prefix_sum[i + k] - prefix_sum[i] == k }\r\n end\r\n}\r\n\r\nDEBUG = false\r\nMOD = 10**9+7\r\nINF = 10**18\r\n$yesno = %w(No Yes)\r\n$YesNo = %w(No Yes)\r\n$YESNO = %w(NO YES)\r\n\r\nrequire \"set\"\r\nrequire \"prime\"\r\n\r\ndef int; gets.to_s.to_i end\r\ndef ints; gets.to_s.split.map { |s| s.to_i } end\r\ndef int1s; gets.to_s.split.map { |s| s.to_i - 1 } end\r\ndef float; gets.to_s.to_f end\r\ndef floats; gets.to_s.split.map { |s| s.to_f } end\r\ndef array_of(&convert); gets.to_s.split.map(&convert) end\r\ndef string; gets.to_s.chomp end\r\ndef strings; gets.to_s.split end\r\ndef rep(n, &b); Array.new(n, &b) end\r\ndef yes; puts $yesno[1] end\r\ndef no; puts $yesno[0] end\r\ndef yesno t; puts $yesno[t ? 1 : 0] end\r\ndef YesNo t; puts $YesNo[t ? 1 : 0] end\r\ndef YESNO t; puts $YESNO[t ? 1 : 0] end\r\ndef zip(xs, *yss); Enumerator.new { |y| xs.zip(*yss) { |a| y.yield(*a) } } end\r\ndef max(*xs, &block); block_given? ? xs.max_by(&block) : xs.max end\r\ndef min(*xs, &block); block_given? ? xs.min_by(&block) : xs.min end\r\ndef minmax(*xs, &block); block_given? ? xs.minmax_by(&block) : xs.minmax end\r\ndef gcd(*xs); xs.inject(0, :gcd) end\r\ndef matrix(h, w, fill=nil, &block); return Array.new(h) { Array.new(w, &block) } if block_given?; Array.new(h) { [fill] * w } end\r\ndef debug(x = nil); STDERR.puts (block_given? ? yield(x) : x).inspect if DEBUG; x end\r\ndef debug_grid(grid, width = 1); grid.each { |row| STDERR.puts row.map { |x| x.inspect.ljust(width) }.join(\"\") } if DEBUG; grid end\r\ndef if_debug; yield if DEBUG end\r\n\r\nmodule Boolean\r\n def coerce(other); [other, to_i] end\r\n def +@; to_i end\r\n def to_int; to_i end\r\n def *(other); to_i * other end\r\nend\r\n\r\nclass TrueClass\r\n include Boolean\r\n def to_i; 1 end\r\nend\r\n\r\nclass FalseClass\r\n include Boolean\r\n def to_i; 0 end\r\nend\r\n\r\nclass Integer\r\n def div_ceil(y); (self + y - 1) / y end\r\n def mod_inv(mod = MOD); pow(mod-2, mod) end\r\n def mod_div(y, mod = MOD); self * y.mod_inv(mod) % mod end\r\n def mod_nCr(r, mod = MOD); x = y = 1; (1..r).each { |i| x = x * (self + 1 - i) % mod; y = y * i % mod }; x.mod_div(y, mod); end\r\n def factorial(mod = MOD); (2..self).inject(1) { |f, x| f * x % mod } end\r\n def popcount; x = self; c = 0; while x > 0; c += 1 if x & 1 == 1; x >>= 1 end; c end #TODO: faster\r\n def bitbrute(&block); (1< 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\r\n def lower_bound; ac, wa = self.end_open, self.begin; while ac - wa > 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\r\n def shakutori(r2, &pred); Enumerator.new { |y| j, r = r2.begin, r2.end_open; each { |i| j += 1 while j + 1 < r and pred[i, j+1]; y.yield(i, j) } }; end\r\n def widest(&block); Enumerator.new { |y| j, n = self.begin, self.end_open; each { |i| j += 1 while j < n and block[i, j]; y.yield(i, j) if block[i, j] } } end\r\nend\r\n\r\nclass Array\r\n def power(&block); (0 ... 1 << size).each(&block) end\r\n def sorted_merge(other); a = []; i = j = 0; n, m = size, other.size; if j < m and other[j] < self[i]; a << other[j]; j += 1 else; a << self[i]; i += 1 end while i < n; a.push(*other[j..-1]) if j < m; a end\r\n def upper_bound; ac, wa = 0, size; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\r\n def lower_bound; ac, wa = size, 0; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\r\n def cum(*xs, &op); a = []; a << xs[0] if xs.size > 0; a << x = self[0]; (1...size).each { |i| a << x = op[x, self[i]] }; a end\r\n def cumdiff(range); self[range.end_open] - self[range.begin]; end\r\n def compress(kinds = uniq.sort!); map { |x| kinds.bsearch_index { _1 >= x } }; end\r\nend\r\n\r\nmodule Enumerable\r\n def sorted_uniq; x = nil; filter { |y| c = x === y; x = y; !c } end\r\n def cumsum; ys = [0]; each { |x| ys << x + ys[-1] }; ys end\r\nend\r\n\r\nmain[]"}], "negative_code": [], "src_uid": "b5ef56f2eb482682414c32fba83a3949"} {"nl": {"description": "Little penguin Polo likes permutations. But most of all he likes permutations of integers from 0 to n, inclusive.For permutation p = p0, p1, ..., pn, Polo has defined its beauty — number .Expression means applying the operation of bitwise excluding \"OR\" to numbers x and y. This operation exists in all modern programming languages, for example, in language C++ and Java it is represented as \"^\" and in Pascal — as \"xor\".Help him find among all permutations of integers from 0 to n the permutation with the maximum beauty.", "input_spec": "The single line contains a positive integer n (1 ≤ n ≤ 106).", "output_spec": "In the first line print integer m the maximum possible beauty. In the second line print any permutation of integers from 0 to n with the beauty equal to m. If there are several suitable permutations, you are allowed to print any of them.", "sample_inputs": ["4"], "sample_outputs": ["20\n0 2 1 4 3"], "notes": null}, "positive_code": [{"source_code": "h,n=[],gets.to_i+1\np n*(n-1)\nnow=2**(Math.log2(n-1).floor+1)-1\n(0..n-1).reverse_each do |i|\n if i==now/2\n now/=2\n end\nif(h[i])\n next\nend\nh[i]=now-i\nh[now-i]=i\nend\nputs h.join ' '"}, {"source_code": "h,n=[],gets.to_i+1\np n*(n-1)\nnow=2**(Math.log2(n-1).floor+1)-1\n(0..n-1).reverse_each do |i|\n if i==now/2\n now/=2\n end\nif(h[i])\n next\nend\nh[i]=now^i\nh[now^i]=i\nend\nputs h.join ' '"}, {"source_code": "def f n\n cur = 1\n while cur <= n\n cur = cur * 2\n end\n cur /= 2\n i = cur\n cur -= 1\n while i <= n && cur >= 0\n $a[cur] = i\n $a[i] = cur\n $ans += 2 * (cur ^ i)\n i += 1\n cur -= 1\n end\n if cur >= 1 \n f cur \n end\nend\n\nn = gets.to_i\n$ans = 0\n$a = Array.new(n + 1, 0)\nf n\np $ans\nputs $a.join(' ')\n"}], "negative_code": [], "src_uid": "ab410ffc2bfe9360abf278328b9c3055"} {"nl": {"description": "Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a1, a2, ..., an, where ai is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others. We define as bj the number of songs the group j is going to perform tomorrow. Polycarp wants to change the playlist in such a way that the minimum among the numbers b1, b2, ..., bm will be as large as possible.Find this maximum possible value of the minimum among the bj (1 ≤ j ≤ m), and the minimum number of changes in the playlist Polycarp needs to make to achieve it. One change in the playlist is a replacement of the performer of the i-th song with any other group.", "input_spec": "The first line of the input contains two integers n and m (1 ≤ m ≤ n ≤ 2000). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is the performer of the i-th song.", "output_spec": "In the first line print two integers: the maximum possible value of the minimum among the bj (1 ≤ j ≤ m), where bj is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make. In the second line print the changed playlist. If there are multiple answers, print any of them.", "sample_inputs": ["4 2\n1 2 3 2", "7 3\n1 3 2 2 2 2 1", "4 4\n1000000000 100 7 1000000000"], "sample_outputs": ["2 1\n1 2 1 2", "2 1\n1 3 3 2 2 2 1", "1 4\n1 2 3 4"], "notes": "NoteIn the first sample, after Polycarp's changes the first band performs two songs (b1 = 2), and the second band also performs two songs (b2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist. In the second sample, after Polycarp's changes the first band performs two songs (b1 = 2), the second band performs three songs (b2 = 3), and the third band also performs two songs (b3 = 2). Thus, the best minimum value is 2. "}, "positive_code": [{"source_code": "n, m = gets.split.map(&:to_i)\narray = gets.split.map(&:to_i)\ncount = Array.new(m + 1, 0)\nlabel = []\narray.each_with_index do |x, i|\n if x <= m and count[x] < n / m\n count[x] += 1\n else\n label[i] = true\n end\nend\nchanges = 0\n(1..m).each do |i|\n while(count[i] < n / m) do\n j = label.index(true)\n array[j] = i\n label[j] = false\n count[i] += 1\n changes += 1\n end\nend\nputs \"#{n / m} #{changes}\"\narray.each { |x| print \"#{x} \" }\nputs\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\ngoal = n / m\ncnt = Array.new(m + 1, 0)\ncgable = []\na.each_with_index do |x, i|\n if x <= m && cnt[x] < goal\n cnt[x] += 1\n else\n cgable << i\n end\nend\nans = 0\n(1..m).each do |i|\n while cnt[i] < goal\n a[cgable.shift] = i\n cnt[i] += 1\n ans += 1\n end\nend\nputs \"#{goal} #{ans}\"\nputs a.join(' ')\n"}], "negative_code": [{"source_code": "n, m = gets.split.map(&:to_i)\narray = gets.split.map(&:to_i)\ncount, replace = Array.new(m + 1, 0), []\narray.each_with_index do |x, i|\n if x <= m and count[x] < n / m\n count[x] += 1\n else\n replace[i] = true\n end\nend\nchanges = 0\n(1..m).each do |i|\n if(count[i] < n / m)\n array[j = replace.index(true)] = i\n replace[j] = false\n count[i] += 1\n changes += 1\n end\nend\n\nputs \"#{n / m} #{changes}\"\narray.each { |x| print \"#{x} \" }\n"}], "src_uid": "0d89602f5ed765adf6807f86df1205bd"} {"nl": {"description": "Given an array $$$a$$$ of $$$n$$$ elements, print any value that appears at least three times or print -1 if there is no such value.", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$) — the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$1 \\leq n \\leq 2\\cdot10^5$$$) — the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\leq a_i \\leq n$$$) — the elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\\cdot10^5$$$.", "output_spec": "For each test case, print any value that appears at least three times or print -1 if there is no such value.", "sample_inputs": ["7\n\n1\n\n1\n\n3\n\n2 2 2\n\n7\n\n2 2 3 3 4 2 2\n\n8\n\n1 4 3 4 3 2 4 1\n\n9\n\n1 1 1 2 2 2 3 3 3\n\n5\n\n1 5 2 4 3\n\n4\n\n4 4 4 4"], "sample_outputs": ["-1\n2\n2\n4\n3\n-1\n4"], "notes": "NoteIn the first test case there is just a single element, so it can't occur at least three times and the answer is -1.In the second test case, all three elements of the array are equal to $$$2$$$, so $$$2$$$ occurs three times, and so the answer is $$$2$$$.For the third test case, $$$2$$$ occurs four times, so the answer is $$$2$$$.For the fourth test case, $$$4$$$ occurs three times, so the answer is $$$4$$$.For the fifth test case, $$$1$$$, $$$2$$$ and $$$3$$$ all occur at least three times, so they are all valid outputs.For the sixth test case, all elements are distinct, so none of them occurs at least three times and the answer is -1."}, "positive_code": [{"source_code": "t = gets.chomp().to_i\r\n\r\nwhile t > 0\r\n n = gets.chomp().to_i\r\n a = Array.new(n + 1, 0)\r\n ind = gets.chomp.split(' ').map!{ |num| num.to_i}\r\n \r\n ans = -1\r\n \r\n ind.each do |i|\r\n a[i] += 1\r\n if a[i] == 3\r\n ans = i\r\n end\r\n end\r\n \r\n puts ans\r\n t -= 1\r\n\r\nend\r\n\r\n"}, {"source_code": "t = gets.chomp().to_i\r\n\r\nwhile t > 0\r\n n = gets.chomp().to_i\r\n a = Array.new(n + 1, 0)\r\n ind = gets.chomp.split(' ').map!{ |num| num.to_i}\r\n \r\n ans = -1\r\n \r\n ind.each do |i|\r\n a[i] += 1\r\n if a[i] == 3\r\n ans = i\r\n end\r\n end\r\n \r\n puts ans\r\n a = nil\r\n t -= 1\r\nend\r\n\r\n"}, {"source_code": "t = gets.chomp().to_i\r\n\r\nwhile t > 0\r\n n = gets.chomp().to_i\r\n a = Array.new(n + 1, 0)\r\n ind = gets.chomp.split(' ').map!{ |num| num.to_i}\r\n \r\n ans = -1\r\n \r\n ind.each do |i|\r\n a[i] += 1\r\n if a[i] == 3\r\n ans = i\r\n end\r\n end\r\n \r\n puts ans\r\n t -= 1\r\nend\r\n\r\n"}, {"source_code": "def triple(element)\n len = element[0]\n arr = element[1]\n map = {}\n arr.each do |i|\n map.key?(i) ? map[i] += 1 : map[i] = 1\n end\n map.each do |k, v|\n if v >= 3\n return k\n end\n end\n return -1\nend\n\nt = gets.chomp.to_i\n\ninputs = []\n(0..t-1).each do |i|\n len = gets.chomp.to_i\n input = gets.chomp\n input = input.split(\" \").map(&:to_i)\n inputs.push([len, input])\nend\n\ninputs.each do |element|\n p triple(element)\nend"}, {"source_code": "gets.to_i.times do\r\n gets\r\n xs = gets.split.map(&:to_i)\r\n x = xs.tally.find { |x, count| count >= 3 }\r\n p x ? x[0] : -1\r\nend\r\n"}, {"source_code": "gets.to_i.times do\r\n gets\r\n a = gets.split.map(&:to_i).tally.sort_by { |_k, v| -v }\r\n if a[0][1] < 3\r\n puts(-1)\r\n else\r\n puts a[0][0]\r\n end\r\nend\r\n"}], "negative_code": [], "src_uid": "7d4174e3ae76de7b1389f618eb89d334"} {"nl": {"description": "You've got an array a, consisting of n integers. The array elements are indexed from 1 to n. Let's determine a two step operation like that: First we build by the array a an array s of partial sums, consisting of n elements. Element number i (1 ≤ i ≤ n) of array s equals . The operation x mod y means that we take the remainder of the division of number x by number y. Then we write the contents of the array s to the array a. Element number i (1 ≤ i ≤ n) of the array s becomes the i-th element of the array a (ai = si). You task is to find array a after exactly k described operations are applied.", "input_spec": "The first line contains two space-separated integers n and k (1 ≤ n ≤ 2000, 0 ≤ k ≤ 109). The next line contains n space-separated integers a1, a2, ..., an — elements of the array a (0 ≤ ai ≤ 109).", "output_spec": "Print n integers  — elements of the array a after the operations are applied to it. Print the elements in the order of increasing of their indexes in the array a. Separate the printed numbers by spaces.", "sample_inputs": ["3 1\n1 2 3", "5 0\n3 14 15 92 6"], "sample_outputs": ["1 3 6", "3 14 15 92 6"], "notes": null}, "positive_code": [{"source_code": "Mod = 1000000007\nPow = \"111011100110101100101000000101\"\nLen = 30\n\nn, k = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\n\ndef inverse(n)\n c = 1\n for i in 0...Len\n c = (c*c)%Mod\n if Pow[i] == '1'\n c = (c*n)%Mod\n end\n end\n return c\nend\n\ncom = Array.new\ncom[0] = 1\ns = Array.new\ns[0] = a[0]\nfor i in 1...n\n com[i] = (com[i-1]*(k+i-1)*inverse(i))%Mod\n s[i] = 0\n for j in 0..i\n s[i] = (s[i]+a[i-j]*com[j])%Mod\n end\nend\n\ns.each {|x| print x, \" \"}"}, {"source_code": "buf = gets.split(\" \")\nn = buf[0].to_i\nk = buf[1].to_i\na = [0]\nmod = 1000000007\nfor i in gets.split(\" \")\n a << i.to_i\nend\nif k == 0 then\n 1.upto(n) { |i|\n print a[i], \" \"\n }\nelse\n k -= 1\n now = 1\n sum = 0\n list = []\n 1.upto(n) { |i|\n list << now % mod\n now *= i + k\n now /= i\n }\n 1.upto(n) { |i|\n sum = 0\n i.downto(1) { |j|\n sum = sum + list[i - j] * a[j];\n sum %= mod\n }\n print sum, \" \"\n }\nend\n"}, {"source_code": "def modinv(num)\n a = num\n x = 0; nx = 1; b = 1000000007\n while(a>0) do\n q = b/a\n x -= q*nx\n x,nx = nx, x\n b -= q*a\n a,b = b,a\n end\n return x\nend\n\nn,k = gets.chomp.split.map(&:to_i)\narray = gets.chomp.split.map(&:to_i)\nmul = [1]\na = k\nb = 1\n1.upto(array.size - 1) do |i|\n mul[i] = (mul[i-1] * a * modinv(b)) % 1000000007\n a = a + 1\n b = b + 1\nend\n0.upto(array.size-1) do |i|\n ans = 0\n 0.upto(i) do |j|\n ans += mul[i-j]*array[j]\n end\n print \"#{ans%1000000007} \"\nend\nputs"}, {"source_code": "buf = gets.split(\" \")\nn = buf[0].to_i\nk = buf[1].to_i\na = [0]\nmod = 1000000007\nfor i in gets.split(\" \")\n a << i.to_i\nend\nif k == 0 then\n 1.upto(n) { |i|\n print a[i], \" \"\n }\nelse\n k -= 1\n now = 1\n sum = 0\n list = []\n 1.upto(n) { |i|\n list << now % mod\n now *= i + k\n now /= i\n }\n 1.upto(n) { |i|\n sum = 0\n i.downto(1) { |j|\n sum = sum + list[i - j] * a[j];\n sum %= mod\n }\n print sum, \" \"\n }\nend\n"}], "negative_code": [{"source_code": "Mod = 1000000007\nPow = \"111011100110101100101000000101\"\nLen = 30\n\nn, k = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\n\ndef inverse(n)\n c = 1\n for i in 0...Len\n c = (c*c)%Mod\n if Pow[i] == '1'\n c = (c*n)%Mod\n end\n end\n return c\nend\n\ncom = Array.new\ncom[0] = 1\ns = Array.new\ns[0] = a[0]\nfor i in 1...n\n com[i] = (com[i-1]*(k+i-1)*inverse(i))%Mod\n s[i] = 0\n for j in 0..i\n s[i] += a[i-j]*com[j]\n end\nend\n\ns.each {|x| print x, \" \"}"}, {"source_code": "buf = gets.split(\" \")\nn = buf[0].to_i\nk = buf[1].to_i\na = [0]\nmod = 1000000007\nfor i in gets.split(\" \")\n a << i.to_i\nend\n#p a\nif k == 0 then\n 1.upto(n) { |i|\n print a[i], \" \"\n }\nelse\n k -= 1\n now = 1\n sum = 0\n list = []\n 1.upto(n) { |i|\n list << now % mod\n now /= i\n now *= i + k\n }\n 1.upto(n) { |i|\n sum = 0\n i.downto(1) { |j|\n sum = sum + list[i - j] * a[j];\n sum %= mod\n }\n print sum, \" \"\n }\nend\n"}, {"source_code": "def modinv(num)\n a = num\n x = 0; nx = 1; b = 1000000007\n while(a>0) do\n q = b/a\n x -= q*nx\n x,nx = nx, x\n b -= q*a\n a,b = b,a\n end\n return x\nend\n\nn,k = gets.chomp.split.map(&:to_i)\narray = gets.chomp.split.map(&:to_i)\nmul = [1]\na = k\nb = 1\n1.upto(array.size - 1) do |i|\n mul[i] = (mul[i-1] * a * modinv(b)) % 1000000007\n a = a + 1\n b = b + 1\nend\n0.upto(array.size-1) do |i|\n ans = 0\n 0.upto(i) do |j|\n ans += mul[i-j]*array[j]\n end\n print \"#{ans} \"\nend\nputs"}], "src_uid": "584f7008e27dde53037396d2459efd84"} {"nl": {"description": "Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.Petya loves long lucky numbers very much. He is interested in the minimum lucky number d that meets some condition. Let cnt(x) be the number of occurrences of number x in number d as a substring. For example, if d = 747747, then cnt(4) = 2, cnt(7) = 4, cnt(47) = 2, cnt(74) = 2. Petya wants the following condition to fulfil simultaneously: cnt(4) = a1, cnt(7) = a2, cnt(47) = a3, cnt(74) = a4. Petya is not interested in the occurrences of other numbers. Help him cope with this task.", "input_spec": "The single line contains four integers a1, a2, a3 and a4 (1 ≤ a1, a2, a3, a4 ≤ 106).", "output_spec": "On the single line print without leading zeroes the answer to the problem — the minimum lucky number d such, that cnt(4) = a1, cnt(7) = a2, cnt(47) = a3, cnt(74) = a4. If such number does not exist, print the single number \"-1\" (without the quotes).", "sample_inputs": ["2 2 1 1", "4 7 3 1"], "sample_outputs": ["4774", "-1"], "notes": null}, "positive_code": [{"source_code": "a1,a2,a3,a4=gets.split.map(&:to_i)\nif a3==a4+1 then\n\t# 47が多い\n\t# 47を作っていく\n\ta1-=a3\n\ta2-=a3\n\tif a1<0 || a2<0 then #生成不能\n\t\tp -1\n\t\texit\n\tend\n\tprint \"4\"*a1\n\tprint \"47\"*a3\n\tprint \"7\"*a2\nelsif a4==a3+1\n\t# 47が多い\n\t# 47を作っていく\n\ta1-=a4\n\ta2-=a4\n\tif a1<0 || a2<0 then #生成不能\n\t\tp -1\n\t\texit\n\tend\n\tprint 7\n\tprint \"4\"*a1\n\tprint \"47\"*a3\n\tprint \"7\"*a2\n\tprint 4\nelsif a3==a4\n\t#47優先解法\n\ta1-=a3+1\n\ta2-=a3\n\tif a1<0||a2<0 then\n\t\ta1+=a3+1\n\t\ta2+=a3\n\n\t\ta1-=a3\n\t\ta2-=a3+1\n\t\tif a1<0||a2<0 then\n\t\t\tp -1\n\t\t\texit\n\t\tend\n\t\t#74優先解法\n\t\tprint 7\n\t\tprint \"4\"*(a1+1)\n\t\tprint \"74\"*(a3-1)\n\t\tprint \"7\"*(a2+1)\n\t\texit\n\tend\n\tprint \"4\"*a1\n\tprint 4\n\tprint \"74\"*(a3-1)\n\tprint \"7\"*(a2+1)\n\tprint 4\nelse\n\tputs \"-1\"\nend\n\n"}], "negative_code": [{"source_code": "a1,a2,a3,a4=gets.split.map(&:to_i)\nif a3==a4+1 then\n\t# 47が多い\n\t# 47を作っていく\n\ta1-=a3\n\ta2-=a3\n\tif a1<0 || a2<0 then #生成不能\n\t\tp -1\n\t\texit\n\tend\n\ta1.times{print 4}\n\ta3.times{print 47}\n\ta2.times{print 7}\nelsif a4==a3+1\n\t# 47が多い\n\t# 47を作っていく\n\ta1-=a4\n\ta2-=a4\n\tif a1<0 || a2<0 then #生成不能\n\t\tp -1\n\t\texit\n\tend\n\tprint 7\n\ta1.times{print 4}\n\ta3.times{print 47}\n\ta2.times{print 7}\n\tprint 4\nelsif a3==a4\n\t#47優先解法\n\ta1-=a3+1\n\ta2-=a3\n\tif a1<0||a2<0 then\n\t\ta1+=a3+1\n\t\ta2+=a3\n\n\t\ta1-=a3\n\t\ta2-=a3+1\n\t\t#74優先解法\n\t\tprint 7\n\t\t(a1+1).times{print 4}\n\t\t(a3-1).times{print 74}\n\t\t(a2+1).times{print 7}\n\t\texit\n\tend\n\ta1.times{print 4}\n\tprint 4\n\t(a3-1).times{print 74}\n\t(a2+1).times{ print 7 }\n\tprint 4\nelse\n\tputs \"-1\"\nend\n\n"}], "src_uid": "f63cef419fdfd4ab1c61ac6d7e7657a6"} {"nl": {"description": "During the \"Russian Code Cup\" programming competition, the testing system stores all sent solutions for each participant. We know that many participants use random numbers in their programs and are often sent several solutions with the same source code to check.Each participant is identified by some unique positive integer k, and each sent solution A is characterized by two numbers: x — the number of different solutions that are sent before the first solution identical to A, and k — the number of the participant, who is the author of the solution. Consequently, all identical solutions have the same x.It is known that the data in the testing system are stored in the chronological order, that is, if the testing system has a solution with number x (x > 0) of the participant with number k, then the testing system has a solution with number x - 1 of the same participant stored somewhere before.During the competition the checking system crashed, but then the data of the submissions of all participants have been restored. Now the jury wants to verify that the recovered data is in chronological order. Help the jury to do so.", "input_spec": "The first line of the input contains an integer n (1 ≤ n ≤ 105) — the number of solutions. Each of the following n lines contains two integers separated by space x and k (0 ≤ x ≤ 105; 1 ≤ k ≤ 105) — the number of previous unique solutions and the identifier of the participant.", "output_spec": "A single line of the output should contain «YES» if the data is in chronological order, and «NO» otherwise.", "sample_inputs": ["2\n0 1\n1 1", "4\n0 1\n1 2\n1 1\n0 2", "4\n0 1\n1 1\n0 1\n0 2"], "sample_outputs": ["YES", "NO", "YES"], "notes": null}, "positive_code": [{"source_code": "\n#$stdin.reopen('/home/chen/Desktop/input.txt', 'r')\n\n=begin\nn,k=gets.split.map(&:to_i)\n\nif k*(n/2)>=n\n\tputs -1\nelse\n\tputs n*k\n\t(1..n).each do |ind|\n\t\t(1..k).each do |ind2|\n\t\t\tputs \"#{ind} #{(ind+ind2-1)%n+1}\"\n\t\tend\n\tend\nend\n\n\n=end\n\n\narray = [-1]* (10**5+1)\n\nn=gets.to_i\ni=0\n\nwhile iarray[k]+1\n\t\tputs 'NO'\n\t\texit 0\n\tend\n\ti+=1\nend\n\nputs 'YES'\n\n\n\n\n"}, {"source_code": "prevmax = Hash.new -1\nn = gets.to_i\nn.times do\n x,k = gets.split.map &:to_i\n d = x - prevmax[k]\n if d == 1 \n prevmax[k] += 1\n elsif d > 1\n puts \"NO\"\n exit\n end\nend\n\nputs \"YES\"\n\n"}, {"source_code": "parties = Array.new(10**5+1, nil)\n\nn = gets.to_i\n\ndata = Array.new(n) { gets.chomp.split(' ').map(&:to_i) }\n\nans = 'YES'\ndata.each do |x, k|\n t = parties[k]\n if t\n if x > t + 1\n ans = 'NO'\n break\n elsif x == t + 1\n parties[k] = x\n end\n else\n if x == 0\n parties[k] = 0\n else\n ans = 'NO'\n break\n end\n end\nend\n\nputs ans\n"}, {"source_code": "require 'set'\nn = Integer gets\na = Array.new(10 ** 5)\ncnt = Array.new(10 ** 5, 0)\nfor i in 0...10**5\n a[i] = [].to_set\nend\nfor i in 0...n\n x, k = gets.split.map {|x| Integer x}\n k -= 1\n if !a[k].include?(x) && x != cnt[k]\n puts 'NO'\n exit\n end\n unless a[k].include?(x)\n a[k].add(cnt[k])\n cnt[k] += 1\n end\nend\nputs 'YES'"}], "negative_code": [{"source_code": "require 'set'\nn = Integer gets\na = Array.new(10 ** 5, [].to_set)\ncnt = 0\nfor i in 0...n\n x, k = gets.split.map {|x| Integer x}\n k -= 1\n if !a[k].include?(x) && x != cnt\n puts 'NO'\n exit\n end\n unless a[k].include?(x)\n a[k].add(cnt)\n cnt += 1\n end\nend\nputs 'YES'"}, {"source_code": "require 'set'\nn = Integer gets\na = Array.new(10 ** 5, [].to_set)\ncnt = Array.new(10 ** 5, 0)\nfor i in 0...n\n x, k = gets.split.map {|x| Integer x}\n k -= 1\n if !a[k].include?(x) && x != cnt[k]\n puts 'NO'\n exit\n end\n unless a[k].include?(x)\n a[k].add(cnt[k])\n cnt[k] += 1\n end\nend\nputs 'YES'"}, {"source_code": "require 'set'\nn = Integer gets\na = Array.new(10 ** 5, [].to_set)\ncnt = Array.new(10 ** 5, 0)\nfor i in 0...n\n x, k = gets.split.map {|x| Integer x}\n k -= 1\n if !a[k].include?(x) && x != cnt[k]\n puts 'NO'\n exit\n end\n unless a[k].include?(x)\n a[k].add(cnt[k])\n end\n cnt[k] += 1\nend\nputs 'YES'"}], "src_uid": "6f6f56d3106e09d51ebb3a206fa4be3c"} {"nl": {"description": "Pasha loves his phone and also putting his hair up... But the hair is now irrelevant.Pasha has installed a new game to his phone. The goal of the game is following. There is a rectangular field consisting of n row with m pixels in each row. Initially, all the pixels are colored white. In one move, Pasha can choose any pixel and color it black. In particular, he can choose the pixel that is already black, then after the boy's move the pixel does not change, that is, it remains black. Pasha loses the game when a 2 × 2 square consisting of black pixels is formed. Pasha has made a plan of k moves, according to which he will paint pixels. Each turn in his plan is represented as a pair of numbers i and j, denoting respectively the row and the column of the pixel to be colored on the current move.Determine whether Pasha loses if he acts in accordance with his plan, and if he does, on what move the 2 × 2 square consisting of black pixels is formed.", "input_spec": "The first line of the input contains three integers n, m, k (1 ≤ n, m ≤ 1000, 1 ≤ k ≤ 105) — the number of rows, the number of columns and the number of moves that Pasha is going to perform. The next k lines contain Pasha's moves in the order he makes them. Each line contains two integers i and j (1 ≤ i ≤ n, 1 ≤ j ≤ m), representing the row number and column number of the pixel that was painted during a move.", "output_spec": "If Pasha loses, print the number of the move when the 2 × 2 square consisting of black pixels is formed. If Pasha doesn't lose, that is, no 2 × 2 square consisting of black pixels is formed during the given k moves, print 0.", "sample_inputs": ["2 2 4\n1 1\n1 2\n2 1\n2 2", "2 3 6\n2 3\n2 2\n1 3\n2 2\n1 2\n1 1", "5 3 7\n2 3\n1 2\n1 1\n4 1\n3 1\n5 3\n3 2"], "sample_outputs": ["4", "5", "0"], "notes": null}, "positive_code": [{"source_code": "h, w, k = gets.split.map( &:to_i )\n\nboard = Array.new( h + 2 ) { |idx| Array.new( w + 2, 0 ) }\n\n\nk.times do | t |\n\ty, x = gets.split.map( &:to_i )\n\tboard[y][x] = 1\n\n\t2.times do | i |\n\t\t2.times do | j |\n\t\t\tf = true \n\t\t\t2.times do | k |\n\t\t\t\t2.times do | l |\n\t\t\t\t\tf &= board[ y - 1 + i + k ][ x - 1 + j + l ] == 1\n\t\t\t\tend\n\t\t\tend\n\n\t\t\tif f\n\t\t\t\tputs t + 1\n\t\t\t\texit 0\n\t\t\tend\n\t\tend\n\tend\nend\n\nputs 0\n\t\n"}, {"source_code": "# http://codeforces.com/contest/508/problem/A\nn, m, k = gets.chomp.split.map(&:to_i)\nf = []\n(n + 2).times { f << Array.new(m + 2) }\n\nans = nil\nk.times do |i|\n y, x = gets.chomp.split.map(&:to_i)\n f[y][x] = 1\n\n points = [\n [f[y][x], f[y][x + 1], f[y + 1][x], f[y + 1][x + 1]],\n [f[y][x], f[y][x - 1], f[y + 1][x], f[y + 1][x - 1]],\n [f[y][x], f[y][x + 1], f[y - 1][x], f[y - 1][x + 1]],\n [f[y][x], f[y][x - 1], f[y - 1][x], f[y - 1][x - 1]]\n ]\n\n if points.find { |arr| arr.map(&:to_i).inject(0, :+) == 4 }\n ans = i + 1 if ans.nil?\n end\nend\n\nputs ans.nil? ? 0 : ans\n"}, {"source_code": "#!/usr/bin/ruby\n# coding: utf-8\n\nn, m, k = gets.split.map &:to_i\n\nans = Array.new(n + 1) { Array.new(m + 1, 0) }\n\nl = Array.new(k + 1, Array.new(2))\n\n1.upto(k){|i|\n l[i] = gets.split.map &:to_i\n}\n\n1.upto(k){|i|\n ans[l[i][0]][l[i][1]] |= 1\n ans[l[i][0]][l[i][1] - 1] |= 2\n ans[l[i][0] - 1][l[i][1]] |= 4\n ans[l[i][0] - 1][l[i][1] - 1] |= 8\n if ans[l[i][0] - 1][l[i][1] - 1] == 15 || ans[l[i][0] - 1][l[i][1]] == 15 || ans[l[i][0]][l[i][1] - 1] == 15 || ans[l[i][0]][l[i][1]] == 15 \n puts i\n exit 0\n end\n}\nputs 0"}, {"source_code": "DEBUG = false\n\nclass Board\n DELTA = [\n [ [-1, 0], [-1, 1], [0, 1] ],\n [ [0, 1], [1, 1], [1, 0] ],\n [ [1, 0], [1, -1], [0, -1] ],\n [ [0, -1], [-1, -1], [-1, 0] ]\n ]\n attr_accessor :boards, :height, :width\n\n def initialize(height, width)\n self.boards = {}\n self.height = height\n self.width = width\n end\n\n def go(row, col)\n return false if row <= 0 || col <= 0 || row > self.height || col > self.width\n self.boards[[row, col]] = true\n return true if self.end_game?(row, col)\n return false\n end\n\n def end_game?(row, col)\n if DEBUG\n puts \"----- Check step -----\"\n puts \"row = #{row}, col = #{col}\"\n end\n\n Board::DELTA.any? do |delta_set|\n\n if DEBUG\n puts \"----- Check data set -----\"\n end\n\n delta_set.all? do |delta_row, delta_col|\n if DEBUG\n puts \"#{row + delta_row} #{col + delta_col}\"\n end\n\n (row + delta_row > 0) &&\n (col + delta_col > 0) &&\n (row + delta_row <= self.height) &&\n (col + delta_col <= self.width) &&\n (self.boards[[row + delta_row, col + delta_col]] == true)\n end\n end\n end\nend\n\nheight, width, steps = gets.strip.split(' ').map &:to_i\n\nboard = Board.new(height, width)\n\nend_game_at = 0\n1.upto(steps).each do |index|\n step_row, step_col = gets.strip.split(' ').map &:to_i\n end_game = board.go(step_row, step_col)\n if end_game\n end_game_at = index\n break\n end\nend\n\nputs end_game_at\n"}, {"source_code": "DEBUG = false\n\nclass Board\n DELTA = [\n [ [-1, 0], [-1, 1], [0, 1] ],\n [ [0, 1], [1, 1], [1, 0] ],\n [ [1, 0], [1, -1], [0, -1] ],\n [ [0, -1], [-1, -1], [-1, 0] ]\n ]\n attr_accessor :boards, :height, :width\n\n def initialize(height, width)\n self.boards = {}\n self.height = height\n self.width = width\n end\n\n def go(row, col)\n return false if row <= 0 || col <= 0 || row > self.height || col > self.width\n self.boards[\"#{row}_#{col}\"] = true\n return true if self.end_game?(row, col)\n return false\n end\n\n def end_game?(row, col)\n if DEBUG\n puts \"----- Check step -----\"\n puts \"row = #{row}, col = #{col}\"\n end\n\n Board::DELTA.any? do |delta_set|\n\n if DEBUG\n puts \"----- Check data set -----\"\n end\n\n delta_set.all? do |delta_row, delta_col|\n if DEBUG\n puts \"#{row + delta_row} #{col + delta_col}\"\n end\n\n (row + delta_row > 0) &&\n (col + delta_col > 0) &&\n (row + delta_row <= self.height) &&\n (col + delta_col <= self.width) &&\n (self.boards[\"#{row + delta_row}_#{col + delta_col}\"] == true)\n end\n end\n end\nend\n\nheight, width, steps = gets.strip.split(' ').map &:to_i\n\nboard = Board.new(height, width)\n\nend_game_at = 0\n1.upto(steps).each do |index|\n step_row, step_col = gets.strip.split(' ').map &:to_i\n end_game = board.go(step_row, step_col)\n if end_game\n end_game_at = index\n break\n end\nend\n\nputs end_game_at\n"}, {"source_code": "class Board\n DELTA = [\n [ [-1, 0], [-1, 1], [0, 1] ],\n [ [0, 1], [1, 1], [1, 0] ],\n [ [1, 0], [1, -1], [0, -1] ],\n [ [0, -1], [-1, -1], [-1, 0] ]\n ]\n attr_accessor :boards, :height, :width\n\n def initialize(height, width)\n self.boards = {}\n self.height = height\n self.width = width\n end\n\n def go(row, col)\n return false if row <= 0 || col <= 0 || row > self.height || col > self.width\n self.boards[row] ||= []\n self.boards[row][col] = true\n return true if self.end_game?(row, col)\n return false\n end\n\n def end_game?(row, col)\n Board::DELTA.any? do |delta_set|\n delta_set.all? do |delta_row, delta_col|\n (row + delta_row > 0) &&\n (col + delta_col > 0) &&\n (row + delta_row <= self.height) &&\n (col + delta_col <= self.width) &&\n (!self.boards[row + delta_row].nil? && self.boards[row + delta_row][col + delta_col] == true)\n end\n end\n end\nend\n\nheight, width, steps = gets.strip.split(' ').map &:to_i\n\nboard = Board.new(height, width)\n\nend_game_at = 0\n1.upto(steps).each do |index|\n step_row, step_col = gets.strip.split(' ').map &:to_i\n end_game = board.go(step_row, step_col)\n if end_game\n end_game_at = index\n break\n end\nend\n\nputs end_game_at\n"}, {"source_code": "DEBUG = false\n\nclass Board\n DELTA = [\n [ [-1, 0], [-1, 1], [0, 1] ],\n [ [0, 1], [1, 1], [1, 0] ],\n [ [1, 0], [1, -1], [0, -1] ],\n [ [0, -1], [-1, -1], [-1, 0] ]\n ]\n attr_accessor :boards, :height, :width\n\n def initialize(height, width)\n self.boards = {}\n self.height = height\n self.width = width\n end\n\n def go(row, col)\n return false if row <= 0 || col <= 0 || row > self.height || col > self.width\n self.boards[\"#{row}_#{col}\".to_sym] = true\n return true if self.end_game?(row, col)\n return false\n end\n\n def end_game?(row, col)\n if DEBUG\n puts \"----- Check step -----\"\n puts \"row = #{row}, col = #{col}\"\n end\n\n Board::DELTA.any? do |delta_set|\n\n if DEBUG\n puts \"----- Check data set -----\"\n end\n\n delta_set.all? do |delta_row, delta_col|\n if DEBUG\n puts \"#{row + delta_row} #{col + delta_col}\"\n end\n\n (row + delta_row > 0) &&\n (col + delta_col > 0) &&\n (row + delta_row <= self.height) &&\n (col + delta_col <= self.width) &&\n (self.boards[\"#{row + delta_row}_#{col + delta_col}\".to_sym] == true)\n end\n end\n end\nend\n\nheight, width, steps = gets.strip.split(' ').map &:to_i\n\nboard = Board.new(height, width)\n\nend_game_at = 0\n1.upto(steps).each do |index|\n step_row, step_col = gets.strip.split(' ').map &:to_i\n end_game = board.go(step_row, step_col)\n if end_game\n end_game_at = index\n break\n end\nend\n\nputs end_game_at\n"}, {"source_code": "DELTA = [\n [ [-1, 0], [-1, 1], [0, 1] ],\n [ [0, 1], [1, 1], [1, 0] ],\n [ [1, 0], [1, -1], [0, -1] ],\n [ [0, -1], [-1, -1], [-1, 0] ]\n]\ndef go(boards, row, col, height, width)\n return if row <= 0 || col <= 0 || row > height || col > width\n boards[row] ||= []\n boards[row][col] = true\nend\n\ndef end_game?(boards, row, col, height, width)\n DELTA.any? do |delta_set|\n delta_set.all? do |delta_row, delta_col|\n (row + delta_row > 0) &&\n (col + delta_col > 0) &&\n (row + delta_row <= height) &&\n (col + delta_col <= width) &&\n (!boards[row + delta_row].nil? && boards[row + delta_row][col + delta_col] == true)\n end\n end\nend\n\nheight, width, steps = gets.strip.split(' ').map &:to_i\nboards = []\nend_game_at = 0\n1.upto(steps).each do |index|\n step_row, step_col = gets.strip.split(' ').map &:to_i\n go(boards, step_row, step_col, height, width)\n if end_game?(boards, step_row, step_col, height, width)\n end_game_at = index\n break\n end\nend\n\nputs end_game_at\n"}, {"source_code": "DEBUG = false\n\nclass Board\n DELTA = [\n [ [-1, 0], [-1, 1], [0, 1] ],\n [ [0, 1], [1, 1], [1, 0] ],\n [ [1, 0], [1, -1], [0, -1] ],\n [ [0, -1], [-1, -1], [-1, 0] ]\n ]\n attr_accessor :boards, :height, :width\n\n def initialize(height, width)\n self.boards = []\n self.height = height\n self.width = width\n end\n\n def go(row, col)\n return false if row <= 0 || col <= 0 || row > self.height || col > self.width\n self.boards[row] ||= []\n self.boards[row][col] = true\n return true if self.end_game?(row, col)\n return false\n end\n\n def end_game?(row, col)\n if DEBUG\n puts \"----- Check step -----\"\n puts \"row = #{row}, col = #{col}\"\n end\n\n Board::DELTA.any? do |delta_set|\n\n if DEBUG\n puts \"----- Check data set -----\"\n end\n\n delta_set.all? do |delta_row, delta_col|\n if DEBUG\n puts \"#{row + delta_row} #{col + delta_col}\"\n end\n\n (row + delta_row > 0) &&\n (col + delta_col > 0) &&\n (row + delta_row <= self.height) &&\n (col + delta_col <= self.width) &&\n (!self.boards[row + delta_row].nil? && self.boards[row + delta_row][col + delta_col] == true)\n end\n end\n end\nend\n\nheight, width, steps = gets.strip.split(' ').map &:to_i\n\nboard = Board.new(height, width)\n\nend_game_at = 0\n1.upto(steps).each do |index|\n step_row, step_col = gets.strip.split(' ').map &:to_i\n end_game = board.go(step_row, step_col)\n if end_game\n end_game_at = index\n break\n end\nend\n\nputs end_game_at\n"}, {"source_code": "# File.open(\"5.txt\",\"r\") do |file|\n\ta=Array.new\n\tb=Array.new(1005)\n\tfor i in 0..1004\n\t\tb[i]=Array.new(1005,0)\n\tend\n\t# str=file.gets.to_s\n\tstr=gets.to_s\n\ta=str.split\n\tn=a[0].to_i\n\tm=a[1].to_i\n\tk=a[2].to_i\n\th=0\n\tfor r in 0..k\n\t\t# str=file.gets.to_s\n\t\tstr=gets.to_s\n\t\ta=str.split\n\t\ti=a[0].to_i\n\t\tj=a[1].to_i\n\t\tb[i][j]=1\n\t\tif b[i-1][j-1]==1 and b[i-1][j]==1 and b[i][j-1]==1 and h==0\n\t\t\th=r+1\n\t\telsif b[i-1][j+1]==1 and b[i-1][j]==1 and b[i][j+1]==1 and h==0\n\t\t\th=r+1\n\t\telsif b[i+1][j-1]==1 and b[i+1][j]==1 and b[i][j-1]==1 and h==0\n\t\t\th=r+1\n\t\telsif b[i+1][j+1]==1 and b[i+1][j]==1 and b[i][j+1]==1 and h==0\n\t\t\th=r+1\n\t\tend\n\tend\n\tif h==0\n\t\tputs 0\n\telse\n\t\tputs h\n\tend\n# end"}, {"source_code": "def checksquare(array,i,j)\n\tif array[i][j+1]==1 && array[i+1][j]==1 && array[i+1][j+1]==1\n\t\treturn true\n\tend\n\tif array[i][j-1]==1 && array[i+1][j-1]==1 && array[i+1][j]==1\n\t\treturn true\n\tend\n\tif array[i-1][j-1]==1 && array[i-1][j]==1 && array[i][j-1]==1\n\t\treturn true\n\tend\n\tif array[i-1][j]==1 && array[i-1][j+1]==1 && array[i][j+1]==1\n\t\treturn true\n\tend\n\treturn false\nend\nn,m,k=gets.split().map{|i| Integer(i)}\npos=0\nflag=1\narray=Array.new\nfor i in 0..n\n\tarray[i]=Array.new\n\tfor j in 0..m\n\t\tarray[i][j]=0\n\tend\nend\nstart=1\nwhile start<=k\n\tstart+=1\n\ti,j=gets.split().map{|h| Integer(h)}\n\tarray[i-1][j-1]=1\n\tif flag==1 && checksquare(array,i-1,j-1)==true\n\t\tpos=start-1\n\t\tflag=0\n\tend\nend\nputs pos\n"}], "negative_code": [{"source_code": "#!/usr/bin/ruby\n# coding: utf-8\n\nn, m, k = gets.split.map &:to_i\n\nans = Array.new(n + 1, Array.new(m + 1, 0))\n\nl = Array.new(k + 1, Array.new(2))\n\n1.upto(k){|i|\n l[i] = gets.split.map &:to_i\n}\n\n1.upto(k){|i|\n ans[l[i][0] - 1][l[i][1] - 1] |= 1\n ans[l[i][0] - 1][l[i][1]] |= 2\n ans[l[i][0]][l[i][1] - 1] |= 4\n ans[l[i][0]][l[i][1]] |= 8\n if ans[l[i][0] - 1][l[i][1] - 1] == 15 || ans[l[i][0] - 1][l[i][1]] == 15 || ans[l[i][0]][l[i][1] - 1] == 15 || ans[l[i][0]][l[i][1]] == 15 \n puts k\n break\n end\n}"}, {"source_code": "#!/usr/bin/ruby\n# coding: utf-8\n\nn, m, k = gets.split.map &:to_i\n\nans = Array.new(n + 1) { Array.new(m + 1, 0) }\n\nl = Array.new(k + 1, Array.new(2))\n\n1.upto(k){|i|\n l[i] = gets.split.map &:to_i\n}\n\n1.upto(k){|i|\n ans[l[i][0]][l[i][1]] |= 1\n ans[l[i][0]][l[i][1] - 1] |= 2\n ans[l[i][0] - 1][l[i][1]] |= 4\n ans[l[i][0] - 1][l[i][1] - 1] |= 8\n if ans[l[i][0] - 1][l[i][1] - 1] == 15 || ans[l[i][0] - 1][l[i][1]] == 15 || ans[l[i][0]][l[i][1] - 1] == 15 || ans[l[i][0]][l[i][1]] == 15 \n puts i\n break\n end\n}"}, {"source_code": "#!/usr/bin/ruby\n# coding: utf-8\n\nn, m, k = gets.split.map &:to_i\n\nans = Array.new(n + 1) { Array.new(m + 1, 0) }\n\nl = Array.new(k + 1, Array.new(2))\n\n1.upto(k){|i|\n l[i] = gets.split.map &:to_i\n}\n\n1.upto(k){|i|\n ans[l[i][0]][l[i][1]] |= 1\n ans[l[i][0]][l[i][1] - 1] |= 2\n ans[l[i][0] - 1][l[i][1]] |= 4\n ans[l[i][0] - 1][l[i][1] - 1] |= 8\n if ans[l[i][0] - 1][l[i][1] - 1] == 15 || ans[l[i][0] - 1][l[i][1]] == 15 || ans[l[i][0]][l[i][1] - 1] == 15 || ans[l[i][0]][l[i][1]] == 15 \n puts i\n break\n end\n}\nputs 0"}, {"source_code": "DEBUG = false\n\nclass Board\n DELTA = [\n [ [-1, 0], [-1, 1], [0, 1] ],\n [ [0, 1], [1, 1], [1, 0] ],\n [ [1, 0], [1, -1], [0, -1] ],\n [ [0, -1], [-1, -1], [-1, 0] ]\n ]\n attr_accessor :boards, :height, :width\n\n def initialize(height, width)\n self.boards = {}\n self.height = height\n self.width = width\n end\n\n def go(row, col)\n return false if row <= 0 || col <= 0 || row > self.height || col > self.width\n self.boards[\"#{row}_#{col}\"] = true\n if self.end_game?(row, col)\n return true\n end\n return false\n end\n\n def end_game?(row, col)\n if DEBUG\n puts \"----- Check step -----\"\n puts \"row = #{row}, col = #{col}\"\n end\n\n Board::DELTA.any? do |delta_set|\n\n if DEBUG\n puts \"----- Check data set -----\"\n end\n\n delta_set.all? do |delta_row, delta_col|\n if DEBUG\n puts \"#{row + delta_row} #{col + delta_col}\"\n end\n\n (row + delta_row > 0) &&\n (col + delta_col > 0) &&\n (row + delta_row <= self.height) &&\n (row + delta_col <= self.width) &&\n (self.boards[\"#{row + delta_row}_#{col + delta_col}\"] == true)\n end\n end\n end\nend\n\nheight, width, steps = gets.strip.split(' ').map &:to_i\n\nboard = Board.new(height, width)\n\nend_game_at = 0\n1.upto(steps).each do |index|\n step_row, step_col = gets.strip.split(' ').map &:to_i\n end_game = board.go(step_row, step_col)\n if end_game\n end_game_at = index\n break\n end\nend\n\nputs end_game_at\n"}, {"source_code": "# File.open(\"5.txt\",\"r\") do |file|\n\ta=Array.new\n\tb=Array.new(1005)\n\tfor i in 0..1004\n\t\tb[i]=Array.new(1005,0)\n\tend\n\t# str=file.gets.to_s\n\tstr=gets.to_s\n\ta=str.split\n\tn=a[0].to_i\n\tm=a[1].to_i\n\tk=a[2].to_i\n\th=0\n\tfor r in 0..k\n\t\t# str=file.gets.to_s\n\t\tstr=gets.to_s\n\t\ta=str.split\n\t\ti=a[0].to_i\n\t\tj=a[1].to_i\n\t\tb[i][j]=1\n\t\tif b[i-1][j-1]==1 and b[i-1][j]==1 and b[i][j-1]==1\n\t\t\th=r+1\n\t\telsif b[i-1][j+1]==1 and b[i-1][j]==1 and b[i][j+1]==1\n\t\t\th=r+1\n\t\telsif b[i+1][j-1]==1 and b[i+1][j]==1 and b[i][j-1]==1\n\t\t\th=r+1\n\t\telsif b[i+1][j+1]==1 and b[i+1][j]==1 and b[i][j+1]==1\n\t\t\th=r+1\n\t\tend\n\tend\n\tif h==0\n\t\tputs 0\n\telse\n\t\tputs h\n\tend\n# end"}], "src_uid": "0dc5469831c1d5d34aa3b7b172e3237b"} {"nl": {"description": "Today the kindergarten has a new group of $$$n$$$ kids who need to be seated at the dinner table. The chairs at the table are numbered from $$$1$$$ to $$$4n$$$. Two kids can't sit on the same chair. It is known that two kids who sit on chairs with numbers $$$a$$$ and $$$b$$$ ($$$a \\neq b$$$) will indulge if: $$$gcd(a, b) = 1$$$ or, $$$a$$$ divides $$$b$$$ or $$$b$$$ divides $$$a$$$. $$$gcd(a, b)$$$ — the maximum number $$$x$$$ such that $$$a$$$ is divisible by $$$x$$$ and $$$b$$$ is divisible by $$$x$$$.For example, if $$$n=3$$$ and the kids sit on chairs with numbers $$$2$$$, $$$3$$$, $$$4$$$, then they will indulge since $$$4$$$ is divided by $$$2$$$ and $$$gcd(2, 3) = 1$$$. If kids sit on chairs with numbers $$$4$$$, $$$6$$$, $$$10$$$, then they will not indulge.The teacher really doesn't want the mess at the table, so she wants to seat the kids so there are no $$$2$$$ of the kid that can indulge. More formally, she wants no pair of chairs $$$a$$$ and $$$b$$$ that the kids occupy to fulfill the condition above.Since the teacher is very busy with the entertainment of the kids, she asked you to solve this problem.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\leq t \\leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of one line containing an integer $$$n$$$ ($$$1 \\leq n \\leq 100$$$) — the number of kids.", "output_spec": "Output $$$t$$$ lines, which contain $$$n$$$ distinct integers from $$$1$$$ to $$$4n$$$ — the numbers of chairs that the kids should occupy in the corresponding test case. If there are multiple answers, print any of them. You can print $$$n$$$ numbers in any order.", "sample_inputs": ["3\n2\n3\n4"], "sample_outputs": ["6 4\n4 6 10\n14 10 12 8"], "notes": null}, "positive_code": [{"source_code": "# frozen_string_literal: true\n\ngets.chomp.to_i.times do |_m|\n n = gets.chomp.to_i\n (2 * n + 2).step(4 * n, 2) { |i| print i.to_s + ' ' }\n puts\nend\n"}, {"source_code": "t = gets.chomp.to_i\nt.times do\n n = gets.chomp.to_i\n res = [4 * n]\n while res.size < n\n cur = res[-1]\n if cur <= 1\n break\n end\n while not(res.all? {|el| el.gcd(cur) > 1} && \n res.all? {|el| el % cur > 0 && cur % el > 0})\n cur -= 1\n end\n res << cur\n end\n puts res.join(' ')\nend "}, {"source_code": "$\\ = \" \"\ngets.to_i.times do\n n = gets.to_i\n cur = 2 * n + 2\n while cur <= 4 * n do\n print cur\n cur += 2\n end\n puts\nend"}], "negative_code": [], "src_uid": "cf4d8a5caa37def0b7c0007743139e36"} {"nl": {"description": "You are given $$$n$$$ sticks with positive integral length $$$a_1, a_2, \\ldots, a_n$$$.You can perform the following operation any number of times (possibly zero): choose one stick, then either increase or decrease its length by $$$1$$$. After each operation, all sticks should have positive lengths. What is the minimum number of operations that you have to perform such that it is possible to select three of the $$$n$$$ sticks and use them without breaking to form an equilateral triangle?An equilateral triangle is a triangle where all of its three sides have the same length.", "input_spec": "The first line of the input contains a single integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$3 \\le n \\le 300$$$) — the number of sticks. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$) — the lengths of the sticks. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$300$$$. ", "output_spec": "For each test case, print one integer on a single line — the minimum number of operations to be made.", "sample_inputs": ["4\n\n3\n\n1 2 3\n\n4\n\n7 3 7 3\n\n5\n\n3 4 2 1 1\n\n8\n\n3 1 4 1 5 9 2 6"], "sample_outputs": ["2\n4\n1\n1"], "notes": "NoteIn the first test case, you can increase the length of the first stick by $$$1$$$, then decrease the length of the third stick by $$$1$$$. In total, you perform $$$2$$$ operations, such that the three sticks form an equilateral triangle of side length $$$2$$$.In the fourth test case, you can decrease the length of the seventh stick by $$$1$$$. An equilateral triangle of side length $$$1$$$ can be selected and formed by the second, fourth, and seventh sticks."}, "positive_code": [{"source_code": "def main\n t = inp.to_i\n t.times do\n n = inp.to_i\n a = mapint(inarr).sort!\n ans = 1 << 32\n 0.upto(n - 1) do|i|\n (i + 1).upto(n - 1) do|j|\n (j + 1).upto(n - 1) do|k|\n dif = a[j] - a[i] + a[k] - a[j]\n ans = [ans, dif].min\n end\n end\n\n end\n puts ans\n\n end\n\nend\n\n# lib\ndef inp\n gets.chomp\nend\n\ndef inarr\n gets.chomp.split\nend\n\ndef mapint(lst)\n lst.map(&:to_i)\nend\n\nmain"}, {"source_code": "gets.to_i.times do\n n=gets.to_i\n a=gets.split.map(&:to_i).sort\n ans=Float::INFINITY\n a.each_cons(3) do |i,j,k|\n ans=[ans,j-i+k-j].min\n end\n puts ans\nend\n"}], "negative_code": [], "src_uid": "53ff11122a277d1eb29fe2034017fd75"} {"nl": {"description": "You've got an n × m pixel picture. Each pixel can be white or black. Your task is to change the colors of as few pixels as possible to obtain a barcode picture.A picture is a barcode if the following conditions are fulfilled: All pixels in each column are of the same color. The width of each monochrome vertical line is at least x and at most y pixels. In other words, if we group all neighbouring columns of the pixels with equal color, the size of each group can not be less than x or greater than y. ", "input_spec": "The first line contains four space-separated integers n, m, x and y (1 ≤ n, m, x, y ≤ 1000; x ≤ y). Then follow n lines, describing the original image. Each of these lines contains exactly m characters. Character \".\" represents a white pixel and \"#\" represents a black pixel. The picture description doesn't have any other characters besides \".\" and \"#\".", "output_spec": "In the first line print the minimum number of pixels to repaint. It is guaranteed that the answer exists. ", "sample_inputs": ["6 5 1 2\n##.#.\n.###.\n###..\n#...#\n.##.#\n###..", "2 5 1 1\n#####\n....."], "sample_outputs": ["11", "5"], "notes": "NoteIn the first test sample the picture after changing some colors can looks as follows: .##...##...##...##...##...##.. In the second test sample the picture after changing some colors can looks as follows: .#.#..#.#. "}, "positive_code": [{"source_code": "n, m, x, y = gets.split.map(&:to_i)\na = Array.new(n){gets.chomp.chars.to_a}\na = a.transpose.map{|_| _.count('#')}\nw = [0] + [n * m] * m\nb = [0] + [n * m] * m\nx.upto(m) do |i|\n c = 0\n (i - 1).downto([i - y, 0].max) do |j|\n c += a[j]\n if i - j >= x\n w[i] = [w[i], b[j] + c].min\n b[i] = [b[i], w[j] + (i - j) * n - c].min\n end\n end\nend\np [w.last, b.last].min\n"}, {"source_code": "n, m, x, y = gets.split.map(&:to_i)\na = Array.new(n){gets.chomp.chars.to_a}\na = a.transpose.map{|_| _.count('#')}\nw = [0] + [n * m] * m\nb = [0] + [n * m] * m\nx.upto(m) do |i|\n c = 0\n (i - 1).downto([i - y, 0].max) do |j|\n c += a[j]\n if i - j >= x\n w[i] = [w[i], b[j] + c].min\n b[i] = [b[i], w[j] + (i - j) * n - c].min\n end\n end\nend\np [w.last, b.last].min\n\n"}, {"source_code": "n, m, x, y = gets.split.map(&:to_i)\na = Array.new(n){gets.chomp.chars.to_a}\na = a.transpose.map{|_| _.count('#')}\nw = [0] + [n * m] * m\nb = [0] + [n * m] * m\nx.upto(m) do |i|\n c = 0\n (i - 1).downto([i - y, 0].max) do |j|\n c += a[j]\n if i - j >= x\n w[i] = [w[i], b[j] + c].min\n b[i] = [b[i], w[j] + (i - j) * n - c].min\n end\n end\nend\np [w.last, b.last].min\n\n"}, {"source_code": "n, m, x, y = gets.split.map(&:to_i)\na = Array.new(n){gets.chomp.chars.to_a}\na = a.transpose.map{|_| _.count('#')}\nw = [0] + [n * m] * m\nb = [0] + [n * m] * m\nx.upto(m) do |i|\n c = 0\n (i - 1).downto([i - y, 0].max) do |j|\n c += a[j]\n if i - j >= x\n w[i] = [w[i], b[j] + c].min\n b[i] = [b[i], w[j] + (i - j) * n - c].min\n end\n end\nend\np [w.last, b.last].min\n\n"}, {"source_code": "n, m, x, y = gets.split.map(&:to_i)\na = Array.new(n){gets.chomp.chars.to_a}\na = a.transpose.map{|_| _.count('#')}\nw = [0] + [n * m] * m\nb = [0] + [n * m] * m\nx.upto(m) do |i|\n c = 0\n (i - 1).downto([i - y, 0].max) do |j|\n c += a[j]\n if i - j >= x\n w[i] = [w[i], b[j] + c].min\n b[i] = [b[i], w[j] + (i - j) * n - c].min\n end\n end\nend\np [w.last, b.last].min\n\n"}, {"source_code": "n, m, x, y = gets.split.map(&:to_i)\na = Array.new(n){gets.chomp.chars.to_a}\na = a.transpose.map{|_| _.count('#')}\nw = [0] + [n * m] * m\nb = [0] + [n * m] * m\nx.upto(m) do |i|\n c = 0\n (i - 1).downto([i - y, 0].max) do |j|\n c += a[j]\n if i - j >= x\n w[i] = [w[i], b[j] + c].min\n b[i] = [b[i], w[j] + (i - j) * n - c].min\n end\n end\nend\np [w.last, b.last].min"}, {"source_code": "n, m, x, y = gets.split.map(&:to_i)\na = Array.new(n){gets.chomp.chars.to_a}\na = a.transpose.map{|_| _.count('#')}\nw = [0] + [n * m] * m\nb = [0] + [n * m] * m\nx.upto(m) do |i|\n c = 0\n (i - 1).downto([i - y, 0].max) do |j|\n c += a[j]\n if i - j >= x\n w[i] = [w[i], b[j] + c].min\n b[i] = [b[i], w[j] + (i - j) * n - c].min\n end\n end\nend\np [w.last, b.last].min\n\n"}, {"source_code": "n, m, x, y = gets.split.map(&:to_i)\na = Array.new(n){gets.chomp.chars.to_a}\na = a.transpose.map{|_| _.count('#')}\nw = [0] + [n * m] * m\nb = [0] + [n * m] * m\nx.upto(m) do |i|\n c = 0\n (i - 1).downto([i - y, 0].max) do |j|\n c += a[j]\n if i - j >= x\n w[i] = [w[i], b[j] + c].min\n b[i] = [b[i], w[j] + (i - j) * n - c].min\n end\n end\nend\np [w.last, b.last].min\n\n"}, {"source_code": "n, m, x, y = gets.split.map(&:to_i)\na = Array.new(n){gets.chomp.chars.to_a}\na = a.transpose.map{|_| _.count('#')}\nw = [0] + [n * m] * m\nb = [0] + [n * m] * m\nx.upto(m) do |i|\n c = 0\n (i - 1).downto([i - y, 0].max) do |j|\n c += a[j]\n if i - j >= x\n w[i] = [w[i], b[j] + c].min\n b[i] = [b[i], w[j] + (i - j) * n - c].min\n end\n end\nend\np [w.last, b.last].min\n\n"}, {"source_code": "n, m, x, y = gets.split.map(&:to_i)\na = Array.new(n){gets.chomp.chars.to_a}\na = a.transpose.map{|_| _.count('#')}\nw = [0] + [n * m] * m\nb = [0] + [n * m] * m\nx.upto(m) do |i|\n c = 0\n (i - 1).downto([i - y, 0].max) do |j|\n c += a[j]\n if i - j >= x\n w[i] = [w[i], b[j] + c].min\n b[i] = [b[i], w[j] + (i - j) * n - c].min\n end\n end\nend\np [w.last, b.last].min\n\n"}], "negative_code": [], "src_uid": "08d13e3c71040684d5a056bd1b53ef3b"} {"nl": {"description": "There was a big bank robbery in Tablecity. In order to catch the thief, the President called none other than Albert – Tablecity’s Chief of Police. Albert does not know where the thief is located, but he does know how he moves.Tablecity can be represented as 1000 × 2 grid, where every cell represents one district. Each district has its own unique name “(X, Y)”, where X and Y are the coordinates of the district in the grid. The thief’s movement is as Every hour the thief will leave the district (X, Y) he is currently hiding in, and move to one of the districts: (X - 1, Y), (X + 1, Y), (X - 1, Y - 1), (X - 1, Y + 1), (X + 1, Y - 1), (X + 1, Y + 1) as long as it exists in Tablecity. Below is an example of thief’s possible movements if he is located in district (7,1):Albert has enough people so that every hour he can pick any two districts in Tablecity and fully investigate them, making sure that if the thief is located in one of them, he will get caught. Albert promised the President that the thief will be caught in no more than 2015 hours and needs your help in order to achieve that.", "input_spec": "There is no input for this problem. ", "output_spec": "The first line of output contains integer N – duration of police search in hours. Each of the following N lines contains exactly 4 integers Xi1, Yi1, Xi2, Yi2 separated by spaces, that represent 2 districts (Xi1, Yi1), (Xi2, Yi2) which got investigated during i-th hour. Output is given in chronological order (i-th line contains districts investigated during i-th hour) and should guarantee that the thief is caught in no more than 2015 hours, regardless of thief’s initial position and movement. N ≤ 2015 1 ≤ X ≤ 1000 1 ≤ Y ≤ 2 ", "sample_inputs": ["В этой задаче нет примеров ввода-вывода.\nThis problem doesn't have sample input and output."], "sample_outputs": ["Смотрите замечание ниже.\nSee the note below."], "notes": "NoteLet's consider the following output:25 1 50 28 1 80 2This output is not guaranteed to catch the thief and is not correct. It is given to you only to show the expected output format. There exists a combination of an initial position and a movement strategy such that the police will not catch the thief.Consider the following initial position and thief’s movement:In the first hour, the thief is located in district (1,1). Police officers will search districts (5,1) and (50,2) and will not find him.At the start of the second hour, the thief moves to district (2,2). Police officers will search districts (8,1) and (80,2) and will not find him.Since there is no further investigation by the police, the thief escaped!"}, "positive_code": [{"source_code": "puts 2000;(1..1000).each{|x|puts \"#{x} 1 #{x} 2\"};(1..1000).each{|x|puts \"#{1001-x} 1 #{1001-x} 2\"}"}], "negative_code": [{"source_code": "puts 2001;(1..1000).each{|x|puts \"#{x} 1 #{x} 2\"};puts \"1000 1 1000 2\";(1..1000).each{|x|puts \"#{1001-x} 1 #{1001-x} 2\"}"}], "src_uid": "19190fd248eb2c621ac2c78b803049a8"} {"nl": {"description": "Polycarp is mad about coding, that is why he writes Sveta encoded messages. He calls the median letter in a word the letter which is in the middle of the word. If the word's length is even, the median letter is the left of the two middle letters. In the following examples, the median letter is highlighted: contest, info. If the word consists of single letter, then according to above definition this letter is the median letter. Polycarp encodes each word in the following way: he writes down the median letter of the word, then deletes it and repeats the process until there are no letters left. For example, he encodes the word volga as logva.You are given an encoding s of some word, your task is to decode it. ", "input_spec": "The first line contains a positive integer n (1 ≤ n ≤ 2000) — the length of the encoded word. The second line contains the string s of length n consisting of lowercase English letters — the encoding.", "output_spec": "Print the word that Polycarp encoded.", "sample_inputs": ["5\nlogva", "2\nno", "4\nabba"], "sample_outputs": ["volga", "no", "baba"], "notes": "NoteIn the first example Polycarp encoded the word volga. At first, he wrote down the letter l from the position 3, after that his word looked like voga. After that Polycarp wrote down the letter o from the position 2, his word became vga. Then Polycarp wrote down the letter g which was at the second position, the word became va. Then he wrote down the letter v, then the letter a. Thus, the encoding looked like logva.In the second example Polycarp encoded the word no. He wrote down the letter n, the word became o, and he wrote down the letter o. Thus, in this example, the word and its encoding are the same.In the third example Polycarp encoded the word baba. At first, he wrote down the letter a, which was at the position 2, after that the word looked like bba. Then he wrote down the letter b, which was at the position 2, his word looked like ba. After that he wrote down the letter b, which was at the position 1, the word looked like a, and he wrote down that letter a. Thus, the encoding is abba."}, "positive_code": [{"source_code": "n = gets.chomp.to_i\nword = gets.chomp\nempty = \" \"*n\nmiddle = n%2 == 0 ? (n/2) - 1: n / 2\nleft = middle - 1\nright = middle + 1\nempty[ middle ] = word[0]\n\nif n % 2 == 1\n for i in 1 ... n\n if i % 2 == 1\n empty[ left ] = word[i]\n left -= 1\n else\n empty[ right ] = word[i]\n right += 1\n end\n end\nelse\n for i in 1 ... n\n if i % 2 == 0\n empty[ left ] = word[i]\n left -= 1\n else\n empty[ right ] = word[i]\n right += 1\n end\n end\nend\n\nputs empty\n"}, {"source_code": "n = gets.to_i\ns = gets.chomp.reverse\nx, y = \"\", \"\"\ns.chars.each_with_index do |c, i|\n if i % 2 == 0\n x << c\n else\n y << c\n end\nend\nputs y + x.reverse\n"}, {"source_code": "gets\ns = gets.chomp\n\na = []\ni = 0\nif s.length % 2 != 0\n a.push(s[i])\n i += 1\nend\n\nwhile i < s.length\n a.unshift(s[i])\n a.push(s[i + 1])\n i += 2\nend\n\nputs a.join\n"}, {"source_code": "n = gets.to_i\ns = gets.chomp.chars\n\ni = n\nans = ''\nwhile !s.empty? do\n ss = s.shift\n if i.odd?\n ans += ss\n else\n ans = ss + ans\n end\n i += 1\nend\n\nputs ans\n"}, {"source_code": "def g(ps)\n\tif ps.empty? == true then\n\t\treturn []\n\telse\n\t\tp=ps.pop\n\t\treturn (f(ps).unshift(p))\n\tend\nend\n\ndef f(ps)\n\tif ps.empty? == true then\n\t\treturn []\n\telse\n\t\tp=ps.pop\n\t\treturn (g(ps).push(p))\n\tend\nend\n\nn=gets.to_i\ns=gets.chomp\n\nps=(0..(n-1)).map{|i| i}\nps2=f(ps)\nps2.each{|i|\n\tprint s[i]\n}\nprint \"\\n\""}, {"source_code": "n = gets.to_i\nstr = gets.chomp\nres = Array.new\nif n%2 != 0\n k = i =1\n ctr =1\n res[n/2] = str[0]\n while(k <= n/2)\n if (i == 1)\n res[n/2-k] = str[ctr]\n i = -1\n else\n res[n/2+k] = str[ctr]\n i=1\n end\n ctr +=1\n if(ctr%2 != 0)\n k = k+1\n end\n end\nelse\n k=1\n i=1\n ctr = 0\n while(k <= n/2)\n if (i == 1)\n res[n/2-k] = str[ctr]\n i = -1\n else\n res[n/2+k-1] = str[ctr]\n i=1\n end\n ctr +=1\n if(ctr%2 == 0)\n k = k+1\n end\n end\nend\n\nprint res.join(\"\")\n"}, {"source_code": "n = gets.to_i\nstr = gets.chomp\nres = Array.new\nif n%2 != 0\n k = i =1\n ctr =1\n res[n/2] = str[0]\n while(k <= n/2)\n if (i == 1)\n res[n/2-k] = str[ctr]\n i = -1\n else\n res[n/2+k] = str[ctr]\n i=1\n end\n ctr +=1\n if(ctr%2 != 0)\n k = k+1\n end\n end\nelse\n k=1\n i=1\n ctr = 0\n while(k <= n/2)\n if (i == 1)\n res[n/2-k] = str[ctr]\n i = -1\n else\n res[n/2+k-1] = str[ctr]\n i=1\n end\n ctr +=1\n if(ctr%2 == 0)\n k = k+1\n end\n end\nend\n\nprint res.join(\"\")\n"}, {"source_code": "n=gets.to_i\ns=gets\nr=''\nfor i in 0...n\n c=s[i]\n if (n+i).odd?\n r</) do\n\tt -= 2 if $&['/']\n\ts << ' ' * t + $&\n\tt += 2 unless $&['/']\nend\nputs s * \"\\n\"\n"}, {"source_code": "str=gets\ninput=str[1..str.length-3].split(\"><\")\ndepth=0\ninput.each{|a|\n if a[0]==\"/\" then\n depth-=1\n print \" \"*depth\n else\n print \" \"*depth\n depth+=1 \n end\n print \"<\"+a+\">\\n\"\n}\n"}, {"source_code": "l=0;gets.scan(/.*?>/){v=$&<'<0';v&&l-=2;puts\" \"*l+$&;v||l+=2}\n"}, {"source_code": "l=0;gets.scan(/.*?>/){v=$&<'<0';v&&l-=2;puts\" \"*l+$&;v||l+=2}\n"}, {"source_code": "line = STDIN.gets()\n\ndef rec(arr) \n def _rec(_arr, ret, depth)\n if _arr.empty?\n return ret\n end\n \n e = _arr.shift.shift\n\n if e.start_with?('/') then\n depth -= 1\n ret << ([' '] * depth).join('') + '<' + e + '>'\n return _rec(_arr,ret,depth)\n else\n ret << ([' '] * depth).join('') + '<' + e + '>'\n return _rec(_arr,ret,depth+1)\n end\n\n end\n\n return _rec(arr, [], 0)\nend\n\nrec(line.scan(/<([a-z\\/]+)>/)).each do |l|\n puts l\nend\n"}, {"source_code": "l=0;gets.scan(/.*?>/){v=$&<'<0';v&&l-=2;puts\" \"*l+$&;v||l+=2}"}, {"source_code": "l=0;gets.scan(/.*?>/){v=$&<'<0';v&&l-=2;puts\" \"*l+$&;v||l+=2}\n"}, {"source_code": "l=0;gets.scan(/.*?>/){v=$&<'<0';v&&l-=2;puts\" \"*l+$&;v||l+=2}\n"}, {"source_code": "l=0;gets.scan(/.*?>/){v=$&<'<0';v&&l-=2;puts\" \"*l+$&;v||l+=2}"}, {"source_code": "l=0;gets.scan(/.*?>/){v=$&<'<0';v&&l-=2;puts\" \"*l+$&;v||l+=2}"}, {"source_code": "\nxml = gets.strip!\n\nde = []\nwhile xml.size > 0\n r = xml.index \">\"\n if xml[1] == \"/\"\n puts \" \" * (de.size - 1) * 2 + xml[0..r]\n de.pop\n else\n puts \" \" * (de.size) * 2 + xml[0..r]\n de << 1\n end\n xml = xml[r + 1..-1]\nend\n"}, {"source_code": "str = gets.chomp\nh = []\n\nwhile str.size > 0\n res = str.index \">\"\n if str[1] == \"/\"\n puts \" \" * (h.size - 1) *2 + str[0..res]\n h.pop\n else\n puts \" \" * (h.size) *2 + str[0..res]\n h << 1\n end\n str = str[res + 1..-1]\nend"}, {"source_code": "l=0;gets.scan(/.*?>/){v=$&<'<0';v&&l-=2;puts\" \"*l+$&;v||l+=2}\n"}, {"source_code": "s = gets.strip\ndepth = 0\ns.scan(/<(\\/)?.*?>/) do\n depth -= 1 if $1\n puts \" \"*(2*depth)+$&\n depth += 1 unless $1\nend\n"}, {"source_code": "l=0;gets.scan(/.*?>/){v=$&<'<0';v&&l-=2;puts\" \"*l+$&;v||l+=2}\n"}, {"source_code": "s, d = gets.chomp, 0\nwhile s.length > 0\n s.sub!(/(<.*?>)/, \"\")\n d -= 1 if $+[1] == \"/\"\n puts \" \"*d + $+\n d += 1 if $+[1] != \"/\"\nend"}, {"source_code": "l=0;gets.scan(/.*?>/){v=$&<'<0';v&&l-=2;puts\" \"*l+$&;v||l+=2}\n"}, {"source_code": "l=0\ngets.scan(/<(\\/)?[a-z]+>/){l-=2 if $1;puts \" \"*l+$&;l+=2 unless $1}"}, {"source_code": "l=0;gets.scan(/.*?>/){v=$&<'<0';v&&l-=2;puts\" \"*l+$&;v||l+=2}\n"}], "negative_code": [{"source_code": "\nxml = gets.strip!\n\nde = []\nwhile xml.size > 0\n r = xml.index \">\"\n if xml[1] == \"/\"\n puts \" \" * (de.size - 1) * 2 + xml[0..r]\n de.pop\n else\n puts \" \" * (de.size) * 2 + xml[0..r]\n de << 1\n end\n xml = xml[r + 1..-1]\n puts xml\n puts xml.size\nend\n"}], "src_uid": "45207d5ff60bbb6897feb1d9a3f85a65"} {"nl": {"description": "You are given two integers $$$n$$$ and $$$k$$$. Your task is to find if $$$n$$$ can be represented as a sum of $$$k$$$ distinct positive odd (not divisible by $$$2$$$) integers or not.You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 10^5$$$) — the number of test cases. The next $$$t$$$ lines describe test cases. The only line of the test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le n, k \\le 10^7$$$).", "output_spec": "For each test case, print the answer — \"YES\" (without quotes) if $$$n$$$ can be represented as a sum of $$$k$$$ distinct positive odd (not divisible by $$$2$$$) integers and \"NO\" otherwise.", "sample_inputs": ["6\n3 1\n4 2\n10 3\n10 2\n16 4\n16 5"], "sample_outputs": ["YES\nYES\nNO\nYES\nYES\nNO"], "notes": "NoteIn the first test case, you can represent $$$3$$$ as $$$3$$$.In the second test case, the only way to represent $$$4$$$ is $$$1+3$$$.In the third test case, you cannot represent $$$10$$$ as the sum of three distinct positive odd integers.In the fourth test case, you can represent $$$10$$$ as $$$3+7$$$, for example.In the fifth test case, you can represent $$$16$$$ as $$$1+3+5+7$$$.In the sixth test case, you cannot represent $$$16$$$ as the sum of five distinct positive odd integers."}, "positive_code": [{"source_code": "def main\n t = gets.chomp.to_i\n t.times { solve }\nend\n\ndef solve\n # n = gets.chomp.to_i\n n, k = gets.chomp.split.map(&:to_i)\n # S = gets.chomp.chars\n\n if (k == 1)\n # Here, we should check the oddness of n\n if (n%2 == 1)\n print \"YES\\n\"\n return\n else\n print \"NO\\n\"\n return\n end\n end\n \n # Here, k >= 2\n # minimum k-1 sum is k_sum. [0, k-2]\n k_sum = (k-1)*(k-1)\n remain = n - k_sum\n if (remain % 2 == 1 && remain > (2*k - 3))\n print \"YES\\n\"\n else\n print \"NO\\n\"\n end\nend\n\nmain\n"}, {"source_code": "t = gets.to_i\nt.times do\n n, k = gets.split.map(&:to_i)\n puts (n >= k * k && (n - k) % 2 == 0) ? \"YES\" : \"NO\"\nend\n"}, {"source_code": "lines = []\nfor i in 0...gets.to_i\n\tlines << gets\nend\ndef summable(n, k)\n\treturn false if n - (k ** 2) < 0\n\tif n % 2 != k % 2\n\t\treturn false\n\tend\n\treturn true\nend\ndef problem1(lines)\n\tfor i in lines\n\t\ti = i.split(\" \")\n\t\tsummable(i[0].to_i, i[1].to_i) ? (puts \"YES\") : (puts \"NO\")\n\tend\nend\nproblem1(lines)"}, {"source_code": "1.upto(gets.to_i) do\n n, k = gets.split(' ').map(&:to_i)\n\n puts (n.modulo(2) == k.modulo(2) && n >= k ** 2) ? 'YES' : 'NO'\nend\n"}], "negative_code": [{"source_code": "t = gets.to_i\nt.times do\n n, k = gets.split.map(&:to_i)\n puts (n >= k && (n - k) % 2 == 0) ? \"YES\" : \"NO\"\nend\n"}, {"source_code": "lines = []\nfor i in 0...gets.to_i\n\tlines << gets\nend\ndef summable(n, k)\n\tfirstKSum = k ** 2\n\tif (n - k) % 2 == 1\n\t\treturn false\n\tend\n\treturn true\nend\ndef problem1(lines)\n\tfor i in lines\n\t\ti = i.split(\" \")\n\t\tsummable(i[0].to_i, i[1].to_i) ? (puts \"YES\") : (puts \"NO\")\n\tend\nend\nproblem1(lines)"}, {"source_code": "lines = []\nfor i in 0...gets.to_i\n\tlines << gets\nend\ndef summable(n, k)\n\tif k == 1\n\t\tif n % 2 == 0\n\t\t\treturn false\n\t\tend\n\t\treturn true\n\tend\n\tfor i in 2..k\n\t\treturn true if n % i == k % i\n\tend\n\treturn false\nend\ndef problem1(lines)\n\tfor i in lines\n\t\ti = i.split(\" \")\n\t\tsummable(i[0].to_i, i[1].to_i) ? (puts \"YES\") : (puts \"NO\")\n\tend\nend\nproblem1(lines)"}, {"source_code": "lines = []\nfor i in 0...gets.to_i\n\tlines << gets\nend\ndef summable(n, k)\n\tif n % 2 != k % 2\n\t\treturn false\n\tend\n\treturn true\nend\ndef problem1(lines)\n\tfor i in lines\n\t\ti = i.split(\" \")\n\t\tsummable(i[0].to_i, i[1].to_i) ? (puts \"YES\") : (puts \"NO\")\n\tend\nend\nproblem1(lines)"}], "src_uid": "a4c82fffb31bc7e42870fd84e043e815"} {"nl": {"description": "Every year a race takes place on the motorway between cities A and B. This year Vanya decided to take part in the race and drive his own car that has been around and bears its own noble name — The Huff-puffer.So, Vasya leaves city A on the Huff-puffer, besides, at the very beginning he fills the petrol tank with α liters of petrol (α ≥ 10 is Vanya's favorite number, it is not necessarily integer). Petrol stations are located on the motorway at an interval of 100 kilometers, i.e. the first station is located 100 kilometers away from the city A, the second one is 200 kilometers away from the city A, the third one is 300 kilometers away from the city A and so on. The Huff-puffer spends 10 liters of petrol every 100 kilometers. Vanya checks the petrol tank every time he passes by a petrol station. If the petrol left in the tank is not enough to get to the next station, Vanya fills the tank with α liters of petrol. Otherwise, he doesn't stop at the station and drives on. For example, if α = 43.21, then the car will be fuelled up for the first time at the station number 4, when there'll be 3.21 petrol liters left. After the fuelling up the car will have 46.42 liters. Then Vanya stops at the station number 8 and ends up with 6.42 + 43.21 = 49.63 liters. The next stop is at the station number 12, 9.63 + 43.21 = 52.84. The next stop is at the station number 17 and so on. You won't believe this but the Huff-puffer has been leading in the race! Perhaps it is due to unexpected snow. Perhaps it is due to video cameras that have been installed along the motorway which register speed limit breaking. Perhaps it is due to the fact that Vanya threatened to junk the Huff-puffer unless the car wins. Whatever the reason is, the Huff-puffer is leading, and jealous people together with other contestants wrack their brains trying to think of a way to stop that outrage.One way to do this is to mine the next petrol station where Vanya will stop. Your task is to calculate at which station this will happen and warn Vanya. You don't know the α number, however, you are given the succession of the numbers of the stations where Vanya has stopped. Find the number of the station where the next stop will be.", "input_spec": "The first line contains an integer n (1 ≤ n ≤ 1000) which represents the number of petrol stations where Vanya has stopped. The next line has n space-separated integers which represent the numbers of the stations. The numbers are positive and do not exceed 106, they are given in the increasing order. No two numbers in the succession match. It is guaranteed that there exists at least one number α ≥ 10, to which such a succession of stops corresponds.", "output_spec": "Print in the first line \"unique\" (without quotes) if the answer can be determined uniquely. In the second line print the number of the station where the next stop will take place. If the answer is not unique, print in the first line \"not unique\".", "sample_inputs": ["3\n1 2 4", "2\n1 2"], "sample_outputs": ["unique\n5", "not unique"], "notes": "NoteIn the second example the answer is not unique. For example, if α = 10, we'll have such a sequence as 1, 2, 3, and if α = 14, the sequence will be 1, 2, 4."}, "positive_code": [{"source_code": "gets\nx = gets.split.map &:to_i\nmina = []\nmaxa = []\nx.map.with_index do |o,i|\n mina << Float(o)/(i+1)\n maxa << Float(o+1)/(i+1)-0.00000001\nend\na = ([mina.max,1].max*(x.size+1)).floor\nb = (maxa.min*(x.size+1)).floor\nputs a == b ? \"unique\\n#{a}\" : \"not unique\"\n"}], "negative_code": [], "src_uid": "bfbd7a73e65d240ee7e8c83cc68ca0a1"} {"nl": {"description": "Polycarp wants to train before another programming competition. During the first day of his training he should solve exactly $$$1$$$ problem, during the second day — exactly $$$2$$$ problems, during the third day — exactly $$$3$$$ problems, and so on. During the $$$k$$$-th day he should solve $$$k$$$ problems.Polycarp has a list of $$$n$$$ contests, the $$$i$$$-th contest consists of $$$a_i$$$ problems. During each day Polycarp has to choose exactly one of the contests he didn't solve yet and solve it. He solves exactly $$$k$$$ problems from this contest. Other problems are discarded from it. If there are no contests consisting of at least $$$k$$$ problems that Polycarp didn't solve yet during the $$$k$$$-th day, then Polycarp stops his training.How many days Polycarp can train if he chooses the contests optimally?", "input_spec": "The first line of the input contains one integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) — the number of contests. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 2 \\cdot 10^5$$$) — the number of problems in the $$$i$$$-th contest.", "output_spec": "Print one integer — the maximum number of days Polycarp can train if he chooses the contests optimally.", "sample_inputs": ["4\n3 1 4 1", "3\n1 1 1", "5\n1 1 1 2 2"], "sample_outputs": ["3", "1", "2"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\na = gets.split.map(&:to_i).sort\n\nnow = 1\na.each { |i| now += 1 if i >= now }\nputs now - 1\n"}, {"source_code": "len = gets.to_i\narr = gets.split(' ').map(&:to_i).sort()\nfor x in 1..len\n n = x\n while n > 0 && arr.length > 0\n n -= arr[0]\n arr.shift()\n end\n while arr.length > 0 && arr[0] <= x\n arr.shift()\n end\n if arr.length == 0\n puts \"#{x}\"\n break\n end\nend\nif arr.length > 0\n puts \"#{len}\"\nend"}], "negative_code": [{"source_code": "p`sed 1d`.split.uniq.size"}], "src_uid": "4f02ac641ab112b9d6aee222e1365c09"} {"nl": {"description": "Monocarp plays a computer game (yet again!). This game has a unique trading mechanics.To trade with a character, Monocarp has to choose one of the items he possesses and trade it for some item the other character possesses. Each item has an integer price. If Monocarp's chosen item has price $$$x$$$, then he can trade it for any item (exactly one item) with price not greater than $$$x+k$$$.Monocarp initially has $$$n$$$ items, the price of the $$$i$$$-th item he has is $$$a_i$$$. The character Monocarp is trading with has $$$m$$$ items, the price of the $$$i$$$-th item they have is $$$b_i$$$. Monocarp can trade with this character as many times as he wants (possibly even zero times), each time exchanging one of his items with one of the other character's items according to the aforementioned constraints. Note that if Monocarp gets some item during an exchange, he can trade it for another item (since now the item belongs to him), and vice versa: if Monocarp trades one of his items for another item, he can get his item back by trading something for it.You have to answer $$$q$$$ queries. Each query consists of one integer, which is the value of $$$k$$$, and asks you to calculate the maximum possible total cost of items Monocarp can have after some sequence of trades, assuming that he can trade an item of cost $$$x$$$ for an item of cost not greater than $$$x+k$$$ during each trade. Note that the queries are independent: the trades do not actually occur, Monocarp only wants to calculate the maximum total cost he can get.", "input_spec": "The first line contains three integers $$$n$$$, $$$m$$$ and $$$q$$$ ($$$1 \\le n, m, q \\le 2 \\cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$) — the prices of the items Monocarp has. The third line contains $$$m$$$ integers $$$b_1, b_2, \\dots, b_m$$$ ($$$1 \\le b_i \\le 10^9$$$) — the prices of the items the other character has. The fourth line contains $$$q$$$ integers, where the $$$i$$$-th integer is the value of $$$k$$$ for the $$$i$$$-th query ($$$0 \\le k \\le 10^9$$$).", "output_spec": "For each query, print one integer — the maximum possible total cost of items Monocarp can have after some sequence of trades, given the value of $$$k$$$ from the query.", "sample_inputs": ["3 4 5\n10 30 15\n12 31 14 18\n0 1 2 3 4"], "sample_outputs": ["55\n56\n60\n64\n64"], "notes": null}, "positive_code": [{"source_code": "class Unionfind\r\n attr_accessor(:par, :size)\r\n\r\n def initialize(n)\r\n @par = Array.new(n+1)\r\n (n+1).times do |i|\r\n @par[i] = i\r\n end\r\n @size = Array.new(n+1,1)\r\n end\r\n\r\n def root(x)\r\n while @par[x] != x\r\n x = @par[@par[x]]\r\n @par[x] = @par[@par[x]]\r\n end\r\n x\r\n end\r\n\r\n def unite(x,y)\r\n rx = root(x)\r\n ry = root(y)\r\n if rx != ry\r\n @size[ry] += size(rx)\r\n @par[rx] = ry\r\n end\r\n end\r\n\r\n def same(x,y)\r\n return root(x)==root(y)\r\n end\r\n\r\n def size(x)\r\n if @par[x] == x\r\n @size[x]\r\n else\r\n size(root(par[x]))\r\n end\r\n end\r\n\r\nend\r\n\r\nn,m,Q = gets.split.map(&:to_i)\r\na = gets.split.map(&:to_i).sort\r\nb = gets.split.map(&:to_i).sort\r\nq = gets.split.map(&:to_i)\r\nquery = []\r\nQ.times do |i|\r\n query << [q[i],i]\r\nend\r\nquery.sort_by!{|q,i|q}\r\nhash = Hash.new(-1)\r\ndist = {}\r\ndists = []\r\narr = (a + b).sort\r\nlen = n+m\r\nlen.times do |i|\r\n hash[arr[i]] = i if hash[arr[i]] == -1\r\n if i > 0\r\n d = arr[i] - arr[i-1]\r\n if dist[d] == nil\r\n dist[d] = []\r\n dists << d\r\n end\r\n dist[d] << i-1\r\n end\r\nend\r\ndists_len = dists.size\r\ndists.sort!\r\n\r\nuf = Unionfind.new(len)\r\nhave = Hash.new(0)\r\nstart = Hash.new(-1)\r\nfinish = Hash.new(-1)\r\na.each do |v|\r\n index = hash[v]\r\n have[index] += 1\r\n start[index] = index\r\n finish[index] = index\r\nend\r\nb.each do |v|\r\n index = hash[v]\r\n start[index] = index\r\n finish[index] = index\r\nend\r\nlen.times do |i|\r\n if start[i] == -1\r\n start[i] = i\r\n finish[i] = i\r\n end\r\nend\r\n\r\nsum = a.sum\r\nacm = [0]\r\nlen.times do |i|\r\n acm[i+1] = acm[i] + arr[i]\r\nend\r\n\r\nans = []\r\ni = 0\r\nj = 0\r\nwhile i < Q\r\n k = query[i][0]\r\n while j < dists_len && dists[j] <= k\r\n dist[dists[j]].each do |from|\r\n root_from = uf.root(from)\r\n root_to = uf.root(from+1)\r\n have1 = have[root_from]\r\n have2 = have[root_to]\r\n start1 = start[root_from]\r\n finish1 = finish[root_from]\r\n finish2 = finish[root_to]\r\n uf.unite(from,from+1)\r\n root = uf.root(from)\r\n have[root] = have1+have2\r\n start[root] = start1\r\n finish[root] = finish2\r\n if have1 > 0 && dists[j] != 0\r\n sum -= acm[finish1+1] - acm[finish1+1-have1]\r\n sum -= acm[finish2+1] - acm[finish2+1-have2]\r\n sum += acm[finish2+1] - acm[finish2+1-have1-have2]\r\n end\r\n end\r\n j += 1\r\n end\r\n ans[query[i][1]] = sum\r\n i += 1\r\nend\r\nputs ans"}], "negative_code": [{"source_code": "class Unionfind\r\n attr_accessor(:par, :size)\r\n\r\n def initialize(n)\r\n @par = Array.new(n+1)\r\n (n+1).times do |i|\r\n @par[i] = i\r\n end\r\n @size = Array.new(n+1,1)\r\n end\r\n\r\n def root(x)\r\n while @par[x] != x\r\n x = @par[@par[x]]\r\n @par[x] = @par[@par[x]]\r\n end\r\n x\r\n end\r\n\r\n def unite(x,y)\r\n rx = root(x)\r\n ry = root(y)\r\n if rx != ry\r\n @size[ry] += size(rx)\r\n @par[rx] = ry\r\n end\r\n end\r\n\r\n def same(x,y)\r\n return root(x)==root(y)\r\n end\r\n\r\n def size(x)\r\n if @par[x] == x\r\n @size[x]\r\n else\r\n size(root(par[x]))\r\n end\r\n end\r\n\r\nend\r\n\r\nn,m,Q = gets.split.map(&:to_i)\r\na = gets.split.map(&:to_i).sort\r\nb = gets.split.map(&:to_i).sort\r\nq = gets.split.map(&:to_i)\r\nquery = []\r\nQ.times do |i|\r\n query << [q[i],i]\r\nend\r\nquery.sort_by!{|q,i|q}\r\nhash = Hash.new(-1)\r\ndist = {}\r\ndists = []\r\narr = (a + b).sort\r\nlen = n+m\r\nlen.times do |i|\r\n hash[arr[i]] = i if hash[arr[i]] == -1\r\n if i > 0\r\n d = arr[i] - arr[i-1]\r\n if dist[d] == nil\r\n dist[d] = []\r\n dists << d\r\n end\r\n dist[d] << i-1\r\n end\r\nend\r\ndists_len = dists.size\r\ndists.sort!\r\n\r\nuf = Unionfind.new(len)\r\nhave = Hash.new(0)\r\nstart = Hash.new(-1)\r\nfinish = Hash.new(-1)\r\na.each do |v|\r\n index = hash[v]\r\n have[index] += 1\r\n start[index] = index\r\n finish[index] = index\r\nend\r\nb.each do |v|\r\n index = hash[v]\r\n start[index] = index\r\n finish[index] = index\r\nend\r\n\r\nsum = a.sum\r\nacm = [0]\r\nlen.times do |i|\r\n acm[i+1] = acm[i] + arr[i]\r\nend\r\n\r\nans = []\r\ni = 0\r\nj = 0\r\nwhile i < Q\r\n k = query[i][0]\r\n while j < dists_len && dists[j] <= k\r\n dist[dists[j]].each do |from|\r\n root_from = uf.root(from)\r\n root_to = uf.root(from+1)\r\n have1 = have[root_from]\r\n have2 = have[root_to]\r\n start1 = start[root_from]\r\n finish1 = finish[root_from]\r\n finish2 = finish[root_to]\r\n uf.unite(from,from+1)\r\n root = uf.root(from)\r\n have[root] = have1+have2\r\n start[root] = start1\r\n finish[root] = finish2\r\n if have1 > 0 && dists[j] != 0\r\n sum -= acm[finish1+1] - acm[finish1+1-have1]\r\n sum -= acm[finish2+1] - acm[finish2+1-have2]\r\n sum += acm[finish2+1] - acm[finish2+1-have1-have2]\r\n end\r\n end\r\n j += 1\r\n end\r\n ans[query[i][1]] = sum\r\n i += 1\r\nend\r\nputs ans"}], "src_uid": "43188c8c24eee697bff59b210aa9e3de"} {"nl": {"description": "There is a square painted on a piece of paper, the square's side equals n meters. John Doe draws crosses on the square's perimeter. John paints the first cross in the lower left corner of the square. Then John moves along the square's perimeter in the clockwise direction (first upwards, then to the right, then downwards, then to the left and so on). Every time he walks (n + 1) meters, he draws a cross (see picture for clarifications).John Doe stops only when the lower left corner of the square has two crosses. How many crosses will John draw? The figure shows the order in which John draws crosses for a square with side 4. The lower left square has two crosses. Overall John paints 17 crosses. ", "input_spec": "The first line contains integer t (1 ≤ t ≤ 104) — the number of test cases. The second line contains t space-separated integers ni (1 ≤ ni ≤ 109) — the sides of the square for each test sample.", "output_spec": "For each test sample print on a single line the answer to it, that is, the number of crosses John will draw as he will move along the square of the corresponding size. Print the answers to the samples in the order in which the samples are given in the input. Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. ", "sample_inputs": ["3\n4 8 100"], "sample_outputs": ["17\n33\n401"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\ninput = gets.split.map(&:to_i)\n\nfor i in input\n if i%2 == 0 then puts i*4+1\n else\n if i%4 == 1\n puts i*2+1\n else\n puts i+1\n end\n end\nend\n"}, {"source_code": "def NOD(x, y)\n if x!=0\n return NOD((y%x),x)\n else \n return y\n end\nend\n\ndef NOK(x,y)\n return (x*y)/NOD(x,y)\nend\n\ntotal = gets.chomp.to_i\nline= gets.chomp\nnumbers=line.split(\" \", total)\nnumbers.each do |number|\n number=number.chomp.to_i\n number=number+1\n mod=4*number-4\n puts NOK(mod, number)/number+1\nend\n"}, {"source_code": "\"\"\"(c) gorlum0 [at] gmail.com\"\"\"\n\ndef solve(n)\n g = (4*n).gcd(n+1)\n 4*n / g + 1\nend\n\nt = STDIN.gets.to_i\nns = STDIN.gets.split.map(&:to_i)\nns.each do |n|\n puts solve(n)\nend\n"}, {"source_code": "\"\"\"(c) gorlum0 [at] gmail.com\"\"\"\n\ndef solve(n)\n g = (4*n).gcd(n+1)\n 4*n / g + 1\nend\n\ndef main\nt = gets.to_i\nns = gets.split.map(&:to_i)\nns.each do |n|\n puts solve(n)\nend\nend\n\nmain\n"}, {"source_code": "\"\"\"(c) gorlum0 [at] gmail.com\"\"\"\n\ndef solve(n)\n g = (4*n).gcd(n+1)\n 4*n / g + 1\nend\n\ndef main\n t = gets.to_i\n ns = gets.split.map(&:to_i)\n ns.each do |n|\n puts solve(n)\n end\nend\n\nif __FILE__ == $0\n main\nend\n"}, {"source_code": "\"\"\"(c) gorlum0 [at] gmail.com\"\"\"\n\ndef solve(n)\n g = (4*n).gcd(n+1)\n 4*n / g + 1\nend\n\nt = gets.to_i\nns = gets.split.map(&:to_i)\nns.each do |n|\n puts solve(n)\nend\n"}, {"source_code": "t = gets.to_i\na = gets.split(\" \").map(&:to_i)\na.each do |i|\n j = (i+1).gcd(4*i)\n p (4*i/j)+1\nend\n"}, {"source_code": "t = gets.to_i\narray = gets.split.map(&:to_i)\narray.each do |n|\n if n % 4 == 3\n puts n+1\n elsif n % 4 == 1\n puts 2*n+1\n else\n puts 4*n+1\n end\nend"}, {"source_code": "#!/usr/bin/ruby\n\nt = $stdin.gets.to_i\nns = $stdin.gets.split.map(&:to_i)\nns.each do |n|\n g = (n+1).gcd(4*n)\n puts (4*n/g)+1\nend\n"}, {"source_code": "t=gets.to_i;gets.split.each{|v| v=v.to_i; x=(v.even?)?(v):(((v+1)%4>0)?(v/2):((v+1)/4-1)); p 4*v*(x+1)/(v+1)+1}\n"}, {"source_code": "gets;gets.split.map{ |v| v=v.to_i; if v.even?; 4*v+1; else if (v+1)%4>0; 2*v+2*v/(v+1); else; v+1; end; end; }.map{ |v| p v }\n"}, {"source_code": "gets;gets.split.map{ |v| v=v.to_i; p (v.odd? and (v+1)%4>0)?(2*v+2*v/(v+1)):(((v.even?)?4:1)*v+1) }\n"}, {"source_code": "#!/usr/bin/ruby\n\ndef gcd(a, b)\n\treturn (b != 0) ? gcd(b, a % b) : a\nend\n\ngets\ngets.split.map(&:to_i).each do |n|\n\tputs (n * 4) / gcd(n * 4, n + 1) + 1\nend\n"}, {"source_code": "#!/usr/bin/ruby\n\ndef gcd(a, b)\n\treturn (b != 0) ? gcd(b, a % b) : a\nend\n\ndef solve(n)\n\tputs (n * 4) / gcd(n * 4, n + 1) + 1\nend\n\ngets\ngets.split.map(&:to_i).map{ |n| solve(n) }\n"}, {"source_code": "gets\ngets.split.map { |x| x.to_i } . map { |x| x*4 / (x*4).gcd(x+1) } . each { |x| puts x+1}"}], "negative_code": [{"source_code": "n = gets.to_i\ninput = gets.split.map(&:to_i)\n\nfor i in input\n if i%2 == 0 then puts i*2+1\n else\n if i == 1 \n puts 2\n elsif i%4 == 1\n puts i*2+1\n else\n puts i+1\n end\n end\nend"}, {"source_code": "n = gets.to_i\ninput = gets.split.map(&:to_i)\n\nfor i in input\n if i%2 == 0 then puts i*4+1\n else\n if i == 1 \n puts 2\n elsif i%4 == 1\n puts i*2+1\n else\n puts i+1\n end\n end\nend"}, {"source_code": "t = gets.to_i\narray = gets.split.map(&:to_i)\narray.each do |n|\n a = 4 * n\n b = n + 1\n c = 0\n count = 0\n while c != 0\n c = a - b\n if c < 0\n c = a + c\n end\n count += 1\n end\n puts count\nend"}, {"source_code": "t = gets.to_i\narray = gets.split.map(&:to_i)\narray.each do |n|\n if n == 1\n puts 3\n elsif n == 3\n puts 4\n else\n puts n * 4 + 1\n end\nend"}, {"source_code": "t = gets.to_i\narray = gets.split.map(&:to_i)\narray.each do |n|\n puts n * 4 + 1\nend"}, {"source_code": "t = gets.to_i\narray = gets.split.map(&:to_i)\narray.each do |n|\n if n == 1\n puts 3\n else\n puts n * 4 + 1\n end\nend"}, {"source_code": "t = gets.to_i\narray = gets.split.map(&:to_i)\narray.each do |n|\n if n == 1\n puts 2\n else\n puts n * 4 + 1\n end\nend"}, {"source_code": "t = gets.to_i\narray = gets.split.map(&:to_i)\na = 0\narray.each do |n|\n a += 1\n print n * 4 + 1\n if a != t\n print \"\\n\"\n end\nend"}, {"source_code": "t=gets.to_i;gets.split.each{|v| p (v.to_i*4)+1}\n"}, {"source_code": "t=gets.to_i;gets.split.each{|v| v=v.to_i; p (((v.even?)?(4*v):((v+1)%4>0?(2*v+2*v/(v+1)):v))+1)}\n"}, {"source_code": "gets\ngets.split.map { |x| x.to_i } . map { |x| x*4 / x.gcd(x+1) } . each { |x| puts x+1}"}], "src_uid": "168dbc4994529f5407a440b0c71086da"} {"nl": {"description": "You've got array a[1], a[2], ..., a[n], consisting of n integers. Count the number of ways to split all the elements of the array into three contiguous parts so that the sum of elements in each part is the same. More formally, you need to find the number of such pairs of indices i, j (2 ≤ i ≤ j ≤ n - 1), that .", "input_spec": "The first line contains integer n (1 ≤ n ≤ 5·105), showing how many numbers are in the array. The second line contains n integers a[1], a[2], ..., a[n] (|a[i]| ≤  109) — the elements of array a.", "output_spec": "Print a single integer — the number of ways to split the array into three parts with the same sum.", "sample_inputs": ["5\n1 2 3 0 3", "4\n0 1 -1 0", "2\n4 1"], "sample_outputs": ["2", "1", "0"], "notes": null}, "positive_code": [{"source_code": "# \n# E - Number of Ways\n# \n\nn = gets.to_i\na = gets.split.map(&:to_i)\n\ns = a.reduce(:+)\n\nif (s % 3 != 0)\n puts 0\nelse\n\n count = (1..10000000).map { |i| 0 }\n\n s /= 3\n sub_sum = 0\n\n (n - 1).downto(0) do |i|\n sub_sum += a[i]\n count[i] = 1 if sub_sum == s\n end\n\n (n - 2).downto(0) do |i|\n count[i] += count[i + 1]\n end\n\n answer = 0\n sub_sum = 0\n i = 0\n\n loop do\n break if i + 2 >= n\n\n sub_sum += a[i]\n answer += count[i + 2] if sub_sum == s\n i += 1\n end\n\n puts answer\nend\n"}, {"source_code": "#!/usr/bin/ruby\n\nn = STDIN.readline.to_i\narr = STDIN.readline.split.map {|s| s.to_i }\n\ntotal = arr.reduce(:+)\nif total%3 != 0\n\tputs 0\n\texit\nend\ntarget = total / 3\n\nsums = [0]\nsum = 0\narr.each {|x| sums.push(sum += x) }\n\nlow, result = 0, 0\n(1..n-2).each do |i|\n\tlow += 1 if sums[i] == target\n\tresult += low if total - sums[i+1] == target\nend\nputs result\n"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split.map(&:to_i)\nsum = 0\na.each { |x| sum += x }\nif sum % 3 != 0 \n puts 0\n exit\nend\nsum /= 3\nans, cnt, psum = 0, 0, 0\nfor x in 0...n-1 do\n psum += a[x]\n ans += cnt if psum == 2 * sum\n cnt += 1 if psum == sum\nend\nputs ans\n"}, {"source_code": "n=gets.to_i\nx=gets.split.map{|e| e.to_i}\ns1=x.inject(0){|sum1,e|\n\tsum1+e\n}\nif s1%3!=0 || n<3 then\n\tputs 0\nelse\n\ts3=s1/3\n\tsum=0\n\tarr=[]\n\ti=0\n\tx.each{|e|\n\t\tsum=sum+e\n\t\tif s3==sum then\n\t\t\tarr< 0\n puts ans\nelsif (an[1] < 1 || an[2] < 1) || (goal == 0 && an[1] < 3) || goal * 3 != a_sum[n - 1]\n puts 0\nelsif goal == 0\n puts (an[1] - 1) * (an[1] - 2) / 2\nelse\n puts ans\nend\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\na_sum = [a[0]]\n(n - 1).times.each{|i| a_sum[i + 1] = a_sum[i] + a[i + 1]}\ngoal = a_sum[n - 1] == 0 ? 0 : a_sum[n - 1] / 3\nan = [nil, 0, 0]\nind = 1\nans = 0\nn.times do |i|\n if ind == 1 && a_sum[i] == goal && a_sum[i] == goal * 2\n an[1] += 1\n an[2] += 1\n elsif ind == 1 && a_sum[i] == goal\n an[1] += 1\n elsif ind == 2 && a_sum[i] == goal\n p an\n ans += an[1] * an[2]\n ind = 1\n an[1] = 1\n elsif a_sum[i] == goal * 2\n ind = 2\n an[2] += 1\n end\n if i == n - 1 && ind == 2\n ans += an[1] * an[2]\n end\nend\nif ans > 0\n puts ans\nelsif (an[1] < 1 || an[2] < 1) || (goal == 0 && an[1] < 3) || goal * 3 != a_sum[n - 1]\n puts 0\nelsif goal == 0\n puts (an[1] - 1) * (an[1] - 2) / 2\nelse\n puts ans\nend\n"}], "src_uid": "2558db57229e55ffe0de0d8cf217035b"} {"nl": {"description": "You've got an undirected graph, consisting of n vertices and m edges. We will consider the graph's vertices numbered with integers from 1 to n. Each vertex of the graph has a color. The color of the i-th vertex is an integer ci.Let's consider all vertices of the graph, that are painted some color k. Let's denote a set of such as V(k). Let's denote the value of the neighbouring color diversity for color k as the cardinality of the set Q(k) = {cu :  cu ≠ k and there is vertex v belonging to set V(k) such that nodes v and u are connected by an edge of the graph}.Your task is to find such color k, which makes the cardinality of set Q(k) maximum. In other words, you want to find the color that has the most diverse neighbours. Please note, that you want to find such color k, that the graph has at least one vertex with such color.", "input_spec": "The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of vertices end edges of the graph, correspondingly. The second line contains a sequence of integers c1, c2, ..., cn (1 ≤ ci ≤ 105) — the colors of the graph vertices. The numbers on the line are separated by spaces. Next m lines contain the description of the edges: the i-th line contains two space-separated integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi) — the numbers of the vertices, connected by the i-th edge. It is guaranteed that the given graph has no self-loops or multiple edges.", "output_spec": "Print the number of the color which has the set of neighbours with the maximum cardinality. It there are multiple optimal colors, print the color with the minimum number. Please note, that you want to find such color, that the graph has at least one vertex with such color.", "sample_inputs": ["6 6\n1 1 2 3 5 8\n1 2\n3 2\n1 4\n4 3\n4 5\n4 6", "5 6\n4 2 5 2 4\n1 2\n2 3\n3 1\n5 3\n5 4\n3 4"], "sample_outputs": ["3", "2"], "notes": null}, "positive_code": [{"source_code": "require 'set'\nn,m=gets.split.map(&:to_i)\nid = [0] + gets.split.map(&:to_i);\ninn = [-1]*(1e5.to_i+1)\ns = Set[]\nm.times do |u|\n\ta,b = gets.split.map(&:to_i)\n\tx,y = id[a],id[b]\n\tx,y=y,x if x>y\n\tif x!=y and not s.include? [x,y] then\n\t\ts << [x,y]\n\t\tinn[x]=0 if inn[x]==-1\n\t\tinn[y]=0 if inn[y]==-1\n\t\tinn[x]+=1\n\t\tinn[y]+=1\n\tend\nend\n\nans,j=-1,0\ninn.each_with_index do |v,u|\n\tif ansy\n\tif x!=y and not s.include? [x,y] then\n\t\tinn[x]=0 if inn[x]==-1\n\t\tinn[y]=0 if inn[y]==-1\n\t\tinn[x]+=1\n\t\tinn[y]+=1\n\tend\nend\n\nans,j=-1,0\ninn.each_with_index do |v,u|\n\tif ansy\n\tif x!=y and not s.include? [x,y] then\n\t\tinn[x]=0 if inn[x]==-1\n\t\tinn[y]=0 if inn[y]==-1\n\t\tinn[x]+=1\n\t\tinn[y]+=1\n\tend\nend\n\nans,j=-1,0\ninn.each_with_index do |v,u|\n\tif ans= min\n (0..arr.size-1).each { |k|\n next if i==k || j==k\n sum = arr[i][0] + arr[j][1] + arr[k][2]\n next if sum >= min\n min = sum if sum < min\n }\n }\n }\n return min\nend\n\ndef solve(input, output)\n n, m = parse_int_array(input[0])\n arr = input[1..-1].collect {|str|\n analyze(str)\n }\n p(arr)\n output << findmin(arr).to_s\nend\n\ntests do\n # test cases\n t([\"3 4\",\n\"1**2\",\n\"a3*0\",\n\"c4**\"], \"1\")\n t([\"5 5\",\n\"#*&#*\",\n\"*a1c&\",\n\"&q2w*\",\n\"#a3c#\",\n\"*&#*&\"], \"3\")\nend\n\nputs solve(STDIN.read.split(\"\\n\"), []) unless TEST_MODE\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\na = n.times.map{gets.chomp}\n\naa = Array.new(3){Array.new(n, 0)}\n[/[[:alpha:]]/, /\\d/, /\\W/].each_with_index do |reg, i|\n n.times do |j|\n t1 = a[j] =~ reg\n t2 = (a[j].reverse =~ reg) + 1 if a[j].reverse =~ reg\n aa[i][j] = [t1, t2].min || 1000\n end\nend\n\nmi = 1000\nn.times do |i|\n n.times do |j|\n next if j == i\n n.times do |k|\n next if k == i || k == j\n mi = [aa[0][i] + aa[1][j] + aa[2][k], mi].min\n end\n end\nend\np mi\n"}], "negative_code": [], "src_uid": "b7ff1ded73a9f130312edfe0dafc626d"} {"nl": {"description": "Seryozha has a very changeable character. This time he refused to leave the room to Dima and his girlfriend (her hame is Inna, by the way). However, the two lovebirds can always find a way to communicate. Today they are writing text messages to each other.Dima and Inna are using a secret code in their text messages. When Dima wants to send Inna some sentence, he writes out all words, inserting a heart before each word and after the last word. A heart is a sequence of two characters: the \"less\" characters (<) and the digit three (3). After applying the code, a test message looks like that: <3word1<3word2<3 ... wordn<3.Encoding doesn't end here. Then Dima inserts a random number of small English characters, digits, signs \"more\" and \"less\" into any places of the message.Inna knows Dima perfectly well, so she knows what phrase Dima is going to send her beforehand. Inna has just got a text message. Help her find out if Dima encoded the message correctly. In other words, find out if a text message could have been received by encoding in the manner that is described above.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 105) — the number of words in Dima's message. Next n lines contain non-empty words, one word per line. The words only consist of small English letters. The total length of all words doesn't exceed 105. The last line contains non-empty text message that Inna has got. The number of characters in the text message doesn't exceed 105. A text message can contain only small English letters, digits and signs more and less.", "output_spec": "In a single line, print \"yes\" (without the quotes), if Dima decoded the text message correctly, and \"no\" (without the quotes) otherwise.", "sample_inputs": ["3\ni\nlove\nyou\n<3i<3love<23you<3", "7\ni\nam\nnot\nmain\nin\nthe\nfamily\n<3i<>3am<3the<3<main<3in<3the<3><3family<3"], "sample_outputs": ["yes", "no"], "notes": "NotePlease note that Dima got a good old kick in the pants for the second sample from the statement."}, "positive_code": [{"source_code": "n=gets.to_i\nw=['']\nn.times{w<= b\n print \"no\"\n exit\n end\n end\n j += 1\nend\n\nprint \"yes\"\n"}, {"source_code": "n=gets.to_i\nw=['']\nn.times{w<= ans.size()\n end\n index += 1\n break if index >= ans.size()\nend\nif cnt == 1 then\n puts \"yes\"\nelse\n puts \"no\"\nend"}, {"source_code": "n = gets.to_i\ns = []\nn.times do\n s << gets.chop\nend\nans = gets.chop\npattern = \"<3\"\nn.times do |i|\n pattern = pattern + s[i]\n pattern = pattern + \"<3\"\nend\nlen = pattern.size()\ninsert = false\nindex = 0\nif ans.size() - len != 1 then\n puts \"no\"\n exit\nend\nlen.times do |i|\n if ans[index] != pattern[i] then\n puts \"no\" if insert\n insert = true\n index += 1\n end\n index += 1\nend\nputs \"yes\"\n"}, {"source_code": "n = gets.to_i\ns = []\nn.times do\n s << gets.chop\nend\nans = gets.chop\npattern = \"<3\"\nn.times do |i|\n pattern = pattern + s[i]\n pattern = pattern + \"<3\"\nend\nlen = pattern.size()\ncnt = 0\nindex = 0\nlen.times do |i|\n while pattern[i] != ans[index] do\n index += 1\n cnt += 1\n break if index >= ans.size()\n end\n index += 1\n break if index >= ans.size()\nend\nif cnt == 1 then\n puts \"yes\"\nelse\n puts \"no\"\nend"}, {"source_code": "n = Integer(gets.chomp)\noriginal_text = \"\"\nfor i in 0...n\n original_text += gets.chomp\nend\ntext_received = gets.chomp\npointer = 0\ncount = \"\"\nfor i in 0...original_text.length\n a = original_text[i]\n for j in pointer...text_received.length\n if text_received[j] == a\n pointer = j + 1\n count += a\n break\n end\n end\nend\n\nif count == original_text && text_received.length > original_text.length + 2 * n\n puts \"yes\"\nelse\n puts \"no\"\nend"}, {"source_code": "n = Integer(gets.chomp)\noriginal_text = \"\"\nfor i in 0...n\n original_text += gets.chomp\nend\ntext_received = gets.chomp\npointer = 0\ncount = \"\"\nfor i in 0...original_text.length\n a = original_text[i]\n for j in pointer...text_received.length\n if text_received[j] == a\n pointer = j + 1\n count += a\n break\n end\n end\nend\n\nif count == original_text\n puts \"yes\"\nelse\n puts \"no\"\nend"}, {"source_code": "d = ('<3' + (gets.to_i.times.map{gets.split}).join('<3') + '<3').chars.to_a\nstr = gets.strip.chars.to_a\nindex = 0; get = 0;\n(d.length).times do |i|\n gets = i\n while(d[i] != str[index])\n if index == str.length-1\n break\n end\n index+=1\n end\n\n if index == str.length-1\n break\n end\nend\n(get == d.length-1)? (puts \"yes\") : (puts \"no\")\n"}, {"source_code": "d = ('<3' + (gets.to_i.times.map{gets.split}).join('<3') + '<3').chars.to_a\nstr = gets.strip.chars.to_a\nindex = 0; get = 0;\n(d.length).times do |i|\n get = i\n while(d[i] != str[index])\n if index == str.length-1\n break\n end\n index+=1\n end\n\n if index == str.length-1\n break\n end\nend\n(get == d.length-1)? (puts \"yes\") : (puts \"no\")\n"}, {"source_code": "d = gets.to_i.times.map{gets.split}\nstr = gets.strip.chars.to_a\nans = \"\"\nstr.each{|x| (ans+=x) if (x>='a' && x<='z')}\n(ans == d.join)? (puts \"yes\") : (puts \"no\")"}, {"source_code": "d = ('<3' + (gets.to_i.times.map{gets.split}).join('<3') + '<3').chars.to_a\nstr = gets.strip.chars.to_a\nindex = 0; get = 0;\n(d.length).times do |i|\n\twhile(d[i] != str[index])\n\t\tif index == str.length-1\n\t\t\tbreak\n\t\tend\n\t\tindex+=1\n\tend\n\n\tif index == str.length-1\n\t\tget = i\n\t\tbreak\n\tend\nend\n(get == d.length-1)? (puts \"yes\") : (puts \"no\")\n"}, {"source_code": "n=gets.to_i\nw=[]\nn.times{w<=w.size ? 'yes':'no'"}, {"source_code": "n=gets.to_i\nw=[]\nn.times{w< a[k]\n a.delete(k)\n else\n b.delete(k)\n end\n end\n}\n\nputs a.values.inject(0){|sum,x| sum+x} + b.values.inject(0){|sum,x| sum+x}"}, {"source_code": "# http://codeforces.com/problemset/problem/981/B\na, b = [], []\nn = gets.chomp.to_i\nn.times do\n a << gets.chomp.split.map(&:to_i)\nend\na.sort!\n\nm = gets.chomp.to_i\nm.times do\n b << gets.chomp.split.map(&:to_i)\nend\nb.sort!\n\nsum = 0\nfor i in 0..(n - 1)\n sum += a[i][1]\nend\n\nfor i in 0..(m - 1)\n sum += b[i][1]\nend\n\nrepeated_index = []\ni, j = 0, 0\nwhile i < n && j < m\n if a[i][0] == b[j][0]\n repeated_index << [i, j]\n i += 1\n j += 1\n elsif a[i][0] < b[j][0]\n i += 1\n else\n j += 1\n end\nend\n\nif repeated_index.empty?\n puts sum\nelse\n for i in 0..(repeated_index.length - 1)\n a_repeated_index = repeated_index[i][0]\n b_repeated_index = repeated_index[i][1]\n\n sum -= a[a_repeated_index][1] < b[b_repeated_index][1] ? a[a_repeated_index][1] : b[b_repeated_index][1]\n end\n\n puts sum\nend\n"}], "negative_code": [], "src_uid": "fe5c302d844b0b94d030b180e017b9b2"} {"nl": {"description": "Don't you tell me what you think that I can beIf you say that Arkady is a bit old-fashioned playing checkers, you won't be right. There is also a modern computer game Arkady and his friends are keen on. We won't discuss its rules, the only feature important to this problem is that each player has to pick a distinct hero in the beginning of the game.There are $$$2$$$ teams each having $$$n$$$ players and $$$2n$$$ heroes to distribute between the teams. The teams take turns picking heroes: at first, the first team chooses a hero in its team, after that the second team chooses a hero and so on. Note that after a hero is chosen it becomes unavailable to both teams.The friends estimate the power of the $$$i$$$-th of the heroes as $$$p_i$$$. Each team wants to maximize the total power of its heroes. However, there is one exception: there are $$$m$$$ pairs of heroes that are especially strong against each other, so when any team chooses a hero from such a pair, the other team must choose the other one on its turn. Each hero is in at most one such pair.This is an interactive problem. You are to write a program that will optimally choose the heroes for one team, while the jury's program will play for the other team. Note that the jury's program may behave inefficiently, in this case you have to take the opportunity and still maximize the total power of your team. Formally, if you ever have chance to reach the total power of $$$q$$$ or greater regardless of jury's program choices, you must get $$$q$$$ or greater to pass a test.", "input_spec": "The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n \\le 10^3$$$, $$$0 \\le m \\le n$$$) — the number of players in one team and the number of special pairs of heroes. The second line contains $$$2n$$$ integers $$$p_1, p_2, \\ldots, p_{2n}$$$ ($$$1 \\le p_i \\le 10^3$$$) — the powers of the heroes. Each of the next $$$m$$$ lines contains two integer $$$a$$$ and $$$b$$$ ($$$1 \\le a, b \\le 2n$$$, $$$a \\ne b$$$) — a pair of heroes that are especially strong against each other. It is guaranteed that each hero appears at most once in this list. The next line contains a single integer $$$t$$$ ($$$1 \\le t \\le 2$$$) — the team you are to play for. If $$$t = 1$$$, the first turn is yours, otherwise you have the second turn. Hacks In order to hack, use the format described above with one additional line. In this line output $$$2n$$$ distinct integers from $$$1$$$ to $$$2n$$$ — the priority order for the jury's team. The jury's team will on each turn select the first possible hero from this list. Here possible means that it is not yet taken and does not contradict the rules about special pair of heroes.", "output_spec": null, "sample_inputs": ["3 1\n1 2 3 4 5 6\n2 6\n1\n\n2\n\n4\n\n1", "3 1\n1 2 3 4 5 6\n1 5\n2\n6\n\n1\n\n3"], "sample_outputs": ["6\n\n5\n\n3", "5\n\n4\n\n2"], "notes": "NoteIn the first example the first turn is yours. In example, you choose $$$6$$$, the other team is forced to reply with $$$2$$$. You choose $$$5$$$, the other team chooses $$$4$$$. Finally, you choose $$$3$$$ and the other team choose $$$1$$$.In the second example you have the second turn. The other team chooses $$$6$$$, you choose $$$5$$$, forcing the other team to choose $$$1$$$. Now you choose $$$4$$$, the other team chooses $$$3$$$ and you choose $$$2$$$."}, "positive_code": [{"source_code": "require 'set'\nnm = gets.chomp.split(\" \").map(&:to_i)\npowers = gets.chomp.split(\" \").map(&:to_i)\npairs = []\nn = nm[0]\nm = nm[1]\n\nm.times do\n pairs.push gets.chomp.split(\" \").map(&:to_i)\nend\n\nnopairs = []\n\n\nt = gets.to_i\nforcedmove = false\nselected = Set.new\njustplayedpair = false\ntoplayforced = -1\n\n(2*n).times do\n# puts \"#{t} #{forcedmove} #{toplayforced} #{justplayedpair}\"\n if t==2\n opponentmove = gets.to_i\n if opponentmove==-1\n# powers[nil] =0 \n exit\n end\n selected.add(opponentmove)\n powers[opponentmove-1] = 0\n forcedmove = false\n if not justplayedpair\n toplayforced = -1\n\n pairs.each do |pair|\n if pair.include?(opponentmove)\n forcedmove = true\n toplayforced = (pair - [opponentmove])[0]\n break\n end\n end\n end\n t=1\n\n else\n #your move\n if forcedmove\n toplay = toplayforced\n justplayedpair = false\n else\n #your move and it's free\n played = false\n pairs.each do |pair|\n if not selected.include?(pair[0])\n #play this pair\n toplay = pair.max\n score1 = powers[pair[0]-1]\n score2 = powers[pair[1]-1]\n\n if score1>score2\n toplay = pair[0]\n else\n toplay = pair[1]\n end\n\n played = true\n justplayedpair = true\n end\n end\n\n if not played\n toplay = powers.index(powers.max)+1\n justplayedpair = false\n #put the largest unselected no-pair dude\n end\n end\n\n# puts forcedmove\n# puts played\n puts toplay\n STDOUT.flush\n powers[toplay-1] = 0\n selected.add(toplay)\n t = 2\n end\nend\n\n"}], "negative_code": [{"source_code": "require 'set'\nnm = gets.chomp.split(\" \").map(&:to_i)\npowers = gets.chomp.split(\" \").map(&:to_i)\npairs = []\nn = nm[0]\nm = nm[1]\n\nm.times do\n pairs.push gets.chomp.split(\" \").map(&:to_i)\nend\n\nnopairs = []\n\n\nt = gets.to_i\nforcedmove = false\nselected = Set.new\njustplayedpair = false\ntoplayforced = -1\n\n(2*n).times do\n# puts \"#{t} #{forcedmove} #{toplayforced} #{justplayedpair}\"\n if t==2\n opponentmove = gets.to_i\n if opponentmove==-1\n exit\n end\n selected.add(opponentmove)\n powers[opponentmove-1] = 0\n forcedmove = false\n if not justplayedpair\n toplayforced = -1\n\n pairs.each do |pair|\n if pair.include?(opponentmove)\n forcedmove = true\n toplayforced = (pair - [opponentmove])[0]\n break\n end\n end\n end\n t=1\n\n else\n #your move\n if forcedmove\n toplay = toplayforced\n justplayedpair = false\n else\n #your move and it's free\n played = false\n pairs.each do |pair|\n if not selected.include?(pair[0])\n #play this pair\n toplay = pair.max\n played = true\n justplayedpair = true\n end\n end\n\n if not played\n toplay = powers.index(powers.max)+1\n justplayedpair = false\n #put the largest unselected no-pair dude\n end\n end\n\n# puts forcedmove\n# puts played\n puts toplay\n STDOUT.flush\n powers[toplay-1] = 0\n selected.add(toplay)\n t = 2\n end\nend\n\n"}, {"source_code": "require 'set'\nnm = gets.chomp.split(\" \").map(&:to_i)\npowers = gets.chomp.split(\" \").map(&:to_i)\npairs = []\nn = nm[0]\nm = nm[1]\n\nm.times do\n pairs.push gets.chomp.split(\" \").map(&:to_i)\nend\n\nnopairs = []\n\n\nt = gets.to_i\nforcedmove = false\nselected = Set.new\njustplayedpair = false\n\n(2*n).times do\n if t==2\n opponentmove = gets.to_i\n if opponentmove==-1\n exit\n end\n selected.add(opponentmove)\n powers[opponentmove-1] = 0\n forcedmove = false\n if not justplayedpair\n toplayforced = -1\n\n pairs.each do |pair|\n if pair.include?(opponentmove)\n forcedmove = true\n toplayforced = (pair - [opponentmove])[0]\n end\n end\n end\n t=1\n\n else\n #your move\n if forcedmove\n toplay = toplayforced\n justplayedpair = false\n else\n #your move and it's free\n played = false\n pairs.each do |pair|\n if not selected.include?(pair[0])\n #play this pair\n toplay = pair.max\n played = true\n justplayedpair = true\n end\n end\n\n if not played\n toplay = powers.index(powers.max)+1\n justplayedpair = false\n #put the largest unselected no-pair dude\n end\n end\n\n# puts forcedmove\n# puts played\n puts toplay\n STDOUT.flush\n powers[toplay-1] = 0\n selected.add(toplay)\n t = 2\n end\nend\n"}, {"source_code": "require 'set'\nnm = gets.chomp.split(\" \").map(&:to_i)\npowers = gets.chomp.split(\" \").map(&:to_i)\npairs = []\nn = nm[0]\nm = nm[1]\n\nm.times do\n pairs.push gets.chomp.split(\" \").map(&:to_i)\nend\n\nnopairs = []\n\n\nt = gets.to_i\nforcedmove = false\nselected = Set.new\njustplayedpair = false\ntoplayforced = -1\n\n(2*n).times do\n# puts \"#{t} #{forcedmove} #{toplayforced} #{justplayedpair}\"\n if t==2\n opponentmove = gets.to_i\n if opponentmove==-1\n powers[nil] =0 \n# exit\n end\n selected.add(opponentmove)\n powers[opponentmove-1] = 0\n forcedmove = false\n if not justplayedpair\n toplayforced = -1\n\n pairs.each do |pair|\n if pair.include?(opponentmove)\n forcedmove = true\n toplayforced = (pair - [opponentmove])[0]\n break\n end\n end\n end\n t=1\n\n else\n #your move\n if forcedmove\n toplay = toplayforced\n justplayedpair = false\n else\n #your move and it's free\n played = false\n pairs.each do |pair|\n if not selected.include?(pair[0])\n #play this pair\n toplay = pair.max\n played = true\n justplayedpair = true\n end\n end\n\n if not played\n toplay = powers.index(powers.max)+1\n justplayedpair = false\n #put the largest unselected no-pair dude\n end\n end\n\n# puts forcedmove\n# puts played\n puts toplay\n STDOUT.flush\n powers[toplay-1] = 0\n selected.add(toplay)\n t = 2\n end\nend\n\n"}, {"source_code": "require 'set'\nnm = gets.chomp.split(\" \").map(&:to_i)\npowers = gets.chomp.split(\" \").map(&:to_i)\npairs = []\nn = nm[0]\nm = nm[1]\n\nm.times do\n pairs.push gets.chomp.split(\" \").map(&:to_i)\nend\n\nnopairs = []\n\n\nt = gets.to_i\nforcedmove = false\nselected = Set.new\njustplayedpair = false\n\n(2*n).times do\n if t==2\n opponentmove = gets.to_i\n if opponentmove==-1\n exit\n end\n selected.add(opponentmove)\n powers[opponentmove-1] = 0\n\n if not justplayedpair\n forcedmove = false\n toplayforced = -1\n\n pairs.each do |pair|\n if pair.include?(opponentmove)\n forcedmove = true\n toplayforced = (pair - [opponentmove])[0]\n end\n end\n end\n t=1\n\n else\n #your move\n if forcedmove\n toplay = toplayforced\n justplayedpair = false\n else\n #your move and it's free\n played = false\n pairs.each do |pair|\n if not selected.include?(pair[0])\n #play this pair\n toplay = pair.max\n played = true\n justplayedpair = true\n end\n end\n\n if not played\n toplay = powers.index(powers.max)+1\n justplayedpair = false\n #put the largest unselected no-pair dude\n end\n end\n\n# puts forcedmove\n# puts played\n puts toplay\n STDOUT.flush\n powers[toplay-1] = 0\n selected.add(toplay)\n t = 2\n end\nend\n\n"}, {"source_code": "require 'set'\nnm = gets.chomp.split(\" \").map(&:to_i)\npowers = gets.chomp.split(\" \").map(&:to_i)\npairs = []\nn = nm[0]\nm = nm[1]\n\nm.times do\n pairs.push gets.chomp.split(\" \").map(&:to_i)\nend\n\nnopairs = []\n\n\nt = gets.to_i\nforcedmove = false\nselected = Set.new\njustplayedpair = false\n\n(2*n).times do\n if t==2\n opponentmove = gets.to_i\n if opponentmove==-1\n exit\n end\n selected.add(opponentmove)\n powers[opponentmove-1] = 0\n\n if not justplayedpair\n forcedmove = false\n toplayforced = -1\n\n pairs.each do |pair|\n if pair.include?(opponentmove)\n forcedmove = true\n toplayforced = (pair - [opponentmove])[0]\n end\n end\n end\n t=1\n\n else\n #your move\n if forcedmove\n toplay = toplayforced\n else\n #your move and it's free\n played = false\n pairs.each do |pair|\n if not selected.include?(pair[0])\n #play this pair\n toplay = pair.max\n played = true\n justplayedpair = true\n end\n end\n\n if not played\n toplay = powers.index(powers.max)+1\n #put the largest unselected no-pair dude\n end\n end\n\n# puts forcedmove\n# puts played\n puts toplay\n STDOUT.flush\n powers[toplay-1] = 0\n selected.add(toplay)\n t = 2\n end\nend\n\n"}], "src_uid": "5e07da229bc2762f3a68bc083e34b9a1"} {"nl": {"description": "Four players participate in the playoff tournament. The tournament is held according to the following scheme: the first player will play with the second, and the third player with the fourth, then the winners of the pairs will play in the finals of the tournament.It is known that in a match between two players, the one whose skill is greater will win. The skill of the $$$i$$$-th player is equal to $$$s_i$$$ and all skill levels are pairwise different (i. e. there are no two identical values in the array $$$s$$$).The tournament is called fair if the two players with the highest skills meet in the finals.Determine whether the given tournament is fair.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. A single line of test case contains four integers $$$s_1, s_2, s_3, s_4$$$ ($$$1 \\le s_i \\le 100$$$) — skill of the players. It is guaranteed that all the numbers in the array are different.", "output_spec": "For each testcase, output YES if the tournament is fair, or NO otherwise.", "sample_inputs": ["4\n3 7 9 5\n4 5 6 9\n5 3 8 1\n6 5 3 2"], "sample_outputs": ["YES\nNO\nYES\nNO"], "notes": "NoteConsider the example: in the first test case, players $$$2$$$ and $$$3$$$ with skills $$$7$$$ and $$$9$$$ advance to the finals; in the second test case, players $$$2$$$ and $$$4$$$ with skills $$$5$$$ and $$$9$$$ advance to the finals. The player with skill $$$6$$$ does not advance, but the player with skill $$$5$$$ advances to the finals, so the tournament is not fair; in the third test case, players $$$1$$$ and $$$3$$$ with skills $$$5$$$ and $$$8$$$ advance to the finals; in the fourth test case, players $$$1$$$ and $$$3$$$ with skills $$$6$$$ and $$$3$$$ advance to the finals. The player with skill $$$5$$$ does not advance, but the player with skill $$$3$$$ advances to the finals, so the tournament is not fair. "}, "positive_code": [{"source_code": "t = gets.to_i\r\nt.times{|x|\r\n\ta = gets.split.map &:to_i\r\n\tif a.max(2) == a[0..1].sort.reverse || a.max(2) == a[2..3].sort.reverse then\r\n\t\tputs \"NO\"\r\n\telse\r\n\t\tputs \"YES\"\r\n\tend\r\n}"}, {"source_code": "def solution(arr)\r\n\tm1, m2 = [arr[0], arr[1]].max, [arr[2], arr[3]].max\r\n\t[m1, m2].sort == arr.max(2).sort\r\nend\r\n\r\nt = gets.to_i\r\nt.times do\r\n\tarr = gets.chomp.split(' ').map(&:to_i)\r\n\tputs solution(arr) ? 'Yes' : 'No'\r\nend\r\n"}], "negative_code": [{"source_code": "t = gets.to_i\r\nt.times{|x|\r\n\ta = gets.split.map &:to_i\r\n\tif a.max(2) == a[0..1].sort.reverse || a.max(2) == a[2..3].sort.reverse then\r\n\t\tputs \"YES\"\r\n\telse\r\n\t\tputs \"NO\"\r\n\tend\r\n}"}], "src_uid": "cb24509580ff9b2f1a11103a0e4cdcbd"} {"nl": {"description": "Ivan recently bought a detective book. The book is so interesting that each page of this book introduces some sort of a mystery, which will be explained later. The $$$i$$$-th page contains some mystery that will be explained on page $$$a_i$$$ ($$$a_i \\ge i$$$).Ivan wants to read the whole book. Each day, he reads the first page he didn't read earlier, and continues to read the following pages one by one, until all the mysteries he read about are explained and clear to him (Ivan stops if there does not exist any page $$$i$$$ such that Ivan already has read it, but hasn't read page $$$a_i$$$). After that, he closes the book and continues to read it on the following day from the next page.How many days will it take to read the whole book?", "input_spec": "The first line contains single integer $$$n$$$ ($$$1 \\le n \\le 10^4$$$) — the number of pages in the book. The second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$i \\le a_i \\le n$$$), where $$$a_i$$$ is the number of page which contains the explanation of the mystery on page $$$i$$$.", "output_spec": "Print one integer — the number of days it will take to read the whole book.", "sample_inputs": ["9\n1 3 3 6 7 6 8 8 9"], "sample_outputs": ["4"], "notes": "NoteExplanation of the example test:During the first day Ivan will read only the first page. During the second day Ivan will read pages number $$$2$$$ and $$$3$$$. During the third day — pages $$$4$$$-$$$8$$$. During the fourth (and the last) day Ivan will read remaining page number $$$9$$$."}, "positive_code": [{"source_code": "N = gets.to_i\nA = gets.split.map &:to_i\nans = 0\nam = nil\n(1..N).each{ |i|\n a = A[i-1]\n if !am\n am = a\n ans += 1\n end\n am = a if a > am\n if am <= i\n am = nil\n next\n end\n}\np ans\n"}, {"source_code": "require 'set'\n#if File.exists?(\"input\")\n# $stdin = File.open(\"input\")\n# $stdout = File.open(\"output\",\"w\")\n#end\nn = gets.to_i\narr = gets.chomp.split(\" \").map(&:to_i)\n\ni=0\nans=0\nmySet = Set.new\n\nwhile(i t\n puts \"#{s-1}\" + \" \" + \"#{t}\"\n elsif s < t\n puts \"#{s-1}\" + \" \" + \"#{t}\"\n end\nend"}, {"source_code": "t = gets.to_i\nt.times do\n a, b = gets.split.map(&:to_i)\n puts (a-1).to_s + \" \" + b.to_s\nend"}], "negative_code": [{"source_code": "n = gets.chomp.to_i\nn.times do \n s, t = gets.chomp.split(\" \").map(&:to_i)\n if s == t\n if s == 1\n x = 0\n y = 1\n puts \"#{x}\" + \" \" \"#{y}\"\n else\n puts \"#{s}\" + \" \" + \"#{t}\"\n end\n elsif s > t\n puts \"#{s-t}\" + \" \" + \"#{t}\"\n elsif s < t\n puts \"#{s-1}\" + \" \" + \"#{t-(s-1)}\"\n end\nend"}, {"source_code": "n = gets.chomp.to_i\nn.times do \n s, t = gets.chomp.split(\" \").map(&:to_i)\n if s == t\n puts \"#{s-1}\" + \" \" + \"#{t}\"\n elsif s > t\n puts \"#{s-1}\" + \" \" + \"#{t}\"\n elsif s < t\n puts \"#{s-1}\" + \" \" + \"#{t-1}\"\n end\nend"}, {"source_code": "n = gets.chomp.to_i\nn.times do \n s, t = gets.chomp.split(\" \").map(&:to_i)\n if s == t\n puts \"#{s-1}\" + \" \" + \"#{t}\"\n elsif s > t\n puts \"#{s-t}\" + \" \" + \"#{t}\"\n elsif s < t\n puts \"#{s-1}\" + \" \" + \"#{t-(s-1)}\"\n end\nend"}, {"source_code": "n = gets.chomp.to_i\nn.times do \n s, t = gets.chomp.split(\" \").map(&:to_i)\n if s == t\n if s == 1\n x = 0\n y = 1\n puts \"#{x}\" + \" \" \"#{y}\"\n else\n puts \"#{s}\" + \" \" + \"#{t}\"\n end\n elsif s > t\n puts \"#{s-1}\" + \" \" + \"#{t}\"\n elsif s < t\n puts \"#{s}\" + \" \" + \"#{t-1}\"\n end\nend"}], "src_uid": "09625e65e62fc1c618d12969932508de"} {"nl": {"description": "You are given a string $$$t$$$ consisting of $$$n$$$ lowercase Latin letters and an integer number $$$k$$$.Let's define a substring of some string $$$s$$$ with indices from $$$l$$$ to $$$r$$$ as $$$s[l \\dots r]$$$.Your task is to construct such string $$$s$$$ of minimum possible length that there are exactly $$$k$$$ positions $$$i$$$ such that $$$s[i \\dots i + n - 1] = t$$$. In other words, your task is to construct such string $$$s$$$ of minimum possible length that there are exactly $$$k$$$ substrings of $$$s$$$ equal to $$$t$$$.It is guaranteed that the answer is always unique.", "input_spec": "The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le n, k \\le 50$$$) — the length of the string $$$t$$$ and the number of substrings. The second line of the input contains the string $$$t$$$ consisting of exactly $$$n$$$ lowercase Latin letters.", "output_spec": "Print such string $$$s$$$ of minimum possible length that there are exactly $$$k$$$ substrings of $$$s$$$ equal to $$$t$$$. It is guaranteed that the answer is always unique.", "sample_inputs": ["3 4\naba", "3 2\ncat"], "sample_outputs": ["ababababa", "catcat"], "notes": null}, "positive_code": [{"source_code": "n, k = gets.split.map(&:to_i)\ns = gets.chomp\nm = (n + 1..n * 2).find { |i|\n\tstr = \"?\" * i\n\tstr[...n] = s\n\tstr[-n..] = s\n\tstr[...n] == s\n} - n\nputs s + s[-m..] * (k - 1)"}, {"source_code": "_,k,t=$<.read.split\ni=1;i+=1 while t[i..-1]!=t[0...-i]\nputs t[0,i]*k.to_i+t[i..-1]"}, {"source_code": "N, K = gets.split.map(&:to_i)\ns = gets.chomp\nn = N\n(1...N).each do |i|\n if s[i, N-i] == s[0, N-i]\n n = i\n break\n end\nend\nans = ''\n(K-1).times do\n ans << s[0, n]\nend\nans << s\nputs ans"}, {"source_code": "n,k=gets.strip.split.map(&:to_i)\ns=gets.strip\n\nt=s.dup\nk-=1\n\nj = n-1\nwhile j>0\n break if t[(n-j)...n] == s[0...j]\n j-=1\nend\n\nk.times do\n t << s[j...n]\nend\nputs t\n"}, {"source_code": "N,K = gets.split.map(&:to_i)\nS = gets.chomp\nn = (1..N-1).map{ |k| N-k }.find{ |n| S[0...n] == S[-n..-1] } || 0\nputs S[0...N-n] * (K-1) + S"}, {"source_code": "n, k = gets.split.map(&:to_i)\nt = gets.chop\n\ni = nil\nj = 0\nloop {\n i = j if t[0..j] == t[-1-j..-1]\n j += 1\n break if j >= t.size-1\n}\n\nputs (t.size == 1 || i.nil? ? t * k : t[0..i] + t[i+1..-1] * k)\n"}, {"source_code": "#i_am_fucking_stupid\n\nv = gets.chomp.split(\" \")\na = gets.chomp\n\ns = a.size\n\nm = 0\n\nfor i in 1..s-1\n\ttemp = a[0...i]\n\tif temp == a[-i..-1]\n\t\tif m < temp.size\n\t\t\tm = temp.size\n\t\tend\n\tend\nend\n\nprint a\n(v[1].to_i - 1).times do \n\tprint(a[m..-1])\nend\n"}, {"source_code": "n, k = gets.split().map{ |x| x.to_i }\ns = gets.chomp\nlen = n - 1\nuntil len == 0 do\n if s[0, len] == s[n - len, len]\n break\n end\n len -= 1\nend\nt = s[len, n - len]\n(k - 1).times { s = s + t }\nputs s"}, {"source_code": "def ascan; gets.split.map(&:to_i); end\n\nn,k = ascan\nstr = gets.chomp\n\nif k == 1\n puts str; exit\nelsif n == 1\n puts str*k; exit\nelse\n t = nil\n 1.upto(n-1) do |i|\n if str[i..-1] == str[0..(-1-i)]\n header = str[0...i]\n footer = str[i..-1]\n puts header*k+footer; exit\n end\n end\n puts str*k; exit\nend"}, {"source_code": "nk = gets.chomp.split(\" \").map(&:to_i)\nstr = gets.chomp\nk = nk[1]\n\ndef splitof(str)\n a = str.length-2\n while a >=0\n break if str[0..a]==str[-1-a..-1]\n a-=1\n end\n\n if a==-1\n return [str,\"\"]\n else \n return [str[0..-2-a],str[-1-a..-1]]\n end\nend\n\nasd = splitof(str)\n#p asd\nk.times do\n print(asd[0])\nend\n\nprint(asd[1])\nputs\n"}, {"source_code": "#! /usr/bin/env ruby\n\nn, k=gets.split.map(&:to_i)\ns=gets.chomp\n\ndef c(s, t)\n cnt=0\n (0..(s.size-t.size)).each do |i|\n cnt+=1 if s[i...(i+t.size)]==t\n end\n # print \"#{s} #{t} #{cnt}\\n\"\n return cnt==2\nend\n\nn.times do |i|\n t=s+s[-(i+1)..-1]\n # print \"#{t} #{s} #{s[-(i+1)..-1]}\\n\"\n if c(t, s)\n puts s+s[-(i+1)..-1]*(k-1)\n break\n end\nend\n"}], "negative_code": [{"source_code": "_,k,t=$<.read.split\ni=1;i+=1 if t[i..-1]!=t[0...-i]\nputs t[0,i]*k.to_i+t[i..-1]"}, {"source_code": "n,k=gets.strip.split.map(&:to_i)\ns=gets.strip\n\nt=s.dup\nk-=1\n\nk.times do\n j = 1\n while j= t.size-1\n}\ni = -1 if t[0] != t[-1]\n\nputs (t.size == 1 ? t : i == -1 ? t * k : t[0..i] + t[i+1..-1] * k)\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\nt = gets.chop\n\ni = j = 0\nloop {\n t[0..j] == t[-1-j..-1] ? i = j : nil\n j += 1\n break if j > t.size-1-j\n}\ni = -1 if t[0] != t[-1]\n\nputs (t.size == 1 ? t * k : i == -1 ? t * k : t[0..t.size-i-2] * k + t[t.size-i-1..-1])\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\nt = gets.chop\n\ni = 0\nloop {\n t[0..i] == t[-1-i..-1] ? i += 1 : break\n break if i > t.size-1-i\n}\ni = -1 if t[0] != t[-1]\n\nputs (t.size == 1 ? t * k : i == -1 ? t * k : t[0..t.size-i-1] * k + t[t.size-i..-1])\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\nt = gets.chop\n\ni = j = 0\nloop {\n t[0..j] == t[-1-j..-1] ? i = j : nil\n j += 1\n break if j >= t.size-1\n}\ni = -1 if t[0] != t[-1]\n\nputs (t.size == 1 ? t * k : i == -1 ? t * k : t[0..i] + t[i+1..-1] * k)\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\nt = gets.chop\n\ni = 0\nloop {\n t[0..i] == t[-1-i..-1] ? i += 1 : break\n}\ni = -1 if t[0] != t[-1]\n\nputs (t.size == 1 ? t * k : i == -1 ? t * k : t[0..t.size-i-1] * k + t[t.size-i..-1])\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\nt = gets.chop\n\ni = 0\nloop {\n t[0..i] == t[-1-i..-1] ? i += 1 : break\n}\ni = -1 if t[0] != t[-1]\n\nputs (i == -1 ? t * k : t[0..t.size-i-1] * k + t[t.size-i..-1])\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\nt = gets.chop\n\ni = j = 0\nloop {\n t[0..j] == t[-1-j..-1] ? i = j : nil\n j += 1\n break if j > t.size-1-j\n}\ni = -1 if t[0] != t[-1]\n\nputs (t.size == 1 ? t * k : i == -1 ? t * k : t[0..t.size-i-1] + t[t.size-i..-1] * k)\n"}, {"source_code": "a = gets.chomp.split(\" \")\nst = gets.chomp\n\nj = 0\ni = 1\nwhile i < a[0].to_i\n if(st[i] == st[j])\n j += 1\n else\n if(j > 0)\n i -= 1\n end\n j = 0\n end\n i += 1\nend\n\nprint(st)\n\n(a[1].to_i-1).times do\n print(st[j...a[0].to_i])\nend\n"}, {"source_code": "a = gets.chomp.split(\" \")\nst = gets.chomp\n\nj = 0\ni = 1\nwhile i < a[0].to_i\n if(st[i] == st[j])\n j += 1\n else\n j = 0\n end\n i += 1\nend\n\nprint(st)\n\n(a[1].to_i-1).times do\n print(st[j...a[0].to_i])\nend\n"}, {"source_code": "v = gets.chomp.split(\" \")\na = gets.chomp\n\ns = (a.size / 2).ceil\nm = 0\n\nfor i in 1..s\n\ttemp = a[0...i]\n\tif temp == a[-i..-1]\n\t\tif m < temp.size\n\t\t\tm = temp.size\n\t\tend\n\tend\nend\n\nprint a\n(v[1].to_i - 1).times do \n\tprint(a[m..-1])\nend\n"}], "src_uid": "34dd4c8400ff7a67f9feb1487f2669a6"} {"nl": {"description": "You had $$$n$$$ positive integers $$$a_1, a_2, \\dots, a_n$$$ arranged in a circle. For each pair of neighboring numbers ($$$a_1$$$ and $$$a_2$$$, $$$a_2$$$ and $$$a_3$$$, ..., $$$a_{n - 1}$$$ and $$$a_n$$$, and $$$a_n$$$ and $$$a_1$$$), you wrote down: are the numbers in the pair equal or not.Unfortunately, you've lost a piece of paper with the array $$$a$$$. Moreover, you are afraid that even information about equality of neighboring elements may be inconsistent. So, you are wondering: is there any array $$$a$$$ which is consistent with information you have about equality or non-equality of corresponding pairs?", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains a non-empty string $$$s$$$ consisting of characters E and/or N. The length of $$$s$$$ is equal to the size of array $$$n$$$ and $$$2 \\le n \\le 50$$$. For each $$$i$$$ from $$$1$$$ to $$$n$$$: if $$$s_i =$$$ E then $$$a_i$$$ is equal to $$$a_{i + 1}$$$ ($$$a_n = a_1$$$ for $$$i = n$$$); if $$$s_i =$$$ N then $$$a_i$$$ is not equal to $$$a_{i + 1}$$$ ($$$a_n \\neq a_1$$$ for $$$i = n$$$). ", "output_spec": "For each test case, print YES if it's possible to choose array $$$a$$$ that are consistent with information from $$$s$$$ you know. Otherwise, print NO. It can be proved, that if there exists some array $$$a$$$, then there exists an array $$$a$$$ of positive integers with values less or equal to $$$10^9$$$.", "sample_inputs": ["4\nEEE\nEN\nENNEENE\nNENN"], "sample_outputs": ["YES\nNO\nYES\nYES"], "notes": "NoteIn the first test case, you can choose, for example, $$$a_1 = a_2 = a_3 = 5$$$.In the second test case, there is no array $$$a$$$, since, according to $$$s_1$$$, $$$a_1$$$ is equal to $$$a_2$$$, but, according to $$$s_2$$$, $$$a_2$$$ is not equal to $$$a_1$$$.In the third test case, you can, for example, choose array $$$a = [20, 20, 4, 50, 50, 50, 20]$$$.In the fourth test case, you can, for example, choose $$$a = [1, 3, 3, 7]$$$."}, "positive_code": [{"source_code": "gets.to_i.times{puts gets.count(?N)==1 ? \"NO\" : \"YES\"}"}, {"source_code": "gets.to_i.times{puts gets.count(?N)==1 ? \"NO\":\"YES\"}"}], "negative_code": [{"source_code": "gets.to_i.times{puts gets[/^E+N$/]?\"NO\":\"YES\"}"}], "src_uid": "e744184150bf55a30568060cca69de04"} {"nl": {"description": "You are given a sequence $$$a$$$, initially consisting of $$$n$$$ integers.You want to transform this sequence so that all elements in it are equal (i. e. it contains several occurrences of the same element).To achieve this, you choose some integer $$$x$$$ that occurs at least once in $$$a$$$, and then perform the following operation any number of times (possibly zero): choose some segment $$$[l, r]$$$ of the sequence and remove it. But there is one exception: you are not allowed to choose a segment that contains $$$x$$$. More formally, you choose some contiguous subsequence $$$[a_l, a_{l + 1}, \\dots, a_r]$$$ such that $$$a_i \\ne x$$$ if $$$l \\le i \\le r$$$, and remove it. After removal, the numbering of elements to the right of the removed segment changes: the element that was the $$$(r+1)$$$-th is now $$$l$$$-th, the element that was $$$(r+2)$$$-th is now $$$(l+1)$$$-th, and so on (i. e. the remaining sequence just collapses).Note that you can not change $$$x$$$ after you chose it.For example, suppose $$$n = 6$$$, $$$a = [1, 3, 2, 4, 1, 2]$$$. Then one of the ways to transform it in two operations is to choose $$$x = 1$$$, then: choose $$$l = 2$$$, $$$r = 4$$$, so the resulting sequence is $$$a = [1, 1, 2]$$$; choose $$$l = 3$$$, $$$r = 3$$$, so the resulting sequence is $$$a = [1, 1]$$$. Note that choosing $$$x$$$ is not an operation. Also, note that you can not remove any occurrence of $$$x$$$.Your task is to find the minimum number of operations required to transform the sequence in a way described above.You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 2 \\cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 \\cdot 10^5$$$ ($$$\\sum n \\le 2 \\cdot 10^5$$$).", "output_spec": "For each test case, print the answer — the minimum number of operations required to transform the given sequence in a way described in the problem statement. It can be proven that it is always possible to perform a finite sequence of operations so the sequence is transformed in the required way.", "sample_inputs": ["5\n3\n1 1 1\n5\n1 2 3 4 5\n5\n1 2 3 2 1\n7\n1 2 3 1 2 3 1\n11\n2 2 1 2 3 2 1 2 3 1 2"], "sample_outputs": ["0\n1\n1\n2\n3"], "notes": null}, "positive_code": [{"source_code": "gets;$<.map{a=gets.split.map(&:to_i).chunk{|x|x}.map{|x|x[0]};b=a[1..-2].tally;p a.uniq.size<2?0:1+a.map{|x|b[x]||0}.min}"}, {"source_code": "t = gets.chomp.to_i\nt.times do\n n = gets.chomp.to_i\n arr = gets.chomp.split(' ').map(&:to_i)\n dp = Hash.new {|_| [-1]}\n arr.each_with_index do |el, ind|\n if dp.has_key?(el)\n dp[el] << ind\n else\n dp[el] = dp[el] + [ind]\n end\n #dp[el] = dp[el] + [ind]\n end\n mn = 10 ** 10\n mn_v = -1\n dp.each do |k, v|\n tmp = 0\n st = v[0]\n (v[1..] + [n]).each do |el|\n if (el - st > 1)\n tmp += 1\n end\n st = el\n end\n mn = [mn, tmp].min\n end\n puts mn\nend"}, {"source_code": "t = gets.to_i\n\n1.upto t do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n new = a.chunk(&:itself).map(&:first)\n h = new.tally\n h[new[0]] -= 1\n h[new[-1]] -= 1\n puts h.values.min + 1\nend\n"}, {"source_code": "gets;$<.map{a=gets.split.map(&:to_i).chunk{|x|x}.map{|x|x[0]};b=a[1..-2].tally;p a.uniq.size<2?0:1+a.map{|x|b[x]||0}.min}"}], "negative_code": [{"source_code": "t = gets.chomp.to_i\nt.times do\n n = gets.chomp.to_i\n arr = gets.chomp.split(' ').map(&:to_i)\n dp = Hash.new {|_| [-1]}\n arr.each_with_index do |el, ind|\n if dp.has_key?(el)\n dp[el] << ind\n else\n dp[el] = dp[el]\n end\n end\n mn = 10 ** 10\n mn_v = -1\n dp.each do |k, v|\n tmp = 0\n st = v[0]\n (v[1..] + [n]).each do |el|\n if (el - st > 1)\n tmp += 1\n end\n st = el\n end\n mn = [mn, tmp].min\n end\n puts mn\nend"}], "src_uid": "080eb61cbc4b0ffcbab10b86918f70fe"} {"nl": {"description": "Students of group 199 have written their lectures dismally. Now an exam on Mathematical Analysis is approaching and something has to be done asap (that is, quickly). Let's number the students of the group from 1 to n. Each student i (1 ≤ i ≤ n) has a best friend p[i] (1 ≤ p[i] ≤ n). In fact, each student is a best friend of exactly one student. In other words, all p[i] are different. It is possible that the group also has some really \"special individuals\" for who i = p[i].Each student wrote exactly one notebook of lecture notes. We know that the students agreed to act by the following algorithm: on the first day of revising each student studies his own Mathematical Analysis notes, in the morning of each following day each student gives the notebook to his best friend and takes a notebook from the student who calls him the best friend. Thus, on the second day the student p[i] (1 ≤ i ≤ n) studies the i-th student's notes, on the third day the notes go to student p[p[i]] and so on. Due to some characteristics of the boys' friendship (see paragraph 1), each day each student has exactly one notebook to study.You are given two sequences that describe the situation on the third and fourth days of revising: a1, a2, ..., an, where ai means the student who gets the i-th student's notebook on the third day of revising; b1, b2, ..., bn, where bi means the student who gets the i-th student's notebook on the fourth day of revising. You do not know array p, that is you do not know who is the best friend to who. Write a program that finds p by the given sequences a and b.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 105) — the number of students in the group. The second line contains sequence of different integers a1, a2, ..., an (1 ≤ ai ≤ n). The third line contains the sequence of different integers b1, b2, ..., bn (1 ≤ bi ≤ n).", "output_spec": "Print sequence n of different integers p[1], p[2], ..., p[n] (1 ≤ p[i] ≤ n). It is guaranteed that the solution exists and that it is unique.", "sample_inputs": ["4\n2 1 4 3\n3 4 2 1", "5\n5 2 3 1 4\n1 3 2 4 5", "2\n1 2\n2 1"], "sample_outputs": ["4 3 1 2", "4 3 2 5 1", "2 1"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\na = gets.split.map &:to_i\nb = gets.split.map &:to_i\nd = Array.new(n + 1)\na.each_with_index { |x, i| d[x] = i + 1 }\nc = b.each.map { |x| d[x] }\nputs c\n"}, {"source_code": "n = gets.to_i\na = gets.split.map &:to_i\nb = gets.split.map &:to_i\nd = []\na.each_with_index { |x, i| d[x] = i + 1 }\nc = b.each.map { |x| d[x] }\nputs c\n"}, {"source_code": "gets\nd = []\ngets.split.each_with_index { |x, i| d[x.to_i] = i + 1 }\nputs gets.split.each.map { |x| d[x.to_i] }\n"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split(/ /).map!{|x| x.to_i}\nb = gets.chomp.split(/ /).map!{|x| x.to_i}\nab = []\nfor i in 0..n-1\n ab << [a[i], b[i]]\nend\nab.sort!{|ab1,ab2|\n ab1[0] <=> ab2[0]\n}\nret = []\nfor i in 0..n-1\n ret << ab[i][1]\nend\nputs ret.join(\" \")\n"}, {"source_code": "gets\nd = []\ngets.split.each_with_index { |x, i| d[x.to_i] = i + 1 }\nputs gets.split.each.map { |x| d[x.to_i] }\n\n"}, {"source_code": "gets\nd = []\ngets.split.each_with_index { |x, i| d[x.to_i] = i + 1 }\nputs gets.split.each.map { |x| d[x.to_i] }\n\n"}, {"source_code": "gets\nd = []\ngets.split.each_with_index { |x, i| d[x.to_i] = i + 1 }\nputs gets.split.each.map { |x| d[x.to_i] }\n\n"}, {"source_code": "gets\nd = []\ngets.split.each_with_index { |x, i| d[x.to_i] = i + 1 }\nputs gets.split.each.map { |x| d[x.to_i] }\n\n"}, {"source_code": "gets\nd = []\ngets.split.each_with_index { |x, i| d[x.to_i] = i + 1 }\nputs gets.split.each.map { |x| d[x.to_i] }\n\n"}, {"source_code": "gets\nd = []\ngets.split.each_with_index { |x, i| d[x.to_i] = i + 1 }\nputs gets.split.each.map { |x| d[x.to_i] }\n\n"}, {"source_code": "n = gets.to_i\na, b = [0, 1].map { gets.split.map(&:to_i) }\np = []\nn.times do |i|\n p[a[i] - 1] = b[i]\nend\nputs p.join(' ')\n"}, {"source_code": "gets\nd = []\ngets.split.each_with_index { |x, i| d[x.to_i] = i + 1 }\nputs gets.split.each.map { |x| d[x.to_i] }\n\n"}, {"source_code": "gets\nd = []\ngets.split.each_with_index { |x, i| d[x.to_i] = i + 1 }\nputs gets.split.each.map { |x| d[x.to_i] }\n\n"}], "negative_code": [], "src_uid": "a133b2c63e1025bdf13d912497f9b6a4"} {"nl": {"description": "There is an array $$$a$$$ with $$$n-1$$$ integers. Let $$$x$$$ be the bitwise XOR of all elements of the array. The number $$$x$$$ is added to the end of the array $$$a$$$ (now it has length $$$n$$$), and then the elements are shuffled.You are given the newly formed array $$$a$$$. What is $$$x$$$? If there are multiple possible values of $$$x$$$, you can output any of them.", "input_spec": "The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \\leq t \\leq 1000$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$2 \\leq n \\leq 100$$$) — the number of integers in the resulting array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$0 \\le a_i \\le 127$$$) — the elements of the newly formed array $$$a$$$. Additional constraint on the input: the array $$$a$$$ is made by the process described in the statement; that is, some value of $$$x$$$ exists.", "output_spec": "For each test case, output a single integer — the value of $$$x$$$, as described in the statement. If there are multiple possible values of $$$x$$$, output any of them.", "sample_inputs": ["4\n\n4\n\n4 3 2 5\n\n5\n\n6 1 10 7 10\n\n6\n\n6 6 6 6 6 6\n\n3\n\n100 100 0"], "sample_outputs": ["3\n7\n6\n0"], "notes": "NoteIn the first test case, one possible array $$$a$$$ is $$$a=[2, 5, 4]$$$. Then $$$x = 2 \\oplus 5 \\oplus 4 = 3$$$ ($$$\\oplus$$$ denotes the bitwise XOR), so the new array is $$$[2, 5, 4, 3]$$$. Afterwards, the array is shuffled to form $$$[4, 3, 2, 5]$$$.In the second test case, one possible array $$$a$$$ is $$$a=[1, 10, 6, 10]$$$. Then $$$x = 1 \\oplus 10 \\oplus 6 \\oplus 10 = 7$$$, so the new array is $$$[1, 10, 6, 10, 7]$$$. Afterwards, the array is shuffled to form $$$[6, 1, 10, 7, 10]$$$.In the third test case, all elements of the array are equal to $$$6$$$, so $$$x=6$$$.In the fourth test case, one possible array $$$a$$$ is $$$a=[100, 100]$$$. Then $$$x = 100 \\oplus 100 = 0$$$, so the new array is $$$[100, 100, 0]$$$. Afterwards, the array is shuffled to form $$$[100, 100, 0]$$$. (Note that after the shuffle, the array can remain the same.)"}, "positive_code": [{"source_code": "(1..gets.to_i).map{gets\r\nputs gets.split[0]}"}, {"source_code": "(1..gets.to_i).map{\r\n gets \r\n puts gets.split[0]}\r\n"}, {"source_code": "(1..gets.to_i).map{\r\n gets \r\n puts gets.sub(/\\s.*$/,\"\") }"}, {"source_code": "(1..gets.to_i).map{\r\n gets \r\n puts gets.split.map(&:to_i)[0] }"}, {"source_code": "gets.to_i.times do\r\n gets\r\n puts gets.split.map(&:to_i)[0]\r\nend"}, {"source_code": "gets.to_i.times do\r\n gets.to_i\r\n puts gets.split.map(&:to_i)[0]\r\nend"}, {"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n a = gets.split.map(&:to_i)\r\n n.times do |i|\r\n x = 0\r\n n.times do |j|\r\n x ^= a[j] if j != i\r\n end\r\n if (a[i] == x)\r\n puts a[i]\r\n break\r\n end\r\n end\r\nend"}, {"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n a = gets.split.map(&:to_i)\r\n for i in 0..(n - 1)\r\n x = 0\r\n for j in 0..(n - 1)\r\n x ^= a[j] if j != i\r\n end\r\n if (a[i] == x)\r\n puts a[i]\r\n break\r\n end\r\n end\r\nend"}, {"source_code": "# gets.to_i\r\n# gets.split.map(&:to_i)\r\n\r\ngets.to_i.times do\r\n n = gets.to_i\r\n a = gets.split.map(&:to_i)\r\n n.times do |i|\r\n now = a[i]\r\n sum = 0\r\n n.times do |j|\r\n next if i == j\r\n sum ^= a[j]\r\n end\r\n if now == sum\r\n puts now\r\n break\r\n end\r\n end\r\nend\r\n"}], "negative_code": [], "src_uid": "329ac6671e26b73ab70749ca509f5b09"} {"nl": {"description": "One not particularly beautiful evening Valera got very bored. To amuse himself a little bit, he found the following game.He took a checkered white square piece of paper, consisting of n × n cells. After that, he started to paint the white cells black one after the other. In total he painted m different cells on the piece of paper. Since Valera was keen on everything square, he wondered, how many moves (i.e. times the boy paints a square black) he should make till a black square with side 3 can be found on the piece of paper. But Valera does not know the answer to this question, so he asks you to help him.Your task is to find the minimum number of moves, till the checkered piece of paper has at least one black square with side of 3. Otherwise determine that such move does not exist.", "input_spec": "The first line contains two integers n and m (1 ≤ n ≤ 1000, 1 ≤ m ≤ min(n·n, 105)) — the size of the squared piece of paper and the number of moves, correspondingly. Then, m lines contain the description of the moves. The i-th line contains two integers xi, yi (1 ≤ xi, yi ≤ n) — the number of row and column of the square that gets painted on the i-th move. All numbers on the lines are separated by single spaces. It is guaranteed that all moves are different. The moves are numbered starting from 1 in the order, in which they are given in the input. The columns of the squared piece of paper are numbered starting from 1, from the left to the right. The rows of the squared piece of paper are numbered starting from 1, from top to bottom.", "output_spec": "On a single line print the answer to the problem — the minimum number of the move after which the piece of paper has a black square with side 3. If no such move exists, print -1.", "sample_inputs": ["4 11\n1 1\n1 2\n1 3\n2 2\n2 3\n1 4\n2 4\n3 4\n3 2\n3 3\n4 1", "4 12\n1 1\n1 2\n1 3\n2 2\n2 3\n1 4\n2 4\n3 4\n3 2\n4 2\n4 1\n3 1"], "sample_outputs": ["10", "-1"], "notes": null}, "positive_code": [{"source_code": "n,m = gets.split.map(&:to_i)\nbins = Array.new(n+3){Array.new(n+3){0}}\np [*1..m].find{\n x,y = gets.split.map(&:to_i)\n (y..y+2).any?{ |i|\n (x..x+2).any?{ |j|\n 9 == bins[i][j] += 1\n }\n }\n} || -1"}, {"source_code": "n,m = gets.split.map(&:to_i)\nbins = Array.new(n){Array.new(n){0}}\np [*1..m].find{ |o|\n x,y = gets.split.map(&:to_i)\n y-=1; x-=1\n !->{\n if y>=2\n return if 9 == bins[y-2][x-2] += 1 if x>=2\n return if 9 == bins[y-2][x-1] += 1 if x>=1\n return if 9 == bins[y-2][x] += 1\n end\n if y>=1\n return if 9 == bins[y-1][x-2] += 1 if x>=2\n return if 9 == bins[y-1][x-1] += 1 if x>=1\n return if 9 == bins[y-1][x] += 1\n end\n return if 9 == bins[y][x-2] += 1 if x>=2\n return if 9 == bins[y][x-1] += 1 if x>=1\n return if 9 == bins[y][x] += 1\n true\n }[]\n} || -1"}, {"source_code": "n,m = gets.split.map(&:to_i)\nbins = Array.new(n+3){Array.new(n+3){0}}\np [*1..m].find{\n x,y = gets.split.map(&:to_i)\n (y..y+2).any?{ |i|\n (x..x+2).any?{ |j|\n 9 == bins[i][j] += 1\n }\n }\n} || -1\n"}, {"source_code": "n,m = gets.split.map(&:to_i)\nbins = Array.new(n+3){Array.new(n+3){0}}\np [*1..m].find{\n x,y = gets.split.map(&:to_i)\n (y..y+2).any?{ |i|\n (x..x+2).any?{ |j|\n 9 == bins[i][j] += 1\n }\n }\n} || -1\n"}, {"source_code": "n, m = STDIN.readline.split.map {|s| s.to_i }\nlines = STDIN.readlines\ncount = n.times.map { n.times.map { 0 } }\ndr = [-1, -1, -1, 0, 0, 0, 1, 1, 1]\ndc = [-1, 0, 1, -1, 0, 1, -1, 0, 1]\nlines.each_with_index do |line, i|\n\tx, y = line.split.map {|s| s.to_i-1 }\n\t9.times do |j|\n\t\tr = x+dr[j]\n\t\tnext if r == -1 or r == n\n\t\tc = y+dc[j]\n\t\tnext if c == -1 or c == n\n\t\tcount[r][c] += 1\n\t\tif count[r][c] == 9\n\t\t\tputs i+1\n\t\t\texit\n\t\tend\n\tend\nend\nputs -1\n"}, {"source_code": "n, m = STDIN.readline.split.map {|s| s.to_i }\ncount = n.times.map { n.times.map { 0 } }\nresult = -1\ndr = [-1, -1, -1, 0, 0, 0, 1, 1, 1]\ndc = [-1, 0, 1, -1, 0, 1, -1, 0, 1]\nm.times do |i|\n\tx, y = STDIN.readline.split.map {|s| s.to_i-1 }\n\tnext if result != -1\n\t9.times do |j|\n\t\tr = x+dr[j]\n\t\tnext if r == -1 or r == n\n\t\tc = y+dc[j]\n\t\tnext if c == -1 or c == n\n\t\tcount[r][c] += 1\n\t\tif count[r][c] == 9\n\t\t\tresult = i+1\n\t\tend\n\tend\nend\nputs result\n"}, {"source_code": "n,m = gets.split.map(&:to_i)\nbins = Array.new(n+3){Array.new(n+3){0}}\np [*1..m].find{\n x,y = gets.split.map(&:to_i)\n (y..y+2).any?{ |i|\n (x..x+2).any?{ |j|\n 9 == bins[i][j] += 1\n }\n }\n} || -1"}, {"source_code": "n,m = gets.split.map(&:to_i)\nbins = Array.new(n+3){Array.new(n+3){0}}\np [*1..m].find{\n x,y = gets.split.map(&:to_i)\n (y..y+2).any?{ |i|\n (x..x+2).any?{ |j|\n 9 == bins[i][j] += 1\n }\n }\n} || -1\n"}, {"source_code": "n,m = gets.split.map(&:to_i)\nbins = Array.new(n+3){Array.new(n+3){0}}\np [*1..m].find{\n x,y = gets.split.map(&:to_i)\n (y..y+2).any?{ |i|\n (x..x+2).any?{ |j|\n 9 == bins[i][j] += 1\n }\n }\n} || -1\n"}, {"source_code": "n,m = gets.split.map(&:to_i)\narray = Array.new(n){ Array.new(n) }\n\nm.times do |z|\n x,y = gets.split.map(&:to_i)\n x -= 1\n y -= 1\n array[x][y] = true\n -1.upto(1) do |i|\n if x+i <0 || x+i >n-1\n next\n end\n -1.upto(1) do |j|\n if y+j <0 || y+j >n-1\n next\n end\n if(!array[x+i][y+j])\n next\n end\n black = 0\n catch(:exit) {\n -1.upto(1) do |k|\n if x+i+k < 0 || x+i+k > n-1\n next\n end\n -1.upto(1) do |l|\n if y+j+l < 0 || y+j+l > n-1\n next\n end\n if(array[x+i+k][y+j+l])\n black += 1\n else\n throw :exit\n end\n end\n end\n if black == 9\n puts z+1\n exit\n end\n }\n end\n end\nend\nputs -1"}, {"source_code": "n,m = gets.split.map(&:to_i)\nbins = Array.new(n+3){Array.new(n+3){0}}\np [*1..m].find{\n x,y = gets.split.map(&:to_i)\n (y..y+2).any?{ |i|\n (x..x+2).any?{ |j|\n 9 == bins[i][j] += 1\n }\n }\n} || -1"}, {"source_code": "n,m = gets.split.map(&:to_i)\nbins = Array.new(n+3){Array.new(n+3){0}}\np [*1..m].find{\n x,y = gets.split.map(&:to_i)\n (y..y+2).any?{ |i|\n (x..x+2).any?{ |j|\n 9 == bins[i][j] += 1\n }\n }\n} || -1\n"}, {"source_code": "n,m = gets.split.map(&:to_i)\nbins = Array.new(n+3){Array.new(n+3){0}}\np [*1..m].find{\n x,y = gets.split.map(&:to_i)\n (y..y+2).any?{ |i|\n (x..x+2).any?{ |j|\n 9 == bins[i][j] += 1\n }\n }\n} || -1\n"}, {"source_code": "n,m = gets.split.map(&:to_i)\nbins = Array.new(n+3){Array.new(n+3){0}}\np [*1..m].find{\n x,y = gets.split.map(&:to_i)\n (y..y+2).any?{ |i|\n (x..x+2).any?{ |j|\n 9 == bins[i][j] += 1\n }\n }\n} || -1\n"}, {"source_code": "n,m = gets.split.map(&:to_i)\nbins = Array.new(n+3){Array.new(n+3){0}}\np [*1..m].find{\n x,y = gets.split.map(&:to_i)\n (y..y+2).any?{ |i|\n (x..x+2).any?{ |j|\n 9 == bins[i][j] += 1\n }\n }\n} || -1\n"}], "negative_code": [{"source_code": "n,m = gets.split.map(&:to_i)\nbins = Array.new(n-2){Array.new(n-2){0}}\n\np [*1..m].find{ |o|\n x,y = gets.split.map(&:to_i)\n ([*y-2..y]&[*0..n-3]).product(\n [*x-2..x]&[*0..n-3]).any?{ |i,j|\n 9 == bins[i][j] += 1\n }\n} || -1"}, {"source_code": "n,m = gets.split.map(&:to_i)\na = Array.new(n){[]}\n\np [*1..m].find{ |o|\n y,x = gets.split.map(&:to_i)\n a[y -= 1][x -= 1] = true\n ([*x-2..x]&[*0..n-3]).product(\n [*y-2..y]&[*0..n-3]).any?{ |t,l|\n [*t..t+2].product([*l..l+2]).\n all?{ |i,j| a[i][j] }\n }\n} || -1"}, {"source_code": "n,m = gets.split.map(&:to_i)\narray = Array.new(n){ Array.new(n) }\n\nm.times do |i|\n x,y = gets.split.map(&:to_i)\n x -= 1\n y -= 1\n array[x][y] = true\n -1.upto(1) do |i|\n if x+i <0 || x+i >n-1\n next\n end\n -1.upto(1) do |j|\n if y+j <0 || y+j >n-1\n next\n end\n if(!array[x+i][y+j])\n next\n end\n black = 0\n -1.upto(1) do |k|\n if x+i+k < 0 || x+i+k > n-1\n next\n end\n -1.upto(1) do |l|\n if y+j+l < 0 || y+j+l > n-1\n next\n end\n if(array[x+i+k][y+j+l])\n black += 1\n else\n break\n end\n end\n end\n if black == 9\n puts m-1\n exit\n end\n end\n end\nend\nputs -1"}], "src_uid": "8a4a46710104de78bdf3b9d5462f12bf"} {"nl": {"description": "A magic island Geraldion, where Gerald lives, has its own currency system. It uses banknotes of several values. But the problem is, the system is not perfect and sometimes it happens that Geraldionians cannot express a certain sum of money with any set of banknotes. Of course, they can use any number of banknotes of each value. Such sum is called unfortunate. Gerald wondered: what is the minimum unfortunate sum?", "input_spec": "The first line contains number n (1 ≤ n ≤ 1000) — the number of values of the banknotes that used in Geraldion. The second line contains n distinct space-separated numbers a1, a2, ..., an (1 ≤ ai ≤ 106) — the values of the banknotes.", "output_spec": "Print a single line — the minimum unfortunate sum. If there are no unfortunate sums, print  - 1.", "sample_inputs": ["5\n1 2 3 4 5"], "sample_outputs": ["-1"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/ruby\ngets\nm=gets.split.map(&:to_i).min-1\np m==0 ? -1 : 1"}, {"source_code": "gets\nz = gets.split.map(&:to_i).min - 1\nif z==0\n puts -1\nelse\n puts 1\nend\n "}, {"source_code": "n = gets.to_i\ncurrency = gets.split(' ')\ncurrency.map!{|c| c.to_i}\ncurrency.sort!\nif currency.first == 1\n puts -1\nelse\n puts 1\nend"}, {"source_code": "gets;puts gets=~/\\b1\\b/?-1:1\n"}, {"source_code": "# http://codeforces.com/contest/560/problem/A\n# A. Currency System in Geraldion\ndef unfortunate_value(values)\n return nil if values.include?(1)\n\n values.sort!\n (1..(10**6)).to_a.find do |n|\n values.all? { |v| n % v != 0 }\n end\nend\n\nn = gets.chomp.to_i\nvalues = gets.chomp.split.map(&:to_i)\n\nans = unfortunate_value(values)\nputs ans.nil? ? -1 : ans\n"}, {"source_code": "n = gets.to_i\ninput = gets.split(' ')\nnums = (input.collect { |x| x.to_i }).sort!\n\nif nums[0] == 1\n\tputs -1 \nelse\n\tputs 1\nend\n"}, {"source_code": "gets;puts gets=~/\\b1\\b/?-1:1\n"}, {"source_code": "gets;puts gets=~/\\b1\\b/?-1:1"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i).sort\nputs a[0] == 1 ? -1 : 1"}, {"source_code": "gets;puts gets=~/\\b1\\b/?-1:1\n"}, {"source_code": "gets;puts gets=~/\\b1\\b/?-1:1\n"}, {"source_code": "n=gets.to_i\nx=gets.split.map{|e| e.to_i}.min\nif x==1 then\n\tputs -1\nelse\n\tputs 1\nend\n\n"}, {"source_code": "gets\nnums = gets.chomp.split.map(&:to_i)\nputs nums.include?(1) ? -1 : 1"}, {"source_code": "gets\nq = gets.split.map { |c| c.to_i }.sort\nputs q[0] == 1 ? -1 : 1"}, {"source_code": "n = gets.chomp\nA = []\nnbs = gets.chomp.split(\" \")\nexist = false\nnbs.each do |x|\n exist ||= x.to_i == 1\nend\n\nif exist\n print -1\nelse\n print 1\nend"}, {"source_code": "n = gets.chomp\nA = []\nnbs = gets.chomp.split(\" \")\nexist = false\nnbs.each do |x|\n exist = true if x.to_i == 1\nend\n\nprint exist ? -1 : 1"}, {"source_code": "# coding: utf-8\n\nn = gets.to_i\nar = gets.split.map(&:to_i).sort\n\nif ar.first == 1\n puts \"-1\" # 1 can express all!\nelse\n puts 1\nend"}, {"source_code": "gets;puts gets=~/\\b1\\b/?-1:1\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nputs a.include?(1) ? -1 : 1\n"}, {"source_code": "arr=gets(gets).split.map(&:to_i).sort(); puts arr[0] == 1 ? \"-1\" : \"1\"\n"}, {"source_code": "gets;puts gets=~/\\b1\\b/?-1:1"}, {"source_code": "num = gets.to_i\nnumbers=gets.split(' ')\nresult = numbers.collect{ |x| x.to_i }.sort!\nputs -1 if result[0] == 1\nputs 1 if result[0] != 1"}, {"source_code": "gets;puts gets=~/\\b1\\b/?-1:1\n"}, {"source_code": "gets\nputs gets=~/\\b1\\b/?-1:1"}, {"source_code": "gets\nputs gets.split.include?('1')?-1:1"}, {"source_code": "gets;puts gets=~/\\b1\\b/?-1:1\n"}, {"source_code": "banknoteNum = gets.chomp.to_i\nbanknotes = gets.chomp.split(' ').map {|item| item.to_i}\n\nif banknotes.include?(1)\n\tputs \"-1\"\n\texit\nelse\n\tputs \"1\"\n\texit\nend\n\nbanknotes.sort!\n\nnowIndex = 0\n\nuntil banknotes.length - 1 == nowIndex\n\tif banknotes[nowIndex + 1] - banknotes[nowIndex] > 1\n\t\tputs banknotes[nowIndex] + 1\n\t\texit\n\tend\n\n \tnowIndex += 1\n end "}], "negative_code": [{"source_code": "#!/usr/bin/ruby\ngets\nm=gets.split.map(&:to_i).min-1\np m==0 ? -1 : m"}, {"source_code": "# http://codeforces.com/contest/560/problem/A\n# A. Currency System in Geraldion\ndef unfortunate_value(values)\n return nil if values.include?(1)\n\n values.sort!\n (1..(10**6)).to_a.select do |n|\n return true if values.all? { |v| n % v != 0 }\n end.first\nend\n\nn = gets.chomp.to_i\nvalues = gets.chomp.split.map(&:to_i)\n\nans = unfortunate_value(values)\nputs ans.nil? ? -1 : ans\n"}, {"source_code": "# coding: utf-8\n\nn = gets.to_i\nar = gets.split.map(&:to_i).sort\ntotal_sum = ar.reduce(:+)\ni = 0\nuf_num = -1\nfor j in ar\n if j - i > 1\n uf_num = i\n break\n else\n i = j\n end\nend\n\nif uf_num == -1\n puts \"-1\"\nelse\n puts total_sum - 1\nend"}, {"source_code": "# coding: utf-8\n\nn = gets.to_i\nar = gets.split.map(&:to_i).sort\n\nif ar.first == 1\n puts \"-1\" # 1 can express all!\nelse\n puts ar.first - 1\nend"}, {"source_code": "# coding: utf-8\n\nn = gets.to_i\nar = gets.split.map(&:to_i).sort\ntotal_sum = ar.reduce(:+)\ni = ar.first\nuf_num = -1\nfor j in ar[1..-1]\n if j - i > 1\n uf_num = i + 1\n break\n else\n i = j\n end\nend\n\nif uf_num == -1\n puts \"-1\"\nelse\n puts total_sum - (uf_num - 1) + uf_num\nend"}, {"source_code": "arr=gets.split.map(&:to_i).sort(); puts arr[0] == 1 ? \"-1\" : \"1\"\n"}], "src_uid": "df94080c5b0b046af2397cafc02b5bdc"} {"nl": {"description": "A star is a figure of the following type: an asterisk character '*' in the center of the figure and four rays (to the left, right, top, bottom) of the same positive length. The size of a star is the length of its rays. The size of a star must be a positive number (i.e. rays of length $$$0$$$ are not allowed).Let's consider empty cells are denoted by '.', then the following figures are stars: The leftmost figure is a star of size $$$1$$$, the middle figure is a star of size $$$2$$$ and the rightmost figure is a star of size $$$3$$$. You are given a rectangular grid of size $$$n \\times m$$$ consisting only of asterisks '*' and periods (dots) '.'. Rows are numbered from $$$1$$$ to $$$n$$$, columns are numbered from $$$1$$$ to $$$m$$$. Your task is to draw this grid using any number of stars or find out that it is impossible. Stars can intersect, overlap or even coincide with each other. The number of stars in the output can't exceed $$$n \\cdot m$$$. Each star should be completely inside the grid. You can use stars of same and arbitrary sizes.In this problem, you do not need to minimize the number of stars. Just find any way to draw the given grid with at most $$$n \\cdot m$$$ stars.", "input_spec": "The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$3 \\le n, m \\le 100$$$) — the sizes of the given grid. The next $$$n$$$ lines contains $$$m$$$ characters each, the $$$i$$$-th line describes the $$$i$$$-th row of the grid. It is guaranteed that grid consists of characters '*' and '.' only.", "output_spec": "If it is impossible to draw the given grid using stars only, print \"-1\". Otherwise in the first line print one integer $$$k$$$ ($$$0 \\le k \\le n \\cdot m$$$) — the number of stars needed to draw the given grid. The next $$$k$$$ lines should contain three integers each — $$$x_j$$$, $$$y_j$$$ and $$$s_j$$$, where $$$x_j$$$ is the row index of the central star character, $$$y_j$$$ is the column index of the central star character and $$$s_j$$$ is the size of the star. Each star should be completely inside the grid.", "sample_inputs": ["6 8\n....*...\n...**...\n..*****.\n...**...\n....*...\n........", "5 5\n.*...\n****.\n.****\n..**.\n.....", "5 5\n.*...\n***..\n.*...\n.*...\n.....", "3 3\n*.*\n.*.\n*.*"], "sample_outputs": ["3\n3 4 1\n3 5 2\n3 5 1", "3\n2 2 1\n3 3 1\n3 4 1", "-1", "-1"], "notes": "NoteIn the first example the output 23 4 13 5 2is also correct."}, "positive_code": [{"source_code": "\ndef make_hash(n, m, boardstr)\n {}.tap do |h|\n (0...n).each do |i|\n (0...m).each do |j|\n h[[i,j]] = true if boardstr[i][j] == ?*\n end\n end\n end\nend\n\nrequire 'set'\n\ndef move(point, y, x)\n i, j = point\n [i + y, j + x]\nend\n\ndef get_star(board, i, j)\n p, q = i, j\n\n points = Set.new\n\n up = [p, q]\n down = [p, q]\n left = [p, q]\n right = [p, q]\n\n size = -1\n\n while board[up] && board[down] && board[left] && board[right]\n points << up\n points << down\n points << left\n points << right\n\n size += 1\n\n up = move(up, -1, 0)\n down = move(down, 1, 0)\n left = move(left, 0, -1)\n right = move(right, 0, 1)\n end\n\n [points, size] if size > 0\n\nend\n\n\ndef solution\n n, m = read_ints\n\n boardstr = []\n\n n.times do\n boardstr << read_string\n end\n\n board = make_hash(n, m, boardstr)\n lit = Set.new(board.keys)\n\n stars = []\n\n (1...(n-1)).each do |i|\n (1...(m-1)).each do |j|\n star_points, star_size = get_star(board, i, j)\n next unless star_points\n\n stars << [i+1, j+1, star_size]\n lit.subtract(star_points)\n\n end\n end\n\n if lit.empty?\n puts stars.size\n stars.each do |s|\n puts s.join(' ')\n end\n else\n puts(-1)\n end\n\nend\n\ndef read_int\n gets.to_i\nend\n\ndef read_ints\n gets.split.map(&:to_i)\nend\n\ndef read_string\n gets.chomp\nend\n\nsolution unless ENV['TEST__MODE']\n\n"}, {"source_code": "H, W = gets.split.map(&:to_i)\npic = []\nH.times do |i|\n pic[i] = gets.chomp\nend\n\ndown = Array.new(H+1){Array.new(W+1, -1)}\nup = Array.new(H+1){Array.new(W+1, -1)}\nleft = Array.new(H+1){Array.new(W+1, -1)}\nright = Array.new(H+1){Array.new(W+1, -1)}\n\nH.times do |i|\n W.times do |j|\n up[i][j] = (pic[i][j] == '*' ? up[i-1][j] + 1 : -1)\n left[i][j] = (pic[i][j] == '*' ? left[i][j-1] + 1 : -1)\n end\nend\n(H-1).downto(0) do |i|\n (W-1).downto(0) do |j|\n down[i][j] = pic[i][j] == '*' ? down[i+1][j] + 1 : -1\n right[i][j] = pic[i][j] == '*' ? right[i][j+1] + 1 : -1\n end\nend\n\nans = []\nH.times do |i|\n W.times do |j|\n num = [down[i][j], left[i][j], up[i][j], right[i][j]].min\n if num > 0\n pic[i][j] = '.'\n (1..num).each do |k|\n pic[i-k][j] = '.'\n pic[i+k][j] = '.'\n pic[i][j-k] = '.'\n pic[i][j+k] = '.'\n end\n ans.push([i+1, j+1, num])\n end\n end\nend\n\nif pic.all?{|row| row.count('*') == 0}\n puts ans.length\n ans.each do |arr|\n puts arr.join(' ')\n end\nelse\n puts -1\nend"}], "negative_code": [], "src_uid": "59d4c66892fa3157d1163225a550a368"} {"nl": {"description": "DZY has a sequence a, consisting of n integers.We'll call a sequence ai, ai + 1, ..., aj (1 ≤ i ≤ j ≤ n) a subsegment of the sequence a. The value (j - i + 1) denotes the length of the subsegment.Your task is to find the longest subsegment of a, such that it is possible to change at most one number (change one number to any integer you want) from the subsegment to make the subsegment strictly increasing.You only need to output the length of the subsegment you find.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109).", "output_spec": "In a single line print the answer to the problem — the maximum length of the required subsegment.", "sample_inputs": ["6\n7 2 3 1 5 6"], "sample_outputs": ["5"], "notes": "NoteYou can choose subsegment a2, a3, a4, a5, a6 and change its 3rd element (that is a4) to 4."}, "positive_code": [{"source_code": "n = gets.chomp.to_i\nif n == 1\n\tputs 1\n\texit\nend\na = gets.split.map{ |i| i.to_i }\nr = [1] * n\nl = r.clone\n\n( 1..n-1 ).each do |i|\n\tl[i] = l[i-1] + 1 if a[i] > a[i-1]\nend\n\n(n - 2).downto( 0 ).each do |i|\n\tr[i] = r[i+1] + 1 if a[i] < a[i+1]\nend\n\nans = r[1] + 1\n( 1..n-2 ).each do |i|\n\tans = [ans, l[i-1] + r[i+1] + 1].max if a[i+1] - a[i-1] >= 2\n\tans = [ans, l[i-1] + 1].max\n\tans = [ans, r[i+1] + 1].max\nend\nans = [ans, l[-2] + 1].max\nputs ans"}, {"source_code": "\n# Calculates the derivative\ndef derivative(ar) \n return ar.clone().unshift(0).each_cons(2).map{|x, y| y - x}\n # each_cons returns an iterator over pairs of elements\n # clone to get over the dumb ruby passig by reference\nend\n\n# Returns indices of non-increaseing points\ndef loc(ar)\n # Obtain the derivative of the input\n der = derivative(ar)\n return der.each_index.select{|i| der[i] <= 0} \n # each_index returns an itetrator over indices\n # select returns elements that match condition\nend\n\n# Read Input\nn = gets.strip.to_i\na = gets.strip.split.map(&:to_i)\n\n# Obtain the locations of non-increasing points\nnegloc = loc(a).unshift(0).push(n)\n\n# Create an array to store lengths of segments\nseqlength = [negloc[1]]\n\n# Loop over locations of non-increasing points\nfor i in 1..(negloc.length - 1) do\n j = negloc[i]\n if i == negloc.length - 1\n seqlength[i] = negloc[i] - negloc[i-1] + 1\n elsif j + 1 < a.length && a[j-1] + 1 < a[j+1]\n seqlength[i] = negloc[i+1] - negloc[i-1]\n elsif j - 2 > -1 && a[j-2] + 1 < a[j]\n seqlength[i] = negloc[i+1] - negloc[i-1]\n else\n seqlength[i] = negloc[i] - negloc[i-1] + 1\n end\nend\n\nputs [seqlength.max,n].min\n"}, {"source_code": "n = gets.to_i\na = gets.strip.split.map(&:to_i)\n\nleft, right = Array.new(n), Array.new(n)\n\nbest = n <= 2 ? n : 1\nleft[0] = 1\n(1..(n - 1)).each do |i|\n left[i] = a[i] > a[i - 1] ? left[i - 1] + 1 : 1\nend\n\nright[n - 1] = 1\n(0..(n - 2)).to_a.reverse.each do |i|\n right[i] = a[i] < a[i + 1] ? right[i + 1] + 1 : 1\nend\n\n(1..(n - 2)).each do |i|\n best = [best, left[i - 1] + right[i + 1] + 1].max if a[i - 1] + 1 < a[i + 1]\n best = [best, left[i] + 1].max\n best = [best, right[i] + 1].max\nend\n\nputs best\n"}, {"source_code": "n = gets.to_i\na = gets.strip.split.map(&:to_i)\n\nleft, right = Array.new(n), Array.new(n)\n\nbest = n <= 2 ? n : 1\nleft[0] = 1\n(1..(n - 1)).each do |i|\n left[i] = a[i] > a[i - 1] ? left[i - 1] + 1 : 1\n # puts best\nend\n\nright[n - 1] = 1\n(0..(n - 2)).to_a.reverse.each do |i|\n right[i] = a[i] < a[i + 1] ? right[i + 1] + 1 : 1\n # puts best\nend\n\n(1..(n - 2)).each do |i|\n best = [best, left[i - 1] + right[i + 1] + 1].max if a[i - 1] + 1 < a[i + 1]\n best = [best, left[i] + 1].max\n best = [best, right[i] + 1].max\n # puts best\nend\n\n# puts left.join(\" \")\n# puts right.join(\" \")\nputs best\n"}, {"source_code": "n = gets.to_i\na = gets.split(' ').map { |c| c.to_i }\n\nif n == 1\n puts 1\n exit\nend\n\nleftright = Array.new(n, 0)\nrightleft = Array.new(n, 0)\nleftright[0] = rightleft[n - 1] = 1\n\n(n - 1).times do |i|\n pos = i + 1\n leftright[pos] = 1 + (a[pos] > a[pos - 1] ? leftright[pos - 1] : 0)\nend\n(n - 1).times do |i|\n pos = n - i - 2\n rightleft[pos] = 1 + (a[pos] < a[pos + 1] ? rightleft[pos + 1] : 0)\nend\n\nbest = 1 + [rightleft[1], leftright[n - 2]].max\n(n - 2).times do |i|\n pos = i + 1\n best = [best, leftright[pos - 1] + 1].max\n best = [best, rightleft[pos + 1] + 1].max\n if a[pos - 1] + 2 <= a[pos + 1]\n best = [best, leftright[pos - 1] + 1 + rightleft[pos + 1]].max\n end\nend\n\nputs best"}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\nb=[0]*n\nc=[0]*n\nn.times do |i|\n b[i]=i>0&&a[i-1]0&&a[n-i]>a[n-1-i]?c[n-i]+1:1\nend\nr=0\nn.times do |i|\n r=b[i-1]+c[i+1]+1if i>0&&i1&&r a[i-1]\nend\n\n(n - 2).downto( 0 ).each do |i|\n\tr[i] = r[i+1] + 1 if a[i] < a[i+1]\nend\n\nans = r[0]\n( 1..n-2 ).each do |i|\n\tans = [ans, l[i-1] + r[i+1] + 1].max if a[i+1] - a[i-1] >= 2\nend\nans = [ans, l[-1]].max\nputs ans"}, {"source_code": "n = gets.chomp.to_i\na = gets.split.map{ |i| i.to_i }\nr = [1] * n\nl = r.clone\n\n( 1..n-1 ).each do |i|\n\tl[i] = l[i-1] + 1 if a[i] > a[i-1]\nend\n\n(n - 2).downto( 0 ).each do |i|\n\tr[i] = r[i+1] + 1 if a[i] < a[i+1]\nend\n\nans = r[0]\n( 1..n-2 ).each do |i|\n\tans = [ans, l[i-1] + r[i+1] + 1].max if a[i+1] - a[i-1] >= 2\n\tans = [ans, l[i-1] + 1].max\n\tans = [ans, r[i+1] + 1].max\nend\nans = [ans, l[-1]].max\nputs ans"}, {"source_code": "n = gets.chomp.to_i\na = gets.split.map{ |i| i.to_i }\nl = 0\nr = 1\nans = 1\nchanges = -1\nwhile l < n && r < n do\n\tif a[r] <= a[r - 1]\n\t\tif changes < 0\n\t\t\tif l == r - 1\n\t\t\t\ta[r-1] = a[r] - 1\n\t\t\t\tchanges = l\n\t\t\telse\n\t\t\t\ta[r] = a[r-1] + 1\n\t\t\t\tchanges = r\n\t\t\tend\n\t\telse\n\t\t\tl = changes + 1\n\t\t\tchanges = -1\n\t\tend\n\tend\n\tans = [ans, r - l + 1].max\n\tr += 1\nend\nputs ans"}, {"source_code": "\nn = gets.strip.to_i\na = gets.strip.split.map(&:to_i)\n\nlongest_so_far = 0\nlength = 1\nhicat = 0\ni = 0\n\nwhile i < n - 1\n if a[i] >= a[i+1]\n lognest_so_far = length if length > longest_so_far\n length = length - hicat\n hicat = length\n end\n length = length + 1\n i = i + 1\nend\n\nlognest_so_far = length if length > longest_so_far\n\nputs lognest_so_far"}, {"source_code": "def derivative(ar) \n # Each cons returns an iterator over pairs of elements\n return ar.clone().unshift(0).each_cons(2).map{|x, y| y - x}\nend\n\ndef loc(ar, v)\n return ar.each_index.select{|i| ar[i] <= v} \nend\n\n# Read Input\nn = gets.strip.to_i\na = gets.strip.split.map(&:to_i)\n\nif (n <= 2)\n puts n\n exit\nend\n\n# Obtain the derivative of the input\nder = derivative(a)\n\n# Obtain the locations of negatives\nnegloc = loc(der,0).unshift(0).push(n)\n\nseqlength = []\n\nfor i in 0..(negloc.length - 1) do\n j = negloc[i]\n\n if i == 0\n seqlength[i] = negloc[1]\n elsif i == negloc.length - 1\n seqlength[i] = negloc[i] - negloc[i-1] + 1\n elsif j+1 < a.length && a[j-1] + 1 < a[j+1]\n seqlength[i] = negloc[i+1] - negloc[i-1]\n else\n seqlength[i] = negloc[i] - negloc[i-1] + 1\n end\n\nend\n\nputs [seqlength.max,n].min\n"}, {"source_code": "\ndef derivative(ar) \n # Each cons returns an iterator over pairs of elements\n return ar.clone().unshift(0).each_cons(2).map{|x, y| y - x}\nend\n\ndef loc(ar, v)\n return ar.each_index.select{|i| ar[i] <= v} \nend\n\n# Read Input\nn = gets.strip.to_i\na = gets.strip.split.map(&:to_i)\n\nif (n <= 2)\n puts n\n exit\nend\n\n\n# Obtain the derivative of the input\nder = derivative(a)\n\n# Obtain the locations of negatives\nnegloc = loc(der,0).unshift(0).push(n)\n\nseqlength = []\n\nfor i in 0..(negloc.length - 1) do\n j = negloc[i]\n\n if i == 0\n seqlength[i] = negloc[1]\n elsif i == negloc.length - 1\n seqlength[i] = negloc[i] - negloc[i-1] + 1\n elsif j+1 < a.length && a[j-1] + 1 < a[j+1]\n seqlength[i] = negloc[i+1] - negloc[i-1]\n else\n seqlength[i] = negloc[i] - negloc[i-1] + 1\n end\n\nend\n\nputs seqlength.max\n\n\n"}, {"source_code": "\ndef derivative(ar) \n # Each cons returns an iterator over pairs of elements\n return ar.clone().unshift(0).each_cons(2).map{|x, y| y - x}\nend\n\ndef loc(ar, v)\n return ar.each_index.select{|i| ar[i] <= v} \nend\n\n# Read Input\nn = gets.strip.to_i\na = gets.strip.split.map(&:to_i)\n\n# Obtain the derivative of the input\nder = derivative(a)\n\n# Obtain the locations of negatives\nnegloc = loc(der,0).unshift(0).push(n)\n\nseqlength = []\n\nfor i in 0..(negloc.length - 1) do\n j = negloc[i]\n\n if i == 0\n seqlength[i] = negloc[1]\n elsif i == negloc.length - 1\n seqlength[i] = negloc[i] - negloc[i-1] + 1\n elsif j+1 < a.length && a[j-1] + 1 < a[j+1]\n seqlength[i] = negloc[i+1] - negloc[i-1]\n else\n seqlength[i] = negloc[i] - negloc[i-1] + 1\n end\n\nend\nputs seqlength.max\n\n\n"}, {"source_code": "n = gets.strip.to_i\na = gets.strip.split.map(&:to_i)\n\nlongest_so_far = 0\nlength = 1\nhicat = 0\ni = 0\n\nwhile i < n - 1\n if a[i] >= a[i+1]\n longest_so_far = length if length > longest_so_far\n length = length - hicat\n hicat = length\n end\n length = length + 1\n i = i + 1\nend\n\nlongest_so_far = length if length > longest_so_far\n\nputs longest_so_far"}, {"source_code": "\ndef derivative(ar) \n # Each cons returns an iterator over pairs of elements\n return ar.clone().unshift(0).each_cons(2).map{|x, y| y - x}\nend\n\ndef loc(ar, v)\n return ar.each_index.select{|i| ar[i] <= v} \nend\n\n# Read Input\nn = gets.strip.to_i\na = gets.strip.split.map(&:to_i)\n\nif (n <= 2)\n puts n\n exit\nend\n\n\n# Obtain the derivative of the input\nder = derivative(a)\n\n# Obtain the locations of negatives\nnegloc = loc(der,0).unshift(0).push(n)\n\nseqlength = []\n\nfor i in 0..(negloc.length - 1) do\n j = negloc[i]\n\n if i == 0\n seqlength[i] = negloc[1]\n elsif i == negloc.length - 1\n seqlength[i] = negloc[i] - negloc[i-1] + 1\n elsif j+1 < a.length && a[j-1] + 1 < a[j+1]\n seqlength[i] = negloc[i+1] - negloc[i-1]\n else\n seqlength[i] = negloc[i] - negloc[i-1] + 1\n end\n\nend\n\nputs [seqlength.max,n].min\n\n\n"}, {"source_code": "\nn = gets.strip.to_i\na = gets.strip.split.map(&:to_i)\n\nputs n\nputs a\n\nlongest_so_far = 0\nlength = 1\nhicat = 0\ni = 0\n\nwhile i < n - 1\n if a[i] >= a[i+1]\n lognest_so_far = length if length > longest_so_far\n length = length - hicat\n hicat = length\n end\n length = length + 1\n i = i + 1\nend\n\nlognest_so_far = length if length > longest_so_far\n\nputs lognest_so_far"}, {"source_code": "\ndef derivative(ar) \n # Each cons returns an iterator over pairs of elements\n return ar.clone().unshift(0).each_cons(2).map{|x, y| y - x}\nend\n\ndef loc(ar, v)\n return ar.each_index.select{|i| ar[i] <= v} \nend\n\n# Read Input\nn = gets.strip.to_i\na = gets.strip.split.map(&:to_i)\n\n# Obtain the derivative of the input\nder = derivative(a)\n\n# Obtain the locations of negatives\nnegloc = loc(der,0).unshift(0).push(n)\n\nseqlength = []\n\nfor i in 0..(negloc.length - 1) do\n j = negloc[i]\n\n if i == 0\n seqlength[i] = negloc[1]\n elsif i == negloc.length - 1\n seqlength[i] = negloc[i] - negloc[i-1]\n elsif j+1 < a.length && a[j-1] + 1 < a[j+1]\n seqlength[i] = negloc[i+1] - negloc[i-1]\n else\n seqlength[i] = negloc[i] - negloc[i-1] + 1\n end\n\nend\n\nputs seqlength.max\n\n\n"}, {"source_code": "n = gets.strip.to_i\na = gets.strip.split.map(&:to_i)\n\nlongest_so_far = 0\nlength = 1\nhicat = 0\ni = 0\n\nwhile i < n - 1\n if a[i] >= a[i+1]\n longest_so_far = length if length > longest_so_far\n length = length - hicat\n hicat = length\n end\n length = length + 1\n i = i + 1\nend\n\nlognest_so_far = length if length > longest_so_far\n\nputs longest_so_far"}, {"source_code": "\ndef derivative(ar) \n # Each cons returns an iterator over pairs of elements\n return ar.clone().unshift(0).each_cons(2).map{|x, y| y - x}\nend\n\ndef loc(ar, v)\n return ar.each_index.select{|i| ar[i] <= v} \nend\n\n# Read Input\nn = gets.strip.to_i\na = gets.strip.split.map(&:to_i)\n\nif (n <= 2)\n return n\n exit\nend\n\n\n# Obtain the derivative of the input\nder = derivative(a)\n\n# Obtain the locations of negatives\nnegloc = loc(der,0).unshift(0).push(n)\n\nseqlength = []\n\nfor i in 0..(negloc.length - 1) do\n print \"Now at: \"\n puts negloc[i]\n\n j = negloc[i]\n\n if i == 0\n seqlength[i] = negloc[1]\n elsif i == negloc.length - 1\n seqlength[i] = negloc[i] - negloc[i-1] + 1\n elsif j+1 < a.length && a[j-1] + 1 < a[j+1]\n seqlength[i] = negloc[i+1] - negloc[i-1]\n else\n seqlength[i] = negloc[i] - negloc[i-1] + 1\n end\n\nend\n\nputs seqlength.max\n\n\n"}, {"source_code": "n = gets.to_i\na = gets.strip.split.map(&:to_i)\n\nleft, right = Array.new(n), Array.new(n)\n\nleft[0] = 1\n(1..(n - 1)).each do |i|\n left[i] = a[i] > a[i - 1] ? left[i - 1] + 1 : 1\nend\n\nright[n - 1] = 1\n(0..(n - 2)).to_a.reverse.each do |i|\n right[i] = a[i] < a[i + 1] ? right[i + 1] + 1 : 1\nend\n\nbest = 1\n(1..(n - 2)).each do |i|\n best = [best, left[i] + right[i] + 1].max if a[i - 1] + 1 < a[i + 1]\nend\n\nputs best\n"}, {"source_code": "n = gets.to_i\na = gets.strip.split.map(&:to_i)\n\nleft, right = Array.new(n), Array.new(n)\n\nleft[0] = 1\n(1..(n - 1)).each do |i|\n left[i] = a[i] > a[i - 1] ? left[i - 1] + 1 : 1\nend\n\nright[n - 1] = 1\n(0..(n - 2)).to_a.reverse.each do |i|\n right[i] = a[i] < a[i + 1] ? right[i + 1] + 1 : 1\nend\n\nbest = 1\n(1..(n - 2)).each do |i|\n best = [best, left[i - 1] + right[i + 1] + 1].max if a[i - 1] + 1 < a[i + 1]\n best = [best, left[i] + 1, right[i] + 1].max\n # puts best\nend\n\n# puts left.join(\" \")\n# puts right.join(\" \")\nputs best\n"}, {"source_code": "n = gets.to_i\na = gets.strip.split.map(&:to_i)\n\nleft, right = Array.new(n), Array.new(n)\n\nbest = 1\nleft[0] = 1\n(1..(n - 1)).each do |i|\n left[i] = a[i] > a[i - 1] ? left[i - 1] + 1 : 1\n best = [best, left[i] + 1].max\nend\n\nright[n - 1] = 1\n(0..(n - 2)).to_a.reverse.each do |i|\n right[i] = a[i] < a[i + 1] ? right[i + 1] + 1 : 1\n best = [best, right[i] + 1].max\nend\n\n(1..(n - 2)).each do |i|\n best = [best, left[i - 1] + right[i + 1] + 1].max if a[i - 1] + 1 < a[i + 1]\nend\n\nputs best\n"}, {"source_code": "n = gets.to_i\na = gets.split(' ').map { |c| c.to_i }\nleftright = Array.new(n, 0)\nrightleft = Array.new(n, 0)\nleftright[0] = rightleft[n - 1] = 1\n\n(n-1).times do |i|\n pos = i + 1\n leftright[pos] = 1\n if a[pos] > a[pos - 1]\n leftright[pos] += 1\n end\nend\n\n(n-1).times do |i|\n pos = n - 2 - i\n rightleft[pos] = 1\n if a[pos] < a[pos + 1]\n rightleft[pos] += 1\n end\nend\n\nbest = [1 + rightleft[1], 1 + leftright[n - 2]].max\na.each_with_index do |val, i|\n next if i == 0 || i == n - 1 || a[i - 1] + 2 > a[i + 1]\n best = [best, leftright[i - 1] + 1 + rightleft[i + 1]].max\nend\n\nputs best"}], "src_uid": "9e0d271b9bada1364dfcb47297549652"} {"nl": {"description": "Alice and Bob are playing a game on a matrix, consisting of $$$2$$$ rows and $$$m$$$ columns. The cell in the $$$i$$$-th row in the $$$j$$$-th column contains $$$a_{i, j}$$$ coins in it.Initially, both Alice and Bob are standing in a cell $$$(1, 1)$$$. They are going to perform a sequence of moves to reach a cell $$$(2, m)$$$.The possible moves are: Move right — from some cell $$$(x, y)$$$ to $$$(x, y + 1)$$$; Move down — from some cell $$$(x, y)$$$ to $$$(x + 1, y)$$$. First, Alice makes all her moves until she reaches $$$(2, m)$$$. She collects the coins in all cells she visit (including the starting cell).When Alice finishes, Bob starts his journey. He also performs the moves to reach $$$(2, m)$$$ and collects the coins in all cells that he visited, but Alice didn't.The score of the game is the total number of coins Bob collects.Alice wants to minimize the score. Bob wants to maximize the score. What will the score of the game be if both players play optimally?", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of testcases. Then the descriptions of $$$t$$$ testcases follow. The first line of the testcase contains a single integer $$$m$$$ ($$$1 \\le m \\le 10^5$$$) — the number of columns of the matrix. The $$$i$$$-th of the next $$$2$$$ lines contain $$$m$$$ integers $$$a_{i,1}, a_{i,2}, \\dots, a_{i,m}$$$ ($$$1 \\le a_{i,j} \\le 10^4$$$) — the number of coins in the cell in the $$$i$$$-th row in the $$$j$$$-th column of the matrix. The sum of $$$m$$$ over all testcases doesn't exceed $$$10^5$$$.", "output_spec": "For each testcase print a single integer — the score of the game if both players play optimally.", "sample_inputs": ["3\n3\n1 3 7\n3 5 1\n3\n1 3 9\n3 5 1\n1\n4\n7"], "sample_outputs": ["7\n8\n0"], "notes": "NoteThe paths for the testcases are shown on the following pictures. Alice's path is depicted in red and Bob's path is depicted in blue. "}, "positive_code": [{"source_code": "MOD = 10 ** 9 + 7\r\ndef gs; gets.chomp; end\r\ndef gi; gets.to_i; end\r\ndef gli; gets.split.map(&:to_i); end\r\ndef cyesno(x); puts x ? :Yes : :No; end\r\ndef uyesno(x); puts x ? :YES : :NO; end\r\ndef array_to_hash_by_count(c); Hash.new(0).tap { |x| c.each { |z| x[z] += 1 } }; end\r\n\r\ndef solve\r\n n = gi\r\n a = gli\r\n b = gli\r\n a_s = a.sum\r\n b_s = 0\r\n mn = [a_s, b.sum].max\r\n for i in (0..n-1)\r\n a_s -= a[i]\r\n tmp = [a_s, b_s].max\r\n mn = tmp if tmp < mn\r\n b_s += b[i]\r\n end\r\n puts mn\r\nend\r\n\r\ndef main\r\n gi.times { solve }\r\nend\r\n\r\nmain\r\n"}, {"source_code": "#!/usr/bin/env ruby\r\n\r\ndef arline\r\n gets.chomp.split.map(&:to_i)\r\nend\r\n\r\ndef pfr(ar)\r\n res = [0]\r\n ar.each do |x| res.append(res[-1] + x) end\r\n res\r\nend\r\n\r\nclass Grid\r\n def initialize(ar)\r\n @pfr = ar.map{|x| pfr(x)}\r\n @n = ar[0].length\r\n end\r\n def score(x)\r\n [@pfr[1][x-1], @pfr[0][@n] - @pfr[0][x]].max\r\n end\r\nend\r\n\r\ngets.chomp.to_i.times do\r\n n = gets.chomp.to_i\r\n a = (1..2).map{arline}\r\n g = Grid.new(a)\r\n puts (1..n).map{|x| g.score(x) }.min\r\nend\r\n"}], "negative_code": [{"source_code": "MOD = 10 ** 9 + 7\r\ndef gs; gets.chomp; end\r\ndef gi; gets.to_i; end\r\ndef gli; gets.split.map(&:to_i); end\r\ndef cyesno(x); puts x ? :Yes : :No; end\r\ndef uyesno(x); puts x ? :YES : :NO; end\r\ndef array_to_hash_by_count(c); Hash.new(0).tap { |x| c.each { |z| x[z] += 1 } }; end\r\n\r\ndef solve\r\n n = gi\r\n a = gli\r\n b = gli\r\n a_s = a.sum\r\n b_s = b.sum\r\n mn = [a_s, b_s].max\r\n b_s = 0\r\n for i in (0..n-1)\r\n a_s -= a[i]\r\n b_s += b[i]\r\n tmp = [a_s, b_s].max\r\n mn = tmp if mn > tmp\r\n end\r\n puts mn - b[-1]\r\nend\r\n\r\ndef main\r\n gi.times { solve }\r\nend\r\n\r\nmain\r\n"}], "src_uid": "f3ee3a0de5ddf3cf15ef02fb62a2768e"} {"nl": {"description": "You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$|x_1 - x_2| + |y_1 - y_2|$$$, where $$$|x|$$$ is the absolute value of $$$x$$$. ", "input_spec": "First line contains a single integer $$$t$$$ $$$(1 \\leq t \\leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \\leq n \\leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 \\leq x_i, y_i \\leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$.", "output_spec": "For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.", "sample_inputs": ["6\n3\n0 0\n2 0\n1 2\n4\n1 0\n0 2\n2 3\n3 1\n4\n0 0\n0 1\n1 0\n1 1\n2\n0 0\n1 1\n2\n0 0\n2 0\n2\n0 0\n0 0"], "sample_outputs": ["1\n4\n4\n4\n3\n1"], "notes": "NoteHere are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$."}, "positive_code": [{"source_code": "def solve(a)\r\n n = a.size\r\n a.sort!\r\n a[n / 2] - a[(n - 1) / 2] + 1\r\nend\r\ngets.to_i.times do\r\n n = gets.to_i\r\n x, y = [], []\r\n n.times do |i|\r\n cx, cy = gets.split.map &:to_i\r\n x << cx\r\n y << cy\r\n end\r\n puts solve(x) * solve(y)\r\nend"}, {"source_code": "alias g gets;g;$<.map{|l|n=l.to_i;x,y=[],[];(0...n).map{c=g.split.map &:to_i;x<>1]-i[n-1>>1]+1};p a*b}"}, {"source_code": "dist = ->zs, z1 { zs.map { |z2| (z2 - z1).abs }.sum }\r\n\r\ngets.to_i.times do\r\n n = gets.to_i\r\n xys = n.times.map { gets.split.map(&:to_i) }\r\n p ([0, 1].map do |i|\r\n zs = xys.map { |xy| xy[i] }\r\n min = zs.min_by { |a| dist[zs, a] }\r\n mind = dist[zs, min]\r\n a = (0..min).bsearch { |i| dist[zs, i] == mind }\r\n b = (min..(10 ** 10)).bsearch { |i| dist[zs, i] > mind }\r\n (b - a).abs\r\n end).reduce(:*)\r\nend\r\n"}, {"source_code": "def solve(range, n)\n range.sort!\n\n return 1 if n % 2 == 1\n\n range[n/2] - range[n/2 - 1] + 1\nend\n\ncases = gets.chomp.to_i\n\ncases.times do\n n = gets.chomp.to_i\n xs = []\n ys = []\n n.times do\n x, y = gets.chomp.split(\" \").map(&:to_i)\n xs << x\n ys << y\n end\n puts solve(xs, n) * solve(ys, n)\nend"}, {"source_code": "def solve(range, n)\n range.sort!\n\n range[n/2] - range[(n - 1)/2] + 1\nend\n\ncases = gets.chomp.to_i\n\ncases.times do\n n = gets.chomp.to_i\n xs = []\n ys = []\n n.times do\n x, y = gets.chomp.split(\" \").map(&:to_i)\n xs << x\n ys << y\n end\n puts solve(xs, n) * solve(ys, n)\nend"}], "negative_code": [], "src_uid": "e0a1dc397838852957d0e15ec98d5efe"} {"nl": {"description": "A very brave explorer Petya once decided to explore Paris catacombs. Since Petya is not really experienced, his exploration is just walking through the catacombs.Catacombs consist of several rooms and bidirectional passages between some pairs of them. Some passages can connect a room to itself and since the passages are built on different depths they do not intersect each other. Every minute Petya arbitrary chooses a passage from the room he is currently in and then reaches the room on the other end of the passage in exactly one minute. When he enters a room at minute i, he makes a note in his logbook with number ti: If Petya has visited this room before, he writes down the minute he was in this room last time; Otherwise, Petya writes down an arbitrary non-negative integer strictly less than current minute i. Initially, Petya was in one of the rooms at minute 0, he didn't write down number t0.At some point during his wandering Petya got tired, threw out his logbook and went home. Vasya found his logbook and now he is curious: what is the minimum possible number of rooms in Paris catacombs according to Petya's logbook?", "input_spec": "The first line contains a single integer n (1 ≤ n ≤ 2·105) — then number of notes in Petya's logbook. The second line contains n non-negative integers t1, t2, ..., tn (0 ≤ ti < i) — notes in the logbook.", "output_spec": "In the only line print a single integer — the minimum possible number of rooms in Paris catacombs.", "sample_inputs": ["2\n0 0", "5\n0 1 0 1 3"], "sample_outputs": ["2", "3"], "notes": "NoteIn the first sample, sequence of rooms Petya visited could be, for example 1 → 1 → 2, 1 → 2 → 1 or 1 → 2 → 3. The minimum possible number of rooms is 2.In the second sample, the sequence could be 1 → 2 → 3 → 1 → 2 → 1."}, "positive_code": [{"source_code": "n = gets.to_i\na = gets.split.collect(&:to_i)\n#s = \"0 0\"\n#a = s.split.collect(&:to_i)\ntw = Array.new(2000001) { |i| false }\ntw[0] = true\nr = 1\na.each_index{|t| \n if tw[a[t]] then\n \ttw[a[t]] = false\n \ttw[t+1] = true\n else\n \ttw[t+1] = true\n \tr+=1\n end\n}\nputs r"}], "negative_code": [], "src_uid": "81c6342b7229892d71cb43e72aee99e9"} {"nl": {"description": "You are given two strings $$$s$$$ and $$$t$$$ both of length $$$2$$$ and both consisting only of characters 'a', 'b' and 'c'.Possible examples of strings $$$s$$$ and $$$t$$$: \"ab\", \"ca\", \"bb\".You have to find a string $$$res$$$ consisting of $$$3n$$$ characters, $$$n$$$ characters should be 'a', $$$n$$$ characters should be 'b' and $$$n$$$ characters should be 'c' and $$$s$$$ and $$$t$$$ should not occur in $$$res$$$ as substrings.A substring of a string is a contiguous subsequence of that string. So, the strings \"ab\", \"ac\" and \"cc\" are substrings of the string \"abacc\", but the strings \"bc\", \"aa\" and \"cb\" are not substrings of the string \"abacc\".If there are multiple answers, you can print any of them.", "input_spec": "The first line of the input contains one integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$) — the number of characters 'a', 'b' and 'c' in the resulting string. The second line of the input contains one string $$$s$$$ of length $$$2$$$ consisting of characters 'a', 'b' and 'c'. The third line of the input contains one string $$$t$$$ of length $$$2$$$ consisting of characters 'a', 'b' and 'c'.", "output_spec": "If it is impossible to find the suitable string, print \"NO\" on the first line. Otherwise print \"YES\" on the first line and string $$$res$$$ on the second line. $$$res$$$ should consist of $$$3n$$$ characters, $$$n$$$ characters should be 'a', $$$n$$$ characters should be 'b' and $$$n$$$ characters should be 'c' and $$$s$$$ and $$$t$$$ should not occur in $$$res$$$ as substrings. If there are multiple answers, you can print any of them.", "sample_inputs": ["2\nab\nbc", "3\naa\nbc", "1\ncb\nac"], "sample_outputs": ["YES\nacbbac", "YES\ncacbacbab", "YES\nabc"], "notes": null}, "positive_code": [{"source_code": "class NL\n def initialize(n)\n @n = n\n end\n def left(arr)\n return \"a\" unless arr.include?(\"a\")\n return \"b\" unless arr.include?(\"b\")\n return \"c\" unless arr.include?(\"c\")\n end\n\n def p_part(part)\n puts \"YES\\n#{part*@n}\"\n exit\n end\n\n def p_pat(pat)\n puts \"YES\\n#{pat}\"\n exit\n end\nend\n\nnn = gets.to_i\ns = gets.chomp.chars\nt = gets.chomp.chars\nnl = NL.new(nn)\n\npart = \"\"\n# 1文字目が同じ\nif s[0]==t[0]\n # 全部同じ\n if s[1]==t[1] && s[0]==s[1]\n pat = \"abc\"*nn\n nl.p_pat(pat)\n end\n\n if s[1]!=s[0] && t[1]!=s[0]\n pat = (s[1]<< nl.left([s[0],s[1]]))*nn << s[0]*nn\n nl.p_pat(pat)\n else\n if s[0]==s[1]\n part=t[0]< v\n xs = target-v\n elsif target < v\n xs = mod+target-v\n end\n ops << \"1 #{i+1} #{xs}\"\n sum += xs\nend\nops << \"2 #{n} #{mod}\"\nputs ops.size\nputs ops.join(\"\\n\")"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nm = 10**5 + 1\nputs(n + 1)\n(n - 1).downto(0) do |i|\n d = (m - a[i] + i)%m\n (0..i).each{|j| a[j] += d}\n puts '1 ' + (i + 1).to_s + ' ' + d.to_s\n# p a\nend\nputs '2 ' + n.to_s + ' ' + m.to_s\n\n#(0...n).each{|j| a[j] %= m}\n#p a\n"}, {"source_code": "n = gets.to_i\narr = gets.chomp.split(\" \").map(&:to_i)\n\nputs n+1\n\ndef getdiff(elem,n,modn)\n k = elem/n\n req = k*n + modn\n if req=0)\n toadd = getdiff(arr[i],n,i)\n j = 0\n while(j<=i)\n arr[j]+= toadd\n j = j+1\n end\n puts \"1 #{i+1} #{toadd}\"\n i = i-1\nend\n\nputs \"2 #{n} #{n}\"\n\n=begin\n(0..n-1).each do |i|\n arr[i] = arr[i]%n\nend\np arr\n=end\n"}], "negative_code": [{"source_code": "n = gets.to_i\nary = gets.strip.split.map(&:to_i)\n# tmp = ary.dup\n\nsum = 0\nops = []\nmod = 2100 # 500000\n\n(n-1).downto(0).each do |i|\n v = ary[i]+sum\n v %= mod\n target = mod - (n-i)\n # puts \"target:#{target}, v:#{v}\"\n next if v == target\n xs = 0\n if target > v\n xs = target-v\n elsif target < v\n xs = mod+target-v\n end\n ops << \"1 #{i+1} #{xs}\"\n sum += xs\nend\nops << \"2 #{n} #{mod}\"\nputs ops.join(\"\\n\")"}, {"source_code": "n = gets.to_i\nary = gets.strip.split.map(&:to_i)\n# tmp = ary.dup\n\nsum = 0\nops = []\nmod = 500000\n\n(n-1).downto(0).each do |i|\n v = ary[i]+sum\n v %= mod\n target = mod - (n-i)\n # puts \"target:#{target}, v:#{v}\"\n next if v == target\n xs = 0\n if target > v\n xs = target-v\n elsif target < v\n xs = mod+target-v\n end\n ops << \"1 #{i+1} #{xs}\"\n sum += xs\nend\nops << \"2 #{n} #{mod}\"\nputs ops.join(\"\\n\")"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nm = 10**5 + 1\n(n - 1).downto(0) do |i|\n d = (m - a[i] + i)%m\n (0..i).each{|j| a[j] += d}\n puts '1 ' + (i + 1).to_s + ' ' + d.to_s\n# p a\nend\nputs '2 ' + n.to_s + ' ' + m.to_s\n\n#(0...n).each{|j| a[j] %= m}\n#p a\n"}], "src_uid": "e0ba8e0bfd9e1365daa41e1d14610200"} {"nl": {"description": "Bob programmed a robot to navigate through a 2d maze.The maze has some obstacles. Empty cells are denoted by the character '.', where obstacles are denoted by '#'.There is a single robot in the maze. Its start position is denoted with the character 'S'. This position has no obstacle in it. There is also a single exit in the maze. Its position is denoted with the character 'E'. This position has no obstacle in it.The robot can only move up, left, right, or down.When Bob programmed the robot, he wrote down a string of digits consisting of the digits 0 to 3, inclusive. He intended for each digit to correspond to a distinct direction, and the robot would follow the directions in order to reach the exit. Unfortunately, he forgot to actually assign the directions to digits.The robot will choose some random mapping of digits to distinct directions. The robot will map distinct digits to distinct directions. The robot will then follow the instructions according to the given string in order and chosen mapping. If an instruction would lead the robot to go off the edge of the maze or hit an obstacle, the robot will crash and break down. If the robot reaches the exit at any point, then the robot will stop following any further instructions.Bob is having trouble debugging his robot, so he would like to determine the number of mappings of digits to directions that would lead the robot to the exit.", "input_spec": "The first line of input will contain two integers n and m (2 ≤ n, m ≤ 50), denoting the dimensions of the maze. The next n lines will contain exactly m characters each, denoting the maze. Each character of the maze will be '.', '#', 'S', or 'E'. There will be exactly one 'S' and exactly one 'E' in the maze. The last line will contain a single string s (1 ≤ |s| ≤ 100) — the instructions given to the robot. Each character of s is a digit from 0 to 3.", "output_spec": "Print a single integer, the number of mappings of digits to directions that will lead the robot to the exit.", "sample_inputs": ["5 6\n.....#\nS....#\n.#....\n.#....\n...E..\n333300012", "6 6\n......\n......\n..SE..\n......\n......\n......\n01232123212302123021", "5 3\n...\n.S.\n###\n.E.\n...\n3"], "sample_outputs": ["1", "14", "0"], "notes": "NoteFor the first sample, the only valid mapping is , where D is down, L is left, U is up, R is right."}, "positive_code": [{"source_code": "\ndef valid_perm(n,m,arr,start_i,start_j,ends_i,ends_j,perm,str)\n\ncur_i = start_i\ncur_j = start_j\n\nstr.each do |c|\n\n\t\tind = perm.index(c)\n\t\tif ind==0 #up\n\t\t\tif cur_i - 1 <0 or arr[cur_i - 1][cur_j] == '#'\n\t\t\t\treturn false\n\t\t\telsif cur_i - 1 == ends_i and cur_j == ends_j\n\t\t\t\treturn true\n\t\t\tend\n\t\t\tcur_i-=1\n\t\telsif ind == 1 #down\n\t\t\tif cur_i + 1 >=n or arr[cur_i + 1][cur_j] == '#'\n\t\t\t\treturn false\n\t\t\telsif cur_i + 1 == ends_i and cur_j == ends_j\n\t\t\t\t\n\t\t\t\treturn true\n\t\t\tend\n\t\t\tcur_i+=1\n\t\t\t\n\t\telsif ind== 2 #left\n\t\t\tif cur_j - 1 <0 or arr[cur_i][cur_j-1] == '#'\n\t\t\t\treturn false\n\t\t\telsif cur_i == ends_i and cur_j-1 == ends_j\n\t\t\t\t\n\t\t\t\treturn true\n\t\t\tend\n\t\t\tcur_j-=1\n\t\t\t\n\t\telsif ind==3 #right\n\t\t\tif c == \"0\" and perm.index(c)==3\n\t \t\tend\n\t\t\tif cur_j + 1 >=m or arr[cur_i ][cur_j+1] == '#'\n\t\t\t\treturn false\n\t\t\telsif cur_i == ends_i and cur_j+1 == ends_j\n\t\t\t\treturn true\n\t\t\tend\n\t\t\tcur_j+=1\t\t\n\t\tend\t\t\n\tend\nreturn false\nend\n\n\n\n\n\n\n\n\n\nn,m = gets.chomp.split\nn = n.to_i\nm = m.to_i\n\narr = []\nstart_i=0\nstart_j=0\nends_i=0\nends_j=0\n(0...n).each do |i|\n\ts = gets.chomp.split('') \n\tif s.include?(\"S\")\n\t\tstart_i = i\n\t\tstart_j = s.index(\"S\")\n\tend\t\t\n\tif s.include?(\"E\")\n\t\tends_i = i\n\t\tends_j = s.index(\"E\")\n\tend\t\n\tarr<= $n || c >= $m || $s[r][c] == '#'\n return false\n end\n end\n return r == $er && c == $ec\nend\n\ng = gets.chomp.split(' ')\n$n = g[0].to_i\n$m = g[1].to_i\nfor i in (0...$n)\n g = gets.chomp\n $s.push(g)\n for j in (0...$m)\n if g[j] == 'S'\n $sr = i\n $sc = j\n end\n if g[j] == 'E'\n $er = i\n $ec = j\n end\n end\nend\n$x = gets.chomp\nans = 0\nfor i in (0...p.length)\n $f = p[i]\n ans += check ? 1 : 0\nend\nputs ans"}, {"source_code": "n, m = gets.split.map(&:to_i)\n\nmaze = Array.new(n)\nn.times do |i|\n maze[i] = gets.chomp\n S = [i, maze[i].index(?S)] if maze[i].include?(?S)\n G = [i, maze[i].index(?E)] if maze[i].include?(?E)\nend\n\ns = gets.chomp\n\nans = 0\n%w(U D R L).permutation(4) do |a, b, c, d| \n \n R = S.dup\n i = [a, b, c, d]\n \n s.each_char do |c|\n \n d = i[c.to_i]\n \n if d == ?U\n R[0] += 1\n elsif d == ?D\n R[0] -= 1\n elsif d == ?R\n R[1] += 1\n else\n R[1] -= 1\n end\n \n break if R[0] < 0 || n <= R[0] || R[1] < 0 || m <= R[1] || maze[R[0]][R[1]] == ?#\n \n if R == G\n ans += 1\n break\n end\n end\nend\n\nputs ans"}, {"source_code": "n, m = gets.split.map(&:to_i)\n\nmaze = Array.new(n)\nn.times do |i|\n maze[i] = gets.chomp\n if maze[i].include?(?S)\n S = [i, maze[i].index(?S)]\n end\n if maze[i].include?(?E)\n G = [i, maze[i].index(?E)]\n end\nend\n\n# p [n,m]\n# p [S, G]\n\ns = gets.chomp\n\nmaze\n\nans = 0\n%w(U D R L).permutation(4) do |a, b, c, d| \n \n R = S.dup\n # is_next = false\n \n i = [a, b, c, d]\n \n s.each_char do |c|\n \n d = i[c.to_i]\n \n if d == ?U\n R[0] += 1\n elsif d == ?D\n R[0] -= 1\n elsif d == ?R\n R[1] += 1\n else\n R[1] -= 1\n end\n \n if R[0] < 0 || n <= R[0] || R[1] < 0 || m <= R[1] || maze[R[0]][R[1]] == ?#\n # is_next = true\n break\n end\n \n if R == G\n ans += 1\n break\n end\n end\n \n # next if is_next\n # p R\n # if G == R\n # ans += 1\n # end\nend\n\nputs ans\n\n\n"}], "negative_code": [], "src_uid": "8a1799f4ca028d48d5a12e8ac4b8bee1"} {"nl": {"description": "Igor has been into chess for a long time and now he is sick of the game by the ordinary rules. He is going to think of new rules of the game and become world famous.Igor's chessboard is a square of size n × n cells. Igor decided that simple rules guarantee success, that's why his game will have only one type of pieces. Besides, all pieces in his game are of the same color. The possible moves of a piece are described by a set of shift vectors. The next passage contains a formal description of available moves.Let the rows of the board be numbered from top to bottom and the columns be numbered from left to right from 1 to n. Let's assign to each square a pair of integers (x, y) — the number of the corresponding column and row. Each of the possible moves of the piece is defined by a pair of integers (dx, dy); using this move, the piece moves from the field (x, y) to the field (x + dx, y + dy). You can perform the move if the cell (x + dx, y + dy) is within the boundaries of the board and doesn't contain another piece. Pieces that stand on the cells other than (x, y) and (x + dx, y + dy) are not important when considering the possibility of making the given move (for example, like when a knight moves in usual chess).Igor offers you to find out what moves his chess piece can make. He placed several pieces on the board and for each unoccupied square he told you whether it is attacked by any present piece (i.e. whether some of the pieces on the field can move to that cell). Restore a possible set of shift vectors of the piece, or else determine that Igor has made a mistake and such situation is impossible for any set of shift vectors.", "input_spec": "The first line contains a single integer n (1 ≤ n ≤ 50). The next n lines contain n characters each describing the position offered by Igor. The j-th character of the i-th string can have the following values: o — in this case the field (i, j) is occupied by a piece and the field may or may not be attacked by some other piece; x — in this case field (i, j) is attacked by some piece; . — in this case field (i, j) isn't attacked by any piece. It is guaranteed that there is at least one piece on the board.", "output_spec": "If there is a valid set of moves, in the first line print a single word 'YES' (without the quotes). Next, print the description of the set of moves of a piece in the form of a (2n - 1) × (2n - 1) board, the center of the board has a piece and symbols 'x' mark cells that are attacked by it, in a format similar to the input. See examples of the output for a full understanding of the format. If there are several possible answers, print any of them. If a valid set of moves does not exist, print a single word 'NO'.", "sample_inputs": ["5\noxxxx\nx...x\nx...x\nx...x\nxxxxo", "6\n.x.x..\nx.x.x.\n.xo..x\nx..ox.\n.x.x.x\n..x.x.", "3\no.x\noxx\no.x"], "sample_outputs": ["YES\n....x....\n....x....\n....x....\n....x....\nxxxxoxxxx\n....x....\n....x....\n....x....\n....x....", "YES\n...........\n...........\n...........\n....x.x....\n...x...x...\n.....o.....\n...x...x...\n....x.x....\n...........\n...........\n...........", "NO"], "notes": "NoteIn the first sample test the piece is a usual chess rook, and in the second sample test the piece is a usual chess knight."}, "positive_code": [{"source_code": "n = gets.to_i\nboard = Array.new\npieces = Array.new\nempties = Array.new\nn.times do |i|\n board[i] = gets.strip\n n.times do |j|\n if board[i][j] == \"o\"\n pieces.push [i, j]\n elsif board[i][j] == \".\"\n empties.push [i, j]\n end\n end\nend\n\nmoves = Array.new\n(2*n-1).times do |i|\n moves.push \"x\"*(2*n-1)\nend\nmoves[n-1][n-1] = \"o\"\npieces.each do |piece|\n empties.each do |empty|\n dr = empty[0] - piece[0]\n dc = empty[1] - piece[1]\n moves[n-1+dr][n-1+dc] = \".\"\n end\nend\n\npossible = true\nn.times do |i|\n n.times do |j|\n if board[i][j] == \"x\"\n attacked = false\n pieces.each do |piece|\n dr = i - piece[0]\n dc = j - piece[1]\n attacked = true if moves[n-1+dr][n-1+dc] == \"x\"\n end\n possible = false if not attacked\n end\n end\nend\n\nif possible\n puts \"YES\"\n puts moves\nelse\n puts \"NO\"\nend\n"}], "negative_code": [], "src_uid": "2fad1534434f042214dcd1f1dc75405a"} {"nl": {"description": "Haiku is a genre of Japanese traditional poetry.A haiku poem consists of 17 syllables split into three phrases, containing 5, 7 and 5 syllables correspondingly (the first phrase should contain exactly 5 syllables, the second phrase should contain exactly 7 syllables, and the third phrase should contain exactly 5 syllables). A haiku masterpiece contains a description of a moment in those three phrases. Every word is important in a small poem, which is why haiku are rich with symbols. Each word has a special meaning, a special role. The main principle of haiku is to say much using a few words.To simplify the matter, in the given problem we will consider that the number of syllable in the phrase is equal to the number of vowel letters there. Only the following letters are regarded as vowel letters: \"a\", \"e\", \"i\", \"o\" and \"u\".Three phases from a certain poem are given. Determine whether it is haiku or not.", "input_spec": "The input data consists of three lines. The length of each line is between 1 and 100, inclusive. The i-th line contains the i-th phrase of the poem. Each phrase consists of one or more words, which are separated by one or more spaces. A word is a non-empty sequence of lowercase Latin letters. Leading and/or trailing spaces in phrases are allowed. Every phrase has at least one non-space character. See the example for clarification.", "output_spec": "Print \"YES\" (without the quotes) if the poem is a haiku. Otherwise, print \"NO\" (also without the quotes).", "sample_inputs": ["on codeforces \nbeta round is running\n a rustling of keys", "how many gallons\nof edo s rain did you drink\n cuckoo"], "sample_outputs": ["YES", "NO"], "notes": null}, "positive_code": [{"source_code": "c = Array.new\n3.times do |i|\n\ts = STDIN.gets\n\tc[i] = s.count('aiueo')\nend\nif c[0] == 5 && c[1] == 7 && c[2] == 5 then\n\tprint \"YES\"\nelse\n\tprint \"NO\"\nend\n"}, {"source_code": "v = 'aeiou'\nif gets.count(v) == 5 && gets.count(v) == 7 && gets.count(v) == 5\n puts 'YES'\nelse\n puts 'NO'\nend"}, {"source_code": "puts $<.map{|_|_.count\"aeiuo\"}*\"\"==\"575\"?\"YES\":\"NO\"\n"}, {"source_code": "[5, 7, 5].each { |c|\n unless gets.chomp.scan(/[aeiou]/).size == c\n puts 'NO'\n exit\n end\n}\nputs 'YES'"}, {"source_code": "i=j=k=0\ngets.chomp.gsub(/[aeiou]/){i+=1}\ngets.chomp.gsub(/[aeiou]/){j+=1}\ngets.chomp.gsub(/[aeiou]/){k+=1}\nputs (i==5 and j==7 and k==5)?\"YES\":\"NO\""}, {"source_code": "puts $<.map{|_|_.count\"aeiuo\"}*\"\"==\"575\"?\"YES\":\"NO\""}, {"source_code": "input, heiku = STDIN.read.split(\"\\n\"), [5, 7, 5]\ninput.each_with_index { |v, k| (print :NO; exit) if v.count(\"aeiou\") != heiku[k] }\nprint :YES\n"}, {"source_code": "puts $<.map{|_|_.count\"aeiuo\"}*\"\"==\"575\"?\"YES\":\"NO\"\n"}, {"source_code": "a=3.times.map{gets.count('aeiou')}\nputs a==[5,7,5]?:YES:\"NO\"\n"}, {"source_code": "a = 3.times.map{gets.count('aeiou')}\nputs a == [5, 7, 5] ? :YES : :NO\n"}, {"source_code": "n = []\n3.times do\nn.push gets.tr('^aeiou', '').size\nend\nprint n == [5, 7, 5] ? \"YES\" : \"NO\"\n"}, {"source_code": "a=[gets,gets,gets]\nl=[]\na.each{ |s|\n l.push(s.length)\n}\nfor i in 0..2\n a[i]=a[i].gsub(\"a\",\"\")\n a[i]=a[i].gsub(\"o\",\"\")\n a[i]=a[i].gsub(\"u\",\"\")\n a[i]=a[i].gsub(\"i\",\"\")\n a[i]=a[i].gsub(\"e\",\"\")\nend\nif l[0]-a[0].length==5 && l[1]-a[1].length==7 && l[2]-a[2].length==5 then\n puts \"YES\"\nelse\n puts \"NO\"\nend\n"}, {"source_code": "def function\nstrings=Array.new(3)\ncounters=Array.new(3)\n3.times do |i|\nstrings[i]=gets.chomp\nend\nt=/[aeiou]/\nif strings[0].scan(t).length==5 && strings[1].scan(t).length==7 && strings[2].scan(t).length==5\nprint \"YES\"\nelse\nprint \"NO\"\nend\nend\nfunction"}, {"source_code": "puts $<.map{|_|_.count\"aeiuo\"}*\"\"==\"575\"?\"YES\":\"NO\"\n"}, {"source_code": "puts $<.map{|_|_.count\"aeiuo\"}*\"\"==\"575\"?\"YES\":\"NO\"\n"}, {"source_code": "puts $<.map{|_|_.count\"aeiuo\"}*\"\"==\"575\"?\"YES\":\"NO\""}, {"source_code": "a = gets.chomp\nb = gets.chomp\nc = gets.chomp\nVowels = ['a', 'e', 'i', 'o', 'u']\na_s = 0\nfor char in a.split('')\n\ta_s += 1 if Vowels.include?char\nend\nb_s = 0\nfor char in b.split('')\n\tb_s += 1 if Vowels.include?char\nend\nc_s = 0\nfor char in c.split('')\n\tc_s += 1 if Vowels.include?char\nend\nif a_s == 5 && b_s == 7 && c_s == 5\n\tputs \"YES\"\nelse\n\tputs \"NO\"\nend"}, {"source_code": "puts $<.map{|_|_.count\"aeiuo\"}*\"\"==\"575\"?\"YES\":\"NO\""}, {"source_code": "a=gets.chomp.split('')\nb=gets.chomp.split('')\nc=gets.chomp.split('')\nx=0\ny=0\nz=0\na.each do |i|\n\tif i=='a' || i=='e' || i=='i' || i=='o' || i=='u'\n\t\tx+=1\n\tend\nend\nb.each do |i|\n\tif i=='a' || i=='e' || i=='i' || i=='o' || i=='u'\n\t\ty+=1\n\tend\nend\nc.each do |i|\n\tif i=='a' || i=='e' || i=='i' || i=='o' || i=='u'\n\t\tz+=1\n\tend\nend\nif x==5 && y==7 && z==5\n\tputs \"YES\"\nelse\n\tputs \"NO\"\nend"}, {"source_code": "a = readlines.map(&:strip)\nif a[0].gsub(/[aeiou]/).to_a.size == 5 and a[1].gsub(/[aeiou]/).to_a.size == 7 and a[2].gsub(/[aeiou]/).to_a.size == 5\n puts :YES\nelse\n puts :NO\nend\n"}, {"source_code": "#!/usr/bin/env ruby\n# -*- coding: utf-8 -*-\n\na = 3.times.map{ gets.count('aeiou') }\nputs a == [5,7,5] ? \"YES\" : \"NO\"\n\n"}, {"source_code": "puts $<.map{|_|_.count\"aeiuo\"}*\"\"==\"575\"?\"YES\":\"NO\"\n"}, {"source_code": "puts $<.map{|_|_.count\"aeiuo\"}*\"\"==\"575\"?\"YES\":\"NO\"\n"}, {"source_code": "str = gets.split\n\nfirst = 0\nsecond = 0\nthird = 0\n\nstr.each{|s|\n loop{\n if s =~ /[aiueo]/\n s.sub!($&, \"\")\n first += 1\n else\n break\n end\n }\n}\n\nstr = gets.split\n\nstr.each{|s|\n loop{\n if s =~ /[aiueo]/\n s.sub!($&, \"\")\n second += 1\n else\n break\n end\n }\n}\n\nstr = gets.split\n\nstr.each{|s|\n loop{\n if s =~ /[aiueo]/\n s.sub!($&, \"\")\n third += 1\n else\n break\n end\n }\n}\n\nif first == 5\n if second == 7\n if third == 5\n puts \"YES\"\n exit\n end\n end\nend\n\nputs \"NO\"\n\n"}, {"source_code": "a = gets().chomp\nb = gets().chomp\nc = gets().chomp\n\ndef check( c )\n if( c == \"a\" || c == \"i\" || c == \"u\" || c == \"e\" || c == \"o\" )\n return true\n else\n return false\n end\nend\n\naa = 0\na.split(//).each{|chr| aa += 1 if(check(chr)) }\n\nbb = 0\nb.split(//).each{|chr| bb += 1 if(check(chr)) }\n\ncc = 0\nc.split(//).each{|chr| cc += 1 if(check(chr)) }\n\nif( aa == 5 && bb == 7 && cc == 5 )\n puts \"YES\"\nelsif\n puts \"NO\"\nend\n\n"}, {"source_code": "puts $<.map{|_|_.count\"aeiuo\"}*\"\"==\"575\"?\"YES\":\"NO\""}, {"source_code": "puts readlines.collect{|line| line.chomp.scan(/[aeiou]/).size}==[5,7,5]?\"YES\":\"NO\"\n"}, {"source_code": "puts readlines.map{|l|l.count(\"aeiou\")}==[5,7,5]?\"YES\":\"NO\"\n"}, {"source_code": "puts readlines.collect{|l| l.scan(/[aeiou]/).size}==[5,7,5]?\"YES\":\"NO\"\n"}, {"source_code": "puts $<.map{|l|l.count\"aeiou\"}*\"\"==\"575\"?:YES:\"NO\"\n"}, {"source_code": "puts $<.map{|_|_.count\"aeiuo\"}*\"\"==\"575\"?\"YES\":\"NO\"\n"}, {"source_code": "a = [ gets.count(\"aeiou\"), gets.count(\"aeiou\"), gets.count(\"aeiou\") ]\nputs a==[5,7,5] ?\"YES\":\"NO\""}, {"source_code": "phrases = Array.new\nphrases.push(gets.split(\"\"))\nphrases.push(gets.split(\"\"))\nphrases.push(gets.split(\"\"))\nif phrases[0].count(\"a\") + phrases[0].count(\"e\") + phrases[0].count(\"i\") + phrases[0].count(\"o\") + phrases[0].count(\"u\") == 5 and\\\n phrases[1].count(\"a\") + phrases[1].count(\"e\") + phrases[1].count(\"i\") + phrases[1].count(\"o\") + phrases[1].count(\"u\") == 7 and\\\n phrases[2].count(\"a\") + phrases[2].count(\"e\") + phrases[2].count(\"i\") + phrases[2].count(\"o\") + phrases[2].count(\"u\") == 5\n puts \"YES\"\nelse\n puts \"NO\"\nend\n"}], "negative_code": [{"source_code": "v = 'aeiou'\nif gets.count(v) == 5 || gets.count(v) == 7 || gets.count(v) == 5\n puts 'YES'\nelse\n puts 'NO'\nend"}], "src_uid": "46d734178b3acaddf2ee3706f04d603d"} {"nl": {"description": "Sean is trying to save a large file to a USB flash drive. He has n USB flash drives with capacities equal to a1, a2, ..., an megabytes. The file size is equal to m megabytes. Find the minimum number of USB flash drives needed to write Sean's file, if he can split the file between drives.", "input_spec": "The first line contains positive integer n (1 ≤ n ≤ 100) — the number of USB flash drives. The second line contains positive integer m (1 ≤ m ≤ 105) — the size of Sean's file. Each of the next n lines contains positive integer ai (1 ≤ ai ≤ 1000) — the sizes of USB flash drives in megabytes. It is guaranteed that the answer exists, i. e. the sum of all ai is not less than m.", "output_spec": "Print the minimum number of USB flash drives to write Sean's file, if he can split the file between drives.", "sample_inputs": ["3\n5\n2\n1\n3", "3\n6\n2\n3\n2", "2\n5\n5\n10"], "sample_outputs": ["2", "3", "1"], "notes": "NoteIn the first example Sean needs only two USB flash drives — the first and the third.In the second example Sean needs all three USB flash drives.In the third example Sean needs only one USB flash drive and he can use any available USB flash drive — the first or the second."}, "positive_code": [{"source_code": "a=(1..gets.to_i)\nb=gets.to_i\na=a.map{gets.to_i}.sort\nc=0\nwhile b > 0\n c += 1\n b -= a.pop\nend\np c"}, {"source_code": "n,x,*a=$<.map &:to_i;a=a.sort.reverse\np (1..n).find{|i|(x-=a[i-1])<=0}"}, {"source_code": "n = gets.chomp.to_i\nm = gets.chomp.to_i\na = []\nfor i in 1..n do\n a.push(gets.chomp.to_i)\nend\n\na = a.sort.reverse\ncount = 0\nwhile m > 0 do\n m -= a[count]\n count += 1\nend\n\nputs count\n"}, {"source_code": "n,x,*a=$<.map &:to_i;a=a.sort.reverse\np (1..n).find{|i|(x-=a[i-1])<=0}\n"}, {"source_code": "n,x,*a=$<.map &:to_i;a=a.sort.reverse\np (1..n).find{|i|(x-=a[i-1])<=0}\n"}, {"source_code": "#! ruby\n# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n############################################################\ndef gs() gets.chomp end\ndef gi() gets.to_i end\ndef gss() gs.split end\ndef gis() gss.map(&:to_i) end\ndef nmapf(n,f) n.times.map{ __send__ f } end\ndef arr2d(h,w,v=0) h.times.map{[v] * w} end\ndef ngs(n) nmapf n,:gs end\ndef ngi(n) nmapf n,:gi end\ndef ngss(n) nmapf n,:gss end\ndef ngis(n) nmapf n,:gis end\ndef for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end\ndef nsum(n) n * (n + 1) / 2 end\n############################################################\n\nn, m = ngi 2\na = ngi(n).sort.reverse\nc = 0\ns = 0\na.each do |v|\n s += v\n c += 1\n break if s >= m\nend\nputs c\n\n"}, {"source_code": "n,x,*a=$<.map &:to_i;a=a.sort.reverse\np (1..n).find{|i|(x-=a[i-1])<=0}\n"}, {"source_code": "n = gets.chomp.to_i\nm = gets.chomp.to_i\narr = Array.new(n)\nn.times do |i|\n arr[i] = gets.chomp.to_i\nend\narr = arr.sort ; arr = arr.reverse\nsum = 0 ; res = 0\nwhile sum < m\n sum+=arr[0] ; arr.shift ; res+=1\nend\nputs res"}, {"source_code": "n=gets.chomp.to_i\nsize=gets.chomp.to_i\nfile_arry=[]\n0.upto(n-1) do |i|\n\tfile_arry[i]=gets.chomp.to_i\nend\nfile_arry=file_arry.sort.reverse\nk=0\noutput=0\nloop do\n\tk=k+file_arry[output]\n\toutput+=1\n\tbreak if k>=size\nend\nputs output\t\n"}, {"source_code": "n = gets.to_i\nm = gets.to_i\nas = []\nn.times do\n as << gets.to_i\nend\nas.sort!.reverse!\np = 0\nwhile m > 0 do\n m -= as[p]\n p += 1\nend\nputs p\n"}, {"source_code": "n,x,*a=$<.map &:to_i;a=a.sort.reverse\np (1..n).find{|i|(x-=a[i-1])<=0}\n"}, {"source_code": "n=gets.chomp.to_i\nsize=gets.chomp.to_i\nfile_arry=[]\n0.upto(n-1) do |i|\n\tfile_arry[i]=gets.chomp.to_i\nend\nfile_arry=file_arry.sort.reverse\nk=0\noutput=0\nloop do\n\tk=k+file_arry[output]\n\toutput+=1\n\tbreak if k>=size\nend\nputs output"}, {"source_code": "n=gets.chomp.to_i\nsize=gets.chomp.to_i\n\ndata=[]\n\na=0\nwhile a=size\n\t\tbreak\n\tend\nend\n\nputs x\n"}, {"source_code": "n=gets.to_i\nm=gets.to_i\nxs=[]\nn.times{\n\txs<<(gets.to_i)\n}\nans=0\nxs.sort.reverse.each{|e|\n\tm-=e\n\tans+=1\n\tbreak if m<=0 \n}\nputs ans"}, {"source_code": "#!usr/bin/env ruby\n\na = []\nn = gets.to_i\nm = gets.to_i\nn.times {\n a << gets.to_i\n}\n\na.sort!\na.reverse!\n\nb = 0\nc = 0\nwhile b < m\n c += 1\n b += a.shift\nend\n\nputs c\n"}, {"source_code": "t = gets.to_i\nm = gets.to_i\narr1 = Array.new(t) \nindex=0\nt.times do\n\tarr1[index] = gets.to_i\n\tindex+=1\nend\n\narr1.sort!\nsum=m\nindex=t-1\ncount=0\nwhile m>0\n\tcount+=1\n\tm = m - arr1[index]\n\tindex-=1\nend\nputs count"}, {"source_code": "# coding: utf-8\n\nn = gets.to_i\nm = gets.to_i\nar = []\nn.times {\n ar << gets.to_i\n}\nar.sort!\nsticks = 0\nwhile m > 0\n cap = ar.pop\n sticks += 1\n m -= cap\nend\nputs sticks"}, {"source_code": "n,x,*a=$<.map &:to_i;a=a.sort.reverse\np (1..n).find{|i|(x-=a[i-1])<=0}\n"}, {"source_code": "carts = gets.to_i\nfile_mb = gets.to_i\n\ncart = Array.new(carts)\n\nfor i in 0..carts-1\n\tcart[i]= gets.chomp.to_i\nend\n\ncart = cart.sort.reverse\n\nsum = 0\nnum = 0\nfor i in 0..carts-1\n\tsum += cart[i]\n\tnum +=1\n\tif sum >= file_mb\n\t\tputs num\n\t\texit\n\tend\nend"}, {"source_code": "n = gets.to_i\nm = gets.to_i\na = []\n(1..n).each do\n a.push gets.to_i\nend\n\na.sort!\nt = 0\ntmp = 0\n(0...n).each do |i|\n tmp +=a[n-i-1]\n t +=1\n break if tmp >= m\nend\n\nputs t\n"}, {"source_code": "n = gets.chomp.to_i\nm = gets.chomp.to_i\na = []\nn.times do\n a << gets.chomp.to_i\nend\nres = 0\na.sort.reverse.each do |i|\n break if m <= 0\n m -= i\n res += 1\nend\nputs res\n"}, {"source_code": "n = gets.to_i\nm = gets.to_i\na = Array.new(n)\nfor i in 0..n-1\n a[i] = gets.to_i\nend\na = a.sort {|x,y| y <=> x}\nk = 0\nfor item in a\n if m <= 0\n break\n end\n k += 1\n m -= item\nend\nputs k\n \n"}, {"source_code": "n = gets.to_i\nm = gets.to_i\narr = []\nfor i in 0..n-1\n arr[i] = gets.to_i\nend\n\narr = arr.sort\ncount = 0\nidx = n-1\nwhile m > 0\n m -= arr[idx]\n idx -= 1\n count += 1\nend\n\nputs count\n"}, {"source_code": "n=gets.chomp.to_i\nsize=gets.chomp.to_i\nfile_arry=[]\n0.upto(n-1) do |i|\n\tfile_arry[i]=gets.chomp.to_i\nend\nfile_arry=file_arry.sort.reverse\nk=0\noutput=0\nloop do\n\tk=k+file_arry[output]\n\toutput+=1\n\tbreak if k>=size\nend\nputs output\t\n"}, {"source_code": "n = gets.to_i\nm = gets.to_i\na = []\nn.times { a << gets.to_i }\na.sort!.reverse!\ns = 0\n(0...a.size).each do |i|\n s += a[i]\n if s >= m\n puts i + 1\n exit\n end\nend\n"}, {"source_code": "\nn = gets.chomp.to_i\nm = gets.chomp.to_i\n\nflash_drives = Array.new\nn.times {|i| flash_drives << gets.chomp.to_i}\n\nflash_drives.sort!\n\nk = 0\nwhile m > 0 do \n\tm = m - flash_drives[n - k - 1]\n\tk = k + 1\nend\n\nprint k\n"}, {"source_code": "n,x,*a=$<.map &:to_i;a=a.sort.reverse\np (1..n).find{|i|(x-=a[i-1])<=0}\n"}, {"source_code": "t = gets.chomp\nt = t.to_i\nn = gets.chomp\nn = n.to_i\nv = []\nfor i in 1 ..t do\n v << gets.chomp\nend\nv = v.map(&:to_i)\nc=0\ns=0\nv.sort.reverse.each do |num|\ns = s+num\nif s >= n\nc=c+1\nbreak\nelse\nc=c+1\nend\nend\nputs c"}, {"source_code": "class Solver\n\tdef main\n\t\tn = gets.to_i\n\t\tm = gets.to_i\n\t\ta = []\n\t\tn.times do\n\t\t\ta << gets.to_i\n\t\tend\n\t\ta = a.sort.reverse\n\t\tans = 0\n\t\ta.each do |x|\n\t\t\tif m > 0\n\t\t\t\tm -= x\n\t\t\t\tans += 1\n\t\t\tend\n\t\tend\n\t\tputs ans\n\tend\nend\nSolver.new.main"}, {"source_code": "n,x,*a=$<.map &:to_i;a=a.sort.reverse\np (1..n).find{|i|(x-=a[i-1])<=0}\n"}], "negative_code": [{"source_code": "a=(1..gets.to_i).map{gets.to_i}.sort.reverse\nb=gets.to_i\nc=0\nwhile b > 0\n c += 1\n b -= a.shift\nend\np c"}, {"source_code": "n,x,*a=$<.map &:to_i;a.sort!\np (1..n).find{|i|(x-=a[i-1])<=0}"}, {"source_code": "n,x,*a=$<.map &:to_i\np (1..n).find{|i|(x-=a[i-1])<=0}"}, {"source_code": "input=gets.chomp.to_i\nif input>5 && input%2==0\nputs (input-2)/4\nelsif input>5 && input%2==1\nputs 0\nelse\nputs 0\nend\n\n\n\n\n"}], "src_uid": "a02a9e37a4499f9c86ac690a3a427466"} {"nl": {"description": "A telephone number is a sequence of exactly 11 digits, where the first digit is 8. For example, the sequence 80011223388 is a telephone number, but the sequences 70011223388 and 80000011223388 are not.You are given a string $$$s$$$ of length $$$n$$$, consisting of digits.In one operation you can delete any character from string $$$s$$$. For example, it is possible to obtain strings 112, 111 or 121 from string 1121.You need to determine whether there is such a sequence of operations (possibly empty), after which the string $$$s$$$ becomes a telephone number.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 \\le n \\le 100$$$) — the length of string $$$s$$$. The second line of each test case contains the string $$$s$$$ ($$$|s| = n$$$) consisting of digits.", "output_spec": "For each test print one line. If there is a sequence of operations, after which $$$s$$$ becomes a telephone number, print YES. Otherwise, print NO.", "sample_inputs": ["2\n13\n7818005553535\n11\n31415926535"], "sample_outputs": ["YES\nNO"], "notes": "NoteIn the first test case you need to delete the first and the third digits. Then the string 7818005553535 becomes 88005553535."}, "positive_code": [{"source_code": "t = gets.to_i \nt.times do \n\ts = gets.to_i \n\tn = gets \n\tflag = true\n\tfor e in 0...s\n\t\tif(n[e] == '8')\n\t\t\tif(s-e >= 11) \n\t\t\t\tputs(\"YES\")\n\t\t\t\tflag = false \n\t\t\t\tbreak \n\t\t\tend\n\t\tend\n\tend\n\tif(flag)\n\t\tputs(\"NO\")\n\tend\nend"}, {"source_code": "gets.to_i.times do\n d = gets.to_i\n num = gets.chomp.index('8')\n puts num && d - num >= 11 ? 'YES' : 'NO'\nend"}, {"source_code": "gets.to_i.times do\n gets\n puts (gets =~ /8\\d{10}/ ? :YES : :NO)\nend\n"}, {"source_code": "\nT = gets.to_i\nT.times do\n\tn = gets.to_i\n\tstr = gets.chomp\n\tif str.index('8')\n\t\tif str.size - str.index('8') >= 11\n\t\t\tputs \"YES\"\n\t\t\tnext\n\t\tend\n\tend\n\tputs \"NO\"\nend"}, {"source_code": "t = gets.to_i\nfor i in 0...t do\n n = gets.to_i\n s = gets.chomp\n flag = false\n for j in 0..(n-11) do\n if s[j] == '8'\n flag = true\n break\n end\n end\n puts flag ? \"YES\" : \"NO\"\nend\n"}, {"source_code": "for x in 1..gets.to_i\n len = gets.to_i\n str = gets\n idx = str.index('8')\n if idx.nil? || len - idx < 11\n puts \"NO\"\n else\n puts \"YES\"\n end\nend"}], "negative_code": [{"source_code": "t = gets.to_i \nt.times do \n\ts = gets.to_i \n\tn = gets \n\tfor e in 0...s\n\t\tflag = true\n\t\tif(n[e] == '8')\n\t\t\tif(s-e-1 >= 11) \n\t\t\t\tputs(\"YES\")\n\t\t\t\tflag = false \n\t\t\t\tbreak \n\t\t\tend\n\t\tend\n\tend\n\tif(!flag)\n\t\tputs(\"NO\")\n\tend\nend"}, {"source_code": "t = gets.to_i \nt.times do \n\ts = gets.to_i \n\tn = gets \n\tflag = true\n\tfor e in 0...s\n\t\tif(n[e] == '8')\n\t\t\tif(s-e-1 >= 11) \n\t\t\t\tputs(\"YES\")\n\t\t\t\tflag = false \n\t\t\t\tbreak \n\t\t\tend\n\t\tend\n\tend\n\tif(flag)\n\t\tputs(\"NO\")\n\tend\nend"}, {"source_code": "for x in 1..gets.to_i\n len = gets.to_i\n str = gets\n if str.index('8').nil? || len - str.index('8') < 10\n puts \"NO\"\n else\n puts \"YES\"\n end\nend"}, {"source_code": "cases = gets\nfor x in 1..cases.to_i\n len = gets.to_i\n str = gets\n if str.index('8').nil? || len - str.index('8') < 10\n puts \"NO\"\n else\n puts \"YES\"\n end\nend"}], "src_uid": "bcdd7862b718d6bcc25c9aba8716d487"} {"nl": {"description": "In some country live wizards. They love to ride trolleybuses.A city in this country has a trolleybus depot with n trolleybuses. Every day the trolleybuses leave the depot, one by one and go to the final station. The final station is at a distance of d meters from the depot. We know for the i-th trolleybus that it leaves at the moment of time ti seconds, can go at a speed of no greater than vi meters per second, and accelerate with an acceleration no greater than a meters per second squared. A trolleybus can decelerate as quickly as you want (magic!). It can change its acceleration as fast as you want, as well. Note that the maximum acceleration is the same for all trolleys.Despite the magic the trolleys are still powered by an electric circuit and cannot overtake each other (the wires are to blame, of course). If a trolleybus catches up with another one, they go together one right after the other until they arrive at the final station. Also, the drivers are driving so as to arrive at the final station as quickly as possible.You, as head of the trolleybuses' fans' club, are to determine for each trolley the minimum time by which it can reach the final station. At the time of arrival at the destination station the trolleybus does not necessarily have zero speed. When a trolley is leaving the depot, its speed is considered equal to zero. From the point of view of physics, the trolleybuses can be considered as material points, and also we should ignore the impact on the speed of a trolley bus by everything, except for the acceleration and deceleration provided by the engine.", "input_spec": "The first input line contains three space-separated integers n, a, d (1 ≤ n ≤ 105, 1 ≤ a, d ≤ 106) — the number of trolleybuses, their maximum acceleration and the distance from the depot to the final station, correspondingly. Next n lines contain pairs of integers ti vi (0 ≤ t1 < t2... < tn - 1 < tn ≤ 106, 1 ≤ vi ≤ 106) — the time when the i-th trolleybus leaves the depot and its maximum speed, correspondingly. The numbers in the lines are separated by spaces.", "output_spec": "For each trolleybus print a single line the time it arrives to the final station. Print the times for the trolleybuses in the order in which the trolleybuses are given in the input. The answer will be accepted if the absolute or relative error doesn't exceed 10 - 4.", "sample_inputs": ["3 10 10000\n0 10\n5 11\n1000 1", "1 2 26\n28 29"], "sample_outputs": ["1000.5000000000\n1000.5000000000\n11000.0500000000", "33.0990195136"], "notes": "NoteIn the first sample the second trolleybus will catch up with the first one, that will happen at distance 510.5 meters from the depot. The trolleybuses will go the remaining 9489.5 meters together at speed 10 meters per second. As a result, both trolleybuses will arrive to the final station by the moment of time 1000.5 seconds. The third trolleybus will not catch up with them. It will arrive to the final station by the moment of time 11000.05 seconds."}, "positive_code": [{"source_code": "#!/usr/bin/ruby\n\nn, a, d = $stdin.gets.chomp.split.map(&:to_i)\nprev = 0.0\nn.times do\n t, v = $stdin.gets.chomp.split.map(&:to_f)\n l = v*v/(2.0*a)\n x =\n if l < d\n u = v/a.to_f\n u + (d-l)/v.to_f\n else\n Math.sqrt(2.0*d/a.to_f)\n end\n ans = [x+t, prev].max\n printf \"%.5f\\n\", ans\n prev = ans\nend\n"}, {"source_code": "# ProblemC\nrequire 'bigdecimal'\nrequire 'bigdecimal/math'\ninclude BigMath\ninclude Math\nn, a, d = gets.split().map {|s| s.to_i}\n\nprev = 0.0\ntm = sqrt((2 * d) / a.to_f)\n\nn.times do\n t, v = gets.split().map {|s| s.to_i}\n ti = v / a.to_f\n tt = t.to_f\n if tm <= ti\n tt += tm\n else\n dm = (a * ti * ti) / 2.0\n tr = (d - dm) / v.to_f\n tt += tr + ti\n end\n if prev > tt\n puts prev\n else\n puts tt\n prev = tt \n end \nend\n\n"}], "negative_code": [{"source_code": "# ProblemC\ninclude Math\nn, a, d = gets.split().map {|s| s.to_i}\n\nprev = 0.0\ntm = sqrt((2 * d) / a.to_f)\n\nn.times do\n t, v = gets.split().map {|s| s.to_i}\n ti = v / a.to_f\n tt = t.to_f\n if tm <= ti\n tt += tm\n else\n dm = (a * ti * ti) / 2.0\n tr = (d - dm) / v.to_f\n tt += tr + ti\n end\n if prev > tt\n puts prev \n else\n puts tt\n end\n prev = tt\nend\n\n"}], "src_uid": "894f407ca706788b13571878da8570f5"} {"nl": {"description": "Maria participates in a bicycle race.The speedway takes place on the shores of Lake Lucerne, just repeating its contour. As you know, the lake shore consists only of straight sections, directed to the north, south, east or west.Let's introduce a system of coordinates, directing the Ox axis from west to east, and the Oy axis from south to north. As a starting position of the race the southernmost point of the track is selected (and if there are several such points, the most western among them). The participants start the race, moving to the north. At all straight sections of the track, the participants travel in one of the four directions (north, south, east or west) and change the direction of movement only in bends between the straight sections. The participants, of course, never turn back, that is, they do not change the direction of movement from north to south or from east to west (or vice versa).Maria is still young, so she does not feel confident at some turns. Namely, Maria feels insecure if at a failed or untimely turn, she gets into the water. In other words, Maria considers the turn dangerous if she immediately gets into the water if it is ignored.Help Maria get ready for the competition — determine the number of dangerous turns on the track.", "input_spec": "The first line of the input contains an integer n (4 ≤ n ≤ 1000) — the number of straight sections of the track. The following (n + 1)-th line contains pairs of integers (xi, yi) ( - 10 000 ≤ xi, yi ≤ 10 000). The first of these points is the starting position. The i-th straight section of the track begins at the point (xi, yi) and ends at the point (xi + 1, yi + 1). It is guaranteed that: the first straight section is directed to the north; the southernmost (and if there are several, then the most western of among them) point of the track is the first point; the last point coincides with the first one (i.e., the start position); any pair of straight sections of the track has no shared points (except for the neighboring ones, they share exactly one point); no pair of points (except for the first and last one) is the same; no two adjacent straight sections are directed in the same direction or in opposite directions. ", "output_spec": "Print a single integer — the number of dangerous turns on the track.", "sample_inputs": ["6\n0 0\n0 1\n1 1\n1 2\n2 2\n2 0\n0 0", "16\n1 1\n1 5\n3 5\n3 7\n2 7\n2 9\n6 9\n6 7\n5 7\n5 3\n4 3\n4 4\n3 4\n3 2\n5 2\n5 1\n1 1"], "sample_outputs": ["1", "6"], "notes": "NoteThe first sample corresponds to the picture: The picture shows that you can get in the water under unfortunate circumstances only at turn at the point (1, 1). Thus, the answer is 1."}, "positive_code": [{"source_code": "def danger(x, y, xx, yy, xxx, yyy)\n dx, dy, ddx, ddy = xx - x, yy - y, xxx - xx, yyy - yy\n return ddx * dy - ddy * dx < 0\nend\n\nn = readline.to_i\nx, y = readline.split(' ').collect(&:to_i)\nxx, yy = readline.split(' ').collect(&:to_i)\nret = 0\n(n - 1).times do\n xxx, yyy = readline.split(' ').collect(&:to_i)\n ret += 1 if danger(x, y, xx, yy, xxx, yyy)\n x, y, xx, yy = xx, yy, xxx, yyy\nend\n\nputs ret\n"}, {"source_code": "def get_dir(dx, dy)\n\treturn 0 if dy>0\n\treturn 2 if dy<0\n\treturn 1 if dx>0\n\treturn 3 if dx<0\nend\n\ndef calc(dir, last_dir)\n\treturn 1 if last_dir == (dir+1)%4\n\treturn 0\nend\n\nn=gets.to_i\np=Array.new\nans=dir=last_dir=0\nfor i in 0..n\n\tbuf=gets.split(' ')\n\tp[i] = [buf[0].to_i, buf[1].to_i]\n\tdir = get_dir(p[i][0]-p[i-1][0], p[i][1] - p[i-1][1]) if i>0\n\tans += calc(dir, last_dir) if i>1\n\tlast_dir = dir\nend\nputs ans"}], "negative_code": [], "src_uid": "0054f9e2549900487d78fae9aa4c2d65"} {"nl": {"description": "Given a set of integers (it can contain equal elements).You have to split it into two subsets $$$A$$$ and $$$B$$$ (both of them can contain equal elements or be empty). You have to maximize the value of $$$mex(A)+mex(B)$$$.Here $$$mex$$$ of a set denotes the smallest non-negative integer that doesn't exist in the set. For example: $$$mex(\\{1,4,0,2,2,1\\})=3$$$ $$$mex(\\{3,3,2,1,3,0,0\\})=4$$$ $$$mex(\\varnothing)=0$$$ ($$$mex$$$ for empty set) The set is splitted into two subsets $$$A$$$ and $$$B$$$ if for any integer number $$$x$$$ the number of occurrences of $$$x$$$ into this set is equal to the sum of the number of occurrences of $$$x$$$ into $$$A$$$ and the number of occurrences of $$$x$$$ into $$$B$$$.", "input_spec": "The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1\\leq t\\leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1\\leq n\\leq 100$$$) — the size of the set. The second line of each testcase contains $$$n$$$ integers $$$a_1,a_2,\\dots a_n$$$ ($$$0\\leq a_i\\leq 100$$$) — the numbers in the set.", "output_spec": "For each test case, print the maximum value of $$$mex(A)+mex(B)$$$.", "sample_inputs": ["4\n6\n0 2 1 5 0 1\n3\n0 1 2\n4\n0 2 0 1\n6\n1 2 3 4 5 6"], "sample_outputs": ["5\n3\n4\n0"], "notes": "NoteIn the first test case, $$$A=\\left\\{0,1,2\\right\\},B=\\left\\{0,1,5\\right\\}$$$ is a possible choice.In the second test case, $$$A=\\left\\{0,1,2\\right\\},B=\\varnothing$$$ is a possible choice.In the third test case, $$$A=\\left\\{0,1,2\\right\\},B=\\left\\{0\\right\\}$$$ is a possible choice.In the fourth test case, $$$A=\\left\\{1,3,5\\right\\},B=\\left\\{2,4,6\\right\\}$$$ is a possible choice."}, "positive_code": [{"source_code": "gets.to_i.times do\n gets\n a = gets.split(\" \").map(&:to_i)\n\n x = 0\n 1000.times do |i|\n if a.include? i\n a[a.index(i)] = -999\n else\n x = i\n break\n end\n end\n\n y = 0\n 1000.times do |i|\n if a.include? i\n a[a.index(i)] = -999\n else\n y = i\n break\n end\n end\n\n p x+y\nend\n"}, {"source_code": "gets.to_i.times{gets\nt=gets.split.map &:to_i\np [1,2].map{|x|(0..t.max+1).map{|x|t.count x}.find_index{|y|y= 2\n\n if counter[x] == 1\n vals[0] = x + 1\n vals[1] = x if vals[1] == -1\n else\n vals[0] = x\n vals[1] = x if vals[1] == -1\n\n break\n end\n end\n\n puts vals.sum\nend\n"}], "negative_code": [], "src_uid": "e26b0b117593c159b7f01cfac66a71d1"} {"nl": {"description": "Today on a lecture about strings Gerald learned a new definition of string equivalency. Two strings a and b of equal length are called equivalent in one of the two cases: They are equal. If we split string a into two halves of the same size a1 and a2, and string b into two halves of the same size b1 and b2, then one of the following is correct: a1 is equivalent to b1, and a2 is equivalent to b2 a1 is equivalent to b2, and a2 is equivalent to b1 As a home task, the teacher gave two strings to his students and asked to determine if they are equivalent.Gerald has already completed this home task. Now it's your turn!", "input_spec": "The first two lines of the input contain two strings given by the teacher. Each of them has the length from 1 to 200 000 and consists of lowercase English letters. The strings have the same length.", "output_spec": "Print \"YES\" (without the quotes), if these two strings are equivalent, and \"NO\" (without the quotes) otherwise.", "sample_inputs": ["aaba\nabaa", "aabb\nabab"], "sample_outputs": ["YES", "NO"], "notes": "NoteIn the first sample you should split the first string into strings \"aa\" and \"ba\", the second one — into strings \"ab\" and \"aa\". \"aa\" is equivalent to \"aa\"; \"ab\" is equivalent to \"ba\" as \"ab\" = \"a\" + \"b\", \"ba\" = \"b\" + \"a\".In the second sample the first string can be splitted into strings \"aa\" and \"bb\", that are equivalent only to themselves. That's why string \"aabb\" is equivalent only to itself and to string \"bbaa\"."}, "positive_code": [{"source_code": "def min_rpst(x)\n\tif x.length.even?\n\t\tm = x.length / 2\n\t\tp = x[0...m]\n\t\tq = x[m..-1]\n\t\tp = min_rpst p\n\t\tq = min_rpst q\n\t\tx = p < q ? p + q : q + p\n\tend\n\tx\nend\n\ns1 = gets.strip\ns2 = gets.strip\nputs (min_rpst(s1) == min_rpst(s2)) ? \"YES\" : \"NO\"\n"}, {"source_code": "$MOD = 1000000000000000003\n\ndef pow2?(n)\n return true if n == 0 || n == 1\n return false if n < 0 || n % 2 == 1\n return pow2?(n / 2)\nend\n\ndef get_hash(s)\n #p s\n return s[0].ord if s.size == 1\n m = s.size / 2\n h1 = get_hash(s[0..m-1])\n h2 = get_hash(s[m..2 * m])\n return ((h1 + h2) * h1 * h2) % $MOD\nend\n\ndef eq(s1,s2)\n return true if (s1 == s2) \n return false if (s1.size != s2.size) || (s1.size % 2 == 1)\n if pow2?(s1.size)\n return get_hash(s1) == get_hash(s2)\n end\n\n m = s1.size / 2\n s11 = s1[0..m - 1]\n s12 = s1[m, 2 * m]\n s21 = s2[0..m - 1]\n s22 = s2[m, 2 * m]\n return true if eq(s11,s21) && eq(s12,s22) || eq(s12,s21) && eq(s11,s22)\n return false\nend\n\n\ns1 = gets.strip\ns2 = gets.strip\nputs (eq(s1,s2) ? 'YES' : 'NO')\n\n\n\n\n"}], "negative_code": [{"source_code": "\ndef eq_hash(s1,s2)\n base = 7\n mod = 1000000000000000003\n return true if s1 == s2\n return false if (s1.size % 2 == 1) || (s2.size % 2 == 1) || \n (s1.size != s2.size)\n hash1 = 0\n hash2 = 0\n s1.size.times do |i|\n hash1 += base ** s1[i].ord\n hash2 += base ** s2[i].ord\n hash1 %= mod\n hash2 %= mod\n end\n\n return hash1 == hash2\nend\n\n#p prime? gets.to_i\n#100.times {|i| p i if prime? i }\n#n = 10 ** 18 + 1\n#while !prime? n do\n# n += 2\n#end\n#p n \ns1 = gets.strip\ns2 = gets.strip\np (eq_hash(s1,s2) ? \"YES\" : \"NO\")\n\n"}, {"source_code": "\n# def prime?(n)\n# return true if n == 2 \n# return false if n < 2 || (n % 2 == 0)\n# i = 3\n# while i * i <= n do\n# \treturn false if n % i == 0\n# \ti += 2\n# end\n# return true\n# end\n\ndef eq_hash(s1,s2)\n base = 7\n mod = 1000000000000000003\n return true if s1 == s2\n return false if (s1.size % 2 == 1) || (s2.size % 2 == 1) || \n\t (s1.size != s2.size)\n hash1 = 0\n hash2 = 0\n s1.size.times do |i|\n\t hash1 += base ** s1[i].ord\n\t hash2 += base ** s2[i].ord\n\t hash1 %= mod\n\t hash2 %= mod\n end\n\n return hash1 == hash2\nend\n\n#p prime? gets.to_i\n#100.times {|i| p i if prime? i }\n#n = 10 ** 18 + 1\n#while !prime? n do\n# n += 2\n#end\n#p n \ns1 = gets.strip\ns2 = gets.strip\nputs (eq_hash(s1,s2) ? 'YES' : 'NO')\n\n\n\n\n"}], "src_uid": "21c0e12347d8be7dd19cb9f43a31be85"} {"nl": {"description": "It is raining heavily. But this is the first day for Serval, who just became 3 years old, to go to the kindergarten. Unfortunately, he lives far from kindergarten, and his father is too busy to drive him there. The only choice for this poor little boy is to wait for a bus on this rainy day. Under such circumstances, the poor boy will use the first bus he sees no matter where it goes. If several buses come at the same time, he will choose one randomly.Serval will go to the bus station at time $$$t$$$, and there are $$$n$$$ bus routes which stop at this station. For the $$$i$$$-th bus route, the first bus arrives at time $$$s_i$$$ minutes, and each bus of this route comes $$$d_i$$$ minutes later than the previous one.As Serval's best friend, you wonder which bus route will he get on. If several buses arrive at the same time, you can print any of them.", "input_spec": "The first line contains two space-separated integers $$$n$$$ and $$$t$$$ ($$$1\\leq n\\leq 100$$$, $$$1\\leq t\\leq 10^5$$$) — the number of bus routes and the time Serval goes to the station. Each of the next $$$n$$$ lines contains two space-separated integers $$$s_i$$$ and $$$d_i$$$ ($$$1\\leq s_i,d_i\\leq 10^5$$$) — the time when the first bus of this route arrives and the interval between two buses of this route.", "output_spec": "Print one number — what bus route Serval will use. If there are several possible answers, you can print any of them.", "sample_inputs": ["2 2\n6 4\n9 5", "5 5\n3 3\n2 5\n5 6\n4 9\n6 1", "3 7\n2 2\n2 3\n2 4"], "sample_outputs": ["1", "3", "1"], "notes": "NoteIn the first example, the first bus of the first route arrives at time $$$6$$$, and the first bus of the second route arrives at time $$$9$$$, so the first route is the answer.In the second example, a bus of the third route arrives at time $$$5$$$, so it is the answer.In the third example, buses of the first route come at times $$$2$$$, $$$4$$$, $$$6$$$, $$$8$$$, and so fourth, buses of the second route come at times $$$2$$$, $$$5$$$, $$$8$$$, and so fourth and buses of the third route come at times $$$2$$$, $$$6$$$, $$$10$$$, and so on, so $$$1$$$ and $$$2$$$ are both acceptable answers while $$$3$$$ is not."}, "positive_code": [{"source_code": "n,t = gets.split.map(&:to_i)\nmin = 10 ** 6\nans = 0\nn.times do |i|\n s,d = gets.split.map(&:to_i)\n if s > t\n tmp = s \n else\n tmp = ((t-s)/d.to_f).ceil * d + s\n end\n if tmp < min\n min = tmp\n ans = i+1\n end\n #p tmp\nend\np ans\n"}, {"source_code": "N, T = gets.split.map(&:to_i)\n\nbuses = N.times.map { gets.split.map(&:to_i) }\n\nans = -1\ntime = Float::INFINITY\n\nbuses.each.with_index(1) do |(s, d), i|\n t = if s >= T\n s\n else\n s + d * ((T - s) / d.to_f).ceil\n end\n\n if time > t\n time = t\n ans = i\n end\nend\n\nputs ans\n"}, {"source_code": "n, t = gets.split.map(&:to_i)\nmn = 2147483647\nidx = -1\n\nn.times do |i|\n s, d = gets.split.map(&:to_i)\n x = ((t-s).to_f/d).ceil\n\n if x < 0\n x = 0\n end\n\n if mn > s+d*x\n mn = s+d*x\n idx = i+1\n end\nend\n\np idx\n"}, {"source_code": "n, t = gets.chomp.split(\" \").map(&:to_i)\nret = 10**5\nans = -1\nn.times do |i|\n s, d = gets.chomp.split(\" \").map(&:to_i)\n while true\n if s >= t\n st = s - t\n if ret > st\n ret = st\n ans = i + 1\n end\n break\n end\n s += d\n end\nend\nputs ans"}], "negative_code": [], "src_uid": "71be4cccd3b8c494ad7cc2d8a00cf5ed"} {"nl": {"description": "Given an array $$$a$$$ of length $$$n$$$, you can do at most $$$k$$$ operations of the following type on it: choose $$$2$$$ different elements in the array, add $$$1$$$ to the first, and subtract $$$1$$$ from the second. However, all the elements of $$$a$$$ have to remain non-negative after this operation. What is lexicographically the smallest array you can obtain?An array $$$x$$$ is lexicographically smaller than an array $$$y$$$ if there exists an index $$$i$$$ such that $$$x_i<y_i$$$, and $$$x_j=y_j$$$ for all $$$1 \\le j < i$$$. Less formally, at the first index $$$i$$$ in which they differ, $$$x_i<y_i$$$.", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 20$$$) – the number of test cases you need to solve. The first line of each test case contains $$$2$$$ integers $$$n$$$ and $$$k$$$ ($$$2 \\le n \\le 100$$$, $$$1 \\le k \\le 10000$$$) — the number of elements in the array and the maximum number of operations you can make. The second line contains $$$n$$$ space-separated integers $$$a_1$$$, $$$a_2$$$, $$$\\ldots$$$, $$$a_{n}$$$ ($$$0 \\le a_i \\le 100$$$) — the elements of the array $$$a$$$.", "output_spec": "For each test case, print the lexicographically smallest array you can obtain after at most $$$k$$$ operations.", "sample_inputs": ["2\n3 1\n3 1 4\n2 10\n1 0"], "sample_outputs": ["2 1 5 \n0 1"], "notes": "NoteIn the second test case, we start by subtracting $$$1$$$ from the first element and adding $$$1$$$ to the second. Then, we can't get any lexicographically smaller arrays, because we can't make any of the elements negative."}, "positive_code": [{"source_code": "def solution(arr, n, k)\n i = 0\n\n while i < n - 1 && k > 0\n if arr[i] == 0\n i += 1\n next\n end\n\n arr[i] -= 1\n arr[n - 1] += 1\n k -= 1\n end\n\n arr\nend\n\ntest_cases = gets.to_i\n\ntest_cases.times do |test_case|\n n, k = gets.split(' ').map(&:to_i)\n arr = gets.split(' ').map(&:to_i)\n res = solution(arr, n, k)\n\n puts res.join(' ')\nend\n"}, {"source_code": "t = Integer(gets.chomp)\r\nt.times{\r\n n, k = gets.chomp.split().map!{|c| Integer(c)}\r\n a = gets.chomp.split().map!{|c| Integer(c)}\r\n \r\n l = 0\r\n until k == 0 || l == n-1 do \r\n if(a[l] > k)\r\n a[l] -= k\r\n a[n - 1] += k\r\n k = 0\r\n else\r\n k -= a[l]\r\n a[n-1] += a[l]\r\n a[l] = 0\r\n l += 1\r\n end\r\n end\r\n \r\n a.each do |e| print \"#{e} \" end\r\n puts \"\"\r\n}"}, {"source_code": "test_cases = gets.chomp.to_i\r\nfor i in 1..test_cases do\r\n line = gets.chomp.split(\" \")\r\n n = line[0].to_i\r\n k = line[1].to_i\r\n a = gets.chomp.split(\" \")\r\n b = Array.new(n)\r\n for j in 0..n - 1 do\r\n b[j] = a[j].to_i\r\n end\r\n for j in 0..n - 1 do\r\n d = [k, b[j]].min\r\n b[j] -= d\r\n b[n - 1] += d\r\n k -= d\r\n if k == 0\r\n break\r\n end\r\n end\r\n for j in 0..n - 1 do\r\n print \"#{b[j]} \"\r\n end\r\n puts \"\"\r\nend"}, {"source_code": "t = gets.to_s.to_i\n1.upto(t) do |casenum|\n n, k = gets.to_s.split.map {|x| x.to_i }\n a = gets.to_s.split.map {|x| x.to_i }\n i = 0\n k.times do\n while a[i] == 0 && i < a.size - 1\n i += 1\n end\n if a[i] > 0 && i < a.size - 1\n a[i] -= 1\n a[-1] += 1\n end\n end\n printf(\"%s\\n\", a.join(\" \"))\nend\n\n"}], "negative_code": [{"source_code": "def solution(arr, n, k)\n i = 0\n\n while i < n - 1 && k > 0\n i += 1 if arr[i] == 0\n arr[i] -= 1\n arr[n - 1] += 1\n k -= 1\n end\n\n arr\nend\n\ntest_cases = gets.to_i\n\ntest_cases.times do |test_case|\n n, k = gets.split(' ').map(&:to_i)\n arr = gets.split(' ').map(&:to_i)\n res = solution(arr, n, k)\n\n puts res.join(' ')\nend\n"}, {"source_code": "def solution(arr, n, k)\n i = 0\n\n while arr[i] >= 0 && k > 0 && i < n - 1\n break if arr[i] == 0\n arr[i] -= 1\n arr[n - i - 1] += 1\n k -= 1\n i += 1 if arr[i] < 1\n end\n\n arr\nend\n\ntest_cases = gets.to_i\n\ntest_cases.times do |test_case|\n n, k = gets.split(' ').map(&:to_i)\n arr = gets.split(' ').map(&:to_i)\n res = solution(arr, n, k)\n\n puts res.join(' ')\nend\n"}, {"source_code": "def solution(arr, n, k)\n i = 0\n\n while arr[i] >= 0 && k > 0 && i < n - 1\n arr[i] -= 1\n arr[n - i - 1] += 1\n k -= 1\n i += 1 if arr[i] == 0\n # puts arr.join(' ')\n end\n\n arr\nend\n\ntest_cases = gets.to_i\n\ntest_cases.times do |test_case|\n n, k = gets.split(' ').map(&:to_i)\n arr = gets.split(' ').map(&:to_i)\n res = solution(arr, n, k)\n\n puts res.join(' ')\nend\n"}, {"source_code": "def solution(arr, n, k)\n i = 0\n\n while i < n && arr[i] > 0 && k > 0\n arr[i] -= 1\n arr[n - i - 1] += 1\n k -= 1\n i += 1 if arr[i] == 1\n end\n\n arr\nend\n\ntest_cases = gets.to_i\n\ntest_cases.times do |test_case|\n n, k = gets.split(' ').map(&:to_i)\n arr = gets.split(' ').map(&:to_i)\n res = solution(arr, n, k)\n\n puts res.join(' ')\nend\n"}], "src_uid": "6854ad3597f9f41d475aacb774b823ed"} {"nl": {"description": "You are given the array of integer numbers a0, a1, ..., an - 1. For each element find the distance to the nearest zero (to the element which equals to zero). There is at least one zero element in the given array.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 2·105) — length of the array a. The second line contains integer elements of the array separated by single spaces ( - 109 ≤ ai ≤ 109).", "output_spec": "Print the sequence d0, d1, ..., dn - 1, where di is the difference of indices between i and nearest j such that aj = 0. It is possible that i = j.", "sample_inputs": ["9\n2 1 0 3 0 0 3 2 4", "5\n0 1 2 3 4", "7\n5 6 0 1 -2 3 4"], "sample_outputs": ["2 1 0 1 0 0 1 2 3", "0 1 2 3 4", "2 1 0 1 2 3 4"], "notes": null}, "positive_code": [{"source_code": "n=gets.chomp\na=gets.chomp.split.map!{|x| x.to_i}\nlast=-a.size\na.each_index do |i|\n last = i if a[i]==0\n a[i]=i-last\nend\na.reverse!\n\nlast=-a.size\na.each_index do |i|\n last = i if a[i]==0\n a[i]=i-last if a[i]>i-last\nend\na.reverse!\nputs a * ' '"}, {"source_code": "#!/usr/bin/ruby\nn,*a=$<.read.split.map &:to_i\nz=[-Float::INFINITY]+(0...n).select{|i|a[i]==0}+[Float::INFINITY]\nputs (0...n).map{|i|\n\tidx=((0...z.size).bsearch{|j|z[j]>=i}||z.size)-1\n\t[i-z[idx],z[idx+1]-i].min\n}*' '"}, {"source_code": "size = gets.chomp.to_i\nvalues = gets.chomp.split(' ').map!(&:to_i)\n\nindexes = values.each_with_object([]).with_index do |(value, obj), index|\n obj << index if value.zero?\nend\n\nresult = []\nleft = nil\nright = indexes.shift\n\nsize.times do |index|\n #puts \"left: #{left} right: #{right} tested: #{index}\"\n distance_to_left = (index - (left || right)).abs\n distance_to_right = (index - (right || left)).abs\n\n result << [distance_to_left, distance_to_right].min # pohybujeme se v mantinelu\n # mantinel posuneme jakmile prejdeme pres index\n if index == right\n left = right\n right = indexes.shift\n end\nend\n\nputs result.join(' ')"}, {"source_code": "n=gets.to_i\na=gets.split(\" \").map(&:to_i)\nh=Hash.new\nzeroIndex=-1\nfor i in 0...n\n\tif a[i]==0\n\t\tzeroIndex=i\n\t\th[i]=0\n\telse\n\t\tif zeroIndex>=0\n\t\t\th[i]=i-zeroIndex\n\t\telse\n\t\t\th[i]=n\n\t\tend\n\tend\nend\nzeroIndex=-1\nfor i in (n-1).downto(0)\n\tif a[i]==0\n\t\tzeroIndex=i\n\telse\n\t\tif zeroIndex>=0\n\t\t\th[i]=[h[i],zeroIndex-i].min\n\t\tend\n\tend\nend\nputs h.values.join(\" \")"}], "negative_code": [{"source_code": "n=gets.chomp\na=gets.chomp.split.map!{|x| x.to_i}\nlast=-a.size\na.each_index do |i|\n last = i if a[i]=='0'\n a[i]=i-last\nend\na.reverse!\n\nlast=-a.size\na.each_index do |i|\n last = i if a[i]==0\n a[i]=i-last if a[i]>i-last\nend\na.reverse!\nputs a"}, {"source_code": "size = gets.chomp.to_i\nvalues = gets.chomp.split(' ').map!(&:to_i)\n\nindexes = values.each_with_object([]).with_index do |(value, obj), index|\n obj << index if value.zero?\nend\n\nresult = []\nleft = nil\nright = indexes.shift\n\nsize.times do |index|\n puts \"left: #{left} right: #{right} tested: #{index}\"\n distance_to_left = (index - (left || right)).abs\n distance_to_right = (index - (right || left)).abs\n\n result << [distance_to_left, distance_to_right].min # pohybujeme se v mantinelu\n # mantinel posuneme jakmile prejdeme pres index\n if index == right\n left = right\n right = indexes.shift\n end\nend\n\nputs result.join(' ')"}], "src_uid": "2031f2ac5652609fda77830e93ffcc2c"} {"nl": {"description": "Given an array $$$a$$$ of positive integers with length $$$n$$$, determine if there exist three distinct indices $$$i$$$, $$$j$$$, $$$k$$$ such that $$$a_i + a_j + a_k$$$ ends in the digit $$$3$$$.", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\leq t \\leq 1000$$$) — the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$3 \\leq n \\leq 2 \\cdot 10^5$$$) — the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\leq a_i \\leq 10^9$$$) — the elements of the array. The sum of $$$n$$$ across all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "Output $$$t$$$ lines, each of which contains the answer to the corresponding test case. Output \"YES\" if there exist three distinct indices $$$i$$$, $$$j$$$, $$$k$$$ satisfying the constraints in the statement, and \"NO\" otherwise. You can output the answer in any case (for example, the strings \"yEs\", \"yes\", \"Yes\" and \"YES\" will be recognized as a positive answer).", "sample_inputs": ["6\n\n4\n\n20 22 19 84\n\n4\n\n1 11 1 2022\n\n4\n\n1100 1100 1100 1111\n\n5\n\n12 34 56 78 90\n\n4\n\n1 9 8 4\n\n6\n\n16 38 94 25 18 99"], "sample_outputs": ["YES\nYES\nNO\nNO\nYES\nYES"], "notes": "NoteIn the first test case, you can select $$$i=1$$$, $$$j=4$$$, $$$k=3$$$. Then $$$a_1 + a_4 + a_3 = 20 + 84 + 19 = 123$$$, which ends in the digit $$$3$$$.In the second test case, you can select $$$i=1$$$, $$$j=2$$$, $$$k=3$$$. Then $$$a_1 + a_2 + a_3 = 1 + 11 + 1 = 13$$$, which ends in the digit $$$3$$$.In the third test case, it can be proven that no such $$$i$$$, $$$j$$$, $$$k$$$ exist. Note that $$$i=4$$$, $$$j=4$$$, $$$k=4$$$ is not a valid solution, since although $$$a_4 + a_4 + a_4 = 1111 + 1111 + 1111 = 3333$$$, which ends in the digit $$$3$$$, the indices need to be distinct.In the fourth test case, it can be proven that no such $$$i$$$, $$$j$$$, $$$k$$$ exist.In the fifth test case, you can select $$$i=4$$$, $$$j=3$$$, $$$k=1$$$. Then $$$a_4 + a_3 + a_1 = 4 + 8 + 1 = 13$$$, which ends in the digit $$$3$$$.In the sixth test case, you can select $$$i=1$$$, $$$j=2$$$, $$$k=6$$$. Then $$$a_1 + a_2 + a_6 = 16 + 38 + 99 = 153$$$, which ends in the digit $$$3$$$."}, "positive_code": [{"source_code": "def solve\r\n n = gets.to_i\r\n a = gets.split.map(&:to_i)\r\n cnt = Array.new(10)\r\n cnt2 = Array.new(10)\r\n i = 0\r\n while i < n\r\n a[i] = a[i] % 10\r\n i = i + 1\r\n end\r\n i = 0\r\n while i < 10\r\n cnt[i] = 0\r\n cnt2[i] = 0\r\n i = i + 1\r\n end\r\n a.each do |i|\r\n cnt[i] = cnt[i] + 1\r\n end\r\n ok = false\r\n i = 0\r\n while i < 10\r\n j = 0\r\n cnt2[i] = cnt2[i] + 1\r\n while j < 10\r\n cnt2[j] = cnt2[j] + 1\r\n k = 0\r\n while k < 10\r\n cnt2[k] = cnt2[k] + 1\r\n x = (i + j + k) % 10\r\n if cnt[i] >= cnt2[i] and cnt[j] >= cnt2[j] and cnt[k] >= cnt2[k] and x == 3\r\n ok = true\r\n end\r\n cnt2[k] = cnt2[k] - 1\r\n k = k + 1\r\n end\r\n cnt2[j] = cnt2[j] - 1\r\n j = j + 1\r\n end\r\n cnt2[i] = cnt2[i] - 1\r\n i = i + 1\r\n end\r\n if ok == true\r\n puts \"YES\"\r\n else \r\n puts \"NO\"\r\n end\r\nend\r\n\r\ngets.to_i.times do\r\n solve\r\nend"}, {"source_code": "# 和が 3 になる組み合わせを列挙\ncombinations = []\n(0 .. 9).each do |x|\n (x .. 9).each do |y|\n (y .. 9).each do |z|\n if (x + y + z) % 10 == 3\n counts = [0] * 10\n counts[x] += 1\n counts[y] += 1\n counts[z] += 1\n combinations << counts\n end\n end\n end\nend\n\nt = gets.to_i\nt.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n \n counts = [0] * 10\n a.each do |x|\n counts[x % 10] += 1\n end\n \n ans = combinations.any? { |counts2| (0 .. 9).all? { |x| counts[x] >= counts2[x] } }\n puts ans ? \"YES\" : \"NO\"\nend"}, {"source_code": "main = -> {\r\n t = int\r\n t.times do\r\n n = int\r\n a = ints\r\n \r\n count = [0] * 10\r\n a.each do |x|\r\n count[x % 10] += 1\r\n end\r\n \r\n yesno ((0 ... 10).any? do |x|\r\n next false if count[x] == 0\r\n count[x] -= 1\r\n b = (x ... 10).any? do |y|\r\n next false if count[y] == 0\r\n count[y] -= 1\r\n c = (y ... 10).any? do |z|\r\n next false if count[z] == 0\r\n (x + y + z) % 10 == 3\r\n end\r\n count[y] += 1\r\n c\r\n end\r\n count[x] += 1\r\n b\r\n end)\r\n end\r\n}\r\n\r\nDEBUG = false\r\nMOD = 10**9+7\r\nINF = 10**18\r\n$yesno = %w(NO YES)\r\n$YesNo = %w(No Yes)\r\n$YESNO = %w(NO YES)\r\n\r\nrequire \"set\"\r\nrequire \"prime\"\r\n\r\ndef int; gets.to_s.to_i end\r\ndef ints; gets.to_s.split.map { |s| s.to_i } end\r\ndef int1s; gets.to_s.split.map { |s| s.to_i - 1 } end\r\ndef float; gets.to_s.to_f end\r\ndef floats; gets.to_s.split.map { |s| s.to_f } end\r\ndef array_of(&convert); gets.to_s.split.map(&convert) end\r\ndef string; gets.to_s.chomp end\r\ndef strings; gets.to_s.split end\r\ndef rep(n, &b); Array.new(n, &b) end\r\ndef yes; puts $yesno[1] end\r\ndef no; puts $yesno[0] end\r\ndef yesno t; puts $yesno[t ? 1 : 0] end\r\ndef YesNo t; puts $YesNo[t ? 1 : 0] end\r\ndef YESNO t; puts $YESNO[t ? 1 : 0] end\r\ndef zip(xs, *yss); Enumerator.new { |y| xs.zip(*yss) { |a| y.yield(*a) } } end\r\ndef max(*xs, &block); block_given? ? xs.max_by(&block) : xs.max end\r\ndef min(*xs, &block); block_given? ? xs.min_by(&block) : xs.min end\r\ndef minmax(*xs, &block); block_given? ? xs.minmax_by(&block) : xs.minmax end\r\ndef gcd(*xs); xs.inject(0, :gcd) end\r\ndef matrix(h, w, fill=nil, &block); return Array.new(h) { Array.new(w, &block) } if block_given?; Array.new(h) { [fill] * w } end\r\ndef debug(x = nil); STDERR.puts (block_given? ? yield(x) : x).inspect if DEBUG; x end\r\ndef debug_grid(grid, width = 1); grid.each { |row| STDERR.puts row.map { |x| x.inspect.ljust(width) }.join(\"\") } if DEBUG; grid end\r\ndef if_debug; yield if DEBUG end\r\n\r\nmodule Boolean\r\n def coerce(other); [other, to_i] end\r\n def +@; to_i end\r\n def to_int; to_i end\r\n def *(other); to_i * other end\r\nend\r\n\r\nclass TrueClass\r\n include Boolean\r\n def to_i; 1 end\r\nend\r\n\r\nclass FalseClass\r\n include Boolean\r\n def to_i; 0 end\r\nend\r\n\r\nclass Integer\r\n def div_ceil(y); (self + y - 1) / y end\r\n def mod_inv(mod = MOD); pow(mod-2, mod) end\r\n def mod_div(y, mod = MOD); self * y.mod_inv(mod) % mod end\r\n def mod_nCr(r, mod = MOD); x = y = 1; (1..r).each { |i| x = x * (self + 1 - i) % mod; y = y * i % mod }; x.mod_div(y, mod); end\r\n def factorial(mod = MOD); (2..self).inject(1) { |f, x| f * x % mod } end\r\n def popcount; x = self; c = 0; while x > 0; c += 1 if x & 1 == 1; x >>= 1 end; c end #TODO: faster\r\n def bitbrute(&block); (1< 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\r\n def lower_bound; ac, wa = self.end_open, self.begin; while ac - wa > 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\r\n def shakutori(r2, &pred); Enumerator.new { |y| j, r = r2.begin, r2.end_open; each { |i| j += 1 while j + 1 < r and pred[i, j+1]; y.yield(i, j) } }; end\r\n def widest(&block); Enumerator.new { |y| j, n = self.begin, self.end_open; each { |i| j += 1 while j < n and block[i, j]; y.yield(i, j) if block[i, j] } } end\r\nend\r\n\r\nclass Array\r\n def power(&block); (0 ... 1 << size).each(&block) end\r\n def sorted_merge(other); a = []; i = j = 0; n, m = size, other.size; if j < m and other[j] < self[i]; a << other[j]; j += 1 else; a << self[i]; i += 1 end while i < n; a.push(*other[j..-1]) if j < m; a end\r\n def upper_bound; ac, wa = 0, size; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\r\n def lower_bound; ac, wa = size, 0; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\r\n def cum(*xs, &op); a = []; a << xs[0] if xs.size > 0; a << x = self[0]; (1...size).each { |i| a << x = op[x, self[i]] }; a end\r\n def cumdiff(range); self[range.end_open] - self[range.begin]; end\r\n def compress(kinds = uniq.sort!); map { |x| kinds.bsearch_index { _1 >= x } }; end\r\nend\r\n\r\nmodule Enumerable\r\n def sorted_uniq; x = nil; filter { |y| c = x === y; x = y; !c } end\r\n def cumsum; ys = [0]; each { |x| ys << x + ys[-1] }; ys end\r\nend\r\n\r\nmain[]"}, {"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n a = gets.split.map{|e| e.to_i % 10}\r\n at = a.tally\r\n new_a = []\r\n at.each do |k,v|\r\n [v,3].min.times do\r\n new_a << k\r\n end\r\n end\r\n\r\n ans = false\r\n m = new_a.size\r\n m.times do |i|\r\n m.times do |j|\r\n next if i == j\r\n m.times do |k|\r\n next if i == k || k == j\r\n\r\n if (new_a[i]+new_a[j]+new_a[k])%10 == 3\r\n ans = true\r\n break\r\n end\r\n end\r\n break if ans\r\n end\r\n break if ans\r\n end\r\n\r\n puts ans ? \"YES\" : \"NO\"\r\nend\r\n"}, {"source_code": "def solve(a)\n arr = gets.split.map(&:to_i).map { |number| number.modulo(10) }\n\n hash = Hash.new(0)\n\n arr.each do |n|\n hash[n] = hash[n] > 3 ? 3 : hash[n].succ\n end\n\n numbers = []\n hash.each_key do |key|\n while hash[key] > 0\n numbers.push(key)\n hash[key] -= 1\n end\n end\n\n (0...numbers.size).each do |i|\n ((i + 1)...numbers.size).each do |j|\n ((j + 1)...numbers.size).each do |k|\n if (numbers[i] + numbers[j] + numbers[k]) % 10 == 3\n puts \"YES\"\n return\n end\n end\n end\n end\n\n puts \"NO\"\nend\n\nn = gets.to_i\n\nn.times do\n a = gets.to_i\n solve(a)\nend\n"}, {"source_code": "array = []\r\n0.upto(9) do |a|\r\n 0.upto(9) do |b|\r\n 0.upto(9) do |c|\r\n array << [a, b, c] if (a + b + c) % 10 == 3\r\n end\r\n end\r\nend\r\n\r\ngets.to_i.times do\r\n gets\r\n xs = gets.split.map(&:to_i)\r\n map = Hash.new(0)\r\n xs.each do |x|\r\n map[x % 10] += 1\r\n end\r\n ok = array.any? { |array| 0.upto(9).all? { |d| array.count(d) <= map[d] } }\r\n puts ok ? \"YES\" : \"NO\"\r\nend\r\n"}], "negative_code": [{"source_code": "def solve\r\n n = gets.to_i\r\n a = gets.split.map(&:to_i)\r\n cnt = Array.new(10)\r\n cnt2 = Array.new(10)\r\n pairs = Array.new(n) {Array.new(10)}\r\n i = 0\r\n while i < n\r\n j = 0\r\n while j < 10\r\n pairs[i][j] = 0\r\n j = j + 1\r\n end\r\n i = i + 1\r\n end\r\n i = 0\r\n while i < 10\r\n cnt[i] = 0\r\n cnt2[i] = 0\r\n i = i + 1\r\n end\r\n a.each do |x|\r\n i = x % 10\r\n cnt[i] = cnt[i] + 1\r\n end\r\n id = 0\r\n a.each do |x|\r\n i = x % 10\r\n j = 0\r\n cnt[i] = cnt[i] - 1\r\n while j < 10\r\n k = (i + j) % 10\r\n cnt2[k] = cnt2[k] + cnt[j]\r\n pairs[id][k] = pairs[id][k] + cnt[j]\r\n j = j + 1\r\n end\r\n cnt[i] = cnt[i] + 1\r\n id = id + 1\r\n end\r\n id = 0\r\n ok = false\r\n a.each do |x|\r\n i = x % 10\r\n j = 0\r\n while j < 10\r\n cnt2[j] = cnt2[j] - pairs[id][j]\r\n j = j + 1\r\n end\r\n j = 0\r\n while j < 10\r\n k = (i + j) % 10\r\n if k == 3 and cnt2[j] > 0\r\n ok = true\r\n end\r\n j = j + 1\r\n end\r\n j = 0\r\n while j < 10\r\n cnt2[j] = cnt2[j] + pairs[id][j]\r\n j = j + 1\r\n end\r\n id = id + 1\r\n end\r\n if ok == true\r\n puts \"YES\"\r\n else \r\n puts \"NO\"\r\n end\r\nend\r\n\r\ngets.to_i.times do\r\n solve\r\nend"}, {"source_code": "def solve\r\n n = gets.to_i\r\n a = gets.split.map(&:to_i)\r\n cnt = Array.new(10)\r\n cnt2 = Array.new(10)\r\n pairs = Array.new(n) {Array.new(10)}\r\n i = 0\r\n while i < n\r\n j = 0\r\n while j < 10\r\n pairs[i][j] = 0\r\n j = j + 1\r\n end\r\n i = i + 1\r\n end\r\n i = 0\r\n while i < 10\r\n cnt[i] = 0\r\n cnt2[i] = 0\r\n i = i + 1\r\n end\r\n a.each do |x|\r\n i = x % 10\r\n cnt[i] = cnt[i] + 1\r\n end\r\n id = 0\r\n a.each do |x|\r\n i = x % 10\r\n j = 0\r\n cnt[i] = cnt[i] - 1\r\n while j < 10\r\n k = (i + j) % 10\r\n cnt2[k] = cnt2[k] + cnt[j]\r\n pairs[id][k] = pairs[id][k] + cnt[j]\r\n j = j + 1\r\n end\r\n cnt[i] = cnt[i] + 1\r\n id = id + 1\r\n end\r\n id = 0\r\n ok = false\r\n a.each do |x|\r\n i = x % 10\r\n j = 0\r\n while j < 10\r\n cnt2[j] = cnt2[j] - pairs[id][j]\r\n j = j + 1\r\n end\r\n j = 0\r\n while j < 10\r\n k = (i + j) % 10\r\n if k == 3 and cnt2[j] > 0\r\n ok = true\r\n end\r\n j = j + 1\r\n end\r\n j = 0\r\n while j < 10\r\n cnt2[j] = cnt2[j] + pairs[id][j]\r\n j = j + 1\r\n end\r\n end\r\n if ok == true\r\n puts \"YES\"\r\n else \r\n puts \"NO\"\r\n end\r\nend\r\n\r\ngets.to_i.times do\r\n solve\r\nend"}, {"source_code": "def solve\r\n n = gets.to_i\r\n a = gets.split.map(&:to_i)\r\n cnt = Array.new(10)\r\n cnt2 = Array.new(10)\r\n pairs = Array.new(n) {Array.new(10)}\r\n i = 0\r\n while i < n\r\n j = 0\r\n while j < 10\r\n pairs[i][j] = 0\r\n j = j + 1\r\n end\r\n i = i + 1\r\n end\r\n i = 0\r\n while i < 10\r\n cnt[i] = 0\r\n cnt2[i] = 0\r\n i = i + 1\r\n end\r\n a.each do |x|\r\n i = x % 10\r\n cnt[i] = cnt[i] + 1\r\n end\r\n id = 0\r\n a.each do |x|\r\n i = x % 10\r\n j = 0\r\n cnt[i] = cnt[i] - 1\r\n while j < 10\r\n k = (i + j) % 10\r\n cnt2[k] = cnt2[k] + cnt[j]\r\n pairs[id][k] = pairs[id][k] + cnt[j]\r\n j = j + 1\r\n end\r\n cnt[i] = cnt[i] + 1\r\n id = id + 1\r\n end\r\n id = 0\r\n ok = false\r\n a.each do |x|\r\n i = x % 10\r\n j = 0\r\n while j < 10\r\n k = (i + j) % 10\r\n cnt2[k] = cnt2[k] - pairs[id][k]\r\n if k == 3 and cnt2[k] > 0\r\n ok = true\r\n end\r\n j = j + 1\r\n end\r\n j = 0\r\n while j < 10\r\n k = (i + j) % 10\r\n cnt2[k] = cnt2[k] + pairs[id][k] \r\n j = j + 1\r\n end\r\n end\r\n if ok == true\r\n puts \"YES\"\r\n else \r\n puts \"NO\"\r\n end\r\nend\r\n\r\ngets.to_i.times do\r\n solve\r\nend"}, {"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n a = gets.split.map{|e| e.to_i % 10}\r\n at = a.tally\r\n new_a = []\r\n at.each do |k,v|\r\n [v,2].min.times do\r\n new_a << k\r\n end\r\n end\r\n\r\n ans = false\r\n m = new_a.size\r\n m.times do |i|\r\n m.times do |j|\r\n next if i == j\r\n k = new_a[i]+new_a[j]\r\n if k <= 3\r\n ans = true if !at[(3-k)].nil?\r\n elsif k <= 13\r\n ans = true if !at[(13-k)].nil?\r\n elsif k <= 23\r\n ans = true if !at[(23-k)].nil?\r\n end\r\n break if ans\r\n end\r\n break if ans\r\n end\r\n\r\n puts ans ? \"YES\" : \"NO\"\r\nend\r\n"}], "src_uid": "3ae4f35808348841e0f47540cdf4abe6"} {"nl": {"description": "We just discovered a new data structure in our research group: a suffix three!It's very useful for natural language processing. Given three languages and three suffixes, a suffix three can determine which language a sentence is written in.It's super simple, 100% accurate, and doesn't involve advanced machine learning algorithms.Let us tell you how it works. If a sentence ends with \"po\" the language is Filipino. If a sentence ends with \"desu\" or \"masu\" the language is Japanese. If a sentence ends with \"mnida\" the language is Korean. Given this, we need you to implement a suffix three that can differentiate Filipino, Japanese, and Korean.Oh, did I say three suffixes? I meant four.", "input_spec": "The first line of input contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 30$$$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing a single string denoting the sentence. Spaces are represented as underscores (the symbol \"_\") for ease of reading. The sentence has at least $$$1$$$ and at most $$$1000$$$ characters, and consists only of lowercase English letters and underscores. The sentence has no leading or trailing underscores and no two consecutive underscores. It is guaranteed that the sentence ends with one of the four suffixes mentioned above.", "output_spec": "For each test case, print a single line containing either \"FILIPINO\", \"JAPANESE\", or \"KOREAN\" (all in uppercase, without quotes), depending on the detected language.", "sample_inputs": ["8\nkamusta_po\ngenki_desu\nohayou_gozaimasu\nannyeong_hashimnida\nhajime_no_ippo\nbensamu_no_sentou_houhou_ga_okama_kenpo\nang_halaman_doon_ay_sarisari_singkamasu\nsi_roy_mustang_ay_namamasu"], "sample_outputs": ["FILIPINO\nJAPANESE\nJAPANESE\nKOREAN\nFILIPINO\nFILIPINO\nJAPANESE\nJAPANESE"], "notes": "NoteThe first sentence ends with \"po\", so it is written in Filipino.The second and third sentences end with \"desu\" and \"masu\", so they are written in Japanese.The fourth sentence ends with \"mnida\", so it is written in Korean."}, "positive_code": [{"source_code": "test=gets.strip.to_i\n\nwhile test != 0 \n\t\tn_str=gets.strip\n\t\tstr = n_str.reverse();\n\t\tif str[0] == 'o' && str[1] == 'p'\n\t\t\tputs \"FILIPINO\"\n\t\telsif str[0] == 'a' && str[1] == 'd' && str[2] == 'i' && str[3] == 'n' && str[4] == 'm'\n\t\t\tputs \"KOREAN\"\n\t\telse \n\t\t\tputs \"JAPANESE\"\n\t\tend\n\t\ttest -= 1\nend"}, {"source_code": "T=gets.chomp.to_i\nT.times do |x|\n string = gets.chomp\n puts \"FILIPINO\" if string.end_with? \"po\"\n puts \"JAPANESE\" if string.end_with? \"masu\"\n puts \"JAPANESE\" if string.end_with? \"desu\"\n puts \"KOREAN\" if string.end_with? \"mnida\"\nend"}, {"source_code": "t = gets.to_i\nt.times do\n\ts = gets.chomp.reverse!\n\tif s.slice(0,2) == 'op'\n\t\tputs \"FILIPINO\"\n\telsif [\"used\", \"usam\"].include?s.slice(0,4)\n\t\tputs \"JAPANESE\"\n\telse\n\t\tputs \"KOREAN\"\n\tend\nend"}, {"source_code": "N=STDIN.gets.strip.to_i\n(0..N-1).each do |i|\n s=STDIN.gets.strip\n if s =~ /po$/\n puts 'FILIPINO'\n elsif s =~ /desu$/ || s =~ /masu$/\n puts 'JAPANESE'\n elsif s =~ /mnida$/\n puts 'KOREAN'\n end\nend\n\n"}, {"source_code": "number = gets.chomp.to_i\ncases = []\nnumber.times { cases << gets.chomp }\nfor line in cases\n\tif line[-5..-1] == 'mnida'\n\t\tputs(\"KOREAN\")\n\telsif line[-4..-1] == 'desu' || line[-4..-1] == 'masu'\n\t\tputs(\"JAPANESE\")\n\telsif line[-2..-1] == 'po'\n\t\tputs(\"FILIPINO\")\n\telse\n\t\tputs(\"Just for safety, ya know :3\")\n\tend\nend"}, {"source_code": "def solution\n n = gets.chomp.to_i\n data = []\n n.times do\n input = gets.chomp\n if input.size >= 2 && input[-2..-1] == 'po'\n data.push 'FILIPINO'\n elsif input.size >= 4 && input[-4..-1] == 'desu' || input[-4..-1] == 'masu'\n data.push 'JAPANESE'\n elsif input.size >= 5 && input[-5..-1] == 'mnida'\n data.push 'KOREAN'\n end\n end\n data.each { |e| puts e }\nend\n\nsolution\n"}, {"source_code": "gets.to_i.times do\n s = gets.chomp\n if s[-1] == \"o\" \n puts \"FILIPINO\"\n elsif s[-1] == \"a\" \n puts \"KOREAN\"\n elsif s[-1] == \"u\"\n puts \"JAPANESE\"\n end\nend"}, {"source_code": "nb = gets.to_i\nnb.times do\n case gets.chomp[-1]\n when 'a'\n puts 'KOREAN'\n when 'u'\n puts 'JAPANESE'\n when 'o'\n puts 'FILIPINO'\n end\nend"}, {"source_code": "words = []\ngets.chomp().to_i.times do |i|\n words << gets.chomp().to_s #input\nend\nwords.length().times do |i|\n if(words[i][-1].eql? \"o\")\n puts \"FILIPINO\"\n elsif(words[i][-1].eql? \"u\")\n puts \"JAPANESE\"\n else\n puts \"KOREAN\"\n end\nend"}, {"source_code": "gets.to_i.times do\n s = gets.chop\n puts 'FILIPINO' if s[-2..-1] == 'po'\n puts 'JAPANESE' if s[-4..-1] == 'masu' or s[-4..-1] == 'desu'\n puts 'KOREAN' if s[-5..-1] == 'mnida'\nend\n"}, {"source_code": "gets.to_i.times do\n s = gets.chomp\n puts 'FILIPINO' if s[-2..-1] == 'po'\n puts 'JAPANESE' if s[-4..-1] == 'masu' or s[-4..-1] == 'desu'\n puts 'KOREAN' if s[-5..-1] == 'mnida'\nend\n"}], "negative_code": [{"source_code": "if ARGV[0]==\"LOCAL_DEV\"\n define_method(\"debug\") do |var|\n p var\n end\nelse\n define_method(\"debug\") do |var|\n end\nend\n\nN=STDIN.gets.strip.to_i\n(0..N-1).each do |i|\n s=STDIN.gets.strip\n if s =~ /^.+?po$/\n puts 'FILIPINO'\n elsif s =~ /^.+?desu$/ || s =~ /^.+?masu$/\n puts 'JAPANESE'\n elsif s =~ /^.+?mnida$/\n puts 'KOREAN'\n end\nend\n\n"}], "src_uid": "816907d873bce3573ce1e5ae3f494768"} {"nl": {"description": "Phoenix has $$$n$$$ coins with weights $$$2^1, 2^2, \\dots, 2^n$$$. He knows that $$$n$$$ is even.He wants to split the coins into two piles such that each pile has exactly $$$\\frac{n}{2}$$$ coins and the difference of weights between the two piles is minimized. Formally, let $$$a$$$ denote the sum of weights in the first pile, and $$$b$$$ denote the sum of weights in the second pile. Help Phoenix minimize $$$|a-b|$$$, the absolute value of $$$a-b$$$.", "input_spec": "The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$2 \\le n \\le 30$$$; $$$n$$$ is even) — the number of coins that Phoenix has. ", "output_spec": "For each test case, output one integer — the minimum possible difference of weights between the two piles.", "sample_inputs": ["2\n2\n4"], "sample_outputs": ["2\n6"], "notes": "NoteIn the first test case, Phoenix has two coins with weights $$$2$$$ and $$$4$$$. No matter how he divides the coins, the difference will be $$$4-2=2$$$.In the second test case, Phoenix has four coins of weight $$$2$$$, $$$4$$$, $$$8$$$, and $$$16$$$. It is optimal for Phoenix to place coins with weights $$$2$$$ and $$$16$$$ in one pile, and coins with weights $$$4$$$ and $$$8$$$ in another pile. The difference is $$$(2+16)-(4+8)=6$$$."}, "positive_code": [{"source_code": "\ndef solve n\n first = (1...(n/2)).to_a + [n]\n first = first.flatten\n second = ((n/2)...n).to_a\n a = first.map {|i| 2**i}.reduce(:+)\n b = second.map {|i| 2**i}.reduce(:+)\n (a-b).abs\nend\n\nt = STDIN.gets.chomp.to_i\n\nt.times do |i|\n n = STDIN.gets.chomp.to_i\n res = solve n\n puts res\nend\n"}, {"source_code": "# require 'pry-byebug'\n\nfile = File.exist?('in.in') ? File.new('in.in','r') : STDIN\nt = file.gets.chomp.to_i\n\nt.times do\n n = file.gets.chomp.to_i\n\n a = 2**n\n min = 2**(n/2)-2\n\n b = 2**(n-1)\n min2 = 2**(n-1)-2-min\n\n puts (a+min)-(b+min2)\nend"}, {"source_code": "pw2 = Array.new(32,0);\npw2[0]=1\nfor i in 1..31\n pw2[i]=pw2[i-1]*2\nend\n\nt = gets.chomp.to_i\nwhile t>0 do\n n = gets.chomp.to_i\n sum = 0\n for i in 1..(n/2-1)\n sum+=pw2[i]\n end\n sum+=pw2[n]\n puts sum - (pw2[n+1]-sum-2)\n t-=1\n\nend \n"}, {"source_code": "n = gets.to_i\n\nn.times {\n v = gets.to_i\n puts 2 ** (v / 2 + 1) - 2\n}\n\n"}, {"source_code": "def min_weigth(n)\n f1 = (2 ** (n/2)) - 2\n (f1 + (2 ** n)) - ((2 ** n) - 2 - f1)\nend\n\nt = gets.chomp.to_i\n\nt.times do\n n = gets.chomp.to_i\n puts min_weigth(n)\nend\n"}, {"source_code": "# Phoenix has 𝑛 coins with weights 21,22,…,2𝑛. He knows that 𝑛 is even.\n\n# He wants to split the coins into two piles such that each pile has exactly 𝑛2\n# coins and the difference of weights between the two piles is minimized.\n# Formally, let 𝑎 denote the sum of weights in the first pile, and 𝑏 denote the\n# sum of weights in the second pile. Help Phoenix minimize |𝑎−𝑏|, the absolute\n# value of 𝑎−𝑏.\n\n# Input\n# The input consists of multiple test cases. The first line contains an integer\n# 𝑡 (1≤𝑡≤100) — the number of test cases.\n\n# The first line of each test case contains an\n# integer 𝑛 (2≤𝑛≤30; 𝑛 is even) — the number of coins that Phoenix has.\n\n# Output\n# For each test case, output one integer — the minimum possible difference of\n# weights between the two piles.\n\n# Example\n# inputCopy\n# 2\n# 2\n# 4\n# outputCopy\n# 2\n# 6\n# Note\n# In the first test case, Phoenix has two coins with weights 2 and 4. No matter\n# how he divides the coins, the difference will be 4−2=2.\n\n# In the second test case, Phoenix has four coins of weight 2, 4, 8, and 16.\n# It is optimal for Phoenix to place coins\n# with weights 2 and 16 in one pile, and coins with weights 4 and 8 in another\n# pile. The difference is (2+16)−(4+8)=6.\n\ndef phoenix(queries)\n queries.each do |query|\n query = query.split(' ')\n\n return 0 unless query.length == 1\n\n stack_a = 0\n stack_a_count = 0\n stack_b = 0\n stack_b_count = 0\n input_n = query.first.to_i\n half = input_n / 2\n\n (input_n).downto(1) do |operand|\n product = 2 ** operand\n\n if stack_a_count == half\n stack_b += product\n stack_b_count += 1\n next\n end\n\n if stack_b_count == half\n stack_a += product\n stack_a_count += 1\n next\n end\n\n if stack_a > stack_b\n stack_b += product\n stack_b_count += 1\n else\n stack_a += product\n stack_a_count += 1\n end\n end\n\n puts (stack_a - stack_b).abs\n end\n\n return nil\nend\n\n# Codeforces required setup\nnumber_of_queries = gets.rstrip.to_i\n\nqueries = []\n\nnumber_of_queries.times do |query|\n queries << gets.rstrip\nend\n\nresult = phoenix queries\nputs result\n"}, {"source_code": "1.upto(gets.to_i) do\n n = gets.to_i\n\n l = (1..(n.div(2)-1)).inject(0) { |s, e| s + 2 ** e } + 2 ** n\n r = (n.div(2)..(n-1)).inject(0) { |s, e| s + 2 ** e }\n\n puts l - r\nend\n"}], "negative_code": [{"source_code": "pw2 = Array.new(32,0);\npw2[0]=1\nfor i in 1..31\n pw2[i]=pw2[i-1]*2\nend\n\nt = gets.chomp.to_i\nwhile t>0 do\n n = gets.chomp.to_i\n sum = 0\n for i in i..(n/2-1)\n sum+=pw[i]\n end\n sum+=pw2[n]\n\n puts sum - (pw2[n+1]-sum-2)\n t-=1\n\nend \n"}, {"source_code": "def min_weigth(n)\n f1 = (2 ** (n/2)) - 2\n (f1 + (2 ** n)) - ((2 ** n) - 2 - f1)\nend\n\nt = gets.chomp.to_i\n\nt.times do\n n = gets.chomp.to_i\n min_weigth(n)\nend\n"}, {"source_code": "# Phoenix has 𝑛 coins with weights 21,22,…,2𝑛. He knows that 𝑛 is even.\n\n# He wants to split the coins into two piles such that each pile has exactly 𝑛2 coins and the difference of weights\n# between the two piles is minimized. Formally, let 𝑎 denote the sum of weights in the first pile, and 𝑏 denote the sum of\n# weights in the second pile. Help Phoenix minimize |𝑎−𝑏|, the absolute value of 𝑎−𝑏.\n\n# Input\n# The input consists of multiple test cases. The first line contains an integer 𝑡 (1≤𝑡≤100) — the number of test cases.\n\n# The first line of each test case contains an integer 𝑛 (2≤𝑛≤30; 𝑛 is even) — the number of coins that Phoenix has.\n\n# Output\n# For each test case, output one integer — the minimum possible difference of weights between the two piles.\n\n# Example\n# inputCopy\n# 2\n# 2\n# 4\n# outputCopy\n# 2\n# 6\n# Note\n# In the first test case, Phoenix has two coins with weights 2 and 4. No matter how he divides the coins, the difference will be 4−2=2.\n\n# In the second test case, Phoenix has four coins of weight 2, 4, 8, and 16. It is optimal for Phoenix to place coins\n# with weights 2 and 16 in one pile, and coins with weights 4 and 8 in another pile. The difference is (2+16)−(4+8)=6.\n\ndef phoenix(queries)\n queries.each do |query|\n query = query.split(' ')\n\n return 0 unless query.length == 1\n\n stack_a = 0\n stack_a_count = 0\n stack_b = 0\n stack_b_count = 0\n n = query.first.to_i\n\n (n).downto(1) do |operand|\n product = 2 ** operand\n\n if stack_b > stack_a\n if stack_a_count > stack_b_count\n stack_b_count += 1\n stack_b += product\n next\n end\n\n stack_a_count += 1\n stack_a += product\n else\n if stack_b_count > stack_a_count\n stack_a_count += 1\n stack_a += product\n break\n end\n\n stack_b_count += 1\n stack_b += product\n end\n end\n\n puts (stack_a - stack_b).abs\n end\n\n return nil\nend\n\n# Codeforces required setup\nnumber_of_queries = gets.rstrip.to_i\n\nqueries = []\n\nnumber_of_queries.times do |query|\n queries << gets.rstrip\nend\n\nresult = phoenix queries\nputs result\n"}], "src_uid": "787a45f427c2db98b2ddb78925e0e0f1"} {"nl": {"description": "Devu is a dumb guy, his learning curve is very slow. You are supposed to teach him n subjects, the ith subject has ci chapters. When you teach him, you are supposed to teach all the chapters of a subject continuously.Let us say that his initial per chapter learning power of a subject is x hours. In other words he can learn a chapter of a particular subject in x hours.Well Devu is not complete dumb, there is a good thing about him too. If you teach him a subject, then time required to teach any chapter of the next subject will require exactly 1 hour less than previously required (see the examples to understand it more clearly). Note that his per chapter learning power can not be less than 1 hour.You can teach him the n subjects in any possible order. Find out minimum amount of time (in hours) Devu will take to understand all the subjects and you will be free to do some enjoying task rather than teaching a dumb guy.Please be careful that answer might not fit in 32 bit data type.", "input_spec": "The first line will contain two space separated integers n, x (1 ≤ n, x ≤ 105). The next line will contain n space separated integers: c1, c2, ..., cn (1 ≤ ci ≤ 105).", "output_spec": "Output a single integer representing the answer to the problem.", "sample_inputs": ["2 3\n4 1", "4 2\n5 1 2 1", "3 3\n1 1 1"], "sample_outputs": ["11", "10", "6"], "notes": "NoteLook at the first example. Consider the order of subjects: 1, 2. When you teach Devu the first subject, it will take him 3 hours per chapter, so it will take 12 hours to teach first subject. After teaching first subject, his per chapter learning time will be 2 hours. Now teaching him second subject will take 2 × 1 = 2 hours. Hence you will need to spend 12 + 2 = 14 hours.Consider the order of subjects: 2, 1. When you teach Devu the second subject, then it will take him 3 hours per chapter, so it will take 3 × 1 = 3 hours to teach the second subject. After teaching the second subject, his per chapter learning time will be 2 hours. Now teaching him the first subject will take 2 × 4 = 8 hours. Hence you will need to spend 11 hours.So overall, minimum of both the cases is 11 hours.Look at the third example. The order in this example doesn't matter. When you teach Devu the first subject, it will take him 3 hours per chapter. When you teach Devu the second subject, it will take him 2 hours per chapter. When you teach Devu the third subject, it will take him 1 hours per chapter. In total it takes 6 hours."}, "positive_code": [{"source_code": "#!/usr/bin/ruby\nn,x=gets.split.map(&:to_i)\nr=0\ngets.split.map(&:to_i).sort.each_with_index{|e,i|\n\tr+=[x-i,1].max*e\n}\np r"}, {"source_code": "n,x=gets.split.map &:to_i\nmas=gets.split.map(&:to_i).sort\nk=0\nmas.each { |m| k+=m*x; x-=1 if x!=1 }\nputs k"}, {"source_code": "n,m=gets.split.map{|e| e.to_i}\nans=0\ngets.split.map{|e| e.to_i}.sort.each{|e|\n\tans+=e*m\n\tm-=1\n\tm=1 if m<1\n}\nputs ans\n"}, {"source_code": "num = gets.chomp.split\nn = Integer(num[0])\nx = Integer(num[1])\nnums = gets.chomp.split\nc = []\nfor i in 0...n\n c[i] = Integer(nums[i])\nend\n\nc.sort!\ntime = 0\nfor i in 0...n\n time += c[i] * x\n if x > 1 \n x -= 1\n end\nend\nputs time"}, {"source_code": "def read_ints; STDIN.gets.split.map(&:to_i); end\nn,x = read_ints\na = read_ints\na.sort!\ni=0\nres = 0\nn.times do\n res+=a[i]*x\n j = [x-1, 1]\n x = j.max\n i+=1\nend\nputs res\n"}, {"source_code": "n, x = gets.split.map(&:to_i)\nc = gets.split.map(&:to_i).sort\nresult = 0\nc.each do |ci| \n result += ci * x\n x -= 1 if x > 1\nend\nputs result\n"}, {"source_code": "n,x = gets.split.map(&:to_i)\nc = gets.split.map(&:to_i).sort\n\nputs c.inject([x,0]) { |now, subjects|\n [[1, now[0]-1].max, now[1] + now[0] * subjects]\n}[1]"}, {"source_code": "# http://codeforces.com/problemset/problem/439/B\n\nn, x = gets.chomp.split.map(&:to_i)\nc = gets.chomp.split.map(&:to_i).sort\n\nmin = 0\nfor i in 0..(n - 1)\n min += x * c[i]\n x -= 1 if x > 1\nend\n\np min\n"}, {"source_code": "class DumbGuy\n n,x = gets.chomp.split(/\\s+/).map{|o| o.to_i}\n array = gets.chomp.split(/\\s+/).map{|o| o.to_i}\n array.sort!\n res = 0\n for i in 0..n-1\n res += array[i] * x\n x = x - 1 < 1 ? 1 : x - 1\n end\n puts res\nend"}], "negative_code": [], "src_uid": "ce27e56433175ebf9d3bbcb97e71091e"} {"nl": {"description": "A boy named Vasya has taken part in an Olympiad. His teacher knows that in total Vasya got at least x points for both tours of the Olympiad. The teacher has the results of the first and the second tour of the Olympiad but the problem is, the results have only points, no names. The teacher has to know Vasya's chances.Help Vasya's teacher, find two numbers — the best and the worst place Vasya could have won. Note that the total results' table sorts the participants by the sum of points for both tours (the first place has the participant who has got the most points). If two or more participants have got the same number of points, it's up to the jury to assign places to them according to their choice. It is guaranteed that each participant of the Olympiad participated in both tours of the Olympiad.", "input_spec": "The first line contains two space-separated integers n, x (1 ≤ n ≤ 105; 0 ≤ x ≤ 2·105) — the number of Olympiad participants and the minimum number of points Vasya earned. The second line contains n space-separated integers: a1, a2, ..., an (0 ≤ ai ≤ 105) — the participants' points in the first tour. The third line contains n space-separated integers: b1, b2, ..., bn (0 ≤ bi ≤ 105) — the participants' points in the second tour. The participants' points are given in the arbitrary order. It is guaranteed that Vasya was present in the Olympiad — there are two integers i, j (1 ≤ i, j ≤ n) such, that ai + bj ≥ x.", "output_spec": "Print two space-separated integers — the best and the worst place Vasya could have got on the Olympiad.", "sample_inputs": ["5 2\n1 1 1 1 1\n1 1 1 1 1", "6 7\n4 3 5 6 4 4\n8 6 0 4 3 4"], "sample_outputs": ["1 5", "1 5"], "notes": "NoteIn the first text sample all 5 participants earn 2 points each in any case. Depending on the jury's decision, Vasya can get the first (the best) as well as the last (the worst) fifth place.In the second test sample in the best case scenario Vasya wins again: he can win 12 points and become the absolute winner if the total results' table looks like that — {4:8, 6:4, 3:6, 4:4, 4:3, 5:0}.In this table all participants are sorted by decreasing points and we can see how much a participant earned in the first and in the second tour.In the worst case scenario Vasya can get the fifth place if the table looks like that — {4:8, 4:6, 6:4, 5:4, 4:3, 3:0}, and he earned 4 and 3 points in the first and second tours, correspondingly."}, "positive_code": [{"source_code": "I =->{gets.split.map &:to_i}\nn,x=I[]; a=I[]; b=I[]\na.sort!; b.sort!; r = n - 1; a.each{|ai|\t\n\tr -= 1 if ai + b[r] >= x\n}\nputs [1,n-r-1]*' '"}, {"source_code": "n,x=gets.split.map &:to_i\na=gets.split.map &:to_i\nb=gets.split.map &:to_i\na.sort!; b.sort!; r = n - 1; a.each{|ai|\t\n\tr -= 1 if ai + b[r] >= x\n}\nputs [1,n-r-1]*' '"}, {"source_code": "n, x = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\nb = gets.split.map(&:to_i)\na.sort! { |i, j| j <=> i }\nb.sort! \nworst = 0\nj = 0;\nfor i in 0..a.size do\n while j < b.size && a[i] + b[j] < x do\n j = j + 1\n end\n worst += 1 if j < b.size\n j = j + 1\nend\n\nputs \"1 #{worst}\"\n"}, {"source_code": "n,x=gets.split.map &:to_i\na=gets.split.map &:to_i\nb=gets.split.map &:to_i\na.sort!;b.sort!;\nz=0;n-=1;a.each{|_|\n\tif _+b[n]>=x then\n\t\tz+=1;n-=1;\n\tend\n}\nputs [1,z]*' '"}], "negative_code": [], "src_uid": "77919677f562a6fd1af64bc8cbc79de5"} {"nl": {"description": "The last contest held on Johnny's favorite competitive programming platform has been received rather positively. However, Johnny's rating has dropped again! He thinks that the presented tasks are lovely, but don't show the truth about competitors' skills.The boy is now looking at the ratings of consecutive participants written in a binary system. He thinks that the more such ratings differ, the more unfair is that such people are next to each other. He defines the difference between two numbers as the number of bit positions, where one number has zero, and another has one (we suppose that numbers are padded with leading zeros to the same length). For example, the difference of $$$5 = 101_2$$$ and $$$14 = 1110_2$$$ equals to $$$3$$$, since $$$0101$$$ and $$$1110$$$ differ in $$$3$$$ positions. Johnny defines the unfairness of the contest as the sum of such differences counted for neighboring participants.Johnny has just sent you the rating sequence and wants you to find the unfairness of the competition. You have noticed that you've got a sequence of consecutive integers from $$$0$$$ to $$$n$$$. That's strange, but the boy stubbornly says that everything is right. So help him and find the desired unfairness for received numbers.", "input_spec": "The input consists of multiple test cases. The first line contains one integer $$$t$$$ ($$$1 \\leq t \\leq 10\\,000$$$) — the number of test cases. The following $$$t$$$ lines contain a description of test cases. The first and only line in each test case contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 10^{18})$$$.", "output_spec": "Output $$$t$$$ lines. For each test case, you should output a single line with one integer — the unfairness of the contest if the rating sequence equals to $$$0$$$, $$$1$$$, ..., $$$n - 1$$$, $$$n$$$.", "sample_inputs": ["5\n5\n7\n11\n1\n2000000000000"], "sample_outputs": ["8\n11\n19\n1\n3999999999987"], "notes": "NoteFor $$$n = 5$$$ we calculate unfairness of the following sequence (numbers from $$$0$$$ to $$$5$$$ written in binary with extra leading zeroes, so they all have the same length): $$$000$$$ $$$001$$$ $$$010$$$ $$$011$$$ $$$100$$$ $$$101$$$ The differences are equal to $$$1$$$, $$$2$$$, $$$1$$$, $$$3$$$, $$$1$$$ respectively, so unfairness is equal to $$$1 + 2 + 1 + 3 + 1 = 8$$$."}, "positive_code": [{"source_code": "def solve\n n = gets.strip.to_i\n an = 0\n (0..64).each { |i|\n if !(n & (1 << i)).zero?\n an += ((1 << (i + 1)) - 1)\n end\n }\n puts an\nend\n\ngets.strip.to_i.times do\n solve\nend\n"}], "negative_code": [], "src_uid": "2a4f4c91522d83bc8f1ca2f086d24c3c"} {"nl": {"description": "Monocarp and Polycarp are learning new programming techniques. Now they decided to try pair programming.It's known that they have worked together on the same file for $$$n + m$$$ minutes. Every minute exactly one of them made one change to the file. Before they started, there were already $$$k$$$ lines written in the file.Every minute exactly one of them does one of two actions: adds a new line to the end of the file or changes one of its lines.Monocarp worked in total for $$$n$$$ minutes and performed the sequence of actions $$$[a_1, a_2, \\dots, a_n]$$$. If $$$a_i = 0$$$, then he adds a new line to the end of the file. If $$$a_i > 0$$$, then he changes the line with the number $$$a_i$$$. Monocarp performed actions strictly in this order: $$$a_1$$$, then $$$a_2$$$, ..., $$$a_n$$$.Polycarp worked in total for $$$m$$$ minutes and performed the sequence of actions $$$[b_1, b_2, \\dots, b_m]$$$. If $$$b_j = 0$$$, then he adds a new line to the end of the file. If $$$b_j > 0$$$, then he changes the line with the number $$$b_j$$$. Polycarp performed actions strictly in this order: $$$b_1$$$, then $$$b_2$$$, ..., $$$b_m$$$.Restore their common sequence of actions of length $$$n + m$$$ such that all actions would be correct — there should be no changes to lines that do not yet exist. Keep in mind that in the common sequence Monocarp's actions should form the subsequence $$$[a_1, a_2, \\dots, a_n]$$$ and Polycarp's — subsequence $$$[b_1, b_2, \\dots, b_m]$$$. They can replace each other at the computer any number of times.Let's look at an example. Suppose $$$k = 3$$$. Monocarp first changed the line with the number $$$2$$$ and then added a new line (thus, $$$n = 2, \\: a = [2, 0]$$$). Polycarp first added a new line and then changed the line with the number $$$5$$$ (thus, $$$m = 2, \\: b = [0, 5]$$$).Since the initial length of the file was $$$3$$$, in order for Polycarp to change line number $$$5$$$ two new lines must be added beforehand. Examples of correct sequences of changes, in this case, would be $$$[0, 2, 0, 5]$$$ and $$$[2, 0, 0, 5]$$$. Changes $$$[0, 0, 5, 2]$$$ (wrong order of actions) and $$$[0, 5, 2, 0]$$$ (line $$$5$$$ cannot be edited yet) are not correct.", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 1000$$$). Then $$$t$$$ test cases follow. Before each test case, there is an empty line. Each test case contains three lines. The first line contains three integers $$$k$$$, $$$n$$$, $$$m$$$ ($$$0 \\le k \\le 100$$$, $$$1 \\le n, m \\le 100$$$) — the initial number of lines in file and lengths of Monocarp's and Polycarp's sequences of changes respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$0 \\le a_i \\le 300$$$). The third line contains $$$m$$$ integers $$$b_1, b_2, \\dots, b_m$$$ ($$$0 \\le b_j \\le 300$$$).", "output_spec": "For each test case print any correct common sequence of Monocarp's and Polycarp's actions of length $$$n + m$$$ or -1 if such sequence doesn't exist.", "sample_inputs": ["5\n\n3 2 2\n2 0\n0 5\n\n4 3 2\n2 0 5\n0 6\n\n0 2 2\n1 0\n2 3\n\n5 4 4\n6 0 8 0\n0 7 0 9\n\n5 4 1\n8 7 8 0\n0"], "sample_outputs": ["2 0 0 5 \n0 2 0 6 5 \n-1\n0 6 0 7 0 8 0 9\n-1"], "notes": null}, "positive_code": [{"source_code": "gets.to_i.times {\n gets\n k, n, m = gets.split.map!(&:to_i)\n as = gets.split.map!(&:to_i)\n bs = gets.split.map!(&:to_i)\n xs = []\n i = 0; j = 0\n while i < n || j < m\n if i < n && (as[i] <= k)\n xs << as[i]\n k += 1 if as[i] == 0\n i += 1\n elsif j < m && (bs[j] <= k)\n xs << bs[j]\n k += 1 if bs[j] == 0\n j += 1\n else\n break\n end\n end\n\n if i == n && j == m\n puts xs.join(' ')\n else\n p -1\n end\n}\n"}, {"source_code": "\ndef solve(first_sec, second_sec, k)\n first_ptr = 0\n second_ptr = 0\n solution = []\n while first_ptr < first_sec.size && second_ptr < second_sec.size do\n if first_sec[first_ptr] < second_sec[second_ptr]\n return -1 if first_sec[first_ptr] > k\n solution << first_sec[first_ptr]\n first_ptr += 1\n else\n return -1 if second_sec[second_ptr] > k\n solution << second_sec[second_ptr]\n second_ptr += 1\n end\n k += 1 if solution[-1] == 0\n end\n\n while first_ptr < first_sec.size do\n return -1 if first_sec[first_ptr] > k\n solution << first_sec[first_ptr]\n first_ptr += 1\n k += 1 if solution[-1] == 0\n end\n\n while second_ptr < second_sec.size do \n return -1 if second_sec[second_ptr] > k\n solution << second_sec[second_ptr]\n second_ptr += 1\n k += 1 if solution[-1] == 0\n end\n\n solution.join(' ')\nend\n\n\nt = gets.chomp.to_i\nt.times do\n gets\n k, _ = gets.chomp.split(' ').map(&:to_i)\n first_sec = gets.chomp.split(' ').map(&:to_i)\n second_sec = gets.chomp.split(' ').map(&:to_i)\n\n puts \"#{solve(first_sec, second_sec, k)}\\n\"\nend\n"}], "negative_code": [], "src_uid": "6628bd89b4d4fdfa90d2357f7cc1b795"} {"nl": {"description": "Mike and Ann are sitting in the classroom. The lesson is boring, so they decided to play an interesting game. Fortunately, all they need to play this game is a string $$$s$$$ and a number $$$k$$$ ($$$0 \\le k < |s|$$$).At the beginning of the game, players are given a substring of $$$s$$$ with left border $$$l$$$ and right border $$$r$$$, both equal to $$$k$$$ (i.e. initially $$$l=r=k$$$). Then players start to make moves one by one, according to the following rules: A player chooses $$$l^{\\prime}$$$ and $$$r^{\\prime}$$$ so that $$$l^{\\prime} \\le l$$$, $$$r^{\\prime} \\ge r$$$ and $$$s[l^{\\prime}, r^{\\prime}]$$$ is lexicographically less than $$$s[l, r]$$$. Then the player changes $$$l$$$ and $$$r$$$ in this way: $$$l := l^{\\prime}$$$, $$$r := r^{\\prime}$$$. Ann moves first. The player, that can't make a move loses.Recall that a substring $$$s[l, r]$$$ ($$$l \\le r$$$) of a string $$$s$$$ is a continuous segment of letters from s that starts at position $$$l$$$ and ends at position $$$r$$$. For example, \"ehn\" is a substring ($$$s[3, 5]$$$) of \"aaaehnsvz\" and \"ahz\" is not.Mike and Ann were playing so enthusiastically that they did not notice the teacher approached them. Surprisingly, the teacher didn't scold them, instead of that he said, that he can figure out the winner of the game before it starts, even if he knows only $$$s$$$ and $$$k$$$.Unfortunately, Mike and Ann are not so keen in the game theory, so they ask you to write a program, that takes $$$s$$$ and determines the winner for all possible $$$k$$$.", "input_spec": "The first line of the input contains a single string $$$s$$$ ($$$1 \\leq |s| \\leq 5 \\cdot 10^5$$$) consisting of lowercase English letters.", "output_spec": "Print $$$|s|$$$ lines. In the line $$$i$$$ write the name of the winner (print Mike or Ann) in the game with string $$$s$$$ and $$$k = i$$$, if both play optimally", "sample_inputs": ["abba", "cba"], "sample_outputs": ["Mike\nAnn\nAnn\nMike", "Mike\nMike\nMike"], "notes": null}, "positive_code": [{"source_code": "require 'stringio'\nio = StringIO.new\n\nstr = gets.chomp\n\nmin_c = 'zz'\n\nstr.split('').each do |c|\n\tif c > min_c\n\t\tio.puts \"Ann\"\n\telse\n\t\tio.puts \"Mike\"\n\t\tmin_c = c\n\tend\nend\n\nprint io.string\n"}], "negative_code": [{"source_code": "require 'stringio'\nio = StringIO.new\n\nstr = gets.chomp\n\nmin_c = 'z'.next\n\nstr.split('').each do |c|\n\tif c > min_c\n\t\tio.puts \"Ann\"\n\telse\n\t\tio.puts \"Mike\"\n\t\tmin_c = c\n\tend\nend\n\nprint io.string\n"}, {"source_code": "str = gets.chomp\nks = str.length\n\nks.times do |k|\n\tl = r = k\n\n\tstart = \"Ann\"\n\n\twhile true\n\t\tif start == \"Ann\"\n\t\t\tstart = \"Mike\"\n\t\telse\n\t\t\tstart = \"Ann\"\n\t\tend\n\n\t\tif (l <= 0 || r >= str.length)\n\t\t\tp start\n\t\t\tbreak\n\t\tend\n\n\t\told_str = str[l..r]\n\t\t\n\t\tif l - 1 > 0\n\t\t\tl_str = str[l-1..r]\n\t\tend\n\n\t\tif r + 1 < str.length\n\t\t\tr_str = str[l..r+1]\n\t\tend\n\n\t\tif (l_str.nil? && r_str.nil?) || ((l_str && l_str > old_str) && (r_str && r_str > old_str))\n\t\t\tp start\n\t\t\tbreak\n\t\tend\n\n\t\tif l_str && r_str\n\t\t\tif l_str < r_str\n\t\t\t\tl = l-1\n\t\t\telse\n\t\t\t\tr = r+1 \n\t\t\tend\n\t\telse\n\t\t\tl = l-1 if l_str && r_str.nil?\n\t\t\tr = r+1 if r_str && l_str.nil?\n\t\tend\n\n\tend\nend"}], "src_uid": "1e107d9fb8bead4ad942b857685304c4"} {"nl": {"description": "Maxim wants to buy some games at the local game shop. There are $$$n$$$ games in the shop, the $$$i$$$-th game costs $$$c_i$$$.Maxim has a wallet which can be represented as an array of integers. His wallet contains $$$m$$$ bills, the $$$j$$$-th bill has value $$$a_j$$$.Games in the shop are ordered from left to right, Maxim tries to buy every game in that order.When Maxim stands at the position $$$i$$$ in the shop, he takes the first bill from his wallet (if his wallet is empty then he proceeds to the next position immediately) and tries to buy the $$$i$$$-th game using this bill. After Maxim tried to buy the $$$n$$$-th game, he leaves the shop.Maxim buys the $$$i$$$-th game if and only if the value of the first bill (which he takes) from his wallet is greater or equal to the cost of the $$$i$$$-th game. If he successfully buys the $$$i$$$-th game, the first bill from his wallet disappears and the next bill becomes first. Otherwise Maxim leaves the first bill in his wallet (this bill still remains the first one) and proceeds to the next game.For example, for array $$$c = [2, 4, 5, 2, 4]$$$ and array $$$a = [5, 3, 4, 6]$$$ the following process takes place: Maxim buys the first game using the first bill (its value is $$$5$$$), the bill disappears, after that the second bill (with value $$$3$$$) becomes the first one in Maxim's wallet, then Maxim doesn't buy the second game because $$$c_2 > a_2$$$, the same with the third game, then he buys the fourth game using the bill of value $$$a_2$$$ (the third bill becomes the first one in Maxim's wallet) and buys the fifth game using the bill of value $$$a_3$$$.Your task is to get the number of games Maxim will buy.", "input_spec": "The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n, m \\le 1000$$$) — the number of games and the number of bills in Maxim's wallet. The second line of the input contains $$$n$$$ integers $$$c_1, c_2, \\dots, c_n$$$ ($$$1 \\le c_i \\le 1000$$$), where $$$c_i$$$ is the cost of the $$$i$$$-th game. The third line of the input contains $$$m$$$ integers $$$a_1, a_2, \\dots, a_m$$$ ($$$1 \\le a_j \\le 1000$$$), where $$$a_j$$$ is the value of the $$$j$$$-th bill from the Maxim's wallet.", "output_spec": "Print a single integer — the number of games Maxim will buy.", "sample_inputs": ["5 4\n2 4 5 2 4\n5 3 4 6", "5 2\n20 40 50 20 40\n19 20", "6 4\n4 8 15 16 23 42\n1000 1000 1000 1000"], "sample_outputs": ["3", "0", "4"], "notes": "NoteThe first example is described in the problem statement.In the second example Maxim cannot buy any game because the value of the first bill in his wallet is smaller than the cost of any game in the shop.In the third example the values of the bills in Maxim's wallet are large enough to buy any game he encounter until he runs out of bills in his wallet."}, "positive_code": [{"source_code": "_,a,b=$<.map{|e|e.split.map &:to_i}\nr=0\na.each{|e|b[r].to_i>=e&&r+=1}\np r"}, {"source_code": "N, M = gets.split.map(&:to_i)\ncs = gets.split.map(&:to_i)\nas = gets.split.map(&:to_i)\nans = 0\ncs.each do |c|\n if c <= as[0]\n ans += 1\n as.shift\n break if as.length == 0\n end\nend\nputs ans"}, {"source_code": "n, m = gets.strip.split.map(&:to_i)\ncost = gets.strip.split.map(&:to_i)\nbills = gets.strip.split.map(&:to_i)\n\nans = 0\nj = 0\n\n0.upto(n-1).each do |i|\n if cost[i] <= bills[j]\n ans += 1\n j += 1\n end\n break if j >= m\nend\n\nputs ans"}, {"source_code": "n, m = gets.split.map(&:to_i)\ncs = gets.split.map(&:to_i)\nas = gets.split.map(&:to_i)\n\nans = 0\na = as.shift\n(0..n-1).each do |i|\n if a >= cs[i]\n ans += 1\n a = as.shift\n end\n break unless a\nend\n\nputs ans"}, {"source_code": "n, m = gets.split.map &:to_i\nc = gets.split.map &:to_i\na = gets.split.map &:to_i\nngames = 0\n(0...n).each do |i|\n if a[0] && a[0] >= c[i]\n ngames += 1\n a.shift\n end\nend\nputs ngames\n"}, {"source_code": "_,a,b=$<.map{|e|e.split.map &:to_i}\nr=0\na.each{|e|b[r].to_i>=e&&r+=1}\np r"}, {"source_code": "tmp=gets.split\nn=tmp[0].to_i\nm=tmp[1].to_i\nc=gets.split\na=gets.split\ni=0\nj=0\nans=0\nwhile ic[i]\n\tif a[j]>=c[i]\n\t\tans=ans+1\n\t\tj=j+1\n\tend\n\ti=i+1\t\nend\nputs ans\n"}, {"source_code": "n,m=gets.split.map(&:to_i)\nc=gets.split.map(&:to_i)\na=gets.split.map(&:to_i)\nans=0\nc.each{|v|\n break if a.empty?\n if v<=a.first\n ans+=1\n a.shift\n end\n}\n\nputs ans"}], "negative_code": [], "src_uid": "c3f080681e3da5e1290ef935ff91f364"} {"nl": {"description": "Alice and Bob play a game. There is a paper strip which is divided into n + 1 cells numbered from left to right starting from 0. There is a chip placed in the n-th cell (the last one).Players take turns, Alice is first. Each player during his or her turn has to move the chip 1, 2 or k cells to the left (so, if the chip is currently in the cell i, the player can move it into cell i - 1, i - 2 or i - k). The chip should not leave the borders of the paper strip: it is impossible, for example, to move it k cells to the left if the current cell has number i < k. The player who can't make a move loses the game.Who wins if both participants play optimally?Alice and Bob would like to play several games, so you should determine the winner in each game.", "input_spec": "The first line contains the single integer T (1 ≤ T ≤ 100) — the number of games. Next T lines contain one game per line. All games are independent. Each of the next T lines contains two integers n and k (0 ≤ n ≤ 109, 3 ≤ k ≤ 109) — the length of the strip and the constant denoting the third move, respectively.", "output_spec": "For each game, print Alice if Alice wins this game and Bob otherwise.", "sample_inputs": ["4\n0 3\n3 3\n3 4\n4 4"], "sample_outputs": ["Bob\nAlice\nBob\nAlice"], "notes": null}, "positive_code": [{"source_code": "T = gets.to_i\nT.times {\n n,k = gets.split.map &:to_i\n puts case k % 3\n when 0\n n % (k+1) % 3 == 0 && n % (k+1) != k\n else n % 3 == 0\n end ? \"Bob\" : \"Alice\"\n}"}], "negative_code": [], "src_uid": "8e0b8f3cbee8e770245de72a8fb62e05"} {"nl": {"description": "Lolek and Bolek are about to travel abroad by plane. The local airport has a special \"Choose Your Plane\" offer. The offer's conditions are as follows: it is up to a passenger to choose a plane to fly on; if the chosen plane has x (x > 0) empty seats at the given moment, then the ticket for such a plane costs x zlotys (units of Polish currency). The only ticket office of the airport already has a queue of n passengers in front of it. Lolek and Bolek have not stood in the queue yet, but they are already wondering what is the maximum and the minimum number of zlotys the airport administration can earn if all n passengers buy tickets according to the conditions of this offer?The passengers buy tickets in turn, the first person in the queue goes first, then goes the second one, and so on up to n-th person.", "input_spec": "The first line contains two integers n and m (1 ≤ n, m ≤ 1000) — the number of passengers in the queue and the number of planes in the airport, correspondingly. The next line contains m integers a1, a2, ..., am (1 ≤ ai ≤ 1000) — ai stands for the number of empty seats in the i-th plane before the ticket office starts selling tickets. The numbers in the lines are separated by a space. It is guaranteed that there are at least n empty seats in total.", "output_spec": "Print two integers — the maximum and the minimum number of zlotys that the airport administration can earn, correspondingly.", "sample_inputs": ["4 3\n2 1 1", "4 3\n2 2 2"], "sample_outputs": ["5 5", "7 6"], "notes": "NoteIn the first test sample the number of passengers is equal to the number of empty seats, so regardless of the way the planes are chosen, the administration will earn the same sum.In the second sample the sum is maximized if the 1-st person in the queue buys a ticket to the 1-st plane, the 2-nd person — to the 2-nd plane, the 3-rd person — to the 3-rd plane, the 4-th person — to the 1-st plane. The sum is minimized if the 1-st person in the queue buys a ticket to the 1-st plane, the 2-nd person — to the 1-st plane, the 3-rd person — to the 2-nd plane, the 4-th person — to the 2-nd plane."}, "positive_code": [{"source_code": "n,k = gets.chomp.split(/ /).map!{|x| x.to_i}\nra = gets.chomp.split(/ /).map!{|x| x.to_i}\nn1 = n\nn2 = n\ns1 = 0\ns2 = 0\nra2 = ra.sort\nra1 = ra.sort.reverse\nidx = 0\nwhile n1 > 0\n rew = false\n rew = true if idx >= k - 1 || ra1[idx] > ra1[idx+1]\n s1 += ra1[idx]\n ra1[idx] -= 1\n n1 -= 1\n if rew\n idx = 0\n else\n idx += 1\n end\nend\n\nidx = 0\nwhile n2 > 0\n s2 += ra2[idx]\n n2 -= 1\n ra2[idx] -= 1\n if ra2[idx] <= 0\n idx += 1\n end\nend\nputs s1.to_s + \" \" + s2.to_s\n"}, {"source_code": "n,m=gets.split.map &:to_i\na=gets.split.map &:to_i\nb = a; x=y=0\nn.times{b=b.sort;x+=b[-1];b[-1]-=1;}\nn.times{a=a.sort;y+=a[0];a[0]-=1;a=a[1..-1] if a[0]==0}\n$><<[x,y]*' '"}, {"source_code": "n, m = gets.split.map &:to_i\na = gets.split.map &:to_i\nb = a; x=y=0\nn.times{b=b.sort; x+=b[-1]; b[-1]-=1}\nn.times{a=a.sort; y+=a[0];a[0]-=1;a=a[1..-1] if a[0]==0}\n$><<[x,y]*' '\n\n\n\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t \n\t\t\t\t\t\t\t\t \n\t\t\t\t\t\t\t\t\t "}, {"source_code": "n,m=gets.split.map &:to_i\na=gets.split.map &:to_i\nb=a;x=y=0\nn.times{b=b.sort;x+=b[-1];b[-1]-=1}\nn.times{a=a.sort;y+=a[0];a[0]-=1;a=a[1..-1] if a[0]==0}\n$><<[x,y]*' '"}, {"source_code": "n,m=gets.split.map &:to_i;\na=gets.split.map &:to_i;\n\nb = a; x=y=0\n\nn.times{\n\ta=a.sort;x+=a[0];a[0]-=1;\n\ta=a[1..-1]if a[0]==0\n}\n\nn.times{\n\tb=b.sort;y+=b[-1];b[-1]-=1;#b.pop if b[-1]==0\n}\n\nputs [y, x]*' '"}, {"source_code": "n,m=gets.split.map &:to_i;\na=gets.split.map &:to_i;\n\n\nb=a; x=y=0;\n\n\nn.times{\n\ta=a.sort;x+=a[0];a[0]-=1;a=a[1..-1]if a[0]==0\n}\n\nn.times{\n\tb=b.sort;y+=b[-1];b[-1]-=1;b.pop if b[-1]==0\n}\n\nputs [y, x]*' '"}, {"source_code": "n,m=gets.split.map &:to_i;\na=gets.split.map &:to_i;\n\nb = a; x=y=0\n\nn.times{\n\ta=a.sort;x+=a[0];a[0]-=1;\n\ta=a[1..-1]if a[0]==0\n}\n\nn.times{\n\tb=b.sort;y+=b[-1];b[-1]-=1;b.pop if b[-1]==0\n}\n\nputs [y, x]*' '"}, {"source_code": "I =->{gets.split.map &:to_i}\nn,m=I[];a=I[];b=a.sort;x=y=0\nn.times{b.sort!;x+=b[-1];b[-1]-=1}\nn.times{a.sort!;y+=a[0];a[0]-=1;a=a[1..-1] if a[0]==0}\n$><<[x,y]*' '"}, {"source_code": "I =->{gets.split.map &:to_i}\nn,m=I[];a=I[];b=a.sort;x=y=0\nn.times{b.sort!;x+=b[-1];b[-1]-=1}\nn.times{a.sort!;y+=a[0];a[0]-=1;a=a[1..-1] if a[0]==0}\n$><<[x,y]*' '\n"}, {"source_code": "I =->{gets.split.map &:to_i}\nn,m=I[];a=I[];b=a.sort;x=y=0\nn.times{b.sort!;x+=b[-1];b[-1]-=1}\nn.times{a.sort!;y+=a[0];a[0]-=1;a=a[1..-1] if a[0]==0}\n$><<[x,y]*' '\n"}, {"source_code": "I =->{gets.split.map &:to_i}\nn,m=I[];a=I[];b=a.sort;x=y=0\nn.times{b.sort!;x+=b[-1];b[-1]-=1}\nn.times{a.sort!;y+=a[0];a[0]-=1;a=a[1..-1] if a[0]==0}\n$><<[x,y]*' '"}, {"source_code": "n, m = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\n\nmin_a, k, j, score_min = a.sort, n, 0, 0\nwhile k > 0\n score_min += (min_a[j] <= k ? min_a[j]*(min_a[j]+1)/2 : min_a[j]*(min_a[j]+1)/2-(min_a[j]-k)*(min_a[j]-k+1)/2)\n k -= min_a[j]\n j+=1\nend\n\nmax_a, k, score_max = a.sort.reverse, n, 0, 0\nel = max_a[0]\nc = max_a.count el\nwhile k > 0\n score_max += (c <= k ? el*c : el*k)\n el -= 1\n k -= c\n if max_a[c] == el\n while c < m and max_a[c] == el\n c+=1\n end\n end\nend\n\nputs \"#{score_max} #{score_min}\""}, {"source_code": "I =->{gets.split.map &:to_i}\nn,m=I[];a=I[];b=a.sort;x=y=0\nn.times{b.sort!;x+=b[-1];b[-1]-=1}\nn.times{a.sort!;y+=a[0];a[0]-=1;a=a[1..-1] if a[0]==0}\n$><<[x,y]*' '\n"}, {"source_code": "I =->{gets.split.map &:to_i}\nn,m=I[];a=I[];b=a.sort;x=y=0\nn.times{b.sort!;x+=b[-1];b[-1]-=1}\nn.times{a.sort!;y+=a[0];a[0]-=1;a=a[1..-1] if a[0]==0}\n$><<[x,y]*' '\n"}, {"source_code": "n,m = gets.chomp.split.collect{|i| i.to_i}\nseats = gets.chomp.split.collect{|i| i.to_i}.sort\n\nminz = 0\nmint = 0\nnplanes = seats.size\n0.upto(nplanes-1) do |i|\n s = seats[i]\n while mint!=n and s!=0\n minz += s\n s -= 1\n mint += 1\n end\n break if mint == n\nend\nmaxz = 0\nmaxt = 0\nseats = seats.reverse\ni = 0\nwhile maxt != n\n maxz += seats[0]\n seats[0] -= 1\n maxt += 1\n seats[0] = nil if seats[0] == 0\n seats = seats.compact.sort.reverse\nend\n\nputs \"#{maxz} #{minz}\"\n"}, {"source_code": "n, m = gets.chomp.split.map{|e|e.to_i}\na = Array.new(m){}\na = gets.chomp.split.map{|e|e.to_i}\nb = Marshal.load(Marshal.dump(a))\nmax = min = 0\n\nn.times do |i|\n a.sort!{|c, d| d <=> c }.reject!{|c| c == 0}\n max += a[0]\n a[0] -= 1\nend\n\nn.times do |i|\n b.sort!.reject!{|c| c == 0}\n min += b[0]\n b[0] -= 1\nend\n\nputs \"#{max} #{min}\""}, {"source_code": "#!/usr/bin/ruby\nn,m = gets.chomp.split(' ').map{|x| x.to_i}\na = gets.chomp.split(' ').map{|x| x.to_i}\natm = a.clone\nminc = 0\nmaxc = 0\nfor i in 0..n-1\n max = -1\n k = 0\n for j in 0..m-1\n if atm[j] > max && atm[j] > 0\n max = atm[j]\n k = j\n end\n end\n atm[k]-=1\n maxc += max\nend\nfor i in 0..n-1\n min = 2147483647\n k = 0\n for j in 0..m-1\n if a[j] < min && a[j] > 0\n min = a[j]\n k = j\n end\n end\n a[k]-=1;\n minc += min\nend\nputs \"#{maxc} #{minc}\"\n"}, {"source_code": "n,m=gets.split.map &:to_i;a=gets.split.map &:to_i;b=a[0,m];x=y=0;n.times{a=a.sort;x+=a[0];a[0]-=1;a=a[1..-1]if a[0]==0};n.times{b=b.sort;y+=b[-1];b[-1]-=1;b.pop if b[-1]==0};$><<[y,x]*' '"}, {"source_code": "I =->{gets.split.map &:to_i}\nn,m=I[];a=I[];b=a.sort;x=y=0\nn.times{b.sort!;x+=b[-1];b[-1]-=1}\nn.times{a.sort!;y+=a[0];a[0]-=1;a=a[1..-1] if a[0]==0}\n$><<[x,y]*' '\n"}, {"source_code": "n,m=gets.split.map &:to_i\na=gets.split.map &:to_i\nb=a;x=y=0\nn.times{b=b.sort;x+=b[-1];b[-1]-=1}\nn.times{a=a.sort;y+=a[0];a[0]-=1;a=a[1..-1] if a[0]==0}\n$><<[x,y]*' '"}, {"source_code": "n, m = gets.split.map(&:to_i)\nar_down = gets.split.map(&:to_i).sort { |e| -e }\nar_up = ar_down.sort\n\n\nmin = max = 0\nn_up = n\nn_down = n\n\n\nar_up.each do |e|\n if n_up > e\n n_up -= e\n min += e*(1 + e)/2\n else\n min += (n_up)*(e-n_up+e+1)/2\n break\n end\nend\n\n\nn_down.times do\n m = ar_down.max\n max += m\n ar_down[ar_down.find_index(m)] -=1\nend\n\n\nputs \"#{max} #{min}\""}, {"source_code": "I =->{gets.split.map &:to_i}\nn,m=I[];a=I[];b=a.sort;x=y=0\nn.times{b.sort!;x+=b[-1];b[-1]-=1}\nn.times{a.sort!;y+=a[0];a[0]-=1;a=a[1..-1] if a[0]==0}\n$><<[x,y]*' '\n"}, {"source_code": "def calc(p, a)\n cost = 0\n while p > 0\n index = yield(a)\n cost += a[index]\n a[index] -= 1\n p -= 1\n end\n cost\nend\n\nn, _ = gets.chomp.split.map { |x| x.to_i }\na = gets.chomp.split.map { |x| x.to_i }\nputs [calc(n, a.clone) { |x| max, index = -1, 0; x.each_with_index { |e, i| if e > max; max = e; index = i end }; index },\n calc(n, a) { |x| min, index = 1001, 0; x.each_with_index { |e, i| if e > 0 && e < min; min = e; index = i end }; index }] * ' '"}, {"source_code": "I =->{gets.split.map &:to_i}\nn,m=I[];a=I[];b=a.sort;x=y=0\nn.times{b.sort!;x+=b[-1];b[-1]-=1}\nn.times{a.sort!;y+=a[0];a[0]-=1;a=a[1..-1] if a[0]==0}\n$><<[x,y]*' '\n"}, {"source_code": "I =->{gets.split.map &:to_i}\nn,m=I[];a=I[];b=a.sort;x=y=0\nn.times{b.sort!;x+=b[-1];b[-1]-=1}\nn.times{a.sort!;y+=a[0];a[0]-=1;a=a[1..-1] if a[0]==0}\n$><<[x,y]*' '\n"}], "negative_code": [{"source_code": "n,m=gets.split.map &:to_i\na=gets.split.map &:to_i\nb = a; x=y=0\nn.times{b=b.sort;x+=b[-1];b[-1]-=1;}\nn.times{a=a.sort;y+=a[0];a[0]-=1;a=a[1..-1] if a[0]==0}\n$><<[y,x]*' '"}, {"source_code": "n,m=gets.split.map &:to_i;\na=gets.split.map &:to_i;\n\n\nb = a; x=y=0;\n\nn.times{\n\ta=a.sort;x+=a[0];a[0]-=1;\t\n}\n\n\nn.times{\n\tb=b.sort;y+=b[-1];b[-1]-=1;b.pop if b[-1]==0\n}\n\nputs [y, x]*' '"}, {"source_code": "I =->{gets.split.map &:to_i}\nn,m=I[];a=I[];b=a;x=y=0\nn.times{b.sort!;x+=b[-1];b[-1]-=1}\nn.times{a.sort!;y+=a[0];a[0]-=1;a=a[1..-1] if a[0]==0}\n$><<[x,y]*' '"}, {"source_code": "n, m = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\n\nmin_a, k, j, score_min = a.sort, n, 0, 0\nwhile k > 0\n score_min += (min_a[j] <= k ? min_a[j]*(min_a[j]+1)/2 : k*(k+1)/2)\n k -= min_a[j]\n j+=1\nend\n\nmax_a, k, score_max = a.sort.reverse, n, 0, 0\nel = max_a[0]\nc = max_a.count el\nwhile k > 0\n score_max += (c <= k ? el*c : el*k)\n el -= 1\n k -= c\n if max_a[c] == el\n while c < m and a[c] == el\n c+=1\n end\n end\nend\n\nputs \"#{score_max} #{score_min}\""}, {"source_code": "n, m = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\n\nmin_a, k, j, score_min = a.sort, n, 0, 0\nwhile k > 0\n score_min += (min_a[j] <= k ? min_a[j]*(min_a[j]+1)/2 : min_a[j]*(min_a[j]+1)/2-(min_a[j]-k)*(min_a[j]-k+1)/2)\n k -= min_a[j]\n j+=1\nend\n\nmax_a, k, score_max = a.sort.reverse, n, 0, 0\nel = max_a[0]\nc = max_a.count el\nwhile k > 0\n score_max += (c <= k ? el*c : el*k)\n el -= 1\n k -= c\n if max_a[c] == el\n while c < m and a[c] == el\n c+=1\n end\n end\nend\n\nputs \"#{score_max} #{score_min}\""}, {"source_code": "n,m = gets.chomp.split.collect{|i| i.to_i}\nseats = gets.chomp.split.collect{|i| i.to_i}.sort\n\nminz = 0\nmint = 0\nnplanes = seats.size\n0.upto(nplanes-1) do |i|\n s = seats[i]\n while mint!=n and s!=0\n minz += s\n s -= 1\n mint += 1\n end\n break if mint == n\nend\nmaxz = 0\nmaxt = 0\nseats = seats.reverse\ni = 0\nwhile maxt != n\n if seats[i] > 0 then \n maxz += seats[i]\n seats[i] -= 1\n maxt += 1\n end\n i += 1\n i %= nplanes\nend\n\nputs \"#{maxz} #{minz}\"\n"}, {"source_code": "n,m = gets.chomp.split.collect{|i| i.to_i}\nseats = gets.chomp.split.collect{|i| i.to_i}.sort\n\nminz = 0\nmint = 0\nnplanes = seats.size\n0.upto(nplanes-1) do |i|\n s = seats[i]\n while mint!=n and s!=0\n minz += s\n s -= 1\n mint += 1\n end\n break if mint == n\nend\nmaxz = 0\nmaxt = 0\nseats = seats.reverse\ni = 0\nwhile maxt != n\n while seats[i] >= seats[(i+1)%nplanes] and maxt!=n\n maxz += seats[i]\n seats[i] -= 1\n maxt += 1\n end\n break if maxt == n\n i += 1\n i %= nplanes\nend\n\nputs \"#{maxz} #{minz}\"\n"}], "src_uid": "6dea4611ca210b34ae2da93ebfa9896c"} {"nl": {"description": "Once Bob took a paper stripe of n squares (the height of the stripe is 1 square). In each square he wrote an integer number, possibly negative. He became interested in how many ways exist to cut this stripe into three pieces so that the sum of numbers from each piece is equal to the sum of numbers from any other piece, and each piece contains positive integer amount of squares. Would you help Bob solve this problem?", "input_spec": "The first input line contains integer n (1 ≤ n ≤ 105) — amount of squares in the stripe. The second line contains n space-separated numbers — they are the numbers written in the squares of the stripe. These numbers are integer and do not exceed 10000 in absolute value.", "output_spec": "Output the amount of ways to cut the stripe into three non-empty pieces so that the sum of numbers from each piece is equal to the sum of numbers from any other piece. Don't forget that it's allowed to cut the stripe along the squares' borders only.", "sample_inputs": ["4\n1 2 3 3", "5\n1 2 3 4 5"], "sample_outputs": ["1", "0"], "notes": null}, "positive_code": [{"source_code": "def get_right_count(array, value)\n n, sum = array.size, array[-1]\n result = Array.new(n, 0)\n result[-1] = 1 if sum == value\n (array.size - 2).downto(0) do |index|\n sum += array[index]\n result[index] += result[index + 1]\n result[index] += 1 if sum == value\n end\n result\nend\n\nn = gets.to_i\nvalues = gets.split.map { |v| v.to_i }\nsum, result = values.inject { |s, v| s + v }, 0\n\nif sum % 3 == 0\n sum /= 3\n left_sum, count = 0, get_right_count(values, sum) + [0, 0]\n values.each_index do |index|\n left_sum += values[index]\n result += count[index + 2] if left_sum == sum\n end\nend\n\nputs result\n\n"}, {"source_code": "def solve(a)\n sum = a.inject { |s, x| s + x }\n return 0 unless sum % 3 == 0\n one_third = sum / 3\n a.pop\n local = 0\n thirds_count = 0\n ans = 0\n a.each do |x|\n local += x\n ans += thirds_count if local == one_third * 2\n thirds_count += 1 if local == one_third\n end\n return ans\nend\n\ngets\na = gets.split(' ').map { |x| x.to_i }\n#a = [1, 2, 3, 3]\nputs solve(a)\n\n\n"}], "negative_code": [{"source_code": "def get_right_count(array, value)\n n, sum = array.size, 0\n result = Array.new(n, 0)\n result[-1] = 1 if array[-1] == value\n (array.size - 2).downto(0) do |index|\n sum += array[index]\n result[index] += result[index + 1]\n result[index] += 1 if sum == value\n end\n result\nend\n\nn = gets.to_i\nvalues = gets.split.map { |v| v.to_i }\nsum, result = values.inject { |s, v| s + v }, 0\n\nif sum % 3 == 0\n sum /= 3\n left_sum, count = 0, get_right_count(values, sum) + [0, 0]\n values.each_index do |index|\n left_sum += values[index]\n result += count[index + 2] if left_sum == sum\n end\nend\n\nputs result\n\n"}], "src_uid": "4798211615bcff8730378330756ae63f"} {"nl": {"description": "Roma works in a company that sells TVs. Now he has to prepare a report for the last year.Roma has got a list of the company's incomes. The list is a sequence that consists of n integers. The total income of the company is the sum of all integers in sequence. Roma decided to perform exactly k changes of signs of several numbers in the sequence. He can also change the sign of a number one, two or more times.The operation of changing a number's sign is the operation of multiplying this number by -1.Help Roma perform the changes so as to make the total income of the company (the sum of numbers in the resulting sequence) maximum. Note that Roma should perform exactly k changes.", "input_spec": "The first line contains two integers n and k (1 ≤ n, k ≤ 105), showing, how many numbers are in the sequence and how many swaps are to be made. The second line contains a non-decreasing sequence, consisting of n integers ai (|ai| ≤ 104). The numbers in the lines are separated by single spaces. Please note that the given sequence is sorted in non-decreasing order.", "output_spec": "In the single line print the answer to the problem — the maximum total income that we can obtain after exactly k changes.", "sample_inputs": ["3 2\n-1 -1 1", "3 1\n-1 -1 1"], "sample_outputs": ["3", "1"], "notes": "NoteIn the first sample we can get sequence [1, 1, 1], thus the total income equals 3.In the second test, the optimal strategy is to get sequence [-1, 1, 1], thus the total income equals 1."}, "positive_code": [{"source_code": "n, k = gets.split(' ').map{|i| i.to_i}\na = gets.split(' ').map{|i| i.to_i}\ni = 0\nwhile k > 0\n\tif i < a.length and a[i] < 0\n\t\ta[i] *= -1\n\t\tk -= 1\n\t\ti += 1\n\telse\n\t\tbreak\n\tend\nend\n\nsum = a.reduce{|a, b| a+b}\nsum -= (k%2) * a.min * 2\nputs sum"}, {"source_code": "# -*- encoding: utf-8 -*-\n\nn, k = gets.chomp.split(\" \").map{|n| n.to_i}\nsequence = gets.chomp.split(\" \").map{|n| n.to_i}\nsequence.sort!\n\ni = 0\nk.times do\n break if i >= n or sequence[i] >= 0\n \n sequence[i] *= -1 \n i += 1\nend\n\nif (k - i) > 0\n sequence.sort!\n if (k - i) % 2 == 1\n sequence[0] *= -1\n end\nend\n\nputs sequence.inject{|sum, i| sum+= i}\n"}, {"source_code": "n,k=gets.split(' ').map(&:to_i)\na=gets.split(' ').map(&:to_i)\nneg=a.count {|i| i<0}\n[k,neg].min.times {|i| a[i]=-a[i]}\nmin=a.min_by {|i| i.abs}\nsum=a.inject(:+)\nk-=neg\nif k>0 && k%2==1\n sum-=2*min\nend\nputs sum"}, {"source_code": "def run\n lines = ARGF.readlines\n\n n, k = lines.shift.chomp.split(\" \").map {|x| x.to_i }\n vals = lines.shift.chomp.split(\" \").map {|x| x.to_i }\n\n vals.length.times do |i|\n if vals[i] < 0\n vals[i] *= -1\n k -= 1\n else\n if k % 2 == 1\n if vals[i - 1] < vals[i]\n vals[i - 1] *= -1\n else\n vals[i] *= -1\n end\n end\n k = 0\n end\n\n break if k == 0\n end\n\n vals[vals.length - 1] *= -1 if k % 2 == 1\n\n puts vals.reduce(0) { |sum, n| sum + n }\nend\n\nrun if $0 == __FILE__\n"}, {"source_code": "n,k = gets.chomp.split(\" \").collect! {|x| x.to_i}\nline = gets.chomp.split(\" \").collect! {|x| x.to_i}\n\ns = 0\nmin = 10001\nline.sort!\nline.each {|x|\n if x<0\n\tif k>0\n\t\tk -= 1\n\t\ts += x.abs\n\telse\n\t\ts += x\n\tend\n\tmin = x.abs\n else\n\tif min > x\n\t\tmin = x\n\tend\n\ts += x\n end\n}\n\nif k%2 == 1\n\ts -= (2*min)\nend\n\nputs s"}, {"source_code": "n, k = gets.split.collect{|i| i.to_i}\na = gets.split.collect{|i| i.to_i}.sort\nans = 0\ncnt = 0\na.each{|i| cnt+=1 if i<=0}\nif cnt>=k\n n.times do |i|\n if i < k\n ans += -a[i]\n else\n ans += a[i]\n end\n end\nelse\n k -= cnt\n a = a.collect{|i| i.abs}\n if k.odd?\n ans = a.inject{|sum, i| sum+=i}-2*a.min\n else\n ans = a.inject{|sum, i| sum+=i}\n end\nend\nputs ans\n"}, {"source_code": "n,k=gets.split.map(&:to_i)\na=gets.split.map(&:to_i)\n\nneg=a.count {|i| i<0}\n[k,neg].min.times {|i| a[i]=-a[i]}\n\nmin=a.min_by {|i| i.abs}\nsum=a.inject(:+)\n\nk-=neg\nsum-=2*min if k>0 && k%2==1\n\np sum\n"}, {"source_code": "n,k=gets.split.map(&:to_i)\na=gets.split.map(&:to_i)\n\nneg=a.count {|i| i<0}\n[k,neg].min.times {|i| a[i]=-a[i]}\n\nsum=a.inject(:+)\n\nk-=neg\nsum-=2*a.min if k>0 && k%2==1\n\np sum\n"}, {"source_code": "n,k=gets.split.map(&:to_i)\na=gets.split.map(&:to_i)\n\nneg=a.count {|i| i<0}\n[k,neg].min.times {|i| a[i]=-a[i]}\n\nmin=a.min_by {|i| i.abs}\nsum=a.inject(:+)\n\nk-=neg\nif k>0 && k%2==1\n sum-=2*min\nend\n\np sum\n"}, {"source_code": "n,k = gets.split.map &:to_i\na = gets.split.map &:to_i\ncounter = 0\na.each {|el| counter+=1 if el < 0}\nnull_position = a.index(0)\npre_null = nil\n\nif counter >= k\n k.times do |index|\n a[index] = -a[index]\n end\nelsif null_position\n a.each_with_index do |el,index|\n if el<0\n a[index] *= -1\n else\n break\n end\n end\nelse\n a.each_with_index do |el,index|\n if el<0\n a[index] = -el\n else\n pre_null = index-1\n break\n end\n end\n if (k-counter) % 2 != 0\n a[a.index(a.min)] *= -1\n end\n\nend\nputs a.inject{|sum, el| sum+el}\n"}, {"source_code": "def read_next_line\n gets.split.map(&:to_i)\nend\n\nn, k = read_next_line\na = read_next_line\ni, p = 0, [n, k].min\nwhile i < p && a[i] < 0\n a[i] = -a[i]\n i += 1\nend\nif (k - i).odd?\n i -= 1 if i == n || (i > 0 && a[i - 1] < a[i])\n a[i] = -a[i]\nend\nputs a.inject(:+)"}, {"source_code": "BEGIN{\n\tdef get_line_int()\n\t\treturn gets.split.map(&:to_i)\n\tend\n\tdef main()\n\t\tn, k = get_line_int\n\t\ta = get_line_int\n\t\tsum = 0\n\t\ta.each_with_index do |x,i|\n\t\t\tbreak if k == 0\n\t\t\tif x < 0 then\n\t\t\t\ta[i] *= -1\n\t\t\t\tk -= 1\n\t\t\telse\n\t\t\t\tbreak\n\t\t\tend\n\t\tend\n\t\ta.each do |x|\n\t\t\tsum += x\n\t\tend\n\t\tif k != 0 then\n\t\t\tsum -= a.min * 2 if k % 2 == 1\n\t\tend\n\t\tputs sum\n\tend\n\tTest_case = 1\n\tTest_case.times do\n\t\tmain()\n\tend\n}\n"}, {"source_code": "a = gets.split(\" \").collect{|x| x.to_i}\nn = a[0]\nk = a[1]\n\na = gets.split(\" \").collect{|x| x.to_i}\nresult = 0\n\na.each_index { |i| \n\tif a[i] < 0 and k > 0\n\t\ta[i] *= -1\n\t\tk -= 1\n\tend\n}\n\nresult = a.inject{|result, x| result += x}\n\nif k % 2 == 1\n\tm = a.min { |a, b| a.abs <=> b.abs }\n\tresult -= 2*m\nend\n\nputs result"}, {"source_code": "n, k = gets.chomp.split.map(&:to_i)\nputs gets.chomp.split.map(&:to_i).map { |x| (k > 0 && x < 0) ? (k -= 1; -x) : x }.sort.tap { |x| k = k % 2 }.map { |x| \n\tk > 0 ? (k = 0; -x ) : x }.reduce(:+)\n"}], "negative_code": [{"source_code": "# -*- encoding: utf-8 -*-\n\nn, k = gets.chomp.split(\" \").map{|n| n.to_i}\nsequence = gets.chomp.split(\" \").map{|n| n.to_i}\nsequence.sort!\n\nidx = 0\n\nk.times do\n if idx < n\n sequence[idx] *= -1\n idx += 1\n else\n sequence.sort!\n idx = 0\n end\nend\n\n\nputs sequence.inject{|sum, i| sum+= i}\n"}, {"source_code": "def run\n lines = ARGF.readlines\n\n n, k = lines.shift.chomp.split(\" \").map {|x| x.to_i }\n vals = lines.shift.chomp.split(\" \").map {|x| x.to_i }\n\n vals.length.times do |i|\n if vals[i] < 0\n vals[i] *= -1\n k -= 1\n else\n if vals[i - 1] < vals[i]\n vals[i - 1] *= -1\n else\n vals[i] *= -1\n end\n k = 0\n end\n\n break if k == 0\n end\n\n puts vals.reduce(0) { |sum, n| sum + n }\nend\n\nrun if $0 == __FILE__\n"}, {"source_code": "def run\n lines = ARGF.readlines\n\n n, k = lines.shift.chomp.split(\" \").map {|x| x.to_i }\n vals = lines.shift.chomp.split(\" \").map {|x| x.to_i }\n\n vals.length.times do |i|\n if vals[i] < 0\n vals[i] *= -1\n k -= 1\n else\n if vals[i - 1].abs < vals[i].abs\n vals[i - 1] *= -1\n else\n vals[i] *= -1\n end\n k = 0\n end\n\n break if k == 0\n end\n\n puts vals.reduce(0) { |sum, n| sum + n }\nend\n\nrun if $0 == __FILE__\n"}, {"source_code": "def run\n lines = ARGF.readlines\n\n n, k = lines.shift.chomp.split(\" \").map {|x| x.to_i }\n vals = lines.shift.chomp.split(\" \").map {|x| x.to_i }\n\n vals.length.times do |i|\n if vals[i] < 0\n vals[i] *= -1\n k -= 1\n else\n if vals[i - 1].abs < vals[i].abs\n vals[i - 1] *= -1\n else\n vals[i] *= -1\n end\n k = 0\n end\n\n break if k == 0\n end\n\n puts vals.reduce(0) { |sum, n| sum + n }\nend\n\nrun if $0 == __FILE__\n"}, {"source_code": "def run\n lines = ARGF.readlines\n\n n, k = lines.shift.chomp.split(\" \").map {|x| x.to_i }\n vals = lines.shift.chomp.split(\" \").map {|x| x.to_i }\n\n vals.length.times do |i|\n if vals[i] < 0\n vals[i] *= -1\n k -= 1\n else\n if k % 2 == 1\n if vals[i - 1] < vals[i]\n vals[i - 1] *= -1\n else\n vals[i] *= -1\n end\n end\n k = 0\n end\n\n break if k == 0\n end\n\n puts vals.reduce(0) { |sum, n| sum + n }\nend\n\nrun if $0 == __FILE__\n"}, {"source_code": "n,k = gets.chomp.split(\" \").collect! {|x| x.to_i}\nline = gets.chomp.split(\" \").collect! {|x| x.to_i}\n\ns = 0\nmin = 0\nline.each {|x|\n if x<0\n\tif k>0\n\t\tk -= 1\n\t\ts += x.abs\n\telse\n\t\ts += x\n\tend\n\tmin = x.abs\n else\n\tif min > x\n\t\tmin = x\n\tend\n\ts += x\n end\n}\n\nif k%2 == 1\n\ts -= (2*min)\nend\n\nputs s"}, {"source_code": "n,k = gets.chomp.split(\" \").collect! {|x| x.to_i}\nline = gets.chomp.split(\" \").collect! {|x| x.to_i}\n\ns = 0\nmin = 0\nline.sort!\nline.each {|x|\n if x<0\n\tif k>0\n\t\tk -= 1\n\t\ts += x.abs\n\telse\n\t\ts += x\n\tend\n\tmin = x.abs\n else\n\tif min > x\n\t\tmin = x\n\tend\n\ts += x\n end\n}\n\nif k%2 == 1\n\ts -= (2*min)\nend\n\nputs s"}, {"source_code": "n,k = gets.chomp.split(\" \").collect! {|x| x.to_i}\nline = gets.chomp.split(\" \").collect! {|x| x.to_i}\n\ns = 0\nmin = 0\nline.each {|x|\n if x<0\n\tif k>0\n\t\tk -= 1\n\t\ts += x.abs\n\telse\n\t\ts += x\n\tend\n\tmin = x.abs\n else\n\tif min > x\n\t\tmin = x\n\tend\n\ts += x\n end\n}\n\nif k%2 == 1\n\ts -= min\nend\n\nputs s"}, {"source_code": "n, k = gets.split.collect{|i| i.to_i}\na = gets.split.collect{|i| i.to_i}.sort\nans = 0\nn.times do |i|\n if i < k\n ans += -a[i]\n else\n ans += a[i]\n end\nend\nputs ans\n"}, {"source_code": "n,k=gets.split.map(&:to_i)\na=gets.split.map(&:to_i)\nk%=2*n\n\n[k,n].min.times {|i| a[i]=-a[i]}\nk-=n\n(-1..-k).each {|i| a[i]=-a[i]} if k>0\n\nputs a.inject(:+)\n"}, {"source_code": "n,k=gets.split.map(&:to_i)\na=gets.split.map(&:to_i)\n\nk.times do |i|\n if i=n\n a[0]=-a[0]\n elsif a[i]<0\n a[i]=-a[i]\n elsif a[i]==0\n break\n else\n a[0]=-a[0]\n end\nend\n\nputs a.inject(:+)\n"}, {"source_code": "n,k=gets.split.map(&:to_i)\na=gets.split.map(&:to_i)\nk.times {|i| a[i%n]=-a[i%n]}\nputs a.inject(:+)\n"}, {"source_code": "a = gets.split(\" \").collect{|x| x.to_i}\nn = a[0]\nk = a[1]\n\na = gets.split(\" \").collect{|x| x.to_i}\nresult = 0\nminus = 0\n\na.sort!\n\na.each{|x| \n\tif x < 0\n\t\tminus += 1\n\tend\n}\n\nif minus >= k\n\ta.reverse_each { |e| \n\t\tif e < 0 and k > 0\n\t\t\te *= -1\n\t\t\tk -= 1\n\t\tend\n\t\tresult += e\n\t}\nelse \n\ta.inject{|result, x| result += x}\n\tk -= n\n\tif k % 2 == 1\n\t\tresult -= a[0]\n\tend\nend\n\nputs result"}], "src_uid": "befd3b1b4afe19ff619c0b34ed1a4966"} {"nl": {"description": "Vasiliy has an exam period which will continue for n days. He has to pass exams on m subjects. Subjects are numbered from 1 to m.About every day we know exam for which one of m subjects can be passed on that day. Perhaps, some day you can't pass any exam. It is not allowed to pass more than one exam on any day. On each day Vasiliy can either pass the exam of that day (it takes the whole day) or prepare all day for some exam or have a rest. About each subject Vasiliy know a number ai — the number of days he should prepare to pass the exam number i. Vasiliy can switch subjects while preparing for exams, it is not necessary to prepare continuously during ai days for the exam number i. He can mix the order of preparation for exams in any way.Your task is to determine the minimum number of days in which Vasiliy can pass all exams, or determine that it is impossible. Each exam should be passed exactly one time. ", "input_spec": "The first line contains two integers n and m (1 ≤ n, m ≤ 105) — the number of days in the exam period and the number of subjects. The second line contains n integers d1, d2, ..., dn (0 ≤ di ≤ m), where di is the number of subject, the exam of which can be passed on the day number i. If di equals 0, it is not allowed to pass any exams on the day number i. The third line contains m positive integers a1, a2, ..., am (1 ≤ ai ≤ 105), where ai is the number of days that are needed to prepare before passing the exam on the subject i.", "output_spec": "Print one integer — the minimum number of days in which Vasiliy can pass all exams. If it is impossible, print -1.", "sample_inputs": ["7 2\n0 1 0 2 1 0 2\n2 1", "10 3\n0 0 1 2 3 0 2 0 1 2\n1 1 4", "5 1\n1 1 1 1 1\n5"], "sample_outputs": ["5", "9", "-1"], "notes": "NoteIn the first example Vasiliy can behave as follows. On the first and the second day he can prepare for the exam number 1 and pass it on the fifth day, prepare for the exam number 2 on the third day and pass it on the fourth day.In the second example Vasiliy should prepare for the exam number 3 during the first four days and pass it on the fifth day. Then on the sixth day he should prepare for the exam number 2 and then pass it on the seventh day. After that he needs to prepare for the exam number 1 on the eighth day and pass it on the ninth day. In the third example Vasiliy can't pass the only exam because he hasn't anough time to prepare for it. "}, "positive_code": [{"source_code": "eval\"N,M,*D=\"+$<.read.split*?,\np (M+D[N,M].inject(:+)..N).find{|i|D[i-1]>0}||-1"}, {"source_code": "def func x\n return true if x>=@n\n h={}\n i=x\n s=0\n while i>=0\n k=@d[i]\n if k>=0 and !h[k]\n h[k]=1\n s+=@a[k]\n elsif s>0\n s-=1\n end\n i-=1\n end\n h.size==@m and s<=0\nend\n\n@n, @m=gets.split.map &:to_i\n@d=gets.split.map{|e| e.to_i-1}\n@a=gets.split.map &:to_i\n\nx=1<<32\nres=2*x-1\nwhile x>0\n res-=x if func res-x\n x>>=1\nend\n\nputs (res<@n ? res+1 : -1)\n"}, {"source_code": "eval\"N,M,*D=\"+$<.read.split*?,\na=D.pop M\nx=r=-1+t=1<<32\n32.times{(h={};s=0;(0..x).map{|i|!h[k=D[x-i]]&&k>0?h[k]=s+=a[k-1]:s>0&&s-=1}\nh.size==M&&s<1&&r=x)if f=N>x=r-t/=2;f||r=x}\np r=@n\n i, s, h=x, 0, {}\n while i>=0\n k=@d[i]\n k<0 or h[k] ? s=s>0 ? s-1 : 0 : (h[k]=1; s+=@a[k])\n i-=1\n end\n h.size==@m and s==0\nend\n\n@n, @m=gets.split.map &:to_i\n@d=gets.split.map{|e| e.to_i-1}\n@a=gets.split.map &:to_i\n\nx=1<<32\nres=2*x-1\nwhile x>0\n res-=x if func res-x\n x>>=1\nend\n\nputs (res<@n ? res+1 : -1)\n"}, {"source_code": "def func x\n return true if x>=@n\n h={}\n i=x\n p=x\n while i>=0\n k=@d[i]\n if k>=0 and !h[k]\n h[k]=1\n return false if @a[k]>p\n p-=@a[k]\n else\n p-=1\n end\n i-=1\n end\n h.size==@m\nend\n\n@n, @m=gets.split.map &:to_i\n@d=gets.split.map{|e| e.to_i-1}\n@a=gets.split.map &:to_i\n\nx=1<<32\nres=2*x-1\nwhile x>0\n res-=x if func res-x\n x>>=1\nend\n\nputs (res<@n ? res+1 : -1)\n"}, {"source_code": "def func x\n return true if x>=@n\n h={}\n i=x\n s=0\n while i>=0\n k=@d[i]\n if k>=0 and !h[k]\n h[k]=1\n s+=@a[k]\n else\n s-=1\n end\n i-=1\n end\n s<=0\nend\n\n@n, @m=gets.split.map &:to_i\n@d=gets.split.map{|e| e.to_i-1}\n@a=gets.split.map &:to_i\n\nx=1<<32\nres=2*x-1\nwhile x>0\n res-=x if func res-x\n x>>=1\nend\n\nputs (res<@n ? res+1 : -1)\n"}, {"source_code": "def func x\n return true if x>=@n\n h={}\n i=x\n s=0\n while i>=0\n k=@d[i]\n if k>=0 and !h[k]\n h[k]=1\n s+=@a[k]\n else\n s-=1\n end\n i-=1\n end\n h.size==@m and s<=0\nend\n\n@n, @m=gets.split.map &:to_i\n@d=gets.split.map{|e| e.to_i-1}\n@a=gets.split.map &:to_i\n\nx=1<<32\nres=2*x-1\nwhile x>0\n res-=x if func res-x\n x>>=1\nend\n\nputs (res<@n ? res+1 : -1)\n"}], "src_uid": "ab31ad331748994ddeb08be646d0787e"} {"nl": {"description": "Innopolis University scientists continue to investigate the periodic table. There are n·m known elements and they form a periodic table: a rectangle with n rows and m columns. Each element can be described by its coordinates (r, c) (1 ≤ r ≤ n, 1 ≤ c ≤ m) in the table.Recently scientists discovered that for every four different elements in this table that form a rectangle with sides parallel to the sides of the table, if they have samples of three of the four elements, they can produce a sample of the fourth element using nuclear fusion. So if we have elements in positions (r1, c1), (r1, c2), (r2, c1), where r1 ≠ r2 and c1 ≠ c2, then we can produce element (r2, c2). Samples used in fusion are not wasted and can be used again in future fusions. Newly crafted elements also can be used in future fusions.Innopolis University scientists already have samples of q elements. They want to obtain samples of all n·m elements. To achieve that, they will purchase some samples from other laboratories and then produce all remaining elements using an arbitrary number of nuclear fusions in some order. Help them to find the minimal number of elements they need to purchase.", "input_spec": "The first line contains three integers n, m, q (1 ≤ n, m ≤ 200 000; 0 ≤ q ≤ min(n·m, 200 000)), the chemical table dimensions and the number of elements scientists already have. The following q lines contain two integers ri, ci (1 ≤ ri ≤ n, 1 ≤ ci ≤ m), each describes an element that scientists already have. All elements in the input are different.", "output_spec": "Print the minimal number of elements to be purchased.", "sample_inputs": ["2 2 3\n1 2\n2 2\n2 1", "1 5 3\n1 3\n1 1\n1 5", "4 3 6\n1 2\n1 3\n2 2\n2 3\n3 1\n3 3"], "sample_outputs": ["0", "2", "1"], "notes": "NoteFor each example you have a picture which illustrates it.The first picture for each example describes the initial set of element samples available. Black crosses represent elements available in the lab initially.The second picture describes how remaining samples can be obtained. Red dashed circles denote elements that should be purchased from other labs (the optimal solution should minimize the number of red circles). Blue dashed circles are elements that can be produced with nuclear fusion. They are numbered in order in which they can be produced.Test 1We can use nuclear fusion and get the element from three other samples, so we don't need to purchase anything. Test 2We cannot use any nuclear fusion at all as there is only one row, so we have to purchase all missing elements. Test 3There are several possible solutions. One of them is illustrated below.Note that after purchasing one element marked as red it's still not possible to immidiately produce the middle element in the bottom row (marked as 4). So we produce the element in the left-top corner first (marked as 1), and then use it in future fusions. "}, "positive_code": [{"source_code": "class UnionFind\n attr_reader :set_size\n def initialize(n)\n @par = Array.new(n)\n n.times{ |i| @par[i] = i }\n @rank = Array.new(n, 0)\n @set_size = Array.new(n, 1)\n end\n\n def find(x)\n if @par[x] == x\n x\n else\n @par[x] = find(@par[x])\n end\n end\n\n def unite(x,y)\n x = find(x)\n y = find(y)\n return if x == y\n\n if @rank[x] < @rank[y]\n @par[x] = y\n @set_size[y] += @set_size[x]\n else\n @par[y] = x\n @set_size[x] += @set_size[y]\n @rank[x] += 1 if @rank[x] == @rank[y]\n end\n end\n\n def same?(x, y)\n find(x) == find(y)\n end\nend\n\n\nN, M, Q = gets.split.map(&:to_i)\nuf = UnionFind.new(N+M)\nQ.times do\n r, c = gets.split.map(&:to_i)\n uf.unite(r-1, c-1+N)\nend\nans = -1\n(N+M).times do |i|\n ans += 1 if uf.find(i) == i\nend\nputs ans"}], "negative_code": [], "src_uid": "acd48e32c96a10cc0d4161225407bf67"} {"nl": {"description": "Little Artem likes electronics. He can spend lots of time making different schemas and looking for novelties in the nearest electronics store. The new control element was delivered to the store recently and Artem immediately bought it.That element can store information about the matrix of integers size n × m. There are n + m inputs in that element, i.e. each row and each column can get the signal. When signal comes to the input corresponding to some row, this row cyclically shifts to the left, that is the first element of the row becomes last element, second element becomes first and so on. When signal comes to the input corresponding to some column, that column shifts cyclically to the top, that is first element of the column becomes last element, second element becomes first and so on. Rows are numbered with integers from 1 to n from top to bottom, while columns are numbered with integers from 1 to m from left to right.Artem wants to carefully study this element before using it. For that purpose he is going to set up an experiment consisting of q turns. On each turn he either sends the signal to some input or checks what number is stored at some position of the matrix.Artem has completed his experiment and has written down the results, but he has lost the chip! Help Artem find any initial matrix that will match the experiment results. It is guaranteed that experiment data is consistent, which means at least one valid matrix exists.", "input_spec": "The first line of the input contains three integers n, m and q (1 ≤ n, m ≤ 100, 1 ≤ q ≤ 10 000) — dimensions of the matrix and the number of turns in the experiment, respectively. Next q lines contain turns descriptions, one per line. Each description starts with an integer ti (1 ≤ ti ≤ 3) that defines the type of the operation. For the operation of first and second type integer ri (1 ≤ ri ≤ n) or ci (1 ≤ ci ≤ m) follows, while for the operations of the third type three integers ri, ci and xi (1 ≤ ri ≤ n, 1 ≤ ci ≤ m,  - 109 ≤ xi ≤ 109) are given. Operation of the first type (ti = 1) means that signal comes to the input corresponding to row ri, that is it will shift cyclically. Operation of the second type (ti = 2) means that column ci will shift cyclically. Finally, operation of the third type means that at this moment of time cell located in the row ri and column ci stores value xi.", "output_spec": "Print the description of any valid initial matrix as n lines containing m integers each. All output integers should not exceed 109 by their absolute value. If there are multiple valid solutions, output any of them.", "sample_inputs": ["2 2 6\n2 1\n2 2\n3 1 1 1\n3 2 2 2\n3 1 2 8\n3 2 1 8", "3 3 2\n1 2\n3 2 2 5"], "sample_outputs": ["8 2 \n1 8", "0 0 0 \n0 0 5 \n0 0 0"], "notes": null}, "positive_code": [{"source_code": "class Mat\n\tattr_reader :rows, :columns\n\n\tdef initialize(rows, columns)\n\t\t@data = []\n\t\t@rows = rows\n\t\t@columns = columns\n\t\tfor i in 1..rows\n\t\t\trow = []\n\t\t\tfor j in 1..columns\n\t\t\t\trow << nil\n\t\t\tend\n\t\t\t@data << row\n\t\tend\n\tend\n\t\n\tdef get(r, c)\n\t\t@data[r][c]\n\tend\n\tdef set(r, c, value)\n\t\t@data[r][c] = value\t\n\tend\n\t\n\tdef rotate_row(row, amt)\n\t\tamt += columns if amt < 0\n\t\trev_horiz(row, 0, amt - 1)\n\t\trev_horiz(row, amt, columns - 1)\n\t\trev_horiz(row, 0, columns - 1)\n\tend\n\t\n\tdef rotate_column(column, amt)\n\t\tamt += rows if amt < 0\n\t\trev_vert(column, 0, amt - 1)\n\t\trev_vert(column, amt, rows - 1)\n\t\trev_vert(column, 0, rows - 1)\n\tend\n\t\n\tdef print\n\t\t@data.each do |row|\n\t\t\tputs row.join(' ')\t\n\t\tend\n\tend\n\t\n\tprivate\n\t\n\tdef rev_horiz(row, start, finish)\n\t\twhile (start < finish)\n\t\t\ttmp = @data[row][start]\n\t\t\t@data[row][start] = @data[row][finish]\n\t\t\t@data[row][finish] = tmp\n\t\t\tstart += 1\n\t\t\tfinish -= 1\n\t\tend\n\tend\n\t\n\tdef rev_vert(col, start, finish)\n\t\twhile (start < finish)\n\t\t\ttmp = @data[start][col]\n\t\t\t@data[start][col] = @data[finish][col]\n\t\t\t@data[finish][col] = tmp\n\t\t\tstart += 1\n\t\t\tfinish -= 1\n\t\tend\n\tend\nend\n\n\nline = gets.chomp.split(' ')\nn = line[0].to_i\nm = line[1].to_i\nq = line[2].to_i\n\nRowOp = Struct.new(:row)\nColOp = Struct.new(:column)\nSetOp = Struct.new(:row, :column, :value)\n\nops = []\n\nfor i in 1..q\n\tline = gets.chomp.split(' ')\n\ttype = line[0].to_i\n\tcase type\n\t\twhen 1\n\t\t\tops << RowOp.new(line[1].to_i - 1)\n\t\twhen 2\n\t\t\tops << ColOp.new(line[1].to_i - 1)\n\t\telse\n\t\t\tops << SetOp.new(line[1].to_i - 1, line[2].to_i - 1, line[3].to_i)\n\tend\nend\n\nmatrix = Mat.new(n, m)\n\nops.reverse!\n\nfor i in 0..ops.length-1\n\tcase ops[i]\n\t\twhen SetOp\n\t\t\tmatrix.set(ops[i].row, ops[i].column, ops[i].value)\n\t\twhen RowOp\n\t\t\tmatrix.rotate_row(ops[i].row, -1)\n\t\twhen ColOp\n\t\t\tmatrix.rotate_column(ops[i].column, -1)\n\tend\nend\n\n\nfor i in 0..matrix.rows-1\n\tfor j in 0..matrix.columns-1\n\t\tmatrix.set(i, j, 0) if matrix.get(i, j).nil?\n\tend\nend\n\nmatrix.print"}, {"source_code": "#! ruby\n# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n############################################################\ndef gs() gets.chomp end\ndef gi() gets.to_i end\ndef gss() gs.split end\ndef gis() gss.map(&:to_i) end\ndef nmapf(n,f) n.times.map{ __send__ f } end\ndef ngs(n) nmapf n,:gs end\ndef ngi(n) nmapf n,:gi end\ndef ngss(n) nmapf n,:gss end\ndef ngis(n) nmapf n,:gis end\ndef arr2d(h,w,v=0) h.times.map{[v] * w} end\ndef for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end\ndef nsum(n) n * (n + 1) / 2 end\ndef vcount(d,r=Hash.new(0)) d.inject(r){|r,e| r[e]+=1;r} end\n############################################################\n\nn, m, q = gis\n\na = arr2d n, m\n\nv = []\nn.times do |i|\n tv = []\n m.times do |j|\n tv << [i, j]\n end\n v << tv\nend\n\nq.times do\n t, *rcx = gis\n case t\n when 1\n r, * = rcx\n v[r - 1].rotate!\n when 2\n c, * = rcx\n ta = v[0][c - 1]\n (n - 1).times do |i|\n v[i][c - 1] = v[i + 1][c - 1]\n end\n v[n - 1][c - 1] = ta\n when 3\n r, c, x = rcx\n i, j = v[r - 1][c - 1]\n a[i][j] = x\n end\nend\n\na.map{|z| puts z.map(&:to_s) * \" \" }\n"}, {"source_code": "n, m, q = gets.chomp.split(\" \").map(&:to_i)\na = Array.new(n).map{Array.new(m, 0)}\nqs = []\nwhile line = gets\n qs << line.split(\" \").map(&:to_i)\nend\nqs.reverse.each do |ope|\n t = ope.shift\n if t == 1 then\n r = ope.shift - 1\n cyc = a[r].dup\n temp = cyc.pop\n cyc.unshift(temp)\n a[r] = cyc\n elsif t == 2 then\n c = ope.shift - 1\n cyc = a.map{|b| b[c]}\n temp = cyc.pop\n cyc.unshift(temp)\n n.times do |i|\n a[i][c] = cyc[i]\n end\n else\n r, c, x = ope\n r -= 1\n c -= 1\n a[r][c] = x\n end\nend\na.each do |r|\n puts r.join(\" \")\nend\n"}, {"source_code": "class Numeric\n def sign\n self<=>0\n end\nend\n\nclass Array\n def loop_slide!(x=1)\n return false if self.length <= 1\n x = (x.abs % self.length) * (-x.sign)\n x += self.length if x < 0\n new_a = self.drop(x)+self.take(x)\n self.length.times{|i| self[i] = new_a[i]}\n end\n\n def loop_slide_simple!(x=1) # x == 1 always\n new_a = self.drop(1)+self.take(1)\n self.length.times{|i| self[i] = new_a[i]}\n end\n\n def second\n self[1]\n end\n\n def third\n self[2]\n end\nend\n\nclass XElement\n def initialize(n,m)\n @rows, @cols = n, m\n @mtx = Array.new\n n.times{@mtx.push Array.new(m, 0)}\n end\n\n def print_mtx\n @rows.times { |row|\n @cols.times { |col|\n print @mtx[row][col]\n print ' '\n }\n print \"\\n\"\n }\n #print \"\\n\"\n end\n\n def shift_row!(row_num)\n @mtx[row_num-1].loop_slide!\n end\n\n def shift_col!(col_num)\n column = Array.new\n @rows.times{|i| column.push(@mtx[i][col_num-1])}\n column.loop_slide!(1)\n @rows.times{|i| @mtx[i][col_num-1] = column[i]}\n end\n\n def set_num!(row, col, value)\n row, col = row-1, col-1\n @mtx[row][col] = value\n end\n\n=begin\n def command!(args)\n COMMAND[args.first.to_s].call(args.drop(1))\n end\n=end\nend\n\nCOMMAND = {\n \"1\" => lambda{|x| $elem.shift_row!(x.first)},\n \"2\" => lambda{|x| $elem.shift_col!(x.first)},\n \"3\" => lambda{|x| $elem.set_num!(x.first, x.second, x.third)}\n}\n\nn,m,q, = gets.split.map{|x| x.to_i}\n$elem = XElement.new(n,m)\nstack = Array.new\nq.times {|x| stack.push gets}\n#$elem.print_mtx\nq.times {|_|\n com = stack.pop.split.map{|x| x.to_i}\n COMMAND[com.first.to_s].call(com.drop(1))\n}\n$elem.print_mtx"}], "negative_code": [{"source_code": "class Mat\n\tattr_reader :rows, :columns\n\n\tdef initialize(rows, columns)\n\t\t@data = []\n\t\t@rows = rows\n\t\t@columns = columns\n\t\tfor i in 1..rows\n\t\t\trow = []\n\t\t\tfor j in 1..columns\n\t\t\t\trow << nil\n\t\t\tend\n\t\t\t@data << row\n\t\tend\n\tend\n\t\n\tdef get(r, c)\n\t\t@data[r][c]\n\tend\n\tdef set(r, c, value)\n\t\t@data[r][c] = value\t\n\tend\n\t\n\tdef rotate_row(row, amt)\n\t\tamt += rows if amt < 0\n\t\trev_horiz(row, 0, amt - 1)\n\t\trev_horiz(row, amt, rows - 1)\n\t\trev_horiz(row, 0, rows - 1)\n\tend\n\t\n\tdef rotate_column(column, amt)\n\t\tamt += columns if amt < 0\n\t\trev_vert(column, 0, amt - 1)\n\t\trev_vert(column, amt, columns - 1)\n\t\trev_vert(column, 0, columns - 1)\n\tend\n\t\n\tdef print\n\t\t@data.each do |row|\n\t\t\tputs row.join(' ')\t\n\t\tend\n\tend\n\t\n\tprivate\n\t\n\tdef rev_horiz(row, start, finish)\n\t\twhile (start < finish)\n\t\t\ttmp = @data[row][start]\n\t\t\t@data[row][start] = @data[row][finish]\n\t\t\t@data[row][finish] = tmp\n\t\t\tstart += 1\n\t\t\tfinish -= 1\n\t\tend\n\tend\n\t\n\tdef rev_vert(col, start, finish)\n\t\twhile (start < finish)\n\t\t\ttmp = @data[start][col]\n\t\t\t@data[start][col] = @data[finish][col]\n\t\t\t@data[finish][col] = tmp\n\t\t\tstart += 1\n\t\t\tfinish -= 1\n\t\tend\n\tend\nend\n\n\nline = gets.chomp.split(' ')\nn = line[0].to_i\nm = line[1].to_i\nq = line[2].to_i\n\nRowOp = Struct.new(:row)\nColOp = Struct.new(:column)\nSetOp = Struct.new(:row, :column, :value)\n\nops = []\n\nfor i in 1..q\n\tline = gets.chomp.split(' ')\n\ttype = line[0].to_i\n\tcase type\n\t\twhen 1\n\t\t\tops << RowOp.new(line[1].to_i - 1)\n\t\twhen 2\n\t\t\tops << ColOp.new(line[1].to_i - 1)\n\t\telse\n\t\t\tops << SetOp.new(line[1].to_i - 1, line[2].to_i - 1, line[3].to_i)\n\tend\nend\n\nmatrix = Mat.new(n, m)\n\n\nfirst_set = nil\nfor i in 0..ops.length-1\n\tnext if !first_set && !ops[i].is_a?(SetOp)\n\tfirst_set ||= i\n\t\n\tcase ops[i]\n\t\twhen SetOp\n\t\t\tmatrix.set(ops[i].row, ops[i].column, ops[i].value)\n\t\twhen RowOp\n\t\t\tmatrix.rotate_row(ops[i].row, 1)\n\t\twhen ColOp\n\t\t\tmatrix.rotate_column(ops[i].column, 1)\n\tend\nend\n\nrev_ops = ops[0..first_set-1].reverse\nrev_ops.each do |op|\n\tcase op\n\t\twhen RowOp\n\t\t\tmatrix.rotate_row(ops[i].row, -1)\n\t\twhen ColOp\n\t\t\tmatrix.rotate_column(ops[i].column, -1)\n\tend\nend\n\nfor i in 0..matrix.rows-1\n\tfor j in 0..matrix.columns-1\n\t\tmatrix.set(i, j, 0) if matrix.get(i, j).nil?\n\tend\nend\n\nmatrix.print"}, {"source_code": "class Numeric\n def sign\n self<=>0\n end\nend\n\nclass Array\n def loop_slide!(x=1)\n x = (x.abs % self.length) * (-x.sign)\n x += self.length if x < 0\n new_a = self.drop(x)+self.take(x)\n self.length.times{|i| self[i] = new_a[i]}\n end\n\n def second\n self[1]\n end\n\n def third\n self[2]\n end\nend\n\nclass XElement\n def initialize(n,m)\n @rows, @cols = n, m\n @mtx = Array.new\n n.times{@mtx.push Array.new(m, 0)}\n end\n\n def print_mtx\n @rows.times { |row|\n @cols.times { |col|\n print @mtx[row][col]\n print ' '\n }\n print \"\\n\"\n }\n #print \"\\n\"\n end\n\n def shift_row!(row_num)\n @mtx[row_num-1].loop_slide!\n end\n\n def shift_col!(col_num)\n column = Array.new\n @cols.times{|i| column.push(@mtx[i][col_num-1])}\n column.loop_slide!(-1)\n @cols.times{|i| @mtx[i][col_num-1] = column[i]}\n end\n\n def set_num!(row, col, value)\n row, col = row-1, col-1\n @mtx[row][col] = value\n end\n\n=begin\n def command!(args)\n COMMAND[args.first.to_s].call(args.drop(1))\n end\n=end\nend\n\nCOMMAND = {\n \"1\" => lambda{|x| $elem.shift_row!(x.first)},\n \"2\" => lambda{|x| $elem.shift_col!(x.first)},\n \"3\" => lambda{|x| $elem.set_num!(x.first, x.second, x.third)}\n}\n\nn,m,q, = gets.split.map{|x| x.to_i}\n$elem = XElement.new(n,m)\nstack = Array.new\nq.times {|x| stack.push gets}\n#$elem.print_mtx\nq.times {|_|\n com = stack.pop.split.map{|x| x.to_i}\n COMMAND[com.first.to_s].call(com.drop(1))\n\n}\n$elem.print_mtx"}], "src_uid": "f710958b96d788a19a1dda436728b9eb"} {"nl": {"description": "Wilbur is playing with a set of n points on the coordinate plane. All points have non-negative integer coordinates. Moreover, if some point (x, y) belongs to the set, then all points (x', y'), such that 0 ≤ x' ≤ x and 0 ≤ y' ≤ y also belong to this set.Now Wilbur wants to number the points in the set he has, that is assign them distinct integer numbers from 1 to n. In order to make the numbering aesthetically pleasing, Wilbur imposes the condition that if some point (x, y) gets number i, then all (x',y') from the set, such that x' ≥ x and y' ≥ y must be assigned a number not less than i. For example, for a set of four points (0, 0), (0, 1), (1, 0) and (1, 1), there are two aesthetically pleasing numberings. One is 1, 2, 3, 4 and another one is 1, 3, 2, 4.Wilbur's friend comes along and challenges Wilbur. For any point he defines it's special value as s(x, y) = y - x. Now he gives Wilbur some w1, w2,..., wn, and asks him to find an aesthetically pleasing numbering of the points in the set, such that the point that gets number i has it's special value equal to wi, that is s(xi, yi) = yi - xi = wi.Now Wilbur asks you to help him with this challenge.", "input_spec": "The first line of the input consists of a single integer n (1 ≤ n ≤ 100 000) — the number of points in the set Wilbur is playing with. Next follow n lines with points descriptions. Each line contains two integers x and y (0 ≤ x, y ≤ 100 000), that give one point in Wilbur's set. It's guaranteed that all points are distinct. Also, it is guaranteed that if some point (x, y) is present in the input, then all points (x', y'), such that 0 ≤ x' ≤ x and 0 ≤ y' ≤ y, are also present in the input. The last line of the input contains n integers. The i-th of them is wi ( - 100 000 ≤ wi ≤ 100 000) — the required special value of the point that gets number i in any aesthetically pleasing numbering.", "output_spec": "If there exists an aesthetically pleasant numbering of points in the set, such that s(xi, yi) = yi - xi = wi, then print \"YES\" on the first line of the output. Otherwise, print \"NO\". If a solution exists, proceed output with n lines. On the i-th of these lines print the point of the set that gets number i. If there are multiple solutions, print any of them.", "sample_inputs": ["5\n2 0\n0 0\n1 0\n1 1\n0 1\n0 -1 -2 1 0", "3\n1 0\n0 0\n2 0\n0 1 2"], "sample_outputs": ["YES\n0 0\n1 0\n2 0\n0 1\n1 1", "NO"], "notes": "NoteIn the first sample, point (2, 0) gets number 3, point (0, 0) gets number one, point (1, 0) gets number 2, point (1, 1) gets number 5 and point (0, 1) gets number 4. One can easily check that this numbering is aesthetically pleasing and yi - xi = wi.In the second sample, the special values of the points in the set are 0,  - 1, and  - 2 while the sequence that the friend gives to Wilbur is 0, 1, 2. Therefore, the answer does not exist."}, "positive_code": [{"source_code": "n = gets.chomp.to_i\n\ndiff = []\nmap_x = {}\nmap_y = {}\n\n\nn.times do\n x,y = gets.chomp.split.map(&:to_i)\n d = y - x\n \n diff << d\n \n if(!map_x[d])\n \n map_x[d] = [] \n map_y[d] = []\n end\n \n map_x[d] << x\n map_y[d] << y\n \nend\n\nws = gets.chomp.split.map(&:to_i)\n\npx = []\npy = []\n\nmap_x.keys.each do |k|\n map_x[k].sort!.reverse!\nend\n\nmap_y.keys.each do |k|\n map_y[k].sort!.reverse!\nend\n\nenable = true\n\nif(diff.sort != ws.sort)\n \n enable = false\nelse\n ws.each do |d| \n if(!map_x[d] || map_x[d].empty?)\n enable = false;\n break;\n end\n px << map_x[d].pop\n py << map_y[d].pop\n \n end \nend\n\nif enable\n x = -1\n y = -1\n \n 0.upto(px.size - 1) do |index|\n if(px[index] <= x && py[index] <= y)\n enable = false\n break;\n end\n x = px[index]\n y = py[index]\n end\nend\n\nif(enable)\n puts \"YES\"\n 0.upto(px.size - 1) do |index|\n printf(\"%d %d\\n\", px[index], py[index]); \n end\n \nelse \n puts \"NO\"\nend\n\n\n\n\n"}, {"source_code": "require 'set'\n\ndef solve(a, s)\n front = Hash.new do |h, diff|\n h[diff] = diff.abs - 2\n end\n a = a.to_set\n\n pts = s.map do |diff|\n existing = front[diff]\n\n left = front[diff + 1]\n return -1 if left < existing\n right = front[diff - 1]\n return -1 if right < existing\n\n nw = existing + 2\n # y + x = nw\n # y - x = diff\n y = (nw + diff) / 2\n x = (nw - diff) / 2\n pt = [x,y]\n return -1 unless a.include?(pt)\n\n front[diff] = nw\n pt\n end\nend\n\nn = gets.to_i\na = Array.new(n) { gets.split.map(&:to_i) }\ndiffs = gets.split.map(&:to_i)\n\nres = solve(a, diffs)\nif res == -1\n puts 'NO'\nelse\n puts 'YES'\n puts res.map{|cs| cs.join(' ')}\nend\n"}, {"source_code": "points = Array.new { Array.new(3) }\na = Array.new { Array.new(2) }\nn = STDIN.gets.to_i\n\ndef check_values(a, points, n)\n points.sort! do |x, y|\n if x[2] != y[2]\n x[2] <=> y[2]\n else\n x[0] <=> y[0]\n end\n end\n a.sort! do |x, y| \n if x[0] != y[0]\n x[0] <=> y[0]\n else\n x[1] <=> y[1]\n end\n end\n \n a.each_with_index do |val, i|\n if (val[0] != points[i][2])\n return false\n end\n end\n\n return true\nend\n\ndef print_res(a, points, n)\n if !check_values(a, points, n)\n puts \"NO\"\n return\n else\n res = Array.new\n points.each_with_index do |point, i|\n res[a[i][1]] = point\n end\n\n #min_x = 0\n #min_y = 0\n #max_x = 0\n #max_y = 0\n #print \"#{res} \\n\"\n max_l = 0\n max_r = 0\n #res.each_with_index do |r, i|\n #if (r[0] < min_x && r[1] < max_y) || (r[0] < max_x && r[1] < min_y)\n # print \"#{r[0]} #{r[1]} #{min_x} #{max_x} #{max_y} \"\n # puts \"NO\"\n # return\n #else\n # if r[0] > max_x\n # max_x = r[0]\n # min_y = r[1]\n # end\n # if r[1] > max_y\n # max_y = r[1]\n # min_x = r[0]\n # end\n # print \"#{max_y} #{r[1]} \"\n # for j in 0..i-1 do\n # if r[0] < res[j][0] && r[1] < res[j][1]\n # puts \"NO\"\n # return\n # end\n # end\n # cur_l = r[1] - r[0]\n # cur_r = r[0] - r[1]\n # if (max_l > cur_l && max_r > cur_r)\n # puts \"NO\"\n # return\n # end\n # max_l = cur_l if max_l > \n \n #end\n for i in 1..n - 1 do\n if res[i][0] <= res[i-1][0] && res[i][1] <= res[i-1][1]\n puts \"NO\"\n return\n end\n end\n \n puts \"YES\"\n res.each do |r|\n puts \"#{r[0]} #{r[1]}\"\n end\n end\nend\n\nfor i in 0..n-1 do\n points.push(STDIN.gets.split(\" \").to_a.map{|q| q.to_i})\n points[i][2] = points[i][1] - points[i][0]\nend\n\nSTDIN.gets.split(\" \").to_a.each_with_index do |num, i|\n a.push((num + \" #{i}\").split(\" \").to_a.map{|q| q.to_i})\nend\n\nprint_res(a, points, n)\n"}], "negative_code": [{"source_code": "n = gets.chomp.to_i\n\ndiff = []\nmap_x = {}\nmap_y = {}\n\n\nn.times do\n x,y = gets.chomp.split.map(&:to_i)\n d = y - x\n \n diff << d\n \n if(!map_x[d])\n \n map_x[d] = [] \n map_y[d] = []\n end\n \n map_x[d] << x\n map_y[d] << y\n \nend\n\nws = gets.chomp.split.map(&:to_i)\n\npx = []\npy = []\n\nmap_x.keys.each do |k|\n map_x[k].sort!.reverse!\nend\n\nmap_y.keys.each do |k|\n map_y[k].sort!.reverse!\nend\n\nenable = true\n\nif(diff.sort != ws.sort)\n \n enable = false\nelse\n ws.each do |d| \n if(!map_x[d] || map_x[d].empty?)\n enable = false;\n break;\n end\n px << map_x[d].pop\n py << map_y[d].pop\n \n end \nend\n\nif enable\n x = -1\n y = -1\n \n 0.upto(px.size - 1) do |index|\n if(px[index] < x && py[index] < y)\n puts \"h3e\";\n enable = false\n break;\n end\n x = px[index]\n y = py[index]\n end\nend\n\nif(enable)\n puts \"YES\"\n 0.upto(px.size - 1) do |index|\n printf(\"%d %d\\n\", px[index], py[index]); \n end\n \nelse \n puts \"NO\"\nend\n\n\n\n\n"}, {"source_code": "n = gets.chomp.to_i\n\ndiff = []\nmap_x = {}\nmap_y = {}\n\n\nn.times do\n x,y = gets.chomp.split.map(&:to_i)\n d = y - x\n \n diff << d\n \n if(!map_x[d])\n \n map_x[d] = [] \n map_y[d] = []\n end\n \n map_x[d] << x\n map_y[d] << y\n \nend\n\nws = gets.chomp.split.map(&:to_i)\n\npx = []\npy = []\n\nmap_x.keys.each do |k|\n map_x[k].sort!.reverse!\nend\n\nmap_y.keys.each do |k|\n map_y[k].sort!.reverse!\nend\n\nenable = true\n\nif(diff.sort != ws.sort)\n \n enable = false\nelse\n ws.each do |d| \n if(!map_x[d] || map_x[d].empty?)\n enable = false;\n break;\n end\n px << map_x[d].pop\n py << map_y[d].pop\n \n end \nend\n\nif enable\n x = -1\n y = -1\n \n 0.upto(px.size - 1) do |index|\n if(px[index] < x && py[index] < y)\n enable = false\n break;\n end\n x = px[index]\n y = py[index]\n end\nend\n\nif(enable)\n puts \"YES\"\n 0.upto(px.size - 1) do |index|\n printf(\"%d %d\\n\", px[index], py[index]); \n end\n \nelse \n puts \"NO\"\nend\n\n\n\n\n"}, {"source_code": "points = Array.new { Array.new(3) }\na = Array.new { Array.new(2) }\nn = STDIN.gets.to_i\n\ndef check_values(a, points, n)\n points.sort! do |x, y|\n if x[2] != y[2]\n x[2] <=> y[2]\n else\n x[1] <=> y[1]\n end\n end\n a.sort! { |x, y| x[0] <=> y[0] }\n \n a.each_with_index do |val, i|\n if (val[0] != points[i][2])\n return false\n end\n end\n\n return true\nend\n\ndef print_res(a, points, n)\n if !check_values(a, points, n)\n puts \"NO\"\n return\n else\n a.sort! { |x, y| x[0] <=> y[0] }\n res = Array.new\n points.each_with_index do |point, i|\n res[a[i][1]] = point\n end\n \n max_x = 0\n max_y = 0\n res.each do |r|\n if r[0] < max_x && r[1] < max_y\n puts \"NO\"\n return\n else\n max_x = r[0] if r[0] > max_x\n max_y = r[1] if r[1] > max_y\n end\n end\n \n puts \"YES\"\n res.each do |r|\n puts \"#{r[0]} #{r[1]}\"\n end\n end\nend\n\nfor i in 0..n-1 do\n points.push(STDIN.gets.split(\" \").to_a.map{|q| q.to_i})\n points[i][2] = points[i][1] - points[i][0]\nend\n\nSTDIN.gets.split(\" \").to_a.each_with_index do |num, i|\n a.push((num + \" #{i}\").split(\" \").to_a.map{|q| q.to_i})\nend\n\nprint_res(a, points, n)\n"}, {"source_code": "points = Array.new { Array.new(3) }\na = Array.new { Array.new(2) }\nn = STDIN.gets.to_i\n\ndef check_values(a, points, n)\n points.sort! do |x, y|\n if x[2] != y[2]\n x[2] <=> y[2]\n else\n x[1] <=> y[1]\n end\n end\n a.sort! { |x, y| x[0] <=> y[0] }\n \n a.each_with_index do |val, i|\n if (val[0] != points[i][2])\n return false\n end\n end\n\n return true\nend\n\ndef print_res(a, points, n)\n if !check_values(a, points, n)\n puts \"NO\"\n return\n else\n a.sort! { |x, y| x[0] <=> y[0] }\n res = Array.new\n points.each_with_index do |point, i|\n res[a[i][1]] = point\n end\n \n max_x = 0\n max_y = 0\n res.each do |r|\n if r[0] < max_x && r[1] < max_y\n puts \"NO\"\n return\n else\n max_x = r[0] if r[0] > max_x\n max_y = r[1] if r[1] > max_y\n end\n end\n \n puts \"Yes\"\n res.each do |r|\n puts \"#{r[0]} #{r[1]}\"\n end\n end\nend\n\nfor i in 0..n-1 do\n points.push(STDIN.gets.split(\" \").to_a.map{|q| q.to_i})\n points[i][2] = points[i][1] - points[i][0]\nend\n\nSTDIN.gets.split(\" \").to_a.each_with_index do |num, i|\n a.push((num + \" #{i}\").split(\" \").to_a.map{|q| q.to_i})\nend\n\nprint_res(a, points, n)\n"}, {"source_code": "points = Array.new { Array.new(3) }\na = Array.new { Array.new(2) }\nn = STDIN.gets.to_i\n\ndef check_values(a, points, n)\n points.sort! do |x, y|\n if x[2] != y[2]\n x[2] <=> y[2]\n else\n x[0] <=> y[0]\n end\n end\n a.sort! do |x, y| \n if x[0] != y[0]\n x[0] <=> y[0]\n else\n x[1] <=> y[1]\n end\n end\n \n a.each_with_index do |val, i|\n if (val[0] != points[i][2])\n return false\n end\n end\n\n return true\nend\n\ndef print_res(a, points, n)\n if !check_values(a, points, n)\n puts \"NO\"\n return\n else\n res = Array.new\n points.each_with_index do |point, i|\n res[a[i][1]] = point\n end\n \n max_x = 0\n max_y = 0\n # print \"#{res} \\n\"\n # res.each do |r|\n # if r[0] < max_x && r[1] < max_y\n # print \"#{r[0]} #{r[1]} #{max_x} #{max_y} \"\n # puts \"NO\"\n # return\n # else\n # max_x = r[0] if r[0] > max_x\n # max_y = r[1] if r[1] > max_y\n # print \"#{max_y} #{r[1]} \"\n # end\n #end\n \n puts \"YES\"\n res.each do |r|\n puts \"#{r[0]} #{r[1]}\"\n end\n end\nend\n\nfor i in 0..n-1 do\n points.push(STDIN.gets.split(\" \").to_a.map{|q| q.to_i})\n points[i][2] = points[i][1] - points[i][0]\nend\n\nSTDIN.gets.split(\" \").to_a.each_with_index do |num, i|\n a.push((num + \" #{i}\").split(\" \").to_a.map{|q| q.to_i})\nend\n\nprint_res(a, points, n)\n"}, {"source_code": "points = Array.new { Array.new(3) }\na = Array.new { Array.new(2) }\nn = STDIN.gets.to_i\n\ndef check_values(a, points, n)\n points.sort! do |x, y|\n if x[2] != y[2]\n x[2] <=> y[2]\n else\n x[0] <=> y[0]\n end\n end\n a.sort! do |x, y| \n if x[0] != y[0]\n x[0] <=> y[0]\n else\n x[1] <=> y[1]\n end\n end\n \n a.each_with_index do |val, i|\n if (val[0] != points[i][2])\n return false\n end\n end\n\n return true\nend\n\ndef print_res(a, points, n)\n if !check_values(a, points, n)\n puts \"NO\"\n return\n else\n res = Array.new\n points.each_with_index do |point, i|\n res[a[i][1]] = point\n end\n\n min_x = 0\n min_y = 0\n max_x = 0\n max_y = 0\n #print \"#{res} \\n\"\n res.each do |r|\n if (r[0] < min_x && r[1] < max_y) && (r[0] < max_x && r[1] < max_y)\n #print \"#{r[0]} #{r[1]} #{max_x} #{max_y} \"\n puts \"NO\"\n return\n else\n if r[0] > max_x\n max_x = r[0]\n min_y = r[1]\n end\n if r[1] > max_y\n max_y = r[1]\n min_x = r[0]\n end\n # print \"#{max_y} #{r[1]} \"\n end\n end\n \n puts \"YES\"\n res.each do |r|\n puts \"#{r[0]} #{r[1]}\"\n end\n end\nend\n\nfor i in 0..n-1 do\n points.push(STDIN.gets.split(\" \").to_a.map{|q| q.to_i})\n points[i][2] = points[i][1] - points[i][0]\nend\n\nSTDIN.gets.split(\" \").to_a.each_with_index do |num, i|\n a.push((num + \" #{i}\").split(\" \").to_a.map{|q| q.to_i})\nend\n\nprint_res(a, points, n)\n"}, {"source_code": "points = Array.new { Array.new(3) }\na = Array.new { Array.new(2) }\nn = STDIN.gets.to_i\n\ndef check_values(a, points, n)\n points.sort! do |x, y|\n if x[2] != y[2]\n x[2] <=> y[2]\n else\n x[1] <=> y[1]\n end\n end\n #print points\n #puts\n a.sort! { |x, y| x[0] <=> y[0] }\n \n a.each_with_index do |val, i|\n if (val[0] != points[i][2])\n return false\n end\n end\n\n return true\nend\n\ndef print_res(a, points, n)\n if !check_values(a, points, n)\n puts \"NO\"\n return\n else\n res = Array.new\n points.each_with_index do |point, i|\n res[a[i][1]] = point\n end\n \n max_x = 0\n max_y = 0\n res.each do |r|\n if r[0] < max_x && r[1] < max_y\n puts \"NO\"\n return\n else\n max_x = r[0] if r[0] > max_x\n max_y = r[1] if r[1] > max_y\n end\n end\n \n puts \"YES\"\n res.each do |r|\n puts \"#{r[0]} #{r[1]}\"\n end\n end\nend\n\nfor i in 0..n-1 do\n points.push(STDIN.gets.split(\" \").to_a.map{|q| q.to_i})\n points[i][2] = points[i][1] - points[i][0]\nend\n\nSTDIN.gets.split(\" \").to_a.each_with_index do |num, i|\n a.push((num + \" #{i}\").split(\" \").to_a.map{|q| q.to_i})\nend\n\nprint_res(a, points, n)\n"}], "src_uid": "ad186f6df24d31243cde541b7fc69a8c"} {"nl": {"description": "Highway 201 is the most busy street in Rockport. Traffic cars cause a lot of hindrances to races, especially when there are a lot of them. The track which passes through this highway can be divided into $$$n$$$ sub-tracks. You are given an array $$$a$$$ where $$$a_i$$$ represents the number of traffic cars in the $$$i$$$-th sub-track. You define the inconvenience of the track as $$$\\sum\\limits_{i=1}^{n} \\sum\\limits_{j=i+1}^{n} \\lvert a_i-a_j\\rvert$$$, where $$$|x|$$$ is the absolute value of $$$x$$$. You can perform the following operation any (possibly zero) number of times: choose a traffic car and move it from its current sub-track to any other sub-track.Find the minimum inconvenience you can achieve.", "input_spec": "The first line of input contains a single integer $$$t$$$ ($$$1\\leq t\\leq 10\\,000$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1\\leq n\\leq 2\\cdot 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$0\\leq a_i\\leq 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\\cdot 10^5$$$.", "output_spec": "For each test case, print a single line containing a single integer: the minimum inconvenience you can achieve by applying the given operation any (possibly zero) number of times.", "sample_inputs": ["3\n3\n1 2 3\n4\n0 1 1 0\n10\n8 3 6 11 5 2 1 7 10 4"], "sample_outputs": ["0\n4\n21"], "notes": "NoteFor the first test case, you can move a car from the $$$3$$$-rd sub-track to the $$$1$$$-st sub-track to obtain $$$0$$$ inconvenience.For the second test case, moving any car won't decrease the inconvenience of the track."}, "positive_code": [{"source_code": "\nt = gets.chomp.to_i\n\nt.times do \n n = gets.chomp.to_i\n arr = gets.chomp.split(' ').map(&:to_i)\n sum = arr.reduce(&:+)\n rem = sum % arr.size\n print \"#{rem * (arr.size - rem)}\\n\"\nend\n"}], "negative_code": [], "src_uid": "935bceb69117d06eb75121c805bff69c"} {"nl": {"description": "The Little Girl loves problems on games very much. Here's one of them.Two players have got a string s, consisting of lowercase English letters. They play a game that is described by the following rules: The players move in turns; In one move the player can remove an arbitrary letter from string s. If the player before his turn can reorder the letters in string s so as to get a palindrome, this player wins. A palindrome is a string that reads the same both ways (from left to right, and vice versa). For example, string \"abba\" is a palindrome and string \"abc\" isn't. Determine which player will win, provided that both sides play optimally well — the one who moves first or the one who moves second.", "input_spec": "The input contains a single line, containing string s (1 ≤ |s|  ≤  103). String s consists of lowercase English letters.", "output_spec": "In a single line print word \"First\" if the first player wins (provided that both players play optimally well). Otherwise, print word \"Second\". Print the words without the quotes.", "sample_inputs": ["aba", "abca"], "sample_outputs": ["First", "Second"], "notes": null}, "positive_code": [{"source_code": "s = gets.to_s.chomp\ns_ = s.split(//).uniq\nans = 0\ns_.each do |chr|\n\t\tans += 1 if s.count(chr).odd?\nend\nif ans.odd? or ans.zero? then\n\tputs \"First\"\n\telse\n\t\tputs \"Second\"\nend\n"}, {"source_code": "s, ans = gets.to_s.chomp, 0\ns_ = s.split(//).uniq\ns_.each{|chr| ans += 1 if s.count(chr).odd?}\n(ans.odd? or ans.zero?) ? (puts \"First\") : (puts \"Second\")\n"}, {"source_code": "# -*- encoding: utf-8 -*-\nchars = gets.chomp.split(\"\")\n\nhash = Hash.new 0\nchars.each do |char|\n hash[char] += 1\nend\n\nodds = 0\nhash.each do |key, value|\n odds += 1 if value % 2 == 1\nend\n\nputs (odds == 0 or odds % 2 == 1) ? \"First\" : \"Second\"\n\n\n\n \n\n"}, {"source_code": "hash=Array.new(26,0)\ngets.chomp.split('').each{ |i| hash[i.ord-97]+=1 }\nct=0\nhash.each{ |i| ct+=1 if i&1==1 }\nputs (ct==0 || ct&1!=0) ? \"First\" : \"Second\""}, {"source_code": "def is_palindrome?(word)\n\todd_count = 0\n\tword.uniq.map do |x|\n\t\todd_count = odd_count + 1 if word.count(x).odd?\n\tend\n\tif odd_count > 1\n\t\tfalse\n\telse\n\t\ttrue\n\tend\nend\n\ndef remove_letter(stringArray)\n\tstringArray.uniq.map do |x|\n\t\tif stringArray.count(x) == 1\n\t\t\tstringArray.delete(x)\n\t\t\tputs stringArray\n\t\t\treturn stringArray\n\t\telse\n\t\t\treturn stringArray\n\t\tend\n\tend\nend\n\ndef do_stuff(stringArray)\n\tif is_palindrome(stringArray)\n\t\t\"win\"\n\telse\n\t\tnext_turn\n\tend\nend\n\ndef test(word)\n\todd_count = 0\n\tword.uniq.map do |x|\n\t\todd_count = odd_count + 1 if word.count(x).odd?\n\tend\n\tif odd_count.odd? || odd_count <= 1\n\t\tputs \"First\"\n\telse\n\t\tputs \"Second\"\n\tend\nend\n\nstring = \"aabb\"\nstringArray = gets.chomp.split(//)\n\ntest(stringArray)"}, {"source_code": "# coding: utf-8\n\ns = gets.chomp.split(//)\nco = Array.new(26) { 0 }\nl = s.length\nfor c in s\n co[c.ord - 'a'.ord] += 1\nend\nturns = 0\nodds = co.count { |c| c.odd? }\nif odds > 1\n turns += odds - 1\nend\n\nif turns.even?\n puts \"First\"\nelse\n puts \"Second\"\nend\n\n\n# 각 턴에서 임의의 한 글자를 지울 수 있다\n# 각 턴이 돌아오기 전 플레이어는 글자를 재배치하여\n# 팰린드롬을 구성할 수 있다. 이러면 승리한다.\n# 두 플레이어가 최적의 플레이를 수행할 때 누가 이길것인가?\n\n# 팰린드롬을 구성하기 위해 최소로 지워야하는 문자수 = 턴 수\n# 이 수에 따라 승패가 갈린다.\n# 전체 문자는 reorder 될 수 있으므로 문자의 배열 순서는\n# 중요하지 않다. 회문을 구성할 수 있는지 여부만 중요한듯\n"}, {"source_code": "s = gets.strip\n\nif s == s.reverse\n\tprint :First\nelse\n\tar = s.split('')\n\n\tletters = {}\n\tar.each do |let|\n\t\tif letters[let]\n\t\t\tletters[let] += 1\n\t\telse\n\t\t\tletters[let] = 1\n\t\tend\n\tend\n\n\tsum = 0\n\tletters.each_value do |num|\n\t\tsum += 1 if !num.even?\n\tend\n\n\t# p sum\n\t# p (sum-1).even? \n\n\tif sum == 0 || (sum-1).even? \n\t\tprint :First\n\telse\n\t\tprint :Second\n\tend\nend\n\n\n"}, {"source_code": "s = gets.chomp\nchar_count = Hash.new(0)\ns.each_char do |c|\n\tchar_count[c] += 1\nend\nodd_count = char_count.values.count{|a| a.odd? }\nif odd_count == 1 || odd_count == 0\n\tfirst = true\nelsif s.length % 2 == 0\n\tfirst = false\nelse\n\tfirst = true\nend\nputs first ? \"First\" : \"Second\""}, {"source_code": "s = gets.chomp\nhash = {}\ns.each_char { |ch|\n\tif !hash.has_key? ch\n\t\thash[ch] = 1\n\telse\n\t\thash[ch] +=1\n\tend\n}\n\nn = 0\n\nhash.values.each { |v|\n if v%2==1\n\tn += 1\n end\n}\n\nk = n%2\nif n==0\n k=1\nend\nputs (k==1) ? \"First\" : \"Second\""}, {"source_code": "class String\n def is_possible_palindrome?\n g = self.split('').inject(Hash.new(0)) { |h, e| h[e] += 1; h }\n odd = false\n g.values.each { |v|\n if v.odd?\n if odd\n return false\n else\n odd = true\n end\n end\n }\n true\n end\nend\n\nw = gets.chomp\nputs(w.is_possible_palindrome? || w.size.odd? ? 'First' : 'Second')"}, {"source_code": "# Codeforces Round #169\n# Problem B -- Little Girl and Game\ncount = [0] * 26\ngets.chomp.each_char do |c|\n count[c.ord - 'a'.ord] += 1\nend\nodds = count.count { |x| x % 2 == 1 }\nputs (odds <= 1 or odds % 2 == 1) ? \"First\" : \"Second\"\n"}, {"source_code": "s = gets.strip\t\na = Array.new(30, 0)\ns.size.times do |i| \n\ta[ s[i].ord - 'a'.ord] += 1\nend\nans = 0\n26.times do |i|\n\ta[i] -= (a[i]/2)*2\t\n\tans += a[i]\nend\nputs (ans>0 && ans%2==0) ? \"Second\" : \"First\""}], "negative_code": [{"source_code": "n,k=gets.chomp.split.map(&:to_i)\nm=[]\nn.times do\n\ta,b=gets.chomp.split.map(&:to_i)\n\tif b>k\n\t\tm.push(a-b+k)\n\telse\n\t\tm.push(a)\n\tend\nend\nputs m.max"}, {"source_code": "def is_palindrome?(word)\n\todd_count = 0\n\tword.uniq.map do |x|\n\t\todd_count = odd_count + 1 if word.count(x).odd?\n\tend\n\tif odd_count > 1\n\t\tfalse\n\telse\n\t\ttrue\n\tend\nend\n\ndef remove_letter(stringArray)\n\tstringArray.uniq.map do |x|\n\t\tif stringArray.count(x) == 1\n\t\t\tstringArray.delete(x)\n\t\t\tputs stringArray\n\t\t\treturn stringArray\n\t\telse\n\t\t\treturn stringArray\n\t\tend\n\tend\nend\n\ndef do_stuff(stringArray)\n\tif is_palindrome(stringArray)\n\t\t\"win\"\n\telse\n\t\tnext_turn\n\tend\nend\n\ndef test(word)\n\todd_count = 0\n\tword.uniq.map do |x|\n\t\todd_count = odd_count + 1 if word.count(x).odd?\n\tend\n\tif odd_count.odd?\n\t\tputs \"First\"\n\telse\n\t\tputs \"Second\"\n\tend\nend\n\nstring = \"abddddbca\"\nstringArray = gets.chomp.split(//)\n\ntest(stringArray)"}, {"source_code": "s = gets.strip\n\nif s == s.reverse\n\tprint :First\nelse\n\tar = s.split('')\n\n\tletters = {}\n\tar.each do |let|\n\t\tif letters[let]\n\t\t\tletters[let] += 1\n\t\telse\n\t\t\tletters[let] = 1\n\t\tend\n\tend\n\n\tsum = 0\n\tletters.each_value do |num|\n\t\tsum += 1 if !num.even?\n\tend\n\n\t# p sum\n\n\tif sum = 0 || !(sum-1).even? \n\t\tprint :First\n\telse\n\t\tprint :Second\n\tend\nend\n\n\n"}, {"source_code": "s = gets.strip\n\nif s == s.reverse\n\tprint :First\nelse\n\tar = s.split('')\n\n\tletters = {}\n\tar.each do |let|\n\t\tif letters[let]\n\t\t\tletters[let] += 1\n\t\telse\n\t\t\tletters[let] = 1\n\t\tend\n\tend\n\n\tsum = 0\n\tletters.each_value do |num|\n\t\tsum += 1 if !num.even?\n\tend\n\n\tif sum = 0 || (sum-1).even? \n\t\tprint :First\n\telse\n\t\tprint :Second\n\tend\nend\n\n\n"}, {"source_code": "s = gets.strip\n\nif s == s.reverse\n\tprint :First\nelse\n\tar = s.split('')\n\n\tletters = {}\n\tar.each do |let|\n\t\tif letters[let]\n\t\t\tletters[let] += 1\n\t\telse\n\t\t\tletters[let] = 1\n\t\tend\n\tend\n\n\tsum = 0\n\tletters.each_value do |num|\n\t\tsum += 1 if !num.even?\n\tend\n\n\tif (sum-1).even?\n\t\tprint :First\n\telse\n\t\tprint :Second\n\tend\nend\n\n\n"}, {"source_code": "s = gets.chomp\nchar_count = Hash.new(0)\ns.each_char do |c|\n\tchar_count[c] += 1\nend\nodd_count = char_count.values.count{|a| a.odd? }\nif odd_count == 1\n\tfirst = true\nelsif s.length % 2 == 0\n\tfirst = false\nelse\n\tfirst = true\nend\nputs first ? \"First\" : \"Second\""}, {"source_code": "class String\n def is_palindrome?\n self[0...size / 2] == self[-size / 2..-1].reverse\n end\nend\n\nw = gets.chomp\nputs(w.is_palindrome? || w.size.odd? ? 'First' : 'Second')"}], "src_uid": "bbf2dbdea6dd3aa45250ab5a86833558"} {"nl": {"description": "During their New Year holidays, Alice and Bob play the following game using an array $$$a$$$ of $$$n$$$ integers: Players take turns, Alice moves first. Each turn a player chooses any element and removes it from the array. If Alice chooses even value, then she adds it to her score. If the chosen value is odd, Alice's score does not change. Similarly, if Bob chooses odd value, then he adds it to his score. If the chosen value is even, then Bob's score does not change. If there are no numbers left in the array, then the game ends. The player with the highest score wins. If the scores of the players are equal, then a draw is declared.For example, if $$$n = 4$$$ and $$$a = [5, 2, 7, 3]$$$, then the game could go as follows (there are other options): On the first move, Alice chooses $$$2$$$ and get two points. Her score is now $$$2$$$. The array $$$a$$$ is now $$$[5, 7, 3]$$$. On the second move, Bob chooses $$$5$$$ and get five points. His score is now $$$5$$$. The array $$$a$$$ is now $$$[7, 3]$$$. On the third move, Alice chooses $$$7$$$ and get no points. Her score is now $$$2$$$. The array $$$a$$$ is now $$$[3]$$$. On the last move, Bob chooses $$$3$$$ and get three points. His score is now $$$8$$$. The array $$$a$$$ is empty now. Since Bob has more points at the end of the game, he is the winner. You want to find out who will win if both players play optimally. Note that there may be duplicate numbers in the array.", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains an integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) — the number of elements in the array $$$a$$$. The next line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$) — the array $$$a$$$ used to play the game. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, output on a separate line: \"Alice\" if Alice wins with the optimal play; \"Bob\" if Bob wins with the optimal play; \"Tie\", if a tie is declared during the optimal play. ", "sample_inputs": ["4\n4\n5 2 7 3\n3\n3 2 1\n4\n2 2 2 2\n2\n7 8"], "sample_outputs": ["Bob\nTie\nAlice\nAlice"], "notes": null}, "positive_code": [{"source_code": "def arline\r\n gets.chomp.split.map(&:to_i)\r\nend\r\n\r\ndef solve\r\n n = gets.chomp.to_i\r\n ar = arline.sort.reverse\r\n odd = ar.select{|x| x%2 == 1}.sum\r\n alice = ar.zip(0..n).select{|x, y| y%2 == 0}.map{|x| x[0]}.sum\r\n\r\n case odd <=> alice\r\n when 0\r\n puts \"Tie\"\r\n when 1\r\n puts \"Bob\"\r\n when -1\r\n puts \"Alice\"\r\n end\r\nend\r\n\r\ntc = gets.chomp.to_i\r\n(1..tc).each do\r\n solve\r\nend\r\n"}, {"source_code": "def solve()\r\n odds, evens = [], []\r\n n = gets.to_i\r\n nums = gets.chomp.split.map!(&:to_i)\r\n nums.each do |i|\r\n i.odd? ? odds.append(i) : evens.append(i)\r\n end\r\n odds.sort!\r\n evens.sort!\r\n alice_point = 0\r\n bob_point = 0\r\n alices_turn = true\r\n until (odds.empty? && evens.empty?) do\r\n # puts \"Size:#{odds.length}, #{evens.length}\"\r\n if alices_turn\r\n if evens.empty? \r\n odds.pop\r\n elsif (not odds.empty?) and odds.last > evens.last\r\n odds.pop\r\n else\r\n alice_point += evens.pop\r\n end\r\n else\r\n if odds.empty? \r\n evens.pop\r\n elsif (not evens.empty?) and evens.last > odds.last\r\n evens.pop\r\n else\r\n bob_point += odds.pop\r\n end\r\n end\r\n alices_turn = !alices_turn\r\n end\r\n res = alice_point <=> bob_point\r\n if res == 1\r\n puts \"Alice\"\r\n elsif res == 0\r\n puts \"Tie\"\r\n else \r\n puts \"Bob\"\r\n end\r\nend\r\n\r\ntcase = gets.to_i\r\ntcase.times do solve() end"}, {"source_code": "gets.to_i.times do\n\tn = gets.to_i\n\ta = gets.split.map(&:to_i).sort.reverse\n\tx, y = 0, 0\n\ta.each_with_index do |val, i|\n\t\tx += val if i.even? && val.even?\n\t\ty += val if i.odd? && val.odd?\n\tend\n\tputs x < y ? 'Bob' : x > y ? 'Alice' : 'Tie'\nend\n"}, {"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n a = gets.split.map &:to_i\r\n a.sort!.reverse!\r\n res = [0, 0]\r\n a.each_with_index do |it, i|\r\n res[1] += it if it % 2 == 1 && i % 2 == 1\r\n res[0] += it if it % 2 == 0 && i % 2 == 0\r\n end\r\n puts res[0] > res[1] ? \"Alice\" : (res[0] < res[1] ? \"Bob\" : \"Tie\")\r\nend"}, {"source_code": "t = gets.chomp.to_i\r\nt.times do\r\n n = gets.chomp.to_i\r\n arr = gets.chomp.split(' ').map(&:to_i)\r\n arr = arr.sort.reverse\r\n ch = 0\r\n a = b = 0\r\n arr.each do |el|\r\n if ch == 0 && el % 2 == 0\r\n a += el\r\n end\r\n if ch == 1 && el % 2 == 1\r\n b += el\r\n end\r\n ch = 1 - ch\r\n end\r\n case\r\n when a > b\r\n puts \"Alice\"\r\n when b > a\r\n puts \"Bob\"\r\n else\r\n puts \"Tie\"\r\n end\r\nend"}, {"source_code": "gets.to_i.times do\r\n n=gets.to_i\r\n a=gets.split.map(&:to_i).sort.reverse\r\n f=0\r\n s=0\r\n n.times do |i|\r\n if i.even?\r\n if a[i].even?\r\n f+=a[i]\r\n end\r\n else\r\n if a[i].odd?\r\n s+=a[i]\r\n end\r\n end\r\n end\r\n if f>s\r\n puts \"Alice\"\r\n elsif f 0 && y > 0\n if x == 0 && y == 0\n puts move\n else\n move += 1\n if x > 0\n x -= 1\n else\n y -= 1\n end\n if x == 0 && y == 0\n puts move\n else\n (\n x -= 2\n move += 4\n ) while x > 1\n (\n y -= 2\n move += 4\n ) while y > 1\n if x == 1 || y == 1\n move += 2 \n end\n puts move\n end\n end\n end\n}"}, {"source_code": "def get_int\n gets.chomp.to_i\nend\n\nget_int.times do |i|\n x, y = gets.split.map(&:to_i)\n puts 2 * [x, y].max - 1 + ((x == y) ? 1 : 0)\nend\n"}, {"source_code": "t = gets.chomp.to_i\nt.times do\n x, y = gets.chomp.split(' ').map(&:to_i).map{|el| el.abs}\n if (x - y).abs < 2\n puts x + y\n else\n x, y = y, x if x < y\n puts x + y + (x - y) - 1\n end\nend\n"}, {"source_code": "t = gets.chomp.to_i\nt.times do\n x, y = gets.chomp.split(\" \").map(&:to_i)\n big = [x,y].max\n small = [x,y].min\n sa = big - small\n if x == 0 && y == 0\n puts \"0\"\n elsif sa <= 1\n puts big + small\n else\n puts small * 2 + (big - small - 1)*2 + 1\n end\nend"}, {"source_code": "n = gets.to_i\nn.times do\n x, y = gets.chomp!.split.map(&:to_i)\n x, y = y, x unless x >= y\n if x > y\n p y * 2 + ((x - y) * 2 - 1)\n else\n p y * 2\n end\nend\n"}], "negative_code": [], "src_uid": "8864c6a04fed970fcbc04e220df9d88d"} {"nl": {"description": "Let's introduce a number system which is based on a roman digits. There are digits I, V, X, L which correspond to the numbers $$$1$$$, $$$5$$$, $$$10$$$ and $$$50$$$ respectively. The use of other roman digits is not allowed.Numbers in this system are written as a sequence of one or more digits. We define the value of the sequence simply as the sum of digits in it.For example, the number XXXV evaluates to $$$35$$$ and the number IXI — to $$$12$$$.Pay attention to the difference to the traditional roman system — in our system any sequence of digits is valid, moreover the order of digits doesn't matter, for example IX means $$$11$$$, not $$$9$$$.One can notice that this system is ambiguous, and some numbers can be written in many different ways. Your goal is to determine how many distinct integers can be represented by exactly $$$n$$$ roman digits I, V, X, L.", "input_spec": "The only line of the input file contains a single integer $$$n$$$ ($$$1 \\le n \\le 10^9$$$) — the number of roman digits to use.", "output_spec": "Output a single integer — the number of distinct integers which can be represented using $$$n$$$ roman digits exactly.", "sample_inputs": ["1", "2", "10"], "sample_outputs": ["4", "10", "244"], "notes": "NoteIn the first sample there are exactly $$$4$$$ integers which can be represented — I, V, X and L.In the second sample it is possible to represent integers $$$2$$$ (II), $$$6$$$ (VI), $$$10$$$ (VV), $$$11$$$ (XI), $$$15$$$ (XV), $$$20$$$ (XX), $$$51$$$ (IL), $$$55$$$ (VL), $$$60$$$ (XL) and $$$100$$$ (LL)."}, "positive_code": [{"source_code": "def comb3(n)\n n < 0 ? 0 : (n+1)*(n+2)*(n+3)/6\nend\nN = gets.to_i\nputs comb3(N) - comb3(N-6) - 2*comb3(N-9) + comb3(N-10) + comb3(N-14)"}, {"source_code": "a = [4, 10, 20, 35, 56, 83, 116, 155, 198, 244, 292]\nn = gets.chomp.to_i\nputs n < 12 ? a[n - 1] : (n - 12) * 49 + 341"}], "negative_code": [], "src_uid": "75ec99318736a8a1b62a8d51efd95355"} {"nl": {"description": "You are given a string s consisting of lowercase Latin letters. Character c is called k-dominant iff each substring of s with length at least k contains this character c.You have to find minimum k such that there exists at least one k-dominant character.", "input_spec": "The first line contains string s consisting of lowercase Latin letters (1 ≤ |s| ≤ 100000).", "output_spec": "Print one number — the minimum value of k such that there exists at least one k-dominant character.", "sample_inputs": ["abacaba", "zzzzz", "abcde"], "sample_outputs": ["2", "1", "3"], "notes": null}, "positive_code": [{"source_code": "module Memoizer\n def memoize(methodname)\n raise ArgumentError, \"statefull method [#{methodname}]should not be memoized\" if methodname.match(/\\!\\z/)\n unbound_method = instance_method(methodname)\n raise ArgumentError, \"method [#{methodname}] with argument cannot be memoized\" if unbound_method.arity > 0\n memoized_object_name = \"@_memoized_#{methodname}\"\n define_method(methodname) do\n if(instance_variable_get(memoized_object_name).nil?)\n instance_variable_set(memoized_object_name, unbound_method.bind(self).call)\n end\n instance_variable_get(memoized_object_name)\n end\n end\nend\n\nclass Array\n def enumerate(&block)\n self.zip(0..length).map(&block)\n end\nend\nclass Problem\n def initialize(xs)\n @xs = xs\n @last_index = Array.new(30){ -1 }\n @longests = Array.new(30){ -1 }\n end\n\n def solve!\n @xs.split(\"\").each.with_index do |x, i|\n p = x.ord - 'a'.ord\n distance = i - @last_index[p]\n @longests[p] = [@longests[p], distance].max\n @last_index[p] = i\n end\n l = @xs.length\n (0..26).each do |x|\n distance = l - @last_index[x]\n @longests[x] = [@longests[x], distance].max\n end\n end\n\n attr_reader(:longests)\n attr_reader(:last_index)\nend\n\nprob = Problem.new(gets.strip)\nprob.solve!\nprint prob.longests.select{|x| x >= 0 }.min\n"}, {"source_code": "def is_posible?(s, k)\n n = s.size\n return true if n == k\n chars = [0]*255\n s[0...k].split(\"\").each{ |c| chars[c.ord] += 1 }\n i = 1\n\n while i+k <= n do\n chars[s[i+k-1].ord] += 1 if chars[s[i+k-1].ord] and chars[s[i+k-1].ord] > 0\n chars[s[i-1].ord] -= 1 if chars[s[i-1].ord]\n i += 1\n end\n\n chars.each do |v|\n return true if v > 0\n end\n false\nend\n\n\ndef solution(s)\n beg = 1\n last = s.size - 1\n min_k = s.size\n\n while (beg <= last) do\n mid = (last + beg)/2\n\n if is_posible?(s, mid)\n min_k = mid\n last = mid - 1\n else\n beg = mid + 1\n end\n end\n min_k\nend\n\nputs solution(gets.strip())\n"}, {"source_code": "def is_posible?(s, k)\n n = s.size\n return true if n == k\n chars = [0]*255\n s[0...k].split(\"\").each{ |c| chars[c.ord] += 1 }\n i = 1\n\n while i+k <= n do\n chars[s[i+k-1].ord] += 1 if chars[s[i+k-1].ord] and chars[s[i+k-1].ord] > 0\n chars[s[i-1].ord] -= 1 if chars[s[i-1].ord]\n i += 1\n end\n\n chars.each do |v|\n return true if v > 0\n end\n false\nend\n\n\ndef solution(s)\n beg = 1\n last = s.size - 1\n min_k = s.size\n\n while (beg <= last) do\n mid = (last + beg)/2\n\n if is_posible?(s, mid)\n min_k = mid\n last = mid - 1\n else\n beg = mid + 1\n end\n end\n min_k\nend\n\nputs solution(gets.strip())"}], "negative_code": [{"source_code": "def is_posible?(s, k)\n n = s.size\n return true if n == k\n chars = {}\n s[0...k].split(\"\").uniq.each{ |c| chars[c] = 1 }\n i = 1\n\n while i+k < n do\n chars[s[i+k-1]] += 1 if chars[s[i+k-1]] and chars[s[i+k-1]] > 0\n chars[s[i-1]] -= 1 if chars[s[i-1]] and chars[s[i-1]] > 0\n i += 1\n end\n\n\n chars.each do |k,v|\n return true if v > 0\n end\n false\nend\n\n\ndef solution(s)\n beg = 1\n last = s.size - 1\n min_k = s.size\n\n while (beg <= last) do\n mid = (last + beg)/2 \n \n if is_posible?(s, mid)\n min_k = mid\n last = mid - 1\n else\n beg = mid + 1\n end\n end\n min_k\nend\n\nputs solution(gets.strip()) "}, {"source_code": "def is_posible?(s, k)\n n = s.size\n return true if n == k\n chars = [0]*255\n s[0...k].split(\"\").each{ |c| chars[c.ord] += 1 }\n i = 1\n\n while i+k <= n do\n puts \"#{chars[97..122]}\"\n chars[s[i+k-1].ord] += 1 if chars[s[i+k-1].ord] and chars[s[i+k-1].ord] > 0\n chars[s[i-1].ord] -= 1 if chars[s[i-1].ord]\n i += 1\n end\n puts \"#{chars[97..122]}\"\n\n chars.each do |v|\n return true if v > 0\n end\n false\nend\n\n\ndef solution(s)\n beg = 1\n last = s.size - 1\n min_k = s.size\n\n while (beg <= last) do\n mid = (last + beg)/2\n\n if is_posible?(s, mid)\n min_k = mid\n last = mid - 1\n else\n beg = mid + 1\n end\n end\n min_k\nend\n\nputs solution(gets.strip())\n"}, {"source_code": "def is_posible?(s, k)\n n = s.size\n return true if n == k\n chars = [0]*255\n s[0...k].split(\"\").each{ |c| chars[c.ord] += 1 }\n i = 1\n \n while i+k < n do\n chars[s[i+k-1].ord] += 1 if chars[s[i+k-1].ord] and chars[s[i+k-1].ord] > 0\n chars[s[i-1].ord] -= 1 if chars[s[i-1].ord]\n i += 1\n end\n\n chars.each do |v|\n return true if v > 0\n end\n false\nend\n\n\ndef solution(s)\n beg = 1\n last = s.size - 1\n min_k = s.size\n\n while (beg <= last) do\n mid = (last + beg)/2 \n \n if is_posible?(s, mid)\n min_k = mid\n last = mid - 1\n else\n beg = mid + 1\n end\n end\n min_k\nend\n\nputs solution(gets.strip()) "}, {"source_code": "def is_posible?(s, k)\n n = s.size\n chars = s[0...k].split(\"\").uniq\n \n i = 0\n while i+k < n do\n chars.each do |c|\n chars.delete(c) if !s[i...(i+k)].include?(c) \n end\n return false if chars.size == 0\n i += 1\n end\n true\nend\n\n\ndef solution(s)\n beg = 1\n last = s.size - 1\n min_k = s.size\n\n while (beg <= last) do\n mid = (last + beg)/2 \n \n if is_posible?(s, mid)\n min_k = mid\n last = mid - 1\n else\n puts mid\n beg = mid + 1\n end\n end\n min_k\nend\n\ns = gets.strip()\n"}], "src_uid": "81a558873e39efca5066ef91a9255f11"} {"nl": {"description": "Lunar New Year is approaching, and Bob is struggling with his homework – a number division problem.There are $$$n$$$ positive integers $$$a_1, a_2, \\ldots, a_n$$$ on Bob's homework paper, where $$$n$$$ is always an even number. Bob is asked to divide those numbers into groups, where each group must contain at least $$$2$$$ numbers. Suppose the numbers are divided into $$$m$$$ groups, and the sum of the numbers in the $$$j$$$-th group is $$$s_j$$$. Bob's aim is to minimize the sum of the square of $$$s_j$$$, that is $$$$$$\\sum_{j = 1}^{m} s_j^2.$$$$$$Bob is puzzled by this hard problem. Could you please help him solve it?", "input_spec": "The first line contains an even integer $$$n$$$ ($$$2 \\leq n \\leq 3 \\cdot 10^5$$$), denoting that there are $$$n$$$ integers on Bob's homework paper. The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\leq a_i \\leq 10^4$$$), describing the numbers you need to deal with.", "output_spec": "A single line containing one integer, denoting the minimum of the sum of the square of $$$s_j$$$, which is $$$$$$\\sum_{i = j}^{m} s_j^2,$$$$$$ where $$$m$$$ is the number of groups.", "sample_inputs": ["4\n8 5 2 3", "6\n1 1 1 2 2 2"], "sample_outputs": ["164", "27"], "notes": "NoteIn the first sample, one of the optimal solutions is to divide those $$$4$$$ numbers into $$$2$$$ groups $$$\\{2, 8\\}, \\{5, 3\\}$$$. Thus the answer is $$$(2 + 8)^2 + (5 + 3)^2 = 164$$$.In the second sample, one of the optimal solutions is to divide those $$$6$$$ numbers into $$$3$$$ groups $$$\\{1, 2\\}, \\{1, 2\\}, \\{1, 2\\}$$$. Thus the answer is $$$(1 + 2)^2 + (1 + 2)^2 + (1 + 2)^2 = 27$$$."}, "positive_code": [{"source_code": "# Input\nn = gets.to_i\na = gets.split.map { |i| i.to_i }\n\n# Most effecient way: splitting into pairs from the middle\n\n# Processing\na.sort!\nsum = 0\n(n / 2).times { |i| sum += (a[i] + a[n - 1 - i])**2 }\n\n# Output\nputs sum\n"}, {"source_code": "DBG = true\nn = gets.to_i\na = gets.split.map{|z| z.to_i}\n #a,b = gets.split\n #a = a.to_i\n #b = b.to_i\n\na.sort!\nsum = 0\nfor i in 0...(n/2)\n sum += (a[i]+a[n-1-i])**2\nend\nputs \"#{sum}\"\n"}, {"source_code": "n = gets.to_i\na = gets.split(\" \").sort_by{|e| e.to_i}\nr = 0\nfor i in 0..n/2-1\n\tr+=(a[i].to_i+a[n-1-i].to_i)**2\nend \t\np r"}, {"source_code": "gets\na=gets.split.map{|e|e.to_i}.sort\nr=0\na.size.times{|i|r+=(a[i]+a[-i-1])**2}\np r/2"}, {"source_code": "gets\na=gets.split.map{|e|e.to_i}.sort\nr=0\n(0..a.size/2-1).each{|i|r+=(a[i]+a[-i-1])**2}\np r"}, {"source_code": "gets\na = gets.split(' ').map{|z| z.to_i}.sort\nr = 0\n(0..a.size-1).each{|i|\tr+=(a[i]+a[-i-1])**2}\np r/2"}, {"source_code": "n = gets.to_i\na = gets.split(\" \").map{|z| z.to_i}.sort\nr = 0\nfor i in 0..n-1\n\tr+=(a[i]+a[n-1-i])**2\nend \t\np r/2"}, {"source_code": "n = gets.to_i\na = gets.split(\" \").map{|z| z.to_i}.sort\nr = 0\n(0..n-1).each{|i|\tr+=(a[i]+a[n-1-i])**2}\np r/2"}, {"source_code": "n = gets.to_i\na = gets.split(\" \")\nfor i in 0..n-1 do\n\ta[i] = a[i].to_i\nend\na = a.sort\nans = 0\nfor i in 0..n/2-1 do\n\tans+=(a[i]+a[n-1-i])*(a[i]+a[n-1-i])\nend \np ans"}, {"source_code": "n = gets.to_i\na = gets.split(' ').map{|z| z.to_i}.sort\nr = 0\n(0..n-1).each{|i|\tr+=(a[i]+a[-i-1])**2}\np r/2"}, {"source_code": "gets\na = gets.split.map{|e| e.to_i}.sort\nr=0\n(0..a.size/2-1).each{|i|\tr+=(a[i]+a[-i-1])**2}\np r"}, {"source_code": "if File.exists?(\"input\")\n $stdin = File.open(\"input\")\n $stdout = File.open(\"output\",\"w\")\nend\n\n\nn = gets.to_i\narr = gets.chomp.split(\" \").map(&:to_i).sort\n\nk = n/2\ns = 0\n\n(0..k-1).each do |i|\n\ts += (arr[i]+arr[n-1-i])**2\nend\n\nputs s"}], "negative_code": [], "src_uid": "28c555fefd5c36697a5082c61d8a82b3"} {"nl": {"description": "It's been almost a week since Polycarp couldn't get rid of insomnia. And as you may already know, one week in Berland lasts k days!When Polycarp went to a doctor with his problem, the doctor asked him about his sleeping schedule (more specifically, the average amount of hours of sleep per week). Luckily, Polycarp kept records of sleep times for the last n days. So now he has a sequence a1, a2, ..., an, where ai is the sleep time on the i-th day.The number of records is so large that Polycarp is unable to calculate the average value by himself. Thus he is asking you to help him with the calculations. To get the average Polycarp is going to consider k consecutive days as a week. So there will be n - k + 1 weeks to take into consideration. For example, if k = 2, n = 3 and a = [3, 4, 7], then the result is .You should write a program which will calculate average sleep times of Polycarp over all weeks.", "input_spec": "The first line contains two integer numbers n and k (1 ≤ k ≤ n ≤ 2·105). The second line contains n integer numbers a1, a2, ..., an (1 ≤ ai ≤ 105).", "output_spec": "Output average sleeping time over all weeks. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 6. In particular, it is enough to output real number with at least 6 digits after the decimal point.", "sample_inputs": ["3 2\n3 4 7", "1 1\n10", "8 2\n1 2 4 100000 123 456 789 1"], "sample_outputs": ["9.0000000000", "10.0000000000", "28964.2857142857"], "notes": "NoteIn the third example there are n - k + 1 = 7 weeks, so the answer is sums of all weeks divided by 7."}, "positive_code": [{"source_code": "n,k,*a=$<.read.split.map(&:to_i)\ns=0.0\n(k-1).times{|i|s+=a[i]}\nr=0\n(k-1...n).each{|i|s+=a[i];r+=s;s-=a[i-k+1]}\np r/(n-k+1)"}, {"source_code": "def scan;gets.split.map(&:to_i);end\n\nn,k = scan\naa = scan\n\nres = 0.0\nl = 0\nsum = 0\naa.each_index{|r|\n if r-l >= k\n res += sum\n sum -= aa[l]\n l+=1\n end\n sum += aa[r]\n}\nres += sum\nres /= n-k+1\n\np res"}], "negative_code": [{"source_code": "n,k,*a=$<.read.split.map(&:to_i)\ns=0.0\n(k-1).times{|i|s+=a[i]}\nr=0\n(k-1...n).each{|i|s+=a[i];r+=s;s-=a[i-k+1]}\np s/(n-k+1)"}], "src_uid": "838e643a978adbeed791a24ac1046ab5"} {"nl": {"description": "The student council is preparing for the relay race at the sports festival.The council consists of $$$n$$$ members. They will run one after the other in the race, the speed of member $$$i$$$ is $$$s_i$$$. The discrepancy $$$d_i$$$ of the $$$i$$$-th stage is the difference between the maximum and the minimum running speed among the first $$$i$$$ members who ran. Formally, if $$$a_i$$$ denotes the speed of the $$$i$$$-th member who participated in the race, then $$$d_i = \\max(a_1, a_2, \\dots, a_i) - \\min(a_1, a_2, \\dots, a_i)$$$.You want to minimize the sum of the discrepancies $$$d_1 + d_2 + \\dots + d_n$$$. To do this, you are allowed to change the order in which the members run. What is the minimum possible sum that can be achieved?", "input_spec": "The first line contains a single integer $$$n$$$ ($$$1 \\le n \\le 2000$$$)  — the number of members of the student council. The second line contains $$$n$$$ integers $$$s_1, s_2, \\dots, s_n$$$ ($$$1 \\le s_i \\le 10^9$$$)  – the running speeds of the members.", "output_spec": "Print a single integer  — the minimum possible value of $$$d_1 + d_2 + \\dots + d_n$$$ after choosing the order of the members.", "sample_inputs": ["3\n3 1 2", "1\n5", "6\n1 6 3 3 6 3", "6\n104 943872923 6589 889921234 1000000000 69"], "sample_outputs": ["3", "0", "11", "2833800505"], "notes": "NoteIn the first test case, we may choose to make the third member run first, followed by the first member, and finally the second. Thus $$$a_1 = 2$$$, $$$a_2 = 3$$$, and $$$a_3 = 1$$$. We have: $$$d_1 = \\max(2) - \\min(2) = 2 - 2 = 0$$$. $$$d_2 = \\max(2, 3) - \\min(2, 3) = 3 - 2 = 1$$$. $$$d_3 = \\max(2, 3, 1) - \\min(2, 3, 1) = 3 - 1 = 2$$$. The resulting sum is $$$d_1 + d_2 + d_3 = 0 + 1 + 2 = 3$$$. It can be shown that it is impossible to achieve a smaller value.In the second test case, the only possible rearrangement gives $$$d_1 = 0$$$, so the minimum possible result is $$$0$$$."}, "positive_code": [{"source_code": "MAXN = 2000\n\ndef calcd(a, i, j, cache)\n return 0 if i == j\n return a[j] - a[i] if i + 1 == j\n\n return cache[i * MAXN + j] if cache[i * MAXN + j]\n\n x = a[j] - a[i]\n x1 = cache[(i + 1) * MAXN + j + 0] || calcd(a, i + 1, j, cache)\n x2 = cache[i * MAXN + j - 1] || calcd(a, i, j - 1, cache)\n x += x1 < x2 ? x1 : x2\n cache[i * MAXN + j] = x\n return x\nend\n\nn = gets.to_s.split.map {|x| x.to_i }\ns = gets.to_s.split.map {|x| x.to_i }.sort\n\nprintf(\"%d\\n\", calcd(s, 0, s.size - 1, [nil] * (MAXN * MAXN)))\n"}], "negative_code": [{"source_code": "def calcd(a)\n return 0 if a.size <= 1\n x = a[-1] - a[0]\n if a[1] - a[0] > a[-1] - a[-2]\n return x + calcd(a[1 .. -1])\n elsif a[1] - a[0] == a[-1] - a[-2]\n x1 = calcd(a[1 .. -1])\n x2 = calcd(a[0 .. -2])\n return x + [x1, x2].min\n else\n return x + calcd(a[0 .. -2])\n end\nend\n\nn = gets.to_s.split.map {|x| x.to_i }\ns = gets.to_s.split.map {|x| x.to_i }.sort\n\np calcd(s)\n"}, {"source_code": "def calcd(a)\n return 0 if a.size <= 1\n if a[1] - a[0] > a[-1] - a[-2]\n return a[-1] - a[0] + calcd(a[1 .. -1])\n else\n return a[-1] - a[0] + calcd(a[0 .. -2])\n end\nend\n\nn = gets.to_s.split.map {|x| x.to_i }\ns = gets.to_s.split.map {|x| x.to_i }.sort\n\np calcd(s)\n"}, {"source_code": "def calcd(a)\n sum = 0\n min = a[0]\n max = a[0]\n a.each do |x|\n min = x if x < min\n max = x if x > max\n sum += max - min\n end\n sum\nend\n\ndef calcdx(s, m)\n calcd(s.sort {|a, b| (a - m).abs <=> (b - m).abs })\nend\n\nn = gets.to_s.split.map {|x| x.to_i }\ns = gets.to_s.split.map {|x| x.to_i }\n\nmin = s.min\nmax = s.max + 1\n\nbest = calcdx(s, min)\n\nwhile max - min > 0\n mid = (min + max) / 2\n tmp = calcdx(s, mid)\n if tmp < best\n best = tmp\n min = mid\n else\n max = mid\n end\nend\n\nprintf(\"%d\\n\", best)\n\n"}], "src_uid": "96ba63c7fea9bd0a5ddd8ffc886da153"} {"nl": {"description": "Captain Flint and his crew keep heading to a savage shore of Byteland for several months already, drinking rum and telling stories. In such moments uncle Bogdan often remembers his nephew Denis. Today, he has told a story about how Denis helped him to come up with an interesting problem and asked the crew to solve it.In the beginning, uncle Bogdan wrote on a board a positive integer $$$x$$$ consisting of $$$n$$$ digits. After that, he wiped out $$$x$$$ and wrote integer $$$k$$$ instead, which was the concatenation of binary representations of digits $$$x$$$ consists of (without leading zeroes). For example, let $$$x = 729$$$, then $$$k = 111101001$$$ (since $$$7 = 111$$$, $$$2 = 10$$$, $$$9 = 1001$$$).After some time, uncle Bogdan understood that he doesn't know what to do with $$$k$$$ and asked Denis to help. Denis decided to wipe last $$$n$$$ digits of $$$k$$$ and named the new number as $$$r$$$.As a result, Denis proposed to find such integer $$$x$$$ of length $$$n$$$ that $$$r$$$ (as number) is maximum possible. If there are multiple valid $$$x$$$ then Denis is interested in the minimum one.All crew members, including captain Flint himself, easily solved the task. All, except cabin boy Kostya, who was too drunk to think straight. But what about you?Note: in this task, we compare integers ($$$x$$$ or $$$k$$$) as numbers (despite what representations they are written in), so $$$729 < 1999$$$ or $$$111 < 1000$$$.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. Next $$$t$$$ lines contain test cases — one per test case. The one and only line of each test case contains the single integer $$$n$$$ ($$$1 \\le n \\le 10^5$$$) — the length of the integer $$$x$$$ you need to find. It's guaranteed that the sum of $$$n$$$ from all test cases doesn't exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, print the minimum integer $$$x$$$ of length $$$n$$$ such that obtained by Denis number $$$r$$$ is maximum possible.", "sample_inputs": ["2\n1\n3"], "sample_outputs": ["8\n998"], "notes": "NoteIn the second test case (with $$$n = 3$$$), if uncle Bogdan had $$$x = 998$$$ then $$$k = 100110011000$$$. Denis (by wiping last $$$n = 3$$$ digits) will obtain $$$r = 100110011$$$.It can be proved that the $$$100110011$$$ is the maximum possible $$$r$$$ Denis can obtain and $$$998$$$ is the minimum $$$x$$$ to obtain it."}, "positive_code": [{"source_code": "t=gets.chomp.to_i\n\nwhile t!=0\nn=gets.chomp.to_i\nk=4.0\n\n \nval=n/k\nval=val.ceil()\n\n\nl=n-val\nl.times {print \"#{9}\"}\nval.times{print \"#{8}\"}\nputs\nt-=1\n\nend"}, {"source_code": "require 'set'; require 'prime'; require 'pp'\nINF=Float::INFINITY; MOD=10**9+7\nt = gets.chomp.to_i\nt.times do\n n = gets.chomp.to_i\n zero = n / 4\n if n % 4 == 0\n puts '9' * (n - zero) + '8' * zero\n else\n puts '9' * (n - zero - 1) + '8' + '8' * zero\n end\nend\n"}, {"source_code": "t = gets.chomp.to_i\nt.times do \n n = gets.chomp.to_i\n # 1.upto (n) do |i|\n # if i == n\n # print \"8\"\n # print \"\\n\"\n # else\n # print \"9\"\n # end\n # end\n atai = n / 4\n if n % 4 != 0\n hituyou = atai + 1\n else\n hituyou = atai\n end\n arr = \"9\" * (n - hituyou)\n print arr\n ar = \"8\" * hituyou\n print ar\n print \"\\n\"\n\nend"}, {"source_code": "gets.to_i.times do\n n = gets.to_i\n c = (n + 3) / 4\n (n - c).times do\n print 9\n end\n c.times do\n print 8\n end\n puts\nend"}, {"source_code": "def pow(str,n)\n if n == 0\n \"\"\n elsif n % 2 == 0\n pow((str+str), n/2)\n else\n str + pow(str, n-1)\n end\nend\nt = gets.to_i\nans = []\nt.times do\n n = gets.to_i\n num8 = 1 + (n-1)/4\n num9 = n - num8\n str = \"\"\n str = pow(\"9\",num9) + pow(\"8\",num8)\n ans << str\nend\nans.each do |s|\n puts s\nend"}], "negative_code": [{"source_code": "require 'set'; require 'prime'; require 'pp'\nINF=Float::INFINITY; MOD=10**9+7\nt = gets.chomp.to_i\nt.times do\n n = gets.chomp.to_i\n puts '9' * (n - 1) + '8'\nend\n"}, {"source_code": "require 'set'; require 'prime'; require 'pp'\nINF=Float::INFINITY; MOD=10**9+7\nt = gets.chomp.to_i\nt.times do\n n = gets.chomp.to_i\n zero = n / 4\n if n % 4 == 0\n puts '9' * (n - zero) + '0' * zero\n else\n puts '9' * (n - zero - 1) + '8' + '0' * zero\n end\nend\n"}, {"source_code": "t = gets.chomp.to_i\nt.times do \n n = gets.chomp.to_i\n # 1.upto (n) do |i|\n # if i == n\n # print \"8\"\n # print \"\\n\"\n # else\n # print \"9\"\n # end\n # end\n arr = \"9\" * (n-1)\n print arr\n print \"8\"\n print \"\\n\"\n\nend"}, {"source_code": "gets.to_i.times do\n n = gets.to_i\n c = (n + 3) / 4\n (n - c).times do\n print 9\n end\n c.times do\n print 8\n end\nend"}], "src_uid": "80c75a4c163c6b63a614075e094ad489"} {"nl": {"description": "You're given two arrays $$$a[1 \\dots n]$$$ and $$$b[1 \\dots n]$$$, both of the same length $$$n$$$.In order to perform a push operation, you have to choose three integers $$$l, r, k$$$ satisfying $$$1 \\le l \\le r \\le n$$$ and $$$k > 0$$$. Then, you will add $$$k$$$ to elements $$$a_l, a_{l+1}, \\ldots, a_r$$$.For example, if $$$a = [3, 7, 1, 4, 1, 2]$$$ and you choose $$$(l = 3, r = 5, k = 2)$$$, the array $$$a$$$ will become $$$[3, 7, \\underline{3, 6, 3}, 2]$$$.You can do this operation at most once. Can you make array $$$a$$$ equal to array $$$b$$$?(We consider that $$$a = b$$$ if and only if, for every $$$1 \\le i \\le n$$$, $$$a_i = b_i$$$)", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 20$$$) — the number of test cases in the input. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\le n \\le 100\\ 000$$$) — the number of elements in each array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le 1000$$$). The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \\ldots, b_n$$$ ($$$1 \\le b_i \\le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.", "output_spec": "For each test case, output one line containing \"YES\" if it's possible to make arrays $$$a$$$ and $$$b$$$ equal by performing at most once the described operation or \"NO\" if it's impossible. You can print each letter in any case (upper or lower).", "sample_inputs": ["4\n6\n3 7 1 4 1 2\n3 7 3 6 3 2\n5\n1 1 1 1 1\n1 2 1 3 1\n2\n42 42\n42 42\n1\n7\n6"], "sample_outputs": ["YES\nNO\nYES\nNO"], "notes": "NoteThe first test case is described in the statement: we can perform a push operation with parameters $$$(l=3, r=5, k=2)$$$ to make $$$a$$$ equal to $$$b$$$.In the second test case, we would need at least two operations to make $$$a$$$ equal to $$$b$$$.In the third test case, arrays $$$a$$$ and $$$b$$$ are already equal.In the fourth test case, it's impossible to make $$$a$$$ equal to $$$b$$$, because the integer $$$k$$$ has to be positive."}, "positive_code": [{"source_code": "#!/usr/bin/env ruby\ncount = gets.to_i\n\nanswers = []\n\ncount.times do\n n = gets.chomp.to_i\n\n a = gets.chomp.split(' ').map(&:to_i)\n b = gets.chomp.split(' ').map(&:to_i)\n\n i = 0\n while a[i] == b[i] && i < n do\n i += 1\n end\n\n if i == n\n answers << \"YES\"\n elsif (k = a[i] - b[i]) > 0\n answers << \"NO\"\n else\n while i < n && a[i] - b[i] == k do\n i += 1\n end\n\n if i == n\n answers << \"YES\"\n elsif a[i] == b[i]\n i += 1\n while i < n && a[i] == b[i] do\n i += 1\n end\n\n if i == n\n answers << \"YES\"\n else\n answers << \"NO\"\n end\n else\n answers << \"NO\"\n end\n end\nend\n\nanswers.each { |answer| puts answer }\n"}, {"source_code": "def solve()\n n = gets.to_i\n a = gets.split.map(&:to_i)\n b = gets.split.map(&:to_i)\n done = false\n i = 0\n while i < n\n x = a[i] - b[i]\n if x != 0\n if done || x > 0\n puts \"NO\"\n return\n end\n done = true\n diff = x\n while i < n\n y = a[i] - b[i]\n if y == 0\n break\n elsif y != diff\n puts \"NO\"\n return\n end\n i += 1\n end\n end\n i += 1\n end\n puts \"YES\"\nend\n\nt = gets.to_i\nt.times do\n solve()\nend"}, {"source_code": "g = gets.chomp.to_i\ng.times{\nn = gets.to_i\na = gets.chomp.split(\" \")\nb = gets.chomp.split(\" \")\nmore = false\nmore2 = false\ndif = []\nn.times{|w|\ndif.push(b[w].to_i-a[w].to_i) \nif a[w].to_i > b[w].to_i\nputs \"NO\"\nmore = true\nbreak\nend\n}\nif (!more)\nif dif.uniq.size > 2 or (!dif.uniq.include?(0) and dif.uniq.size == 2)\n puts \"NO\"\nelse\n index = []\n dif.size.times{|w| if dif[w].to_i > 0 then index.push(w) end }\n index.size.times{|w|\n if w!=0 \n if index[w].to_i - index[w-1] != 1\n puts \"NO\"\n more2 = true\n break\n end\n end\n }\n if (!more2) then puts \"YES\" end\n end\nend\n \n}"}, {"source_code": "t = gets.to_i\nt.times do\n n = gets.to_i\n a = gets.split.map &:to_i\n b = gets.split.map &:to_i\n i = 0\n i += 1 while i < n && b[i] - a[i] == 0\n diff = b[i] - a[i] if i < n\n yes = true\n while i < n\n d = b[i] - a[i]\n if d < 0\n yes = false\n break\n else\n if d != diff\n if d != 0\n yes = false\n end\n break\n end\n end\n i += 1\n end\n i += 1 while yes && i < n && b[i] - a[i] == 0\n yes = false if i < n\n puts yes ? 'YES' : 'NO'\nend\n"}], "negative_code": [{"source_code": "g = gets.chomp.to_i\ng.times{\nn = gets.to_i\na = gets.chomp.split(\" \")\nb = gets.chomp.split(\" \")\nmore = false\nmore2 = false\ndif = []\nn.times{|w|\ndif.push(b[w].to_i-a[w].to_i) \nif a[w].to_i > b[w].to_i\nputs \"NO\"\nmore = true\nbreak\nend\n}\nif (!more)\nif dif.uniq.size > 1 and !dif.uniq.include?(0)\n puts \"NO\"\n break\nelse\n index = []\n dif.size.times{|w| if dif[w].to_i > 0 then index.push(w) end }\n index.size.times{|w|\n if w!=0 \n if index[w].to_i - index[w-1] != 1\n puts \"NO\"\n more2 = true\n break\n end\n end\n }\n \n if (!more2) then puts \"YES\" end\n end\nend\n \n}"}, {"source_code": "g = gets.chomp.to_i\ng.times{\nn = gets.to_i\na = gets.chomp.split(\" \")\nb = gets.chomp.split(\" \")\nmore = false\nmore2 = false\ndif = []\nn.times{|w|\ndif.push(b[w].to_i-a[w].to_i) \nif a[w].to_i > b[w].to_i\nputs \"NO\"\nmore = true\n#break\nend\n}\nif (!more)\nif dif.uniq.size > 2 or (!dif.uniq.include?(0) and dif.uniq.size == 2)\n puts \"NO\"\nelse\n index = []\n dif.size.times{|w| if dif[w].to_i > 0 then index.push(w) end }\n index.size.times{|w|\n if w!=0 \n if index[w].to_i - index[w-1] != 1\n puts \"NO\"\n more2 = true\n #break\n end\n end\n }\n if (!more2) then puts \"YES\" end\n end\nend\n \n}"}, {"source_code": "g = gets.chomp.to_i\ng.times{\nn = gets.to_i\na = gets.chomp.split(\" \")\nb = gets.chomp.split(\" \")\nmore = false\nmore2 = false\ndif = []\nn.times{|w|\ndif.push(b[w].to_i-a[w].to_i)\nif a[w].to_i > b[w].to_i\nputs \"NO\"\nmore = true\nbreak\nend\n}\nif (!more)\nif dif.uniq.size > 2\nputs \"NO\"\nelse\nf = 0\ndif.size.times{|w|\nf+=dif[w].to_i\nif dif[w].to_s == \"0\" and f.to_i > 0 and w != dif.size-1\nputs \"NO\"\nmore2 = true\nbreak\nend\n}\nif (!more2)\nputs \"YES\"\nend\n\nend\n\n\nend\n\n}\n"}, {"source_code": "g = gets.chomp.to_i\ng.times{\nn = gets.to_i\na = gets.chomp.split(\" \")\nb = gets.chomp.split(\" \")\nmore = false\nmore2 = false\ndif = []\nn.times{|w|\ndif.push(b[w].to_i-a[w].to_i) \nif a[w].to_i > b[w].to_i\nputs \"NO\"\nmore = true\nbreak\nend\n}\nif (!more)\nif dif.uniq.size > 2\n puts \"NO\" \nelse\n index = []\n dif.size.times{|w| if dif[w].to_i > 0 then index.push(w) end }\n index.size.times{|w|\n if w!=0 \n if index[w].to_i - index[w-1] != 1\n puts \"NO\"\n more2 = true\n break\n end\n end\n }\n \n if (!more2) then puts \"YES\" end\n end\nend\n \n}"}, {"source_code": "g = gets.chomp.to_i\ng.times{\nn = gets.to_i\na = gets.chomp.split(\" \")\nb = gets.chomp.split(\" \")\nmore = false\ndif = []\nn.times{|w|\nif a[w].to_i < b[w].to_i then dif.push(b[w].to_i-a[w].to_i) end\nif a[w].to_i > b[w].to_i\nputs \"NO\"\nmore = true\nbreak\nend\n}\nif (!more)\nif dif.uniq.size > 1 then puts \"NO\" else puts \"YES\" end\nend\n\n}\n\n"}, {"source_code": "g = gets.chomp.to_i\ng.times{\nn = gets.to_i\na = gets.chomp.split(\" \")\nb = gets.chomp.split(\" \")\nmore = false\nmore2 = false\ndif = []\nn.times{|w|\ndif.push(b[w].to_i-a[w].to_i) \nif a[w].to_i > b[w].to_i\nputs \"NO\"\nmore = true\n#break\nend\n}\nif (!more)\nif dif.uniq.size > 1 and !dif.uniq.include?(0)\n puts \"NO\"\nelse\n index = []\n dif.size.times{|w| if dif[w].to_i > 0 then index.push(w) end }\n index.size.times{|w|\n if w!=0 \n if index[w].to_i - index[w-1] != 1\n puts \"NO\"\n more2 = true\n #break\n end\n end\n }\n \n if (!more2) then puts \"YES\" end\n end\nend\n \n}"}], "src_uid": "0e0ef011ebe7198b7189fce562b7d6c1"} {"nl": {"description": "Anton likes to play chess. Also he likes to do programming. No wonder that he decided to attend chess classes and programming classes.Anton has n variants when he will attend chess classes, i-th variant is given by a period of time (l1, i, r1, i). Also he has m variants when he will attend programming classes, i-th variant is given by a period of time (l2, i, r2, i).Anton needs to choose exactly one of n possible periods of time when he will attend chess classes and exactly one of m possible periods of time when he will attend programming classes. He wants to have a rest between classes, so from all the possible pairs of the periods he wants to choose the one where the distance between the periods is maximal.The distance between periods (l1, r1) and (l2, r2) is the minimal possible distance between a point in the first period and a point in the second period, that is the minimal possible |i - j|, where l1 ≤ i ≤ r1 and l2 ≤ j ≤ r2. In particular, when the periods intersect, the distance between them is 0.Anton wants to know how much time his rest between the classes will last in the best case. Help Anton and find this number!", "input_spec": "The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of time periods when Anton can attend chess classes. Each of the following n lines of the input contains two integers l1, i and r1, i (1 ≤ l1, i ≤ r1, i ≤ 109) — the i-th variant of a period of time when Anton can attend chess classes. The following line of the input contains a single integer m (1 ≤ m ≤ 200 000) — the number of time periods when Anton can attend programming classes. Each of the following m lines of the input contains two integers l2, i and r2, i (1 ≤ l2, i ≤ r2, i ≤ 109) — the i-th variant of a period of time when Anton can attend programming classes.", "output_spec": "Output one integer — the maximal possible distance between time periods.", "sample_inputs": ["3\n1 5\n2 6\n2 3\n2\n2 4\n6 8", "3\n1 5\n2 6\n3 7\n2\n2 4\n1 4"], "sample_outputs": ["3", "0"], "notes": "NoteIn the first sample Anton can attend chess classes in the period (2, 3) and attend programming classes in the period (6, 8). It's not hard to see that in this case the distance between the periods will be equal to 3.In the second sample if he chooses any pair of periods, they will intersect. So the answer is 0."}, "positive_code": [{"source_code": "n=gets.chomp.to_i\nchess_start=[]\nchess_end=[]\nn.times do\n time=gets.chomp.split\n #puts \"#{time[0]} #{time[1]}\"\n chess_start< rest\n rest = chess_start[-1]-program_end[0]\nend\nif rest < 0\n puts 0\nelse\n puts rest\nend\n"}, {"source_code": "n = gets.to_i\nnt = n.times.map{gets.split.map(&:to_i)}\nm = gets.to_i\nmt = m.times.map{gets.split.map(&:to_i)}\nmx = 0\nmx = [mt.max_by{|l, r| l}[0] - nt.min_by{|l, r| r}[1], mx].max\nmx = [nt.max_by{|l, r| l}[0] - mt.min_by{|l, r| r}[1], mx].max\np mx\n\n"}, {"source_code": "n = gets.to_i\n\nchess = []\n\nn.times do\n chess << gets.strip.split(' ').map(&:to_i)\nend\n\nm = gets.to_i\n\nprog = []\n\nm.times do\n prog << gets.strip.split(' ').map(&:to_i)\nend\n\nchess.sort! { |x,y| x[1] <=> y[1] }\nprog.sort! { |x,y| y[0] <=> x[0] }\n\nmax_diff = 0\n\n# chess.each do |c|\n# prog.each do |p|\n# break if c[0] - p[1] <= 0 && p[0] - c[1] <= 0\n# break if c[0] - p[1] <= max_diff && p[0] - c[1] <= max_diff\n# max_diff = [c[0] - p[1], max_diff].max\n# max_diff = [p[0] - c[1], max_diff].max\n# end\n# end\n\n# puts max_diff\n\nlowest_in_chess = chess[0][1]\nhighest_in_prog = prog[0][0]\nmax_diff = [highest_in_prog - lowest_in_chess, max_diff].max\n\nchess.sort! { |x,y| y[0] <=> x[0] }\nprog.sort! { |x,y| x[1] <=> y[1] }\nhighest_in_chess = chess[0][0]\nlowest_in_prog = prog[0][1]\n\nmax_diff = [highest_in_chess - lowest_in_prog, max_diff].max\n\nputs max_diff\n\n"}, {"source_code": "def f()\n lo, hi = 10**9, 0\n gets.to_i.times do\n l, r = gets.split.map { |s| s.to_i }\n if lo > r\n lo = r\n end\n if hi < l\n hi = l\n end\n end\n return lo, hi\nend\n\na, b = f\nc, d = f\np [0, b - c, d - a].max\n"}, {"source_code": "def f()\n lo, hi = 10**9, 0\n gets.to_i.times do\n l, r = gets.split.map(&:to_i)\n if lo > r\n lo = r\n end\n if hi < l\n hi = l\n end\n end\n return lo, hi\nend\n\na, b = f\nc, d = f\np [0, b - c, d - a].max\n"}], "negative_code": [{"source_code": "n = gets.to_i\n\nchess = []\n\nn.times do\n chess << gets.strip.split(' ').map(&:to_i)\nend\n\nm = gets.to_i\n\nprog = []\n\nm.times do\n prog << gets.strip.split(' ').map(&:to_i)\nend\n\nchess.sort! { |x,y| x[0] <=> y[0] }\nprog.sort! { |x,y| y[0] <=> x[0] }\n\nmax_diff = 0\n\nchess.each do |c|\n prog.each do |p|\n break if p[0] <= c[1]\n max_diff = [c[1] - p[0], max_diff].max\n max_diff = [p[0] - c[1], max_diff].max\n end\nend\n\nputs max_diff\n"}], "src_uid": "4849a1f65afe69aad12c010302465ccd"} {"nl": {"description": "It is nighttime and Joe the Elusive got into the country's main bank's safe. The safe has n cells positioned in a row, each of them contains some amount of diamonds. Let's make the problem more comfortable to work with and mark the cells with positive numbers from 1 to n from the left to the right.Unfortunately, Joe didn't switch the last security system off. On the plus side, he knows the way it works.Every minute the security system calculates the total amount of diamonds for each two adjacent cells (for the cells between whose numbers difference equals 1). As a result of this check we get an n - 1 sums. If at least one of the sums differs from the corresponding sum received during the previous check, then the security system is triggered.Joe can move the diamonds from one cell to another between the security system's checks. He manages to move them no more than m times between two checks. One of the three following operations is regarded as moving a diamond: moving a diamond from any cell to any other one, moving a diamond from any cell to Joe's pocket, moving a diamond from Joe's pocket to any cell. Initially Joe's pocket is empty, and it can carry an unlimited amount of diamonds. It is considered that before all Joe's actions the system performs at least one check.In the morning the bank employees will come, which is why Joe has to leave the bank before that moment. Joe has only k minutes left before morning, and on each of these k minutes he can perform no more than m operations. All that remains in Joe's pocket, is considered his loot.Calculate the largest amount of diamonds Joe can carry with him. Don't forget that the security system shouldn't be triggered (even after Joe leaves the bank) and Joe should leave before morning.", "input_spec": "The first line contains integers n, m and k (1 ≤ n ≤ 104, 1 ≤ m, k ≤ 109). The next line contains n numbers. The i-th number is equal to the amount of diamonds in the i-th cell — it is an integer from 0 to 105.", "output_spec": "Print a single number — the maximum number of diamonds Joe can steal.", "sample_inputs": ["2 3 1\n2 3", "3 2 2\n4 1 3"], "sample_outputs": ["0", "2"], "notes": "NoteIn the second sample Joe can act like this:The diamonds' initial positions are 4 1 3.During the first period of time Joe moves a diamond from the 1-th cell to the 2-th one and a diamond from the 3-th cell to his pocket.By the end of the first period the diamonds' positions are 3 2 2. The check finds no difference and the security system doesn't go off.During the second period Joe moves a diamond from the 3-rd cell to the 2-nd one and puts a diamond from the 1-st cell to his pocket.By the end of the second period the diamonds' positions are 2 3 1. The check finds no difference again and the security system doesn't go off.Now Joe leaves with 2 diamonds in his pocket."}, "positive_code": [{"source_code": "n, m, k = gets.split.map(&:to_i)\nif n.even?\n\tb = 0\nelse\n\ta = gets.split.map(&:to_i)\n\tb = m / ((n + 1) / 2) * k\n\ta.each_with_index do |j, i|\n\t\tb = [b, j].min if i.even?\n\tend\nend\nputs b\n"}], "negative_code": [], "src_uid": "b81e7a786e4083cf7188f718bc045a85"} {"nl": {"description": "On a random day, Neko found $$$n$$$ treasure chests and $$$m$$$ keys. The $$$i$$$-th chest has an integer $$$a_i$$$ written on it and the $$$j$$$-th key has an integer $$$b_j$$$ on it. Neko knows those chests contain the powerful mysterious green Grapes, thus Neko wants to open as many treasure chests as possible.The $$$j$$$-th key can be used to unlock the $$$i$$$-th chest if and only if the sum of the key number and the chest number is an odd number. Formally, $$$a_i + b_j \\equiv 1 \\pmod{2}$$$. One key can be used to open at most one chest, and one chest can be opened at most once.Find the maximum number of chests Neko can open.", "input_spec": "The first line contains integers $$$n$$$ and $$$m$$$ ($$$1 \\leq n, m \\leq 10^5$$$) — the number of chests and the number of keys. The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\leq a_i \\leq 10^9$$$) — the numbers written on the treasure chests. The third line contains $$$m$$$ integers $$$b_1, b_2, \\ldots, b_m$$$ ($$$1 \\leq b_i \\leq 10^9$$$) — the numbers written on the keys.", "output_spec": "Print the maximum number of chests you can open.", "sample_inputs": ["5 4\n9 14 6 2 11\n8 4 7 20", "5 1\n2 4 6 8 10\n5", "1 4\n10\n20 30 40 50"], "sample_outputs": ["3", "1", "0"], "notes": "NoteIn the first example, one possible way to unlock $$$3$$$ chests is as follows: Use first key to unlock the fifth chest, Use third key to unlock the second chest, Use fourth key to unlock the first chest. In the second example, you can use the only key to unlock any single chest (note that one key can't be used twice).In the third example, no key can unlock the given chest."}, "positive_code": [{"source_code": "N, M = gets.split.map(&:to_i)\nA = gets.split.map(&:to_i)\nB = gets.split.map(&:to_i)\n\na_odds = A.count(&:odd?)\na_evens = A.count(&:even?)\nb_odds = B.count(&:odd?)\nb_evens = B.count(&:even?)\n\nputs [a_odds, b_evens].min + [a_evens, b_odds].min\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\nas = gets.split.map(&:to_i)\nbs = gets.split.map(&:to_i)\n\nasodd = as.count { |a| a.odd? }\naseven = n - asodd\n\nbsodd = bs.count { |b| b.odd? }\nbseven = m - bsodd\n\nputs [asodd, bseven].min + [aseven, bsodd].min"}, {"source_code": "n,m = gets.split.map(&:to_i)\na = gets.split.map{|i|i.to_i%2}\nb = gets.split.map{|i|i.to_i%2}\nputs [a.count(0),b.count(1)].min+[a.count(1),b.count(0)].min"}, {"source_code": "\nn, m = gets.strip.split(\" \").map(&:to_i)\na = gets.strip.split(\" \").map(&:to_i)\nb = gets.strip.split(\" \").map(&:to_i)\n\na_even = a.select{|e| e.even? }.size\na_odd = n - a_even\n\nb_even = b.select{|e| e.even? }.size\nb_odd = m - b_even\n\nmax = [a_even, b_odd].min + [a_odd, b_even].min\np max\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\nchests = gets.split.map(&:to_i)\nkeys = gets.split.map(&:to_i)\n\ncntC0 = 0\ncntC1 = 0\ncntK0 = 0\ncntK1 = 0\n\nchests.each do |c|\n if c % 2 == 0\n cntC0 += 1\n elsif c % 2 == 1\n cntC1 += 1\n end\nend\n\nkeys.each do |k|\n if k % 2 == 0\n cntK0 += 1\n elsif k % 2 == 1\n cntK1 += 1\n end\nend\n\nputs [cntC0, cntK1].min + [cntC1, cntK0].min\n"}, {"source_code": "n, m = gets.split.map &:to_i\na = gets.split.map &:to_i\nb = gets.split.map &:to_i\nf = a.select { |x| x%2 == 0 }.size\ns = b.select { |x| x%2 == 0 }.size\nputs [f, b.size - s].min + [a.size - f, s].min\n"}], "negative_code": [], "src_uid": "bc532d5c9845940b5f59485394187bf6"} {"nl": {"description": "We often go to supermarkets to buy some fruits or vegetables, and on the tag there prints the price for a kilo. But in some supermarkets, when asked how much the items are, the clerk will say that $$$a$$$ yuan for $$$b$$$ kilos (You don't need to care about what \"yuan\" is), the same as $$$a/b$$$ yuan for a kilo.Now imagine you'd like to buy $$$m$$$ kilos of apples. You've asked $$$n$$$ supermarkets and got the prices. Find the minimum cost for those apples.You can assume that there are enough apples in all supermarkets.", "input_spec": "The first line contains two positive integers $$$n$$$ and $$$m$$$ ($$$1 \\leq n \\leq 5\\,000$$$, $$$1 \\leq m \\leq 100$$$), denoting that there are $$$n$$$ supermarkets and you want to buy $$$m$$$ kilos of apples. The following $$$n$$$ lines describe the information of the supermarkets. Each line contains two positive integers $$$a, b$$$ ($$$1 \\leq a, b \\leq 100$$$), denoting that in this supermarket, you are supposed to pay $$$a$$$ yuan for $$$b$$$ kilos of apples.", "output_spec": "The only line, denoting the minimum cost for $$$m$$$ kilos of apples. Please make sure that the absolute or relative error between your answer and the correct answer won't exceed $$$10^{-6}$$$. Formally, let your answer be $$$x$$$, and the jury's answer be $$$y$$$. Your answer is considered correct if $$$\\frac{|x - y|}{\\max{(1, |y|)}} \\le 10^{-6}$$$.", "sample_inputs": ["3 5\n1 2\n3 4\n1 3", "2 1\n99 100\n98 99"], "sample_outputs": ["1.66666667", "0.98989899"], "notes": "NoteIn the first sample, you are supposed to buy $$$5$$$ kilos of apples in supermarket $$$3$$$. The cost is $$$5/3$$$ yuan.In the second sample, you are supposed to buy $$$1$$$ kilo of apples in supermarket $$$2$$$. The cost is $$$98/99$$$ yuan."}, "positive_code": [{"source_code": "n,m = gets.chomp.split \n\nn = n.to_i \nm = m.to_f \n\n# price = Array.new(n)\ntprice = Array.new(n) \n\nfor i in 0...n \n a,b = gets.chomp.split \n tprice[i] = a.to_f / b.to_f \nend \n\nputs (tprice.min)*m"}, {"source_code": "n,m=gets.split.map &:to_i\nans=1000000000.0\nn.times do\n a,b=gets.split.map &:to_f\n cost=m*(a/b)\n ans=[ans,cost].min\nend\np ans"}, {"source_code": "n,m=gets.split.map &:to_i\nans=1000000000.0\nn.times do\n a,b=gets.split.map &:to_f\n cost=m*(a/b)\n ans=ans= mx \n puts player\n break\n end\n end\nend"}, {"source_code": "a={}\nc={}\nz={}\nl=[]\ngets.to_i.times{\n\tq,w=gets.split\n\ta[q]=a[q].to_i+w.to_i;\n\tl+=[[q,w]]\n}\nm=a.values.max\nl.each{|q,w|\n\tc[q]=c[q].to_i+w.to_i;\n\tif c[q]>=m and a[q]==m then\n\t\tputs q\n\t\texit\n\tend\n}\n\n"}, {"source_code": "# frozen_string_literal: true\n\nlines = gets.to_i\n\nscore_map = {}\nat_index_by_name = {}\n1.upto(lines) do |i|\n name, score = gets.split\n score = score.to_i\n\n score_map[name] ||= 0\n score_map[name] += score\n\n at_index_by_name[name] ||= []\n at_index_by_name[name] << [score_map[name], i]\nend\n\nscores = score_map.to_a.sort_by(&:last)\nputs(\n scores\n .select { |score| scores.last.last == score.last }\n .min_by { |score| at_index_by_name[score.first].find { |e| e.first >= score.last }.last }\n .first\n)\n"}, {"source_code": "n = gets.to_i\nnames = Array.new\nscores = Array.new\nwinn = Array.new\nwins = Array.new\nn.times do\n name, score = gets.split(\" \")\n score = score.to_i\n index = nil\n 0.upto(names.size-1) do |i|\n if names[i] == name\n index = i\n scores[index] += score\n break\n end\n end\n if index.nil?\n index = names.size\n names << name\n scores << score\n end\n winn << index\n wins << score\nend\nm = scores.max\nscoresim = [0]*names.size\nwinner = -1\nn.times do |i|\n scoresim[winn[i]] += wins[i]\n if scoresim[winn[i]] >= m and scores[winn[i]] == m\n winner = winn[i]\n break\n end\nend\nputs names[winner]\n"}, {"source_code": "a={}\nc={}\nz={}\nl=[]\ngets.to_i.times{\n\tq,w=gets.split\n\ta[q]=a[q].to_i+w.to_i;\n\tl+=[[q,w]]\n}\nm=a.values.max\nl.each{|q,w|\n\tc[q]=c[q].to_i+w.to_i;\n\tif c[q]>=m and a[q]==m then\n\t\tputs q\n\t\texit\n\tend\n}\n\n"}, {"source_code": "log = []\nscores = Hash.new(0)\ncheck = Hash.new(0)\ngets.to_i.times{\n n,q = gets.split\n scores[n] += q.to_i\n log << [ n, q ] \n}\nm = scores.values.max\nlog.each{ | n, q |\n check[n] += q.to_i\n if check[n] >= m and scores[n] == m then\n puts n\n exit\n end\n}"}, {"source_code": "input = []\ngets.to_i.times { input << gets }\n\ns = Hash.new(0)\ninput.each do |i|\n name, score = i.split\n s[name] += score.to_i\nend\n\nmax = s.values.max\n\nss = Hash.new(0)\ninput.each do |i|\n name, score = i.split\n ss[name] += score.to_i\n if ss[name] >= max && s[name] == max\n puts name\n break\n end\nend"}, {"source_code": "\ndef process(m, ss)\n max = m.values.max\n maxes = m.select {|k, v| v == max}\n\n res = nil\n idx = 1001\n\n if maxes.size > 1\n ss = ss.select {|k, v| maxes.include? k}\n ss.each do |k, v|\n sum = 0\n v.each do |vv|\n sum += vv.score\n if sum >= max || vv.index > idx\n if res == nil || vv.index < idx\n res = k\n idx = vv.index\n end\n break\n end\n end\n end\n return res\n end\n\n maxes.keys[0]\nend\n\nnum = STDIN.gets.chomp.to_i\n\nS = Struct.new(:index, :score)\nm = {}\ns = {}\n\nn = 0\nwhile n < num\n n += 1\n line = STDIN.gets\n line.chomp!\n name, score = line.split\n score = score.to_i\n if m.include? name\n m[name] = m[name] + score\n else\n m[name] = score\n end\n if s.include? name\n s[name] << S.new(n, score)\n else\n s[name] = [S.new(n, score)]\n end\nend\n\nprint process(m, s)\n"}, {"source_code": "a={}\nc={}\nz={}\nl=[]\ngets.to_i.times{\n\tq,w=gets.split\n\ta[q]=a[q].to_i+w.to_i;\n\tl+=[[q,w]]\n}\nm=a.values.max\nl.each{|q,w|\n\tc[q]=c[q].to_i+w.to_i;\n\tif c[q]>=m and a[q]==m then\n\t\tputs q\n\t\texit\n\tend\n}\n"}, {"source_code": "h = {}\nn = gets.to_i\nl = []\nn.times { |i|\n a, b = gets.split\n l << [a,b]\n if h.key? a\n h[a] += b.to_i\n else\n h[a] = b.to_i\n end\n}\n\nmax = -1000 * n - 1\nindexes = []\nh.each do |a, b|\n if b > max\n max = b\n indexes = [a]\n elsif b == max\n indexes << a\n end\nend\n\nh.clear\n\nn.times { |i|\n\n a, b = l[i]\n\n if h.key? a\n h[a] += b.to_i\n else\n h[a] = b.to_i\n end\n\n if h[a] >= max and indexes.include? a\n print a\n exit(0)\n end\n\n}\n\n"}, {"source_code": "#!/usr/bin/ruby\n\nn = gets.to_i\nrecords = []\nrecords = (1..n).map do\n name, score = gets.split\n {name: name, score: score.to_i}\nend\n\ntable = {}\nrecords.each do |rec|\n name = rec[:name]\n table[name] ||= 0\n table[name] += rec[:score]\nend\n\nmax = table.values.max\ntable2 = {}\nrecords.each do |rec|\n name = rec[:name]\n table2[name] ||= 0\n table2[name] += rec[:score]\n if table2[name] >= max and table[name] == max\n puts name\n exit\n end\nend"}, {"source_code": "input = gets.chomp.to_i\nlines=Array.new\nplayers = Hash.new\nwinner = 0\ninput.times do\n lines.push gets.chomp\n player , points = lines.at(lines.length-1).split(\" \")\n players[player] = players[player].to_i + points.to_i\nend\nplayers.each do |k,v|\n winner = v if v > winner\nend\nanswers = players.select do |k ,v|\n v==winner\nend\nplayers=Hash.new\nlines.each do |i|\n player , points = i.split(\" \")\n players[player] = players[player].to_i + points.to_i\n if answers.key? player\n if winner <= players[player]\n puts player\n break\n end\n end\nend"}, {"source_code": "n = gets.to_i\nans = Hash.new\nrec = Array.new\nfor i in 0...n do\n name, point = gets.split\n name = name.to_sym\n point = point.to_i\n\n if ans[name].nil?\n ans[name] = point\n else\n ans[name] = ans[name] + point\n end\n\n rec[i] = name, ans[name]\nend\n\ntemp = ans.to_a\nmax_num = temp.max { |a, b| a[1] <=> b[1] }[1]\nans_index = 0\n\nfor r in rec do\n if r[1] >= max_num && ans[r[0]] == max_num\n ans_index = r[0]\n break\n end\nend\n\nputs ans_index"}, {"source_code": "a={}\nc={}\nz={}\nl=[]\ngets.to_i.times{\n\tq,w=gets.split\n\ta[q]=a[q].to_i+w.to_i;\n\tl+=[[q,w]]\n}\nm=a.values.max\nl.each{|q,w|\n\tc[q]=c[q].to_i+w.to_i;\n\tif c[q]>=m and a[q]==m then\n\t\tputs q\n\t\texit\n\tend\n}\n"}, {"source_code": "#!/usr/bin/env ruby\n\n\nn = gets.chomp!.to_i\nh1 = {}\na = []\nn.times do\n name, round = gets.chomp!.split(\" \")\n round = round.to_i\n h1[name] = h1[name].to_i + round\n a << [name, round.to_i]\nend\n\nmax_score = h1.values.max\nh2 = {}\na.each do | e |\n h2[e[0]] = h2[e[0]].to_i + e[1]\n if h2[e[0]] >= max_score and h1[e[0]] == max_score\n puts e[0]\n exit 0\n end\nend\n\n"}, {"source_code": "n=gets.chomp!.to_i\nmemo=Hash.new(0)\nresult=[]\nn.times do\n result << gets.chomp.split\nend\nresult.each do |name,score|\n memo[name]+=score.to_i\nend\nmax=memo.values.max\nmemo.each_key do |key|\n if memo[key] == max\n memo[key]=0\n else\n memo[key] =-10000\n end\nend\nresult.each do |name,score|\n memo[name]+=score.to_i\n if memo[name] >= max\n puts name\n break\n end\nend\n"}, {"source_code": "a={}\nc={}\nz={}\nl=[]\ngets.to_i.times{\n\tq,w=gets.split\n\ta[q]=a[q].to_i+w.to_i;\n\tl+=[[q,w]]\n}\nm=a.values.max\nl.each{|q,w|\n\tc[q]=c[q].to_i+w.to_i;\n\tif c[q]>=m and a[q]==m then\n\t\tputs q\n\t\texit\n\tend\n}\n\n"}, {"source_code": "#!/usr/bin/env ruby\n\nt = gets.to_i\nh1 = Hash.new(0)\nh2 = Hash.new(0)\na = Array.new(t)\nb = Array.new(t)\nm = -1\nwinner = \"\"\n\nfor i in 0...t\n\n s = gets.split(' ')\n\n a[i] = s[0]\n b[i] = s[1].to_i\n\n h1[a[i]] += b[i]\n\nend\n\nh1.each_pair { |key, val|\n if val > m\n m = val\n end\n}\n\nfor i in 0...t\n\n h2[a[i]] += b[i]\n if h2[a[i]] >= m && h1[a[i]] == m\n winner = a[i]\n break\n end\n\nend\n\nputs winner\n\n \n"}, {"source_code": "n=gets.chomp.to_i\nnc=0 \n@hash={}\n@hash_total={}\nloop do\t\n\tbreak if nc == n\n\tinput=gets.chomp.split(\" \")\n\tk=0\n\tif nc >= 1\n\t\t@hash.each_key do |plr_nm|\n\t\t\tif plr_nm!=input[0]\n\t\t\t\tk+=1\n\t\t\tend\n\t\tend\n\t\tif k==@hash.length\n\t\t\t@hash[input [0]]=[0]\n\t\t\t@hash[input [0]][nc]=input[1].to_i\n\t\telse\n\t\t@hash[input [0]][nc]=input[1].to_i\n\t\tend\n\telse\n\t\t@hash[input [0]]=[input[1].to_i]\n\tend\n\n\tnc+=1\nend\n\n\ndef Total(key,array)\n k=0\n\tarray.each do|l|\n\t\tif l== nil\n\t\t\tl=0\n\t\tend\n\t k=k+l\n\tend\n @hash_total[key]=k\nend\n@hash.each_pair do|a,b|\n\tTotal(a,b)\nend\n\ndef Tiebreaker(a,b)\n ka=0\n ba=0\n k=0\n @hash[a[0]].each do|cn|\n\t\tif cn== nil\n\t\t\tcn=0\n\t\tend\n\t\tk=k+cn\n\t\tka+=1\n\t\tbreak if k >= a[1]\n\tend\t\n\tk=0\t\t\n @hash[b[0]].each do|cn|\n if cn== nil\n\t\t\tcn=0\n\t\tend\n\t\tk=k+cn\n\t\tba+=1\n\t\tbreak if k >= b[1]\n\tend\t\n\tif ka>ba\n\t\treturn b\n\telse \n\t\treturn a\n\tend\t\nend\t\n\t\t\t\nwin= @hash_total.inject do |memo,n|\n\tif memo[1]>n[1]\n\t\tmemo\n\telsif memo[1]==n[1]\n\t\tTiebreaker(memo,n)\n\telse \n\t\tn\n\tend\t\nend\nputs win[0]\n"}, {"source_code": "num=Hash.new(0)\nturn=Hash.new(0)\narray=[]\na=gets.chomp.to_i\n0.upto(a-1) do |i|\ntemp=gets.chomp.split(\" \")\nnum[temp[0]]+=temp[1].to_i\narray<= mx)\n puts nm\n exit\n end\nend\n\n"}, {"source_code": "a={}\nc={}\nz={}\nl=[]\ngets.to_i.times{\n\tq,w=gets.split\n\ta[q]=a[q].to_i+w.to_i;\n\tl+=[[q,w]]\n}\nm=a.values.max\nl.each{|q,w|\n\tc[q]=c[q].to_i+w.to_i;\n\tif c[q]>=m and a[q]==m then\n\t\tputs q\n\t\texit\n\tend\n}\n\n"}, {"source_code": "# http://codeforces.com/problemset/problem/2/A\n\nRound = Struct.new(:player, :score)\n\nclass WinnerCalculator\n def self.get_winner(scoreboard)\n rounds = []\n scoreboard.each do |round_line|\n player, score = round_line.split\n rounds << Round.new(player, score.to_i)\n end\n\n scoring = Hash.new(0)\n rounds.each do |round|\n scoring[round.player] += round.score\n end\n max_score = scoring.values.max\n players_with_max_score = scoring.select { |player, score| score == max_score }\n\n if players_with_max_score.size == 1\n return players_with_max_score.keys.first\n end\n\n tiebreak_scoring = Hash.new(0)\n rounds.each do |round|\n tiebreak_scoring[round.player] += round.score\n if tiebreak_scoring[round.player] >= max_score && scoring[round.player] == max_score\n return round.player\n end\n end\n end\nend\n\nif __FILE__ == $0\n scoreboard = []\n gets.to_i.times { scoreboard << gets }\n winner = WinnerCalculator.get_winner(scoreboard)\n puts winner\nend\n"}, {"source_code": "scoring = Hash.new(0)\nscoreboard = []\ngets.to_i.times do\n player, score = gets.split\n scoring[player] += score.to_i\n scoreboard << [player, score.to_i]\nend\n\nmax_score = scoring.values.max\n\ntiebreak_scoring = Hash.new(0)\nscoreboard.each do |player, score|\n tiebreak_scoring[player] += score.to_i\n if scoring[player] == max_score && tiebreak_scoring[player] >= max_score\n puts player\n break\n end\nend\n"}, {"source_code": "n=gets.chomp.to_i\nnc=0 \n@hash={}\n@hash_total={}\nloop do\t\n\tbreak if nc == n\n\tinput=gets.chomp.split(\" \")\n\tk=0\n\tif nc >= 1\n\t\t@hash.each_key do |plr_nm|\n\t\t\tif plr_nm!=input[0]\n\t\t\t\tk+=1\n\t\t\tend\n\t\tend\n\t\tif k==@hash.length\n\t\t\t@hash[input [0]]=[0]\n\t\t\t@hash[input [0]][nc]=input[1].to_i\n\t\telse\n\t\t@hash[input [0]][nc]=input[1].to_i\n\t\tend\n\telse\n\t\t@hash[input [0]]=[input[1].to_i]\n\tend\n\n\tnc+=1\nend\n\n\ndef Total(key,array)\n k=0\n\tarray.each do|l|\n\t\tif l== nil\n\t\t\tl=0\n\t\tend\n\t k=k+l\n\tend\n @hash_total[key]=k\nend\n@hash.each_pair do|a,b|\n\tTotal(a,b)\nend\n\ndef Tiebreaker(a,b)\n ka=0\n ba=0\n k=0\n @hash[a[0]].each do|cn|\n\t\tif cn== nil\n\t\t\tcn=0\n\t\tend\n\t\tk=k+cn\n\t\tka+=1\n\t\tbreak if k >= a[1]\n\tend\t\n\tk=0\t\t\n @hash[b[0]].each do|cn|\n if cn== nil\n\t\t\tcn=0\n\t\tend\n\t\tk=k+cn\n\t\tba+=1\n\t\tbreak if k >= b[1]\n\tend\t\n\tif ka>ba\n\t\treturn b\n\telse \n\t\treturn a\n\tend\t\nend\t\n\t\t\t\nwin= @hash_total.inject do |memo,n|\n\tif memo[1]>n[1]\n\t\tmemo\n\telsif memo[1]==n[1]\n\t\tTiebreaker(memo,n)\n\telse \n\t\tn\n\tend\t\nend\nputs win[0]\n"}, {"source_code": "num=Hash.new(0)\nturn=Hash.new(0)\narray=[]\na=gets.chomp.to_i\n0.upto(a-1) do |i|\ntemp=gets.chomp.split(\" \")\nnum[temp[0]]+=temp[1].to_i\narray< competitors[winner]\nend\n\nwinner_definition = {}\n\nstep_logs.each do |step_result|\n step_winner = step_result[0]\n step_winner_score = step_result[1]\n\n winner_definition[step_winner] ||= 0\n\n winner_definition[step_winner] += step_winner_score\n\n if (winner_definition[step_winner] >= competitors[winner]) && (competitors[step_winner] >= competitors[winner])\n puts step_winner\n break\n end\nend\n"}, {"source_code": "h = Hash.new {|h, k| h[k] = [[-1, 0, 0]]}\nnum_turns = gets.chomp.to_i\nnum_turns.times {|turn|\n name, score = gets.chomp.split\n score = score.to_i\n prev = h[name].last\n h[name] << [turn, score, prev.last + score]\n}\n\nplayers = h.keys\nmax_score = -1\nmax_players = []\nplayers.each {|player; player_score|\n player_score = h[player].last.last\n if player_score > max_score\n max_score = player_score\n max_players = [player]\n elsif player_score == max_score\n max_score = player_score\n max_players << player\n end\n}\n\nwinner = if max_players.size > 1\n max_players.map {|player|\n [h[player].detect {|(turn, _, score)| if score >= max_score; turn; end},\n player]\n }.sort_by {|(score, _)| score}.first.last\nelse\n max_players.first\nend\nputs winner\n"}, {"source_code": "#=================================\n# Better be sure than sorry. \n#=================================\n$stdin=File.open(\"./input.txt\",\"r\") if ENV[\"LOGNAME\"]==\"skydos\"\n#=================================\n\nn=gets.to_i\n\ndata = []\nhs = Hash.new(0)\n\nn.times{\n\tname, points = gets.split\t\n\t\n\tdata << [name, points.to_i]\n\ths[name]+=points.to_i\n}\n\nmax_points = hs.values.max\n\nplayers = hs.keys.select{|k| hs[k]==max_points}\n\nhs2 = Hash.new(0)\ndata.each{|name, points|\t\t\n\tnext if !players.include? name\n\n\ths2[name]+=points\n\t\n\tif hs2[name]>=max_points\n\t\tputs name\n\t\tbreak\n\tend\n}\n"}, {"source_code": "n = gets.to_i\nh = Hash.new\nnames = []\nscores = []\nwinners = []\n\nfor i in 0...n\n\n s, k = gets.chomp.split\n k = k.to_i\n\n h[s] = 0 if h[s].nil?\n\n h[s] += k\n\n names.push(s)\n scores.push(k)\nend\n\nmax = h.values.max\n\nh.each do |k,v|\n winners.push(k) if v >= max\nend\n\nwinners = winners.uniq\n\nh.clear\n\nfor i in 0...n\n if winners.include?(names[i])\n h[names[i]] = 0 if h[names[i]].nil?\n h[names[i]] += scores[i]\n if h[names[i]] >= max\n puts names[i]\n exit\n end\n end\nend\n"}, {"source_code": "# coding: utf-8\n\nn = gets.to_i\nh = {}\nst = []\nn.times {\n name, score = gets.split\n score = score.to_i\n h[name] ||= 0\n h[name] += score\n st << [name, score]\n}\nmax = h.values.max\ncands = []\nfor k,v in h\n if v == max\n cands << k\n end\nend\n\nif cands.length == 1\n puts cands[0]\nelse\n h2 = {}\n for s in st\n next if cands.index(s[0]) == nil\n h2[s[0]] ||= 0\n h2[s[0]] += s[1]\n if h2[s[0]] >= max\n puts s[0]\n break\n end\n end\nend"}, {"source_code": "a={}\nc={}\nz={}\nl=[]\ngets.to_i.times{\n\tq,w=gets.split\n\ta[q]=a[q].to_i+w.to_i;\n\tl+=[[q,w]]\n}\nm=a.values.max\nl.each{|q,w|\n\tc[q]=c[q].to_i+w.to_i;\n\tif c[q]>=m and a[q]==m then\n\t\tputs q\n\t\texit\n\tend\n}\n"}, {"source_code": "num_lines = gets().chomp.to_i\nscores = {}\nlog = []\nnum_lines.times{\n|i|\n name, score = gets().chomp.split(/[ ]+/)\n score = score.to_i\n if(scores.has_key?(name)) then\n scores[name] += score\n else\n scores[name] = score\n end\n log << [name, scores[name], i]\n}\n\nscores_a = scores.to_a\n\nmax_score = scores_a.max {\n|player1,player2|\n player1[1] <=> player2[1]\n}[1]\n\ntop_scores = scores_a.select {\n|player|\n player[1] == max_score\n}\n\nwinner = log.select{\n|line|\n top_scores.collect {\n |plr|\n plr[0]\n }.index(line[0]) &&\n line[1] >= max_score\n}.min{\n|line1,line2|\n line1[2] <=> line2[2]\n}[0]\n\nputs winner\n"}, {"source_code": "n = gets.to_i\nr = []\nfor i in 0 ... n \n a,b = gets.split\n r << [a, b.to_i]\nend\n\nh1 = Hash.new{0}\nr.each do |name,val|\n h1[name] += val\nend\n\nmaxi = h1.max{|x,y| x[1] <=> y[1]}[1]\n\nh2 = Hash.new{0}\nr.each do |name,val|\n h2[name] += val\n if (h1[name] == maxi && h2[name] >= maxi)\n puts name\n break\n end\nend\n"}, {"source_code": "def getIndex(val, h)\n if (h.key?(val))\n return h[val]\n end\n index = h.size\n return h[val] = index\nend\n\nn = gets.to_i\nh = {}\nlast = []\nsum = []\n\nnames = []\nvalues = []\n\nfor i in 0 ... n\n name,val = gets.split\n index = getIndex(name,h)\n val = val.to_i \n if (index < sum.size)\n sum[index] += val\n last[index] = i\n else\n sum << val\n last << i\n end\n names << name\n values << val\nend\n\nans = \"\"\nfastest = n\nmaxi = sum.max\nh.each do |key,index|\n next if (sum[index] != maxi)\n tsum, tmp = 0, n\n for i in 0 ... n\n next if (names[i] != key)\n tsum += values[i]\n if (tsum >= maxi)\n tmp = i\n break\n end\n end\n if (tmp < fastest)\n fastest, ans = tmp,key\n end\nend\n\n=begin\nmaxi = sum.max\nfastest = n\nh.each do |key,index|\n if (sum[index] == maxi && last[index] < fastest)\n fastest = last[index]\n ans = key\n end\nend\n=end\n\nputs ans\n"}, {"source_code": "n = gets.to_i\nr = []\nn.times do\n w, s = gets.split\n r << [w, s.to_i]\nend\n\na = Hash.new(0)\nr.each do |w, s|\n a[w] += s\nend\nmx = a.values.max\n\nb = Hash.new(0)\nr.each do |w, s|\n b[w] += s\n if b[w] >= mx and a[w] == mx\n puts w\n exit\n end\nend\n"}, {"source_code": "n = gets.to_i\n\nmax = 0\nplayers = Hash.new(0)\nrounds = []\nn.times do\n\tname, score = gets.split\n\tscore = score.to_i\n\trounds << [name, score]\nend\n\nplayers = rounds.inject(Hash.new(0)) { |h, round| h[round[0]] += round[1]; h }\nmax = players.max_by { |k, v| v}[1]\nwinners = players.select { |k, v| v == max }\nplayers = rounds.inject(Hash.new(0)) do |h, round|\n\tplayer = round[0]\n\th[player] += round[1]\n\tif (h[player] >= max) && winners.member?(player)\n\t\tputs player\n\t\tbreak\n\tend\n\th\nend"}, {"source_code": "scores = Hash.new(0)\nin_array = []\n\nn = gets.to_i\nn.times do\n\tl = gets.split(\" \")\n\tscores[l[0]] += l[1].to_i\n\tin_array << [l[0], l[1]]\nend\n\ntally = Hash.new(0)\nmax = scores.values.max\nin_array.each do |score|\n\ttally[score[0]] += score[1].to_i\n\tif (max == scores[score[0]].to_i) && (tally[score[0]].to_i >= max)\n\t\tputs score[0]\n\t\texit\n\tend\nend"}, {"source_code": "#http://codeforces.com/problemset/problem/2/A\n\nn = gets.to_i\ndir = Hash.new(0)\nstep = Array.new\nmax_val = 0\nmax_name = Array.new\n\nfor i in 1..n\n name, score = gets.chomp.strip.split(\" \")\n score = score.to_i\n dir[name] += score\n record = Hash[\"name\" => name, \"score\" => dir[name]]\n step << record\nend\n\ndir.each_key do |key|\n if dir[key] > max_val\n max_val = dir[key]\n max_name.clear\n max_name << key\n elsif dir[key] == max_val\n max_name << key\n end\nend\n\nflag = 0\nif max_name.size == 1\n puts max_name[0]\nelse\n for i in 0...step.size\n if step[i][\"score\"] >= max_val\n for j in 0...max_name.size\n if step[i][\"name\"] == max_name[j] and flag == 0\n puts max_name[j]\n flag = 1\n end\n end\n end\n end\nend"}, {"source_code": "a={}\nc={}\nz={}\nl=[]\ngets.to_i.times{\n\tq,w=gets.split\n\ta[q]=a[q].to_i+w.to_i;\n\tl+=[[q,w]]\n}\nm=a.values.max\nl.each{|q,w|\n\tc[q]=c[q].to_i+w.to_i;\n\tif c[q]>=m and a[q]==m then\n\t\tputs q\n\t\texit\n\tend\n}\n\n"}, {"source_code": "#!/usr/bin/env ruby\n\nclass Transaction < Struct.new(:index, :value, :result)\nend\n\n$players_scores = Hash.new\n$operation_index = 0\n\ndef process_score(score_string)\n\tparts = score_string.split(' ')\n\tname = parts[0]\n\tvalue = parts[1].to_i\n\n\tif (!$players_scores.include?(name))\n\t\t$players_scores[name] = Array.new\n\t\t$players_scores[name] << Transaction.new($operation_index, value, value)\n\telse\n\t\tplayer_info = $players_scores[name]\n\t\t$players_scores[name] << Transaction.new($operation_index, value, player_info[player_info.count - 1][:result] + value)\n\tend\n\n\t$operation_index += 1\nend\n\n# results for testing\n# match_results = []\n# match_results << \"andrew 3\"\n# match_results << \"andrew 2\"\n# match_results << \"andrew 1\"\n# match_results << \"mike 5\"\n# match_results << \"andrew -1\"\n# match_results << \"john 5\"\n\n# match_results << \"andrew 3\"\n# match_results << \"andrew 2\"\n# match_results << \"mike 5\"\n\nrepeats = gets.to_i\nmatch_results = []\n\nwhile (repeats > 0)\n\tmatch_results << gets.strip\n\trepeats -= 1\nend\n\nfor score_string in match_results\n\tprocess_score(score_string)\nend\n\nwinning_players = Array.new\nmax_score = 0\n\nfor score in $players_scores.values\n\tif (score[score.count - 1][:result] > max_score)\n\t\twinning_players.clear\n\t\tmax_score = score[score.count - 1][:result]\n\t\twinning_players << score\n\telsif score[score.count - 1][:result] == max_score\n\t\twinning_players << score\n\tend\t\t\nend\n\nmin_index = $operation_index\nwinner = winning_players[0]\n\nfor score_history in winning_players\n\tfor transaction in score_history\n\t\tif transaction[:result] >= max_score && transaction[:index] < min_index\n\t\t\tmin_index = transaction[:index]\n\t\t\twinner = score_history\n\t\tend\n\tend\nend\n\nputs $players_scores.invert[winner]"}, {"source_code": "n = STDIN.readline.to_i\nscore = Hash.new\nt = []\n\nn.times do |i|\n\tt << STDIN.readline.split\n\t# print STDIN.readline.split\n\t# print t\n\tt[i][1] = t[i][1].to_i\n\tif score[t[i][0]] == nil ;\n\t\tscore[t[i][0]] = 0\n\tend\n\tscore[t[i][0]] += t[i][1]\nend\n\nmaxscore = 0\nscore.each_value do |i|\n\tmaxscore = i if i > maxscore\nend\n\niscore = Hash.new\nans = \"\"\nn.times do |i|\n\tif iscore[t[i][0]] == nil ;\n\t\tiscore[t[i][0]] = 0\n\tend\n\tiscore[t[i][0]] += t[i][1]\n\tans = t[i][0]; break if iscore[t[i][0]] >= maxscore &&\n\t\t\t\t\t\t\t score[t[i][0]] == maxscore\nend\nprint ans\n"}, {"source_code": "a={}\nc={}\nz={}\nl=[]\ngets.to_i.times{\n\tq,w=gets.split\n\ta[q]=a[q].to_i+w.to_i;\n\tl+=[[q,w]]\n}\nm=a.values.max\nl.each{|q,w|\n\tc[q]=c[q].to_i+w.to_i;\n\tif c[q]>=m and a[q]==m then\n\t\tputs q\n\t\texit\n\tend\n}"}, {"source_code": "n = gets.to_i\na = n.times.map{gets.split}\nh = Hash.new(0)\na.each{|name, s| h[name] += s.to_i}\nmx = h.max_by{|name, s| s}[1]\nh2 = Hash.new(0)\na.each do |name, s|\n h2[name] += s.to_i\n if h[name] == mx && h2[name] >= mx\n puts name\n break\n end\nend\n"}, {"source_code": "n = gets.to_i\na = n.times.map{gets.split}\nh1 = a.each_with_object(Hash.new(0)){|(x, y), h| h[x] += y.to_i}\nmx = h1.values.max\nh2 = Hash.new(0)\na.each do |name, s|\n h2[name] += s.to_i\n if h1[name] == mx && h2[name] >= mx\n puts name\n break\n end\nend\n"}, {"source_code": "final = {}\n\nn = gets.to_i\nnames = []\nscores = []\nn.times do\n a = gets.split(/\\s+/)\n name = a[0]\n score = a[1].to_i\n if final[name].nil? then\n final[name] = 0\n end\n final[name] += score\n names << name\n scores << score\nend\n\nmax = 0\nfinal.each{|key, value| max = value if max < value}\n\ntotal = {}\nn.times do |i|\n name = names[i]\n score = scores[i]\n if total[name].nil? then\n total[name] = 0\n end\n total[name] += score\n if total[name] >= max && final[name] == max then\n puts name\n break\n end\nend\n"}, {"source_code": "a={}\nc={}\nz={}\nl=[]\ngets.to_i.times{\n\tq,w=gets.split\n\ta[q]=a[q].to_i+w.to_i;\n\tl+=[[q,w]]\n}\nm=a.values.max\nl.each{|q,w|\n\tc[q]=c[q].to_i+w.to_i;\n\tif c[q]>=m and a[q]==m then\n\t\tputs q\n\t\texit\n\tend\n}"}, {"source_code": "num_lines = gets().chomp.to_i\nscores = {}\nscores_fset ={}\n\naddscore = -> (fn, maxscore, index) do\n (-> (score) {\n prev = fn.call(score)\n return prev if prev >= 0\n return index if score <= maxscore\n return -1\n })\nend\n\nnum_lines.times do |index|\n line = gets().chomp\n name, score = line.split\n score = score.to_i\n scores[name] ||= 0\n scores[name] += score\n\n scores_fset[name] ||= -> (x) do\n (-> () {\n return index if x <= score\n return -1\n }).call()\n end\n\n if scores_fset[name].call(scores[name]) == -1\n scores_fset[name] = addscore.call(scores_fset[name], scores[name], index)\n end\nend\n\ntop_score = scores.values.max\ntop_scorers = scores.select {|k, v| v == top_score}\nputs top_scorers.min_by {|k, v| scores_fset[k].call(top_score)}[0]\n"}, {"source_code": "N = gets.to_i\nh = Hash.new { |hash, key| hash[key] = 0 }\na = []\nN.times do \n n, p = gets.split\n a << [n, p.to_i]\n h[n] += p.to_i\nend\n\nm = h.values.max\na.reject! { |x| h[x[0]] < m }\nh = Hash.new { |hash, key| hash[key] = 0 }\na.each do |n, p|\n h[n] += p\n if h[n] >= m\n puts n\n exit\n end\nend\n"}, {"source_code": "n = gets.chomp.to_i\n\nlog = []\ntotal = {}\n\nn.times do |i|\n line = gets.chomp.split(\" \")\n player = line[0]\n cur_score = line[1].to_i\n log << [player, cur_score]\n score = total[player]\n total[player] = score.nil? ? cur_score : score + cur_score\nend\n\nmax_score = total.values.max\nplayers_with_max_score = total.select{|k, v| v == max_score }\n\nif players_with_max_score.length > 1\n tot = {}\n log.each do |row|\n if players_with_max_score.keys.include? row[0]\n score = tot[row[0]]\n tot[row[0]] = score.nil? ? row[1] : score + row[1]\n if tot[row[0]] >= max_score\n puts row[0]\n exit\n end\n end\n \n end\nelse\n puts players_with_max_score.keys.first\nend"}, {"source_code": "n=gets.chomp.to_i\nnc=0 \n@hash={}\n@hash_total={}\nloop do\t\n\tbreak if nc == n\n\tinput=gets.chomp.split(\" \")\n\tk=0\n\tif nc >= 1\n\t\t@hash.each_key do |plr_nm|\n\t\t\tif plr_nm!=input[0]\n\t\t\t\tk+=1\n\t\t\tend\n\t\tend\n\t\tif k==@hash.length\n\t\t\t@hash[input [0]]=[0]\n\t\t\t@hash[input [0]][nc]=input[1].to_i\n\t\telse\n\t\t@hash[input [0]][nc]=input[1].to_i\n\t\tend\n\telse\n\t\t@hash[input [0]]=[input[1].to_i]\n\tend\n\n\tnc+=1\nend\n\n\ndef Total(key,array)\n k=0\n\tarray.each do|l|\n\t\tif l== nil\n\t\t\tl=0\n\t\tend\n\t k=k+l\n\tend\n @hash_total[key]=k\nend\n@hash.each_pair do|a,b|\n\tTotal(a,b)\nend\n\ndef Tiebreaker(a,b)\n ka=0\n ba=0\n k=0\n @hash[a[0]].each do|cn|\n\t\tif cn== nil\n\t\t\tcn=0\n\t\tend\n\t\tk=k+cn\n\t\tka+=1\n\t\tbreak if k >= a[1]\n\tend\t\n\tk=0\t\t\n @hash[b[0]].each do|cn|\n if cn== nil\n\t\t\tcn=0\n\t\tend\n\t\tk=k+cn\n\t\tba+=1\n\t\tbreak if k >= b[1]\n\tend\t\n\tif ka>ba\n\t\treturn b\n\telse \n\t\treturn a\n\tend\t\nend\t\n\t\t\t\nwin= @hash_total.inject do |memo,n|\n\tif memo[1]>n[1]\n\t\tmemo\n\telsif memo[1]==n[1]\n\t\tTiebreaker(memo,n)\n\telse \n\t\tn\n\tend\t\nend\nputs win[0]\n"}, {"source_code": "num=Hash.new(0)\nturn=Hash.new(0)\narray=[]\na=gets.chomp.to_i\n0.upto(a-1) do |i|\ntemp=gets.chomp.split(\" \")\nnum[temp[0]]+=temp[1].to_i\narray<=m and a[q]==m then\n\t\tputs q\n\t\texit\n\tend\n}"}, {"source_code": "n = gets.to_i\nlst = []\ntot = {}\nn.times do\n name, score = gets.split\n score = score.to_i\n lst << [name, score]\n tot[name] ||= 0\n tot[name] += score\nend\n\nm = tot.values.max\n\nlst.reject! { |elem| tot[elem[0]] < m}\n#puts lst\n \ntot = {}\nfor name,score in lst do\n tot[name] ||= 0\n tot[name] += score\n if tot[name] >= m\n puts name\n break\n end\nend\n"}, {"source_code": "n = gets.to_i\nh = []\nn.times do\n name, score = gets.split\n score = score.to_i\n h << [name, score]\nend\n\na = {}\nh.each do |con|\n a[con[0]] ||= 0\n a[con[0]] += con[1]\nend\n\nm = a.max {|x,y| x[1] <=> y[1]}[1]\n\nb = Hash.new(0)\nh.each do |con|\n b[con[0]] += con[1]\n if a[con[0]] == m && b[con[0]] >=m\n puts con[0]\n exit\n end\nend"}, {"source_code": "a={}\nc={}\nz={}\nl=[]\ngets.to_i.times{\n\tq,w=gets.split\n\ta[q]=a[q].to_i+w.to_i;\n\tl+=[[q,w]]\n}\nm=a.values.max\nl.each{|q,w|\n\tc[q]=c[q].to_i+w.to_i;\n\tif c[q]>=m and a[q]==m then\n\t\tputs q\n\t\texit\n\tend\n}\n\n"}, {"source_code": "n = gets.to_i\n\nm = Array.new\n\na = {}\n\nn.times do \n\tk = gets.chomp.split(\" \")\n\tkey = k[0]\n\tvalue = k[1].to_i\n\tif a[ key ].nil? then\n\t\ta[ key ] = value\n\telse\n\t\ta[ key ] += value\n\tend\n\tm << k\nend\n\n\nmax = a.values.max\n\nb = {}\n\nm.each{ |s|\n\tkey = s[0]\n\tvalue = s[1].to_i\n\tif b[ key ].nil? then\n\t\tb[ key ] = value\n\telse\n\t\tb[ key ] += value\n\tend\n\tif a[ key ] == max and b [ key ] >= max then\n\t\tputs key\n\t\tbreak\n\tend\t\n}"}, {"source_code": "log = []\nscores = Hash.new(0)\ncheck = Hash.new(0)\ngets.to_i.times{\n n,q=gets.split\n scores[n]+=q.to_i\n log << [q,n]\n}\nm = scores.values.max\nlog.each{|q,n|\n check[n] += q.to_i\n if check[n] >= m and scores[n] == m then\n puts n\n exit\n end\n}\n"}, {"source_code": " \na={}\nc={}\nz={}\nl=[]\ngets.to_i.times{\n\tq,w=gets.split\n\ta[q]=a[q].to_i+w.to_i;\n\tl+=[[q,w]]\n}\nm=a.values.max\nl.each{|q,w|\n\tc[q]=c[q].to_i+w.to_i;\n\tif c[q]>=m and a[q]==m then\n\t\tputs q\n\t\texit\n\tend\n}"}, {"source_code": "a={}\nc={}\nz={}\nl=[]\ngets.to_i.times{\n\tq,w=gets.split\n\ta[q]=a[q].to_i+w.to_i;\n\tl+=[[q,w]]\n}\nm=a.values.max\nl.each{|q,w|\n\tc[q]=c[q].to_i+w.to_i;\n\tif c[q]>=m and a[q]==m then\n\t\tputs q\n\t\texit\n\tend\n}\n\n"}, {"source_code": "players = Hash.new(0)\ngames = []\ngets.to_i.times do |i|\n name, point = gets.chomp.split\n point = point.to_i\n games << [name, point]\n players[name] += point\nend\nsorted_players = players.sort{|a, b| b[1] <=> a[1]}\nmax = sorted_players[0][1]\nmax_players = {}\nsorted_players.each do |s|\n break if s[1] < max\n max_players[s[0]] = 0\nend\n\ngames.each do |game|\n if max_players.has_key?(game[0])\n max_players[game[0]] += game[1]\n if max_players[game[0]] >= max\n puts game[0]\n exit\n end\n end\nend\n"}, {"source_code": "a={}\nc={}\nz={}\nl=[]\ngets.to_i.times{\n\tq,w=gets.split\n\ta[q]=a[q].to_i+w.to_i;\n\tl+=[[q,w]]\n}\nm=a.values.max\nl.each{|q,w|\n\tc[q]=c[q].to_i+w.to_i;\n\tif c[q]>=m and a[q]==m then\n\t\tputs q\n\t\texit\n\tend\n}\n\n"}, {"source_code": "n = gets.chomp.to_i\nh = Hash.new\nas = Array.new\nfor i in 1..n\n\ts = gets.chomp\n\tas.push( s )\nend\nfor i in 1..n\n\tnm,sc = as[i-1].split(/ /)\n\tsc = sc.to_i\n\tsc += h[nm] if h.has_key?(nm)\n\th[nm] = sc\nend\nhv = h.to_a.sort {|a,b|\n\tb[1] <=> a[1]\n}\n\nif hv.length == 1 || hv[0][1] > hv[1][1]\n\tputs hv[0][0]\nelse\n\thnm = Hash.new\n\tfor i in 0..hv.length-1\n\t\tbreak if hv[0][1] > hv[i][1]\n\t\thnm[hv[i][0]] = 1\n\tend\n\t\n\th.clear\n\tfor i in 1..n\n\t\tnm,sc = as[i-1].split(/ /)\n\t\tsc = sc.to_i\n\t\tsc += h[nm] if h.has_key?(nm)\n\t\th[nm] = sc\n\t\tif sc >= hv[0][1] && hnm.has_key?(nm)\n\t\t\tputs nm\n\t\t\texit\n\t\tend\n\tend\nend\n"}, {"source_code": "n=gets.to_i\nv=[]\nn.times do\n\ts,w=gets.split\n\tv << [s,w.to_i]\nend\na=Hash.new(0)\nv.each{|s,w|a[s]+=w}\nm=a.max{|x,y|x[1]<=>y[1]}[1]\nf=Hash.new(0)\nv.each{|s,w|\n\tf[s]+=w\n\tif a[s]==m and f[s]>=m then\n\t\tputs s\n\t\texit\n\tend\n}\n"}, {"source_code": "table = {}\nlined = []\nARGF.each_line do |line|\n if ARGF.lineno != 1\n name, mark = line.split\n if !table.has_key?(name)\n table[name] = [mark.to_i]\n else\n table[name] += [table[name].last + mark.to_i]\n end\n lined.push({:name => name, :mark => table[name].last})\n end\nend\n\nmaxNames = []\nmax = 0\n\ntable.keys.each do |name|\n if max < table[name].last\n maxNames = [name]\n max = table[name].last\n elsif max == table[name].last\n maxNames.push name\n end\nend\n\nif maxNames.length == 1\n puts maxNames[0]\nelse\n lined.each do |val|\n if val[:mark] >= max and maxNames.include? val[:name]\n puts val[:name]\n break\n end\n end\nend\n"}, {"source_code": "#!/usr/bin/ruby\nn=gets.chomp.to_i\nh=Hash.new;h.default=0\na=Array.new\nn.times{|z|\n s=gets.chomp.split(\" \")\n s[1]=s[1].to_i\n h[s[0]]+=s[1]\n a.push(s)\n}\nm=h.values.max\nh.each_key{|k| h[k]=(h[k]==m)?0:nil}\n\n#re-read\na.each{|i|\n if h[i[0]]\n h[i[0]]+=i[1]\n if h[i[0]]>=m then puts i[0]; exit end\n end\n}\n"}, {"source_code": "n = gets.to_i\nr = []\nn.times do\n\tw, s = gets.split\n\tr << [w, s.to_i]\nend\n\na = Hash.new(0)\nr.each do |w,s|\n\ta[w] += s\nend\nm = a.max{|x,y| x[1] <=> y[1] }[1]\n\nb = Hash.new(0)\nr.each do |w,s|\n\tb[w] += s\n\tif a[w] == m && b[w] >= m then\n\t\tputs w\n\t\texit\n\tend\nend\n"}, {"source_code": "n = gets.to_i\nary = []\nhash = Hash.new 0\nn.times do\n\tlist = gets.split\n\tname = list[0]\n\tscore = list[1].to_i\n\thash.has_key?(name) ? hash[name] += score : hash[name] = score\n\tary << [name, hash[name]]\nend\nmaximum = hash.values.max\ncandidates = hash.select {|key, value| value == maximum}\nary.each do |entry|\n\tif candidates.key?(entry[0]) && entry[1] >= maximum\n\t\tputs entry[0]\n\t\tbreak\n\tend\nend"}, {"source_code": "h = {}\nhh = {}\nfor name,score in $<.read.scan(/(\\S+) (\\S+)/)\n t = h[name] = (h[name]||0)+score.to_i\n for i in 0..t\n hh[i] = (hh[i]||[])+[name]\n end\nend\nmax = h.values.max\nputs hh[max].find{|x|h[x]==max}\n"}, {"source_code": "n=STDIN.gets.chomp.to_i\nplayers={}\nn.times {|i|\n player,score=STDIN.gets.chomp.split(' ')\n if players.key?(player)\n scores=players[player]\n players[player]<<[score.to_i+scores[scores.length-1][0],i]\n else\n players[player]=[[score.to_i,i]]\n end\n}\nmax=0\nwinners=[]\nplayers.each_pair {|player,scores|\n score=scores[scores.length-1][0]\n if max=m and A[q]==m then\n puts q\n exit\nend\n}"}, {"source_code": "# Codeforces, Problem 2A\n# http://codeforces.com/contest/1/problem/C\n# Nate Collings\n\ndef get_names_scores(num)\n names = []\n scores = []\n num.times do\n player, score = gets.chomp.split(\"\\s\")\n names << player\n scores << score.to_i\n end\n return names, scores\nend\n\ndef get_max(players)\n max = players.values.map { |v| v }.max\n with_max = players.select { |_,v| v == max}\n return max, with_max\nend\n\ndef run_round(rounds, names, scores, players=nil, second=false)\n max, with_max = get_max(players) if second\n players = {}\n rounds.times do |round|\n player = names[round]\n score = scores[round]\n if !second || with_max.has_key?(player)\n score += players[player] if players.has_key?(player)\n players[player] = score\n return player if second && score >= max\n end\n end\n players\nend\n\ndef first_round(rounds, names, scores)\n run_round(rounds, names, scores)\nend\n\ndef second_round(rounds, names, scores, players)\n run_round(rounds, names, scores, players, true)\nend\n\ndef solve\n rounds = gets.chomp.to_i\n names, scores = get_names_scores(rounds)\n\n candidates = first_round(rounds, names, scores)\n return candidates.keys[0] if candidates.length == 1\n return second_round(rounds, names, scores, candidates)\nend\n\nputs solve\n"}], "negative_code": [{"source_code": "log = []\nscores = Hash.new(0)\ngets.to_i.times{\n n,q=gets.split\n scores[n]+=q.to_i\n log << [q,n]\n}\nm = scores.values.max\nlog.each{|q,n|\n if q.to_i>=m then\n p n\n exit\n end\n} \n \n"}, {"source_code": "n = gets.to_i\na = Hash.new([0])\nn.times do |_|\n\tw, s = gets.split\n\ta[w] = a[w][0] + s.to_i, -_\nend\nputs a.max{|a,b| a[1] <=> b[1] }[0]\n"}, {"source_code": "n = gets.to_i\nh = Hash.new\nmax = 0\nname = ''\n\nfor i in 1..n\n s, k = gets.chomp.split\n k = k.to_i\n if h[s].nil?\n h[s] = k\n else\n h[s] += k\n end\n if h[s] > max\n max = h[s]\n name = s\n end\nend\n\nputs name"}, {"source_code": "\ndef process(m, ss)\n max = m.values.max\n maxes = m.select {|k, v| v == max}\n\n res = nil\n idx = 1001\n\n if maxes.size > 1\n ss.each do |k, v|\n sum = 0\n v.each do |vv|\n sum += vv.score\n if sum >= max || vv.index > idx\n if res == nil || vv.index < idx\n res = k\n idx = vv.index\n end\n break\n end\n end\n end\n return res\n end\n\n maxes.keys[0]\nend\n\nnum = STDIN.gets.chomp.to_i\n\nS = Struct.new(:index, :score)\nm = {}\ns = {}\n\nn = 0\nwhile n < num\n n += 1\n line = STDIN.gets\n line.chomp!\n name, score = line.split\n score = score.to_i\n if m.include? name\n m[name] = m[name] + score\n else\n m[name] = score\n end\n if s.include? name\n s[name] << S.new(n, score)\n else\n s[name] = [S.new(n, score)]\n end\nend\n\nprint process(m, s)"}, {"source_code": "n = gets.to_i\nr = []\nfor i in 0 ... n \n a,b = gets.split\n r << [a, b.to_i]\nend\n\nh1 = Hash.new{0}\nr.each do |name,val|\n h1[name] += val\nend\n\nmaxi = r.max{|x,y| x[1] <=> y[1]}[1]\n\nh2 = Hash.new{0}\nr.each do |name,val|\n h2[name] += val\n if (h1[name] == maxi && h2[name] >= maxi)\n puts name\n break\n end\nend\n"}, {"source_code": "log = []\nscores = Hash.new(0)\ncheck = Hash.new(0)\ngets.to_i.times{\n n,q=gets.split\n scores[n]+=q.to_i\n log << [q,n]\n}\nm = scores.values.max\nlog.each{|q,n|\n check[n]+=q.to_i\n if check[n] >= m\n print n\n exit\n end\n} \n \n"}, {"source_code": "def getIndex(val, h)\n if (h.key?(val))\n return h[val]\n end\n index = h.size\n return h[val] = index\nend\n\nn = gets.to_i\nh = {}\nlast = []\nsum = []\n\nfor i in 0 ... n\n name,val = gets.split\n index = getIndex(name,h)\n val = val.to_i \n if (index < sum.size)\n sum[index] += val\n last[index] = i\n else\n sum << val\n last << i\n end\nend\n\nans = \"\"\nmaxi = sum.max\nfastest = n\nh.each do |key,index|\n if (sum[index] == maxi && last[index] < fastest)\n fastest = last[index]\n ans = key\n end\nend\nputs ans\n"}, {"source_code": "scores = {}\ngets.to_i.times do |i|\n name, score = gets.split\n score = score.to_i\n if scores.has_key? name\n if scores[name][2] < scores[name][0]+score\n scores[name][1] = i\n scores[name][2] = scores[name][0]+score\n end\n scores[name][0] += score\n else\n scores[name] = [score, i, score]\n end\nend\n\nscores = scores.to_a.sort_by! { |key, value| -value[0] }\nmax_value = scores[0]\n#puts scores.inspect\nscores.each do |v|\n if v[1][0] mx\n ans = s[0]\n mx = h[s[0]]\n end\nend\nputs ans\n"}, {"source_code": "n = gets.to_i\nh = {}\nans = '', mx = -1e9\nn.times do\n s = gets.split(' ')\n h[s[0]] = 0 if (h.has_key?(s[0]) == false)\n h[s[0]] += s[1].to_i\n if h[s[0]] > mx\n ans = s[0]\n mx = h[s[0]]\n end\nend\nputs ans\n"}, {"source_code": "n = STDIN.readline.to_i\nscore = Hash.new\nt = []\n\nn.times do |i|\n\tt << STDIN.readline.split\n\t# print STDIN.readline.split\n\t# print t\n\tt[i][1] = t[i][1].to_i\n\tif score[t[i][0]] == nil ;\n\t\tscore[t[i][0]] = 0\n\tend\n\tscore[t[i][0]] += t[i][1]\nend\n\nmaxscore = 0\nscore.each_value do |i|\n\tmaxscore = i if i > maxscore\nend\n\nscore.clear\nans = \"\"\nn.times do |i|\n\tif score[t[i][0]] == nil ;\n\t\tscore[t[i][0]] = 0\n\tend\n\tscore[t[i][0]] += t[i][1]\n\tans = t[i][0]; break if score[t[i][0]] >= maxscore\nend\nprint ans\n"}, {"source_code": "log = []\nscores = Hash.new(0)\ncheck = Hash.new(0)\ngets.to_i.times{\n n,q=gets.split\n scores[n]+=q.to_i\n log << [q,n]\n}\nm = scores.values.max\nlog.each{|q,n|\n check[n] += q.to_i\n if check[n] >= m and scores[n] == q.to_i then\n puts n\n exit\n end\n}\n"}, {"source_code": "\ndef process(m, ss)\n max = m.values.max\n maxes = m.select {|k, v| v == max}\n\n res = nil\n idx = 1001\n\n if maxes.size > 1\n ss.each do |k, v|\n sum = 0\n v.each do |vv|\n sum += vv.score\n if sum >= max || vv.index > idx\n if res == nil || vv.index < idx\n res = k\n idx = vv.index\n end\n break\n end\n end\n end\n return res\n end\n\n maxes.keys[0]\nend\n\nnum = STDIN.gets.chomp.to_i\n\nS = Struct.new(:index, :score)\nm = {}\ns = {}\n\nn = 0\nwhile n < num\n n += 1\n line = STDIN.gets\n line.chomp!\n name, score = line.split\n score = score.to_i\n if m.include? name\n m[name] = m[name] + score\n else\n m[name] = score\n end\n if s.include? name\n s[name] << S.new(n, score)\n else\n s[name] = [S.new(n, score)]\n end\nend\n\nprint process(m, s)"}, {"source_code": "N = gets.to_i\nh = Hash.new { |hash, key| hash[key] = [0, 10000, 0] }\nN.times do |i| \n n, p = gets.chomp.split\n p = p.to_i\n h[n][0] += p\n h[n][1] = i if p > 0\n h[n][2] += p if p > 0\nend\ns = h.sort { |a, b| b[1][0] <=> a[1][0]}\nu = s.select {|x| x[1][0] == s[0][1][0] && x[1][2] >= s[0][1][0] }\nu.sort! do |a, b|\n a[1][1] <=> b[1][1]\nend\nputs u[0][0]\n"}, {"source_code": "# Codeforces, Problem 2A\n# http://codeforces.com/contest/1/problem/C\n# Nate Collings\n\nPlayer = Struct.new(:score, :round)\n\nplayers = {}\nrounds = gets.chomp.to_i\n\nrounds.times do |round|\n player, score = gets.chomp.split(\"\\s\")\n score = score.to_i\n if players.has_key?(player)\n players[player].score += score\n players[player].round = round\n else\n players[player] = Player.new(score, round)\n end\nend\n\nmax = players.values.map { |v| v.score }.max\nwith_max = players.select { |_,v| v.score == max }\nwinner = with_max.min_by { |_,v| v.round }[0]\n\nputs winner"}, {"source_code": "num_lines = gets().chomp.to_i\nplayers_info = {}\nnum_lines.times{\n |i|\n name, score = gets().chomp.split(/[ ]+/)\n score = score.to_i\n if(players_info.has_key?(name)) then\n players_info[name][0] += score\n players_info[name][1] = i\n else\n players_info[name] = []\n players_info[name][0] = score\n players_info[name][1] = i\n end\n}\n\nputs players_info.to_a.sort {\n |a,b|\n (((b[1][0] <=> a[1][0]) * 2 + (a[1][1] <=> b[1][1])) <=> 0)\n}[0][0]\n"}, {"source_code": "n = gets.to_i\n\nh = Hash.new\nins = Array.new\nn.times do\n ins.push(gets)\n s = ins[-1].split(' ')\n h[s[0]] = 0 if (h.has_key?(s[0]) == false)\n h[s[0]] += s[1].to_i\nend\nmx = h.values.max\nb = h.select {|k, v| v == mx}\n\nh.clear()\nins.each do |s|\n s = s.split(' ')\n h[s[0]] = 0 if (h.has_key?(s[0]) == false)\n h[s[0]] += s[1].to_i\n if h[s[0]] == mx and b.has_key?(s[0])\n puts s[0]\n break\n end\nend\n"}, {"source_code": "log = []\nscores = Hash.new(0)\ngets.to_i.times{\n n,q=gets.split\n scores[n]+=q.to_i\n log << [q,n]\n}\nm = scores.values.max\nlog.each{|q,n|\n if q.to_i>=m then\n p n\n exit\n end\n} \n \n"}, {"source_code": "#!/usr/bin/env ruby\n\nn = gets.chomp!.to_i\nscores = {}\nscores['max-scorer'] = nil\nn.times do\n name, round = gets.chomp!.split(\" \")\n max_score = scores[scores['max-scorer']].to_i\n curr_score = round.to_i + scores[name].to_i\n scores[name] = curr_score\n scores['max-scorer'] = name if (curr_score > max_score)\nend\n\nputs scores['max-scorer']\n"}, {"source_code": "#!/usr/bin/env ruby\n\nh = Hash.new(0)\nt = gets.to_i\nm = -1\nwinner = \"\"\n\nfor i in 0...t\n\n s = gets.split(' ')\n\n a = s[0]\n b = s[1].to_i\n\n h[a] += b\n\n if h[a] > m\n m = h[a]\n winner = a\n end\n\nend\n\nputs winner\n\n \n"}, {"source_code": "n = gets.to_i\nans = Hash.new\nfor i in 0...n do\n name, point = gets.split\n name = name.to_sym\n point = point.to_i\n\n if ans[name].nil?\n ans[name] = point, i \n else\n ans[name][0] = ans[name][0] + point\n end\n\n ans[name][1] = i\nend\n\nans = ans.to_a\nans.sort! { |a, b| b[1][0] <=> a[1][0] }\n\ni = 0\nn = ans.size - 1\nfinal = ans[0]\nfinal_next = final\n\nwhile i < n && final_next[1][0] == final[1][0]\n if final_next[1][1] < final[1][1]\n final = final_next\n final_next = ans[i + 1]\n else\n final_next = ans[i + 1]\n end\n i = i + 1\nend\n\nif final_next[1][1] < final[1][1] && final_next[1][0] == final[1][0]\n final = final_next\nend\n\np final[0].to_s"}, {"source_code": "table = {}\nlined = []\nARGF.each_line do |line|\n if ARGF.lineno != 1\n name, mark = line.split\n if !table.has_key?(name)\n table[name] = [mark.to_i]\n else\n table[name] += [table[name].last + mark.to_i]\n end\n lined.push({:name => name, :mark => table[name].last})\n end\nend\n\nmaxNames = []\nmax = 0\n\ntable.keys.each do |name|\n if max < table[name].last\n maxNames = [name]\n max = table[name].last\n elsif max = table[name].last\n maxNames.push name\n end\nend\n\nif maxNames.length == 1\n puts maxNames[0]\nelse\n lined.each do |val|\n if val[:mark] >= max\n puts val[:name]\n break\n end\n end\nend\n"}, {"source_code": "n = gets.to_i\nans = Hash.new\nrec = Array.new\nfor i in 0...n do\n name, point = gets.split\n name = name.to_sym\n point = point.to_i\n\n if ans[name].nil?\n ans[name] = point\n else\n ans[name] = ans[name] + point\n end\n\n rec[i] = name, ans[name]\nend\n\nans = ans.to_a\nmax_num = ans.max { |a, b| a[1] <=> b[1] }[1]\nans_index = 0\n\nfor r in rec do\n if r[1] >= max_num\n ans_index = r[0]\n break\n end\nend\n\nputs ans_index"}, {"source_code": "N = gets.to_i\nh = Hash.new { |hash, key| hash[key] = [0, 10000, 0] }\nN.times do |i| \n n, p = gets.chomp.split\n p = p.to_i\n h[n][0] += p\n h[n][1] = i\n h[n][2] += p if p > 0\nend\ns = h.sort { |a, b| b[1][0] <=> a[1][0]}\nu = s.select {|x| x[1][0] == s[0][1][0] && x[1][2] >= s[0][1][0] }\nu.sort! { |a, b| a[1][1] <=> b[1][1] }\nputs u[0][0]\n"}, {"source_code": "n = gets.chomp.to_i\nh = Hash.new\nas = Array.new\nfor i in 1..n\n\ts = gets.chomp\n\tas.push( s )\nend\nfor i in 1..n\n\tnm,sc = as[i-1].split(/ /)\n\tsc = sc.to_i\n\tsc += h[nm] if h.has_key?(nm)\n\th[nm] = sc\nend\nhv = h.to_a.sort {|a,b|\n\tb[1] <=> a[1]\n}\n\nif hv.length == 1 || hv[0][1] > hv[1][1]\n\tputs hv[0][0]\nelse\n\th.clear\n\tfor i in 1..n\n\t\tnm,sc = as[i-1].split(/ /)\n\t\tsc = sc.to_i\n\t\tsc += h[nm] if h.has_key?(nm)\n\t\th[nm] = sc\n\t\tif sc >= hv[0][1]\n\t\t\tputs nm\n\t\t\texit\n\t\tend\n\tend\nend\n"}, {"source_code": "#!/usr/bin/env ruby\n\n$global_hash = Hash.new\n$max_score = 0\n$leader = \"\"\n\ndef process_score(score_string)\n\tparts = score_string.split(' ')\n\tkey = parts[0]\n\tvalue = parts[1].to_i\n\n\tif ($global_hash.has_key? key)\n\t\t$global_hash[key] += value\n\telse\n\t\t$global_hash[key] = value\n\tend\n\n\tif ($global_hash[key] > $max_score)\n\t\t$max_score = $global_hash[key]\n\t\t$leader = key\n\tend\nend\n\n#results for testing\n# match_results = []\n# match_results << \"andrew 3\"\n# match_results << \"andrew 2\"\n# match_results << \"mike 5\"\n\nrepeats = gets.to_i\nmatch_results = []\n\nwhile (repeats > 0)\n\tmatch_results << gets.strip\n\trepeats -= 1\nend\n\nfor score_string in match_results\n\tprocess_score(score_string)\nend\n\nputs $leader"}, {"source_code": "n=gets.chomp!.to_i\nmemo=Hash.new\nscore=[]\nn.times do\n score << gets.chomp\nend\nn.times do |j|\n tmp=score[j].split\n if memo.key?(tmp[0])\n memo[tmp[0]]+=tmp[1].to_i\n else\n memo[tmp[0]]=tmp[1].to_i\n end\nend\nmax=memo.values.max\nmemo.clear()\nn.times do |j|\n tmp=score[j].split\n if memo.key?(tmp[0])\n memo[tmp[0]]+=tmp[1].to_i\n else\n memo[tmp[0]]=tmp[1].to_i\n end\n if memo[tmp[0]]>=max\n puts tmp[0]\n break\n end\nend\n"}, {"source_code": "# frozen_string_literal: true\n\nlines = gets.to_i\n\nscore_map = {}\nat_index_by_name = {}\n1.upto(lines) do |i|\n name, score = gets.split\n score = score.to_i\n\n score_map[name] ||= 0\n score_map[name] += score\n\n at_index_by_name[name] ||= {\n score: -99_999_999,\n index: 9_999_999\n }\n\n next unless at_index_by_name[name][:score] < score_map[name]\n\n at_index_by_name[name] = {\n score: score_map[name],\n index: i\n }\nend\n\nscores = score_map.to_a.sort_by(&:last).reverse\nputs(\n scores\n .select { |score| scores.last.last == score.last }\n .min_by { |score| at_index_by_name[score.first][:index] }\n .first\n)\n"}, {"source_code": "# frozen_string_literal: true\n\nlines = gets.to_i\n\nscore_map = {}\nat_index_by_name = {}\n1.upto(lines) do |i|\n name, score = gets.split\n score = score.to_i\n\n score_map[name] ||= 0\n score_map[name] += score\n\n at_index_by_name[name] ||= {}\n\n next if at_index_by_name[name][score_map[name]]\n\n at_index_by_name[name][score_map[name]] = i\nend\n\nscores = score_map.to_a.sort_by(&:last)\nputs(\n scores\n .select { |score| scores.last.last == score.last }\n .min_by { |score| at_index_by_name[score.first][score.last] }\n .first\n)\n"}, {"source_code": "# Codeforces, Problem 2A\n# http://codeforces.com/contest/1/problem/C\n# Nate Collings\n\nplayers = {}\nmax = 0\nwinner = nil\nrounds = gets.chomp.to_i\n\nrounds.times do\n player, score = gets.chomp.split(\"\\s\")\n score = score.to_i\n players[player] = players.has_key?(player) ? players[player] += score : score\n if players[player] > max\n winner = player\n max = players[player]\n end\nend\n\nputs winner"}, {"source_code": "#!/usr/bin/env ruby\n\nn = gets.chomp!.to_i\nscores = {}\nscores['max-scorer'] = nil\nn.times do\n name, round = gets.chomp!.split(\" \")\n max_score = scores[scores['max-scorer']].to_i\n curr_score = round.to_i + scores[name].to_i\n scores[name] = curr_score\n scores['max-scorer'] = name if (curr_score > max_score)\nend\n\nputs scores['max-scorer']\n"}, {"source_code": "#!/usr/bin/env ruby\n\nh = Hash.new(0)\nt = gets.to_i\nm = -1\nwinner = \"\"\n\nfor i in 0...t\n\n s = gets.split(' ')\n\n a = s[0]\n b = s[1].to_i\n\n h[a] += b\n\n if h[a] > m\n m = h[a]\n winner = a\n end\n\nend\n\nputs winner\n\n \n"}, {"source_code": "#=================================\n# Better be sure than sorry. \n#=================================\n$stdin=File.open(\"./input.txt\",\"r\") if ENV[\"LOGNAME\"]==\"skydos\"\n#=================================\n\nn=gets.to_i\n\ndata = []\nhs = Hash.new(0)\n\nn.times{\n\tname, points = gets.split\t\n\t\n\tdata << [name, points.to_i]\n\ths[name]+=points.to_i\n}\n\nmax_points = hs.values.max\n\nhs2 = Hash.new(0)\ndata.each{|name, points|\n\ths2[name]+=points\n\t\n\tif hs2[name]==max_points\n\t\tputs name\n\t\tbreak\n\tend\n}\n"}, {"source_code": "num_lines = gets().chomp.to_i\nplayers_info = {}\nnum_lines.times{\n |i|\n name, score = gets().chomp.split(/[ ]+/)\n score = score.to_i\n if(players_info.has_key?(name)) then\n if(score != 0) then\n players_info[name][0] += score\n players_info[name][1] = i\n end\n else\n players_info[name] = []\n players_info[name][0] = score\n players_info[name][1] = i\n end\n}\n\nputs players_info.to_a.sort {\n |a,b|\n (((b[1][0] <=> a[1][0]) * 3 + (a[1][1] <=> b[1][1])) <=> 0)\n}[0][0]\n"}, {"source_code": "input = gets.chomp.to_i\nlines=Array.new\nplayers = Hash.new\nwinner = 0\nwon=''\ninput.times do\n lines.push gets.chomp\n player , points = lines.at(lines.length-1).split(\" \")\n players[player] = players[player].to_i + points.to_i\nend\nplayers.each do |k,v|\n winner = v if v > winner\nend\nanswers = players.select do |k ,v|\n v==winner\nend\nplayers=Hash.new\nlines.each do|i|\n player , points = i.split(\" \")\n players[player] = players[player].to_i + points.to_i\n if winner == players[player] and answers.key? player\n won=player\n break\n end\nend\nputs won"}, {"source_code": "n=gets.chomp!.to_i\nmemo=Hash.new\npremax=-1111\nmax=-1000\nname=\"\"\nn.times do \n tmp=gets.chomp!.split()\n if memo.key?(tmp[0])\n memo[tmp[0]]+=tmp[1].to_i\n else\n memo[tmp[0]]=tmp[1].to_i\n end\n max=-1000\n tmpname=\"\"\n memo.each do |key,value|\n if max= 0\n\t\t\t$players_scores[key][:index] = $operation_index\n\t\telse\n\t\t\t$players_scores[key][:index] = 0\n\t\tend\n\tend\n\n\t$operation_index += 1\nend\n\n# results for testing\n# match_results = []\n# match_results << \"andrew 3\"\n# match_results << \"andrew 2\"\n# match_results << \"andrew 1\"\n# match_results << \"mike 5\"\n# match_results << \"andrew -1\"\n# match_results << \"john 5\"\n\n# match_results << \"andrew 3\"\n# match_results << \"andrew 2\"\n# match_results << \"mike 5\"\n\nrepeats = gets.to_i\nmatch_results = []\n\nwhile (repeats > 0)\n\tmatch_results << gets.strip\n\trepeats -= 1\nend\n\nfor score_string in match_results\n\tprocess_score(score_string)\nend\n\nplayers = $players_scores.values\nplayers.sort! { |a,b| a[:score] == b [:score] ? a[:index] <=> b[:index] : b[:score] <=> a[:score]}\n\nputs $players_scores.invert[players[0]]"}, {"source_code": "# frozen_string_literal: true\n\nlines = gets.to_i\n\nscore_map = {}\nat_index_by_name = {}\n1.upto(lines) do |i|\n name, score = gets.split\n score = score.to_i\n\n score_map[name] ||= 0\n score_map[name] += score\n\n at_index_by_name[name] ||= {\n score: score_map[name],\n index: i\n }\n\n next unless at_index_by_name[name][:score] < score_map[name]\n\n at_index_by_name[name] = {\n score: score_map[name],\n index: i\n }\nend\n\nscores = score_map.to_a.sort_by(&:last)\nputs(\n scores\n .select { |score| scores.last.last == score.last }\n .min_by { |score| at_index_by_name[score.first][:index] }\n .first\n)\n"}, {"source_code": "# frozen_string_literal: true\n\nlines = gets.to_i\n\nscore_map = {}\nat_index_by_name = {}\n1.upto(lines) do |i|\n name, score = gets.split\n score = score.to_i\n\n score_map[name] ||= 0\n score_map[name] += score\n\n at_index_by_name[name] ||= {}\n\n next if at_index_by_name[name][score_map[name]]\n\n at_index_by_name[name][score_map[name]] = i\nend\n\nscores = score_map.to_a.sort_by(&:last)\nputs(\n scores\n .select { |score| scores.last.last == score.last }\n .min_by { |score| at_index_by_name[score.first][score.last] }\n .first\n)\n"}, {"source_code": "#=================================\n# Better be sure than sorry. \n#=================================\n$stdin=File.open(\"./input.txt\",\"r\") if ENV[\"LOGNAME\"]==\"skydos\"\n#=================================\n\nn=gets.to_i\n\ndata = []\nhs = Hash.new(0)\n\nn.times{\n\tname, points = gets.split\t\n\t\n\tdata << [name, points.to_i]\n\ths[name]+=points.to_i\n}\n\nmax_points = hs.values.max\n\nhs2 = Hash.new(0)\ndata.each{|name, points|\n\ths2[name]+=points\n\t\n\tif hs2[name]==max_points\n\t\tputs name\n\t\tbreak\n\tend\n}\n"}, {"source_code": "def process(m, ss)\n max = m.values.max\n maxes = m.select {|k, v| v == max}\n\n while maxes.size > 1 && !ss.empty?\n s = ss.pop()\n if s.score > 0 && maxes.include?(s.name)\n maxes.delete s.name\n end\n end\n\n maxes.keys()[0]\nend\n\nnum = STDIN.gets.chomp.to_i\n\nS = Struct.new(:name, :score)\nm = {}\ns = []\n\nn = 0\nwhile n < num\n n += 1\n line = STDIN.gets\n line.chomp!\n name, score = line.split\n score = score.to_i\n if m.include? name\n m[name] = m[name] + score\n else\n m[name] = score\n end\n s << S.new(name, score)\nend\n\nprint process(m, s)"}, {"source_code": "# coding: utf-8\n\nn = gets.to_i\nh = {}\nst = []\nn.times {\n name, score = gets.split\n score = score.to_i\n h[name] ||= 0\n h[name] += score if score >= 0\n st << [name, score]\n}\nmax = h.values.max\ncands = []\nfor k,v in h\n if v == max\n cands << k\n end\nend\nif cands.length == 1\n puts cands[0]\nelse\n h2 = {}\n for s in st\n h2[s[0]] ||= 0\n h2[s[0]] += s[1] if s[1] >= 0\n if h2[s[0]] >= max\n puts s[0]\n break\n end\n end\nend"}, {"source_code": "# frozen_string_literal: true\n\nlines = gets.to_i\n\nscore_map = {}\nat_index_by_name = {}\n1.upto(lines) do |i|\n name, score = gets.split\n score = score.to_i\n\n score_map[name] ||= 0\n score_map[name] += score\n\n at_index_by_name[name] ||= {\n score: score_map[name],\n index: i\n }\n\n next unless at_index_by_name[name][:score] < score_map[name]\n\n at_index_by_name[name] = {\n score: score_map[name],\n index: i\n }\nend\n\nscores = score_map.to_a.sort_by(&:last)\nputs(\n scores\n .select { |score| scores.last.last == score.last }\n .min_by { |score| at_index_by_name[score.first][:index] }\n .first\n)\n"}, {"source_code": "# frozen_string_literal: true\n\nlines = gets.to_i\n\nscore_map = {}\nat_index_by_name = {}\n1.upto(lines) do |i|\n name, score = gets.split\n score = score.to_i\n\n score_map[name] ||= 0\n score_map[name] += score\n\n at_index_by_name[name] ||= {\n score: 0,\n index: 0\n }\n\n next unless at_index_by_name[name][:score] <= score_map[name]\n\n at_index_by_name[name] = {\n score: score_map[name],\n index: i\n }\nend\n\nscores = score_map.sort_by(&:last)\nputs(\n scores\n .select { |score| scores.last.last == score.last }\n .min_by { |score| at_index_by_name[score.first][:index] }\n .first\n)\n"}, {"source_code": "num_lines = gets().chomp.to_i\nplayers_info = {}\nnum_lines.times{\n |i|\n name, score = gets().chomp.split(/[ ]+/)\n score = score.to_i\n if(players_info.has_key?(name)) then\n players_info[name][0] += score\n if(score > 0) then players_info[name][1] = i end\n else\n players_info[name] = []\n players_info[name][0] = score\n players_info[name][1] = i\n end\n}\n\nputs players_info.to_a.sort {\n |a,b|\n (((b[1][0] <=> a[1][0]) * 2 + (a[1][1] <=> b[1][1])) <=> 0)\n}[0][0]\n"}, {"source_code": "n = gets.to_i\nh = Hash.new(0)\np = ''\nn.times do\n name, score = gets.split\n score = score.to_i\n h[name] += score\n p = name if h[name] > h[p]\nend\nmx = h.max_by{|name, s| s}\nputs mx[1] == h[p] ? p : mx[0]\n"}, {"source_code": "h = {}\nn = gets.to_i\nn.times { |i|\n a, b = gets.split\n if h.key? a\n h[a] = [h[a][0] + b.to_i, h[a][0] + b.to_i > h[a][2] ? i : h[a][1], h[a][0] + b.to_i > h[a][2] ? h[a][0] + b.to_i : h[a][2] ]\n else\n h[a] = [b.to_i, i, b.to_i]\n end\n}\n\nmax2 = -1000 * n - 1\nmax1 = n\nindex = ''\n\nh.each do |a, b|\n if b[2] > max2\n max2 = b[2]\n max1 = b[1]\n index = a\n elsif b[2] == max2 and b[1] < max1\n max1 = b[1]\n index = a\n end\nend\n\nprint index\n"}, {"source_code": "def getIndex(val, h)\n if (h.key?(val))\n return h[val]\n end\n index = h.size\n return h[val] = index\nend\n\nn = gets.to_i\nh = {}\ninput = []\nsum = []\ninputLog = []\n\nfor i in 0 ... n\n name,val = gets.split\n index = getIndex(name,h)\n val = val.to_i \n input << val\n if (index < sum.size)\n sum[index] += val\n else\n sum << val\n inputLog << []\n end\n inputLog[index] << i\nend\n\nans = \"\"\nmaxi = sum.max\nfastest = n\nh.each do |key,index|\n if (sum[index] == maxi)\n sum,tmp = 0,0\n for i in 0 ... inputLog[index].size \n sum += input[inputLog[index][i]];\n if (sum >= maxi)\n tmp = inputLog[index][i];\n break\n end\n end\n if (tmp < fastest)\n ans = key\n fastest = tmp\n end\n end\nend\nputs ans\n"}, {"source_code": "require 'set'\n\nn = gets.to_i\n\nh = Hash.new\na = Array.new\nb = Array.new\nn.times do\n s = gets.split(' ')\n h[s[0]] = 0 if (h.has_key?(s[0]) == false)\n h[s[0]] += s[1].to_i\n a.push(s[0])\n b.push(h[s[0]])\nend\nmx = h.values.max\n\nd = Set.new\nh.each do |k, v|\n d.add(k) if v == mx\nend\n\na.each_index do |i|\n if d.include? (a[i]) and b[i] == mx\n puts a[i]\n break\n end\nend\n"}, {"source_code": "num_lines = gets().chomp.to_i\nplayers_info = {}\nnum_lines.times{\n |i|\n name, score = gets().chomp.split(/[ ]+/)\n score = score.to_i\n if(players_info.has_key?(name)) then\n players_info[name][0] += score\n players_info[name][1] = i\n else\n players_info[name] = []\n players_info[name][0] = score\n players_info[name][1] = i\n end\n}\n\nputs players_info.to_a.sort {\n |a,b|\n (((b[1][0] <=> a[1][0]) * 2 + (a[1][1] <=> b[1][1])) <=> 0)\n}[0][0]\n"}, {"source_code": "s = Hash.new(0)\nt = {}\ngets.to_i.times do |i|\n name, score = gets.split\n s[name] += score.to_i\n t[name] = i\nend\n\nmax = s.values.max\nputs t.select { |k, v| s[k] == max }.min_by {|k, v| v}.first"}, {"source_code": "def getIndex(val, h)\n if (h.key?(val))\n return h[val]\n end\n index = h.size\n return h[val] = index\nend\n\nn = gets.to_i\nh = {}\nsum = []\nlast = []\n\nfor i in 0 ... n\n name,val = gets.split\n index = getIndex(name,h)\n val = val.to_i \n if (index < sum.size)\n sum[index] += val\n last[index] = i\n else\n sum << val\n last << i\n end\nend\n\nans = \"\"\nmaxi = sum.max\nfastest = n\nh.each do |key,value|\n if (sum[value] == maxi && last[value] < fastest)\n ans = key\n fastest = last[value]\n end\nend\nputs ans\n"}, {"source_code": "A={}\nb={}\nl=[]\ngets.to_i.times{\n q,w=gets.split;\n A[q]=A[q].to_i+w.to_i;\n l+=[[q,w]]\n \n \n}\nm=A.values.max\nl.each{|q,w|\n b[q]=b[q].to_i+w.to_i;\nif b[q]>=m then\n puts q\n exit\nend\n}"}, {"source_code": "n = gets.to_i\na = n.times.map{gets.split}\nh = Hash.new(0)\na.each{|name, s| h[name] += s.to_i}\nmx = h.max_by{|name, s| s}[1]\nh2 = Hash.new(0)\na.each do |name, s|\n h2[name] += s.to_i\n if h2[name] == mx\n puts name\n break\n end\nend\n"}, {"source_code": "A={}\nb={}\nl=[]\ngets.to_i.times{\n q,w=gets.split;\n A[q]=A[q].to_i+w.to_i;\n l+=[[q,w]]\n \n \n}\nm=A.values.max\nl.each{|q,w|\n b[q]=b[q].to_i+w.to_i;\nif b[q]>=m then\n puts q\n exit\nend\n}"}, {"source_code": "n=gets.chomp!.to_i\nmemo=Hash.new\npremax=-1111\nmax=-1000\nname=\"\"\nn.times do \n tmp=gets.chomp!.split()\n if memo.key?(tmp[0])\n memo[tmp[0]]+=tmp[1].to_i\n else\n memo[tmp[0]]=tmp[1].to_i\n end\n max=-1000\n tmpname=\"\"\n memo.each do |key,value|\n if max m\n m = h[a]\n winner = a\n end\n\nend\n\nputs winner\n\n \n"}, {"source_code": "players = {}\ngets.to_i.times do |i|\n name, point = gets.chomp.split\n players[name] ||= {:point => 0, :last => nil}\n players[name][:point] += point.to_i\n players[name][:last] = i\nend\nresult = players.sort{|a, b| (b[1][:point] <=> a[1][:point]).nonzero? || a[1][:last] <=> b[1][:last]}\n#p result\nputs result[0][0]\n"}, {"source_code": "log = []\nscores = Hash.new(0)\ncheck = Hash.new(0)\ngets.to_i.times{\n n,q=gets.split\n scores[n]+=q.to_i\n log << [q,n]\n}\nm = scores.values.max\nlog.each{|q,n|\n check[n]+=q.to_i\n if check[n] >= m\n print n\n exit\n end\n} \n \n"}, {"source_code": "# Codeforces, Problem 2A\n# http://codeforces.com/contest/1/problem/C\n# Nate Collings\n\nplayers = {}\nmax = 0\nwinner = nil\nrounds = gets.chomp.to_i\n\nrounds.times do\n player, score = gets.chomp.split(\"\\s\")\n score = score.to_i\n players[player] = players.has_key?(player) ? players[player] += score : score\n players[player] = -1 if score < 0\n if players[player] > max\n winner = player\n max = players[player]\n end\nend\n\nputs winner"}, {"source_code": "h = {}\nn = gets.to_i\nl = []\nn.times { |i|\n a, b = gets.split\n l << [a,b]\n if h.key? a\n h[a] += b.to_i\n else\n h[a] = b.to_i\n end\n}\n\nmax = -1000 * n - 1\nh.each do |a, b|\n if b > max\n max = b\n end\nend\n\nh.clear\n\nn.times { |i|\n\n a, b = l[i]\n\n if h.key? a\n h[a] += b.to_i\n else\n h[a] = b.to_i\n end\n\n if h[a] >= max\n print a\n exit(0)\n end\n\n}\n\n"}, {"source_code": "num_lines = gets().chomp.to_i\ntop_score = 0\nscores = {}\nscores_fset ={}\n\naddscore = -> (fn, maxscore, index) do\n (-> (score) {\n prev = fn.call(score)\n return prev if prev\n return index if score <= maxscore\n return -1 \n })\nend\n\nnum_lines.times do |index|\n line = gets().chomp\n name, score = line.split\n score = score.to_i\n scores[name] ||= 0\n scores[name] += score\n\n scores_fset[name] ||= -> (x) do\n (-> () {\n return index if x <= score\n return -1\n }).call()\n end\n\n if scores_fset[name].call(scores[name]) == -1\n scores_fset[name] = addscore.call(scores_fset[name], scores[name], index)\n end\n\n top_score = scores[name] if scores[name] > top_score\nend\n\ntop_scorers = scores.select {|k, v| v == scores.values.max}\nputs top_scorers.min_by {|k, v| scores_fset[k].call(top_score)}[0]\n"}, {"source_code": "table = {}\nlined = []\nARGF.each_line do |line|\n if ARGF.lineno != 1\n name, mark = line.split\n if !table.has_key?(name)\n table[name] = [mark.to_i]\n else\n table[name] += [table[name].last + mark.to_i]\n end\n lined.push({:name => name, :mark => table[name].last})\n end\nend\n\nmaxNames = []\nmax = 0\n\ntable.keys.each do |name|\n if max < table[name].last\n maxNames = [name]\n max = table[name].last\n elsif max == table[name].last\n puts \"Add #{name} with #{max}\"\n end\nend\n\nif maxNames.length == 1\n puts maxNames[0]\nelse\n lined.each do |val|\n if val[:mark] >= max\n puts val[:name]\n break\n end\n end\nend\n"}, {"source_code": "A={}\nb={}\nl=[]\ngets.to_i.times{\n q,w=gets.split;\n A[q]=A[q].to_i+w.to_i;\n l+=[[q,w]]\n \n \n}\nm=A.values.max\nl.each{|q,w|\n b[q]=b[q].to_i+w.to_i;\nif b[q]>=m and A[q]=m then\n puts q\n exit\nend\n}"}, {"source_code": "winner = \"\"\nmax_score = 0\nscoreboard = Hash.new(0)\ngets.to_i.times do\n player, score = gets.split\n scoreboard[player] += score.to_i\n if scoreboard[player] > max_score\n winner = player\n max_score = scoreboard[player]\n end\nend\n\nputs winner\n"}, {"source_code": "log = []\nscores = Hash.new(0)\ngets.to_i.times{\n n,q=gets.split\n scores[n]+=q.to_i\n log << [q,n]\n}\nm = scores.values.max\nlog.each{|q,n|\n if q.to_i>=m then\n print n\n exit\n end\n} \n "}, {"source_code": "h = {}\nn = gets.to_i\nn.times { |i|\n a, b = gets.split\n if h.key? a\n h[a] = [h[a][0] + b.to_i, h[a][0] + b.to_i > h[a][2] ? i : h[a][1], h[a][0] + b.to_i > h[a][2] ? h[a][0] + b.to_i : h[a][2] ]\n else\n h[a] = [b.to_i, i, b.to_i]\n end\n}\n\nmax2 = -1000 * n - 1\nmax1 = n\nindex = ''\n\nh.each do |a, b|\n if b[2] > max2\n max2 = b[2]\n max1 = b[1]\n index = a\n elsif b[2] == max2 and b[1] < max1\n max1 = b[1]\n index = a\n end\nend\n\nprint index\n"}, {"source_code": "s = Hash.new(0)\nt = {}\ngets.to_i.times do |i|\n name, score = gets.split\n s[name] += score.to_i\n t[name] = i\nend\n\nmax = s.values.max\nputs t.select { |k, v| s[k] == max }.min_by {|k, v| v}.first"}, {"source_code": "idx = 0\ntable = {}\nARGF.each_line do |line|\n if ARGF.lineno != 1\n name, mark = line.split\n if !table.has_key?(name)\n table[name] = {'mark' => mark.to_i, 'pos' => idx}\n else\n table[name]['mark'] += mark.to_i\n table[name]['pos'] = idx\n end\n idx = idx + 1\n end\nend\n\nmaxMark = 0\nminIdx = 0\nmax = ''\ntable.keys.each do |key|\n val = table[key]\n if maxMark < val['mark']\n maxMark = val['mark']\n minIdx = val['pos']\n max = key\n elsif maxMark == val['mark'] && minIdx > val['pos']\n minIdx = val['pos']\n max = key\n end\nend\n\nputs max"}, {"source_code": "num_lines = gets().chomp.to_i\ntop_score = 0\nscores = {}\nscores_fset ={}\n\naddscore = -> (fn, maxscore, index) do\n (-> (score) {\n prev = fn.call(score)\n return prev if prev\n return index if score <= maxscore\n return false\n })\nend\n\nnum_lines.times do |index|\n line = gets().chomp\n name, score = line.split\n score = score.to_i\n scores[name] ||= 0\n scores[name] += score\n\n scores_fset[name] ||= -> (x) do\n (-> () {\n return index if x <= score\n return false\n }).call()\n end\n\n unless scores_fset[name].call(scores[name])\n scores_fset[name] = addscore.call(scores_fset[name], scores[name], index)\n end\n\n top_score = scores[name] if scores[name] > top_score\nend\n\nputs top_scorers = scores.select {|k, v| v == scores.values.max}\nputs top_scorers.min_by {|k, v| scores_fset[k].call(top_score)}[0]\n"}, {"source_code": "N = gets.to_i\nh = Hash.new { |hash, key| hash[key] = [0, 10000] }\nN.times do |i| \n n, p = gets.chomp.split\n p = p.to_i\n h[n][0] += p\n h[n][1] = i\nend\ns = h.sort { |a, b| a[1][0] == b[1][0] ? a[1][1] <=> b[1][1] : b[1][0] <=> a[1][0] }\nputs s[0][0]\n"}, {"source_code": "#http://codeforces.com/problemset/problem/2/A\n\nn = gets.to_i\ndir = Hash.new(0)\nstep = Array.new\nmax_val = 0\nmax_name = Array.new\n\nfor i in 1..n\n name, score = gets.chomp.strip.split(\" \")\n score = score.to_i\n dir[name] += score\n record = Hash[\"name\" => name, \"score\" => dir[name]]\n step << record\nend\n\ndir.each_key do |key|\n if dir[key] > max_val\n max_val = dir[key]\n max_name << key\n elsif dir[key] == max_val\n max_name << key\n end\nend\n\nflag = 0\nif max_name.size == 1\n puts max_name[0]\nelse\n for i in 0...step.size\n if step[i][\"score\"] >= max_val\n for j in 0...max_name.size\n if step[i][\"name\"] == max_name[j] and flag == 0\n puts max_name[j]\n flag = 1\n end\n end\n end\n end\nend"}, {"source_code": "def getIndex(val, h)\n if (h.key?(val))\n return h[val]\n end\n index = h.size\n return h[val] = index\nend\n\nn = gets.to_i\nh = {}\ninput = []\nsum = []\ninputLog = []\n\nfor i in 0 ... n\n name,val = gets.split\n index = getIndex(name,h)\n val = val.to_i \n input << val\n if (index < sum.size)\n sum[index] += val\n else\n sum << val\n inputLog << []\n end\n inputLog[index] << i\nend\n\nans = \"\"\nmaxi = sum.max\nfastest = n\nh.each do |key,index|\n if (sum[index] == maxi)\n sum,tmp = 0,0\n for i in 0 ... inputLog[index].size \n sum += input[inputLog[index][i]];\n if (sum > maxi)\n tmp = inputLog[index][i];\n break\n end\n end\n if (tmp < fastest)\n ans = key\n fastest = tmp\n end\n end\nend\nputs ans\n"}, {"source_code": "h = {}\nn = gets.to_i\nn.times { |i|\n a, b = gets.split\n if h.key? a\n h[a] = [h[a][0] + b.to_i, h[a][0] + b.to_i > h[a][2] ? i : h[a][1], h[a][0] + b.to_i > h[a][2] ? h[a][0] + b.to_i : h[a][2] ]\n else\n h[a] = [b.to_i, i, b.to_i]\n end\n}\n\nmax2 = -1000 * n - 1\nmax1 = n\nindex = ''\n\nh.each do |a, b|\n if b[2] > max2\n max2 = b[2]\n max1 = b[1]\n index = a\n elsif b[2] == max2 and b[1] < max1\n max1 = b[1]\n index = a\n end\nend\n\nprint index\n"}, {"source_code": "#=================================\n# Better be sure than sorry. \n#=================================\n$stdin=File.open(\"./input.txt\",\"r\") if ENV[\"LOGNAME\"]==\"skydos\"\n#=================================\n\nn=gets.to_i\n\ndata = []\nhs = Hash.new(0)\n\nn.times{\n\tname, points = gets.split\t\n\t\n\tdata << [name, points.to_i]\n\ths[name]+=points.to_i\n}\n\nmax_points = hs.values.max\n\nhs2 = Hash.new(0)\ndata.each{|name, points|\n\ths2[name]+=points\n\t\n\tif hs2[name]==max_points\n\t\tputs name\n\t\tbreak\n\tend\n}\n"}, {"source_code": "n=gets.chomp.to_i\nnc=0 \n@hash={}\n@hash_total={}\nloop do\t\n\tbreak if nc == n\n\tinput=gets.chomp.split(\" \")\n\tk=0\n\tif nc >= 1\n\t\t@hash.each_key do |plr_nm|\n\t\t\tif plr_nm!=input[0]\n\t\t\t\tk+=1\n\t\t\tend\n\t\tend\n\t\tif k==@hash.length\n\t\t\t@hash[input [0]]=[input[1].to_i]\n\t\telse\n\t\t@hash[input [0]]=@hash[input [0]]+[input[1].to_i]\n\t\tend\n\telse\n\t\t@hash[input [0]]=[input[1].to_i]\n\tend\n\n\tnc+=1\nend\ndef Total(key,array)\n k=0\n\tarray.each do|l|\n\t k=k+l\n\tend\n @hash_total[key]=k\nend\n@hash.each_pair do|a,b|\n\tTotal(a,b)\nend\n\ndef Tiebreaker(a,b)\n ka=0\n ba=0\n k=0\n @hash[a[0]].each do|cn|\n\t\tk=k+cn\n\t\tka+=1\n\t\tbreak if k >= a[1]\n\tend\t\n\tk=0\t\t\n @hash[b[0]].each do|cn|\n\t\tk=k+cn\n\t\tba+=1\n\t\tbreak if k >= b[1]\n\tend\t\n\tif ka>ba\n\t\treturn b\n\telse \n\t\treturn a\n\tend\t\nend\t\n\t\t\t\n \n\n\n\nwin= @hash_total.inject do |memo,n|\n if memo[1]>n[1]\n\tmemo\n elsif memo[1]=n[1]\n Tiebreaker(memo,n)\n else \n\tn\nend\t\nend\nputs win[0]\n"}, {"source_code": "n = gets.chomp.to_i\nh = Hash.new(n)\nhsnm = \"\"\nhssc = 0\nfor i in 1..n\n\tnm,sc = gets.chomp.split(/ /)\n\tsc = sc.to_i\n\tsc += h[nm] if h.has_key?(nm)\n\th[nm] = sc\n\tif hssc < sc\n\t\thsnm = nm\n\t\thssc = sc\n\tend\nend\nputs hsnm\n"}, {"source_code": "h = {}\nhh = {}\nfor name,score in $<.read.scan(/(\\S+) (\\S+)/)\n t = h[name] = (h[name]||0)+score.to_i\n hh[t] = name unless hh[t]\nend\nputs hh[h.map(&:last).max]"}, {"source_code": "log = []\nscores = Hash.new(0)\ncheck = Hash.new(0)\ngets.to_i.times{\n n,q=gets.split\n scores[n]+=q.to_i\n log << [q,n]\n}\nm = scores.values.max\nlog.each{|q,n|\n check[n] += q.to_i\n if check[n] >= m and scores[n] == q.to_i then\n puts n\n exit\n end\n}\n"}, {"source_code": "# frozen_string_literal: true\n\nlines = gets.to_i\n\nscore_map = {}\nat_index_by_name = {}\n1.upto(lines) do |i|\n name, score = gets.split\n score = score.to_i\n\n score_map[name] ||= 0\n score_map[name] += score\n\n at_index_by_name[name] ||= {\n score: -99_999_999,\n index: 9_999_999\n }\n\n next unless at_index_by_name[name][:score] < score_map[name]\n\n at_index_by_name[name] = {\n score: score_map[name],\n index: i\n }\nend\n\nscores = score_map.to_a.sort_by(&:last)\nputs(\n scores\n .select { |score| scores.last.last == score.last }\n .min_by { |score| at_index_by_name[score.first][:index] }\n .first\n)\n"}, {"source_code": "#!/usr/bin/env ruby\n\n$global_hash = Hash.new\n$max_score = 0\n$leader = \"\"\n\nif ($global_hash.has_key? \"key\")\n\t$global_hash[\"key\"] += 2\nelse\n\t$global_hash[\"key\"] = 2\nend\n\ndef process_score(score_string)\n\tparts = score_string.split(' ')\n\tkey = parts[0]\n\tvalue = parts[1].to_i\n\n\tif ($global_hash.has_key? key)\n\t\t$global_hash[key] += value\n\telse\n\t\t$global_hash[key] = value\n\tend\n\n\tif ($global_hash[key] > $max_score)\n\t\t$max_score = $global_hash[key]\n\t\t$leader = key\n\tend\nend\n\n#results for testing\n# match_results = []\n# match_results << \"andrew 3\"\n# match_results << \"andrew 2\"\n# match_results << \"mike 5\"\n\nrepeats = gets.to_i\nmatch_results = []\n\nwhile (repeats > 0)\n\tmatch_results << gets.strip\n\trepeats -= 1\nend\n\nfor score_string in match_results\n\tprocess_score(score_string)\nend\n\nputs $leader"}, {"source_code": "scores = {}\nhistory = {}\ngets.to_i.times do |i|\n name, score = gets.split\n score = score.to_i\n history[i] = [name, score]\n if scores.has_key? name\n scores[name] = scores[name]+score\n else\n scores[name] = score\n end\nend\n\nscores = scores.to_a.sort_by! { |key, value| -value }\nm = scores[0][1]\nscores = {}\n#puts m\nhistory.each do |k,v|\n name = v[0]\n score = v[1]\n if scores.has_key? name\n scores[name] = scores[name]+score\n else\n scores[name] = score\n end\n #puts \"#{scores.inspect} #{scores[name]}\"\n if scores[name] >=m\n puts name\n break\n end\nend"}, {"source_code": "players = {}\ngets.to_i.times do |i|\n name, point = gets.chomp.split\n players[name] ||= {:point => 0, :max => nil, :max_at => nil}\n players[name][:point] += point.to_i\n if players[name][:max].nil? || players[name][:point] > players[name][:max]\n players[name][:max] = players[name][:max]\n players[name][:max_at] = i\n end\nend\nputs players.sort{|a, b| (b[1][:max] <=> a[1][:max]).nonzero? || a[1][:max_at] <=> b[1][:max_at]}[0][0]\n"}, {"source_code": "# frozen_string_literal: true\n\nlines = gets.to_i\n\nscore_map = {}\nat_index_by_name = {}\n1.upto(lines) do |i|\n name, score = gets.split\n score = score.to_i\n\n score_map[name] ||= 0\n score_map[name] += score\n\n at_index_by_name[name] ||= {}\n\n next if at_index_by_name[name][score_map[name]]\n\n at_index_by_name[name][score_map[name]] = i\nend\n\nscores = score_map.to_a.sort_by(&:last)\nputs(\n scores\n .select { |score| scores.last.last == score.last }\n .min_by { |score| at_index_by_name[score.first][score.last] }\n .first\n)\n"}, {"source_code": "A={}\nb={}\nl=[]\ngets.to_i.times{\n q,w=gets.split;\n A[q]=A[q].to_i+w.to_i;\n b[q]=b[q].to_i+w.to_i;\n l+=[[q,w]]\n \n \n}\nm=A.values.max\nl.each{|q,w|\n\nif b[q]>=m and A[q]=m then\n puts q\n exit\nend\n}"}, {"source_code": "# coding: utf-8\n\nn = gets.to_i\nh = {}\nst = []\nn.times {\n name, score = gets.split\n score = score.to_i\n h[name] ||= 0\n h[name] += score\n st << [name, score]\n}\nmax = h.values.max\ncands = []\nfor k,v in h\n if v == max\n cands << k\n end\nend\nif cands.length == 1\n puts cands[0]\nelse\n h2 = {}\n for s in st\n h2[s[0]] ||= 0\n h2[s[0]] += s[1]\n if h2[s[0]] == max\n puts s[0]\n break\n end\n end\nend"}, {"source_code": "num_lines = gets().chomp.to_i\nplayers_info = {}\nnum_lines.times{\n |i|\n name, score = gets().chomp.split(/[ ]+/)\n score = score.to_i\n if(players_info.has_key?(name)) then\n if(score != 0) then\n players_info[name][0] += score\n players_info[name][1] = i\n end\n else\n players_info[name] = []\n players_info[name][0] = score\n players_info[name][1] = i\n end\n}\n\nputs players_info.to_a.sort {\n |a,b|\n (((b[1][0] <=> a[1][0]) * 3 + (a[1][1] <=> b[1][1])) <=> 0)\n}[0][0]\n"}, {"source_code": "A={}\nb={}\nl=[]\ngets.to_i.times{\n q,w=gets.split;\n A[q]=A[q].to_i+w.to_i;\n b[q]=b[q].to_i+w.to_i;\n l+=[[q,w]]\n \n \n}\nm=A.values.max\nl.each{|q,w|\n\nif b[q]>=m and A[q]=m then\n puts q\n exit\nend\n}"}, {"source_code": "h = {}\nn = gets.to_i\nl = []\nn.times { |i|\n a, b = gets.split\n l << [a,b]\n if h.key? a\n h[a] += b.to_i\n else\n h[a] = b.to_i\n end\n}\n\nmax = -1000 * n - 1\nh.each do |a, b|\n if b > max\n max = b\n end\nend\n\nh.clear\n\nn.times { |i|\n\n a, b = l[i]\n\n if h.key? a\n h[a] += b.to_i\n else\n h[a] = b.to_i\n end\n\n if h[a] >= max\n print a\n exit(0)\n end\n\n}\n\n"}, {"source_code": "num_lines = gets().chomp.to_i\ntop_score = 0\nscores = {}\nscores_fset ={}\n\naddscore = -> (fn, maxscore, index) do\n (-> (score) {\n prev = fn.call(score)\n return prev if prev\n return index if score <= maxscore\n return -1 \n })\nend\n\nnum_lines.times do |index|\n line = gets().chomp\n name, score = line.split\n score = score.to_i\n scores[name] ||= 0\n scores[name] += score\n\n scores_fset[name] ||= -> (x) do\n (-> () {\n return index if x <= score\n return -1\n }).call()\n end\n\n if scores_fset[name].call(scores[name]) == -1\n scores_fset[name] = addscore.call(scores_fset[name], scores[name], index)\n end\n\n top_score = scores[name] if scores[name] > top_score\nend\n\ntop_scorers = scores.select {|k, v| v == scores.values.max}\nputs top_scorers.min_by {|k, v| scores_fset[k].call(top_score)}[0]\n"}, {"source_code": "steps = gets.chomp.to_i\n\nwinner = nil\ncompetitors = {}\n\nsteps.times do\n step_result = gets.chomp.split\n step_winner = step_result[0]\n step_winner_score = step_result[1].to_i\n winner ||= step_winner\n competitors[step_winner] ||= 0\n competitors[step_winner] += step_winner_score\n winner = step_winner if competitors[winner] < competitors[step_winner]\nend\n\nputs winner\n"}, {"source_code": "# coding: utf-8\n\nn = gets.to_i\nh = {}\nst = []\nn.times {\n name, score = gets.split\n score = score.to_i\n h[name] ||= 0\n h[name] += score\n st << [name, score]\n}\nmax = h.values.max\ncands = []\nfor k,v in h\n if v == max\n cands << k\n end\nend\nif cands.length == 1\n puts cands[0]\nelse\n h2 = {}\n for s in st\n h2[s[0]] ||= 0\n h2[s[0]] += s[1]\n if h2[s[0]] >= max\n puts s[0]\n break\n end\n end\nend"}, {"source_code": "# frozen_string_literal: true\n\nlines = gets.to_i\n\nscore_map = {}\nat_index_by_name = {}\n1.upto(lines) do |i|\n name, score = gets.split\n score = score.to_i\n\n score_map[name] ||= 0\n score_map[name] += score\n\n at_index_by_name[name] ||= {\n score: -99_999_999,\n index: 9_999_999\n }\n\n next unless at_index_by_name[name][:score] < score_map[name]\n\n at_index_by_name[name] = {\n score: score_map[name],\n index: i\n }\nend\n\nscores = score_map.to_a.sort_by(&:last).reverse\nputs(\n scores\n .select { |score| scores.last.last == score.last }\n .min_by { |score| at_index_by_name[score.first][:index] }\n .first\n)\n"}, {"source_code": "n=gets.chomp.to_i\n\nnames=[]\nvalues=[]\n\na=0\nwhile amaxvalue\n\t\t\t\t\tmaxvalue=sortvalues[k]\n\t\t\t\t\tmax=k\n\t\t\t\tend\n\t\t\t\tbreak\n\t\t\tend\n\t\tend\n\n\t\tif !flag\n\t\t\tsortnames.push(names[i])\n\t\t\tsortvalues.push(values[i])\n\t\t\tif values[i]>maxvalue\n\t\t\t\tmaxvalue=values[i]\n\t\t\t\tmax=sortnames.length-1\n\t\t\tend\n\t\tend\n\tend\nend\n\nputs sortnames[max]"}, {"source_code": "n=gets.chomp!.to_i\nmemo=Hash.new\nscore=[]\nn.times do\n score << gets.chomp\nend\nn.times do |j|\n tmp=score[j].split\n if memo.key?(tmp[0])\n memo[tmp[0]]+=tmp[1].to_i\n else\n memo[tmp[0]]=tmp[1].to_i\n end\nend\nmax=memo.values.max\nmemo.clear()\nn.times do |j|\n tmp=score[j].split\n if memo.key?(tmp[0])\n memo[tmp[0]]+=tmp[1].to_i\n else\n memo[tmp[0]]=tmp[1].to_i\n end\n if memo[tmp[0]]==max\n puts tmp[0]\n break\n end\nend\n"}, {"source_code": "s = Hash.new(0)\nt = {}\ngets.to_i.times do |i|\n name, score = gets.split\n s[name] += score.to_i\n t[name] = i\nend\n\nmin = s.values.min\nputs t.select { |k, v| s[k] == min }.min_by {|k, v| v}.first"}, {"source_code": "n = gets.to_i\nans = Hash.new\nfor i in 0...n do\n name, point = gets.split\n name = name.to_sym\n point = point.to_i\n\n if ans[name].nil?\n ans[name] = point, i \n else\n ans[name][0] = ans[name][0] + point\n end\n\n ans[name][1] = i\nend\n\nans = ans.to_a\nans.sort! { |a, b| b[1][0] <=> a[1][0] }\n\ni = 0\nn = ans.size - 1\nfinal = ans[0]\nfinal_next = final\n\nwhile i < n && final_next[1][0] == final[1][0]\n if final_next[1][1] < final[1][1]\n final = final_next\n final_next = ans[i + 1]\n else\n final_next = ans[i + 1]\n end\n i = i + 1\nend\n\nif final_next[1][1] < final[1][1] && final_next[1][0] == final[1][0]\n final = final_next\nend\n\np final[0].to_s"}, {"source_code": "n = gets.to_i\nh = Hash.new\nnames = []\nscores = []\n\nfor i in 0...n\n\n s, k = gets.chomp.split\n k = k.to_i\n\n if h[s].nil?\n h[s] = 0\n end\n\n h[s] += k\n\n names.push(s)\n scores.push(k)\nend\n\nmax = h.values.max\nh.clear\n\nfor i in 0...n\n if h[names[i]].nil?\n h[names[i]] = 0\n end\n h[names[i]] += scores[i]\n if h[names[i]] >= max\n puts names[i]\n exit\n end\nend\n"}, {"source_code": "n=gets.chomp.to_i\nnc=0 \n@hash={}\n@hash_total={}\nloop do\t\n\tbreak if nc == n\n\tinput=gets.chomp.split(\" \")\n\tk=0\n\tif nc >= 1\n\t\t@hash.each_key do |plr_nm|\n\t\t\tif plr_nm!=input[0]\n\t\t\t\tk+=1\n\t\t\tend\n\t\tend\n\t\tif k==@hash.length\n\t\t\t@hash[input [0]]=[input[1].to_i]\n\t\telse\n\t\t@hash[input [0]]=@hash[input [0]]+[input[1].to_i]\n\t\tend\n\telse\n\t\t@hash[input [0]]=[input[1].to_i]\n\tend\n\n\tnc+=1\nend\ndef Total(key,array)\n k=0\n\tarray.each do|l|\n\t k=k+l\n\tend\n @hash_total[key]=k\nend\n@hash.each_pair do|a,b|\n\tTotal(a,b)\nend\n\ndef Tiebreaker(a,b)\n ka=0\n ba=0\n k=0\n @hash[a[0]].each do|cn|\n\t\tk=k+cn\n\t\tka+=1\n\t\tbreak if k >= a[1]\n\tend\t\n\tk=0\t\t\n @hash[b[0]].each do|cn|\n\t\tk=k+cn\n\t\tba+=1\n\t\tbreak if k >= b[1]\n\tend\t\n\tif ka>ba\n\t\treturn b\n\telse \n\t\treturn a\n\tend\t\nend\t\n\t\t\t\n \n\n\n\nwin= @hash_total.inject do |memo,n|\n if memo[1]>n[1]\n\tmemo\n elsif memo[1]=n[1]\n Tiebreaker(memo,n)\n else \n\tn\nend\t\nend\nputs win[0]\n"}, {"source_code": "s = Hash.new(0)\nt = {}\ngets.to_i.times do |i|\n name, score = gets.split\n s[name] += score.to_i\n t[name] = i\nend\n\nmin = s.values.min\nputs t.select { |k, v| s[k] == min }.min_by {|k, v| v}.first"}], "src_uid": "c9e9b82185481951911db3af72fd04e7"} {"nl": {"description": "The 2050 volunteers are organizing the \"Run! Chase the Rising Sun\" activity. Starting on Apr 25 at 7:30 am, runners will complete the 6km trail around the Yunqi town.There are $$$n+1$$$ checkpoints on the trail. They are numbered by $$$0$$$, $$$1$$$, ..., $$$n$$$. A runner must start at checkpoint $$$0$$$ and finish at checkpoint $$$n$$$. No checkpoint is skippable — he must run from checkpoint $$$0$$$ to checkpoint $$$1$$$, then from checkpoint $$$1$$$ to checkpoint $$$2$$$ and so on. Look at the picture in notes section for clarification.Between any two adjacent checkpoints, there are $$$m$$$ different paths to choose. For any $$$1\\le i\\le n$$$, to run from checkpoint $$$i-1$$$ to checkpoint $$$i$$$, a runner can choose exactly one from the $$$m$$$ possible paths. The length of the $$$j$$$-th path between checkpoint $$$i-1$$$ and $$$i$$$ is $$$b_{i,j}$$$ for any $$$1\\le j\\le m$$$ and $$$1\\le i\\le n$$$.To test the trail, we have $$$m$$$ runners. Each runner must run from the checkpoint $$$0$$$ to the checkpoint $$$n$$$ once, visiting all the checkpoints. Every path between every pair of adjacent checkpoints needs to be ran by exactly one runner. If a runner chooses the path of length $$$l_i$$$ between checkpoint $$$i-1$$$ and $$$i$$$ ($$$1\\le i\\le n$$$), his tiredness is $$$$$$\\min_{i=1}^n l_i,$$$$$$ i. e. the minimum length of the paths he takes.Please arrange the paths of the $$$m$$$ runners to minimize the sum of tiredness of them.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 10\\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\leq n,m \\leq 100$$$). The $$$i$$$-th of the next $$$n$$$ lines contains $$$m$$$ integers $$$b_{i,1}$$$, $$$b_{i,2}$$$, ..., $$$b_{i,m}$$$ ($$$1 \\le b_{i,j} \\le 10^9$$$). It is guaranteed that the sum of $$$n\\cdot m$$$ over all test cases does not exceed $$$10^4$$$.", "output_spec": "For each test case, output $$$n$$$ lines. The $$$j$$$-th number in the $$$i$$$-th line should contain the length of the path that runner $$$j$$$ chooses to run from checkpoint $$$i-1$$$ to checkpoint $$$i$$$. There should be exactly $$$m$$$ integers in the $$$i$$$-th line and these integers should form a permuatation of $$$b_{i, 1}$$$, ..., $$$b_{i, m}$$$ for all $$$1\\le i\\le n$$$. If there are multiple answers, print any.", "sample_inputs": ["2\n2 3\n2 3 4\n1 3 5\n3 2\n2 3\n4 1\n3 5"], "sample_outputs": ["2 3 4\n5 3 1\n2 3\n4 1\n3 5"], "notes": "NoteIn the first case, the sum of tiredness is $$$\\min(2,5) + \\min(3,3) + \\min(4,1) = 6$$$. In the second case, the sum of tiredness is $$$\\min(2,4,3) + \\min(3,1,5) = 3$$$."}, "positive_code": [{"source_code": "t = gets.to_s.to_i\n1.upto(t) do |casenum|\n checkpoints, paths = gets.to_s.split.map {|x| x.to_i }\n\n a = []\n checkpoints.times do\n a.push(gets.to_s.split.map {|x| x.to_i }.sort)\n end\n\n results = []\n\n paths.times do\n result = []\n best = a[0][0]\n best_i = 0\n a.each.with_index do |x, i|\n if x[0] < best\n best = x[0]\n best_i = i\n end\n end\n a.each_index do |i|\n if i == best_i\n result.push(a[i].shift)\n else\n result.push(a[i].pop)\n end\n end\n results.push(result)\n end\n 0.upto(checkpoints - 1) do |i|\n printf(\"%s\\n\", 0.upto(paths - 1).map {|j| results[j][i] }.join(\" \"))\n end\nend\n"}], "negative_code": [], "src_uid": "a9021fed22299e90aaef50c4d0d9f5b2"} {"nl": {"description": "In order to write a string, Atilla needs to first learn all letters that are contained in the string.Atilla needs to write a message which can be represented as a string $$$s$$$. He asks you what is the minimum alphabet size required so that one can write this message.The alphabet of size $$$x$$$ ($$$1 \\leq x \\leq 26$$$) contains only the first $$$x$$$ Latin letters. For example an alphabet of size $$$4$$$ contains only the characters $$$\\texttt{a}$$$, $$$\\texttt{b}$$$, $$$\\texttt{c}$$$ and $$$\\texttt{d}$$$.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 1000$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 100$$$) — the length of the string. The second line of each test case contains a string $$$s$$$ of length $$$n$$$, consisting of lowercase Latin letters.", "output_spec": "For each test case, output a single integer — the minimum alphabet size required to so that Atilla can write his message $$$s$$$.", "sample_inputs": ["5\n\n1\n\na\n\n4\n\ndown\n\n10\n\ncodeforces\n\n3\n\nbcf\n\n5\n\nzzzzz"], "sample_outputs": ["1\n23\n19\n6\n26"], "notes": "NoteFor the first test case, Atilla needs to know only the character $$$\\texttt{a}$$$, so the alphabet of size $$$1$$$ which only contains $$$\\texttt{a}$$$ is enough.For the second test case, Atilla needs to know the characters $$$\\texttt{d}$$$, $$$\\texttt{o}$$$, $$$\\texttt{w}$$$, $$$\\texttt{n}$$$. The smallest alphabet size that contains all of them is $$$23$$$ (such alphabet can be represented as the string $$$\\texttt{abcdefghijklmnopqrstuvw}$$$)."}, "positive_code": [{"source_code": "main = -> {\n n = gets.to_i\n s = gets.chomp.chars\n s.max.ord - 'a'.ord + 1\n}\n\nt = gets.to_i\nputs t.times.map{ main.call }.join(\"\\n\")\n"}, {"source_code": "gets.to_i.times {\n gets\n s=gets.chars.map(&:ord)\n puts s.max.to_i-96\n}"}, {"source_code": "=begin\n\nWelcome to GDB Online.\nGDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,\nC#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.\nCode, Compile, Run and Debug online from anywhere in world.\n\n=end\n\nt=gets.to_i\nfor i in 1..t\n n =gets.to_i\n p gets.chars.map{|x|x.ord}.max-\"a\".ord+1\n\nend\n"}, {"source_code": "gets.to_i.times{\r\n gets\r\n s=gets.chars.map{|x|x.ord-97}\r\n p s.max+1\r\n}"}], "negative_code": [], "src_uid": "4841cbc3d3ef29929690b78e30fbf22e"} {"nl": {"description": "You are given two integers $$$x$$$ and $$$y$$$. You want to choose two strictly positive (greater than zero) integers $$$a$$$ and $$$b$$$, and then apply the following operation to $$$x$$$ exactly $$$a$$$ times: replace $$$x$$$ with $$$b \\cdot x$$$.You want to find two positive integers $$$a$$$ and $$$b$$$ such that $$$x$$$ becomes equal to $$$y$$$ after this process. If there are multiple possible pairs, you can choose any of them. If there is no such pair, report it.For example: if $$$x = 3$$$ and $$$y = 75$$$, you may choose $$$a = 2$$$ and $$$b = 5$$$, so that $$$x$$$ becomes equal to $$$3 \\cdot 5 \\cdot 5 = 75$$$; if $$$x = 100$$$ and $$$y = 100$$$, you may choose $$$a = 3$$$ and $$$b = 1$$$, so that $$$x$$$ becomes equal to $$$100 \\cdot 1 \\cdot 1 \\cdot 1 = 100$$$; if $$$x = 42$$$ and $$$y = 13$$$, there is no answer since you cannot decrease $$$x$$$ with the given operations. ", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. Each test case consists of one line containing two integers $$$x$$$ and $$$y$$$ ($$$1 \\le x, y \\le 100$$$).", "output_spec": "If it is possible to choose a pair of positive integers $$$a$$$ and $$$b$$$ so that $$$x$$$ becomes $$$y$$$ after the aforementioned process, print these two integers. The integers you print should be not less than $$$1$$$ and not greater than $$$10^9$$$ (it can be shown that if the answer exists, there is a pair of integers $$$a$$$ and $$$b$$$ meeting these constraints). If there are multiple such pairs, print any of them. If it is impossible to choose a pair of integers $$$a$$$ and $$$b$$$ so that $$$x$$$ becomes $$$y$$$, print the integer $$$0$$$ twice.", "sample_inputs": ["3\n\n3 75\n\n100 100\n\n42 13"], "sample_outputs": ["2 5\n3 1\n0 0"], "notes": null}, "positive_code": [{"source_code": "def try(i, y)\n cnt = 0\n while y % i == 0\n cnt += 1\n y /= i\n end\n\n return nil unless y == 1\n cnt\nend\n\ngets.to_i.times do\n x, y = gets.split.map(&:to_i)\n\n if y % x != 0\n puts '0 0'\n next\n end\n\n y /= x\n\n if y == 1\n puts '1 1'\n next\n end\n\n i = 2\n found = false\n while i <= y\n j = try(i, y)\n if j\n puts \"#{j} #{i}\"\n found = true\n break\n end\n\n i += 1\n end\n\n next if found\n puts '0 0'\nend\n"}, {"source_code": "pow = (1..10).flat_map do |a|\r\n (1..100).map do |b|\r\n [b ** a, [a, b]]\r\n end\r\nend.to_h\r\n\r\ngets.to_i.times do\r\n x, y = gets.split.map(&:to_i)\r\n if y >= x && y % x == 0 && pow[y / x]\r\n puts pow[y / x].join(\" \")\r\n else\r\n puts \"0 0\"\r\n end\r\nend\r\n"}, {"source_code": "for t in 1..gets.to_i do\r\n\ta=gets.split\r\n\ta[0]=a[0].to_i\r\n\ta[1]=a[1].to_i\r\n\tc=a[0]\r\n\td=a[1]\r\n\r\n\tif d%c==0\r\n\t\tprint 1,' ',d.div(c),\"\\n\"\r\n\telse\r\n\t\tprint 0,' ',0,\"\\n\"\r\n\tend\r\nend"}, {"source_code": "t=gets.chomp.to_i\r\nt.times do\r\n s= gets.chomp.split\r\n x=s[0].to_i\r\n y=s[1].to_i\r\n if (y % x ==0)\r\n puts \"#{1} #{y/x}\"\r\n else \r\n puts \"#{0} #{0}\"\r\n end\r\nend\r\n"}], "negative_code": [{"source_code": "def try(i, y)\n cnt = 0\n while y % i == 0\n cnt += 1\n y /= i\n end\n\n return nil unless y == 1\n cnt\nend\n\ngets.to_i.times do\n x, y = gets.split.map(&:to_i)\n\n if y % x != 0\n puts '0 0'\n next\n end\n\n y /= x\n\n if y == 1\n puts '1 1'\n next\n end\n\n i = 2\n found = false\n while i * i <= y\n j = try(i, y)\n if j\n puts \"#{j} #{i}\"\n found = true\n break\n end\n\n i += 1\n end\n\n next if found\n puts '0 0'\nend\n"}, {"source_code": "pow = (1..10).flat_map do |a|\r\n (1..10).map do |b|\r\n [b ** a, [a, b]]\r\n end\r\nend.to_h\r\n\r\ngets.to_i.times do\r\n x, y = gets.split.map(&:to_i)\r\n if y >= x && y % x == 0 && pow[y / x]\r\n puts pow[y / x].join(\" \")\r\n else\r\n puts \"0 0\"\r\n end\r\nend\r\n"}], "src_uid": "f7defb09175c842de490aa13a4f5a0c9"} {"nl": {"description": "Ohana Matsumae is trying to clean a room, which is divided up into an n by n grid of squares. Each square is initially either clean or dirty. Ohana can sweep her broom over columns of the grid. Her broom is very strange: if she sweeps over a clean square, it will become dirty, and if she sweeps over a dirty square, it will become clean. She wants to sweep some columns of the room to maximize the number of rows that are completely clean. It is not allowed to sweep over the part of the column, Ohana can only sweep the whole column.Return the maximum number of rows that she can make completely clean.", "input_spec": "The first line of input will be a single integer n (1 ≤ n ≤ 100). The next n lines will describe the state of the room. The i-th line will contain a binary string with n characters denoting the state of the i-th row of the room. The j-th character on this line is '1' if the j-th square in the i-th row is clean, and '0' if it is dirty.", "output_spec": "The output should be a single line containing an integer equal to a maximum possible number of rows that are completely clean.", "sample_inputs": ["4\n0101\n1000\n1111\n0101", "3\n111\n111\n111"], "sample_outputs": ["2", "3"], "notes": "NoteIn the first sample, Ohana can sweep the 1st and 3rd columns. This will make the 1st and 4th row be completely clean.In the second sample, everything is already clean, so Ohana doesn't need to do anything."}, "positive_code": [{"source_code": "n = gets.chomp.to_i\narr = Hash.new{0}\nn.times do\narr[gets.chomp]+=1\nend\nmax = 0\narr.each do |key,value|\n\tif value > max\n\tmax = value\n\tend\nend \nprint max\n\n=begin\n4\n0101\n1000\n1111\n0101\n\n3\n111\n111\n111\n=end\n"}, {"source_code": "#!/usr/bin/ruby\np$<.drop(1).group_by{|e|e}.map{|k,v|v.size}.max"}, {"source_code": "def function\nb=gets.chomp.to_i\nstring_array=[]\nb.times do |i|\na=gets.chomp\nstring_array.push(a)\nend\ncount_array=[]\n0.upto string_array.length-1 do |i|\ncount_array.push(string_array.count(string_array[i]))\nend\nputs count_array.max\nend\nfunction\n"}, {"source_code": "rows = gets.to_i\nrow_hash = {}\n(rows).times do \n row_val = gets\n if row_hash.include?(row_val)\n row_hash[row_val]+=1\n else\n row_hash[row_val] = 1\n end\nend\nputs (row_hash.max_by{|k, v| v}).last\n"}, {"source_code": "#!/usr/bin/ruby\np$<.drop(1).group_by{|e|e}.map{|k,v|v.size}.max\n"}, {"source_code": "#!/usr/bin/ruby\np$<.drop(1).group_by{|e|e}.map{|k,v|v.size}.max\n"}, {"source_code": "$pair = {}\n\ndef sweep(arr, row)\n\t\tstr = arr[row] * \"\"\n\t\tif $pair.has_key?(str)\n\t\t\t$pair[str] += 1\n\t\telse\n\t\t\t$pair[str] = 1\n\t\tend\nend\n\n=begin\ndef count(arr, col)\n\tl = 0\n\t(0...arr[0].length).each do |iterNum|\n\t\tl += 1 if arr[iterNum][0] == 0\n\tend\n\n\tl\nend\n\ndef cleanCount(arr)\n\tl = 0\n\n\t(0...arr[0].length).each do |iterNum|\n\t\tstr = arr[iterNum] * \"\"\n\t\tl += 1 if str == (\"1\" * arr[0].length)\n\tend\n\n\tl\nend\n=end\n\nn = gets.chomp.to_i\nroom = []\ncountArr = []\n\n(0...n).each do |iter|\n\troom << (gets.chomp.split('').map {|item| item.to_i})\nend\n\n(0...n).each do |iter|\n\tcountArr << sweep(room, iter)\n\t#print $pair\n\t#puts ''\nend\n\nmax = 0\n$pair.each_value do |val|\n\tmax = val if max < val\nend\n\nputs max"}, {"source_code": "#!/usr/bin/ruby\np$<.drop(1).group_by{|e|e}.map{|k,v|v.size}.max"}, {"source_code": "#!/usr/bin/ruby\np$<.drop(1).group_by{|e|e}.map{|k,v|v.size}.max\n"}, {"source_code": "#!/usr/bin/ruby\np$<.drop(1).group_by{|e|e}.map{|k,v|v.size}.max\n"}, {"source_code": "n = gets.to_i\ns = {}\nn.times do |i|\n r = gets.chomp\n s[r] ||= 0\n s[r] += 1\nend\n\nputs s.values.max"}, {"source_code": "n,h = gets.to_i,{}\n(1..n).each{\n r = gets.strip\n h[r] ||= 0\n h[r] += 1 \n}\np h.values.max"}, {"source_code": "#!/usr/bin/ruby\np$<.drop(1).group_by{|e|e}.map{|k,v|v.size}.max\n"}, {"source_code": "n = gets.to_i\na = n.times.map{gets.chomp}\np a.uniq.reduce(0){|mx, x| mx = [mx, a.count(x)].max}\n"}, {"source_code": "#!/usr/bin/ruby\np$<.drop(1).group_by{|e|e}.map{|k,v|v.size}.max\n"}, {"source_code": "#!/usr/bin/ruby\np$<.drop(1).group_by{|e|e}.map{|k,v|v.size}.max\n"}, {"source_code": "\nn = gets().chomp().to_i\nb = []\n\nn.times{\n b.push(gets().chomp())\n}\n\nc=b.sort{|x,y| x <=> y}\n\nstr=c[0]\nmax = 1\ntemp =1\nfor i in (1..c.length-1)\n if c[i]==str\n temp+=1\n\n else\n str=c[i]\n if temp > max\n max = temp\n end\n temp=1\n end\n\nend\n\nif temp > max\n max = temp\nend\n\nputs max\n\n"}, {"source_code": "#!/usr/bin/ruby\np$<.drop(1).group_by{|e|e}.map{|k,v|v.size}.max\n"}], "negative_code": [{"source_code": "\nn = gets().chomp().to_i\nb = []\n\nn.times{\n s = gets().chomp().split('')\n a = []\n for i in (0..n-1)\n if s[i]=='0'\n a.push(\"#{i+1}\")\n end\n end\n if a.length()==0\n a.push(\"0\")\n end\n b.push(a.join(''))\n}\n\nc=b.sort{|x,y| x.length <=> y.length}\n\nstr=c[0]\nmax = 1\ntemp =1\nfor i in (1..c.length-1)\n if c[i]==str\n temp+=1\n\n else\n str=c[i]\n if temp > max\n max = temp\n end\n temp=1\n end\n\nend\n\nputs max\n\n"}], "src_uid": "f48ddaf1e1db5073d74a2aa318b46704"} {"nl": {"description": "You are given an array of n elements, you must make it a co-prime array in as few moves as possible.In each move you can insert any positive integral number you want not greater than 109 in any place in the array.An array is co-prime if any two adjacent numbers of it are co-prime.In the number theory, two integers a and b are said to be co-prime if the only positive integer that divides both of them is 1.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 1000) — the number of elements in the given array. The second line contains n integers ai (1 ≤ ai ≤ 109) — the elements of the array a.", "output_spec": "Print integer k on the first line — the least number of elements needed to add to the array a to make it co-prime. The second line should contain n + k integers aj — the elements of the array a after adding k elements to it. Note that the new array should be co-prime, so any two adjacent values should be co-prime. Also the new array should be got from the original array a by adding k elements to it. If there are multiple answers you can print any one of them.", "sample_inputs": ["3\n2 7 28"], "sample_outputs": ["1\n2 7 9 28"], "notes": null}, "positive_code": [{"source_code": "gets;a=gets.split.map(&:to_i);b=[a[0],*a.each_cons(2).flat_map{|x,y|x.gcd(y)==1 ? [y] : [1,y]}];p b.size-a.size;puts b*' '"}, {"source_code": "def gcd(a, b)\n if b == 0\n return a\n else\n return gcd(b, a % b)\n end\nend\n\nn = readline.to_i\narr = readline.split(' ').collect(&:to_i)\nct = 0\nres = []\n0.upto(arr.length - 2) do |ind|\n a, b = arr[ind], arr[ind + 1]\n res += [a]\n if gcd(a, b) != 1\n ct += 1\n res += [1]\n end\nend\nres += [arr.last]\nputs ct\nres.each_with_index do |n, i|\n if i != 0\n print \" \"\n end\n print n\nend\nputs \"\"\n"}, {"source_code": "a=gets.chomp.to_i\nb=gets.chomp.split(\" \").map{|i| i.to_i}\nans=[]\np=0\n0.upto(a-2) do |i|\n if b[i].gcd(b[i+1])!=1\n ans << b[i]\n ans << 1\n p+=1\n else\n ans << b[i]\n end\nend\nans << b[a-1]\nputs \"#{p}\"\nputs ans.join(\" \")"}, {"source_code": "a=gets.chomp.to_i\nb=gets.chomp.split(\" \").map{|i| i.to_i}\nans=[]\np=0\n0.upto(a-2) do |i|\n if b[i].gcd(b[i+1])!=1\n ans << b[i]\n ans << 1\n p+=1\n else\n ans << b[i]\n end\nend\nans << b[a-1]\nputs \"#{p}\"\nputs ans.join(\" \")"}, {"source_code": "def gcd(a,b)\n\treturn b>0 ? gcd(b,a%b) : a\nend\n\nn=gets.to_i\na=gets.split(' ')\ns=0\nfor i in 1..a.size-1 \n\ts+=1 if gcd(a[i].to_i,a[i-1].to_i)>1\nend\nputs s\nprint a[0]\nfor i in 1..a.size-1 \n\tprint \" 1\" if gcd(a[i].to_i,a[i-1].to_i)>1\n\tprint \" \"+a[i]\nend\nputs"}, {"source_code": "gets;a=gets.split.map(&:to_i);b=[a[0],*a.each_cons(2).flat_map{|x,y|x.gcd(y)==1 ? [y] : [1,y]}];p b.size-a.size;puts b*' '\n"}], "negative_code": [{"source_code": "a=gets.chomp.to_i\nb=gets.chomp.split(\" \").map{|i| i.to_i}\nans=[]\n0.upto(a-2) do |i|\n if b[i].gcd(b[i+1])!=1\n ans << b[i]\n ans << 1\n else\n ans << b[i]\n end\nend\nans << b[a-1]\nputs ans.join(\" \")"}, {"source_code": "def gcd(a,b)\n\treturn b>0 ? gcd(b,a%b) : a\nend\n\nn=gets.to_i\na=gets.split(' ')\nprint a[0]\nfor i in 1..a.size-1 \n\tprint \" 1\" if gcd(a[i].to_i,a[i-1].to_i)>1\n\tprint \" \"+a[i]\nend\nputs"}], "src_uid": "b0b4cadc46f9fd056bf7dc36d1cf51f2"} {"nl": {"description": "Yaroslav has an array that consists of n integers. In one second Yaroslav can swap two neighboring array elements. Now Yaroslav is wondering if he can obtain an array where any two neighboring elements would be distinct in a finite time.Help Yaroslav.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 100) — the number of elements in the array. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1000) — the array elements.", "output_spec": "In the single line print \"YES\" (without the quotes) if Yaroslav can obtain the array he needs, and \"NO\" (without the quotes) otherwise.", "sample_inputs": ["1\n1", "3\n1 1 2", "4\n7 7 7 7"], "sample_outputs": ["YES", "YES", "NO"], "notes": "NoteIn the first sample the initial array fits well.In the second sample Yaroslav can get array: 1, 2, 1. He can swap the last and the second last elements to obtain it.In the third sample Yarosav can't get the array he needs. "}, "positive_code": [{"source_code": "c = Hash.new(0)\n\nn = gets.to_i\n\nmax = -1\n\ngets.split.map(&:to_i).each do |i|\n\tc[i]+=1\n\tmax = [c[i], max].max\nend\n\ncan = max<=(n/2.0).ceil.to_i\n\nputs can ? 'YES' : 'NO'"}, {"source_code": "n=gets.chomp.to_i\nn=(n+1)>>1\na=gets.chomp.split\nfor i in a do\n\tif a.count(i)>n\n\t\tputs \"NO\"\n\t\texit\n\tend\nend\nputs \"YES\"\n"}, {"source_code": "n = gets.to_i\ncnt = [], maxs = 0\ngets.split(' ').map do |i|\n i = i.to_i\n cnt[i] = cnt[i].nil? ? 1 : cnt[i] + 1\n maxs = [cnt[i], maxs].max\nend\nif maxs > (n + 1) / 2 && n > 1 then puts \"NO\"\nelse puts \"YES\"\nend\n"}, {"source_code": "n = gets.to_i\na = gets.split(' ').map { |i| i.to_i }\ncnt = []\nmaxs = 0\na.each do |i|\n cnt[i] = 0 if cnt[i].nil?\n cnt[i] += 1\n maxs = [cnt[i], maxs].max\nend\nif maxs > (n + 1) / 2 && n > 1\n puts \"NO\"\nelse \n puts \"YES\"\nend\n"}, {"source_code": "def run\n n = $stdin.gets.to_i\n a = $stdin.gets.split.map(&:to_i)\n h = Hash.new(0)\n a.each do |item|\n h[item] += 1\n end\n\n h.each do |key, value|\n if (value - 1) * 2 >= n\n puts \"NO\"\n return\n end\n end\n\n puts \"YES\"\nend\n\nrun if $0 == __FILE__\n"}, {"source_code": "class A\n def initialize\n n = $stdin.gets.chomp.to_i\n array = $stdin.gets.chomp.split(' ').map(&:to_i)\n\n if array.size == 1\n puts \"YES\"\n exit\n elsif array.size == 2\n if array[0] == array[1]\n puts \"NO\"\n else\n puts \"YES\"\n end\n exit\n end\n\n if array.size.even?\n 1.upto(1000) do |num|\n count = array.count(num)\n\n if(count > array.size/2)\n puts \"NO\"\n exit\n end\n end\n else\n 1.upto(1000) do |num|\n count = array.count(num)\n\n if(count > array.size/2+1)\n puts \"NO\"\n exit\n end\n end\n end\n\n puts \"YES\"\n end\nend\n\na = A.new"}, {"source_code": "n = gets.to_i\na = gets.split #.map{|_|_.to_i}\nh = {}\n\na.each{|i|\n if h[i] then\n h[i]+=1\n else\n h[i]=1\n end\n}\nx = n%2==0 ? n/2 : (n+1)/2\nr = true\nh.each{|k,v|\n r = false if v > x\n}\n\nputs (r ? 'YES':'NO' )\n"}, {"source_code": "n = gets.chomp.to_i\nelements = gets.chomp.split(/ /).map(&:to_i)\nans = \"YES\"\ncnt = Array.new(1001, 0)\nelements.each do |e|\n cnt[e] += 1\nend\nc = 0\ncnt.each do |count|\n c = [count, c].max\nend\nans = \"NO\" if c > (n + 1) / 2\nputs ans\n"}, {"source_code": "a=gets.chomp.to_i\nb=gets.chomp.split(\" \")\nif a==1\nputs \"YES\"\nelsif a!=1 && b.uniq.length==1\nputs \"NO\"\n\nelse\n\nk=Hash.new(0)\nb.each do |i|\nk[\"#{i}\"]+=1\nend\nl=k.values.sort.reverse[0]\nif a-l>=l-1\nputs \"YES\"\nelse\nputs \"NO\"\nend\n\n\nend"}, {"source_code": "$n, $a = gets.to_i, gets.split(' ').map { |x| x.to_i }\n$b = []\n$a.group_by{ |x| x }.each_value{ |x| $b << x.length }\n$k = (if $a.length % 2 == 0 then $a.length / 2 else $a.length / 2 + 1 end)\nputs (if $b.any? { |x| x > $k } then \"NO\" else \"YES\" end)"}, {"source_code": "n = gets.to_i\narr = gets.split(\" \").map(&:to_i)\ncount = Hash.new(0)\n\narr.each do |a|\n\tcount[a] += 1\nend\n\nans = true\ncount.values.each do |c|\n\tunless c <= (n+1)/2\n\t\tans = false\n\t\tbreak\n\tend\nend\n\nputs ans ? \"YES\" : \"NO\"\n\n# count_arr = count.values.sort.reverse\n# # puts \"#{count_arr}\"\n# (0..count_arr.length-2).each do |i|\n# \tj = i + 1\n# \tputs \"#{count_arr}\"\n# \tnext if count_arr[i] == 0\n# \twhile j <= count_arr.length - 1\n# \t\tif count_arr[i] <= count_arr[j]\n# \t\t\tcount_arr[i] = 0\n# \t\t\tcount_arr[j] -= count_arr[i]\n# \t\t\tbreak\n# \t\telse\n# \t\t\tcount_arr[i] -= count_arr[j]\n# \t\t\tcount_arr[j] = 0\n# \t\tend\n# \t\tj += 1\n# \tend\n# end\n# count_arr.reject!{|a| a == 0}\n# puts \"#{count}\"\n# puts \"#{count_arr}\"\n# if count_arr[0] > 1\n# \tputs \"NO\"\n# else\n# \tputs \"YES\"\n# end"}, {"source_code": "#!/usr/bin/ruby -w\nn = (gets.to_i + 1) >> 1\nx = gets.split;\nx.each do |i|\n if x.count(i) > n\n puts \"NO\"\n exit\n end\nend\nputs \"YES\""}, {"source_code": "\ndef solve\n\th = {}\n\tgets\n\ta = gets.chomp.split(/ /)\n\ta.each do |x| h[x] = if h[x] == nil then 1 else h[x] + 1 end end\n\th.each do |x, y| return 'NO' if y > a.length/2 + a.length%2 end\n\t'YES'\nend\n\nprint solve"}, {"source_code": "def div_ceil(x, y)\n x / y + x % y\nend\n\nn = div_ceil(gets.chomp.to_i, 2)\nputs(gets.chomp.split.map { |x| x.to_i }.inject(Hash.new(0)) { |h, e| h[e] += 1; h }.values.any? { |v| v > n } ? 'NO' : 'YES')"}, {"source_code": "n = gets[0..-2].to_i\narr = gets.split(' ')\na = Array.new(n) { |i| arr[i].to_i }\nmark = Array.new(1001, 0)\nfor i in 0...n do\n mark[a[i]] += 1\nend\n\nmax = mark.max\nif max * 2 - n >= 2\n print \"NO\"\nelse\n print \"YES\"\nend\n"}, {"source_code": "#!/usr/bin/env ruby\n\nn = (gets.to_i + 1) >> 1\nx = gets.split.collect{ |i| i.to_i }\nx.each do |i|\n\tif x.count(i) > n\n\t\tputs \"NO\"\n\t\texit\n\tend\nend\n\nputs \"YES\"\n"}, {"source_code": "at_exit do\n n = get_numbers_from_line[0]\n numbers = get_numbers_from_line[0...n]\n\n iter = 0\n while true\n i = get_incorrect_number_index(numbers)\n if i.nil?\n puts \"YES\"\n break\n else\n unless move_number_to_correct_place(i, numbers)\n puts \"NO\"\n break\n end\n end\n iter += 1\n if iter > 1000\n puts \"NO\"\n break\n end\n end\nend\n\ndef get_incorrect_number_index(numbers)\n (0...numbers.length).each do |i|\n return i if numbers[i] == numbers[i+1] || (i-1 >= 0 && numbers[i] == numbers[i-1])\n end\n nil\nend\n\ndef move_number_to_correct_place(index, numbers)\n number = numbers.delete_at(index)\n (0..numbers.length).each do |i|\n if numbers[i] != number && (i-1 >= 0 && numbers[i-1] != number)\n numbers.insert i, number\n return true\n end\n end\n false\nend\n\n\ndef get_numbers_from_line\n gets.strip.split.map(&:to_i)\nend"}, {"source_code": "n = gets.to_i\na = gets.split.map { |x| x.to_i }\nmax = 0\n1.upto(1000) do |i|\n\tmax = [max, a.count(i)].max\nend\n\nif max <= (n + 1) / 2\n\tputs 'YES'\nelse\n\tputs 'NO'\nend"}], "negative_code": [{"source_code": "c = Hash.new(0)\n\nn = gets.to_i\n\nmax = -1\n\ngets.split.map(&:to_i).each do |i|\n\tc[i]+=1\n\tmax = i if c[i]>max\nend\n\ncan = max<=(n/2.0).ceil\n\nputs can ? 'YES' : 'NO'"}, {"source_code": "n = gets.to_i\na = gets.split(' ').map { |i| i.to_i }\ncnt = []\nmaxs = 0\na.each do |i|\n\tcnt[i] = 0 if cnt[i].nil?\n\tcnt[i] += 1\n\tmaxs = [cnt[i], maxs].max\nend\nif maxs > n / 2 && n > 1\n\tputs \"NO\"\nelse \n\tputs \"YES\"\nend\n"}, {"source_code": "n = gets.to_i\na = gets.split(' ').map { |i| i.to_i }\ncnt = []\nmaxs = 0\na.each do |i|\n cnt[i] = 0 if cnt[i].nil?\n cnt[i] += 1\n maxs = [cnt[i], maxs].max\nend\nif maxs > n / 2 && n > 1\n puts \"NO\"\nelse \n puts \"YES\"\nend\n"}, {"source_code": "n = gets.to_i\ncnt = [], maxs = 0\ngets.split(' ').map do |i|\n i = i.to_i\n cnt[i] = cnt[i].nil? ? 0 : cnt[i] + 1\n maxs = [cnt[i], maxs].max\nend\nif maxs > (n + 1) / 2 && n > 1 then puts \"NO\"\nelse puts \"YES\"\nend\n"}, {"source_code": "n = gets.to_i\na = gets.split #.map{|_|_.to_i}\nh = {}\n\na.each{|i|\n if h[i] then\n h[i]+=1\n else\n h[i]=1\n end\n}\nx = n%2 ? n/2 : (n+1)/2\nr = true\nh.each{|k,v|\n r = false if v > x\n}\n\nputs (r ? 'YES':'NO' )\n\n\n\n\n"}, {"source_code": "n = gets.to_i\na = gets.split #.map{|_|_.to_i}\nh = {}\n\na.each{|i|\n if h[i] then\n h[i]+=1\n else\n h[i]=0\n end\n}\nx = n%2 ? n/2 : (n+1)/2\nr = true\nh.each{|k,v|\n r = false if v > x\n}\n\nputs (r ? 'YES':'NO' )\n\n\n\n\n"}, {"source_code": "n = gets.to_i\na = gets.split.map{|_|_.to_i}\n\nr = true\n(n-1).times{|i|\n r = true\n if a[i] == a[i+1] then\n b = a\n t = b[i+1]\n b[i+1] = b[i]\n b[i] = t\n (n-1).times{|j|\n r = false if b[j] == b[j+1]\n }\n end\n}\n\nputs (r ? 'YES':'NO' )\n\n\n\n\n"}, {"source_code": "def distinct?( elements )\n (1...elements.length).each do |i|\n return false if elements[i - 1] == elements[i]\n end\n true\nend\nn = gets.chomp.to_i\nelements = gets.chomp.split(/ /).map(&:to_i)\nans = \"YES\"\n(1...n).each do |i|\n elements[i - 1], elements[i] = elements[i], elements[i - 1] if elements[i - 1] != elements[i]\nend\nans = \"NO\" if !distinct? elements\nputs ans\n"}, {"source_code": "n = gets.chomp.to_i\nelements = gets.chomp.split(/ /).map(&:to_i)\nduplicates = []\n(1...n).each do |i|\n duplicates += [i - 1, i] if elements[i - 1] == elements[i]\nend\nduplicates.uniq!\nans = \"YES\"\nans = \"NO\" if duplicates.length > (n + 1) / 2\nputs ans\n"}, {"source_code": "n = gets.chomp.to_i\nelements = gets.chomp.split(/ /).map(&:to_i)\nduplicates = []\n(1...n).each do |i|\n duplicates += [i - 1, i] if elements[i - 1] == elements[i]\nend\nans = \"YES\"\nans = \"NO\" if duplicates.length > (n + 1) / 2\nputs ans\n"}, {"source_code": "n = gets.to_i\narr = gets.split(\" \").map(&:to_i)\ncount = Hash.new(0)\n\narr.each do |a|\n\tcount[a] += 1\nend\n\ncount_arr = count.values.sort.reverse\n(0..count_arr.length-2).each do |i|\n\tj = i + 1\n\twhile j <= count_arr.length - 1\n\t\tif count_arr[i] <= count_arr[j]\n\t\t\tcount_arr[i] = 0\n\t\t\tcount_arr[j] -= count_arr[i]\n\t\t\tbreak\n\t\telse\n\t\t\tcount_arr[i] -= count_arr[j]\n\t\t\tcount_arr[j] = 0\n\t\tend\n\t\tj += 1\n\tend\nend\ncount_arr.reject!{|a| a == 0}\n# puts \"#{count_arr}\"\nif count_arr[0] > 1\n\tputs \"NO\"\nelse\n\tputs \"YES\"\nend"}, {"source_code": "n = gets.to_i\narr = gets.split(\" \").map(&:to_i)\ncount = Hash.new(0)\n\narr.each do |a|\n\tcount[a] += 1\nend\n\ncount_arr = count.values\n(0..count_arr.length-2).each do |i|\n\tj = i + 1\n\twhile j <= count_arr.length - 1\n\t\tif count_arr[i] <= count_arr[j]\n\t\t\tcount_arr[i] = 0\n\t\t\tcount_arr[j] -= count_arr[i]\n\t\t\tbreak\n\t\telse\n\t\t\tcount_arr[i] -= count_arr[j]\n\t\t\tcount_arr[j] = 0\n\t\tend\n\t\tj += 1\n\tend\nend\ncount_arr.reject!{|a| a == 0}\nif count_arr[0] > 1\n\tputs \"NO\"\nelse\n\tputs \"YES\"\nend"}, {"source_code": "\ndef solve\n\th = {}\n\tgets\n\ta = gets.chomp.split(/ /)\n\ta.each do |x| h[x] = if h[x] == nil then 1 else h[x] + 1 end end\n\th.each do |x, y| return 'NO' if y > a.length/2 + a.length%2 end\n\t'YES'\nend\n\np solve"}, {"source_code": "n = gets[0..-2].to_i\narr = gets.split(' ')\na = Array.new(n) { |i| arr[i].to_i }\nmark = Array.new(1001, 0)\nans = true\nfor i in 0...n do\n mark[a[i]] += 1\n if mark[a[i]] > n / 2\n ans = false\n break\n end\nend\n\nmax = mark.max\nif max * 2 - n >= 2\n print \"NO\"\nelse\n print \"YES\"\nend\n"}], "src_uid": "2c58d94f37a9dd5fb09fd69fc7788f0d"} {"nl": {"description": "The great hero guards the country where Homer lives. The hero has attack power $$$A$$$ and initial health value $$$B$$$. There are $$$n$$$ monsters in front of the hero. The $$$i$$$-th monster has attack power $$$a_i$$$ and initial health value $$$b_i$$$. The hero or a monster is said to be living, if his or its health value is positive (greater than or equal to $$$1$$$); and he or it is said to be dead, if his or its health value is non-positive (less than or equal to $$$0$$$).In order to protect people in the country, the hero will fight with monsters until either the hero is dead or all the monsters are dead. In each fight, the hero can select an arbitrary living monster and fight with it. Suppose the $$$i$$$-th monster is selected, and the health values of the hero and the $$$i$$$-th monster are $$$x$$$ and $$$y$$$ before the fight, respectively. After the fight, the health values of the hero and the $$$i$$$-th monster become $$$x-a_i$$$ and $$$y-A$$$, respectively. Note that the hero can fight the same monster more than once.For the safety of the people in the country, please tell them whether the great hero can kill all the monsters (even if the great hero himself is dead after killing the last monster).", "input_spec": "Each test contains multiple test cases. The first line contains $$$t$$$ ($$$1 \\le t \\le 10^5$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains three integers $$$A$$$ ($$$1 \\leq A \\leq 10^6$$$), $$$B$$$ ($$$1 \\leq B \\leq 10^6$$$) and $$$n$$$ ($$$1 \\leq n \\leq 10^5$$$) — the attack power of the great hero, the initial health value of the great hero, and the number of monsters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\leq a_i \\leq 10^6$$$), where $$$a_i$$$ denotes the attack power of the $$$i$$$-th monster. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \\dots, b_n$$$ ($$$1 \\leq b_i \\leq 10^6$$$), where $$$b_i$$$ denotes the initial health value of the $$$i$$$-th monster. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.", "output_spec": "For each test case print the answer: \"YES\" (without quotes) if the great hero can kill all the monsters. Otherwise, print \"NO\" (without quotes).", "sample_inputs": ["5\n3 17 1\n2\n16\n10 999 3\n10 20 30\n100 50 30\n1000 1000 4\n200 300 400 500\n1000 1000 1000 1000\n999 999 1\n1000\n1000\n999 999 1\n1000000\n999"], "sample_outputs": ["YES\nYES\nYES\nNO\nYES"], "notes": "NoteIn the first example: There will be $$$6$$$ fights between the hero and the only monster. After that, the monster is dead and the health value of the hero becomes $$$17 - 6 \\times 2 = 5 > 0$$$. So the answer is \"YES\", and moreover, the hero is still living.In the second example: After all monsters are dead, the health value of the hero will become $$$709$$$, regardless of the order of all fights. So the answer is \"YES\".In the third example: A possible order is to fight with the $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd and $$$4$$$-th monsters. After all fights, the health value of the hero becomes $$$-400$$$. Unfortunately, the hero is dead, but all monsters are also dead. So the answer is \"YES\".In the fourth example: The hero becomes dead but the monster is still living with health value $$$1000 - 999 = 1$$$. So the answer is \"NO\"."}, "positive_code": [{"source_code": "gets.to_i.times do\n d, h, n = gets.split.map &:to_i\n a = gets.split.map(&:to_i).zip(gets.split.map(&:to_i)).sort{ |a, b|\n a[0] <=> b[0]\n }\n catch :no do\n i = 0\n while i < n\n h -= (a[i][1] + d - 1)/d*a[i][0]\n if h <= 0 && (i < n - 1 || h <= -a[i][0])\n puts \"NO\"\n throw :no\n end\n i += 1\n end\n puts \"YES\"\n end\nend\n"}, {"source_code": "gets.to_i.times do\r\n aa,bb,n=gets.split.map(&:to_i)\r\n a=gets.split.map(&:to_i)\r\n b=gets.split.map(&:to_i)\r\n bb+=a.max\r\n n.times do |i|\r\n bb-=a[i]*((b[i]-1)/aa+1)\r\n if bb<=0\r\n break\r\n end\r\n end\r\n if bb<=0\r\n puts \"NO\"\r\n else\r\n puts \"YES\"\r\n end\r\nend\r\n"}], "negative_code": [], "src_uid": "b63a6369023642a8e7e8f449d7d4b73f"} {"nl": {"description": "Bob is playing with $$$6$$$-sided dice. A net of such standard cube is shown below.He has an unlimited supply of these dice and wants to build a tower by stacking multiple dice on top of each other, while choosing the orientation of each dice. Then he counts the number of visible pips on the faces of the dice.For example, the number of visible pips on the tower below is $$$29$$$ — the number visible on the top is $$$1$$$, from the south $$$5$$$ and $$$3$$$, from the west $$$4$$$ and $$$2$$$, from the north $$$2$$$ and $$$4$$$ and from the east $$$3$$$ and $$$5$$$.The one at the bottom and the two sixes by which the dice are touching are not visible, so they are not counted towards total.Bob also has $$$t$$$ favourite integers $$$x_i$$$, and for every such integer his goal is to build such a tower that the number of visible pips is exactly $$$x_i$$$. For each of Bob's favourite integers determine whether it is possible to build a tower that has exactly that many visible pips.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 1000$$$) — the number of favourite integers of Bob. The second line contains $$$t$$$ space-separated integers $$$x_i$$$ ($$$1 \\leq x_i \\leq 10^{18}$$$) — Bob's favourite integers.", "output_spec": "For each of Bob's favourite integers, output \"YES\" if it is possible to build the tower, or \"NO\" otherwise (quotes for clarity).", "sample_inputs": ["4\n29 34 19 38"], "sample_outputs": ["YES\nYES\nYES\nNO"], "notes": "NoteThe first example is mentioned in the problem statement.In the second example, one can build the tower by flipping the top dice from the previous tower.In the third example, one can use a single die that has $$$5$$$ on top.The fourth example is impossible."}, "positive_code": [{"source_code": "require 'set'; require 'prime'\nINF=Float::INFINITY; MOD=10**9+7\nt = gets.chomp.to_i\na = gets.chomp.split.map(&:to_i)\n\na.each do |e|\n ee = e % 14\n ee += 14\n if e > 14 && 15 <= ee && ee <= 20\n puts 'YES'\n else\n puts 'NO'\n end\nend\n"}, {"source_code": "require 'set'; require 'prime'\nINF=Float::INFINITY; MOD=10**9+7\nt = gets.chomp.to_i\na = gets.chomp.split.map(&:to_i)\n\na.each do |e|\n ee = e % 14\n ee += 14\n if e > 14 && 15 <= ee && ee <= 20\n puts 'YES'\n else\n puts 'NO'\n end\nend\n"}], "negative_code": [{"source_code": "require 'set'; require 'prime'\nINF=Float::INFINITY; MOD=10**9+7\nt = gets.chomp.to_i\na = gets.chomp.split.map(&:to_i)\n\na.each do |e|\n ee = e % 14\n ee += 14\n if e <= 14 || (15 <= ee && ee <= 20)\n puts 'YES'\n else\n puts 'NO'\n end\nend\n"}, {"source_code": "require 'set'; require 'prime'\nINF=Float::INFINITY; MOD=10**9+7\nt = gets.chomp.to_i\na = gets.chomp.split.map(&:to_i)\n\na.each do |e|\n e = e % 14\n e += 14\n if 15 <= e && e <= 20\n puts 'YES'\n else\n puts 'NO'\n end\nend\n"}], "src_uid": "840a4e16454290471faa5a27a3c795d9"} {"nl": {"description": "A string is called diverse if it contains consecutive (adjacent) letters of the Latin alphabet and each letter occurs exactly once. For example, the following strings are diverse: \"fced\", \"xyz\", \"r\" and \"dabcef\". The following string are not diverse: \"az\", \"aa\", \"bad\" and \"babc\". Note that the letters 'a' and 'z' are not adjacent.Formally, consider positions of all letters in the string in the alphabet. These positions should form contiguous segment, i.e. they should come one by one without any gaps. And all letters in the string should be distinct (duplicates are not allowed).You are given a sequence of strings. For each string, if it is diverse, print \"Yes\". Otherwise, print \"No\".", "input_spec": "The first line contains integer $$$n$$$ ($$$1 \\le n \\le 100$$$), denoting the number of strings to process. The following $$$n$$$ lines contains strings, one string per line. Each string contains only lowercase Latin letters, its length is between $$$1$$$ and $$$100$$$, inclusive.", "output_spec": "Print $$$n$$$ lines, one line per a string in the input. The line should contain \"Yes\" if the corresponding string is diverse and \"No\" if the corresponding string is not diverse. You can print each letter in any case (upper or lower). For example, \"YeS\", \"no\" and \"yES\" are all acceptable.", "sample_inputs": ["8\nfced\nxyz\nr\ndabcef\naz\naa\nbad\nbabc"], "sample_outputs": ["Yes\nYes\nYes\nYes\nNo\nNo\nNo\nNo"], "notes": null}, "positive_code": [{"source_code": "\nn = gets.to_i\n\nn.times do |r|\n string = gets.chomp\n freq = Array.new(26, 0)\n\n string_size = string.size\n\n string_size.times do |i|\n freq[string[i].ord - \"a\".ord] += 1\n end\n\n chain_count = 0\n in_chain = false\n printed = false\n freq.each do |value|\n if value > 1\n puts 'No'\n printed = true\n break\n end\n if value == 1\n if !in_chain\n chain_count += 1\n in_chain = true\n end\n if chain_count >= 2\n puts 'No'\n printed = true\n break\n end\n end\n if value == 0\n if in_chain\n in_chain = false\n end\n end\n end\n if !printed && chain_count <= 1\n puts 'Yes'\n end\nend\n"}, {"source_code": "require 'set'\nalph = \"abcdefghijklmnopqrstuvwxyz\"\nt = gets.chomp.to_i\nt.times do |testcase|\n inp = gets.chomp\n s = []\n inp.size.times do |i|\n s.push(inp[i])\n end\n s = s.sort\n flag = true\n (inp.size-1).times do |i|\n if alph.index(s[i]) != alph.index(s[i+1])-1\n flag = false\n break\n end\n end\n if flag\n puts \"yES\"\n else\n puts \"nO\"\n end\nend"}, {"source_code": "n = gets.to_i\nn.times do\n ss = gets.chomp.chars.map(&:ord).sort\n unless ss.size == ss.uniq.size\n puts 'No'\n next\n end\n\n ans = 'Yes'\n (0..ss.size-2).each do |i|\n if ss[i] + 1 != ss[i+1]\n ans = 'No'\n end\n end\n puts ans\nend\n"}, {"source_code": "t = \"abcdefghijklmnopqrstuvwxyz\".split(\"\")\ngets.to_i.times do\n s = gets.chomp.split(\"\").sort\n n = s.size\n f = false\n (n-1).times do |i|\n if s[i] == s[i+1]\n puts \"No\"\n f = true\n break\n end\n end\n next if f\n (27-n).times do |i|\n if s == t[i,n]\n puts \"Yes\"\n f = true\n break\n end\n end\n next if f\n puts \"No\"\nend"}, {"source_code": "alphabet = \"abcdefghijklmnopqrstuvwxyz\"\nt = gets.to_i\n\nt.times do\n word = gets.chomp\n puts alphabet.include?(word.chars.sort.join) ? \"Yes\" : \"No\"\nend\n"}, {"source_code": "\nn = gets.strip.to_i\ninputs = []\n\n(1..n).each do |i|\n inputs << gets.strip\nend\n\nres = [\"Yes\"]*n\ninputs.each_with_index do |a, index|\n codes = a.chars.map(&:ord).sort\n (1..codes.size-1).each do |i|\n if codes[i] - codes[i-1] != 1\n res[index] = \"No\" \n break\n end\n \n end\nend\n\nres.each do |r|\n puts r\nend\n"}, {"source_code": "n = gets.to_i\nn.times do\n s = gets.strip.split('').sort.join\n puts s == (s[0]..(2..s.size).inject(s[0]) { |a| a.next }).to_a.join ? 'Yes' : 'No'\nend\n"}], "negative_code": [{"source_code": "\nn = gets.to_i\n\nn.times do |r|\n string = gets.chomp\n freq = Array.new(26, 0)\n\n string_size = string.size\n\n string_size.times do |i|\n freq[string[i].ord - \"a\".ord] += 1\n end\n\n chain_count = 0\n in_chain = false\n printed = false\n freq.each do |value|\n if value > 1\n p 'No'\n printed = true\n break\n end\n if value == 1\n if !in_chain\n chain_count += 1\n in_chain = true\n end\n if chain_count >= 2\n p 'No'\n printed = true\n break\n end\n end\n if value == 0\n if in_chain\n in_chain = false\n end\n end\n end\n if !printed && chain_count <= 1\n puts 'Yes'\n end\nend"}, {"source_code": "\nn = gets.to_i\n\nn.times do |r|\n string = gets.chomp\n freq = Array.new(26, 0)\n\n string_size = string.size\n\n string_size.times do |i|\n freq[string[i].ord - \"a\".ord] += 1\n end\n\n chain_count = 0\n in_chain = false\n printed = false\n freq.each do |value|\n if value > 1\n p \"No\"\n printed = true\n break\n end\n if value == 1\n if !in_chain\n chain_count += 1\n in_chain = true\n end\n if chain_count >= 2\n p \"No\"\n printed = true\n break\n end\n end\n if value == 0\n if in_chain\n in_chain = false\n end\n end\n end\n if !printed && chain_count <= 1\n p \"Yes\"\n end\nend\n"}, {"source_code": "n = gets.to_i\nn.times do\n ss = gets.chomp.chars.map(&:ord).sort\n puts 'No' unless ss.size == ss.uniq.size\n\n ans = 'Yes'\n (0..ss.size-2).each do |i|\n if ss[i] + 1 != ss[i+1]\n ans = 'No'\n end\n end\n puts ans\nend\n"}, {"source_code": "\nn = gets.strip.to_i\ninputs = []\n\n(1..n).each do |i|\n inputs << gets.strip\nend\n\nres = [\"Yes\"]*n\ninputs.each_with_index do |a, index|\n codes = a.chars.map(&:ord).sort\n (1..codes.size-1).each do |i|\n if codes[i] - codes[i-1] != 1\n res[index] = \"No\" \n break\n end\n \n end\nend\n\nres.each do |r|\n p r\nend\n"}], "src_uid": "02a94c136d3fb198025242e91264823b"} {"nl": {"description": "A car number in Berland consists of exactly n digits. A number is called beautiful if it has at least k equal digits. Vasya wants to change the digits in his car's number so that the number became beautiful. To replace one of n digits Vasya has to pay the sum of money, equal to the absolute difference between the old digit and the new one.Help Vasya: find the minimum sum of money he should pay to make the number of his car beautiful. You should also find the resulting beautiful number. If there are several such numbers, then print the lexicographically minimum one.", "input_spec": "The first line contains two space-separated integers n and k (2 ≤ n ≤ 104, 2 ≤ k ≤ n) which represent how many digits the number has and how many equal digits a beautiful number should have. The second line consists of n digits. It describes the old number of Vasya's car. It is guaranteed that the number contains no spaces and only contains digits.", "output_spec": "On the first line print the minimum sum of money Vasya needs to change the number. On the second line print the car's new number. If there are several solutions, print the lexicographically minimum one.", "sample_inputs": ["6 5\n898196", "3 2\n533", "10 6\n0001112223"], "sample_outputs": ["4\n888188", "0\n533", "3\n0000002223"], "notes": "NoteIn the first sample replacing the second digit with an \"8\" costs |9 - 8| = 1. Replacing the fifth digit with an \"8\" costs the same. Replacing the sixth digit costs |6 - 8| = 2. As a result, Vasya will pay 1 + 1 + 2 = 4 for a beautiful number \"888188\".The lexicographical comparison of strings is performed by the < operator in modern programming languages. The string x is lexicographically smaller than the string y, if there exists such i (1 ≤ i ≤ n), that xi < yi, and for any j (1 ≤ j < i) xj = yj. The strings compared in this problem will always have the length n."}, "positive_code": [{"source_code": "k=gets.split[1].to_i\na=gets.split[0].bytes.to_a\nn=a.length\ncnt=Array.new(10,0)\nn.times{|i|\n a[i]-=48\n cnt[a[i]]+=1\n}\nmincost=99999999\nres=[]\n10.times{|d|\n cost=0\n count=cnt[d]\n 1.upto(9){|i|\n if count>=k then\n break\n end\n if d+i<=9 then\n if cnt[d+i]+count>=k then\n cost+=i*(k-count)\n count=k\n else\n count+=cnt[d+i]\n cost+=cnt[d+i]*i\n end\n end\n if d-i>=0 then\n if cnt[d-i]+count>=k then\n cost+=i*(k-count)\n count=k\n else\n count+=cnt[d-i]\n cost+=cnt[d-i]*i\n end\n end\n }\n if cost=k then\n break\n end\n if used[j]==0 && a[j]==tar then\n ans[j]=d\n count+=1\n used[j]=true\n end\n }\n tar=d-i\n (n-1).downto(0){|j|\n if count>=k then\n break\n end\n if used[j]==0 && a[j]==tar then\n ans[j]=d\n count+=1\n used[j]=true\n end\n }\n }\n n.times{|j|\n if used[j]==0 then\n ans[j]=a[j]\n end\n }\n res.push(ans)\n end\n}\nputs mincost\nans=res.min\nans.each{|i| print i}\nputs"}, {"source_code": "k=gets.split[1].to_i\na=gets.split[0].bytes.to_a\nn=a.length\ncnt=Array.new(10,0)\nn.times{|i|\n a[i]-=48\n cnt[a[i]]+=1\n}\nmincost=99999999\nres=[]\n10.times{|d|\n cost=0\n count=cnt[d]\n 1.upto(9){|i|\n if count>=k then\n break\n end\n if d+i<=9 then\n if cnt[d+i]+count>=k then\n cost+=i*(k-count)\n count=k\n else\n count+=cnt[d+i]\n cost+=cnt[d+i]*i\n end\n end\n if d-i>=0 then\n if cnt[d-i]+count>=k then\n cost+=i*(k-count)\n count=k\n else\n count+=cnt[d-i]\n cost+=cnt[d-i]*i\n end\n end\n }\n if cost=k then\n break\n end\n if used[j]==0 && a[j]==tar then\n ans[j]=d\n count+=1\n used[j]=true\n end\n }\n tar=d-i\n (n-1).downto(0){|j|\n if count>=k then\n break\n end\n if used[j]==0 && a[j]==tar then\n ans[j]=d\n count+=1\n used[j]=true\n end\n }\n }\n n.times{|j|\n if used[j]==0 then\n ans[j]=a[j]\n end\n }\n res.push(ans)\n end\n}\nputs mincost\nans=res.min\nans.each{|i| print i}\nputs"}], "negative_code": [], "src_uid": "cb082cbe9b34a45da851b6764bbc30c3"} {"nl": {"description": "Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $$$a$$$ of $$$n$$$ non-negative integers.Dark created that array $$$1000$$$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $$$k$$$ ($$$0 \\leq k \\leq 10^{9}$$$) and replaces all missing elements in the array $$$a$$$ with $$$k$$$.Let $$$m$$$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $$$|a_i - a_{i+1}|$$$ for all $$$1 \\leq i \\leq n - 1$$$) in the array $$$a$$$ after Dark replaces all missing elements with $$$k$$$.Dark should choose an integer $$$k$$$ so that $$$m$$$ is minimized. Can you help him?", "input_spec": "The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains one integer $$$n$$$ ($$$2 \\leq n \\leq 10^{5}$$$) — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$-1 \\leq a_i \\leq 10 ^ {9}$$$). If $$$a_i = -1$$$, then the $$$i$$$-th integer is missing. It is guaranteed that at least one integer is missing in every test case. It is guaranteed, that the sum of $$$n$$$ for all test cases does not exceed $$$4 \\cdot 10 ^ {5}$$$.", "output_spec": "Print the answers for each test case in the following format: You should print two integers, the minimum possible value of $$$m$$$ and an integer $$$k$$$ ($$$0 \\leq k \\leq 10^{9}$$$) that makes the maximum absolute difference between adjacent elements in the array $$$a$$$ equal to $$$m$$$. Make sure that after replacing all the missing elements with $$$k$$$, the maximum absolute difference between adjacent elements becomes $$$m$$$. If there is more than one possible $$$k$$$, you can print any of them.", "sample_inputs": ["7\n5\n-1 10 -1 12 -1\n5\n-1 40 35 -1 35\n6\n-1 -1 9 -1 3 -1\n2\n-1 -1\n2\n0 -1\n4\n1 -1 3 -1\n7\n1 -1 7 5 2 -1 5"], "sample_outputs": ["1 11\n5 35\n3 6\n0 42\n0 0\n1 2\n3 4"], "notes": "NoteIn the first test case after replacing all missing elements with $$$11$$$ the array becomes $$$[11, 10, 11, 12, 11]$$$. The absolute difference between any adjacent elements is $$$1$$$. It is impossible to choose a value of $$$k$$$, such that the absolute difference between any adjacent element will be $$$\\leq 0$$$. So, the answer is $$$1$$$.In the third test case after replacing all missing elements with $$$6$$$ the array becomes $$$[6, 6, 9, 6, 3, 6]$$$. $$$|a_1 - a_2| = |6 - 6| = 0$$$; $$$|a_2 - a_3| = |6 - 9| = 3$$$; $$$|a_3 - a_4| = |9 - 6| = 3$$$; $$$|a_4 - a_5| = |6 - 3| = 3$$$; $$$|a_5 - a_6| = |3 - 6| = 3$$$. So, the maximum difference between any adjacent elements is $$$3$$$."}, "positive_code": [{"source_code": "T = gets.to_i\n\nT.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n\n cand = Hash.new(false)\n max_diff = 0\n\n n.times do |i|\n v = a[i]\n\n if v == -1\n cand[a[i - 1]] = true if i > 0 && a[i - 1] != -1\n cand[a[i + 1]] = true if i < n - 1 && a[i + 1] != -1\n end\n\n if i < n - 1 && v != -1 && a[i + 1] != -1\n diff = (v - a[i + 1]).abs\n\n max_diff = diff if max_diff < diff\n end\n end\n\n nums = cand.keys.minmax.compact\n\n if nums.empty?\n puts \"0 0\"\n else\n ave = Rational(nums.inject(:+), nums.size)\n\n c1 = ave.ceil\n c2 = ave.floor\n\n min_diff = Float::INFINITY\n\n d1 = nums.map { |x| (x - c1).abs }.max\n d2 = nums.map { |x| (x - c2).abs }.max\n\n if d1 > d2\n puts [[max_diff, d2].max, c2].join(' ')\n else\n puts [[max_diff, d1].max, c1].join(' ')\n end\n end\nend\n"}], "negative_code": [{"source_code": "T = gets.to_i\n\nT.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n\n cand = Hash.new(false)\n max_diff = 0\n\n n.times do |i|\n v = a[i]\n\n if v == -1\n cand[a[i - 1]] = true if i > 0 && a[i - 1] != -1\n cand[a[i + 1]] = true if i < n - 1 && a[i + 1] != -1\n end\n\n if i < n - 1 && v != -1 && a[i + 1] != -1\n diff = (v - a[i + 1]).abs\n\n max_diff = diff if max_diff < diff\n end\n end\n\n nums = cand.keys\n\n if nums.empty?\n puts \"0 0\"\n else\n ave = Rational(nums.inject(:+), nums.size)\n\n c1 = ave.ceil\n c2 = ave.floor\n\n min_diff = Float::INFINITY\n\n d1 = nums.map { |x| (x - c1).abs }.max\n d2 = nums.map { |x| (x - c2).abs }.max\n\n if d1 > d2\n puts [[max_diff, d2].max, c2].join(' ')\n else\n puts [[max_diff, d1].max, c1].join(' ')\n end\n end\nend\n"}], "src_uid": "8ffd80167fc4396788b745b53068c9d3"} {"nl": {"description": "Little Tanya decided to present her dad a postcard on his Birthday. She has already created a message — string s of length n, consisting of uppercase and lowercase English letters. Tanya can't write yet, so she found a newspaper and decided to cut out the letters and glue them into the postcard to achieve string s. The newspaper contains string t, consisting of uppercase and lowercase English letters. We know that the length of string t greater or equal to the length of the string s.The newspaper may possibly have too few of some letters needed to make the text and too many of some other letters. That's why Tanya wants to cut some n letters out of the newspaper and make a message of length exactly n, so that it looked as much as possible like s. If the letter in some position has correct value and correct letter case (in the string s and in the string that Tanya will make), then she shouts joyfully \"YAY!\", and if the letter in the given position has only the correct value but it is in the wrong case, then the girl says \"WHOOPS\".Tanya wants to make such message that lets her shout \"YAY!\" as much as possible. If there are multiple ways to do this, then her second priority is to maximize the number of times she says \"WHOOPS\". Your task is to help Tanya make the message.", "input_spec": "The first line contains line s (1 ≤ |s| ≤ 2·105), consisting of uppercase and lowercase English letters — the text of Tanya's message. The second line contains line t (|s| ≤ |t| ≤ 2·105), consisting of uppercase and lowercase English letters — the text written in the newspaper. Here |a| means the length of the string a.", "output_spec": "Print two integers separated by a space: the first number is the number of times Tanya shouts \"YAY!\" while making the message, the second number is the number of times Tanya says \"WHOOPS\" while making the message. ", "sample_inputs": ["AbC\nDCbA", "ABC\nabc", "abacaba\nAbaCaBA"], "sample_outputs": ["3 0", "0 3", "3 4"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/env ruby\nh=$<.map{|e|\n\ts=e.chomp\n\t[s,s.upcase].map{|f|f.bytes.each_with_object(Hash.new(0)){|e,s|s[e]+=1}}\n}\ny=[]\n[*65..90,*97..122].each{|e|\n\tx=[h[0][0][e],h[1][0][e]].min\n\ty<0\n\n\nif i==i.downcase\nk=bhash[i.upcase]\nif ahash[i]>=k\nwhoops+=k\nbhash[i.upcase]=0\nelse\nwhoops+=ahash[i]\nbhash[i.upcase]-=ahash[i]\nend\nelse\nk=bhash[i.downcase]\nif ahash[i]>=k\nwhoops+=k\nbhash[i.downcase]=0\nelse\nwhoops+=ahash[i]\nbhash[i.downcase]-=ahash[i]\nend\nend\n\n\nend\nend\n\n\nputs \"#{yay} #{whoops}\""}, {"source_code": "a=gets.chomp\nb=gets.chomp\nahash=Hash.new(0)\nbhash=Hash.new(0)\na.each_char {|i| ahash[i]+=1}\nb.each_char {|i| bhash[i]+=1}\nc=ahash.keys\nyay=0\nwhoops=0\n\nc.each do |i|\nif ahash[i]<=bhash[i]\nyay+=ahash[i]\nbhash[i]-=ahash[i]\nahash[i]=0\nelse\nyay+=bhash[i]\nahash[i]-=bhash[i]\nbhash[i]=0\nend\nend\n\nc.each do |i|\nif ahash[i]>0\n\n\nif i==i.downcase\nk=bhash[i.upcase]\nif ahash[i]>=k\nwhoops+=k\nbhash[i.upcase]=0\nelse\nwhoops+=ahash[i]\nbhash[i.upcase]-=ahash[i]\nend\nelse\nk=bhash[i.downcase]\nif ahash[i]>=k\nwhoops+=k\nbhash[i.downcase]=0\nelse\nwhoops+=ahash[i]\nbhash[i.downcase]-=ahash[i]\nend\nend\n\n\nend\nend\n\n\nputs \"#{yay} #{whoops}\""}, {"source_code": "s = gets.chomp\nt = gets.chomp\nss = Array.new(256, 0)\ntt = Array.new(256, 0)\ns.each_byte do |x|\n ss[x] += 1\nend\nt.each_byte do |x|\n tt[x] += 1\nend\nsu = ss['A'.ord..'Z'.ord]\nsl = ss['a'.ord..'z'.ord]\ntu = tt['A'.ord..'Z'.ord]\ntl = tt['a'.ord..'z'.ord]\n\na = 0\nb = 0\n26.times do |i|\n x = [su[i], tu[i]].min\n a += x\n tu[i] -= x\n su[i] -= x\n x = [sl[i], tl[i]].min\n a += x\n tl[i] -= x\n sl[i] -= x\nend\n26.times do |i|\n x = [su[i], tl[i]].min\n b += x\n tl[i] -= x\n su[i] -= x\n x = [sl[i], tu[i]].min\n b += x\n tu[i] -= x\n sl[i] -= x\nend\n\nputs \"#{a} #{b}\"\n"}, {"source_code": "s = gets.chomp.chars.sort\nt = gets.chomp.chars.sort\nss = Array.new(58, 0)\ntt = Array.new(58, 0)\ns.each do |x|\n ss[x.ord - 65] += 1\nend\nt.each do |x|\n tt[x.ord - 65] += 1\nend\nsu = ss[0..25]\nsl = ss[32..57]\ntu = tt[0..25]\ntl = tt[32..57]\n\na = 0\nb = 0\n26.times do |i|\n x = [su[i], tu[i]].min\n a += x\n tu[i] -= x\n su[i] -= x\n x = [sl[i], tl[i]].min\n a += x\n tl[i] -= x\n sl[i] -= x\nend\n26.times do |i|\n x = [su[i], tl[i]].min\n b += x\n tl[i] -= x\n su[i] -= x\n x = [sl[i], tu[i]].min\n b += x\n tu[i] -= x\n sl[i] -= x\nend\n\nputs \"#{a} #{b}\"\n"}, {"source_code": "s = gets\nt = gets\n\nhs = Hash.new(0)\ns.split('').each { |c| hs[ c ] += 1 }\n\nht = Hash.new(0)\nt.split('').each { |c| ht[ c ] += 1 }\n\na = 0\nb = 0\n\nhs.each do |k, v|\n m = [hs[k], ht[k]].min\n a += m\n hs[k] -= m\n ht[k] -= m\nend\n\nhs.each do |k, v|\n m = [hs[k], ht[ k.downcase ]].min\n b += m\n hs[ k ] -= m\n ht[ k.downcase ] += m\n\n m = [hs[k], ht[ k.upcase ]].min\n b += m\n hs[ k ] -= m\n ht[ k.upcase ] += m\nend\n\nputs \"#{a - 1} #{b}\""}, {"source_code": "s = gets.chomp\nt = gets.chomp\nn = t.size()\nm = s.size()\na=Array.new(300).map{|e| e=0 }\nb=Array.new(300).map{|e| e=0 }\n for i in 0..n-1\n a[t[i].ord] += 1\n end\n for i in 0..m-1\n b[s[i].ord] += 1\n end\nyra = 0\noops = 0\nfor i in 0..200\n if a[i] > 0 && b[i] > 0\n a1 = a[i]\n b1 = b[i]\n # puts (a1.to_s + ' '+ b1.to_s + ' ' + i.to_s)\n yra += [a[i], b[i]].min\n a[i] -= b1\n b[i] -= a1\n end\n end\n for i in 64..91\n if (a[i] > 0 && b[i + 32] > 0)\n a1 = a[i]\n b1 = b[i + 32]\n oops += [a1, b1].min\n a[i] -= b1\n b[i] -= a1\n end\n end\n for i in 97..123\n if (a[i] > 0 && b[i - 32] > 0)\n a1 = a[i]\n b1 = b[i - 32]\n oops += [a1, b1].min\n a[i] -= b1\n b[i] -= a1\n end\n end\n s2 = \"\"\n s2 = yra.to_s + \" \" + oops.to_s\nputs (s2)"}, {"source_code": "if __FILE__ == $0\n END { main }\nend\n\ndef main\n s = gets.strip\n t = gets.strip\n\n s_freqs = get_letter_freqs(s)\n t_freqs = get_letter_freqs(t)\n\n yay_count = get_yay_count(s_freqs, t_freqs)\n whoops_count = get_whoops_count(s_freqs, t_freqs)\n\n puts [yay_count, whoops_count].join(\" \")\nend\n\ndef get_letter_freqs(s)\n h = Hash.new(0)\n s.chars.each do |ch|\n h[ch] += 1\n end\n h\nend\n\ndef get_yay_count(s_freqs, t_freqs)\n count = 0\n s_freqs.each do |ch, freq|\n freq.times do\n if t_freqs[ch] > 0\n s_freqs[ch] -= 1\n t_freqs[ch] -= 1\n count += 1\n end\n end\n end\n count\nend\n\ndef get_whoops_count(s_freqs, t_freqs)\n count = 0\n s_freqs.each do |s_ch, s_freq|\n t_ch = s_ch.swapcase\n s_freq.times do\n if t_freqs[t_ch] > 0\n s_freqs[s_ch] -= 1\n t_freqs[t_ch] -= 1\n count += 1\n end\n end\n end\n count\nend\n"}], "negative_code": [{"source_code": "a=gets.chomp\nb=gets.chomp\nahash=Hash.new(0)\nbhash=Hash.new(0)\na.each_char {|i| ahash[i]+=1}\nb.each_char {|i| bhash[i]+=1}\nc=ahash.keys\nyay=0\nwhoops=0\nc.each do |i|\nif ahash[i]<=bhash[i]\nyay+=ahash[i]\nelse\nyay+=bhash[i]\nif i=i.downcase\np=(ahash[i]-bhash[i])\nk=bhash[i.upcase]\nif p>=k\nwhoops+=k\nelse\nwhoops+=p\nend\nelse\np=(ahash[i]-bhash[i])\nk=bhash[i.dowbcase]\nif p>=k\nwhoops+=k\nelse\nwhoops+=p\nend\nend\nend\nend\n\nputs \"#{yay} #{whoops}\""}, {"source_code": "a=gets.chomp\nb=gets.chomp\nahash=Hash.new(0)\nbhash=Hash.new(0)\na.each_char {|i| ahash[i]+=1}\nb.each_char {|i| bhash[i]+=1}\nc=ahash.keys\nyay=0\nwhoops=0\nc.each do |i|\nif ahash[i]<=bhash[i]\nyay+=ahash[i]\nelse\nyay+=bhash[i]\nwhoops+=(ahash[i]-bhash[i])\nend\nend\nputs \"#{yay} #{whoops}\""}, {"source_code": "a=gets.chomp\nb=gets.chomp\nahash=Hash.new(0)\nbhash=Hash.new(0)\na.each_char {|i| ahash[i]+=1}\nb.each_char {|i| bhash[i]+=1}\nc=ahash.keys\nyay=0\nwhoops=0\nc.each do |i|\nif ahash[i]<=bhash[i]\nyay+=ahash[i]\nelse\nyay+=bhash[i]\nif i==i.downcase\np=(ahash[i]-bhash[i])\nk=bhash[i.upcase]\nif p>=k\nwhoops+=k\nelse\nwhoops+=p\nend\nelse\np=(ahash[i]-bhash[i])\nk=bhash[i.downcase]\nif p>=k\nwhoops+=k\nelse\nwhoops+=p\nend\nend\nend\nend\n\nputs \"#{yay} #{whoops}\""}, {"source_code": "s = gets\nt = gets\nn = t.size()\nm = s.size()\na=Array.new(300).map{|e| e=0 }\nb=Array.new(300).map{|e| e=0 }\n for i in 0..n-1\n a[t[i].ord] += 1\n end\n for i in 0..m-1\n b[s[i].ord] += 1\n end\nyra = 0\noops = 0\nfor i in 0..200\n if a[i] > 0 && b[i] > 0\n a1 = a[i]\n b1 = b[i]\n yra += [a[i], b[i]].min\n a[i] -= b1\n b[i] -= a1\n end\n end\n for i in 62..94\n if (a[i] > 0 && b[i + 32] > 0)\n a1 = a[i]\n b1 = b[i + 32]\n oops += [a1, b1].min\n a[i] -= b1\n b[i] -= a1\n end\n end\n for i in 95..125\n if (a[i] > 0 && b[i - 32] > 0)\n a1 = a[i]\n b1 = b[i - 32]\n oops += [a1, b1].min\n a[i] -= b1\n b[i] -= a1\n end\n end\n s2 = \"\"\n s2 = yra.to_s + \" \" + oops.to_s\nputs (s2)"}, {"source_code": "s = gets\nt = gets\nn = t.size()\nm = s.size()\na=Array.new(300).map{|e| e=0 }\nb=Array.new(300).map{|e| e=0 }\n for i in 0..n-1\n a[t[i].ord] += 1\n end\n for i in 0..m-1\n b[s[i].ord] += 1\n end\nyra = 0\noops = 0\nfor i in 0..200\n if a[i] > 0 && b[i] > 0\n a1 = a[i]\n b1 = b[i]\n yra += [a[i], b[i]].min\n a[i] -= b1\n b[i] -= a1\n end\n end\n for i in 64..91\n if (a[i] > 0 && b[i + 32] > 0)\n a1 = a[i]\n b1 = b[i + 32]\n oops += [a1, b1].min\n a[i] -= b1\n b[i] -= a1\n end\n end\n for i in 97..123\n if (a[i] > 0 && b[i - 32] > 0)\n a1 = a[i]\n b1 = b[i - 32]\n oops += [a1, b1].min\n a[i] -= b1\n b[i] -= a1\n end\n end\n s2 = \"\"\n s2 = yra.to_s + \" \" + oops.to_s\nputs (s2)"}, {"source_code": "if __FILE__ == $0\n END { main }\nend\n\ndef main\n s = gets.strip\n t = gets.strip\n\n s_freqs = get_letter_freqs(s)\n t_freqs = get_letter_freqs(t)\n\n yay_count = get_yay_count(s_freqs, t_freqs)\n whoops_count = get_whoops_count(s_freqs, t_freqs)\n\n puts [yay_count, whoops_count].join(\" \")\nend\n\ndef get_letter_freqs(s)\n h = Hash.new(0)\n s.chars.each do |ch|\n h[ch] += 1\n end\n h\nend\n\ndef get_yay_count(s_freqs, t_freqs)\n count = 0\n s_freqs.each do |ch, freq|\n freq.times do\n if t_freqs[ch] > 0\n s_freqs[ch] += 1\n t_freqs[ch] -= 1\n count += 1\n end\n end\n end\n count\nend\n\ndef get_whoops_count(s_freqs, t_freqs)\n count = 0\n s_freqs.each do |s_ch, s_freq|\n t_ch = s_ch.swapcase\n s_freq.times do\n if t_freqs[t_ch] > 0\n s_freqs[s_ch] += 1\n t_freqs[t_ch] -= 1\n count += 1\n end\n end\n end\n count\nend\n"}], "src_uid": "96e2ba997eff50ffb805b6be62c56222"} {"nl": {"description": "Stepan has the newest electronic device with a display. Different digits can be shown on it. Each digit is shown on a seven-section indicator like it is shown on the picture below. So, for example, to show the digit 3 on the display, 5 sections must be highlighted; and for the digit 6, 6 sections must be highlighted. The battery of the newest device allows to highlight at most n sections on the display. Stepan wants to know the maximum possible integer number which can be shown on the display of his newest device. Your task is to determine this number. Note that this number must not contain leading zeros. Assume that the size of the display is enough to show any integer.", "input_spec": "The first line contains the integer n (2 ≤ n ≤ 100 000) — the maximum number of sections which can be highlighted on the display.", "output_spec": "Print the maximum integer which can be shown on the display of Stepan's newest device.", "sample_inputs": ["2", "3"], "sample_outputs": ["1", "7"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\nif n % 2 == 1 then\n\tres = '7'\n\tn -= 3\nelse\n\tres = '1'\n\tn -= 2\nend\nn = n / 2\ns = '1'*n\nres += s\nputs res"}, {"source_code": "n = gets\nn = n.to_i\nif n % 2 == 0\n print '1' * (n / 2)\nelse\n print '7' + '1' * (n / 2 - 1)\nend"}, {"source_code": "n = gets.to_i\nif (n & 1 != 0) then\n print \"7\"\n n = n-2\nend\n(1..n/2).each do |i|\n print \"1\"\nend"}, {"source_code": "n = gets.chomp.to_i\nif n % 2 == 1\n print 7\n n -= 3\nend\ni = 0\nwhile i < n do\n i += 2\n print 1\nend"}, {"source_code": "i = gets.to_i\nif i % 2 == 0\n c = i / 2\n answer = \"\"\n for counter in 1..c\n answer += \"1\"\n end\nelse\n c = i / 2 - 1\n answer = \"7\"\n for counter in 1..c\n answer += \"1\"\n end\nend\nputs answer\n"}, {"source_code": "puts (i=gets.chomp.to_i) % 2 == 0 ? '1'*(i/2) : '7'+('1'*((i-3)/2))"}, {"source_code": "n = gets.to_i\nif n % 2 == 0\n puts '1'*(n / 2)\nelse\n puts '7'+'1'*( (n - 3) / 2)\nend"}, {"source_code": "h = gets.to_i\nif h % 2 == 0\n puts '1'*(h / 2)\nelse\n puts '7'+'1'*( (h - 3) / 2)\nend"}, {"source_code": "puts (i=gets.chomp.to_i) % 2 == 0 ? '1'*(i/2) : '7'+('1'*((i-3)/2))"}, {"source_code": "n = gets.to_i\nif (n % 2 == 1)\n print(7)\n n = n - 3\nend\nwhile (n > 0)\n print(1)\n n = n - 2\nend"}, {"source_code": "a = gets\na = a.to_i\nif a > 1\n if a % 2 == 1\n print 7\n a -=3\n end\nend\nwhile a > 1\n print 1\n a -= 2\nend"}, {"source_code": "x = gets.chomp.to_i\ns = \"\"\nif(x % 2 == 0)\n\twhile(x > 0)\n\t\ts += \"1\"\n\t\tx-=2\n\tend\nend\nif x % 2 == 1\n\ts += \"7\"\n\tx-=3\n\twhile(x > 0)\n\t\ts += \"1\"\n\t\tx-=2\n\tend\nend\nputs s"}, {"source_code": "n = gets.to_i\n\nif n % 2 == 1\n ans = '7' + '1' * ((n - 3) / 2)\nelse\n ans = '1' * (n / 2)\nend\n\nputs ans\n"}, {"source_code": "n = gets.to_i\n\nans = \"1\" * (n/2)\nif n % 2 == 1\n ans[0]=\"7\"\nend\n\nputs ans"}, {"source_code": "n = gets.to_i\nstr = \"\"\nif n % 2 > 0\n print 7\nelse\n print 1\nend\n\nn -= 2\nn /= 2\nn.times do\n print 1 \nend"}, {"source_code": "num = gets.to_i\n\nrem = num % 2\nif rem == 1\n if num > 2\n print 7\n num = num - 3\nend\nend\nwhile num>0\nprint 1\nnum = num-2;\nend"}, {"source_code": "a = gets.to_i\n\nif a % 2 == 1\n print '7'\n a -= 3\nend\n\na = (a/2).to_i\n\nfor i in 1..a\n print 1\nend\n"}, {"source_code": "n1 = gets.chomp\nn = n1.to_i\nc = \"1\"*(n/2)\nd = \"1\"*(n/2 -1)\nif n%2==0\n print c\nelse\n print \"7\"\n print d\nend"}, {"source_code": "# your code goes here\nr = gets.strip.split(/\\s+/).map(&:to_i)\nt = r[0]\nif t % 2 == 1\n\tputs '7' + ('1' * ((t - 3) / 2))\nelse\n\tputs '1' * (t / 2)\nend"}, {"source_code": "a = gets.to_i\ns = '1' * ((a - 2) / 2)\nif (a & 1) == 0\n s = s + '1'\nelse\n s = '7' + s\nend\nputs s"}, {"source_code": "number = gets.to_i\n\nif number % 2 == 0\n\tl = number / 2\n\tlimit = l - 1\n\tfor counter in 0..limit\n\t\tprint \"1\"\n\tend\nend\nif number % 2 == 1\n\tl = number / 2\n\tlimit = l - 2;\n\tprint \"7\"\n\tfor counter in 0..limit\n\t\tprint \"1\"\n\tend\nend"}, {"source_code": "c = Integer(gets)\ns = \"\"\nif c % 2 == 1\n\ts += '7'\n\tc -= 3\nend\n(c/2).times do\n\ts += '1'\nend\nputs s"}, {"source_code": "n=gets.chomp.to_i\nz=n/2\nt=n%2\nans=''\nif t==1\n z=z-1\n ans+='7'\nend\nfor i in 1..z \n ans+='1'\nend\nputs ans\n"}, {"source_code": "#!/bin/ruby\nt = gets.strip.to_i\n#for a0 in (0..t-1)\n# n = gets.strip.to_i\n#end\ns=\"\"\nb=t\nwhile t>7\n t-=2\n s.concat(\"1\")\nend\nif t==2\n s=\"1\" \nend\nif t==3\n s=\"7\"\nend\nif t==4\n s=\"11\"\nend\nif t==5\n s=\"71\"\nend\nif t==6\n s.concat(\"1\")\n s.concat(\"1\")\n s.concat(\"1\")\nend\nif t==7\n s=\"7\"\n while b>7\n b-=2\n s.concat(\"1\")\n end\n s.concat(\"1\")\n s.concat(\"1\")\n #s.concat(\"1\")\n #s[0]=7\nend\n\nprint s,\"\\n\""}, {"source_code": "r=->{gets.split.map &:to_i}\nns=r[]\nn = ns[0]\nif n % 2 == 0 then\n\tputs \"1\"*(n/2)\nelse\n\tputs \"7\" + \"1\"*(n/2-1)\nend\n"}, {"source_code": "x=gets.to_i\ny=x%2\nx=x/2-y\nputs \"7\"*y+\"1\"*x\n"}, {"source_code": "a = gets.chomp.to_i\nb = a / 2\nc = a % 2\n\nif c == 1\n puts '7' + '1' * (b - 1)\nelse\n puts '1' * b\nend\n"}], "negative_code": [{"source_code": "n = gets\nn = n.to_i\nif n % 2 == 0\n print '1' * (n / 2)\nelse\n if n == 3\n print '17'\n else\n print '1' * ((n / 2) - 1) + '7'\n end\nend"}, {"source_code": "n = gets\nn = n.to_i\nif n % 2 == 0\n print '1' * (n / 2)\nelse\n print '1' * (n / 2 - 1) + '7'\nend"}, {"source_code": "n = gets\nn = n.to_i\nif n % 2 == 0\n print '1' * (n / 2)\nelse\n puts '1' * ((n / 2) - 1) + '7'\nend"}, {"source_code": "c = Integer(gets)\nwhile c >= 6\n\tprint('9')\n\t$stdout.flush\n\tc -= 6\nend\nif c >= 3\n\tprint('7')\n\t$stdout.flush\nelsif c == 2\n\tprint('1')\n\t$stdout.flush\nend\nputs ''"}, {"source_code": "c = Integer(gets)\nif c == 3\n\tprint '7'\n\t$stdout.flush\nelse\n\t(c/2).times do\n\t\tprint '1'\n\t\t$stdout.flush\n\tend\nend"}, {"source_code": "a=[6,2,5,5,4,5,6,3,7,6]\nn=gets.chomp.to_i\nans=0\nwhile n>=2 do\n i=9\n while i>=0 do\n if a[i]<=n \n ans+=ans*10+i\n n=n-a[i]\n end \n i=i-1\n end\nend\nputs ans\n"}], "src_uid": "4ebea3f56ffd43efc9e1496a0ef7fa2d"} {"nl": {"description": "Polycarp has a strict daily schedule. He has n alarms set for each day, and the i-th alarm rings each day at the same time during exactly one minute.Determine the longest time segment when Polycarp can sleep, i. e. no alarm rings in that period. It is possible that Polycarp begins to sleep in one day, and wakes up in another.", "input_spec": "The first line contains a single integer n (1 ≤ n ≤ 100) — the number of alarms. Each of the next n lines contains a description of one alarm. Each description has a format \"hh:mm\", where hh is the hour when the alarm rings, and mm is the minute of that hour when the alarm rings. The number of hours is between 0 and 23, and the number of minutes is between 0 and 59. All alarm times are distinct. The order of the alarms is arbitrary. Each alarm starts ringing in the beginning of the corresponding minute and rings for exactly one minute (i. e. stops ringing in the beginning of the next minute). Polycarp can start sleeping instantly when no alarm is ringing, and he wakes up at the moment when some alarm starts ringing.", "output_spec": "Print a line in format \"hh:mm\", denoting the maximum time Polycarp can sleep continuously. hh denotes the number of hours, and mm denotes the number of minutes. The number of minutes should be between 0 and 59. Look through examples to understand the format better.", "sample_inputs": ["1\n05:43", "4\n22:00\n03:21\n16:03\n09:59"], "sample_outputs": ["23:59", "06:37"], "notes": "NoteIn the first example there is only one alarm which rings during one minute of a day, and then rings again on the next day, 23 hours and 59 minutes later. Polycarp can sleep all this time."}, "positive_code": [{"source_code": "n=gets.to_i\nt=Array.new\nfor i in 1..n do\n s=gets\n t.push(s[0,2].to_i*60+s[3,2].to_i)\nend\nt.sort!\nmx=1439-t[n-1]+t[0]\nfor i in 1..n-1 do\n if t[i]-t[i-1]-1>mx then mx=t[i]-t[i-1]-1 end\nend\nif mx/60<10 then print 0 end\nprint mx/60\nprint ':'\nif mx%60<10 then print 0 end\nprint mx%60"}, {"source_code": "n = gets.to_i\ntimes = Array.new(n * 2, 0)\nfor i in 0..n-1\n v = gets.split(\":\")\n t = v[0].to_i * 60 + v[1].to_i\n times[i*2] = t\n times[i*2+1] = t + 1\nend\n\ntimes = times.sort\n#puts times\n\ntf = -1\ntl = 0\nres = 0\nfor i in 0..2*n-1\n t = times[i]\n if tf == -1\n tf = t\n elsif res < t - tl\n res = t - tl\n end\n tl = t\n #puts res\nend\nif res < 24 * 60 - tl + tf\n res = 24 * 60 - tl + tf\nend\nif (res / 60) < 10\n print \"0\"\nend\nprint (res / 60).to_s\nprint \":\"\nif (res % 60) < 10\n print \"0\"\nend\nprint (res % 60).to_s"}, {"source_code": "def strtime(str)\n h = str.split(':')[0].to_i\n m = str.split(':')[1].to_i\n h * 60 + m\nend\n\nn = STDIN.gets.chomp!.to_i\nt = (1..n).map { |i| strtime(STDIN.gets.chomp!) }\nt.sort!\n\nans = 0\n(0...n).map do |i|\n d = t[(i + 1) % n] - t[i] - 1\n d = d + 24 * 60 if d < 0\n ans = [ans, d].max\nend\n\nprintf(\"%02d:%02d\\n\", ans / 60, ans % 60)\n"}, {"source_code": "n = gets.to_i\ntimes = []\n\nn.times do\n hh, mm = gets.split(':').map(&:to_i)\n time = hh * 60 + mm\n times.push(time)\n times.push(time + 24 * 60)\nend\n\ntimes.sort!\n\nt = 0\nans = 0\n\ntimes.each {\n |time|\n \n if time - t - 1 > ans\n ans = time - t - 1\n end\n \n t = time\n}\nhh, mm = ans / 60, ans % 60\nhh, mm = \"%02d\" % hh, \"%02d\" % mm\n\nputs \"#{hh}:#{mm}\"\n"}, {"source_code": "n=gets.to_i\nt=Array.new\nfor i in 1..n do\n s=gets\n t.push(s[0,2].to_i*60+s[3,2].to_i)\nend\nt.sort!\nmx=1439-t[n-1]+t[0]\nfor i in 1..n-1 do\n if t[i]-t[i-1]-1>mx then mx=t[i]-t[i-1]-1 end\nend\nif mx/60<10 then print 0 end\nprint mx/60\nprint ':'\nif mx%60<10 then print 0 end\nprint mx%60"}], "negative_code": [{"source_code": "n=gets.to_i\nt=Array.new\nfor i in 1..n do\n s=gets\n t.push(s[0,2].to_i*60+s[3,2].to_i)\nend\nt.sort!\nmx=1439-t[n-1]+t[0]-1\nif n==1 then mx+=1 end\nfor i in 1..n-1 do\n if t[i]-t[i-1]-1>mx then mx=t[i]-t[i-1]-1 end\nend\nif mx/60<10 then print 0 end\nprint mx/60\nprint ':'\nif mx%60<10 then print 0 end\nprint mx%60"}, {"source_code": "n = gets.to_i\ntimes = Array.new(n * 2, 0)\nfor i in 0..n-1\n v = gets.split(\":\")\n t = v[0].to_i * 60 + v[1].to_i\n times[i*2] = t\n times[i*2+1] = t + 1\nend\n\ntimes = times.sort\n#puts times\n\ntf = -1\ntl = 0\nres = 0\nfor i in 0..2*n-1\n t = times[i]\n if tf == -1\n tf = t\n elsif res < t - tl\n res = t - tl\n end\n tl = t\n #puts res\nend\nif res < 24 * 60 - tl + tf\n res = 24 * 60 - tl + tf\nend\nprint (res / 60).to_s + \":\" + (res % 60).to_s"}, {"source_code": "n = gets.to_i\ntimes = Array.new(n * 2)\ntf = -1\ntl = 0\nres = 200000\nfor i in 0..n-1\n v = gets.split(\":\")\n t = v[0].to_i * 60 + v[1].to_i\n if tf == -1\n tf = t\n elsif res > t - tl\n res = t - tl\n end\n tl = t + 1\nend\nif res > 24 * 60 - tl + tf\n res = 24 * 60 - tl + tf\nend\nprint (res / 60).to_s + \":\" + (res % 60).to_s"}], "src_uid": "c3b0b7194ce018bea9c0b9139e537a09"} {"nl": {"description": "Blake is a CEO of a large company called \"Blake Technologies\". He loves his company very much and he thinks that his company should be the best. That is why every candidate needs to pass through the interview that consists of the following problem.We define function f(x, l, r) as a bitwise OR of integers xl, xl + 1, ..., xr, where xi is the i-th element of the array x. You are given two arrays a and b of length n. You need to determine the maximum value of sum f(a, l, r) + f(b, l, r) among all possible 1 ≤ l ≤ r ≤ n. ", "input_spec": "The first line of the input contains a single integer n (1 ≤ n ≤ 1000) — the length of the arrays. The second line contains n integers ai (0 ≤ ai ≤ 109). The third line contains n integers bi (0 ≤ bi ≤ 109).", "output_spec": "Print a single integer — the maximum value of sum f(a, l, r) + f(b, l, r) among all possible 1 ≤ l ≤ r ≤ n.", "sample_inputs": ["5\n1 2 4 3 2\n2 3 3 12 1", "10\n13 2 7 11 8 4 9 8 5 1\n5 7 18 9 2 3 0 11 8 6"], "sample_outputs": ["22", "46"], "notes": "NoteBitwise OR of two non-negative integers a and b is the number c = a OR b, such that each of its digits in binary notation is 1 if and only if at least one of a or b have 1 in the corresponding position in binary notation.In the first sample, one of the optimal answers is l = 2 and r = 4, because f(a, 2, 4) + f(b, 2, 4) = (2 OR 4 OR 3) + (3 OR 3 OR 12) = 7 + 15 = 22. Other ways to get maximum value is to choose l = 1 and r = 4, l = 1 and r = 5, l = 2 and r = 4, l = 2 and r = 5, l = 3 and r = 4, or l = 3 and r = 5.In the second sample, the maximum value is obtained for l = 1 and r = 9."}, "positive_code": [{"source_code": "gets;p$<.map{|e|e.split.map(&:to_i).reduce(:|)}.reduce(:+)"}, {"source_code": "def f(a)\n b, *c = a\n c.each { |e| b |= e }\n\n b\nend\n\nn = gets.chomp.to_i\narr1 = gets.chomp.split.map(&:to_i)\narr2 = gets.chomp.split.map(&:to_i)\nputs f(arr1) + f(arr2)\n"}], "negative_code": [], "src_uid": "475239ff4ae3675354d2229aba081a58"} {"nl": {"description": "Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller.Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array.Help Amr by choosing the smallest subsegment possible.", "input_spec": "The first line contains one number n (1 ≤ n ≤ 105), the size of the array. The second line contains n integers ai (1 ≤ ai ≤ 106), representing elements of the array.", "output_spec": "Output two integers l, r (1 ≤ l ≤ r ≤ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. ", "sample_inputs": ["5\n1 1 2 2 1", "5\n1 2 2 3 1", "6\n1 2 2 1 1 2"], "sample_outputs": ["1 5", "2 3", "1 5"], "notes": "NoteA subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≤ i ≤ r - l + 1"}, "positive_code": [{"source_code": "#!/usr/bin/ruby\nh=Hash.new{|h,k|h[k]=[]}\ngets\ngets.split.each.with_index(1){|e,i|\n\th[e.to_i]< min_boy\n potential = min_boy / 2.0\nelse\n potential = min_girl\nend\n\nmax = [3 * n * potential, w].min\n\np max == max.to_i ? max.to_i : max\n\n[2,2,2,4,4,4]\n"}, {"source_code": "# http://codeforces.com/problemset/problem/557/B\nn, w = gets.chomp.split.map(&:to_i)\na = gets.chomp.split.map(&:to_i).sort\n\nmin_girl = a[0]\nmin_boy = a[n]\n\nif min_girl * 2 > min_boy\n potential = min_boy / 2.0\nelse\n potential = min_girl\nend\n\nmax = [3 * n * potential, w].min\n\np max\n"}], "negative_code": [{"source_code": "def read_ints; gets.chomp.split.map(&:to_i) end\nn,w = read_ints\na = read_ints\nx = [a.sort[n]/2.0,w/(3.0*n)].min\nputs 3*x*n\n"}, {"source_code": "# http://codeforces.com/problemset/problem/557/B\n\nn, w = gets.chomp.split.map(&:to_i)\na = gets.chomp.split.map(&:to_i).sort\n\ni = 2 * n - 1\nwhile i > 0\n if a[i] <= w\n break potential = a[i]\n end\n i -= 1\nend\n\nif potential\n x = potential / 2.0\n max = [3 * n * x, w].min\nelse\n max = w\nend\n\np max == max.to_i ? max.to_i : max\n"}, {"source_code": "# http://codeforces.com/problemset/problem/557/B\n\nn, w = gets.chomp.split.map(&:to_i)\na = gets.chomp.split.map(&:to_i)\n\nif a.max > w\n p w\nelse\n x = a.max / 2.0\n\n max = 3 * n * x\n\n p max == max.to_i ? max.to_i : max\nend\n"}, {"source_code": "# http://codeforces.com/problemset/problem/557/B\n\nn, w = gets.chomp.split.map(&:to_i)\na = gets.chomp.split.map(&:to_i)\n\nx = a.max / 2.0\n\nmax = 3 * n * x\n\np max == max.to_i ? max.to_i : max\n"}], "src_uid": "b2031a328d72b464f965b4789fd35b93"} {"nl": {"description": "Vasya has a sequence of cubes and exactly one integer is written on each cube. Vasya exhibited all his cubes in a row. So the sequence of numbers written on the cubes in the order from the left to the right equals to a1, a2, ..., an.While Vasya was walking, his little brother Stepan played with Vasya's cubes and changed their order, so now the sequence of numbers written on the cubes became equal to b1, b2, ..., bn. Stepan said that he swapped only cubes which where on the positions between l and r, inclusive, and did not remove or add any other cubes (i. e. he said that he reordered cubes between positions l and r, inclusive, in some way).Your task is to determine if it is possible that Stepan said the truth, or it is guaranteed that Stepan deceived his brother.", "input_spec": "The first line contains three integers n, l, r (1 ≤ n ≤ 105, 1 ≤ l ≤ r ≤ n) — the number of Vasya's cubes and the positions told by Stepan. The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ n) — the sequence of integers written on cubes in the Vasya's order. The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ n) — the sequence of integers written on cubes after Stepan rearranged their order. It is guaranteed that Stepan did not remove or add other cubes, he only rearranged Vasya's cubes.", "output_spec": "Print \"LIE\" (without quotes) if it is guaranteed that Stepan deceived his brother. In the other case, print \"TRUTH\" (without quotes).", "sample_inputs": ["5 2 4\n3 4 2 3 1\n3 2 3 4 1", "3 1 2\n1 2 3\n3 1 2", "4 2 4\n1 1 1 1\n1 1 1 1"], "sample_outputs": ["TRUTH", "LIE", "TRUTH"], "notes": "NoteIn the first example there is a situation when Stepan said the truth. Initially the sequence of integers on the cubes was equal to [3, 4, 2, 3, 1]. Stepan could at first swap cubes on positions 2 and 3 (after that the sequence of integers on cubes became equal to [3, 2, 4, 3, 1]), and then swap cubes in positions 3 and 4 (after that the sequence of integers on cubes became equal to [3, 2, 3, 4, 1]).In the second example it is not possible that Stepan said truth because he said that he swapped cubes only between positions 1 and 2, but we can see that it is guaranteed that he changed the position of the cube which was on the position 3 at first. So it is guaranteed that Stepan deceived his brother.In the third example for any values l and r there is a situation when Stepan said the truth."}, "positive_code": [{"source_code": "# your code goes here\n\nn,l,r = gets.split(\" \").map(&:to_i)\na = gets.split(\" \").map(&:to_i)\nb = gets.split(\" \").map(&:to_i)\nl-=1\nr-=1\na[l..r] = a[l..r].sort()\nb[l..r] = b[l..r].sort()\nif a==b\n\tputs \"TRUTH\"\nelse\n\tputs \"LIE\"\nend"}, {"source_code": "n, l, r = gets.split.map &:to_i\ntrueseq = gets.split.map &:to_i\nmainseq = gets.split.map &:to_i\n\nl, r = l-1, r-1\n# trueseq[0...l] == mainseq[0...l]\n# trueseq[r+1..-1] == mainseq[r+1..-1]\n# trueseq[l..r].set ~~~ mainseq[l..r].set\n\ndef oops\n\tputs 'LIE'\n\texit\nend\n\ngood = true\noops if trueseq[0...l] != mainseq[0...l]\noops if trueseq[r+1..-1] != mainseq[r+1..-1]\noops if trueseq[l..r].sort != mainseq[l..r].sort\n\nputs 'TRUTH'\n"}, {"source_code": "# require 'set'\n\nn, l, r = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\nb = gets.split.map(&:to_i)\n\na1 = a.take(r).drop(l - 1)\nb1 = b.take(r).drop(l - 1)\na1 = a1.sort()\nb1 = b1.sort()\n\nif (a.take(l - 1) == b.take(l - 1) && a.drop(r) == b.drop(r) && a1 == b1) then\n\tputs \"TRUTH\"\nelse\n\tputs \"LIE\"\nend\n"}, {"source_code": "#!/bin/ruby\n\nrequire 'set'\n\nn, l, r = gets.strip.split(' ')\nn = n.to_i\nl = l.to_i\nr = r.to_i\nl -= 1\nr -= 1\n\na = gets.strip.split(' ').map(&:to_i)\nb = gets.strip.split(' ').map(&:to_i)\n\t\ncount = false\nh = Hash.new\n\nfor i in l..r do\n\th[a[i]] += 1 if h.has_key?(a[i])\n\th[a[i]] = 1 unless h.has_key?(a[i])\nend\n\nfor i in l..r\n\tif (h.has_key?(b[i]) && h[b[i]] == 0) || !h.has_key?(b[i])\n\t\tcount = true\n\t\tbreak\n\tend\n\th[b[i]] -= 1 if h.has_key?(b[i]) && h[b[i]] > 0\nend\n\nif count\n\tputs \"LIE\"\nelse\n\tcount = false\n\tfor i in 0...n\n\t\tnext if l <= i && i <= r\n\t\tif a[i] != b[i]\n\t\t\tcount = true\n\t\t\tbreak\n\t\tend\n\tend\n\tputs count ? \"LIE\" : \"TRUTH\"\nend"}], "negative_code": [{"source_code": "#!/bin/ruby\n\nrequire 'set'\n\nn, l, r = gets.strip.split(' ')\nn = n.to_i\nl = l.to_i\nr = r.to_i\nl -= 1\nr -= 1\n\na = gets.strip.split(' ').map(&:to_i)\nb = gets.strip.split(' ').map(&:to_i)\n\t\ncount = false\nh = Hash.new\n\nfor i in l..r do\n\th[a[i]] += 1 if h.has_key?(a[i])\n\th[a[i]] = 1 unless h.has_key?(a[i])\nend\n\nfor i in l..r\n\tif (h.has_key?(b[i]) && h[b[i]] == 0) || !h.has_key?(b[i])\n\t\tcount = 0\n\t\tbreak\n\tend\n\th[b[i]] -= 1 if h.has_key?(b[i]) && h[b[i]] > 0\nend\n\nif count\n\tputs \"LIE\"\nelse\n\tcount = false\n\tfor i in 0...n\n\t\tif i < l && i > r && a[i] != b[i]\n\t\t\tcount = true\n\t\t\tbreak\n\t\tend\n\tend\n\tputs count ? \"LIE\" : \"TRUTH\"\nend"}, {"source_code": "#!/bin/ruby\n\nrequire 'set'\n\nn, l, r = gets.strip.split(' ')\nn = n.to_i\nl = l.to_i\nr = r.to_i\nl -= 1\nr -= 1\n\na = gets.strip.split(' ').map(&:to_i)\nb = gets.strip.split(' ').map(&:to_i)\n\t\ncount = 0\nh = Hash.new\nfor i in l..r do\n\th[a[i]] += 1 if h.has_key?(a[i])\n\th[a[i]] = 1 unless h.has_key?(a[i])\nend\n\nfor i in l..r\n\tif h.has_key?(b[i]) && h[b[i]] == 0 || !h.has_key?(b[i])\n\t\tcount += 1\n\t\tbreak\n\tend\nend\n\nif count > 0\n\tputs \"LIE\"\nelse\n\tcount = 0\n\tfor i in 0...n\n\t\tif i < l && i > r && a[i] != b[i]\n\t\t\tcount += 1\n\t\t\tbreak\n\t\tend\n\tend\n\tif count > 0\n\t\tputs \"LIE\"\n\telse\n\t\tputs \"TRUTH\"\n\tend\nend"}, {"source_code": "#!/bin/ruby\n\nrequire 'set'\na, b = Array.new(100010), Array.new(100010)\n\nn, l, r = gets.strip.split(' ')\nn = n.to_i\nl = l.to_i\nr = r.to_i\nl -= 1\nr -= 1\n\na = gets.strip\na = a.split(' ').map(&:to_i)\n\nb = gets.strip\nb = b.split(' ').map(&:to_i)\n\t\ncount = 0\ns = Set.new\nfor i in l..r do\n\ts.add(a[i])\nend\n\nfor i in l..r\n\tunless s.include?(b[i])\n\t\tcount += 1\n\t\tbreak\n\tend\nend\n\nif count > 0\n\tputs \"LIE\"\nelse\n\tcount = 0\n\tfor i in 0...n\n\t\tif i < l && i > r && a[i] != b[i]\n\t\t\tcount += 1\n\t\t\tbreak\n\t\tend\n\tend\n\tif count > 0\n\t\tputs \"LIE\"\n\telse\n\t\tputs \"TRUTH\"\n\tend\nend"}], "src_uid": "1951bf085050c7e32fcf713132b30605"} {"nl": {"description": "Vanya got bored and he painted n distinct points on the plane. After that he connected all the points pairwise and saw that as a result many triangles were formed with vertices in the painted points. He asks you to count the number of the formed triangles with the non-zero area.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 2000) — the number of the points painted on the plane. Next n lines contain two integers each xi, yi ( - 100 ≤ xi, yi ≤ 100) — the coordinates of the i-th point. It is guaranteed that no two given points coincide.", "output_spec": "In the first line print an integer — the number of triangles with the non-zero area among the painted points.", "sample_inputs": ["4\n0 0\n1 1\n2 0\n2 2", "3\n0 0\n1 1\n2 0", "1\n1 1"], "sample_outputs": ["3", "1", "0"], "notes": "NoteNote to the first sample test. There are 3 triangles formed: (0, 0) - (1, 1) - (2, 0); (0, 0) - (2, 2) - (2, 0); (1, 1) - (2, 2) - (2, 0).Note to the second sample test. There is 1 triangle formed: (0, 0) - (1, 1) - (2, 0).Note to the third sample test. A single point doesn't form a single triangle."}, "positive_code": [{"source_code": "n = gets.to_i\nl = n.times.map{gets.split.map(&:to_i)}.sort\nans = n * (n - 1) * (n - 2) / 6\n0.upto(n - 1) do |i|\n a = (i + 1).upto(n - 1).map{|j| (l[j][1] - l[i][1]).to_f / (l[j][0] - l[i][0])}.sort\n s = 1\n a.each_cons(2) do |x, y|\n if x == y\n s += 1\n else\n ans -= s * (s - 1) / 2\n s = 1\n end\n end\n ans -= s * (s - 1) / 2\nend\nputs ans\n"}, {"source_code": "n = gets.to_i\nl = n.times.map{gets.split.map(&:to_i)}.sort\n\ndef f(n)\n return n if n <= 1\n n * f(n - 1)\nend\n\ndef c(n, m)\n if n > m\n f(n) / (f(m) * f(n - m))\n elsif n < m\n 0\n else\n 1\n end\nend\n\nans = c(n, 3)\n0.upto(n - 1) do |i|\n a = []\n (i + 1).upto(n - 1) do |j|\n a << (l[j][1] - l[i][1]).to_f / (l[j][0] - l[i][0])\n end\n a.sort!\n b = nil\n s = 1\n a.size.times do |j|\n if a[j] != b\n ans -= c(s, 2)\n s = 1\n b = a[j]\n else\n s += 1\n ans -= c(s, 2) if j == a.size - 1\n end\n end\nend\nputs ans\n"}, {"source_code": "n = gets.to_i\np = n.times.map{gets.split.map(&:to_i)}.sort\nans = n * (n - 1) * (n - 2) / 6\nn.times do |i|\n c = 1\n (i + 1)\n .upto(n - 1)\n .map{|j| (p[j][1] - p[i][1]).to_f / (p[j][0] - p[i][0])}\n .sort\n .each_cons(2) do |a, b|\n if a == b\n c += 1\n else\n ans -= c * (c - 1) / 2;\n c = 1\n end\n end\n ans -= c * (c - 1) / 2\nend\nputs ans\n"}, {"source_code": "n = gets.to_i\np = n.times.map{gets.split.map(&:to_i)}.sort\nans = n * (n - 1) * (n - 2) / 6\nn.times do |i|\n c = 1\n (i + 1)\n .upto(n - 1)\n .map{|j| Math.atan2(p[j][1] - p[i][1], p[j][0] - p[i][0])}\n .sort\n .each_cons(2) do |a, b|\n if a == b\n c += 1\n else\n ans -= c * (c - 1) / 2;\n c = 1\n end\n end\n ans -= c * (c - 1) / 2\nend\nputs ans\n"}], "negative_code": [{"source_code": "n = gets.to_i\nl = n.times.map{gets.split.map(&:to_i)}\nans = 0\n0.upto(n - 1) do |i|\n (i + 1).upto(n - 1) do |j|\n (j + 1).upto(n - 1) do |k|\n a = l[i][0] - l[j][0] == 0 ? '-' : (l[i][1] - l[j][1]) / (l[i][0] - l[j][0])\n b = l[j][0] - l[k][0] == 0 ? '-' : (l[j][1] - l[k][1]) / (l[j][0] - l[k][0])\n c = l[i][0] - l[k][0] == 0 ? '-' : (l[i][1] - l[k][1]) / (l[i][0] - l[k][0])\n ans += 1 if a != b && b != c && a != c\n end\n end\nend\nputs ans\n"}, {"source_code": "n = gets.to_i\nl = n.times.map{gets.split.map(&:to_i)}\n\ndef f(n)\n return n if n <= 1\n n * f(n - 1)\nend\n\ndef c(n, m)\n if n > m\n f(n) / (f(m) * f(n - m))\n elsif n < m\n 0\n else\n 1\n end\nend\n\nans = c(n, 3)\n0.upto(n - 1) do |i|\n a = []\n (i + 1).upto(n - 1) do |j|\n a << (l[j][1] - l[i][1]).to_f / (l[j][0] - l[i][0])\n end\n a.sort!\n b = nil\n s = 1\n a.size.times do |j|\n if a[j] != b\n ans -= c(s, 2)\n s = 1\n b = a[j]\n else\n s += 1\n ans -= c(s, 2) if j == a.size - 1\n end\n end\nend\nputs ans\n"}, {"source_code": "n = gets.to_i\na = n.times.map{gets.split.map(&:to_i)}\nans = 0\n0.upto(n - 1) do |i|\n (i + 1).upto(n - 1) do |j|\n (j + 1).upto(n - 1) do |k|\n puts \"#{i} #{j} #{k}\" if n == 100\n p = a[i][0] - a[j][0] == 0 ? '-' : (a[i][1] - a[j][1]) / (a[i][0] - a[j][0])\n q = a[j][0] - a[k][0] == 0 ? '-' : (a[j][1] - a[k][1]) / (a[j][0] - a[k][0])\n ans += 1 if p != q\n end\n end\nend\nputs ans\n"}, {"source_code": "n = gets.to_i\na = n.times.map{gets.split.map(&:to_i)}\nans = 0\n0.upto(n - 1) do |i|\n (i + 1).upto(n - 1) do |j|\n (j + 1).upto(n - 1) do |k|\n p = a[i][0] - a[j][0] == 0 ? '-' : (a[i][1] - a[j][1]) / (a[i][0] - a[j][0])\n q = a[j][0] - a[k][0] == 0 ? '-' : (a[j][1] - a[k][1]) / (a[j][0] - a[k][0])\n ans += 1 if p != q\n end\n end\nend\nputs ans\n"}, {"source_code": "n = gets.to_i\na = n.times.map{gets.split.map(&:to_i)}\nans = 0\n0.upto(n - 1) do |i|\n (i + 1).upto(n - 1) do |j|\n (j + 1).upto(n - 1) do |k|\n p = a[i][0] - a[j][0] == 0 ? '-' : (a[i][1] - a[j][1]) / (a[i][0] - a[j][0])\n q = a[k][0] - a[j][0] == 0 ? '-' : (a[k][1] - a[j][1]) / (a[k][0] - a[j][0])\n ans += 1 if p != q\n end\n end\nend\nputs ans\n"}, {"source_code": "n = gets.to_i\nl = n.times.map{gets.split.map(&:to_i)}\nans = 0\n0.upto(n - 1) do |i|\n (i + 1).upto(n - 1) do |j|\n (j + 1).upto(n - 1) do |k|\n a = l[i][0] - l[j][0] == 0 ? '-' : (l[i][1] - l[j][1]) / (l[i][0] - l[j][0])\n b = l[j][0] - l[k][0] == 0 ? '-' : (l[j][1] - l[k][1]) / (l[j][0] - l[k][0])\n c = l[i][0] - l[k][0] == 0 ? '-' : (l[i][1] - l[k][1]) / (l[i][0] - l[k][0])\n ans += 1 if a != b || b != c || a != c\n end\n end\nend\nputs ans\n"}, {"source_code": "n = gets.to_i\nl = n.times.map{gets.split.map(&:to_i)}\nans = 0\n0.upto(n - 1) do |i|\n (i + 1).upto(n - 1) do |j|\n (j + 1).upto(n - 1) do |k|\n a = l[i][0] - l[j][0] == 0 ? '-' : (l[i][1] - l[j][1]) / (l[i][0] - l[j][0])\n b = l[j][0] - l[k][0] == 0 ? '-' : (l[j][1] - l[k][1]) / (l[j][0] - l[k][0])\n c = l[i][0] - l[k][0] == 0 ? '-' : (l[i][1] - l[k][1]) / (l[i][0] - l[k][0])\n ans += 1 if a != b && b != c\n end\n end\nend\nputs ans\n"}], "src_uid": "d5913f8208efa1641f53caaee7624622"} {"nl": {"description": "There are n pictures delivered for the new exhibition. The i-th painting has beauty ai. We know that a visitor becomes happy every time he passes from a painting to a more beautiful one.We are allowed to arranged pictures in any order. What is the maximum possible number of times the visitor may become happy while passing all pictures from first to last? In other words, we are allowed to rearrange elements of a in any order. What is the maximum possible number of indices i (1 ≤ i ≤ n - 1), such that ai + 1 > ai.", "input_spec": "The first line of the input contains integer n (1 ≤ n ≤ 1000) — the number of painting. The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000), where ai means the beauty of the i-th painting.", "output_spec": "Print one integer — the maximum possible number of neighbouring pairs, such that ai + 1 > ai, after the optimal rearrangement.", "sample_inputs": ["5\n20 30 10 50 40", "4\n200 100 100 200"], "sample_outputs": ["4", "2"], "notes": "NoteIn the first sample, the optimal order is: 10, 20, 30, 40, 50.In the second sample, the optimal order is: 100, 200, 100, 200."}, "positive_code": [{"source_code": "n,*a=$<.read.split.map &:to_i;p n-a.group_by{|e|e}.map{|k,v|v.size}.max"}, {"source_code": "n = gets.chomp.to_i\nh1 = Hash.new { 0 }\ngets.chomp.split.map(&:to_i).each { |a| h1[a] += 1 }\n\nh2 = Hash.new { 0 }\nh1.each do |_, v|\n (1..v).each { |e| h2[e] += 1 }\nend\n\nans = 0\n\nh2.each { |_, v| ans += [v - 1, 0].max }\n\nputs ans\n"}, {"source_code": "n = readline.to_i\narr = readline.split(' ').collect(&:to_i)\nret = 0\nwhile !arr.empty?\n a = arr.uniq\n ret += a.length - 1\n a.each do |num|\n arr.delete_at(arr.find_index(num))\n end\nend\nputs ret\n"}, {"source_code": "n=gets.to_i\nhs=Hash.new(0)\ngets.split.map{|e| e.to_i}.each{|e|\n\ths[e]+=1\n}\nxs=hs.sort.map{|e| e[1]}\nans=0\nmax1=0\nxs.each{|e|\n\tif max1>=e then\n\t\tans+=e\n\telse\n\t\tans+=max1\n\t\tmax1=e\n\tend\n}\nputs ans"}, {"source_code": "n = gets.chomp.to_i\nax = gets.chomp.split(\"\\s\").map(&:to_i).sort\n\ntmp_ax = ax.reduce(Hash.new(0)) { |acc, e| acc.tap { |hash| hash[e] += 1 } }\ncount = 0\n\nuntil tmp_ax.size == 0\n # Add the number of elements - 1 to count\n count += tmp_ax.size - 1\n\n # Minus 1 from all elements of tmp_ax\n tmp_ax = tmp_ax.reduce({}) { |acc, (k, v)| acc.merge(k => v - 1) }\n\n # Delete elements whose value is 0\n tmp_ax = tmp_ax.select { |k, v| v > 0 }\nend\n\nputs count\n"}, {"source_code": "n = gets.chomp.to_i\nas = gets.chomp.split.map(&:to_i)\n\nhs = Hash.new(0)\nas.each do |a|\n hs[a] += 1\nend\n\ncs = Hash.new(0)\nhs.each do |_, v|\n for i in 1..v\n cs[i] += 1\n end\nend\n\nans = 0\ncs.each do |_, v|\n ans += [v - 1, 0].max\nend\nputs ans\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nr = 0\nwhile a.any?\n b = a.uniq\n r += b.count - 1\n b.each {|x| a.delete_at(a.index(x))}\nend\nputs r"}, {"source_code": "def way(n, img)\n\tshow = []\n # puts \"count #{img.count}\"\n for i in 1...img.count\n #puts \"i = #{i}\"\n unless img[i-1] == img[i]\n # puts \"index = #{i}\"\n n += 1\n else\n show.push(img[i-1])\n end\n end\n\n #puts \"way n = #{n} img = #{img} show = #{show}\"\n return n, show\nend\n\ncount_img = gets.to_i\n\nimg = Array.new(count_img)\n\nimg = gets.split.map(&:to_i)\nimg = img.sort\nn = 0\n\nwhile true do\n answer = way(n, img)\n n = answer[0]\n img = answer[1]\n # puts \"main n = #{n} img = #{img}\"\n if img.count < 2\n puts n\n exit\n end\nend\n"}, {"source_code": "n,*a=$<.read.split;p n.to_i-a.map{|e|a.count(e)}.max.to_i\n"}, {"source_code": "n,*a=$<.read.split.map &:to_i;p n-a.group_by{|e|e}.map{|k,v|v.size}.max"}, {"source_code": "n,*a=$<.read.split.map &:to_i;p n-a.group_by{|e|e}.map{|k,v|v.size}.max"}, {"source_code": "n = gets.to_i\na = gets.split(\" \")\ntot = Array.new(1111, 0)\na.each do |x|\n\ttot[x.to_i] += 1\nend\nans=1\ntot.each do |c|\n\tans = [ans, c].max\nend\nputs n-ans"}, {"source_code": "def way(n, img)\n\tshow = []\n # puts \"count #{img.count}\"\n for i in 1...img.count\n #puts \"i = #{i}\"\n unless img[i-1] == img[i]\n # puts \"index = #{i}\"\n n += 1\n else\n show.push(img[i-1])\n end\n end\n \n #puts \"way n = #{n} img = #{img} show = #{show}\"\n return n, show\nend\n\ncount_img = gets.to_i\n\nimg = Array.new(count_img)\n\nimg = gets.split.map(&:to_i)\nimg = img.sort\nn = 0\n\nwhile true do\n answer = way(n, img)\n n = answer[0]\n img = answer[1]\n # puts \"main n = #{n} img = #{img}\"\n if img.count < 2\n puts n\n exit\n end\nend"}, {"source_code": "#!/usr/bin/ruby\nrequire 'set'\nrequire 'scanf'\n\nbeauty_sets = [Set.new]\nnum_paintings = scanf(\"%d\\n\").first.to_i\nbeauties = gets().split(' ').map(&:to_i)\nbeauties.each do |beauty|\n placed = false\n beauty_sets.each do |bs|\n if !bs.include?(beauty)\n bs.add(beauty)\n placed = true\n break\n end\n end\n if !placed\n beauty_sets << Set.new\n beauty_sets.last.add(beauty)\n end\nend\n\nsum = 0\nbeauty_sets.each { |bs| sum += bs.count-1 }\n\nputs sum\n"}, {"source_code": "n,*a=$<.read.split;p n.to_i-a.map{|e|a.count(e)}.max.to_i\n\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i).sort\nc = 0\n\naf = [0]*n\nwhile af.inject {|s, aa| s + aa } < n\n\tac = -1\n\tn.times do |i|\n\t\tif af[i]==0 && ac -1\n\t\t\tac = a[i]\n\t\t\taf[i] = 1\n\t\tend\n\tend\nend\n\nputs c"}], "negative_code": [{"source_code": "gets;p gets.split.uniq.size"}, {"source_code": "n = gets.chomp.to_i\nh1 = Hash.new { 0 }\ngets.chomp.split.map(&:to_i).each { |a| h1[a] += 1 }\n\nh2 = Hash.new { 0 }\nh1.each do |_, v|\n (1..v).each { |e| h2[e] += 1 }\nend\n\nans = 0\np h1\np h2\nh2.each { |_, v| ans += [v - 1, 0].max }\n\nputs ans\n"}, {"source_code": "def way(n, img)\n\tshow = []\n for i in 1...img.count\n if img[i] != img[i+1]\n n += 1\n else\n show.push(img[i])\n end\n end\n\t\n return n, show\nend\n\ncount_img = gets.to_i\n\nimg = Array.new(count_img)\n\nimg = gets.split.map(&:to_i)\nimg = img.sort\nn = 0\n\nwhile true do\n answer = way(n, img)\n n = answer[0]\n img = answer[1]\n if img.count <= 2\n puts n\n exit\n end\nend\n"}, {"source_code": "def way(n, img)\n\tshow = []\n for i in 1...img.count\n if img[i] != img[i+1]\n n += 1\n else\n show.push(img[i])\n end\n end\n return n, show\nend\n\ndef main\n count_img = gets.to_i\n\n img = Array.new(count_img)\n\n img = gets.split.map(&:to_i)\n img = img.sort\n n = 0\n\n while true do\n answer = way(n, img)\n n = answer[0]\n img = answer[1]\n return if img.count <= 2\n end\n\n p n\nend\n\nmain\n"}, {"source_code": "def way(n, img)\n\tshow = []\n for i in 1...img.count\n if img[i] != img[i+1]\n n += 1\n else\n show.push(img[i])\n end\n end\n\n puts \"way n = #{n} img = #{img} show = #{show}\"\n return n, show\nend\n\ncount_img = gets.to_i\n\nimg = Array.new(count_img)\n\nimg = gets.split.map(&:to_i)\nimg = img.sort\nn = 0\n\nwhile true do\n answer = way(n, img)\n n = answer[0]\n img = answer[1]\n puts \"main n = #{n} img = #{img}\"\n if img.count <= 2\n puts n\n exit\n end\nend\n"}, {"source_code": "def way(n, img)\n\tshow = []\n # puts \"count #{img.count}\"\n for i in 1...img.count\n #puts \"i = #{i}\"\n unless img[i-1] == img[i]\n # puts \"index = #{i}\"\n n += 1\n else\n show.push(img[i-1])\n end\n end\n \n #puts \"way n = #{n} img = #{img} show = #{show}\"\n return n, show\nend\n\ncount_img = gets.to_i\n\nimg = Array.new(count_img)\n\nimg = gets.split.map(&:to_i)\nimg = img.sort\nn = 0\n\nwhile true do\n answer = way(n, img)\n n = answer[0]\n img = answer[1]\n # puts \"main n = #{n} img = #{img}\"\n if img.count <= 2\n puts n\n exit\n end\nend"}, {"source_code": "def way(n, img)\n\tshow = []\n puts \"count #{img.count}\"\n for i in 1...img.count\n #puts \"i = #{i}\"\n unless img[i-1] == img[i]\n # puts \"index = #{i}\"\n n += 1\n else\n show.push(img[i-1])\n end\n end\n \n #puts \"way n = #{n} img = #{img} show = #{show}\"\n return n, show\nend\n\ncount_img = gets.to_i\n\nimg = Array.new(count_img)\n\nimg = gets.split.map(&:to_i)\nimg = img.sort\nn = 0\n\nwhile true do\n answer = way(n, img)\n n = answer[0]\n img = answer[1]\n # puts \"main n = #{n} img = #{img}\"\n if img.count <= 2\n puts n\n exit\n end\nend"}, {"source_code": "#!/usr/bin/ruby\nrequire 'set'\nrequire 'scanf'\n\nbeauty_sets = [Set.new]\nnum_paintings = scanf(\"%d\").first.to_i\nbeauties = gets().split(' ').map(&:to_i)\nbeauties.each do |beauty|\n placed = false\n beauty_sets.each do |bs|\n if !bs.include?(beauty)\n bs.add(beauty)\n placed = true\n break\n end\n end\n if !placed\n beauty_sets << Set.new\n beauty_sets.last.add(beauty)\n end\nend\n\nsum = 0\nbeauty_sets.each { |bs| sum += bs.count-1 }\n\nputs sum\n"}], "src_uid": "30ad5bdc019fcd8a4e642c90decca58f"} {"nl": {"description": "To become the king of Codeforces, Kuroni has to solve the following problem.He is given $$$n$$$ numbers $$$a_1, a_2, \\dots, a_n$$$. Help Kuroni to calculate $$$\\prod_{1\\le i<j\\le n} |a_i - a_j|$$$. As result can be very big, output it modulo $$$m$$$.If you are not familiar with short notation, $$$\\prod_{1\\le i<j\\le n} |a_i - a_j|$$$ is equal to $$$|a_1 - a_2|\\cdot|a_1 - a_3|\\cdot$$$ $$$\\dots$$$ $$$\\cdot|a_1 - a_n|\\cdot|a_2 - a_3|\\cdot|a_2 - a_4|\\cdot$$$ $$$\\dots$$$ $$$\\cdot|a_2 - a_n| \\cdot$$$ $$$\\dots$$$ $$$\\cdot |a_{n-1} - a_n|$$$. In other words, this is the product of $$$|a_i - a_j|$$$ for all $$$1\\le i < j \\le n$$$.", "input_spec": "The first line contains two integers $$$n$$$, $$$m$$$ ($$$2\\le n \\le 2\\cdot 10^5$$$, $$$1\\le m \\le 1000$$$) — number of numbers and modulo. The second line contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$0 \\le a_i \\le 10^9$$$).", "output_spec": "Output the single number — $$$\\prod_{1\\le i<j\\le n} |a_i - a_j| \\bmod m$$$.", "sample_inputs": ["2 10\n8 5", "3 12\n1 4 5", "3 7\n1 4 9"], "sample_outputs": ["3", "0", "1"], "notes": "NoteIn the first sample, $$$|8 - 5| = 3 \\equiv 3 \\bmod 10$$$.In the second sample, $$$|1 - 4|\\cdot|1 - 5|\\cdot|4 - 5| = 3\\cdot 4 \\cdot 1 = 12 \\equiv 0 \\bmod 12$$$.In the third sample, $$$|1 - 4|\\cdot|1 - 9|\\cdot|4 - 9| = 3 \\cdot 8 \\cdot 5 = 120 \\equiv 1 \\bmod 7$$$."}, "positive_code": [{"source_code": "\tdef abs( n )\n\t\tn > 0 ? n : -n\n\tend\n\n\tn,m = gets.chomp.strip.split(\" \")\n\tarr = gets.chomp.strip.split(\" \")\n\tm = m.to_i\n\tn = n.to_i\n\n\tif n > m\n\t\tputs 0\n\telse\n\t\tans = 1\n\t\t(0..n-1).each do | i |\n\t\t\t(i+1..n-1).each do | j |\n\t\t\t\tans = ( ans * abs(arr[ j ].to_i - arr[ i ].to_i ) ) % m\n\t\t\tend\n\t\tend\n\t\tputs ans\n\tend"}, {"source_code": "N, M = gets.split.map(&:to_i)\nA = gets.split.map(&:to_i).sort\nB = A.map { |a| a % M }\n\ncounter = Hash.new(0)\n\nB.each do |b|\n counter[b] += 1\nend\n\nif counter.values.max >= 2\n puts 0\nelse\n ans = 1\n\n N.times do |i|\n (i + 1).upto(N - 1) do |j|\n ans = ans * (A[i] - A[j]).abs % M\n end\n end\n\n puts ans % M\nend\n"}], "negative_code": [{"source_code": "def abs( n )\n\tn > 0 ? n : -n\nend\n\nn,m = gets.chomp\nn = n.to_i\nm = m.to_i\narr = gets.chomp.strip.split(\" \")\n\nif n > m\n\tputs 0\nelse\n\tans = 0\n\t(0..n-1).each do | i |\n\t\t(i+1..n-1).each do | j |\n\t\t\tans = ( ans * abs(arr[ j ].to_i - arr[ i ].to_i ) ) % m\n\t\tend\n\tend\nend"}, {"source_code": "def abs( n )\n\tn > 0 ? n : -n\nend\n\nn,m = gets.chomp.strip.split(\" \")\narr = gets.chomp.strip.split(\" \")\nm = m.to_i\nn = n.to_i\n\nif n > m\n\tputs 0\nelse\n\tans = 0\n\t(0..n-1).each do | i |\n\t\t(i+1..n-1).each do | j |\n\t\t\tans = ( ans * abs(arr[ j ].to_i - arr[ i ].to_i ) ) % m\n\t\tend\n\tend\n\tputs ans\nend"}, {"source_code": "def abs( n )\n\tn > 0 ? n : -n\nend\n\nn,m = gets.chomp.strip.split(\" \")\narr = gets.chomp.strip.split(\" \")\nm = m.to_i\nn = n.to_i\n\nif n > m\n\tputs 0\nelse\n\tans = 0\n\t(0..n-1).each do | i |\n\t\t(i+1..n-1).each do | j |\n\t\t\tans = ( ans * abs(arr[ j ].to_i - arr[ i ].to_i ) ) % m\n\t\tend\n\tend\nend"}], "src_uid": "bf115b24d85a0581e709c012793b248b"} {"nl": {"description": "As you could know there are no male planes nor female planes. However, each plane on Earth likes some other plane. There are n planes on Earth, numbered from 1 to n, and the plane with number i likes the plane with number fi, where 1 ≤ fi ≤ n and fi ≠ i.We call a love triangle a situation in which plane A likes plane B, plane B likes plane C and plane C likes plane A. Find out if there is any love triangle on Earth.", "input_spec": "The first line contains a single integer n (2 ≤ n ≤ 5000) — the number of planes. The second line contains n integers f1, f2, ..., fn (1 ≤ fi ≤ n, fi ≠ i), meaning that the i-th plane likes the fi-th.", "output_spec": "Output «YES» if there is a love triangle consisting of planes on Earth. Otherwise, output «NO». You can output any letter in lower case or in upper case.", "sample_inputs": ["5\n2 4 5 1 3", "5\n5 5 5 5 1"], "sample_outputs": ["YES", "NO"], "notes": "NoteIn first example plane 2 likes plane 4, plane 4 likes plane 1, plane 1 likes plane 2 and that is a love triangle.In second example there are no love triangles."}, "positive_code": [{"source_code": "a=$<.read.split.map &:to_i\nputs (1...a.size).any?{|i|\n\ta[a[a[i]]]==i\n} ? :YES : :NO"}, {"source_code": "n = gets.to_i;\ninp = gets.chomp.split().map(&:to_i);\nt = true;\nfor i in 0..n-1\n if(inp[inp[inp[i] - 1] - 1] - 1 == i)\n t = false;\n puts \"YES\";\n break;\n end;\nend;\nif (t)\n puts \"NO\";\nend;\n"}, {"source_code": "gets\nar = gets.split.map(&:to_i)\nh = {}\nar.each_index do |i|\n h[ar[i]] = ar[ar[i]-1] if ar.include?(ar[ar[i]-1])\nend\nh.each do |key, val|\n b = h[val]\n if h[b] == key\n puts \"Yes\"\n exit 0\n end\nend\nputs \"No\""}, {"source_code": "n = Integer(gets)\nfriends = gets.split.map{|a| Integer(a)}\na1 = (1..n).to_a\na2 = friends\na3 = a2.map{|a| friends[a-1] }\na4 = a3.map{|a| friends[a-1] }\n\nputs (if a1.zip(a2,a3,a4).detect{|a,b,c,d| a==d and a!=b and a!=c and b!=c} then \"YES\" else \"NO\" end) "}, {"source_code": "n = gets.to_i\nfs = gets.split.map(&:to_i)\nans = 'NO'\n\nfs.each_with_index do |f, i|\n if i == fs[fs[f-1]-1] - 1\n ans = 'YES'\n end\nend\n\nputs ans"}, {"source_code": "a=$<.read.split.map &:to_i\nputs (1...a.size).any?{|i|\n\ta[a[a[i]]]==i\n} ? :YES : :NO\n"}, {"source_code": "$n=gets.chomp.to_i\n$f=gets.split.map &:to_i\ndef check (a,b,c)\n if (b==4 and a==c)\n return true\n end\n if (b>3)\n return false\n end\n return check($f[a]-1,b+1,c)\nend\nfor i in 0.upto($n-1)\n if (check(i,1,i))\n print \"YES\"\n exit\n end\nend\nprint \"NO\""}, {"source_code": "a=$<.read.split.map &:to_i\nputs (1...a.size).any?{|i|\n\ta[a[a[i]]]==i\n} ? :YES : :NO"}, {"source_code": "def judge(n, f)\n (1..n).each do |i|\n return true if f[f[f[i]]] == i\n end\n return false\nend\n\nn = gets.to_i\nf = gets.split.map(&:to_i).insert(0, 0)\nputs judge(n, f) ? \"YES\" : \"NO\"\n"}, {"source_code": "n = gets.to_i\na = gets.split(' ')\nfor i in 0..n-1\n a[i]=a[i].to_i-1\nend\nfor i in 0..n-1\n t= (a[a[a[i]]]==i)|t\nend\nif t==true\n puts \"YES\"\nelse \n puts \"NO\" \nend"}, {"source_code": "n = gets.to_i\nf = [nil] + gets.split.map(&:to_i)\n(1...f.size).each do |i|\n if f[f[f[i]]] == i\n puts 'YES'\n exit\n end\nend\nputs 'NO'\n"}, {"source_code": "# Love Triangles\n\ndef track(a, e)\n\treturn a[e-1]\nend\n\ndef check(a, i)\n\tx = track(a, a[i])\n\treturn 1\tif track(a, x)== i+1\n\treturn 0\nend\n\ndef find_luv(inp,n)\n\tfor i in 0..n-1\n\t\treturn 'YES' if check(inp,i) == 1\n\tend\n\treturn 'NO'\nend\n\nn = gets.to_i\ninp = gets.chomp.split().map(&:to_i)\nputs find_luv(inp,n)"}, {"source_code": "def solve(n, f)\n n = n.to_i\n f = f.split(&:to_i)\n for i in 0...n\n x = f[i].to_i\n y = f[x - 1].to_i\n return true if f[y - 1].to_i == i.to_i + 1\n end\n\n return false\nend\n\nn = gets.chomp\nf = gets.chomp\n\nif solve(n, f)\n puts \"YES\"\nelse\n puts \"NO\"\nend\n"}], "negative_code": [{"source_code": "n,*a=$<.read.split.map &:to_i\nputs [*0...n].permutation(2).any?{|i,j|\n\ta[a[a[j]-1]-1]==i\n} ? :YES : :NO"}, {"source_code": "n,*a=$<.read.split.map &:to_i\nputs [*0...n].permutation(2).any?{|i,j|\n\ta[a[j]-1]==i\n} ? :YES : :NO"}, {"source_code": "n = gets.to_i\na = gets.split(' ')\nfor i in 0..n-1\n a[i]=a[i].to_i-1\nend\nt=1\nfor i in 0..n-1\n t= (a[a[a[i]]]==i)\n t=1\nend\nif t==1\n puts \"YES\"\nelse \n puts \"NO\" \nend"}], "src_uid": "a37c3f2828490c70301b5b5deeee0f88"} {"nl": {"description": "You are given an array $$$a$$$ consisting of $$$n$$$ positive integers. You can perform operations on it.In one operation you can replace any element of the array $$$a_i$$$ with $$$\\lfloor \\frac{a_i}{2} \\rfloor$$$, that is, by an integer part of dividing $$$a_i$$$ by $$$2$$$ (rounding down).See if you can apply the operation some number of times (possible $$$0$$$) to make the array $$$a$$$ become a permutation of numbers from $$$1$$$ to $$$n$$$ —that is, so that it contains all numbers from $$$1$$$ to $$$n$$$, each exactly once.For example, if $$$a = [1, 8, 25, 2]$$$, $$$n = 4$$$, then the answer is yes. You could do the following: Replace $$$8$$$ with $$$\\lfloor \\frac{8}{2} \\rfloor = 4$$$, then $$$a = [1, 4, 25, 2]$$$. Replace $$$25$$$ with $$$\\lfloor \\frac{25}{2} \\rfloor = 12$$$, then $$$a = [1, 4, 12, 2]$$$. Replace $$$12$$$ with $$$\\lfloor \\frac{12}{2} \\rfloor = 6$$$, then $$$a = [1, 4, 6, 2]$$$. Replace $$$6$$$ with $$$\\lfloor \\frac{6}{2} \\rfloor = 3$$$, then $$$a = [1, 4, 3, 2]$$$. ", "input_spec": "The first line of input data contains an integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) —the number of test cases. Each test case contains exactly two lines. The first one contains an integer $$$n$$$ ($$$1 \\le n \\le 50$$$), the second one contains integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$).", "output_spec": "For each test case, output on a separate line: YES if you can make the array $$$a$$$ become a permutation of numbers from $$$1$$$ to $$$n$$$, NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).", "sample_inputs": ["6\n\n4\n\n1 8 25 2\n\n2\n\n1 1\n\n9\n\n9 8 3 4 2 7 1 5 6\n\n3\n\n8 2 1\n\n4\n\n24 7 16 7\n\n5\n\n22 6 22 4 22"], "sample_outputs": ["YES\nNO\nYES\nNO\nNO\nYES"], "notes": "NoteThe first test case is explained in the text of the problem statement.In the second test case, it is not possible to get a permutation."}, "positive_code": [{"source_code": "inputs = STDIN.read.strip.split(\"\\n\")\r\ninputs.shift\r\n\r\ninputs.each_slice(2) do |inp|\r\n a = inp[0].to_i\r\n nums = inp[1].split(' ').map(&:to_i).sort\r\n\r\n ex = Hash[ (1..a).map{|n| [n, true] } ]\r\n nums.each do |n|\r\n while n >= 1\r\n if ex[n]\r\n ex.delete(n)\r\n break\r\n end\r\n\r\n n /= 2\r\n end\r\n end\r\n\r\n if ex.length == 0\r\n puts \"YES\"\r\n else\r\n puts \"NO\"\r\n end\r\nend"}, {"source_code": "inputs = STDIN.read.strip.split(\"\\n\")\r\ninputs.shift\r\n\r\ninputs.each_slice(2) do |inp|\r\n a = inp[0].to_i\r\n nums = inp[1].split(' ').map(&:to_i).sort\r\n\r\n ex = Hash[ (1..a).map{|n| [n, true] } ]\r\n nums.each do |n|\r\n i = n\r\n while i >= 1\r\n if ex[i]\r\n ex.delete(i)\r\n break\r\n end\r\n\r\n i /= 2\r\n end\r\n end\r\n\r\n if ex.length == 0\r\n puts \"YES\"\r\n else\r\n puts \"NO\"\r\n end\r\nend"}, {"source_code": "inputs = STDIN.read.strip.split(\"\\n\")\r\ninputs.shift\r\n\r\ninputs.each_slice(2) do |inp|\r\n a = inp[0].to_i\r\n nums = inp[1].split(' ').map(&:to_i).sort\r\n\r\n ex = Hash[ (1..a).map{|n| [n, true] } ]\r\n nums.each do |n|\r\n i = n\r\n while i >= 1\r\n if ex[i]\r\n ex.delete(i)\r\n break\r\n end\r\n\r\n i = (i / 2).round\r\n end\r\n end\r\n\r\n if ex.length == 0\r\n puts \"YES\"\r\n else\r\n puts \"NO\"\r\n end\r\nend"}, {"source_code": "require 'set'\r\n\r\ninputs = STDIN.read.strip.split(\"\\n\")\r\ninputs.shift\r\n\r\ninputs.each_slice(2) do |inp|\r\n a = inp[0].to_i\r\n nums = inp[1].split(' ').map(&:to_i).sort\r\n\r\n ex = Set.new((1..a).to_a)\r\n nums.each do |n|\r\n i = n\r\n while i >= 1\r\n if ex.member?(i)\r\n ex.delete(i)\r\n break\r\n end\r\n\r\n i = (i / 2).round\r\n end\r\n end\r\n\r\n if ex.length == 0\r\n puts \"YES\"\r\n else\r\n puts \"NO\"\r\n end\r\nend"}, {"source_code": "gets.to_i.times do\n n = gets.to_i\n xs = gets.split.map(&:to_i)\n\n h = {}\n xs.each_with_index do |x, i|\n loop {\n (h[x] ||= []) << i if x <= n\n x /= 2\n break if x == 0\n }\n end\n\n if h.size != n\n puts \"NO\"\n next\n end\n\n used = [false] * n\n h = h.values.sort_by! { _1.size }\n\n solution_exists = true\n\n h.each do |indices|\n found = false\n indices.each do |i|\n if !used[i]\n used[i] = true\n found = true\n break\n end\n end\n\n if !found\n solution_exists = false\n break\n end\n end\n\n puts(solution_exists ? \"YES\" : \"NO\")\nend\n"}, {"source_code": "require 'set'\n\nt = readline.strip.to_i\nt.times do\n readline\n a = readline.strip.split(/\\s+/).map(&:to_i)\n s = Set.new((1..a.count).to_a)\n a.each do |e|\n while e > 0\n if s.member?(e)\n s.delete(e)\n break\n end\n\n e /= 2\n end\n end\n\n puts s.count == 0 ? 'YES' : 'NO'\nend\n"}], "negative_code": [], "src_uid": "645459e0a41ec63b13648ea8dbe0f053"} {"nl": {"description": "Gerald plays the following game. He has a checkered field of size n × n cells, where m various cells are banned. Before the game, he has to put a few chips on some border (but not corner) board cells. Then for n - 1 minutes, Gerald every minute moves each chip into an adjacent cell. He moves each chip from its original edge to the opposite edge. Gerald loses in this game in each of the three cases: At least one of the chips at least once fell to the banned cell. At least once two chips were on the same cell. At least once two chips swapped in a minute (for example, if you stand two chips on two opposite border cells of a row with even length, this situation happens in the middle of the row). In that case he loses and earns 0 points. When nothing like that happened, he wins and earns the number of points equal to the number of chips he managed to put on the board. Help Gerald earn the most points.", "input_spec": "The first line contains two space-separated integers n and m (2 ≤ n ≤ 1000, 0 ≤ m ≤ 105) — the size of the field and the number of banned cells. Next m lines each contain two space-separated integers. Specifically, the i-th of these lines contains numbers xi and yi (1 ≤ xi, yi ≤ n) — the coordinates of the i-th banned cell. All given cells are distinct. Consider the field rows numbered from top to bottom from 1 to n, and the columns — from left to right from 1 to n.", "output_spec": "Print a single integer — the maximum points Gerald can earn in this game.", "sample_inputs": ["3 1\n2 2", "3 0", "4 3\n3 1\n3 2\n3 3"], "sample_outputs": ["0", "1", "1"], "notes": "NoteIn the first test the answer equals zero as we can't put chips into the corner cells.In the second sample we can place one chip into either cell (1, 2), or cell (3, 2), or cell (2, 1), or cell (2, 3). We cannot place two chips.In the third sample we can only place one chip into either cell (2, 1), or cell (2, 4)."}, "positive_code": [{"source_code": "n, m = gets.split.map(&:to_i)\nrows = Array.new(n, true)\nrows[0] = rows[-1] = false\ncols = rows.dup\nfor line in (1..m)\n\tr, c = gets.split.map(&:to_i)\n\trows[r-1] = false\n\tcols[c-1] = false\nend\nrows[n/2] = false if (n%2 == 1 and cols[n/2])\nputs rows.count(true) + cols.count(true)"}, {"source_code": "n, m = gets.split.map(&:to_i)\nr, c = Array.new(n + 1), Array.new(n + 1)\nn.times do |i|\n r[i] = c[i] = 0\nend\nm.times do\n x, y = gets.split.map(&:to_i)\n r[x], c[y] = 1, 1\nend\nans = 0\n(2...n).step(1) do |i|\n ans += 1 if r[i].zero?\n ans += 1 if c[i].zero?\nend\nans -= 1 if (n.odd? && r[n / 2 + 1].zero? && c[n / 2 + 1].zero?)\np ans\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\nr, c = Array.new(n + 1){0}, Array.new(n + 1){0}\nm.times do\n x, y = gets.split.map(&:to_i)\n r[x], c[y] = 1, 1\nend\nans = 0\n(2...n).step(1){|i| ans += (r[i].zero?? 1 : 0) + (c[i].zero?? 1 : 0)}\nans -= 1 if (n.odd? && r[n / 2 + 1].zero? && c[n / 2 + 1].zero?)\np ans\n"}], "negative_code": [{"source_code": "n, m = gets.split.map(&:to_i)\nrows = Array.new(n, true)\nrows[0] = rows[-1] = false\ncols = rows.dup\nfor line in (1..m)\n\tr, c = gets.split.map(&:to_i)\n\trows[r-1] = false\n\tcols[c-1] = false\nend\nrows[n/2] = false if (n & 1 and cols[n/2])\nputs rows.count(true) + cols.count(true)"}, {"source_code": "n, m = gets.split.map(&:to_i)\nrows = Array.new(n, true)\nrows[0] = rows[-1] = false\ncols = rows.dup\nfor line in (1..m)\n\tr, c = gets.split.map(&:to_i)\n\trows[r-1] = false\n\tcols[c-1] = false\nend\nrows[n/2] = false if (n/2 == 1 and cols[n/2])\nputs rows.count(true) + cols.count(true)"}], "src_uid": "a26a97586d4efb5855aa3b930e9effa7"} {"nl": {"description": "A sequence $$$a_1, a_2, \\dots, a_k$$$ is called an arithmetic progression if for each $$$i$$$ from $$$1$$$ to $$$k$$$ elements satisfy the condition $$$a_i = a_1 + c \\cdot (i - 1)$$$ for some fixed $$$c$$$.For example, these five sequences are arithmetic progressions: $$$[5, 7, 9, 11]$$$, $$$[101]$$$, $$$[101, 100, 99]$$$, $$$[13, 97]$$$ and $$$[5, 5, 5, 5, 5]$$$. And these four sequences aren't arithmetic progressions: $$$[3, 1, 2]$$$, $$$[1, 2, 4, 8]$$$, $$$[1, -1, 1, -1]$$$ and $$$[1, 2, 3, 3, 3]$$$.You are given a sequence of integers $$$b_1, b_2, \\dots, b_n$$$. Find any index $$$j$$$ ($$$1 \\le j \\le n$$$), such that if you delete $$$b_j$$$ from the sequence, you can reorder the remaining $$$n-1$$$ elements, so that you will get an arithmetic progression. If there is no such index, output the number -1.", "input_spec": "The first line of the input contains one integer $$$n$$$ ($$$2 \\le n \\le 2\\cdot10^5$$$) — length of the sequence $$$b$$$. The second line contains $$$n$$$ integers $$$b_1, b_2, \\dots, b_n$$$ ($$$-10^9 \\le b_i \\le 10^9$$$) — elements of the sequence $$$b$$$.", "output_spec": "Print such index $$$j$$$ ($$$1 \\le j \\le n$$$), so that if you delete the $$$j$$$-th element from the sequence, you can reorder the remaining elements, so that you will get an arithmetic progression. If there are multiple solutions, you are allowed to print any of them. If there is no such index, print -1.", "sample_inputs": ["5\n2 6 8 7 4", "8\n1 2 3 4 5 6 7 8", "4\n1 2 4 8"], "sample_outputs": ["4", "1", "-1"], "notes": "NoteNote to the first example. If you delete the $$$4$$$-th element, you can get the arithmetic progression $$$[2, 4, 6, 8]$$$.Note to the second example. The original sequence is already arithmetic progression, so you can delete $$$1$$$-st or last element and you will get an arithmetical progression again."}, "positive_code": [{"source_code": "n = gets.to_i\nb = gets.split.each_with_index.map{|x,i|[x.to_i,i+1]}\nb.sort_by!{|a| a[0]}\n\nif n <= 3\n p 1\n exit\nend\n\nl = b[1][0] - b[0][0]\nprev = b[1][0]\nansL = 0\n2.upto(n-1) do |i|\n if b[i][0]-prev == l\n prev = b[i][0]\n else\n if ansL > 0\n ansL = -1\n break\n else\n ansL = b[i][1]\n end\n end\nend\n\nr = b[n-2][0] - b[n-1][0]\nprev = b[n-2][0]\nansR = 0\n(n-3).downto(0) do |i|\n if b[i][0]-prev == r\n prev = b[i][0]\n else\n if ansR > 0\n ansR = -1\n break\n else\n ansR = b[i][1]\n end\n end\nend\n\nif ansL == -1 && ansR == -1\n p -1\nelsif ansL > 0\n p ansL\nelsif ansR > 0\n p ansR\nelse\n p b[0][1]\nend"}], "negative_code": [{"source_code": "n = gets.to_i\nb = gets.split.each_with_index.map{|x,i|[x.to_i,i+1]}.sort\n\nif b.length <= 3\n p 1\n exit\nend\n\nl = b[1][0]-b[0][0]\n\nprev = b[1][0]\nansL = 0\n2.upto(b.length-1) do |i|\n if b[i][0]-prev == l\n prev = b[i][0]\n else\n if ansL > 0\n ansL = -1\n break\n else\n ansL = b[i][1]\n end\n end\nend\n\nr = b[-2][0]-b[-1][0]\n\nprev = b[-2][0]\nansR = 0\n(b.length-3).downto(0) do |i|\n if b[i][0]-prev == r\n prev = b[i][0]\n else\n if ansR > 0\n ansR = -1\n break\n else\n ansR = b[i][1]\n end\n end\nend\n\nif ansL==-1 && ansR==-1\n p -1\nelsif ansL > 0\n p ansL\nelsif ansR > 0\n p ansR\nelse\n p 1\nend"}, {"source_code": "n = gets.to_i\nb = gets.split.each_with_index.map{|x,i|[x.to_i,i+1]}.sort\n\nif n <= 3\n p 1\n exit\nend\n\nl = b[1][0] - b[0][0]\nprev = b[1][0]\nansL = 0\n2.upto(n-1) do |i|\n if b[i][0]-prev == l\n prev = b[i][0]\n else\n if ansL > 0\n ansL = -1\n break\n else\n ansL = b[i][1]\n end\n end\nend\n\nr = b[n-2][0] - b[n-1][0]\nprev = b[n-2][0]\nansR = 0\n(n-3).downto(0) do |i|\n if b[i][0]-prev == r\n prev = b[i][0]\n else\n if ansR > 0\n ansR = -1\n break\n else\n ansR = b[i][1]\n end\n end\nend\n\nif ansL == -1 && ansR == -1\n p -1\nelsif ansL > 0\n p ansL\nelsif ansR > 0\n p ansR\nelse\n p 1\nend"}], "src_uid": "6946f088e462d12da47419f492ad51ea"} {"nl": {"description": "You are given a string $$$s$$$, consisting of lowercase Latin letters. Every letter appears in it no more than twice.Your task is to rearrange the letters in the string in such a way that for each pair of letters that appear exactly twice, the distance between the letters in the pair is the same. You are not allowed to add or remove letters.It can be shown that the answer always exists. If there are multiple answers, print any of them.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^3$$$) — the number of testcases. Each testcase consists of a non-empty string $$$s$$$, consisting of lowercase Latin letters. Every letter appears in the string no more than twice. The length of the string doesn't exceed $$$52$$$.", "output_spec": "For each testcase, print a single string. Every letter should appear in it the same number of times as it appears in string $$$s$$$. For each pair of letters that appear exactly twice, the distance between the letters in the pair should be the same. If there are multiple answers, print any of them.", "sample_inputs": ["3\n\noelhl\n\nabcdcba\n\nac"], "sample_outputs": ["hello\nababcdc\nac"], "notes": "NoteIn the first testcase of the example, the only letter that appears exactly twice is letter 'l'. You can rearrange the letters arbitrarily, since there are no distances to compare.In the second testcase of the example, the letters that appear exactly twice are 'a', 'b' and 'c'. Initially, letters 'a' are distance $$$6$$$ apart, letters 'b' are distance $$$4$$$ apart and letters 'c' are distance $$$2$$$ apart. They are not the same, so we have to rearrange the letters. After rearrangement, letters 'a' are distance $$$2$$$ apart, letters 'b' are distance $$$2$$$ apart and letters 'c' are distance $$$2$$$ apart. They are all the same, so the answer is valid.In the third testcase of the example, there are no letters that appear exactly twice. Thus, any rearrangement is valid. Including not changing the string at all."}, "positive_code": [{"source_code": "for i in 0..gets.chomp.to_i-1 do\r\n s = gets.chomp.split(\"\").sort.join(\"\")\r\n puts s\r\nend\r\n"}], "negative_code": [], "src_uid": "28102f75e0798960740e5a2625393c8f"} {"nl": {"description": "Alice has just learned addition. However, she hasn't learned the concept of \"carrying\" fully — instead of carrying to the next column, she carries to the column two columns to the left.For example, the regular way to evaluate the sum $$$2039 + 2976$$$ would be as shown: However, Alice evaluates it as shown: In particular, this is what she does: add $$$9$$$ and $$$6$$$ to make $$$15$$$, and carry the $$$1$$$ to the column two columns to the left, i. e. to the column \"$$$0$$$ $$$9$$$\"; add $$$3$$$ and $$$7$$$ to make $$$10$$$ and carry the $$$1$$$ to the column two columns to the left, i. e. to the column \"$$$2$$$ $$$2$$$\"; add $$$1$$$, $$$0$$$, and $$$9$$$ to make $$$10$$$ and carry the $$$1$$$ to the column two columns to the left, i. e. to the column above the plus sign; add $$$1$$$, $$$2$$$ and $$$2$$$ to make $$$5$$$; add $$$1$$$ to make $$$1$$$. Thus, she ends up with the incorrect result of $$$15005$$$.Alice comes up to Bob and says that she has added two numbers to get a result of $$$n$$$. However, Bob knows that Alice adds in her own way. Help Bob find the number of ordered pairs of positive integers such that when Alice adds them, she will get a result of $$$n$$$. Note that pairs $$$(a, b)$$$ and $$$(b, a)$$$ are considered different if $$$a \\ne b$$$.", "input_spec": "The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \\leq t \\leq 1000$$$) — the number of test cases. The description of the test cases follows. The only line of each test case contains an integer $$$n$$$ ($$$2 \\leq n \\leq 10^9$$$) — the number Alice shows Bob.", "output_spec": "For each test case, output one integer — the number of ordered pairs of positive integers such that when Alice adds them, she will get a result of $$$n$$$. ", "sample_inputs": ["5\n100\n12\n8\n2021\n10000"], "sample_outputs": ["9\n4\n7\n44\n99"], "notes": "NoteIn the first test case, when Alice evaluates any of the sums $$$1 + 9$$$, $$$2 + 8$$$, $$$3 + 7$$$, $$$4 + 6$$$, $$$5 + 5$$$, $$$6 + 4$$$, $$$7 + 3$$$, $$$8 + 2$$$, or $$$9 + 1$$$, she will get a result of $$$100$$$. The picture below shows how Alice evaluates $$$6 + 4$$$: "}, "positive_code": [{"source_code": "t = gets.to_i\r\n\r\nt.times do\r\n\r\ns = gets.chomp.chars\r\ns.unshift(\"0\") if s.size.odd?\r\n\r\nn1, n2 = s.each_slice(2).to_a.transpose.map! { |x| x.join.to_i }\r\n\r\np n1.succ * n2.succ - 2\r\n\r\nend\r\n"}], "negative_code": [], "src_uid": "8588dd273c9651f8d51cd3a2b7bd81bd"} {"nl": {"description": "This problem is same as the next one, but has smaller constraints.Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time.For each of the $$$n$$$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $$$i$$$-th day has a ribbon with color $$$u_i$$$. Shiro wants to know the largest number $$$x$$$, such that if we consider the streak of the first $$$x$$$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $$$x - 1$$$ will have the same number of occurrences.For example, consider the following sequence of $$$u_i$$$: $$$[2, 2, 1, 1, 5, 4, 4, 5]$$$. Then $$$x = 7$$$ makes a streak, since if we remove the leftmost $$$u_i = 5$$$, each ribbon color will appear exactly twice in the prefix of $$$x - 1$$$ days. Note that $$$x = 8$$$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak.", "input_spec": "The first line contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 10^5$$$) — the total number of days. The second line contains $$$n$$$ integers $$$u_1, u_2, \\ldots, u_n$$$ ($$$1 \\leq u_i \\leq 10$$$) — the colors of the ribbons the cats wear. ", "output_spec": "Print a single integer $$$x$$$ — the largest possible streak of days.", "sample_inputs": ["13\n1 1 1 2 2 2 3 3 3 4 4 4 5", "5\n10 2 5 4 1", "1\n10", "7\n3 2 1 1 4 5 1", "6\n1 1 1 2 2 2"], "sample_outputs": ["13", "5", "1", "6", "5"], "notes": "NoteIn the first example, we can choose the longest streak of $$$13$$$ days, since upon removing the last day out of the streak, all of the remaining colors $$$1$$$, $$$2$$$, $$$3$$$, and $$$4$$$ will have the same number of occurrences of $$$3$$$. Note that the streak can also be $$$10$$$ days (by removing the $$$10$$$-th day from this streak) but we are interested in the longest streak.In the fourth example, if we take the streak of the first $$$6$$$ days, we can remove the third day from this streak then all of the remaining colors $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$ and $$$5$$$ will occur exactly once."}, "positive_code": [{"source_code": "n = readline.to_i\nu = readline.split.map(&:to_i)\n\nC = 100000\n\ncs = [0] * C\ncsr = {0 => n}\ncsr.default = 0\nmax_x = 1\n(0...n).each do |i|\n ui = u[i] - 1\n c = cs[ui]\n csr[c] -= 1\n csr.delete(c) if csr[c] == 0\n c += 1\n cs[ui] = c\n csr[c] += 1\n if csr.length <= 3 then\n ks = csr.keys\n ks.delete_if { |x| x == 0 }\n if ks.length == 1 and (ks[0] == 1 or csr[ks[0]] == 1)\n max_x = i + 1\n elsif ks.length == 2\n ks = ks.sort\n if (ks[1] == ks[0] + 1 and csr[ks[1]] == 1) or (ks[0] == 1 and csr[ks[0]] == 1)\n max_x = i + 1\n end\n end\n end\nend\np max_x\n"}], "negative_code": [{"source_code": "n = readline.to_i\nu = readline.split.map(&:to_i)\n\nC = 10\n\ncs = [0] * C\ncsr = {0 => n}\ncsr.default = 0\nmax_x = 1\n(0...n).each do |i|\n ui = u[i] - 1\n c = cs[ui]\n csr[c] -= 1\n csr.delete(c) if csr[c] == 0\n c += 1\n cs[ui] = c\n csr[c] += 1\n ks = csr.keys\n if ks.length <= 3 then\n ks.delete_if { |x| x == 0 }\n if ks.length == 1 and (ks[0] == 1 or csr[ks[0]] == 1)\n max_x = i + 1\n elsif ks.length == 2\n ks = ks.sort\n if (ks[1] == ks[0] + 1 and csr[ks[1]] == 1) or csr[ks[0]] == 1\n max_x = i + 1\n end\n end\n end\nend\np max_x\n"}], "src_uid": "2d020e6c3000836e8b903a12ae39dd9b"} {"nl": {"description": "The life goes up and down, just like nice sequences. Sequence t1, t2, ..., tn is called nice if the following two conditions are satisfied: ti < ti + 1 for each odd i < n; ti > ti + 1 for each even i < n. For example, sequences (2, 8), (1, 5, 1) and (2, 5, 1, 100, 99, 120) are nice, while (1, 1), (1, 2, 3) and (2, 5, 3, 2) are not.Bear Limak has a sequence of positive integers t1, t2, ..., tn. This sequence is not nice now and Limak wants to fix it by a single swap. He is going to choose two indices i < j and swap elements ti and tj in order to get a nice sequence. Count the number of ways to do so. Two ways are considered different if indices of elements chosen for a swap are different.", "input_spec": "The first line of the input contains one integer n (2 ≤ n ≤ 150 000) — the length of the sequence. The second line contains n integers t1, t2, ..., tn (1 ≤ ti ≤ 150 000) — the initial sequence. It's guaranteed that the given sequence is not nice.", "output_spec": "Print the number of ways to swap two elements exactly once in order to get a nice sequence.", "sample_inputs": ["5\n2 8 4 7 7", "4\n200 150 100 50", "10\n3 2 1 4 1 4 1 4 1 4", "9\n1 2 3 4 5 6 7 8 9"], "sample_outputs": ["2", "1", "8", "0"], "notes": "NoteIn the first sample, there are two ways to get a nice sequence with one swap: Swap t2 = 8 with t4 = 7. Swap t1 = 2 with t5 = 7. In the second sample, there is only one way — Limak should swap t1 = 200 with t4 = 50."}, "positive_code": [{"source_code": "#http://www.codeforces.com/problemset/problem/653/C\nclass BearUpDown\n def initialize(input)\n @input = input.split.map(&:to_i)\n @n = @input.length\n end\n\n def problem_element_indices(array)\n a = array.each_cons(2).with_index.inject([]) do |acc, (element, index)|\n if (index.even? && (element.last - element.first) <= 0) || \n (index.odd? && (element.first - element.last) <= 0)\n acc << index\n end\n # return false if acc.length == 5\n acc\n end\n a\n end\n\n def happy?(array, *sites)\n sites.all? do |index|\n left = index == 0 ? nil : array[index-1]\n right = index == @n - 1 ? nil : array[index + 1]\n if index.odd?\n [left, right].compact.all? { |x| array[index] > x }\n else\n [left, right].compact.all? { |x| array[index] < x }\n end\n end\n end\n\n def swap(input, x, y)\n input[x], input[y] = input[y], input[x]\n input\n end\n\n def remove_consecutive(x)\n y = x.dup\n y.each_with_index do |e, i|\n next if i == 0\n y.delete_at(i) if e == y[i - 1] + 1\n end\n y\n end\n\n\n def run\n x = problem_element_indices(@input)\n p 0 and return if x.size > 4\n\n z = remove_consecutive(x)\n p 0 and return if z.size > 2\n\n\n result = []\n if z.size == 1\n z.map { |y| [y, y + 1]}.flatten.each do |index|\n (0..@n-1).each do |n|\n check_sites = x + [n]\n swapped = swap(@input, index, n)\n if happy?(swapped, *check_sites)\n result << [index, n]\n end\n swap(@input, n, index)\n end\n end\n else\n p1, p2 = z.map { |y| [y, y + 1]}\n check_sites = (p1 + p2)\n possible_swaps = p1.product(p2)\n possible_swaps.each do |x, y|\n swapped = swap(@input, x, y)\n if happy?(swapped, *check_sites)\n result << [x, y]\n end\n swap(@input, y, x)\n end\n end\n\n # p result\n p result.uniq { |x| x.sort.join }.count\n end\n\nend\n\nnumber = gets.chomp\nBearUpDown.new(gets.chomp).run\n# BearUpDown.new(DATA.first.chomp).run"}], "negative_code": [{"source_code": "#http://www.codeforces.com/problemset/problem/653/C\nclass BearUpDown\n def initialize(input)\n @input = input.split.map(&:to_i)\n @n = @input.length\n end\n\n def problem_element_indices(array)\n a = array.each_cons(2).with_index.inject([]) do |acc, (element, index)|\n if (index.even? && (element.last - element.first) <= 0) || \n (index.odd? && (element.first - element.last) <= 0)\n acc << index\n end\n return false if acc.length == 3\n acc\n end\n a\n end\n\n def happy?(array, *sites)\n sites.all? do |index|\n left = index == 0 ? nil : array[index-1]\n right = index == @n - 1 ? nil : array[index + 1]\n if index.odd?\n [left, right].compact.all? { |x| array[index] > x }\n else\n [left, right].compact.all? { |x| array[index] < x }\n end\n end\n end\n\n def swap(input, x, y)\n input[x], input[y] = input[y], input[x]\n input\n end\n\n def run\n x = problem_element_indices(@input)\n p 0 and return unless x\n \n if @input.first.to_i == 79667\n p x\n end\n\n if x.size == 2 && x.last == x.first + 1\n z = [x.first]\n else \n z = x\n end\n\n result = []\n if z.size == 1\n z.map { |y| [y, y + 1]}.flatten.each do |index|\n (0..@n-1).each do |n|\n check_sites = x + [n]\n swapped = swap(@input, index, n)\n if happy?(swapped, *check_sites)\n result << [index, n]\n end\n swap(@input, n, index)\n end\n end\n else\n p1, p2 = z.map { |y| [y, y + 1]}\n check_sites = (p1 + p2)\n possible_swaps = p1.product(p2)\n possible_swaps.each do |x, y|\n swapped = swap(@input, x, y)\n if happy?(swapped, *check_sites)\n result << [x, y]\n end\n swap(@input, y, x)\n end\n end\n\n # p result\n p result.uniq { |x| x.sort.join }.count\n end\n\nend\n\nnumber = gets.chomp\nBearUpDown.new(gets.chomp).run\n# BearUpDown.new(DATA.first.chomp).run"}, {"source_code": "#http://www.codeforces.com/problemset/problem/653/C\nclass BearUpDown\n def initialize(input)\n @input = input.split.map(&:to_i)\n @n = @input.length\n end\n\n def problem_element_indices(array)\n a = array.each_cons(2).with_index.inject([]) do |acc, (element, index)|\n if (index.even? && (element.last - element.first) <= 0) || \n (index.odd? && (element.first - element.last) <= 0)\n acc << index\n end\n return false if acc.length == 3\n acc\n end\n a\n end\n\n def happy?(array, *sites)\n sites.all? do |index|\n left = index == 0 ? nil : array[index-1]\n right = index == @n - 1 ? nil : array[index + 1]\n if index.odd?\n [left, right].compact.all? { |x| array[index] > x }\n else\n [left, right].compact.all? { |x| array[index] < x }\n end\n end\n end\n\n def swap(input, x, y)\n input[x], input[y] = input[y], input[x]\n input\n end\n\n def run\n x = problem_element_indices(@input)\n p 0 and return unless x\n\n if x.size == 2 && x.last == x.first + 1\n z = [x.first]\n else \n z = x\n end\n\n result = []\n if z.size == 1\n z.map { |y| [y, y + 1]}.flatten.each do |index|\n (0..@n-1).each do |n|\n check_sites = x + [n]\n swapped = swap(@input, index, n)\n if happy?(swapped, *check_sites)\n result << [index, n]\n end\n swap(@input, n, index)\n end\n end\n else\n p1, p2 = z.map { |y| [y, y + 1]}\n check_sites = (p1 + p2)\n possible_swaps = p1.product(p2)\n possible_swaps.each do |x, y|\n swapped = swap(@input, x, y)\n if happy?(swapped, *check_sites)\n result << [x, y]\n end\n swap(@input, y, x)\n end\n end\n\n # p result\n p result.uniq { |x| x.sort.join }.count\n end\n\nend\n\nnumber = gets.chomp\nBearUpDown.new(gets.chomp).run\n# BearUpDown.new(DATA.first.chomp).run"}, {"source_code": "#http://www.codeforces.com/problemset/problem/653/C\nclass BearUpDown\n def initialize(input)\n @input = input.split.map(&:to_i)\n @n = @input.length\n end\n\n def problem_element_indices(array)\n a = array.each_cons(2).with_index.inject([]) do |acc, (element, index)|\n if (index.even? && (element.last - element.first) <= 0) || \n (index.odd? && (element.first - element.last) <= 0)\n acc << index\n end\n return false if acc.length == 5\n acc\n end\n a\n end\n\n def happy?(array, *sites)\n sites.all? do |index|\n left = index == 0 ? nil : array[index-1]\n right = index == @n - 1 ? nil : array[index + 1]\n if index.odd?\n [left, right].compact.all? { |x| array[index] > x }\n else\n [left, right].compact.all? { |x| array[index] < x }\n end\n end\n end\n\n def swap(input, x, y)\n input[x], input[y] = input[y], input[x]\n input\n end\n\n def run\n x = problem_element_indices(@input)\n p 0 and return unless x\n\n if @input.first == 79667\n p x\n end\n\n if x.size == 2 && x.last == x.first + 1\n z = [x.first]\n else \n z = x\n end\n\n result = []\n if z.size == 1\n z.map { |y| [y, y + 1]}.flatten.each do |index|\n (0..@n-1).each do |n|\n check_sites = x + [n]\n swapped = swap(@input, index, n)\n if happy?(swapped, *check_sites)\n result << [index, n]\n end\n swap(@input, n, index)\n end\n end\n else\n p1, p2 = z.map { |y| [y, y + 1]}\n check_sites = (p1 + p2)\n possible_swaps = p1.product(p2)\n possible_swaps.each do |x, y|\n swapped = swap(@input, x, y)\n if happy?(swapped, *check_sites)\n result << [x, y]\n end\n swap(@input, y, x)\n end\n end\n\n # p result\n p result.uniq { |x| x.sort.join }.count\n end\n\nend\n\nnumber = gets.chomp\nBearUpDown.new(gets.chomp).run\n# BearUpDown.new(DATA.first.chomp).run"}, {"source_code": "#http://www.codeforces.com/problemset/problem/653/C\nclass BearUpDown\n def initialize(input)\n @input = input.split.map(&:to_i)\n @n = @input.length\n end\n\n def problem_element_indices(array)\n a = array.each_cons(2).with_index.inject([]) do |acc, (element, index)|\n if (index.even? && (element.last - element.first) <= 0) || \n (index.odd? && (element.first - element.last) <= 0)\n acc << index\n end\n # return false if acc.length == 5\n acc\n end\n a\n end\n\n def happy?(array, *sites)\n sites.all? do |index|\n left = index == 0 ? nil : array[index-1]\n right = index == @n - 1 ? nil : array[index + 1]\n if index.odd?\n [left, right].compact.all? { |x| array[index] > x }\n else\n [left, right].compact.all? { |x| array[index] < x }\n end\n end\n end\n\n def swap(input, x, y)\n input[x], input[y] = input[y], input[x]\n input\n end\n\n def run\n x = problem_element_indices(@input)\n z = if x.size == 1\n x\n else\n x.each_cons(2).map { |x, y| y == x + 1 ? [x, x] : [x, y]}.flatten.uniq\n end\n p 0 and return if z.size > 2\n\n\n result = []\n if z.size == 1\n z.map { |y| [y, y + 1]}.flatten.each do |index|\n (0..@n-1).each do |n|\n check_sites = x + [n]\n swapped = swap(@input, index, n)\n if happy?(swapped, *check_sites)\n result << [index, n]\n end\n swap(@input, n, index)\n end\n end\n else\n p1, p2 = z.map { |y| [y, y + 1]}\n check_sites = (p1 + p2)\n possible_swaps = p1.product(p2)\n possible_swaps.each do |x, y|\n swapped = swap(@input, x, y)\n if happy?(swapped, *check_sites)\n result << [x, y]\n end\n swap(@input, y, x)\n end\n end\n\n # p result\n p result.uniq { |x| x.sort.join }.count\n end\n\nend\n\nnumber = gets.chomp\nBearUpDown.new(gets.chomp).run\n# BearUpDown.new(DATA.first.chomp).run"}, {"source_code": "#http://www.codeforces.com/problemset/problem/653/C\nclass BearUpDown\n def initialize(input)\n @input = input.split.map(&:to_i)\n @n = @input.length\n end\n\n def problem_element_indices(array)\n a = array.each_cons(2).with_index.inject([]) do |acc, (element, index)|\n if (index.even? && (element.last - element.first) <= 0) || \n (index.odd? && (element.first - element.last) <= 0)\n acc << index\n end\n # return false if acc.length == 5\n acc\n end\n a\n end\n\n def happy?(array, *sites)\n sites.all? do |index|\n left = index == 0 ? nil : array[index-1]\n right = index == @n - 1 ? nil : array[index + 1]\n if index.odd?\n [left, right].compact.all? { |x| array[index] > x }\n else\n [left, right].compact.all? { |x| array[index] < x }\n end\n end\n end\n\n def swap(input, x, y)\n input[x], input[y] = input[y], input[x]\n input\n end\n\n def run\n x = problem_element_indices(@input)\n # p 0 and return unless x\n\n\n if x.size > 2\n if @input.first == 79667\n p x\n else\n p 0 and return\n end\n end\n\n if x.size == 2 && x.last == x.first + 1\n z = [x.first]\n else \n z = x\n end\n\n result = []\n if z.size == 1\n z.map { |y| [y, y + 1]}.flatten.each do |index|\n (0..@n-1).each do |n|\n check_sites = x + [n]\n swapped = swap(@input, index, n)\n if happy?(swapped, *check_sites)\n result << [index, n]\n end\n swap(@input, n, index)\n end\n end\n else\n p1, p2 = z.map { |y| [y, y + 1]}\n check_sites = (p1 + p2)\n possible_swaps = p1.product(p2)\n possible_swaps.each do |x, y|\n swapped = swap(@input, x, y)\n if happy?(swapped, *check_sites)\n result << [x, y]\n end\n swap(@input, y, x)\n end\n end\n\n # p result\n p result.uniq { |x| x.sort.join }.count\n end\n\nend\n\nnumber = gets.chomp\nBearUpDown.new(gets.chomp).run\n# BearUpDown.new(DATA.first.chomp).run"}, {"source_code": "#http://www.codeforces.com/problemset/problem/653/C\nclass BearUpDown\n def initialize(input)\n @input = input.split.map(&:to_i)\n end\n\n def problem_element_indices(array)\n a = array.each_cons(2).with_index.inject([]) do |acc, (element, index)|\n if (index.even? && (element.last - element.first) <= 0) || \n (index.odd? && (element.first - element.last) <= 0)\n acc << index\n end\n return false if acc.length > 5\n acc\n end\n a.map { |x| [x, x + 1]}.flatten\n end\n\n def unhappy?(array)\n array.each_cons(2).with_index.any? do |element, index|\n if (index.even? && (element.last - element.first) <= 0) || \n (index.odd? && (element.first - element.last) <= 0)\n 1\n end\n end\n end\n\n def happy_after_swap?(array, x, y)\n input = array.dup\n input[x], input[y] = input[y], input[x]\n !unhappy?(input)\n end\n\n def run\n all_combos = (0..@input.length - 1).to_a\n x = problem_element_indices(@input)\n return 0 unless x\n swapping_combos = x.product(all_combos)\n .uniq { |x| x.sort.join }\n .select { |x, y| x != y}\n\n result = swapping_combos.select do |x, y|\n happy_after_swap?(@input, x, y)\n end\n\n p result.count\n end\n\nend\n\nnumber = gets.chomp\nBearUpDown.new(gets.chomp).run\n"}, {"source_code": "#http://www.codeforces.com/problemset/problem/653/C\nclass BearUpDown\n def initialize(input)\n @input = input.split.map(&:to_i)\n @n = @input.length\n end\n\n def problem_element_indices(array)\n a = array.each_cons(2).with_index.inject([]) do |acc, (element, index)|\n if (index.even? && (element.last - element.first) <= 0) || \n (index.odd? && (element.first - element.last) <= 0)\n acc << index\n end\n return false if acc.length == 3\n acc\n end\n a\n end\n\n def happy?(array, *sites)\n sites.all? do |index|\n left = index == 0 ? nil : array[index-1]\n right = index == @n - 1 ? nil : array[index + 1]\n if index.odd?\n [left, right].compact.all? { |x| array[index] > x }\n else\n [left, right].compact.all? { |x| array[index] < x }\n end\n end\n end\n\n def swap(input, x, y)\n input[x], input[y] = input[y], input[x]\n input\n end\n\n def run\n x = problem_element_indices(@input)\n p 0 and return unless x\n if x.size == 2 && x.last == x.first + 1\n z = [x.first]\n else \n z = x\n end\n\n result = []\n if z.size == 1\n z.map { |y| [y, y + 1]}.flatten.each do |index|\n (0..@n-1).each do |n|\n check_sites = x + [n]\n swapped = swap(@input, index, n)\n if happy?(swapped, *check_sites)\n result << [index, n]\n end\n swap(@input, n, index)\n end\n end\n else\n p1, p2 = z.map { |y| [y, y + 1]}\n check_sites = (p1 + p2)\n possible_swaps = p1.product(p2)\n possible_swaps.each do |x, y|\n swapped = swap(@input, x, y)\n if happy?(swapped, *check_sites)\n result << [x, y]\n end\n swap(@input, y, x)\n end\n end\n\n # p result\n p result.uniq { |x| x.sort.join }.count\n p x\n end\n\nend\n\nnumber = gets.chomp\nBearUpDown.new(gets.chomp).run\n# BearUpDown.new(DATA.first.chomp).run"}, {"source_code": "#http://www.codeforces.com/problemset/problem/653/C\nclass BearUpDown\n def initialize(input)\n @input = input.split.map(&:to_i)\n @n = @input.length\n end\n\n def problem_element_indices(array)\n a = array.each_cons(2).with_index.inject([]) do |acc, (element, index)|\n if (index.even? && (element.last - element.first) <= 0) || \n (index.odd? && (element.first - element.last) <= 0)\n acc << index\n end\n return false if acc.length > 5\n acc\n end\n a.map { |x| [x, x + 1]}.flatten\n end\n\n def unhappy?(array)\n array.each_cons(2).with_index.any? do |element, index|\n if (index.even? && (element.last - element.first) <= 0) || \n (index.odd? && (element.first - element.last) <= 0)\n 1\n end\n end\n end\n\n def happy_after_swap?(array, x, y)\n input = array.dup\n input[x], input[y] = input[y], input[x]\n !unhappy?(input)\n end\n\n def run\n x = problem_element_indices(@input)\n p 0 and return unless x\n\n p x\n result = []\n x.each do |index|\n left = index == 0 ? nil : @input[index-1]\n right = index == @n - 1 ? nil : @input[index + 1]\n\n if index.odd?\n swap_floor = [left, right].compact.max\n (0..@n-1).each do |n|\n next if index == n\n next unless (@input[n] > swap_floor || x.include?(n))\n if happy_after_swap?(@input, index, n)\n result << [index, n]\n end\n end\n else\n swap_ceiling = [left, right].compact.min\n (0..@n-1).each do |n|\n next if index == n\n next unless (@input[n] < swap_ceiling || x.include?(n))\n if happy_after_swap?(@input, index, n)\n result << [index, n]\n end\n end\n end\n end\n\n # p result\n p result.uniq { |x| x.sort.join }.count\n end\n\nend\n\nnumber = gets.chomp\nBearUpDown.new(gets.chomp).run\n# BearUpDown.new(DATA.first.chomp).run\n\n"}, {"source_code": "#http://www.codeforces.com/problemset/problem/653/C\nclass BearUpDown\n def initialize(input)\n @input = input.split.map(&:to_i)\n @n = @input.length\n end\n\n def problem_element_indices(array)\n a = array.each_cons(2).with_index.inject([]) do |acc, (element, index)|\n if (index.even? && (element.last - element.first) <= 0) || \n (index.odd? && (element.first - element.last) <= 0)\n acc << index\n end\n # return false if acc.length == 5\n acc\n end\n a\n end\n\n def happy?(array, *sites)\n sites.all? do |index|\n left = index == 0 ? nil : array[index-1]\n right = index == @n - 1 ? nil : array[index + 1]\n if index.odd?\n [left, right].compact.all? { |x| array[index] > x }\n else\n [left, right].compact.all? { |x| array[index] < x }\n end\n end\n end\n\n def swap(input, x, y)\n input[x], input[y] = input[y], input[x]\n input\n end\n\n def remove_consecutive(x)\n y = x.dup\n y.each_with_index do |e, i|\n next if i == 0\n y.delete_at(i) if e == y[i - 1] + 1\n end\n y\n end\n\n\n def run\n x = problem_element_indices(@input)\n p 0 and return if x.size > 3\n\n z = remove_consecutive(x)\n p 0 and return if z.size > 2\n\n\n result = []\n if z.size == 1\n z.map { |y| [y, y + 1]}.flatten.each do |index|\n (0..@n-1).each do |n|\n check_sites = x + [n]\n swapped = swap(@input, index, n)\n if happy?(swapped, *check_sites)\n result << [index, n]\n end\n swap(@input, n, index)\n end\n end\n else\n p1, p2 = z.map { |y| [y, y + 1]}\n check_sites = (p1 + p2)\n possible_swaps = p1.product(p2)\n possible_swaps.each do |x, y|\n swapped = swap(@input, x, y)\n if happy?(swapped, *check_sites)\n result << [x, y]\n end\n swap(@input, y, x)\n end\n end\n\n # p result\n p result.uniq { |x| x.sort.join }.count\n end\n\nend\n\nnumber = gets.chomp\nBearUpDown.new(gets.chomp).run\n# BearUpDown.new(DATA.first.chomp).run"}, {"source_code": "#http://www.codeforces.com/problemset/problem/653/C\nclass BearUpDown\n def initialize(input)\n @input = input.split.map(&:to_i)\n end\n\n def problem_element_indices(array)\n a = array.each_cons(2).with_index.inject([]) do |acc, (element, index)|\n if (index.even? && (element.last - element.first) <= 0) || \n (index.odd? && (element.first - element.last) <= 0)\n acc << index\n end\n return false if acc.length > 5\n acc\n end\n a.map { |x| [x, x + 1]}.flatten\n end\n\n def unhappy?(array)\n array.each_cons(2).with_index.any? do |element, index|\n if (index.even? && (element.last - element.first) <= 0) || \n (index.odd? && (element.first - element.last) <= 0)\n 1\n end\n end\n end\n\n def happy_after_swap?(array, x, y)\n input = array.dup\n input[x], input[y] = input[y], input[x]\n !unhappy?(input)\n end\n\n def run\n x = problem_element_indices(@input)\n if @input.length == 150000\n if x\n p 1\n else\n p 0\n end\n return\n end\n p 0 and return unless x\n all_combos = (0..@input.length - 1).to_a\n swapping_combos = x.product(all_combos)\n .uniq { |x| x.sort.join }\n .select { |x, y| x != y}\n\n result = swapping_combos.select do |x, y|\n happy_after_swap?(@input, x, y)\n end\n\n p result.count\n end\n\nend\n\nnumber = gets.chomp\nBearUpDown.new(gets.chomp).run\n# BearUpDown.new(DATA.first.chomp).run\n\n"}, {"source_code": "#http://www.codeforces.com/problemset/problem/653/C\nclass BearUpDown\n def initialize(input)\n @input = input.split.map(&:to_i)\n end\n\n def problem_element_indices(array)\n a = array.each_cons(2).with_index.inject([]) do |acc, (element, index)|\n if (index.even? && (element.last - element.first) <= 0) || \n (index.odd? && (element.first - element.last) <= 0)\n acc << index\n end\n return false if acc.length > 5\n acc\n end\n a.map { |x| [x, x + 1]}.flatten\n end\n\n def unhappy?(array)\n array.each_cons(2).with_index.any? do |element, index|\n if (index.even? && (element.last - element.first) <= 0) || \n (index.odd? && (element.first - element.last) <= 0)\n 1\n end\n end\n end\n\n def happy_after_swap?(array, x, y)\n input = array.dup\n input[x], input[y] = input[y], input[x]\n !unhappy?(input)\n end\n\n def run\n p @input.length and return\n all_combos = (0..@input.length - 1).to_a\n x = problem_element_indices(@input)\n p 0 and return unless x\n swapping_combos = x.product(all_combos)\n .uniq { |x| x.sort.join }\n .select { |x, y| x != y}\n\n result = swapping_combos.select do |x, y|\n happy_after_swap?(@input, x, y)\n end\n\n p result.count\n end\n\nend\n\nnumber = gets.chomp\nBearUpDown.new(gets.chomp).run\n# BearUpDown.new(DATA.first.chomp).run\n"}, {"source_code": "#http://www.codeforces.com/problemset/problem/653/C\nclass BearUpDown\n def initialize(input)\n @input = input.split.map(&:to_i)\n end\n\n def problem_element_indices(array)\n a = array.each_cons(2).with_index.inject([]) do |acc, (element, index)|\n if (index.even? && (element.last - element.first) <= 0) || \n (index.odd? && (element.first - element.last) <= 0)\n acc << index\n end\n return false if acc.length > 5\n acc\n end\n a.map { |x| [x, x + 1]}.flatten\n end\n\n def unhappy?(array)\n array.each_cons(2).with_index.any? do |element, index|\n if (index.even? && (element.last - element.first) <= 0) || \n (index.odd? && (element.first - element.last) <= 0)\n 1\n end\n end\n end\n\n def happy_after_swap?(array, x, y)\n input = array.dup\n input[x], input[y] = input[y], input[x]\n !unhappy?(input)\n end\n\n def run\n if @input.length == 150000\n p \"reaching here\" and return\n end\n \n all_combos = (0..@input.length - 1).to_a\n x = problem_element_indices(@input)\n p 0 and return unless x\n swapping_combos = x.product(all_combos)\n .uniq { |x| x.sort.join }\n .select { |x, y| x != y}\n\n result = swapping_combos.select do |x, y|\n happy_after_swap?(@input, x, y)\n end\n\n p result.count\n end\n\nend\n\nnumber = gets.chomp\nBearUpDown.new(gets.chomp).run\n"}, {"source_code": "#http://www.codeforces.com/problemset/problem/653/C\nclass BearUpDown\n def initialize(input)\n @input = input.split.map(&:to_i)\n end\n\n def problem_element_indices(array)\n a = array.each_cons(2).with_index.inject([]) do |acc, (element, index)|\n if (index.even? && (element.last - element.first) <= 0) || \n (index.odd? && (element.first - element.last) <= 0)\n acc << index\n end\n return false if acc.length > 5\n acc\n end\n a.map { |x| [x, x + 1]}.flatten\n end\n\n def unhappy?(array)\n array.each_cons(2).with_index.any? do |element, index|\n if (index.even? && (element.last - element.first) <= 0) || \n (index.odd? && (element.first - element.last) <= 0)\n 1\n end\n end\n end\n\n def happy_after_swap?(array, x, y)\n input = array.dup\n input[x], input[y] = input[y], input[x]\n !unhappy?(input)\n end\n\n def run\n x = problem_element_indices(@input)\n if @input.length == 150000\n if x\n p x.join(',')\n else\n p 0\n end\n return\n end\n p 0 and return unless x\n all_combos = (0..@input.length - 1).to_a\n swapping_combos = x.product(all_combos)\n .uniq { |x| x.sort.join }\n .select { |x, y| x != y}\n\n result = swapping_combos.select do |x, y|\n happy_after_swap?(@input, x, y)\n end\n\n p result.count\n end\n\nend\n\nnumber = gets.chomp\nBearUpDown.new(gets.chomp).run\n# BearUpDown.new(DATA.first.chomp).run\n\n"}, {"source_code": "#http://www.codeforces.com/problemset/problem/653/C\nclass BearUpDown\n def initialize(input)\n @input = input.split.map(&:to_i)\n end\n\n def problem_element_indices(array)\n a = array.each_cons(2).with_index.inject([]) do |acc, (element, index)|\n if (index.even? && (element.last - element.first) <= 0) || \n (index.odd? && (element.first - element.last) <= 0)\n acc << index\n end\n return false if acc.length > 5\n acc\n end\n a.map { |x| [x, x + 1]}.flatten\n end\n\n def unhappy?(array)\n array.each_cons(2).with_index.any? do |element, index|\n if (index.even? && (element.last - element.first) <= 0) || \n (index.odd? && (element.first - element.last) <= 0)\n 1\n end\n end\n end\n\n def happy_after_swap?(array, x, y)\n input = array.dup\n input[x], input[y] = input[y], input[x]\n !unhappy?(input)\n end\n\n def run\n x = problem_element_indices(@input)\n if x\n p 1\n else\n p 0\n end\n return\n # p 0 and return unless x\n all_combos = (0..@input.length - 1).to_a\n swapping_combos = x.product(all_combos)\n .uniq { |x| x.sort.join }\n .select { |x, y| x != y}\n\n result = swapping_combos.select do |x, y|\n happy_after_swap?(@input, x, y)\n end\n\n p result.count\n end\n\nend\n\nnumber = gets.chomp\nBearUpDown.new(gets.chomp).run\n# BearUpDown.new(DATA.first.chomp).run\n\n"}, {"source_code": "#http://www.codeforces.com/problemset/problem/653/C\nclass BearUpDown\n def initialize(input)\n @input = input.split.map(&:to_i)\n @n = @input.length\n end\n\n def problem_element_indices(array)\n a = array.each_cons(2).with_index.inject([]) do |acc, (element, index)|\n if (index.even? && (element.last - element.first) <= 0) || \n (index.odd? && (element.first - element.last) <= 0)\n acc << index\n end\n # return false if acc.length == 5\n acc\n end\n a\n end\n\n def happy?(array, *sites)\n sites.all? do |index|\n left = index == 0 ? nil : array[index-1]\n right = index == @n - 1 ? nil : array[index + 1]\n if index.odd?\n [left, right].compact.all? { |x| array[index] > x }\n else\n [left, right].compact.all? { |x| array[index] < x }\n end\n end\n end\n\n def swap(input, x, y)\n input[x], input[y] = input[y], input[x]\n input\n end\n\n def remove_consecutive(x)\n y = x.dup\n y.each_with_index do |e, i|\n next if i == 0\n y.delete_at(i) if e == y[i - 1] + 1\n end\n y\n end\n\n\n def run\n x = problem_element_indices(@input)\n p 0 and return if x.size > 3\n z = remove_consecutive(x)\n # if @input.first == 150000\n # p x\n # end\n\n\n result = []\n if z.size == 1\n z.map { |y| [y, y + 1]}.flatten.each do |index|\n (0..@n-1).each do |n|\n check_sites = x + [n]\n swapped = swap(@input, index, n)\n if happy?(swapped, *check_sites)\n result << [index, n]\n end\n swap(@input, n, index)\n end\n end\n else\n p1, p2 = z.map { |y| [y, y + 1]}\n check_sites = (p1 + p2)\n possible_swaps = p1.product(p2)\n possible_swaps.each do |x, y|\n swapped = swap(@input, x, y)\n if happy?(swapped, *check_sites)\n result << [x, y]\n end\n swap(@input, y, x)\n end\n end\n\n # p result\n p result.uniq { |x| x.sort.join }.count\n end\n\nend\n\nnumber = gets.chomp\nBearUpDown.new(gets.chomp).run\n# BearUpDown.new(DATA.first.chomp).run"}, {"source_code": "#http://www.codeforces.com/problemset/problem/653/C\nclass BearUpDown\n def initialize(input)\n @input = input.split.map(&:to_i)\n @n = @input.length\n end\n\n def problem_element_indices(array)\n a = array.each_cons(2).with_index.inject([]) do |acc, (element, index)|\n if (index.even? && (element.last - element.first) <= 0) || \n (index.odd? && (element.first - element.last) <= 0)\n acc << index\n end\n return false if acc.length == 3\n acc\n end\n a\n end\n\n def happy?(array, *sites)\n sites.all? do |index|\n left = index == 0 ? nil : array[index-1]\n right = index == @n - 1 ? nil : array[index + 1]\n if index.odd?\n [left, right].compact.all? { |x| array[index] > x }\n else\n [left, right].compact.all? { |x| array[index] < x }\n end\n end\n end\n\n def swap(input, x, y)\n input[x], input[y] = input[y], input[x]\n input\n end\n\n def run\n x = problem_element_indices(@input)\n p 0 and return unless x\n p x\n if x.size == 2 && x.last == x.first + 1\n z = [x.first]\n else \n z = x\n end\n\n result = []\n if z.size == 1\n z.map { |y| [y, y + 1]}.flatten.each do |index|\n (0..@n-1).each do |n|\n check_sites = x + [n]\n swapped = swap(@input, index, n)\n if happy?(swapped, *check_sites)\n result << [index, n]\n end\n swap(@input, n, index)\n end\n end\n else\n p1, p2 = z.map { |y| [y, y + 1]}\n check_sites = (p1 + p2)\n possible_swaps = p1.product(p2)\n possible_swaps.each do |x, y|\n swapped = swap(@input, x, y)\n if happy?(swapped, *check_sites)\n result << [x, y]\n end\n swap(@input, y, x)\n end\n end\n\n # p result\n p result.uniq { |x| x.sort.join }.count\n end\n\nend\n\nnumber = gets.chomp\nBearUpDown.new(gets.chomp).run\n# BearUpDown.new(DATA.first.chomp).run"}], "src_uid": "e5b0e43beaca9baf428afec6ce454c50"} {"nl": {"description": "Currently Tiny is learning Computational Geometry. When trying to solve a problem called \"The Closest Pair Of Points In The Plane\", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is .The pseudo code of the unexpected code is as follows:input nfor i from 1 to n input the i-th point's coordinates into p[i]sort array p[] by increasing of x coordinate first and increasing of y coordinate secondd=INF //here INF is a number big enoughtot=0for i from 1 to n for j from (i+1) to n ++tot if (p[j].x-p[i].x>=d) then break //notice that \"break\" is only to be //out of the loop \"for j\" d=min(d,distance(p[i],p[j]))output dHere, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded.You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded?", "input_spec": "A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109).", "output_spec": "If there doesn't exist such a data which let the given code get TLE, print \"no solution\" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point. The conditions below must be held: All the points must be distinct. |xi|, |yi| ≤ 109. After running the given code, the value of tot should be larger than k. ", "sample_inputs": ["4 3", "2 100"], "sample_outputs": ["0 0\n0 1\n1 0\n1 1", "no solution"], "notes": null}, "positive_code": [{"source_code": "n, k = gets.split.map(&:to_i)\nputs 2 * k >= n * (n - 1) ? \"no solution\" : [*1..n].map{|i|\"0 \"+i.to_s}\n\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\nputs 2 * k >= n * (n - 1) ? \"no solution\" : [*1..n].map{|i|\"0 \"+i.to_s}\n\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\nputs 2 * k >= n * (n - 1) ? \"no solution\" : [*1..n].map{|i|\"0 \"+i.to_s}\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\nputs 2 * k >= n * (n - 1) ? \"no solution\" : [*1..n].map{|i|\"0 \"+i.to_s}\n\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\nputs 2 * k >= n * (n - 1) ? \"no solution\" : [*1..n].map{|i|\"0 \"+i.to_s}\n\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\nputs 2 * k >= n * (n - 1) ? \"no solution\" : [*1..n].map{|i|\"0 \"+i.to_s}\n\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\nputs 2 * k >= n * (n - 1) ? \"no solution\" : [*1..n].map{|i|\"0 \"+i.to_s}\n\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\nif (n - 1) * n > k * 2\n n.times do |i|\n puts [0, i].join(' ')\n end\nelse\n puts \"no solution\"\nend\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\nputs 2 * k >= n * (n - 1) ? \"no solution\" : [*1..n].map{|i|\"0 \"+i.to_s}\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\nputs 2 * k >= n * (n - 1) ? \"no solution\" : [*1..n].map{|i|\"0 \"+i.to_s}\n\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\nputs 2 * k >= n * (n - 1) ? \"no solution\" : [*1..n].map{|i|\"0 \"+i.to_s}\n\n"}, {"source_code": "#!/usr/bin/ruby1.9\n\npoint_num, limit = STDIN.readline.split.collect {|s| s.to_i }\n\nif point_num*(point_num-1)/2 <= limit\n\tputs 'no solution'\n\texit\nend\n\n(point_num-1).times do |i|\n\tputs '%d %d' % [i, i*10000]\nend\nputs '%d %d' % [point_num, 0]\n"}, {"source_code": "n,k = gets().split().map{ |x| x.to_i}\nif (1.0*n*(n-1)/2 <= k)\n puts \"no solution\";\nelse\n (1..n).each do |i|\n puts \"0 %d\" % [i];\n end\nend"}, {"source_code": "input = STDIN.read\n\ninput =~ /^(\\d+) (\\d+)/\nn, k = $1.to_i, $2.to_i\nl = (n * (n - 1)) / 2\n\nif (k >= l)\n puts 'no solution'\nelse\n (0...n).each { |y| puts \"0 #{y}\" }\nend\n"}, {"source_code": "n, k = gets.chomp.split.map { |num| num.to_i }\nif k >= ((n - 1) * n / 2)\n puts \"no solution\" \nelse\n (1..n).each do |i|\n puts \"0 #{i}\"\n end\nend\n"}, {"source_code": "n,k = gets.chomp.split(\" \").collect {|x| x.to_i}\n\nif k>=(n*(n-1)/2)\n puts \"no solution\"\nelse\n (1..n).each { |k|\n puts \"0 #{k}\"\n }\nend"}, {"source_code": "n,k = gets.strip.split(' ').map {|x| x.to_i}\n \nif( k>= (n*(n-1))/2 )\n puts 'no solution'\nelse\n n.times do |i|\n puts \"0 #{i*2}\"\n end\nend"}, {"source_code": "#!/usr/bin/ruby\n\nn, k = gets.split(\" \").map(&:to_i)\n\ntot = n * (n - 1) / 2\n\nif tot <= k\n puts \"no solution\"\nelse\n 0.upto(n - 1) {|y| puts \"0 #{y}\" }\nend\n"}], "negative_code": [{"source_code": "n, k = gets.split.map(&:to_i)\nif (n - 1) * n > k\n n.times do |i|\n puts [0, i].join(' ')\n end\nelse\n puts \"no solution\"\nend\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\nputs k >= (n == 2 ? 1 : 3 * n - 6) ? \"no solution\" : ([*2..n]<<-3000).map{|i|i.to_s+\" 0\"}.reverse\n"}, {"source_code": "n,k = gets().split().map{ |x| x.to_i}\nif (1.0*n*(n-1)/2 < k)\n puts \"no solution\";\nelse\n (1..n).each do |i|\n puts \"0 %d\" % [i];\n end\nend"}, {"source_code": "input = STDIN.read\n\ninput =~ /^(\\d+) (\\d+)/\nn, k = $1.to_i, $2.to_i\nl = (n * (n - 1)) / 2\n\nif (k > l)\n puts 'no solution'\nelse\n (0...n).each { |y| puts \"0 #{y}\" }\nend\n\n"}, {"source_code": "n,k = gets.chomp.split(\" \").collect {|x| x.to_i}\n\nif k>(n*(n-1)/2)\n puts \"no solution\"\nelse\n (1..n).each { |k|\n puts \"0 #{k}\"\n }\nend"}, {"source_code": "n,k = gets.chomp.split(\" \").collect {|x| x.to_i}\n\nif k>(n*(n+1)/2)\n puts \"no solution\"\nelse\n (1..n).each { |k|\n puts \"0 #{k}\"\n }\nend"}], "src_uid": "a7846e4ae1f3fa5051fab9139a25539c"} {"nl": {"description": "You are given two huge binary integer numbers $$$a$$$ and $$$b$$$ of lengths $$$n$$$ and $$$m$$$ respectively. You will repeat the following process: if $$$b > 0$$$, then add to the answer the value $$$a~ \\&~ b$$$ and divide $$$b$$$ by $$$2$$$ rounding down (i.e. remove the last digit of $$$b$$$), and repeat the process again, otherwise stop the process.The value $$$a~ \\&~ b$$$ means bitwise AND of $$$a$$$ and $$$b$$$. Your task is to calculate the answer modulo $$$998244353$$$.Note that you should add the value $$$a~ \\&~ b$$$ to the answer in decimal notation, not in binary. So your task is to calculate the answer in decimal notation. For example, if $$$a = 1010_2~ (10_{10})$$$ and $$$b = 1000_2~ (8_{10})$$$, then the value $$$a~ \\&~ b$$$ will be equal to $$$8$$$, not to $$$1000$$$.", "input_spec": "The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n, m \\le 2 \\cdot 10^5$$$) — the length of $$$a$$$ and the length of $$$b$$$ correspondingly. The second line of the input contains one huge integer $$$a$$$. It is guaranteed that this number consists of exactly $$$n$$$ zeroes and ones and the first digit is always $$$1$$$. The third line of the input contains one huge integer $$$b$$$. It is guaranteed that this number consists of exactly $$$m$$$ zeroes and ones and the first digit is always $$$1$$$.", "output_spec": "Print the answer to this problem in decimal notation modulo $$$998244353$$$.", "sample_inputs": ["4 4\n1010\n1101", "4 5\n1001\n10101"], "sample_outputs": ["12", "11"], "notes": "NoteThe algorithm for the first example: add to the answer $$$1010_2~ \\&~ 1101_2 = 1000_2 = 8_{10}$$$ and set $$$b := 110$$$; add to the answer $$$1010_2~ \\&~ 110_2 = 10_2 = 2_{10}$$$ and set $$$b := 11$$$; add to the answer $$$1010_2~ \\&~ 11_2 = 10_2 = 2_{10}$$$ and set $$$b := 1$$$; add to the answer $$$1010_2~ \\&~ 1_2 = 0_2 = 0_{10}$$$ and set $$$b := 0$$$. So the answer is $$$8 + 2 + 2 + 0 = 12$$$.The algorithm for the second example: add to the answer $$$1001_2~ \\&~ 10101_2 = 1_2 = 1_{10}$$$ and set $$$b := 1010$$$; add to the answer $$$1001_2~ \\&~ 1010_2 = 1000_2 = 8_{10}$$$ and set $$$b := 101$$$; add to the answer $$$1001_2~ \\&~ 101_2 = 1_2 = 1_{10}$$$ and set $$$b := 10$$$; add to the answer $$$1001_2~ \\&~ 10_2 = 0_2 = 0_{10}$$$ and set $$$b := 1$$$; add to the answer $$$1001_2~ \\&~ 1_2 = 1_2 = 1_{10}$$$ and set $$$b := 0$$$. So the answer is $$$1 + 8 + 1 + 0 + 1 = 11$$$."}, "positive_code": [{"source_code": "\nMOD = 998244353\n\ndef solution\n read_ints\n\n astr = read_string.reverse\n bstr = read_string\n\n cumm = 0\n b = bstr.chars.map { |i| cumm += i.to_i; b = cumm }\n\n b = b.reverse\n\n total = 0\n\n pow = nil\n\n astr.chars.each.with_index do |c, i|\n pow = (pow ? pow * 2 : 1) % MOD\n next if c != ?1\n break unless b[i]\n total += pow * b[i]\n total = total % MOD\n end\n\n puts total\n\nend\n\ndef read_int\n gets.to_i\nend\n\ndef read_ints\n gets.split.map(&:to_i)\nend\n\ndef read_string\n gets.chomp\nend\n\nsolution unless ENV['TEST__MODE']\n\n"}], "negative_code": [], "src_uid": "d2fe5a201d1ec20c5b32cd99b54e13d0"} {"nl": {"description": "You are given two arrays of integers a and b. For each element of the second array bj you should find the number of elements in array a that are less than or equal to the value bj.", "input_spec": "The first line contains two integers n, m (1 ≤ n, m ≤ 2·105) — the sizes of arrays a and b. The second line contains n integers — the elements of array a ( - 109 ≤ ai ≤ 109). The third line contains m integers — the elements of array b ( - 109 ≤ bj ≤ 109).", "output_spec": "Print m integers, separated by spaces: the j-th of which is equal to the number of such elements in array a that are less than or equal to the value bj.", "sample_inputs": ["5 4\n1 3 5 7 9\n6 4 2 8", "5 5\n1 2 1 2 5\n3 1 4 1 5"], "sample_outputs": ["3 2 1 4", "4 2 4 2 5"], "notes": null}, "positive_code": [{"source_code": "gets\na=gets.split.map(&:to_i).sort\nputs gets.split.map(&:to_i).map{|e|\n\t(0...a.size).bsearch{|i|a[i]>e}||a.size\n}*' '"}, {"source_code": "n, m = gets.chomp.split.map(&:to_i)\nas = gets.chomp.split.map(&:to_i)\nbs = gets.chomp.split.map(&:to_i)\n\nas.sort!\ncnt = 0\nhs = Hash.new(0)\nas.each do |a|\n cnt += 1\n hs[a] = cnt\nend\nas.reverse!\ncs = []\nbs.each do |b|\n cs << hs[as.bsearch {|a| a <= b }]\nend\n\nputs cs.*' '\n"}, {"source_code": "n, m = gets.chomp.split(\" \").map(&:to_i)\na = gets.chomp.split(\" \").map(&:to_i).sort\nb = gets.chomp.split(\" \").map(&:to_i)\nres = Array.new()\nb.each do |e|\n tmp = (0...n).bsearch {|i| a[i] > e}\n if tmp.nil? then\n res << n\n else\n res << tmp\n end\nend\nputs res.join(\" \")"}], "negative_code": [], "src_uid": "e9a519be33f25c828bae787330c18dd4"} {"nl": {"description": "Saitama accidentally destroyed a hotel again. To repay the hotel company, Genos has volunteered to operate an elevator in one of its other hotels. The elevator is special — it starts on the top floor, can only move down, and has infinite capacity. Floors are numbered from 0 to s and elevator initially starts on floor s at time 0.The elevator takes exactly 1 second to move down exactly 1 floor and negligible time to pick up passengers. Genos is given a list detailing when and on which floor passengers arrive. Please determine how long in seconds it will take Genos to bring all passengers to floor 0.", "input_spec": "The first line of input contains two integers n and s (1 ≤ n ≤ 100, 1 ≤ s ≤ 1000) — the number of passengers and the number of the top floor respectively. The next n lines each contain two space-separated integers fi and ti (1 ≤ fi ≤ s, 1 ≤ ti ≤ 1000) — the floor and the time of arrival in seconds for the passenger number i.", "output_spec": "Print a single integer — the minimum amount of time in seconds needed to bring all the passengers to floor 0.", "sample_inputs": ["3 7\n2 1\n3 8\n5 2", "5 10\n2 77\n3 33\n8 21\n9 12\n10 64"], "sample_outputs": ["11", "79"], "notes": "NoteIn the first sample, it takes at least 11 seconds to bring all passengers to floor 0. Here is how this could be done:1. Move to floor 5: takes 2 seconds.2. Pick up passenger 3.3. Move to floor 3: takes 2 seconds.4. Wait for passenger 2 to arrive: takes 4 seconds.5. Pick up passenger 2.6. Go to floor 2: takes 1 second.7. Pick up passenger 1.8. Go to floor 0: takes 2 seconds.This gives a total of 2 + 2 + 4 + 1 + 2 = 11 seconds."}, "positive_code": [{"source_code": "#!/usr/bin/ruby\nn,f=gets.split.map(&:to_i)\nr=f\nn.times{\n\ta,b=gets.split.map(&:to_i)\n\tr=[r,[f-a,b].max+a].max\n}\np r"}, {"source_code": "n, s = gets.chomp.split.map(&:to_i)\nfloors = []\nn.times { floors << gets.chomp.split.map(&:to_i) }\n\nc = 0\nidx = s\nfloors.reverse.each do |a, b|\n c += (idx - a)\n c += (b > c) ? (b - c) : 0\n idx = a\nend\nputs c + idx\n"}, {"source_code": "#! ruby\n# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n############################################################\ndef gs() gets.chomp end\ndef gi() gets.to_i end\ndef gss() gs.split end\ndef gis() gss.map(&:to_i) end\ndef nmapf(n,f) n.times.map{ __send__ f } end\ndef ngs(n) nmapf n,:gs end\ndef ngi(n) nmapf n,:gi end\ndef ngss(n) nmapf n,:gss end\ndef ngis(n) nmapf n,:gis end\ndef arr2d(h,w,v=0) h.times.map{[v] * w} end\ndef for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end\ndef nsum(n) n * (n + 1) / 2 end\ndef vcount(d,r=Hash.new(0)) d.inject(r){|r,e| r[e]+=1;r} end\n############################################################\n\n\nn, s = gis\n\nmx = s\n\nn.times do\n f, t = gis\n mx = [f + t, mx].max\nend\n\nputs mx\n\n\n"}, {"source_code": "n, s = gets.split.map(&:to_i)\nn.times do\n\tt, f = gets.split.map(&:to_i)\n\ts = [s, t+f].max\nend\nputs s\n"}, {"source_code": "def Int_maker(a)\n\t0.upto(a.length-1) {|i| a[i]=a[i].to_i }\n\treturn a\nend\ninfo=Int_maker(gets.chomp.split(\" \"))\npassenger=[]\n0.upto(info[0]-1){|i| passenger[i]=Int_maker(gets.chomp.split(\" \"))}\npassenger.sort!\npassenger.reverse!\nk=info[1]\ntime=0\n0.upto(info[0]-1) do |i|\n\tif i==0\n\t\ttime=k-(passenger[0][0])\n\t\tif time<(passenger[i][1])\n\t\t\ttime=(passenger[i][1])\n\t\tend\n\telse\n\t\ttime=time+(passenger[i-1][0])-(passenger[i][0])\n\t\tif time<(passenger[i][1])\n\t\t\ttime=(passenger[i][1])\n\t\tend\n\tend\nend\ntime=time+passenger[passenger.length-1][0]\nputs time\n\t\n\n"}, {"source_code": "a = gets.split.map(&:to_i)\nn = a[0]\ns = a[1]\n\nmax = s\n(1..n).each do\n\ta = gets.split.map(&:to_i)\n\tsum = a[0] + a[1]\n\tmax = sum if sum > max\nend\n\nputs max"}, {"source_code": "def Int_maker(a)\n\t0.upto(a.length-1) {|i| a[i]=a[i].to_i }\n\treturn a\nend\ninfo=Int_maker(gets.chomp.split(\" \"))\npassenger=[]\n0.upto(info[0]-1){|i| passenger[i]=Int_maker(gets.chomp.split(\" \"))}\npassenger.sort!\npassenger.reverse!\nk=info[1]\ntime=0\n0.upto(info[0]-1) do |i|\n\tif i==0\n\t\ttime=k-(passenger[0][0])\n\t\tif time<(passenger[i][1])\n\t\t\ttime=(passenger[i][1])\n\t\tend\n\telse\n\t\ttime=time+(passenger[i-1][0])-(passenger[i][0])\n\t\tif time<(passenger[i][1])\n\t\t\ttime=(passenger[i][1])\n\t\tend\n\tend\nend\ntime=time+passenger[passenger.length-1][0]\nputs time"}, {"source_code": "arr1 = gets.chomp.split(/ /).map(&:to_i)\nnum = arr1[0]\nfloor_max = arr1[1]\npass = Array.new\ntime = Array.new\nindex=0\nnum.times do\n\tstr = gets.chomp.split(/ /).map(&:to_i)\n\tpass[index] = str[0]\n\ttime[index] = str[1]\n\tindex+=1\nend \n\n_final=0\n\nindex=floor_max\nloop do \n\tbreak if index < 0\n\tindex1=0\n\tloop do \n\t\tbreak if index1>=num\n\t\tif pass[index1] == index && time[index1]>_final\n\t\t\t_final = time[index1]\n\t\tend\n\t\tindex1+=1\n\tend\n\t#puts \"Final is #{_final} and index is #{index}\"\n\t\n\tindex=index-1\n\tbreak if index < 0\n\t_final+=1\nend\n\nputs _final"}, {"source_code": "n, s = gets.chomp.split.map(&:to_i)\nans = s\nn.times do\n f, t = gets.chomp.split.map(&:to_i)\n ans = [ans, f + t].max\nend\nputs ans\n"}, {"source_code": "sum, top_floor = gets.split.map(&:to_i)\nwait = Hash.new{|hsh,key| hsh[key] = [] }\nwait[0].push 0\n\nfor i in 0..sum-1\n f_t = gets.split\n floor = f_t[0].to_i\n time = f_t[1].to_i\n\twait[floor].push time\nend\n\nwait = wait.sort.reverse\nall_time = 0\n\nfor i in 0..wait.length-1\n\n shift = wait.shift\n c_floor = shift[0]\n c_floor_time = shift[1]\n\n\tif top_floor > c_floor \n\t\tall_time += top_floor - c_floor\n\t\ttop_floor = c_floor\n\tend\n\n\tif top_floor.to_i == c_floor.to_i\n\t\tc_floor_time = c_floor_time.sort.reverse\n\t\tc_floor_time[0] = c_floor_time[0] - all_time\n\n\t\tif c_floor_time[0] < 0\n\t\t\tc_floor_time[0] = 0 \n\t\tend\n\n\t\tall_time +=c_floor_time[0]\n\tend\nend\np all_time"}, {"source_code": "n, s = gets.split.map(&:to_i)\na = n.times.map{gets.split.map(&:to_i)}.reverse + [[0, 0]]\ncurrent_floor = s\ntime_spent = 0\na.each do |f, t|\n time_spent += [current_floor - f, t - time_spent].max\n current_floor = f\nend\nputs time_spent\n"}, {"source_code": "a = gets.split(' ').collect {|x| x.to_i}\nn = a[0]\ns = a[1]\nresult = s\n(1..n).each do\n a = gets.split(' ').collect {|x| x.to_i}\n result = a[0] + a[1] if a[0]+a[1] > result\nend\n\nputs result"}, {"source_code": "n, s = gets.chomp.split(\" \").map(&:to_i)\npsgs = Array.new()\nn.times do\n psgs << gets.chomp.split(\" \").map(&:to_i)\nend\npsgs = psgs.sort_by{|x| x[0]}.reverse\nres = 0\ncurrent = s\npsgs.each do |f, t|\n res += current - f\n current = f\n if res < t then\n res = t\n end\nend\nif current != 0 then\n res += current\nend\nputs res\n"}, {"source_code": "n, s = gets.split.map(&:to_i)\nmap = Hash.new\n(1..n).each do\n floor, time = gets.split.map(&:to_i)\n if map[floor] == nil\n map[floor] = time\n else\n map[floor] = [map[floor], time].max\n end\nend\n\ncurr = s\ntime = 0\ns.downto(1).each do |floor|\n if map[floor] == nil\n next\n end\n time = [time+curr-floor, map[floor]].max\n curr = floor\nend\n\ntime += curr\n\nputs time\n\n"}, {"source_code": "def Int_maker(a)\n\t0.upto(a.length-1) {|i| a[i]=a[i].to_i }\n\treturn a\nend\ninfo=Int_maker(gets.chomp.split(\" \"))\npassenger=[]\n0.upto(info[0]-1){|i| passenger[i]=Int_maker(gets.chomp.split(\" \"))}\npassenger.sort!\npassenger.reverse!\nk=info[1]\ntime=0\n0.upto(info[0]-1) do |i|\n\tif i==0\n\t\ttime=k-(passenger[0][0])\n\t\tif time<(passenger[i][1])\n\t\t\ttime=(passenger[i][1])\n\t\tend\n\telse\n\t\ttime=time+(passenger[i-1][0])-(passenger[i][0])\n\t\tif time<(passenger[i][1])\n\t\t\ttime=(passenger[i][1])\n\t\tend\n\tend\nend\ntime=time+passenger[passenger.length-1][0]\nputs time\n\t\n"}, {"source_code": "n, s = gets.split.map(&:to_i)\nn.times do\n\tt, f = gets.split.map(&:to_i)\n\ts = [s, t+f].max\nend\nputs s"}, {"source_code": "class Solver\n\tdef main\n\t\tn, s = gets.split.map { |x| x.to_i }\n\t\tans = []\n\t\tn.times do\n\t\t\tf, t = gets.split.map { |x| x.to_i }\n\t\t\tans << [t, s - f].max + f\n\t\tend\n\t\tputs ans.max\n\tend\nend\nSolver.new.main"}], "negative_code": [{"source_code": "def Int_maker(a)\n\t0.upto(a.length-1) {|i| a[i]=a[i].to_i }\n\treturn a\nend\ninfo=Int_maker(gets.chomp.split(\" \"))\npassenger=[]\n0.upto(info[0]-1){|i| passenger[i]=Int_maker(gets.chomp.split(\" \"))}\npassenger.sort!\npassenger.reverse!\nk=info[1]-1\ntime=0\n0.upto(info[0]-1) do |i|\n\tif i==0\n\t\ttime=k-(passenger[0][0])\n\t\tif time<(passenger[i][1])\n\t\t\ttime=(passenger[i][1])\n\t\tend\n\telse\n\t\ttime=time+(passenger[i-1][0])-(passenger[i][0])\n\t\tif time<(passenger[i][1])\n\t\t\ttime=(passenger[i][1])\n\t\tend\n\tend\nend\ntime=time+passenger[passenger.length-1][0]\nputs time\n\t\n\n"}, {"source_code": "def Int_maker(a)\n\t0.upto(a.length-1) {|i| a[i]=a[i].to_i }\n\treturn a\nend\ninfo=Int_maker(gets.chomp.split(\" \"))\npassenger=[]\n0.upto(info[0]-1){|i| passenger[i]=Int_maker(gets.chomp.split(\" \"))}\npassenger.sort!\npassenger.reverse!\nk=info[1]-1\ntime=0\n0.upto(info[0]-1) do |i|\n\tif i==0\n\t\ttime=k-(passenger[0][0])\n\telse\n\t\ttime=time+(passenger[i-1][0])-(passenger[i][0])\n\t\tif time<(passenger[i][1])\n\t\t\ttime=(passenger[i][1])\n\t\tend\n\tend\nend\ntime=time+passenger[passenger.length-1][0]\nputs time\n\t\n\n"}, {"source_code": "n, s = gets.chomp.split.map(&:to_i)\nans = s + 1\nn.times do\n f, t = gets.chomp.split.map(&:to_i)\n ans = [ans, f + t].max\nend\nputs ans\n"}, {"source_code": "s_f = gets.split\nsum = s_f[0].to_i \ntop_floor = s_f[1].to_i \n\nwait = Hash.new\nfor i in 0..sum-1\n\tf_t = gets.split\n\tfloor = f_t[0].to_i\n\ttime = f_t[1].to_i\n\twait[floor] = time\nend\n\nwait = wait.sort.reverse\nstep_flag = 0\n\nall_time = 0\nfor i in 0..sum-1\n\t#Записываем значение wait в top и wait_time\n\tshift = wait.shift\n\tc_floor = shift[0]\n\tc_wait_time = shift[1]\n\tif step_flag==0\n\t\tstep = top_floor - c_floor\n\tend\n\tstep_flag = 1\n\n\t#Время ожидания у текущего пассажира\n\twait_time = c_wait_time - all_time\n\tif wait_time < 0\n\t\twait_time = 0\n\tend\n\n\n\tall_time += wait_time\nend\nall_time+=step+1\nputs all_time"}, {"source_code": "n, s = gets.split.map(&:to_i)\nmap = Hash.new\n(1..n).each do\n floor, time = gets.split.map(&:to_i)\n map[floor] = time\nend\n\ncurr = s\ntime = 0\ns.downto(0).each do |floor|\n if map[floor] == nil\n next\n end\n time = [time+curr-floor, map[floor]].max\n curr = floor\nend\n\ntime += curr\n\nputs time\n\n"}], "src_uid": "5c12573b3964ee30af0349c11c0ced3b"} {"nl": {"description": "This problem is given in two editions, which differ exclusively in the constraints on the number $$$n$$$.You are given an array of integers $$$a[1], a[2], \\dots, a[n].$$$ A block is a sequence of contiguous (consecutive) elements $$$a[l], a[l+1], \\dots, a[r]$$$ ($$$1 \\le l \\le r \\le n$$$). Thus, a block is defined by a pair of indices $$$(l, r)$$$.Find a set of blocks $$$(l_1, r_1), (l_2, r_2), \\dots, (l_k, r_k)$$$ such that: They do not intersect (i.e. they are disjoint). Formally, for each pair of blocks $$$(l_i, r_i)$$$ and $$$(l_j, r_j$$$) where $$$i \\neq j$$$ either $$$r_i < l_j$$$ or $$$r_j < l_i$$$. For each block the sum of its elements is the same. Formally, $$$$$$a[l_1]+a[l_1+1]+\\dots+a[r_1]=a[l_2]+a[l_2+1]+\\dots+a[r_2]=$$$$$$ $$$$$$\\dots =$$$$$$ $$$$$$a[l_k]+a[l_k+1]+\\dots+a[r_k].$$$$$$ The number of the blocks in the set is maximum. Formally, there does not exist a set of blocks $$$(l_1', r_1'), (l_2', r_2'), \\dots, (l_{k'}', r_{k'}')$$$ satisfying the above two requirements with $$$k' > k$$$. The picture corresponds to the first example. Blue boxes illustrate blocks. Write a program to find such a set of blocks.", "input_spec": "The first line contains integer $$$n$$$ ($$$1 \\le n \\le 50$$$) — the length of the given array. The second line contains the sequence of elements $$$a[1], a[2], \\dots, a[n]$$$ ($$$-10^5 \\le a_i \\le 10^5$$$).", "output_spec": "In the first line print the integer $$$k$$$ ($$$1 \\le k \\le n$$$). The following $$$k$$$ lines should contain blocks, one per line. In each line print a pair of indices $$$l_i, r_i$$$ ($$$1 \\le l_i \\le r_i \\le n$$$) — the bounds of the $$$i$$$-th block. You can print blocks in any order. If there are multiple answers, print any of them.", "sample_inputs": ["7\n4 1 2 2 1 5 3", "11\n-5 -4 -3 -2 -1 0 1 2 3 4 5", "4\n1 1 1 1"], "sample_outputs": ["3\n7 7\n2 3\n4 5", "2\n3 4\n1 1", "4\n4 4\n1 1\n2 2\n3 3"], "notes": null}, "positive_code": [{"source_code": "N = gets.to_i\nA = gets.split.map &:to_i\nss = [0]\nA.each{ |a| ss << (ss[-1] + a) }\nsegs = {}\n(1..N).each{ |j|\n (1..j).each{ |i|\n seg = [i,j]\n v = ss[j] - ss[i-1]\n segs[v] ||= []\n segs[v] << seg\n }\n}\n\nseqs = []\nsegs.each{ |v,ss|\n rb = 0\n sss = []\n ss.each{ |l,r|\n if rb < l\n sss << [l,r]\n rb = r\n end\n }\n if seqs.size < sss.size\n seqs = sss\n end\n}\n\np seqs.size\nseqs.each{ |a,b|\n puts [a,b] * \" \"\n}"}, {"source_code": "N = gets.to_i\nA = gets.split.map &:to_i\nss = [0]\nA.each{ |a| ss << (ss[-1] + a) }\nans = 0\nseqs = []\n(0...N).each{ |j|\n (0..j).each{ |i|\n m = 1\n qs = [[i+1,j+1]]\n d = ss[j+1] - ss[i]\n k0 = j+1\n (j+1...N).each{ |k|\n (k0..k).each{ |l|\n e = ss[k+1] - ss[l]\n if d == e\n m += 1\n qs << [l+1,k+1]\n k0 = k+1\n break\n end\n }\n }\n if m > ans\n ans = m\n seqs = qs\n end\n }\n}\nputs ans\nseqs.each{ |a,b| puts [a,b] * \" \" }\n"}], "negative_code": [{"source_code": "N = gets.to_i\nA = gets.split.map &:to_i\nss = [0]\nA.each{ |a| ss << (ss[-1] + a) }\nans = 0\nseqs = []\n(0...N).each{ |j|\n (0..j).each{ |i|\n m = 1\n qs = [[i+1,j+1]]\n d = ss[j+1] - ss[i]\n k0 = j+1\n (j+1...N).each{ |k|\n (k0..k).each{ |l|\n e = ss[k+1] - ss[l]\n if d == e\n m += 1\n qs << [l+1,k+1]\n k0 = k+1\n end\n }\n }\n if m > ans\n ans = m\n seqs = qs\n end\n }\n}\nputs ans\nseqs.each{ |a,b| puts [a,b] * \" \" }"}, {"source_code": "N = gets.to_i\nA = gets.split.map &:to_i\nss = [0]\nA.each{ |a| ss << (ss[-1] + a) }\nans = 0\nseqs = []\np A, ss\n(0...N).each{ |j|\n (0..j).each{ |i|\n m = 1\n qs = [[i+1,j+1]]\n d = ss[j+1] - ss[i]\n k0 = j+1\n (j+1...N).each{ |k|\n (k0..k).each{ |l|\n e = ss[k+1] - ss[l]\n if d == e\n m += 1\n qs << [l+1,k+1]\n k0 = k+1\n end\n }\n }\n if m > ans\n ans = m\n seqs = qs\n end\n }\n}\nputs ans\nseqs.each{ |a,b| puts [a,b] * \" \" }"}], "src_uid": "3e834f89ecedc5f210e680f24e40ba19"} {"nl": {"description": "Sasha likes investigating different math objects, for example, magic squares. But Sasha understands that magic squares have already been studied by hundreds of people, so he sees no sense of studying them further. Instead, he invented his own type of square — a prime square. A square of size $$$n \\times n$$$ is called prime if the following three conditions are held simultaneously: all numbers on the square are non-negative integers not exceeding $$$10^5$$$; there are no prime numbers in the square; sums of integers in each row and each column are prime numbers. Sasha has an integer $$$n$$$. He asks you to find any prime square of size $$$n \\times n$$$. Sasha is absolutely sure such squares exist, so just help him!", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10$$$) — the number of test cases. Each of the next $$$t$$$ lines contains a single integer $$$n$$$ ($$$2 \\le n \\le 100$$$) — the required size of a square.", "output_spec": "For each test case print $$$n$$$ lines, each containing $$$n$$$ integers — the prime square you built. If there are multiple answers, print any.", "sample_inputs": ["2\n4\n2"], "sample_outputs": ["4 6 8 1\n4 9 9 9\n4 10 10 65\n1 4 4 4\n1 1\n1 1"], "notes": null}, "positive_code": [{"source_code": "T = gets.to_i\n\nT.times do\n n = gets.to_i\n ans = Array.new(n) { Array.new(n, 0) }\n\n n.times do |y|\n 2.times do |x|\n ans[y][(y + x) % n] = 1\n end\n end\n\n puts ans.map { |row| row.join(' ') }\nend\n"}, {"source_code": "t = gets.chomp.to_i\nt.times do\n n = gets.chomp.to_i\n arr = Array.new(n){[0] * n}\n n.times do |i|\n arr[i][i] = 1\n if (i + 1 < n)\n arr[i][i + 1] = 1\n end\n end\n arr[1][0] = arr[n - 1][n - 2] = 1\n n.times do |i|\n puts arr[i].join(\" \")\n end\nend"}, {"source_code": "t = gets.to_i\n\n1.upto t do\n n = gets.to_i\n arr = Array.new(n) { Array.new(n, 0) }\n\n 0.upto n-1 do |i|\n arr[i][i] = 1\n arr[i][n-1-i] = 1\n end\n\n if n.odd?\n arr[(n-1)/2][((n-1)/2)+1] = 1\n arr[(n+1)/2][(n-1)/2] = 1\n end\n\n puts arr.map { |x| x.join(' ') }\nend\n"}], "negative_code": [], "src_uid": "df6ee0d8bb25dc2040adf1f115f4a83b"} {"nl": {"description": "You are given a number $$$n$$$ (divisible by $$$3$$$) and an array $$$a[1 \\dots n]$$$. In one move, you can increase any of the array elements by one. Formally, you choose the index $$$i$$$ ($$$1 \\le i \\le n$$$) and replace $$$a_i$$$ with $$$a_i + 1$$$. You can choose the same index $$$i$$$ multiple times for different moves.Let's denote by $$$c_0$$$, $$$c_1$$$ and $$$c_2$$$ the number of numbers from the array $$$a$$$ that have remainders $$$0$$$, $$$1$$$ and $$$2$$$ when divided by the number $$$3$$$, respectively. Let's say that the array $$$a$$$ has balanced remainders if $$$c_0$$$, $$$c_1$$$ and $$$c_2$$$ are equal.For example, if $$$n = 6$$$ and $$$a = [0, 2, 5, 5, 4, 8]$$$, then the following sequence of moves is possible: initially $$$c_0 = 1$$$, $$$c_1 = 1$$$ and $$$c_2 = 4$$$, these values are not equal to each other. Let's increase $$$a_3$$$, now the array $$$a = [0, 2, 6, 5, 4, 8]$$$; $$$c_0 = 2$$$, $$$c_1 = 1$$$ and $$$c_2 = 3$$$, these values are not equal. Let's increase $$$a_6$$$, now the array $$$a = [0, 2, 6, 5, 4, 9]$$$; $$$c_0 = 3$$$, $$$c_1 = 1$$$ and $$$c_2 = 2$$$, these values are not equal. Let's increase $$$a_1$$$, now the array $$$a = [1, 2, 6, 5, 4, 9]$$$; $$$c_0 = 2$$$, $$$c_1 = 2$$$ and $$$c_2 = 2$$$, these values are equal to each other, which means that the array $$$a$$$ has balanced remainders. Find the minimum number of moves needed to make the array $$$a$$$ have balanced remainders.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$). Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$3 \\le n \\le 3 \\cdot 10^4$$$) — the length of the array $$$a$$$. It is guaranteed that the number $$$n$$$ is divisible by $$$3$$$. The next line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$0 \\leq a_i \\leq 100$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$150\\,000$$$.", "output_spec": "For each test case, output one integer — the minimum number of moves that must be made for the $$$a$$$ array to make it have balanced remainders.", "sample_inputs": ["4\n6\n0 2 5 5 4 8\n6\n2 0 2 1 0 0\n9\n7 1 3 4 2 10 3 9 6\n6\n0 1 2 3 4 5"], "sample_outputs": ["3\n1\n3\n0"], "notes": "NoteThe first test case is explained in the statements.In the second test case, you need to make one move for $$$i=2$$$.The third test case you need to make three moves: the first move: $$$i=9$$$; the second move: $$$i=9$$$; the third move: $$$i=2$$$. In the fourth test case, the values $$$c_0$$$, $$$c_1$$$ and $$$c_2$$$ initially equal to each other, so the array $$$a$$$ already has balanced remainders."}, "positive_code": [{"source_code": "def method(h, n)\r\n res = 0\r\n (0..2).each { |v| h[v] = 0 unless h.key?(v) }\r\n while h.max_by { |k, v| v }[1] > n\r\n k, v = h.each.max_by { |k, v| v }\r\n h[k] = n\r\n h[(k + 1) % 3] += v - n\r\n res += v - n\r\n end\r\n res\r\nend\r\n \r\nt = gets.to_i\r\nt.times do\r\n n = gets.to_i\r\n a = gets.split.map(&:to_i)\r\n puts method(a.group_by { |x| x % 3 }.transform_values(&:size), n / 3)\r\nend"}, {"source_code": "def method(h, n)\r\n res = 0\r\n (0..2).each { |v| h[v] = 0 unless h.key?(v) }\r\n while h.each_value.max > n\r\n k, v = h.each.max_by { |k, v| v }\r\n h[k] = n\r\n h[(k + 1) % 3] += v - n\r\n res += v - n\r\n end\r\n res\r\nend\r\n \r\nt = gets.to_i\r\nt.times do\r\n n = gets.to_i\r\n a = gets.split.map(&:to_i)\r\n puts method(a.group_by { |x| x % 3 }.transform_values(&:size), n / 3)\r\nend\r\n# "}, {"source_code": "def method(h, n)\r\n res = 0\r\n (0..2).each { |v| h[v] = 0 unless h.key?(v) }\r\n while h.each_value.max > n\r\n k, v = h.each.max_by { |k, v| v }\r\n h[k] = n\r\n h[(k + 1) % 3] += v - n\r\n res += v - n\r\n end\r\n res\r\nend\r\n \r\nt = gets.to_i\r\nt.times do\r\n n = gets.to_i\r\n a = gets.split.map(&:to_i)\r\n puts method(a.group_by { |x| x % 3 }.transform_values(&:size), n / 3)\r\nend"}, {"source_code": "def method(h, n)\r\n res = 0\r\n (0..2).each { |v| h[v] = 0 unless h.key?(v) }\r\n h.each do |k, v|\r\n if v > n\r\n res += v - n\r\n h[k] = n\r\n h[(k + 1) % 3] += v - n\r\n end\r\n end while h.values.max > n\r\n res\r\nend\r\n \r\nt = gets.to_i\r\nt.times do\r\n n = gets.to_i\r\n a = gets.split.map(&:to_i)\r\n puts method(a.group_by { |x| x % 3 }.transform_values(&:size), n / 3)\r\nend"}, {"source_code": "def method(h, n)\r\n res = 0\r\n (0..2).each { |v| h[v] = 0 unless h.key?(v) }\r\n while h.values.max > n\r\n k, v = h.each.max_by { |k, v| v }\r\n h[k] = n\r\n h[(k + 1) % 3] += v - n\r\n res += v - n\r\n end\r\n res\r\nend\r\n \r\nt = gets.to_i\r\nt.times do\r\n n = gets.to_i\r\n a = gets.split.map(&:to_i)\r\n puts method(a.group_by { |x| x % 3 }.transform_values(&:size), n / 3)\r\nend\r\n# "}, {"source_code": "def method(h, n)\r\n res = 0\r\n (0..2).each { |v| h[v] = 0 unless h.key?(v) }\r\n while h.values.max > n\r\n k, v = h.each.max_by { |k, v| v }\r\n h[k] = n\r\n h[(k + 1) % 3] += v - n\r\n res += v - n\r\n end\r\n res\r\nend\r\n \r\nt = gets.to_i\r\nt.times do\r\n n = gets.to_i\r\n a = gets.split.map(&:to_i)\r\n puts method(a.group_by { |x| x % 3 }.transform_values(&:size), n / 3)\r\nend"}, {"source_code": "# frozen_string_literal: true\r\n\r\n\r\ntest = gets.to_i\r\ntest.times do\r\n n = gets.chomp.to_i\r\n numbers = gets.chomp.split.map(&:to_i)\r\n remainders = []\r\n (0..2).each do |index|\r\n remainders << numbers.count{|x| x % 3 == index}\r\n end\r\n\r\n\r\n\r\n result = 0\r\n while idx = (0..2).detect{|x| remainders[x] > n / 3} \r\n result += remainders[idx] - n / 3\r\n remainders[(idx+1)% 3] += remainders[idx] - n / 3\r\n remainders[idx] = n / 3\r\n end\r\n puts result\r\nend\r\n"}, {"source_code": "gets.to_i.times do\n\tn = gets.to_i\n\ta = gets.split.map(&:to_i)\n\tc = (0...3).map { |k| a.count { _1 % 3 == k } }\n\n\tans = 0\n\twhile true\n\t\ti = c.index { _1 > n / 3 }\n\t\tbreak unless i\n\t\tans += 1\n\t\tc[i] -= 1\n\t\tc[-~i % 3] += 1\n\tend\n\tputs ans\nend\n"}, {"source_code": "def solve\r\n count = [0, 0, 0]\r\n n = gets.to_i\r\n arr = gets.split(' ').map(&:to_i)\r\n arr.each do |i|\r\n count[i % 3] += 1\r\n end\r\n\r\n ans = 0\r\n\r\n 2.times do\r\n 3.times do |i|\r\n if count[i] > n / 3\r\n ans += count[i] - n / 3\r\n count[(i + 1) % 3] += count[i] - n / 3\r\n count[i] = n / 3\r\n end\r\n end\r\n end\r\n\r\n puts ans\r\nend\r\n\r\n\r\nt = gets.to_i\r\nt.times { solve }"}, {"source_code": "gets;$<.map{c=[0]*3;gets.split.each{|i|c[i.to_i%3]+=1};p [0,1,2].map{|i|c[i]-c[i-1]}.max}"}, {"source_code": "gets.to_i.times do\r\n n=gets.to_i/3\r\n a=gets.split.map(&:to_i)\r\n c0,c1,c2=0,0,0\r\n a.each do |aa|\r\n if aa%3==0\r\n c0+=1\r\n elsif aa%3==1\r\n c1+=1\r\n else\r\n c2+=1\r\n end\r\n end\r\n c=0\r\n while !(c0==c1&&c1==c2)\r\n if c0>n\r\n c1+=c0-n\r\n c+=c0-n\r\n c0=n\r\n elsif c1>n\r\n c2+=c1-n\r\n c+=c1-n\r\n c1=n\r\n elsif c2>n\r\n c0+=c2-n\r\n c+=c2-n\r\n c2=n\r\n end\r\n end\r\n puts c\r\nend\r\n"}], "negative_code": [], "src_uid": "e0de8a6441614d1e41a53223b5fa576b"} {"nl": {"description": "There are n sharks who grow flowers for Wet Shark. They are all sitting around the table, such that sharks i and i + 1 are neighbours for all i from 1 to n - 1. Sharks n and 1 are neighbours too.Each shark will grow some number of flowers si. For i-th shark value si is random integer equiprobably chosen in range from li to ri. Wet Shark has it's favourite prime number p, and he really likes it! If for any pair of neighbouring sharks i and j the product si·sj is divisible by p, then Wet Shark becomes happy and gives 1000 dollars to each of these sharks.At the end of the day sharks sum all the money Wet Shark granted to them. Find the expectation of this value.", "input_spec": "The first line of the input contains two space-separated integers n and p (3 ≤ n ≤ 100 000, 2 ≤ p ≤ 109) — the number of sharks and Wet Shark's favourite prime number. It is guaranteed that p is prime. The i-th of the following n lines contains information about i-th shark — two space-separated integers li and ri (1 ≤ li ≤ ri ≤ 109), the range of flowers shark i can produce. Remember that si is chosen equiprobably among all integers from li to ri, inclusive.", "output_spec": "Print a single real number — the expected number of dollars that the sharks receive in total. You answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .", "sample_inputs": ["3 2\n1 2\n420 421\n420420 420421", "3 5\n1 4\n2 3\n11 14"], "sample_outputs": ["4500.0", "0.0"], "notes": "NoteA prime number is a positive integer number that is divisible only by 1 and itself. 1 is not considered to be prime.Consider the first sample. First shark grows some number of flowers from 1 to 2, second sharks grows from 420 to 421 flowers and third from 420420 to 420421. There are eight cases for the quantities of flowers (s0, s1, s2) each shark grows: (1, 420, 420420): note that s0·s1 = 420, s1·s2 = 176576400, and s2·s0 = 420420. For each pair, 1000 dollars will be awarded to each shark. Therefore, each shark will be awarded 2000 dollars, for a total of 6000 dollars. (1, 420, 420421): now, the product s2·s0 is not divisible by 2. Therefore, sharks s0 and s2 will receive 1000 dollars, while shark s1 will receive 2000. The total is 4000. (1, 421, 420420): total is 4000 (1, 421, 420421): total is 0. (2, 420, 420420): total is 6000. (2, 420, 420421): total is 6000. (2, 421, 420420): total is 6000. (2, 421, 420421): total is 4000.The expected value is .In the second sample, no combination of quantities will garner the sharks any money."}, "positive_code": [{"source_code": "n, p = gets.chomp.split(\" \").map(&:to_i)\nsharks = []\n\nn.times do\n l, r = gets.chomp.split(\" \").map(&:to_i)\n\n x, y = l, r\n if x % p != 0\n x += p-x%p\n end\n y -= y%p\n dividends = (y-x)/p+1\n\n sharks << {l: l, r: r, dividends: dividends, total: r-l+1}\nend\n\nsharks << sharks.first\n\n# puts sharks.inspect\n\nsum = sharks.each_cons(2).map do |shark_a, shark_b|\n shark_a_not_p = 1 - shark_a[:dividends]/shark_a[:total].to_f\n shark_b_not_p = 1 - shark_b[:dividends]/shark_b[:total].to_f\n\n (1 - (shark_a_not_p * shark_b_not_p))*2000\nend.inject(0, :+)\n\nputs sum\n"}, {"source_code": "n, p = gets.chomp.split.map(&:to_i)\nls = []\nrs = []\nn.times do\n l, r = gets.chomp.split.map(&:to_i)\n ls << l\n rs << r\nend\n\ne = 0.0\nfor i in 0...n\n e += 2000.0 * (1.0 - (1.0 - (rs[i] / p - (ls[i] - 1) / p) / (rs[i] - ls[i] + 1).to_f) * (1.0 - (rs[i - 1] / p - (ls[i - 1] - 1) / p) / (rs[i - 1] - ls[i - 1] + 1).to_f))\nend\n\nputs e\n"}, {"source_code": "def solve(a, p)\n q = a.map do |l, r|\n (r / p - (l - 1) / p).to_f / (r - l + 1)\n end\n 2000 * ( 2*q[1..-1].reduce(0, :+) - q.each_cons(2).map {|q1, q2| q1*q2}.reduce(0, :+))\nend\n\nn, p = gets.split.map(&:to_i)\na = Array.new(n+1) do |i|\n next if i == n\n gets.split.map(&:to_i)\nend\na[-1] = a[0]\n\nputs solve(a, p)\n"}, {"source_code": "n, p = gets.chomp.split(\" \").map(&:to_i)\nlr = []\nn.times do \n lr << gets.chomp.split(\" \").map(&:to_i)\nend\nres = 0\nn.times do |i|\n l1, r1 = lr[i]\n l2, r2 = lr[i - 1]\n a1 = r1 / p - (l1 - 1) / p\n a2 = r2 / p - (l2 - 1) / p\n res += 2000 * (1 - (r1 - l1 + 1 - a1) / (r1 - l1 + 1).to_f * (r2 - l2 + 1 - a2) / (r2 - l2 + 1).to_f)\nend\nputs res\n"}], "negative_code": [{"source_code": "n, p = gets.chomp.split(\" \").map(&:to_i)\nsharks = []\n\nn.times do\n l, r = gets.chomp.split(\" \").map(&:to_i)\n\n x, y = l, r\n if x % p != 0\n x += p-x%p\n end\n y -= y%p\n dividends = (y-x)/p+1\n\n sharks << {l: l, r: r, dividends: dividends, total: r-l+1}\nend\n\nsharks << sharks.first\n\nputs sharks.inspect\n\nsum = sharks.each_cons(2).map do |shark_a, shark_b|\n shark_a_not_p = 1 - shark_a[:dividends]/shark_a[:total].to_f\n shark_b_not_p = 1 - shark_b[:dividends]/shark_b[:total].to_f\n\n (1 - (shark_a_not_p * shark_b_not_p))*2000\nend.inject(0, :+)\n\nputs sum\n"}, {"source_code": "n, p = gets.chomp.split(\" \").map(&:to_i)\nsharks = []\n\nn.times do\n l, r = gets.chomp.split(\" \").map(&:to_i)\n dividends = (r-l+1)/p\n sharks << {l: l, r: r, dividends: dividends, total: r-l+1}\nend\n\nsharks << sharks.first\n\nsum = sharks.each_cons(2).map do |shark_a, shark_b|\n shark_a_not_p = 1 - shark_a[:dividends]/shark_a[:total].to_f\n shark_b_not_p = 1 - shark_b[:dividends]/shark_b[:total].to_f\n\n (1 - (shark_a_not_p * shark_b_not_p))*2000\nend.inject(0, :+)\n\nputs sum\n"}], "src_uid": "5aad0a82748d931338140ae81fed301d"} {"nl": {"description": "In the school computer room there are n servers which are responsible for processing several computing tasks. You know the number of scheduled tasks for each server: there are mi tasks assigned to the i-th server.In order to balance the load for each server, you want to reassign some tasks to make the difference between the most loaded server and the least loaded server as small as possible. In other words you want to minimize expression ma - mb, where a is the most loaded server and b is the least loaded one.In one second you can reassign a single task. Thus in one second you can choose any pair of servers and move a single task from one server to another.Write a program to find the minimum number of seconds needed to balance the load of servers.", "input_spec": "The first line contains positive number n (1 ≤ n ≤ 105) — the number of the servers. The second line contains the sequence of non-negative integers m1, m2, ..., mn (0 ≤ mi ≤ 2·104), where mi is the number of tasks assigned to the i-th server.", "output_spec": "Print the minimum number of seconds required to balance the load.", "sample_inputs": ["2\n1 6", "7\n10 11 10 11 10 11 11", "5\n1 2 3 4 5"], "sample_outputs": ["2", "0", "3"], "notes": "NoteIn the first example two seconds are needed. In each second, a single task from server #2 should be moved to server #1. After two seconds there should be 3 tasks on server #1 and 4 tasks on server #2.In the second example the load is already balanced.A possible sequence of task movements for the third example is: move a task from server #4 to server #1 (the sequence m becomes: 2 2 3 3 5); then move task from server #5 to server #1 (the sequence m becomes: 3 2 3 3 4); then move task from server #5 to server #2 (the sequence m becomes: 3 3 3 3 3). The above sequence is one of several possible ways to balance the load of servers in three seconds."}, "positive_code": [{"source_code": "n = gets.chomp.to_i\na = gets.split.map(&:to_i)\nsum = a.reduce(0, :+)\nlower_median = sum / n\nupper_median = lower_median + (sum % n == 0 ? 0 : 1)\nupper_total = 0\nlower_total = 0\na.each do |val|\n lower_total += lower_median - val if val < lower_median\n upper_total += val - upper_median if val > upper_median\nend\nputs lower_total > upper_total ? lower_total : upper_total\n"}, {"source_code": "#!/usr/bin/env ruby\n\ngets\nm = gets.split.map(&:to_i)\navg = m.inject(:+) / m.size\noverage = m.inject(:+) - (avg * m.size)\nbal = 0\nm.sort!\nm.reverse!\nm.each do |l|\n if l > avg\n if overage > 0\n bal += l - (avg + 1)\n overage -= 1\n else\n bal += l - avg\n end\n else\n if overage > 0\n bal += (avg + 1) - l\n overage -= 1\n else\n bal += avg - l\n end\n end\nend\nputs bal / 2\n"}, {"source_code": "# Accept at \nn = gets.to_i\na = gets.split(' ').map { |x|\n x.to_i\n}\nsum = a.inject { |sum, t| sum += t}\nave = sum / a.length\nn1 = sum - ave * a.length\nresult = 0\ntmp = 0\na.each { |t|\n result += t - ave - 1 if t >= ave+1\n tmp += 1 if t >= ave+1\n}\n\nresult += tmp - n1 if tmp > n1\nputs result\n"}, {"source_code": "n = gets.chomp.to_i\nm = gets.chomp.split(\" \").map(&:to_i).sort\nmsum = m.inject(:+)\nmmod = msum % n\nif mmod == 0\n mm = Array.new(n, msum / n)\nelse\n mm = Array.new(n - mmod, msum / n) + Array.new(mmod, msum / n + 1)\nend\nres = 0\nm.zip(mm).each do |a, b|\n res += (a - b).abs\nend\nputs res / 2"}, {"source_code": "n = gets.to_i\nm = gets.split(' ').map{ |x| x.to_i }\nmsum = m.inject{|sum,x| sum+x}\navg = msum / n\nans1 = ans2 = 0\nfor iter in m\n if iter < avg\n ans1 += avg - iter\n end\n if iter > avg \n ans2 += iter - avg - 1\n end\nend\nputs [ans1, ans2].max\n"}, {"source_code": "n = gets.to_i\nservers = gets.split.map(&:to_i).sort.reverse\nsum = servers.reduce(&:+)\nmin_load = sum/servers.length\nextra = sum%servers.length\noptimal = Array.new(n, min_load)\nfor i in 0...extra\n optimal[i] += 1\nend\ncount = 0\nfor i in 0...n\n count += (servers[i]-optimal[i]).abs\nend\nputs count/2\n"}, {"source_code": "\nm = Array.new\nn = gets.chomp.to_i\nt = gets.chomp.split(/ /)\nt.each {|u| m << u.to_i}\nm.sort!\n\ns = 0\nm.each {|t| s = s + t}\n\nhash = Hash.new\nif (s % n == 0)\n\thash[s / n] = n\nelse \n\thash[s/n] = n - s % n\n\thash[s/n+1] = s % n\nend\n\nfor i in 0..m.size do\n\tif hash.has_key?(m[i]) && hash[m[i]] != 0 \n\t\thash[m[i]] = hash[m[i]] - 1\n\t\tif hash[m[i]] == 0\n\t\t\thash.delete m[i]\n\t\tend\n\t\tm[i] = 0\n\tend\nend\n\nsec = 0\nif hash.size == 1\n\thash.each_key {|key| \n\t\tif hash[key] != 0\n\t\t\tm.each {|m_i| \n\t\t\t\tif m_i != 0\n\t\t\t\t\tsec = sec + (m_i - key).abs\n\t\t\t\tend\n\t\t\t}\n\t\tend\n\t}\nelse\n\tm.each {|m_i|\n\t\tif m_i != 0\n\t\t\tif m_i < s / n\n\t\t\t\tif hash[s/n] > 0\n\t\t\t\t\tsec = sec + (s/n - m_i).abs\n\t\t\t\t\thash[s/n] = hash[s/n] - 1\n\t\t\t\telsif hash[s/n + 1] > 0\n\t\t\t\t\tsec = sec + (m_i - s/n - 1).abs\n\t\t\t\t\thash[s/n + 1] = hash[s/n + 1] - 1\n\t\t\t\tend\n\t\t\telsif m_i > s/n + 1\n\t\t\t if hash[s/n + 1] > 0\n\t\t\t\t\tsec = sec + (m_i - s/n - 1).abs\n\t\t\t\t\thash[s/n + 1] = hash[s/n + 1] - 1\n\t\t\t\telsif hash[s/n] > 0\n\t\t\t\t\tsec = sec + (s/n - m_i).abs\n\t\t\t\t\thash[s/n] = hash[s/n] - 1\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\t}\nend\n\nprint sec / 2"}, {"source_code": "\nm = Array.new\nn = gets.chomp.to_i\nt = gets.chomp.split(/ /)\nt.each {|u| m << u.to_i}\nm.sort!\n\ns = 0\nm.each {|t| s = s + t}\n\nhash = Hash.new\nif (s % n == 0)\n\thash[s / n] = n\nelse \n\thash[s/n] = n - s % n\n\thash[s/n+1] = s % n\nend\n\nsec = 0\nif hash.size == 1\n\t\t\tm.each {|m_i| \n\t\t\t\tsec = sec + (m_i - s/n).abs\n\t\t\t}\nelse\n\tm.each {|m_i|\n\t\tif m_i != 0\n\t\t\tif m_i <= s / n\n\t\t\t\tif hash[s/n] > 0\n\t\t\t\t\tsec = sec + (s/n - m_i).abs\n\t\t\t\t\thash[s/n] = hash[s/n] - 1\n\t\t\t\telsif hash[s/n + 1] > 0\n\t\t\t\t\tsec = sec + (m_i - s/n - 1).abs\n\t\t\t\t\thash[s/n + 1] = hash[s/n + 1] - 1\n\t\t\t\tend\n\t\t\telsif m_i >= s/n + 1\n\t\t\t if hash[s/n + 1] > 0\n\t\t\t\t\tsec = sec + (m_i - s/n - 1).abs\n\t\t\t\t\thash[s/n + 1] = hash[s/n + 1] - 1\n\t\t\t\telsif hash[s/n] > 0\n\t\t\t\t\tsec = sec + (s/n - m_i).abs\n\t\t\t\t\thash[s/n] = hash[s/n] - 1\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\t}\nend\n\nprint sec / 2"}, {"source_code": "\nm = Array.new\nn = gets.chomp.to_i\nt = gets.chomp.split(/ /)\n\ns = 0\nt.each {|u| m << u.to_i; s = s + u.to_i}\nm.sort!\n\nhash = Hash.new\nif (s % n == 0)\n\thash[s / n] = n\nelse \n\thash[s/n] = n - s % n\n\thash[s/n+1] = s % n\nend\n\nsec = 0\nif hash.size == 1\n\t\t\tm.each {|m_i| \n\t\t\t\tsec = sec + (m_i - s/n).abs\n\t\t\t}\nelse\n\tm.each {|m_i|\n\t\tif m_i <= s / n\n\t\t\tif hash[s/n] > 0\n\t\t\t\tsec = sec + (s/n - m_i).abs\n\t\t\t\thash[s/n] = hash[s/n] - 1\n\t\t\telse\n\t\t\t\tsec = sec + (m_i - s/n - 1).abs\n\t\t\t\thash[s/n + 1] = hash[s/n + 1] - 1\n\t\t\tend\n\t\telse\n\t\t if hash[s/n + 1] > 0\n\t\t\t\tsec = sec + (m_i - s/n - 1).abs\n\t\t\t\thash[s/n + 1] = hash[s/n + 1] - 1\n\t\t\telse\n\t\t\t\tsec = sec + (s/n - m_i).abs\n\t\t\t\thash[s/n] = hash[s/n] - 1\n\t\t\tend\n\t\tend\n\t}\nend\n\nprint sec / 2"}], "negative_code": [{"source_code": "n = gets.chomp.to_i\na = gets.split.map(&:to_i).sort\n\ncount = 0\nwhile a[n - 1] - a[0] > 1 do\n # balance\n diff = a[n - 1] - a[0]\n a[0] += diff / 2\n a[n - 1] -= diff / 2\n a[n - 1] -= 1 if diff % 2 != 0\n count += diff / 2\n\n # resort\n i = n - 1\n while a[i] < a[i - 1] do\n temp = a[i]\n a[i] = a[i - 1]\n a[i - 1] = temp\n i -= 1\n end\n i = 0\n while a[i] > a[i + 1] do\n temp = a[i]\n a[i] = a[i + 1]\n a[i + 1] = temp\n i += 1\n end\nend\n\nputs count\n"}, {"source_code": "n = gets.chomp.to_i\na = gets.split.map(&:to_i).sort\n\ncount = 0\nwhile a[n - 1] - a[0] > 1 do\n # balance\n a[0] += 1\n a[n - 1] -= 1\n\n # resort\n i = n - 1\n while a[i] < a[i - 1] do\n temp = a[i]\n a[i] = a[i - 1]\n a[i - 1] = temp\n end\n i = 0\n while a[i] > a[i + 1] do\n temp = a[i]\n a[i] = a[i + 1]\n a[i + 1] = temp\n end\n count += 1\nend\n\nputs count\n"}, {"source_code": "#!/usr/bin/env ruby\n\ngets\nm = gets.split.map(&:to_i)\navg = m.inject(:+) / m.size\noverage = m.inject(:+) - (avg * m.size)\nbal = 0\nm.sort!\nm.reverse!\nm.each do |l|\n p overage, bal\n if l > avg\n if overage > 0\n bal += l - (avg + 1)\n overage -= 1\n else\n bal += l - avg\n end\n else\n if overage > 0\n bal += (avg + 1) - l\n overage -= 1\n else\n bal += avg - l\n end\n end\nend\nputs bal\n"}, {"source_code": "#!/usr/bin/env ruby\n\ngets\nm = gets.split.map(&:to_i)\navg = m.inject(:+) / m.size\noverage = m.inject(:+) - (avg * m.size)\nbal = 0\nm.sort!\nm.reverse!\nm.each do |l|\n if l > avg\n if overage > 0\n bal += l - (avg + 1)\n overage -= 1\n else\n bal += l - avg\n end\n else\n if overage > 0\n bal += (avg + 1) - l\n overage -= 1\n else\n bal += l - avg\n end\n end\nend\nputs bal\n"}, {"source_code": "#!/usr/bin/env ruby\n\ngets\nm = gets.split.map(&:to_i)\nm.sort!\nc = 0\nwhile m.last - m.first > 1\n diff = m.last - m.first\n move = diff / 2\n m[0] += move\n m[m.size - 1] -= move\n m.sort!\n c += move\nend\nputs c\n"}, {"source_code": "#!/usr/bin/env ruby\n\ngets\nm = gets.split.map(&:to_i)\navg = m.inject(:+) / m.size\nbal = 0\nm.each do |l|\n if l < avg\n bal += avg - l\n end\nend\nputs bal\n"}, {"source_code": "#!/usr/bin/env ruby\n\ngets\nm = gets.split.map(&:to_i)\navg = m.inject(:+) / m.size\nm.sort!\nc = 0\nwhile m.last - m.first > 1\n move = [avg - m.first, m.last - (avg + 1)].max\n diff = m.last - m.first\n m[0] += move\n m[m.size - 1] -= move\n m.sort!\n c += move\nend\nputs c\n"}, {"source_code": "#!/usr/bin/env ruby\n\ngets\nm = gets.split.map(&:to_i)\navg = m.inject(:+) / m.size\noverage = m.inject(:+) - (avg * m.size)\nbal = 0\nm.sort!\nm.reverse!\nm.each do |l|\n if l > avg\n if overage > 0\n bal += l - (avg + 1)\n overage -= 1\n else\n bal += l - avg\n end\n else\n if overage > 0\n bal += (avg + 1) - l\n overage -= 1\n else\n bal += l - avg\n end\n end\nend\n"}, {"source_code": "n = gets.to_i\na = gets.split(' ').map { |x|\n x.to_i\n}\nsum = a.inject { |sum, t| sum += t}\nave = sum / a.length\nn1 = sum - ave * a.length\nresult = 0\na.each { |t|\n result += t - ave if t >= ave\n}\nputs result - n1\n"}, {"source_code": "n = gets.to_i\na = gets.split(' ').map { |x|\n x.to_i\n}\nm = a.inject { |sum, t| sum += t} / 1.0 / a.length\nresult = 0\na.each { |t|\n result += t - m.ceil if t >= m\n}\nputs result\n"}, {"source_code": "n = gets.to_i\na = gets.split(' ').map { |x|\n x.to_i\n}\nsum = a.inject { |sum, t| sum += t}\nave = sum / a.length\neven= true if (ave * a.length == sum)\n\nceil = ave\nceil = ave+1 unless even\n\n\nresult = 0\na.each { |t|\n result += t - ceil if t >= ceil\n}\nputs result\n"}, {"source_code": "n = gets.chomp.to_i\nloads = gets.chomp.split(\" \").map(&:to_i)\nm = loads.inject(:+) / n\nloads = loads.select{|i| i - m < 0}\nif loads.empty? then\n puts 0\nelse\n puts loads.map{|i| i - m}.inject(:+).abs()\nend "}, {"source_code": "n = gets.to_i\nm = gets.split(' ').map{ |x| x.to_i }\nmsum = m.inject{|sum,x| sum+x}\navg = msum / n\nans1 = 0\nfor iter in m\n if iter < avg\n ans1 += avg - iter\n end\nend\nputs ans1\n"}, {"source_code": "n = gets.to_i\nservers = gets.split.map(&:to_i)\nsum = servers.reduce(&:+)\nmin_load = sum/servers.length\ncount = 0\nfor server in servers\n if server < min_load\n count += min_load-server\n end\nend\n\nputs count\n"}, {"source_code": "n = gets.to_i\nservers = gets.split.map(&:to_i)\nsum = servers.reduce(&:+)\nmin_load = sum/servers.length\nextra = sum-(min_load*servers.length)\ncount = 0\nfor server in servers\n if server > min_load\n count += server-min_load\n end\nend\n\nputs count-extra\n"}, {"source_code": "n = gets.to_i\nservers = gets.split.map(&:to_i).sort.reverse\nsum = servers.reduce(&:+)\nmin_load = sum/servers.length\ncount = 0\nfor server in servers\n count += (server-min_load).abs\nend\n\nputs count/2\n"}, {"source_code": "\nm = Array.new\nn = gets.chomp.to_i\nt = gets.chomp.split(/ /)\nt.each {|u| m << u.to_i}\nm.sort!\n\ns = 0\nm.each {|t| s = s + t}\n\nhash = Hash.new\nif (s % n == 0)\n\thash[s / n] = n\nelse \n\thash[s/n] = n - s % n\n\thash[s/n+1] = s % n\nend\n\nm.each {|x| \n\tif hash.has_key? x \n\t\thash[x] = hash[x] - 1\n\t\tm.delete x\n\t\tif hash[x] == 0\n\t\t\thash.delete x\n\t\tend\n\tend\n}\n\nsec = 0\nif hash.size == 1\n\thash.each_key {|key| \n\t\tm.each {|m_i| \n\t\t\tsec = sec + (m_i - key).abs\n\t\t}\n\t}\n\tprint sec / 2\nelse\n\thash.each_key { |key|\n\t\tm.each {|m_i|\n\t\t\tif m_i < hash[s/n]\n\t\t\t\t\tsec = sec + (s/n - m_i).abs\n\t\t\t\t\thash[s/n] = hash[s/n] - 1\n\t\t\telsif hash[s/n + 1] > 0\n\t\t\t\tif m_i > hash[s/n + 1]\n\t\t\t\t\tsec = sec + (m_i - s/n + 1).abs\n\t\t\t\t\thash[s/n + 1] = hash[s/n + 1] - 1\n\t\t\t\tend\n\t\t\tend\n\t\t}\n\t}\n\tprint sec / 2\nend"}, {"source_code": "\nm = Array.new\nn = gets.chomp.to_i\nt = gets.chomp.split(/ /)\nt.each {|u| m << u.to_i}\nm.sort!\n\ns = 0\nm.each {|t| s = s + t}\n\nhash = Hash.new\nif (s % n == 0)\n\thash[s / n] = n\nelse \n\thash[s/n] = n - s % n\n\thash[s/n+1] = s % n\nend\n\nfor i in 0..m.size do\n\tif hash.has_key?(m[i]) && hash[m[i]] != 0 \n\t\thash[m[i]] = hash[m[i]] - 1\n\t\tif hash[m[i]] == 0\n\t\t\thash.delete m[i]\n\t\tend\n\t\tm[i] = 0\n\tend\nend\n\nsec = 0\nif hash.size == 1\n\thash.each_key {|key| \n\t\tif hash[key] != 0\n\t\t\tm.each {|m_i| \n\t\t\t\tif m_i != 0\n\t\t\t\t\tsec = sec + (m_i - key).abs\n\t\t\t\tend\n\t\t\t}\n\t\tend\n\t}\nelse\n\tm.each {|m_i|\n\t\tif m_i != 0\n\t\t\tif m_i < s/n\n\t\t\t\tsec = sec + (s/n - m_i).abs\n\t\t\t\thash[s/n] = hash[s/n] - 1\n\t\t\telsif hash[s/n + 1] > 0\n\t\t\t\tif m_i > s/n + 1\n\t\t\t\t\tsec = sec + (m_i - s/n - 1).abs\n\t\t\t\t\thash[s/n + 1] = hash[s/n + 1] - 1\n\t\t\t\tend\n\t\t\tend\n\t\tend\n\t}\nend\n\nprint sec / 2"}], "src_uid": "c0c29565e465840103a4af884f951cda"} {"nl": {"description": "Alex doesn't like boredom. That's why whenever he gets bored, he comes up with games. One long winter evening he came up with a game and decided to play it.Given a sequence a consisting of n integers. The player can make several steps. In a single step he can choose an element of the sequence (let's denote it ak) and delete it, at that all elements equal to ak + 1 and ak - 1 also must be deleted from the sequence. That step brings ak points to the player. Alex is a perfectionist, so he decided to get as many points as possible. Help him.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 105) that shows how many numbers are in Alex's sequence. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 105).", "output_spec": "Print a single integer — the maximum number of points that Alex can earn.", "sample_inputs": ["2\n1 2", "3\n1 2 3", "9\n1 2 1 3 2 2 2 2 3"], "sample_outputs": ["2", "4", "10"], "notes": "NoteConsider the third test example. At first step we need to choose any element equal to 2. After that step our sequence looks like this [2, 2, 2, 2]. Then we do 4 steps, on each step we choose any element equals to 2. In total we earn 10 points."}, "positive_code": [{"source_code": "L=10**5\nn=gets.to_i\na=gets.split.map{|e| e.to_i}\nc=(0..L).map{0}\ndp=(0..L).map{0}\na.each{|e|\n\tc[e]=c[e]+e\n}\ndp[1]=c[1]\ndp[2]=c[2]\nans=[c[1],c[2]].max\n3.upto(L){|i|\n\tdp[i]=[dp[i-2],dp[i-3]].max+c[i]\n\tans=[dp[i],ans].max\n}\nputs ans"}, {"source_code": "def main\n gets\n seq = gets.split.map(&:to_i)\n puts max_score(seq)\nend\n\ndef max_score(sequence)\n count = Hash.new(0)\n sequence.each { |x| count[x] += 1 }\n nums = count.keys.sort\n\n # best[k] is the best Alex can do with the first k nums\n best = []\n best[0] = 0\n\n nums.each_index do |i|\n next_best = best.last\n if i > 0 && nums[i - 1] + 1 == nums[i]\n base = best[-2]\n else\n base = best[-1]\n end\n with_cur = base + nums[i] * count[nums[i]]\n next_best = with_cur if with_cur > next_best\n best << next_best\n end\n best.last\nend\n\nmain if __FILE__ == $0\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\ns = Array.new(100002, 0)\nfor i in 0...a.size\n s[a[i]] += a[i]\nend\nfor i in 2..100001\n\ts[i] = [ s[i] + s[i - 2] , s[i - 1] ].max\nend\nputs s[100001]"}, {"source_code": "# def bore(arr)\t\n# \tpoi = []\n\n# \treturn 0 if arr.empty?\n# \tarr.uniq.each do |a|\n\t\t\n# \t\ti = arr.index(a)\t\t\n# \t \t@freq[a] = arr.count(a)\tunless @freq[a]\n# \t\tval = @freq[a]\n# \t\tpoi << (val * a) + bore( arr - [a,a+1,a-1])\t\t\n# \tend\n# \treturn poi.max\t\n# end\n\n\nn = gets.to_i\narr = gets.split(\" \").map(&:to_i)\n\n@freq = Hash.new(0)\n\narr.each{|i| @freq[i] += 1}\n\none_count = @freq[1]\n\nf = []\n\nf[0] = 0\nf[1] = one_count\n\n\n(2..1000001).each do |i|\n\tf[i] = [f[i-1],f[i-2] + (@freq[i] * i)].max\t\nend\nputs f[-1]"}, {"source_code": "def solution(arr)\n dp = Array.new(arr.max+2, 0)\n arr.each { |a| dp[a] += 1 }\n \n for i in 2...dp.size do\n dp[i] = [dp[i-2] + i*dp[i], dp[i-1]].max\n end\n dp.last\nend\n\nn = gets()\narr = gets.split.map(&:to_i)\n\nputs solution(arr)"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nc = Array.new(a.max + 1, 0)\nfor i in 0...a.size\n c[a[i]] += a[i]\nend\n\ndp = Array.new(c.size, 0)\ndp[1] = c[1]\nfor i in 2...c.size\n dp[i] = [dp[i - 2] + c[i], dp[i - 1]].max\nend\nputs dp[c.size - 1]\n"}, {"source_code": "\ndef parse(s)\n nn = s.split.map(&:to_i)\n nn.group_by { |n| n }.reduce(Hash.new(0)) { |acc, (k, v)| acc[k] = v.reduce(:+); acc }\nend\n\ndef next_smallest_keys(ks, i)\n s1, s2 = 0\n j = i-1\n while j >= 0\n next_k = ks[j]\n if next_k < ks[i] - 1\n if s1.zero?\n s1 = next_k\n else\n s2 = next_k\n break\n end\n end\n j -= 1\n end\n [s1, s2]\nend\n\ndef get_max_points(m)\n ks = m.keys.sort\n t = Hash.new(0)\n ans = 0\n\n ks.each_with_index do |k, i|\n\n k1, k2 = next_smallest_keys(ks, i)\n v = m[k]\n t[k] = v + [t[k1], t[k2]].max\n ans = [ans, t[k]].max\n end\n\n ans\nend\n#input = '6 6 8 9 7 9 6 9 5 7 7 4 5 3 9 1 10 3 4 5 8 9 6 5 6 4 10 9 1 4 1 7 1 4 9 10 8 2 9 9 10 5 8 9 5 6 8 7 2 8 7 6 2 6 10 8 6 2 5 5 3 2 8 8 5 3 6 2 1 4 7 2 7 3 7 4 10 10 7 5 4 7 5 10 7 1 1 10 7 7 7 2 3 4 2 8 4 7 4 4'\n\nSTDIN.gets\ninput = STDIN.gets.chomp\n\nm = parse input\np get_max_points m\n"}, {"source_code": "def f(x)\n return 0 if x < 0\n $c[x]\nend\n\ndef n(x)\n return 0 unless $d.key?(x)\n $d[x]\nend\n\ngets\n\na = gets.split(' ').map(&:to_i).sort.reverse\n$d = Hash[a.group_by{|n|n}.map{|k, v|[k, v.size]}]\nm = a.first\n$c = [ 0 ]\n\n1.upto(m) do |i|\n $c.push([f(i-2) + i*n(i), f(i-1)].max)\nend\n\nputs $c.last\n"}, {"source_code": "n = gets.to_i\na = gets.split.map{|i| i.to_i }\n\nb = Array.new(10**5 + 1, 0)\nd = Array.new(10**5 + 2, [0, 0])\n\na.each { |i| b[i]+=1 }\n\n(10**5).downto(1) do |i|\n d[i] = Array.new(2)\n d[i][0] = b[i] * i + d[i + 1][1]\n d[i][1] = d[i + 1].max\nend\n\nputs d[1].max\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\n\ncnt = Array.new(1e5+5,0)\na.map{|x| cnt[x]+=1}\n\ndp = Array.new(1e5+5,0)\n\ni = 1\nhi2 = 0\ndp[1] = cnt[1]\n(dp.length-2).times do\n i += 1\n hi2 = [hi2,dp[i-2]].max\n dp[i] = i * cnt[i] + hi2\nend\n\nputs dp.max\n"}, {"source_code": "# Author : Fuad Ashraful Mehmet \n#Date: 18th April\n\nn=gets.to_i\na=gets.split.map(&:to_i)\nc=Array.new(a.max+1,0)\n\nfor i in 0...a.size\n c[a[i]]+=a[i]\n\nend\n\ndp=Array.new(c.size,0)\ndp[1]=c[1]\n\nfor i in 2...c.size\n dp[i]=[dp[i-2]+c[i],dp[i-1]].max\n\nend\n\nputs dp[c.size-1]"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split(\" \").map(&:to_i)\nh = Hash.new(0)\na.each do |v|\n h[v] += 1\nend\nkeys = h.keys.push(0).sort\n\nr = keys.size.times.map{ 0 }\nrw = keys.size.times.map{ 0 }\n\nkeys.each_with_index do |k, i|\n rw[i] = [r[i-1], rw[i-1]].max\n if keys[i-1] == k - 1\n r[i] = rw[i-1] + k*h[k]\n else\n r[i] = rw[i-1] > r[i-1] ? rw[i-1] : r[i-1]\n r[i] += k*h[k]\n end\nend\n\nputs [r.last, rw.last].max\n"}, {"source_code": "n = gets.strip.to_i\narr = gets.strip.split(\" \").map(&:to_i)\narr.sort!\nj = 1\nuniq_array = []\nuniq_array[0] = arr[0]\nfrequency = Hash.new(0)\nfrequency[arr[0]] = 1\nwhile j < arr.length do \n if arr[j] != arr[j - 1] then\n uniq_array.push(arr[j])\n frequency[arr[j]] = 1\n else\n frequency[arr[j]] += 1\n end\n j += 1\nend\n# p uniq_array\n# p frequency\nresults = []\nresults[0] = uniq_array[0] * frequency[uniq_array[0]]\nif uniq_array.length > 1 then\n if uniq_array[1] - uniq_array[0] > 1 then\n results[1] = results[0] + uniq_array[1] * frequency[uniq_array[1]]\n else\n second_sum = uniq_array[1] * frequency[uniq_array[1]]\n results[1] = second_sum > results[0] ? second_sum : results[0]\n end\nend\ni = 2\nwhile i < uniq_array.length do \n if uniq_array[i] - uniq_array[i - 1] > 1 then\n results[i] = results[i - 1] + uniq_array[i] * frequency[uniq_array[i]]\n elsif results[i - 1] > uniq_array[i] * frequency[uniq_array[i]] + results[i - 2] then\n results[i] = results[i - 1]\n else\n results[i] = uniq_array[i] * frequency[uniq_array[i]] + results[i - 2]\n end \n i += 1\nend\nputs results.last"}, {"source_code": "n = gets.strip.to_i\narr = gets.strip.split(\" \").map(&:to_i)\narr.sort!\nj = 1\nuniq_array = []\nuniq_array[0] = arr[0]\nfrequency = Hash.new(0)\nfrequency[arr[0]] = 1\nwhile j < arr.length do \n if arr[j] != arr[j - 1] then\n uniq_array.push(arr[j])\n frequency[arr[j]] = 1\n else\n frequency[arr[j]] += 1\n end\n j += 1\nend\n# p uniq_array\n# p frequency\n\nprev = uniq_array[0] * frequency[uniq_array[0]]\nif uniq_array.length > 1 then\n if uniq_array[1] - uniq_array[0] > 1 then\n current = prev + uniq_array[1] * frequency[uniq_array[1]]\n else\n second_sum = uniq_array[1] * frequency[uniq_array[1]]\n current = second_sum > prev ? second_sum : prev\n end\nelse\n current = prev\nend\ni = 2\nwhile i < uniq_array.length do \n if uniq_array[i] - uniq_array[i - 1] > 1 then\n prev = current\n current += uniq_array[i] * frequency[uniq_array[i]]\n elsif current < uniq_array[i] * frequency[uniq_array[i]] + prev then\n temp = current\n current = uniq_array[i] * frequency[uniq_array[i]] + prev\n prev = temp\n else\n prev = current\n end \n i += 1\nend\nputs current"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nc = Array.new(a.max + 1, 0)\nfor i in 0...a.size\n c[a[i]] += a[i]\nend\n\ndp = Array.new(c.size, 0)\ndp[1] = c[1]\nfor i in 2...c.size\n dp[i] = [dp[i - 2] + c[i], dp[i - 1]].max\nend\nputs dp[c.size - 1]"}, {"source_code": "def calc (mas)\n pairs = {}\n mas.each_index { |i| mas[i]= mas[i].to_i}\n cur = mas[0]\n count = 0\n mas.each do |x|\n if x==cur\n count += 1\n else\n pairs[cur] = count\n cur = x\n count = 1\n end\n end\n pairs[cur] = count\n pairs\nend\nn = gets.to_i\na = gets.split(\" \")\na.sort_by! {|x| x.to_i}\nb = calc(a)\nbest1 = 0\nprev1 = -1\nbest2 = 0\nb.each_pair {|x,y|\n new_best2 = [best1,best2].max\n if x!=prev1+1\n new_best1 = [best2, best1].max + x*y\n else\n new_best1 = best2 + x*y\n end\n prev1 = x\n best1 = new_best1\n best2 = new_best2\n}\nputs [best1,best2].max"}, {"source_code": "gets\na=gets.split.map(&:to_i).sort\nh={}\na.each{|i|\n\th[i]=(!h[i] ? 1 : h[i] + 1)\n}\na.uniq!\nd=[0]*a.size\nd[0]=a[0]*h[a[0]]\na.each_with_index{|e,i|\n\tnext if i==0\n\td[i]=d[i-1]\n\ts=(i>1) ? d[i-2] : 0\n\ts=[s,d[i-1]].max if a[i] != a[i-1]+1\n\td[i]=[d[i], s+e*h[e]].max\n}\np d[-1]"}], "negative_code": [{"source_code": "gets\na=gets.split.map(&:to_i).sort\nh={}\na.each{|i|\n\th[i]=(!h[i] ? 1 : h[i] + 1)\n}\na.uniq!\nd=[0]*a.size\nd[0]=a[0]*h[a[0]]\na.each_with_index{|e,i|\n\tnext if i==0\n\td[i]=d[i-1]\n\ts=(i>1) ? d[i-2] : 0\n\td[i]=[d[i], s+e*h[e]].max\n}\np d[-1]"}, {"source_code": "gets\na=gets.split.map(&:to_i).sort\nd=[0]*a.size\nd[0]=a[0]\na.each_with_index{|e,i|\n\tnext if i==0\n\td[i]=d[i-1]\n\ts=(i>1) ? d[i-2] : 0\n\td[i]=[d[i], s+e].max\n}\np d[-1]"}, {"source_code": "n=gets.to_i\na=gets.split.map{|e| e.to_i}\nc=(0..10**5).map{0}\ndp=(0..10**5).map{0}\na.each{|e|\n\tc[e]=c[e]+e\n}\na.sort!.uniq!\ndp[1]=c[1]\ndp[2]=c[2]\n3.upto(10**5){|i|\n\tdp[i]=[dp[i-2]+c[i],dp[i-3]+c[i]].max\n}\nputs dp[10**5]\n\n"}, {"source_code": "# def bore(arr)\t\n# \tpoi = []\n\n# \treturn 0 if arr.empty?\n# \tarr.uniq.each do |a|\n\t\t\n# \t\ti = arr.index(a)\t\t\n# \t \t@freq[a] = arr.count(a)\tunless @freq[a]\n# \t\tval = @freq[a]\n# \t\tpoi << (val * a) + bore( arr - [a,a+1,a-1])\t\t\n# \tend\n# \treturn poi.max\t\n# end\n\n\nn = gets.to_i\narr = gets.split(\" \").map(&:to_i)\n\n# @freq = {}\n\n# puts bore(arr)\n@freq = Hash.new(0)\n\narr.uniq.each{|i| @freq[i] += 1}\n\none_count = @freq[1]\n\nf = []\n\nf[0] = 0\nf[1] = one_count\nmax = one_count\n\n(2..10000).each do |i|\n\tf[i] = [f[i-1],f[i-2] + (@freq[i] * i)].max \n\t\nend\nputs f[-1]"}, {"source_code": "# def bore(arr)\t\n# \tpoi = []\n\n# \treturn 0 if arr.empty?\n# \tarr.uniq.each do |a|\n\t\t\n# \t\ti = arr.index(a)\t\t\n# \t \t@freq[a] = arr.count(a)\tunless @freq[a]\n# \t\tval = @freq[a]\n# \t\tpoi << (val * a) + bore( arr - [a,a+1,a-1])\t\t\n# \tend\n# \treturn poi.max\t\n# end\n\n\nn = gets.to_i\narr = gets.split(\" \").map(&:to_i)\n\n@freq = Hash.new(0)\n\narr.each{|i| @freq[i] += 1}\n\none_count = @freq[1]\n\nf = []\n\nf[0] = 0\nf[1] = one_count\n\n\n(2..10000).each do |i|\n\tf[i] = [f[i-1],f[i-2] + (@freq[i] * i)].max\t\nend\nputs f[-1]"}, {"source_code": "# def bore(arr)\t\n# \tpoi = []\n\n# \treturn 0 if arr.empty?\n# \tarr.uniq.each do |a|\n\t\t\n# \t\ti = arr.index(a)\t\t\n# \t \t@freq[a] = arr.count(a)\tunless @freq[a]\n# \t\tval = @freq[a]\n# \t\tpoi << (val * a) + bore( arr - [a,a+1,a-1])\t\t\n# \tend\n# \treturn poi.max\t\n# end\n\n\nn = gets.to_i\narr = gets.split(\" \").map(&:to_i)\n\n# @freq = {}\n\n# puts bore(arr)\n@freq = Hash.new(0)\n\narr.uniq.each{|i| @freq[i] += i}\n\none_count = @freq[1]\n\nf = []\n\nf[0] = 0\nf[1] = one_count\nmax = one_count\n\n(2..10000).each do |i|\n\tf[i] = [f[i-1],f[i-2] + (@freq[i] * i)].max \n\t\nend\nputs f[-1]"}, {"source_code": "# def bore(arr)\t\n# \tpoi = []\n\n# \treturn 0 if arr.empty?\n# \tarr.uniq.each do |a|\n\t\t\n# \t\ti = arr.index(a)\t\t\n# \t \t@freq[a] = arr.count(a)\tunless @freq[a]\n# \t\tval = @freq[a]\n# \t\tpoi << (val * a) + bore( arr - [a,a+1,a-1])\t\t\n# \tend\n# \treturn poi.max\t\n# end\n\n\nn = gets.to_i\narr = gets.split(\" \").map(&:to_i)\n\n# @freq = {}\n\n# puts bore(arr)\n@freq = Hash.new(0)\n\narr.each{|i| @freq[i] += 1}\n\none_count = @freq[1]\n\nf = []\n\nf[0] = 0\nf[1] = one_count\nmax = one_count\n\n(2..10000).each do |i|\n\tf[i] = [f[i-1],f[i-2] + (@freq[i] * i)].max \n\t\nend\nputs f[-1]"}, {"source_code": "# def bore(arr)\t\n# \tpoi = []\n\n# \treturn 0 if arr.empty?\n# \tarr.uniq.each do |a|\n\t\t\n# \t\ti = arr.index(a)\t\t\n# \t \t@freq[a] = arr.count(a)\tunless @freq[a]\n# \t\tval = @freq[a]\n# \t\tpoi << (val * a) + bore( arr - [a,a+1,a-1])\t\t\n# \tend\n# \treturn poi.max\t\n# end\n\n\nn = gets.to_i\narr = gets.split(\" \").map(&:to_i)\n\n# @freq = {}\n# puts bore(arr)\n@freq = {}\n\n# (1..10000).each{|i| @freq[i] = arr.count(i)}\none_count = arr.count(1)\nf = Hash.new(0)\n\nf[0] = 0\nf[1] = one_count\nmax = one_count\n\narr.sort.uniq.each do |i|\n\tf[i] = [f[i-1],f[i-2] + (arr.count(i) * i)].max \n\tmax = f[i] if f[i] > max\nend\n# puts f.max\nputs max"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nb = a.sort.uniq\nc = Array.new(b.max + 1, 0)\nfor i in 0...b.size\n c[b[i]] = b[i] * a.count(b[i])\nend\n\ns = Array.new(b.max + 1, 0)\ns[0] = 1\nfor i in 1...c.size - 1\n if c[i] >= c[i - 1] + c[i + 1]\n s[i - 1] = 0\n s[i + 1] = 0\n s[i] = 1\n else\n s[i - 1] = 1 if s[i - 2] == 0\n s[i + 1] = 1\n s[i] = 0\n end\nend\nans = 0\nfor i in 0...c.size\n ans += c[i] * s[i]\nend\np ans\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nb = a.sort.uniq\nc = Array.new(b.max + 1, 0)\nfor i in 0...b.size\n c[b[i]] = b[i] * a.count(b[i])\nend\n\ns = Array.new(b.max + 1, 0)\nans = c[1]\nfor i in 2...c.size\n if ans + c[i] - c[i - 1] > ans\n s[i] = 1\n ans += c[i] - c[i - 1]\n if s[i - 2] == 0 && s[i - 3] == 0 && c[i - 2] > 0\n ans += c[i - 2]\n s[i - 2] = 1\n end\n end\nend\np ans\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nb = a.sort.uniq\nc = Array.new(b.max + 1, 0)\nfor i in 0...b.size\n c[b[i]] = b[i] * a.count(b[i])\nend\n\ns = Array.new(b.max + 1, 0)\ns[0] = 0\nfor i in 1...c.size - 1\n if c[i] >= c[i - 1] + c[i + 1]\n s[i - 1] = 0\n s[i + 1] = 0\n s[i] = 1\n else\n s[i - 1] = 1 if s[i - 2] == 0\n s[i + 1] = 1\n s[i] = 0\n end\nend\ns[0] = 0\nfor i in 1...c.size\n s[i] = 1 if s[i - 1] == 0 && s[i + 1] == 0\nend\nans = 0\nfor i in 0...c.size\n ans += c[i] * s[i]\nend\np ans\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nb = a.sort.uniq\nc = Array.new(b.max + 1, 0)\nfor i in 0...b.size\n c[b[i]] = b[i] * a.count(b[i])\nend\n\ns = Array.new(b.max + 1, 0)\ns[0] = 1\nfor i in 1...c.size - 1\n if c[i] >= c[i - 1] + c[i + 1]\n s[i - 1] = 0\n s[i + 1] = 0\n s[i] = 1\n else\n s[i - 1] = 1\n s[i + 1] = 1\n s[i] = 0\n end\nend\nans = 0\nfor i in 0...c.size\n ans += c[i] * s[i]\nend\np ans\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nb = a.sort.uniq\nc = Array.new(b.max + 1, 0)\nfor i in 0...b.size\n c[b[i]] = b[i] * a.count(b[i])\nend\n\ns = Array.new(b.max + 1, 0)\ns[0] = 1\nfor i in 1...c.size - 1\n if c[i] >= c[i - 1] + c[i + 1]\n s[i - 1] = 0\n s[i + 1] = 0\n s[i] = 1\n else\n s[i - 1] = 1 if s[i - 2] == 0\n s[i + 1] = 1\n s[i] = 0\n end\n p s\nend\np s\np c\nans = 0\nfor i in 0...c.size\n ans += c[i] * s[i]\nend\np ans\n"}, {"source_code": "# Hypothesis\n# choose the most popular number\n# if popularity is equal,\n# take the largest number\ndef step(ak, sequence)\n sequence.delete_at(sequence.index(ak))\n sequence.select do |a|\n a != ak + 1 && a != ak - 1\n end\nend\n\ndef get_next_pick(sequence)\n freq = sequence.inject(Hash.new(0)) { |h, v| h[v] += 1; h }\n if freq.values.uniq.length == 1\n sequence.max\n else\n sequence.max_by { |v| freq[v] }\n end\nend\n\ndef simulate(input)\n sequence = input.split.map(&:to_i)\n score = 0\n until sequence.length == 0\n next_pick = get_next_pick(sequence)\n score += next_pick\n sequence = step(next_pick, sequence)\n end\n score\nend\n\n#input = '1 2'\n#score = simulate(input)\n#p \"#{input}: #{score}\"\n#\n#input = '1 2 3'\n#score = simulate(input)\n#p \"#{input}: #{score}\"\n#\n#input = '1 2 1 3 2 2 2 2 3'\n#score = simulate(input)\n#p \"#{input}: #{score}\"\n\ninput = gets.chomp\np input\ninput = gets.chomp\nsimulate(input)\n\n"}, {"source_code": "# Hypothesis\n# choose the most popular number\n# if popularity is equal,\n# take the largest number\ndef step(ak, sequence)\n sequence.delete_at(sequence.index(ak))\n sequence.select do |a|\n a != ak + 1 && a != ak - 1\n end\nend\n\ndef get_next_pick(sequence)\n freq = sequence.inject(Hash.new(0)) { |h, v| h[v] += 1; h }\n if freq.values.uniq.length == 1\n sequence.max\n else\n sequence.max_by { |v| freq[v] }\n end\nend\n\ndef simulate(input)\n sequence = input.split.map(&:to_i)\n score = 0\n until sequence.length == 0\n next_pick = get_next_pick(sequence)\n score += next_pick\n sequence = step(next_pick, sequence)\n end\n score\nend\n\ninput = gets.chomp\nsimulate(input)\n"}, {"source_code": "# Hypothesis\n# choose the most popular number\n# if popularity is equal,\n# take the largest number\ndef step(ak, sequence)\n sequence.delete_at(sequence.index(ak))\n sequence.select do |a|\n a != ak + 1 && a != ak - 1\n end\nend\n\ndef get_next_pick(sequence)\n freq = sequence.inject(Hash.new(0)) { |h, v| h[v] += 1; h }\n if freq.values.uniq.length == 1\n sequence.max\n else\n sequence.max_by { |v| freq[v] }\n end\nend\n\ndef simulate(a)\n sequence = a\n score = 0\n until sequence.length == 0\n next_pick = get_next_pick(sequence)\n score += next_pick\n sequence = step(next_pick, sequence)\n end\n score\nend\n\nwhile STDIN.gets\n a = $_.split.map(&:to_i)\n if a.length > 1\n puts simulate(a)\n end\nend\n"}, {"source_code": "gets\na = gets.split(' ').map(&:to_i).sort.reverse\nb = a.uniq.map { |x| [x, a.count{ |y| y == x } ] }.sort do |x, y|\n y.last != x.last ? y.last <=> x.last : y.first <=> x.first\nend\nc = Hash[ b ]\n\nputs(\n b.map do |elm|\n res = [elm.last, c[elm.first]].min\n c[elm.first-1] = [0, c[elm.first-1]-res].max if c.key?(elm.first-1)\n c[elm.first+1] = [0, c[elm.first+1]-res].max if c.key?(elm.first+1)\n c[elm.first] = 0\n\n res*elm.first\n end.inject(&:+)\n)\n"}, {"source_code": "n = gets.to_i\na = gets.split.map{|i| i.to_i }\n\nb = Array.new(10**5 + 1, 0)\nd = Array.new(10**5 + 2, [0, 0])\n\na.each { |i| b[i]+=1 }\n\n(100).downto(1) do |i|\n d[i] = Array.new(2)\n d[i][0] = b[i] * i + d[i + 1][1]\n d[i][1] = d[i + 1].max\nend\n\nputs d[1].max\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i).sort.reverse\n\n#p a\n\nprv = 1e6\nscore = 0\nn.times do\n x = a.shift\n if x < prv - 1\n score += x\n prv = x\n end\nend\n\nputs score\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\n\ncnt = Array.new(1e5+5,0)\na.map{|x| cnt[x]+=1}\n\ndp = Array.new(1e5+5,0)\n\ni = 1\nhi2 = 0\n(dp.length-2).times do\n i += 1\n hi2 = [hi2,dp[i-2]].max\n dp[i] = i * cnt[i] + hi2\nend\n\nputs dp.max\n"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split(\" \").map(&:to_i)\nh = Hash.new(0)\na.each do |v|\n h[v] += 1\nend\nkeys = h.keys.push(0).sort\n\nr = keys.size.times.map{ 0 }\nrw = keys.size.times.map{ 0 }\n\nkeys.each_with_index do |k, i|\n rw[i] = r[i-1]\n if keys[i-1] == k - 1\n r[i] = rw[i-1] + k*h[k]\n else\n r[i] = rw[i-1] > r[i-1] ? rw[i-1] : r[i-1]\n r[i] += + k*h[k]\n end\nend\n\nputs [r[keys.last], rw[keys.last]].max\n"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split(\" \").map(&:to_i)\nh = Hash.new(0)\na.each do |v|\n h[v] += 1\nend\nr = n.times.map{ 0 }\n\nkeys = h.keys.push(0).sort\nkeys.each_with_index do |k, i|\n r[i] = r[i-1]\n prev_key = keys[i-1]\n prev_score = h[prev_key]*prev_key\n score = h[k]*k\n if prev_key == k-1\n if score > prev_score\n r[i] += score - prev_score\n if keys[i-1] == keys[i-2] + 1\n r[i]+= r[i-2]\n end\n end\n else\n r[i] += h[k]*k\n end\nend\n\nputs r[keys.last]\n"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split(\" \").map(&:to_i)\nh = Hash.new(0)\na.each do |v|\n h[v] += 1\nend\nkeys = h.keys.push(0).sort\n\nr = keys.size.times.map{ 0 }\nrw = keys.size.times.map{ 0 }\n\nkeys.each_with_index do |k, i|\n rw[i] = r[i-1]\n if keys[i-1] == k - 1\n r[i] = rw[i-1] + k*h[k]\n else\n r[i] = rw[i-1] > r[i-1] ? rw[i-1] : r[i-1]\n r[i] += + k*h[k]\n end\nend\n\nputs [r.last, rw.last].max\n"}, {"source_code": "n = gets.strip.to_i\narr = gets.strip.split(\" \").map(&:to_i)\narr.sort!\nfrequency = arr.inject({}){|freq, elem| freq.merge(elem => arr.count(elem))}\narr.uniq!\n# p arr\n# p frequency\nresults = []\nresults[0] = arr[0] * frequency[arr[0]]\nif arr.length > 1 then\n second_sum = arr[1] * frequency[arr[1]]\n results[1] = second_sum > results[0] ? second_sum : results[0]\nend\ni = 2\nwhile i < arr.length do \n if arr[i] - arr[i - 1] > 1 then\n results[i] = results[i - 1] + arr[i] * frequency[arr[i]]\n elsif results[i - 1] > arr[i] * frequency[arr[i]] + results[i - 2] then\n results[i] = results[i - 1]\n else\n results[i] = arr[i] * frequency[arr[i]] + results[i - 2]\n end \n i += 1\nend\nputs results.last"}, {"source_code": "n = gets.strip.to_i\narr = gets.strip.split(\" \").map(&:to_i)\narr.sort!\nfrequency = arr.inject({}){|freq, elem| freq.merge(elem => arr.count(elem))}\narr.uniq!\n# p arr\n# p frequency\nresults = []\nresults[0] = arr[0] * frequency[arr[0]]\nresults[1] = arr[1] * frequency[arr[1]] if arr.length > 1\ni = 2\nwhile i < arr.length do \n if arr[i] - arr[i - 1] > 1 then\n results[i] = results[i - 1] + arr[i] * frequency[arr[i]]\n elsif results[i - 1] > arr[i] * frequency[arr[i]] + results[i - 2] then\n results[i] = results[i - 1]\n else\n results[i] = arr[i] * frequency[arr[i]] + results[i - 2]\n end \n i += 1\nend\nputs results.last"}, {"source_code": "n = gets.strip.to_i\narr = gets.strip.split(\" \").map(&:to_i)\narr.sort!\nj = 1\nuniq_array = []\nuniq_array[0] = arr[0]\nfrequency = Hash.new(0)\nfrequency[arr[0]] = 1\nwhile j < arr.length do \n if arr[j] != arr[j - 1] then\n uniq_array.push(arr[j])\n frequency[arr[j]] = 1\n else\n frequency[arr[j]] += 1\n end\n j += 1\nend\n# p uniq_array\n# p frequency\n\nprev = uniq_array[0] * frequency[uniq_array[0]]\nif uniq_array.length > 1 then\n if uniq_array[1] - uniq_array[0] > 1 then\n current = prev + uniq_array[1] * frequency[uniq_array[1]]\n else\n second_sum = uniq_array[1] * frequency[uniq_array[1]]\n current = second_sum > prev ? second_sum : prev\n end\nend\ni = 2\nwhile i < uniq_array.length do \n if uniq_array[i] - uniq_array[i - 1] > 1 then\n current = current + uniq_array[i] * frequency[uniq_array[i]]\n elsif current <= uniq_array[i] * frequency[uniq_array[i]] + prev then\n current = uniq_array[i] * frequency[uniq_array[i]] + prev\n end \n i += 1\nend\nputs current"}, {"source_code": "n = gets.strip.to_i\narr = gets.strip.split(\" \").map(&:to_i)\narr.sort!\nj = 1\nuniq_array = []\nuniq_array[0] = arr[0]\nfrequency = Hash.new(0)\nfrequency[arr[0]] = 1\nwhile j < arr.length do \n if arr[j] != arr[j - 1] then\n uniq_array.push(arr[j])\n frequency[arr[j]] = 1\n else\n frequency[arr[j]] += 1\n end\n j += 1\nend\n# p uniq_array\n# p frequency\n\nprev = uniq_array[0] * frequency[uniq_array[0]]\nif uniq_array.length > 1 then\n if uniq_array[1] - uniq_array[0] > 1 then\n current = prev + uniq_array[1] * frequency[uniq_array[1]]\n else\n second_sum = uniq_array[1] * frequency[uniq_array[1]]\n current = second_sum > prev ? second_sum : prev\n end\nend\ni = 2\nwhile i < uniq_array.length do \n if uniq_array[i] - uniq_array[i - 1] > 1 then\n prev = current\n current += uniq_array[i] * frequency[uniq_array[i]]\n elsif current < uniq_array[i] * frequency[uniq_array[i]] + prev then\n temp = current\n current = uniq_array[i] * frequency[uniq_array[i]] + prev\n prev = temp\n else\n prev = current\n end \n i += 1\nend\nputs current"}, {"source_code": "n = gets.to_i\narr = gets.split.map { |x| x.to_i }.sort\nhash = Hash.new(0)\narr.each { |x| hash[x] += 1 }\nhash = Hash[hash.sort_by { |key,value| value }]\nsum = 0\nwhile hash.size != 0\n temp = hash[hash.keys.last]\n sum += temp * hash.keys.last\n temp_key = hash.keys.last\n hash.delete(temp_key)\n hash.delete(temp_key - 1)\n hash.delete(temp_key + 1)\nend\nputs sum\n"}], "src_uid": "41b3e726b8146dc733244ee8415383c0"} {"nl": {"description": "You are given an array $$$a$$$ consisting of $$$n$$$ positive integers. You have to choose a positive integer $$$d$$$ and paint all elements into two colors. All elements which are divisible by $$$d$$$ will be painted red, and all other elements will be painted blue.The coloring is called beautiful if there are no pairs of adjacent elements with the same color in the array. Your task is to find any value of $$$d$$$ which yields a beautiful coloring, or report that it is impossible.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of testcases. The first line of each testcase contains one integer $$$n$$$ ($$$2 \\le n \\le 100$$$) — the number of elements of the array. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^{18}$$$).", "output_spec": "For each testcase print a single integer. If there is no such value of $$$d$$$ that yields a beautiful coloring, print $$$0$$$. Otherwise, print any suitable value of $$$d$$$ ($$$1 \\le d \\le 10^{18}$$$).", "sample_inputs": ["5\n5\n1 2 3 4 5\n3\n10 5 15\n3\n100 10 200\n10\n9 8 2 6 6 2 8 6 5 4\n2\n1 3"], "sample_outputs": ["2\n0\n100\n0\n3"], "notes": null}, "positive_code": [{"source_code": "t = gets.to_i\r\nans = []\r\nt.times do\r\n n = gets.to_i\r\n x = gets.chomp.split(\" \").map(&:to_i)\r\n a = []\r\n b = []\r\n n.times do |i|\r\n if i%2==0\r\n a << x[i]\r\n else\r\n b << x[i]\r\n end\r\n end\r\n a_gcd = a.inject(:gcd)\r\n b_gcd = b.inject(:gcd)\r\n if b.all?{|v|v%a_gcd != 0}\r\n ans << a_gcd\r\n elsif a.all?{|v|v%b_gcd != 0}\r\n ans << b_gcd\r\n else\r\n ans << 0\r\n end\r\nend\r\nputs ans"}], "negative_code": [], "src_uid": "e6c91f6872c4dd845cb7a156aacab7c7"} {"nl": {"description": "Polycarp is playing a new computer game. This game has $$$n$$$ stones in a row. The stone on the position $$$i$$$ has integer power $$$a_i$$$. The powers of all stones are distinct.Each turn Polycarp can destroy either stone on the first position or stone on the last position (in other words, either the leftmost or the rightmost stone). When Polycarp destroys the stone it does not exist any more.Now, Polycarp wants two achievements. He gets them if he destroys the stone with the least power and the stone with the greatest power. Help Polycarp find out what is the minimum number of moves he should make in order to achieve his goal.For example, if $$$n = 5$$$ and $$$a = [1, 5, 4, 3, 2]$$$, then Polycarp could make the following moves: Destroy the leftmost stone. After this move $$$a = [5, 4, 3, 2]$$$; Destroy the rightmost stone. After this move $$$a = [5, 4, 3]$$$; Destroy the leftmost stone. After this move $$$a = [4, 3]$$$. Polycarp destroyed the stones with the greatest and least power, so he can end the game. Please note that in the example above, you can complete the game in two steps. For example: Destroy the leftmost stone. After this move $$$a = [5, 4, 3, 2]$$$; Destroy the leftmost stone. After this move $$$a = [4, 3, 2]$$$. Polycarp destroyed the stones with the greatest and least power, so he can end the game. ", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 100$$$). Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \\le n \\le 100$$$) — the number of stones. The second line contains $$$n$$$ distinct integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le n$$$) — the power of the stones.", "output_spec": "For each test case, output the minimum number of moves required to destroy the stones with the greatest and the lowest power.", "sample_inputs": ["5\n5\n1 5 4 3 2\n8\n2 1 3 4 5 6 8 7\n8\n4 2 3 1 8 6 7 5\n4\n3 4 2 1\n4\n2 3 1 4"], "sample_outputs": ["2\n4\n5\n3\n2"], "notes": null}, "positive_code": [{"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n a = gets.split.map &:to_i\r\n mn, mx = a.index(a.min), a.index(a.max)\r\n puts [[mn, mx].max + 1, n - [mn, mx].min, n - mn + mx + 1, n - mx + mn + 1].min\r\nend"}, {"source_code": "t = gets.to_i\r\nfor i in 0...t\r\n\tn = gets.to_i\r\n\ta = gets.split.map(&:to_i)\r\n\tx, y = [a.index(a.min), a.index(a.max)].sort\r\n\tputs [y + 1, n - x, x + 1 + n - y].min\r\nend"}, {"source_code": "gets.to_i.times do\r\n n=gets.to_i\r\n a=gets.split.map(&:to_i)\r\n s,t=[a.index(a.min),a.index(a.max)].sort\r\n puts [t+1,n-s,s+1+n-t].min\r\nend\r\n"}], "negative_code": [{"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n a = gets.split.map &:to_i\r\n mn, mx = a.index(a.min), a.index(a.max)\r\n puts [[mn, mx].max + 1, n - [mn, mx].min, n - mn + mx + 1, n - mx + mn - 1].min\r\nend"}], "src_uid": "d5fc2bc618dd9d453a5e7ae30ccb73f3"} {"nl": {"description": "Is it rated?Here it is. The Ultimate Question of Competitive Programming, Codeforces, and Everything. And you are here to answer it.Another Codeforces round has been conducted. No two participants have the same number of points. For each participant, from the top to the bottom of the standings, their rating before and after the round is known.It's known that if at least one participant's rating has changed, then the round was rated for sure.It's also known that if the round was rated and a participant with lower rating took a better place in the standings than a participant with higher rating, then at least one round participant's rating has changed.In this problem, you should not make any other assumptions about the rating system.Determine if the current round is rated, unrated, or it's impossible to determine whether it is rated of not.", "input_spec": "The first line contains a single integer n (2 ≤ n ≤ 1000) — the number of round participants. Each of the next n lines contains two integers ai and bi (1 ≤ ai, bi ≤ 4126) — the rating of the i-th participant before and after the round, respectively. The participants are listed in order from the top to the bottom of the standings.", "output_spec": "If the round is rated for sure, print \"rated\". If the round is unrated for sure, print \"unrated\". If it's impossible to determine whether the round is rated or not, print \"maybe\".", "sample_inputs": ["6\n3060 3060\n2194 2194\n2876 2903\n2624 2624\n3007 2991\n2884 2884", "4\n1500 1500\n1300 1300\n1200 1200\n1400 1400", "5\n3123 3123\n2777 2777\n2246 2246\n2246 2246\n1699 1699"], "sample_outputs": ["rated", "unrated", "maybe"], "notes": "NoteIn the first example, the ratings of the participants in the third and fifth places have changed, therefore, the round was rated.In the second example, no one's rating has changed, but the participant in the second place has lower rating than the participant in the fourth place. Therefore, if the round was rated, someone's rating would've changed for sure.In the third example, no one's rating has changed, and the participants took places in non-increasing order of their rating. Therefore, it's impossible to determine whether the round is rated or not."}, "positive_code": [{"source_code": "n=gets.to_i\na=[]\nb=[]\nrated=false\nn.times do\n x, y=gets.split.map &:to_i\n a<> 1) & 1)\nend\ndef rotate(r,c,g)\n c.times.map { |j| r.times.map { |i| g[i][j] && rot(g[i][j]) } }\nend\n\nr,c=gets.split.map(&:to_i)\ng = []\n# r.times {\n# g << gets.chomp.chars.map{ |c|\n# c == '.' ? nil : TILES[c.to_i-1]\n# }\n# }\ns = STDIN.read\nr.times {|i|\n k = []\n (i*(c+1)).upto(i*(c+1)+c-1) {|j|\n k << (s[j] == '.' ? nil : TILES[s[j].to_i-1])\n }\n g << k\n}\n\ntd = top_down r, c, g\nrl = top_down c, r, rotate(r,c,g)\n\ndef power2(n)\n return 2**n if n <= 1\n v = (power2(n/2)**2) % MOD\n v = (v*2) % MOD if n.odd?\n v\nend\n\nif rl.nil? || td.nil?\n puts '0'\nelse\n puts power2 (rl+td).count(&:nil?)\nend\n"}], "negative_code": [{"source_code": "MOD = 1000003\nUP = 0b1000\nDOWN = 0b0001\nLEFT = 0b0100\nRIGHT = 0b0010\nTILES = [\n 0b1100,\n 0b0101,\n 0b0011,\n 0b1010,\n]\n%w(UP DOWN LEFT RIGHT).each {|dir|\n eval < 0\n puts \"NO\"\nelse\n puts \"YES\"\nend"}, {"source_code": "r=0\ngets;gets.split.map(&:to_i).each{|e|\nbreak if r>e\nr^=e%2\n}\nputs [:YES,:NO][r]"}, {"source_code": "n = gets.to_i\nas = gets.split.map(&:to_i)\nas.each_with_index do |a, i|\n if a < 0 || a.odd? && i == n - 1\n puts 'NO'\n exit\n end\n if a.odd?\n as[i + 1] -= 1\n end\nend\nputs 'YES'\n"}, {"source_code": "n = gets.to_i\na = gets.split(\" \").map(&:to_i)\nans = 0\na.each do |e|\n break if (e 0? \"NO\" : \"YES\"\n"}, {"source_code": "n=gets.to_i-2\na=gets.split.map &:to_i\n\nres=\"YES\"\nfor i in 0..n\n if a[i]<0\n res=\"NO\"\n break\n end\n\n if a[i].odd?\n a[i]-=1\n a[i+1]-=1\n end\nend\nres=\"NO\" if a[-1].odd?\n\nputs res\n"}, {"source_code": "n=gets.to_i-1\na=gets.split.map &:to_i\n\nn.times { |i|\n if a[i].odd?\n a[i] -= 1\n a[i+1] -= 1\n end\n (puts 'NO'; exit) if a[i] < 0\n}\nputs (a[n].even?)?'YES':'NO'\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nk = 0\na.each do |x| \n break if x-k<0\n k = (x-k)%2\nend\nputs k>0 ? \"NO\" : \"YES\""}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\n\n\ndef meow(n, a)\n n.times do |i|\n return false if a[i] < 0\n if a[i].odd?\n return false if i == n - 1\n a[i + 1] -= 1\n end\n end\nend\n\nputs meow(n, a) ? 'YES' : 'NO'\n"}, {"source_code": "n = gets.to_i\na = gets.strip.split(' ').map(&:to_i)\nflag = false\n0.upto(n - 1) do |i|\n if flag\n if a[i] == 0\n break\n else\n a[i] -= 1\n flag = !flag\n end\n end\n \n if a[i] % 2 == 1\n flag = !flag\n else\n if flag == true\n break\n end\n end\nend\n\nprintf(\"%s\\n\", flag ? \"NO\" : \"YES\")\n"}, {"source_code": "require 'set'\n\nparse_int = lambda {gets.split.map{|x| x.to_i}}\n\n\ngets \npz = parse_int.call\n\n(pz.length-1).times do |i|\n if pz[i].odd?\n if pz[i+1]>0\n pz[i] -= 1\n pz[i+1] -= 1\n else\n puts 'NO'\n exit\n end\n end\nend\n\n\n \nputs pz[-1].even? ? 'YES' : \"NO\"\n"}], "negative_code": [{"source_code": "p = 0\nn = gets.to_i\n\na = gets.split.map &:to_i\ns = false\na.each do |i|\n if i == p + 1\n p = 1\n elsif i == p+2 or i == p\n p = 0\n else\n s = true\n break\n end\nend\n\nif s or p > 0\n puts \"NO\"\nelse\n puts \"YES\"\nend"}, {"source_code": "p = 0\nn = gets.to_i\n\na = gets.split.map &:to_i\ns = false\na.each do |i|\n if i == p + 1\n p = 1\n elsif i == p+2\n p = 0\n elsif i != p\n puts \"NO\"\n s = true\n break\n end\nend\n\nputs \"YES\" unless s"}, {"source_code": "r=0\ngets.split.map(&:to_i).each{|e|\nbreak if r>e\nr^=e%2\n}\nputs [:YES,:NO][r]"}, {"source_code": "r=0\ngets.split.map(&:to_i).each{|e|\nbreak if e>r\nr^=e%2\n}\nputs [:YES,:NO][r]"}, {"source_code": "n = gets.to_i\na = gets.split(\" \").map(&:to_i)\nans = 0\na.each do |e|\n break if (ans < e)\n ans = (e - ans)%2\nend\nprint ans>0? \"NO\" : \"YES\"\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nk = 0\na.each { |x| k = (x-k)%2 }\nputs k>0 ? \"NO\" : \"YES\""}], "src_uid": "97697eba87bd21ae5c979a5ea7a81cb7"} {"nl": {"description": "This problem differs from one which was on the online contest.The sequence a1, a2, ..., an is called increasing, if ai < ai + 1 for i < n.The sequence s1, s2, ..., sk is called the subsequence of the sequence a1, a2, ..., an, if there exist such a set of indexes 1 ≤ i1 < i2 < ... < ik ≤ n that aij = sj. In other words, the sequence s can be derived from the sequence a by crossing out some elements.You are given two sequences of integer numbers. You are to find their longest common increasing subsequence, i.e. an increasing sequence of maximum length that is the subsequence of both sequences.", "input_spec": "The first line contains an integer n (1 ≤ n ≤ 500) — the length of the first sequence. The second line contains n space-separated integers from the range [0, 109] — elements of the first sequence. The third line contains an integer m (1 ≤ m ≤ 500) — the length of the second sequence. The fourth line contains m space-separated integers from the range [0, 109] — elements of the second sequence.", "output_spec": "In the first line output k — the length of the longest common increasing subsequence. In the second line output the subsequence itself. Separate the elements with a space. If there are several solutions, output any.", "sample_inputs": ["7\n2 3 1 6 5 4 6\n4\n1 3 5 6", "5\n1 2 0 2 1\n3\n1 0 1"], "sample_outputs": ["3\n3 5 6", "2\n0 1"], "notes": null}, "positive_code": [{"source_code": "n=gets.to_i\na=gets.split.map &:to_i\nm=gets.to_i\nb=gets.split.map &:to_i\nd=[0]*m\nq=[0]*n\np=[-1]*n\nn.times{|i|\n\tmx=0\n\tm.times{|j|\n\t\tmx=[mx,d[j]].max if a[i]>b[j]\n\t\tq[i]=d[j]=mx+1 if a[i]==b[j]\n\t}\n\ti.times{|j|p[i]=j if a[j]=0 and q[i]>0\np q.max\nputs l.reverse.join \" \"\n\n"}, {"source_code": "n=gets.to_i\na=gets.split.map &:to_i\nm=gets.to_i\nb=gets.split.map &:to_i\nd=[0]*m\nq=[0]*n\np=[-1]*n\nn.times{|i|\n\tmx=0\n\tm.times{|j|\n\t\tmx=[mx,d[j]].max if a[i]>b[j]\n\t\tq[i]=d[j]=mx+1 if a[i]==b[j]\n\t}\n\ti.times{|j|p[i]=j if a[j]=0 and q[i]>0\np q.max\nputs l.reverse.join \" \"\n\n"}, {"source_code": "n=gets.to_i\na=gets.split.map &:to_i\nm=gets.to_i\nb=gets.split.map &:to_i\nd=[0]*m\nq=[0]*n\np=[-1]*n\nn.times{|i|\n\tmx=0\n\tm.times{|j|\n\t\tmx=[mx,d[j]].max if a[i]>b[j]\n\t\tq[i]=d[j]=mx+1 if a[i]==b[j]\n\t}\n\ti.times{|j|p[i]=j if a[j]=0 and q[i]>0\np q.max\nputs l.reverse.join \" \"\n\n"}, {"source_code": "n=gets.to_i\na=gets.split.map &:to_i\nm=gets.to_i\nb=gets.split.map &:to_i\nd=[0]*m\nq=[0]*n\np=[-1]*n\nn.times{|i|\n\tmx=0\n\tm.times{|j|\n\t\tmx=[mx,d[j]].max if a[i]>b[j]\n\t\tq[i]=d[j]=mx+1 if a[i]==b[j]\n\t}\n\ti.times{|j|p[i]=j if a[j]=0 and q[i]>0\np q.max\nputs l.reverse.join \" \"\n\n"}, {"source_code": "n=gets.to_i\na=gets.split.map &:to_i\nm=gets.to_i\nb=gets.split.map &:to_i\nd=[0]*m\nq=[0]*n\np=[-1]*n\nn.times{|i|\n mx=0\n m.times{|j|\n mx=[mx,d[j]].max if a[i]>b[j]\n q[i]=d[j]=mx+1 if a[i]==b[j]\n }\n i.times{|j|p[i]=j if a[j]=0 and q[i]>0\np q.max\nputs l.reverse.join \" \""}, {"source_code": "n=gets.to_i\na=gets.split.map &:to_i\nm=gets.to_i\nb=gets.split.map &:to_i\nd=[0]*m\nq=[0]*n\np=[-1]*n\nn.times{|i|\n\tmx=0\n\tm.times{|j|\n\t\tmx=[mx,d[j]].max if a[i]>b[j]\n\t\tq[i]=d[j]=mx+1 if a[i]==b[j]\n\t}\n\ti.times{|j|p[i]=j if a[j]=0 and q[i]>0\np q.max\nputs l.reverse.join \" \"\n"}, {"source_code": "n=gets.to_i\na=gets.split.map &:to_i\nm=gets.to_i\nb=gets.split.map &:to_i\nd=[0]*m\nq=[0]*n\np=[-1]*n\nn.times{|i|\n\tmx=0\n\tm.times{|j|\n\t\tmx=[mx,d[j]].max if a[i]>b[j]\n\t\tq[i]=d[j]=mx+1 if a[i]==b[j]\n\t}\n\ti.times{|j|p[i]=j if a[j]=0 and q[i]>0\np q.max\nputs l.reverse.join \" \"\n\n"}, {"source_code": "n=gets.to_i\na=gets.split.map &:to_i\nm=gets.to_i\nb=gets.split.map &:to_i\nd=[0]*m\nq=[0]*n\np=[-1]*n\nn.times{|i|\n\tmx=0\n\tm.times{|j|\n\t\tmx=[mx,d[j]].max if a[i]>b[j]\n\t\tq[i]=d[j]=mx+1 if a[i]==b[j]\n\t}\n\ti.times{|j|p[i]=j if a[j]=0 and q[i]>0\np q.max\nputs l.reverse.join \" \""}, {"source_code": "n=gets.to_i\na=gets.split.map &:to_i\nm=gets.to_i\nb=gets.split.map &:to_i\nd=[0]*m\nq=[0]*n\np=[-1]*n\nn.times{|i|\n\tmx=0\n\tm.times{|j|\n\t\tmx=[mx,d[j]].max if a[i]>b[j]\n\t\tq[i]=d[j]=mx+1 if a[i]==b[j]\n\t}\n\ti.times{|j|p[i]=j if a[j]=0 and q[i]>0\np q.max\nputs l.reverse.join \" \"\n\n"}, {"source_code": "n=gets.to_i\na=gets.split.map &:to_i\nm=gets.to_i\nb=gets.split.map &:to_i\nd=[0]*m\nq=[0]*n\np=[-1]*n\nn.times{|i|\n\tmx=0\n\tm.times{|j|\n\t\tmx=[mx,d[j]].max if a[i]>b[j]\n\t\tq[i]=d[j]=mx+1 if a[i]==b[j]\n\t}\n\ti.times{|j|p[i]=j if a[j]=0 and q[i]>0\np q.max\nputs l.reverse.join \" \"\n\n"}, {"source_code": "n=gets.to_i\na=gets.split.map &:to_i\nm=gets.to_i\nb=gets.split.map &:to_i\nd=[0]*m\nq=[0]*n\np=[-1]*n\nn.times{|i|\n\tmx=0\n\tm.times{|j|\n\t\tmx=[mx,d[j]].max if a[i]>b[j]\n\t\tq[i]=d[j]=mx+1 if a[i]==b[j]\n\t}\n\ti.times{|j|p[i]=j if a[j]=0 and q[i]>0\np q.max\nputs l.reverse.join \" \"\n\n"}], "negative_code": [{"source_code": "n=gets.to_i\na=gets.split.map &:to_i\nm=gets.to_i\nb=gets.split.map &:to_i\nd=[0]*m\nq=[0]*n\np=[-1]*m\nn.times{|i|\n\tmx=0\n\tm.times{|j|\n\t\tmx=[mx,d[j]].max if a[i]>b[j]\n\t\tq[i]=d[j]=mx+1 if a[i]==b[j]\n\t}\n\ti.times{|j|p[i]=j if a[j]=0\np q.max\nputs l.reverse.join \" \"\n"}], "src_uid": "dca6eee27f002413d5e2eaf28fd60750"} {"nl": {"description": "You are given a string $$$s$$$ consisting only of characters + and -. You perform some process with this string. This process can be described by the following pseudocode: res = 0for init = 0 to inf cur = init ok = true for i = 1 to |s| res = res + 1 if s[i] == '+' cur = cur + 1 else cur = cur - 1 if cur < 0 ok = false break if ok breakNote that the $$$inf$$$ denotes infinity, and the characters of the string are numbered from $$$1$$$ to $$$|s|$$$.You have to calculate the value of the $$$res$$$ after the process ends.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. The only lines of each test case contains string $$$s$$$ ($$$1 \\le |s| \\le 10^6$$$) consisting only of characters + and -. It's guaranteed that sum of $$$|s|$$$ over all test cases doesn't exceed $$$10^6$$$.", "output_spec": "For each test case print one integer — the value of the $$$res$$$ after the process ends.", "sample_inputs": ["3\n--+-\n---\n++--+-"], "sample_outputs": ["7\n9\n6"], "notes": null}, "positive_code": [{"source_code": "t = gets.to_i\nt.times do\n a = []\n b = {}\n cur = 0\n gets.each_char.with_index do |c, i|\n \n case c\n when '+'\n a << (cur += 1)\n when '-'\n a << (cur -= 1)\n else\n a << cur\n end\n \n b[cur] = i + 1 unless b.include? cur\n end\n \n res = 0\n init = 0\n loop do\n \n if b.include?(-init-1)\n res += b[-init-1]\n else\n res += a.size - 1\n break\n end\n \n init += 1\n end\n \n p res\nend\n"}, {"source_code": "def solve\n str = gets.strip\n cur = 0\n h = Hash.new(0)\n str.each_char.with_index { |e, i|\n if e == '+'\n cur += 1\n else\n cur -= 1\n if cur < 0 && h[-cur].zero?\n h[-cur] = i + 1\n end\n end\n }\n res = 0\n (0..str.size).each { |i|\n ck = h[i + 1]\n if ck.zero?\n res += str.size\n break\n else\n res += ck\n end\n }\n puts res\nend\n\ngets.strip.to_i.times { solve }\n"}, {"source_code": "def solve\n str = gets.strip\n cur = 0\n h = Hash.new(0)\n str.each_char.with_index { |e, i|\n if e == '+'\n cur += 1\n else\n cur -= 1\n if cur < 0 && h[-cur].zero?\n h[-cur] = i + 1\n end\n end\n }\n res = 0\n (0..str.size).each { |i|\n ck = h[i + 1]\n if ck.zero?\n res += str.size\n break\n else\n res += ck\n end\n }\n puts res\nend\n\ngets.strip.to_i.times { solve }"}, {"source_code": "def solve\n str = gets.strip\n cur = 0\n h = Hash.new()\n str.each_char.with_index { |e, i|\n if e == '+'\n cur += 1\n else\n cur -= 1\n if cur < 0 && !h.has_key?(-cur)\n h[-cur] = i + 1\n end\n end\n }\n res = 0\n (0..str.size).each { |i|\n ck = h[i + 1] if h.has_key?(i + 1)\n if ck.nil?\n res += str.size\n break\n else\n res += ck\n end\n }\n puts res\nend\n\ngets.strip.to_i.times { solve }\n"}], "negative_code": [{"source_code": "def solve\n str = gets.strip\n cur = 0\n h = Hash.new(0)\n str.each_char.with_index { |e, i|\n if e == '+'\n cur += 1\n else\n cur -= 1\n if cur < 0 && h[-cur].zero?\n h[-cur] = i + 1\n end\n end\n }\n res = 0\n (0..str.size).each { |i|\n ck = h[i + 1]\n if ck.zero?\n res += str.size\n break\n else\n res += h[ck]\n end\n }\n puts res\nend\n\ngets.strip.to_i.times { solve }\n"}, {"source_code": "def solve\n str = gets.strip\n cur = 0\n h = Hash.new(1e7 + 1)\n a = []\n str.each_char.with_index { |e, i|\n if e == '+'\n cur += 1\n else\n cur -= 1\n if cur < 0\n h[-cur] = [h[-cur], i + 1].min\n a << -cur\n end\n end\n }\n res = 0\n (0..str.size).each { |i|\n ck = a.bsearch { |x| x > i}\n if ck.nil?\n res += str.size\n break\n else\n res += h[ck]\n end\n }\n puts res\nend\n\ngets.strip.to_i.times { solve }\n"}, {"source_code": "def solve\n str = gets.strip\n cur = 0\n h = Hash.new(0)\n str.each_char.with_index { |e, i|\n if e == '+'\n cur += 1\n else\n cur -= 1\n if cur < 0 && h[-cur].nil?\n h[-cur] = i + 1\n end\n end\n }\n res = 0\n (0..str.size).each { |i|\n ck = h[i + 1]\n if ck.zero?\n res += str.size\n break\n else\n res += h[ck]\n end\n }\n puts res\nend\n\ngets.strip.to_i.times { solve }\n"}], "src_uid": "07eecfe948aa78623586b5e30e84e415"} {"nl": {"description": "On a strip of land of length $$$n$$$ there are $$$k$$$ air conditioners: the $$$i$$$-th air conditioner is placed in cell $$$a_i$$$ ($$$1 \\le a_i \\le n$$$). Two or more air conditioners cannot be placed in the same cell (i.e. all $$$a_i$$$ are distinct).Each air conditioner is characterized by one parameter: temperature. The $$$i$$$-th air conditioner is set to the temperature $$$t_i$$$. Example of strip of length $$$n=6$$$, where $$$k=2$$$, $$$a=[2,5]$$$ and $$$t=[14,16]$$$. For each cell $$$i$$$ ($$$1 \\le i \\le n$$$) find it's temperature, that can be calculated by the formula $$$$$$\\min_{1 \\le j \\le k}(t_j + |a_j - i|),$$$$$$where $$$|a_j - i|$$$ denotes absolute value of the difference $$$a_j - i$$$.In other words, the temperature in cell $$$i$$$ is equal to the minimum among the temperatures of air conditioners, increased by the distance from it to the cell $$$i$$$.Let's look at an example. Consider that $$$n=6, k=2$$$, the first air conditioner is placed in cell $$$a_1=2$$$ and is set to the temperature $$$t_1=14$$$ and the second air conditioner is placed in cell $$$a_2=5$$$ and is set to the temperature $$$t_2=16$$$. In that case temperatures in cells are: temperature in cell $$$1$$$ is: $$$\\min(14 + |2 - 1|, 16 + |5 - 1|)=\\min(14 + 1, 16 + 4)=\\min(15, 20)=15$$$; temperature in cell $$$2$$$ is: $$$\\min(14 + |2 - 2|, 16 + |5 - 2|)=\\min(14 + 0, 16 + 3)=\\min(14, 19)=14$$$; temperature in cell $$$3$$$ is: $$$\\min(14 + |2 - 3|, 16 + |5 - 3|)=\\min(14 + 1, 16 + 2)=\\min(15, 18)=15$$$; temperature in cell $$$4$$$ is: $$$\\min(14 + |2 - 4|, 16 + |5 - 4|)=\\min(14 + 2, 16 + 1)=\\min(16, 17)=16$$$; temperature in cell $$$5$$$ is: $$$\\min(14 + |2 - 5|, 16 + |5 - 5|)=\\min(14 + 3, 16 + 0)=\\min(17, 16)=16$$$; temperature in cell $$$6$$$ is: $$$\\min(14 + |2 - 6|, 16 + |5 - 6|)=\\min(14 + 4, 16 + 1)=\\min(18, 17)=17$$$. For each cell from $$$1$$$ to $$$n$$$ find the temperature in it.", "input_spec": "The first line contains one integer $$$q$$$ ($$$1 \\le q \\le 10^4$$$) — the number of test cases in the input. Then test cases follow. Before each test case, there is an empty line. Each test case contains three lines. The first line contains two integers $$$n$$$ ($$$1 \\le n \\le 3 \\cdot 10^5$$$) and $$$k$$$ ($$$1 \\le k \\le n$$$) — the length of the strip of land and the number of air conditioners respectively. The second line contains $$$k$$$ integers $$$a_1, a_2, \\ldots, a_k$$$ ($$$1 \\le a_i \\le n$$$) — positions of air conditioners on the strip of land. The third line contains $$$k$$$ integers $$$t_1, t_2, \\ldots, t_k$$$ ($$$1 \\le t_i \\le 10^9$$$) — temperatures of air conditioners. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 \\cdot 10^5$$$.", "output_spec": "For each test case output $$$n$$$ integers separated by space: temperatures of air in cells.", "sample_inputs": ["5\n\n6 2\n2 5\n14 16\n\n10 1\n7\n30\n\n5 5\n3 1 4 2 5\n3 1 4 2 5\n\n7 1\n1\n1000000000\n\n6 3\n6 1 3\n5 5 5"], "sample_outputs": ["15 14 15 16 16 17 \n36 35 34 33 32 31 30 31 32 33 \n1 2 3 4 5 \n1000000000 1000000001 1000000002 1000000003 1000000004 1000000005 1000000006 \n5 6 5 6 6 5"], "notes": null}, "positive_code": [{"source_code": "\nt = gets.chomp.to_i\nt.times do \n gets\n n, _ = gets.chomp.split(' ').map(&:to_i)\n pos = gets.chomp.split(' ').map(&:to_i)\n temp = gets.chomp.split(' ').map(&:to_i)\n\n ans_l = ans_r = Array.new(n+1) { 10**10 }\n pos.zip(temp).each do |p, t|\n ans_l[p] = ans_r[p] = t\n end\n\n 2.upto n do |i|\n ans_l[i] = [ans_l[i], ans_l[i-1] + 1].min\n end\n\n (n-1).downto 1 do |i|\n ans_r[i] = [ans_r[i], ans_r[i+1] + 1].min\n end\n\n ans = ans_l.zip(ans_r).map { |a, b| [a,b].min } \n puts \"#{ans[1..].join(' ')}\\n\"\nend\n"}], "negative_code": [], "src_uid": "16bd6786078dbaa443e97eec581cff73"} {"nl": {"description": "You are given two positive integers $$$a$$$ and $$$b$$$.In one move, you can change $$$a$$$ in the following way: Choose any positive odd integer $$$x$$$ ($$$x > 0$$$) and replace $$$a$$$ with $$$a+x$$$; choose any positive even integer $$$y$$$ ($$$y > 0$$$) and replace $$$a$$$ with $$$a-y$$$. You can perform as many such operations as you want. You can choose the same numbers $$$x$$$ and $$$y$$$ in different moves.Your task is to find the minimum number of moves required to obtain $$$b$$$ from $$$a$$$. It is guaranteed that you can always obtain $$$b$$$ from $$$a$$$.You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case is given as two space-separated integers $$$a$$$ and $$$b$$$ ($$$1 \\le a, b \\le 10^9$$$).", "output_spec": "For each test case, print the answer — the minimum number of moves required to obtain $$$b$$$ from $$$a$$$ if you can perform any number of moves described in the problem statement. It is guaranteed that you can always obtain $$$b$$$ from $$$a$$$.", "sample_inputs": ["5\n2 3\n10 10\n2 4\n7 4\n9 3"], "sample_outputs": ["1\n0\n2\n2\n1"], "notes": "NoteIn the first test case, you can just add $$$1$$$.In the second test case, you don't need to do anything.In the third test case, you can add $$$1$$$ two times.In the fourth test case, you can subtract $$$4$$$ and add $$$1$$$.In the fifth test case, you can just subtract $$$6$$$."}, "positive_code": [{"source_code": "# cook your code here\nt = gets.to_i\nuntil t == 0\n t-=1\n x,y = gets.chomp.split().map { |e| e.to_i }\n if(x == y)\n puts \"0\"\n elsif(x > y)\n if((x-y)%2 == 0)\n puts \"1\"\n else\n puts \"2\"\n end\n else\n if((y-x)%2 == 1)\n puts \"1\"\n else\n puts \"2\"\n end\n end\nend"}, {"source_code": "t = gets.chomp.to_i\n\nuntil t==0\n t-=1\n x, y = gets.split.map(&:to_i)\n if(x>y && (x-y)%2!=0)\n puts 2\n elsif(x>y && (x-y)%2==0)\n puts 1\n elsif(x 0\n k = gets.split(\" \")\n a = k[0].to_i\n b = k[1].to_i\n \n if a == b\n puts 0\n elsif a > b\n if a % 2 != b % 2\n puts 2\n else\n puts 1\n end\n else\n if a % 2 != b % 2\n puts 1\n else\n puts 2\n end\n end\n t -= 1\nend\n"}, {"source_code": "require 'set'; require 'prime'\nINF=Float::INFINITY; MOD=10**9+7\nt = gets.chomp.to_i\nt.times do\n a,b = gets.chomp.split.map(&:to_i)\n if a < b\n if (b-a).odd?\n puts 1\n else\n puts 2\n end\n elsif a > b\n if (a-b).even?\n puts 1\n else\n puts 2\n end\n else\n puts 0\n end\nend\n"}, {"source_code": "N = gets.to_i\nN.times do \n a,b = gets.split.map(&:to_i)\n f = b - a;\n ans = 0\n if f == 0\n ans = 0\n elsif f<0 && f.odd?\n ans = 2\n elsif f<0 && f.even?\n ans = 1\n elsif f>0 && f.odd?\n ans = 1\n elsif f>0 && f.even?\n ans = 2\n end\n puts ans\nend"}, {"source_code": "n=gets.to_i\n(1..n).each do |x|\n a,b=gets.chomp.split(\" \").map(&:to_i)\n if a == b\n print(\"0\\n\")\n elsif a > b && (a - b) % 2 == 0\n \tprint(\"1\\n\")\n elsif a > b && (a - b) % 2 == 1\n \tprint(\"2\\n\")\n elsif a < b && (b - a) % 2 == 0\n \tprint(\"2\\n\")\n elsif a < b && (b - a) % 2 == 1\n \tprint(\"1\\n\")\n end\nend"}, {"source_code": "gets.to_i.times {\n a, b = gets.split.map { |v| v.to_i }\n\n if a == b\n puts 0\n elsif a < b\n puts 2 - (b - a) % 2\n else\n puts 1 + (b - a) % 2\n end\n}\n"}, {"source_code": "def gli\n gets.split.map(&:to_i)\nend\n\nn = gets.to_i\nn.times {\n a, b = gli\n\n if a < b\n if (a - b) % 2 == 1\n puts 1\n else\n puts 2\n end\n elsif a == b\n puts 0\n else\n if (b - a) % 2 == 1\n puts 2\n else\n puts 1\n end\n end\n}\n"}, {"source_code": "def input_line \n gets.split.map(&:to_i) \nend\n\nn = gets.to_i\n\nn.times do |l|\n a, b = input_line\n if a == b then\n puts 0\n elsif a > b\n puts (b - a).even? ? 1 : 2\n else\n puts (a - b).even? ? 2 : 1\n end\nend"}, {"source_code": "gets.to_i.times do\n a, b = gets.split.map &:to_i\n d = a - b\n if d == 0\n puts 0\n elsif d%2 == 0\n puts d > 0 ? 1 : 2\n else\n puts d < 0 ? 1 : 2\n end\nend\n"}, {"source_code": "1.upto(gets.to_i) do\n a, b = gets.split(' ').map(&:to_i)\n result = (b-a).zero? ? 0 : 2 - (b-a).modulo(2) \n\n result = 3 - result if (b - a) < 0\n\n puts result\nend\n"}, {"source_code": "t = gets.to_i\n(1).upto(t) do\n a = gets.split(' ').map(&:to_i)\n answer = 2\n if(a[0]a[1] and (a[0]-a[1]).modulo(2) == 0)\n answer=1\n elsif a[0]==a[1]\n answer=0\n end\n puts answer\nend\n"}], "negative_code": [], "src_uid": "fcd55a1ca29e96c05a3b65b7a8103842"} {"nl": {"description": "Let's assume that we are given a matrix b of size x × y, let's determine the operation of mirroring matrix b. The mirroring of matrix b is a 2x × y matrix c which has the following properties: the upper half of matrix c (rows with numbers from 1 to x) exactly matches b; the lower half of matrix c (rows with numbers from x + 1 to 2x) is symmetric to the upper one; the symmetry line is the line that separates two halves (the line that goes in the middle, between rows x and x + 1). Sereja has an n × m matrix a. He wants to find such matrix b, that it can be transformed into matrix a, if we'll perform on it several (possibly zero) mirrorings. What minimum number of rows can such matrix contain?", "input_spec": "The first line contains two integers, n and m (1 ≤ n, m ≤ 100). Each of the next n lines contains m integers — the elements of matrix a. The i-th line contains integers ai1, ai2, ..., aim (0 ≤ aij ≤ 1) — the i-th row of the matrix a.", "output_spec": "In the single line, print the answer to the problem — the minimum number of rows of matrix b.", "sample_inputs": ["4 3\n0 0 1\n1 1 0\n1 1 0\n0 0 1", "3 3\n0 0 0\n0 0 0\n0 0 0", "8 1\n0\n1\n1\n0\n0\n1\n1\n0"], "sample_outputs": ["2", "3", "2"], "notes": "NoteIn the first test sample the answer is a 2 × 3 matrix b:001110If we perform a mirroring operation with this matrix, we get the matrix a that is given in the input:001110110001"}, "positive_code": [{"source_code": "\n\n#$stdin.reopen('/home/chen/Desktop/input.txt', 'r')\n#$stdout.reopen('/home/chen/Desktop/output.txt', 'w')\n\n\ndef min(a)\n\tf = 1\n\ts = 0\n\ta.each do |e|\n\t\tif f\n\t\t\ts=e\n\t\t\tf=0\n\t\telse\n\t\t\ts=e if e < s\t\t\n\t\tend\n\tend\n\ts\nend\n\n\n\ndef solve(ib,ie,j,a)\n\tif (ie-ib+1)%2 !=0\n\t\treturn 0\n\tend\n\tb ,e = ib, ie\n\twhile e>b\n\t\tif a[e][j] != a[b][j]\n\t\t\treturn 0\n\t\tend\n\t\tb+=1\n\t\te-=1\n\tend\n\n\treturn solve(ib, (ib+ie)/2,j,a)+1\nend\n\nn,m = gets.split.map(&:to_i)\na = [0]*n\nans = [0]*m\n\n(1..n).each do |i|\n\ta[i-1] = gets.split.map(&:to_i)\nend\n\n(0..ans.size-1).each do |i|\n\tans[i] = solve(0,n-1,i,a)\nend\n\n\nputs n/(2**min(ans))"}, {"source_code": "def run\n n, m = $stdin.gets.split.map(&:to_i)\n a = []\n n.times do |i|\n a << $stdin.gets.chomp\n end\n\n ans = n\n flag = true\n cur = n\n while flag\n if cur % 2 == 0\n cur = cur / 2\n else\n ans = cur\n break\n end\n\n cur.times do |i|\n if a[i] != a[cur * 2 - 1 - i]\n flag = false\n ans = cur * 2\n break\n end\n end\n end\n\n puts ans\nend\n\nrun if $0 == __FILE__\n"}], "negative_code": [], "src_uid": "90125e9d42c6dcb0bf3b2b5e4d0f845e"} {"nl": {"description": "Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex cover problem very interesting.Suppose the graph G is given. Subset A of its vertices is called a vertex cover of this graph, if for each edge uv there is at least one endpoint of it in this set, i.e. or (or both).Pari and Arya have won a great undirected graph as an award in a team contest. Now they have to split it in two parts, but both of them want their parts of the graph to be a vertex cover.They have agreed to give you their graph and you need to find two disjoint subsets of its vertices A and B, such that both A and B are vertex cover or claim it's impossible. Each vertex should be given to no more than one of the friends (or you can even keep it for yourself).", "input_spec": "The first line of the input contains two integers n and m (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000) — the number of vertices and the number of edges in the prize graph, respectively. Each of the next m lines contains a pair of integers ui and vi (1  ≤  ui,  vi  ≤  n), denoting an undirected edge between ui and vi. It's guaranteed the graph won't contain any self-loops or multiple edges.", "output_spec": "If it's impossible to split the graph between Pari and Arya as they expect, print \"-1\" (without quotes). If there are two disjoint sets of vertices, such that both sets are vertex cover, print their descriptions. Each description must contain two lines. The first line contains a single integer k denoting the number of vertices in that vertex cover, and the second line contains k integers — the indices of vertices. Note that because of m ≥ 1, vertex cover cannot be empty.", "sample_inputs": ["4 2\n1 2\n2 3", "3 3\n1 2\n2 3\n1 3"], "sample_outputs": ["1\n2 \n2\n1 3", "-1"], "notes": "NoteIn the first sample, you can give the vertex number 2 to Arya and vertices numbered 1 and 3 to Pari and keep vertex number 4 for yourself (or give it someone, if you wish).In the second sample, there is no way to satisfy both Pari and Arya."}, "positive_code": [{"source_code": "require 'set'\n\nn, m = gets.split.map(&:to_i)\nadj = Array.new(n){[]}\nm.times do\n u, v = gets.split.map{|s| s.to_i-1}\n adj[u] << v\n adj[v] << u\nend\n\nrem = (0...n).to_set\nsides = Array.new(n)\nwhile rem.any?\n v = rem.first\n rem.delete v\n sides[v] = 0\n q = [v]\n while q.any?\n v = q.shift\n side = sides[v];\n adj[v].each do |s|\n if not sides[s]\n newside = (side + 1) % 2\n sides[s] = newside\n q << s\n rem.delete s\n else\n if sides[s] == side\n puts -1\n exit\n end\n end\n end\n end\nend\n\nsides.each_with_index.group_by(&:first).values.each do |vs|\n puts vs.size\n puts vs.map(&:last).map{|v| v+1}.join(\" \")\nend"}], "negative_code": [], "src_uid": "810f267655da0ad14538c275fd13821d"} {"nl": {"description": "There are n boys and m girls studying in the class. They should stand in a line so that boys and girls alternated there as much as possible. Let's assume that positions in the line are indexed from left to right by numbers from 1 to n + m. Then the number of integers i (1 ≤ i < n + m) such that positions with indexes i and i + 1 contain children of different genders (position i has a girl and position i + 1 has a boy or vice versa) must be as large as possible. Help the children and tell them how to form the line.", "input_spec": "The single line of the input contains two integers n and m (1 ≤ n, m ≤ 100), separated by a space.", "output_spec": "Print a line of n + m characters. Print on the i-th position of the line character \"B\", if the i-th position of your arrangement should have a boy and \"G\", if it should have a girl. Of course, the number of characters \"B\" should equal n and the number of characters \"G\" should equal m. If there are multiple optimal solutions, print any of them.", "sample_inputs": ["3 3", "4 2"], "sample_outputs": ["GBGBGB", "BGBGBB"], "notes": "NoteIn the first sample another possible answer is BGBGBG. In the second sample answer BBGBGB is also optimal."}, "positive_code": [{"source_code": "f = File.open(\"input.txt\", \"r\")\ns = f.read.split\nn = s[0].to_i\nm = s[1].to_i\nf.close\n\nif n > m then\n s = \"BG\"*m + \"B\"*(n-m)\nelse\n s = \"GB\"*n + \"G\"*(m-n)\nend\n\nf = File.open(\"output.txt\", \"w\")\nf.write(s)\nf.close\nputs s"}, {"source_code": "#!/usr/bin/ruby\nf=nil\nif true\n\tf=File.open('input.txt','rb')\nelse\n\tf=$<\nend\nb,g=f.gets.split.map(&:to_i)\nm=[b,g].min\nb-=m;g-=m\np [g,m,b]\nFile.write('output.txt','G'*g+'BG'*m+'B'*b)"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\ninput=gets.to_s.split\nn = input[0].to_i\nm = input[1].to_i\nstr = \"\"\nif n==m\n\tn.times{str+=\"BG\"}\nelsif n > m\n\tm.times{str+=\"BG\"}\n\t(n-m).times{str+=\"B\"}\nelse\n\tn.times{str+=\"GB\"}\n\t(m-n).times{str+=\"G\"}\nend\nputs str"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt', 'w')\nn,m=gets.split.map(&:to_i)\nm>=n ? (print \"GB\"*[m,n].min; print \"G\"*(m-n) if nm) \n"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt', 'w')\nn,m=gets.split.map(&:to_i)\nprint \"GB\"*[m,n].min if m>=n\n print \"BG\"*[m,n].min if mm \nprint \"G\"*(m-n) if n m\n puts \"BG\" * m + \"B\" * (n - m)\nelse\n puts \"GB\" * n + \"G\" * (m - n)\nend"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\nn, m = gets.split.map(&:to_i)\nputs (n <= m ? \"GB\" : \"BG\")*[n, m].min + (n <= m ? \"G\" : \"B\")*([n,m].max-[n,m].min)"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\na=gets.chomp.split(\" \")\nb=a[0].to_i\ng=a[1].to_i\nans=\"\"\nif b>=g\nloop do\nbreak if b==0 && g==0\nif b!=0\nans+=\"B\"\nb-=1\nend\nif g!=0\nans+=\"G\"\ng-=1\nend\nend\nelse\nloop do\nbreak if b==0 && g==0\nif g!=0\nans+=\"G\"\ng-=1\nend\nif b!=0\nans+=\"B\"\nb-=1\nend\nend\nend\n\nputs \"#{ans}\""}, {"source_code": "n,m=File.open(\"input.txt\").gets.split.map(&:to_i)\nFile.open(\"output.txt\", \"w\") { |f|\n\tif n < m then\n\t\tf.puts \"GB\"*n + \"G\"*(m-n)\n\telse\n\t\tf.puts \"BG\"*m + \"B\"*(n-m)\n\tend\n}\n"}, {"source_code": "m, n= File.open(\"input.txt\").gets.split.map(&:to_i)\nFile.open(\"output.txt\", \"w\") { |f|\n if m > n\n f.puts 'BG'*n + 'B'*(m-n)\n else\n f.puts 'GB'*m + 'G'*(n-m)\n end\n}"}, {"source_code": "a=[]\nFile.open(\"input.txt\",\"r\") do |file|\n a=file.gets.chomp.split\nend\n\nn=a[0].to_i\nm=a[1].to_i\n\n\n\nif(n>m)\n maxc='B'\n minc='G'\n mini=m\nelse\n maxc='G'\n minc='B'\n mini=n\nend\n\na=\"\"\n\n(n+m).times do\n a+=maxc\nend\n\nind=1\nmini.times do\n a[ind]=minc\n ind+=2\nend\nFile.open(\"output.txt\",\"w\") do |file|\n file.puts a\nend\n\n"}, {"source_code": "n, m = 0, 0\nFile.open(\"input.txt\", \"r\") do |file|\n n, m = file.gets.split.collect{|i| i.to_i}\nend\nFile.open(\"output.txt\", \"w\") do |file|\n if n >= m\n file.puts (['B','G']*m+['B']*(n-m)).join\n else\n file.puts (['G','B']*n+['G']*(m-n)).join\n end\nend\n"}, {"source_code": "#n, m = IO.readlines(\"input.txt\")[0].split.collect{|i| i.to_i}\nn, m = File.new(\"input.txt\", \"r\").gets.split.collect{|i| i.to_i}\nif n >= m\n File.new(\"output.txt\", \"w\").puts (['B','G']*m+['B']*(n-m)).join\nelse\n File.new(\"output.txt\", \"w\").puts (['G','B']*n+['G']*(m-n)).join\nend\n"}, {"source_code": "file = File.new(\"input.txt\", \"r\")\nn, m = file.gets.split.collect{|i| i.to_i}\nfile.close\nfile = File.new(\"output.txt\", \"w\")\nif n >= m\n file.puts (['B','G']*m+['B']*(n-m)).join\nelse\n file.puts (['G','B']*n+['G']*(m-n)).join\nend\nfile.close\n"}, {"source_code": "n, m = File.new(\"input.txt\", \"r\").gets.split.collect{|i| i.to_i}\nif n >= m\n File.new(\"output.txt\", \"w\").puts (['B','G']*m+['B']*(n-m)).join\nelse\n File.new(\"output.txt\", \"w\").puts (['G','B']*n+['G']*(m-n)).join\nend\n"}, {"source_code": "file = File.open('input.txt', \"r\")\nn, m = file.gets.split.map { |x| x.to_i }\nfile.close\n\nFile.open('output.txt', 'w') { |f| f.write(n == m ? 'GB' * n : n > m ? 'BG' * m + 'B' * (n - m) : 'GB' * n + 'G' * (m - n)) }"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\na = gets.split.map(&:to_i)\n\nif a[0] == a[1]\n a[0].times do\n print \"BG\"\n end\n exit\nend\np=a[0]-a[1]\nr=a[1]-a[0]\nif a[0]>a[1]\n a[1].times do\n print \"BG\"\n end\n p.times do\n print \"B\" \n end\n exit\nend\n\nif a[1]>a[0]\n a[0].times do\n print \"GB\"\n end\n r.times do\n print \"G\" \n end\n exit\nend "}, {"source_code": "n, m = File.open(\"input.txt\").gets.split.map(&:to_i)\noutput = File.open(\"output.txt\", \"w\")\nwhile n > 0 || m > 0\n\tif (n >= m)\n\t\toutput.print 'B'\n\t\toutput.print 'G' if m > 0\n\telse\n\t\toutput.print 'G'\n\t\toutput.print 'B' if n > 0\n\tend\n\tn, m = n - 1, m - 1\nend\n"}, {"source_code": "input = File.new('input.txt', 'r')\noutput = File.new('output.txt', 'w')\n\nt = input.gets.split.collect { |e| e.to_i }\n\nn,m = t[0], t[1]\n\nf,l = 'B','G'\nif n < m\n\tf,l = 'G','B'\nend\nmin = t.min\nothers = t.max - min\n\nfor i in 1..min do\n\toutput.print f,l\nend\nfor i in 1..others do\n\toutput.print f\nend\n\ninput.close\noutput.close"}], "negative_code": [{"source_code": "f = File.open(\"input.txt\", \"r\")\ns = f.read.split\nn = s[0].to_i\nm = s[1].to_i\nf.close\n\nif n > m then\n s = \"BG\"*m + \"B\"*(n-m)\nelse\n s = \"GB\"*n + \"B\"*(m-n)\nend\n\nf = File.open(\"output.txt\", \"w\")\nf.write(s)\nf.close\nputs s"}, {"source_code": "#!/usr/bin/ruby\nf=nil\nif true\n\tf=File.open('input.txt','rb')\nelse\n\tf=$<\nend\nb,g=f.gets.split.map(&:to_i)\nm=[b,g].min\nb-=m;g-=m\np [g,m,b]\nputs 'G'*g+'BG'*m+'B'*b"}, {"source_code": "#!/usr/bin/ruby\nf=nil\nif true\n\tf=File.open('input.txt','rb')\nelse\n\tf=$<\nend\nb,g=f.gets.split.map(&:to_i)\nm=[b,g].min\nb-=m;g-=m\nputs 'G'*g+'BG'*m+'B'*b"}, {"source_code": "#!/usr/bin/ruby\n$stdin=File.open('input.txt','rb')\nb,g=gets.split.map(&:to_i)\nm=[b,g].min\nb-=m;g-=m\nputs 'G'*g+'BG'*m+'B'*b"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt', 'w')\nn,m=gets.split.map(&:to_i)\nprint \"GB\"*[m,n].min if m>=n\n print \"BG\"*[m,n].min if mm \nprint \"G\"*m if nm\nprint \"G\"*m if n=g\nloop do\nbreak if b==0 && g==0\n\nif b!=0\nans+=\"B\"\nb-=1\nend\n\nif g!=0\nans+=\"G\"\ng-=1\nend\n\nend\nelse\nloop do\nbreak if b==0 && g==0\n\nif b!=0\nans+=\"B\"\nb-=1\nend\n\nif g!=0\nans+=\"G\"\ng-=1\nend\n\nend\nend\n\nputs \"#{ans}\""}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\na=gets.chomp.split(\" \")\nb=a[0].to_i\ng=a[1].to_i\nans=\"\"\n\nloop do\nbreak if b==0 && g==0\n\nif b!=0\nans+=\"B\"\nb-=1\nend\n\nif g!=0\nans+=\"G\"\ng-=1\nend\n\nend\n\nputs \"#{ans}\""}, {"source_code": "m, n= File.open(\"input.txt\").gets.split.map(&:to_i)\nFile.open(\"output.txt\", \"w\") { |f|\n if m > n\n f.puts 'BG'*n + 'B'*(m-n)\n else\n f.puts 'GB'*n + 'G'*(n-m)\n end\n}"}, {"source_code": "n, m = IO.readlines(\"input.txt\")[0].split.collect{|i| i.to_i}\nif n >= m\n puts (['B','G']*m+['B']*(n-m)).join\nelse\n puts (['G','B']*n+['G']*(m-n)).join\nend\n"}, {"source_code": "puts 2"}, {"source_code": "b=gets.to_i\ng=gets.to_i\nif b == g\n b.times do\n puts \"BG\"\n end\nend\n \nif b>g\n g.times do\n puts \"BG\"\n end\n b-g.times do\n puts \"B\" \n end\nend\n\nif g>b\n b.times do\n puts \"GB\"\n end\n g-b.times do\n puts \"G\" \n end\nend"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\na = gets.split.map(&:to_i)\n\nif a[0] == a[1]\n a[0].times do\n print \"BG\"\n end\n exit\nend\n \nif a[0]>a[1]\n a[1].times do\n print \"BG\"\n end\n a[0]-a[1].times do\n print \"B\" \n end\n exit\nend\n\nif a[0]>a[1]\n a[1].times do\n print \"GB\"\n end\n a[1]-a[0].times do\n print \"G\" \n end\n exit\nend"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\na = gets.split.map(&:to_i)\nif a[0] == a[1]\n a[0].times do\n print \"BG\"\n end\nend\n \nif a[0]>a[1]\n a[1].times do\n print \"BG\"\n end\n a[0]-a[1].times do\n print \"B\" \n end\nend\n\nif a[0]>a[1]\n a[1].times do\n print \"GB\"\n end\n a[1]-a[0].times do\n print \"G\" \n end\nend"}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\na = gets.split.map(&:to_i)\n\nif a[0] == a[1]\n a[0].times do\n print \"BG\"\n end\n exit\nend\n \nif a[0]>a[1]\n a[0].times do\n print \"BG\"\n end\n a[0]-a[1].times do\n print \"B\" \n end\n exit\nend\n\nif a[0]>a[1]\n a[1].times do\n print \"GB\"\n end\n a[1]-a[0].times do\n print \"G\" \n end\n exit\nend "}, {"source_code": "$stdin=File.open('input.txt')\n$stdout=File.open('output.txt','w')\na = gets.split.map(&:to_i)\n\nif a[0] == a[1]\n a[0].times do\n print \"BG\"\n end\n exit\nend\n \nif a[0]>a[1]\n a[1].times do\n print \"BG\"\n end\n a[0]-a[1].times do\n print \"B\" \n end\n exit\nend\n\nif a[1]>a[0]\n a[0].times do\n print \"GB\"\n end\n a[1]-a[0].times do\n print \"G\" \n end\n exit\nend "}], "src_uid": "5392996bd06bf52b48fe30b64493f8f5"} {"nl": {"description": "The grasshopper is located on the numeric axis at the point with coordinate $$$x_0$$$.Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate $$$x$$$ with a distance $$$d$$$ to the left moves the grasshopper to a point with a coordinate $$$x - d$$$, while jumping to the right moves him to a point with a coordinate $$$x + d$$$.The grasshopper is very fond of positive integers, so for each integer $$$i$$$ starting with $$$1$$$ the following holds: exactly $$$i$$$ minutes after the start he makes a jump with a distance of exactly $$$i$$$. So, in the first minutes he jumps by $$$1$$$, then by $$$2$$$, and so on.The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.For example, if after $$$18$$$ consecutive jumps he arrives at the point with a coordinate $$$7$$$, he will jump by a distance of $$$19$$$ to the right, since $$$7$$$ is an odd number, and will end up at a point $$$7 + 19 = 26$$$. Since $$$26$$$ is an even number, the next jump the grasshopper will make to the left by a distance of $$$20$$$, and it will move him to the point $$$26 - 20 = 6$$$.Find exactly which point the grasshopper will be at after exactly $$$n$$$ jumps.", "input_spec": "The first line of input contains an integer $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$) — the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$x_0$$$ ($$$-10^{14} \\leq x_0 \\leq 10^{14}$$$) and $$$n$$$ ($$$0 \\leq n \\leq 10^{14}$$$) — the coordinate of the grasshopper's initial position and the number of jumps.", "output_spec": "Print exactly $$$t$$$ lines. On the $$$i$$$-th line print one integer — the answer to the $$$i$$$-th test case — the coordinate of the point the grasshopper will be at after making $$$n$$$ jumps from the point $$$x_0$$$.", "sample_inputs": ["9\n0 1\n0 2\n10 10\n10 99\n177 13\n10000000000 987654321\n-433494437 87178291199\n1 0\n-1 1"], "sample_outputs": ["-1\n1\n11\n110\n190\n9012345679\n-87611785637\n1\n0"], "notes": "NoteThe first two test cases in the example correspond to the first two jumps from the point $$$x_0 = 0$$$. Since $$$0$$$ is an even number, the first jump of length $$$1$$$ is made to the left, and the grasshopper ends up at the point $$$0 - 1 = -1$$$.Then, since $$$-1$$$ is an odd number, a jump of length $$$2$$$ is made to the right, bringing the grasshopper to the point with coordinate $$$-1 + 2 = 1$$$."}, "positive_code": [{"source_code": "t = gets.to_i\nt.times do\n x0, n = gets.split.map(&:to_i)\n x = x0\n m = n / 4 * 4 + 1\n (m .. n).each do |i|\n if x.odd?\n x += i\n else\n x -= i\n end\n end\n puts x\nend"}, {"source_code": "gets.to_i.times do\r\n x,n=gets.split.map(&:to_i)\r\n if n%4==0\r\n puts x\r\n elsif n%4==1\r\n if x.even?\r\n puts x-n\r\n else\r\n puts x+n\r\n end\r\n elsif n%4==2\r\n if x.even?\r\n puts x+1\r\n else\r\n puts x-1\r\n end\r\n else\r\n if x.even?\r\n puts x+n+1\r\n else\r\n puts x-n-1\r\n end\r\n end\r\nend\r\n"}], "negative_code": [], "src_uid": "dbe12a665c374ce3745e20b4a8262eac"} {"nl": {"description": "And now the numerous qualifying tournaments for one of the most prestigious Russian contests Russian Codec Cup are over. All n participants who have made it to the finals found themselves in a huge m-floored 108-star hotel. Of course the first thought to come in a place like this is \"How about checking out the elevator?\".The hotel's elevator moves between floors according to one never changing scheme. Initially (at the moment of time 0) the elevator is located on the 1-st floor, then it moves to the 2-nd floor, then — to the 3-rd floor and so on until it reaches the m-th floor. After that the elevator moves to floor m - 1, then to floor m - 2, and so on until it reaches the first floor. This process is repeated infinitely. We know that the elevator has infinite capacity; we also know that on every floor people get on the elevator immediately. Moving between the floors takes a unit of time.For each of the n participant you are given si, which represents the floor where the i-th participant starts, fi, which represents the floor the i-th participant wants to reach, and ti, which represents the time when the i-th participant starts on the floor si.For each participant print the minimum time of his/her arrival to the floor fi. If the elevator stops on the floor si at the time ti, then the i-th participant can enter the elevator immediately. If the participant starts on the floor si and that's the floor he wanted to reach initially (si = fi), then the time of arrival to the floor fi for this participant is considered equal to ti.", "input_spec": "The first line contains two space-separated integers n and m (1 ≤ n ≤ 105, 2 ≤ m ≤ 108). Next n lines contain information about the participants in the form of three space-separated integers si fi ti (1 ≤ si, fi ≤ m, 0 ≤ ti ≤ 108), described in the problem statement.", "output_spec": "Print n lines each containing one integer — the time of the arrival for each participant to the required floor.", "sample_inputs": ["7 4\n2 4 3\n1 2 0\n2 2 0\n1 2 1\n4 3 5\n1 2 2\n4 2 0", "5 5\n1 5 4\n1 3 1\n1 3 4\n3 1 5\n4 2 5"], "sample_outputs": ["9\n1\n0\n7\n10\n7\n5", "12\n10\n10\n8\n7"], "notes": "NoteLet's consider the first sample. The first participant starts at floor s = 2 by the time equal to t = 3. To get to the floor f = 4, he has to wait until the time equals 7, that's the time when the elevator will go upwards for the second time. Then the first participant should get on the elevator and go two floors up. In this case the first participant gets to the floor f at time equal to 9. The second participant starts at the time t = 0 on the floor s = 1, enters the elevator immediately, and arrives to the floor f = 2. The third participant doesn't wait for the elevator, because he needs to arrive to the same floor where he starts."}, "positive_code": [{"source_code": "# -*- coding: utf-8 -*-\n\ndef hoge(s,f,t,m)\n return t if s==f\n p=(m-1)*2\n base= (t/p)*p\n t -= base\n base + if s < f\n #up\n if t<=s-1\n f-1\n else\n p+f-1\n end\n else\n #down\n if t<=p-s+1\n p-f+1\n else\n p-f+1 + p \n end\n end\nend\n\nn,m=gets.split.map(&:to_i)\nn.times {\n s,f,t=gets.split.map(&:to_i)\n r = hoge(s,f,t,m)\n puts r\n}\n"}, {"source_code": "# -*- coding: utf-8 -*-\ndef f(s,f,t,m)\n return t if s==f\n p=(m-1)*2\n base= (t/p)*p\n ont = [base+s-1,base+p-s+1,base+s-1+p,base+p-s+1+p].find{|i| i>=t}\n [base+f-1,base+p-f+1,base+f-1+p,base+p-f+1+p].find{ |i| i>=ont}\nend\n\nn,m=gets.split.map(&:to_i)\nn.times {\n s,f,t=gets.split.map(&:to_i)\n puts f(s,f,t,m)\n}\n"}, {"source_code": "n,m=gets.split.map(&:to_i)\np=(m-1)*2\nn.times {\n s,f,t=gets.split.map(&:to_i)\n if s==f\n puts t\n else\n base= (t/p)*p\n ont = [base+s-1,base+p-s+1,base+s-1+p,base+p-s+1+p].find{|i| i>=t}\n puts [base+f-1,base+p-f+1,base+f-1+p,base+p-f+1+p].find{ |i| i>=ont} \n end\n}\n"}, {"source_code": "def calctime(m, s, f, t)\n if s < f\n wk = s - 1\n else\n wk = (m-1) + (m-s)\n end\n while true\n break if t <= wk\n wk += (m-1)*2\n end\n return wk + (s-f).abs\nend\nn, m = gets.chomp.split(/ /).map!{|x| x.to_i}\nfor i in 0..n-1\n s,f,t = gets.chomp.split(/ /).map!{|x| x.to_i}\n ret = t\n if s != f\n ret = calctime(m,s,f,t)\n end\n puts ret\nend\n"}, {"source_code": "\ninput=gets.split.map(&:to_i)\nn=input[0]\nm=input[1]-1\nn.times{\n input=gets.split.map(&:to_i)\n s=input[0]-1\n f=input[1]-1\n t=input[2]\n if s==f then\n puts t\n elsif ss then\n cnt+=1\n end\n puts cnt*2*m+f\n else\n tt=t%(2*m)\n cnt=t/(2*m)\n s,f=f,s\n if tt>m-f+m then\n cnt+=1\n end\n puts cnt*2*m+m+m-s\n end\n}\n"}, {"source_code": "def solve\n n, m = gets.strip.split(' ').map(&:to_i)\n m -= 1\n n.times do\n s, f, t = gets.strip.split(' ').map(&:to_i)\n r = nil\n s -= 1\n f -= 1\n if s == f\n r = t\n elsif s < f\n r = s\n while r < t\n r += 2 * m\n end\n r += (f-s).abs\n else\n r = 2 * m - s\n while r < t\n r += 2 * m\n end\n r += (f-s).abs\n end\n puts r\n end\nend\n\nsolve"}, {"source_code": "def solve(s, f, t, m)\n lapse = 2 * (m - 1)\n\n # if a person is already at his destination\n # then we just return the time when he start waiting\n return t if s == f\n\n if s < f\n ret = s - 1\n ret += ((t - ret) / lapse.to_f).ceil * lapse if ret < t\n # same as this\n # while ret < t\n # ret += lapse\n # end\n ret += f - s\n else\n ret = lapse - (s - 1)\n ret += ((t - ret) / lapse.to_f).ceil * lapse if ret < t\n # same as this\n # while ret < t\n # ret += lapse\n # end\n ret += s - f\n end\n ret\nend\n\nn, m = gets.strip.split(' ').map(&:to_i)\n\nn.times do\n s, f, t = gets.strip.split(' ').map(&:to_i)\n puts solve(s, f, t, m)\nend\n"}, {"source_code": "def solve(s, f, t, m)\n lapse = 2 * (m - 1)\n # if a person is already at his destination\n # then we just return the time when he start waiting\n return t if s == f\n\n if s < f\n ret = s - 1\n while ret < t\n ret += lapse\n end\n ret += f - s\n else\n ret = lapse - (s - 1)\n while ret < t\n ret += lapse\n end\n ret += s - f\n end\n ret\nend\n\nn, m = gets.strip.split(' ').map(&:to_i)\n\nn.times do\n s, f, t = gets.strip.split(' ').map(&:to_i)\n puts solve(s, f, t, m)\nend\n"}, {"source_code": "def read_next_line\n gets.split.map { |x| x.to_i }\nend\n\nn, m = read_next_line\nq = 2 * m\nz = m - 1\nk = 2 * z\nfor _ in 1..n\n s, f, t = read_next_line\n if s == f\n puts t\n next\n end\n r = t % k\n es = m - (z - r).abs\n d = r > z\n pt = es == s ? 0 : d ^ (es < s) ? (s - es).abs : (d ^= 1) ? q - (s + es) : (s + es) - 2\n pt += d ^ (f > s) ? (s -f).abs : d ? (s + f) - 2 : q - (s + f)\n puts t + pt\nend"}], "negative_code": [{"source_code": "# -*- coding: utf-8 -*-\n\ndef hoge(s,f,t,m)\n return 0 if s==f\n p=(m-1)*2\n base= (t/p)*p\n t -= base\n base + if s < f\n #up\n if t<=s-1\n f-1\n else\n p+f-1\n end\n else\n #down\n if t<=p-s+1\n p-f+1\n else\n p-f+1 + p \n end\n end\nend\n\nn,m=gets.split.map(&:to_i)\nn.times {\n s,f,t=gets.split.map(&:to_i)\n r = hoge(s,f,t,m)\n puts r\n}\n"}, {"source_code": "def calctime(m, s, f, t)\n if s < f\n wk = s - 1\n else\n wk = (m-1) + (m-s)\n end\n while true\n break if t <= wk\n wk += (m-1)*2\n end\n return wk + (s-f).abs\nend\nn, m = gets.chomp.split(/ /).map!{|x| x.to_i}\nfor i in 0..n-1\n ret = 0\n s,f,t = gets.chomp.split(/ /).map!{|x| x.to_i}\n if s != f\n ret = calctime(m,s,f,t)\n end\n puts ret\nend\n"}, {"source_code": "\ninput=gets.split.map(&:to_i)\nn=input[0]\nm=input[1]-1\nn.times{\n input=gets.split.map(&:to_i)\n s=input[0]-1\n f=input[1]-1\n t=input[2]\n if s==f then\n puts \n elsif ss then\n cnt+=1\n end\n puts cnt*2*m+f\n else\n tt=t%(2*m)\n cnt=t/(2*m)\n s,f=f,s\n if tt>m-f+m then\n cnt+=1\n end\n puts cnt*2*m+m+m-s\n end\n}\n"}, {"source_code": "\ninput=gets.split.map(&:to_i)\nn=input[0]\nm=input[1]-1\nn.times{\n input=gets.split.map(&:to_i)\n s=input[0]-1\n f=input[1]-1\n t=input[2]\n if s==f then\n puts 0\n elsif ss then\n cnt+=1\n end\n puts cnt*2*m+f\n else\n tt=t%(2*m)\n cnt=t/(2*m)\n s,f=f,s\n if tt>m-f+m then\n cnt+=1\n end\n puts cnt*2*m+m+m-s\n end\n}\n"}, {"source_code": "def running(ans, max, n, k, sum)\n limit = max / ans;\n ret = 0;\n 1.upto(limit) do |j|\n sum[j * ans + k] ||= 0\n sum[j * ans - 1] ||= 0\n ret += sum[j * ans + k] - sum[j * ans - 1]\n end\n return ret == n;\nend\n\ndef solve\n min = Float::INFINITY\n max = 0\n\n n, k = gets.strip.split.map(&:to_i)\n a = []\n sum = []\n\n gets.strip.split.each do |x|\n x = x.to_i\n a << x\n min = [x, min].min\n max = [x, max].max\n sum[x] ||= 0\n sum[x] += 1\n end\n\n min.upto(max+k) do |i|\n sum[i] ||= 0\n sum[i-1] ||= 0\n sum[i] += sum[i-1]\n end\n\n if min <= k+1\n puts min\n return\n end\n min.downto(k+1) do |i|\n if running(i, max, n, k, sum)\n puts i\n return\n end\n end\nend\n\nsolve"}], "src_uid": "b8321b0a3fc8c295182a4c2c8d2e9d01"} {"nl": {"description": "According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower. However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.Write a program that models the behavior of Ankh-Morpork residents.", "input_spec": "The first line contains single integer n (1 ≤ n ≤ 100 000) — the total number of snacks. The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n. ", "output_spec": "Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.", "sample_inputs": ["3\n3 1 2", "5\n4 5 1 2 3"], "sample_outputs": ["3\n \n2 1", "5 4\n \n \n3 2 1"], "notes": "NoteIn the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before."}, "positive_code": [{"source_code": "#!/usr/bin/ruby\nl,*a=$<.read.split.map &:to_i\nh={}\na.each{|i|\n\th[i]=1\n\tr=[]\n\twhile h[l]\n\t\tr<0\n\t\tif state[i]==2\n\t\t\tstart-=1\n\t\telse\n\t\t\tif state[i]==1\n\t\t\t\tprint \" #{i}\"\n\t\t\t\tstate[i]=2#就是说在state 2下,i糖果已经被放好!\n\t\t\telse\n\t\t\t\tbreak\n\t\t\tend\n\t\tend\n\t\ti-=1\n\tend\n\tputs\nend\n"}, {"source_code": "#!/usr/bin/env ruby\n\nn = gets.chomp.to_i\nmax_size = n\nsnack_days = gets.split.map(&:to_i)\n\nsnack_back = {}\n\nsnack_days.each do |new_snack|\n if new_snack == max_size\n\tprint new_snack\n\tmax_size -= 1\n\twhile snack_back[max_size]\n\t print \" #{max_size}\"\n\t max_size -= 1\n\tend\n else\n\tsnack_back[new_snack] = true\n end\n print \"\\n\"\nend\n"}], "negative_code": [], "src_uid": "3d648acee2abbf834f865b582aa9b7bc"} {"nl": {"description": "You are given an array $$$a$$$ consisting of $$$n$$$ positive integers. Find a non-empty subset of its elements such that their sum is even (i.e. divisible by $$$2$$$) or determine that there is no such subset.Both the given array and required subset may contain equal values.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 100$$$), number of test cases to solve. Descriptions of $$$t$$$ test cases follow. A description of each test case consists of two lines. The first line contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 100$$$), length of array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\leq a_i \\leq 100$$$), elements of $$$a$$$. The given array $$$a$$$ can contain equal values (duplicates).", "output_spec": "For each test case output $$$-1$$$ if there is no such subset of elements. Otherwise output positive integer $$$k$$$, number of elements in the required subset. Then output $$$k$$$ distinct integers ($$$1 \\leq p_i \\leq n$$$), indexes of the chosen elements. If there are multiple solutions output any of them.", "sample_inputs": ["3\n3\n1 4 3\n1\n15\n2\n3 5"], "sample_outputs": ["1\n2\n-1\n2\n1 2"], "notes": "NoteThere are three test cases in the example.In the first test case, you can choose the subset consisting of only the second element. Its sum is $$$4$$$ and it is even.In the second test case, there is only one non-empty subset of elements consisting of the first element, however sum in it is odd, so there is no solution.In the third test case, the subset consisting of all array's elements has even sum."}, "positive_code": [{"source_code": "#!usr/bin/env ruby\n\n# https://codeforces.com/problemset/problem/1323/A\n\ncount = gets.to_i\n\nanswers = []\n\ncount.times do\n n = gets.to_i\n a = gets.split(' ').map(&:to_i)\n \n odd_index = nil\n \n a.each_with_index do |num, index|\n if num % 2 == 0\n answers << 1 << index + 1\n break\n elsif n == 1\n answers << -1\n break\n else\n if odd_index.nil?\n odd_index = index + 1\n else\n answers << 2 << \"#{odd_index} #{index+1}\"\n break\n end \n end\n end\nend\n\nanswers.each { |answer| puts answer }\n"}, {"source_code": "t = gets.to_i\nt.times do \n n = gets.to_i\n a = gets.split.map(&:to_i)\n flg = false\n n.times do |i|\n if a[i].even?\n p 1\n p i+1\n flg = true\n break\n end\n end\n next if flg \n if n == 1\n p -1\n else\n p 2\n puts \"1 2\"\n end\nend"}, {"source_code": "#! /usr/bin/env ruby\n# encoding: UTF-8\n\ndef solve\nn = gets.to_i\nxs = gets.split.map{|x| x.to_i }\nevens = n.times.select {|i| xs[i] % 2 == 0 }\nodds = n.times.select {|i| xs[i] % 2 == 1 }\nif evens.size > 0 then\tputs 1; puts evens.first + 1; return; end\nif odds.size > 1 then puts 2; print(odds[0]+1, ' ', odds[1]+1); puts; return; end\nputs -1\nend\n\nt = gets.to_i\nt.times {solve}\n"}, {"source_code": "T = gets.to_i\n\nT.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n\n idx = a.index { |x| x % 2 == 0 }\n\n if idx\n puts 1\n puts idx + 1\n else\n odds = []\n\n n.times do |i|\n odds << i + 1 if a[i].odd?\n end\n\n if odds.size >= 2\n ans = odds.take(2)\n puts ans.size\n puts ans.join(' ')\n else\n puts -1\n end\n end\nend\n"}, {"source_code": "# coding: utf-8\n\nt = gets.to_i\nt.times {\n n = gets.to_i\n ar_odds, ar_evens = [], []\n gets.split.each_with_index { |i, index|\n i = i.to_i\n index += 1 # 1-based\n if i.odd?\n ar_odds << index\n else\n ar_evens << index\n end\n }\n\n if ar_evens.length > 0\n puts 1\n puts ar_evens.first\n else\n if ar_odds.length == 1\n puts -1\n else\n puts 2\n puts ar_odds[0...2].join(' ')\n end\n end\n\n}"}, {"source_code": "t = gets.to_i\nt.times do\n n = gets.to_i\n a = gets.split.map(&:to_i)\n even = []\n odd = []\n a.each_with_index do |v, i|\n if v % 2 == 0\n even.push(i + 1)\n else\n odd.push(i + 1)\n end\n end\n if even.length >= 1\n puts 1\n puts even[0]\n elsif odd.length >= 2\n puts 2\n puts \"#{odd[0]} #{odd[1]}\"\n else\n puts -1\n end\nend\n"}], "negative_code": [{"source_code": "#!usr/bin/env ruby\n\n# https://codeforces.com/problemset/problem/1323/A\n\ncount = gets.to_i\n\nanswers = []\n\ncount.times do\n n = gets.to_i\n a = gets.split(' ').map(&:to_i)\n \n odd_index = nil\n \n a.each_with_index do |num, index|\n if n == 1\n answers << -1\n break\n end\n if num % 2 == 0\n answers << 1 << index + 1\n break\n else\n if odd_index.nil?\n odd_index = index + 1\n else\n answers << 2 << \"#{odd_index} #{index+1}\"\n break\n end \n end\n end\nend\n\nanswers.each { |answer| puts answer }\n"}, {"source_code": "t = gets.to_i\nt.times do \n n = gets.to_i\n a = gets.split.map(&:to_i)\n flg = false\n n.times do |i|\n if a[i].even?\n p 1\n p i+1\n flg = true\n end\n end\n next if flg \n if n == 1\n p -1\n else\n p 2\n puts \"1 2\"\n end\nend"}], "src_uid": "3fe51d644621962fe41c32a2d90c7f94"} {"nl": {"description": "Alice is playing with some stones.Now there are three numbered heaps of stones. The first of them contains $$$a$$$ stones, the second of them contains $$$b$$$ stones and the third of them contains $$$c$$$ stones.Each time she can do one of two operations: take one stone from the first heap and two stones from the second heap (this operation can be done only if the first heap contains at least one stone and the second heap contains at least two stones); take one stone from the second heap and two stones from the third heap (this operation can be done only if the second heap contains at least one stone and the third heap contains at least two stones). She wants to get the maximum number of stones, but she doesn't know what to do. Initially, she has $$$0$$$ stones. Can you help her?", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\leq t \\leq 100$$$)  — the number of test cases. Next $$$t$$$ lines describe test cases in the following format: Line contains three non-negative integers $$$a$$$, $$$b$$$ and $$$c$$$, separated by spaces ($$$0 \\leq a,b,c \\leq 100$$$) — the number of stones in the first, the second and the third heap, respectively. In hacks it is allowed to use only one test case in the input, so $$$t = 1$$$ should be satisfied.", "output_spec": "Print $$$t$$$ lines, the answers to the test cases in the same order as in the input. The answer to the test case is the integer  — the maximum possible number of stones that Alice can take after making some operations. ", "sample_inputs": ["3\n3 4 5\n1 0 5\n5 3 2"], "sample_outputs": ["9\n0\n6"], "notes": "NoteFor the first test case in the first test, Alice can take two stones from the second heap and four stones from the third heap, making the second operation two times. Then she can take one stone from the first heap and two stones from the second heap, making the first operation one time. The summary number of stones, that Alice will take is $$$9$$$. It is impossible to make some operations to take more than $$$9$$$ stones, so the answer is $$$9$$$."}, "positive_code": [{"source_code": "T = gets.to_i\nT.times do\n a,b,c = gets.split.map(&:to_i)\n ans = 0\n (a+1).times do |i|\n x = [a,b/2,i].min\n d,e,f = a-x, b-x*2, c\n sum = x*3\n while e > 0 && f > 1\n sum += 3\n e -= 1\n f -= 2\n end\n ans = [ans, sum].max\n end\n p ans\nend"}, {"source_code": "gets.to_i.times do\n a, b, c = gets.split.map(&:to_i)\n\n ans1 = 0\n t = [b, c/2].min\n b -= t\n c -= t * 2\n ans1 += t * 3\n t = [a, b/2].min\n a -= t\n b -= t * 2\n ans1 += t * 3\n\n ans2 = 0\n t = [a, b/2].min\n a -= t\n b -= t * 2\n ans2 += t * 3\n t = [b, c/2].min\n b -= t\n c -= t * 2\n ans2 += t * 3\n\n puts [ans1, ans2].max\nend\n"}, {"source_code": "vals = STDIN.readlines\nn = vals[0].to_i\nstones = []\nn.times { |i|\n s = vals[i + 1].split(' ').map(&:to_i)\n stones.push(s)\n}\nstones.each { |stone|\n ret = 0\n while stone[2] > 1 && stone[1] >= 1 \n stone[2] = stone[2] - 2\n stone[1] = stone[1] - 1\n ret += 3\n end\n while stone[1] > 1 && stone[0] >= 1\n stone[1] = stone[1] - 2\n stone[0] = stone[0] - 1\n ret += 3\n end\n p ret\n}\n"}, {"source_code": "def calcs(x,y,ax,ay,bx,by) ((ax-x)*(by-y)-(bx-x)*(ay-y))/2.0 end\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaizyo(n)(n < 2)? 1 : (2..n).inject(:*) end\n\ndef scount(a) b=na(a.max+1);a.each{|n|b[n]+=1};return b end\n\ndef na(n=0,d=0) Array.new(n,d)end\n\ndef na2(n=0,m=0,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n=0,m=0,l=0,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef bit(n) n.to_s(2).split(\"\").map(&:to_i) end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\nn = inp[0]\nans = 0\nn.times do |i|\n t = inp\n f = [t[2]/2,t[1]].min\n t[1] -= f\n s = [t[0],t[1]/2].min\n p (f+s)*3\nend"}, {"source_code": "MOD=10**9+7\ncnt=0; sum=0; prev=nil; can=true; h=Hash.new(0)\ndef gs() gets.chomp end\ndef gi() gets.to_i end\ndef gsmi() gets.chomp.split.map(&:to_i) end\n\ndef desc(ar) ar.sort!{|x,y|y<=>x} end\ndef min(a,b) a<=b ? a : b end\ndef max(a,b) a>=b ? a : b end\ndef sum(ar) ar.inject(:+) end\n\n#def bs(ar,eq,n) en=(eq==1 ? :>= : :>); ar.bsearch_index{|x|x.send(en,n)} end\ndef C(a,b) b==0||a==b ? 1 : (b=a-b if a/2 1 && stone[1] > 1 \n stone[2] = stone[2] - 2\n stone[1] = stone[1] - 1\n ret += 3\n end\n while stone[1] > 1 && stone[0] > 1\n stone[1] = stone[1] - 2\n stone[0] = stone[0] - 1\n ret += 3\n end\n p ret\n}\n"}], "src_uid": "14fccd50d5dfb557dd53f2896ed844c3"} {"nl": {"description": "A number is called 2050-number if it is $$$2050$$$, $$$20500$$$, ..., ($$$2050 \\cdot 10^k$$$ for integer $$$k \\ge 0$$$).Given a number $$$n$$$, you are asked to represent $$$n$$$ as the sum of some (not necessarily distinct) 2050-numbers. Compute the minimum number of 2050-numbers required for that.", "input_spec": "The first line contains a single integer $$$T$$$ ($$$1\\le T\\leq 1\\,000$$$) denoting the number of test cases. The only line of each test case contains a single integer $$$n$$$ ($$$1\\le n\\le 10^{18}$$$) denoting the number to be represented.", "output_spec": "For each test case, output the minimum number of 2050-numbers in one line. If $$$n$$$ cannot be represented as the sum of 2050-numbers, output $$$-1$$$ instead. ", "sample_inputs": ["6\n205\n2050\n4100\n20500\n22550\n25308639900"], "sample_outputs": ["-1\n1\n2\n1\n2\n36"], "notes": "NoteIn the third case, $$$4100 = 2050 + 2050$$$.In the fifth case, $$$22550 = 20500 + 2050$$$."}, "positive_code": [{"source_code": "[*$<][1..-1].map{|n|n=n.to_i;p n%2050>0?-1:(n/2050).to_s.chars.map(&:to_i).sum}"}, {"source_code": "[*$<][1..-1].map{|n|n=n.to_i;p n%2050>0?-1:(n/2050).to_s.chars.map(&:to_i).inject(:+)}"}, {"source_code": "test_cases = gets.chomp.to_i\r\nfor i in 1..test_cases do\r\n n = gets.chomp.to_i\r\n if (n % 2050 == 0)\r\n n /= 2050\r\n s = n.to_s\r\n ans = 0\r\n for j in 0..s.size - 1 do\r\n ans += s[j].ord - '0'.ord\r\n end\r\n puts ans\r\n else\r\n puts -1\r\n end\r\nend"}, {"source_code": "def solution(n)\n min_number = 0\n k = 0\n\n while twenty_fifty_number(k) <= n\n k += 1\n end\n\n while k >= 0\n if n < twenty_fifty_number(k)\n k -= 1\n else\n n = n - twenty_fifty_number(k) \n min_number += 1\n end\n end\n\n if n == 0 \n min_number\n else\n - 1\n end\nend\n\ndef twenty_fifty_number(k)\n 2050 * (10 ** k)\nend\n\ntest_cases = gets.to_i\n\ntest_cases.times do |test_case|\n n = gets.to_i\n res = solution(n)\n\n puts res\nend\n"}, {"source_code": "t = gets.to_s.to_i\n1.upto(t) do |casenum|\n n = gets.to_s.to_i\n if n % 2050 != 0\n printf(\"-1\\n\")\n next\n end\n\n n /= 2050\n res = 0\n while n > 0\n res += n % 10\n n /= 10\n end\n\n printf(\"%d\\n\", res)\nend\n"}], "negative_code": [], "src_uid": "f3e413954c9c02520fd25bd2cba4747e"} {"nl": {"description": "George is a cat, so he really likes to play. Most of all he likes to play with his array of positive integers b. During the game, George modifies the array by using special changes. Let's mark George's current array as b1, b2, ..., b|b| (record |b| denotes the current length of the array). Then one change is a sequence of actions: Choose two distinct indexes i and j (1 ≤ i, j ≤ |b|; i ≠ j), such that bi ≥ bj. Get number v = concat(bi, bj), where concat(x, y) is a number obtained by adding number y to the end of the decimal record of number x. For example, concat(500, 10) = 50010, concat(2, 2) = 22. Add number v to the end of the array. The length of the array will increase by one. Remove from the array numbers with indexes i and j. The length of the array will decrease by two, and elements of the array will become re-numbered from 1 to current length of the array. George played for a long time with his array b and received from array b an array consisting of exactly one number p. Now George wants to know: what is the maximum number of elements array b could contain originally? Help him find this number. Note that originally the array could contain only positive integers.", "input_spec": "The first line of the input contains a single integer p (1 ≤ p < 10100000). It is guaranteed that number p doesn't contain any leading zeroes.", "output_spec": "Print an integer — the maximum number of elements array b could contain originally.", "sample_inputs": ["9555", "10000000005", "800101", "45", "1000000000000001223300003342220044555", "19992000", "310200"], "sample_outputs": ["4", "2", "3", "1", "17", "1", "2"], "notes": "NoteLet's consider the test examples: Originally array b can be equal to {5, 9, 5, 5}. The sequence of George's changes could have been: {5, 9, 5, 5} → {5, 5, 95} → {95, 55} → {9555}. Originally array b could be equal to {1000000000, 5}. Please note that the array b cannot contain zeros. Originally array b could be equal to {800, 10, 1}. Originally array b could be equal to {45}. It cannot be equal to {4, 5}, because George can get only array {54} from this array in one operation. Note that the numbers can be very large."}, "positive_code": [{"source_code": "s = ARGF.read().chomp!\nl, r, a = 0, s.size-1, 0\nwhile l <= r\n a += 1\n m = r\n m -= 1 while l=s[m])) ? r = m-1 : break\nend\nputs a\n"}], "negative_code": [{"source_code": "s = ARGF.read().chomp!\nl, r = 0, s.size-1\nans = 0\nwhile l < r\n ans += 1\n m = r\n m -= 1 while l=s[m])) then r = m-1\n else break end\nend\nputs ans\n"}], "src_uid": "4e00fe693a636316d478978abf21d976"} {"nl": {"description": "Polycarpus has n markers and m marker caps. Each marker is described by two numbers: xi is the color and yi is the diameter. Correspondingly, each cap is described by two numbers: aj is the color and bj is the diameter. Cap (aj, bj) can close marker (xi, yi) only if their diameters match, that is, bj = yi. Besides, a marker is considered to be beautifully closed, if the cap color and the marker color match, that is, aj = xi.Find the way to close the maximum number of markers. If there are several such ways, then choose the one that has the maximum number of beautifully closed markers.", "input_spec": "The first input line contains two space-separated integers n and m (1 ≤ n, m ≤ 105) — the number of markers and the number of caps, correspondingly. Next n lines describe the markers. The i-th line contains two space-separated integers xi, yi (1 ≤ xi, yi ≤ 1000) — the i-th marker's color and diameter, correspondingly. Next m lines describe the caps. The j-th line contains two space-separated integers aj, bj (1 ≤ aj, bj ≤ 1000) — the color and diameter of the j-th cap, correspondingly.", "output_spec": "Print two space-separated integers u, v, where u is the number of closed markers and v is the number of beautifully closed markers in the sought optimal way. Remember that you have to find the way to close the maximum number of markers, and if there are several such ways, you should choose the one where the number of beautifully closed markers is maximum.", "sample_inputs": ["3 4\n1 2\n3 4\n2 4\n5 4\n2 4\n1 1\n1 2", "2 2\n1 2\n2 1\n3 4\n5 1"], "sample_outputs": ["3 2", "1 0"], "notes": "NoteIn the first test sample the first marker should be closed by the fourth cap, the second marker should be closed by the first cap and the third marker should be closed by the second cap. Thus, three markers will be closed, and two of them will be beautifully closed — the first and the third markers."}, "positive_code": [{"source_code": "I = ->{gets.split.map(&:to_i)}\nn, m = I[]\ndef r(n)\n h = {}\n n.times{\n x, y = I[]\n h[y] = h[y] || Hash.new(0)\n h[y][x] += 1\n }\n h\nend\na, b, u, v = r(n), r(m), 0, 0\na.merge(b){|_, c, d|\n u += [c, d].map{|_| _.values.inject(&:+)}.min\n c.merge(d){|_, e, f|\n v += [e, f].min\n }\n}\nputs \"#{u} #{v}\"\n"}, {"source_code": "n,m = gets.split.map(&:to_i)\na = Hash.new\na.default = 0 \nb = Hash.new\nb.default = 0\nc = Hash.new\nc.default = 0\nd = Hash.new\nd.default = 0\n\nn.times{\n tmp = gets.split.map(&:to_i)\n a[tmp[1]] += 1\n b[tmp] += 1\n}\nm.times{\n tmp = gets.split.map(&:to_i)\n c[tmp[1]] += 1\n d[tmp] += 1\n}\nu,v = 0,0\n\na.each{|x,y|\n u += [y, c[x]].min\n}\n\nb.each{|x,y|\n v += [y, d[x]].min\n}\n\np u\np v\n"}, {"source_code": "n,m = STDIN.gets.chomp.split(/ /).map(&:to_i)\nmarkers = n.times.map { STDIN.gets.chomp.split(/ /).map(&:to_i) }\ncaps_ = m.times.map { STDIN.gets.chomp.split(/ /).map(&:to_i) }\n\ncaps = {}\ncaps_.each do |i|\n caps[i[1]] ||= {}\n caps[i[1]][i[0]] ||= []\n caps[i[1]][i[0]] << true\nend\n\nbeautiful = 0\nclosed = 0\ndone = []\n\n\nmarkers.each_with_index do |m,_|\n if (h = caps[m[1]]) && (a = h[m[0]]) && a.shift\n done << _\n beautiful += 1\n closed += 1\n h.delete m[0] if a.empty?\n end\nend\n\ndone.each{|i| markers[i] = nil }\n\nmarkers.each_with_index do |m,_|\n next unless m\n if (h = caps[m[1]]) && (a = h.first) && a[1].shift\n closed += 1\n h.delete a[0] if a[1].empty?\n end\nend\n\nputs \"#{closed} #{beautiful}\"\n"}, {"source_code": "n,m=gets.split.map(&:to_i)\nmarkers = {}\nn.times{\n color,diameter = gets.split.map(&:to_i)\n markers[diameter] ||= {}\n markers[diameter][color] ||= 0\n markers[diameter][color] += 1\n}\n\nunused_caps = {}\nbeautiful_closed = 0\n\nm.times do\n color,diameter = gets.split.map(&:to_i)\n if markers[diameter] and markers[diameter][color] and markers[diameter][color] > 0\n beautiful_closed += 1\n markers[diameter][color] -= 1\n else\n unused_caps[diameter] ||= 0\n unused_caps[diameter] += 1\n end\nend\n\nclosed = beautiful_closed\nopen_markers = {}\nmarkers.each{|diameter,hsh|\n open_markers[diameter]=hsh.inject(0){|s,(k,v)| s+v}\n}\nunused_caps.each{|diameter, number|\n closed += [open_markers[diameter] || 0, unused_caps[diameter] || 0].min\n}\n\nputs \"#{closed} #{beautiful_closed}\""}, {"source_code": "a = STDIN.gets.split(\" \").map(&:to_i)\narr = (a[0]+a[1]).times.map{STDIN.gets.split(\" \").map(&:to_i)}\n\nc1h=arr[a[0]...(a[0]+a[1])].inject(Hash.new(0)) {|h, e| h.tap {|h| h[e] += 1} }\nm1h=arr[0...a[0]].inject(Hash.new(0)) {|h, e| h.tap {|h| h[e] += 1} }\nbclosed=0\nc1h.each do |k,v|\n next unless m1h[k]\n bclosed+=m1h[k] >= v ? v : m1h[k]\nend\nc2h=arr[a[0]...(a[0]+a[1])].map{|v| v[1]}.inject(Hash.new(0)) {|h, e| h.tap {|h| h[e] += 1} }\nm2h=arr[0...a[0]].map{|v| v[1]}.inject(Hash.new(0)) {|h, e| h.tap {|h| h[e] += 1} }\nclosed=0\nc2h.each do |k,v|\n next unless m2h[k]\n closed+=m2h[k] >= v ? v : m2h[k]\nend\n\nputs \"#{closed} #{bclosed}\""}, {"source_code": "def read_array(size)\n a = []\n for _ in 1..size\n a << read_next_line\n end\n group(a)\nend\n\ndef read_next_line\n gets.split.map { |x| x.to_i }\nend\n\ndef group(a)\n Hash[a.inject(Hash.new { |h, e| h[e] = [] }) { |h, e| h[e[1]] << e[0]; h }.map { |k, v|\n [k, [v.inject(Hash.new(0)) { |h, e| h[e] += 1; h }, v.size]]\n }]\nend\n\nn, m = read_next_line\nmarkers = read_array(n)\ncaps = read_array(m)\n\nclosed = b_closed = 0\nmarkers.each { |d, c|\n if caps.has_key? d\n closed += [c[1], caps[d][1]].min\n c[0].each { |k, v|\n if caps[d][0].has_key? k\n b_closed += [v, caps[d][0][k]].min\n end\n }\n end\n}\nputs \"#{closed} #{b_closed}\""}, {"source_code": "require 'scanf'\nlines = STDIN.read.split(\"\\n\")\nn, m = lines[0].scanf(\"%d%d\")\nmarkers = lines[1..n].map { |line|\n\tsplit = line.split(' ')\n\t[Integer(split[0]), Integer(split[1])]\n}\ncaps = lines[n+1..n+m].map { |line|\n\tsplit = line.split(' ')\n\t[Integer(split[0]), Integer(split[1])]\n}\n\nmarkers = markers.sort { |a, b| a[1] > b[1] ? 1 : a[1] < b[1] ? -1 : a[0] - b[0] }\ncaps = caps.sort { |a, b| a[1] > b[1] ? 1 : a[1] < b[1] ? -1 : a[0] - b[0] }\n\nclosed = 0\nbeautifully_closed = 0\ncurrent_size = -1\ni = 0 # marker index\nj = 0 # cap index\nwhile i < markers.length && j < caps.length\n\tunclosed_markers = 0\n\tunused_caps = 0\n\tcurrent_size = markers[i][1] # or = caps[j][1]\n\twhile i < markers.length && j < caps.length && markers[i][1] == current_size && caps[j][1] == current_size\n\t\twhile i < markers.length && j < caps.length && markers[i][1] == current_size && caps[j][1] == current_size && markers[i][0] == caps[j][0]\n\t\t\tclosed += 1\n\t\t\tbeautifully_closed += 1\n\t\t\ti += 1\n\t\t\tj += 1\n\t\tend\n\t\twhile i < markers.length && j < caps.length && markers[i][1] == current_size && caps[j][1] == current_size && markers[i][0] < caps[j][0]\n\t\t\tunclosed_markers += 1\n\t\t\ti += 1\n\t\tend\n\t\twhile i < markers.length && j < caps.length && markers[i][1] == current_size && caps[j][1] == current_size && caps[j][0] < markers[i][0]\n\t\t\tunused_caps += 1\n\t\t\tj += 1\n\t\tend\n\t\tif j >= caps.length || caps[j][1] != current_size\n\t\t\twhile i < markers.length && markers[i][1] == current_size\n\t\t\t\tunclosed_markers += 1\n\t\t\t\ti += 1\n\t\t\tend\n\t\tend\n\t\tif i >= markers.length || markers[i][1] != current_size\n\t\t\twhile j < caps.length && caps[j][1] == current_size\n\t\t\t\tunused_caps += 1\n\t\t\t\tj += 1\n\t\t\tend\n\t\tend\n\tend\n\tclosed += unclosed_markers < unused_caps ? unclosed_markers : unused_caps\n\twhile i < markers.length && j < caps.length && markers[i][1] < caps[j][1]\n\t\ti += 1\n\tend\n\twhile i < markers.length && j < caps.length && caps[j][1] < markers[i][1]\n\t\tj += 1\n\tend\nend\n\nputs \"#{closed} #{beautifully_closed}\"\n"}, {"source_code": "require 'scanf'\nlines = STDIN.read.split(\"\\n\")\nn, m = lines[0].scanf(\"%d%d\")\nmarkers = lines[1..n].map { |line|\n\tsplit = line.split(' ')\n\t[Integer(split[0]), Integer(split[1])]\n}\ncups = lines[n+1..n+m].map { |line|\n\tsplit = line.split(' ')\n\t[Integer(split[0]), Integer(split[1])]\n}\n\nmarkers = markers.sort { |a, b| a[1] > b[1] ? 1 : a[1] < b[1] ? -1 : a[0] - b[0] }\ncups = cups.sort { |a, b| a[1] > b[1] ? 1 : a[1] < b[1] ? -1 : a[0] - b[0] }\n\nclosed = 0\nbeautifully_closed = 0\n\nmarkers_map = {}\nmarkers.each do |marker|\n\tmarkers_map[marker[1]] = {-1 => 0}unless markers_map.key?(marker[1])\n\tmarkers_map[marker[1]][marker[0]] = 0 unless markers_map[marker[1]].key?(marker[0])\n\tmarkers_map[marker[1]][marker[0]] += 1\n\tmarkers_map[marker[1]][-1] += 1\nend\n\ni = 0\nwhile i < cups.length\n\tif markers_map.key?(cups[i][1])\n\t\tif markers_map[cups[i][1]].key?(cups[i][0]) && markers_map[cups[i][1]][cups[i][0]] > 0\n\t\t\tmarkers_map[cups[i][1]][cups[i][0]] -= 1\n\t\t\tmarkers_map[cups[i][1]][-1] -= 1\n\t\t\tbeautifully_closed += 1\n\t\t\tclosed += 1\n\t\t\tcups.delete_at(i)\n\t\t\tnext\n\t\tend\n\tend\n\ti += 1\nend\n\ni = 0\nwhile i < cups.length\n\tif markers_map.key?(cups[i][1])\n\t\tif markers_map[cups[i][1]][-1] > 0\n\t\t\tmarkers_map[cups[i][1]][-1] -= 1\n\t\t\tclosed += 1\n\t\tend\n\tend\n\ti += 1\nend\n\nputs \"#{closed} #{beautifully_closed}\"\n"}, {"source_code": "class Array\n\tdef my_slice_when\n\t\tarr_groups = []\n\t\tarr = [self[0]]\n\t\ti = 1\n\t\twhile i < self.length\n\t\t\tif yield(self[i - 1], self[i])\n\t\t\t\tarr_groups.push(arr)\n\t\t\t\tarr = [self[i]]\n\t\t\telse\n\t\t\t\tarr.push(self[i])\n\t\t\tend\n\t\t\ti += 1\n\t\tend\n\t\tarr_groups.push(arr)\n\t\tarr_groups\n\tend\nend\n\nrequire 'scanf'\nlines = STDIN.read.split(\"\\n\")\nn, m = lines[0].scanf(\"%d%d\")\nmarkers = lines[1..n].map { |line|\n\tsplit = line.split(' ')\n\t[Integer(split[0]), Integer(split[1])]\n}\ncaps = lines[n+1..n+m].map { |line|\n\tsplit = line.split(' ')\n\t[Integer(split[0]), Integer(split[1])]\n}\n\nmarkers = markers.sort { |a, b| a[1] > b[1] ? 1 : a[1] < b[1] ? -1 : a[0] - b[0] }\ncaps = caps.sort { |a, b| a[1] > b[1] ? 1 : a[1] < b[1] ? -1 : a[0] - b[0] }\n\nmarkers_by_size = markers.my_slice_when { |a, b| a[1] != b[1] }\ncaps_by_size = caps.my_slice_when { |a, b| a[1] != b[1] }\n\nclosed = 0\nbeautifully_closed = 0\ni = 0;\nmarkers_by_size.each do |markers_size_group|\n\tsize = markers_size_group[0][1]\n\twhile i < caps_by_size.length && caps_by_size[i][0][1] < size\n\t\ti += 1\n\tend\n\tbreak if i >= caps_by_size.length\n\tnext if caps_by_size[i][0][1] > size\n\tclosed += markers_size_group.length < caps_by_size[i].length ? markers_size_group.length : caps_by_size[i].length\n\t\n\tmarkers_by_color = markers_size_group.my_slice_when { |a, b| a[0] != b[0] }\n\tcaps_by_color = caps_by_size[i].my_slice_when { |a, b| a[0] != b[0] }\t\n\tj = 0;\n\tmarkers_by_color.each do |markers_color_group|\n\t\tcolor = markers_color_group[0][0]\n\t\twhile j < caps_by_color.length && caps_by_color[j][0][0] < color\n\t\t\tj += 1\n\t\tend\n\t\tbreak if j >= caps_by_color.length\n\t\tnext if caps_by_color[j][0][0] > color\n\t\tbeautifully_closed += markers_color_group.length < caps_by_color[j].length ? markers_color_group.length : caps_by_color[j].length\n\tend\nend\n\nputs \"#{closed} #{beautifully_closed}\"\n"}, {"source_code": "require 'scanf'\nlines = STDIN.read.split(\"\\n\")\nn, m = lines[0].scanf(\"%d%d\")\nmarkers = lines[1..n].map { |line|\n\tsplit = line.split(' ')\n\t[Integer(split[0]), Integer(split[1])]\n}\ncups = lines[n+1..n+m].map { |line|\n\tsplit = line.split(' ')\n\t[Integer(split[0]), Integer(split[1])]\n}\n\nclosed = 0\nbeautifully_closed = 0\n\nmarkers_map = {}\nmarkers.each do |marker|\n\tmarkers_map[marker[1]] = {-1 => 0}unless markers_map.key?(marker[1])\n\tmarkers_map[marker[1]][marker[0]] = 0 unless markers_map[marker[1]].key?(marker[0])\n\tmarkers_map[marker[1]][marker[0]] += 1\n\tmarkers_map[marker[1]][-1] += 1\nend\n\ni = 0\nwhile i < cups.length\n\tif markers_map.key?(cups[i][1])\n\t\tif markers_map[cups[i][1]].key?(cups[i][0]) && markers_map[cups[i][1]][cups[i][0]] > 0\n\t\t\tmarkers_map[cups[i][1]][cups[i][0]] -= 1\n\t\t\tmarkers_map[cups[i][1]][-1] -= 1\n\t\t\tbeautifully_closed += 1\n\t\t\tclosed += 1\n\t\t\tcups.delete_at(i)\n\t\t\tnext\n\t\tend\n\tend\n\ti += 1\nend\n\ni = 0\nwhile i < cups.length\n\tif markers_map.key?(cups[i][1])\n\t\tif markers_map[cups[i][1]][-1] > 0\n\t\t\tmarkers_map[cups[i][1]][-1] -= 1\n\t\t\tclosed += 1\n\t\tend\n\tend\n\ti += 1\nend\n\nputs \"#{closed} #{beautifully_closed}\"\n"}], "negative_code": [{"source_code": "I = ->{gets.split.map(&:to_i)}\nn, m = I[]\ndef r(n)\n h = {}\n n.times{\n x, y = I[]\n h[y] = h[y] || Hash.new(0)\n h[y][x] += 1\n }\n h\nend\na, b, u, v = r(n), r(m), 0, 0\na.merge(b){|_, c, d|\n u += [c.size, d.size].min\n c.merge(d){|_, e, f|\n v += [e, f].min\n }\n}\nputs \"#{u} #{v}\"\n"}, {"source_code": "n,m = STDIN.gets.chomp.split(/ /).map(&:to_i)\nmarkers = n.times.map { STDIN.gets.chomp.split(/ /).map(&:to_i) }\ncaps = m.times.map { STDIN.gets.chomp.split(/ /).map(&:to_i) }\n\nbeautiful = 0\nclosed = 0\n\nmarkers.each_with_index do |m,_|\n if i = caps.index(m)\n caps.delete_at i\n markers.delete_at _\n beautiful += 1\n closed += 1\n end\nend\n\nmarkers.each_with_index do |m,_|\n if i = caps.index{|c| m[1] == c[1] }\n caps.delete_at i\n markers.delete_at _\n closed += 1\n end\nend\n\nputs \"#{closed} #{beautiful}\"\n"}, {"source_code": "n,m = STDIN.gets.chomp.split(/ /).map(&:to_i)\nmarkers = n.times.map { STDIN.gets.chomp.split(/ /).map(&:to_i) }\ncaps = m.times.map { STDIN.gets.chomp.split(/ /).map(&:to_i) }\n\nbeautiful = 0\nclosed = 0\n\nmarkers.each do |(mc,md)|\n if i = caps.index{|(cc,cd)| mc == cc && md == cd }\n caps.delete_at i\n beautiful += 1\n closed += 1\n elsif i = caps.index{|(cc,cd)| md == cd }\n caps.delete_at i\n closed += 1\n end\nend\n\nputs \"#{closed} #{beautiful}\"\n"}, {"source_code": "n,m = STDIN.gets.chomp.split(/ /).map(&:to_i)\nmarkers = n.times.map { STDIN.gets.chomp.split(/ /).map(&:to_i) }\ncaps_ = m.times.map { STDIN.gets.chomp.split(/ /).map(&:to_i) }\n\ncaps = {}\ncaps_.each do |i|\n caps[i[1]] ||= {}\n caps[i[1]][i[0]] ||= []\n caps[i[1]][i[0]] << true\nend\n\nbeautiful = 0\nclosed = 0\ndone = []\n\nmarkers.each_with_index do |m,_|\n if (h = caps[m[1]]) && (a = h[m[0]]) && a.shift\n beautiful += 1\n closed += 1\n end\nend\n\n\nmarkers.each_with_index do |m,_|\n next unless m\n if (h = caps[m[1]]) && (a = h.first[1]) && a.shift\n closed += 1\n end\nend\n\nputs \"#{closed} #{beautiful}\""}, {"source_code": "n,m = STDIN.gets.chomp.split(/ /).map(&:to_i)\nmarkers = n.times.map { STDIN.gets.chomp.split(/ /).map(&:to_i) }\ncaps_ = m.times.map { STDIN.gets.chomp.split(/ /).map(&:to_i) }\n\ncaps = {}\ncaps_.each do |i|\n caps[i[1]] ||= {}\n caps[i[1]][i[0]] ||= []\n caps[i[1]][i[0]] << true\nend\n\nbeautiful = 0\nclosed = 0\ndone = []\n\nmarkers.each_with_index do |m,_|\n if (h = caps[m[1]]) && (a = h[m[0]]) && a.shift\n done << _\n beautiful += 1\n closed += 1\n end\nend\n\ndone.each{|i| markers[i] = nil }\n\nmarkers.each_with_index do |m,_|\n next unless m\n if (h = caps[m[1]]) && (a = h.first[1]) && a.shift\n closed += 1\n end\nend\n\nputs \"#{closed} #{beautiful}\"\n"}, {"source_code": "a = STDIN.gets.split(\" \").map(&:to_i)\narr = (a[0]+a[1]).times.map{STDIN.gets.split(\" \").map(&:to_i)}\nm=arr[0...a[0]]\nc=arr[a[0]...(a[0]+a[1])]\n\nclosed = 0\nbclosed = 0\nm.each do |pa|\n bclosed += c.count{|pair| pa == pair}\n arr1= c.map{|h| h[1]}.uniq\n closed += arr1.count{|val| val == pa[1]}\nend\nputs \"#{closed} #{bclosed}\""}, {"source_code": "a = STDIN.gets.split(\" \").map(&:to_i)\narr = (a[0]+a[1]).times.map{STDIN.gets.split(\" \").map(&:to_i)}\nm=arr[0...a[0]].uniq\nc=arr[a[0]...(a[0]+a[1])].uniq\n\nclosed = 0\nbclosed = 0\nm.each do |pa|\n bclosed += c.count{|pair| pa == pair}\nend\nm.map{|h| h[1]}.uniq.each do |v|\n closed += c.map{|h| h[1]}.count{|val| val == v}\nend\nputs \"#{closed} #{bclosed}\""}, {"source_code": "a = STDIN.gets.split(\" \").map(&:to_i)\narr = (a[0]+a[1]).times.map{STDIN.gets.split(\" \").map(&:to_i)}\np m=arr[0...a[0]].uniq\np c=arr[a[0]...(a[0]+a[1])].uniq\n\nclosed = 0\nbclosed = 0\nm.each do |pa|\n bclosed += c.count{|pair| pa == pair}\nend\nm.map{|h| h[1]}.uniq.each do |v|\n closed += c.map{|h| h[1]}.count{|val| val == v}\nend\nputs \"#{closed} #{bclosed}\""}, {"source_code": "class Array\n\tdef my_slice_when\n\t\tarr_groups = []\n\t\tarr = [self[0]]\n\t\ti = 1\n\t\twhile i < self.length\n\t\t\tif yield(self[i - 1], self[i])\n\t\t\t\tarr_groups.push(arr)\n\t\t\t\tarr = [self[i]]\n\t\t\telse\n\t\t\t\tarr.push(self[i])\n\t\t\tend\n\t\t\ti += 1\n\t\tend\n\t\tarr_groups.push(arr)\n\t\tarr_groups\n\tend\nend\n\nrequire 'scanf'\nlines = STDIN.read.split(\"\\n\")\nn, m = lines[0].scanf(\"%d%d\")\nmarkers = lines[1..n].map { |line|\n\tsplit = line.split(' ')\n\t[Integer(split[0]), Integer(split[1])]\n}\ncaps = lines[n+1..n+m].map { |line|\n\tsplit = line.split(' ')\n\t[Integer(split[0]), Integer(split[1])]\n}\n\nmarkers = markers.sort { |a, b| a[1] > b[1] ? 1 : a[1] < b[1] ? -1 : a[0] - b[0] }\ncaps = caps.sort { |a, b| a[1] > b[1] ? 1 : a[1] < b[1] ? -1 : a[0] - b[0] }\n\nmarkers_by_size = markers.my_slice_when { |a, b| a[1] != b[1] }\ncaps_by_size = caps.my_slice_when { |a, b| a[1] != b[1] }\n\nclosed = 0\nbeautifully_closed = 0\ni = 0;\nmarkers_by_size.each do |markers_size_group|\n\tsize = markers_size_group[0][1]\n\twhile i < caps_by_size.length && caps_by_size[i][0][1] < size\n\t\ti += 1\n\tend\n\tbreak if i >= caps_by_size.length || caps_by_size[i][0][1] > size\n\tclosed += markers_size_group.length < caps_by_size[i].length ? markers_size_group.length : caps_by_size[i].length\n\t\n\tmarkers_by_color = markers_size_group.my_slice_when { |a, b| a[0] != b[0] }\n\tcaps_by_color = caps_by_size[i].my_slice_when { |a, b| a[0] != b[0] }\t\n\tj = 0;\n\tmarkers_by_color.each do |markers_color_group|\n\t\tcolor = markers_color_group[0][0]\n\t\twhile j < caps_by_color.length && caps_by_color[j][0][0] < color\n\t\t\tj += 1\n\t\tend\n\t\tbreak if j >= caps_by_color.length || caps_by_color[j][0][0] > color\n\t\tbeautifully_closed += markers_color_group.length < caps_by_color[j].length ? markers_color_group.length : caps_by_color[j].length\n\tend\nend\n\nputs \"#{closed} #{beautifully_closed}\"\n"}], "src_uid": "3c9fb72577838f54b1670e84b4b60c61"} {"nl": {"description": "The name of one small but proud corporation consists of n lowercase English letters. The Corporation has decided to try rebranding — an active marketing strategy, that includes a set of measures to change either the brand (both for the company and the goods it produces) or its components: the name, the logo, the slogan. They decided to start with the name.For this purpose the corporation has consecutively hired m designers. Once a company hires the i-th designer, he immediately contributes to the creation of a new corporation name as follows: he takes the newest version of the name and replaces all the letters xi by yi, and all the letters yi by xi. This results in the new version. It is possible that some of these letters do no occur in the string. It may also happen that xi coincides with yi. The version of the name received after the work of the last designer becomes the new name of the corporation.Manager Arkady has recently got a job in this company, but is already soaked in the spirit of teamwork and is very worried about the success of the rebranding. Naturally, he can't wait to find out what is the new name the Corporation will receive.Satisfy Arkady's curiosity and tell him the final version of the name.", "input_spec": "The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the length of the initial name and the number of designers hired, respectively. The second line consists of n lowercase English letters and represents the original name of the corporation. Next m lines contain the descriptions of the designers' actions: the i-th of them contains two space-separated lowercase English letters xi and yi.", "output_spec": "Print the new name of the corporation.", "sample_inputs": ["6 1\npolice\np m", "11 6\nabacabadaba\na b\nb c\na d\ne g\nf a\nb b"], "sample_outputs": ["molice", "cdcbcdcfcdc"], "notes": "NoteIn the second sample the name of the corporation consecutively changes as follows: "}, "positive_code": [{"source_code": "A = ('a'..'z').to_a.join\nn, m = gets.split.map(&:to_i)\ns = gets.chomp\nds = m.times.map{gets.split}\na = A.dup\nds.each{|x, y| a.tr!(x + y, y + x)}\nputs s.tr(A, a)\n\n"}, {"source_code": "# http://codeforces.com/contest/591/problem/B\n# B. Rebranding\n\nhash = {}.tap do |h|\n ('a'..'z').each { |ch| h[ch] = ch }\nend\nn, m = gets.chomp.split.map(&:to_i)\nchars = gets.chomp.chars\n\nm.times do\n a, b = gets.split\n\n tmp = hash[a]\n hash[a] = hash[b]\n hash[b] = tmp\nend\ninvert = hash.invert\nputs chars.map { |ch| invert[ch] }.join\n"}, {"source_code": "A = ('a'..'z').to_a.join\nn, m = gets.split.map(&:to_i)\ns = gets.chomp\nds = m.times.map{gets.split}\na = A.dup\nds.each{|x, y| a.tr!(x + y, y + x)}\nputs s.tr(A, a)\n\n"}, {"source_code": "n = gets.split(' ')[1].to_i\ns = gets\nhash = {}\n\"a\".upto(\"z\") { |s| hash[s] = s }\nn.times do\n a = gets.split(' ')\n k1 = hash.key(a[0])\n k2 = hash.key(a[1])\n tmp = hash[k1]\n hash[k1] = hash[k2]\n hash[k2] = tmp\nend\n\nhash.each { |key, value|\n s.gsub!(key, value.upcase)\n}\n\nputs s.downcase\n\n"}, {"source_code": "A = ('a'..'z').to_a.join\nn, m = gets.split.map(&:to_i)\ns = gets.chomp\nds = m.times.map{gets.split}\na = A.dup\nds.each{|x, y| a.tr!(x + y, y + x)}\nputs s.tr(A, a)\n\n"}, {"source_code": "input = gets.chomp.split\nn = Integer(input[0])\nm = Integer(input[1])\nname = gets.chomp\ndesign = Array.new(m)\nfor i in 0...m\n design[i] = gets.chomp.split\nend\n\n#puts design\n\nreplace = Array.new(26) {|i| i }\nrev_replace = Array.new(26) {|i| i }\n\nfor i in 0...design.length\n x, y = design[i]\n if x != y\n x = x.ord - 97\n y = y.ord - 97\n a = rev_replace[x]\n b = rev_replace[y]\n replace[a] = y\n rev_replace[y] = a\n\n replace[b] = x\n rev_replace[x] = b\n end\nend\n\nfor i in 0...n\n print (replace[name[i].ord - 97] + 97).chr\nend\n\nputs"}, {"source_code": "A = ('a'..'z').to_a.join\nn, m = gets.split.map(&:to_i)\ns = gets.chomp\nds = m.times.map{gets.split}\na = A.dup\nds.each{|x, y| a.tr!(x + y, y + x)}\nputs s.tr(A, a)\n\n"}, {"source_code": "# http://codeforces.com/contest/591/problem/B\n# B. Rebranding\n\nhash = {}.tap do |h|\n ('a'..'z').each { |ch| h[ch] = ch }\nend\nn, m = gets.chomp.split.map(&:to_i)\nchars = gets.chomp.chars\n\nm.times do\n a, b = gets.split\n\n tmp = hash[a]\n hash[a] = hash[b]\n hash[b] = tmp\nend\ninvert = hash.invert\nputs chars.map { |ch| invert[ch] }.join"}, {"source_code": "A = ('a'..'z').to_a.join\nn, m = gets.split.map(&:to_i)\ns = gets.chomp\nds = m.times.map{gets.split}\na = A.dup\nds.each{|x, y| a.tr!(x + y, y + x)}\nputs s.tr(A, a)\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\ns = gets.chomp\nds = m.times.map{gets.split}\na = ('a'..'z').to_a\nds.each do |x, y|\n 26.times do |i|\n if a[i] == x\n a[i] = y\n elsif a[i] == y\n a[i] = x\n end\n end\nend\nn.times do |i|\n s[i] = a[s[i].ord - 'a'.ord]\nend\nputs s\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\nstr = gets.chomp\nstr1 = str.clone\nresult, uniq = [], []\nwhile str1.size > 0 do\n uniq << str1[0]\n str1.delete!(str1[0])\nend\nuniq_arr = Array.new(uniq.length){ Array.new }\ni = 0\nstr.each_char do |s|\n uniq.each_with_index do |u, index|\n if s == u\n uniq_arr[index] << i\n break\n end\n end\n i += 1\nend\n(1..m).each do |i|\n x, y = gets.split\n next if x == y\n i = 0\n uniq.each do |c|\n uniq[i] = y if c == x\n uniq[i] = x if c == y\n i += 1\n end\nend\nuniq.each_with_index do |u, index|\n uniq_arr[index].each do |i|\n result[i] = u\n end\nend\nputs result.join()\n"}, {"source_code": "A = ('a'..'z').to_a.join\nn, m = gets.split.map(&:to_i)\ns = gets.chomp\nds = m.times.map{gets.split}\na = A.dup\nds.each{|x, y| a.tr!(x + y, y + x)}\nputs s.tr(A, a)\n\n"}, {"source_code": "A = ('a'..'z').to_a.join\nn, m = gets.split.map(&:to_i)\ns = gets.chomp\nds = m.times.map{gets.split}\na = A.dup\nds.each{|x, y| a.tr!(x + y, y + x)}\nputs s.tr(A, a)\n\n"}, {"source_code": "class Solver\n\tdef main\n\t\tn, m = gets.split.map { |s| s.to_i }\n\t\tstr = gets\n\t\t\n\t\tpos = Array.new(128) { |id, v| v = id }\n\t\tto = Array.new(128) { |id, v| v = id }\n\t\tm.times do\n\t\t\ta, b = gets.split.map { |c| c.ord }\n\t\t\tto[pos[a]], to[pos[b]] = to[pos[b]], to[pos[a]]\n\t\t\tpos[a], pos[b] = pos[b], pos[a]\n\t\tend\n\n\t\tn.times do |i|\n\t\t\tprint to[str[i].ord].chr\n\t\tend\n\t\tprint ?\\n\n\n\tend\nend\n\nSolver.new.main"}, {"source_code": "A = ('a'..'z').to_a.join\nn, m = gets.split.map(&:to_i)\ns = gets.chomp\nds = m.times.map{gets.split}\na = A.dup\nds.each{|x, y| a.tr!(x + y, y + x)}\nputs s.tr(A, a)\n\n"}], "negative_code": [{"source_code": "n = gets.split(' ')[1].to_i\ns = gets\nhash = {}\nn.times do\n a = gets.split(' ')\n hash[a[0]] = a[1]\n hash[a[1]] = a[0]\nend\n\nhash.each { |key, value|\n s.gsub!(key, value.upcase)\n}\n\nputs s.downcase\n\n"}, {"source_code": "n = gets.split(' ')[1].to_i\ns = gets\nhash = {}\n\"a\".upto(\"z\") { |s| hash[s] = s }\nn.times do\n a = gets.split(' ')\n tmp = hash[a[0]]\n hash[a[0]] = hash[a[1]]\n hash[a[1]] = tmp\nend\n\nhash.each { |key, value|\n s.gsub!(key, value)\n}\n\nputs s\n\n"}, {"source_code": "n = gets.split(' ')[1].to_i\ns = gets\nn.times do\n a = gets.split(' ')\n s.gsub!(a[0], a[1])\nend\n\nputs s\n\n"}, {"source_code": "input = gets.chomp.split\nn = Integer(input[0])\nm = Integer(input[1])\nname = gets.chomp.split(\"\")\ndesign_request = []\nfor i in 0...m\n design_request[i] = []\n design_request[i] = gets.chomp.split\nend\ndesign = []\nfor i in 0...m\n if design_request[i][0] != design_request[i][1]\n design.push(design_request[i])\n end\nend\n\nl = design.length\nx = design[0][0]\ny = design[0][1]\nreplace = [design[0], [y, x]]\n\nfor i in 1...l\n a = 0\n b = 0\n for j in 0...replace.length \n if replace[j][1] == design[i][0]\n replace[j][1] = design[i][1]\n a = 1 \n break \n end\n if replace[j][1] == design[i][1]\n replace[j][1] = design[i][0]\n b = 1\n end\n end\n if a == 0\n replace.push(design[i])\n end\n if b == 0\n replace.push([design[i][1], design[i][0]])\n end\nend\n\nfor i in 0...n\n for j in 0...replace.length\n if name[i] == replace[j][0]\n name[i] = replace[j][1]\n break\n end\n end\nend\nprint name.join\nputs"}, {"source_code": "l = gets.to_i\np = gets.to_i\nq = gets.to_i\n\nputs l * ( p * 1.0 / (p + q) )\n"}], "src_uid": "67a70d58415dc381afaa74de1fee7215"} {"nl": {"description": "In this problem you are to calculate the sum of all integers from 1 to n, but you should take all powers of two with minus in the sum.For example, for n = 4 the sum is equal to  - 1 - 2 + 3 - 4 =  - 4, because 1, 2 and 4 are 20, 21 and 22 respectively.Calculate the answer for t values of n.", "input_spec": "The first line of the input contains a single integer t (1 ≤ t ≤ 100) — the number of values of n to be processed. Each of next t lines contains a single integer n (1 ≤ n ≤ 109).", "output_spec": "Print the requested sum for each of t integers n given in the input.", "sample_inputs": ["2\n4\n1000000000"], "sample_outputs": ["-4\n499999998352516354"], "notes": "NoteThe answer for the first sample is explained in the statement."}, "positive_code": [{"source_code": "t = gets.to_i\nt.times do\n n = gets.to_i\n a = (1 + n) * n / 2\n x = 1\n while x <= n do\n a -= 2 * x\n x *= 2\n end\n puts a\nend\n"}, {"source_code": "#!/usr/bin/ruby\ndef solve(n)\n\ti=1\n\ti*=2 while i*2<=n\n\tn*(n+1)/2-2*(i*2-1)\nend\nputs gets.to_i.times.map{solve(gets.to_i)}"}, {"source_code": "#!/usr/bin/env ruby\n\n\n\n\tt=gets.chomp.to_i\n\ti=0\n\twhile i lessOrEqThan\n return sum\n end\n\n sum += powerOf2\n end\nend\n\nt.times.each do\n n = gets.to_i\n\n sum = (n*(n+1)/2) - (2*sumPowerOf2(n))\n\n puts sum\nend\n"}, {"source_code": "n=gets.to_i\n\nn.times{\n\tans=0\n\ta=gets.to_i\n\tans=a*(a+1)/2\n\tb=1\n\twhile b<=a\n\t\tans-=(b*2)\n\t\tb*=2\n\tend\n\tputs ans\n}\n\n\n"}, {"source_code": "def sum_of_two(n)\n r, x = 0, 1\n while x <= n\n r += x\n x *= 2\n end\n r\nend\nt = gets.to_i\n(0...t).each do |i|\n n = gets.to_i\n v = n * (n + 1) / 2\n puts v - sum_of_two(n) * 2\nend\n"}, {"source_code": "gets.to_i.times do\n n = gets.to_i\n res = n*(n+1)/2\n i = 1\n while i <= n do\n res -= 2*i\n i *= 2\n end\n puts res\nend\n"}, {"source_code": "# coding: utf-8\n\nt = gets.to_i\nt.times {\n n = gets.to_i\n sum = (n*(n+1))/2\n powersum = 0\n i = 0\n loop {\n power = 1 << i\n if power <= n\n powersum += power\n i += 1\n else\n break\n end\n }\n\n puts sum - 2*powersum\n}"}, {"source_code": "t = gets.chomp.to_i\nt.times do\n n = gets.chomp.to_i\n s = n * (n + 1) / 2\n m = 1\n while m <= n\n s -= 2 * m\n m <<= 1\n end\n puts s\nend\n"}, {"source_code": "num = gets.chomp.to_i\n\nnum.times do\n n = gets.chomp.to_i\n res = (1 + n) * n / 2\n (Math.log2(n).to_int+1).times do |n|\n res -= 2 * (2 ** (n))\n end\n puts res\nend"}, {"source_code": "t = gets.chomp.to_i\nfor i in 1..t\n\tn = gets.chomp.to_i\n\tk = (Math.log(n) / Math.log(2)).floor + 2\n\ts = n*(n+1)/2 - 2**k+2\n\tputs s\nend\n"}, {"source_code": "t = gets.to_i\nt.times do\n n = gets.to_i\n ans = (1 + n) * n / 2\n minus = 1\n while minus <= n\n ans -= minus * 2\n minus *= 2\n end\n puts ans\nend\n"}, {"source_code": "t = gets.to_i\nt.times do\n n = gets.to_i\n minus = 0\n 30.times do |i|\n break if 2 ** i > n\n minus += 2 ** i\n end\n puts (1 + n) * n / 2 - 2 * minus\nend\n"}, {"source_code": "def cal(n)\n r = (1+n)*n/2\n k = 1\n while k <= n\n r -= k * 2\n k *= 2\n end\n r\nend\n\nt = gets.to_i\n\n(1..t).each do\n n = gets.to_i\n puts cal(n)\nend\n"}, {"source_code": "n = gets.to_i\nfor i in 1..n\n\tk = gets.to_i\n\ts = (k*(k+1))/2\n\tv = 1\n\twhile v<=k\n\t\ts-=v*2\n\t\tv*=2\n\tend\n\tputs(s)\nend"}, {"source_code": "t = gets.chomp.to_i\nt.times do\n n = gets.chomp.to_i\n power2 = []\n snd = 1\n i = 1\n while snd <= n\n power2 << snd\n snd = 2**i\n i += 1\n end\n puts n*(n+1)/2 - 2 * power2.inject(:+)\nend\n"}, {"source_code": "#!/usr/bin/ruby\n\nlen = gets.to_i\nfor iter in 0..len - 1\n t = gets.to_i\n pow = (Math.log(t) / Math.log(2)).to_i\n puts t * (t + 1) / 2 - (2 ** (pow + 1) - 1) * 2\nend\n"}, {"source_code": "l=gets.chomp.to_i\np=[]\n0.upto(l-1)\t{|i| p[i]=gets.chomp.to_i}\n\ndef Ans(a)\n\tk=(a*(a+1))/2-(2*(2**((Math.log(a,2).to_i+1))-1))\n\treturn k\nend\t\n0.upto(l-1){|i| puts Ans(p[i])}\n\n"}, {"source_code": "t = gets.to_i\n\nt.times do\n n = gets.to_i\n sum = ((1 + n)*n)/2\n step = 1\n while step <= n\n sum -= 2*step\n step *= 2\n end\n puts(sum)\nend"}, {"source_code": "t = gets.to_i\n\nt.times do\n n = gets.to_i\n sum = ((1 + n)*n)/2\n n = 1 + Math.log(n,2).to_i\n sum -= 2 * (2**n - 1)\n puts(sum)\nend\n"}, {"source_code": "class Solver\n\tdef main\n\t\tn = gets.to_i\n\t\tans = n * (n + 1) / 2\n\t\ttmp = 1\n\t\twhile tmp <= n do\n\t\t\tans -= 2 * tmp\n\t\t\ttmp *= 2\n\t\tend\n\t\tputs ans\n\tend\nend\n\ncase_cnt = gets.to_i\ncase_cnt.times do\n\tSolver.new.main\nend\n"}], "negative_code": [{"source_code": "# c598-A\n\ndef tricky_sum(n)\n\tp = Math.log(n,2)\n\tn * (n+1) / 2 - 2 ** p + 2\nend\n\nt = gets.to_i\nt.times do\n\tputs tricky_sum(gets.to_i)\nend\n\n\n"}, {"source_code": "# c598-A\n\ndef tricky_sum(n)\n\tp = Math.log(n,2).to_i\n\tn * (n+1) / 2 - 2 ** p + 2\nend\n\nt = gets.to_i\nt.times do\n\tputs tricky_sum(gets.to_i)\nend\n\n\n"}, {"source_code": "t = gets.to_i\n\nt.times do\n n = gets.to_i\n sum = ((1 + n)*n)/2\n step = 1\n while step <= n\n sum -= step\n step *= 2\n end\n puts(sum)\nend"}], "src_uid": "a3705f29b9a8be97a9e9e54b6eccba09"} {"nl": {"description": "Lindsey Buckingham told Stevie Nicks \"Go your own way\". Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world.Consider a hexagonal tiling of the plane as on the picture below. Nicks wishes to go from the cell marked $$$(0, 0)$$$ to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from $$$(0, 0)$$$ to $$$(1, 1)$$$ will take the exact same cost as going from $$$(-2, -1)$$$ to $$$(-1, 0)$$$. The costs are given in the input in the order $$$c_1$$$, $$$c_2$$$, $$$c_3$$$, $$$c_4$$$, $$$c_5$$$, $$$c_6$$$ as in the picture below. Print the smallest cost of a path from the origin which has coordinates $$$(0, 0)$$$ to the given cell.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 10^{4}$$$). Description of the test cases follows. The first line of each test case contains two integers $$$x$$$ and $$$y$$$ ($$$-10^{9} \\le x, y \\le 10^{9}$$$) representing the coordinates of the target hexagon. The second line of each test case contains six integers $$$c_1$$$, $$$c_2$$$, $$$c_3$$$, $$$c_4$$$, $$$c_5$$$, $$$c_6$$$ ($$$1 \\le c_1, c_2, c_3, c_4, c_5, c_6 \\le 10^{9}$$$) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value).", "output_spec": "For each testcase output the smallest cost of a path from the origin to the given cell.", "sample_inputs": ["2\n-3 1\n1 3 5 7 9 11\n1000000000 1000000000\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000"], "sample_outputs": ["18\n1000000000000000000"], "notes": "NoteThe picture below shows the solution for the first sample. The cost $$$18$$$ is reached by taking $$$c_3$$$ 3 times and $$$c_2$$$ once, amounting to $$$5+5+5+3=18$$$. "}, "positive_code": [{"source_code": "T = gets.to_i\n\nT.times do\n y, x = gets.split.map(&:to_i)\n c = gets.split.map(&:to_i)\n\n if c[0] > c[1] + c[5]\n c[0] = c[1] + c[5]\n end\n\n if c[1] > c[0] + c[2]\n c[1] = c[0] + c[2]\n end\n\n if c[2] > c[1] + c[3]\n c[2] = c[1] + c[3]\n end\n\n if c[3] > c[2] + c[4]\n c[3] = c[2] + c[4]\n end\n\n if c[4] > c[3] + c[5]\n c[4] = c[3] + c[5]\n end\n\n if c[5] > c[0] + c[4]\n c[5] = c[0] + c[4]\n end\n\n cy = 0\n cx = 0\n ans = 0\n\n if cx < x && cy < y\n d = [x - cx, y - cy].min\n cy += d\n cx += d\n ans += c[0] * d\n end\n\n if x < cx && y < cy\n d = [cx - x, cy - y].min\n cy -= d\n cx -= d\n ans += c[3] * d\n end\n\n if cx < x\n d = x - cx\n cx += d\n ans += c[1] * d\n end\n\n if x < cx\n d = cx - x\n cx -= d\n ans += c[4] * d\n end\n\n if cy < y\n d = y - cy\n cy += d\n ans += c[5] * d\n end\n\n if y < cy\n d = cy - y\n cy -= d\n ans += c[2] * d\n end\n\n puts ans\nend\n"}], "negative_code": [], "src_uid": "7e6bc93543ad7bf80a019559b7bf99e0"} {"nl": {"description": "This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even.Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even.For example, if $$$s$$$ is \"11001111\", it will be divided into \"11\", \"00\" and \"1111\". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so \"11001111\" is good. Another example, if $$$s$$$ is \"1110011000\", it will be divided into \"111\", \"00\", \"11\" and \"000\", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, \"1110011000\" is not good.Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 \\leq i \\leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good?", "input_spec": "The first contains a single positive integer $$$t$$$ ($$$1 \\leq t \\leq 10\\,000$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 \\leq n \\leq 2 \\cdot 10^5$$$) — the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, print a single line with one integer — the minimum number of operations to make $$$s$$$ good.", "sample_inputs": ["5\n10\n1110011000\n8\n11001111\n2\n00\n2\n11\n6\n100110"], "sample_outputs": ["3\n0\n0\n0\n3"], "notes": "NoteIn the first test case, one of the ways to make $$$s$$$ good is the following.Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes \"1100000000\", it can be divided into \"11\" and \"00000000\", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as \"1111110000\", \"1100001100\", \"1111001100\".In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required."}, "positive_code": [{"source_code": "gets.to_i.times do\n n = gets.to_i\n a = gets.split('').map(&:to_i)\n sl = [1]\n c = 0\n 1.upto(n-1) do |i|\n if a[i] != a[i-1] then\n c += 1\n sl[c] = 1\n else\n sl[c] += 1\n end\n end\n ans = 0\n r = 0\n sl.each do |l|\n if (l%2 == 1) then \n r = 1 - r\n end\n ans += r\n end\n puts ans\nend\n"}], "negative_code": [], "src_uid": "aded139ff4d3f313f8e4d81559760f12"} {"nl": {"description": "Little X has met the following problem recently. Let's define f(x) as the sum of digits in decimal representation of number x (for example, f(1234) = 1 + 2 + 3 + 4). You are to calculate Of course Little X has solved this problem quickly, has locked it, and then has tried to hack others. He has seen the following C++ code: ans = solve(l, r) % a; if (ans <= 0) ans += a; This code will fail only on the test with . You are given number a, help Little X to find a proper test for hack.", "input_spec": "The first line contains a single integer a (1 ≤ a ≤ 1018).", "output_spec": "Print two integers: l, r (1 ≤ l ≤ r < 10200) — the required test data. Leading zeros aren't allowed. It's guaranteed that the solution exists.", "sample_inputs": ["46", "126444381000032"], "sample_outputs": ["1 10", "2333333 2333333333333"], "notes": null}, "positive_code": [{"source_code": "a = gets.to_i\nputs \"#{10**20 - a} #{10**20 + a - 1}\"\n"}, {"source_code": "a=gets.to_i\nr=(45*20*10**19+1)%a\nputs \"#{a-r+1} #{a-r+10**20}\""}], "negative_code": [], "src_uid": "52b8d97f216ea22693bc16fadcd46dae"} {"nl": {"description": "You are given an n × m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the tableabcdedfghijk we obtain the table:acdefghjk A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good.", "input_spec": "The first line contains two integers  — n and m (1 ≤ n, m ≤ 100). Next n lines contain m small English letters each — the characters of the table.", "output_spec": "Print a single number — the minimum number of columns that you need to remove in order to make the table good.", "sample_inputs": ["1 10\ncodeforces", "4 4\ncase\ncare\ntest\ncode", "5 4\ncode\nforc\nesco\ndefo\nrces"], "sample_outputs": ["0", "2", "4"], "notes": "NoteIn the first sample the table is already good.In the second sample you may remove the first and third column.In the third sample you have to remove all the columns (note that the table where all rows are empty is considered good by definition).Let strings s and t have equal length. Then, s is lexicographically larger than t if they are not equal and the character following the largest common prefix of s and t (the prefix may be empty) in s is alphabetically larger than the corresponding character of t."}, "positive_code": [{"source_code": "n, m = STDIN.readline.split.map {|s| s.to_i }\nwords = []\nn.times { words.push(STDIN.readline.strip) }\nsame = n.times.map { true }\nprevious_same = n.times.map { false }\ndelete = 0\n\nm.times do |c|\n previous_same, same = same, previous_same\n (1..n-1).each do |r|\n if previous_same[r]\n a, b = words[r-1][c], words[r][c]\n if a < b\n same[r] = false\n elsif a == b\n same[r] = true\n else\n delete += 1\n (1..n-1).each {|i| same[i] = previous_same[i] }\n break\n end\n else\n same[r] = false\n end\n end\nend\n\nputs delete\n"}, {"source_code": "# Codeforces My Practice\n# author: Leonardone @ NEETSDKASU\n############################################################\ndef gs() gets.strip end\ndef gi() gets.to_i end\ndef gss() gs.split end\ndef gis() gss.map(&:to_i) end\ndef nmapf(n,f) n.times.map{ __send__ f } end\ndef ngs(n) nmapf n,:gs end\ndef ngi(n) nmapf n,:gi end\ndef ngss(n) nmapf n,:gss end\ndef ngis(n) nmapf n,:gis end\ndef arr2d(h,w,v=0) h.times.map{[v] * w} end\ndef for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end\ndef nsum(n) n * (n + 1) / 2 end\ndef vcount(d,r=Hash.new(0)) d.inject(r){|m,e| m[e]+=1;m} end\n############################################################\n\nN, M = gis\nW = ngs N\n\na = [false] * N\n\nc = 0\n\nM.times do |i|\n b = [false] * N\n ok = true\n (N - 1).times do |j|\n if a[j]\n b[j] = true\n next\n end\n case W[j][i] <=> W[j + 1][i]\n when -1\n b[j] = true\n when 1\n ok = false\n c += 1\n break\n end\n end\n a = b if ok\nend\n\nputs c\n"}, {"source_code": "def read_ints; STDIN.gets.split.map(&:to_i); end\n\nn, m = read_ints\nb = n.times.map{gets.rstrip}\nis_g = [true] + [false] * (n-1)\nans = 0\n\n(0.. m-1).each do |j|\n if (1..n-1).all? {|i| is_g[i] || b[i][j] >= b[i-1][j]}\n (1..n-1).each do |i|\n is_g[i] = true if b[i][j] > b[i-1][j]\n end\n else\n ans += 1\n end\nend\n\nputs ans\n"}, {"source_code": "pa=Array.new\npb=Array.new\npc=Array.new\npd=Array.new\n\nstr=gets.to_s\npa=str.split\nn=pa[0].to_i\nm=pa[1].to_i\nfor i in 0..n-1\n pb[i]=gets.to_s\n pc[i]=0\nend\n\nk=0\nfor j in 0..m-1\n r=flag=0\n for i in 0..n-2\n if pc[i]==0 && pb[i][j]>pb[i+1][j]\n k+=1\n flag=1\n break\n elsif pc[i]==0 && pb[i][j] matrix[j - 1][i]\n checked << j\n end\n end || next\n to_check -= checked\nend\nputs count"}], "negative_code": [], "src_uid": "a45cfc2855f82f162133930d9834a9f0"} {"nl": {"description": "You are given an array $$$a[0 \\dots n-1]$$$ of $$$n$$$ integers. This array is called a \"valley\" if there exists exactly one subarray $$$a[l \\dots r]$$$ such that: $$$0 \\le l \\le r \\le n-1$$$, $$$a_l = a_{l+1} = a_{l+2} = \\dots = a_r$$$, $$$l = 0$$$ or $$$a_{l-1} > a_{l}$$$, $$$r = n-1$$$ or $$$a_r < a_{r+1}$$$. Here are three examples: The first image shows the array [$$$3, 2, 2, 1, 2, 2, 3$$$], it is a valley because only subarray with indices $$$l=r=3$$$ satisfies the condition.The second image shows the array [$$$1, 1, 1, 2, 3, 3, 4, 5, 6, 6, 6$$$], it is a valley because only subarray with indices $$$l=0, r=2$$$ satisfies the codition.The third image shows the array [$$$1, 2, 3, 4, 3, 2, 1$$$], it is not a valley because two subarrays $$$l=r=0$$$ and $$$l=r=6$$$ that satisfy the condition.You are asked whether the given array is a valley or not.Note that we consider the array to be indexed from $$$0$$$.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \\leq n \\leq 2\\cdot10^5$$$) — the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_i$$$ ($$$1 \\leq a_i \\leq 10^9$$$) — the elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases is smaller than $$$2\\cdot10^5$$$.", "output_spec": "For each test case, output \"YES\" (without quotes) if the array is a valley, and \"NO\" (without quotes) otherwise. You can output the answer in any case (for example, the strings \"yEs\", \"yes\", \"Yes\" and \"YES\" will be recognized as a positive answer).", "sample_inputs": ["6\n\n7\n\n3 2 2 1 2 2 3\n\n11\n\n1 1 1 2 3 3 4 5 6 6 6\n\n7\n\n1 2 3 4 3 2 1\n\n7\n\n9 7 4 6 9 9 10\n\n1\n\n1000000000\n\n8\n\n9 4 4 5 9 4 9 10"], "sample_outputs": ["YES\nYES\nNO\nYES\nYES\nNO"], "notes": "NoteThe first three test cases are explained in the statement."}, "positive_code": [{"source_code": "main = -> {\n n = gets.to_i\n a = gets.split.map(&:to_i)\n\n cur = 0\n while cur + 1 < n && a[cur + 1] <= a[cur]\n cur += 1\n end\n\n while cur + 1 < n && a[cur + 1] >= a[cur]\n cur += 1\n end\n\n cur + 1 == n ? \"YES\" : \"NO\"\n}\n\nt = gets.to_i\nputs t.times.map{ main.call }.join(\"\\n\")\n"}, {"source_code": "gets.to_i.times{\n gets\n climbing=0\n sinked=0\n climbed=0\n valley=1\n s=gets.split.map(&:to_i)\n (s.size-1).times {|i|\n if s[i]s[i+1]\n if climbing==1\n valley=0\n end\n sinked=1\n climbing=0\n end\n }\n if valley==1\n puts(\"YES\")\n else\n puts(\"NO\")\n end\n}"}, {"source_code": "=begin\n\nWelcome to GDB Online.\nGDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,\nC#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.\nCode, Compile, Run and Debug online from anywhere in world.\n\n=end\n\nt=gets.to_i\nfor i in 1..t\n n =gets.to_i\n s=gets\n maks=s.split.map{|x|x.to_i}\n num=1\n inc=0\n prev=maks[0]\n for j in 1..(n-1)\n if (maks[j]>prev)\n inc=1\n elsif (maks[j]0\r\n c+=1\r\n d=1\r\n end\r\n else\r\n if x[i+1]-x[i]<0\r\n c+=1\r\n d=-1\r\n end\r\n end\r\n }\r\n puts c<2 ? \"YES\" : \"NO\"\r\n}"}], "negative_code": [], "src_uid": "7b6a3de5ad0975e4c43f097d4648c9c9"} {"nl": {"description": "Two people are playing a game with a string $$$s$$$, consisting of lowercase latin letters. On a player's turn, he should choose two consecutive equal letters in the string and delete them. For example, if the string is equal to \"xaax\" than there is only one possible turn: delete \"aa\", so the string will become \"xx\". A player not able to make a turn loses.Your task is to determine which player will win if both play optimally.", "input_spec": "The only line contains the string $$$s$$$, consisting of lowercase latin letters ($$$1 \\leq |s| \\leq 100\\,000$$$), where $$$|s|$$$ means the length of a string $$$s$$$.", "output_spec": "If the first player wins, print \"Yes\". If the second player wins, print \"No\".", "sample_inputs": ["abacaba", "iiq", "abba"], "sample_outputs": ["No", "Yes", "No"], "notes": "NoteIn the first example the first player is unable to make a turn, so he loses.In the second example first player turns the string into \"q\", then second player is unable to move, so he loses."}, "positive_code": [{"source_code": "s=gets.chomp\nt=[]\ns.bytes{|c|\nt<0 ? :Yes : :No"}, {"source_code": "s = gets.chomp\nchars = s.split('')\n\ncount = 0\na = Array.new\nchars.each { |i|\n\tif a[a.size - 1] != i\n\t\ta << i\n\telse\n\t\ta.pop()\n\t\tcount += 1\n\tend\n}\nif count % 2 == 1\n\tputs \"Yes\"\nelse\n\tputs \"No\"\nend"}, {"source_code": "s = gets.chomp\nt = Time.now\n# s = 'a' * 10 ** 5\nf = true\nz = 0\nprev = [s[0]]\nprev_ind = 0\ncur = s[1]\ni = 2\nwhile i <= s.size do\n if (cur == prev.last)\n # puts cur, prev.last, \"1----\"\n prev.pop\n cur = s[i]\n i += 1\n f = !f\n else\n if (s[i] != cur)\n prev << cur\n cur = s[i]\n i += 1\n else\n # puts s[i], cur, \"2----\"\n cur = s[i + 1]\n i += 2\n f = !f\n end\n end\nend\nif (cur == prev.last && !cur.nil? && cur != '')\n # puts prev.last, cur, \"3----\"\n f = !f\nend\nputs f ? \"No\" : \"Yes\"\n# puts Time.now - t"}, {"source_code": "s = gets.chomp\nused = Array.new(s.size,false)\nl = 0\nt = []\nfor r in 1..s.size-1\n next if l == r\n if s[l] == s[r]\n used[l] = used[r] = true\n if t.empty?\n l = r+1\n else\n l = t[-1]\n t.pop\n end\n else\n t.push(l)\n l = r\n end\nend\nputs [\"No\",\"Yes\"][used.count(true)/2%2]"}, {"source_code": "DBG = !true\ns = gets.chomp\ncnt = 0\na = []\nfor i in 0...s.size\n a.push(s[i])\n if a.size >= 2 && a[-1] == a[-2]\n a.pop\n a.pop\n cnt += 1\n end\nend\nputs ((cnt%2 == 1) ? \"Yes\" : \"No\")\n"}], "negative_code": [{"source_code": "s=gets.chomp\nt=''.dup\ns.chars{|c|\nt<0 ? :No : :Yes"}, {"source_code": "s = gets.chomp\nt = Time.now\n# s = 'a' * 10 ** 5\nf = true\nz = 0\nprev = s[0]\nprev_ind = 0\ncur = s[1]\ni = 2\nwhile i < s.size do\n if (cur == prev)\n # puts cur, prev, \"----\"\n prev_ind = [prev_ind - 1, 0].max\n prev = s[prev_ind]\n cur = s[i]\n i += 1\n f = !f\n else\n if (s[i] != cur)\n prev = cur\n prev_ind = [i - 1, 0].max\n cur = s[i]\n i += 1\n else\n # puts s[i], cur, \"----\"\n cur = s[i + 1]\n i += 2\n f = !f\n end\n end\nend\nif (cur == prev && cur != '')\n # puts prev, cur, \"----\"\n f = !f\nend\nputs f ? \"No\" : \"Yes\""}, {"source_code": "s = gets.chomp\nt = Time.now\n# s = 'a' * 10 ** 5\nf = true\nz = 0\nprev = [s[0]]\nprev_ind = 0\ncur = s[1]\ni = 2\nwhile i < s.size do\n if (cur == prev.last)\n # puts cur, prev.last, \"----\"\n prev.pop\n cur = s[i]\n i += 1\n f = !f\n else\n if (s[i] != cur)\n prev << cur\n cur = s[i]\n i += 1\n else\n # puts s[i], cur, \"----\"\n cur = s[i + 1]\n i += 2\n f = !f\n end\n end\nend\nif (cur == prev.last && cur != '')\n # puts prev, cur, \"----\"\n f = !f\nend\nputs f ? \"No\" : \"Yes\""}, {"source_code": "s = gets.chomp\nt = Time.now\n# s = 'a' * 10 ** 5\nf = true\nz = 0\nprev = s[0]\nprev_ind = 0\ncur = s[1]\ni = 2\nwhile i < s.size do\n if (cur == prev)\n # puts cur, prev, \"----\"\n prev_ind -= 1\n prev = s[prev_ind]\n if (prev_ind < 0)\n prev_ind = 0\n prev = ''\n end\n cur = s[i]\n i += 1\n f = !f\n else\n if (s[i] != cur)\n prev = cur\n prev_ind = [i - 1, 0].max\n cur = s[i]\n i += 1\n else\n # puts s[i], cur, \"----\"\n cur = s[i + 1]\n i += 2\n f = !f\n end\n end\nend\nif (cur == prev && cur != '')\n # puts prev, cur, \"----\"\n f = !f\nend\nputs f ? \"No\" : \"Yes\""}, {"source_code": "DBG = !true\ns = gets.chomp\ncnt = 0\ncur = 0\nwhile cur < s.size\n if s[cur] != s[cur+1]\n cur += 1\n next\n end\n puts \"loop on cur #{cur}\" if DBG\n len = 1\n while 0 <= cur - len && cur+1+len < s.size &&\n s[cur-len] == s[cur+1+len]\n len += 1\n end\n cur += len+1\n cnt += len\n puts \"cur #{cur} cnt #{cnt}\" if DBG\nend\nputs ((cnt%2 == 1) ? \"Yes\" : \"No\")\n"}], "src_uid": "8fd51c391728b433cc94923820e908ff"} {"nl": {"description": "Vanya walks late at night along a straight street of length l, lit by n lanterns. Consider the coordinate system with the beginning of the street corresponding to the point 0, and its end corresponding to the point l. Then the i-th lantern is at the point ai. The lantern lights all points of the street that are at the distance of at most d from it, where d is some positive number, common for all lanterns. Vanya wonders: what is the minimum light radius d should the lanterns have to light the whole street?", "input_spec": "The first line contains two integers n, l (1 ≤ n ≤ 1000, 1 ≤ l ≤ 109) — the number of lanterns and the length of the street respectively. The next line contains n integers ai (0 ≤ ai ≤ l). Multiple lanterns can be located at the same point. The lanterns may be located at the ends of the street.", "output_spec": "Print the minimum light radius d, needed to light the whole street. The answer will be considered correct if its absolute or relative error doesn't exceed 10 - 9.", "sample_inputs": ["7 15\n15 5 3 7 9 14 0", "2 5\n2 5"], "sample_outputs": ["2.5000000000", "2.0000000000"], "notes": "NoteConsider the second sample. At d = 2 the first lantern will light the segment [0, 4] of the street, and the second lantern will light segment [3, 5]. Thus, the whole street will be lit."}, "positive_code": [{"source_code": "n, l = gets.split.map(&:to_i)\na = gets.split.map(&:to_i).sort\n\nans = ([a[0],l-a[n-1]].max)*1.0\n\ni = 0\n(n-1).times do\n ans = [ans,(a[i+1]-a[i])*0.5].max\n i += 1\nend\n\nputs ans\n"}, {"source_code": "n, l = gets.split(' ').map(&:to_i)\narr = gets.split(' ').map(&:to_i)\n\narr = arr.sort\nres = [arr[0], l - arr[n-1]]\nfor i in (1...n)\n res.push((arr[i] - arr[i-1]) / 2.0)\nend\n\np res.max"}, {"source_code": "# http://codeforces.com/contest/492/problem/B\nn, l = gets.chomp.split.map(&:to_i)\nlanterns = gets.chomp.split.map(&:to_f).sort\n\narr = [lanterns.first - 0, l - lanterns.last]\nlanterns.each_cons(2){ |a, b| arr << (b - a) / 2.0 }\n\nputs arr.max\n"}, {"source_code": "input = gets.chomp\nginp = input.split ' '\n\nb, s = ginp.first.to_i, ginp.last.to_i\n\nai = gets.chomp().split(' ').map(&:to_i).sort!\nmax = ai[0] > (s - ai.last) ? ai[0] : (s - ai.last)\nai.each_with_index do |v, i|\n res = ((ai[i+1] - v) / 2.0).to_f unless i == b-1\n max = res if res && res > max\nend\nputs max.to_f"}, {"source_code": "a,b=gets.chomp.split.map(&:to_i)\nc=gets.chomp.split.map(&:to_i).sort\nmax=2*c[0]\nmax=(b-c[a-1])*2 if 2*(b-c[a-1])>max\nfor i in 0...a-1 do\n\tmax=c[i+1]-c[i] if c[i+1]-c[i]>max\nend\nputs max/2.0"}, {"source_code": "require 'pp'\n\nn, l = STDIN.gets.chomp.split(/ /).map(&:to_i)\na = STDIN.gets.chomp.split(/ /).map(&:to_i)\n\na.sort!\n\nd2 = 0\n\n(a.count-1).times do |i|\n d2 = [d2, a[i+1] - a[i]].max\nend\n\nd = [d2.to_f/2, a.first, l-a.last].max\nputs d\n"}, {"source_code": "n, l = gets.split.map &:to_i\narr = gets.split.map { |x| x.to_i }.sort\nmax_dif = 0\nfor i in 0..arr.size - 2\n if arr[i + 1] - arr[i] > max_dif\n max_dif = arr[i + 1] - arr[i]\n end\nend\nd = [arr.first, (l - arr.last), max_dif / 2.0].max\nprintf \"%.9f\",d\n"}, {"source_code": "n, l = gets.split.map(&:to_i)\na = gets.split(\" \")\na.each_index{|i| a[i]=a[i].to_i}\na.sort!\nbest = 0\na.each_index{|i|\n if i>0\n best = [best,a[i]-a[i-1]].max\n end\n}\nputs [a[0], l-a[a.size-1], best/2.0].max"}, {"source_code": "n, l = gets.split.map(&:to_i)\np = gets.split.map(&:to_i).sort\n\ndiff = [0]\n(1...p.length).each do |i|\n diff << p[i] - p[i - 1]\nend\n\nmax = [l - p.max, diff.max / 2.0, p.min].max\nputs max"}, {"source_code": "#! /usr/bin/env ruby\n# coding: utf-8\n\nn, l = gets.chomp.split.map(&:to_i)\np = gets.chomp.split.map(&:to_i).sort\n\nmax = -1\ny = p[0] - 0 if 0 < p[0] - 0\nmax = y if y != nil and max < y\ny = l - p[-1] if 0 < l - p[-1]\nmax = y if y != nil and max < y\nx = -1\np.each do |i|\n y = (i - x) / 2.0 unless x < 0\n max = y if y != nil and max < y\n x = i\nend\n\nputs \"#{max}\"\n\n"}, {"source_code": "def read_ints; STDIN.gets.split.map(&:to_i); end\n\nn, l = read_ints\na = read_ints.sort\na << -a[0] << 2 * l - a[-2]\nputs a.sort.each_cons(2).map {|x| x[1] - x[0]}.max / 2.0\n"}, {"source_code": "n,l=gets.split.map{|e| e.to_i}\nx=gets.split.map{|e| e.to_f}\n\nx.sort!\nans=[x[0],l-x.last].max\n\n0.upto(x.size-2){|i|\n\td=(x[i+1]-x[i])/2.0\n\tans=[ans,d].max\n}\nputs sprintf(\"%.10f\",ans)"}, {"source_code": "#!/usr/bin/env ruby -w\n\ndef main\n _, l = gets.split.map(&:to_i)\n lights = gets.split.map(&:to_i)\n puts light_radius(l, lights)\nend\n\ndef light_radius(street_length, lights)\n lights.sort!\n\n distances = []\n distances << lights.first\n distances << street_length - lights.last\n\n lights.each_cons(2) do |foo, bar|\n distances << (bar - foo) / 2.0\n end\n\n distances.max\nend\n\nmain if __FILE__ == $0\n"}, {"source_code": "n,l = gets.chomp.split.map(&:to_i)\narr = gets.chomp.split.map(&:to_i)\n\nres = 0\narr.sort!\nif arr[0] != 0\n res = [res,arr[0]].max\nend\nif arr[n-1] != n\n res = [res, l - arr[n-1]].max\nend \n\nfor i in 0...n-1\n res = [res,(arr[i+1]-arr[i])/2.0].max\nend\n\nputs res"}, {"source_code": "n,l = gets.split.map(&:to_i)\na = gets.split.map(&:to_i).sort\nx = 0\nfor i in 0...n\n\tx = [ a[i] - a[i - 1] , x ].max\nend\nx = x/2.00\nputs [ x , a[0], l-a[n-1] ].max\n\n"}, {"source_code": "input = gets.chomp\nginp = input.split ' '\n\nn, l = ginp.first.to_i, ginp.last.to_i\n\nai = gets.chomp().split(' ').map(&:to_i).sort!\nmax = ai[0] > (l - ai.last) ? ai[0] : (l - ai.last)\nai.each_with_index do |v, i|\n x = ((ai[i+1] - v) / 2.0).to_f unless i == n-1\n max = x if x && x > max\nend\nputs max.to_f"}, {"source_code": "def main\n n, l = gets.split.map(&:to_i)\n a = gets.split.map(&:to_i).sort!\n ans = [a[0], l - a[-1]].max.to_f\n (1...n).each do |i|\n ans = [ans, (a[i] - a[i - 1]) * 0.5].max\n end\n ans\nend\n\nputs main"}, {"source_code": "nums = gets.chomp.split\nn = Integer(nums[0])\nl = Integer(nums[1])\nnums2 = gets.chomp.split\nlanterns = Array.new\nfor i in 0...nums2.length\n lanterns[i] = Integer(nums2[i])\nend\n\nif n == 1\n x = lanterns[0]\n y = l - lanterns[0]\n if x >= y\n puts x\n else\n puts y\n end\nelse\n lanterns.sort!\n a = lanterns[0]\n c = l - lanterns[n-1]\n dis = lanterns[1] - lanterns[0]\n for i in 0...n-1\n b = lanterns[i+1] - lanterns[i]\n if b > dis\n dis = b\n end\n end\n dis = dis.to_f / 2\n\n max = Array.new\n max.push(a)\n max.push(dis)\n max.push(c)\n\n max.sort!\n puts max[2]\nend\n"}, {"source_code": "n,l = gets.split(\" \").map(&:to_i)\narr = gets.split(\" \").map(&:to_i)\narr.sort!\ndiff = []\n(1..n-1).each do |i|\n\tdiff << arr[i] - arr[i-1]\nend\n\nmax_diff = diff.max || 0\nans = max_diff / 2.0\nval = arr[0] unless arr.include?(0)\nans = val if val && val > ans\n\nval = l - arr[-1] unless arr.include?(l)\nans = val if val && val > ans\n\n\nputs ans.to_f"}, {"source_code": "n, l = gets.split.map(&:to_i)\nq = gets.split.map(&:to_i)\n\nq = q.uniq.sort\n\nr = 2 * [q[0].to_f, (l - q[-1])].max\n\nfor i in (1..q.size-1)\n if r < q[i] - q[i-1]\n r = q[i] - q[i-1]\n end\nend\n\nputs r.to_f/2\n"}, {"source_code": "def read_ints; STDIN.gets.split.map(&:to_i); end\n\nn, l = read_ints\na = read_ints.sort\na << -a[0] << 2 * l - a[-2]\nputs a.sort.each_cons(2).map {|x| x[1] - x[0]}.max / 2.0"}, {"source_code": "n, l = gets.split(' ').map(&:to_i)\na = gets.split(' ').map(&:to_i).sort\ncurrent = -a.first\na.push l + l-a.last\nresult = 0\n\nuntil a.empty? \n result = [(a.first-current)/2.0, result].max\n current = a.shift\nend\n\np result\n"}, {"source_code": "# 492B\n\ndef read_next_line\n gets.chomp.split.map &:to_i\nend\n\nn, l = read_next_line\na = read_next_line.sort\nb = []\n(0...n - 1).each { |i| b << a[i + 1] - a[i] }\nputs [a[0], l - a[-1], b.max.to_f / 2].max"}, {"source_code": "n, l = gets.split.map { |x| x.to_i }\na = gets.split.map { |x| x.to_i }\na.sort!\n\nf = [a[0] * 2, (l - a[-1]) * 2]\n1.upto(n - 1) do |i|\n\tf << (a[i] - a[i - 1])\nend\n\nans = f.max / 2.0\nputs '%.12f' % ans\n"}], "negative_code": [{"source_code": "n, l = gets.split.map(&:to_i)\np = gets.split.map(&:to_i).sort\n\ndiff = [0]\n(1...p.length).each do |i|\n diff << p[i] - p[i - 1]\nend\n\nmax = [l - p.max, diff.max / 2.0, diff.min].max\nputs max"}, {"source_code": "#! /usr/bin/env ruby\n# coding: utf-8\n\nn, l = gets.chomp.split.map(&:to_i)\np = gets.chomp.split.map(&:to_i).sort\n\nmax = -1.000000000\ny = p[0] - 0 if 0 < p[0] - 0\nmax = y if y != nil and max < y\ny = l - p[-1] if 0 < l - p[-1]\nmax = y if y != nil and max < y\nx = -1\nmax = -1\np.each do |i|\n y = (i - x) / 2.000000000 unless x < 0\n max = y if y != nil and max < y\n x = i\nend\n\nputs \"#{max}\"\n\n"}, {"source_code": "n,l = gets.split(\" \").map(&:to_i)\narr = gets.split(\" \").map(&:to_i)\narr.sort!\ndiff = []\n(1..n-1).each do |i|\n\tdiff << arr[i] - arr[i-1]\nend\n\nmax_diff = diff.max || 0\nans = max_diff / 2\nval = arr[0] unless arr.include?(0)\nans = val if val && val > ans\n\nval = l - arr[-1] unless arr.include?(l)\nans = val if val && val > ans\n\n\nputs ans.to_f"}, {"source_code": "n, l = gets.split.map(&:to_i)\nq = gets.split.map(&:to_i)\n\nq = q.uniq.sort\n\nr = 2 * [q[0].to_f, (l - q[-1])].max\nputs r\n\nfor i in (1..q.size-1)\n if r < q[i] - q[i-1]\n r = q[i] - q[i-1]\n end\nend\n\nputs r.to_f/2\n"}], "src_uid": "d79166497eb61d81fdfa4ef80ec1c8e8"} {"nl": {"description": "You are given four integers $$$n$$$, $$$c_0$$$, $$$c_1$$$ and $$$h$$$ and a binary string $$$s$$$ of length $$$n$$$.A binary string is a string consisting of characters $$$0$$$ and $$$1$$$.You can change any character of the string $$$s$$$ (the string should be still binary after the change). You should pay $$$h$$$ coins for each change.After some changes (possibly zero) you want to buy the string. To buy the string you should buy all its characters. To buy the character $$$0$$$ you should pay $$$c_0$$$ coins, to buy the character $$$1$$$ you should pay $$$c_1$$$ coins.Find the minimum number of coins needed to buy the string.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\leq t \\leq 10$$$) — the number of test cases. Next $$$2t$$$ lines contain descriptions of test cases. The first line of the description of each test case contains four integers $$$n$$$, $$$c_{0}$$$, $$$c_{1}$$$, $$$h$$$ ($$$1 \\leq n, c_{0}, c_{1}, h \\leq 1000$$$). The second line of the description of each test case contains the binary string $$$s$$$ of length $$$n$$$.", "output_spec": "For each test case print a single integer — the minimum number of coins needed to buy the string.", "sample_inputs": ["6\n3 1 1 1\n100\n5 10 100 1\n01010\n5 10 1 1\n11111\n5 1 10 1\n11111\n12 2 1 10\n101110110101\n2 100 1 10\n00"], "sample_outputs": ["3\n52\n5\n10\n16\n22"], "notes": "NoteIn the first test case, you can buy all characters and pay $$$3$$$ coins, because both characters $$$0$$$ and $$$1$$$ costs $$$1$$$ coin.In the second test case, you can firstly change $$$2$$$-nd and $$$4$$$-th symbols of the string from $$$1$$$ to $$$0$$$ and pay $$$2$$$ coins for that. Your string will be $$$00000$$$. After that, you can buy the string and pay $$$5 \\cdot 10 = 50$$$ coins for that. The total number of coins paid will be $$$2 + 50 = 52$$$."}, "positive_code": [{"source_code": "t=gets.to_i\nt.times{\n n,c0,c1,h=gets.split.map &:to_i\n bin=gets.chomp\n if c0 == c1\n puts bin.size*c0\n elsif c0 < h && c1 < h\n puts bin.count(\"0\")*c0+bin.count(\"1\")*c1\n else\n if c0 + h < c1\n puts bin.size*c0 + bin.count(\"1\")*h\n elsif c1 + h < c0\n puts bin.size*c1 + bin.count(\"0\")*h\n else\n puts bin.count(\"0\")*c0+bin.count(\"1\")*c1\n end\n end\n}"}, {"source_code": "t = gets.chomp.to_i\nt.times do\n n, c0, c1, h = gets.chomp.split(' ').map(&:to_i)\n s = gets.chomp.split('')\n res = 0\n s.each do |c|\n case c\n when '1'\n res += [c1, c0 + h].min\n when '0'\n res += [c0, c1 + h].min\n end\n end\n puts res\nend "}], "negative_code": [{"source_code": "t=gets.to_i\nt.times{\n n,c0,c1,h=gets.split.map &:to_i\n bin=gets.chomp\n if c0 == c1\n puts bin.size*c0\n elsif c0 < h && c1 < h\n puts bin.count(\"0\")*c0+bin.count(\"1\")*c1\n else\n if c0 + h < c1\n puts bin.size*c0 + bin.count(\"1\")*h\n elsif c1 + h < c0\n puts bin.size*c1 + bin.count(\"0\")*h\n end\n end\n}"}], "src_uid": "7cc0a6df601e3dcf4e1d9578dd599a36"} {"nl": {"description": " This is an interactive problem.We picked an array of whole numbers $$$a_1, a_2, \\ldots, a_n$$$ ($$$0 \\le a_i \\le 10^9$$$) and concealed exactly one zero in it! Your goal is to find the location of this zero, that is, to find $$$i$$$ such that $$$a_i = 0$$$.You are allowed to make several queries to guess the answer. For each query, you can think up three distinct indices $$$i, j, k$$$, and we will tell you the value of $$$\\max(a_i, a_j, a_k) - \\min(a_i, a_j, a_k)$$$. In other words, we will tell you the difference between the maximum and the minimum number among $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$.You are allowed to make no more than $$$2 \\cdot n - 2$$$ queries, and after that you have two tries to guess where the zero is. That is, you have to tell us two numbers $$$i$$$ and $$$j$$$ and you win if $$$a_i = 0$$$ or $$$a_j = 0$$$.Can you guess where we hid the zero?Note that the array in each test case is fixed beforehand and will not change during the game. In other words, the interactor is not adaptive.", "input_spec": "Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \\le t \\le 500$$$). Description of the test cases follows. The first and only line of each test case contains an integer $$$n$$$ ($$$4 \\le n \\le 1000$$$) — the length of the array that we picked. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3000$$$.", "output_spec": null, "sample_inputs": ["1\n\n4\n\n2\n\n3\n\n3\n\n2"], "sample_outputs": ["? 1 2 3\n\n? 2 3 4\n\n? 3 4 1\n\n? 4 1 2\n\n! 2 3"], "notes": "NoteArray from sample: $$$[1, 2, 0, 3]$$$."}, "positive_code": [{"source_code": "gets.to_i.times do\n n = gets.to_i\n\n m = 4\n heap = [1, 2, 3, 4]\n\n query = lambda do |indices|\n puts '? ' + indices.join(' ')\n $stdout.flush\n gets.to_i\n end\n\n loop do\n complement = []\n heap.each do |i|\n complement << query.call(heap - [i])\n end\n\n d = complement.max\n heap = heap.zip(complement).filter { |h, c| c != d }.map { _1.first }\n\n if m == n\n heap << heap.first if heap.size < 2\n puts '! ' + heap.join(' ')\n $stdout.flush\n break\n end\n\n while heap.size < 4\n break unless m < n\n m += 1\n heap << m\n end\n\n while heap.size < 4\n (1..).each do |x|\n next if heap.include? x\n heap << x\n break\n end\n end\n end\nend\n"}], "negative_code": [{"source_code": "gets.to_i.times do\n n = gets.to_i\n\n m = 4\n heap = [1, 2, 3, 4]\n\n query = lambda do |indices|\n puts '? ' + indices.join(' ')\n $stdout.flush\n gets.to_i\n end\n\n complement = []\n heap.each do |i|\n complement << query.call(heap - [i])\n end\n\n d = complement.max\n heap = heap.zip(complement).filter { |h, c| c != d }.map { _1.first }\n\n if m == n\n puts '! ' + heap.join(' ')\n $stdout.flush\n next\n end\n\n 2.times do\n break unless m < n\n heap << m\n m += 1\n end\n\n if heap.size < 4\n (1..).each do |x|\n next if heap.include? x\n heap << x\n break\n end\n end\nend\n"}, {"source_code": "gets.to_i.times do\n n = gets.to_i\n\n max_diff = 0\n max_i = nil\n\n (n - 2).times do |i|\n puts \"? #{i + 1} #{i + 2} #{i + 3}\"\n $stdout.flush\n d = gets.to_i\n\n if d > max_diff\n max_diff = d\n max_i = i\n end\n end\n\n # Let's eliminate `max_i`.\n ans = nil\n n.times do |i|\n next if max_i <= i && i <= max_i + 2\n\n puts \"? #{i + 1} #{max_i + 2} #{max_i + 3}\"\n $stdout.flush\n d = gets.to_i\n\n if d == max_diff\n # Yep, `max_i` is not zero.\n ans = [max_i + 2, max_i + 3]\n break\n end\n end\n\n if ans\n puts '! ' + ans.join(' ')\n else\n puts \"! #{max_i + 1} #{max_i + 2}\"\n end\n $stdout.flush\nend\n"}], "src_uid": "84e79bd83c51a4966b496bb767ec4f0d"} {"nl": {"description": "After finding and moving to the new planet that supports human life, discussions started on which currency should be used. After long negotiations, Bitcoin was ultimately chosen as the universal currency.These were the great news for Alice, whose grandfather got into Bitcoin mining in 2013, and accumulated a lot of them throughout the years. Unfortunately, when paying something in bitcoin everyone can see how many bitcoins you have in your public address wallet. This worried Alice, so she decided to split her bitcoins among multiple different addresses, so that every address has at most $$$x$$$ satoshi (1 bitcoin = $$$10^8$$$ satoshi). She can create new public address wallets for free and is willing to pay $$$f$$$ fee in satoshi per transaction to ensure acceptable speed of transfer. The fee is deducted from the address the transaction was sent from. Tell Alice how much total fee in satoshi she will need to pay to achieve her goal.", "input_spec": "First line contains number $$$N$$$ ($$$1 \\leq N \\leq 200\\,000$$$) representing total number of public addresses Alice has. Next line contains $$$N$$$ integer numbers $$$a_i$$$ ($$$1 \\leq a_i \\leq 10^9$$$) separated by a single space, representing how many satoshi Alice has in her public addresses. Last line contains two numbers $$$x$$$, $$$f$$$ ($$$1 \\leq f < x \\leq 10^9$$$) representing maximum number of satoshies Alice can have in one address, as well as fee in satoshies she is willing to pay per transaction. ", "output_spec": "Output one integer number representing total fee in satoshi Alice will need to pay to achieve her goal.", "sample_inputs": ["3\n13 7 6\n6 2"], "sample_outputs": ["4"], "notes": "NoteAlice can make two transactions in a following way:0. 13 7 6 (initial state)1. 6 7 6 5 (create new address and transfer from first public address 5 satoshies)2. 6 4 6 5 1 (create new address and transfer from second address 1 satoshi)Since cost per transaction is 2 satoshies, total fee is 4."}, "positive_code": [{"source_code": "n = gets.to_i\nary = gets.strip.split.map(&:to_i)\nx, f = gets.strip.split.map(&:to_i)\n\nans = 0\nmtv = x+f\nary.each do |v|\n t = (v-1)/mtv\n rv = v % mtv\n t += 1 if rv > x\n ans += (t*f)\nend\n\nputs ans"}], "negative_code": [], "src_uid": "8a3e8a5db6d7d668ffea0f59e2639e87"} {"nl": {"description": "The Berland University is preparing to celebrate the 256-th anniversary of its founding! A specially appointed Vice Rector for the celebration prepares to decorate the campus. In the center of the campus n ice sculptures were erected. The sculptures are arranged in a circle at equal distances from each other, so they form a regular n-gon. They are numbered in clockwise order with numbers from 1 to n.The site of the University has already conducted a voting that estimated each sculpture's characteristic of ti — the degree of the sculpture's attractiveness. The values of ti can be positive, negative or zero.When the university rector came to evaluate the work, he said that this might be not the perfect arrangement. He suggested to melt some of the sculptures so that: the remaining sculptures form a regular polygon (the number of vertices should be between 3 and n), the sum of the ti values of the remaining sculptures is maximized. Help the Vice Rector to analyze the criticism — find the maximum value of ti sum which can be obtained in this way. It is allowed not to melt any sculptures at all. The sculptures can not be moved.", "input_spec": "The first input line contains an integer n (3 ≤ n ≤ 20000) — the initial number of sculptures. The second line contains a sequence of integers t1, t2, ..., tn, ti — the degree of the i-th sculpture's attractiveness ( - 1000 ≤ ti ≤ 1000). The numbers on the line are separated by spaces.", "output_spec": "Print the required maximum sum of the sculptures' attractiveness.", "sample_inputs": ["8\n1 2 -3 4 -5 5 2 3", "6\n1 -2 3 -4 5 -6", "6\n1 2 3 4 5 6"], "sample_outputs": ["14", "9", "21"], "notes": "NoteIn the first sample it is best to leave every second sculpture, that is, leave sculptures with attractivenesses: 2, 4, 5 и 3."}, "positive_code": [{"source_code": "n = gets.to_i\nar = gets.split(' ').map(&:to_i)\nans = ar.inject(:+)\n(2..(n/3)).each do |d|\n if n % d == 0\n m = (0...n).each_slice(d).map{|x| x}.transpose.map{|x|\n x.map{|y| ar[y]}.inject(:+)\n }.max\n ans = [ans,m].max\n end\nend\nputs ans"}, {"source_code": "n=gets.to_i\na=gets.split.map &:to_i\ns=a.inject &:+\n2.upto(n/3){|m|\nnext if n%m!=0\nm.times{|i|\nt=(i...n).step(m).map{|_|a[_]}.inject &:+\ns=t if s0}-[p]).max"}, {"source_code": "n = gets.to_i\nt = gets.split(\" \").map(&:to_i)\nmax = 0\nt.each{|el| max += el}\n\nfor d in 2..n/3 do\n next if n % d != 0\n for i in 0..d-1 do\n sum = 0\n for j in 0..n / d - 1 do\n sum += t[i + j * d]\n end\n max = sum if sum > max\n end\nend\n\nputs max"}, {"source_code": "$n = gets.to_i\n$a = gets.split(/\\s+/).map { |i| i.to_i }\n$best = $a.inject(:+)\n\ndef count(start, step)\n\tres = 0\n\twhile start < $n do\n\t\tres += $a[start]\n\t\tstart += step\n\tend\n\tres\nend\n\ndef solve(step)\n\tstep.times do |i|\n\t\tcur = count(i, step)\n\t\t$best = cur if cur > $best\n\tend\nend\n\n(2..Math.sqrt($n).round.to_i).each do |i|\n\tif $n % i == 0\n\t\tsolve(i) if $n/i >= 3\n\t\tsolve($n/i) if i >= 3\n\tend\nend\nputs $best\n"}, {"source_code": "$n = gets.to_i\n$tis = []\ngets.split(' ').each {|x| $tis.push x.to_i}\n\n$k, $s = [], []\n\n3.upto($n) {|x| if $n % x == 0 then $k.push x end}\n\n$k.reverse!\n\n$k.each do |ki|\n step = $n / ki\n dots = $n / step\n shift = 0\n step.times do\n sum = 0\n i = 0\n dots.times do\n sum += $tis[i * step + shift]\n i += 1\n end\n $s.push sum\n shift += 1\n end \nend\n\n$s.sort! {|a, b| b <=> a}\nputs $s[0]\n"}, {"source_code": "n = gets.to_i\nx = gets.split.map(&:to_i)\nsum, max = 0, -(2**32)\n1.upto(n/3) { |i| if n%i == 0\n for j in (0...i)\n (j..n-1).step(i).map { |k| sum += x[k]} \n max = sum if max < sum\n sum = 0\n end\n end\n }\n puts max"}, {"source_code": "n=gets.to_i\na = gets.split.map(&:to_i)\n\ndef go(a, s)\n ret = -1111111111111\n b = 0\n while b < s\n ans = 0\n i = b\n while i < a.size\n ans += a[i]\n i += s\n end\n\n ret = ans if ans > ret\n b += 1\n end\n ret\n end\n\nans = -111111111\n1.upto(n) do |x|\n next if n % x != 0\n next if n / x < 3\n ret = go(a,x)\n ans = ret if ret > ans\nend\n\nputs ans\n"}, {"source_code": "n = gets.to_i\nt = gets.split.map(&:to_i)\nsum = t.inject(:+)\n(1..n).each do |i|\n next if n / i < 3 || n % i != 0\n (0...i).each do |j|\n k = j\n ssum = 0\n while k < n\n ssum += t[k]\n k += i\n end \n sum = ssum if ssum > sum\n end\nend\nputs sum\n"}, {"source_code": "n = gets.to_i\nt = gets.split.map(&:to_i)\nans = t.inject(:+)\n1.upto(n / 3) do |i|\n next if n % i != 0\n i.times do |j|\n sum = j.step(n - 1, i).map{|x| t[x]}.inject(:+)\n ans = sum if sum > ans\n end\nend\nputs ans\n"}, {"source_code": "n = gets.chomp.to_i\narr = gets.split.collect{|x| x.to_i}\n \nmax = 0\narr.collect{|i| max += i}\n \nfor i in (1..(n/3))\n next if n % i != 0 or n / i < 3\n for j in (0..i-1)\n sum = 0\n (0..n-1).step(i) {|idx|\n sum += arr[idx+j] \n }\n max = sum if sum > max\n end\nend\n\nputs max"}, {"source_code": "n = Integer(gets.chomp)\nseq = gets.chomp.split.map { |x| x.to_i }\nmax = seq.inject(:+)\nfor i in 2..n / 3\n if n % i == 0 && n / i > 2\n for j in 0..i - 1\n sum = 0\n for k in (j...n).step(i)\n sum += seq[k]\n end\n max = sum if sum > max\n end\n end\nend\nputs max"}], "negative_code": [{"source_code": "n=gets.to_i\na=gets.split.map &:to_i\np ((3..n).flat_map{|i|a.each_slice(n/i).inject(0){|j,k|j+k.inject(:+)}unless n%i>0}-[p]).max"}, {"source_code": "n = gets.to_i\nx = gets.split.map(&:to_i)\nsum, max = 0, 0\n1.upto(n/3) { |i| if n%i == 0\n for j in (0...i)\n (j..n-1).step(i).map { |k| sum += x[k]} \n max = sum if max < sum\n sum = 0\n end\n end\n }\n puts max"}, {"source_code": "n=gets.to_i\na = gets.split.map(&:to_i)\n\na.sort!.reverse!\n\nans = 0\ni = 0\nwhile i < a.length\n break if i >= 3 and a[i] < 0\n ans += a[i]\n i += 1\nend\n\nputs ans\n\n"}, {"source_code": "n=gets.to_i\na = gets.split.map(&:to_i)\n\ndef go(a, s)\n ret = 0\n b = 0\n while b < s\n ans = 0\n i = b\n while i < a.size\n ans += a[i]\n i += s\n end\n\n ret = ans if ans > ret\n b += 1\n end\n ret\n end\n\nans = -111111111\n1.upto(n) do |x|\n next if n % x != 0\n next if n / x < 3\n ret = go(a,x)\n ans = ret if ret > ans\nend\n\nputs ans\n"}, {"source_code": "n=gets.to_i\na = gets.split.map(&:to_i)\n\ndef go(a, s)\n ret = 0\n b = 0\n while b < s\n ans = 0\n i = b\n while i < a.size\n ans += a[i]\n i += s\n end\n\n ret = ans if ans > ret\n b += 1\n end\n ret\n end\n\nans = -111111111\n1.upto(n) do |x|\n next if n % x != 0\n ret = go(a,x)\n ans = ret if ret > ans\nend\n\nputs ans\n"}, {"source_code": "n = gets.to_i\nt = gets.split.map(&:to_i)\nsum = t.inject(:+)\nnn = 2 * n ** 0.5\n(0..nn).each do |i|\n next if i != 0 && n / i < 3\n (0...i).each do |j|\n k = j\n ssum = 0\n while k < n\n ssum += t[k]\n k += i\n end \n sum = ssum if ssum > sum\n end\nend\nputs sum\n"}, {"source_code": "n = gets.to_i\nt = gets.split.map(&:to_i)\nsum = t.inject(:+)\nnn = 2 * n ** 0.5\n(0..nn).each do |i|\n (0...i).each do |j|\n k = j\n ssum = 0\n while k < n\n ssum += t[k]\n k += i\n end \n sum = ssum if ssum > sum\n end\nend\nputs sum\n"}, {"source_code": "n = gets.to_i\nt = gets.split.map(&:to_i)\nsum = t.inject(:+)\nnn = 2 * n ** 0.5\n(0..nn).each do |i|\n next if i != 0 && n / i < 2\n (0...i).each do |j|\n k = j\n ssum = 0\n while k < n\n ssum += t[k]\n k += i\n end \n sum = ssum if ssum > sum\n end\nend\nputs sum\n"}, {"source_code": "n = gets.to_i\nt = gets.split.map(&:to_i)\nsum = t.inject(:+)\nnn = 2 * n ** 0.5\n(1..nn).each do |i|\n next if n / i < 3\n (0...i).each do |j|\n k = j\n ssum = 0\n while k < n\n ssum += t[k]\n k += i\n end \n sum = ssum if ssum > sum\n end\nend\nputs sum\n"}, {"source_code": "n = gets.to_i\nt = gets.split.map(&:to_i)\nsum = t.inject(:+)\nnn = 2 * n ** 0.5\n(3..nn).each do |i|\n (0...i).each do |j|\n k = j\n ssum = 0\n while k < n\n ssum += t[k]\n k += i\n end \n sum = ssum if ssum > sum\n end\nend\nputs sum\n"}], "src_uid": "0ff4ac859403db4e29554ca7870f5490"} {"nl": {"description": "You are given a string $$$s$$$ of length $$$n$$$ consisting only of lowercase Latin letters.A substring of a string is a contiguous subsequence of that string. So, string \"forces\" is substring of string \"codeforces\", but string \"coder\" is not.Your task is to calculate the number of ways to remove exactly one substring from this string in such a way that all remaining characters are equal (the number of distinct characters either zero or one).It is guaranteed that there is at least two different characters in $$$s$$$.Note that you can remove the whole string and it is correct. Also note that you should remove at least one character.Since the answer can be rather large (not very large though) print it modulo $$$998244353$$$.If you are Python programmer, consider using PyPy instead of Python when you submit your code.", "input_spec": "The first line of the input contains one integer $$$n$$$ ($$$2 \\le n \\le 2 \\cdot 10^5$$$) — the length of the string $$$s$$$. The second line of the input contains the string $$$s$$$ of length $$$n$$$ consisting only of lowercase Latin letters. It is guaranteed that there is at least two different characters in $$$s$$$.", "output_spec": "Print one integer — the number of ways modulo $$$998244353$$$ to remove exactly one substring from $$$s$$$ in such way that all remaining characters are equal.", "sample_inputs": ["4\nabaa", "7\naacdeee", "2\naz"], "sample_outputs": ["6", "6", "3"], "notes": "NoteLet $$$s[l; r]$$$ be the substring of $$$s$$$ from the position $$$l$$$ to the position $$$r$$$ inclusive.Then in the first example you can remove the following substrings: $$$s[1; 2]$$$; $$$s[1; 3]$$$; $$$s[1; 4]$$$; $$$s[2; 2]$$$; $$$s[2; 3]$$$; $$$s[2; 4]$$$. In the second example you can remove the following substrings: $$$s[1; 4]$$$; $$$s[1; 5]$$$; $$$s[1; 6]$$$; $$$s[1; 7]$$$; $$$s[2; 7]$$$; $$$s[3; 7]$$$. In the third example you can remove the following substrings: $$$s[1; 1]$$$; $$$s[1; 2]$$$; $$$s[2; 2]$$$. "}, "positive_code": [{"source_code": "n = gets().to_i\ns = gets().chomp\ncur = nil\nres = 0\nans = 0\ncount1 = 0\ns.each_char do |c|\n if c == cur\n count1+=1\n else\n res += count1 + 1 if !cur.nil?\n if !cur.nil?\n break\n end\n cur = c\n count1 = 0\n end\nend\nans += res\nres = 0\ncur = nil\ncount = 0\ns.reverse.each_char do |c|\n if c == cur\n count+=1\n else\n res += count + 1 if !cur.nil?\n if !cur.nil?\n break\n end\n cur = c\n count = 0\n end\nend\nans += res\nif (s[0] == s[-1])\n ans += (count + 1) * (count1 + 1)\nend\n\nputs (ans + 1) % 998244353"}, {"source_code": "N = gets.to_i\nS = gets.chomp.split(\"\").map &:intern\nMod = 998244353\nans = 1\nH,T = S[0],S[-1]\nmh = S.index{ |c| c != H }\nmt = N - S.rindex{ |c| c != T } - 1\nans = (ans + mh) % Mod\nans = (ans + mt) % Mod\nif S[0] == S[-1]\n ans = (ans + mh*mt) % Mod\nend\np ans"}, {"source_code": "n = gets.to_i\ns = gets.strip\ni = 1\nwhile i < s.size\n break if s[i] != s[i - 1]\n i += 1\nend\nj = s.size - 2\nj = s.size - 1 if i == s.size\nwhile j >= 0 && j >= i\n break if s[j] != s[j + 1]\n j -= 1\nend\n\nnx = i\nny = s.size - 1 - j\n\nans = nx + ny + 1\nans = (1 + nx)*nx/2 if s[0] == s[-1]\nans = (1 + nx)*(1 + ny) if i <= j && s[0] == s[-1]\np ans%998244353\n"}], "negative_code": [{"source_code": "n = gets().to_i\ns = gets().chomp\ncur = nil\nres = 0\nans = 0\ncount1 = 0\ns.each_char do |c|\n if c == cur\n count1+=1\n else\n res += count1 + 1 if !cur.nil?\n if !cur.nil?\n break\n end\n cur = c\n count1 = 0\n end\nend\nans += res\nres = 0\ncur = nil\ncount = 0\ns.reverse.each_char do |c|\n if c == cur\n count+=1\n else\n res += count + 1 if !cur.nil?\n if !cur.nil?\n break\n end\n cur = c\n count = 0\n end\nend\nans += res\nif (s[0] == s[-1])\n ans += (count + 1) * (count1 + 1)\nend\n\nputs ans + 1"}], "src_uid": "9693f60fc65065d00a1899df999405fe"} {"nl": {"description": "Little boy Petya loves stairs very much. But he is bored from simple going up and down them — he loves jumping over several stairs at a time. As he stands on some stair, he can either jump to the next one or jump over one or two stairs at a time. But some stairs are too dirty and Petya doesn't want to step on them.Now Petya is on the first stair of the staircase, consisting of n stairs. He also knows the numbers of the dirty stairs of this staircase. Help Petya find out if he can jump through the entire staircase and reach the last stair number n without touching a dirty stair once.One has to note that anyway Petya should step on the first and last stairs, so if the first or the last stair is dirty, then Petya cannot choose a path with clean steps only.", "input_spec": "The first line contains two integers n and m (1 ≤ n ≤ 109, 0 ≤ m ≤ 3000) — the number of stairs in the staircase and the number of dirty stairs, correspondingly. The second line contains m different space-separated integers d1, d2, ..., dm (1 ≤ di ≤ n) — the numbers of the dirty stairs (in an arbitrary order).", "output_spec": "Print \"YES\" if Petya can reach stair number n, stepping only on the clean stairs. Otherwise print \"NO\".", "sample_inputs": ["10 5\n2 4 8 3 6", "10 5\n2 4 5 7 9"], "sample_outputs": ["NO", "YES"], "notes": null}, "positive_code": [{"source_code": "n,m=gets.chomp.split.map(&:to_i)\na=gets.chomp.split.map(&:to_i).sort if m>0\nflag=0\nm.times do |i|\n\tif a[i]==1 || a[i]==n\n\t\tflag=1;\n\t\tbreak;\n\telsif i>1 && a[i-1]==a[i]-1 && a[i-2]==a[i]-2\n\t\tflag=1;\n\t\tbreak;\n\tend\nend\nputs (flag==0) ? \"YES\" : \"NO\""}, {"source_code": "n,m = gets.split(\" \").map(&:to_i)\nif m != 0\n\tdirty = gets.split(\" \").map(&:to_i)\n\tdirty.sort!\n\tans = true\n\tdirty.each_cons(3) do |a,b,c|\n\t\t# puts \"ele #{a} #{b} #{c}\"\n\t\t# puts \"diff #{b - a == 0 && c - b == 0}\"\n\t\tif b - a == 1 && c - b == 1\n\t\t\tans = false\n\t\t\tbreak\n\t\tend\n\tend\n\tans = false if dirty.include?(1) || dirty.include?(n)\n\tputs ans ? \"YES\" : \"NO\"\nelse\n\tputs \"YES\"\nend\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\nif m==0 \n puts \"YES\"\n exit\nend\na = gets.split.map(&:to_i).sort\n\nb = true\n\n(2..m-1).each do |i|\n if a[i-2]+2 == a[i] \n b = false\n break\n end\nend\n\nputs a.first != 1 && a.last !=n && b ? \"YES\" : \"NO\""}, {"source_code": "def can(n, m, d)\n return true if m == 0\n return false if d[0] == 1 or d[m - 1] == n\n 0.upto(m - 3) do |i|\n break if i + 2 >= m\n return false if d[i] + 1 == d[i + 1] and d[i + 1] + 1 == d[i + 2]\n end\n return true\nend\n\nn, m = *gets.split.map(&:to_i)\nd = (m == 0) ? [] : gets.split.map(&:to_i).sort\n\nputs can(n, m, d) ? \"YES\" : \"NO\""}, {"source_code": "require 'set'\n\nn,m=gets.split.map(&:to_i)\nif m > 0\n a=gets.split.map(&:to_i).to_set\nelse\n a = Set.new\nend\n\nif a.include?(1) || a.include?(n)\n puts 'NO'\n exit\nend\n\na.each do |x|\n if a.include?(x+1) && a.include?(x+2)\n puts 'NO'\n exit\n end\nend\n\nputs 'YES'"}, {"source_code": "def read_next_line\n gets.split.map { |x| x.to_i }\nend\n\nn, m = read_next_line\nif m == 0\n puts 'YES'\n exit\nend\n\na = read_next_line.sort\nif a[0] == 1 || a[m - 1] == n\n puts 'NO'\n exit\nend\n\nif a.size > 2\n for i in 0...m - 2\n if a[i] == a[i + 1] - 1 && a[i] == a[i + 2] - 2\n puts 'NO'\n exit\n end\n end\nend\nputs 'YES'"}], "negative_code": [{"source_code": "n, m = gets.split.map(&:to_i)\na = gets.split.sort.map(&:to_i)\n\nb = true\n\n(2..m-1).each do |i|\n if a[i-2]+2 == a[i] \n b = false\n break\n end\nend\n\nputs a.first != 1 && a.last !=n && b ? \"YES\" : \"NO\""}], "src_uid": "422cbf106fac3216d58bdc8411c0bf9f"} {"nl": {"description": "Wilbur the pig is tinkering with arrays again. He has the array a1, a2, ..., an initially consisting of n zeros. At one step, he can choose any index i and either add 1 to all elements ai, ai + 1, ... , an or subtract 1 from all elements ai, ai + 1, ..., an. His goal is to end up with the array b1, b2, ..., bn. Of course, Wilbur wants to achieve this goal in the minimum number of steps and asks you to compute this value.", "input_spec": "The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the length of the array ai. Initially ai = 0 for every position i, so this array is not given in the input. The second line of the input contains n integers b1, b2, ..., bn ( - 109 ≤ bi ≤ 109).", "output_spec": "Print the minimum number of steps that Wilbur needs to make in order to achieve ai = bi for all i.", "sample_inputs": ["5\n1 2 3 4 5", "4\n1 2 2 1"], "sample_outputs": ["5", "3"], "notes": "NoteIn the first sample, Wilbur may successively choose indices 1, 2, 3, 4, and 5, and add 1 to corresponding suffixes.In the second sample, Wilbur first chooses indices 1 and 2 and adds 1 to corresponding suffixes, then he chooses index 4 and subtract 1."}, "positive_code": [{"source_code": "n = gets.chomp.to_i\nb = gets.chomp.split.map(&:to_i)\nc = 0\na = b.map do |a|\n t = (a - c).abs\n c = a\n t\nend\nputs a.inject(0, :+)\n"}, {"source_code": "n = gets.chomp.to_i\n\nb = gets.chomp.split.map(&:to_i)\n\nx = 0;\nanswer = 0;\nb.each do |i|\n x = (i-x)\n answer += x.abs\n x= i\n \nend \n\nputs answer"}, {"source_code": "n=gets\n\ndata=gets.chomp.split\n\nb=[]\n\ndata.each do |item|\n\tb.push(item.to_i)\nend\n\na=(b[0]).abs\nfor i in(0..b.length-2)\n\ta+=(b[i]-b[i+1]).abs\nend\n\nputs a"}, {"source_code": "n = gets.chomp.to_i\nas = gets.chomp.split.map(&:to_i)\n\nputs (1...n).to_a.inject (as[0].abs) { |s, i| s += (as[i] - as[i - 1]).abs }\n"}, {"source_code": "def solve(a)\n a.each_cons(2).map{|x,y| (x-y).abs}.reduce(a.first.abs, :+)\nend\n\ngets\na = gets.split.map(&:to_i)\nputs solve a\n"}, {"source_code": "class Array\n def r\n self.map { |q| q.to_i }\n end\nend\n\nn = STDIN.gets.to_i\n\ncur = 0\nres = 0\nSTDIN.gets.split(\" \").to_a.r.each do |i|\n res += (cur - i).abs\n cur = i\nend\n\nputs res\n"}, {"source_code": "n = gets.to_i\na = [0] + gets.split.map(&:to_i)\nputs n.times.map{|i| (a[i + 1] - a[i]).abs}.inject(:+)\n"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split(\" \").map(&:to_i)\ne = 0\nres = 0\nn.times do |i|\n res += (a[i] - e).abs\n e = a[i]\nend\nputs res\n"}, {"source_code": "class Solver\n\tdef main\n\t\tn = gets.to_i\n\t\tb = gets.split.map { |x| x.to_i }\n\t\tlast = 0\n\t\tans = 0\n\t\tb.each do |x|\n\t\t\tx -= last\n\t\t\tans += x.abs\n\t\t\tlast += x\n\t\tend\n\t\tputs ans\n\tend\nend\n\nSolver.new.main"}], "negative_code": [{"source_code": "n=gets\n\ndata=gets.chomp.split\n\nb=[]\n\ndata.each do |item|\n\tb.push(item.to_i)\nend\n\na=b[0]\nfor i in(0..b.length-2)\n\ta+=(b[i]-b[i+1]).abs\nend\n\nputs a"}, {"source_code": "n = STDIN.gets.to_i\n\ncur = 0\nres = 0\nn.times do\n i = STDIN.getc.to_i\n STDIN.getc\n res += (cur - i).abs\n cur = i\nend\n\nputs res\n"}], "src_uid": "97a226f47973fcb39c40e16f66654b5f"} {"nl": {"description": "You have $$$a$$$ coins of value $$$n$$$ and $$$b$$$ coins of value $$$1$$$. You always pay in exact change, so you want to know if there exist such $$$x$$$ and $$$y$$$ that if you take $$$x$$$ ($$$0 \\le x \\le a$$$) coins of value $$$n$$$ and $$$y$$$ ($$$0 \\le y \\le b$$$) coins of value $$$1$$$, then the total value of taken coins will be $$$S$$$.You have to answer $$$q$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$q$$$ ($$$1 \\le q \\le 10^4$$$) — the number of test cases. Then $$$q$$$ test cases follow. The only line of the test case contains four integers $$$a$$$, $$$b$$$, $$$n$$$ and $$$S$$$ ($$$1 \\le a, b, n, S \\le 10^9$$$) — the number of coins of value $$$n$$$, the number of coins of value $$$1$$$, the value $$$n$$$ and the required total value.", "output_spec": "For the $$$i$$$-th test case print the answer on it — YES (without quotes) if there exist such $$$x$$$ and $$$y$$$ that if you take $$$x$$$ coins of value $$$n$$$ and $$$y$$$ coins of value $$$1$$$, then the total value of taken coins will be $$$S$$$, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).", "sample_inputs": ["4\n1 2 3 4\n1 2 3 6\n5 2 6 27\n3 3 5 18"], "sample_outputs": ["YES\nNO\nNO\nYES"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/env ruby\ncount = gets.chomp.to_i\nanswers = []\ncount.times do\n input = gets.chomp.split(' ').map(&:to_i)\n a = input[0] # number of n coins\n b = input[1] # number of 1 coins\n n = input[2] # value of a coins\n sum = input[3] # wanted sum\n\n c = sum / n\n \n answers <<\n if (a <= c && a * n + b >= sum) || (a > c && c * n + b >= sum)\n \"YES\"\n else\n \"NO\"\n end\nend\n\nanswers.each { |answer| puts answer }\n"}, {"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\na = inp[0]\na.times do\n a,b,n,s = inp\n def is_able(useable_a_count, b, s, n)\n used_a_count = [(s/n), useable_a_count].min\n remnant = s - used_a_count * n\n return (remnant <= b)\n end\n puts is_able(a,b,s,n)? \"YES\" : \"NO\"\nend"}, {"source_code": "g = gets.chomp.to_i\ng.times{\na,b,n,s = gets.split(\" \").map{|x|x.to_i}\nmax_a = [(s/n).round,a].min*n\nmax_a+=b\nputs (max_a>=s)?\"YES\":\"NO\"\n}\n"}, {"source_code": "MOD=10**9+7\ncnt=0; sum=0; prev=nil; can=true; h=Hash.new(0)\ndef gs() gets.chomp end\ndef gi() gets.to_i end\ndef gsmi() gets.chomp.split.map(&:to_i) end\n\ndef desc(ar) ar.sort!{|x,y|y<=>x} end\ndef min(a,b) a<=b ? a : b end\ndef max(a,b) a>=b ? a : b end\ndef sum(ar) ar.inject(:+) end\n\ndef C(a,b) b==0||a==b ? 1 : (b=a-b if a/2s\n s=s%n\n end\n ans << (s<=b ? 'YES' : 'NO')\nend\n\nputs ans"}, {"source_code": "gets.to_i.times do\n a, b, n, s = gets.split(' ').map(&:to_i)\n\n p = [a, s.div(n)].min\n\n puts (s - p*n) <= b ? 'YES' : 'NO'\nend\n"}], "negative_code": [{"source_code": "g = gets.chomp.to_i\ng.times{\na,b,n,s = gets.split(\" \").map{|x|x.to_i}\nmax_a = 0\na.times{\nmax_a+=(max_a+n=s)?\"YES\":\"NO\"\n}\n\n"}], "src_uid": "e2434fd5f9d16d59e646b6e69e37684a"} {"nl": {"description": "You are given an array $$$a$$$ consisting of $$$n$$$ integers. You can perform the following operations arbitrary number of times (possibly, zero): Choose a pair of indices $$$(i, j)$$$ such that $$$|i-j|=1$$$ (indices $$$i$$$ and $$$j$$$ are adjacent) and set $$$a_i := a_i + |a_i - a_j|$$$; Choose a pair of indices $$$(i, j)$$$ such that $$$|i-j|=1$$$ (indices $$$i$$$ and $$$j$$$ are adjacent) and set $$$a_i := a_i - |a_i - a_j|$$$. The value $$$|x|$$$ means the absolute value of $$$x$$$. For example, $$$|4| = 4$$$, $$$|-3| = 3$$$.Your task is to find the minimum number of operations required to obtain the array of equal elements and print the order of operations to do it.It is guaranteed that you always can obtain the array of equal elements using such operations.Note that after each operation each element of the current array should not exceed $$$10^{18}$$$ by absolute value.", "input_spec": "The first line of the input contains one integer $$$n$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$0 \\le a_i \\le 2 \\cdot 10^5$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$.", "output_spec": "In the first line print one integer $$$k$$$ — the minimum number of operations required to obtain the array of equal elements. In the next $$$k$$$ lines print operations itself. The $$$p$$$-th operation should be printed as a triple of integers $$$(t_p, i_p, j_p)$$$, where $$$t_p$$$ is either $$$1$$$ or $$$2$$$ ($$$1$$$ means that you perform the operation of the first type, and $$$2$$$ means that you perform the operation of the second type), and $$$i_p$$$ and $$$j_p$$$ are indices of adjacent elements of the array such that $$$1 \\le i_p, j_p \\le n$$$, $$$|i_p - j_p| = 1$$$. See the examples for better understanding. Note that after each operation each element of the current array should not exceed $$$10^{18}$$$ by absolute value. If there are many possible answers, you can print any.", "sample_inputs": ["5\n2 4 6 6 6", "3\n2 8 10", "4\n1 1 1 1"], "sample_outputs": ["2\n1 2 3 \n1 1 2", "2\n2 2 1 \n2 3 2", "0"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nb = Hash.new(0)\nn.times do |i|\n b[a[i]] += 1\nend\nm = a[0]\nb.keys.each do |i|\n m = i if b[m] < b[i]\nend\nans = []\n(n-1).times do |i|\n if a[i] == m && a[i+1] != m\n if a[i] > a[i+1]\n ans << [1,i+2,i+1]\n else\n ans << [2,i+2,i+1]\n end\n a[i+1] = m\n end\nend\n(n-1).downto(1) do |i|\n if a[i] == m && a[i-1] != m\n if a[i] > a[i-1]\n ans << [1,i,i+1]\n else\n ans << [2,i,i+1]\n end\n a[i-1] = m\n end\nend\nputs ans.size\nans.each do |arr|\n puts arr.join(\" \")\nend"}, {"source_code": "gets.chomp\na = gets.chomp.split(\" \").map(&:to_i)\n\nh = Hash.new(0)\na.each { |x| h[x] += 1 }\nh = h.sort_by { |k, v| v }.reverse\nk, v = h.first\nv = a.size - v\n\nputs v\nif v > 0\n base_pos = a.index(k)\n range = base_pos..0\n (range.first).downto(range.last).each do |i|\n if a[i] != k\n puts \"#{a[i] < k ? 1 : 2} #{i+1} #{i+2}\"\n end\n end\n\n for i in base_pos..a.size-1\n if a[i] != k\n puts \"#{a[i] < k ? 1 : 2} #{i+1} #{i}\"\n end\n end\nend\n"}], "negative_code": [{"source_code": "gets.chomp\na = gets.chomp.split(\" \").map(&:to_i)\nprint \"a: #{a}\\n\"\n\nh = Hash.new(0)\na.each { |x| h[x] += 1 }\nh = h.sort_by { |k, v| v }.reverse\nk, v = h.first\nv = a.size - v\n\nputs v\nif v > 0\n base_pos = a.index(k)\n range = base_pos..0\n (range.first).downto(range.last).each do |i|\n if a[i] != k\n puts \"#{a[i] < k ? 1 : 2} #{i+1} #{i+2}\"\n end\n end\n\n for i in base_pos..a.size-1\n if a[i] != k\n puts \"#{a[i] < k ? 1 : 2} #{i+1} #{i}\"\n end\n end\nend\n"}], "src_uid": "732d28ed9092883dccadbd1999308567"} {"nl": {"description": "Beroffice text editor has a wide range of features that help working with text. One of the features is an automatic search for typos and suggestions of how to fix them.Beroffice works only with small English letters (i.e. with 26 letters from a to z). Beroffice thinks that a word is typed with a typo if there are three or more consonants in a row in the word. The only exception is that if the block of consonants has all letters the same, then this block (even if its length is greater than three) is not considered a typo. Formally, a word is typed with a typo if there is a block of not less that three consonants in a row, and there are at least two different letters in this block.For example: the following words have typos: \"hellno\", \"hackcerrs\" and \"backtothefutttture\"; the following words don't have typos: \"helllllooooo\", \"tobeornottobe\" and \"oooooo\". When Beroffice editor finds a word with a typo, it inserts as little as possible number of spaces in this word (dividing it into several words) in such a way that each of the resulting words is typed without any typos.Implement this feature of Beroffice editor. Consider the following letters as the only vowels: 'a', 'e', 'i', 'o' and 'u'. All the other letters are consonants in this problem.", "input_spec": "The only line contains a non-empty word consisting of small English letters. The length of the word is between 1 and 3000 letters.", "output_spec": "Print the given word without any changes if there are no typos. If there is at least one typo in the word, insert the minimum number of spaces into the word so that each of the resulting words doesn't have any typos. If there are multiple solutions, print any of them.", "sample_inputs": ["hellno", "abacaba", "asdfasdf"], "sample_outputs": ["hell no", "abacaba", "asd fasd f"], "notes": null}, "positive_code": [{"source_code": "def pos(ch)\n\tch.ord - 'a'.ord\nend\n\ns = gets.chomp\n\nabc = [0]\n25.times{abc << 1}\nabc[pos('o')] = 0\nabc[pos('e')] = 0\nabc[pos('i')] = 0\nabc[pos('u')] = 0\n\nans = \"\"\n\ncnt = 0\ni = 2\nwhile i < s.size\n\tif abc[pos(s[i])] + abc[pos(s[i - 1])] + abc[pos(s[i - 2])] == 3\n\t\tj = i\n\t\twhile j < s.size && s[j] == s[i - 1]\n\t\t\tj += 1\n\t\tend\n\t\t\t\n\t\tif s[i - 2] == s[i - 1] && j >= i + 1\n\t\t\tans << s[i-2..j-3]\n\t\t\ti = j\n\t\telsif j <= i + 1\n\t\t\tans << s[i-2..i-1] + \" \"\n\t\t\ti += 2\n\t\telse \n\t\t\tans << s[i - 2] + \" \"\n\t\t\ti += 1\n\t\tend\n\telse \n\t\tif abc[pos(s[i])] + abc[pos(s[i - 1])] == 2\n\t\t\tans << s[i - 2]\n\t\t\ti += 1\n\t\telsif abc[pos(s[i])] == 1\n\t\t\tans << s[i-2..i-1]\n\t\t\ti += 2\n\t\telse \n\t\t\tans << s[i-2..i]\n\t\t\ti += 3\n\t\tend\n\tend\nend\n\nputs ans + s[i-2..-1]"}], "negative_code": [], "src_uid": "436c00c832de8df739fc391f2ed6dac4"} {"nl": {"description": "Several days ago you bought a new house and now you are planning to start a renovation. Since winters in your region can be very cold you need to decide how to heat rooms in your house.Your house has $$$n$$$ rooms. In the $$$i$$$-th room you can install at most $$$c_i$$$ heating radiators. Each radiator can have several sections, but the cost of the radiator with $$$k$$$ sections is equal to $$$k^2$$$ burles.Since rooms can have different sizes, you calculated that you need at least $$$sum_i$$$ sections in total in the $$$i$$$-th room. For each room calculate the minimum cost to install at most $$$c_i$$$ radiators with total number of sections not less than $$$sum_i$$$.", "input_spec": "The first line contains single integer $$$n$$$ ($$$1 \\le n \\le 1000$$$) — the number of rooms. Each of the next $$$n$$$ lines contains the description of some room. The $$$i$$$-th line contains two integers $$$c_i$$$ and $$$sum_i$$$ ($$$1 \\le c_i, sum_i \\le 10^4$$$) — the maximum number of radiators and the minimum total number of sections in the $$$i$$$-th room, respectively.", "output_spec": "For each room print one integer — the minimum possible cost to install at most $$$c_i$$$ radiators with total number of sections not less than $$$sum_i$$$.", "sample_inputs": ["4\n1 10000\n10000 1\n2 6\n4 6"], "sample_outputs": ["100000000\n1\n18\n10"], "notes": "NoteIn the first room, you can install only one radiator, so it's optimal to use the radiator with $$$sum_1$$$ sections. The cost of the radiator is equal to $$$(10^4)^2 = 10^8$$$.In the second room, you can install up to $$$10^4$$$ radiators, but since you need only one section in total, it's optimal to buy one radiator with one section.In the third room, there $$$7$$$ variants to install radiators: $$$[6, 0]$$$, $$$[5, 1]$$$, $$$[4, 2]$$$, $$$[3, 3]$$$, $$$[2, 4]$$$, $$$[1, 5]$$$, $$$[0, 6]$$$. The optimal variant is $$$[3, 3]$$$ and it costs $$$3^2+ 3^2 = 18$$$."}, "positive_code": [{"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\n\nrequire 'prime'\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\ndef big(a,b) return (a>b)? a:b end\n\ndef small(a,b) return (a 0\n duration += b if po[j] == 'w'\n break if t + duration > max_time\n t += duration\n j += 1\nend\n\nmax_count = j - i\nunless max_count == 0 || max_count == n\n i = n\n while i > 0 && j >= 1\n i -= 1\n duration = 1 + a\n duration += b if po[i] == 'w'\n\n t += duration\n while t + a*[n-i,j-1].min > max_time\n j -= 1\n t -= 1 + a\n t -= b if po[j] == 'w'\n end\n max_count = [max_count, (j-i) + n].max\n end\nend\n\nputs max_count\n"}], "negative_code": [{"source_code": "#!/usr/bin/ruby\nn, a, b, max_time = gets().split(' ').map(&:to_i)\npo = gets().chars.to_a\n\ni = 0\nj = 0\nt = 0\nmax_count = 0\nwhile j < n\n duration = 1\n duration += a if j > 0\n duration += b if po[j] == 'w'\n break if t + duration > max_time\n t += duration\n j += 1\nend\n\nmax_count = j - i\nunless max_count == 0 || max_count == n\n i = n\n while i > 0 && j >= 0\n i -= 1\n duration = 1 + a\n duration += b if po[i] == 'w'\n\n t += duration\n while t + a*[n-i,j-1].min > max_time\n j -= 1\n t -= 1 + a\n t -= b if po[j] == 'w'\n end\n max_count = [max_count, (j-i) + n].max\n end\nend\n\nputs max_count\n"}], "src_uid": "e53eabd41647dc17bd8daf060d736c63"} {"nl": {"description": "Bearland has n cities, numbered 1 through n. Cities are connected via bidirectional roads. Each road connects two distinct cities. No two roads connect the same pair of cities.Bear Limak was once in a city a and he wanted to go to a city b. There was no direct connection so he decided to take a long walk, visiting each city exactly once. Formally: There is no road between a and b. There exists a sequence (path) of n distinct cities v1, v2, ..., vn that v1 = a, vn = b and there is a road between vi and vi + 1 for . On the other day, the similar thing happened. Limak wanted to travel between a city c and a city d. There is no road between them but there exists a sequence of n distinct cities u1, u2, ..., un that u1 = c, un = d and there is a road between ui and ui + 1 for .Also, Limak thinks that there are at most k roads in Bearland. He wonders whether he remembers everything correctly.Given n, k and four distinct cities a, b, c, d, can you find possible paths (v1, ..., vn) and (u1, ..., un) to satisfy all the given conditions? Find any solution or print -1 if it's impossible.", "input_spec": "The first line of the input contains two integers n and k (4 ≤ n ≤ 1000, n - 1 ≤ k ≤ 2n - 2) — the number of cities and the maximum allowed number of roads, respectively. The second line contains four distinct integers a, b, c and d (1 ≤ a, b, c, d ≤ n).", "output_spec": "Print -1 if it's impossible to satisfy all the given conditions. Otherwise, print two lines with paths descriptions. The first of these two lines should contain n distinct integers v1, v2, ..., vn where v1 = a and vn = b. The second line should contain n distinct integers u1, u2, ..., un where u1 = c and un = d. Two paths generate at most 2n - 2 roads: (v1, v2), (v2, v3), ..., (vn - 1, vn), (u1, u2), (u2, u3), ..., (un - 1, un). Your answer will be considered wrong if contains more than k distinct roads or any other condition breaks. Note that (x, y) and (y, x) are the same road.", "sample_inputs": ["7 11\n2 4 7 3", "1000 999\n10 20 30 40"], "sample_outputs": ["2 7 1 3 6 5 4\n7 1 5 4 6 2 3", "-1"], "notes": "NoteIn the first sample test, there should be 7 cities and at most 11 roads. The provided sample solution generates 10 roads, as in the drawing. You can also see a simple path of length n between 2 and 4, and a path between 7 and 3. "}, "positive_code": [{"source_code": "n, k = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\nif k < n + 1 || n <= 4\n puts -1\nelse\n seq_1 = [a[0], a[2]] + ( (1..n).to_a - a ) + [a[3], a[1]]\n seq_2 = seq_1[0..1].reverse + seq_1[2..-3] + seq_1[-2..-1].reverse\n puts seq_1.join(' ')\n puts seq_2.join(' ')\nend"}, {"source_code": "n, k = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\nif k < n + 1 || n <= 4\n puts -1\nelse\n seq_1 = [a[0], a[2]] + ( (1..n).to_a - a ) + [a[3], a[1]]\n seq_2 = seq_1[0..1].reverse + seq_1[2..-3] + seq_1[-2..-1].reverse\n puts seq_1.join(' ')\n puts seq_2.join(' ')\nend\n"}, {"source_code": "n, k = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\nif k < n + 1 || n <= 4\n puts -1\nelse\n seq_1 = [a[0], a[2]] + ( (1..n).to_a - a ) + [a[3], a[1]]\n seq_2 = seq_1[0..1].reverse + seq_1[2..-3] + seq_1[-2..-1].reverse\n puts seq_1.join(' ')\n puts seq_2.join(' ')\nend"}], "negative_code": [], "src_uid": "6b398790adbd26dd9af64e9086e38f7f"} {"nl": {"description": "Vlad likes to eat in cafes very much. During his life, he has visited cafes n times. Unfortunately, Vlad started to feel that his last visits are not any different from each other. To fix that Vlad had a small research.First of all, Vlad assigned individual indices to all cafes. Then, he wrote down indices of cafes he visited in a row, in order of visiting them. Now, Vlad wants to find such a cafe that his last visit to that cafe was before his last visits to every other cafe. In other words, he wants to find such a cafe that he hasn't been there for as long as possible. Help Vlad to find that cafe.", "input_spec": "In first line there is one integer n (1 ≤ n ≤ 2·105) — number of cafes indices written by Vlad. In second line, n numbers a1, a2, ..., an (0 ≤ ai ≤ 2·105) are written — indices of cafes in order of being visited by Vlad. Vlad could visit some cafes more than once. Note that in numeration, some indices could be omitted.", "output_spec": "Print one integer — index of the cafe that Vlad hasn't visited for as long as possible.", "sample_inputs": ["5\n1 3 2 1 2", "6\n2 1 2 2 4 1"], "sample_outputs": ["3", "2"], "notes": "NoteIn first test, there are three cafes, and the last visits to cafes with indices 1 and 2 were after the last visit to cafe with index 3; so this cafe is the answer. In second test case, there are also three cafes, but with indices 1, 2 and 4. Cafes with indices 1 and 4 were visited after the last visit of cafe with index 2, so the answer is 2. Note that Vlad could omit some numbers while numerating the cafes."}, "positive_code": [{"source_code": "require 'Set'\n\nn = gets.to_i\na = gets.split().map(&:to_i)\ns = a.uniq.length\n\nwas = Set.new\nans = -1\nfor i in a.reverse\n unless was.length == s\n if !was.include?(i)\n was.add(i)\n ans = i\n end\n end\nend\n\nputs ans\n"}, {"source_code": "n = gets.to_i\na = gets.split.collect(&:to_i)\n#s = \"2 1 2 2 4 1\"\n#a = s.split.collect(&:to_i)\nb = Array.new(2000001) { |i| 10000000 }\n\n\na.each_index{|t| \n\tb[a[t]] = t\n}\n#puts b\nputs a[b.min]"}, {"source_code": "gets\nx=gets.split.map{|e| e.to_i}.reverse\nhs={}\nans=-1\nx.each{|e|\n\tif !hs.key?(e)\n\t\tans=e\n\t\ths[e]=0\n\tend\n}\nputs ans"}], "negative_code": [], "src_uid": "bdea209c7b628e4cc90ebc2572826485"} {"nl": {"description": "Bessie has way too many friends because she is everyone's favorite cow! Her new friend Rabbit is trying to hop over so they can play! More specifically, he wants to get from $$$(0,0)$$$ to $$$(x,0)$$$ by making multiple hops. He is only willing to hop from one point to another point on the 2D plane if the Euclidean distance between the endpoints of a hop is one of its $$$n$$$ favorite numbers: $$$a_1, a_2, \\ldots, a_n$$$. What is the minimum number of hops Rabbit needs to get from $$$(0,0)$$$ to $$$(x,0)$$$? Rabbit may land on points with non-integer coordinates. It can be proved that Rabbit can always reach his destination.Recall that the Euclidean distance between points $$$(x_i, y_i)$$$ and $$$(x_j, y_j)$$$ is $$$\\sqrt{(x_i-x_j)^2+(y_i-y_j)^2}$$$.For example, if Rabbit has favorite numbers $$$1$$$ and $$$3$$$ he could hop from $$$(0,0)$$$ to $$$(4,0)$$$ in two hops as shown below. Note that there also exists other valid ways to hop to $$$(4,0)$$$ in $$$2$$$ hops (e.g. $$$(0,0)$$$ $$$\\rightarrow$$$ $$$(2,-\\sqrt{5})$$$ $$$\\rightarrow$$$ $$$(4,0)$$$). Here is a graphic for the first example. Both hops have distance $$$3$$$, one of Rabbit's favorite numbers. In other words, each time Rabbit chooses some number $$$a_i$$$ and hops with distance equal to $$$a_i$$$ in any direction he wants. The same number can be used multiple times.", "input_spec": "The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 1000$$$)  — the number of test cases. Next $$$2t$$$ lines contain test cases — two lines per test case. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \\le n \\le 10^5$$$, $$$1 \\le x \\le 10^9$$$)  — the number of favorite numbers and the distance Rabbit wants to travel, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$)  — Rabbit's favorite numbers. It is guaranteed that the favorite numbers are distinct. It is guaranteed that the sum of $$$n$$$ over all the test cases will not exceed $$$10^5$$$.", "output_spec": "For each test case, print a single integer — the minimum number of hops needed.", "sample_inputs": ["4\n\n2 4\n\n1 3\n\n3 12\n\n3 4 5\n\n1 5\n\n5\n\n2 10\n\n15 4"], "sample_outputs": ["2\n3\n1\n2"], "notes": "NoteThe first test case of the sample is shown in the picture above. Rabbit can hop to $$$(2,\\sqrt{5})$$$, then to $$$(4,0)$$$ for a total of two hops. Each hop has a distance of $$$3$$$, which is one of his favorite numbers.In the second test case of the sample, one way for Rabbit to hop $$$3$$$ times is: $$$(0,0)$$$ $$$\\rightarrow$$$ $$$(4,0)$$$ $$$\\rightarrow$$$ $$$(8,0)$$$ $$$\\rightarrow$$$ $$$(12,0)$$$.In the third test case of the sample, Rabbit can hop from $$$(0,0)$$$ to $$$(5,0)$$$.In the fourth test case of the sample, Rabbit can hop: $$$(0,0)$$$ $$$\\rightarrow$$$ $$$(5,10\\sqrt{2})$$$ $$$\\rightarrow$$$ $$$(10,0)$$$."}, "positive_code": [{"source_code": "T = gets.to_i\n\nT.times do\n n, x = gets.split.map(&:to_i)\n a = gets.split.map(&:to_i)\n\n ans = 0\n m = a.max\n c = [0, (x / m) - 1].max\n remain = x - m * c\n\n ans += c\n\n if remain % m == 0\n ans += remain / m\n else\n if a.find { |x| x == remain }\n ans += 1\n else\n ans += 2\n end\n end\n\n puts ans\nend\n"}], "negative_code": [], "src_uid": "b6a7e8abc747bdcee74653817085002c"} {"nl": {"description": "There is an infinite set generated as follows: $$$1$$$ is in this set. If $$$x$$$ is in this set, $$$x \\cdot a$$$ and $$$x+b$$$ both are in this set. For example, when $$$a=3$$$ and $$$b=6$$$, the five smallest elements of the set are: $$$1$$$, $$$3$$$ ($$$1$$$ is in this set, so $$$1\\cdot a=3$$$ is in this set), $$$7$$$ ($$$1$$$ is in this set, so $$$1+b=7$$$ is in this set), $$$9$$$ ($$$3$$$ is in this set, so $$$3\\cdot a=9$$$ is in this set), $$$13$$$ ($$$7$$$ is in this set, so $$$7+b=13$$$ is in this set). Given positive integers $$$a$$$, $$$b$$$, $$$n$$$, determine if $$$n$$$ is in this set.", "input_spec": "The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1\\leq t\\leq 10^5$$$) — the number of test cases. The description of the test cases follows. The only line describing each test case contains three integers $$$n$$$, $$$a$$$, $$$b$$$ ($$$1\\leq n,a,b\\leq 10^9$$$) separated by a single space.", "output_spec": "For each test case, print \"Yes\" if $$$n$$$ is in this set, and \"No\" otherwise. You can print each letter in any case.", "sample_inputs": ["5\n24 3 5\n10 3 6\n2345 1 4\n19260817 394 485\n19260817 233 264"], "sample_outputs": ["Yes\nNo\nYes\nNo\nYes"], "notes": "NoteIn the first test case, $$$24$$$ is generated as follows: $$$1$$$ is in this set, so $$$3$$$ and $$$6$$$ are in this set; $$$3$$$ is in this set, so $$$9$$$ and $$$8$$$ are in this set; $$$8$$$ is in this set, so $$$24$$$ and $$$13$$$ are in this set. Thus we can see $$$24$$$ is in this set.The five smallest elements of the set in the second test case is described in statements. We can see that $$$10$$$ isn't among them."}, "positive_code": [{"source_code": "gets\r\nwhile line = gets\r\n n, a, b = line.split.map(&:to_i)\r\n check = lambda do\r\n loop.reduce(1) do |s|\r\n return true if s % b == n % b\r\n return false if s > n / a || a == 1\r\n s * a\r\n end\r\n end\r\n puts check.call ? 'Yes' : 'No'\r\nend"}, {"source_code": "gets\r\nwhile line = gets\r\n n, a, b = line.split.map(&:to_i)\r\n check = lambda do\r\n if a == 1\r\n return a <= n && a % b == n % b\r\n end\r\n loop.reduce(1) do |s|\r\n return true if s % b == n % b\r\n return false if s > n / a\r\n s * a\r\n end\r\n end\r\n puts check.call ? 'Yes' : 'No'\r\nend"}, {"source_code": "gets.to_i.times do\r\n n,a,b=gets.split.map(&:to_i)\r\n r=n%b\r\n if n==1\r\n puts \"Yes\"\r\n elsif b==1\r\n puts \"Yes\"\r\n elsif a==1\r\n if r==1\r\n puts \"Yes\"\r\n else\r\n puts \"No\"\r\n end\r\n else\r\n x=1\r\n f=false\r\n while x<=n\r\n if x%b==r\r\n puts \"Yes\"\r\n f=true\r\n break\r\n end\r\n x*=a\r\n end\r\n puts \"No\" if !f\r\n end\r\nend\r\n"}, {"source_code": "def res\r\n return \"Yes\" if $n == 1\r\n sh = [1]\r\n x = $a\r\n #count = 0\r\n until x > $n or $a == 1\r\n return \"Yes\" if $n == x\r\n # count += 1\r\n sh.push x\r\n x *= $a\r\n end\r\n #puts \"#{sh} #{$n} -- #{count}\"\r\n sh.each { |k|\r\n x = $n - k\r\n return \"Yes\" if x % $b == 0\r\n }\r\n return \"No\"\r\nend\r\n\r\n\r\nt = gets.to_i\r\nt.times { |u|\r\n $n, $a, $b = gets.chomp.split(\" \").map(&:to_i)\r\n puts res\r\n}"}], "negative_code": [{"source_code": "gets\r\nwhile line = gets\r\n n, a, b = line.split.map(&:to_i)\r\n check = lambda do\r\n return n % b == 1 if a == 1\r\n loop.reduce(1) do |s|\r\n return true if s % b == n % b\r\n return false if s > n / a\r\n s * a\r\n end\r\n end\r\n puts check.call ? 'Yes' : 'No'\r\nend"}, {"source_code": "gets\r\nwhile line = gets\r\n n, a, b = line.split.map(&:to_i)\r\n check = lambda do\r\n return true if a == 1\r\n loop.reduce(1) do |s|\r\n return true if s % b == n % b\r\n return false if s > n / a\r\n s * a\r\n end\r\n end\r\n puts check.call ? 'Yes' : 'No'\r\nend"}, {"source_code": "gets.to_i.times do\r\n n,a,b=gets.split.map(&:to_i)\r\n r=n%b\r\n if a==1\r\n if r==1\r\n puts \"Yes\"\r\n else\r\n puts \"No\"\r\n end\r\n elsif b==1\r\n puts \"Yes\"\r\n else\r\n x=1\r\n f=false\r\n while x<=n\r\n if x%b==r\r\n puts \"Yes\"\r\n f=true\r\n break\r\n end\r\n x*=a\r\n end\r\n puts \"No\" if !f\r\n end\r\nend\r\n"}], "src_uid": "e0a3c678f6d1d89420c8162b0ddfcef7"} {"nl": {"description": "Zane the wizard is going to perform a magic show shuffling the cups.There are n cups, numbered from 1 to n, placed along the x-axis on a table that has m holes on it. More precisely, cup i is on the table at the position x = i.The problematic bone is initially at the position x = 1. Zane will confuse the audience by swapping the cups k times, the i-th time of which involves the cups at the positions x = ui and x = vi. If the bone happens to be at the position where there is a hole at any time, it will fall into the hole onto the ground and will not be affected by future swapping operations.Do not forget that Zane is a wizard. When he swaps the cups, he does not move them ordinarily. Instead, he teleports the cups (along with the bone, if it is inside) to the intended positions. Therefore, for example, when he swaps the cup at x = 4 and the one at x = 6, they will not be at the position x = 5 at any moment during the operation. Zane’s puppy, Inzane, is in trouble. Zane is away on his vacation, and Inzane cannot find his beloved bone, as it would be too exhausting to try opening all the cups. Inzane knows that the Codeforces community has successfully helped Zane, so he wants to see if it could help him solve his problem too. Help Inzane determine the final position of the bone.", "input_spec": "The first line contains three integers n, m, and k (2 ≤ n ≤ 106, 1 ≤ m ≤ n, 1 ≤ k ≤ 3·105) — the number of cups, the number of holes on the table, and the number of swapping operations, respectively. The second line contains m distinct integers h1, h2, ..., hm (1 ≤ hi ≤ n) — the positions along the x-axis where there is a hole on the table. Each of the next k lines contains two integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — the positions of the cups to be swapped.", "output_spec": "Print one integer — the final position along the x-axis of the bone.", "sample_inputs": ["7 3 4\n3 4 6\n1 2\n2 5\n5 7\n7 1", "5 1 2\n2\n1 2\n2 4"], "sample_outputs": ["1", "2"], "notes": "NoteIn the first sample, after the operations, the bone becomes at x = 2, x = 5, x = 7, and x = 1, respectively.In the second sample, after the first operation, the bone becomes at x = 2, and falls into the hole onto the ground."}, "positive_code": [{"source_code": "n, m, k = gets.split(\" \").map(&:to_i)\nh = []\ngets.split(\" \").map(&:to_i).each {|i| h[i] = true}\ncur = 1\nif h[cur]\n p cur\n exit\nend\nk.times do\n u, v = gets.split(\" \").map(&:to_i)\n if cur == u\n cur = v\n elsif cur == v\n cur = u\n end\n if h[cur]\n p cur\n exit\n end\nend\n\np cur"}, {"source_code": "(n,_,_),f,*x=$<.map{|x|x.split.map &:to_i}\nary = Array.new(n+1, false)\nf.each{|v|ary[v] = true}\ncur = 1\nfor z in x\n if ary[cur]\n p cur\n exit 0\n end\n a, b = z\n if a == cur\n cur = b\n elsif cur == b\n cur = a\n end\nend\np cur"}, {"source_code": "n,_,k=gets.split.map &:to_i\nary = Array.new(n+1, false)\ngets.split.map(&:to_i).each{|v|ary[v] = true}\ncur = 1\nk.times {\n if ary[cur]\n puts cur\n exit 0\n end\n a, b = gets.split.map(&:to_i)\n if a == cur\n cur = b\n elsif cur == b\n cur = a\n end\n}\nputs cur"}, {"source_code": "(n,_,_),f,*x=$<.map{|x|x.split.map &:to_i}\nary = [false]*(n+1)\nf.each{|v|ary[v] = true}\ncur = 1\nfor z in x \n if ary[cur]\n p cur\n exit 0\n end\n a, b = z\n if a == cur\n cur = b\n elsif cur == b\n cur = a\n end\nend\np cur"}, {"source_code": "require 'set'\nn, m, k = gets.split.map(&:to_i)\nif m * 20 < n then\n mm, x = gets.split.to_set, '1'\n begin\n break if mm.member?(x)\n u, v = gets.split\n if x == u\n x = v\n elsif x == v\n x = u\n end\n k -= 1\n end until k == 0\n puts(x)\nelse\n mm, x = [false] * (n + 1), 1\n gets.split.map(&:to_i).each {|t| mm[t] = true}\n begin\n break if mm[x]\n u, v = gets.split.map(&:to_i)\n if x == u\n x = v\n elsif x == v\n x = u\n end\n k -= 1\n end until k == 0\n puts(x)\nend\n\n"}, {"source_code": "n, _, k = gets.split.map(&:to_i)\nh = gets.split.map(&:to_i)\nary = Array.new(n, false)\nh.each {|v|\n ary[v - 1] = true;\n}\ncur = 0\nk.times {\n if ary[cur]\n puts cur + 1\n exit 0\n end\n a, b = gets.split.map(&:to_i)\n a -= 1\n b -= 1\n if a == cur\n cur = b\n elsif cur == b\n cur = a\n end\n}\nputs cur + 1\n"}, {"source_code": "n,m,k=gets.split.map(&:to_i)\nh=gets.split.map(&:to_i)\nholes=Array.new(n)\nh.each{|i|holes[i-1]=true}\nif holes[0]\n\tputs 1\n\texit\nend\npos=0\nk.times{\n\tu,v=gets.split.map(&:to_i)\n\tu-=1\n\tv-=1\n\tto=0\n\tif u==pos\n\t\tto=v\n\telsif v==pos\n\t\tto=u\n\telse\n\t\tnext\n\tend\n\tif holes[to]\n\t\tputs to+1\n\t\texit\n\tend\n\tpos=to\n}\n\nputs pos+1"}], "negative_code": [{"source_code": "require 'set'\nn, m, k = gets.split.map(&:to_i)\nif m * 20 < n then\n mm, x = gets.split.to_set, '1'\n begin\n break if mm.member?(x)\n u, v = gets.split\n if x == u\n x = v\n elsif x == v\n x = u\n end\n k -= 1\n end until k == 0\n puts(x)\nelse\n mm, x = [false] * (n + 1), 1\n gets.split.map(&:to_i).each {|x| mm[x] = true}\n begin\n break if mm[x]\n u, v = gets.split\n if x == u\n x = v\n elsif x == v\n x = u\n end\n k -= 1\n end until k == 0\n puts(x)\nend\n\n"}], "src_uid": "1f41c017102f4a997be324a4ec9b7fd6"} {"nl": {"description": "You are given an array of $$$n$$$ integers $$$a_1,a_2,\\dots,a_n$$$.You have to create an array of $$$n$$$ integers $$$b_1,b_2,\\dots,b_n$$$ such that: The array $$$b$$$ is a rearrangement of the array $$$a$$$, that is, it contains the same values and each value appears the same number of times in the two arrays. In other words, the multisets $$$\\{a_1,a_2,\\dots,a_n\\}$$$ and $$$\\{b_1,b_2,\\dots,b_n\\}$$$ are equal.For example, if $$$a=[1,-1,0,1]$$$, then $$$b=[-1,1,1,0]$$$ and $$$b=[0,1,-1,1]$$$ are rearrangements of $$$a$$$, but $$$b=[1,-1,-1,0]$$$ and $$$b=[1,0,2,-3]$$$ are not rearrangements of $$$a$$$. For all $$$k=1,2,\\dots,n$$$ the sum of the first $$$k$$$ elements of $$$b$$$ is nonzero. Formally, for all $$$k=1,2,\\dots,n$$$, it must hold $$$$$$b_1+b_2+\\cdots+b_k\\not=0\\,.$$$$$$ If an array $$$b_1,b_2,\\dots, b_n$$$ with the required properties does not exist, you have to print NO.", "input_spec": "Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1\\le t \\le 1000$$$) — the number of test cases. The description of the test cases follows. The first line of each testcase contains one integer $$$n$$$ ($$$1\\le n\\le 50$$$)  — the length of the array $$$a$$$. The second line of each testcase contains $$$n$$$ integers $$$a_1,a_2,\\dots, a_n$$$ ($$$-50\\le a_i\\le 50$$$)  — the elements of $$$a$$$.", "output_spec": "For each testcase, if there is not an array $$$b_1,b_2,\\dots,b_n$$$ with the required properties, print a single line with the word NO. Otherwise print a line with the word YES, followed by a line with the $$$n$$$ integers $$$b_1,b_2,\\dots,b_n$$$. If there is more than one array $$$b_1,b_2,\\dots,b_n$$$ satisfying the required properties, you can print any of them.", "sample_inputs": ["4\n4\n1 -2 3 -4\n3\n0 0 0\n5\n1 -1 1 -1 1\n6\n40 -31 -9 0 13 -40"], "sample_outputs": ["YES\n1 -2 3 -4\nNO\nYES\n1 1 -1 1 -1\nYES\n-40 13 40 0 -9 -31"], "notes": "NoteExplanation of the first testcase: An array with the desired properties is $$$b=[1,-2,3,-4]$$$. For this array, it holds: The first element of $$$b$$$ is $$$1$$$. The sum of the first two elements of $$$b$$$ is $$$-1$$$. The sum of the first three elements of $$$b$$$ is $$$2$$$. The sum of the first four elements of $$$b$$$ is $$$-2$$$. Explanation of the second testcase: Since all values in $$$a$$$ are $$$0$$$, any rearrangement $$$b$$$ of $$$a$$$ will have all elements equal to $$$0$$$ and therefore it clearly cannot satisfy the second property described in the statement (for example because $$$b_1=0$$$). Hence in this case the answer is NO.Explanation of the third testcase: An array with the desired properties is $$$b=[1, 1, -1, 1, -1]$$$. For this array, it holds: The first element of $$$b$$$ is $$$1$$$. The sum of the first two elements of $$$b$$$ is $$$2$$$. The sum of the first three elements of $$$b$$$ is $$$1$$$. The sum of the first four elements of $$$b$$$ is $$$2$$$. The sum of the first five elements of $$$b$$$ is $$$1$$$. Explanation of the fourth testcase: An array with the desired properties is $$$b=[-40,13,40,0,-9,-31]$$$. For this array, it holds: The first element of $$$b$$$ is $$$-40$$$. The sum of the first two elements of $$$b$$$ is $$$-27$$$. The sum of the first three elements of $$$b$$$ is $$$13$$$. The sum of the first four elements of $$$b$$$ is $$$13$$$. The sum of the first five elements of $$$b$$$ is $$$4$$$. The sum of the first six elements of $$$b$$$ is $$$-27$$$. "}, "positive_code": [{"source_code": "T = gets.to_i\n\nT.times do\n n = gets.to_i\n a = gets.split.map(&:to_i).sort\n sum = a.sum\n\n if sum == 0\n puts 'NO'\n elsif sum > 0\n puts 'YES'\n puts a.reverse.join(' ')\n else\n puts 'YES'\n puts a.join(' ')\n end\nend\n"}], "negative_code": [], "src_uid": "e57345f5757654749b411727ebb99c80"} {"nl": {"description": "Peter has a sequence of integers a1, a2, ..., an. Peter wants all numbers in the sequence to equal h. He can perform the operation of \"adding one on the segment [l, r]\": add one to all elements of the sequence with indices from l to r (inclusive). At that, Peter never chooses any element as the beginning of the segment twice. Similarly, Peter never chooses any element as the end of the segment twice. In other words, for any two segments [l1, r1] and [l2, r2], where Peter added one, the following inequalities hold: l1 ≠ l2 and r1 ≠ r2.How many distinct ways are there to make all numbers in the sequence equal h? Print this number of ways modulo 1000000007 (109 + 7). Two ways are considered distinct if one of them has a segment that isn't in the other way.", "input_spec": "The first line contains two integers n, h (1 ≤ n, h ≤ 2000). The next line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 2000).", "output_spec": "Print a single integer — the answer to the problem modulo 1000000007 (109 + 7).", "sample_inputs": ["3 2\n1 1 1", "5 1\n1 1 1 1 1", "4 3\n3 2 1 1"], "sample_outputs": ["4", "1", "0"], "notes": null}, "positive_code": [{"source_code": "n, h = gets.chomp.split.map(&:to_i)\na = gets.chomp.split.map(&:to_i)\nmod = 1000000007\ndp = Array.new(n,0)\ndp[0]=1\nfor i in 0...n do\n tdp = Array.new(n,0)\n nd = h-a[i]\n tdp[nd] = ( tdp[nd] + dp[nd] ) % mod if nd >= 0 && nd < n\n tdp[nd-1] = ( tdp[nd-1] + dp[nd] * nd ) % mod if nd-1 >= 0 && nd < n\n tdp[nd] = ( tdp[nd] + dp[nd-1] ) % mod if nd-1 >= 0 && nd < n\n tdp[nd-1] = ( tdp[nd-1] + dp[nd-1] ) % mod if nd-1 >= 0\n tdp[nd-1] = ( tdp[nd-1] + dp[nd-1] * (nd-1) ) % mod if nd-1 >= 0 && nd < n\n dp = tdp\nend\nputs dp[0]\n"}], "negative_code": [{"source_code": "n, h = gets.chomp.split.map(&:to_i)\na = gets.chomp.split.map(&:to_i)\nmod = 1000000007\ndp = Array.new(n,0)\ndp[0]=1\nfor i in 0...n do\n tdp = Array.new(n,0)\n nd = h-a[i]\n tdp[nd] = ( tdp[nd] + dp[nd] ) % mod if nd >= 0 && nd < n\n tdp[nd-1] = ( tdp[nd-1] + dp[nd] ) % mod if nd-1 >= 0 && nd < n\n tdp[nd] = ( tdp[nd] + dp[nd-1] ) % mod if nd-1 >= 0 && nd < n\n tdp[nd-1] = ( tdp[nd-1] + dp[nd-1] ) % mod if nd-1 >= 0\n dp = tdp\nend\nputs dp[0]\n"}, {"source_code": "n, h = gets.chomp.split.map(&:to_i)\na = gets.chomp.split.map(&:to_i)\nmod = 1000000007\ndp = Array.new(n,0)\ndp[0]=1\nfor i in 0...n do\n tdp = Array.new(n,0)\n nd = h-a[i]\n tdp[nd] = ( tdp[nd] + dp[nd] ) % mod if nd >= 0 && nd < n\n tdp[nd-1] = ( tdp[nd-1] + dp[nd] * nd ) % mod if nd-1 >= 0 && nd < n\n tdp[nd] = ( tdp[nd] + dp[nd-1] ) % mod if nd-1 >= 0 && nd < n\n tdp[nd-1] = ( tdp[nd-1] + dp[nd-1] ) % mod if nd-1 >= 0\n dp = tdp\nend\nputs dp[0]\n"}], "src_uid": "27eac8ca184bf08aa724d49057bfba71"} {"nl": {"description": "Let's call an undirected graph $$$G = (V, E)$$$ relatively prime if and only if for each edge $$$(v, u) \\in E$$$  $$$GCD(v, u) = 1$$$ (the greatest common divisor of $$$v$$$ and $$$u$$$ is $$$1$$$). If there is no edge between some pair of vertices $$$v$$$ and $$$u$$$ then the value of $$$GCD(v, u)$$$ doesn't matter. The vertices are numbered from $$$1$$$ to $$$|V|$$$.Construct a relatively prime graph with $$$n$$$ vertices and $$$m$$$ edges such that it is connected and it contains neither self-loops nor multiple edges.If there exists no valid graph with the given number of vertices and edges then output \"Impossible\".If there are multiple answers then print any of them.", "input_spec": "The only line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n, m \\le 10^5$$$) — the number of vertices and the number of edges.", "output_spec": "If there exists no valid graph with the given number of vertices and edges then output \"Impossible\". Otherwise print the answer in the following format: The first line should contain the word \"Possible\". The $$$i$$$-th of the next $$$m$$$ lines should contain the $$$i$$$-th edge $$$(v_i, u_i)$$$ of the resulting graph ($$$1 \\le v_i, u_i \\le n, v_i \\neq u_i$$$). For each pair $$$(v, u)$$$ there can be no more pairs $$$(v, u)$$$ or $$$(u, v)$$$. The vertices are numbered from $$$1$$$ to $$$n$$$. If there are multiple answers then print any of them.", "sample_inputs": ["5 6", "6 12"], "sample_outputs": ["Possible\n2 5\n3 2\n5 1\n3 4\n4 1\n5 4", "Impossible"], "notes": "NoteHere is the representation of the graph from the first example: "}, "positive_code": [{"source_code": "N, M = gets.split.map(&:to_i)\nif N-1 > M\n puts 'Impossible'\n exit\nend\n\n$ans = []\n(2..N).each do |i|\n $ans.push([1, i])\nend\n\ndef coprimes(a, b)\n d = a+b\n while d <= N\n $ans.push([b, d]) if b.between?(2, N) && d.between?(2, N)\n if $ans.length == M\n puts 'Possible'\n $ans.each do |arr|\n puts arr.join(' ')\n end\n exit\n end\n coprimes(b, d)\n d += b\n end\nend\n\ncoprimes(1, 1)\nputs 'Impossible'"}, {"source_code": "def gcd(a, b)\n return a if b == 0\n gcd(b, a%b)\nend\n\nn, m = gets.strip.split.map(&:to_i)\n\nif m 1\n v += 1\n if v > n\n u += 1\n v = u + 1\n end\n end\n if u<=n && u n\n u += 1\n v = u + 1\n end\n else\n possible = false\n break\n end\nend\n\nif possible\n puts \"Possible\"\n edges.each do |e|\n puts e.join(' ')\n end\nelse\n puts \"Impossible\"\nend"}], "negative_code": [{"source_code": "N, M = gets.split.map(&:to_i)\nif N-1 > M\n puts 'Impossible'\n exit\nend\n\n$ans = []\n(2..N).each do |i|\n $ans.push([1, i])\nend\n\ndef coprimes(a, b)\n d = a+b\n while d <= N\n $ans.push([b, d]) if a.between?(2, N) && b.between?(2, N)\n if $ans.length == M\n puts 'Possible'\n $ans.each do |arr|\n puts arr.join(' ')\n end\n exit\n end\n coprimes(b, d)\n d += b\n end\nend\n\ncoprimes(1, 1)\nputs 'Impossible'"}, {"source_code": "N, M = gets.split.map(&:to_i)\nif N-1 > M\n puts 'Impossible'\n exit\nend\n\n$ans = []\n\ndef coprimes(a, b)\n d = a+b\n while d <= N\n $ans.push([b, d]) if a.between?(1, N) && b.between?(1, N)\n if $ans.length == M\n puts 'Possible'\n $ans.each do |arr|\n puts arr.join(' ')\n end\n exit\n end\n coprimes(b, d)\n d += b\n end\nend\n\ncoprimes(1, 1)\nputs 'Impossible'"}], "src_uid": "0ab1b97a8d2e0290cda31a3918ff86a4"} {"nl": {"description": "Valera wanted to prepare a Codesecrof round. He's already got one problem and he wants to set a time limit (TL) on it.Valera has written n correct solutions. For each correct solution, he knows its running time (in seconds). Valera has also wrote m wrong solutions and for each wrong solution he knows its running time (in seconds).Let's suppose that Valera will set v seconds TL in the problem. Then we can say that a solution passes the system testing if its running time is at most v seconds. We can also say that a solution passes the system testing with some \"extra\" time if for its running time, a seconds, an inequality 2a ≤ v holds.As a result, Valera decided to set v seconds TL, that the following conditions are met: v is a positive integer; all correct solutions pass the system testing; at least one correct solution passes the system testing with some \"extra\" time; all wrong solutions do not pass the system testing; value v is minimum among all TLs, for which points 1, 2, 3, 4 hold. Help Valera and find the most suitable TL or else state that such TL doesn't exist.", "input_spec": "The first line contains two integers n, m (1 ≤ n, m ≤ 100). The second line contains n space-separated positive integers a1, a2, ..., an (1 ≤ ai ≤ 100) — the running time of each of the n correct solutions in seconds. The third line contains m space-separated positive integers b1, b2, ..., bm (1 ≤ bi ≤ 100) — the running time of each of m wrong solutions in seconds. ", "output_spec": "If there is a valid TL value, print it. Otherwise, print -1.", "sample_inputs": ["3 6\n4 5 2\n8 9 6 10 7 11", "3 1\n3 4 5\n6"], "sample_outputs": ["5", "-1"], "notes": null}, "positive_code": [{"source_code": "gets;a=gets.split.map &:to_i;b=gets.split.map &:to_i;z=[a.min*2,a.max].max;p z i)\n puts i\n exit\n end\n}\n\nputs -1\n"}, {"source_code": "nr, nw = gets.split.map{|s| s.to_i}\nrst = gets.split.map{|s| s.to_i}\nwst = gets.split.map{|s| s.to_i}\nrst_min = rst.min\nrst_max = rst.max\nwst_min = wst.min\nwst_max = wst.max\nif wst_min <= rst_max || wst_min <= rst_min*2\n\tputs -1\nelse\n\tputs [rst_max, rst_min*2].max\nend"}, {"source_code": "gets;a=gets.split.map &:to_i;b=gets.split.map &:to_i;z=[a.min*2,a.max].max;p zv\n }\n if ok and extra then\n res=v\n break\n end\n}\np res\n"}, {"source_code": "gets;a=gets.split.map &:to_i;b=gets.split.map &:to_i;z=[a.min*2,a.max].max;p z= wrong[0]\n puts -1\nelse\n puts ans\nend"}, {"source_code": "gets\na=gets.split.map{|s|s.to_i}\nb=gets.split.map{|s|s.to_i}\nm=[2*a.min,a.max].max\nputs (m= wrong[0] || 2 * correct[0] > ans\n puts -1\nelse\n puts ans\nend"}, {"source_code": "gets\na=gets.split.map{|s|s.to_i}\nb=gets.split.map{|s|s.to_i}\nif a.size>1\nputs (2*a.min<=a.max&&a.max\" ? 1 : -1)\n if now >= n || now < 0 || hash.has_key?(now)\n break\n end\n hash[now] = true\nend\nif now >= n || now < 0\n puts \"FINITE\"\nelse\n puts \"INFINITE\"\nend\n"}, {"source_code": "require 'set'\n\nstrip_length = gets.chomp.to_i\n\nJump = Struct.new(:direction, :length)\njumps = []\na = gets.chomp\nb = gets.chomp.split(' ')\n\nfor i in 0..a.length-1\n\tjumps[i] = Jump.new(a[i], b[i].to_i)\nend\n\n\nvisited = Set.new\nx = 0\n\nloop {\n\tvisited.add(x)\n\tdir = jumps[x].direction == \">\" ? 1 : -1\n\tx += dir * jumps[x].length\n\tif x >= jumps.length || x < 0\n\t\tputs \"FINITE\"\n\t\tbreak\n\tend\n\t\n\tif visited.include?(x)\n\t\tputs \"INFINITE\"\n\t\tbreak\n\tend\n\t\n}"}, {"source_code": "l = gets.to_i\ndirs = gets.to_s\nnums = gets.split(' ').map { |i| i.to_i }\nex = false\nnext_pos = 1\ndirs.length.times { |i|\n n = dirs[next_pos-1]\n next_pos = (n == '>' ? next_pos + nums[next_pos-1] : next_pos - nums[next_pos-1])\n if next_pos > l or next_pos <= 0\n ex = true\n break\n end\n}\nputs ex ? 'FINITE' : 'INFINITE'\n"}, {"source_code": "#! ruby\n# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n############################################################\ndef gs() gets.chomp end\ndef gi() gets.to_i end\ndef gss() gs.split end\ndef gis() gss.map(&:to_i) end\ndef nmapf(n,f) n.times.map{ __send__ f } end\ndef ngs(n) nmapf n,:gs end\ndef ngi(n) nmapf n,:gi end\ndef ngss(n) nmapf n,:gss end\ndef ngis(n) nmapf n,:gis end\ndef arr2d(h,w,v=0) h.times.map{[v] * w} end\ndef for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end\ndef nsum(n) n * (n + 1) / 2 end\ndef vcount(d,r=Hash.new(0)) d.inject(r){|r,e| r[e]+=1;r} end\n############################################################\n\nFIN = 'FINITE'\nINF = 'INFINITE'\n\nn = gi\nj = gs.each_char.to_a\nd = gis\n\njp = n.times.map{|i| j[i] == \"<\" ? -d[i] : d[i]}\n\nf = [false] * jp.size\n\ni = 0\n\nloop {\n if f[i]\n puts INF\n exit\n end\n f[i] = true\n i += jp[i]\n if i < 0 || i > n - 1\n puts FIN\n exit\n end \n}\n\n\n\n\n\n\n"}, {"source_code": "a=gets.chomp.to_i\nb=gets.chomp.split(\"\")\nc=gets.chomp.split(\" \").map{|i| i.to_i}\nd=Array.new(a,1)\n\ncur=0\nd[0]=0\ntrig=1\nloop do\nif b[cur]==\">\"\ncur+=c[cur]\nbreak if cur>=a || cur<0\nif d[cur]==0\ntrig=0\nbreak\nelse\nd[cur]=0\nend\nend\nif b[cur]==\"<\"\ncur-=c[cur]\nbreak if cur>=a || cur<0\nif d[cur]==0\ntrig=0\nbreak\nelse\nd[cur]=0\nend\nend\nend\n\nif trig==1\nputs \"FINITE\"\nelse\nputs \"INFINITE\"\nend"}, {"source_code": "n = gets.to_i\ns = gets\nvalues = gets.split.map{|x| x.to_i}\nvis = Array.new(n , false)\n\ni = 0\nflag = false\n\nwhile (vis[i] == false)\n\n\tvis[i] = true\n\t\n\tif(s[i] == '>')\n\t\ti += values[i]\n\telse\n\t\ti -= values[i]\n\n\tend\n\n\tif(i < 0 || i >= s.length - 1)\n\t\tflag = true\n\t\tbreak\n\tend\nend\n\nputs \"FINITE\" if flag == true\nputs \"INFINITE\" if flag == false"}, {"source_code": "a=gets.chomp.to_i\nb=gets.chomp.split(\"\")\nc=gets.chomp.split(\" \").map{|i| i.to_i}\nd=Array.new(a,1)\n\ncur=0\nd[0]=0\ntrig=1\nloop do\nif b[cur]==\">\"\ncur+=c[cur]\nbreak if cur>=a || cur<0\nif d[cur]==0\ntrig=0\nbreak\nelse\nd[cur]=0\nend\nend\nif b[cur]==\"<\"\ncur-=c[cur]\nbreak if cur>=a || cur<0\nif d[cur]==0\ntrig=0\nbreak\nelse\nd[cur]=0\nend\nend\nend\n\nif trig==1\nputs \"FINITE\"\nelse\nputs \"INFINITE\"\nend"}, {"source_code": "n = gets.chomp.to_i\n\ndirecs = gets.chomp.split(\"\")\nlens = gets.chomp.split(\" \").map(&:to_i)\n\na = Array.new(n, 0)\n\ncurr = 0\nwhile curr >= 0 && curr < n\n if a[curr] != 0\n puts \"INFINITE\"\n exit\n end\n a[curr] = 1\n if direcs[curr] == \"<\" then\n move = lens[curr] * -1\n else\n move = lens[curr]\n end\n curr += move\nend\nputs \"FINITE\"\n"}, {"source_code": "n = gets.to_i\nmap = gets.chomp\nlen = gets.chomp.split(' ')\nlen.each_index { |i| len[i] = len[i].to_i }\n\ncnt = Array.new(len.size) { |e| e = 0 }\npos, loo = 0, false\n\nwhile true\n cnt[pos] += 1\n if cnt[pos] == 2\n loo = true\n break\n end\n if map[pos] == '>'\n npos = pos + len[pos]\n else\n npos = pos - len[pos]\n end\n if npos > n-1 or npos < 0 #out of range\n loo = false\n break\n end\n pos = npos\nend\n\nif loo\n puts \"INFINITE\"\nelse\n puts \"FINITE\"\nend\n"}, {"source_code": "n = gets.to_i\ndir = gets\njlen = gets.split.map{|x| x.to_i}\nused = Array.new(n, false)\n\n(dir.length-1).times do |i|\n jlen[i] *= -1 if dir[i] == '<'\nend\n\ncur_pos = 0\nwhile 0 <= cur_pos && cur_pos < n\n if used[cur_pos]\n puts 'INFINITE'\n exit\n end\n used[cur_pos] = true\n cur_pos+=jlen[cur_pos]\nend\n\n#puts jlen\nputs 'FINITE'\n"}], "negative_code": [{"source_code": "l = gets.to_i\ndirs = gets.to_s\nnums = gets.split(' ').map { |i| i.to_i }\nex = false\nnext_pos = 0\nnums.each_with_index { |n, i|\n next_pos = dirs[i] == '>' ? next_pos + n : next_pos - n\n if next_pos > l or next_pos < 0\n ex = true\n break\n end\n}\nputs ex ? 'FINITE' : 'INFINITE'\n"}, {"source_code": "#! ruby\n# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n############################################################\ndef gs() gets.chomp end\ndef gi() gets.to_i end\ndef gss() gs.split end\ndef gis() gss.map(&:to_i) end\ndef nmapf(n,f) n.times.map{ __send__ f } end\ndef ngs(n) nmapf n,:gs end\ndef ngi(n) nmapf n,:gi end\ndef ngss(n) nmapf n,:gss end\ndef ngis(n) nmapf n,:gis end\ndef arr2d(h,w,v=0) h.times.map{[v] * w} end\ndef for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end\ndef nsum(n) n * (n + 1) / 2 end\ndef vcount(d,r=Hash.new(0)) d.inject(r){|r,e| r[e]+=1;r} end\n############################################################\n\nFIN = 'FINITE'\nINF = 'INFINITE'\n\nn = gi\nj = gs.each_char.to_a\nd = gis\n\njp = j.zip(d).map{|y, x| y == \"<\" ? -x : x}\n\nf = [false] * jp.size\n\ni = 0\n\nloop {\n if f[i]\n puts INF\n exit\n end\n f[i] = true\n i += jp[i]\n if i < 0 || i >= n - 1\n puts FIN\n exit\n end \n}\n\n\n\n\n\n\n"}, {"source_code": "#! ruby\n# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n############################################################\ndef gs() gets.chomp end\ndef gi() gets.to_i end\ndef gss() gs.split end\ndef gis() gss.map(&:to_i) end\ndef nmapf(n,f) n.times.map{ __send__ f } end\ndef ngs(n) nmapf n,:gs end\ndef ngi(n) nmapf n,:gi end\ndef ngss(n) nmapf n,:gss end\ndef ngis(n) nmapf n,:gis end\ndef arr2d(h,w,v=0) h.times.map{[v] * w} end\ndef for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end\ndef nsum(n) n * (n + 1) / 2 end\ndef vcount(d,r=Hash.new(0)) d.inject(r){|r,e| r[e]+=1;r} end\n############################################################\n\nFIN = 'FINITE'\nINF = 'INFINITE'\n\nn = gi\nj = gs.each_char.to_a\nd = gis\n\na = j.zip(d).reduce(0) {|r,k|\n y, x = k\n y == \"<\" ? r - x : r + x\n}\n\nif a < 0\n puts FIN\nelse\n puts INF\nend\n\n\n"}, {"source_code": "#! ruby\n# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n############################################################\ndef gs() gets.chomp end\ndef gi() gets.to_i end\ndef gss() gs.split end\ndef gis() gss.map(&:to_i) end\ndef nmapf(n,f) n.times.map{ __send__ f } end\ndef ngs(n) nmapf n,:gs end\ndef ngi(n) nmapf n,:gi end\ndef ngss(n) nmapf n,:gss end\ndef ngis(n) nmapf n,:gis end\ndef arr2d(h,w,v=0) h.times.map{[v] * w} end\ndef for2p(hr,wr,&pr) hr.each{|i|wr.each{|j| yield(i,j)}} end\ndef nsum(n) n * (n + 1) / 2 end\ndef vcount(d,r=Hash.new(0)) d.inject(r){|r,e| r[e]+=1;r} end\n############################################################\n\nFIN = 'FINITE'\nINF = 'INFINITE'\n\nn = gi\nj = gs.each_char.to_a\nd = gis\n\na = j.zip(d).reduce(0) {|r,k|\n if r < 0\n puts FIN\n exit\n end\n y, x = k\n y == \"<\" ? r - x : r + x\n}\n\nif a < 0\n puts FIN\nelse\n puts INF\nend\n\n\n"}, {"source_code": "n = gets.to_i\ns = gets\nvalues = gets.split.map{|x| x.to_i}\nvis = Array.new(n , false)\n\ni = 0\nflag = false\n\nwhile (vis[i] == false)\n\n\tvis[i] = true\n\t\n\tif(s[i] == '>')\n\t\ti += values[i]\n\telse\n\t\ti -= values[i]\n\n\tend\n\n\tif(i < 0 || i >= s.length)\n\t\tflag = true\n\t\tbreak\n\tend\nend\n\nputs \"FINITE\" if flag == true\nputs \"INFINITE\" if flag == false"}, {"source_code": "n = gets.to_i\ns = gets\nvalues = gets.split.map{|x| x.to_i}\nvis = Array.new(n , false)\n\ni = 0\nflag = false\n\nwhile (vis[i] == false)\n\n\tvis[i] = true\n\t\n\tif(s[i] == '>')\n\t\ti += values[i]\n\telse\n\t\ti -= values[i]\n\n\tend\n\n\tif(i < 0 || i >= s.length)\n\t\tflag = true\n\t\tbreak\n\tend\nend\n\np \"FINITE\" if flag == true\np \"INFINITE\" if flag == false"}], "src_uid": "5fcc22cdc38693723d8a9577d685db12"} {"nl": {"description": "Initially, you have the array $$$a$$$ consisting of one element $$$1$$$ ($$$a = [1]$$$).In one move, you can do one of the following things: Increase some (single) element of $$$a$$$ by $$$1$$$ (choose some $$$i$$$ from $$$1$$$ to the current length of $$$a$$$ and increase $$$a_i$$$ by one); Append the copy of some (single) element of $$$a$$$ to the end of the array (choose some $$$i$$$ from $$$1$$$ to the current length of $$$a$$$ and append $$$a_i$$$ to the end of the array). For example, consider the sequence of five moves: You take the first element $$$a_1$$$, append its copy to the end of the array and get $$$a = [1, 1]$$$. You take the first element $$$a_1$$$, increase it by $$$1$$$ and get $$$a = [2, 1]$$$. You take the second element $$$a_2$$$, append its copy to the end of the array and get $$$a = [2, 1, 1]$$$. You take the first element $$$a_1$$$, append its copy to the end of the array and get $$$a = [2, 1, 1, 2]$$$. You take the fourth element $$$a_4$$$, increase it by $$$1$$$ and get $$$a = [2, 1, 1, 3]$$$. Your task is to find the minimum number of moves required to obtain the array with the sum at least $$$n$$$.You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 \\le n \\le 10^9$$$) — the lower bound on the sum of the array.", "output_spec": "For each test case, print the answer: the minimum number of moves required to obtain the array with the sum at least $$$n$$$.", "sample_inputs": ["5\n1\n5\n42\n1337\n1000000000"], "sample_outputs": ["0\n3\n11\n72\n63244"], "notes": null}, "positive_code": [{"source_code": "t = gets.chomp.to_i\nt.times do\n n = gets.chomp.to_i\n ans = n ** (1/2.0)\n min = ans.floor\n max = ans.ceil\n count = 0\n \n if n == min ** 2\n count = (min - 1) + (min - 1)\n else\n sa = n - min ** 2\n count = min - 1 + (min - 1 ) + 1 + (sa / max)\n end\n puts count\nend"}], "negative_code": [], "src_uid": "d78cd4a06fb566d58275e92f976166f2"} {"nl": {"description": "There are $$$n$$$ candies put from left to right on a table. The candies are numbered from left to right. The $$$i$$$-th candy has weight $$$w_i$$$. Alice and Bob eat candies. Alice can eat any number of candies from the left (she can't skip candies, she eats them in a row). Bob can eat any number of candies from the right (he can't skip candies, he eats them in a row). Of course, if Alice ate a candy, Bob can't eat it (and vice versa).They want to be fair. Their goal is to eat the same total weight of candies. What is the most number of candies they can eat in total?", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$) — the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$1 \\leq n \\leq 2\\cdot10^5$$$) — the number of candies on the table. The second line of each test case contains $$$n$$$ integers $$$w_1, w_2, \\dots, w_n$$$ ($$$1 \\leq w_i \\leq 10^4$$$) — the weights of candies from left to right. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\\cdot10^5$$$.", "output_spec": "For each test case, print a single integer — the maximum number of candies Alice and Bob can eat in total while satisfying the condition.", "sample_inputs": ["4\n3\n10 20 10\n6\n2 1 4 2 4 1\n5\n1 2 4 8 16\n9\n7 3 20 5 15 1 11 8 10"], "sample_outputs": ["2\n6\n0\n7"], "notes": "NoteFor the first test case, Alice will eat one candy from the left and Bob will eat one candy from the right. There is no better way for them to eat the same total amount of weight. The answer is $$$2$$$ because they eat two candies in total.For the second test case, Alice will eat the first three candies from the left (with total weight $$$7$$$) and Bob will eat the first three candies from the right (with total weight $$$7$$$). They cannot eat more candies since all the candies have been eaten, so the answer is $$$6$$$ (because they eat six candies in total).For the third test case, there is no way Alice and Bob will eat the same non-zero weight so the answer is $$$0$$$.For the fourth test case, Alice will eat candies with weights $$$[7, 3, 20]$$$ and Bob will eat candies with weights $$$[10, 8, 11, 1]$$$, they each eat $$$30$$$ weight. There is no better partition so the answer is $$$7$$$."}, "positive_code": [{"source_code": "def eating_cadies(arr, n)\r\n i = 0\r\n j = n - 1\r\n alice = arr[0]\r\n bob = arr[j]\r\n count = 0\r\n while i < j\r\n if alice == bob\r\n count = [count, i+1+n-j].max\r\n end\r\n if alice <= bob\r\n i+=1\r\n alice+=arr[i]\r\n elsif bob < alice\r\n j-=1\r\n bob+=arr[j]\r\n end\r\n \r\n end\r\n count\r\nend\r\nt = gets.to_i\r\ninputs = []\r\n(0..t-1).each do |i|\r\n n = gets.to_i\r\n arr = gets.chomp\r\n arr = arr.split(\" \").map(&:to_i)\r\n inputs.push([arr, n])\r\nend\r\n\r\ninputs.each do |input|\r\n puts eating_cadies(input[0], input[1])\r\nend\r\n"}, {"source_code": "gets.to_i.times do\n gets()\n xs = gets.split.map(&:to_i)\n h = {}\n sum = 0\n xs.each_with_index do |x, i|\n sum += x\n h[sum] = i + 1\n end\n\n ans = 0\n sum = 0\n xs.reverse.each_with_index do |x, i|\n sum += x\n if h.include?(sum)\n j = xs.size() - h[sum] + 1\n if j > i + 1\n ans = [ans, h[sum] + i + 1].max\n end\n end\n end\n\n p ans\nend\n"}, {"source_code": "gets.to_i.times do\r\n gets\r\n xs = gets.split.map(&:to_i)\r\n l, r = 0, xs.size - 1\r\n sl, sr, max, ate = 0, 0, 0, 0\r\n while l <= r\r\n if sl < sr || (sl == sr && xs[l] < xs[r])\r\n sl += xs[l]\r\n l += 1\r\n else\r\n sr += xs[r]\r\n r -= 1\r\n end\r\n ate += 1\r\n max = ate if sl == sr\r\n end\r\n p max\r\nend\r\n"}], "negative_code": [{"source_code": "def eating_cadies(arr, n)\r\n i = 0\r\n j = n - 1\r\n alice = arr[0]\r\n bob = arr[j]\r\n count = 0\r\n while i < j\r\n if alice <= bob\r\n i+=1\r\n alice+=arr[i]\r\n elsif bob < alice\r\n j-=1\r\n bob+=arr[j]\r\n end\r\n if alice == bob\r\n count = [count, i+1+n-j].max\r\n end\r\n end\r\n count\r\nend\r\nt = gets.to_i\r\ninputs = []\r\n(0..t-1).each do |i|\r\n n = gets.to_i\r\n arr = gets.chomp\r\n arr = arr.split(\" \").map(&:to_i)\r\n inputs.push([arr, n])\r\nend\r\n\r\ninputs.each do |input|\r\n puts eating_cadies(input[0], input[1])\r\nend\r\n"}], "src_uid": "90e448ed43ba9a0533ac7dc24f92a7f8"} {"nl": {"description": "When Sasha was studying in the seventh grade, he started listening to music a lot. In order to evaluate which songs he likes more, he introduced the notion of the song's prettiness. The title of the song is a word consisting of uppercase Latin letters. The prettiness of the song is the prettiness of its title.Let's define the simple prettiness of a word as the ratio of the number of vowels in the word to the number of all letters in the word.Let's define the prettiness of a word as the sum of simple prettiness of all the substrings of the word.More formally, let's define the function vowel(c) which is equal to 1, if c is a vowel, and to 0 otherwise. Let si be the i-th character of string s, and si..j be the substring of word s, staring at the i-th character and ending at the j-th character (sisi + 1... sj, i ≤ j).Then the simple prettiness of s is defined by the formula:The prettiness of s equals Find the prettiness of the given song title.We assume that the vowels are I, E, A, O, U, Y.", "input_spec": "The input contains a single string s (1 ≤ |s| ≤ 5·105) — the title of the song.", "output_spec": "Print the prettiness of the song with the absolute or relative error of at most 10 - 6.", "sample_inputs": ["IEAIAIO", "BYOB", "YISVOWEL"], "sample_outputs": ["28.0000000", "5.8333333", "17.0500000"], "notes": "NoteIn the first sample all letters are vowels. The simple prettiness of each substring is 1. The word of length 7 has 28 substrings. So, the prettiness of the song equals to 28."}, "positive_code": [{"source_code": "d=gets\nn=d.size-1\nsum,ans,now=[0],0,0\nn.times{|i|sum[i+1]=sum[i]+1.0/(i+1)}\n(0...n).each do |i|\n now+=sum[-i-1]-sum[i]\n ans+=now if 'IEAOUY'[d[i]]\nend\np ans"}, {"source_code": "v=['I','E','A','O','U','Y']\nd=['Q']+gets.split(//)\nd.pop\nn=d.size-1\nsum,ans,now=[0.0],0.0,0.0\nn.times{|i|sum[i+1]=sum[i]+1.0/(i+1)}\n(1..n/2).each do |i|\n now+=sum[-i]-sum[i-1]\n ans+=now if v.include?d[i]\n ans+=now if v.include?d[-i]\nend\nnow+=sum[n/2+1]-sum[n/2]\nans+=now if n.odd?&&v.include?(d[n/2+1])\np ans"}, {"source_code": "gets\nsum,a,now,n=[0],0,0,$_.size-1\nn.times{|i|sum[i+1]=sum[i]+1.0/(i+1)}\n(0...n).each do |i|\n now+=sum[-i-1]-sum[i]\n a+=now if 'IEAOUY'[$_[i]]\nend\np a"}, {"source_code": "d=gets.split(//)\nd.pop\nn=d.size\nsum,ans,now=[0],0,0\nn.times{|i|sum[i+1]=sum[i]+1.0/(i+1)}\n(0...n).each do |i|\n now+=sum[-i-1]-sum[i]\n ans+=now if 'IEAOUY'[d[i]]\nend\np ans"}], "negative_code": [], "src_uid": "2b37188c17b7cbc3e533e9ad341441b2"} {"nl": {"description": "To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, wants to know the optimal temperature for brewing the perfect cup of coffee. Indeed, she has spent some time reading several recipe books, including the universally acclaimed \"The Art of the Covfefe\".She knows n coffee recipes. The i-th recipe suggests that coffee should be brewed between li and ri degrees, inclusive, to achieve the optimal taste.Karen thinks that a temperature is admissible if at least k recipes recommend it.Karen has a rather fickle mind, and so she asks q questions. In each question, given that she only wants to prepare coffee with a temperature between a and b, inclusive, can you tell her how many admissible integer temperatures fall within the range?", "input_spec": "The first line of input contains three integers, n, k (1 ≤ k ≤ n ≤ 200000), and q (1 ≤ q ≤ 200000), the number of recipes, the minimum number of recipes a certain temperature must be recommended by to be admissible, and the number of questions Karen has, respectively. The next n lines describe the recipes. Specifically, the i-th line among these contains two integers li and ri (1 ≤ li ≤ ri ≤ 200000), describing that the i-th recipe suggests that the coffee be brewed between li and ri degrees, inclusive. The next q lines describe the questions. Each of these lines contains a and b, (1 ≤ a ≤ b ≤ 200000), describing that she wants to know the number of admissible integer temperatures between a and b degrees, inclusive.", "output_spec": "For each question, output a single integer on a line by itself, the number of admissible integer temperatures between a and b degrees, inclusive.", "sample_inputs": ["3 2 4\n91 94\n92 97\n97 99\n92 94\n93 97\n95 96\n90 100", "2 1 1\n1 1\n200000 200000\n90 100"], "sample_outputs": ["3\n3\n0\n4", "0"], "notes": "NoteIn the first test case, Karen knows 3 recipes. The first one recommends brewing the coffee between 91 and 94 degrees, inclusive. The second one recommends brewing the coffee between 92 and 97 degrees, inclusive. The third one recommends brewing the coffee between 97 and 99 degrees, inclusive. A temperature is admissible if at least 2 recipes recommend it.She asks 4 questions.In her first question, she wants to know the number of admissible integer temperatures between 92 and 94 degrees, inclusive. There are 3: 92, 93 and 94 degrees are all admissible.In her second question, she wants to know the number of admissible integer temperatures between 93 and 97 degrees, inclusive. There are 3: 93, 94 and 97 degrees are all admissible.In her third question, she wants to know the number of admissible integer temperatures between 95 and 96 degrees, inclusive. There are none.In her final question, she wants to know the number of admissible integer temperatures between 90 and 100 degrees, inclusive. There are 4: 92, 93, 94 and 97 degrees are all admissible.In the second test case, Karen knows 2 recipes. The first one, \"wikiHow to make Cold Brew Coffee\", recommends brewing the coffee at exactly 1 degree. The second one, \"What good is coffee that isn't brewed at at least 36.3306 times the temperature of the surface of the sun?\", recommends brewing the coffee at exactly 200000 degrees. A temperature is admissible if at least 1 recipe recommends it.In her first and only question, she wants to know the number of admissible integer temperatures that are actually reasonable. There are none."}, "positive_code": [{"source_code": "N, K, Q = gets.split.map(&:to_i)\nims = Array.new(200000 + 1 + 1, 0)\nN.times {\n a, b = gets.split.map(&:to_i)\n ims[a] += 1\n ims[b + 1] -= 1\n}\n200000.times { |i|\n ims[i + 1] += ims[i]\n}\npre = Array.new(200000 + 1 + 1, 0)\n200000.times { |i|\n pre[i + 1] = pre[i] + (ims[i + 1] >= K ? 1 : 0)\n}\nQ.times {\n a, b = gets.split.map(&:to_i)\n puts pre[b] - pre[a - 1]\n}\n"}, {"source_code": "N, K, Q = $stdin.readline.split.map(&:to_i)\nims = Array.new(200000 + 1 + 1, 0)\nN.times {\n a, b = $stdin.readline.split.map(&:to_i)\n ims[a] += 1\n ims[b + 1] -= 1\n}\n200000.times { |i|\n ims[i + 1] += ims[i]\n}\npre = Array.new(200000 + 1 + 1, 0)\n200000.times { |i|\n pre[i + 1] = pre[i] + (ims[i + 1] >= K ? 1 : 0)\n}\nQ.times {\n a, b = $stdin.readline.split.map(&:to_i)\n puts pre[b] - pre[a - 1]\n}\n"}, {"source_code": "N, K, Q = $stdin.gets.split.map(&:to_i)\nims = Array.new(200000 + 1 + 1, 0)\nN.times {\n a, b = $stdin.gets.split.map(&:to_i)\n ims[a] += 1\n ims[b + 1] -= 1\n}\n200000.times { |i|\n ims[i + 1] += ims[i]\n}\npre = Array.new(200000 + 1 + 1, 0)\n200000.times { |i|\n pre[i + 1] = pre[i] + (ims[i + 1] >= K ? 1 : 0)\n}\nQ.times {\n a, b = $stdin.gets.split.map(&:to_i)\n puts pre[b] - pre[a - 1]\n}\n"}], "negative_code": [], "src_uid": "4bdf819b73ffb708ad571accf3b8c23d"} {"nl": {"description": "Valera runs a 24/7 fast food cafe. He magically learned that next day n people will visit his cafe. For each person we know the arrival time: the i-th person comes exactly at hi hours mi minutes. The cafe spends less than a minute to serve each client, but if a client comes in and sees that there is no free cash, than he doesn't want to wait and leaves the cafe immediately. Valera is very greedy, so he wants to serve all n customers next day (and get more profit). However, for that he needs to ensure that at each moment of time the number of working cashes is no less than the number of clients in the cafe. Help Valera count the minimum number of cashes to work at his cafe next day, so that they can serve all visitors.", "input_spec": "The first line contains a single integer n (1 ≤ n ≤ 105), that is the number of cafe visitors. Each of the following n lines has two space-separated integers hi and mi (0 ≤ hi ≤ 23; 0 ≤ mi ≤ 59), representing the time when the i-th person comes into the cafe. Note that the time is given in the chronological order. All time is given within one 24-hour period.", "output_spec": "Print a single integer — the minimum number of cashes, needed to serve all clients next day.", "sample_inputs": ["4\n8 0\n8 10\n8 10\n8 45", "3\n0 12\n10 11\n22 22"], "sample_outputs": ["2", "1"], "notes": "NoteIn the first sample it is not enough one cash to serve all clients, because two visitors will come into cafe in 8:10. Therefore, if there will be one cash in cafe, then one customer will be served by it, and another one will not wait and will go away.In the second sample all visitors will come in different times, so it will be enough one cash."}, "positive_code": [{"source_code": "n = gets.to_i\nb = []\nx,y = 1,0\nn.times do |i|\t\n\t a = gets.split.map(&:to_i)\n\t if a == b \t \t \t\n\t \ty += 1\n\t \tx = [x,y].max\n\t else\n\t \ty = 1\n\t end\n\t b = a\nend\np x"}, {"source_code": "i = gets.to_i; x = Hash.new(0);\ni.times { x[gets.chomp] += 1 }\nputs x.values.max"}, {"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\n\nn = gets.to_i\nph, pm = -1, -1\ncons = 0\nmax_cons = 0\nn.times {\n h, m = gets.split.map(&:to_i)\n if h != ph || m != pm\n if max_cons < cons\n max_cons = cons\n end\n cons = 0\n ph, pm = h, m\n end\n\n cons += 1\n\n if max_cons < cons\n max_cons = cons\n end\n}\nputs max_cons"}, {"source_code": "h = Hash.new(0)\ngets.to_i.times {\n h[gets.chomp] += 1\n}\n\np h.values.max"}, {"source_code": "n = gets.chomp.to_i\ncashiers = []\nfor i in (0..n-1)\n\tcashiers[i] = gets.chomp\nend\n\ncashes = 1\nfree = 0\nfor i in (0..cashiers.size - 2)\n\tif cashiers[i] == cashiers[i+1] && free <= 1\n\t\tcashes += 1\n\telse\n\t\tif cashiers[i] == cashiers[i+1]\n\t\t\tfree -= 1\n\t\telse\n\t\t\tfree = cashes\n\t\tend\n\tend\nend\n\nprint cashes"}, {"source_code": "def max_cash(arrival_times)\n max = 0 \n counter = 0\n prev_value = arrival_times[0]\n\n arrival_times.each do |arrival_time|\n if arrival_time == prev_value\n counter += 1\n else\n counter = 1\n end\n prev_value = arrival_time\n max = [max, counter].max\n end\n \n max\nend\n\nn = gets.chomp.to_i\nclientes_arrival_time = []\nn.times do \n clientes_arrival_time << gets.chomp.gsub(\" \", \":\")\nend\n\nputs max_cash(clientes_arrival_time)\n"}, {"source_code": "def max_cash(arrival_times)\n arrival_time_counter = Hash.new { |k, v| k[v] = 0 }\n \n arrival_times.each do |arrival_time|\n arrival_time_counter[arrival_time] += 1\n end\n \n arrival_time_counter.values.max\nend\n\nn = gets.chomp.to_i\nclientes_arrival_time = []\nn.times do \n clientes_arrival_time << gets.chomp.gsub(\" \", \":\")\nend\n\nputs max_cash(clientes_arrival_time)\n"}, {"source_code": "n = gets.to_i\na = Hash.new(0)\nn.times do\n a[gets.chomp] += 1\nend\nputs a.values.max\n"}, {"source_code": "n = gets.to_i\na = Array.new(24){Array.new(60, 0)}\nn.times do\n h, m = gets.split.map(&:to_i)\n a[h][m] += 1\nend\naa = []\n24.times do |i|\n aa[i] = a[i].max\nend\np aa.max\n"}, {"source_code": "n = gets.chomp.to_i\nd = {}\nn.times do\n h, m = gets.chomp.split(\" \").map(&:to_i)\n d[[h, m]] = d.fetch([h, m], 0) + 1\nend\nputs d.values.max"}, {"source_code": "n = gets.to_i\nc = {}\nn.times do\n h, m = gets.split.map{|x| x.to_i}\n c[[h, m]] = 0 unless c[[h, m]]\n c[[h, m]] += 1\nend\nputs c.max{|a, b| a[1] <=> b[1]}[1]\n"}, {"source_code": "n = gets.to_i\n\ntime = Hash.new(0)\nmax = 0\nn.times do |_|\n h, m = gets.split(\" \").map(&:to_i)\n max = [max, time[m + h*60] += 1].max\nend\n\np max"}, {"source_code": "n = gets.to_i\n\ntime = Hash.new(0)\nn.times do |_|\n h, m = gets.split(\" \").map(&:to_i)\n time[h*60 + m] += 1\nend\n\np time.max { |a, b| a[1] <=> b[1]}[1]"}, {"source_code": "n = gets.to_i\n\ntime = Hash.new(0)\nn.times do |_|\n h, m = gets.split(\" \").map(&:to_i)\n time[h*60 + m] += 1\nend\n\np time.values.max"}, {"source_code": "n = gets.to_i\n\ntime = Hash.new(0)\nn.times do |_|\n h, m = gets.split(\" \").map(&:to_i)\n time[h*60 + m] += 1\nend\n\np time.max_by { |k, v| v}[1]"}, {"source_code": "n = gets.to_i\n\ntime = Hash.new(0)\nmax = 0\nn.times do |_|\n h, m = gets.split(\" \").map(&:to_i)\n m += h*60\n time[m] += 1\n max = [max, time[m]].max\nend\n\np max"}, {"source_code": "n = gets.to_i\npr = [-1, -1]\ntmp, max = 0, 1\nn.times do\n ar = gets.split(' ').map(&:to_i)\n ar == pr ? tmp+=1 : tmp = 1\n max = tmp if tmp > max\n pr = ar\nend\nputs max"}, {"source_code": "\na=Array.new(24*60+1,0)\n\n#print a\nn=gets.chomp.to_i\ni=0\nfor i in (0...n)\nb=gets.chomp.split(\" \")\na[(b[0].to_i*60+b[1].to_i)]+=1\nend\nputs a.max\n\n\n"}, {"source_code": "\na=Hash.new(0)\n\n#print a\nn=gets.chomp.to_i\ni=0\nfor i in (0...n)\nb=gets.chomp.split(\" \")\na[(b[0].to_i*60+b[1].to_i)]+=1\nend\nputs a.values.max\n\n\n"}, {"source_code": "n = gets.to_i\n\nt = {}\nn.times do |i|\n s = gets.chomp.sub(\" \", \":\")\n if t[s] then\n t[s] += 1\n else\n t[s] = 1\n end\nend\n\nt = t.sort{|a,b| a[1]<=>b[1]}\n\np t[-1][1]"}, {"source_code": "h = Hash.new(0)\ngets.to_i.times{\n h[gets] += 1\n}\np h.values.max\n"}, {"source_code": "I =->{gets.split.map &:to_i}\nRD =->{gets.to_i}\n\naa = bb = -1; cnt = res = 1; RD[].times{\n\ta, b = I[]\t\t\n\tif a == aa && b == bb\t\t\n\t\tcnt += 1; res = cnt if (cnt > res) \n\telse\n\t\tcnt = 1; aa, bb = a, b\n\tend\n}\n\np res"}, {"source_code": "n = gets.to_i\nhLast, mLast = -1, -1\nmax = 1\ncount = 1\nn.times do\n h, m = gets.split(\" \").map(&:to_i)\n if hLast == h && mLast == m\n count += 1\n else\n count = 1\n end\n max = count if count > max\n hLast, mLast = h, m\nend\n\nputs max"}, {"source_code": "n = gets.chomp.to_i\n\nhash = {}\nn.times do\n v = gets.chomp\n if hash.has_key? v\n hash[v] += 1\n else\n hash[v] = 1\n end\nend\n\nputs hash.values.max"}, {"source_code": "n = gets.to_i\ncus = Array.new(24)\nfor i in 0...24\n cus[i] = Array.new(60, 0)\nend\nn.times do\n a, b = gets.split.map(&:to_i)\n cus[a][b] += 1\nend\nm = Array.new\nfor i in 0...24\n m << cus[i].max\nend\nprint m.max"}, {"source_code": "times = []\npeople = gets.chomp.to_i\npeople.times { time = gets\n times.push(time) }\n\nb = Hash.new(0)\ntimes.each do |element|\n b[element] += 1\nend\n\np b.values.max"}, {"source_code": "hash = Array.new(24*60) {0}\nf = Proc.new{ |a,b| a * 60 + b }\ngets.to_i.times {\n hash[f.call gets.split.map(&:to_i)] += 1 \n}\nputs hash.max"}, {"source_code": "a = Hash.new(0)\ngets.to_i.times { a[gets.strip] += 1 }\nputs a.values.max"}, {"source_code": "n = gets.chomp.to_i\na = []\nn.times do\n a << gets\nend\n\nh = {}\n\na.each_with_index do |t, i|\n h[t] = (h[t].nil? ? 1 : h[t]+1)\nend\n\nputs h.values.max"}, {"source_code": "n = gets.to_i\ncox = []\nfor i in 0..n-1\n cox[i] = gets.split.map(&:to_i)\nend\n\n\nc = 0\nfox = []\ncox.sort!\nfor i in 0..n-1\n if cox[i] == cox[i+1]\n c+=1\n else\n fox.push(c)\n c = 0\n end\nend\n\nputs fox.max + 1\n"}, {"source_code": "n = $stdin.gets.to_i\nr = []\nn.times {r << $stdin.readline.chop}\n\nclass Array # use self.partition\n\n def complect_and_count\n self.dup.complect_and_count!\n end\n\n def complect_and_count!\n a = self.shift\n h = {sym: a, count: 1}\n result, k = [h], 1\n while self.size>0\n b = self.shift\n if a != b\n h[:count] = k\n h = {sym: b, count: 1}\n result << h\n k = 0\n end\n a = b\n k += 1\n end\n h[:count]=k\n result\n end\n\nend\n\nt = r.complect_and_count\nm = t.max_by {|x| x[:count]}\n\nputs m[:count]\n"}, {"source_code": "arr = []\ngets.to_i.times do\n arr << gets.chomp\nend\nhash = Hash.new(0)\narr.each { |v| hash[v] += 1 }\nputs hash.values.max\n"}, {"source_code": "t = []\nmax_t = 1\ngets.to_i.times do |i|\n in_time = gets.chomp.split.map(&:to_i)\n tt = in_time[0]*60 + in_time[1]\n if t[tt].nil?\n t[tt] = 1\n else\n t[tt] += 1\n end\n max_t = t[tt] if t[tt] > max_t\nend\nputs max_t\n"}, {"source_code": "n=gets.to_i\nn-=1\ns=gets.chomp\nans=1\nc=1\nn.times{\n\ts2=gets.chomp\n\tif s==s2 then\n\t\tc=c+1\n\telse\n\t\tc=1\n\t\ts=s2\n\tend\n\tans=[ans,c].max\n}\nputs ans\n\n"}], "negative_code": [{"source_code": "i = gets.to_i; x = Hash.new(0);\ni.times { x[gets.chomp] += 1 }\nputs x.max[1]"}, {"source_code": "customers = gets.chomp.to_i\ntime = []\nfor i in (0..customers-1)\n\tt = gets.chomp\n\ttime[i] = t.split\n\ttime[i] = (time[i][0].to_i * 60) + time[i][1].to_i\nend\n\ncashiers = 1\nfor i in (0..customers-2)\n\tif time[i] == time[i+1] || (time[i]).abs - (time[i+1]).abs == 1\n\t\tcashiers += 1\n\tend\nend\n\nprint cashiers"}, {"source_code": "n = gets.chomp.to_i\ncashiers = []\nfor i in (0..n-1)\n\tcashiers[i] = gets.chomp\nend\n\ncashes = 0\navailable = 0\nfor i in (0..n-2)\n\tif cashiers[i] == cashiers[i+1]\n\t\tif cashes == 0 && available == 0\n\t\t\tcashes += 2\n\t\telsif available == 0\n\t\t\tcashes += 1\n\t\tend\n\telse\n\t\tavailable = cashes\n\tend\nend\nif cashes == 0\n\tprint 1\nelse\n\tprint cashes\nend"}, {"source_code": "n = gets.to_i\na = Array.new(24, Array.new(60, 0))\nn.times do\n h, m = gets.split.map(&:to_i)\n a[h][m] += 1\nend\naa = []\n24.times do |i|\n aa[i] = a[i].max\nend\np aa.max\n"}, {"source_code": "n = gets.to_i\na = Array.new(24, Array.new(60, 0))\nn.times do\n h, m = gets.split.map(&:to_i)\n a[h][m] += 1\nend\np a.max.max\n"}, {"source_code": "n = gets.to_i\ncox = []\nfor i in 0..n-1\n cox[i] = gets.split.map(&:to_i)\nend\n\nk = 1\n\nputs k + (n - cox.uniq.size)\n"}, {"source_code": "arr = []\ngets.to_i.times do\n arr << gets.chomp\nend\nputs 1 + (arr.size - arr.uniq.size)\n"}, {"source_code": "n = gets.to_i\nb = []\nx,y = 0,0\nn.times do |i|\t\n\t a = gets.split.map(&:to_i)\n\t if a == b \t \t \t\n\t \ty += 1\n\t \tx = [x,y].max\n\t else\n\t \ty = 1\n\t end\n\t b = a\nend\np x"}], "src_uid": "cfad2cb472e662e037f3415a84daca57"} {"nl": {"description": "You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $$$p$$$ units and your follower — at most $$$f$$$ units.In the blacksmith shop, you found $$$cnt_s$$$ swords and $$$cnt_w$$$ war axes. Each sword weights $$$s$$$ units and each war axe — $$$w$$$ units. You don't care what to take, since each of them will melt into one steel ingot.What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$p$$$ and $$$f$$$ ($$$1 \\le p, f \\le 10^9$$$) — yours and your follower's capacities. The second line of each test case contains two integers $$$cnt_s$$$ and $$$cnt_w$$$ ($$$1 \\le cnt_s, cnt_w \\le 2 \\cdot 10^5$$$) — the number of swords and war axes in the shop. The third line of each test case contains two integers $$$s$$$ and $$$w$$$ ($$$1 \\le s, w \\le 10^9$$$) — the weights of each sword and each war axe. It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $$$2 \\cdot 10^5$$$.", "output_spec": "For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.", "sample_inputs": ["3\n33 27\n6 10\n5 6\n100 200\n10 10\n5 5\n1 19\n1 3\n19 5"], "sample_outputs": ["11\n20\n3"], "notes": "NoteIn the first test case: you should take $$$3$$$ swords and $$$3$$$ war axes: $$$3 \\cdot 5 + 3 \\cdot 6 = 33 \\le 33$$$ and your follower — $$$3$$$ swords and $$$2$$$ war axes: $$$3 \\cdot 5 + 2 \\cdot 6 = 27 \\le 27$$$. $$$3 + 3 + 3 + 2 = 11$$$ weapons in total.In the second test case, you can take all available weapons even without your follower's help, since $$$5 \\cdot 10 + 5 \\cdot 10 \\le 100$$$.In the third test case, you can't take anything, but your follower can take $$$3$$$ war axes: $$$3 \\cdot 5 \\le 19$$$."}, "positive_code": [{"source_code": "t = gets.to_i\nans = []\nt.times do\n p,f = gets.chomp.split(\" \").map(&:to_i)\n cnt1,cnt2 = gets.chomp.split(\" \").map(&:to_i)\n s,w = gets.chomp.split(\" \").map(&:to_i)\n max = 0\n (cnt1+1).times do |i|\n next if i*s>p\n num_p_s = i\n rest_p = p - i*s\n rest_cnt_s = cnt1-i\n num_p_w = [rest_p/w, cnt2].min\n rest_cnt_w = cnt2 - num_p_w\n num_f_s = 0\n num_f_w = 0\n if s > w\n num_f_w = [f/w, rest_cnt_w].min\n rest_f = f - num_f_w*w\n num_f_s = [rest_f/s, rest_cnt_s].min\n else\n num_f_s = [f/s, rest_cnt_s].min\n rest_f = f - num_f_s*s\n num_f_w = [rest_f/w, rest_cnt_w].min\n end\n max = [num_p_s + num_p_w + num_f_s + num_f_w, max].max\n end\n ans << max\nend\nans.each do |x|\n puts x\nend\n"}], "negative_code": [{"source_code": "t = gets.to_i\nans = []\nt.times do\n p,f = gets.chomp.split(\" \").map(&:to_i)\n cnt1,cnt2 = gets.chomp.split(\" \").map(&:to_i)\n s,w = gets.chomp.split(\" \").map(&:to_i)\n if p < f\n p,f = f,p\n end\n if s < w\n s,w = w,s\n cnt1,cnt2 = cnt2,cnt1\n end\n num2 = f / w\n if num2 > cnt2\n num2 = cnt2\n end\n cnt2 -= num2\n f -= num2 * w\n num1 = 0\n if f >= s\n num1 = f / s\n if num1 > cnt1\n num1 = cnt1\n end\n cnt1 -= num1\n end\n num3 = 0\n num4 = 0\n if cnt2 > 0\n num4 = p / w\n if num4 > cnt2\n num4 = cnt2\n end\n cnt2 -= num4\n p -= num4 * w\n end\n if p >= s\n num3 = p / s\n if num3 > cnt1\n num3 = cnt1\n end\n cnt1 -= num3\n p -= num3 * s\n end\n max = num1+num2+num3+num4\n puts \"#{num1} #{num2} #{num3} #{num4} #{cnt1} #{cnt2} #{p} #{f}\"\n while num2 > 0\n num2 -= 1\n f += w\n cnt2 += 1\n if f >= s && cnt1 > 0\n num1 += 1\n cnt1 -= 1\n f -= s\n end\n if num3 > 0 && cnt2 > 0\n num3 -= 1\n cnt1 += 1\n num4 += 1\n cnt2 -= 1\n p = p + s - w\n end\n if p >= w && cnt1 > 0\n nu = p / w\n if nu > cnt1\n nu = cnt1\n end\n num4 += nu\n cnt1 -= nu\n p -= nu * w\n end\n max = [max,num1+num2+num3+num4].max\n end\n ans << max\nend\nans.each do |x|\n puts x\nend"}, {"source_code": "t = gets.to_i\nans = []\nt.times do\n p,f = gets.chomp.split(\" \").map(&:to_i)\n cnt1,cnt2 = gets.chomp.split(\" \").map(&:to_i)\n s,w = gets.chomp.split(\" \").map(&:to_i)\n max = 0\n cnt1.times do |i|\n next if i*s>p\n num_p_s = i\n rest_p = p - i*s\n rest_cnt_s = cnt1-i\n num_p_w = [rest_p/w, cnt2].min\n rest_cnt_w = cnt2 - num_p_w\n num_f_s = 0\n num_f_w = 0\n if s > w\n num_f_w = [f/w, rest_cnt_w].min\n rest_f = f - num_f_w*w\n num_f_s = [rest_f/s, rest_cnt_s].min\n else\n num_f_s = [f/s, rest_cnt_s].min\n rest_f = f - num_f_s*s\n num_f_w = [rest_f/w, rest_cnt_w].min\n end\n max = [num_p_s + num_p_w + num_f_s + num_f_w, max].max\n end\n ans << max\nend\nans.each do |x|\n puts x\nend\n"}, {"source_code": "t = gets.to_i\nans = []\nt.times do\n p,f = gets.chomp.split(\" \").map(&:to_i)\n cnt1,cnt2 = gets.chomp.split(\" \").map(&:to_i)\n s,w = gets.chomp.split(\" \").map(&:to_i)\n if p < f\n p,f = f,p\n end\n if s < w\n s,w = w,s\n cnt1,cnt2 = cnt2,cnt1\n end\n num2 = f / w\n if num2 > cnt2\n num2 = cnt2\n end\n cnt2 -= num2\n f -= num2 * w\n num1 = 0\n if f >= s\n num1 = f / s\n if num1 > cnt1\n num1 = cnt1\n end\n cnt1 -= num1\n end\n num3 = 0\n num4 = 0\n if cnt2 > 0\n num4 = p / w\n if num4 > cnt2\n num4 = cnt2\n end\n cnt2 -= num4\n p -= num4 * w\n end\n if p >= s\n num3 = p / s\n if num3 > cnt1\n num3 = cnt1\n end\n cnt1 -= num3\n p -= num3 * s\n end\n max = num1+num2+num3+num4\n puts \"#{num1},#{num2},#{num3},#{num4},#{cnt1},#{cnt2},#{p},#{f}\"\n while num2 > 0\n num2 -= 1\n f += w\n cnt2 += 1\n if f >= s && cnt1 > 0\n num1 += 1\n cnt1 -= 1\n f -= s\n end\n if num3 > 0 && cnt2 > 0\n num3 -= 1\n cnt1 += 1\n num4 += 1\n cnt2 -= 1\n p = p + s - w\n end\n if p >= s && cnt1 > 0\n nu = p / s\n if nu > cnt1\n nu = cnt1\n end\n num4 += nu\n cnt2 -= nu\n p -= nu * w\n end\n max = [max,num1+num2+num3+num4].max\n end\n ans << max\nend\nans.each do |x|\n puts x\nend"}, {"source_code": "t = gets.to_i\nans = []\nt.times do\n p,f = gets.chomp.split(\" \").map(&:to_i)\n cnt1,cnt2 = gets.chomp.split(\" \").map(&:to_i)\n s,w = gets.chomp.split(\" \").map(&:to_i)\n if p < f\n p,f = f,p\n end\n if s < w\n s,w = w,s\n cnt1,cnt2 = cnt2,cnt1\n end\n num2 = f / w\n if num2 > cnt2\n num2 = cnt2\n end\n cnt2 -= num2\n f -= num2 * w\n num1 = 0\n if f >= s\n num1 = f / s\n if num1 > cnt1\n num1 = cnt1\n end\n cnt1 -= num1\n end\n num3 = 0\n num4 = 0\n if cnt2 > 0\n num4 = p / w\n if num4 > cnt2\n num4 = cnt2\n end\n cnt2 -= num4\n p -= num4 * w\n end\n if p >= s\n num3 = p / s\n if num3 > cnt1\n num3 = cnt1\n end\n cnt1 -= num3\n p -= num3 * s\n end\n max = num1+num2+num3+num4\n while num2 > 0\n num2 -= 1\n f += w\n cnt2 += 1\n if f >= s && cnt1 > 0\n num1 += 1\n cnt1 -= 1\n f -= s\n end\n if num3 > 0 && cnt2 > 0\n num3 -= 1\n cnt1 += 1\n num4 += 1\n cnt2 -= 1\n p = p + s - w\n end\n if p >= s && cnt1 > 0\n nu = p / s\n if nu > cnt1\n nu = cnt1\n end\n num4 += nu\n cnt2 -= nu\n p -= nu * w\n end\n max = [max,num1+num2+num3+num4].max\n end\n ans << max\nend\nans.each do |x|\n puts x\nend\n"}, {"source_code": "t = gets.to_i\nans = []\nt.times do\n p,f = gets.chomp.split(\" \").map(&:to_i)\n cnt1,cnt2 = gets.chomp.split(\" \").map(&:to_i)\n s,w = gets.chomp.split(\" \").map(&:to_i)\n if p < f\n p,f = f,p\n end\n if s < w\n s,w = w,s\n cnt1,cnt2 = cnt2,cnt1\n end\n num2 = f / w\n if num2 > cnt2\n num2 = cnt2\n end\n cnt2 -= num2\n f -= num2 * w\n num1 = 0\n if f >= s\n num1 = f / s\n if num1 > cnt1\n num1 = cnt1\n end\n cnt1 -= num1\n end\n num3 = 0\n num4 = 0\n if cnt2 > 0\n num4 = p / w\n if num4 > cnt2\n num4 = cnt2\n end\n cnt2 -= num4\n p -= num4 * w\n end\n if p >= s\n num3 = p / s\n if num3 > cnt1\n num3 = cnt1\n end\n cnt1 -= num3\n p -= num3 * s\n end\n max = num1+num2+num3+num4\n while num2 > 0\n num2 -= 1\n f += w\n cnt2 += 1\n if f >= s && cnt1 > 0\n num1 += 1\n cnt1 -= 1\n f -= s\n end\n if num3 > 0 && cnt2 > 0\n num3 -= 1\n cnt1 += 1\n num4 += 1\n cnt2 -= 1\n p = p + s - w\n end\n if p >= w && cnt1 > 0\n nu = p / w\n if nu > cnt1\n nu = cnt1\n end\n num4 += nu\n cnt1 -= nu\n p -= nu * w\n end\n max = [max,num1+num2+num3+num4].max\n end\n ans << max\nend\nans.each do |x|\n puts x\nend\n"}, {"source_code": "t = gets.to_i\nans = []\nt.times do\n p,f = gets.chomp.split(\" \").map(&:to_i)\n cnt1,cnt2 = gets.chomp.split(\" \").map(&:to_i)\n s,w = gets.chomp.split(\" \").map(&:to_i)\n if p < f\n p,f = f,p\n end\n if s < w\n s,w = w,s\n cnt1,cnt2 = cnt2,cnt1\n end\n num2 = f / w\n if num2 > cnt2\n num2 = cnt2\n end\n cnt2 -= num2\n f -= num2 * w\n num1 = 0\n if f >= s\n num1 = f / s\n if num1 > cnt1\n num1 = cnt1\n end\n cnt1 -= num1\n f -= num1 * s\n end\n num3 = 0\n num4 = 0\n if cnt2 > 0\n num4 = p / w\n if num4 > cnt2\n num4 = cnt2\n end\n cnt2 -= num4\n p -= num4 * w\n end\n if p >= s\n num3 = p / s\n if num3 > cnt1\n num3 = cnt1\n end\n cnt1 -= num3\n p -= num3 * s\n end\n max = num1+num2+num3+num4\n while num2 > 0\n num2 -= 1\n f += w\n cnt2 += 1\n if num3 > 0 && cnt2 > 0\n num3 -= 1\n cnt1 += 1\n num4 += 1\n cnt2 -= 1\n p = p + s - w\n end\n if f >= s && cnt1 > 0\n num1 += 1\n cnt1 -= 1\n f -= s\n end\n if p >= s && cnt1 > 0\n nu = p / s\n if nu > cnt1\n nu = cnt1\n end\n num3 += nu\n cnt1 -= nu\n p -= nu * s\n end\n max = [max,num1+num2+num3+num4].max\n end\n ans << max\nend\nans.each do |x|\n puts x\nend\n"}], "src_uid": "ee32db8e7cdd9561d9215651ff8a262e"} {"nl": {"description": "Dima's got a staircase that consists of n stairs. The first stair is at height a1, the second one is at a2, the last one is at an (1 ≤ a1 ≤ a2 ≤ ... ≤ an). Dima decided to play with the staircase, so he is throwing rectangular boxes at the staircase from above. The i-th box has width wi and height hi. Dima throws each box vertically down on the first wi stairs of the staircase, that is, the box covers stairs with numbers 1, 2, ..., wi. Each thrown box flies vertically down until at least one of the two following events happen: the bottom of the box touches the top of a stair; the bottom of the box touches the top of a box, thrown earlier. We only consider touching of the horizontal sides of stairs and boxes, at that touching with the corners isn't taken into consideration. Specifically, that implies that a box with width wi cannot touch the stair number wi + 1.You are given the description of the staircase and the sequence in which Dima threw the boxes at it. For each box, determine how high the bottom of the box after landing will be. Consider a box to fall after the previous one lands.", "input_spec": "The first line contains integer n (1 ≤ n ≤ 105) — the number of stairs in the staircase. The second line contains a non-decreasing sequence, consisting of n integers, a1, a2, ..., an (1 ≤ ai ≤ 109; ai ≤ ai + 1). The next line contains integer m (1 ≤ m ≤ 105) — the number of boxes. Each of the following m lines contains a pair of integers wi, hi (1 ≤ wi ≤ n; 1 ≤ hi ≤ 109) — the size of the i-th thrown box. The numbers in the lines are separated by spaces.", "output_spec": "Print m integers — for each box the height, where the bottom of the box will be after landing. Print the answers for the boxes in the order, in which the boxes are given in the input. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.", "sample_inputs": ["5\n1 2 3 6 6\n4\n1 1\n3 1\n1 1\n4 3", "3\n1 2 3\n2\n1 1\n3 1", "1\n1\n5\n1 2\n1 10\n1 10\n1 10\n1 10"], "sample_outputs": ["1\n3\n4\n6", "1\n3", "1\n3\n13\n23\n33"], "notes": "NoteThe first sample are shown on the picture. "}, "positive_code": [{"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\ngets.to_i.times do\n w,h=gets.split.map(&:to_i)\n t=[a[0], a[w-1]].max\n puts t\n a[0]=t+h\nend"}, {"source_code": "n = gets.to_i #kol stypenei\na = gets.split(' ').collect!{|i| i.to_i} #visota stypenei\nm = gets.to_i #kol korobok\n\nw = []\nh = []\nm.times do\n\tt=gets.split(' ')\n\tw << t[0].to_i\n\th << t[1].to_i\nend\n\nx = []\n\n# m.times do |i|\n# \twid = w[i]-1\n# \thei = h[i]\n# \tx << a[wid]\n# \tnewhei = a[wid]+hei\n# \tn.times do |j|\n# \t\tif newhei > a[j]\n# \t\t\ta[j] = newhei\n# \t\telse\n# \t\t\tbreak\n# \t\tend\n# \tend\n# end\nnewhei = 0\nm.times do |i|\n\twid = w[i]-1\n\tif a[wid] > newhei\n\t\tx << a[wid]\n\t\tnewhei = a[wid]+h[i]\n\telse\n\t\tx << newhei\n\t\tnewhei += h[i]\n\tend\nend\n\nputs x"}], "negative_code": [{"source_code": "n = gets.to_i #kol stypenei\na = gets.split(' ').collect!{|i| i.to_i} #visota stypenei\nm = gets.to_i #kol korobok\n\nw = []\nh = []\nm.times do\n\tt=gets.split(' ')\n\tw << t[0].to_i\n\th << t[1].to_i\nend\n\nx = []\n\nm.times do |i|\n\twid = w[i]-1\n\thei = h[i]\n\tx << a[wid]\n\tnewhei = a[wid]+hei\n\t(0..(wid)).each do |j|\n\t\ta[j] = newhei\n\tend\nend\n\nputs x"}], "src_uid": "fb0e6a573daa0ee7c20d20b0d2b83756"} {"nl": {"description": "The only difference between the two versions is that in this version $$$n \\leq 1000$$$ and the sum of $$$n$$$ over all test cases does not exceed $$$1000$$$.A terminal is a row of $$$n$$$ equal segments numbered $$$1$$$ to $$$n$$$ in order. There are two terminals, one above the other. You are given an array $$$a$$$ of length $$$n$$$. For all $$$i = 1, 2, \\dots, n$$$, there should be a straight wire from some point on segment $$$i$$$ of the top terminal to some point on segment $$$a_i$$$ of the bottom terminal. You can't select the endpoints of a segment. For example, the following pictures show two possible wirings if $$$n=7$$$ and $$$a=[4,1,4,6,7,7,5]$$$. A crossing occurs when two wires share a point in common. In the picture above, crossings are circled in red.What is the maximum number of crossings there can be if you place the wires optimally?", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\leq t \\leq 1000$$$) — the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$1 \\leq n \\leq 1000$$$) — the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\leq a_i \\leq n$$$) — the elements of the array. The sum of $$$n$$$ across all test cases does not exceed $$$1000$$$.", "output_spec": "For each test case, output a single integer — the maximum number of crossings there can be if you place the wires optimally.", "sample_inputs": ["4\n\n7\n\n4 1 4 6 7 7 5\n\n2\n\n2 1\n\n1\n\n1\n\n3\n\n2 2 2"], "sample_outputs": ["6\n1\n0\n3"], "notes": "NoteThe first test case is shown in the second picture in the statement.In the second test case, the only wiring possible has the two wires cross, so the answer is $$$1$$$.In the third test case, the only wiring possible has one wire, so the answer is $$$0$$$."}, "positive_code": [{"source_code": "def merge_sort(l, r)\n return if r <= l + 1\n m = (l + r) / 2\n\n merge_sort(l, m)\n merge_sort(m, r)\n\n i = l\n j = m\n o = l\n while i < m && j < r\n if $xs[j] <= $xs[i]\n $inversions += m - i\n $tmp[o] = $xs[j]\n j += 1\n else\n $tmp[o] = $xs[i]\n i += 1\n end\n o += 1\n end\n\n while i < m\n $tmp[o] = $xs[i]\n i += 1\n o += 1\n end\n\n while j < r\n $tmp[o] = $xs[j]\n j += 1\n o += 1\n end\n\n $xs[l...r] = $tmp[l...r]\nend\n\ndef inversions(xs)\n $xs = xs\n $tmp = Array.new(xs.size)\n $inversions = 0\n merge_sort(0, xs.size)\n $inversions\nend\n\ngets.to_i.times do\n gets\n xs = gets.split.map(&:to_i)\n p inversions(xs)\nend\n"}, {"source_code": "main = -> {\r\n t = int\r\n t.times do\r\n n = int\r\n a = int1s\r\n ans = 0\r\n n.times do |i|\r\n ans += (0 ... i).count { |j| a[j] >= a[i] }\r\n end\r\n puts ans\r\n end\r\n}\r\n\r\nDEBUG = true\r\nMOD = 10**9+7\r\n$yesno = %w(No Yes)\r\nYesNo = %w(No Yes)\r\nYESNO = %w(NO YES)\r\nINF = 10**18\r\n\r\nclass Segtree\r\n def initialize(size, id, fill = id, &op)\r\n if size.is_a? Array\r\n @n = 1 << (size.size - 1).bit_length\r\n @a = [fill] * (2 * @n)\r\n @a[@n, @n] = size\r\n (@n - 1).downto(1) do |i|\r\n @a[i] = op[@a[i * 2], @a[i * 2 + 1]]\r\n end\r\n else\r\n @n = 1 << (size - 1).bit_length\r\n @a = [fill] * (2 * @n)\r\n end\r\n @a[0], @op = id, op\r\n end\r\n def [](i) @a[i + @n] end\r\n def []=(i, x)\r\n @a[i += @n] = x\r\n @a[i] = @op[@a[i * 2], @a[i * 2 + 1]] while (i >>= 1) > 0\r\n end\r\n def prod(l, r)\r\n return @a[0] if l >= r\r\n x = prod(l + 1 >> 1, r >> 1)\r\n x = @op[@a[@n + l], x] if l & 1 == 1\r\n x = @op[x, @a[@n + r - 1]] if r & 1 == 1\r\n x\r\n end\r\nend\r\n\r\nrequire \"set\"\r\nrequire \"prime\"\r\n\r\ndef int; gets.to_s.to_i end\r\ndef ints; gets.to_s.split.map { |s| s.to_i } end\r\ndef int1s; gets.to_s.split.map { |s| s.to_i - 1 } end\r\ndef float; gets.to_s.to_f end\r\ndef floats; gets.to_s.split.map { |s| s.to_f } end\r\ndef array_of(&convert); gets.to_s.split.map(&convert) end\r\ndef string; gets.to_s.chomp end\r\ndef strings; gets.to_s.split end\r\ndef rep(n, &b); Array.new(n, &b) end\r\ndef yes; puts $yesno[1] end\r\ndef no; puts $yesno[0] end\r\ndef yesno t; puts $yesno[t] end\r\ndef YesNo t; puts YesNo[t] end\r\ndef YESNO t; puts YESNO[t] end\r\ndef zip(xs, *yss); Enumerator.new { |y| xs.zip(*yss) { |a| y.yield(*a) } } end\r\ndef max(*xs, &block); block_given? ? xs.max_by(&block) : xs.max end\r\ndef min(*xs, &block); block_given? ? xs.min_by(&block) : xs.min end\r\ndef minmax(*xs, &block); block_given? ? xs.minmax_by(&block) : xs.minmax end\r\ndef gcd(*xs); xs.inject(0, :gcd) end\r\ndef matrix(h, w, fill=nil, &block); return Array.new(h) { Array.new(w, &block) } if block_given?; Array.new(h) { [fill] * w } end\r\ndef debug(x = nil); STDERR.puts (block_given? ? yield(x) : x).inspect if DEBUG; x end\r\ndef debug_grid(grid, width = 1); grid.each { |row| STDERR.puts row.map { |x| x.inspect.ljust(width) }.join(\"\") } if DEBUG; grid end\r\ndef if_debug; yield if DEBUG end\r\n\r\nmodule Boolean\r\n def coerce(other); [other, to_i] end\r\n def +@; to_i end\r\n def to_int; to_i end\r\n def *(other); to_i * other end\r\nend\r\n\r\nclass TrueClass\r\n include Boolean\r\n def to_i; 1 end\r\nend\r\n\r\nclass FalseClass\r\n include Boolean\r\n def to_i; 0 end\r\nend\r\n\r\nclass Integer\r\n def div_ceil(y); (self + y - 1) / y end\r\n def mod_inv(mod = MOD); pow(mod-2, mod) end\r\n def mod_div(y, mod = MOD); self * y.mod_inv(mod) % mod end\r\n def mod_nCr(r, mod = MOD); x = y = 1; (1..r).each { |i| x = x * (self + 1 - i) % mod; y = y * i % mod }; x.mod_div(y, mod); end\r\n def factorial(mod = MOD); (2..self).inject(1) { |f, x| f * x % mod } end\r\n def popcount; x = self; c = 0; while x > 0; c += 1 if x & 1 == 1; x >>= 1 end; c end #TODO: faster\r\n def bitbrute(&block); (1< 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\r\n def lower_bound; ac, wa = self.end_open, self.begin; while ac - wa > 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\r\n def shakutori(r2, &pred); Enumerator.new { |y| j, r = r2.begin, r2.end_open; each { |i| j += 1 while j + 1 < r and pred[i, j+1]; y.yield(i, j) } }; end\r\n def widest(&block); Enumerator.new { |y| j, n = self.begin, self.end_open; each { |i| j += 1 while j < n and block[i, j]; y.yield(i, j) if block[i, j] } } end\r\nend\r\n\r\nclass Array\r\n def power(&block); (0 ... 1 << size).each(&block) end\r\n def sorted_merge(other); a = []; i = j = 0; n, m = size, other.size; if j < m and other[j] < self[i]; a << other[j]; j += 1 else; a << self[i]; i += 1 end while i < n; a.push(*other[j..-1]) if j < m; a end\r\n def upper_bound; ac, wa = 0, size; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\r\n def lower_bound; ac, wa = size, 0; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\r\n def cum(*xs, &op); a = []; a << xs[0] if xs.size > 0; a << x = self[0]; (1...size).each { |i| a << x = op[x, self[i]] }; a end\r\n def cumdiff(range); self[range.end_open] - self[range.begin]; end\r\n def compress(kinds = uniq.sort!); map { |x| kinds.bsearch_index { _1 >= x } }; end\r\nend\r\n\r\nmodule Enumerable\r\n def sorted_uniq; x = nil; filter { |y| c = x === y; x = y; !c } end\r\n def cumsum; ys = [0]; each { |x| ys << x + ys[-1] }; ys end\r\nend\r\n\r\nmain[]"}], "negative_code": [], "src_uid": "759b3909ccdf1b9ffd800bf0f65361cc"} {"nl": {"description": "Little Susie loves strings. Today she calculates distances between them. As Susie is a small girl after all, her strings contain only digits zero and one. She uses the definition of Hamming distance:We will define the distance between two strings s and t of the same length consisting of digits zero and one as the number of positions i, such that si isn't equal to ti. As besides everything else Susie loves symmetry, she wants to find for two strings s and t of length n such string p of length n, that the distance from p to s was equal to the distance from p to t.It's time for Susie to go to bed, help her find such string p or state that it is impossible.", "input_spec": "The first line contains string s of length n. The second line contains string t of length n. The length of string n is within range from 1 to 105. It is guaranteed that both strings contain only digits zero and one.", "output_spec": "Print a string of length n, consisting of digits zero and one, that meets the problem statement. If no such string exist, print on a single line \"impossible\" (without the quotes). If there are multiple possible answers, print any of them.", "sample_inputs": ["0001\n1011", "000\n111"], "sample_outputs": ["0011", "impossible"], "notes": "NoteIn the first sample different answers are possible, namely — 0010, 0011, 0110, 0111, 1000, 1001, 1100, 1101."}, "positive_code": [{"source_code": "a, b = gets, gets\nn, c = a.length, 0\n(0...n).each do |i|\n\tif a[i] != b[i]\n\t\tc += 1\n\tend\nend\n\nif c & 1 != 0\n\tputs \"impossible\"\nelse\n\tp, i = 0, 0\n\twhile i < n do\n\t\tif p >= c/2\n\t\t\tbreak\n\t\tend\n\t\tif a[i] != b[i]\n\t\t\tprint b[i]\n\t\t\tp += 1\n\t\telse\n\t\t\tprint a[i]\n\t\tend\n\t\ti += 1\n\tend\n\tputs a[i..-1]\nend\n"}, {"source_code": "a, b = gets, gets\nn, c = a.length, 0\n(0...n).each do |i|\n\tif a[i] != b[i]\n\t\tc += 1\n\tend\nend\n\nif c & 1 != 0\n\tputs \"impossible\"\nelse\n\ts, p, i = '', 0, 0\n\twhile i < n do\n\t\tif p >= c/2\n\t\t\tbreak\n\t\tend\n\t\tif a[i] != b[i]\n\t\t\ts << b[i]\n\t\t\tp += 1\n\t\telse\n\t\t\ts << a[i]\n\t\tend\n\t\ti += 1\n\tend\n\ts << a[i..-1]\n\tputs s\nend\n"}, {"source_code": "s = gets\nt = gets\nz = s.split('').zip(t.split(''))\ndist = 0\nz.each do |si, ti|\n dist += 1 if si != ti\nend\nif dist.odd?\n puts 'impossible'\nelse\n dist /= 2\n z.each do |si, ti|\n if si == ti\n print si\n else\n if dist > 0\n dist -= 1\n print si\n else\n print ti\n end\n end\n end\n puts\nend"}, {"source_code": "# http://codeforces.com/contest/545/problem/B\ndef hd(a, b)\n (0...a.length).map do |idx|\n a[idx] == b[idx] ? 0 : 1\n end.inject(0, :+)\nend\n\ndef modify(a, b, n)\n (0...a.length).map do |idx|\n if a[idx] == b[idx]\n a[idx]\n else\n if n > 0\n n -= 1\n a[idx]\n else\n b[idx]\n end\n end\n end.join\nend\n\ns = gets.chomp.chars\nt = gets.chomp.chars\nd = hd(s, t)\n\nif d.even?\n puts modify(s, t, d / 2)\nelse\n puts 'impossible'\nend\n"}, {"source_code": "s=gets.chomp\nt=gets.chomp\n\nc1=0\ns.size.times{|i|\n\tc1+=1 if s[i]!=t[i]\n}\n\nif c1%2==1 then\n\tputs \"impossible\"\nelse\n\tans=\"\"\n\tf=true\n\ts.size.times{|i|\n\t\tif s[i]==t[i] then\n\t\t\tans<= 0\n answer.push a\n diff -= 1\n else\n answer.push b\n diff += 1\n end\n end\nend\n\nif diff != 0\n puts \"impossible\"\nelse\n puts answer.join\nend"}], "negative_code": [{"source_code": "# http://codeforces.com/contest/545/problem/B\ndef hd(a, b)\n (0...a.length).map do |idx|\n a[idx] == b[idx] ? 0 : 1\n end.inject(0, :+)\nend\n\ndef pad(chars)\n (0...chars.length).to_a.reverse.each do |idx|\n if chars[idx] == '0'\n chars[idx] = '1'\n break\n end\n end\n\n chars.join\nend\n\ns = gets.chomp\nt = gets.chomp\ns, t = [s, t].sort\nd = hd(s.chars, t.chars)\n\nif d.even?\n puts pad(s.chars)\nelse\n puts 'impossible'\nend\n"}, {"source_code": "#!/usr/bin/env ruby\n# -*- coding: utf-8 -*-\n\n# b.rb\n\ndef main()\n s = gets.chomp\n t = gets.chomp\n v = 0\n s.size.times do |i|\n if s[i] != t[i] then\n s[i] = \"10\"[v % 2]\n v += 1\n end\n end\n if (v % 2) == 0 then\n puts s\n else\n puts \"impossible\"\n end\nend\n\nif __FILE__ == $0 then\n main()\nend\n"}, {"source_code": "#!/usr/bin/env ruby\n# -*- coding: utf-8 -*-\n\n# b.rb\n\ndef main()\n s = gets\n t = gets\n v = 0\n s.size.times do |i|\n if s[i] != t[i] then\n s[i] = \"01\"[v % 2]\n v += 1\n end\n end\n if v % 2 == 0 then\n puts s\n else\n puts \"impossible\"\n end\nend\n\nif __FILE__ == $0 then\n main()\nend\n"}], "src_uid": "2df181f2d1f4063a22fd2fa2d47eef92"} {"nl": {"description": "Happy PMP is freshman and he is learning about algorithmic problems. He enjoys playing algorithmic games a lot.One of the seniors gave Happy PMP a nice game. He is given two permutations of numbers 1 through n and is asked to convert the first one to the second. In one move he can remove the last number from the permutation of numbers and inserts it back in an arbitrary position. He can either insert last number between any two consecutive numbers, or he can place it at the beginning of the permutation.Happy PMP has an algorithm that solves the problem. But it is not fast enough. He wants to know the minimum number of moves to convert the first permutation to the second. ", "input_spec": "The first line contains a single integer n (1 ≤ n ≤ 2·105) — the quantity of the numbers in the both given permutations. Next line contains n space-separated integers — the first permutation. Each number between 1 to n will appear in the permutation exactly once. Next line describe the second permutation in the same format.", "output_spec": "Print a single integer denoting the minimum number of moves required to convert the first permutation to the second.", "sample_inputs": ["3\n3 2 1\n1 2 3", "5\n1 2 3 4 5\n1 5 2 3 4", "5\n1 5 2 3 4\n1 2 3 4 5"], "sample_outputs": ["2", "1", "3"], "notes": "NoteIn the first sample, he removes number 1 from end of the list and places it at the beginning. After that he takes number 2 and places it between 1 and 3.In the second sample, he removes number 5 and inserts it after 1.In the third sample, the sequence of changes are like this: 1 5 2 3 4 1 4 5 2 3 1 3 4 5 2 1 2 3 4 5 So he needs three moves."}, "positive_code": [{"source_code": "n = gets.to_i\na = gets.split(' ').map { |t| t.to_i - 1 }\nb = gets.split(' ').map { |t| t.to_i - 1 }\nposition = []\nn.times { |i| position[b[i]] = i }\nprev = -1\nlen = 0\nn.times do |i|\n\tbreak if position[a[i]] < prev\n len += 1\n prev = position[a[i]]\t\t\nend\nputs n - len"}, {"source_code": "\ninput = ARGF.to_a\n\nshit = input.shift.to_i;\nfirst = input.shift.split ' '\nlast = input.shift.split ' '\n\nk = j = 0\n\nfor i in 0..shit-1\n if first[j] == last[i] \n j += 1 \n else \n k += 1\n end\nend\n\nputs k\n"}], "negative_code": [], "src_uid": "a26e048eb9b18f87f1703d42e172f318"} {"nl": {"description": "Vasya is about to take his first university exam in about several minutes. And it's not just some ordinary exam, it's on mathematical analysis. Of course, right now Vasya can only think of one thing: what the result of his talk with the examiner will be...To prepare for the exam, one has to study proofs of n theorems. It is known that there will be k examination cards on the exam and each card contains distinct theorems. Besides, no theorem is mentioned in more than one card (that is, theorems won't be mentioned in any card). During the exam several students may get the same card.We do not know the exact way theorems are distributed by cards, however the students that took the exam before Vasya told him what theorems their cards contained. Vasya evaluates his level of proficiency in the i-th theorem by some number ai. The level of proficiency in some card is the average of the levels of proficiency in the theorems that are included in the card. Now Vasya wants to know the minimally and maximally possible levels of his proficiency in the card he gets on the exam. Vasya wants to determine it by the data he has collected from other students. Unfortunately, Vasya has no time left to do the math and he asked you to help him.", "input_spec": "The first line contains two integers n and k (1 ≤ k ≤ n ≤ 100) — the number of theorems and the number of cards correspondingly. The second line contains n integers ai (0 ≤ ai ≤ 100), the i-th number (1 ≤ i ≤ n) corresponds to Vasya's proficiency in the i-th theorem. The third line contains number q (0 ≤ q ≤ 100) — the number of people that have taken the exam before Vasya. Each of the following q lines contains the description of a student's card: integers from 1 to n inclusive. They are the numbers of theorems included in the card in the order in which they are enumerated in the input data. The numbers are given in an arbitrary order. It is guaranteed that the given cards are valid (that is, that all theorems in one card are different and that different people get cards that either don't contain the same theorems or coincide up to the theorems' permutation).", "output_spec": "Print two real numbers, representing Vasya's minimum and maximum proficiency in the card he will get on the exam. The absolute or relative error should not exceed 10 - 6.", "sample_inputs": ["7 3\n7 15 0 19 10 5 12\n2\n1 6\n7 4", "4 2\n10 8 1 17\n2\n2 3\n3 2"], "sample_outputs": ["5.0000000000 15.5000000000", "4.5000000000 13.5000000000"], "notes": "NoteLet's analyze the first sample. Vasya's proficiency in the cards whose content he already knows equals 6 and 15.5 correspondingly. The three theorems that are left are only enough to make one exam card. If we consider all possible variants of theorems included in the card we can see that in the best case scenario Vasya gets the card that contains theorems 4 and 7 (his proficiency would equal 15.5) and in the worst case scenario he gets theorems 3 and 5 (his proficiency would equal 5).The ⌊ x⌋ operation denotes taking integer part of real number x (rounding down)."}, "positive_code": [{"source_code": "n,k=gets.split.map(&:to_i)\na=gets.split.map(&:to_i)\ni=n/k\n\nvals = []\ndels = []\nh = {}\ngets.to_i.times{\n v=0\n f = gets.split.map(&:to_i).each{|t|\n v+=a[t-1]\n dels << t-1\n }.sort.first\n h[f]=true\n vals << v/i.to_f\n}\ndels.each{|j| a[j]=nil }\na.compact!\na.sort!\n\nvals += [a[0...i].inject(&:+)/i.to_f, a.reverse[0...i].inject(&:+)/i.to_f] if a.size >= i && h.size < k\nvals.sort!\nputs \"%.7f %.7f\" % [vals[0], vals[-1]]\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\nl = n / m\na = gets.split.map(&:to_f)\nc = []\ngets.to_i.times do\n b = gets.split.map{|_| _.to_i - 1}\n next unless a[b[0]]\n c << b.map{|_| a[_]}\n b.each{|_| a[_] = nil}\nend\na.compact!\nif a.size > n - m * l\n a.sort!\n c << a[0, l] << a.reverse[0, l]\nend\nc.map!{|_| _.inject(&:+)}\nputs \"#{c.min / l} #{c.max / l}\"\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\nl = n / m\na = gets.split.map(&:to_f)\nc = []\ngets.to_i.times do\n b = gets.split.map{|_| _.to_i - 1}\n next unless a[b[0]]\n c << b.map{|_| a[_]}\n b.each{|_| a[_] = nil}\nend\na.compact!\nif a.size > n - m * l\n a.sort!\n c << a[0, l] << a.reverse[0, l]\nend\nc.map!{|_| _.inject(&:+)}\nputs \"#{c.min / l} #{c.max / l}\"\n\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\nl = n / m\na = gets.split.map(&:to_f)\nc = []\ngets.to_i.times do\n b = gets.split.map{|_| _.to_i - 1}\n next unless a[b[0]]\n c << b.map{|_| a[_]}\n b.each{|_| a[_] = nil}\nend\na.compact!\nif a.size > n - m * l\n a.sort!\n c << a[0, l] << a.reverse[0, l]\nend\nc.map!{|_| _.inject(&:+)}\nputs \"#{c.min / l} #{c.max / l}\"\n\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\nl = n / m\na = gets.split.map(&:to_f)\nc = []\ngets.to_i.times do\n b = gets.split.map{|_| _.to_i - 1}\n next unless a[b[0]]\n c << b.map{|_| a[_]}\n b.each{|_| a[_] = nil}\nend\na.compact!\nif a.size > n - m * l\n a.sort!\n c << a[0, l] << a.reverse[0, l]\nend\nc.map!{|_| _.inject(&:+)}\nputs \"#{c.min / l} #{c.max / l}\"\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\nl = n / m\na = gets.split.map(&:to_f)\nc = []\ngets.to_i.times do\n b = gets.split.map{|_| _.to_i - 1}\n next unless a[b[0]]\n c << b.map{|_| a[_]}\n b.each{|_| a[_] = nil}\nend\na.compact!\nif a.size > n - m * l\n a.sort!\n c << a[0, l] << a.reverse[0, l]\nend\nc.map!{|_| _.inject(&:+)}\nputs \"#{c.min / l} #{c.max / l}\"\n\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\nl = n / m\na = gets.split.map(&:to_f)\nc = []\ngets.to_i.times do\n b = gets.split.map{|_| _.to_i - 1}\n next unless a[b[0]]\n c << b.map{|_| a[_]}\n b.each{|_| a[_] = nil}\nend\na.compact!\nif a.size > n - m * l\n a.sort!\n c << a[0, l] << a.reverse[0, l]\nend\nc.map!{|_| _.inject(&:+)}\nputs \"#{c.min / l} #{c.max / l}\"\n\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\nl = n / m\na = gets.split.map(&:to_f)\nc = []\ngets.to_i.times do\n b = gets.split.map{|_| _.to_i - 1}\n next unless a[b[0]]\n c << b.map{|_| a[_]}\n b.each{|_| a[_] = nil}\nend\na.compact!\nif a.size > n - m * l\n a.sort!\n c << a[0, l] << a.reverse[0, l]\nend\nc.map!{|_| _.inject(&:+)}\nputs \"#{c.min / l} #{c.max / l}\""}, {"source_code": "n, m = gets.split.map(&:to_i)\nl = n / m\na = gets.split.map(&:to_f)\nc = []\ngets.to_i.times do\n b = gets.split.map{|_| _.to_i - 1}\n next unless a[b[0]]\n c << b.map{|_| a[_]}\n b.each{|_| a[_] = nil}\nend\na.compact!\nif a.size > n - m * l\n a.sort!\n c << a[0, l] << a.reverse[0, l]\nend\nc.map!{|_| _.inject(&:+)}\nputs \"#{c.min / l} #{c.max / l}\"\n\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\nl = n / m\na = gets.split.map(&:to_f)\nc = []\ngets.to_i.times do\n b = gets.split.map{|_| _.to_i - 1}\n next unless a[b[0]]\n c << b.map{|_| a[_]}\n b.each{|_| a[_] = nil}\nend\na.compact!\nif a.size > n - m * l\n a.sort!\n c << a[0, l] << a.reverse[0, l]\nend\nc.map!{|_| _.inject(&:+)}\nputs \"#{c.min / l} #{c.max / l}\"\n\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\nl = n / m\na = gets.split.map(&:to_f)\nc = []\ngets.to_i.times do\n b = gets.split.map{|_| _.to_i - 1}\n next unless a[b[0]]\n c << b.map{|_| a[_]}\n b.each{|_| a[_] = nil}\nend\na.compact!\nif a.size > n - m * l\n a.sort!\n c << a[0, l] << a.reverse[0, l]\nend\nc.map!{|_| _.inject(&:+)}\nputs \"#{c.min / l} #{c.max / l}\"\n\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\nl = n / m\na = gets.split.map(&:to_f)\nc = []\ngets.to_i.times do\n b = gets.split.map{|_| _.to_i - 1}\n next unless a[b[0]]\n c << b.map{|_| a[_]}\n b.each{|_| a[_] = nil}\nend\na.compact!\nif a.size > n - m * l\n a.sort!\n c << a[0, l] << a.reverse[0, l]\nend\nc.map!{|_| _.inject(&:+)}\nputs \"#{c.min / l} #{c.max / l}\"\n\n"}], "negative_code": [{"source_code": "n,k=gets.split.map(&:to_i)\na=gets.split.map(&:to_i)\ni=n/k\n\nvals = []\ndels = []\ngets.to_i.times{\n v=0\n gets.split.map(&:to_i).each{|t|\n v+=a[t-1]\n dels << t-1\n }\n vals << v/i.to_f\n}\ndels.each{|j|\n a[j]=nil\n}\na.compact!\na.sort!\n\nvals += [a[0...i].inject(&:+)/i.to_f, a.reverse[0...i].inject(&:+)/i.to_f] if a.size >= i\nvals.sort!\nputs \"%.7f %.7f\" % [vals[0], vals[-1]]\n"}, {"source_code": "n,k=gets.split.map(&:to_i)\na=gets.split.map(&:to_i)\ni=n/k\n\nvals = []\ndels = []\ngets.to_i.times{\n v=0\n gets.split.map(&:to_i).each{|t|\n v+=a[t-1]\n dels << t-1\n }\n vals << v/i.to_f\n}\ndels.each{|j| a[j]=nil }\na.compact!\na.sort!\n\nvals += [a[0...i].inject(&:+)/i.to_f, a.reverse[0...i].inject(&:+)/i.to_f] if a.size >= i && vals.size < k\nvals.sort!\nputs \"%.7f %.7f\" % [vals[0], vals[-1]]\n"}, {"source_code": "n,k=gets.split.map(&:to_i)\na=gets.split.map(&:to_i)\ni=n/k\n\nvals = []\ndels = []\ngets.to_i.times{\n v=0\n gets.split.map(&:to_i).each{|t|\n v+=a[t-1]\n dels << t-1\n }\n vals << v/i.to_f\n}\ndels.each{|j| a[j]=nil }\na.compact!\na.sort!\n\nvals += [a[0...i].inject(&:+)/i.to_f, a.reverse[0...i].inject(&:+)/i.to_f] if a.size >= i\nvals.sort!\nputs \"%.7f %.7f\" % [vals[0], vals[-1]]\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\nm = n / m\na = gets.split.map(&:to_f)\nc = []\ngets.to_i.times do\n b = gets.split.map{|_| _.to_i - 1}\n next unless a[b[0]]\n c << b.map{|_| a[_]}\n b.each{|_| a[_] = nil}\nend\na.compact!\nif a.size >= m\n a.sort!\n c << a[0, m] << a.reverse[0, m]\nend\nc.map!{|_| _.inject(&:+)}\nputs \"#{c.min / m} #{c.max / m}\"\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\na = gets.split.map(&:to_f)\nc = []\ngets.to_i.times do\n b = gets.split.map{|_| _.to_i - 1}\n next unless a[b[0]]\n c << b.map{|_| a[_]}\n b.each{|_| a[_] = nil}\nend\na.compact!\nif a.size > n - n / m * m\n a.sort!\n m = n / m\n c << a[0, m] << a.reverse[0, m]\nend\nc.map!{|_| _.inject(&:+)}\nputs \"#{c.min / m} #{c.max / m}\"\n"}], "src_uid": "899c5b77bfc0b4b99aff310741c9c0dd"} {"nl": {"description": "You are given n segments on the Ox-axis. You can drive a nail in any integer point on the Ox-axis line nail so, that all segments containing this point, are considered nailed down. If the nail passes through endpoint of some segment, this segment is considered to be nailed too. What is the smallest number of nails needed to nail all the segments down?", "input_spec": "The first line of the input contains single integer number n (1 ≤ n ≤ 1000) — amount of segments. Following n lines contain descriptions of the segments. Each description is a pair of integer numbers — endpoints coordinates. All the coordinates don't exceed 10000 by absolute value. Segments can degenarate to points.", "output_spec": "The first line should contain one integer number — the smallest number of nails needed to nail all the segments down. The second line should contain coordinates of driven nails separated by space in any order. If the answer is not unique, output any.", "sample_inputs": ["2\n0 2\n2 5", "5\n0 3\n4 2\n4 8\n8 10\n7 7"], "sample_outputs": ["1\n2", "3\n7 10 3"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\np = [-9e9]\nn.times.map{gets.split.map(&:to_i).sort.reverse}.sort.each do |a,b|\n\tnext if b <= p[-1]\n\tp << a\nend\np p.size - 1\nputs p[1..-1] * ' '\n"}, {"source_code": "n=gets.to_i;s=[-9e9];n.times.map{gets.split.map(&:to_i).sort.reverse}.sort.each{|x,y|s<s[-1]};p s.size-1;$><s[-1]};p s.size-1;$><s[-1]};p s.size-1;$><s[-1]};p s.size-1;$><s[-1]};p s.size-1;$><s[-1]};p s.size-1;$><(other)\n return x - other.x if x != other.x\n return (endo? ? 1 : 0) - (other.endo? ? 1 : 0)\n end\nend\n\n \nn = gets.to_i\na = []\nn.times do |i|\n x, y = *gets.split(' ').map { |x| x.to_i }\n x, y = y, x if x > y\n a << Point.new(i*2, x, false)\n a << Point.new(i*2+1, y, true)\nend\n\ndone = Array.new(n, false)\nstack = []\npts = []\n\na.sort.each do |p|\n next if done[p.n / 2]\n if p.begin?\n stack << p\n next\n end\n\n pts << p.x\n \n stack.each do |visited|\n done[visited.n / 2] = true\n end\n \n stack.clear\nend\n\nputs pts.size\nputs pts.join(' ')\n"}, {"source_code": "n=gets.to_i;s=[-9e9];n.times.map{gets.split.map(&:to_i).sort.reverse}.sort.each{|x,y|s<s[-1]};p s.size-1;$><s[-1]};p s.size-1;$><s[-1]};p s.size-1;$><s[-1]};p s.size-1;$>< 0\n print \"1 \"\n ones -= 1\n end\nend\n\nif zeros != nil\n while zeros > 0\n print \"0 \"\n zeros -= 1\n end\nend\n"}, {"source_code": "#!usr/bin/env ruby\n\n# https://codeforces.com/problemset/problem/1220/A\n\ngets\n\nword = gets.chomp\n\nz_count = 0\nn_count = 0\n\nword.each_char do |c|\n if c == 'z'\n z_count += 1 \n elsif c == 'n'\n n_count += 1\n end\nend\n\nputs (\"1 \" * n_count + \"0 \" * z_count).rstrip\n"}, {"source_code": "n = gets.to_i\na = gets.chomp.split('')\nz = e = r = o = n = 0\n\nfor char in a\n\tcase char\n\twhen 'z'\n\t\tz += 1\n\twhen 'e'\n\t\te += 1\n\twhen 'r'\n\t\tr += 1\n\twhen 'o'\n\t\to += 1\n\twhen 'n'\n\t\tn += 1\n\tend\nend\n\none_number = [o, n, e].min\no -= one_number\ne -= one_number\nzero_number = [z, e, r, o].min\n\none_number.times do\n\tprint '1 '\nend\nzero_number.times do\n\tprint '0 '\nend"}, {"source_code": "num = gets.to_i\narr = gets\n\nn = arr.split('').select{|x| x == 'n' }.count\nz = arr.split('').select{|x| x == 'z' }.count\n\nprint \"#{Array.new(n, '1').join(' ')}\" if n > 0\nprint \" \" if n > 0 && z > 0\nprint \"#{Array.new(z, '0').join(' ')}\" if z > 0\n"}, {"source_code": "n = gets.to_i\ns = gets.chomp\nn_c = s.count('n')\nz_c = s.count('z')\n\nputs ((['1'] * n_c) + (['0'] * z_c)).join(\" \")\n"}, {"source_code": "def big(n, line)\n\tplace = 0\n\treturned = ''\n\tn.times do\n\t\treturned << \"1 \" if line[place] == \"n\"\n\t\tplace = place + 1\n\tend\n\tplace = 0\n\tn.times do\n\t\treturned << \"0 \" if line[place] == \"z\"\n\t\tplace = place + 1\n\tend\n\treturned[-1] = '' unless returned[-1] == \"0\" || returned[-1] == \"1\"\n\treturn returned\nend\n\nn = gets.chomp.to_i\nline = gets.chomp\nputs(big(n, line))"}, {"source_code": "n = gets\nstr = gets\nzeros = str.count 'z'\nones = str.count 'n'\n\nans = []\n1.upto(ones) {ans << \"1\"}\n1.upto(zeros) {ans << \"0\"}\n\nputs ans.join(\" \")\n"}, {"source_code": "n = gets.chomp.to_i\ns = gets.chomp\nzero = s.count(\"z\")\none = s.count(\"n\")\nputs \"1 \" * one + \"0 \" * zero\n\n"}, {"source_code": "gets\ns = gets.strip\nz = s.split('').count('z')\n((s.size - z*4)/3).times { print '1 ' }\nz.times { print '0 ' }"}, {"source_code": "gets\ns = gets.chomp\n\nputs (Array.new(s.count('n'), '1') + Array.new(s.count('z'), '0')).join(' ')\n"}], "negative_code": [{"source_code": "gets\ns = gets.strip\nz = s.split('').count('z')\n((s.size - z*4)/3).times { print 1 }\nz.times { print 0 }"}], "src_uid": "5e5dbd70c7fcedf0f965aed5bafeb06c"} {"nl": {"description": "A group of tourists is going to kayak and catamaran tour. A rented lorry has arrived to the boat depot to take kayaks and catamarans to the point of departure. It's known that all kayaks are of the same size (and each of them occupies the space of 1 cubic metre), and all catamarans are of the same size, but two times bigger than kayaks (and occupy the space of 2 cubic metres).Each waterborne vehicle has a particular carrying capacity, and it should be noted that waterborne vehicles that look the same can have different carrying capacities. Knowing the truck body volume and the list of waterborne vehicles in the boat depot (for each one its type and carrying capacity are known), find out such set of vehicles that can be taken in the lorry, and that has the maximum total carrying capacity. The truck body volume of the lorry can be used effectively, that is to say you can always put into the lorry a waterborne vehicle that occupies the space not exceeding the free space left in the truck body.", "input_spec": "The first line contains a pair of integer numbers n and v (1 ≤ n ≤ 105; 1 ≤ v ≤ 109), where n is the number of waterborne vehicles in the boat depot, and v is the truck body volume of the lorry in cubic metres. The following n lines contain the information about the waterborne vehicles, that is a pair of numbers ti, pi (1 ≤ ti ≤ 2; 1 ≤ pi ≤ 104), where ti is the vehicle type (1 – a kayak, 2 – a catamaran), and pi is its carrying capacity. The waterborne vehicles are enumerated in order of their appearance in the input file.", "output_spec": "In the first line print the maximum possible carrying capacity of the set. In the second line print a string consisting of the numbers of the vehicles that make the optimal set. If the answer is not unique, print any of them.", "sample_inputs": ["3 2\n1 2\n2 7\n1 3"], "sample_outputs": ["7\n2"], "notes": null}, "positive_code": [{"source_code": "n,v = gets.split(\" \").map{|x| x.to_i}\nts1 = Array.new\nts2 = Array.new\nn.times do |i|\n t,p = gets.split(\" \").map{|x| x.to_i}\n if t == 1\n ts1 << [i,p]\n else\n ts2 << [i,p]\n end\nend\nts1.sort!{|a,b| -(a[1]<=>b[1])}\nts2.sort!{|a,b| -(a[1]<=>b[1])}\np2 = [(v-2)/2,ts2.size-1].min\np1 = [v-3-p2*2,ts1.size-1].min\nwhile p1+1=0\n if p1+2 ts2[p2][1]\n p1+=2\n p2-=1\n else\n break\n end\n else\n if ts1[p1+1][1] > ts2[p2][1]\n p1+=1\n p2-=1\n else\n break\n end\n end\nend\nsum=0\nts = Array.new\n0.upto(p1){|x| sum+=ts1[x][1];ts< a[1] }\ncatamaran.sort! { |a, b| b[1] <=> a[1] }\n\n\nmax_c = [max_size / 2, catamaran.length].min\nmax_k = [max_size - 2 * max_c, kayak.length].min\n\nwhile max_c > 0 && max_k <= kayak.length\n if max_k + 2 <= kayak.length #there are two more elements in the array\n if catamaran[max_c - 1][1] < (kayak[max_k][1] + kayak[max_k + 1][1])\n max_c = max_c - 1\n max_k = max_k + 2\n else\n break\n end\n else\n break\n end\nend\n\nans_set = []\nsum = 0\n0.upto(max_c - 1) { |i| sum = sum + catamaran[i][1]; ans_set << catamaran[i][0] }\n0.upto(max_k - 1) { |i| sum = sum + kayak[i][1]; ans_set << kayak[i][0] }\nputs sum\nputs ans_set.join(\" \")"}, {"source_code": "n, max = gets().split(\" \").collect {|x| x.to_i}\nkayaks = []\ncatemerons = []\nn.times do |i|\n type, capacity = (gets().split(\" \").collect {|x| x.to_i})\n if type == 1\n kayaks << [i, capacity]\n else\n catemerons << [i, capacity]\n end\nend\nkayaks.sort! {|x, y| y[1] <=> x[1]}\ncatemerons.sort! {|x, y| y[1] <=> x[1]}\n\nmax_c = [catemerons.length, (max/2).to_i].min\nmax_k = [kayaks.length, max].min\n\n#puts \"kayaks: #{kayaks.inspect}, catemerons: #{catemerons.inspect}, max_k: #{max_k}, max_c: #{max_c}\"\n\nwt = 0\noptimal = 0\nselected_k = []\nselected_c = []\ni = 0\nwhile (wt < max) && (i < max_c) do\n if (wt + 2) <= max\n wt += 2\n optimal += catemerons[i][1]\n i += 1\n else\n break\n end \nend\n\n#puts \"After catemerons, wt: #{wt}, i: #{i}\"\n\nj = 0\nwhile (wt < max) && (j < max_k) do\n wt += 1\n optimal += kayaks[j][1]\n j += 1\nend\n\n#puts \"After kayaks, wt: #{wt}, j: #{j}\"\n\nwhile (i > 0) && (j < max_k) do\n proposed_optimal1 = optimal - catemerons[i - 1][1] + kayaks[j][1]\n proposed_optimal2 = proposed_optimal1\n proposed_optimal2 += kayaks[j + 1][1] if (j < (max_k - 1))\n if proposed_optimal2 > optimal\n i -= 1\n j += 1\n optimal = proposed_optimal2\n j += 1 if (proposed_optimal2 > proposed_optimal1)\n else\n break\n end\nend\n\nputs optimal\n(0..(i - 1)).each do |i| print \"#{catemerons[i][0] + 1} \" end\n(0..(j - 1)).each do |j| print \"#{kayaks[j][0] + 1} \" end\n"}, {"source_code": "n, max = gets().split(\" \").collect {|x| x.to_i}\nkayaks = []\ncatemerons = []\nn.times do |i|\n type, capacity = (gets().split(\" \").collect {|x| x.to_i})\n if type == 1\n kayaks << [i, capacity]\n else\n catemerons << [i, capacity]\n end\nend\nkayaks.sort! {|x, y| y[1] <=> x[1]}\ncatemerons.sort! {|x, y| y[1] <=> x[1]}\n\nmax_c = [catemerons.length, (max/2).to_i].min\nmax_k = [kayaks.length, max].min\n\n#puts \"kayaks: #{kayaks.inspect}, catemerons: #{catemerons.inspect}, max_k: #{max_k}, max_c: #{max_c}\"\n\nwt = 0\noptimal = 0\nselected_k = []\nselected_c = []\ni = 0\nwhile (wt < max) && (i < max_c) do\n if (wt + 2) <= max\n wt += 2\n optimal += catemerons[i][1]\n i += 1\n else\n break\n end \nend\n\n#puts \"After catemerons, wt: #{wt}, i: #{i}\"\n\nj = 0\nwhile (wt < max) && (j < max_k) do\n wt += 1\n optimal += kayaks[j][1]\n j += 1\nend\n\n#puts \"After kayaks, wt: #{wt}, j: #{j}\"\n\nwhile (i > 0) && (j < max_k) do\n proposed_optimal1 = optimal - catemerons[i - 1][1] + kayaks[j][1]\n proposed_optimal2 = proposed_optimal1\n proposed_optimal2 += kayaks[j + 1][1] if (j < (max_k - 1))\n if (proposed_optimal1 > optimal) || (proposed_optimal2 > optimal)\n i -= 1\n j += 1\n optimal = proposed_optimal1\n if (proposed_optimal2 > proposed_optimal1)\n j += 1\n optimal = proposed_optimal2\n end\n else\n break\n end\nend\n\nputs optimal\n(0..(i - 1)).each do |i| print \"#{catemerons[i][0] + 1} \" end\n(0..(j - 1)).each do |j| print \"#{kayaks[j][0] + 1} \" end\n"}, {"source_code": "n,v = gets.split.map(&:to_i)\n\na1 = []\na2 = []\n\nn.times do |i|\n t,q = gets.split.map(&:to_i)\n if t == 1 then a1 << [q, i]\n else a2 << [q, i]\n end\nend\n\na2.sort!\na2.reverse!\na1.sort!\na1.reverse!\n\ntwo = [v / 2, a2.length].min\ntotal = 0\ntwo.times do |i|\n total += a2[i][0]\nend\n\none = [v - two * 2, a1.length].min\none.times do |i| total += a1[i][0] end\n\nwhile two > 0 and one <= a1.length - 2 and a2[two - 1][0] < a1[one][0] + a1[one + 1][0] do\n total -= a2[two - 1][0]\n total += a1[one][0] + a1[one + 1][0];\n two -= 1\n one += 2\nend\n\nputs total\nans = []\ntwo.times do |i| ans << 1 + a2[i][1] end\none.times do |i| ans << 1 + a1[i][1] end\n\nputs ans * ' '\n\n"}, {"source_code": "Vehicle = Struct.new(:index, :capacity)\n\nclass LorryLoader\n def self.load(lorry_volume, vehicle_registry)\n kayaks = []\n catamarans = []\n vehicle_registry.each.with_index do |vehicle, index|\n type, capacity = vehicle.split.map(&:to_i)\n kayaks << Vehicle.new(index + 1, capacity) if type == 1\n catamarans << Vehicle.new(index + 1, capacity) if type == 2\n end\n\n kayaks.sort_by! { |x| -x.capacity }\n catamarans.sort_by! { |x| -x.capacity }\n\n selected_vehicles = []\n total_capacity = volume = ki = ci = 0\n while (ki < kayaks.size || ci < catamarans.size) && volume < lorry_volume\n k_cap = ki < kayaks.size ? kayaks[ki].capacity : 0\n k_cap += kayaks[ki + 1].capacity if ki < kayaks.size - 1\n c_cap = ci < catamarans.size && lorry_volume - volume > 1 ? catamarans[ci].capacity : 0\n\n break if k_cap == 0 && c_cap == 0\n\n if (c_cap >= k_cap)\n selected_vehicles << catamarans[ci].index\n total_capacity += catamarans[ci].capacity\n volume += 2\n ci += 1\n else\n selected_vehicles << kayaks[ki].index\n total_capacity += kayaks[ki].capacity\n volume += 1\n ki += 1\n end\n end\n\n return total_capacity, selected_vehicles\n end\nend\n\nif __FILE__ == $0\n vehicle_count, lorry_volume = gets.split.map(&:to_i)\n vehicle_registry = []\n vehicle_count.times { vehicle_registry << gets }\n\n capacity, selected_vehicles = LorryLoader.load(lorry_volume, vehicle_registry)\n\n puts capacity\n puts selected_vehicles.join(' ')\nend\n"}, {"source_code": "def load_lorry(lorryVolume, kayaks, catamarans)\n kayaks.sort_by! { |x| x[1] * -1 }\n catamarans.sort_by! { |x| x[1] * -1 }\n\n capacity = volume = ki = ci = 0\n selected_vehicles = []\n while (ki < kayaks.length || ci < catamarans.length) && volume < lorryVolume\n c_value = ci < catamarans.length && lorryVolume - volume > 1 ? catamarans[ci][1] : 0\n k_value = ki < kayaks.length ? kayaks[ki][1] : 0\n\n k_value += kayaks[ki + 1][1] if ki < kayaks.length - 1\n\n break if k_value == 0 && c_value == 0\n\n if c_value > k_value\n capacity += catamarans[ci][1]\n selected_vehicles << catamarans[ci][0]\n ci += 1;\n volume += 2\n else\n capacity += kayaks[ki][1]\n selected_vehicles << kayaks[ki][0]\n ki += 1\n volume += 1\n end\n end\n\n return capacity, selected_vehicles\nend\n\nif __FILE__ == $0\n n, lorryVolume = gets.split.map(&:to_i)\n kayaks = []\n catamarans = []\n n.times do |i|\n t, c = gets.split.map(&:to_i)\n kayaks << [i + 1, c] if t == 1\n catamarans << [i + 1, c] if t == 2\n end\n\n capacity, selected_vehicles = load_lorry(lorryVolume, kayaks, catamarans)\n\n puts capacity\n puts selected_vehicles.join(\" \")\nend\n"}, {"source_code": "def load_lorry(lorryVolume, vehicles)\n kayaks = []\n catamarans = []\n\n vehicles.each_with_index do |v, i|\n kayaks << [i + 1, v[1]] if v[0] == 1\n catamarans << [i + 1, v[1]] if v[0] == 2\n end\n\n kayaks.sort_by! { |x| x[1] * -1 }\n catamarans.sort_by! { |x| x[1] * -1 }\n\n capacity = volume = ki = ci = 0\n selected_vehicles = []\n while (ki < kayaks.length || ci < catamarans.length) && volume < lorryVolume\n c_value = ci < catamarans.length && lorryVolume - volume > 1 ? catamarans[ci][1] : 0\n k_value = ki < kayaks.length ? kayaks[ki][1] : 0\n\n k_value += kayaks[ki + 1][1] if ki < kayaks.length - 1\n\n break if k_value == 0 && c_value == 0\n\n if c_value > k_value\n capacity += catamarans[ci][1]\n selected_vehicles << catamarans[ci][0]\n ci += 1;\n volume += 2\n else\n capacity += kayaks[ki][1]\n selected_vehicles << kayaks[ki][0]\n ki += 1\n volume += 1\n end\n end\n\n return capacity, selected_vehicles\nend\n\nif __FILE__ == $0\n n, lorryVolume = gets.split.map(&:to_i)\n vehicles = []\n n.times { vehicles << gets.split.map(&:to_i) }\n\n capacity, selected_vehicles = load_lorry(lorryVolume, vehicles)\n\n puts capacity\n puts selected_vehicles.join(\" \")\nend\n"}, {"source_code": "def load_lorry(lorryVolume, vehicles)\n kayaks = []\n catamarans = []\n\n vehicles.each_with_index do |v, i|\n kayaks << [i + 1, v[1]] if v[0] == 1\n catamarans << [i + 1, v[1]] if v[0] == 2\n end\n\n kayaks.sort! { |a, b| b[1] <=> a[1] }\n catamarans.sort! { |a, b| b[1] <=> a[1] }\n\n capacity = volume = ki = ci = 0\n selected_vehicles = []\n while (ki < kayaks.length || ci < catamarans.length) && volume < lorryVolume\n c_value = ci < catamarans.length && lorryVolume - volume > 1 ? catamarans[ci][1] : 0\n k_value = ki < kayaks.length ? kayaks[ki][1] : 0\n\n k_value += kayaks[ki + 1][1] if ki < kayaks.length - 1\n\n break if k_value == 0 && c_value == 0\n\n if c_value > k_value\n capacity += catamarans[ci][1]\n selected_vehicles << catamarans[ci][0]\n ci += 1;\n volume += 2\n else\n capacity += kayaks[ki][1]\n selected_vehicles << kayaks[ki][0]\n ki += 1\n volume += 1\n end\n end\n\n return capacity, selected_vehicles\nend\n\nif __FILE__ == $0\n n, lorryVolume = gets.split.map(&:to_i)\n vehicles = []\n n.times { vehicles << gets.split.map(&:to_i) }\n\n capacity, selected_vehicles = load_lorry(lorryVolume, vehicles)\n\n puts capacity\n puts selected_vehicles.join(\" \")\nend\n"}, {"source_code": "def load_lorry(lorryVolume, vehicles)\n kayaks = []\n catamarans = []\n\n vehicles.each_with_index do |v, i|\n if (v[0] == 1)\n kayaks << [i + 1, v[1]]\n else\n catamarans << [i + 1, v[1]]\n end\n end\n\n kayaks.sort_by! { |x| x[1] * -1 }\n catamarans.sort_by! { |x| x[1] * -1 }\n\n volume = ki = ci = 0\n while ((ki < kayaks.length || ci < catamarans.length) && volume < lorryVolume)\n c_value = ci < catamarans.length && lorryVolume - volume > 1 ? catamarans[ci][1] : 0\n k_value = ki < kayaks.length ? kayaks[ki][1] : 0\n\n k_value += kayaks[ki + 1][1] if ki < kayaks.length - 1\n\n if (c_value > k_value)\n ci += 1;\n volume += 2\n else\n ki += 1\n volume += 1\n end\n end\n\n sel_vehicles = kayaks.take(ki) + catamarans.take(ci)\n return sel_vehicles.reduce(0) { |c, x| c + x[1] }, sel_vehicles.map { |x| x[0] }\nend\n\nif __FILE__ == $0\n n, lorryVolume = gets.split.map(&:to_i)\n vehicles = []\n n.times { vehicles << gets.split.map(&:to_i) }\n\n capacity, selected_vehicles = load_lorry(lorryVolume, vehicles)\n\n puts capacity\n puts selected_vehicles.join(\" \")\nend\n"}, {"source_code": "n,v = gets.split.map(&:to_i)\n\na1 = []\na2 = []\n\nn.times do |i|\n t,q = gets.split.map(&:to_i)\n if t == 1 then a1 << [q, i]\n else a2 << [q, i]\n end\nend\n\na2.sort!\na2.reverse!\na1.sort!\na1.reverse!\n\ntwo = [v / 2, a2.length].min\ntotal = 0\ntwo.times do |i|\n total += a2[i][0]\nend\n\none = [v - two * 2, a1.length].min\none.times do |i| total += a1[i][0] end\n\nwhile two > 0 and one <= a1.length - 2 and a2[two - 1][0] < a1[one][0] + a1[one + 1][0] do\n total -= a2[two - 1][0]\n total += a1[one][0] + a1[one + 1][0];\n two -= 1\n one += 2\nend\n\nputs total\nans = []\ntwo.times do |i| ans << 1 + a2[i][1] end\none.times do |i| ans << 1 + a1[i][1] end\n\nputs ans * ' '"}, {"source_code": "n, v = gets.split.map(&:to_i)\n\nkayaks = []\ncatamarans = []\nn.times do |index|\n\ttype, p = gets.split.map(&:to_i)\n\tif type == 1\n\t\tkayaks << [index + 1, p]\n\telse\n\t\tcatamarans << [index + 1, p]\n\tend\nend\n\nkayaks.sort! { |e1, e2| e2[1] <=> e1[1] }\ncatamarans.sort! { |e1, e2| e2[1] <=> e1[1] }\n\npossible_capacity = {}\nmax_c = [v / 2, catamarans.length].min\nmax_k = [v - 2 * max_c, kayaks.length].min\nindexes = []\ntotal = (0...max_c).inject(0) { |sum, value| sum + catamarans[value][1] } +\n\t\t\t\t(0...max_k).inject(0) { |sum, value| sum + kayaks[value][1] }\n\nwhile max_c > 0 && max_k <= kayaks.length - 2 && catamarans[max_c - 1][1] < kayaks[max_k][1] + kayaks[max_k + 1][1]\n\ttotal -= catamarans[max_c - 1][1]\n\ttotal += kayaks[max_k][1] + kayaks[max_k + 1][1]\n\tmax_c -= 1\n\tmax_k += 2\nend\n\nindexes = []\nmax_c.times do |index|\n\tindexes << catamarans[index][0]\nend\nmax_k.times do |index|\n\tindexes << kayaks[index][0]\nend\n\nputs total\nputs indexes.join(' ')"}], "negative_code": [{"source_code": "n,v = gets.split(\" \").map{|x| x.to_i}\nts = Array.new\nn.times do |i|\n t,p = gets.split(\" \").map{|x| x.to_i}\n ts << [i,t,p]\nend\nts.sort!{|a,b| -(a[2]*2/a[1]<=>b[2]*2/b[1])}\nmax = 0\nset = Array.new\nts.each_with_index do |t,i|\n break if v<=0\n applicable = false\n if v>2\n applicable = true\n else\n if v==t[1]\n applicable=true\n elsif v>t[1]\n if ts[i+1]\n if ts[i+1][1]==1\n applicable=true\n else\n nt = ts[i+1..-1].select{|x| x[1]==1}.first\n if nt\n if t[2]+nt[2]>ts[i+1][2]\n applicable=true\n end\n end\n end\n else\n applicable=true\n end\n end\n end\n if applicable\n set << t[0]+1\n v -= t[1]\n max += t[2]\n end\nend\nputs max\nputs set.join(\" \")\n"}, {"source_code": "n,v = gets.split(\" \").map{|x| x.to_i}\nts1 = Array.new\nts2 = Array.new\nn.times do |i|\n t,p = gets.split(\" \").map{|x| x.to_i}\n if t == 1\n ts1 << [i,p]\n else\n ts2 << [i,p]\n end\nend\nts1.sort!{|a,b| -(a[1]<=>b[1])}\nts2.sort!{|a,b| -(a[1]<=>b[1])}\np2 = [(v-2)/2,ts2.size-1].min\np1 = n-3-p2*2\nwhile p1+2=0\n if ts1[p1+1][1]+ts1[p1+2][1] > ts2[p2][1]\n p1+=2\n p2-=1\n else\n break\n end\nend\nsum=0\nts = Array.new\n0.upto(p1){|x| sum+=ts1[x][1];ts< a[1] }\ncatamaran.sort! { |a, b| b[1] <=> a[1] }\n\n\nmax_c = [max_size / 2, catamaran.length].min\nmax_k = [max_size - 2 * max_c, kayak.length].min\n\nwhile max_c > 0 && max_k <= kayak.length\n if max_k + 2 <= kayak.length #there are two more elements in the array\n if catamaran[max_c - 1][1] < (kayak[max_k][1] + kayak[max_k + 1][1])\n max_c = max_c - 1\n max_k = max_k + 2\n else\n break\n end\n else\n if catamaran[max_c - 1][1] < kayak[max_k][1]\n max_c = max_c - 1\n max_k = max_k + 1\n else\n break\n end\n end\nend\n\nans_set = []\nsum = 0\n0.upto(max_c - 1) { |i| sum = sum + catamaran[i][1]; ans_set << catamaran[i][0] }\n0.upto(max_k - 1) { |i| sum = sum + kayak[i][1]; ans_set << kayak[i][0] }\nputs sum\nans_set.sort!\nputs ans_set.to_s[1..-2]"}, {"source_code": "num, max_size = gets.split.map(& :to_i)\nkayak = []\ncatamaran = []\nnum.times do |index|\n t, p = gets.split.map(& :to_i)\n if t == 1\n kayak << [index + 1, p]\n else\n catamaran << [index + 1, p]\n end\nend\n\nkayak.sort! { |a, b| b[1] <=> a[1] }\ncatamaran.sort! { |a, b| b[1] <=> a[1] }\n\n\nmax_c = [max_size / 2, catamaran.length].min\nmax_k = [max_size - 2 * max_c, kayak.length].min\n\nwhile max_c > 0 && max_k <= kayak.length && max_k > 0\n if max_k + 2 <= kayak.length #there are two more elements in the array\n if catamaran[max_c - 1][1] < (kayak[max_k][1] + kayak[max_k + 1][1])\n max_c = max_c - 1\n max_k = max_k + 2\n else\n break\n end\n else\n if catamaran[max_c - 1][1] < kayak[max_k][1]\n max_c = max_c - 1\n max_k = max_k + 1\n else\n break\n end\n end\nend\n\nans_set = []\nsum = 0\n0.upto(max_c - 1) { |i| sum = sum + catamaran[i][1]; ans_set << catamaran[i][0] }\n0.upto(max_k - 1) { |i| sum = sum + kayak[i][1]; ans_set << kayak[i][0] }\nputs sum\nputs ans_set.join(\" \")"}, {"source_code": "num, max_size = gets.split.map(& :to_i)\nkayak = []\ncatamaran = []\nnum.times do |index|\n t, p = gets.split.map(& :to_i)\n if t == 1\n kayak << [index + 1, p]\n else\n catamaran << [index + 1, p]\n end\nend\n\nkayak.sort! { |a, b| b[1] <=> a[1] }\ncatamaran.sort! { |a, b| b[1] <=> a[1] }\n\n\nmax_c = [max_size / 2, catamaran.length].min\nmax_k = [max_size - 2 * max_c, kayak.length].min\n\nwhile max_c > 0 && max_k <= kayak.length\n if max_k + 2 <= kayak.length #there are two more elements in the array\n p \"OK\"\n if catamaran[max_c - 1][1] < (kayak[max_k][1] + kayak[max_k + 1][1])\n max_c = max_c - 1\n max_k = max_k + 2\n else\n break\n end\n else\n if catamaran[max_c - 1][1] < kayak[max_k][1]\n max_c = max_c - 1\n max_k = max_k + 1\n else\n break\n end\n end\nend\n\nans_set = []\nsum = 0\n0.upto(max_c - 1) { |i| sum = sum + catamaran[i][1]; ans_set << catamaran[i][0] }\n0.upto(max_k - 1) { |i| sum = sum + kayak[i][1]; ans_set << kayak[i][0] }\nputs sum\nans_set.sort!\nputs ans_set.to_s[1..-2]"}, {"source_code": "n, max = gets().split(\" \").collect {|x| x.to_i}\narr = []\ni = 0\nn.times do |i|\n arr << (gets().split(\" \").collect {|x| x.to_i}).push(i + 1)\nend\narr.sort! {|x, y| y[1] <=> x[1]}\n#puts arr.inspect\nwt = 0\noptimal = 0\nn = []\narr.each_with_index do |a, i|\n if ((wt + a[0]) <= max)\n optimal += a[1]\n n << a[2]\n wt += a[0]\n break if wt == max\n end\nend\nputs optimal\nputs n.join(\",\")\n"}, {"source_code": "n, max = gets().split(\" \").collect {|x| x.to_i}\narr = []\ni = 0\nn.times do |i|\n arr << (gets().split(\" \").collect {|x| x.to_i}).push(i + 1)\nend\narr.sort! {|x, y| y[1] <=> x[1]}\n#puts arr.inspect\nwt = 0\noptimal = 0\nn = []\narr.each_with_index do |a, i|\n if ((wt + a[0]) <= max)\n optimal += a[1]\n n << a[2]\n wt += a[0]\n break if wt == max\n end\nend\nputs optimal\nputs n.join(\" \")\n"}, {"source_code": "n, max = gets().split(\" \").collect {|x| x.to_i}\narr = []\ni = 0\nn.times do |i|\n arr << (gets().split(\" \").collect {|x| x.to_i}).push(i + 1)\nend\narr.sort! {|x, y| y[1] <=> x[1]}\n#puts arr.inspect\nwt = 0\noptimal = 0\nn1 = []\nn2 = []\narr.each_with_index do |a, i|\n if ((wt + a[0]) <= max)\n optimal += a[1]\n if a[0] == 1\n n1 << a[2]\n else\n n2 << a[2]\n end\n wt += a[0]\n break if wt == max\n end\nend\nputs optimal\nputs n2.concat(n1).join(\" \")\n"}, {"source_code": "n, max = gets().split(\" \").collect {|x| x.to_i}\nkayaks = []\ncatemerons = []\nn.times do |i|\n type, capacity = (gets().split(\" \").collect {|x| x.to_i})\n if type == 1\n kayaks << [i, capacity]\n else\n catemerons << [i, capacity]\n end\nend\nkayaks.sort! {|x, y| y[1] <=> x[1]}\ncatemerons.sort! {|x, y| y[1] <=> x[1]}\n\nmax_c = [catemerons.length, (max/2).to_i].min\nmax_k = [kayaks.length, max].min\n\nputs \"kayaks: #{kayaks.inspect}, catemerons: #{catemerons.inspect}, max_k: #{max_k}, max_c: #{max_c}\"\n\nwt = 0\noptimal = 0\nselected_k = []\nselected_c = []\ni = 0\nwhile (wt < max) && (i < max_c) do\n if (wt + 2) <= max\n wt += 2\n optimal += catemerons[i][1]\n i += 1\n else\n break\n end \nend\n\n#puts \"After catemerons, wt: #{wt}, i: #{i}\"\n\nj = 0\nwhile (wt < max) && (j < max_k) do\n wt += 1\n optimal += kayaks[j][1]\n j += 1\nend\n\n#puts \"After kayaks, wt: #{wt}, j: #{j}\"\n\nwhile (i > 0) && (j < max_k) do\n proposed_optimal1 = optimal - catemerons[i - 1][1] + kayaks[j][1]\n proposed_optimal2 = proposed_optimal1\n proposed_optimal2 += kayaks[j + 1][1] if (j < (max_k - 1))\n if (proposed_optimal1 > optimal) || (proposed_optimal2 > optimal)\n i -= 1\n j += 1\n optimal = proposed_optimal1\n if (proposed_optimal2 > proposed_optimal1)\n j += 1\n optimal = proposed_optimal2\n end\n else\n break\n end\nend\n\nputs optimal\n(0..(i - 1)).each do |i| print \"#{catemerons[i][0] + 1} \" end\n(0..(j - 1)).each do |j| print \"#{kayaks[j][0] + 1} \" end\n"}, {"source_code": "n,v = gets.split.map(&:to_i)\n\na1 = []\na2 = []\n\nn.times do |i|\n t,q = gets.split.map(&:to_i)\n if t == 1 then a1 << [q, i]\n else a2 << [q, i]\n end\nend\n\na2.sort!\na2.reverse!\na1.sort!\na1.reverse!\n\none = 0\ntwo = [v / 2, a2.length].min\ntotal = 0\ntwo.times do |i|\n total += a2[i][0]\nend\n\nwhile two > 0 and one < a1.length - 2 and a2[two - 1][0] < a1[one][0] + a1[one + 1][0] do\n total -= a2[two - 1][0]\n total += a1[one][0] + a1[one + 1][0];\n two -= 1\n one += 2\nend\n\nputs total\nans = []\ntwo.times do |i| ans << 1 + a2[i][1] end\none.times do |i| ans << 1 + a1[i][1] end\n\nputs ans * ' '\n\n"}, {"source_code": "Vehicle = Struct.new(:index, :capacity)\n\nclass LorryLoader\n def self.load(lorry_volume, vehicle_registry)\n kayaks = []\n catamarans = []\n\n vehicle_registry.each.with_index do |vehicle, index|\n type, capacity = vehicle.split.map(&:to_i)\n if type == 1\n kayaks << Vehicle.new(index + 1, capacity)\n elsif\n catamarans << Vehicle.new(index + 1, capacity)\n end\n end\n\n kayaks.sort_by! { |x| -x.capacity }\n catamarans.sort_by! { |x| -x.capacity }\n\n selected_vehicles = []\n total_capacity = volume = ki = ci = 0\n while (ki < kayaks.size || ci < catamarans.size) && volume < lorry_volume\n k_cap = ki < kayaks.size ? kayaks[ki].capacity : 0\n k_cap += (ki < kayaks.size - 1 ? kayaks[ki - 1].capacity : 0)\n c_cap = ci < catamarans.size && lorry_volume - volume > 1 ? catamarans[ci].capacity : 0\n\n break if k_cap == 0 && c_cap == 0\n\n if (c_cap >= k_cap)\n selected_vehicles << catamarans[ci].index\n total_capacity += catamarans[ci].capacity\n volume += 2\n ci += 1\n else\n selected_vehicles << kayaks[ki].index\n total_capacity += kayaks[ki].capacity\n volume += 1\n ki += 1\n end\n end\n\n return total_capacity, selected_vehicles\n end\nend\n\nif __FILE__ == $0\n vehicle_count, lorry_volume = gets.split.map(&:to_i)\n vehicle_registry = []\n vehicle_count.times { vehicle_registry << gets }\n\n capacity, selected_vehicles = LorryLoader.load(lorry_volume, vehicle_registry)\n\n puts capacity\n puts selected_vehicles.join(' ')\nend\n"}, {"source_code": "def load_lorry(lorryVolume, vehicles)\n kayaks = []\n catamarans = []\n\n vehicles.each_with_index do |v, i|\n kayaks << [i + 1, v[1]] if v[0] == 1\n catamarans << [i + 1, v[1]] if v[0] == 2\n end\n\n kayaks.sort!\n catamarans.sort!\n\n capacity = volume = ki = ci = 0\n selected_vehicles = []\n while (ki < kayaks.length || ci < catamarans.length) && volume < lorryVolume\n c_value = ci < catamarans.length && lorryVolume - volume > 1 ? catamarans[ci][1] : 0\n k_value = ki < kayaks.length ? kayaks[ki][1] : 0\n\n k_value += kayaks[ki + 1][1] if ki < kayaks.length - 1\n\n break if k_value == 0 && c_value == 0\n\n if c_value > k_value\n capacity += catamarans[ci][1]\n selected_vehicles << catamarans[ci][0]\n ci += 1;\n volume += 2\n else\n capacity += kayaks[ki][1]\n selected_vehicles << kayaks[ki][0]\n ki += 1\n volume += 1\n end\n end\n\n return capacity, selected_vehicles\nend\n\nif __FILE__ == $0\n n, lorryVolume = gets.split.map(&:to_i)\n vehicles = []\n n.times { vehicles << gets.split.map(&:to_i) }\n\n capacity, selected_vehicles = load_lorry(lorryVolume, vehicles)\n\n puts capacity\n puts selected_vehicles.join(\" \")\nend\n"}, {"source_code": "n, v = gets.split.map(&:to_i)\n\nkayaks = []\ncatamarans = []\n\nn.times do\n\tvehicle_info = gets.split.map(&:to_i)\n\tif vehicle_info[0] == 1\n\t\tkayaks << vehicle_info[1]\n\telse\n\t\tcatamarans << vehicle_info[1]\n\tend\nend\n\nkayaks.sort! { |e1, e2| e2 <=> e1 }\ncatamarans.sort! { |e1, e2| e2 <=> e1 }\n\npossible_capacity = []\nmax_catamarans = v / 2\nmax_kayaks = [v, kayaks.length].min\n(0..max_catamarans).each do |num_of_c|\n\tnext if (num_of_k = v - 2 * num_of_c) > max_kayaks\n\tcapacity = kayaks[0...num_of_k].inject(0) { |sum, value| sum + value } + \n\t\t\t\t\t\t catamarans[0...num_of_c].inject(0) { |sum, value| sum + value }\n\tcount = num_of_k + num_of_c\n\tpossible_capacity << [capacity, count]\nend\n\nputs possible_capacity"}, {"source_code": "n, v = gets.split.map(&:to_i)\n\nkayaks = []\ncatamarans = []\nn.times do |index|\n\ttype, p = gets.split.map(&:to_i)\n\tif type == 1\n\t\tkayaks << [index + 1, p]\n\telse\n\t\tcatamarans << [index + 1, p]\n\tend\nend\n\nkayaks.sort! { |e1, e2| e2[1] <=> e1[1] }\ncatamarans.sort! { |e1, e2| e2[1] <=> e1[1] }\n\npossible_capacity = {}\nmax_c = [v / 2, catamarans.length].min\nmax_k = [v - 2 * max_c, kayaks.length].min\nindexes = []\ntotal = (0...max_c).inject(0) { |sum, value| sum + catamarans[value][1] } +\n\t\t\t\t(0...max_k).inject(0) { |sum, value| sum + kayaks[value][1] }\nputs total\nwhile max_c > 0 && max_k <= kayaks.length - 2 && catamarans[max_c - 1][1] < kayaks[max_k][1] + kayaks[max_k + 1][1]\n\ttotal -= catamarans[max_c - 1][1]\n\ttotal += kayaks[max_k][1] + kayaks[max_k + 1][1]\n\tmax_c -= 1\n\tmax_k += 2\nend\n\nindexes = []\nmax_c.times do |index|\n\tindexes << catamarans[index][0]\nend\nmax_k.times do |index|\n\tindexes << kayaks[index][0]\nend\n\nputs total\nputs indexes.join(' ')"}, {"source_code": "n, v = gets.split.map(&:to_i)\n\nkayaks = []\ncatamarans = []\nn.times do |index|\n\ttype, p = gets.split.map(&:to_i)\n\tif type == 1\n\t\tkayaks << [index + 1, p]\n\telse\n\t\tcatamarans << [index + 1, p]\n\tend\nend\n\nkayaks.sort! { |e1, e2| e2[1] <=> e1[1] }\ncatamarans.sort! { |e1, e2| e2[1] <=> e1[1] }\n\npossible_capacity = {}\nmax_c = [v / 2, catamarans.length].min\nmax_k = [v - 2 * max_c, kayaks.length].min\nindexes = []\ntotal = (0...max_c).inject(0) { |sum, value| sum + catamarans[value][1] } +\n\t\t\t\t(0...max_k).inject(0) { |sum, value| sum + kayaks[value][1] }\n\nwhile max_c > 0 && max_k <= kayaks.length - 2 && catamarans[max_c - 1][0] < kayaks[max_k][0] + kayaks[max_k + 1][0]\n\ttotal -= catamarans[max_c - 1][0]\n\ttotal += kayaks[max_k][0] + kayaks[max_k + 1][0]\n\tmax_c -= 1\n\tmax_k += 2\nend\n\nindexes = []\nmax_c.times do |index|\n\tindexes << catamarans[index][0]\nend\nmax_k.times do |index|\n\tindexes << kayaks[index][0]\nend\n\nputs total\nputs indexes.join(' ')"}, {"source_code": "n, v = gets.split.map(&:to_i)\n\nkayaks = []\ncatamarans = []\nn.times do |index|\n\ttype, p = gets.split.map(&:to_i)\n\tif type == 1\n\t\tkayaks << [index + 1, p]\n\telse\n\t\tcatamarans << [index + 1, p]\n\tend\nend\n\nkayaks.sort! { |e1, e2| e2[1] <=> e1[1] }\ncatamarans.sort! { |e1, e2| e2[1] <=> e1[1] }\n\npossible_capacity = {}\nmax_c = [v / 2, catamarans.length].min\nmax_k = [v - 2 * max_c, kayaks.length].min\nindexes = []\ntotal = (0...max_c).inject(0) { |sum, value| sum + catamarans[value][1] } +\n\t\t\t\t(0...max_k).inject(0) { |sum, value| sum + kayaks[value][1] }\n\nwhile max_c > 0 && max_k <= kayaks.length - 2 && catamarans[max_c - 1][1] < kayaks[max_k][1] + kayaks[max_k + 1][1]\n\ttotal -= catamarans[max_c - 1][0]\n\ttotal += kayaks[max_k][0] + kayaks[max_k + 1][0]\n\tmax_c -= 1\n\tmax_k += 2\nend\n\nindexes = []\nmax_c.times do |index|\n\tindexes << catamarans[index][0]\nend\nmax_k.times do |index|\n\tindexes << kayaks[index][0]\nend\n\nputs total\nputs indexes.join(' ')"}, {"source_code": "n, v = gets.split.map(&:to_i)\n\nkayaks = []\ncatamarans = []\nn.times do |index|\n\ttype, p = gets.split.map(&:to_i)\n\tif type == 1\n\t\tkayaks << [index + 1, p]\n\telse\n\t\tcatamarans << [index + 1, p]\n\tend\nend\n\nkayaks.sort! { |e1, e2| e2[1] <=> e1[1] }\ncatamarans.sort! { |e1, e2| e2[1] <=> e1[1] }\n\npossible_capacity = {}\nmax_c = [v / 2, catamarans.length].min\nmax_k = [v - 2 * max_c, kayaks.length].min\nindexes = []\ntotal = (0...max_c).inject(0) { |sum, value| sum + catamarans[value][1] } +\n\t\t\t\t(0...max_k).inject(0) { |sum, value| sum + kayaks[value][1] }\n\nwhile max_c > 0 && max_k <= kayaks.length - 2 && catamarans[max_c - 1][0] < kayaks[max_k][0] + kayaks[max_k + 1][0]\n\ttotal -= catamarans[max_c - 1][0]\n\ttotal += kayaks[max_k][0] + kayaks[max_k + 1][0]\n\tmax_c -= 1\n\tmax_k += 2\nend\n\nindexes = []\nmax_k.times do |index|\n\tindexes << kayaks[index][0]\nend\nmax_c.times do |index|\n\tindexes << catamarans[index][0]\nend\n\nputs total\nputs indexes.join(' ')"}, {"source_code": "n, v = gets.split.map(&:to_i)\n\nkayaks = []\ncatamarans = []\n\nn.times do\n\tvehicle_info = gets.split.map(&:to_i)\n\tif vehicle_info[0] == 1\n\t\tkayaks << vehicle_info[1]\n\telse\n\t\tcatamarans << vehicle_info[1]\n\tend\nend\n\nkayaks.sort! { |e1, e2| e2 <=> e1 }\ncatamarans.sort! { |e1, e2| e2 <=> e1 }\n\npossible_capacity = {}\nmax_catamarans = v / 2\n(0..max_catamarans).each do |num_of_c|\n\tnext if (num_of_k = v - 2 * num_of_c) > kayaks.length\n\tcapacity = kayaks[0...num_of_k].inject(0) { |sum, value| sum + value } + \n\t\t\t\t\t\t catamarans[0...num_of_c].inject(0) { |sum, value| sum + value }\n\tpossible_capacity[capacity] = [num_of_k, num_of_c]\nend\n\nmax_capacity = possible_capacity.max\nputs max_capacity[0]\nputs ([1] * max_capacity[1][0] + [2] * max_capacity[1][1]).join(' ')"}], "src_uid": "a1f98b06650a5755e784cd6ec6f3b211"} {"nl": {"description": "Polycarp remembered the $$$2020$$$-th year, and he is happy with the arrival of the new $$$2021$$$-th year. To remember such a wonderful moment, Polycarp wants to represent the number $$$n$$$ as the sum of a certain number of $$$2020$$$ and a certain number of $$$2021$$$.For example, if: $$$n=4041$$$, then the number $$$n$$$ can be represented as the sum $$$2020 + 2021$$$; $$$n=4042$$$, then the number $$$n$$$ can be represented as the sum $$$2021 + 2021$$$; $$$n=8081$$$, then the number $$$n$$$ can be represented as the sum $$$2020 + 2020 + 2020 + 2021$$$; $$$n=8079$$$, then the number $$$n$$$ cannot be represented as the sum of the numbers $$$2020$$$ and $$$2021$$$. Help Polycarp to find out whether the number $$$n$$$ can be represented as the sum of a certain number of numbers $$$2020$$$ and a certain number of numbers $$$2021$$$.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\leq t \\leq 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case contains one integer $$$n$$$ ($$$1 \\leq n \\leq 10^6$$$) — the number that Polycarp wants to represent as the sum of the numbers $$$2020$$$ and $$$2021$$$.", "output_spec": "For each test case, output on a separate line: \"YES\" if the number $$$n$$$ is representable as the sum of a certain number of $$$2020$$$ and a certain number of $$$2021$$$; \"NO\" otherwise. You can output \"YES\" and \"NO\" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive).", "sample_inputs": ["5\n1\n4041\n4042\n8081\n8079"], "sample_outputs": ["NO\nYES\nYES\nYES\nNO"], "notes": null}, "positive_code": [{"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n puts n % 2020 <= n / 2020 ? \"YES\" : \"NO\"\r\nend"}, {"source_code": "require 'set'\r\ns = Set.new\r\n\r\nflag = 0\r\n\r\nval1 = 1000000/2020;\r\nval2 = 1000000/2021;\r\n\r\nfor i in (0..val1)\r\n for j in (0..val2)\r\n temp = i*2020+j*2021;\r\n s << temp;\r\n end\r\nend\r\n\r\nn = gets.chomp.to_i;\r\nwhile n != 0\r\n n = n-1;\r\n value = gets.chomp.to_i;\r\n if s.include?(value)\r\n puts \"YES\"\r\n else\r\n puts \"NO\"\r\n end\r\nend"}, {"source_code": "MAX_N = 10**6 + 5\nflag = [false] * MAX_N\nflag[0] = true\n(0...MAX_N).each do |i|\n\tif flag[i]\n\t\tflag[i + 2020] = true if i + 2020 < MAX_N\n\t\tflag[i + 2021] = true if i + 2021 < MAX_N\n\tend\nend\n\ngets.to_i.times do\n\tn = gets.to_i\n\tputs flag[n] ? 'YES' : 'NO'\nend\n"}, {"source_code": "t = gets.to_i\r\nt.times do\r\n n = gets.to_i\r\n puts (n % 2020 <= n / 2020) ? \"YES\" : \"NO\"\r\nend"}, {"source_code": "tcases = gets.chomp.to_i\r\ndef solve()\r\n n = gets.chomp.to_i \r\n q = n / 2020\r\n r = n % 2020\r\n puts (r <= q) ? \"YES\" : \"NO\"\r\nend\r\ntcases.times do solve() end\r\n"}, {"source_code": "t = gets.chomp.to_i\r\n# arr = []\r\nt.times do\r\n n = gets.chomp.to_i\r\n ok = false\r\n 0.upto(500) do |i|\r\n ans = n - i * 2021\r\n # p ans\r\n if ans >= 0\r\n amari = ans % 2020\r\n if amari == 0\r\n puts \"YES\"\r\n # arr << i\r\n ok = true\r\n break\r\n end\r\n else\r\n break\r\n end\r\n end\r\n if ok == false\r\n puts \"NO\"\r\n end\r\nend"}, {"source_code": "t=gets.to_i\r\nt.times{\r\n\tn = gets.to_i\r\n\tif n<2020 \r\n\t\tputs \"NO\"\r\n\telse\r\n\t\tputs n%2020 <= n/2020? \"YES\" : \"NO\"\r\n\tend\r\n}\r\n"}, {"source_code": "l=[0]*1000001\r\ni=0\r\nwhile i*2020<=1000000\r\n j=0\r\n while i*2020+j*2021<=1000000\r\n l[2020*i+2021*j]+=1 if l[2020*i+2021*j]==0\r\n j+=1\r\n end\r\n i+=1\r\nend\r\ngets.to_i.times do\r\n n=gets.to_i\r\n if l[n]==1\r\n puts \"YES\"\r\n else\r\n puts \"NO\"\r\n end\r\nend\r\n\r\n"}], "negative_code": [{"source_code": "gets.to_i.times do\r\n n = gets.to_i\r\n puts n % 2020 >= n / 2020 ? \"YES\" : \"NO\"\r\nend"}], "src_uid": "3ec1b7027f487181dbdfb12f295f11ae"} {"nl": {"description": "As their story unravels, a timeless tale is told once again...Shirahime, a friend of Mocha's, is keen on playing the music game Arcaea and sharing Mocha interesting puzzles to solve. This day, Shirahime comes up with a new simple puzzle and wants Mocha to solve them. However, these puzzles are too easy for Mocha to solve, so she wants you to solve them and tell her the answers. The puzzles are described as follow.There are $$$n$$$ squares arranged in a row, and each of them can be painted either red or blue.Among these squares, some of them have been painted already, and the others are blank. You can decide which color to paint on each blank square.Some pairs of adjacent squares may have the same color, which is imperfect. We define the imperfectness as the number of pairs of adjacent squares that share the same color.For example, the imperfectness of \"BRRRBBR\" is $$$3$$$, with \"BB\" occurred once and \"RR\" occurred twice.Your goal is to minimize the imperfectness and print out the colors of the squares after painting. ", "input_spec": "Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 100$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains an integer $$$n$$$ ($$$1\\leq n\\leq 100$$$) — the length of the squares row. The second line of each test case contains a string $$$s$$$ with length $$$n$$$, containing characters 'B', 'R' and '?'. Here 'B' stands for a blue square, 'R' for a red square, and '?' for a blank square.", "output_spec": "For each test case, print a line with a string only containing 'B' and 'R', the colors of the squares after painting, which imperfectness is minimized. If there are multiple solutions, print any of them.", "sample_inputs": ["5\n7\n?R???BR\n7\n???R???\n1\n?\n1\nB\n10\n?R??RB??B?"], "sample_outputs": ["BRRBRBR\nBRBRBRB\nB\nB\nBRRBRBBRBR"], "notes": "NoteIn the first test case, if the squares are painted \"BRRBRBR\", the imperfectness is $$$1$$$ (since squares $$$2$$$ and $$$3$$$ have the same color), which is the minimum possible imperfectness."}, "positive_code": [{"source_code": "gets.to_i.times do\r\n n=gets.to_i\r\n s=gets.chomp.chars\r\n q=\"\"\r\n t=0\r\n s.each.with_index do |ss,i|\r\n if q==\"\"\r\n if ss==\"?\"\r\n t+=1\r\n elsif ss==\"B\"\r\n s[0,i]=[\"R\"]*(i%2)+[\"B\",\"R\"]*(i/2)\r\n q=\"B\"\r\n elsif ss==\"R\"\r\n s[0,i]=[\"B\"]*(i%2)+[\"R\",\"B\"]*(i/2)\r\n q=\"R\"\r\n end\r\n elsif q==\"B\"\r\n if ss==\"?\"\r\n s[i]=\"R\"\r\n q=\"R\"\r\n elsif ss==\"R\"\r\n q=\"R\"\r\n end\r\n else\r\n if ss==\"?\"\r\n s[i]=\"B\"\r\n q=\"B\"\r\n elsif ss==\"B\"\r\n q=\"B\"\r\n end\r\n end\r\n end\r\n if q==\"\"\r\n s=[\"R\"]*(n%2)+[\"B\",\"R\"]*(n/2)\r\n end\r\n puts s.join\r\nend\r\n"}, {"source_code": "END { load __FILE__ unless $stdin.eof? }\n\nt = gets.to_i\nt.times do\n\nn = gets.to_i\ns = gets.chomp.chars.map! { |c| c == \"B\" ? 1 : c == \"R\" ? 2 : 0 }\n\ni = s.index(&:nonzero?) || n\nc = s[i] || 1\ni.times.reverse_each do |k|\n s[k] = (c ^= 3)\nend\n\nwhile i < n\n i += 1\n j = (i...n).find { |k| s[k] != 0 } || n\n\n c = s[i-1]\n (i...j).each { |k| s[k] = (c ^= 3) }\n\n i = j\nend\n\nputs (s * \"\").tr(\"12\", \"BR\")\n\nend\n"}], "negative_code": [], "src_uid": "280487a73e6070a7dc7deb44332d6319"} {"nl": {"description": "Consider all binary strings of length $$$m$$$ ($$$1 \\le m \\le 60$$$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $$$2^m$$$ such strings in total.The string $$$s$$$ is lexicographically smaller than the string $$$t$$$ (both have the same length $$$m$$$) if in the first position $$$i$$$ from the left in which they differ, we have $$$s[i] < t[i]$$$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second.We remove from this set $$$n$$$ ($$$1 \\le n \\le \\min(2^m-1, 100)$$$) distinct binary strings $$$a_1, a_2, \\ldots, a_n$$$, each of length $$$m$$$. Thus, the set will have $$$k=2^m-n$$$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary).We number all the strings after sorting from $$$0$$$ to $$$k-1$$$. Print the string whose index is $$$\\lfloor \\frac{k-1}{2} \\rfloor$$$ (such an element is called median), where $$$\\lfloor x \\rfloor$$$ is the rounding of the number down to the nearest integer.For example, if $$$n=3$$$, $$$m=3$$$ and $$$a=[$$$010, 111, 001$$$]$$$, then after removing the strings $$$a_i$$$ and sorting, the result will take the form: $$$[$$$000, 011, 100, 101, 110$$$]$$$. Thus, the desired median is 100.", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. Then, $$$t$$$ test cases follow. The first line of each test case contains integers $$$n$$$ ($$$1 \\le n \\le \\min(2^m-1, 100)$$$) and $$$m$$$ ($$$1 \\le m \\le 60$$$), where $$$n$$$ is the number of strings to remove, and $$$m$$$ is the length of binary strings. The next $$$n$$$ lines contain $$$a_1, a_2, \\ldots, a_n$$$ — distinct binary strings of length $$$m$$$. The total length of all given binary strings in all test cases in one test does not exceed $$$10^5$$$.", "output_spec": "Print $$$t$$$ answers to the test cases. For each test case, print a string of length $$$m$$$ — the median of the sorted sequence of remaining strings in the corresponding test case.", "sample_inputs": ["5\n3 3\n010\n001\n111\n4 3\n000\n111\n100\n011\n1 1\n1\n1 1\n0\n3 2\n00\n01\n10"], "sample_outputs": ["100\n010\n0\n1\n11"], "notes": "NoteThe first test case is explained in the statement.In the second test case, the result after removing strings and sorting is $$$[$$$001, 010, 101, 110$$$]$$$. Therefore, the desired median is 010."}, "positive_code": [{"source_code": "gets.to_i.times do\n\tn, m = gets.split.map(&:to_i)\n\ta = (1..n).map { gets.chomp.to_i(2) }\n\tputs (0...2**m).bsearch { |i|\n\t\t\ti + 1 - a.count { _1 <= i } >= (2**m - n + 1) / 2\n\t }.to_s(2).rjust(m, '0')\nend\n"}], "negative_code": [], "src_uid": "b313fde45b6567bad265b8288c213cf0"} {"nl": {"description": "Sometimes it is not easy to come to an agreement in a bargain. Right now Sasha and Vova can't come to an agreement: Sasha names a price as high as possible, then Vova wants to remove as many digits from the price as possible. In more details, Sasha names some integer price $$$n$$$, Vova removes a non-empty substring of (consecutive) digits from the price, the remaining digits close the gap, and the resulting integer is the price.For example, is Sasha names $$$1213121$$$, Vova can remove the substring $$$1312$$$, and the result is $$$121$$$.It is allowed for result to contain leading zeros. If Vova removes all digits, the price is considered to be $$$0$$$.Sasha wants to come up with some constraints so that Vova can't just remove all digits, but he needs some arguments supporting the constraints. To start with, he wants to compute the sum of all possible resulting prices after Vova's move.Help Sasha to compute this sum. Since the answer can be very large, print it modulo $$$10^9 + 7$$$.", "input_spec": "The first and only line contains a single integer $$$n$$$ ($$$1 \\le n < 10^{10^5}$$$).", "output_spec": "In the only line print the required sum modulo $$$10^9 + 7$$$.", "sample_inputs": ["107", "100500100500"], "sample_outputs": ["42", "428101984"], "notes": "NoteConsider the first example.Vova can choose to remove $$$1$$$, $$$0$$$, $$$7$$$, $$$10$$$, $$$07$$$, or $$$107$$$. The results are $$$07$$$, $$$17$$$, $$$10$$$, $$$7$$$, $$$1$$$, $$$0$$$. Their sum is $$$42$$$."}, "positive_code": [{"source_code": "class Integer\n def mod_pow(n, mod)\n x = self\n res = 1\n\n while n > 0\n res = res * x % mod if n[0] == 1\n x = x * x % mod\n n >>= 1\n end\n\n res\n end\n\n def mod_inverse(mod)\n mod_pow(mod - 2, mod)\n end\nend\n\nN = gets.chomp\nn = N.size\nMOD = 10 ** 9 + 7\ninv = 10.mod_inverse(MOD)\nbase = 10.pow(n - 1, MOD)\nans = 0\nsum = 0\n\nN.chars.map(&:to_i).each.with_index do |x, k|\n # add left\n s = k * (k + 1) / 2\n ans += s * (base * x)\n ans %= MOD\n\n # add right\n ans += sum * (n - k)\n ans %= MOD\n\n sum *= inv\n sum %= MOD\n\n base *= inv\n base %= MOD\n sum += x * base\n sum %= MOD\nend\n\nputs ans\n"}], "negative_code": [], "src_uid": "0ffa96d83e63d20bdfa2ecbf535adcdd"} {"nl": {"description": "Orac is studying number theory, and he is interested in the properties of divisors.For two positive integers $$$a$$$ and $$$b$$$, $$$a$$$ is a divisor of $$$b$$$ if and only if there exists an integer $$$c$$$, such that $$$a\\cdot c=b$$$.For $$$n \\ge 2$$$, we will denote as $$$f(n)$$$ the smallest positive divisor of $$$n$$$, except $$$1$$$.For example, $$$f(7)=7,f(10)=2,f(35)=5$$$.For the fixed integer $$$n$$$, Orac decided to add $$$f(n)$$$ to $$$n$$$. For example, if he had an integer $$$n=5$$$, the new value of $$$n$$$ will be equal to $$$10$$$. And if he had an integer $$$n=6$$$, $$$n$$$ will be changed to $$$8$$$.Orac loved it so much, so he decided to repeat this operation several times.Now, for two positive integers $$$n$$$ and $$$k$$$, Orac asked you to add $$$f(n)$$$ to $$$n$$$ exactly $$$k$$$ times (note that $$$n$$$ will change after each operation, so $$$f(n)$$$ may change too) and tell him the final value of $$$n$$$.For example, if Orac gives you $$$n=5$$$ and $$$k=2$$$, at first you should add $$$f(5)=5$$$ to $$$n=5$$$, so your new value of $$$n$$$ will be equal to $$$n=10$$$, after that, you should add $$$f(10)=2$$$ to $$$10$$$, so your new (and the final!) value of $$$n$$$ will be equal to $$$12$$$.Orac may ask you these queries many times.", "input_spec": "The first line of the input is a single integer $$$t\\ (1\\le t\\le 100)$$$: the number of times that Orac will ask you. Each of the next $$$t$$$ lines contains two positive integers $$$n,k\\ (2\\le n\\le 10^6, 1\\le k\\le 10^9)$$$, corresponding to a query by Orac. It is guaranteed that the total sum of $$$n$$$ is at most $$$10^6$$$. ", "output_spec": "Print $$$t$$$ lines, the $$$i$$$-th of them should contain the final value of $$$n$$$ in the $$$i$$$-th query by Orac.", "sample_inputs": ["3\n5 1\n8 2\n3 4"], "sample_outputs": ["10\n12\n12"], "notes": "NoteIn the first query, $$$n=5$$$ and $$$k=1$$$. The divisors of $$$5$$$ are $$$1$$$ and $$$5$$$, the smallest one except $$$1$$$ is $$$5$$$. Therefore, the only operation adds $$$f(5)=5$$$ to $$$5$$$, and the result is $$$10$$$.In the second query, $$$n=8$$$ and $$$k=2$$$. The divisors of $$$8$$$ are $$$1,2,4,8$$$, where the smallest one except $$$1$$$ is $$$2$$$, then after one operation $$$8$$$ turns into $$$8+(f(8)=2)=10$$$. The divisors of $$$10$$$ are $$$1,2,5,10$$$, where the smallest one except $$$1$$$ is $$$2$$$, therefore the answer is $$$10+(f(10)=2)=12$$$.In the third query, $$$n$$$ is changed as follows: $$$3 \\to 6 \\to 8 \\to 10 \\to 12$$$."}, "positive_code": [{"source_code": "def minimum(x)\n for i in 2..x do\n return i if x % i == 0\n end\nend\n\nt = gets.chomp.to_i\n\nt.times do\n n, k = gets.chomp.split(' ').collect(&:to_i)\n puts minimum(n) + n + (2*(k-1))\nend\n"}, {"source_code": "\ndef ans(n, k)\n # 偶数 为2, 加2后还是偶数, 所以 n + 2k\n # 奇数 第一个为奇书, 相加后也为偶数 n + first.x + 2 (k-1)\n if n & 0x1 == 0\n return n + 2 * k\n end\n\n d = 3\n \n while d <= Math.sqrt(n)\n n % d == 0 and return n + d + 2 *(k-1)\n d += 2\n end\n\n return n + n + 2 * (k-1)\nend\n\nline = gets().to_i\n\nline.times {\n (n,k) = gets.scan(/\\d+/).map(&:to_i)\n puts ans(n,k)\n}"}, {"source_code": "\ndef ans(n, k)\n # 偶数 为2, 加2后还是偶数, 所以 n + 2k\n # 奇数 第一个为奇书, 相加后也为偶数 n + first.x + 2 (k-1)\n if n & 0x1 == 0\n return n + 2 * k\n end\n\n d = 3\n while d < n\n n % d == 0 and break\n d += 2\n end\n\n return n + d + 2*(k-1)\nend\n\nline = gets().to_i\n\nline.times {\n (n,k) = gets.scan(/\\d+/).map(&:to_i)\n puts ans(n,k)\n}"}, {"source_code": "t = gets.to_i\n\n1.upto t do\n c = p = 0\n n, k = gets.split.map(&:to_i)\n if n % 2 == 0\n n += k*2\n else\n 2.upto Math.sqrt(n+1).to_i do |i|\n if n % i == 0\n n += i\n p = 1\n break\n end\n end\n if p == 0\n n += n\n end\n n += (k-1)*2\n end\n puts n\nend"}, {"source_code": "t = gets.to_i\n\n1.upto t do\n c = 0\n n, k = gets.split.map(&:to_i)\n if n % 2 == 0\n n += k*2\n else\n 3.upto n do |i|\n if n % i == 0\n n += i\n break\n end\n end\n n += (k-1)*2\n end\n puts n\nend"}, {"source_code": "require 'prime'\ngets.to_i.times do\n n, k=gets.split.map &:to_i\n f=n.prime_division[0][0]\n puts n+f+2*(k-1)\nend\n"}, {"source_code": "#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']\nrequire 'prime'\n\ndef inpf() a=gets.chomp.split(\" \").map(&:to_f)end\n\ndef inps() a=gets.chomp.split(\" \")end\n\ndef copy(a) Marshal.load(Marshal.dump(a)) end\n\ndef kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end\n\ndef na(n,d=0) Array.new(n,d)end\n\ndef na2(n,m,d=0) Array.new(n){Array.new(m,d)}end\n\ndef na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end\n\ndef inp() a=gets.chomp.split(\" \").map(&:to_i)end\n\ndef r_up(a, b) (a+b-1)/b end\n\ndef sum(a) a.inject(:+) end\n\ndef big(a,b) return (a>b)? a:b end\n\ndef small(a,b) return (aj\n\t\t\tputs 'somethong wrong with index in func _query'\n\t\t\texit 1\n\t\tend\n\n\t\tif i==b and j==e\n\t\t\treturn @tree[ind]\n\t\telse\n\t\t\tmid = (b+e)/2\n\t\t\tif i>mid\n\t\t\t\treturn _query(i, j, 2*ind+1, mid+1, e)\n\t\t\telsif j<= mid\n\t\t\t\treturn _query(i,j,2*ind,b,mid)\n\t\t\telse\n\t\t\t\ta = _query(i, mid,2*ind, b, mid)\n\t\t\t\tb = _query(mid+1,j,2*ind+1, mid+1,e)\n\t\t\t\treturn min(a,b)\n\t\t\tend\n\t\tend\n\tend\n\n\tdef set(ind, value)\n\t\t@data[ind] = value\n\t\tupdate(ind, value)\n\tend\n\n\tdef update(ind, value); _update( ind, value,1, 0, @data.size-1); end\n\n\tdef _update(ind, value,i,b,e)\n\n\t\tif b==e\n\t\t\t@tree[i] = value\n\t\t\treturn\n\t\tend\t\t\n\t\tmid = (b+e)/2\n\t\tif ind > mid\n\t\t\ta=_update(ind, value,2*i+1, mid+1, e)\t\t\n\n\t\telse\n\t\t\t_update(ind, value, 2*i, b, mid)\n\t\tend\n\t\t@tree[i] = min(@tree[2*i], @tree[2*i+1])\n\tend\n\nend\n\nclass Bitree\n\n\tdef initialize(array)\n\t\t@data=array.clone\n\t\t@tree= [0]*(array.size+1)\n\t\tconstruct()\n\tend\n\n\tattr_reader :data, :tree\n\tdef construct()\n\t\tsum = [0]*(@data.size+1)\n\t\t(1..sum.size-1).each do |ind|\n\t\t\tsum[ind] = @data[ind-1]+sum[ind-1]\n\t\tend\n\t\t@tree[0] = 0\n\t\t(1..@tree.size-1).each do |ind|\n\t\t\ti = ind-(ind & -ind)\n\t\t\t@tree[ind] = sum[ind]-sum[i]\n\t\tend\n\tend\n\n\tdef query(i)\n\t\ti+=1\n\t\ts=0\n\t\twhile i>=1\n\t\t\ts+=@tree[i]\n\t\t\ti-= (i&-i)\n\t\tend\n\t\ts\n\tend\n\n\tdef add(i,a)\n\t\t@data[i]+=a\n\t\ti+=1\n\t\twhile i<@tree.size\n\t\t\t@tree[i]+=a\n\t\t\ti+=(i&-i)\n\t\tend\n\tend\n\n\tdef [](i)\n\t\t@data[i]\n\tend\nend\n=end\n=begin\nclass Graph\n\n\tdef initialize(g)\n\n\t\tputs 'expected a Array type input' if g.class != Array\n\t\t@graph = g\n\t\tsize = g.size+1\n\t\t@visited = [0]*size\n\t\t@time = [0] *size\n\t\t@father = [-1] *size\n\t\t@lev = [-1] * size\n\n\tend\n\n\tdef dfs(); _dfs(-1, [0],0, 1); self;end\n\n\tdef _dfs(father, time, lev, id)\n\n\t\t@time[id]=time[0]\n\t\t@father[id]=father\n\t\t@visited[id] = 1\n\t\t@lev[id] = lev\n\n\t\t@graph[id].each do |i|\n\t\t\tif @visited[i]!=1\n\t\t\t\ttime[0]+=1\n\t\t\t\t_dfs(id, time, lev+1 ,i)\n\t\t\tend\n\t\tend\n\tend\n\n\tdef inspect\n\t\tputs \"time: #{@time.inspect}\"\n\t\tputs \"father: #{@father.inspect}\"\n\t\tputs \"lev: #{@lev.inspect}\"\n\tend\nend\n\nn = gets.to_i\ng = [0]*(n+1)\n\n(1..n).each do |i|\n\ta = gets.split.map(&:to_i)\n\tg[a[0]] = a[1..(a.size-1)]\nend\n\ng = Graph.new(g)\n\ng.dfs.inspect\n\n=end\n\n=begin\n\nn = gets.to_i\nm = gets\nstanding = 0\n\nm.each_char do |c|\n\tif c == 'X'\n\t\tstanding += 1\n\tend\nend\n\nif standing == m.size/2\n\tputs '0'\n\tputs m\nelse\n\n\tif standing > m.size/2\n\t\td = standing-m.size/2\n\t\t\n\t\t(0..m.size-1).each do |i|\n\t\t\tif m[i] == 'X'\n\t\t\t\tm[i] = 'x'\n\t\t\t\td-=1\n\t\t\t\tbreak if d==0\n\t\t\tend\n\t\tend\n\t\tputs '1'\n\t\tputs m\n\telse\n\t\td = m.size/2-standing\n\t\t(0..m.size-1).each do |i|\n\t\t\tif m[i] == 'x'\n\t\t\t\tm[i] = 'X'\n\t\t\t\td-=1\n\t\t\t\tbreak if d == 0\n\t\t\tend\n\t\tend\n\t\tputs '1'\n\t\tputs m\n\tend\nend\n\n=end\n\nncity, p = gets.split.map(&:to_i)\n\narray = []\n(1..ncity).each do |i|\n\ta = gets.split.map(&:to_i)\n\n\tarray.push(a)\nend\n\narray.sort! do |a,b|\n\n\t_a = a[0]*a[0]+a[1]*a[1]\n\t_b = b[0]*b[0]+b[1]*b[1]\n\n\tif _a >_b\n\t\t1\n\telse\n\t\t-1\n\tend\nend\n\narray.each do |a|\n\n\tp+=a[2]\n\tif p>=1000000\n\t\t_a = a[0]*a[0]+a[1]*a[1]\n\t\tputs \"%.7f\" % [Math.sqrt(_a)]\n\t\texit 0\n\tend\nend\n\nputs -1\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"}, {"source_code": "n,s = gets.split.map(&:to_i)\ncities = (1..n).map { |_|\n gets.split.map(&:to_i)\n}\n\nok = false\nneed = 1000000 - s\nmin = 0\nmax = 1000000000\nwhile max - min > 1\n med = (max + min) / 2\n pop = cities.map { |c|\n if c[0] ** 2 + c[1] ** 2 <= med\n c[2]\n else\n 0\n end\n }.inject(&:+)\n if need <= pop\n ok = true\n max = med\n else\n min = med\n end\nend\n\nif ok\n puts max ** 0.5\nelse\n puts -1\nend\n\n\n\n\n"}], "negative_code": [], "src_uid": "bcc758394d012519f0865479b3c6770c"} {"nl": {"description": "Polycarp got an array of integers $$$a[1 \\dots n]$$$ as a gift. Now he wants to perform a certain number of operations (possibly zero) so that all elements of the array become the same (that is, to become $$$a_1=a_2=\\dots=a_n$$$). In one operation, he can take some indices in the array and increase the elements of the array at those indices by $$$1$$$.For example, let $$$a=[4,2,1,6,2]$$$. He can perform the following operation: select indices 1, 2, and 4 and increase elements of the array in those indices by $$$1$$$. As a result, in one operation, he can get a new state of the array $$$a=[5,3,1,7,2]$$$.What is the minimum number of operations it can take so that all elements of the array become equal to each other (that is, to become $$$a_1=a_2=\\dots=a_n$$$)?", "input_spec": "The first line of the input contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$)  — the number of test cases in the test. The following are descriptions of the input test cases. The first line of the description of each test case contains one integer $$$n$$$ ($$$1 \\le n \\le 50$$$)  — the array $$$a$$$. The second line of the description of each test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$)  — elements of the array $$$a$$$.", "output_spec": "For each test case, print one integer  — the minimum number of operations to make all elements of the array $$$a$$$ equal.", "sample_inputs": ["3\n6\n3 4 2 4 1 2\n3\n1000 1002 998\n2\n12 11"], "sample_outputs": ["3\n4\n1"], "notes": "NoteFirst test case: $$$a=[3,4,2,4,1,2]$$$ take $$$a_3, a_5$$$ and perform an operation plus one on them, as a result we get $$$a=[3,4,3,4,2,2]$$$. $$$a=[3,4,3,4,2,2]$$$ we take $$$a_1, a_5, a_6$$$ and perform an operation on them plus one, as a result we get $$$a=[4,4,3,4,3,3]$$$. $$$a=[4,4,3,4,3,3]$$$ we take $$$a_3, a_5, a_6$$$ and perform an operation on them plus one, as a result we get $$$a=[4,4,4,4,4,4]$$$. There are other sequences of $$$3$$$ operations, after the application of which all elements become equal.Second test case: $$$a=[1000,1002,998]$$$ 2 times we take $$$a_1, a_3$$$ and perform an operation plus one on them, as a result we get $$$a=[1002,1002,1000]$$$. $$$a=[1002,1002,1000]$$$ also take $$$a_3$$$ 2 times and perform an operation plus one on it, as a result we get $$$a=[1002,1002,1002]$$$. Third test case: $$$a=[12,11]$$$ take $$$a_2$$$ and perform an operation plus one on it, as a result we get $$$a=[12,12]$$$. "}, "positive_code": [{"source_code": "inputs = STDIN.read.strip.split(\"\\n\")\r\ninputs.shift\r\n\r\ninputs.each_slice(2) do |inp|\r\n nums = inp[1].split(' ').map(&:to_i)\r\n\r\n max = nums.max\r\n c = 0\r\n nums.each_with_index do |num, i|\r\n c = [c, max - num].max\r\n end\r\n\r\n puts c\r\nend"}, {"source_code": "n = gets.to_i\r\nmax = []\r\nmin = []\r\nn.times do |i|\r\n a = gets.to_i\r\n a1 = gets.split.map(&:to_i)\r\n max << a1.max\r\n min << a1.min\r\nend\r\n\r\nn.times do |i|\r\n puts max[i] - min[i]\r\nend"}, {"source_code": "t = gets.to_i\ninputs = []\n(0...t).each do\n n = gets.to_i\n arr = gets.chomp\n arr = arr.split(\" \").map(&:to_i)\n inputs.push([arr, n])\nend\n\ninputs.each do |input|\n max = input[0].max\n min = input[0].min\n puts max-min\nend"}, {"source_code": "gets.to_i.times do\n gets\n xs = gets.split.map(&:to_i)\n p xs.max - xs.min\nend\n"}, {"source_code": "t = gets.to_i\nwhile t > 0\n n = gets.to_i\n arr = gets.chomp.split.map(&:to_i).sort\n puts arr[-1] - arr[0]\n t-=1\nend\n"}, {"source_code": "n = gets.chomp.to_i\r\nn.times do |i|\r\nl = gets.chomp.to_i\r\nst = gets.chomp.split\r\nmx = 0\r\nmn = 1000000001\r\nl.times do |element|\r\n el = st[element].to_i\r\n\tif el mx\r\n\t mx = el\r\n\tend\r\nend\r\nans = mx - mn\r\nputs ans\r\nend"}, {"source_code": "t = readline.strip.to_i\n\nt.times do\n readline\n numbers = readline.strip.split(/\\s+/).map(&:to_i)\n\n max = numbers.max\n min = numbers.min\n\n puts max - min\nend\n"}], "negative_code": [{"source_code": "n = gets.to_i\r\na = gets.to_i\r\na1 = gets.split.map(&:to_i)\r\nb = gets.to_i\r\nb1 = gets.split.map(&:to_i)\r\nc = gets.to_i\r\nc1 = gets.split.map(&:to_i)\r\n\r\nputs a1.max - a1.min\r\nputs b1.max - b1.min\r\nputs c1.max - c1.min"}], "src_uid": "cf3cfcae029a6997ee62701eda959a60"} {"nl": {"description": "DZY loves chemistry, and he enjoys mixing chemicals.DZY has n chemicals, and m pairs of them will react. He wants to pour these chemicals into a test tube, and he needs to pour them in one by one, in any order. Let's consider the danger of a test tube. Danger of an empty test tube is 1. And every time when DZY pours a chemical, if there are already one or more chemicals in the test tube that can react with it, the danger of the test tube will be multiplied by 2. Otherwise the danger remains as it is.Find the maximum possible danger after pouring all the chemicals one by one in optimal order.", "input_spec": "The first line contains two space-separated integers n and m . Each of the next m lines contains two space-separated integers xi and yi (1 ≤ xi < yi ≤ n). These integers mean that the chemical xi will react with the chemical yi. Each pair of chemicals will appear at most once in the input. Consider all the chemicals numbered from 1 to n in some order.", "output_spec": "Print a single integer — the maximum possible danger.", "sample_inputs": ["1 0", "2 1\n1 2", "3 2\n1 2\n2 3"], "sample_outputs": ["1", "2", "4"], "notes": "NoteIn the first sample, there's only one way to pour, and the danger won't increase.In the second sample, no matter we pour the 1st chemical first, or pour the 2nd chemical first, the answer is always 2.In the third sample, there are four ways to achieve the maximum possible danger: 2-1-3, 2-3-1, 1-2-3 and 3-2-1 (that is the numbers of the chemicals in order of pouring)."}, "positive_code": [{"source_code": "n, m = gets.split(' ').map(&:to_i)\nar=(0...n).to_a\ndef rt(rr,v)\n if ( rr[v]==v )\n elsif ( rr[v]!=rt(rr,rr[v]) )\n rr[v] = rt(rr,rr[v])\n end\n rr[v]\nend\n(1..m).each do |_|\n x, y = gets.split(' ').map(&:to_i)\n rx = rt(ar,ar[x-1])\n ry = rt(ar,ar[y-1])\n if rx < ry\n ar[ry]=rx\n elsif rx > ry\n ar[rx]=ry\n end\nend\n(0...n).each do |x|\n rt(ar,x)\nend\np 2**(n-(ar.uniq.count))"}, {"source_code": "n,m=gets.split.map(&:to_i)\nc=(0..n).to_a\nm.times do\n a,b=gets.split.map(&:to_i)\n if c[a]!=c[b]\n x=c[b]\n (n+1).times do |i|\n c[i] = c[a] if c[i] == x\n end\n end\nend\np 1< 0\nend\n\np sum\n"}, {"source_code": "n,m=gets.split.map(&:to_i)\nc=(0..n).to_a\nm.times do\n a,b=gets.split.map(&:to_i)\n if c[a]!=c[b]\n x=c[b]\n (n+1).times do |i|\n c[i] = c[a] if c[i] == x\n end\n end\nend\np 1< 0\n gr[q[0]].each do |a|\n if visited[a] == -1\n visited[a] = 1\n q.push(a) \n end\n end\n q.shift\n end\n\n\n end\nend\n\n\nputs 2**visited[1..-1].inject{|x, y| x+y}\n\n\n\n\n"}, {"source_code": "n,m=gets.split.map(&:to_i)\nc=(0..n).to_a\nm.times do\n a,b=gets.split.map(&:to_i)\n if c[a]!=c[b]\n x=c[b]\n (n+1).times do |i|\n c[i] = c[a] if c[i] == x\n end\n end\nend\np 1< n + k ? 0 : m > k ? 1 - 0.upto(k).map{|i| (m - i) / (n + i + 1.0)}.inject(&:*) : 1\n"}, {"source_code": "n, m, k = gets.split.map(&:to_i)\np m > n + k ? 0 : m > k ? 1 - 0.upto(k).map{|i| (m - i) / (n + i + 1.0)}.inject(&:*) : 1\n\n"}, {"source_code": "n, m, k = gets.split.map(&:to_i)\np m > n + k ? 0 : m > k ? 1 - 0.upto(k).map{|i| (m - i) / (n + i + 1.0)}.inject(&:*) : 1\n\n"}, {"source_code": "n, m, k = gets.split.map(&:to_i)\np m > n + k ? 0 : m > k ? 1 - 0.upto(k).map{|i| (m - i) / (n + i + 1.0)}.inject(&:*) : 1\n\n"}, {"source_code": "n, m, k = gets.split.map(&:to_i)\np m > n + k ? 0 : m > k ? 1 - 0.upto(k).map{|i| (m - i) / (n + i + 1.0)}.inject(&:*) : 1\n\n"}, {"source_code": "n, m, k = gets.split.map(&:to_i)\np m > n + k ? 0 : m > k ? 1 - 0.upto(k).map{|i| (m - i) / (n + i + 1.0)}.inject(&:*) : 1"}, {"source_code": "n, m, k = gets.split.map(&:to_i)\np m > n + k ? 0 : m > k ? 1 - 0.upto(k).map{|i| (m - i) / (n + i + 1.0)}.inject(&:*) : 1\n\n"}, {"source_code": "n, m, k = gets.split.map(&:to_i)\np m > n + k ? 0 : m > k ? 1 - 0.upto(k).map{|i| (m - i) / (n + i + 1.0)}.inject(&:*) : 1"}, {"source_code": "n, m, k = gets.split.map(&:to_i)\np m > n + k ? 0 : m > k ? 1 - 0.upto(k).map{|i| (m - i) / (n + i + 1.0)}.inject(&:*) : 1\n\n"}, {"source_code": "n, m, k = gets.split.map(&:to_i)\np m > n + k ? 0 : m > k ? 1 - 0.upto(k).map{|i| (m - i) / (n + i + 1.0)}.inject(&:*) : 1\n\n"}, {"source_code": "n, m, k = gets.split.map(&:to_i)\np m > n + k ? 0 : m > k ? 1 - 0.upto(k).map{|i| (m - i) / (n + i + 1.0)}.inject(&:*) : 1\n\n"}], "negative_code": [], "src_uid": "5a2c9caec9c20a3caed1982ee8ed8f9c"} {"nl": {"description": "A Ministry for Defense sent a general to inspect the Super Secret Military Squad under the command of the Colonel SuperDuper. Having learned the news, the colonel ordered to all n squad soldiers to line up on the parade ground.By the military charter the soldiers should stand in the order of non-increasing of their height. But as there's virtually no time to do that, the soldiers lined up in the arbitrary order. However, the general is rather short-sighted and he thinks that the soldiers lined up correctly if the first soldier in the line has the maximum height and the last soldier has the minimum height. Please note that the way other solders are positioned does not matter, including the case when there are several soldiers whose height is maximum or minimum. Only the heights of the first and the last soldier are important.For example, the general considers the sequence of heights (4, 3, 4, 2, 1, 1) correct and the sequence (4, 3, 1, 2, 2) wrong.Within one second the colonel can swap any two neighboring soldiers. Help him count the minimum time needed to form a line-up which the general will consider correct.", "input_spec": "The first input line contains the only integer n (2 ≤ n ≤ 100) which represents the number of soldiers in the line. The second line contains integers a1, a2, ..., an (1 ≤ ai ≤ 100) the values of the soldiers' heights in the order of soldiers' heights' increasing in the order from the beginning of the line to its end. The numbers are space-separated. Numbers a1, a2, ..., an are not necessarily different.", "output_spec": "Print the only integer — the minimum number of seconds the colonel will need to form a line-up the general will like.", "sample_inputs": ["4\n33 44 11 22", "7\n10 10 58 31 63 40 76"], "sample_outputs": ["2", "10"], "notes": "NoteIn the first sample the colonel will need to swap the first and second soldier and then the third and fourth soldier. That will take 2 seconds. The resulting position of the soldiers is (44, 33, 22, 11).In the second sample the colonel may swap the soldiers in the following sequence: (10, 10, 58, 31, 63, 40, 76) (10, 58, 10, 31, 63, 40, 76) (10, 58, 10, 31, 63, 76, 40) (10, 58, 10, 31, 76, 63, 40) (10, 58, 31, 10, 76, 63, 40) (10, 58, 31, 76, 10, 63, 40) (10, 58, 31, 76, 63, 10, 40) (10, 58, 76, 31, 63, 10, 40) (10, 76, 58, 31, 63, 10, 40) (76, 10, 58, 31, 63, 10, 40) (76, 10, 58, 31, 63, 40, 10) "}, "positive_code": [{"source_code": "n = gets.to_i\nar = gets.split(' ').map(&:to_i)\nmin, max = ar.minmax\nmaxid, minid = ar.index(max), ar.rindex(min)\nr = maxid + n-1 - minid\nr -= 1 if maxid > minid\nputs r\n\n"}, {"source_code": "\nn=gets.chomp.to_i\ni=0\n#for i in (0...n)\nb=gets.chomp.split(\" \").map &:to_i\n#end\nt1= b.index(b.max)\nt2=n-1-b.rindex(b.min)\n#puts b.max\n#puts t1\n#puts b.min\n#puts t2\nif n>t1+t2\nputs t1+t2\nelse \nputs t1+t2-1\nend\n\n\n#end\n"}, {"source_code": "\nn = gets.chomp.to_i\nna = gets.chomp.split(/ /).map!{|x| x.to_i}\n\nnsa = na.sort\nminidx = na.rindex(nsa.first)\nmaxidx = na.index(nsa.last)\nmv = na.length-(minidx+1) + maxidx\nmv -= 1 if minidx < maxidx\nputs mv\n\n"}, {"source_code": "gets\ns=gets.split.map &:to_i\nn=s.size\nz=n-1+s.index(s.max)-s.rindex(s.min)\np n>z ?z:z-1"}, {"source_code": "#!/usr/bin/env ruby\n# -*- coding: utf-8 -*-\n# Aurhor: seniorihor (12.03.2012)\n\nclass General\n def initialize(quantity, numbers)\n @quantity = quantity\n @numbers = numbers\n @seconds = 0\n end\n def calculation_seconds\n if @numbers.rindex(@numbers.min) > @numbers.index(@numbers.max)\n @seconds = @numbers.index(@numbers.max) + @numbers.length - 1 - @numbers.rindex(@numbers.min)\n else\n @seconds = @numbers.index(@numbers.max) + @numbers.length - 2 - @numbers.rindex(@numbers.min)\n end\n end\nend\nquantity = gets.chomp.to_i\nnumbers = gets.chomp.split(\" \").map! do |i|\n i = i.to_i\nend\n\ng = General.new(quantity, numbers)\np g.calculation_seconds\n"}, {"source_code": "n=gets.to_i;a=gets.split.map(&:to_i);\nmin=0;max=0;\n0.upto(n-1){|i| min=i if a.min==a[i] }\n(n-1).downto(0){|i| max=i if a.max==a[i] }\np max+(n-1-min)-(( min=0)\n if(soldatu[posMensh]>soldatu[i])\n posMensh=i\n end \n i-=1\nend\n\nsec=0\nsec+=posBilsh\nsec+=(kilkist-1-posMensh)\nsec-=1 if(posBilsh>posMensh)\n \nprint sec "}, {"source_code": "#!/usr/bin/env ruby\n# -*- coding: utf-8 -*-\n# Aurhor: seniorihor and vertoldo (12.03.2012)\n\nclass General\n def initialize(quantity, numbers)\n @quantity = quantity\n @numbers = numbers\n @posMensh=0\n @posBilsh=0\n @sec=0\n end\n \n def calculation_seconds\n\n @numbers.size().times do |j|\n if(@numbers[@posBilsh]<@numbers[j])\n @posBilsh=j\n end \n end\n \n @posMensh=@quantity-1\n i=@posMensh\n \n while(i>=0)\n if(@numbers[@posMensh]>@numbers[i])\n @posMensh=i\n end \n i-=1\n end\n\n @sec+=@posBilsh\n @sec+=(@quantity-1-@posMensh)\n @sec-=1 if(@posBilsh>@posMensh)\n\n return @sec\n end\nend\n\n\nquantity = gets.chomp.to_i\nnumbers = gets.chomp.split(\" \").map! {|el| el=el.to_i}\ng = General.new(quantity, numbers)\nprint g.calculation_seconds"}, {"source_code": "gets\ns=gets.split.map &:to_i\nn=s.size\nz=n-1+s.index(s.max)-s.rindex(s.min)\np n>z ?z:z-1\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\n\nmin, max = 0, 0\na.each_index do |i|\n min = i if a[min] >= a[i]\n max = i if a[max] < a[i]\nend\ncnt = max + (a.size - 1 - min)\ncnt -= 1 if max > min\nputs cnt"}, {"source_code": "gets\ns=gets.split.map &:to_i\nn=s.size\nz=n-1+s.index(s.max)-s.rindex(s.min)\np n>z ?z:z-1\n"}, {"source_code": "gets\ns=gets.split.map &:to_i\nn=s.size\nz=n-1+s.index(s.max)-s.rindex(s.min)\np n>z ?z:z-1"}, {"source_code": "def order(n,a)\n minmax = a.minmax\n min = minmax.shift\n max = minmax.shift\n\n #As the smaller soldier has to be at the end of the line we need to look for the closest one to that pos\n min_pos = a.rindex(min)\n # The highest soldier has to be first that's why we start looking from the beginig of the line\n max_pos = a.index(max)\n\n tot_sec = 0\n #Checking if the soldiers have to be swapped between them\n if min_pos < max_pos\n # Has to be -2 because of the array index and one less to the end of line,\n # that's because the soldiers are swapped between them and we consider that time on the max_pos\n tot_sec = max_pos + ((n-2) - min_pos)\n else\n tot_sec = max_pos + ((n-1) - min_pos)\n end\n\n tot_sec\nend\n\nn = gets.to_i\na = gets.split(/\\s/).map(&:to_i)\nputs order(n,a)"}, {"source_code": "n = $stdin.gets.to_i\nmilita = $stdin.gets.split.map { |x| x.to_i }\n\n\nmax = 0\nmaxidx = -1\n\nn.times do |i|\n if max < milita[i]\n max = milita[i]\n maxidx = i\n end\nend\n\nresult = maxidx\n\nmaxidx.downto(1) do |i|\n t = milita[i]\n milita[i] = milita[i-1]\n milita[i-1] = t\nend\n\nmin = 10000000\nminidx = -1\n\nn.times do |k|\n i = n - k - 1\n\n if min > milita[i]\n min = milita[i]\n minidx = i\n end\nend\n\nresult += n - 1 - minidx\n\np result\n"}, {"source_code": "gets\ns=gets.split.map &:to_i\nn=s.size\nz=n-1+s.index(s.max)-s.rindex(s.min)\np n>z ?z:z-1"}, {"source_code": "gets\ns=gets.split.map &:to_i\nn=s.size\nz=n-1+s.index(s.max)-s.rindex(s.min)\np n>z ?z:z-1"}, {"source_code": "n=gets.chomp.to_i\na=gets.chomp.split.map(&:to_i)\nmn=a.min\nmx=a.max\nmn_pos = a.rindex(mn)\nans=0\nfor i in mn_pos...a.size-1\n\ta[i],a[i+1]=a[i+1],a[i]\n\tans+=1\nend\nmx_pos=a.index(mx)\nfor i in 1..mx_pos\n\ta[i],a[i+1]=a[i+1],a[i]\n\tans+=1\nend\nputs ans"}, {"source_code": "gets\na = gets.strip.split.map(&:to_i).to_a\np a.size - a.rindex(a.min) - 1 + a.index(a.max) - (a.index(a.max) > a.rindex(a.min) ? 1 : 0)"}, {"source_code": "n = gets.to_i\n\na = gets.chomp.split(' ').map &:to_i\n\nl = a.index(a.max)\nr = a.rindex(a.min)\nif l > r\n puts a.size-2 +l - r\nelse\n puts a.size-1 + l -r\nend"}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\nmax_index=a.index(a.max)\nmin_index=a.rindex(a.min)\np max_index0\n a[i-1], a[i] = a[i], a[i-1]\n i-=1\n k+=1\nend\n\nmini = a.count - a.reverse.find_index(min) - 1\ni = mini\nwhile i min_ind\n puts max_ind + arr.size - min_ind - 2 \nelse\n puts max_ind + arr.size - min_ind - 1\nend\n"}, {"source_code": "s=gets()\nn=s.to_i\ns=gets()\ns=s.chop()\nat=0\n\n\na=Array.new(n)\nfor i in (0..n-1) do\n\tcur=0\n\twhile (ata[max])\nmax=i\nend\nif(a[n-1-i]z ?z:z-1\n"}, {"source_code": "n = gets.chomp.to_i\narr = Array.new(n)\narr = gets.split.map &:to_i\nbig = [0,0]\nsmall = [1000,0]\nn.times do |i|\n if arr[i] > big[0]\n big[0] = arr[i]\n big[1] = i\n end\n\n if arr[i] <= small[0]\n small[0] = arr[i]\n small[1] = i\n end\n\nend\n\nflag = 0\nif big[1] > small[1]\n flag = 1\nend\n\nputs (big[1] - 0) + ((n-1) - small[1]) - flag\n"}, {"source_code": "n = gets.to_i\nas = gets.split.map(&:to_i)\n\nmin_index = as.rindex(as.min)\nmax_index = as.index(as.max)\n\nputs (n - min_index - 1) + max_index - (min_index < max_index ? 1 : 0) "}, {"source_code": "gets\ns=gets.split.map &:to_i\nn=s.size\nz=n-1+s.index(s.max)-s.rindex(s.min)\np n>z ?z:z-1\n"}, {"source_code": "gets.chomp.to_i\nline = gets.chomp.split(/ /).map(&:to_i)\nlen = line.length - 1\nmin_height = line.min\nmax_height = line.max\nmin_pos = line.rindex min_height\nmax_pos = line.index max_height\ndelta = 0\ndelta = 1 if min_pos < max_pos\nputs (max_pos - 0) + (len - min_pos) - delta\n"}, {"source_code": "gets\ns=gets.split.map &:to_i\nn=s.size\nz=n-1+s.index(s.max)-s.rindex(s.min)\np n>z ?z:z-1"}, {"source_code": "n=gets.to_i\nx=gets.split.map{|e| e.to_i}\nmin1=x[0]\nmax1=x[0]\nminP=0\nmaxP=0\nn.times{|i|\n\tif x[i]<=min1 then\n\t\tmin1=x[i]\n\t\tminP=i\n\tend\n}\nn.times{|i|\n\tif max1 imn\n p imx + (n-imn-1) - 1\nelse\n p imx + (n-imn-1)\nend\n"}, {"source_code": "n = gets.chomp.to_i\narr = gets.chomp.split.map(&:to_i)\n\nmax_ind = arr.find_index(arr.max)\nmin_ind = arr.rindex(arr.min)\n# p max_ind, min_ind\nif max_ind > min_ind\n puts (max_ind) + ((n-1) - min_ind) - 1 \nelse\n puts (max_ind) + ((n-1) - min_ind)\nend"}, {"source_code": "gets\ns=gets.split.map &:to_i\nn=s.size\nz=n-1+s.index(s.max)-s.rindex(s.min)\np n>z ?z:z-1"}, {"source_code": "#=================================\n# Better be sure than sorry. \n#=================================\n$stdin=File.open(\"./input.txt\",\"r\") if ENV[\"LOGNAME\"]==\"skydos\"\nrequire 'prime'\n#require 'mathn'\nrequire 'matrix'\nrequire 'set'\ninclude Math\n#=================================\n\nn=gets.to_i\ndata=gets.split.map(&:to_i)\n\nmini=data.min\nmaxy=data.max\n\nmini_pos = data.rindex(mini)\nans=0\nfor i in mini_pos...data.size-1\n\ttmp=data[i]\n\tdata[i]=data[i+1]\n\tdata[i+1]=tmp\n\tans+=1\nend\n\nmaxy_pos=data.index(maxy)\nfor i in 1..maxy_pos\n\ttmp=data[i]\n\tdata[i]=data[i+1]\n\tdata[i+1]=tmp\n\tans+=1\nend\n\nputs ans\n"}, {"source_code": "#=================================\n# Better be sure than sorry. \n#=================================\n$stdin=File.open(\"./input.txt\",\"r\") if ENV[\"LOGNAME\"]==\"skydos\"\nrequire 'prime'\n#require 'mathn'\nrequire 'matrix'\nrequire 'set'\ninclude Math\n#=================================\n\nn=gets.to_i\ndata=gets.split.map(&:to_i)\n\nmini=data.min\nmaxy=data.max\n\nmini_pos = data.rindex(mini)\nans=0\nfor i in mini_pos...data.size-1\n\ttmp=data[i]\n\tdata[i]=data[i+1]\n\tdata[i+1]=tmp\n\tans+=1\nend\n\nmaxy_pos=data.index(maxy)\nfor i in 1..maxy_pos\n\ttmp=data[i]\n\tdata[i]=data[i+1]\n\tdata[i+1]=tmp\n\tans+=1\nend\n\nputs ans"}, {"source_code": "numSol = gets.to_i\na = gets.split.map {|x| x.to_i}\n\nleftMax = 0\nrightMin = numSol-1\n\nfor x in 0...numSol\n\ty = numSol-1-x\n\tleftMax = x if a[x]>a[leftMax]\n\trightMin = y if a[y]rightMin\n\nputs ans"}, {"source_code": "n=gets.to_i\na=gets.split\na.collect! {|i| i.to_i}\n\nfor i in 1..n\n\tif a[-i] == a.max then l=n-i end\n\tif a[i-1] == a.min then r=n-i end\nend\n\nputs (if l+r>=n then l+r-1 else l+r end)\n"}, {"source_code": "n=gets.to_i\na=gets.split.collect! {|i| i.to_i}\nfor i in 1..n\n\tif a[-i] == a.max then l=n-i end\n\tif a[i-1] == a.min then r=n-i end\nend\nputs (if l+r>=n then l+r-1 else l+r end)\n"}, {"source_code": "i = gets.to_i\n\narray = gets.chomp.split.map(&:to_i)\n\nmin_index = array.rindex(array.min)\nmax_index = array.index(array.max)\n\nlength = array.length - 1\n\nif max_index < min_index\n swaps = (length-min_index) + max_index\nelse\n swaps = (length-min_index) + max_index - 1\nend\n\nputs swaps"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\n\nans = 0\nmaxv = a.max\nminv = a.min\n\ni = a.index(maxv)\nans += i\na.delete_at(i)\na.unshift(maxv)\n\ni = a.rindex(minv)\nans += a.size - i - 1\n\nputs ans"}, {"source_code": "gets\ns = gets.split(' ').map(&:to_i)\nsorted = s.sort\nmin = sorted.first\nmax = sorted.last\nmax_index = s.find_index(max)\nmin_index = s.reverse.find_index(min)\nresult = s.find_index(max) + s.reverse.find_index(min)\nresult -= 1 if max_index > s.size - min_index - 1\nputs result\n"}, {"source_code": "n = Integer(gets.chomp)\nh = gets.chomp.split\nheight = Array.new\nfor i in 0...n\n height[i] = Integer(h[i])\nend\n\nmax = 0\nmin = 101\nfor i in 0...n\n if height[i] > max\n max = height[i]\n end\nend\nfor i in 0...n\n if height[i] < min\n min = height[i]\n end\nend\n\nmax_index = Array.new\nmin_index = Array.new\nfor i in 0...n\n if height[i] == max\n max_index.push(i)\n elsif height[i] == min\n min_index.push(i)\n end\nend\n\nx = min_index.length\nif max == min \n puts 0\nelsif max_index[0] > min_index[x-1]\n puts max_index[0] + n - 2 - min_index[x-1]\nelse\n puts max_index[0] + n - 1 - min_index[x-1]\nend"}, {"source_code": "gets\ns=gets.split.map &:to_i\nn=s.size\nz=n-1+s.index(s.max)-s.rindex(s.min)\np n>z ?z:z-1\n"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split.map(&:to_i)\n\nmin, max = a.minmax\n\nwmax = a.index(max)\nwmin = a.rindex(min)\n\nans = wmax + a.size - wmin - 1\nans -= 1 if wmax > wmin\n\nputs ans"}, {"source_code": "n = gets.to_i\narr = gets.split(\" \").map(&:to_i)\narr.reverse!\n\nmin,min_index = arr.each_with_index.min\n\narr.unshift(arr.delete_at(min_index))\nsecs = min_index\n\nmax,max_index = arr.each_with_index.max\nsecs += (n - max_index - 1)\nputs secs"}, {"source_code": "number_of_soldiers = gets.to_i\nsoldiers = gets.split.map &:to_i\n\nshortest = number_of_soldiers - 1 - soldiers.reverse.index(soldiers.min)\ntallest = soldiers.index(soldiers.max)\nputs tallest + number_of_soldiers - shortest - (tallest > shortest ? 1 : 0) - 1\n"}, {"source_code": "ar = []\nn = gets.to_i\nar = gets.split.map &:to_i\n#puts ar.index(ar.max)\nx = ar.index(ar.max)\ny = ar.rindex(ar.min)\na = x\nb = n-y-1\nputs y < x ? a + b-1 : a + b\n"}, {"source_code": "gets\ns=gets.split.map &:to_i\nn=s.size\nz=n-1+s.index(s.max)-s.rindex(s.min)\np n>z ?z:z-1"}, {"source_code": "gets\nl = gets.split.map(&:to_i)\nm = l.index(l.max)\nl.delete_at(m)\nputs m + l.reverse.index(l.min)"}, {"source_code": "n=gets.to_i; d=gets.split.map(&:to_i); sum=d.index(d.max); d[d.index(d.max)]+=1;\np d.index(d.max) + (d.select{|x| x!=d.max}.reverse.push(d.max).index(d.min))"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nmii = a.rindex(a.min)\nmxi = a.index(a.max)\nans = mxi + (n - 1 - mii)\nans -= 1 if mxi > mii\nputs ans\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nmi = a.min\nmx = a.max\nmii = a.rindex(mi)\nmxi = a.index(mx)\nans = mxi + (n - 1 - mii)\nans -= 1 if mxi > mii\nputs ans\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nmi = a.min\nmx = a.max\nmii = 0\nmxi = 0\nfor i in 0...n\n if a[i] == mi\n mii = i\n mi = a[i]\n end\nend\nfor i in 0...n\n if a[i] == mx\n mxi = i\n mx = a[i]\n break;\n end\nend\nans = mxi + (n - 1 - mii)\nans -= 1 if mxi > mii\nputs ans\n"}, {"source_code": "def swap (i, d)\n tmp = $a[i]\n $a[i] = $a[i + d]\n $a[i + d] = tmp\n i + d\nend\nn = gets.to_i\n$a = gets.split.map(&:to_i)\nmi = $a.min\nmx = $a.max\nans = 0\nmii = 0\nmxi = 0\nn.times do |i|\n if $a[i] == mx\n mxi = i\n break\n end\nend\nwhile mxi != 0\n mxi = swap(mxi, -1)\n ans += 1\nend\nn.times do |i|\n mii = i if $a[i] == mi\nend\nwhile mii != n - 1\n mii = swap(mii, 1)\n ans += 1\nend\nputs ans\n"}, {"source_code": "n = gets.chomp.to_i\n\narray = gets.chomp.split(\" \")\n\nmaxi = 0\nmini = 105\n\nid = 0\n\nfor i in 0...n\n\ta = array[i].to_i\n\n\tif a<=mini\n\t\tmini = a\n\t\tid = i;\n\tend\n\n\ti += 1\nend\n\ncont = 0\n\nwhile id=0 do\n\ta = array[j].to_i\n\n\tif a>=maxi\n\t\tmaxi = a\n\t\tid = j;\n\tend\n\n\tj -= 1\nend\n\nwhile id>=1 do\n\n\ta = array[id]\n\tarray[id] = array[id-1]\n\tarray[id - 1] = a\n\n\tid -= 1\n\tcont += 1\n\nend\n\n\nputs cont\n"}, {"source_code": "gets\ns=gets.split.map &:to_i\nn=s.size\nz=n-1+s.index(s.max)-s.rindex(s.min)\np n>z ?z:z-1"}, {"source_code": "n=gets.to_i\n\na=[]\n(n-1).times {\na.push gets(' ').to_i\n}\na.push gets.to_i\n\nmi=a.min\nma=a.max\n\nfor i in 0..a.size-1\nif a[i]==ma\nma_i=i\nbreak\nend\nend\n\ni=a.size-1\nwhile i>=0\nif a[i]==mi\nmi_i=i\nbreak\nend\n\ni-=1\nend\n\nret=ma_i+n-mi_i-1\nif ma_i>mi_i\nret-=1\nend\n\np ret"}, {"source_code": "length = gets.chomp.to_i\nheights = gets.chomp.split()\nmin, max = heights.minmax_by {|h| h.to_i}\nminindex = heights.rindex(min)\nmaxindex = heights.index(max)\nif maxindex < minindex\n print maxindex + heights.length - minindex - 1\nelse\n print maxindex - minindex + minindex + heights.length - minindex - 1 - 1\nend\n"}, {"source_code": "gets\ns=gets.split.map &:to_i\nn=s.size\nz=n-1+s.index(s.max)-s.rindex(s.min)\np n>z ?z:z-1\n"}, {"source_code": "gets\na = gets.split(' ').map(&:to_i)\n\nimax = a.index(a.max)\nimin = a.size - a.rindex(a.min) -1\n\nputs imax + imin - (imax > a.size - imin - 1 ? 1 : 0)\n"}, {"source_code": "gets\ns=gets.split.map &:to_i\nn=s.size\nz=n-1+s.index(s.max)-s.rindex(s.min)\np n>z ?z:z-1"}, {"source_code": "max = [0, 0]\nmin = [101, 0]\nn = Integer(gets.chomp)\ngets.chomp.split.each_with_index { |s, i|\n h = s.to_i\n if h > max[0]\n max = [h, i]\n end\n if h <= min[0]\n min = [h, i]\n end\n}\nputs max[1] + (n - min[1] - 1) - (max[1] > min[1] ? 1 : 0)"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\na.delete_at(ans = a.find_index(a.max))\na.reverse!\nans += a.find_index(a.min)\np ans\n"}, {"source_code": "gets\ns=gets.split.map &:to_i\nn=s.size\nz=n-1+s.index(s.max)-s.rindex(s.min)\np n>z ?z:z-1\n"}, {"source_code": "STDIN.gets\ni = STDIN.gets.split(' ').map(&:to_i)\n\nmax, min = i.max, i.min\n\nmax_i = -1; min_i = 100\n\ni.length.times do |k|\n\tmax_i = k if max_i == -1 and i[k] == max\n\tmin_i = k if i[k] == min\nend\n\nc = max_i - 1 + i.length - min_i\nc -= 1 if max_i > min_i\n\nSTDOUT.puts c"}, {"source_code": "#!ruby\n\nn = gets.to_i\n\ndata = gets.split.map{|e|e.to_i}\n\nmax_num = data.max\nmin_num = data.min\nmax_index = data.index(max_num)\nmin_index = data.rindex(min_num)\n\nputs max_index + data.length - min_index -1+ (max_index>min_index ? -1 : 0 )\n"}, {"source_code": "gets\ns=gets.split.map &:to_i\nn=s.size\nz=n-1+s.index(s.max)-s.rindex(s.min)\np n>z ?z:z-1\n"}], "negative_code": [{"source_code": "\nn=gets.chomp.to_i\ni=0\n#for i in (0...n)\nb=gets.chomp.split(\" \").map &:to_i\n#end\nt1= b.index(b.max)\nt2=n-1-b.index(b.min)\nputs b.max\nputs t1\nputs b.min\nputs t2\nif n>t1+t2\nputs t1+t2\nelse \nputs t1+t2-1\nend\n\n\n#end\n"}, {"source_code": "\nn=gets.chomp.to_i\ni=0\n#for i in (0...n)\nb=gets.chomp.split(\" \").map &:to_i\n#end\nt1= b.index(b.max)\nt2=n-1-b.index(b.min)\n#puts b.max\n#puts t1\n#puts b.min\n#puts t2\nif n>t1+t2\nputs t1+t2\nelse \nputs t1+t2-1\nend\n\n\n#end\n"}, {"source_code": "\nn=gets.chomp.to_i\ni=0\n#for i in (0...n)\nb=gets.chomp.split(\" \")\n#end\nt1= b.index(b.max)\nt2=n-b.rindex(b.min)-1\nif n>t1+t2\nputs t1+t2\nelse \nputs t1+t2-1\nend\n\n\n\n"}, {"source_code": "\nkilkist = gets.chomp.to_i\n\nsoldatu=gets.chomp.split(\" \").map! {|el| el=el.to_i}\n\nposMensh=soldatu.rindex(soldatu.min)\n\nposBilsh=soldatu.rindex(soldatu.max)\n\nsec=0\n\nif(posMensh!=posBilsh)\n\n sec+=posBilsh\n \n sec+=(kilkist-1-posMensh)\n \n sec-=1 if(posBilsh>posMensh)\n\nend\n\nprint sec "}, {"source_code": "n = $stdin.readline.to_i\na = $stdin.readline.split.map(&:to_i)\n\nmax = a.max\nmin = a.min\n\nmaxi = a.find_index(max)\nmini = a.reverse.find_index(min)\n\nif maxi!=mini\n k = maxi+mini-1\nelse\n k = maxi+mini\nend\n\nputs k\n"}, {"source_code": "n = $stdin.readline.to_i\na = $stdin.readline.split.map(&:to_i)\n\nmax = a.max\nmin = a.min\n\nif a.count == 2\n if a.first == max && a.last == min\n puts 0\n else\n puts 1\n end\nelse\n \n maxi = a.find_index(max)\n mini = a.reverse.find_index(min)\n\n if maxi!=mini\n k = maxi+mini-1\n else\n k = maxi+mini\n end\n\n puts k\nend"}, {"source_code": "gets\nh = gets.split.map &:to_i\nnumb = h.index(h.max) + h.size - (h.reverse.index(h.min) - h.size).abs \nnumb -= 1 if h.index(h.max) > h.index(h.min)\nputs numb\n"}, {"source_code": "gets\nh = gets.split.map &:to_i\nnumb = h.index(h.max) + h.size - (h.reverse.index(h.min) - h.size).abs \nnumb -= 1 if h.index(h.max) > (h.reverse.index(h.min) - h.size).abs\nputs numb\n"}, {"source_code": "n = gets.chomp.to_i\narr = gets.chomp.split.map(&:to_i)\n\nmax_ind = arr.rindex(arr.max)\nmin_ind = arr.rindex(arr.min)\n# p max_ind, min_ind\nif max_ind > min_ind\n puts (max_ind) + ((n-1) - min_ind) - 1 \nelse\n puts (max_ind) + ((n-1) - min_ind)\nend"}, {"source_code": "ar = []\nn = gets.to_i\nar = gets.split.map &:to_i\n#puts ar.index(ar.max)\nx = ar.index(ar.max)\ny = n-ar.rindex(ar.min)-1\nputs y < x ? x+y-1 : x+y\n"}, {"source_code": "gets\nl = gets.split.map(&:to_i)\nm = l.index(l.max)\nl.delete(l.max)\nputs m + l.reverse.index(l.min)"}, {"source_code": "gets\nl = gets.split.map(&:to_i)\nm = l.index(l.max)\nl.delete(m)\nputs m + l.reverse.index(l.min)"}, {"source_code": "gets\nl = gets.split.map(&:to_i)\nm = l.each_with_index.max\nl.delete(m[0])\nputs m[1] + l.reverse.each_with_index.min[1]"}, {"source_code": "gets\nl = gets.split.map(&:to_i)\nm = l.index(l.max)\nl.delete(m)\nputs m + l.reverse.each_with_index.min[1]"}, {"source_code": "n=gets.to_i; d=gets.split.map(&:to_i); sum=d.index(d.max);\np d.index(d.max) + (d.reverse.select{|x| x!=d.max}.push(d.max).index(d.min))"}, {"source_code": "n=gets.to_i\n\na=[]\n(n-1).times {\na.push gets(' ').to_i\n}\na.push gets.to_i\n\nmi=a.min\nma=a.max\n\nfor i in 0..a.size-1\nif a[i]==ma\nma_i=i\nbreak\nend\nend\n\ni=a.size-1\nwhile i>=0\nif a[i]==mi\nmi_i=i\nbreak\nend\n\ni-=1\nend\n\nret=ma_i+n-mi_i\nif ma_i>mi_i\nret-=1\nend\n\np ret"}, {"source_code": "gets\na = gets.split(' ').map(&:to_i)\n\nimax = a.index(a.max)\nimin = a.rindex(a.min)\n\nputs imax + (a.size - imin - 1) - (imax > a.index(a.min) ? 1 : 0)\n"}, {"source_code": "gets\na = gets.split(' ').map(&:to_i)\n\nimax = a.index(a.max)\nimin = a.size - a.rindex(a.min) -1\n\nputs imax + imin - (imax > a.size - imin ? 1 : 0)\n"}, {"source_code": "gets\na = gets.split(' ').map(&:to_i)\n\nimax = a.index(a.max)\nimin = a.size - a.rindex(a.min) -1\n\np imax, imin\n\nputs imax + imin - (imax > a.size - imin ? 1 : 0)\n"}, {"source_code": "STDIN.gets\ni = STDIN.gets.split(' ').map(&:to_i)\n\nmax, min = i.max, i.min\n\nif max == i.first and min = i.last\n STDOUT.puts 0\n exit\nend\n\nmax_i = 0; min_i = 101\n\ni.length.times do |k|\n max_i = k if max_i == 0 and i[k] == max\n min_i = k if i[k] == min\nend\n\nc = max_i - 1 + i.length - min_i\nc -= 1 if max_i > min_i\n\nSTDOUT.puts c"}, {"source_code": "STDIN.gets\ni = STDIN.gets.split(' ').map(&:to_i)\n\nmax, min = i.max, i.min\nmax_i = 0; min_i = 101\n\ni.length.times do |k|\n\tmax_i = k if max_i == 0 and i[k] == max\n\tmin_i = k if i[k] == min\nend\n\nc = max_i - 1 + i.length - min_i\nc -= 1 if max_i > min_i\n\nSTDOUT.puts c"}, {"source_code": "STDIN.gets\ni = STDIN.gets.split(' ').map(&:to_i)\n\nmax, min = i.max, i.min\n\nif max == i.first and min == i.last\n\tSTDOUT.puts 0\n\texit\nend\n\nmax_i = 0; min_i = 101\n\ni.length.times do |k|\n\tmax_i = k if max_i == 0 and i[k] == max\n\tmin_i = k if i[k] == min\nend\n\nc = max_i - 1 + i.length - min_i\nc -= 1 if max_i > min_i\n\nSTDOUT.puts c"}, {"source_code": "#!ruby\n\nn = gets.to_i\n\ndata = gets.split.map{|e|e.to_i}\n\nmax_num = data.max\nmin_num = data.min\nmax_index = data.index(max_num)\nmin_index = data.length - data.rindex(min_num) -1\n\nputs max_index + min_index + (max_index>min_index ? -1 : 0 )\n"}], "src_uid": "ef9ff63d225811868e786e800ce49c92"} {"nl": {"description": "There are n student groups at the university. During the study day, each group can take no more than 7 classes. Seven time slots numbered from 1 to 7 are allocated for the classes.The schedule on Monday is known for each group, i. e. time slots when group will have classes are known.Your task is to determine the minimum number of rooms needed to hold classes for all groups on Monday. Note that one room can hold at most one group class in a single time slot.", "input_spec": "The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of groups. Each of the following n lines contains a sequence consisting of 7 zeroes and ones — the schedule of classes on Monday for a group. If the symbol in a position equals to 1 then the group has class in the corresponding time slot. In the other case, the group has no class in the corresponding time slot.", "output_spec": "Print minimum number of rooms needed to hold all groups classes on Monday.", "sample_inputs": ["2\n0101010\n1010101", "3\n0101011\n0011001\n0110111"], "sample_outputs": ["1", "3"], "notes": "NoteIn the first example one room is enough. It will be occupied in each of the seven time slot by the first group or by the second group.In the second example three rooms is enough, because in the seventh time slot all three groups have classes."}, "positive_code": [{"source_code": "gets;p$<.map{|e|e.chomp.chars}.transpose.map{|e|e.count('1')}.max"}], "negative_code": [], "src_uid": "d8743905d56c6c670b6eeeddc7af0e36"} {"nl": {"description": "You are given a huge decimal number consisting of $$$n$$$ digits. It is guaranteed that this number has no leading zeros. Each digit of this number is either 0 or 1.You may perform several (possibly zero) operations with this number. During each operation you are allowed to change any digit of your number; you may change 0 to 1 or 1 to 0. It is possible that after some operation you can obtain a number with leading zeroes, but it does not matter for this problem.You are also given two integers $$$0 \\le y < x < n$$$. Your task is to calculate the minimum number of operations you should perform to obtain the number that has remainder $$$10^y$$$ modulo $$$10^x$$$. In other words, the obtained number should have remainder $$$10^y$$$ when divided by $$$10^x$$$.", "input_spec": "The first line of the input contains three integers $$$n, x, y$$$ ($$$0 \\le y < x < n \\le 2 \\cdot 10^5$$$) — the length of the number and the integers $$$x$$$ and $$$y$$$, respectively. The second line of the input contains one decimal number consisting of $$$n$$$ digits, each digit of this number is either 0 or 1. It is guaranteed that the first digit of the number is 1.", "output_spec": "Print one integer — the minimum number of operations you should perform to obtain the number having remainder $$$10^y$$$ modulo $$$10^x$$$. In other words, the obtained number should have remainder $$$10^y$$$ when divided by $$$10^x$$$.", "sample_inputs": ["11 5 2\n11010100101", "11 5 1\n11010100101"], "sample_outputs": ["1", "3"], "notes": "NoteIn the first example the number will be $$$11010100100$$$ after performing one operation. It has remainder $$$100$$$ modulo $$$100000$$$.In the second example the number will be $$$11010100010$$$ after performing three operations. It has remainder $$$10$$$ modulo $$$100000$$$."}, "positive_code": [{"source_code": "n, x, y = gets.split.map(&:to_i)\ns = gets.chomp.reverse\ngoal = '0' * x\ngoal[y] = '1'\nputs s[0, x].chars.zip(goal.chars).count { _1 != _2 }\n"}, {"source_code": "#!usr/bin/env ruby\n\n# https://codeforces.com/problemset/problem/1165/A\n\ninput = gets.split(' ').map(&:to_i)\nnum = gets.chomp\n\nn = input[0]\nx = input[1]\ny = input[2]\n\ncounter = 0\n\n(0..x-1).each do |i|\n if i == y\n counter += 1 if num[n-i-1] == '0'\n elsif num[n-i-1] == '1'\n counter += 1\n end\nend\n\nputs counter\n"}, {"source_code": "if File.exists?(\"input\")\n $stdin = File.open(\"input\")\n $stdout = File.open(\"output\",\"w\")\nend\n\nnxy = gets.chomp.split(\" \").map(&:to_i)\nn = nxy[0]\nx = nxy[1]\ny = nxy[2]\n\ns = gets.chomp\n\nans=0\n(0..x-1).each do |i|\n#\tp i\n#\tp s[s.length-i-1]\n\tif(i==y)\n\t\tif(s[s.length-i-1]=='0')\n\t\t\tans+=1\n\t\tend\n\telse\n#\t\tp i\n\t\tif(s[s.length-i-1]=='1')\n\t\t\tans+=1\n\t\tend\n\tend\nend\n\nputs ans\n"}], "negative_code": [], "src_uid": "075988685fa3f9b20bd215037c504a4f"} {"nl": {"description": "One day Ms Swan bought an orange in a shop. The orange consisted of n·k segments, numbered with integers from 1 to n·k. There were k children waiting for Ms Swan at home. The children have recently learned about the orange and they decided to divide it between them. For that each child took a piece of paper and wrote the number of the segment that he would like to get: the i-th (1 ≤ i ≤ k) child wrote the number ai (1 ≤ ai ≤ n·k). All numbers ai accidentally turned out to be different.Now the children wonder, how to divide the orange so as to meet these conditions: each child gets exactly n orange segments; the i-th child gets the segment with number ai for sure; no segment goes to two children simultaneously. Help the children, divide the orange and fulfill the requirements, described above.", "input_spec": "The first line contains two integers n, k (1 ≤ n, k ≤ 30). The second line contains k space-separated integers a1, a2, ..., ak (1 ≤ ai ≤ n·k), where ai is the number of the orange segment that the i-th child would like to get. It is guaranteed that all numbers ai are distinct.", "output_spec": "Print exactly n·k distinct integers. The first n integers represent the indexes of the segments the first child will get, the second n integers represent the indexes of the segments the second child will get, and so on. Separate the printed numbers with whitespaces. You can print a child's segment indexes in any order. It is guaranteed that the answer always exists. If there are multiple correct answers, print any of them.", "sample_inputs": ["2 2\n4 1", "3 1\n2"], "sample_outputs": ["2 4 \n1 3", "3 2 1"], "notes": null}, "positive_code": [{"source_code": "nk = gets.split\nwishes = gets.split\nn = nk[0].to_i\nk = nk[1].to_i\na = Array.new(n*k){|i| i+= 1}\nres = []\ni = 0\nwhile i 0 do\n if ar[pointer].nil?\n print \"%d \" % pointer\n counter -= 1\n ar[pointer] = 1\n end\n pointer += 1\n end\n print \"\\n\"\nend\n"}, {"source_code": "n,k,*a=gets(p).split.map(&:to_i)\nb=[*1..n*k]-a\nputs a.map{|_|(b.shift(n-1)<<_)*\" \"}\n"}, {"source_code": "def read_next_line\n gets.chomp.split.map { |x| x.to_i }\nend\n\nn, k = read_next_line\na = read_next_line\ng = []\nfor i in 0...a.size\n g << [a[i]]\nend\nif n > 1\n i, j = 0, 1\n for x in 1..k * n\n unless a.include? x\n g[i] << x\n j += 1\n if j == n\n j = 1\n i += 1\n end\n end\n end\nend\ng.each { |z| puts z * ' ' }"}, {"source_code": "n,k,*a=gets(p).split.map(&:to_i)\nb=[*1..n*k]-a\nputs a.map{|_|(b.shift(n-1)<<_)*\" \"}\n"}, {"source_code": "n,k,*a=gets(p).split.map(&:to_i)\nb=[*1..n*k]-a\nputs a.map{|_|(b.shift(n-1)<<_)*\" \"}"}, {"source_code": "t = gets.split.collect{|x| x.to_i}\nn,k = t[0], t[1]\n\na = gets.split.collect{|x| x.to_i}\nanswer = []\n\na.each do |c|\n\tprint c,' '\n\tm = 1\n\tfor i in 1..n*k\n\t\tif m == n\n\t\t\tbreak\n\t\tend\n\t\tif not a.include?(i) and not answer.include?(i)\n\t\t\tanswer << i\n\t\t\tm+=1\n\t\t\tprint i,' '\n\t\tend\n\tend\n\tputs\nend"}, {"source_code": "n,k,*a=gets(p).split.map(&:to_i)\nb=[*1..n*k]-a\nputs a.map{|_|(b.shift(n-1)<<_)*\" \"}\n"}], "negative_code": [{"source_code": "n,k=gets.split.map(&:to_i)\na=gets.split.map(&:to_i)\nb=(1..(n*k)).to_a-a\nfor i in 0...k do\n\tprint a[i],\" \",b[0...n-1].join(\" \")\n\tprint \"\\n\"\nend\n\t\t\n\t\n"}], "src_uid": "928f18ee5dbf44364c0d578f4317944c"} {"nl": {"description": "Emuskald needs a fence around his farm, but he is too lazy to build it himself. So he purchased a fence-building robot.He wants the fence to be a regular polygon. The robot builds the fence along a single path, but it can only make fence corners at a single angle a.Will the robot be able to build the fence Emuskald wants? In other words, is there a regular polygon which angles are equal to a?", "input_spec": "The first line of input contains an integer t (0 < t < 180) — the number of tests. Each of the following t lines contains a single integer a (0 < a < 180) — the angle the robot can make corners at measured in degrees.", "output_spec": "For each test, output on a single line \"YES\" (without quotes), if the robot can build a fence Emuskald wants, and \"NO\" (without quotes), if it is impossible.", "sample_inputs": ["3\n30\n60\n90"], "sample_outputs": ["NO\nYES\nYES"], "notes": "NoteIn the first test case, it is impossible to build the fence, since there is no regular polygon with angle .In the second test case, the fence is a regular triangle, and in the last test case — a square."}, "positive_code": [{"source_code": "gets.to_i.times{\n\ta=gets.to_i\n\t#angles=[60, 90, 108, 120, 129, 135, 140, 144, 148, 150, 153, 155, 156, 158, 159, 160, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179]\n\tangles=[60, 90, 108, 120, 135, 140, 144, 150, 156, 160, 162, 165, 168, 170, 171, 172, 174, 175, 176, 177, 178, 179]\n\tangles.include?(a) ? (puts 'YES') : (puts 'NO')\n}\n"}, {"source_code": "gets.to_i.times do\n a=gets.to_i\n angles=[60, 90, 108, 120, 135, 140, 144, 150, 156, 160, 162, 165, 168, 170, 171, 172, 174, 175, 176, 177, 178, 179]\n angles.include?(a) ? (puts 'YES') : (puts 'NO')\nend"}, {"source_code": "gets.to_i.times{puts 360%(180-gets.to_i)==0 ?(\"YES\"):(\"NO\")}"}, {"source_code": "l = [0, 60, 90, 108, 120, 135, 140, 144, 150, 156, 160, 162, 165, 168, 170, 171, 172, 174, 175, 176, 177, 178, 179]\nn = gets.to_i\nn.times do\n\tif l.include?(gets.to_i)\n\t\tputs \"YES\"\n\telse\n\t\tputs \"NO\"\n\tend\nend"}, {"source_code": "n = gets.chomp.to_i\nres = []\n(1..n).each {|k|\n\ta = gets.chomp.to_i\n\ta /= 180.0\n\t\n\tq = 2.0/(1.0-a)\n\t\n\tif (q-q.round).abs < 0.000001\n\t\tres.push \"YES\"\n\telse\n\t\tres.push \"NO\"\n\tend\n}\n\nres.each { |k|\n puts k\n}"}, {"source_code": "x =(1..179).to_a.select{|x| (360%(180-x)==0)}\nn = gets.to_i\nd = n.times.map{gets.to_i}\nn.times{|y| (x.include?(d[y]))? (puts \"YES\") : (puts \"NO\")}\n"}, {"source_code": "gets.to_i.times{x=gets.to_i; puts ((360%(180-x)==0)? \"YES\" : \"NO\");}"}, {"source_code": "t = gets.to_i\nans = []\nfor i in 0...t\n ans[i] = 360 % (180 - gets.to_i) == 0 ? \"YES\" : \"NO\"\nend\nputs ans\n"}, {"source_code": "t = gets.to_i\n\n1.upto(t) { puts 360.modulo(180 - gets.to_i).zero? ? 'YES' : 'NO' }\n"}, {"source_code": "#!/usr/bin/env ruby\n\nn = gets.to_i\nn.times do\n\tx = gets.to_i\n\tputs 360 % (180 - x) == 0 ? \"YES\" : \"NO\"\nend\n"}, {"source_code": "t = gets.chomp.to_i\nt.times do\n a = gets.chomp.to_i\n \n rem = 360 % (180 - a)\n \n puts (rem == 0 ? \"YES\" : \"NO\")\nend"}, {"source_code": "n = gets.to_i\n\nok = []\n\n180.times do |i|\n\tk = 360.0 / (180 - i)\n\tif k == k.to_i\n\t\tok << i\n\tend\nend\n\nn.times do |i|\n\ta = gets.to_i\n\t\n\tif ok.include?(a)\n\t\tputs 'YES'\n\telse \n\t\tputs 'NO'\n\tend\nend"}, {"source_code": "t = gets.chomp.to_i\n(1..t).each do |x|\n\ty = gets.chomp.to_i\n\tif y == 180 then puts \"NO\" \n\telse puts((360 % (180-y) == 0) ?\"YES\":\"NO\") end\nend"}, {"source_code": "#$stdin = File.open(\"1.txt\")\nT = gets.chomp.to_i\nT.times do\n\ta = gets.chomp.to_f\n\tif (360/(180-a)) % 1 == 0\n\t\tputs \"YES\"\n\telse\n\t\tputs \"NO\"\n\tend\nend"}, {"source_code": "gets.to_i.times do\n puts 360 % (180 - gets.to_i) == 0 ? \"YES\" : \"NO\"\nend"}, {"source_code": "n = gets.to_i\n\nans = []\nn.times do |i|\n ang = gets.to_f\n a = (360/(180-ang)).to_i == (360/(180-ang)) ? \"YES\" : \"NO\"\n ans << a\nend\n\nans.each {|i| puts i}"}, {"source_code": "# cook your code here\n\nnum = gets.to_i \n\nfor i in 1..num\n x = gets.to_i \n if 360%(180-x)==0 \n puts \"YES\"\n else \n puts \"NO\" \n end\nend "}, {"source_code": "n = gets.chomp.to_i\nfor i in 1..n\n a = gets.chomp.to_i\n if 360 % (180-a) == 0\n puts \"YES\"\n else\n puts \"NO\"\n end\nend\n"}, {"source_code": "n=gets.to_i.times do\na=gets.to_i\nif a>=60 and a<180 and 360%(180-a)==0\nputs \"YES\"\nelse\nputs \"NO\"\nend\nend\n"}, {"source_code": "require 'set'\ns = Set.new\na = Array.new\nn = gets.to_i.times do\n\ta << gets.to_i\nend\nfor i in 1..179 do\n\ts.add(i) if 360 % (180 - i) == 0\nend\na.each{|el| s.include?(el) ? (puts \"YES\") : (puts \"NO\")}\n\t\n\n\n\n\t\n\n\t\t\n\n\n\n\n\n"}, {"source_code": "n = gets.to_i\nn.times do\n a = gets.to_i\n puts(if 360 % (180 - a) == 0 then \"YES\" else \"NO\" end)\nend"}, {"source_code": "num_tests = gets.to_i\nfor i in 0...num_tests\n\tangle = gets.to_i\n\tsides = 2.0/(1-(angle/180.0))\n\tif (sides - (sides+0.5).to_i).abs < 0.00000001\n\t\tputs \"YES\"\n\telse\n\t\tputs \"NO\"\n\tend\nend\n"}, {"source_code": "#!/usr/bin/ruby1.9\n\nt = STDIN.readline.to_i\nt.times do\n\tinterior_angle = STDIN.readline.to_i\n\tif 360%(180-interior_angle) == 0\n\t\tputs \"YES\"\n\telse\n\t\tputs \"NO\"\n\tend\nend\n\n"}, {"source_code": "n = gets.to_i\nn.times{\n\t\ta = gets.to_i\n\t\tx = 360.0 / ( 180.0 - a )\n\t\tif (x == x.floor) then\n\t\t\tputs \"YES\"\n\t\telse\n\t\t\tputs \"NO\"\n\t\tend\n}\t"}, {"source_code": "gets.to_i.times do\n x = gets.to_i\n puts 360 % (180 - x) == 0 ? \"YES\" : \"NO\"\nend\n"}, {"source_code": "n = gets.chomp.to_i\n\nn.times do\n x = gets.chomp.to_i\n puts 360 % (180 - x) == 0 ? 'YES' : \"NO\"\nend\n"}, {"source_code": "n = gets.to_i\nn.times do \n a = gets.to_i\n n = 360.0 / (180 - a)\n puts n.to_i == n ? \"YES\" : \"NO\"\nend"}, {"source_code": "def run\n ary = []\n cnt = 3\n begin\n ary << (180.0 * (cnt - 2)) / cnt\n cnt += 1\n end while ary.last < 179\n ary.uniq!\n\n n = ARGF.readline.chomp\n ARGF.readlines.map(&:to_i).each do |val|\n puts ary.include?(val) ? \"YES\" : \"NO\"\n end\nend\n\nrun if $0 == __FILE__\n"}, {"source_code": "arr = []\ngets.to_i.times do\n arr << gets.to_i\nend\narr.each { |x| puts 360 % (180 - x) == 0 ? \"YES\" : \"NO\" }\n"}, {"source_code": "n=gets.to_i\ns=(3..360).select{|e|\n\t(360%e==0)\n}\ns.map!{|e|\n\t180-360/e\n}\n\nn.times{\n\tm=gets.to_i\n\tif s.include?(m) then\n\t\tputs \"YES\"\n\telse\n\t\tputs \"NO\"\n\tend\n}"}, {"source_code": "n = gets.to_i\nn.times do |i|\n\tx = gets.to_f\n\tx = (-2 * 180) / (x - 180)\n\tputs x % 1 == 0 ? 'YES' : 'NO'\nend"}, {"source_code": "require 'set'\ninclude Math\n$stdin = File.open(\"./input.txt\", \"r\") if ENV[\"USER\"] == \"new\"\n# ====================================\n\nn = gets.to_i\n\nn.times do\n angle = gets.to_i\n n = 360.0 / (180.0 - angle)\n puts ((n.ceil == n.floor) ? :YES : :NO)\nend"}, {"source_code": "require 'set'\ninclude Math\n$stdin = File.open(\"./input.txt\", \"r\") if ENV[\"USER\"] == \"new\"\n# ====================================\n\nn = gets.to_i\n\ndegrees = Set.new\nindex = 3\nloop do\n angle = (index - 2) * 180.0 / index\n degrees << angle.to_i if angle.ceil == angle.floor\n index += 1\n \n break if index>2000\nend\n\nn.times do\n angle = gets.to_i\n puts ((degrees.include? angle) ? :YES : :NO)\nend"}], "negative_code": [{"source_code": "require 'set'\ninclude Math\n$stdin = File.open(\"./input.txt\", \"r\") if ENV[\"USER\"] == \"new\"\n# ====================================\n\nn = gets.to_i\n\ndegrees = Set.new\nindex = 3\nloop do\n angle = (index - 2) * 180 / index\n degrees << angle\n index += 1\n \n break if index>1000\nend\n\nn.times do\n angle = gets.to_i\n puts ((degrees.include? angle) ? :YES : :NO)\nend"}, {"source_code": "require 'set'\ninclude Math\n$stdin = File.open(\"./input.txt\", \"r\") if ENV[\"USER\"] == \"new\"\n# ====================================\n\nn = gets.to_i\n\ndegrees = [60, 90, 108, 120, 128, 135, 140, 144, 147, 150, 152, 154, 156, 157, 158, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179]\n\nn.times do\n angle = gets.to_i\n puts ((degrees.include? angle) ? :YES : :NO)\nend"}, {"source_code": "require 'set'\ninclude Math\n$stdin = File.open(\"./input.txt\", \"r\") if ENV[\"USER\"] == \"new\"\n# ====================================\n\nn = gets.to_i\n\ndegrees = Set.new\nindex = 3\nloop do\n angle = (index - 2) * 180.0 / index\n degrees << angle.to_i if angle.ceil == angle.floor\n index += 1\n \n break if index>2000\nend\n\nputs degrees.inspect\n\nn.times do\n angle = gets.to_i\n puts ((degrees.include? angle) ? :YES : :NO)\nend"}, {"source_code": "require 'set'\ninclude Math\n$stdin = File.open(\"./input.txt\", \"r\") if ENV[\"USER\"] == \"new\"\n# ====================================\n\nn = gets.to_i\n\ndegrees = []\n3.upto(12) do |index|\n degrees << (index - 2) * 180 / index\nend\n\nn.times do\n angle = gets.to_i\n puts ((degrees.include? angle) ? :YES : :NO)\nend"}, {"source_code": "gets.to_i.times{\n\ta=gets.to_i\n\tputs ((a>=60 && a<180) ? 'YES' : 'NO')\n}\n"}, {"source_code": "puts gets.to_i.times.map{gets.to_i}.map{|x| (x>=60&&360%x==0)}"}, {"source_code": "puts gets.to_i.times.map{gets.to_i}.map{|x| (x>=60&&360%x==0)? \"YES\" : \"NO\"}"}, {"source_code": "puts gets.to_i.times.map{gets.to_i}.map{|x| (x>=60)? \"YES\" : \"NO\"}"}, {"source_code": "#$stdin = File.open(\"1.txt\")\nT = gets.chomp.to_i\n\nH = {}\n\n3.upto(300) do |c|\n\tH[180*(c-2)/c] = 1\nend\nT.times do\n\ta = gets.chomp.to_i\n\tif H.has_key? (a)\n\t\tputs \"YES\"\n\telse\n\t\tputs \"NO\"\n\tend\nend"}, {"source_code": "n=gets.to_i.times do\na=gets.to_i\nif a>=60 and a<180\nputs \"YES\"\nelse\nputs \"NO\"\nend\nend\n"}, {"source_code": "possible_angles = [0,60,90,108,120,128,135,140,144,147,150,152,154,156,157,158,160]\ntests = gets.to_i\nfor i in 0...tests\n\ttest_case = gets.to_i\n\tif test_case > 160 and test_case <= 180\n\t\tputs \"YES\"\n\t\tnext\n\tend\n\twhile test_case > 180\n\t\ttest_case -= 180\n\tend\n\tif possible_angles.include?(test_case)\n\t\tputs \"YES\"\n\telse\n\t\tputs \"NO\"\n\tend\nend\n"}, {"source_code": "num_tests = gets.to_i\nfor i in 0...num_tests\n\tangle = gets.to_i\n\tsides = 2.0/(1-(angle/180.0))\n\tif (sides - (sides+0.5).to_i) < 0.00000001\n\t\tputs \"YES\"\n\telse\n\t\tputs \"NO\"\n\tend\nend\n"}, {"source_code": "n = gets.to_i\nn.times do \n a = gets.to_i\n n = 360 / (180 - a)\n puts n.to_i == n ? \"YES\" : \"NO\"\nend\nputs \"over\""}, {"source_code": "n = gets.to_i\nn.times do \n a = gets.to_i\n n = 360 / (180 - a)\n puts n.to_i == n ? \"YES\" : \"NO\"\nend"}, {"source_code": "def run\n ary = []\n cnt = 3\n begin\n ary << (180 * (cnt - 2)) / cnt\n cnt += 1\n end while ary.last < 179\n ary.uniq!\n\n n = ARGF.readline\n ARGF.readlines.map(&:to_i).each do |val|\n puts ary.include?(val) ? \"YES\" : \"NO\"\n end\nend\n\nrun if $0 == __FILE__\n"}, {"source_code": "def run\n ary = []\n cnt = 3\n begin\n angle = (180 * (cnt - 2)) / cnt\n ary[cnt] = angle\n cnt += 1\n end while angle < 179\n\n n = ARGF.readline\n ARGF.readlines.map(&:to_i).each do |val|\n puts ary.include?(val) ? \"YES\" : \"NO\"\n end\nend\n\nrun if $0 == __FILE__\n"}, {"source_code": "n = gets.to_i\nn.times do |i|\n\tx = gets.to_f\n\tx = (-2 * 180) / (x - 180)\n\tputs x\n\tputs x % 1 == 0 ? 'YES' : 'NO'\nend"}, {"source_code": "require 'set'\ninclude Math\n$stdin = File.open(\"./input.txt\", \"r\") if ENV[\"USER\"] == \"new\"\n# ====================================\n\nn = gets.to_i\n\ndegrees = []\n3.upto(30) do |index|\n degrees << (index - 2) * 180 / index\nend\n\nn.times do\n angle = gets.to_i\n puts ((degrees.include? angle) ? :YES : :NO)\nend"}], "src_uid": "9037f487a426ead347baa803955b2c00"} {"nl": {"description": "You are given $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$. You choose any subset of the given numbers (possibly, none or all numbers) and negate these numbers (i. e. change $$$x \\to (-x)$$$). What is the maximum number of different values in the array you can achieve?", "input_spec": "The first line of input contains one integer $$$t$$$ ($$$1 \\leq t \\leq 100$$$): the number of test cases. The next lines contain the description of the $$$t$$$ test cases, two lines per a test case. In the first line you are given one integer $$$n$$$ ($$$1 \\leq n \\leq 100$$$): the number of integers in the array. The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$-100 \\leq a_i \\leq 100$$$).", "output_spec": "For each test case, print one integer: the maximum number of different elements in the array that you can achieve negating numbers in the array.", "sample_inputs": ["3\n4\n1 1 2 2\n3\n1 2 3\n2\n0 0"], "sample_outputs": ["4\n3\n1"], "notes": "NoteIn the first example we can, for example, negate the first and the last numbers, achieving the array $$$[-1, 1, 2, -2]$$$ with four different values.In the second example all three numbers are already different.In the third example negation does not change anything."}, "positive_code": [{"source_code": "inputs = STDIN.read.strip.split(\"\\n\")\r\ninputs.shift\r\n\r\ninputs.each_slice(2) do |inp|\r\n inp.shift\r\n nums = inp[0].split(' ').map(&:to_i)\r\n\r\n found = {}\r\n nums.each do |n|\r\n if found[n]\r\n found[n * - 1] = true\r\n else\r\n found[n] = true\r\n end\r\n end\r\n\r\n pp found.keys.count\r\nend"}], "negative_code": [], "src_uid": "23ef311011b381d0ca2e84bc861f0a31"} {"nl": {"description": "While Patrick was gone shopping, Spongebob decided to play a little trick on his friend. The naughty Sponge browsed through Patrick's personal stuff and found a sequence a1, a2, ..., am of length m, consisting of integers from 1 to n, not necessarily distinct. Then he picked some sequence f1, f2, ..., fn of length n and for each number ai got number bi = fai. To finish the prank he erased the initial sequence ai.It's hard to express how sad Patrick was when he returned home from shopping! We will just say that Spongebob immediately got really sorry about what he has done and he is now trying to restore the original sequence. Help him do this or determine that this is impossible.", "input_spec": "The first line of the input contains two integers n and m (1 ≤ n, m ≤ 100 000) — the lengths of sequences fi and bi respectively. The second line contains n integers, determining sequence f1, f2, ..., fn (1 ≤ fi ≤ n). The last line contains m integers, determining sequence b1, b2, ..., bm (1 ≤ bi ≤ n).", "output_spec": "Print \"Possible\" if there is exactly one sequence ai, such that bi = fai for all i from 1 to m. Then print m integers a1, a2, ..., am. If there are multiple suitable sequences ai, print \"Ambiguity\". If Spongebob has made a mistake in his calculations and no suitable sequence ai exists, print \"Impossible\".", "sample_inputs": ["3 3\n3 2 1\n1 2 3", "3 3\n1 1 1\n1 1 1", "3 3\n1 2 1\n3 3 3"], "sample_outputs": ["Possible\n3 2 1", "Ambiguity", "Impossible"], "notes": "NoteIn the first sample 3 is replaced by 1 and vice versa, while 2 never changes. The answer exists and is unique.In the second sample all numbers are replaced by 1, so it is impossible to unambiguously restore the original sequence.In the third sample fi ≠ 3 for all i, so no sequence ai transforms into such bi and we can say for sure that Spongebob has made a mistake."}, "positive_code": [{"source_code": "#! ruby\n# Try Codeforces\n# author: Leonardone @ NEETSDKASU\n\ngt = proc { gets.chomp.split.map(&:to_i) }\n\nn, m = gt.call\nf = gt.call\nb = gt.call\n\nfm = {}\nn.times do |i|\n k = f[i]\n if fm.key? k\n fm[k] << [i]\n else\n fm[k] = [i]\n end\nend\n\nans = \"\"\na = [0] * m\nm.times do |i|\n k = b[i]\n if fm.key? k\n a[i] = fm[k][0].succ\n if fm[k].size > 1\n ans = \"Ambiguity\"\n end\n else\n puts 'Impossible'\n exit\n end\nend\n\nif ans.empty?\n puts 'Possible'\n puts a * \" \"\nelse\n puts ans\nend\n\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\nf = gets.split.map(&:to_i)\nb = gets.split.map(&:to_i)\n\nf_map = {}\n\nf.each_index do |i|\n f_map[f[i]] ||= []\n f_map[f[i]] << i + 1\nend\n\na = []\nstatus = 'Possible'\n\nb.each do |bi|\n f_map[bi] ||= []\n case f_map[bi].size\n when 0\n status = 'Impossible'\n break\n when 1\n a << f_map[bi][0]\n else\n if status == 'Possible'\n status = 'Ambiguity'\n end\n end\nend\n\nputs status\nif status == 'Possible'\n puts a.join ' '\nend\n"}, {"source_code": "n, m = gets.chomp.split.map(&:to_i)\nfs = gets.chomp.split.map(&:to_i)\nbs = gets.chomp.split.map(&:to_i)\n\nhs = Hash.new { |hs, k| hs[k] = [] }\n\n(1..n).map { |i| hs[fs[i - 1]] << i }\n\nas = []\nans = 'Possible'\nbs.each do |b|\n s = hs[b].size\n if s == 0\n ans = 'Impossible'\n break\n elsif 1 < s\n ans = 'Ambiguity'\n end\n as << hs[b][0]\nend\n\nputs ans\nputs as.*' ' if ans == 'Possible'\n"}, {"source_code": "require \"set\"\nn, m = gets.split().map(&:to_i)\nf = gets.split().map(&:to_i)\nb = gets.split().map(&:to_i)\nf = [0] + f\nb = [0] + b\ng = [0]*n\nimf = Set.new()\nd = Set.new()\nx = 0\nfor i in 1..n\n\tif imf.include? f[i]\n\t\td.add(f[i])\n\telse\n\t\timf.add(f[i])\n\tend\n\tg[f[i]] = i\nend\nfor i in 1..m\n\tif not imf.include? b[i]\n\t\tputs \"Impossible\"\n\t\tx = 1\n\t\tbreak\n\tend\nend\nif x != 1\n\ty = 0\n\tfor i in 1..m\n\t\tif d.include? b[i]\n\t\t\tputs \"Ambiguity\"\n\t\t\ty =1\n\t\t\tbreak\n\t\tend\n\tend\n\tif y != 1\n\t\tputs \"Possible\"\n\t\tfor i in 1...m\n\t\t\tprint(g[b[i]])\n\t\t\tprint(\" \")\n\t\tend\n\t\tputs g[b[m]]\n\tend\nend\n"}, {"source_code": "require 'scanf'\n\nn, m = scanf(\"%d %d\\n\")\n#f = Array.new(n)\nf_inv = Array.new(n + 1)\n#b = Array.new(m)\n\nf = gets.split.map { |i| i.to_i }\nb = gets.split.map { |i| i.to_i }\n(0..n-1).each do |i|\n f_inv[f[i]] = i + 1\nend\nf_map = Array.new(n + 1, 0)\n(0..n-1).each do |i|\n f_map[f[i]] += 1\nend\na = false, im = false\nans = []\n(0..m-1).each do |i|\n if f_map[b[i]] > 1\n a = true\n elsif f_map[b[i]] == 0\n im = true\n break\n else\n ans.push(f_inv[b[i]])\n end\nend\n\nif im == true\n puts \"Impossible\"\nelsif a == true\n puts \"Ambiguity\"\nelse\n puts \"Possible\"\n (0..ans.size-1).each do |i|\n print ans[i], ' '\n end\n puts\nend"}, {"source_code": "class Solver\n\tdef main\n\t\tn, m = gets.split.map { |x| x.to_i }\n\t\tf = gets.split.map { |x| x.to_i }\n\t\tb = gets.split.map { |x| x.to_i }\n\t\th = Hash.new { |hash, key| hash[key] = Array.new }\n\t\tf.each.with_index do |x, id|\n\t\t\th[x] << id\n\t\tend\n\n\t\tambiguity = false\n\t\timpossible = false\n\t\tans = []\n\t\tb.each do |x|\n\t\t\tif h[x].size == 0 then\n\t\t\t\timpossible = true\n\t\t\telsif h[x].size > 1 then\n\t\t\t\tambiguity = true\n\t\t\telse\n\t\t\t\tans << h[x][0] + 1\n\t\t\tend\n\t\tend\n\t\tif impossible then\n\t\t\tputs \"Impossible\"\n\t\telsif ambiguity then\n\t\t\tputs \"Ambiguity\"\n\t\telse\n\t\t\tputs \"Possible\"\n\t\t\tputs ans.join \" \"\n\t\tend\n\tend\nend\n\nSolver.new.main\n"}], "negative_code": [{"source_code": "n, m = gets.chomp.split.map(&:to_i)\nfs = gets.chomp.split.map(&:to_i)\nbs = gets.chomp.split.map(&:to_i)\n\nhs = Hash.new { |hs, k| hs[k] = [] }\n\n(1..m).map { |i| hs[fs[i - 1]] << i }\n\nas = []\nans = 'Possible'\nbs.each do |b|\n s = hs[b].size\n if s == 0\n ans = 'Impossible'\n break\n elsif 1 < s\n ans = 'Ambiguity'\n break\n end\n as << hs[b][0]\nend\n\nputs ans\nputs as.*' ' if ans == 'Possible'\n\n"}, {"source_code": "n, m = gets.chomp.split.map(&:to_i)\nfs = gets.chomp.split.map(&:to_i)\nbs = gets.chomp.split.map(&:to_i)\n\nhs = Hash.new()\n\n(1..m).map { |i| hs[fs[i - 1]] = i }\nfs.sort!\nbs.sort!\n\nif fs != bs\n puts 'Impossible'\nelse\n fs.uniq!\n if fs.size != m\n puts 'Ambiguity'\n else\n puts 'Possible'\n puts bs.map { |b| hs[b] }.*' '\n end\nend\n\n\n"}, {"source_code": "n, m = gets.chomp.split.map(&:to_i)\nfs = gets.chomp.split.map(&:to_i)\nbs = gets.chomp.split.map(&:to_i)\n\nhs = Hash.new { |hs, k| hs[k] = [] }\n\n(1..n).map { |i| hs[fs[i - 1]] << i }\n\nas = []\nans = 'Possible'\nbs.each do |b|\n s = hs[b].size\n if s == 0\n ans = 'Impossible'\n break\n elsif 1 < s\n ans = 'Ambiguity'\n break\n end\n as << hs[b][0]\nend\n\nputs ans\nputs as.*' ' if ans == 'Possible'\n"}], "src_uid": "468e8a14dbdca471f143f59b945508d0"} {"nl": {"description": "One day Alex decided to remember childhood when computers were not too powerful and lots of people played only default games. Alex enjoyed playing Minesweeper that time. He imagined that he saved world from bombs planted by terrorists, but he rarely won.Alex has grown up since then, so he easily wins the most difficult levels. This quickly bored him, and he thought: what if the computer gave him invalid fields in the childhood and Alex could not win because of it?He needs your help to check it.A Minesweeper field is a rectangle $$$n \\times m$$$, where each cell is either empty, or contains a digit from $$$1$$$ to $$$8$$$, or a bomb. The field is valid if for each cell: if there is a digit $$$k$$$ in the cell, then exactly $$$k$$$ neighboring cells have bombs. if the cell is empty, then all neighboring cells have no bombs. Two cells are neighbors if they have a common side or a corner (i. e. a cell has at most $$$8$$$ neighboring cells).", "input_spec": "The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n, m \\le 100$$$) — the sizes of the field. The next $$$n$$$ lines contain the description of the field. Each line contains $$$m$$$ characters, each of them is \".\" (if this cell is empty), \"*\" (if there is bomb in this cell), or a digit from $$$1$$$ to $$$8$$$, inclusive.", "output_spec": "Print \"YES\", if the field is valid and \"NO\" otherwise. You can choose the case (lower or upper) for each letter arbitrarily.", "sample_inputs": ["3 3\n111\n1*1\n111", "2 4\n*.*.\n1211"], "sample_outputs": ["YES", "NO"], "notes": "NoteIn the second example the answer is \"NO\" because, if the positions of the bombs are preserved, the first line of the field should be *2*1.You can read more about Minesweeper in Wikipedia's article."}, "positive_code": [{"source_code": "n, $m = gets.split.map &:to_i\n\n$prev = nil\n$curr = gets\n$ne = nil\n\ndef checkCurr() \n\tfor j in 0..$m-1\n \tif $curr[j] == \"*\"\n \t\tnext\n \tend\n \tbomb = 0\n \tfor k in [j-1, 0].max..[j+1, $m-1].min\n \t\tbomb += ($prev[k] == \"*\" ? 1 : 0) if $prev != nil\n \t\tbomb += ($curr[k] == \"*\" ? 1 : 0) if k != j\n \t\tbomb += ($ne[k] == \"*\" ? 1 : 0) if $ne != nil\n \tend\n\n \tif bomb != $curr[j].to_i\n \t\treturn false\n \tend\n end\nend\n\nres = 1\n\nfor i in 0..n-1\n\t\n\tif i < n-1\n \t$ne = gets\n else\n \t$ne = nil\n end\n \n if !checkCurr\n \tres = 0\n \tbreak\n end\n\n $prev = $curr \n $curr = $ne\n \nend\n\nif res == 1\n\tputs \"YES\"\nelse\n\tputs \"NO\"\nend\n\n\n"}], "negative_code": [], "src_uid": "0d586ba7d304902caaeb7cd9e6917cd6"} {"nl": {"description": "Codehorses has just hosted the second Codehorses Cup. This year, the same as the previous one, organizers are giving T-shirts for the winners.The valid sizes of T-shirts are either \"M\" or from $$$0$$$ to $$$3$$$ \"X\" followed by \"S\" or \"L\". For example, sizes \"M\", \"XXS\", \"L\", \"XXXL\" are valid and \"XM\", \"Z\", \"XXXXL\" are not.There are $$$n$$$ winners to the cup for both the previous year and the current year. Ksenia has a list with the T-shirt sizes printed for the last year cup and is yet to send the new list to the printing office. Organizers want to distribute the prizes as soon as possible, so now Ksenia is required not to write the whole list from the scratch but just make some changes to the list of the previous year. In one second she can choose arbitrary position in any word and replace its character with some uppercase Latin letter. Ksenia can't remove or add letters in any of the words.What is the minimal number of seconds Ksenia is required to spend to change the last year list to the current one?The lists are unordered. That means, two lists are considered equal if and only if the number of occurrences of any string is the same in both lists.", "input_spec": "The first line contains one integer $$$n$$$ ($$$1 \\le n \\le 100$$$) — the number of T-shirts. The $$$i$$$-th of the next $$$n$$$ lines contains $$$a_i$$$ — the size of the $$$i$$$-th T-shirt of the list for the previous year. The $$$i$$$-th of the next $$$n$$$ lines contains $$$b_i$$$ — the size of the $$$i$$$-th T-shirt of the list for the current year. It is guaranteed that all the sizes in the input are valid. It is also guaranteed that Ksenia can produce list $$$b$$$ from the list $$$a$$$.", "output_spec": "Print the minimal number of seconds Ksenia is required to spend to change the last year list to the current one. If the lists are already equal, print 0.", "sample_inputs": ["3\nXS\nXS\nM\nXL\nS\nXS", "2\nXXXL\nXXL\nXXL\nXXXS", "2\nM\nXS\nXS\nM"], "sample_outputs": ["2", "1", "0"], "notes": "NoteIn the first example Ksenia can replace \"M\" with \"S\" and \"S\" in one of the occurrences of \"XS\" with \"L\".In the second example Ksenia should replace \"L\" in \"XXXL\" with \"S\".In the third example lists are equal."}, "positive_code": [{"source_code": "n=gets.to_i\nh=Hash.new 0\ng=Hash.new 0\nn.times{h[gets.chomp]+=1}\nn.times{g[gets.chomp]+=1}\nr=0\nh.each{|k,v|r+=[0,g[k]-v].max;g.delete(k)}\ng.each{|k,v|r+=v}\np r"}, {"source_code": "N = gets.to_i\nhash = Hash.new(0)\nN.times do\n str = gets.chomp\n hash[str] += 1\nend\nans = 0\nN.times do\n str = gets.chomp\n if hash[str] > 0\n hash[str] -= 1\n else\n ans += 1\n end\nend\nputs ans"}, {"source_code": "n = gets.to_i\na =[]\nn.times {a << gets.chomp}\nn.times do\n s = gets.chomp\n n.times do |i|\n if a[i]==s\n a.delete_at(i)\n break\n end\n end\nend\nputs a.length"}], "negative_code": [{"source_code": "n=gets.to_i\nh=Hash.new 0\ng=Hash.new 0\nn.times{h[gets.to_i]+=1}\nn.times{g[gets.to_i]+=1}\nr=0\nh.each{|k,v|r+=(v-g[k]).abs;g.delete(k)}\ng.each{|k,v|r+=v}\np r"}, {"source_code": "n=gets.to_i\nh=Hash.new 0\ng=Hash.new 0\nn.times{h[gets.to_i]+=1}\nn.times{g[gets.to_i]+=1}\nr=0\nh.each{|k,v|r+=(v-g[k]).abs;g.delete(k)}\nr/=2\ng.each{|k,v|r+=v}\np r"}, {"source_code": "n=gets.to_i\nh=Hash.new 0\ng=Hash.new 0\nn.times{h[gets.chomp]+=1}\nn.times{g[gets.chomp]+=1}\nr=0\nh.each{|k,v|r+=[0,g[k]-v].max}\ng.each{|k,v|r+=v}\np r"}, {"source_code": "n=gets.to_i\nh=Hash.new 0\ng=Hash.new 0\nn.times{h[gets.chomp]+=1}\nn.times{g[gets.chomp]+=1}\nr=0\nh.each{|k,v|r+=(v-g[k]).abs;g.delete(k)}\ng.each{|k,v|r+=v}\np r"}, {"source_code": "n = gets.to_i\na1 = []\na2 = []\nn.times { a1 << gets.chomp.chars}\n\nn.times { a2 << gets.chomp.chars}\n\na1.sort!{|a,b| a.length <=> b.length}\na2.sort!{|a,b| a.length <=> b.length}\nres = 0\n\nn.times do |i|\n res += (a1[i] - a2[i]).length\nend\nputs res"}, {"source_code": "n = gets.to_i\na =[]\na2=[]\nn.times {a << gets.chomp}\nn.times{a2 << gets.chomp}\nputs (a2-a).length"}, {"source_code": "n = gets.to_i\na1 = []\na2 = []\nn.times { s = gets.chomp\nvalid = (s =~ /(\\A(X{0,3})(S|L)$)|(\\A(S|L|M)$)/) != nil\na1 << s.chars if valid\n}\n\nn.times { s = gets.chomp\nvalid = (s =~ /(\\A(X{0,3})(S|L)$)|(\\A(S|L|M)$)/) != nil\na2 << s.chars if valid}\n\na1.sort!{|a,b| a.length <=> b.length}\na2.sort!{|a,b| a.length <=> b.length}\nres = 0\nn.times do |i|\n res += (a1[i] - a2[i]).length\nend\nputs res"}], "src_uid": "c8321b60a6ad04093dee3eeb9ee27b6f"} {"nl": {"description": "A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values — the start time li and the finish time ri (li ≤ ri).Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.", "input_spec": "The first line contains integer number n (1 ≤ n ≤ 5·105) — number of orders. The following n lines contain integer values li and ri each (1 ≤ li ≤ ri ≤ 109).", "output_spec": "Print the maximal number of orders that can be accepted.", "sample_inputs": ["2\n7 11\n4 7", "5\n1 2\n2 3\n3 4\n4 5\n5 6", "6\n4 8\n1 5\n4 7\n2 5\n1 3\n6 8"], "sample_outputs": ["1", "3", "2"], "notes": null}, "positive_code": [{"source_code": "#!/usr/bin/env ruby\n\nnum_orders = gets.to_i\norders = num_orders.times.map do gets.split(' ').map(&:to_i) end\norders = orders.sort_by do |order| order[1] end\n\ntotal = 0\ncurrent_right = 0\norders.each do |left, right|\n if current_right < left\n current_right = right\n total += 1\n end\nend\n\nputs total\n"}, {"source_code": "\nMAX = 10**9 + 1\n\nn = 0\nn = gets.chomp.to_i\n\nnumbers = Array.new\n# input\nfor i in 0...n do\n\tt = gets.chomp.split \" \"\n\tnumbers.push t.last.to_i * MAX + t.first.to_i\nend\n\nnumbers.sort!\n\n# calculating answer\nans = 1\nlast_fin = numbers[0] / MAX\n\nfor i in 1...n do\n\tif numbers[i] % MAX > last_fin\n\t\tans = ans + 1\n\t\tlast_fin = numbers[i] / MAX\n\tend\n\t\nend\n\nputs ans"}, {"source_code": "class Solver\n\tdef main\n\t\tn = gets.to_i\n\t\torders = []\n\t\tn.times do\n\t\t\torders << gets.split.map{ |x| x.to_i }\n\t\tend\n\t\torders.sort! { |a, b| a[1] <=> b[1] }\n\n\t\tans = 0\n\t\tlast = 0\n\t\torders.each do |order|\n\t\t\tif order[0] > last then\n\t\t\t\tlast = order[1]\n\t\t\t\tans += 1\n\t\t\tend\n\t\tend\n\t\tputs ans\n\tend\nend\n\nSolver.new.main"}], "negative_code": [{"source_code": "\nn = 0\nl = Array.new\nr = Array.new\n\nn = gets.chomp.to_i\n# input\nfor i in 0...n do\n\tt = gets.chomp.split \" \"\n\tl[i] = t.first.to_i\n\tr[i] = t.last.to_i\nend\n\n# insertion sorting\nfor i in 1...n do\n\t\n\tk = r[i]\n\tj = i\n\n\twhile j > 0 and r[j] < r[j-1] do\n\t\tr[j] = r[j-1]\n\t\tj = j - 1\n\tend\n\t\n\tr[j] = k\n\t\n\tk = l[i]\n\tl[i] = l[j]\n\tl[j] = k\t\nend\n\n# calculating answer\nans = 1\nr_last = r[0]\n\nfor i in 1...n do\n\tif l[i] > r_last\n\t\tans = ans + 1\n\t\tr_last = r[i]\n\tend\nend\n\nputs ans"}, {"source_code": "\nn = 0\nl = Array.new\nr = Array.new\n\nn = gets.chomp.to_i\n\npointer = Hash.new\nmax_len = 1\n# input\nfor i in 0...n do\n\tt = gets.chomp.split \" \"\n\tl[i] = t.first\n\tr[i] = t.last\n\tif r[i].length > max_len\n\t\tmax_len = r[i].length\n\tend\n\t\n\tif pointer.keys.include? r[i]\n\t\tpointer[r[i]].push l[i]\n\telse\n\t\tpointer[r[i]] = Array.new\n\t\tpointer[r[i]].push l[i]\n\tend\nend\n\npointer.each_key {|key| pointer[key].sort!}\nr.sort!\n# radix sorting\n\n\n# calculating answer\nans = 1\nr_last = r[0]\n\nfor i in 1...n do\n\tif pointer[r[i]].shift.to_i > r_last.to_i\n\t\tans = ans + 1\n\t\tr_last = r[i]\n\tend\nend\n\nputs ans\n"}, {"source_code": "n = 0\nl = Array.new\nr = Array.new\n\nn = gets.chomp.to_i\n\nstrings = Array.new\nmax_len = 1\n# input\nfor i in 0...n do\n\tt = gets.chomp.split \" \"\n\tstrings.push t.last + \" \" + t.first\nend\n\nstrings.sort!\n\n# calculating answer\nans = 1\nlast_fin = (strings[0].split(\" \")).first.to_i\n\nfor i in 1...n do\n\tt = strings[i].split(\" \")\n\tif t.last.to_i > last_fin.to_i\n\t\tans = ans + 1\n\t\tlast_fin = strings[i].split(\" \").first.to_i\n\tend\n\t\nend\n\n=begin\nfor i in 0...n \n\tprint l[i], \" \", r[i]\n\tputs\n=end\n\nputs ans"}, {"source_code": "n = 0\nn = gets.chomp.to_i\n\nnumbers = Array.new\n# input\nfor i in 0...n do\n\tt = gets.chomp.split \" \"\n\tnumbers.push t.last.to_i * 500001 + t.first.to_i\nend\n\nnumbers.sort!\n\n# calculating answer\nans = 1\nlast_fin = numbers[0] / 500001\n\nfor i in 1...n do\n\tif numbers[i] % 500001 > last_fin\n\t\tans = ans + 1\n\t\tlast_fin = numbers[i] / 500001\n\tend\n\t\nend\n\nputs ans"}], "src_uid": "73e8984cceded15027c4ab89c5624a92"} {"nl": {"description": "Kefa decided to celebrate his first big salary by going to the restaurant. He lives by an unusual park. The park is a rooted tree consisting of n vertices with the root at vertex 1. Vertex 1 also contains Kefa's house. Unfortunaely for our hero, the park also contains cats. Kefa has already found out what are the vertices with cats in them.The leaf vertices of the park contain restaurants. Kefa wants to choose a restaurant where he will go, but unfortunately he is very afraid of cats, so there is no way he will go to the restaurant if the path from the restaurant to his house contains more than m consecutive vertices with cats. Your task is to help Kefa count the number of restaurants where he can go.", "input_spec": "The first line contains two integers, n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ n) — the number of vertices of the tree and the maximum number of consecutive vertices with cats that is still ok for Kefa. The second line contains n integers a1, a2, ..., an, where each ai either equals to 0 (then vertex i has no cat), or equals to 1 (then vertex i has a cat). Next n - 1 lines contains the edges of the tree in the format \"xi yi\" (without the quotes) (1 ≤ xi, yi ≤ n, xi ≠ yi), where xi and yi are the vertices of the tree, connected by an edge. It is guaranteed that the given set of edges specifies a tree.", "output_spec": "A single integer — the number of distinct leaves of a tree the path to which from Kefa's home contains at most m consecutive vertices with cats.", "sample_inputs": ["4 1\n1 1 0 0\n1 2\n1 3\n1 4", "7 1\n1 0 1 1 0 0 0\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7"], "sample_outputs": ["2", "2"], "notes": "NoteLet us remind you that a tree is a connected graph on n vertices and n - 1 edge. A rooted tree is a tree with a special vertex called root. In a rooted tree among any two vertices connected by an edge, one vertex is a parent (the one closer to the root), and the other one is a child. A vertex is called a leaf, if it has no children.Note to the first sample test: The vertices containing cats are marked red. The restaurants are at vertices 2, 3, 4. Kefa can't go only to the restaurant located at vertex 2.Note to the second sample test: The restaurants are located at vertices 4, 5, 6, 7. Kefa can't go to restaurants 6, 7."}, "positive_code": [{"source_code": "def getai; return gets.split.map{|e| e.to_i}; end\nnm = getai\nn = nm[0]; m = nm[1]\na = getai\nadj = Array.new(n)\n(n - 1).times do\n xy = getai\n x = xy[0] - 1\n y = xy[1] - 1\n nil == adj[x] ? adj[x] = [y] : adj[x] << y\n nil == adj[y] ? adj[y] = [x] : adj[y] << x\nend\nstack = [0]\nused = Array.new(n, false)\nlast = Array.new(n, -1)\ncats = Array.new(n)\ncats[0] = a[0]\nwhile (!stack.empty?) # DFS\n v = stack.last\n used[v] = true\n l = last[v] + 1\n if l < adj[v].length && used[adj[v][l]]\n l += 1\n end\n if l < adj[v].length\n to = adj[v][l]\n cats[to] = cats[v] * a[to] + a[to]\n if cats[to] <= m\n stack << to\n end\n else\n stack.pop\n end\n last[v] = l\nend\ngood_leaves = 0\nfor v in 0..n-1\n if adj[v].length == 1 && used[v] && v != 0\n good_leaves += 1\n end\nend\nputs good_leaves\n"}, {"source_code": "def ri; gets.split.map(&:to_i); end\nn, m = ri\ncats = [nil] + ri\nedges = Array.new(n + 1){Array.new}\n(n - 1).times do\n u, v = ri\n edges[u] << v\n edges[v] << u\nend\n\nans = 0\nque = [[1, cats[1]]]\nuntil que.empty?\n u, s = que.shift\n ans += 1 if edges[u].empty?\n edges[u].each do |v|\n edges[v].delete(u)\n ns = cats[v] == 1 ? s + 1 : 0\n que << [v, ns] if ns <= m\n end\nend\nputs ans\n\n"}, {"source_code": "def ri; gets.split.map(&:to_i); end\nn, m = ri\ncats = [nil] + ri\nedges = Array.new(n + 1){Array.new}\n(n - 1).times do\n u, v = ri\n edges[u] << v\n edges[v] << u\nend\n\nans = 0\nque = [[1, cats[1]]]\nuntil que.empty?\n u, s = que.shift\n ans += 1 if edges[u].empty?\n edges[u].each do |v|\n edges[v].delete(u)\n ns = cats[v] == 1 ? s + 1 : 0\n que << [v, ns] if ns <= m\n end\nend\nputs ans\n\n"}, {"source_code": "def ri; gets.split.map(&:to_i); end\nn, m = ri\ncats = [nil] + ri\nedges = Array.new(n + 1){Array.new}\n(n - 1).times do\n u, v = ri\n edges[u] << v\n edges[v] << u\nend\n\nans = 0\nque = [[1, cats[1]]]\nuntil que.empty?\n u, s = que.shift\n ans += 1 if edges[u].empty?\n edges[u].each do |v|\n edges[v].delete(u)\n ns = cats[v] == 1 ? s + 1 : 0\n que << [v, ns] if ns <= m\n end\nend\nputs ans"}, {"source_code": "def ri; gets.split.map(&:to_i); end\nn, m = ri\ncats = [nil] + ri\nedges = Array.new(n + 1){Array.new}\n(n - 1).times do\n u, v = ri\n edges[u] << v\n edges[v] << u\nend\n\nans = 0\nque = [[1, cats[1]]]\nuntil que.empty?\n u, s = que.shift\n ans += 1 if edges[u].empty?\n edges[u].each do |v|\n edges[v].delete(u)\n ns = cats[v] == 1 ? s + 1 : 0\n que << [v, ns] if ns <= m\n end\nend\nputs ans\n\n"}, {"source_code": "def ri; gets.split.map(&:to_i); end\nn, m = ri\ncats = [nil] + ri\nedges = Array.new(n + 1){Array.new}\n(n - 1).times do\n u, v = ri\n edges[u] << v\n edges[v] << u\nend\n\nans = 0\nque = [[1, cats[1]]]\nuntil que.empty?\n u, s = que.shift\n ans += 1 if edges[u].empty?\n edges[u].each do |v|\n edges[v].delete(u)\n ns = cats[v] == 1 ? s + 1 : 0\n que << [v, ns] if ns <= m\n end\nend\nputs ans\n\n"}, {"source_code": "n,m=gets.split.map{|e| e.to_i}\nx=gets.split.map{|e| e.to_i}\narr=[]\nn.times{\n\tarr<<[]\n}\n(n-1).times{\n\ta,b=gets.split.map{|e| e.to_i-1}\n\tarr[a]<1) && arr[e[0]].size>e[1] then\n\t\tp1=arr[e[0]][e[1]]\n\t\tstc.last[1]+=1\n\t\ts1=stc.size\n\t\tif s1>1 && stc[s1-2][0]==p1 then\n\t\t\tnext\n\t\tend\n\t\tadd=x[p1]\n\t\tt1=0\n\t\tif add==1 then\n\t\t\tt1=e[2]+add\n\t\tend\n\t\tif t1<=m then\n\t\t\tstc<<[p1,0,t1]\n\t\tend\n\telse\n\t\tif e[0]==0 then\n\t\t\tbreak\n\t\telsif arr[e[0]].size==1 then\n\t\t\tif e[2]<=m then\n\t\t\t\tans+=1\n\t\t\tend\n\t\tend\n\t\tstc.pop\n\tend\n\t\nend\nputs ans\n\n"}, {"source_code": "# coding: utf-8\n\nrequire \"set\"\n\n# if __FILE__ == $0\n# end\n\nn, m = gets.split.map(&:to_i)\ncats = [nil] + gets.split.map(&:to_i) # 1-based vertices\nvertices = {}\n(n-1).times {\n u, v = gets.split.map(&:to_i)\n\n vertices[v] ||= [] # initialize u with empty array\n vertices[u] ||= []\n vertices[u] << v\n vertices[v] << u\n}\n\n# Remember, rooted tree\n#p vertices\nTRAVERSE_UP = -99\nstack = [1]\ntraverse = []\ntraverse_hashed = {} # do not delete, just mark with nil, put/delete with traverse stack control\ncat_acc_stack = [0] # accumulator stack. push/pop with traverse stack control\nreachables = []\nwhile !stack.empty?\n i = stack.pop\n if i == TRAVERSE_UP\n traverse.pop\n cat_acc_stack.pop\n next\n end\n\n if cat_acc_stack.last + cats[i] > m\n # you can stop traversing from here. children can not be reached.\n # visiting node_i should stop\n next # back to stack again\n else\n #p vertices[i]\n #p traverse\n if vertices[i].length == 1 && vertices[i][0] == traverse.last\n reachables << i\n else\n # new traverse starts!\n if cats[i] == 0\n cat_acc_stack << 0\n else\n cat_acc_stack << cat_acc_stack.last + cats[i]\n end\n\n traverse.push(i)\n traverse_hashed[i] = 1\n stack.push(TRAVERSE_UP)\n for ni in vertices[i]\n if traverse_hashed[ni] == nil\n stack.push(ni)\n end\n end\n end \n end\nend\n\n#p vertices\n#p reachables\nputs reachables.length # print reachables\n"}, {"source_code": "def ri; gets.split.map(&:to_i); end\nn, m = ri\ncats = [nil] + ri\nedges = Array.new(n + 1){Array.new}\n(n - 1).times do\n u, v = ri\n edges[u] << v\n edges[v] << u\nend\n\nans = 0\nque = [[1, cats[1]]]\nuntil que.empty?\n u, s = que.shift\n ans += 1 if edges[u].empty?\n edges[u].each do |v|\n edges[v].delete(u)\n ns = cats[v] == 1 ? s + 1 : 0\n que << [v, ns] if ns <= m\n end\nend\nputs ans\n\n"}, {"source_code": "def ri; gets.split.map(&:to_i); end\nn, m = ri\ncats = [nil] + ri\nedges = Array.new(n + 1){Array.new}\n(n - 1).times do\n u, v = ri\n edges[u] << v\n edges[v] << u\nend\n\nans = 0\nque = [[1, cats[1]]]\nuntil que.empty?\n u, s = que.shift\n ans += 1 if edges[u].empty?\n edges[u].each do |v|\n edges[v].delete(u)\n ns = cats[v] == 1 ? s + 1 : 0\n que << [v, ns] if ns <= m\n end\nend\nputs ans\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\ncats = [nil] + gets.split.map(&:to_i)\nedges = Array.new(n + 1){Array.new}\n(n - 1).times do\n u, v = gets.split.map(&:to_i)\n edges[u] << v\n edges[v] << u\nend\n\nans = 0\nque = [[1, cats[1]]]\nuntil que.empty?\n u, s = que.shift\n ans += 1 if edges[u].empty?\n edges[u].each do |v|\n edges[v].delete(u)\n ns = cats[v] == 1 ? s + 1 : 0\n que << [v, ns] if ns <= m\n end\nend\nputs ans\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\ncats = [nil] + gets.split.map(&:to_i)\nedges = Array.new(n + 1){Array.new}\n(n - 1).times do\n u, v = gets.split.map(&:to_i)\n edges[u] << v\n edges[v] << u\nend\ncon_cats = Array.new(n + 1)\n\nque = [[1, 0]]\nans = 0\nloop do\n ind, accum = que[0]\n break unless ind\n que.shift\n next if con_cats[ind]\n if cats[ind] == 1\n accum += 1\n else\n accum = 0\n end\n con_cats[ind] = accum\n if accum <= m\n ans += 1 if edges[ind].size == 1 && ind != 1\n que.push(*edges[ind].zip([accum] * edges[ind].size))\n end\nend\nputs ans\n"}, {"source_code": "def ri; gets.split.map(&:to_i); end\nn, m = ri\ncats = [nil] + ri\nedges = Array.new(n + 1){Array.new}\n(n - 1).times do\n u, v = ri\n edges[u] << v\n edges[v] << u\nend\n\nans = 0\nque = [[1, cats[1]]]\nuntil que.empty?\n u, s = que.shift\n ans += 1 if edges[u].empty?\n edges[u].each do |v|\n edges[v].delete(u)\n ns = cats[v] == 1 ? s + 1 : 0\n que << [v, ns] if ns <= m\n end\nend\nputs ans\n\n"}, {"source_code": "n, m = gets.split(' ').map(&:to_i)\ncats = gets.split(' ').map(&:to_i)\ncats.unshift 0\n\nvert = Hash.new { |h, k| h[k] = [] }\n\n1.upto(n - 1) do\n v1, v2 = gets.split(' ').map(&:to_i)\n vert[v1] << v2\n vert[v2] << v1\nend\n\nque = [prev: 0, curr: 1, cats: cats[1], failed: 1 > m]\n\nresult = 0\n\nwhile !que.empty?\n curr = que.shift\n\n any = false\n\n vert[curr[:curr]].each do |v|\n next if curr[:prev] == v\n\n any = true\n\n que << {\n curr: v,\n cats: (curr[:cats] + cats[v]) * cats[v],\n prev: curr[:curr],\n failed: curr[:failed] || (curr[:cats] + cats[v]) * cats[v] > m\n }\n end\n\n result += 1 unless any || curr[:failed]\nend\n\nputs result\n"}, {"source_code": "def ri; gets.split.map(&:to_i); end\nn, m = ri\ncats = [nil] + ri\nedges = Array.new(n + 1){Array.new}\n(n - 1).times do\n u, v = ri\n edges[u] << v\n edges[v] << u\nend\n\nans = 0\nque = [[1, cats[1]]]\nuntil que.empty?\n u, s = que.shift\n ans += 1 if edges[u].empty?\n edges[u].each do |v|\n edges[v].delete(u)\n ns = cats[v] == 1 ? s + 1 : 0\n que << [v, ns] if ns <= m\n end\nend\nputs ans\n\n"}, {"source_code": "n=gets.split(' ').map(&:to_i)\nm=n[1]\nn=n[0]\na=gets.split(' ').map(&:to_i)\ng=Array.new(n+1)\n(1).upto(n) do |i|\n g[i]=[]\nend\n(1).upto(n-1) do\n x=gets.split(' ').map(&:to_i)\n g[x[0]].push(x[1])\n g[x[1]].push(x[0])\nend\nq=Queue.new\nq.push([1,0])\nans=0\nvis = Array.new(n+1,false)\nwhile !(q.empty?)\n cur = q.pop\n node = cur[0]\n cats = cur[1]\n cats=0 if a[node-1]==0\n cats+=1 if a[node-1]==1\n next if cats==m+1\n ans+=1 if g[node].length==1 and node!=1\n g[node].each do |next_node|\n next if vis[next_node]\n vis[next_node]=true\n q.push([next_node,cats])\n end\nend\nputs ans\n"}, {"source_code": "def ri; gets.split.map(&:to_i); end\nn, m = ri\ncats = [nil] + ri\nedges = Array.new(n + 1){Array.new}\n(n - 1).times do\n u, v = ri\n edges[u] << v\n edges[v] << u\nend\n\nans = 0\nque = [[1, cats[1]]]\nuntil que.empty?\n u, s = que.shift\n ans += 1 if edges[u].empty?\n edges[u].each do |v|\n edges[v].delete(u)\n ns = cats[v] == 1 ? s + 1 : 0\n que << [v, ns] if ns <= m\n end\nend\nputs ans\n\n"}], "negative_code": [{"source_code": "def get_i_arr\n return gets.chomp.split.map{|e| e.to_i}\nend\nnm = get_i_arr\nn = nm[0]; m = nm[1]\na = get_i_arr\nadj = Array.new(n)\n(n - 1).times do\n xy = get_i_arr\n x = xy[0] - 1; y = xy[1] - 1\n if nil == adj[x]\n adj[x] = [y]\n else\n adj[x] << y\n end\n if nil == adj[y]\n adj[y] = [x]\n else\n adj[y] << x\n end\nend\n# DFS\nstack = [0]\nused = Array.new(n, false)\nlast = Array.new(n, -1)\ncats = Array.new(n, 0)\ncur_cats = cats[0] = a[0]\nwhile (!stack.empty?)\n v = stack.last\n used[v] = true\n l = last[v] + 1\n if l < adj[v].length && used[adj[v][l]]\n l += 1\n end\n if l < adj[v].length\n to = adj[v][l]\n if 1 == a[to] && 0 == a[v]\n new_cur_cats = 1\n else\n new_cur_cats = cur_cats + a[to]\n end\n if new_cur_cats <= m\n stack << to\n cur_cats = new_cur_cats\n cats[to] = [cur_cats, cats[v]].max\n end\n else\n prev = stack.length > 1 ? stack[stack.length - 2] : -1\n if prev == -1\n cur_cats = 0\n else\n cur_cats = cats[prev]\n end\n stack.pop\n end\n last[v] = l\nend\ngood_leaves = 0\nfor v in 0..n-1\n if adj[v].length == 1 && used[v] && v != 0\n good_leaves += 1\n end\nend\nputs good_leaves\n"}, {"source_code": "def getai; return gets.split.map{|e| e.to_i}; end\nnm = getai\nn = nm[0]; m = nm[1]\na = getai\nadj = Array.new(n)\n(n - 1).times do\n xy = getai\n x = xy[0] - 1; y = xy[1] - 1\n if nil == adj[x] then adj[x] = [y]\n else adj[x] << y end\n if nil == adj[y] then adj[y] = [x]\n else adj[y] << x end\nend\nstack = [0]\nused = Array.new(n, false)\nlast = Array.new(n, -1)\ncats = Array.new(n, 0)\ncur_cats = cats[0] = a[0]\nwhile (!stack.empty?) # DFS\n v = stack.last\n used[v] = true\n l = last[v] + 1\n if l < adj[v].length && used[adj[v][l]]\n l += 1\n end\n if l < adj[v].length\n to = adj[v][l]\n if 1 == a[v] && 0 == a[to]\n new_cats = 0\n else\n new_cats = cur_cats + a[to]\n end\n cats[to] = [new_cats, cats[v]].max\n if cats[to] <= m\n stack << to\n cur_cats = new_cats\n end\n else\n if stack.length > 1\n prev = stack[stack.length - 2]\n if 0 == a[v] && 1 == a[prev]\n cur_cats = cats[prev]\n else\n cur_cats -= a[v]\n end\n else \n cur_cats = 0\n end\n stack.pop\n end\n last[v] = l\nend\ngood_leaves = 0\nfor v in 0..n-1\n if adj[v].length == 1 && used[v] && v != 0\n good_leaves += 1\n end\nend\nputs good_leaves\n"}, {"source_code": "n,m=gets.split.map{|e| e.to_i}\nx=gets.split.map{|e| e.to_i}\narr=[]\nn.times{\n\tarr<<[]\n}\n(n-1).times{\n\ta,b=gets.split.map{|e| e.to_i-1}\n\tarr[a]<1 && arr[e[0]].size>e[1] then\n\t\tp1=arr[e[0]][e[1]]\n\t\tstc.last[1]+=1\n\t\ts1=stc.size\n\t\tif s1>1 && stc[s1-2][0]==p1 then\n\t\t\tnext\n\t\tend\n\t\tadd=x[p1]\n\t\tt1=0\n\t\tif add==1 then\n\t\t\tt1=e[2]+add\n\t\tend\n\t\tif t1<=m then\n\t\t\tstc<<[p1,0,t1]\n\t\tend\n\telse\n\t\tif e[0]==0 then\n\t\t\tbreak\n\t\telsif arr[e[0]].size==1 then\n\t\t\tif e[2]<=m then\n\t\t\t\tans+=1\n\t\t\tend\n\t\tend\n\t\tstc.pop\n\tend\n\t\nend\nputs ans\n\n"}, {"source_code": "n,m=gets.split.map{|e| e.to_i}\nm-=1\nx=gets.split.map{|e| e.to_i}\narr=[]\nn.times{\n\tarr<<[]\n}\n(n-1).times{\n\ta,b=gets.split.map{|e| e.to_i-1}\n\tarr[a]<e[1] then\n\t\ts1=stc.size\n\t\tp1=arr[e[0]][e[1]]\n\t\tstc.last[1]+=1\n\t\tif s1>1 && stc[s1-2][0]==p1 then\n\t\t\tnext\n\t\tend\n\t\tstc<<[p1,0,0]\n\telse\n\t\tstc.pop\n\t\tif e[0]==m then\n\t\t\tputs e[2]+1\n\t\t\tbreak\n\t\telse\n\t\t\tif x[e[0]]==1 then\n\t\t\t\tstc.last[2]+=e[2]+1\n\t\t\tend\n\t\tend\n\tend\nend\n\n"}, {"source_code": "n,m=gets.split.map{|e| e.to_i}\nx=gets.split.map{|e| e.to_i}\narr=[]\nn.times{\n\tarr<<[]\n}\n(n-1).times{\n\ta,b=gets.split.map{|e| e.to_i-1}\n\tarr[a]<1 && arr[e[0]].size>e[1] then\n\t\tp1=arr[e[0]][e[1]]\n\t\tstc.last[1]+=1\n\t\ts1=stc.size\n\t\tif s1>1 && stc[s1-2][0]==p1 then\n\t\t\tnext\n\t\tend\n\t\tadd=x[e[0]]\n\t\tadd=0 if e[0]==0\n\t\tstc<<[p1,0,add+e[2]]\n\telse\n\t\tif e[0]==0 then\n\t\t\tbreak\n\t\telsif arr[e[0]].size==1 then\n\t\t\tif e[2] #{j}\"\n if cats[j] == 1\n cats_on_way += cats[j]\n else\n cats_on_way = 0 # reset, because not consecutive.\n end\n if cats_on_way > max_consecutives\n max_consecutives = cats_on_way # updates\n end\n break if j == 1\n # p \"> #{j} -> #{rvertices[j][0].v}\"\n j = rvertices[j][0].v\n end\n\n if max_consecutives <= m\n reachables << i\n end\n}\n\n# p reachables\nputs reachables.length # print reachables\n"}, {"source_code": "# coding: utf-8\n\nrequire \"set\"\n\n# if __FILE__ == $0\n# end\n\nn, m = gets.split.map(&:to_i)\ncats = [nil] + gets.split.map(&:to_i) # 1-based vertices\nrvertices = {}\nleaves = Set.new\n(n-1).times {\n u, v = gets.split.map(&:to_i) \n leaves.add(v)\n leaves.delete(u)\n rvertices[u] ||= [] # initialize u with empty array\n rvertices[v] ||= []\n rvertices[v] << u\n\n}\n\n# Remember, rooted tree\n#p rvertices\nreachables = []\n\nleaves.each { |i|\n if i == 1 # root node\n next\n end\n j = i\n cat_count = 0\n longest_cat_seq = 0\n loop do\n if cats[j] == 0\n if longest_cat_seq < cat_count\n longest_cat_seq = cat_count\n cat_count = 0\n end\n else\n cat_count += cats[j]\n end\n\n if rvertices[j].empty?\n if longest_cat_seq < cat_count\n longest_cat_seq = cat_count\n end\n break\n else\n j = rvertices[j][0]\n end\n end\n\n if longest_cat_seq <= m\n reachables << i\n end\n}\n\np reachables\nputs reachables.length # print reachables\n"}, {"source_code": "# coding: utf-8\n\nrequire \"set\"\n\n# if __FILE__ == $0\n# end\n\nn, m = gets.split.map(&:to_i)\ncats = [nil] + gets.split.map(&:to_i) # 1-based vertices\nvertices = {}\n(n-1).times {\n u, v = gets.split.map(&:to_i)\n\n vertices[v] ||= [] # initialize u with empty array\n vertices[u] ||= []\n vertices[u] << v\n vertices[v] << u\n}\n\n# Remember, rooted tree\n#p vertices\nTRAVERSE_UP = -99\nstack = [1]\ntraverse = []\nreachables = []\nwhile !stack.empty?\n i = stack.pop\n if i == TRAVERSE_UP\n traverse.pop\n next\n else\n traverse.push(i)\n end\n\n if vertices[i].length == 1\n if i == 1\n break\n end\n cat_count = 0\n longest_cats = 0\n traverse.each { |j|\n if cats[j] == 0\n if longest_cats < cat_count\n longest_cats = cat_count\n end\n cat_count = 0\n else\n cat_count += cats[j]\n end\n }\n if longest_cats < cat_count\n longest_cats = cat_count #last update.\n end\n\n #p \"longest_cats #{longest_cats} #{m}\"\n #p stack\n #p traverse\n if longest_cats <= m\n reachables << i\n end\n\n traverse.pop\n else\n stack.push(TRAVERSE_UP)\n for ni in vertices[i]\n unless traverse.include?(ni)\n stack.push(ni)\n end\n end\n end \nend\n\n#p vertices\n#p reachables\nputs reachables.length # print reachables\n"}, {"source_code": "# coding: utf-8\n\nrequire \"set\"\n\n# if __FILE__ == $0\n# end\n\nn, m = gets.split.map(&:to_i)\ncats = [nil] + gets.split.map(&:to_i) # 1-based vertices\nrvertices = {}\nleaves = Set.new\n(n-1).times {\n u, v = gets.split.map(&:to_i) \n leaves.add(v)\n leaves.delete(u)\n rvertices[u] ||= [] # initialize u with empty array\n rvertices[v] ||= []\n rvertices[v] << u\n\n}\n\n# Remember, rooted tree\n#p rvertices\nreachables = []\n\nleaves.each { |i|\n if i == 1 # root node\n next\n end\n j = i\n cat_count = 0\n longest_cat_seq = 0\n loop do\n if cats[j] == 0\n if longest_cat_seq < cat_count\n longest_cat_seq = cat_count\n cat_count = 0\n end\n else\n cat_count += cats[j]\n end\n\n if rvertices[j].empty?\n if longest_cat_seq < cat_count\n longest_cat_seq = cat_count\n end\n break\n else\n j = rvertices[j][0]\n end\n end\n\n if longest_cat_seq <= m\n reachables << i\n end\n}\n\n# p reachables\nputs reachables.length # print reachables\n"}, {"source_code": "n, $m = gets.split.map(&:to_i)\n$cats = [nil] + gets.split.map(&:to_i)\n$edges = Array.new(n + 1){Array.new}\n$edges[0] = [1]\n(n - 1).times do\n u, v = gets.split.map(&:to_i)\n $edges[u] << v\nend\n\n$ans = 0\ndef dfs(lv, accum)\n return unless $edges[lv]\n if $cats[lv] == 1\n accum += 1\n elsif !$cats[lv].nil?\n accum = 0\n $ans += 1\n end\n $edges[lv].each{|v| dfs(v, accum)} if accum <= $m\nend\n\ndfs(0, 0)\nputs $ans\n"}, {"source_code": "n, $m = gets.split.map(&:to_i)\n$cats = [nil] + gets.split.map(&:to_i)\n$edges = Array.new(n + 1){Array.new}\n(n - 1).times do\n u, v = gets.split.map(&:to_i)\n $edges[u] << v\n $edges[v] << u\nend\n$con_cats = Array.new(n + 1)\n\n$ans = 0\ndef dfs(lv, accum)\n return if $con_cats[lv]\n if $edges[lv].size == 1\n $ans += 1 if accum + $cats[lv] <= $m\n return\n end\n if $cats[lv] == 1\n accum += 1\n else\n accum = 0\n end\n $con_cats[lv] = accum\n $edges[lv].each{|v| dfs(v, accum)} if accum <= $m\nend\n\ndfs(1, 0)\nputs $ans\n"}, {"source_code": "n, $m = gets.split.map(&:to_i)\n$cats = [nil] + gets.split.map(&:to_i)\n$edges = Array.new(n + 1){Array.new}\n$edges[0] = [1]\n(n - 1).times do\n u, v = gets.split.map(&:to_i).sort\n $edges[u] << v\nend\n\n$ans = 0\ndef dfs(lv, accum)\n if $edges[lv] == []\n $ans += 1 if accum + $cats[lv] <= $m\n return\n end\n if $cats[lv] == 1\n accum += 1\n else\n accum = 0\n end\n $edges[lv].each{|v| dfs(v, accum)} if accum <= $m\nend\n\ndfs(0, 0)\nputs $ans\n"}, {"source_code": "n, $m = gets.split.map(&:to_i)\n$cats = [nil] + gets.split.map(&:to_i)\n$edges = Array.new(n + 1){Array.new}\n$edges[0] = [1]\n(n - 1).times do\n u, v = gets.split.map(&:to_i)\n $edges[u] << v\nend\n\n$ans = 0\ndef dfs(lv, accum)\n if $edges[lv] == []\n $ans += 1 if accum + $cats[lv] <= $m\n return\n end\n if $cats[lv] == 1\n accum += 1\n else\n accum = 0\n end\n $edges[lv].each{|v| dfs(v, accum)} if accum <= $m\nend\n\ndfs(0, 0)\nputs $ans\n"}, {"source_code": "n, m = gets.split(' ').map(&:to_i)\ncats = gets.split(' ').map(&:to_i)\ncats.unshift 0\n\nvert = Hash.new { [] }\n\n1.upto(n - 1) do\n v1, v2 = gets.split(' ').map(&:to_i)\n vert[v1] = vert[v1] + [v2]\n vert[v2] = vert[v2] + [v1]\nend\n\nque = [v: [1], cats: 1, failed: false]\n\nresult = 0\n\nwhile !que.empty?\n curr = que.shift\n vort = curr[:v].last\n\n any = false\n\n vert[vort].each do |v|\n next if curr[:v].include? v\n\n any = true\n\n que << {\n v: curr[:v] + [v],\n cats: (curr[:cats] + cats[v]) * cats[v],\n failed: curr[:failed] || (curr[:cats] + cats[v]) * cats[v] > m\n }\n end\n\n result += 1 unless any || curr[:failed]\nend\n\nputs result\n"}], "src_uid": "875e7048b7a254992b9f62b9365fcf9b"} {"nl": {"description": "Eugeny has array a = a1, a2, ..., an, consisting of n integers. Each integer ai equals to -1, or to 1. Also, he has m queries: Query number i is given as a pair of integers li, ri (1 ≤ li ≤ ri ≤ n). The response to the query will be integer 1, if the elements of array a can be rearranged so as the sum ali + ali + 1 + ... + ari = 0, otherwise the response to the query will be integer 0. Help Eugeny, answer all his queries.", "input_spec": "The first line contains integers n and m (1 ≤ n, m ≤ 2·105). The second line contains n integers a1, a2, ..., an (ai = -1, 1). Next m lines contain Eugene's queries. The i-th line contains integers li, ri (1 ≤ li ≤ ri ≤ n).", "output_spec": "Print m integers — the responses to Eugene's queries in the order they occur in the input.", "sample_inputs": ["2 3\n1 -1\n1 1\n1 2\n2 2", "5 5\n-1 1 1 1 -1\n1 1\n2 3\n3 5\n2 5\n1 5"], "sample_outputs": ["0\n1\n0", "0\n1\n0\n1\n0"], "notes": null}, "positive_code": [{"source_code": "_, a, *q = readlines.map{|i| i.split.map(&:to_i)}\nn = 2 * [a.count(1), a.count(-1)].min\nputs q.map{|i, j|\n j -= i\n j.odd? && j < n ? 1 : 0\n}\n"}, {"source_code": "_, a, *q = readlines.map{|i| i.split.map(&:to_i)}\nn = 2 * [a.count(1), a.count(-1)].min\nputs q.map{|i, j|\n j -= i\n j.odd? && j < n ? 1 : 0\n}\n\n"}, {"source_code": "_, a, *q = readlines.map{|i| i.split.map(&:to_i)}\nn = 2 * [a.count(1), a.count(-1)].min\nputs q.map{|i, j|\n j -= i\n j.odd? && j < n ? 1 : 0\n}\n\n"}, {"source_code": "_, a, *q = readlines.map{|i| i.split.map(&:to_i)}\nn = 2 * [a.count(1), a.count(-1)].min\nputs q.map{|i, j|\n j -= i\n j.odd? && j < n ? 1 : 0\n}\n"}, {"source_code": "n,m=gets.chomp.split.map(&:to_i)\na=gets.chomp.split.map(&:to_i)\naa,aaa=a.count(1),a.count(-1)\nm.times do\n\tl,r=gets.chomp.split.map(&:to_i)\n\tx=a[l-1..r-1]\n\tlen=x.length\n\tputs (len.even? && 2*aa>=len && 2*aaa>=len)? \"1\" : \"0\"\nend"}, {"source_code": "n, m = gets.split(' ').map { |i| i.to_i }\na = b = 0\ngets.split(' ').map {|i|\n i = i.to_i\n a += 1 if i == 1\n b += 1 if i == -1\n}\nm.times do\n l, r = gets.split(' ').map { |i| i.to_i}\n width = r - l + 1\n if width % 2 != 0 or (width / 2 > a or width / 2 > b)\n puts 0\n else\n puts 1\n end\nend"}, {"source_code": "n, m = gets.split(' ').map { |i| i.to_i }\na = b = 0\ngets.split(' ').map {|i| i.to_i == 1 ? a += 1 : b += 1}\nm.times do\n l, r = gets.split(' ').map { |i| i.to_i}\n width = r - l + 1\n puts (width % 2 != 0 or (width / 2 > a or width / 2 > b)) ? 0 : 1\nend"}, {"source_code": "def run\n n, m = $stdin.gets.split.map(&:to_i)\n a = $stdin.gets.split.map(&:to_i)\n\n plus = a.count {|x| x > 0 }\n minus = n - plus\n $stdin.readlines.each do |line|\n l, r = line.split.map(&:to_i)\n len = r - l + 1\n\n if len % 2 == 0 and plus*2 >= len and minus*2 >= len\n puts \"1\"\n else\n puts \"0\"\n end\n end\nend\n\nrun if $0 == __FILE__\n"}, {"source_code": "class A\n def initialize\n n, m = gets.chomp.split(' ').map(&:to_i)\n\n array = gets.chomp.split(' ').map(&:to_i)\n\n query = []\n\n m.times do\n query << gets.chomp.split(' ').map(&:to_i)\n end\n\n mc = array.count(-1)\n pc = array.count(1)\n\n query.each do |q|\n size = q[1] - q[0] + 1\n\n if(size.even? && size > 1 && size/2 <= mc && size/2 <= pc)\n puts 1\n else\n puts 0\n end\n end\n end\nend\n\na = A.new"}, {"source_code": "_, a, *q = readlines.map{|i| i.split.map(&:to_i)}\nn = 2 * [a.count(1), a.count(-1)].min\nputs q.map{|i, j|\n j -= i\n j.odd? && j < n ? 1 : 0\n}\n\n"}, {"source_code": "_, a, *q = readlines.map{|i| i.split.map(&:to_i)}\nn = 2 * [a.count(1), a.count(-1)].min\nputs q.map{|i, j|\n j -= i\n j.odd? && j < n ? 1 : 0\n}\n\n"}, {"source_code": "_, a, *q = readlines.map{|i| i.split.map(&:to_i)}\nn = 2 * [a.count(1), a.count(-1)].min\nputs q.map{|i, j|\n j -= i\n j.odd? && j < n ? 1 : 0\n}"}, {"source_code": "_, a, *q = readlines.map{|i| i.split.map(&:to_i)}\nn = 2 * [a.count(1), a.count(-1)].min\nputs q.map{|i, j|\n j -= i\n j.odd? && j < n ? 1 : 0\n}\n\n"}, {"source_code": "# def query(l,r)\n# ele = r - l + 1\n# return false if ele.odd?\n# val = ele / 2\n# return true if val <= @one_count && val <= @minus_count\n# return false\n# end\n\nn,m = gets.split(\" \").map(&:to_i)\narr = gets.split(\" \").map(&:to_i)\n@one_count = arr.count(1)\n@minus_count = n - @one_count\nans = []\nm.times do\n\tl,r = gets.split(\" \")\n\tl = l.to_i\n\tr = r.to_i\n\tele = r - l + 1\n\tif ele.odd?\n \t\tans << \"0\"\n \t\tnext \n \tend\n \tval = ele / 2\n \tif val <= @one_count && val <= @minus_count\n \t\tans << \"1\"\n \t\tnext \n \tend\n \tans << \"0\"\nend\n\nans.each do |a|\n\tputs a\nend\n\n\n#Good http://codeforces.com/contest/302/problem/A\n# From TLE to 811 ms\n# Appending to array and iterating and displaying result is way faster than puts result directly around 100 ms diff\n# Removed the method calls\n# removed boolean checks\n# Removed the map and did to_i directly for l and r \n# Directly appended string to answer - there will be a to_s call for puts that cost from 889 to 811 the fuck"}, {"source_code": "# def query(l,r)\n# ele = r - l + 1\n# return false if ele.odd?\n# val = ele / 2\n# return true if val <= @one_count && val <= @minus_count\n# return false\n# end\n\nn,m = gets.split(\" \").map(&:to_i)\narr = gets.split(\" \").map(&:to_i)\n@one_count = arr.count(1)\n@minus_count = n - @one_count\nans = []\nm.times do\n\tl,r = gets.split(\" \")\n\tl = l.to_i\n\tr = r.to_i\n\tele = r - l + 1\n\tif ele.odd?\n \t\tputs \"0\"\n \t\tnext \n \tend\n \tval = ele / 2\n \tif val <= @one_count && val <= @minus_count\n \t\tputs \"1\"\n \t\tnext \n \tend\n \tputs \"0\"\nend\n\n# ans.each do |a|\n# \tputs a\n# end\n\n\n#Good http://codeforces.com/contest/302/problem/A\n# From TLE to 811 ms\n# Removed the method calls\n# removed boolean checks\n# Removed the map and did to_i directly for l and r \n# Directly appended string to answer - there will be a to_s call for puts that cost from 889 to 811 the fuck"}, {"source_code": "# def query(l,r)\n# ele = r - l + 1\n# return false if ele.odd?\n# val = ele / 2\n# return true if val <= @one_count && val <= @minus_count\n# return false\n# end\n\nn,m = gets.split(\" \").map(&:to_i)\narr = gets.split(\" \").map(&:to_i)\n@one_count = arr.count(1)\n@minus_count = n - @one_count\nans = []\nm.times do\n\tl,r = gets.split(\" \").map(&:to_i)\n\tele = r - l + 1\n\tif ele.odd?\n \t\tans << false\n \t\tnext \n \tend\n \tval = ele / 2\n \tif val <= @one_count && val <= @minus_count\n \t\tans << true\n \t\tnext \n \tend\n \tans << false\nend\n\nans.each do |a|\n\tputs a ? 1 : 0\nend"}, {"source_code": "# def query(l,r)\n# ele = r - l + 1\n# return false if ele.odd?\n# val = ele / 2\n# return true if val <= @one_count && val <= @minus_count\n# return false\n# end\n\nn,m = gets.split(\" \").map(&:to_i)\narr = gets.split(\" \").map(&:to_i)\n@one_count = arr.count(1)\n@minus_count = n - @one_count\nans = []\nm.times do\n\tl,r = gets.split(\" \")\n\tl = l.to_i\n\tr = r.to_i\n\tele = r - l + 1\n\tif ele.odd?\n \t\tans << \"0\"\n \t\tnext \n \tend\n \tval = ele / 2\n \tif val <= @one_count && val <= @minus_count\n \t\tans << \"1\"\n \t\tnext \n \tend\n \tans << \"0\"\nend\n\nans.each do |a|\n\tputs a\nend"}, {"source_code": "# def query(l,r)\n# ele = r - l + 1\n# return false if ele.odd?\n# val = ele / 2\n# return true if val <= @one_count && val <= @minus_count\n# return false\n# end\n\nn,m = gets.split(\" \").map(&:to_i)\narr = gets.split(\" \").map(&:to_i)\n@one_count = arr.count(1)\n@minus_count = n - @one_count\nans = []\nm.times do\n\tl,r = gets.split(\" \")\n\tl = l.to_i\n\tr = r.to_i\n\tele = r - l + 1\n\tif ele.odd?\n \t\tans << 0\n \t\tnext \n \tend\n \tval = ele / 2\n \tif val <= @one_count && val <= @minus_count\n \t\tans << 1\n \t\tnext \n \tend\n \tans << 0\nend\n\nans.each do |a|\n\tputs a\nend"}, {"source_code": "def query(l,r)\n ele = r - l + 1\n return false if ele.odd?\n val = ele / 2\n return true if val <= @one_count && val <= @minus_count\n return false\nend\n\nn,m = gets.split(\" \").map(&:to_i)\narr = gets.split(\" \").map(&:to_i)\n@one_count = arr.count(1)\n@minus_count = n - @one_count\nans = []\nm.times do\n\tl,r = gets.split(\" \").map(&:to_i)\n\tans << (query(l,r) ? 1 : 0)\nend\nans.each do |a|\n\tputs a\nend"}, {"source_code": "# def query(l,r)\n# ele = r - l + 1\n# return false if ele.odd?\n# val = ele / 2\n# return true if val <= @one_count && val <= @minus_count\n# return false\n# end\n\nn,m = gets.split(\" \").map(&:to_i)\narr = gets.split(\" \").map(&:to_i)\n@one_count = arr.count(1)\n@minus_count = n - @one_count\nans = []\nm.times do\n\tl,r = gets.split(\" \")\n\tl = l.to_i\n\tr = r.to_i\n\tele = r - l + 1\n\tif ele.odd?\n \t\tans << false\n \t\tnext \n \tend\n \tval = ele / 2\n \tif val <= @one_count && val <= @minus_count\n \t\tans << true\n \t\tnext \n \tend\n \tans << false\nend\n\nans.each do |a|\n\tputs a ? 1 : 0\nend"}, {"source_code": "n, m = gets.split.collect{|i| i.to_i}\na = Array.new(3,0)\ngets.split.each{|i| a[i.to_i+1]+=1}\n(1..m).each do\n l, r = gets.split.collect{|i| i.to_i}\n if (r-l+1).odd?\n puts 0\n elsif a[0] < (r-l+1)/2 or a[2] < (r-l+1)/2\n puts 0\n else\n puts 1\n end\nend\n"}, {"source_code": "n,m = gets.strip.split(' ').map {|x| x.to_i}\na = gets.strip.split(' ')\nh = {'1' => 0, '-1' => 0}\na.each do |e| h[e] += 1 end\n\nm.times do\n l, r = gets.strip.split(' ').map {|x| x.to_i}\n if( (l+r)%2 == 0)\n puts 0\n else\n k = ((r-l).abs+1)/2\n puts( (h.values.min >= k) ? 1 : 0 )\n end\nend"}, {"source_code": "n_m = gets.split.map { |i| i.to_i }\nn = n_m[0]\nm = n_m[1]\n\na_count = b_count = 0\n\na = gets.split.map do |i|\n r = i.to_i\n if r == 1\n a_count += 1\n else\n b_count += 1\n end\n r\nend\n\nfor i in 1..m\n from_to = gets.split.map { |i| i.to_i }\n from = from_to[0]\n to = from_to[1]\n len = to - from + 1\n\n if (len % 2 == 0) && a_count >= len / 2 && b_count >= len / 2\n puts 1\n else\n puts 0\n end\nend\n"}, {"source_code": "_, a, *q = readlines.map{|i| i.split.map(&:to_i)}\nn = 2 * [a.count(1), a.count(-1)].min\nputs q.map{|i, j|\n j -= i\n j.odd? && j < n ? 1 : 0\n}\n\n"}, {"source_code": "n, m = gets.chomp.split(' ').collect{|item| item.to_i}\na = gets.chomp.split(' ').collect{|item| item.to_i }\na1 = a.count{|item| item == -1}\na2 = a.length - a1\nfor i in 1..m\n l, r = gets.chomp.split(' ').collect{|item| item.to_i}\n numbers = r - l + 1\n if numbers % 2 == 0\n if a1 >= numbers / 2 and a2 >= numbers / 2\n puts 1\n else \n puts 0\n end\n else\n puts 0\n end\nend"}, {"source_code": "#!/usr/bin/env ruby\n\nn, m = gets.split.map(&:to_i)\nx = gets.split.map(&:to_i)\na, b = x.count(-1), x.count(1)\nm.times do\n\tl, r = gets.split.map(&:to_i)\n\tl = r - l + 1\n\tif l % 2 == 1\n\t\tputs 0\n\telse\n\t\tl = l / 2\n\t\tputs a >= l && b >= l ? 1 : 0\n\tend\nend\n"}, {"source_code": "_, a, *q = readlines.map{|i| i.split.map(&:to_i)}\nn = 2 * [a.count(1), a.count(-1)].min\nputs q.map{|i, j|\n j -= i\n j.odd? && j < n ? 1 : 0\n}\n\n"}, {"source_code": "n,m = gets.split.map{|x| x.to_i}\na = gets.split(' ').map{|x| x.to_i}\np = a.count{|x| x<0}\nq = a.count{|x| x>0}\np += p;q += q;\n# p p,q\nm.times{ \n\t(l,r) = gets.split.map{|x| x.to_i}\n\tl = r-l+1\n\tif (l&1)==1\n\t\tputs 0\n\telsif l>p\n\t\tputs 0\n\telsif l>q\n\t\tputs 0\n\telse\n\t\tputs 1\n\tend\n}\n"}, {"source_code": "_, a, *q = readlines.map{|i| i.split.map(&:to_i)}\nn = 2 * [a.count(1), a.count(-1)].min\nputs q.map{|i, j|\n j -= i\n j.odd? && j < n ? 1 : 0\n}\n\n"}], "negative_code": [{"source_code": "puts readlines[2..-1].map{|i| i.split.map(&:to_i).inject(&:-) % 2}\n"}, {"source_code": "_, a, *q = readlines.map{|i| i.split.map(&:to_i)}\nn = 2 * [a.count(1), a.count(-1)].min\nputs q.map{|i, j|\n j -= i\n j.even? && j <= n ? 1 : 0\n}\n"}, {"source_code": "n,m=gets.chomp.split.map(&:to_i)\na=gets.chomp.split.map(&:to_i)\naa,aaa=a.count(1),a.count(-1)\nm.times do\n\tl,r=gets.chomp.split.map(&:to_i)\n\tx=a[l-1..r-1]\n\tlen=x.length\n\tputs(len.even? && 2*aa>=len && 2*aaa>=x.length)? \"1\" : \"0\"\nend"}, {"source_code": "n,m=gets.chomp.split.map(&:to_i)\na=gets.chomp.split.map(&:to_i)\nm.times do\n\tl,r=gets.chomp.split.map(&:to_i)\n\tx=a[l-1..r-1]\n\tputs (x.length.even? && 2*a.count(1)>=x.length && 2*a.count(1)>=x.length)? \"1\" : \"0\"\nend"}, {"source_code": "n,m=gets.chomp.split.map(&:to_i)\na=gets.chomp.split.map(&:to_i)\nm.times do\n\tl,r=gets.chomp.split.map(&:to_i)\n\tx=a[l-1..r-1]\n\tputs (x.length.even? && 2*x.count(1)>=x.length && 2*x.count(1)>=x.length)? \"1\" : \"0\"\nend"}, {"source_code": "class A\n def initialize\n n, m = gets.chomp.split(' ').map(&:to_i)\n\n array = gets.chomp.split(' ').map(&:to_i)\n\n query = []\n\n m.times do\n query << gets.chomp.split(' ').map(&:to_i)\n end\n\n query.each do |q|\n if array[(q[0]-1)..(q[1]-1)].inject(:+) == 0\n puts 1\n else\n puts 0\n end\n end\n end\nend\n\na = A.new"}, {"source_code": "\n\nn,m = gets.strip.split(' ').map {|x| x.to_i}\na = gets.strip.split(' ')\nh = Hash.new 0\na.each do |e|\n h[e] += 1\nend\np h\n\n(0...m).to_a.map do\n l, r = gets.strip.split(' ').map {|x| x.to_i}\n if( (l+r)%2 == 0)\n puts 0\n else\n k = ((r-l).abs+1)/2\n puts( (h.values.min >= k) ? 1 : 0 )\n end\nend"}, {"source_code": "\n\nn,m = gets.strip.split(' ').map {|x| x.to_i}\na = gets.strip.split(' ')\nh = Hash.new 0\na.each do |e|\n h[e] += 1\nend\n\n(0...m).to_a.map do\n l, r = gets.strip.split(' ').map {|x| x.to_i}\n if( (l+r)%2 == 0)\n puts 0\n else\n k = ((r-l).abs+1)/2\n puts( (h.values.min >= k) ? 1 : 0 )\n end\nend"}, {"source_code": "\n\nn,m = gets.strip.split(' ').map {|x| x.to_i}\na = gets.strip.split(' ')\nh = Hash.new 0\na.each do |e|\n h[e] += 1\nend\n\n(0...m).to_a.map do\n l, r = gets.strip.split(' ').map {|x| x.to_i}\n if( (l+r)%2 == 0)\n puts 0\n else\n k = (r-l+1)/2\n puts( (h.values.min >= k) ? 1 : 0 )\n end\nend"}, {"source_code": "n_m = gets.split.map { |i| i.to_i }\nn = n_m[0]\nm = n_m[1]\n\na = gets.split.map { |i| i.to_i }\n\nfor i in 1..m\n from_to = gets.split.map { |i| i.to_i }\n from = from_to[0]\n to = from_to[1]\n\n sum = 0\n for j in from..to\n sum += a[j-1]\n end\n if sum == 0\n puts 1\n else\n puts 0\n end\nend\n"}, {"source_code": "n, m = gets.chomp.split(' ').collect{|item| item.to_i}\na = gets.chomp.split(' ').collect{|item| item.to_i }\na1 = a.count{|item| item == -1}\na2 = a.length - a1\nputs m\nfor i in 1..m\n l, r = gets.chomp.split(' ').collect{|item| item.to_i}\n numbers = r - l + 1\n if numbers % 2 == 0\n if a1 >= numbers / 2 and a2 >= numbers / 2\n puts 1\n else \n puts 0\n end\n else\n puts 0\n end\nend"}, {"source_code": "n, m = gets.chomp.split(' ').collect{|item| item.to_i}\na = gets.chomp.split(' ').collect{|item| item.to_i }\na1 = a.count{|item| item == -1}\na2 = a.length - a1\nfor i in 1..m\n l, r = gets.chomp.split(' ').collect{|item| item.to_i}\n numbers = r - l + 1\n if numbers % 2 == 0\n puts 1 if a1 >= numbers / 2 and a2 >= numbers / 2\n else\n puts 0\n end\nend"}], "src_uid": "deeb49969ac4bc4f1c7d76b89ac1402f"} {"nl": {"description": "Last year Bob earned by selling memory sticks. During each of n days of his work one of the two following events took place: A customer came to Bob and asked to sell him a 2x MB memory stick. If Bob had such a stick, he sold it and got 2x berllars. Bob won some programming competition and got a 2x MB memory stick as a prize. Bob could choose whether to present this memory stick to one of his friends, or keep it. Bob never kept more than one memory stick, as he feared to mix up their capacities, and deceive a customer unintentionally. It is also known that for each memory stick capacity there was at most one customer, who wanted to buy that memory stick. Now, knowing all the customers' demands and all the prizes won at programming competitions during the last n days, Bob wants to know, how much money he could have earned, if he had acted optimally.", "input_spec": "The first input line contains number n (1 ≤ n ≤ 5000) — amount of Bob's working days. The following n lines contain the description of the days. Line sell x stands for a day when a customer came to Bob to buy a 2x MB memory stick (0 ≤ x ≤ 2000). It's guaranteed that for each x there is not more than one line sell x. Line win x stands for a day when Bob won a 2x MB memory stick (0 ≤ x ≤ 2000).", "output_spec": "Output the maximum possible earnings for Bob in berllars, that he would have had if he had known all the events beforehand. Don't forget, please, that Bob can't keep more than one memory stick at a time.", "sample_inputs": ["7\nwin 10\nwin 5\nwin 3\nsell 5\nsell 3\nwin 10\nsell 10", "3\nwin 5\nsell 6\nsell 4"], "sample_outputs": ["1056", "0"], "notes": null}, "positive_code": [{"source_code": "def read\n\ta, b = gets.split\n\ta == 'win' ? b.to_i : -1 - b.to_i\nend\n\nn = gets.to_i\nn.times{$* << read}\nans = 0\n$*.reject{|i| i >= 0}.sort.each do |x|\n\tnext unless b = $*.index(x)\n\tnext unless a = $*[0 .. b].rindex(-1 - x)\n\tnext unless $*[a..b].all?\t# WA5\n\tans += 2 ** (-1 - x)\n\t$*[a..b] = [nil]\nend\np ans\n"}, {"source_code": "n = gets.to_i\nx = Array.new(n, -1)\ndp = Array.new(n, 0)\n(0 .. n - 1).each do |i|\n\ta, b = gets.split\n\tb = b.to_i\n\tdp[i] = dp[i - 1] if i > 0\n\tif a == 'win' then\n\t\tx[i] = b\n\telse\n\t\t(0 .. i - 1).each do |j|\n\t\t\tdp[i] = [dp[i], dp[j] + (1 << b)].max if x[j] == b\n\t\tend\n\tend\nend\np dp[-1]\n\n__END__\ndef read\n\ta, b = gets.split\n\ta == 'win' ? b.to_i : -1 - b.to_i\nend\n\nn, s, a = gets.to_i, 0, []\nn.times{a << read}\na.reject{|i| i >= 0}.sort.each do |x|\n\tnext unless t = a.index(x)\n\tnext unless f = a[0 .. t].rindex(-1 - x)\n\ts += 1 << -1 - x\n\ta.slice!(f .. t)\nend\np s\n\nWA5: test data error?\n"}, {"source_code": "dp = [0];\nfor i in 1..2001\n\tdp[i] = -1\nend\n\nn = gets.to_i\n\nfor i in 1..n\n\tt, x = gets.split(' ')\n\tx = x.to_i + 1\n\tif t == 'sell' && dp[x] >= 0\n\t\tdp[0] = [dp[0], dp[x] + 2**(x-1)].max\n\telse\n\t\tdp[x] = dp[0]\n\tend\nend\n\nputs dp[0]"}, {"source_code": "dp = [0]\nfor i in 1..2001\n dp[i] = -1\nend\nn = STDIN.gets.to_i\n\nfor i in 1..n\n data = STDIN.gets.split(' ')\n s = data[0]\n t = data[1].to_i + 1\n if s == \"sell\"\n if dp[t] >= 0 && dp[t] + 2 ** (t-1) > dp[0]\n dp[0] = dp[t] + 2 ** (t-1)\n end\n else\n dp[t] = dp[0]\n end\nend\n\nputs dp[0]\n"}], "negative_code": [{"source_code": "def read\n\ta, b = gets.split\n\ta == 'win' ? b.to_i : -1 - b.to_i\nend\n\nn, s, a = gets.to_i, 10000000000000000000, []\nn.times{a << read}\na.reject{|i| i >= 0}.sort.each do |x|\n\tnext unless t = a.index(x)\n\tnext unless f = a[0 .. t].rindex(-1 - x)\n\ts += 1 << -1 - x\n\ta.slice!(f .. t)\nend\np s - 10000000000000000000\n"}, {"source_code": "def read\n a, b = gets.split\n a == 'win' ? b.to_i : -1 - b.to_i\nend\n\nn, s, a = gets.to_i, 0, []\nn.times{a << read}\ng = a.reject{|i| i >= 0}.sort\ng.each do |x|\n next if (t = a.index(x)).nil?\n next if (f = a[0 .. t].rindex(-1 - x)).nil?\n s += 1 << -1 - x\n a.slice!(f .. t)\nend\np s\n"}, {"source_code": "def read\n\ta, b = gets.split\n\ta == 'win' ? b.to_i : -1 - b.to_i\nend\n\nn = gets.to_i\nn.times{$* << read}\nans = 0\n$*.reject{|i| i >= 0}.sort.each do |x|\n\tnext unless b = $*.index(x)\n\tnext unless a = $*[0 .. b].rindex(-1 - x)\n\tans += 2 ** (-1 - x)\n\t$*.slice!(a, b - a)\nend\np ans\n"}, {"source_code": "def read\n a, b = gets.split\n a == 'win' ? b.to_i : -1 - b.to_i\nend\n\nn, s, a = gets.to_i, 0, []\nn.times{a << read}\nt = a.reject{|i| i >= 0}.sort\nt.each do |x|\n next if (t = a.index(x)).nil?\n next if (f = a[0 .. t].rindex(-1 - x)).nil?\n s += 1 << -1 - x\n a.slice!(f .. t)\nend\np s\n"}, {"source_code": "def read\n a, b = gets.split\n a == 'win' ? -1 - b.to_i : b.to_i\nend\n\nn, s, a = gets.to_i, 0, []\nn.times{a << read}\na.reject{|i| i < 0}.sort.reverse.each do |x|\n next unless t = a.rindex(x)\n next unless f = a[0 .. t].rindex(-1 - x)\n s += 1 << x\n a.slice!(f .. t)\nend\np s\n"}, {"source_code": "def read\n a, b = gets.split\n a == 'win' ? b.to_i : -1 - b.to_i\nend\n\nn, s, a = gets.to_i, 0, []\nn.times{a << read}\na.reject{|i| i >= 0}.sort.each do |x|\n next unless t = a.index(x)\n raise Exception.new() if a.index(x) != a.rindex(x)\n next unless f = a[0 .. t].rindex(-1 - x)\n s += 1 << -1 - x\n a.slice!(f .. t)\nend\np s\n"}, {"source_code": "def read\n a, b = gets.split\n a == 'win' ? b.to_i : -1 - b.to_i\nend\n\nn, s, a = gets.to_i, 0, []\nn.times{a << read}\na.reject{|i| i >= 0}.sort.each do |x|\n next if (t = a.index(x)).nil?\n next if (f = a[0 .. t].rindex(-1 - x)).nil?\n s += 1 << -1 - x\n a.slice!(f .. t)\nend\np s\n"}, {"source_code": "def read\n\ta, b = gets.split\n\ta == 'win' ? b.to_i : -1 - b.to_i\nend\n\nn = gets.to_i\na = []\nn.times{a << read}\ns = 0\na.reject{|i| i >= 0}.sort.each do |x|\n\tnext unless t = a.index(x)\n\tnext unless f = a[0 .. t].rindex(-1 - x)\n\ts += 2 ** (-1 - x)\n\ta.slice!(f .. t)\nend\nputs s\n"}, {"source_code": "def read\n a, b = gets.split\n a == 'win' ? b.to_i : -1 - b.to_i\nend\n\nn, s, a = gets.to_i, 0, []\nn.times{a << read}\nraise Exception.new() if a.reject{|i| i >= 0}.sort.uniq!\na.reject{|i| i >= 0}.sort.each do |x|\n next unless t = a.index(x)\n next unless f = a[0 .. t].rindex(-1 - x)\n s += 1 << -1 - x\n a.slice!(f .. t)\nend\np s\n"}, {"source_code": "def read\n a, b = gets.split\n a == 'win' ? -1 - b.to_i : b.to_i\nend\n\nn, s, a = gets.to_i, 0, []\nn.times{a << read}\na.reject{|i| i < 0}.sort.each do |x|\n next unless t = a.index(x)\n next unless f = a[0 .. t].rindex(-1 - x)\n s += 1 << x\n a.slice!(f .. t)\nend\np s\n"}, {"source_code": "def read\n a, b = gets.split\n a == 'win' ? -1 - b.to_i : b.to_i\nend\n\nn, s, a = gets.to_i, 0, []\nn.times{a << read}\na.reject{|i| i < 0}.sort.reverse.each do |x|\n next unless t = a.index(x)\n next unless f = a[0 .. t].rindex(-1 - x)\n s += 1 << x\n a.slice!(f .. t)\nend\np s\n"}, {"source_code": "def read\n\ta, b = gets.split\n\ta == 'win' ? b.to_i : -1 - b.to_i\nend\n\nn = gets.to_i\nn.times{$* << read}\nans = 0\n$*.reject{|i| i >= 0}.sort.each do |x|\n\tnext unless b = $*.index(x)\n\tnext unless a = $*[0 .. b].rindex(-1 - x)\n\tans += 2 ** (-1 - x)\n\t$*.slice!(a .. b)\nend\np ans\n"}, {"source_code": "def read\n a, b = gets.split\n a == 'win' ? b.to_i : -1 - b.to_i\nend\n\nn, s, a = gets.to_i, 0, []\nn.times{a << read}\na.reject{|i| i >= 0}.sort.each do |x|\n next unless t = a.index(x)\n next unless f = a[0 .. t].rindex(-1 - x)\n s += 1 << -1 - x\n a[f .. t] = nil\nend\np s\n"}], "src_uid": "217a3a48b927213f4d5e0a19048e2a32"} {"nl": {"description": "ZS the Coder is coding on a crazy computer. If you don't type in a word for a c consecutive seconds, everything you typed disappear! More formally, if you typed a word at second a and then the next word at second b, then if b - a ≤ c, just the new word is appended to other words on the screen. If b - a > c, then everything on the screen disappears and after that the word you have typed appears on the screen.For example, if c = 5 and you typed words at seconds 1, 3, 8, 14, 19, 20 then at the second 8 there will be 3 words on the screen. After that, everything disappears at the second 13 because nothing was typed. At the seconds 14 and 19 another two words are typed, and finally, at the second 20, one more word is typed, and a total of 3 words remain on the screen.You're given the times when ZS the Coder typed the words. Determine how many words remain on the screen after he finished typing everything.", "input_spec": "The first line contains two integers n and c (1 ≤ n ≤ 100 000, 1 ≤ c ≤ 109) — the number of words ZS the Coder typed and the crazy computer delay respectively. The next line contains n integers t1, t2, ..., tn (1 ≤ t1 < t2 < ... < tn ≤ 109), where ti denotes the second when ZS the Coder typed the i-th word.", "output_spec": "Print a single positive integer, the number of words that remain on the screen after all n words was typed, in other words, at the second tn.", "sample_inputs": ["6 5\n1 3 8 14 19 20", "6 1\n1 3 5 7 9 10"], "sample_outputs": ["3", "2"], "notes": "NoteThe first sample is already explained in the problem statement.For the second sample, after typing the first word at the second 1, it disappears because the next word is typed at the second 3 and 3 - 1 > 1. Similarly, only 1 word will remain at the second 9. Then, a word is typed at the second 10, so there will be two words on the screen, as the old word won't disappear because 10 - 9 ≤ 1."}, "positive_code": [{"source_code": "a,b=gets.split.map &:to_i\nk=1\nm=gets.split\na.times{|x|\nm[a-x-1].to_i-m[a-x-2].to_i>b ? break : k+=1}\n$><<[k,a].min"}, {"source_code": "n, c = gets.split.map &:to_i\na = gets.split.map &:to_i\nans = 1\n(1...n).each do |i|\n if a[i] - a[i - 1] > c\n ans = 1\n else\n ans += 1\n end\nend\nputs ans"}, {"source_code": "#!/usr/bin/ruby\nn,c,*a=$<.read.split.map(&:to_i)\nr=1\na.each_cons(2){|x,y|\n\tif y-x>c\n\t\tr=1\n\telse\n\t\tr+=1\n\tend\n}\np r"}, {"source_code": "a,b=gets.split.map &:to_i\nk=1\nm=gets.split\na.times{|x|\nm[a-x-1].to_i-m[a-x-2].to_i>b ? break : k+=1}\n$><<[k,a].min"}, {"source_code": "n,c = gets.split.map(&:to_i)\nmas = gets.split.map(&:to_i)\n\nans = 1\nfor i in 1...mas.size() do\n if(mas[i]-mas[i-1] <= c)\n ans+=1;\n else\n ans = 1;\n end\nend\n\nputs ans;\n"}, {"source_code": "n, c = gets.chomp.split.map(&:to_i)\na = gets.chomp.split.map(&:to_i).reverse\n\nlast = a.first\ncount = 0\na.each do |d|\n if last - d <= c\n count += 1\n last = d\n else\n break\n end\nend\nputs count\n"}, {"source_code": "\nc = gets.chop.split(' ').map{ |x| x.to_i }[1]\n\nt = gets.chop.split(' ').map{ |x| x.to_i }\n\ncount = 1\n\ni = t.size - 1\n\nwhile i != 0\n break if t[i] - c > t[i - 1]\n count += 1\n i -= 1;\nend\n\n\nprint count\n"}, {"source_code": "n, c = gets.split.map{|x| x.to_i}\n\ntimes = gets.split.map{|x| x.to_i}\n\nwords = 0\nprevtime = 0\nfor sec in times\n if (sec - prevtime) > c\n words = 1\n else\n words += 1\n end\n prevtime = sec\nend\n\nputs words\n"}, {"source_code": "class Program\n def initialize(input)\n input.each_with_index do |line, idx|\n case idx\n when 0\n args = line.split(\" \")\n @n = Integer(args[0])\n @c = Integer(args[1])\n when 1\n @arr = line.split(\" \")\n @arr = @arr.map{ |i| i = Integer(i) }\n @arr.reverse!\n puts getScreen()\n #@arr.sort!\n when 2\n \n else\n\n end\n end\n end\n \n def getScreen()\n rtn = 1\n @arr.each_with_index do |e,i|\n if i + 1 < @n\n if e - @arr[i+1] > @c\n rtn = i + 1\n break\n end\n if i + 1 == @n - 1 \n rtn = @n\n end\n end\n end\n return rtn\n end\nend\n\nmain = Program.new(ARGF)"}, {"source_code": "n, c = gets.split.map(&:to_i)\nts = gets.split.map(&:to_i)\n\nans = 1\n(n-1).downto(1) do |i|\n b = ts[i]\n a = ts[i-1]\n if b - a <= c\n ans += 1\n else\n break\n end\nend\nputs ans"}, {"source_code": "n, c = gets.strip.split.map(&:to_i)\nt = gets.strip.split.map(&:to_i)\na = t[0]\nwords = 1\n(1...n).each do |i|\n b = t[i]\n if b - a <= c\n words += 1\n else\n words = 1\n end\n a = b\nend\nputs words\n"}, {"source_code": "n,c=gets.split.map{|e| e.to_i}\nx=gets.split.reverse.map{|e| e.to_i}\nans=1\nwhile x.size>1\n\tif x[0]-x[1]>c then\n\t\tbreak\n\tend\n\tans+=1\n\tx.shift\nend\nputs ans\n\n"}, {"source_code": "n,c = gets.split.map(&:to_i)\nt = gets.split.map(&:to_i)\nr = 1\n(n - 1).downto(1) do |i|\n\tif t[i] - t[i - 1] <= c\n\t\tr += 1\n\telse\n\t\tbreak\n\tend\nend\np r"}, {"source_code": "total, dist = gets.split(' ').map(&:to_i)\nats = gets.split(' ').map(&:to_i)\n\nnext_at = ats.last\ncount = 0\nats.reverse_each do |at|\n if next_at - at <= dist\n count += 1\n else\n break\n end\n next_at = at\nend\n\nputs count\n"}, {"source_code": "n, c=gets.split.map &:to_i\nt=gets.split.map &:to_i\n\nres=0\nx=t[0]\nt.each {|i|\n if i-x<=c\n res+=1\n else\n res=1\n end\n x=i\n}\np res\n"}, {"source_code": "n,c,a=gets.split.map(&:to_i)<<1\ngets.split.map(&:to_i).each_cons(2){|x,y|a=(y-x>c)?1:a+1}\np a"}, {"source_code": "n,c=gets.split.map(&:to_i)\ncnt=1\ngets.split.map(&:to_i).each_cons(2) do|x,y|\n cnt+=1\n cnt=1 if y-x>c\nend\np cnt"}, {"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\n\nn, c = gets.split.map(&:to_i)\nwords = 0\nlast_words_spent = 0\ngets.split.each { |i|\n i = i.to_i\n if (i - last_words_spent) > c\n words = 1\n else\n words += 1\n end\n last_words_spent = i\n}\n\nputs words"}, {"source_code": "# be name khoda ...\ndef read_ints; STDIN.gets.split.map(&:to_i); end\nn, c = read_ints\na = read_ints\nres = 1\ni = 0\n(n-1).times do\n if(a[i]+c < a[i+1])\n res = 1\n else\n res +=1\n end\n i+=1\nend\nputs res"}, {"source_code": "input_values = gets.chomp\nin_val = input_values.split(' ').map(&:to_i)\ntotal_input = in_val[0]\ncons_value = in_val[1]\nstring_input = gets.chomp\narray = string_input.split(' ').map(&:to_i)\ni = 1\ncount = 1\nwhile i < total_input do\n\tif array[i]-array[i-1] <= cons_value\n\t\tcount += 1\n\telse\n\t count = 1\n\tend\n\ti += 1\nend\nputs count"}, {"source_code": "n, c = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\n\nk = 0\np = 0\na.each do |x|\n k += 1\n k = 1 if x-p>c\n p = x\nend\n\nputs k\n"}, {"source_code": "n, c = gets.split.map(&:to_i)\na = gets.split.map(&:to_i)\nans = 1\nn.times do |i|\n if a[i] - a[i - 1] > c || i == 0\n ans = 1\n else\n ans += 1\n end\nend\nputs ans\n\n"}, {"source_code": "n, c = gets.split.map(&:to_i)\nt = gets.split.map(&:to_i)\nans = 1\n1.upto(n - 1){|i|\n if t[i] - t[i - 1] > c\n ans = 1\n else\n ans += 1\n end\n }\np ans"}, {"source_code": "n, c = gets.split.map{|x| x.to_i}\nputs(gets.split.map{|x| x.to_i}.inject([0, 0]){|s, x| [x - s[1] <= c ? s[0] + 1 : 1, x]}[0])\n"}, {"source_code": "typeNum, delay = gets.chomp.split(' ').map(&:to_i)\ninputs = gets.chomp.split(' ').map(&:to_i)\n\nchar = 1\n\nfor i in (1...(inputs.length))\n\tdiff = inputs[i] - inputs[i - 1]\n\tchar = 1 if diff > delay\n\tchar += 1 if diff <= delay\nend\n\nputs char"}], "negative_code": [{"source_code": "a,b=gets.split.map &:to_i\nk=1\nm=gets.split\na.times{|x|\nm[a-x-1].to_i-m[a-x-2].to_i<=b ? k+=1 : break}\n$><= c\n words = 2\n else\n words += 1\n end\n prevtime = sec\nend\n\nputs words\n"}, {"source_code": "class Program\n def initialize(input)\n input.each_with_index do |line, idx|\n case idx\n when 0\n args = line.split(\" \")\n @n = Integer(args[0])\n @c = Integer(args[1])\n when 1\n @arr = line.split(\" \")\n @arr = @arr.map{ |i| i = Integer(i) }\n @arr.reverse!\n puts getScreen()\n #@arr.sort!\n when 2\n \n else\n\n end\n end\n end\n \n def getScreen()\n rtn = 1\n @arr.each_with_index do |e,i|\n if i + 1 < @n\n if e - @arr[i+1] > @c\n rtn = i + 1\n break\n end\n end\n end\n return rtn\n end\nend\n\nmain = Program.new(ARGF)"}, {"source_code": "class Program\n def initialize(input)\n input.each_with_index do |line, idx|\n case idx\n when 0\n args = line.split(\" \")\n @n = Integer(args[0])\n @c = Integer(args[1])\n when 1\n @arr = line.split(\" \")\n @arr = @arr.map{ |i| i = Integer(i) }\n @arr.reverse!\n puts getScreen()\n #@arr.sort!\n when 2\n \n else\n\n end\n end\n end\n \n def getScreen()\n rtn = 1\n @arr.each_with_index do |e,i|\n if i + 1 < @n\n if e - @arr[i+1] > @c\n rtn = i + 1\n break\n end\n if i == @n - 1 \n rtn = @n\n end\n end\n end\n return rtn\n end\nend\n\nmain = Program.new(ARGF)"}, {"source_code": "class Program\n def initialize(input)\n input.each_with_index do |line, idx|\n case idx\n when 0\n args = line.split(\" \")\n @n = Integer(args[0])\n @c = Integer(args[1])\n when 1\n @arr = line.split(\" \")\n @arr = @arr.map{ |i| i = Integer(i) }\n @arr.reverse!\n puts getScreen()\n #@arr.sort!\n when 2\n \n else\n\n end\n end\n end\n \n def getScreen()\n rtn = 0\n @arr.each_with_index do |e,i|\n if i + 1 < @n\n if e - @arr[i+1] > @c\n rtn = i + 1\n break\n end\n end\n end\n return rtn\n end\nend\n\nmain = Program.new(ARGF)"}, {"source_code": "total, dist = gets.split(' ').map(&:to_i)\nats = gets.split(' ').map(&:to_i)\n\nnext_at = ats.last\ncount = 0\nats.reverse_each do |at|\n if next_at - at <= dist\n count += 1\n end\n next_at = at\nend\n\nputs count\n"}, {"source_code": "input_values = gets.chomp\nin_val = input_values.split(' ').map(&:to_i)\ntotal_input = in_val[0]\ncons_value = in_val[1]\nstring_input = gets.chomp\narray = string_input.split(' ').map(&:to_i)\ni = 1\ncount = 0\nwhile i < total_input do\n\tif array[i]-array[i-1] <= cons_value\n\t\tcount += 1\n\telse\n\t count = 1\n\tend\n\ti += 1\nend\nputs count\n\n\n\n"}, {"source_code": "n, c = gets.split.map(&:to_i)\nt = gets.split.map(&:to_i)\nans = 0\ntmp = 1\n1.upto(n - 1){|i|\n if t[i] - t[i - 1] > c\n ans = [ans, tmp].max\n tmp = 1\n else\n tmp += 1\n end\n }\nans = [ans, tmp].max\np ans"}], "src_uid": "fb58bc3be4a7a78bdc001298d35c6b21"} {"nl": {"description": "You are given an array $$$a_1, a_2, \\ldots, a_n$$$ consisting of $$$n$$$ positive integers and a positive integer $$$m$$$.You should divide elements of this array into some arrays. You can order the elements in the new arrays as you want.Let's call an array $$$m$$$-divisible if for each two adjacent numbers in the array (two numbers on the positions $$$i$$$ and $$$i+1$$$ are called adjacent for each $$$i$$$) their sum is divisible by $$$m$$$. An array of one element is $$$m$$$-divisible.Find the smallest number of $$$m$$$-divisible arrays that $$$a_1, a_2, \\ldots, a_n$$$ is possible to divide into.", "input_spec": "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 two integers $$$n$$$, $$$m$$$ $$$(1 \\le n \\le 10^5, 1 \\le m \\le 10^5)$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ $$$(1 \\le a_i \\le 10^9)$$$. It is guaranteed that the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases do not exceed $$$10^5$$$.", "output_spec": "For each test case print the answer to the problem.", "sample_inputs": ["4\n6 4\n2 2 8 6 9 4\n10 8\n1 1 1 5 2 4 4 8 6 7\n1 1\n666\n2 2\n2 4"], "sample_outputs": ["3\n6\n1\n1"], "notes": "NoteIn the first test case we can divide the elements as follows: $$$[4, 8]$$$. It is a $$$4$$$-divisible array because $$$4+8$$$ is divisible by $$$4$$$. $$$[2, 6, 2]$$$. It is a $$$4$$$-divisible array because $$$2+6$$$ and $$$6+2$$$ are divisible by $$$4$$$. $$$[9]$$$. It is a $$$4$$$-divisible array because it consists of one element. "}, "positive_code": [{"source_code": "t = gets.chomp.to_i\r\nfor test in (1..t)\r\n n, m = gets.chomp.split(' ').map { |x| x.to_i }\r\n a = gets.chomp.split(' ').map { |x| x.to_i }\r\n r = Array.new(m).fill(0)\r\n for i in (0..n-1)\r\n r[a[i] % m] += 1\r\n end\r\n ans = 0\r\n for i in (1..m/2)\r\n if r[i] == r[m - i]\r\n if r[i] > 0\r\n ans += 1\r\n end\r\n elsif r[i] == 0 && r[m - i] != 0\r\n ans += r[m - i]\r\n elsif r[i] != 0 && r[m - i] == 0\r\n ans += r[i]\r\n else\r\n ans += 1 + [0, (r[i] - r[m - i]).abs - 1].max\r\n end\r\n end\r\n if r[0] != 0\r\n ans += 1\r\n end\r\n puts ans\r\nend\r\n"}, {"source_code": "require 'set'\n\ndef fn(aa, d)\n out = []\n ret = 0\n for e in aa\n n = e % d\n if (n != 0) \n out << n \n else\n ret = 1\n end\n end\n r = out.tally\n \n for k,v in r\n if k + k == d\n ret += 1\n next\n end\n if v == 0; next; end\n \n j = d - k\n r[k] = 0\n vj = r[j]\n if vj\n r[j] = 0\n if vj >= v+1\n ret += 1\n ret += vj - (v+1)\n # printf(\"j:%d k:%d > ret:%d\\n\",k,j,ret)\n elsif vj == v \n ret += 1\n # printf(\"k:%d j:%d == ret:%d\\n\",k,j,ret)\n else\n ret += 1\n ret += v - (vj+1)\n # printf(\"k:%d j:%d < ret:%d\\n\",k,j,ret)\n end\n else\n ret += v\n # printf(\"j:%d ret:%d\\n\",k,ret);\n end\n\n end\n puts ret\nend\n\ntt = gets.to_i\ntt.times {\n (_, d)= gets.chomp!.split(' ').map &:to_i\n aa = gets.chomp!.split(' ').map &:to_i\n fn(aa,d)\n}\n\n\t \t\t \t \t\t\t\t\t \t\t \t\t \t"}, {"source_code": "gets.to_i.times {\n n, m = gets.split(' ').map(&:to_i)\n xs = gets.split(' ').map(&:to_i)\n\n groups = Array.new(m) { [] }\n xs.each do |el|\n groups[el % m].push(el)\n end\n\n res = 0\n if groups[0].size > 0\n res += 1\n end\n if m % 2 == 0 && groups[m / 2].size > 0\n res += 1\n end\n (1..((m - 1) / 2)).each do |rem|\n min = [groups[rem].size, groups[m - rem].size].min\n max = [groups[rem].size, groups[m - rem].size].max\n if min == 0\n res += max\n elsif min == max\n res += 1\n else\n res += (max - min)\n end\n end\n puts res\n}\n"}], "negative_code": [{"source_code": "t = gets.chomp.to_i\r\nfor test in (1..t)\r\n n, m = gets.chomp.split(' ').map { |x| x.to_i }\r\n a = gets.chomp.split(' ').map { |x| x.to_i }\r\n r = Array.new(m).fill(0)\r\n for i in (0..n-1)\r\n r[a[i] % m] += 1\r\n end\r\n z = 0\r\n for i in (1..(m-1)/2)\r\n y = [r[i], r[m - i]].min * 2 + 1\r\n z += 1 + [0, r[i] + r[m - i] - y].max\r\n end\r\n if r[0] != 0\r\n z += 1\r\n end\r\n if m % 2 == 0 && r[m / 2] != 0\r\n z += 1\r\n end\r\n puts z\r\nend\r\n"}], "src_uid": "d107db1395ded62904d0adff164e5c1e"} {"nl": {"description": "You are given two arrays $$$a$$$ and $$$b$$$ both consisting of $$$n$$$ positive (greater than zero) integers. You are also given an integer $$$k$$$.In one move, you can choose two indices $$$i$$$ and $$$j$$$ ($$$1 \\le i, j \\le n$$$) and swap $$$a_i$$$ and $$$b_j$$$ (i.e. $$$a_i$$$ becomes $$$b_j$$$ and vice versa). Note that $$$i$$$ and $$$j$$$ can be equal or different (in particular, swap $$$a_2$$$ with $$$b_2$$$ or swap $$$a_3$$$ and $$$b_9$$$ both are acceptable moves).Your task is to find the maximum possible sum you can obtain in the array $$$a$$$ if you can do no more than (i.e. at most) $$$k$$$ such moves (swaps).You have to answer $$$t$$$ independent test cases.", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 200$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le n \\le 30; 0 \\le k \\le n$$$) — the number of elements in $$$a$$$ and $$$b$$$ and the maximum number of moves you can do. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 30$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. The third line of the test case contains $$$n$$$ integers $$$b_1, b_2, \\dots, b_n$$$ ($$$1 \\le b_i \\le 30$$$), where $$$b_i$$$ is the $$$i$$$-th element of $$$b$$$.", "output_spec": "For each test case, print the answer — the maximum possible sum you can obtain in the array $$$a$$$ if you can do no more than (i.e. at most) $$$k$$$ swaps.", "sample_inputs": ["5\n2 1\n1 2\n3 4\n5 5\n5 5 6 6 5\n1 2 5 4 3\n5 3\n1 2 3 4 5\n10 9 10 10 9\n4 0\n2 2 4 3\n2 4 2 3\n4 4\n1 2 2 1\n4 4 5 4"], "sample_outputs": ["6\n27\n39\n11\n17"], "notes": "NoteIn the first test case of the example, you can swap $$$a_1 = 1$$$ and $$$b_2 = 4$$$, so $$$a=[4, 2]$$$ and $$$b=[3, 1]$$$.In the second test case of the example, you don't need to swap anything.In the third test case of the example, you can swap $$$a_1 = 1$$$ and $$$b_1 = 10$$$, $$$a_3 = 3$$$ and $$$b_3 = 10$$$ and $$$a_2 = 2$$$ and $$$b_4 = 10$$$, so $$$a=[10, 10, 10, 4, 5]$$$ and $$$b=[1, 9, 3, 2, 9]$$$.In the fourth test case of the example, you cannot swap anything.In the fifth test case of the example, you can swap arrays $$$a$$$ and $$$b$$$, so $$$a=[4, 4, 5, 4]$$$ and $$$b=[1, 2, 2, 1]$$$."}, "positive_code": [{"source_code": "gets.to_i.times do\n\tn, k = gets.split.map(&:to_i)\n\ta = gets.split.map(&:to_i).sort\n\tb = gets.split.map(&:to_i).sort.reverse\n\tputs (0...k).sum { |i| [a[i], b[i]].max } + (k...n).sum { |i| a[i] }\nend\n"}, {"source_code": "t = gets.to_i\nt.times do\n\tn,k = gets.split.map(&:to_i)\n\ta = gets.split.map(&:to_i)\n\tb = gets.split.map(&:to_i)\n\tsum = a.sum\n\ta.sort!\n\tb.sort!.reverse!\n\tfor i in 0...k\n\t\tsum += b[i] - a[i] if b[i] > a[i]\n\tend\n\tputs sum\nend\n"}, {"source_code": "t = gets.chomp.to_i\nt.times do |testcase|\n (n,k) = gets.split.map(&:to_i)\n a = gets.split.map(&:to_i)\n b = gets.split.map(&:to_i)\n a = a.sort;a = a.reverse;b = b.sort\n #print a ; print \"\\n\"\n a_ptr = -1 ; b_ptr = -1\n while k>0\n if b[b_ptr] >= a[a_ptr]\n a[a_ptr] = b[b_ptr]\n a_ptr-=1 ; b_ptr-=1\n end\n k-=1\n end\n res = 0\n a.each do |num|\n res += num\n end\n puts res\nend"}, {"source_code": "1.upto(gets.to_i) do\n n, k = gets.split(' ').map(&:to_i)\n\n a = gets.split(' ').map(&:to_i).sort\n b = gets.split(' ').map(&:to_i).sort.reverse\n\n result = a.inject(&:+)\n \n a[0, k].zip(b[0, k]).each do |x|\n break if x.first >= x.last\n\n result += x.last - x.first\n end\n\n puts result\nend\n"}], "negative_code": [], "src_uid": "7c2337c1575b4a62e062fc9990c0b098"} {"nl": {"description": "You are given two very long integers a, b (leading zeroes are allowed). You should check what number a or b is greater or determine that they are equal.The input size is very large so don't use the reading of symbols one by one. Instead of that use the reading of a whole line or token.As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java. Don't use the function input() in Python2 instead of it use the function raw_input().", "input_spec": "The first line contains a non-negative integer a. The second line contains a non-negative integer b. The numbers a, b may contain leading zeroes. Each of them contains no more than 106 digits.", "output_spec": "Print the symbol \"<\" if a < b and the symbol \">\" if a > b. If the numbers are equal print the symbol \"=\".", "sample_inputs": ["9\n10", "11\n10", "00012345\n12345", "0123\n9", "0123\n111"], "sample_outputs": ["<", ">", "=", ">", ">"], "notes": null}, "positive_code": [{"source_code": "p = readline.chomp.rjust(10**6, \"0\")\n#puts p\nq = readline.chomp.rjust(10**6, \"0\")\n#puts q\nif p == q\n puts(\"=\")\nelsif p < q\n puts(\"<\")\nelse\n puts(\">\")\nend"}, {"source_code": "a,b=$<.map{|e|e.chomp.sub(/^0+/,'')}\nputs '=><'[(a.size<=>b.size)!=0 ? a.size<=>b.size : a<=>b]"}, {"source_code": "a = gets.to_i\nb = gets.to_i\nif a < b\n puts \"<\"\nelsif a > b\n puts \">\"\nelse\n puts \"=\"\nend"}, {"source_code": "a = gets.chomp.rjust(10**6, \"0\")\nb = gets.chomp.rjust(10**6, \"0\")\nputs (a == b ? \"=\" : (a>b ? \">\" : \"<\"))\n"}, {"source_code": "a = gets.to_i\nb = gets.to_i\nif a < b\n puts \"<\"\nelsif a > b\n puts \">\"\nelse\n puts \"=\"\nend"}, {"source_code": "a = gets.to_i\nb = gets.to_i\nif a < b\n puts \"<\"\nelsif a > b\n puts \">\"\nelse\n puts \"=\"\nend"}, {"source_code": "a=gets.to_i\nb=gets.to_i\nif ab\n puts \">\"\nelse\n puts \"=\"\nend"}, {"source_code": "num1, num2 = STDIN.read.split(\"\\n\").map { |s| s.sub /^0+/,'' }\nres = case (num1.size <=> num2.size)\n\twhen 1\n\t\t'>'\n\twhen -1\n\t\t'<'\n\telse\n\t\tcase num1 <=> num2\n\t\twhen 1\n\t\t\t'>'\n\t\twhen -1\n\t\t\t'<'\n\t\telse\n\t\t\t'='\n\t\tend\nend\n\nputs res"}, {"source_code": "a = gets.chomp.rjust(10**6, \"0\")\nb = gets.chomp.rjust(10**6, \"0\")\n\nif a == b then\n puts \"=\"\nelsif a > b then\n puts \">\"\nelse\n puts \"<\"\nend"}, {"source_code": "a,b=$<.map{|x|x.chomp.sub(/^0+/,'')}\nputs '=><'[(a.size<=>b.size)!=0 ? a.size<=>b.size : a<=>b]"}, {"source_code": "a = gets.strip\nb = gets.strip\n\nif a.size > b.size\n b = (\"0\" * (a.size - b.size)).concat(b)\nelsif b.size > a.size\n a = (\"0\" * (b.size - a.size)).concat(a)\nend\n\n0.upto(a.size-1) do |shift|\n if a[shift] > b[shift]\n puts(\">\")\n exit\n end\n if a[shift] < b[shift]\n puts(\"<\")\n exit\n end\nend\n\nputs \"=\""}, {"source_code": "a=gets.to_i\nb=gets.to_i\nif ab\n puts\">\"\nelse\n puts\"=\"\nend"}, {"source_code": "a = gets.chomp\nb = gets.chomp\na = a[1..-1] while a[0] == '0'\nb = b[1..-1] while b[0] == '0'\nputs \">\" if a.length > b.length\nputs \"<\" if a.length < b.length\nexit unless a.length == b.length\n\n(0..a.length - 1).each do |i|\n puts \"<\" if a[i] < b[i]\n puts \">\" if a[i] > b[i]\n exit unless a[i] == b[i]\nend\nputs \"=\"\n"}, {"source_code": "a = gets.chomp\nb = gets.chomp\nwhile a[0] == '0'\n a = a[1..-1]\nend\nwhile b[0] == '0'\n b = b[1..-1]\nend\nif a.length > b.length\n puts \">\"\n exit\nend\nif a.length < b.length\n puts \"<\"\n exit\nend\n\n(0..a.length - 1).each do |i|\n if a[i] < b[i]\n puts \"<\"\n exit\n end\n if a[i] > b[i]\n puts \">\"\n exit\n end\nend\nputs \"=\"\n"}], "negative_code": [{"source_code": "a,b=$<.map{|e|e.chomp.sub(/^0+/,'')}\nputs '=><'[(a.size<=>b.size)==0 ? a.size<=>b.size : a<=>b]"}, {"source_code": "a = gets\nb = gets\nif a > b\n puts \"<\"\nelsif a < b\n puts \">\"\nelse\n puts \"=\"\nend"}, {"source_code": "a = gets\nb = gets\nif a > b\n puts \">\"\nelsif a < b\n puts \"<\"\nelse\n puts \"=\"\nend"}, {"source_code": "a,b=$<.map{|x|x.chomp.sub(/^0+/,'')}\nputs '>=<'[(a.size<=>b.size)==0 ? a.size<=>b.size : a<=>b]"}, {"source_code": "a=gets.to_i\nb=gets.to_i\np \"=\" if a==b\np \"<\" if a\" if a>b"}, {"source_code": "a,b=$<.map{|x|x.chomp.sub(/^0+/,'')}\nputs '>=<'[(a.size<=>b.size)!=0 ? a.size<=>b.size : a<=>b]"}, {"source_code": "a = gets\nb = gets\n\nif a.size > b.size\n b = (\"0\" * (a.size - b.size)).concat(b)\nelseif b.size > a.size\n a = (\"0\" * (b.size - a.size)).concat(a)\nend\n\n0.upto(a.size-1) do |shift|\n if a[-1-shift] > b[-1-shift]\n puts(\">\")\n exit\n end\n if a[-1-shift] < b[-1-shift]\n puts(\"<\")\n exit\n end\nend\n\nputs \"=\""}, {"source_code": "a = gets\nb = gets\n\nif a.size > b.size\n b = (\"0\" * (a.size - b.size)).concat(b)\nelseif b.size > a.size\n a = (\"0\" * (b.size - a.size)).concat(a)\nend\n\n0.upto(a.size-1) do |shift|\n puts \">\" and exit if a[-1-shift] > b[-1-shift]\n puts \"<\" and exit if a[-1-shift] > b[-1-shift]\nend\n\nputs \"=\"\n"}, {"source_code": "a = gets.strip\nb = gets.strip\n\nif a.size > b.size\n b = (\"0\" * (a.size - b.size)).concat(b)\nelsif b.size > a.size\n a = (\"0\" * (b.size - a.size)).concat(a)\nend\n\n0.upto(a.size-1) do |shift|\n if a[-1-shift] > b[-1-shift]\n puts(\">\")\n exit\n end\n if a[-1-shift] < b[-1-shift]\n puts(\"<\")\n exit\n end\nend\n\nputs \"=\""}, {"source_code": "a = gets.strip\nb = gets.strip\n\nif a.size > b.size\n b = (\"0\" * (a.size - b.size)).concat(b)\nelsif b.size > a.size\n a = (\"0\" * (b.size - a.size)).concat(a)\nend\n\nputs a\nputs b\n0.upto(a.size-1) do |shift|\n if a[shift] > b[shift]\n puts(\">\")\n exit\n end\n if a[shift] < b[shift]\n puts(\"<\")\n exit\n end\nend\n\nputs \"=\""}, {"source_code": "a = gets\nb = gets\n\nif a.size > b.size\n b = (\"0\" * (a.size - b.size)).concat(b)\nelseif b.size > a.size\n a = (\"0\" * (b.size - a.size)).concat(a)\nend\n\n0.upto(a.size-1) do |shift|\n puts \">\" && exit if a[-1-shift] > b[-1-shift]\n puts \"<\" && exit if a[-1-shift] < b[-1-shift]\nend\n\nputs \"=\"\n"}, {"source_code": "a = gets.chomp\nb = gets.chomp\nwhile a[0] == '0'\n a = a[1..-1]\nend\nwhile b[0] == '0'\n b = b[1..-1]\nend\nif a.length > b.length\n puts \">\"\n exit\nend\nif a.length < b.length\n puts \"<\"\n exit\nend\n\na.length.downto 1 do |i|\n if a[i - 1] < b[i - 1]\n puts \"<\"\n exit\n end\n if a[i - 1] > b[i - 1]\n puts \">\"\n exit\n end\nend\nputs \"=\"\n"}, {"source_code": "a = gets.chomp\nb = gets.chomp\na = a[1..-1] while a[0] == '0'\nb = b[1..-1] while b[0] == '0'\np a\np b\nputs \">\" if a.length > b.length\nputs \"<\" if a.length < b.length\nexit unless a.length == b.length\n\n(0..a.length - 1).each do |i|\n puts \"<\" if a[i] < b[i]\n puts \">\" if a[i] > b[i]\n exit unless a[i] == b[i]\nend\nputs \"=\"\n"}, {"source_code": "a = gets.chomp\nb = gets.chomp\na = a[1...-1] while a[0] == '0'\nb = b[1..-1] while b[0] == '0'\nputs \">\" if a.length > b.length\nputs \"<\" if a.length < b.length\nexit unless a.length == b.length\n\n(0..a.length - 1).each do |i|\n puts \"<\" if a[i] < b[i]\n puts \">\" if a[i] > b[i]\n exit unless a[i] == b[i]\nend\nputs \"=\"\n"}], "src_uid": "fd63aeefba89bef7d16212a0d9e756cd"} {"nl": {"description": "It's the year 4527 and the tanks game that we all know and love still exists. There also exists Great Gena's code, written in 2016. The problem this code solves is: given the number of tanks that go into the battle from each country, find their product. If it is turns to be too large, then the servers might have not enough time to assign tanks into teams and the whole game will collapse!There are exactly n distinct countries in the world and the i-th country added ai tanks to the game. As the developers of the game are perfectionists, the number of tanks from each country is beautiful. A beautiful number, according to the developers, is such number that its decimal representation consists only of digits '1' and '0', moreover it contains at most one digit '1'. However, due to complaints from players, some number of tanks of one country was removed from the game, hence the number of tanks of this country may not remain beautiful.Your task is to write the program that solves exactly the same problem in order to verify Gena's code correctness. Just in case.", "input_spec": "The first line of the input contains the number of countries n (1 ≤ n ≤ 100 000). The second line contains n non-negative integers ai without leading zeroes — the number of tanks of the i-th country. It is guaranteed that the second line contains at least n - 1 beautiful numbers and the total length of all these number's representations doesn't exceed 100 000.", "output_spec": "Print a single number without leading zeroes — the product of the number of tanks presented by each country.", "sample_inputs": ["3\n5 10 1", "4\n1 1 10 11", "5\n0 3 1 100 1"], "sample_outputs": ["50", "110", "0"], "notes": "NoteIn sample 1 numbers 10 and 1 are beautiful, number 5 is not not.In sample 2 number 11 is not beautiful (contains two '1's), all others are beautiful.In sample 3 number 3 is not beautiful, all others are beautiful."}, "positive_code": [{"source_code": "n = gets.to_i\nas = gets.chomp.split\n\np = 1\nz = 0\nas.each do |a|\n if a == '0'\n p = 0\n z = 0\n break\n elsif a != '1' + '0' * (a.size - 1)\n p = a\n else\n z += a.size - 1\n end\nend\n\nputs p.to_s + '0' * z\n"}, {"source_code": "def isb(s)\n ones = 0\n zero = 0\n n = false\n for i in 0...s.length do\n ones += 1 if s[i] == '1'\n zero += 1 if s[i] == '0'\n n = true if s[i] != '1' && s[i] != '0'\n end\n return ones == 1 && n == false\nend\ndef czero(s)\n zero = 0\n for i in 0...s.length do\n zero += 1 if s[i] == '0'\n end\n return zero\nend\nn = gets.to_i\np = gets.strip.split(\" \")\nans = \"\"\nze = 0\ncb = false\ncontzero = false\nfor str in p do\n contzero = true if str == \"0\"\n if isb(str)\n ze += czero(str)\n else\n cb = true\n ans << str\n end\nend\nif contzero \n puts (\"0\")\nelse\n ans << \"1\" if cb == false\n for i in 0...ze do\n ans << \"0\" \n end\n puts ans \nend"}, {"source_code": "n = gets.chomp.split(\" \").map(&:to_i)\ntanks = gets.chomp.split(\" \")\n\npow = 0\nnotbeautiful = \"1\"\ntanks.each do |t|\n n1 = t.count(\"1\")\n n0 = t.count(\"0\")\n nrem = t.length - n1 - n0\n if t.length == 1 && n0 == 1 then\n puts 0\n exit\n elsif n1 != 1 or nrem != 0 then\n notbeautiful = t\n else\n pow += t.length - 1\n end\nend\nputs notbeautiful + \"0\" * pow"}, {"source_code": "n = Integer( gets.chomp )\ninput = gets.chomp\nnum = input.split(\" \")\n\nn -= 1\naux = \"-1\"\ncont = 0\nflag = false\n\nwhile n>=0\n\tone = zero = 0\n\ti = 0\n\n\twhile i0\n\t\tprint \"0\"\n\t\tcont -= 1\n\tend\n\n\tprint \"\\n\"\nend\n"}], "negative_code": [{"source_code": "def isb(s)\n ones = 0\n zero = 0\n n = false\n for i in 0...s.length do\n ones += 1 if s[i] == '1'\n zero += 1 if s[i] == '0'\n n = true if s[i] != '1' && s[i] != '0'\n end\n return ones == 1 && n == false\nend\ndef czero(s)\n zero = 0\n for i in 0...s.length do\n zero += 1 if s[i] == '0'\n end\n return zero\nend\nn = gets.to_i\np = gets.strip.split(\" \")\nans = \"\"\nze = 0\nfor str in p do\n if isb(str)\n ze += czero(str)\n else\n ans << str\n end\nend\nfor i in 0...ze do\n ans << \"0\" \nend\nputs ans "}, {"source_code": "def isb(s)\n ones = 0\n zero = 0\n n = false\n for i in 0...s.length do\n ones += 1 if s[i] == '1'\n zero += 1 if s[i] == '0'\n n = true if s[i] != '1' && s[i] != '0'\n end\n return ones == 1 && n == false\nend\ndef czero(s)\n zero = 0\n for i in 0...s.length do\n zero += 1 if s[i] == '0'\n end\n return zero\nend\nn = gets.to_i\np = gets.strip.split(\" \")\nans = \"\"\nze = 0\ncontzero = false\nfor str in p do\n contzero = true if str == \"0\"\n if isb(str)\n ze += czero(str)\n else\n ans << str\n end\nend\nif contzero \n puts (\"0\")\nelse\n for i in 0...ze do\n ans << \"0\" \n end\n puts ans \nend"}, {"source_code": "n = gets.chomp.split(\" \").map(&:to_i)\ntanks = gets.chomp.split(\" \")\n\npow = 0\nnotbeautiful = \"1\"\ntanks.each do |t|\n n1 = t.count(\"1\")\n n0 = t.count(\"0\")\n nrem = t.length - n1 - n0\n if n1 == 0 && n0 == 1 then\n puts 0\n exit\n elsif n1 != 1 or nrem != 0 then\n notbeautiful = t\n else\n pow += t.length - 1\n end\nend\nputs notbeautiful + \"0\" * pow"}, {"source_code": "n = Integer( gets.chomp )\ninput = gets.chomp\nnum = input.split(\" \")\n\nn -= 1\naux = \"0\"\ncont = 0\nflag = false\n\nwhile n>=0\n\tone = zero = 0\n\ti = 0\n\n\twhile i0\n\t\tprint \"0\"\n\t\tcont -= 1\n\tend\n\tprint \"\\n\"\nend\n"}], "src_uid": "9b1b082319d045cf0ec6d124f97a8184"} {"nl": {"description": "You got a job as a marketer in a pet shop, and your current task is to boost sales of cat food. One of the strategies is to sell cans of food in packs with discounts. Suppose you decided to sell packs with $$$a$$$ cans in a pack with a discount and some customer wants to buy $$$x$$$ cans of cat food. Then he follows a greedy strategy: he buys $$$\\left\\lfloor \\frac{x}{a} \\right\\rfloor$$$ packs with a discount; then he wants to buy the remaining $$$(x \\bmod a)$$$ cans one by one. $$$\\left\\lfloor \\frac{x}{a} \\right\\rfloor$$$ is $$$x$$$ divided by $$$a$$$ rounded down, $$$x \\bmod a$$$ is the remainer of $$$x$$$ divided by $$$a$$$.But customers are greedy in general, so if the customer wants to buy $$$(x \\bmod a)$$$ cans one by one and it happens that $$$(x \\bmod a) \\ge \\frac{a}{2}$$$ he decides to buy the whole pack of $$$a$$$ cans (instead of buying $$$(x \\bmod a)$$$ cans). It makes you, as a marketer, happy since the customer bought more than he wanted initially.You know that each of the customers that come to your shop can buy any number of cans from $$$l$$$ to $$$r$$$ inclusive. Can you choose such size of pack $$$a$$$ that each customer buys more cans than they wanted initially?", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. The first and only line of each test case contains two integers $$$l$$$ and $$$r$$$ ($$$1 \\le l \\le r \\le 10^9$$$) — the range of the number of cans customers can buy.", "output_spec": "For each test case, print YES if you can choose such size of pack $$$a$$$ that each customer buys more cans than they wanted initially. Otherwise, print NO. You can print each character in any case.", "sample_inputs": ["3\n3 4\n1 2\n120 150"], "sample_outputs": ["YES\nNO\nYES"], "notes": "NoteIn the first test case, you can take, for example, $$$a = 5$$$ as the size of the pack. Then if a customer wants to buy $$$3$$$ cans, he'll buy $$$5$$$ instead ($$$3 \\bmod 5 = 3$$$, $$$\\frac{5}{2} = 2.5$$$). The one who wants $$$4$$$ cans will also buy $$$5$$$ cans.In the second test case, there is no way to choose $$$a$$$.In the third test case, you can take, for example, $$$a = 80$$$."}, "positive_code": [{"source_code": "T = gets.to_i\n\nT.times do\n l, r = gets.split.map(&:to_i)\n\n if r < 2 * l\n puts 'YES'\n else\n puts 'NO'\n end\nend\n"}, {"source_code": "t = gets.chomp.to_i\nt.times do\n l, r = gets.chomp.split(\" \").map(&:to_i)\n kizyun = r\n if l <= kizyun / 2\n puts \"NO\"\n else\n puts \"YES\"\n end\nend"}, {"source_code": "gets.to_i.times do\n l, r = gets.split.map &:to_i\n puts 2 * l > r ? \"YES\" : \"NO\"\nend"}, {"source_code": "gets.to_i.times do\n l, r = gets.split.map(&:to_i)\n a = r + 1\n puts(if l * 2 >= a then \"YES\" else \"NO\" end)\nend\n"}], "negative_code": [{"source_code": "t = gets.chomp.to_i\nt.times do\n l, r = gets.chomp.split(\" \").map(&:to_i)\n kizyun = r + 1\n if l <= kizyun / 2\n puts \"NO\"\n else\n puts \"YES\"\n end\nend"}], "src_uid": "5172d358f1d451b42efff1019219a54d"} {"nl": {"description": "DZY loves Physics, and he enjoys calculating density.Almost everything has density, even a graph. We define the density of a non-directed graph (nodes and edges of the graph have some values) as follows: where v is the sum of the values of the nodes, e is the sum of the values of the edges.Once DZY got a graph G, now he wants to find a connected induced subgraph G' of the graph, such that the density of G' is as large as possible.An induced subgraph G'(V', E') of a graph G(V, E) is a graph that satisfies: ; edge if and only if , and edge ; the value of an edge in G' is the same as the value of the corresponding edge in G, so as the value of a node. Help DZY to find the induced subgraph with maximum density. Note that the induced subgraph you choose must be connected. ", "input_spec": "The first line contains two space-separated integers n (1 ≤ n ≤ 500), . Integer n represents the number of nodes of the graph G, m represents the number of edges. The second line contains n space-separated integers xi (1 ≤ xi ≤ 106), where xi represents the value of the i-th node. Consider the graph nodes are numbered from 1 to n. Each of the next m lines contains three space-separated integers ai, bi, ci (1 ≤ ai < bi ≤ n; 1 ≤ ci ≤ 103), denoting an edge between node ai and bi with value ci. The graph won't contain multiple edges.", "output_spec": "Output a real number denoting the answer, with an absolute or relative error of at most 10 - 9.", "sample_inputs": ["1 0\n1", "2 1\n1 2\n1 2 1", "5 6\n13 56 73 98 17\n1 2 56\n1 3 29\n1 4 42\n2 3 95\n2 4 88\n3 4 63"], "sample_outputs": ["0.000000000000000", "3.000000000000000", "2.965517241379311"], "notes": "NoteIn the first sample, you can only choose an empty subgraph, or the subgraph containing only node 1.In the second sample, choosing the whole graph is optimal."}, "positive_code": [{"source_code": "n, m = gets.split(' ').map(&:to_i)\nnode = gets.split(' ').map(&:to_i)\nmax = 0.0\nm.times do\n a, b, c = gets.split(' ').map(&:to_i)\n v = (node[a-1]+node[b-1]+0.0)/(c+0.0)\n max = v if max < v\nend\nputs max"}, {"source_code": "numbers = gets.split(' ')\nn = numbers[0].to_i\nm = numbers[1].to_i\n\nvalues = gets.split(' ').map { |s| s.to_f }\nbest = 0.0\nm.times do\n edge = gets.split(' ')\n a = edge[0].to_i\n b = edge[1].to_i\n edge_val = edge[2].to_f\n best = [best, (values[a - 1] + values[b - 1]) / edge_val].max\nend\n\nputs best"}, {"source_code": "n,m=gets.split.map(&:to_i)\nx=[0]+gets.split.map(&:to_f)\nr=0.0\nm.times do\n a,b,c=gets.split.map(&:to_i)\n y=(x[a]+x[b])/c\n r=y if rx\n\t\t\tr+=1\n\t\telse\n\t\t\tx-=b\n\t\tend\n\telse\n\t\tx+=b\n\tend\n}\nputs [x,r]*' '"}, {"source_code": "I =->{gets.split.map &:to_i}\nRD =->{gets.to_i}\n\nn, x = I[]\nz = 0\nn.times{\n a, b = gets.split\n b = b.to_i\n if a == '+'\n x += b\n else\n if x < b\n z += 1\n else\n x -= b\n end\n end\n}\n\nprint x, ' ', z\n\n\n"}, {"source_code": "n, x = gets.chomp.split.map(&:to_i)\nd = 0\nn.times do\n op, num = gets.chomp.split.map { |a| a.to_i.to_s == a ? a.to_i : a }\n\n if op == '+'\n x += num\n else\n if num > x\n d += 1\n else\n x -= num\n end\n end\nend\nputs [x, d].join(' ')\n"}, {"source_code": "loobnum , ice = gets.chomp.split.map(&:to_i)\nch = 0 \nwhile loobnum > 0 \n sign , num = gets.chomp.split.map(&:to_s)\n num = num.to_i\n if sign == '-'\n if num > ice\n ch += 1\n else\n ice = ice - num \n end\n elsif sign == '+'\n ice = ice + num \n end\n loobnum -= 1 \nend\n\nprint \"#{ice} \" \" #{ch}\"\n\n"}, {"source_code": "n, x = gets.split.map(&:to_i)\nans = 0\nn.times do\n op, num = gets.split\n num = num.to_i\n if op == \"+\"\n x += num\n else\n if x >= num\n x -= num\n else\n ans += 1\n end\n end\nend\nputs \"#{x} #{ans}\""}, {"source_code": "n, x = gets.strip.split.map(&:to_i)\npacks = x\ndistressed = 0\nn.times do\n query, d = gets.strip.split\n d = d.to_i\n if query == '+'\n packs += d\n else\n if packs < d\n distressed += 1\n else\n packs -= d\n end\n end\nend\nputs \"#{packs} #{distressed}\"\n"}, {"source_code": "n,x=gets.split.map{|e| e.to_i}\nans=0\nn.times{\n\tc,a=gets.split\n\ta=a.to_i\n\tif c==\"+\" then\n\t\tx+=a\n\telse\n\t\tif x=tmp then x-=tmp\n elsif str==\"-\" and x count\n sad += 1\n next\n end\n\n count -= x\nend\n\nputs \"#{count} #{sad}\"\n"}, {"source_code": "lineNum, initial = gets.chomp.split(' ').map(&:to_i)\n\nice = initial\nchild = 0\n\nlineNum.times do\n\topCode, amount = gets.chomp.split(' ')\n\n\tamount = amount.to_i\n\tif opCode == \"+\"\n\t\tice += amount\n\telsif opCode == \"-\"\n\t\t\tif ice >= amount\n\t\t\t\tice -= amount \n\t\t\telse\n\t\t\t\tchild += 1 \n\t\t\tend\n\tend\t\nend\n\nputs \"#{ice} #{child}\""}], "negative_code": [], "src_uid": "0a9ee8cbfa9888caef39b024563b7dcd"} {"nl": {"description": "There are $$$n$$$ positive integers $$$a_1, a_2, \\dots, a_n$$$. For the one move you can choose any even value $$$c$$$ and divide by two all elements that equal $$$c$$$.For example, if $$$a=[6,8,12,6,3,12]$$$ and you choose $$$c=6$$$, and $$$a$$$ is transformed into $$$a=[3,8,12,3,3,12]$$$ after the move.You need to find the minimal number of moves for transforming $$$a$$$ to an array of only odd integers (each element shouldn't be divisible by $$$2$$$).", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. The first line of a test case contains $$$n$$$ ($$$1 \\le n \\le 2\\cdot10^5$$$) — the number of integers in the sequence $$$a$$$. The second line contains positive integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^9$$$). The sum of $$$n$$$ for all test cases in the input doesn't exceed $$$2\\cdot10^5$$$.", "output_spec": "For $$$t$$$ test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by $$$2$$$).", "sample_inputs": ["4\n6\n40 6 40 3 20 1\n1\n1024\n4\n2 4 8 16\n3\n3 1 7"], "sample_outputs": ["4\n10\n4\n0"], "notes": "NoteIn the first test case of the example, the optimal sequence of moves can be as follows: before making moves $$$a=[40, 6, 40, 3, 20, 1]$$$; choose $$$c=6$$$; now $$$a=[40, 3, 40, 3, 20, 1]$$$; choose $$$c=40$$$; now $$$a=[20, 3, 20, 3, 20, 1]$$$; choose $$$c=20$$$; now $$$a=[10, 3, 10, 3, 10, 1]$$$; choose $$$c=10$$$; now $$$a=[5, 3, 5, 3, 5, 1]$$$ — all numbers are odd. Thus, all numbers became odd after $$$4$$$ moves. In $$$3$$$ or fewer moves, you cannot make them all odd."}, "positive_code": [{"source_code": "#!usr/bin/env ruby\n\n#https://codeforces.com/problemset/problem/1277/B\n\nrequire 'set'\n\nn = gets.to_i\n\nn.times do \n size = gets\n input = gets.split(' ').map(&:to_i) \n counter = Set.new\n\n input.each do |i|\n while i % 2 == 0 do \n counter.add(i)\n i /= 2 \n end \n end\n\n puts counter.size\nend\n"}], "negative_code": [], "src_uid": "afcd41492158e68095b01ff1e88c3dd4"} {"nl": {"description": "Professor GukiZ doesn't accept string as they are. He likes to swap some letters in string to obtain a new one.GukiZ has strings a, b, and c. He wants to obtain string k by swapping some letters in a, so that k should contain as many non-overlapping substrings equal either to b or c as possible. Substring of string x is a string formed by consecutive segment of characters from x. Two substrings of string x overlap if there is position i in string x occupied by both of them.GukiZ was disappointed because none of his students managed to solve the problem. Can you help them and find one of possible strings k?", "input_spec": "The first line contains string a, the second line contains string b, and the third line contains string c (1 ≤ |a|, |b|, |c| ≤ 105, where |s| denotes the length of string s). All three strings consist only of lowercase English letters. It is possible that b and c coincide.", "output_spec": "Find one of possible strings k, as described in the problem statement. If there are multiple possible answers, print any of them.", "sample_inputs": ["aaa\na\nb", "pozdravstaklenidodiri\nniste\ndobri", "abbbaaccca\nab\naca"], "sample_outputs": ["aaa", "nisteaadddiiklooprrvz", "ababacabcc"], "notes": "NoteIn the third sample, this optimal solutions has three non-overlaping substrings equal to either b or c on positions 1 – 2 (ab), 3 – 4 (ab), 5 – 7 (aca). In this sample, there exist many other optimal solutions, one of them would be acaababbcc."}, "positive_code": [{"source_code": "def next_h\n s = gets.strip\n d = Hash.new(0)\n s.each_char { |i| d[i] += 1 }\n [s, d]\nend\n\ndef times (a, b)\n b.each.map { |key, value| a[key] / value }.min\nend\n\ndef minus(a, b, n = 1)\n b.each { |key, value| a[key] -= value * n}\nend\n\ndef sol(a, b, c, b0, c0)\n a0 = a.dup\n s = [0] * 3\n n = times(a, b)\n 0.upto(n) do |bt|\n minus(a, b) if bt > 0\n ct = times(a, c)\n s = [s, [bt + ct, bt, ct]].max\n end\n a = a0\n answer = b0 * s[1] + c0 * s[2]\n minus(a, b, s[1])\n minus(a, c, s[2])\n answer << a.each.map {|k, v| k * v}.join\nend\n\na = next_h[1]\nb0, b = next_h\nc0, c = next_h\nputs sol(a, b, c, b0, c0)"}], "negative_code": [{"source_code": "def next_h\n s = gets.strip\n d = Hash.new(0)\n s.each_char { |i| d[i] += 1 }\n [s, d]\nend\n\ndef minus? (a, b)\n b.each.all? { |key, value| a[key] >= value }\nend\n\ndef minus(a, b)\n b.each { |key, value| a[key] -= value}\nend\n\ndef sol(a, b, c, b0, c0)\n count = 0\n str = ''\n while minus?(a, b)\n count += 1\n str << b0\n minus(a, b)\n end\n while minus?(a, c)\n count += 1\n str << c0\n minus(a, c)\n end\n str << a.each.map { |k, v| k * v}.join\n [count, str]\nend\n\na = next_h[1]\nb0, b = next_h\nc0, c = next_h\nb.freeze\nc.freeze\nputs [sol(a.dup, b, c, b0, c0), sol(a.dup, c, b, c0, b0)].max[1]"}], "src_uid": "9dc956306e2826229e393657f2d0d9bd"} {"nl": {"description": "Let's say you are standing on the $$$XY$$$-plane at point $$$(0, 0)$$$ and you want to reach point $$$(n, n)$$$.You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $$$x$$$ coordinate, or up, i. e. vertically and in the direction that increase your $$$y$$$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $$$n - 1$$$ direction changes.As a result, your path will be a polygonal chain from $$$(0, 0)$$$ to $$$(n, n)$$$, consisting of at most $$$n$$$ line segments where each segment has positive integer length and vertical and horizontal segments alternate.Not all paths are equal. You have $$$n$$$ integers $$$c_1, c_2, \\dots, c_n$$$ where $$$c_i$$$ is the cost of the $$$i$$$-th segment.Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $$$k$$$ segments ($$$k \\le n$$$), then the cost of the path is equal to $$$\\sum\\limits_{i=1}^{k}{c_i \\cdot length_i}$$$ (segments are numbered from $$$1$$$ to $$$k$$$ in the order they are in the path).Find the path of the minimum cost and print its cost.", "input_spec": "The first line contains the 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$$$ ($$$2 \\le n \\le 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$c_1, c_2, \\dots, c_n$$$ ($$$1 \\le c_i \\le 10^9$$$) — the costs of each segment. It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$10^5$$$.", "output_spec": "For each test case, print the minimum possible cost of the path from $$$(0, 0)$$$ to $$$(n, n)$$$ consisting of at most $$$n$$$ alternating segments.", "sample_inputs": ["3\n2\n13 88\n3\n2 3 1\n5\n4 3 2 1 4"], "sample_outputs": ["202\n13\n19"], "notes": "NoteIn the first test case, to reach $$$(2, 2)$$$ you need to make at least one turn, so your path will consist of exactly $$$2$$$ segments: one horizontal of length $$$2$$$ and one vertical of length $$$2$$$. The cost of the path will be equal to $$$2 \\cdot c_1 + 2 \\cdot c_2 = 26 + 176 = 202$$$.In the second test case, one of the optimal paths consists of $$$3$$$ segments: the first segment of length $$$1$$$, the second segment of length $$$3$$$ and the third segment of length $$$2$$$.The cost of the path is $$$1 \\cdot 2 + 3 \\cdot 3 + 2 \\cdot 1 = 13$$$.In the third test case, one of the optimal paths consists of $$$4$$$ segments: the first segment of length $$$1$$$, the second one — $$$1$$$, the third one — $$$4$$$, the fourth one — $$$4$$$. The cost of the path is $$$1 \\cdot 4 + 1 \\cdot 3 + 4 \\cdot 2 + 4 \\cdot 1 = 19$$$."}, "positive_code": [{"source_code": "def solve(n, c)\r\n v = (0 ... n).step(2).map { |i| c[i] }\r\n v_cumsum = v.cum(0, &:+)\r\n v_cummin = v.cum(INF) { |*xy| xy.min }\r\n h = (1 ... n).step(2).map { |i| c[i] }\r\n h_cumsum = h.cum(0, &:+)\r\n h_cummin = h.cum(INF) { |*xy| xy.min }\r\n\r\n ans = INF\r\n n.times do |i|\r\n cost = 0\r\n iv = i / 2 + 1\r\n cost += v_cummin[iv] * (n - iv) + v_cumsum[iv]\r\n ih = (i + 1) / 2\r\n cost += h_cummin[ih] * (n - ih) + h_cumsum[ih]\r\n ans = min(ans, cost)\r\n end\r\n\r\n ans\r\nend\r\n\r\ndef main\r\n t = int\r\n t.times do\r\n n = int\r\n c = ints\r\n puts solve(n, c)\r\n end\r\nend\r\n\r\nDEBUG = true\r\nMOD = 10**9+7\r\nYESNO = %w(NO YES)\r\nINF = 10**18\r\n\r\ndef int; gets.to_s.to_i end\r\ndef ints; gets.to_s.split.map { |s| s.to_i } end\r\ndef int1s; gets.to_s.split.map { |s| s.to_i - 1 } end\r\ndef float; gets.to_s.to_f end\r\ndef floats; gets.to_s.split.map { |s| s.to_f } end\r\ndef array_of(&convert); gets.to_s.split.map(&convert) end\r\ndef string; gets.to_s.chomp end\r\ndef chars; gets.to_s.chomp.chars; end\r\ndef rep(n, &b); Array.new(n, &b) end\r\ndef yes; puts YESNO[1] end\r\ndef no; puts YESNO[0] end\r\ndef yesno t; puts YESNO[t] end\r\ndef zip(xs, *yss); Enumerator.new { |y| xs.zip(*yss) { |a| y.yield(*a) } } end\r\ndef max(*xs, &block); block_given? ? xs.max_by(&block) : xs.max end\r\ndef min(*xs, &block); block_given? ? xs.min_by(&block) : xs.min end\r\ndef minmax(*xs, &block); block_given? ? xs.minmax_by(&block) : xs.minmax end\r\ndef gcd(*xs); xs.inject(0, :gcd) end\r\ndef matrix(h, w, fill=nil, &block); return Array.new(h) { Array.new(w, &block) } if block_given?; Array.new(h) { [fill] * w } end\r\ndef debug(x = nil); STDERR.puts (block_given? ? yield(x) : x).inspect if DEBUG; x end\r\ndef debug_grid(grid, width = 1); grid.each { |row| STDERR.puts row.map { |x| x.inspect.ljust(width) }.join(\"\") } if DEBUG; grid end\r\ndef if_debug; yield if DEBUG end\r\n\r\nmodule Boolean\r\n def coerce(other); [other, to_i] end\r\n def +@; to_i end\r\n def to_int; to_i end\r\n def *(other); to_i * other end\r\nend\r\n\r\nclass TrueClass\r\n include Boolean\r\n def to_i; 1 end\r\nend\r\n\r\nclass FalseClass\r\n include Boolean\r\n def to_i; 0 end\r\nend\r\n\r\nclass Integer\r\n def div_ceil(y); (self + y - 1) / y end\r\n def mod_inv(mod = MOD); pow(mod-2, mod) end\r\n def mod_div(y, mod = MOD); self * mod_inv(y, mod) % mod end\r\n def factorial(mod = MOD); (2..self).inject(1) { |f, x| f * x % mod } end\r\n def popcount; x = self; c = 0; while x > 0; c += 1 if x & 1 == 1; x >>= 1 end; c end #TODO: faster\r\n def bitbrute(&block); (1< 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\r\n def lower_bound; ac, wa = self.end_open, self.begin; while ac - wa > 1; if yield((wj = (ac + wa) / 2)); ac = wj else wa = wj end; end; yield(ac) ? ac : nil end\r\n def shakutori(r2, &pred); Enumerator.new { |y| j, r = r2.begin, r2.end_open; each { |i| j += 1 while j + 1 < r and pred[i, j+1]; y.yield(i, j) } }; end\r\n def widest(&block); Enumerator.new { |y| j, n = self.begin, self.end_open; each { |i| j += 1 while j < n and block[i, j]; y.yield(i, j) if block[i, j] } } end\r\nend\r\n\r\nclass Array\r\n def power(&block); (0 ... 1 << size).each(&block) end\r\n def sorted_merge(other); a = []; i = j = 0; n, m = size, other.size; if j < m and other[j] < self[i]; a << other[j]; j += 1 else; a << self[i]; i += 1 end while i < n; a.push(*other[j..-1]) if j < m; a end\r\n def upper_bound; ac, wa = 0, size; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\r\n def lower_bound; ac, wa = size, 0; while wa - ac > 1; if yield(self[(wj = (ac + wa) / 2)]); ac = wj else; wa = wj end; end; ac end\r\n def cum(*xs, &op); a = []; a << xs[0] if xs.size > 0; a << x = self[0]; (1...size).each { |i| a << x = op[x, self[i]] }; a end\r\n def cumdiff(range); self[range.end_open] - self[range.begin]; end\r\nend\r\n\r\nmodule Enumerable\r\n def sorted_uniq; x = nil; filter { |y| c = x === y; x = y; !c } end\r\n def cumsum; ys = [0]; each { |x| ys << x + ys[-1] }; ys end\r\nend\r\n\r\nmain"}], "negative_code": [], "src_uid": "fe9279aa95148f0887cc7eeb89afbac6"} {"nl": {"description": "You are both a shop keeper and a shop assistant at a small nearby shop. You have $$$n$$$ goods, the $$$i$$$-th good costs $$$a_i$$$ coins.You got tired of remembering the price of each product when customers ask for it, thus you decided to simplify your life. More precisely you decided to set the same price for all $$$n$$$ goods you have.However, you don't want to lose any money so you want to choose the price in such a way that the sum of new prices is not less than the sum of the initial prices. It means that if you sell all $$$n$$$ goods for the new price, you will receive at least the same (or greater) amount of money as if you sell them for their initial prices.On the other hand, you don't want to lose customers because of big prices so among all prices you can choose you need to choose the minimum one.So you need to find the minimum possible equal price of all $$$n$$$ goods so if you sell them for this price, you will receive at least the same (or greater) amount of money as if you sell them for their initial prices.You have to answer $$$q$$$ independent queries.", "input_spec": "The first line of the input contains one integer $$$q$$$ ($$$1 \\le q \\le 100$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \\le n \\le 100)$$$ — the number of goods. The second line of the query contains $$$n$$$ integers $$$a_1, a_2, \\dots, a_n$$$ ($$$1 \\le a_i \\le 10^7$$$), where $$$a_i$$$ is the price of the $$$i$$$-th good.", "output_spec": "For each query, print the answer for it — the minimum possible equal price of all $$$n$$$ goods so if you sell them for this price, you will receive at least the same (or greater) amount of money as if you sell them for their initial prices.", "sample_inputs": ["3\n5\n1 2 3 4 5\n3\n1 2 2\n4\n1 1 1 1"], "sample_outputs": ["3\n2\n1"], "notes": null}, "positive_code": [{"source_code": "q = gets.to_i\nq.times do\n n = gets.to_i\n as = gets.split.map(&:to_f)\n puts (as.inject(:+)/n).ceil\nend\n"}, {"source_code": "t = gets.to_i\nz = []\n(0..t - 1).each do |i|\n sum = 0\n po = gets\n str = gets\n z[i] = str\n bloc = z[i].split(' ')\n # bloc.map { |w| w = w.to_i}\n bloc.map { |v| sum += v.to_f }\n avg1 = sum/bloc.size\n puts avg1.ceil\nend\n"}, {"source_code": "# coding: utf-8\n\n# if __FILE__ == $0\n# end\n\nq = gets.to_i\nq.times {\n n = gets.to_i\n a = gets.split.map(&:to_i)\n sum = a.reduce(&:+)\n puts ((sum*1.0)/n).ceil\n}"}, {"source_code": "q = gets.to_i\nq.times {\n n = gets.to_i\n a = gets.split.map(&:to_i)\n p (a.inject(:+) * 1.0 / n).ceil\n}\n\n"}, {"source_code": "MOD=10**9+7\ncnt=0; sum=0; prev=nil; can=true; h=Hash.new(0)\ndef gs() gets.chomp end\ndef gi() gets.to_i end\ndef gsmi() gets.chomp.split.map(&:to_i) end\n\ndef desc(ar) ar.sort!{|x,y|y<=>x} end\ndef min(a,b) a<=b ? a : b end\ndef max(a,b) a>=b ? a : b end\ndef sum(ar) ar.inject(:+) end\n\n#def bs(ar,eq,n) en=(eq==1 ? :>= : :>); ar.bsearch_index{|x|x.send(en,n)} end\ndef C(a,b) b==0||a==b ? 1 : (b=a-b if a/2 2\n time = n - ( k - 2 )\n\n if time.even?\n str += \"ab\"*(time/2)\n else\n str += \"ab\"*(time/2)+\"a\"\n end\n\n ch = \"c\"\n\n (k-2).times do \n str += ch\n ch = ch.next\n end\n else\n if k == 1 && n > 1\n puts -1\n exit\n elsif k == 1 && n == 1\n puts \"a\"\n exit\n end\n\n if n.even?\n str += \"ab\" * (n/2)\n else\n str += \"ab\" * (n/2) + \"a\"\n end\n end\n\n puts str\n end\nend\n\nc = C.new"}, {"source_code": "n, k = gets.split(\" \").collect{|x| x.to_i}\n\nif k == 1 and n > 1\n puts \"-1\"\nelsif k == 1 and n == 1\n puts \"a\"\nelsif k > n or k > 26\n puts \"-1\"\nelse\n abc = \"abcdefghijklmnopqrstuvwxyz\"\n s = \"ab\" * (n/2)\n s += \"a\" if (n % 2) == 1\n (k-2).times do |x|\n s[s.length-k+2+x] = abc[x+2]\n end\n puts s\nend\n"}, {"source_code": "n,k=gets.split.map(&:to_i)\nif n 1\n puts \"-1\"\nelsif k == 1 and n == 1\n puts \"a\"\nelsif k > n or k > 26\n puts \"-1\"\nelse\n abc = \"abcdefghijklmnopqrstuvwxyz\"\n s = \"ab\" * (n/2)\n s += \"a\" if (n % 2) == 1\n (k-2).times do |x|\n s[s.length-k+2+x] = abc[x+2]\n end\n puts s\nend"}, {"source_code": "n, m = gets.split.map(& :to_i)\n\nif m == 1 && n != 1 || m > n\n ans = \"-1\"\nelsif m == 1\n ans = \"a\"\nelse\n L = n - (m - 2);\n ans = \"ab\" * (L/2);\n if L % 2 == 1\n ans += \"a\"\n end\n ans += ('c'..(m + 96).chr).to_a.join('');\nend\nputs ans\n"}, {"source_code": "letters = ('c'..'z').to_a.join('')\n\nn, k = gets.chomp.split.map(&:to_i)\nif n == 1 && k == 1\n puts 'a'\n exit\nend\nif k > n || k == 1\n s = -1\nelse\n p = n - k + 2\n s = 'ab' * (p / 2)\n s += 'a' if p.odd?\n s += letters[0..(n - p - 1)] if n - p > 0\nend\nputs s"}, {"source_code": "n,k = gets.strip.split.map(&:to_i)\n\nif k == 1 && n == 1\n puts 'a'\nelsif k > 1 && n >= k\n chars = []\n c = 0\n (n - (k-2)).times do\n chars << c\n c = (c + 1) % 2\n end\n\n c = 2\n (k-2).times do\n chars << c\n c += 1\n end\n\n puts chars.map{|x| ('a'.ord + x).chr}.join\nelse\n puts -1\nend\n\n\n"}], "negative_code": [{"source_code": "class C\n def initialize\n n, k = $stdin.gets.chomp.split(' ').map(&:to_i)\n\n if n < k\n puts -1\n exit\n end\n\n str = \"\"\n\n if k > 2\n time = n - ( k - 2 )\n\n if time.even?\n str += \"ab\"*time\n else\n str += \"ab\"*(time/2)+\"a\"\n end\n\n ch = \"c\"\n\n (k-2).times do \n str += ch\n ch = ch.next\n end\n else\n if k == 1 && n > 1\n puts -1\n exit\n elsif k == 1 && n == 1\n puts \"a\"\n exit\n end\n\n if n.even?\n str += \"ab\" * (n/2)\n else\n str += \"ab\" * (n/2) + \"a\"\n end\n end\n\n puts str\n end\nend\n\nc = C.new"}, {"source_code": "n,k=gets.split.map(&:to_i)\nif n 1\n puts \"-1\"\nelsif k == 1 and n == 1\n puts \"a\"\nelsif k > n or k > 26\n puts \"-1\"\nelse\n s = \"\"\n abc = \"abcdefghijklmnopqrstuvwxyz\"\n s = \"ab\" * (n/2)\n s += \"a\" if n % 2\n (k-2).times do |x|\n s[s.length-k+2+x] = abc[x+2]\n end\n puts s\nend\n"}, {"source_code": "n, k = gets.split(\" \").collect{|x| x.to_i}\n\nif k == 1 and n > 1\n puts \"-1\"\nelsif k == 1 and n == 1\n puts \"a\"\nelsif k > n or k > 26\n puts \"-1\"\nelse\n abc = \"abcdefghijklmnopqrstuvwxyz\"\n s = \"ab\" * (n/2)\n s += \"a\" if (n % 2) == 1\n puts s\n (k-2).times do |x|\n s[s.length-k+2+x] = abc[x+2]\n end\n puts s\nend\n"}, {"source_code": "letters = ('c'..'z').to_a.join('')\n\nn, k = gets.chomp.split.map(&:to_i)\nif k > n || k == 1\n s = -1\nelse\n p = n - k + 2\n s = 'ab' * (p / 2)\n s += 'a' if p.odd?\n s += letters[0..(n - p - 1)] if n - p > 0\nend\nputs s"}, {"source_code": "n,k = gets.strip.split.map(&:to_i)\n\n\nif k <= n && (k > 1 || k == n )\n chars = []\n c = 0\n (n - (k-2)).times do\n chars << c\n c = (c + 1) % 2\n end\n\n c = 2\n (k-2).times do\n chars << c\n c += 1\n end\n\n puts chars.map{|x| ('a'.ord + x).chr}.join\nelse\n puts -1\nend\n\n\n"}], "src_uid": "2f659be28674a81f58f5c587b6a0f465"} {"nl": {"description": "Let's define the cost of a string $$$s$$$ as the number of index pairs $$$i$$$ and $$$j$$$ ($$$1 \\le i < j < |s|$$$) such that $$$s_i = s_j$$$ and $$$s_{i+1} = s_{j+1}$$$.You are given two positive integers $$$n$$$ and $$$k$$$. Among all strings with length $$$n$$$ that contain only the first $$$k$$$ characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost — find any of them.", "input_spec": "The only line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \\le n \\le 2 \\cdot 10^5; 1 \\le k \\le 26$$$).", "output_spec": "Print the string $$$s$$$ such that it consists of $$$n$$$ characters, each its character is one of the $$$k$$$ first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings — print any of them.", "sample_inputs": ["9 4", "5 1", "10 26"], "sample_outputs": ["aabacadbb", "aaaaa", "codeforces"], "notes": null}, "positive_code": [{"source_code": "n, k = gets.to_s.split.map {|x| x.to_i }\ns = [k - 1] * k\nfreq = [0] * (k * k)\n\nidx = 1\nwhile idx < n\n bestfreq = freq[0 + s[idx - 1]]\n bestletter = 0\n 1.upto(k - 1) do |letter|\n if (freq[letter * k + s[idx - 1]] < bestfreq)\n bestletter = letter\n bestfreq = freq[letter * k + s[idx - 1]]\n end\n end\n freq[bestletter * k + s[idx - 1]] += 1\n s[idx] = bestletter\n idx += 1\nend\n\nprintf(\"%s\\n\", s.map {|x| x + 'a'.ord}.pack(\"c\" * n))\n"}], "negative_code": [{"source_code": "n, k = gets.to_s.split.map {|x| x.to_i }\ns = [0] * k\nfreq = [0] * (k * k)\nsinglefreq = [0] * k\n\nidx = 1\nsinglefreq[0] = 0\nwhile idx < n\n bestfreq = freq[0 + s[idx - 1]]\n bestletter = 0\n 1.upto(k - 1) do |letter|\n if (freq[letter * k + s[idx - 1]] < bestfreq) || (freq[letter * k + s[idx - 1]] == bestfreq && singlefreq[letter] < singlefreq[bestletter])\n bestletter = letter\n bestfreq = freq[letter * k + s[idx - 1]]\n end\n end\n freq[bestletter * k + s[idx - 1]] += 1\n singlefreq[bestletter] += 1\n singlefreq[s[idx - 1]] += 1\n s[idx] = bestletter\n idx += 1\nend\n\nprintf(\"%s\\n\", s.map {|x| x + 'a'.ord}.pack(\"c\" * n))\n"}, {"source_code": "n, k = gets.to_s.split.map {|x| x.to_i }\ns = [0] * k\nfreq = [0] * (k * k)\nsinglefreq = [0] * k\n\nidx = 1\nsinglefreq[0] = 1\nwhile idx < n\n bestfreq = freq[0 + s[idx - 1]]\n bestletter = 0\n 1.upto(k - 1) do |letter|\n if (freq[letter * k + s[idx - 1]] < bestfreq) || (freq[letter * k + s[idx - 1]] == bestfreq && singlefreq[letter] < singlefreq[bestletter])\n bestletter = letter\n bestfreq = freq[letter * k + s[idx - 1]]\n end\n end\n freq[bestletter * k + s[idx - 1]] += 1\n singlefreq[bestletter] += 1\n singlefreq[s[idx - 1]] += 1\n s[idx] = bestletter\n idx += 1\nend\n\nprintf(\"%s\\n\", s.map {|x| x + 'a'.ord}.pack(\"c\" * n))\n"}, {"source_code": "n, k = gets.to_s.split.map {|x| x.to_i }\ns = [0] * k\nfreq = [0] * (k * k)\nsinglefreq = [0] * k\n\nidx = 1\nsinglefreq[0] = 1\nwhile idx < n\n bestfreq = freq[0 + s[idx - 1]]\n bestletter = 0\n 1.upto(k - 1) do |letter|\n if (freq[letter * k + s[idx - 1]] < bestfreq) || (freq[letter * k + s[idx - 1]] == bestfreq && singlefreq[letter] < singlefreq[bestletter])\n bestletter = letter\n bestfreq = freq[letter * k + s[idx - 1]]\n end\n end\n freq[bestletter * k + s[idx - 1]] += 1\n singlefreq[bestletter] += 1\n s[idx] = bestletter\n idx += 1\nend\n\nprintf(\"%s\\n\", s.map {|x| x + 'a'.ord}.pack(\"c\" * n))\n"}, {"source_code": "n, k = gets.to_s.split.map {|x| x.to_i }\ns = [0] * k\nfreq = [0] * (k * k)\n\nidx = 1\nwhile idx < n\n bestfreq = freq[0 + s[idx - 1]]\n bestletter = 0\n 1.upto(k - 1) do |letter|\n if freq[letter * k + s[idx - 1]] < bestfreq\n bestletter = letter\n bestfreq = freq[letter * k + s[idx - 1]]\n end\n end\n freq[bestletter * k + s[idx - 1]] += 1\n s[idx] = bestletter\n idx += 1\nend\n\nprintf(\"%s\\n\", s.map {|x| x + 'a'.ord}.pack(\"c\" * n))\n"}], "src_uid": "19cc504c81bd4f224ecb17f03cfb9bd7"} {"nl": {"description": "Ezzat has an array of $$$n$$$ integers (maybe negative). He wants to split it into two non-empty subsequences $$$a$$$ and $$$b$$$, such that every element from the array belongs to exactly one subsequence, and the value of $$$f(a) + f(b)$$$ is the maximum possible value, where $$$f(x)$$$ is the average of the subsequence $$$x$$$. A sequence $$$x$$$ is a subsequence of a sequence $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) elements.The average of a subsequence is the sum of the numbers of this subsequence divided by the size of the subsequence.For example, the average of $$$[1,5,6]$$$ is $$$(1+5+6)/3 = 12/3 = 4$$$, so $$$f([1,5,6]) = 4$$$.", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^3$$$)— the number of test cases. Each test case consists of two lines. The first line contains a single integer $$$n$$$ ($$$2 \\le n \\le 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \\ldots, a_n$$$ ($$$-10^9 \\le a_i \\le 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3\\cdot10^5$$$.", "output_spec": "For each test case, print a single value — the maximum value that Ezzat can achieve. Your answer is considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is accepted if and only if $$$\\frac{|a - b|}{\\max{(1, |b|)}} \\le 10^{-6}$$$.", "sample_inputs": ["4\n3\n3 1 2\n3\n-7 -6 -6\n3\n2 2 2\n4\n17 3 5 -3"], "sample_outputs": ["4.500000000\n-12.500000000\n4.000000000\n18.666666667"], "notes": "NoteIn the first test case, the array is $$$[3, 1, 2]$$$. These are all the possible ways to split this array: $$$a = [3]$$$, $$$b = [1,2]$$$, so the value of $$$f(a) + f(b) = 3 + 1.5 = 4.5$$$. $$$a = [3,1]$$$, $$$b = [2]$$$, so the value of $$$f(a) + f(b) = 2 + 2 = 4$$$. $$$a = [3,2]$$$, $$$b = [1]$$$, so the value of $$$f(a) + f(b) = 2.5 + 1 = 3.5$$$. Therefore, the maximum possible value $$$4.5$$$.In the second test case, the array is $$$[-7, -6, -6]$$$. These are all the possible ways to split this array: $$$a = [-7]$$$, $$$b = [-6,-6]$$$, so the value of $$$f(a) + f(b) = (-7) + (-6) = -13$$$. $$$a = [-7,-6]$$$, $$$b = [-6]$$$, so the value of $$$f(a) + f(b) = (-6.5) + (-6) = -12.5$$$. Therefore, the maximum possible value $$$-12.5$$$."}, "positive_code": [{"source_code": "#!/usr/bin/env ruby\r\n\r\ndef arline\r\n gets.chomp.split.map(&:to_i)\r\nend\r\n \r\ntc = arline[0]\r\ntc.times do\r\n n = arline[0]\r\n ar = arline.sort\r\n puts (ar.pop + ar.sum.to_f/ar.length)\r\nend\r\n"}, {"source_code": "testcases = gets.to_i\ntestcases.times do\n n = gets.to_i\n arr = gets.split(\" \").map!(&:to_f)\n half = (arr.sum - arr.max)/(n-1)\n res = arr.max + half\n puts res.to_f\n testcases -= 1\nend"}, {"source_code": "t = gets.chomp.to_i\r\nt.times do\r\n n = gets.chomp.to_i\r\n a = gets.chomp.split(\" \").map(&:to_i).sort\r\n sum = 0\r\n a.each do |i|\r\n sum += i\r\n end\r\n mae = 0\r\n ushiro = 0\r\n cnt = 0\r\n max = -Float::INFINITY\r\n a.each do |j|\r\n mae += j\r\n cnt += 1\r\n ushiro = sum - mae\r\n ans = (mae/cnt.to_f) + (ushiro/(n-cnt).to_f) if cnt != n\r\n max = [max,ans].max if cnt != n\r\n # puts \"~~~~~\"\r\n # p ans\r\n # puts \"===\"\r\n end\r\n puts max\r\nend"}, {"source_code": "END { load __FILE__ unless $stdin.eof? }\n\nt = gets.to_i\n\nt.times do\n\tn = gets.to_i\n\ta = gets.split.map!(&:to_i)\n\n\ta_max = a.max\n\tputs a_max + (a.sum - a_max).fdiv(n - 1)\nend\n"}], "negative_code": [{"source_code": "t = gets.chomp.to_i\r\nt.times do\r\n n = gets.chomp.to_i\r\n a = gets.chomp.split(\" \").map(&:to_i)\r\n sum = 0\r\n a.each do |i|\r\n sum += i\r\n end\r\n mae = 0\r\n ushiro = 0\r\n cnt = 0\r\n max = -Float::INFINITY\r\n a.each do |j|\r\n mae += j\r\n cnt += 1\r\n ushiro = sum - mae\r\n ans = (mae/cnt.to_f) + (ushiro/(n-cnt).to_f) if cnt != n\r\n max = [max,ans].max if cnt != n\r\n end\r\n puts max\r\nend"}], "src_uid": "159b9c743d6d8792548645b9f7be2753"} {"nl": {"description": "Suppose you are living with two cats: A and B. There are $$$n$$$ napping spots where both cats usually sleep.Your cats like to sleep and also like all these spots, so they change napping spot each hour cyclically: Cat A changes its napping place in order: $$$n, n - 1, n - 2, \\dots, 3, 2, 1, n, n - 1, \\dots$$$ In other words, at the first hour it's on the spot $$$n$$$ and then goes in decreasing order cyclically; Cat B changes its napping place in order: $$$1, 2, 3, \\dots, n - 1, n, 1, 2, \\dots$$$ In other words, at the first hour it's on the spot $$$1$$$ and then goes in increasing order cyclically. The cat B is much younger, so they have a strict hierarchy: A and B don't lie together. In other words, if both cats'd like to go in spot $$$x$$$ then the A takes this place and B moves to the next place in its order (if $$$x < n$$$ then to $$$x + 1$$$, but if $$$x = n$$$ then to $$$1$$$). Cat B follows his order, so it won't return to the skipped spot $$$x$$$ after A frees it, but will move to the spot $$$x + 2$$$ and so on.Calculate, where cat B will be at hour $$$k$$$?", "input_spec": "The first line contains a single integer $$$t$$$ ($$$1 \\le t \\le 10^4$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \\le n \\le 10^9$$$; $$$1 \\le k \\le 10^9$$$) — the number of spots and hour $$$k$$$.", "output_spec": "For each test case, print one integer — the index of the spot where cat B will sleep at hour $$$k$$$.", "sample_inputs": ["7\n2 1\n2 2\n3 1\n3 2\n3 3\n5 5\n69 1337"], "sample_outputs": ["1\n2\n1\n3\n2\n2\n65"], "notes": "NoteIn the first and second test cases $$$n = 2$$$, so: at the $$$1$$$-st hour, A is on spot $$$2$$$ and B is on $$$1$$$; at the $$$2$$$-nd hour, A moves to spot $$$1$$$ and B — to $$$2$$$. If $$$n = 3$$$ then: at the $$$1$$$-st hour, A is on spot $$$3$$$ and B is on $$$1$$$; at the $$$2$$$-nd hour, A moves to spot $$$2$$$; B'd like to move from $$$1$$$ to $$$2$$$, but this spot is occupied, so it moves to $$$3$$$; at the $$$3$$$-rd hour, A moves to spot $$$1$$$; B also would like to move from $$$3$$$ to $$$1$$$, but this spot is occupied, so it moves to $$$2$$$. In the sixth test case: A's spots at each hour are $$$[5, 4, 3, 2, 1]$$$; B's spots at each hour are $$$[1, 2, 4, 5, 2]$$$. "}, "positive_code": [{"source_code": "gets.to_i.times do\n n, k = gets.split.map(&:to_i)\n k -= 1\n puts (k + (n % 2) * (k / (n / 2))) % n + 1\nend\n"}, {"source_code": "gets.to_i.times do\r\n n,k=gets.split.map(&:to_i)\r\n k -= 1\r\n p (k+n%2*k/(n/2))%n+1\r\nend"}, {"source_code": "gets.to_i.times do\r\n n,k=gets.split.map(&:to_i)\r\n if n.even?\r\n puts (k-1)%n+1\r\n else\r\n k%=n/2*n\r\n puts (k+(k-1)/(n/2)-1)%n+1\r\n end\r\nend\r\n"}], "negative_code": [], "src_uid": "2e837d3afc48177516578a950e957586"} {"nl": {"description": "One day Anna got the following task at school: to arrange several numbers in a circle so that any two neighboring numbers differs exactly by 1. Anna was given several numbers and arranged them in a circle to fulfill the task. Then she wanted to check if she had arranged the numbers correctly, but at this point her younger sister Maria came and shuffled all numbers. Anna got sick with anger but what's done is done and the results of her work had been destroyed. But please tell Anna: could she have hypothetically completed the task using all those given numbers?", "input_spec": "The first line contains an integer n — how many numbers Anna had (3 ≤ n ≤ 105). The next line contains those numbers, separated by a space. All numbers are integers and belong to the range from 1 to 109.", "output_spec": "Print the single line \"YES\" (without the quotes), if Anna could have completed the task correctly using all those numbers (using all of them is necessary). If Anna couldn't have fulfilled the task, no matter how hard she would try, print \"NO\" (without the quotes).", "sample_inputs": ["4\n1 2 3 2", "6\n1 1 2 2 2 3", "6\n2 4 1 1 2 2"], "sample_outputs": ["YES", "YES", "NO"], "notes": null}, "positive_code": [{"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\nt=Hash.new()\nt.default=0\na.each{|q| t[q]+=1 }\na.sort!\nnow,count=a[0],1\nwhile count0\n now+=1\n elsif t[now-1]>0\n now-=1\n else\n puts \"NO\"\n exit\n end\n count+=1\nend\nif (a[0]-now).abs==1\n puts \"YES\"\nelse\n puts \"NO\"\nend\n"}], "negative_code": [], "src_uid": "de41c260a37ec9db2371efb9d246a470"} {"nl": {"description": "You are given a string $$$s[1 \\dots n]$$$ consisting of lowercase Latin letters. It is guaranteed that $$$n = 2^k$$$ for some integer $$$k \\ge 0$$$.The string $$$s[1 \\dots n]$$$ is called $$$c$$$-good if at least one of the following three conditions is satisfied: The length of $$$s$$$ is $$$1$$$, and it consists of the character $$$c$$$ (i.e. $$$s_1=c$$$); The length of $$$s$$$ is greater than $$$1$$$, the first half of the string consists of only the character $$$c$$$ (i.e. $$$s_1=s_2=\\dots=s_{\\frac{n}{2}}=c$$$) and the second half of the string (i.e. the string $$$s_{\\frac{n}{2} + 1}s_{\\frac{n}{2} + 2} \\dots s_n$$$) is a $$$(c+1)$$$-good string; The length of $$$s$$$ is greater than $$$1$$$, the second half of the string consists of only the character $$$c$$$ (i.e. $$$s_{\\frac{n}{2} + 1}=s_{\\frac{n}{2} + 2}=\\dots=s_n=c$$$) and the first half of the string (i.e. the string $$$s_1s_2 \\dots s_{\\frac{n}{2}}$$$) is a $$$(c+1)$$$-good string. For example: \"aabc\" is 'a'-good, \"ffgheeee\" is 'e'-good.In one move, you can choose one index $$$i$$$ from $$$1$$$ to $$$n$$$ and replace $$$s_i$$$ with any lowercase Latin letter (any character from 'a' to 'z').Your task is to find the minimum number of moves required to obtain an 'a'-good string from $$$s$$$ (i.e. $$$c$$$-good string for $$$c=$$$ 'a'). It is guaranteed that the answer always exists.You have to answer $$$t$$$ independent test cases.Another example of an 'a'-good string is as follows. Consider the string $$$s = $$$\"cdbbaaaa\". It is an 'a'-good string, because: the second half of the string (\"aaaa\") consists of only the character 'a'; the first half of the string (\"cdbb\") is 'b'-good string, because: the second half of the string (\"bb\") consists of only the character 'b'; the first half of the string (\"cd\") is 'c'-good string, because: the first half of the string (\"c\") consists of only the character 'c'; the second half of the string (\"d\") is 'd'-good string. ", "input_spec": "The first line of the input contains one integer $$$t$$$ ($$$1 \\le t \\le 2 \\cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 \\le n \\le 131~072$$$) — the length of $$$s$$$. It is guaranteed that $$$n = 2^k$$$ for some integer $$$k \\ge 0$$$. The second line of the test case contains the string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 \\cdot 10^5$$$ ($$$\\sum n \\le 2 \\cdot 10^5$$$).", "output_spec": "For each test case, print the answer — the minimum number of moves required to obtain an 'a'-good string from $$$s$$$ (i.e. $$$c$$$-good string with $$$c =$$$ 'a'). It is guaranteed that the answer exists.", "sample_inputs": ["6\n8\nbbdcaaaa\n8\nasdfghjk\n8\nceaaaabb\n8\nbbaaddcc\n1\nz\n2\nac"], "sample_outputs": ["0\n7\n4\n5\n1\n1"], "notes": null}, "positive_code": [{"source_code": "def solve(s, c)\n return s != c ? 1 : 0 if s.size == 1\n m = s.size / 2\n l = m - s[m...s.size].count(c) + solve(s[0...m], c.succ)\n r = m - s[0...m].count(c) + solve(s[m...s.size], c.succ)\n [l, r].min\nend\ngets.to_i.times do\n n = gets.to_i\n s = gets.chomp\n puts solve(s, 'a')\nend"}], "negative_code": [], "src_uid": "324b7957b46dfe4948074c781159b7e7"} {"nl": {"description": "According to Berland laws it is only allowed to sell alcohol to people not younger than 18 years. Vasya's job is to monitor the law's enforcement. Tonight he entered a bar and saw n people sitting there. For every one of them Vasya happened to determine either the age or the drink the person is having. Vasya can check any person, i.e. learn his age and the drink he is having at the same time. What minimal number of people should Vasya check additionally to make sure that there are no clients under 18 having alcohol drinks?The list of all alcohol drinks in Berland is: ABSINTH, BEER, BRANDY, CHAMPAGNE, GIN, RUM, SAKE, TEQUILA, VODKA, WHISKEY, WINE", "input_spec": "The first line contains an integer n (1 ≤ n ≤ 100) which is the number of the bar's clients. Then follow n lines, each describing one visitor. A line either contains his age (an integer from 0 to 1000) or his drink (a string of capital Latin letters from 1 to 100 in length). It is guaranteed that the input data does not contain spaces and other unnecessary separators. Only the drinks from the list given above should be considered alcohol.", "output_spec": "Print a single number which is the number of people Vasya should check to guarantee the law enforcement.", "sample_inputs": ["5\n18\nVODKA\nCOKE\n19\n17"], "sample_outputs": ["2"], "notes": "NoteIn the sample test the second and fifth clients should be checked."}, "positive_code": [{"source_code": "gets\np gets(p).split.select{|_|(%w(ABSINTH BEER BRANDY CHAMPAGNE GIN RUM SAKE TEQUILA VODKA WHISKEY WINE)+[*?0..\"17\"]).include? _}.size"}, {"source_code": "gets\np gets(p).split.select{|_|(%w(ABSINTH BEER BRANDY CHAMPAGNE GIN RUM SAKE TEQUILA VODKA WHISKEY WINE)+[*?0..\"17\"]).include? _}.size\n"}, {"source_code": "def toi(n)\n if n.to_i.to_s != n\n 10000\n else\n n.to_i\n end\nend\n\ndef check(str)\n alchohol = [\"ABSINTH\", \"BEER\", \"BRANDY\", \"CHAMPAGNE\", \"GIN\", \"RUM\", \"SAKE\", \"TEQUILA\", \"VODKA\", \"WHISKEY\", \"WINE\"]\n is_al = alchohol.find { |drink| str == drink }\n return true if (is_al)\n return toi(str) < 18\nend\n\nn = gets.chomp.to_i\ns = 0\nn.times do\n str = gets.chomp\n if check str\n s += 1\n end\nend\n\nputs s\n"}, {"source_code": "s=[\"ABSINTH\", \"BEER\", \"BRANDY\", \"CHAMPAGNE\", \"GIN\", \"RUM\", \"SAKE\", \"TEQUILA\", \"VODKA\", \"WHISKEY\", \"WINE\",\"0\",\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\",\"10\",\"11\",\"12\",\"13\",\"14\",\"15\",\"16\",\"17\"]\nn=gets.to_i\nans=0\nn.times{\n t=gets.split[0]\n if s.index(t)!=nil then\n ans+=1\n end\n}\nputs ans"}, {"source_code": "gets\np gets(p).split.select{|_|(%w(ABSINTH BEER BRANDY CHAMPAGNE GIN RUM SAKE TEQUILA VODKA WHISKEY WINE)+[*?0..\"17\"]).include? _}.size\n"}, {"source_code": "gets\np gets(p).split.select{|_|(%w(ABSINTH BEER BRANDY CHAMPAGNE GIN RUM SAKE TEQUILA VODKA WHISKEY WINE)+[*?0..\"17\"]).include? _}.size\n"}, {"source_code": "gets\np gets(p).split.select{|_|(%w(ABSINTH BEER BRANDY CHAMPAGNE GIN RUM SAKE TEQUILA VODKA WHISKEY WINE)+[*?0..\"17\"]).include? _}.size"}, {"source_code": "a,ctr=[],0\nal=[\"ABSINTH\", \"BEER\", \"BRANDY\", \"CHAMPAGNE\", \"GIN\", \"RUM\", \"SAKE\", \"TEQUILA\", \"VODKA\", \"WHISKEY\", \"WINE\"]\nn=gets.to_i\nn.times do\n\tx=gets.chomp\n\tctr+=1 if al.include?(x) || (x =~ /[0-9]+/ && x.to_i < 18)\nend\nputs ctr\n"}, {"source_code": "gets\np gets(p).split.select{|_|(%w(ABSINTH BEER BRANDY CHAMPAGNE GIN RUM SAKE TEQUILA VODKA WHISKEY WINE)+[*?0..\"17\"]).include? _}.size\n"}, {"source_code": "gets\np gets(p).split.select{|_|(%w(ABSINTH BEER BRANDY CHAMPAGNE GIN RUM SAKE TEQUILA VODKA WHISKEY WINE)+[*?0..\"17\"]).include? _}.size\n"}, {"source_code": "n = gets.strip.to_i\ndrinks = [ \"ABSINTH\", \"BEER\", \"BRANDY\", \"CHAMPAGNE\", \"GIN\", \"RUM\", \"SAKE\", \"TEQUILA\", \"VODKA\", \"WHISKEY\", \"WINE\" ]\nans = 0\nn.times do\n i = gets.strip\n if i =~ /[0-9]/\n age = i.to_i\n ans += 1 if age < 18\n else\n ans += 1 if drinks.include? i\n end\nend\nputs ans\n"}, {"source_code": "gets\np gets(p).split.select{|_|(%w(ABSINTH BEER BRANDY CHAMPAGNE GIN RUM SAKE TEQUILA VODKA WHISKEY WINE)+[*?0..\"17\"]).include? _}.size"}, {"source_code": "#=================================\nrequire 'prime'\ninclude Math\n\nif ARGV.include?(\"ONLINE_JUDGE\")\n\t$stdin = File.open(\"./input.txt\", \"r\")\n\tARGV.delete(\"ONLINE_JUDGE\")\nend\n#=================================\n\nn=gets.to_i\ndr=[:ABSINTH, :BEER, :BRANDY, :CHAMPAGNE, :GIN, :RUM, :SAKE, :TEQUILA, :VODKA, :WHISKEY, :WINE].map{|v| v.to_s}\n\n0.upto(17){|age|\n\tdr << age.to_s\n}\n\ncount=0\nn.times{\n\tline=gets.strip\n\t\n\tif dr.include?(line)\n\t\tcount+=1\n\tend\n}\n\nputs count\n"}, {"source_code": "judge_arr = %w(ABSINTH BEER BRANDY CHAMPAGNE GIN RUM SAKE TEQUILA VODKA WHISKEY WINE)\ns=gets\nvcount=0\ns.to_i.times {\n a=gets \n a.chop!\n if(a.match(/\\d+/)==nil) then\n judge_arr.include?(a)?vcount+=1:vcount\n else\n a.to_i<18?vcount+=1:vcount\n end\n }\n puts vcount"}, {"source_code": "gets\np gets(p).split.select{|_|(%w(ABSINTH BEER BRANDY CHAMPAGNE GIN RUM SAKE TEQUILA VODKA WHISKEY WINE)+[*?0..\"17\"]).include? _}.size\n"}, {"source_code": "drinks = [\"ABSINTH\",\"BEER\",\"BRANDY\",\"CHAMPAGNE\",\"GIN\",\"RUM\",\"SAKE\",\"TEQUILA\", \"VODKA\",\"WHISKEY\", \"WINE\"]\nn = gets.to_i\ncount = 0\nn.times do |i|\n\tline = gets.chomp.upcase\n\n\tage = line.to_i unless line =~ /[A-Z]/\n\t# puts line\n\tcount += 1 if ((age && age < 18) || drinks.include?(line) ) \n\t# puts \"count #{count}\"\nend\nputs count"}, {"source_code": "n = gets.to_i\n\ncount = 0\nmas = \",ABSINTH,BEER,BRANDY,CHAMPAGNE,GIN,RUM,SAKE,TEQUILA,VODKA,WHISKEY,WINE,\"\nfor i in 0..n-1\n s = gets.chomp!\n if ((s =~ /[0-9]+/ && s.to_i < 18) || (mas.index(\",\"+s+\",\") != nil))\n count += 1\n end\nend\n\nputs count"}, {"source_code": "alcohol = [\"ABSINTH\", \"BEER\", \"BRANDY\", \"CHAMPAGNE\", \"GIN\", \"RUM\",\n \"SAKE\", \"TEQUILA\", \"VODKA\", \"WHISKEY\", \"WINE\"]\n\nnum = gets.chomp.to_i\n\ncount = 0\n\nnum.times do |i|\n str = gets.chomp\n if str =~ /\\d+/\n if str.to_i < 18\n count = count + 1\n end\n else\n if alcohol.include? str\n count = count + 1\n end\n end\nend\n\nputs count\n"}, {"source_code": "n = gets.chomp.to_i\nguys = n.times.map { gets.chomp }\n\n\nalc = %w(ABSINTH BEER BRANDY CHAMPAGNE GIN RUM SAKE TEQUILA VODKA WHISKEY WINE)\n\ncnt = 0\nguys.each do |g|\n cnt += 1 if (g =~ /(\\d+)/ && $1.to_i < 18) || alc.include?(g)\nend\n\nputs cnt"}, {"source_code": "gets\np gets(p).split.select{|_|(%w(ABSINTH BEER BRANDY CHAMPAGNE GIN RUM SAKE TEQUILA VODKA WHISKEY WINE)+[*?0..\"17\"]).include? _}.size\n"}, {"source_code": "gets\np gets(p).split.select{|_|(%w(ABSINTH BEER BRANDY CHAMPAGNE GIN RUM SAKE TEQUILA VODKA WHISKEY WINE)+[*?0..\"17\"]).include? _}.size\n"}], "negative_code": [{"source_code": "a,ctr=[],0\nal=[\"ABSINTH\", \"BEER\", \"BRANDY\", \"CHAMPAGNE\", \"GIN\", \"RUM\", \"SAKE\", \"TEQUILA\", \"VODKA\", \"WHISKEY\", \"WINE\"]\nn=gets.to_i\nn.times do\n\tx=gets.chomp\n\tctr+=1 if al.include?(x) || (x.to_i!=0 && x.to_i<18)\nend\nputs ctr\n"}, {"source_code": "#=================================\nrequire 'prime'\ninclude Math\n\nif ARGV.include?(\"ONLINE_JUDGE\")\n\t$stdin = File.open(\"./input.txt\", \"r\")\n\tARGV.delete(\"ONLINE_JUDGE\")\nend\n#=================================\n\nn=gets.to_i\ndr=[:ABSINTH, :BEER, :BRANDY, :CHAMPAGNE, :GIN, :RUM, :SAKE, :TEQUILA, :VODKA, :WHISKEY, :WINE].map{|v| v.to_s}\n\ncount=0\nn.times{\n\tline=gets.strip\n\t\n\tage=line.to_i\n\tage=19 if age==0\n\t\n\tif dr.include?(line) || age<18\n\t\tputs \"line=#{line} age=#{age}\"\n\t\tcount+=1\n\tend\n}\n\nputs count\n"}, {"source_code": "#=================================\nrequire 'prime'\ninclude Math\n\nif ARGV.include?(\"ONLINE_JUDGE\")\n\t$stdin = File.open(\"./input.txt\", \"r\")\n\tARGV.delete(\"ONLINE_JUDGE\")\nend\n#=================================\n\nn=gets.to_i\ndr=[:ABSINTH, :BEER, :BRANDY, :CHAMPAGNE, :GIN, :RUM, :SAKE, :TEQUILA, :VODKA, :WHISKEY, :WINE].map{|v| v.to_s}\n\ncount=0\nn.times{\n\tline=gets.strip\n\t\n\tage=line.to_i\n\tage=19 if age==0\n\t\n\tif dr.include?(line) || age<18\n\t\tcount+=1\n\tend\n}\n\nputs count\n"}, {"source_code": "#=================================\nrequire 'prime'\ninclude Math\n\nif ARGV.include?(\"ONLINE_JUDGE\")\n\t$stdin = File.open(\"./input.txt\", \"r\")\n\tARGV.delete(\"ONLINE_JUDGE\")\nend\n#=================================\n\nn=gets.to_i\ndr=[:ABSINTH, :BEER, :BRANDY, :CHAMPAGNE, :GIN, :RUM, :SAKE, :TEQUILA, :VODKA, :WHISKEY, :WINE].map{|v| v.to_s}\n\n0.upto(17){|age|\n\tputs age\n\tdr << age.to_s\n}\n\ncount=0\nn.times{\n\tline=gets.strip\n\t\n\tif dr.include?(line)\n\t\tcount+=1\n\tend\n}\n\nputs count\n"}, {"source_code": "#=================================\nrequire 'prime'\ninclude Math\n\nif ARGV.include?(\"ONLINE_JUDGE\")\n\t$stdin = File.open(\"./input.txt\", \"r\")\n\tARGV.delete(\"ONLINE_JUDGE\")\nend\n#=================================\n\nn=gets.to_i\ndr=[:ABSINTH, :BEER, :BRANDY, :CHAMPAGNE, :GIN, :RUM, :SAKE, :TEQUILA, :VODKA, :WHISKEY, :WINE].map{|v| v.to_s}\n\n1.upto(17){|age|\n\tdr << age.to_s\n}\n\ncount=0\nn.times{\n\tline=gets.strip\n\t\n\tif dr.include?(line)\n\t\tcount+=1\n\tend\n}\n\nputs count\n"}, {"source_code": "drinks = [\"ABSINTH\",\"BEER\",\"BRANDY\",\"CHAMPAGNE\",\"GIN\",\"RUM\",\"SAKE\",\"TEQUILA\", \"VODKA\",\"WHISKEY\", \"WINE\"]\nn = gets.to_i\ncount = 0\nn.times do |i|\n\tline = gets.chomp\n\tage = line.to_i\n\t# puts line\n\tcount += 1 if ((age > 0 && age < 18) || (age == 0 && drinks.include?(line)) ) \n\t# puts \"count #{count}\"\nend\nputs count"}, {"source_code": "n = gets.to_i\n\ncount = 0\nmas = \"ABSINTH,BEER,BRANDY,CHAMPAGNE,GIN,RUM,SAKE,TEQUILA,VODKA,WHISKEY,WINE\"\nfor i in 0..n-1\n s = gets.chomp!\n if ((s =~ /[0-9]+/ && s.to_i < 18) || (mas.index(s) != nil))\n count += 1\n end\nend\n\nputs count"}], "src_uid": "4b7b0fba7b0af78c3956c34c29785e7c"} {"nl": {"description": "One popular website developed an unusual username editing procedure. One can change the username only by deleting some characters from it: to change the current name s, a user can pick number p and character c and delete the p-th occurrence of character c from the name. After the user changed his name, he can't undo the change.For example, one can change name \"arca\" by removing the second occurrence of character \"a\" to get \"arc\". Polycarpus learned that some user initially registered under nickname t, where t is a concatenation of k copies of string s. Also, Polycarpus knows the sequence of this user's name changes. Help Polycarpus figure out the user's final name.", "input_spec": "The first line contains an integer k (1 ≤ k ≤ 2000). The second line contains a non-empty string s, consisting of lowercase Latin letters, at most 100 characters long. The third line contains an integer n (0 ≤ n ≤ 20000) — the number of username changes. Each of the next n lines contains the actual changes, one per line. The changes are written as \"pi ci\" (without the quotes), where pi (1 ≤ pi ≤ 200000) is the number of occurrences of letter ci, ci is a lowercase Latin letter. It is guaranteed that the operations are correct, that is, the letter to be deleted always exists, and after all operations not all letters are deleted from the name. The letters' occurrences are numbered starting from 1.", "output_spec": "Print a single string — the user's final name after all changes are applied to it.", "sample_inputs": ["2\nbac\n3\n2 a\n1 b\n2 c", "1\nabacaba\n4\n1 a\n1 a\n1 c\n2 b"], "sample_outputs": ["acb", "baa"], "notes": "NoteLet's consider the first sample. Initially we have name \"bacbac\"; the first operation transforms it into \"bacbc\", the second one — to \"acbc\", and finally, the third one transforms it into \"acb\"."}, "positive_code": [{"source_code": "class T\n def initialize(l, r)\n @l = l\n @r = r\n @m = (l + r) / 2\n @c = r - l\n end\n\n def l\n @pl = @pl || T.new(@l, @m)\n end\n\n def r\n @pr = @pr || T.new(@m, @r)\n end\n\n def c\n @c\n end\n\n def d(i)\n @c -= 1\n if @r - @l == 1\n @l\n elsif i < l.c\n l.d(i)\n else\n r.d(i - l.c)\n end\n end\nend\n\nr = gets.to_i\ns = gets.chop\nt = Array.new(128){T.new(0, r * s.size)}\nd = Array.new(128){[]}\ngets.to_i.times{|i|\n p, c = gets.split\n c = c.ord\n d[c] << t[c].d(p.to_i - 1)\n}\nd.each{|_|\n _.sort!.reverse!\n}\nt = [0] * 128\nz = []\nr.times{\n s.each_byte{|b|\n if t[b] == d[b][-1]\n d[b].pop\n else\n z << b\n end\n t[b] += 1\n }\n}\nputs z.map(&:chr) * ''\n"}, {"source_code": "k = gets().to_i\ns = gets().chomp\nn = gets().to_i\nchanges = []\nn.times { changes << gets().chomp.split }\n\nname = s * k\n\nletter_indexes = {}\n0.upto(name.size - 1) do |i|\n letter = name[i]\n letter_indexes[letter] ||= []\n letter_indexes[letter] << i\nend\n\nchanges.each do |position, letter|\n position = position.to_i\n \n if indexes = letter_indexes[letter] and indexes.size >= position\n name[indexes[position - 1]] = '0'\n letter_indexes[letter].delete_at(position - 1)\n end\nend\n\nputs name.delete('0')"}, {"source_code": "# http://codeforces.ru/contest/159/problem/C\n\ndef run(input = STDIN.read)\n lines = input.split(\"\\n\")\n\n k = lines[0].to_i\n s = lines[1]\n n = lines[2].to_i\n\n cmd = lines[3..-1]\n\n word = (s * k)\n\n h = {}\n l = {}\n\n i = 0\n word.each_char { |c|\n if h.has_key?(c) then\n h[c].push(i)\n else\n h[c] = [i]\n end\n i += 1\n }\n\n #p h\n\n cmd.each { |c| \n (occurance, letter) = c.split(\" \")\n occurance = occurance.to_i\n\n pos = h[letter][occurance - 1]\n word[pos] = \"0\"\n h[letter].delete_at(occurance - 1)\n \n #p word\n }\n\n return word.delete(\"0\")\nend\n\nbegin\n require './testingx.rb'\nrescue LoadError\n puts run\nend\n__END__\n2\nbac\n3\n2 a\n1 b\n2 c\n=\nacb\n---\n1\nabacaba\n4\n1 a\n1 c\n2 b\n1 a\n=\nbaa\n---\n3\nderp\n3\n1 p\n1 p\n1 p\n1 r\n2 r\n2 d\n=\ndeerde\n---\n3\nxxx\n2\n1 x\n8 x\n=\nxxxxxxx\n---\n3\nderp\n2\n2 p\n1 p\n1 d\n2 d\n=\nerdererp"}], "negative_code": [{"source_code": "class T\n def initialize(l, r)\n @l = l\n @r = r\n @m = (l + r) / 2\n @c = r - l\n end\n\n def l\n @pl = @pl || T.new(@l, @m)\n end\n\n def r\n @pr = @pr || T.new(@m, @r)\n end\n\n def c\n @c\n end\n\n def d(i)\n @c -= 1\n if @r - @l == 1\n @l\n elsif i < l.c\n l.d(i)\n else\n r.d(i - l.c)\n end\n end\nend\n\nr = gets.to_i\ns = gets.chop\nt = Array.new(128){T.new(0, r * s.size)}\nd = Array.new(128){[]}\ngets.to_i.times{|i|\n p, c = gets.split\n c = c.ord\n d[c] << t[c].d(p.to_i - 1)\n}\nd.each{|_|\n _.sort!.reverse!\n}\nt = [0] * 128\nz = []\nr.times{\n s.each_byte{|b|\n if t[b] == d[b][-1]\n d[b].pop\n else\n t[b] += 1\n z << b\n end\n }\n}\nputs z.map(&:chr) * ''\n"}, {"source_code": "k = gets().to_i\ns = gets().chomp\nname = s * k\n\nletter_entries = {}\n\nn = gets().to_i\nn.times do\n change = gets().chomp.split\n entry = change[0].to_i\n letter = change[1]\n \n letter_entries[letter] ||= []\n letter_entries[letter] << entry\nend\n\nletter_entries.each do |letter, entries|\n entries.sort!\n \n 1.upto(entries.size - 1) do |i|\n entries[i] += i\n end\nend\n\nname_letters = name.split('')\ncurrent_letter_entries = {}\n\n0.upto(name_letters.size-1) do |i|\n letter = name_letters[i]\n \n current_letter_entries[letter] ||= 0\n current_letter_entries[letter] += 1\n \n name_letters[i] = nil if letter_entries[letter].include?(current_letter_entries[letter])\nend\n\nputs name_letters.compact.join"}, {"source_code": "class String\n def char_distr\n split('').inject({}) { |hash, e| hash[e] ||= 0; hash[e] += 1; hash }\n end\n \n def delete_nth_entry(letter, entry)\n index = 0\n \n while entry > 0\n entry -= 1 if self[index] == letter\n index += 1\n end\n \n self[index-1] = ''\n self\n end\nend\n\ndef find_piece_and_index(letter, entry, char_distributions)\n index = 0\n sum = char_distributions[index][letter]\n prev_sum = 0\n while entry > sum && index < char_distributions.size\n index += 1\n prev_sum = sum\n sum += char_distributions[index][letter]\n end\n return index, (entry - prev_sum)\nend\n\nk = gets().to_i\ns = gets().chomp\npieces = []\nk.times { pieces << s.dup }\n\nchar_distribution = s.char_distr\nchar_distributions = []\nk.times { char_distributions << char_distribution.dup }\n\nn = gets().to_i\nn.times do \n change = gets().split\n entry = change[0].to_i\n letter = change[1]\n \n piece, local_entry = find_piece_and_index(letter, entry, char_distributions)\n pieces[piece].delete_nth_entry(letter, local_entry)\n char_distributions[piece][letter] -= 1\nend\n\np pieces.join('')"}, {"source_code": "k = gets().to_i\ns = gets().chomp\nname = s * k\n\nletter_entries = {}\n\nn = gets().to_i\nn.times do\n change = gets().chomp.split\n entry = change[0].to_i\n letter = change[1]\n \n letter_entries[letter] ||= []\n letter_entries[letter] << entry\nend\n\nletter_entries.each do |letter, entries|\n old_value = entries[0]\n 1.upto(entries.size - 1) do |i|\n entries[i] += i if entries[i] >= old_value\n end\nend\n\nname_letters = name.split('')\ncurrent_letter_entries = {}\n\n0.upto(name_letters.size-1) do |i|\n letter = name_letters[i]\n \n current_letter_entries[letter] ||= 0\n current_letter_entries[letter] += 1\n \n if letter_entries[letter] && letter_entries[letter].include?(current_letter_entries[letter])\n name_letters[i] = nil\n end\nend\n\nputs name_letters.compact.join"}, {"source_code": "k = gets().to_i\ns = gets().chomp\nname = s * k\n\nletter_entries = {}\n\nn = gets().to_i\nn.times do\n change = gets().chomp.split\n entry = change[0].to_i\n letter = change[1]\n \n letter_entries[letter] ||= []\n letter_entries[letter] << entry\nend\n\nletter_entries.each do |letter, entries|\n entries.sort!\n \n 1.upto(entries.size - 1) do |i|\n entries[i] += i\n end\nend\n\nname_letters = name.split('')\ncurrent_letter_entries = {}\n\n0.upto(name_letters.size-1) do |i|\n letter = name_letters[i]\n \n current_letter_entries[letter] ||= 0\n current_letter_entries[letter] += 1\n \n if letter_entries[letter] && letter_entries[letter].include?(current_letter_entries[letter])\n name_letters[i] = nil \n end\nend\n\nputs name_letters.compact.join"}, {"source_code": "class String\n def delete_nth_entries!(letter, entries)\n index = 0\n entry_index = 0\n current_entry = 0\n \n while entry_index < entries.size\n if self[index] == letter\n current_entry += 1\n if current_entry == entries[entry_index]\n self[index] = ''\n entry_index += 1\n end\n end\n index += 1\n end\n \n self\n end\nend\n\nk = gets().to_i\ns = gets().chomp\nname = s * k\n\nletter_entries = {}\n\nn = gets().to_i\nn.times do\n change = gets().chomp.split\n entry = change[0].to_i\n letter = change[1]\n \n letter_entries[letter] ||= []\n letter_entries[letter] << entry\nend\n\nletter_entries.each do |letter, entries|\n entries.sort!\n \n 1.upto(entries.size - 1) do |i|\n entries[i] += i\n end\n p letter_entries\n p name\n name.delete_nth_entries!(letter, entries)\nend\n\nputs name"}, {"source_code": "# http://codeforces.ru/contest/159/problem/C\n\ndef run(input = STDIN.read)\n lines = input.split(\"\\n\")\n\n k = lines[0].to_i\n s = lines[1]\n n = lines[2].to_i\n\n cmd = lines[3..-1]\n\n word = (s * k)\n\n h = {}\n l = {}\n\n i = 0\n word.each_char { |c|\n if h.has_key?(c) then\n h[c].push(i)\n else\n h[c] = [i]\n end\n i += 1\n }\n\n cmd.each { |c|\n (occurance, letter) = c.split(\" \")\n occurance = occurance.to_i\n\n while (pos = h[letter][occurance - 1]) == nil\n occurance += 1\n end\n \n word[pos] = \"0\"\n h[letter][occurance - 1] = nil\n }\n\n return word.delete(\"0\")\nend\n\nbegin\n require './testing.rb'\nrescue LoadError\n puts run\nend\n__END__\n2\nbac\n3\n2 a\n1 b\n2 c\n=\nacb\n---\n1\nabacaba\n4\n1 a\n1 c\n2 b\n1 a\n=\nbaa\n---\n3\nderp\n2\n1 p\n1 p\n1 p\n2 r\n2 r\n1 r\n2 d\n=\ndeede"}], "src_uid": "adbfdb0d2fd9e4eb48a27d43f401f0e0"} {"nl": {"description": "...Once upon a time a man came to the sea. The sea was stormy and dark. The man started to call for the little mermaid to appear but alas, he only woke up Cthulhu...Whereas on the other end of the world Pentagon is actively collecting information trying to predict the monster's behavior and preparing the secret super weapon. Due to high seismic activity and poor weather conditions the satellites haven't yet been able to make clear shots of the monster. The analysis of the first shot resulted in an undirected graph with n vertices and m edges. Now the world's best minds are about to determine whether this graph can be regarded as Cthulhu or not.To add simplicity, let's suppose that Cthulhu looks from the space like some spherical body with tentacles attached to it. Formally, we shall regard as Cthulhu such an undirected graph that can be represented as a set of three or more rooted trees, whose roots are connected by a simple cycle.It is guaranteed that the graph contains no multiple edges and self-loops. ", "input_spec": "The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ ). Each of the following m lines contains a pair of integers x and y, that show that an edge exists between vertices x and y (1 ≤ x, y ≤ n, x ≠ y). For each pair of vertices there will be at most one edge between them, no edge connects a vertex to itself.", "output_spec": "Print \"NO\", if the graph is not Cthulhu and \"FHTAGN!\" if it is.", "sample_inputs": ["6 6\n6 3\n6 4\n5 1\n2 5\n1 4\n5 4", "6 5\n5 6\n4 6\n3 1\n5 1\n1 2"], "sample_outputs": ["FHTAGN!", "NO"], "notes": "NoteLet us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., v - 1 and v, v and 1.A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).A rooted tree is a tree where one vertex is selected to be the root."}, "positive_code": [{"source_code": "n,m = gets.split(' ').map(&:to_i)\n\nsm = [];\nn.times do |i| sm[i] = [] end\n\nm.times do\n a,b = gets.split(' ').map(&:to_i)\n a -= 1\n b -= 1\n sm[a][b] = 1\nend\n\np = []\n$q = 0\ndef rec(id,p,sm,n)\n p[id] |= 0\n if p[id] == 1 then return end\n p[id] = 1\n $q += 1\n n.times do |i|\n sm[id][i] |= 0\n if sm[id][i] == 1 or sm[i][id] == 1 then\n rec(i,p,sm,n)\n end\n end\nend\n\nrec(0,p,sm,n)\nif($q == n and $q == m) then puts('FHTAGN!');\nelse puts('NO');\nend\n\n"}, {"source_code": "$n,$m=gets.split.map &:to_i\n$e=Array.new($n+1){[]}\n$m.times{a,b=gets.split.map &:to_i;$e[a]<2&&$n==$m&&dfs(1) ? \"FHTAGN!\" : \"NO\""}], "negative_code": [{"source_code": "$n,$m=gets.split.map &:to_i\n$e=Array.new($n+1){[]}\n$m.times{a,b=gets.split.map &:to_i;$e[a]<2&&$n==$m&&dfs(1) ? \"FHTAGN!\" : \"NO\""}, {"source_code": "$n,$m=gets.split.map &:to_i\n$e=Array.new($n+1){[]}\n$m.times{a,b=gets.split.map &:to_i;$e[a]< b } \n bb = elms[idx][1]\n #p [bb-aa, aa, s[aa..bb-1], idx]\n\n result = [bb-aa, aa] if bb-aa > result[0]\n\n break if r[idx].empty?\n elms[idx] = r[idx].shift\nend\n#p result\nputs result*\" \"\n"}], "negative_code": [{"source_code": "s = gets.chomp\nn = gets.to_i\nwords = []\nn.times do\n words << gets.chomp\nend\n\nlen = s.size\nr = []\nwords.each do |w|\n r2 = []\n idx = 0\n pos = 0\n while p2 = s.index(w, pos)\n r2 << [pos, p2+w.size-1] if pos > p2\n\n pos = p2 + 1\n end\n r2 << [pos, len]\n\n #p r2.map{|a,b| [a, b, s[a..b-1]]}\n r << r2\nend\n\nelms = r.map{|v|v.shift}\n\nresult = [0, 0]\nwhile true\n #p elms\n idx = 0\n elms.each_with_index{|(a,b),i| idx = i if elms[idx][0] < a }\n aa = elms[idx][0]\n\n idx = 0\n elms.each_with_index{|(a,b),i| idx = i if elms[idx][1] > b } \n bb = elms[idx][1]\n #p [bb-aa, aa, s[aa..bb-1], idx]\n\n result = [bb-aa, aa] if bb-aa > result[0]\n\n break if r[idx].empty?\n elms[idx] = r[idx].shift\nend\n#p result\nputs result*\" \"\n"}, {"source_code": "s = gets.chomp\nn = gets.to_i\nwords = []\nn.times do\n words << gets.chomp\nend\n\nlen = s.size\nr = []\nwords.each do |w|\n r2 = []\n idx = 0\n pos = 0\n while p2 = s.index(w, pos)\n r2 << [pos, p2+w.size-1] if pos < p2\n\n pos = p2 + 1\n end\n r2 << [pos, len]\n\n #p r2.map{|a,b| [a, b, s[a..b-1]]}\n r << r2\nend\n\nelms = r.map{|v|v.shift}\n\nresult = [0, 0]\nwhile true\n #p elms\n idx = 0\n elms.each_with_index{|(a,b),i| idx = i if elms[idx][0] < a }\n aa = elms[idx][0]\n\n idx = 0\n elms.each_with_index{|(a,b),i| idx = i if elms[idx][1] > b } \n bb = elms[idx][1]\n #p [bb-aa, aa, s[aa..bb-1], idx]\n\n result = [bb-aa, aa] if bb-aa > result[0]\n\n break if r[idx].empty?\n elms[idx] = r[idx].shift\nend\n#p result\nputs result*\" \"\n"}], "src_uid": "b5f5fc50e36b2afa3b5f16dacdf5710b"} {"nl": {"description": "One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs.For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2).Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs.", "input_spec": "The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs. It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order.", "output_spec": "If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number \"-1\" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction.", "sample_inputs": ["5\n1 2\n2 3\n3 4\n4 5\n5 1\n1 3\n2 4\n3 5\n4 1\n5 2", "6\n5 6\n4 3\n5 3\n2 4\n6 1\n3 1\n6 2\n2 5\n1 4\n3 6\n1 2\n4 5"], "sample_outputs": ["1 2 3 4 5", "1 2 4 5 3 6"], "notes": null}, "positive_code": [{"source_code": "def check ans\n used=[false]*$n\n ans.each {|i| used[i]=true}\n (3...$n).each {|i|\n $a[ans[i-1]].each {|j|\n if !used[j] && $a[j].include?(ans[i-2])\n ans<=2\n ans=[0]\n ans<=2\n ans=[0]\n ans<=2\n ans=[0]\n ans<=2\n ans=[0]\n ans<=2\n ans=[0]\n ans<=2\n ans=[0]\n ans<=2\n ans=[0]\n ans<=2\n ans=[0]\n ans<=2\n ans=[0]\n ans<2\n ans=[0]*$n\n ans[1]=i\n ans[2]=j\n if check ans\n ans.each {|k| print (k+1).to_s + ' '}\n exit\n end\n end\n }\n}\n\nputs -1"}, {"source_code": "require 'set'\n\ndef check ans\n (1...$n-1).each {|i|\n common=($a[ans[i]]-[ans[i-1],ans[(i+1)%$n]]) & $a[ans[(i+1)%$n]]\n return false if common.size==0\n\n if i+2<$n\n return false if common.size==0\n ans<4 || $a[input[1]].length>4\n puts -1\n exit\n end\nend\n\n$a[0].each {|i|\n $a[0].each {|j|\n if i!=j && $a[i].include?(j)\n ans=[0]\n ans<=2\n ans=[0]\n ans<0\n ans=[0]*$n\n ans[1]=i\n ans[2]=j\n if check ans\n ans.each {|k| print (k+1).to_s + ' '}\n exit\n end\n end\n }\n}\n\nputs -1"}, {"source_code": "require 'set'\n\ndef check ans\n used=[false]*$n\n 3.times {|i| used[i]=true}\n (3...$n).each {|i|\n $a[ans[i-1]].each {|j|\n if !used[j] && $a[j].include?(ans[i-2])\n ans[i]=j\n used[j]=true\n break\n end\n }\n return false if ans[i]==0\n }\n true\nend\n\n$n=gets.to_i\n$a=Array.new($n) {Set.new}\n\n($n*2).times do\n input=gets.split.map{|i| i.to_i-1}\n $a[input[0]]<=2\n ans=[0]*$n\n ans[1]=i\n ans[2]=j\n if check ans\n ans.each {|k| print (k+1).to_s + ' '}\n exit\n end\n end\n }\n}\n\nputs -1"}, {"source_code": "require 'set'\n\ndef check ans\n (1...$n-1).each {|i|\n common=($a[ans[i]]-[ans[i-1],ans[(i+1)%$n]]) & $a[ans[(i+1)%$n]]\n return false if common.size==0\n\n if i+2<$n\n common-=ans\n return false if common.size!=1\n ans<4 || $a[input[1]].length>4\n puts -1\n exit\n end\nend\n\n$a[0].each {|i|\n $a[0].each {|j|\n if i!=j && $a[i].include?(j)\n ans=[0]\n ans<4 || $a[input[1]].length>4\n puts -1\n exit\n end\nend\n\n$a[0].each {|i|\n $a[0].each {|j|\n if i!=j && $a[i].include?(j)\n ans=[0]\n ans<= [a[1], b[1]].min && f[1] <= [a[1], b[1]].max) \n ans += 2 if a[1] == f[1] && a[1] == b[1] && (f[0] >= [a[0], b[0]].min && f[0] <= [a[0], b[0]].max) \n puts \"#{ans}\\n\"\n end\nend\n\n"}, {"source_code": "def incl(a, b, f, i)\n a = a[i]\n b = b[i]\n a, b = b, a if b < a\n (a..b).include?(f[i])\nend\n\ngets.to_i.times {\n gets\n a = gets.split.map!(&:to_i)\n b = gets.split.map!(&:to_i)\n f = gets.split.map!(&:to_i)\n x = a.zip(b).map { (_1 - _2).abs }\n\n ans = x.sum\n ans += 2 if x[0] == 0 && a[0] == f[0] && incl(a, b, f, 1)\n ans += 2 if x[1] == 0 && a[1] == f[1] && incl(a, b, f, 0)\n\n p ans\n}\n"}], "negative_code": [], "src_uid": "6422d76f71702e77808b1cc041962bb8"} {"nl": {"description": "There are several days left before the fiftieth birthday of a famous Berland's writer Berlbury. In this connection the local library decided to make an exposition of the works of this famous science-fiction writer. It was decided as well that it is necessary to include into the exposition only those books that were published during a particular time period. It is obvious that if the books differ much in size, the visitors will not like it. That was why the organizers came to the opinion, that the difference between the highest and the lowest books in the exposition should be not more than k millimeters.The library has n volumes of books by Berlbury, arranged in chronological order of their appearance. The height of each book in millimeters is know, it is hi. As Berlbury is highly respected in the city, the organizers want to include into the exposition as many books as possible, and to find out what periods of his creative work they will manage to cover. You are asked to help the organizers cope with this hard task.", "input_spec": "The first line of the input data contains two integer numbers separated by a space n (1 ≤ n ≤ 105) and k (0 ≤ k ≤ 106) — the amount of books by Berlbury in the library, and the maximum allowed height difference between the lowest and the highest books. The second line contains n integer numbers separated by a space. Each number hi (1 ≤ hi ≤ 106) is the height of the i-th book in millimeters.", "output_spec": "In the first line of the output data print two numbers a and b (separate them by a space), where a is the maximum amount of books the organizers can include into the exposition, and b — the amount of the time periods, during which Berlbury published a books, and the height difference between the lowest and the highest among these books is not more than k milllimeters. In each of the following b lines print two integer numbers separated by a space — indexes of the first and the last volumes from each of the required time periods of Berlbury's creative work.", "sample_inputs": ["3 3\n14 12 10", "2 0\n10 10", "4 5\n8 19 10 13"], "sample_outputs": ["2 2\n1 2\n2 3", "2 1\n1 2", "2 1\n3 4"], "notes": null}, "positive_code": [{"source_code": "number_of_books,limit_height = gets.split.collect! {|x| x.to_i}\n\nbooks_height = []\nbooks_height = gets.split.collect! {|x| x.to_i}\n\nmax_books = 1\nresult = []\nmax_h = min_h = books_height[0]\nmax_i = min_i = 0\nmax_sub_h = 0\nmin_sub_h = 1000000\nmax_sub_i = min_sub_i = 0\ncount = 0\ni = 0\nwhile i < number_of_books\n if max_sub_i > i\n max_h = max_sub_h\n max_i = max_sub_i\n else\n max_h = books_height[i]\n max_i = i\n end\n if min_sub_i > i\n min_h = min_sub_i\n min_i = min_sub_i\n else\n min_h = books_height[i]\n min_i = i\n end\n if count <= i\n count = i + 1\n end\n max_sub_h = 0\n min_sub_h = 1000000\n max_sub_i = min_sub_i = 0\n while count < number_of_books\n if max_h <= books_height[count]\n max_h = books_height[count]\n max_i = count\n elsif min_h >= books_height[count]\n min_h = books_height[count]\n min_i = count\n elsif max_i < min_i && max_sub_h < max_h\n max_sub_h = books_height[count]\n max_sub_i = count\n elsif min_i < max_i && min_sub_h > min_h\n min_sub_h = books_height[count]\n min_sub_i = count\n end\n if (max_h - min_h).abs > limit_height\n break\n end\n count += 1\n end\n if count - i >= max_books\n if count - i > max_books\n result.clear\n end\n max_books = count - i\n result.push [i + 1,count]\n end\n if count == number_of_books || number_of_books - i <= max_books\n break\n elsif max_i >= min_i\n i = min_i + 1\n elsif min_i > max_i\n i = max_i + 1\n else\n i += 1\n end\n count -= 1\nend\nputs max_books.to_s + ' ' + result.length.to_s\nresult.length.times do |i|\n puts result[i][0].to_s + ' ' + result[i][1].to_s\nend\n\n"}, {"source_code": "number_of_books,limit_height = gets.split.collect! {|x| x.to_i}\n\nbooks_height = []\nbooks_height = gets.split.collect! {|x| x.to_i}\n\nmax_books = 1\nresult = []\nmax_h = min_h = books_height[0]\nmax_i = min_i = 0\nmax_sub_h = 0\nmin_sub_h = 1000000\nmax_sub_i = min_sub_i = 0\ncount = 0\ni = 0\nwhile i < number_of_books\n#(number_of_books).times do |i|\n# count = i + 1\n# max_h = min_h = books_height[i]\n# max_i = min_i = count - 1\n if max_sub_i > i\n max_h = max_sub_h\n max_i = max_sub_i\n else\n max_h = books_height[i]\n max_i = i\n# count = i + 1\n end\n if min_sub_i > i\n min_h = min_sub_i\n min_i = min_sub_i\n else\n min_h = books_height[i]\n min_i = i\n# count = i + 1\n end\n if count <= i\n count = i + 1\n end\n max_sub_h = 0\n min_sub_h = 1000000\n max_sub_i = min_sub_i = 0\n while count < number_of_books\n if max_h <= books_height[count]\n max_h = books_height[count]\n max_i = count\n elsif min_h >= books_height[count]\n min_h = books_height[count]\n min_i = count\n elsif max_i < min_i && max_sub_h < max_h\n max_sub_h = books_height[count]\n max_sub_i = count\n elsif min_i < max_i && min_sub_h > min_h\n min_sub_h = books_height[count]\n min_sub_i = count\n end\n if (max_h - min_h).abs > limit_height\n break\n end\n count += 1\n end\n if count - i >= max_books\n if count - i > max_books\n result.clear\n end\n max_books = count - i\n result.push [i + 1,count]\n end\n if count == number_of_books || number_of_books - i <= max_books\n break\n elsif max_i >= min_i\n i = min_i + 1\n elsif min_i > max_i\n i = max_i + 1\n else\n i += 1\n end\n count -= 1\nend\nputs max_books.to_s + ' ' + result.length.to_s\nresult.length.times do |i|\n puts result[i][0].to_s + ' ' + result[i][1].to_s\nend\n\n"}], "negative_code": [{"source_code": "number_of_books,limit_height = gets.split.collect! {|x| x.to_i}\n\nbooks_height = []\nbooks_height = gets.split.collect! {|x| x.to_i}\n\nmax_books = 1\nresult = []\nmax_h = min_h = books_height[0]\nmax_i = min_i = 0\nmax_sub_h = 0\nmin_sub_h = 1000000\nmax_sub_i = min_sub_i = 0\ncount = 0\ni = 0\nwhile i < number_of_books\n#(number_of_books).times do |i|\n# count = i + 1\n# max_h = min_h = books_height[i]\n# max_i = min_i = count - 1\n if max_sub_i > i\n max_h = max_sub_h\n max_i = max_sub_i\n else\n max_h = books_height[i]\n max_i = i\n# count = i + 1\n end\n if min_sub_i > i\n min_h = min_sub_i\n min_i = min_sub_i\n else\n min_h = books_height[i]\n min_i = i\n# count = i + 1\n end\n max_sub_h = 0\n min_sub_h = 1000000\n max_sub_i = min_sub_i = 0\n while count < number_of_books\n if max_h <= books_height[count]\n max_h = books_height[count]\n max_i = count\n elsif min_h >= books_height[count]\n min_h = books_height[count]\n min_i = count\n elsif max_i < min_i && max_sub_h < max_h\n max_sub_h = books_height[count]\n max_sub_i = count\n elsif min_i < max_i && min_sub_h > min_h\n min_sub_h = books_height[count]\n min_sub_i = count\n end\n if (max_h - min_h).abs > limit_height\n break\n end\n count += 1\n end\n if count - i >= max_books\n if count - i > max_books\n result.clear\n end\n max_books = count - i\n result.push [i + 1,count]\n end\n if count == number_of_books || number_of_books - i <= max_books\n break\n elsif max_i >= min_i\n i = min_i + 1\n elsif min_i > max_i\n i = max_i + 1\n else\n i += 1\n end\nend\nputs max_books.to_s + ' ' + result.length.to_s\nresult.length.times do |i|\n puts result[i][0].to_s + ' ' + result[i][1].to_s\nend\n\n"}, {"source_code": "number_of_books,limit_height = gets.split.collect! {|x| x.to_i}\n\nbooks_height = []\nbooks_height = gets.split.collect! {|x| x.to_i}\n\nmax_books = 0\nresult = []\n\n(number_of_books - 1).times do |i|\n count = i + 1\n while count < number_of_books\n if (books_height[i] - books_height[count]).abs > limit_height\n break\n end\n count += 1\n end\n if count - i >= max_books\n if count - i > max_books\n result.clear\n end\n max_books = count - i\n result.push [i + 1,count]\n end\nend\nputs max_books.to_s + ' ' + result.length.to_s\nresult.length.times do |i|\n puts result[i][0].to_s + ' ' + result[i][1].to_s\nend\n"}, {"source_code": "number_of_books,limit_height = gets.split.collect! {|x| x.to_i}\n\nbooks_height = []\nbooks_height = gets.split.collect! {|x| x.to_i}\n\nmax_books = 1\nresult = []\nmax_h = min_h = books_height[0]\nmax_i = min_i = 0\nmax_sub_h = 0\nmin_sub_h = 1000000\nmax_sub_i = min_sub_i = 0\ncount = 0\ni = 0\nwhile i < number_of_books\n#(number_of_books).times do |i|\n# count = i + 1\n# max_h = min_h = books_height[i]\n# max_i = min_i = count - 1\n if max_sub_i > i\n max_h = max_sub_h\n max_i = max_sub_i\n else\n max_h = books_height[i]\n max_i = i\n# count = i + 1\n end\n if min_sub_i > i\n min_h = min_sub_i\n min_i = min_sub_i\n else\n min_h = books_height[i]\n min_i = i\n# count = i + 1\n end\n if count == i\n count += 1\n end\n max_sub_h = 0\n min_sub_h = 1000000\n max_sub_i = min_sub_i = 0\n while count < number_of_books\n if max_h <= books_height[count]\n max_h = books_height[count]\n max_i = count\n elsif min_h >= books_height[count]\n min_h = books_height[count]\n min_i = count\n elsif max_i < min_i && max_sub_h < max_h\n max_sub_h = books_height[count]\n max_sub_i = count\n elsif min_i < max_i && min_sub_h > min_h\n min_sub_h = books_height[count]\n min_sub_i = count\n end\n if (max_h - min_h).abs > limit_height\n break\n end\n count += 1\n end\n if count - i >= max_books\n if count - i > max_books\n result.clear\n end\n max_books = count - i\n result.push [i + 1,count]\n end\n if count == number_of_books || number_of_books - i <= max_books\n break\n elsif max_i >= min_i\n i = min_i + 1\n elsif min_i > max_i\n i = max_i + 1\n else\n i += 1\n end\nend\nputs max_books.to_s + ' ' + result.length.to_s\nresult.length.times do |i|\n puts result[i][0].to_s + ' ' + result[i][1].to_s\nend\n\n"}, {"source_code": "number_of_books,limit_height = gets.split.collect! {|x| x.to_i}\n\nbooks_height = []\nbooks_height = gets.split.collect! {|x| x.to_i}\n\nmax_books = 0\nresult = []\n\n(number_of_books - 1).times do |i|\n count = i + 1\n max_h = min_h = books_height[i]\n while count < number_of_books\n if max_h < books_height[count]\n max_h = books_height[count]\n elsif min_h > books_height[count]\n min_h = books_height[count]\n end\n if (max_h - min_h).abs >= limit_height\n break\n end\n count += 1\n end\n if count - i >= max_books && count - i > 1\n if count - i > max_books\n result.clear\n end\n max_books = count - i\n result.push [i + 1,count]\n end\nend\nputs max_books.to_s + ' ' + result.length.to_s\nresult.length.times do |i|\n puts result[i][0].to_s + ' ' + result[i][1].to_s\nend\n"}, {"source_code": "number_of_books,limit_height = gets.split.collect! {|x| x.to_i}\n\nbooks_height = []\nbooks_height = gets.split.collect! {|x| x.to_i}\n\nmax_books = 0\nresult = []\n\n(number_of_books - 1).times do |i|\n count = i + 1\n max_h = min_h = books_height[i]\n while count < number_of_books\n if max_h < books_height[count]\n max_h = books_height[count]\n elsif min_h > books_height[count]\n min_h = books_height[count]\n end\n if (max_h - min_h).abs > limit_height\n break\n end\n count += 1\n end\n if count - i >= max_books\n if count - i > max_books\n result.clear\n end\n max_books = count - i\n result.push [i + 1,count]\n end\nend\nputs max_books.to_s + ' ' + result.length.to_s\nresult.length.times do |i|\n puts result[i][0].to_s + ' ' + result[i][1].to_s\nend\n"}, {"source_code": "number_of_books,limit_height = gets.split.collect! {|x| x.to_i}\n\nbooks_height = []\nbooks_height = gets.split.collect! {|x| x.to_i}\n\nmax_books = 1\nresult = []\n\ni = 0\nwhile i < number_of_books\n#(number_of_books).times do |i|\n count = i + 1\n max_h = min_h = books_height[i]\n max_i = min_i = count\n while count < number_of_books\n if max_h < books_height[count]\n max_h = books_height[count]\n max_i = count\n elsif min_h > books_height[count]\n min_h = books_height[count]\n min_i = count\n end\n if (max_h - min_h).abs > limit_height\n break\n end\n count += 1\n end\n if count - i >= max_books\n if count - i > max_books\n result.clear\n end\n max_books = count - i\n result.push [i + 1,count]\n end\n if count == number_of_books\n break\n elsif max_i >= min_i\n i += max_i\n elsif min_i > max_i\n i += min_i\n else\n i += 1\n end\nend\nputs max_books.to_s + ' ' + result.length.to_s\nresult.length.times do |i|\n puts result[i][0].to_s + ' ' + result[i][1].to_s\nend\n\n"}, {"source_code": "number_of_books,limit_height = gets.split.collect! {|x| x.to_i}\n\nbooks_height = []\nbooks_height = gets.split.collect! {|x| x.to_i}\n\nmax_books = 0\nresult = []\n\n(number_of_books - 1).times do |i|\n count = i + 1\n while count < number_of_books\n if (books_height[i] - books_height[count]).abs > limit_height\n break\n end\n count += 1\n end\n if count - i >= max_books && count - i > 1\n if count - i > max_books\n result.clear\n end\n max_books = count - i\n result.push [i + 1,count]\n end\nend\nputs max_books.to_s + ' ' + result.length.to_s\nresult.length.times do |i|\n puts result[i][0].to_s + ' ' + result[i][1].to_s\nend\n"}, {"source_code": "number_of_books,limit_height = gets.split.collect! {|x| x.to_i}\n\nbooks_height = []\nbooks_height = gets.split.collect! {|x| x.to_i}\n\nmax_books = 1\nresult = []\n\ni = 0\nwhile i < number_of_books\n#(number_of_books).times do |i|\n count = i + 1\n max_h = min_h = books_height[i]\n max_i = min_i = count\n while count < number_of_books\n if max_h < books_height[count]\n max_h = books_height[count]\n max_i = count\n elsif min_h > books_height[count]\n min_h = books_height[count]\n min_i = count\n end\n if (max_h - min_h).abs > limit_height\n break\n end\n count += 1\n end\n if count - i >= max_books\n if count - i > max_books\n result.clear\n end\n max_books = count - i\n result.push [i + 1,count]\n end\n if count == number_of_books\n break\n elsif max_i >= min_i\n i = max_i\n elsif min_i > max_i\n i = min_i\n else\n i += 1\n end\nend\nputs max_books.to_s + ' ' + result.length.to_s\nresult.length.times do |i|\n puts result[i][0].to_s + ' ' + result[i][1].to_s\nend\n\n"}, {"source_code": "number_of_books,limit_height = gets.split.collect! {|x| x.to_i}\n\nbooks_height = []\nbooks_height = gets.split.collect! {|x| x.to_i}\n\nmax_books = 0\nresult = []\n\n(number_of_books - 1).times do |i|\n count = i + 1\n max_h = min_h = books_height[i]\n while count < number_of_books\n if max_h < books_height[count]\n max_h = books_height[count]\n elsif min_h > books_height[count]\n min_h = books_height[count]\n end\n if (max_h - min_h).abs > limit_height\n count += 1\n break\n end\n count += 1\n end\n count -= 1\n if count - i >= max_books\n if count - i > max_books\n result.clear\n end\n max_books = count - i\n result.push [i + 1,count]\n end\nend\nputs max_books.to_s + ' ' + result.length.to_s\nresult.length.times do |i|\n puts result[i][0].to_s + ' ' + result[i][1].to_s\nend\n"}, {"source_code": "number_of_books,limit_height = gets.split.collect! {|x| x.to_i}\n\nbooks_height = []\nbooks_height = gets.split.collect! {|x| x.to_i}\n\nmax_books = 1\nresult = []\nmax_h = min_h = books_height[0]\nmax_i = min_i = 0\nmax_sub_h = 0\nmin_sub_h = 1000000\nmax_sub_i = min_sub_i = 0\ncount = 0\ni = 0\nwhile i < number_of_books\n#(number_of_books).times do |i|\n# count = i + 1\n# max_h = min_h = books_height[i]\n# max_i = min_i = count - 1\n count += 1\n if max_sub_i > i\n max_h = max_sub_h\n max_i = max_sub_i\n else\n max_h = books_height[i]\n max_i = i\n end\n if min_sub_i > i\n min_h = min_sub_i\n min_i = min_sub_i\n else\n min_h = books_height[i]\n min_i = i\n end\n max_sub_h = 0\n min_sub_h = 1000000\n max_sub_i = min_sub_i = 0\n while count < number_of_books\n if max_h <= books_height[count]\n max_h = books_height[count]\n max_i = count\n elsif min_h >= books_height[count]\n min_h = books_height[count]\n min_i = count\n elsif max_i < min_i && max_sub_h < max_h\n max_sub_h = books_height[count]\n max_sub_i = count\n elsif min_i min_h\n min_sub_h = books_height[count]\n min_sub_i = count\n end\n if (max_h - min_h).abs > limit_height\n break\n end\n count += 1\n end\n if count - i >= max_books\n if count - i > max_books\n result.clear\n end\n max_books = count - i\n result.push [i + 1,count]\n end\n if count == number_of_books || number_of_books - i <= max_books\n break\n elsif max_i >= min_i\n i = min_i + 1\n elsif min_i > max_i\n i = max_i + 1\n else\n i += 1\n end\nend\nputs max_books.to_s + ' ' + result.length.to_s\nresult.length.times do |i|\n puts result[i][0].to_s + ' ' + result[i][1].to_s\nend\n\n"}, {"source_code": "number_of_books,limit_height = gets.split.collect! {|x| x.to_i}\n\nbooks_height = []\nbooks_height = gets.split.collect! {|x| x.to_i}\n\nmax_books = 1\nresult = []\n\ni = 0\nwhile i < number_of_books\n#(number_of_books).times do |i|\n count = i + 1\n max_h = min_h = books_height[i]\n max_i = min_i = count\n while count < number_of_books\n if max_h < books_height[count]\n max_h = books_height[count]\n max_i = count\n elsif min_h > books_height[count]\n min_h = books_height[count]\n min_i = count\n end\n if (max_h - min_h).abs > limit_height\n break\n end\n count += 1\n end\n if count - i >= max_books\n if count - i > max_books\n result.clear\n end\n max_books = count - i\n result.push [i + 1,count]\n end\n if count == number_of_books || max_books > number_of_books - count\n break\n elsif max_i >= min_i\n i = min_i\n elsif min_i > max_i\n i = max_i\n else\n i += 1\n end\nend\nputs max_books.to_s + ' ' + result.length.to_s\nresult.length.times do |i|\n puts result[i][0].to_s + ' ' + result[i][1].to_s\nend\n\n"}, {"source_code": "number_of_books,limit_height = gets.split.collect! {|x| x.to_i}\n\nbooks_height = []\nbooks_height = gets.split.collect! {|x| x.to_i}\n\nmax_books = 0\nresult = []\n\n(number_of_books - 1).times do |i|\n count = i + 1\n max_h = min_h = books_height[i]\n while count < number_of_books\n if max_h < books_height[count]\n max_h = books_height[count]\n elsif min_h > books_height[count]\n min_h = books_height[count]\n end\n if (max_h - min_h).abs > limit_height\n break\n end\n count += 1\n end\n if count - i >= max_books && count - i > 1\n if count - i > max_books\n result.clear\n end\n max_books = count - i\n result.push [i + 1,count]\n end\nend\nputs max_books.to_s + ' ' + result.length.to_s\nresult.length.times do |i|\n puts result[i][0].to_s + ' ' + result[i][1].to_s\nend\n"}, {"source_code": "number_of_books,limit_height = gets.split.collect! {|x| x.to_i}\n\nbooks_height = []\nbooks_height = gets.split.collect! {|x| x.to_i}\n\nmax_books = 1\nresult = []\n\nif number_of_books == 1\n result.push [1,1]\nend\n\n(number_of_books).times do |i|\n count = i + 1\n max_h = min_h = books_height[i]\n while count < number_of_books\n if max_h < books_height[count]\n max_h = books_height[count]\n elsif min_h > books_height[count]\n min_h = books_height[count]\n end\n if (max_h - min_h).abs > limit_height\n break\n end\n count += 1\n end\n if count - i >= max_books\n if count - i > max_books\n result.clear\n end\n max_books = count - i\n result.push [i + 1,count]\n end\nend\nputs max_books.to_s + ' ' + result.length.to_s\nresult.length.times do |i|\n puts result[i][0].to_s + ' ' + result[i][1].to_s\nend\n"}, {"source_code": "number_of_books,limit_height = gets.split.collect! {|x| x.to_i}\n\nbooks_height = []\nbooks_height = gets.split.collect! {|x| x.to_i}\n\nmax_books = 1\nresult = []\n\nif number_of_books == 1\n result.push [1,1]\nend\n\n(number_of_books - 1).times do |i|\n count = i + 1\n max_h = min_h = books_height[i]\n while count < number_of_books\n if max_h < books_height[count]\n max_h = books_height[count]\n elsif min_h > books_height[count]\n min_h = books_height[count]\n end\n if (max_h - min_h).abs > limit_height\n break\n end\n count += 1\n end\n if count - i >= max_books\n if count - i > max_books\n result.clear\n end\n max_books = count - i\n result.push [i + 1,count]\n end\nend\nputs max_books.to_s + ' ' + result.length.to_s\nresult.length.times do |i|\n puts result[i][0].to_s + ' ' + result[i][1].to_s\nend\n"}, {"source_code": "number_of_books,limit_height = gets.split.collect! {|x| x.to_i}\n\nbooks_height = []\nbooks_height = gets.split.collect! {|x| x.to_i}\n\nmax_books = 1\nresult = []\n\ni = 0\nwhile i < number_of_books\n#(number_of_books).times do |i|\n count = i + 1\n max_h = min_h = books_height[i]\n max_i = min_i = count\n while count < number_of_books\n if max_h < books_height[count]\n max_h = books_height[count]\n max_i = count\n elsif min_h > books_height[count]\n min_h = books_height[count]\n min_i = count\n end\n if (max_h - min_h).abs > limit_height\n break\n end\n count += 1\n max_i -= 1\n min_i -= 1\n end\n if count - i >= max_books\n if count - i > max_books\n result.clear\n end\n max_books = count - i\n result.push [i + 1,count]\n end\n if count == number_of_books\n break\n elsif max_i >= min_i\n p i\n i = max_i\n elsif min_i > max_i\n i = min_i\n else\n i += 1\n end\nend\nputs max_books.to_s + ' ' + result.length.to_s\nresult.length.times do |i|\n puts result[i][0].to_s + ' ' + result[i][1].to_s\nend\n\n"}], "src_uid": "bc8b4b74c2f2d486e2d2f03982ef1013"} {"nl": {"description": "You are given an array $$$a_1, a_2, \\dots , a_n$$$, which is sorted in non-decreasing order ($$$a_i \\le a_{i + 1})$$$. Find three indices $$$i$$$, $$$j$$$, $$$k$$$ such that $$$1 \\le i < j < k \\le n$$$ and it is impossible to construct a non-degenerate triangle (a triangle with nonzero area) having sides equal to $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$ (for example it is possible to construct a non-degenerate triangle with sides $$$3$$$, $$$4$$$ and $$$5$$$ but impossible with sides $$$3$$$, $$$4$$$ and $$$7$$$). If it is impossible to find such triple, report it.", "input_spec": "The first line contains one integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \\le n \\le 5 \\cdot 10^4$$$) — the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \\dots , a_n$$$ ($$$1 \\le a_i \\le 10^9$$$; $$$a_{i - 1} \\le a_i$$$) — the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.", "output_spec": "For each test case print the answer to it in one line. If there is a triple of indices $$$i$$$, $$$j$$$, $$$k$$$ ($$$i < j < k$$$) such that it is impossible to construct a non-degenerate triangle having sides equal to $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$, print that three indices in ascending order. If there are multiple answers, print any of them. Otherwise, print -1.", "sample_inputs": ["3\n7\n4 6 11 11 15 18 20\n4\n10 10 10 11\n3\n1 1 1000000000"], "sample_outputs": ["2 3 6\n-1\n1 2 3"], "notes": "NoteIn the first test case it is impossible with sides $$$6$$$, $$$11$$$ and $$$18$$$. Note, that this is not the only correct answer.In the second test case you always can construct a non-degenerate triangle."}, "positive_code": [{"source_code": "gets.to_i.times do\n gets\n arr = gets.split(' ').map(&:to_i)\n \n puts arr[0] + arr[1] > arr.last ? -1 : \"1 2 #{arr.length}\"\nend\n"}, {"source_code": "\nt = gets.chomp.to_i\nt.times {\n n = gets.chomp.to_i\n arr = gets.chomp.split(' ').map(&:to_i)\n if arr[n - 1] >= (arr[0] + arr[1])\n puts \"1 2 #{n}\"\n else\n puts '-1'\n end\n}\n\n"}, {"source_code": "t = gets.to_i\nans = []\nt.times do\n n = gets.to_i\n a = gets.chomp.split(\" \").map(&:to_i)\n if a[0] + a[1] <= a[-1]\n ans << [1,2,n]\n else\n ans << [-1]\n end\nend\nans.each do |x|\n x.each do |v|\n print \"#{v} \"\n end\n puts \"\"\nend\n"}, {"source_code": "gets.to_i.times do\n gets\n arr = gets.split(' ').map(&:to_i)\n \n puts arr[0] + arr[1] > arr.last ? -1 : \"1 2 #{arr.length}\"\nend"}], "negative_code": [{"source_code": "t = gets.chomp.to_i\nt.times {\n n = gets.chomp.to_i\n arr = gets.chomp.split(' ').map(&:to_i)\n if arr[n - 1] > (arr[0] + arr[1])\n puts '1 2 3'\n else\n puts '-1'\n end\n}"}, {"source_code": "\nt = gets.chomp.to_i\nt.times {\n n = gets.chomp.to_i\n arr = gets.chomp.split(' ').map(&:to_i)\n if arr[n - 1] >= (arr[0] + arr[1])\n puts '1 2 3'\n else\n puts '-1'\n end\n}\n\n"}], "src_uid": "341555349b0c1387334a0541730159ac"} {"nl": {"description": "Casimir has a string $$$s$$$ which consists of capital Latin letters 'A', 'B', and 'C' only. Each turn he can choose to do one of the two following actions: he can either erase exactly one letter 'A' and exactly one letter 'B' from arbitrary places of the string (these letters don't have to be adjacent); or he can erase exactly one letter 'B' and exactly one letter 'C' from arbitrary places in the string (these letters don't have to be adjacent). Therefore, each turn the length of the string is decreased exactly by $$$2$$$. All turns are independent so for each turn, Casimir can choose any of two possible actions.For example, with $$$s$$$ $$$=$$$ \"ABCABC\" he can obtain a string $$$s$$$ $$$=$$$ \"ACBC\" in one turn (by erasing the first occurrence of 'B' and the second occurrence of 'A'). There are also many other options for a turn aside from this particular example.For a given string $$$s$$$ determine whether there is a sequence of actions leading to an empty string. In other words, Casimir's goal is to erase all letters from the string. Is there a way to do this?", "input_spec": "The first line contains an integer $$$t$$$ ($$$1 \\le t \\le 1000$$$) — the number of test cases. Each test case is described by one string $$$s$$$, for which you need to determine if it can be fully erased by some sequence of turns. The string $$$s$$$ consists of capital letters 'A', 'B', 'C' and has a length from $$$1$$$ to $$$50$$$ letters, inclusive.", "output_spec": "Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to a test case should be YES if there is a way to fully erase the corresponding string and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes, and YES will all be recognized as positive answers).", "sample_inputs": ["6\nABACAB\nABBA\nAC\nABC\nCABCBB\nBCBCBCBCBCBCBCBC"], "sample_outputs": ["NO\nYES\nNO\nNO\nYES\nYES"], "notes": null}, "positive_code": [{"source_code": "test_case = gets.to_i\r\n(1..test_case).each do |index|\r\n str = gets\r\n length = str.length - 1\r\n half = length / 2\r\n a = b = c = 0\r\n str.chars.each do |char|\r\n if char == 'A'\r\n a += 1\r\n elsif char == 'B'\r\n b += 1\r\n else\r\n c += 1\r\n end\r\n end\r\n if length % 2 == 0 && half == b\r\n puts 'YES'\r\n else\r\n puts 'NO'\r\n end\r\nend"}, {"source_code": "t = gets.chomp.to_i\r\nfor _ in 0...t\r\n string = gets.chomp\r\n count_a, count_b, count_c = 0, 0, 0\r\n string.each_char{|char|\r\n if char == \"A\"\r\n count_a += 1\r\n elsif char == \"B\"\r\n count_b += 1\r\n else\r\n count_c += 1\r\n end\r\n }\r\n if count_a + count_c == count_b\r\n puts \"YES\"\r\n else\r\n puts \"NO\"\r\n end\r\nend"}, {"source_code": "t = gets.chomp.to_i\r\nfor _ in 0...t\r\n string = gets.chomp\r\n count_a, count_b, count_c = string.count(\"A\"), string.count(\"B\"), string.count(\"C\")\r\n if count_a + count_c == count_b\r\n puts \"YES\"\r\n else\r\n puts \"NO\"\r\n end\r\nend"}, {"source_code": "def solve\r\n str = gets.strip\r\n if str.split('').select { |e| e == 'B' }.length * 2 == str.length\r\n puts \"YES\"\r\n else\r\n puts \"NO\"\r\n end\r\nend\r\n\r\ngets.strip.to_i.times {\r\n solve\r\n}"}, {"source_code": "def solve\r\n str = gets.strip\r\n a = str.split('').select { |e| e == 'A' }\r\n b = str.split('').select { |e| e == 'B' }\r\n c = str.split('').select { |e| e == 'C' }\r\n if a.length.zero?\r\n if b.length == c.length\r\n puts \"YES\"\r\n return\r\n end\r\n end\r\n\r\n if c.length.zero?\r\n if b.length == a.length\r\n puts \"YES\"\r\n return\r\n end\r\n end\r\n\r\n res = b.length\r\n res -= a.length\r\n res -= c.length\r\n if res.zero?\r\n puts \"YES\"\r\n else\r\n puts \"NO\"\r\n end\r\n \r\nend\r\n\r\ngets.strip.to_i.times {\r\n solve\r\n}"}, {"source_code": "# 6\r\n# ABACAB\r\n# ABBA\r\n# AC\r\n# ABC\r\n# CABCBB\r\n# BCBCBCBCBCBCBCBC\r\n\r\nn = gets.to_i\r\nputs(n.times.map do\r\n s = gets.strip\r\n loop do\r\n if s.include?('B') && s.include?('C')\r\n s = s.sub('B', '')\r\n s = s.sub('C', '')\r\n elsif s.include?('B') && s.include?('A')\r\n s = s.sub('B', '')\r\n s = s.sub('A', '')\r\n else\r\n break\r\n end\r\n end\r\n s.empty? ? 'YES' : 'NO'\r\nend.join(\"\\n\"))\r\n"}], "negative_code": [{"source_code": "test_case = gets.to_i\r\n(1...test_case).each do |index|\r\n str = gets\r\n length = str.length - 1\r\n half = length / 2\r\n a = b = c = 0\r\n str.chars.each do |char|\r\n if char == 'A'\r\n a += 1\r\n elsif char == 'B'\r\n b += 1\r\n else\r\n c += 1\r\n end\r\n end\r\n if length % 2 == 0 && half == b\r\n puts 'YES'\r\n else\r\n puts 'NO'\r\n end\r\nend"}, {"source_code": "test_case = gets.to_i\r\n(1...5).each do |index|\r\n str = gets\r\n length = str.length - 1\r\n half = length / 2\r\n a = b = c = 0\r\n str.chars.each do |char|\r\n if char == 'A'\r\n a += 1\r\n elsif char == 'B'\r\n b += 1\r\n else\r\n c += 1\r\n end\r\n end\r\n if length % 2 == 0 && half == b\r\n puts 'YES'\r\n else\r\n puts 'NO'\r\n end\r\nend"}, {"source_code": "test_case = gets.to_i\r\n(1...5).each do |index|\r\n str = gets\r\n length = str.length\r\n half = length / 2\r\n a = b = c = 0\r\n str.chars.each do |char|\r\n if char == 'A'\r\n a += 1\r\n elsif char == 'B'\r\n b += 1\r\n else\r\n c += 1\r\n end\r\n end\r\n if length % 2 == 0 && half == b\r\n puts 'YES'\r\n else\r\n puts 'NO'\r\n end\r\nend"}], "src_uid": "ca6b162f945d4216055bf92d7263dbd5"} {"nl": {"description": "Are you going to Scarborough Fair?Parsley, sage, rosemary and thyme.Remember me to one who lives there.He once was the true love of mine.Willem is taking the girl to the highest building in island No.28, however, neither of them knows how to get there.Willem asks his friend, Grick for directions, Grick helped them, and gave them a task.Although the girl wants to help, Willem insists on doing it by himself.Grick gave Willem a string of length n.Willem needs to do m operations, each operation has four parameters l, r, c1, c2, which means that all symbols c1 in range [l, r] (from l-th to r-th, including l and r) are changed into c2. String is 1-indexed.Grick wants to know the final string after all the m operations.", "input_spec": "The first line contains two integers n and m (1 ≤ n, m ≤ 100). The second line contains a string s of length n, consisting of lowercase English letters. Each of the next m lines contains four parameters l, r, c1, c2 (1 ≤ l ≤ r ≤ n, c1, c2 are lowercase English letters), separated by space.", "output_spec": "Output string s after performing m operations described above.", "sample_inputs": ["3 1\nioi\n1 1 i n", "5 3\nwxhak\n3 3 h x\n1 5 x a\n1 3 w g"], "sample_outputs": ["noi", "gaaak"], "notes": "NoteFor the second example:After the first operation, the string is wxxak.After the second operation, the string is waaak.After the third operation, the string is gaaak."}, "positive_code": [{"source_code": "gets\ns=gets.chomp\n$<.each{|_|\na,b,c,d=_.split\n(a.to_i-1...b.to_i).each{|i|s[i]=d if s[i]==c}\n}\nputs s"}, {"source_code": "n, m = gets.split(/\\s+/).map(&:to_i)\ns = gets.chomp\nm.times do\n\tl, r, c1, c2 = gets.split(/\\s+/)\n\tl = l.to_i - 1\n\tr = r.to_i - 1\n\t(l..r).each{ |i| s[i] = c2 if s[i] == c1 }\nend\nputs s\n"}, {"source_code": "str_length, changes = gets.chomp.split(' ').map(&:to_i)\nstring = gets.chomp\n\nchanges.times do\n l, r, target, new_val = gets.chomp.split(' ')\n l = l.to_i\n r = r.to_i\n\n (l..r).each do |i|\n string[i - 1] = new_val if string[i - 1] == target\n end\nend\n\nputs string\n"}, {"source_code": "n, m = gets.split.map(&:to_i)\ns = gets.chomp\nm.times do\n l, r, c1, c2 = gets.chomp.split\n l = l.to_i\n r = r.to_i\n (l..r).each do |i|\n if s[i-1] == c1\n s[i-1] = c2\n end\n end\nend\nputs s"}, {"source_code": "x,n=gets.split.map{|e| e.to_i}\ns=gets.chomp\nn.times{\n\tl,r,e1,e2=gets.split\n\tl=l.to_i\n\tr=r.to_i\n\te2=e2.chomp\n\t(l..r).each{|i|\n\t\tbreak if s.size<=i-1\n\t\ts[i-1]=e2 if s[i-1]==e1\n\t}\n\t\n}\nputs s"}, {"source_code": "n, m=gets.split.map &:to_i\ns=gets.chomp\nm.times{\n l, r, a, b=gets.split\n l=l.to_i-1\n r=r.to_i\n while l 0\r\n t -= 1\r\n n = gets.to_i\r\n ar = gets.split(' ').map(&:to_i).sort\r\n \r\n print(ar[n - 1] + ar[n - 2], \"\\n\")\r\nend"}], "negative_code": [], "src_uid": "b856eafe350fccaabd39b97fb18d9c2f"} {"nl": {"description": "A recently found Ancient Prophesy is believed to contain the exact Apocalypse date. The prophesy is a string that only consists of digits and characters \"-\".We'll say that some date is mentioned in the Prophesy if there is a substring in the Prophesy that is the date's record in the format \"dd-mm-yyyy\". We'll say that the number of the date's occurrences is the number of such substrings in the Prophesy. For example, the Prophesy \"0012-10-2012-10-2012\" mentions date 12-10-2012 twice (first time as \"0012-10-2012-10-2012\", second time as \"0012-10-2012-10-2012\").The date of the Apocalypse is such correct date that the number of times it is mentioned in the Prophesy is strictly larger than that of any other correct date.A date is correct if the year lies in the range from 2013 to 2015, the month is from 1 to 12, and the number of the day is strictly more than a zero and doesn't exceed the number of days in the current month. Note that a date is written in the format \"dd-mm-yyyy\", that means that leading zeroes may be added to the numbers of the months or days if needed. In other words, date \"1-1-2013\" isn't recorded in the format \"dd-mm-yyyy\", and date \"01-01-2013\" is recorded in it.Notice, that any year between 2013 and 2015 is not a leap year.", "input_spec": "The first line contains the Prophesy: a non-empty string that only consists of digits and characters \"-\". The length of the Prophesy doesn't exceed 105 characters.", "output_spec": "In a single line print the date of the Apocalypse. It is guaranteed that such date exists and is unique.", "sample_inputs": ["777-444---21-12-2013-12-2013-12-2013---444-777"], "sample_outputs": ["13-12-2013"], "notes": null}, "positive_code": [{"source_code": "# -*- encoding: utf-8 -*-\nrequire 'date'\n\ns = gets.chomp\nhash = Hash.new {0}\n\n(9 .. s.length).each do |idx|\n s2 = s[idx - 9 .. idx]\n match = s2.match(/(3[01]|[12][0-9]|0[1-9])-(1[0-2]|0[1-9])-(201[3-5])/)\n next if not match\n \n year, month, date = match[3].to_i, match[2].to_i, match[1].to_i \n\n if Date.valid_date? year, month, date\n d = Date.new year, month, date\n hash[d] += 1\n end\nend\nans = hash.max_by { |k, v| v }.first\nputs ans.strftime \"%d-%m-%Y\"\n"}, {"source_code": "#!/usr/bin/ruby1.9\n\nprophecy = STDIN.readline.strip\n\nmonthdays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n\t\t\t#Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec\n\ncount = {}\n\n(9..prophecy.length).each do |pos|\n\ts = prophecy[pos-9..pos]\n\tmatch = s.match(/(\\d\\d)-(\\d\\d)-(\\d\\d\\d\\d)/)\n\tnext if not match\n\tday, month, year = match.to_a[1..-1].collect {|s| s.to_i }\n\tnext if year < 2013 or year > 2015\n\tnext if month < 1 or month > 12\n\tnext if day < 1 or day > monthdays[month-1]\n\tif count.member? s\n\t\tcount[s] += 1\n\telse\n\t\tcount[s] = 1\n\tend\nend\narr = count.to_a.sort {|x, y| x[1]-y[1] }\nputs arr[-1][0]\n"}, {"source_code": "infile = STDIN\ndays_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\ninput = infile.readline.chomp\nfound = Hash.new {0}\nlength = \"13-12-2013\".length\n(0..(input.size - length)).each do |i|\n substr = input[i, length]\n if substr =~ /((\\d\\d)-(\\d\\d)-(\\d\\d\\d\\d))/\n d, m, y = $2.to_i, $3.to_i, $4.to_i\n if (2013..2015) === y && (1..12) === m && d > 0 && d <= days_in_month[m - 1]\n found[$1] += 1\n end\n end\nend\nputs found.max_by { |k, v| v }.first"}, {"source_code": "require 'date'\n\ndef run\n hash = Hash.new(0)\n\n line = ARGF.readline.chomp\n line.length.times do |i|\n begin\n str = line[i,10]\n if str =~ /[0-9]{2}-[0-9]{2}-[0-9]{4}/\n parse = Date.strptime(str, \"%d-%m-%Y\")\n hash[str] += 1 if parse.year.between?(2013, 2015)\n end\n rescue\n end\n end\n\n ret = hash.to_a.max_by { |item| item[1] }\n puts ret[0] if ret\nend\n\nrun if $0 == __FILE__\n"}, {"source_code": "def is_date?(str)\n if not str =~ /\\d{2}-\\d{2}-\\d{4}/ then return false end\n m = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n (2013..2015) === str[6, 4].to_i and (1..12) === str[3, 2].to_i and (1..m[str[3, 2].to_i]) === str[0, 2].to_i\nend\nhsh = Hash.new(0)\ns = gets.chop!\n(0..(s.size - 10)).each do |i|\n t = s[i, 10]\n if is_date? t then hsh[t] += 1 end\nend\nputs hsh.max_by { |k, v| v} .first"}, {"source_code": "# 777-444---21-12-2013-12-2013-12-2013---444-777\nrequire 'Date'\n\ndef days_in_month(year, month)\n\tDate.new(year, month, -1).day\nend\ncount = Hash.new(0)\n\nl = gets.chomp.split(\"-\")\n# puts \"#{l}\"\nl.each_cons(3) do |(d,m,y)|\n\th = d.to_i\n\tif h > 0\n\t\tm = m.to_i\n\t\tif m >= 1 && m <= 12\t\t\t\n\t\t\t\n\t\t\ty = y.length > 4 ? y[0..3].to_i : y.to_i\n\t\t\td = d.length > 2 ? d[-2..-1].to_i : d.to_i\n\n\t\t\tif y >= 2013 && y <= 2015 && (d <= days_in_month(y,m))\n\t\t\t\tcount[\"#{d.to_s.rjust(2,\"0\")}-#{m.to_s.rjust(2,\"0\")}-#{y}\"] += 1\n\t\t\tend\n\t\tend \n\tend\t\nend\nmax = count.values.max\n# puts \"#{count}\"\n\nputs count.detect{|k,v| v == max}[0]"}, {"source_code": "days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\ninput = gets.chomp\nfound = Hash.new {0}\nlength = \"13-12-2013\".length\n(0..(input.size - length)).each do |i|\n substr = input[i, length]\n if substr =~ /((\\d\\d)-(\\d\\d)-(\\d\\d\\d\\d))/\n d, m, y = $2.to_i, $3.to_i, $4.to_i\n if (2013..2015) === y && (1..12) === m && d > 0 && d <= days_in_month[m - 1]\n found[$1] += 1\n end\n end\nend\n\nputs found.max_by { |k, v| v }.first"}, {"source_code": "s=gets\n\nRUI=[1,3,5,7,8,10,12]\n\ndef day? month, day\n return false if day==0\n if month==2\n return day<=28\n end\n \n if RUI.include?month\n return day<=31\n end\n \n return day<=30\nend\n\ndef valid? date\n return false if not /(?\\d{2})-(?\\d{2})-(?\\d{4})/ =~ date\n \n return false if year.to_i<2013 || year.to_i>2015\n return day? month.to_i,day.to_i\nend\n\nhm={}\nhm.default=0\nans=\"\"\n\n(0...s.length-10).each do |i|\n key=s[i,10]\n hm[key]+=1 if valid? key\n ans=key if ans==\"\" || hm[key]>hm[ans]\nend\n\nputs ans\n\n"}, {"source_code": "s=gets\n\nRUI=[1,3,5,7,8,10,12]\n\ndef digit? c\n c>= ?0 && c<= ?9\nend\n\ndef day? month, day\n return false if day==0\n if month==2\n return day<=28\n end\n \n if RUI.include?month\n return day<=31\n end\n \n return day<=30\nend\n\ndef valid? date\n return false if !digit?(date[0]) || !digit?(date[1])\n day=date[0,2].to_i\n date=date[2..-1]\n \n return false if digit? date[0]\n date=date[1..-1]\n \n return false if !digit?(date[0]) || !digit?(date[1])\n month=date[0,2].to_i\n date=date[2..-1]\n \n return false if digit? date[0]\n date=date[1..-1]\n \n return false if !digit?(date[0]) || !digit?(date[1]) || !digit?(date[2]) || !digit?(date[3])\n year=date[0,4].to_i\n\n return false if year<2013 || year>2015\n return day? month,day\nend\n\nhm={}\n(0...s.length-10).each do |i|\n key=s[i,10]\n if valid? key\n hm[key]? hm[key]+=1:hm[key]=1\n end\nend\n\nans=\"\"\n\nhm.each do |key,value|\n if ans==\"\" || value>hm[ans]\n ans=key\n end\nend\n\nputs ans\n\n"}, {"source_code": "s=gets\n\nRUI=[1,3,5,7,8,10,12]\n\ndef digit? c\n c>= ?0 && c<= ?9\nend\n\ndef day? month, day\n return false if day==0\n if month==2\n return day<=28\n end\n \n if RUI.include?month\n return day<=31\n end\n \n return day<=30\nend\n\ndef valid? date\n return false if not /(?\\d{2})-(?\\d{2})-(?\\d{4})/ =~ date\n \n return false if year.to_i<2013 || year.to_i>2015\n return day? month.to_i,day.to_i\nend\n\nhm={}\nhm.default=0\nans=\"\"\n\n(0...s.length-10).each do |i|\n key=s[i,10]\n hm[key]+=1 if valid? key\n ans=key if ans==\"\" || hm[key]>hm[ans]\nend\n\nputs ans\n\n"}, {"source_code": "s=gets\n\nRUI=[1,3,5,7,8,10,12]\n\ndef digit? c\n c>= ?0 && c<= ?9\nend\n\ndef day? month, day\n return false if day==0\n if month==2\n return day<=28\n end\n \n if RUI.include?month\n return day<=31\n end\n \n return day<=30\nend\n\ndef valid? date\n return false if !digit?(date[0]) || !digit?(date[1])\n day=date[0,2].to_i\n date=date[2..-1]\n \n return false if digit? date[0]\n date=date[1..-1]\n \n return false if !digit?(date[0]) || !digit?(date[1])\n month=date[0,2].to_i\n date=date[2..-1]\n \n return false if digit? date[0]\n date=date[1..-1]\n \n return false if !digit?(date[0]) || !digit?(date[1]) || !digit?(date[2]) || !digit?(date[3])\n year=date[0,4].to_i\n\n return false if year<2013 || year>2015\n return day? month,day\nend\n\nhm={}\nhm.default=0\nans=\"\"\n\n(0...s.length-10).each do |i|\n key=s[i,10]\n hm[key]+=1 if valid? key\n ans=key if ans==\"\" || hm[key]>hm[ans]\nend\n\nputs ans\n\n"}, {"source_code": "s=gets\n\nRUI=[1,3,5,7,8,10,12]\n\ndef day? month, day\n return false if day==0\n if month==2\n return day<=28\n end\n \n if RUI.include?month\n return day<=31\n end\n \n return day<=30\nend\n\ndef valid? date\n return false if not /(?\\d{2})-(?\\d{2})-(?\\d{4})/ =~ date\n \n return false if year.to_i<2013 || year.to_i>2015\n return day? month.to_i,day.to_i\nend\n\nhm={}\nhm.default=0\nans=nil\n\n(0...s.length-10).each do |i|\n key=s[i,10]\n hm[key]+=1 if valid? key\n ans=key if hm[key]>hm[ans]\nend\n\nputs ans\n\n"}, {"source_code": "s=gets\nw = {\n\n}\nw.default=0\nans=-1;\nj = -1;\nm=[0,31,28,31,30,31,30,31,31,30,31,30,31]\n\ndef good?(s,m) \n\ta=s[0..1].to_i\n\tb=s[3..4].to_i\n\tc=s[6..9].to_i\n\tif c>=2013 and c<=2015 and b>=1 and b<=12 and a>=1 and a<=m[b] then\n\t\treturn true\n\tend\n\treturn false\nend\n\ns.length.times do |i|\n\tif s.length-10 <= i then\n\t\tbreak\n\tend\n\tt = s[i,10]\n\tif t =~ /\\d{2}-\\d{2}-\\d{4}/ and good?(t,m) then\n\t\tw[t]+=1\n\t\tif(ans ans\n ans = d\n end\n end\nend\n\nputs ans.strftime \"%d-%m-%Y\"\n"}, {"source_code": "# -*- encoding: utf-8 -*-\nrequire 'date'\n\ns = gets.chomp\ndate = nil\n\n(9 .. s.length).each do |idx|\n s2 = s[idx - 9 .. idx]\n match = s2.match(/(3[01]|[12][0-9]|0[1-9])-(1[0-2]|0[1-9])-(201[3-5])/)\n next if not match\n \n year, month, date = match[3].to_i, match[2].to_i, match[1].to_i \n\n if Date.valid_date? year, month, date\n d = Date.new year, month, date\n if date.nil? or d > date\n date = d\n end\n end\nend\n\nputs date.strftime \"%d-%m-%Y\"\n"}, {"source_code": "def is_date?(str)\n if not str =~ /\\d{2}-\\d{2}-\\d{4}/ then false end\n m = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n (2013..2015) === str[6, 4].to_i and (1..12) === str[3, 2].to_i and (1..m[str[3, 2].to_i]) === str[0, 2].to_i\nend\nhsh = Hash.new(0)\ns = gets.chop!\n(0..(s.size - 10)).each do |i|\n t = s[i, 10]\n if is_date? t then hsh[t] += 1 end\nend\nputs hsh.max_by { |k, v| v} .first\n"}, {"source_code": "# 777-444---21-12-2013-12-2013-12-2013---444-777\nrequire 'Date'\n\ndef days_in_month(year, month)\n\tDate.new(year, month, -1).day\nend\ncount = Hash.new(0)\n\nl = gets.chomp.split(\"-\")\n# puts \"#{l}\"\nl.each_cons(3) do |(d,m,y)|\n\th = d.to_i\n\tif h > 0\n\t\tm = m.to_i\n\t\tif m >= 1 && m <= 12\n\t\t\ty = y.to_i\n\t\t\tif y >= 2013 && y <= 2015\n\t\t\t\tif(h <= days_in_month(y,m))\n\t\t\t\t\tcount[\"#{d}/#{m}/#{y}\"] += 1\n\t\t\t\telsif(d[-2..-1].to_i <= days_in_month(y,m))\n\t\t\t\t\tcount[\"#{d[-2..-1]}-#{m}-#{y}\"] += 1\n\t\t\t\tend\n\n\t\t\tend\n\t\tend \n\tend\t\nend\nmax = count.values.max\n# puts \"#{count}\"\n\nputs count.detect{|k,v| v == max}[0]"}, {"source_code": "# 777-444---21-12-2013-12-2013-12-2013---444-777\nrequire 'Date'\n\ndef days_in_month(year, month)\n\tDate.new(year, month, -1).day\nend\ncount = Hash.new(0)\n\nl = gets.chomp.split(\"-\")\n# puts \"#{l}\"\nl.each_cons(3) do |(d,m,y)|\n\th = d.to_i\n\tif h > 0\n\t\tm = m.to_i\n\t\tif m >= 1 && m <= 12\n\t\t\ty = y.to_i\n\t\t\tif y >= 2013 && y <= 2015\n\t\t\t\tif(h <= days_in_month(y,m))\n\t\t\t\t\tcount[\"#{d}/#{m}/#{y}\"] += 1\n\t\t\t\telsif(d[-2..-1].to_i <= days_in_month(y,m))\n\t\t\t\t\tcount[\"#{d[-2..-1]}-#{m}-#{y}\"] += 1\n\t\t\t\tend\n\n\t\t\tend\n\t\tend \n\tend\t\nend\nmax = count.values.max\nputs \"#{count}\"\n\nputs count.detect{|k,v| v == max}[0]"}, {"source_code": "# 777-444---21-12-2013-12-2013-12-2013---444-777\nrequire 'Date'\n\ndef days_in_month(year, month)\n\tDate.new(year, month, -1).day\nend\ncount = Hash.new(0)\n\nl = gets.chomp.split(\"-\")\n# puts \"#{l}\"\nl.each_cons(3) do |(d,m,y)|\n\th = d.to_i\n\tif h > 0\n\t\tm = m.to_i\n\t\tif m >= 1 && m <= 12\n\t\t\ty = y.to_i\n\t\t\tif y >= 2013 && y <= 2015\n\t\t\t\t# if ( || h[-2..-1].to_i <= days_in_month(y,m))\n\t\t\t\tif(h <= days_in_month(y,m))\n\t\t\t\t\tcount[\"#{d}/#{m}/#{y}\"] += 1\n\t\t\t\tend\n\t\t\t\tif(d[-2..-1].to_i <= days_in_month(y,m))\n\t\t\t\t\tcount[\"#{d[-2..-1]}/#{m}/#{y}\"] += 1\n\t\t\t\tend\n\n\t\t\tend\n\t\tend \n\tend\t\nend\nmax = count.values.max\n# puts \"#{count}\"\n\nputs count.detect{|k,v| v == max}[0]"}, {"source_code": "days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\ninput = gets.chomp\nfound = Hash.new {0}\nlength = \"13-12-2013\".length\n(0..(input.size - length)).each do |i|\n substr = input[i, length]\n if substr =~ /((\\d\\d)-(\\d\\d)-(\\d\\d\\d\\d))/\n d, m, y = $2.to_i, $3.to_i, $4.to_i\n if (2013...2015) === y && (1..12) === m && d > 0 && d <= days_in_month[m - 1]\n found[$1] += 1\n end\n end\nend\n\nputs found.max_by { |k, v| v }.first"}, {"source_code": "s=gets\nw,ans,j={},-1,0\nw.default=0\ndef good?(day,month,year) \n\tbegin\n\t\tt = Time.new(year,month,day)\n\t\tif t<=Time.new(2015,12,31) and t>=Time.new(2013,1,1) then\n\t\t\treturn true\n\t\tend\n\trescue\n\t\treturn false\n\tend\n\treturn false\nend\ns.scan(/(?\\d{2})-(?\\d{2})-(?\\d{4})/).each do |item|\n\tif(good?(item[0].to_i,item[1].to_i,item[2].to_i)) then\n\t\tw[$&]+=1\n\t\tif ans=Time.new(2013,1,1) then\n\t\t\treturn true\n\t\tend\n\trescue\n\t\treturn false\n\tend\n\treturn false\nend\ns.scan(/(?\\d{2})-(?\\d{2})-(?\\d{4})/).each do |item|\n\tif(good?(item[0].to_i,item[1].to_i,item[2].to_i)) then\n\t\tt = \"\" << item[0]<<'-'<< item[1]<< '-' << item[2]\n\t\tw[t]+=1\n\t\tif ansmax \n\tend\nend\nputs max\n\t\t\n"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nmax = 0\nfor i in 0...n\n xor = 0\n for j in i...n\n xor = xor^a[j]\n if xor > max then max = xor end\n end\nend\nprint max"}, {"source_code": "n = gets.to_i\na = gets.split.map(&:to_i)\nans = 0\nfor i in 0...n\n\tsum = 0\n\tfor j in i...n\n\t\tsum = sum^a[j]\n\t\tif sum > ans then ans = sum end\n\tend\nend\nprint ans"}, {"source_code": "#!/usr/bin/env ruby\nn = gets.to_i\nt = gets.split.map(&:to_i)\nresult = 0\nn.times do |d|\n (0..(n - d - 1)).each do |i|\n x = 0\n (i..(i + d)).each do |j|\n x ^= t[j]\n end\n result = [result, x].max\n end\nend\nputs result"}, {"source_code": "n = gets.chomp.to_i\na = gets.chomp.split.map(&:to_i)\nret = []\nfor i in 0...n do\n t = 0\n for j in i...n do\n t ^= a[j]\n ret << t \n end\nend\nputs ret.max"}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\nres=a.max\nfor i in 0..n-1 do\n t=a[i]\n for j in i+1..n-1 do\n t^=a[j]\n res=t if res ans\n\t}\nend\nputs ans\n"}, {"source_code": "\n\nn = gets.chomp.to_i\nline = gets.chomp.split(\" \").collect! {|x| x.to_i}\nmax = 0\n(0..(n-1)).each {|k|\n (k..(n-1)).each { |t|\n p = 0\n (k..t).each {|q|\n p ^= line[q]\n }\n if p> max\n\tmax = p\n end\n }\n \n}\n\nputs max"}, {"source_code": "n = gets.to_i\nx = gets.split.map(&:to_i).unshift(0)\nn.times { |i| x[i+1] ^= x[i] }\nmx = 0\n1.upto(n) do |i|\n\ti.upto(n) do |j|\n\t\tmx = [mx, x[j] ^ x[i-1]].max\n\tend\nend\nputs mx\n"}, {"source_code": "t = gets.to_i\n\na = gets.split.collect{|x| x.to_i}\n\nr = 0\n\ni = 0\nwhile i < a.size\n\tj = i\n\twhile j < a.size\n\t\tt = a[i]\n\t\t\n\t\tk = i+1\n\n\t\twhile k <= j\n\t\t\t\n\t\t\tt ^= a[k]\n\t\t\tk += 1\n\t\tend\n\n\t\tr = [t,r].max\n\t\tj += 1\n\tend\n\ti+=1\nend\n\nputs r"}], "negative_code": [{"source_code": "#!/usr/bin/env ruby\nn = gets.to_i\nt = gets.split.map(&:to_i)\nresult = 0\nn.times do |d|\n (0..(n - d - 1)).each do |i|\n x = 0\n (i..(i + d)).each do |j|\n x |= t[j]\n end\n result = [result, x].max\n end\nend\nputs result"}, {"source_code": "n=gets.to_i\na=gets.split.map(&:to_i)\nres=0\nfor i in 0..n-1 do\n t=a[i]\n for j in i+1..n-1 do\n t^=a[j]\n end\n res=t if res ans\nend\nputs ans\n"}, {"source_code": "\n\nn = gets.chomp.to_i\nline = gets.chomp.split(\" \").collect! {|x| x.to_i}\nmax = 0\n(0..(n-1)).each {|k|\n p = 0\n (k..(n-1)).each { |t|\n p ^= line[t]\n }\n if p> max\n\tmax = p\n end\n}\n\nputs max"}, {"source_code": "n = gets.to_i\nx = gets.split.map(&:to_i)\nn.times { |i| i > 0 ? x[i] ^= x[i-1] : 1 }\nmx = x[0]\n1.upto(n-1) do |i|\n\ti.upto(n-1) do |j|\n\t\tmx = [mx, x[j] ^ x[i-1]].max\n\tend\nend\nputs mx\n"}, {"source_code": "n = gets.to_i\nx = gets.split.map(&:to_i)\nn.times { |i| i ? x[i] ^= x[i-1] : _ }\nmx = x[0]\n1.upto(n-1) do |i|\n\ti.upto(n-1) do |j|\n\t\tmx = [mx, x[j] ^ x[i-1]].max\n\tend\nend\nputs mx\n"}], "src_uid": "a33991c9d7fdb4a4e00c0d0e6902bee1"} {"nl": {"description": "In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23. Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.", "input_spec": "The first line of the input contains integer number n (1 ≤ n ≤ 105), the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 106 .", "output_spec": "Write n lines, each line should contain a cell coordinates in the other numeration system.", "sample_inputs": ["2\nR23C55\nBC23"], "sample_outputs": ["BC23\nR23C55"], "notes": null}, "positive_code": [{"source_code": "n = gets.to_i\n\nn.times do\n\texpression = gets\n\tif expression.match /R(\\d+)C(\\d+)/\n\t\tresult = \"\"\n\t\tcolumn = $2.to_i\n\t\tuntil column == 0\n\t\t\tmod = column % 26\n\t\t\tif mod == 0\n\t\t\t\tmod = 26\n\t\t\t\tcolumn -= mod\n\t\t\tend\n\n\t\t\tresult << (mod + 64).chr\n\t\t\tcolumn /= 26\n\t\tend\n\t\tputs result.reverse + $1\n\telse\n\t\texpression.match /([A-Z]+)(\\d+)/\n\t\tcolumn = 0\n\t\t$1.each_byte { |chr| column = column * 26 + chr - 64 }\n\t\tputs \"R\" + $2 + \"C\" + column.to_s\n\tend\nend"}, {"source_code": "d=[*?A..'BEAAA'];def t s;p=a=0;s.each_char{|c|a+=(c.ord-?A.ord+1)*26**p;p+=1};a;end;gets.to_i.times{l=gets;puts (if /R(\\d+)C(\\d+)/.match l;\"#{d[$2.to_i-1]}#{$1}\";else;a,b=l.scan(/([A-Z]+)(\\d+)/)[0];\"R#{b}C#{t a.reverse}\";end)}"}, {"source_code": "def f x;x<26?((x+65).chr):f(x/26-1)+(x%26+65).chr;end;gets.to_i.times{x=gets;puts /R(\\d+)C(\\d+)/.match(x)?f($2.to_i-1)+$1:(y=0;v=1;/(\\D+)(\\d+)/.match x;$1.reverse.each_char{|c|y+=(c.ord-64)*v;v*=26};\"R#{$2}C#{y}\")}\n"}, {"source_code": "d=[*?A..'BEAAA'];def t s;p=a=0;s.each_char{|c|a+=(c.ord-?A.ord+1)*26**p;p+=1};a;end;gets.to_i.times{l=gets;puts (if /R(\\d+)C(\\d+)/.match l;\"#{d[$2.to_i-1]}#{$1}\";else;a,b=l.scan(/([A-Z]+)(\\d+)/)[0];\"R#{b}C#{t a.reverse}\";end)}"}, {"source_code": "num = gets()\n\ndef is_excel_format(name)\n !!(name.match /^[A-Z]+[0-9]+$/)\nend\n\ndef to_excell(rowcol)\n rowcol =~ /R(\\d+)C(\\d+)/\n to_letters($2.to_i) + $1\nend\n\ndef to_rowcol(excell)\n excell =~ /([A-Z]+)(\\d+)/\n \"R\" + $2 + \"C\" + to_nums($1).to_s\nend\n\ndef to_letters(num, base=26)\n answer = []\n while num > 0\n mod = (num -1) % 26\n answer.push mod\n num = (num - mod) / base\n end\n answer.reverse.map{|x| (x+65).chr }.join\nend\n\ndef to_nums(letters)\n letters.reverse.split('').map.with_index{|x, i|\n (x.ord - 65 + 1) * 26 ** i # convert from letter to number, base 26\n }.reduce{|a,b| a+b}\nend\n\nnum.to_i.times do |index|\n line = gets()\n if is_excel_format(line)\n puts to_rowcol(line)\n else\n puts to_excell(line)\n end\nend\n"}, {"source_code": "def solve(input)\n if (/R\\d+C\\d+/ =~ input) == nil\n fromBase26(input)\n else\n toBase26(input)\n end\nend\n\ndef fromBase26(input)\n charByte = \"A\".bytes[0]\n numByte = \"1\".bytes[0]\n letters = Array.new\n numbers = Array.new\n\n input.chomp.each_byte do |byte|\n if byte < charByte\n numbers.unshift(byte-(numByte-1))\n else\n letters.unshift(byte-(charByte-1))\n end\n end\n\n rows = arrayToInt(numbers, 10)\n cols = arrayToInt(letters, 26)\n \"R#{rows}C#{cols}\"\nend\n\ndef toBase26(input)\n input[0] = \"\"\n rows, cols = input.split(\"C\")\n val = cols.to_i\n digits = []\n while val != 0\n digits.unshift((val-1)%26)\n val = (val-1)/26\n end\n cols = \"\"\n for digit in digits\n val = digit + (\"A\".bytes[0])\n cols += val.chr\n end\n cols + \"#{rows}\"\nend\n\ndef arrayToInt(arr, base)\n curr = 1\n int = 0\n for val in arr\n int += val*curr\n curr *= base\n end\n int\nend\n\nn = gets.to_i\nans = Array.new(n, nil)\nfor i in (0...n)\n ans[i] = solve(gets)\nend\n\nfor answer in ans\n puts answer\nend\n"}, {"source_code": "ask = gets.to_i\nask.times do\n str = gets\n if str =~ /\\D\\d+\\D\\d+/ #R23C55\n rows, columns = str.scan(/\\d+/)\n num = columns.to_i\n str = ''\n while num >= 1 do\n rem = num % 26\n str = (rem == 0) ? 'Z' + str : (rem + 64).chr + str\n num /= 26\n num -= 1 if rem == 0\n end\n ans = str + rows\n else #BC23\n columns, rows = str.scan(/\\D+|\\d+/)\n str = columns.reverse\n num = 0\n (0...str.length).each do |i|\n num += (str[i].ord - 64) * 26**i\n end\n ans = 'R' + rows + 'C' + num.to_s\n end\n puts ans\nend"}, {"source_code": "alph = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']\n\nn = gets.chomp.to_i\n\nlines = []\nn.times {lines << gets.chomp}\nlines.each do |l|\n if (l =~ /R\\d+C\\d+/).nil?\n col = 0\n colstr = l.scan(/[A-Z]+/).first.split(\"\")\n colstr.each_with_index do |ch, index|\n col += (alph.index(ch) + 1) * (26 ** (colstr.length - index - 1))\n end\n row = l.scan(/[0-9]+/).first\n puts \"R#{row}C#{col}\"\n else\n row = l.scan(/R([0-9]+)/).flatten.first\n col = l.scan(/C([0-9]+)/).flatten.first.to_i\n cols = \"\"\n delta = 0\n loop do\n remainder = (col % 26)\n col = col / 26\n unless col == 0 and remainder - delta == 0\n cols = alph[remainder - 1 - delta] + cols\n end\n delta = remainder - delta <= 0 ? 1 : 0\n break if col == 0\n\n end\n puts \"#{cols}#{row}\"\n end\nend\n"}, {"source_code": "number_of_input=gets.to_i\ninput_arry=[]\ni_a=0\n\n\nloop do\n\tbreak if i_a==number_of_input\n\tinput_arry[i_a]=gets.chomp\n\ti_a +=1\nend\n\nvalue=(1..26)\nalph=('A'..'Z')\nvalue=[*value]\nalph=[*alph]\n@hash={}\ni_b=0\n\nloop do\n\tbreak if i_b==26\n\t@hash[alph[i_b]]=value[i_b]\n\ti_b+=1\nend\t\n\n\n\ndef S1_to_S2 (a1)\n\tr=a1[0]\n\ti=1\n\t\n\tloop do\n\t\tbreak if a1[i].to_i !=0\n\t\tr =r + a1[i]\n\t\ti+=1\n\tend\n\n\tr1=r.length\n\tr2=a1.length\n\tc1= a1[0 .. (r1-1)].reverse\n\trow= a1[ r1 .. r2].to_i\n\tc=0\n\ti=0\n\t\n\tloop do\n\t\tbreak if i==c1.length\n\t\tc= c + (@hash[c1[i]]*(26**i))\n\t\ti+=1\n\tend\n\t\n\tputs \"R#{row}C#{c}\"\nend\n\ndef S2_to_S1 (a1)\n\ts2=a1.split(\"\").map{|s| s}\n\tr3=3\n\ts2.find {|i| r3=s2.index(i)+1 if i=='C'}\n\ti=s2[r3 .. (s2.length - 1)].join.to_i\n\trow=s2[1 .. (r3-2)].join.to_i\n\tstr_c=\"\"\n\t\n\tloop do\n\t\ti=i-26 if i>25 && i%26==0\n\t\tif i%26==0\n\t\tstr_c = str_c + \"Z\"\n\t\telse\n\t\tstr_c = str_c + @hash.key(i%26)\n\t\tend\n\t\ti=i/26\n\t\tbreak if i==0\n\tend\n\t\n\tstr_c=str_c.reverse\n\tputs \"#{str_c}#{row}\"\nend\n\t\ni_c =0\n\nloop do\n break if i_c==input_arry.length\n\tinput1=input_arry[i_c]\n\t\n\tif \tinput1[0]!='R' || (input1[0]=='R' && input1[1].to_i==0) \n\t\tS1_to_S2 (input1)\n\telsif input1[0]=='R' && input1[1].to_i!=0\n\t\tk2=input1.split(\"\")\n\t\tk=0\n\t\tk2.find{|s| k=1 if s=='C'}\n\t\tif k==1\n\t\t\tS2_to_S1(input1)\n\t\telse\n\t\t\tS1_to_S2 (input1)\n\t\tend\t\n\tend\n\t\n\ti_c +=1\n\t\nend"}, {"source_code": "def toBase26 number\n converted = []\n while (number > 0)\n remainder = (number - 1) % 26\n converted << (remainder + 65).chr\n number = (number - remainder) / 26;\n end\n return converted.join.reverse;\nend\ndef fromBase26 number\n s = 0\n if number.length > 0\n s = number[0].ord - 65 + 1;\n for i in 1..number.length-1\n s *= 26;\n s += (number[i].ord - 65 + 1);\n end\n end\n return s\nend\nt = gets.to_i\nfor i in 1..t\n s = gets.chomp\n len = s.length\n format = false\n if s[0] == 'R' && s[1].ord < 65 && len >= 4\n j = 2\n r = s[1].to_i\n while j < len && s[j].ord < 65\n r = r*10 + s[j].to_i\n j += 1\n end\n if j!= len && s[j] == 'C'\n c = s[j + 1].to_i\n j += 2\n while j < len && s[j].ord < 65\n c = c*10 + s[j].to_i\n j += 1\n end\n format = true\n else\n c = r\n r = 'R'\n end\n else\n j = 0\n while s[j].ord >= 65\n j += 1\n end\n r = s[0,j]\n c = s[j].to_i\n j += 1\n while j < len\n c = c*10 + s[j].to_i\n j += 1\n end\n end\n\n # true RXCX \n # false ABCXXX\n if !format\n puts \"R#{c}C#{fromBase26 r}\"\n else\n puts \"#{toBase26 c}#{r}\"\n end\nend"}, {"source_code": "def whitespace?(c)\n !\" \\r\\n\".index(c).nil?\nend\ndef read_word\n s = ''\n c = ' '\n c = $stdin.getc while whitespace?(c)\n begin\n s << c\n c = $stdin.getc\n end while !whitespace?(c)\n return s\nend\ndef read_int\n read_word.to_i\nend\ndef read_float\n read_word.to_f\nend\ndef read_line\n s = ''\n while s.count(\"\\r\\n\")==s.length\n s = $stdin.readline\n end\n return s\nend\n\n$base = ?A\n\ndef to_hex(n)\n res = \"\"\n while n>0\n res << ((n-1)%26 + $base).chr\n n = (n-1) / 26\n end\n res.reverse\nend\ndef from_hex(s)\n res = 0\n s.each_byte do |b|\n res = res*26 + (b - $base + 1)\n end\n res\nend\nn = read_int\nn.times do\n s = read_line\n if s =~ /R(\\d+)C(\\d+)/\n x,y = $1,$2.to_i\n puts to_hex(y)+x\n else\n i = 0\n y = \"\"\n while s[i..i]>='A'\n y << s[i]\n i+=1\n end\n puts \"R#{s[i...-1]}C#{from_hex(y)}\"\n end\nend\n\n"}, {"source_code": "letters = %w(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z)\n\ngets.to_i.times do\n input = gets.strip\n if input =~ /R(\\d)+C/\n codes = input.gsub(\"R\", \"\").split(\"C\").map(&:to_i)\n ans = \"\"\n codes[1]\n while codes[1] > 0 do\n ans << letters[(codes[1]%26-1)]\n if codes[1]%26!=0\n codes[1] /= 26\n else\n codes[1] /= 26\n codes[1] -=1\n end\n end\n\n\n puts \"#{ans.reverse}#{codes[0]}\"\n else\n code = input.match(/([A-Z])+/).to_s\n sum = 0\n code.chars.each_with_index do |el, i|\n sum += (1+letters.index(el)) * (26**(code.size-i-1))\n end\n puts \"R#{input.match(/([0-9])+/)}C#{sum}\"\n end\nend"}, {"source_code": " \ndef f x;x<26?((x+65).chr):f(x/26-1)+(x%26+65).chr;end;gets.to_i.times{x=gets;puts /R(\\d+)C(\\d+)/.match(x)?f($2.to_i-1)+$1:(y=0;v=1;/(\\D+)(\\d+)/.match x;$1.reverse.each_char{|c|y+=(c.ord-64)*v;v*=26};\"R#{$2}C#{y}\")}"}, {"source_code": "def to_spread_number(number)\n spread = ''\n while number > 0\n rem = number % 26\n number /= 26\n if rem == 0\n rem = 26\n number -= 1\n end\n spread = (rem + 64).chr + spread\n end\n spread\nend\n\ndef to_number(spread_num)\n num, m = 0, 1\n spread_num.reverse.each_char { |c|\n num += (c.ord - 64) * m\n m *= 26\n }\n num\nend\n\nfor _ in 1..Integer(gets.chomp)\n cell = gets.chomp\n coords = cell.scan(/R(\\d+)C(\\d+)/).first\n if coords\n puts to_spread_number(coords[1].to_i) + coords[0]\n else\n cell.scan(/([A-Z]+)(\\d+)/) { puts \"R#{$2}C#{to_number($1)}\" }\n end\nend"}, {"source_code": "d=[*?A..'BEAAA'];def t s;p=a=0;s.each_char{|c|a+=(c.ord-?A.ord+1)*26**p;p+=1};a;end;gets.to_i.times{l=gets;puts (if /R(\\d+)C(\\d+)/.match l;\"#{d[$2.to_i-1]}#{$1}\";else;a,b=l.scan(/([A-Z]+)(\\d+)/)[0];\"R#{b}C#{t a.reverse}\";end)}\n"}, {"source_code": "def to_digits(s)\n\tres = 0\n\ts.each_byte { |a|\n \tres = res * 26 + a - 'A'[0].ord + 1\n\t}\n\tres\nend\n\ndef to_letters(n)\n\ts = ''\n\twhile n > 0 do\n \tr = n % 26\n \tif r == 0\n \t\tad = 'Z'\n\t\telse\n\t\t\tad = ('A'[0].ord + r - 1).chr\n\t\tend\n \tn = (n - 1) / 26\n \ts = s + ad\n\tend\n\ts.reverse\nend\n\nn = gets.to_i\nn.times do \n\ts = gets\n\tif /^(\\D+)(\\d+)$/i =~ s\n\t\tm = Regexp.last_match\n\t\tputs \"R#{m[2]}C#{to_digits(m[1])}\"\n elsif /^R(\\d+)C(\\d+)$/i =~ s\n \tm = Regexp.last_match\n \tputs \"#{to_letters(m[2].to_i)}#{m[1]}\"\n\tend\nend"}, {"source_code": "# $stdin = File.open('in')\nn = gets.to_i\n\ndef conv(num)\n\tres = ''\n\twhile num > 0\n\t\tif num % 26 == 0\n\t\t\tres.concat('Z')\n\t\t\tnum -= 1\n\t\telse\n\t\t\tres.concat((num % 26 + 65 - 1).chr)\n\t\tend\n\t\tnum /= 26\n\tend\n\tres.reverse!\nend\n\nfor i in 0...n \n\ts = gets\n\tm = /R(\\d+)C(\\d+)/.match(s)\n\tif m != nil\n\t\tnum = m[2].to_i\n\t\tputs \"#{conv(num)}#{m[1]}\"\n\telse\n\t\tm = /([A-Z]+)(\\d+)/.match(s)\n\t\tcolumn = m[1]\n\t\tcolumn.reverse!\n\t\tcnt, base = 0, 1\n\t\tcolumn.length.times do |i|\n\t\t\tcnt += (column[i].ord - 'A'.ord + 1) * base\n\t\t\tbase *= 26\n\t\tend\n\t\tputs \"R#{m[2]}C#{cnt}\"\n\tend\nend"}, {"source_code": "gets\nwhile i = gets do\n\tif i =~ /^([A-Z]+)(\\d+)$/\n\t\tc = 0\n\t\t$1.each_byte {|chr| c = c * 26 + chr - 64}\n\t\tprint \"R\", $2, \"C\", c, \"\\n\"\n\telsif i =~ /^R(\\d+)C(\\d+)/\n\t\tc = $2.to_i\n\t\tstr=\"\"\n\t\twhile c != 0\n\t\t\tmod = c % 26\n\t\t\tif mod == 0 then\n\t\t\t\tmod = 26\n\t\t\t\tc -= mod\n\t\t\tend\n\t\t\tstr << (mod + 64).chr\n\t\t\tc /= 26\n\t\tend\n\t\tprint str.reverse, $1, \"\\n\"\n\tend\nend"}, {"source_code": "gets; while i = gets do\n\tif i =~ /^([A-Z]+)(\\d+)$/\n\t\tc = 0\n\t\t$1.each_byte {|chr| c = c * 26 + chr - 64}\n\t\tprint \"R\", $2, \"C\", c, \"\\n\"\n\telsif i =~ /^R(\\d+)C(\\d+)/\n\t\tc = $2.to_i\n\t\tstr=\"\"\n\t\twhile c != 0\n\t\t\tmod = c % 26\n\t\t\tif mod == 0 then\n\t\t\t\tmod = 26\n\t\t\t\tc -= mod\n\t\t\tend\n\t\t\tstr << (mod + 64).chr\n\t\t\tc /= 26\n\t\tend\n\t\tprint str.reverse, $1, \"\\n\"\n\tend\nend"}, {"source_code": "\ndef is_num(ch)\n return ((ch >= \"0\") and (ch <= \"9\")) ? true : false\nend\ndef is_pattern2(str)\n len = str.length()\n return false if str[0]!=\"R\"\n return false if not(is_num(str[1]))\n for i in (2..len-1)\n return true if str[i]==\"C\"\n end\n\n false\nend\n\nnum_line = gets.to_i()\nnum_line.times {\n str = gets().chomp()\n len = str.length()\n if is_pattern2(str)\n\n for i in (2..len-1)\n if str[i]==\"C\"\n c_index = i\n break\n end\n end\n\n row = str[1..c_index-1].to_i\n col = str[c_index+1..len-1].to_i\n\n col_string = []\n\n loop do\n col -= 1\n a= (col%26 + 65).chr\n col_string.unshift(a)\n col /= 26\n break if col <=0\n end\n puts \"#{col_string.join('')}#{row}\"\n else\n\n for i in (1..len-1)\n if is_num(str[i])\n num_index = i\n break\n end\n end\n row = str[num_index..len-1].to_i\n col = 0\n for i in (0..num_index-1)\n base = 26**i\n col += base * (str[num_index-1-i].ord-\"A\".ord+1)\n end\n puts \"R#{row}C#{col}\"\n end\n}\n"}, {"source_code": "n = gets\nn = Integer(n)\nwhile n>0\n string = gets\n m = string.size()\n index = 0\n while ('A'..'Z').include? string[index]\n index += 1\n end\n while !(('A'..'Z').include? string[index])\n index += 1\n if index >= m\n break;\n end\n end\n if index == m\n index = 0\n while ('A'..'Z').include? string[index]\n index+=1\n end\n index -= 1\n c = 0\n count = 1\n til = index\n until index < 0\n c += (string[index].unpack(\"c\")[0] - 'A'.unpack(\"c\")[0] + 1) * count\n index -= 1\n count *= 26\n end\n puts \"R#{string[til+1..m].chomp}C#{c}\"\n else\n index = string.index(\"C\")\n count = Integer(string[index+1..m-2].chomp)\n k = \"\"\n while count > 0\n while count % 26 == 0 && count > 0\n k = \"Z\" + k\n count /= 26\n count -= 1\n end\n if count>0\n k = (count % 26 + 64).chr + k\n count /= 26 \n end\n end\n puts \"#{k}#{string[1..index-1]}\"\n end\n n-=1\nend"}, {"source_code": "#!/usr/bin/env ruby\n\ndef convert(str)\n if str =~ /^R(\\d+)C(\\d+)$/\n rows, cols = $1.to_i, $2.to_i\n a = []\n loop do\n cols -= 1\n a.unshift cols % 26\n cols /= 26\n break if cols == 0\n end\n \"#{a.map{|i| (i + 65).chr}.join}#{rows}\"\n elsif str =~ /^([A-Z]+)(\\d+)$/\n cols = 0\n $1.size.times do |i|\n cols += ($1.unpack('C*')[-i - 1] - 64) * (26 ** i)\n end\n \"R#{$2}C#{cols}\"\n else\n \"invalid arg\"\n end\nend\n\nn = gets.to_i\nlines = []\nn.times do\n lines << convert(gets.chomp)\nend\n\nlines.each{|s| puts s}\n"}, {"source_code": "require 'scanf'\nn = STDIN.gets.scanf('%d').first\nwhile n>0\n l = STDIN.gets.chop\n row = nil\n col = nil\n /R(?\\d+)C(?